2005-01-07 01:45:51 +00:00
|
|
|
/*-
|
1994-05-24 10:09:53 +00:00
|
|
|
* Copyright (c) 1989, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Rick Macklem at The University of Guelph.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
1997-02-10 02:22:35 +00:00
|
|
|
* @(#)nfs_node.c 8.6 (Berkeley) 5/22/95
|
1994-05-24 10:09:53 +00:00
|
|
|
*/
|
|
|
|
|
2001-09-18 23:32:09 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1997-02-10 02:22:35 +00:00
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
2010-10-19 00:20:00 +00:00
|
|
|
#include <sys/fcntl.h>
|
2001-05-01 08:13:21 +00:00
|
|
|
#include <sys/fnv_hash.h>
|
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/malloc.h>
|
2009-05-22 12:35:12 +00:00
|
|
|
#include <sys/mbuf.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/namei.h>
|
2001-05-01 08:13:21 +00:00
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/socket.h>
|
2001-04-11 00:39:20 +00:00
|
|
|
#include <sys/sysctl.h>
|
2011-08-02 11:24:42 +00:00
|
|
|
#include <sys/taskqueue.h>
|
2001-05-01 08:13:21 +00:00
|
|
|
#include <sys/vnode.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
|
2002-03-20 10:07:52 +00:00
|
|
|
#include <vm/uma.h>
|
1997-12-27 02:56:39 +00:00
|
|
|
|
1995-06-27 11:07:30 +00:00
|
|
|
#include <nfs/nfsproto.h>
|
2010-10-19 00:20:00 +00:00
|
|
|
#include <nfs/nfs_lock.h>
|
2001-09-18 23:32:09 +00:00
|
|
|
#include <nfsclient/nfs.h>
|
|
|
|
#include <nfsclient/nfsnode.h>
|
|
|
|
#include <nfsclient/nfsmount.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
|
2002-03-20 10:07:52 +00:00
|
|
|
static uma_zone_t nfsnode_zone;
|
2002-10-20 21:40:55 +00:00
|
|
|
|
2011-08-02 11:24:42 +00:00
|
|
|
static void nfs_freesillyrename(void *arg, __unused int pending);
|
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
#define TRUE 1
|
|
|
|
#define FALSE 0
|
|
|
|
|
1994-05-25 09:21:21 +00:00
|
|
|
void
|
2001-09-18 23:32:09 +00:00
|
|
|
nfs_nhinit(void)
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2002-03-20 10:07:52 +00:00
|
|
|
nfsnode_zone = uma_zcreate("NFSNODE", sizeof(struct nfsnode), NULL,
|
|
|
|
NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
|
2004-04-11 13:30:20 +00:00
|
|
|
void
|
|
|
|
nfs_nhuninit(void)
|
|
|
|
{
|
|
|
|
uma_zdestroy(nfsnode_zone);
|
|
|
|
}
|
|
|
|
|
2005-03-16 11:28:19 +00:00
|
|
|
struct nfs_vncmp {
|
|
|
|
int fhsize;
|
|
|
|
void *fh;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
nfs_vncmpf(struct vnode *vp, void *arg)
|
|
|
|
{
|
|
|
|
struct nfs_vncmp *a;
|
|
|
|
struct nfsnode *np;
|
|
|
|
|
|
|
|
a = arg;
|
|
|
|
np = VTONFS(vp);
|
|
|
|
return (bcmp(a->fh, np->n_fhp, a->fhsize));
|
|
|
|
}
|
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
/*
|
|
|
|
* Look up a vnode/nfsnode by file handle.
|
|
|
|
* Callers must check for mount points!!
|
|
|
|
* In all cases, a pointer to a
|
|
|
|
* nfsnode structure is returned.
|
|
|
|
*/
|
1994-05-25 09:21:21 +00:00
|
|
|
int
|
2006-09-13 18:39:09 +00:00
|
|
|
nfs_nget(struct mount *mntp, nfsfh_t *fhp, int fhsize, struct nfsnode **npp, int flags)
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
2001-09-12 08:38:13 +00:00
|
|
|
struct thread *td = curthread; /* XXX */
|
2005-03-16 11:28:19 +00:00
|
|
|
struct nfsnode *np;
|
2001-09-18 23:32:09 +00:00
|
|
|
struct vnode *vp;
|
1994-05-24 10:09:53 +00:00
|
|
|
struct vnode *nvp;
|
|
|
|
int error;
|
2005-03-16 11:28:19 +00:00
|
|
|
u_int hash;
|
2000-01-05 05:11:37 +00:00
|
|
|
struct nfsmount *nmp;
|
2005-03-16 11:28:19 +00:00
|
|
|
struct nfs_vncmp ncmp;
|
2000-01-05 05:11:37 +00:00
|
|
|
|
|
|
|
nmp = VFSTONFS(mntp);
|
2005-03-16 11:28:19 +00:00
|
|
|
*npp = NULL;
|
|
|
|
|
|
|
|
hash = fnv_32_buf(fhp->fh_bytes, fhsize, FNV1_32_INIT);
|
|
|
|
ncmp.fhsize = fhsize;
|
|
|
|
ncmp.fh = fhp;
|
|
|
|
|
2006-09-13 18:39:09 +00:00
|
|
|
error = vfs_hash_get(mntp, hash, flags,
|
2005-03-16 11:28:19 +00:00
|
|
|
td, &nvp, nfs_vncmpf, &ncmp);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
if (nvp != NULL) {
|
|
|
|
*npp = VTONFS(nvp);
|
|
|
|
return (0);
|
1995-07-21 10:25:13 +00:00
|
|
|
}
|
2005-03-16 11:28:19 +00:00
|
|
|
np = uma_zalloc(nfsnode_zone, M_WAITOK | M_ZERO);
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2009-05-22 12:35:12 +00:00
|
|
|
error = getnewvnode("nfs", mntp, &nfs_vnodeops, &nvp);
|
1994-10-02 17:27:07 +00:00
|
|
|
if (error) {
|
2002-03-20 10:07:52 +00:00
|
|
|
uma_zfree(nfsnode_zone, np);
|
1994-05-24 10:09:53 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
vp = nvp;
|
2009-05-22 12:35:12 +00:00
|
|
|
vp->v_bufobj.bo_ops = &buf_ops_nfs;
|
1994-05-24 10:09:53 +00:00
|
|
|
vp->v_data = np;
|
|
|
|
np->n_vnode = vp;
|
2006-11-27 19:06:43 +00:00
|
|
|
/*
|
|
|
|
* Initialize the mutex even if the vnode is going to be a loser.
|
|
|
|
* This simplifies the logic in reclaim, which can then unconditionally
|
|
|
|
* destroy the mutex (in the case of the loser, or if hash_insert happened
|
|
|
|
* to return an error no special casing is needed).
|
|
|
|
*/
|
|
|
|
mtx_init(&np->n_mtx, "NFSnode lock", NULL, MTX_DEF);
|
2006-05-23 16:07:23 +00:00
|
|
|
/*
|
|
|
|
* NFS supports recursive and shared locking.
|
|
|
|
*/
|
2010-08-20 19:46:50 +00:00
|
|
|
lockmgr(vp->v_vnlock, LK_EXCLUSIVE | LK_NOWITNESS, NULL);
|
2008-02-24 16:38:58 +00:00
|
|
|
VN_LOCK_AREC(vp);
|
|
|
|
VN_LOCK_ASHARE(vp);
|
2006-11-29 02:21:40 +00:00
|
|
|
if (fhsize > NFS_SMALLFH) {
|
2008-10-23 15:53:51 +00:00
|
|
|
np->n_fhp = malloc(fhsize, M_NFSBIGFH, M_WAITOK);
|
2006-11-29 02:21:40 +00:00
|
|
|
} else
|
|
|
|
np->n_fhp = &np->n_fh;
|
|
|
|
bcopy((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize);
|
|
|
|
np->n_fhsize = fhsize;
|
2007-03-13 01:50:27 +00:00
|
|
|
error = insmntque(vp, mntp);
|
|
|
|
if (error != 0) {
|
|
|
|
*npp = NULL;
|
|
|
|
if (np->n_fhsize > NFS_SMALLFH) {
|
2008-10-23 15:53:51 +00:00
|
|
|
free((caddr_t)np->n_fhp, M_NFSBIGFH);
|
2007-03-13 01:50:27 +00:00
|
|
|
}
|
|
|
|
mtx_destroy(&np->n_mtx);
|
|
|
|
uma_zfree(nfsnode_zone, np);
|
|
|
|
return (error);
|
|
|
|
}
|
2006-09-13 18:39:09 +00:00
|
|
|
error = vfs_hash_insert(vp, hash, flags,
|
2005-03-16 11:28:19 +00:00
|
|
|
td, &nvp, nfs_vncmpf, &ncmp);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
if (nvp != NULL) {
|
|
|
|
*npp = VTONFS(nvp);
|
2006-11-16 23:03:46 +00:00
|
|
|
/* vfs_hash_insert() vput()'s the losing vnode */
|
2005-03-16 11:28:19 +00:00
|
|
|
return (0);
|
1999-06-05 05:26:36 +00:00
|
|
|
}
|
1994-05-24 10:09:53 +00:00
|
|
|
*npp = np;
|
1995-06-27 11:07:30 +00:00
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2011-08-02 11:24:42 +00:00
|
|
|
/*
|
|
|
|
* Do the vrele(sp->s_dvp) as a separate task in order to avoid a
|
|
|
|
* deadlock because of a LOR when vrele() locks the directory vnode.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
nfs_freesillyrename(void *arg, __unused int pending)
|
|
|
|
{
|
|
|
|
struct sillyrename *sp;
|
|
|
|
|
|
|
|
sp = arg;
|
|
|
|
vrele(sp->s_dvp);
|
|
|
|
free(sp, M_NFSREQ);
|
|
|
|
}
|
|
|
|
|
1994-05-25 09:21:21 +00:00
|
|
|
int
|
2001-09-18 23:32:09 +00:00
|
|
|
nfs_inactive(struct vop_inactive_args *ap)
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
2001-09-18 23:32:09 +00:00
|
|
|
struct nfsnode *np;
|
|
|
|
struct sillyrename *sp;
|
2001-09-12 08:38:13 +00:00
|
|
|
struct thread *td = curthread; /* XXX */
|
1994-05-24 10:09:53 +00:00
|
|
|
|
|
|
|
np = VTONFS(ap->a_vp);
|
2010-08-04 01:19:11 +00:00
|
|
|
mtx_lock(&np->n_mtx);
|
1997-05-09 13:04:43 +00:00
|
|
|
if (ap->a_vp->v_type != VDIR) {
|
1995-06-27 11:07:30 +00:00
|
|
|
sp = np->n_sillyrename;
|
2002-07-11 17:54:58 +00:00
|
|
|
np->n_sillyrename = NULL;
|
1997-05-09 13:04:43 +00:00
|
|
|
} else
|
2002-07-11 17:54:58 +00:00
|
|
|
sp = NULL;
|
1994-05-24 10:09:53 +00:00
|
|
|
if (sp) {
|
2010-08-04 01:19:11 +00:00
|
|
|
mtx_unlock(&np->n_mtx);
|
2005-01-24 12:31:06 +00:00
|
|
|
(void)nfs_vinvalbuf(ap->a_vp, 0, td, 1);
|
1994-05-24 10:09:53 +00:00
|
|
|
/*
|
|
|
|
* Remove the silly file that was rename'd earlier
|
|
|
|
*/
|
2003-11-14 20:54:10 +00:00
|
|
|
(sp->s_removeit)(sp);
|
1994-05-24 10:09:53 +00:00
|
|
|
crfree(sp->s_cred);
|
2011-08-02 11:24:42 +00:00
|
|
|
TASK_INIT(&sp->s_task, 0, nfs_freesillyrename, sp);
|
|
|
|
taskqueue_enqueue(taskqueue_thread, &sp->s_task);
|
2010-08-04 01:19:11 +00:00
|
|
|
mtx_lock(&np->n_mtx);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
2004-12-06 18:52:28 +00:00
|
|
|
np->n_flag &= NMODIFIED;
|
2010-08-04 01:19:11 +00:00
|
|
|
mtx_unlock(&np->n_mtx);
|
1994-05-24 10:09:53 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reclaim an nfsnode so that it can be used for other purposes.
|
|
|
|
*/
|
1994-05-25 09:21:21 +00:00
|
|
|
int
|
2001-09-18 23:32:09 +00:00
|
|
|
nfs_reclaim(struct vop_reclaim_args *ap)
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
2001-09-18 23:32:09 +00:00
|
|
|
struct vnode *vp = ap->a_vp;
|
|
|
|
struct nfsnode *np = VTONFS(vp);
|
|
|
|
struct nfsdmap *dp, *dp2;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
2008-06-26 10:21:54 +00:00
|
|
|
/*
|
|
|
|
* If the NLM is running, give it a chance to abort pending
|
|
|
|
* locks.
|
|
|
|
*/
|
|
|
|
if (nfs_reclaim_p)
|
|
|
|
nfs_reclaim_p(ap);
|
|
|
|
|
2006-01-17 17:29:03 +00:00
|
|
|
/*
|
|
|
|
* Destroy the vm object and flush associated pages.
|
|
|
|
*/
|
|
|
|
vnode_destroy_vobject(vp);
|
|
|
|
|
2005-03-16 11:28:19 +00:00
|
|
|
vfs_hash_remove(vp);
|
1994-05-24 10:09:53 +00:00
|
|
|
|
1995-06-27 11:07:30 +00:00
|
|
|
/*
|
|
|
|
* Free up any directory cookie structures and
|
|
|
|
* large file handle structures that might be associated with
|
|
|
|
* this nfs node.
|
|
|
|
*/
|
|
|
|
if (vp->v_type == VDIR) {
|
2001-09-18 23:32:09 +00:00
|
|
|
dp = LIST_FIRST(&np->n_cookies);
|
1995-06-27 11:07:30 +00:00
|
|
|
while (dp) {
|
|
|
|
dp2 = dp;
|
2001-09-18 23:32:09 +00:00
|
|
|
dp = LIST_NEXT(dp, ndm_list);
|
2008-10-23 15:53:51 +00:00
|
|
|
free((caddr_t)dp2, M_NFSDIROFF);
|
1995-06-27 11:07:30 +00:00
|
|
|
}
|
|
|
|
}
|
2012-05-12 12:02:51 +00:00
|
|
|
if (np->n_writecred != NULL)
|
|
|
|
crfree(np->n_writecred);
|
1995-06-27 11:07:30 +00:00
|
|
|
if (np->n_fhsize > NFS_SMALLFH) {
|
2008-10-23 15:53:51 +00:00
|
|
|
free((caddr_t)np->n_fhp, M_NFSBIGFH);
|
1995-06-27 11:07:30 +00:00
|
|
|
}
|
2006-05-19 00:04:24 +00:00
|
|
|
mtx_destroy(&np->n_mtx);
|
2002-03-20 10:07:52 +00:00
|
|
|
uma_zfree(nfsnode_zone, vp->v_data);
|
2002-07-11 17:54:58 +00:00
|
|
|
vp->v_data = NULL;
|
1994-05-24 10:09:53 +00:00
|
|
|
return (0);
|
|
|
|
}
|