refuse.c revision 1.75 1 /* $NetBSD: refuse.c,v 1.75 2007/08/25 12:03:59 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.75 2007/08/25 12:03:59 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(const struct puffs_cid *pcid)
245 {
246 struct fuse_context *fusectx;
247
248 fusectx = fuse_get_context();
249 puffs_cid_getpid(pcid, &fusectx->pid);
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 set_fuse_context_pid(pcid);
617
618 return fuse_getattr(fuse, pn, path, va);
619 }
620
621 /* read the contents of the symbolic link */
622 /* ARGSUSED2 */
623 static int
624 puffs_fuse_node_readlink(struct puffs_cc *pcc, void *opc,
625 const struct puffs_cred *cred, char *linkname, size_t *linklen)
626 {
627 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
628 struct puffs_node *pn = opc;
629 struct fuse *fuse;
630 const char *path = PNPATH(pn), *p;
631 int ret;
632
633 fuse = puffs_getspecific(pu);
634 if (fuse->op.readlink == NULL) {
635 return ENOSYS;
636 }
637
638 set_fuse_context_uid_gid(cred);
639
640 /* wrap up return code */
641 ret = (*fuse->op.readlink)(path, linkname, *linklen);
642
643 if (ret == 0) {
644 p = memchr(linkname, '\0', *linklen);
645 if (!p)
646 return EINVAL;
647
648 *linklen = p - linkname;
649 }
650
651 return -ret;
652 }
653
654 /* make the special node */
655 /* ARGSUSED1 */
656 static int
657 puffs_fuse_node_mknod(struct puffs_cc *pcc, void *opc,
658 struct puffs_newinfo *pni, const struct puffs_cn *pcn,
659 const struct vattr *va)
660 {
661 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
662 struct fuse *fuse;
663 mode_t mode;
664 const char *path = PCNPATH(pcn);
665 int ret;
666
667 fuse = puffs_getspecific(pu);
668 if (fuse->op.mknod == NULL) {
669 return ENOSYS;
670 }
671
672 set_fuse_context_uid_gid(pcn->pcn_cred);
673 set_fuse_context_pid(pcn->pcn_cid);
674
675 /* wrap up return code */
676 mode = puffs_addvtype2mode(va->va_mode, va->va_type);
677 ret = (*fuse->op.mknod)(path, mode, va->va_rdev);
678
679 if (ret == 0) {
680 ret = fuse_newnode(pu, path, va, NULL, pni, NULL);
681 }
682
683 return -ret;
684 }
685
686 /* make a directory */
687 /* ARGSUSED1 */
688 static int
689 puffs_fuse_node_mkdir(struct puffs_cc *pcc, void *opc,
690 struct puffs_newinfo *pni, const struct puffs_cn *pcn,
691 const struct vattr *va)
692 {
693 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
694 struct fuse *fuse;
695 mode_t mode = va->va_mode;
696 const char *path = PCNPATH(pcn);
697 int ret;
698
699 fuse = puffs_getspecific(pu);
700
701 set_fuse_context_uid_gid(pcn->pcn_cred);
702 set_fuse_context_pid(pcn->pcn_cid);
703
704 if (fuse->op.mkdir == NULL) {
705 return ENOSYS;
706 }
707
708 /* wrap up return code */
709 ret = (*fuse->op.mkdir)(path, mode);
710
711 if (ret == 0) {
712 ret = fuse_newnode(pu, path, va, NULL, pni, NULL);
713 }
714
715 return -ret;
716 }
717
718 /*
719 * create a regular file
720 *
721 * since linux/fuse sports using mknod for creating regular files
722 * instead of having a separate call for it in some versions, if
723 * we don't have create, just jump to op->mknod.
724 */
725 /*ARGSUSED1*/
726 static int
727 puffs_fuse_node_create(struct puffs_cc *pcc, void *opc,
728 struct puffs_newinfo *pni, const struct puffs_cn *pcn,
729 const struct vattr *va)
730 {
731 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
732 struct fuse *fuse;
733 struct fuse_file_info fi;
734 struct puffs_node *pn;
735 mode_t mode = va->va_mode;
736 const char *path = PCNPATH(pcn);
737 int ret, created;
738
739 fuse = puffs_getspecific(pu);
740
741 set_fuse_context_uid_gid(pcn->pcn_cred);
742 set_fuse_context_pid(pcn->pcn_cid);
743
744 created = 0;
745 if (fuse->op.create) {
746 ret = fuse->op.create(path, mode, &fi);
747 if (ret == 0)
748 created = 1;
749
750 } else if (fuse->op.mknod) {
751 ret = fuse->op.mknod(path, mode | S_IFREG, 0);
752
753 } else {
754 ret = -ENOSYS;
755 }
756
757 if (ret == 0) {
758 ret = fuse_newnode(pu, path, va, &fi, pni, &pn);
759
760 /* sweet.. create also open the file */
761 if (created) {
762 struct refusenode *rn;
763
764 rn = pn->pn_data;
765 rn->flags |= RN_OPEN;
766 rn->opencount++;
767 }
768 }
769
770 return -ret;
771 }
772
773 /* remove the directory entry */
774 /* ARGSUSED1 */
775 static int
776 puffs_fuse_node_remove(struct puffs_cc *pcc, void *opc, void *targ,
777 const struct puffs_cn *pcn)
778 {
779 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
780 struct puffs_node *pn_targ = targ;
781 struct fuse *fuse;
782 const char *path = PNPATH(pn_targ);
783 int ret;
784
785 fuse = puffs_getspecific(pu);
786
787 set_fuse_context_uid_gid(pcn->pcn_cred);
788 set_fuse_context_pid(pcn->pcn_cid);
789
790 if (fuse->op.unlink == NULL) {
791 return ENOSYS;
792 }
793
794 /* wrap up return code */
795 ret = (*fuse->op.unlink)(path);
796
797 return -ret;
798 }
799
800 /* remove the directory */
801 /* ARGSUSED1 */
802 static int
803 puffs_fuse_node_rmdir(struct puffs_cc *pcc, void *opc, void *targ,
804 const struct puffs_cn *pcn)
805 {
806 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
807 struct puffs_node *pn_targ = targ;
808 struct fuse *fuse;
809 const char *path = PNPATH(pn_targ);
810 int ret;
811
812 fuse = puffs_getspecific(pu);
813
814 set_fuse_context_uid_gid(pcn->pcn_cred);
815 set_fuse_context_pid(pcn->pcn_cid);
816
817 if (fuse->op.rmdir == NULL) {
818 return ENOSYS;
819 }
820
821 /* wrap up return code */
822 ret = (*fuse->op.rmdir)(path);
823
824 return -ret;
825 }
826
827 /* create a symbolic link */
828 /* ARGSUSED1 */
829 static int
830 puffs_fuse_node_symlink(struct puffs_cc *pcc, void *opc,
831 struct puffs_newinfo *pni, const struct puffs_cn *pcn_src,
832 const struct vattr *va, const char *link_target)
833 {
834 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
835 struct fuse *fuse;
836 const char *path = PCNPATH(pcn_src);
837 int ret;
838
839 fuse = puffs_getspecific(pu);
840
841 set_fuse_context_uid_gid(pcn_src->pcn_cred);
842 set_fuse_context_pid(pcn_src->pcn_cid);
843
844 if (fuse->op.symlink == NULL) {
845 return ENOSYS;
846 }
847
848 /* wrap up return code */
849 ret = fuse->op.symlink(link_target, path);
850
851 if (ret == 0) {
852 ret = fuse_newnode(pu, path, va, NULL, pni, NULL);
853 }
854
855 return -ret;
856 }
857
858 /* rename a directory entry */
859 /* ARGSUSED1 */
860 static int
861 puffs_fuse_node_rename(struct puffs_cc *pcc, void *opc, void *src,
862 const struct puffs_cn *pcn_src, void *targ_dir, void *targ,
863 const struct puffs_cn *pcn_targ)
864 {
865 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
866 struct fuse *fuse;
867 const char *path_src = PCNPATH(pcn_src);
868 const char *path_dest = PCNPATH(pcn_targ);
869 int ret;
870
871 fuse = puffs_getspecific(pu);
872
873 set_fuse_context_uid_gid(pcn_targ->pcn_cred);
874 set_fuse_context_pid(pcn_targ->pcn_cid);
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 set_fuse_context_pid(pcn->pcn_cid);
903
904 if (fuse->op.link == NULL) {
905 return ENOSYS;
906 }
907
908 /* wrap up return code */
909 ret = (*fuse->op.link)(PNPATH(pn), PCNPATH(pcn));
910
911 return -ret;
912 }
913
914 /*
915 * fuse's regular interface provides chmod(), chown(), utimes()
916 * and truncate() + some variations, so try to fit the square block
917 * in the circle hole and the circle block .... something like that
918 */
919 /* ARGSUSED3 */
920 static int
921 puffs_fuse_node_setattr(struct puffs_cc *pcc, void *opc,
922 const struct vattr *va, const struct puffs_cred *pcr,
923 const struct puffs_cid *pcid)
924 {
925 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
926 struct puffs_node *pn = opc;
927 struct fuse *fuse;
928 const char *path = PNPATH(pn);
929
930 fuse = puffs_getspecific(pu);
931
932 set_fuse_context_uid_gid(pcr);
933 set_fuse_context_pid(pcid);
934
935 return fuse_setattr(fuse, pn, path, va);
936 }
937
938 /* ARGSUSED2 */
939 static int
940 puffs_fuse_node_open(struct puffs_cc *pcc, void *opc, int mode,
941 const struct puffs_cred *cred, const struct puffs_cid *pcid)
942 {
943 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
944 struct puffs_node *pn = opc;
945 struct refusenode *rn = pn->pn_data;
946 struct fuse_file_info *fi = &rn->file_info;
947 struct fuse *fuse;
948 const char *path = PNPATH(pn);
949
950 fuse = puffs_getspecific(pu);
951
952 set_fuse_context_uid_gid(cred);
953 set_fuse_context_pid(pcid);
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 set_fuse_context_pid(pcid);
997
998 if (rn->flags & RN_OPEN) {
999 if (pn->pn_va.va_type == VDIR) {
1000 if (fuse->op.releasedir)
1001 ret = fuse->op.releasedir(path, fi);
1002 } else {
1003 if (fuse->op.release)
1004 ret = fuse->op.release(path, fi);
1005 }
1006 }
1007 rn->flags &= ~RN_OPEN;
1008 rn->opencount--;
1009
1010 return ret;
1011 }
1012
1013 /* read some more from the file */
1014 /* ARGSUSED5 */
1015 static int
1016 puffs_fuse_node_read(struct puffs_cc *pcc, void *opc, uint8_t *buf,
1017 off_t offset, size_t *resid, const struct puffs_cred *pcr,
1018 int ioflag)
1019 {
1020 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
1021 struct puffs_node *pn = opc;
1022 struct refusenode *rn = pn->pn_data;
1023 struct fuse *fuse;
1024 const char *path = PNPATH(pn);
1025 size_t maxread;
1026 int ret;
1027
1028 fuse = puffs_getspecific(pu);
1029 if (fuse->op.read == NULL) {
1030 return ENOSYS;
1031 }
1032
1033 set_fuse_context_uid_gid(pcr);
1034
1035 maxread = *resid;
1036 if (maxread > pn->pn_va.va_size - offset) {
1037 /*LINTED*/
1038 maxread = pn->pn_va.va_size - offset;
1039 }
1040 if (maxread == 0)
1041 return 0;
1042
1043 ret = (*fuse->op.read)(path, (char *)buf, maxread, offset,
1044 &rn->file_info);
1045
1046 if (ret > 0) {
1047 *resid -= ret;
1048 ret = 0;
1049 }
1050
1051 return -ret;
1052 }
1053
1054 /* write to the file */
1055 /* ARGSUSED0 */
1056 static int
1057 puffs_fuse_node_write(struct puffs_cc *pcc, void *opc, uint8_t *buf,
1058 off_t offset, size_t *resid, const struct puffs_cred *pcr,
1059 int ioflag)
1060 {
1061 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
1062 struct puffs_node *pn = opc;
1063 struct refusenode *rn = pn->pn_data;
1064 struct fuse *fuse;
1065 const char *path = PNPATH(pn);
1066 int ret;
1067
1068 fuse = puffs_getspecific(pu);
1069 if (fuse->op.write == NULL) {
1070 return ENOSYS;
1071 }
1072
1073 set_fuse_context_uid_gid(pcr);
1074
1075 if (ioflag & PUFFS_IO_APPEND)
1076 offset = pn->pn_va.va_size;
1077
1078 ret = (*fuse->op.write)(path, (char *)buf, *resid, offset,
1079 &rn->file_info);
1080
1081 if (ret > 0) {
1082 if (offset + ret > pn->pn_va.va_size)
1083 pn->pn_va.va_size = offset + ret;
1084 *resid -= ret;
1085 ret = 0;
1086 }
1087
1088 return -ret;
1089 }
1090
1091
1092 /* ARGSUSED3 */
1093 static int
1094 puffs_fuse_node_readdir(struct puffs_cc *pcc, void *opc, struct dirent *dent,
1095 off_t *readoff, size_t *reslen, const struct puffs_cred *pcr,
1096 int *eofflag, off_t *cookies, size_t *ncookies)
1097 {
1098 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
1099 struct puffs_node *pn = opc;
1100 struct refusenode *rn = pn->pn_data;
1101 struct puffs_fuse_dirh *dirh;
1102 struct fuse *fuse;
1103 struct dirent *fromdent;
1104 const char *path = PNPATH(pn);
1105 int ret;
1106
1107 fuse = puffs_getspecific(pu);
1108 if (fuse->op.readdir == NULL && fuse->op.getdir == NULL) {
1109 return ENOSYS;
1110 }
1111
1112 set_fuse_context_uid_gid(pcr);
1113
1114 if (pn->pn_va.va_type != VDIR)
1115 return ENOTDIR;
1116
1117 dirh = &rn->dirh;
1118
1119 /*
1120 * if we are starting from the beginning, slurp entire directory
1121 * into our buffers
1122 */
1123 if (*readoff == 0) {
1124 /* free old buffers */
1125 free(dirh->dbuf);
1126 memset(dirh, 0, sizeof(struct puffs_fuse_dirh));
1127
1128 if (fuse->op.readdir)
1129 ret = fuse->op.readdir(path, dirh, puffs_fuse_fill_dir,
1130 0, &rn->file_info);
1131 else
1132 ret = fuse->op.getdir(path, dirh, puffs_fuse_dirfil);
1133 if (ret)
1134 return -ret;
1135 }
1136
1137 /* now, stuff results into the kernel buffers */
1138 while (*readoff < dirh->bufsize - dirh->reslen) {
1139 /*LINTED*/
1140 fromdent = (struct dirent *)((uint8_t *)dirh->dbuf + *readoff);
1141
1142 if (*reslen < _DIRENT_SIZE(fromdent))
1143 break;
1144
1145 memcpy(dent, fromdent, _DIRENT_SIZE(fromdent));
1146 *readoff += _DIRENT_SIZE(fromdent);
1147 *reslen -= _DIRENT_SIZE(fromdent);
1148
1149 dent = _DIRENT_NEXT(dent);
1150 }
1151
1152 return 0;
1153 }
1154
1155 /* ARGSUSED */
1156 static int
1157 puffs_fuse_node_reclaim(struct puffs_cc *pcc, void *opc,
1158 const struct puffs_cid *pcid)
1159 {
1160 struct puffs_node *pn = opc;
1161
1162 nukern(pn);
1163
1164 set_fuse_context_pid(pcid);
1165
1166 return 0;
1167 }
1168
1169 /* ARGSUSED1 */
1170 static int
1171 puffs_fuse_fs_unmount(struct puffs_cc *pcc, int flags,
1172 const struct puffs_cid *pcid)
1173 {
1174 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
1175 struct fuse *fuse;
1176
1177 fuse = puffs_getspecific(pu);
1178 set_fuse_context_pid(pcid);
1179 if (fuse->op.destroy == NULL) {
1180 return 0;
1181 }
1182 (*fuse->op.destroy)(fuse);
1183 return 0;
1184 }
1185
1186 /* ARGSUSED0 */
1187 static int
1188 puffs_fuse_fs_sync(struct puffs_cc *pcc, int flags,
1189 const struct puffs_cred *cr, const struct puffs_cid *pcid)
1190 {
1191 set_fuse_context_uid_gid(cr);
1192 set_fuse_context_pid(pcid);
1193 return 0;
1194 }
1195
1196 /* ARGSUSED2 */
1197 static int
1198 puffs_fuse_fs_statvfs(struct puffs_cc *pcc, struct statvfs *svfsb,
1199 const struct puffs_cid *pcid)
1200 {
1201 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
1202 struct fuse *fuse;
1203 int ret;
1204
1205 fuse = puffs_getspecific(pu);
1206 set_fuse_context_pid(pcid);
1207 if (fuse->op.statfs == NULL) {
1208 if ((ret = statvfs(PNPATH(puffs_getroot(pu)), svfsb)) == -1) {
1209 return errno;
1210 }
1211 } else {
1212 ret = fuse->op.statfs(PNPATH(puffs_getroot(pu)), svfsb);
1213 }
1214
1215 return ret;
1216 }
1217
1218
1219 /* End of puffs_fuse operations */
1220 /* ARGSUSED3 */
1221 int
1222 fuse_main_real(int argc, char **argv, const struct fuse_operations *ops,
1223 size_t size, void *userdata)
1224 {
1225 struct fuse *fuse;
1226 char *mountpoint;
1227 int multithreaded;
1228 int fd;
1229
1230 fuse = fuse_setup(argc, argv, ops, size, &mountpoint, &multithreaded,
1231 &fd);
1232
1233 return fuse_loop(fuse);
1234 }
1235
1236 /*
1237 * XXX: just defer the operation until fuse_new() when we have more
1238 * info on our hands. The real beef is why's this separate in fuse in
1239 * the first place?
1240 */
1241 /* ARGSUSED1 */
1242 struct fuse_chan *
1243 fuse_mount(const char *dir, struct fuse_args *args)
1244 {
1245 struct fuse_chan *fc;
1246 char name[64];
1247
1248 if ((fc = calloc(1, sizeof(*fc))) == NULL) {
1249 err(EXIT_FAILURE, "fuse_mount");
1250 }
1251 fc->dead = 0;
1252
1253 if ((fc->dir = strdup(dir)) == NULL) {
1254 err(EXIT_FAILURE, "fuse_mount");
1255 }
1256
1257 /*
1258 * we need to deep copy the args struct - some fuse file
1259 * systems "clean up" the argument vector for "security
1260 * reasons"
1261 */
1262 fc->args = fuse_opt_deep_copy_args(args->argc, args->argv);
1263
1264 if (args->argc > 0) {
1265 set_refuse_mount_name(args->argv, name, sizeof(name));
1266 if ((args->argv[0] = strdup(name)) == NULL)
1267 err(1, "fuse_mount");
1268 }
1269
1270 return fc;
1271 }
1272
1273 /* ARGSUSED1 */
1274 struct fuse *
1275 fuse_new(struct fuse_chan *fc, struct fuse_args *args,
1276 const struct fuse_operations *ops, size_t size, void *userdata)
1277 {
1278 struct puffs_usermount *pu;
1279 struct fuse_context *fusectx;
1280 struct puffs_pathobj *po_root;
1281 struct puffs_node *pn_root;
1282 struct puffs_ops *pops;
1283 struct refusenode *rn_root;
1284 struct statvfs svfsb;
1285 struct stat st;
1286 struct fuse *fuse;
1287 extern int puffs_fakecc;
1288 char name[64];
1289 char *argv0;
1290
1291 if ((fuse = calloc(1, sizeof(*fuse))) == NULL) {
1292 err(EXIT_FAILURE, "fuse_new");
1293 }
1294
1295 /* copy fuse ops to their own stucture */
1296 (void) memcpy(&fuse->op, ops, sizeof(fuse->op));
1297
1298 fusectx = fuse_get_context();
1299 fusectx->fuse = fuse;
1300 fusectx->uid = 0;
1301 fusectx->gid = 0;
1302 fusectx->pid = 0;
1303 fusectx->private_data = userdata;
1304
1305 fuse->fc = fc;
1306
1307 /* initialise the puffs operations structure */
1308 PUFFSOP_INIT(pops);
1309
1310 PUFFSOP_SET(pops, puffs_fuse, fs, sync);
1311 PUFFSOP_SET(pops, puffs_fuse, fs, statvfs);
1312 PUFFSOP_SET(pops, puffs_fuse, fs, unmount);
1313
1314 /*
1315 * XXX: all of these don't possibly need to be
1316 * unconditionally set
1317 */
1318 PUFFSOP_SET(pops, puffs_fuse, node, lookup);
1319 PUFFSOP_SET(pops, puffs_fuse, node, getattr);
1320 PUFFSOP_SET(pops, puffs_fuse, node, setattr);
1321 PUFFSOP_SET(pops, puffs_fuse, node, readdir);
1322 PUFFSOP_SET(pops, puffs_fuse, node, readlink);
1323 PUFFSOP_SET(pops, puffs_fuse, node, mknod);
1324 PUFFSOP_SET(pops, puffs_fuse, node, create);
1325 PUFFSOP_SET(pops, puffs_fuse, node, remove);
1326 PUFFSOP_SET(pops, puffs_fuse, node, mkdir);
1327 PUFFSOP_SET(pops, puffs_fuse, node, rmdir);
1328 PUFFSOP_SET(pops, puffs_fuse, node, symlink);
1329 PUFFSOP_SET(pops, puffs_fuse, node, rename);
1330 PUFFSOP_SET(pops, puffs_fuse, node, link);
1331 PUFFSOP_SET(pops, puffs_fuse, node, open);
1332 PUFFSOP_SET(pops, puffs_fuse, node, close);
1333 PUFFSOP_SET(pops, puffs_fuse, node, read);
1334 PUFFSOP_SET(pops, puffs_fuse, node, write);
1335 PUFFSOP_SET(pops, puffs_fuse, node, reclaim);
1336
1337 argv0 = (*args->argv[0] == 0x0) ? fc->args->argv[0] : args->argv[0];
1338 set_refuse_mount_name(&argv0, name, sizeof(name));
1339
1340 puffs_fakecc = 1; /* XXX */
1341 pu = puffs_init(pops, _PATH_PUFFS, name, fuse,
1342 PUFFS_FLAG_BUILDPATH
1343 | PUFFS_FLAG_HASHPATH
1344 | PUFFS_KFLAG_NOCACHE);
1345 if (pu == NULL) {
1346 err(EXIT_FAILURE, "puffs_init");
1347 }
1348 fc->pu = pu;
1349
1350 pn_root = newrn(pu);
1351 puffs_setroot(pu, pn_root);
1352 rn_root = pn_root->pn_data;
1353 rn_root->flags |= RN_ROOT;
1354
1355 po_root = puffs_getrootpathobj(pu);
1356 if ((po_root->po_path = strdup("/")) == NULL)
1357 err(1, "fuse_new");
1358 po_root->po_len = 1;
1359 puffs_path_buildhash(pu, po_root);
1360
1361 /* sane defaults */
1362 puffs_vattr_null(&pn_root->pn_va);
1363 pn_root->pn_va.va_type = VDIR;
1364 pn_root->pn_va.va_mode = 0755;
1365 if (fuse->op.getattr)
1366 if (fuse->op.getattr(po_root->po_path, &st) == 0)
1367 puffs_stat2vattr(&pn_root->pn_va, &st);
1368 assert(pn_root->pn_va.va_type == VDIR);
1369
1370 if (fuse->op.init)
1371 fusectx->private_data = fuse->op.init(NULL); /* XXX */
1372
1373 puffs_zerostatvfs(&svfsb);
1374 if (puffs_mount(pu, fc->dir, MNT_NODEV | MNT_NOSUID, pn_root) == -1) {
1375 err(EXIT_FAILURE, "puffs_mount: directory \"%s\"", fc->dir);
1376 }
1377
1378 return fuse;
1379 }
1380
1381 int
1382 fuse_loop(struct fuse *fuse)
1383 {
1384
1385 return puffs_mainloop(fuse->fc->pu, 0);
1386 }
1387
1388 void
1389 fuse_destroy(struct fuse *fuse)
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