ops.c revision 1.13 1 /* $NetBSD: ops.c,v 1.13 2010/09/07 16:58:13 manu Exp $ */
2
3 /*-
4 * Copyright (c) 2010 Emmanuel Dreyfus. 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 *
15 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <stdio.h>
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include <libgen.h>
32 #include <errno.h>
33 #include <err.h>
34 #include <sysexits.h>
35 #include <syslog.h>
36 #include <puffs.h>
37 #include <sys/vnode.h>
38 #include <sys/socket.h>
39 #include <machine/vmparam.h>
40
41 #include "perfuse_priv.h"
42 #include "fuse.h"
43
44 extern int perfuse_diagflags;
45
46 static int node_close_common(struct puffs_usermount *, puffs_cookie_t, int);
47 static int no_access(puffs_cookie_t, const struct puffs_cred *, mode_t);
48 static void fuse_attr_to_vap(struct perfuse_state *,
49 struct vattr *, struct fuse_attr *);
50 static int node_lookup_dir_nodot(struct puffs_usermount *,
51 puffs_cookie_t, char *, size_t, struct puffs_node **);
52 static int node_lookup_common(struct puffs_usermount *, puffs_cookie_t,
53 const char*, struct puffs_node **);
54 static int node_mk_common(struct puffs_usermount *, puffs_cookie_t,
55 struct puffs_newinfo *, const struct puffs_cn *pcn, perfuse_msg_t *);
56 static const char *basename_r(const char *);
57 static ssize_t fuse_to_dirent(struct puffs_usermount *, puffs_cookie_t,
58 struct fuse_dirent *, size_t);
59 static int readdir_buffered(struct perfuse_state *, puffs_cookie_t,
60 struct dirent *, off_t *, size_t *, const struct puffs_cred *,
61 int *, off_t *, size_t *);
62 static void requeue_request(struct puffs_usermount *,
63 puffs_cookie_t opc, enum perfuse_qtype);
64 static int dequeue_requests(struct perfuse_state *,
65 puffs_cookie_t opc, enum perfuse_qtype, int);
66 #define DEQUEUE_ALL 0
67
68 /*
69 * From <sys/vnode>, inside #ifdef _KERNEL section
70 */
71 #define IO_SYNC (0x40|IO_DSYNC)
72 #define IO_DSYNC 0x00200
73 #define IO_DIRECT 0x02000
74
75 /*
76 * From <fcntl>, inside #ifdef _KERNEL section
77 */
78 #define F_WAIT 0x010
79 #define F_FLOCK 0x020
80 #define OFLAGS(fflags) ((fflags) - 1)
81
82 /*
83 * Borrowed from src/sys/kern/vfs_subr.c and src/sys/sys/vnode.h
84 */
85 const enum vtype iftovt_tab[16] = {
86 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
87 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
88 };
89 const int vttoif_tab[9] = {
90 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
91 S_IFSOCK, S_IFIFO, S_IFMT,
92 };
93
94 #define IFTOVT(mode) (iftovt_tab[((mode) & S_IFMT) >> 12])
95 #define VTTOIF(indx) (vttoif_tab[(int)(indx)])
96
97 static int
98 node_close_common(pu, opc, mode)
99 struct puffs_usermount *pu;
100 puffs_cookie_t opc;
101 int mode;
102 {
103 struct perfuse_state *ps;
104 perfuse_msg_t *pm;
105 int op;
106 uint64_t fh;
107 struct fuse_release_in *fri;
108 struct perfuse_node_data *pnd;
109 struct puffs_node *pn;
110 int error;
111
112 ps = puffs_getspecific(pu);
113 pn = (struct puffs_node *)opc;
114 pnd = PERFUSE_NODE_DATA(pn);
115
116 if (puffs_pn_getvap(pn)->va_type == VDIR) {
117 op = FUSE_RELEASEDIR;
118 mode = FREAD;
119 } else {
120 op = FUSE_RELEASE;
121 }
122
123 /*
124 * Destroy the filehandle before sending the
125 * request to the FUSE filesystem, otherwise
126 * we may get a second close() while we wait
127 * for the reply, and we would end up closing
128 * the same fh twice instead of closng both.
129 */
130 fh = perfuse_get_fh(opc, mode);
131 perfuse_destroy_fh(pn, fh);
132
133 /*
134 * release_flags may be set to FUSE_RELEASE_FLUSH
135 * to flush locks. lock_owner must be set in that case
136 */
137 pm = ps->ps_new_msg(pu, opc, op, sizeof(*fri), NULL);
138 fri = GET_INPAYLOAD(ps, pm, fuse_release_in);
139 fri->fh = fh;
140 fri->flags = 0;
141 fri->release_flags = 0;
142 fri->lock_owner = pnd->pnd_lock_owner;
143 fri->flags = (fri->lock_owner != 0) ? FUSE_RELEASE_FLUSH : 0;
144
145 #ifdef PERFUSE_DEBUG
146 if (perfuse_diagflags & PDF_FH)
147 DPRINTF("%s: opc = %p, ino = %"PRId64", fh = 0x%"PRIx64"\n",
148 __func__, (void *)opc, pnd->pnd_ino, fri->fh);
149 #endif
150
151 if ((error = XCHG_MSG(ps, pu, pm, NO_PAYLOAD_REPLY_LEN)) != 0)
152 goto out;
153
154 ps->ps_destroy_msg(pm);
155
156 error = 0;
157
158 out:
159 if (error != 0)
160 DERRX(EX_SOFTWARE, "%s: freed fh = 0x%"PRIx64" but filesystem "
161 "returned error = %d", __func__, fh, error);
162
163 return error;
164 }
165
166 static int
167 no_access(opc, pcr, mode)
168 puffs_cookie_t opc;
169 const struct puffs_cred *pcr;
170 mode_t mode;
171 {
172 struct puffs_node *pn;
173 struct vattr *va;
174
175 pn = (struct puffs_node *)opc;
176 va = puffs_pn_getvap(pn);
177
178 return puffs_access(va->va_type, va->va_mode,
179 va->va_uid, va->va_gid,
180 mode, pcr);
181 }
182
183 static void
184 fuse_attr_to_vap(ps, vap, fa)
185 struct perfuse_state *ps;
186 struct vattr *vap;
187 struct fuse_attr *fa;
188 {
189 vap->va_type = IFTOVT(fa->mode);
190 vap->va_mode = fa->mode;
191 vap->va_nlink = fa->nlink;
192 vap->va_uid = fa->uid;
193 vap->va_gid = fa->gid;
194 vap->va_fsid = ps->ps_fsid;
195 vap->va_fileid = fa->ino;
196 vap->va_size = fa->size;
197 vap->va_blocksize = fa->blksize;
198 vap->va_atime.tv_sec = (time_t)fa->atime;
199 vap->va_atime.tv_nsec = (long) fa->atimensec;
200 vap->va_mtime.tv_sec = (time_t)fa->mtime;
201 vap->va_mtime.tv_nsec = (long)fa->mtimensec;
202 vap->va_ctime.tv_sec = (time_t)fa->ctime;
203 vap->va_ctime.tv_nsec = (long)fa->ctimensec;
204 vap->va_birthtime.tv_sec = 0;
205 vap->va_birthtime.tv_nsec = 0;
206 vap->va_gen = 0;
207 vap->va_flags = 0;
208 vap->va_rdev = fa->rdev;
209 vap->va_bytes = fa->size;
210 vap->va_filerev = 0;
211 vap->va_vaflags = 0;
212
213 if (vap->va_blocksize == 0)
214 vap->va_blocksize = DEV_BSIZE;
215
216 if (vap->va_size == (size_t)-1) /* XXX */
217 vap->va_size = 0;
218
219 return;
220 }
221
222
223 /*
224 * Lookup name in directory opc
225 * We take special care of name being . or ..
226 * These are returned by readdir and deserve tweaks.
227 */
228 static int
229 node_lookup_dir_nodot(pu, opc, name, namelen, pnp)
230 struct puffs_usermount *pu;
231 puffs_cookie_t opc;
232 char *name;
233 size_t namelen;
234 struct puffs_node **pnp;
235 {
236 char *path;
237 struct puffs_node *dpn = (struct puffs_node *)opc;
238 int error;
239
240 /*
241 * is easy as we already know it
242 */
243 if (strncmp(name, ".", namelen) == 0) {
244 *pnp = (struct puffs_node *)opc;
245 return 0;
246 }
247
248 /*
249 * For .. we just forget the name part
250 */
251 if (strncmp(name, "..", namelen) == 0)
252 namelen = 0;
253
254 namelen = PNPLEN(dpn) + 1 + namelen + 1;
255 if ((path = malloc(namelen)) == NULL)
256 DERR(EX_OSERR, "malloc failed");
257 (void)snprintf(path, namelen, "%s/%s", (char *)PNPATH(dpn), name);
258
259 error = node_lookup_common(pu, opc, path, pnp);
260
261 free(path);
262
263 return error;
264 }
265
266 static int
267 node_lookup_common(pu, opc, path, pnp)
268 struct puffs_usermount *pu;
269 puffs_cookie_t opc;
270 const char *path;
271 struct puffs_node **pnp;
272 {
273 struct perfuse_state *ps;
274 perfuse_msg_t *pm;
275 struct fuse_entry_out *feo;
276 struct puffs_node *pn;
277 size_t len;
278 int error;
279
280 ps = puffs_getspecific(pu);
281
282 path = basename_r(path);
283 len = strlen(path) + 1;
284
285 pm = ps->ps_new_msg(pu, opc, FUSE_LOOKUP, len, NULL);
286 (void)strlcpy(_GET_INPAYLOAD(ps, pm, char *), path, len);
287
288 if ((error = XCHG_MSG(ps, pu, pm, sizeof(*feo))) != 0)
289 goto out;
290
291 feo = GET_OUTPAYLOAD(ps, pm, fuse_entry_out);
292
293 pn = perfuse_new_pn(pu, opc);
294 PERFUSE_NODE_DATA(pn)->pnd_ino = feo->nodeid;
295
296 fuse_attr_to_vap(ps, &pn->pn_va, &feo->attr);
297
298 if (pnp != NULL)
299 *pnp = pn;
300
301 out:
302 ps->ps_destroy_msg(pm);
303
304 return error;
305 }
306
307
308 /*
309 * Common final code for methods that create objects:
310 * perfuse_node_mkdir
311 * perfuse_node_mknod
312 * perfuse_node_symlink
313 */
314 static int
315 node_mk_common(pu, opc, pni, pcn, pm)
316 struct puffs_usermount *pu;
317 puffs_cookie_t opc;
318 struct puffs_newinfo *pni;
319 const struct puffs_cn *pcn;
320 perfuse_msg_t *pm;
321 {
322 struct perfuse_state *ps;
323 struct puffs_node *pn;
324 struct fuse_entry_out *feo;
325 struct fuse_setattr_in *fsi;
326 int error;
327
328 ps = puffs_getspecific(pu);
329
330 if ((error = XCHG_MSG(ps, pu, pm, sizeof(*feo))) != 0)
331 goto out;
332
333 feo = GET_OUTPAYLOAD(ps, pm, fuse_entry_out);
334 if (feo->nodeid == PERFUSE_UNKNOWN_INO)
335 DERRX(EX_SOFTWARE, "%s: no ino", __func__);
336
337 pn = perfuse_new_pn(pu, opc);
338 PERFUSE_NODE_DATA(pn)->pnd_ino = feo->nodeid;
339
340 fuse_attr_to_vap(ps, &pn->pn_va, &feo->attr);
341 puffs_newinfo_setcookie(pni, pn);
342 ps->ps_destroy_msg(pm);
343
344 /*
345 * Set owner and group
346 */
347 (void)puffs_cred_getuid(pcn->pcn_cred, &pn->pn_va.va_uid);
348 (void)puffs_cred_getgid(pcn->pcn_cred, &pn->pn_va.va_gid);
349
350 pm = ps->ps_new_msg(pu, (puffs_cookie_t)pn,
351 FUSE_SETATTR, sizeof(*fsi), NULL);
352 fsi = GET_INPAYLOAD(ps, pm, fuse_setattr_in);
353 fsi->uid = pn->pn_va.va_uid;
354 fsi->gid = pn->pn_va.va_gid;
355 fsi->valid = FUSE_FATTR_UID|FUSE_FATTR_GID;
356
357 /*
358 * A fuse_attr_out is returned, but we ignore it.
359 */
360 error = XCHG_MSG(ps, pu, pm, sizeof(struct fuse_attr_out));
361
362 out:
363 ps->ps_destroy_msg(pm);
364
365 return error;
366 }
367
368 static const char *
369 basename_r(string)
370 const char *string;
371 {
372 char *result;
373
374 if ((result = rindex(string, '/')) == NULL)
375 return string;
376
377 /*
378 * We are finished if this is not a trailing /
379 */
380 if (result[1] != '\0')
381 return result + 1;
382
383
384 /*
385 * Go back until we found something else than a /
386 */
387 while (result != string) {
388 result--;
389 if (result[0] != '/')
390 break;
391 }
392
393 if (result == string)
394 return string;
395
396 if ((result = rindex(string, '/')) == NULL)
397 return string;
398
399 return result + 1;
400
401 }
402
403 static ssize_t
404 fuse_to_dirent(pu, opc, fd, fd_len)
405 struct puffs_usermount *pu;
406 puffs_cookie_t opc;
407 struct fuse_dirent *fd;
408 size_t fd_len;
409 {
410 struct dirent *dents;
411 size_t dents_len;
412 ssize_t written;
413 uint64_t fd_offset;
414 struct fuse_dirent *fd_base;
415 size_t len;
416
417 fd_base = fd;
418 fd_offset = 0;
419 written = 0;
420 dents = PERFUSE_NODE_DATA(opc)->pnd_dirent;
421 dents_len = (size_t)PERFUSE_NODE_DATA(opc)->pnd_dirent_len;
422
423 do {
424 char *ndp;
425 size_t reclen;
426
427 reclen = _DIRENT_RECLEN(dents, fd->namelen);
428
429 /*
430 * Check we do not overflow the output buffer
431 * struct fuse_dirent is bigger than struct dirent,
432 * so we should always use fd_len and never reallocate
433 * later.
434 * If we have to reallocate,try to double the buffer
435 * each time so that we do not have to do it too often.
436 */
437 if (written + reclen > dents_len) {
438 if (dents_len == 0)
439 dents_len = fd_len;
440 else
441 dents_len =
442 MAX(2 * dents_len, written + reclen);
443
444 dents = PERFUSE_NODE_DATA(opc)->pnd_dirent;
445 if ((dents = realloc(dents, dents_len)) == NULL)
446 DERR(EX_OSERR, "malloc failed");
447
448 PERFUSE_NODE_DATA(opc)->pnd_dirent = dents;
449 PERFUSE_NODE_DATA(opc)->pnd_dirent_len = dents_len;
450
451 /*
452 * (void *) for delint
453 */
454 ndp = (char *)(void *)dents + written;
455 dents = (struct dirent *)(void *)ndp;
456 }
457
458
459
460 /*
461 * Filesystem was mounted without -o use_ino
462 * Perform a lookup to find it.
463 * XXX still broken
464 */
465 if (fd->ino == PERFUSE_UNKNOWN_INO) {
466 struct puffs_node *pn;
467
468 if (node_lookup_dir_nodot(pu, opc, fd->name,
469 fd->namelen, &pn) != 0)
470 DERRX(EX_SOFTWARE,
471 "node_lookup_dir_nodot failed");
472
473 fd->ino = PERFUSE_NODE_DATA(pn)->pnd_ino;
474 }
475
476 dents->d_fileno = fd->ino;
477 dents->d_reclen = (unsigned short)reclen;
478 dents->d_namlen = fd->namelen;
479 dents->d_type = fd->type;
480 strlcpy(dents->d_name, fd->name, fd->namelen + 1);
481
482 #ifdef PERFUSE_DEBUG
483 if (perfuse_diagflags & PDF_READDIR)
484 DPRINTF("%s: translated \"%s\" ino = %"PRId64"\n",
485 __func__, dents->d_name, dents->d_fileno);
486 #endif
487
488 dents = _DIRENT_NEXT(dents);
489 written += reclen;
490
491 /*
492 * Move to the next record.
493 * fd->off seems unreliable, for instance, flusterfs
494 * does not clear the unused bits, and we get
495 * 0xffffffffb9b95040 instead of just 0x40. Use
496 * record alignement instead.
497 */
498 len = FUSE_DIRENT_ALIGN(sizeof(*fd) + fd->namelen);
499 #ifdef PERFUSE_DEBUG
500 if (perfuse_diagflags & PDF_READDIR)
501 DPRINTF("%s: record at %"PRId64"/0x%"PRIx64" "
502 "length = %zd/0x%zx. "
503 "next record at %"PRId64"/0x%"PRIx64" "
504 "max %zd/0x%zx\n",
505 __func__, fd_offset, fd_offset, len, len,
506 fd_offset + len, fd_offset + len,
507 fd_len, fd_len);
508 #endif
509 fd_offset += len;
510
511 /*
512 * Check if next record is still within the packet
513 * If it is not, we reached the end of the buffer.
514 */
515 if (fd_offset >= fd_len)
516 break;
517
518 /*
519 * (void *) for delint
520 */
521 ndp = (char *)(void *)fd_base + (size_t)fd_offset;
522 fd = (struct fuse_dirent *)(void *)ndp;
523
524 } while (1 /* CONSTCOND */);
525
526 /*
527 * Adjust the dirent output length
528 */
529 if (written != -1)
530 PERFUSE_NODE_DATA(opc)->pnd_dirent_len = written;
531
532 return written;
533 }
534
535 /* ARGSUSED0 */
536 static int
537 readdir_buffered(ps, opc, dent, readoff,
538 reslen, pcr, eofflag, cookies, ncookies)
539 struct perfuse_state *ps;
540 puffs_cookie_t opc;
541 struct dirent *dent;
542 off_t *readoff;
543 size_t *reslen;
544 const struct puffs_cred *pcr;
545 int *eofflag;
546 off_t *cookies;
547 size_t *ncookies;
548 {
549 struct dirent *fromdent;
550 struct perfuse_node_data *pnd;
551 char *ndp;
552
553 pnd = PERFUSE_NODE_DATA(opc);
554
555 while (*readoff < pnd->pnd_dirent_len) {
556 /*
557 * (void *) for delint
558 */
559 ndp = (char *)(void *)pnd->pnd_dirent + (size_t)*readoff;
560 fromdent = (struct dirent *)(void *)ndp;
561
562 if (*reslen < _DIRENT_SIZE(fromdent))
563 break;
564
565 memcpy(dent, fromdent, _DIRENT_SIZE(fromdent));
566 *readoff += _DIRENT_SIZE(fromdent);
567 *reslen -= _DIRENT_SIZE(fromdent);
568
569 dent = _DIRENT_NEXT(dent);
570 }
571
572 #ifdef PERFUSE_DEBUG
573 if (perfuse_diagflags & PDF_READDIR)
574 DPRINTF("%s: readoff = %"PRId64", "
575 "pnd->pnd_dirent_len = %"PRId64"\n",
576 __func__, *readoff, pnd->pnd_dirent_len);
577 #endif
578 if (*readoff >= pnd->pnd_dirent_len) {
579 free(pnd->pnd_dirent);
580 pnd->pnd_dirent = NULL;
581 pnd->pnd_dirent_len = 0;
582 *eofflag = 1;
583 }
584
585 return 0;
586 }
587
588 /* ARGSUSED0 */
589 static void
590 requeue_request(pu, opc, type)
591 struct puffs_usermount *pu;
592 puffs_cookie_t opc;
593 enum perfuse_qtype type;
594 {
595 struct perfuse_cc_queue pcq;
596 struct perfuse_node_data *pnd;
597 #ifdef PERFUSE_DEBUG
598 struct perfuse_state *ps;
599
600 ps = perfuse_getspecific(pu);
601 #endif
602
603 /*
604 * XXX Add a lock he day we go multithreaded
605 */
606 pnd = PERFUSE_NODE_DATA(opc);
607 pcq.pcq_type = type;
608 pcq.pcq_cc = puffs_cc_getcc(pu);
609 TAILQ_INSERT_TAIL(&pnd->pnd_pcq, &pcq, pcq_next);
610
611 #ifdef PERFUSE_DEBUG
612
613 if (perfuse_diagflags & PDF_REQUEUE)
614 DPRINTF("%s: REQUEUE opc = %p, pcc = %p\n",
615 __func__, (void *)opc, pcq.pcq_cc);
616 #endif
617
618 puffs_cc_yield(pcq.pcq_cc);
619 TAILQ_REMOVE(&pnd->pnd_pcq, &pcq, pcq_next);
620
621 #ifdef PERFUSE_DEBUG
622 if (perfuse_diagflags & PDF_REQUEUE)
623 DPRINTF("%s: RESUME opc = %p, pcc = %p\n",
624 __func__, (void *)opc, pcq.pcq_cc);
625 #endif
626
627 return;
628 }
629
630 /* ARGSUSED0 */
631 static int
632 dequeue_requests(ps, opc, type, max)
633 struct perfuse_state *ps;
634 puffs_cookie_t opc;
635 enum perfuse_qtype type;
636 int max;
637 {
638 struct perfuse_cc_queue *pcq;
639 struct perfuse_node_data *pnd;
640 int dequeued;
641
642 /*
643 * XXX Add a lock he day we go multithreaded
644 */
645 pnd = PERFUSE_NODE_DATA(opc);
646 dequeued = 0;
647 TAILQ_FOREACH(pcq, &pnd->pnd_pcq, pcq_next) {
648 if (pcq->pcq_type != type)
649 continue;
650
651 #ifdef PERFUSE_DEBUG
652 if (perfuse_diagflags & PDF_REQUEUE)
653 DPRINTF("%s: SCHEDULE opc = %p, pcc = %p\n",
654 __func__, (void *)opc, pcq->pcq_cc);
655 #endif
656 puffs_cc_schedule(pcq->pcq_cc);
657
658 if (++dequeued == max)
659 break;
660 }
661
662 #ifdef PERFUSE_DEBUG
663 if (perfuse_diagflags & PDF_REQUEUE)
664 DPRINTF("%s: DONE opc = %p\n", __func__, (void *)opc);
665 #endif
666
667 return dequeued;
668 }
669
670 void
671 perfuse_fs_init(pu)
672 struct puffs_usermount *pu;
673 {
674 struct perfuse_state *ps;
675 perfuse_msg_t *pm;
676 struct fuse_init_in *fii;
677 struct fuse_init_out *fio;
678 int error;
679
680 ps = puffs_getspecific(pu);
681
682 if (puffs_mount(pu, ps->ps_target, ps->ps_mountflags, ps->ps_root) != 0)
683 DERR(EX_OSERR, "puffs_mount failed");
684
685 /*
686 * Linux 2.6.34.1 sends theses flags:
687 * FUSE_ASYNC_READ | FUSE_POSIX_LOCKS | FUSE_ATOMIC_O_TRUNC
688 * FUSE_EXPORT_SUPPORT | FUSE_BIG_WRITES | FUSE_DONT_MASK
689 *
690 * Linux also sets max_readahead at 32 pages (128 kB)
691 */
692 pm = ps->ps_new_msg(pu, 0, FUSE_INIT, sizeof(*fii), NULL);
693 fii = GET_INPAYLOAD(ps, pm, fuse_init_in);
694 fii->major = FUSE_KERNEL_VERSION;
695 fii->minor = FUSE_KERNEL_MINOR_VERSION;
696 fii->max_readahead = 32 * PAGE_SIZE;
697 fii->flags = (FUSE_ASYNC_READ|FUSE_POSIX_LOCKS|FUSE_ATOMIC_O_TRUNC);
698
699 if ((error = XCHG_MSG(ps, pu, pm, sizeof(*fio))) != 0)
700 DERRX(EX_SOFTWARE, "init message exchange failed (%d)", error);
701
702 fio = GET_OUTPAYLOAD(ps, pm, fuse_init_out);
703 ps->ps_max_readahead = fio->max_readahead;
704 ps->ps_max_write = fio->max_write;
705
706 ps->ps_destroy_msg(pm);
707
708 return;
709 }
710
711 int
712 perfuse_fs_unmount(pu, flags)
713 struct puffs_usermount *pu;
714 int flags;
715 {
716 perfuse_msg_t *pm;
717 struct perfuse_state *ps;
718 puffs_cookie_t opc;
719 int error;
720
721 ps = puffs_getspecific(pu);
722
723 opc = (puffs_cookie_t)puffs_getroot(pu);
724 pm = ps->ps_new_msg(pu, opc, FUSE_DESTROY, 0, NULL);
725
726 if ((error = XCHG_MSG(ps, pu, pm, UNSPEC_REPLY_LEN)) != 0) {
727 DWARN("unmount %s", ps->ps_target);
728 if (!(flags & MNT_FORCE))
729 goto out;
730 }
731
732 DPRINTF("%s unmounted, exit\n", ps->ps_target);
733
734 exit(0);
735 out:
736 ps->ps_destroy_msg(pm);
737
738 return error;
739 }
740
741 int
742 perfuse_fs_statvfs(pu, svfsb)
743 struct puffs_usermount *pu;
744 struct statvfs *svfsb;
745 {
746 struct perfuse_state *ps;
747 perfuse_msg_t *pm;
748 puffs_cookie_t opc;
749 struct fuse_statfs_out *fso;
750 int error;
751
752 ps = puffs_getspecific(pu);
753 opc = (puffs_cookie_t)puffs_getroot(pu);
754 pm = ps->ps_new_msg(pu, opc, FUSE_STATFS, 0, NULL);
755
756 if ((error = XCHG_MSG(ps, pu, pm, sizeof(*fso))) != 0)
757 goto out;
758
759 fso = GET_OUTPAYLOAD(ps, pm, fuse_statfs_out);
760 svfsb->f_flag = ps->ps_mountflags;
761 svfsb->f_bsize = fso->st.bsize;
762 svfsb->f_frsize = fso->st.frsize;
763 svfsb->f_iosize = ((struct puffs_node *)opc)->pn_va.va_blocksize;
764 svfsb->f_blocks = fso->st.blocks;
765 svfsb->f_bfree = fso->st.bfree;
766 svfsb->f_bavail = fso->st.bavail;
767 svfsb->f_bresvd = fso->st.bfree - fso->st.bavail;
768 svfsb->f_files = fso->st.files;
769 svfsb->f_ffree = fso->st.ffree;
770 svfsb->f_favail = fso->st.ffree;/* files not reserved for root */
771 svfsb->f_fresvd = 0; /* files reserved for root */
772
773 svfsb->f_syncreads = ps->ps_syncreads;
774 svfsb->f_syncwrites = ps->ps_syncwrites;
775
776 svfsb->f_asyncreads = ps->ps_asyncreads;
777 svfsb->f_asyncwrites = ps->ps_asyncwrites;
778
779 svfsb->f_fsidx.__fsid_val[0] = (int32_t)ps->ps_fsid;
780 svfsb->f_fsidx.__fsid_val[1] = 0;
781 svfsb->f_fsid = ps->ps_fsid;
782 svfsb->f_namemax = MAXPATHLEN; /* XXX */
783 svfsb->f_owner = ps->ps_owner_uid;
784
785 (void)strlcpy(svfsb->f_mntonname, ps->ps_target, _VFS_NAMELEN);
786
787 if (ps->ps_filesystemtype != NULL)
788 (void)strlcpy(svfsb->f_fstypename,
789 ps->ps_filesystemtype, _VFS_NAMELEN);
790 else
791 (void)strlcpy(svfsb->f_fstypename, "fuse", _VFS_NAMELEN);
792
793 if (ps->ps_source != NULL)
794 strlcpy(svfsb->f_mntfromname, ps->ps_source, _VFS_NAMELEN);
795 else
796 strlcpy(svfsb->f_mntfromname, _PATH_FUSE, _VFS_NAMELEN);
797 out:
798 ps->ps_destroy_msg(pm);
799
800 return error;
801 }
802
803 int
804 perfuse_fs_sync(pu, waitfor, pcr)
805 struct puffs_usermount *pu;
806 int waitfor;
807 const struct puffs_cred *pcr;
808 {
809 /*
810 * FUSE does not seem to have a FS sync callback.
811 * Maybe do not even register this callback
812 */
813 return puffs_fsnop_sync(pu, waitfor, pcr);
814 }
815
816 /* ARGSUSED0 */
817 int
818 perfuse_fs_fhtonode(pu, fid, fidsize, pni)
819 struct puffs_usermount *pu;
820 void *fid;
821 size_t fidsize;
822 struct puffs_newinfo *pni;
823 {
824 DERRX(EX_SOFTWARE, "%s: UNIMPLEMENTED (FATAL)", __func__);
825 return 0;
826 }
827
828 /* ARGSUSED0 */
829 int
830 perfuse_fs_nodetofh(pu, cookie, fid, fidsize)
831 struct puffs_usermount *pu;
832 puffs_cookie_t cookie;
833 void *fid;
834 size_t *fidsize;
835 {
836 DERRX(EX_SOFTWARE, "%s: UNIMPLEMENTED (FATAL)", __func__);
837 return 0;
838 }
839
840 #if 0
841 /* ARGSUSED0 */
842 void
843 perfuse_fs_extattrctl(pu, cmd, cookie, flags, namespace, attrname)
844 struct puffs_usermount *pu;
845 int cmd,
846 puffs_cookie_t *cookie;
847 int flags;
848 int namespace;
849 const char *attrname;
850 {
851 DERRX(EX_SOFTWARE, "%s: UNIMPLEMENTED (FATAL)", __func__);
852 return 0;
853 }
854 #endif /* 0 */
855
856 /* ARGSUSED0 */
857 void
858 perfuse_fs_suspend(pu, status)
859 struct puffs_usermount *pu;
860 int status;
861 {
862 return;
863 }
864
865
866
867 int
868 perfuse_node_lookup(pu, opc, pni, pcn)
869 struct puffs_usermount *pu;
870 puffs_cookie_t opc;
871 struct puffs_newinfo *pni;
872 const struct puffs_cn *pcn;
873 {
874 struct puffs_node *pn;
875 int error;
876
877 /*
878 * Special case for ..
879 */
880 if (PCNISDOTDOT(pcn)) {
881 pn = PERFUSE_NODE_DATA(opc)->pnd_parent;
882 PERFUSE_NODE_DATA(pn)->pnd_flags &= ~PND_RECLAIMED;
883
884 puffs_newinfo_setcookie(pni, pn);
885 puffs_newinfo_setvtype(pni, VDIR);
886
887 return 0;
888 }
889
890 /*
891 * XXX This is borrowed from librefuse,
892 * and __UNCONST is said to be fixed.
893 */
894 pn = puffs_pn_nodewalk(pu, puffs_path_walkcmp,
895 __UNCONST(&pcn->pcn_po_full));
896
897 if (pn == NULL) {
898 error = node_lookup_common(pu, opc, (char *)PCNPATH(pcn), &pn);
899 if (error != 0)
900 return error;
901 }
902
903 /*
904 * If that node had a pending reclaim, wipe it out.
905 */
906 PERFUSE_NODE_DATA(pn)->pnd_flags &= ~PND_RECLAIMED;
907
908 puffs_newinfo_setcookie(pni, pn);
909 puffs_newinfo_setvtype(pni, pn->pn_va.va_type);
910 puffs_newinfo_setsize(pni, (voff_t)pn->pn_va.va_size);
911 puffs_newinfo_setrdev(pni, pn->pn_va.va_rdev);
912
913 return 0;
914 }
915
916 int
917 perfuse_node_create(pu, opc, pni, pcn, vap)
918 struct puffs_usermount *pu;
919 puffs_cookie_t opc;
920 struct puffs_newinfo *pni;
921 const struct puffs_cn *pcn;
922 const struct vattr *vap;
923 {
924 perfuse_msg_t *pm;
925 struct perfuse_state *ps;
926 struct fuse_create_in *fci;
927 struct fuse_entry_out *feo;
928 struct fuse_open_out *foo;
929 struct puffs_node *pn;
930 const char *name;
931 size_t namelen;
932 size_t len;
933 int error;
934
935 /*
936 * Create an object require -WX permission in the parent directory
937 */
938 if (no_access(opc, pcn->pcn_cred, PUFFS_VWRITE|PUFFS_VEXEC))
939 return EACCES;
940
941 /*
942 * If create is unimplemented: Check that it does not
943 * already exists, and if not, do mknod and open
944 */
945 ps = puffs_getspecific(pu);
946 if (ps->ps_flags & PS_NO_CREAT) {
947 error = node_lookup_common(pu, opc, (char*)PCNPATH(pcn), &pn);
948 if (error == 0)
949 return EEXIST;
950
951 error = perfuse_node_mknod(pu, opc, pni, pcn, vap);
952 if (error != 0)
953 return error;
954
955 error = node_lookup_common(pu, opc, (char*)PCNPATH(pcn), &pn);
956 if (error != 0)
957 return error;
958
959 opc = (puffs_cookie_t)pn;
960
961 error = perfuse_node_open(pu, opc, FREAD|FWRITE, pcn->pcn_cred);
962 if (error != 0)
963 return error;
964
965 return 0;
966 }
967
968 name = basename_r((char *)PCNPATH(pcn));
969 namelen = strlen(name) + 1;
970 len = sizeof(*fci) + namelen;
971
972 /*
973 * flags should use O_WRONLY instead of O_RDWR, but it
974 * breaks when the caller tries to read from file.
975 *
976 * mode must contain file type (ie: S_IFREG), use VTTOIF(vap->va_type)
977 */
978 pm = ps->ps_new_msg(pu, opc, FUSE_CREATE, len, pcn->pcn_cred);
979 fci = GET_INPAYLOAD(ps, pm, fuse_create_in);
980 fci->flags = O_CREAT | O_TRUNC | O_RDWR;
981 fci->mode = vap->va_mode | VTTOIF(vap->va_type);
982 fci->umask = 0; /* Seems unused by libfuse */
983 (void)strlcpy((char*)(void *)(fci + 1), name, namelen);
984
985 len = sizeof(*feo) + sizeof(*foo);
986 if ((error = XCHG_MSG(ps, pu, pm, len)) != 0)
987 goto out;
988
989 feo = GET_OUTPAYLOAD(ps, pm, fuse_entry_out);
990 foo = (struct fuse_open_out *)(void *)(feo + 1);
991 if (feo->nodeid == PERFUSE_UNKNOWN_INO)
992 DERRX(EX_SOFTWARE, "%s: no ino", __func__);
993
994 /*
995 * Save the file handle and inode in node private data
996 * so that we can reuse it later
997 */
998 pn = perfuse_new_pn(pu, opc);
999 perfuse_new_fh((puffs_cookie_t)pn, foo->fh, FWRITE);
1000 PERFUSE_NODE_DATA(pn)->pnd_ino = feo->nodeid;
1001
1002 #ifdef PERFUSE_DEBUG
1003 if (perfuse_diagflags & PDF_FH)
1004 DPRINTF("%s: opc = %p, file = \"%s\", "
1005 "ino = %"PRId64", rfh = 0x%"PRIx64"\n",
1006 __func__, (void *)pn, (char *)PCNPATH(pcn),
1007 feo->nodeid, foo->fh);
1008 #endif
1009
1010 fuse_attr_to_vap(ps, &pn->pn_va, &feo->attr);
1011 puffs_newinfo_setcookie(pni, pn);
1012
1013 out:
1014 ps->ps_destroy_msg(pm);
1015
1016 /*
1017 * create is unimplmented, remember it for later,
1018 * and start over using mknod and open instead.
1019 */
1020 if (error == ENOSYS) {
1021 ps->ps_flags |= PS_NO_CREAT;
1022 return perfuse_node_create(pu, opc, pni, pcn, vap);
1023 }
1024
1025 return error;
1026 }
1027
1028
1029 int
1030 perfuse_node_mknod(pu, opc, pni, pcn, vap)
1031 struct puffs_usermount *pu;
1032 puffs_cookie_t opc;
1033 struct puffs_newinfo *pni;
1034 const struct puffs_cn *pcn;
1035 const struct vattr *vap;
1036 {
1037 struct perfuse_state *ps;
1038 perfuse_msg_t *pm;
1039 struct fuse_mknod_in *fmi;
1040 const char* path;
1041 size_t len;
1042
1043 /*
1044 * Only superuser can mknod objects other than
1045 * directories, files, socks, fifo and links.
1046 *
1047 * Create an object require -WX permission in the parent directory
1048 */
1049 switch (vap->va_type) {
1050 case VDIR: /* FALLTHROUGH */
1051 case VREG: /* FALLTHROUGH */
1052 case VFIFO: /* FALLTHROUGH */
1053 case VSOCK: /* FALLTHROUGH */
1054 case VLNK:
1055 if (no_access(opc, pcn->pcn_cred, PUFFS_VWRITE|PUFFS_VEXEC))
1056 return EACCES;
1057 break;
1058 default: /* VNON, VBLK, VCHR, VBAD */
1059 if (!puffs_cred_isjuggernaut(pcn->pcn_cred))
1060 return EACCES;
1061 break;
1062 }
1063
1064
1065 ps = puffs_getspecific(pu);
1066 path = basename_r((char *)PCNPATH(pcn));
1067 len = sizeof(*fmi) + strlen(path) + 1;
1068
1069 /*
1070 * mode must contain file type (ie: S_IFREG), use VTTOIF(vap->va_type)
1071 */
1072 pm = ps->ps_new_msg(pu, opc, FUSE_MKNOD, len, pcn->pcn_cred);
1073 fmi = GET_INPAYLOAD(ps, pm, fuse_mknod_in);
1074 fmi->mode = vap->va_mode | VTTOIF(vap->va_type);
1075 fmi->rdev = (uint32_t)vap->va_rdev;
1076 fmi->umask = 0; /* Seems unused bu libfuse */
1077 (void)strlcpy((char *)(void *)(fmi + 1), path, len - sizeof(*fmi));
1078
1079 return node_mk_common(pu, opc, pni, pcn, pm);
1080 }
1081
1082
1083 int
1084 perfuse_node_open(pu, opc, mode, pcr)
1085 struct puffs_usermount *pu;
1086 puffs_cookie_t opc;
1087 int mode;
1088 const struct puffs_cred *pcr;
1089 {
1090 struct perfuse_state *ps;
1091 struct perfuse_node_data *pnd;
1092 perfuse_msg_t *pm;
1093 mode_t pmode;
1094 mode_t fmode;
1095 int op;
1096 struct fuse_open_in *foi;
1097 struct fuse_open_out *foo;
1098 struct puffs_node *pn;
1099 int error;
1100
1101 ps = puffs_getspecific(pu);
1102 pnd = PERFUSE_NODE_DATA(opc);
1103
1104 pn = (struct puffs_node *)opc;
1105 if (puffs_pn_getvap(pn)->va_type == VDIR) {
1106 op = FUSE_OPENDIR;
1107 pmode = PUFFS_VREAD|PUFFS_VEXEC;
1108 } else {
1109 op = FUSE_OPEN;
1110 if (mode & FWRITE)
1111 pmode = PUFFS_VWRITE|PUFFS_VREAD;
1112 else
1113 pmode = PUFFS_VREAD;
1114 }
1115
1116 /*
1117 * Opening a directory require R-X on the directory
1118 * Opening a file requires R-- for reading, -W- for writing
1119 * In both cases, --X is required on the parent.
1120 */
1121 if (no_access((puffs_cookie_t)pnd->pnd_parent, pcr, PUFFS_VEXEC))
1122 return EACCES;
1123
1124 if (no_access(opc, pcr, pmode))
1125 return EACCES;
1126
1127 /*
1128 * libfuse docs say O_CREAT should not be set.
1129 */
1130 mode &= ~O_CREAT;
1131
1132 /*
1133 * Do not open twice, and do not reopen for reading
1134 * if we already have write handle.
1135 * Directories are always open with read access only,
1136 * whatever flags we get.
1137 */
1138 if (op == FUSE_OPENDIR)
1139 mode = (mode & ~(FREAD|FWRITE)) | FREAD;
1140 if ((mode & FREAD) && (pnd->pnd_flags & PND_RFH))
1141 return 0;
1142 if ((mode & FWRITE) && (pnd->pnd_flags & PND_WFH))
1143 return 0;
1144
1145 /*
1146 * Convert PUFFS mode to FUSE mode: convert FREAD/FWRITE
1147 * to O_RDONLY/O_WRONLY while perserving the other options.
1148 */
1149 fmode = mode & ~(FREAD|FWRITE);
1150 fmode |= (mode & FWRITE) ? O_RDWR : O_RDONLY;
1151
1152 pm = ps->ps_new_msg(pu, opc, op, sizeof(*foi), pcr);
1153 foi = GET_INPAYLOAD(ps, pm, fuse_open_in);
1154 foi->flags = fmode;
1155 foi->unused = 0;
1156
1157 if ((error = XCHG_MSG(ps, pu, pm, sizeof(*foo))) != 0)
1158 goto out;
1159
1160 foo = GET_OUTPAYLOAD(ps, pm, fuse_open_out);
1161
1162 /*
1163 * Save the file handle in node private data
1164 * so that we can reuse it later
1165 */
1166 perfuse_new_fh((puffs_cookie_t)pn, foo->fh, mode);
1167
1168 #ifdef PERFUSE_DEBUG
1169 if (perfuse_diagflags & PDF_FH)
1170 DPRINTF("%s: opc = %p, file = \"%s\", "
1171 "ino = %"PRId64", %s%sfh = 0x%"PRIx64"\n",
1172 __func__, (void *)opc,
1173 (char *)PNPATH((struct puffs_node *)opc),
1174 pnd->pnd_ino, mode & FREAD ? "r" : "",
1175 mode & FWRITE ? "w" : "", foo->fh);
1176 #endif
1177 out:
1178 ps->ps_destroy_msg(pm);
1179
1180 return error;
1181 }
1182
1183 /* ARGSUSED0 */
1184 int
1185 perfuse_node_close(pu, opc, flags, pcr)
1186 struct puffs_usermount *pu;
1187 puffs_cookie_t opc;
1188 int flags;
1189 const struct puffs_cred *pcr;
1190 {
1191 struct puffs_node *pn;
1192 struct perfuse_node_data *pnd;
1193
1194 pn = (struct puffs_node *)opc;
1195 pnd = PERFUSE_NODE_DATA(opc);
1196
1197 if (!(pnd->pnd_flags & PND_OPEN))
1198 return EBADF;
1199
1200 /*
1201 * The NetBSD kernel will send sync and setattr(mtime, ctime)
1202 * afer a close on a regular file. Some FUSE filesystem will
1203 * assume theses operations are performed on open files. We
1204 * therefore postpone the close operation at reclaim time.
1205 */
1206 if (puffs_pn_getvap(pn)->va_type != VREG)
1207 return node_close_common(pu, opc, flags);
1208
1209 return 0;
1210 }
1211
1212 int
1213 perfuse_node_access(pu, opc, mode, pcr)
1214 struct puffs_usermount *pu;
1215 puffs_cookie_t opc;
1216 int mode;
1217 const struct puffs_cred *pcr;
1218 {
1219 perfuse_msg_t *pm;
1220 struct perfuse_state *ps;
1221 struct fuse_access_in *fai;
1222 int error;
1223
1224 if (PERFUSE_NODE_DATA(opc)->pnd_flags & PND_REMOVED)
1225 return ENOENT;
1226
1227 /*
1228 * If we previously detected the filesystem does not
1229 * implement access(), short-circuit the call and skip
1230 * to libpffs access() emulation.
1231 */
1232 ps = puffs_getspecific(pu);
1233 if (ps->ps_flags & PS_NO_ACCESS) {
1234 error = ENOSYS;
1235 } else {
1236 pm = ps->ps_new_msg(pu, opc, FUSE_ACCESS, sizeof(*fai), pcr);
1237 fai = GET_INPAYLOAD(ps, pm, fuse_access_in);
1238 fai->mask = mode;
1239
1240 error = XCHG_MSG(ps, pu, pm, NO_PAYLOAD_REPLY_LEN);
1241 ps->ps_destroy_msg(pm);
1242 }
1243
1244 if (error == ENOSYS) {
1245 struct fuse_getattr_in *fgi;
1246 struct fuse_attr_out *fao;
1247
1248 ps->ps_flags |= PS_NO_ACCESS;
1249
1250 pm = ps->ps_new_msg(pu, opc, FUSE_GETATTR,
1251 sizeof(*fgi), NULL);
1252 fgi = GET_INPAYLOAD(ps, pm, fuse_getattr_in);
1253 fgi->getattr_flags = 0;
1254 fgi->dummy = 0;
1255 fgi->fh = perfuse_get_fh(opc, FREAD);
1256
1257 #ifdef PERFUSE_DEBUG
1258 if (perfuse_diagflags & PDF_FH)
1259 DPRINTF("%s: opc = %p, ino = %"PRId64", "
1260 "fh = 0x%"PRIx64"\n", __func__, (void *)opc,
1261 PERFUSE_NODE_DATA(opc)->pnd_ino, fgi->fh);
1262 #endif
1263 if ((error = XCHG_MSG(ps, pu, pm, sizeof(*fao))) != 0) {
1264 ps->ps_destroy_msg(pm);
1265 goto out;
1266 }
1267
1268 fao = GET_OUTPAYLOAD(ps, pm, fuse_attr_out);
1269
1270 error = puffs_access(VREG, fao->attr.mode, fao->attr.uid,
1271 fao->attr.gid, (mode_t)mode, pcr);
1272
1273 ps->ps_destroy_msg(pm);
1274 }
1275
1276 out:
1277 return error;
1278 }
1279
1280 int
1281 perfuse_node_getattr(pu, opc, vap, pcr)
1282 struct puffs_usermount *pu;
1283 puffs_cookie_t opc;
1284 struct vattr *vap;
1285 const struct puffs_cred *pcr;
1286 {
1287 perfuse_msg_t *pm;
1288 struct perfuse_state *ps;
1289 struct fuse_getattr_in *fgi;
1290 struct fuse_attr_out *fao;
1291 int error;
1292
1293 if (PERFUSE_NODE_DATA(opc)->pnd_flags & PND_REMOVED)
1294 return ENOENT;
1295
1296 /*
1297 * getattr requires --X on the parent directory
1298 */
1299 if (no_access((puffs_cookie_t)PERFUSE_NODE_DATA(opc)->pnd_parent,
1300 pcr, PUFFS_VEXEC))
1301 return EACCES;
1302
1303 ps = puffs_getspecific(pu);
1304
1305 /*
1306 * FUSE_GETATTR_FH must be set in fgi->flags
1307 * if we use for fgi->fh, but we do not.
1308 */
1309 pm = ps->ps_new_msg(pu, opc, FUSE_GETATTR, sizeof(*fgi), pcr);
1310 fgi = GET_INPAYLOAD(ps, pm, fuse_getattr_in);
1311 fgi->getattr_flags = 0;
1312 fgi->dummy = 0;
1313 fgi->fh = 0;
1314
1315 if ((error = XCHG_MSG(ps, pu, pm, sizeof(*fao))) != 0)
1316 goto out;
1317
1318 fao = GET_OUTPAYLOAD(ps, pm, fuse_attr_out);
1319
1320 /*
1321 * The message from filesystem has a cache timeout
1322 * XXX this is ignored yet, is that right?
1323 *
1324 * We also set birthtime, flags, filerev,vaflags to 0.
1325 * This seems the best bet, since the information is
1326 * not available from filesystem.
1327 */
1328 fuse_attr_to_vap(ps, vap, &fao->attr);
1329
1330 out:
1331 ps->ps_destroy_msg(pm);
1332
1333 return error;
1334 }
1335
1336 int
1337 perfuse_node_setattr(pu, opc, vap, pcr)
1338 struct puffs_usermount *pu;
1339 puffs_cookie_t opc;
1340 const struct vattr *vap;
1341 const struct puffs_cred *pcr;
1342 {
1343 perfuse_msg_t *pm;
1344 uint64_t fh;
1345 struct perfuse_state *ps;
1346 struct perfuse_node_data *pnd;
1347 struct fuse_setattr_in *fsi;
1348 int error;
1349 struct vattr *old_va;
1350
1351 ps = puffs_getspecific(pu);
1352 pnd = PERFUSE_NODE_DATA(opc);
1353
1354 /*
1355 * The only operation we can do once the file is removed
1356 * is to resize it, and we can do it only if it is open.
1357 */
1358 if (pnd->pnd_flags & PND_REMOVED) {
1359 if (!(pnd->pnd_flags & PND_OPEN))
1360 return ENOENT;
1361
1362 if (vap->va_size == (u_quad_t)PUFFS_VNOVAL)
1363 return 0;
1364 }
1365
1366 /*
1367 * setattr requires --X on the parent directory
1368 */
1369 if (no_access((puffs_cookie_t)pnd->pnd_parent, pcr, PUFFS_VEXEC))
1370 return EACCES;
1371
1372 old_va = puffs_pn_getvap((struct puffs_node *)opc);
1373
1374 /*
1375 * Check for permission to change size
1376 */
1377 if ((vap->va_size != (u_quad_t)PUFFS_VNOVAL) &&
1378 no_access(opc, pcr, PUFFS_VWRITE))
1379 return EACCES;
1380
1381 /*
1382 * Check for permission to change dates
1383 */
1384 if (((vap->va_atime.tv_sec != (time_t)PUFFS_VNOVAL) ||
1385 (vap->va_mtime.tv_sec != (time_t)PUFFS_VNOVAL)) &&
1386 (puffs_access_times(old_va->va_uid, old_va->va_gid,
1387 old_va->va_mode, 0, pcr) != 0))
1388 return EACCES;
1389
1390 /*
1391 * Check for permission to change owner and group
1392 */
1393 if (((vap->va_uid != (uid_t)PUFFS_VNOVAL) ||
1394 (vap->va_gid != (gid_t)PUFFS_VNOVAL)) &&
1395 (puffs_access_chown(old_va->va_uid, old_va->va_gid,
1396 vap->va_uid, vap->va_gid, pcr)) != 0)
1397 return EACCES;
1398
1399 /*
1400 * Check for permission to change permissions
1401 */
1402 if ((vap->va_mode != (mode_t)PUFFS_VNOVAL) &&
1403 (puffs_access_chmod(old_va->va_uid, old_va->va_gid,
1404 old_va->va_type, vap->va_mode, pcr)) != 0)
1405 return EACCES;
1406
1407 /*
1408 * It seems troublesome to resize a file while
1409 * a write is just beeing done. Wait for
1410 * it to finish.
1411 */
1412 if (vap->va_size != (u_quad_t)PUFFS_VNOVAL)
1413 while (pnd->pnd_flags & PND_INWRITE)
1414 requeue_request(pu, opc, PCQ_AFTERWRITE);
1415
1416
1417 pm = ps->ps_new_msg(pu, opc, FUSE_SETATTR, sizeof(*fsi), pcr);
1418 fsi = GET_INPAYLOAD(ps, pm, fuse_setattr_in);
1419 fsi->valid = 0;
1420
1421 if (pnd->pnd_flags & PND_WFH) {
1422 fh = perfuse_get_fh(opc, FWRITE);
1423 fsi->fh = fh;
1424 fsi->valid |= FUSE_FATTR_FH;
1425 }
1426
1427 if (vap->va_size != (u_quad_t)PUFFS_VNOVAL) {
1428 fsi->size = vap->va_size;
1429 fsi->valid |= FUSE_FATTR_SIZE;
1430 }
1431
1432 if (vap->va_atime.tv_sec != (time_t)PUFFS_VNOVAL) {
1433 fsi->atime = vap->va_atime.tv_sec;;
1434 fsi->atimensec = (uint32_t)vap->va_atime.tv_nsec;;
1435 fsi->valid |= (FUSE_FATTR_ATIME|FUSE_FATTR_ATIME_NOW);
1436 }
1437
1438 if (vap->va_mtime.tv_sec != (time_t)PUFFS_VNOVAL) {
1439 fsi->mtime = vap->va_mtime.tv_sec;;
1440 fsi->mtimensec = (uint32_t)vap->va_mtime.tv_nsec;;
1441 fsi->valid |= (FUSE_FATTR_MTIME|FUSE_FATTR_MTIME_NOW);
1442 }
1443
1444 if (vap->va_mode != (mode_t)PUFFS_VNOVAL) {
1445 fsi->mode = vap->va_mode;
1446 fsi->valid |= FUSE_FATTR_MODE;
1447 }
1448
1449 if (vap->va_uid != (uid_t)PUFFS_VNOVAL) {
1450 fsi->uid = vap->va_uid;
1451 fsi->valid |= FUSE_FATTR_UID;
1452 }
1453
1454 if (vap->va_gid != (gid_t)PUFFS_VNOVAL) {
1455 fsi->gid = vap->va_gid;
1456 fsi->valid |= FUSE_FATTR_GID;
1457 }
1458
1459 if (pnd->pnd_lock_owner != 0) {
1460 fsi->lock_owner = pnd->pnd_lock_owner;
1461 fsi->valid |= FUSE_FATTR_LOCKOWNER;
1462 }
1463
1464 /*
1465 * If node was removed, ignore anything but resize
1466 * This works around glusterfs'
1467 * "SETATTR (null) (fuse_loc_fill() failed), ret = -2"
1468 */
1469 if (pnd->pnd_flags & PND_REMOVED)
1470 fsi->valid &=
1471 (FUSE_FATTR_SIZE | FUSE_FATTR_FH | FUSE_FATTR_LOCKOWNER);
1472
1473 /*
1474 * A fuse_attr_out is returned, but we ignore it.
1475 */
1476 error = XCHG_MSG(ps, pu, pm, sizeof(struct fuse_attr_out));
1477
1478 ps->ps_destroy_msg(pm);
1479
1480 return error;
1481 }
1482
1483 int
1484 perfuse_node_poll(pu, opc, events)
1485 struct puffs_usermount *pu;
1486 puffs_cookie_t opc;
1487 int *events;
1488 {
1489 struct perfuse_state *ps;
1490 perfuse_msg_t *pm;
1491 struct fuse_poll_in *fpi;
1492 struct fuse_poll_out *fpo;
1493 int error;
1494
1495 ps = puffs_getspecific(pu);
1496 /*
1497 * kh is set if FUSE_POLL_SCHEDULE_NOTIFY is set.
1498 */
1499 pm = ps->ps_new_msg(pu, opc, FUSE_POLL, sizeof(*fpi), NULL);
1500 fpi = GET_INPAYLOAD(ps, pm, fuse_poll_in);
1501 fpi->fh = perfuse_get_fh(opc, FREAD);
1502 fpi->kh = 0;
1503 fpi->flags = 0;
1504
1505 #ifdef PERFUSE_DEBUG
1506 if (perfuse_diagflags & PDF_FH)
1507 DPRINTF("%s: opc = %p, ino = %"PRId64", fh = 0x%"PRIx64"\n",
1508 __func__, (void *)opc,
1509 PERFUSE_NODE_DATA(opc)->pnd_ino, fpi->fh);
1510 #endif
1511 if ((error = XCHG_MSG(ps, pu, pm, sizeof(*fpo))) != 0)
1512 goto out;
1513
1514 fpo = GET_OUTPAYLOAD(ps, pm, fuse_poll_out);
1515 *events = fpo->revents;
1516 out:
1517 ps->ps_destroy_msg(pm);
1518
1519 return error;
1520 }
1521
1522 /* ARGSUSED0 */
1523 int
1524 perfuse_node_mmap(pu, opc, flags, pcr)
1525 struct puffs_usermount *pu;
1526 puffs_cookie_t opc;
1527 int flags;
1528 const struct puffs_cred *pcr;
1529 {
1530 /*
1531 * Not implemented anymore in libfuse
1532 */
1533 return ENOSYS;
1534 }
1535
1536 /* ARGSUSED2 */
1537 int
1538 perfuse_node_fsync(pu, opc, pcr, flags, offlo, offhi)
1539 struct puffs_usermount *pu;
1540 puffs_cookie_t opc;
1541 const struct puffs_cred *pcr;
1542 int flags;
1543 off_t offlo;
1544 off_t offhi;
1545 {
1546 perfuse_msg_t *pm;
1547 struct perfuse_state *ps;
1548 struct perfuse_node_data *pnd;
1549 struct fuse_fsync_in *ffi;
1550 uint64_t fh;
1551 int open_self;
1552 int error;
1553
1554 pm = NULL;
1555 open_self = 0;
1556
1557 /*
1558 * If we previously detected it as unimplemented,
1559 * skip the call to the filesystem.
1560 */
1561 ps = puffs_getspecific(pu);
1562 if (ps->ps_flags == PS_NO_FSYNC)
1563 return ENOSYS;
1564
1565 /*
1566 * Do not sync if there are no change to sync
1567 * XXX remove that test if we implement mmap
1568 */
1569 pnd = PERFUSE_NODE_DATA(opc);
1570 #ifdef PERFUSE_DEBUG
1571 if (perfuse_diagflags & PDF_SYNC)
1572 DPRINTF("%s: TEST opc = %p, file = \"%s\" is %sdirty\n",
1573 __func__, (void*)opc,
1574 (char *)PNPATH((struct puffs_node *)opc),
1575 pnd->pnd_flags & PND_DIRTY ? "" : "not ");
1576 #endif
1577 if (!(pnd->pnd_flags & PND_DIRTY))
1578 return 0;
1579
1580 /*
1581 * It seems NetBSD can call fsync without open first
1582 * glusterfs complain in such a situation:
1583 * "FSYNC() ERR => -1 (Invalid argument)"
1584 */
1585 if (!(pnd->pnd_flags & PND_OPEN)) {
1586 if ((error = perfuse_node_open(pu, opc, FWRITE, pcr)) != 0)
1587 goto out;
1588 open_self = 1;
1589 }
1590
1591 fh = perfuse_get_fh(opc, FWRITE);
1592
1593 /*
1594 * If fsync_flags is set, meta data should not be flushed.
1595 */
1596 pm = ps->ps_new_msg(pu, opc, FUSE_FSYNC, sizeof(*ffi), NULL);
1597 ffi = GET_INPAYLOAD(ps, pm, fuse_fsync_in);
1598 ffi->fh = fh;
1599 ffi->fsync_flags = (flags & FFILESYNC) ? 0 : 1;
1600
1601 #ifdef PERFUSE_DEBUG
1602 if (perfuse_diagflags & PDF_FH)
1603 DPRINTF("%s: opc = %p, ino = %"PRId64", fh = 0x%"PRIx64"\n",
1604 __func__, (void *)opc,
1605 PERFUSE_NODE_DATA(opc)->pnd_ino, ffi->fh);
1606 #endif
1607
1608 if ((error = XCHG_MSG(ps, pu, pm, NO_PAYLOAD_REPLY_LEN)) != 0)
1609 goto out;
1610
1611 /*
1612 * No reply beyond fuse_out_header: nothing to do on success
1613 * just clear the dirty flag
1614 */
1615 pnd->pnd_flags &= ~PND_DIRTY;
1616
1617 #ifdef PERFUSE_DEBUG
1618 if (perfuse_diagflags & PDF_SYNC)
1619 DPRINTF("%s: CLEAR opc = %p, file = \"%s\"\n",
1620 __func__, (void*)opc,
1621 (char *)PNPATH((struct puffs_node *)opc));
1622 #endif
1623
1624 out:
1625 if (error == ENOSYS)
1626 ps->ps_flags |= PS_NO_FSYNC;
1627
1628 if (pm != NULL)
1629 ps->ps_destroy_msg(pm);
1630
1631 if (open_self)
1632 (void)node_close_common(pu, opc, FWRITE);
1633
1634 return error;
1635 }
1636
1637 /* ARGSUSED0 */
1638 int
1639 perfuse_node_seek(pu, opc, oldoff, newoff, pcr)
1640 struct puffs_usermount *pu;
1641 puffs_cookie_t opc;
1642 off_t oldoff;
1643 off_t newoff;
1644 const struct puffs_cred *pcr;
1645 {
1646 /*
1647 * XXX what should I do with oldoff?
1648 * XXX where is the newoffset returned?
1649 * XXX the held seek pointer seems just unused
1650 */
1651 PERFUSE_NODE_DATA(opc)->pnd_offset = newoff;
1652
1653 return 0;
1654 }
1655
1656 int
1657 perfuse_node_remove(pu, opc, targ, pcn)
1658 struct puffs_usermount *pu;
1659 puffs_cookie_t opc;
1660 puffs_cookie_t targ;
1661 const struct puffs_cn *pcn;
1662 {
1663 struct perfuse_state *ps;
1664 struct puffs_node *pn;
1665 struct perfuse_node_data *pnd;
1666 perfuse_msg_t *pm;
1667 char *path;
1668 const char *name;
1669 size_t len;
1670 int error;
1671
1672 pnd = PERFUSE_NODE_DATA(opc);
1673
1674 /*
1675 * remove requires -WX on the parent directory
1676 * no right required on the object.
1677 */
1678 if (no_access((puffs_cookie_t)pnd->pnd_parent,
1679 pcn->pcn_cred, PUFFS_VWRITE|PUFFS_VEXEC))
1680 return EACCES;
1681
1682 if (targ == NULL)
1683 DERRX(EX_SOFTWARE, "%s: targ is NULL", __func__);
1684
1685 ps = puffs_getspecific(pu);
1686 pn = (struct puffs_node *)targ;
1687 name = basename_r((char *)PNPATH(pn));
1688 len = strlen(name) + 1;
1689
1690 pm = ps->ps_new_msg(pu, opc, FUSE_UNLINK, len, pcn->pcn_cred);
1691 path = _GET_INPAYLOAD(ps, pm, char *);
1692 (void)strlcpy(path, name, len);
1693
1694 if ((error = XCHG_MSG(ps, pu, pm, UNSPEC_REPLY_LEN)) != 0)
1695 goto out;
1696
1697 if (puffs_inval_namecache_dir(pu, opc) != 0)
1698 DERR(EX_OSERR, "puffs_inval_namecache_dir failed");
1699
1700 puffs_setback(puffs_cc_getcc(pu), PUFFS_SETBACK_NOREF_N2);
1701
1702 PERFUSE_NODE_DATA(targ)->pnd_flags |= PND_REMOVED;
1703
1704 /*
1705 * Reclaim should take care of decreasing pnd_childcount
1706 */
1707 out:
1708 ps->ps_destroy_msg(pm);
1709
1710 return error;
1711 }
1712
1713 int
1714 perfuse_node_link(pu, opc, targ, pcn)
1715 struct puffs_usermount *pu;
1716 puffs_cookie_t opc;
1717 puffs_cookie_t targ;
1718 const struct puffs_cn *pcn;
1719 {
1720 struct perfuse_state *ps;
1721 perfuse_msg_t *pm;
1722 const char *name;
1723 size_t len;
1724 struct puffs_node *pn;
1725 struct fuse_link_in *fli;
1726 int error;
1727
1728 /*
1729 * Create an object require -WX permission in the parent directory
1730 */
1731 if (no_access(opc, pcn->pcn_cred, PUFFS_VWRITE|PUFFS_VEXEC))
1732 return EACCES;
1733
1734
1735 ps = puffs_getspecific(pu);
1736 pn = (struct puffs_node *)targ;
1737 name = basename_r((char *)PCNPATH(pcn));
1738 len = sizeof(*fli) + strlen(name) + 1;
1739
1740 pm = ps->ps_new_msg(pu, opc, FUSE_LINK, len, pcn->pcn_cred);
1741 fli = GET_INPAYLOAD(ps, pm, fuse_link_in);
1742 fli->oldnodeid = PERFUSE_NODE_DATA(pn)->pnd_ino;
1743 (void)strlcpy((char *)(void *)(fli + 1), name, len - sizeof(*fli));
1744
1745 error = XCHG_MSG(ps, pu, pm, UNSPEC_REPLY_LEN);
1746
1747 ps->ps_destroy_msg(pm);
1748
1749 return error;
1750 }
1751
1752 /* targ is unused since the name is in pcn_targ */
1753 /* ARGSUSED5 */
1754 int
1755 perfuse_node_rename(pu, opc, src, pcn_src, targ_dir, targ, pcn_targ)
1756 struct puffs_usermount *pu;
1757 puffs_cookie_t opc;
1758 puffs_cookie_t src;
1759 const struct puffs_cn *pcn_src;
1760 puffs_cookie_t targ_dir;
1761 puffs_cookie_t targ;
1762 const struct puffs_cn *pcn_targ;
1763 {
1764 struct perfuse_state *ps;
1765 perfuse_msg_t *pm;
1766 struct fuse_rename_in *fri;
1767 const char *newname;
1768 const char *oldname;
1769 char *np;
1770 int error;
1771 size_t len;
1772 size_t newname_len;
1773 size_t oldname_len;
1774
1775 /*
1776 * move requires -WX on source and destination directory
1777 */
1778 if (no_access(opc, pcn_src->pcn_cred, PUFFS_VWRITE|PUFFS_VEXEC) ||
1779 no_access(targ_dir, pcn_targ->pcn_cred, PUFFS_VWRITE|PUFFS_VEXEC))
1780 return EACCES;
1781
1782 ps = puffs_getspecific(pu);
1783 newname = basename_r((char *)PCNPATH(pcn_targ));
1784 newname_len = strlen(newname) + 1;
1785 oldname = basename_r((char *)PCNPATH(pcn_src));
1786 oldname_len = strlen(oldname) + 1;
1787
1788 len = sizeof(*fri) + oldname_len + newname_len;
1789 pm = ps->ps_new_msg(pu, opc, FUSE_RENAME, len, pcn_src->pcn_cred);
1790 fri = GET_INPAYLOAD(ps, pm, fuse_rename_in);
1791 fri->newdir = PERFUSE_NODE_DATA(targ_dir)->pnd_ino;
1792 np = (char *)(void *)(fri + 1);
1793 (void)strlcpy(np, oldname, oldname_len);
1794 np += oldname_len;
1795 (void)strlcpy(np, newname, newname_len);
1796
1797 if ((error = XCHG_MSG(ps, pu, pm, UNSPEC_REPLY_LEN)) != 0)
1798 goto out;
1799
1800 /*
1801 * Update source and destination directories child count
1802 * Update moved object parent directory
1803 */
1804 PERFUSE_NODE_DATA(opc)->pnd_childcount--;
1805 PERFUSE_NODE_DATA(targ_dir)->pnd_childcount++;
1806 PERFUSE_NODE_DATA(src)->pnd_parent = targ_dir;
1807
1808 out:
1809 ps->ps_destroy_msg(pm);
1810
1811 return error;
1812 }
1813
1814 int
1815 perfuse_node_mkdir(pu, opc, pni, pcn, vap)
1816 struct puffs_usermount *pu;
1817 puffs_cookie_t opc;
1818 struct puffs_newinfo *pni;
1819 const struct puffs_cn *pcn;
1820 const struct vattr *vap;
1821 {
1822 struct perfuse_state *ps;
1823 perfuse_msg_t *pm;
1824 struct fuse_mkdir_in *fmi;
1825 const char *path;
1826 size_t len;
1827
1828 /*
1829 * Create an object require -WX permission in the parent directory
1830 */
1831 if (no_access(opc, pcn->pcn_cred, PUFFS_VWRITE|PUFFS_VEXEC))
1832 return EACCES;
1833
1834 ps = puffs_getspecific(pu);
1835 path = basename_r((char *)PCNPATH(pcn));
1836 len = sizeof(*fmi) + strlen(path) + 1;
1837
1838 pm = ps->ps_new_msg(pu, opc, FUSE_MKDIR, len, pcn->pcn_cred);
1839 fmi = GET_INPAYLOAD(ps, pm, fuse_mkdir_in);
1840 fmi->mode = vap->va_mode;
1841 fmi->umask = 0; /* Seems unused by libfuse? */
1842 (void)strlcpy((char *)(void *)(fmi + 1), path, len - sizeof(*fmi));
1843
1844 return node_mk_common(pu, opc, pni, pcn, pm);
1845 }
1846
1847
1848 int
1849 perfuse_node_rmdir(pu, opc, targ, pcn)
1850 struct puffs_usermount *pu;
1851 puffs_cookie_t opc;
1852 puffs_cookie_t targ;
1853 const struct puffs_cn *pcn;
1854 {
1855 struct perfuse_state *ps;
1856 struct perfuse_node_data *pnd;
1857 perfuse_msg_t *pm;
1858 struct puffs_node *pn;
1859 char *path;
1860 const char *name;
1861 size_t len;
1862 int error;
1863
1864 pnd = PERFUSE_NODE_DATA(opc);
1865
1866 /*
1867 * remove requires -WX on the parent directory
1868 * no right required on the object.
1869 */
1870 if (no_access((puffs_cookie_t)pnd->pnd_parent,
1871 pcn->pcn_cred, PUFFS_VWRITE|PUFFS_VEXEC))
1872 return EACCES;
1873
1874 ps = puffs_getspecific(pu);
1875 pn = (struct puffs_node *)targ;
1876 name = basename_r((char *)PNPATH(pn));
1877 len = strlen(name) + 1;
1878
1879 pm = ps->ps_new_msg(pu, opc, FUSE_RMDIR, len, pcn->pcn_cred);
1880 path = _GET_INPAYLOAD(ps, pm, char *);
1881 (void)strlcpy(path, name, len);
1882
1883 if ((error = XCHG_MSG(ps, pu, pm, UNSPEC_REPLY_LEN)) != 0)
1884 goto out;
1885
1886 if (puffs_inval_namecache_dir(pu, opc) != 0)
1887 DERR(EX_OSERR, "puffs_inval_namecache_dir failed");
1888
1889 puffs_setback(puffs_cc_getcc(pu), PUFFS_SETBACK_NOREF_N2);
1890
1891 PERFUSE_NODE_DATA(targ)->pnd_flags |= PND_REMOVED;
1892
1893 out:
1894 ps->ps_destroy_msg(pm);
1895
1896 return error;
1897 }
1898
1899 /* vap is unused */
1900 /* ARGSUSED4 */
1901 int
1902 perfuse_node_symlink(pu, opc, pni, pcn_src, vap, link_target)
1903 struct puffs_usermount *pu;
1904 puffs_cookie_t opc;
1905 struct puffs_newinfo *pni;
1906 const struct puffs_cn *pcn_src;
1907 const struct vattr *vap;
1908 const char *link_target;
1909 {
1910 struct perfuse_state *ps;
1911 perfuse_msg_t *pm;
1912 char *np;
1913 const char *path;
1914 size_t path_len;
1915 size_t linkname_len;
1916 size_t len;
1917
1918 /*
1919 * Create an object require -WX permission in the parent directory
1920 */
1921 if (no_access(opc, pcn_src->pcn_cred, PUFFS_VWRITE|PUFFS_VEXEC))
1922 return EACCES;
1923
1924 ps = puffs_getspecific(pu);
1925 path = basename_r((char *)PCNPATH(pcn_src));
1926 path_len = strlen(path) + 1;
1927 linkname_len = strlen(link_target) + 1;
1928 len = path_len + linkname_len;
1929
1930 pm = ps->ps_new_msg(pu, opc, FUSE_SYMLINK, len, pcn_src->pcn_cred);
1931 np = _GET_INPAYLOAD(ps, pm, char *);
1932 (void)strlcpy(np, path, path_len);
1933 np += path_len;
1934 (void)strlcpy(np, link_target, linkname_len);
1935
1936 return node_mk_common(pu, opc, pni, pcn_src, pm);
1937 }
1938
1939 int
1940 perfuse_node_readdir(pu, opc, dent, readoff,
1941 reslen, pcr, eofflag, cookies, ncookies)
1942 struct puffs_usermount *pu;
1943 puffs_cookie_t opc;
1944 struct dirent *dent;
1945 off_t *readoff;
1946 size_t *reslen;
1947 const struct puffs_cred *pcr;
1948 int *eofflag;
1949 off_t *cookies;
1950 size_t *ncookies;
1951 {
1952 perfuse_msg_t *pm;
1953 uint64_t fh;
1954 struct perfuse_state *ps;
1955 struct perfuse_node_data *pnd;
1956 struct fuse_read_in *fri;
1957 struct fuse_out_header *foh;
1958 struct fuse_dirent *fd;
1959 size_t foh_len;
1960 int error;
1961 int open_self;
1962 uint64_t fd_offset;
1963
1964 pm = NULL;
1965 error = 0;
1966 open_self = 0;
1967 ps = puffs_getspecific(pu);
1968
1969 /*
1970 * readdir state is kept at node level, and several readdir
1971 * requests can be issued at the same time on the same node.
1972 * We need to queue requests so that only one is in readdir
1973 * code at the same time.
1974 */
1975 pnd = PERFUSE_NODE_DATA(opc);
1976 while (pnd->pnd_flags & PND_INREADDIR)
1977 requeue_request(pu, opc, PCQ_READDIR);
1978 pnd->pnd_flags |= PND_INREADDIR;
1979
1980 #ifdef PERFUSE_DEBUG
1981 if (perfuse_diagflags & PDF_READDIR)
1982 DPRINTF("%s: READDIR opc = %p enter critical section\n",
1983 __func__, (void *)opc);
1984 #endif
1985 /*
1986 * Do we already have the data bufered?
1987 */
1988 if (pnd->pnd_dirent != NULL)
1989 goto out;
1990 pnd->pnd_dirent_len = 0;
1991
1992 /*
1993 * It seems NetBSD can call readdir without open first
1994 * libfuse will crash if it is done that way, hence open first.
1995 */
1996 if (!(pnd->pnd_flags & PND_OPEN)) {
1997 if ((error = perfuse_node_open(pu, opc, FREAD, pcr)) != 0)
1998 goto out;
1999 open_self = 1;
2000 }
2001
2002 fh = perfuse_get_fh(opc, FREAD);
2003
2004 #ifdef PERFUSE_DEBUG
2005 if (perfuse_diagflags & PDF_FH)
2006 DPRINTF("%s: opc = %p, ino = %"PRId64", rfh = 0x%"PRIx64"\n",
2007 __func__, (void *)opc,
2008 PERFUSE_NODE_DATA(opc)->pnd_ino, fh);
2009 #endif
2010
2011 pnd->pnd_all_fd = NULL;
2012 pnd->pnd_all_fd_len = 0;
2013 fd_offset = 0;
2014
2015 do {
2016 size_t fd_len;
2017 char *afdp;
2018
2019 pm = ps->ps_new_msg(pu, opc, FUSE_READDIR, sizeof(*fri), pcr);
2020
2021 /*
2022 * read_flags, lock_owner and flags are unused in libfuse
2023 *
2024 * XXX if fri->size is too big (bigger than PAGE_SIZE?), * we get strange bugs. ktrace shows 16 bytes or garbage
2025 * at the end of sent frames, but perfused does not receive
2026 * that data. The data length is hoverver the same, which
2027 * cause perfused to use the last 16 bytes of the frame
2028 * as the frame header of the next frame.
2029 *
2030 * This may be a kernel bug.
2031 */
2032 fri = GET_INPAYLOAD(ps, pm, fuse_read_in);
2033 fri->fh = fh;
2034 fri->offset = fd_offset;
2035 fri->size = PAGE_SIZE - sizeof(struct fuse_out_header);
2036 fri->read_flags = 0;
2037 fri->lock_owner = 0;
2038 fri->flags = 0;
2039
2040 if ((error = XCHG_MSG(ps, pu, pm, UNSPEC_REPLY_LEN)) != 0)
2041 goto out;
2042
2043 /*
2044 * There are many puffs_framebufs calls later,
2045 * therefore foh will not be valid for a long time.
2046 * Just get the length and forget it.
2047 */
2048 foh = GET_OUTHDR(ps, pm);
2049 foh_len = foh->len;
2050
2051 /*
2052 * It seems that the only way to discover the end
2053 * of the buffer is to get an empty read
2054 */
2055 if (foh_len == sizeof(*foh))
2056 break;
2057
2058 /*
2059 * Corrupted message.
2060 */
2061 if (foh_len < sizeof(*foh) + sizeof(*fd)) {
2062 DWARNX("readdir reply too short");
2063 error = EIO;
2064 goto out;
2065 }
2066
2067
2068 fd = GET_OUTPAYLOAD(ps, pm, fuse_dirent);
2069 fd_len = foh_len - sizeof(*foh);
2070
2071 pnd->pnd_all_fd = realloc(pnd->pnd_all_fd,
2072 pnd->pnd_all_fd_len + fd_len);
2073 if (pnd->pnd_all_fd == NULL)
2074 DERR(EX_OSERR, "malloc failed");
2075
2076 afdp = (char *)(void *)pnd->pnd_all_fd + pnd->pnd_all_fd_len;
2077 (void)memcpy(afdp, fd, fd_len);
2078
2079 pnd->pnd_all_fd_len += fd_len;
2080 fd_offset += fd_len;
2081
2082 ps->ps_destroy_msg(pm);
2083 pm = NULL;
2084 } while (1 /* CONSTCOND */);
2085
2086 if (fuse_to_dirent(pu, opc, pnd->pnd_all_fd, pnd->pnd_all_fd_len) == -1)
2087 error = EIO;
2088
2089 out:
2090 if (pnd->pnd_all_fd != NULL) {
2091 free(pnd->pnd_all_fd);
2092 pnd->pnd_all_fd = NULL;
2093 pnd->pnd_all_fd_len = 0;
2094 }
2095
2096 if (pm != NULL)
2097 ps->ps_destroy_msg(pm);
2098
2099 /*
2100 * If we opened the directory ourselves, close now
2101 * errors are ignored.
2102 */
2103 if (open_self)
2104 (void)perfuse_node_close(pu, opc, FWRITE, pcr);
2105
2106 if (error == 0)
2107 error = readdir_buffered(ps, opc, dent, readoff,
2108 reslen, pcr, eofflag, cookies, ncookies);
2109
2110 /*
2111 * Schedule queued readdir requests
2112 */
2113 pnd->pnd_flags &= ~PND_INREADDIR;
2114 (void)dequeue_requests(ps, opc, PCQ_READDIR, DEQUEUE_ALL);
2115
2116 #ifdef PERFUSE_DEBUG
2117 if (perfuse_diagflags & PDF_READDIR)
2118 DPRINTF("%s: READDIR opc = %p exit critical section\n",
2119 __func__, (void *)opc);
2120 #endif
2121
2122 return error;
2123 }
2124
2125 int
2126 perfuse_node_readlink(pu, opc, pcr, linkname, linklen)
2127 struct puffs_usermount *pu;
2128 puffs_cookie_t opc;
2129 const struct puffs_cred *pcr;
2130 char *linkname;
2131 size_t *linklen;
2132 {
2133 struct perfuse_state *ps;
2134 perfuse_msg_t *pm;
2135 int error;
2136 size_t len;
2137 struct fuse_out_header *foh;
2138
2139 /*
2140 * --X required on parent, R-- required on link
2141 */
2142 if (no_access((puffs_cookie_t)PERFUSE_NODE_DATA(opc)->pnd_parent,
2143 pcr, PUFFS_VEXEC) ||
2144 no_access(opc, pcr, PUFFS_VREAD))
2145 return EACCES;
2146
2147 ps = puffs_getspecific(pu);
2148
2149 pm = ps->ps_new_msg(pu, opc, FUSE_READLINK, 0, pcr);
2150
2151 if ((error = XCHG_MSG(ps, pu, pm, UNSPEC_REPLY_LEN)) != 0)
2152 goto out;
2153
2154 foh = GET_OUTHDR(ps, pm);
2155 len = foh->len - sizeof(*foh) + 1;
2156 if (len > *linklen)
2157 DERRX(EX_PROTOCOL, "path len = %zd too long", len);
2158
2159 *linklen = len;
2160 (void)strlcpy(linkname, _GET_OUTPAYLOAD(ps, pm, char *), len);
2161 out:
2162 ps->ps_destroy_msg(pm);
2163
2164 return error;
2165 }
2166
2167 int
2168 perfuse_node_reclaim(pu, opc)
2169 struct puffs_usermount *pu;
2170 puffs_cookie_t opc;
2171 {
2172 struct perfuse_state *ps;
2173 perfuse_msg_t *pm;
2174 struct perfuse_node_data *pnd;
2175 struct fuse_forget_in *ffi;
2176 struct puffs_node *pn;
2177 struct puffs_node *pn_root;
2178
2179 ps = puffs_getspecific(pu);
2180 pnd = PERFUSE_NODE_DATA(opc);
2181
2182 /*
2183 * Never forget the root.
2184 */
2185 if (pnd->pnd_ino == FUSE_ROOT_ID)
2186 return 0;
2187
2188 pnd->pnd_flags |= PND_RECLAIMED;
2189
2190 #ifdef PERFUSE_DEBUG
2191 if (perfuse_diagflags & PDF_RECLAIM)
2192 DPRINTF("%s (nodeid %"PRId64") reclaimed\n",
2193 (char *)PNPATH((struct puffs_node *)opc), pnd->pnd_ino);
2194 #endif
2195
2196 pn_root = puffs_getroot(pu);
2197 pn = (struct puffs_node *)opc;
2198 while (pn != pn_root) {
2199 struct puffs_node *parent_pn;
2200
2201 pnd = PERFUSE_NODE_DATA(pn);
2202
2203 #ifdef PERFUSE_DEBUG
2204 if (perfuse_diagflags & PDF_RECLAIM)
2205 DPRINTF("%s (nodeid %"PRId64") is %sreclaimed, "
2206 "has childcount %d %s%s%s, pending ops:%s%s\n",
2207 (char *)PNPATH(pn), pnd->pnd_ino,
2208 pnd->pnd_flags & PND_RECLAIMED ? "" : "not ",
2209 pnd->pnd_childcount,
2210 pnd->pnd_flags & PND_OPEN ? "open " : "not open",
2211 pnd->pnd_flags & PND_RFH ? "r" : "",
2212 pnd->pnd_flags & PND_WFH ? "w" : "",
2213 pnd->pnd_flags & PND_INREADDIR ? " readdir" : "",
2214 pnd->pnd_flags & PND_INWRITE ? " write" : "");
2215 #endif
2216
2217 if (!(pnd->pnd_flags & PND_RECLAIMED) ||
2218 (pnd->pnd_childcount != 0))
2219 return 0;
2220
2221 /*
2222 * Make sure all operation are finished
2223 * There can be an ongoing write, or queued operations
2224 */
2225 while (pnd->pnd_flags & PND_INWRITE) {
2226 requeue_request(pu, opc, PCQ_AFTERWRITE);
2227
2228 /*
2229 * reclaim may have been cancelled in the meantime
2230 * if the file as been look'ed up again.
2231 */
2232 if (!(pnd->pnd_flags & PND_RECLAIMED))
2233 return 0;
2234 }
2235
2236 #ifdef PERFUSE_DEBUG
2237 if ((pnd->pnd_flags & (PND_INREADDIR|PND_INWRITE)) ||
2238 !TAILQ_EMPTY(&pnd->pnd_pcq))
2239 DERRX(EX_SOFTWARE, "%s: opc = %p: ongoing operations",
2240 __func__, (void *)opc);
2241 #endif
2242
2243 /*
2244 * Close open files
2245 */
2246 if (pnd->pnd_flags & PND_WFH)
2247 (void)node_close_common(pu, opc, FWRITE);
2248
2249 if (pnd->pnd_flags & PND_RFH)
2250 (void)node_close_common(pu, opc, FREAD);
2251
2252 /*
2253 * And send the FORGET message
2254 */
2255 pm = ps->ps_new_msg(pu, (puffs_cookie_t)pn, FUSE_FORGET,
2256 sizeof(*ffi), NULL);
2257 ffi = GET_INPAYLOAD(ps, pm, fuse_forget_in);
2258 ffi->nlookup = pnd->pnd_nlookup;
2259
2260 /*
2261 * No reply is expected, pm is freed in XCHG_MSG
2262 */
2263 (void)XCHG_MSG_NOREPLY(ps, pu, pm, UNSPEC_REPLY_LEN);
2264
2265 parent_pn = pnd->pnd_parent;
2266
2267 perfuse_destroy_pn(pn);
2268 puffs_pn_put(pn);
2269
2270 pn = parent_pn;
2271 }
2272
2273 return 0;
2274 }
2275
2276 /* ARGSUSED0 */
2277 int
2278 perfuse_node_inactive(pu, opc)
2279 struct puffs_usermount *pu;
2280 puffs_cookie_t opc;
2281 {
2282 return 0;
2283 }
2284
2285
2286 /* ARGSUSED0 */
2287 int
2288 perfuse_node_print(pu, opc)
2289 struct puffs_usermount *pu;
2290 puffs_cookie_t opc;
2291 {
2292 DERRX(EX_SOFTWARE, "%s: UNIMPLEMENTED (FATAL)", __func__);
2293 return 0;
2294 }
2295
2296 /* ARGSUSED0 */
2297 int
2298 perfuse_node_pathconf(pu, opc, name, retval)
2299 struct puffs_usermount *pu;
2300 puffs_cookie_t opc;
2301 int name;
2302 int *retval;
2303 {
2304 DERRX(EX_SOFTWARE, "%s: UNIMPLEMENTED (FATAL)", __func__);
2305 return 0;
2306 }
2307
2308 /* id is unused */
2309 /* ARGSUSED2 */
2310 int
2311 perfuse_node_advlock(pu, opc, id, op, fl, flags)
2312 struct puffs_usermount *pu;
2313 puffs_cookie_t opc;
2314 void *id;
2315 int op;
2316 struct flock *fl;
2317 int flags;
2318 {
2319 struct perfuse_state *ps;
2320 int fop;
2321 perfuse_msg_t *pm;
2322 struct fuse_lk_in *fli;
2323 struct fuse_lk_out *flo;
2324 int error;
2325
2326 ps = puffs_getspecific(pu);
2327
2328 if (op == F_GETLK)
2329 fop = FUSE_GETLK;
2330 else
2331 fop = (flags & F_WAIT) ? FUSE_SETLKW : FUSE_SETLK;
2332
2333 pm = ps->ps_new_msg(pu, opc, fop, sizeof(*fli), NULL);
2334 fli = GET_INPAYLOAD(ps, pm, fuse_lk_in);
2335 fli->fh = perfuse_get_fh(opc, FWRITE);
2336 fli->owner = fl->l_pid;
2337 fli->lk.start = fl->l_start;
2338 fli->lk.end = fl->l_start + fl->l_len;
2339 fli->lk.type = fl->l_type;
2340 fli->lk.pid = fl->l_pid;
2341 fli->lk_flags = (flags & F_FLOCK) ? FUSE_LK_FLOCK : 0;
2342
2343 #ifdef PERFUSE_DEBUG
2344 if (perfuse_diagflags & PDF_FH)
2345 DPRINTF("%s: opc = %p, ino = %"PRId64", fh = 0x%"PRIx64"\n",
2346 __func__, (void *)opc,
2347 PERFUSE_NODE_DATA(opc)->pnd_ino, fli->fh);
2348 #endif
2349
2350 if ((error = XCHG_MSG(ps, pu, pm, sizeof(*flo))) != 0)
2351 goto out;
2352
2353 flo = GET_OUTPAYLOAD(ps, pm, fuse_lk_out);
2354 fl->l_start = flo->lk.start;
2355 fl->l_len = flo->lk.end - flo->lk.start;
2356 fl->l_pid = flo->lk.pid;
2357 fl->l_type = flo->lk.type;
2358 fl->l_whence = SEEK_SET; /* libfuse hardcodes it */
2359
2360 /*
2361 * Save or clear the lock
2362 */
2363 switch (op) {
2364 case F_SETLK:
2365 PERFUSE_NODE_DATA(opc)->pnd_lock_owner = flo->lk.pid;
2366 break;
2367 case F_UNLCK:
2368 PERFUSE_NODE_DATA(opc)->pnd_lock_owner = 0;
2369 break;
2370 default:
2371 break;
2372 }
2373
2374 out:
2375 ps->ps_destroy_msg(pm);
2376
2377 return error;
2378 }
2379
2380 int
2381 perfuse_node_read(pu, opc, buf, offset, resid, pcr, ioflag)
2382 struct puffs_usermount *pu;
2383 puffs_cookie_t opc;
2384 uint8_t *buf;
2385 off_t offset;
2386 size_t *resid;
2387 const struct puffs_cred *pcr;
2388 int ioflag;
2389 {
2390 struct perfuse_state *ps;
2391 struct perfuse_node_data *pnd;
2392 perfuse_msg_t *pm;
2393 struct fuse_read_in *fri;
2394 struct fuse_out_header *foh;
2395 size_t readen;
2396 size_t requested;
2397 int error;
2398
2399 ps = puffs_getspecific(pu);
2400 pnd = PERFUSE_NODE_DATA(opc);
2401 pm = NULL;
2402
2403 if (puffs_pn_getvap((struct puffs_node *)opc)->va_type == VDIR)
2404 return EBADF;
2405
2406 requested = *resid;
2407 if ((ps->ps_readahead + requested) > ps->ps_max_readahead) {
2408 if (perfuse_diagflags & PDF_REQUEUE)
2409 DPRINTF("readahead = %zd\n", ps->ps_readahead);
2410 requeue_request(pu, opc, PCQ_READ);
2411 }
2412 ps->ps_readahead += requested;
2413
2414 do {
2415 /*
2416 * flags may be set to FUSE_READ_LOCKOWNER
2417 * if lock_owner is provided.
2418 *
2419 * XXX See comment about fri->size in perfuse_node_readdir
2420 * We encounter the same bug here.
2421 */
2422 pm = ps->ps_new_msg(pu, opc, FUSE_READ, sizeof(*fri), pcr);
2423 fri = GET_INPAYLOAD(ps, pm, fuse_read_in);
2424 fri->fh = perfuse_get_fh(opc, FREAD);
2425 fri->offset = offset;
2426 fri->size = (uint32_t)MIN(*resid, PAGE_SIZE - sizeof(*foh));
2427 fri->read_flags = 0; /* XXX Unused by libfuse? */
2428 fri->lock_owner = pnd->pnd_lock_owner;
2429 fri->flags = 0;
2430 fri->flags |= (fri->lock_owner != 0) ? FUSE_READ_LOCKOWNER : 0;
2431
2432 #ifdef PERFUSE_DEBUG
2433 if (perfuse_diagflags & PDF_FH)
2434 DPRINTF("%s: opc = %p, ino = %"PRId64", fh = 0x%"PRIx64"\n",
2435 __func__, (void *)opc, pnd->pnd_ino, fri->fh);
2436 #endif
2437 error = XCHG_MSG(ps, pu, pm, UNSPEC_REPLY_LEN);
2438
2439 if (error != 0)
2440 goto out;
2441
2442 foh = GET_OUTHDR(ps, pm);
2443 readen = foh->len - sizeof(*foh);
2444
2445 (void)memcpy(buf, _GET_OUTPAYLOAD(ps, pm, char *), readen);
2446
2447 buf += readen;
2448 offset += readen;
2449 *resid -= readen;
2450
2451 ps->ps_destroy_msg(pm);
2452 pm = NULL;
2453 } while ((*resid != 0) && (readen != 0));
2454
2455 if (ioflag & (IO_SYNC|IO_DSYNC))
2456 ps->ps_syncreads++;
2457 else
2458 ps->ps_asyncreads++;
2459
2460 out:
2461 if (pm != NULL)
2462 ps->ps_destroy_msg(pm);
2463
2464 ps->ps_readahead -= requested;
2465
2466 (void)dequeue_requests(ps, opc, PCQ_READ, 1);
2467
2468 return error;
2469 }
2470
2471 int
2472 perfuse_node_write(pu, opc, buf, offset, resid, pcr, ioflag)
2473 struct puffs_usermount *pu;
2474 puffs_cookie_t opc;
2475 uint8_t *buf;
2476 off_t offset;
2477 size_t *resid;
2478 const struct puffs_cred *pcr;
2479 int ioflag;
2480 {
2481 struct perfuse_state *ps;
2482 struct perfuse_node_data *pnd;
2483 perfuse_msg_t *pm;
2484 struct fuse_write_in *fwi;
2485 struct fuse_write_out *fwo;
2486 size_t data_len;
2487 size_t payload_len;
2488 size_t written;
2489 size_t requested;
2490 int error;
2491
2492 ps = puffs_getspecific(pu);
2493 pnd = PERFUSE_NODE_DATA(opc);
2494 pm = NULL;
2495 written = 0;
2496
2497 if (puffs_pn_getvap((struct puffs_node *)opc)->va_type == VDIR)
2498 return EBADF;
2499
2500 while (pnd->pnd_flags & PND_INWRITE)
2501 requeue_request(pu, opc, PCQ_WRITE);
2502 pnd->pnd_flags |= PND_INWRITE;
2503
2504
2505 requested = *resid;
2506 if ((ps->ps_write + requested) > ps->ps_max_write) {
2507 if (perfuse_diagflags & PDF_REQUEUE)
2508 DPRINTF("write = %zd\n", ps->ps_write);
2509 requeue_request(pu, opc, PCQ_WRITE);
2510 }
2511 ps->ps_write += requested;
2512
2513 do {
2514 /*
2515 * It seems libfuse does not expects big chunks, so
2516 * send it page per page. The writepage feature is
2517 * probably there to minmize data movement.
2518 * XXX use ps->ps_maxwrite?
2519 */
2520 data_len = MIN(*resid, PAGE_SIZE);
2521 payload_len = data_len + sizeof(*fwi);
2522
2523 /*
2524 * flags may be set to FUSE_WRITE_CACHE (XXX usage?)
2525 * or FUSE_WRITE_LOCKOWNER, if lock_owner is provided.
2526 * write_flags is set to 1 for writepage.
2527 */
2528 pm = ps->ps_new_msg(pu, opc, FUSE_WRITE, payload_len, pcr);
2529 fwi = GET_INPAYLOAD(ps, pm, fuse_write_in);
2530 fwi->fh = perfuse_get_fh(opc, FWRITE);
2531 fwi->offset = offset;
2532 fwi->size = (uint32_t)data_len;
2533 fwi->write_flags = (fwi->size % PAGE_SIZE) ? 0 : 1;
2534 fwi->lock_owner = pnd->pnd_lock_owner;
2535 fwi->flags = 0;
2536 fwi->flags |= (fwi->lock_owner != 0) ? FUSE_WRITE_LOCKOWNER : 0;
2537 fwi->flags |= (ioflag & IO_DIRECT) ? 0 : FUSE_WRITE_CACHE;
2538 (void)memcpy((fwi + 1), buf + written, data_len);
2539
2540 #ifdef PERFUSE_DEBUG
2541 if (perfuse_diagflags & PDF_FH)
2542 DPRINTF("%s: opc = %p, ino = %"PRId64", fh = 0x%"PRIx64"\n",
2543 __func__, (void *)opc, pnd->pnd_ino, fwi->fh);
2544 #endif
2545 if ((error = XCHG_MSG(ps, pu, pm, sizeof(*fwo))) != 0)
2546 goto out;
2547
2548 fwo = GET_OUTPAYLOAD(ps, pm, fuse_write_out);
2549 written = fwo->size;
2550 *resid -= written;
2551 offset += written;
2552 buf += written;
2553
2554 ps->ps_destroy_msg(pm);
2555 pm = NULL;
2556 } while (*resid != 0);
2557
2558 /*
2559 * puffs_ops(3) says
2560 * "everything must be written or an error will be generated"
2561 */
2562 if (*resid != 0)
2563 error = EFBIG;
2564
2565 if (ioflag & (IO_SYNC|IO_DSYNC))
2566 ps->ps_syncwrites++;
2567 else
2568 ps->ps_asyncwrites++;
2569
2570 /*
2571 * Remember to sync the file
2572 */
2573 pnd->pnd_flags |= PND_DIRTY;
2574
2575 #ifdef PERFUSE_DEBUG
2576 if (perfuse_diagflags & PDF_SYNC)
2577 DPRINTF("%s: DIRTY opc = %p, file = \"%s\"\n",
2578 __func__, (void*)opc,
2579 (char *)PNPATH((struct puffs_node *)opc));
2580 #endif
2581 out:
2582 if (pm != NULL)
2583 ps->ps_destroy_msg(pm);
2584
2585 ps->ps_write -= requested;
2586
2587
2588 /*
2589 * If there are no more queued write, we can resume
2590 * an operation awaiting write completion.
2591 */
2592 pnd->pnd_flags &= ~PND_INWRITE;
2593 if (dequeue_requests(ps, opc, PCQ_WRITE, 1) == 0)
2594 (void)dequeue_requests(ps, opc, PCQ_AFTERWRITE, DEQUEUE_ALL);
2595
2596 return error;
2597 }
2598
2599 /* ARGSUSED0 */
2600 void
2601 perfuse_cache_write(pu, opc, size, runs)
2602 struct puffs_usermount *pu;
2603 puffs_cookie_t opc;
2604 size_t size;
2605 struct puffs_cacherun *runs;
2606 {
2607 return;
2608 }
2609
2610