ops.c revision 1.84 1 /* $NetBSD: ops.c,v 1.84 2015/06/03 14:07:05 manu Exp $ */
2
3 /*-
4 * Copyright (c) 2010-2011 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/socket.h>
38 #include <sys/socket.h>
39 #include <sys/extattr.h>
40 #include <sys/time.h>
41 #include <machine/vmparam.h>
42
43 #include "perfuse_priv.h"
44 #include "fuse.h"
45
46 extern int perfuse_diagflags;
47
48 #if 0
49 static void print_node(const char *, puffs_cookie_t);
50 #endif
51 #ifdef PUFFS_KFLAG_CACHE_FS_TTL
52 static void perfuse_newinfo_setttl(struct puffs_newinfo *,
53 struct puffs_node *, struct fuse_entry_out *, struct fuse_attr_out *);
54 #endif /* PUFFS_KFLAG_CACHE_FS_TTL */
55 static int xchg_msg(struct puffs_usermount *, puffs_cookie_t,
56 perfuse_msg_t *, size_t, enum perfuse_xchg_pb_reply);
57 static int mode_access(puffs_cookie_t, const struct puffs_cred *, mode_t);
58 static int sticky_access(puffs_cookie_t, struct puffs_node *,
59 const struct puffs_cred *);
60 static void fuse_attr_to_vap(struct perfuse_state *,
61 struct vattr *, struct fuse_attr *);
62 static int node_lookup_common(struct puffs_usermount *, puffs_cookie_t,
63 struct puffs_newinfo *, const char *, const struct puffs_cred *,
64 struct puffs_node **);
65 static int node_mk_common(struct puffs_usermount *, puffs_cookie_t,
66 struct puffs_newinfo *, const struct puffs_cn *pcn, perfuse_msg_t *);
67 static uint64_t readdir_last_cookie(struct fuse_dirent *, size_t);
68 static ssize_t fuse_to_dirent(struct puffs_usermount *, puffs_cookie_t,
69 struct fuse_dirent *, size_t);
70 static void readdir_buffered(puffs_cookie_t, struct dirent *, off_t *,
71 size_t *);
72 static void node_ref(puffs_cookie_t);
73 static void node_rele(puffs_cookie_t);
74 static void requeue_request(struct puffs_usermount *,
75 puffs_cookie_t opc, enum perfuse_qtype);
76 static int dequeue_requests(puffs_cookie_t opc, enum perfuse_qtype, int);
77 #define DEQUEUE_ALL 0
78
79 /*
80 * From <sys/vnode>, inside #ifdef _KERNEL section
81 */
82 #define IO_SYNC (0x40|IO_DSYNC)
83 #define IO_DSYNC 0x00200
84 #define IO_DIRECT 0x02000
85
86 /*
87 * From <fcntl>, inside #ifdef _KERNEL section
88 */
89 #define F_WAIT 0x010
90 #define F_FLOCK 0x020
91 #define OFLAGS(fflags) ((fflags) - 1)
92
93 /*
94 * Borrowed from src/sys/kern/vfs_subr.c and src/sys/sys/vnode.h
95 */
96 const enum vtype iftovt_tab[16] = {
97 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
98 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
99 };
100 const int vttoif_tab[9] = {
101 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
102 S_IFSOCK, S_IFIFO, S_IFMT,
103 };
104
105 #define IFTOVT(mode) (iftovt_tab[((mode) & S_IFMT) >> 12])
106 #define VTTOIF(indx) (vttoif_tab[(int)(indx)])
107
108 #if 0
109 static void
110 print_node(const char *func, puffs_cookie_t opc)
111 {
112 struct puffs_node *pn;
113 struct perfuse_node_data *pnd;
114 struct vattr *vap;
115
116 pn = (struct puffs_node *)opc;
117 pnd = PERFUSE_NODE_DATA(opc);
118 vap = &pn->pn_va;
119
120 printf("%s: \"%s\", opc = %p, nodeid = 0x%"PRIx64" ino = %"PRIu64"\n",
121 func, pnd->pnd_name, opc, pnd->pnd_nodeid, vap->va_fileid);
122
123 return;
124 }
125 #endif /* PERFUSE_DEBUG */
126
127 int
128 perfuse_node_close_common(struct puffs_usermount *pu, puffs_cookie_t opc,
129 int mode)
130 {
131 struct perfuse_state *ps;
132 perfuse_msg_t *pm;
133 int op;
134 uint64_t fh;
135 struct fuse_release_in *fri;
136 struct perfuse_node_data *pnd;
137 struct puffs_node *pn;
138 int error;
139
140 ps = puffs_getspecific(pu);
141 pn = (struct puffs_node *)opc;
142 pnd = PERFUSE_NODE_DATA(pn);
143
144 if (puffs_pn_getvap(pn)->va_type == VDIR) {
145 op = FUSE_RELEASEDIR;
146 mode = FREAD;
147 } else {
148 op = FUSE_RELEASE;
149 }
150
151 /*
152 * Destroy the filehandle before sending the
153 * request to the FUSE filesystem, otherwise
154 * we may get a second close() while we wait
155 * for the reply, and we would end up closing
156 * the same fh twice instead of closng both.
157 */
158 fh = perfuse_get_fh(opc, mode);
159 perfuse_destroy_fh(pn, fh);
160
161 /*
162 * release_flags may be set to FUSE_RELEASE_FLUSH
163 * to flush locks. lock_owner must be set in that case
164 *
165 * ps_new_msg() is called with NULL creds, which will
166 * be interpreted as FUSE superuser. We come here from the
167 * inactive method, which provides no creds, but obviously
168 * runs with kernel privilege.
169 */
170 pm = ps->ps_new_msg(pu, opc, op, sizeof(*fri), NULL);
171 fri = GET_INPAYLOAD(ps, pm, fuse_release_in);
172 fri->fh = fh;
173 fri->flags = 0;
174 fri->release_flags = 0;
175 fri->lock_owner = pnd->pnd_lock_owner;
176 fri->flags = (fri->lock_owner != 0) ? FUSE_RELEASE_FLUSH : 0;
177
178 #ifdef PERFUSE_DEBUG
179 if (perfuse_diagflags & PDF_FH)
180 DPRINTF("%s: opc = %p, nodeid = 0x%"PRIx64", fh = 0x%"PRIx64"\n",
181 __func__, (void *)opc, pnd->pnd_nodeid, fri->fh);
182 #endif
183
184 if ((error = xchg_msg(pu, opc, pm,
185 NO_PAYLOAD_REPLY_LEN, wait_reply)) != 0)
186 DERRX(EX_SOFTWARE, "%s: freed fh = 0x%"PRIx64" but filesystem "
187 "returned error = %d", __func__, fh, error);
188
189 ps->ps_destroy_msg(pm);
190
191 return 0;
192 }
193
194 static int
195 xchg_msg(struct puffs_usermount *pu, puffs_cookie_t opc, perfuse_msg_t *pm,
196 size_t len, enum perfuse_xchg_pb_reply wait)
197 {
198 struct perfuse_state *ps;
199 struct perfuse_node_data *pnd;
200 struct perfuse_trace *pt = NULL;
201 int error;
202
203 ps = puffs_getspecific(pu);
204 pnd = NULL;
205 if ((struct puffs_node *)opc != NULL)
206 pnd = PERFUSE_NODE_DATA(opc);
207
208 #ifdef PERFUSE_DEBUG
209 if ((perfuse_diagflags & PDF_FILENAME) && (opc != 0))
210 DPRINTF("file = \"%s\", ino = %"PRIu64" flags = 0x%x\n",
211 perfuse_node_path(ps, opc),
212 ((struct puffs_node *)opc)->pn_va.va_fileid,
213 PERFUSE_NODE_DATA(opc)->pnd_flags);
214 #endif
215 ps->ps_xchgcount++;
216 if (pnd)
217 pnd->pnd_inxchg++;
218
219 /*
220 * Record FUSE call start if requested
221 */
222 if (perfuse_diagflags & PDF_TRACE)
223 pt = perfuse_trace_begin(ps, opc, pm);
224
225 /*
226 * Do actual FUSE exchange
227 */
228 if ((error = ps->ps_xchg_msg(pu, pm, len, wait)) != 0)
229 ps->ps_destroy_msg(pm);
230
231 /*
232 * Record FUSE call end if requested
233 */
234 if (pt != NULL)
235 perfuse_trace_end(ps, pt, error);
236
237 ps->ps_xchgcount--;
238 if (pnd) {
239 pnd->pnd_inxchg--;
240 (void)dequeue_requests(opc, PCQ_AFTERXCHG, DEQUEUE_ALL);
241 }
242
243 return error;
244 }
245
246 static int
247 mode_access(puffs_cookie_t opc, const struct puffs_cred *pcr, mode_t mode)
248 {
249 struct puffs_node *pn;
250 struct vattr *va;
251
252 /*
253 * pcr is NULL for self open through fsync or readdir.
254 * In both case, access control is useless, as it was
255 * done before, at open time.
256 */
257 if (pcr == NULL)
258 return 0;
259
260 pn = (struct puffs_node *)opc;
261 va = puffs_pn_getvap(pn);
262 return puffs_access(va->va_type, va->va_mode,
263 va->va_uid, va->va_gid,
264 mode, pcr);
265 }
266
267 static int
268 sticky_access(puffs_cookie_t opc, struct puffs_node *targ,
269 const struct puffs_cred *pcr)
270 {
271 uid_t uid;
272 int sticky, owner, parent_owner;
273
274 /*
275 * This covers the case where the kernel requests a DELETE
276 * or RENAME on its own, and where puffs_cred_getuid would
277 * return -1. While such a situation should not happen,
278 * we allow it here.
279 *
280 * This also allows root to tamper with other users' files
281 * that have the sticky bit.
282 */
283 if (puffs_cred_isjuggernaut(pcr))
284 return 0;
285
286 if (puffs_cred_getuid(pcr, &uid) != 0)
287 DERRX(EX_SOFTWARE, "puffs_cred_getuid fails in %s", __func__);
288
289 sticky = puffs_pn_getvap(opc)->va_mode & S_ISTXT;
290 owner = puffs_pn_getvap(targ)->va_uid == uid;
291 parent_owner = puffs_pn_getvap(opc)->va_uid == uid;
292
293 if (sticky && !owner && !parent_owner)
294 return EPERM;
295
296 return 0;
297 }
298
299
300 static void
301 fuse_attr_to_vap(struct perfuse_state *ps, struct vattr *vap,
302 struct fuse_attr *fa)
303 {
304 vap->va_type = IFTOVT(fa->mode);
305 vap->va_mode = fa->mode & ALLPERMS;
306 vap->va_nlink = fa->nlink;
307 vap->va_uid = fa->uid;
308 vap->va_gid = fa->gid;
309 vap->va_fsid = (long)ps->ps_fsid;
310 vap->va_fileid = fa->ino;
311 vap->va_size = fa->size;
312 vap->va_blocksize = fa->blksize;
313 vap->va_atime.tv_sec = (time_t)fa->atime;
314 vap->va_atime.tv_nsec = (long) fa->atimensec;
315 vap->va_mtime.tv_sec = (time_t)fa->mtime;
316 vap->va_mtime.tv_nsec = (long)fa->mtimensec;
317 vap->va_ctime.tv_sec = (time_t)fa->ctime;
318 vap->va_ctime.tv_nsec = (long)fa->ctimensec;
319 vap->va_birthtime.tv_sec = 0;
320 vap->va_birthtime.tv_nsec = 0;
321 vap->va_gen = 0;
322 vap->va_flags = 0;
323 vap->va_rdev = fa->rdev;
324 vap->va_bytes = fa->blocks * S_BLKSIZE;
325 vap->va_filerev = (u_quad_t)PUFFS_VNOVAL;
326 vap->va_vaflags = 0;
327
328 if (vap->va_blocksize == 0)
329 vap->va_blocksize = DEV_BSIZE;
330
331 if (vap->va_size == (size_t)PUFFS_VNOVAL) /* XXX */
332 vap->va_size = 0;
333
334 return;
335 }
336
337 #ifdef PUFFS_KFLAG_CACHE_FS_TTL
338 static void
339 perfuse_newinfo_setttl(struct puffs_newinfo *pni,
340 struct puffs_node *pn, struct fuse_entry_out *feo,
341 struct fuse_attr_out *fao)
342 {
343 #ifdef PERFUSE_DEBUG
344 if ((feo == NULL) && (fao == NULL))
345 DERRX(EX_SOFTWARE, "%s: feo and fao NULL", __func__);
346
347 if ((feo != NULL) && (fao != NULL))
348 DERRX(EX_SOFTWARE, "%s: feo and fao != NULL", __func__);
349 #endif /* PERFUSE_DEBUG */
350
351 if (fao != NULL) {
352 struct timespec va_ttl;
353
354 va_ttl.tv_sec = fao->attr_valid;
355 va_ttl.tv_nsec = fao->attr_valid_nsec;
356
357 puffs_newinfo_setvattl(pni, &va_ttl);
358 }
359
360 if (feo != NULL) {
361 struct timespec va_ttl;
362 struct timespec cn_ttl;
363 struct timespec now;
364 struct perfuse_node_data *pnd = PERFUSE_NODE_DATA(pn);
365
366 va_ttl.tv_sec = feo->attr_valid;
367 va_ttl.tv_nsec = feo->attr_valid_nsec;
368 cn_ttl.tv_sec = feo->entry_valid;
369 cn_ttl.tv_nsec = feo->entry_valid_nsec;
370
371 puffs_newinfo_setvattl(pni, &va_ttl);
372 puffs_newinfo_setcnttl(pni, &cn_ttl);
373
374 if (clock_gettime(CLOCK_REALTIME, &now) != 0)
375 DERR(EX_OSERR, "clock_gettime failed");
376
377 timespecadd(&now, &cn_ttl, &pnd->pnd_cn_expire);
378 }
379
380 return;
381 }
382 #endif /* PUFFS_KFLAG_CACHE_FS_TTL */
383
384 static int
385 node_lookup_common(struct puffs_usermount *pu, puffs_cookie_t opc,
386 struct puffs_newinfo *pni, const char *path,
387 const struct puffs_cred *pcr, struct puffs_node **pnp)
388 {
389 struct perfuse_state *ps;
390 struct perfuse_node_data *oldpnd;
391 perfuse_msg_t *pm;
392 struct fuse_entry_out *feo;
393 struct puffs_node *pn;
394 size_t len;
395 int error;
396
397 /*
398 * Prevent further lookups if the parent was removed
399 */
400 if (PERFUSE_NODE_DATA(opc)->pnd_flags & PND_REMOVED)
401 return ESTALE;
402
403 if (pnp == NULL)
404 DERRX(EX_SOFTWARE, "pnp must be != NULL");
405
406 ps = puffs_getspecific(pu);
407
408 #ifdef PERFUSE_DEBUG
409 if (perfuse_diagflags & PDF_FILENAME)
410 DPRINTF("%s: opc = %p, file = \"%s\" looking up \"%s\"\n",
411 __func__, (void *)opc,
412 perfuse_node_path(ps, opc), path);
413
414 if (strcmp(path, ".") == 0)
415 DERRX(EX_SOFTWARE, "unexpected dot-lookup");
416
417 if (PERFUSE_NODE_DATA(opc)->pnd_flags & PND_RECLAIMED)
418 DERRX(EX_SOFTWARE,
419 "looking up reclaimed node opc = %p, name = \"%s\"",
420 opc, path);
421
422 if (PERFUSE_NODE_DATA(opc)->pnd_flags & PND_INVALID)
423 DERRX(EX_SOFTWARE,
424 "looking up freed node opc = %p, name = \"%s\"",
425 opc, path);
426 #endif /* PERFUSE_DEBUG */
427
428 len = strlen(path) + 1;
429 pm = ps->ps_new_msg(pu, opc, FUSE_LOOKUP, len, pcr);
430 (void)strlcpy(_GET_INPAYLOAD(ps, pm, char *), path, len);
431
432 if ((error = xchg_msg(pu, opc, pm, sizeof(*feo), wait_reply)) != 0)
433 return error;
434
435 feo = GET_OUTPAYLOAD(ps, pm, fuse_entry_out);
436
437 /*
438 * Starting with ABI 7.4, inode number 0 means ENOENT,
439 * with entry_valid / entry_valid_nsec giving negative
440 * cache timeout (which we do not implement yet).
441 */
442 if (feo->attr.ino == 0) {
443 ps->ps_destroy_msg(pm);
444 return ENOENT;
445 }
446
447 /*
448 * Check for a known node, not reclaimed, with another name.
449 * It may have been moved, or we can lookup ../
450 */
451 if (((oldpnd = perfuse_node_bynodeid(ps, feo->nodeid)) != NULL) &&
452 !(oldpnd->pnd_flags & PND_RECLAIMED)) {
453 /*
454 * Save the new node name if not ..
455 */
456 if (strncmp(path, "..", len) != 0)
457 (void)strlcpy(oldpnd->pnd_name,
458 path, MAXPATHLEN);
459 pn = oldpnd->pnd_pn;
460
461 } else {
462 pn = perfuse_new_pn(pu, path, opc);
463 PERFUSE_NODE_DATA(pn)->pnd_nodeid = feo->nodeid;
464 perfuse_node_cache(ps, pn);
465 }
466
467 #ifdef PERFUSE_DEBUG
468 if (PERFUSE_NODE_DATA(pn)->pnd_flags & PND_RECLAIMED)
469 DERRX(EX_SOFTWARE,
470 "reclaimed in lookup opc = %p, name = \"%s\", ck = %p",
471 opc, path, pn);
472
473 if (PERFUSE_NODE_DATA(pn)->pnd_flags & PND_INVALID)
474 DERRX(EX_SOFTWARE,
475 "freed in lookup opc = %p, name = \"%s\", ck = %p",
476 opc, path, pn);
477 #endif /* PERFUSE_DEBUG */
478
479 fuse_attr_to_vap(ps, &pn->pn_va, &feo->attr);
480 pn->pn_va.va_gen = (u_long)(feo->generation);
481 PERFUSE_NODE_DATA(pn)->pnd_fuse_nlookup++;
482
483 *pnp = pn;
484
485 #ifdef PERFUSE_DEBUG
486 if (perfuse_diagflags & PDF_FILENAME)
487 DPRINTF("%s: opc = %p, looked up opc = %p, "
488 "nodeid = 0x%"PRIx64" file = \"%s\"\n", __func__,
489 (void *)opc, pn, feo->nodeid, path);
490 #endif
491
492 if (pni != NULL) {
493 #ifdef PUFFS_KFLAG_CACHE_FS_TTL
494 puffs_newinfo_setva(pni, &pn->pn_va);
495 perfuse_newinfo_setttl(pni, pn, feo, NULL);
496 #endif /* PUFFS_KFLAG_CACHE_FS_TTL */
497 puffs_newinfo_setcookie(pni, pn);
498 puffs_newinfo_setvtype(pni, pn->pn_va.va_type);
499 puffs_newinfo_setsize(pni, (voff_t)pn->pn_va.va_size);
500 puffs_newinfo_setrdev(pni, pn->pn_va.va_rdev);
501 }
502
503 if (PERFUSE_NODE_DATA(pn)->pnd_flags & PND_NODELEAK) {
504 PERFUSE_NODE_DATA(pn)->pnd_flags &= ~PND_NODELEAK;
505 ps->ps_nodeleakcount--;
506 }
507
508 ps->ps_destroy_msg(pm);
509
510 return 0;
511 }
512
513
514 /*
515 * Common code for methods that create objects:
516 * perfuse_node_mkdir
517 * perfuse_node_mknod
518 * perfuse_node_symlink
519 */
520 static int
521 node_mk_common(struct puffs_usermount *pu, puffs_cookie_t opc,
522 struct puffs_newinfo *pni, const struct puffs_cn *pcn,
523 perfuse_msg_t *pm)
524 {
525 struct perfuse_state *ps;
526 struct puffs_node *pn;
527 struct fuse_entry_out *feo;
528 int error;
529
530 ps = puffs_getspecific(pu);
531
532 if ((error = xchg_msg(pu, opc, pm, sizeof(*feo), wait_reply)) != 0)
533 return error;
534
535 feo = GET_OUTPAYLOAD(ps, pm, fuse_entry_out);
536 if (feo->nodeid == PERFUSE_UNKNOWN_NODEID)
537 DERRX(EX_SOFTWARE, "%s: no nodeid", __func__);
538
539 pn = perfuse_new_pn(pu, pcn->pcn_name, opc);
540 PERFUSE_NODE_DATA(pn)->pnd_nodeid = feo->nodeid;
541 PERFUSE_NODE_DATA(pn)->pnd_puffs_nlookup++;
542 perfuse_node_cache(ps, pn);
543
544 fuse_attr_to_vap(ps, &pn->pn_va, &feo->attr);
545 pn->pn_va.va_gen = (u_long)(feo->generation);
546
547 puffs_newinfo_setcookie(pni, pn);
548 #ifdef PUFFS_KFLAG_CACHE_FS_TTL
549 puffs_newinfo_setva(pni, &pn->pn_va);
550 perfuse_newinfo_setttl(pni, pn, feo, NULL);
551 #endif /* PUFFS_KFLAG_CACHE_FS_TTL */
552
553
554 #ifdef PERFUSE_DEBUG
555 if (perfuse_diagflags & PDF_FILENAME)
556 DPRINTF("%s: opc = %p, file = \"%s\", flags = 0x%x "
557 "nodeid = 0x%"PRIx64"\n",
558 __func__, (void *)pn, pcn->pcn_name,
559 PERFUSE_NODE_DATA(pn)->pnd_flags, feo->nodeid);
560 #endif
561 ps->ps_destroy_msg(pm);
562
563 /* Parents is now dirty */
564 PERFUSE_NODE_DATA(opc)->pnd_flags |= PND_DIRTY;
565
566 return 0;
567 }
568
569 static uint64_t
570 readdir_last_cookie(struct fuse_dirent *fd, size_t fd_len)
571 {
572 size_t len;
573 size_t seen = 0;
574 char *ndp;
575
576 do {
577 len = FUSE_DIRENT_ALIGN(sizeof(*fd) + fd->namelen);
578 seen += len;
579
580 if (seen >= fd_len)
581 break;
582
583 ndp = (char *)(void *)fd + (size_t)len;
584 fd = (struct fuse_dirent *)(void *)ndp;
585 } while (1 /* CONSTCOND */);
586
587 return fd->off;
588 }
589
590 static ssize_t
591 fuse_to_dirent(struct puffs_usermount *pu, puffs_cookie_t opc,
592 struct fuse_dirent *fd, size_t fd_len)
593 {
594 struct dirent *dents;
595 size_t dents_len;
596 ssize_t written;
597 uint64_t fd_offset;
598 struct fuse_dirent *fd_base;
599 size_t len;
600
601 fd_base = fd;
602 fd_offset = 0;
603 written = 0;
604 dents = PERFUSE_NODE_DATA(opc)->pnd_dirent;
605 dents_len = (size_t)PERFUSE_NODE_DATA(opc)->pnd_dirent_len;
606
607 do {
608 char *ndp;
609 size_t reclen;
610 char name[MAXPATHLEN];
611
612 reclen = _DIRENT_RECLEN(dents, fd->namelen);
613
614 /*
615 * Check we do not overflow the output buffer
616 * struct fuse_dirent is bigger than struct dirent,
617 * so we should always use fd_len and never reallocate
618 * later.
619 * If we have to reallocate,try to double the buffer
620 * each time so that we do not have to do it too often.
621 */
622 if (written + reclen > dents_len) {
623 if (dents_len == 0)
624 dents_len = fd_len;
625 else
626 dents_len =
627 MAX(2 * dents_len, written + reclen);
628
629 dents = PERFUSE_NODE_DATA(opc)->pnd_dirent;
630 if ((dents = realloc(dents, dents_len)) == NULL)
631 DERR(EX_OSERR, "%s: malloc failed", __func__);
632
633 PERFUSE_NODE_DATA(opc)->pnd_dirent = dents;
634 PERFUSE_NODE_DATA(opc)->pnd_dirent_len = dents_len;
635
636 /*
637 * (void *) for delint
638 */
639 ndp = (char *)(void *)dents + written;
640 dents = (struct dirent *)(void *)ndp;
641 }
642
643 strncpy(name, fd->name, fd->namelen);
644 name[fd->namelen] = '\0';
645
646 /*
647 * Filesystem was mounted without -o use_ino
648 * Perform a lookup to find it.
649 */
650 if (fd->ino == PERFUSE_UNKNOWN_INO) {
651 struct puffs_node *pn;
652 struct perfuse_node_data *pnd = PERFUSE_NODE_DATA(opc);
653
654 if (strcmp(name, "..") == 0) {
655 /*
656 * Avoid breaking out of fs
657 * by lookup to .. on root
658 */
659 if (pnd->pnd_nodeid == FUSE_ROOT_ID)
660 fd->ino = FUSE_ROOT_ID;
661 else
662 fd->ino = pnd->pnd_parent_nodeid;
663 } else if (strcmp(name, ".") == 0 ) {
664 fd->ino = pnd->pnd_nodeid;
665 } else {
666 int error;
667
668 error = node_lookup_common(pu, opc, NULL,
669 name, NULL, &pn);
670 if (error != 0) {
671 DWARNX("node_lookup_common %s "
672 "failed: %d", name, error);
673 } else {
674 fd->ino = pn->pn_va.va_fileid;
675 (void)perfuse_node_reclaim(pu, pn);
676 }
677 }
678 }
679
680 dents->d_fileno = fd->ino;
681 dents->d_reclen = (unsigned short)reclen;
682 dents->d_namlen = fd->namelen;
683 dents->d_type = fd->type;
684 strlcpy(dents->d_name, name, fd->namelen + 1);
685
686 #ifdef PERFUSE_DEBUG
687 if (perfuse_diagflags & PDF_READDIR)
688 DPRINTF("%s: translated \"%s\" ino = %"PRIu64"\n",
689 __func__, dents->d_name, dents->d_fileno);
690 #endif
691
692 dents = _DIRENT_NEXT(dents);
693 written += reclen;
694
695 /*
696 * Move to the next record.
697 * fd->off is not the offset, it is an opaque cookie
698 * given by the filesystem to keep state across multiple
699 * readdir() operation.
700 * Use record alignement instead.
701 */
702 len = FUSE_DIRENT_ALIGN(sizeof(*fd) + fd->namelen);
703 #ifdef PERFUSE_DEBUG
704 if (perfuse_diagflags & PDF_READDIR)
705 DPRINTF("%s: record at %"PRId64"/0x%"PRIx64" "
706 "length = %zd/0x%zx. "
707 "next record at %"PRId64"/0x%"PRIx64" "
708 "max %zd/0x%zx\n",
709 __func__, fd_offset, fd_offset, len, len,
710 fd_offset + len, fd_offset + len,
711 fd_len, fd_len);
712 #endif
713 fd_offset += len;
714
715 /*
716 * Check if next record is still within the packet
717 * If it is not, we reached the end of the buffer.
718 */
719 if (fd_offset >= fd_len)
720 break;
721
722 /*
723 * (void *) for delint
724 */
725 ndp = (char *)(void *)fd_base + (size_t)fd_offset;
726 fd = (struct fuse_dirent *)(void *)ndp;
727
728 } while (1 /* CONSTCOND */);
729
730 /*
731 * Adjust the dirent output length
732 */
733 if (written != -1)
734 PERFUSE_NODE_DATA(opc)->pnd_dirent_len = written;
735
736 return written;
737 }
738
739 static void
740 readdir_buffered(puffs_cookie_t opc, struct dirent *dent, off_t *readoff,
741 size_t *reslen)
742 {
743 struct dirent *fromdent;
744 struct perfuse_node_data *pnd;
745 char *ndp;
746
747 pnd = PERFUSE_NODE_DATA(opc);
748
749 while (*readoff < pnd->pnd_dirent_len) {
750 /*
751 * (void *) for delint
752 */
753 ndp = (char *)(void *)pnd->pnd_dirent + (size_t)*readoff;
754 fromdent = (struct dirent *)(void *)ndp;
755
756 if (*reslen < _DIRENT_SIZE(fromdent))
757 break;
758
759 memcpy(dent, fromdent, _DIRENT_SIZE(fromdent));
760 *readoff += _DIRENT_SIZE(fromdent);
761 *reslen -= _DIRENT_SIZE(fromdent);
762
763 dent = _DIRENT_NEXT(dent);
764 }
765
766 #ifdef PERFUSE_DEBUG
767 if (perfuse_diagflags & PDF_READDIR)
768 DPRINTF("%s: readoff = %"PRId64", "
769 "pnd->pnd_dirent_len = %"PRId64"\n",
770 __func__, *readoff, pnd->pnd_dirent_len);
771 #endif
772 if (*readoff >= pnd->pnd_dirent_len) {
773 free(pnd->pnd_dirent);
774 pnd->pnd_dirent = NULL;
775 pnd->pnd_dirent_len = 0;
776 }
777
778 return;
779 }
780
781
782 static void
783 node_ref(puffs_cookie_t opc)
784 {
785 struct perfuse_node_data *pnd = PERFUSE_NODE_DATA(opc);
786
787 #ifdef PERFUSE_DEBUG
788 if (pnd->pnd_flags & PND_INVALID)
789 DERRX(EX_SOFTWARE, "Use of freed node opc = %p", opc);
790 #endif /* PERFUSE_DEBUG */
791
792 pnd->pnd_ref++;
793 return;
794 }
795
796 static void
797 node_rele(puffs_cookie_t opc)
798 {
799 struct perfuse_node_data *pnd = PERFUSE_NODE_DATA(opc);
800
801 #ifdef PERFUSE_DEBUG
802 if (pnd->pnd_flags & PND_INVALID)
803 DERRX(EX_SOFTWARE, "Use of freed node opc = %p", opc);
804 #endif /* PERFUSE_DEBUG */
805
806 pnd->pnd_ref--;
807
808 if (pnd->pnd_ref == 0)
809 (void)dequeue_requests(opc, PCQ_REF, DEQUEUE_ALL);
810
811 return;
812 }
813
814 static void
815 requeue_request(struct puffs_usermount *pu, puffs_cookie_t opc,
816 enum perfuse_qtype type)
817 {
818 struct perfuse_cc_queue pcq;
819 struct perfuse_node_data *pnd;
820
821 pnd = PERFUSE_NODE_DATA(opc);
822 pcq.pcq_type = type;
823 pcq.pcq_cc = puffs_cc_getcc(pu);
824 TAILQ_INSERT_TAIL(&pnd->pnd_pcq, &pcq, pcq_next);
825
826 #ifdef PERFUSE_DEBUG
827 if (perfuse_diagflags & PDF_REQUEUE)
828 DPRINTF("%s: REQUEUE opc = %p, pcc = %p (%s)\n",
829 __func__, (void *)opc, pcq.pcq_cc,
830 perfuse_qtypestr[type]);
831 #endif
832
833 puffs_cc_yield(pcq.pcq_cc);
834 TAILQ_REMOVE(&pnd->pnd_pcq, &pcq, pcq_next);
835
836 #ifdef PERFUSE_DEBUG
837 if (perfuse_diagflags & PDF_REQUEUE)
838 DPRINTF("%s: RESUME opc = %p, pcc = %p (%s)\n",
839 __func__, (void *)opc, pcq.pcq_cc,
840 perfuse_qtypestr[type]);
841 #endif
842
843 return;
844 }
845
846 static int
847 dequeue_requests(puffs_cookie_t opc, enum perfuse_qtype type, int max)
848 {
849 struct perfuse_cc_queue *pcq;
850 struct perfuse_node_data *pnd;
851 int dequeued;
852
853 pnd = PERFUSE_NODE_DATA(opc);
854 dequeued = 0;
855 TAILQ_FOREACH(pcq, &pnd->pnd_pcq, pcq_next) {
856 if (pcq->pcq_type != type)
857 continue;
858
859 #ifdef PERFUSE_DEBUG
860 if (perfuse_diagflags & PDF_REQUEUE)
861 DPRINTF("%s: SCHEDULE opc = %p, pcc = %p (%s)\n",
862 __func__, (void *)opc, pcq->pcq_cc,
863 perfuse_qtypestr[type]);
864 #endif
865 puffs_cc_schedule(pcq->pcq_cc);
866
867 if (++dequeued == max)
868 break;
869 }
870
871 #ifdef PERFUSE_DEBUG
872 if (perfuse_diagflags & PDF_REQUEUE)
873 DPRINTF("%s: DONE opc = %p\n", __func__, (void *)opc);
874 #endif
875
876 return dequeued;
877 }
878
879 void
880 perfuse_fs_init(struct puffs_usermount *pu)
881 {
882 struct perfuse_state *ps;
883 perfuse_msg_t *pm;
884 struct fuse_init_in *fii;
885 struct fuse_init_out *fio;
886 int error;
887
888 ps = puffs_getspecific(pu);
889
890 if (puffs_mount(pu, ps->ps_target, ps->ps_mountflags, ps->ps_root) != 0)
891 DERR(EX_OSERR, "%s: puffs_mount failed", __func__);
892
893 /*
894 * Linux 2.6.34.1 sends theses flags:
895 * FUSE_ASYNC_READ | FUSE_POSIX_LOCKS | FUSE_ATOMIC_O_TRUNC
896 * FUSE_EXPORT_SUPPORT | FUSE_BIG_WRITES | FUSE_DONT_MASK
897 *
898 * Linux also sets max_readahead at 32 pages (128 kB)
899 *
900 * ps_new_msg() is called with NULL creds, which will
901 * be interpreted as FUSE superuser.
902 */
903 pm = ps->ps_new_msg(pu, 0, FUSE_INIT, sizeof(*fii), NULL);
904 fii = GET_INPAYLOAD(ps, pm, fuse_init_in);
905 fii->major = FUSE_KERNEL_VERSION;
906 fii->minor = FUSE_KERNEL_MINOR_VERSION;
907 fii->max_readahead = (unsigned int)(32 * sysconf(_SC_PAGESIZE));
908 fii->flags = (FUSE_ASYNC_READ|FUSE_POSIX_LOCKS|FUSE_ATOMIC_O_TRUNC);
909
910 if ((error = xchg_msg(pu, 0, pm, sizeof(*fio), wait_reply)) != 0)
911 DERRX(EX_SOFTWARE, "init message exchange failed (%d)", error);
912
913 fio = GET_OUTPAYLOAD(ps, pm, fuse_init_out);
914 ps->ps_max_readahead = fio->max_readahead;
915 ps->ps_max_write = fio->max_write;
916
917 ps->ps_destroy_msg(pm);
918
919 return;
920 }
921
922 int
923 perfuse_fs_unmount(struct puffs_usermount *pu, int flags)
924 {
925 perfuse_msg_t *pm;
926 struct perfuse_state *ps;
927 puffs_cookie_t opc;
928 int error;
929
930 ps = puffs_getspecific(pu);
931 opc = (puffs_cookie_t)puffs_getroot(pu);
932
933 /*
934 * ps_new_msg() is called with NULL creds, which will
935 * be interpreted as FUSE superuser.
936 */
937 pm = ps->ps_new_msg(pu, opc, FUSE_DESTROY, 0, NULL);
938
939 if ((error = xchg_msg(pu, opc, pm, UNSPEC_REPLY_LEN, wait_reply)) != 0){
940 DWARN("unmount %s", ps->ps_target);
941 if (!(flags & MNT_FORCE))
942 return error;
943 else
944 error = 0;
945 } else {
946 ps->ps_destroy_msg(pm);
947 }
948
949 ps->ps_umount(pu);
950
951 if (perfuse_diagflags & PDF_MISC)
952 DPRINTF("%s unmounted, exit\n", ps->ps_target);
953
954 return 0;
955 }
956
957 int
958 perfuse_fs_statvfs(struct puffs_usermount *pu, struct statvfs *svfsb)
959 {
960 struct perfuse_state *ps;
961 perfuse_msg_t *pm;
962 puffs_cookie_t opc;
963 struct fuse_statfs_out *fso;
964 int error;
965
966 ps = puffs_getspecific(pu);
967 opc = (puffs_cookie_t)puffs_getroot(pu);
968
969 /*
970 * ps_new_msg() is called with NULL creds, which will
971 * be interpreted as FUSE superuser.
972 */
973 pm = ps->ps_new_msg(pu, opc, FUSE_STATFS, 0, NULL);
974
975 if ((error = xchg_msg(pu, opc, pm, sizeof(*fso), wait_reply)) != 0)
976 return error;
977
978 fso = GET_OUTPAYLOAD(ps, pm, fuse_statfs_out);
979 svfsb->f_flag = ps->ps_mountflags;
980 svfsb->f_bsize = fso->st.bsize;
981 svfsb->f_frsize = fso->st.frsize;
982 svfsb->f_iosize = ((struct puffs_node *)opc)->pn_va.va_blocksize;
983 svfsb->f_blocks = fso->st.blocks;
984 svfsb->f_bfree = fso->st.bfree;
985 svfsb->f_bavail = fso->st.bavail;
986 svfsb->f_bresvd = fso->st.bfree - fso->st.bavail;
987 svfsb->f_files = fso->st.files;
988 svfsb->f_ffree = fso->st.ffree;
989 svfsb->f_favail = fso->st.ffree;/* files not reserved for root */
990 svfsb->f_fresvd = 0; /* files reserved for root */
991
992 svfsb->f_syncreads = ps->ps_syncreads;
993 svfsb->f_syncwrites = ps->ps_syncwrites;
994
995 svfsb->f_asyncreads = ps->ps_asyncreads;
996 svfsb->f_asyncwrites = ps->ps_asyncwrites;
997
998 (void)memcpy(&svfsb->f_fsidx, &ps->ps_fsid, sizeof(ps->ps_fsid));
999 svfsb->f_fsid = (unsigned long)ps->ps_fsid;
1000 svfsb->f_namemax = MAXPATHLEN; /* XXX */
1001 svfsb->f_owner = ps->ps_owner_uid;
1002
1003 (void)strlcpy(svfsb->f_mntonname, ps->ps_target, _VFS_NAMELEN);
1004
1005 if (ps->ps_filesystemtype != NULL)
1006 (void)strlcpy(svfsb->f_fstypename,
1007 ps->ps_filesystemtype, _VFS_NAMELEN);
1008 else
1009 (void)strlcpy(svfsb->f_fstypename, "fuse", _VFS_NAMELEN);
1010
1011 if (ps->ps_source != NULL)
1012 strlcpy(svfsb->f_mntfromname, ps->ps_source, _VFS_NAMELEN);
1013 else
1014 strlcpy(svfsb->f_mntfromname, _PATH_FUSE, _VFS_NAMELEN);
1015
1016 ps->ps_destroy_msg(pm);
1017
1018 return 0;
1019 }
1020
1021 int
1022 perfuse_fs_sync(struct puffs_usermount *pu, int waitfor,
1023 const struct puffs_cred *pcr)
1024 {
1025 /*
1026 * FUSE does not seem to have a FS sync callback.
1027 * Maybe do not even register this callback
1028 */
1029 return puffs_fsnop_sync(pu, waitfor, pcr);
1030 }
1031
1032 /* ARGSUSED0 */
1033 int
1034 perfuse_fs_fhtonode(struct puffs_usermount *pu, void *fid, size_t fidsize,
1035 struct puffs_newinfo *pni)
1036 {
1037 DERRX(EX_SOFTWARE, "%s: UNIMPLEMENTED (FATAL)", __func__);
1038 return 0;
1039 }
1040
1041 /* ARGSUSED0 */
1042 int
1043 perfuse_fs_nodetofh(struct puffs_usermount *pu, puffs_cookie_t cookie,
1044 void *fid, size_t *fidsize)
1045 {
1046 DERRX(EX_SOFTWARE, "%s: UNIMPLEMENTED (FATAL)", __func__);
1047 return 0;
1048 }
1049
1050 #if 0
1051 /* ARGSUSED0 */
1052 void
1053 perfuse_fs_extattrctl(struct puffs_usermount *pu, int cmd,
1054 puffs_cookie_t *cookie, int flags, int namespace, const char *attrname)
1055 {
1056 DERRX(EX_SOFTWARE, "%s: UNIMPLEMENTED (FATAL)", __func__);
1057 return 0;
1058 }
1059 #endif /* 0 */
1060
1061 /* ARGSUSED0 */
1062 void
1063 perfuse_fs_suspend(struct puffs_usermount *pu, int status)
1064 {
1065 return;
1066 }
1067
1068
1069 int
1070 perfuse_node_lookup(struct puffs_usermount *pu, puffs_cookie_t opc,
1071 struct puffs_newinfo *pni, const struct puffs_cn *pcn)
1072 {
1073 struct perfuse_state *ps;
1074 struct puffs_node *pn;
1075 mode_t mode;
1076 int error;
1077
1078 ps = puffs_getspecific(pu);
1079 node_ref(opc);
1080
1081 /*
1082 * Check permissions
1083 */
1084 switch(pcn->pcn_nameiop) {
1085 case NAMEI_DELETE: /* FALLTHROUGH */
1086 case NAMEI_RENAME: /* FALLTHROUGH */
1087 case NAMEI_CREATE:
1088 if (pcn->pcn_flags & NAMEI_ISLASTCN)
1089 mode = PUFFS_VEXEC|PUFFS_VWRITE;
1090 else
1091 mode = PUFFS_VEXEC;
1092 break;
1093 case NAMEI_LOOKUP: /* FALLTHROUGH */
1094 default:
1095 mode = PUFFS_VEXEC;
1096 break;
1097 }
1098
1099 if ((error = mode_access(opc, pcn->pcn_cred, mode)) != 0)
1100 goto out;
1101
1102 error = node_lookup_common(pu, (puffs_cookie_t)opc, pni,
1103 pcn->pcn_name, pcn->pcn_cred, &pn);
1104
1105 if (error != 0)
1106 goto out;
1107
1108 /*
1109 * Kernel would kill us if the filesystem returned the parent
1110 * itself. If we want to live, hide that!
1111 */
1112 if ((opc == (puffs_cookie_t)pn) && (strcmp(pcn->pcn_name, ".") != 0)) {
1113 DERRX(EX_SOFTWARE, "lookup \"%s\" in \"%s\" returned parent",
1114 pcn->pcn_name, perfuse_node_path(ps, opc));
1115 /* NOTREACHED */
1116 error = ESTALE;
1117 goto out;
1118 }
1119
1120 /*
1121 * Removed node
1122 */
1123 if (PERFUSE_NODE_DATA(pn)->pnd_flags & PND_REMOVED) {
1124 error = ENOENT;
1125 goto out;
1126 }
1127
1128 /*
1129 * Check for sticky bit. Unfortunately there is no way to
1130 * do this before creating the puffs_node, since we require
1131 * this operation to get the node owner.
1132 */
1133 switch (pcn->pcn_nameiop) {
1134 case NAMEI_DELETE: /* FALLTHROUGH */
1135 case NAMEI_RENAME:
1136 error = sticky_access(opc, pn, pcn->pcn_cred);
1137 if (error != 0) {
1138 (void)perfuse_node_reclaim(pu, pn);
1139 goto out;
1140 }
1141 break;
1142 default:
1143 break;
1144 }
1145
1146 PERFUSE_NODE_DATA(pn)->pnd_puffs_nlookup++;
1147
1148 error = 0;
1149
1150 out:
1151 node_rele(opc);
1152 return error;
1153 }
1154
1155 int
1156 perfuse_node_create(struct puffs_usermount *pu, puffs_cookie_t opc,
1157 struct puffs_newinfo *pni, const struct puffs_cn *pcn,
1158 const struct vattr *vap)
1159 {
1160 perfuse_msg_t *pm;
1161 struct perfuse_state *ps;
1162 struct fuse_create_in *fci;
1163 struct fuse_entry_out *feo;
1164 struct fuse_open_out *foo;
1165 struct puffs_node *pn;
1166 const char *name;
1167 size_t namelen;
1168 size_t len;
1169 int error;
1170
1171 if (PERFUSE_NODE_DATA(opc)->pnd_flags & PND_REMOVED)
1172 return ENOENT;
1173
1174 node_ref(opc);
1175
1176 /*
1177 * If create is unimplemented: Check that it does not
1178 * already exists, and if not, do mknod and open
1179 */
1180 ps = puffs_getspecific(pu);
1181 if (ps->ps_flags & PS_NO_CREAT) {
1182 error = node_lookup_common(pu, opc, NULL, pcn->pcn_name,
1183 pcn->pcn_cred, &pn);
1184 if (error == 0) {
1185 (void)perfuse_node_reclaim(pu, pn);
1186 error = EEXIST;
1187 goto out;
1188 }
1189
1190 error = perfuse_node_mknod(pu, opc, pni, pcn, vap);
1191 if (error != 0)
1192 goto out;
1193
1194 error = node_lookup_common(pu, opc, NULL, pcn->pcn_name,
1195 pcn->pcn_cred, &pn);
1196 if (error != 0)
1197 goto out;
1198
1199 /*
1200 * FUSE does the open at create time, while
1201 * NetBSD will open in a subsequent operation.
1202 * We need to open now, in order to retain FUSE
1203 * semantics. The calling process will not get
1204 * a file descriptor before the kernel sends
1205 * the open operation.
1206 */
1207 error = perfuse_node_open(pu, (puffs_cookie_t)pn,
1208 FWRITE, pcn->pcn_cred);
1209 goto out;
1210 }
1211
1212 name = pcn->pcn_name;
1213 namelen = pcn->pcn_namelen + 1;
1214 len = sizeof(*fci) + namelen;
1215
1216 /*
1217 * flags should use O_WRONLY instead of O_RDWR, but it
1218 * breaks when the caller tries to read from file.
1219 *
1220 * mode must contain file type (ie: S_IFREG), use VTTOIF(vap->va_type)
1221 */
1222 pm = ps->ps_new_msg(pu, opc, FUSE_CREATE, len, pcn->pcn_cred);
1223 fci = GET_INPAYLOAD(ps, pm, fuse_create_in);
1224 fci->flags = O_CREAT | O_TRUNC | O_RDWR;
1225 fci->mode = vap->va_mode | VTTOIF(vap->va_type);
1226 fci->umask = 0; /* Seems unused by libfuse */
1227 (void)strlcpy((char*)(void *)(fci + 1), name, namelen);
1228
1229 len = sizeof(*feo) + sizeof(*foo);
1230 if ((error = xchg_msg(pu, opc, pm, len, wait_reply)) != 0) {
1231 /*
1232 * create is unimplmented, remember it for later,
1233 * and start over using mknod and open instead.
1234 */
1235 if (error == ENOSYS) {
1236 ps->ps_flags |= PS_NO_CREAT;
1237 error = perfuse_node_create(pu, opc, pni, pcn, vap);
1238 }
1239
1240 goto out;
1241 }
1242
1243 feo = GET_OUTPAYLOAD(ps, pm, fuse_entry_out);
1244 foo = (struct fuse_open_out *)(void *)(feo + 1);
1245 if (feo->nodeid == PERFUSE_UNKNOWN_NODEID)
1246 DERRX(EX_SOFTWARE, "%s: no nodeid", __func__);
1247
1248 /*
1249 * Save the file handle and inode in node private data
1250 * so that we can reuse it later
1251 */
1252 pn = perfuse_new_pn(pu, name, opc);
1253 perfuse_new_fh((puffs_cookie_t)pn, foo->fh, FWRITE);
1254 PERFUSE_NODE_DATA(pn)->pnd_nodeid = feo->nodeid;
1255 PERFUSE_NODE_DATA(pn)->pnd_puffs_nlookup++;
1256 perfuse_node_cache(ps, pn);
1257
1258 fuse_attr_to_vap(ps, &pn->pn_va, &feo->attr);
1259 pn->pn_va.va_gen = (u_long)(feo->generation);
1260
1261 puffs_newinfo_setcookie(pni, pn);
1262 #ifdef PUFFS_KFLAG_CACHE_FS_TTL
1263 puffs_newinfo_setva(pni, &pn->pn_va);
1264 perfuse_newinfo_setttl(pni, pn, feo, NULL);
1265 #endif /* PUFFS_KFLAG_CACHE_FS_TTL */
1266
1267 #ifdef PERFUSE_DEBUG
1268 if (perfuse_diagflags & (PDF_FH|PDF_FILENAME))
1269 DPRINTF("%s: opc = %p, file = \"%s\", flags = 0x%x "
1270 "nodeid = 0x%"PRIx64", wfh = 0x%"PRIx64"\n",
1271 __func__, (void *)pn, pcn->pcn_name,
1272 PERFUSE_NODE_DATA(pn)->pnd_flags, feo->nodeid,
1273 foo->fh);
1274 #endif
1275
1276 ps->ps_destroy_msg(pm);
1277 error = 0;
1278
1279 out:
1280 node_rele(opc);
1281 return error;
1282 }
1283
1284
1285 int
1286 perfuse_node_mknod(struct puffs_usermount *pu, puffs_cookie_t opc,
1287 struct puffs_newinfo *pni, const struct puffs_cn *pcn,
1288 const struct vattr *vap)
1289 {
1290 struct perfuse_state *ps;
1291 perfuse_msg_t *pm;
1292 struct fuse_mknod_in *fmi;
1293 const char* path;
1294 size_t len;
1295 int error;
1296
1297 if (PERFUSE_NODE_DATA(opc)->pnd_flags & PND_REMOVED)
1298 return ENOENT;
1299
1300 node_ref(opc);
1301
1302 /*
1303 * Only superuser can mknod objects other than
1304 * directories, files, socks, fifo and links.
1305 *
1306 * Create an object require -WX permission in the parent directory
1307 */
1308 switch (vap->va_type) {
1309 case VDIR: /* FALLTHROUGH */
1310 case VREG: /* FALLTHROUGH */
1311 case VFIFO: /* FALLTHROUGH */
1312 case VSOCK:
1313 break;
1314 default: /* VNON, VBLK, VCHR, VBAD */
1315 if (!puffs_cred_isjuggernaut(pcn->pcn_cred)) {
1316 error = EPERM;
1317 goto out;
1318 }
1319 break;
1320 }
1321
1322
1323 ps = puffs_getspecific(pu);
1324 path = pcn->pcn_name;
1325 len = sizeof(*fmi) + pcn->pcn_namelen + 1;
1326
1327 /*
1328 * mode must contain file type (ie: S_IFREG), use VTTOIF(vap->va_type)
1329 */
1330 pm = ps->ps_new_msg(pu, opc, FUSE_MKNOD, len, pcn->pcn_cred);
1331 fmi = GET_INPAYLOAD(ps, pm, fuse_mknod_in);
1332 fmi->mode = vap->va_mode | VTTOIF(vap->va_type);
1333 fmi->rdev = (uint32_t)vap->va_rdev;
1334 fmi->umask = 0; /* Seems unused bu libfuse */
1335 (void)strlcpy((char *)(void *)(fmi + 1), path, len - sizeof(*fmi));
1336
1337 error = node_mk_common(pu, opc, pni, pcn, pm);
1338
1339 out:
1340 node_rele(opc);
1341 return error;
1342 }
1343
1344
1345 int
1346 perfuse_node_open(struct puffs_usermount *pu, puffs_cookie_t opc, int mode,
1347 const struct puffs_cred *pcr)
1348 {
1349 return perfuse_node_open2(pu, opc, mode, pcr, NULL);
1350 }
1351
1352 int
1353 perfuse_node_open2(struct puffs_usermount *pu, puffs_cookie_t opc, int mode,
1354 const struct puffs_cred *pcr, int *oflags)
1355 {
1356 struct perfuse_state *ps;
1357 struct perfuse_node_data *pnd;
1358 perfuse_msg_t *pm;
1359 mode_t fmode;
1360 int op;
1361 struct fuse_open_in *foi;
1362 struct fuse_open_out *foo;
1363 struct puffs_node *pn;
1364 int error;
1365
1366 ps = puffs_getspecific(pu);
1367 pn = (struct puffs_node *)opc;
1368 pnd = PERFUSE_NODE_DATA(opc);
1369 error = 0;
1370
1371 if (pnd->pnd_flags & PND_REMOVED)
1372 return ENOENT;
1373
1374 node_ref(opc);
1375
1376 if (puffs_pn_getvap(pn)->va_type == VDIR)
1377 op = FUSE_OPENDIR;
1378 else
1379 op = FUSE_OPEN;
1380
1381 /*
1382 * libfuse docs says
1383 * - O_CREAT and O_EXCL should never be set.
1384 * - O_TRUNC may be used if mount option atomic_o_trunc is used XXX
1385 *
1386 * O_APPEND makes no sense since FUSE always sends
1387 * the file offset for write operations. If the
1388 * filesystem uses pwrite(), O_APPEND would cause
1389 * the offset to be ignored and cause file corruption.
1390 */
1391 mode &= ~(O_CREAT|O_EXCL|O_APPEND);
1392
1393 /*
1394 * Do not open twice, and do not reopen for reading
1395 * if we already have write handle.
1396 */
1397 switch (mode & (FREAD|FWRITE)) {
1398 case FREAD:
1399 if (pnd->pnd_flags & (PND_RFH|PND_WFH))
1400 goto out;
1401 break;
1402 case FWRITE:
1403 if (pnd->pnd_flags & PND_WFH)
1404 goto out;
1405 break;
1406 case FREAD|FWRITE:
1407 if (pnd->pnd_flags & PND_WFH)
1408 goto out;
1409
1410 /*
1411 * Corner case: if already open for reading (PND_RFH)
1412 * and re-opening FREAD|FWRITE, we need to reopen,
1413 * but only for writing. Note the change on mode
1414 * will only affect perfuse_new_fh()
1415 */
1416 if (pnd->pnd_flags & PND_RFH)
1417 mode &= ~FREAD;
1418 break;
1419 default:
1420 DWARNX("open without either FREAD nor FWRITE");
1421 error = EPERM;
1422 goto out;
1423 }
1424
1425 /*
1426 * Queue open on a node so that we do not open
1427 * twice. This would be better with read and
1428 * write distinguished.
1429 */
1430 while (pnd->pnd_flags & PND_INOPEN)
1431 requeue_request(pu, opc, PCQ_OPEN);
1432 pnd->pnd_flags |= PND_INOPEN;
1433
1434 /*
1435 * Convert PUFFS mode to FUSE mode: convert FREAD/FWRITE
1436 * to O_RDONLY/O_WRONLY while perserving the other options.
1437 */
1438 fmode = mode & ~(FREAD|FWRITE);
1439 fmode |= (mode & FWRITE) ? O_RDWR : O_RDONLY;
1440
1441 pm = ps->ps_new_msg(pu, opc, op, sizeof(*foi), pcr);
1442 foi = GET_INPAYLOAD(ps, pm, fuse_open_in);
1443 foi->flags = fmode;
1444 foi->unused = 0;
1445
1446 if ((error = xchg_msg(pu, opc, pm, sizeof(*foo), wait_reply)) != 0)
1447 goto out;
1448
1449 foo = GET_OUTPAYLOAD(ps, pm, fuse_open_out);
1450
1451 /*
1452 * Save the file handle in node private data
1453 * so that we can reuse it later
1454 */
1455 perfuse_new_fh(opc, foo->fh, mode);
1456
1457 /*
1458 * Set direct I/O if the filesystems forces it
1459 */
1460 if ((foo->open_flags & FUSE_FOPEN_DIRECT_IO) && (oflags != NULL))
1461 *oflags |= PUFFS_OPEN_IO_DIRECT;
1462
1463 #ifdef PERFUSE_DEBUG
1464 if (perfuse_diagflags & (PDF_FH|PDF_FILENAME))
1465 DPRINTF("%s: opc = %p, file = \"%s\", "
1466 "nodeid = 0x%"PRIx64", %s%sfh = 0x%"PRIx64"\n",
1467 __func__, (void *)opc, perfuse_node_path(ps, opc),
1468 pnd->pnd_nodeid, mode & FREAD ? "r" : "",
1469 mode & FWRITE ? "w" : "", foo->fh);
1470 #endif
1471
1472 ps->ps_destroy_msg(pm);
1473 out:
1474
1475 pnd->pnd_flags &= ~PND_INOPEN;
1476 (void)dequeue_requests(opc, PCQ_OPEN, DEQUEUE_ALL);
1477
1478 node_rele(opc);
1479 return error;
1480 }
1481
1482 /* ARGSUSED0 */
1483 int
1484 perfuse_node_close(struct puffs_usermount *pu, puffs_cookie_t opc, int flags,
1485 const struct puffs_cred *pcr)
1486 {
1487 struct perfuse_node_data *pnd;
1488
1489 pnd = PERFUSE_NODE_DATA(opc);
1490
1491 if (!(pnd->pnd_flags & PND_OPEN))
1492 return EBADF;
1493
1494 /*
1495 * Actual close is postponed at inactive time.
1496 */
1497 return 0;
1498 }
1499
1500 int
1501 perfuse_node_access(struct puffs_usermount *pu, puffs_cookie_t opc, int mode,
1502 const struct puffs_cred *pcr)
1503 {
1504 perfuse_msg_t *pm;
1505 struct perfuse_state *ps;
1506 struct fuse_access_in *fai;
1507 int error;
1508
1509 if (PERFUSE_NODE_DATA(opc)->pnd_flags & PND_REMOVED)
1510 return ENOENT;
1511
1512 node_ref(opc);
1513
1514 /*
1515 * If we previously detected the filesystem does not
1516 * implement access(), short-circuit the call and skip
1517 * to libpuffs access() emulation.
1518 */
1519 ps = puffs_getspecific(pu);
1520 if (ps->ps_flags & PS_NO_ACCESS) {
1521 const struct vattr *vap;
1522
1523 vap = puffs_pn_getvap((struct puffs_node *)opc);
1524
1525 error = puffs_access(IFTOVT(vap->va_mode),
1526 vap->va_mode & ACCESSPERMS,
1527 vap->va_uid, vap->va_gid,
1528 (mode_t)mode, pcr);
1529 goto out;
1530 }
1531
1532 /*
1533 * Plain access call
1534 */
1535 pm = ps->ps_new_msg(pu, opc, FUSE_ACCESS, sizeof(*fai), pcr);
1536 fai = GET_INPAYLOAD(ps, pm, fuse_access_in);
1537 fai->mask = 0;
1538 fai->mask |= (mode & PUFFS_VREAD) ? R_OK : 0;
1539 fai->mask |= (mode & PUFFS_VWRITE) ? W_OK : 0;
1540 fai->mask |= (mode & PUFFS_VEXEC) ? X_OK : 0;
1541
1542 error = xchg_msg(pu, opc, pm, NO_PAYLOAD_REPLY_LEN, wait_reply);
1543
1544 ps->ps_destroy_msg(pm);
1545
1546 /*
1547 * If unimplemented, start over with emulation
1548 */
1549 if (error == ENOSYS) {
1550 ps->ps_flags |= PS_NO_ACCESS;
1551 error = perfuse_node_access(pu, opc, mode, pcr);
1552 }
1553
1554 out:
1555 node_rele(opc);
1556 return error;
1557 }
1558
1559 int
1560 perfuse_node_getattr(struct puffs_usermount *pu, puffs_cookie_t opc,
1561 struct vattr *vap, const struct puffs_cred *pcr)
1562 {
1563 return perfuse_node_getattr_ttl(pu, opc, vap, pcr, NULL);
1564 }
1565
1566 int
1567 perfuse_node_getattr_ttl(struct puffs_usermount *pu, puffs_cookie_t opc,
1568 struct vattr *vap, const struct puffs_cred *pcr,
1569 struct timespec *va_ttl)
1570 {
1571 perfuse_msg_t *pm = NULL;
1572 struct perfuse_state *ps;
1573 struct perfuse_node_data *pnd = PERFUSE_NODE_DATA(opc);
1574 struct fuse_getattr_in *fgi;
1575 struct fuse_attr_out *fao;
1576 int error = 0;
1577
1578 if ((pnd->pnd_flags & PND_REMOVED) && !(pnd->pnd_flags & PND_OPEN))
1579 return ENOENT;
1580
1581 node_ref(opc);
1582
1583 /*
1584 * Serialize size access, see comment in perfuse_node_setattr().
1585 */
1586 while (pnd->pnd_flags & PND_INRESIZE)
1587 requeue_request(pu, opc, PCQ_RESIZE);
1588 pnd->pnd_flags |= PND_INRESIZE;
1589
1590 ps = puffs_getspecific(pu);
1591
1592 /*
1593 * FUSE_GETATTR_FH must be set in fgi->flags
1594 * if we use for fgi->fh
1595 */
1596 pm = ps->ps_new_msg(pu, opc, FUSE_GETATTR, sizeof(*fgi), pcr);
1597 fgi = GET_INPAYLOAD(ps, pm, fuse_getattr_in);
1598 fgi->getattr_flags = 0;
1599 fgi->dummy = 0;
1600 fgi->fh = 0;
1601
1602 if (PERFUSE_NODE_DATA(opc)->pnd_flags & PND_OPEN) {
1603 fgi->fh = perfuse_get_fh(opc, FREAD);
1604 fgi->getattr_flags |= FUSE_GETATTR_FH;
1605 }
1606
1607 #ifdef PERFUSE_DEBUG
1608 if (perfuse_diagflags & PDF_RESIZE)
1609 DPRINTF(">> %s %p %" PRIu64 "\n", __func__, (void *)opc,
1610 vap->va_size);
1611 #endif
1612
1613 if ((error = xchg_msg(pu, opc, pm, sizeof(*fao), wait_reply)) != 0)
1614 goto out;
1615
1616 fao = GET_OUTPAYLOAD(ps, pm, fuse_attr_out);
1617
1618 #ifdef PERFUSE_DEBUG
1619 if (perfuse_diagflags & PDF_RESIZE)
1620 DPRINTF("<< %s %p %" PRIu64 " -> %" PRIu64 "\n", __func__,
1621 (void *)opc, vap->va_size, fao->attr.size);
1622 #endif
1623
1624 /*
1625 * We set birthtime, flags, filerev,vaflags to 0.
1626 * This seems the best bet, since the information is
1627 * not available from filesystem.
1628 */
1629 fuse_attr_to_vap(ps, vap, &fao->attr);
1630
1631 if (va_ttl != NULL) {
1632 va_ttl->tv_sec = fao->attr_valid;
1633 va_ttl->tv_nsec = fao->attr_valid_nsec;
1634 }
1635
1636 ps->ps_destroy_msg(pm);
1637 error = 0;
1638 out:
1639
1640 pnd->pnd_flags &= ~PND_INRESIZE;
1641 (void)dequeue_requests(opc, PCQ_RESIZE, DEQUEUE_ALL);
1642
1643 node_rele(opc);
1644 return error;
1645 }
1646
1647 int
1648 perfuse_node_setattr(struct puffs_usermount *pu, puffs_cookie_t opc,
1649 const struct vattr *vap, const struct puffs_cred *pcr)
1650 {
1651 return perfuse_node_setattr_ttl(pu, opc,
1652 __UNCONST(vap), pcr, NULL, 0);
1653 }
1654
1655 int
1656 perfuse_node_setattr_ttl(struct puffs_usermount *pu, puffs_cookie_t opc,
1657 struct vattr *vap, const struct puffs_cred *pcr,
1658 struct timespec *va_ttl, int xflag)
1659 {
1660 perfuse_msg_t *pm;
1661 uint64_t fh;
1662 struct perfuse_state *ps;
1663 struct perfuse_node_data *pnd;
1664 struct fuse_setattr_in *fsi;
1665 struct fuse_attr_out *fao;
1666 struct vattr *old_va;
1667 enum perfuse_xchg_pb_reply reply;
1668 int error;
1669 #ifdef PERFUSE_DEBUG
1670 struct vattr *old_vap;
1671 int resize_debug = 0;
1672 #endif
1673 ps = puffs_getspecific(pu);
1674 pnd = PERFUSE_NODE_DATA(opc);
1675
1676 /*
1677 * The only operation we can do once the file is removed
1678 * is to resize it, and we can do it only if it is open.
1679 * Do not even send the operation to the filesystem: the
1680 * file is not there anymore.
1681 */
1682 if (pnd->pnd_flags & PND_REMOVED) {
1683 if (!(pnd->pnd_flags & PND_OPEN))
1684 return ENOENT;
1685
1686 return 0;
1687 }
1688
1689 old_va = puffs_pn_getvap((struct puffs_node *)opc);
1690
1691 /*
1692 * Check for permission to change size
1693 * It is always allowed if we already have a write file handle
1694 */
1695 if ((vap->va_size != (u_quad_t)PUFFS_VNOVAL) &&
1696 !(pnd->pnd_flags & PND_WFH) &&
1697 (error = mode_access(opc, pcr, PUFFS_VWRITE)) != 0)
1698 return error;
1699
1700 /*
1701 * Check for permission to change dates
1702 */
1703 if (((vap->va_atime.tv_sec != (time_t)PUFFS_VNOVAL) ||
1704 (vap->va_mtime.tv_sec != (time_t)PUFFS_VNOVAL)) &&
1705 (puffs_access_times(old_va->va_uid, old_va->va_gid,
1706 old_va->va_mode, 0, pcr) != 0))
1707 return EPERM;
1708
1709 /*
1710 * Check for permission to change owner and group
1711 */
1712 if (((vap->va_uid != (uid_t)PUFFS_VNOVAL) ||
1713 (vap->va_gid != (gid_t)PUFFS_VNOVAL)) &&
1714 (puffs_access_chown(old_va->va_uid, old_va->va_gid,
1715 vap->va_uid, vap->va_gid, pcr)) != 0)
1716 return EPERM;
1717
1718 /*
1719 * Check for sticky bit on non-directory by non root user
1720 */
1721 if ((vap->va_mode != (mode_t)PUFFS_VNOVAL) &&
1722 (vap->va_mode & S_ISTXT) && (old_va->va_type != VDIR) &&
1723 !puffs_cred_isjuggernaut(pcr))
1724 return EFTYPE;
1725
1726 /*
1727 * Check for permission to change permissions
1728 */
1729 if ((vap->va_mode != (mode_t)PUFFS_VNOVAL) &&
1730 (puffs_access_chmod(old_va->va_uid, old_va->va_gid,
1731 old_va->va_type, vap->va_mode, pcr)) != 0)
1732 return EPERM;
1733
1734 node_ref(opc);
1735
1736 if (pnd->pnd_flags & PND_WFH)
1737 fh = perfuse_get_fh(opc, FWRITE);
1738 else
1739 fh = FUSE_UNKNOWN_FH;
1740
1741 /*
1742 * fchmod() sets mode and fh, and it may carry
1743 * a resize as well. That may break if the
1744 * filesystem does chmod then resize, and fails
1745 * because it does not have permission anymore.
1746 * We work this around by splitting into two setattr.
1747 */
1748 if ((vap->va_size != (u_quad_t)PUFFS_VNOVAL) &&
1749 (vap->va_mode != (mode_t)PUFFS_VNOVAL) &&
1750 (fh != FUSE_UNKNOWN_FH)) {
1751 struct vattr resize_va;
1752
1753 (void)memcpy(&resize_va, vap, sizeof(resize_va));
1754 resize_va.va_mode = (mode_t)PUFFS_VNOVAL;
1755 if ((error = perfuse_node_setattr_ttl(pu, opc, &resize_va,
1756 pcr, va_ttl, xflag)) != 0)
1757 goto out2;
1758
1759 vap->va_size = (u_quad_t)PUFFS_VNOVAL;
1760 }
1761
1762 pm = ps->ps_new_msg(pu, opc, FUSE_SETATTR, sizeof(*fsi), pcr);
1763 fsi = GET_INPAYLOAD(ps, pm, fuse_setattr_in);
1764 fsi->valid = 0;
1765
1766 /*
1767 * Get a fh if the node is open for writing
1768 */
1769 if (fh != FUSE_UNKNOWN_FH) {
1770 fsi->fh = fh;
1771 fsi->valid |= FUSE_FATTR_FH;
1772 }
1773
1774
1775 if (vap->va_size != (u_quad_t)PUFFS_VNOVAL) {
1776 fsi->size = vap->va_size;
1777 fsi->valid |= FUSE_FATTR_SIZE;
1778
1779 /*
1780 * Serialize anything that can touch file size
1781 * to avoid reordered GETATTR and SETATTR.
1782 * Out of order SETATTR can report stale size,
1783 * which will cause the kernel to truncate the file.
1784 * XXX Probably useless now we have a lock on GETATTR
1785 */
1786 while (pnd->pnd_flags & PND_INRESIZE)
1787 requeue_request(pu, opc, PCQ_RESIZE);
1788 pnd->pnd_flags |= PND_INRESIZE;
1789 }
1790
1791 /*
1792 * When not sending a time field, still fill with
1793 * current value, as the filesystem may just reset
1794 * the field to Epoch even if fsi->valid bit is
1795 * not set (GlusterFS does that).
1796 */
1797 if (vap->va_atime.tv_sec != (time_t)PUFFS_VNOVAL) {
1798 fsi->atime = vap->va_atime.tv_sec;
1799 fsi->atimensec = (uint32_t)vap->va_atime.tv_nsec;
1800 fsi->valid |= FUSE_FATTR_ATIME;
1801 } else {
1802 fsi->atime = old_va->va_atime.tv_sec;
1803 fsi->atimensec = (uint32_t)old_va->va_atime.tv_nsec;
1804 }
1805
1806 if (vap->va_mtime.tv_sec != (time_t)PUFFS_VNOVAL) {
1807 fsi->mtime = vap->va_mtime.tv_sec;
1808 fsi->mtimensec = (uint32_t)vap->va_mtime.tv_nsec;
1809 fsi->valid |= FUSE_FATTR_MTIME;
1810 } else {
1811 fsi->mtime = old_va->va_mtime.tv_sec;
1812 fsi->mtimensec = (uint32_t)old_va->va_mtime.tv_nsec;
1813 }
1814
1815 if (vap->va_mode != (mode_t)PUFFS_VNOVAL) {
1816 fsi->mode = vap->va_mode;
1817 fsi->valid |= FUSE_FATTR_MODE;
1818 }
1819
1820 if (vap->va_uid != (uid_t)PUFFS_VNOVAL) {
1821 fsi->uid = vap->va_uid;
1822 fsi->valid |= FUSE_FATTR_UID;
1823 }
1824
1825 if (vap->va_gid != (gid_t)PUFFS_VNOVAL) {
1826 fsi->gid = vap->va_gid;
1827 fsi->valid |= FUSE_FATTR_GID;
1828 }
1829
1830 if (pnd->pnd_lock_owner != 0) {
1831 fsi->lock_owner = pnd->pnd_lock_owner;
1832 fsi->valid |= FUSE_FATTR_LOCKOWNER;
1833 }
1834
1835 #ifndef PUFFS_KFLAG_NOFLUSH_META
1836 /*
1837 * ftruncate() sends only va_size, and metadata cache
1838 * flush adds va_atime and va_mtime. Some FUSE
1839 * filesystems will attempt to detect ftruncate by
1840 * checking for FATTR_SIZE being set without
1841 * FATTR_UID|FATTR_GID|FATTR_ATIME|FATTR_MTIME|FATTR_MODE
1842 *
1843 * Try to adapt and remove FATTR_ATIME|FATTR_MTIME
1844 * if we suspect a ftruncate().
1845 */
1846 if ((vap->va_size != (u_quad_t)PUFFS_VNOVAL) &&
1847 ((vap->va_mode == (mode_t)PUFFS_VNOVAL) &&
1848 (vap->va_uid == (uid_t)PUFFS_VNOVAL) &&
1849 (vap->va_gid == (gid_t)PUFFS_VNOVAL))) {
1850 fsi->atime = 0;
1851 fsi->atimensec = 0;
1852 fsi->mtime = 0;
1853 fsi->mtimensec = 0;
1854 fsi->valid &= ~(FUSE_FATTR_ATIME|FUSE_FATTR_MTIME);
1855 }
1856
1857 /*
1858 * If only atime is changed, discard the operation: it
1859 * happens after read, and in that case the filesystem
1860 * already updaed atime. NB: utimes() also change mtime.
1861 */
1862 if (fsi->valid == FUSE_FATTR_ATIME)
1863 fsi->valid &= ~FUSE_FATTR_ATIME;
1864 #endif /* PUFFS_KFLAG_NOFLUSH_META */
1865
1866 /*
1867 * If nothing remain, discard the operation.
1868 */
1869 if (!(fsi->valid & (FUSE_FATTR_SIZE|FUSE_FATTR_ATIME|FUSE_FATTR_MTIME|
1870 FUSE_FATTR_MODE|FUSE_FATTR_UID|FUSE_FATTR_GID))) {
1871 error = 0;
1872 ps->ps_destroy_msg(pm);
1873 goto out;
1874 }
1875
1876 #ifdef PERFUSE_DEBUG
1877 old_vap = puffs_pn_getvap((struct puffs_node *)opc);
1878
1879 if ((perfuse_diagflags & PDF_RESIZE) &&
1880 (old_vap->va_size != (u_quad_t)PUFFS_VNOVAL)) {
1881 resize_debug = 1;
1882
1883 DPRINTF(">> %s %p %" PRIu64 " -> %" PRIu64 "\n", __func__,
1884 (void *)opc,
1885 puffs_pn_getvap((struct puffs_node *)opc)->va_size,
1886 fsi->size);
1887 }
1888 #endif
1889
1890 /*
1891 * Do not honour FAF when changing size. How do
1892 * you want such a thing to work?
1893 */
1894 reply = wait_reply;
1895 #ifdef PUFFS_SETATTR_FAF
1896 if ((xflag & PUFFS_SETATTR_FAF) && !(fsi->valid & FUSE_FATTR_SIZE))
1897 reply = no_reply;
1898 #endif
1899 if ((error = xchg_msg(pu, opc, pm, sizeof(*fao), reply)) != 0)
1900 goto out;
1901
1902 if (reply == no_reply)
1903 goto out;
1904
1905 /*
1906 * Copy back the new values
1907 */
1908 fao = GET_OUTPAYLOAD(ps, pm, fuse_attr_out);
1909
1910 #ifdef PERFUSE_DEBUG
1911 if (resize_debug)
1912 DPRINTF("<< %s %p %" PRIu64 " -> %" PRIu64 "\n", __func__,
1913 (void *)opc, old_vap->va_size, fao->attr.size);
1914 #endif
1915
1916 fuse_attr_to_vap(ps, old_va, &fao->attr);
1917
1918 if (va_ttl != NULL) {
1919 va_ttl->tv_sec = fao->attr_valid;
1920 va_ttl->tv_nsec = fao->attr_valid_nsec;
1921 (void)memcpy(vap, old_va, sizeof(*vap));
1922 }
1923
1924 ps->ps_destroy_msg(pm);
1925 error = 0;
1926
1927 out:
1928 if (pnd->pnd_flags & PND_INRESIZE) {
1929 pnd->pnd_flags &= ~PND_INRESIZE;
1930 (void)dequeue_requests(opc, PCQ_RESIZE, DEQUEUE_ALL);
1931 }
1932
1933 out2:
1934 node_rele(opc);
1935 return error;
1936 }
1937
1938 int
1939 perfuse_node_poll(struct puffs_usermount *pu, puffs_cookie_t opc, int *events)
1940 {
1941 struct perfuse_state *ps;
1942 perfuse_msg_t *pm;
1943 struct fuse_poll_in *fpi;
1944 struct fuse_poll_out *fpo;
1945 int error;
1946
1947 node_ref(opc);
1948 ps = puffs_getspecific(pu);
1949 /*
1950 * kh is set if FUSE_POLL_SCHEDULE_NOTIFY is set.
1951 *
1952 * XXX ps_new_msg() is called with NULL creds, which will
1953 * be interpreted as FUSE superuser. We have no way to
1954 * know the requesting process' credential, but since poll
1955 * is supposed to operate on a file that has been open,
1956 * permission should have already been checked at open time.
1957 * That still may breaks on filesystems that provides odd
1958 * semantics.
1959 */
1960 pm = ps->ps_new_msg(pu, opc, FUSE_POLL, sizeof(*fpi), NULL);
1961 fpi = GET_INPAYLOAD(ps, pm, fuse_poll_in);
1962 fpi->fh = perfuse_get_fh(opc, FREAD);
1963 fpi->kh = 0;
1964 fpi->flags = 0;
1965
1966 #ifdef PERFUSE_DEBUG
1967 if (perfuse_diagflags & PDF_FH)
1968 DPRINTF("%s: opc = %p, nodeid = 0x%"PRIx64", "
1969 "fh = 0x%"PRIx64"\n", __func__, (void *)opc,
1970 PERFUSE_NODE_DATA(opc)->pnd_nodeid, fpi->fh);
1971 #endif
1972 if ((error = xchg_msg(pu, opc, pm, sizeof(*fpo), wait_reply)) != 0)
1973 goto out;
1974
1975 fpo = GET_OUTPAYLOAD(ps, pm, fuse_poll_out);
1976 *events = fpo->revents;
1977
1978 ps->ps_destroy_msg(pm);
1979 error = 0;
1980
1981 out:
1982 node_rele(opc);
1983 return error;
1984 }
1985
1986 /* ARGSUSED2 */
1987 int
1988 perfuse_node_fsync(struct puffs_usermount *pu, puffs_cookie_t opc,
1989 const struct puffs_cred *pcr, int flags, off_t offlo, off_t offhi)
1990 {
1991 int op;
1992 perfuse_msg_t *pm;
1993 struct perfuse_state *ps;
1994 struct perfuse_node_data *pnd;
1995 struct fuse_fsync_in *ffi;
1996 uint64_t fh;
1997 int error = 0;
1998
1999 pm = NULL;
2000 ps = puffs_getspecific(pu);
2001 pnd = PERFUSE_NODE_DATA(opc);
2002
2003 /*
2004 * No need to sync a removed node
2005 */
2006 if (pnd->pnd_flags & PND_REMOVED)
2007 return 0;
2008
2009 /*
2010 * We do not sync closed files. They have been
2011 * sync at inactive time already.
2012 */
2013 if (!(pnd->pnd_flags & PND_OPEN))
2014 return 0;
2015
2016 node_ref(opc);
2017
2018 if (puffs_pn_getvap((struct puffs_node *)opc)->va_type == VDIR)
2019 op = FUSE_FSYNCDIR;
2020 else /* VREG but also other types such as VLNK */
2021 op = FUSE_FSYNC;
2022
2023 /*
2024 * Do not sync if there are no change to sync
2025 * XXX remove that test on files if we implement mmap
2026 */
2027 #ifdef PERFUSE_DEBUG
2028 if (perfuse_diagflags & PDF_SYNC)
2029 DPRINTF("%s: TEST opc = %p, file = \"%s\" is %sdirty\n",
2030 __func__, (void*)opc, perfuse_node_path(ps, opc),
2031 pnd->pnd_flags & PND_DIRTY ? "" : "not ");
2032 #endif
2033 if (!(pnd->pnd_flags & PND_DIRTY))
2034 goto out;
2035
2036 /*
2037 * It seems NetBSD can call fsync without open first
2038 * glusterfs complain in such a situation:
2039 * "FSYNC() ERR => -1 (Invalid argument)"
2040 * The file will be closed at inactive time.
2041 *
2042 * We open the directory for reading in order to sync.
2043 * This sounds rather counterintuitive, but it works.
2044 */
2045 if (!(pnd->pnd_flags & PND_WFH)) {
2046 if ((error = perfuse_node_open(pu, opc, FREAD, pcr)) != 0)
2047 goto out;
2048 }
2049
2050 if (op == FUSE_FSYNCDIR)
2051 fh = perfuse_get_fh(opc, FREAD);
2052 else
2053 fh = perfuse_get_fh(opc, FWRITE);
2054
2055 /*
2056 * If fsync_flags is set, meta data should not be flushed.
2057 */
2058 pm = ps->ps_new_msg(pu, opc, op, sizeof(*ffi), pcr);
2059 ffi = GET_INPAYLOAD(ps, pm, fuse_fsync_in);
2060 ffi->fh = fh;
2061 ffi->fsync_flags = (flags & FFILESYNC) ? 0 : 1;
2062
2063 #ifdef PERFUSE_DEBUG
2064 if (perfuse_diagflags & PDF_FH)
2065 DPRINTF("%s: opc = %p, nodeid = 0x%"PRIx64", fh = 0x%"PRIx64"\n",
2066 __func__, (void *)opc,
2067 PERFUSE_NODE_DATA(opc)->pnd_nodeid, ffi->fh);
2068 #endif
2069
2070 if ((error = xchg_msg(pu, opc, pm,
2071 NO_PAYLOAD_REPLY_LEN, wait_reply)) != 0)
2072 goto out;
2073
2074 /*
2075 * No reply beyond fuse_out_header: nothing to do on success
2076 * just clear the dirty flag
2077 */
2078 pnd->pnd_flags &= ~PND_DIRTY;
2079
2080 #ifdef PERFUSE_DEBUG
2081 if (perfuse_diagflags & PDF_SYNC)
2082 DPRINTF("%s: CLEAR opc = %p, file = \"%s\"\n",
2083 __func__, (void*)opc, perfuse_node_path(ps, opc));
2084 #endif
2085
2086 ps->ps_destroy_msg(pm);
2087 error = 0;
2088
2089 out:
2090 /*
2091 * ENOSYS is not returned to kernel,
2092 */
2093 if (error == ENOSYS)
2094 error = 0;
2095
2096 node_rele(opc);
2097 return error;
2098 }
2099
2100 int
2101 perfuse_node_remove(struct puffs_usermount *pu, puffs_cookie_t opc,
2102 puffs_cookie_t targ, const struct puffs_cn *pcn)
2103 {
2104 struct perfuse_state *ps;
2105 struct perfuse_node_data *pnd;
2106 perfuse_msg_t *pm;
2107 char *path;
2108 const char *name;
2109 size_t len;
2110 int error;
2111
2112 pnd = PERFUSE_NODE_DATA(opc);
2113
2114 if ((pnd->pnd_flags & PND_REMOVED) ||
2115 (PERFUSE_NODE_DATA(targ)->pnd_flags & PND_REMOVED))
2116 return ENOENT;
2117
2118 #ifdef PERFUSE_DEBUG
2119 if (targ == NULL)
2120 DERRX(EX_SOFTWARE, "%s: targ is NULL", __func__);
2121
2122 if (perfuse_diagflags & (PDF_FH|PDF_FILENAME))
2123 DPRINTF("%s: opc = %p, remove opc = %p, file = \"%s\"\n",
2124 __func__, (void *)opc, (void *)targ, pcn->pcn_name);
2125 #endif
2126 node_ref(opc);
2127 node_ref(targ);
2128
2129 /*
2130 * Await for all operations on the deleted node to drain,
2131 * as the filesystem may be confused to have it deleted
2132 * during a getattr
2133 */
2134 while (PERFUSE_NODE_DATA(targ)->pnd_inxchg)
2135 requeue_request(pu, targ, PCQ_AFTERXCHG);
2136
2137 ps = puffs_getspecific(pu);
2138 pnd = PERFUSE_NODE_DATA(opc);
2139 name = pcn->pcn_name;
2140 len = pcn->pcn_namelen + 1;
2141
2142 pm = ps->ps_new_msg(pu, opc, FUSE_UNLINK, len, pcn->pcn_cred);
2143 path = _GET_INPAYLOAD(ps, pm, char *);
2144 (void)strlcpy(path, name, len);
2145
2146 if ((error = xchg_msg(pu, opc, pm, UNSPEC_REPLY_LEN, wait_reply)) != 0)
2147 goto out;
2148
2149 perfuse_cache_flush(targ);
2150 PERFUSE_NODE_DATA(targ)->pnd_flags |= PND_REMOVED;
2151
2152 if (!(PERFUSE_NODE_DATA(targ)->pnd_flags & PND_OPEN))
2153 puffs_setback(puffs_cc_getcc(pu), PUFFS_SETBACK_NOREF_N2);
2154
2155 /*
2156 * The parent directory needs a sync
2157 */
2158 PERFUSE_NODE_DATA(opc)->pnd_flags |= PND_DIRTY;
2159
2160 #ifdef PERFUSE_DEBUG
2161 if (perfuse_diagflags & PDF_FILENAME)
2162 DPRINTF("%s: remove nodeid = 0x%"PRIx64" file = \"%s\"\n",
2163 __func__, PERFUSE_NODE_DATA(targ)->pnd_nodeid,
2164 pcn->pcn_name);
2165 #endif
2166 ps->ps_destroy_msg(pm);
2167 error = 0;
2168
2169 out:
2170 node_rele(opc);
2171 node_rele(targ);
2172 return error;
2173 }
2174
2175 int
2176 perfuse_node_link(struct puffs_usermount *pu, puffs_cookie_t opc,
2177 puffs_cookie_t targ, const struct puffs_cn *pcn)
2178 {
2179 struct perfuse_state *ps;
2180 perfuse_msg_t *pm;
2181 const char *name;
2182 size_t len;
2183 struct puffs_node *pn;
2184 struct fuse_link_in *fli;
2185 int error;
2186
2187 if (PERFUSE_NODE_DATA(opc)->pnd_flags & PND_REMOVED)
2188 return ENOENT;
2189
2190 node_ref(opc);
2191 node_ref(targ);
2192 ps = puffs_getspecific(pu);
2193 pn = (struct puffs_node *)targ;
2194 name = pcn->pcn_name;
2195 len = sizeof(*fli) + pcn->pcn_namelen + 1;
2196
2197 pm = ps->ps_new_msg(pu, opc, FUSE_LINK, len, pcn->pcn_cred);
2198 fli = GET_INPAYLOAD(ps, pm, fuse_link_in);
2199 fli->oldnodeid = PERFUSE_NODE_DATA(pn)->pnd_nodeid;
2200 (void)strlcpy((char *)(void *)(fli + 1), name, len - sizeof(*fli));
2201
2202 if ((error = xchg_msg(pu, opc, pm, UNSPEC_REPLY_LEN, wait_reply)) != 0)
2203 goto out;
2204
2205 ps->ps_destroy_msg(pm);
2206 error = 0;
2207
2208 out:
2209 node_rele(opc);
2210 node_rele(targ);
2211 return error;
2212 }
2213
2214 int
2215 perfuse_node_rename(struct puffs_usermount *pu, puffs_cookie_t opc,
2216 puffs_cookie_t src, const struct puffs_cn *pcn_src,
2217 puffs_cookie_t targ_dir, puffs_cookie_t targ,
2218 const struct puffs_cn *pcn_targ)
2219 {
2220 struct perfuse_state *ps;
2221 struct perfuse_node_data *dstdir_pnd;
2222 perfuse_msg_t *pm;
2223 struct fuse_rename_in *fri;
2224 const char *newname;
2225 const char *oldname;
2226 char *np;
2227 int error;
2228 size_t len;
2229 size_t newname_len;
2230 size_t oldname_len;
2231
2232 if ((PERFUSE_NODE_DATA(opc)->pnd_flags & PND_REMOVED) ||
2233 (PERFUSE_NODE_DATA(src)->pnd_flags & PND_REMOVED) ||
2234 (PERFUSE_NODE_DATA(targ_dir)->pnd_flags & PND_REMOVED))
2235 return ENOENT;
2236
2237 node_ref(opc);
2238 node_ref(src);
2239
2240 /*
2241 * Await for all operations on the deleted node to drain,
2242 * as the filesystem may be confused to have it deleted
2243 * during a getattr
2244 */
2245 if ((struct puffs_node *)targ != NULL) {
2246 node_ref(targ);
2247 while (PERFUSE_NODE_DATA(targ)->pnd_inxchg)
2248 requeue_request(pu, targ, PCQ_AFTERXCHG);
2249 } else {
2250 while (PERFUSE_NODE_DATA(src)->pnd_inxchg)
2251 requeue_request(pu, src, PCQ_AFTERXCHG);
2252 }
2253
2254 ps = puffs_getspecific(pu);
2255 newname = pcn_targ->pcn_name;
2256 newname_len = pcn_targ->pcn_namelen + 1;
2257 oldname = pcn_src->pcn_name;
2258 oldname_len = pcn_src->pcn_namelen + 1;
2259
2260 len = sizeof(*fri) + oldname_len + newname_len;
2261 pm = ps->ps_new_msg(pu, opc, FUSE_RENAME, len, pcn_targ->pcn_cred);
2262 fri = GET_INPAYLOAD(ps, pm, fuse_rename_in);
2263 fri->newdir = PERFUSE_NODE_DATA(targ_dir)->pnd_nodeid;
2264 np = (char *)(void *)(fri + 1);
2265 (void)strlcpy(np, oldname, oldname_len);
2266 np += oldname_len;
2267 (void)strlcpy(np, newname, newname_len);
2268
2269 if ((error = xchg_msg(pu, opc, pm, UNSPEC_REPLY_LEN, wait_reply)) != 0)
2270 goto out;
2271
2272
2273 /*
2274 * Record new parent nodeid
2275 */
2276 dstdir_pnd = PERFUSE_NODE_DATA(targ_dir);
2277 PERFUSE_NODE_DATA(src)->pnd_parent_nodeid = dstdir_pnd->pnd_nodeid;
2278
2279 if (opc != targ_dir)
2280 dstdir_pnd->pnd_flags |= PND_DIRTY;
2281
2282 if (strcmp(newname, "..") != 0)
2283 (void)strlcpy(PERFUSE_NODE_DATA(src)->pnd_name,
2284 newname, MAXPATHLEN);
2285 else
2286 PERFUSE_NODE_DATA(src)->pnd_name[0] = 0; /* forget name */
2287
2288 PERFUSE_NODE_DATA(opc)->pnd_flags |= PND_DIRTY;
2289
2290 if ((struct puffs_node *)targ != NULL) {
2291 perfuse_cache_flush(targ);
2292 PERFUSE_NODE_DATA(targ)->pnd_flags |= PND_REMOVED;
2293 }
2294
2295 #ifdef PERFUSE_DEBUG
2296 if (perfuse_diagflags & PDF_FILENAME)
2297 DPRINTF("%s: nodeid = 0x%"PRIx64" file = \"%s\" renamed \"%s\" "
2298 "nodeid = 0x%"PRIx64" -> nodeid = 0x%"PRIx64" \"%s\"\n",
2299 __func__, PERFUSE_NODE_DATA(src)->pnd_nodeid,
2300 pcn_src->pcn_name, pcn_targ->pcn_name,
2301 PERFUSE_NODE_DATA(opc)->pnd_nodeid,
2302 PERFUSE_NODE_DATA(targ_dir)->pnd_nodeid,
2303 perfuse_node_path(ps, targ_dir));
2304 #endif
2305
2306 ps->ps_destroy_msg(pm);
2307 error = 0;
2308
2309 out:
2310 node_rele(opc);
2311 node_rele(src);
2312 if ((struct puffs_node *)targ != NULL)
2313 node_rele(targ);
2314
2315 return error;
2316 }
2317
2318 int
2319 perfuse_node_mkdir(struct puffs_usermount *pu, puffs_cookie_t opc,
2320 struct puffs_newinfo *pni, const struct puffs_cn *pcn,
2321 const struct vattr *vap)
2322 {
2323 struct perfuse_state *ps;
2324 perfuse_msg_t *pm;
2325 struct fuse_mkdir_in *fmi;
2326 const char *path;
2327 size_t len;
2328 int error;
2329
2330 if (PERFUSE_NODE_DATA(opc)->pnd_flags & PND_REMOVED)
2331 return ENOENT;
2332
2333 node_ref(opc);
2334 ps = puffs_getspecific(pu);
2335 path = pcn->pcn_name;
2336 len = sizeof(*fmi) + pcn->pcn_namelen + 1;
2337
2338 pm = ps->ps_new_msg(pu, opc, FUSE_MKDIR, len, pcn->pcn_cred);
2339 fmi = GET_INPAYLOAD(ps, pm, fuse_mkdir_in);
2340 fmi->mode = vap->va_mode;
2341 fmi->umask = 0; /* Seems unused by libfuse? */
2342 (void)strlcpy((char *)(void *)(fmi + 1), path, len - sizeof(*fmi));
2343
2344 error = node_mk_common(pu, opc, pni, pcn, pm);
2345
2346 node_rele(opc);
2347 return error;
2348 }
2349
2350
2351 int
2352 perfuse_node_rmdir(struct puffs_usermount *pu, puffs_cookie_t opc,
2353 puffs_cookie_t targ, const struct puffs_cn *pcn)
2354 {
2355 struct perfuse_state *ps;
2356 struct perfuse_node_data *pnd;
2357 perfuse_msg_t *pm;
2358 char *path;
2359 const char *name;
2360 size_t len;
2361 int error;
2362
2363 pnd = PERFUSE_NODE_DATA(opc);
2364
2365 if ((pnd->pnd_flags & PND_REMOVED) ||
2366 (PERFUSE_NODE_DATA(targ)->pnd_flags & PND_REMOVED))
2367 return ENOENT;
2368
2369 /*
2370 * Attempt to rmdir dir/.. shoud raise ENOTEMPTY
2371 */
2372 if (PERFUSE_NODE_DATA(targ)->pnd_nodeid == pnd->pnd_parent_nodeid)
2373 return ENOTEMPTY;
2374
2375 node_ref(opc);
2376 node_ref(targ);
2377
2378 /*
2379 * Await for all operations on the deleted node to drain,
2380 * as the filesystem may be confused to have it deleted
2381 * during a getattr
2382 */
2383 while (PERFUSE_NODE_DATA(targ)->pnd_inxchg)
2384 requeue_request(pu, targ, PCQ_AFTERXCHG);
2385
2386 ps = puffs_getspecific(pu);
2387 name = pcn->pcn_name;
2388 len = pcn->pcn_namelen + 1;
2389
2390 pm = ps->ps_new_msg(pu, opc, FUSE_RMDIR, len, pcn->pcn_cred);
2391 path = _GET_INPAYLOAD(ps, pm, char *);
2392 (void)strlcpy(path, name, len);
2393
2394 if ((error = xchg_msg(pu, opc, pm, UNSPEC_REPLY_LEN, wait_reply)) != 0)
2395 goto out;
2396
2397 perfuse_cache_flush(targ);
2398 PERFUSE_NODE_DATA(targ)->pnd_flags |= PND_REMOVED;
2399
2400 if (!(PERFUSE_NODE_DATA(targ)->pnd_flags & PND_OPEN))
2401 puffs_setback(puffs_cc_getcc(pu), PUFFS_SETBACK_NOREF_N2);
2402
2403 /*
2404 * The parent directory needs a sync
2405 */
2406 PERFUSE_NODE_DATA(opc)->pnd_flags |= PND_DIRTY;
2407
2408 #ifdef PERFUSE_DEBUG
2409 if (perfuse_diagflags & PDF_FILENAME)
2410 DPRINTF("%s: remove nodeid = 0x%"PRIx64" file = \"%s\"\n",
2411 __func__, PERFUSE_NODE_DATA(targ)->pnd_nodeid,
2412 perfuse_node_path(ps, targ));
2413 #endif
2414 ps->ps_destroy_msg(pm);
2415 error = 0;
2416
2417 out:
2418 node_rele(opc);
2419 node_rele(targ);
2420 return error;
2421 }
2422
2423 /* vap is unused */
2424 /* ARGSUSED4 */
2425 int
2426 perfuse_node_symlink(struct puffs_usermount *pu, puffs_cookie_t opc,
2427 struct puffs_newinfo *pni, const struct puffs_cn *pcn_src,
2428 const struct vattr *vap, const char *link_target)
2429 {
2430 struct perfuse_state *ps;
2431 perfuse_msg_t *pm;
2432 char *np;
2433 const char *path;
2434 size_t path_len;
2435 size_t linkname_len;
2436 size_t len;
2437 int error;
2438
2439 if (PERFUSE_NODE_DATA(opc)->pnd_flags & PND_REMOVED)
2440 return ENOENT;
2441
2442 node_ref(opc);
2443 ps = puffs_getspecific(pu);
2444 path = pcn_src->pcn_name;
2445 path_len = pcn_src->pcn_namelen + 1;
2446 linkname_len = strlen(link_target) + 1;
2447 len = path_len + linkname_len;
2448
2449 pm = ps->ps_new_msg(pu, opc, FUSE_SYMLINK, len, pcn_src->pcn_cred);
2450 np = _GET_INPAYLOAD(ps, pm, char *);
2451 (void)strlcpy(np, path, path_len);
2452 np += path_len;
2453 (void)strlcpy(np, link_target, linkname_len);
2454
2455 error = node_mk_common(pu, opc, pni, pcn_src, pm);
2456
2457 node_rele(opc);
2458 return error;
2459 }
2460
2461 /* ARGSUSED4 */
2462 int
2463 perfuse_node_readdir(struct puffs_usermount *pu, puffs_cookie_t opc,
2464 struct dirent *dent, off_t *readoff, size_t *reslen,
2465 const struct puffs_cred *pcr, int *eofflag, off_t *cookies,
2466 size_t *ncookies)
2467 {
2468 perfuse_msg_t *pm;
2469 uint64_t fh;
2470 struct perfuse_state *ps;
2471 struct perfuse_node_data *pnd;
2472 struct fuse_read_in *fri;
2473 struct fuse_out_header *foh;
2474 struct fuse_dirent *fd;
2475 size_t foh_len;
2476 int error;
2477 size_t fd_maxlen;
2478
2479 error = 0;
2480 node_ref(opc);
2481 ps = puffs_getspecific(pu);
2482
2483 /*
2484 * readdir state is kept at node level, and several readdir
2485 * requests can be issued at the same time on the same node.
2486 * We need to queue requests so that only one is in readdir
2487 * code at the same time.
2488 */
2489 pnd = PERFUSE_NODE_DATA(opc);
2490 while (pnd->pnd_flags & PND_INREADDIR)
2491 requeue_request(pu, opc, PCQ_READDIR);
2492 pnd->pnd_flags |= PND_INREADDIR;
2493
2494 #ifdef PERFUSE_DEBUG
2495 if (perfuse_diagflags & PDF_READDIR)
2496 DPRINTF("%s: READDIR opc = %p enter critical section\n",
2497 __func__, (void *)opc);
2498 #endif
2499 /*
2500 * Re-initialize pnd->pnd_fd_cookie on the first readdir for a node
2501 */
2502 if (*readoff == 0)
2503 pnd->pnd_fd_cookie = 0;
2504
2505 /*
2506 * Do we already have the data bufered?
2507 */
2508 if (pnd->pnd_dirent != NULL)
2509 goto out;
2510 pnd->pnd_dirent_len = 0;
2511
2512 /*
2513 * It seems NetBSD can call readdir without open first
2514 * libfuse will crash if it is done that way, hence open first.
2515 */
2516 if (!(pnd->pnd_flags & PND_OPEN)) {
2517 if ((error = perfuse_node_open(pu, opc, FREAD, pcr)) != 0)
2518 goto out;
2519 }
2520
2521 fh = perfuse_get_fh(opc, FREAD);
2522
2523 #ifdef PERFUSE_DEBUG
2524 if (perfuse_diagflags & PDF_FH)
2525 DPRINTF("%s: opc = %p, nodeid = 0x%"PRIx64", "
2526 "rfh = 0x%"PRIx64"\n", __func__, (void *)opc,
2527 PERFUSE_NODE_DATA(opc)->pnd_nodeid, fh);
2528 #endif
2529
2530 pnd->pnd_all_fd = NULL;
2531 pnd->pnd_all_fd_len = 0;
2532 fd_maxlen = ps->ps_max_readahead - sizeof(*foh);
2533
2534 do {
2535 size_t fd_len;
2536 char *afdp;
2537
2538 pm = ps->ps_new_msg(pu, opc, FUSE_READDIR, sizeof(*fri), pcr);
2539
2540 /*
2541 * read_flags, lock_owner and flags are unused in libfuse
2542 */
2543 fri = GET_INPAYLOAD(ps, pm, fuse_read_in);
2544 fri->fh = fh;
2545 fri->offset = pnd->pnd_fd_cookie;
2546 fri->size = (uint32_t)fd_maxlen;
2547 fri->read_flags = 0;
2548 fri->lock_owner = 0;
2549 fri->flags = 0;
2550
2551 if ((error = xchg_msg(pu, opc, pm,
2552 UNSPEC_REPLY_LEN, wait_reply)) != 0)
2553 goto out;
2554
2555 /*
2556 * There are many puffs_framebufs calls later,
2557 * therefore foh will not be valid for a long time.
2558 * Just get the length and forget it.
2559 */
2560 foh = GET_OUTHDR(ps, pm);
2561 foh_len = foh->len;
2562
2563 /*
2564 * Empty read: we reached the end of the buffer.
2565 */
2566 if (foh_len == sizeof(*foh)) {
2567 ps->ps_destroy_msg(pm);
2568 *eofflag = 1;
2569 break;
2570 }
2571
2572 /*
2573 * Check for corrupted message.
2574 */
2575 if (foh_len < sizeof(*foh) + sizeof(*fd)) {
2576 ps->ps_destroy_msg(pm);
2577 DWARNX("readdir reply too short");
2578 error = EIO;
2579 goto out;
2580 }
2581
2582
2583 fd = GET_OUTPAYLOAD(ps, pm, fuse_dirent);
2584 fd_len = foh_len - sizeof(*foh);
2585
2586 pnd->pnd_all_fd = realloc(pnd->pnd_all_fd,
2587 pnd->pnd_all_fd_len + fd_len);
2588 if (pnd->pnd_all_fd == NULL)
2589 DERR(EX_OSERR, "%s: malloc failed", __func__);
2590
2591 afdp = (char *)(void *)pnd->pnd_all_fd + pnd->pnd_all_fd_len;
2592 (void)memcpy(afdp, fd, fd_len);
2593
2594 pnd->pnd_all_fd_len += fd_len;
2595
2596 /*
2597 * The fd->off field is used as a cookie for
2598 * resuming the next readdir() where this one was left.
2599 */
2600 pnd->pnd_fd_cookie = readdir_last_cookie(fd, fd_len);
2601
2602 ps->ps_destroy_msg(pm);
2603 } while (1 /* CONSTCOND */);
2604
2605 if (pnd->pnd_all_fd != NULL) {
2606 if (fuse_to_dirent(pu, opc, pnd->pnd_all_fd,
2607 pnd->pnd_all_fd_len) == -1)
2608 error = EIO;
2609 }
2610
2611 out:
2612 if (pnd->pnd_all_fd != NULL) {
2613 free(pnd->pnd_all_fd);
2614 pnd->pnd_all_fd = NULL;
2615 pnd->pnd_all_fd_len = 0;
2616 }
2617
2618 if (error == 0)
2619 readdir_buffered(opc, dent, readoff, reslen);
2620
2621 /*
2622 * Schedule queued readdir requests
2623 */
2624 pnd->pnd_flags &= ~PND_INREADDIR;
2625 (void)dequeue_requests(opc, PCQ_READDIR, DEQUEUE_ALL);
2626
2627 #ifdef PERFUSE_DEBUG
2628 if (perfuse_diagflags & PDF_READDIR)
2629 DPRINTF("%s: READDIR opc = %p exit critical section\n",
2630 __func__, (void *)opc);
2631 #endif
2632
2633 node_rele(opc);
2634 return error;
2635 }
2636
2637 int
2638 perfuse_node_readlink(struct puffs_usermount *pu, puffs_cookie_t opc,
2639 const struct puffs_cred *pcr, char *linkname, size_t *linklen)
2640 {
2641 struct perfuse_state *ps;
2642 perfuse_msg_t *pm;
2643 int error;
2644 size_t len;
2645 struct fuse_out_header *foh;
2646
2647 if (PERFUSE_NODE_DATA(opc)->pnd_flags & PND_REMOVED)
2648 return ENOENT;
2649
2650 node_ref(opc);
2651 ps = puffs_getspecific(pu);
2652
2653 pm = ps->ps_new_msg(pu, opc, FUSE_READLINK, 0, pcr);
2654
2655 if ((error = xchg_msg(pu, opc, pm, UNSPEC_REPLY_LEN, wait_reply)) != 0)
2656 goto out;
2657
2658 foh = GET_OUTHDR(ps, pm);
2659 len = foh->len - sizeof(*foh);
2660 if (len > *linklen)
2661 DERRX(EX_PROTOCOL, "path len = %zd too long", len);
2662 if (len == 0)
2663 DERRX(EX_PROTOCOL, "path len = %zd too short", len);
2664
2665 (void)memcpy(linkname, _GET_OUTPAYLOAD(ps, pm, char *), len);
2666
2667 /*
2668 * FUSE filesystems return a NUL terminated string, we
2669 * do not want the trailing \0
2670 */
2671 while (len > 0 && linkname[len - 1] == '\0')
2672 len--;
2673
2674 *linklen = len;
2675
2676 ps->ps_destroy_msg(pm);
2677 error = 0;
2678
2679 out:
2680 node_rele(opc);
2681 return error;
2682 }
2683
2684 int
2685 perfuse_node_reclaim(struct puffs_usermount *pu, puffs_cookie_t opc)
2686 {
2687 struct perfuse_state *ps;
2688 perfuse_msg_t *pm;
2689 struct perfuse_node_data *pnd;
2690 struct fuse_forget_in *ffi;
2691 int nlookup;
2692 struct timespec now;
2693
2694 if (opc == 0)
2695 return 0;
2696
2697 ps = puffs_getspecific(pu);
2698 pnd = PERFUSE_NODE_DATA(opc);
2699
2700 /*
2701 * Never forget the root.
2702 */
2703 if (pnd->pnd_nodeid == FUSE_ROOT_ID)
2704 return 0;
2705
2706 /*
2707 * There is a race condition between reclaim and lookup.
2708 * When looking up an already known node, the kernel cannot
2709 * hold a reference on the result until it gets the PUFFS
2710 * reply. It mayy therefore reclaim the node after the
2711 * userland looked it up, and before it gets the reply.
2712 * On rely, the kernel re-creates the node, but at that
2713 * time the node has been reclaimed in userland.
2714 *
2715 * In order to avoid this, we refuse reclaiming nodes that
2716 * are too young since the last lookup - and that we do
2717 * not have removed on our own, of course.
2718 */
2719 if (clock_gettime(CLOCK_REALTIME, &now) != 0)
2720 DERR(EX_OSERR, "clock_gettime failed");
2721
2722 if (timespeccmp(&pnd->pnd_cn_expire, &now, >) &&
2723 !(pnd->pnd_flags & PND_REMOVED)) {
2724 if (!(pnd->pnd_flags & PND_NODELEAK)) {
2725 ps->ps_nodeleakcount++;
2726 pnd->pnd_flags |= PND_NODELEAK;
2727 }
2728 DWARNX("possible leaked node:: opc = %p \"%s\"",
2729 opc, pnd->pnd_name);
2730 return 0;
2731 }
2732
2733 node_ref(opc);
2734 pnd->pnd_flags |= PND_RECLAIMED;
2735 pnd->pnd_puffs_nlookup--;
2736 nlookup = pnd->pnd_puffs_nlookup;
2737
2738 #ifdef PERFUSE_DEBUG
2739 if (perfuse_diagflags & PDF_RECLAIM)
2740 DPRINTF("%s (nodeid %"PRId64") reclaimed\n",
2741 perfuse_node_path(ps, opc), pnd->pnd_nodeid);
2742 #endif
2743
2744 #ifdef PERFUSE_DEBUG
2745 if (perfuse_diagflags & PDF_RECLAIM)
2746 DPRINTF("%s (nodeid %"PRId64") is %sreclaimed, nlookup = %d "
2747 "%s%s%s%s, pending ops:%s%s%s\n",
2748 perfuse_node_path(ps, opc), pnd->pnd_nodeid,
2749 pnd->pnd_flags & PND_RECLAIMED ? "" : "not ",
2750 pnd->pnd_puffs_nlookup,
2751 pnd->pnd_flags & PND_OPEN ? "open " : "not open",
2752 pnd->pnd_flags & PND_RFH ? "r" : "",
2753 pnd->pnd_flags & PND_WFH ? "w" : "",
2754 pnd->pnd_flags & PND_BUSY ? "" : " none",
2755 pnd->pnd_flags & PND_INREADDIR ? " readdir" : "",
2756 pnd->pnd_flags & PND_INWRITE ? " write" : "",
2757 pnd->pnd_flags & PND_INOPEN ? " open" : "");
2758 #endif
2759 /*
2760 * Make sure it is not looked up again
2761 */
2762 if (!(pnd->pnd_flags & PND_REMOVED))
2763 perfuse_cache_flush(opc);
2764
2765 /*
2766 * Purge any activity on the node, while checking
2767 * that it remains eligible for a reclaim.
2768 */
2769 while (pnd->pnd_ref > 1)
2770 requeue_request(pu, opc, PCQ_REF);
2771
2772 /*
2773 * reclaim cancel?
2774 */
2775 if (pnd->pnd_puffs_nlookup > nlookup) {
2776 pnd->pnd_flags &= ~PND_RECLAIMED;
2777 perfuse_node_cache(ps, opc);
2778 node_rele(opc);
2779 return 0;
2780 }
2781
2782
2783 #ifdef PERFUSE_DEBUG
2784 if ((pnd->pnd_flags & PND_OPEN) ||
2785 !TAILQ_EMPTY(&pnd->pnd_pcq))
2786 DERRX(EX_SOFTWARE, "%s: opc = %p \"%s\": still open",
2787 __func__, opc, pnd->pnd_name);
2788
2789 if ((pnd->pnd_flags & PND_BUSY) ||
2790 !TAILQ_EMPTY(&pnd->pnd_pcq))
2791 DERRX(EX_SOFTWARE, "%s: opc = %p: queued operations",
2792 __func__, opc);
2793
2794 if (pnd->pnd_inxchg != 0)
2795 DERRX(EX_SOFTWARE, "%s: opc = %p: ongoing operations",
2796 __func__, opc);
2797 #endif
2798
2799 /*
2800 * Send the FORGET message
2801 *
2802 * ps_new_msg() is called with NULL creds, which will
2803 * be interpreted as FUSE superuser. This is obviously
2804 * fine since we operate with kernel creds here.
2805 */
2806 pm = ps->ps_new_msg(pu, opc, FUSE_FORGET,
2807 sizeof(*ffi), NULL);
2808 ffi = GET_INPAYLOAD(ps, pm, fuse_forget_in);
2809 ffi->nlookup = pnd->pnd_fuse_nlookup;
2810
2811 /*
2812 * No reply is expected, pm is freed in xchg_msg
2813 */
2814 (void)xchg_msg(pu, opc, pm, UNSPEC_REPLY_LEN, no_reply);
2815
2816 perfuse_destroy_pn(pu, opc);
2817
2818 return 0;
2819 }
2820
2821 int
2822 perfuse_node_inactive(struct puffs_usermount *pu, puffs_cookie_t opc)
2823 {
2824 struct perfuse_node_data *pnd;
2825 int error;
2826
2827 if (opc == 0)
2828 return 0;
2829
2830 pnd = PERFUSE_NODE_DATA(opc);
2831 if (!(pnd->pnd_flags & (PND_OPEN|PND_REMOVED)))
2832 return 0;
2833
2834 node_ref(opc);
2835
2836 /*
2837 * Make sure all operation are finished
2838 * There can be an ongoing write. Other
2839 * operation wait for all data before
2840 * the close/inactive.
2841 */
2842 while (pnd->pnd_flags & PND_INWRITE)
2843 requeue_request(pu, opc, PCQ_AFTERWRITE);
2844
2845 /*
2846 * The inactive operation may be cancelled,
2847 * If no open is in progress, set PND_INOPEN
2848 * so that a new open will be queued.
2849 */
2850 if (pnd->pnd_flags & PND_INOPEN)
2851 goto out;
2852
2853 pnd->pnd_flags |= PND_INOPEN;
2854
2855 /*
2856 * Sync data
2857 */
2858 if (pnd->pnd_flags & PND_DIRTY) {
2859 if ((error = perfuse_node_fsync(pu, opc, NULL, 0, 0, 0)) != 0)
2860 DWARN("%s: perfuse_node_fsync failed error = %d",
2861 __func__, error);
2862 }
2863
2864
2865 /*
2866 * Close handles
2867 */
2868 if (pnd->pnd_flags & PND_WFH) {
2869 if ((error = perfuse_node_close_common(pu, opc, FWRITE)) != 0)
2870 DWARN("%s: close write FH failed error = %d",
2871 __func__, error);
2872 }
2873
2874 if (pnd->pnd_flags & PND_RFH) {
2875 if ((error = perfuse_node_close_common(pu, opc, FREAD)) != 0)
2876 DWARN("%s: close read FH failed error = %d",
2877 __func__, error);
2878 }
2879
2880 /*
2881 * This will cause a reclaim to be sent
2882 */
2883 if (pnd->pnd_flags & PND_REMOVED)
2884 puffs_setback(puffs_cc_getcc(pu), PUFFS_SETBACK_NOREF_N1);
2885
2886 /*
2887 * Schedule awaiting operations
2888 */
2889 pnd->pnd_flags &= ~PND_INOPEN;
2890 (void)dequeue_requests(opc, PCQ_OPEN, DEQUEUE_ALL);
2891
2892 /*
2893 * errors are ignored, since the kernel ignores the return code.
2894 */
2895 out:
2896 node_rele(opc);
2897 return 0;
2898 }
2899
2900
2901 /* ARGSUSED0 */
2902 int
2903 perfuse_node_print(struct puffs_usermount *pu, puffs_cookie_t opc)
2904 {
2905 DERRX(EX_SOFTWARE, "%s: UNIMPLEMENTED (FATAL)", __func__);
2906 return 0;
2907 }
2908
2909 int
2910 perfuse_node_pathconf(struct puffs_usermount *pu, puffs_cookie_t opc,
2911 int name, register_t *retval)
2912 {
2913 perfuse_msg_t *pm;
2914 struct perfuse_state *ps;
2915 struct fuse_statfs_out *fso;
2916 int error = 0;
2917
2918 /*
2919 * Static values copied from UFS
2920 * in src/sys/ufs/ufs/ufs_vnops.c
2921 */
2922 switch (name) {
2923 case _PC_LINK_MAX:
2924 *retval = LINK_MAX;
2925 break;
2926 case _PC_PATH_MAX:
2927 *retval = PATH_MAX;
2928 break;
2929 case _PC_PIPE_BUF:
2930 *retval = PIPE_BUF;
2931 break;
2932 case _PC_CHOWN_RESTRICTED:
2933 *retval = 1;
2934 break;
2935 case _PC_NO_TRUNC:
2936 *retval = 1;
2937 break;
2938 case _PC_SYNC_IO:
2939 *retval = 1;
2940 break;
2941 case _PC_FILESIZEBITS:
2942 *retval = 42;
2943 break;
2944 case _PC_SYMLINK_MAX:
2945 *retval = MAXPATHLEN;
2946 break;
2947 case _PC_2_SYMLINKS:
2948 *retval = 1;
2949 break;
2950 case _PC_NAME_MAX:
2951 ps = puffs_getspecific(pu);
2952 pm = ps->ps_new_msg(pu, opc, FUSE_STATFS, 0, NULL);
2953
2954 error = xchg_msg(pu, opc, pm, sizeof(*fso), wait_reply);
2955 if (error != 0)
2956 return error;
2957
2958 fso = GET_OUTPAYLOAD(ps, pm, fuse_statfs_out);
2959 *retval = fso->st.namelen;
2960
2961 ps->ps_destroy_msg(pm);
2962
2963 break;
2964 default:
2965 DWARN("Unimplemented pathconf for name = %d", name);
2966 error = ENOSYS;
2967 break;
2968 }
2969
2970 return error;
2971 }
2972
2973 int
2974 perfuse_node_advlock(struct puffs_usermount *pu, puffs_cookie_t opc,
2975 void *id, int op, struct flock *fl, int flags)
2976 {
2977 struct perfuse_state *ps;
2978 int fop;
2979 perfuse_msg_t *pm;
2980 uint64_t fh;
2981 struct fuse_lk_in *fli;
2982 struct fuse_out_header *foh;
2983 struct fuse_lk_out *flo;
2984 uint32_t owner;
2985 size_t len;
2986 int error;
2987
2988 node_ref(opc);
2989
2990 /*
2991 * Make sure we do have a filehandle, as the FUSE filesystem
2992 * expect one. E.g.: if we provide none, GlusterFS logs an error
2993 * "0-glusterfs-fuse: xl is NULL"
2994 *
2995 * We need the read file handle if the file is open read only,
2996 * in order to support shared locks on read-only files.
2997 * NB: The kernel always sends advlock for read-only
2998 * files at exit time when the process used lock, see
2999 * sys_exit -> exit1 -> fd_free -> fd_close -> VOP_ADVLOCK
3000 */
3001 if ((fh = perfuse_get_fh(opc, FREAD)) == FUSE_UNKNOWN_FH) {
3002 error = EBADF;
3003 goto out;
3004 }
3005
3006 ps = puffs_getspecific(pu);
3007
3008 if (op == F_GETLK)
3009 fop = FUSE_GETLK;
3010 else
3011 fop = (flags & F_WAIT) ? FUSE_SETLKW : FUSE_SETLK;
3012
3013 /*
3014 * XXX ps_new_msg() is called with NULL creds, which will
3015 * be interpreted as FUSE superuser. We have no way to
3016 * know the requesting process' credential, but since advlock()
3017 * is supposed to operate on a file that has been open(),
3018 * permission should have already been checked at open() time.
3019 */
3020 pm = ps->ps_new_msg(pu, opc, fop, sizeof(*fli), NULL);
3021 fli = GET_INPAYLOAD(ps, pm, fuse_lk_in);
3022 fli->fh = fh;
3023 fli->owner = (uint64_t)(vaddr_t)id;
3024 fli->lk.start = fl->l_start;
3025 fli->lk.end = fl->l_start + fl->l_len;
3026 fli->lk.type = fl->l_type;
3027 fli->lk.pid = fl->l_pid;
3028 fli->lk_flags = (flags & F_FLOCK) ? FUSE_LK_FLOCK : 0;
3029
3030 owner = (uint32_t)(vaddr_t)id;
3031
3032 #ifdef PERFUSE_DEBUG
3033 if (perfuse_diagflags & PDF_FH)
3034 DPRINTF("%s: opc = %p, nodeid = 0x%"PRIx64", fh = 0x%"PRIx64"\n",
3035 __func__, (void *)opc,
3036 PERFUSE_NODE_DATA(opc)->pnd_nodeid, fli->fh);
3037 #endif
3038
3039 if ((error = xchg_msg(pu, opc, pm, UNSPEC_REPLY_LEN, wait_reply)) != 0)
3040 goto out;
3041
3042 foh = GET_OUTHDR(ps, pm);
3043 len = foh->len - sizeof(*foh);
3044
3045 /*
3046 * Save or clear the lock
3047 */
3048 switch (op) {
3049 case F_GETLK:
3050 if (len != sizeof(*flo))
3051 DERRX(EX_SOFTWARE,
3052 "%s: Unexpected lock reply len %zd",
3053 __func__, len);
3054
3055 flo = GET_OUTPAYLOAD(ps, pm, fuse_lk_out);
3056 fl->l_start = flo->lk.start;
3057 fl->l_len = flo->lk.end - flo->lk.start;
3058 fl->l_pid = flo->lk.pid;
3059 fl->l_type = flo->lk.type;
3060 fl->l_whence = SEEK_SET; /* libfuse hardcodes it */
3061
3062 PERFUSE_NODE_DATA(opc)->pnd_lock_owner = flo->lk.pid;
3063 break;
3064 case F_UNLCK:
3065 owner = 0;
3066 /* FALLTHROUGH */
3067 case F_SETLK:
3068 /* FALLTHROUGH */
3069 case F_SETLKW:
3070 if (error != 0)
3071 PERFUSE_NODE_DATA(opc)->pnd_lock_owner = owner;
3072
3073 if (len != 0)
3074 DERRX(EX_SOFTWARE,
3075 "%s: Unexpected unlock reply len %zd",
3076 __func__, len);
3077
3078 break;
3079 default:
3080 DERRX(EX_SOFTWARE, "%s: Unexpected op %d", __func__, op);
3081 break;
3082 }
3083
3084 ps->ps_destroy_msg(pm);
3085 error = 0;
3086
3087 out:
3088 node_rele(opc);
3089 return error;
3090 }
3091
3092 int
3093 perfuse_node_read(struct puffs_usermount *pu, puffs_cookie_t opc, uint8_t *buf,
3094 off_t offset, size_t *resid, const struct puffs_cred *pcr, int ioflag)
3095 {
3096 struct perfuse_state *ps;
3097 struct perfuse_node_data *pnd;
3098 const struct vattr *vap;
3099 perfuse_msg_t *pm;
3100 struct fuse_read_in *fri;
3101 struct fuse_out_header *foh;
3102 size_t readen;
3103 int error;
3104
3105 ps = puffs_getspecific(pu);
3106 pnd = PERFUSE_NODE_DATA(opc);
3107 vap = puffs_pn_getvap((struct puffs_node *)opc);
3108
3109 /*
3110 * NetBSD turns that into a getdents(2) output
3111 * We just do a EISDIR as this feature is of little use.
3112 */
3113 if (vap->va_type == VDIR)
3114 return EISDIR;
3115
3116 do {
3117 size_t max_read;
3118
3119 max_read = ps->ps_max_readahead - sizeof(*foh);
3120 /*
3121 * flags may be set to FUSE_READ_LOCKOWNER
3122 * if lock_owner is provided.
3123 */
3124 pm = ps->ps_new_msg(pu, opc, FUSE_READ, sizeof(*fri), pcr);
3125 fri = GET_INPAYLOAD(ps, pm, fuse_read_in);
3126 fri->fh = perfuse_get_fh(opc, FREAD);
3127 fri->offset = offset;
3128 fri->size = (uint32_t)MIN(*resid, max_read);
3129 fri->read_flags = 0; /* XXX Unused by libfuse? */
3130 fri->lock_owner = pnd->pnd_lock_owner;
3131 fri->flags = 0;
3132 fri->flags |= (fri->lock_owner != 0) ? FUSE_READ_LOCKOWNER : 0;
3133
3134 #ifdef PERFUSE_DEBUG
3135 if (perfuse_diagflags & PDF_FH)
3136 DPRINTF("%s: opc = %p, nodeid = 0x%"PRIx64", fh = 0x%"PRIx64"\n",
3137 __func__, (void *)opc, pnd->pnd_nodeid, fri->fh);
3138 #endif
3139 error = xchg_msg(pu, opc, pm, UNSPEC_REPLY_LEN, wait_reply);
3140 if (error != 0)
3141 return error;
3142
3143 foh = GET_OUTHDR(ps, pm);
3144 readen = foh->len - sizeof(*foh);
3145
3146 #ifdef PERFUSE_DEBUG
3147 if (readen > *resid)
3148 DERRX(EX_SOFTWARE, "%s: Unexpected big read %zd",
3149 __func__, readen);
3150 #endif
3151
3152 (void)memcpy(buf, _GET_OUTPAYLOAD(ps, pm, char *), readen);
3153
3154 buf += readen;
3155 offset += readen;
3156 *resid -= readen;
3157
3158 ps->ps_destroy_msg(pm);
3159 } while ((*resid != 0) && (readen != 0));
3160
3161 if (ioflag & (IO_SYNC|IO_DSYNC))
3162 ps->ps_syncreads++;
3163 else
3164 ps->ps_asyncreads++;
3165
3166 return 0;
3167 }
3168
3169 int
3170 perfuse_node_write(struct puffs_usermount *pu, puffs_cookie_t opc,
3171 uint8_t *buf, off_t offset, size_t *resid,
3172 const struct puffs_cred *pcr, int ioflag)
3173 {
3174 return perfuse_node_write2(pu, opc, buf, offset, resid, pcr, ioflag, 0);
3175 }
3176
3177 /* ARGSUSED7 */
3178 int
3179 perfuse_node_write2(struct puffs_usermount *pu, puffs_cookie_t opc,
3180 uint8_t *buf, off_t offset, size_t *resid,
3181 const struct puffs_cred *pcr, int ioflag, int xflag)
3182 {
3183 struct perfuse_state *ps;
3184 struct perfuse_node_data *pnd;
3185 struct vattr *vap;
3186 perfuse_msg_t *pm;
3187 struct fuse_write_in *fwi;
3188 struct fuse_write_out *fwo;
3189 size_t data_len;
3190 size_t payload_len;
3191 size_t written;
3192 int inresize;
3193 int error;
3194
3195 ps = puffs_getspecific(pu);
3196 pnd = PERFUSE_NODE_DATA(opc);
3197 vap = puffs_pn_getvap((struct puffs_node *)opc);
3198 written = 0;
3199 inresize = 0;
3200 error = 0;
3201
3202 if (vap->va_type == VDIR)
3203 return EISDIR;
3204
3205 node_ref(opc);
3206
3207 /*
3208 * We need to queue write requests in order to avoid
3209 * dequeueing PCQ_AFTERWRITE when there are pending writes.
3210 */
3211 while (pnd->pnd_flags & PND_INWRITE)
3212 requeue_request(pu, opc, PCQ_WRITE);
3213 pnd->pnd_flags |= PND_INWRITE;
3214
3215 /*
3216 * append flag: re-read the file size so that
3217 * we get the latest value.
3218 */
3219 if (ioflag & PUFFS_IO_APPEND) {
3220 if ((error = perfuse_node_getattr(pu, opc, vap, pcr)) != 0)
3221 goto out;
3222
3223 offset = vap->va_size;
3224 }
3225
3226 /*
3227 * Serialize size access, see comment in perfuse_node_setattr().
3228 */
3229 if ((u_quad_t)offset + *resid > vap->va_size) {
3230 while (pnd->pnd_flags & PND_INRESIZE)
3231 requeue_request(pu, opc, PCQ_RESIZE);
3232 pnd->pnd_flags |= PND_INRESIZE;
3233 inresize = 1;
3234 }
3235
3236 #ifdef PERFUSE_DEBUG
3237 if (perfuse_diagflags & PDF_RESIZE)
3238 DPRINTF(">> %s %p %" PRIu64 "\n", __func__,
3239 (void *)opc, vap->va_size);
3240 #endif
3241
3242 do {
3243 size_t max_write;
3244 /*
3245 * There is a writepage flag when data
3246 * is aligned to page size. Use it for
3247 * everything but the data after the last
3248 * page boundary.
3249 */
3250 max_write = ps->ps_max_write - sizeof(*fwi);
3251
3252 data_len = MIN(*resid, max_write);
3253 if (data_len > (size_t)sysconf(_SC_PAGESIZE))
3254 data_len = data_len & ~(sysconf(_SC_PAGESIZE) - 1);
3255
3256 payload_len = data_len + sizeof(*fwi);
3257
3258 /*
3259 * flags may be set to FUSE_WRITE_CACHE (XXX usage?)
3260 * or FUSE_WRITE_LOCKOWNER, if lock_owner is provided.
3261 * write_flags is set to 1 for writepage.
3262 */
3263 pm = ps->ps_new_msg(pu, opc, FUSE_WRITE, payload_len, pcr);
3264 fwi = GET_INPAYLOAD(ps, pm, fuse_write_in);
3265 fwi->fh = perfuse_get_fh(opc, FWRITE);
3266 fwi->offset = offset;
3267 fwi->size = (uint32_t)data_len;
3268 fwi->write_flags = (fwi->size % sysconf(_SC_PAGESIZE)) ? 0 : 1;
3269 fwi->lock_owner = pnd->pnd_lock_owner;
3270 fwi->flags = 0;
3271 fwi->flags |= (fwi->lock_owner != 0) ? FUSE_WRITE_LOCKOWNER : 0;
3272 fwi->flags |= (ioflag & IO_DIRECT) ? 0 : FUSE_WRITE_CACHE;
3273 (void)memcpy((fwi + 1), buf, data_len);
3274
3275
3276 #ifdef PERFUSE_DEBUG
3277 if (perfuse_diagflags & PDF_FH)
3278 DPRINTF("%s: opc = %p, nodeid = 0x%"PRIx64", "
3279 "fh = 0x%"PRIx64"\n", __func__,
3280 (void *)opc, pnd->pnd_nodeid, fwi->fh);
3281 #endif
3282 if ((error = xchg_msg(pu, opc, pm,
3283 sizeof(*fwo), wait_reply)) != 0)
3284 goto out;
3285
3286 fwo = GET_OUTPAYLOAD(ps, pm, fuse_write_out);
3287 written = fwo->size;
3288 ps->ps_destroy_msg(pm);
3289
3290 #ifdef PERFUSE_DEBUG
3291 if (written > *resid)
3292 DERRX(EX_SOFTWARE, "%s: Unexpected big write %zd",
3293 __func__, written);
3294 #endif
3295 *resid -= written;
3296 offset += written;
3297 buf += written;
3298
3299 } while (*resid != 0);
3300
3301 /*
3302 * puffs_ops(3) says
3303 * "everything must be written or an error will be generated"
3304 */
3305 if (*resid != 0)
3306 error = EFBIG;
3307
3308 out:
3309 #ifdef PERFUSE_DEBUG
3310 if (perfuse_diagflags & PDF_RESIZE) {
3311 if (offset > (off_t)vap->va_size)
3312 DPRINTF("<< %s %p %" PRIu64 " -> %lld\n", __func__,
3313 (void *)opc, vap->va_size, (long long)offset);
3314 else
3315 DPRINTF("<< %s %p \n", __func__, (void *)opc);
3316 }
3317 #endif
3318
3319 /*
3320 * Update file size if we wrote beyond the end
3321 */
3322 if (offset > (off_t)vap->va_size)
3323 vap->va_size = offset;
3324
3325 /*
3326 * Statistics
3327 */
3328 if (ioflag & (IO_SYNC|IO_DSYNC))
3329 ps->ps_syncwrites++;
3330 else
3331 ps->ps_asyncwrites++;
3332
3333 /*
3334 * Remember to sync the file
3335 */
3336 pnd->pnd_flags |= PND_DIRTY;
3337
3338 #ifdef PERFUSE_DEBUG
3339 if (perfuse_diagflags & PDF_SYNC)
3340 DPRINTF("%s: DIRTY opc = %p, file = \"%s\"\n",
3341 __func__, (void*)opc, perfuse_node_path(ps, opc));
3342 #endif
3343
3344 if (inresize) {
3345 #ifdef PERFUSE_DEBUG
3346 if (!(pnd->pnd_flags & PND_INRESIZE))
3347 DERRX(EX_SOFTWARE, "file write grow without resize");
3348 #endif
3349 pnd->pnd_flags &= ~PND_INRESIZE;
3350 (void)dequeue_requests(opc, PCQ_RESIZE, DEQUEUE_ALL);
3351 }
3352
3353 /*
3354 * VOP_PUTPAGE causes FAF write where kernel does not
3355 * check operation result. At least warn if it failed.
3356 */
3357 #ifdef PUFFS_WRITE_FAF
3358 if (error && (xflag & PUFFS_WRITE_FAF))
3359 DWARN("Data loss caused by FAF write failed on \"%s\"",
3360 pnd->pnd_name);
3361 #endif /* PUFFS_WRITE_FAF */
3362
3363 /*
3364 * If there are no more queued write, we can resume
3365 * an operation awaiting write completion.
3366 */
3367 pnd->pnd_flags &= ~PND_INWRITE;
3368 if (dequeue_requests(opc, PCQ_WRITE, 1) == 0)
3369 (void)dequeue_requests(opc, PCQ_AFTERWRITE, DEQUEUE_ALL);
3370
3371 node_rele(opc);
3372 return error;
3373 }
3374
3375 /* ARGSUSED0 */
3376 void
3377 perfuse_cache_write(struct puffs_usermount *pu, puffs_cookie_t opc, size_t size,
3378 struct puffs_cacherun *runs)
3379 {
3380 return;
3381 }
3382
3383 /* ARGSUSED4 */
3384 int
3385 perfuse_node_getextattr(struct puffs_usermount *pu, puffs_cookie_t opc,
3386 int attrns, const char *attrname, size_t *attrsize, uint8_t *attr,
3387 size_t *resid, const struct puffs_cred *pcr)
3388 {
3389 struct perfuse_state *ps;
3390 char fuse_attrname[LINUX_XATTR_NAME_MAX + 1];
3391 perfuse_msg_t *pm;
3392 struct fuse_getxattr_in *fgi;
3393 struct fuse_getxattr_out *fgo;
3394 struct fuse_out_header *foh;
3395 size_t attrnamelen;
3396 size_t len;
3397 char *np;
3398 int error;
3399
3400 /* system namespace attrs are not accessible to non root users */
3401 if (attrns == EXTATTR_NAMESPACE_SYSTEM && !puffs_cred_isjuggernaut(pcr))
3402 return EPERM;
3403
3404 node_ref(opc);
3405 ps = puffs_getspecific(pu);
3406 attrname = perfuse_native_ns(attrns, attrname, fuse_attrname);
3407 attrnamelen = strlen(attrname) + 1;
3408 len = sizeof(*fgi) + attrnamelen;
3409
3410 pm = ps->ps_new_msg(pu, opc, FUSE_GETXATTR, len, pcr);
3411 fgi = GET_INPAYLOAD(ps, pm, fuse_getxattr_in);
3412 fgi->size = (unsigned int)((resid != NULL) ? *resid : 0);
3413 np = (char *)(void *)(fgi + 1);
3414 (void)strlcpy(np, attrname, attrnamelen);
3415
3416 if ((error = xchg_msg(pu, opc, pm, UNSPEC_REPLY_LEN, wait_reply)) != 0)
3417 goto out;
3418
3419 /*
3420 * We just get fuse_getattr_out with list size if we requested
3421 * a null size.
3422 */
3423 if (resid == NULL) {
3424 fgo = GET_OUTPAYLOAD(ps, pm, fuse_getxattr_out);
3425
3426 if (attrsize != NULL)
3427 *attrsize = fgo->size;
3428
3429 ps->ps_destroy_msg(pm);
3430 error = 0;
3431 goto out;
3432 }
3433
3434 /*
3435 * And with a non null requested size, we get the list just
3436 * after the header
3437 */
3438 foh = GET_OUTHDR(ps, pm);
3439 np = (char *)(void *)(foh + 1);
3440 len = foh->len - sizeof(*foh);
3441
3442 if (attrsize != NULL)
3443 *attrsize = len;
3444
3445 if (resid != NULL) {
3446 if (*resid < len) {
3447 error = ERANGE;
3448 ps->ps_destroy_msg(pm);
3449 goto out;
3450 }
3451
3452 (void)memcpy(attr, np, len);
3453 *resid -= len;
3454 }
3455
3456 ps->ps_destroy_msg(pm);
3457 error = 0;
3458
3459 out:
3460 node_rele(opc);
3461 return error;
3462 }
3463
3464 int
3465 perfuse_node_setextattr(struct puffs_usermount *pu, puffs_cookie_t opc,
3466 int attrns, const char *attrname, uint8_t *attr, size_t *resid,
3467 const struct puffs_cred *pcr)
3468 {
3469 struct perfuse_state *ps;
3470 char fuse_attrname[LINUX_XATTR_NAME_MAX + 1];
3471 perfuse_msg_t *pm;
3472 struct fuse_setxattr_in *fsi;
3473 size_t attrnamelen;
3474 size_t datalen;
3475 size_t len;
3476 char *np;
3477 int error;
3478
3479 /* system namespace attrs are not accessible to non root users */
3480 if (attrns == EXTATTR_NAMESPACE_SYSTEM && !puffs_cred_isjuggernaut(pcr))
3481 return EPERM;
3482
3483 node_ref(opc);
3484 ps = puffs_getspecific(pu);
3485 attrname = perfuse_native_ns(attrns, attrname, fuse_attrname);
3486 attrnamelen = strlen(attrname) + 1;
3487
3488 datalen = (resid != NULL) ? *resid : 0;
3489 len = sizeof(*fsi) + attrnamelen + datalen;
3490
3491 pm = ps->ps_new_msg(pu, opc, FUSE_SETXATTR, len, pcr);
3492 fsi = GET_INPAYLOAD(ps, pm, fuse_setxattr_in);
3493 fsi->size = (unsigned int)datalen;
3494 fsi->flags = 0;
3495 np = (char *)(void *)(fsi + 1);
3496 (void)strlcpy(np, attrname, attrnamelen);
3497 np += attrnamelen;
3498 if (datalen)
3499 (void)memcpy(np, (char *)attr, datalen);
3500
3501 if ((error = xchg_msg(pu, opc, pm,
3502 NO_PAYLOAD_REPLY_LEN, wait_reply)) != 0)
3503 goto out;
3504
3505 ps->ps_destroy_msg(pm);
3506 if (resid)
3507 *resid = 0;
3508 error = 0;
3509
3510 out:
3511 node_rele(opc);
3512 return error;
3513 }
3514
3515 /* ARGSUSED2 */
3516 int
3517 perfuse_node_listextattr(struct puffs_usermount *pu, puffs_cookie_t opc,
3518 int attrns, size_t *attrsize, uint8_t *attrs, size_t *resid, int flag,
3519 const struct puffs_cred *pcr)
3520 {
3521 struct perfuse_state *ps;
3522 perfuse_msg_t *pm;
3523 struct fuse_getxattr_in *fgi;
3524 struct fuse_getxattr_out *fgo;
3525 struct fuse_out_header *foh;
3526 char *np;
3527 size_t len, puffs_len, i, attrlen, outlen;
3528 int error;
3529
3530 /* system namespace attrs are not accessible to non root users */
3531 if (attrns == EXTATTR_NAMESPACE_SYSTEM && !puffs_cred_isjuggernaut(pcr))
3532 return EPERM;
3533
3534 node_ref(opc);
3535
3536 ps = puffs_getspecific(pu);
3537 len = sizeof(*fgi);
3538
3539 pm = ps->ps_new_msg(pu, opc, FUSE_LISTXATTR, len, pcr);
3540 fgi = GET_INPAYLOAD(ps, pm, fuse_getxattr_in);
3541 if (resid != NULL)
3542 fgi->size = (unsigned int)*resid;
3543 else
3544 fgi->size = 0;
3545
3546 if ((error = xchg_msg(pu, opc, pm, UNSPEC_REPLY_LEN, wait_reply)) != 0)
3547 goto out;
3548
3549 /*
3550 * We just get fuse_getattr_out with list size if we requested
3551 * a null size.
3552 */
3553 if (resid == NULL) {
3554 fgo = GET_OUTPAYLOAD(ps, pm, fuse_getxattr_out);
3555
3556 if (attrsize != NULL)
3557 *attrsize = fgo->size;
3558
3559 ps->ps_destroy_msg(pm);
3560
3561 error = 0;
3562 goto out;
3563 }
3564
3565 /*
3566 * And with a non null requested size, we get the list just
3567 * after the header
3568 */
3569 foh = GET_OUTHDR(ps, pm);
3570 np = (char *)(void *)(foh + 1);
3571 puffs_len = foh->len - sizeof(*foh);
3572
3573 if (attrsize != NULL)
3574 *attrsize = puffs_len;
3575
3576 if (attrs != NULL) {
3577 if (*resid < puffs_len) {
3578 error = ERANGE;
3579 ps->ps_destroy_msg(pm);
3580 goto out;
3581 }
3582
3583 outlen = 0;
3584
3585 for (i = 0; i < puffs_len; i += attrlen + 1) {
3586 attrlen = strlen(np + i);
3587
3588 /*
3589 * Filter attributes per namespace
3590 */
3591 if (!perfuse_ns_match(attrns, np + i))
3592 continue;
3593
3594 #ifdef PUFFS_EXTATTR_LIST_LENPREFIX
3595 /*
3596 * Convert the FUSE reply to length prefixed strings
3597 * if this is what the kernel wants.
3598 */
3599 if (flag & PUFFS_EXTATTR_LIST_LENPREFIX) {
3600 (void)memcpy(attrs + outlen + 1,
3601 np + i, attrlen);
3602 *(attrs + outlen) = (uint8_t)attrlen;
3603 } else
3604 #endif /* PUFFS_EXTATTR_LIST_LENPREFIX */
3605 (void)memcpy(attrs + outlen, np + i, attrlen + 1);
3606 outlen += attrlen + 1;
3607 }
3608
3609 *resid -= outlen;
3610 }
3611
3612 ps->ps_destroy_msg(pm);
3613 error = 0;
3614
3615 out:
3616 node_rele(opc);
3617 return error;
3618 }
3619
3620 int
3621 perfuse_node_deleteextattr(struct puffs_usermount *pu, puffs_cookie_t opc,
3622 int attrns, const char *attrname, const struct puffs_cred *pcr)
3623 {
3624 struct perfuse_state *ps;
3625 char fuse_attrname[LINUX_XATTR_NAME_MAX + 1];
3626 perfuse_msg_t *pm;
3627 size_t attrnamelen;
3628 char *np;
3629 int error;
3630
3631 /* system namespace attrs are not accessible to non root users */
3632 if (attrns == EXTATTR_NAMESPACE_SYSTEM && !puffs_cred_isjuggernaut(pcr))
3633 return EPERM;
3634
3635 node_ref(opc);
3636
3637 ps = puffs_getspecific(pu);
3638 attrname = perfuse_native_ns(attrns, attrname, fuse_attrname);
3639 attrnamelen = strlen(attrname) + 1;
3640
3641 pm = ps->ps_new_msg(pu, opc, FUSE_REMOVEXATTR, attrnamelen, pcr);
3642 np = _GET_INPAYLOAD(ps, pm, char *);
3643 (void)strlcpy(np, attrname, attrnamelen);
3644
3645 error = xchg_msg(pu, opc, pm, NO_PAYLOAD_REPLY_LEN, wait_reply);
3646 if (error != 0)
3647 goto out;
3648
3649 ps->ps_destroy_msg(pm);
3650
3651 out:
3652 node_rele(opc);
3653 return error;
3654 }
3655
3656 int
3657 perfuse_node_fallocate(struct puffs_usermount *pu, puffs_cookie_t opc,
3658 off_t off, off_t len)
3659 {
3660 struct perfuse_state *ps;
3661 perfuse_msg_t *pm;
3662 struct fuse_fallocate_in *fai;
3663 int error;
3664
3665 ps = puffs_getspecific(pu);
3666 if (ps->ps_flags & PS_NO_FALLOCATE)
3667 return EOPNOTSUPP;
3668
3669 node_ref(opc);
3670
3671 pm = ps->ps_new_msg(pu, opc, FUSE_FALLOCATE, sizeof(*fai), NULL);
3672
3673 fai = GET_INPAYLOAD(ps, pm, fuse_fallocate_in);
3674 fai->fh = perfuse_get_fh(opc, FWRITE);
3675 fai->offset = off;
3676 fai->length = len;
3677 fai->mode = 0;
3678
3679 error = xchg_msg(pu, opc, pm, NO_PAYLOAD_REPLY_LEN, wait_reply);
3680 if (error == EOPNOTSUPP || error == ENOSYS) {
3681 ps->ps_flags |= PS_NO_FALLOCATE;
3682 error = EOPNOTSUPP;
3683 }
3684 if (error != 0)
3685 goto out;
3686
3687 ps->ps_destroy_msg(pm);
3688
3689 out:
3690 node_rele(opc);
3691 return error;
3692 }
3693