1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
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 * from nfs_node.c 8.6 (Berkeley) 5/22/95
35 */
36
37 #include <sys/cdefs.h>
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/fcntl.h>
41 #include <sys/lock.h>
42 #include <sys/malloc.h>
43 #include <sys/mount.h>
44 #include <sys/namei.h>
45 #include <sys/proc.h>
46 #include <sys/socket.h>
47 #include <sys/sysctl.h>
48 #include <sys/taskqueue.h>
49 #include <sys/vnode.h>
50
51 #include <vm/vm_param.h>
52 #include <vm/vnode_pager.h>
53 #include <vm/uma.h>
54
55 #include <fs/nfs/nfsport.h>
56 #include <fs/nfsclient/nfsnode.h>
57 #include <fs/nfsclient/nfsmount.h>
58 #include <fs/nfsclient/nfs.h>
59 #include <fs/nfsclient/nfs_kdtrace.h>
60
61 #include <nfs/nfs_lock.h>
62
63 extern struct vop_vector newnfs_vnodeops;
64 MALLOC_DECLARE(M_NEWNFSREQ);
65
66 uma_zone_t newnfsnode_zone;
67
68 const char nfs_vnode_tag[] = "nfs";
69
70 static void nfs_freesillyrename(void *arg, __unused int pending);
71
72 void
ncl_nhinit(void)73 ncl_nhinit(void)
74 {
75
76 newnfsnode_zone = uma_zcreate("NCLNODE", sizeof(struct nfsnode), NULL,
77 NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
78 }
79
80 void
ncl_nhuninit(void)81 ncl_nhuninit(void)
82 {
83 uma_zdestroy(newnfsnode_zone);
84 }
85
86 /*
87 * ONLY USED FOR THE ROOT DIRECTORY. nfscl_nget() does the rest. If this
88 * function is going to be used to get Regular Files, code must be added
89 * to fill in the "struct nfsv4node".
90 * Look up a vnode/nfsnode by file handle.
91 * Callers must check for mount points!!
92 * In all cases, a pointer to a
93 * nfsnode structure is returned.
94 */
95 int
ncl_nget(struct mount * mntp,u_int8_t * fhp,int fhsize,struct nfsnode ** npp,int lkflags)96 ncl_nget(struct mount *mntp, u_int8_t *fhp, int fhsize, struct nfsnode **npp,
97 int lkflags)
98 {
99 struct thread *td = curthread; /* XXX */
100 struct nfsnode *np;
101 struct vnode *vp;
102 struct vnode *nvp;
103 int error;
104 u_int hash;
105 struct nfsmount *nmp;
106 struct nfsfh *nfhp;
107
108 nmp = VFSTONFS(mntp);
109 *npp = NULL;
110
111 hash = fnv_32_buf(fhp, fhsize, FNV1_32_INIT);
112
113 nfhp = malloc(sizeof (struct nfsfh) + fhsize,
114 M_NFSFH, M_WAITOK);
115 bcopy(fhp, &nfhp->nfh_fh[0], fhsize);
116 nfhp->nfh_len = fhsize;
117 error = vfs_hash_get(mntp, hash, lkflags,
118 td, &nvp, newnfs_vncmpf, nfhp);
119 free(nfhp, M_NFSFH);
120 if (error)
121 return (error);
122 if (nvp != NULL) {
123 *npp = VTONFS(nvp);
124 return (0);
125 }
126 np = uma_zalloc(newnfsnode_zone, M_WAITOK | M_ZERO);
127
128 error = getnewvnode(nfs_vnode_tag, mntp, &newnfs_vnodeops, &nvp);
129 if (error) {
130 uma_zfree(newnfsnode_zone, np);
131 return (error);
132 }
133 vp = nvp;
134 KASSERT(vp->v_bufobj.bo_bsize != 0, ("ncl_nget: bo_bsize == 0"));
135 vp->v_data = np;
136 np->n_vnode = vp;
137 /*
138 * Initialize the mutex even if the vnode is going to be a loser.
139 * This simplifies the logic in reclaim, which can then unconditionally
140 * destroy the mutex (in the case of the loser, or if hash_insert
141 * happened to return an error no special casing is needed).
142 */
143 mtx_init(&np->n_mtx, "NEWNFSnode lock", NULL, MTX_DEF | MTX_DUPOK);
144 lockinit(&np->n_excl, PVFS, "nfsupg", VLKTIMEOUT, LK_NOSHARE |
145 LK_CANRECURSE);
146
147 /*
148 * NFS supports recursive and shared locking.
149 */
150 lockmgr(vp->v_vnlock, LK_EXCLUSIVE | LK_NOWITNESS, NULL);
151 VN_LOCK_AREC(vp);
152 VN_LOCK_ASHARE(vp);
153 /*
154 * Are we getting the root? If so, make sure the vnode flags
155 * are correct
156 */
157 if (fhsize == NFSX_FHMAX + 1 || (fhsize == nmp->nm_fhsize &&
158 !bcmp(fhp, nmp->nm_fh, fhsize))) {
159 if (vp->v_type == VNON)
160 vp->v_type = VDIR;
161 vp->v_vflag |= VV_ROOT;
162 }
163
164 vp->v_vflag |= VV_VMSIZEVNLOCK;
165
166 np->n_fhp = malloc(sizeof (struct nfsfh) + fhsize,
167 M_NFSFH, M_WAITOK);
168 bcopy(fhp, np->n_fhp->nfh_fh, fhsize);
169 np->n_fhp->nfh_len = fhsize;
170 error = insmntque(vp, mntp);
171 if (error != 0) {
172 *npp = NULL;
173 free(np->n_fhp, M_NFSFH);
174 mtx_destroy(&np->n_mtx);
175 lockdestroy(&np->n_excl);
176 uma_zfree(newnfsnode_zone, np);
177 return (error);
178 }
179 vn_set_state(vp, VSTATE_CONSTRUCTED);
180 error = vfs_hash_insert(vp, hash, lkflags,
181 td, &nvp, newnfs_vncmpf, np->n_fhp);
182 if (error)
183 return (error);
184 if (nvp != NULL) {
185 *npp = VTONFS(nvp);
186 /* vfs_hash_insert() vput()'s the losing vnode */
187 return (0);
188 }
189 *npp = np;
190
191 return (0);
192 }
193
194 /*
195 * Do the vrele(sp->s_dvp) as a separate task in order to avoid a
196 * deadlock because of a LOR when vrele() locks the directory vnode.
197 */
198 static void
nfs_freesillyrename(void * arg,__unused int pending)199 nfs_freesillyrename(void *arg, __unused int pending)
200 {
201 struct sillyrename *sp;
202
203 sp = arg;
204 vrele(sp->s_dvp);
205 free(sp, M_NEWNFSREQ);
206 }
207
208 static void
ncl_releasesillyrename(struct vnode * vp,struct thread * td)209 ncl_releasesillyrename(struct vnode *vp, struct thread *td)
210 {
211 struct nfsnode *np;
212 struct sillyrename *sp;
213
214 ASSERT_VOP_ELOCKED(vp, "releasesillyrename");
215 np = VTONFS(vp);
216 NFSASSERTNODE(np);
217 if (vp->v_type != VDIR) {
218 sp = np->n_sillyrename;
219 np->n_sillyrename = NULL;
220 } else
221 sp = NULL;
222 if (sp != NULL) {
223 NFSUNLOCKNODE(np);
224 (void) ncl_vinvalbuf(vp, 0, td, 1);
225 /*
226 * Remove the silly file that was rename'd earlier
227 */
228 ncl_removeit(sp, vp);
229 crfree(sp->s_cred);
230 TASK_INIT(&sp->s_task, 0, nfs_freesillyrename, sp);
231 taskqueue_enqueue(taskqueue_thread, &sp->s_task);
232 NFSLOCKNODE(np);
233 }
234 }
235
236 int
ncl_inactive(struct vop_inactive_args * ap)237 ncl_inactive(struct vop_inactive_args *ap)
238 {
239 struct vnode *vp = ap->a_vp;
240 struct nfsnode *np;
241 struct thread *td;
242
243 td = curthread;
244 np = VTONFS(vp);
245 if (NFS_ISV4(vp) && vp->v_type == VREG) {
246 NFSLOCKNODE(np);
247 np->n_openstateid = NULL;
248 NFSUNLOCKNODE(np);
249 /*
250 * Since mmap()'d files do I/O after VOP_CLOSE(), the NFSv4
251 * Close operations are delayed until now. Any dirty
252 * buffers/pages must be flushed before the close, so that the
253 * stateid is available for the writes.
254 */
255 vnode_pager_clean_sync(vp);
256 (void)ncl_flush(vp, MNT_WAIT, td, 1, 0);
257 (void)nfsrpc_close(vp, 1, td);
258 }
259
260 NFSLOCKNODE(np);
261 ncl_releasesillyrename(vp, td);
262
263 /*
264 * NMODIFIED means that there might be dirty/stale buffers
265 * associated with the NFS vnode.
266 * NDSCOMMIT means that the file is on a pNFS server and commits
267 * should be done to the DS.
268 * None of the other flags are meaningful after the vnode is unused.
269 */
270 np->n_flag &= (NMODIFIED | NDSCOMMIT);
271 NFSUNLOCKNODE(np);
272 return (0);
273 }
274
275 /*
276 * Reclaim an nfsnode so that it can be used for other purposes.
277 */
278 int
ncl_reclaim(struct vop_reclaim_args * ap)279 ncl_reclaim(struct vop_reclaim_args *ap)
280 {
281 struct vnode *vp = ap->a_vp;
282 struct nfsnode *np = VTONFS(vp);
283 struct nfsdmap *dp, *dp2;
284 struct thread *td;
285 struct mount *mp;
286
287 td = curthread;
288 mp = vp->v_mount;
289
290 /*
291 * If the NLM is running, give it a chance to abort pending
292 * locks.
293 */
294 if (nfs_reclaim_p != NULL)
295 nfs_reclaim_p(ap);
296
297 NFSLOCKNODE(np);
298 ncl_releasesillyrename(vp, td);
299
300 if (NFS_ISV4(vp) && vp->v_type == VREG) {
301 np->n_openstateid = NULL;
302 NFSUNLOCKNODE(np);
303 /*
304 * We can now safely close any remaining NFSv4 Opens for
305 * this file. Most opens will have already been closed by
306 * ncl_inactive(), but there are cases where it is not
307 * called, so we need to do it again here.
308 */
309 (void) nfsrpc_close(vp, 1, td);
310 /*
311 * It it unlikely a delegation will still exist, but
312 * if one does, it must be returned before calling
313 * vfs_hash_remove(), since it cannot be recalled once the
314 * nfs node is no longer available.
315 */
316 MNT_ILOCK(mp);
317 if ((mp->mnt_kern_flag & MNTK_UNMOUNTF) == 0) {
318 MNT_IUNLOCK(mp);
319 nfscl_delegreturnvp(vp, td);
320 } else
321 MNT_IUNLOCK(mp);
322 } else
323 NFSUNLOCKNODE(np);
324
325 vfs_hash_remove(vp);
326
327 /*
328 * Call nfscl_reclaimnode() to save attributes in the delegation,
329 * as required.
330 */
331 if (vp->v_type == VREG)
332 nfscl_reclaimnode(vp);
333
334 /*
335 * Free up any directory cookie structures and
336 * large file handle structures that might be associated with
337 * this nfs node.
338 */
339 if (vp->v_type == VDIR) {
340 dp = LIST_FIRST(&np->n_cookies);
341 while (dp) {
342 dp2 = dp;
343 dp = LIST_NEXT(dp, ndm_list);
344 free(dp2, M_NFSDIROFF);
345 }
346 }
347 if (np->n_writecred != NULL)
348 crfree(np->n_writecred);
349 free(np->n_fhp, M_NFSFH);
350 if (np->n_v4 != NULL)
351 free(np->n_v4, M_NFSV4NODE);
352 mtx_destroy(&np->n_mtx);
353 lockdestroy(&np->n_excl);
354 uma_zfree(newnfsnode_zone, vp->v_data);
355 vp->v_data = NULL;
356 return (0);
357 }
358
359 /*
360 * Invalidate both the access and attribute caches for this vnode.
361 */
362 void
ncl_invalcaches(struct vnode * vp)363 ncl_invalcaches(struct vnode *vp)
364 {
365 struct nfsnode *np = VTONFS(vp);
366 int i;
367
368 NFSLOCKNODE(np);
369 for (i = 0; i < NFS_ACCESSCACHESIZE; i++)
370 np->n_accesscache[i].stamp = 0;
371 KDTRACE_NFS_ACCESSCACHE_FLUSH_DONE(vp);
372 np->n_attrstamp = 0;
373 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
374 NFSUNLOCKNODE(np);
375 }
376