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