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