/
nfs_iod.c
444 lines (404 loc) · 11.9 KB
/
nfs_iod.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/* $NetBSD: nfs_iod.c,v 1.4 2009/12/31 19:38:16 christos Exp $ */
/*
* Copyright (c) 1989, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Rick Macklem at The University of Guelph.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)nfs_syscalls.c 8.5 (Berkeley) 3/30/95
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: nfs_iod.c,v 1.4 2009/12/31 19:38:16 christos Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/vnode.h>
#include <sys/mount.h>
#include <sys/proc.h>
#include <sys/uio.h>
#include <sys/malloc.h>
#include <sys/kmem.h>
#include <sys/buf.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/signalvar.h>
#include <sys/domain.h>
#include <sys/protosw.h>
#include <sys/namei.h>
#include <sys/syslog.h>
#include <sys/filedesc.h>
#include <sys/kthread.h>
#include <sys/kauth.h>
#include <sys/syscallargs.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <nfs/xdr_subs.h>
#include <nfs/rpcv2.h>
#include <nfs/nfsproto.h>
#include <nfs/nfs.h>
#include <nfs/nfsm_subs.h>
#include <nfs/nfsrvcache.h>
#include <nfs/nfsmount.h>
#include <nfs/nfsnode.h>
#include <nfs/nfsrtt.h>
#include <nfs/nfs_var.h>
extern int nuidhash_max;
/*
* locking order:
* nfs_iodlist_lock -> nid_lock -> nm_lock
*/
kmutex_t nfs_iodlist_lock;
struct nfs_iodlist nfs_iodlist_idle;
struct nfs_iodlist nfs_iodlist_all;
int nfs_niothreads = -1; /* == "0, and has never been set" */
int nfs_defect = 0;
/*
* Asynchronous I/O threads for client nfs.
* They do read-ahead and write-behind operations on the block I/O cache.
* Never returns unless it fails or gets killed.
*/
static void
nfssvc_iod(void *arg)
{
struct buf *bp;
struct nfs_iod *myiod;
struct nfsmount *nmp;
myiod = kmem_alloc(sizeof(*myiod), KM_SLEEP);
mutex_init(&myiod->nid_lock, MUTEX_DEFAULT, IPL_NONE);
cv_init(&myiod->nid_cv, "nfsiod");
myiod->nid_exiting = false;
myiod->nid_mount = NULL;
mutex_enter(&nfs_iodlist_lock);
LIST_INSERT_HEAD(&nfs_iodlist_all, myiod, nid_all);
mutex_exit(&nfs_iodlist_lock);
for (;;) {
mutex_enter(&nfs_iodlist_lock);
LIST_INSERT_HEAD(&nfs_iodlist_idle, myiod, nid_idle);
mutex_exit(&nfs_iodlist_lock);
mutex_enter(&myiod->nid_lock);
while (/*CONSTCOND*/ true) {
nmp = myiod->nid_mount;
if (nmp) {
myiod->nid_mount = NULL;
break;
}
if (__predict_false(myiod->nid_exiting)) {
/*
* drop nid_lock to preserve locking order.
*/
mutex_exit(&myiod->nid_lock);
mutex_enter(&nfs_iodlist_lock);
mutex_enter(&myiod->nid_lock);
/*
* recheck nid_mount because nfs_asyncio can
* pick us in the meantime as we are still on
* nfs_iodlist_lock.
*/
if (myiod->nid_mount != NULL) {
mutex_exit(&nfs_iodlist_lock);
continue;
}
LIST_REMOVE(myiod, nid_idle);
mutex_exit(&nfs_iodlist_lock);
goto quit;
}
cv_wait(&myiod->nid_cv, &myiod->nid_lock);
}
mutex_exit(&myiod->nid_lock);
mutex_enter(&nmp->nm_lock);
while ((bp = TAILQ_FIRST(&nmp->nm_bufq)) != NULL) {
/* Take one off the front of the list */
TAILQ_REMOVE(&nmp->nm_bufq, bp, b_freelist);
nmp->nm_bufqlen--;
if (nmp->nm_bufqlen < 2 * nmp->nm_bufqiods) {
cv_broadcast(&nmp->nm_aiocv);
}
mutex_exit(&nmp->nm_lock);
KERNEL_LOCK(1, curlwp);
(void)nfs_doio(bp);
KERNEL_UNLOCK_LAST(curlwp);
mutex_enter(&nmp->nm_lock);
/*
* If there are more than one iod on this mount,
* then defect so that the iods can be shared out
* fairly between the mounts
*/
if (nfs_defect && nmp->nm_bufqiods > 1) {
break;
}
}
KASSERT(nmp->nm_bufqiods > 0);
nmp->nm_bufqiods--;
mutex_exit(&nmp->nm_lock);
}
quit:
KASSERT(myiod->nid_mount == NULL);
mutex_exit(&myiod->nid_lock);
cv_destroy(&myiod->nid_cv);
mutex_destroy(&myiod->nid_lock);
kmem_free(myiod, sizeof(*myiod));
kthread_exit(0);
}
void
nfs_iodinit(void)
{
mutex_init(&nfs_iodlist_lock, MUTEX_DEFAULT, IPL_NONE);
LIST_INIT(&nfs_iodlist_all);
LIST_INIT(&nfs_iodlist_idle);
}
void
nfs_iodfini(void)
{
int error;
error = nfs_set_niothreads(0);
KASSERT(error == 0);
mutex_destroy(&nfs_iodlist_lock);
}
int
nfs_set_niothreads(int newval)
{
struct nfs_iod *nid;
int error = 0;
int hold_count;
KERNEL_UNLOCK_ALL(curlwp, &hold_count);
mutex_enter(&nfs_iodlist_lock);
/* clamp to sane range */
nfs_niothreads = max(0, min(newval, NFS_MAXASYNCDAEMON));
while (nfs_numasync != nfs_niothreads && error == 0) {
while (nfs_numasync < nfs_niothreads) {
/*
* kthread_create can wait for pagedaemon and
* pagedaemon can wait for nfsiod which needs to acquire
* nfs_iodlist_lock.
*/
mutex_exit(&nfs_iodlist_lock);
error = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
nfssvc_iod, NULL, NULL, "nfsio");
mutex_enter(&nfs_iodlist_lock);
if (error) {
/* give up */
nfs_niothreads = nfs_numasync;
break;
}
nfs_numasync++;
}
while (nfs_numasync > nfs_niothreads) {
nid = LIST_FIRST(&nfs_iodlist_all);
if (nid == NULL) {
/* iod has not started yet. */
kpause("nfsiorm", false, hz, &nfs_iodlist_lock);
continue;
}
LIST_REMOVE(nid, nid_all);
mutex_enter(&nid->nid_lock);
KASSERT(!nid->nid_exiting);
nid->nid_exiting = true;
cv_signal(&nid->nid_cv);
mutex_exit(&nid->nid_lock);
nfs_numasync--;
}
}
mutex_exit(&nfs_iodlist_lock);
KERNEL_LOCK(hold_count, curlwp);
return error;
}
/*
* Get an authorization string for the uid by having the mount_nfs sitting
* on this mount point porpous out of the kernel and do it.
*/
int
nfs_getauth(struct nfsmount *nmp, struct nfsreq *rep, kauth_cred_t cred, char **auth_str, int *auth_len, char *verf_str, int *verf_len, NFSKERBKEY_T key)
/* key: return session key */
{
int error = 0;
while ((nmp->nm_iflag & NFSMNT_WAITAUTH) == 0) {
nmp->nm_iflag |= NFSMNT_WANTAUTH;
(void) tsleep((void *)&nmp->nm_authtype, PSOCK,
"nfsauth1", 2 * hz);
error = nfs_sigintr(nmp, rep, rep->r_lwp);
if (error) {
nmp->nm_iflag &= ~NFSMNT_WANTAUTH;
return (error);
}
}
nmp->nm_iflag &= ~(NFSMNT_WAITAUTH | NFSMNT_WANTAUTH);
nmp->nm_authstr = *auth_str = (char *)malloc(RPCAUTH_MAXSIZ, M_TEMP, M_WAITOK);
nmp->nm_authlen = RPCAUTH_MAXSIZ;
nmp->nm_verfstr = verf_str;
nmp->nm_verflen = *verf_len;
nmp->nm_authuid = kauth_cred_geteuid(cred);
wakeup((void *)&nmp->nm_authstr);
/*
* And wait for mount_nfs to do its stuff.
*/
while ((nmp->nm_iflag & NFSMNT_HASAUTH) == 0 && error == 0) {
(void) tsleep((void *)&nmp->nm_authlen, PSOCK,
"nfsauth2", 2 * hz);
error = nfs_sigintr(nmp, rep, rep->r_lwp);
}
if (nmp->nm_iflag & NFSMNT_AUTHERR) {
nmp->nm_iflag &= ~NFSMNT_AUTHERR;
error = EAUTH;
}
if (error)
free((void *)*auth_str, M_TEMP);
else {
*auth_len = nmp->nm_authlen;
*verf_len = nmp->nm_verflen;
memcpy(key, nmp->nm_key, sizeof (NFSKERBKEY_T));
}
nmp->nm_iflag &= ~NFSMNT_HASAUTH;
nmp->nm_iflag |= NFSMNT_WAITAUTH;
if (nmp->nm_iflag & NFSMNT_WANTAUTH) {
nmp->nm_iflag &= ~NFSMNT_WANTAUTH;
wakeup((void *)&nmp->nm_authtype);
}
return (error);
}
/*
* Get a nickname authenticator and verifier.
*/
int
nfs_getnickauth(struct nfsmount *nmp, kauth_cred_t cred, char **auth_str,
int *auth_len, char *verf_str, int verf_len)
{
struct timeval ktvin, ktvout, tv;
struct nfsuid *nuidp;
u_int32_t *nickp, *verfp;
memset(&ktvout, 0, sizeof ktvout); /* XXX gcc */
#ifdef DIAGNOSTIC
if (verf_len < (4 * NFSX_UNSIGNED))
panic("nfs_getnickauth verf too small");
#endif
LIST_FOREACH(nuidp, NMUIDHASH(nmp, kauth_cred_geteuid(cred)), nu_hash) {
if (kauth_cred_geteuid(nuidp->nu_cr) == kauth_cred_geteuid(cred))
break;
}
if (!nuidp || nuidp->nu_expire < time_second)
return (EACCES);
/*
* Move to the end of the lru list (end of lru == most recently used).
*/
TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp, nu_lru);
TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp, nu_lru);
nickp = (u_int32_t *)malloc(2 * NFSX_UNSIGNED, M_TEMP, M_WAITOK);
*nickp++ = txdr_unsigned(RPCAKN_NICKNAME);
*nickp = txdr_unsigned(nuidp->nu_nickname);
*auth_str = (char *)nickp;
*auth_len = 2 * NFSX_UNSIGNED;
/*
* Now we must encrypt the verifier and package it up.
*/
verfp = (u_int32_t *)verf_str;
*verfp++ = txdr_unsigned(RPCAKN_NICKNAME);
getmicrotime(&tv);
if (tv.tv_sec > nuidp->nu_timestamp.tv_sec ||
(tv.tv_sec == nuidp->nu_timestamp.tv_sec &&
tv.tv_usec > nuidp->nu_timestamp.tv_usec))
nuidp->nu_timestamp = tv;
else
nuidp->nu_timestamp.tv_usec++;
ktvin.tv_sec = txdr_unsigned(nuidp->nu_timestamp.tv_sec);
ktvin.tv_usec = txdr_unsigned(nuidp->nu_timestamp.tv_usec);
/*
* Now encrypt the timestamp verifier in ecb mode using the session
* key.
*/
#ifdef NFSKERB
XXX
#endif
*verfp++ = ktvout.tv_sec;
*verfp++ = ktvout.tv_usec;
*verfp = 0;
return (0);
}
/*
* Save the current nickname in a hash list entry on the mount point.
*/
int
nfs_savenickauth(struct nfsmount *nmp, kauth_cred_t cred, int len, NFSKERBKEY_T key, struct mbuf **mdp, char **dposp, struct mbuf *mrep)
{
struct nfsuid *nuidp;
u_int32_t *tl;
int32_t t1;
struct mbuf *md = *mdp;
struct timeval ktvin, ktvout;
u_int32_t nick;
char *dpos = *dposp, *cp2;
int deltasec, error = 0;
memset(&ktvout, 0, sizeof ktvout); /* XXX gcc */
if (len == (3 * NFSX_UNSIGNED)) {
nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
ktvin.tv_sec = *tl++;
ktvin.tv_usec = *tl++;
nick = fxdr_unsigned(u_int32_t, *tl);
/*
* Decrypt the timestamp in ecb mode.
*/
#ifdef NFSKERB
XXX
#endif
ktvout.tv_sec = fxdr_unsigned(long, ktvout.tv_sec);
ktvout.tv_usec = fxdr_unsigned(long, ktvout.tv_usec);
deltasec = time_second - ktvout.tv_sec;
if (deltasec < 0)
deltasec = -deltasec;
/*
* If ok, add it to the hash list for the mount point.
*/
if (deltasec <= NFS_KERBCLOCKSKEW) {
if (nmp->nm_numuids < nuidhash_max) {
nmp->nm_numuids++;
nuidp = kmem_alloc(sizeof(*nuidp), KM_SLEEP);
} else {
nuidp = TAILQ_FIRST(&nmp->nm_uidlruhead);
LIST_REMOVE(nuidp, nu_hash);
TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp,
nu_lru);
}
nuidp->nu_flag = 0;
kauth_cred_seteuid(nuidp->nu_cr, kauth_cred_geteuid(cred));
nuidp->nu_expire = time_second + NFS_KERBTTL;
nuidp->nu_timestamp = ktvout;
nuidp->nu_nickname = nick;
memcpy(nuidp->nu_key, key, sizeof (NFSKERBKEY_T));
TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp,
nu_lru);
LIST_INSERT_HEAD(NMUIDHASH(nmp, kauth_cred_geteuid(cred)),
nuidp, nu_hash);
}
} else
nfsm_adv(nfsm_rndup(len));
nfsmout:
*mdp = md;
*dposp = dpos;
return (error);
}