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