1*d5cb0deaShannken /* $NetBSD: nfs_node.c,v 1.126 2020/05/01 08:43:00 hannken Exp $ */
2fccfa11aScgd
361f28255Scgd /*
4cde1d475Smycroft * Copyright (c) 1989, 1993
5cde1d475Smycroft * The Regents of the University of California. All rights reserved.
661f28255Scgd *
761f28255Scgd * This code is derived from software contributed to Berkeley by
861f28255Scgd * Rick Macklem at The University of Guelph.
961f28255Scgd *
1061f28255Scgd * Redistribution and use in source and binary forms, with or without
1161f28255Scgd * modification, are permitted provided that the following conditions
1261f28255Scgd * are met:
1361f28255Scgd * 1. Redistributions of source code must retain the above copyright
1461f28255Scgd * notice, this list of conditions and the following disclaimer.
1561f28255Scgd * 2. Redistributions in binary form must reproduce the above copyright
1661f28255Scgd * notice, this list of conditions and the following disclaimer in the
1761f28255Scgd * documentation and/or other materials provided with the distribution.
18aad01611Sagc * 3. Neither the name of the University nor the names of its contributors
1961f28255Scgd * may be used to endorse or promote products derived from this software
2061f28255Scgd * without specific prior written permission.
2161f28255Scgd *
2261f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2361f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2461f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2561f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2661f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2761f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2861f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2961f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3061f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3161f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3261f28255Scgd * SUCH DAMAGE.
3361f28255Scgd *
345ac7df1cSfvdl * @(#)nfs_node.c 8.6 (Berkeley) 5/22/95
3561f28255Scgd */
3661f28255Scgd
370ffad693Slukem #include <sys/cdefs.h>
38*d5cb0deaShannken __KERNEL_RCSID(0, "$NetBSD: nfs_node.c,v 1.126 2020/05/01 08:43:00 hannken Exp $");
390ffad693Slukem
4092ce8c6aSad #ifdef _KERNEL_OPT
4133daa8deSbjh21 #include "opt_nfs.h"
4292ce8c6aSad #endif
435ac7df1cSfvdl
4495b048b5Smycroft #include <sys/param.h>
4595b048b5Smycroft #include <sys/systm.h>
4695b048b5Smycroft #include <sys/proc.h>
4795b048b5Smycroft #include <sys/mount.h>
4895b048b5Smycroft #include <sys/namei.h>
4995b048b5Smycroft #include <sys/vnode.h>
5095b048b5Smycroft #include <sys/kernel.h>
519a3664ecSthorpej #include <sys/pool.h>
52acdbbd52Sfvdl #include <sys/lock.h>
5315468351Slukem #include <sys/hash.h>
54fc9422c9Selad #include <sys/kauth.h>
5561f28255Scgd
56cde1d475Smycroft #include <nfs/rpcv2.h>
575ac7df1cSfvdl #include <nfs/nfsproto.h>
5895b048b5Smycroft #include <nfs/nfs.h>
5995b048b5Smycroft #include <nfs/nfsnode.h>
6095b048b5Smycroft #include <nfs/nfsmount.h>
61e4c93ec8Schristos #include <nfs/nfs_var.h>
6261f28255Scgd
63c7637a67Spooka struct pool nfs_node_pool;
64c7637a67Spooka struct pool nfs_vattr_pool;
6527bc6a23Sad static struct workqueue *nfs_sillyworkq;
66b1934809Sthorpej
6727bc6a23Sad static void nfs_gop_size(struct vnode *, off_t, off_t *, int);
6827bc6a23Sad static int nfs_gop_alloc(struct vnode *, off_t, off_t, int, kauth_cred_t);
6927bc6a23Sad static int nfs_gop_write(struct vnode *, struct vm_page **, int, int);
7027bc6a23Sad static void nfs_sillyworker(struct work *, void *);
7164c6d1d2Schs
7244d128faSyamt static const struct genfs_ops nfs_genfsops = {
7344d128faSyamt .gop_size = nfs_gop_size,
7444d128faSyamt .gop_alloc = nfs_gop_alloc,
7544d128faSyamt .gop_write = nfs_gop_write,
76e406c140Schs .gop_putrange = genfs_gop_putrange,
7764c6d1d2Schs };
7864c6d1d2Schs
7961f28255Scgd /*
80a42078efSmatt * Reinitialize inode hash table.
8161f28255Scgd */
82e4c93ec8Schristos void
nfs_node_init(void)83b8817e4aScegger nfs_node_init(void)
8461f28255Scgd {
8527bc6a23Sad
86c7637a67Spooka pool_init(&nfs_node_pool, sizeof(struct nfsnode), 0, 0, 0, "nfsnodepl",
87c7637a67Spooka &pool_allocator_nointr, IPL_NONE);
88c7637a67Spooka pool_init(&nfs_vattr_pool, sizeof(struct vattr), 0, 0, 0, "nfsvapl",
89c7637a67Spooka &pool_allocator_nointr, IPL_NONE);
9027bc6a23Sad if (workqueue_create(&nfs_sillyworkq, "nfssilly", nfs_sillyworker,
9127bc6a23Sad NULL, PRI_NONE, IPL_NONE, 0) != 0) {
9227bc6a23Sad panic("nfs_node_init");
9327bc6a23Sad }
9461f28255Scgd }
9561f28255Scgd
9661f28255Scgd /*
97a42078efSmatt * Free resources previously allocated in nfs_node_reinit().
9889015c46Sjdolecek */
9989015c46Sjdolecek void
nfs_node_done(void)100b8817e4aScegger nfs_node_done(void)
10189015c46Sjdolecek {
10227bc6a23Sad
10389015c46Sjdolecek pool_destroy(&nfs_node_pool);
10489015c46Sjdolecek pool_destroy(&nfs_vattr_pool);
10527bc6a23Sad workqueue_destroy(nfs_sillyworkq);
10689015c46Sjdolecek }
10789015c46Sjdolecek
108ff361c30Shannken /*
109ff361c30Shannken * Initialize this vnode / nfs node pair.
110ff361c30Shannken * Caller assures no other thread will try to load this node.
111ff361c30Shannken */
112ff361c30Shannken int
nfs_loadvnode(struct mount * mp,struct vnode * vp,const void * key,size_t key_len,const void ** new_key)113ff361c30Shannken nfs_loadvnode(struct mount *mp, struct vnode *vp,
114ff361c30Shannken const void *key, size_t key_len, const void **new_key)
115a42078efSmatt {
116ff361c30Shannken int fhsize = key_len;
117ff361c30Shannken const nfsfh_t *fhp = key;
118ff361c30Shannken struct nfsnode *np;
119a42078efSmatt
120ff361c30Shannken /* Aloocate and initialize the nfsnode. */
121ff361c30Shannken np = pool_get(&nfs_node_pool, PR_WAITOK);
122ff361c30Shannken memset(np, 0, sizeof *np);
123ff361c30Shannken if (fhsize > NFS_SMALLFH) {
124ff361c30Shannken np->n_fhp = kmem_alloc(fhsize, KM_SLEEP);
125ff361c30Shannken } else
126ff361c30Shannken np->n_fhp = &np->n_fh;
127ff361c30Shannken vp->v_tag = VT_NFS;
128ff361c30Shannken vp->v_type = VNON;
129ff361c30Shannken vp->v_op = nfsv2_vnodeop_p;
130ff361c30Shannken vp->v_data = np;
131ff361c30Shannken memcpy(np->n_fhp, fhp, fhsize);
132ff361c30Shannken np->n_fhsize = fhsize;
133ff361c30Shannken np->n_accstamp = -1;
134ff361c30Shannken np->n_vattr = pool_get(&nfs_vattr_pool, PR_WAITOK);
135ff361c30Shannken np->n_vnode = vp;
136a42078efSmatt
137ff361c30Shannken /* Initialize genfs node. */
138ff361c30Shannken genfs_node_init(vp, &nfs_genfsops);
139ff361c30Shannken /*
140c56890eeSmsaitoh * Initialize read/write creds to useful values. VOP_OPEN will
141ff361c30Shannken * overwrite these.
142ff361c30Shannken */
143ff361c30Shannken np->n_rcred = curlwp->l_cred;
144ff361c30Shannken kauth_cred_hold(np->n_rcred);
145ff361c30Shannken np->n_wcred = curlwp->l_cred;
146ff361c30Shannken kauth_cred_hold(np->n_wcred);
147ff361c30Shannken NFS_INVALIDATE_ATTRCACHE(np);
148ff361c30Shannken uvm_vnp_setsize(vp, 0);
149ff361c30Shannken *new_key = np->n_fhp;
150ff361c30Shannken return 0;
151a42078efSmatt }
152a42078efSmatt
15389015c46Sjdolecek /*
15461f28255Scgd * Look up a vnode/nfsnode by file handle.
15561f28255Scgd * Callers must check for mount points!!
15661f28255Scgd * In all cases, a pointer to a
15761f28255Scgd * nfsnode structure is returned.
15861f28255Scgd */
159e4c93ec8Schristos int
nfs_nget1(struct mount * mntp,nfsfh_t * fhp,int fhsize,struct nfsnode ** npp,int lkflags)160879d5dfbSrmind nfs_nget1(struct mount *mntp, nfsfh_t *fhp, int fhsize, struct nfsnode **npp,
161879d5dfbSrmind int lkflags)
16261f28255Scgd {
16361f28255Scgd int error;
164ff361c30Shannken struct vnode *vp;
16561f28255Scgd
166ff361c30Shannken error = vcache_get(mntp, fhp, fhsize, &vp);
167d1a0f61aSyamt if (error)
168ff361c30Shannken return error;
169ff361c30Shannken error = vn_lock(vp, LK_EXCLUSIVE | lkflags);
170e4c93ec8Schristos if (error) {
171ff361c30Shannken vrele(vp);
172ff361c30Shannken return error;
17361f28255Scgd }
174ff361c30Shannken *npp = VTONFS(vp);
175ff361c30Shannken return 0;
17661f28255Scgd }
17761f28255Scgd
178e4c93ec8Schristos int
nfs_inactive(void * v)179454af1c0Sdsl nfs_inactive(void *v)
180e4c93ec8Schristos {
18187fb3229Sriastradh struct vop_inactive_v2_args /* {
182cde1d475Smycroft struct vnode *a_vp;
1834a780c9aSad bool *a_recycle;
184e4c93ec8Schristos } */ *ap = v;
1858529438fSaugustss struct nfsnode *np;
1868529438fSaugustss struct sillyrename *sp;
187c18aae5bSfvdl struct vnode *vp = ap->a_vp;
18861f28255Scgd
189*d5cb0deaShannken /* If we have a delayed truncation, do it now. */
190*d5cb0deaShannken nfs_delayedtruncate(vp);
191*d5cb0deaShannken
192c18aae5bSfvdl np = VTONFS(vp);
193c18aae5bSfvdl if (vp->v_type != VDIR) {
19461f28255Scgd sp = np->n_sillyrename;
19561f28255Scgd np->n_sillyrename = (struct sillyrename *)0;
196744438b4Sfvdl } else
197bf714cf8Sfvdl sp = NULL;
198bf714cf8Sfvdl if (sp != NULL)
1994a780c9aSad nfs_vinvalbuf(vp, 0, sp->s_cred, curlwp, 1);
2004a780c9aSad *ap->a_recycle = (np->n_flag & NREMOVED) != 0;
201f1ce35c9Syamt np->n_flag &=
202f1ce35c9Syamt (NMODIFIED | NFLUSHINPROG | NFLUSHWANT | NEOFVALID | NTRUNCDELAYED);
203392d6f9aSyamt
204392d6f9aSyamt if (vp->v_type == VDIR && np->n_dircache)
205cf56f2b8Syamt nfs_invaldircache(vp,
206cf56f2b8Syamt NFS_INVALDIRCACHE_FORCE | NFS_INVALDIRCACHE_KEEPEOF);
207392d6f9aSyamt
208bf714cf8Sfvdl if (sp != NULL) {
20927bc6a23Sad workqueue_enqueue(nfs_sillyworkq, &sp->s_work, NULL);
210cde1d475Smycroft }
2112be3d828Sfvdl
21261f28255Scgd return (0);
21361f28255Scgd }
21461f28255Scgd
21561f28255Scgd /*
21661f28255Scgd * Reclaim an nfsnode so that it can be used for other purposes.
21761f28255Scgd */
218e4c93ec8Schristos int
nfs_reclaim(void * v)219454af1c0Sdsl nfs_reclaim(void *v)
220e4c93ec8Schristos {
2217f7aad09Sriastradh struct vop_reclaim_v2_args /* {
222cde1d475Smycroft struct vnode *a_vp;
223e4c93ec8Schristos } */ *ap = v;
2248529438fSaugustss struct vnode *vp = ap->a_vp;
2258529438fSaugustss struct nfsnode *np = VTONFS(vp);
22661f28255Scgd
2277f7aad09Sriastradh VOP_UNLOCK(vp);
2287f7aad09Sriastradh
2295ac7df1cSfvdl /*
2305ac7df1cSfvdl * Free up any directory cookie structures and
2315ac7df1cSfvdl * large file handle structures that might be associated with
2325ac7df1cSfvdl * this nfs node.
2335ac7df1cSfvdl */
23497eb62abStron if (vp->v_type == VDIR && np->n_dircache != NULL) {
23597eb62abStron nfs_invaldircache(vp, NFS_INVALDIRCACHE_FORCE);
236e071d39cSad hashdone(np->n_dircache, HASH_LIST, nfsdirhashmask);
23797eb62abStron }
238b24da550Syamt KASSERT(np->n_dirgens == NULL);
2392be3d828Sfvdl
2402be3d828Sfvdl if (np->n_fhsize > NFS_SMALLFH)
241677860cbSyamt kmem_free(np->n_fhp, np->n_fhsize);
2425ac7df1cSfvdl
2439a3664ecSthorpej pool_put(&nfs_vattr_pool, np->n_vattr);
2442be3d828Sfvdl if (np->n_rcred)
245fc9422c9Selad kauth_cred_free(np->n_rcred);
2462be3d828Sfvdl
2472be3d828Sfvdl if (np->n_wcred)
248fc9422c9Selad kauth_cred_free(np->n_wcred);
2492be3d828Sfvdl
2506d6b5c51Syamt if (vp->v_type == VREG) {
2516d6b5c51Syamt mutex_destroy(&np->n_commitlock);
2526d6b5c51Syamt }
253adbb9ec2Sad genfs_node_destroy(vp);
2546d6b5c51Syamt pool_put(&nfs_node_pool, np);
255aeda8d3bSchs vp->v_data = NULL;
25661f28255Scgd return (0);
25761f28255Scgd }
25864c6d1d2Schs
25964c6d1d2Schs void
nfs_gop_size(struct vnode * vp,off_t size,off_t * eobp,int flags)26089682b24Syamt nfs_gop_size(struct vnode *vp, off_t size, off_t *eobp, int flags)
26164c6d1d2Schs {
262c5fcdd17Syamt
26364c6d1d2Schs *eobp = MAX(size, vp->v_size);
26464c6d1d2Schs }
26564c6d1d2Schs
26664c6d1d2Schs int
nfs_gop_alloc(struct vnode * vp,off_t off,off_t len,int flags,kauth_cred_t cred)26789682b24Syamt nfs_gop_alloc(struct vnode *vp, off_t off, off_t len, int flags,
26889682b24Syamt kauth_cred_t cred)
26964c6d1d2Schs {
27089682b24Syamt
27164c6d1d2Schs return 0;
27264c6d1d2Schs }
273b4b7853bSchs
274b4b7853bSchs int
nfs_gop_write(struct vnode * vp,struct vm_page ** pgs,int npages,int flags)275b4b7853bSchs nfs_gop_write(struct vnode *vp, struct vm_page **pgs, int npages, int flags)
276b4b7853bSchs {
277b4b7853bSchs int i;
278b4b7853bSchs
279bfc37e92Sad rw_enter(vp->v_uobj.vmobjlock, RW_WRITER);
280b4b7853bSchs for (i = 0; i < npages; i++) {
281b4b7853bSchs pmap_page_protect(pgs[i], VM_PROT_READ);
282b4b7853bSchs }
283bfc37e92Sad rw_exit(vp->v_uobj.vmobjlock);
284e225b7bdSrmind
285b4b7853bSchs return genfs_gop_write(vp, pgs, npages, flags);
286b4b7853bSchs }
28727bc6a23Sad
28827bc6a23Sad /*
28927bc6a23Sad * Remove a silly file that was rename'd earlier
29027bc6a23Sad */
29127bc6a23Sad static void
nfs_sillyworker(struct work * work,void * arg)29227bc6a23Sad nfs_sillyworker(struct work *work, void *arg)
29327bc6a23Sad {
29427bc6a23Sad struct sillyrename *sp;
29527bc6a23Sad int error;
29627bc6a23Sad
29727bc6a23Sad sp = (struct sillyrename *)work;
29827bc6a23Sad error = vn_lock(sp->s_dvp, LK_EXCLUSIVE);
29927bc6a23Sad if (error || sp->s_dvp->v_data == NULL) {
30027bc6a23Sad /* XXX should recover */
30127bc6a23Sad printf("%s: vp=%p error=%d\n", __func__, sp->s_dvp, error);
30227bc6a23Sad if (error == 0) {
30327bc6a23Sad vput(sp->s_dvp);
30427bc6a23Sad } else {
30527bc6a23Sad vrele(sp->s_dvp);
30627bc6a23Sad }
30727bc6a23Sad } else {
30827bc6a23Sad nfs_removeit(sp);
30927bc6a23Sad vput(sp->s_dvp);
31027bc6a23Sad }
31127bc6a23Sad kauth_cred_free(sp->s_cred);
31227bc6a23Sad kmem_free(sp, sizeof(*sp));
31327bc6a23Sad }
314