xref: /freebsd/sys/fs/nfsclient/nfs_clsubs.c (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
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_subs.c  8.8 (Berkeley) 5/22/95
35  */
36 
37 #include <sys/cdefs.h>
38 /*
39  * These functions support the macros and help fiddle mbuf chains for
40  * the nfs op functions. They do things like create the rpc header and
41  * copy data between mbuf chains and uio lists.
42  */
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/bio.h>
48 #include <sys/buf.h>
49 #include <sys/proc.h>
50 #include <sys/mount.h>
51 #include <sys/vnode.h>
52 #include <sys/namei.h>
53 #include <sys/mbuf.h>
54 #include <sys/socket.h>
55 #include <sys/stat.h>
56 #include <sys/malloc.h>
57 #include <sys/syscall.h>
58 #include <sys/sysproto.h>
59 #include <sys/taskqueue.h>
60 
61 #include <vm/vm.h>
62 #include <vm/vm_object.h>
63 #include <vm/vm_extern.h>
64 #include <vm/uma.h>
65 
66 #include <fs/nfs/nfsport.h>
67 #include <fs/nfsclient/nfsnode.h>
68 #include <fs/nfsclient/nfsmount.h>
69 #include <fs/nfsclient/nfs.h>
70 #include <fs/nfsclient/nfs_kdtrace.h>
71 
72 #include <netinet/in.h>
73 
74 /*
75  * Note that stdarg.h and the ANSI style va_start macro is used for both
76  * ANSI and traditional C compilers.
77  */
78 #include <machine/stdarg.h>
79 
80 extern struct mtx ncl_iod_mutex;
81 extern enum nfsiod_state ncl_iodwant[NFS_MAXASYNCDAEMON];
82 extern struct nfsmount *ncl_iodmount[NFS_MAXASYNCDAEMON];
83 extern int ncl_numasync;
84 extern unsigned int ncl_iodmax;
85 extern struct nfsstatsv1 nfsstatsv1;
86 
87 struct task	ncl_nfsiodnew_task;
88 
89 int
90 ncl_uninit(struct vfsconf *vfsp)
91 {
92 	/*
93 	 * XXX: Unloading of nfscl module is unsupported.
94 	 */
95 #if 0
96 	int i;
97 
98 	/*
99 	 * Tell all nfsiod processes to exit. Clear ncl_iodmax, and wakeup
100 	 * any sleeping nfsiods so they check ncl_iodmax and exit.
101 	 */
102 	NFSLOCKIOD();
103 	ncl_iodmax = 0;
104 	for (i = 0; i < ncl_numasync; i++)
105 		if (ncl_iodwant[i] == NFSIOD_AVAILABLE)
106 			wakeup(&ncl_iodwant[i]);
107 	/* The last nfsiod to exit will wake us up when ncl_numasync hits 0 */
108 	while (ncl_numasync)
109 		msleep(&ncl_numasync, &ncl_iod_mutex, PWAIT, "ioddie", 0);
110 	NFSUNLOCKIOD();
111 	ncl_nhuninit();
112 	return (0);
113 #else
114 	return (EOPNOTSUPP);
115 #endif
116 }
117 
118 /* Returns with NFSLOCKNODE() held. */
119 void
120 ncl_dircookie_lock(struct nfsnode *np)
121 {
122 	NFSLOCKNODE(np);
123 	while (np->n_flag & NDIRCOOKIELK)
124 		(void) msleep(&np->n_flag, &np->n_mtx, PZERO, "nfsdirlk", 0);
125 	np->n_flag |= NDIRCOOKIELK;
126 }
127 
128 void
129 ncl_dircookie_unlock(struct nfsnode *np)
130 {
131 	NFSLOCKNODE(np);
132 	np->n_flag &= ~NDIRCOOKIELK;
133 	wakeup(&np->n_flag);
134 	NFSUNLOCKNODE(np);
135 }
136 
137 bool
138 ncl_excl_start(struct vnode *vp)
139 {
140 	struct nfsnode *np;
141 	int vn_lk;
142 
143 	ASSERT_VOP_LOCKED(vp, "ncl_excl_start");
144 	vn_lk = NFSVOPISLOCKED(vp);
145 	if (vn_lk == LK_EXCLUSIVE)
146 		return (false);
147 	KASSERT(vn_lk == LK_SHARED,
148 	    ("ncl_excl_start: wrong vnode lock %d", vn_lk));
149 	/* Ensure exclusive access, this might block */
150 	np = VTONFS(vp);
151 	lockmgr(&np->n_excl, LK_EXCLUSIVE, NULL);
152 	return (true);
153 }
154 
155 void
156 ncl_excl_finish(struct vnode *vp, bool old_lock)
157 {
158 	struct nfsnode *np;
159 
160 	if (!old_lock)
161 		return;
162 	np = VTONFS(vp);
163 	lockmgr(&np->n_excl, LK_RELEASE, NULL);
164 }
165 
166 #ifdef NFS_ACDEBUG
167 #include <sys/sysctl.h>
168 SYSCTL_DECL(_vfs_nfs);
169 static int nfs_acdebug;
170 SYSCTL_INT(_vfs_nfs, OID_AUTO, acdebug, CTLFLAG_RW, &nfs_acdebug, 0, "");
171 #endif
172 
173 /*
174  * Check the time stamp
175  * If the cache is valid, copy contents to *vap and return 0
176  * otherwise return an error
177  */
178 int
179 ncl_getattrcache(struct vnode *vp, struct vattr *vaper)
180 {
181 	struct nfsnode *np;
182 	struct vattr *vap;
183 	struct nfsmount *nmp;
184 	int timeo, mustflush;
185 	u_quad_t nsize;
186 	bool setnsize;
187 
188 	np = VTONFS(vp);
189 	vap = &np->n_vattr.na_vattr;
190 	nmp = VFSTONFS(vp->v_mount);
191 	mustflush = nfscl_mustflush(vp);	/* must be before mtx_lock() */
192 	NFSLOCKNODE(np);
193 	/* XXX n_mtime doesn't seem to be updated on a miss-and-reload */
194 	timeo = (time_second - np->n_mtime.tv_sec) / 10;
195 
196 #ifdef NFS_ACDEBUG
197 	if (nfs_acdebug>1)
198 		printf("ncl_getattrcache: initial timeo = %d\n", timeo);
199 #endif
200 
201 	if (vap->va_type == VDIR) {
202 		if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acdirmin)
203 			timeo = nmp->nm_acdirmin;
204 		else if (timeo > nmp->nm_acdirmax)
205 			timeo = nmp->nm_acdirmax;
206 	} else {
207 		if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acregmin)
208 			timeo = nmp->nm_acregmin;
209 		else if (timeo > nmp->nm_acregmax)
210 			timeo = nmp->nm_acregmax;
211 	}
212 
213 #ifdef NFS_ACDEBUG
214 	if (nfs_acdebug > 2)
215 		printf("acregmin %d; acregmax %d; acdirmin %d; acdirmax %d\n",
216 		    nmp->nm_acregmin, nmp->nm_acregmax,
217 		    nmp->nm_acdirmin, nmp->nm_acdirmax);
218 
219 	if (nfs_acdebug)
220 		printf("ncl_getattrcache: age = %d; final timeo = %d\n",
221 		    (time_second - np->n_attrstamp), timeo);
222 #endif
223 
224 	if ((time_second - np->n_attrstamp) >= timeo &&
225 	    (mustflush != 0 || np->n_attrstamp == 0)) {
226 		nfsstatsv1.attrcache_misses++;
227 		NFSUNLOCKNODE(np);
228 		KDTRACE_NFS_ATTRCACHE_GET_MISS(vp);
229 		return( ENOENT);
230 	}
231 	nfsstatsv1.attrcache_hits++;
232 	setnsize = false;
233 	if (vap->va_size != np->n_size) {
234 		if (vap->va_type == VREG) {
235 			if (np->n_flag & NMODIFIED) {
236 				if (vap->va_size < np->n_size)
237 					vap->va_size = np->n_size;
238 				else
239 					np->n_size = vap->va_size;
240 			} else {
241 				np->n_size = vap->va_size;
242 			}
243 			setnsize = ncl_pager_setsize(vp, &nsize);
244 		} else {
245 			np->n_size = vap->va_size;
246 		}
247 	}
248 	bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr));
249 	if (np->n_flag & NCHG) {
250 		if (np->n_flag & NACC)
251 			vaper->va_atime = np->n_atim;
252 		if (np->n_flag & NUPD)
253 			vaper->va_mtime = np->n_mtim;
254 	}
255 	NFSUNLOCKNODE(np);
256 	if (setnsize)
257 		vnode_pager_setsize(vp, nsize);
258 	KDTRACE_NFS_ATTRCACHE_GET_HIT(vp, vap);
259 	return (0);
260 }
261 
262 static nfsuint64 nfs_nullcookie = { { 0, 0 } };
263 /*
264  * This function finds the directory cookie that corresponds to the
265  * logical byte offset given.
266  */
267 nfsuint64 *
268 ncl_getcookie(struct nfsnode *np, off_t off, int add)
269 {
270 	struct nfsdmap *dp, *dp2;
271 	int pos;
272 	nfsuint64 *retval = NULL;
273 
274 	pos = (uoff_t)off / NFS_DIRBLKSIZ;
275 	if (pos == 0 || off < 0) {
276 		KASSERT(!add, ("nfs getcookie add at <= 0"));
277 		return (&nfs_nullcookie);
278 	}
279 	pos--;
280 	dp = LIST_FIRST(&np->n_cookies);
281 	if (!dp) {
282 		if (add) {
283 			dp = malloc(sizeof (struct nfsdmap),
284 				M_NFSDIROFF, M_WAITOK);
285 			dp->ndm_eocookie = 0;
286 			LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list);
287 		} else
288 			goto out;
289 	}
290 	while (pos >= NFSNUMCOOKIES) {
291 		pos -= NFSNUMCOOKIES;
292 		if (LIST_NEXT(dp, ndm_list)) {
293 			if (!add && dp->ndm_eocookie < NFSNUMCOOKIES &&
294 			    pos >= dp->ndm_eocookie)
295 				goto out;
296 			dp = LIST_NEXT(dp, ndm_list);
297 		} else if (add) {
298 			dp2 = malloc(sizeof (struct nfsdmap),
299 				M_NFSDIROFF, M_WAITOK);
300 			dp2->ndm_eocookie = 0;
301 			LIST_INSERT_AFTER(dp, dp2, ndm_list);
302 			dp = dp2;
303 		} else
304 			goto out;
305 	}
306 	if (pos >= dp->ndm_eocookie) {
307 		if (add)
308 			dp->ndm_eocookie = pos + 1;
309 		else
310 			goto out;
311 	}
312 	retval = &dp->ndm_cookies[pos];
313 out:
314 	return (retval);
315 }
316 
317 /*
318  * Invalidate cached directory information, except for the actual directory
319  * blocks (which are invalidated separately).
320  * Done mainly to avoid the use of stale offset cookies.
321  */
322 void
323 ncl_invaldir(struct vnode *vp)
324 {
325 	struct nfsnode *np = VTONFS(vp);
326 
327 	KASSERT(vp->v_type == VDIR, ("nfs: invaldir not dir"));
328 	ncl_dircookie_lock(np);
329 	np->n_direofoffset = 0;
330 	NFSUNLOCKNODE(np);
331 	np->n_cookieverf.nfsuquad[0] = 0;
332 	np->n_cookieverf.nfsuquad[1] = 0;
333 	if (LIST_FIRST(&np->n_cookies))
334 		LIST_FIRST(&np->n_cookies)->ndm_eocookie = 0;
335 	ncl_dircookie_unlock(np);
336 }
337 
338 /*
339  * The write verifier has changed (probably due to a server reboot), so all
340  * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
341  * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
342  * and B_CLUSTEROK flags.  Once done the new write verifier can be set for the
343  * mount point.
344  *
345  * B_CLUSTEROK must be cleared along with B_NEEDCOMMIT because stage 1 data
346  * writes are not clusterable.
347  */
348 void
349 ncl_clearcommit(struct mount *mp)
350 {
351 	struct vnode *vp, *nvp;
352 	struct buf *bp, *nbp;
353 	struct bufobj *bo;
354 
355 	MNT_VNODE_FOREACH_ALL(vp, mp, nvp) {
356 		bo = &vp->v_bufobj;
357 		vholdl(vp);
358 		VI_UNLOCK(vp);
359 		BO_LOCK(bo);
360 		TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
361 			if (!BUF_ISLOCKED(bp) &&
362 			    (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
363 				== (B_DELWRI | B_NEEDCOMMIT))
364 				bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
365 		}
366 		BO_UNLOCK(bo);
367 		vdrop(vp);
368 	}
369 }
370 
371 /*
372  * Called once to initialize data structures...
373  */
374 int
375 ncl_init(struct vfsconf *vfsp)
376 {
377 	int i;
378 
379 	/* Ensure async daemons disabled */
380 	for (i = 0; i < NFS_MAXASYNCDAEMON; i++) {
381 		ncl_iodwant[i] = NFSIOD_NOT_AVAILABLE;
382 		ncl_iodmount[i] = NULL;
383 	}
384 	TASK_INIT(&ncl_nfsiodnew_task, 0, ncl_nfsiodnew_tq, NULL);
385 	ncl_nhinit();			/* Init the nfsnode table */
386 
387 	return (0);
388 }
389