nfs_clsubs.c revision 1.1 1 1.1 dholland /* $NetBSD: nfs_clsubs.c,v 1.1 2013/09/30 07:18:58 dholland Exp $ */
2 1.1 dholland /*-
3 1.1 dholland * Copyright (c) 1989, 1993
4 1.1 dholland * The Regents of the University of California. All rights reserved.
5 1.1 dholland *
6 1.1 dholland * This code is derived from software contributed to Berkeley by
7 1.1 dholland * Rick Macklem at The University of Guelph.
8 1.1 dholland *
9 1.1 dholland * Redistribution and use in source and binary forms, with or without
10 1.1 dholland * modification, are permitted provided that the following conditions
11 1.1 dholland * are met:
12 1.1 dholland * 1. Redistributions of source code must retain the above copyright
13 1.1 dholland * notice, this list of conditions and the following disclaimer.
14 1.1 dholland * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 dholland * notice, this list of conditions and the following disclaimer in the
16 1.1 dholland * documentation and/or other materials provided with the distribution.
17 1.1 dholland * 4. Neither the name of the University nor the names of its contributors
18 1.1 dholland * may be used to endorse or promote products derived from this software
19 1.1 dholland * without specific prior written permission.
20 1.1 dholland *
21 1.1 dholland * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 dholland * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 dholland * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 dholland * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 dholland * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 dholland * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 dholland * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 dholland * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 dholland * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 dholland * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 dholland * SUCH DAMAGE.
32 1.1 dholland *
33 1.1 dholland * from nfs_subs.c 8.8 (Berkeley) 5/22/95
34 1.1 dholland */
35 1.1 dholland
36 1.1 dholland #include <sys/cdefs.h>
37 1.1 dholland /* __FBSDID("FreeBSD: head/sys/fs/nfsclient/nfs_clsubs.c 234386 2012-04-17 16:28:22Z mckusick "); */
38 1.1 dholland __RCSID("$NetBSD: nfs_clsubs.c,v 1.1 2013/09/30 07:18:58 dholland Exp $");
39 1.1 dholland
40 1.1 dholland #include "opt_kdtrace.h"
41 1.1 dholland
42 1.1 dholland /*
43 1.1 dholland * These functions support the macros and help fiddle mbuf chains for
44 1.1 dholland * the nfs op functions. They do things like create the rpc header and
45 1.1 dholland * copy data between mbuf chains and uio lists.
46 1.1 dholland */
47 1.1 dholland
48 1.1 dholland #include <sys/param.h>
49 1.1 dholland #include <sys/systm.h>
50 1.1 dholland #include <sys/kernel.h>
51 1.1 dholland #include <sys/bio.h>
52 1.1 dholland #include <sys/buf.h>
53 1.1 dholland #include <sys/proc.h>
54 1.1 dholland #include <sys/mount.h>
55 1.1 dholland #include <sys/vnode.h>
56 1.1 dholland #include <sys/namei.h>
57 1.1 dholland #include <sys/mbuf.h>
58 1.1 dholland #include <sys/socket.h>
59 1.1 dholland #include <sys/stat.h>
60 1.1 dholland #include <sys/malloc.h>
61 1.1 dholland #include <sys/sysent.h>
62 1.1 dholland #include <sys/syscall.h>
63 1.1 dholland #include <sys/sysproto.h>
64 1.1 dholland #include <sys/taskqueue.h>
65 1.1 dholland
66 1.1 dholland #include <vm/vm.h>
67 1.1 dholland #include <vm/vm_object.h>
68 1.1 dholland #include <vm/vm_extern.h>
69 1.1 dholland #include <vm/uma.h>
70 1.1 dholland
71 1.1 dholland #include <fs/nfs/nfsport.h>
72 1.1 dholland #include <fs/nfsclient/nfsnode.h>
73 1.1 dholland #include <fs/nfsclient/nfsmount.h>
74 1.1 dholland #include <fs/nfsclient/nfs.h>
75 1.1 dholland #include <fs/nfsclient/nfs_kdtrace.h>
76 1.1 dholland
77 1.1 dholland #include <netinet/in.h>
78 1.1 dholland
79 1.1 dholland /*
80 1.1 dholland * Note that stdarg.h and the ANSI style va_start macro is used for both
81 1.1 dholland * ANSI and traditional C compilers.
82 1.1 dholland */
83 1.1 dholland #include <machine/stdarg.h>
84 1.1 dholland
85 1.1 dholland extern struct mtx ncl_iod_mutex;
86 1.1 dholland extern enum nfsiod_state ncl_iodwant[NFS_MAXASYNCDAEMON];
87 1.1 dholland extern struct nfsmount *ncl_iodmount[NFS_MAXASYNCDAEMON];
88 1.1 dholland extern int ncl_numasync;
89 1.1 dholland extern unsigned int ncl_iodmax;
90 1.1 dholland extern struct nfsstats newnfsstats;
91 1.1 dholland
92 1.1 dholland struct task ncl_nfsiodnew_task;
93 1.1 dholland
94 1.1 dholland int
95 1.1 dholland ncl_uninit(struct vfsconf *vfsp)
96 1.1 dholland {
97 1.1 dholland /*
98 1.1 dholland * XXX: Unloading of nfscl module is unsupported.
99 1.1 dholland */
100 1.1 dholland #if 0
101 1.1 dholland int i;
102 1.1 dholland
103 1.1 dholland /*
104 1.1 dholland * Tell all nfsiod processes to exit. Clear ncl_iodmax, and wakeup
105 1.1 dholland * any sleeping nfsiods so they check ncl_iodmax and exit.
106 1.1 dholland */
107 1.1 dholland mtx_lock(&ncl_iod_mutex);
108 1.1 dholland ncl_iodmax = 0;
109 1.1 dholland for (i = 0; i < ncl_numasync; i++)
110 1.1 dholland if (ncl_iodwant[i] == NFSIOD_AVAILABLE)
111 1.1 dholland wakeup(&ncl_iodwant[i]);
112 1.1 dholland /* The last nfsiod to exit will wake us up when ncl_numasync hits 0 */
113 1.1 dholland while (ncl_numasync)
114 1.1 dholland msleep(&ncl_numasync, &ncl_iod_mutex, PWAIT, "ioddie", 0);
115 1.1 dholland mtx_unlock(&ncl_iod_mutex);
116 1.1 dholland ncl_nhuninit();
117 1.1 dholland return (0);
118 1.1 dholland #else
119 1.1 dholland return (EOPNOTSUPP);
120 1.1 dholland #endif
121 1.1 dholland }
122 1.1 dholland
123 1.1 dholland void
124 1.1 dholland ncl_dircookie_lock(struct nfsnode *np)
125 1.1 dholland {
126 1.1 dholland mtx_lock(&np->n_mtx);
127 1.1 dholland while (np->n_flag & NDIRCOOKIELK)
128 1.1 dholland (void) msleep(&np->n_flag, &np->n_mtx, PZERO, "nfsdirlk", 0);
129 1.1 dholland np->n_flag |= NDIRCOOKIELK;
130 1.1 dholland mtx_unlock(&np->n_mtx);
131 1.1 dholland }
132 1.1 dholland
133 1.1 dholland void
134 1.1 dholland ncl_dircookie_unlock(struct nfsnode *np)
135 1.1 dholland {
136 1.1 dholland mtx_lock(&np->n_mtx);
137 1.1 dholland np->n_flag &= ~NDIRCOOKIELK;
138 1.1 dholland wakeup(&np->n_flag);
139 1.1 dholland mtx_unlock(&np->n_mtx);
140 1.1 dholland }
141 1.1 dholland
142 1.1 dholland int
143 1.1 dholland ncl_upgrade_vnlock(struct vnode *vp)
144 1.1 dholland {
145 1.1 dholland int old_lock;
146 1.1 dholland
147 1.1 dholland ASSERT_VOP_LOCKED(vp, "ncl_upgrade_vnlock");
148 1.1 dholland old_lock = NFSVOPISLOCKED(vp);
149 1.1 dholland if (old_lock != LK_EXCLUSIVE) {
150 1.1 dholland KASSERT(old_lock == LK_SHARED,
151 1.1 dholland ("ncl_upgrade_vnlock: wrong old_lock %d", old_lock));
152 1.1 dholland /* Upgrade to exclusive lock, this might block */
153 1.1 dholland NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
154 1.1 dholland }
155 1.1 dholland return (old_lock);
156 1.1 dholland }
157 1.1 dholland
158 1.1 dholland void
159 1.1 dholland ncl_downgrade_vnlock(struct vnode *vp, int old_lock)
160 1.1 dholland {
161 1.1 dholland if (old_lock != LK_EXCLUSIVE) {
162 1.1 dholland KASSERT(old_lock == LK_SHARED, ("wrong old_lock %d", old_lock));
163 1.1 dholland /* Downgrade from exclusive lock. */
164 1.1 dholland NFSVOPLOCK(vp, LK_DOWNGRADE | LK_RETRY);
165 1.1 dholland }
166 1.1 dholland }
167 1.1 dholland
168 1.1 dholland void
169 1.1 dholland ncl_printf(const char *fmt, ...)
170 1.1 dholland {
171 1.1 dholland va_list ap;
172 1.1 dholland
173 1.1 dholland mtx_lock(&Giant);
174 1.1 dholland va_start(ap, fmt);
175 1.1 dholland vprintf(fmt, ap);
176 1.1 dholland va_end(ap);
177 1.1 dholland mtx_unlock(&Giant);
178 1.1 dholland }
179 1.1 dholland
180 1.1 dholland #ifdef NFS_ACDEBUG
181 1.1 dholland #include <sys/sysctl.h>
182 1.1 dholland SYSCTL_DECL(_vfs_nfs);
183 1.1 dholland static int nfs_acdebug;
184 1.1 dholland SYSCTL_INT(_vfs_nfs, OID_AUTO, acdebug, CTLFLAG_RW, &nfs_acdebug, 0, "");
185 1.1 dholland #endif
186 1.1 dholland
187 1.1 dholland /*
188 1.1 dholland * Check the time stamp
189 1.1 dholland * If the cache is valid, copy contents to *vap and return 0
190 1.1 dholland * otherwise return an error
191 1.1 dholland */
192 1.1 dholland int
193 1.1 dholland ncl_getattrcache(struct vnode *vp, struct vattr *vaper)
194 1.1 dholland {
195 1.1 dholland struct nfsnode *np;
196 1.1 dholland struct vattr *vap;
197 1.1 dholland struct nfsmount *nmp;
198 1.1 dholland int timeo, mustflush;
199 1.1 dholland
200 1.1 dholland np = VTONFS(vp);
201 1.1 dholland vap = &np->n_vattr.na_vattr;
202 1.1 dholland nmp = VFSTONFS(vp->v_mount);
203 1.1 dholland mustflush = nfscl_mustflush(vp); /* must be before mtx_lock() */
204 1.1 dholland #ifdef NFS_ACDEBUG
205 1.1 dholland mtx_lock(&Giant); /* ncl_printf() */
206 1.1 dholland #endif
207 1.1 dholland mtx_lock(&np->n_mtx);
208 1.1 dholland /* XXX n_mtime doesn't seem to be updated on a miss-and-reload */
209 1.1 dholland timeo = (time_second - np->n_mtime.tv_sec) / 10;
210 1.1 dholland
211 1.1 dholland #ifdef NFS_ACDEBUG
212 1.1 dholland if (nfs_acdebug>1)
213 1.1 dholland ncl_printf("nfs_getattrcache: initial timeo = %d\n", timeo);
214 1.1 dholland #endif
215 1.1 dholland
216 1.1 dholland if (vap->va_type == VDIR) {
217 1.1 dholland if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acdirmin)
218 1.1 dholland timeo = nmp->nm_acdirmin;
219 1.1 dholland else if (timeo > nmp->nm_acdirmax)
220 1.1 dholland timeo = nmp->nm_acdirmax;
221 1.1 dholland } else {
222 1.1 dholland if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acregmin)
223 1.1 dholland timeo = nmp->nm_acregmin;
224 1.1 dholland else if (timeo > nmp->nm_acregmax)
225 1.1 dholland timeo = nmp->nm_acregmax;
226 1.1 dholland }
227 1.1 dholland
228 1.1 dholland #ifdef NFS_ACDEBUG
229 1.1 dholland if (nfs_acdebug > 2)
230 1.1 dholland ncl_printf("acregmin %d; acregmax %d; acdirmin %d; acdirmax %d\n",
231 1.1 dholland nmp->nm_acregmin, nmp->nm_acregmax,
232 1.1 dholland nmp->nm_acdirmin, nmp->nm_acdirmax);
233 1.1 dholland
234 1.1 dholland if (nfs_acdebug)
235 1.1 dholland ncl_printf("nfs_getattrcache: age = %d; final timeo = %d\n",
236 1.1 dholland (time_second - np->n_attrstamp), timeo);
237 1.1 dholland #endif
238 1.1 dholland
239 1.1 dholland if ((time_second - np->n_attrstamp) >= timeo &&
240 1.1 dholland (mustflush != 0 || np->n_attrstamp == 0)) {
241 1.1 dholland newnfsstats.attrcache_misses++;
242 1.1 dholland mtx_unlock(&np->n_mtx);
243 1.1 dholland #ifdef NFS_ACDEBUG
244 1.1 dholland mtx_unlock(&Giant); /* ncl_printf() */
245 1.1 dholland #endif
246 1.1 dholland KDTRACE_NFS_ATTRCACHE_GET_MISS(vp);
247 1.1 dholland return( ENOENT);
248 1.1 dholland }
249 1.1 dholland newnfsstats.attrcache_hits++;
250 1.1 dholland if (vap->va_size != np->n_size) {
251 1.1 dholland if (vap->va_type == VREG) {
252 1.1 dholland if (np->n_flag & NMODIFIED) {
253 1.1 dholland if (vap->va_size < np->n_size)
254 1.1 dholland vap->va_size = np->n_size;
255 1.1 dholland else
256 1.1 dholland np->n_size = vap->va_size;
257 1.1 dholland } else {
258 1.1 dholland np->n_size = vap->va_size;
259 1.1 dholland }
260 1.1 dholland vnode_pager_setsize(vp, np->n_size);
261 1.1 dholland } else {
262 1.1 dholland np->n_size = vap->va_size;
263 1.1 dholland }
264 1.1 dholland }
265 1.1 dholland bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr));
266 1.1 dholland if (np->n_flag & NCHG) {
267 1.1 dholland if (np->n_flag & NACC)
268 1.1 dholland vaper->va_atime = np->n_atim;
269 1.1 dholland if (np->n_flag & NUPD)
270 1.1 dholland vaper->va_mtime = np->n_mtim;
271 1.1 dholland }
272 1.1 dholland mtx_unlock(&np->n_mtx);
273 1.1 dholland #ifdef NFS_ACDEBUG
274 1.1 dholland mtx_unlock(&Giant); /* ncl_printf() */
275 1.1 dholland #endif
276 1.1 dholland KDTRACE_NFS_ATTRCACHE_GET_HIT(vp, vap);
277 1.1 dholland return (0);
278 1.1 dholland }
279 1.1 dholland
280 1.1 dholland static nfsuint64 nfs_nullcookie = { { 0, 0 } };
281 1.1 dholland /*
282 1.1 dholland * This function finds the directory cookie that corresponds to the
283 1.1 dholland * logical byte offset given.
284 1.1 dholland */
285 1.1 dholland nfsuint64 *
286 1.1 dholland ncl_getcookie(struct nfsnode *np, off_t off, int add)
287 1.1 dholland {
288 1.1 dholland struct nfsdmap *dp, *dp2;
289 1.1 dholland int pos;
290 1.1 dholland nfsuint64 *retval = NULL;
291 1.1 dholland
292 1.1 dholland pos = (uoff_t)off / NFS_DIRBLKSIZ;
293 1.1 dholland if (pos == 0 || off < 0) {
294 1.1 dholland KASSERT(!add, ("nfs getcookie add at <= 0"));
295 1.1 dholland return (&nfs_nullcookie);
296 1.1 dholland }
297 1.1 dholland pos--;
298 1.1 dholland dp = LIST_FIRST(&np->n_cookies);
299 1.1 dholland if (!dp) {
300 1.1 dholland if (add) {
301 1.1 dholland MALLOC(dp, struct nfsdmap *, sizeof (struct nfsdmap),
302 1.1 dholland M_NFSDIROFF, M_WAITOK);
303 1.1 dholland dp->ndm_eocookie = 0;
304 1.1 dholland LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list);
305 1.1 dholland } else
306 1.1 dholland goto out;
307 1.1 dholland }
308 1.1 dholland while (pos >= NFSNUMCOOKIES) {
309 1.1 dholland pos -= NFSNUMCOOKIES;
310 1.1 dholland if (LIST_NEXT(dp, ndm_list)) {
311 1.1 dholland if (!add && dp->ndm_eocookie < NFSNUMCOOKIES &&
312 1.1 dholland pos >= dp->ndm_eocookie)
313 1.1 dholland goto out;
314 1.1 dholland dp = LIST_NEXT(dp, ndm_list);
315 1.1 dholland } else if (add) {
316 1.1 dholland MALLOC(dp2, struct nfsdmap *, sizeof (struct nfsdmap),
317 1.1 dholland M_NFSDIROFF, M_WAITOK);
318 1.1 dholland dp2->ndm_eocookie = 0;
319 1.1 dholland LIST_INSERT_AFTER(dp, dp2, ndm_list);
320 1.1 dholland dp = dp2;
321 1.1 dholland } else
322 1.1 dholland goto out;
323 1.1 dholland }
324 1.1 dholland if (pos >= dp->ndm_eocookie) {
325 1.1 dholland if (add)
326 1.1 dholland dp->ndm_eocookie = pos + 1;
327 1.1 dholland else
328 1.1 dholland goto out;
329 1.1 dholland }
330 1.1 dholland retval = &dp->ndm_cookies[pos];
331 1.1 dholland out:
332 1.1 dholland return (retval);
333 1.1 dholland }
334 1.1 dholland
335 1.1 dholland /*
336 1.1 dholland * Invalidate cached directory information, except for the actual directory
337 1.1 dholland * blocks (which are invalidated separately).
338 1.1 dholland * Done mainly to avoid the use of stale offset cookies.
339 1.1 dholland */
340 1.1 dholland void
341 1.1 dholland ncl_invaldir(struct vnode *vp)
342 1.1 dholland {
343 1.1 dholland struct nfsnode *np = VTONFS(vp);
344 1.1 dholland
345 1.1 dholland KASSERT(vp->v_type == VDIR, ("nfs: invaldir not dir"));
346 1.1 dholland ncl_dircookie_lock(np);
347 1.1 dholland np->n_direofoffset = 0;
348 1.1 dholland np->n_cookieverf.nfsuquad[0] = 0;
349 1.1 dholland np->n_cookieverf.nfsuquad[1] = 0;
350 1.1 dholland if (LIST_FIRST(&np->n_cookies))
351 1.1 dholland LIST_FIRST(&np->n_cookies)->ndm_eocookie = 0;
352 1.1 dholland ncl_dircookie_unlock(np);
353 1.1 dholland }
354 1.1 dholland
355 1.1 dholland /*
356 1.1 dholland * The write verifier has changed (probably due to a server reboot), so all
357 1.1 dholland * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
358 1.1 dholland * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
359 1.1 dholland * and B_CLUSTEROK flags. Once done the new write verifier can be set for the
360 1.1 dholland * mount point.
361 1.1 dholland *
362 1.1 dholland * B_CLUSTEROK must be cleared along with B_NEEDCOMMIT because stage 1 data
363 1.1 dholland * writes are not clusterable.
364 1.1 dholland */
365 1.1 dholland void
366 1.1 dholland ncl_clearcommit(struct mount *mp)
367 1.1 dholland {
368 1.1 dholland struct vnode *vp, *nvp;
369 1.1 dholland struct buf *bp, *nbp;
370 1.1 dholland struct bufobj *bo;
371 1.1 dholland
372 1.1 dholland MNT_VNODE_FOREACH_ALL(vp, mp, nvp) {
373 1.1 dholland bo = &vp->v_bufobj;
374 1.1 dholland vholdl(vp);
375 1.1 dholland VI_UNLOCK(vp);
376 1.1 dholland BO_LOCK(bo);
377 1.1 dholland TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
378 1.1 dholland if (!BUF_ISLOCKED(bp) &&
379 1.1 dholland (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
380 1.1 dholland == (B_DELWRI | B_NEEDCOMMIT))
381 1.1 dholland bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
382 1.1 dholland }
383 1.1 dholland BO_UNLOCK(bo);
384 1.1 dholland vdrop(vp);
385 1.1 dholland }
386 1.1 dholland }
387 1.1 dholland
388 1.1 dholland /*
389 1.1 dholland * Called once to initialize data structures...
390 1.1 dholland */
391 1.1 dholland int
392 1.1 dholland ncl_init(struct vfsconf *vfsp)
393 1.1 dholland {
394 1.1 dholland int i;
395 1.1 dholland
396 1.1 dholland /* Ensure async daemons disabled */
397 1.1 dholland for (i = 0; i < NFS_MAXASYNCDAEMON; i++) {
398 1.1 dholland ncl_iodwant[i] = NFSIOD_NOT_AVAILABLE;
399 1.1 dholland ncl_iodmount[i] = NULL;
400 1.1 dholland }
401 1.1 dholland TASK_INIT(&ncl_nfsiodnew_task, 0, ncl_nfsiodnew_tq, NULL);
402 1.1 dholland ncl_nhinit(); /* Init the nfsnode table */
403 1.1 dholland
404 1.1 dholland return (0);
405 1.1 dholland }
406 1.1 dholland
407