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