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