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