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