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