blob: 659643b7210e7cddcb5ba81a1d4215dc68018053 [file] [log] [blame]
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001/*
2 FUSE: Filesystem in Userspace
Miklos Szeredi2e6b6f22004-07-07 19:19:53 +00003 Copyright (C) 2001-2004 Miklos Szeredi <miklos@szeredi.hu>
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00004
Miklos Szeredi8b39a9f2002-10-25 12:41:16 +00005 This program can be distributed under the terms of the GNU LGPL.
6 See the file COPYING.LIB
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00007*/
8
Miklos Szeredicb264512004-06-23 18:52:50 +00009#include <config.h>
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000010#include "fuse_i.h"
11#include <linux/fuse.h>
12
13#include <string.h>
Miklos Szeredi97c61e92001-11-07 12:09:43 +000014#include <stdlib.h>
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000015#include <unistd.h>
Miklos Szeredi97c61e92001-11-07 12:09:43 +000016#include <limits.h>
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000017#include <errno.h>
Miklos Szeredi019b4e92001-12-26 18:08:09 +000018#include <sys/param.h>
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000019
Miklos Szeredi97c61e92001-11-07 12:09:43 +000020#define FUSE_MAX_PATH 4096
Miklos Szeredi6bf8b682002-10-28 08:49:39 +000021#define PARAM(inarg) (((char *)(inarg)) + sizeof(*inarg))
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000022
Miklos Szeredi254d5ed2004-03-02 11:11:24 +000023#define ENTRY_REVALIDATE_TIME 1 /* sec */
24#define ATTR_REVALIDATE_TIME 1 /* sec */
25
Miklos Szeredic8ba2372002-12-10 12:26:00 +000026static const char *opname(enum fuse_opcode opcode)
27{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +000028 switch (opcode) {
Miklos Szeredi3ed84232004-03-30 15:17:26 +000029 case FUSE_LOOKUP: return "LOOKUP";
30 case FUSE_FORGET: return "FORGET";
31 case FUSE_GETATTR: return "GETATTR";
32 case FUSE_SETATTR: return "SETATTR";
33 case FUSE_READLINK: return "READLINK";
34 case FUSE_SYMLINK: return "SYMLINK";
35 case FUSE_GETDIR: return "GETDIR";
36 case FUSE_MKNOD: return "MKNOD";
37 case FUSE_MKDIR: return "MKDIR";
38 case FUSE_UNLINK: return "UNLINK";
39 case FUSE_RMDIR: return "RMDIR";
40 case FUSE_RENAME: return "RENAME";
41 case FUSE_LINK: return "LINK";
42 case FUSE_OPEN: return "OPEN";
43 case FUSE_READ: return "READ";
44 case FUSE_WRITE: return "WRITE";
45 case FUSE_STATFS: return "STATFS";
Miklos Szeredi99f20742004-05-19 08:01:10 +000046 case FUSE_FLUSH: return "FLUSH";
Miklos Szeredi3ed84232004-03-30 15:17:26 +000047 case FUSE_RELEASE: return "RELEASE";
48 case FUSE_FSYNC: return "FSYNC";
49 case FUSE_SETXATTR: return "SETXATTR";
50 case FUSE_GETXATTR: return "GETXATTR";
51 case FUSE_LISTXATTR: return "LISTXATTR";
52 case FUSE_REMOVEXATTR: return "REMOVEXATTR";
Miklos Szeredi99f20742004-05-19 08:01:10 +000053 default: return "???";
Miklos Szeredic8ba2372002-12-10 12:26:00 +000054 }
55}
56
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +000057
58static inline void dec_avail(struct fuse *f)
59{
60 pthread_mutex_lock(&f->lock);
61 f->numavail --;
62 pthread_mutex_unlock(&f->lock);
63}
64
Miklos Szeredi97c61e92001-11-07 12:09:43 +000065static struct node *__get_node(struct fuse *f, fino_t ino)
Miklos Szeredia181e612001-11-06 12:03:23 +000066{
Miklos Szeredi97c61e92001-11-07 12:09:43 +000067 size_t hash = ino % f->ino_table_size;
68 struct node *node;
69
Miklos Szeredi7eafcce2004-06-19 22:42:38 +000070 for (node = f->ino_table[hash]; node != NULL; node = node->ino_next)
71 if (node->ino == ino)
Miklos Szeredi97c61e92001-11-07 12:09:43 +000072 return node;
73
74 return NULL;
Miklos Szeredia181e612001-11-06 12:03:23 +000075}
76
Miklos Szeredi97c61e92001-11-07 12:09:43 +000077static struct node *get_node(struct fuse *f, fino_t ino)
Miklos Szeredia181e612001-11-06 12:03:23 +000078{
Miklos Szeredi97c61e92001-11-07 12:09:43 +000079 struct node *node = __get_node(f, ino);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +000080 if (node != NULL)
Miklos Szeredi97c61e92001-11-07 12:09:43 +000081 return node;
82
83 fprintf(stderr, "fuse internal error: inode %lu not found\n", ino);
84 abort();
Miklos Szeredia181e612001-11-06 12:03:23 +000085}
86
Miklos Szeredi76f65782004-02-19 16:55:40 +000087static void hash_ino(struct fuse *f, struct node *node)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000088{
Miklos Szeredi76f65782004-02-19 16:55:40 +000089 size_t hash = node->ino % f->ino_table_size;
Miklos Szeredi97c61e92001-11-07 12:09:43 +000090 node->ino_next = f->ino_table[hash];
91 f->ino_table[hash] = node;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000092}
93
Miklos Szeredi97c61e92001-11-07 12:09:43 +000094static void unhash_ino(struct fuse *f, struct node *node)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000095{
Miklos Szeredi97c61e92001-11-07 12:09:43 +000096 size_t hash = node->ino % f->ino_table_size;
97 struct node **nodep = &f->ino_table[hash];
98
Miklos Szeredi7eafcce2004-06-19 22:42:38 +000099 for (; *nodep != NULL; nodep = &(*nodep)->ino_next)
100 if (*nodep == node) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000101 *nodep = node->ino_next;
102 return;
103 }
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000104}
105
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000106static fino_t next_ino(struct fuse *f)
107{
Miklos Szeredi76f65782004-02-19 16:55:40 +0000108 do {
109 f->ctr++;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000110 if (!f->ctr)
Miklos Szeredi76f65782004-02-19 16:55:40 +0000111 f->generation ++;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000112 } while (f->ctr == 0 || __get_node(f, f->ctr) != NULL);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000113 return f->ctr;
114}
115
116static void free_node(struct node *node)
117{
118 free(node->name);
119 free(node);
120}
121
122static unsigned int name_hash(struct fuse *f, fino_t parent, const char *name)
123{
124 unsigned int hash = *name;
125
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000126 if (hash)
127 for (name += 1; *name != '\0'; name++)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000128 hash = (hash << 5) - hash + *name;
129
130 return (hash + parent) % f->name_table_size;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000131}
132
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000133static struct node *__lookup_node(struct fuse *f, fino_t parent,
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000134 const char *name)
135{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000136 size_t hash = name_hash(f, parent, name);
137 struct node *node;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000138
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000139 for (node = f->name_table[hash]; node != NULL; node = node->name_next)
140 if (node->parent == parent && strcmp(node->name, name) == 0)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000141 return node;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000142
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000143 return NULL;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000144}
145
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000146static struct node *lookup_node(struct fuse *f, fino_t parent,
147 const char *name)
148{
149 struct node *node;
150
151 pthread_mutex_lock(&f->lock);
152 node = __lookup_node(f, parent, name);
153 pthread_mutex_unlock(&f->lock);
154 if (node != NULL)
155 return node;
156
157 fprintf(stderr, "fuse internal error: node %lu/%s not found\n", parent,
158 name);
159 abort();
160}
161
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000162static void hash_name(struct fuse *f, struct node *node, fino_t parent,
Miklos Szeredia181e612001-11-06 12:03:23 +0000163 const char *name)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000164{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000165 size_t hash = name_hash(f, parent, name);
Miklos Szeredia181e612001-11-06 12:03:23 +0000166 node->parent = parent;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000167 node->name = strdup(name);
168 node->name_next = f->name_table[hash];
169 f->name_table[hash] = node;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000170}
171
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000172static void unhash_name(struct fuse *f, struct node *node)
Miklos Szeredia181e612001-11-06 12:03:23 +0000173{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000174 if (node->name != NULL) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000175 size_t hash = name_hash(f, node->parent, node->name);
176 struct node **nodep = &f->name_table[hash];
177
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000178 for (; *nodep != NULL; nodep = &(*nodep)->name_next)
179 if (*nodep == node) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000180 *nodep = node->name_next;
181 node->name_next = NULL;
182 free(node->name);
183 node->name = NULL;
184 node->parent = 0;
185 return;
186 }
187 fprintf(stderr, "fuse internal error: unable to unhash node: %lu\n",
188 node->ino);
189 abort();
Miklos Szeredia181e612001-11-06 12:03:23 +0000190 }
191}
192
Miklos Szeredi76f65782004-02-19 16:55:40 +0000193static struct node *find_node(struct fuse *f, fino_t parent, char *name,
194 struct fuse_attr *attr, int version)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000195{
196 struct node *node;
Miklos Szeredia181e612001-11-06 12:03:23 +0000197 int mode = attr->mode & S_IFMT;
198 int rdev = 0;
199
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000200 if (S_ISCHR(mode) || S_ISBLK(mode))
Miklos Szeredia181e612001-11-06 12:03:23 +0000201 rdev = attr->rdev;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000202
Miklos Szeredia181e612001-11-06 12:03:23 +0000203 pthread_mutex_lock(&f->lock);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000204 node = __lookup_node(f, parent, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000205 if (node != NULL) {
206 if (node->mode == mode && node->rdev == rdev)
Miklos Szeredia181e612001-11-06 12:03:23 +0000207 goto out;
208
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000209 unhash_name(f, node);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000210 }
211
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000212 node = (struct node *) calloc(1, sizeof(struct node));
Miklos Szeredia181e612001-11-06 12:03:23 +0000213 node->mode = mode;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000214 node->rdev = rdev;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000215 node->open_count = 0;
216 node->is_hidden = 0;
Miklos Szeredi76f65782004-02-19 16:55:40 +0000217 node->ino = next_ino(f);
218 node->generation = f->generation;
219 hash_ino(f, node);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000220 hash_name(f, node, parent, name);
Miklos Szeredia181e612001-11-06 12:03:23 +0000221
222 out:
223 node->version = version;
224 pthread_mutex_unlock(&f->lock);
Miklos Szeredi76f65782004-02-19 16:55:40 +0000225 return node;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000226}
227
Miklos Szeredi891b8742004-07-29 09:27:49 +0000228static int path_lookup(struct fuse *f, const char *path, fino_t *inop)
229{
230 fino_t ino;
231 int err;
232 char *s;
233 char *name;
234 char *tmp = strdup(path);
235 if (!tmp)
236 return -ENOMEM;
237
238 pthread_mutex_lock(&f->lock);
239 ino = FUSE_ROOT_INO;
240 err = 0;
241 for (s = tmp; (name = strsep(&s, "/")) != NULL; ) {
242 if (name[0]) {
243 struct node *node = __lookup_node(f, ino, name);
244 if (node == NULL) {
245 err = -ENOENT;
246 break;
247 }
248 ino = node->ino;
249 }
250 }
251 pthread_mutex_unlock(&f->lock);
252 free(tmp);
253 if (!err)
254 *inop = ino;
255
256 return err;
257}
258
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000259static char *add_name(char *buf, char *s, const char *name)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000260{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000261 size_t len = strlen(name);
262 s -= len;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000263 if (s <= buf) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000264 fprintf(stderr, "fuse: path too long: ...%s\n", s + len);
265 return NULL;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000266 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000267 strncpy(s, name, len);
268 s--;
269 *s = '/';
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000270
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000271 return s;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000272}
273
Miklos Szeredia181e612001-11-06 12:03:23 +0000274static char *get_path_name(struct fuse *f, fino_t ino, const char *name)
Miklos Szeredib483c932001-10-29 14:57:57 +0000275{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000276 char buf[FUSE_MAX_PATH];
277 char *s = buf + FUSE_MAX_PATH - 1;
278 struct node *node;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000279
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000280 *s = '\0';
Miklos Szeredia181e612001-11-06 12:03:23 +0000281
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000282 if (name != NULL) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000283 s = add_name(buf, s, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000284 if (s == NULL)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000285 return NULL;
286 }
287
288 pthread_mutex_lock(&f->lock);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000289 for (node = get_node(f, ino); node->ino != FUSE_ROOT_INO;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000290 node = get_node(f, node->parent)) {
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000291 if (node->name == NULL) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000292 s = NULL;
293 break;
294 }
295
296 s = add_name(buf, s, node->name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000297 if (s == NULL)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000298 break;
299 }
300 pthread_mutex_unlock(&f->lock);
301
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000302 if (s == NULL)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000303 return NULL;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000304 else if (*s == '\0')
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000305 return strdup("/");
306 else
307 return strdup(s);
308}
Miklos Szeredia181e612001-11-06 12:03:23 +0000309
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000310static char *get_path(struct fuse *f, fino_t ino)
311{
312 return get_path_name(f, ino, NULL);
Miklos Szeredib483c932001-10-29 14:57:57 +0000313}
314
Miklos Szeredia181e612001-11-06 12:03:23 +0000315static void destroy_node(struct fuse *f, fino_t ino, int version)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000316{
Miklos Szeredia181e612001-11-06 12:03:23 +0000317 struct node *node;
318
319 pthread_mutex_lock(&f->lock);
Miklos Szeredi8b2d3332004-09-09 08:44:01 +0000320 node = __get_node(f, ino);
321 if (node && node->version == version && ino != FUSE_ROOT_INO) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000322 unhash_name(f, node);
323 unhash_ino(f, node);
Miklos Szeredia181e612001-11-06 12:03:23 +0000324 free_node(node);
325 }
326 pthread_mutex_unlock(&f->lock);
327
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000328}
329
Miklos Szeredi5e183482001-10-31 14:52:35 +0000330static void remove_node(struct fuse *f, fino_t dir, const char *name)
331{
Miklos Szeredia181e612001-11-06 12:03:23 +0000332 struct node *node;
333
334 pthread_mutex_lock(&f->lock);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000335 node = __lookup_node(f, dir, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000336 if (node == NULL) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000337 fprintf(stderr, "fuse internal error: unable to remove node %lu/%s\n",
338 dir, name);
339 abort();
340 }
341 unhash_name(f, node);
Miklos Szeredia181e612001-11-06 12:03:23 +0000342 pthread_mutex_unlock(&f->lock);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000343}
344
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000345static int rename_node(struct fuse *f, fino_t olddir, const char *oldname,
346 fino_t newdir, const char *newname, int hide)
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000347{
Miklos Szeredia181e612001-11-06 12:03:23 +0000348 struct node *node;
349 struct node *newnode;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000350 int err = 0;
Miklos Szeredia181e612001-11-06 12:03:23 +0000351
352 pthread_mutex_lock(&f->lock);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000353 node = __lookup_node(f, olddir, oldname);
354 newnode = __lookup_node(f, newdir, newname);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000355 if (node == NULL) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000356 fprintf(stderr, "fuse internal error: unable to rename node %lu/%s\n",
357 olddir, oldname);
358 abort();
359 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000360
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000361 if (newnode != NULL) {
362 if (hide) {
363 fprintf(stderr, "fuse: hidden file got created during hiding\n");
364 err = -1;
365 goto out;
366 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000367 unhash_name(f, newnode);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000368 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000369
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000370 unhash_name(f, node);
371 hash_name(f, node, newdir, newname);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000372 if (hide)
373 node->is_hidden = 1;
374
375 out:
Miklos Szeredia181e612001-11-06 12:03:23 +0000376 pthread_mutex_unlock(&f->lock);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000377 return err;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000378}
379
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000380static void convert_stat(struct stat *stbuf, struct fuse_attr *attr)
381{
Miklos Szeredib5958612004-02-20 14:10:49 +0000382 attr->mode = stbuf->st_mode;
383 attr->nlink = stbuf->st_nlink;
384 attr->uid = stbuf->st_uid;
385 attr->gid = stbuf->st_gid;
386 attr->rdev = stbuf->st_rdev;
387 attr->size = stbuf->st_size;
388 attr->blocks = stbuf->st_blocks;
389 attr->atime = stbuf->st_atime;
Miklos Szeredib5958612004-02-20 14:10:49 +0000390 attr->mtime = stbuf->st_mtime;
Miklos Szeredib5958612004-02-20 14:10:49 +0000391 attr->ctime = stbuf->st_ctime;
Miklos Szeredicb264512004-06-23 18:52:50 +0000392#ifdef HAVE_STRUCT_STAT_ST_ATIM
393 attr->atimensec = stbuf->st_atim.tv_nsec;
394 attr->mtimensec = stbuf->st_mtim.tv_nsec;
Miklos Szeredib5958612004-02-20 14:10:49 +0000395 attr->ctimensec = stbuf->st_ctim.tv_nsec;
Miklos Szeredicb264512004-06-23 18:52:50 +0000396#endif
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000397}
398
Miklos Szeredia181e612001-11-06 12:03:23 +0000399static int fill_dir(struct fuse_dirhandle *dh, char *name, int type)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000400{
401 struct fuse_dirent dirent;
402 size_t reclen;
403 size_t res;
404
Miklos Szeredi43696432001-11-18 19:15:05 +0000405 dirent.ino = (unsigned long) -1;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000406 dirent.namelen = strlen(name);
407 strncpy(dirent.name, name, sizeof(dirent.name));
408 dirent.type = type;
409 reclen = FUSE_DIRENT_SIZE(&dirent);
410 res = fwrite(&dirent, reclen, 1, dh->fp);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000411 if (res == 0) {
Miklos Szeredi96249982001-11-21 12:21:19 +0000412 perror("fuse: writing directory file");
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000413 return -EIO;
414 }
415 return 0;
416}
417
Miklos Szeredi73798f92004-07-12 15:55:11 +0000418static int send_reply_raw(struct fuse *f, char *outbuf, size_t outsize,
419 int locked)
Miklos Szeredi43696432001-11-18 19:15:05 +0000420{
421 int res;
422
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000423 if ((f->flags & FUSE_DEBUG)) {
Miklos Szeredi43696432001-11-18 19:15:05 +0000424 struct fuse_out_header *out = (struct fuse_out_header *) outbuf;
425 printf(" unique: %i, error: %i (%s), outsize: %i\n", out->unique,
426 out->error, strerror(-out->error), outsize);
427 fflush(stdout);
428 }
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000429
Miklos Szeredi4e71c9f2004-02-09 12:05:14 +0000430 /* This needs to be done before the reply, otherwise the scheduler
431 could play tricks with us, and only let the counter be increased
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000432 long after the operation is done */
Miklos Szeredi73798f92004-07-12 15:55:11 +0000433 if (!locked)
434 pthread_mutex_lock(&f->lock);
435 f->numavail ++;
436 if (!locked)
437 pthread_mutex_unlock(&f->lock);
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000438
Miklos Szeredi43696432001-11-18 19:15:05 +0000439 res = write(f->fd, outbuf, outsize);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000440 if (res == -1) {
Miklos Szeredi43696432001-11-18 19:15:05 +0000441 /* ENOENT means the operation was interrupted */
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000442 if (!f->exited && errno != ENOENT)
Miklos Szeredi96249982001-11-21 12:21:19 +0000443 perror("fuse: writing device");
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000444 return -errno;
Miklos Szeredi43696432001-11-18 19:15:05 +0000445 }
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000446 return 0;
Miklos Szeredi43696432001-11-18 19:15:05 +0000447}
448
Miklos Szeredi73798f92004-07-12 15:55:11 +0000449static int __send_reply(struct fuse *f, struct fuse_in_header *in, int error,
450 void *arg, size_t argsize, int locked)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000451{
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000452 int res;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000453 char *outbuf;
454 size_t outsize;
455 struct fuse_out_header *out;
456
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000457 if (error <= -1000 || error > 0) {
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000458 fprintf(stderr, "fuse: bad error value: %i\n", error);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000459 error = -ERANGE;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000460 }
461
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000462 if (error)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000463 argsize = 0;
464
465 outsize = sizeof(struct fuse_out_header) + argsize;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000466 outbuf = (char *) malloc(outsize);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000467 out = (struct fuse_out_header *) outbuf;
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000468 memset(out, 0, sizeof(struct fuse_out_header));
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000469 out->unique = in->unique;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000470 out->error = error;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000471 if (argsize != 0)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000472 memcpy(outbuf + sizeof(struct fuse_out_header), arg, argsize);
473
Miklos Szeredi73798f92004-07-12 15:55:11 +0000474 res = send_reply_raw(f, outbuf, outsize, locked);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000475 free(outbuf);
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000476
477 return res;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000478}
479
Miklos Szeredi73798f92004-07-12 15:55:11 +0000480static int send_reply(struct fuse *f, struct fuse_in_header *in, int error,
481 void *arg, size_t argsize)
482{
483 return __send_reply(f, in, error, arg, argsize, 0);
484}
485
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000486static int is_open(struct fuse *f, fino_t dir, const char *name)
487{
488 struct node *node;
489 int isopen = 0;
490 pthread_mutex_lock(&f->lock);
491 node = __lookup_node(f, dir, name);
492 if (node && node->open_count > 0)
493 isopen = 1;
494 pthread_mutex_unlock(&f->lock);
495 return isopen;
496}
497
498static char *hidden_name(struct fuse *f, fino_t dir, const char *oldname,
499 char *newname, size_t bufsize)
500{
501 struct stat buf;
502 struct node *node;
503 struct node *newnode;
504 char *newpath;
505 int res;
506 int failctr = 10;
507
508 if (!f->op.getattr)
509 return NULL;
510
511 do {
512 node = lookup_node(f, dir, oldname);
513 pthread_mutex_lock(&f->lock);
514 do {
515 f->hidectr ++;
516 snprintf(newname, bufsize, ".fuse_hidden%08x%08x",
517 (unsigned int) node->ino, f->hidectr);
518 newnode = __lookup_node(f, dir, newname);
519 } while(newnode);
520 pthread_mutex_unlock(&f->lock);
521
522 newpath = get_path_name(f, dir, newname);
523 if (!newpath)
524 break;
525
526 res = f->op.getattr(newpath, &buf);
527 if (res != 0)
528 break;
529 free(newpath);
530 newpath = NULL;
531 } while(--failctr);
532
533 return newpath;
534}
535
536static int hide_node(struct fuse *f, const char *oldpath, fino_t dir,
537 const char *oldname)
538{
539 char newname[64];
540 char *newpath;
541 int err = -1;
542
543 if (!f->op.rename || !f->op.unlink)
544 return -EBUSY;
545
546 newpath = hidden_name(f, dir, oldname, newname, sizeof(newname));
547 if (newpath) {
548 err = f->op.rename(oldpath, newpath);
549 if (!err)
550 err = rename_node(f, dir, oldname, dir, newname, 1);
551 free(newpath);
552 }
553 if (err)
554 return -EBUSY;
555
556 return 0;
557}
558
Miklos Szeredi76f65782004-02-19 16:55:40 +0000559static int lookup_path(struct fuse *f, fino_t ino, int version, char *name,
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000560 const char *path, struct fuse_entry_out *arg)
Miklos Szeredi76f65782004-02-19 16:55:40 +0000561{
562 int res;
563 struct stat buf;
564
565 res = f->op.getattr(path, &buf);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000566 if (res == 0) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000567 struct node *node;
568
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000569 memset(arg, 0, sizeof(struct fuse_entry_out));
Miklos Szeredi76f65782004-02-19 16:55:40 +0000570 convert_stat(&buf, &arg->attr);
571 node = find_node(f, ino, name, &arg->attr, version);
572 arg->ino = node->ino;
573 arg->generation = node->generation;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000574 arg->entry_valid = ENTRY_REVALIDATE_TIME;
575 arg->entry_valid_nsec = 0;
576 arg->attr_valid = ATTR_REVALIDATE_TIME;
577 arg->attr_valid_nsec = 0;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000578 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000579 printf(" INO: %li\n", arg->ino);
580 fflush(stdout);
581 }
582 }
583 return res;
584}
585
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000586static void do_lookup(struct fuse *f, struct fuse_in_header *in, char *name)
587{
588 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000589 int res2;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000590 char *path;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000591 struct fuse_entry_out arg;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000592
Miklos Szeredi5e183482001-10-31 14:52:35 +0000593 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000594 path = get_path_name(f, in->ino, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000595 if (path != NULL) {
596 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi6ebe2342002-01-06 21:44:16 +0000597 printf("LOOKUP %s\n", path);
598 fflush(stdout);
599 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000600 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000601 if (f->op.getattr)
Miklos Szeredi76f65782004-02-19 16:55:40 +0000602 res = lookup_path(f, in->ino, in->unique, name, path, &arg);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000603 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000604 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000605 res2 = send_reply(f, in, res, &arg, sizeof(arg));
606 if (res == 0 && res2 == -ENOENT)
607 destroy_node(f, arg.ino, in->unique);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000608}
609
Miklos Szeredia181e612001-11-06 12:03:23 +0000610static void do_forget(struct fuse *f, struct fuse_in_header *in,
611 struct fuse_forget_in *arg)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000612{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000613 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi43696432001-11-18 19:15:05 +0000614 printf("FORGET %li/%i\n", in->ino, arg->version);
615 fflush(stdout);
616 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000617 destroy_node(f, in->ino, arg->version);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000618}
619
620static void do_getattr(struct fuse *f, struct fuse_in_header *in)
621{
622 int res;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000623 char *path;
624 struct stat buf;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000625 struct fuse_attr_out arg;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000626
Miklos Szeredi5e183482001-10-31 14:52:35 +0000627 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000628 path = get_path(f, in->ino);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000629 if (path != NULL) {
Miklos Szeredi5e183482001-10-31 14:52:35 +0000630 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000631 if (f->op.getattr)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000632 res = f->op.getattr(path, &buf);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000633 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000634 }
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000635
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000636 if (res == 0) {
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000637 memset(&arg, 0, sizeof(struct fuse_attr_out));
638 arg.attr_valid = ATTR_REVALIDATE_TIME;
639 arg.attr_valid_nsec = 0;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000640 convert_stat(&buf, &arg.attr);
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000641 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000642
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000643 send_reply(f, in, res, &arg, sizeof(arg));
644}
645
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000646static int do_chmod(struct fuse *f, const char *path, struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000647{
648 int res;
649
650 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000651 if (f->op.chmod)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000652 res = f->op.chmod(path, attr->mode);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000653
654 return res;
655}
656
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000657static int do_chown(struct fuse *f, const char *path, struct fuse_attr *attr,
Miklos Szeredi2e50d432001-12-20 12:17:25 +0000658 int valid)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000659{
660 int res;
661 uid_t uid = (valid & FATTR_UID) ? attr->uid : (uid_t) -1;
662 gid_t gid = (valid & FATTR_GID) ? attr->gid : (gid_t) -1;
663
664 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000665 if (f->op.chown)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000666 res = f->op.chown(path, uid, gid);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000667
668 return res;
669}
670
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000671static int do_truncate(struct fuse *f, const char *path,
672 struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000673{
674 int res;
675
676 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000677 if (f->op.truncate)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000678 res = f->op.truncate(path, attr->size);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000679
680 return res;
681}
682
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000683static int do_utime(struct fuse *f, const char *path, struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000684{
685 int res;
686 struct utimbuf buf;
687 buf.actime = attr->atime;
688 buf.modtime = attr->mtime;
689 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000690 if (f->op.utime)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000691 res = f->op.utime(path, &buf);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000692
693 return res;
694}
695
Miklos Szeredi5e183482001-10-31 14:52:35 +0000696static void do_setattr(struct fuse *f, struct fuse_in_header *in,
697 struct fuse_setattr_in *arg)
698{
699 int res;
700 char *path;
701 int valid = arg->valid;
702 struct fuse_attr *attr = &arg->attr;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000703 struct fuse_attr_out outarg;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000704
705 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000706 path = get_path(f, in->ino);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000707 if (path != NULL) {
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000708 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000709 if (f->op.getattr) {
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000710 res = 0;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000711 if (!res && (valid & FATTR_MODE))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000712 res = do_chmod(f, path, attr);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000713 if (!res && (valid & (FATTR_UID | FATTR_GID)))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000714 res = do_chown(f, path, attr, valid);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000715 if (!res && (valid & FATTR_SIZE))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000716 res = do_truncate(f, path, attr);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000717 if (!res && (valid & (FATTR_ATIME | FATTR_MTIME)) ==
Miklos Szeredib5958612004-02-20 14:10:49 +0000718 (FATTR_ATIME | FATTR_MTIME))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000719 res = do_utime(f, path, attr);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000720 if (!res) {
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000721 struct stat buf;
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000722 res = f->op.getattr(path, &buf);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000723 if (!res) {
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000724 memset(&outarg, 0, sizeof(struct fuse_attr_out));
725 outarg.attr_valid = ATTR_REVALIDATE_TIME;
726 outarg.attr_valid_nsec = 0;
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000727 convert_stat(&buf, &outarg.attr);
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000728 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000729 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000730 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000731 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000732 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000733 send_reply(f, in, res, &outarg, sizeof(outarg));
Miklos Szeredi5e183482001-10-31 14:52:35 +0000734}
735
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000736static void do_readlink(struct fuse *f, struct fuse_in_header *in)
737{
738 int res;
739 char link[PATH_MAX + 1];
Miklos Szeredib483c932001-10-29 14:57:57 +0000740 char *path;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000741
Miklos Szeredi5e183482001-10-31 14:52:35 +0000742 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000743 path = get_path(f, in->ino);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000744 if (path != NULL) {
Miklos Szeredi5e183482001-10-31 14:52:35 +0000745 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000746 if (f->op.readlink)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000747 res = f->op.readlink(path, link, sizeof(link));
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000748 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000749 }
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000750 link[PATH_MAX] = '\0';
Miklos Szeredi4b2bef42002-01-09 12:23:27 +0000751 send_reply(f, in, res, link, res == 0 ? strlen(link) : 0);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000752}
753
754static void do_getdir(struct fuse *f, struct fuse_in_header *in)
755{
756 int res;
757 struct fuse_getdir_out arg;
Miklos Szeredia181e612001-11-06 12:03:23 +0000758 struct fuse_dirhandle dh;
Miklos Szeredib483c932001-10-29 14:57:57 +0000759 char *path;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000760
Miklos Szeredib483c932001-10-29 14:57:57 +0000761 dh.fuse = f;
762 dh.fp = tmpfile();
763 dh.dir = in->ino;
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000764
Miklos Szeredi65afea12004-09-14 07:13:45 +0000765 res = -EIO;
766 if (dh.fp == NULL)
767 perror("fuse: failed to create temporary file");
768 else {
769 res = -ENOENT;
770 path = get_path(f, in->ino);
771 if (path != NULL) {
772 res = -ENOSYS;
773 if (f->op.getdir)
774 res = f->op.getdir(path, &dh, (fuse_dirfil_t) fill_dir);
775 free(path);
776 }
777 fflush(dh.fp);
778 }
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000779 memset(&arg, 0, sizeof(struct fuse_getdir_out));
Miklos Szeredi65afea12004-09-14 07:13:45 +0000780 if (res == 0)
781 arg.fd = fileno(dh.fp);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000782 send_reply(f, in, res, &arg, sizeof(arg));
Miklos Szeredi65afea12004-09-14 07:13:45 +0000783 if (dh.fp != NULL)
784 fclose(dh.fp);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000785}
786
Miklos Szeredib483c932001-10-29 14:57:57 +0000787static void do_mknod(struct fuse *f, struct fuse_in_header *in,
788 struct fuse_mknod_in *inarg)
789{
790 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000791 int res2;
Miklos Szeredib483c932001-10-29 14:57:57 +0000792 char *path;
Miklos Szeredi76f65782004-02-19 16:55:40 +0000793 char *name = PARAM(inarg);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000794 struct fuse_entry_out outarg;
Miklos Szeredib483c932001-10-29 14:57:57 +0000795
Miklos Szeredi5e183482001-10-31 14:52:35 +0000796 res = -ENOENT;
Miklos Szeredi76f65782004-02-19 16:55:40 +0000797 path = get_path_name(f, in->ino, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000798 if (path != NULL) {
799 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000800 printf("MKNOD %s\n", path);
801 fflush(stdout);
802 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000803 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000804 if (f->op.mknod && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000805 res = f->op.mknod(path, inarg->mode, inarg->rdev);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000806 if (res == 0)
Miklos Szeredi76f65782004-02-19 16:55:40 +0000807 res = lookup_path(f, in->ino, in->unique, name, path, &outarg);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000808 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000809 free(path);
Miklos Szeredib483c932001-10-29 14:57:57 +0000810 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000811 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
812 if (res == 0 && res2 == -ENOENT)
813 destroy_node(f, outarg.ino, in->unique);
Miklos Szeredib483c932001-10-29 14:57:57 +0000814}
815
816static void do_mkdir(struct fuse *f, struct fuse_in_header *in,
817 struct fuse_mkdir_in *inarg)
818{
819 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000820 int res2;
Miklos Szeredib483c932001-10-29 14:57:57 +0000821 char *path;
Miklos Szeredi76f65782004-02-19 16:55:40 +0000822 char *name = PARAM(inarg);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000823 struct fuse_entry_out outarg;
Miklos Szeredib483c932001-10-29 14:57:57 +0000824
Miklos Szeredi5e183482001-10-31 14:52:35 +0000825 res = -ENOENT;
Miklos Szeredi76f65782004-02-19 16:55:40 +0000826 path = get_path_name(f, in->ino, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000827 if (path != NULL) {
828 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000829 printf("MKDIR %s\n", path);
830 fflush(stdout);
831 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000832 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000833 if (f->op.mkdir && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000834 res = f->op.mkdir(path, inarg->mode);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000835 if (res == 0)
Miklos Szeredi76f65782004-02-19 16:55:40 +0000836 res = lookup_path(f, in->ino, in->unique, name, path, &outarg);
837 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000838 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000839 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000840 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
841 if (res == 0 && res2 == -ENOENT)
842 destroy_node(f, outarg.ino, in->unique);
Miklos Szeredib483c932001-10-29 14:57:57 +0000843}
844
Miklos Szeredib5958612004-02-20 14:10:49 +0000845static void do_unlink(struct fuse *f, struct fuse_in_header *in, char *name)
Miklos Szeredib483c932001-10-29 14:57:57 +0000846{
847 int res;
848 char *path;
849
Miklos Szeredi5e183482001-10-31 14:52:35 +0000850 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000851 path = get_path_name(f, in->ino, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000852 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +0000853 if (f->flags & FUSE_DEBUG) {
854 printf("UNLINK %s\n", path);
855 fflush(stdout);
856 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000857 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000858 if (f->op.unlink) {
Miklos Szeredi2529ca22004-07-13 15:36:52 +0000859 if (!(f->flags & FUSE_HARD_REMOVE) && is_open(f, in->ino, name))
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000860 res = hide_node(f, path, in->ino, name);
861 else {
862 res = f->op.unlink(path);
863 if (res == 0)
864 remove_node(f, in->ino, name);
865 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000866 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000867 free(path);
Miklos Szeredib483c932001-10-29 14:57:57 +0000868 }
Miklos Szeredib5958612004-02-20 14:10:49 +0000869 send_reply(f, in, res, NULL, 0);
870}
871
872static void do_rmdir(struct fuse *f, struct fuse_in_header *in, char *name)
873{
874 int res;
875 char *path;
876
877 res = -ENOENT;
878 path = get_path_name(f, in->ino, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000879 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +0000880 if (f->flags & FUSE_DEBUG) {
881 printf("RMDIR %s\n", path);
882 fflush(stdout);
883 }
Miklos Szeredib5958612004-02-20 14:10:49 +0000884 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000885 if (f->op.rmdir) {
Miklos Szeredib5958612004-02-20 14:10:49 +0000886 res = f->op.rmdir(path);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000887 if (res == 0)
Miklos Szeredib5958612004-02-20 14:10:49 +0000888 remove_node(f, in->ino, name);
889 }
890 free(path);
891 }
Miklos Szeredib483c932001-10-29 14:57:57 +0000892 send_reply(f, in, res, NULL, 0);
893}
894
895static void do_symlink(struct fuse *f, struct fuse_in_header *in, char *name,
896 char *link)
897{
898 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000899 int res2;
Miklos Szeredib483c932001-10-29 14:57:57 +0000900 char *path;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000901 struct fuse_entry_out outarg;
Miklos Szeredib483c932001-10-29 14:57:57 +0000902
Miklos Szeredi5e183482001-10-31 14:52:35 +0000903 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000904 path = get_path_name(f, in->ino, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000905 if (path != NULL) {
906 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000907 printf("SYMLINK %s\n", path);
908 fflush(stdout);
909 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000910 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000911 if (f->op.symlink && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000912 res = f->op.symlink(link, path);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000913 if (res == 0)
Miklos Szeredi76f65782004-02-19 16:55:40 +0000914 res = lookup_path(f, in->ino, in->unique, name, path, &outarg);
915 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000916 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000917 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000918 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
919 if (res == 0 && res2 == -ENOENT)
920 destroy_node(f, outarg.ino, in->unique);
921
Miklos Szeredib483c932001-10-29 14:57:57 +0000922}
923
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000924static void do_rename(struct fuse *f, struct fuse_in_header *in,
925 struct fuse_rename_in *inarg)
926{
927 int res;
928 fino_t olddir = in->ino;
929 fino_t newdir = inarg->newdir;
Miklos Szeredi6bf8b682002-10-28 08:49:39 +0000930 char *oldname = PARAM(inarg);
931 char *newname = oldname + strlen(oldname) + 1;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000932 char *oldpath;
933 char *newpath;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000934
Miklos Szeredi5e183482001-10-31 14:52:35 +0000935 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000936 oldpath = get_path_name(f, olddir, oldname);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000937 if (oldpath != NULL) {
Miklos Szeredia181e612001-11-06 12:03:23 +0000938 newpath = get_path_name(f, newdir, newname);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000939 if (newpath != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +0000940 if (f->flags & FUSE_DEBUG) {
941 printf("RENAME %s -> %s\n", oldpath, newpath);
942 fflush(stdout);
943 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000944 res = -ENOSYS;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000945 if (f->op.rename) {
946 res = 0;
Miklos Szeredi2529ca22004-07-13 15:36:52 +0000947 if (!(f->flags & FUSE_HARD_REMOVE) &&
948 is_open(f, newdir, newname))
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000949 res = hide_node(f, newpath, newdir, newname);
950 if (res == 0) {
951 res = f->op.rename(oldpath, newpath);
952 if (res == 0)
953 rename_node(f, olddir, oldname, newdir, newname, 0);
954 }
955 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000956 free(newpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000957 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000958 free(oldpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000959 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000960 send_reply(f, in, res, NULL, 0);
961}
962
963static void do_link(struct fuse *f, struct fuse_in_header *in,
Miklos Szeredi5e183482001-10-31 14:52:35 +0000964 struct fuse_link_in *arg)
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000965{
966 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000967 int res2;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000968 char *oldpath;
969 char *newpath;
Miklos Szeredi76f65782004-02-19 16:55:40 +0000970 char *name = PARAM(arg);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000971 struct fuse_entry_out outarg;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000972
Miklos Szeredi5e183482001-10-31 14:52:35 +0000973 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000974 oldpath = get_path(f, in->ino);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000975 if (oldpath != NULL) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000976 newpath = get_path_name(f, arg->newdir, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000977 if (newpath != NULL) {
978 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000979 printf("LINK %s\n", newpath);
980 fflush(stdout);
981 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000982 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000983 if (f->op.link && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000984 res = f->op.link(oldpath, newpath);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000985 if (res == 0)
Miklos Szeredi76f65782004-02-19 16:55:40 +0000986 res = lookup_path(f, arg->newdir, in->unique, name,
987 newpath, &outarg);
988 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000989 free(newpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000990 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000991 free(oldpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000992 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000993 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
994 if (res == 0 && res2 == -ENOENT)
995 destroy_node(f, outarg.ino, in->unique);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000996}
997
Miklos Szeredi5e183482001-10-31 14:52:35 +0000998static void do_open(struct fuse *f, struct fuse_in_header *in,
999 struct fuse_open_in *arg)
1000{
1001 int res;
1002 char *path;
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001003 struct fuse_open_out outarg;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001004
1005 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +00001006 path = get_path(f, in->ino);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001007 if (path != NULL) {
Miklos Szeredi5e183482001-10-31 14:52:35 +00001008 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001009 if (f->op.open)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +00001010 res = f->op.open(path, arg->flags);
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +00001011 }
Miklos Szeredi73798f92004-07-12 15:55:11 +00001012 if (res == 0) {
1013 int res2;
1014
1015 /* If the request is interrupted the lock must be held until
1016 the cancellation is finished. Otherwise there could be
1017 races with rename/unlink, against which the kernel can't
1018 protect */
1019 pthread_mutex_lock(&f->lock);
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001020 f->fh_ctr ++;
1021 outarg.fh = f->fh_ctr;
1022 if (f->flags & FUSE_DEBUG) {
1023 printf("OPEN[%u] flags: 0x%x\n", outarg.fh, arg->flags);
1024 fflush(stdout);
1025 }
1026
1027 res2 = __send_reply(f, in, res, &outarg, sizeof(outarg), 1);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001028 if(res2 == -ENOENT) {
1029 /* The open syscall was interrupted, so it must be cancelled */
1030 if(f->op.release)
1031 f->op.release(path, arg->flags);
Miklos Szeredi73798f92004-07-12 15:55:11 +00001032 } else
1033 get_node(f, in->ino)->open_count ++;
1034 pthread_mutex_unlock(&f->lock);
1035
1036 } else
1037 send_reply(f, in, res, NULL, 0);
1038
Miklos Szeredi9a31cca2004-06-26 21:11:25 +00001039 if (path)
1040 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001041}
1042
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001043static void do_flush(struct fuse *f, struct fuse_in_header *in,
1044 struct fuse_flush_in *arg)
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001045{
1046 char *path;
1047 int res;
1048
1049 res = -ENOENT;
1050 path = get_path(f, in->ino);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001051 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001052 if (f->flags & FUSE_DEBUG) {
1053 printf("FLUSH[%u]\n", arg->fh);
1054 fflush(stdout);
1055 }
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001056 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001057 if (f->op.flush)
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001058 res = f->op.flush(path);
1059 free(path);
1060 }
1061 send_reply(f, in, res, NULL, 0);
1062}
1063
Miklos Szeredi9478e862002-12-11 09:50:26 +00001064static void do_release(struct fuse *f, struct fuse_in_header *in,
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001065 struct fuse_release_in *arg)
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001066{
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001067 struct node *node;
1068 char *path;
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001069
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001070 pthread_mutex_lock(&f->lock);
1071 node = get_node(f, in->ino);
1072 --node->open_count;
1073 pthread_mutex_unlock(&f->lock);
1074
1075 path = get_path(f, in->ino);
1076 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001077 if (f->flags & FUSE_DEBUG) {
1078 printf("RELEASE[%u]\n", arg->fh);
1079 fflush(stdout);
1080 }
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001081 if (f->op.release)
Miklos Szeredib3210582004-06-23 13:54:33 +00001082 f->op.release(path, arg->flags);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001083
1084 if(node->is_hidden && node->open_count == 0)
1085 /* can now clean up this hidden file */
1086 f->op.unlink(path);
1087
1088 free(path);
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001089 }
Miklos Szeredi556d03d2004-06-30 11:13:41 +00001090 send_reply(f, in, 0, NULL, 0);
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001091}
1092
Miklos Szeredi5e183482001-10-31 14:52:35 +00001093static void do_read(struct fuse *f, struct fuse_in_header *in,
1094 struct fuse_read_in *arg)
1095{
1096 int res;
1097 char *path;
Miklos Szeredi43696432001-11-18 19:15:05 +00001098 char *outbuf = (char *) malloc(sizeof(struct fuse_out_header) + arg->size);
1099 struct fuse_out_header *out = (struct fuse_out_header *) outbuf;
1100 char *buf = outbuf + sizeof(struct fuse_out_header);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001101 size_t size;
Miklos Szeredi43696432001-11-18 19:15:05 +00001102 size_t outsize;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001103
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001104 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +00001105 path = get_path(f, in->ino);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001106 if (path != NULL) {
1107 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001108 printf("READ[%u] %u bytes from %llu\n", arg->fh, arg->size,
1109 arg->offset);
Miklos Szeredi6ebe2342002-01-06 21:44:16 +00001110 fflush(stdout);
1111 }
1112
Miklos Szeredi5e183482001-10-31 14:52:35 +00001113 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001114 if (f->op.read)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +00001115 res = f->op.read(path, buf, arg->size, arg->offset);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001116 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001117 }
1118
1119 size = 0;
Miklos Szeredi25385bb2004-07-06 22:27:36 +00001120 if (res >= 0) {
Miklos Szeredi5e183482001-10-31 14:52:35 +00001121 size = res;
1122 res = 0;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001123 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001124 printf(" READ[%u] %u bytes\n", arg->fh, size);
Miklos Szeredi6ebe2342002-01-06 21:44:16 +00001125 fflush(stdout);
1126 }
Miklos Szeredi5e183482001-10-31 14:52:35 +00001127 }
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001128 memset(out, 0, sizeof(struct fuse_out_header));
Miklos Szeredi43696432001-11-18 19:15:05 +00001129 out->unique = in->unique;
1130 out->error = res;
1131 outsize = sizeof(struct fuse_out_header) + size;
1132
Miklos Szeredi73798f92004-07-12 15:55:11 +00001133 send_reply_raw(f, outbuf, outsize, 0);
Miklos Szeredi43696432001-11-18 19:15:05 +00001134 free(outbuf);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001135}
Miklos Szeredib483c932001-10-29 14:57:57 +00001136
Miklos Szeredia181e612001-11-06 12:03:23 +00001137static void do_write(struct fuse *f, struct fuse_in_header *in,
1138 struct fuse_write_in *arg)
1139{
1140 int res;
1141 char *path;
Miklos Szerediad051c32004-07-02 09:22:50 +00001142 struct fuse_write_out outarg;
Miklos Szeredia181e612001-11-06 12:03:23 +00001143
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001144 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +00001145 path = get_path(f, in->ino);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001146 if (path != NULL) {
1147 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001148 printf("WRITE%s[%u] %u bytes to %llu\n",
1149 arg->writepage ? "PAGE" : "", arg->fh, arg->size,
1150 arg->offset);
Miklos Szeredi6ebe2342002-01-06 21:44:16 +00001151 fflush(stdout);
1152 }
1153
Miklos Szeredia181e612001-11-06 12:03:23 +00001154 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001155 if (f->op.write)
Miklos Szeredi6bf8b682002-10-28 08:49:39 +00001156 res = f->op.write(path, PARAM(arg), arg->size, arg->offset);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001157 free(path);
Miklos Szeredia181e612001-11-06 12:03:23 +00001158 }
1159
Miklos Szerediad051c32004-07-02 09:22:50 +00001160 if (res >= 0) {
1161 outarg.size = res;
1162 res = 0;
Miklos Szeredia181e612001-11-06 12:03:23 +00001163 }
1164
Miklos Szerediad051c32004-07-02 09:22:50 +00001165 send_reply(f, in, res, &outarg, sizeof(outarg));
Miklos Szeredia181e612001-11-06 12:03:23 +00001166}
1167
Miklos Szeredi77f39942004-03-25 11:17:52 +00001168static int default_statfs(struct statfs *buf)
1169{
1170 buf->f_namelen = 255;
1171 buf->f_bsize = 512;
1172 return 0;
1173}
1174
Miklos Szeredi18e75e42004-02-19 14:23:27 +00001175static void convert_statfs(struct statfs *statfs, struct fuse_kstatfs *kstatfs)
1176{
1177 kstatfs->bsize = statfs->f_bsize;
1178 kstatfs->blocks = statfs->f_blocks;
1179 kstatfs->bfree = statfs->f_bfree;
1180 kstatfs->bavail = statfs->f_bavail;
1181 kstatfs->files = statfs->f_files;
1182 kstatfs->ffree = statfs->f_ffree;
1183 kstatfs->namelen = statfs->f_namelen;
1184}
1185
Mark Glinesd84b39a2002-01-07 16:32:02 +00001186static void do_statfs(struct fuse *f, struct fuse_in_header *in)
1187{
1188 int res;
Mark Glinesd84b39a2002-01-07 16:32:02 +00001189 struct fuse_statfs_out arg;
Miklos Szeredi18e75e42004-02-19 14:23:27 +00001190 struct statfs buf;
Mark Glinesd84b39a2002-01-07 16:32:02 +00001191
Miklos Szeredi77f39942004-03-25 11:17:52 +00001192 memset(&buf, 0, sizeof(struct statfs));
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001193 if (f->op.statfs)
Miklos Szeredi18e75e42004-02-19 14:23:27 +00001194 res = f->op.statfs("/", &buf);
Miklos Szeredi77f39942004-03-25 11:17:52 +00001195 else
1196 res = default_statfs(&buf);
1197
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001198 if (res == 0)
Miklos Szeredi77f39942004-03-25 11:17:52 +00001199 convert_statfs(&buf, &arg.st);
Miklos Szeredi4b2bef42002-01-09 12:23:27 +00001200
Mark Glinesd84b39a2002-01-07 16:32:02 +00001201 send_reply(f, in, res, &arg, sizeof(arg));
1202}
1203
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001204static void do_fsync(struct fuse *f, struct fuse_in_header *in,
1205 struct fuse_fsync_in *inarg)
1206{
1207 int res;
1208 char *path;
1209
1210 res = -ENOENT;
1211 path = get_path(f, in->ino);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001212 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001213 if (f->flags & FUSE_DEBUG) {
1214 printf("FSYNC[%u]\n", inarg->fh);
1215 fflush(stdout);
1216 }
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001217 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001218 if (f->op.fsync)
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001219 res = f->op.fsync(path, inarg->datasync);
1220 free(path);
1221 }
1222 send_reply(f, in, res, NULL, 0);
1223}
1224
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001225static void do_setxattr(struct fuse *f, struct fuse_in_header *in,
1226 struct fuse_setxattr_in *arg)
1227{
1228 int res;
1229 char *path;
1230 char *name = PARAM(arg);
1231 unsigned char *value = name + strlen(name) + 1;
1232
1233 res = -ENOENT;
1234 path = get_path(f, in->ino);
1235 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001236 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001237 if (f->op.setxattr)
1238 res = f->op.setxattr(path, name, value, arg->size, arg->flags);
1239 free(path);
1240 }
1241 send_reply(f, in, res, NULL, 0);
1242}
1243
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001244static int common_getxattr(struct fuse *f, struct fuse_in_header *in,
1245 const char *name, char *value, size_t size)
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001246{
1247 int res;
1248 char *path;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001249
1250 res = -ENOENT;
1251 path = get_path(f, in->ino);
1252 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001253 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001254 if (f->op.getxattr)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001255 res = f->op.getxattr(path, name, value, size);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001256 free(path);
1257 }
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001258 return res;
1259}
1260
1261static void do_getxattr_read(struct fuse *f, struct fuse_in_header *in,
1262 const char *name, size_t size)
1263{
1264 int res;
1265 char *outbuf = (char *) malloc(sizeof(struct fuse_out_header) + size);
1266 struct fuse_out_header *out = (struct fuse_out_header *) outbuf;
1267 char *value = outbuf + sizeof(struct fuse_out_header);
1268
1269 res = common_getxattr(f, in, name, value, size);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001270 size = 0;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001271 if (res > 0) {
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001272 size = res;
1273 res = 0;
1274 }
1275 memset(out, 0, sizeof(struct fuse_out_header));
1276 out->unique = in->unique;
1277 out->error = res;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001278
Miklos Szeredi73798f92004-07-12 15:55:11 +00001279 send_reply_raw(f, outbuf, sizeof(struct fuse_out_header) + size, 0);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001280 free(outbuf);
1281}
1282
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001283static void do_getxattr_size(struct fuse *f, struct fuse_in_header *in,
1284 const char *name)
1285{
1286 int res;
1287 struct fuse_getxattr_out arg;
1288
1289 res = common_getxattr(f, in, name, NULL, 0);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001290 if (res >= 0) {
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001291 arg.size = res;
1292 res = 0;
1293 }
1294 send_reply(f, in, res, &arg, sizeof(arg));
1295}
1296
1297static void do_getxattr(struct fuse *f, struct fuse_in_header *in,
1298 struct fuse_getxattr_in *arg)
1299{
1300 char *name = PARAM(arg);
1301
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001302 if (arg->size)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001303 do_getxattr_read(f, in, name, arg->size);
1304 else
1305 do_getxattr_size(f, in, name);
1306}
1307
1308static int common_listxattr(struct fuse *f, struct fuse_in_header *in,
1309 char *list, size_t size)
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001310{
1311 int res;
1312 char *path;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001313
1314 res = -ENOENT;
1315 path = get_path(f, in->ino);
1316 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001317 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001318 if (f->op.listxattr)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001319 res = f->op.listxattr(path, list, size);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001320 free(path);
1321 }
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001322 return res;
1323}
1324
1325static void do_listxattr_read(struct fuse *f, struct fuse_in_header *in,
1326 size_t size)
1327{
1328 int res;
1329 char *outbuf = (char *) malloc(sizeof(struct fuse_out_header) + size);
1330 struct fuse_out_header *out = (struct fuse_out_header *) outbuf;
1331 char *list = outbuf + sizeof(struct fuse_out_header);
1332
1333 res = common_listxattr(f, in, list, size);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001334 size = 0;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001335 if (res > 0) {
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001336 size = res;
1337 res = 0;
1338 }
1339 memset(out, 0, sizeof(struct fuse_out_header));
1340 out->unique = in->unique;
1341 out->error = res;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001342
Miklos Szeredi73798f92004-07-12 15:55:11 +00001343 send_reply_raw(f, outbuf, sizeof(struct fuse_out_header) + size, 0);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001344 free(outbuf);
1345}
1346
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001347static void do_listxattr_size(struct fuse *f, struct fuse_in_header *in)
1348{
1349 int res;
1350 struct fuse_getxattr_out arg;
1351
1352 res = common_listxattr(f, in, NULL, 0);
1353 if (res >= 0) {
1354 arg.size = res;
1355 res = 0;
1356 }
1357 send_reply(f, in, res, &arg, sizeof(arg));
1358}
1359
1360static void do_listxattr(struct fuse *f, struct fuse_in_header *in,
1361 struct fuse_getxattr_in *arg)
1362{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001363 if (arg->size)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001364 do_listxattr_read(f, in, arg->size);
1365 else
1366 do_listxattr_size(f, in);
1367}
1368
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001369static void do_removexattr(struct fuse *f, struct fuse_in_header *in,
1370 char *name)
1371{
1372 int res;
1373 char *path;
1374
1375 res = -ENOENT;
1376 path = get_path(f, in->ino);
1377 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001378 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001379 if (f->op.removexattr)
1380 res = f->op.removexattr(path, name);
1381 free(path);
1382 }
1383 send_reply(f, in, res, NULL, 0);
1384}
1385
1386
Miklos Szeredi43696432001-11-18 19:15:05 +00001387static void free_cmd(struct fuse_cmd *cmd)
1388{
1389 free(cmd->buf);
1390 free(cmd);
1391}
1392
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001393void __fuse_process_cmd(struct fuse *f, struct fuse_cmd *cmd)
Miklos Szeredia181e612001-11-06 12:03:23 +00001394{
Miklos Szeredia181e612001-11-06 12:03:23 +00001395 struct fuse_in_header *in = (struct fuse_in_header *) cmd->buf;
1396 void *inarg = cmd->buf + sizeof(struct fuse_in_header);
1397 size_t argsize;
Miklos Szeredife25def2001-12-20 15:38:05 +00001398 struct fuse_context *ctx = fuse_get_context(f);
Miklos Szeredia181e612001-11-06 12:03:23 +00001399
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001400 dec_avail(f);
Miklos Szeredi33232032001-11-19 17:55:51 +00001401
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001402 if ((f->flags & FUSE_DEBUG)) {
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001403 printf("unique: %i, opcode: %s (%i), ino: %li, insize: %i\n",
1404 in->unique, opname(in->opcode), in->opcode, in->ino,
1405 cmd->buflen);
Miklos Szeredic0938ea2001-11-07 12:35:06 +00001406 fflush(stdout);
1407 }
Miklos Szeredife25def2001-12-20 15:38:05 +00001408
1409 ctx->uid = in->uid;
1410 ctx->gid = in->gid;
Miklos Szeredia181e612001-11-06 12:03:23 +00001411
1412 argsize = cmd->buflen - sizeof(struct fuse_in_header);
1413
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001414 switch (in->opcode) {
Miklos Szeredia181e612001-11-06 12:03:23 +00001415 case FUSE_LOOKUP:
1416 do_lookup(f, in, (char *) inarg);
1417 break;
1418
Miklos Szeredia181e612001-11-06 12:03:23 +00001419 case FUSE_GETATTR:
1420 do_getattr(f, in);
1421 break;
1422
1423 case FUSE_SETATTR:
1424 do_setattr(f, in, (struct fuse_setattr_in *) inarg);
1425 break;
1426
1427 case FUSE_READLINK:
1428 do_readlink(f, in);
1429 break;
1430
1431 case FUSE_GETDIR:
1432 do_getdir(f, in);
1433 break;
1434
1435 case FUSE_MKNOD:
1436 do_mknod(f, in, (struct fuse_mknod_in *) inarg);
1437 break;
1438
1439 case FUSE_MKDIR:
1440 do_mkdir(f, in, (struct fuse_mkdir_in *) inarg);
1441 break;
1442
1443 case FUSE_UNLINK:
Miklos Szeredib5958612004-02-20 14:10:49 +00001444 do_unlink(f, in, (char *) inarg);
1445 break;
1446
Miklos Szeredia181e612001-11-06 12:03:23 +00001447 case FUSE_RMDIR:
Miklos Szeredib5958612004-02-20 14:10:49 +00001448 do_rmdir(f, in, (char *) inarg);
Miklos Szeredia181e612001-11-06 12:03:23 +00001449 break;
1450
1451 case FUSE_SYMLINK:
1452 do_symlink(f, in, (char *) inarg,
1453 ((char *) inarg) + strlen((char *) inarg) + 1);
1454 break;
1455
1456 case FUSE_RENAME:
1457 do_rename(f, in, (struct fuse_rename_in *) inarg);
1458 break;
1459
1460 case FUSE_LINK:
1461 do_link(f, in, (struct fuse_link_in *) inarg);
1462 break;
1463
1464 case FUSE_OPEN:
1465 do_open(f, in, (struct fuse_open_in *) inarg);
1466 break;
1467
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001468 case FUSE_FLUSH:
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001469 do_flush(f, in, (struct fuse_flush_in *) inarg);
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001470 break;
1471
Miklos Szeredi9478e862002-12-11 09:50:26 +00001472 case FUSE_RELEASE:
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001473 do_release(f, in, (struct fuse_release_in *) inarg);
Miklos Szeredi9478e862002-12-11 09:50:26 +00001474 break;
1475
Miklos Szeredia181e612001-11-06 12:03:23 +00001476 case FUSE_READ:
1477 do_read(f, in, (struct fuse_read_in *) inarg);
1478 break;
1479
1480 case FUSE_WRITE:
1481 do_write(f, in, (struct fuse_write_in *) inarg);
1482 break;
1483
Mark Glinesd84b39a2002-01-07 16:32:02 +00001484 case FUSE_STATFS:
1485 do_statfs(f, in);
1486 break;
1487
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001488 case FUSE_FSYNC:
1489 do_fsync(f, in, (struct fuse_fsync_in *) inarg);
1490 break;
1491
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001492 case FUSE_SETXATTR:
1493 do_setxattr(f, in, (struct fuse_setxattr_in *) inarg);
1494 break;
1495
1496 case FUSE_GETXATTR:
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001497 do_getxattr(f, in, (struct fuse_getxattr_in *) inarg);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001498 break;
1499
1500 case FUSE_LISTXATTR:
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001501 do_listxattr(f, in, (struct fuse_getxattr_in *) inarg);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001502 break;
1503
1504 case FUSE_REMOVEXATTR:
1505 do_removexattr(f, in, (char *) inarg);
1506 break;
1507
Miklos Szeredia181e612001-11-06 12:03:23 +00001508 default:
Miklos Szeredi43696432001-11-18 19:15:05 +00001509 send_reply(f, in, -ENOSYS, NULL, 0);
Miklos Szeredia181e612001-11-06 12:03:23 +00001510 }
Miklos Szeredi43696432001-11-18 19:15:05 +00001511
1512 free_cmd(cmd);
Miklos Szeredia181e612001-11-06 12:03:23 +00001513}
1514
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001515int __fuse_exited(struct fuse* f)
1516{
1517 return f->exited;
1518}
1519
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001520struct fuse_cmd *__fuse_read_cmd(struct fuse *f)
Miklos Szeredi2df1c042001-11-06 15:07:17 +00001521{
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001522 ssize_t res;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001523 struct fuse_cmd *cmd;
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001524 struct fuse_in_header *in;
1525 void *inarg;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001526
Miklos Szeredi43696432001-11-18 19:15:05 +00001527 cmd = (struct fuse_cmd *) malloc(sizeof(struct fuse_cmd));
1528 cmd->buf = (char *) malloc(FUSE_MAX_IN);
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001529 in = (struct fuse_in_header *) cmd->buf;
1530 inarg = cmd->buf + sizeof(struct fuse_in_header);
Miklos Szeredi43696432001-11-18 19:15:05 +00001531
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001532 res = read(f->fd, cmd->buf, FUSE_MAX_IN);
1533 if (res == -1) {
1534 free_cmd(cmd);
1535 if (__fuse_exited(f) || errno == EINTR)
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001536 return NULL;
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001537
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001538 /* ENODEV means we got unmounted, so we silenty return failure */
1539 if (errno != ENODEV) {
1540 /* BAD... This will happen again */
1541 perror("fuse: reading device");
1542 }
1543
1544 fuse_exit(f);
1545 return NULL;
1546 }
1547 if ((size_t) res < sizeof(struct fuse_in_header)) {
1548 free_cmd(cmd);
1549 /* Cannot happen */
1550 fprintf(stderr, "short read on fuse device\n");
1551 fuse_exit(f);
1552 return NULL;
1553 }
1554 cmd->buflen = res;
1555
1556 /* Forget is special, it can be done without messing with threads. */
1557 if (in->opcode == FUSE_FORGET) {
1558 do_forget(f, in, (struct fuse_forget_in *) inarg);
1559 free_cmd(cmd);
1560 return NULL;
1561 }
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001562
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001563 return cmd;
Miklos Szeredi2df1c042001-11-06 15:07:17 +00001564}
1565
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001566void fuse_loop(struct fuse *f)
1567{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001568 if (f == NULL)
Miklos Szeredic40748a2004-02-20 16:38:45 +00001569 return;
1570
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001571 while (1) {
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001572 struct fuse_cmd *cmd;
1573
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001574 if (__fuse_exited(f))
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001575 return;
1576
1577 cmd = __fuse_read_cmd(f);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001578 if (cmd == NULL)
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001579 continue;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001580
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001581 __fuse_process_cmd(f, cmd);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001582 }
1583}
1584
Miklos Szeredi891b8742004-07-29 09:27:49 +00001585int fuse_invalidate(struct fuse *f, const char *path)
1586{
1587 int res;
1588 int err;
1589 fino_t ino;
1590 struct fuse_user_header h;
1591
1592 err = path_lookup(f, path, &ino);
1593 if (err) {
1594 if (err == -ENOENT)
1595 return 0;
1596 else
1597 return err;
1598 }
1599
1600 memset(&h, 0, sizeof(struct fuse_user_header));
1601 h.opcode = FUSE_INVALIDATE;
1602 h.ino = ino;
1603
1604 if ((f->flags & FUSE_DEBUG)) {
1605 printf("INVALIDATE ino: %li\n", ino);
1606 fflush(stdout);
1607 }
1608
1609 res = write(f->fd, &h, sizeof(struct fuse_user_header));
1610 if (res == -1) {
1611 if (errno != ENOENT) {
1612 perror("fuse: writing device");
1613 return -errno;
1614 }
1615 }
1616 return 0;
1617}
1618
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001619void fuse_exit(struct fuse *f)
1620{
1621 f->exited = 1;
1622}
1623
Miklos Szeredi2e50d432001-12-20 12:17:25 +00001624struct fuse_context *fuse_get_context(struct fuse *f)
1625{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001626 if (f->getcontext)
Miklos Szeredi2e50d432001-12-20 12:17:25 +00001627 return f->getcontext(f);
1628 else
1629 return &f->context;
1630}
1631
Miklos Szeredic40748a2004-02-20 16:38:45 +00001632static int check_version(struct fuse *f)
1633{
1634 int res;
1635 FILE *vf = fopen(FUSE_VERSION_FILE, "r");
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001636 if (vf == NULL) {
Miklos Szeredic40748a2004-02-20 16:38:45 +00001637 fprintf(stderr, "fuse: kernel interface too old, need >= %i.%i\n",
1638 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
1639 return -1;
1640 }
1641 res = fscanf(vf, "%i.%i", &f->majorver, &f->minorver);
1642 fclose(vf);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001643 if (res != 2) {
Miklos Szeredic40748a2004-02-20 16:38:45 +00001644 fprintf(stderr, "fuse: error reading %s\n", FUSE_VERSION_FILE);
1645 return -1;
1646 }
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001647 if (f->majorver != FUSE_KERNEL_VERSION) {
Miklos Szeredic40748a2004-02-20 16:38:45 +00001648 fprintf(stderr, "fuse: bad kernel interface major version: needs %i\n",
1649 FUSE_KERNEL_VERSION);
1650 return -1;
1651 }
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001652 if (f->minorver < FUSE_KERNEL_MINOR_VERSION) {
Miklos Szeredic40748a2004-02-20 16:38:45 +00001653 fprintf(stderr, "fuse: kernel interface too old: need >= %i.%i",
1654 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
1655 return -1;
1656 }
1657
1658 return 0;
1659}
1660
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001661
1662int fuse_is_lib_option(const char *opt)
1663{
1664 if (strcmp(opt, "debug") == 0 ||
1665 strcmp(opt, "hard_remove") == 0)
1666 return 1;
1667 else
1668 return 0;
1669}
1670
1671static void parse_lib_opts(struct fuse *f, const char *opts)
1672{
1673 if (opts) {
1674 char *xopts = strdup(opts);
1675 char *s = xopts;
1676 char *opt;
1677
1678 while((opt = strsep(&s, ","))) {
1679 if (strcmp(opt, "debug") == 0)
1680 f->flags |= FUSE_DEBUG;
1681 else if (strcmp(opt, "hard_remove") == 0)
1682 f->flags |= FUSE_HARD_REMOVE;
1683 else
1684 fprintf(stderr, "fuse: warning: unknown option `%s'\n", opt);
1685 }
1686 free(xopts);
1687 }
1688}
1689
1690struct fuse *fuse_new(int fd, const char *opts, const struct fuse_operations *op)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001691{
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001692 struct fuse *f;
1693 struct node *root;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001694
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001695 f = (struct fuse *) calloc(1, sizeof(struct fuse));
Miklos Szeredi2df1c042001-11-06 15:07:17 +00001696
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001697 if (check_version(f) == -1) {
Miklos Szeredic40748a2004-02-20 16:38:45 +00001698 free(f);
1699 return NULL;
1700 }
1701
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001702 parse_lib_opts(f, opts);
Miklos Szeredi8cffdb92001-11-09 14:49:18 +00001703 f->fd = fd;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001704 f->ctr = 0;
Miklos Szeredi76f65782004-02-19 16:55:40 +00001705 f->generation = 0;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001706 /* FIXME: Dynamic hash table */
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001707 f->name_table_size = 14057;
1708 f->name_table = (struct node **)
1709 calloc(1, sizeof(struct node *) * f->name_table_size);
1710 f->ino_table_size = 14057;
1711 f->ino_table = (struct node **)
1712 calloc(1, sizeof(struct node *) * f->ino_table_size);
Miklos Szeredia181e612001-11-06 12:03:23 +00001713 pthread_mutex_init(&f->lock, NULL);
Miklos Szeredi33232032001-11-19 17:55:51 +00001714 f->numworker = 0;
1715 f->numavail = 0;
Miklos Szeredi2e50d432001-12-20 12:17:25 +00001716 f->op = *op;
1717 f->getcontext = NULL;
1718 f->context.uid = 0;
1719 f->context.gid = 0;
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001720 f->exited = 0;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001721
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001722 root = (struct node *) calloc(1, sizeof(struct node));
Miklos Szeredi8cffdb92001-11-09 14:49:18 +00001723 root->mode = 0;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001724 root->rdev = 0;
1725 root->name = strdup("/");
1726 root->parent = 0;
Miklos Szeredi76f65782004-02-19 16:55:40 +00001727 root->ino = FUSE_ROOT_INO;
1728 root->generation = 0;
1729 hash_ino(f, root);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001730
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001731 return f;
1732}
1733
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001734void fuse_destroy(struct fuse *f)
1735{
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001736 size_t i;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001737 for (i = 0; i < f->ino_table_size; i++) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001738 struct node *node;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001739
1740 for (node = f->ino_table[i]; node != NULL; node = node->ino_next) {
1741 if (node->is_hidden) {
1742 char *path = get_path(f, node->ino);
1743 if (path)
1744 f->op.unlink(path);
1745 }
1746 }
1747 }
1748 for (i = 0; i < f->ino_table_size; i++) {
1749 struct node *node;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001750 struct node *next;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001751
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001752 for (node = f->ino_table[i]; node != NULL; node = next) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001753 next = node->ino_next;
1754 free_node(node);
1755 }
1756 }
1757 free(f->ino_table);
1758 free(f->name_table);
Miklos Szeredia181e612001-11-06 12:03:23 +00001759 pthread_mutex_destroy(&f->lock);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001760 free(f);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001761}