blob: 671fbcc2bcbdf82e1698d9b0c89b7ba70fabcc62 [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 Szeredi97c61e92001-11-07 12:09:43 +0000320 node = get_node(f, ino);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000321 if (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 Szeredi5e183482001-10-31 14:52:35 +0000764 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000765 path = get_path(f, in->ino);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000766 if (path != NULL) {
Miklos Szeredi5e183482001-10-31 14:52:35 +0000767 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000768 if (f->op.getdir)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000769 res = f->op.getdir(path, &dh, (fuse_dirfil_t) fill_dir);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000770 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000771 }
Miklos Szeredib483c932001-10-29 14:57:57 +0000772 fflush(dh.fp);
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000773
774 memset(&arg, 0, sizeof(struct fuse_getdir_out));
Miklos Szeredib483c932001-10-29 14:57:57 +0000775 arg.fd = fileno(dh.fp);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000776 send_reply(f, in, res, &arg, sizeof(arg));
Miklos Szeredib483c932001-10-29 14:57:57 +0000777 fclose(dh.fp);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000778}
779
Miklos Szeredib483c932001-10-29 14:57:57 +0000780static void do_mknod(struct fuse *f, struct fuse_in_header *in,
781 struct fuse_mknod_in *inarg)
782{
783 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000784 int res2;
Miklos Szeredib483c932001-10-29 14:57:57 +0000785 char *path;
Miklos Szeredi76f65782004-02-19 16:55:40 +0000786 char *name = PARAM(inarg);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000787 struct fuse_entry_out outarg;
Miklos Szeredib483c932001-10-29 14:57:57 +0000788
Miklos Szeredi5e183482001-10-31 14:52:35 +0000789 res = -ENOENT;
Miklos Szeredi76f65782004-02-19 16:55:40 +0000790 path = get_path_name(f, in->ino, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000791 if (path != NULL) {
792 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000793 printf("MKNOD %s\n", path);
794 fflush(stdout);
795 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000796 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000797 if (f->op.mknod && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000798 res = f->op.mknod(path, inarg->mode, inarg->rdev);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000799 if (res == 0)
Miklos Szeredi76f65782004-02-19 16:55:40 +0000800 res = lookup_path(f, in->ino, in->unique, name, path, &outarg);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000801 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000802 free(path);
Miklos Szeredib483c932001-10-29 14:57:57 +0000803 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000804 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
805 if (res == 0 && res2 == -ENOENT)
806 destroy_node(f, outarg.ino, in->unique);
Miklos Szeredib483c932001-10-29 14:57:57 +0000807}
808
809static void do_mkdir(struct fuse *f, struct fuse_in_header *in,
810 struct fuse_mkdir_in *inarg)
811{
812 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000813 int res2;
Miklos Szeredib483c932001-10-29 14:57:57 +0000814 char *path;
Miklos Szeredi76f65782004-02-19 16:55:40 +0000815 char *name = PARAM(inarg);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000816 struct fuse_entry_out outarg;
Miklos Szeredib483c932001-10-29 14:57:57 +0000817
Miklos Szeredi5e183482001-10-31 14:52:35 +0000818 res = -ENOENT;
Miklos Szeredi76f65782004-02-19 16:55:40 +0000819 path = get_path_name(f, in->ino, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000820 if (path != NULL) {
821 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000822 printf("MKDIR %s\n", path);
823 fflush(stdout);
824 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000825 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000826 if (f->op.mkdir && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000827 res = f->op.mkdir(path, inarg->mode);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000828 if (res == 0)
Miklos Szeredi76f65782004-02-19 16:55:40 +0000829 res = lookup_path(f, in->ino, in->unique, name, path, &outarg);
830 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000831 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000832 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000833 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
834 if (res == 0 && res2 == -ENOENT)
835 destroy_node(f, outarg.ino, in->unique);
Miklos Szeredib483c932001-10-29 14:57:57 +0000836}
837
Miklos Szeredib5958612004-02-20 14:10:49 +0000838static void do_unlink(struct fuse *f, struct fuse_in_header *in, char *name)
Miklos Szeredib483c932001-10-29 14:57:57 +0000839{
840 int res;
841 char *path;
842
Miklos Szeredi5e183482001-10-31 14:52:35 +0000843 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000844 path = get_path_name(f, in->ino, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000845 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +0000846 if (f->flags & FUSE_DEBUG) {
847 printf("UNLINK %s\n", path);
848 fflush(stdout);
849 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000850 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000851 if (f->op.unlink) {
Miklos Szeredi2529ca22004-07-13 15:36:52 +0000852 if (!(f->flags & FUSE_HARD_REMOVE) && is_open(f, in->ino, name))
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000853 res = hide_node(f, path, in->ino, name);
854 else {
855 res = f->op.unlink(path);
856 if (res == 0)
857 remove_node(f, in->ino, name);
858 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000859 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000860 free(path);
Miklos Szeredib483c932001-10-29 14:57:57 +0000861 }
Miklos Szeredib5958612004-02-20 14:10:49 +0000862 send_reply(f, in, res, NULL, 0);
863}
864
865static void do_rmdir(struct fuse *f, struct fuse_in_header *in, char *name)
866{
867 int res;
868 char *path;
869
870 res = -ENOENT;
871 path = get_path_name(f, in->ino, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000872 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +0000873 if (f->flags & FUSE_DEBUG) {
874 printf("RMDIR %s\n", path);
875 fflush(stdout);
876 }
Miklos Szeredib5958612004-02-20 14:10:49 +0000877 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000878 if (f->op.rmdir) {
Miklos Szeredib5958612004-02-20 14:10:49 +0000879 res = f->op.rmdir(path);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000880 if (res == 0)
Miklos Szeredib5958612004-02-20 14:10:49 +0000881 remove_node(f, in->ino, name);
882 }
883 free(path);
884 }
Miklos Szeredib483c932001-10-29 14:57:57 +0000885 send_reply(f, in, res, NULL, 0);
886}
887
888static void do_symlink(struct fuse *f, struct fuse_in_header *in, char *name,
889 char *link)
890{
891 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000892 int res2;
Miklos Szeredib483c932001-10-29 14:57:57 +0000893 char *path;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000894 struct fuse_entry_out outarg;
Miklos Szeredib483c932001-10-29 14:57:57 +0000895
Miklos Szeredi5e183482001-10-31 14:52:35 +0000896 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000897 path = get_path_name(f, in->ino, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000898 if (path != NULL) {
899 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000900 printf("SYMLINK %s\n", path);
901 fflush(stdout);
902 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000903 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000904 if (f->op.symlink && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000905 res = f->op.symlink(link, path);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000906 if (res == 0)
Miklos Szeredi76f65782004-02-19 16:55:40 +0000907 res = lookup_path(f, in->ino, in->unique, name, path, &outarg);
908 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000909 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000910 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000911 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
912 if (res == 0 && res2 == -ENOENT)
913 destroy_node(f, outarg.ino, in->unique);
914
Miklos Szeredib483c932001-10-29 14:57:57 +0000915}
916
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000917static void do_rename(struct fuse *f, struct fuse_in_header *in,
918 struct fuse_rename_in *inarg)
919{
920 int res;
921 fino_t olddir = in->ino;
922 fino_t newdir = inarg->newdir;
Miklos Szeredi6bf8b682002-10-28 08:49:39 +0000923 char *oldname = PARAM(inarg);
924 char *newname = oldname + strlen(oldname) + 1;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000925 char *oldpath;
926 char *newpath;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000927
Miklos Szeredi5e183482001-10-31 14:52:35 +0000928 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000929 oldpath = get_path_name(f, olddir, oldname);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000930 if (oldpath != NULL) {
Miklos Szeredia181e612001-11-06 12:03:23 +0000931 newpath = get_path_name(f, newdir, newname);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000932 if (newpath != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +0000933 if (f->flags & FUSE_DEBUG) {
934 printf("RENAME %s -> %s\n", oldpath, newpath);
935 fflush(stdout);
936 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000937 res = -ENOSYS;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000938 if (f->op.rename) {
939 res = 0;
Miklos Szeredi2529ca22004-07-13 15:36:52 +0000940 if (!(f->flags & FUSE_HARD_REMOVE) &&
941 is_open(f, newdir, newname))
Miklos Szeredi1ea9c962004-06-24 21:00:00 +0000942 res = hide_node(f, newpath, newdir, newname);
943 if (res == 0) {
944 res = f->op.rename(oldpath, newpath);
945 if (res == 0)
946 rename_node(f, olddir, oldname, newdir, newname, 0);
947 }
948 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000949 free(newpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000950 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000951 free(oldpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000952 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000953 send_reply(f, in, res, NULL, 0);
954}
955
956static void do_link(struct fuse *f, struct fuse_in_header *in,
Miklos Szeredi5e183482001-10-31 14:52:35 +0000957 struct fuse_link_in *arg)
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000958{
959 int res;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000960 int res2;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000961 char *oldpath;
962 char *newpath;
Miklos Szeredi76f65782004-02-19 16:55:40 +0000963 char *name = PARAM(arg);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000964 struct fuse_entry_out outarg;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000965
Miklos Szeredi5e183482001-10-31 14:52:35 +0000966 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000967 oldpath = get_path(f, in->ino);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000968 if (oldpath != NULL) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000969 newpath = get_path_name(f, arg->newdir, name);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000970 if (newpath != NULL) {
971 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000972 printf("LINK %s\n", newpath);
973 fflush(stdout);
974 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000975 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000976 if (f->op.link && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000977 res = f->op.link(oldpath, newpath);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000978 if (res == 0)
Miklos Szeredi76f65782004-02-19 16:55:40 +0000979 res = lookup_path(f, arg->newdir, in->unique, name,
980 newpath, &outarg);
981 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000982 free(newpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000983 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000984 free(oldpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000985 }
Miklos Szeredi2778f6c2004-06-21 09:45:30 +0000986 res2 = send_reply(f, in, res, &outarg, sizeof(outarg));
987 if (res == 0 && res2 == -ENOENT)
988 destroy_node(f, outarg.ino, in->unique);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000989}
990
Miklos Szeredi5e183482001-10-31 14:52:35 +0000991static void do_open(struct fuse *f, struct fuse_in_header *in,
992 struct fuse_open_in *arg)
993{
994 int res;
995 char *path;
Miklos Szeredi209f5d02004-07-24 19:56:16 +0000996 struct fuse_open_out outarg;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000997
998 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000999 path = get_path(f, in->ino);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001000 if (path != NULL) {
Miklos Szeredi5e183482001-10-31 14:52:35 +00001001 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001002 if (f->op.open)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +00001003 res = f->op.open(path, arg->flags);
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +00001004 }
Miklos Szeredi73798f92004-07-12 15:55:11 +00001005 if (res == 0) {
1006 int res2;
1007
1008 /* If the request is interrupted the lock must be held until
1009 the cancellation is finished. Otherwise there could be
1010 races with rename/unlink, against which the kernel can't
1011 protect */
1012 pthread_mutex_lock(&f->lock);
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001013 f->fh_ctr ++;
1014 outarg.fh = f->fh_ctr;
1015 if (f->flags & FUSE_DEBUG) {
1016 printf("OPEN[%u] flags: 0x%x\n", outarg.fh, arg->flags);
1017 fflush(stdout);
1018 }
1019
1020 res2 = __send_reply(f, in, res, &outarg, sizeof(outarg), 1);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001021 if(res2 == -ENOENT) {
1022 /* The open syscall was interrupted, so it must be cancelled */
1023 if(f->op.release)
1024 f->op.release(path, arg->flags);
Miklos Szeredi73798f92004-07-12 15:55:11 +00001025 } else
1026 get_node(f, in->ino)->open_count ++;
1027 pthread_mutex_unlock(&f->lock);
1028
1029 } else
1030 send_reply(f, in, res, NULL, 0);
1031
Miklos Szeredi9a31cca2004-06-26 21:11:25 +00001032 if (path)
1033 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001034}
1035
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001036static void do_flush(struct fuse *f, struct fuse_in_header *in,
1037 struct fuse_flush_in *arg)
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001038{
1039 char *path;
1040 int res;
1041
1042 res = -ENOENT;
1043 path = get_path(f, in->ino);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001044 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001045 if (f->flags & FUSE_DEBUG) {
1046 printf("FLUSH[%u]\n", arg->fh);
1047 fflush(stdout);
1048 }
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001049 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001050 if (f->op.flush)
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001051 res = f->op.flush(path);
1052 free(path);
1053 }
1054 send_reply(f, in, res, NULL, 0);
1055}
1056
Miklos Szeredi9478e862002-12-11 09:50:26 +00001057static void do_release(struct fuse *f, struct fuse_in_header *in,
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001058 struct fuse_release_in *arg)
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001059{
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001060 struct node *node;
1061 char *path;
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001062
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001063 pthread_mutex_lock(&f->lock);
1064 node = get_node(f, in->ino);
1065 --node->open_count;
1066 pthread_mutex_unlock(&f->lock);
1067
1068 path = get_path(f, in->ino);
1069 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001070 if (f->flags & FUSE_DEBUG) {
1071 printf("RELEASE[%u]\n", arg->fh);
1072 fflush(stdout);
1073 }
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001074 if (f->op.release)
Miklos Szeredib3210582004-06-23 13:54:33 +00001075 f->op.release(path, arg->flags);
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001076
1077 if(node->is_hidden && node->open_count == 0)
1078 /* can now clean up this hidden file */
1079 f->op.unlink(path);
1080
1081 free(path);
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001082 }
Miklos Szeredi556d03d2004-06-30 11:13:41 +00001083 send_reply(f, in, 0, NULL, 0);
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001084}
1085
Miklos Szeredi5e183482001-10-31 14:52:35 +00001086static void do_read(struct fuse *f, struct fuse_in_header *in,
1087 struct fuse_read_in *arg)
1088{
1089 int res;
1090 char *path;
Miklos Szeredi43696432001-11-18 19:15:05 +00001091 char *outbuf = (char *) malloc(sizeof(struct fuse_out_header) + arg->size);
1092 struct fuse_out_header *out = (struct fuse_out_header *) outbuf;
1093 char *buf = outbuf + sizeof(struct fuse_out_header);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001094 size_t size;
Miklos Szeredi43696432001-11-18 19:15:05 +00001095 size_t outsize;
Miklos Szeredi5e183482001-10-31 14:52:35 +00001096
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001097 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +00001098 path = get_path(f, in->ino);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001099 if (path != NULL) {
1100 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001101 printf("READ[%u] %u bytes from %llu\n", arg->fh, arg->size,
1102 arg->offset);
Miklos Szeredi6ebe2342002-01-06 21:44:16 +00001103 fflush(stdout);
1104 }
1105
Miklos Szeredi5e183482001-10-31 14:52:35 +00001106 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001107 if (f->op.read)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +00001108 res = f->op.read(path, buf, arg->size, arg->offset);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001109 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001110 }
1111
1112 size = 0;
Miklos Szeredi25385bb2004-07-06 22:27:36 +00001113 if (res >= 0) {
Miklos Szeredi5e183482001-10-31 14:52:35 +00001114 size = res;
1115 res = 0;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001116 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001117 printf(" READ[%u] %u bytes\n", arg->fh, size);
Miklos Szeredi6ebe2342002-01-06 21:44:16 +00001118 fflush(stdout);
1119 }
Miklos Szeredi5e183482001-10-31 14:52:35 +00001120 }
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001121 memset(out, 0, sizeof(struct fuse_out_header));
Miklos Szeredi43696432001-11-18 19:15:05 +00001122 out->unique = in->unique;
1123 out->error = res;
1124 outsize = sizeof(struct fuse_out_header) + size;
1125
Miklos Szeredi73798f92004-07-12 15:55:11 +00001126 send_reply_raw(f, outbuf, outsize, 0);
Miklos Szeredi43696432001-11-18 19:15:05 +00001127 free(outbuf);
Miklos Szeredi5e183482001-10-31 14:52:35 +00001128}
Miklos Szeredib483c932001-10-29 14:57:57 +00001129
Miklos Szeredia181e612001-11-06 12:03:23 +00001130static void do_write(struct fuse *f, struct fuse_in_header *in,
1131 struct fuse_write_in *arg)
1132{
1133 int res;
1134 char *path;
Miklos Szerediad051c32004-07-02 09:22:50 +00001135 struct fuse_write_out outarg;
Miklos Szeredia181e612001-11-06 12:03:23 +00001136
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001137 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +00001138 path = get_path(f, in->ino);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001139 if (path != NULL) {
1140 if (f->flags & FUSE_DEBUG) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001141 printf("WRITE%s[%u] %u bytes to %llu\n",
1142 arg->writepage ? "PAGE" : "", arg->fh, arg->size,
1143 arg->offset);
Miklos Szeredi6ebe2342002-01-06 21:44:16 +00001144 fflush(stdout);
1145 }
1146
Miklos Szeredia181e612001-11-06 12:03:23 +00001147 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001148 if (f->op.write)
Miklos Szeredi6bf8b682002-10-28 08:49:39 +00001149 res = f->op.write(path, PARAM(arg), arg->size, arg->offset);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001150 free(path);
Miklos Szeredia181e612001-11-06 12:03:23 +00001151 }
1152
Miklos Szerediad051c32004-07-02 09:22:50 +00001153 if (res >= 0) {
1154 outarg.size = res;
1155 res = 0;
Miklos Szeredia181e612001-11-06 12:03:23 +00001156 }
1157
Miklos Szerediad051c32004-07-02 09:22:50 +00001158 send_reply(f, in, res, &outarg, sizeof(outarg));
Miklos Szeredia181e612001-11-06 12:03:23 +00001159}
1160
Miklos Szeredi77f39942004-03-25 11:17:52 +00001161static int default_statfs(struct statfs *buf)
1162{
1163 buf->f_namelen = 255;
1164 buf->f_bsize = 512;
1165 return 0;
1166}
1167
Miklos Szeredi18e75e42004-02-19 14:23:27 +00001168static void convert_statfs(struct statfs *statfs, struct fuse_kstatfs *kstatfs)
1169{
1170 kstatfs->bsize = statfs->f_bsize;
1171 kstatfs->blocks = statfs->f_blocks;
1172 kstatfs->bfree = statfs->f_bfree;
1173 kstatfs->bavail = statfs->f_bavail;
1174 kstatfs->files = statfs->f_files;
1175 kstatfs->ffree = statfs->f_ffree;
1176 kstatfs->namelen = statfs->f_namelen;
1177}
1178
Mark Glinesd84b39a2002-01-07 16:32:02 +00001179static void do_statfs(struct fuse *f, struct fuse_in_header *in)
1180{
1181 int res;
Mark Glinesd84b39a2002-01-07 16:32:02 +00001182 struct fuse_statfs_out arg;
Miklos Szeredi18e75e42004-02-19 14:23:27 +00001183 struct statfs buf;
Mark Glinesd84b39a2002-01-07 16:32:02 +00001184
Miklos Szeredi77f39942004-03-25 11:17:52 +00001185 memset(&buf, 0, sizeof(struct statfs));
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001186 if (f->op.statfs)
Miklos Szeredi18e75e42004-02-19 14:23:27 +00001187 res = f->op.statfs("/", &buf);
Miklos Szeredi77f39942004-03-25 11:17:52 +00001188 else
1189 res = default_statfs(&buf);
1190
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001191 if (res == 0)
Miklos Szeredi77f39942004-03-25 11:17:52 +00001192 convert_statfs(&buf, &arg.st);
Miklos Szeredi4b2bef42002-01-09 12:23:27 +00001193
Mark Glinesd84b39a2002-01-07 16:32:02 +00001194 send_reply(f, in, res, &arg, sizeof(arg));
1195}
1196
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001197static void do_fsync(struct fuse *f, struct fuse_in_header *in,
1198 struct fuse_fsync_in *inarg)
1199{
1200 int res;
1201 char *path;
1202
1203 res = -ENOENT;
1204 path = get_path(f, in->ino);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001205 if (path != NULL) {
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001206 if (f->flags & FUSE_DEBUG) {
1207 printf("FSYNC[%u]\n", inarg->fh);
1208 fflush(stdout);
1209 }
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001210 res = -ENOSYS;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001211 if (f->op.fsync)
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001212 res = f->op.fsync(path, inarg->datasync);
1213 free(path);
1214 }
1215 send_reply(f, in, res, NULL, 0);
1216}
1217
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001218static void do_setxattr(struct fuse *f, struct fuse_in_header *in,
1219 struct fuse_setxattr_in *arg)
1220{
1221 int res;
1222 char *path;
1223 char *name = PARAM(arg);
1224 unsigned char *value = name + strlen(name) + 1;
1225
1226 res = -ENOENT;
1227 path = get_path(f, in->ino);
1228 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001229 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001230 if (f->op.setxattr)
1231 res = f->op.setxattr(path, name, value, arg->size, arg->flags);
1232 free(path);
1233 }
1234 send_reply(f, in, res, NULL, 0);
1235}
1236
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001237static int common_getxattr(struct fuse *f, struct fuse_in_header *in,
1238 const char *name, char *value, size_t size)
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001239{
1240 int res;
1241 char *path;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001242
1243 res = -ENOENT;
1244 path = get_path(f, in->ino);
1245 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001246 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001247 if (f->op.getxattr)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001248 res = f->op.getxattr(path, name, value, size);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001249 free(path);
1250 }
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001251 return res;
1252}
1253
1254static void do_getxattr_read(struct fuse *f, struct fuse_in_header *in,
1255 const char *name, size_t size)
1256{
1257 int res;
1258 char *outbuf = (char *) malloc(sizeof(struct fuse_out_header) + size);
1259 struct fuse_out_header *out = (struct fuse_out_header *) outbuf;
1260 char *value = outbuf + sizeof(struct fuse_out_header);
1261
1262 res = common_getxattr(f, in, name, value, size);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001263 size = 0;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001264 if (res > 0) {
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001265 size = res;
1266 res = 0;
1267 }
1268 memset(out, 0, sizeof(struct fuse_out_header));
1269 out->unique = in->unique;
1270 out->error = res;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001271
Miklos Szeredi73798f92004-07-12 15:55:11 +00001272 send_reply_raw(f, outbuf, sizeof(struct fuse_out_header) + size, 0);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001273 free(outbuf);
1274}
1275
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001276static void do_getxattr_size(struct fuse *f, struct fuse_in_header *in,
1277 const char *name)
1278{
1279 int res;
1280 struct fuse_getxattr_out arg;
1281
1282 res = common_getxattr(f, in, name, NULL, 0);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001283 if (res >= 0) {
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001284 arg.size = res;
1285 res = 0;
1286 }
1287 send_reply(f, in, res, &arg, sizeof(arg));
1288}
1289
1290static void do_getxattr(struct fuse *f, struct fuse_in_header *in,
1291 struct fuse_getxattr_in *arg)
1292{
1293 char *name = PARAM(arg);
1294
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001295 if (arg->size)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001296 do_getxattr_read(f, in, name, arg->size);
1297 else
1298 do_getxattr_size(f, in, name);
1299}
1300
1301static int common_listxattr(struct fuse *f, struct fuse_in_header *in,
1302 char *list, size_t size)
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001303{
1304 int res;
1305 char *path;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001306
1307 res = -ENOENT;
1308 path = get_path(f, in->ino);
1309 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001310 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001311 if (f->op.listxattr)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001312 res = f->op.listxattr(path, list, size);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001313 free(path);
1314 }
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001315 return res;
1316}
1317
1318static void do_listxattr_read(struct fuse *f, struct fuse_in_header *in,
1319 size_t size)
1320{
1321 int res;
1322 char *outbuf = (char *) malloc(sizeof(struct fuse_out_header) + size);
1323 struct fuse_out_header *out = (struct fuse_out_header *) outbuf;
1324 char *list = outbuf + sizeof(struct fuse_out_header);
1325
1326 res = common_listxattr(f, in, list, size);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001327 size = 0;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001328 if (res > 0) {
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001329 size = res;
1330 res = 0;
1331 }
1332 memset(out, 0, sizeof(struct fuse_out_header));
1333 out->unique = in->unique;
1334 out->error = res;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001335
Miklos Szeredi73798f92004-07-12 15:55:11 +00001336 send_reply_raw(f, outbuf, sizeof(struct fuse_out_header) + size, 0);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001337 free(outbuf);
1338}
1339
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001340static void do_listxattr_size(struct fuse *f, struct fuse_in_header *in)
1341{
1342 int res;
1343 struct fuse_getxattr_out arg;
1344
1345 res = common_listxattr(f, in, NULL, 0);
1346 if (res >= 0) {
1347 arg.size = res;
1348 res = 0;
1349 }
1350 send_reply(f, in, res, &arg, sizeof(arg));
1351}
1352
1353static void do_listxattr(struct fuse *f, struct fuse_in_header *in,
1354 struct fuse_getxattr_in *arg)
1355{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001356 if (arg->size)
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001357 do_listxattr_read(f, in, arg->size);
1358 else
1359 do_listxattr_size(f, in);
1360}
1361
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001362static void do_removexattr(struct fuse *f, struct fuse_in_header *in,
1363 char *name)
1364{
1365 int res;
1366 char *path;
1367
1368 res = -ENOENT;
1369 path = get_path(f, in->ino);
1370 if (path != NULL) {
Miklos Szeredi63b8c1c2004-06-03 14:45:04 +00001371 res = -ENOSYS;
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001372 if (f->op.removexattr)
1373 res = f->op.removexattr(path, name);
1374 free(path);
1375 }
1376 send_reply(f, in, res, NULL, 0);
1377}
1378
1379
Miklos Szeredi43696432001-11-18 19:15:05 +00001380static void free_cmd(struct fuse_cmd *cmd)
1381{
1382 free(cmd->buf);
1383 free(cmd);
1384}
1385
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001386void __fuse_process_cmd(struct fuse *f, struct fuse_cmd *cmd)
Miklos Szeredia181e612001-11-06 12:03:23 +00001387{
Miklos Szeredia181e612001-11-06 12:03:23 +00001388 struct fuse_in_header *in = (struct fuse_in_header *) cmd->buf;
1389 void *inarg = cmd->buf + sizeof(struct fuse_in_header);
1390 size_t argsize;
Miklos Szeredife25def2001-12-20 15:38:05 +00001391 struct fuse_context *ctx = fuse_get_context(f);
Miklos Szeredia181e612001-11-06 12:03:23 +00001392
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001393 dec_avail(f);
Miklos Szeredi33232032001-11-19 17:55:51 +00001394
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001395 if ((f->flags & FUSE_DEBUG)) {
Miklos Szeredic8ba2372002-12-10 12:26:00 +00001396 printf("unique: %i, opcode: %s (%i), ino: %li, insize: %i\n",
1397 in->unique, opname(in->opcode), in->opcode, in->ino,
1398 cmd->buflen);
Miklos Szeredic0938ea2001-11-07 12:35:06 +00001399 fflush(stdout);
1400 }
Miklos Szeredife25def2001-12-20 15:38:05 +00001401
1402 ctx->uid = in->uid;
1403 ctx->gid = in->gid;
Miklos Szeredia181e612001-11-06 12:03:23 +00001404
1405 argsize = cmd->buflen - sizeof(struct fuse_in_header);
1406
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001407 switch (in->opcode) {
Miklos Szeredia181e612001-11-06 12:03:23 +00001408 case FUSE_LOOKUP:
1409 do_lookup(f, in, (char *) inarg);
1410 break;
1411
Miklos Szeredia181e612001-11-06 12:03:23 +00001412 case FUSE_GETATTR:
1413 do_getattr(f, in);
1414 break;
1415
1416 case FUSE_SETATTR:
1417 do_setattr(f, in, (struct fuse_setattr_in *) inarg);
1418 break;
1419
1420 case FUSE_READLINK:
1421 do_readlink(f, in);
1422 break;
1423
1424 case FUSE_GETDIR:
1425 do_getdir(f, in);
1426 break;
1427
1428 case FUSE_MKNOD:
1429 do_mknod(f, in, (struct fuse_mknod_in *) inarg);
1430 break;
1431
1432 case FUSE_MKDIR:
1433 do_mkdir(f, in, (struct fuse_mkdir_in *) inarg);
1434 break;
1435
1436 case FUSE_UNLINK:
Miklos Szeredib5958612004-02-20 14:10:49 +00001437 do_unlink(f, in, (char *) inarg);
1438 break;
1439
Miklos Szeredia181e612001-11-06 12:03:23 +00001440 case FUSE_RMDIR:
Miklos Szeredib5958612004-02-20 14:10:49 +00001441 do_rmdir(f, in, (char *) inarg);
Miklos Szeredia181e612001-11-06 12:03:23 +00001442 break;
1443
1444 case FUSE_SYMLINK:
1445 do_symlink(f, in, (char *) inarg,
1446 ((char *) inarg) + strlen((char *) inarg) + 1);
1447 break;
1448
1449 case FUSE_RENAME:
1450 do_rename(f, in, (struct fuse_rename_in *) inarg);
1451 break;
1452
1453 case FUSE_LINK:
1454 do_link(f, in, (struct fuse_link_in *) inarg);
1455 break;
1456
1457 case FUSE_OPEN:
1458 do_open(f, in, (struct fuse_open_in *) inarg);
1459 break;
1460
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001461 case FUSE_FLUSH:
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001462 do_flush(f, in, (struct fuse_flush_in *) inarg);
Miklos Szeredie2e4ac22004-05-18 08:45:28 +00001463 break;
1464
Miklos Szeredi9478e862002-12-11 09:50:26 +00001465 case FUSE_RELEASE:
Miklos Szeredi209f5d02004-07-24 19:56:16 +00001466 do_release(f, in, (struct fuse_release_in *) inarg);
Miklos Szeredi9478e862002-12-11 09:50:26 +00001467 break;
1468
Miklos Szeredia181e612001-11-06 12:03:23 +00001469 case FUSE_READ:
1470 do_read(f, in, (struct fuse_read_in *) inarg);
1471 break;
1472
1473 case FUSE_WRITE:
1474 do_write(f, in, (struct fuse_write_in *) inarg);
1475 break;
1476
Mark Glinesd84b39a2002-01-07 16:32:02 +00001477 case FUSE_STATFS:
1478 do_statfs(f, in);
1479 break;
1480
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001481 case FUSE_FSYNC:
1482 do_fsync(f, in, (struct fuse_fsync_in *) inarg);
1483 break;
1484
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001485 case FUSE_SETXATTR:
1486 do_setxattr(f, in, (struct fuse_setxattr_in *) inarg);
1487 break;
1488
1489 case FUSE_GETXATTR:
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001490 do_getxattr(f, in, (struct fuse_getxattr_in *) inarg);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001491 break;
1492
1493 case FUSE_LISTXATTR:
Miklos Szeredi03cebae2004-03-31 10:19:18 +00001494 do_listxattr(f, in, (struct fuse_getxattr_in *) inarg);
Miklos Szeredi3ed84232004-03-30 15:17:26 +00001495 break;
1496
1497 case FUSE_REMOVEXATTR:
1498 do_removexattr(f, in, (char *) inarg);
1499 break;
1500
Miklos Szeredia181e612001-11-06 12:03:23 +00001501 default:
Miklos Szeredi43696432001-11-18 19:15:05 +00001502 send_reply(f, in, -ENOSYS, NULL, 0);
Miklos Szeredia181e612001-11-06 12:03:23 +00001503 }
Miklos Szeredi43696432001-11-18 19:15:05 +00001504
1505 free_cmd(cmd);
Miklos Szeredia181e612001-11-06 12:03:23 +00001506}
1507
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001508int __fuse_exited(struct fuse* f)
1509{
1510 return f->exited;
1511}
1512
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001513struct fuse_cmd *__fuse_read_cmd(struct fuse *f)
Miklos Szeredi2df1c042001-11-06 15:07:17 +00001514{
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001515 ssize_t res;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001516 struct fuse_cmd *cmd;
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001517 struct fuse_in_header *in;
1518 void *inarg;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001519
Miklos Szeredi43696432001-11-18 19:15:05 +00001520 cmd = (struct fuse_cmd *) malloc(sizeof(struct fuse_cmd));
1521 cmd->buf = (char *) malloc(FUSE_MAX_IN);
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001522 in = (struct fuse_in_header *) cmd->buf;
1523 inarg = cmd->buf + sizeof(struct fuse_in_header);
Miklos Szeredi43696432001-11-18 19:15:05 +00001524
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001525 res = read(f->fd, cmd->buf, FUSE_MAX_IN);
1526 if (res == -1) {
1527 free_cmd(cmd);
1528 if (__fuse_exited(f) || errno == EINTR)
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001529 return NULL;
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001530
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001531 /* ENODEV means we got unmounted, so we silenty return failure */
1532 if (errno != ENODEV) {
1533 /* BAD... This will happen again */
1534 perror("fuse: reading device");
1535 }
1536
1537 fuse_exit(f);
1538 return NULL;
1539 }
1540 if ((size_t) res < sizeof(struct fuse_in_header)) {
1541 free_cmd(cmd);
1542 /* Cannot happen */
1543 fprintf(stderr, "short read on fuse device\n");
1544 fuse_exit(f);
1545 return NULL;
1546 }
1547 cmd->buflen = res;
1548
1549 /* Forget is special, it can be done without messing with threads. */
1550 if (in->opcode == FUSE_FORGET) {
1551 do_forget(f, in, (struct fuse_forget_in *) inarg);
1552 free_cmd(cmd);
1553 return NULL;
1554 }
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001555
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001556 return cmd;
Miklos Szeredi2df1c042001-11-06 15:07:17 +00001557}
1558
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001559void fuse_loop(struct fuse *f)
1560{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001561 if (f == NULL)
Miklos Szeredic40748a2004-02-20 16:38:45 +00001562 return;
1563
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001564 while (1) {
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001565 struct fuse_cmd *cmd;
1566
Miklos Szeredi65cf7c72004-06-30 11:34:56 +00001567 if (__fuse_exited(f))
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001568 return;
1569
1570 cmd = __fuse_read_cmd(f);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001571 if (cmd == NULL)
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001572 continue;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001573
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001574 __fuse_process_cmd(f, cmd);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001575 }
1576}
1577
Miklos Szeredi891b8742004-07-29 09:27:49 +00001578int fuse_invalidate(struct fuse *f, const char *path)
1579{
1580 int res;
1581 int err;
1582 fino_t ino;
1583 struct fuse_user_header h;
1584
1585 err = path_lookup(f, path, &ino);
1586 if (err) {
1587 if (err == -ENOENT)
1588 return 0;
1589 else
1590 return err;
1591 }
1592
1593 memset(&h, 0, sizeof(struct fuse_user_header));
1594 h.opcode = FUSE_INVALIDATE;
1595 h.ino = ino;
1596
1597 if ((f->flags & FUSE_DEBUG)) {
1598 printf("INVALIDATE ino: %li\n", ino);
1599 fflush(stdout);
1600 }
1601
1602 res = write(f->fd, &h, sizeof(struct fuse_user_header));
1603 if (res == -1) {
1604 if (errno != ENOENT) {
1605 perror("fuse: writing device");
1606 return -errno;
1607 }
1608 }
1609 return 0;
1610}
1611
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001612void fuse_exit(struct fuse *f)
1613{
1614 f->exited = 1;
1615}
1616
Miklos Szeredi2e50d432001-12-20 12:17:25 +00001617struct fuse_context *fuse_get_context(struct fuse *f)
1618{
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001619 if (f->getcontext)
Miklos Szeredi2e50d432001-12-20 12:17:25 +00001620 return f->getcontext(f);
1621 else
1622 return &f->context;
1623}
1624
Miklos Szeredic40748a2004-02-20 16:38:45 +00001625static int check_version(struct fuse *f)
1626{
1627 int res;
1628 FILE *vf = fopen(FUSE_VERSION_FILE, "r");
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001629 if (vf == NULL) {
Miklos Szeredic40748a2004-02-20 16:38:45 +00001630 fprintf(stderr, "fuse: kernel interface too old, need >= %i.%i\n",
1631 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
1632 return -1;
1633 }
1634 res = fscanf(vf, "%i.%i", &f->majorver, &f->minorver);
1635 fclose(vf);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001636 if (res != 2) {
Miklos Szeredic40748a2004-02-20 16:38:45 +00001637 fprintf(stderr, "fuse: error reading %s\n", FUSE_VERSION_FILE);
1638 return -1;
1639 }
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001640 if (f->majorver != FUSE_KERNEL_VERSION) {
Miklos Szeredic40748a2004-02-20 16:38:45 +00001641 fprintf(stderr, "fuse: bad kernel interface major version: needs %i\n",
1642 FUSE_KERNEL_VERSION);
1643 return -1;
1644 }
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001645 if (f->minorver < FUSE_KERNEL_MINOR_VERSION) {
Miklos Szeredic40748a2004-02-20 16:38:45 +00001646 fprintf(stderr, "fuse: kernel interface too old: need >= %i.%i",
1647 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
1648 return -1;
1649 }
1650
1651 return 0;
1652}
1653
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001654
1655int fuse_is_lib_option(const char *opt)
1656{
1657 if (strcmp(opt, "debug") == 0 ||
1658 strcmp(opt, "hard_remove") == 0)
1659 return 1;
1660 else
1661 return 0;
1662}
1663
1664static void parse_lib_opts(struct fuse *f, const char *opts)
1665{
1666 if (opts) {
1667 char *xopts = strdup(opts);
1668 char *s = xopts;
1669 char *opt;
1670
1671 while((opt = strsep(&s, ","))) {
1672 if (strcmp(opt, "debug") == 0)
1673 f->flags |= FUSE_DEBUG;
1674 else if (strcmp(opt, "hard_remove") == 0)
1675 f->flags |= FUSE_HARD_REMOVE;
1676 else
1677 fprintf(stderr, "fuse: warning: unknown option `%s'\n", opt);
1678 }
1679 free(xopts);
1680 }
1681}
1682
1683struct fuse *fuse_new(int fd, const char *opts, const struct fuse_operations *op)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001684{
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001685 struct fuse *f;
1686 struct node *root;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001687
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001688 f = (struct fuse *) calloc(1, sizeof(struct fuse));
Miklos Szeredi2df1c042001-11-06 15:07:17 +00001689
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001690 if (check_version(f) == -1) {
Miklos Szeredic40748a2004-02-20 16:38:45 +00001691 free(f);
1692 return NULL;
1693 }
1694
Miklos Szeredibd7661b2004-07-23 17:16:29 +00001695 parse_lib_opts(f, opts);
Miklos Szeredi8cffdb92001-11-09 14:49:18 +00001696 f->fd = fd;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001697 f->ctr = 0;
Miklos Szeredi76f65782004-02-19 16:55:40 +00001698 f->generation = 0;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001699 /* FIXME: Dynamic hash table */
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001700 f->name_table_size = 14057;
1701 f->name_table = (struct node **)
1702 calloc(1, sizeof(struct node *) * f->name_table_size);
1703 f->ino_table_size = 14057;
1704 f->ino_table = (struct node **)
1705 calloc(1, sizeof(struct node *) * f->ino_table_size);
Miklos Szeredia181e612001-11-06 12:03:23 +00001706 pthread_mutex_init(&f->lock, NULL);
Miklos Szeredi33232032001-11-19 17:55:51 +00001707 f->numworker = 0;
1708 f->numavail = 0;
Miklos Szeredi2e50d432001-12-20 12:17:25 +00001709 f->op = *op;
1710 f->getcontext = NULL;
1711 f->context.uid = 0;
1712 f->context.gid = 0;
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001713 f->exited = 0;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001714
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001715 root = (struct node *) calloc(1, sizeof(struct node));
Miklos Szeredi8cffdb92001-11-09 14:49:18 +00001716 root->mode = 0;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001717 root->rdev = 0;
1718 root->name = strdup("/");
1719 root->parent = 0;
Miklos Szeredi76f65782004-02-19 16:55:40 +00001720 root->ino = FUSE_ROOT_INO;
1721 root->generation = 0;
1722 hash_ino(f, root);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001723
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001724 return f;
1725}
1726
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001727void fuse_destroy(struct fuse *f)
1728{
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001729 size_t i;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001730 for (i = 0; i < f->ino_table_size; i++) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001731 struct node *node;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001732
1733 for (node = f->ino_table[i]; node != NULL; node = node->ino_next) {
1734 if (node->is_hidden) {
1735 char *path = get_path(f, node->ino);
1736 if (path)
1737 f->op.unlink(path);
1738 }
1739 }
1740 }
1741 for (i = 0; i < f->ino_table_size; i++) {
1742 struct node *node;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001743 struct node *next;
Miklos Szeredi1ea9c962004-06-24 21:00:00 +00001744
Miklos Szeredi7eafcce2004-06-19 22:42:38 +00001745 for (node = f->ino_table[i]; node != NULL; node = next) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001746 next = node->ino_next;
1747 free_node(node);
1748 }
1749 }
1750 free(f->ino_table);
1751 free(f->name_table);
Miklos Szeredia181e612001-11-06 12:03:23 +00001752 pthread_mutex_destroy(&f->lock);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001753 free(f);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001754}