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