Home | History | Annotate | Line # | Download | only in client
nfs_clvnops.c revision 1.1.1.2
      1      1.1  dholland /*	$NetBSD: nfs_clvnops.c,v 1.1.1.2 2016/11/18 07:49:11 pgoyette Exp $	*/
      2      1.1  dholland /*-
      3      1.1  dholland  * Copyright (c) 1989, 1993
      4      1.1  dholland  *	The Regents of the University of California.  All rights reserved.
      5      1.1  dholland  *
      6      1.1  dholland  * This code is derived from software contributed to Berkeley by
      7      1.1  dholland  * Rick Macklem at The University of Guelph.
      8      1.1  dholland  *
      9      1.1  dholland  * Redistribution and use in source and binary forms, with or without
     10      1.1  dholland  * modification, are permitted provided that the following conditions
     11      1.1  dholland  * are met:
     12      1.1  dholland  * 1. Redistributions of source code must retain the above copyright
     13      1.1  dholland  *    notice, this list of conditions and the following disclaimer.
     14      1.1  dholland  * 2. Redistributions in binary form must reproduce the above copyright
     15      1.1  dholland  *    notice, this list of conditions and the following disclaimer in the
     16      1.1  dholland  *    documentation and/or other materials provided with the distribution.
     17      1.1  dholland  * 4. Neither the name of the University nor the names of its contributors
     18      1.1  dholland  *    may be used to endorse or promote products derived from this software
     19      1.1  dholland  *    without specific prior written permission.
     20      1.1  dholland  *
     21      1.1  dholland  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22      1.1  dholland  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23      1.1  dholland  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24      1.1  dholland  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25      1.1  dholland  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26      1.1  dholland  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27      1.1  dholland  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28      1.1  dholland  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29      1.1  dholland  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30      1.1  dholland  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31      1.1  dholland  * SUCH DAMAGE.
     32      1.1  dholland  *
     33      1.1  dholland  *	from nfs_vnops.c	8.16 (Berkeley) 5/27/95
     34      1.1  dholland  */
     35      1.1  dholland 
     36      1.1  dholland #include <sys/cdefs.h>
     37  1.1.1.2  pgoyette /* __FBSDID("FreeBSD: head/sys/fs/nfsclient/nfs_clvnops.c 304026 2016-08-12 22:44:59Z rmacklem "); */
     38      1.1  dholland __RCSID("$NetBSD: nfs_clvnops.c,v 1.1.1.2 2016/11/18 07:49:11 pgoyette Exp $");
     39      1.1  dholland 
     40      1.1  dholland /*
     41      1.1  dholland  * vnode op calls for Sun NFS version 2, 3 and 4
     42      1.1  dholland  */
     43      1.1  dholland 
     44      1.1  dholland #include "opt_inet.h"
     45      1.1  dholland 
     46      1.1  dholland #include <sys/param.h>
     47      1.1  dholland #include <sys/kernel.h>
     48      1.1  dholland #include <sys/systm.h>
     49      1.1  dholland #include <sys/resourcevar.h>
     50      1.1  dholland #include <sys/proc.h>
     51      1.1  dholland #include <sys/mount.h>
     52      1.1  dholland #include <sys/bio.h>
     53      1.1  dholland #include <sys/buf.h>
     54      1.1  dholland #include <sys/jail.h>
     55      1.1  dholland #include <sys/malloc.h>
     56      1.1  dholland #include <sys/mbuf.h>
     57      1.1  dholland #include <sys/namei.h>
     58      1.1  dholland #include <sys/socket.h>
     59      1.1  dholland #include <sys/vnode.h>
     60      1.1  dholland #include <sys/dirent.h>
     61      1.1  dholland #include <sys/fcntl.h>
     62      1.1  dholland #include <sys/lockf.h>
     63      1.1  dholland #include <sys/stat.h>
     64      1.1  dholland #include <sys/sysctl.h>
     65      1.1  dholland #include <sys/signalvar.h>
     66      1.1  dholland 
     67      1.1  dholland #include <vm/vm.h>
     68      1.1  dholland #include <vm/vm_extern.h>
     69      1.1  dholland #include <vm/vm_object.h>
     70      1.1  dholland 
     71      1.1  dholland #include <fs/nfs/nfsport.h>
     72      1.1  dholland #include <fs/nfsclient/nfsnode.h>
     73      1.1  dholland #include <fs/nfsclient/nfsmount.h>
     74      1.1  dholland #include <fs/nfsclient/nfs.h>
     75      1.1  dholland #include <fs/nfsclient/nfs_kdtrace.h>
     76      1.1  dholland 
     77      1.1  dholland #include <net/if.h>
     78      1.1  dholland #include <netinet/in.h>
     79      1.1  dholland #include <netinet/in_var.h>
     80      1.1  dholland 
     81      1.1  dholland #include <nfs/nfs_lock.h>
     82      1.1  dholland 
     83      1.1  dholland #ifdef KDTRACE_HOOKS
     84      1.1  dholland #include <sys/dtrace_bsd.h>
     85      1.1  dholland 
     86      1.1  dholland dtrace_nfsclient_accesscache_flush_probe_func_t
     87      1.1  dholland 		dtrace_nfscl_accesscache_flush_done_probe;
     88      1.1  dholland uint32_t	nfscl_accesscache_flush_done_id;
     89      1.1  dholland 
     90      1.1  dholland dtrace_nfsclient_accesscache_get_probe_func_t
     91      1.1  dholland 		dtrace_nfscl_accesscache_get_hit_probe,
     92      1.1  dholland 		dtrace_nfscl_accesscache_get_miss_probe;
     93      1.1  dholland uint32_t	nfscl_accesscache_get_hit_id;
     94      1.1  dholland uint32_t	nfscl_accesscache_get_miss_id;
     95      1.1  dholland 
     96      1.1  dholland dtrace_nfsclient_accesscache_load_probe_func_t
     97      1.1  dholland 		dtrace_nfscl_accesscache_load_done_probe;
     98      1.1  dholland uint32_t	nfscl_accesscache_load_done_id;
     99      1.1  dholland #endif /* !KDTRACE_HOOKS */
    100      1.1  dholland 
    101      1.1  dholland /* Defs */
    102      1.1  dholland #define	TRUE	1
    103      1.1  dholland #define	FALSE	0
    104      1.1  dholland 
    105  1.1.1.2  pgoyette extern struct nfsstatsv1 nfsstatsv1;
    106      1.1  dholland extern int nfsrv_useacl;
    107      1.1  dholland extern int nfscl_debuglevel;
    108      1.1  dholland MALLOC_DECLARE(M_NEWNFSREQ);
    109      1.1  dholland 
    110      1.1  dholland /*
    111      1.1  dholland  * Ifdef for FreeBSD-current merged buffer cache. It is unfortunate that these
    112      1.1  dholland  * calls are not in getblk() and brelse() so that they would not be necessary
    113      1.1  dholland  * here.
    114      1.1  dholland  */
    115      1.1  dholland #ifndef B_VMIO
    116      1.1  dholland #define	vfs_busy_pages(bp, f)
    117      1.1  dholland #endif
    118      1.1  dholland 
    119      1.1  dholland static vop_read_t	nfsfifo_read;
    120      1.1  dholland static vop_write_t	nfsfifo_write;
    121      1.1  dholland static vop_close_t	nfsfifo_close;
    122      1.1  dholland static int	nfs_setattrrpc(struct vnode *, struct vattr *, struct ucred *,
    123      1.1  dholland 		    struct thread *);
    124      1.1  dholland static vop_lookup_t	nfs_lookup;
    125      1.1  dholland static vop_create_t	nfs_create;
    126      1.1  dholland static vop_mknod_t	nfs_mknod;
    127      1.1  dholland static vop_open_t	nfs_open;
    128      1.1  dholland static vop_pathconf_t	nfs_pathconf;
    129      1.1  dholland static vop_close_t	nfs_close;
    130      1.1  dholland static vop_access_t	nfs_access;
    131      1.1  dholland static vop_getattr_t	nfs_getattr;
    132      1.1  dholland static vop_setattr_t	nfs_setattr;
    133      1.1  dholland static vop_read_t	nfs_read;
    134      1.1  dholland static vop_fsync_t	nfs_fsync;
    135      1.1  dholland static vop_remove_t	nfs_remove;
    136      1.1  dholland static vop_link_t	nfs_link;
    137      1.1  dholland static vop_rename_t	nfs_rename;
    138      1.1  dholland static vop_mkdir_t	nfs_mkdir;
    139      1.1  dholland static vop_rmdir_t	nfs_rmdir;
    140      1.1  dholland static vop_symlink_t	nfs_symlink;
    141      1.1  dholland static vop_readdir_t	nfs_readdir;
    142      1.1  dholland static vop_strategy_t	nfs_strategy;
    143      1.1  dholland static	int	nfs_lookitup(struct vnode *, char *, int,
    144      1.1  dholland 		    struct ucred *, struct thread *, struct nfsnode **);
    145      1.1  dholland static	int	nfs_sillyrename(struct vnode *, struct vnode *,
    146      1.1  dholland 		    struct componentname *);
    147      1.1  dholland static vop_access_t	nfsspec_access;
    148      1.1  dholland static vop_readlink_t	nfs_readlink;
    149      1.1  dholland static vop_print_t	nfs_print;
    150      1.1  dholland static vop_advlock_t	nfs_advlock;
    151      1.1  dholland static vop_advlockasync_t nfs_advlockasync;
    152      1.1  dholland static vop_getacl_t nfs_getacl;
    153      1.1  dholland static vop_setacl_t nfs_setacl;
    154      1.1  dholland 
    155      1.1  dholland /*
    156      1.1  dholland  * Global vfs data structures for nfs
    157      1.1  dholland  */
    158      1.1  dholland struct vop_vector newnfs_vnodeops = {
    159      1.1  dholland 	.vop_default =		&default_vnodeops,
    160      1.1  dholland 	.vop_access =		nfs_access,
    161      1.1  dholland 	.vop_advlock =		nfs_advlock,
    162      1.1  dholland 	.vop_advlockasync =	nfs_advlockasync,
    163      1.1  dholland 	.vop_close =		nfs_close,
    164      1.1  dholland 	.vop_create =		nfs_create,
    165      1.1  dholland 	.vop_fsync =		nfs_fsync,
    166      1.1  dholland 	.vop_getattr =		nfs_getattr,
    167      1.1  dholland 	.vop_getpages =		ncl_getpages,
    168      1.1  dholland 	.vop_putpages =		ncl_putpages,
    169      1.1  dholland 	.vop_inactive =		ncl_inactive,
    170      1.1  dholland 	.vop_link =		nfs_link,
    171      1.1  dholland 	.vop_lookup =		nfs_lookup,
    172      1.1  dholland 	.vop_mkdir =		nfs_mkdir,
    173      1.1  dholland 	.vop_mknod =		nfs_mknod,
    174      1.1  dholland 	.vop_open =		nfs_open,
    175      1.1  dholland 	.vop_pathconf =		nfs_pathconf,
    176      1.1  dholland 	.vop_print =		nfs_print,
    177      1.1  dholland 	.vop_read =		nfs_read,
    178      1.1  dholland 	.vop_readdir =		nfs_readdir,
    179      1.1  dholland 	.vop_readlink =		nfs_readlink,
    180      1.1  dholland 	.vop_reclaim =		ncl_reclaim,
    181      1.1  dholland 	.vop_remove =		nfs_remove,
    182      1.1  dholland 	.vop_rename =		nfs_rename,
    183      1.1  dholland 	.vop_rmdir =		nfs_rmdir,
    184      1.1  dholland 	.vop_setattr =		nfs_setattr,
    185      1.1  dholland 	.vop_strategy =		nfs_strategy,
    186      1.1  dholland 	.vop_symlink =		nfs_symlink,
    187      1.1  dholland 	.vop_write =		ncl_write,
    188      1.1  dholland 	.vop_getacl =		nfs_getacl,
    189      1.1  dholland 	.vop_setacl =		nfs_setacl,
    190      1.1  dholland };
    191      1.1  dholland 
    192      1.1  dholland struct vop_vector newnfs_fifoops = {
    193      1.1  dholland 	.vop_default =		&fifo_specops,
    194      1.1  dholland 	.vop_access =		nfsspec_access,
    195      1.1  dholland 	.vop_close =		nfsfifo_close,
    196      1.1  dholland 	.vop_fsync =		nfs_fsync,
    197      1.1  dholland 	.vop_getattr =		nfs_getattr,
    198      1.1  dholland 	.vop_inactive =		ncl_inactive,
    199      1.1  dholland 	.vop_print =		nfs_print,
    200      1.1  dholland 	.vop_read =		nfsfifo_read,
    201      1.1  dholland 	.vop_reclaim =		ncl_reclaim,
    202      1.1  dholland 	.vop_setattr =		nfs_setattr,
    203      1.1  dholland 	.vop_write =		nfsfifo_write,
    204      1.1  dholland };
    205      1.1  dholland 
    206      1.1  dholland static int nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp,
    207      1.1  dholland     struct componentname *cnp, struct vattr *vap);
    208      1.1  dholland static int nfs_removerpc(struct vnode *dvp, struct vnode *vp, char *name,
    209      1.1  dholland     int namelen, struct ucred *cred, struct thread *td);
    210      1.1  dholland static int nfs_renamerpc(struct vnode *fdvp, struct vnode *fvp,
    211      1.1  dholland     char *fnameptr, int fnamelen, struct vnode *tdvp, struct vnode *tvp,
    212      1.1  dholland     char *tnameptr, int tnamelen, struct ucred *cred, struct thread *td);
    213      1.1  dholland static int nfs_renameit(struct vnode *sdvp, struct vnode *svp,
    214      1.1  dholland     struct componentname *scnp, struct sillyrename *sp);
    215      1.1  dholland 
    216      1.1  dholland /*
    217      1.1  dholland  * Global variables
    218      1.1  dholland  */
    219      1.1  dholland #define	DIRHDSIZ	(sizeof (struct dirent) - (MAXNAMLEN + 1))
    220      1.1  dholland 
    221      1.1  dholland SYSCTL_DECL(_vfs_nfs);
    222      1.1  dholland 
    223      1.1  dholland static int	nfsaccess_cache_timeout = NFS_MAXATTRTIMO;
    224      1.1  dholland SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
    225      1.1  dholland 	   &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
    226      1.1  dholland 
    227      1.1  dholland static int	nfs_prime_access_cache = 0;
    228      1.1  dholland SYSCTL_INT(_vfs_nfs, OID_AUTO, prime_access_cache, CTLFLAG_RW,
    229      1.1  dholland 	   &nfs_prime_access_cache, 0,
    230      1.1  dholland 	   "Prime NFS ACCESS cache when fetching attributes");
    231      1.1  dholland 
    232      1.1  dholland static int	newnfs_commit_on_close = 0;
    233      1.1  dholland SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_on_close, CTLFLAG_RW,
    234      1.1  dholland     &newnfs_commit_on_close, 0, "write+commit on close, else only write");
    235      1.1  dholland 
    236      1.1  dholland static int	nfs_clean_pages_on_close = 1;
    237      1.1  dholland SYSCTL_INT(_vfs_nfs, OID_AUTO, clean_pages_on_close, CTLFLAG_RW,
    238      1.1  dholland 	   &nfs_clean_pages_on_close, 0, "NFS clean dirty pages on close");
    239      1.1  dholland 
    240      1.1  dholland int newnfs_directio_enable = 0;
    241      1.1  dholland SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_directio_enable, CTLFLAG_RW,
    242      1.1  dholland 	   &newnfs_directio_enable, 0, "Enable NFS directio");
    243      1.1  dholland 
    244      1.1  dholland int nfs_keep_dirty_on_error;
    245      1.1  dholland SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_keep_dirty_on_error, CTLFLAG_RW,
    246      1.1  dholland     &nfs_keep_dirty_on_error, 0, "Retry pageout if error returned");
    247      1.1  dholland 
    248      1.1  dholland /*
    249      1.1  dholland  * This sysctl allows other processes to mmap a file that has been opened
    250      1.1  dholland  * O_DIRECT by a process.  In general, having processes mmap the file while
    251      1.1  dholland  * Direct IO is in progress can lead to Data Inconsistencies.  But, we allow
    252      1.1  dholland  * this by default to prevent DoS attacks - to prevent a malicious user from
    253      1.1  dholland  * opening up files O_DIRECT preventing other users from mmap'ing these
    254      1.1  dholland  * files.  "Protected" environments where stricter consistency guarantees are
    255      1.1  dholland  * required can disable this knob.  The process that opened the file O_DIRECT
    256      1.1  dholland  * cannot mmap() the file, because mmap'ed IO on an O_DIRECT open() is not
    257      1.1  dholland  * meaningful.
    258      1.1  dholland  */
    259      1.1  dholland int newnfs_directio_allow_mmap = 1;
    260      1.1  dholland SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_directio_allow_mmap, CTLFLAG_RW,
    261      1.1  dholland 	   &newnfs_directio_allow_mmap, 0, "Enable mmaped IO on file with O_DIRECT opens");
    262      1.1  dholland 
    263      1.1  dholland #define	NFSACCESS_ALL (NFSACCESS_READ | NFSACCESS_MODIFY		\
    264      1.1  dholland 			 | NFSACCESS_EXTEND | NFSACCESS_EXECUTE	\
    265      1.1  dholland 			 | NFSACCESS_DELETE | NFSACCESS_LOOKUP)
    266      1.1  dholland 
    267      1.1  dholland /*
    268      1.1  dholland  * SMP Locking Note :
    269      1.1  dholland  * The list of locks after the description of the lock is the ordering
    270      1.1  dholland  * of other locks acquired with the lock held.
    271      1.1  dholland  * np->n_mtx : Protects the fields in the nfsnode.
    272      1.1  dholland        VM Object Lock
    273      1.1  dholland        VI_MTX (acquired indirectly)
    274      1.1  dholland  * nmp->nm_mtx : Protects the fields in the nfsmount.
    275      1.1  dholland        rep->r_mtx
    276      1.1  dholland  * ncl_iod_mutex : Global lock, protects shared nfsiod state.
    277      1.1  dholland  * nfs_reqq_mtx : Global lock, protects the nfs_reqq list.
    278      1.1  dholland        nmp->nm_mtx
    279      1.1  dholland        rep->r_mtx
    280      1.1  dholland  * rep->r_mtx : Protects the fields in an nfsreq.
    281      1.1  dholland  */
    282      1.1  dholland 
    283      1.1  dholland static int
    284      1.1  dholland nfs34_access_otw(struct vnode *vp, int wmode, struct thread *td,
    285      1.1  dholland     struct ucred *cred, u_int32_t *retmode)
    286      1.1  dholland {
    287      1.1  dholland 	int error = 0, attrflag, i, lrupos;
    288      1.1  dholland 	u_int32_t rmode;
    289      1.1  dholland 	struct nfsnode *np = VTONFS(vp);
    290      1.1  dholland 	struct nfsvattr nfsva;
    291      1.1  dholland 
    292      1.1  dholland 	error = nfsrpc_accessrpc(vp, wmode, cred, td, &nfsva, &attrflag,
    293      1.1  dholland 	    &rmode, NULL);
    294      1.1  dholland 	if (attrflag)
    295      1.1  dholland 		(void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
    296      1.1  dholland 	if (!error) {
    297      1.1  dholland 		lrupos = 0;
    298      1.1  dholland 		mtx_lock(&np->n_mtx);
    299      1.1  dholland 		for (i = 0; i < NFS_ACCESSCACHESIZE; i++) {
    300      1.1  dholland 			if (np->n_accesscache[i].uid == cred->cr_uid) {
    301      1.1  dholland 				np->n_accesscache[i].mode = rmode;
    302      1.1  dholland 				np->n_accesscache[i].stamp = time_second;
    303      1.1  dholland 				break;
    304      1.1  dholland 			}
    305      1.1  dholland 			if (i > 0 && np->n_accesscache[i].stamp <
    306      1.1  dholland 			    np->n_accesscache[lrupos].stamp)
    307      1.1  dholland 				lrupos = i;
    308      1.1  dholland 		}
    309      1.1  dholland 		if (i == NFS_ACCESSCACHESIZE) {
    310      1.1  dholland 			np->n_accesscache[lrupos].uid = cred->cr_uid;
    311      1.1  dholland 			np->n_accesscache[lrupos].mode = rmode;
    312      1.1  dholland 			np->n_accesscache[lrupos].stamp = time_second;
    313      1.1  dholland 		}
    314      1.1  dholland 		mtx_unlock(&np->n_mtx);
    315      1.1  dholland 		if (retmode != NULL)
    316      1.1  dholland 			*retmode = rmode;
    317      1.1  dholland 		KDTRACE_NFS_ACCESSCACHE_LOAD_DONE(vp, cred->cr_uid, rmode, 0);
    318      1.1  dholland 	} else if (NFS_ISV4(vp)) {
    319      1.1  dholland 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
    320      1.1  dholland 	}
    321      1.1  dholland #ifdef KDTRACE_HOOKS
    322      1.1  dholland 	if (error != 0)
    323      1.1  dholland 		KDTRACE_NFS_ACCESSCACHE_LOAD_DONE(vp, cred->cr_uid, 0,
    324      1.1  dholland 		    error);
    325      1.1  dholland #endif
    326      1.1  dholland 	return (error);
    327      1.1  dholland }
    328      1.1  dholland 
    329      1.1  dholland /*
    330      1.1  dholland  * nfs access vnode op.
    331      1.1  dholland  * For nfs version 2, just return ok. File accesses may fail later.
    332      1.1  dholland  * For nfs version 3, use the access rpc to check accessibility. If file modes
    333      1.1  dholland  * are changed on the server, accesses might still fail later.
    334      1.1  dholland  */
    335      1.1  dholland static int
    336      1.1  dholland nfs_access(struct vop_access_args *ap)
    337      1.1  dholland {
    338      1.1  dholland 	struct vnode *vp = ap->a_vp;
    339      1.1  dholland 	int error = 0, i, gotahit;
    340      1.1  dholland 	u_int32_t mode, wmode, rmode;
    341      1.1  dholland 	int v34 = NFS_ISV34(vp);
    342      1.1  dholland 	struct nfsnode *np = VTONFS(vp);
    343      1.1  dholland 
    344      1.1  dholland 	/*
    345      1.1  dholland 	 * Disallow write attempts on filesystems mounted read-only;
    346      1.1  dholland 	 * unless the file is a socket, fifo, or a block or character
    347      1.1  dholland 	 * device resident on the filesystem.
    348      1.1  dholland 	 */
    349      1.1  dholland 	if ((ap->a_accmode & (VWRITE | VAPPEND | VWRITE_NAMED_ATTRS |
    350      1.1  dholland 	    VDELETE_CHILD | VWRITE_ATTRIBUTES | VDELETE | VWRITE_ACL |
    351      1.1  dholland 	    VWRITE_OWNER)) != 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) != 0) {
    352      1.1  dholland 		switch (vp->v_type) {
    353      1.1  dholland 		case VREG:
    354      1.1  dholland 		case VDIR:
    355      1.1  dholland 		case VLNK:
    356      1.1  dholland 			return (EROFS);
    357      1.1  dholland 		default:
    358      1.1  dholland 			break;
    359      1.1  dholland 		}
    360      1.1  dholland 	}
    361      1.1  dholland 	/*
    362      1.1  dholland 	 * For nfs v3 or v4, check to see if we have done this recently, and if
    363      1.1  dholland 	 * so return our cached result instead of making an ACCESS call.
    364      1.1  dholland 	 * If not, do an access rpc, otherwise you are stuck emulating
    365      1.1  dholland 	 * ufs_access() locally using the vattr. This may not be correct,
    366      1.1  dholland 	 * since the server may apply other access criteria such as
    367      1.1  dholland 	 * client uid-->server uid mapping that we do not know about.
    368      1.1  dholland 	 */
    369      1.1  dholland 	if (v34) {
    370      1.1  dholland 		if (ap->a_accmode & VREAD)
    371      1.1  dholland 			mode = NFSACCESS_READ;
    372      1.1  dholland 		else
    373      1.1  dholland 			mode = 0;
    374      1.1  dholland 		if (vp->v_type != VDIR) {
    375      1.1  dholland 			if (ap->a_accmode & VWRITE)
    376      1.1  dholland 				mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
    377      1.1  dholland 			if (ap->a_accmode & VAPPEND)
    378      1.1  dholland 				mode |= NFSACCESS_EXTEND;
    379      1.1  dholland 			if (ap->a_accmode & VEXEC)
    380      1.1  dholland 				mode |= NFSACCESS_EXECUTE;
    381      1.1  dholland 			if (ap->a_accmode & VDELETE)
    382      1.1  dholland 				mode |= NFSACCESS_DELETE;
    383      1.1  dholland 		} else {
    384      1.1  dholland 			if (ap->a_accmode & VWRITE)
    385      1.1  dholland 				mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
    386      1.1  dholland 			if (ap->a_accmode & VAPPEND)
    387      1.1  dholland 				mode |= NFSACCESS_EXTEND;
    388      1.1  dholland 			if (ap->a_accmode & VEXEC)
    389      1.1  dholland 				mode |= NFSACCESS_LOOKUP;
    390      1.1  dholland 			if (ap->a_accmode & VDELETE)
    391      1.1  dholland 				mode |= NFSACCESS_DELETE;
    392      1.1  dholland 			if (ap->a_accmode & VDELETE_CHILD)
    393      1.1  dholland 				mode |= NFSACCESS_MODIFY;
    394      1.1  dholland 		}
    395      1.1  dholland 		/* XXX safety belt, only make blanket request if caching */
    396      1.1  dholland 		if (nfsaccess_cache_timeout > 0) {
    397      1.1  dholland 			wmode = NFSACCESS_READ | NFSACCESS_MODIFY |
    398      1.1  dholland 				NFSACCESS_EXTEND | NFSACCESS_EXECUTE |
    399      1.1  dholland 				NFSACCESS_DELETE | NFSACCESS_LOOKUP;
    400      1.1  dholland 		} else {
    401      1.1  dholland 			wmode = mode;
    402      1.1  dholland 		}
    403      1.1  dholland 
    404      1.1  dholland 		/*
    405      1.1  dholland 		 * Does our cached result allow us to give a definite yes to
    406      1.1  dholland 		 * this request?
    407      1.1  dholland 		 */
    408      1.1  dholland 		gotahit = 0;
    409      1.1  dholland 		mtx_lock(&np->n_mtx);
    410      1.1  dholland 		for (i = 0; i < NFS_ACCESSCACHESIZE; i++) {
    411      1.1  dholland 			if (ap->a_cred->cr_uid == np->n_accesscache[i].uid) {
    412      1.1  dholland 			    if (time_second < (np->n_accesscache[i].stamp
    413      1.1  dholland 				+ nfsaccess_cache_timeout) &&
    414      1.1  dholland 				(np->n_accesscache[i].mode & mode) == mode) {
    415  1.1.1.2  pgoyette 				NFSINCRGLOBAL(nfsstatsv1.accesscache_hits);
    416      1.1  dholland 				gotahit = 1;
    417      1.1  dholland 			    }
    418      1.1  dholland 			    break;
    419      1.1  dholland 			}
    420      1.1  dholland 		}
    421      1.1  dholland 		mtx_unlock(&np->n_mtx);
    422      1.1  dholland #ifdef KDTRACE_HOOKS
    423      1.1  dholland 		if (gotahit != 0)
    424      1.1  dholland 			KDTRACE_NFS_ACCESSCACHE_GET_HIT(vp,
    425      1.1  dholland 			    ap->a_cred->cr_uid, mode);
    426      1.1  dholland 		else
    427      1.1  dholland 			KDTRACE_NFS_ACCESSCACHE_GET_MISS(vp,
    428      1.1  dholland 			    ap->a_cred->cr_uid, mode);
    429      1.1  dholland #endif
    430      1.1  dholland 		if (gotahit == 0) {
    431      1.1  dholland 			/*
    432      1.1  dholland 			 * Either a no, or a don't know.  Go to the wire.
    433      1.1  dholland 			 */
    434  1.1.1.2  pgoyette 			NFSINCRGLOBAL(nfsstatsv1.accesscache_misses);
    435      1.1  dholland 		        error = nfs34_access_otw(vp, wmode, ap->a_td,
    436      1.1  dholland 			    ap->a_cred, &rmode);
    437      1.1  dholland 			if (!error &&
    438      1.1  dholland 			    (rmode & mode) != mode)
    439      1.1  dholland 				error = EACCES;
    440      1.1  dholland 		}
    441      1.1  dholland 		return (error);
    442      1.1  dholland 	} else {
    443      1.1  dholland 		if ((error = nfsspec_access(ap)) != 0) {
    444      1.1  dholland 			return (error);
    445      1.1  dholland 		}
    446      1.1  dholland 		/*
    447      1.1  dholland 		 * Attempt to prevent a mapped root from accessing a file
    448      1.1  dholland 		 * which it shouldn't.  We try to read a byte from the file
    449      1.1  dholland 		 * if the user is root and the file is not zero length.
    450      1.1  dholland 		 * After calling nfsspec_access, we should have the correct
    451      1.1  dholland 		 * file size cached.
    452      1.1  dholland 		 */
    453      1.1  dholland 		mtx_lock(&np->n_mtx);
    454      1.1  dholland 		if (ap->a_cred->cr_uid == 0 && (ap->a_accmode & VREAD)
    455      1.1  dholland 		    && VTONFS(vp)->n_size > 0) {
    456      1.1  dholland 			struct iovec aiov;
    457      1.1  dholland 			struct uio auio;
    458      1.1  dholland 			char buf[1];
    459      1.1  dholland 
    460      1.1  dholland 			mtx_unlock(&np->n_mtx);
    461      1.1  dholland 			aiov.iov_base = buf;
    462      1.1  dholland 			aiov.iov_len = 1;
    463      1.1  dholland 			auio.uio_iov = &aiov;
    464      1.1  dholland 			auio.uio_iovcnt = 1;
    465      1.1  dholland 			auio.uio_offset = 0;
    466      1.1  dholland 			auio.uio_resid = 1;
    467      1.1  dholland 			auio.uio_segflg = UIO_SYSSPACE;
    468      1.1  dholland 			auio.uio_rw = UIO_READ;
    469      1.1  dholland 			auio.uio_td = ap->a_td;
    470      1.1  dholland 
    471      1.1  dholland 			if (vp->v_type == VREG)
    472      1.1  dholland 				error = ncl_readrpc(vp, &auio, ap->a_cred);
    473      1.1  dholland 			else if (vp->v_type == VDIR) {
    474      1.1  dholland 				char* bp;
    475      1.1  dholland 				bp = malloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
    476      1.1  dholland 				aiov.iov_base = bp;
    477      1.1  dholland 				aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
    478      1.1  dholland 				error = ncl_readdirrpc(vp, &auio, ap->a_cred,
    479      1.1  dholland 				    ap->a_td);
    480      1.1  dholland 				free(bp, M_TEMP);
    481      1.1  dholland 			} else if (vp->v_type == VLNK)
    482      1.1  dholland 				error = ncl_readlinkrpc(vp, &auio, ap->a_cred);
    483      1.1  dholland 			else
    484      1.1  dholland 				error = EACCES;
    485      1.1  dholland 		} else
    486      1.1  dholland 			mtx_unlock(&np->n_mtx);
    487      1.1  dholland 		return (error);
    488      1.1  dholland 	}
    489      1.1  dholland }
    490      1.1  dholland 
    491      1.1  dholland 
    492      1.1  dholland /*
    493      1.1  dholland  * nfs open vnode op
    494      1.1  dholland  * Check to see if the type is ok
    495      1.1  dholland  * and that deletion is not in progress.
    496      1.1  dholland  * For paged in text files, you will need to flush the page cache
    497      1.1  dholland  * if consistency is lost.
    498      1.1  dholland  */
    499      1.1  dholland /* ARGSUSED */
    500      1.1  dholland static int
    501      1.1  dholland nfs_open(struct vop_open_args *ap)
    502      1.1  dholland {
    503      1.1  dholland 	struct vnode *vp = ap->a_vp;
    504      1.1  dholland 	struct nfsnode *np = VTONFS(vp);
    505      1.1  dholland 	struct vattr vattr;
    506      1.1  dholland 	int error;
    507      1.1  dholland 	int fmode = ap->a_mode;
    508      1.1  dholland 	struct ucred *cred;
    509      1.1  dholland 
    510      1.1  dholland 	if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK)
    511      1.1  dholland 		return (EOPNOTSUPP);
    512      1.1  dholland 
    513      1.1  dholland 	/*
    514      1.1  dholland 	 * For NFSv4, we need to do the Open Op before cache validation,
    515      1.1  dholland 	 * so that we conform to RFC3530 Sec. 9.3.1.
    516      1.1  dholland 	 */
    517      1.1  dholland 	if (NFS_ISV4(vp)) {
    518      1.1  dholland 		error = nfsrpc_open(vp, fmode, ap->a_cred, ap->a_td);
    519      1.1  dholland 		if (error) {
    520      1.1  dholland 			error = nfscl_maperr(ap->a_td, error, (uid_t)0,
    521      1.1  dholland 			    (gid_t)0);
    522      1.1  dholland 			return (error);
    523      1.1  dholland 		}
    524      1.1  dholland 	}
    525      1.1  dholland 
    526      1.1  dholland 	/*
    527      1.1  dholland 	 * Now, if this Open will be doing reading, re-validate/flush the
    528      1.1  dholland 	 * cache, so that Close/Open coherency is maintained.
    529      1.1  dholland 	 */
    530      1.1  dholland 	mtx_lock(&np->n_mtx);
    531      1.1  dholland 	if (np->n_flag & NMODIFIED) {
    532      1.1  dholland 		mtx_unlock(&np->n_mtx);
    533      1.1  dholland 		error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
    534      1.1  dholland 		if (error == EINTR || error == EIO) {
    535      1.1  dholland 			if (NFS_ISV4(vp))
    536      1.1  dholland 				(void) nfsrpc_close(vp, 0, ap->a_td);
    537      1.1  dholland 			return (error);
    538      1.1  dholland 		}
    539      1.1  dholland 		mtx_lock(&np->n_mtx);
    540      1.1  dholland 		np->n_attrstamp = 0;
    541      1.1  dholland 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
    542      1.1  dholland 		if (vp->v_type == VDIR)
    543      1.1  dholland 			np->n_direofoffset = 0;
    544      1.1  dholland 		mtx_unlock(&np->n_mtx);
    545      1.1  dholland 		error = VOP_GETATTR(vp, &vattr, ap->a_cred);
    546      1.1  dholland 		if (error) {
    547      1.1  dholland 			if (NFS_ISV4(vp))
    548      1.1  dholland 				(void) nfsrpc_close(vp, 0, ap->a_td);
    549      1.1  dholland 			return (error);
    550      1.1  dholland 		}
    551      1.1  dholland 		mtx_lock(&np->n_mtx);
    552      1.1  dholland 		np->n_mtime = vattr.va_mtime;
    553      1.1  dholland 		if (NFS_ISV4(vp))
    554      1.1  dholland 			np->n_change = vattr.va_filerev;
    555      1.1  dholland 	} else {
    556      1.1  dholland 		mtx_unlock(&np->n_mtx);
    557      1.1  dholland 		error = VOP_GETATTR(vp, &vattr, ap->a_cred);
    558      1.1  dholland 		if (error) {
    559      1.1  dholland 			if (NFS_ISV4(vp))
    560      1.1  dholland 				(void) nfsrpc_close(vp, 0, ap->a_td);
    561      1.1  dholland 			return (error);
    562      1.1  dholland 		}
    563      1.1  dholland 		mtx_lock(&np->n_mtx);
    564      1.1  dholland 		if ((NFS_ISV4(vp) && np->n_change != vattr.va_filerev) ||
    565      1.1  dholland 		    NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
    566      1.1  dholland 			if (vp->v_type == VDIR)
    567      1.1  dholland 				np->n_direofoffset = 0;
    568      1.1  dholland 			mtx_unlock(&np->n_mtx);
    569      1.1  dholland 			error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
    570      1.1  dholland 			if (error == EINTR || error == EIO) {
    571      1.1  dholland 				if (NFS_ISV4(vp))
    572      1.1  dholland 					(void) nfsrpc_close(vp, 0, ap->a_td);
    573      1.1  dholland 				return (error);
    574      1.1  dholland 			}
    575      1.1  dholland 			mtx_lock(&np->n_mtx);
    576      1.1  dholland 			np->n_mtime = vattr.va_mtime;
    577      1.1  dholland 			if (NFS_ISV4(vp))
    578      1.1  dholland 				np->n_change = vattr.va_filerev;
    579      1.1  dholland 		}
    580      1.1  dholland 	}
    581      1.1  dholland 
    582      1.1  dholland 	/*
    583      1.1  dholland 	 * If the object has >= 1 O_DIRECT active opens, we disable caching.
    584      1.1  dholland 	 */
    585      1.1  dholland 	if (newnfs_directio_enable && (fmode & O_DIRECT) &&
    586      1.1  dholland 	    (vp->v_type == VREG)) {
    587      1.1  dholland 		if (np->n_directio_opens == 0) {
    588      1.1  dholland 			mtx_unlock(&np->n_mtx);
    589      1.1  dholland 			error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
    590      1.1  dholland 			if (error) {
    591      1.1  dholland 				if (NFS_ISV4(vp))
    592      1.1  dholland 					(void) nfsrpc_close(vp, 0, ap->a_td);
    593      1.1  dholland 				return (error);
    594      1.1  dholland 			}
    595      1.1  dholland 			mtx_lock(&np->n_mtx);
    596      1.1  dholland 			np->n_flag |= NNONCACHE;
    597      1.1  dholland 		}
    598      1.1  dholland 		np->n_directio_opens++;
    599      1.1  dholland 	}
    600      1.1  dholland 
    601      1.1  dholland 	/* If opened for writing via NFSv4.1 or later, mark that for pNFS. */
    602      1.1  dholland 	if (NFSHASPNFS(VFSTONFS(vp->v_mount)) && (fmode & FWRITE) != 0)
    603      1.1  dholland 		np->n_flag |= NWRITEOPENED;
    604      1.1  dholland 
    605      1.1  dholland 	/*
    606      1.1  dholland 	 * If this is an open for writing, capture a reference to the
    607      1.1  dholland 	 * credentials, so they can be used by ncl_putpages(). Using
    608      1.1  dholland 	 * these write credentials is preferable to the credentials of
    609      1.1  dholland 	 * whatever thread happens to be doing the VOP_PUTPAGES() since
    610      1.1  dholland 	 * the write RPCs are less likely to fail with EACCES.
    611      1.1  dholland 	 */
    612      1.1  dholland 	if ((fmode & FWRITE) != 0) {
    613      1.1  dholland 		cred = np->n_writecred;
    614      1.1  dholland 		np->n_writecred = crhold(ap->a_cred);
    615      1.1  dholland 	} else
    616      1.1  dholland 		cred = NULL;
    617      1.1  dholland 	mtx_unlock(&np->n_mtx);
    618      1.1  dholland 
    619      1.1  dholland 	if (cred != NULL)
    620      1.1  dholland 		crfree(cred);
    621      1.1  dholland 	vnode_create_vobject(vp, vattr.va_size, ap->a_td);
    622      1.1  dholland 	return (0);
    623      1.1  dholland }
    624      1.1  dholland 
    625      1.1  dholland /*
    626      1.1  dholland  * nfs close vnode op
    627      1.1  dholland  * What an NFS client should do upon close after writing is a debatable issue.
    628      1.1  dholland  * Most NFS clients push delayed writes to the server upon close, basically for
    629      1.1  dholland  * two reasons:
    630      1.1  dholland  * 1 - So that any write errors may be reported back to the client process
    631      1.1  dholland  *     doing the close system call. By far the two most likely errors are
    632      1.1  dholland  *     NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
    633      1.1  dholland  * 2 - To put a worst case upper bound on cache inconsistency between
    634      1.1  dholland  *     multiple clients for the file.
    635      1.1  dholland  * There is also a consistency problem for Version 2 of the protocol w.r.t.
    636      1.1  dholland  * not being able to tell if other clients are writing a file concurrently,
    637      1.1  dholland  * since there is no way of knowing if the changed modify time in the reply
    638      1.1  dholland  * is only due to the write for this client.
    639      1.1  dholland  * (NFS Version 3 provides weak cache consistency data in the reply that
    640      1.1  dholland  *  should be sufficient to detect and handle this case.)
    641      1.1  dholland  *
    642      1.1  dholland  * The current code does the following:
    643      1.1  dholland  * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
    644      1.1  dholland  * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
    645      1.1  dholland  *                     or commit them (this satisfies 1 and 2 except for the
    646      1.1  dholland  *                     case where the server crashes after this close but
    647      1.1  dholland  *                     before the commit RPC, which is felt to be "good
    648      1.1  dholland  *                     enough". Changing the last argument to ncl_flush() to
    649      1.1  dholland  *                     a 1 would force a commit operation, if it is felt a
    650      1.1  dholland  *                     commit is necessary now.
    651      1.1  dholland  * for NFS Version 4 - flush the dirty buffers and commit them, if
    652      1.1  dholland  *		       nfscl_mustflush() says this is necessary.
    653      1.1  dholland  *                     It is necessary if there is no write delegation held,
    654      1.1  dholland  *                     in order to satisfy open/close coherency.
    655      1.1  dholland  *                     If the file isn't cached on local stable storage,
    656      1.1  dholland  *                     it may be necessary in order to detect "out of space"
    657      1.1  dholland  *                     errors from the server, if the write delegation
    658      1.1  dholland  *                     issued by the server doesn't allow the file to grow.
    659      1.1  dholland  */
    660      1.1  dholland /* ARGSUSED */
    661      1.1  dholland static int
    662      1.1  dholland nfs_close(struct vop_close_args *ap)
    663      1.1  dholland {
    664      1.1  dholland 	struct vnode *vp = ap->a_vp;
    665      1.1  dholland 	struct nfsnode *np = VTONFS(vp);
    666      1.1  dholland 	struct nfsvattr nfsva;
    667      1.1  dholland 	struct ucred *cred;
    668      1.1  dholland 	int error = 0, ret, localcred = 0;
    669      1.1  dholland 	int fmode = ap->a_fflag;
    670      1.1  dholland 
    671      1.1  dholland 	if ((vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF))
    672      1.1  dholland 		return (0);
    673      1.1  dholland 	/*
    674      1.1  dholland 	 * During shutdown, a_cred isn't valid, so just use root.
    675      1.1  dholland 	 */
    676      1.1  dholland 	if (ap->a_cred == NOCRED) {
    677      1.1  dholland 		cred = newnfs_getcred();
    678      1.1  dholland 		localcred = 1;
    679      1.1  dholland 	} else {
    680      1.1  dholland 		cred = ap->a_cred;
    681      1.1  dholland 	}
    682      1.1  dholland 	if (vp->v_type == VREG) {
    683      1.1  dholland 	    /*
    684      1.1  dholland 	     * Examine and clean dirty pages, regardless of NMODIFIED.
    685      1.1  dholland 	     * This closes a major hole in close-to-open consistency.
    686      1.1  dholland 	     * We want to push out all dirty pages (and buffers) on
    687      1.1  dholland 	     * close, regardless of whether they were dirtied by
    688      1.1  dholland 	     * mmap'ed writes or via write().
    689      1.1  dholland 	     */
    690      1.1  dholland 	    if (nfs_clean_pages_on_close && vp->v_object) {
    691      1.1  dholland 		VM_OBJECT_WLOCK(vp->v_object);
    692      1.1  dholland 		vm_object_page_clean(vp->v_object, 0, 0, 0);
    693      1.1  dholland 		VM_OBJECT_WUNLOCK(vp->v_object);
    694      1.1  dholland 	    }
    695      1.1  dholland 	    mtx_lock(&np->n_mtx);
    696      1.1  dholland 	    if (np->n_flag & NMODIFIED) {
    697      1.1  dholland 		mtx_unlock(&np->n_mtx);
    698      1.1  dholland 		if (NFS_ISV3(vp)) {
    699      1.1  dholland 		    /*
    700      1.1  dholland 		     * Under NFSv3 we have dirty buffers to dispose of.  We
    701      1.1  dholland 		     * must flush them to the NFS server.  We have the option
    702      1.1  dholland 		     * of waiting all the way through the commit rpc or just
    703      1.1  dholland 		     * waiting for the initial write.  The default is to only
    704      1.1  dholland 		     * wait through the initial write so the data is in the
    705      1.1  dholland 		     * server's cache, which is roughly similar to the state
    706      1.1  dholland 		     * a standard disk subsystem leaves the file in on close().
    707      1.1  dholland 		     *
    708      1.1  dholland 		     * We cannot clear the NMODIFIED bit in np->n_flag due to
    709      1.1  dholland 		     * potential races with other processes, and certainly
    710      1.1  dholland 		     * cannot clear it if we don't commit.
    711      1.1  dholland 		     * These races occur when there is no longer the old
    712      1.1  dholland 		     * traditional vnode locking implemented for Vnode Ops.
    713      1.1  dholland 		     */
    714      1.1  dholland 		    int cm = newnfs_commit_on_close ? 1 : 0;
    715      1.1  dholland 		    error = ncl_flush(vp, MNT_WAIT, cred, ap->a_td, cm, 0);
    716      1.1  dholland 		    /* np->n_flag &= ~NMODIFIED; */
    717      1.1  dholland 		} else if (NFS_ISV4(vp)) {
    718      1.1  dholland 			if (nfscl_mustflush(vp) != 0) {
    719      1.1  dholland 				int cm = newnfs_commit_on_close ? 1 : 0;
    720      1.1  dholland 				error = ncl_flush(vp, MNT_WAIT, cred, ap->a_td,
    721      1.1  dholland 				    cm, 0);
    722      1.1  dholland 				/*
    723      1.1  dholland 				 * as above w.r.t races when clearing
    724      1.1  dholland 				 * NMODIFIED.
    725      1.1  dholland 				 * np->n_flag &= ~NMODIFIED;
    726      1.1  dholland 				 */
    727      1.1  dholland 			}
    728      1.1  dholland 		} else
    729      1.1  dholland 		    error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
    730      1.1  dholland 		mtx_lock(&np->n_mtx);
    731      1.1  dholland 	    }
    732      1.1  dholland  	    /*
    733      1.1  dholland  	     * Invalidate the attribute cache in all cases.
    734      1.1  dholland  	     * An open is going to fetch fresh attrs any way, other procs
    735      1.1  dholland  	     * on this node that have file open will be forced to do an
    736      1.1  dholland  	     * otw attr fetch, but this is safe.
    737      1.1  dholland 	     * --> A user found that their RPC count dropped by 20% when
    738      1.1  dholland 	     *     this was commented out and I can't see any requirement
    739      1.1  dholland 	     *     for it, so I've disabled it when negative lookups are
    740      1.1  dholland 	     *     enabled. (What does this have to do with negative lookup
    741      1.1  dholland 	     *     caching? Well nothing, except it was reported by the
    742      1.1  dholland 	     *     same user that needed negative lookup caching and I wanted
    743      1.1  dholland 	     *     there to be a way to disable it to see if it
    744      1.1  dholland 	     *     is the cause of some caching/coherency issue that might
    745      1.1  dholland 	     *     crop up.)
    746      1.1  dholland  	     */
    747      1.1  dholland 	    if (VFSTONFS(vp->v_mount)->nm_negnametimeo == 0) {
    748      1.1  dholland 		    np->n_attrstamp = 0;
    749      1.1  dholland 		    KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
    750      1.1  dholland 	    }
    751      1.1  dholland 	    if (np->n_flag & NWRITEERR) {
    752      1.1  dholland 		np->n_flag &= ~NWRITEERR;
    753      1.1  dholland 		error = np->n_error;
    754      1.1  dholland 	    }
    755      1.1  dholland 	    mtx_unlock(&np->n_mtx);
    756      1.1  dholland 	}
    757      1.1  dholland 
    758      1.1  dholland 	if (NFS_ISV4(vp)) {
    759      1.1  dholland 		/*
    760      1.1  dholland 		 * Get attributes so "change" is up to date.
    761      1.1  dholland 		 */
    762  1.1.1.2  pgoyette 		if (error == 0 && nfscl_mustflush(vp) != 0 &&
    763  1.1.1.2  pgoyette 		    vp->v_type == VREG &&
    764  1.1.1.2  pgoyette 		    (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOCTO) == 0) {
    765      1.1  dholland 			ret = nfsrpc_getattr(vp, cred, ap->a_td, &nfsva,
    766      1.1  dholland 			    NULL);
    767      1.1  dholland 			if (!ret) {
    768      1.1  dholland 				np->n_change = nfsva.na_filerev;
    769      1.1  dholland 				(void) nfscl_loadattrcache(&vp, &nfsva, NULL,
    770      1.1  dholland 				    NULL, 0, 0);
    771      1.1  dholland 			}
    772      1.1  dholland 		}
    773      1.1  dholland 
    774      1.1  dholland 		/*
    775      1.1  dholland 		 * and do the close.
    776      1.1  dholland 		 */
    777      1.1  dholland 		ret = nfsrpc_close(vp, 0, ap->a_td);
    778      1.1  dholland 		if (!error && ret)
    779      1.1  dholland 			error = ret;
    780      1.1  dholland 		if (error)
    781      1.1  dholland 			error = nfscl_maperr(ap->a_td, error, (uid_t)0,
    782      1.1  dholland 			    (gid_t)0);
    783      1.1  dholland 	}
    784      1.1  dholland 	if (newnfs_directio_enable)
    785      1.1  dholland 		KASSERT((np->n_directio_asyncwr == 0),
    786      1.1  dholland 			("nfs_close: dirty unflushed (%d) directio buffers\n",
    787      1.1  dholland 			 np->n_directio_asyncwr));
    788      1.1  dholland 	if (newnfs_directio_enable && (fmode & O_DIRECT) && (vp->v_type == VREG)) {
    789      1.1  dholland 		mtx_lock(&np->n_mtx);
    790      1.1  dholland 		KASSERT((np->n_directio_opens > 0),
    791      1.1  dholland 			("nfs_close: unexpectedly value (0) of n_directio_opens\n"));
    792      1.1  dholland 		np->n_directio_opens--;
    793      1.1  dholland 		if (np->n_directio_opens == 0)
    794      1.1  dholland 			np->n_flag &= ~NNONCACHE;
    795      1.1  dholland 		mtx_unlock(&np->n_mtx);
    796      1.1  dholland 	}
    797      1.1  dholland 	if (localcred)
    798      1.1  dholland 		NFSFREECRED(cred);
    799      1.1  dholland 	return (error);
    800      1.1  dholland }
    801      1.1  dholland 
    802      1.1  dholland /*
    803      1.1  dholland  * nfs getattr call from vfs.
    804      1.1  dholland  */
    805      1.1  dholland static int
    806      1.1  dholland nfs_getattr(struct vop_getattr_args *ap)
    807      1.1  dholland {
    808      1.1  dholland 	struct vnode *vp = ap->a_vp;
    809      1.1  dholland 	struct thread *td = curthread;	/* XXX */
    810      1.1  dholland 	struct nfsnode *np = VTONFS(vp);
    811      1.1  dholland 	int error = 0;
    812      1.1  dholland 	struct nfsvattr nfsva;
    813      1.1  dholland 	struct vattr *vap = ap->a_vap;
    814      1.1  dholland 	struct vattr vattr;
    815      1.1  dholland 
    816      1.1  dholland 	/*
    817      1.1  dholland 	 * Update local times for special files.
    818      1.1  dholland 	 */
    819      1.1  dholland 	mtx_lock(&np->n_mtx);
    820      1.1  dholland 	if (np->n_flag & (NACC | NUPD))
    821      1.1  dholland 		np->n_flag |= NCHG;
    822      1.1  dholland 	mtx_unlock(&np->n_mtx);
    823      1.1  dholland 	/*
    824      1.1  dholland 	 * First look in the cache.
    825      1.1  dholland 	 */
    826      1.1  dholland 	if (ncl_getattrcache(vp, &vattr) == 0) {
    827      1.1  dholland 		vap->va_type = vattr.va_type;
    828      1.1  dholland 		vap->va_mode = vattr.va_mode;
    829      1.1  dholland 		vap->va_nlink = vattr.va_nlink;
    830      1.1  dholland 		vap->va_uid = vattr.va_uid;
    831      1.1  dholland 		vap->va_gid = vattr.va_gid;
    832      1.1  dholland 		vap->va_fsid = vattr.va_fsid;
    833      1.1  dholland 		vap->va_fileid = vattr.va_fileid;
    834      1.1  dholland 		vap->va_size = vattr.va_size;
    835      1.1  dholland 		vap->va_blocksize = vattr.va_blocksize;
    836      1.1  dholland 		vap->va_atime = vattr.va_atime;
    837      1.1  dholland 		vap->va_mtime = vattr.va_mtime;
    838      1.1  dholland 		vap->va_ctime = vattr.va_ctime;
    839      1.1  dholland 		vap->va_gen = vattr.va_gen;
    840      1.1  dholland 		vap->va_flags = vattr.va_flags;
    841      1.1  dholland 		vap->va_rdev = vattr.va_rdev;
    842      1.1  dholland 		vap->va_bytes = vattr.va_bytes;
    843      1.1  dholland 		vap->va_filerev = vattr.va_filerev;
    844      1.1  dholland 		/*
    845      1.1  dholland 		 * Get the local modify time for the case of a write
    846      1.1  dholland 		 * delegation.
    847      1.1  dholland 		 */
    848      1.1  dholland 		nfscl_deleggetmodtime(vp, &vap->va_mtime);
    849      1.1  dholland 		return (0);
    850      1.1  dholland 	}
    851      1.1  dholland 
    852      1.1  dholland 	if (NFS_ISV34(vp) && nfs_prime_access_cache &&
    853      1.1  dholland 	    nfsaccess_cache_timeout > 0) {
    854  1.1.1.2  pgoyette 		NFSINCRGLOBAL(nfsstatsv1.accesscache_misses);
    855      1.1  dholland 		nfs34_access_otw(vp, NFSACCESS_ALL, td, ap->a_cred, NULL);
    856      1.1  dholland 		if (ncl_getattrcache(vp, ap->a_vap) == 0) {
    857      1.1  dholland 			nfscl_deleggetmodtime(vp, &ap->a_vap->va_mtime);
    858      1.1  dholland 			return (0);
    859      1.1  dholland 		}
    860      1.1  dholland 	}
    861      1.1  dholland 	error = nfsrpc_getattr(vp, ap->a_cred, td, &nfsva, NULL);
    862      1.1  dholland 	if (!error)
    863      1.1  dholland 		error = nfscl_loadattrcache(&vp, &nfsva, vap, NULL, 0, 0);
    864      1.1  dholland 	if (!error) {
    865      1.1  dholland 		/*
    866      1.1  dholland 		 * Get the local modify time for the case of a write
    867      1.1  dholland 		 * delegation.
    868      1.1  dholland 		 */
    869      1.1  dholland 		nfscl_deleggetmodtime(vp, &vap->va_mtime);
    870      1.1  dholland 	} else if (NFS_ISV4(vp)) {
    871      1.1  dholland 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
    872      1.1  dholland 	}
    873      1.1  dholland 	return (error);
    874      1.1  dholland }
    875      1.1  dholland 
    876      1.1  dholland /*
    877      1.1  dholland  * nfs setattr call.
    878      1.1  dholland  */
    879      1.1  dholland static int
    880      1.1  dholland nfs_setattr(struct vop_setattr_args *ap)
    881      1.1  dholland {
    882      1.1  dholland 	struct vnode *vp = ap->a_vp;
    883      1.1  dholland 	struct nfsnode *np = VTONFS(vp);
    884      1.1  dholland 	struct thread *td = curthread;	/* XXX */
    885      1.1  dholland 	struct vattr *vap = ap->a_vap;
    886      1.1  dholland 	int error = 0;
    887      1.1  dholland 	u_quad_t tsize;
    888      1.1  dholland 
    889      1.1  dholland #ifndef nolint
    890      1.1  dholland 	tsize = (u_quad_t)0;
    891      1.1  dholland #endif
    892      1.1  dholland 
    893      1.1  dholland 	/*
    894      1.1  dholland 	 * Setting of flags and marking of atimes are not supported.
    895      1.1  dholland 	 */
    896      1.1  dholland 	if (vap->va_flags != VNOVAL)
    897      1.1  dholland 		return (EOPNOTSUPP);
    898      1.1  dholland 
    899      1.1  dholland 	/*
    900      1.1  dholland 	 * Disallow write attempts if the filesystem is mounted read-only.
    901      1.1  dholland 	 */
    902      1.1  dholland   	if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
    903      1.1  dholland 	    vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
    904      1.1  dholland 	    vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
    905      1.1  dholland 	    (vp->v_mount->mnt_flag & MNT_RDONLY))
    906      1.1  dholland 		return (EROFS);
    907      1.1  dholland 	if (vap->va_size != VNOVAL) {
    908      1.1  dholland  		switch (vp->v_type) {
    909      1.1  dholland  		case VDIR:
    910      1.1  dholland  			return (EISDIR);
    911      1.1  dholland  		case VCHR:
    912      1.1  dholland  		case VBLK:
    913      1.1  dholland  		case VSOCK:
    914      1.1  dholland  		case VFIFO:
    915      1.1  dholland 			if (vap->va_mtime.tv_sec == VNOVAL &&
    916      1.1  dholland 			    vap->va_atime.tv_sec == VNOVAL &&
    917      1.1  dholland 			    vap->va_mode == (mode_t)VNOVAL &&
    918      1.1  dholland 			    vap->va_uid == (uid_t)VNOVAL &&
    919      1.1  dholland 			    vap->va_gid == (gid_t)VNOVAL)
    920      1.1  dholland 				return (0);
    921      1.1  dholland  			vap->va_size = VNOVAL;
    922      1.1  dholland  			break;
    923      1.1  dholland  		default:
    924      1.1  dholland 			/*
    925      1.1  dholland 			 * Disallow write attempts if the filesystem is
    926      1.1  dholland 			 * mounted read-only.
    927      1.1  dholland 			 */
    928      1.1  dholland 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
    929      1.1  dholland 				return (EROFS);
    930      1.1  dholland 			/*
    931      1.1  dholland 			 *  We run vnode_pager_setsize() early (why?),
    932      1.1  dholland 			 * we must set np->n_size now to avoid vinvalbuf
    933      1.1  dholland 			 * V_SAVE races that might setsize a lower
    934      1.1  dholland 			 * value.
    935      1.1  dholland 			 */
    936      1.1  dholland 			mtx_lock(&np->n_mtx);
    937      1.1  dholland 			tsize = np->n_size;
    938      1.1  dholland 			mtx_unlock(&np->n_mtx);
    939      1.1  dholland 			error = ncl_meta_setsize(vp, ap->a_cred, td,
    940      1.1  dholland 			    vap->va_size);
    941      1.1  dholland 			mtx_lock(&np->n_mtx);
    942      1.1  dholland  			if (np->n_flag & NMODIFIED) {
    943      1.1  dholland 			    tsize = np->n_size;
    944      1.1  dholland 			    mtx_unlock(&np->n_mtx);
    945      1.1  dholland  			    if (vap->va_size == 0)
    946      1.1  dholland  				error = ncl_vinvalbuf(vp, 0, td, 1);
    947      1.1  dholland  			    else
    948      1.1  dholland  				error = ncl_vinvalbuf(vp, V_SAVE, td, 1);
    949      1.1  dholland  			    if (error) {
    950      1.1  dholland 				vnode_pager_setsize(vp, tsize);
    951      1.1  dholland 				return (error);
    952      1.1  dholland 			    }
    953      1.1  dholland 			    /*
    954      1.1  dholland 			     * Call nfscl_delegmodtime() to set the modify time
    955      1.1  dholland 			     * locally, as required.
    956      1.1  dholland 			     */
    957      1.1  dholland 			    nfscl_delegmodtime(vp);
    958      1.1  dholland  			} else
    959      1.1  dholland 			    mtx_unlock(&np->n_mtx);
    960      1.1  dholland 			/*
    961      1.1  dholland 			 * np->n_size has already been set to vap->va_size
    962      1.1  dholland 			 * in ncl_meta_setsize(). We must set it again since
    963      1.1  dholland 			 * nfs_loadattrcache() could be called through
    964      1.1  dholland 			 * ncl_meta_setsize() and could modify np->n_size.
    965      1.1  dholland 			 */
    966      1.1  dholland 			mtx_lock(&np->n_mtx);
    967      1.1  dholland  			np->n_vattr.na_size = np->n_size = vap->va_size;
    968      1.1  dholland 			mtx_unlock(&np->n_mtx);
    969  1.1.1.2  pgoyette   		}
    970      1.1  dholland   	} else {
    971      1.1  dholland 		mtx_lock(&np->n_mtx);
    972      1.1  dholland 		if ((vap->va_mtime.tv_sec != VNOVAL || vap->va_atime.tv_sec != VNOVAL) &&
    973      1.1  dholland 		    (np->n_flag & NMODIFIED) && vp->v_type == VREG) {
    974      1.1  dholland 			mtx_unlock(&np->n_mtx);
    975      1.1  dholland 			if ((error = ncl_vinvalbuf(vp, V_SAVE, td, 1)) != 0 &&
    976      1.1  dholland 			    (error == EINTR || error == EIO))
    977      1.1  dholland 				return (error);
    978      1.1  dholland 		} else
    979      1.1  dholland 			mtx_unlock(&np->n_mtx);
    980      1.1  dholland 	}
    981      1.1  dholland 	error = nfs_setattrrpc(vp, vap, ap->a_cred, td);
    982      1.1  dholland 	if (error && vap->va_size != VNOVAL) {
    983      1.1  dholland 		mtx_lock(&np->n_mtx);
    984      1.1  dholland 		np->n_size = np->n_vattr.na_size = tsize;
    985      1.1  dholland 		vnode_pager_setsize(vp, tsize);
    986      1.1  dholland 		mtx_unlock(&np->n_mtx);
    987      1.1  dholland 	}
    988      1.1  dholland 	return (error);
    989      1.1  dholland }
    990      1.1  dholland 
    991      1.1  dholland /*
    992      1.1  dholland  * Do an nfs setattr rpc.
    993      1.1  dholland  */
    994      1.1  dholland static int
    995      1.1  dholland nfs_setattrrpc(struct vnode *vp, struct vattr *vap, struct ucred *cred,
    996      1.1  dholland     struct thread *td)
    997      1.1  dholland {
    998      1.1  dholland 	struct nfsnode *np = VTONFS(vp);
    999      1.1  dholland 	int error, ret, attrflag, i;
   1000      1.1  dholland 	struct nfsvattr nfsva;
   1001      1.1  dholland 
   1002      1.1  dholland 	if (NFS_ISV34(vp)) {
   1003      1.1  dholland 		mtx_lock(&np->n_mtx);
   1004      1.1  dholland 		for (i = 0; i < NFS_ACCESSCACHESIZE; i++)
   1005      1.1  dholland 			np->n_accesscache[i].stamp = 0;
   1006      1.1  dholland 		np->n_flag |= NDELEGMOD;
   1007      1.1  dholland 		mtx_unlock(&np->n_mtx);
   1008      1.1  dholland 		KDTRACE_NFS_ACCESSCACHE_FLUSH_DONE(vp);
   1009      1.1  dholland 	}
   1010      1.1  dholland 	error = nfsrpc_setattr(vp, vap, NULL, cred, td, &nfsva, &attrflag,
   1011      1.1  dholland 	    NULL);
   1012      1.1  dholland 	if (attrflag) {
   1013      1.1  dholland 		ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
   1014      1.1  dholland 		if (ret && !error)
   1015      1.1  dholland 			error = ret;
   1016      1.1  dholland 	}
   1017      1.1  dholland 	if (error && NFS_ISV4(vp))
   1018      1.1  dholland 		error = nfscl_maperr(td, error, vap->va_uid, vap->va_gid);
   1019      1.1  dholland 	return (error);
   1020      1.1  dholland }
   1021      1.1  dholland 
   1022      1.1  dholland /*
   1023      1.1  dholland  * nfs lookup call, one step at a time...
   1024      1.1  dholland  * First look in cache
   1025      1.1  dholland  * If not found, unlock the directory nfsnode and do the rpc
   1026      1.1  dholland  */
   1027      1.1  dholland static int
   1028      1.1  dholland nfs_lookup(struct vop_lookup_args *ap)
   1029      1.1  dholland {
   1030      1.1  dholland 	struct componentname *cnp = ap->a_cnp;
   1031      1.1  dholland 	struct vnode *dvp = ap->a_dvp;
   1032      1.1  dholland 	struct vnode **vpp = ap->a_vpp;
   1033      1.1  dholland 	struct mount *mp = dvp->v_mount;
   1034      1.1  dholland 	int flags = cnp->cn_flags;
   1035      1.1  dholland 	struct vnode *newvp;
   1036      1.1  dholland 	struct nfsmount *nmp;
   1037      1.1  dholland 	struct nfsnode *np, *newnp;
   1038      1.1  dholland 	int error = 0, attrflag, dattrflag, ltype, ncticks;
   1039      1.1  dholland 	struct thread *td = cnp->cn_thread;
   1040      1.1  dholland 	struct nfsfh *nfhp;
   1041      1.1  dholland 	struct nfsvattr dnfsva, nfsva;
   1042      1.1  dholland 	struct vattr vattr;
   1043      1.1  dholland 	struct timespec nctime;
   1044      1.1  dholland 
   1045      1.1  dholland 	*vpp = NULLVP;
   1046      1.1  dholland 	if ((flags & ISLASTCN) && (mp->mnt_flag & MNT_RDONLY) &&
   1047      1.1  dholland 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
   1048      1.1  dholland 		return (EROFS);
   1049      1.1  dholland 	if (dvp->v_type != VDIR)
   1050      1.1  dholland 		return (ENOTDIR);
   1051      1.1  dholland 	nmp = VFSTONFS(mp);
   1052      1.1  dholland 	np = VTONFS(dvp);
   1053      1.1  dholland 
   1054      1.1  dholland 	/* For NFSv4, wait until any remove is done. */
   1055      1.1  dholland 	mtx_lock(&np->n_mtx);
   1056      1.1  dholland 	while (NFSHASNFSV4(nmp) && (np->n_flag & NREMOVEINPROG)) {
   1057      1.1  dholland 		np->n_flag |= NREMOVEWANT;
   1058      1.1  dholland 		(void) msleep((caddr_t)np, &np->n_mtx, PZERO, "nfslkup", 0);
   1059      1.1  dholland 	}
   1060      1.1  dholland 	mtx_unlock(&np->n_mtx);
   1061      1.1  dholland 
   1062      1.1  dholland 	if ((error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td)) != 0)
   1063      1.1  dholland 		return (error);
   1064      1.1  dholland 	error = cache_lookup(dvp, vpp, cnp, &nctime, &ncticks);
   1065      1.1  dholland 	if (error > 0 && error != ENOENT)
   1066      1.1  dholland 		return (error);
   1067      1.1  dholland 	if (error == -1) {
   1068      1.1  dholland 		/*
   1069      1.1  dholland 		 * Lookups of "." are special and always return the
   1070      1.1  dholland 		 * current directory.  cache_lookup() already handles
   1071      1.1  dholland 		 * associated locking bookkeeping, etc.
   1072      1.1  dholland 		 */
   1073      1.1  dholland 		if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
   1074      1.1  dholland 			/* XXX: Is this really correct? */
   1075      1.1  dholland 			if (cnp->cn_nameiop != LOOKUP &&
   1076      1.1  dholland 			    (flags & ISLASTCN))
   1077      1.1  dholland 				cnp->cn_flags |= SAVENAME;
   1078      1.1  dholland 			return (0);
   1079      1.1  dholland 		}
   1080      1.1  dholland 
   1081      1.1  dholland 		/*
   1082      1.1  dholland 		 * We only accept a positive hit in the cache if the
   1083      1.1  dholland 		 * change time of the file matches our cached copy.
   1084      1.1  dholland 		 * Otherwise, we discard the cache entry and fallback
   1085      1.1  dholland 		 * to doing a lookup RPC.  We also only trust cache
   1086      1.1  dholland 		 * entries for less than nm_nametimeo seconds.
   1087      1.1  dholland 		 *
   1088      1.1  dholland 		 * To better handle stale file handles and attributes,
   1089      1.1  dholland 		 * clear the attribute cache of this node if it is a
   1090      1.1  dholland 		 * leaf component, part of an open() call, and not
   1091      1.1  dholland 		 * locally modified before fetching the attributes.
   1092      1.1  dholland 		 * This should allow stale file handles to be detected
   1093      1.1  dholland 		 * here where we can fall back to a LOOKUP RPC to
   1094      1.1  dholland 		 * recover rather than having nfs_open() detect the
   1095      1.1  dholland 		 * stale file handle and failing open(2) with ESTALE.
   1096      1.1  dholland 		 */
   1097      1.1  dholland 		newvp = *vpp;
   1098      1.1  dholland 		newnp = VTONFS(newvp);
   1099      1.1  dholland 		if (!(nmp->nm_flag & NFSMNT_NOCTO) &&
   1100      1.1  dholland 		    (flags & (ISLASTCN | ISOPEN)) == (ISLASTCN | ISOPEN) &&
   1101      1.1  dholland 		    !(newnp->n_flag & NMODIFIED)) {
   1102      1.1  dholland 			mtx_lock(&newnp->n_mtx);
   1103      1.1  dholland 			newnp->n_attrstamp = 0;
   1104      1.1  dholland 			KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(newvp);
   1105      1.1  dholland 			mtx_unlock(&newnp->n_mtx);
   1106      1.1  dholland 		}
   1107      1.1  dholland 		if (nfscl_nodeleg(newvp, 0) == 0 ||
   1108      1.1  dholland 		    ((u_int)(ticks - ncticks) < (nmp->nm_nametimeo * hz) &&
   1109      1.1  dholland 		    VOP_GETATTR(newvp, &vattr, cnp->cn_cred) == 0 &&
   1110      1.1  dholland 		    timespeccmp(&vattr.va_ctime, &nctime, ==))) {
   1111  1.1.1.2  pgoyette 			NFSINCRGLOBAL(nfsstatsv1.lookupcache_hits);
   1112      1.1  dholland 			if (cnp->cn_nameiop != LOOKUP &&
   1113      1.1  dholland 			    (flags & ISLASTCN))
   1114      1.1  dholland 				cnp->cn_flags |= SAVENAME;
   1115      1.1  dholland 			return (0);
   1116      1.1  dholland 		}
   1117      1.1  dholland 		cache_purge(newvp);
   1118      1.1  dholland 		if (dvp != newvp)
   1119      1.1  dholland 			vput(newvp);
   1120      1.1  dholland 		else
   1121      1.1  dholland 			vrele(newvp);
   1122      1.1  dholland 		*vpp = NULLVP;
   1123      1.1  dholland 	} else if (error == ENOENT) {
   1124      1.1  dholland 		if (dvp->v_iflag & VI_DOOMED)
   1125      1.1  dholland 			return (ENOENT);
   1126      1.1  dholland 		/*
   1127      1.1  dholland 		 * We only accept a negative hit in the cache if the
   1128      1.1  dholland 		 * modification time of the parent directory matches
   1129      1.1  dholland 		 * the cached copy in the name cache entry.
   1130      1.1  dholland 		 * Otherwise, we discard all of the negative cache
   1131      1.1  dholland 		 * entries for this directory.  We also only trust
   1132      1.1  dholland 		 * negative cache entries for up to nm_negnametimeo
   1133      1.1  dholland 		 * seconds.
   1134      1.1  dholland 		 */
   1135      1.1  dholland 		if ((u_int)(ticks - ncticks) < (nmp->nm_negnametimeo * hz) &&
   1136      1.1  dholland 		    VOP_GETATTR(dvp, &vattr, cnp->cn_cred) == 0 &&
   1137      1.1  dholland 		    timespeccmp(&vattr.va_mtime, &nctime, ==)) {
   1138  1.1.1.2  pgoyette 			NFSINCRGLOBAL(nfsstatsv1.lookupcache_hits);
   1139      1.1  dholland 			return (ENOENT);
   1140      1.1  dholland 		}
   1141      1.1  dholland 		cache_purge_negative(dvp);
   1142      1.1  dholland 	}
   1143      1.1  dholland 
   1144      1.1  dholland 	error = 0;
   1145      1.1  dholland 	newvp = NULLVP;
   1146  1.1.1.2  pgoyette 	NFSINCRGLOBAL(nfsstatsv1.lookupcache_misses);
   1147      1.1  dholland 	error = nfsrpc_lookup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
   1148      1.1  dholland 	    cnp->cn_cred, td, &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
   1149      1.1  dholland 	    NULL);
   1150      1.1  dholland 	if (dattrflag)
   1151      1.1  dholland 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
   1152      1.1  dholland 	if (error) {
   1153      1.1  dholland 		if (newvp != NULLVP) {
   1154      1.1  dholland 			vput(newvp);
   1155      1.1  dholland 			*vpp = NULLVP;
   1156      1.1  dholland 		}
   1157      1.1  dholland 
   1158      1.1  dholland 		if (error != ENOENT) {
   1159      1.1  dholland 			if (NFS_ISV4(dvp))
   1160      1.1  dholland 				error = nfscl_maperr(td, error, (uid_t)0,
   1161      1.1  dholland 				    (gid_t)0);
   1162      1.1  dholland 			return (error);
   1163      1.1  dholland 		}
   1164      1.1  dholland 
   1165      1.1  dholland 		/* The requested file was not found. */
   1166      1.1  dholland 		if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
   1167      1.1  dholland 		    (flags & ISLASTCN)) {
   1168      1.1  dholland 			/*
   1169      1.1  dholland 			 * XXX: UFS does a full VOP_ACCESS(dvp,
   1170      1.1  dholland 			 * VWRITE) here instead of just checking
   1171      1.1  dholland 			 * MNT_RDONLY.
   1172      1.1  dholland 			 */
   1173      1.1  dholland 			if (mp->mnt_flag & MNT_RDONLY)
   1174      1.1  dholland 				return (EROFS);
   1175      1.1  dholland 			cnp->cn_flags |= SAVENAME;
   1176      1.1  dholland 			return (EJUSTRETURN);
   1177      1.1  dholland 		}
   1178      1.1  dholland 
   1179  1.1.1.2  pgoyette 		if ((cnp->cn_flags & MAKEENTRY) != 0 && dattrflag) {
   1180      1.1  dholland 			/*
   1181      1.1  dholland 			 * Cache the modification time of the parent
   1182      1.1  dholland 			 * directory from the post-op attributes in
   1183      1.1  dholland 			 * the name cache entry.  The negative cache
   1184      1.1  dholland 			 * entry will be ignored once the directory
   1185      1.1  dholland 			 * has changed.  Don't bother adding the entry
   1186      1.1  dholland 			 * if the directory has already changed.
   1187      1.1  dholland 			 */
   1188      1.1  dholland 			mtx_lock(&np->n_mtx);
   1189      1.1  dholland 			if (timespeccmp(&np->n_vattr.na_mtime,
   1190      1.1  dholland 			    &dnfsva.na_mtime, ==)) {
   1191      1.1  dholland 				mtx_unlock(&np->n_mtx);
   1192      1.1  dholland 				cache_enter_time(dvp, NULL, cnp,
   1193      1.1  dholland 				    &dnfsva.na_mtime, NULL);
   1194      1.1  dholland 			} else
   1195      1.1  dholland 				mtx_unlock(&np->n_mtx);
   1196      1.1  dholland 		}
   1197      1.1  dholland 		return (ENOENT);
   1198      1.1  dholland 	}
   1199      1.1  dholland 
   1200      1.1  dholland 	/*
   1201      1.1  dholland 	 * Handle RENAME case...
   1202      1.1  dholland 	 */
   1203      1.1  dholland 	if (cnp->cn_nameiop == RENAME && (flags & ISLASTCN)) {
   1204      1.1  dholland 		if (NFS_CMPFH(np, nfhp->nfh_fh, nfhp->nfh_len)) {
   1205      1.1  dholland 			FREE((caddr_t)nfhp, M_NFSFH);
   1206      1.1  dholland 			return (EISDIR);
   1207      1.1  dholland 		}
   1208      1.1  dholland 		error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL,
   1209      1.1  dholland 		    LK_EXCLUSIVE);
   1210      1.1  dholland 		if (error)
   1211      1.1  dholland 			return (error);
   1212      1.1  dholland 		newvp = NFSTOV(np);
   1213      1.1  dholland 		if (attrflag)
   1214      1.1  dholland 			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
   1215      1.1  dholland 			    0, 1);
   1216      1.1  dholland 		*vpp = newvp;
   1217      1.1  dholland 		cnp->cn_flags |= SAVENAME;
   1218      1.1  dholland 		return (0);
   1219      1.1  dholland 	}
   1220      1.1  dholland 
   1221      1.1  dholland 	if (flags & ISDOTDOT) {
   1222      1.1  dholland 		ltype = NFSVOPISLOCKED(dvp);
   1223      1.1  dholland 		error = vfs_busy(mp, MBF_NOWAIT);
   1224      1.1  dholland 		if (error != 0) {
   1225      1.1  dholland 			vfs_ref(mp);
   1226      1.1  dholland 			NFSVOPUNLOCK(dvp, 0);
   1227      1.1  dholland 			error = vfs_busy(mp, 0);
   1228      1.1  dholland 			NFSVOPLOCK(dvp, ltype | LK_RETRY);
   1229      1.1  dholland 			vfs_rel(mp);
   1230      1.1  dholland 			if (error == 0 && (dvp->v_iflag & VI_DOOMED)) {
   1231      1.1  dholland 				vfs_unbusy(mp);
   1232      1.1  dholland 				error = ENOENT;
   1233      1.1  dholland 			}
   1234      1.1  dholland 			if (error != 0)
   1235      1.1  dholland 				return (error);
   1236      1.1  dholland 		}
   1237      1.1  dholland 		NFSVOPUNLOCK(dvp, 0);
   1238      1.1  dholland 		error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL,
   1239      1.1  dholland 		    cnp->cn_lkflags);
   1240      1.1  dholland 		if (error == 0)
   1241      1.1  dholland 			newvp = NFSTOV(np);
   1242      1.1  dholland 		vfs_unbusy(mp);
   1243      1.1  dholland 		if (newvp != dvp)
   1244      1.1  dholland 			NFSVOPLOCK(dvp, ltype | LK_RETRY);
   1245      1.1  dholland 		if (dvp->v_iflag & VI_DOOMED) {
   1246      1.1  dholland 			if (error == 0) {
   1247      1.1  dholland 				if (newvp == dvp)
   1248      1.1  dholland 					vrele(newvp);
   1249      1.1  dholland 				else
   1250      1.1  dholland 					vput(newvp);
   1251      1.1  dholland 			}
   1252      1.1  dholland 			error = ENOENT;
   1253      1.1  dholland 		}
   1254      1.1  dholland 		if (error != 0)
   1255      1.1  dholland 			return (error);
   1256      1.1  dholland 		if (attrflag)
   1257      1.1  dholland 			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
   1258      1.1  dholland 			    0, 1);
   1259      1.1  dholland 	} else if (NFS_CMPFH(np, nfhp->nfh_fh, nfhp->nfh_len)) {
   1260      1.1  dholland 		FREE((caddr_t)nfhp, M_NFSFH);
   1261      1.1  dholland 		VREF(dvp);
   1262      1.1  dholland 		newvp = dvp;
   1263      1.1  dholland 		if (attrflag)
   1264      1.1  dholland 			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
   1265      1.1  dholland 			    0, 1);
   1266      1.1  dholland 	} else {
   1267      1.1  dholland 		error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL,
   1268      1.1  dholland 		    cnp->cn_lkflags);
   1269      1.1  dholland 		if (error)
   1270      1.1  dholland 			return (error);
   1271      1.1  dholland 		newvp = NFSTOV(np);
   1272      1.1  dholland 		if (attrflag)
   1273      1.1  dholland 			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
   1274      1.1  dholland 			    0, 1);
   1275      1.1  dholland 		else if ((flags & (ISLASTCN | ISOPEN)) == (ISLASTCN | ISOPEN) &&
   1276      1.1  dholland 		    !(np->n_flag & NMODIFIED)) {
   1277      1.1  dholland 			/*
   1278      1.1  dholland 			 * Flush the attribute cache when opening a
   1279      1.1  dholland 			 * leaf node to ensure that fresh attributes
   1280      1.1  dholland 			 * are fetched in nfs_open() since we did not
   1281      1.1  dholland 			 * fetch attributes from the LOOKUP reply.
   1282      1.1  dholland 			 */
   1283      1.1  dholland 			mtx_lock(&np->n_mtx);
   1284      1.1  dholland 			np->n_attrstamp = 0;
   1285      1.1  dholland 			KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(newvp);
   1286      1.1  dholland 			mtx_unlock(&np->n_mtx);
   1287      1.1  dholland 		}
   1288      1.1  dholland 	}
   1289      1.1  dholland 	if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
   1290      1.1  dholland 		cnp->cn_flags |= SAVENAME;
   1291      1.1  dholland 	if ((cnp->cn_flags & MAKEENTRY) &&
   1292      1.1  dholland 	    (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN)) &&
   1293      1.1  dholland 	    attrflag != 0 && (newvp->v_type != VDIR || dattrflag != 0))
   1294      1.1  dholland 		cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
   1295      1.1  dholland 		    newvp->v_type != VDIR ? NULL : &dnfsva.na_ctime);
   1296      1.1  dholland 	*vpp = newvp;
   1297      1.1  dholland 	return (0);
   1298      1.1  dholland }
   1299      1.1  dholland 
   1300      1.1  dholland /*
   1301      1.1  dholland  * nfs read call.
   1302      1.1  dholland  * Just call ncl_bioread() to do the work.
   1303      1.1  dholland  */
   1304      1.1  dholland static int
   1305      1.1  dholland nfs_read(struct vop_read_args *ap)
   1306      1.1  dholland {
   1307      1.1  dholland 	struct vnode *vp = ap->a_vp;
   1308      1.1  dholland 
   1309      1.1  dholland 	switch (vp->v_type) {
   1310      1.1  dholland 	case VREG:
   1311      1.1  dholland 		return (ncl_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred));
   1312      1.1  dholland 	case VDIR:
   1313      1.1  dholland 		return (EISDIR);
   1314      1.1  dholland 	default:
   1315      1.1  dholland 		return (EOPNOTSUPP);
   1316      1.1  dholland 	}
   1317      1.1  dholland }
   1318      1.1  dholland 
   1319      1.1  dholland /*
   1320      1.1  dholland  * nfs readlink call
   1321      1.1  dholland  */
   1322      1.1  dholland static int
   1323      1.1  dholland nfs_readlink(struct vop_readlink_args *ap)
   1324      1.1  dholland {
   1325      1.1  dholland 	struct vnode *vp = ap->a_vp;
   1326      1.1  dholland 
   1327      1.1  dholland 	if (vp->v_type != VLNK)
   1328      1.1  dholland 		return (EINVAL);
   1329      1.1  dholland 	return (ncl_bioread(vp, ap->a_uio, 0, ap->a_cred));
   1330      1.1  dholland }
   1331      1.1  dholland 
   1332      1.1  dholland /*
   1333      1.1  dholland  * Do a readlink rpc.
   1334      1.1  dholland  * Called by ncl_doio() from below the buffer cache.
   1335      1.1  dholland  */
   1336      1.1  dholland int
   1337      1.1  dholland ncl_readlinkrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
   1338      1.1  dholland {
   1339      1.1  dholland 	int error, ret, attrflag;
   1340      1.1  dholland 	struct nfsvattr nfsva;
   1341      1.1  dholland 
   1342      1.1  dholland 	error = nfsrpc_readlink(vp, uiop, cred, uiop->uio_td, &nfsva,
   1343      1.1  dholland 	    &attrflag, NULL);
   1344      1.1  dholland 	if (attrflag) {
   1345      1.1  dholland 		ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
   1346      1.1  dholland 		if (ret && !error)
   1347      1.1  dholland 			error = ret;
   1348      1.1  dholland 	}
   1349      1.1  dholland 	if (error && NFS_ISV4(vp))
   1350      1.1  dholland 		error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
   1351      1.1  dholland 	return (error);
   1352      1.1  dholland }
   1353      1.1  dholland 
   1354      1.1  dholland /*
   1355      1.1  dholland  * nfs read rpc call
   1356      1.1  dholland  * Ditto above
   1357      1.1  dholland  */
   1358      1.1  dholland int
   1359      1.1  dholland ncl_readrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
   1360      1.1  dholland {
   1361      1.1  dholland 	int error, ret, attrflag;
   1362      1.1  dholland 	struct nfsvattr nfsva;
   1363      1.1  dholland 	struct nfsmount *nmp;
   1364      1.1  dholland 
   1365      1.1  dholland 	nmp = VFSTONFS(vnode_mount(vp));
   1366      1.1  dholland 	error = EIO;
   1367      1.1  dholland 	attrflag = 0;
   1368      1.1  dholland 	if (NFSHASPNFS(nmp))
   1369      1.1  dholland 		error = nfscl_doiods(vp, uiop, NULL, NULL,
   1370      1.1  dholland 		    NFSV4OPEN_ACCESSREAD, cred, uiop->uio_td);
   1371      1.1  dholland 	NFSCL_DEBUG(4, "readrpc: aft doiods=%d\n", error);
   1372      1.1  dholland 	if (error != 0)
   1373      1.1  dholland 		error = nfsrpc_read(vp, uiop, cred, uiop->uio_td, &nfsva,
   1374      1.1  dholland 		    &attrflag, NULL);
   1375      1.1  dholland 	if (attrflag) {
   1376      1.1  dholland 		ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
   1377      1.1  dholland 		if (ret && !error)
   1378      1.1  dholland 			error = ret;
   1379      1.1  dholland 	}
   1380      1.1  dholland 	if (error && NFS_ISV4(vp))
   1381      1.1  dholland 		error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
   1382      1.1  dholland 	return (error);
   1383      1.1  dholland }
   1384      1.1  dholland 
   1385      1.1  dholland /*
   1386      1.1  dholland  * nfs write call
   1387      1.1  dholland  */
   1388      1.1  dholland int
   1389      1.1  dholland ncl_writerpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
   1390      1.1  dholland     int *iomode, int *must_commit, int called_from_strategy)
   1391      1.1  dholland {
   1392      1.1  dholland 	struct nfsvattr nfsva;
   1393      1.1  dholland 	int error, attrflag, ret;
   1394      1.1  dholland 	struct nfsmount *nmp;
   1395      1.1  dholland 
   1396      1.1  dholland 	nmp = VFSTONFS(vnode_mount(vp));
   1397      1.1  dholland 	error = EIO;
   1398      1.1  dholland 	attrflag = 0;
   1399      1.1  dholland 	if (NFSHASPNFS(nmp))
   1400      1.1  dholland 		error = nfscl_doiods(vp, uiop, iomode, must_commit,
   1401      1.1  dholland 		    NFSV4OPEN_ACCESSWRITE, cred, uiop->uio_td);
   1402      1.1  dholland 	NFSCL_DEBUG(4, "writerpc: aft doiods=%d\n", error);
   1403      1.1  dholland 	if (error != 0)
   1404      1.1  dholland 		error = nfsrpc_write(vp, uiop, iomode, must_commit, cred,
   1405      1.1  dholland 		    uiop->uio_td, &nfsva, &attrflag, NULL,
   1406      1.1  dholland 		    called_from_strategy);
   1407      1.1  dholland 	if (attrflag) {
   1408      1.1  dholland 		if (VTONFS(vp)->n_flag & ND_NFSV4)
   1409      1.1  dholland 			ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 1,
   1410      1.1  dholland 			    1);
   1411      1.1  dholland 		else
   1412      1.1  dholland 			ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0,
   1413      1.1  dholland 			    1);
   1414      1.1  dholland 		if (ret && !error)
   1415      1.1  dholland 			error = ret;
   1416      1.1  dholland 	}
   1417      1.1  dholland 	if (DOINGASYNC(vp))
   1418      1.1  dholland 		*iomode = NFSWRITE_FILESYNC;
   1419      1.1  dholland 	if (error && NFS_ISV4(vp))
   1420      1.1  dholland 		error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
   1421      1.1  dholland 	return (error);
   1422      1.1  dholland }
   1423      1.1  dholland 
   1424      1.1  dholland /*
   1425      1.1  dholland  * nfs mknod rpc
   1426      1.1  dholland  * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
   1427      1.1  dholland  * mode set to specify the file type and the size field for rdev.
   1428      1.1  dholland  */
   1429      1.1  dholland static int
   1430      1.1  dholland nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
   1431      1.1  dholland     struct vattr *vap)
   1432      1.1  dholland {
   1433      1.1  dholland 	struct nfsvattr nfsva, dnfsva;
   1434      1.1  dholland 	struct vnode *newvp = NULL;
   1435      1.1  dholland 	struct nfsnode *np = NULL, *dnp;
   1436      1.1  dholland 	struct nfsfh *nfhp;
   1437      1.1  dholland 	struct vattr vattr;
   1438      1.1  dholland 	int error = 0, attrflag, dattrflag;
   1439      1.1  dholland 	u_int32_t rdev;
   1440      1.1  dholland 
   1441      1.1  dholland 	if (vap->va_type == VCHR || vap->va_type == VBLK)
   1442      1.1  dholland 		rdev = vap->va_rdev;
   1443      1.1  dholland 	else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
   1444      1.1  dholland 		rdev = 0xffffffff;
   1445      1.1  dholland 	else
   1446      1.1  dholland 		return (EOPNOTSUPP);
   1447      1.1  dholland 	if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
   1448      1.1  dholland 		return (error);
   1449      1.1  dholland 	error = nfsrpc_mknod(dvp, cnp->cn_nameptr, cnp->cn_namelen, vap,
   1450      1.1  dholland 	    rdev, vap->va_type, cnp->cn_cred, cnp->cn_thread, &dnfsva,
   1451      1.1  dholland 	    &nfsva, &nfhp, &attrflag, &dattrflag, NULL);
   1452      1.1  dholland 	if (!error) {
   1453      1.1  dholland 		if (!nfhp)
   1454      1.1  dholland 			(void) nfsrpc_lookup(dvp, cnp->cn_nameptr,
   1455      1.1  dholland 			    cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread,
   1456      1.1  dholland 			    &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
   1457      1.1  dholland 			    NULL);
   1458      1.1  dholland 		if (nfhp)
   1459      1.1  dholland 			error = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp,
   1460      1.1  dholland 			    cnp->cn_thread, &np, NULL, LK_EXCLUSIVE);
   1461      1.1  dholland 	}
   1462      1.1  dholland 	if (dattrflag)
   1463      1.1  dholland 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
   1464      1.1  dholland 	if (!error) {
   1465      1.1  dholland 		newvp = NFSTOV(np);
   1466      1.1  dholland 		if (attrflag != 0) {
   1467      1.1  dholland 			error = nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
   1468      1.1  dholland 			    0, 1);
   1469      1.1  dholland 			if (error != 0)
   1470      1.1  dholland 				vput(newvp);
   1471      1.1  dholland 		}
   1472      1.1  dholland 	}
   1473      1.1  dholland 	if (!error) {
   1474      1.1  dholland 		*vpp = newvp;
   1475      1.1  dholland 	} else if (NFS_ISV4(dvp)) {
   1476      1.1  dholland 		error = nfscl_maperr(cnp->cn_thread, error, vap->va_uid,
   1477      1.1  dholland 		    vap->va_gid);
   1478      1.1  dholland 	}
   1479      1.1  dholland 	dnp = VTONFS(dvp);
   1480      1.1  dholland 	mtx_lock(&dnp->n_mtx);
   1481      1.1  dholland 	dnp->n_flag |= NMODIFIED;
   1482      1.1  dholland 	if (!dattrflag) {
   1483      1.1  dholland 		dnp->n_attrstamp = 0;
   1484      1.1  dholland 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
   1485      1.1  dholland 	}
   1486      1.1  dholland 	mtx_unlock(&dnp->n_mtx);
   1487      1.1  dholland 	return (error);
   1488      1.1  dholland }
   1489      1.1  dholland 
   1490      1.1  dholland /*
   1491      1.1  dholland  * nfs mknod vop
   1492      1.1  dholland  * just call nfs_mknodrpc() to do the work.
   1493      1.1  dholland  */
   1494      1.1  dholland /* ARGSUSED */
   1495      1.1  dholland static int
   1496      1.1  dholland nfs_mknod(struct vop_mknod_args *ap)
   1497      1.1  dholland {
   1498      1.1  dholland 	return (nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap));
   1499      1.1  dholland }
   1500      1.1  dholland 
   1501      1.1  dholland static struct mtx nfs_cverf_mtx;
   1502      1.1  dholland MTX_SYSINIT(nfs_cverf_mtx, &nfs_cverf_mtx, "NFS create verifier mutex",
   1503      1.1  dholland     MTX_DEF);
   1504      1.1  dholland 
   1505      1.1  dholland static nfsquad_t
   1506      1.1  dholland nfs_get_cverf(void)
   1507      1.1  dholland {
   1508      1.1  dholland 	static nfsquad_t cverf;
   1509      1.1  dholland 	nfsquad_t ret;
   1510      1.1  dholland 	static int cverf_initialized = 0;
   1511      1.1  dholland 
   1512      1.1  dholland 	mtx_lock(&nfs_cverf_mtx);
   1513      1.1  dholland 	if (cverf_initialized == 0) {
   1514      1.1  dholland 		cverf.lval[0] = arc4random();
   1515      1.1  dholland 		cverf.lval[1] = arc4random();
   1516      1.1  dholland 		cverf_initialized = 1;
   1517      1.1  dholland 	} else
   1518      1.1  dholland 		cverf.qval++;
   1519      1.1  dholland 	ret = cverf;
   1520      1.1  dholland 	mtx_unlock(&nfs_cverf_mtx);
   1521      1.1  dholland 
   1522      1.1  dholland 	return (ret);
   1523      1.1  dholland }
   1524      1.1  dholland 
   1525      1.1  dholland /*
   1526      1.1  dholland  * nfs file create call
   1527      1.1  dholland  */
   1528      1.1  dholland static int
   1529      1.1  dholland nfs_create(struct vop_create_args *ap)
   1530      1.1  dholland {
   1531      1.1  dholland 	struct vnode *dvp = ap->a_dvp;
   1532      1.1  dholland 	struct vattr *vap = ap->a_vap;
   1533      1.1  dholland 	struct componentname *cnp = ap->a_cnp;
   1534      1.1  dholland 	struct nfsnode *np = NULL, *dnp;
   1535      1.1  dholland 	struct vnode *newvp = NULL;
   1536      1.1  dholland 	struct nfsmount *nmp;
   1537      1.1  dholland 	struct nfsvattr dnfsva, nfsva;
   1538      1.1  dholland 	struct nfsfh *nfhp;
   1539      1.1  dholland 	nfsquad_t cverf;
   1540      1.1  dholland 	int error = 0, attrflag, dattrflag, fmode = 0;
   1541      1.1  dholland 	struct vattr vattr;
   1542      1.1  dholland 
   1543      1.1  dholland 	/*
   1544      1.1  dholland 	 * Oops, not for me..
   1545      1.1  dholland 	 */
   1546      1.1  dholland 	if (vap->va_type == VSOCK)
   1547      1.1  dholland 		return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
   1548      1.1  dholland 
   1549      1.1  dholland 	if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
   1550      1.1  dholland 		return (error);
   1551      1.1  dholland 	if (vap->va_vaflags & VA_EXCLUSIVE)
   1552      1.1  dholland 		fmode |= O_EXCL;
   1553      1.1  dholland 	dnp = VTONFS(dvp);
   1554      1.1  dholland 	nmp = VFSTONFS(vnode_mount(dvp));
   1555      1.1  dholland again:
   1556      1.1  dholland 	/* For NFSv4, wait until any remove is done. */
   1557      1.1  dholland 	mtx_lock(&dnp->n_mtx);
   1558      1.1  dholland 	while (NFSHASNFSV4(nmp) && (dnp->n_flag & NREMOVEINPROG)) {
   1559      1.1  dholland 		dnp->n_flag |= NREMOVEWANT;
   1560      1.1  dholland 		(void) msleep((caddr_t)dnp, &dnp->n_mtx, PZERO, "nfscrt", 0);
   1561      1.1  dholland 	}
   1562      1.1  dholland 	mtx_unlock(&dnp->n_mtx);
   1563      1.1  dholland 
   1564      1.1  dholland 	cverf = nfs_get_cverf();
   1565      1.1  dholland 	error = nfsrpc_create(dvp, cnp->cn_nameptr, cnp->cn_namelen,
   1566      1.1  dholland 	    vap, cverf, fmode, cnp->cn_cred, cnp->cn_thread, &dnfsva, &nfsva,
   1567      1.1  dholland 	    &nfhp, &attrflag, &dattrflag, NULL);
   1568      1.1  dholland 	if (!error) {
   1569      1.1  dholland 		if (nfhp == NULL)
   1570      1.1  dholland 			(void) nfsrpc_lookup(dvp, cnp->cn_nameptr,
   1571      1.1  dholland 			    cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread,
   1572      1.1  dholland 			    &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
   1573      1.1  dholland 			    NULL);
   1574      1.1  dholland 		if (nfhp != NULL)
   1575      1.1  dholland 			error = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp,
   1576      1.1  dholland 			    cnp->cn_thread, &np, NULL, LK_EXCLUSIVE);
   1577      1.1  dholland 	}
   1578      1.1  dholland 	if (dattrflag)
   1579      1.1  dholland 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
   1580      1.1  dholland 	if (!error) {
   1581      1.1  dholland 		newvp = NFSTOV(np);
   1582      1.1  dholland 		if (attrflag == 0)
   1583      1.1  dholland 			error = nfsrpc_getattr(newvp, cnp->cn_cred,
   1584      1.1  dholland 			    cnp->cn_thread, &nfsva, NULL);
   1585      1.1  dholland 		if (error == 0)
   1586      1.1  dholland 			error = nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
   1587      1.1  dholland 			    0, 1);
   1588      1.1  dholland 	}
   1589      1.1  dholland 	if (error) {
   1590      1.1  dholland 		if (newvp != NULL) {
   1591      1.1  dholland 			vput(newvp);
   1592      1.1  dholland 			newvp = NULL;
   1593      1.1  dholland 		}
   1594      1.1  dholland 		if (NFS_ISV34(dvp) && (fmode & O_EXCL) &&
   1595      1.1  dholland 		    error == NFSERR_NOTSUPP) {
   1596      1.1  dholland 			fmode &= ~O_EXCL;
   1597      1.1  dholland 			goto again;
   1598      1.1  dholland 		}
   1599      1.1  dholland 	} else if (NFS_ISV34(dvp) && (fmode & O_EXCL)) {
   1600      1.1  dholland 		if (nfscl_checksattr(vap, &nfsva)) {
   1601      1.1  dholland 			error = nfsrpc_setattr(newvp, vap, NULL, cnp->cn_cred,
   1602      1.1  dholland 			    cnp->cn_thread, &nfsva, &attrflag, NULL);
   1603      1.1  dholland 			if (error && (vap->va_uid != (uid_t)VNOVAL ||
   1604      1.1  dholland 			    vap->va_gid != (gid_t)VNOVAL)) {
   1605      1.1  dholland 				/* try again without setting uid/gid */
   1606      1.1  dholland 				vap->va_uid = (uid_t)VNOVAL;
   1607      1.1  dholland 				vap->va_gid = (uid_t)VNOVAL;
   1608      1.1  dholland 				error = nfsrpc_setattr(newvp, vap, NULL,
   1609      1.1  dholland 				    cnp->cn_cred, cnp->cn_thread, &nfsva,
   1610      1.1  dholland 				    &attrflag, NULL);
   1611      1.1  dholland 			}
   1612      1.1  dholland 			if (attrflag)
   1613      1.1  dholland 				(void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
   1614      1.1  dholland 				    NULL, 0, 1);
   1615      1.1  dholland 			if (error != 0)
   1616      1.1  dholland 				vput(newvp);
   1617      1.1  dholland 		}
   1618      1.1  dholland 	}
   1619      1.1  dholland 	if (!error) {
   1620      1.1  dholland 		if ((cnp->cn_flags & MAKEENTRY) && attrflag)
   1621      1.1  dholland 			cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
   1622      1.1  dholland 			    NULL);
   1623      1.1  dholland 		*ap->a_vpp = newvp;
   1624      1.1  dholland 	} else if (NFS_ISV4(dvp)) {
   1625      1.1  dholland 		error = nfscl_maperr(cnp->cn_thread, error, vap->va_uid,
   1626      1.1  dholland 		    vap->va_gid);
   1627      1.1  dholland 	}
   1628      1.1  dholland 	mtx_lock(&dnp->n_mtx);
   1629      1.1  dholland 	dnp->n_flag |= NMODIFIED;
   1630      1.1  dholland 	if (!dattrflag) {
   1631      1.1  dholland 		dnp->n_attrstamp = 0;
   1632      1.1  dholland 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
   1633      1.1  dholland 	}
   1634      1.1  dholland 	mtx_unlock(&dnp->n_mtx);
   1635      1.1  dholland 	return (error);
   1636      1.1  dholland }
   1637      1.1  dholland 
   1638      1.1  dholland /*
   1639      1.1  dholland  * nfs file remove call
   1640      1.1  dholland  * To try and make nfs semantics closer to ufs semantics, a file that has
   1641      1.1  dholland  * other processes using the vnode is renamed instead of removed and then
   1642      1.1  dholland  * removed later on the last close.
   1643      1.1  dholland  * - If v_usecount > 1
   1644      1.1  dholland  *	  If a rename is not already in the works
   1645      1.1  dholland  *	     call nfs_sillyrename() to set it up
   1646      1.1  dholland  *     else
   1647      1.1  dholland  *	  do the remove rpc
   1648      1.1  dholland  */
   1649      1.1  dholland static int
   1650      1.1  dholland nfs_remove(struct vop_remove_args *ap)
   1651      1.1  dholland {
   1652      1.1  dholland 	struct vnode *vp = ap->a_vp;
   1653      1.1  dholland 	struct vnode *dvp = ap->a_dvp;
   1654      1.1  dholland 	struct componentname *cnp = ap->a_cnp;
   1655      1.1  dholland 	struct nfsnode *np = VTONFS(vp);
   1656      1.1  dholland 	int error = 0;
   1657      1.1  dholland 	struct vattr vattr;
   1658      1.1  dholland 
   1659      1.1  dholland 	KASSERT((cnp->cn_flags & HASBUF) != 0, ("nfs_remove: no name"));
   1660      1.1  dholland 	KASSERT(vrefcnt(vp) > 0, ("nfs_remove: bad v_usecount"));
   1661      1.1  dholland 	if (vp->v_type == VDIR)
   1662      1.1  dholland 		error = EPERM;
   1663      1.1  dholland 	else if (vrefcnt(vp) == 1 || (np->n_sillyrename &&
   1664      1.1  dholland 	    VOP_GETATTR(vp, &vattr, cnp->cn_cred) == 0 &&
   1665      1.1  dholland 	    vattr.va_nlink > 1)) {
   1666      1.1  dholland 		/*
   1667      1.1  dholland 		 * Purge the name cache so that the chance of a lookup for
   1668      1.1  dholland 		 * the name succeeding while the remove is in progress is
   1669      1.1  dholland 		 * minimized. Without node locking it can still happen, such
   1670      1.1  dholland 		 * that an I/O op returns ESTALE, but since you get this if
   1671      1.1  dholland 		 * another host removes the file..
   1672      1.1  dholland 		 */
   1673      1.1  dholland 		cache_purge(vp);
   1674      1.1  dholland 		/*
   1675      1.1  dholland 		 * throw away biocache buffers, mainly to avoid
   1676      1.1  dholland 		 * unnecessary delayed writes later.
   1677      1.1  dholland 		 */
   1678      1.1  dholland 		error = ncl_vinvalbuf(vp, 0, cnp->cn_thread, 1);
   1679      1.1  dholland 		/* Do the rpc */
   1680      1.1  dholland 		if (error != EINTR && error != EIO)
   1681      1.1  dholland 			error = nfs_removerpc(dvp, vp, cnp->cn_nameptr,
   1682      1.1  dholland 			    cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread);
   1683      1.1  dholland 		/*
   1684      1.1  dholland 		 * Kludge City: If the first reply to the remove rpc is lost..
   1685      1.1  dholland 		 *   the reply to the retransmitted request will be ENOENT
   1686      1.1  dholland 		 *   since the file was in fact removed
   1687      1.1  dholland 		 *   Therefore, we cheat and return success.
   1688      1.1  dholland 		 */
   1689      1.1  dholland 		if (error == ENOENT)
   1690      1.1  dholland 			error = 0;
   1691      1.1  dholland 	} else if (!np->n_sillyrename)
   1692      1.1  dholland 		error = nfs_sillyrename(dvp, vp, cnp);
   1693      1.1  dholland 	mtx_lock(&np->n_mtx);
   1694      1.1  dholland 	np->n_attrstamp = 0;
   1695      1.1  dholland 	mtx_unlock(&np->n_mtx);
   1696      1.1  dholland 	KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
   1697      1.1  dholland 	return (error);
   1698      1.1  dholland }
   1699      1.1  dholland 
   1700      1.1  dholland /*
   1701      1.1  dholland  * nfs file remove rpc called from nfs_inactive
   1702      1.1  dholland  */
   1703      1.1  dholland int
   1704      1.1  dholland ncl_removeit(struct sillyrename *sp, struct vnode *vp)
   1705      1.1  dholland {
   1706      1.1  dholland 	/*
   1707      1.1  dholland 	 * Make sure that the directory vnode is still valid.
   1708      1.1  dholland 	 * XXX we should lock sp->s_dvp here.
   1709      1.1  dholland 	 */
   1710      1.1  dholland 	if (sp->s_dvp->v_type == VBAD)
   1711      1.1  dholland 		return (0);
   1712      1.1  dholland 	return (nfs_removerpc(sp->s_dvp, vp, sp->s_name, sp->s_namlen,
   1713      1.1  dholland 	    sp->s_cred, NULL));
   1714      1.1  dholland }
   1715      1.1  dholland 
   1716      1.1  dholland /*
   1717      1.1  dholland  * Nfs remove rpc, called from nfs_remove() and ncl_removeit().
   1718      1.1  dholland  */
   1719      1.1  dholland static int
   1720      1.1  dholland nfs_removerpc(struct vnode *dvp, struct vnode *vp, char *name,
   1721      1.1  dholland     int namelen, struct ucred *cred, struct thread *td)
   1722      1.1  dholland {
   1723      1.1  dholland 	struct nfsvattr dnfsva;
   1724      1.1  dholland 	struct nfsnode *dnp = VTONFS(dvp);
   1725      1.1  dholland 	int error = 0, dattrflag;
   1726      1.1  dholland 
   1727      1.1  dholland 	mtx_lock(&dnp->n_mtx);
   1728      1.1  dholland 	dnp->n_flag |= NREMOVEINPROG;
   1729      1.1  dholland 	mtx_unlock(&dnp->n_mtx);
   1730      1.1  dholland 	error = nfsrpc_remove(dvp, name, namelen, vp, cred, td, &dnfsva,
   1731      1.1  dholland 	    &dattrflag, NULL);
   1732      1.1  dholland 	mtx_lock(&dnp->n_mtx);
   1733      1.1  dholland 	if ((dnp->n_flag & NREMOVEWANT)) {
   1734      1.1  dholland 		dnp->n_flag &= ~(NREMOVEWANT | NREMOVEINPROG);
   1735      1.1  dholland 		mtx_unlock(&dnp->n_mtx);
   1736      1.1  dholland 		wakeup((caddr_t)dnp);
   1737      1.1  dholland 	} else {
   1738      1.1  dholland 		dnp->n_flag &= ~NREMOVEINPROG;
   1739      1.1  dholland 		mtx_unlock(&dnp->n_mtx);
   1740      1.1  dholland 	}
   1741      1.1  dholland 	if (dattrflag)
   1742      1.1  dholland 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
   1743      1.1  dholland 	mtx_lock(&dnp->n_mtx);
   1744      1.1  dholland 	dnp->n_flag |= NMODIFIED;
   1745      1.1  dholland 	if (!dattrflag) {
   1746      1.1  dholland 		dnp->n_attrstamp = 0;
   1747      1.1  dholland 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
   1748      1.1  dholland 	}
   1749      1.1  dholland 	mtx_unlock(&dnp->n_mtx);
   1750      1.1  dholland 	if (error && NFS_ISV4(dvp))
   1751      1.1  dholland 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
   1752      1.1  dholland 	return (error);
   1753      1.1  dholland }
   1754      1.1  dholland 
   1755      1.1  dholland /*
   1756      1.1  dholland  * nfs file rename call
   1757      1.1  dholland  */
   1758      1.1  dholland static int
   1759      1.1  dholland nfs_rename(struct vop_rename_args *ap)
   1760      1.1  dholland {
   1761      1.1  dholland 	struct vnode *fvp = ap->a_fvp;
   1762      1.1  dholland 	struct vnode *tvp = ap->a_tvp;
   1763      1.1  dholland 	struct vnode *fdvp = ap->a_fdvp;
   1764      1.1  dholland 	struct vnode *tdvp = ap->a_tdvp;
   1765      1.1  dholland 	struct componentname *tcnp = ap->a_tcnp;
   1766      1.1  dholland 	struct componentname *fcnp = ap->a_fcnp;
   1767      1.1  dholland 	struct nfsnode *fnp = VTONFS(ap->a_fvp);
   1768      1.1  dholland 	struct nfsnode *tdnp = VTONFS(ap->a_tdvp);
   1769      1.1  dholland 	struct nfsv4node *newv4 = NULL;
   1770      1.1  dholland 	int error;
   1771      1.1  dholland 
   1772      1.1  dholland 	KASSERT((tcnp->cn_flags & HASBUF) != 0 &&
   1773      1.1  dholland 	    (fcnp->cn_flags & HASBUF) != 0, ("nfs_rename: no name"));
   1774      1.1  dholland 	/* Check for cross-device rename */
   1775      1.1  dholland 	if ((fvp->v_mount != tdvp->v_mount) ||
   1776      1.1  dholland 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
   1777      1.1  dholland 		error = EXDEV;
   1778      1.1  dholland 		goto out;
   1779      1.1  dholland 	}
   1780      1.1  dholland 
   1781      1.1  dholland 	if (fvp == tvp) {
   1782  1.1.1.2  pgoyette 		printf("nfs_rename: fvp == tvp (can't happen)\n");
   1783      1.1  dholland 		error = 0;
   1784      1.1  dholland 		goto out;
   1785      1.1  dholland 	}
   1786      1.1  dholland 	if ((error = NFSVOPLOCK(fvp, LK_EXCLUSIVE)) != 0)
   1787      1.1  dholland 		goto out;
   1788      1.1  dholland 
   1789      1.1  dholland 	/*
   1790      1.1  dholland 	 * We have to flush B_DELWRI data prior to renaming
   1791      1.1  dholland 	 * the file.  If we don't, the delayed-write buffers
   1792      1.1  dholland 	 * can be flushed out later after the file has gone stale
   1793      1.1  dholland 	 * under NFSV3.  NFSV2 does not have this problem because
   1794      1.1  dholland 	 * ( as far as I can tell ) it flushes dirty buffers more
   1795      1.1  dholland 	 * often.
   1796      1.1  dholland 	 *
   1797      1.1  dholland 	 * Skip the rename operation if the fsync fails, this can happen
   1798      1.1  dholland 	 * due to the server's volume being full, when we pushed out data
   1799      1.1  dholland 	 * that was written back to our cache earlier. Not checking for
   1800      1.1  dholland 	 * this condition can result in potential (silent) data loss.
   1801      1.1  dholland 	 */
   1802      1.1  dholland 	error = VOP_FSYNC(fvp, MNT_WAIT, fcnp->cn_thread);
   1803      1.1  dholland 	NFSVOPUNLOCK(fvp, 0);
   1804      1.1  dholland 	if (!error && tvp)
   1805      1.1  dholland 		error = VOP_FSYNC(tvp, MNT_WAIT, tcnp->cn_thread);
   1806      1.1  dholland 	if (error)
   1807      1.1  dholland 		goto out;
   1808      1.1  dholland 
   1809      1.1  dholland 	/*
   1810      1.1  dholland 	 * If the tvp exists and is in use, sillyrename it before doing the
   1811      1.1  dholland 	 * rename of the new file over it.
   1812      1.1  dholland 	 * XXX Can't sillyrename a directory.
   1813      1.1  dholland 	 */
   1814      1.1  dholland 	if (tvp && vrefcnt(tvp) > 1 && !VTONFS(tvp)->n_sillyrename &&
   1815      1.1  dholland 		tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
   1816      1.1  dholland 		vput(tvp);
   1817      1.1  dholland 		tvp = NULL;
   1818      1.1  dholland 	}
   1819      1.1  dholland 
   1820      1.1  dholland 	error = nfs_renamerpc(fdvp, fvp, fcnp->cn_nameptr, fcnp->cn_namelen,
   1821      1.1  dholland 	    tdvp, tvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
   1822      1.1  dholland 	    tcnp->cn_thread);
   1823      1.1  dholland 
   1824      1.1  dholland 	if (error == 0 && NFS_ISV4(tdvp)) {
   1825      1.1  dholland 		/*
   1826      1.1  dholland 		 * For NFSv4, check to see if it is the same name and
   1827      1.1  dholland 		 * replace the name, if it is different.
   1828      1.1  dholland 		 */
   1829      1.1  dholland 		MALLOC(newv4, struct nfsv4node *,
   1830      1.1  dholland 		    sizeof (struct nfsv4node) +
   1831      1.1  dholland 		    tdnp->n_fhp->nfh_len + tcnp->cn_namelen - 1,
   1832      1.1  dholland 		    M_NFSV4NODE, M_WAITOK);
   1833      1.1  dholland 		mtx_lock(&tdnp->n_mtx);
   1834      1.1  dholland 		mtx_lock(&fnp->n_mtx);
   1835      1.1  dholland 		if (fnp->n_v4 != NULL && fvp->v_type == VREG &&
   1836      1.1  dholland 		    (fnp->n_v4->n4_namelen != tcnp->cn_namelen ||
   1837      1.1  dholland 		      NFSBCMP(tcnp->cn_nameptr, NFS4NODENAME(fnp->n_v4),
   1838      1.1  dholland 		      tcnp->cn_namelen) ||
   1839      1.1  dholland 		      tdnp->n_fhp->nfh_len != fnp->n_v4->n4_fhlen ||
   1840      1.1  dholland 		      NFSBCMP(tdnp->n_fhp->nfh_fh, fnp->n_v4->n4_data,
   1841      1.1  dholland 			tdnp->n_fhp->nfh_len))) {
   1842      1.1  dholland #ifdef notdef
   1843      1.1  dholland { char nnn[100]; int nnnl;
   1844      1.1  dholland nnnl = (tcnp->cn_namelen < 100) ? tcnp->cn_namelen : 99;
   1845      1.1  dholland bcopy(tcnp->cn_nameptr, nnn, nnnl);
   1846      1.1  dholland nnn[nnnl] = '\0';
   1847      1.1  dholland printf("ren replace=%s\n",nnn);
   1848      1.1  dholland }
   1849      1.1  dholland #endif
   1850      1.1  dholland 			FREE((caddr_t)fnp->n_v4, M_NFSV4NODE);
   1851      1.1  dholland 			fnp->n_v4 = newv4;
   1852      1.1  dholland 			newv4 = NULL;
   1853      1.1  dholland 			fnp->n_v4->n4_fhlen = tdnp->n_fhp->nfh_len;
   1854      1.1  dholland 			fnp->n_v4->n4_namelen = tcnp->cn_namelen;
   1855      1.1  dholland 			NFSBCOPY(tdnp->n_fhp->nfh_fh, fnp->n_v4->n4_data,
   1856      1.1  dholland 			    tdnp->n_fhp->nfh_len);
   1857      1.1  dholland 			NFSBCOPY(tcnp->cn_nameptr,
   1858      1.1  dholland 			    NFS4NODENAME(fnp->n_v4), tcnp->cn_namelen);
   1859      1.1  dholland 		}
   1860      1.1  dholland 		mtx_unlock(&tdnp->n_mtx);
   1861      1.1  dholland 		mtx_unlock(&fnp->n_mtx);
   1862      1.1  dholland 		if (newv4 != NULL)
   1863      1.1  dholland 			FREE((caddr_t)newv4, M_NFSV4NODE);
   1864      1.1  dholland 	}
   1865      1.1  dholland 
   1866      1.1  dholland 	if (fvp->v_type == VDIR) {
   1867      1.1  dholland 		if (tvp != NULL && tvp->v_type == VDIR)
   1868      1.1  dholland 			cache_purge(tdvp);
   1869      1.1  dholland 		cache_purge(fdvp);
   1870      1.1  dholland 	}
   1871      1.1  dholland 
   1872      1.1  dholland out:
   1873      1.1  dholland 	if (tdvp == tvp)
   1874      1.1  dholland 		vrele(tdvp);
   1875      1.1  dholland 	else
   1876      1.1  dholland 		vput(tdvp);
   1877      1.1  dholland 	if (tvp)
   1878      1.1  dholland 		vput(tvp);
   1879      1.1  dholland 	vrele(fdvp);
   1880      1.1  dholland 	vrele(fvp);
   1881      1.1  dholland 	/*
   1882      1.1  dholland 	 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
   1883      1.1  dholland 	 */
   1884      1.1  dholland 	if (error == ENOENT)
   1885      1.1  dholland 		error = 0;
   1886      1.1  dholland 	return (error);
   1887      1.1  dholland }
   1888      1.1  dholland 
   1889      1.1  dholland /*
   1890      1.1  dholland  * nfs file rename rpc called from nfs_remove() above
   1891      1.1  dholland  */
   1892      1.1  dholland static int
   1893      1.1  dholland nfs_renameit(struct vnode *sdvp, struct vnode *svp, struct componentname *scnp,
   1894      1.1  dholland     struct sillyrename *sp)
   1895      1.1  dholland {
   1896      1.1  dholland 
   1897      1.1  dholland 	return (nfs_renamerpc(sdvp, svp, scnp->cn_nameptr, scnp->cn_namelen,
   1898      1.1  dholland 	    sdvp, NULL, sp->s_name, sp->s_namlen, scnp->cn_cred,
   1899      1.1  dholland 	    scnp->cn_thread));
   1900      1.1  dholland }
   1901      1.1  dholland 
   1902      1.1  dholland /*
   1903      1.1  dholland  * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
   1904      1.1  dholland  */
   1905      1.1  dholland static int
   1906      1.1  dholland nfs_renamerpc(struct vnode *fdvp, struct vnode *fvp, char *fnameptr,
   1907      1.1  dholland     int fnamelen, struct vnode *tdvp, struct vnode *tvp, char *tnameptr,
   1908      1.1  dholland     int tnamelen, struct ucred *cred, struct thread *td)
   1909      1.1  dholland {
   1910      1.1  dholland 	struct nfsvattr fnfsva, tnfsva;
   1911      1.1  dholland 	struct nfsnode *fdnp = VTONFS(fdvp);
   1912      1.1  dholland 	struct nfsnode *tdnp = VTONFS(tdvp);
   1913      1.1  dholland 	int error = 0, fattrflag, tattrflag;
   1914      1.1  dholland 
   1915      1.1  dholland 	error = nfsrpc_rename(fdvp, fvp, fnameptr, fnamelen, tdvp, tvp,
   1916      1.1  dholland 	    tnameptr, tnamelen, cred, td, &fnfsva, &tnfsva, &fattrflag,
   1917      1.1  dholland 	    &tattrflag, NULL, NULL);
   1918      1.1  dholland 	mtx_lock(&fdnp->n_mtx);
   1919      1.1  dholland 	fdnp->n_flag |= NMODIFIED;
   1920      1.1  dholland 	if (fattrflag != 0) {
   1921      1.1  dholland 		mtx_unlock(&fdnp->n_mtx);
   1922      1.1  dholland 		(void) nfscl_loadattrcache(&fdvp, &fnfsva, NULL, NULL, 0, 1);
   1923      1.1  dholland 	} else {
   1924      1.1  dholland 		fdnp->n_attrstamp = 0;
   1925      1.1  dholland 		mtx_unlock(&fdnp->n_mtx);
   1926      1.1  dholland 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(fdvp);
   1927      1.1  dholland 	}
   1928      1.1  dholland 	mtx_lock(&tdnp->n_mtx);
   1929      1.1  dholland 	tdnp->n_flag |= NMODIFIED;
   1930      1.1  dholland 	if (tattrflag != 0) {
   1931      1.1  dholland 		mtx_unlock(&tdnp->n_mtx);
   1932      1.1  dholland 		(void) nfscl_loadattrcache(&tdvp, &tnfsva, NULL, NULL, 0, 1);
   1933      1.1  dholland 	} else {
   1934      1.1  dholland 		tdnp->n_attrstamp = 0;
   1935      1.1  dholland 		mtx_unlock(&tdnp->n_mtx);
   1936      1.1  dholland 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(tdvp);
   1937      1.1  dholland 	}
   1938      1.1  dholland 	if (error && NFS_ISV4(fdvp))
   1939      1.1  dholland 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
   1940      1.1  dholland 	return (error);
   1941      1.1  dholland }
   1942      1.1  dholland 
   1943      1.1  dholland /*
   1944      1.1  dholland  * nfs hard link create call
   1945      1.1  dholland  */
   1946      1.1  dholland static int
   1947      1.1  dholland nfs_link(struct vop_link_args *ap)
   1948      1.1  dholland {
   1949      1.1  dholland 	struct vnode *vp = ap->a_vp;
   1950      1.1  dholland 	struct vnode *tdvp = ap->a_tdvp;
   1951      1.1  dholland 	struct componentname *cnp = ap->a_cnp;
   1952      1.1  dholland 	struct nfsnode *np, *tdnp;
   1953      1.1  dholland 	struct nfsvattr nfsva, dnfsva;
   1954      1.1  dholland 	int error = 0, attrflag, dattrflag;
   1955      1.1  dholland 
   1956      1.1  dholland 	/*
   1957      1.1  dholland 	 * Push all writes to the server, so that the attribute cache
   1958      1.1  dholland 	 * doesn't get "out of sync" with the server.
   1959      1.1  dholland 	 * XXX There should be a better way!
   1960      1.1  dholland 	 */
   1961      1.1  dholland 	VOP_FSYNC(vp, MNT_WAIT, cnp->cn_thread);
   1962      1.1  dholland 
   1963      1.1  dholland 	error = nfsrpc_link(tdvp, vp, cnp->cn_nameptr, cnp->cn_namelen,
   1964      1.1  dholland 	    cnp->cn_cred, cnp->cn_thread, &dnfsva, &nfsva, &attrflag,
   1965      1.1  dholland 	    &dattrflag, NULL);
   1966      1.1  dholland 	tdnp = VTONFS(tdvp);
   1967      1.1  dholland 	mtx_lock(&tdnp->n_mtx);
   1968      1.1  dholland 	tdnp->n_flag |= NMODIFIED;
   1969      1.1  dholland 	if (dattrflag != 0) {
   1970      1.1  dholland 		mtx_unlock(&tdnp->n_mtx);
   1971      1.1  dholland 		(void) nfscl_loadattrcache(&tdvp, &dnfsva, NULL, NULL, 0, 1);
   1972      1.1  dholland 	} else {
   1973      1.1  dholland 		tdnp->n_attrstamp = 0;
   1974      1.1  dholland 		mtx_unlock(&tdnp->n_mtx);
   1975      1.1  dholland 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(tdvp);
   1976      1.1  dholland 	}
   1977      1.1  dholland 	if (attrflag)
   1978      1.1  dholland 		(void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
   1979      1.1  dholland 	else {
   1980      1.1  dholland 		np = VTONFS(vp);
   1981      1.1  dholland 		mtx_lock(&np->n_mtx);
   1982      1.1  dholland 		np->n_attrstamp = 0;
   1983      1.1  dholland 		mtx_unlock(&np->n_mtx);
   1984      1.1  dholland 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
   1985      1.1  dholland 	}
   1986      1.1  dholland 	/*
   1987      1.1  dholland 	 * If negative lookup caching is enabled, I might as well
   1988      1.1  dholland 	 * add an entry for this node. Not necessary for correctness,
   1989      1.1  dholland 	 * but if negative caching is enabled, then the system
   1990      1.1  dholland 	 * must care about lookup caching hit rate, so...
   1991      1.1  dholland 	 */
   1992      1.1  dholland 	if (VFSTONFS(vp->v_mount)->nm_negnametimeo != 0 &&
   1993      1.1  dholland 	    (cnp->cn_flags & MAKEENTRY) && attrflag != 0 && error == 0) {
   1994      1.1  dholland 		cache_enter_time(tdvp, vp, cnp, &nfsva.na_ctime, NULL);
   1995      1.1  dholland 	}
   1996      1.1  dholland 	if (error && NFS_ISV4(vp))
   1997      1.1  dholland 		error = nfscl_maperr(cnp->cn_thread, error, (uid_t)0,
   1998      1.1  dholland 		    (gid_t)0);
   1999      1.1  dholland 	return (error);
   2000      1.1  dholland }
   2001      1.1  dholland 
   2002      1.1  dholland /*
   2003      1.1  dholland  * nfs symbolic link create call
   2004      1.1  dholland  */
   2005      1.1  dholland static int
   2006      1.1  dholland nfs_symlink(struct vop_symlink_args *ap)
   2007      1.1  dholland {
   2008      1.1  dholland 	struct vnode *dvp = ap->a_dvp;
   2009      1.1  dholland 	struct vattr *vap = ap->a_vap;
   2010      1.1  dholland 	struct componentname *cnp = ap->a_cnp;
   2011      1.1  dholland 	struct nfsvattr nfsva, dnfsva;
   2012      1.1  dholland 	struct nfsfh *nfhp;
   2013      1.1  dholland 	struct nfsnode *np = NULL, *dnp;
   2014      1.1  dholland 	struct vnode *newvp = NULL;
   2015      1.1  dholland 	int error = 0, attrflag, dattrflag, ret;
   2016      1.1  dholland 
   2017      1.1  dholland 	vap->va_type = VLNK;
   2018      1.1  dholland 	error = nfsrpc_symlink(dvp, cnp->cn_nameptr, cnp->cn_namelen,
   2019      1.1  dholland 	    ap->a_target, vap, cnp->cn_cred, cnp->cn_thread, &dnfsva,
   2020      1.1  dholland 	    &nfsva, &nfhp, &attrflag, &dattrflag, NULL);
   2021      1.1  dholland 	if (nfhp) {
   2022      1.1  dholland 		ret = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, cnp->cn_thread,
   2023      1.1  dholland 		    &np, NULL, LK_EXCLUSIVE);
   2024      1.1  dholland 		if (!ret)
   2025      1.1  dholland 			newvp = NFSTOV(np);
   2026      1.1  dholland 		else if (!error)
   2027      1.1  dholland 			error = ret;
   2028      1.1  dholland 	}
   2029      1.1  dholland 	if (newvp != NULL) {
   2030      1.1  dholland 		if (attrflag)
   2031      1.1  dholland 			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
   2032      1.1  dholland 			    0, 1);
   2033      1.1  dholland 	} else if (!error) {
   2034      1.1  dholland 		/*
   2035      1.1  dholland 		 * If we do not have an error and we could not extract the
   2036      1.1  dholland 		 * newvp from the response due to the request being NFSv2, we
   2037      1.1  dholland 		 * have to do a lookup in order to obtain a newvp to return.
   2038      1.1  dholland 		 */
   2039      1.1  dholland 		error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
   2040      1.1  dholland 		    cnp->cn_cred, cnp->cn_thread, &np);
   2041      1.1  dholland 		if (!error)
   2042      1.1  dholland 			newvp = NFSTOV(np);
   2043      1.1  dholland 	}
   2044      1.1  dholland 	if (error) {
   2045      1.1  dholland 		if (newvp)
   2046      1.1  dholland 			vput(newvp);
   2047      1.1  dholland 		if (NFS_ISV4(dvp))
   2048      1.1  dholland 			error = nfscl_maperr(cnp->cn_thread, error,
   2049      1.1  dholland 			    vap->va_uid, vap->va_gid);
   2050      1.1  dholland 	} else {
   2051      1.1  dholland 		*ap->a_vpp = newvp;
   2052      1.1  dholland 	}
   2053      1.1  dholland 
   2054      1.1  dholland 	dnp = VTONFS(dvp);
   2055      1.1  dholland 	mtx_lock(&dnp->n_mtx);
   2056      1.1  dholland 	dnp->n_flag |= NMODIFIED;
   2057      1.1  dholland 	if (dattrflag != 0) {
   2058      1.1  dholland 		mtx_unlock(&dnp->n_mtx);
   2059      1.1  dholland 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
   2060      1.1  dholland 	} else {
   2061      1.1  dholland 		dnp->n_attrstamp = 0;
   2062      1.1  dholland 		mtx_unlock(&dnp->n_mtx);
   2063      1.1  dholland 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
   2064      1.1  dholland 	}
   2065      1.1  dholland 	/*
   2066      1.1  dholland 	 * If negative lookup caching is enabled, I might as well
   2067      1.1  dholland 	 * add an entry for this node. Not necessary for correctness,
   2068      1.1  dholland 	 * but if negative caching is enabled, then the system
   2069      1.1  dholland 	 * must care about lookup caching hit rate, so...
   2070      1.1  dholland 	 */
   2071      1.1  dholland 	if (VFSTONFS(dvp->v_mount)->nm_negnametimeo != 0 &&
   2072      1.1  dholland 	    (cnp->cn_flags & MAKEENTRY) && attrflag != 0 && error == 0) {
   2073      1.1  dholland 		cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime, NULL);
   2074      1.1  dholland 	}
   2075      1.1  dholland 	return (error);
   2076      1.1  dholland }
   2077      1.1  dholland 
   2078      1.1  dholland /*
   2079      1.1  dholland  * nfs make dir call
   2080      1.1  dholland  */
   2081      1.1  dholland static int
   2082      1.1  dholland nfs_mkdir(struct vop_mkdir_args *ap)
   2083      1.1  dholland {
   2084      1.1  dholland 	struct vnode *dvp = ap->a_dvp;
   2085      1.1  dholland 	struct vattr *vap = ap->a_vap;
   2086      1.1  dholland 	struct componentname *cnp = ap->a_cnp;
   2087      1.1  dholland 	struct nfsnode *np = NULL, *dnp;
   2088      1.1  dholland 	struct vnode *newvp = NULL;
   2089      1.1  dholland 	struct vattr vattr;
   2090      1.1  dholland 	struct nfsfh *nfhp;
   2091      1.1  dholland 	struct nfsvattr nfsva, dnfsva;
   2092      1.1  dholland 	int error = 0, attrflag, dattrflag, ret;
   2093      1.1  dholland 
   2094      1.1  dholland 	if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)) != 0)
   2095      1.1  dholland 		return (error);
   2096      1.1  dholland 	vap->va_type = VDIR;
   2097      1.1  dholland 	error = nfsrpc_mkdir(dvp, cnp->cn_nameptr, cnp->cn_namelen,
   2098      1.1  dholland 	    vap, cnp->cn_cred, cnp->cn_thread, &dnfsva, &nfsva, &nfhp,
   2099      1.1  dholland 	    &attrflag, &dattrflag, NULL);
   2100      1.1  dholland 	dnp = VTONFS(dvp);
   2101      1.1  dholland 	mtx_lock(&dnp->n_mtx);
   2102      1.1  dholland 	dnp->n_flag |= NMODIFIED;
   2103      1.1  dholland 	if (dattrflag != 0) {
   2104      1.1  dholland 		mtx_unlock(&dnp->n_mtx);
   2105      1.1  dholland 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
   2106      1.1  dholland 	} else {
   2107      1.1  dholland 		dnp->n_attrstamp = 0;
   2108      1.1  dholland 		mtx_unlock(&dnp->n_mtx);
   2109      1.1  dholland 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
   2110      1.1  dholland 	}
   2111      1.1  dholland 	if (nfhp) {
   2112      1.1  dholland 		ret = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, cnp->cn_thread,
   2113      1.1  dholland 		    &np, NULL, LK_EXCLUSIVE);
   2114      1.1  dholland 		if (!ret) {
   2115      1.1  dholland 			newvp = NFSTOV(np);
   2116      1.1  dholland 			if (attrflag)
   2117      1.1  dholland 			   (void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
   2118      1.1  dholland 				NULL, 0, 1);
   2119      1.1  dholland 		} else if (!error)
   2120      1.1  dholland 			error = ret;
   2121      1.1  dholland 	}
   2122      1.1  dholland 	if (!error && newvp == NULL) {
   2123      1.1  dholland 		error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
   2124      1.1  dholland 		    cnp->cn_cred, cnp->cn_thread, &np);
   2125      1.1  dholland 		if (!error) {
   2126      1.1  dholland 			newvp = NFSTOV(np);
   2127      1.1  dholland 			if (newvp->v_type != VDIR)
   2128      1.1  dholland 				error = EEXIST;
   2129      1.1  dholland 		}
   2130      1.1  dholland 	}
   2131      1.1  dholland 	if (error) {
   2132      1.1  dholland 		if (newvp)
   2133      1.1  dholland 			vput(newvp);
   2134      1.1  dholland 		if (NFS_ISV4(dvp))
   2135      1.1  dholland 			error = nfscl_maperr(cnp->cn_thread, error,
   2136      1.1  dholland 			    vap->va_uid, vap->va_gid);
   2137      1.1  dholland 	} else {
   2138      1.1  dholland 		/*
   2139      1.1  dholland 		 * If negative lookup caching is enabled, I might as well
   2140      1.1  dholland 		 * add an entry for this node. Not necessary for correctness,
   2141      1.1  dholland 		 * but if negative caching is enabled, then the system
   2142      1.1  dholland 		 * must care about lookup caching hit rate, so...
   2143      1.1  dholland 		 */
   2144      1.1  dholland 		if (VFSTONFS(dvp->v_mount)->nm_negnametimeo != 0 &&
   2145      1.1  dholland 		    (cnp->cn_flags & MAKEENTRY) &&
   2146      1.1  dholland 		    attrflag != 0 && dattrflag != 0)
   2147      1.1  dholland 			cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
   2148      1.1  dholland 			    &dnfsva.na_ctime);
   2149      1.1  dholland 		*ap->a_vpp = newvp;
   2150      1.1  dholland 	}
   2151      1.1  dholland 	return (error);
   2152      1.1  dholland }
   2153      1.1  dholland 
   2154      1.1  dholland /*
   2155      1.1  dholland  * nfs remove directory call
   2156      1.1  dholland  */
   2157      1.1  dholland static int
   2158      1.1  dholland nfs_rmdir(struct vop_rmdir_args *ap)
   2159      1.1  dholland {
   2160      1.1  dholland 	struct vnode *vp = ap->a_vp;
   2161      1.1  dholland 	struct vnode *dvp = ap->a_dvp;
   2162      1.1  dholland 	struct componentname *cnp = ap->a_cnp;
   2163      1.1  dholland 	struct nfsnode *dnp;
   2164      1.1  dholland 	struct nfsvattr dnfsva;
   2165      1.1  dholland 	int error, dattrflag;
   2166      1.1  dholland 
   2167      1.1  dholland 	if (dvp == vp)
   2168      1.1  dholland 		return (EINVAL);
   2169      1.1  dholland 	error = nfsrpc_rmdir(dvp, cnp->cn_nameptr, cnp->cn_namelen,
   2170      1.1  dholland 	    cnp->cn_cred, cnp->cn_thread, &dnfsva, &dattrflag, NULL);
   2171      1.1  dholland 	dnp = VTONFS(dvp);
   2172      1.1  dholland 	mtx_lock(&dnp->n_mtx);
   2173      1.1  dholland 	dnp->n_flag |= NMODIFIED;
   2174      1.1  dholland 	if (dattrflag != 0) {
   2175      1.1  dholland 		mtx_unlock(&dnp->n_mtx);
   2176      1.1  dholland 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
   2177      1.1  dholland 	} else {
   2178      1.1  dholland 		dnp->n_attrstamp = 0;
   2179      1.1  dholland 		mtx_unlock(&dnp->n_mtx);
   2180      1.1  dholland 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
   2181      1.1  dholland 	}
   2182      1.1  dholland 
   2183      1.1  dholland 	cache_purge(dvp);
   2184      1.1  dholland 	cache_purge(vp);
   2185      1.1  dholland 	if (error && NFS_ISV4(dvp))
   2186      1.1  dholland 		error = nfscl_maperr(cnp->cn_thread, error, (uid_t)0,
   2187      1.1  dholland 		    (gid_t)0);
   2188      1.1  dholland 	/*
   2189      1.1  dholland 	 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
   2190      1.1  dholland 	 */
   2191      1.1  dholland 	if (error == ENOENT)
   2192      1.1  dholland 		error = 0;
   2193      1.1  dholland 	return (error);
   2194      1.1  dholland }
   2195      1.1  dholland 
   2196      1.1  dholland /*
   2197      1.1  dholland  * nfs readdir call
   2198      1.1  dholland  */
   2199      1.1  dholland static int
   2200      1.1  dholland nfs_readdir(struct vop_readdir_args *ap)
   2201      1.1  dholland {
   2202      1.1  dholland 	struct vnode *vp = ap->a_vp;
   2203      1.1  dholland 	struct nfsnode *np = VTONFS(vp);
   2204      1.1  dholland 	struct uio *uio = ap->a_uio;
   2205  1.1.1.2  pgoyette 	ssize_t tresid, left;
   2206      1.1  dholland 	int error = 0;
   2207      1.1  dholland 	struct vattr vattr;
   2208      1.1  dholland 
   2209      1.1  dholland 	if (ap->a_eofflag != NULL)
   2210      1.1  dholland 		*ap->a_eofflag = 0;
   2211      1.1  dholland 	if (vp->v_type != VDIR)
   2212      1.1  dholland 		return(EPERM);
   2213      1.1  dholland 
   2214      1.1  dholland 	/*
   2215      1.1  dholland 	 * First, check for hit on the EOF offset cache
   2216      1.1  dholland 	 */
   2217      1.1  dholland 	if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
   2218      1.1  dholland 	    (np->n_flag & NMODIFIED) == 0) {
   2219      1.1  dholland 		if (VOP_GETATTR(vp, &vattr, ap->a_cred) == 0) {
   2220      1.1  dholland 			mtx_lock(&np->n_mtx);
   2221      1.1  dholland 			if ((NFS_ISV4(vp) && np->n_change == vattr.va_filerev) ||
   2222      1.1  dholland 			    !NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
   2223      1.1  dholland 				mtx_unlock(&np->n_mtx);
   2224  1.1.1.2  pgoyette 				NFSINCRGLOBAL(nfsstatsv1.direofcache_hits);
   2225      1.1  dholland 				if (ap->a_eofflag != NULL)
   2226      1.1  dholland 					*ap->a_eofflag = 1;
   2227      1.1  dholland 				return (0);
   2228      1.1  dholland 			} else
   2229      1.1  dholland 				mtx_unlock(&np->n_mtx);
   2230      1.1  dholland 		}
   2231      1.1  dholland 	}
   2232      1.1  dholland 
   2233      1.1  dholland 	/*
   2234  1.1.1.2  pgoyette 	 * NFS always guarantees that directory entries don't straddle
   2235  1.1.1.2  pgoyette 	 * DIRBLKSIZ boundaries.  As such, we need to limit the size
   2236  1.1.1.2  pgoyette 	 * to an exact multiple of DIRBLKSIZ, to avoid copying a partial
   2237  1.1.1.2  pgoyette 	 * directory entry.
   2238  1.1.1.2  pgoyette 	 */
   2239  1.1.1.2  pgoyette 	left = uio->uio_resid % DIRBLKSIZ;
   2240  1.1.1.2  pgoyette 	if (left == uio->uio_resid)
   2241  1.1.1.2  pgoyette 		return (EINVAL);
   2242  1.1.1.2  pgoyette 	uio->uio_resid -= left;
   2243  1.1.1.2  pgoyette 
   2244  1.1.1.2  pgoyette 	/*
   2245      1.1  dholland 	 * Call ncl_bioread() to do the real work.
   2246      1.1  dholland 	 */
   2247      1.1  dholland 	tresid = uio->uio_resid;
   2248      1.1  dholland 	error = ncl_bioread(vp, uio, 0, ap->a_cred);
   2249      1.1  dholland 
   2250      1.1  dholland 	if (!error && uio->uio_resid == tresid) {
   2251  1.1.1.2  pgoyette 		NFSINCRGLOBAL(nfsstatsv1.direofcache_misses);
   2252      1.1  dholland 		if (ap->a_eofflag != NULL)
   2253      1.1  dholland 			*ap->a_eofflag = 1;
   2254      1.1  dholland 	}
   2255  1.1.1.2  pgoyette 
   2256  1.1.1.2  pgoyette 	/* Add the partial DIRBLKSIZ (left) back in. */
   2257  1.1.1.2  pgoyette 	uio->uio_resid += left;
   2258      1.1  dholland 	return (error);
   2259      1.1  dholland }
   2260      1.1  dholland 
   2261      1.1  dholland /*
   2262      1.1  dholland  * Readdir rpc call.
   2263      1.1  dholland  * Called from below the buffer cache by ncl_doio().
   2264      1.1  dholland  */
   2265      1.1  dholland int
   2266      1.1  dholland ncl_readdirrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
   2267      1.1  dholland     struct thread *td)
   2268      1.1  dholland {
   2269      1.1  dholland 	struct nfsvattr nfsva;
   2270      1.1  dholland 	nfsuint64 *cookiep, cookie;
   2271      1.1  dholland 	struct nfsnode *dnp = VTONFS(vp);
   2272      1.1  dholland 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
   2273      1.1  dholland 	int error = 0, eof, attrflag;
   2274      1.1  dholland 
   2275      1.1  dholland 	KASSERT(uiop->uio_iovcnt == 1 &&
   2276      1.1  dholland 	    (uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 &&
   2277      1.1  dholland 	    (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
   2278      1.1  dholland 	    ("nfs readdirrpc bad uio"));
   2279      1.1  dholland 
   2280      1.1  dholland 	/*
   2281      1.1  dholland 	 * If there is no cookie, assume directory was stale.
   2282      1.1  dholland 	 */
   2283      1.1  dholland 	ncl_dircookie_lock(dnp);
   2284      1.1  dholland 	cookiep = ncl_getcookie(dnp, uiop->uio_offset, 0);
   2285      1.1  dholland 	if (cookiep) {
   2286      1.1  dholland 		cookie = *cookiep;
   2287      1.1  dholland 		ncl_dircookie_unlock(dnp);
   2288      1.1  dholland 	} else {
   2289      1.1  dholland 		ncl_dircookie_unlock(dnp);
   2290      1.1  dholland 		return (NFSERR_BAD_COOKIE);
   2291      1.1  dholland 	}
   2292      1.1  dholland 
   2293      1.1  dholland 	if (NFSHASNFSV3(nmp) && !NFSHASGOTFSINFO(nmp))
   2294      1.1  dholland 		(void)ncl_fsinfo(nmp, vp, cred, td);
   2295      1.1  dholland 
   2296      1.1  dholland 	error = nfsrpc_readdir(vp, uiop, &cookie, cred, td, &nfsva,
   2297      1.1  dholland 	    &attrflag, &eof, NULL);
   2298      1.1  dholland 	if (attrflag)
   2299      1.1  dholland 		(void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
   2300      1.1  dholland 
   2301      1.1  dholland 	if (!error) {
   2302      1.1  dholland 		/*
   2303      1.1  dholland 		 * We are now either at the end of the directory or have filled
   2304      1.1  dholland 		 * the block.
   2305      1.1  dholland 		 */
   2306      1.1  dholland 		if (eof)
   2307      1.1  dholland 			dnp->n_direofoffset = uiop->uio_offset;
   2308      1.1  dholland 		else {
   2309      1.1  dholland 			if (uiop->uio_resid > 0)
   2310  1.1.1.2  pgoyette 				printf("EEK! readdirrpc resid > 0\n");
   2311      1.1  dholland 			ncl_dircookie_lock(dnp);
   2312      1.1  dholland 			cookiep = ncl_getcookie(dnp, uiop->uio_offset, 1);
   2313      1.1  dholland 			*cookiep = cookie;
   2314      1.1  dholland 			ncl_dircookie_unlock(dnp);
   2315      1.1  dholland 		}
   2316      1.1  dholland 	} else if (NFS_ISV4(vp)) {
   2317      1.1  dholland 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
   2318      1.1  dholland 	}
   2319      1.1  dholland 	return (error);
   2320      1.1  dholland }
   2321      1.1  dholland 
   2322      1.1  dholland /*
   2323      1.1  dholland  * NFS V3 readdir plus RPC. Used in place of ncl_readdirrpc().
   2324      1.1  dholland  */
   2325      1.1  dholland int
   2326      1.1  dholland ncl_readdirplusrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
   2327      1.1  dholland     struct thread *td)
   2328      1.1  dholland {
   2329      1.1  dholland 	struct nfsvattr nfsva;
   2330      1.1  dholland 	nfsuint64 *cookiep, cookie;
   2331      1.1  dholland 	struct nfsnode *dnp = VTONFS(vp);
   2332      1.1  dholland 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
   2333      1.1  dholland 	int error = 0, attrflag, eof;
   2334      1.1  dholland 
   2335      1.1  dholland 	KASSERT(uiop->uio_iovcnt == 1 &&
   2336      1.1  dholland 	    (uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 &&
   2337      1.1  dholland 	    (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
   2338      1.1  dholland 	    ("nfs readdirplusrpc bad uio"));
   2339      1.1  dholland 
   2340      1.1  dholland 	/*
   2341      1.1  dholland 	 * If there is no cookie, assume directory was stale.
   2342      1.1  dholland 	 */
   2343      1.1  dholland 	ncl_dircookie_lock(dnp);
   2344      1.1  dholland 	cookiep = ncl_getcookie(dnp, uiop->uio_offset, 0);
   2345      1.1  dholland 	if (cookiep) {
   2346      1.1  dholland 		cookie = *cookiep;
   2347      1.1  dholland 		ncl_dircookie_unlock(dnp);
   2348      1.1  dholland 	} else {
   2349      1.1  dholland 		ncl_dircookie_unlock(dnp);
   2350      1.1  dholland 		return (NFSERR_BAD_COOKIE);
   2351      1.1  dholland 	}
   2352      1.1  dholland 
   2353      1.1  dholland 	if (NFSHASNFSV3(nmp) && !NFSHASGOTFSINFO(nmp))
   2354      1.1  dholland 		(void)ncl_fsinfo(nmp, vp, cred, td);
   2355      1.1  dholland 	error = nfsrpc_readdirplus(vp, uiop, &cookie, cred, td, &nfsva,
   2356      1.1  dholland 	    &attrflag, &eof, NULL);
   2357      1.1  dholland 	if (attrflag)
   2358      1.1  dholland 		(void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
   2359      1.1  dholland 
   2360      1.1  dholland 	if (!error) {
   2361      1.1  dholland 		/*
   2362      1.1  dholland 		 * We are now either at end of the directory or have filled the
   2363      1.1  dholland 		 * the block.
   2364      1.1  dholland 		 */
   2365      1.1  dholland 		if (eof)
   2366      1.1  dholland 			dnp->n_direofoffset = uiop->uio_offset;
   2367      1.1  dholland 		else {
   2368      1.1  dholland 			if (uiop->uio_resid > 0)
   2369  1.1.1.2  pgoyette 				printf("EEK! readdirplusrpc resid > 0\n");
   2370      1.1  dholland 			ncl_dircookie_lock(dnp);
   2371      1.1  dholland 			cookiep = ncl_getcookie(dnp, uiop->uio_offset, 1);
   2372      1.1  dholland 			*cookiep = cookie;
   2373      1.1  dholland 			ncl_dircookie_unlock(dnp);
   2374      1.1  dholland 		}
   2375      1.1  dholland 	} else if (NFS_ISV4(vp)) {
   2376      1.1  dholland 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
   2377      1.1  dholland 	}
   2378      1.1  dholland 	return (error);
   2379      1.1  dholland }
   2380      1.1  dholland 
   2381      1.1  dholland /*
   2382      1.1  dholland  * Silly rename. To make the NFS filesystem that is stateless look a little
   2383      1.1  dholland  * more like the "ufs" a remove of an active vnode is translated to a rename
   2384      1.1  dholland  * to a funny looking filename that is removed by nfs_inactive on the
   2385      1.1  dholland  * nfsnode. There is the potential for another process on a different client
   2386      1.1  dholland  * to create the same funny name between the nfs_lookitup() fails and the
   2387      1.1  dholland  * nfs_rename() completes, but...
   2388      1.1  dholland  */
   2389      1.1  dholland static int
   2390      1.1  dholland nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
   2391      1.1  dholland {
   2392      1.1  dholland 	struct sillyrename *sp;
   2393      1.1  dholland 	struct nfsnode *np;
   2394      1.1  dholland 	int error;
   2395      1.1  dholland 	short pid;
   2396      1.1  dholland 	unsigned int lticks;
   2397      1.1  dholland 
   2398      1.1  dholland 	cache_purge(dvp);
   2399      1.1  dholland 	np = VTONFS(vp);
   2400      1.1  dholland 	KASSERT(vp->v_type != VDIR, ("nfs: sillyrename dir"));
   2401      1.1  dholland 	MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
   2402      1.1  dholland 	    M_NEWNFSREQ, M_WAITOK);
   2403      1.1  dholland 	sp->s_cred = crhold(cnp->cn_cred);
   2404      1.1  dholland 	sp->s_dvp = dvp;
   2405      1.1  dholland 	VREF(dvp);
   2406      1.1  dholland 
   2407      1.1  dholland 	/*
   2408      1.1  dholland 	 * Fudge together a funny name.
   2409  1.1.1.2  pgoyette 	 * Changing the format of the funny name to accommodate more
   2410      1.1  dholland 	 * sillynames per directory.
   2411      1.1  dholland 	 * The name is now changed to .nfs.<ticks>.<pid>.4, where ticks is
   2412      1.1  dholland 	 * CPU ticks since boot.
   2413      1.1  dholland 	 */
   2414      1.1  dholland 	pid = cnp->cn_thread->td_proc->p_pid;
   2415      1.1  dholland 	lticks = (unsigned int)ticks;
   2416      1.1  dholland 	for ( ; ; ) {
   2417      1.1  dholland 		sp->s_namlen = sprintf(sp->s_name,
   2418      1.1  dholland 				       ".nfs.%08x.%04x4.4", lticks,
   2419      1.1  dholland 				       pid);
   2420      1.1  dholland 		if (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
   2421      1.1  dholland 				 cnp->cn_thread, NULL))
   2422      1.1  dholland 			break;
   2423      1.1  dholland 		lticks++;
   2424      1.1  dholland 	}
   2425      1.1  dholland 	error = nfs_renameit(dvp, vp, cnp, sp);
   2426      1.1  dholland 	if (error)
   2427      1.1  dholland 		goto bad;
   2428      1.1  dholland 	error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
   2429      1.1  dholland 		cnp->cn_thread, &np);
   2430      1.1  dholland 	np->n_sillyrename = sp;
   2431      1.1  dholland 	return (0);
   2432      1.1  dholland bad:
   2433      1.1  dholland 	vrele(sp->s_dvp);
   2434      1.1  dholland 	crfree(sp->s_cred);
   2435      1.1  dholland 	free((caddr_t)sp, M_NEWNFSREQ);
   2436      1.1  dholland 	return (error);
   2437      1.1  dholland }
   2438      1.1  dholland 
   2439      1.1  dholland /*
   2440      1.1  dholland  * Look up a file name and optionally either update the file handle or
   2441      1.1  dholland  * allocate an nfsnode, depending on the value of npp.
   2442      1.1  dholland  * npp == NULL	--> just do the lookup
   2443      1.1  dholland  * *npp == NULL --> allocate a new nfsnode and make sure attributes are
   2444      1.1  dholland  *			handled too
   2445      1.1  dholland  * *npp != NULL --> update the file handle in the vnode
   2446      1.1  dholland  */
   2447      1.1  dholland static int
   2448      1.1  dholland nfs_lookitup(struct vnode *dvp, char *name, int len, struct ucred *cred,
   2449      1.1  dholland     struct thread *td, struct nfsnode **npp)
   2450      1.1  dholland {
   2451      1.1  dholland 	struct vnode *newvp = NULL, *vp;
   2452      1.1  dholland 	struct nfsnode *np, *dnp = VTONFS(dvp);
   2453      1.1  dholland 	struct nfsfh *nfhp, *onfhp;
   2454      1.1  dholland 	struct nfsvattr nfsva, dnfsva;
   2455      1.1  dholland 	struct componentname cn;
   2456      1.1  dholland 	int error = 0, attrflag, dattrflag;
   2457      1.1  dholland 	u_int hash;
   2458      1.1  dholland 
   2459      1.1  dholland 	error = nfsrpc_lookup(dvp, name, len, cred, td, &dnfsva, &nfsva,
   2460      1.1  dholland 	    &nfhp, &attrflag, &dattrflag, NULL);
   2461      1.1  dholland 	if (dattrflag)
   2462      1.1  dholland 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
   2463      1.1  dholland 	if (npp && !error) {
   2464      1.1  dholland 		if (*npp != NULL) {
   2465      1.1  dholland 		    np = *npp;
   2466      1.1  dholland 		    vp = NFSTOV(np);
   2467      1.1  dholland 		    /*
   2468      1.1  dholland 		     * For NFSv4, check to see if it is the same name and
   2469      1.1  dholland 		     * replace the name, if it is different.
   2470      1.1  dholland 		     */
   2471      1.1  dholland 		    if (np->n_v4 != NULL && nfsva.na_type == VREG &&
   2472      1.1  dholland 			(np->n_v4->n4_namelen != len ||
   2473      1.1  dholland 			 NFSBCMP(name, NFS4NODENAME(np->n_v4), len) ||
   2474      1.1  dholland 			 dnp->n_fhp->nfh_len != np->n_v4->n4_fhlen ||
   2475      1.1  dholland 			 NFSBCMP(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
   2476      1.1  dholland 			 dnp->n_fhp->nfh_len))) {
   2477      1.1  dholland #ifdef notdef
   2478      1.1  dholland { char nnn[100]; int nnnl;
   2479      1.1  dholland nnnl = (len < 100) ? len : 99;
   2480      1.1  dholland bcopy(name, nnn, nnnl);
   2481      1.1  dholland nnn[nnnl] = '\0';
   2482      1.1  dholland printf("replace=%s\n",nnn);
   2483      1.1  dholland }
   2484      1.1  dholland #endif
   2485      1.1  dholland 			    FREE((caddr_t)np->n_v4, M_NFSV4NODE);
   2486      1.1  dholland 			    MALLOC(np->n_v4, struct nfsv4node *,
   2487      1.1  dholland 				sizeof (struct nfsv4node) +
   2488      1.1  dholland 				dnp->n_fhp->nfh_len + len - 1,
   2489      1.1  dholland 				M_NFSV4NODE, M_WAITOK);
   2490      1.1  dholland 			    np->n_v4->n4_fhlen = dnp->n_fhp->nfh_len;
   2491      1.1  dholland 			    np->n_v4->n4_namelen = len;
   2492      1.1  dholland 			    NFSBCOPY(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
   2493      1.1  dholland 				dnp->n_fhp->nfh_len);
   2494      1.1  dholland 			    NFSBCOPY(name, NFS4NODENAME(np->n_v4), len);
   2495      1.1  dholland 		    }
   2496      1.1  dholland 		    hash = fnv_32_buf(nfhp->nfh_fh, nfhp->nfh_len,
   2497      1.1  dholland 			FNV1_32_INIT);
   2498      1.1  dholland 		    onfhp = np->n_fhp;
   2499      1.1  dholland 		    /*
   2500      1.1  dholland 		     * Rehash node for new file handle.
   2501      1.1  dholland 		     */
   2502      1.1  dholland 		    vfs_hash_rehash(vp, hash);
   2503      1.1  dholland 		    np->n_fhp = nfhp;
   2504      1.1  dholland 		    if (onfhp != NULL)
   2505      1.1  dholland 			FREE((caddr_t)onfhp, M_NFSFH);
   2506      1.1  dholland 		    newvp = NFSTOV(np);
   2507      1.1  dholland 		} else if (NFS_CMPFH(dnp, nfhp->nfh_fh, nfhp->nfh_len)) {
   2508      1.1  dholland 		    FREE((caddr_t)nfhp, M_NFSFH);
   2509      1.1  dholland 		    VREF(dvp);
   2510      1.1  dholland 		    newvp = dvp;
   2511      1.1  dholland 		} else {
   2512      1.1  dholland 		    cn.cn_nameptr = name;
   2513      1.1  dholland 		    cn.cn_namelen = len;
   2514      1.1  dholland 		    error = nfscl_nget(dvp->v_mount, dvp, nfhp, &cn, td,
   2515      1.1  dholland 			&np, NULL, LK_EXCLUSIVE);
   2516      1.1  dholland 		    if (error)
   2517      1.1  dholland 			return (error);
   2518      1.1  dholland 		    newvp = NFSTOV(np);
   2519      1.1  dholland 		}
   2520      1.1  dholland 		if (!attrflag && *npp == NULL) {
   2521      1.1  dholland 			if (newvp == dvp)
   2522      1.1  dholland 				vrele(newvp);
   2523      1.1  dholland 			else
   2524      1.1  dholland 				vput(newvp);
   2525      1.1  dholland 			return (ENOENT);
   2526      1.1  dholland 		}
   2527      1.1  dholland 		if (attrflag)
   2528      1.1  dholland 			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
   2529      1.1  dholland 			    0, 1);
   2530      1.1  dholland 	}
   2531      1.1  dholland 	if (npp && *npp == NULL) {
   2532      1.1  dholland 		if (error) {
   2533      1.1  dholland 			if (newvp) {
   2534      1.1  dholland 				if (newvp == dvp)
   2535      1.1  dholland 					vrele(newvp);
   2536      1.1  dholland 				else
   2537      1.1  dholland 					vput(newvp);
   2538      1.1  dholland 			}
   2539      1.1  dholland 		} else
   2540      1.1  dholland 			*npp = np;
   2541      1.1  dholland 	}
   2542      1.1  dholland 	if (error && NFS_ISV4(dvp))
   2543      1.1  dholland 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
   2544      1.1  dholland 	return (error);
   2545      1.1  dholland }
   2546      1.1  dholland 
   2547      1.1  dholland /*
   2548      1.1  dholland  * Nfs Version 3 and 4 commit rpc
   2549      1.1  dholland  */
   2550      1.1  dholland int
   2551      1.1  dholland ncl_commit(struct vnode *vp, u_quad_t offset, int cnt, struct ucred *cred,
   2552      1.1  dholland    struct thread *td)
   2553      1.1  dholland {
   2554      1.1  dholland 	struct nfsvattr nfsva;
   2555      1.1  dholland 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
   2556      1.1  dholland 	int error, attrflag;
   2557      1.1  dholland 
   2558      1.1  dholland 	mtx_lock(&nmp->nm_mtx);
   2559      1.1  dholland 	if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0) {
   2560      1.1  dholland 		mtx_unlock(&nmp->nm_mtx);
   2561      1.1  dholland 		return (0);
   2562      1.1  dholland 	}
   2563      1.1  dholland 	mtx_unlock(&nmp->nm_mtx);
   2564      1.1  dholland 	error = nfsrpc_commit(vp, offset, cnt, cred, td, &nfsva,
   2565      1.1  dholland 	    &attrflag, NULL);
   2566      1.1  dholland 	if (attrflag != 0)
   2567      1.1  dholland 		(void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL,
   2568      1.1  dholland 		    0, 1);
   2569      1.1  dholland 	if (error != 0 && NFS_ISV4(vp))
   2570      1.1  dholland 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
   2571      1.1  dholland 	return (error);
   2572      1.1  dholland }
   2573      1.1  dholland 
   2574      1.1  dholland /*
   2575      1.1  dholland  * Strategy routine.
   2576      1.1  dholland  * For async requests when nfsiod(s) are running, queue the request by
   2577      1.1  dholland  * calling ncl_asyncio(), otherwise just all ncl_doio() to do the
   2578      1.1  dholland  * request.
   2579      1.1  dholland  */
   2580      1.1  dholland static int
   2581      1.1  dholland nfs_strategy(struct vop_strategy_args *ap)
   2582      1.1  dholland {
   2583      1.1  dholland 	struct buf *bp = ap->a_bp;
   2584      1.1  dholland 	struct ucred *cr;
   2585      1.1  dholland 
   2586      1.1  dholland 	KASSERT(!(bp->b_flags & B_DONE),
   2587      1.1  dholland 	    ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
   2588      1.1  dholland 	BUF_ASSERT_HELD(bp);
   2589      1.1  dholland 
   2590      1.1  dholland 	if (bp->b_iocmd == BIO_READ)
   2591      1.1  dholland 		cr = bp->b_rcred;
   2592      1.1  dholland 	else
   2593      1.1  dholland 		cr = bp->b_wcred;
   2594      1.1  dholland 
   2595      1.1  dholland 	/*
   2596      1.1  dholland 	 * If the op is asynchronous and an i/o daemon is waiting
   2597      1.1  dholland 	 * queue the request, wake it up and wait for completion
   2598      1.1  dholland 	 * otherwise just do it ourselves.
   2599      1.1  dholland 	 */
   2600      1.1  dholland 	if ((bp->b_flags & B_ASYNC) == 0 ||
   2601      1.1  dholland 	    ncl_asyncio(VFSTONFS(ap->a_vp->v_mount), bp, NOCRED, curthread))
   2602      1.1  dholland 		(void) ncl_doio(ap->a_vp, bp, cr, curthread, 1);
   2603      1.1  dholland 	return (0);
   2604      1.1  dholland }
   2605      1.1  dholland 
   2606      1.1  dholland /*
   2607      1.1  dholland  * fsync vnode op. Just call ncl_flush() with commit == 1.
   2608      1.1  dholland  */
   2609      1.1  dholland /* ARGSUSED */
   2610      1.1  dholland static int
   2611      1.1  dholland nfs_fsync(struct vop_fsync_args *ap)
   2612      1.1  dholland {
   2613      1.1  dholland 
   2614      1.1  dholland 	if (ap->a_vp->v_type != VREG) {
   2615      1.1  dholland 		/*
   2616      1.1  dholland 		 * For NFS, metadata is changed synchronously on the server,
   2617      1.1  dholland 		 * so there is nothing to flush. Also, ncl_flush() clears
   2618      1.1  dholland 		 * the NMODIFIED flag and that shouldn't be done here for
   2619      1.1  dholland 		 * directories.
   2620      1.1  dholland 		 */
   2621      1.1  dholland 		return (0);
   2622      1.1  dholland 	}
   2623      1.1  dholland 	return (ncl_flush(ap->a_vp, ap->a_waitfor, NULL, ap->a_td, 1, 0));
   2624      1.1  dholland }
   2625      1.1  dholland 
   2626      1.1  dholland /*
   2627      1.1  dholland  * Flush all the blocks associated with a vnode.
   2628      1.1  dholland  * 	Walk through the buffer pool and push any dirty pages
   2629      1.1  dholland  *	associated with the vnode.
   2630      1.1  dholland  * If the called_from_renewthread argument is TRUE, it has been called
   2631      1.1  dholland  * from the NFSv4 renew thread and, as such, cannot block indefinitely
   2632      1.1  dholland  * waiting for a buffer write to complete.
   2633      1.1  dholland  */
   2634      1.1  dholland int
   2635      1.1  dholland ncl_flush(struct vnode *vp, int waitfor, struct ucred *cred, struct thread *td,
   2636      1.1  dholland     int commit, int called_from_renewthread)
   2637      1.1  dholland {
   2638      1.1  dholland 	struct nfsnode *np = VTONFS(vp);
   2639      1.1  dholland 	struct buf *bp;
   2640      1.1  dholland 	int i;
   2641      1.1  dholland 	struct buf *nbp;
   2642      1.1  dholland 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
   2643      1.1  dholland 	int error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
   2644      1.1  dholland 	int passone = 1, trycnt = 0;
   2645      1.1  dholland 	u_quad_t off, endoff, toff;
   2646      1.1  dholland 	struct ucred* wcred = NULL;
   2647      1.1  dholland 	struct buf **bvec = NULL;
   2648      1.1  dholland 	struct bufobj *bo;
   2649      1.1  dholland #ifndef NFS_COMMITBVECSIZ
   2650      1.1  dholland #define	NFS_COMMITBVECSIZ	20
   2651      1.1  dholland #endif
   2652      1.1  dholland 	struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
   2653      1.1  dholland 	int bvecsize = 0, bveccount;
   2654      1.1  dholland 
   2655      1.1  dholland 	if (called_from_renewthread != 0)
   2656      1.1  dholland 		slptimeo = hz;
   2657      1.1  dholland 	if (nmp->nm_flag & NFSMNT_INT)
   2658      1.1  dholland 		slpflag = PCATCH;
   2659      1.1  dholland 	if (!commit)
   2660      1.1  dholland 		passone = 0;
   2661      1.1  dholland 	bo = &vp->v_bufobj;
   2662      1.1  dholland 	/*
   2663      1.1  dholland 	 * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
   2664      1.1  dholland 	 * server, but has not been committed to stable storage on the server
   2665      1.1  dholland 	 * yet. On the first pass, the byte range is worked out and the commit
   2666      1.1  dholland 	 * rpc is done. On the second pass, ncl_writebp() is called to do the
   2667      1.1  dholland 	 * job.
   2668      1.1  dholland 	 */
   2669      1.1  dholland again:
   2670      1.1  dholland 	off = (u_quad_t)-1;
   2671      1.1  dholland 	endoff = 0;
   2672      1.1  dholland 	bvecpos = 0;
   2673      1.1  dholland 	if (NFS_ISV34(vp) && commit) {
   2674      1.1  dholland 		if (bvec != NULL && bvec != bvec_on_stack)
   2675      1.1  dholland 			free(bvec, M_TEMP);
   2676      1.1  dholland 		/*
   2677      1.1  dholland 		 * Count up how many buffers waiting for a commit.
   2678      1.1  dholland 		 */
   2679      1.1  dholland 		bveccount = 0;
   2680      1.1  dholland 		BO_LOCK(bo);
   2681      1.1  dholland 		TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
   2682      1.1  dholland 			if (!BUF_ISLOCKED(bp) &&
   2683      1.1  dholland 			    (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
   2684      1.1  dholland 				== (B_DELWRI | B_NEEDCOMMIT))
   2685      1.1  dholland 				bveccount++;
   2686      1.1  dholland 		}
   2687      1.1  dholland 		/*
   2688      1.1  dholland 		 * Allocate space to remember the list of bufs to commit.  It is
   2689      1.1  dholland 		 * important to use M_NOWAIT here to avoid a race with nfs_write.
   2690      1.1  dholland 		 * If we can't get memory (for whatever reason), we will end up
   2691      1.1  dholland 		 * committing the buffers one-by-one in the loop below.
   2692      1.1  dholland 		 */
   2693      1.1  dholland 		if (bveccount > NFS_COMMITBVECSIZ) {
   2694      1.1  dholland 			/*
   2695      1.1  dholland 			 * Release the vnode interlock to avoid a lock
   2696      1.1  dholland 			 * order reversal.
   2697      1.1  dholland 			 */
   2698      1.1  dholland 			BO_UNLOCK(bo);
   2699      1.1  dholland 			bvec = (struct buf **)
   2700      1.1  dholland 				malloc(bveccount * sizeof(struct buf *),
   2701      1.1  dholland 				       M_TEMP, M_NOWAIT);
   2702      1.1  dholland 			BO_LOCK(bo);
   2703      1.1  dholland 			if (bvec == NULL) {
   2704      1.1  dholland 				bvec = bvec_on_stack;
   2705      1.1  dholland 				bvecsize = NFS_COMMITBVECSIZ;
   2706      1.1  dholland 			} else
   2707      1.1  dholland 				bvecsize = bveccount;
   2708      1.1  dholland 		} else {
   2709      1.1  dholland 			bvec = bvec_on_stack;
   2710      1.1  dholland 			bvecsize = NFS_COMMITBVECSIZ;
   2711      1.1  dholland 		}
   2712      1.1  dholland 		TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
   2713      1.1  dholland 			if (bvecpos >= bvecsize)
   2714      1.1  dholland 				break;
   2715      1.1  dholland 			if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
   2716      1.1  dholland 				nbp = TAILQ_NEXT(bp, b_bobufs);
   2717      1.1  dholland 				continue;
   2718      1.1  dholland 			}
   2719      1.1  dholland 			if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
   2720      1.1  dholland 			    (B_DELWRI | B_NEEDCOMMIT)) {
   2721      1.1  dholland 				BUF_UNLOCK(bp);
   2722      1.1  dholland 				nbp = TAILQ_NEXT(bp, b_bobufs);
   2723      1.1  dholland 				continue;
   2724      1.1  dholland 			}
   2725      1.1  dholland 			BO_UNLOCK(bo);
   2726      1.1  dholland 			bremfree(bp);
   2727      1.1  dholland 			/*
   2728      1.1  dholland 			 * Work out if all buffers are using the same cred
   2729      1.1  dholland 			 * so we can deal with them all with one commit.
   2730      1.1  dholland 			 *
   2731      1.1  dholland 			 * NOTE: we are not clearing B_DONE here, so we have
   2732      1.1  dholland 			 * to do it later on in this routine if we intend to
   2733      1.1  dholland 			 * initiate I/O on the bp.
   2734      1.1  dholland 			 *
   2735      1.1  dholland 			 * Note: to avoid loopback deadlocks, we do not
   2736      1.1  dholland 			 * assign b_runningbufspace.
   2737      1.1  dholland 			 */
   2738      1.1  dholland 			if (wcred == NULL)
   2739      1.1  dholland 				wcred = bp->b_wcred;
   2740      1.1  dholland 			else if (wcred != bp->b_wcred)
   2741      1.1  dholland 				wcred = NOCRED;
   2742      1.1  dholland 			vfs_busy_pages(bp, 1);
   2743      1.1  dholland 
   2744      1.1  dholland 			BO_LOCK(bo);
   2745      1.1  dholland 			/*
   2746      1.1  dholland 			 * bp is protected by being locked, but nbp is not
   2747      1.1  dholland 			 * and vfs_busy_pages() may sleep.  We have to
   2748      1.1  dholland 			 * recalculate nbp.
   2749      1.1  dholland 			 */
   2750      1.1  dholland 			nbp = TAILQ_NEXT(bp, b_bobufs);
   2751      1.1  dholland 
   2752      1.1  dholland 			/*
   2753      1.1  dholland 			 * A list of these buffers is kept so that the
   2754      1.1  dholland 			 * second loop knows which buffers have actually
   2755      1.1  dholland 			 * been committed. This is necessary, since there
   2756      1.1  dholland 			 * may be a race between the commit rpc and new
   2757      1.1  dholland 			 * uncommitted writes on the file.
   2758      1.1  dholland 			 */
   2759      1.1  dholland 			bvec[bvecpos++] = bp;
   2760      1.1  dholland 			toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
   2761      1.1  dholland 				bp->b_dirtyoff;
   2762      1.1  dholland 			if (toff < off)
   2763      1.1  dholland 				off = toff;
   2764      1.1  dholland 			toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
   2765      1.1  dholland 			if (toff > endoff)
   2766      1.1  dholland 				endoff = toff;
   2767      1.1  dholland 		}
   2768      1.1  dholland 		BO_UNLOCK(bo);
   2769      1.1  dholland 	}
   2770      1.1  dholland 	if (bvecpos > 0) {
   2771      1.1  dholland 		/*
   2772      1.1  dholland 		 * Commit data on the server, as required.
   2773      1.1  dholland 		 * If all bufs are using the same wcred, then use that with
   2774      1.1  dholland 		 * one call for all of them, otherwise commit each one
   2775      1.1  dholland 		 * separately.
   2776      1.1  dholland 		 */
   2777      1.1  dholland 		if (wcred != NOCRED)
   2778      1.1  dholland 			retv = ncl_commit(vp, off, (int)(endoff - off),
   2779      1.1  dholland 					  wcred, td);
   2780      1.1  dholland 		else {
   2781      1.1  dholland 			retv = 0;
   2782      1.1  dholland 			for (i = 0; i < bvecpos; i++) {
   2783      1.1  dholland 				off_t off, size;
   2784      1.1  dholland 				bp = bvec[i];
   2785      1.1  dholland 				off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
   2786      1.1  dholland 					bp->b_dirtyoff;
   2787      1.1  dholland 				size = (u_quad_t)(bp->b_dirtyend
   2788      1.1  dholland 						  - bp->b_dirtyoff);
   2789      1.1  dholland 				retv = ncl_commit(vp, off, (int)size,
   2790      1.1  dholland 						  bp->b_wcred, td);
   2791      1.1  dholland 				if (retv) break;
   2792      1.1  dholland 			}
   2793      1.1  dholland 		}
   2794      1.1  dholland 
   2795      1.1  dholland 		if (retv == NFSERR_STALEWRITEVERF)
   2796      1.1  dholland 			ncl_clearcommit(vp->v_mount);
   2797      1.1  dholland 
   2798      1.1  dholland 		/*
   2799      1.1  dholland 		 * Now, either mark the blocks I/O done or mark the
   2800      1.1  dholland 		 * blocks dirty, depending on whether the commit
   2801      1.1  dholland 		 * succeeded.
   2802      1.1  dholland 		 */
   2803      1.1  dholland 		for (i = 0; i < bvecpos; i++) {
   2804      1.1  dholland 			bp = bvec[i];
   2805      1.1  dholland 			bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
   2806      1.1  dholland 			if (retv) {
   2807      1.1  dholland 				/*
   2808      1.1  dholland 				 * Error, leave B_DELWRI intact
   2809      1.1  dholland 				 */
   2810      1.1  dholland 				vfs_unbusy_pages(bp);
   2811      1.1  dholland 				brelse(bp);
   2812      1.1  dholland 			} else {
   2813      1.1  dholland 				/*
   2814      1.1  dholland 				 * Success, remove B_DELWRI ( bundirty() ).
   2815      1.1  dholland 				 *
   2816      1.1  dholland 				 * b_dirtyoff/b_dirtyend seem to be NFS
   2817      1.1  dholland 				 * specific.  We should probably move that
   2818      1.1  dholland 				 * into bundirty(). XXX
   2819      1.1  dholland 				 */
   2820      1.1  dholland 				bufobj_wref(bo);
   2821      1.1  dholland 				bp->b_flags |= B_ASYNC;
   2822      1.1  dholland 				bundirty(bp);
   2823      1.1  dholland 				bp->b_flags &= ~B_DONE;
   2824      1.1  dholland 				bp->b_ioflags &= ~BIO_ERROR;
   2825      1.1  dholland 				bp->b_dirtyoff = bp->b_dirtyend = 0;
   2826      1.1  dholland 				bufdone(bp);
   2827      1.1  dholland 			}
   2828      1.1  dholland 		}
   2829      1.1  dholland 	}
   2830      1.1  dholland 
   2831      1.1  dholland 	/*
   2832      1.1  dholland 	 * Start/do any write(s) that are required.
   2833      1.1  dholland 	 */
   2834      1.1  dholland loop:
   2835      1.1  dholland 	BO_LOCK(bo);
   2836      1.1  dholland 	TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
   2837      1.1  dholland 		if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
   2838      1.1  dholland 			if (waitfor != MNT_WAIT || passone)
   2839      1.1  dholland 				continue;
   2840      1.1  dholland 
   2841      1.1  dholland 			error = BUF_TIMELOCK(bp,
   2842      1.1  dholland 			    LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
   2843      1.1  dholland 			    BO_LOCKPTR(bo), "nfsfsync", slpflag, slptimeo);
   2844      1.1  dholland 			if (error == 0) {
   2845      1.1  dholland 				BUF_UNLOCK(bp);
   2846      1.1  dholland 				goto loop;
   2847      1.1  dholland 			}
   2848      1.1  dholland 			if (error == ENOLCK) {
   2849      1.1  dholland 				error = 0;
   2850      1.1  dholland 				goto loop;
   2851      1.1  dholland 			}
   2852      1.1  dholland 			if (called_from_renewthread != 0) {
   2853      1.1  dholland 				/*
   2854      1.1  dholland 				 * Return EIO so the flush will be retried
   2855      1.1  dholland 				 * later.
   2856      1.1  dholland 				 */
   2857      1.1  dholland 				error = EIO;
   2858      1.1  dholland 				goto done;
   2859      1.1  dholland 			}
   2860      1.1  dholland 			if (newnfs_sigintr(nmp, td)) {
   2861      1.1  dholland 				error = EINTR;
   2862      1.1  dholland 				goto done;
   2863      1.1  dholland 			}
   2864      1.1  dholland 			if (slpflag == PCATCH) {
   2865      1.1  dholland 				slpflag = 0;
   2866      1.1  dholland 				slptimeo = 2 * hz;
   2867      1.1  dholland 			}
   2868      1.1  dholland 			goto loop;
   2869      1.1  dholland 		}
   2870      1.1  dholland 		if ((bp->b_flags & B_DELWRI) == 0)
   2871      1.1  dholland 			panic("nfs_fsync: not dirty");
   2872      1.1  dholland 		if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) {
   2873      1.1  dholland 			BUF_UNLOCK(bp);
   2874      1.1  dholland 			continue;
   2875      1.1  dholland 		}
   2876      1.1  dholland 		BO_UNLOCK(bo);
   2877      1.1  dholland 		bremfree(bp);
   2878      1.1  dholland 		if (passone || !commit)
   2879      1.1  dholland 		    bp->b_flags |= B_ASYNC;
   2880      1.1  dholland 		else
   2881      1.1  dholland 		    bp->b_flags |= B_ASYNC;
   2882      1.1  dholland 		bwrite(bp);
   2883      1.1  dholland 		if (newnfs_sigintr(nmp, td)) {
   2884      1.1  dholland 			error = EINTR;
   2885      1.1  dholland 			goto done;
   2886      1.1  dholland 		}
   2887      1.1  dholland 		goto loop;
   2888      1.1  dholland 	}
   2889      1.1  dholland 	if (passone) {
   2890      1.1  dholland 		passone = 0;
   2891      1.1  dholland 		BO_UNLOCK(bo);
   2892      1.1  dholland 		goto again;
   2893      1.1  dholland 	}
   2894      1.1  dholland 	if (waitfor == MNT_WAIT) {
   2895      1.1  dholland 		while (bo->bo_numoutput) {
   2896      1.1  dholland 			error = bufobj_wwait(bo, slpflag, slptimeo);
   2897      1.1  dholland 			if (error) {
   2898      1.1  dholland 			    BO_UNLOCK(bo);
   2899      1.1  dholland 			    if (called_from_renewthread != 0) {
   2900      1.1  dholland 				/*
   2901      1.1  dholland 				 * Return EIO so that the flush will be
   2902      1.1  dholland 				 * retried later.
   2903      1.1  dholland 				 */
   2904      1.1  dholland 				error = EIO;
   2905      1.1  dholland 				goto done;
   2906      1.1  dholland 			    }
   2907      1.1  dholland 			    error = newnfs_sigintr(nmp, td);
   2908      1.1  dholland 			    if (error)
   2909      1.1  dholland 				goto done;
   2910      1.1  dholland 			    if (slpflag == PCATCH) {
   2911      1.1  dholland 				slpflag = 0;
   2912      1.1  dholland 				slptimeo = 2 * hz;
   2913      1.1  dholland 			    }
   2914      1.1  dholland 			    BO_LOCK(bo);
   2915      1.1  dholland 			}
   2916      1.1  dholland 		}
   2917      1.1  dholland 		if (bo->bo_dirty.bv_cnt != 0 && commit) {
   2918      1.1  dholland 			BO_UNLOCK(bo);
   2919      1.1  dholland 			goto loop;
   2920      1.1  dholland 		}
   2921      1.1  dholland 		/*
   2922      1.1  dholland 		 * Wait for all the async IO requests to drain
   2923      1.1  dholland 		 */
   2924      1.1  dholland 		BO_UNLOCK(bo);
   2925      1.1  dholland 		mtx_lock(&np->n_mtx);
   2926      1.1  dholland 		while (np->n_directio_asyncwr > 0) {
   2927      1.1  dholland 			np->n_flag |= NFSYNCWAIT;
   2928      1.1  dholland 			error = newnfs_msleep(td, &np->n_directio_asyncwr,
   2929      1.1  dholland 			    &np->n_mtx, slpflag | (PRIBIO + 1),
   2930      1.1  dholland 			    "nfsfsync", 0);
   2931      1.1  dholland 			if (error) {
   2932      1.1  dholland 				if (newnfs_sigintr(nmp, td)) {
   2933      1.1  dholland 					mtx_unlock(&np->n_mtx);
   2934      1.1  dholland 					error = EINTR;
   2935      1.1  dholland 					goto done;
   2936      1.1  dholland 				}
   2937      1.1  dholland 			}
   2938      1.1  dholland 		}
   2939      1.1  dholland 		mtx_unlock(&np->n_mtx);
   2940      1.1  dholland 	} else
   2941      1.1  dholland 		BO_UNLOCK(bo);
   2942      1.1  dholland 	if (NFSHASPNFS(nmp)) {
   2943      1.1  dholland 		nfscl_layoutcommit(vp, td);
   2944      1.1  dholland 		/*
   2945      1.1  dholland 		 * Invalidate the attribute cache, since writes to a DS
   2946      1.1  dholland 		 * won't update the size attribute.
   2947      1.1  dholland 		 */
   2948      1.1  dholland 		mtx_lock(&np->n_mtx);
   2949      1.1  dholland 		np->n_attrstamp = 0;
   2950      1.1  dholland 	} else
   2951      1.1  dholland 		mtx_lock(&np->n_mtx);
   2952      1.1  dholland 	if (np->n_flag & NWRITEERR) {
   2953      1.1  dholland 		error = np->n_error;
   2954      1.1  dholland 		np->n_flag &= ~NWRITEERR;
   2955      1.1  dholland 	}
   2956      1.1  dholland   	if (commit && bo->bo_dirty.bv_cnt == 0 &&
   2957      1.1  dholland 	    bo->bo_numoutput == 0 && np->n_directio_asyncwr == 0)
   2958      1.1  dholland   		np->n_flag &= ~NMODIFIED;
   2959      1.1  dholland 	mtx_unlock(&np->n_mtx);
   2960      1.1  dholland done:
   2961      1.1  dholland 	if (bvec != NULL && bvec != bvec_on_stack)
   2962      1.1  dholland 		free(bvec, M_TEMP);
   2963      1.1  dholland 	if (error == 0 && commit != 0 && waitfor == MNT_WAIT &&
   2964      1.1  dholland 	    (bo->bo_dirty.bv_cnt != 0 || bo->bo_numoutput != 0 ||
   2965      1.1  dholland 	     np->n_directio_asyncwr != 0) && trycnt++ < 5) {
   2966      1.1  dholland 		/* try, try again... */
   2967      1.1  dholland 		passone = 1;
   2968      1.1  dholland 		wcred = NULL;
   2969      1.1  dholland 		bvec = NULL;
   2970      1.1  dholland 		bvecsize = 0;
   2971      1.1  dholland printf("try%d\n", trycnt);
   2972      1.1  dholland 		goto again;
   2973      1.1  dholland 	}
   2974      1.1  dholland 	return (error);
   2975      1.1  dholland }
   2976      1.1  dholland 
   2977      1.1  dholland /*
   2978      1.1  dholland  * NFS advisory byte-level locks.
   2979      1.1  dholland  */
   2980      1.1  dholland static int
   2981      1.1  dholland nfs_advlock(struct vop_advlock_args *ap)
   2982      1.1  dholland {
   2983      1.1  dholland 	struct vnode *vp = ap->a_vp;
   2984      1.1  dholland 	struct ucred *cred;
   2985      1.1  dholland 	struct nfsnode *np = VTONFS(ap->a_vp);
   2986      1.1  dholland 	struct proc *p = (struct proc *)ap->a_id;
   2987      1.1  dholland 	struct thread *td = curthread;	/* XXX */
   2988      1.1  dholland 	struct vattr va;
   2989      1.1  dholland 	int ret, error = EOPNOTSUPP;
   2990      1.1  dholland 	u_quad_t size;
   2991      1.1  dholland 
   2992      1.1  dholland 	if (NFS_ISV4(vp) && (ap->a_flags & (F_POSIX | F_FLOCK)) != 0) {
   2993      1.1  dholland 		if (vp->v_type != VREG)
   2994      1.1  dholland 			return (EINVAL);
   2995      1.1  dholland 		if ((ap->a_flags & F_POSIX) != 0)
   2996      1.1  dholland 			cred = p->p_ucred;
   2997      1.1  dholland 		else
   2998      1.1  dholland 			cred = td->td_ucred;
   2999      1.1  dholland 		NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
   3000      1.1  dholland 		if (vp->v_iflag & VI_DOOMED) {
   3001      1.1  dholland 			NFSVOPUNLOCK(vp, 0);
   3002      1.1  dholland 			return (EBADF);
   3003      1.1  dholland 		}
   3004      1.1  dholland 
   3005      1.1  dholland 		/*
   3006      1.1  dholland 		 * If this is unlocking a write locked region, flush and
   3007      1.1  dholland 		 * commit them before unlocking. This is required by
   3008      1.1  dholland 		 * RFC3530 Sec. 9.3.2.
   3009      1.1  dholland 		 */
   3010      1.1  dholland 		if (ap->a_op == F_UNLCK &&
   3011      1.1  dholland 		    nfscl_checkwritelocked(vp, ap->a_fl, cred, td, ap->a_id,
   3012      1.1  dholland 		    ap->a_flags))
   3013      1.1  dholland 			(void) ncl_flush(vp, MNT_WAIT, cred, td, 1, 0);
   3014      1.1  dholland 
   3015      1.1  dholland 		/*
   3016      1.1  dholland 		 * Loop around doing the lock op, while a blocking lock
   3017      1.1  dholland 		 * must wait for the lock op to succeed.
   3018      1.1  dholland 		 */
   3019      1.1  dholland 		do {
   3020      1.1  dholland 			ret = nfsrpc_advlock(vp, np->n_size, ap->a_op,
   3021      1.1  dholland 			    ap->a_fl, 0, cred, td, ap->a_id, ap->a_flags);
   3022      1.1  dholland 			if (ret == NFSERR_DENIED && (ap->a_flags & F_WAIT) &&
   3023      1.1  dholland 			    ap->a_op == F_SETLK) {
   3024      1.1  dholland 				NFSVOPUNLOCK(vp, 0);
   3025      1.1  dholland 				error = nfs_catnap(PZERO | PCATCH, ret,
   3026      1.1  dholland 				    "ncladvl");
   3027      1.1  dholland 				if (error)
   3028      1.1  dholland 					return (EINTR);
   3029      1.1  dholland 				NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
   3030      1.1  dholland 				if (vp->v_iflag & VI_DOOMED) {
   3031      1.1  dholland 					NFSVOPUNLOCK(vp, 0);
   3032      1.1  dholland 					return (EBADF);
   3033      1.1  dholland 				}
   3034      1.1  dholland 			}
   3035      1.1  dholland 		} while (ret == NFSERR_DENIED && (ap->a_flags & F_WAIT) &&
   3036      1.1  dholland 		     ap->a_op == F_SETLK);
   3037      1.1  dholland 		if (ret == NFSERR_DENIED) {
   3038      1.1  dholland 			NFSVOPUNLOCK(vp, 0);
   3039      1.1  dholland 			return (EAGAIN);
   3040      1.1  dholland 		} else if (ret == EINVAL || ret == EBADF || ret == EINTR) {
   3041      1.1  dholland 			NFSVOPUNLOCK(vp, 0);
   3042      1.1  dholland 			return (ret);
   3043      1.1  dholland 		} else if (ret != 0) {
   3044      1.1  dholland 			NFSVOPUNLOCK(vp, 0);
   3045      1.1  dholland 			return (EACCES);
   3046      1.1  dholland 		}
   3047      1.1  dholland 
   3048      1.1  dholland 		/*
   3049      1.1  dholland 		 * Now, if we just got a lock, invalidate data in the buffer
   3050      1.1  dholland 		 * cache, as required, so that the coherency conforms with
   3051      1.1  dholland 		 * RFC3530 Sec. 9.3.2.
   3052      1.1  dholland 		 */
   3053      1.1  dholland 		if (ap->a_op == F_SETLK) {
   3054      1.1  dholland 			if ((np->n_flag & NMODIFIED) == 0) {
   3055      1.1  dholland 				np->n_attrstamp = 0;
   3056      1.1  dholland 				KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
   3057      1.1  dholland 				ret = VOP_GETATTR(vp, &va, cred);
   3058      1.1  dholland 			}
   3059      1.1  dholland 			if ((np->n_flag & NMODIFIED) || ret ||
   3060      1.1  dholland 			    np->n_change != va.va_filerev) {
   3061      1.1  dholland 				(void) ncl_vinvalbuf(vp, V_SAVE, td, 1);
   3062      1.1  dholland 				np->n_attrstamp = 0;
   3063      1.1  dholland 				KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
   3064      1.1  dholland 				ret = VOP_GETATTR(vp, &va, cred);
   3065      1.1  dholland 				if (!ret) {
   3066      1.1  dholland 					np->n_mtime = va.va_mtime;
   3067      1.1  dholland 					np->n_change = va.va_filerev;
   3068      1.1  dholland 				}
   3069      1.1  dholland 			}
   3070  1.1.1.2  pgoyette 			/* Mark that a file lock has been acquired. */
   3071  1.1.1.2  pgoyette 			mtx_lock(&np->n_mtx);
   3072  1.1.1.2  pgoyette 			np->n_flag |= NHASBEENLOCKED;
   3073  1.1.1.2  pgoyette 			mtx_unlock(&np->n_mtx);
   3074      1.1  dholland 		}
   3075      1.1  dholland 		NFSVOPUNLOCK(vp, 0);
   3076      1.1  dholland 		return (0);
   3077      1.1  dholland 	} else if (!NFS_ISV4(vp)) {
   3078      1.1  dholland 		error = NFSVOPLOCK(vp, LK_SHARED);
   3079      1.1  dholland 		if (error)
   3080      1.1  dholland 			return (error);
   3081      1.1  dholland 		if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
   3082      1.1  dholland 			size = VTONFS(vp)->n_size;
   3083      1.1  dholland 			NFSVOPUNLOCK(vp, 0);
   3084      1.1  dholland 			error = lf_advlock(ap, &(vp->v_lockf), size);
   3085      1.1  dholland 		} else {
   3086      1.1  dholland 			if (nfs_advlock_p != NULL)
   3087      1.1  dholland 				error = nfs_advlock_p(ap);
   3088      1.1  dholland 			else {
   3089      1.1  dholland 				NFSVOPUNLOCK(vp, 0);
   3090      1.1  dholland 				error = ENOLCK;
   3091      1.1  dholland 			}
   3092      1.1  dholland 		}
   3093  1.1.1.2  pgoyette 		if (error == 0 && ap->a_op == F_SETLK) {
   3094  1.1.1.2  pgoyette 			error = NFSVOPLOCK(vp, LK_SHARED);
   3095  1.1.1.2  pgoyette 			if (error == 0) {
   3096  1.1.1.2  pgoyette 				/* Mark that a file lock has been acquired. */
   3097  1.1.1.2  pgoyette 				mtx_lock(&np->n_mtx);
   3098  1.1.1.2  pgoyette 				np->n_flag |= NHASBEENLOCKED;
   3099  1.1.1.2  pgoyette 				mtx_unlock(&np->n_mtx);
   3100  1.1.1.2  pgoyette 				NFSVOPUNLOCK(vp, 0);
   3101  1.1.1.2  pgoyette 			}
   3102  1.1.1.2  pgoyette 		}
   3103      1.1  dholland 	}
   3104      1.1  dholland 	return (error);
   3105      1.1  dholland }
   3106      1.1  dholland 
   3107      1.1  dholland /*
   3108      1.1  dholland  * NFS advisory byte-level locks.
   3109      1.1  dholland  */
   3110      1.1  dholland static int
   3111      1.1  dholland nfs_advlockasync(struct vop_advlockasync_args *ap)
   3112      1.1  dholland {
   3113      1.1  dholland 	struct vnode *vp = ap->a_vp;
   3114      1.1  dholland 	u_quad_t size;
   3115      1.1  dholland 	int error;
   3116      1.1  dholland 
   3117      1.1  dholland 	if (NFS_ISV4(vp))
   3118      1.1  dholland 		return (EOPNOTSUPP);
   3119      1.1  dholland 	error = NFSVOPLOCK(vp, LK_SHARED);
   3120      1.1  dholland 	if (error)
   3121      1.1  dholland 		return (error);
   3122      1.1  dholland 	if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
   3123      1.1  dholland 		size = VTONFS(vp)->n_size;
   3124      1.1  dholland 		NFSVOPUNLOCK(vp, 0);
   3125      1.1  dholland 		error = lf_advlockasync(ap, &(vp->v_lockf), size);
   3126      1.1  dholland 	} else {
   3127      1.1  dholland 		NFSVOPUNLOCK(vp, 0);
   3128      1.1  dholland 		error = EOPNOTSUPP;
   3129      1.1  dholland 	}
   3130      1.1  dholland 	return (error);
   3131      1.1  dholland }
   3132      1.1  dholland 
   3133      1.1  dholland /*
   3134      1.1  dholland  * Print out the contents of an nfsnode.
   3135      1.1  dholland  */
   3136      1.1  dholland static int
   3137      1.1  dholland nfs_print(struct vop_print_args *ap)
   3138      1.1  dholland {
   3139      1.1  dholland 	struct vnode *vp = ap->a_vp;
   3140      1.1  dholland 	struct nfsnode *np = VTONFS(vp);
   3141      1.1  dholland 
   3142  1.1.1.2  pgoyette 	printf("\tfileid %ld fsid 0x%x", np->n_vattr.na_fileid,
   3143  1.1.1.2  pgoyette 	    np->n_vattr.na_fsid);
   3144      1.1  dholland 	if (vp->v_type == VFIFO)
   3145      1.1  dholland 		fifo_printinfo(vp);
   3146      1.1  dholland 	printf("\n");
   3147      1.1  dholland 	return (0);
   3148      1.1  dholland }
   3149      1.1  dholland 
   3150      1.1  dholland /*
   3151      1.1  dholland  * This is the "real" nfs::bwrite(struct buf*).
   3152      1.1  dholland  * We set B_CACHE if this is a VMIO buffer.
   3153      1.1  dholland  */
   3154      1.1  dholland int
   3155      1.1  dholland ncl_writebp(struct buf *bp, int force __unused, struct thread *td)
   3156      1.1  dholland {
   3157      1.1  dholland 	int s;
   3158      1.1  dholland 	int oldflags = bp->b_flags;
   3159      1.1  dholland #if 0
   3160      1.1  dholland 	int retv = 1;
   3161      1.1  dholland 	off_t off;
   3162      1.1  dholland #endif
   3163      1.1  dholland 
   3164      1.1  dholland 	BUF_ASSERT_HELD(bp);
   3165      1.1  dholland 
   3166      1.1  dholland 	if (bp->b_flags & B_INVAL) {
   3167      1.1  dholland 		brelse(bp);
   3168      1.1  dholland 		return(0);
   3169      1.1  dholland 	}
   3170      1.1  dholland 
   3171      1.1  dholland 	bp->b_flags |= B_CACHE;
   3172      1.1  dholland 
   3173      1.1  dholland 	/*
   3174      1.1  dholland 	 * Undirty the bp.  We will redirty it later if the I/O fails.
   3175      1.1  dholland 	 */
   3176      1.1  dholland 
   3177      1.1  dholland 	s = splbio();
   3178      1.1  dholland 	bundirty(bp);
   3179      1.1  dholland 	bp->b_flags &= ~B_DONE;
   3180      1.1  dholland 	bp->b_ioflags &= ~BIO_ERROR;
   3181      1.1  dholland 	bp->b_iocmd = BIO_WRITE;
   3182      1.1  dholland 
   3183      1.1  dholland 	bufobj_wref(bp->b_bufobj);
   3184      1.1  dholland 	curthread->td_ru.ru_oublock++;
   3185      1.1  dholland 	splx(s);
   3186      1.1  dholland 
   3187      1.1  dholland 	/*
   3188      1.1  dholland 	 * Note: to avoid loopback deadlocks, we do not
   3189      1.1  dholland 	 * assign b_runningbufspace.
   3190      1.1  dholland 	 */
   3191      1.1  dholland 	vfs_busy_pages(bp, 1);
   3192      1.1  dholland 
   3193      1.1  dholland 	BUF_KERNPROC(bp);
   3194      1.1  dholland 	bp->b_iooffset = dbtob(bp->b_blkno);
   3195      1.1  dholland 	bstrategy(bp);
   3196      1.1  dholland 
   3197      1.1  dholland 	if( (oldflags & B_ASYNC) == 0) {
   3198      1.1  dholland 		int rtval = bufwait(bp);
   3199      1.1  dholland 
   3200      1.1  dholland 		if (oldflags & B_DELWRI) {
   3201      1.1  dholland 			s = splbio();
   3202      1.1  dholland 			reassignbuf(bp);
   3203      1.1  dholland 			splx(s);
   3204      1.1  dholland 		}
   3205      1.1  dholland 		brelse(bp);
   3206      1.1  dholland 		return (rtval);
   3207      1.1  dholland 	}
   3208      1.1  dholland 
   3209      1.1  dholland 	return (0);
   3210      1.1  dholland }
   3211      1.1  dholland 
   3212      1.1  dholland /*
   3213      1.1  dholland  * nfs special file access vnode op.
   3214      1.1  dholland  * Essentially just get vattr and then imitate iaccess() since the device is
   3215      1.1  dholland  * local to the client.
   3216      1.1  dholland  */
   3217      1.1  dholland static int
   3218      1.1  dholland nfsspec_access(struct vop_access_args *ap)
   3219      1.1  dholland {
   3220      1.1  dholland 	struct vattr *vap;
   3221      1.1  dholland 	struct ucred *cred = ap->a_cred;
   3222      1.1  dholland 	struct vnode *vp = ap->a_vp;
   3223      1.1  dholland 	accmode_t accmode = ap->a_accmode;
   3224      1.1  dholland 	struct vattr vattr;
   3225      1.1  dholland 	int error;
   3226      1.1  dholland 
   3227      1.1  dholland 	/*
   3228      1.1  dholland 	 * Disallow write attempts on filesystems mounted read-only;
   3229      1.1  dholland 	 * unless the file is a socket, fifo, or a block or character
   3230      1.1  dholland 	 * device resident on the filesystem.
   3231      1.1  dholland 	 */
   3232      1.1  dholland 	if ((accmode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
   3233      1.1  dholland 		switch (vp->v_type) {
   3234      1.1  dholland 		case VREG:
   3235      1.1  dholland 		case VDIR:
   3236      1.1  dholland 		case VLNK:
   3237      1.1  dholland 			return (EROFS);
   3238      1.1  dholland 		default:
   3239      1.1  dholland 			break;
   3240      1.1  dholland 		}
   3241      1.1  dholland 	}
   3242      1.1  dholland 	vap = &vattr;
   3243      1.1  dholland 	error = VOP_GETATTR(vp, vap, cred);
   3244      1.1  dholland 	if (error)
   3245      1.1  dholland 		goto out;
   3246      1.1  dholland 	error  = vaccess(vp->v_type, vap->va_mode, vap->va_uid, vap->va_gid,
   3247      1.1  dholland 	    accmode, cred, NULL);
   3248      1.1  dholland out:
   3249      1.1  dholland 	return error;
   3250      1.1  dholland }
   3251      1.1  dholland 
   3252      1.1  dholland /*
   3253      1.1  dholland  * Read wrapper for fifos.
   3254      1.1  dholland  */
   3255      1.1  dholland static int
   3256      1.1  dholland nfsfifo_read(struct vop_read_args *ap)
   3257      1.1  dholland {
   3258      1.1  dholland 	struct nfsnode *np = VTONFS(ap->a_vp);
   3259      1.1  dholland 	int error;
   3260      1.1  dholland 
   3261      1.1  dholland 	/*
   3262      1.1  dholland 	 * Set access flag.
   3263      1.1  dholland 	 */
   3264      1.1  dholland 	mtx_lock(&np->n_mtx);
   3265      1.1  dholland 	np->n_flag |= NACC;
   3266      1.1  dholland 	vfs_timestamp(&np->n_atim);
   3267      1.1  dholland 	mtx_unlock(&np->n_mtx);
   3268      1.1  dholland 	error = fifo_specops.vop_read(ap);
   3269      1.1  dholland 	return error;
   3270      1.1  dholland }
   3271      1.1  dholland 
   3272      1.1  dholland /*
   3273      1.1  dholland  * Write wrapper for fifos.
   3274      1.1  dholland  */
   3275      1.1  dholland static int
   3276      1.1  dholland nfsfifo_write(struct vop_write_args *ap)
   3277      1.1  dholland {
   3278      1.1  dholland 	struct nfsnode *np = VTONFS(ap->a_vp);
   3279      1.1  dholland 
   3280      1.1  dholland 	/*
   3281      1.1  dholland 	 * Set update flag.
   3282      1.1  dholland 	 */
   3283      1.1  dholland 	mtx_lock(&np->n_mtx);
   3284      1.1  dholland 	np->n_flag |= NUPD;
   3285      1.1  dholland 	vfs_timestamp(&np->n_mtim);
   3286      1.1  dholland 	mtx_unlock(&np->n_mtx);
   3287      1.1  dholland 	return(fifo_specops.vop_write(ap));
   3288      1.1  dholland }
   3289      1.1  dholland 
   3290      1.1  dholland /*
   3291      1.1  dholland  * Close wrapper for fifos.
   3292      1.1  dholland  *
   3293      1.1  dholland  * Update the times on the nfsnode then do fifo close.
   3294      1.1  dholland  */
   3295      1.1  dholland static int
   3296      1.1  dholland nfsfifo_close(struct vop_close_args *ap)
   3297      1.1  dholland {
   3298      1.1  dholland 	struct vnode *vp = ap->a_vp;
   3299      1.1  dholland 	struct nfsnode *np = VTONFS(vp);
   3300      1.1  dholland 	struct vattr vattr;
   3301      1.1  dholland 	struct timespec ts;
   3302      1.1  dholland 
   3303      1.1  dholland 	mtx_lock(&np->n_mtx);
   3304      1.1  dholland 	if (np->n_flag & (NACC | NUPD)) {
   3305      1.1  dholland 		vfs_timestamp(&ts);
   3306      1.1  dholland 		if (np->n_flag & NACC)
   3307      1.1  dholland 			np->n_atim = ts;
   3308      1.1  dholland 		if (np->n_flag & NUPD)
   3309      1.1  dholland 			np->n_mtim = ts;
   3310      1.1  dholland 		np->n_flag |= NCHG;
   3311      1.1  dholland 		if (vrefcnt(vp) == 1 &&
   3312      1.1  dholland 		    (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
   3313      1.1  dholland 			VATTR_NULL(&vattr);
   3314      1.1  dholland 			if (np->n_flag & NACC)
   3315      1.1  dholland 				vattr.va_atime = np->n_atim;
   3316      1.1  dholland 			if (np->n_flag & NUPD)
   3317      1.1  dholland 				vattr.va_mtime = np->n_mtim;
   3318      1.1  dholland 			mtx_unlock(&np->n_mtx);
   3319      1.1  dholland 			(void)VOP_SETATTR(vp, &vattr, ap->a_cred);
   3320      1.1  dholland 			goto out;
   3321      1.1  dholland 		}
   3322      1.1  dholland 	}
   3323      1.1  dholland 	mtx_unlock(&np->n_mtx);
   3324      1.1  dholland out:
   3325      1.1  dholland 	return (fifo_specops.vop_close(ap));
   3326      1.1  dholland }
   3327      1.1  dholland 
   3328      1.1  dholland /*
   3329      1.1  dholland  * Just call ncl_writebp() with the force argument set to 1.
   3330      1.1  dholland  *
   3331      1.1  dholland  * NOTE: B_DONE may or may not be set in a_bp on call.
   3332      1.1  dholland  */
   3333      1.1  dholland static int
   3334      1.1  dholland nfs_bwrite(struct buf *bp)
   3335      1.1  dholland {
   3336      1.1  dholland 
   3337      1.1  dholland 	return (ncl_writebp(bp, 1, curthread));
   3338      1.1  dholland }
   3339      1.1  dholland 
   3340      1.1  dholland struct buf_ops buf_ops_newnfs = {
   3341      1.1  dholland 	.bop_name	=	"buf_ops_nfs",
   3342      1.1  dholland 	.bop_write	=	nfs_bwrite,
   3343      1.1  dholland 	.bop_strategy	=	bufstrategy,
   3344      1.1  dholland 	.bop_sync	=	bufsync,
   3345      1.1  dholland 	.bop_bdflush	=	bufbdflush,
   3346      1.1  dholland };
   3347      1.1  dholland 
   3348      1.1  dholland static int
   3349      1.1  dholland nfs_getacl(struct vop_getacl_args *ap)
   3350      1.1  dholland {
   3351      1.1  dholland 	int error;
   3352      1.1  dholland 
   3353      1.1  dholland 	if (ap->a_type != ACL_TYPE_NFS4)
   3354      1.1  dholland 		return (EOPNOTSUPP);
   3355      1.1  dholland 	error = nfsrpc_getacl(ap->a_vp, ap->a_cred, ap->a_td, ap->a_aclp,
   3356      1.1  dholland 	    NULL);
   3357      1.1  dholland 	if (error > NFSERR_STALE) {
   3358      1.1  dholland 		(void) nfscl_maperr(ap->a_td, error, (uid_t)0, (gid_t)0);
   3359      1.1  dholland 		error = EPERM;
   3360      1.1  dholland 	}
   3361      1.1  dholland 	return (error);
   3362      1.1  dholland }
   3363      1.1  dholland 
   3364      1.1  dholland static int
   3365      1.1  dholland nfs_setacl(struct vop_setacl_args *ap)
   3366      1.1  dholland {
   3367      1.1  dholland 	int error;
   3368      1.1  dholland 
   3369      1.1  dholland 	if (ap->a_type != ACL_TYPE_NFS4)
   3370      1.1  dholland 		return (EOPNOTSUPP);
   3371      1.1  dholland 	error = nfsrpc_setacl(ap->a_vp, ap->a_cred, ap->a_td, ap->a_aclp,
   3372      1.1  dholland 	    NULL);
   3373      1.1  dholland 	if (error > NFSERR_STALE) {
   3374      1.1  dholland 		(void) nfscl_maperr(ap->a_td, error, (uid_t)0, (gid_t)0);
   3375      1.1  dholland 		error = EPERM;
   3376      1.1  dholland 	}
   3377      1.1  dholland 	return (error);
   3378      1.1  dholland }
   3379      1.1  dholland 
   3380      1.1  dholland /*
   3381      1.1  dholland  * Return POSIX pathconf information applicable to nfs filesystems.
   3382      1.1  dholland  */
   3383      1.1  dholland static int
   3384      1.1  dholland nfs_pathconf(struct vop_pathconf_args *ap)
   3385      1.1  dholland {
   3386      1.1  dholland 	struct nfsv3_pathconf pc;
   3387      1.1  dholland 	struct nfsvattr nfsva;
   3388      1.1  dholland 	struct vnode *vp = ap->a_vp;
   3389      1.1  dholland 	struct thread *td = curthread;
   3390      1.1  dholland 	int attrflag, error;
   3391      1.1  dholland 
   3392  1.1.1.2  pgoyette 	if ((NFS_ISV34(vp) && (ap->a_name == _PC_LINK_MAX ||
   3393      1.1  dholland 	    ap->a_name == _PC_NAME_MAX || ap->a_name == _PC_CHOWN_RESTRICTED ||
   3394  1.1.1.2  pgoyette 	    ap->a_name == _PC_NO_TRUNC)) ||
   3395  1.1.1.2  pgoyette 	    (NFS_ISV4(vp) && ap->a_name == _PC_ACL_NFS4)) {
   3396      1.1  dholland 		/*
   3397      1.1  dholland 		 * Since only the above 4 a_names are returned by the NFSv3
   3398      1.1  dholland 		 * Pathconf RPC, there is no point in doing it for others.
   3399  1.1.1.2  pgoyette 		 * For NFSv4, the Pathconf RPC (actually a Getattr Op.) can
   3400  1.1.1.2  pgoyette 		 * be used for _PC_NFS4_ACL as well.
   3401      1.1  dholland 		 */
   3402      1.1  dholland 		error = nfsrpc_pathconf(vp, &pc, td->td_ucred, td, &nfsva,
   3403      1.1  dholland 		    &attrflag, NULL);
   3404      1.1  dholland 		if (attrflag != 0)
   3405      1.1  dholland 			(void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0,
   3406      1.1  dholland 			    1);
   3407      1.1  dholland 		if (error != 0)
   3408      1.1  dholland 			return (error);
   3409      1.1  dholland 	} else {
   3410      1.1  dholland 		/*
   3411      1.1  dholland 		 * For NFSv2 (or NFSv3 when not one of the above 4 a_names),
   3412      1.1  dholland 		 * just fake them.
   3413      1.1  dholland 		 */
   3414      1.1  dholland 		pc.pc_linkmax = LINK_MAX;
   3415      1.1  dholland 		pc.pc_namemax = NFS_MAXNAMLEN;
   3416      1.1  dholland 		pc.pc_notrunc = 1;
   3417      1.1  dholland 		pc.pc_chownrestricted = 1;
   3418      1.1  dholland 		pc.pc_caseinsensitive = 0;
   3419      1.1  dholland 		pc.pc_casepreserving = 1;
   3420      1.1  dholland 		error = 0;
   3421      1.1  dholland 	}
   3422      1.1  dholland 	switch (ap->a_name) {
   3423      1.1  dholland 	case _PC_LINK_MAX:
   3424      1.1  dholland 		*ap->a_retval = pc.pc_linkmax;
   3425      1.1  dholland 		break;
   3426      1.1  dholland 	case _PC_NAME_MAX:
   3427      1.1  dholland 		*ap->a_retval = pc.pc_namemax;
   3428      1.1  dholland 		break;
   3429      1.1  dholland 	case _PC_PATH_MAX:
   3430      1.1  dholland 		*ap->a_retval = PATH_MAX;
   3431      1.1  dholland 		break;
   3432      1.1  dholland 	case _PC_PIPE_BUF:
   3433      1.1  dholland 		*ap->a_retval = PIPE_BUF;
   3434      1.1  dholland 		break;
   3435      1.1  dholland 	case _PC_CHOWN_RESTRICTED:
   3436      1.1  dholland 		*ap->a_retval = pc.pc_chownrestricted;
   3437      1.1  dholland 		break;
   3438      1.1  dholland 	case _PC_NO_TRUNC:
   3439      1.1  dholland 		*ap->a_retval = pc.pc_notrunc;
   3440      1.1  dholland 		break;
   3441      1.1  dholland 	case _PC_ACL_EXTENDED:
   3442      1.1  dholland 		*ap->a_retval = 0;
   3443      1.1  dholland 		break;
   3444      1.1  dholland 	case _PC_ACL_NFS4:
   3445      1.1  dholland 		if (NFS_ISV4(vp) && nfsrv_useacl != 0 && attrflag != 0 &&
   3446      1.1  dholland 		    NFSISSET_ATTRBIT(&nfsva.na_suppattr, NFSATTRBIT_ACL))
   3447      1.1  dholland 			*ap->a_retval = 1;
   3448      1.1  dholland 		else
   3449      1.1  dholland 			*ap->a_retval = 0;
   3450      1.1  dholland 		break;
   3451      1.1  dholland 	case _PC_ACL_PATH_MAX:
   3452      1.1  dholland 		if (NFS_ISV4(vp))
   3453      1.1  dholland 			*ap->a_retval = ACL_MAX_ENTRIES;
   3454      1.1  dholland 		else
   3455      1.1  dholland 			*ap->a_retval = 3;
   3456      1.1  dholland 		break;
   3457      1.1  dholland 	case _PC_MAC_PRESENT:
   3458      1.1  dholland 		*ap->a_retval = 0;
   3459      1.1  dholland 		break;
   3460      1.1  dholland 	case _PC_ASYNC_IO:
   3461      1.1  dholland 		/* _PC_ASYNC_IO should have been handled by upper layers. */
   3462      1.1  dholland 		KASSERT(0, ("_PC_ASYNC_IO should not get here"));
   3463      1.1  dholland 		error = EINVAL;
   3464      1.1  dholland 		break;
   3465      1.1  dholland 	case _PC_PRIO_IO:
   3466      1.1  dholland 		*ap->a_retval = 0;
   3467      1.1  dholland 		break;
   3468      1.1  dholland 	case _PC_SYNC_IO:
   3469      1.1  dholland 		*ap->a_retval = 0;
   3470      1.1  dholland 		break;
   3471      1.1  dholland 	case _PC_ALLOC_SIZE_MIN:
   3472      1.1  dholland 		*ap->a_retval = vp->v_mount->mnt_stat.f_bsize;
   3473      1.1  dholland 		break;
   3474      1.1  dholland 	case _PC_FILESIZEBITS:
   3475      1.1  dholland 		if (NFS_ISV34(vp))
   3476      1.1  dholland 			*ap->a_retval = 64;
   3477      1.1  dholland 		else
   3478      1.1  dholland 			*ap->a_retval = 32;
   3479      1.1  dholland 		break;
   3480      1.1  dholland 	case _PC_REC_INCR_XFER_SIZE:
   3481      1.1  dholland 		*ap->a_retval = vp->v_mount->mnt_stat.f_iosize;
   3482      1.1  dholland 		break;
   3483      1.1  dholland 	case _PC_REC_MAX_XFER_SIZE:
   3484      1.1  dholland 		*ap->a_retval = -1; /* means ``unlimited'' */
   3485      1.1  dholland 		break;
   3486      1.1  dholland 	case _PC_REC_MIN_XFER_SIZE:
   3487      1.1  dholland 		*ap->a_retval = vp->v_mount->mnt_stat.f_iosize;
   3488      1.1  dholland 		break;
   3489      1.1  dholland 	case _PC_REC_XFER_ALIGN:
   3490      1.1  dholland 		*ap->a_retval = PAGE_SIZE;
   3491      1.1  dholland 		break;
   3492      1.1  dholland 	case _PC_SYMLINK_MAX:
   3493      1.1  dholland 		*ap->a_retval = NFS_MAXPATHLEN;
   3494      1.1  dholland 		break;
   3495      1.1  dholland 
   3496      1.1  dholland 	default:
   3497      1.1  dholland 		error = EINVAL;
   3498      1.1  dholland 		break;
   3499      1.1  dholland 	}
   3500      1.1  dholland 	return (error);
   3501      1.1  dholland }
   3502      1.1  dholland 
   3503