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