Deleted Added
full compact
nfs_clnode.c (197048) nfs_clnode.c (207082)
1/*-
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * from nfs_node.c 8.6 (Berkeley) 5/22/95
33 */
34
35#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * from nfs_node.c 8.6 (Berkeley) 5/22/95
33 */
34
35#include <sys/cdefs.h>
36__FBSDID("$FreeBSD: head/sys/fs/nfsclient/nfs_clnode.c 197048 2009-09-09 20:37:49Z rmacklem $");
36__FBSDID("$FreeBSD: head/sys/fs/nfsclient/nfs_clnode.c 207082 2010-04-22 23:51:01Z rmacklem $");
37
38#include <sys/param.h>
39#include <sys/systm.h>
40#include <sys/lock.h>
41#include <sys/malloc.h>
42#include <sys/mount.h>
43#include <sys/namei.h>
44#include <sys/proc.h>
45#include <sys/socket.h>
46#include <sys/sysctl.h>
47#include <sys/vnode.h>
48
49#include <vm/uma.h>
50
51#include <fs/nfs/nfsport.h>
52#include <fs/nfsclient/nfsnode.h>
53#include <fs/nfsclient/nfsmount.h>
54#include <fs/nfsclient/nfs.h>
55
56extern struct vop_vector newnfs_vnodeops;
57extern struct buf_ops buf_ops_newnfs;
58MALLOC_DECLARE(M_NEWNFSREQ);
59
60uma_zone_t newnfsnode_zone;
61vop_reclaim_t *ncl_reclaim_p = NULL;
62
63void
64ncl_nhinit(void)
65{
66
67 newnfsnode_zone = uma_zcreate("NCLNODE", sizeof(struct nfsnode), NULL,
68 NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
69}
70
71void
72ncl_nhuninit(void)
73{
74 uma_zdestroy(newnfsnode_zone);
75}
76
77/*
78 * ONLY USED FOR THE ROOT DIRECTORY. nfscl_nget() does the rest. If this
79 * function is going to be used to get Regular Files, code must be added
80 * to fill in the "struct nfsv4node".
81 * Look up a vnode/nfsnode by file handle.
82 * Callers must check for mount points!!
83 * In all cases, a pointer to a
84 * nfsnode structure is returned.
85 */
86int
87ncl_nget(struct mount *mntp, u_int8_t *fhp, int fhsize, struct nfsnode **npp)
88{
89 struct thread *td = curthread; /* XXX */
90 struct nfsnode *np;
91 struct vnode *vp;
92 struct vnode *nvp;
93 int error;
94 u_int hash;
95 struct nfsmount *nmp;
96 struct nfsfh *nfhp;
97
98 nmp = VFSTONFS(mntp);
99 *npp = NULL;
100
101 hash = fnv_32_buf(fhp, fhsize, FNV1_32_INIT);
102
103 MALLOC(nfhp, struct nfsfh *, sizeof (struct nfsfh) + fhsize,
104 M_NFSFH, M_WAITOK);
105 bcopy(fhp, &nfhp->nfh_fh[0], fhsize);
106 nfhp->nfh_len = fhsize;
107 error = vfs_hash_get(mntp, hash, LK_EXCLUSIVE,
108 td, &nvp, newnfs_vncmpf, nfhp);
109 FREE(nfhp, M_NFSFH);
110 if (error)
111 return (error);
112 if (nvp != NULL) {
113 *npp = VTONFS(nvp);
114 return (0);
115 }
116
117 /*
118 * Allocate before getnewvnode since doing so afterward
119 * might cause a bogus v_data pointer to get dereferenced
120 * elsewhere if zalloc should block.
121 */
122 np = uma_zalloc(newnfsnode_zone, M_WAITOK | M_ZERO);
123
124 error = getnewvnode("newnfs", mntp, &newnfs_vnodeops, &nvp);
125 if (error) {
126 uma_zfree(newnfsnode_zone, np);
127 return (error);
128 }
129 vp = nvp;
130 vp->v_bufobj.bo_ops = &buf_ops_newnfs;
131 vp->v_data = np;
132 np->n_vnode = vp;
133 /*
134 * Initialize the mutex even if the vnode is going to be a loser.
135 * This simplifies the logic in reclaim, which can then unconditionally
136 * destroy the mutex (in the case of the loser, or if hash_insert
137 * happened to return an error no special casing is needed).
138 */
139 mtx_init(&np->n_mtx, "NEWNFSnode lock", NULL, MTX_DEF | MTX_DUPOK);
140 /*
141 * NFS supports recursive and shared locking.
142 */
143 VN_LOCK_AREC(vp);
144 VN_LOCK_ASHARE(vp);
145 /*
146 * Are we getting the root? If so, make sure the vnode flags
147 * are correct
148 */
149 if ((fhsize == nmp->nm_fhsize) &&
150 !bcmp(fhp, nmp->nm_fh, fhsize)) {
151 if (vp->v_type == VNON)
152 vp->v_type = VDIR;
153 vp->v_vflag |= VV_ROOT;
154 }
155
156 MALLOC(np->n_fhp, struct nfsfh *, sizeof (struct nfsfh) + fhsize,
157 M_NFSFH, M_WAITOK);
158 bcopy(fhp, np->n_fhp->nfh_fh, fhsize);
159 np->n_fhp->nfh_len = fhsize;
160 lockmgr(vp->v_vnlock, LK_EXCLUSIVE | LK_NOWITNESS, NULL);
161 error = insmntque(vp, mntp);
162 if (error != 0) {
163 *npp = NULL;
164 FREE((caddr_t)np->n_fhp, M_NFSFH);
165 mtx_destroy(&np->n_mtx);
166 uma_zfree(newnfsnode_zone, np);
167 return (error);
168 }
169 error = vfs_hash_insert(vp, hash, LK_EXCLUSIVE,
170 td, &nvp, newnfs_vncmpf, np->n_fhp);
171 if (error)
172 return (error);
173 if (nvp != NULL) {
174 *npp = VTONFS(nvp);
175 /* vfs_hash_insert() vput()'s the losing vnode */
176 return (0);
177 }
178 *npp = np;
179
180 return (0);
181}
182
183int
184ncl_inactive(struct vop_inactive_args *ap)
185{
186 struct nfsnode *np;
187 struct sillyrename *sp;
188 struct vnode *vp = ap->a_vp;
189
190 np = VTONFS(vp);
191 if (prtactive && vrefcnt(vp) != 0)
192 vprint("ncl_inactive: pushing active", vp);
193
194 if (NFS_ISV4(vp) && vp->v_type == VREG) {
195 /*
196 * Since mmap()'d files do I/O after VOP_CLOSE(), the NFSv4
197 * Close operations are delayed until now. Any dirty buffers
198 * must be flushed before the close, so that the stateid is
199 * available for the writes.
200 */
201 if (nfscl_mustflush(vp))
37
38#include <sys/param.h>
39#include <sys/systm.h>
40#include <sys/lock.h>
41#include <sys/malloc.h>
42#include <sys/mount.h>
43#include <sys/namei.h>
44#include <sys/proc.h>
45#include <sys/socket.h>
46#include <sys/sysctl.h>
47#include <sys/vnode.h>
48
49#include <vm/uma.h>
50
51#include <fs/nfs/nfsport.h>
52#include <fs/nfsclient/nfsnode.h>
53#include <fs/nfsclient/nfsmount.h>
54#include <fs/nfsclient/nfs.h>
55
56extern struct vop_vector newnfs_vnodeops;
57extern struct buf_ops buf_ops_newnfs;
58MALLOC_DECLARE(M_NEWNFSREQ);
59
60uma_zone_t newnfsnode_zone;
61vop_reclaim_t *ncl_reclaim_p = NULL;
62
63void
64ncl_nhinit(void)
65{
66
67 newnfsnode_zone = uma_zcreate("NCLNODE", sizeof(struct nfsnode), NULL,
68 NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
69}
70
71void
72ncl_nhuninit(void)
73{
74 uma_zdestroy(newnfsnode_zone);
75}
76
77/*
78 * ONLY USED FOR THE ROOT DIRECTORY. nfscl_nget() does the rest. If this
79 * function is going to be used to get Regular Files, code must be added
80 * to fill in the "struct nfsv4node".
81 * Look up a vnode/nfsnode by file handle.
82 * Callers must check for mount points!!
83 * In all cases, a pointer to a
84 * nfsnode structure is returned.
85 */
86int
87ncl_nget(struct mount *mntp, u_int8_t *fhp, int fhsize, struct nfsnode **npp)
88{
89 struct thread *td = curthread; /* XXX */
90 struct nfsnode *np;
91 struct vnode *vp;
92 struct vnode *nvp;
93 int error;
94 u_int hash;
95 struct nfsmount *nmp;
96 struct nfsfh *nfhp;
97
98 nmp = VFSTONFS(mntp);
99 *npp = NULL;
100
101 hash = fnv_32_buf(fhp, fhsize, FNV1_32_INIT);
102
103 MALLOC(nfhp, struct nfsfh *, sizeof (struct nfsfh) + fhsize,
104 M_NFSFH, M_WAITOK);
105 bcopy(fhp, &nfhp->nfh_fh[0], fhsize);
106 nfhp->nfh_len = fhsize;
107 error = vfs_hash_get(mntp, hash, LK_EXCLUSIVE,
108 td, &nvp, newnfs_vncmpf, nfhp);
109 FREE(nfhp, M_NFSFH);
110 if (error)
111 return (error);
112 if (nvp != NULL) {
113 *npp = VTONFS(nvp);
114 return (0);
115 }
116
117 /*
118 * Allocate before getnewvnode since doing so afterward
119 * might cause a bogus v_data pointer to get dereferenced
120 * elsewhere if zalloc should block.
121 */
122 np = uma_zalloc(newnfsnode_zone, M_WAITOK | M_ZERO);
123
124 error = getnewvnode("newnfs", mntp, &newnfs_vnodeops, &nvp);
125 if (error) {
126 uma_zfree(newnfsnode_zone, np);
127 return (error);
128 }
129 vp = nvp;
130 vp->v_bufobj.bo_ops = &buf_ops_newnfs;
131 vp->v_data = np;
132 np->n_vnode = vp;
133 /*
134 * Initialize the mutex even if the vnode is going to be a loser.
135 * This simplifies the logic in reclaim, which can then unconditionally
136 * destroy the mutex (in the case of the loser, or if hash_insert
137 * happened to return an error no special casing is needed).
138 */
139 mtx_init(&np->n_mtx, "NEWNFSnode lock", NULL, MTX_DEF | MTX_DUPOK);
140 /*
141 * NFS supports recursive and shared locking.
142 */
143 VN_LOCK_AREC(vp);
144 VN_LOCK_ASHARE(vp);
145 /*
146 * Are we getting the root? If so, make sure the vnode flags
147 * are correct
148 */
149 if ((fhsize == nmp->nm_fhsize) &&
150 !bcmp(fhp, nmp->nm_fh, fhsize)) {
151 if (vp->v_type == VNON)
152 vp->v_type = VDIR;
153 vp->v_vflag |= VV_ROOT;
154 }
155
156 MALLOC(np->n_fhp, struct nfsfh *, sizeof (struct nfsfh) + fhsize,
157 M_NFSFH, M_WAITOK);
158 bcopy(fhp, np->n_fhp->nfh_fh, fhsize);
159 np->n_fhp->nfh_len = fhsize;
160 lockmgr(vp->v_vnlock, LK_EXCLUSIVE | LK_NOWITNESS, NULL);
161 error = insmntque(vp, mntp);
162 if (error != 0) {
163 *npp = NULL;
164 FREE((caddr_t)np->n_fhp, M_NFSFH);
165 mtx_destroy(&np->n_mtx);
166 uma_zfree(newnfsnode_zone, np);
167 return (error);
168 }
169 error = vfs_hash_insert(vp, hash, LK_EXCLUSIVE,
170 td, &nvp, newnfs_vncmpf, np->n_fhp);
171 if (error)
172 return (error);
173 if (nvp != NULL) {
174 *npp = VTONFS(nvp);
175 /* vfs_hash_insert() vput()'s the losing vnode */
176 return (0);
177 }
178 *npp = np;
179
180 return (0);
181}
182
183int
184ncl_inactive(struct vop_inactive_args *ap)
185{
186 struct nfsnode *np;
187 struct sillyrename *sp;
188 struct vnode *vp = ap->a_vp;
189
190 np = VTONFS(vp);
191 if (prtactive && vrefcnt(vp) != 0)
192 vprint("ncl_inactive: pushing active", vp);
193
194 if (NFS_ISV4(vp) && vp->v_type == VREG) {
195 /*
196 * Since mmap()'d files do I/O after VOP_CLOSE(), the NFSv4
197 * Close operations are delayed until now. Any dirty buffers
198 * must be flushed before the close, so that the stateid is
199 * available for the writes.
200 */
201 if (nfscl_mustflush(vp))
202 (void) ncl_flush(vp, MNT_WAIT, NULL, ap->a_td, 1);
202 (void) ncl_flush(vp, MNT_WAIT, NULL, ap->a_td, 1, 0);
203 (void) nfsrpc_close(vp, 1, ap->a_td);
204 }
205
206 if (vp->v_type != VDIR) {
207 sp = np->n_sillyrename;
208 np->n_sillyrename = NULL;
209 } else
210 sp = NULL;
211 if (sp) {
212 (void) ncl_vinvalbuf(vp, 0, ap->a_td, 1);
213 /*
214 * Remove the silly file that was rename'd earlier
215 */
216 ncl_removeit(sp, vp);
217 crfree(sp->s_cred);
218 vrele(sp->s_dvp);
219 FREE((caddr_t)sp, M_NEWNFSREQ);
220 }
221 np->n_flag &= NMODIFIED;
222 return (0);
223}
224
225/*
226 * Reclaim an nfsnode so that it can be used for other purposes.
227 */
228int
229ncl_reclaim(struct vop_reclaim_args *ap)
230{
231 struct vnode *vp = ap->a_vp;
232 struct nfsnode *np = VTONFS(vp);
233 struct nfsdmap *dp, *dp2;
234
235 if (prtactive && vrefcnt(vp) != 0)
236 vprint("ncl_reclaim: pushing active", vp);
237
238 /*
239 * If the NLM is running, give it a chance to abort pending
240 * locks.
241 */
242 if (ncl_reclaim_p)
243 ncl_reclaim_p(ap);
244
245 /*
246 * Destroy the vm object and flush associated pages.
247 */
248 vnode_destroy_vobject(vp);
249
250 vfs_hash_remove(vp);
251
252 /*
253 * Call nfscl_reclaimnode() to save attributes in the delegation,
254 * as required.
255 */
256 if (vp->v_type == VREG)
257 nfscl_reclaimnode(vp);
258
259 /*
260 * Free up any directory cookie structures and
261 * large file handle structures that might be associated with
262 * this nfs node.
263 */
264 if (vp->v_type == VDIR) {
265 dp = LIST_FIRST(&np->n_cookies);
266 while (dp) {
267 dp2 = dp;
268 dp = LIST_NEXT(dp, ndm_list);
269 FREE((caddr_t)dp2, M_NFSDIROFF);
270 }
271 }
272 FREE((caddr_t)np->n_fhp, M_NFSFH);
273 if (np->n_v4 != NULL)
274 FREE((caddr_t)np->n_v4, M_NFSV4NODE);
275 mtx_destroy(&np->n_mtx);
276 uma_zfree(newnfsnode_zone, vp->v_data);
277 vp->v_data = NULL;
278 return (0);
279}
280
281/*
282 * Invalidate both the access and attribute caches for this vnode.
283 */
284void
285ncl_invalcaches(struct vnode *vp)
286{
287 struct nfsnode *np = VTONFS(vp);
288 int i;
289
290 mtx_lock(&np->n_mtx);
291 for (i = 0; i < NFS_ACCESSCACHESIZE; i++)
292 np->n_accesscache[i].stamp = 0;
293 np->n_attrstamp = 0;
294 mtx_unlock(&np->n_mtx);
295}
296
203 (void) nfsrpc_close(vp, 1, ap->a_td);
204 }
205
206 if (vp->v_type != VDIR) {
207 sp = np->n_sillyrename;
208 np->n_sillyrename = NULL;
209 } else
210 sp = NULL;
211 if (sp) {
212 (void) ncl_vinvalbuf(vp, 0, ap->a_td, 1);
213 /*
214 * Remove the silly file that was rename'd earlier
215 */
216 ncl_removeit(sp, vp);
217 crfree(sp->s_cred);
218 vrele(sp->s_dvp);
219 FREE((caddr_t)sp, M_NEWNFSREQ);
220 }
221 np->n_flag &= NMODIFIED;
222 return (0);
223}
224
225/*
226 * Reclaim an nfsnode so that it can be used for other purposes.
227 */
228int
229ncl_reclaim(struct vop_reclaim_args *ap)
230{
231 struct vnode *vp = ap->a_vp;
232 struct nfsnode *np = VTONFS(vp);
233 struct nfsdmap *dp, *dp2;
234
235 if (prtactive && vrefcnt(vp) != 0)
236 vprint("ncl_reclaim: pushing active", vp);
237
238 /*
239 * If the NLM is running, give it a chance to abort pending
240 * locks.
241 */
242 if (ncl_reclaim_p)
243 ncl_reclaim_p(ap);
244
245 /*
246 * Destroy the vm object and flush associated pages.
247 */
248 vnode_destroy_vobject(vp);
249
250 vfs_hash_remove(vp);
251
252 /*
253 * Call nfscl_reclaimnode() to save attributes in the delegation,
254 * as required.
255 */
256 if (vp->v_type == VREG)
257 nfscl_reclaimnode(vp);
258
259 /*
260 * Free up any directory cookie structures and
261 * large file handle structures that might be associated with
262 * this nfs node.
263 */
264 if (vp->v_type == VDIR) {
265 dp = LIST_FIRST(&np->n_cookies);
266 while (dp) {
267 dp2 = dp;
268 dp = LIST_NEXT(dp, ndm_list);
269 FREE((caddr_t)dp2, M_NFSDIROFF);
270 }
271 }
272 FREE((caddr_t)np->n_fhp, M_NFSFH);
273 if (np->n_v4 != NULL)
274 FREE((caddr_t)np->n_v4, M_NFSV4NODE);
275 mtx_destroy(&np->n_mtx);
276 uma_zfree(newnfsnode_zone, vp->v_data);
277 vp->v_data = NULL;
278 return (0);
279}
280
281/*
282 * Invalidate both the access and attribute caches for this vnode.
283 */
284void
285ncl_invalcaches(struct vnode *vp)
286{
287 struct nfsnode *np = VTONFS(vp);
288 int i;
289
290 mtx_lock(&np->n_mtx);
291 for (i = 0; i < NFS_ACCESSCACHESIZE; i++)
292 np->n_accesscache[i].stamp = 0;
293 np->n_attrstamp = 0;
294 mtx_unlock(&np->n_mtx);
295}
296