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