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