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