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