blob: 137b5ef5ea1af812f1c229713c0da022602ae974 [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
5 This program can be distributed under the terms of the GNU GPL.
6 See the file COPYING.
7*/
8
9#include "fuse_i.h"
10#include <linux/fuse.h>
11
Miklos Szeredia181e612001-11-06 12:03:23 +000012
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000013#include <string.h>
14#include <unistd.h>
15#include <errno.h>
Miklos Szeredi19dff1b2001-10-30 15:06:52 +000016#include <assert.h>
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000017
18
Miklos Szeredia181e612001-11-06 12:03:23 +000019static inline struct node *get_node(fino_t ino)
20{
21 return (struct node *) ((ino << 3) + 0x8000000);
22}
23
24static inline fino_t get_ino(struct node *node)
25{
26 return (((fino_t) node) - 0x8000000) >> 3;
27}
28
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000029static guint name_hash(const struct node *node)
30{
31 return g_str_hash(node->name) ^ node->parent;
32}
33
34static gint name_compare(const struct node *node1, const struct node *node2)
35{
36 return
37 node1->parent == node2->parent &&
38 strcmp(node1->name, node2->name) == 0;
39}
40
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000041static int free_node(struct node *node)
42{
43 g_free(node->name);
44 g_free(node);
45 return 1;
46}
47
Miklos Szeredi19dff1b2001-10-30 15:06:52 +000048static struct node *lookup_node(struct fuse *f, fino_t parent,
49 const char *name)
50{
51 struct node tmp;
52
53 tmp.name = (char *) name;
54 tmp.parent = parent;
55
56 return g_hash_table_lookup(f->nametab, &tmp);
57}
58
Miklos Szeredia181e612001-11-06 12:03:23 +000059static void hash_node(struct fuse *f, struct node *node, fino_t parent,
60 const char *name)
Miklos Szeredi5e183482001-10-31 14:52:35 +000061{
Miklos Szeredia181e612001-11-06 12:03:23 +000062 node->name = g_strdup(name);
63 node->parent = parent;
64 g_hash_table_insert(f->nametab, node, node);
Miklos Szeredi5e183482001-10-31 14:52:35 +000065}
66
Miklos Szeredia181e612001-11-06 12:03:23 +000067static void unhash_node(struct fuse *f, struct node *node)
68{
69 if(node->name != NULL) {
70 g_hash_table_remove(f->nametab, node);
71 g_free(node->name);
72 node->parent = 0;
73 node->name = NULL;
74 }
75}
76
77static fino_t find_node(struct fuse *f, fino_t parent, char *name,
78 struct fuse_attr *attr, int version)
Miklos Szeredi5e183482001-10-31 14:52:35 +000079{
80 struct node *node;
Miklos Szeredia181e612001-11-06 12:03:23 +000081 int mode = attr->mode & S_IFMT;
82 int rdev = 0;
83
84 if(S_ISCHR(mode) || S_ISBLK(mode))
85 rdev = attr->rdev;
Miklos Szeredi5e183482001-10-31 14:52:35 +000086
Miklos Szeredia181e612001-11-06 12:03:23 +000087 pthread_mutex_lock(&f->lock);
Miklos Szeredi5e183482001-10-31 14:52:35 +000088 node = lookup_node(f, parent, name);
89 if(node != NULL) {
Miklos Szeredia181e612001-11-06 12:03:23 +000090 if(node->mode == mode && node->rdev == rdev)
91 goto out;
92
Miklos Szeredi5e183482001-10-31 14:52:35 +000093 unhash_node(f, node);
94 }
95
Miklos Szeredia181e612001-11-06 12:03:23 +000096 node = g_new0(struct node, 1);
97 node->mode = mode;
98 node->rdev = rdev;
99 hash_node(f, node, parent, name);
100
101 out:
102 node->version = version;
103 pthread_mutex_unlock(&f->lock);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000104 return get_ino(node);
105}
106
107static fino_t find_node_dir(struct fuse *f, fino_t parent, char *name)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000108{
109 struct node *node;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000110
Miklos Szeredia181e612001-11-06 12:03:23 +0000111 pthread_mutex_lock(&f->lock);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000112 node = lookup_node(f, parent, name);
Miklos Szeredia181e612001-11-06 12:03:23 +0000113 pthread_mutex_unlock(&f->lock);
114
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000115 if(node != NULL)
116 return get_ino(node);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000117 else
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000118 return (fino_t) -1;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000119}
120
Miklos Szeredia181e612001-11-06 12:03:23 +0000121static char *get_path(struct fuse *f, fino_t ino)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000122{
123 GString *s;
124 char *ss;
125
126 s = g_string_new("");
127 if(ino == FUSE_ROOT_INO)
128 g_string_prepend_c(s, '/');
129 else {
130 struct node *node;
Miklos Szeredia181e612001-11-06 12:03:23 +0000131 pthread_mutex_lock(&f->lock);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000132 for(; ino != FUSE_ROOT_INO; ino = node->parent) {
133 node = get_node(ino);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000134 if(node->name == NULL) {
Miklos Szeredia181e612001-11-06 12:03:23 +0000135 pthread_mutex_unlock(&f->lock);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000136 g_string_free(s, TRUE);
137 return NULL;
138 }
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000139 g_string_prepend(s, node->name);
140 g_string_prepend_c(s, '/');
141 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000142 pthread_mutex_unlock(&f->lock);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000143 }
144
145 ss = s->str;
146 g_string_free(s, FALSE);
147
148 return ss;
149}
150
Miklos Szeredia181e612001-11-06 12:03:23 +0000151static char *get_path_name(struct fuse *f, fino_t ino, const char *name)
Miklos Szeredib483c932001-10-29 14:57:57 +0000152{
Miklos Szeredi5e183482001-10-31 14:52:35 +0000153 char *path2;
Miklos Szeredia181e612001-11-06 12:03:23 +0000154 char *path;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000155
Miklos Szeredia181e612001-11-06 12:03:23 +0000156 if(ino == FUSE_ROOT_INO)
157 return g_strconcat("/", name, NULL);
158
159 path = get_path(f, ino);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000160 if(path == NULL)
161 return NULL;
Miklos Szeredia181e612001-11-06 12:03:23 +0000162
Miklos Szeredi5e183482001-10-31 14:52:35 +0000163 path2 = g_strconcat(path, "/", name, NULL);
Miklos Szeredib483c932001-10-29 14:57:57 +0000164 g_free(path);
Miklos Szeredia181e612001-11-06 12:03:23 +0000165
Miklos Szeredib483c932001-10-29 14:57:57 +0000166 return path2;
167}
168
Miklos Szeredia181e612001-11-06 12:03:23 +0000169static void destroy_node(struct fuse *f, fino_t ino, int version)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000170{
Miklos Szeredia181e612001-11-06 12:03:23 +0000171 struct node *node;
172
173 pthread_mutex_lock(&f->lock);
174 node = get_node(ino);
175 if(node->version == version) {
176 unhash_node(f, node);
177 free_node(node);
178 }
179 pthread_mutex_unlock(&f->lock);
180
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000181}
182
Miklos Szeredi5e183482001-10-31 14:52:35 +0000183static void remove_node(struct fuse *f, fino_t dir, const char *name)
184{
Miklos Szeredia181e612001-11-06 12:03:23 +0000185 struct node *node;
186
187 pthread_mutex_lock(&f->lock);
188 node = lookup_node(f, dir, name);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000189 assert(node != NULL);
190 unhash_node(f, node);
Miklos Szeredia181e612001-11-06 12:03:23 +0000191 pthread_mutex_unlock(&f->lock);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000192}
193
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000194static void rename_node(struct fuse *f, fino_t olddir, const char *oldname,
195 fino_t newdir, const char *newname)
196{
Miklos Szeredia181e612001-11-06 12:03:23 +0000197 struct node *node;
198 struct node *newnode;
199
200 pthread_mutex_lock(&f->lock);
201 node = lookup_node(f, olddir, oldname);
202 newnode = lookup_node(f, newdir, newname);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000203 assert(node != NULL);
204
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000205 if(newnode != NULL)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000206 unhash_node(f, newnode);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000207
Miklos Szeredia181e612001-11-06 12:03:23 +0000208 unhash_node(f, node);
209 hash_node(f, node, newdir, newname);
210 pthread_mutex_unlock(&f->lock);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000211}
212
213
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000214static void convert_stat(struct stat *stbuf, struct fuse_attr *attr)
215{
216 attr->mode = stbuf->st_mode;
217 attr->nlink = stbuf->st_nlink;
218 attr->uid = stbuf->st_uid;
219 attr->gid = stbuf->st_gid;
220 attr->rdev = stbuf->st_rdev;
221 attr->size = stbuf->st_size;
222 attr->blksize = stbuf->st_blksize;
223 attr->blocks = stbuf->st_blocks;
224 attr->atime = stbuf->st_atime;
225 attr->mtime = stbuf->st_mtime;
226 attr->ctime = stbuf->st_ctime;
227}
228
Miklos Szeredia181e612001-11-06 12:03:23 +0000229static int fill_dir(struct fuse_dirhandle *dh, char *name, int type)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000230{
231 struct fuse_dirent dirent;
232 size_t reclen;
233 size_t res;
234
Miklos Szeredi5e183482001-10-31 14:52:35 +0000235 dirent.ino = find_node_dir(dh->fuse, dh->dir, name);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000236 dirent.namelen = strlen(name);
237 strncpy(dirent.name, name, sizeof(dirent.name));
238 dirent.type = type;
239 reclen = FUSE_DIRENT_SIZE(&dirent);
240 res = fwrite(&dirent, reclen, 1, dh->fp);
241 if(res == 0) {
242 perror("writing directory file");
243 return -EIO;
244 }
245 return 0;
246}
247
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000248static void send_reply(struct fuse *f, struct fuse_in_header *in, int error,
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000249 void *arg, size_t argsize)
250{
251 int res;
252 char *outbuf;
253 size_t outsize;
254 struct fuse_out_header *out;
255
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000256 if(error > 0) {
257 fprintf(stderr, "positive error code: %i\n", error);
258 error = -ERANGE;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000259 }
260
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000261 if(error)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000262 argsize = 0;
263
264 outsize = sizeof(struct fuse_out_header) + argsize;
265 outbuf = (char *) g_malloc(outsize);
266 out = (struct fuse_out_header *) outbuf;
267 out->unique = in->unique;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000268 out->error = error;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000269 if(argsize != 0)
270 memcpy(outbuf + sizeof(struct fuse_out_header), arg, argsize);
271
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000272 printf(" unique: %i, error: %i (%s), outsize: %i\n", out->unique,
273 out->error, strerror(-out->error), outsize);
Miklos Szeredia181e612001-11-06 12:03:23 +0000274 fflush(stdout);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000275
276 res = write(f->fd, outbuf, outsize);
277 if(res == -1)
278 perror("writing fuse device");
279
280 g_free(outbuf);
281}
282
Miklos Szeredia181e612001-11-06 12:03:23 +0000283static void fill_cred(struct fuse_in_header *in, struct fuse_cred *cred)
284{
285 cred->uid = in->uid;
286 cred->gid = in->gid;
287}
288
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000289static void do_lookup(struct fuse *f, struct fuse_in_header *in, char *name)
290{
291 int res;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000292 char *path;
293 struct stat buf;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000294 struct fuse_lookup_out arg;
Miklos Szeredia181e612001-11-06 12:03:23 +0000295 struct fuse_cred cred;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000296
Miklos Szeredia181e612001-11-06 12:03:23 +0000297 fill_cred(in, &cred);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000298 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000299 path = get_path_name(f, in->ino, name);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000300 if(path != NULL) {
301 res = -ENOSYS;
302 if(f->op.getattr)
Miklos Szeredia181e612001-11-06 12:03:23 +0000303 res = f->op.getattr(&cred, path, &buf);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000304 g_free(path);
305 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000306 if(res == 0) {
307 convert_stat(&buf, &arg.attr);
Miklos Szeredia181e612001-11-06 12:03:23 +0000308 arg.ino = find_node(f, in->ino, name, &arg.attr, in->unique);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000309 }
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000310 send_reply(f, in, res, &arg, sizeof(arg));
311}
312
Miklos Szeredia181e612001-11-06 12:03:23 +0000313static void do_forget(struct fuse *f, struct fuse_in_header *in,
314 struct fuse_forget_in *arg)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000315{
Miklos Szeredia181e612001-11-06 12:03:23 +0000316 destroy_node(f, in->ino, arg->version);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000317}
318
319static void do_getattr(struct fuse *f, struct fuse_in_header *in)
320{
321 int res;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000322 char *path;
323 struct stat buf;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000324 struct fuse_getattr_out arg;
Miklos Szeredia181e612001-11-06 12:03:23 +0000325 struct fuse_cred cred;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000326
Miklos Szeredia181e612001-11-06 12:03:23 +0000327 fill_cred(in, &cred);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000328 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000329 path = get_path(f, in->ino);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000330 if(path != NULL) {
331 res = -ENOSYS;
332 if(f->op.getattr)
Miklos Szeredia181e612001-11-06 12:03:23 +0000333 res = f->op.getattr(&cred, path, &buf);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000334 g_free(path);
335 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000336 if(res == 0)
337 convert_stat(&buf, &arg.attr);
338
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000339 send_reply(f, in, res, &arg, sizeof(arg));
340}
341
Miklos Szeredi5e183482001-10-31 14:52:35 +0000342static void do_setattr(struct fuse *f, struct fuse_in_header *in,
343 struct fuse_setattr_in *arg)
344{
345 int res;
346 char *path;
347 int valid = arg->valid;
348 struct fuse_attr *attr = &arg->attr;
Miklos Szeredia181e612001-11-06 12:03:23 +0000349 struct fuse_setattr_out outarg;
350 struct fuse_cred cred;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000351
Miklos Szeredia181e612001-11-06 12:03:23 +0000352 fill_cred(in, &cred);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000353 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000354 path = get_path(f, in->ino);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000355 if(path != NULL) {
356 res = 0;
357 if(!res && (valid & FATTR_MODE)) {
358 res = -ENOSYS;
359 if(f->op.chmod)
Miklos Szeredia181e612001-11-06 12:03:23 +0000360 res = f->op.chmod(&cred, path, attr->mode);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000361 }
362 if(!res && (valid & (FATTR_UID | FATTR_GID))) {
363 uid_t uid = (valid & FATTR_UID) ? attr->uid : (uid_t) -1;
364 gid_t gid = (valid & FATTR_GID) ? attr->gid : (gid_t) -1;
365
366 res = -ENOSYS;
367 if(f->op.chown)
Miklos Szeredia181e612001-11-06 12:03:23 +0000368 res = f->op.chown(&cred, path, uid, gid);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000369 }
370 if(!res && (valid & FATTR_SIZE)) {
371 res = -ENOSYS;
Miklos Szeredia181e612001-11-06 12:03:23 +0000372 if(f->op.truncate && f->op.getattr) {
373 res = f->op.truncate(&cred, path, attr->size);
374 if(!res) {
375 struct stat buf;
376 res = f->op.getattr(&cred, path, &buf);
377 outarg.newsize = buf.st_size;
378 }
379 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000380 }
381 if(!res && (valid & FATTR_UTIME)) {
382 struct utimbuf buf;
383 buf.actime = attr->atime;
384 buf.modtime = attr->mtime;
385 res = -ENOSYS;
386 if(f->op.utime)
Miklos Szeredia181e612001-11-06 12:03:23 +0000387 res = f->op.utime(&cred, path, &buf);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000388 }
389 g_free(path);
390 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000391 send_reply(f, in, res, &outarg, sizeof(outarg));
Miklos Szeredi5e183482001-10-31 14:52:35 +0000392}
393
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000394static void do_readlink(struct fuse *f, struct fuse_in_header *in)
395{
396 int res;
397 char link[PATH_MAX + 1];
Miklos Szeredib483c932001-10-29 14:57:57 +0000398 char *path;
Miklos Szeredia181e612001-11-06 12:03:23 +0000399 struct fuse_cred cred;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000400
Miklos Szeredia181e612001-11-06 12:03:23 +0000401 fill_cred(in, &cred);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000402 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000403 path = get_path(f, in->ino);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000404 if(path != NULL) {
405 res = -ENOSYS;
406 if(f->op.readlink)
Miklos Szeredia181e612001-11-06 12:03:23 +0000407 res = f->op.readlink(&cred, path, link, sizeof(link));
Miklos Szeredi5e183482001-10-31 14:52:35 +0000408 g_free(path);
409 }
410 send_reply(f, in, res, link, !res ? strlen(link) : 0);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000411}
412
413static void do_getdir(struct fuse *f, struct fuse_in_header *in)
414{
415 int res;
416 struct fuse_getdir_out arg;
Miklos Szeredia181e612001-11-06 12:03:23 +0000417 struct fuse_dirhandle dh;
Miklos Szeredib483c932001-10-29 14:57:57 +0000418 char *path;
Miklos Szeredia181e612001-11-06 12:03:23 +0000419 struct fuse_cred cred;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000420
Miklos Szeredia181e612001-11-06 12:03:23 +0000421 fill_cred(in, &cred);
Miklos Szeredib483c932001-10-29 14:57:57 +0000422 dh.fuse = f;
423 dh.fp = tmpfile();
424 dh.dir = in->ino;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000425 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000426 path = get_path(f, in->ino);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000427 if(path != NULL) {
428 res = -ENOSYS;
429 if(f->op.getdir)
Miklos Szeredia181e612001-11-06 12:03:23 +0000430 res = f->op.getdir(&cred, path, &dh, (fuse_dirfil_t) fill_dir);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000431 g_free(path);
432 }
Miklos Szeredib483c932001-10-29 14:57:57 +0000433 fflush(dh.fp);
434 arg.fd = fileno(dh.fp);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000435 send_reply(f, in, res, &arg, sizeof(arg));
Miklos Szeredib483c932001-10-29 14:57:57 +0000436 fclose(dh.fp);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000437}
438
Miklos Szeredib483c932001-10-29 14:57:57 +0000439static void do_mknod(struct fuse *f, struct fuse_in_header *in,
440 struct fuse_mknod_in *inarg)
441{
442 int res;
443 char *path;
444 struct fuse_mknod_out outarg;
445 struct stat buf;
Miklos Szeredia181e612001-11-06 12:03:23 +0000446 struct fuse_cred cred;
Miklos Szeredib483c932001-10-29 14:57:57 +0000447
Miklos Szeredia181e612001-11-06 12:03:23 +0000448 fill_cred(in, &cred);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000449 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000450 path = get_path_name(f, in->ino, inarg->name);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000451 if(path != NULL) {
452 res = -ENOSYS;
453 if(f->op.mknod && f->op.getattr) {
Miklos Szeredia181e612001-11-06 12:03:23 +0000454 res = f->op.mknod(&cred, path, inarg->mode, inarg->rdev);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000455 if(res == 0)
Miklos Szeredia181e612001-11-06 12:03:23 +0000456 res = f->op.getattr(&cred, path, &buf);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000457 }
458 g_free(path);
Miklos Szeredib483c932001-10-29 14:57:57 +0000459 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000460 if(res == 0) {
Miklos Szeredib483c932001-10-29 14:57:57 +0000461 convert_stat(&buf, &outarg.attr);
Miklos Szeredia181e612001-11-06 12:03:23 +0000462 outarg.ino = find_node(f, in->ino, inarg->name, &outarg.attr,
463 in->unique);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000464 }
Miklos Szeredib483c932001-10-29 14:57:57 +0000465
466 send_reply(f, in, res, &outarg, sizeof(outarg));
467}
468
469static void do_mkdir(struct fuse *f, struct fuse_in_header *in,
470 struct fuse_mkdir_in *inarg)
471{
472 int res;
473 char *path;
Miklos Szeredia181e612001-11-06 12:03:23 +0000474 struct fuse_cred cred;
Miklos Szeredib483c932001-10-29 14:57:57 +0000475
Miklos Szeredia181e612001-11-06 12:03:23 +0000476 fill_cred(in, &cred);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000477 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000478 path = get_path_name(f, in->ino, inarg->name);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000479 if(path != NULL) {
480 res = -ENOSYS;
481 if(f->op.mkdir)
Miklos Szeredia181e612001-11-06 12:03:23 +0000482 res = f->op.mkdir(&cred, path, inarg->mode);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000483 g_free(path);
484 }
Miklos Szeredib483c932001-10-29 14:57:57 +0000485 send_reply(f, in, res, NULL, 0);
486}
487
488static void do_remove(struct fuse *f, struct fuse_in_header *in, char *name)
489{
490 int res;
491 char *path;
Miklos Szeredia181e612001-11-06 12:03:23 +0000492 struct fuse_cred cred;
Miklos Szeredib483c932001-10-29 14:57:57 +0000493
Miklos Szeredia181e612001-11-06 12:03:23 +0000494 fill_cred(in, &cred);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000495 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000496 path = get_path_name(f, in->ino, name);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000497 if(path != NULL) {
498 res = -ENOSYS;
499 if(in->opcode == FUSE_UNLINK) {
500 if(f->op.unlink)
Miklos Szeredia181e612001-11-06 12:03:23 +0000501 res = f->op.unlink(&cred, path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000502 }
503 else {
504 if(f->op.rmdir)
Miklos Szeredia181e612001-11-06 12:03:23 +0000505 res = f->op.rmdir(&cred, path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000506 }
507 g_free(path);
Miklos Szeredib483c932001-10-29 14:57:57 +0000508 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000509 if(res == 0)
510 remove_node(f, in->ino, name);
Miklos Szeredib483c932001-10-29 14:57:57 +0000511 send_reply(f, in, res, NULL, 0);
512}
513
514static void do_symlink(struct fuse *f, struct fuse_in_header *in, char *name,
515 char *link)
516{
517 int res;
518 char *path;
Miklos Szeredia181e612001-11-06 12:03:23 +0000519 struct fuse_cred cred;
Miklos Szeredib483c932001-10-29 14:57:57 +0000520
Miklos Szeredia181e612001-11-06 12:03:23 +0000521 fill_cred(in, &cred);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000522 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000523 path = get_path_name(f, in->ino, name);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000524 if(path != NULL) {
525 res = -ENOSYS;
526 if(f->op.symlink)
Miklos Szeredia181e612001-11-06 12:03:23 +0000527 res = f->op.symlink(&cred, link, path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000528 g_free(path);
529 }
Miklos Szeredib483c932001-10-29 14:57:57 +0000530 send_reply(f, in, res, NULL, 0);
531}
532
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000533static void do_rename(struct fuse *f, struct fuse_in_header *in,
534 struct fuse_rename_in *inarg)
535{
536 int res;
537 fino_t olddir = in->ino;
538 fino_t newdir = inarg->newdir;
539 char *oldname = inarg->names;
540 char *newname = inarg->names + strlen(oldname) + 1;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000541 char *oldpath;
542 char *newpath;
Miklos Szeredia181e612001-11-06 12:03:23 +0000543 struct fuse_cred cred;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000544
Miklos Szeredia181e612001-11-06 12:03:23 +0000545 fill_cred(in, &cred);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000546 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000547 oldpath = get_path_name(f, olddir, oldname);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000548 if(oldpath != NULL) {
Miklos Szeredia181e612001-11-06 12:03:23 +0000549 newpath = get_path_name(f, newdir, newname);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000550 if(newpath != NULL) {
551 res = -ENOSYS;
552 if(f->op.rename)
Miklos Szeredia181e612001-11-06 12:03:23 +0000553 res = f->op.rename(&cred, oldpath, newpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000554 if(res == 0)
555 rename_node(f, olddir, oldname, newdir, newname);
556 g_free(newpath);
557 }
558 g_free(oldpath);
559 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000560 send_reply(f, in, res, NULL, 0);
561}
562
563static void do_link(struct fuse *f, struct fuse_in_header *in,
Miklos Szeredi5e183482001-10-31 14:52:35 +0000564 struct fuse_link_in *arg)
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000565{
566 int res;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000567 char *oldpath;
568 char *newpath;
Miklos Szeredia181e612001-11-06 12:03:23 +0000569 struct fuse_cred cred;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000570
Miklos Szeredia181e612001-11-06 12:03:23 +0000571 fill_cred(in, &cred);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000572 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000573 oldpath = get_path(f, in->ino);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000574 if(oldpath != NULL) {
Miklos Szeredia181e612001-11-06 12:03:23 +0000575 newpath = get_path_name(f, arg->newdir, arg->name);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000576 if(newpath != NULL) {
577 res = -ENOSYS;
578 if(f->op.link)
Miklos Szeredia181e612001-11-06 12:03:23 +0000579 res = f->op.link(&cred, oldpath, newpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000580 g_free(newpath);
581 }
582 g_free(oldpath);
583 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000584 send_reply(f, in, res, NULL, 0);
585}
586
Miklos Szeredi5e183482001-10-31 14:52:35 +0000587static void do_open(struct fuse *f, struct fuse_in_header *in,
588 struct fuse_open_in *arg)
589{
590 int res;
591 char *path;
Miklos Szeredia181e612001-11-06 12:03:23 +0000592 struct fuse_cred cred;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000593
Miklos Szeredia181e612001-11-06 12:03:23 +0000594 fill_cred(in, &cred);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000595 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000596 path = get_path(f, in->ino);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000597 if(path != NULL) {
598 res = -ENOSYS;
599 if(f->op.open)
Miklos Szeredia181e612001-11-06 12:03:23 +0000600 res = f->op.open(&cred, path, arg->flags);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000601 g_free(path);
602 }
603 send_reply(f, in, res, NULL, 0);
604}
605
606static void do_read(struct fuse *f, struct fuse_in_header *in,
607 struct fuse_read_in *arg)
608{
609 int res;
610 char *path;
611 char *buf = g_malloc(arg->size);
612 size_t size;
Miklos Szeredia181e612001-11-06 12:03:23 +0000613 struct fuse_cred cred;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000614
Miklos Szeredia181e612001-11-06 12:03:23 +0000615 fill_cred(in, &cred);
616 path = get_path(f, in->ino);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000617 if(path != NULL) {
618 res = -ENOSYS;
Miklos Szeredia181e612001-11-06 12:03:23 +0000619 if(f->op.read)
620 res = f->op.read(&cred, path, buf, arg->size, arg->offset);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000621 g_free(path);
622 }
623
624 size = 0;
625 if(res > 0) {
626 size = res;
627 res = 0;
628 }
629
630 send_reply(f, in, res, buf, size);
631 g_free(buf);
632}
Miklos Szeredib483c932001-10-29 14:57:57 +0000633
Miklos Szeredia181e612001-11-06 12:03:23 +0000634static void do_write(struct fuse *f, struct fuse_in_header *in,
635 struct fuse_write_in *arg)
636{
637 int res;
638 char *path;
639 struct fuse_cred cred;
640
641 fill_cred(in, &cred);
642 path = get_path(f, in->ino);
643 if(path != NULL) {
644 res = -ENOSYS;
645 if(f->op.write)
646 res = f->op.write(&cred, path, arg->buf, arg->size, arg->offset);
647 g_free(path);
648 }
649
650 if(res > 0) {
651 if((size_t) res != arg->size) {
652 fprintf(stderr, "short write: %u (should be %u)\n", res,
653 arg->size);
654 res = -EIO;
655 }
656 else
657 res = 0;
658 }
659
660 send_reply(f, in, res, NULL, 0);
661}
662
663struct cmd {
664 struct fuse *f;
665 char *buf;
666 size_t buflen;
667};
668
669static void *do_command(void *data)
670{
671 struct cmd *cmd = (struct cmd *) data;
672 struct fuse_in_header *in = (struct fuse_in_header *) cmd->buf;
673 void *inarg = cmd->buf + sizeof(struct fuse_in_header);
674 size_t argsize;
675 struct fuse *f = cmd->f;
676
677 printf("unique: %i, opcode: %i, ino: %li, insize: %i\n", in->unique,
678 in->opcode, in->ino, cmd->buflen);
679 fflush(stdout);
680
681 argsize = cmd->buflen - sizeof(struct fuse_in_header);
682
683 switch(in->opcode) {
684 case FUSE_LOOKUP:
685 do_lookup(f, in, (char *) inarg);
686 break;
687
688 case FUSE_FORGET:
689 do_forget(f, in, (struct fuse_forget_in *) inarg);
690 break;
691
692 case FUSE_GETATTR:
693 do_getattr(f, in);
694 break;
695
696 case FUSE_SETATTR:
697 do_setattr(f, in, (struct fuse_setattr_in *) inarg);
698 break;
699
700 case FUSE_READLINK:
701 do_readlink(f, in);
702 break;
703
704 case FUSE_GETDIR:
705 do_getdir(f, in);
706 break;
707
708 case FUSE_MKNOD:
709 do_mknod(f, in, (struct fuse_mknod_in *) inarg);
710 break;
711
712 case FUSE_MKDIR:
713 do_mkdir(f, in, (struct fuse_mkdir_in *) inarg);
714 break;
715
716 case FUSE_UNLINK:
717 case FUSE_RMDIR:
718 do_remove(f, in, (char *) inarg);
719 break;
720
721 case FUSE_SYMLINK:
722 do_symlink(f, in, (char *) inarg,
723 ((char *) inarg) + strlen((char *) inarg) + 1);
724 break;
725
726 case FUSE_RENAME:
727 do_rename(f, in, (struct fuse_rename_in *) inarg);
728 break;
729
730 case FUSE_LINK:
731 do_link(f, in, (struct fuse_link_in *) inarg);
732 break;
733
734 case FUSE_OPEN:
735 do_open(f, in, (struct fuse_open_in *) inarg);
736 break;
737
738 case FUSE_READ:
739 do_read(f, in, (struct fuse_read_in *) inarg);
740 break;
741
742 case FUSE_WRITE:
743 do_write(f, in, (struct fuse_write_in *) inarg);
744 break;
745
746 default:
747 fprintf(stderr, "Operation %i not implemented\n", in->opcode);
748 /* No need to send reply to async requests */
749 if(in->unique != 0)
750 send_reply(f, in, -ENOSYS, NULL, 0);
751 }
752
753 g_free(cmd->buf);
754 g_free(cmd);
755
756 return NULL;
757}
758
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000759void fuse_loop(struct fuse *f)
760{
761 int res;
762 char inbuf[FUSE_MAX_IN];
Miklos Szeredia181e612001-11-06 12:03:23 +0000763 pthread_attr_t attr;
764 pthread_t thrid;
765
766 pthread_attr_init(&attr);
767 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000768
769 while(1) {
Miklos Szeredia181e612001-11-06 12:03:23 +0000770 struct cmd *cmd;
771
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000772 res = read(f->fd, inbuf, sizeof(inbuf));
773 if(res == -1) {
774 perror("reading fuse device");
775 continue;
776 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000777 if((size_t) res < sizeof(struct fuse_in_header)) {
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000778 fprintf(stderr, "short read on fuse device\n");
779 continue;
780 }
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000781
Miklos Szeredia181e612001-11-06 12:03:23 +0000782 cmd = g_new0(struct cmd, 1);
783 cmd->f = f;
784 cmd->buflen = res;
785 cmd->buf = g_malloc(cmd->buflen);
786 memcpy(cmd->buf, inbuf, cmd->buflen);
787
788 if(f->flags & FUSE_MULTITHREAD) {
789 res = pthread_create(&thrid, &attr, do_command, cmd);
790 if(res != 0) {
791 fprintf(stderr, "Error creating thread: %s\n",
792 strerror(errno));
793 exit(1);
794 }
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000795 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000796 else
797 do_command(cmd);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000798 }
799}
800
Miklos Szeredia181e612001-11-06 12:03:23 +0000801struct fuse *fuse_new(int flags)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000802{
803 struct fuse *f = g_new0(struct fuse, 1);
804
Miklos Szeredia181e612001-11-06 12:03:23 +0000805 f->flags = flags;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000806 f->fd = -1;
807 f->dir = NULL;
808 f->nametab = g_hash_table_new((GHashFunc) name_hash,
809 (GCompareFunc) name_compare);
Miklos Szeredia181e612001-11-06 12:03:23 +0000810 pthread_mutex_init(&f->lock, NULL);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000811
812 return f;
813}
814
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000815void fuse_set_operations(struct fuse *f, const struct fuse_operations *op)
816{
817 f->op = *op;
818}
819
820void fuse_destroy(struct fuse *f)
821{
Miklos Szeredia181e612001-11-06 12:03:23 +0000822 close(f->fd);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000823 g_hash_table_foreach_remove(f->nametab, (GHRFunc) free_node, NULL);
824 g_hash_table_destroy(f->nametab);
Miklos Szeredia181e612001-11-06 12:03:23 +0000825 pthread_mutex_destroy(&f->lock);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000826 g_free(f);
827}