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