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