refuse.c revision 1.53 1 /* $NetBSD: refuse.c,v 1.53 2007/05/15 22:56:16 agc Exp $ */
2
3 /*
4 * Copyright 2007 Alistair Crooks. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote
15 * products derived from this software without specific prior written
16 * permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include <sys/cdefs.h>
32 #if !defined(lint)
33 __RCSID("$NetBSD: refuse.c,v 1.53 2007/05/15 22:56:16 agc Exp $");
34 #endif /* !lint */
35
36 #include <assert.h>
37 #include <err.h>
38 #include <errno.h>
39 #include <fuse.h>
40 #include <unistd.h>
41
42 #include "defs.h"
43
44 typedef uint64_t fuse_ino_t;
45
46 struct fuse_config {
47 uid_t uid;
48 gid_t gid;
49 mode_t umask;
50 double entry_timeout;
51 double negative_timeout;
52 double attr_timeout;
53 double ac_attr_timeout;
54 int ac_attr_timeout_set;
55 int debug;
56 int hard_remove;
57 int use_ino;
58 int readdir_ino;
59 int set_mode;
60 int set_uid;
61 int set_gid;
62 int direct_io;
63 int kernel_cache;
64 int auto_cache;
65 int intr;
66 int intr_signal;
67 };
68
69 struct fuse_chan {
70 const char *dir;
71 struct fuse_args *args;
72 struct puffs_usermount *pu;
73 };
74
75 /* this is the private fuse structure */
76 struct fuse {
77 struct fuse_chan *fc; /* fuse channel pointer */
78 struct fuse_operations op; /* switch table of operations */
79 int compat; /* compat level -
80 * not used in puffs_fuse */
81 struct node **name_table;
82 size_t name_table_size;
83 struct node **id_table;
84 size_t id_table_size;
85 fuse_ino_t ctr;
86 unsigned int generation;
87 unsigned int hidectr;
88 pthread_mutex_t lock;
89 pthread_rwlock_t tree_lock;
90 void *user_data;
91 struct fuse_config conf;
92 int intr_installed;
93 };
94
95 struct puffs_fuse_dirh {
96 void *dbuf;
97 struct dirent *d;
98
99 size_t reslen;
100 size_t bufsize;
101 };
102
103 struct refusenode {
104 struct fuse_file_info file_info;
105 struct puffs_fuse_dirh dirh;
106 int opencount;
107 int flags;
108 };
109 #define RN_ROOT 0x01
110 #define RN_OPEN 0x02 /* XXX: could just use opencount */
111
112 static int fuse_setattr(struct fuse *, struct puffs_node *,
113 const char *, const struct vattr *);
114
115 static struct puffs_node *
116 newrn(struct puffs_usermount *pu)
117 {
118 struct puffs_node *pn;
119 struct refusenode *rn;
120
121 NEW(struct refusenode, rn, "newrn", exit(EXIT_FAILURE));
122 pn = puffs_pn_new(pu, rn);
123
124 return pn;
125 }
126
127 static void
128 nukern(struct puffs_node *pn)
129 {
130 struct refusenode *rn = pn->pn_data;
131
132 free(rn->dirh.dbuf);
133 free(rn);
134 puffs_pn_put(pn);
135 }
136
137 static ino_t fakeino = 3;
138
139 /*
140 * XXX: do this otherwise if/when we grow thread support
141 *
142 * XXX2: does not consistently supply uid, gid or pid currently
143 */
144 static struct fuse_context fcon;
145
146 #define DIR_CHUNKSIZE 4096
147 static int
148 fill_dirbuf(struct puffs_fuse_dirh *dh, const char *name, ino_t dino,
149 uint8_t dtype)
150 {
151
152 /* initial? */
153 if (dh->bufsize == 0) {
154 dh->dbuf = malloc(DIR_CHUNKSIZE);
155 if (dh->dbuf == NULL)
156 err(1, "fill_dirbuf");
157 dh->d = dh->dbuf;
158 dh->reslen = dh->bufsize = DIR_CHUNKSIZE;
159 }
160
161 if (puffs_nextdent(&dh->d, name, dino, dtype, &dh->reslen))
162 return 0;
163
164 /* try to increase buffer space */
165 dh->dbuf = realloc(dh->dbuf, dh->bufsize + DIR_CHUNKSIZE);
166 if (dh->dbuf == NULL)
167 err(1, "fill_dirbuf realloc");
168 dh->d = (void *)((uint8_t *)dh->dbuf + (dh->bufsize - dh->reslen));
169 dh->reslen += DIR_CHUNKSIZE;
170 dh->bufsize += DIR_CHUNKSIZE;
171
172 return !puffs_nextdent(&dh->d, name, dino, dtype, &dh->reslen);
173 }
174
175 /* ARGSUSED3 */
176 /* XXX: I have no idea how "off" is supposed to be used */
177 static int
178 puffs_fuse_fill_dir(void *buf, const char *name,
179 const struct stat *stbuf, off_t off)
180 {
181 struct puffs_fuse_dirh *deh = buf;
182 ino_t dino;
183 uint8_t dtype;
184
185 if (stbuf == NULL) {
186 dtype = DT_UNKNOWN;
187 dino = fakeino++;
188 } else {
189 dtype = puffs_vtype2dt(puffs_mode2vt(stbuf->st_mode));
190 dino = stbuf->st_ino;
191
192 /*
193 * Some FUSE file systems like to always use 0 as the
194 * inode number. Our readdir() doesn't like to show
195 * directory entries with inode number 0 ==> workaround.
196 */
197 if (dino == 0)
198 dino = fakeino++;
199 }
200
201 return fill_dirbuf(deh, name, dino, dtype);
202 }
203
204 static int
205 puffs_fuse_dirfil(fuse_dirh_t h, const char *name, int type, ino_t ino)
206 {
207 ino_t dino;
208 int dtype;
209
210 if (type == 0)
211 dtype = DT_UNKNOWN;
212 else
213 dtype = type;
214
215 if (ino)
216 dino = ino;
217 else
218 dino = fakeino++;
219
220 return fill_dirbuf(h, name, dino, dtype);
221 }
222
223 static struct fuse_args *
224 deep_copy_args(int argc, char **argv)
225 {
226 struct fuse_args *ap;
227 int i;
228
229 NEW(struct fuse_args, ap, "deep_copy_args", return NULL);
230 /* deep copy args structure into channel args */
231 ap->allocated = ((argc / 10) + 1) * 10;
232 NEWARRAY(char *, ap->argv, ap->allocated, "deep_copy_args",
233 return NULL);
234 for (i = 0 ; i < argc ; i++) {
235 ap->argv[i] = strdup(argv[i]);
236 }
237 return ap;
238 }
239
240 static void
241 free_args(struct fuse_args *ap)
242 {
243 int i;
244
245 for (i = 0 ; i < ap->argc ; i++) {
246 free(ap->argv[i]);
247 }
248 free(ap);
249 }
250
251 static void
252 set_refuse_mount_name(char **argv, char *name, size_t size)
253 {
254 char *slash;
255
256 if (argv == NULL || *argv == NULL) {
257 (void) strlcpy(name, "refuse", size);
258 } else {
259 if ((slash = strrchr(*argv, '/')) == NULL) {
260 slash = *argv;
261 } else {
262 slash += 1;
263 }
264 if (strncmp(*argv, "refuse:", 7) == 0) {
265 /* we've already done this */
266 (void) strlcpy(name, *argv, size);
267 } else {
268 (void) snprintf(name, size, "refuse:%s", slash);
269 }
270 }
271 }
272
273
274 /* this function exposes struct fuse to userland */
275 struct fuse *
276 fuse_setup(int argc, char **argv, const struct fuse_operations *ops,
277 size_t size, char **mountpoint, int *multithreaded, int *fd)
278 {
279 struct fuse_chan *fc;
280 struct fuse_args *args;
281 struct fuse *fuse;
282 char name[64];
283 int i;
284
285 /* whilst this (assigning the pu_privdata in the puffs
286 * usermount struct to be the fuse struct) might seem like
287 * we are chasing our tail here, the logic is as follows:
288 + the operation wrapper gets called with the puffs
289 calling conventions
290 + we need to fix up args first
291 + then call the fuse user-supplied operation
292 + then we fix up any values on return that we need to
293 + and fix up any nodes, etc
294 * so we need to be able to get at the fuse ops from within the
295 * puffs_usermount struct
296 */
297 set_refuse_mount_name(argv, name, sizeof(name));
298
299 /* stuff name into fuse_args */
300 args = deep_copy_args(argc, argv);
301 if (args->argc > 0) {
302 FREE(args->argv[0]);
303 }
304 args->argv[0] = strdup(name);
305
306 for (i = argc - 1 ; i > 0 && *argv[i] == '-' ; --i) {
307 }
308
309 fc = fuse_mount(*mountpoint = argv[i], args);
310 fuse = fuse_new(fc, args, ops, size, NULL);
311
312 free_args(args);
313
314 /* XXX - wait for puffs to become multi-threaded */
315 if (multithreaded) {
316 *multithreaded = 0;
317 }
318
319 /* XXX - this is unused */
320 if (fd) {
321 *fd = 0;
322 }
323
324 return fuse;
325 }
326
327 #define FUSE_ERR_UNLINK(fuse, file) if (fuse->op.unlink) fuse->op.unlink(file)
328 #define FUSE_ERR_RMDIR(fuse, dir) if (fuse->op.rmdir) fuse->op.rmdir(dir)
329
330 /* ARGSUSED1 */
331 static int
332 fuse_getattr(struct fuse *fuse, struct puffs_node *pn, const char *path,
333 struct vattr *va)
334 {
335 struct stat st;
336 int ret;
337
338 if (fuse->op.getattr == NULL) {
339 return ENOSYS;
340 }
341
342 /* wrap up return code */
343 ret = (*fuse->op.getattr)(path, &st);
344
345 if (ret == 0) {
346 puffs_stat2vattr(va, &st);
347 }
348
349 return -ret;
350 }
351
352 static int
353 fuse_setattr(struct fuse *fuse, struct puffs_node *pn, const char *path,
354 const struct vattr *va)
355 {
356 struct refusenode *rn = pn->pn_data;
357 mode_t mode;
358 uid_t uid;
359 gid_t gid;
360 int error, ret;
361
362 error = 0;
363
364 mode = va->va_mode;
365 uid = va->va_uid;
366 gid = va->va_gid;
367
368 if (mode != (mode_t)PUFFS_VNOVAL) {
369 ret = 0;
370
371 if (fuse->op.chmod == NULL) {
372 error = -ENOSYS;
373 } else {
374 ret = fuse->op.chmod(path, mode);
375 if (ret)
376 error = ret;
377 }
378 }
379 if (uid != (uid_t)PUFFS_VNOVAL || gid != (gid_t)PUFFS_VNOVAL) {
380 ret = 0;
381
382 if (fuse->op.chown == NULL) {
383 error = -ENOSYS;
384 } else {
385 ret = fuse->op.chown(path, uid, gid);
386 if (ret)
387 error = ret;
388 }
389 }
390 if (va->va_atime.tv_sec != (time_t)PUFFS_VNOVAL
391 || va->va_mtime.tv_sec != (long)PUFFS_VNOVAL) {
392 ret = 0;
393
394 if (fuse->op.utimens) {
395 struct timespec tv[2];
396
397 tv[0].tv_sec = va->va_atime.tv_sec;
398 tv[0].tv_nsec = va->va_atime.tv_nsec;
399 tv[1].tv_sec = va->va_mtime.tv_sec;
400 tv[1].tv_nsec = va->va_mtime.tv_nsec;
401
402 ret = fuse->op.utimens(path, tv);
403 } else if (fuse->op.utime) {
404 struct utimbuf timbuf;
405
406 timbuf.actime = va->va_atime.tv_sec;
407 timbuf.modtime = va->va_mtime.tv_sec;
408
409 ret = fuse->op.utime(path, &timbuf);
410 } else {
411 error = -ENOSYS;
412 }
413
414 if (ret)
415 error = ret;
416 }
417 if (va->va_size != (u_quad_t)PUFFS_VNOVAL) {
418 ret = 0;
419
420 if (fuse->op.truncate) {
421 ret = fuse->op.truncate(path, (off_t)va->va_size);
422 } else if (fuse->op.ftruncate) {
423 ret = fuse->op.ftruncate(path, (off_t)va->va_size,
424 &rn->file_info);
425 } else {
426 error = -ENOSYS;
427 }
428
429 if (ret)
430 error = ret;
431 }
432 /* XXX: no reflection with reality */
433 puffs_setvattr(&pn->pn_va, va);
434
435 return -error;
436
437 }
438
439 static int
440 fuse_newnode(struct puffs_usermount *pu, const char *path,
441 const struct vattr *va, struct fuse_file_info *fi, void **newnode)
442 {
443 struct vattr newva;
444 struct fuse *fuse;
445 struct puffs_node *pn;
446 struct refusenode *rn;
447
448 fuse = puffs_getspecific(pu);
449
450 /* fix up nodes */
451 pn = newrn(pu);
452 if (pn == NULL) {
453 if (va->va_type == VDIR) {
454 FUSE_ERR_RMDIR(fuse, path);
455 } else {
456 FUSE_ERR_UNLINK(fuse, path);
457 }
458 return ENOMEM;
459 }
460 fuse_setattr(fuse, pn, path, va);
461 if (fuse_getattr(fuse, pn, path, &newva) == 0)
462 puffs_setvattr(&pn->pn_va, &newva);
463
464 rn = pn->pn_data;
465 if (fi)
466 memcpy(&rn->file_info, fi, sizeof(struct fuse_file_info));
467
468 *newnode = pn;
469
470 return 0;
471 }
472
473
474 /* operation wrappers start here */
475
476 /* lookup the path */
477 /* ARGSUSED1 */
478 static int
479 puffs_fuse_node_lookup(struct puffs_cc *pcc, void *opc, void **newnode,
480 enum vtype *newtype, voff_t *newsize, dev_t *newrdev,
481 const struct puffs_cn *pcn)
482 {
483 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
484 struct puffs_node *pn_res;
485 struct stat st;
486 struct fuse *fuse;
487 const char *path = PCNPATH(pcn);
488 int ret;
489
490 fuse = puffs_getspecific(pu);
491 ret = fuse->op.getattr(path, &st);
492
493 if (ret != 0) {
494 return -ret;
495 }
496
497 /* XXX: fiXXXme unconst */
498 pn_res = puffs_pn_nodewalk(pu, puffs_path_walkcmp,
499 __UNCONST(&pcn->pcn_po_full));
500 if (pn_res == NULL) {
501 pn_res = newrn(pu);
502 if (pn_res == NULL)
503 return errno;
504 puffs_stat2vattr(&pn_res->pn_va, &st);
505 }
506
507 *newnode = pn_res;
508 *newtype = pn_res->pn_va.va_type;
509 *newsize = pn_res->pn_va.va_size;
510 *newrdev = pn_res->pn_va.va_rdev;
511
512 return 0;
513 }
514
515 /* get attributes for the path name */
516 /* ARGSUSED3 */
517 static int
518 puffs_fuse_node_getattr(struct puffs_cc *pcc, void *opc, struct vattr *va,
519 const struct puffs_cred *pcr, pid_t pid)
520 {
521 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
522 struct puffs_node *pn = opc;
523 struct fuse *fuse;
524 const char *path = PNPATH(pn);
525
526 fuse = puffs_getspecific(pu);
527 return fuse_getattr(fuse, pn, path, va);
528 }
529
530 /* read the contents of the symbolic link */
531 /* ARGSUSED2 */
532 static int
533 puffs_fuse_node_readlink(struct puffs_cc *pcc, void *opc,
534 const struct puffs_cred *cred, char *linkname, size_t *linklen)
535 {
536 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
537 struct puffs_node *pn = opc;
538 struct fuse *fuse;
539 const char *path = PNPATH(pn), *p;
540 int ret;
541
542 fuse = puffs_getspecific(pu);
543 if (fuse->op.readlink == NULL) {
544 return ENOSYS;
545 }
546
547 /* wrap up return code */
548 ret = (*fuse->op.readlink)(path, linkname, *linklen);
549
550 if (ret == 0) {
551 p = memchr(linkname, '\0', *linklen);
552 if (!p)
553 return EINVAL;
554
555 *linklen = p - linkname;
556 }
557
558 return -ret;
559 }
560
561 /* make the special node */
562 /* ARGSUSED1 */
563 static int
564 puffs_fuse_node_mknod(struct puffs_cc *pcc, void *opc, void **newnode,
565 const struct puffs_cn *pcn, const struct vattr *va)
566 {
567 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
568 struct fuse *fuse;
569 mode_t mode;
570 const char *path = PCNPATH(pcn);
571 int ret;
572
573 fuse = puffs_getspecific(pu);
574 if (fuse->op.mknod == NULL) {
575 return ENOSYS;
576 }
577
578 /* wrap up return code */
579 mode = puffs_addvtype2mode(va->va_mode, va->va_type);
580 ret = (*fuse->op.mknod)(path, mode, va->va_rdev);
581
582 if (ret == 0) {
583 ret = fuse_newnode(pu, path, va, NULL, newnode);
584 }
585
586 return -ret;
587 }
588
589 /* make a directory */
590 /* ARGSUSED1 */
591 static int
592 puffs_fuse_node_mkdir(struct puffs_cc *pcc, void *opc, void **newnode,
593 const struct puffs_cn *pcn, const struct vattr *va)
594 {
595 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
596 struct fuse *fuse;
597 mode_t mode = va->va_mode;
598 const char *path = PCNPATH(pcn);
599 int ret;
600
601 fuse = puffs_getspecific(pu);
602 if (fuse->op.mkdir == NULL) {
603 return ENOSYS;
604 }
605
606 /* wrap up return code */
607 ret = (*fuse->op.mkdir)(path, mode);
608
609 if (ret == 0) {
610 ret = fuse_newnode(pu, path, va, NULL, newnode);
611 }
612
613 return -ret;
614 }
615
616 /*
617 * create a regular file
618 *
619 * since linux/fuse sports using mknod for creating regular files
620 * instead of having a separate call for it in some versions, if
621 * we don't have create, just jump to op->mknod.
622 */
623 /*ARGSUSED1*/
624 static int
625 puffs_fuse_node_create(struct puffs_cc *pcc, void *opc, void **newnode,
626 const struct puffs_cn *pcn, const struct vattr *va)
627 {
628 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
629 struct fuse *fuse;
630 struct fuse_file_info fi;
631 mode_t mode = va->va_mode;
632 const char *path = PCNPATH(pcn);
633 int ret, created;
634
635 fuse = puffs_getspecific(pu);
636
637 created = 0;
638 if (fuse->op.create) {
639 ret = fuse->op.create(path, mode, &fi);
640 if (ret == 0)
641 created = 1;
642
643 } else if (fuse->op.mknod) {
644 fcon.uid = va->va_uid; /*XXX*/
645 fcon.gid = va->va_gid; /*XXX*/
646
647 ret = fuse->op.mknod(path, mode | S_IFREG, 0);
648
649 } else {
650 ret = -ENOSYS;
651 }
652
653 if (ret == 0) {
654 ret = fuse_newnode(pu, path, va, &fi, newnode);
655
656 /* sweet.. create also open the file */
657 if (created) {
658 struct puffs_node *pn;
659 struct refusenode *rn;
660
661 pn = *newnode;
662 rn = pn->pn_data;
663 rn->flags |= RN_OPEN;
664 rn->opencount++;
665 }
666 }
667
668 return -ret;
669 }
670
671 /* remove the directory entry */
672 /* ARGSUSED1 */
673 static int
674 puffs_fuse_node_remove(struct puffs_cc *pcc, void *opc, void *targ,
675 const struct puffs_cn *pcn)
676 {
677 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
678 struct puffs_node *pn_targ = targ;
679 struct fuse *fuse;
680 const char *path = PNPATH(pn_targ);
681 int ret;
682
683 fuse = puffs_getspecific(pu);
684 if (fuse->op.unlink == NULL) {
685 return ENOSYS;
686 }
687
688 /* wrap up return code */
689 ret = (*fuse->op.unlink)(path);
690
691 return -ret;
692 }
693
694 /* remove the directory */
695 /* ARGSUSED1 */
696 static int
697 puffs_fuse_node_rmdir(struct puffs_cc *pcc, void *opc, void *targ,
698 const struct puffs_cn *pcn)
699 {
700 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
701 struct puffs_node *pn_targ = targ;
702 struct fuse *fuse;
703 const char *path = PNPATH(pn_targ);
704 int ret;
705
706 fuse = puffs_getspecific(pu);
707 if (fuse->op.rmdir == NULL) {
708 return ENOSYS;
709 }
710
711 /* wrap up return code */
712 ret = (*fuse->op.rmdir)(path);
713
714 return -ret;
715 }
716
717 /* create a symbolic link */
718 /* ARGSUSED1 */
719 static int
720 puffs_fuse_node_symlink(struct puffs_cc *pcc, void *opc, void **newnode,
721 const struct puffs_cn *pcn_src, const struct vattr *va,
722 const char *link_target)
723 {
724 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
725 struct fuse *fuse;
726 const char *path = PCNPATH(pcn_src);
727 int ret;
728
729 fuse = puffs_getspecific(pu);
730 if (fuse->op.symlink == NULL) {
731 return ENOSYS;
732 }
733
734 /* wrap up return code */
735 ret = fuse->op.symlink(link_target, path);
736
737 if (ret == 0) {
738 ret = fuse_newnode(pu, path, va, NULL, newnode);
739 }
740
741 return -ret;
742 }
743
744 /* rename a directory entry */
745 /* ARGSUSED1 */
746 static int
747 puffs_fuse_node_rename(struct puffs_cc *pcc, void *opc, void *src,
748 const struct puffs_cn *pcn_src, void *targ_dir, void *targ,
749 const struct puffs_cn *pcn_targ)
750 {
751 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
752 struct fuse *fuse;
753 const char *path_src = PCNPATH(pcn_src);
754 const char *path_dest = PCNPATH(pcn_targ);
755 int ret;
756
757 fuse = puffs_getspecific(pu);
758 if (fuse->op.rename == NULL) {
759 return ENOSYS;
760 }
761
762 ret = fuse->op.rename(path_src, path_dest);
763
764 if (ret == 0) {
765 }
766
767 return -ret;
768 }
769
770 /* create a link in the file system */
771 /* ARGSUSED1 */
772 static int
773 puffs_fuse_node_link(struct puffs_cc *pcc, void *opc, void *targ,
774 const struct puffs_cn *pcn)
775 {
776 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
777 struct puffs_node *pn = targ;
778 struct fuse *fuse;
779 int ret;
780
781 fuse = puffs_getspecific(pu);
782 if (fuse->op.link == NULL) {
783 return ENOSYS;
784 }
785
786 /* wrap up return code */
787 ret = (*fuse->op.link)(PNPATH(pn), PCNPATH(pcn));
788
789 return -ret;
790 }
791
792 /*
793 * fuse's regular interface provides chmod(), chown(), utimes()
794 * and truncate() + some variations, so try to fit the square block
795 * in the circle hole and the circle block .... something like that
796 */
797 /* ARGSUSED3 */
798 static int
799 puffs_fuse_node_setattr(struct puffs_cc *pcc, void *opc,
800 const struct vattr *va, const struct puffs_cred *pcr, pid_t pid)
801 {
802 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
803 struct puffs_node *pn = opc;
804 struct fuse *fuse;
805 const char *path = PNPATH(pn);
806
807 fuse = puffs_getspecific(pu);
808
809 return fuse_setattr(fuse, pn, path, va);
810 }
811
812 /* ARGSUSED2 */
813 static int
814 puffs_fuse_node_open(struct puffs_cc *pcc, void *opc, int mode,
815 const struct puffs_cred *cred, pid_t pid)
816 {
817 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
818 struct puffs_node *pn = opc;
819 struct refusenode *rn = pn->pn_data;
820 struct fuse_file_info *fi = &rn->file_info;
821 struct fuse *fuse;
822 const char *path = PNPATH(pn);
823
824 fuse = puffs_getspecific(pu);
825
826 /* if open, don't open again, lest risk nuking file private info */
827 if (rn->flags & RN_OPEN) {
828 rn->opencount++;
829 return 0;
830 }
831
832 /* OFLAGS(), need to convert FREAD/FWRITE to O_RD/WR */
833 fi->flags = (mode & ~(O_CREAT | O_EXCL | O_TRUNC)) - 1;
834
835 if (pn->pn_va.va_type == VDIR) {
836 if (fuse->op.opendir)
837 fuse->op.opendir(path, fi);
838 } else {
839 if (fuse->op.open)
840 fuse->op.open(path, fi);
841 }
842
843 rn->flags |= RN_OPEN;
844 rn->opencount++;
845
846 return 0;
847 }
848
849 /* ARGSUSED2 */
850 static int
851 puffs_fuse_node_close(struct puffs_cc *pcc, void *opc, int fflag,
852 const struct puffs_cred *pcr, pid_t pid)
853 {
854 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
855 struct puffs_node *pn = opc;
856 struct refusenode *rn = pn->pn_data;
857 struct fuse *fuse;
858 struct fuse_file_info *fi;
859 const char *path = PNPATH(pn);
860 int ret;
861
862 fuse = puffs_getspecific(pu);
863 fi = &rn->file_info;
864 ret = 0;
865
866 if (rn->flags & RN_OPEN) {
867 if (pn->pn_va.va_type == VDIR) {
868 if (fuse->op.releasedir)
869 ret = fuse->op.releasedir(path, fi);
870 } else {
871 if (fuse->op.release)
872 ret = fuse->op.release(path, fi);
873 }
874 }
875 rn->flags &= ~RN_OPEN;
876 rn->opencount--;
877
878 return ret;
879 }
880
881 /* read some more from the file */
882 /* ARGSUSED5 */
883 static int
884 puffs_fuse_node_read(struct puffs_cc *pcc, void *opc, uint8_t *buf,
885 off_t offset, size_t *resid, const struct puffs_cred *pcr,
886 int ioflag)
887 {
888 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
889 struct puffs_node *pn = opc;
890 struct refusenode *rn = pn->pn_data;
891 struct fuse *fuse;
892 const char *path = PNPATH(pn);
893 size_t maxread;
894 int ret;
895
896 fuse = puffs_getspecific(pu);
897 if (fuse->op.read == NULL) {
898 return ENOSYS;
899 }
900
901 maxread = *resid;
902 if (maxread > pn->pn_va.va_size - offset) {
903 /*LINTED*/
904 maxread = pn->pn_va.va_size - offset;
905 }
906 if (maxread == 0)
907 return 0;
908
909 ret = (*fuse->op.read)(path, (char *)buf, maxread, offset,
910 &rn->file_info);
911
912 if (ret > 0) {
913 *resid -= ret;
914 ret = 0;
915 }
916
917 return -ret;
918 }
919
920 /* write to the file */
921 /* ARGSUSED0 */
922 static int
923 puffs_fuse_node_write(struct puffs_cc *pcc, void *opc, uint8_t *buf,
924 off_t offset, size_t *resid, const struct puffs_cred *pcr,
925 int ioflag)
926 {
927 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
928 struct puffs_node *pn = opc;
929 struct refusenode *rn = pn->pn_data;
930 struct fuse *fuse;
931 const char *path = PNPATH(pn);
932 int ret;
933
934 fuse = puffs_getspecific(pu);
935 if (fuse->op.write == NULL) {
936 return ENOSYS;
937 }
938
939 if (ioflag & PUFFS_IO_APPEND)
940 offset = pn->pn_va.va_size;
941
942 ret = (*fuse->op.write)(path, (char *)buf, *resid, offset,
943 &rn->file_info);
944
945 if (ret > 0) {
946 if (offset + ret > pn->pn_va.va_size)
947 pn->pn_va.va_size = offset + ret;
948 *resid -= ret;
949 ret = 0;
950 }
951
952 return -ret;
953 }
954
955
956 /* ARGSUSED3 */
957 static int
958 puffs_fuse_node_readdir(struct puffs_cc *pcc, void *opc, struct dirent *dent,
959 off_t *readoff, size_t *reslen, const struct puffs_cred *pcr,
960 int *eofflag, off_t *cookies, size_t *ncookies)
961 {
962 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
963 struct puffs_node *pn = opc;
964 struct refusenode *rn = pn->pn_data;
965 struct puffs_fuse_dirh *dirh;
966 struct fuse *fuse;
967 struct dirent *fromdent;
968 const char *path = PNPATH(pn);
969 int ret;
970
971 fuse = puffs_getspecific(pu);
972 if (fuse->op.readdir == NULL && fuse->op.getdir == NULL) {
973 return ENOSYS;
974 }
975
976 if (pn->pn_va.va_type != VDIR)
977 return ENOTDIR;
978
979 dirh = &rn->dirh;
980
981 /*
982 * if we are starting from the beginning, slurp entire directory
983 * into our buffers
984 */
985 if (*readoff == 0) {
986 /* free old buffers */
987 free(dirh->dbuf);
988 memset(dirh, 0, sizeof(struct puffs_fuse_dirh));
989
990 if (fuse->op.readdir)
991 ret = fuse->op.readdir(path, dirh, puffs_fuse_fill_dir,
992 0, &rn->file_info);
993 else
994 ret = fuse->op.getdir(path, dirh, puffs_fuse_dirfil);
995 if (ret)
996 return -ret;
997 }
998
999 /* now, stuff results into the kernel buffers */
1000 while (*readoff < dirh->bufsize - dirh->reslen) {
1001 /*LINTED*/
1002 fromdent = (struct dirent *)((uint8_t *)dirh->dbuf + *readoff);
1003
1004 if (*reslen < _DIRENT_SIZE(fromdent))
1005 break;
1006
1007 memcpy(dent, fromdent, _DIRENT_SIZE(fromdent));
1008 *readoff += _DIRENT_SIZE(fromdent);
1009 *reslen -= _DIRENT_SIZE(fromdent);
1010
1011 dent = _DIRENT_NEXT(dent);
1012 }
1013
1014 return 0;
1015 }
1016
1017 /* ARGSUSED */
1018 static int
1019 puffs_fuse_node_reclaim(struct puffs_cc *pcc, void *opc, pid_t pid)
1020 {
1021 struct puffs_node *pn = opc;
1022
1023 nukern(pn);
1024
1025 return 0;
1026 }
1027
1028 /* ARGSUSED1 */
1029 static int
1030 puffs_fuse_fs_unmount(struct puffs_cc *pcc, int flags, pid_t pid)
1031 {
1032 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
1033 struct fuse *fuse;
1034
1035 fuse = puffs_getspecific(pu);
1036 if (fuse->op.destroy == NULL) {
1037 return 0;
1038 }
1039 (*fuse->op.destroy)(fuse);
1040 return 0;
1041 }
1042
1043 /* ARGSUSED0 */
1044 static int
1045 puffs_fuse_fs_sync(struct puffs_cc *pcc, int flags,
1046 const struct puffs_cred *cr, pid_t pid)
1047 {
1048 return 0;
1049 }
1050
1051 /* ARGSUSED2 */
1052 static int
1053 puffs_fuse_fs_statvfs(struct puffs_cc *pcc, struct statvfs *svfsb, pid_t pid)
1054 {
1055 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
1056 struct fuse *fuse;
1057 int ret;
1058
1059 fuse = puffs_getspecific(pu);
1060 if (fuse->op.statfs == NULL) {
1061 if ((ret = statvfs(PNPATH(puffs_getroot(pu)), svfsb)) == -1) {
1062 return errno;
1063 }
1064 } else {
1065 ret = fuse->op.statfs(PNPATH(puffs_getroot(pu)), svfsb);
1066 }
1067
1068 return ret;
1069 }
1070
1071
1072 /* End of puffs_fuse operations */
1073 /* ARGSUSED3 */
1074 int
1075 fuse_main_real(int argc, char **argv, const struct fuse_operations *ops,
1076 size_t size, void *userdata)
1077 {
1078 struct fuse *fuse;
1079 char *mountpoint;
1080 int multithreaded;
1081 int fd;
1082
1083 fuse = fuse_setup(argc, argv, ops, size, &mountpoint, &multithreaded,
1084 &fd);
1085
1086 return fuse_loop(fuse);
1087 }
1088
1089 /*
1090 * XXX: just defer the operation until fuse_new() when we have more
1091 * info on our hands. The real beef is why's this separate in fuse in
1092 * the first place?
1093 */
1094 /* ARGSUSED1 */
1095 struct fuse_chan *
1096 fuse_mount(const char *dir, struct fuse_args *args)
1097 {
1098 struct fuse_chan *fc;
1099 char name[64];
1100
1101 NEW(struct fuse_chan, fc, "fuse_mount", exit(EXIT_FAILURE));
1102
1103 fc->dir = strdup(dir);
1104
1105 /*
1106 * we need to deep copy the args struct - some fuse file
1107 * systems "clean up" the argument vector for "security
1108 * reasons"
1109 */
1110 fc->args = deep_copy_args(args->argc, args->argv);
1111
1112 if (args->argc > 0) {
1113 set_refuse_mount_name(args->argv, name, sizeof(name));
1114 args->argv[0] = strdup(name);
1115 }
1116
1117 return fc;
1118 }
1119
1120 /* ARGSUSED1 */
1121 struct fuse *
1122 fuse_new(struct fuse_chan *fc, struct fuse_args *args,
1123 const struct fuse_operations *ops, size_t size, void *userdata)
1124 {
1125 struct puffs_usermount *pu;
1126 struct puffs_pathobj *po_root;
1127 struct puffs_node *pn_root;
1128 struct puffs_ops *pops;
1129 struct refusenode *rn_root;
1130 struct statvfs svfsb;
1131 struct stat st;
1132 struct fuse *fuse;
1133 char name[64];
1134 char *argv0;
1135
1136 NEW(struct fuse, fuse, "fuse_new", exit(EXIT_FAILURE));
1137
1138 /* copy fuse ops to their own stucture */
1139 (void) memcpy(&fuse->op, ops, sizeof(fuse->op));
1140
1141 fcon.fuse = fuse;
1142 fcon.private_data = userdata;
1143
1144 fuse->fc = fc;
1145
1146 /* initialise the puffs operations structure */
1147 PUFFSOP_INIT(pops);
1148
1149 PUFFSOP_SET(pops, puffs_fuse, fs, sync);
1150 PUFFSOP_SET(pops, puffs_fuse, fs, statvfs);
1151 PUFFSOP_SET(pops, puffs_fuse, fs, unmount);
1152
1153 /*
1154 * XXX: all of these don't possibly need to be
1155 * unconditionally set
1156 */
1157 PUFFSOP_SET(pops, puffs_fuse, node, lookup);
1158 PUFFSOP_SET(pops, puffs_fuse, node, getattr);
1159 PUFFSOP_SET(pops, puffs_fuse, node, setattr);
1160 PUFFSOP_SET(pops, puffs_fuse, node, readdir);
1161 PUFFSOP_SET(pops, puffs_fuse, node, readlink);
1162 PUFFSOP_SET(pops, puffs_fuse, node, mknod);
1163 PUFFSOP_SET(pops, puffs_fuse, node, create);
1164 PUFFSOP_SET(pops, puffs_fuse, node, remove);
1165 PUFFSOP_SET(pops, puffs_fuse, node, mkdir);
1166 PUFFSOP_SET(pops, puffs_fuse, node, rmdir);
1167 PUFFSOP_SET(pops, puffs_fuse, node, symlink);
1168 PUFFSOP_SET(pops, puffs_fuse, node, rename);
1169 PUFFSOP_SET(pops, puffs_fuse, node, link);
1170 PUFFSOP_SET(pops, puffs_fuse, node, open);
1171 PUFFSOP_SET(pops, puffs_fuse, node, close);
1172 PUFFSOP_SET(pops, puffs_fuse, node, read);
1173 PUFFSOP_SET(pops, puffs_fuse, node, write);
1174 PUFFSOP_SET(pops, puffs_fuse, node, reclaim);
1175
1176 argv0 = (*args->argv[0] == 0x0) ? fc->args->argv[0] : args->argv[0];
1177 set_refuse_mount_name(&argv0, name, sizeof(name));
1178
1179 pu = puffs_mount(pops, fc->dir, MNT_NODEV | MNT_NOSUID,
1180 name, fuse,
1181 PUFFS_FLAG_BUILDPATH
1182 | PUFFS_FLAG_HASHPATH
1183 | PUFFS_FLAG_OPDUMP
1184 | PUFFS_KFLAG_NOCACHE);
1185 if (pu == NULL) {
1186 err(EXIT_FAILURE, "puffs_mount: directory \"%s\"", fc->dir);
1187 }
1188 fc->pu = pu;
1189
1190 pn_root = newrn(pu);
1191 puffs_setroot(pu, pn_root);
1192 rn_root = pn_root->pn_data;
1193 rn_root->flags |= RN_ROOT;
1194
1195 po_root = puffs_getrootpathobj(pu);
1196 po_root->po_path = strdup("/");
1197 po_root->po_len = 1;
1198
1199 /* sane defaults */
1200 puffs_vattr_null(&pn_root->pn_va);
1201 pn_root->pn_va.va_type = VDIR;
1202 pn_root->pn_va.va_mode = 0755;
1203 if (fuse->op.getattr)
1204 if (fuse->op.getattr(po_root->po_path, &st) == 0)
1205 puffs_stat2vattr(&pn_root->pn_va, &st);
1206 assert(pn_root->pn_va.va_type == VDIR);
1207
1208 if (fuse->op.init)
1209 fcon.private_data = fuse->op.init(NULL); /* XXX */
1210
1211 puffs_zerostatvfs(&svfsb);
1212 if (puffs_start(pu, pn_root, &svfsb) == -1) {
1213 err(EXIT_FAILURE, "puffs_start");
1214 }
1215
1216 return fuse;
1217 }
1218
1219 int
1220 fuse_loop(struct fuse *fuse)
1221 {
1222
1223 return puffs_mainloop(fuse->fc->pu, PUFFSLOOP_NODAEMON);
1224 }
1225
1226 void
1227 fuse_destroy(struct fuse *fuse)
1228 {
1229
1230
1231 /* XXXXXX: missing stuff */
1232 FREE(fuse);
1233 }
1234
1235 /* XXX: threads */
1236 struct fuse_context *
1237 fuse_get_context()
1238 {
1239
1240 return &fcon;
1241 }
1242
1243 void
1244 fuse_exit(struct fuse *fuse)
1245 {
1246
1247 puffs_exit(fuse->fc->pu, 1);
1248 }
1249
1250 /*
1251 * XXX: obviously not the most perfect of functions, but needs some
1252 * puffs tweaking for a better tomorrow
1253 */
1254 /*ARGSUSED*/
1255 void
1256 fuse_unmount(const char *mp, struct fuse_chan *fc)
1257 {
1258
1259 puffs_exit(fc->pu, 1);
1260 }
1261
1262 /*ARGSUSED*/
1263 void
1264 fuse_unmount_compat22(const char *mp)
1265 {
1266
1267 return;
1268 }
1269
1270 /* The next function "exposes" struct fuse to userland. Not much
1271 * that we can do about this, as we're conforming to a defined
1272 * interface. */
1273
1274 void
1275 fuse_teardown(struct fuse *fuse, char *mountpoint)
1276 {
1277 fuse_unmount(mountpoint, fuse->fc);
1278 fuse_destroy(fuse);
1279 }
1280