Home | History | Annotate | Line # | Download | only in nfs
nfs_node.c revision 1.121
      1 /*	$NetBSD: nfs_node.c,v 1.121 2017/05/26 14:21:02 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Rick Macklem at The University of Guelph.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  *
     34  *	@(#)nfs_node.c	8.6 (Berkeley) 5/22/95
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: nfs_node.c,v 1.121 2017/05/26 14:21:02 riastradh Exp $");
     39 
     40 #ifdef _KERNEL_OPT
     41 #include "opt_nfs.h"
     42 #endif
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/proc.h>
     47 #include <sys/mount.h>
     48 #include <sys/namei.h>
     49 #include <sys/vnode.h>
     50 #include <sys/kernel.h>
     51 #include <sys/pool.h>
     52 #include <sys/lock.h>
     53 #include <sys/hash.h>
     54 #include <sys/kauth.h>
     55 
     56 #include <nfs/rpcv2.h>
     57 #include <nfs/nfsproto.h>
     58 #include <nfs/nfs.h>
     59 #include <nfs/nfsnode.h>
     60 #include <nfs/nfsmount.h>
     61 #include <nfs/nfs_var.h>
     62 
     63 struct pool nfs_node_pool;
     64 struct pool nfs_vattr_pool;
     65 static struct workqueue *nfs_sillyworkq;
     66 
     67 extern int prtactive;
     68 
     69 static void nfs_gop_size(struct vnode *, off_t, off_t *, int);
     70 static int nfs_gop_alloc(struct vnode *, off_t, off_t, int, kauth_cred_t);
     71 static int nfs_gop_write(struct vnode *, struct vm_page **, int, int);
     72 static void nfs_sillyworker(struct work *, void *);
     73 
     74 static const struct genfs_ops nfs_genfsops = {
     75 	.gop_size = nfs_gop_size,
     76 	.gop_alloc = nfs_gop_alloc,
     77 	.gop_write = nfs_gop_write,
     78 };
     79 
     80 /*
     81  * Reinitialize inode hash table.
     82  */
     83 void
     84 nfs_node_init(void)
     85 {
     86 
     87 	pool_init(&nfs_node_pool, sizeof(struct nfsnode), 0, 0, 0, "nfsnodepl",
     88 	    &pool_allocator_nointr, IPL_NONE);
     89 	pool_init(&nfs_vattr_pool, sizeof(struct vattr), 0, 0, 0, "nfsvapl",
     90 	    &pool_allocator_nointr, IPL_NONE);
     91 	if (workqueue_create(&nfs_sillyworkq, "nfssilly", nfs_sillyworker,
     92 	    NULL, PRI_NONE, IPL_NONE, 0) != 0) {
     93 	    	panic("nfs_node_init");
     94 	}
     95 }
     96 
     97 /*
     98  * Free resources previously allocated in nfs_node_reinit().
     99  */
    100 void
    101 nfs_node_done(void)
    102 {
    103 
    104 	pool_destroy(&nfs_node_pool);
    105 	pool_destroy(&nfs_vattr_pool);
    106 	workqueue_destroy(nfs_sillyworkq);
    107 }
    108 
    109 /*
    110  * Initialize this vnode / nfs node pair.
    111  * Caller assures no other thread will try to load this node.
    112  */
    113 int
    114 nfs_loadvnode(struct mount *mp, struct vnode *vp,
    115     const void *key, size_t key_len, const void **new_key)
    116 {
    117 	int fhsize = key_len;
    118 	const nfsfh_t *fhp = key;
    119 	struct nfsnode *np;
    120 
    121 	/* Aloocate and initialize the nfsnode. */
    122 	np = pool_get(&nfs_node_pool, PR_WAITOK);
    123 	memset(np, 0, sizeof *np);
    124 	if (fhsize > NFS_SMALLFH) {
    125 		np->n_fhp = kmem_alloc(fhsize, KM_SLEEP);
    126 	} else
    127 		np->n_fhp = &np->n_fh;
    128 	vp->v_tag = VT_NFS;
    129 	vp->v_type = VNON;
    130 	vp->v_op = nfsv2_vnodeop_p;
    131 	vp->v_data = np;
    132 	memcpy(np->n_fhp, fhp, fhsize);
    133 	np->n_fhsize = fhsize;
    134 	np->n_accstamp = -1;
    135 	np->n_vattr = pool_get(&nfs_vattr_pool, PR_WAITOK);
    136 	np->n_vnode = vp;
    137 
    138 	/* Initialize genfs node. */
    139 	genfs_node_init(vp, &nfs_genfsops);
    140 	/*
    141 	 * Initalize read/write creds to useful values. VOP_OPEN will
    142 	 * overwrite these.
    143 	 */
    144 	np->n_rcred = curlwp->l_cred;
    145 	kauth_cred_hold(np->n_rcred);
    146 	np->n_wcred = curlwp->l_cred;
    147 	kauth_cred_hold(np->n_wcred);
    148 	NFS_INVALIDATE_ATTRCACHE(np);
    149 	uvm_vnp_setsize(vp, 0);
    150 	*new_key = np->n_fhp;
    151 	return 0;
    152 }
    153 
    154 /*
    155  * Look up a vnode/nfsnode by file handle.
    156  * Callers must check for mount points!!
    157  * In all cases, a pointer to a
    158  * nfsnode structure is returned.
    159  */
    160 int
    161 nfs_nget1(struct mount *mntp, nfsfh_t *fhp, int fhsize, struct nfsnode **npp,
    162     int lkflags)
    163 {
    164 	int error;
    165 	struct vnode *vp;
    166 
    167 	error = vcache_get(mntp, fhp, fhsize, &vp);
    168 	if (error)
    169 		return error;
    170 	error = vn_lock(vp, LK_EXCLUSIVE | lkflags);
    171 	if (error) {
    172 		vrele(vp);
    173 		return error;
    174 	}
    175 	*npp = VTONFS(vp);
    176 	return 0;
    177 }
    178 
    179 int
    180 nfs_inactive(void *v)
    181 {
    182 	struct vop_inactive_v2_args /* {
    183 		struct vnode *a_vp;
    184 		bool *a_recycle;
    185 	} */ *ap = v;
    186 	struct nfsnode *np;
    187 	struct sillyrename *sp;
    188 	struct vnode *vp = ap->a_vp;
    189 
    190 	np = VTONFS(vp);
    191 	if (vp->v_type != VDIR) {
    192 		sp = np->n_sillyrename;
    193 		np->n_sillyrename = (struct sillyrename *)0;
    194 	} else
    195 		sp = NULL;
    196 	if (sp != NULL)
    197 		nfs_vinvalbuf(vp, 0, sp->s_cred, curlwp, 1);
    198 	*ap->a_recycle = (np->n_flag & NREMOVED) != 0;
    199 	np->n_flag &=
    200 	    (NMODIFIED | NFLUSHINPROG | NFLUSHWANT | NEOFVALID | NTRUNCDELAYED);
    201 
    202 	if (vp->v_type == VDIR && np->n_dircache)
    203 		nfs_invaldircache(vp,
    204 		    NFS_INVALDIRCACHE_FORCE | NFS_INVALDIRCACHE_KEEPEOF);
    205 
    206 	if (sp != NULL) {
    207 		workqueue_enqueue(nfs_sillyworkq, &sp->s_work, NULL);
    208 	}
    209 
    210 	return (0);
    211 }
    212 
    213 /*
    214  * Reclaim an nfsnode so that it can be used for other purposes.
    215  */
    216 int
    217 nfs_reclaim(void *v)
    218 {
    219 	struct vop_reclaim_v2_args /* {
    220 		struct vnode *a_vp;
    221 	} */ *ap = v;
    222 	struct vnode *vp = ap->a_vp;
    223 	struct nfsnode *np = VTONFS(vp);
    224 
    225 	VOP_UNLOCK(vp);
    226 
    227 	if (prtactive && vp->v_usecount > 1)
    228 		vprint("nfs_reclaim: pushing active", vp);
    229 
    230 	/*
    231 	 * Free up any directory cookie structures and
    232 	 * large file handle structures that might be associated with
    233 	 * this nfs node.
    234 	 */
    235 	if (vp->v_type == VDIR && np->n_dircache != NULL) {
    236 		nfs_invaldircache(vp, NFS_INVALDIRCACHE_FORCE);
    237 		hashdone(np->n_dircache, HASH_LIST, nfsdirhashmask);
    238 	}
    239 	KASSERT(np->n_dirgens == NULL);
    240 
    241 	if (np->n_fhsize > NFS_SMALLFH)
    242 		kmem_free(np->n_fhp, np->n_fhsize);
    243 
    244 	pool_put(&nfs_vattr_pool, np->n_vattr);
    245 	if (np->n_rcred)
    246 		kauth_cred_free(np->n_rcred);
    247 
    248 	if (np->n_wcred)
    249 		kauth_cred_free(np->n_wcred);
    250 
    251 	if (vp->v_type == VREG) {
    252 		mutex_destroy(&np->n_commitlock);
    253 	}
    254 	genfs_node_destroy(vp);
    255 	pool_put(&nfs_node_pool, np);
    256 	vp->v_data = NULL;
    257 	return (0);
    258 }
    259 
    260 void
    261 nfs_gop_size(struct vnode *vp, off_t size, off_t *eobp, int flags)
    262 {
    263 
    264 	*eobp = MAX(size, vp->v_size);
    265 }
    266 
    267 int
    268 nfs_gop_alloc(struct vnode *vp, off_t off, off_t len, int flags,
    269     kauth_cred_t cred)
    270 {
    271 
    272 	return 0;
    273 }
    274 
    275 int
    276 nfs_gop_write(struct vnode *vp, struct vm_page **pgs, int npages, int flags)
    277 {
    278 	int i;
    279 
    280 	mutex_enter(vp->v_interlock);
    281 	for (i = 0; i < npages; i++) {
    282 		pmap_page_protect(pgs[i], VM_PROT_READ);
    283 	}
    284 	mutex_exit(vp->v_interlock);
    285 
    286 	return genfs_gop_write(vp, pgs, npages, flags);
    287 }
    288 
    289 /*
    290  * Remove a silly file that was rename'd earlier
    291  */
    292 static void
    293 nfs_sillyworker(struct work *work, void *arg)
    294 {
    295 	struct sillyrename *sp;
    296 	int error;
    297 
    298 	sp = (struct sillyrename *)work;
    299 	error = vn_lock(sp->s_dvp, LK_EXCLUSIVE);
    300 	if (error || sp->s_dvp->v_data == NULL) {
    301 		/* XXX should recover */
    302 		printf("%s: vp=%p error=%d\n", __func__, sp->s_dvp, error);
    303 		if (error == 0) {
    304 			vput(sp->s_dvp);
    305 		} else {
    306 			vrele(sp->s_dvp);
    307 		}
    308 	} else {
    309 		nfs_removeit(sp);
    310 		vput(sp->s_dvp);
    311 	}
    312 	kauth_cred_free(sp->s_cred);
    313 	kmem_free(sp, sizeof(*sp));
    314 }
    315