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