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