Deleted Added
full compact
nfs_subs.c (36503) nfs_subs.c (36511)
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 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)nfs_subs.c 8.8 (Berkeley) 5/22/95
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 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)nfs_subs.c 8.8 (Berkeley) 5/22/95
37 * $Id: nfs_subs.c,v 1.55 1998/05/24 14:41:53 peter Exp $
37 * $Id: nfs_subs.c,v 1.56 1998/05/31 17:27:50 peter Exp $
38 */
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#include <sys/param.h>
46#include <sys/buf.h>
47#include <sys/proc.h>
48#include <sys/systm.h>
49#include <sys/kernel.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
60#include <vm/vm.h>
61#include <vm/vm_object.h>
62#include <vm/vm_extern.h>
63#include <vm/vm_zone.h>
64
65#include <nfs/rpcv2.h>
66#include <nfs/nfsproto.h>
67#include <nfs/nfs.h>
68#include <nfs/nfsnode.h>
69#include <nfs/xdr_subs.h>
70#include <nfs/nfsm_subs.h>
71#include <nfs/nfsmount.h>
72#include <nfs/nqnfs.h>
73#include <nfs/nfsrtt.h>
74
75#include <miscfs/specfs/specdev.h>
76
77#include <netinet/in.h>
78#ifdef ISO
79#include <netiso/iso.h>
80#endif
81
82/*
83 * Data items converted to xdr at startup, since they are constant
84 * This is kinda hokey, but may save a little time doing byte swaps
85 */
86u_long nfs_xdrneg1;
87u_long rpc_call, rpc_vers, rpc_reply, rpc_msgdenied, rpc_autherr,
88 rpc_mismatch, rpc_auth_unix, rpc_msgaccepted,
89 rpc_auth_kerb;
90u_long nfs_prog, nqnfs_prog, nfs_true, nfs_false;
91
92/* And other global data */
93static u_long nfs_xid = 0;
94static enum vtype nv2tov_type[8]= {
95 VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON, VNON
96};
97enum vtype nv3tov_type[8]= {
98 VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO
99};
100
101int nfs_mount_type;
102int nfs_ticks;
103
104struct nfs_reqq nfs_reqq;
105struct nfssvc_sockhead nfssvc_sockhead;
106int nfssvc_sockhead_flag;
107struct nfsd_head nfsd_head;
108int nfsd_head_flag;
109struct nfs_bufq nfs_bufq;
110struct nqtimerhead nqtimerhead;
111struct nqfhhashhead *nqfhhashtbl;
112u_long nqfhhash;
113
114#ifndef NFS_NOSERVER
115/*
116 * Mapping of old NFS Version 2 RPC numbers to generic numbers.
117 */
118int nfsv3_procid[NFS_NPROCS] = {
119 NFSPROC_NULL,
120 NFSPROC_GETATTR,
121 NFSPROC_SETATTR,
122 NFSPROC_NOOP,
123 NFSPROC_LOOKUP,
124 NFSPROC_READLINK,
125 NFSPROC_READ,
126 NFSPROC_NOOP,
127 NFSPROC_WRITE,
128 NFSPROC_CREATE,
129 NFSPROC_REMOVE,
130 NFSPROC_RENAME,
131 NFSPROC_LINK,
132 NFSPROC_SYMLINK,
133 NFSPROC_MKDIR,
134 NFSPROC_RMDIR,
135 NFSPROC_READDIR,
136 NFSPROC_FSSTAT,
137 NFSPROC_NOOP,
138 NFSPROC_NOOP,
139 NFSPROC_NOOP,
140 NFSPROC_NOOP,
141 NFSPROC_NOOP,
142 NFSPROC_NOOP,
143 NFSPROC_NOOP,
144 NFSPROC_NOOP
145};
146
147#endif /* NFS_NOSERVER */
148/*
149 * and the reverse mapping from generic to Version 2 procedure numbers
150 */
151int nfsv2_procid[NFS_NPROCS] = {
152 NFSV2PROC_NULL,
153 NFSV2PROC_GETATTR,
154 NFSV2PROC_SETATTR,
155 NFSV2PROC_LOOKUP,
156 NFSV2PROC_NOOP,
157 NFSV2PROC_READLINK,
158 NFSV2PROC_READ,
159 NFSV2PROC_WRITE,
160 NFSV2PROC_CREATE,
161 NFSV2PROC_MKDIR,
162 NFSV2PROC_SYMLINK,
163 NFSV2PROC_CREATE,
164 NFSV2PROC_REMOVE,
165 NFSV2PROC_RMDIR,
166 NFSV2PROC_RENAME,
167 NFSV2PROC_LINK,
168 NFSV2PROC_READDIR,
169 NFSV2PROC_NOOP,
170 NFSV2PROC_STATFS,
171 NFSV2PROC_NOOP,
172 NFSV2PROC_NOOP,
173 NFSV2PROC_NOOP,
174 NFSV2PROC_NOOP,
175 NFSV2PROC_NOOP,
176 NFSV2PROC_NOOP,
177 NFSV2PROC_NOOP,
178};
179
180#ifndef NFS_NOSERVER
181/*
182 * Maps errno values to nfs error numbers.
183 * Use NFSERR_IO as the catch all for ones not specifically defined in
184 * RFC 1094.
185 */
186static u_char nfsrv_v2errmap[ELAST] = {
187 NFSERR_PERM, NFSERR_NOENT, NFSERR_IO, NFSERR_IO, NFSERR_IO,
188 NFSERR_NXIO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
189 NFSERR_IO, NFSERR_IO, NFSERR_ACCES, NFSERR_IO, NFSERR_IO,
190 NFSERR_IO, NFSERR_EXIST, NFSERR_IO, NFSERR_NODEV, NFSERR_NOTDIR,
191 NFSERR_ISDIR, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
192 NFSERR_IO, NFSERR_FBIG, NFSERR_NOSPC, NFSERR_IO, NFSERR_ROFS,
193 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
194 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
195 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
196 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
197 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
198 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
199 NFSERR_IO, NFSERR_IO, NFSERR_NAMETOL, NFSERR_IO, NFSERR_IO,
200 NFSERR_NOTEMPTY, NFSERR_IO, NFSERR_IO, NFSERR_DQUOT, NFSERR_STALE,
201 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
202 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
203 NFSERR_IO,
204};
205
206/*
207 * Maps errno values to nfs error numbers.
208 * Although it is not obvious whether or not NFS clients really care if
209 * a returned error value is in the specified list for the procedure, the
210 * safest thing to do is filter them appropriately. For Version 2, the
211 * X/Open XNFS document is the only specification that defines error values
212 * for each RPC (The RFC simply lists all possible error values for all RPCs),
213 * so I have decided to not do this for Version 2.
214 * The first entry is the default error return and the rest are the valid
215 * errors for that RPC in increasing numeric order.
216 */
217static short nfsv3err_null[] = {
218 0,
219 0,
220};
221
222static short nfsv3err_getattr[] = {
223 NFSERR_IO,
224 NFSERR_IO,
225 NFSERR_STALE,
226 NFSERR_BADHANDLE,
227 NFSERR_SERVERFAULT,
228 0,
229};
230
231static short nfsv3err_setattr[] = {
232 NFSERR_IO,
233 NFSERR_PERM,
234 NFSERR_IO,
235 NFSERR_ACCES,
236 NFSERR_INVAL,
237 NFSERR_NOSPC,
238 NFSERR_ROFS,
239 NFSERR_DQUOT,
240 NFSERR_STALE,
241 NFSERR_BADHANDLE,
242 NFSERR_NOT_SYNC,
243 NFSERR_SERVERFAULT,
244 0,
245};
246
247static short nfsv3err_lookup[] = {
248 NFSERR_IO,
249 NFSERR_NOENT,
250 NFSERR_IO,
251 NFSERR_ACCES,
252 NFSERR_NOTDIR,
253 NFSERR_NAMETOL,
254 NFSERR_STALE,
255 NFSERR_BADHANDLE,
256 NFSERR_SERVERFAULT,
257 0,
258};
259
260static short nfsv3err_access[] = {
261 NFSERR_IO,
262 NFSERR_IO,
263 NFSERR_STALE,
264 NFSERR_BADHANDLE,
265 NFSERR_SERVERFAULT,
266 0,
267};
268
269static short nfsv3err_readlink[] = {
270 NFSERR_IO,
271 NFSERR_IO,
272 NFSERR_ACCES,
273 NFSERR_INVAL,
274 NFSERR_STALE,
275 NFSERR_BADHANDLE,
276 NFSERR_NOTSUPP,
277 NFSERR_SERVERFAULT,
278 0,
279};
280
281static short nfsv3err_read[] = {
282 NFSERR_IO,
283 NFSERR_IO,
284 NFSERR_NXIO,
285 NFSERR_ACCES,
286 NFSERR_INVAL,
287 NFSERR_STALE,
288 NFSERR_BADHANDLE,
289 NFSERR_SERVERFAULT,
290 0,
291};
292
293static short nfsv3err_write[] = {
294 NFSERR_IO,
295 NFSERR_IO,
296 NFSERR_ACCES,
297 NFSERR_INVAL,
298 NFSERR_FBIG,
299 NFSERR_NOSPC,
300 NFSERR_ROFS,
301 NFSERR_DQUOT,
302 NFSERR_STALE,
303 NFSERR_BADHANDLE,
304 NFSERR_SERVERFAULT,
305 0,
306};
307
308static short nfsv3err_create[] = {
309 NFSERR_IO,
310 NFSERR_IO,
311 NFSERR_ACCES,
312 NFSERR_EXIST,
313 NFSERR_NOTDIR,
314 NFSERR_NOSPC,
315 NFSERR_ROFS,
316 NFSERR_NAMETOL,
317 NFSERR_DQUOT,
318 NFSERR_STALE,
319 NFSERR_BADHANDLE,
320 NFSERR_NOTSUPP,
321 NFSERR_SERVERFAULT,
322 0,
323};
324
325static short nfsv3err_mkdir[] = {
326 NFSERR_IO,
327 NFSERR_IO,
328 NFSERR_ACCES,
329 NFSERR_EXIST,
330 NFSERR_NOTDIR,
331 NFSERR_NOSPC,
332 NFSERR_ROFS,
333 NFSERR_NAMETOL,
334 NFSERR_DQUOT,
335 NFSERR_STALE,
336 NFSERR_BADHANDLE,
337 NFSERR_NOTSUPP,
338 NFSERR_SERVERFAULT,
339 0,
340};
341
342static short nfsv3err_symlink[] = {
343 NFSERR_IO,
344 NFSERR_IO,
345 NFSERR_ACCES,
346 NFSERR_EXIST,
347 NFSERR_NOTDIR,
348 NFSERR_NOSPC,
349 NFSERR_ROFS,
350 NFSERR_NAMETOL,
351 NFSERR_DQUOT,
352 NFSERR_STALE,
353 NFSERR_BADHANDLE,
354 NFSERR_NOTSUPP,
355 NFSERR_SERVERFAULT,
356 0,
357};
358
359static short nfsv3err_mknod[] = {
360 NFSERR_IO,
361 NFSERR_IO,
362 NFSERR_ACCES,
363 NFSERR_EXIST,
364 NFSERR_NOTDIR,
365 NFSERR_NOSPC,
366 NFSERR_ROFS,
367 NFSERR_NAMETOL,
368 NFSERR_DQUOT,
369 NFSERR_STALE,
370 NFSERR_BADHANDLE,
371 NFSERR_NOTSUPP,
372 NFSERR_SERVERFAULT,
373 NFSERR_BADTYPE,
374 0,
375};
376
377static short nfsv3err_remove[] = {
378 NFSERR_IO,
379 NFSERR_NOENT,
380 NFSERR_IO,
381 NFSERR_ACCES,
382 NFSERR_NOTDIR,
383 NFSERR_ROFS,
384 NFSERR_NAMETOL,
385 NFSERR_STALE,
386 NFSERR_BADHANDLE,
387 NFSERR_SERVERFAULT,
388 0,
389};
390
391static short nfsv3err_rmdir[] = {
392 NFSERR_IO,
393 NFSERR_NOENT,
394 NFSERR_IO,
395 NFSERR_ACCES,
396 NFSERR_EXIST,
397 NFSERR_NOTDIR,
398 NFSERR_INVAL,
399 NFSERR_ROFS,
400 NFSERR_NAMETOL,
401 NFSERR_NOTEMPTY,
402 NFSERR_STALE,
403 NFSERR_BADHANDLE,
404 NFSERR_NOTSUPP,
405 NFSERR_SERVERFAULT,
406 0,
407};
408
409static short nfsv3err_rename[] = {
410 NFSERR_IO,
411 NFSERR_NOENT,
412 NFSERR_IO,
413 NFSERR_ACCES,
414 NFSERR_EXIST,
415 NFSERR_XDEV,
416 NFSERR_NOTDIR,
417 NFSERR_ISDIR,
418 NFSERR_INVAL,
419 NFSERR_NOSPC,
420 NFSERR_ROFS,
421 NFSERR_MLINK,
422 NFSERR_NAMETOL,
423 NFSERR_NOTEMPTY,
424 NFSERR_DQUOT,
425 NFSERR_STALE,
426 NFSERR_BADHANDLE,
427 NFSERR_NOTSUPP,
428 NFSERR_SERVERFAULT,
429 0,
430};
431
432static short nfsv3err_link[] = {
433 NFSERR_IO,
434 NFSERR_IO,
435 NFSERR_ACCES,
436 NFSERR_EXIST,
437 NFSERR_XDEV,
438 NFSERR_NOTDIR,
439 NFSERR_INVAL,
440 NFSERR_NOSPC,
441 NFSERR_ROFS,
442 NFSERR_MLINK,
443 NFSERR_NAMETOL,
444 NFSERR_DQUOT,
445 NFSERR_STALE,
446 NFSERR_BADHANDLE,
447 NFSERR_NOTSUPP,
448 NFSERR_SERVERFAULT,
449 0,
450};
451
452static short nfsv3err_readdir[] = {
453 NFSERR_IO,
454 NFSERR_IO,
455 NFSERR_ACCES,
456 NFSERR_NOTDIR,
457 NFSERR_STALE,
458 NFSERR_BADHANDLE,
459 NFSERR_BAD_COOKIE,
460 NFSERR_TOOSMALL,
461 NFSERR_SERVERFAULT,
462 0,
463};
464
465static short nfsv3err_readdirplus[] = {
466 NFSERR_IO,
467 NFSERR_IO,
468 NFSERR_ACCES,
469 NFSERR_NOTDIR,
470 NFSERR_STALE,
471 NFSERR_BADHANDLE,
472 NFSERR_BAD_COOKIE,
473 NFSERR_NOTSUPP,
474 NFSERR_TOOSMALL,
475 NFSERR_SERVERFAULT,
476 0,
477};
478
479static short nfsv3err_fsstat[] = {
480 NFSERR_IO,
481 NFSERR_IO,
482 NFSERR_STALE,
483 NFSERR_BADHANDLE,
484 NFSERR_SERVERFAULT,
485 0,
486};
487
488static short nfsv3err_fsinfo[] = {
489 NFSERR_STALE,
490 NFSERR_STALE,
491 NFSERR_BADHANDLE,
492 NFSERR_SERVERFAULT,
493 0,
494};
495
496static short nfsv3err_pathconf[] = {
497 NFSERR_STALE,
498 NFSERR_STALE,
499 NFSERR_BADHANDLE,
500 NFSERR_SERVERFAULT,
501 0,
502};
503
504static short nfsv3err_commit[] = {
505 NFSERR_IO,
506 NFSERR_IO,
507 NFSERR_STALE,
508 NFSERR_BADHANDLE,
509 NFSERR_SERVERFAULT,
510 0,
511};
512
513static short *nfsrv_v3errmap[] = {
514 nfsv3err_null,
515 nfsv3err_getattr,
516 nfsv3err_setattr,
517 nfsv3err_lookup,
518 nfsv3err_access,
519 nfsv3err_readlink,
520 nfsv3err_read,
521 nfsv3err_write,
522 nfsv3err_create,
523 nfsv3err_mkdir,
524 nfsv3err_symlink,
525 nfsv3err_mknod,
526 nfsv3err_remove,
527 nfsv3err_rmdir,
528 nfsv3err_rename,
529 nfsv3err_link,
530 nfsv3err_readdir,
531 nfsv3err_readdirplus,
532 nfsv3err_fsstat,
533 nfsv3err_fsinfo,
534 nfsv3err_pathconf,
535 nfsv3err_commit,
536};
537
538#endif /* NFS_NOSERVER */
539
540extern struct nfsrtt nfsrtt;
541extern time_t nqnfsstarttime;
542extern int nqsrv_clockskew;
543extern int nqsrv_writeslack;
544extern int nqsrv_maxlease;
545extern struct nfsstats nfsstats;
546extern int nqnfs_piggy[NFS_NPROCS];
547extern nfstype nfsv2_type[9];
548extern nfstype nfsv3_type[9];
549extern struct nfsnodehashhead *nfsnodehashtbl;
550extern u_long nfsnodehash;
551
552struct getfh_args;
553extern int getfh(struct proc *, struct getfh_args *, int *);
554struct nfssvc_args;
555extern int nfssvc(struct proc *, struct nfssvc_args *, int *);
556
557LIST_HEAD(nfsnodehashhead, nfsnode);
558
559int nfs_webnamei __P((struct nameidata *, struct vnode *, struct proc *));
560
561u_quad_t
562nfs_curusec()
563{
564 struct timeval tv;
565
566 getmicrotime(&tv);
567 return ((u_quad_t)tv.tv_sec * 1000000 + (u_quad_t)tv.tv_usec);
568}
569
570/*
571 * Create the header for an rpc request packet
572 * The hsiz is the size of the rest of the nfs request header.
573 * (just used to decide if a cluster is a good idea)
574 */
575struct mbuf *
576nfsm_reqh(vp, procid, hsiz, bposp)
577 struct vnode *vp;
578 u_long procid;
579 int hsiz;
580 caddr_t *bposp;
581{
582 register struct mbuf *mb;
583 register u_long *tl;
584 register caddr_t bpos;
585 struct mbuf *mb2;
586 struct nfsmount *nmp;
587 int nqflag;
588
589 MGET(mb, M_WAIT, MT_DATA);
590 if (hsiz >= MINCLSIZE)
591 MCLGET(mb, M_WAIT);
592 mb->m_len = 0;
593 bpos = mtod(mb, caddr_t);
594
595 /*
596 * For NQNFS, add lease request.
597 */
598 if (vp) {
599 nmp = VFSTONFS(vp->v_mount);
600 if (nmp->nm_flag & NFSMNT_NQNFS) {
601 nqflag = NQNFS_NEEDLEASE(vp, procid);
602 if (nqflag) {
603 nfsm_build(tl, u_long *, 2*NFSX_UNSIGNED);
604 *tl++ = txdr_unsigned(nqflag);
605 *tl = txdr_unsigned(nmp->nm_leaseterm);
606 } else {
607 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
608 *tl = 0;
609 }
610 }
611 }
612 /* Finally, return values */
613 *bposp = bpos;
614 return (mb);
615}
616
617/*
618 * Build the RPC header and fill in the authorization info.
619 * The authorization string argument is only used when the credentials
620 * come from outside of the kernel.
621 * Returns the head of the mbuf list.
622 */
623struct mbuf *
624nfsm_rpchead(cr, nmflag, procid, auth_type, auth_len, auth_str, verf_len,
625 verf_str, mrest, mrest_len, mbp, xidp)
626 register struct ucred *cr;
627 int nmflag;
628 int procid;
629 int auth_type;
630 int auth_len;
631 char *auth_str;
632 int verf_len;
633 char *verf_str;
634 struct mbuf *mrest;
635 int mrest_len;
636 struct mbuf **mbp;
637 u_long *xidp;
638{
639 register struct mbuf *mb;
640 register u_long *tl;
641 register caddr_t bpos;
642 register int i;
643 struct mbuf *mreq, *mb2;
644 int siz, grpsiz, authsiz;
645 static u_long base;
646
647 authsiz = nfsm_rndup(auth_len);
648 MGETHDR(mb, M_WAIT, MT_DATA);
649 if ((authsiz + 10 * NFSX_UNSIGNED) >= MINCLSIZE) {
650 MCLGET(mb, M_WAIT);
651 } else if ((authsiz + 10 * NFSX_UNSIGNED) < MHLEN) {
652 MH_ALIGN(mb, authsiz + 10 * NFSX_UNSIGNED);
653 } else {
654 MH_ALIGN(mb, 8 * NFSX_UNSIGNED);
655 }
656 mb->m_len = 0;
657 mreq = mb;
658 bpos = mtod(mb, caddr_t);
659
660 /*
661 * First the RPC header.
662 */
663 nfsm_build(tl, u_long *, 8 * NFSX_UNSIGNED);
664
665 /* Get a pretty random xid to start with */
666 if (!nfs_xid)
667 nfs_xid = random();
668 /*
669 * Skip zero xid if it should ever happen.
670 */
671 if (++nfs_xid == 0)
672 nfs_xid++;
673
674 *tl++ = *xidp = txdr_unsigned(nfs_xid);
675 *tl++ = rpc_call;
676 *tl++ = rpc_vers;
677 if (nmflag & NFSMNT_NQNFS) {
678 *tl++ = txdr_unsigned(NQNFS_PROG);
679 *tl++ = txdr_unsigned(NQNFS_VER3);
680 } else {
681 *tl++ = txdr_unsigned(NFS_PROG);
682 if (nmflag & NFSMNT_NFSV3)
683 *tl++ = txdr_unsigned(NFS_VER3);
684 else
685 *tl++ = txdr_unsigned(NFS_VER2);
686 }
687 if (nmflag & NFSMNT_NFSV3)
688 *tl++ = txdr_unsigned(procid);
689 else
690 *tl++ = txdr_unsigned(nfsv2_procid[procid]);
691
692 /*
693 * And then the authorization cred.
694 */
695 *tl++ = txdr_unsigned(auth_type);
696 *tl = txdr_unsigned(authsiz);
697 switch (auth_type) {
698 case RPCAUTH_UNIX:
699 nfsm_build(tl, u_long *, auth_len);
700 *tl++ = 0; /* stamp ?? */
701 *tl++ = 0; /* NULL hostname */
702 *tl++ = txdr_unsigned(cr->cr_uid);
703 *tl++ = txdr_unsigned(cr->cr_groups[0]);
704 grpsiz = (auth_len >> 2) - 5;
705 *tl++ = txdr_unsigned(grpsiz);
706 for (i = 1; i <= grpsiz; i++)
707 *tl++ = txdr_unsigned(cr->cr_groups[i]);
708 break;
709 case RPCAUTH_KERB4:
710 siz = auth_len;
711 while (siz > 0) {
712 if (M_TRAILINGSPACE(mb) == 0) {
713 MGET(mb2, M_WAIT, MT_DATA);
714 if (siz >= MINCLSIZE)
715 MCLGET(mb2, M_WAIT);
716 mb->m_next = mb2;
717 mb = mb2;
718 mb->m_len = 0;
719 bpos = mtod(mb, caddr_t);
720 }
721 i = min(siz, M_TRAILINGSPACE(mb));
722 bcopy(auth_str, bpos, i);
723 mb->m_len += i;
724 auth_str += i;
725 bpos += i;
726 siz -= i;
727 }
728 if ((siz = (nfsm_rndup(auth_len) - auth_len)) > 0) {
729 for (i = 0; i < siz; i++)
730 *bpos++ = '\0';
731 mb->m_len += siz;
732 }
733 break;
734 };
735
736 /*
737 * And the verifier...
738 */
739 nfsm_build(tl, u_long *, 2 * NFSX_UNSIGNED);
740 if (verf_str) {
741 *tl++ = txdr_unsigned(RPCAUTH_KERB4);
742 *tl = txdr_unsigned(verf_len);
743 siz = verf_len;
744 while (siz > 0) {
745 if (M_TRAILINGSPACE(mb) == 0) {
746 MGET(mb2, M_WAIT, MT_DATA);
747 if (siz >= MINCLSIZE)
748 MCLGET(mb2, M_WAIT);
749 mb->m_next = mb2;
750 mb = mb2;
751 mb->m_len = 0;
752 bpos = mtod(mb, caddr_t);
753 }
754 i = min(siz, M_TRAILINGSPACE(mb));
755 bcopy(verf_str, bpos, i);
756 mb->m_len += i;
757 verf_str += i;
758 bpos += i;
759 siz -= i;
760 }
761 if ((siz = (nfsm_rndup(verf_len) - verf_len)) > 0) {
762 for (i = 0; i < siz; i++)
763 *bpos++ = '\0';
764 mb->m_len += siz;
765 }
766 } else {
767 *tl++ = txdr_unsigned(RPCAUTH_NULL);
768 *tl = 0;
769 }
770 mb->m_next = mrest;
771 mreq->m_pkthdr.len = authsiz + 10 * NFSX_UNSIGNED + mrest_len;
772 mreq->m_pkthdr.rcvif = (struct ifnet *)0;
773 *mbp = mb;
774 return (mreq);
775}
776
777/*
778 * copies mbuf chain to the uio scatter/gather list
779 */
780int
781nfsm_mbuftouio(mrep, uiop, siz, dpos)
782 struct mbuf **mrep;
783 register struct uio *uiop;
784 int siz;
785 caddr_t *dpos;
786{
787 register char *mbufcp, *uiocp;
788 register int xfer, left, len;
789 register struct mbuf *mp;
790 long uiosiz, rem;
791 int error = 0;
792
793 mp = *mrep;
794 mbufcp = *dpos;
795 len = mtod(mp, caddr_t)+mp->m_len-mbufcp;
796 rem = nfsm_rndup(siz)-siz;
797 while (siz > 0) {
798 if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
799 return (EFBIG);
800 left = uiop->uio_iov->iov_len;
801 uiocp = uiop->uio_iov->iov_base;
802 if (left > siz)
803 left = siz;
804 uiosiz = left;
805 while (left > 0) {
806 while (len == 0) {
807 mp = mp->m_next;
808 if (mp == NULL)
809 return (EBADRPC);
810 mbufcp = mtod(mp, caddr_t);
811 len = mp->m_len;
812 }
813 xfer = (left > len) ? len : left;
814#ifdef notdef
815 /* Not Yet.. */
816 if (uiop->uio_iov->iov_op != NULL)
817 (*(uiop->uio_iov->iov_op))
818 (mbufcp, uiocp, xfer);
819 else
820#endif
821 if (uiop->uio_segflg == UIO_SYSSPACE)
822 bcopy(mbufcp, uiocp, xfer);
823 else
824 copyout(mbufcp, uiocp, xfer);
825 left -= xfer;
826 len -= xfer;
827 mbufcp += xfer;
828 uiocp += xfer;
829 uiop->uio_offset += xfer;
830 uiop->uio_resid -= xfer;
831 }
832 if (uiop->uio_iov->iov_len <= siz) {
833 uiop->uio_iovcnt--;
834 uiop->uio_iov++;
835 } else {
836 uiop->uio_iov->iov_base += uiosiz;
837 uiop->uio_iov->iov_len -= uiosiz;
838 }
839 siz -= uiosiz;
840 }
841 *dpos = mbufcp;
842 *mrep = mp;
843 if (rem > 0) {
844 if (len < rem)
845 error = nfs_adv(mrep, dpos, rem, len);
846 else
847 *dpos += rem;
848 }
849 return (error);
850}
851
852/*
853 * copies a uio scatter/gather list to an mbuf chain.
854 * NOTE: can ony handle iovcnt == 1
855 */
856int
857nfsm_uiotombuf(uiop, mq, siz, bpos)
858 register struct uio *uiop;
859 struct mbuf **mq;
860 int siz;
861 caddr_t *bpos;
862{
863 register char *uiocp;
864 register struct mbuf *mp, *mp2;
865 register int xfer, left, mlen;
866 int uiosiz, clflg, rem;
867 char *cp;
868
869 if (uiop->uio_iovcnt != 1)
870 panic("nfsm_uiotombuf: iovcnt != 1");
871
872 if (siz > MLEN) /* or should it >= MCLBYTES ?? */
873 clflg = 1;
874 else
875 clflg = 0;
876 rem = nfsm_rndup(siz)-siz;
877 mp = mp2 = *mq;
878 while (siz > 0) {
879 left = uiop->uio_iov->iov_len;
880 uiocp = uiop->uio_iov->iov_base;
881 if (left > siz)
882 left = siz;
883 uiosiz = left;
884 while (left > 0) {
885 mlen = M_TRAILINGSPACE(mp);
886 if (mlen == 0) {
887 MGET(mp, M_WAIT, MT_DATA);
888 if (clflg)
889 MCLGET(mp, M_WAIT);
890 mp->m_len = 0;
891 mp2->m_next = mp;
892 mp2 = mp;
893 mlen = M_TRAILINGSPACE(mp);
894 }
895 xfer = (left > mlen) ? mlen : left;
896#ifdef notdef
897 /* Not Yet.. */
898 if (uiop->uio_iov->iov_op != NULL)
899 (*(uiop->uio_iov->iov_op))
900 (uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
901 else
902#endif
903 if (uiop->uio_segflg == UIO_SYSSPACE)
904 bcopy(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
905 else
906 copyin(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
907 mp->m_len += xfer;
908 left -= xfer;
909 uiocp += xfer;
910 uiop->uio_offset += xfer;
911 uiop->uio_resid -= xfer;
912 }
913 uiop->uio_iov->iov_base += uiosiz;
914 uiop->uio_iov->iov_len -= uiosiz;
915 siz -= uiosiz;
916 }
917 if (rem > 0) {
918 if (rem > M_TRAILINGSPACE(mp)) {
919 MGET(mp, M_WAIT, MT_DATA);
920 mp->m_len = 0;
921 mp2->m_next = mp;
922 }
923 cp = mtod(mp, caddr_t)+mp->m_len;
924 for (left = 0; left < rem; left++)
925 *cp++ = '\0';
926 mp->m_len += rem;
927 *bpos = cp;
928 } else
929 *bpos = mtod(mp, caddr_t)+mp->m_len;
930 *mq = mp;
931 return (0);
932}
933
934/*
935 * Help break down an mbuf chain by setting the first siz bytes contiguous
936 * pointed to by returned val.
937 * This is used by the macros nfsm_dissect and nfsm_dissecton for tough
938 * cases. (The macros use the vars. dpos and dpos2)
939 */
940int
941nfsm_disct(mdp, dposp, siz, left, cp2)
942 struct mbuf **mdp;
943 caddr_t *dposp;
944 int siz;
945 int left;
946 caddr_t *cp2;
947{
948 register struct mbuf *mp, *mp2;
949 register int siz2, xfer;
950 register caddr_t p;
951
952 mp = *mdp;
953 while (left == 0) {
954 *mdp = mp = mp->m_next;
955 if (mp == NULL)
956 return (EBADRPC);
957 left = mp->m_len;
958 *dposp = mtod(mp, caddr_t);
959 }
960 if (left >= siz) {
961 *cp2 = *dposp;
962 *dposp += siz;
963 } else if (mp->m_next == NULL) {
964 return (EBADRPC);
965 } else if (siz > MHLEN) {
966 panic("nfs S too big");
967 } else {
968 MGET(mp2, M_WAIT, MT_DATA);
969 mp2->m_next = mp->m_next;
970 mp->m_next = mp2;
971 mp->m_len -= left;
972 mp = mp2;
973 *cp2 = p = mtod(mp, caddr_t);
974 bcopy(*dposp, p, left); /* Copy what was left */
975 siz2 = siz-left;
976 p += left;
977 mp2 = mp->m_next;
978 /* Loop around copying up the siz2 bytes */
979 while (siz2 > 0) {
980 if (mp2 == NULL)
981 return (EBADRPC);
982 xfer = (siz2 > mp2->m_len) ? mp2->m_len : siz2;
983 if (xfer > 0) {
984 bcopy(mtod(mp2, caddr_t), p, xfer);
985 NFSMADV(mp2, xfer);
986 mp2->m_len -= xfer;
987 p += xfer;
988 siz2 -= xfer;
989 }
990 if (siz2 > 0)
991 mp2 = mp2->m_next;
992 }
993 mp->m_len = siz;
994 *mdp = mp2;
995 *dposp = mtod(mp2, caddr_t);
996 }
997 return (0);
998}
999
1000/*
1001 * Advance the position in the mbuf chain.
1002 */
1003int
1004nfs_adv(mdp, dposp, offs, left)
1005 struct mbuf **mdp;
1006 caddr_t *dposp;
1007 int offs;
1008 int left;
1009{
1010 register struct mbuf *m;
1011 register int s;
1012
1013 m = *mdp;
1014 s = left;
1015 while (s < offs) {
1016 offs -= s;
1017 m = m->m_next;
1018 if (m == NULL)
1019 return (EBADRPC);
1020 s = m->m_len;
1021 }
1022 *mdp = m;
1023 *dposp = mtod(m, caddr_t)+offs;
1024 return (0);
1025}
1026
1027/*
1028 * Copy a string into mbufs for the hard cases...
1029 */
1030int
1031nfsm_strtmbuf(mb, bpos, cp, siz)
1032 struct mbuf **mb;
1033 char **bpos;
38 */
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#include <sys/param.h>
46#include <sys/buf.h>
47#include <sys/proc.h>
48#include <sys/systm.h>
49#include <sys/kernel.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
60#include <vm/vm.h>
61#include <vm/vm_object.h>
62#include <vm/vm_extern.h>
63#include <vm/vm_zone.h>
64
65#include <nfs/rpcv2.h>
66#include <nfs/nfsproto.h>
67#include <nfs/nfs.h>
68#include <nfs/nfsnode.h>
69#include <nfs/xdr_subs.h>
70#include <nfs/nfsm_subs.h>
71#include <nfs/nfsmount.h>
72#include <nfs/nqnfs.h>
73#include <nfs/nfsrtt.h>
74
75#include <miscfs/specfs/specdev.h>
76
77#include <netinet/in.h>
78#ifdef ISO
79#include <netiso/iso.h>
80#endif
81
82/*
83 * Data items converted to xdr at startup, since they are constant
84 * This is kinda hokey, but may save a little time doing byte swaps
85 */
86u_long nfs_xdrneg1;
87u_long rpc_call, rpc_vers, rpc_reply, rpc_msgdenied, rpc_autherr,
88 rpc_mismatch, rpc_auth_unix, rpc_msgaccepted,
89 rpc_auth_kerb;
90u_long nfs_prog, nqnfs_prog, nfs_true, nfs_false;
91
92/* And other global data */
93static u_long nfs_xid = 0;
94static enum vtype nv2tov_type[8]= {
95 VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON, VNON
96};
97enum vtype nv3tov_type[8]= {
98 VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO
99};
100
101int nfs_mount_type;
102int nfs_ticks;
103
104struct nfs_reqq nfs_reqq;
105struct nfssvc_sockhead nfssvc_sockhead;
106int nfssvc_sockhead_flag;
107struct nfsd_head nfsd_head;
108int nfsd_head_flag;
109struct nfs_bufq nfs_bufq;
110struct nqtimerhead nqtimerhead;
111struct nqfhhashhead *nqfhhashtbl;
112u_long nqfhhash;
113
114#ifndef NFS_NOSERVER
115/*
116 * Mapping of old NFS Version 2 RPC numbers to generic numbers.
117 */
118int nfsv3_procid[NFS_NPROCS] = {
119 NFSPROC_NULL,
120 NFSPROC_GETATTR,
121 NFSPROC_SETATTR,
122 NFSPROC_NOOP,
123 NFSPROC_LOOKUP,
124 NFSPROC_READLINK,
125 NFSPROC_READ,
126 NFSPROC_NOOP,
127 NFSPROC_WRITE,
128 NFSPROC_CREATE,
129 NFSPROC_REMOVE,
130 NFSPROC_RENAME,
131 NFSPROC_LINK,
132 NFSPROC_SYMLINK,
133 NFSPROC_MKDIR,
134 NFSPROC_RMDIR,
135 NFSPROC_READDIR,
136 NFSPROC_FSSTAT,
137 NFSPROC_NOOP,
138 NFSPROC_NOOP,
139 NFSPROC_NOOP,
140 NFSPROC_NOOP,
141 NFSPROC_NOOP,
142 NFSPROC_NOOP,
143 NFSPROC_NOOP,
144 NFSPROC_NOOP
145};
146
147#endif /* NFS_NOSERVER */
148/*
149 * and the reverse mapping from generic to Version 2 procedure numbers
150 */
151int nfsv2_procid[NFS_NPROCS] = {
152 NFSV2PROC_NULL,
153 NFSV2PROC_GETATTR,
154 NFSV2PROC_SETATTR,
155 NFSV2PROC_LOOKUP,
156 NFSV2PROC_NOOP,
157 NFSV2PROC_READLINK,
158 NFSV2PROC_READ,
159 NFSV2PROC_WRITE,
160 NFSV2PROC_CREATE,
161 NFSV2PROC_MKDIR,
162 NFSV2PROC_SYMLINK,
163 NFSV2PROC_CREATE,
164 NFSV2PROC_REMOVE,
165 NFSV2PROC_RMDIR,
166 NFSV2PROC_RENAME,
167 NFSV2PROC_LINK,
168 NFSV2PROC_READDIR,
169 NFSV2PROC_NOOP,
170 NFSV2PROC_STATFS,
171 NFSV2PROC_NOOP,
172 NFSV2PROC_NOOP,
173 NFSV2PROC_NOOP,
174 NFSV2PROC_NOOP,
175 NFSV2PROC_NOOP,
176 NFSV2PROC_NOOP,
177 NFSV2PROC_NOOP,
178};
179
180#ifndef NFS_NOSERVER
181/*
182 * Maps errno values to nfs error numbers.
183 * Use NFSERR_IO as the catch all for ones not specifically defined in
184 * RFC 1094.
185 */
186static u_char nfsrv_v2errmap[ELAST] = {
187 NFSERR_PERM, NFSERR_NOENT, NFSERR_IO, NFSERR_IO, NFSERR_IO,
188 NFSERR_NXIO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
189 NFSERR_IO, NFSERR_IO, NFSERR_ACCES, NFSERR_IO, NFSERR_IO,
190 NFSERR_IO, NFSERR_EXIST, NFSERR_IO, NFSERR_NODEV, NFSERR_NOTDIR,
191 NFSERR_ISDIR, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
192 NFSERR_IO, NFSERR_FBIG, NFSERR_NOSPC, NFSERR_IO, NFSERR_ROFS,
193 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
194 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
195 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
196 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
197 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
198 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
199 NFSERR_IO, NFSERR_IO, NFSERR_NAMETOL, NFSERR_IO, NFSERR_IO,
200 NFSERR_NOTEMPTY, NFSERR_IO, NFSERR_IO, NFSERR_DQUOT, NFSERR_STALE,
201 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
202 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
203 NFSERR_IO,
204};
205
206/*
207 * Maps errno values to nfs error numbers.
208 * Although it is not obvious whether or not NFS clients really care if
209 * a returned error value is in the specified list for the procedure, the
210 * safest thing to do is filter them appropriately. For Version 2, the
211 * X/Open XNFS document is the only specification that defines error values
212 * for each RPC (The RFC simply lists all possible error values for all RPCs),
213 * so I have decided to not do this for Version 2.
214 * The first entry is the default error return and the rest are the valid
215 * errors for that RPC in increasing numeric order.
216 */
217static short nfsv3err_null[] = {
218 0,
219 0,
220};
221
222static short nfsv3err_getattr[] = {
223 NFSERR_IO,
224 NFSERR_IO,
225 NFSERR_STALE,
226 NFSERR_BADHANDLE,
227 NFSERR_SERVERFAULT,
228 0,
229};
230
231static short nfsv3err_setattr[] = {
232 NFSERR_IO,
233 NFSERR_PERM,
234 NFSERR_IO,
235 NFSERR_ACCES,
236 NFSERR_INVAL,
237 NFSERR_NOSPC,
238 NFSERR_ROFS,
239 NFSERR_DQUOT,
240 NFSERR_STALE,
241 NFSERR_BADHANDLE,
242 NFSERR_NOT_SYNC,
243 NFSERR_SERVERFAULT,
244 0,
245};
246
247static short nfsv3err_lookup[] = {
248 NFSERR_IO,
249 NFSERR_NOENT,
250 NFSERR_IO,
251 NFSERR_ACCES,
252 NFSERR_NOTDIR,
253 NFSERR_NAMETOL,
254 NFSERR_STALE,
255 NFSERR_BADHANDLE,
256 NFSERR_SERVERFAULT,
257 0,
258};
259
260static short nfsv3err_access[] = {
261 NFSERR_IO,
262 NFSERR_IO,
263 NFSERR_STALE,
264 NFSERR_BADHANDLE,
265 NFSERR_SERVERFAULT,
266 0,
267};
268
269static short nfsv3err_readlink[] = {
270 NFSERR_IO,
271 NFSERR_IO,
272 NFSERR_ACCES,
273 NFSERR_INVAL,
274 NFSERR_STALE,
275 NFSERR_BADHANDLE,
276 NFSERR_NOTSUPP,
277 NFSERR_SERVERFAULT,
278 0,
279};
280
281static short nfsv3err_read[] = {
282 NFSERR_IO,
283 NFSERR_IO,
284 NFSERR_NXIO,
285 NFSERR_ACCES,
286 NFSERR_INVAL,
287 NFSERR_STALE,
288 NFSERR_BADHANDLE,
289 NFSERR_SERVERFAULT,
290 0,
291};
292
293static short nfsv3err_write[] = {
294 NFSERR_IO,
295 NFSERR_IO,
296 NFSERR_ACCES,
297 NFSERR_INVAL,
298 NFSERR_FBIG,
299 NFSERR_NOSPC,
300 NFSERR_ROFS,
301 NFSERR_DQUOT,
302 NFSERR_STALE,
303 NFSERR_BADHANDLE,
304 NFSERR_SERVERFAULT,
305 0,
306};
307
308static short nfsv3err_create[] = {
309 NFSERR_IO,
310 NFSERR_IO,
311 NFSERR_ACCES,
312 NFSERR_EXIST,
313 NFSERR_NOTDIR,
314 NFSERR_NOSPC,
315 NFSERR_ROFS,
316 NFSERR_NAMETOL,
317 NFSERR_DQUOT,
318 NFSERR_STALE,
319 NFSERR_BADHANDLE,
320 NFSERR_NOTSUPP,
321 NFSERR_SERVERFAULT,
322 0,
323};
324
325static short nfsv3err_mkdir[] = {
326 NFSERR_IO,
327 NFSERR_IO,
328 NFSERR_ACCES,
329 NFSERR_EXIST,
330 NFSERR_NOTDIR,
331 NFSERR_NOSPC,
332 NFSERR_ROFS,
333 NFSERR_NAMETOL,
334 NFSERR_DQUOT,
335 NFSERR_STALE,
336 NFSERR_BADHANDLE,
337 NFSERR_NOTSUPP,
338 NFSERR_SERVERFAULT,
339 0,
340};
341
342static short nfsv3err_symlink[] = {
343 NFSERR_IO,
344 NFSERR_IO,
345 NFSERR_ACCES,
346 NFSERR_EXIST,
347 NFSERR_NOTDIR,
348 NFSERR_NOSPC,
349 NFSERR_ROFS,
350 NFSERR_NAMETOL,
351 NFSERR_DQUOT,
352 NFSERR_STALE,
353 NFSERR_BADHANDLE,
354 NFSERR_NOTSUPP,
355 NFSERR_SERVERFAULT,
356 0,
357};
358
359static short nfsv3err_mknod[] = {
360 NFSERR_IO,
361 NFSERR_IO,
362 NFSERR_ACCES,
363 NFSERR_EXIST,
364 NFSERR_NOTDIR,
365 NFSERR_NOSPC,
366 NFSERR_ROFS,
367 NFSERR_NAMETOL,
368 NFSERR_DQUOT,
369 NFSERR_STALE,
370 NFSERR_BADHANDLE,
371 NFSERR_NOTSUPP,
372 NFSERR_SERVERFAULT,
373 NFSERR_BADTYPE,
374 0,
375};
376
377static short nfsv3err_remove[] = {
378 NFSERR_IO,
379 NFSERR_NOENT,
380 NFSERR_IO,
381 NFSERR_ACCES,
382 NFSERR_NOTDIR,
383 NFSERR_ROFS,
384 NFSERR_NAMETOL,
385 NFSERR_STALE,
386 NFSERR_BADHANDLE,
387 NFSERR_SERVERFAULT,
388 0,
389};
390
391static short nfsv3err_rmdir[] = {
392 NFSERR_IO,
393 NFSERR_NOENT,
394 NFSERR_IO,
395 NFSERR_ACCES,
396 NFSERR_EXIST,
397 NFSERR_NOTDIR,
398 NFSERR_INVAL,
399 NFSERR_ROFS,
400 NFSERR_NAMETOL,
401 NFSERR_NOTEMPTY,
402 NFSERR_STALE,
403 NFSERR_BADHANDLE,
404 NFSERR_NOTSUPP,
405 NFSERR_SERVERFAULT,
406 0,
407};
408
409static short nfsv3err_rename[] = {
410 NFSERR_IO,
411 NFSERR_NOENT,
412 NFSERR_IO,
413 NFSERR_ACCES,
414 NFSERR_EXIST,
415 NFSERR_XDEV,
416 NFSERR_NOTDIR,
417 NFSERR_ISDIR,
418 NFSERR_INVAL,
419 NFSERR_NOSPC,
420 NFSERR_ROFS,
421 NFSERR_MLINK,
422 NFSERR_NAMETOL,
423 NFSERR_NOTEMPTY,
424 NFSERR_DQUOT,
425 NFSERR_STALE,
426 NFSERR_BADHANDLE,
427 NFSERR_NOTSUPP,
428 NFSERR_SERVERFAULT,
429 0,
430};
431
432static short nfsv3err_link[] = {
433 NFSERR_IO,
434 NFSERR_IO,
435 NFSERR_ACCES,
436 NFSERR_EXIST,
437 NFSERR_XDEV,
438 NFSERR_NOTDIR,
439 NFSERR_INVAL,
440 NFSERR_NOSPC,
441 NFSERR_ROFS,
442 NFSERR_MLINK,
443 NFSERR_NAMETOL,
444 NFSERR_DQUOT,
445 NFSERR_STALE,
446 NFSERR_BADHANDLE,
447 NFSERR_NOTSUPP,
448 NFSERR_SERVERFAULT,
449 0,
450};
451
452static short nfsv3err_readdir[] = {
453 NFSERR_IO,
454 NFSERR_IO,
455 NFSERR_ACCES,
456 NFSERR_NOTDIR,
457 NFSERR_STALE,
458 NFSERR_BADHANDLE,
459 NFSERR_BAD_COOKIE,
460 NFSERR_TOOSMALL,
461 NFSERR_SERVERFAULT,
462 0,
463};
464
465static short nfsv3err_readdirplus[] = {
466 NFSERR_IO,
467 NFSERR_IO,
468 NFSERR_ACCES,
469 NFSERR_NOTDIR,
470 NFSERR_STALE,
471 NFSERR_BADHANDLE,
472 NFSERR_BAD_COOKIE,
473 NFSERR_NOTSUPP,
474 NFSERR_TOOSMALL,
475 NFSERR_SERVERFAULT,
476 0,
477};
478
479static short nfsv3err_fsstat[] = {
480 NFSERR_IO,
481 NFSERR_IO,
482 NFSERR_STALE,
483 NFSERR_BADHANDLE,
484 NFSERR_SERVERFAULT,
485 0,
486};
487
488static short nfsv3err_fsinfo[] = {
489 NFSERR_STALE,
490 NFSERR_STALE,
491 NFSERR_BADHANDLE,
492 NFSERR_SERVERFAULT,
493 0,
494};
495
496static short nfsv3err_pathconf[] = {
497 NFSERR_STALE,
498 NFSERR_STALE,
499 NFSERR_BADHANDLE,
500 NFSERR_SERVERFAULT,
501 0,
502};
503
504static short nfsv3err_commit[] = {
505 NFSERR_IO,
506 NFSERR_IO,
507 NFSERR_STALE,
508 NFSERR_BADHANDLE,
509 NFSERR_SERVERFAULT,
510 0,
511};
512
513static short *nfsrv_v3errmap[] = {
514 nfsv3err_null,
515 nfsv3err_getattr,
516 nfsv3err_setattr,
517 nfsv3err_lookup,
518 nfsv3err_access,
519 nfsv3err_readlink,
520 nfsv3err_read,
521 nfsv3err_write,
522 nfsv3err_create,
523 nfsv3err_mkdir,
524 nfsv3err_symlink,
525 nfsv3err_mknod,
526 nfsv3err_remove,
527 nfsv3err_rmdir,
528 nfsv3err_rename,
529 nfsv3err_link,
530 nfsv3err_readdir,
531 nfsv3err_readdirplus,
532 nfsv3err_fsstat,
533 nfsv3err_fsinfo,
534 nfsv3err_pathconf,
535 nfsv3err_commit,
536};
537
538#endif /* NFS_NOSERVER */
539
540extern struct nfsrtt nfsrtt;
541extern time_t nqnfsstarttime;
542extern int nqsrv_clockskew;
543extern int nqsrv_writeslack;
544extern int nqsrv_maxlease;
545extern struct nfsstats nfsstats;
546extern int nqnfs_piggy[NFS_NPROCS];
547extern nfstype nfsv2_type[9];
548extern nfstype nfsv3_type[9];
549extern struct nfsnodehashhead *nfsnodehashtbl;
550extern u_long nfsnodehash;
551
552struct getfh_args;
553extern int getfh(struct proc *, struct getfh_args *, int *);
554struct nfssvc_args;
555extern int nfssvc(struct proc *, struct nfssvc_args *, int *);
556
557LIST_HEAD(nfsnodehashhead, nfsnode);
558
559int nfs_webnamei __P((struct nameidata *, struct vnode *, struct proc *));
560
561u_quad_t
562nfs_curusec()
563{
564 struct timeval tv;
565
566 getmicrotime(&tv);
567 return ((u_quad_t)tv.tv_sec * 1000000 + (u_quad_t)tv.tv_usec);
568}
569
570/*
571 * Create the header for an rpc request packet
572 * The hsiz is the size of the rest of the nfs request header.
573 * (just used to decide if a cluster is a good idea)
574 */
575struct mbuf *
576nfsm_reqh(vp, procid, hsiz, bposp)
577 struct vnode *vp;
578 u_long procid;
579 int hsiz;
580 caddr_t *bposp;
581{
582 register struct mbuf *mb;
583 register u_long *tl;
584 register caddr_t bpos;
585 struct mbuf *mb2;
586 struct nfsmount *nmp;
587 int nqflag;
588
589 MGET(mb, M_WAIT, MT_DATA);
590 if (hsiz >= MINCLSIZE)
591 MCLGET(mb, M_WAIT);
592 mb->m_len = 0;
593 bpos = mtod(mb, caddr_t);
594
595 /*
596 * For NQNFS, add lease request.
597 */
598 if (vp) {
599 nmp = VFSTONFS(vp->v_mount);
600 if (nmp->nm_flag & NFSMNT_NQNFS) {
601 nqflag = NQNFS_NEEDLEASE(vp, procid);
602 if (nqflag) {
603 nfsm_build(tl, u_long *, 2*NFSX_UNSIGNED);
604 *tl++ = txdr_unsigned(nqflag);
605 *tl = txdr_unsigned(nmp->nm_leaseterm);
606 } else {
607 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
608 *tl = 0;
609 }
610 }
611 }
612 /* Finally, return values */
613 *bposp = bpos;
614 return (mb);
615}
616
617/*
618 * Build the RPC header and fill in the authorization info.
619 * The authorization string argument is only used when the credentials
620 * come from outside of the kernel.
621 * Returns the head of the mbuf list.
622 */
623struct mbuf *
624nfsm_rpchead(cr, nmflag, procid, auth_type, auth_len, auth_str, verf_len,
625 verf_str, mrest, mrest_len, mbp, xidp)
626 register struct ucred *cr;
627 int nmflag;
628 int procid;
629 int auth_type;
630 int auth_len;
631 char *auth_str;
632 int verf_len;
633 char *verf_str;
634 struct mbuf *mrest;
635 int mrest_len;
636 struct mbuf **mbp;
637 u_long *xidp;
638{
639 register struct mbuf *mb;
640 register u_long *tl;
641 register caddr_t bpos;
642 register int i;
643 struct mbuf *mreq, *mb2;
644 int siz, grpsiz, authsiz;
645 static u_long base;
646
647 authsiz = nfsm_rndup(auth_len);
648 MGETHDR(mb, M_WAIT, MT_DATA);
649 if ((authsiz + 10 * NFSX_UNSIGNED) >= MINCLSIZE) {
650 MCLGET(mb, M_WAIT);
651 } else if ((authsiz + 10 * NFSX_UNSIGNED) < MHLEN) {
652 MH_ALIGN(mb, authsiz + 10 * NFSX_UNSIGNED);
653 } else {
654 MH_ALIGN(mb, 8 * NFSX_UNSIGNED);
655 }
656 mb->m_len = 0;
657 mreq = mb;
658 bpos = mtod(mb, caddr_t);
659
660 /*
661 * First the RPC header.
662 */
663 nfsm_build(tl, u_long *, 8 * NFSX_UNSIGNED);
664
665 /* Get a pretty random xid to start with */
666 if (!nfs_xid)
667 nfs_xid = random();
668 /*
669 * Skip zero xid if it should ever happen.
670 */
671 if (++nfs_xid == 0)
672 nfs_xid++;
673
674 *tl++ = *xidp = txdr_unsigned(nfs_xid);
675 *tl++ = rpc_call;
676 *tl++ = rpc_vers;
677 if (nmflag & NFSMNT_NQNFS) {
678 *tl++ = txdr_unsigned(NQNFS_PROG);
679 *tl++ = txdr_unsigned(NQNFS_VER3);
680 } else {
681 *tl++ = txdr_unsigned(NFS_PROG);
682 if (nmflag & NFSMNT_NFSV3)
683 *tl++ = txdr_unsigned(NFS_VER3);
684 else
685 *tl++ = txdr_unsigned(NFS_VER2);
686 }
687 if (nmflag & NFSMNT_NFSV3)
688 *tl++ = txdr_unsigned(procid);
689 else
690 *tl++ = txdr_unsigned(nfsv2_procid[procid]);
691
692 /*
693 * And then the authorization cred.
694 */
695 *tl++ = txdr_unsigned(auth_type);
696 *tl = txdr_unsigned(authsiz);
697 switch (auth_type) {
698 case RPCAUTH_UNIX:
699 nfsm_build(tl, u_long *, auth_len);
700 *tl++ = 0; /* stamp ?? */
701 *tl++ = 0; /* NULL hostname */
702 *tl++ = txdr_unsigned(cr->cr_uid);
703 *tl++ = txdr_unsigned(cr->cr_groups[0]);
704 grpsiz = (auth_len >> 2) - 5;
705 *tl++ = txdr_unsigned(grpsiz);
706 for (i = 1; i <= grpsiz; i++)
707 *tl++ = txdr_unsigned(cr->cr_groups[i]);
708 break;
709 case RPCAUTH_KERB4:
710 siz = auth_len;
711 while (siz > 0) {
712 if (M_TRAILINGSPACE(mb) == 0) {
713 MGET(mb2, M_WAIT, MT_DATA);
714 if (siz >= MINCLSIZE)
715 MCLGET(mb2, M_WAIT);
716 mb->m_next = mb2;
717 mb = mb2;
718 mb->m_len = 0;
719 bpos = mtod(mb, caddr_t);
720 }
721 i = min(siz, M_TRAILINGSPACE(mb));
722 bcopy(auth_str, bpos, i);
723 mb->m_len += i;
724 auth_str += i;
725 bpos += i;
726 siz -= i;
727 }
728 if ((siz = (nfsm_rndup(auth_len) - auth_len)) > 0) {
729 for (i = 0; i < siz; i++)
730 *bpos++ = '\0';
731 mb->m_len += siz;
732 }
733 break;
734 };
735
736 /*
737 * And the verifier...
738 */
739 nfsm_build(tl, u_long *, 2 * NFSX_UNSIGNED);
740 if (verf_str) {
741 *tl++ = txdr_unsigned(RPCAUTH_KERB4);
742 *tl = txdr_unsigned(verf_len);
743 siz = verf_len;
744 while (siz > 0) {
745 if (M_TRAILINGSPACE(mb) == 0) {
746 MGET(mb2, M_WAIT, MT_DATA);
747 if (siz >= MINCLSIZE)
748 MCLGET(mb2, M_WAIT);
749 mb->m_next = mb2;
750 mb = mb2;
751 mb->m_len = 0;
752 bpos = mtod(mb, caddr_t);
753 }
754 i = min(siz, M_TRAILINGSPACE(mb));
755 bcopy(verf_str, bpos, i);
756 mb->m_len += i;
757 verf_str += i;
758 bpos += i;
759 siz -= i;
760 }
761 if ((siz = (nfsm_rndup(verf_len) - verf_len)) > 0) {
762 for (i = 0; i < siz; i++)
763 *bpos++ = '\0';
764 mb->m_len += siz;
765 }
766 } else {
767 *tl++ = txdr_unsigned(RPCAUTH_NULL);
768 *tl = 0;
769 }
770 mb->m_next = mrest;
771 mreq->m_pkthdr.len = authsiz + 10 * NFSX_UNSIGNED + mrest_len;
772 mreq->m_pkthdr.rcvif = (struct ifnet *)0;
773 *mbp = mb;
774 return (mreq);
775}
776
777/*
778 * copies mbuf chain to the uio scatter/gather list
779 */
780int
781nfsm_mbuftouio(mrep, uiop, siz, dpos)
782 struct mbuf **mrep;
783 register struct uio *uiop;
784 int siz;
785 caddr_t *dpos;
786{
787 register char *mbufcp, *uiocp;
788 register int xfer, left, len;
789 register struct mbuf *mp;
790 long uiosiz, rem;
791 int error = 0;
792
793 mp = *mrep;
794 mbufcp = *dpos;
795 len = mtod(mp, caddr_t)+mp->m_len-mbufcp;
796 rem = nfsm_rndup(siz)-siz;
797 while (siz > 0) {
798 if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
799 return (EFBIG);
800 left = uiop->uio_iov->iov_len;
801 uiocp = uiop->uio_iov->iov_base;
802 if (left > siz)
803 left = siz;
804 uiosiz = left;
805 while (left > 0) {
806 while (len == 0) {
807 mp = mp->m_next;
808 if (mp == NULL)
809 return (EBADRPC);
810 mbufcp = mtod(mp, caddr_t);
811 len = mp->m_len;
812 }
813 xfer = (left > len) ? len : left;
814#ifdef notdef
815 /* Not Yet.. */
816 if (uiop->uio_iov->iov_op != NULL)
817 (*(uiop->uio_iov->iov_op))
818 (mbufcp, uiocp, xfer);
819 else
820#endif
821 if (uiop->uio_segflg == UIO_SYSSPACE)
822 bcopy(mbufcp, uiocp, xfer);
823 else
824 copyout(mbufcp, uiocp, xfer);
825 left -= xfer;
826 len -= xfer;
827 mbufcp += xfer;
828 uiocp += xfer;
829 uiop->uio_offset += xfer;
830 uiop->uio_resid -= xfer;
831 }
832 if (uiop->uio_iov->iov_len <= siz) {
833 uiop->uio_iovcnt--;
834 uiop->uio_iov++;
835 } else {
836 uiop->uio_iov->iov_base += uiosiz;
837 uiop->uio_iov->iov_len -= uiosiz;
838 }
839 siz -= uiosiz;
840 }
841 *dpos = mbufcp;
842 *mrep = mp;
843 if (rem > 0) {
844 if (len < rem)
845 error = nfs_adv(mrep, dpos, rem, len);
846 else
847 *dpos += rem;
848 }
849 return (error);
850}
851
852/*
853 * copies a uio scatter/gather list to an mbuf chain.
854 * NOTE: can ony handle iovcnt == 1
855 */
856int
857nfsm_uiotombuf(uiop, mq, siz, bpos)
858 register struct uio *uiop;
859 struct mbuf **mq;
860 int siz;
861 caddr_t *bpos;
862{
863 register char *uiocp;
864 register struct mbuf *mp, *mp2;
865 register int xfer, left, mlen;
866 int uiosiz, clflg, rem;
867 char *cp;
868
869 if (uiop->uio_iovcnt != 1)
870 panic("nfsm_uiotombuf: iovcnt != 1");
871
872 if (siz > MLEN) /* or should it >= MCLBYTES ?? */
873 clflg = 1;
874 else
875 clflg = 0;
876 rem = nfsm_rndup(siz)-siz;
877 mp = mp2 = *mq;
878 while (siz > 0) {
879 left = uiop->uio_iov->iov_len;
880 uiocp = uiop->uio_iov->iov_base;
881 if (left > siz)
882 left = siz;
883 uiosiz = left;
884 while (left > 0) {
885 mlen = M_TRAILINGSPACE(mp);
886 if (mlen == 0) {
887 MGET(mp, M_WAIT, MT_DATA);
888 if (clflg)
889 MCLGET(mp, M_WAIT);
890 mp->m_len = 0;
891 mp2->m_next = mp;
892 mp2 = mp;
893 mlen = M_TRAILINGSPACE(mp);
894 }
895 xfer = (left > mlen) ? mlen : left;
896#ifdef notdef
897 /* Not Yet.. */
898 if (uiop->uio_iov->iov_op != NULL)
899 (*(uiop->uio_iov->iov_op))
900 (uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
901 else
902#endif
903 if (uiop->uio_segflg == UIO_SYSSPACE)
904 bcopy(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
905 else
906 copyin(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
907 mp->m_len += xfer;
908 left -= xfer;
909 uiocp += xfer;
910 uiop->uio_offset += xfer;
911 uiop->uio_resid -= xfer;
912 }
913 uiop->uio_iov->iov_base += uiosiz;
914 uiop->uio_iov->iov_len -= uiosiz;
915 siz -= uiosiz;
916 }
917 if (rem > 0) {
918 if (rem > M_TRAILINGSPACE(mp)) {
919 MGET(mp, M_WAIT, MT_DATA);
920 mp->m_len = 0;
921 mp2->m_next = mp;
922 }
923 cp = mtod(mp, caddr_t)+mp->m_len;
924 for (left = 0; left < rem; left++)
925 *cp++ = '\0';
926 mp->m_len += rem;
927 *bpos = cp;
928 } else
929 *bpos = mtod(mp, caddr_t)+mp->m_len;
930 *mq = mp;
931 return (0);
932}
933
934/*
935 * Help break down an mbuf chain by setting the first siz bytes contiguous
936 * pointed to by returned val.
937 * This is used by the macros nfsm_dissect and nfsm_dissecton for tough
938 * cases. (The macros use the vars. dpos and dpos2)
939 */
940int
941nfsm_disct(mdp, dposp, siz, left, cp2)
942 struct mbuf **mdp;
943 caddr_t *dposp;
944 int siz;
945 int left;
946 caddr_t *cp2;
947{
948 register struct mbuf *mp, *mp2;
949 register int siz2, xfer;
950 register caddr_t p;
951
952 mp = *mdp;
953 while (left == 0) {
954 *mdp = mp = mp->m_next;
955 if (mp == NULL)
956 return (EBADRPC);
957 left = mp->m_len;
958 *dposp = mtod(mp, caddr_t);
959 }
960 if (left >= siz) {
961 *cp2 = *dposp;
962 *dposp += siz;
963 } else if (mp->m_next == NULL) {
964 return (EBADRPC);
965 } else if (siz > MHLEN) {
966 panic("nfs S too big");
967 } else {
968 MGET(mp2, M_WAIT, MT_DATA);
969 mp2->m_next = mp->m_next;
970 mp->m_next = mp2;
971 mp->m_len -= left;
972 mp = mp2;
973 *cp2 = p = mtod(mp, caddr_t);
974 bcopy(*dposp, p, left); /* Copy what was left */
975 siz2 = siz-left;
976 p += left;
977 mp2 = mp->m_next;
978 /* Loop around copying up the siz2 bytes */
979 while (siz2 > 0) {
980 if (mp2 == NULL)
981 return (EBADRPC);
982 xfer = (siz2 > mp2->m_len) ? mp2->m_len : siz2;
983 if (xfer > 0) {
984 bcopy(mtod(mp2, caddr_t), p, xfer);
985 NFSMADV(mp2, xfer);
986 mp2->m_len -= xfer;
987 p += xfer;
988 siz2 -= xfer;
989 }
990 if (siz2 > 0)
991 mp2 = mp2->m_next;
992 }
993 mp->m_len = siz;
994 *mdp = mp2;
995 *dposp = mtod(mp2, caddr_t);
996 }
997 return (0);
998}
999
1000/*
1001 * Advance the position in the mbuf chain.
1002 */
1003int
1004nfs_adv(mdp, dposp, offs, left)
1005 struct mbuf **mdp;
1006 caddr_t *dposp;
1007 int offs;
1008 int left;
1009{
1010 register struct mbuf *m;
1011 register int s;
1012
1013 m = *mdp;
1014 s = left;
1015 while (s < offs) {
1016 offs -= s;
1017 m = m->m_next;
1018 if (m == NULL)
1019 return (EBADRPC);
1020 s = m->m_len;
1021 }
1022 *mdp = m;
1023 *dposp = mtod(m, caddr_t)+offs;
1024 return (0);
1025}
1026
1027/*
1028 * Copy a string into mbufs for the hard cases...
1029 */
1030int
1031nfsm_strtmbuf(mb, bpos, cp, siz)
1032 struct mbuf **mb;
1033 char **bpos;
1034 char *cp;
1034 const char *cp;
1035 long siz;
1036{
1037 register struct mbuf *m1 = 0, *m2;
1038 long left, xfer, len, tlen;
1039 u_long *tl;
1040 int putsize;
1041
1042 putsize = 1;
1043 m2 = *mb;
1044 left = M_TRAILINGSPACE(m2);
1045 if (left > 0) {
1046 tl = ((u_long *)(*bpos));
1047 *tl++ = txdr_unsigned(siz);
1048 putsize = 0;
1049 left -= NFSX_UNSIGNED;
1050 m2->m_len += NFSX_UNSIGNED;
1051 if (left > 0) {
1052 bcopy(cp, (caddr_t) tl, left);
1053 siz -= left;
1054 cp += left;
1055 m2->m_len += left;
1056 left = 0;
1057 }
1058 }
1059 /* Loop around adding mbufs */
1060 while (siz > 0) {
1061 MGET(m1, M_WAIT, MT_DATA);
1062 if (siz > MLEN)
1063 MCLGET(m1, M_WAIT);
1064 m1->m_len = NFSMSIZ(m1);
1065 m2->m_next = m1;
1066 m2 = m1;
1067 tl = mtod(m1, u_long *);
1068 tlen = 0;
1069 if (putsize) {
1070 *tl++ = txdr_unsigned(siz);
1071 m1->m_len -= NFSX_UNSIGNED;
1072 tlen = NFSX_UNSIGNED;
1073 putsize = 0;
1074 }
1075 if (siz < m1->m_len) {
1076 len = nfsm_rndup(siz);
1077 xfer = siz;
1078 if (xfer < len)
1079 *(tl+(xfer>>2)) = 0;
1080 } else {
1081 xfer = len = m1->m_len;
1082 }
1083 bcopy(cp, (caddr_t) tl, xfer);
1084 m1->m_len = len+tlen;
1085 siz -= xfer;
1086 cp += xfer;
1087 }
1088 *mb = m1;
1089 *bpos = mtod(m1, caddr_t)+m1->m_len;
1090 return (0);
1091}
1092
1093/*
1094 * Called once to initialize data structures...
1095 */
1096int
1097nfs_init(vfsp)
1098 struct vfsconf *vfsp;
1099{
1100 register int i;
1101
1102 nfsmount_zone = zinit("NFSMOUNT", sizeof(struct nfsmount), 0, 0, 1);
1103
1104 /*
1105 * Check to see if major data structures haven't bloated.
1106 */
1107 if (sizeof (struct nfssvc_sock) > NFS_SVCALLOC) {
1108 printf("struct nfssvc_sock bloated (> %dbytes)\n",NFS_SVCALLOC);
1109 printf("Try reducing NFS_UIDHASHSIZ\n");
1110 }
1111 if (sizeof (struct nfsuid) > NFS_UIDALLOC) {
1112 printf("struct nfsuid bloated (> %dbytes)\n",NFS_UIDALLOC);
1113 printf("Try unionizing the nu_nickname and nu_flag fields\n");
1114 }
1115 nfs_mount_type = vfsp->vfc_typenum;
1116 nfsrtt.pos = 0;
1117 rpc_vers = txdr_unsigned(RPC_VER2);
1118 rpc_call = txdr_unsigned(RPC_CALL);
1119 rpc_reply = txdr_unsigned(RPC_REPLY);
1120 rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED);
1121 rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED);
1122 rpc_mismatch = txdr_unsigned(RPC_MISMATCH);
1123 rpc_autherr = txdr_unsigned(RPC_AUTHERR);
1124 rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX);
1125 rpc_auth_kerb = txdr_unsigned(RPCAUTH_KERB4);
1126 nfs_prog = txdr_unsigned(NFS_PROG);
1127 nqnfs_prog = txdr_unsigned(NQNFS_PROG);
1128 nfs_true = txdr_unsigned(TRUE);
1129 nfs_false = txdr_unsigned(FALSE);
1130 nfs_xdrneg1 = txdr_unsigned(-1);
1131 nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000;
1132 if (nfs_ticks < 1)
1133 nfs_ticks = 1;
1134 /* Ensure async daemons disabled */
1135 for (i = 0; i < NFS_MAXASYNCDAEMON; i++) {
1136 nfs_iodwant[i] = (struct proc *)0;
1137 nfs_iodmount[i] = (struct nfsmount *)0;
1138 }
1139 nfs_nhinit(); /* Init the nfsnode table */
1140#ifndef NFS_NOSERVER
1141 nfsrv_init(0); /* Init server data structures */
1142 nfsrv_initcache(); /* Init the server request cache */
1143#endif
1144
1145 /*
1146 * Initialize the nqnfs server stuff.
1147 */
1148 if (nqnfsstarttime == 0) {
1149 nqnfsstarttime = boottime.tv_sec + nqsrv_maxlease
1150 + nqsrv_clockskew + nqsrv_writeslack;
1151 NQLOADNOVRAM(nqnfsstarttime);
1152 CIRCLEQ_INIT(&nqtimerhead);
1153 nqfhhashtbl = hashinit(NQLCHSZ, M_NQLEASE, &nqfhhash);
1154 }
1155
1156 /*
1157 * Initialize reply list and start timer
1158 */
1159 TAILQ_INIT(&nfs_reqq);
1160
1161 nfs_timer(0);
1162
1163
1164 /*
1165 * Set up lease_check and lease_updatetime so that other parts
1166 * of the system can call us, if we are loadable.
1167 */
1168#ifndef NFS_NOSERVER
1169 default_vnodeop_p[VOFFSET(vop_lease)] = (vop_t *)nqnfs_vop_lease_check;
1170#endif
1171 lease_updatetime = nfs_lease_updatetime;
1172 vfsp->vfc_refcount++; /* make us non-unloadable */
1173 sysent[SYS_nfssvc].sy_narg = 2;
1174 sysent[SYS_nfssvc].sy_call = (sy_call_t *)nfssvc;
1175#ifndef NFS_NOSERVER
1176 sysent[SYS_getfh].sy_narg = 2;
1177 sysent[SYS_getfh].sy_call = (sy_call_t *)getfh;
1178#endif
1179
1180 return (0);
1181}
1182
1183/*
1184 * Attribute cache routines.
1185 * nfs_loadattrcache() - loads or updates the cache contents from attributes
1186 * that are on the mbuf list
1187 * nfs_getattrcache() - returns valid attributes if found in cache, returns
1188 * error otherwise
1189 */
1190
1191/*
1192 * Load the attribute cache (that lives in the nfsnode entry) with
1193 * the values on the mbuf list and
1194 * Iff vap not NULL
1195 * copy the attributes to *vaper
1196 */
1197int
1198nfs_loadattrcache(vpp, mdp, dposp, vaper)
1199 struct vnode **vpp;
1200 struct mbuf **mdp;
1201 caddr_t *dposp;
1202 struct vattr *vaper;
1203{
1204 register struct vnode *vp = *vpp;
1205 register struct vattr *vap;
1206 register struct nfs_fattr *fp;
1207 register struct nfsnode *np;
1208 register long t1;
1209 caddr_t cp2;
1210 int error = 0, rdev;
1211 struct mbuf *md;
1212 enum vtype vtyp;
1213 u_short vmode;
1214 struct timespec mtime;
1215 struct vnode *nvp;
1216 int v3 = NFS_ISV3(vp);
1217
1218 md = *mdp;
1219 t1 = (mtod(md, caddr_t) + md->m_len) - *dposp;
1220 if (error = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, &cp2))
1221 return (error);
1222 fp = (struct nfs_fattr *)cp2;
1223 if (v3) {
1224 vtyp = nfsv3tov_type(fp->fa_type);
1225 vmode = fxdr_unsigned(u_short, fp->fa_mode);
1226 rdev = makedev(fxdr_unsigned(int, fp->fa3_rdev.specdata1),
1227 fxdr_unsigned(int, fp->fa3_rdev.specdata2));
1228 fxdr_nfsv3time(&fp->fa3_mtime, &mtime);
1229 } else {
1230 vtyp = nfsv2tov_type(fp->fa_type);
1231 vmode = fxdr_unsigned(u_short, fp->fa_mode);
1232 /*
1233 * XXX
1234 *
1235 * The duplicate information returned in fa_type and fa_mode
1236 * is an ambiguity in the NFS version 2 protocol.
1237 *
1238 * VREG should be taken literally as a regular file. If a
1239 * server intents to return some type information differently
1240 * in the upper bits of the mode field (e.g. for sockets, or
1241 * FIFOs), NFSv2 mandates fa_type to be VNON. Anyway, we
1242 * leave the examination of the mode bits even in the VREG
1243 * case to avoid breakage for bogus servers, but we make sure
1244 * that there are actually type bits set in the upper part of
1245 * fa_mode (and failing that, trust the va_type field).
1246 *
1247 * NFSv3 cleared the issue, and requires fa_mode to not
1248 * contain any type information (while also introduing sockets
1249 * and FIFOs for fa_type).
1250 */
1251 if (vtyp == VNON || (vtyp == VREG && (vmode & S_IFMT) != 0))
1252 vtyp = IFTOVT(vmode);
1253 rdev = fxdr_unsigned(long, fp->fa2_rdev);
1254 fxdr_nfsv2time(&fp->fa2_mtime, &mtime);
1255
1256 /*
1257 * Really ugly NFSv2 kludge.
1258 */
1259 if (vtyp == VCHR && rdev == 0xffffffff)
1260 vtyp = VFIFO;
1261 }
1262
1263 /*
1264 * If v_type == VNON it is a new node, so fill in the v_type,
1265 * n_mtime fields. Check to see if it represents a special
1266 * device, and if so, check for a possible alias. Once the
1267 * correct vnode has been obtained, fill in the rest of the
1268 * information.
1269 */
1270 np = VTONFS(vp);
1271 if (vp->v_type != vtyp) {
1272 vp->v_type = vtyp;
1273 if (vp->v_type == VFIFO) {
1274 vp->v_op = fifo_nfsv2nodeop_p;
1275 }
1276 if (vp->v_type == VCHR || vp->v_type == VBLK) {
1277 vp->v_op = spec_nfsv2nodeop_p;
1278 nvp = checkalias(vp, (dev_t)rdev, vp->v_mount);
1279 if (nvp) {
1280 /*
1281 * Discard unneeded vnode, but save its nfsnode.
1282 * Since the nfsnode does not have a lock, its
1283 * vnode lock has to be carried over.
1284 */
1285 nvp->v_vnlock = vp->v_vnlock;
1286 vp->v_vnlock = NULL;
1287 nvp->v_data = vp->v_data;
1288 vp->v_data = NULL;
1289 vp->v_op = spec_vnodeop_p;
1290 vrele(vp);
1291 vgone(vp);
1292 /*
1293 * Reinitialize aliased node.
1294 */
1295 np->n_vnode = nvp;
1296 *vpp = vp = nvp;
1297 }
1298 }
1299 np->n_mtime = mtime.tv_sec;
1300 }
1301 vap = &np->n_vattr;
1302 vap->va_type = vtyp;
1303 vap->va_mode = (vmode & 07777);
1304 vap->va_rdev = (dev_t)rdev;
1305 vap->va_mtime = mtime;
1306 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
1307 if (v3) {
1308 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1309 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
1310 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
1311 fxdr_hyper(&fp->fa3_size, &vap->va_size);
1312 vap->va_blocksize = NFS_FABLKSIZE;
1313 fxdr_hyper(&fp->fa3_used, &vap->va_bytes);
1314 vap->va_fileid = fxdr_unsigned(int, fp->fa3_fileid.nfsuquad[1]);
1315 fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime);
1316 fxdr_nfsv3time(&fp->fa3_ctime, &vap->va_ctime);
1317 vap->va_flags = 0;
1318 vap->va_filerev = 0;
1319 } else {
1320 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1321 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
1322 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
1323 vap->va_size = fxdr_unsigned(u_long, fp->fa2_size);
1324 vap->va_blocksize = fxdr_unsigned(long, fp->fa2_blocksize);
1325 vap->va_bytes = fxdr_unsigned(long, fp->fa2_blocks) * NFS_FABLKSIZE;
1326 vap->va_fileid = fxdr_unsigned(long, fp->fa2_fileid);
1327 fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime);
1328 vap->va_flags = 0;
1329 vap->va_ctime.tv_sec = fxdr_unsigned(long, fp->fa2_ctime.nfsv2_sec);
1330 vap->va_ctime.tv_nsec = 0;
1331 vap->va_gen = fxdr_unsigned(u_long, fp->fa2_ctime.nfsv2_usec);
1332 vap->va_filerev = 0;
1333 }
1334 if (vap->va_size != np->n_size) {
1335 if (vap->va_type == VREG) {
1336 if (np->n_flag & NMODIFIED) {
1337 if (vap->va_size < np->n_size)
1338 vap->va_size = np->n_size;
1339 else
1340 np->n_size = vap->va_size;
1341 } else
1342 np->n_size = vap->va_size;
1343 vnode_pager_setsize(vp, (u_long)np->n_size);
1344 } else
1345 np->n_size = vap->va_size;
1346 }
1347 np->n_attrstamp = time_second;
1348 if (vaper != NULL) {
1349 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap));
1350 if (np->n_flag & NCHG) {
1351 if (np->n_flag & NACC)
1352 vaper->va_atime = np->n_atim;
1353 if (np->n_flag & NUPD)
1354 vaper->va_mtime = np->n_mtim;
1355 }
1356 }
1357 return (0);
1358}
1359
1360#ifdef NFS_ACDEBUG
1361#include <sys/sysctl.h>
1362static int nfs_acdebug;
1363SYSCTL_INT(_vfs_nfs, OID_AUTO, acdebug, CTLFLAG_RW, &nfs_acdebug, 0, "");
1364#endif
1365
1366/*
1367 * Check the time stamp
1368 * If the cache is valid, copy contents to *vap and return 0
1369 * otherwise return an error
1370 */
1371int
1372nfs_getattrcache(vp, vaper)
1373 register struct vnode *vp;
1374 struct vattr *vaper;
1375{
1376 register struct nfsnode *np;
1377 register struct vattr *vap;
1378 struct nfsmount *nmp;
1379 int timeo;
1380
1381 np = VTONFS(vp);
1382 vap = &np->n_vattr;
1383 nmp = VFSTONFS(vp->v_mount);
1384 /* XXX n_mtime doesn't seem to be updated on a miss-and-reload */
1385 timeo = (time_second - np->n_mtime) / 10;
1386
1387#ifdef NFS_ACDEBUG
1388 if (nfs_acdebug>1)
1389 printf("nfs_getattrcache: initial timeo = %d\n", timeo);
1390#endif
1391
1392 if (vap->va_type == VDIR) {
1393 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acdirmin)
1394 timeo = nmp->nm_acdirmin;
1395 else if (timeo > nmp->nm_acdirmax)
1396 timeo = nmp->nm_acdirmax;
1397 } else {
1398 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acregmin)
1399 timeo = nmp->nm_acregmin;
1400 else if (timeo > nmp->nm_acregmax)
1401 timeo = nmp->nm_acregmax;
1402 }
1403
1404#ifdef NFS_ACDEBUG
1405 if (nfs_acdebug > 2)
1406 printf("acregmin %d; acregmax %d; acdirmin %d; acdirmax %d\n",
1407 nmp->nm_acregmin, nmp->nm_acregmax,
1408 nmp->nm_acdirmin, nmp->nm_acdirmax);
1409
1410 if (nfs_acdebug)
1411 printf("nfs_getattrcache: age = %d; final timeo = %d\n",r
1412 (time_second - np->n_attrstamp), timeo);
1413#endif
1414
1415 if ((time_second - np->n_attrstamp) >= timeo) {
1416 nfsstats.attrcache_misses++;
1417 return (ENOENT);
1418 }
1419 nfsstats.attrcache_hits++;
1420 if (vap->va_size != np->n_size) {
1421 if (vap->va_type == VREG) {
1422 if (np->n_flag & NMODIFIED) {
1423 if (vap->va_size < np->n_size)
1424 vap->va_size = np->n_size;
1425 else
1426 np->n_size = vap->va_size;
1427 } else
1428 np->n_size = vap->va_size;
1429 vnode_pager_setsize(vp, (u_long)np->n_size);
1430 } else
1431 np->n_size = vap->va_size;
1432 }
1433 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr));
1434 if (np->n_flag & NCHG) {
1435 if (np->n_flag & NACC)
1436 vaper->va_atime = np->n_atim;
1437 if (np->n_flag & NUPD)
1438 vaper->va_mtime = np->n_mtim;
1439 }
1440 return (0);
1441}
1442
1443#ifndef NFS_NOSERVER
1444/*
1445 * Set up nameidata for a lookup() call and do it.
1446 *
1447 * If pubflag is set, this call is done for a lookup operation on the
1448 * public filehandle. In that case we allow crossing mountpoints and
1449 * absolute pathnames. However, the caller is expected to check that
1450 * the lookup result is within the public fs, and deny access if
1451 * it is not.
1452 */
1453int
1454nfs_namei(ndp, fhp, len, slp, nam, mdp, dposp, retdirp, p, kerbflag, pubflag)
1455 register struct nameidata *ndp;
1456 fhandle_t *fhp;
1457 int len;
1458 struct nfssvc_sock *slp;
1459 struct sockaddr *nam;
1460 struct mbuf **mdp;
1461 caddr_t *dposp;
1462 struct vnode **retdirp;
1463 struct proc *p;
1464 int kerbflag, pubflag;
1465{
1466 register int i, rem;
1467 register struct mbuf *md;
1468 register char *fromcp, *tocp, *cp;
1469 struct iovec aiov;
1470 struct uio auio;
1471 struct vnode *dp;
1472 int error, rdonly, linklen;
1473 struct componentname *cnp = &ndp->ni_cnd;
1474
1475 *retdirp = (struct vnode *)0;
1476 cnp->cn_pnbuf = zalloc(namei_zone);
1477
1478 /*
1479 * Copy the name from the mbuf list to ndp->ni_pnbuf
1480 * and set the various ndp fields appropriately.
1481 */
1482 fromcp = *dposp;
1483 tocp = cnp->cn_pnbuf;
1484 md = *mdp;
1485 rem = mtod(md, caddr_t) + md->m_len - fromcp;
1486 cnp->cn_hash = 0;
1487 for (i = 0; i < len; i++) {
1488 while (rem == 0) {
1489 md = md->m_next;
1490 if (md == NULL) {
1491 error = EBADRPC;
1492 goto out;
1493 }
1494 fromcp = mtod(md, caddr_t);
1495 rem = md->m_len;
1496 }
1497 if (*fromcp == '\0' || (!pubflag && *fromcp == '/')) {
1498 error = EACCES;
1499 goto out;
1500 }
1501 cnp->cn_hash += (unsigned char)*fromcp;
1502 *tocp++ = *fromcp++;
1503 rem--;
1504 }
1505 *tocp = '\0';
1506 *mdp = md;
1507 *dposp = fromcp;
1508 len = nfsm_rndup(len)-len;
1509 if (len > 0) {
1510 if (rem >= len)
1511 *dposp += len;
1512 else if ((error = nfs_adv(mdp, dposp, len, rem)) != 0)
1513 goto out;
1514 }
1515
1516 /*
1517 * Extract and set starting directory.
1518 */
1519 error = nfsrv_fhtovp(fhp, FALSE, &dp, ndp->ni_cnd.cn_cred, slp,
1520 nam, &rdonly, kerbflag, pubflag);
1521 if (error)
1522 goto out;
1523 if (dp->v_type != VDIR) {
1524 vrele(dp);
1525 error = ENOTDIR;
1526 goto out;
1527 }
1528
1529 if (rdonly)
1530 cnp->cn_flags |= RDONLY;
1531
1532 *retdirp = dp;
1533
1534 if (pubflag) {
1535 /*
1536 * Oh joy. For WebNFS, handle those pesky '%' escapes,
1537 * and the 'native path' indicator.
1538 */
1539 cp = zalloc(namei_zone);
1540 fromcp = cnp->cn_pnbuf;
1541 tocp = cp;
1542 if ((unsigned char)*fromcp >= WEBNFS_SPECCHAR_START) {
1543 switch ((unsigned char)*fromcp) {
1544 case WEBNFS_NATIVE_CHAR:
1545 /*
1546 * 'Native' path for us is the same
1547 * as a path according to the NFS spec,
1548 * just skip the escape char.
1549 */
1550 fromcp++;
1551 break;
1552 /*
1553 * More may be added in the future, range 0x80-0xff
1554 */
1555 default:
1556 error = EIO;
1557 zfree(namei_zone, cp);
1558 goto out;
1559 }
1560 }
1561 /*
1562 * Translate the '%' escapes, URL-style.
1563 */
1564 while (*fromcp != '\0') {
1565 if (*fromcp == WEBNFS_ESC_CHAR) {
1566 if (fromcp[1] != '\0' && fromcp[2] != '\0') {
1567 fromcp++;
1568 *tocp++ = HEXSTRTOI(fromcp);
1569 fromcp += 2;
1570 continue;
1571 } else {
1572 error = ENOENT;
1573 zfree(namei_zone, cp);
1574 goto out;
1575 }
1576 } else
1577 *tocp++ = *fromcp++;
1578 }
1579 *tocp = '\0';
1580 zfree(namei_zone, cnp->cn_pnbuf);
1581 cnp->cn_pnbuf = cp;
1582 }
1583
1584 ndp->ni_pathlen = (tocp - cnp->cn_pnbuf) + 1;
1585 ndp->ni_segflg = UIO_SYSSPACE;
1586
1587 if (pubflag) {
1588 ndp->ni_rootdir = rootvnode;
1589 ndp->ni_loopcnt = 0;
1590 if (cnp->cn_pnbuf[0] == '/')
1591 dp = rootvnode;
1592 } else {
1593 cnp->cn_flags |= NOCROSSMOUNT;
1594 }
1595
1596 cnp->cn_proc = p;
1597 VREF(dp);
1598
1599 for (;;) {
1600 cnp->cn_nameptr = cnp->cn_pnbuf;
1601 ndp->ni_startdir = dp;
1602 /*
1603 * And call lookup() to do the real work
1604 */
1605 error = lookup(ndp);
1606 if (error)
1607 break;
1608 /*
1609 * Check for encountering a symbolic link
1610 */
1611 if ((cnp->cn_flags & ISSYMLINK) == 0) {
1612 nfsrv_object_create(ndp->ni_vp);
1613 if (cnp->cn_flags & (SAVENAME | SAVESTART)) {
1614 cnp->cn_flags |= HASBUF;
1615 return (0);
1616 }
1617 break;
1618 } else {
1619 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
1620 VOP_UNLOCK(ndp->ni_dvp, 0, p);
1621 if (!pubflag) {
1622 vrele(ndp->ni_dvp);
1623 vput(ndp->ni_vp);
1624 ndp->ni_vp = NULL;
1625 error = EINVAL;
1626 break;
1627 }
1628
1629 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
1630 error = ELOOP;
1631 break;
1632 }
1633 if (ndp->ni_pathlen > 1)
1634 cp = zalloc(namei_zone);
1635 else
1636 cp = cnp->cn_pnbuf;
1637 aiov.iov_base = cp;
1638 aiov.iov_len = MAXPATHLEN;
1639 auio.uio_iov = &aiov;
1640 auio.uio_iovcnt = 1;
1641 auio.uio_offset = 0;
1642 auio.uio_rw = UIO_READ;
1643 auio.uio_segflg = UIO_SYSSPACE;
1644 auio.uio_procp = (struct proc *)0;
1645 auio.uio_resid = MAXPATHLEN;
1646 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
1647 if (error) {
1648 badlink:
1649 if (ndp->ni_pathlen > 1)
1650 zfree(namei_zone, cp);
1651 break;
1652 }
1653 linklen = MAXPATHLEN - auio.uio_resid;
1654 if (linklen == 0) {
1655 error = ENOENT;
1656 goto badlink;
1657 }
1658 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
1659 error = ENAMETOOLONG;
1660 goto badlink;
1661 }
1662 if (ndp->ni_pathlen > 1) {
1663 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
1664 zfree(namei_zone, cnp->cn_pnbuf);
1665 cnp->cn_pnbuf = cp;
1666 } else
1667 cnp->cn_pnbuf[linklen] = '\0';
1668 ndp->ni_pathlen += linklen;
1669 vput(ndp->ni_vp);
1670 dp = ndp->ni_dvp;
1671 /*
1672 * Check if root directory should replace current directory.
1673 */
1674 if (cnp->cn_pnbuf[0] == '/') {
1675 vrele(dp);
1676 dp = ndp->ni_rootdir;
1677 VREF(dp);
1678 }
1679 }
1680 }
1681out:
1682 zfree(namei_zone, cnp->cn_pnbuf);
1683 return (error);
1684}
1685
1686/*
1687 * A fiddled version of m_adj() that ensures null fill to a long
1688 * boundary and only trims off the back end
1689 */
1690void
1691nfsm_adj(mp, len, nul)
1692 struct mbuf *mp;
1693 register int len;
1694 int nul;
1695{
1696 register struct mbuf *m;
1697 register int count, i;
1698 register char *cp;
1699
1700 /*
1701 * Trim from tail. Scan the mbuf chain,
1702 * calculating its length and finding the last mbuf.
1703 * If the adjustment only affects this mbuf, then just
1704 * adjust and return. Otherwise, rescan and truncate
1705 * after the remaining size.
1706 */
1707 count = 0;
1708 m = mp;
1709 for (;;) {
1710 count += m->m_len;
1711 if (m->m_next == (struct mbuf *)0)
1712 break;
1713 m = m->m_next;
1714 }
1715 if (m->m_len > len) {
1716 m->m_len -= len;
1717 if (nul > 0) {
1718 cp = mtod(m, caddr_t)+m->m_len-nul;
1719 for (i = 0; i < nul; i++)
1720 *cp++ = '\0';
1721 }
1722 return;
1723 }
1724 count -= len;
1725 if (count < 0)
1726 count = 0;
1727 /*
1728 * Correct length for chain is "count".
1729 * Find the mbuf with last data, adjust its length,
1730 * and toss data from remaining mbufs on chain.
1731 */
1732 for (m = mp; m; m = m->m_next) {
1733 if (m->m_len >= count) {
1734 m->m_len = count;
1735 if (nul > 0) {
1736 cp = mtod(m, caddr_t)+m->m_len-nul;
1737 for (i = 0; i < nul; i++)
1738 *cp++ = '\0';
1739 }
1740 break;
1741 }
1742 count -= m->m_len;
1743 }
1744 for (m = m->m_next;m;m = m->m_next)
1745 m->m_len = 0;
1746}
1747
1748/*
1749 * Make these functions instead of macros, so that the kernel text size
1750 * doesn't get too big...
1751 */
1752void
1753nfsm_srvwcc(nfsd, before_ret, before_vap, after_ret, after_vap, mbp, bposp)
1754 struct nfsrv_descript *nfsd;
1755 int before_ret;
1756 register struct vattr *before_vap;
1757 int after_ret;
1758 struct vattr *after_vap;
1759 struct mbuf **mbp;
1760 char **bposp;
1761{
1762 register struct mbuf *mb = *mbp, *mb2;
1763 register char *bpos = *bposp;
1764 register u_long *tl;
1765
1766 if (before_ret) {
1767 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
1768 *tl = nfs_false;
1769 } else {
1770 nfsm_build(tl, u_long *, 7 * NFSX_UNSIGNED);
1771 *tl++ = nfs_true;
1772 txdr_hyper(&(before_vap->va_size), tl);
1773 tl += 2;
1774 txdr_nfsv3time(&(before_vap->va_mtime), tl);
1775 tl += 2;
1776 txdr_nfsv3time(&(before_vap->va_ctime), tl);
1777 }
1778 *bposp = bpos;
1779 *mbp = mb;
1780 nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp);
1781}
1782
1783void
1784nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp)
1785 struct nfsrv_descript *nfsd;
1786 int after_ret;
1787 struct vattr *after_vap;
1788 struct mbuf **mbp;
1789 char **bposp;
1790{
1791 register struct mbuf *mb = *mbp, *mb2;
1792 register char *bpos = *bposp;
1793 register u_long *tl;
1794 register struct nfs_fattr *fp;
1795
1796 if (after_ret) {
1797 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
1798 *tl = nfs_false;
1799 } else {
1800 nfsm_build(tl, u_long *, NFSX_UNSIGNED + NFSX_V3FATTR);
1801 *tl++ = nfs_true;
1802 fp = (struct nfs_fattr *)tl;
1803 nfsm_srvfattr(nfsd, after_vap, fp);
1804 }
1805 *mbp = mb;
1806 *bposp = bpos;
1807}
1808
1809void
1810nfsm_srvfattr(nfsd, vap, fp)
1811 register struct nfsrv_descript *nfsd;
1812 register struct vattr *vap;
1813 register struct nfs_fattr *fp;
1814{
1815
1816 fp->fa_nlink = txdr_unsigned(vap->va_nlink);
1817 fp->fa_uid = txdr_unsigned(vap->va_uid);
1818 fp->fa_gid = txdr_unsigned(vap->va_gid);
1819 if (nfsd->nd_flag & ND_NFSV3) {
1820 fp->fa_type = vtonfsv3_type(vap->va_type);
1821 fp->fa_mode = vtonfsv3_mode(vap->va_mode);
1822 txdr_hyper(&vap->va_size, &fp->fa3_size);
1823 txdr_hyper(&vap->va_bytes, &fp->fa3_used);
1824 fp->fa3_rdev.specdata1 = txdr_unsigned(major(vap->va_rdev));
1825 fp->fa3_rdev.specdata2 = txdr_unsigned(minor(vap->va_rdev));
1826 fp->fa3_fsid.nfsuquad[0] = 0;
1827 fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(vap->va_fsid);
1828 fp->fa3_fileid.nfsuquad[0] = 0;
1829 fp->fa3_fileid.nfsuquad[1] = txdr_unsigned(vap->va_fileid);
1830 txdr_nfsv3time(&vap->va_atime, &fp->fa3_atime);
1831 txdr_nfsv3time(&vap->va_mtime, &fp->fa3_mtime);
1832 txdr_nfsv3time(&vap->va_ctime, &fp->fa3_ctime);
1833 } else {
1834 fp->fa_type = vtonfsv2_type(vap->va_type);
1835 fp->fa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1836 fp->fa2_size = txdr_unsigned(vap->va_size);
1837 fp->fa2_blocksize = txdr_unsigned(vap->va_blocksize);
1838 if (vap->va_type == VFIFO)
1839 fp->fa2_rdev = 0xffffffff;
1840 else
1841 fp->fa2_rdev = txdr_unsigned(vap->va_rdev);
1842 fp->fa2_blocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE);
1843 fp->fa2_fsid = txdr_unsigned(vap->va_fsid);
1844 fp->fa2_fileid = txdr_unsigned(vap->va_fileid);
1845 txdr_nfsv2time(&vap->va_atime, &fp->fa2_atime);
1846 txdr_nfsv2time(&vap->va_mtime, &fp->fa2_mtime);
1847 txdr_nfsv2time(&vap->va_ctime, &fp->fa2_ctime);
1848 }
1849}
1850
1851/*
1852 * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked)
1853 * - look up fsid in mount list (if not found ret error)
1854 * - get vp and export rights by calling VFS_FHTOVP()
1855 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
1856 * - if not lockflag unlock it with VOP_UNLOCK()
1857 */
1858int
1859nfsrv_fhtovp(fhp, lockflag, vpp, cred, slp, nam, rdonlyp, kerbflag, pubflag)
1860 fhandle_t *fhp;
1861 int lockflag;
1862 struct vnode **vpp;
1863 struct ucred *cred;
1864 struct nfssvc_sock *slp;
1865 struct sockaddr *nam;
1866 int *rdonlyp;
1867 int kerbflag;
1868 int pubflag;
1869{
1870 struct proc *p = curproc; /* XXX */
1871 register struct mount *mp;
1872 register int i;
1873 struct ucred *credanon;
1874 int error, exflags;
1875
1876 *vpp = (struct vnode *)0;
1877
1878 if (nfs_ispublicfh(fhp)) {
1879 if (!pubflag || !nfs_pub.np_valid)
1880 return (ESTALE);
1881 fhp = &nfs_pub.np_handle;
1882 }
1883
1884 mp = vfs_getvfs(&fhp->fh_fsid);
1885 if (!mp)
1886 return (ESTALE);
1887 error = VFS_FHTOVP(mp, &fhp->fh_fid, nam, vpp, &exflags, &credanon);
1888 if (error)
1889 return (error);
1890 /*
1891 * Check/setup credentials.
1892 */
1893 if (exflags & MNT_EXKERB) {
1894 if (!kerbflag) {
1895 vput(*vpp);
1896 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1897 }
1898 } else if (kerbflag) {
1899 vput(*vpp);
1900 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1901 } else if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
1902 cred->cr_uid = credanon->cr_uid;
1903 for (i = 0; i < credanon->cr_ngroups && i < NGROUPS; i++)
1904 cred->cr_groups[i] = credanon->cr_groups[i];
1905 cred->cr_ngroups = i;
1906 }
1907 if (exflags & MNT_EXRDONLY)
1908 *rdonlyp = 1;
1909 else
1910 *rdonlyp = 0;
1911
1912 nfsrv_object_create(*vpp);
1913
1914 if (!lockflag)
1915 VOP_UNLOCK(*vpp, 0, p);
1916 return (0);
1917}
1918
1919
1920/*
1921 * WebNFS: check if a filehandle is a public filehandle. For v3, this
1922 * means a length of 0, for v2 it means all zeroes. nfsm_srvmtofh has
1923 * transformed this to all zeroes in both cases, so check for it.
1924 */
1925int
1926nfs_ispublicfh(fhp)
1927 fhandle_t *fhp;
1928{
1929 char *cp = (char *)fhp;
1930 int i;
1931
1932 for (i = 0; i < NFSX_V3FH; i++)
1933 if (*cp++ != 0)
1934 return (FALSE);
1935 return (TRUE);
1936}
1937
1938#endif /* NFS_NOSERVER */
1939/*
1940 * This function compares two net addresses by family and returns TRUE
1941 * if they are the same host.
1942 * If there is any doubt, return FALSE.
1943 * The AF_INET family is handled as a special case so that address mbufs
1944 * don't need to be saved to store "struct in_addr", which is only 4 bytes.
1945 */
1946int
1947netaddr_match(family, haddr, nam)
1948 int family;
1949 union nethostaddr *haddr;
1950 struct sockaddr *nam;
1951{
1952 register struct sockaddr_in *inetaddr;
1953
1954 switch (family) {
1955 case AF_INET:
1956 inetaddr = (struct sockaddr_in *)nam;
1957 if (inetaddr->sin_family == AF_INET &&
1958 inetaddr->sin_addr.s_addr == haddr->had_inetaddr)
1959 return (1);
1960 break;
1961#ifdef ISO
1962 case AF_ISO:
1963 {
1964 register struct sockaddr_iso *isoaddr1, *isoaddr2;
1965
1966 isoaddr1 = (struct sockaddr_iso *)nam;
1967 isoaddr2 = (struct sockaddr_iso *)haddr->had_nam;
1968 if (isoaddr1->siso_family == AF_ISO &&
1969 isoaddr1->siso_nlen > 0 &&
1970 isoaddr1->siso_nlen == isoaddr2->siso_nlen &&
1971 SAME_ISOADDR(isoaddr1, isoaddr2))
1972 return (1);
1973 break;
1974 }
1975#endif /* ISO */
1976 default:
1977 break;
1978 };
1979 return (0);
1980}
1981
1982static nfsuint64 nfs_nullcookie = { 0, 0 };
1983/*
1984 * This function finds the directory cookie that corresponds to the
1985 * logical byte offset given.
1986 */
1987nfsuint64 *
1988nfs_getcookie(np, off, add)
1989 register struct nfsnode *np;
1990 off_t off;
1991 int add;
1992{
1993 register struct nfsdmap *dp, *dp2;
1994 register int pos;
1995
1996 pos = off / NFS_DIRBLKSIZ;
1997 if (pos == 0) {
1998#ifdef DIAGNOSTIC
1999 if (add)
2000 panic("nfs getcookie add at 0");
2001#endif
2002 return (&nfs_nullcookie);
2003 }
2004 pos--;
2005 dp = np->n_cookies.lh_first;
2006 if (!dp) {
2007 if (add) {
2008 MALLOC(dp, struct nfsdmap *, sizeof (struct nfsdmap),
2009 M_NFSDIROFF, M_WAITOK);
2010 dp->ndm_eocookie = 0;
2011 LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list);
2012 } else
2013 return ((nfsuint64 *)0);
2014 }
2015 while (pos >= NFSNUMCOOKIES) {
2016 pos -= NFSNUMCOOKIES;
2017 if (dp->ndm_list.le_next) {
2018 if (!add && dp->ndm_eocookie < NFSNUMCOOKIES &&
2019 pos >= dp->ndm_eocookie)
2020 return ((nfsuint64 *)0);
2021 dp = dp->ndm_list.le_next;
2022 } else if (add) {
2023 MALLOC(dp2, struct nfsdmap *, sizeof (struct nfsdmap),
2024 M_NFSDIROFF, M_WAITOK);
2025 dp2->ndm_eocookie = 0;
2026 LIST_INSERT_AFTER(dp, dp2, ndm_list);
2027 dp = dp2;
2028 } else
2029 return ((nfsuint64 *)0);
2030 }
2031 if (pos >= dp->ndm_eocookie) {
2032 if (add)
2033 dp->ndm_eocookie = pos + 1;
2034 else
2035 return ((nfsuint64 *)0);
2036 }
2037 return (&dp->ndm_cookies[pos]);
2038}
2039
2040/*
2041 * Invalidate cached directory information, except for the actual directory
2042 * blocks (which are invalidated separately).
2043 * Done mainly to avoid the use of stale offset cookies.
2044 */
2045void
2046nfs_invaldir(vp)
2047 register struct vnode *vp;
2048{
2049 register struct nfsnode *np = VTONFS(vp);
2050
2051#ifdef DIAGNOSTIC
2052 if (vp->v_type != VDIR)
2053 panic("nfs: invaldir not dir");
2054#endif
2055 np->n_direofoffset = 0;
2056 np->n_cookieverf.nfsuquad[0] = 0;
2057 np->n_cookieverf.nfsuquad[1] = 0;
2058 if (np->n_cookies.lh_first)
2059 np->n_cookies.lh_first->ndm_eocookie = 0;
2060}
2061
2062/*
2063 * The write verifier has changed (probably due to a server reboot), so all
2064 * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
2065 * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
2066 * flag. Once done the new write verifier can be set for the mount point.
2067 */
2068void
2069nfs_clearcommit(mp)
2070 struct mount *mp;
2071{
2072 register struct vnode *vp, *nvp;
2073 register struct buf *bp, *nbp;
2074 int s;
2075
2076 s = splbio();
2077loop:
2078 for (vp = mp->mnt_vnodelist.lh_first; vp; vp = nvp) {
2079 if (vp->v_mount != mp) /* Paranoia */
2080 goto loop;
2081 nvp = vp->v_mntvnodes.le_next;
2082 for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = nbp) {
2083 nbp = bp->b_vnbufs.le_next;
2084 if ((bp->b_flags & (B_BUSY | B_DELWRI | B_NEEDCOMMIT))
2085 == (B_DELWRI | B_NEEDCOMMIT))
2086 bp->b_flags &= ~B_NEEDCOMMIT;
2087 }
2088 }
2089 splx(s);
2090}
2091
2092#ifndef NFS_NOSERVER
2093/*
2094 * Map errnos to NFS error numbers. For Version 3 also filter out error
2095 * numbers not specified for the associated procedure.
2096 */
2097int
2098nfsrv_errmap(nd, err)
2099 struct nfsrv_descript *nd;
2100 register int err;
2101{
2102 register short *defaulterrp, *errp;
2103
2104 if (nd->nd_flag & ND_NFSV3) {
2105 if (nd->nd_procnum <= NFSPROC_COMMIT) {
2106 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
2107 while (*++errp) {
2108 if (*errp == err)
2109 return (err);
2110 else if (*errp > err)
2111 break;
2112 }
2113 return ((int)*defaulterrp);
2114 } else
2115 return (err & 0xffff);
2116 }
2117 if (err <= ELAST)
2118 return ((int)nfsrv_v2errmap[err - 1]);
2119 return (NFSERR_IO);
2120}
2121
2122int
2123nfsrv_object_create(vp)
2124 struct vnode *vp;
2125{
2126
2127 if (vp == NULL || vp->v_type != VREG)
2128 return (1);
2129 return (vfs_object_create(vp, curproc,
2130 curproc ? curproc->p_ucred : NULL, 1));
2131}
2132
2133/*
2134 * Sort the group list in increasing numerical order.
2135 * (Insertion sort by Chris Torek, who was grossed out by the bubble sort
2136 * that used to be here.)
2137 */
2138void
2139nfsrvw_sort(list, num)
2140 register gid_t *list;
2141 register int num;
2142{
2143 register int i, j;
2144 gid_t v;
2145
2146 /* Insertion sort. */
2147 for (i = 1; i < num; i++) {
2148 v = list[i];
2149 /* find correct slot for value v, moving others up */
2150 for (j = i; --j >= 0 && v < list[j];)
2151 list[j + 1] = list[j];
2152 list[j + 1] = v;
2153 }
2154}
2155
2156/*
2157 * copy credentials making sure that the result can be compared with bcmp().
2158 */
2159void
2160nfsrv_setcred(incred, outcred)
2161 register struct ucred *incred, *outcred;
2162{
2163 register int i;
2164
2165 bzero((caddr_t)outcred, sizeof (struct ucred));
2166 outcred->cr_ref = 1;
2167 outcred->cr_uid = incred->cr_uid;
2168 outcred->cr_ngroups = incred->cr_ngroups;
2169 for (i = 0; i < incred->cr_ngroups; i++)
2170 outcred->cr_groups[i] = incred->cr_groups[i];
2171 nfsrvw_sort(outcred->cr_groups, outcred->cr_ngroups);
2172}
2173#endif /* NFS_NOSERVER */
1035 long siz;
1036{
1037 register struct mbuf *m1 = 0, *m2;
1038 long left, xfer, len, tlen;
1039 u_long *tl;
1040 int putsize;
1041
1042 putsize = 1;
1043 m2 = *mb;
1044 left = M_TRAILINGSPACE(m2);
1045 if (left > 0) {
1046 tl = ((u_long *)(*bpos));
1047 *tl++ = txdr_unsigned(siz);
1048 putsize = 0;
1049 left -= NFSX_UNSIGNED;
1050 m2->m_len += NFSX_UNSIGNED;
1051 if (left > 0) {
1052 bcopy(cp, (caddr_t) tl, left);
1053 siz -= left;
1054 cp += left;
1055 m2->m_len += left;
1056 left = 0;
1057 }
1058 }
1059 /* Loop around adding mbufs */
1060 while (siz > 0) {
1061 MGET(m1, M_WAIT, MT_DATA);
1062 if (siz > MLEN)
1063 MCLGET(m1, M_WAIT);
1064 m1->m_len = NFSMSIZ(m1);
1065 m2->m_next = m1;
1066 m2 = m1;
1067 tl = mtod(m1, u_long *);
1068 tlen = 0;
1069 if (putsize) {
1070 *tl++ = txdr_unsigned(siz);
1071 m1->m_len -= NFSX_UNSIGNED;
1072 tlen = NFSX_UNSIGNED;
1073 putsize = 0;
1074 }
1075 if (siz < m1->m_len) {
1076 len = nfsm_rndup(siz);
1077 xfer = siz;
1078 if (xfer < len)
1079 *(tl+(xfer>>2)) = 0;
1080 } else {
1081 xfer = len = m1->m_len;
1082 }
1083 bcopy(cp, (caddr_t) tl, xfer);
1084 m1->m_len = len+tlen;
1085 siz -= xfer;
1086 cp += xfer;
1087 }
1088 *mb = m1;
1089 *bpos = mtod(m1, caddr_t)+m1->m_len;
1090 return (0);
1091}
1092
1093/*
1094 * Called once to initialize data structures...
1095 */
1096int
1097nfs_init(vfsp)
1098 struct vfsconf *vfsp;
1099{
1100 register int i;
1101
1102 nfsmount_zone = zinit("NFSMOUNT", sizeof(struct nfsmount), 0, 0, 1);
1103
1104 /*
1105 * Check to see if major data structures haven't bloated.
1106 */
1107 if (sizeof (struct nfssvc_sock) > NFS_SVCALLOC) {
1108 printf("struct nfssvc_sock bloated (> %dbytes)\n",NFS_SVCALLOC);
1109 printf("Try reducing NFS_UIDHASHSIZ\n");
1110 }
1111 if (sizeof (struct nfsuid) > NFS_UIDALLOC) {
1112 printf("struct nfsuid bloated (> %dbytes)\n",NFS_UIDALLOC);
1113 printf("Try unionizing the nu_nickname and nu_flag fields\n");
1114 }
1115 nfs_mount_type = vfsp->vfc_typenum;
1116 nfsrtt.pos = 0;
1117 rpc_vers = txdr_unsigned(RPC_VER2);
1118 rpc_call = txdr_unsigned(RPC_CALL);
1119 rpc_reply = txdr_unsigned(RPC_REPLY);
1120 rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED);
1121 rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED);
1122 rpc_mismatch = txdr_unsigned(RPC_MISMATCH);
1123 rpc_autherr = txdr_unsigned(RPC_AUTHERR);
1124 rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX);
1125 rpc_auth_kerb = txdr_unsigned(RPCAUTH_KERB4);
1126 nfs_prog = txdr_unsigned(NFS_PROG);
1127 nqnfs_prog = txdr_unsigned(NQNFS_PROG);
1128 nfs_true = txdr_unsigned(TRUE);
1129 nfs_false = txdr_unsigned(FALSE);
1130 nfs_xdrneg1 = txdr_unsigned(-1);
1131 nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000;
1132 if (nfs_ticks < 1)
1133 nfs_ticks = 1;
1134 /* Ensure async daemons disabled */
1135 for (i = 0; i < NFS_MAXASYNCDAEMON; i++) {
1136 nfs_iodwant[i] = (struct proc *)0;
1137 nfs_iodmount[i] = (struct nfsmount *)0;
1138 }
1139 nfs_nhinit(); /* Init the nfsnode table */
1140#ifndef NFS_NOSERVER
1141 nfsrv_init(0); /* Init server data structures */
1142 nfsrv_initcache(); /* Init the server request cache */
1143#endif
1144
1145 /*
1146 * Initialize the nqnfs server stuff.
1147 */
1148 if (nqnfsstarttime == 0) {
1149 nqnfsstarttime = boottime.tv_sec + nqsrv_maxlease
1150 + nqsrv_clockskew + nqsrv_writeslack;
1151 NQLOADNOVRAM(nqnfsstarttime);
1152 CIRCLEQ_INIT(&nqtimerhead);
1153 nqfhhashtbl = hashinit(NQLCHSZ, M_NQLEASE, &nqfhhash);
1154 }
1155
1156 /*
1157 * Initialize reply list and start timer
1158 */
1159 TAILQ_INIT(&nfs_reqq);
1160
1161 nfs_timer(0);
1162
1163
1164 /*
1165 * Set up lease_check and lease_updatetime so that other parts
1166 * of the system can call us, if we are loadable.
1167 */
1168#ifndef NFS_NOSERVER
1169 default_vnodeop_p[VOFFSET(vop_lease)] = (vop_t *)nqnfs_vop_lease_check;
1170#endif
1171 lease_updatetime = nfs_lease_updatetime;
1172 vfsp->vfc_refcount++; /* make us non-unloadable */
1173 sysent[SYS_nfssvc].sy_narg = 2;
1174 sysent[SYS_nfssvc].sy_call = (sy_call_t *)nfssvc;
1175#ifndef NFS_NOSERVER
1176 sysent[SYS_getfh].sy_narg = 2;
1177 sysent[SYS_getfh].sy_call = (sy_call_t *)getfh;
1178#endif
1179
1180 return (0);
1181}
1182
1183/*
1184 * Attribute cache routines.
1185 * nfs_loadattrcache() - loads or updates the cache contents from attributes
1186 * that are on the mbuf list
1187 * nfs_getattrcache() - returns valid attributes if found in cache, returns
1188 * error otherwise
1189 */
1190
1191/*
1192 * Load the attribute cache (that lives in the nfsnode entry) with
1193 * the values on the mbuf list and
1194 * Iff vap not NULL
1195 * copy the attributes to *vaper
1196 */
1197int
1198nfs_loadattrcache(vpp, mdp, dposp, vaper)
1199 struct vnode **vpp;
1200 struct mbuf **mdp;
1201 caddr_t *dposp;
1202 struct vattr *vaper;
1203{
1204 register struct vnode *vp = *vpp;
1205 register struct vattr *vap;
1206 register struct nfs_fattr *fp;
1207 register struct nfsnode *np;
1208 register long t1;
1209 caddr_t cp2;
1210 int error = 0, rdev;
1211 struct mbuf *md;
1212 enum vtype vtyp;
1213 u_short vmode;
1214 struct timespec mtime;
1215 struct vnode *nvp;
1216 int v3 = NFS_ISV3(vp);
1217
1218 md = *mdp;
1219 t1 = (mtod(md, caddr_t) + md->m_len) - *dposp;
1220 if (error = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, &cp2))
1221 return (error);
1222 fp = (struct nfs_fattr *)cp2;
1223 if (v3) {
1224 vtyp = nfsv3tov_type(fp->fa_type);
1225 vmode = fxdr_unsigned(u_short, fp->fa_mode);
1226 rdev = makedev(fxdr_unsigned(int, fp->fa3_rdev.specdata1),
1227 fxdr_unsigned(int, fp->fa3_rdev.specdata2));
1228 fxdr_nfsv3time(&fp->fa3_mtime, &mtime);
1229 } else {
1230 vtyp = nfsv2tov_type(fp->fa_type);
1231 vmode = fxdr_unsigned(u_short, fp->fa_mode);
1232 /*
1233 * XXX
1234 *
1235 * The duplicate information returned in fa_type and fa_mode
1236 * is an ambiguity in the NFS version 2 protocol.
1237 *
1238 * VREG should be taken literally as a regular file. If a
1239 * server intents to return some type information differently
1240 * in the upper bits of the mode field (e.g. for sockets, or
1241 * FIFOs), NFSv2 mandates fa_type to be VNON. Anyway, we
1242 * leave the examination of the mode bits even in the VREG
1243 * case to avoid breakage for bogus servers, but we make sure
1244 * that there are actually type bits set in the upper part of
1245 * fa_mode (and failing that, trust the va_type field).
1246 *
1247 * NFSv3 cleared the issue, and requires fa_mode to not
1248 * contain any type information (while also introduing sockets
1249 * and FIFOs for fa_type).
1250 */
1251 if (vtyp == VNON || (vtyp == VREG && (vmode & S_IFMT) != 0))
1252 vtyp = IFTOVT(vmode);
1253 rdev = fxdr_unsigned(long, fp->fa2_rdev);
1254 fxdr_nfsv2time(&fp->fa2_mtime, &mtime);
1255
1256 /*
1257 * Really ugly NFSv2 kludge.
1258 */
1259 if (vtyp == VCHR && rdev == 0xffffffff)
1260 vtyp = VFIFO;
1261 }
1262
1263 /*
1264 * If v_type == VNON it is a new node, so fill in the v_type,
1265 * n_mtime fields. Check to see if it represents a special
1266 * device, and if so, check for a possible alias. Once the
1267 * correct vnode has been obtained, fill in the rest of the
1268 * information.
1269 */
1270 np = VTONFS(vp);
1271 if (vp->v_type != vtyp) {
1272 vp->v_type = vtyp;
1273 if (vp->v_type == VFIFO) {
1274 vp->v_op = fifo_nfsv2nodeop_p;
1275 }
1276 if (vp->v_type == VCHR || vp->v_type == VBLK) {
1277 vp->v_op = spec_nfsv2nodeop_p;
1278 nvp = checkalias(vp, (dev_t)rdev, vp->v_mount);
1279 if (nvp) {
1280 /*
1281 * Discard unneeded vnode, but save its nfsnode.
1282 * Since the nfsnode does not have a lock, its
1283 * vnode lock has to be carried over.
1284 */
1285 nvp->v_vnlock = vp->v_vnlock;
1286 vp->v_vnlock = NULL;
1287 nvp->v_data = vp->v_data;
1288 vp->v_data = NULL;
1289 vp->v_op = spec_vnodeop_p;
1290 vrele(vp);
1291 vgone(vp);
1292 /*
1293 * Reinitialize aliased node.
1294 */
1295 np->n_vnode = nvp;
1296 *vpp = vp = nvp;
1297 }
1298 }
1299 np->n_mtime = mtime.tv_sec;
1300 }
1301 vap = &np->n_vattr;
1302 vap->va_type = vtyp;
1303 vap->va_mode = (vmode & 07777);
1304 vap->va_rdev = (dev_t)rdev;
1305 vap->va_mtime = mtime;
1306 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
1307 if (v3) {
1308 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1309 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
1310 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
1311 fxdr_hyper(&fp->fa3_size, &vap->va_size);
1312 vap->va_blocksize = NFS_FABLKSIZE;
1313 fxdr_hyper(&fp->fa3_used, &vap->va_bytes);
1314 vap->va_fileid = fxdr_unsigned(int, fp->fa3_fileid.nfsuquad[1]);
1315 fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime);
1316 fxdr_nfsv3time(&fp->fa3_ctime, &vap->va_ctime);
1317 vap->va_flags = 0;
1318 vap->va_filerev = 0;
1319 } else {
1320 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1321 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
1322 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
1323 vap->va_size = fxdr_unsigned(u_long, fp->fa2_size);
1324 vap->va_blocksize = fxdr_unsigned(long, fp->fa2_blocksize);
1325 vap->va_bytes = fxdr_unsigned(long, fp->fa2_blocks) * NFS_FABLKSIZE;
1326 vap->va_fileid = fxdr_unsigned(long, fp->fa2_fileid);
1327 fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime);
1328 vap->va_flags = 0;
1329 vap->va_ctime.tv_sec = fxdr_unsigned(long, fp->fa2_ctime.nfsv2_sec);
1330 vap->va_ctime.tv_nsec = 0;
1331 vap->va_gen = fxdr_unsigned(u_long, fp->fa2_ctime.nfsv2_usec);
1332 vap->va_filerev = 0;
1333 }
1334 if (vap->va_size != np->n_size) {
1335 if (vap->va_type == VREG) {
1336 if (np->n_flag & NMODIFIED) {
1337 if (vap->va_size < np->n_size)
1338 vap->va_size = np->n_size;
1339 else
1340 np->n_size = vap->va_size;
1341 } else
1342 np->n_size = vap->va_size;
1343 vnode_pager_setsize(vp, (u_long)np->n_size);
1344 } else
1345 np->n_size = vap->va_size;
1346 }
1347 np->n_attrstamp = time_second;
1348 if (vaper != NULL) {
1349 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap));
1350 if (np->n_flag & NCHG) {
1351 if (np->n_flag & NACC)
1352 vaper->va_atime = np->n_atim;
1353 if (np->n_flag & NUPD)
1354 vaper->va_mtime = np->n_mtim;
1355 }
1356 }
1357 return (0);
1358}
1359
1360#ifdef NFS_ACDEBUG
1361#include <sys/sysctl.h>
1362static int nfs_acdebug;
1363SYSCTL_INT(_vfs_nfs, OID_AUTO, acdebug, CTLFLAG_RW, &nfs_acdebug, 0, "");
1364#endif
1365
1366/*
1367 * Check the time stamp
1368 * If the cache is valid, copy contents to *vap and return 0
1369 * otherwise return an error
1370 */
1371int
1372nfs_getattrcache(vp, vaper)
1373 register struct vnode *vp;
1374 struct vattr *vaper;
1375{
1376 register struct nfsnode *np;
1377 register struct vattr *vap;
1378 struct nfsmount *nmp;
1379 int timeo;
1380
1381 np = VTONFS(vp);
1382 vap = &np->n_vattr;
1383 nmp = VFSTONFS(vp->v_mount);
1384 /* XXX n_mtime doesn't seem to be updated on a miss-and-reload */
1385 timeo = (time_second - np->n_mtime) / 10;
1386
1387#ifdef NFS_ACDEBUG
1388 if (nfs_acdebug>1)
1389 printf("nfs_getattrcache: initial timeo = %d\n", timeo);
1390#endif
1391
1392 if (vap->va_type == VDIR) {
1393 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acdirmin)
1394 timeo = nmp->nm_acdirmin;
1395 else if (timeo > nmp->nm_acdirmax)
1396 timeo = nmp->nm_acdirmax;
1397 } else {
1398 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acregmin)
1399 timeo = nmp->nm_acregmin;
1400 else if (timeo > nmp->nm_acregmax)
1401 timeo = nmp->nm_acregmax;
1402 }
1403
1404#ifdef NFS_ACDEBUG
1405 if (nfs_acdebug > 2)
1406 printf("acregmin %d; acregmax %d; acdirmin %d; acdirmax %d\n",
1407 nmp->nm_acregmin, nmp->nm_acregmax,
1408 nmp->nm_acdirmin, nmp->nm_acdirmax);
1409
1410 if (nfs_acdebug)
1411 printf("nfs_getattrcache: age = %d; final timeo = %d\n",r
1412 (time_second - np->n_attrstamp), timeo);
1413#endif
1414
1415 if ((time_second - np->n_attrstamp) >= timeo) {
1416 nfsstats.attrcache_misses++;
1417 return (ENOENT);
1418 }
1419 nfsstats.attrcache_hits++;
1420 if (vap->va_size != np->n_size) {
1421 if (vap->va_type == VREG) {
1422 if (np->n_flag & NMODIFIED) {
1423 if (vap->va_size < np->n_size)
1424 vap->va_size = np->n_size;
1425 else
1426 np->n_size = vap->va_size;
1427 } else
1428 np->n_size = vap->va_size;
1429 vnode_pager_setsize(vp, (u_long)np->n_size);
1430 } else
1431 np->n_size = vap->va_size;
1432 }
1433 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr));
1434 if (np->n_flag & NCHG) {
1435 if (np->n_flag & NACC)
1436 vaper->va_atime = np->n_atim;
1437 if (np->n_flag & NUPD)
1438 vaper->va_mtime = np->n_mtim;
1439 }
1440 return (0);
1441}
1442
1443#ifndef NFS_NOSERVER
1444/*
1445 * Set up nameidata for a lookup() call and do it.
1446 *
1447 * If pubflag is set, this call is done for a lookup operation on the
1448 * public filehandle. In that case we allow crossing mountpoints and
1449 * absolute pathnames. However, the caller is expected to check that
1450 * the lookup result is within the public fs, and deny access if
1451 * it is not.
1452 */
1453int
1454nfs_namei(ndp, fhp, len, slp, nam, mdp, dposp, retdirp, p, kerbflag, pubflag)
1455 register struct nameidata *ndp;
1456 fhandle_t *fhp;
1457 int len;
1458 struct nfssvc_sock *slp;
1459 struct sockaddr *nam;
1460 struct mbuf **mdp;
1461 caddr_t *dposp;
1462 struct vnode **retdirp;
1463 struct proc *p;
1464 int kerbflag, pubflag;
1465{
1466 register int i, rem;
1467 register struct mbuf *md;
1468 register char *fromcp, *tocp, *cp;
1469 struct iovec aiov;
1470 struct uio auio;
1471 struct vnode *dp;
1472 int error, rdonly, linklen;
1473 struct componentname *cnp = &ndp->ni_cnd;
1474
1475 *retdirp = (struct vnode *)0;
1476 cnp->cn_pnbuf = zalloc(namei_zone);
1477
1478 /*
1479 * Copy the name from the mbuf list to ndp->ni_pnbuf
1480 * and set the various ndp fields appropriately.
1481 */
1482 fromcp = *dposp;
1483 tocp = cnp->cn_pnbuf;
1484 md = *mdp;
1485 rem = mtod(md, caddr_t) + md->m_len - fromcp;
1486 cnp->cn_hash = 0;
1487 for (i = 0; i < len; i++) {
1488 while (rem == 0) {
1489 md = md->m_next;
1490 if (md == NULL) {
1491 error = EBADRPC;
1492 goto out;
1493 }
1494 fromcp = mtod(md, caddr_t);
1495 rem = md->m_len;
1496 }
1497 if (*fromcp == '\0' || (!pubflag && *fromcp == '/')) {
1498 error = EACCES;
1499 goto out;
1500 }
1501 cnp->cn_hash += (unsigned char)*fromcp;
1502 *tocp++ = *fromcp++;
1503 rem--;
1504 }
1505 *tocp = '\0';
1506 *mdp = md;
1507 *dposp = fromcp;
1508 len = nfsm_rndup(len)-len;
1509 if (len > 0) {
1510 if (rem >= len)
1511 *dposp += len;
1512 else if ((error = nfs_adv(mdp, dposp, len, rem)) != 0)
1513 goto out;
1514 }
1515
1516 /*
1517 * Extract and set starting directory.
1518 */
1519 error = nfsrv_fhtovp(fhp, FALSE, &dp, ndp->ni_cnd.cn_cred, slp,
1520 nam, &rdonly, kerbflag, pubflag);
1521 if (error)
1522 goto out;
1523 if (dp->v_type != VDIR) {
1524 vrele(dp);
1525 error = ENOTDIR;
1526 goto out;
1527 }
1528
1529 if (rdonly)
1530 cnp->cn_flags |= RDONLY;
1531
1532 *retdirp = dp;
1533
1534 if (pubflag) {
1535 /*
1536 * Oh joy. For WebNFS, handle those pesky '%' escapes,
1537 * and the 'native path' indicator.
1538 */
1539 cp = zalloc(namei_zone);
1540 fromcp = cnp->cn_pnbuf;
1541 tocp = cp;
1542 if ((unsigned char)*fromcp >= WEBNFS_SPECCHAR_START) {
1543 switch ((unsigned char)*fromcp) {
1544 case WEBNFS_NATIVE_CHAR:
1545 /*
1546 * 'Native' path for us is the same
1547 * as a path according to the NFS spec,
1548 * just skip the escape char.
1549 */
1550 fromcp++;
1551 break;
1552 /*
1553 * More may be added in the future, range 0x80-0xff
1554 */
1555 default:
1556 error = EIO;
1557 zfree(namei_zone, cp);
1558 goto out;
1559 }
1560 }
1561 /*
1562 * Translate the '%' escapes, URL-style.
1563 */
1564 while (*fromcp != '\0') {
1565 if (*fromcp == WEBNFS_ESC_CHAR) {
1566 if (fromcp[1] != '\0' && fromcp[2] != '\0') {
1567 fromcp++;
1568 *tocp++ = HEXSTRTOI(fromcp);
1569 fromcp += 2;
1570 continue;
1571 } else {
1572 error = ENOENT;
1573 zfree(namei_zone, cp);
1574 goto out;
1575 }
1576 } else
1577 *tocp++ = *fromcp++;
1578 }
1579 *tocp = '\0';
1580 zfree(namei_zone, cnp->cn_pnbuf);
1581 cnp->cn_pnbuf = cp;
1582 }
1583
1584 ndp->ni_pathlen = (tocp - cnp->cn_pnbuf) + 1;
1585 ndp->ni_segflg = UIO_SYSSPACE;
1586
1587 if (pubflag) {
1588 ndp->ni_rootdir = rootvnode;
1589 ndp->ni_loopcnt = 0;
1590 if (cnp->cn_pnbuf[0] == '/')
1591 dp = rootvnode;
1592 } else {
1593 cnp->cn_flags |= NOCROSSMOUNT;
1594 }
1595
1596 cnp->cn_proc = p;
1597 VREF(dp);
1598
1599 for (;;) {
1600 cnp->cn_nameptr = cnp->cn_pnbuf;
1601 ndp->ni_startdir = dp;
1602 /*
1603 * And call lookup() to do the real work
1604 */
1605 error = lookup(ndp);
1606 if (error)
1607 break;
1608 /*
1609 * Check for encountering a symbolic link
1610 */
1611 if ((cnp->cn_flags & ISSYMLINK) == 0) {
1612 nfsrv_object_create(ndp->ni_vp);
1613 if (cnp->cn_flags & (SAVENAME | SAVESTART)) {
1614 cnp->cn_flags |= HASBUF;
1615 return (0);
1616 }
1617 break;
1618 } else {
1619 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
1620 VOP_UNLOCK(ndp->ni_dvp, 0, p);
1621 if (!pubflag) {
1622 vrele(ndp->ni_dvp);
1623 vput(ndp->ni_vp);
1624 ndp->ni_vp = NULL;
1625 error = EINVAL;
1626 break;
1627 }
1628
1629 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
1630 error = ELOOP;
1631 break;
1632 }
1633 if (ndp->ni_pathlen > 1)
1634 cp = zalloc(namei_zone);
1635 else
1636 cp = cnp->cn_pnbuf;
1637 aiov.iov_base = cp;
1638 aiov.iov_len = MAXPATHLEN;
1639 auio.uio_iov = &aiov;
1640 auio.uio_iovcnt = 1;
1641 auio.uio_offset = 0;
1642 auio.uio_rw = UIO_READ;
1643 auio.uio_segflg = UIO_SYSSPACE;
1644 auio.uio_procp = (struct proc *)0;
1645 auio.uio_resid = MAXPATHLEN;
1646 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
1647 if (error) {
1648 badlink:
1649 if (ndp->ni_pathlen > 1)
1650 zfree(namei_zone, cp);
1651 break;
1652 }
1653 linklen = MAXPATHLEN - auio.uio_resid;
1654 if (linklen == 0) {
1655 error = ENOENT;
1656 goto badlink;
1657 }
1658 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
1659 error = ENAMETOOLONG;
1660 goto badlink;
1661 }
1662 if (ndp->ni_pathlen > 1) {
1663 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
1664 zfree(namei_zone, cnp->cn_pnbuf);
1665 cnp->cn_pnbuf = cp;
1666 } else
1667 cnp->cn_pnbuf[linklen] = '\0';
1668 ndp->ni_pathlen += linklen;
1669 vput(ndp->ni_vp);
1670 dp = ndp->ni_dvp;
1671 /*
1672 * Check if root directory should replace current directory.
1673 */
1674 if (cnp->cn_pnbuf[0] == '/') {
1675 vrele(dp);
1676 dp = ndp->ni_rootdir;
1677 VREF(dp);
1678 }
1679 }
1680 }
1681out:
1682 zfree(namei_zone, cnp->cn_pnbuf);
1683 return (error);
1684}
1685
1686/*
1687 * A fiddled version of m_adj() that ensures null fill to a long
1688 * boundary and only trims off the back end
1689 */
1690void
1691nfsm_adj(mp, len, nul)
1692 struct mbuf *mp;
1693 register int len;
1694 int nul;
1695{
1696 register struct mbuf *m;
1697 register int count, i;
1698 register char *cp;
1699
1700 /*
1701 * Trim from tail. Scan the mbuf chain,
1702 * calculating its length and finding the last mbuf.
1703 * If the adjustment only affects this mbuf, then just
1704 * adjust and return. Otherwise, rescan and truncate
1705 * after the remaining size.
1706 */
1707 count = 0;
1708 m = mp;
1709 for (;;) {
1710 count += m->m_len;
1711 if (m->m_next == (struct mbuf *)0)
1712 break;
1713 m = m->m_next;
1714 }
1715 if (m->m_len > len) {
1716 m->m_len -= len;
1717 if (nul > 0) {
1718 cp = mtod(m, caddr_t)+m->m_len-nul;
1719 for (i = 0; i < nul; i++)
1720 *cp++ = '\0';
1721 }
1722 return;
1723 }
1724 count -= len;
1725 if (count < 0)
1726 count = 0;
1727 /*
1728 * Correct length for chain is "count".
1729 * Find the mbuf with last data, adjust its length,
1730 * and toss data from remaining mbufs on chain.
1731 */
1732 for (m = mp; m; m = m->m_next) {
1733 if (m->m_len >= count) {
1734 m->m_len = count;
1735 if (nul > 0) {
1736 cp = mtod(m, caddr_t)+m->m_len-nul;
1737 for (i = 0; i < nul; i++)
1738 *cp++ = '\0';
1739 }
1740 break;
1741 }
1742 count -= m->m_len;
1743 }
1744 for (m = m->m_next;m;m = m->m_next)
1745 m->m_len = 0;
1746}
1747
1748/*
1749 * Make these functions instead of macros, so that the kernel text size
1750 * doesn't get too big...
1751 */
1752void
1753nfsm_srvwcc(nfsd, before_ret, before_vap, after_ret, after_vap, mbp, bposp)
1754 struct nfsrv_descript *nfsd;
1755 int before_ret;
1756 register struct vattr *before_vap;
1757 int after_ret;
1758 struct vattr *after_vap;
1759 struct mbuf **mbp;
1760 char **bposp;
1761{
1762 register struct mbuf *mb = *mbp, *mb2;
1763 register char *bpos = *bposp;
1764 register u_long *tl;
1765
1766 if (before_ret) {
1767 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
1768 *tl = nfs_false;
1769 } else {
1770 nfsm_build(tl, u_long *, 7 * NFSX_UNSIGNED);
1771 *tl++ = nfs_true;
1772 txdr_hyper(&(before_vap->va_size), tl);
1773 tl += 2;
1774 txdr_nfsv3time(&(before_vap->va_mtime), tl);
1775 tl += 2;
1776 txdr_nfsv3time(&(before_vap->va_ctime), tl);
1777 }
1778 *bposp = bpos;
1779 *mbp = mb;
1780 nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp);
1781}
1782
1783void
1784nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp)
1785 struct nfsrv_descript *nfsd;
1786 int after_ret;
1787 struct vattr *after_vap;
1788 struct mbuf **mbp;
1789 char **bposp;
1790{
1791 register struct mbuf *mb = *mbp, *mb2;
1792 register char *bpos = *bposp;
1793 register u_long *tl;
1794 register struct nfs_fattr *fp;
1795
1796 if (after_ret) {
1797 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
1798 *tl = nfs_false;
1799 } else {
1800 nfsm_build(tl, u_long *, NFSX_UNSIGNED + NFSX_V3FATTR);
1801 *tl++ = nfs_true;
1802 fp = (struct nfs_fattr *)tl;
1803 nfsm_srvfattr(nfsd, after_vap, fp);
1804 }
1805 *mbp = mb;
1806 *bposp = bpos;
1807}
1808
1809void
1810nfsm_srvfattr(nfsd, vap, fp)
1811 register struct nfsrv_descript *nfsd;
1812 register struct vattr *vap;
1813 register struct nfs_fattr *fp;
1814{
1815
1816 fp->fa_nlink = txdr_unsigned(vap->va_nlink);
1817 fp->fa_uid = txdr_unsigned(vap->va_uid);
1818 fp->fa_gid = txdr_unsigned(vap->va_gid);
1819 if (nfsd->nd_flag & ND_NFSV3) {
1820 fp->fa_type = vtonfsv3_type(vap->va_type);
1821 fp->fa_mode = vtonfsv3_mode(vap->va_mode);
1822 txdr_hyper(&vap->va_size, &fp->fa3_size);
1823 txdr_hyper(&vap->va_bytes, &fp->fa3_used);
1824 fp->fa3_rdev.specdata1 = txdr_unsigned(major(vap->va_rdev));
1825 fp->fa3_rdev.specdata2 = txdr_unsigned(minor(vap->va_rdev));
1826 fp->fa3_fsid.nfsuquad[0] = 0;
1827 fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(vap->va_fsid);
1828 fp->fa3_fileid.nfsuquad[0] = 0;
1829 fp->fa3_fileid.nfsuquad[1] = txdr_unsigned(vap->va_fileid);
1830 txdr_nfsv3time(&vap->va_atime, &fp->fa3_atime);
1831 txdr_nfsv3time(&vap->va_mtime, &fp->fa3_mtime);
1832 txdr_nfsv3time(&vap->va_ctime, &fp->fa3_ctime);
1833 } else {
1834 fp->fa_type = vtonfsv2_type(vap->va_type);
1835 fp->fa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1836 fp->fa2_size = txdr_unsigned(vap->va_size);
1837 fp->fa2_blocksize = txdr_unsigned(vap->va_blocksize);
1838 if (vap->va_type == VFIFO)
1839 fp->fa2_rdev = 0xffffffff;
1840 else
1841 fp->fa2_rdev = txdr_unsigned(vap->va_rdev);
1842 fp->fa2_blocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE);
1843 fp->fa2_fsid = txdr_unsigned(vap->va_fsid);
1844 fp->fa2_fileid = txdr_unsigned(vap->va_fileid);
1845 txdr_nfsv2time(&vap->va_atime, &fp->fa2_atime);
1846 txdr_nfsv2time(&vap->va_mtime, &fp->fa2_mtime);
1847 txdr_nfsv2time(&vap->va_ctime, &fp->fa2_ctime);
1848 }
1849}
1850
1851/*
1852 * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked)
1853 * - look up fsid in mount list (if not found ret error)
1854 * - get vp and export rights by calling VFS_FHTOVP()
1855 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
1856 * - if not lockflag unlock it with VOP_UNLOCK()
1857 */
1858int
1859nfsrv_fhtovp(fhp, lockflag, vpp, cred, slp, nam, rdonlyp, kerbflag, pubflag)
1860 fhandle_t *fhp;
1861 int lockflag;
1862 struct vnode **vpp;
1863 struct ucred *cred;
1864 struct nfssvc_sock *slp;
1865 struct sockaddr *nam;
1866 int *rdonlyp;
1867 int kerbflag;
1868 int pubflag;
1869{
1870 struct proc *p = curproc; /* XXX */
1871 register struct mount *mp;
1872 register int i;
1873 struct ucred *credanon;
1874 int error, exflags;
1875
1876 *vpp = (struct vnode *)0;
1877
1878 if (nfs_ispublicfh(fhp)) {
1879 if (!pubflag || !nfs_pub.np_valid)
1880 return (ESTALE);
1881 fhp = &nfs_pub.np_handle;
1882 }
1883
1884 mp = vfs_getvfs(&fhp->fh_fsid);
1885 if (!mp)
1886 return (ESTALE);
1887 error = VFS_FHTOVP(mp, &fhp->fh_fid, nam, vpp, &exflags, &credanon);
1888 if (error)
1889 return (error);
1890 /*
1891 * Check/setup credentials.
1892 */
1893 if (exflags & MNT_EXKERB) {
1894 if (!kerbflag) {
1895 vput(*vpp);
1896 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1897 }
1898 } else if (kerbflag) {
1899 vput(*vpp);
1900 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1901 } else if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
1902 cred->cr_uid = credanon->cr_uid;
1903 for (i = 0; i < credanon->cr_ngroups && i < NGROUPS; i++)
1904 cred->cr_groups[i] = credanon->cr_groups[i];
1905 cred->cr_ngroups = i;
1906 }
1907 if (exflags & MNT_EXRDONLY)
1908 *rdonlyp = 1;
1909 else
1910 *rdonlyp = 0;
1911
1912 nfsrv_object_create(*vpp);
1913
1914 if (!lockflag)
1915 VOP_UNLOCK(*vpp, 0, p);
1916 return (0);
1917}
1918
1919
1920/*
1921 * WebNFS: check if a filehandle is a public filehandle. For v3, this
1922 * means a length of 0, for v2 it means all zeroes. nfsm_srvmtofh has
1923 * transformed this to all zeroes in both cases, so check for it.
1924 */
1925int
1926nfs_ispublicfh(fhp)
1927 fhandle_t *fhp;
1928{
1929 char *cp = (char *)fhp;
1930 int i;
1931
1932 for (i = 0; i < NFSX_V3FH; i++)
1933 if (*cp++ != 0)
1934 return (FALSE);
1935 return (TRUE);
1936}
1937
1938#endif /* NFS_NOSERVER */
1939/*
1940 * This function compares two net addresses by family and returns TRUE
1941 * if they are the same host.
1942 * If there is any doubt, return FALSE.
1943 * The AF_INET family is handled as a special case so that address mbufs
1944 * don't need to be saved to store "struct in_addr", which is only 4 bytes.
1945 */
1946int
1947netaddr_match(family, haddr, nam)
1948 int family;
1949 union nethostaddr *haddr;
1950 struct sockaddr *nam;
1951{
1952 register struct sockaddr_in *inetaddr;
1953
1954 switch (family) {
1955 case AF_INET:
1956 inetaddr = (struct sockaddr_in *)nam;
1957 if (inetaddr->sin_family == AF_INET &&
1958 inetaddr->sin_addr.s_addr == haddr->had_inetaddr)
1959 return (1);
1960 break;
1961#ifdef ISO
1962 case AF_ISO:
1963 {
1964 register struct sockaddr_iso *isoaddr1, *isoaddr2;
1965
1966 isoaddr1 = (struct sockaddr_iso *)nam;
1967 isoaddr2 = (struct sockaddr_iso *)haddr->had_nam;
1968 if (isoaddr1->siso_family == AF_ISO &&
1969 isoaddr1->siso_nlen > 0 &&
1970 isoaddr1->siso_nlen == isoaddr2->siso_nlen &&
1971 SAME_ISOADDR(isoaddr1, isoaddr2))
1972 return (1);
1973 break;
1974 }
1975#endif /* ISO */
1976 default:
1977 break;
1978 };
1979 return (0);
1980}
1981
1982static nfsuint64 nfs_nullcookie = { 0, 0 };
1983/*
1984 * This function finds the directory cookie that corresponds to the
1985 * logical byte offset given.
1986 */
1987nfsuint64 *
1988nfs_getcookie(np, off, add)
1989 register struct nfsnode *np;
1990 off_t off;
1991 int add;
1992{
1993 register struct nfsdmap *dp, *dp2;
1994 register int pos;
1995
1996 pos = off / NFS_DIRBLKSIZ;
1997 if (pos == 0) {
1998#ifdef DIAGNOSTIC
1999 if (add)
2000 panic("nfs getcookie add at 0");
2001#endif
2002 return (&nfs_nullcookie);
2003 }
2004 pos--;
2005 dp = np->n_cookies.lh_first;
2006 if (!dp) {
2007 if (add) {
2008 MALLOC(dp, struct nfsdmap *, sizeof (struct nfsdmap),
2009 M_NFSDIROFF, M_WAITOK);
2010 dp->ndm_eocookie = 0;
2011 LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list);
2012 } else
2013 return ((nfsuint64 *)0);
2014 }
2015 while (pos >= NFSNUMCOOKIES) {
2016 pos -= NFSNUMCOOKIES;
2017 if (dp->ndm_list.le_next) {
2018 if (!add && dp->ndm_eocookie < NFSNUMCOOKIES &&
2019 pos >= dp->ndm_eocookie)
2020 return ((nfsuint64 *)0);
2021 dp = dp->ndm_list.le_next;
2022 } else if (add) {
2023 MALLOC(dp2, struct nfsdmap *, sizeof (struct nfsdmap),
2024 M_NFSDIROFF, M_WAITOK);
2025 dp2->ndm_eocookie = 0;
2026 LIST_INSERT_AFTER(dp, dp2, ndm_list);
2027 dp = dp2;
2028 } else
2029 return ((nfsuint64 *)0);
2030 }
2031 if (pos >= dp->ndm_eocookie) {
2032 if (add)
2033 dp->ndm_eocookie = pos + 1;
2034 else
2035 return ((nfsuint64 *)0);
2036 }
2037 return (&dp->ndm_cookies[pos]);
2038}
2039
2040/*
2041 * Invalidate cached directory information, except for the actual directory
2042 * blocks (which are invalidated separately).
2043 * Done mainly to avoid the use of stale offset cookies.
2044 */
2045void
2046nfs_invaldir(vp)
2047 register struct vnode *vp;
2048{
2049 register struct nfsnode *np = VTONFS(vp);
2050
2051#ifdef DIAGNOSTIC
2052 if (vp->v_type != VDIR)
2053 panic("nfs: invaldir not dir");
2054#endif
2055 np->n_direofoffset = 0;
2056 np->n_cookieverf.nfsuquad[0] = 0;
2057 np->n_cookieverf.nfsuquad[1] = 0;
2058 if (np->n_cookies.lh_first)
2059 np->n_cookies.lh_first->ndm_eocookie = 0;
2060}
2061
2062/*
2063 * The write verifier has changed (probably due to a server reboot), so all
2064 * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
2065 * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
2066 * flag. Once done the new write verifier can be set for the mount point.
2067 */
2068void
2069nfs_clearcommit(mp)
2070 struct mount *mp;
2071{
2072 register struct vnode *vp, *nvp;
2073 register struct buf *bp, *nbp;
2074 int s;
2075
2076 s = splbio();
2077loop:
2078 for (vp = mp->mnt_vnodelist.lh_first; vp; vp = nvp) {
2079 if (vp->v_mount != mp) /* Paranoia */
2080 goto loop;
2081 nvp = vp->v_mntvnodes.le_next;
2082 for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = nbp) {
2083 nbp = bp->b_vnbufs.le_next;
2084 if ((bp->b_flags & (B_BUSY | B_DELWRI | B_NEEDCOMMIT))
2085 == (B_DELWRI | B_NEEDCOMMIT))
2086 bp->b_flags &= ~B_NEEDCOMMIT;
2087 }
2088 }
2089 splx(s);
2090}
2091
2092#ifndef NFS_NOSERVER
2093/*
2094 * Map errnos to NFS error numbers. For Version 3 also filter out error
2095 * numbers not specified for the associated procedure.
2096 */
2097int
2098nfsrv_errmap(nd, err)
2099 struct nfsrv_descript *nd;
2100 register int err;
2101{
2102 register short *defaulterrp, *errp;
2103
2104 if (nd->nd_flag & ND_NFSV3) {
2105 if (nd->nd_procnum <= NFSPROC_COMMIT) {
2106 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
2107 while (*++errp) {
2108 if (*errp == err)
2109 return (err);
2110 else if (*errp > err)
2111 break;
2112 }
2113 return ((int)*defaulterrp);
2114 } else
2115 return (err & 0xffff);
2116 }
2117 if (err <= ELAST)
2118 return ((int)nfsrv_v2errmap[err - 1]);
2119 return (NFSERR_IO);
2120}
2121
2122int
2123nfsrv_object_create(vp)
2124 struct vnode *vp;
2125{
2126
2127 if (vp == NULL || vp->v_type != VREG)
2128 return (1);
2129 return (vfs_object_create(vp, curproc,
2130 curproc ? curproc->p_ucred : NULL, 1));
2131}
2132
2133/*
2134 * Sort the group list in increasing numerical order.
2135 * (Insertion sort by Chris Torek, who was grossed out by the bubble sort
2136 * that used to be here.)
2137 */
2138void
2139nfsrvw_sort(list, num)
2140 register gid_t *list;
2141 register int num;
2142{
2143 register int i, j;
2144 gid_t v;
2145
2146 /* Insertion sort. */
2147 for (i = 1; i < num; i++) {
2148 v = list[i];
2149 /* find correct slot for value v, moving others up */
2150 for (j = i; --j >= 0 && v < list[j];)
2151 list[j + 1] = list[j];
2152 list[j + 1] = v;
2153 }
2154}
2155
2156/*
2157 * copy credentials making sure that the result can be compared with bcmp().
2158 */
2159void
2160nfsrv_setcred(incred, outcred)
2161 register struct ucred *incred, *outcred;
2162{
2163 register int i;
2164
2165 bzero((caddr_t)outcred, sizeof (struct ucred));
2166 outcred->cr_ref = 1;
2167 outcred->cr_uid = incred->cr_uid;
2168 outcred->cr_ngroups = incred->cr_ngroups;
2169 for (i = 0; i < incred->cr_ngroups; i++)
2170 outcred->cr_groups[i] = incred->cr_groups[i];
2171 nfsrvw_sort(outcred->cr_groups, outcred->cr_ngroups);
2172}
2173#endif /* NFS_NOSERVER */