refuse.c revision 1.35 1 /* $NetBSD: refuse.c,v 1.35 2007/02/26 00:25:40 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.35 2007/02/26 00:25:40 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 /* this is the private fuse structure */
70 struct fuse {
71 struct fuse_session *se; /* fuse session pointer */
72 struct fuse_operations op; /* switch table of operations */
73 int compat; /* compat level -
74 * not used in puffs_fuse */
75 struct node **name_table;
76 size_t name_table_size;
77 struct node **id_table;
78 size_t id_table_size;
79 fuse_ino_t ctr;
80 unsigned int generation;
81 unsigned int hidectr;
82 pthread_mutex_t lock;
83 pthread_rwlock_t tree_lock;
84 void *user_data;
85 struct fuse_config conf;
86 int intr_installed;
87 struct puffs_usermount *pu;
88 };
89
90 struct refusenode {
91 struct fuse_file_info file_info;
92 int opencount;
93 int flags;
94 };
95 #define RN_ROOT 0x01
96 #define RN_OPEN 0x02 /* XXX: could just use opencount */
97
98 static int fuse_setattr(struct fuse *, struct puffs_node *,
99 const char *, const struct vattr *);
100
101 static struct puffs_node *
102 newrn(struct puffs_usermount *pu)
103 {
104 struct puffs_node *pn;
105 struct refusenode *rn;
106
107 rn = malloc(sizeof(struct refusenode));
108 if (!rn)
109 abort(); /*XXX*/
110
111 memset(rn, 0, sizeof(struct refusenode));
112 pn = puffs_pn_new(pu, rn);
113
114 return pn;
115 }
116
117 static void
118 nukern(struct puffs_node *pn)
119 {
120
121 free(pn->pn_data);
122 puffs_pn_put(pn);
123 }
124
125 static ino_t fakeino = 3;
126
127 /*
128 * XXX: do this otherwise if/when we grow thread support
129 *
130 * XXX2: does not consistently supply uid, gid or pid currently
131 */
132 static struct fuse_context fcon;
133
134
135 /* XXX: rethinkme */
136 struct fuse_dirh {
137 struct dirent *dent;
138 size_t reslen;
139 off_t readoff;
140 };
141
142 /* ARGSUSED2 */
143 static int
144 puffs_fuse_fill_dir(void *buf, const char *name,
145 const struct stat *stbuf, off_t off)
146 {
147 struct fuse_dirh *deh = buf;
148 ino_t dino;
149 uint8_t dtype;
150
151 if (stbuf == NULL) {
152 dtype = DT_UNKNOWN;
153 dino = fakeino++;
154 } else {
155 dtype = puffs_vtype2dt(puffs_mode2vt(stbuf->st_mode));
156 dino = stbuf->st_ino;
157 }
158
159 return !puffs_nextdent(&deh->dent, name, dino, dtype, &deh->reslen);
160 }
161
162 static int
163 puffs_fuse_dirfil(fuse_dirh_t h, const char *name, int type, ino_t ino)
164 {
165 ino_t dino;
166 int dtype;
167
168 if (type == 0)
169 dtype = DT_UNKNOWN;
170 else
171 dtype = type;
172
173 if (ino)
174 dino = ino;
175 else
176 dino = fakeino++;
177
178 return !puffs_nextdent(&h->dent, name, dino, dtype, &h->reslen);
179 }
180
181 int
182 fuse_opt_add_arg(struct fuse_args *args, const char *arg)
183 {
184 char **oldargv;
185 int oldargc;
186
187 if (args->allocated) {
188 RENEW(char *, args->argv, args->argc + 1,
189 "fuse_opt_add_arg1", return 0);
190 } else {
191 oldargv = args->argv;
192 oldargc = args->argc;
193 NEWARRAY(char *, args->argv, oldargc + 1,
194 "fuse_opt_add_arg2", return 0);
195 (void) memcpy(args->argv, oldargv, oldargc * sizeof(char *));
196 args->allocated = 1;
197 }
198 args->argv[args->argc++] = strdup(arg);
199 return 1;
200 }
201
202 void
203 fuse_opt_free_args(struct fuse_args *args)
204 {
205 if (args && args->argv) {
206 int i;
207 for (i = 0; i < args->argc; i++)
208 FREE(args->argv[i]);
209 FREE(args->argv);
210 }
211 }
212
213 #define FUSE_ERR_UNLINK(fuse, file) if (fuse->op.unlink) fuse->op.unlink(file)
214 #define FUSE_ERR_RMDIR(fuse, dir) if (fuse->op.rmdir) fuse->op.rmdir(dir)
215
216 /* ARGSUSED1 */
217 static int
218 fuse_getattr(struct fuse *fuse, struct puffs_node *pn, const char *path,
219 struct vattr *va)
220 {
221 struct stat st;
222 int ret;
223
224 if (fuse->op.getattr == NULL) {
225 return ENOSYS;
226 }
227
228 /* wrap up return code */
229 ret = (*fuse->op.getattr)(path, &st);
230
231 if (ret == 0) {
232 puffs_stat2vattr(va, &st);
233 }
234
235 return -ret;
236 }
237
238 static int
239 fuse_setattr(struct fuse *fuse, struct puffs_node *pn, const char *path,
240 const struct vattr *va)
241 {
242 struct refusenode *rn = pn->pn_data;
243 mode_t mode;
244 uid_t uid;
245 gid_t gid;
246 int error, ret;
247
248 error = 0;
249
250 mode = va->va_mode;
251 uid = va->va_uid;
252 gid = va->va_gid;
253
254 if (mode != (mode_t)PUFFS_VNOVAL) {
255 ret = 0;
256
257 if (fuse->op.chmod == NULL) {
258 error = -ENOSYS;
259 } else {
260 ret = fuse->op.chmod(path, mode);
261 if (ret)
262 error = ret;
263 }
264 }
265 if (uid != (uid_t)PUFFS_VNOVAL || gid != (gid_t)PUFFS_VNOVAL) {
266 ret = 0;
267
268 if (fuse->op.chown == NULL) {
269 error = -ENOSYS;
270 } else {
271 ret = fuse->op.chown(path, uid, gid);
272 if (ret)
273 error = ret;
274 }
275 }
276 if (va->va_atime.tv_sec != (time_t)PUFFS_VNOVAL
277 || va->va_mtime.tv_sec != (long)PUFFS_VNOVAL) {
278 ret = 0;
279
280 if (fuse->op.utimens) {
281 struct timespec tv[2];
282
283 tv[0].tv_sec = va->va_atime.tv_sec;
284 tv[0].tv_nsec = va->va_atime.tv_nsec;
285 tv[1].tv_sec = va->va_mtime.tv_sec;
286 tv[1].tv_nsec = va->va_mtime.tv_nsec;
287
288 ret = fuse->op.utimens(path, tv);
289 } else if (fuse->op.utime) {
290 struct utimbuf timbuf;
291
292 timbuf.actime = va->va_atime.tv_sec;
293 timbuf.modtime = va->va_mtime.tv_sec;
294
295 ret = fuse->op.utime(path, &timbuf);
296 } else {
297 error = -ENOSYS;
298 }
299
300 if (ret)
301 error = ret;
302 }
303 if (va->va_size != (u_quad_t)PUFFS_VNOVAL) {
304 ret = 0;
305
306 if (fuse->op.truncate) {
307 ret = fuse->op.truncate(path, (off_t)va->va_size);
308 } else if (fuse->op.ftruncate) {
309 ret = fuse->op.ftruncate(path, (off_t)va->va_size,
310 &rn->file_info);
311 } else {
312 error = -ENOSYS;
313 }
314
315 if (ret)
316 error = ret;
317 }
318 /* XXX: no reflection with reality */
319 puffs_setvattr(&pn->pn_va, va);
320
321 return -error;
322
323 }
324
325 static int
326 fuse_newnode(struct puffs_usermount *pu, const char *path,
327 const struct vattr *va, struct fuse_file_info *fi, void **newnode)
328 {
329 struct vattr newva;
330 struct fuse *fuse;
331 struct puffs_node *pn;
332 struct refusenode *rn;
333
334 fuse = (struct fuse *)pu->pu_privdata;
335
336 /* fix up nodes */
337 pn = newrn(pu);
338 if (pn == NULL) {
339 if (va->va_type == VDIR) {
340 FUSE_ERR_RMDIR(fuse, path);
341 } else {
342 FUSE_ERR_UNLINK(fuse, path);
343 }
344 return ENOMEM;
345 }
346 fuse_setattr(fuse, pn, path, va);
347 if (fuse_getattr(fuse, pn, path, &newva) == 0)
348 puffs_setvattr(&pn->pn_va, &newva);
349
350 rn = pn->pn_data;
351 if (fi)
352 memcpy(&rn->file_info, fi, sizeof(struct fuse_file_info));
353
354 *newnode = pn;
355
356 return 0;
357 }
358
359
360 /* operation wrappers start here */
361
362 /* lookup the path */
363 /* ARGSUSED1 */
364 static int
365 puffs_fuse_node_lookup(struct puffs_cc *pcc, void *opc, void **newnode,
366 enum vtype *newtype, voff_t *newsize, dev_t *newrdev,
367 const struct puffs_cn *pcn)
368 {
369 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
370 struct puffs_node *pn_res;
371 struct stat st;
372 struct fuse *fuse;
373 const char *path = PCNPATH(pcn);
374 int ret;
375
376 fuse = (struct fuse *)pu->pu_privdata;
377 ret = fuse->op.getattr(path, &st);
378
379 if (ret != 0) {
380 return -ret;
381 }
382
383 /* XXX: fiXXXme unconst */
384 pn_res = puffs_pn_nodewalk(pu, puffs_path_walkcmp,
385 __UNCONST(&pcn->pcn_po_full));
386 if (pn_res == NULL) {
387 pn_res = newrn(pu);
388 if (pn_res == NULL)
389 return errno;
390 puffs_stat2vattr(&pn_res->pn_va, &st);
391 }
392
393 *newnode = pn_res;
394 *newtype = pn_res->pn_va.va_type;
395 *newsize = pn_res->pn_va.va_size;
396 *newrdev = pn_res->pn_va.va_rdev;
397
398 return 0;
399 }
400
401 /* get attributes for the path name */
402 /* ARGSUSED3 */
403 static int
404 puffs_fuse_node_getattr(struct puffs_cc *pcc, void *opc, struct vattr *va,
405 const struct puffs_cred *pcr, pid_t pid)
406 {
407 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
408 struct puffs_node *pn = opc;
409 struct fuse *fuse;
410 const char *path = PNPATH(pn);
411
412 fuse = (struct fuse *)pu->pu_privdata;
413 return fuse_getattr(fuse, pn, path, va);
414 }
415
416 /* read the contents of the symbolic link */
417 /* ARGSUSED2 */
418 static int
419 puffs_fuse_node_readlink(struct puffs_cc *pcc, void *opc,
420 const struct puffs_cred *cred, char *linkname, size_t *linklen)
421 {
422 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
423 struct puffs_node *pn = opc;
424 struct fuse *fuse;
425 const char *path = PNPATH(pn), *p;
426 int ret;
427
428 fuse = (struct fuse *)pu->pu_privdata;
429 if (fuse->op.readlink == NULL) {
430 return ENOSYS;
431 }
432
433 /* wrap up return code */
434 ret = (*fuse->op.readlink)(path, linkname, *linklen);
435
436 if (ret == 0) {
437 p = memchr(linkname, '\0', *linklen);
438 if (!p)
439 return EINVAL;
440
441 *linklen = p - linkname;
442 }
443
444 return -ret;
445 }
446
447 /* make the special node */
448 /* ARGSUSED1 */
449 static int
450 puffs_fuse_node_mknod(struct puffs_cc *pcc, void *opc, void **newnode,
451 const struct puffs_cn *pcn, const struct vattr *va)
452 {
453 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
454 struct fuse *fuse;
455 mode_t mode = va->va_mode;
456 const char *path = PCNPATH(pcn);
457 int ret;
458
459 fuse = (struct fuse *)pu->pu_privdata;
460 if (fuse->op.mknod == NULL) {
461 return ENOSYS;
462 }
463
464 /* wrap up return code */
465 ret = (*fuse->op.mknod)(path, mode, va->va_rdev);
466
467 if (ret == 0) {
468 ret = fuse_newnode(pu, path, va, NULL, newnode);
469 }
470
471 return -ret;
472 }
473
474 /* make a directory */
475 /* ARGSUSED1 */
476 static int
477 puffs_fuse_node_mkdir(struct puffs_cc *pcc, void *opc, void **newnode,
478 const struct puffs_cn *pcn, const struct vattr *va)
479 {
480 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
481 struct fuse *fuse;
482 mode_t mode = va->va_mode;
483 const char *path = PCNPATH(pcn);
484 int ret;
485
486 fuse = (struct fuse *)pu->pu_privdata;
487 if (fuse->op.mkdir == NULL) {
488 return ENOSYS;
489 }
490
491 /* wrap up return code */
492 ret = (*fuse->op.mkdir)(path, mode);
493
494 if (ret == 0) {
495 ret = fuse_newnode(pu, path, va, NULL, newnode);
496 }
497
498 return -ret;
499 }
500
501 /*
502 * create a regular file
503 *
504 * since linux/fuse sports using mknod for creating regular files
505 * instead of having a separate call for it in some versions, if
506 * we don't have create, just jump to op->mknod.
507 */
508 /*ARGSUSED1*/
509 static int
510 puffs_fuse_node_create(struct puffs_cc *pcc, void *opc, void **newnode,
511 const struct puffs_cn *pcn, const struct vattr *va)
512 {
513 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
514 struct fuse *fuse;
515 struct fuse_file_info fi;
516 mode_t mode = va->va_mode;
517 const char *path = PCNPATH(pcn);
518 int ret, created;
519
520 fuse = (struct fuse *)pu->pu_privdata;
521
522 created = 0;
523 if (fuse->op.create) {
524 ret = fuse->op.create(path, mode, &fi);
525 if (ret == 0)
526 created = 1;
527
528 } else if (fuse->op.mknod) {
529 fcon.uid = va->va_uid; /*XXX*/
530 fcon.gid = va->va_gid; /*XXX*/
531
532 ret = fuse->op.mknod(path, mode | S_IFREG, 0);
533
534 } else {
535 ret = -ENOSYS;
536 }
537
538 if (ret == 0) {
539 ret = fuse_newnode(pu, path, va, &fi, newnode);
540
541 /* sweet.. create also open the file */
542 if (created) {
543 struct puffs_node *pn;
544 struct refusenode *rn;
545
546 pn = *newnode;
547 rn = pn->pn_data;
548 rn->flags |= RN_OPEN;
549 rn->opencount++;
550 }
551 }
552
553 return -ret;
554 }
555
556 /* remove the directory entry */
557 /* ARGSUSED1 */
558 static int
559 puffs_fuse_node_remove(struct puffs_cc *pcc, void *opc, void *targ,
560 const struct puffs_cn *pcn)
561 {
562 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
563 struct puffs_node *pn_targ = targ;
564 struct fuse *fuse;
565 const char *path = PNPATH(pn_targ);
566 int ret;
567
568 fuse = (struct fuse *)pu->pu_privdata;
569 if (fuse->op.unlink == NULL) {
570 return ENOSYS;
571 }
572
573 /* wrap up return code */
574 ret = (*fuse->op.unlink)(path);
575
576 return -ret;
577 }
578
579 /* remove the directory */
580 /* ARGSUSED1 */
581 static int
582 puffs_fuse_node_rmdir(struct puffs_cc *pcc, void *opc, void *targ,
583 const struct puffs_cn *pcn)
584 {
585 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
586 struct puffs_node *pn_targ = targ;
587 struct fuse *fuse;
588 const char *path = PNPATH(pn_targ);
589 int ret;
590
591 fuse = (struct fuse *)pu->pu_privdata;
592 if (fuse->op.rmdir == NULL) {
593 return ENOSYS;
594 }
595
596 /* wrap up return code */
597 ret = (*fuse->op.rmdir)(path);
598
599 return -ret;
600 }
601
602 /* create a symbolic link */
603 /* ARGSUSED1 */
604 static int
605 puffs_fuse_node_symlink(struct puffs_cc *pcc, void *opc, void **newnode,
606 const struct puffs_cn *pcn_src, const struct vattr *va,
607 const char *link_target)
608 {
609 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
610 struct fuse *fuse;
611 const char *path = PCNPATH(pcn_src);
612 int ret;
613
614 fuse = (struct fuse *)pu->pu_privdata;
615 if (fuse->op.symlink == NULL) {
616 return ENOSYS;
617 }
618
619 /* wrap up return code */
620 ret = fuse->op.symlink(link_target, path);
621
622 if (ret == 0) {
623 ret = fuse_newnode(pu, path, va, NULL, newnode);
624 }
625
626 return -ret;
627 }
628
629 /* rename a directory entry */
630 /* ARGSUSED1 */
631 static int
632 puffs_fuse_node_rename(struct puffs_cc *pcc, void *opc, void *src,
633 const struct puffs_cn *pcn_src, void *targ_dir, void *targ,
634 const struct puffs_cn *pcn_targ)
635 {
636 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
637 struct fuse *fuse;
638 const char *path_src = PCNPATH(pcn_src);
639 const char *path_dest = PCNPATH(pcn_targ);
640 int ret;
641
642 fuse = (struct fuse *)pu->pu_privdata;
643 if (fuse->op.rename == NULL) {
644 return ENOSYS;
645 }
646
647 ret = fuse->op.rename(path_src, path_dest);
648
649 if (ret == 0) {
650 }
651
652 return -ret;
653 }
654
655 /* create a link in the file system */
656 /* ARGSUSED1 */
657 static int
658 puffs_fuse_node_link(struct puffs_cc *pcc, void *opc, void *targ,
659 const struct puffs_cn *pcn)
660 {
661 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
662 struct puffs_node *pn = targ;
663 struct fuse *fuse;
664 int ret;
665
666 fuse = (struct fuse *)pu->pu_privdata;
667 if (fuse->op.link == NULL) {
668 return ENOSYS;
669 }
670
671 /* wrap up return code */
672 ret = (*fuse->op.link)(PNPATH(pn), PCNPATH(pcn));
673
674 return -ret;
675 }
676
677 /*
678 * fuse's regular interface provides chmod(), chown(), utimes()
679 * and truncate() + some variations, so try to fit the square block
680 * in the circle hole and the circle block .... something like that
681 */
682 /* ARGSUSED3 */
683 static int
684 puffs_fuse_node_setattr(struct puffs_cc *pcc, void *opc,
685 const struct vattr *va, const struct puffs_cred *pcr, pid_t pid)
686 {
687 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
688 struct puffs_node *pn = opc;
689 struct fuse *fuse;
690 const char *path = PNPATH(pn);
691
692 fuse = (struct fuse *)pu->pu_privdata;
693
694 return fuse_setattr(fuse, pn, path, va);
695 }
696
697 /* ARGSUSED2 */
698 static int
699 puffs_fuse_node_open(struct puffs_cc *pcc, void *opc, int mode,
700 const struct puffs_cred *cred, pid_t pid)
701 {
702 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
703 struct puffs_node *pn = opc;
704 struct refusenode *rn = pn->pn_data;
705 struct fuse_file_info *fi = &rn->file_info;
706 struct fuse *fuse;
707 const char *path = PNPATH(pn);
708
709 fuse = (struct fuse *)pu->pu_privdata;
710
711 /* if open, don't open again, lest risk nuking file private info */
712 if (rn->flags & RN_OPEN) {
713 rn->opencount++;
714 return 0;
715 }
716
717 fi->flags = mode & ~(O_CREAT | O_EXCL | O_TRUNC);
718 if (pn->pn_va.va_type == VDIR) {
719 if (fuse->op.opendir)
720 fuse->op.opendir(path, fi);
721 } else {
722 if (fuse->op.open)
723 fuse->op.open(path, fi);
724 }
725
726 rn->flags |= RN_OPEN;
727 rn->opencount++;
728
729 return 0;
730 }
731
732 /* ARGSUSED2 */
733 static int
734 puffs_fuse_node_close(struct puffs_cc *pcc, void *opc, int fflag,
735 const struct puffs_cred *pcr, pid_t pid)
736 {
737 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
738 struct puffs_node *pn = opc;
739 struct refusenode *rn = pn->pn_data;
740 struct fuse *fuse;
741 struct fuse_file_info *fi;
742 const char *path = PNPATH(pn);
743 int ret;
744
745 fuse = (struct fuse *)pu->pu_privdata;
746 fi = &rn->file_info;
747 ret = 0;
748
749 if (rn->flags & RN_OPEN) {
750 if (pn->pn_va.va_type == VDIR) {
751 if (fuse->op.releasedir)
752 ret = fuse->op.releasedir(path, fi);
753 } else {
754 if (fuse->op.release)
755 ret = fuse->op.release(path, fi);
756 }
757 }
758 rn->flags &= ~RN_OPEN;
759 rn->opencount--;
760
761 return ret;
762 }
763
764 /* read some more from the file */
765 /* ARGSUSED5 */
766 static int
767 puffs_fuse_node_read(struct puffs_cc *pcc, void *opc, uint8_t *buf,
768 off_t offset, size_t *resid, const struct puffs_cred *pcr,
769 int ioflag)
770 {
771 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
772 struct puffs_node *pn = opc;
773 struct refusenode *rn = pn->pn_data;
774 struct fuse *fuse;
775 const char *path = PNPATH(pn);
776 size_t maxread;
777 int ret;
778
779 fuse = (struct fuse *)pu->pu_privdata;
780 if (fuse->op.read == NULL) {
781 return ENOSYS;
782 }
783
784 maxread = *resid;
785 if (maxread > pn->pn_va.va_size - offset) {
786 /*LINTED*/
787 maxread = pn->pn_va.va_size - offset;
788 }
789 if (maxread == 0)
790 return 0;
791
792 ret = (*fuse->op.read)(path, (char *)buf, maxread, offset,
793 &rn->file_info);
794
795 if (ret > 0) {
796 *resid -= ret;
797 ret = 0;
798 }
799
800 return -ret;
801 }
802
803 /* write to the file */
804 /* ARGSUSED0 */
805 static int
806 puffs_fuse_node_write(struct puffs_cc *pcc, void *opc, uint8_t *buf,
807 off_t offset, size_t *resid, const struct puffs_cred *pcr,
808 int ioflag)
809 {
810 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
811 struct puffs_node *pn = opc;
812 struct refusenode *rn = pn->pn_data;
813 struct fuse *fuse;
814 const char *path = PNPATH(pn);
815 int ret;
816
817 fuse = (struct fuse *)pu->pu_privdata;
818 if (fuse->op.write == NULL) {
819 return ENOSYS;
820 }
821
822 if (ioflag & PUFFS_IO_APPEND)
823 offset = pn->pn_va.va_size;
824
825 ret = (*fuse->op.write)(path, (char *)buf, *resid, offset,
826 &rn->file_info);
827
828 if (ret > 0) {
829 if (offset + ret > pn->pn_va.va_size)
830 pn->pn_va.va_size = offset + ret;
831 *resid -= ret;
832 ret = 0;
833 }
834
835 return -ret;
836 }
837
838
839 /* ARGSUSED3 */
840 static int
841 puffs_fuse_node_readdir(struct puffs_cc *pcc, void *opc,
842 struct dirent *dent, const struct puffs_cred *pcr, off_t *readoff,
843 size_t *reslen)
844 {
845 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
846 struct puffs_node *pn = opc;
847 struct refusenode *rn = pn->pn_data;
848 struct fuse *fuse;
849 const char *path = PNPATH(pn);
850 struct fuse_dirh deh;
851 int ret;
852
853 fuse = (struct fuse *)pu->pu_privdata;
854 if (fuse->op.readdir == NULL && fuse->op.getdir == NULL) {
855 return ENOSYS;
856 }
857
858 /* XXX: how to handle this??? */
859 if (*readoff != 0) {
860 return 0;
861 }
862
863 deh.dent = dent;
864 deh.reslen = *reslen;
865 deh.readoff = *readoff;
866
867 if (fuse->op.readdir)
868 ret = fuse->op.readdir(path, &deh, puffs_fuse_fill_dir,
869 *readoff, &rn->file_info);
870 else
871 ret = fuse->op.getdir(path, &deh, puffs_fuse_dirfil);
872 *reslen = deh.reslen;
873 *readoff = 1;
874
875 if (ret == 0) {
876 }
877
878 return -ret;
879 }
880
881 /* ARGSUSED */
882 static int
883 puffs_fuse_node_reclaim(struct puffs_cc *pcc, void *opc, pid_t pid)
884 {
885 struct puffs_node *pn = opc;
886
887 nukern(pn);
888
889 return 0;
890 }
891
892 /* ARGSUSED1 */
893 static int
894 puffs_fuse_fs_unmount(struct puffs_cc *pcc, int flags, pid_t pid)
895 {
896 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
897 struct fuse *fuse;
898
899 fuse = (struct fuse *)pu->pu_privdata;
900 if (fuse->op.destroy == NULL) {
901 return 0;
902 }
903 (*fuse->op.destroy)(fuse);
904 return 0;
905 }
906
907 /* ARGSUSED0 */
908 static int
909 puffs_fuse_fs_sync(struct puffs_cc *pcc, int flags,
910 const struct puffs_cred *cr, pid_t pid)
911 {
912 return 0;
913 }
914
915 /* ARGSUSED2 */
916 static int
917 puffs_fuse_fs_statvfs(struct puffs_cc *pcc, struct statvfs *svfsb, pid_t pid)
918 {
919 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
920 struct fuse *fuse;
921 int ret;
922
923 fuse = (struct fuse *)pu->pu_privdata;
924 if (fuse->op.statfs == NULL) {
925 if ((ret = statvfs(PNPATH(pu->pu_pn_root), svfsb)) == -1) {
926 return errno;
927 }
928 } else {
929 ret = (*fuse->op.statfs)(PNPATH(pu->pu_pn_root), svfsb);
930 }
931
932 return ret;
933 }
934
935
936 /* End of puffs_fuse operations */
937
938 /* ARGSUSED3 */
939 int
940 fuse_main_real(int argc, char **argv, const struct fuse_operations *ops,
941 size_t size, void *userdata)
942 {
943 struct puffs_usermount *pu;
944 struct puffs_pathobj *po_root;
945 struct puffs_ops *pops;
946 struct refusenode *rn_root;
947 struct statvfs svfsb;
948 struct stat st;
949 struct fuse *fuse;
950 char name[64];
951 char *slash;
952 int ret;
953
954 /* initialise the puffs operations structure */
955 PUFFSOP_INIT(pops);
956
957 PUFFSOP_SET(pops, puffs_fuse, fs, sync);
958 PUFFSOP_SET(pops, puffs_fuse, fs, statvfs);
959 PUFFSOP_SET(pops, puffs_fuse, fs, unmount);
960
961 /*
962 * XXX: all of these don't possibly need to be
963 * unconditionally set
964 */
965 PUFFSOP_SET(pops, puffs_fuse, node, lookup);
966 PUFFSOP_SET(pops, puffs_fuse, node, getattr);
967 PUFFSOP_SET(pops, puffs_fuse, node, setattr);
968 PUFFSOP_SET(pops, puffs_fuse, node, readdir);
969 PUFFSOP_SET(pops, puffs_fuse, node, readlink);
970 PUFFSOP_SET(pops, puffs_fuse, node, mknod);
971 PUFFSOP_SET(pops, puffs_fuse, node, create);
972 PUFFSOP_SET(pops, puffs_fuse, node, remove);
973 PUFFSOP_SET(pops, puffs_fuse, node, mkdir);
974 PUFFSOP_SET(pops, puffs_fuse, node, rmdir);
975 PUFFSOP_SET(pops, puffs_fuse, node, symlink);
976 PUFFSOP_SET(pops, puffs_fuse, node, rename);
977 PUFFSOP_SET(pops, puffs_fuse, node, link);
978 PUFFSOP_SET(pops, puffs_fuse, node, open);
979 PUFFSOP_SET(pops, puffs_fuse, node, close);
980 PUFFSOP_SET(pops, puffs_fuse, node, read);
981 PUFFSOP_SET(pops, puffs_fuse, node, write);
982 PUFFSOP_SET(pops, puffs_fuse, node, reclaim);
983
984 NEW(struct fuse, fuse, "fuse_main_real", exit(EXIT_FAILURE));
985
986 /* copy fuse ops to their own stucture */
987 (void) memcpy(&fuse->op, ops, sizeof(fuse->op));
988
989 fcon.fuse = fuse;
990 fcon.private_data = userdata;
991
992 /* whilst this (assigning the pu_privdata in the puffs
993 * usermount struct to be the fuse struct) might seem like
994 * we are chasing our tail here, the logic is as follows:
995 + the operation wrapper gets called with the puffs
996 calling conventions
997 + we need to fix up args first
998 + then call the fuse user-supplied operation
999 + then we fix up any values on return that we need to
1000 + and fix up any nodes, etc
1001 * so we need to be able to get at the fuse ops from within the
1002 * puffs_usermount struct
1003 */
1004 if ((slash = strrchr(*argv, '/')) == NULL) {
1005 slash = *argv;
1006 } else {
1007 slash += 1;
1008 }
1009 (void) snprintf(name, sizeof(name), "refuse:%s", slash);
1010 pu = puffs_mount(pops, argv[argc - 1], MNT_NODEV | MNT_NOSUID,
1011 name, fuse,
1012 PUFFS_FLAG_BUILDPATH
1013 | PUFFS_FLAG_OPDUMP
1014 | PUFFS_KFLAG_NOCACHE,
1015 0);
1016 if (pu == NULL) {
1017 err(EXIT_FAILURE, "puffs_mount");
1018 }
1019
1020 fuse->pu = pu;
1021 pu->pu_pn_root = newrn(pu);
1022 rn_root = pu->pu_pn_root->pn_data;
1023 rn_root->flags |= RN_ROOT;
1024
1025 po_root = puffs_getrootpathobj(pu);
1026 po_root->po_path = strdup("/");
1027 po_root->po_len = 1;
1028
1029 /* sane defaults */
1030 puffs_vattr_null(&pu->pu_pn_root->pn_va);
1031 pu->pu_pn_root->pn_va.va_type = VDIR;
1032 pu->pu_pn_root->pn_va.va_mode = 0755;
1033 if (fuse->op.getattr)
1034 if (fuse->op.getattr(po_root->po_path, &st) == 0)
1035 puffs_stat2vattr(&pu->pu_pn_root->pn_va, &st);
1036 assert(pu->pu_pn_root->pn_va.va_type == VDIR);
1037
1038 if (fuse->op.init)
1039 fcon.private_data = fuse->op.init(NULL); /* XXX */
1040
1041 statvfs(argv[argc - 1], &svfsb); /* XXX - not really the correct dir */
1042 if (puffs_start(pu, pu->pu_pn_root, &svfsb) == -1) {
1043 err(EXIT_FAILURE, "puffs_start");
1044 }
1045
1046 ret = puffs_mainloop(fuse->pu, PUFFSLOOP_NODAEMON);
1047
1048 (void) free(po_root->po_path);
1049 FREE(fuse);
1050 return ret;
1051 }
1052
1053 /* ARGSUSED0 */
1054 int
1055 fuse_opt_parse(struct fuse_args *args, void *data,
1056 const struct fuse_opt *opts, fuse_opt_proc_t proc)
1057 {
1058 return 0;
1059 }
1060
1061 /* XXX: threads */
1062 struct fuse_context *
1063 fuse_get_context()
1064 {
1065
1066 return &fcon;
1067 }
1068
1069 void
1070 fuse_exit(struct fuse *f)
1071 {
1072
1073 puffs_exit(f->pu, 1);
1074 }
1075
1076 /*
1077 * XXX: obviously not the most perfect of functions, but needs some
1078 * puffs tweaking for a better tomorrow
1079 */
1080 /*ARGSUSED*/
1081 void
1082 fuse_unmount(const char *mp)
1083 {
1084
1085 return;
1086 }
1087