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