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