Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame^] | 1 | /* |
| 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 | |
| 12 | #include <string.h> |
| 13 | #include <unistd.h> |
| 14 | #include <errno.h> |
| 15 | |
| 16 | |
| 17 | static guint name_hash(const struct node *node) |
| 18 | { |
| 19 | return g_str_hash(node->name) ^ node->parent; |
| 20 | } |
| 21 | |
| 22 | static gint name_compare(const struct node *node1, const struct node *node2) |
| 23 | { |
| 24 | return |
| 25 | node1->parent == node2->parent && |
| 26 | strcmp(node1->name, node2->name) == 0; |
| 27 | } |
| 28 | |
| 29 | static struct node *new_node(fino_t parent, const char *name) |
| 30 | { |
| 31 | struct node *node = g_new0(struct node, 1); |
| 32 | node->name = strdup(name); |
| 33 | node->parent = parent; |
| 34 | return node; |
| 35 | } |
| 36 | |
| 37 | static int free_node(struct node *node) |
| 38 | { |
| 39 | g_free(node->name); |
| 40 | g_free(node); |
| 41 | return 1; |
| 42 | } |
| 43 | |
| 44 | static inline struct node *get_node(fino_t ino) |
| 45 | { |
| 46 | return (struct node *) ino; |
| 47 | } |
| 48 | |
| 49 | static inline fino_t get_ino(struct node *node) |
| 50 | { |
| 51 | return (fino_t) node; |
| 52 | } |
| 53 | |
| 54 | |
| 55 | static fino_t find_node(struct fuse *f, fino_t parent, char *name, int create) |
| 56 | { |
| 57 | struct node *node; |
| 58 | struct node tmp; |
| 59 | |
| 60 | tmp.name = name; |
| 61 | tmp.parent = parent; |
| 62 | |
| 63 | node = g_hash_table_lookup(f->nametab, &tmp); |
| 64 | if(node != NULL) |
| 65 | return get_ino(node); |
| 66 | |
| 67 | if(!create) |
| 68 | return (fino_t) -1; |
| 69 | |
| 70 | node = new_node(parent, name); |
| 71 | g_hash_table_insert(f->nametab, node, node); |
| 72 | return get_ino(node); |
| 73 | } |
| 74 | |
| 75 | static char *get_path(fino_t ino) |
| 76 | { |
| 77 | GString *s; |
| 78 | char *ss; |
| 79 | |
| 80 | s = g_string_new(""); |
| 81 | if(ino == FUSE_ROOT_INO) |
| 82 | g_string_prepend_c(s, '/'); |
| 83 | else { |
| 84 | struct node *node; |
| 85 | for(; ino != FUSE_ROOT_INO; ino = node->parent) { |
| 86 | node = get_node(ino); |
| 87 | g_string_prepend(s, node->name); |
| 88 | g_string_prepend_c(s, '/'); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | ss = s->str; |
| 93 | g_string_free(s, FALSE); |
| 94 | |
| 95 | return ss; |
| 96 | } |
| 97 | |
| 98 | static void remove_node(struct fuse *f, fino_t ino) |
| 99 | { |
| 100 | struct node *node = get_node(ino); |
| 101 | g_hash_table_remove(f->nametab, node); |
| 102 | free_node(node); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | static void convert_stat(struct stat *stbuf, struct fuse_attr *attr) |
| 107 | { |
| 108 | attr->mode = stbuf->st_mode; |
| 109 | attr->nlink = stbuf->st_nlink; |
| 110 | attr->uid = stbuf->st_uid; |
| 111 | attr->gid = stbuf->st_gid; |
| 112 | attr->rdev = stbuf->st_rdev; |
| 113 | attr->size = stbuf->st_size; |
| 114 | attr->blksize = stbuf->st_blksize; |
| 115 | attr->blocks = stbuf->st_blocks; |
| 116 | attr->atime = stbuf->st_atime; |
| 117 | attr->mtime = stbuf->st_mtime; |
| 118 | attr->ctime = stbuf->st_ctime; |
| 119 | } |
| 120 | |
| 121 | static int get_attributes(struct fuse *f, fino_t ino, struct fuse_attr *attr) |
| 122 | { |
| 123 | char *path; |
| 124 | struct stat buf; |
| 125 | int res; |
| 126 | |
| 127 | if(f->op.getattr == NULL) |
| 128 | return -ENOSYS; |
| 129 | |
| 130 | path = get_path(ino); |
| 131 | res = f->op.getattr(path, &buf); |
| 132 | g_free(path); |
| 133 | if(res == 0) |
| 134 | convert_stat(&buf, attr); |
| 135 | |
| 136 | return res; |
| 137 | } |
| 138 | |
| 139 | static int read_link(struct fuse *f, fino_t ino, char *buf, size_t size) |
| 140 | { |
| 141 | char *path; |
| 142 | int res; |
| 143 | |
| 144 | if(f->op.readlink == NULL) |
| 145 | return -ENOSYS; |
| 146 | |
| 147 | path = get_path(ino); |
| 148 | res = f->op.readlink(path, buf, size); |
| 149 | g_free(path); |
| 150 | |
| 151 | return res; |
| 152 | } |
| 153 | |
| 154 | static int fill_dir(struct fuse_dh *dh, char *name, int type) |
| 155 | { |
| 156 | struct fuse_dirent dirent; |
| 157 | size_t reclen; |
| 158 | size_t res; |
| 159 | |
| 160 | dirent.ino = find_node(dh->fuse, dh->dir, name, 0); |
| 161 | dirent.namelen = strlen(name); |
| 162 | strncpy(dirent.name, name, sizeof(dirent.name)); |
| 163 | dirent.type = type; |
| 164 | reclen = FUSE_DIRENT_SIZE(&dirent); |
| 165 | res = fwrite(&dirent, reclen, 1, dh->fp); |
| 166 | if(res == 0) { |
| 167 | perror("writing directory file"); |
| 168 | return -EIO; |
| 169 | } |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | static int get_dir(struct fuse *f, fino_t ino, FILE *fp) |
| 174 | { |
| 175 | char *path; |
| 176 | int res; |
| 177 | struct fuse_dh dh; |
| 178 | |
| 179 | if(f->op.getdir == NULL) |
| 180 | return -ENOSYS; |
| 181 | |
| 182 | dh.fuse = f; |
| 183 | dh.fp = fp; |
| 184 | dh.dir = ino; |
| 185 | |
| 186 | path = get_path(ino); |
| 187 | res = f->op.getdir(path, &dh, (dirfiller_t) fill_dir); |
| 188 | g_free(path); |
| 189 | |
| 190 | return res; |
| 191 | } |
| 192 | |
| 193 | |
| 194 | static void send_reply(struct fuse *f, struct fuse_in_header *in, int result, |
| 195 | void *arg, size_t argsize) |
| 196 | { |
| 197 | int res; |
| 198 | char *outbuf; |
| 199 | size_t outsize; |
| 200 | struct fuse_out_header *out; |
| 201 | |
| 202 | if(result > 0) { |
| 203 | fprintf(stderr, "positive result to operation %i : %i\n", in->opcode, |
| 204 | result); |
| 205 | result = -ERANGE; |
| 206 | } |
| 207 | |
| 208 | if(result != 0) |
| 209 | argsize = 0; |
| 210 | |
| 211 | outsize = sizeof(struct fuse_out_header) + argsize; |
| 212 | outbuf = (char *) g_malloc(outsize); |
| 213 | out = (struct fuse_out_header *) outbuf; |
| 214 | out->unique = in->unique; |
| 215 | out->result = result; |
| 216 | if(argsize != 0) |
| 217 | memcpy(outbuf + sizeof(struct fuse_out_header), arg, argsize); |
| 218 | |
| 219 | printf(" unique: %i, result: %i (%s), outsize: %i\n", out->unique, |
| 220 | out->result, strerror(-out->result), outsize); |
| 221 | |
| 222 | res = write(f->fd, outbuf, outsize); |
| 223 | if(res == -1) |
| 224 | perror("writing fuse device"); |
| 225 | |
| 226 | g_free(outbuf); |
| 227 | } |
| 228 | |
| 229 | static void do_lookup(struct fuse *f, struct fuse_in_header *in, char *name) |
| 230 | { |
| 231 | int res; |
| 232 | struct fuse_lookup_out arg; |
| 233 | |
| 234 | arg.ino = find_node(f, in->ino, name, 1); |
| 235 | res = get_attributes(f, arg.ino, &arg.attr); |
| 236 | |
| 237 | send_reply(f, in, res, &arg, sizeof(arg)); |
| 238 | } |
| 239 | |
| 240 | |
| 241 | static void do_forget(struct fuse *f, unsigned long *inos, size_t num) |
| 242 | { |
| 243 | size_t i; |
| 244 | |
| 245 | for(i = 0; i < num; i++) |
| 246 | remove_node(f, inos[i]); |
| 247 | } |
| 248 | |
| 249 | static void do_getattr(struct fuse *f, struct fuse_in_header *in) |
| 250 | { |
| 251 | int res; |
| 252 | struct fuse_getattr_out arg; |
| 253 | |
| 254 | res = get_attributes(f, in->ino, &arg.attr); |
| 255 | send_reply(f, in, res, &arg, sizeof(arg)); |
| 256 | } |
| 257 | |
| 258 | static void do_readlink(struct fuse *f, struct fuse_in_header *in) |
| 259 | { |
| 260 | int res; |
| 261 | char link[PATH_MAX + 1]; |
| 262 | |
| 263 | res = read_link(f, in->ino, link, PATH_MAX + 1); |
| 264 | send_reply(f, in, res, link, res == 0 ? strlen(link) : 0); |
| 265 | } |
| 266 | |
| 267 | static void do_mknod(struct fuse *f, struct fuse_in_header *in, |
| 268 | struct fuse_mknod_in *inarg) |
| 269 | { |
| 270 | int res; |
| 271 | struct fuse_mknod_out outarg; |
| 272 | |
| 273 | res = -ENOSYS; |
| 274 | if(f->op.mknod != NULL && f->op.getattr != NULL) { |
| 275 | char *path; |
| 276 | struct stat buf; |
| 277 | |
| 278 | outarg.ino = find_node(f, in->ino, inarg->name, 1); |
| 279 | path = get_path(outarg.ino); |
| 280 | res = f->op.mknod(path, inarg->mode, inarg->rdev); |
| 281 | if(res == 0) |
| 282 | res = f->op.getattr(path, &buf); |
| 283 | g_free(path); |
| 284 | |
| 285 | if(res == 0) |
| 286 | convert_stat(&buf, &outarg.attr); |
| 287 | else |
| 288 | remove_node(f, outarg.ino); |
| 289 | } |
| 290 | send_reply(f, in, res, &outarg, sizeof(outarg)); |
| 291 | } |
| 292 | |
| 293 | static void do_getdir(struct fuse *f, struct fuse_in_header *in) |
| 294 | { |
| 295 | int res; |
| 296 | struct fuse_getdir_out arg; |
| 297 | FILE *fp = tmpfile(); |
| 298 | |
| 299 | res = get_dir(f, in->ino, fp); |
| 300 | fflush(fp); |
| 301 | arg.fd = fileno(fp); |
| 302 | send_reply(f, in, res, &arg, sizeof(arg)); |
| 303 | fclose(fp); |
| 304 | } |
| 305 | |
| 306 | void fuse_loop(struct fuse *f) |
| 307 | { |
| 308 | int res; |
| 309 | char inbuf[FUSE_MAX_IN]; |
| 310 | struct fuse_in_header *in = (struct fuse_in_header *) inbuf; |
| 311 | void *inarg = inbuf + sizeof(struct fuse_in_header); |
| 312 | size_t insize; |
| 313 | size_t argsize; |
| 314 | |
| 315 | while(1) { |
| 316 | res = read(f->fd, inbuf, sizeof(inbuf)); |
| 317 | if(res == -1) { |
| 318 | perror("reading fuse device"); |
| 319 | continue; |
| 320 | } |
| 321 | insize = res; |
| 322 | |
| 323 | if(insize < sizeof(struct fuse_in_header)) { |
| 324 | fprintf(stderr, "short read on fuse device\n"); |
| 325 | continue; |
| 326 | } |
| 327 | printf("unique: %i, opcode: %i, ino: %li, insize: %i (%i)\n", |
| 328 | in->unique, in->opcode, in->ino, insize, |
| 329 | g_hash_table_size(f->nametab)); |
| 330 | |
| 331 | argsize = insize - sizeof(struct fuse_in_header); |
| 332 | |
| 333 | switch(in->opcode) { |
| 334 | case FUSE_LOOKUP: |
| 335 | do_lookup(f, in, (char *) inarg); |
| 336 | break; |
| 337 | |
| 338 | case FUSE_FORGET: |
| 339 | do_forget(f, (unsigned long *) inarg, |
| 340 | argsize / sizeof(unsigned long)); |
| 341 | break; |
| 342 | |
| 343 | case FUSE_GETATTR: |
| 344 | do_getattr(f, in); |
| 345 | break; |
| 346 | |
| 347 | case FUSE_READLINK: |
| 348 | do_readlink(f, in); |
| 349 | break; |
| 350 | |
| 351 | case FUSE_GETDIR: |
| 352 | do_getdir(f, in); |
| 353 | break; |
| 354 | |
| 355 | case FUSE_MKNOD: |
| 356 | do_mknod(f, in, (struct fuse_mknod_in *) inarg); |
| 357 | break; |
| 358 | |
| 359 | default: |
| 360 | fprintf(stderr, "Operation %i not implemented\n", in->opcode); |
| 361 | /* No need to send reply to async requests */ |
| 362 | if(in->unique != 0) |
| 363 | send_reply(f, in, -ENOSYS, NULL, 0); |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | struct fuse *fuse_new() |
| 369 | { |
| 370 | struct fuse *f = g_new0(struct fuse, 1); |
| 371 | |
| 372 | f->fd = -1; |
| 373 | f->dir = NULL; |
| 374 | f->nametab = g_hash_table_new((GHashFunc) name_hash, |
| 375 | (GCompareFunc) name_compare); |
| 376 | |
| 377 | return f; |
| 378 | } |
| 379 | |
| 380 | |
| 381 | void fuse_set_operations(struct fuse *f, const struct fuse_operations *op) |
| 382 | { |
| 383 | f->op = *op; |
| 384 | } |
| 385 | |
| 386 | void fuse_destroy(struct fuse *f) |
| 387 | { |
| 388 | fuse_unmount(f); |
| 389 | g_hash_table_foreach_remove(f->nametab, (GHRFunc) free_node, NULL); |
| 390 | g_hash_table_destroy(f->nametab); |
| 391 | g_free(f); |
| 392 | } |
| 393 | |