nfs_clsubs.c revision 210201
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_subs.c  8.8 (Berkeley) 5/22/95
33 */
34
35#include <sys/cdefs.h>
36__FBSDID("$FreeBSD: head/sys/fs/nfsclient/nfs_clsubs.c 210201 2010-07-18 00:24:01Z rmacklem $");
37
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/sysent.h>
58#include <sys/syscall.h>
59#include <sys/sysproto.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_lock.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
80extern struct mtx ncl_iod_mutex;
81extern enum nfsiod_state ncl_iodwant[NFS_MAXRAHEAD];
82extern struct nfsmount *ncl_iodmount[NFS_MAXRAHEAD];
83extern int ncl_numasync;
84extern unsigned int ncl_iodmax;
85extern struct nfsstats newnfsstats;
86
87int
88ncl_uninit(struct vfsconf *vfsp)
89{
90	/*
91	 * XXX: Unloading of nfscl module is unsupported.
92	 */
93#if 0
94	int i;
95
96	/*
97	 * Tell all nfsiod processes to exit. Clear ncl_iodmax, and wakeup
98	 * any sleeping nfsiods so they check ncl_iodmax and exit.
99	 */
100	mtx_lock(&ncl_iod_mutex);
101	ncl_iodmax = 0;
102	for (i = 0; i < ncl_numasync; i++)
103		if (ncl_iodwant[i] == NFSIOD_AVAILABLE)
104			wakeup(&ncl_iodwant[i]);
105	/* The last nfsiod to exit will wake us up when ncl_numasync hits 0 */
106	while (ncl_numasync)
107		msleep(&ncl_numasync, &ncl_iod_mutex, PWAIT, "ioddie", 0);
108	mtx_unlock(&ncl_iod_mutex);
109	ncl_nhuninit();
110	return (0);
111#else
112	return (EOPNOTSUPP);
113#endif
114}
115
116void
117ncl_dircookie_lock(struct nfsnode *np)
118{
119	mtx_lock(&np->n_mtx);
120	while (np->n_flag & NDIRCOOKIELK)
121		(void) msleep(&np->n_flag, &np->n_mtx, PZERO, "nfsdirlk", 0);
122	np->n_flag |= NDIRCOOKIELK;
123	mtx_unlock(&np->n_mtx);
124}
125
126void
127ncl_dircookie_unlock(struct nfsnode *np)
128{
129	mtx_lock(&np->n_mtx);
130	np->n_flag &= ~NDIRCOOKIELK;
131	wakeup(&np->n_flag);
132	mtx_unlock(&np->n_mtx);
133}
134
135int
136ncl_upgrade_vnlock(struct vnode *vp)
137{
138	int old_lock;
139
140	ASSERT_VOP_LOCKED(vp, "ncl_upgrade_vnlock");
141	old_lock = VOP_ISLOCKED(vp);
142	if (old_lock != LK_EXCLUSIVE) {
143		KASSERT(old_lock == LK_SHARED,
144		    ("ncl_upgrade_vnlock: wrong old_lock %d", old_lock));
145		/* Upgrade to exclusive lock, this might block */
146		vn_lock(vp, LK_UPGRADE | LK_RETRY);
147  	}
148	return (old_lock);
149}
150
151void
152ncl_downgrade_vnlock(struct vnode *vp, int old_lock)
153{
154	if (old_lock != LK_EXCLUSIVE) {
155		KASSERT(old_lock == LK_SHARED, ("wrong old_lock %d", old_lock));
156		/* Downgrade from exclusive lock. */
157		vn_lock(vp, LK_DOWNGRADE | LK_RETRY);
158  	}
159}
160
161void
162ncl_printf(const char *fmt, ...)
163{
164	va_list ap;
165
166	mtx_lock(&Giant);
167	va_start(ap, fmt);
168	printf(fmt, ap);
169	va_end(ap);
170	mtx_unlock(&Giant);
171}
172
173#ifdef NFS_ACDEBUG
174#include <sys/sysctl.h>
175SYSCTL_DECL(_vfs_newnfs);
176static int nfs_acdebug;
177SYSCTL_INT(_vfs_newnfs, OID_AUTO, acdebug, CTLFLAG_RW, &nfs_acdebug, 0, "");
178#endif
179
180/*
181 * Check the time stamp
182 * If the cache is valid, copy contents to *vap and return 0
183 * otherwise return an error
184 */
185int
186ncl_getattrcache(struct vnode *vp, struct vattr *vaper)
187{
188	struct nfsnode *np;
189	struct vattr *vap;
190	struct nfsmount *nmp;
191	int timeo;
192	boolean_t mustflush;
193
194	np = VTONFS(vp);
195	vap = &np->n_vattr.na_vattr;
196	nmp = VFSTONFS(vp->v_mount);
197	mustflush = nfscl_mustflush(vp);	/* must be before mtx_lock() */
198#ifdef NFS_ACDEBUG
199	mtx_lock(&Giant);	/* ncl_printf() */
200#endif
201	mtx_lock(&np->n_mtx);
202	/* XXX n_mtime doesn't seem to be updated on a miss-and-reload */
203	timeo = (time_second - np->n_mtime.tv_sec) / 10;
204
205#ifdef NFS_ACDEBUG
206	if (nfs_acdebug>1)
207		ncl_printf("nfs_getattrcache: initial timeo = %d\n", timeo);
208#endif
209
210	if (vap->va_type == VDIR) {
211		if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acdirmin)
212			timeo = nmp->nm_acdirmin;
213		else if (timeo > nmp->nm_acdirmax)
214			timeo = nmp->nm_acdirmax;
215	} else {
216		if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acregmin)
217			timeo = nmp->nm_acregmin;
218		else if (timeo > nmp->nm_acregmax)
219			timeo = nmp->nm_acregmax;
220	}
221
222#ifdef NFS_ACDEBUG
223	if (nfs_acdebug > 2)
224		ncl_printf("acregmin %d; acregmax %d; acdirmin %d; acdirmax %d\n",
225			   nmp->nm_acregmin, nmp->nm_acregmax,
226			   nmp->nm_acdirmin, nmp->nm_acdirmax);
227
228	if (nfs_acdebug)
229		ncl_printf("nfs_getattrcache: age = %d; final timeo = %d\n",
230			   (time_second - np->n_attrstamp), timeo);
231#endif
232
233	if ((time_second - np->n_attrstamp) >= timeo &&
234	    (mustflush || np->n_attrstamp == 0)) {
235		newnfsstats.attrcache_misses++;
236		mtx_unlock(&np->n_mtx);
237#ifdef NFS_ACDEBUG
238		mtx_unlock(&Giant);	/* ncl_printf() */
239#endif
240		return( ENOENT);
241	}
242	newnfsstats.attrcache_hits++;
243	if (vap->va_size != np->n_size) {
244		if (vap->va_type == VREG) {
245			if (np->n_flag & NMODIFIED) {
246				if (vap->va_size < np->n_size)
247					vap->va_size = np->n_size;
248				else
249					np->n_size = vap->va_size;
250			} else {
251				np->n_size = vap->va_size;
252			}
253			vnode_pager_setsize(vp, np->n_size);
254		} else {
255			np->n_size = vap->va_size;
256		}
257	}
258	bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr));
259	if (np->n_flag & NCHG) {
260		if (np->n_flag & NACC)
261			vaper->va_atime = np->n_atim;
262		if (np->n_flag & NUPD)
263			vaper->va_mtime = np->n_mtim;
264	}
265	mtx_unlock(&np->n_mtx);
266#ifdef NFS_ACDEBUG
267	mtx_unlock(&Giant);	/* ncl_printf() */
268#endif
269	return (0);
270}
271
272static nfsuint64 nfs_nullcookie = { { 0, 0 } };
273/*
274 * This function finds the directory cookie that corresponds to the
275 * logical byte offset given.
276 */
277nfsuint64 *
278ncl_getcookie(struct nfsnode *np, off_t off, int add)
279{
280	struct nfsdmap *dp, *dp2;
281	int pos;
282	nfsuint64 *retval = NULL;
283
284	pos = (uoff_t)off / NFS_DIRBLKSIZ;
285	if (pos == 0 || off < 0) {
286		KASSERT(!add, ("nfs getcookie add at <= 0"));
287		return (&nfs_nullcookie);
288	}
289	pos--;
290	dp = LIST_FIRST(&np->n_cookies);
291	if (!dp) {
292		if (add) {
293			MALLOC(dp, struct nfsdmap *, sizeof (struct nfsdmap),
294				M_NFSDIROFF, M_WAITOK);
295			dp->ndm_eocookie = 0;
296			LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list);
297		} else
298			goto out;
299	}
300	while (pos >= NFSNUMCOOKIES) {
301		pos -= NFSNUMCOOKIES;
302		if (LIST_NEXT(dp, ndm_list)) {
303			if (!add && dp->ndm_eocookie < NFSNUMCOOKIES &&
304			    pos >= dp->ndm_eocookie)
305				goto out;
306			dp = LIST_NEXT(dp, ndm_list);
307		} else if (add) {
308			MALLOC(dp2, struct nfsdmap *, sizeof (struct nfsdmap),
309				M_NFSDIROFF, M_WAITOK);
310			dp2->ndm_eocookie = 0;
311			LIST_INSERT_AFTER(dp, dp2, ndm_list);
312			dp = dp2;
313		} else
314			goto out;
315	}
316	if (pos >= dp->ndm_eocookie) {
317		if (add)
318			dp->ndm_eocookie = pos + 1;
319		else
320			goto out;
321	}
322	retval = &dp->ndm_cookies[pos];
323out:
324	return (retval);
325}
326
327/*
328 * Invalidate cached directory information, except for the actual directory
329 * blocks (which are invalidated separately).
330 * Done mainly to avoid the use of stale offset cookies.
331 */
332void
333ncl_invaldir(struct vnode *vp)
334{
335	struct nfsnode *np = VTONFS(vp);
336
337	KASSERT(vp->v_type == VDIR, ("nfs: invaldir not dir"));
338	ncl_dircookie_lock(np);
339	np->n_direofoffset = 0;
340	np->n_cookieverf.nfsuquad[0] = 0;
341	np->n_cookieverf.nfsuquad[1] = 0;
342	if (LIST_FIRST(&np->n_cookies))
343		LIST_FIRST(&np->n_cookies)->ndm_eocookie = 0;
344	ncl_dircookie_unlock(np);
345}
346
347/*
348 * The write verifier has changed (probably due to a server reboot), so all
349 * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
350 * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
351 * and B_CLUSTEROK flags.  Once done the new write verifier can be set for the
352 * mount point.
353 *
354 * B_CLUSTEROK must be cleared along with B_NEEDCOMMIT because stage 1 data
355 * writes are not clusterable.
356 */
357void
358ncl_clearcommit(struct mount *mp)
359{
360	struct vnode *vp, *nvp;
361	struct buf *bp, *nbp;
362	struct bufobj *bo;
363
364	MNT_ILOCK(mp);
365	MNT_VNODE_FOREACH(vp, mp, nvp) {
366		bo = &vp->v_bufobj;
367		VI_LOCK(vp);
368		if (vp->v_iflag & VI_DOOMED) {
369			VI_UNLOCK(vp);
370			continue;
371		}
372		vholdl(vp);
373		VI_UNLOCK(vp);
374		MNT_IUNLOCK(mp);
375		BO_LOCK(bo);
376		TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
377			if (!BUF_ISLOCKED(bp) &&
378			    (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
379				== (B_DELWRI | B_NEEDCOMMIT))
380				bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
381		}
382		BO_UNLOCK(bo);
383		vdrop(vp);
384		MNT_ILOCK(mp);
385	}
386	MNT_IUNLOCK(mp);
387}
388
389/*
390 * Called once to initialize data structures...
391 */
392int
393ncl_init(struct vfsconf *vfsp)
394{
395	int i;
396
397	/* Ensure async daemons disabled */
398	for (i = 0; i < NFS_MAXRAHEAD; i++) {
399		ncl_iodwant[i] = NFSIOD_NOT_AVAILABLE;
400		ncl_iodmount[i] = NULL;
401	}
402	ncl_nhinit();			/* Init the nfsnode table */
403
404	return (0);
405}
406
407