blob: a798eaf35ab9f51f156601a2210f8f08fa2c5c23 [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 Szeredi31066bb2005-08-01 14:49:31 +0000985 char *path = NULL;
986 int err = 0;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000987
Miklos Szeredic2a33ee2005-05-09 13:29:17 +0000988 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredi31066bb2005-08-01 14:49:31 +0000989 if (f->op.open) {
990 err = -ENOENT;
991 path = get_path(f, ino);
992 if (path != NULL) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +0000993 if (!f->compat)
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000994 err = f->op.open(path, fi);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +0000995 else
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000996 err = ((struct fuse_operations_compat2 *) &f->op)->open(path, fi->flags);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +0000997 }
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000998 }
Miklos Szeredibd10a7b2005-07-15 09:59:59 +0000999 if (!err) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001000 if (f->flags & FUSE_DEBUG) {
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001001 printf("OPEN[%lu] flags: 0x%x\n", fi->fh, fi->flags);
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001002 fflush(stdout);
1003 }
Miklos Szeredie5183742005-02-02 11:14:04 +00001004
Miklos Szeredie77cc072005-08-01 11:58:51 +00001005 if (f->flags & FUSE_DIRECT_IO)
1006 fi->direct_io = 1;
1007 if (f->flags & FUSE_KERNEL_CACHE)
1008 fi->keep_cache = 1;
1009
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001010 pthread_mutex_lock(&f->lock);
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001011 if (fuse_reply_open(req, fi) == -ENOENT) {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001012 /* The open syscall was interrupted, so it must be cancelled */
Miklos Szeredi31066bb2005-08-01 14:49:31 +00001013 if(f->op.release && path != NULL) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001014 if (!f->compat)
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001015 f->op.release(path, fi);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001016 else
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001017 ((struct fuse_operations_compat2 *) &f->op)->release(path, fi->flags);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001018 }
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001019 } else {
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001020 struct node *node = get_node(f, ino);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001021 node->open_count ++;
1022 }
Miklos Szeredi73798f92004-07-12 15:55:11 +00001023 pthread_mutex_unlock(&f->lock);
Miklos Szeredi73798f92004-07-12 15:55:11 +00001024 } else
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001025 reply_err(req, err);
Miklos Szeredi73798f92004-07-12 15:55:11 +00001026
Miklos Szeredi9a31cca2004-06-26 21:11:25 +00001027 if (path)
1028 free(path);
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001029 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001030}
1031
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001032static void fuse_read(fuse_req_t req, fuse_ino_t ino, size_t size, off_t off,
1033 struct fuse_file_info *fi)
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001034{
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001035 struct fuse *f = req_fuse_prepare(req);
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001036 char *path;
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001037 char *buf;
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001038 int res;
1039
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001040 buf = (char *) malloc(size);
1041 if (buf == NULL) {
1042 reply_err(req, -ENOMEM);
1043 return;
1044 }
1045
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001046 res = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001047 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001048 path = get_path(f, ino);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001049 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001050 if (f->flags & FUSE_DEBUG) {
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001051 printf("READ[%lu] %u bytes from %llu\n", fi->fh, size, off);
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001052 fflush(stdout);
1053 }
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001054
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001055 res = -ENOSYS;
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001056 if (f->op.read)
1057 res = f->op.read(path, buf, size, off, fi);
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001058 free(path);
1059 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001060 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001061
1062 if (res >= 0) {
1063 if (f->flags & FUSE_DEBUG) {
1064 printf(" READ[%lu] %u bytes\n", fi->fh, res);
1065 fflush(stdout);
1066 }
1067 fuse_reply_buf(req, buf, res);
1068 } else
1069 reply_err(req, res);
1070
1071 free(buf);
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001072}
1073
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001074static void fuse_write(fuse_req_t req, fuse_ino_t ino, const char *buf,
1075 size_t size, off_t off, struct fuse_file_info *fi)
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001076{
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001077 struct fuse *f = req_fuse_prepare(req);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001078 char *path;
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001079 int res;
1080
1081 res = -ENOENT;
1082 pthread_rwlock_rdlock(&f->tree_lock);
1083 path = get_path(f, ino);
1084 if (path != NULL) {
1085 if (f->flags & FUSE_DEBUG) {
1086 printf("WRITE%s[%lu] %u bytes to %llu\n",
1087 fi->writepage ? "PAGE" : "", fi->fh, size, off);
1088 fflush(stdout);
1089 }
1090
1091 res = -ENOSYS;
1092 if (f->op.write)
1093 res = f->op.write(path, buf, size, off, fi);
1094 free(path);
1095 }
1096 pthread_rwlock_unlock(&f->tree_lock);
1097
1098 if (res >= 0)
1099 fuse_reply_write(req, res);
1100 else
1101 reply_err(req, res);
1102}
1103
1104static void fuse_flush(fuse_req_t req, fuse_ino_t ino,
1105 struct fuse_file_info *fi)
1106{
1107 struct fuse *f = req_fuse_prepare(req);
1108 char *path;
1109 int err;
1110
1111 err = -ENOENT;
1112 pthread_rwlock_rdlock(&f->tree_lock);
1113 path = get_path(f, ino);
1114 if (path != NULL) {
1115 if (f->flags & FUSE_DEBUG) {
1116 printf("FLUSH[%lu]\n", fi->fh);
1117 fflush(stdout);
1118 }
1119 err = -ENOSYS;
1120 if (f->op.flush)
1121 err = f->op.flush(path, fi);
1122 free(path);
1123 }
1124 pthread_rwlock_unlock(&f->tree_lock);
1125 reply_err(req, err);
1126}
1127
1128static void fuse_release(fuse_req_t req, fuse_ino_t ino,
1129 struct fuse_file_info *fi)
1130{
1131 struct fuse *f = req_fuse_prepare(req);
1132 char *path;
1133 struct node *node;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001134 int unlink_hidden;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001135
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001136 pthread_mutex_lock(&f->lock);
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001137 node = get_node(f, ino);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001138 assert(node->open_count > 0);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001139 --node->open_count;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001140 unlink_hidden = (node->is_hidden && !node->open_count);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001141 pthread_mutex_unlock(&f->lock);
1142
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001143 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001144 path = get_path(f, ino);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001145 if (f->flags & FUSE_DEBUG) {
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001146 printf("RELEASE[%lu] flags: 0x%x\n", fi->fh, fi->flags);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001147 fflush(stdout);
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001148 }
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001149 if (f->op.release) {
1150 if (!f->compat)
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001151 f->op.release(path ? path : "-", fi);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001152 else if (path)
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001153 ((struct fuse_operations_compat2 *) &f->op)->release(path, fi->flags);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001154 }
Miklos Szeredie5183742005-02-02 11:14:04 +00001155
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001156 if(unlink_hidden && path)
1157 f->op.unlink(path);
Miklos Szeredie5183742005-02-02 11:14:04 +00001158
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001159 if (path)
1160 free(path);
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001161 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001162
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001163 reply_err(req, 0);
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001164}
1165
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001166static void fuse_fsync(fuse_req_t req, fuse_ino_t ino, int datasync,
1167 struct fuse_file_info *fi)
Miklos Szeredi5e183482001-10-31 14:52:35 +00001168{
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001169 struct fuse *f = req_fuse_prepare(req);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001170 char *path;
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001171 int err;
Miklos Szerediab974562005-04-07 15:40:21 +00001172
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001173 err = -ENOENT;
1174 pthread_rwlock_rdlock(&f->tree_lock);
1175 path = get_path(f, ino);
1176 if (path != NULL) {
1177 if (f->flags & FUSE_DEBUG) {
1178 printf("FSYNC[%lu]\n", fi->fh);
1179 fflush(stdout);
1180 }
1181 err = -ENOSYS;
1182 if (f->op.fsync)
1183 err = f->op.fsync(path, datasync, fi);
1184 free(path);
1185 }
1186 pthread_rwlock_unlock(&f->tree_lock);
1187 reply_err(req, err);
1188}
1189
1190static struct fuse_dirhandle *get_dirhandle(const struct fuse_file_info *llfi,
1191 struct fuse_file_info *fi)
1192{
1193 struct fuse_dirhandle *dh = (struct fuse_dirhandle *) llfi->fh;
1194 memset(fi, 0, sizeof(struct fuse_file_info));
1195 fi->fh = dh->fh;
1196 return dh;
1197}
1198
1199static void fuse_opendir(fuse_req_t req, fuse_ino_t ino,
1200 struct fuse_file_info *llfi)
1201{
1202 struct fuse *f = req_fuse_prepare(req);
1203 struct fuse_dirhandle *dh;
1204
1205 dh = (struct fuse_dirhandle *) malloc(sizeof(struct fuse_dirhandle));
1206 if (dh == NULL) {
1207 reply_err(req, -ENOMEM);
Miklos Szerediab974562005-04-07 15:40:21 +00001208 return;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001209 }
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001210 memset(dh, 0, sizeof(struct fuse_dirhandle));
1211 dh->fuse = f;
1212 dh->contents = NULL;
1213 dh->len = 0;
1214 dh->filled = 0;
1215 dh->nodeid = ino;
1216 mutex_init(&dh->lock);
Miklos Szerediab974562005-04-07 15:40:21 +00001217
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001218 llfi->fh = (unsigned long) dh;
Miklos Szerediab974562005-04-07 15:40:21 +00001219
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001220 if (f->op.opendir) {
1221 struct fuse_file_info fi;
1222 char *path;
1223 int err;
1224
1225 memset(&fi, 0, sizeof(fi));
1226 fi.flags = llfi->flags;
1227
1228 err = -ENOENT;
1229 pthread_rwlock_rdlock(&f->tree_lock);
1230 path = get_path(f, ino);
1231 if (path != NULL) {
1232 err = f->op.opendir(path, &fi);
1233 dh->fh = fi.fh;
Miklos Szerediab974562005-04-07 15:40:21 +00001234 }
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001235 if (!err) {
1236 pthread_mutex_lock(&f->lock);
1237 if (fuse_reply_open(req, llfi) == -ENOENT) {
1238 /* The opendir syscall was interrupted, so it must be
1239 cancelled */
1240 if(f->op.releasedir)
1241 f->op.releasedir(path, &fi);
1242 pthread_mutex_destroy(&dh->lock);
1243 free(dh);
1244 }
1245 pthread_mutex_unlock(&f->lock);
1246 } else {
1247 reply_err(req, err);
1248 free(dh);
1249 }
Miklos Szerediab974562005-04-07 15:40:21 +00001250 free(path);
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001251 pthread_rwlock_unlock(&f->tree_lock);
1252 } else
1253 fuse_reply_open(req, llfi);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001254}
Miklos Szeredib483c932001-10-29 14:57:57 +00001255
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001256static int fill_dir_common(struct fuse_dirhandle *dh, const char *name,
1257 const struct stat *stat, off_t off)
Miklos Szeredia181e612001-11-06 12:03:23 +00001258{
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001259 struct stat stbuf;
1260 unsigned namelen = strlen(name);
1261 unsigned entsize;
1262 unsigned newlen;
Miklos Szeredi1b188022005-07-28 11:07:29 +00001263 char *newptr;
Miklos Szeredifb28c5e2004-11-26 12:15:06 +00001264
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001265 if (stat)
1266 stbuf = *stat;
1267 else {
1268 memset(&stbuf, 0, sizeof(stbuf));
1269 stbuf.st_ino = -1;
1270 }
Miklos Szeredia181e612001-11-06 12:03:23 +00001271
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001272 if (!(dh->fuse->flags & FUSE_USE_INO)) {
1273 stbuf.st_ino = (ino_t) -1;
1274 if (dh->fuse->flags & FUSE_READDIR_INO) {
1275 struct node *node;
1276 pthread_mutex_lock(&dh->fuse->lock);
1277 node = lookup_node(dh->fuse, dh->nodeid, name);
1278 if (node)
1279 stbuf.st_ino = (ino_t) node->nodeid;
1280 pthread_mutex_unlock(&dh->fuse->lock);
Miklos Szeredi6ebe2342002-01-06 21:44:16 +00001281 }
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001282 }
Miklos Szeredi6ebe2342002-01-06 21:44:16 +00001283
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001284 entsize = fuse_dirent_size(namelen);
1285 newlen = dh->len + entsize;
1286
1287 if (off) {
1288 dh->filled = 0;
1289 if (newlen > dh->needlen)
1290 return 1;
1291 }
1292
1293 newptr = realloc(dh->contents, newlen);
1294 if (!newptr) {
1295 dh->error = -ENOMEM;
1296 return 1;
1297 }
1298 dh->contents = newptr;
1299 fuse_add_dirent(dh->contents + dh->len, name, &stbuf, off ? off : newlen);
1300 dh->len = newlen;
1301 return 0;
1302}
1303
1304static int fill_dir(void *buf, const char *name, const struct stat *stat,
1305 off_t off)
1306{
1307 return fill_dir_common((struct fuse_dirhandle *) buf, name, stat, off);
1308}
1309
1310static int fill_dir_old(struct fuse_dirhandle *dh, const char *name, int type,
1311 ino_t ino)
1312{
1313 struct stat stbuf;
1314
1315 memset(&stbuf, 0, sizeof(stbuf));
1316 stbuf.st_mode = type << 12;
1317 stbuf.st_ino = ino;
1318
1319 fill_dir_common(dh, name, &stbuf, 0);
1320 return dh->error;
1321}
1322
1323static int readdir_fill(struct fuse *f, fuse_ino_t ino, size_t size,
1324 off_t off, struct fuse_dirhandle *dh,
1325 struct fuse_file_info *fi)
1326{
1327 int err = -ENOENT;
1328 char *path;
1329 pthread_rwlock_rdlock(&f->tree_lock);
1330 path = get_path(f, ino);
1331 if (path != NULL) {
1332 dh->len = 0;
1333 dh->error = 0;
1334 dh->needlen = size;
1335 dh->filled = 1;
1336 err = -ENOSYS;
1337 if (f->op.readdir)
1338 err = f->op.readdir(path, dh, fill_dir, off, fi);
1339 else if (f->op.getdir)
1340 err = f->op.getdir(path, dh, fill_dir_old);
1341 if (!err)
1342 err = dh->error;
1343 if (err)
1344 dh->filled = 0;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001345 free(path);
Miklos Szeredia181e612001-11-06 12:03:23 +00001346 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001347 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001348 return err;
1349}
Miklos Szeredie5183742005-02-02 11:14:04 +00001350
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001351static void fuse_readdir(fuse_req_t req, fuse_ino_t ino, size_t size,
1352 off_t off, struct fuse_file_info *llfi)
1353{
1354 struct fuse *f = req_fuse_prepare(req);
1355 struct fuse_file_info fi;
1356 struct fuse_dirhandle *dh = get_dirhandle(llfi, &fi);
1357
1358 pthread_mutex_lock(&dh->lock);
1359 if (!dh->filled) {
1360 int err = readdir_fill(f, ino, size, off, dh, &fi);
1361 if (err) {
1362 reply_err(req, err);
1363 goto out;
1364 }
Miklos Szeredia181e612001-11-06 12:03:23 +00001365 }
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001366 if (dh->filled) {
1367 if (off < dh->len) {
1368 if (off + size > dh->len)
1369 size = dh->len - off;
1370 } else
1371 size = 0;
1372 } else {
1373 size = dh->len;
1374 off = 0;
1375 }
1376 fuse_reply_buf(req, dh->contents + off, size);
1377 out:
1378 pthread_mutex_unlock(&dh->lock);
1379}
Miklos Szeredia181e612001-11-06 12:03:23 +00001380
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001381static void fuse_releasedir(fuse_req_t req, fuse_ino_t ino,
1382 struct fuse_file_info *llfi)
1383{
1384 struct fuse *f = req_fuse_prepare(req);
Miklos Szeredi9b813af2005-07-21 07:59:37 +00001385 struct fuse_file_info fi;
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001386 struct fuse_dirhandle *dh = get_dirhandle(llfi, &fi);
1387 if (f->op.releasedir) {
1388 char *path;
1389
1390 pthread_rwlock_rdlock(&f->tree_lock);
1391 path = get_path(f, ino);
1392 f->op.releasedir(path ? path : "-", &fi);
1393 free(path);
1394 pthread_rwlock_unlock(&f->tree_lock);
1395 }
1396 pthread_mutex_lock(&dh->lock);
1397 pthread_mutex_unlock(&dh->lock);
1398 pthread_mutex_destroy(&dh->lock);
1399 free(dh->contents);
1400 free(dh);
1401 reply_err(req, 0);
1402}
1403
1404static void fuse_fsyncdir(fuse_req_t req, fuse_ino_t ino, int datasync,
1405 struct fuse_file_info *llfi)
1406{
1407 struct fuse *f = req_fuse_prepare(req);
1408 struct fuse_file_info fi;
1409 char *path;
1410 int err;
1411
1412 get_dirhandle(llfi, &fi);
1413
1414 err = -ENOENT;
1415 pthread_rwlock_rdlock(&f->tree_lock);
1416 path = get_path(f, ino);
1417 if (path != NULL) {
1418 err = -ENOSYS;
1419 if (f->op.fsyncdir)
1420 err = f->op.fsyncdir(path, datasync, &fi);
1421 free(path);
1422 }
1423 pthread_rwlock_unlock(&f->tree_lock);
1424 reply_err(req, err);
Miklos Szeredia181e612001-11-06 12:03:23 +00001425}
1426
Miklos Szeredi77f39942004-03-25 11:17:52 +00001427static int default_statfs(struct statfs *buf)
1428{
1429 buf->f_namelen = 255;
1430 buf->f_bsize = 512;
1431 return 0;
1432}
1433
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001434static void convert_statfs_compat(struct fuse_statfs_compat1 *compatbuf,
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001435 struct statfs *statfs)
1436{
1437 statfs->f_bsize = compatbuf->block_size;
1438 statfs->f_blocks = compatbuf->blocks;
1439 statfs->f_bfree = compatbuf->blocks_free;
1440 statfs->f_bavail = compatbuf->blocks_free;
1441 statfs->f_files = compatbuf->files;
1442 statfs->f_ffree = compatbuf->files_free;
1443 statfs->f_namelen = compatbuf->namelen;
1444}
1445
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001446static void fuse_statfs(fuse_req_t req)
Miklos Szeredi18e75e42004-02-19 14:23:27 +00001447{
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001448 struct fuse *f = req_fuse_prepare(req);
Miklos Szeredi18e75e42004-02-19 14:23:27 +00001449 struct statfs buf;
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001450 int err;
Mark Glinesd84b39a2002-01-07 16:32:02 +00001451
Miklos Szeredi77f39942004-03-25 11:17:52 +00001452 memset(&buf, 0, sizeof(struct statfs));
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001453 if (f->op.statfs) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001454 if (!f->compat || f->compat > 11)
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001455 err = f->op.statfs("/", &buf);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001456 else {
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001457 struct fuse_statfs_compat1 compatbuf;
1458 memset(&compatbuf, 0, sizeof(struct fuse_statfs_compat1));
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001459 err = ((struct fuse_operations_compat1 *) &f->op)->statfs(&compatbuf);
1460 if (!err)
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001461 convert_statfs_compat(&compatbuf, &buf);
1462 }
1463 }
Miklos Szeredi77f39942004-03-25 11:17:52 +00001464 else
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001465 err = default_statfs(&buf);
Miklos Szeredi77f39942004-03-25 11:17:52 +00001466
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001467 if (!err)
1468 fuse_reply_statfs(req, &buf);
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001469 else
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001470 reply_err(req, err);
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001471}
1472
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001473static void fuse_setxattr(fuse_req_t req, fuse_ino_t ino, const char *name,
1474 const char *value, size_t size, int flags)
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001475{
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001476 struct fuse *f = req_fuse_prepare(req);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001477 char *path;
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001478 int err;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001479
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001480 err = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001481 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001482 path = get_path(f, ino);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001483 if (path != NULL) {
Miklos Szerediab974562005-04-07 15:40:21 +00001484 err = -ENOSYS;
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001485 if (f->op.setxattr)
1486 err = f->op.setxattr(path, name, value, size, flags);
1487 free(path);
1488 }
1489 pthread_rwlock_unlock(&f->tree_lock);
1490 reply_err(req, err);
1491}
1492
1493static int common_getxattr(struct fuse *f, fuse_ino_t ino, const char *name,
1494 char *value, size_t size)
1495{
1496 int err;
1497 char *path;
1498
1499 err = -ENOENT;
1500 pthread_rwlock_rdlock(&f->tree_lock);
1501 path = get_path(f, ino);
1502 if (path != NULL) {
1503 err = -ENOSYS;
1504 if (f->op.getxattr)
1505 err = f->op.getxattr(path, name, value, size);
Miklos Szerediab974562005-04-07 15:40:21 +00001506 free(path);
1507 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001508 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szerediab974562005-04-07 15:40:21 +00001509 return err;
1510}
1511
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001512static void fuse_getxattr(fuse_req_t req, fuse_ino_t ino, const char *name,
1513 size_t size)
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001514{
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001515 struct fuse *f = req_fuse_prepare(req);
Miklos Szeredi4283ee72005-03-21 12:09:04 +00001516 int res;
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001517
1518 if (size) {
1519 char *value = (char *) malloc(size);
1520 if (value == NULL) {
1521 reply_err(req, -ENOMEM);
1522 return;
1523 }
1524 res = common_getxattr(f, ino, name, value, size);
1525 if (res > 0)
1526 fuse_reply_buf(req, value, res);
1527 else
1528 reply_err(req, res);
1529 free(value);
1530 } else {
1531 res = common_getxattr(f, ino, name, NULL, 0);
1532 if (res >= 0)
1533 fuse_reply_xattr(req, res);
1534 else
1535 reply_err(req, res);
1536 }
1537}
1538
1539static int common_listxattr(struct fuse *f, fuse_ino_t ino, char *list,
1540 size_t size)
1541{
Miklos Szeredi4283ee72005-03-21 12:09:04 +00001542 char *path;
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001543 int err;
Miklos Szeredi4283ee72005-03-21 12:09:04 +00001544
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001545 err = -ENOENT;
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001546 pthread_rwlock_rdlock(&f->tree_lock);
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001547 path = get_path(f, ino);
Miklos Szeredi4283ee72005-03-21 12:09:04 +00001548 if (path != NULL) {
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001549 err = -ENOSYS;
1550 if (f->op.listxattr)
1551 err = f->op.listxattr(path, list, size);
Miklos Szeredi4283ee72005-03-21 12:09:04 +00001552 free(path);
1553 }
Miklos Szeredic2a33ee2005-05-09 13:29:17 +00001554 pthread_rwlock_unlock(&f->tree_lock);
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001555 return err;
Miklos Szeredi4283ee72005-03-21 12:09:04 +00001556}
Miklos Szeredi3ead28e2005-01-18 21:23:41 +00001557
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001558static void fuse_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size)
Miklos Szeredi43696432001-11-18 19:15:05 +00001559{
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001560 struct fuse *f = req_fuse_prepare(req);
1561 int res;
1562
1563 if (size) {
1564 char *list = (char *) malloc(size);
1565 if (list == NULL) {
1566 reply_err(req, -ENOMEM);
1567 return;
1568 }
1569 res = common_listxattr(f, ino, list, size);
1570 if (res > 0)
1571 fuse_reply_buf(req, list, res);
1572 else
1573 reply_err(req, res);
1574 free(list);
1575 } else {
1576 res = common_listxattr(f, ino, NULL, 0);
1577 if (res >= 0)
1578 fuse_reply_xattr(req, res);
1579 else
1580 reply_err(req, res);
1581 }
Miklos Szeredi43696432001-11-18 19:15:05 +00001582}
1583
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001584static void fuse_removexattr(fuse_req_t req, fuse_ino_t ino, const char *name)
1585{
1586 struct fuse *f = req_fuse_prepare(req);
1587 char *path;
1588 int err;
1589
1590 err = -ENOENT;
1591 pthread_rwlock_rdlock(&f->tree_lock);
1592 path = get_path(f, ino);
1593 if (path != NULL) {
1594 err = -ENOSYS;
1595 if (f->op.removexattr)
1596 err = f->op.removexattr(path, name);
1597 free(path);
1598 }
1599 pthread_rwlock_unlock(&f->tree_lock);
1600 reply_err(req, err);
1601}
1602
1603static struct fuse_ll_operations fuse_path_ops = {
1604 .init = fuse_data_init,
1605 .destroy = fuse_data_destroy,
1606 .lookup = fuse_lookup,
1607 .forget = fuse_forget,
1608 .getattr = fuse_getattr,
1609 .setattr = fuse_setattr,
Miklos Szeredi7b28eae2005-08-01 12:48:30 +00001610 .access = fuse_access,
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001611 .readlink = fuse_readlink,
1612 .mknod = fuse_mknod,
1613 .mkdir = fuse_mkdir,
1614 .unlink = fuse_unlink,
1615 .rmdir = fuse_rmdir,
1616 .symlink = fuse_symlink,
1617 .rename = fuse_rename,
1618 .link = fuse_link,
1619 .open = fuse_open,
1620 .read = fuse_read,
1621 .write = fuse_write,
1622 .flush = fuse_flush,
1623 .release = fuse_release,
1624 .fsync = fuse_fsync,
1625 .opendir = fuse_opendir,
1626 .readdir = fuse_readdir,
1627 .releasedir = fuse_releasedir,
1628 .fsyncdir = fuse_fsyncdir,
1629 .statfs = fuse_statfs,
1630 .setxattr = fuse_setxattr,
1631 .getxattr = fuse_getxattr,
1632 .listxattr = fuse_listxattr,
1633 .removexattr = fuse_removexattr,
1634};
1635
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001636void fuse_process_cmd(struct fuse *f, struct fuse_cmd *cmd)
Miklos Szeredia181e612001-11-06 12:03:23 +00001637{
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001638 fuse_ll_process_cmd(f->fll, cmd);
Miklos Szeredia181e612001-11-06 12:03:23 +00001639}
1640
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001641int fuse_exited(struct fuse *f)
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001642{
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001643 return fuse_ll_exited(f->fll);
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001644}
1645
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001646struct fuse_cmd *fuse_read_cmd(struct fuse *f)
Miklos Szeredi2df1c042001-11-06 15:07:17 +00001647{
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001648 return fuse_ll_read_cmd(f->fll);
Miklos Szeredi2df1c042001-11-06 15:07:17 +00001649}
1650
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001651int fuse_loop(struct fuse *f)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001652{
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001653 if (f)
1654 return fuse_ll_loop(f->fll);
1655 else
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001656 return -1;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001657}
1658
Miklos Szeredi891b8742004-07-29 09:27:49 +00001659int fuse_invalidate(struct fuse *f, const char *path)
1660{
Miklos Szeredie56818b2004-12-12 11:45:24 +00001661 (void) f;
1662 (void) path;
1663 return -EINVAL;
Miklos Szeredi891b8742004-07-29 09:27:49 +00001664}
1665
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001666void fuse_exit(struct fuse *f)
1667{
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001668 fuse_ll_exit(f->fll);
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001669}
1670
Miklos Szeredid169f312004-09-22 08:48:26 +00001671struct fuse_context *fuse_get_context()
Miklos Szeredi2e50d432001-12-20 12:17:25 +00001672{
Miklos Szeredid169f312004-09-22 08:48:26 +00001673 static struct fuse_context context;
1674 if (fuse_getcontext)
1675 return fuse_getcontext();
Miklos Szeredi2e50d432001-12-20 12:17:25 +00001676 else
Miklos Szeredid169f312004-09-22 08:48:26 +00001677 return &context;
1678}
1679
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001680void fuse_set_getcontext_func(struct fuse_context *(*func)(void))
Miklos Szeredid169f312004-09-22 08:48:26 +00001681{
1682 fuse_getcontext = func;
Miklos Szeredi2e50d432001-12-20 12:17:25 +00001683}
1684
Miklos Szeredie2aa2e22005-07-15 13:31:36 +00001685struct fuse_ll *fuse_get_lowlevel(struct fuse *f)
1686{
1687 return f->fll;
1688}
1689
Miklos Szeredie331c4b2005-07-06 13:34:02 +00001690static int begins_with(const char *s, const char *beg)
1691{
1692 if (strncmp(s, beg, strlen(beg)) == 0)
1693 return 1;
1694 else
1695 return 0;
1696}
1697
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001698int fuse_is_lib_option(const char *opt)
1699{
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001700 if (fuse_ll_is_lib_option(opt) ||
1701 strcmp(opt, "debug") == 0 ||
Miklos Szeredia13d9002004-11-02 17:32:03 +00001702 strcmp(opt, "hard_remove") == 0 ||
Miklos Szeredi0111f9d2005-04-22 12:04:55 +00001703 strcmp(opt, "use_ino") == 0 ||
Miklos Szeredi33be22d2005-05-27 09:12:43 +00001704 strcmp(opt, "allow_root") == 0 ||
Miklos Szeredie331c4b2005-07-06 13:34:02 +00001705 strcmp(opt, "readdir_ino") == 0 ||
Miklos Szeredie77cc072005-08-01 11:58:51 +00001706 strcmp(opt, "direct_io") == 0 ||
1707 strcmp(opt, "kernel_cache") == 0 ||
Miklos Szeredie331c4b2005-07-06 13:34:02 +00001708 begins_with(opt, "umask=") ||
1709 begins_with(opt, "uid=") ||
1710 begins_with(opt, "gid="))
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001711 return 1;
1712 else
1713 return 0;
1714}
1715
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001716static int parse_lib_opts(struct fuse *f, const char *opts, char **llopts)
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001717{
1718 if (opts) {
1719 char *xopts = strdup(opts);
1720 char *s = xopts;
1721 char *opt;
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001722 char *d = xopts;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001723
Miklos Szeredie56818b2004-12-12 11:45:24 +00001724 if (xopts == NULL) {
1725 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001726 return -1;
Miklos Szeredie56818b2004-12-12 11:45:24 +00001727 }
Miklos Szeredie5183742005-02-02 11:14:04 +00001728
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001729 while((opt = strsep(&s, ","))) {
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001730 if (fuse_ll_is_lib_option(opt)) {
1731 size_t optlen = strlen(opt);
1732 if (strcmp(opt, "debug") == 0)
Miklos Szeredi9b813af2005-07-21 07:59:37 +00001733 f->flags |= FUSE_DEBUG;
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001734 memmove(d, opt, optlen);
1735 d += optlen;
1736 *d++ = ',';
1737 } else if (strcmp(opt, "hard_remove") == 0)
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001738 f->flags |= FUSE_HARD_REMOVE;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001739 else if (strcmp(opt, "use_ino") == 0)
1740 f->flags |= FUSE_USE_INO;
Miklos Szeredi33be22d2005-05-27 09:12:43 +00001741 else if (strcmp(opt, "readdir_ino") == 0)
1742 f->flags |= FUSE_READDIR_INO;
Miklos Szeredie77cc072005-08-01 11:58:51 +00001743 else if (strcmp(opt, "direct_io") == 0)
1744 f->flags |= FUSE_DIRECT_IO;
1745 else if (strcmp(opt, "kernel_cache") == 0)
1746 f->flags |= FUSE_KERNEL_CACHE;
Miklos Szeredie331c4b2005-07-06 13:34:02 +00001747 else if (sscanf(opt, "umask=%o", &f->umask) == 1)
1748 f->flags |= FUSE_SET_MODE;
1749 else if (sscanf(opt, "uid=%u", &f->uid) == 1)
1750 f->flags |= FUSE_SET_UID;
1751 else if(sscanf(opt, "gid=%u", &f->gid) == 1)
1752 f->flags |= FUSE_SET_GID;
Miklos Szeredie5183742005-02-02 11:14:04 +00001753 else
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001754 fprintf(stderr, "fuse: warning: unknown option `%s'\n", opt);
1755 }
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001756 if (d != xopts) {
1757 d[-1] = '\0';
1758 *llopts = xopts;
1759 }
1760 else
1761 free(xopts);
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001762 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001763 return 0;
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001764}
1765
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001766struct fuse *fuse_new_common(int fd, const char *opts,
1767 const struct fuse_operations *op,
1768 size_t op_size, int compat)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001769{
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001770 struct fuse *f;
1771 struct node *root;
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001772 char *llopts = NULL;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001773
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001774 if (sizeof(struct fuse_operations) < op_size) {
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001775 fprintf(stderr, "fuse: warning: library too old, some operations may not not work\n");
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001776 op_size = sizeof(struct fuse_operations);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001777 }
1778
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001779 f = (struct fuse *) calloc(1, sizeof(struct fuse));
Miklos Szeredie56818b2004-12-12 11:45:24 +00001780 if (f == NULL) {
1781 fprintf(stderr, "fuse: failed to allocate fuse object\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001782 goto out;
Miklos Szeredie56818b2004-12-12 11:45:24 +00001783 }
Miklos Szeredi2df1c042001-11-06 15:07:17 +00001784
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001785 if (parse_lib_opts(f, opts, &llopts) == -1)
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001786 goto out_free;
1787
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001788 f->fll = fuse_ll_new(fd, llopts, &fuse_path_ops, sizeof(fuse_path_ops), f);
1789 free(llopts);
1790 if (f->fll == NULL)
1791 goto out_free;
1792
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001793 f->ctr = 0;
Miklos Szeredi76f65782004-02-19 16:55:40 +00001794 f->generation = 0;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001795 /* FIXME: Dynamic hash table */
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001796 f->name_table_size = 14057;
1797 f->name_table = (struct node **)
1798 calloc(1, sizeof(struct node *) * f->name_table_size);
Miklos Szeredie56818b2004-12-12 11:45:24 +00001799 if (f->name_table == NULL) {
1800 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001801 goto out_free;
Miklos Szeredie56818b2004-12-12 11:45:24 +00001802 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001803
Miklos Szeredia13d9002004-11-02 17:32:03 +00001804 f->id_table_size = 14057;
1805 f->id_table = (struct node **)
1806 calloc(1, sizeof(struct node *) * f->id_table_size);
Miklos Szeredie56818b2004-12-12 11:45:24 +00001807 if (f->id_table == NULL) {
1808 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001809 goto out_free_name_table;
Miklos Szeredie56818b2004-12-12 11:45:24 +00001810 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001811
Miklos Szerediab974562005-04-07 15:40:21 +00001812 mutex_init(&f->lock);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001813 memcpy(&f->op, op, op_size);
1814 f->compat = compat;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001815
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001816 root = (struct node *) calloc(1, sizeof(struct node));
Miklos Szeredie56818b2004-12-12 11:45:24 +00001817 if (root == NULL) {
1818 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredia13d9002004-11-02 17:32:03 +00001819 goto out_free_id_table;
Miklos Szeredie56818b2004-12-12 11:45:24 +00001820 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001821
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001822 root->name = strdup("/");
Miklos Szeredie56818b2004-12-12 11:45:24 +00001823 if (root->name == NULL) {
1824 fprintf(stderr, "fuse: memory allocation failed\n");
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001825 goto out_free_root;
Miklos Szeredie56818b2004-12-12 11:45:24 +00001826 }
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001827
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001828 root->parent = 0;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001829 root->nodeid = FUSE_ROOT_ID;
Miklos Szeredi76f65782004-02-19 16:55:40 +00001830 root->generation = 0;
Miklos Szeredi0f62d722005-01-04 12:45:54 +00001831 root->refctr = 1;
Miklos Szeredi38009022005-05-08 19:47:22 +00001832 root->nlookup = 1;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001833 hash_id(f, root);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001834
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001835 return f;
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001836
1837 out_free_root:
1838 free(root);
Miklos Szeredia13d9002004-11-02 17:32:03 +00001839 out_free_id_table:
1840 free(f->id_table);
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001841 out_free_name_table:
1842 free(f->name_table);
1843 out_free:
1844 free(f);
1845 out:
Miklos Szeredib2cf9562004-09-16 08:42:40 +00001846 return NULL;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001847}
1848
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001849struct fuse *fuse_new(int fd, const char *opts,
1850 const struct fuse_operations *op, size_t op_size)
1851{
1852 return fuse_new_common(fd, opts, op, op_size, 0);
1853}
1854
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001855struct fuse *fuse_new_compat2(int fd, const char *opts,
1856 const struct fuse_operations_compat2 *op)
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001857{
1858 return fuse_new_common(fd, opts, (struct fuse_operations *) op,
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001859 sizeof(struct fuse_operations_compat2), 21);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001860}
1861
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001862struct fuse *fuse_new_compat1(int fd, int flags,
1863 const struct fuse_operations_compat1 *op)
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001864{
1865 char *opts = NULL;
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001866 if (flags & FUSE_DEBUG_COMPAT1)
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001867 opts = "debug";
1868 return fuse_new_common(fd, opts, (struct fuse_operations *) op,
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001869 sizeof(struct fuse_operations_compat1), 11);
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001870}
1871
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001872void fuse_destroy(struct fuse *f)
1873{
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001874 size_t i;
Miklos Szeredia13d9002004-11-02 17:32:03 +00001875 for (i = 0; i < f->id_table_size; i++) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001876 struct node *node;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001877
Miklos Szeredia13d9002004-11-02 17:32:03 +00001878 for (node = f->id_table[i]; node != NULL; node = node->id_next) {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001879 if (node->is_hidden) {
Miklos Szeredia13d9002004-11-02 17:32:03 +00001880 char *path = get_path(f, node->nodeid);
Miklos Szeredi21019c92005-05-09 11:22:41 +00001881 if (path) {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001882 f->op.unlink(path);
Miklos Szeredi21019c92005-05-09 11:22:41 +00001883 free(path);
1884 }
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001885 }
1886 }
1887 }
Miklos Szeredia13d9002004-11-02 17:32:03 +00001888 for (i = 0; i < f->id_table_size; i++) {
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001889 struct node *node;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001890 struct node *next;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001891
Miklos Szeredia13d9002004-11-02 17:32:03 +00001892 for (node = f->id_table[i]; node != NULL; node = next) {
1893 next = node->id_next;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001894 free_node(node);
1895 }
1896 }
Miklos Szeredia13d9002004-11-02 17:32:03 +00001897 free(f->id_table);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001898 free(f->name_table);
Miklos Szeredia181e612001-11-06 12:03:23 +00001899 pthread_mutex_destroy(&f->lock);
Miklos Szeredibd10a7b2005-07-15 09:59:59 +00001900 fuse_ll_destroy(f->fll);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001901 free(f);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001902}
Miklos Szeredi0b6a0ad2004-12-04 00:40:50 +00001903
Miklos Szeredif458b8c2004-12-07 16:46:42 +00001904__asm__(".symver fuse_exited,__fuse_exited@");
1905__asm__(".symver fuse_process_cmd,__fuse_process_cmd@");
1906__asm__(".symver fuse_read_cmd,__fuse_read_cmd@");
1907__asm__(".symver fuse_set_getcontext_func,__fuse_set_getcontext_func@");
1908__asm__(".symver fuse_new_compat2,fuse_new@");