blob: 2ca30bbd7cb0748ad5315c570f2cc40b6cd66b41 [file] [log] [blame]
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001/*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001 Miklos Szeredi (mszeredi@inf.bme.hu)
4
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
9#include "fuse_i.h"
10#include <linux/fuse.h>
11
12#include <string.h>
Miklos Szeredi97c61e92001-11-07 12:09:43 +000013#include <stdlib.h>
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000014#include <unistd.h>
Miklos Szeredi97c61e92001-11-07 12:09:43 +000015#include <limits.h>
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000016#include <errno.h>
Miklos Szeredi019b4e92001-12-26 18:08:09 +000017#include <sys/param.h>
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000018
Miklos Szeredi97c61e92001-11-07 12:09:43 +000019#define FUSE_MAX_PATH 4096
Miklos Szeredi6bf8b682002-10-28 08:49:39 +000020#define PARAM(inarg) (((char *)(inarg)) + sizeof(*inarg))
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000021
Miklos Szeredi254d5ed2004-03-02 11:11:24 +000022#define ENTRY_REVALIDATE_TIME 1 /* sec */
23#define ATTR_REVALIDATE_TIME 1 /* sec */
24
Miklos Szeredic8ba2372002-12-10 12:26:00 +000025static const char *opname(enum fuse_opcode opcode)
26{
27 switch(opcode) {
28 case FUSE_LOOKUP: return "LOOKUP";
29 case FUSE_FORGET: return "FORGET";
30 case FUSE_GETATTR: return "GETATTR";
31 case FUSE_SETATTR: return "SETATTR";
32 case FUSE_READLINK: return "READLINK";
33 case FUSE_SYMLINK: return "SYMLINK";
34 case FUSE_GETDIR: return "GETDIR";
35 case FUSE_MKNOD: return "MKNOD";
36 case FUSE_MKDIR: return "MKDIR";
37 case FUSE_UNLINK: return "UNLINK";
38 case FUSE_RMDIR: return "RMDIR";
39 case FUSE_RENAME: return "RENAME";
40 case FUSE_LINK: return "LINK";
41 case FUSE_OPEN: return "OPEN";
42 case FUSE_READ: return "READ";
43 case FUSE_WRITE: return "WRITE";
44 case FUSE_STATFS: return "STATFS";
45 case FUSE_RELEASE: return "RELEASE";
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +000046 case FUSE_FSYNC: return "FSYNC";
Miklos Szeredic8ba2372002-12-10 12:26:00 +000047 default: return "???";
48 }
49}
50
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +000051static inline void inc_avail(struct fuse *f)
52{
53 pthread_mutex_lock(&f->lock);
54 f->numavail ++;
55 pthread_mutex_unlock(&f->lock);
56}
57
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
70 for(node = f->ino_table[hash]; node != NULL; node = node->ino_next)
71 if(node->ino == ino)
72 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);
80 if(node != NULL)
81 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
99 for(; *nodep != NULL; nodep = &(*nodep)->ino_next)
100 if(*nodep == node) {
101 *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++;
110 if(!f->ctr)
111 f->generation ++;
112 } 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
126 if(hash)
127 for(name += 1; *name != '\0'; name++)
128 hash = (hash << 5) - hash + *name;
129
130 return (hash + parent) % f->name_table_size;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000131}
132
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000133static struct node *lookup_node(struct fuse *f, fino_t parent,
134 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 Szeredi97c61e92001-11-07 12:09:43 +0000139 for(node = f->name_table[hash]; node != NULL; node = node->name_next)
140 if(node->parent == parent && strcmp(node->name, name) == 0)
141 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 Szeredi97c61e92001-11-07 12:09:43 +0000146static void hash_name(struct fuse *f, struct node *node, fino_t parent,
Miklos Szeredia181e612001-11-06 12:03:23 +0000147 const char *name)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000148{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000149 size_t hash = name_hash(f, parent, name);
Miklos Szeredia181e612001-11-06 12:03:23 +0000150 node->parent = parent;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000151 node->name = strdup(name);
152 node->name_next = f->name_table[hash];
153 f->name_table[hash] = node;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000154}
155
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000156static void unhash_name(struct fuse *f, struct node *node)
Miklos Szeredia181e612001-11-06 12:03:23 +0000157{
158 if(node->name != NULL) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000159 size_t hash = name_hash(f, node->parent, node->name);
160 struct node **nodep = &f->name_table[hash];
161
162 for(; *nodep != NULL; nodep = &(*nodep)->name_next)
163 if(*nodep == node) {
164 *nodep = node->name_next;
165 node->name_next = NULL;
166 free(node->name);
167 node->name = NULL;
168 node->parent = 0;
169 return;
170 }
171 fprintf(stderr, "fuse internal error: unable to unhash node: %lu\n",
172 node->ino);
173 abort();
Miklos Szeredia181e612001-11-06 12:03:23 +0000174 }
175}
176
Miklos Szeredi76f65782004-02-19 16:55:40 +0000177static struct node *find_node(struct fuse *f, fino_t parent, char *name,
178 struct fuse_attr *attr, int version)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000179{
180 struct node *node;
Miklos Szeredia181e612001-11-06 12:03:23 +0000181 int mode = attr->mode & S_IFMT;
182 int rdev = 0;
183
184 if(S_ISCHR(mode) || S_ISBLK(mode))
185 rdev = attr->rdev;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000186
Miklos Szeredia181e612001-11-06 12:03:23 +0000187 pthread_mutex_lock(&f->lock);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000188 node = lookup_node(f, parent, name);
189 if(node != NULL) {
Miklos Szeredia181e612001-11-06 12:03:23 +0000190 if(node->mode == mode && node->rdev == rdev)
191 goto out;
192
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000193 unhash_name(f, node);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000194 }
195
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000196 node = (struct node *) calloc(1, sizeof(struct node));
Miklos Szeredia181e612001-11-06 12:03:23 +0000197 node->mode = mode;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000198 node->rdev = rdev;
Miklos Szeredi76f65782004-02-19 16:55:40 +0000199 node->ino = next_ino(f);
200 node->generation = f->generation;
201 hash_ino(f, node);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000202 hash_name(f, node, parent, name);
Miklos Szeredia181e612001-11-06 12:03:23 +0000203
204 out:
205 node->version = version;
206 pthread_mutex_unlock(&f->lock);
Miklos Szeredi76f65782004-02-19 16:55:40 +0000207 return node;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000208}
209
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000210static char *add_name(char *buf, char *s, const char *name)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000211{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000212 size_t len = strlen(name);
213 s -= len;
214 if(s <= buf) {
215 fprintf(stderr, "fuse: path too long: ...%s\n", s + len);
216 return NULL;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000217 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000218 strncpy(s, name, len);
219 s--;
220 *s = '/';
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000221
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000222 return s;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000223}
224
Miklos Szeredia181e612001-11-06 12:03:23 +0000225static char *get_path_name(struct fuse *f, fino_t ino, const char *name)
Miklos Szeredib483c932001-10-29 14:57:57 +0000226{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000227 char buf[FUSE_MAX_PATH];
228 char *s = buf + FUSE_MAX_PATH - 1;
229 struct node *node;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000230
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000231 *s = '\0';
Miklos Szeredia181e612001-11-06 12:03:23 +0000232
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000233 if(name != NULL) {
234 s = add_name(buf, s, name);
235 if(s == NULL)
236 return NULL;
237 }
238
239 pthread_mutex_lock(&f->lock);
240 for(node = get_node(f, ino); node->ino != FUSE_ROOT_INO;
241 node = get_node(f, node->parent)) {
242 if(node->name == NULL) {
243 s = NULL;
244 break;
245 }
246
247 s = add_name(buf, s, node->name);
248 if(s == NULL)
249 break;
250 }
251 pthread_mutex_unlock(&f->lock);
252
253 if(s == NULL)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000254 return NULL;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000255 else if(*s == '\0')
256 return strdup("/");
257 else
258 return strdup(s);
259}
Miklos Szeredia181e612001-11-06 12:03:23 +0000260
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000261static char *get_path(struct fuse *f, fino_t ino)
262{
263 return get_path_name(f, ino, NULL);
Miklos Szeredib483c932001-10-29 14:57:57 +0000264}
265
Miklos Szeredia181e612001-11-06 12:03:23 +0000266static void destroy_node(struct fuse *f, fino_t ino, int version)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000267{
Miklos Szeredia181e612001-11-06 12:03:23 +0000268 struct node *node;
269
270 pthread_mutex_lock(&f->lock);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000271 node = get_node(f, ino);
Miklos Szeredi39f28672001-11-14 14:52:54 +0000272 if(node->version == version && ino != FUSE_ROOT_INO) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000273 unhash_name(f, node);
274 unhash_ino(f, node);
Miklos Szeredia181e612001-11-06 12:03:23 +0000275 free_node(node);
276 }
277 pthread_mutex_unlock(&f->lock);
278
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000279}
280
Miklos Szeredi5e183482001-10-31 14:52:35 +0000281static void remove_node(struct fuse *f, fino_t dir, const char *name)
282{
Miklos Szeredia181e612001-11-06 12:03:23 +0000283 struct node *node;
284
285 pthread_mutex_lock(&f->lock);
286 node = lookup_node(f, dir, name);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000287 if(node == NULL) {
288 fprintf(stderr, "fuse internal error: unable to remove node %lu/%s\n",
289 dir, name);
290 abort();
291 }
292 unhash_name(f, node);
Miklos Szeredia181e612001-11-06 12:03:23 +0000293 pthread_mutex_unlock(&f->lock);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000294}
295
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000296static void rename_node(struct fuse *f, fino_t olddir, const char *oldname,
297 fino_t newdir, const char *newname)
298{
Miklos Szeredia181e612001-11-06 12:03:23 +0000299 struct node *node;
300 struct node *newnode;
301
302 pthread_mutex_lock(&f->lock);
303 node = lookup_node(f, olddir, oldname);
304 newnode = lookup_node(f, newdir, newname);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000305 if(node == NULL) {
306 fprintf(stderr, "fuse internal error: unable to rename node %lu/%s\n",
307 olddir, oldname);
308 abort();
309 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000310
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000311 if(newnode != NULL)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000312 unhash_name(f, newnode);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000313
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000314 unhash_name(f, node);
315 hash_name(f, node, newdir, newname);
Miklos Szeredia181e612001-11-06 12:03:23 +0000316 pthread_mutex_unlock(&f->lock);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000317}
318
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000319static void convert_stat(struct stat *stbuf, struct fuse_attr *attr)
320{
Miklos Szeredib5958612004-02-20 14:10:49 +0000321 attr->mode = stbuf->st_mode;
322 attr->nlink = stbuf->st_nlink;
323 attr->uid = stbuf->st_uid;
324 attr->gid = stbuf->st_gid;
325 attr->rdev = stbuf->st_rdev;
326 attr->size = stbuf->st_size;
327 attr->blocks = stbuf->st_blocks;
328 attr->atime = stbuf->st_atime;
329 attr->atimensec = stbuf->st_atim.tv_nsec;
330 attr->mtime = stbuf->st_mtime;
331 attr->mtimensec = stbuf->st_mtim.tv_nsec;
332 attr->ctime = stbuf->st_ctime;
333 attr->ctimensec = stbuf->st_ctim.tv_nsec;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000334}
335
Miklos Szeredia181e612001-11-06 12:03:23 +0000336static int fill_dir(struct fuse_dirhandle *dh, char *name, int type)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000337{
338 struct fuse_dirent dirent;
339 size_t reclen;
340 size_t res;
341
Miklos Szeredi43696432001-11-18 19:15:05 +0000342 dirent.ino = (unsigned long) -1;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000343 dirent.namelen = strlen(name);
344 strncpy(dirent.name, name, sizeof(dirent.name));
345 dirent.type = type;
346 reclen = FUSE_DIRENT_SIZE(&dirent);
347 res = fwrite(&dirent, reclen, 1, dh->fp);
348 if(res == 0) {
Miklos Szeredi96249982001-11-21 12:21:19 +0000349 perror("fuse: writing directory file");
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000350 return -EIO;
351 }
352 return 0;
353}
354
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000355static int send_reply_raw(struct fuse *f, char *outbuf, size_t outsize)
Miklos Szeredi43696432001-11-18 19:15:05 +0000356{
357 int res;
358
359 if((f->flags & FUSE_DEBUG)) {
360 struct fuse_out_header *out = (struct fuse_out_header *) outbuf;
361 printf(" unique: %i, error: %i (%s), outsize: %i\n", out->unique,
362 out->error, strerror(-out->error), outsize);
363 fflush(stdout);
364 }
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000365
Miklos Szeredi4e71c9f2004-02-09 12:05:14 +0000366 /* This needs to be done before the reply, otherwise the scheduler
367 could play tricks with us, and only let the counter be increased
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000368 long after the operation is done */
369 inc_avail(f);
370
Miklos Szeredi43696432001-11-18 19:15:05 +0000371 res = write(f->fd, outbuf, outsize);
372 if(res == -1) {
373 /* ENOENT means the operation was interrupted */
Miklos Szeredi4e71c9f2004-02-09 12:05:14 +0000374 if(!f->exited && errno != ENOENT)
Miklos Szeredi96249982001-11-21 12:21:19 +0000375 perror("fuse: writing device");
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000376 return -errno;
Miklos Szeredi43696432001-11-18 19:15:05 +0000377 }
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000378 return 0;
Miklos Szeredi43696432001-11-18 19:15:05 +0000379}
380
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000381static int send_reply(struct fuse *f, struct fuse_in_header *in, int error,
382 void *arg, size_t argsize)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000383{
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000384 int res;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000385 char *outbuf;
386 size_t outsize;
387 struct fuse_out_header *out;
388
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000389 if(error <= -512 || error > 0) {
390 fprintf(stderr, "fuse: bad error value: %i\n", error);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000391 error = -ERANGE;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000392 }
393
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000394 if(error)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000395 argsize = 0;
396
397 outsize = sizeof(struct fuse_out_header) + argsize;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000398 outbuf = (char *) malloc(outsize);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000399 out = (struct fuse_out_header *) outbuf;
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000400 memset(out, 0, sizeof(struct fuse_out_header));
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000401 out->unique = in->unique;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000402 out->error = error;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000403 if(argsize != 0)
404 memcpy(outbuf + sizeof(struct fuse_out_header), arg, argsize);
405
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000406 res = send_reply_raw(f, outbuf, outsize);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000407 free(outbuf);
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000408
409 return res;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000410}
411
Miklos Szeredi76f65782004-02-19 16:55:40 +0000412static int lookup_path(struct fuse *f, fino_t ino, int version, char *name,
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000413 const char *path, struct fuse_entry_out *arg)
Miklos Szeredi76f65782004-02-19 16:55:40 +0000414{
415 int res;
416 struct stat buf;
417
418 res = f->op.getattr(path, &buf);
419 if(res == 0) {
420 struct node *node;
421
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000422 memset(arg, 0, sizeof(struct fuse_entry_out));
Miklos Szeredi76f65782004-02-19 16:55:40 +0000423 convert_stat(&buf, &arg->attr);
424 node = find_node(f, ino, name, &arg->attr, version);
425 arg->ino = node->ino;
426 arg->generation = node->generation;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000427 arg->entry_valid = ENTRY_REVALIDATE_TIME;
428 arg->entry_valid_nsec = 0;
429 arg->attr_valid = ATTR_REVALIDATE_TIME;
430 arg->attr_valid_nsec = 0;
Miklos Szeredi76f65782004-02-19 16:55:40 +0000431 if(f->flags & FUSE_DEBUG) {
432 printf(" INO: %li\n", arg->ino);
433 fflush(stdout);
434 }
435 }
436 return res;
437}
438
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000439static void do_lookup(struct fuse *f, struct fuse_in_header *in, char *name)
440{
441 int res;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000442 char *path;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000443 struct fuse_entry_out arg;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000444
Miklos Szeredi5e183482001-10-31 14:52:35 +0000445 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000446 path = get_path_name(f, in->ino, name);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000447 if(path != NULL) {
Miklos Szeredi6ebe2342002-01-06 21:44:16 +0000448 if(f->flags & FUSE_DEBUG) {
449 printf("LOOKUP %s\n", path);
450 fflush(stdout);
451 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000452 res = -ENOSYS;
453 if(f->op.getattr)
Miklos Szeredi76f65782004-02-19 16:55:40 +0000454 res = lookup_path(f, in->ino, in->unique, name, path, &arg);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000455 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000456 }
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000457 send_reply(f, in, res, &arg, sizeof(arg));
458}
459
Miklos Szeredia181e612001-11-06 12:03:23 +0000460static void do_forget(struct fuse *f, struct fuse_in_header *in,
461 struct fuse_forget_in *arg)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000462{
Miklos Szeredi43696432001-11-18 19:15:05 +0000463 if(f->flags & FUSE_DEBUG) {
464 printf("FORGET %li/%i\n", in->ino, arg->version);
465 fflush(stdout);
466 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000467 destroy_node(f, in->ino, arg->version);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000468}
469
470static void do_getattr(struct fuse *f, struct fuse_in_header *in)
471{
472 int res;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000473 char *path;
474 struct stat buf;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000475 struct fuse_attr_out arg;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000476
Miklos Szeredi5e183482001-10-31 14:52:35 +0000477 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000478 path = get_path(f, in->ino);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000479 if(path != NULL) {
480 res = -ENOSYS;
481 if(f->op.getattr)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000482 res = f->op.getattr(path, &buf);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000483 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000484 }
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000485
486 if(res == 0) {
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000487 memset(&arg, 0, sizeof(struct fuse_attr_out));
488 arg.attr_valid = ATTR_REVALIDATE_TIME;
489 arg.attr_valid_nsec = 0;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000490 convert_stat(&buf, &arg.attr);
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000491 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000492
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000493 send_reply(f, in, res, &arg, sizeof(arg));
494}
495
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000496static int do_chmod(struct fuse *f, const char *path, struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000497{
498 int res;
499
500 res = -ENOSYS;
501 if(f->op.chmod)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000502 res = f->op.chmod(path, attr->mode);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000503
504 return res;
505}
506
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000507static int do_chown(struct fuse *f, const char *path, struct fuse_attr *attr,
Miklos Szeredi2e50d432001-12-20 12:17:25 +0000508 int valid)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000509{
510 int res;
511 uid_t uid = (valid & FATTR_UID) ? attr->uid : (uid_t) -1;
512 gid_t gid = (valid & FATTR_GID) ? attr->gid : (gid_t) -1;
513
514 res = -ENOSYS;
515 if(f->op.chown)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000516 res = f->op.chown(path, uid, gid);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000517
518 return res;
519}
520
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000521static int do_truncate(struct fuse *f, const char *path,
522 struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000523{
524 int res;
525
526 res = -ENOSYS;
527 if(f->op.truncate)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000528 res = f->op.truncate(path, attr->size);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000529
530 return res;
531}
532
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000533static int do_utime(struct fuse *f, const char *path, struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000534{
535 int res;
536 struct utimbuf buf;
537 buf.actime = attr->atime;
538 buf.modtime = attr->mtime;
539 res = -ENOSYS;
540 if(f->op.utime)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000541 res = f->op.utime(path, &buf);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000542
543 return res;
544}
545
Miklos Szeredi5e183482001-10-31 14:52:35 +0000546static void do_setattr(struct fuse *f, struct fuse_in_header *in,
547 struct fuse_setattr_in *arg)
548{
549 int res;
550 char *path;
551 int valid = arg->valid;
552 struct fuse_attr *attr = &arg->attr;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000553 struct fuse_attr_out outarg;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000554
555 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000556 path = get_path(f, in->ino);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000557 if(path != NULL) {
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000558 res = -ENOSYS;
559 if(f->op.getattr) {
560 res = 0;
561 if(!res && (valid & FATTR_MODE))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000562 res = do_chmod(f, path, attr);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000563 if(!res && (valid & (FATTR_UID | FATTR_GID)))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000564 res = do_chown(f, path, attr, valid);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000565 if(!res && (valid & FATTR_SIZE))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000566 res = do_truncate(f, path, attr);
Miklos Szeredib5958612004-02-20 14:10:49 +0000567 if(!res && (valid & (FATTR_ATIME | FATTR_MTIME)) ==
568 (FATTR_ATIME | FATTR_MTIME))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000569 res = do_utime(f, path, attr);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000570 if(!res) {
571 struct stat buf;
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000572 res = f->op.getattr(path, &buf);
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000573 if(!res) {
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000574 memset(&outarg, 0, sizeof(struct fuse_attr_out));
575 outarg.attr_valid = ATTR_REVALIDATE_TIME;
576 outarg.attr_valid_nsec = 0;
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000577 convert_stat(&buf, &outarg.attr);
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000578 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000579 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000580 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000581 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000582 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000583 send_reply(f, in, res, &outarg, sizeof(outarg));
Miklos Szeredi5e183482001-10-31 14:52:35 +0000584}
585
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000586static void do_readlink(struct fuse *f, struct fuse_in_header *in)
587{
588 int res;
589 char link[PATH_MAX + 1];
Miklos Szeredib483c932001-10-29 14:57:57 +0000590 char *path;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000591
Miklos Szeredi5e183482001-10-31 14:52:35 +0000592 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000593 path = get_path(f, in->ino);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000594 if(path != NULL) {
595 res = -ENOSYS;
596 if(f->op.readlink)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000597 res = f->op.readlink(path, link, sizeof(link));
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000598 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000599 }
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000600 link[PATH_MAX] = '\0';
Miklos Szeredi4b2bef42002-01-09 12:23:27 +0000601 send_reply(f, in, res, link, res == 0 ? strlen(link) : 0);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000602}
603
604static void do_getdir(struct fuse *f, struct fuse_in_header *in)
605{
606 int res;
607 struct fuse_getdir_out arg;
Miklos Szeredia181e612001-11-06 12:03:23 +0000608 struct fuse_dirhandle dh;
Miklos Szeredib483c932001-10-29 14:57:57 +0000609 char *path;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000610
Miklos Szeredib483c932001-10-29 14:57:57 +0000611 dh.fuse = f;
612 dh.fp = tmpfile();
613 dh.dir = in->ino;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000614 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000615 path = get_path(f, in->ino);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000616 if(path != NULL) {
617 res = -ENOSYS;
618 if(f->op.getdir)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000619 res = f->op.getdir(path, &dh, (fuse_dirfil_t) fill_dir);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000620 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000621 }
Miklos Szeredib483c932001-10-29 14:57:57 +0000622 fflush(dh.fp);
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000623
624 memset(&arg, 0, sizeof(struct fuse_getdir_out));
Miklos Szeredib483c932001-10-29 14:57:57 +0000625 arg.fd = fileno(dh.fp);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000626 send_reply(f, in, res, &arg, sizeof(arg));
Miklos Szeredib483c932001-10-29 14:57:57 +0000627 fclose(dh.fp);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000628}
629
Miklos Szeredib483c932001-10-29 14:57:57 +0000630static void do_mknod(struct fuse *f, struct fuse_in_header *in,
631 struct fuse_mknod_in *inarg)
632{
633 int res;
634 char *path;
Miklos Szeredi76f65782004-02-19 16:55:40 +0000635 char *name = PARAM(inarg);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000636 struct fuse_entry_out outarg;
Miklos Szeredib483c932001-10-29 14:57:57 +0000637
Miklos Szeredi5e183482001-10-31 14:52:35 +0000638 res = -ENOENT;
Miklos Szeredi76f65782004-02-19 16:55:40 +0000639 path = get_path_name(f, in->ino, name);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000640 if(path != NULL) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000641 if(f->flags & FUSE_DEBUG) {
642 printf("MKNOD %s\n", path);
643 fflush(stdout);
644 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000645 res = -ENOSYS;
646 if(f->op.mknod && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000647 res = f->op.mknod(path, inarg->mode, inarg->rdev);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000648 if(res == 0)
Miklos Szeredi76f65782004-02-19 16:55:40 +0000649 res = lookup_path(f, in->ino, in->unique, name, path, &outarg);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000650 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000651 free(path);
Miklos Szeredib483c932001-10-29 14:57:57 +0000652 }
Miklos Szeredib483c932001-10-29 14:57:57 +0000653 send_reply(f, in, res, &outarg, sizeof(outarg));
654}
655
656static void do_mkdir(struct fuse *f, struct fuse_in_header *in,
657 struct fuse_mkdir_in *inarg)
658{
659 int res;
660 char *path;
Miklos Szeredi76f65782004-02-19 16:55:40 +0000661 char *name = PARAM(inarg);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000662 struct fuse_entry_out outarg;
Miklos Szeredib483c932001-10-29 14:57:57 +0000663
Miklos Szeredi5e183482001-10-31 14:52:35 +0000664 res = -ENOENT;
Miklos Szeredi76f65782004-02-19 16:55:40 +0000665 path = get_path_name(f, in->ino, name);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000666 if(path != NULL) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000667 if(f->flags & FUSE_DEBUG) {
668 printf("MKDIR %s\n", path);
669 fflush(stdout);
670 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000671 res = -ENOSYS;
Miklos Szeredi76f65782004-02-19 16:55:40 +0000672 if(f->op.mkdir && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000673 res = f->op.mkdir(path, inarg->mode);
Miklos Szeredi76f65782004-02-19 16:55:40 +0000674 if(res == 0)
675 res = lookup_path(f, in->ino, in->unique, name, path, &outarg);
676 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000677 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000678 }
Miklos Szeredi76f65782004-02-19 16:55:40 +0000679 send_reply(f, in, res, &outarg, sizeof(outarg));
Miklos Szeredib483c932001-10-29 14:57:57 +0000680}
681
Miklos Szeredib5958612004-02-20 14:10:49 +0000682static void do_unlink(struct fuse *f, struct fuse_in_header *in, char *name)
Miklos Szeredib483c932001-10-29 14:57:57 +0000683{
684 int res;
685 char *path;
686
Miklos Szeredi5e183482001-10-31 14:52:35 +0000687 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000688 path = get_path_name(f, in->ino, name);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000689 if(path != NULL) {
690 res = -ENOSYS;
Miklos Szeredib5958612004-02-20 14:10:49 +0000691 if(f->op.unlink) {
692 res = f->op.unlink(path);
693 if(res == 0)
694 remove_node(f, in->ino, name);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000695 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000696 free(path);
Miklos Szeredib483c932001-10-29 14:57:57 +0000697 }
Miklos Szeredib5958612004-02-20 14:10:49 +0000698 send_reply(f, in, res, NULL, 0);
699}
700
701static void do_rmdir(struct fuse *f, struct fuse_in_header *in, char *name)
702{
703 int res;
704 char *path;
705
706 res = -ENOENT;
707 path = get_path_name(f, in->ino, name);
708 if(path != NULL) {
709 res = -ENOSYS;
710 if(f->op.rmdir) {
711 res = f->op.rmdir(path);
712 if(res == 0)
713 remove_node(f, in->ino, name);
714 }
715 free(path);
716 }
Miklos Szeredib483c932001-10-29 14:57:57 +0000717 send_reply(f, in, res, NULL, 0);
718}
719
720static void do_symlink(struct fuse *f, struct fuse_in_header *in, char *name,
721 char *link)
722{
723 int res;
724 char *path;
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000725 struct fuse_entry_out outarg;
Miklos Szeredib483c932001-10-29 14:57:57 +0000726
Miklos Szeredi5e183482001-10-31 14:52:35 +0000727 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000728 path = get_path_name(f, in->ino, name);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000729 if(path != NULL) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000730 if(f->flags & FUSE_DEBUG) {
731 printf("SYMLINK %s\n", path);
732 fflush(stdout);
733 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000734 res = -ENOSYS;
Miklos Szeredi76f65782004-02-19 16:55:40 +0000735 if(f->op.symlink && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000736 res = f->op.symlink(link, path);
Miklos Szeredi76f65782004-02-19 16:55:40 +0000737 if(res == 0)
738 res = lookup_path(f, in->ino, in->unique, name, path, &outarg);
739 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000740 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000741 }
Miklos Szeredi76f65782004-02-19 16:55:40 +0000742 send_reply(f, in, res, &outarg, sizeof(outarg));
Miklos Szeredib483c932001-10-29 14:57:57 +0000743}
744
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000745static void do_rename(struct fuse *f, struct fuse_in_header *in,
746 struct fuse_rename_in *inarg)
747{
748 int res;
749 fino_t olddir = in->ino;
750 fino_t newdir = inarg->newdir;
Miklos Szeredi6bf8b682002-10-28 08:49:39 +0000751 char *oldname = PARAM(inarg);
752 char *newname = oldname + strlen(oldname) + 1;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000753 char *oldpath;
754 char *newpath;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000755
Miklos Szeredi5e183482001-10-31 14:52:35 +0000756 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000757 oldpath = get_path_name(f, olddir, oldname);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000758 if(oldpath != NULL) {
Miklos Szeredia181e612001-11-06 12:03:23 +0000759 newpath = get_path_name(f, newdir, newname);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000760 if(newpath != NULL) {
761 res = -ENOSYS;
762 if(f->op.rename)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000763 res = f->op.rename(oldpath, newpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000764 if(res == 0)
765 rename_node(f, olddir, oldname, newdir, newname);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000766 free(newpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000767 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000768 free(oldpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000769 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000770 send_reply(f, in, res, NULL, 0);
771}
772
773static void do_link(struct fuse *f, struct fuse_in_header *in,
Miklos Szeredi5e183482001-10-31 14:52:35 +0000774 struct fuse_link_in *arg)
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000775{
776 int res;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000777 char *oldpath;
778 char *newpath;
Miklos Szeredi76f65782004-02-19 16:55:40 +0000779 char *name = PARAM(arg);
Miklos Szeredi254d5ed2004-03-02 11:11:24 +0000780 struct fuse_entry_out outarg;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000781
Miklos Szeredi5e183482001-10-31 14:52:35 +0000782 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000783 oldpath = get_path(f, in->ino);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000784 if(oldpath != NULL) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000785 newpath = get_path_name(f, arg->newdir, name);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000786 if(newpath != NULL) {
Miklos Szeredi76f65782004-02-19 16:55:40 +0000787 if(f->flags & FUSE_DEBUG) {
788 printf("LINK %s\n", newpath);
789 fflush(stdout);
790 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000791 res = -ENOSYS;
Miklos Szeredi76f65782004-02-19 16:55:40 +0000792 if(f->op.link && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000793 res = f->op.link(oldpath, newpath);
Miklos Szeredi76f65782004-02-19 16:55:40 +0000794 if(res == 0)
795 res = lookup_path(f, arg->newdir, in->unique, name,
796 newpath, &outarg);
797 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000798 free(newpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000799 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000800 free(oldpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000801 }
Miklos Szeredi76f65782004-02-19 16:55:40 +0000802 send_reply(f, in, res, &outarg, sizeof(outarg));
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000803}
804
Miklos Szeredi5e183482001-10-31 14:52:35 +0000805static void do_open(struct fuse *f, struct fuse_in_header *in,
806 struct fuse_open_in *arg)
807{
808 int res;
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000809 int res2;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000810 char *path;
811
812 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000813 path = get_path(f, in->ino);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000814 if(path != NULL) {
815 res = -ENOSYS;
816 if(f->op.open)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000817 res = f->op.open(path, arg->flags);
Miklos Szeredi40b9a5a2002-12-10 16:09:02 +0000818 }
819 res2 = send_reply(f, in, res, NULL, 0);
820 if(path != NULL) {
821 /* The open syscall was interrupted, so it must be cancelled */
822 if(res == 0 && res2 == -ENOENT && f->op.release)
Miklos Szeredi9478e862002-12-11 09:50:26 +0000823 f->op.release(path, arg->flags);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000824 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000825 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000826}
827
Miklos Szeredi9478e862002-12-11 09:50:26 +0000828static void do_release(struct fuse *f, struct fuse_in_header *in,
829 struct fuse_open_in *arg)
Miklos Szeredic8ba2372002-12-10 12:26:00 +0000830{
Miklos Szeredic8ba2372002-12-10 12:26:00 +0000831 char *path;
832
Miklos Szeredic8ba2372002-12-10 12:26:00 +0000833 path = get_path(f, in->ino);
834 if(path != NULL) {
Miklos Szeredic8ba2372002-12-10 12:26:00 +0000835 if(f->op.release)
Miklos Szeredi9478e862002-12-11 09:50:26 +0000836 f->op.release(path, arg->flags);
Miklos Szeredic8ba2372002-12-10 12:26:00 +0000837 free(path);
838 }
Miklos Szeredic8ba2372002-12-10 12:26:00 +0000839}
840
Miklos Szeredi5e183482001-10-31 14:52:35 +0000841static void do_read(struct fuse *f, struct fuse_in_header *in,
842 struct fuse_read_in *arg)
843{
844 int res;
845 char *path;
Miklos Szeredi43696432001-11-18 19:15:05 +0000846 char *outbuf = (char *) malloc(sizeof(struct fuse_out_header) + arg->size);
847 struct fuse_out_header *out = (struct fuse_out_header *) outbuf;
848 char *buf = outbuf + sizeof(struct fuse_out_header);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000849 size_t size;
Miklos Szeredi43696432001-11-18 19:15:05 +0000850 size_t outsize;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000851
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000852 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000853 path = get_path(f, in->ino);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000854 if(path != NULL) {
Miklos Szeredi6ebe2342002-01-06 21:44:16 +0000855 if(f->flags & FUSE_DEBUG) {
856 printf("READ %u bytes from %llu\n", arg->size, arg->offset);
857 fflush(stdout);
858 }
859
Miklos Szeredi5e183482001-10-31 14:52:35 +0000860 res = -ENOSYS;
Miklos Szeredia181e612001-11-06 12:03:23 +0000861 if(f->op.read)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000862 res = f->op.read(path, buf, arg->size, arg->offset);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000863 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000864 }
865
866 size = 0;
867 if(res > 0) {
868 size = res;
869 res = 0;
Miklos Szeredi6ebe2342002-01-06 21:44:16 +0000870 if(f->flags & FUSE_DEBUG) {
871 printf(" READ %u bytes\n", size);
872 fflush(stdout);
873 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000874 }
Miklos Szeredi0f48a262002-12-05 14:23:01 +0000875 memset(out, 0, sizeof(struct fuse_out_header));
Miklos Szeredi43696432001-11-18 19:15:05 +0000876 out->unique = in->unique;
877 out->error = res;
878 outsize = sizeof(struct fuse_out_header) + size;
879
880 send_reply_raw(f, outbuf, outsize);
881 free(outbuf);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000882}
Miklos Szeredib483c932001-10-29 14:57:57 +0000883
Miklos Szeredia181e612001-11-06 12:03:23 +0000884static void do_write(struct fuse *f, struct fuse_in_header *in,
885 struct fuse_write_in *arg)
886{
887 int res;
888 char *path;
Miklos Szeredia181e612001-11-06 12:03:23 +0000889
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000890 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000891 path = get_path(f, in->ino);
892 if(path != NULL) {
Miklos Szeredi6ebe2342002-01-06 21:44:16 +0000893 if(f->flags & FUSE_DEBUG) {
894 printf("WRITE %u bytes to %llu\n", arg->size, arg->offset);
895 fflush(stdout);
896 }
897
Miklos Szeredia181e612001-11-06 12:03:23 +0000898 res = -ENOSYS;
899 if(f->op.write)
Miklos Szeredi6bf8b682002-10-28 08:49:39 +0000900 res = f->op.write(path, PARAM(arg), arg->size, arg->offset);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000901 free(path);
Miklos Szeredia181e612001-11-06 12:03:23 +0000902 }
903
904 if(res > 0) {
905 if((size_t) res != arg->size) {
906 fprintf(stderr, "short write: %u (should be %u)\n", res,
907 arg->size);
Miklos Szeredi0e535082003-10-13 10:08:06 +0000908 res = -EINVAL;
Miklos Szeredia181e612001-11-06 12:03:23 +0000909 }
910 else
911 res = 0;
912 }
913
914 send_reply(f, in, res, NULL, 0);
915}
916
Miklos Szeredi77f39942004-03-25 11:17:52 +0000917static int default_statfs(struct statfs *buf)
918{
919 buf->f_namelen = 255;
920 buf->f_bsize = 512;
921 return 0;
922}
923
Miklos Szeredi18e75e42004-02-19 14:23:27 +0000924static void convert_statfs(struct statfs *statfs, struct fuse_kstatfs *kstatfs)
925{
926 kstatfs->bsize = statfs->f_bsize;
927 kstatfs->blocks = statfs->f_blocks;
928 kstatfs->bfree = statfs->f_bfree;
929 kstatfs->bavail = statfs->f_bavail;
930 kstatfs->files = statfs->f_files;
931 kstatfs->ffree = statfs->f_ffree;
932 kstatfs->namelen = statfs->f_namelen;
933}
934
Mark Glinesd84b39a2002-01-07 16:32:02 +0000935static void do_statfs(struct fuse *f, struct fuse_in_header *in)
936{
937 int res;
Mark Glinesd84b39a2002-01-07 16:32:02 +0000938 struct fuse_statfs_out arg;
Miklos Szeredi18e75e42004-02-19 14:23:27 +0000939 struct statfs buf;
Mark Glinesd84b39a2002-01-07 16:32:02 +0000940
Miklos Szeredi77f39942004-03-25 11:17:52 +0000941 memset(&buf, 0, sizeof(struct statfs));
942 if(f->op.statfs)
Miklos Szeredi18e75e42004-02-19 14:23:27 +0000943 res = f->op.statfs("/", &buf);
Miklos Szeredi77f39942004-03-25 11:17:52 +0000944 else
945 res = default_statfs(&buf);
946
947 if(res == 0)
948 convert_statfs(&buf, &arg.st);
Miklos Szeredi4b2bef42002-01-09 12:23:27 +0000949
Mark Glinesd84b39a2002-01-07 16:32:02 +0000950 send_reply(f, in, res, &arg, sizeof(arg));
951}
952
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +0000953static void do_fsync(struct fuse *f, struct fuse_in_header *in,
954 struct fuse_fsync_in *inarg)
955{
956 int res;
957 char *path;
958
959 res = -ENOENT;
960 path = get_path(f, in->ino);
961 if(path != NULL) {
962 /* fsync is not mandatory, so don't return ENOSYS */
963 res = 0;
964 if(f->op.fsync)
965 res = f->op.fsync(path, inarg->datasync);
966 free(path);
967 }
968 send_reply(f, in, res, NULL, 0);
969}
970
Miklos Szeredi43696432001-11-18 19:15:05 +0000971static void free_cmd(struct fuse_cmd *cmd)
972{
973 free(cmd->buf);
974 free(cmd);
975}
976
Miklos Szeredifff56ab2001-11-16 10:12:59 +0000977void __fuse_process_cmd(struct fuse *f, struct fuse_cmd *cmd)
Miklos Szeredia181e612001-11-06 12:03:23 +0000978{
Miklos Szeredia181e612001-11-06 12:03:23 +0000979 struct fuse_in_header *in = (struct fuse_in_header *) cmd->buf;
980 void *inarg = cmd->buf + sizeof(struct fuse_in_header);
981 size_t argsize;
Miklos Szeredife25def2001-12-20 15:38:05 +0000982 struct fuse_context *ctx = fuse_get_context(f);
Miklos Szeredia181e612001-11-06 12:03:23 +0000983
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +0000984 dec_avail(f);
Miklos Szeredi33232032001-11-19 17:55:51 +0000985
Miklos Szeredic0938ea2001-11-07 12:35:06 +0000986 if((f->flags & FUSE_DEBUG)) {
Miklos Szeredic8ba2372002-12-10 12:26:00 +0000987 printf("unique: %i, opcode: %s (%i), ino: %li, insize: %i\n",
988 in->unique, opname(in->opcode), in->opcode, in->ino,
989 cmd->buflen);
Miklos Szeredic0938ea2001-11-07 12:35:06 +0000990 fflush(stdout);
991 }
Miklos Szeredife25def2001-12-20 15:38:05 +0000992
993 ctx->uid = in->uid;
994 ctx->gid = in->gid;
Miklos Szeredia181e612001-11-06 12:03:23 +0000995
996 argsize = cmd->buflen - sizeof(struct fuse_in_header);
997
998 switch(in->opcode) {
999 case FUSE_LOOKUP:
1000 do_lookup(f, in, (char *) inarg);
1001 break;
1002
Miklos Szeredia181e612001-11-06 12:03:23 +00001003 case FUSE_GETATTR:
1004 do_getattr(f, in);
1005 break;
1006
1007 case FUSE_SETATTR:
1008 do_setattr(f, in, (struct fuse_setattr_in *) inarg);
1009 break;
1010
1011 case FUSE_READLINK:
1012 do_readlink(f, in);
1013 break;
1014
1015 case FUSE_GETDIR:
1016 do_getdir(f, in);
1017 break;
1018
1019 case FUSE_MKNOD:
1020 do_mknod(f, in, (struct fuse_mknod_in *) inarg);
1021 break;
1022
1023 case FUSE_MKDIR:
1024 do_mkdir(f, in, (struct fuse_mkdir_in *) inarg);
1025 break;
1026
1027 case FUSE_UNLINK:
Miklos Szeredib5958612004-02-20 14:10:49 +00001028 do_unlink(f, in, (char *) inarg);
1029 break;
1030
Miklos Szeredia181e612001-11-06 12:03:23 +00001031 case FUSE_RMDIR:
Miklos Szeredib5958612004-02-20 14:10:49 +00001032 do_rmdir(f, in, (char *) inarg);
Miklos Szeredia181e612001-11-06 12:03:23 +00001033 break;
1034
1035 case FUSE_SYMLINK:
1036 do_symlink(f, in, (char *) inarg,
1037 ((char *) inarg) + strlen((char *) inarg) + 1);
1038 break;
1039
1040 case FUSE_RENAME:
1041 do_rename(f, in, (struct fuse_rename_in *) inarg);
1042 break;
1043
1044 case FUSE_LINK:
1045 do_link(f, in, (struct fuse_link_in *) inarg);
1046 break;
1047
1048 case FUSE_OPEN:
1049 do_open(f, in, (struct fuse_open_in *) inarg);
1050 break;
1051
Miklos Szeredi9478e862002-12-11 09:50:26 +00001052 case FUSE_RELEASE:
1053 do_release(f, in, (struct fuse_open_in *) inarg);
1054 break;
1055
Miklos Szeredia181e612001-11-06 12:03:23 +00001056 case FUSE_READ:
1057 do_read(f, in, (struct fuse_read_in *) inarg);
1058 break;
1059
1060 case FUSE_WRITE:
1061 do_write(f, in, (struct fuse_write_in *) inarg);
1062 break;
1063
Mark Glinesd84b39a2002-01-07 16:32:02 +00001064 case FUSE_STATFS:
1065 do_statfs(f, in);
1066 break;
1067
Miklos Szeredi5e43f2c2003-12-12 14:06:41 +00001068 case FUSE_FSYNC:
1069 do_fsync(f, in, (struct fuse_fsync_in *) inarg);
1070 break;
1071
Miklos Szeredia181e612001-11-06 12:03:23 +00001072 default:
Miklos Szeredi43696432001-11-18 19:15:05 +00001073 send_reply(f, in, -ENOSYS, NULL, 0);
Miklos Szeredia181e612001-11-06 12:03:23 +00001074 }
Miklos Szeredi43696432001-11-18 19:15:05 +00001075
1076 free_cmd(cmd);
Miklos Szeredia181e612001-11-06 12:03:23 +00001077}
1078
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001079struct fuse_cmd *__fuse_read_cmd(struct fuse *f)
Miklos Szeredi2df1c042001-11-06 15:07:17 +00001080{
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001081 ssize_t res;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001082 struct fuse_cmd *cmd;
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001083 struct fuse_in_header *in;
1084 void *inarg;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001085
Miklos Szeredi43696432001-11-18 19:15:05 +00001086 cmd = (struct fuse_cmd *) malloc(sizeof(struct fuse_cmd));
1087 cmd->buf = (char *) malloc(FUSE_MAX_IN);
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001088 in = (struct fuse_in_header *) cmd->buf;
1089 inarg = cmd->buf + sizeof(struct fuse_in_header);
Miklos Szeredi43696432001-11-18 19:15:05 +00001090
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001091 do {
1092 res = read(f->fd, cmd->buf, FUSE_MAX_IN);
1093 if(res == -1) {
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001094 free_cmd(cmd);
Miklos Szeredi307242f2004-01-26 11:28:44 +00001095 if(f->exited || errno == EINTR)
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001096 return NULL;
1097
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001098 /* ENODEV means we got unmounted, so we silenty return failure */
1099 if(errno != ENODEV) {
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001100 /* BAD... This will happen again */
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001101 perror("fuse: reading device");
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001102 }
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001103
1104 fuse_exit(f);
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001105 return NULL;
Miklos Szeredi96249982001-11-21 12:21:19 +00001106 }
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001107 if((size_t) res < sizeof(struct fuse_in_header)) {
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001108 free_cmd(cmd);
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001109 /* Cannot happen */
1110 fprintf(stderr, "short read on fuse device\n");
1111 fuse_exit(f);
Miklos Szeredi99ddf0e2001-11-25 17:19:59 +00001112 return NULL;
1113 }
1114 cmd->buflen = res;
1115
1116 /* Forget is special, it can be done without messing with threads. */
1117 if(in->opcode == FUSE_FORGET)
1118 do_forget(f, in, (struct fuse_forget_in *) inarg);
1119
1120 } while(in->opcode == FUSE_FORGET);
1121
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001122 return cmd;
Miklos Szeredi2df1c042001-11-06 15:07:17 +00001123}
1124
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001125void fuse_loop(struct fuse *f)
1126{
Miklos Szeredic40748a2004-02-20 16:38:45 +00001127 if(f == NULL)
1128 return;
1129
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001130 while(1) {
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001131 struct fuse_cmd *cmd;
1132
1133 if(f->exited)
1134 return;
1135
1136 cmd = __fuse_read_cmd(f);
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001137 if(cmd == NULL)
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001138 continue;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001139
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001140 __fuse_process_cmd(f, cmd);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001141 }
1142}
1143
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001144void fuse_exit(struct fuse *f)
1145{
1146 f->exited = 1;
1147}
1148
Miklos Szeredi2e50d432001-12-20 12:17:25 +00001149struct fuse_context *fuse_get_context(struct fuse *f)
1150{
1151 if(f->getcontext)
1152 return f->getcontext(f);
1153 else
1154 return &f->context;
1155}
1156
Miklos Szeredic40748a2004-02-20 16:38:45 +00001157static int check_version(struct fuse *f)
1158{
1159 int res;
1160 FILE *vf = fopen(FUSE_VERSION_FILE, "r");
1161 if(vf == NULL) {
1162 fprintf(stderr, "fuse: kernel interface too old, need >= %i.%i\n",
1163 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
1164 return -1;
1165 }
1166 res = fscanf(vf, "%i.%i", &f->majorver, &f->minorver);
1167 fclose(vf);
1168 if(res != 2) {
1169 fprintf(stderr, "fuse: error reading %s\n", FUSE_VERSION_FILE);
1170 return -1;
1171 }
1172 if(f->majorver != FUSE_KERNEL_VERSION) {
1173 fprintf(stderr, "fuse: bad kernel interface major version: needs %i\n",
1174 FUSE_KERNEL_VERSION);
1175 return -1;
1176 }
1177 if(f->minorver < FUSE_KERNEL_MINOR_VERSION) {
1178 fprintf(stderr, "fuse: kernel interface too old: need >= %i.%i",
1179 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
1180 return -1;
1181 }
1182
1183 return 0;
1184}
1185
Miklos Szeredi2e50d432001-12-20 12:17:25 +00001186struct fuse *fuse_new(int fd, int flags, const struct fuse_operations *op)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001187{
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001188 struct fuse *f;
1189 struct node *root;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001190
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001191 f = (struct fuse *) calloc(1, sizeof(struct fuse));
Miklos Szeredi2df1c042001-11-06 15:07:17 +00001192
Miklos Szeredic40748a2004-02-20 16:38:45 +00001193 if(check_version(f) == -1) {
1194 free(f);
1195 return NULL;
1196 }
1197
Miklos Szeredia181e612001-11-06 12:03:23 +00001198 f->flags = flags;
Miklos Szeredi8cffdb92001-11-09 14:49:18 +00001199 f->fd = fd;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001200 f->ctr = 0;
Miklos Szeredi76f65782004-02-19 16:55:40 +00001201 f->generation = 0;
Miklos Szeredifff56ab2001-11-16 10:12:59 +00001202 /* FIXME: Dynamic hash table */
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001203 f->name_table_size = 14057;
1204 f->name_table = (struct node **)
1205 calloc(1, sizeof(struct node *) * f->name_table_size);
1206 f->ino_table_size = 14057;
1207 f->ino_table = (struct node **)
1208 calloc(1, sizeof(struct node *) * f->ino_table_size);
Miklos Szeredia181e612001-11-06 12:03:23 +00001209 pthread_mutex_init(&f->lock, NULL);
Miklos Szeredi33232032001-11-19 17:55:51 +00001210 f->numworker = 0;
1211 f->numavail = 0;
Miklos Szeredi2e50d432001-12-20 12:17:25 +00001212 f->op = *op;
1213 f->getcontext = NULL;
1214 f->context.uid = 0;
1215 f->context.gid = 0;
Miklos Szeredi0f48a262002-12-05 14:23:01 +00001216 f->exited = 0;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001217
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001218 root = (struct node *) calloc(1, sizeof(struct node));
Miklos Szeredi8cffdb92001-11-09 14:49:18 +00001219 root->mode = 0;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001220 root->rdev = 0;
1221 root->name = strdup("/");
1222 root->parent = 0;
Miklos Szeredi76f65782004-02-19 16:55:40 +00001223 root->ino = FUSE_ROOT_INO;
1224 root->generation = 0;
1225 hash_ino(f, root);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001226
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001227 return f;
1228}
1229
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001230void fuse_destroy(struct fuse *f)
1231{
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001232 size_t i;
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001233 for(i = 0; i < f->ino_table_size; i++) {
1234 struct node *node;
1235 struct node *next;
1236 for(node = f->ino_table[i]; node != NULL; node = next) {
1237 next = node->ino_next;
1238 free_node(node);
1239 }
1240 }
1241 free(f->ino_table);
1242 free(f->name_table);
Miklos Szeredia181e612001-11-06 12:03:23 +00001243 pthread_mutex_destroy(&f->lock);
Miklos Szeredi97c61e92001-11-07 12:09:43 +00001244 free(f);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001245}