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