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