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