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