blob: a84964833b905e06c85ac32862d4cf4e9c699cd4 [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 Szeredie2aa2e22005-07-15 13:31:36 +00009
10/* For pthread_rwlock_t */
11#define _GNU_SOURCE
12
13#include "fuse.h"
14#include "fuse_lowlevel.h"
Miklos Szeredi0f62d722005-01-04 12:45:54 +000015#include "fuse_compat.h"
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000016
Miklos Szeredi0f62d722005-01-04 12:45:54 +000017#include <stdio.h>
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000018#include <string.h>
Miklos Szeredi97c61e92001-11-07 12:09:43 +000019#include <stdlib.h>
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000020#include <unistd.h>
Miklos Szeredi97c61e92001-11-07 12:09:43 +000021#include <limits.h>
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000022#include <errno.h>
Miklos Szeredi0f62d722005-01-04 12:45:54 +000023#include <assert.h>
24#include <stdint.h>
Miklos Szeredie2aa2e22005-07-15 13:31:36 +000025#include <pthread.h>
Miklos Szeredi019b4e92001-12-26 18:08:09 +000026#include <sys/param.h>
Miklos Szerediab974562005-04-07 15:40:21 +000027#include <sys/uio.h>
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000028
Miklos Szeredi0f62d722005-01-04 12:45:54 +000029/* FUSE flags: */
30
31/** Enable debuging output */
32#define FUSE_DEBUG (1 << 1)
33
34/** If a file is removed but it's still open, don't hide the file but
35 remove it immediately */
36#define FUSE_HARD_REMOVE (1 << 2)
37
38/** Use st_ino field in getattr instead of generating inode numbers */
39#define FUSE_USE_INO (1 << 3)
40
Miklos Szeredi33be22d2005-05-27 09:12:43 +000041/** Make a best effort to fill in inode number in a readdir **/
42#define FUSE_READDIR_INO (1 << 5)
Miklos Szeredia25d4c22004-11-23 22:32:16 +000043
Miklos Szeredie331c4b2005-07-06 13:34:02 +000044/** Ignore file mode supplied by the filesystem, and create one based
45 on the 'umask' option */
46#define FUSE_SET_MODE (1 << 6)
47
48/** Ignore st_uid supplied by the filesystem and set it based on the
49 'uid' option*/
50#define FUSE_SET_UID (1 << 7)
51
52/** Ignore st_gid supplied by the filesystem and set it based on the
53 'gid' option*/
54#define FUSE_SET_GID (1 << 8)
55
Miklos Szeredie77cc072005-08-01 11:58:51 +000056/** Bypass the page cache for read and write operations */
57#define FUSE_DIRECT_IO (1 << 9)
58
59/** If the FUSE_KERNEL_CACHE flag is given, then cached data will not
60 be flushed on open */
61#define FUSE_KERNEL_CACHE (1 << 10)
Miklos Szeredie331c4b2005-07-06 13:34:02 +000062
Miklos Szeredi97c61e92001-11-07 12:09:43 +000063#define FUSE_MAX_PATH 4096
Miklos Szeredi30e093a2005-04-03 17:44:54 +000064
Miklos Szeredibd10a7b2005-07-15 09:59:59 +000065#define ENTRY_REVALIDATE_TIME 1.0 /* sec */
66#define ATTR_REVALIDATE_TIME 1.0 /* sec */
Miklos Szeredi0f62d722005-01-04 12:45:54 +000067
Miklos Szeredie2aa2e22005-07-15 13:31:36 +000068struct fuse {
69 struct fuse_ll *fll;
70 int flags;
71 struct fuse_operations op;
72 int compat;
73 struct node **name_table;
74 size_t name_table_size;
75 struct node **id_table;
76 size_t id_table_size;
77 fuse_ino_t ctr;
78 unsigned int generation;
79 unsigned int hidectr;
80 pthread_mutex_t lock;
81 pthread_rwlock_t tree_lock;
82 void *user_data;
83 uid_t uid;
84 gid_t gid;
85 mode_t umask;
86};
87
Miklos Szeredi0f62d722005-01-04 12:45:54 +000088struct node {
89 struct node *name_next;
90 struct node *id_next;
Miklos Szeredibd10a7b2005-07-15 09:59:59 +000091 fuse_ino_t nodeid;
Miklos Szeredi0f62d722005-01-04 12:45:54 +000092 unsigned int generation;
93 int refctr;
Miklos Szeredibd10a7b2005-07-15 09:59:59 +000094 fuse_ino_t parent;
Miklos Szeredi0f62d722005-01-04 12:45:54 +000095 char *name;
Miklos Szeredi38009022005-05-08 19:47:22 +000096 uint64_t nlookup;
Miklos Szeredi0f62d722005-01-04 12:45:54 +000097 int open_count;
98 int is_hidden;
99};
100
101struct fuse_dirhandle {
Miklos Szerediab974562005-04-07 15:40:21 +0000102 pthread_mutex_t lock;
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000103 struct fuse *fuse;
Miklos Szeredi1b188022005-07-28 11:07:29 +0000104 char *contents;
Miklos Szerediab974562005-04-07 15:40:21 +0000105 int allocated;
Miklos Szeredib92d9782005-02-07 16:10:49 +0000106 unsigned len;
Miklos Szerediab974562005-04-07 15:40:21 +0000107 unsigned needlen;
Miklos Szeredi3ead28e2005-01-18 21:23:41 +0000108 int filled;
Miklos Szerediede1f7a2005-04-01 21:08:57 +0000109 unsigned long fh;
Miklos Szerediab974562005-04-07 15:40:21 +0000110 int error;
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000111 fuse_ino_t nodeid;
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000112};
113
Miklos Szeredid169f312004-09-22 08:48:26 +0000114static struct fuse_context *(*fuse_getcontext)(void) = NULL;
115
Miklos Szerediab974562005-04-07 15:40:21 +0000116#ifndef USE_UCLIBC
117#define mutex_init(mut) pthread_mutex_init(mut, NULL)
118#else
Miklos Szeredi129ef8f2005-06-20 13:48:42 +0000119static void mutex_init(pthread_mutex_t *mut)
Miklos Szerediab974562005-04-07 15:40:21 +0000120{
121 pthread_mutexattr_t attr;
122 pthread_mutexattr_init(&attr);
123 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP);
124 pthread_mutex_init(mut, &attr);
125 pthread_mutexattr_destroy(&attr);
126}
127#endif
128
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000129static struct node *get_node_nocheck(struct fuse *f, fuse_ino_t nodeid)
Miklos Szeredia181e612001-11-06 12:03:23 +0000130{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000131 size_t hash = nodeid % f->id_table_size;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000132 struct node *node;
133
Miklos Szeredia13d9002004-11-02 17:32:03 +0000134 for (node = f->id_table[hash]; node != NULL; node = node->id_next)
135 if (node->nodeid == nodeid)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000136 return node;
Miklos Szeredie5183742005-02-02 11:14:04 +0000137
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000138 return NULL;
Miklos Szeredia181e612001-11-06 12:03:23 +0000139}
140
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000141static struct node *get_node(struct fuse *f, fuse_ino_t nodeid)
Miklos Szeredia181e612001-11-06 12:03:23 +0000142{
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000143 struct node *node = get_node_nocheck(f, nodeid);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000144 if (!node) {
145 fprintf(stderr, "fuse internal error: node %lu not found\n",
146 nodeid);
147 abort();
148 }
149 return node;
Miklos Szeredia181e612001-11-06 12:03:23 +0000150}
151
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000152static void free_node(struct node *node)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000153{
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000154 free(node->name);
155 free(node);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000156}
157
Miklos Szeredia13d9002004-11-02 17:32:03 +0000158static void unhash_id(struct fuse *f, struct node *node)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000159{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000160 size_t hash = node->nodeid % f->id_table_size;
161 struct node **nodep = &f->id_table[hash];
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000162
Miklos Szeredie5183742005-02-02 11:14:04 +0000163 for (; *nodep != NULL; nodep = &(*nodep)->id_next)
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000164 if (*nodep == node) {
Miklos Szeredia13d9002004-11-02 17:32:03 +0000165 *nodep = node->id_next;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000166 return;
167 }
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000168}
169
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000170static void hash_id(struct fuse *f, struct node *node)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000171{
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000172 size_t hash = node->nodeid % f->id_table_size;
173 node->id_next = f->id_table[hash];
Miklos Szeredie5183742005-02-02 11:14:04 +0000174 f->id_table[hash] = node;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000175}
176
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000177static unsigned int name_hash(struct fuse *f, fuse_ino_t parent, const char *name)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000178{
179 unsigned int hash = *name;
180
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000181 if (hash)
182 for (name += 1; *name != '\0'; name++)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000183 hash = (hash << 5) - hash + *name;
184
185 return (hash + parent) % f->name_table_size;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000186}
187
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000188static void unref_node(struct fuse *f, struct node *node);
189
190static void unhash_name(struct fuse *f, struct node *node)
191{
192 if (node->name) {
193 size_t hash = name_hash(f, node->parent, node->name);
194 struct node **nodep = &f->name_table[hash];
Miklos Szeredie5183742005-02-02 11:14:04 +0000195
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000196 for (; *nodep != NULL; nodep = &(*nodep)->name_next)
197 if (*nodep == node) {
198 *nodep = node->name_next;
199 node->name_next = NULL;
200 unref_node(f, get_node(f, node->parent));
201 free(node->name);
202 node->name = NULL;
203 node->parent = 0;
204 return;
205 }
206 fprintf(stderr, "fuse internal error: unable to unhash node: %lu\n",
207 node->nodeid);
208 abort();
209 }
210}
211
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000212static int hash_name(struct fuse *f, struct node *node, fuse_ino_t parent,
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000213 const char *name)
214{
215 size_t hash = name_hash(f, parent, name);
216 node->name = strdup(name);
217 if (node->name == NULL)
218 return -1;
219
220 get_node(f, parent)->refctr ++;
221 node->parent = parent;
222 node->name_next = f->name_table[hash];
223 f->name_table[hash] = node;
224 return 0;
225}
226
227static void delete_node(struct fuse *f, struct node *node)
228{
Miklos Szeredi38009022005-05-08 19:47:22 +0000229 if (f->flags & FUSE_DEBUG) {
230 printf("delete: %lu\n", node->nodeid);
231 fflush(stdout);
232 }
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000233 assert(!node->name);
234 unhash_id(f, node);
235 free_node(node);
236}
237
238static void unref_node(struct fuse *f, struct node *node)
239{
240 assert(node->refctr > 0);
241 node->refctr --;
242 if (!node->refctr)
243 delete_node(f, node);
244}
245
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000246static fuse_ino_t next_id(struct fuse *f)
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000247{
248 do {
249 f->ctr++;
250 if (!f->ctr)
251 f->generation ++;
252 } while (f->ctr == 0 || get_node_nocheck(f, f->ctr) != NULL);
253 return f->ctr;
254}
255
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000256static struct node *lookup_node(struct fuse *f, fuse_ino_t parent,
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000257 const char *name)
258{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000259 size_t hash = name_hash(f, parent, name);
260 struct node *node;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000261
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000262 for (node = f->name_table[hash]; node != NULL; node = node->name_next)
263 if (node->parent == parent && strcmp(node->name, name) == 0)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000264 return node;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000265
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000266 return NULL;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000267}
268
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000269static struct node *find_node(struct fuse *f, fuse_ino_t parent,
270 const char *name)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000271{
272 struct node *node;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000273
Miklos Szeredia181e612001-11-06 12:03:23 +0000274 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000275 node = lookup_node(f, parent, name);
Miklos Szeredie331c4b2005-07-06 13:34:02 +0000276 if (node == NULL) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000277 node = (struct node *) calloc(1, sizeof(struct node));
278 if (node == NULL)
279 goto out_err;
Miklos Szeredie5183742005-02-02 11:14:04 +0000280
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000281 node->refctr = 1;
282 node->nodeid = next_id(f);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000283 node->open_count = 0;
284 node->is_hidden = 0;
285 node->generation = f->generation;
286 if (hash_name(f, node, parent, name) == -1) {
287 free(node);
288 node = NULL;
289 goto out_err;
290 }
291 hash_id(f, node);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000292 }
Miklos Szeredi38009022005-05-08 19:47:22 +0000293 node->nlookup ++;
Miklos Szeredic2309912004-09-21 13:40:38 +0000294 out_err:
Miklos Szeredia181e612001-11-06 12:03:23 +0000295 pthread_mutex_unlock(&f->lock);
Miklos Szeredi76f65782004-02-19 16:55:40 +0000296 return node;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000297}
298
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000299static char *add_name(char *buf, char *s, const char *name)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000300{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000301 size_t len = strlen(name);
302 s -= len;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000303 if (s <= buf) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000304 fprintf(stderr, "fuse: path too long: ...%s\n", s + len);
305 return NULL;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000306 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000307 strncpy(s, name, len);
308 s--;
309 *s = '/';
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000310
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000311 return s;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000312}
313
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000314static char *get_path_name(struct fuse *f, fuse_ino_t nodeid, const char *name)
Miklos Szeredib483c932001-10-29 14:57:57 +0000315{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000316 char buf[FUSE_MAX_PATH];
317 char *s = buf + FUSE_MAX_PATH - 1;
318 struct node *node;
Miklos Szeredie5183742005-02-02 11:14:04 +0000319
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000320 *s = '\0';
Miklos Szeredia181e612001-11-06 12:03:23 +0000321
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000322 if (name != NULL) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000323 s = add_name(buf, s, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000324 if (s == NULL)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000325 return NULL;
326 }
327
328 pthread_mutex_lock(&f->lock);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000329 for (node = get_node(f, nodeid); node && node->nodeid != FUSE_ROOT_ID;
330 node = get_node(f, node->parent)) {
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000331 if (node->name == NULL) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000332 s = NULL;
333 break;
334 }
Miklos Szeredie5183742005-02-02 11:14:04 +0000335
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000336 s = add_name(buf, s, node->name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000337 if (s == NULL)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000338 break;
339 }
340 pthread_mutex_unlock(&f->lock);
341
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000342 if (node == NULL || s == NULL)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000343 return NULL;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000344 else if (*s == '\0')
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000345 return strdup("/");
346 else
347 return strdup(s);
348}
Miklos Szeredia181e612001-11-06 12:03:23 +0000349
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000350static char *get_path(struct fuse *f, fuse_ino_t nodeid)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000351{
Miklos Szeredia13d9002004-11-02 17:32:03 +0000352 return get_path_name(f, nodeid, NULL);
Miklos Szeredib483c932001-10-29 14:57:57 +0000353}
354
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000355static void forget_node(struct fuse *f, fuse_ino_t nodeid, uint64_t nlookup)
Miklos Szeredi38009022005-05-08 19:47:22 +0000356{
357 struct node *node;
358 if (nodeid == FUSE_ROOT_ID)
359 return;
360 pthread_mutex_lock(&f->lock);
361 node = get_node(f, nodeid);
362 assert(node->nlookup >= nlookup);
363 node->nlookup -= nlookup;
364 if (!node->nlookup) {
365 unhash_name(f, node);
366 unref_node(f, node);
367 }
368 pthread_mutex_unlock(&f->lock);
369}
370
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000371static void remove_node(struct fuse *f, fuse_ino_t dir, const char *name)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000372{
Miklos Szeredia181e612001-11-06 12:03:23 +0000373 struct node *node;
374
375 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000376 node = lookup_node(f, dir, name);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000377 if (node != NULL)
378 unhash_name(f, node);
Miklos Szeredia181e612001-11-06 12:03:23 +0000379 pthread_mutex_unlock(&f->lock);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000380}
381
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000382static int rename_node(struct fuse *f, fuse_ino_t olddir, const char *oldname,
383 fuse_ino_t newdir, const char *newname, int hide)
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000384{
Miklos Szeredia181e612001-11-06 12:03:23 +0000385 struct node *node;
386 struct node *newnode;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000387 int err = 0;
Miklos Szeredie5183742005-02-02 11:14:04 +0000388
Miklos Szeredia181e612001-11-06 12:03:23 +0000389 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000390 node = lookup_node(f, olddir, oldname);
391 newnode = lookup_node(f, newdir, newname);
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000392 if (node == NULL)
393 goto out;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000394
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000395 if (newnode != NULL) {
396 if (hide) {
397 fprintf(stderr, "fuse: hidden file got created during hiding\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000398 err = -EBUSY;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000399 goto out;
400 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000401 unhash_name(f, newnode);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000402 }
Miklos Szeredie5183742005-02-02 11:14:04 +0000403
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000404 unhash_name(f, node);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000405 if (hash_name(f, node, newdir, newname) == -1) {
406 err = -ENOMEM;
407 goto out;
408 }
Miklos Szeredie5183742005-02-02 11:14:04 +0000409
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000410 if (hide)
411 node->is_hidden = 1;
412
413 out:
Miklos Szeredia181e612001-11-06 12:03:23 +0000414 pthread_mutex_unlock(&f->lock);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000415 return err;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000416}
417
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000418static void set_stat(struct fuse *f, fuse_ino_t nodeid, struct stat *stbuf)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000419{
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000420 if (!(f->flags & FUSE_USE_INO))
421 stbuf->st_ino = nodeid;
Miklos Szeredie331c4b2005-07-06 13:34:02 +0000422 if (f->flags & FUSE_SET_MODE)
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000423 stbuf->st_mode = (stbuf->st_mode & S_IFMT) | (0777 & ~f->umask);
424 if (f->flags & FUSE_SET_UID)
425 stbuf->st_uid = f->uid;
426 if (f->flags & FUSE_SET_GID)
427 stbuf->st_gid = f->gid;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000428}
429
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000430static int is_open(struct fuse *f, fuse_ino_t dir, const char *name)
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000431{
432 struct node *node;
433 int isopen = 0;
434 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000435 node = lookup_node(f, dir, name);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000436 if (node && node->open_count > 0)
437 isopen = 1;
438 pthread_mutex_unlock(&f->lock);
439 return isopen;
440}
441
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000442static char *hidden_name(struct fuse *f, fuse_ino_t dir, const char *oldname,
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000443 char *newname, size_t bufsize)
444{
445 struct stat buf;
446 struct node *node;
447 struct node *newnode;
448 char *newpath;
449 int res;
450 int failctr = 10;
451
452 if (!f->op.getattr)
453 return NULL;
454
455 do {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000456 pthread_mutex_lock(&f->lock);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000457 node = lookup_node(f, dir, oldname);
458 if (node == NULL) {
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000459 pthread_mutex_unlock(&f->lock);
460 return NULL;
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000461 }
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000462 do {
463 f->hidectr ++;
464 snprintf(newname, bufsize, ".fuse_hidden%08x%08x",
Miklos Szeredia13d9002004-11-02 17:32:03 +0000465 (unsigned int) node->nodeid, f->hidectr);
Miklos Szeredif458b8c2004-12-07 16:46:42 +0000466 newnode = lookup_node(f, dir, newname);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000467 } while(newnode);
468 pthread_mutex_unlock(&f->lock);
Miklos Szeredie5183742005-02-02 11:14:04 +0000469
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000470 newpath = get_path_name(f, dir, newname);
471 if (!newpath)
472 break;
Miklos Szeredie5183742005-02-02 11:14:04 +0000473
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000474 res = f->op.getattr(newpath, &buf);
475 if (res != 0)
476 break;
477 free(newpath);
478 newpath = NULL;
479 } while(--failctr);
480
481 return newpath;
482}
483
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000484static int hide_node(struct fuse *f, const char *oldpath, fuse_ino_t dir,
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000485 const char *oldname)
486{
487 char newname[64];
488 char *newpath;
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000489 int err = -EBUSY;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000490
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000491 if (f->op.rename && f->op.unlink) {
492 newpath = hidden_name(f, dir, oldname, newname, sizeof(newname));
493 if (newpath) {
494 int res = f->op.rename(oldpath, newpath);
495 if (res == 0)
496 err = rename_node(f, dir, oldname, dir, newname, 1);
497 free(newpath);
498 }
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000499 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000500 return err;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000501}
502
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000503static int lookup_path(struct fuse *f, fuse_ino_t nodeid, const char *name,
504 const char *path, struct fuse_entry_param *e)
Miklos Szeredi76f65782004-02-19 16:55:40 +0000505{
506 int res;
Miklos Szeredi76f65782004-02-19 16:55:40 +0000507
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000508 memset(e, 0, sizeof(struct fuse_entry_param));
509 res = f->op.getattr(path, &e->attr);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000510 if (res == 0) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000511 struct node *node;
512
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000513 node = find_node(f, nodeid, name);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000514 if (node == NULL)
515 res = -ENOMEM;
516 else {
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000517 e->ino = node->nodeid;
518 e->generation = node->generation;
519 e->entry_timeout = ENTRY_REVALIDATE_TIME;
520 e->attr_timeout = ATTR_REVALIDATE_TIME;
521 set_stat(f, e->ino, &e->attr);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000522 if (f->flags & FUSE_DEBUG) {
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000523 printf(" NODEID: %lu\n", (unsigned long) e->ino);
Miklos Szeredib2cf9562004-09-16 08:42:40 +0000524 fflush(stdout);
525 }
Miklos Szeredi76f65782004-02-19 16:55:40 +0000526 }
527 }
528 return res;
529}
530
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000531static struct fuse *req_fuse(fuse_req_t req)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000532{
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000533 return (struct fuse *) fuse_req_userdata(req);
534}
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000535
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000536static struct fuse *req_fuse_prepare(fuse_req_t req)
537{
538 struct fuse_context *c = fuse_get_context();
539 const struct fuse_ctx *ctx = fuse_req_ctx(req);
540 c->fuse = req_fuse(req);
541 c->uid = ctx->uid;
542 c->gid = ctx->gid;
543 c->pid = ctx->pid;
544 c->private_data = c->fuse->user_data;
545
546 return c->fuse;
547}
548
549static inline void reply_err(fuse_req_t req, int err)
550{
551 /* fuse_reply_err() uses non-negated errno values */
552 fuse_reply_err(req, -err);
553}
554
555static void reply_entry(fuse_req_t req, const struct fuse_entry_param *e,
556 int err)
557{
558 if (!err) {
Miklos Szeredi9b813af2005-07-21 07:59:37 +0000559 if (fuse_reply_entry(req, e) == -ENOENT)
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000560 forget_node(req_fuse(req), e->ino, 1);
561 } else
562 reply_err(req, err);
563}
564
565static void *fuse_data_init(void *data)
566{
567 struct fuse *f = (struct fuse *) data;
568
569 if (f->op.init)
570 f->user_data = f->op.init();
571
572 return f;
573}
574
575static void fuse_data_destroy(void *data)
576{
577 struct fuse *f = (struct fuse *) data;
578
579 if (f->op.destroy)
580 f->op.destroy(f->user_data);
581}
582
583static void fuse_lookup(fuse_req_t req, fuse_ino_t parent, const char *name)
584{
585 struct fuse *f = req_fuse_prepare(req);
586 struct fuse_entry_param e;
587 char *path;
588 int err;
589
590 err = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000591 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000592 path = get_path_name(f, parent, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000593 if (path != NULL) {
594 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi6ebe2342002-01-06 21:44:16 +0000595 printf("LOOKUP %s\n", path);
596 fflush(stdout);
597 }
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000598 err = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000599 if (f->op.getattr)
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000600 err = lookup_path(f, parent, name, path, &e);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000601 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000602 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000603 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000604 reply_entry(req, &e, err);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000605}
606
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000607static void fuse_forget(fuse_req_t req, fuse_ino_t ino, unsigned long nlookup)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000608{
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000609 struct fuse *f = req_fuse(req);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000610 if (f->flags & FUSE_DEBUG) {
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000611 printf("FORGET %lu/%lu\n", ino, nlookup);
Miklos Szeredi43696432001-11-18 19:15:05 +0000612 fflush(stdout);
613 }
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000614 forget_node(f, ino, nlookup);
615 fuse_reply_none(req);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000616}
617
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000618static void fuse_getattr(fuse_req_t req, fuse_ino_t ino)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000619{
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000620 struct fuse *f = req_fuse_prepare(req);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000621 struct stat buf;
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000622 char *path;
623 int err;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000624
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000625 err = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000626 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000627 path = get_path(f, ino);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000628 if (path != NULL) {
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000629 err = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000630 if (f->op.getattr)
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000631 err = f->op.getattr(path, &buf);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000632 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000633 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000634 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000635 if (!err) {
636 set_stat(f, ino, &buf);
637 fuse_reply_attr(req, &buf, ATTR_REVALIDATE_TIME);
638 } else
639 reply_err(req, err);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000640}
641
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000642static int do_chmod(struct fuse *f, const char *path, struct stat *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000643{
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000644 int err;
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000645
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000646 err = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000647 if (f->op.chmod)
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000648 err = f->op.chmod(path, attr->st_mode);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000649
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000650 return err;
Miklos Szeredie5183742005-02-02 11:14:04 +0000651}
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000652
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000653static int do_chown(struct fuse *f, const char *path, struct stat *attr,
Miklos Szeredi2e50d432001-12-20 12:17:25 +0000654 int valid)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000655{
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000656 int err;
657 uid_t uid = (valid & FUSE_SET_ATTR_UID) ? attr->st_uid : (uid_t) -1;
658 gid_t gid = (valid & FUSE_SET_ATTR_GID) ? attr->st_gid : (gid_t) -1;
Miklos Szeredie5183742005-02-02 11:14:04 +0000659
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000660 err = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000661 if (f->op.chown)
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000662 err = f->op.chown(path, uid, gid);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000663
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000664 return err;
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000665}
666
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000667static int do_truncate(struct fuse *f, const char *path, struct stat *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000668{
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000669 int err;
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000670
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000671 err = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000672 if (f->op.truncate)
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000673 err = f->op.truncate(path, attr->st_size);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000674
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000675 return err;
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000676}
677
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000678static int do_utime(struct fuse *f, const char *path, struct stat *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000679{
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000680 int err;
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000681 struct utimbuf buf;
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000682 buf.actime = attr->st_atime;
683 buf.modtime = attr->st_mtime;
684 err = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000685 if (f->op.utime)
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000686 err = f->op.utime(path, &buf);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000687
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000688 return err;
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000689}
690
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000691static void fuse_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr,
692 int valid)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000693{
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000694 struct fuse *f = req_fuse_prepare(req);
695 struct stat buf;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000696 char *path;
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000697 int err;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000698
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000699 err = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000700 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000701 path = get_path(f, ino);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000702 if (path != NULL) {
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000703 err = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000704 if (f->op.getattr) {
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000705 err = 0;
706 if (!err && (valid & FUSE_SET_ATTR_MODE))
707 err = do_chmod(f, path, attr);
708 if (!err && (valid & (FUSE_SET_ATTR_UID | FUSE_SET_ATTR_GID)))
709 err = do_chown(f, path, attr, valid);
710 if (!err && (valid & FUSE_SET_ATTR_SIZE))
711 err = do_truncate(f, path, attr);
712 if (!err && (valid & (FUSE_SET_ATTR_ATIME | FUSE_SET_ATTR_MTIME)) == (FUSE_SET_ATTR_ATIME | FUSE_SET_ATTR_MTIME))
713 err = do_utime(f, path, attr);
714 if (!err)
715 err = f->op.getattr(path, &buf);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000716 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000717 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000718 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000719 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000720 if (!err) {
721 set_stat(f, ino, &buf);
722 fuse_reply_attr(req, &buf, ATTR_REVALIDATE_TIME);
723 } else
724 reply_err(req, err);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000725}
726
Miklos Szeredi7b28eae2005-08-01 12:48:30 +0000727static void fuse_access(fuse_req_t req, fuse_ino_t ino, int mask)
728{
729 struct fuse *f = req_fuse_prepare(req);
730 char *path;
731 int err;
732
733 err = -ENOENT;
734 pthread_rwlock_rdlock(&f->tree_lock);
735 path = get_path(f, ino);
736 if (path != NULL) {
737 err = -ENOSYS;
738 if (f->op.access)
739 err = f->op.access(path, mask);
740 free(path);
741 }
742 pthread_rwlock_unlock(&f->tree_lock);
743 reply_err(req, err);
744}
745
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000746static void fuse_readlink(fuse_req_t req, fuse_ino_t ino)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000747{
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000748 struct fuse *f = req_fuse_prepare(req);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000749 char link[PATH_MAX + 1];
Miklos Szeredib483c932001-10-29 14:57:57 +0000750 char *path;
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000751 int err;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000752
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000753 err = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000754 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000755 path = get_path(f, ino);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000756 if (path != NULL) {
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000757 err = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000758 if (f->op.readlink)
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000759 err = f->op.readlink(path, link, sizeof(link));
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000760 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000761 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000762 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000763 if (!err) {
764 link[PATH_MAX] = '\0';
765 fuse_reply_readlink(req, link);
766 } else
767 reply_err(req, err);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000768}
769
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000770static void fuse_mknod(fuse_req_t req, fuse_ino_t parent, const char *name,
771 mode_t mode, dev_t rdev)
Miklos Szeredib483c932001-10-29 14:57:57 +0000772{
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000773 struct fuse *f = req_fuse_prepare(req);
774 struct fuse_entry_param e;
Miklos Szeredib483c932001-10-29 14:57:57 +0000775 char *path;
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000776 int err;
Miklos Szeredib483c932001-10-29 14:57:57 +0000777
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000778 err = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000779 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000780 path = get_path_name(f, parent, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000781 if (path != NULL) {
782 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000783 printf("MKNOD %s\n", path);
784 fflush(stdout);
785 }
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000786 err = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000787 if (f->op.mknod && f->op.getattr) {
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000788 err = f->op.mknod(path, mode, rdev);
789 if (!err)
790 err = lookup_path(f, parent, name, path, &e);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000791 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000792 free(path);
Miklos Szeredib483c932001-10-29 14:57:57 +0000793 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000794 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000795 reply_entry(req, &e, err);
Miklos Szeredib483c932001-10-29 14:57:57 +0000796}
797
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000798static void fuse_mkdir(fuse_req_t req, fuse_ino_t parent, const char *name,
799 mode_t mode)
Miklos Szeredib483c932001-10-29 14:57:57 +0000800{
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000801 struct fuse *f = req_fuse_prepare(req);
802 struct fuse_entry_param e;
Miklos Szeredib483c932001-10-29 14:57:57 +0000803 char *path;
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000804 int err;
Miklos Szeredib483c932001-10-29 14:57:57 +0000805
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000806 err = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000807 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000808 path = get_path_name(f, parent, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000809 if (path != NULL) {
810 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000811 printf("MKDIR %s\n", path);
812 fflush(stdout);
813 }
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000814 err = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000815 if (f->op.mkdir && f->op.getattr) {
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000816 err = f->op.mkdir(path, mode);
817 if (!err)
818 err = lookup_path(f, parent, name, path, &e);
Miklos Szeredi76f65782004-02-19 16:55:40 +0000819 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000820 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000821 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000822 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000823 reply_entry(req, &e, err);
Miklos Szeredib483c932001-10-29 14:57:57 +0000824}
825
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000826static void fuse_unlink(fuse_req_t req, fuse_ino_t parent, const char *name)
Miklos Szeredib483c932001-10-29 14:57:57 +0000827{
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000828 struct fuse *f = req_fuse_prepare(req);
Miklos Szeredib483c932001-10-29 14:57:57 +0000829 char *path;
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000830 int err;
Miklos Szeredib483c932001-10-29 14:57:57 +0000831
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000832 err = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000833 pthread_rwlock_wrlock(&f->tree_lock);
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000834 path = get_path_name(f, parent, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000835 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +0000836 if (f->flags & FUSE_DEBUG) {
837 printf("UNLINK %s\n", path);
838 fflush(stdout);
839 }
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000840 err = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000841 if (f->op.unlink) {
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000842 if (!(f->flags & FUSE_HARD_REMOVE) && is_open(f, parent, name))
843 err = hide_node(f, path, parent, name);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000844 else {
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000845 err = f->op.unlink(path);
846 if (!err)
847 remove_node(f, parent, name);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000848 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000849 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000850 free(path);
Miklos Szeredib483c932001-10-29 14:57:57 +0000851 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000852 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000853 reply_err(req, err);
Miklos Szeredib5958612004-02-20 14:10:49 +0000854}
855
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000856static void fuse_rmdir(fuse_req_t req, fuse_ino_t parent, const char *name)
Miklos Szeredib5958612004-02-20 14:10:49 +0000857{
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000858 struct fuse *f = req_fuse_prepare(req);
Miklos Szeredib5958612004-02-20 14:10:49 +0000859 char *path;
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000860 int err;
Miklos Szeredib5958612004-02-20 14:10:49 +0000861
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000862 err = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000863 pthread_rwlock_wrlock(&f->tree_lock);
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000864 path = get_path_name(f, parent, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000865 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +0000866 if (f->flags & FUSE_DEBUG) {
867 printf("RMDIR %s\n", path);
868 fflush(stdout);
869 }
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000870 err = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000871 if (f->op.rmdir) {
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000872 err = f->op.rmdir(path);
873 if (!err)
874 remove_node(f, parent, name);
Miklos Szeredib5958612004-02-20 14:10:49 +0000875 }
876 free(path);
877 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000878 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000879 reply_err(req, err);
Miklos Szeredib483c932001-10-29 14:57:57 +0000880}
881
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000882static void fuse_symlink(fuse_req_t req, const char *link, fuse_ino_t parent,
883 const char *name)
Miklos Szeredib483c932001-10-29 14:57:57 +0000884{
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000885 struct fuse *f = req_fuse_prepare(req);
886 struct fuse_entry_param e;
Miklos Szeredib483c932001-10-29 14:57:57 +0000887 char *path;
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000888 int err;
Miklos Szeredib483c932001-10-29 14:57:57 +0000889
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000890 err = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000891 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000892 path = get_path_name(f, parent, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000893 if (path != NULL) {
894 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000895 printf("SYMLINK %s\n", path);
896 fflush(stdout);
897 }
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000898 err = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000899 if (f->op.symlink && f->op.getattr) {
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000900 err = f->op.symlink(link, path);
901 if (!err)
902 err = lookup_path(f, parent, name, path, &e);
Miklos Szeredi76f65782004-02-19 16:55:40 +0000903 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000904 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000905 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000906 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000907 reply_entry(req, &e, err);
Miklos Szeredib483c932001-10-29 14:57:57 +0000908}
909
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000910static void fuse_rename(fuse_req_t req, fuse_ino_t olddir, const char *oldname,
911 fuse_ino_t newdir, const char *newname)
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000912{
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000913 struct fuse *f = req_fuse_prepare(req);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000914 char *oldpath;
915 char *newpath;
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000916 int err;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000917
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000918 err = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000919 pthread_rwlock_wrlock(&f->tree_lock);
Miklos Szeredia181e612001-11-06 12:03:23 +0000920 oldpath = get_path_name(f, olddir, oldname);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000921 if (oldpath != NULL) {
Miklos Szeredia181e612001-11-06 12:03:23 +0000922 newpath = get_path_name(f, newdir, newname);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000923 if (newpath != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +0000924 if (f->flags & FUSE_DEBUG) {
925 printf("RENAME %s -> %s\n", oldpath, newpath);
926 fflush(stdout);
927 }
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000928 err = -ENOSYS;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000929 if (f->op.rename) {
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000930 err = 0;
Miklos Szeredie5183742005-02-02 11:14:04 +0000931 if (!(f->flags & FUSE_HARD_REMOVE) &&
Miklos Szeredi2529ca22004-07-13 15:36:52 +0000932 is_open(f, newdir, newname))
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000933 err = hide_node(f, newpath, newdir, newname);
934 if (!err) {
935 err = f->op.rename(oldpath, newpath);
936 if (!err)
937 err = rename_node(f, olddir, oldname, newdir, newname, 0);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000938 }
939 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000940 free(newpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000941 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000942 free(oldpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000943 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000944 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000945 reply_err(req, err);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000946}
947
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000948static void fuse_link(fuse_req_t req, fuse_ino_t ino, fuse_ino_t newparent,
949 const char *newname)
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000950{
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000951 struct fuse *f = req_fuse_prepare(req);
952 struct fuse_entry_param e;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000953 char *oldpath;
954 char *newpath;
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000955 int err;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000956
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000957 err = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000958 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000959 oldpath = get_path(f, ino);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000960 if (oldpath != NULL) {
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000961 newpath = get_path_name(f, newparent, newname);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000962 if (newpath != NULL) {
963 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000964 printf("LINK %s\n", newpath);
965 fflush(stdout);
966 }
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000967 err = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000968 if (f->op.link && f->op.getattr) {
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000969 err = f->op.link(oldpath, newpath);
970 if (!err)
971 err = lookup_path(f, newparent, newname, newpath, &e);
Miklos Szeredi76f65782004-02-19 16:55:40 +0000972 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000973 free(newpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000974 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000975 free(oldpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000976 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000977 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000978 reply_entry(req, &e, err);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000979}
980
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000981static void fuse_open(fuse_req_t req, fuse_ino_t ino,
982 struct fuse_file_info *fi)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000983{
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000984 struct fuse *f = req_fuse_prepare(req);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000985 char *path;
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000986 int err;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000987
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000988 err = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000989 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000990 path = get_path(f, ino);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000991 if (path != NULL) {
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000992 err = -ENOSYS;
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000993 if (f->op.open) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +0000994 if (!f->compat)
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000995 err = f->op.open(path, fi);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +0000996 else
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000997 err = ((struct fuse_operations_compat2 *) &f->op)->open(path, fi->flags);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +0000998 }
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000999 }
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001000 if (!err) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001001 if (f->flags & FUSE_DEBUG) {
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001002 printf("OPEN[%lu] flags: 0x%x\n", fi->fh, fi->flags);
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001003 fflush(stdout);
1004 }
Miklos Szeredie5183742005-02-02 11:14:04 +00001005
Miklos Szeredie77cc072005-08-01 11:58:51 +00001006 if (f->flags & FUSE_DIRECT_IO)
1007 fi->direct_io = 1;
1008 if (f->flags & FUSE_KERNEL_CACHE)
1009 fi->keep_cache = 1;
1010
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001011 pthread_mutex_lock(&f->lock);
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001012 if (fuse_reply_open(req, fi) == -ENOENT) {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001013 /* The open syscall was interrupted, so it must be cancelled */
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001014 if(f->op.release) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001015 if (!f->compat)
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001016 f->op.release(path, fi);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001017 else
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001018 ((struct fuse_operations_compat2 *) &f->op)->release(path, fi->flags);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001019 }
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001020 } else {
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001021 struct node *node = get_node(f, ino);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001022 node->open_count ++;
1023 }
Miklos Szeredi73798f92004-07-12 15:55:11 +00001024 pthread_mutex_unlock(&f->lock);
Miklos Szeredi73798f92004-07-12 15:55:11 +00001025 } else
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001026 reply_err(req, err);
Miklos Szeredi73798f92004-07-12 15:55:11 +00001027
Miklos Szeredi9a31cca2004-06-26 21:11:25 +00001028 if (path)
1029 free(path);
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001030 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001031}
1032
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001033static void fuse_read(fuse_req_t req, fuse_ino_t ino, size_t size, off_t off,
1034 struct fuse_file_info *fi)
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001035{
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001036 struct fuse *f = req_fuse_prepare(req);
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001037 char *path;
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001038 char *buf;
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001039 int res;
1040
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001041 buf = (char *) malloc(size);
1042 if (buf == NULL) {
1043 reply_err(req, -ENOMEM);
1044 return;
1045 }
1046
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001047 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001048 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001049 path = get_path(f, ino);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001050 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001051 if (f->flags & FUSE_DEBUG) {
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001052 printf("READ[%lu] %u bytes from %llu\n", fi->fh, size, off);
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001053 fflush(stdout);
1054 }
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001055
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001056 res = -ENOSYS;
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001057 if (f->op.read)
1058 res = f->op.read(path, buf, size, off, fi);
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001059 free(path);
1060 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001061 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001062
1063 if (res >= 0) {
1064 if (f->flags & FUSE_DEBUG) {
1065 printf(" READ[%lu] %u bytes\n", fi->fh, res);
1066 fflush(stdout);
1067 }
1068 fuse_reply_buf(req, buf, res);
1069 } else
1070 reply_err(req, res);
1071
1072 free(buf);
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001073}
1074
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001075static void fuse_write(fuse_req_t req, fuse_ino_t ino, const char *buf,
1076 size_t size, off_t off, struct fuse_file_info *fi)
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001077{
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001078 struct fuse *f = req_fuse_prepare(req);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001079 char *path;
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001080 int res;
1081
1082 res = -ENOENT;
1083 pthread_rwlock_rdlock(&f->tree_lock);
1084 path = get_path(f, ino);
1085 if (path != NULL) {
1086 if (f->flags & FUSE_DEBUG) {
1087 printf("WRITE%s[%lu] %u bytes to %llu\n",
1088 fi->writepage ? "PAGE" : "", fi->fh, size, off);
1089 fflush(stdout);
1090 }
1091
1092 res = -ENOSYS;
1093 if (f->op.write)
1094 res = f->op.write(path, buf, size, off, fi);
1095 free(path);
1096 }
1097 pthread_rwlock_unlock(&f->tree_lock);
1098
1099 if (res >= 0)
1100 fuse_reply_write(req, res);
1101 else
1102 reply_err(req, res);
1103}
1104
1105static void fuse_flush(fuse_req_t req, fuse_ino_t ino,
1106 struct fuse_file_info *fi)
1107{
1108 struct fuse *f = req_fuse_prepare(req);
1109 char *path;
1110 int err;
1111
1112 err = -ENOENT;
1113 pthread_rwlock_rdlock(&f->tree_lock);
1114 path = get_path(f, ino);
1115 if (path != NULL) {
1116 if (f->flags & FUSE_DEBUG) {
1117 printf("FLUSH[%lu]\n", fi->fh);
1118 fflush(stdout);
1119 }
1120 err = -ENOSYS;
1121 if (f->op.flush)
1122 err = f->op.flush(path, fi);
1123 free(path);
1124 }
1125 pthread_rwlock_unlock(&f->tree_lock);
1126 reply_err(req, err);
1127}
1128
1129static void fuse_release(fuse_req_t req, fuse_ino_t ino,
1130 struct fuse_file_info *fi)
1131{
1132 struct fuse *f = req_fuse_prepare(req);
1133 char *path;
1134 struct node *node;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001135 int unlink_hidden;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001136
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001137 pthread_mutex_lock(&f->lock);
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001138 node = get_node(f, ino);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001139 assert(node->open_count > 0);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001140 --node->open_count;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001141 unlink_hidden = (node->is_hidden && !node->open_count);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001142 pthread_mutex_unlock(&f->lock);
1143
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001144 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001145 path = get_path(f, ino);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001146 if (f->flags & FUSE_DEBUG) {
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001147 printf("RELEASE[%lu] flags: 0x%x\n", fi->fh, fi->flags);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001148 fflush(stdout);
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001149 }
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001150 if (f->op.release) {
1151 if (!f->compat)
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001152 f->op.release(path ? path : "-", fi);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001153 else if (path)
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001154 ((struct fuse_operations_compat2 *) &f->op)->release(path, fi->flags);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001155 }
Miklos Szeredie5183742005-02-02 11:14:04 +00001156
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001157 if(unlink_hidden && path)
1158 f->op.unlink(path);
Miklos Szeredie5183742005-02-02 11:14:04 +00001159
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001160 if (path)
1161 free(path);
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001162 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001163
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001164 reply_err(req, 0);
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001165}
1166
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001167static void fuse_fsync(fuse_req_t req, fuse_ino_t ino, int datasync,
1168 struct fuse_file_info *fi)
Miklos Szeredi5e183482001-10-31 14:52:35 +00001169{
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001170 struct fuse *f = req_fuse_prepare(req);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001171 char *path;
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001172 int err;
Miklos Szerediab974562005-04-07 15:40:21 +00001173
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001174 err = -ENOENT;
1175 pthread_rwlock_rdlock(&f->tree_lock);
1176 path = get_path(f, ino);
1177 if (path != NULL) {
1178 if (f->flags & FUSE_DEBUG) {
1179 printf("FSYNC[%lu]\n", fi->fh);
1180 fflush(stdout);
1181 }
1182 err = -ENOSYS;
1183 if (f->op.fsync)
1184 err = f->op.fsync(path, datasync, fi);
1185 free(path);
1186 }
1187 pthread_rwlock_unlock(&f->tree_lock);
1188 reply_err(req, err);
1189}
1190
1191static struct fuse_dirhandle *get_dirhandle(const struct fuse_file_info *llfi,
1192 struct fuse_file_info *fi)
1193{
1194 struct fuse_dirhandle *dh = (struct fuse_dirhandle *) llfi->fh;
1195 memset(fi, 0, sizeof(struct fuse_file_info));
1196 fi->fh = dh->fh;
1197 return dh;
1198}
1199
1200static void fuse_opendir(fuse_req_t req, fuse_ino_t ino,
1201 struct fuse_file_info *llfi)
1202{
1203 struct fuse *f = req_fuse_prepare(req);
1204 struct fuse_dirhandle *dh;
1205
1206 dh = (struct fuse_dirhandle *) malloc(sizeof(struct fuse_dirhandle));
1207 if (dh == NULL) {
1208 reply_err(req, -ENOMEM);
Miklos Szerediab974562005-04-07 15:40:21 +00001209 return;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001210 }
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001211 memset(dh, 0, sizeof(struct fuse_dirhandle));
1212 dh->fuse = f;
1213 dh->contents = NULL;
1214 dh->len = 0;
1215 dh->filled = 0;
1216 dh->nodeid = ino;
1217 mutex_init(&dh->lock);
Miklos Szerediab974562005-04-07 15:40:21 +00001218
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001219 llfi->fh = (unsigned long) dh;
Miklos Szerediab974562005-04-07 15:40:21 +00001220
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001221 if (f->op.opendir) {
1222 struct fuse_file_info fi;
1223 char *path;
1224 int err;
1225
1226 memset(&fi, 0, sizeof(fi));
1227 fi.flags = llfi->flags;
1228
1229 err = -ENOENT;
1230 pthread_rwlock_rdlock(&f->tree_lock);
1231 path = get_path(f, ino);
1232 if (path != NULL) {
1233 err = f->op.opendir(path, &fi);
1234 dh->fh = fi.fh;
Miklos Szerediab974562005-04-07 15:40:21 +00001235 }
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001236 if (!err) {
1237 pthread_mutex_lock(&f->lock);
1238 if (fuse_reply_open(req, llfi) == -ENOENT) {
1239 /* The opendir syscall was interrupted, so it must be
1240 cancelled */
1241 if(f->op.releasedir)
1242 f->op.releasedir(path, &fi);
1243 pthread_mutex_destroy(&dh->lock);
1244 free(dh);
1245 }
1246 pthread_mutex_unlock(&f->lock);
1247 } else {
1248 reply_err(req, err);
1249 free(dh);
1250 }
Miklos Szerediab974562005-04-07 15:40:21 +00001251 free(path);
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001252 pthread_rwlock_unlock(&f->tree_lock);
1253 } else
1254 fuse_reply_open(req, llfi);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001255}
Miklos Szeredib483c932001-10-29 14:57:57 +00001256
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001257static int fill_dir_common(struct fuse_dirhandle *dh, const char *name,
1258 const struct stat *stat, off_t off)
Miklos Szeredia181e612001-11-06 12:03:23 +00001259{
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001260 struct stat stbuf;
1261 unsigned namelen = strlen(name);
1262 unsigned entsize;
1263 unsigned newlen;
Miklos Szeredi1b188022005-07-28 11:07:29 +00001264 char *newptr;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001265
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001266 if (stat)
1267 stbuf = *stat;
1268 else {
1269 memset(&stbuf, 0, sizeof(stbuf));
1270 stbuf.st_ino = -1;
1271 }
Miklos Szeredia181e612001-11-06 12:03:23 +00001272
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001273 if (!(dh->fuse->flags & FUSE_USE_INO)) {
1274 stbuf.st_ino = (ino_t) -1;
1275 if (dh->fuse->flags & FUSE_READDIR_INO) {
1276 struct node *node;
1277 pthread_mutex_lock(&dh->fuse->lock);
1278 node = lookup_node(dh->fuse, dh->nodeid, name);
1279 if (node)
1280 stbuf.st_ino = (ino_t) node->nodeid;
1281 pthread_mutex_unlock(&dh->fuse->lock);
Miklos Szeredi6ebe2342002-01-06 21:44:16 +00001282 }
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001283 }
Miklos Szeredi6ebe2342002-01-06 21:44:16 +00001284
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001285 entsize = fuse_dirent_size(namelen);
1286 newlen = dh->len + entsize;
1287
1288 if (off) {
1289 dh->filled = 0;
1290 if (newlen > dh->needlen)
1291 return 1;
1292 }
1293
1294 newptr = realloc(dh->contents, newlen);
1295 if (!newptr) {
1296 dh->error = -ENOMEM;
1297 return 1;
1298 }
1299 dh->contents = newptr;
1300 fuse_add_dirent(dh->contents + dh->len, name, &stbuf, off ? off : newlen);
1301 dh->len = newlen;
1302 return 0;
1303}
1304
1305static int fill_dir(void *buf, const char *name, const struct stat *stat,
1306 off_t off)
1307{
1308 return fill_dir_common((struct fuse_dirhandle *) buf, name, stat, off);
1309}
1310
1311static int fill_dir_old(struct fuse_dirhandle *dh, const char *name, int type,
1312 ino_t ino)
1313{
1314 struct stat stbuf;
1315
1316 memset(&stbuf, 0, sizeof(stbuf));
1317 stbuf.st_mode = type << 12;
1318 stbuf.st_ino = ino;
1319
1320 fill_dir_common(dh, name, &stbuf, 0);
1321 return dh->error;
1322}
1323
1324static int readdir_fill(struct fuse *f, fuse_ino_t ino, size_t size,
1325 off_t off, struct fuse_dirhandle *dh,
1326 struct fuse_file_info *fi)
1327{
1328 int err = -ENOENT;
1329 char *path;
1330 pthread_rwlock_rdlock(&f->tree_lock);
1331 path = get_path(f, ino);
1332 if (path != NULL) {
1333 dh->len = 0;
1334 dh->error = 0;
1335 dh->needlen = size;
1336 dh->filled = 1;
1337 err = -ENOSYS;
1338 if (f->op.readdir)
1339 err = f->op.readdir(path, dh, fill_dir, off, fi);
1340 else if (f->op.getdir)
1341 err = f->op.getdir(path, dh, fill_dir_old);
1342 if (!err)
1343 err = dh->error;
1344 if (err)
1345 dh->filled = 0;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001346 free(path);
Miklos Szeredia181e612001-11-06 12:03:23 +00001347 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001348 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001349 return err;
1350}
Miklos Szeredie5183742005-02-02 11:14:04 +00001351
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001352static void fuse_readdir(fuse_req_t req, fuse_ino_t ino, size_t size,
1353 off_t off, struct fuse_file_info *llfi)
1354{
1355 struct fuse *f = req_fuse_prepare(req);
1356 struct fuse_file_info fi;
1357 struct fuse_dirhandle *dh = get_dirhandle(llfi, &fi);
1358
1359 pthread_mutex_lock(&dh->lock);
1360 if (!dh->filled) {
1361 int err = readdir_fill(f, ino, size, off, dh, &fi);
1362 if (err) {
1363 reply_err(req, err);
1364 goto out;
1365 }
Miklos Szeredia181e612001-11-06 12:03:23 +00001366 }
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001367 if (dh->filled) {
1368 if (off < dh->len) {
1369 if (off + size > dh->len)
1370 size = dh->len - off;
1371 } else
1372 size = 0;
1373 } else {
1374 size = dh->len;
1375 off = 0;
1376 }
1377 fuse_reply_buf(req, dh->contents + off, size);
1378 out:
1379 pthread_mutex_unlock(&dh->lock);
1380}
Miklos Szeredia181e612001-11-06 12:03:23 +00001381
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001382static void fuse_releasedir(fuse_req_t req, fuse_ino_t ino,
1383 struct fuse_file_info *llfi)
1384{
1385 struct fuse *f = req_fuse_prepare(req);
Miklos Szeredi9b813af2005-07-21 07:59:37 +00001386 struct fuse_file_info fi;
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001387 struct fuse_dirhandle *dh = get_dirhandle(llfi, &fi);
1388 if (f->op.releasedir) {
1389 char *path;
1390
1391 pthread_rwlock_rdlock(&f->tree_lock);
1392 path = get_path(f, ino);
1393 f->op.releasedir(path ? path : "-", &fi);
1394 free(path);
1395 pthread_rwlock_unlock(&f->tree_lock);
1396 }
1397 pthread_mutex_lock(&dh->lock);
1398 pthread_mutex_unlock(&dh->lock);
1399 pthread_mutex_destroy(&dh->lock);
1400 free(dh->contents);
1401 free(dh);
1402 reply_err(req, 0);
1403}
1404
1405static void fuse_fsyncdir(fuse_req_t req, fuse_ino_t ino, int datasync,
1406 struct fuse_file_info *llfi)
1407{
1408 struct fuse *f = req_fuse_prepare(req);
1409 struct fuse_file_info fi;
1410 char *path;
1411 int err;
1412
1413 get_dirhandle(llfi, &fi);
1414
1415 err = -ENOENT;
1416 pthread_rwlock_rdlock(&f->tree_lock);
1417 path = get_path(f, ino);
1418 if (path != NULL) {
1419 err = -ENOSYS;
1420 if (f->op.fsyncdir)
1421 err = f->op.fsyncdir(path, datasync, &fi);
1422 free(path);
1423 }
1424 pthread_rwlock_unlock(&f->tree_lock);
1425 reply_err(req, err);
Miklos Szeredia181e612001-11-06 12:03:23 +00001426}
1427
Miklos Szeredi77f39942004-03-25 11:17:52 +00001428static int default_statfs(struct statfs *buf)
1429{
1430 buf->f_namelen = 255;
1431 buf->f_bsize = 512;
1432 return 0;
1433}
1434
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001435static void convert_statfs_compat(struct fuse_statfs_compat1 *compatbuf,
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001436 struct statfs *statfs)
1437{
1438 statfs->f_bsize = compatbuf->block_size;
1439 statfs->f_blocks = compatbuf->blocks;
1440 statfs->f_bfree = compatbuf->blocks_free;
1441 statfs->f_bavail = compatbuf->blocks_free;
1442 statfs->f_files = compatbuf->files;
1443 statfs->f_ffree = compatbuf->files_free;
1444 statfs->f_namelen = compatbuf->namelen;
1445}
1446
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001447static void fuse_statfs(fuse_req_t req)
Miklos Szeredi18e75e42004-02-19 14:23:27 +00001448{
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001449 struct fuse *f = req_fuse_prepare(req);
Miklos Szeredi18e75e42004-02-19 14:23:27 +00001450 struct statfs buf;
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001451 int err;
Mark Glinesd84b39a2002-01-07 16:32:02 +00001452
Miklos Szeredi77f39942004-03-25 11:17:52 +00001453 memset(&buf, 0, sizeof(struct statfs));
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001454 if (f->op.statfs) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001455 if (!f->compat || f->compat > 11)
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001456 err = f->op.statfs("/", &buf);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001457 else {
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001458 struct fuse_statfs_compat1 compatbuf;
1459 memset(&compatbuf, 0, sizeof(struct fuse_statfs_compat1));
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001460 err = ((struct fuse_operations_compat1 *) &f->op)->statfs(&compatbuf);
1461 if (!err)
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001462 convert_statfs_compat(&compatbuf, &buf);
1463 }
1464 }
Miklos Szeredi77f39942004-03-25 11:17:52 +00001465 else
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001466 err = default_statfs(&buf);
Miklos Szeredi77f39942004-03-25 11:17:52 +00001467
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001468 if (!err)
1469 fuse_reply_statfs(req, &buf);
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001470 else
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001471 reply_err(req, err);
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001472}
1473
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001474static void fuse_setxattr(fuse_req_t req, fuse_ino_t ino, const char *name,
1475 const char *value, size_t size, int flags)
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001476{
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001477 struct fuse *f = req_fuse_prepare(req);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001478 char *path;
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001479 int err;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001480
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001481 err = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001482 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001483 path = get_path(f, ino);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001484 if (path != NULL) {
Miklos Szerediab974562005-04-07 15:40:21 +00001485 err = -ENOSYS;
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001486 if (f->op.setxattr)
1487 err = f->op.setxattr(path, name, value, size, flags);
1488 free(path);
1489 }
1490 pthread_rwlock_unlock(&f->tree_lock);
1491 reply_err(req, err);
1492}
1493
1494static int common_getxattr(struct fuse *f, fuse_ino_t ino, const char *name,
1495 char *value, size_t size)
1496{
1497 int err;
1498 char *path;
1499
1500 err = -ENOENT;
1501 pthread_rwlock_rdlock(&f->tree_lock);
1502 path = get_path(f, ino);
1503 if (path != NULL) {
1504 err = -ENOSYS;
1505 if (f->op.getxattr)
1506 err = f->op.getxattr(path, name, value, size);
Miklos Szerediab974562005-04-07 15:40:21 +00001507 free(path);
1508 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001509 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szerediab974562005-04-07 15:40:21 +00001510 return err;
1511}
1512
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001513static void fuse_getxattr(fuse_req_t req, fuse_ino_t ino, const char *name,
1514 size_t size)
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001515{
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001516 struct fuse *f = req_fuse_prepare(req);
Miklos Szeredi4283ee72005-03-21 12:09:04 +00001517 int res;
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001518
1519 if (size) {
1520 char *value = (char *) malloc(size);
1521 if (value == NULL) {
1522 reply_err(req, -ENOMEM);
1523 return;
1524 }
1525 res = common_getxattr(f, ino, name, value, size);
1526 if (res > 0)
1527 fuse_reply_buf(req, value, res);
1528 else
1529 reply_err(req, res);
1530 free(value);
1531 } else {
1532 res = common_getxattr(f, ino, name, NULL, 0);
1533 if (res >= 0)
1534 fuse_reply_xattr(req, res);
1535 else
1536 reply_err(req, res);
1537 }
1538}
1539
1540static int common_listxattr(struct fuse *f, fuse_ino_t ino, char *list,
1541 size_t size)
1542{
Miklos Szeredi4283ee72005-03-21 12:09:04 +00001543 char *path;
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001544 int err;
Miklos Szeredi4283ee72005-03-21 12:09:04 +00001545
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001546 err = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001547 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001548 path = get_path(f, ino);
Miklos Szeredi4283ee72005-03-21 12:09:04 +00001549 if (path != NULL) {
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001550 err = -ENOSYS;
1551 if (f->op.listxattr)
1552 err = f->op.listxattr(path, list, size);
Miklos Szeredi4283ee72005-03-21 12:09:04 +00001553 free(path);
1554 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001555 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001556 return err;
Miklos Szeredi4283ee72005-03-21 12:09:04 +00001557}
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001558
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001559static void fuse_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size)
Miklos Szeredi43696432001-11-18 19:15:05 +00001560{
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001561 struct fuse *f = req_fuse_prepare(req);
1562 int res;
1563
1564 if (size) {
1565 char *list = (char *) malloc(size);
1566 if (list == NULL) {
1567 reply_err(req, -ENOMEM);
1568 return;
1569 }
1570 res = common_listxattr(f, ino, list, size);
1571 if (res > 0)
1572 fuse_reply_buf(req, list, res);
1573 else
1574 reply_err(req, res);
1575 free(list);
1576 } else {
1577 res = common_listxattr(f, ino, NULL, 0);
1578 if (res >= 0)
1579 fuse_reply_xattr(req, res);
1580 else
1581 reply_err(req, res);
1582 }
Miklos Szeredi43696432001-11-18 19:15:05 +00001583}
1584
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001585static void fuse_removexattr(fuse_req_t req, fuse_ino_t ino, const char *name)
1586{
1587 struct fuse *f = req_fuse_prepare(req);
1588 char *path;
1589 int err;
1590
1591 err = -ENOENT;
1592 pthread_rwlock_rdlock(&f->tree_lock);
1593 path = get_path(f, ino);
1594 if (path != NULL) {
1595 err = -ENOSYS;
1596 if (f->op.removexattr)
1597 err = f->op.removexattr(path, name);
1598 free(path);
1599 }
1600 pthread_rwlock_unlock(&f->tree_lock);
1601 reply_err(req, err);
1602}
1603
1604static struct fuse_ll_operations fuse_path_ops = {
1605 .init = fuse_data_init,
1606 .destroy = fuse_data_destroy,
1607 .lookup = fuse_lookup,
1608 .forget = fuse_forget,
1609 .getattr = fuse_getattr,
1610 .setattr = fuse_setattr,
Miklos Szeredi7b28eae2005-08-01 12:48:30 +00001611 .access = fuse_access,
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001612 .readlink = fuse_readlink,
1613 .mknod = fuse_mknod,
1614 .mkdir = fuse_mkdir,
1615 .unlink = fuse_unlink,
1616 .rmdir = fuse_rmdir,
1617 .symlink = fuse_symlink,
1618 .rename = fuse_rename,
1619 .link = fuse_link,
1620 .open = fuse_open,
1621 .read = fuse_read,
1622 .write = fuse_write,
1623 .flush = fuse_flush,
1624 .release = fuse_release,
1625 .fsync = fuse_fsync,
1626 .opendir = fuse_opendir,
1627 .readdir = fuse_readdir,
1628 .releasedir = fuse_releasedir,
1629 .fsyncdir = fuse_fsyncdir,
1630 .statfs = fuse_statfs,
1631 .setxattr = fuse_setxattr,
1632 .getxattr = fuse_getxattr,
1633 .listxattr = fuse_listxattr,
1634 .removexattr = fuse_removexattr,
1635};
1636
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001637void fuse_process_cmd(struct fuse *f, struct fuse_cmd *cmd)
Miklos Szeredia181e612001-11-06 12:03:23 +00001638{
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001639 fuse_ll_process_cmd(f->fll, cmd);
Miklos Szeredia181e612001-11-06 12:03:23 +00001640}
1641
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001642int fuse_exited(struct fuse *f)
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001643{
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001644 return fuse_ll_exited(f->fll);
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001645}
1646
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001647struct fuse_cmd *fuse_read_cmd(struct fuse *f)
Miklos Szeredi2df1c042001-11-06 15:07:17 +00001648{
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001649 return fuse_ll_read_cmd(f->fll);
Miklos Szeredi2df1c042001-11-06 15:07:17 +00001650}
1651
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001652int fuse_loop(struct fuse *f)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001653{
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001654 if (f)
1655 return fuse_ll_loop(f->fll);
1656 else
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001657 return -1;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001658}
1659
Miklos Szeredi891b8742004-07-29 09:27:49 +00001660int fuse_invalidate(struct fuse *f, const char *path)
1661{
Miklos Szeredie56818b2004-12-12 11:45:24 +00001662 (void) f;
1663 (void) path;
1664 return -EINVAL;
Miklos Szeredi891b8742004-07-29 09:27:49 +00001665}
1666
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001667void fuse_exit(struct fuse *f)
1668{
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001669 fuse_ll_exit(f->fll);
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001670}
1671
Miklos Szeredid169f312004-09-22 08:48:26 +00001672struct fuse_context *fuse_get_context()
Miklos Szeredi2e50d432001-12-20 12:17:25 +00001673{
Miklos Szeredid169f312004-09-22 08:48:26 +00001674 static struct fuse_context context;
1675 if (fuse_getcontext)
1676 return fuse_getcontext();
Miklos Szeredi2e50d432001-12-20 12:17:25 +00001677 else
Miklos Szeredid169f312004-09-22 08:48:26 +00001678 return &context;
1679}
1680
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001681void fuse_set_getcontext_func(struct fuse_context *(*func)(void))
Miklos Szeredid169f312004-09-22 08:48:26 +00001682{
1683 fuse_getcontext = func;
Miklos Szeredi2e50d432001-12-20 12:17:25 +00001684}
1685
Miklos Szeredie2aa2e22005-07-15 13:31:36 +00001686struct fuse_ll *fuse_get_lowlevel(struct fuse *f)
1687{
1688 return f->fll;
1689}
1690
Miklos Szeredie331c4b2005-07-06 13:34:02 +00001691static int begins_with(const char *s, const char *beg)
1692{
1693 if (strncmp(s, beg, strlen(beg)) == 0)
1694 return 1;
1695 else
1696 return 0;
1697}
1698
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001699int fuse_is_lib_option(const char *opt)
1700{
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001701 if (fuse_ll_is_lib_option(opt) ||
1702 strcmp(opt, "debug") == 0 ||
Miklos Szeredia13d9002004-11-02 17:32:03 +00001703 strcmp(opt, "hard_remove") == 0 ||
Miklos Szeredi0111f9d2005-04-22 12:04:55 +00001704 strcmp(opt, "use_ino") == 0 ||
Miklos Szeredi33be22d2005-05-27 09:12:43 +00001705 strcmp(opt, "allow_root") == 0 ||
Miklos Szeredie331c4b2005-07-06 13:34:02 +00001706 strcmp(opt, "readdir_ino") == 0 ||
Miklos Szeredie77cc072005-08-01 11:58:51 +00001707 strcmp(opt, "direct_io") == 0 ||
1708 strcmp(opt, "kernel_cache") == 0 ||
Miklos Szeredie331c4b2005-07-06 13:34:02 +00001709 begins_with(opt, "umask=") ||
1710 begins_with(opt, "uid=") ||
1711 begins_with(opt, "gid="))
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001712 return 1;
1713 else
1714 return 0;
1715}
1716
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001717static int parse_lib_opts(struct fuse *f, const char *opts, char **llopts)
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001718{
1719 if (opts) {
1720 char *xopts = strdup(opts);
1721 char *s = xopts;
1722 char *opt;
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001723 char *d = xopts;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001724
Miklos Szeredie56818b2004-12-12 11:45:24 +00001725 if (xopts == NULL) {
1726 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001727 return -1;
Miklos Szeredie56818b2004-12-12 11:45:24 +00001728 }
Miklos Szeredie5183742005-02-02 11:14:04 +00001729
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001730 while((opt = strsep(&s, ","))) {
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001731 if (fuse_ll_is_lib_option(opt)) {
1732 size_t optlen = strlen(opt);
1733 if (strcmp(opt, "debug") == 0)
Miklos Szeredi9b813af2005-07-21 07:59:37 +00001734 f->flags |= FUSE_DEBUG;
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001735 memmove(d, opt, optlen);
1736 d += optlen;
1737 *d++ = ',';
1738 } else if (strcmp(opt, "hard_remove") == 0)
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001739 f->flags |= FUSE_HARD_REMOVE;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001740 else if (strcmp(opt, "use_ino") == 0)
1741 f->flags |= FUSE_USE_INO;
Miklos Szeredi33be22d2005-05-27 09:12:43 +00001742 else if (strcmp(opt, "readdir_ino") == 0)
1743 f->flags |= FUSE_READDIR_INO;
Miklos Szeredie77cc072005-08-01 11:58:51 +00001744 else if (strcmp(opt, "direct_io") == 0)
1745 f->flags |= FUSE_DIRECT_IO;
1746 else if (strcmp(opt, "kernel_cache") == 0)
1747 f->flags |= FUSE_KERNEL_CACHE;
Miklos Szeredie331c4b2005-07-06 13:34:02 +00001748 else if (sscanf(opt, "umask=%o", &f->umask) == 1)
1749 f->flags |= FUSE_SET_MODE;
1750 else if (sscanf(opt, "uid=%u", &f->uid) == 1)
1751 f->flags |= FUSE_SET_UID;
1752 else if(sscanf(opt, "gid=%u", &f->gid) == 1)
1753 f->flags |= FUSE_SET_GID;
Miklos Szeredie5183742005-02-02 11:14:04 +00001754 else
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001755 fprintf(stderr, "fuse: warning: unknown option `%s'\n", opt);
1756 }
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001757 if (d != xopts) {
1758 d[-1] = '\0';
1759 *llopts = xopts;
1760 }
1761 else
1762 free(xopts);
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001763 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001764 return 0;
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001765}
1766
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001767struct fuse *fuse_new_common(int fd, const char *opts,
1768 const struct fuse_operations *op,
1769 size_t op_size, int compat)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001770{
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001771 struct fuse *f;
1772 struct node *root;
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001773 char *llopts = NULL;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001774
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001775 if (sizeof(struct fuse_operations) < op_size) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001776 fprintf(stderr, "fuse: warning: library too old, some operations may not not work\n");
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001777 op_size = sizeof(struct fuse_operations);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001778 }
1779
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001780 f = (struct fuse *) calloc(1, sizeof(struct fuse));
Miklos Szeredie56818b2004-12-12 11:45:24 +00001781 if (f == NULL) {
1782 fprintf(stderr, "fuse: failed to allocate fuse object\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001783 goto out;
Miklos Szeredie56818b2004-12-12 11:45:24 +00001784 }
Miklos Szeredi2df1c042001-11-06 15:07:17 +00001785
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001786 if (parse_lib_opts(f, opts, &llopts) == -1)
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001787 goto out_free;
1788
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001789 f->fll = fuse_ll_new(fd, llopts, &fuse_path_ops, sizeof(fuse_path_ops), f);
1790 free(llopts);
1791 if (f->fll == NULL)
1792 goto out_free;
1793
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001794 f->ctr = 0;
Miklos Szeredi76f65782004-02-19 16:55:40 +00001795 f->generation = 0;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001796 /* FIXME: Dynamic hash table */
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001797 f->name_table_size = 14057;
1798 f->name_table = (struct node **)
1799 calloc(1, sizeof(struct node *) * f->name_table_size);
Miklos Szeredie56818b2004-12-12 11:45:24 +00001800 if (f->name_table == NULL) {
1801 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001802 goto out_free;
Miklos Szeredie56818b2004-12-12 11:45:24 +00001803 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001804
Miklos Szeredia13d9002004-11-02 17:32:03 +00001805 f->id_table_size = 14057;
1806 f->id_table = (struct node **)
1807 calloc(1, sizeof(struct node *) * f->id_table_size);
Miklos Szeredie56818b2004-12-12 11:45:24 +00001808 if (f->id_table == NULL) {
1809 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001810 goto out_free_name_table;
Miklos Szeredie56818b2004-12-12 11:45:24 +00001811 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001812
Miklos Szerediab974562005-04-07 15:40:21 +00001813 mutex_init(&f->lock);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001814 memcpy(&f->op, op, op_size);
1815 f->compat = compat;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001816
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001817 root = (struct node *) calloc(1, sizeof(struct node));
Miklos Szeredie56818b2004-12-12 11:45:24 +00001818 if (root == NULL) {
1819 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredia13d9002004-11-02 17:32:03 +00001820 goto out_free_id_table;
Miklos Szeredie56818b2004-12-12 11:45:24 +00001821 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001822
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001823 root->name = strdup("/");
Miklos Szeredie56818b2004-12-12 11:45:24 +00001824 if (root->name == NULL) {
1825 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001826 goto out_free_root;
Miklos Szeredie56818b2004-12-12 11:45:24 +00001827 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001828
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001829 root->parent = 0;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001830 root->nodeid = FUSE_ROOT_ID;
Miklos Szeredi76f65782004-02-19 16:55:40 +00001831 root->generation = 0;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001832 root->refctr = 1;
Miklos Szeredi38009022005-05-08 19:47:22 +00001833 root->nlookup = 1;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001834 hash_id(f, root);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001835
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001836 return f;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001837
1838 out_free_root:
1839 free(root);
Miklos Szeredia13d9002004-11-02 17:32:03 +00001840 out_free_id_table:
1841 free(f->id_table);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001842 out_free_name_table:
1843 free(f->name_table);
1844 out_free:
1845 free(f);
1846 out:
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001847 return NULL;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001848}
1849
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001850struct fuse *fuse_new(int fd, const char *opts,
1851 const struct fuse_operations *op, size_t op_size)
1852{
1853 return fuse_new_common(fd, opts, op, op_size, 0);
1854}
1855
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001856struct fuse *fuse_new_compat2(int fd, const char *opts,
1857 const struct fuse_operations_compat2 *op)
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001858{
1859 return fuse_new_common(fd, opts, (struct fuse_operations *) op,
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001860 sizeof(struct fuse_operations_compat2), 21);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001861}
1862
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001863struct fuse *fuse_new_compat1(int fd, int flags,
1864 const struct fuse_operations_compat1 *op)
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001865{
1866 char *opts = NULL;
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001867 if (flags & FUSE_DEBUG_COMPAT1)
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001868 opts = "debug";
1869 return fuse_new_common(fd, opts, (struct fuse_operations *) op,
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001870 sizeof(struct fuse_operations_compat1), 11);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001871}
1872
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001873void fuse_destroy(struct fuse *f)
1874{
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001875 size_t i;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001876 for (i = 0; i < f->id_table_size; i++) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001877 struct node *node;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001878
Miklos Szeredia13d9002004-11-02 17:32:03 +00001879 for (node = f->id_table[i]; node != NULL; node = node->id_next) {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001880 if (node->is_hidden) {
Miklos Szeredia13d9002004-11-02 17:32:03 +00001881 char *path = get_path(f, node->nodeid);
Miklos Szeredi21019c92005-05-09 11:22:41 +00001882 if (path) {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001883 f->op.unlink(path);
Miklos Szeredi21019c92005-05-09 11:22:41 +00001884 free(path);
1885 }
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001886 }
1887 }
1888 }
Miklos Szeredia13d9002004-11-02 17:32:03 +00001889 for (i = 0; i < f->id_table_size; i++) {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001890 struct node *node;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001891 struct node *next;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001892
Miklos Szeredia13d9002004-11-02 17:32:03 +00001893 for (node = f->id_table[i]; node != NULL; node = next) {
1894 next = node->id_next;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001895 free_node(node);
1896 }
1897 }
Miklos Szeredia13d9002004-11-02 17:32:03 +00001898 free(f->id_table);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001899 free(f->name_table);
Miklos Szeredia181e612001-11-06 12:03:23 +00001900 pthread_mutex_destroy(&f->lock);
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001901 fuse_ll_destroy(f->fll);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001902 free(f);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001903}
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001904
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001905__asm__(".symver fuse_exited,__fuse_exited@");
1906__asm__(".symver fuse_process_cmd,__fuse_process_cmd@");
1907__asm__(".symver fuse_read_cmd,__fuse_read_cmd@");
1908__asm__(".symver fuse_set_getcontext_func,__fuse_set_getcontext_func@");
1909__asm__(".symver fuse_new_compat2,fuse_new@");