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