nfs_subs.c revision 1.67 1 /* $NetBSD: nfs_subs.c,v 1.67 1999/03/16 23:22:57 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 0,
305 };
306
307 static short nfsv3err_create[] = {
308 NFSERR_IO,
309 NFSERR_IO,
310 NFSERR_ACCES,
311 NFSERR_EXIST,
312 NFSERR_NOTDIR,
313 NFSERR_NOSPC,
314 NFSERR_ROFS,
315 NFSERR_NAMETOL,
316 NFSERR_DQUOT,
317 NFSERR_STALE,
318 NFSERR_BADHANDLE,
319 NFSERR_NOTSUPP,
320 NFSERR_SERVERFAULT,
321 0,
322 };
323
324 static short nfsv3err_mkdir[] = {
325 NFSERR_IO,
326 NFSERR_IO,
327 NFSERR_ACCES,
328 NFSERR_EXIST,
329 NFSERR_NOTDIR,
330 NFSERR_NOSPC,
331 NFSERR_ROFS,
332 NFSERR_NAMETOL,
333 NFSERR_DQUOT,
334 NFSERR_STALE,
335 NFSERR_BADHANDLE,
336 NFSERR_NOTSUPP,
337 NFSERR_SERVERFAULT,
338 0,
339 };
340
341 static short nfsv3err_symlink[] = {
342 NFSERR_IO,
343 NFSERR_IO,
344 NFSERR_ACCES,
345 NFSERR_EXIST,
346 NFSERR_NOTDIR,
347 NFSERR_NOSPC,
348 NFSERR_ROFS,
349 NFSERR_NAMETOL,
350 NFSERR_DQUOT,
351 NFSERR_STALE,
352 NFSERR_BADHANDLE,
353 NFSERR_NOTSUPP,
354 NFSERR_SERVERFAULT,
355 0,
356 };
357
358 static short nfsv3err_mknod[] = {
359 NFSERR_IO,
360 NFSERR_IO,
361 NFSERR_ACCES,
362 NFSERR_EXIST,
363 NFSERR_NOTDIR,
364 NFSERR_NOSPC,
365 NFSERR_ROFS,
366 NFSERR_NAMETOL,
367 NFSERR_DQUOT,
368 NFSERR_STALE,
369 NFSERR_BADHANDLE,
370 NFSERR_NOTSUPP,
371 NFSERR_SERVERFAULT,
372 NFSERR_BADTYPE,
373 0,
374 };
375
376 static short nfsv3err_remove[] = {
377 NFSERR_IO,
378 NFSERR_NOENT,
379 NFSERR_IO,
380 NFSERR_ACCES,
381 NFSERR_NOTDIR,
382 NFSERR_ROFS,
383 NFSERR_NAMETOL,
384 NFSERR_STALE,
385 NFSERR_BADHANDLE,
386 NFSERR_SERVERFAULT,
387 0,
388 };
389
390 static short nfsv3err_rmdir[] = {
391 NFSERR_IO,
392 NFSERR_NOENT,
393 NFSERR_IO,
394 NFSERR_ACCES,
395 NFSERR_EXIST,
396 NFSERR_NOTDIR,
397 NFSERR_INVAL,
398 NFSERR_ROFS,
399 NFSERR_NAMETOL,
400 NFSERR_NOTEMPTY,
401 NFSERR_STALE,
402 NFSERR_BADHANDLE,
403 NFSERR_NOTSUPP,
404 NFSERR_SERVERFAULT,
405 0,
406 };
407
408 static short nfsv3err_rename[] = {
409 NFSERR_IO,
410 NFSERR_NOENT,
411 NFSERR_IO,
412 NFSERR_ACCES,
413 NFSERR_EXIST,
414 NFSERR_XDEV,
415 NFSERR_NOTDIR,
416 NFSERR_ISDIR,
417 NFSERR_INVAL,
418 NFSERR_NOSPC,
419 NFSERR_ROFS,
420 NFSERR_MLINK,
421 NFSERR_NAMETOL,
422 NFSERR_NOTEMPTY,
423 NFSERR_DQUOT,
424 NFSERR_STALE,
425 NFSERR_BADHANDLE,
426 NFSERR_NOTSUPP,
427 NFSERR_SERVERFAULT,
428 0,
429 };
430
431 static short nfsv3err_link[] = {
432 NFSERR_IO,
433 NFSERR_IO,
434 NFSERR_ACCES,
435 NFSERR_EXIST,
436 NFSERR_XDEV,
437 NFSERR_NOTDIR,
438 NFSERR_INVAL,
439 NFSERR_NOSPC,
440 NFSERR_ROFS,
441 NFSERR_MLINK,
442 NFSERR_NAMETOL,
443 NFSERR_DQUOT,
444 NFSERR_STALE,
445 NFSERR_BADHANDLE,
446 NFSERR_NOTSUPP,
447 NFSERR_SERVERFAULT,
448 0,
449 };
450
451 static short nfsv3err_readdir[] = {
452 NFSERR_IO,
453 NFSERR_IO,
454 NFSERR_ACCES,
455 NFSERR_NOTDIR,
456 NFSERR_STALE,
457 NFSERR_BADHANDLE,
458 NFSERR_BAD_COOKIE,
459 NFSERR_TOOSMALL,
460 NFSERR_SERVERFAULT,
461 0,
462 };
463
464 static short nfsv3err_readdirplus[] = {
465 NFSERR_IO,
466 NFSERR_IO,
467 NFSERR_ACCES,
468 NFSERR_NOTDIR,
469 NFSERR_STALE,
470 NFSERR_BADHANDLE,
471 NFSERR_BAD_COOKIE,
472 NFSERR_NOTSUPP,
473 NFSERR_TOOSMALL,
474 NFSERR_SERVERFAULT,
475 0,
476 };
477
478 static short nfsv3err_fsstat[] = {
479 NFSERR_IO,
480 NFSERR_IO,
481 NFSERR_STALE,
482 NFSERR_BADHANDLE,
483 NFSERR_SERVERFAULT,
484 0,
485 };
486
487 static short nfsv3err_fsinfo[] = {
488 NFSERR_STALE,
489 NFSERR_STALE,
490 NFSERR_BADHANDLE,
491 NFSERR_SERVERFAULT,
492 0,
493 };
494
495 static short nfsv3err_pathconf[] = {
496 NFSERR_STALE,
497 NFSERR_STALE,
498 NFSERR_BADHANDLE,
499 NFSERR_SERVERFAULT,
500 0,
501 };
502
503 static short nfsv3err_commit[] = {
504 NFSERR_IO,
505 NFSERR_IO,
506 NFSERR_STALE,
507 NFSERR_BADHANDLE,
508 NFSERR_SERVERFAULT,
509 0,
510 };
511
512 static short *nfsrv_v3errmap[] = {
513 nfsv3err_null,
514 nfsv3err_getattr,
515 nfsv3err_setattr,
516 nfsv3err_lookup,
517 nfsv3err_access,
518 nfsv3err_readlink,
519 nfsv3err_read,
520 nfsv3err_write,
521 nfsv3err_create,
522 nfsv3err_mkdir,
523 nfsv3err_symlink,
524 nfsv3err_mknod,
525 nfsv3err_remove,
526 nfsv3err_rmdir,
527 nfsv3err_rename,
528 nfsv3err_link,
529 nfsv3err_readdir,
530 nfsv3err_readdirplus,
531 nfsv3err_fsstat,
532 nfsv3err_fsinfo,
533 nfsv3err_pathconf,
534 nfsv3err_commit,
535 };
536
537 extern struct nfsrtt nfsrtt;
538 extern time_t nqnfsstarttime;
539 extern int nqsrv_clockskew;
540 extern int nqsrv_writeslack;
541 extern int nqsrv_maxlease;
542 extern int nqnfs_piggy[NFS_NPROCS];
543 extern nfstype nfsv2_type[9];
544 extern nfstype nfsv3_type[9];
545 extern struct nfsnodehashhead *nfsnodehashtbl;
546 extern u_long nfsnodehash;
547
548 LIST_HEAD(nfsnodehashhead, nfsnode);
549 u_long nfsdirhashmask;
550
551 int nfs_webnamei __P((struct nameidata *, struct vnode *, struct proc *));
552
553 /*
554 * Create the header for an rpc request packet
555 * The hsiz is the size of the rest of the nfs request header.
556 * (just used to decide if a cluster is a good idea)
557 */
558 struct mbuf *
559 nfsm_reqh(vp, procid, hsiz, bposp)
560 struct vnode *vp;
561 u_long procid;
562 int hsiz;
563 caddr_t *bposp;
564 {
565 register struct mbuf *mb;
566 register u_int32_t *tl;
567 register caddr_t bpos;
568 struct mbuf *mb2;
569 struct nfsmount *nmp;
570 int nqflag;
571
572 MGET(mb, M_WAIT, MT_DATA);
573 if (hsiz >= MINCLSIZE)
574 MCLGET(mb, M_WAIT);
575 mb->m_len = 0;
576 bpos = mtod(mb, caddr_t);
577
578 /*
579 * For NQNFS, add lease request.
580 */
581 if (vp) {
582 nmp = VFSTONFS(vp->v_mount);
583 if (nmp->nm_flag & NFSMNT_NQNFS) {
584 nqflag = NQNFS_NEEDLEASE(vp, procid);
585 if (nqflag) {
586 nfsm_build(tl, u_int32_t *, 2*NFSX_UNSIGNED);
587 *tl++ = txdr_unsigned(nqflag);
588 *tl = txdr_unsigned(nmp->nm_leaseterm);
589 } else {
590 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
591 *tl = 0;
592 }
593 }
594 }
595 /* Finally, return values */
596 *bposp = bpos;
597 return (mb);
598 }
599
600 /*
601 * Build the RPC header and fill in the authorization info.
602 * The authorization string argument is only used when the credentials
603 * come from outside of the kernel.
604 * Returns the head of the mbuf list.
605 */
606 struct mbuf *
607 nfsm_rpchead(cr, nmflag, procid, auth_type, auth_len, auth_str, verf_len,
608 verf_str, mrest, mrest_len, mbp, xidp)
609 register struct ucred *cr;
610 int nmflag;
611 int procid;
612 int auth_type;
613 int auth_len;
614 char *auth_str;
615 int verf_len;
616 char *verf_str;
617 struct mbuf *mrest;
618 int mrest_len;
619 struct mbuf **mbp;
620 u_int32_t *xidp;
621 {
622 register struct mbuf *mb;
623 register u_int32_t *tl;
624 register caddr_t bpos;
625 register int i;
626 struct mbuf *mreq, *mb2;
627 int siz, grpsiz, authsiz;
628 struct timeval tv;
629 static u_int32_t base;
630
631 authsiz = nfsm_rndup(auth_len);
632 MGETHDR(mb, M_WAIT, MT_DATA);
633 if ((authsiz + 10 * NFSX_UNSIGNED) >= MINCLSIZE) {
634 MCLGET(mb, M_WAIT);
635 } else if ((authsiz + 10 * NFSX_UNSIGNED) < MHLEN) {
636 MH_ALIGN(mb, authsiz + 10 * NFSX_UNSIGNED);
637 } else {
638 MH_ALIGN(mb, 8 * NFSX_UNSIGNED);
639 }
640 mb->m_len = 0;
641 mreq = mb;
642 bpos = mtod(mb, caddr_t);
643
644 /*
645 * First the RPC header.
646 */
647 nfsm_build(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
648
649 /*
650 * derive initial xid from system time
651 * XXX time is invalid if root not yet mounted
652 */
653 if (!base && (rootvp)) {
654 microtime(&tv);
655 base = tv.tv_sec << 12;
656 nfs_xid = base;
657 }
658 /*
659 * Skip zero xid if it should ever happen.
660 */
661 if (++nfs_xid == 0)
662 nfs_xid++;
663
664 *tl++ = *xidp = txdr_unsigned(nfs_xid);
665 *tl++ = rpc_call;
666 *tl++ = rpc_vers;
667 if (nmflag & NFSMNT_NQNFS) {
668 *tl++ = txdr_unsigned(NQNFS_PROG);
669 *tl++ = txdr_unsigned(NQNFS_VER3);
670 } else {
671 *tl++ = txdr_unsigned(NFS_PROG);
672 if (nmflag & NFSMNT_NFSV3)
673 *tl++ = txdr_unsigned(NFS_VER3);
674 else
675 *tl++ = txdr_unsigned(NFS_VER2);
676 }
677 if (nmflag & NFSMNT_NFSV3)
678 *tl++ = txdr_unsigned(procid);
679 else
680 *tl++ = txdr_unsigned(nfsv2_procid[procid]);
681
682 /*
683 * And then the authorization cred.
684 */
685 *tl++ = txdr_unsigned(auth_type);
686 *tl = txdr_unsigned(authsiz);
687 switch (auth_type) {
688 case RPCAUTH_UNIX:
689 nfsm_build(tl, u_int32_t *, auth_len);
690 *tl++ = 0; /* stamp ?? */
691 *tl++ = 0; /* NULL hostname */
692 *tl++ = txdr_unsigned(cr->cr_uid);
693 *tl++ = txdr_unsigned(cr->cr_gid);
694 grpsiz = (auth_len >> 2) - 5;
695 *tl++ = txdr_unsigned(grpsiz);
696 for (i = 0; i < grpsiz; i++)
697 *tl++ = txdr_unsigned(cr->cr_groups[i]);
698 break;
699 case RPCAUTH_KERB4:
700 siz = auth_len;
701 while (siz > 0) {
702 if (M_TRAILINGSPACE(mb) == 0) {
703 MGET(mb2, M_WAIT, MT_DATA);
704 if (siz >= MINCLSIZE)
705 MCLGET(mb2, M_WAIT);
706 mb->m_next = mb2;
707 mb = mb2;
708 mb->m_len = 0;
709 bpos = mtod(mb, caddr_t);
710 }
711 i = min(siz, M_TRAILINGSPACE(mb));
712 memcpy(bpos, auth_str, i);
713 mb->m_len += i;
714 auth_str += i;
715 bpos += i;
716 siz -= i;
717 }
718 if ((siz = (nfsm_rndup(auth_len) - auth_len)) > 0) {
719 for (i = 0; i < siz; i++)
720 *bpos++ = '\0';
721 mb->m_len += siz;
722 }
723 break;
724 };
725
726 /*
727 * And the verifier...
728 */
729 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
730 if (verf_str) {
731 *tl++ = txdr_unsigned(RPCAUTH_KERB4);
732 *tl = txdr_unsigned(verf_len);
733 siz = verf_len;
734 while (siz > 0) {
735 if (M_TRAILINGSPACE(mb) == 0) {
736 MGET(mb2, M_WAIT, MT_DATA);
737 if (siz >= MINCLSIZE)
738 MCLGET(mb2, M_WAIT);
739 mb->m_next = mb2;
740 mb = mb2;
741 mb->m_len = 0;
742 bpos = mtod(mb, caddr_t);
743 }
744 i = min(siz, M_TRAILINGSPACE(mb));
745 memcpy(bpos, verf_str, i);
746 mb->m_len += i;
747 verf_str += i;
748 bpos += i;
749 siz -= i;
750 }
751 if ((siz = (nfsm_rndup(verf_len) - verf_len)) > 0) {
752 for (i = 0; i < siz; i++)
753 *bpos++ = '\0';
754 mb->m_len += siz;
755 }
756 } else {
757 *tl++ = txdr_unsigned(RPCAUTH_NULL);
758 *tl = 0;
759 }
760 mb->m_next = mrest;
761 mreq->m_pkthdr.len = authsiz + 10 * NFSX_UNSIGNED + mrest_len;
762 mreq->m_pkthdr.rcvif = (struct ifnet *)0;
763 *mbp = mb;
764 return (mreq);
765 }
766
767 /*
768 * copies mbuf chain to the uio scatter/gather list
769 */
770 int
771 nfsm_mbuftouio(mrep, uiop, siz, dpos)
772 struct mbuf **mrep;
773 register struct uio *uiop;
774 int siz;
775 caddr_t *dpos;
776 {
777 register char *mbufcp, *uiocp;
778 register int xfer, left, len;
779 register struct mbuf *mp;
780 long uiosiz, rem;
781 int error = 0;
782
783 mp = *mrep;
784 mbufcp = *dpos;
785 len = mtod(mp, caddr_t)+mp->m_len-mbufcp;
786 rem = nfsm_rndup(siz)-siz;
787 while (siz > 0) {
788 if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
789 return (EFBIG);
790 left = uiop->uio_iov->iov_len;
791 uiocp = uiop->uio_iov->iov_base;
792 if (left > siz)
793 left = siz;
794 uiosiz = left;
795 while (left > 0) {
796 while (len == 0) {
797 mp = mp->m_next;
798 if (mp == NULL)
799 return (EBADRPC);
800 mbufcp = mtod(mp, caddr_t);
801 len = mp->m_len;
802 }
803 xfer = (left > len) ? len : left;
804 #ifdef notdef
805 /* Not Yet.. */
806 if (uiop->uio_iov->iov_op != NULL)
807 (*(uiop->uio_iov->iov_op))
808 (mbufcp, uiocp, xfer);
809 else
810 #endif
811 if (uiop->uio_segflg == UIO_SYSSPACE)
812 memcpy(uiocp, mbufcp, xfer);
813 else
814 copyout(mbufcp, uiocp, xfer);
815 left -= xfer;
816 len -= xfer;
817 mbufcp += xfer;
818 uiocp += xfer;
819 uiop->uio_offset += xfer;
820 uiop->uio_resid -= xfer;
821 }
822 if (uiop->uio_iov->iov_len <= siz) {
823 uiop->uio_iovcnt--;
824 uiop->uio_iov++;
825 } else {
826 (caddr_t)uiop->uio_iov->iov_base += uiosiz;
827 uiop->uio_iov->iov_len -= uiosiz;
828 }
829 siz -= uiosiz;
830 }
831 *dpos = mbufcp;
832 *mrep = mp;
833 if (rem > 0) {
834 if (len < rem)
835 error = nfs_adv(mrep, dpos, rem, len);
836 else
837 *dpos += rem;
838 }
839 return (error);
840 }
841
842 /*
843 * copies a uio scatter/gather list to an mbuf chain.
844 * NOTE: can ony handle iovcnt == 1
845 */
846 int
847 nfsm_uiotombuf(uiop, mq, siz, bpos)
848 register struct uio *uiop;
849 struct mbuf **mq;
850 int siz;
851 caddr_t *bpos;
852 {
853 register char *uiocp;
854 register struct mbuf *mp, *mp2;
855 register int xfer, left, mlen;
856 int uiosiz, clflg, rem;
857 char *cp;
858
859 #ifdef DIAGNOSTIC
860 if (uiop->uio_iovcnt != 1)
861 panic("nfsm_uiotombuf: iovcnt != 1");
862 #endif
863
864 if (siz > MLEN) /* or should it >= MCLBYTES ?? */
865 clflg = 1;
866 else
867 clflg = 0;
868 rem = nfsm_rndup(siz)-siz;
869 mp = mp2 = *mq;
870 while (siz > 0) {
871 left = uiop->uio_iov->iov_len;
872 uiocp = uiop->uio_iov->iov_base;
873 if (left > siz)
874 left = siz;
875 uiosiz = left;
876 while (left > 0) {
877 mlen = M_TRAILINGSPACE(mp);
878 if (mlen == 0) {
879 MGET(mp, M_WAIT, MT_DATA);
880 if (clflg)
881 MCLGET(mp, M_WAIT);
882 mp->m_len = 0;
883 mp2->m_next = mp;
884 mp2 = mp;
885 mlen = M_TRAILINGSPACE(mp);
886 }
887 xfer = (left > mlen) ? mlen : left;
888 #ifdef notdef
889 /* Not Yet.. */
890 if (uiop->uio_iov->iov_op != NULL)
891 (*(uiop->uio_iov->iov_op))
892 (uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
893 else
894 #endif
895 if (uiop->uio_segflg == UIO_SYSSPACE)
896 memcpy(mtod(mp, caddr_t)+mp->m_len, uiocp, xfer);
897 else
898 copyin(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
899 mp->m_len += xfer;
900 left -= xfer;
901 uiocp += xfer;
902 uiop->uio_offset += xfer;
903 uiop->uio_resid -= xfer;
904 }
905 (caddr_t)uiop->uio_iov->iov_base += uiosiz;
906 uiop->uio_iov->iov_len -= uiosiz;
907 siz -= uiosiz;
908 }
909 if (rem > 0) {
910 if (rem > M_TRAILINGSPACE(mp)) {
911 MGET(mp, M_WAIT, MT_DATA);
912 mp->m_len = 0;
913 mp2->m_next = mp;
914 }
915 cp = mtod(mp, caddr_t)+mp->m_len;
916 for (left = 0; left < rem; left++)
917 *cp++ = '\0';
918 mp->m_len += rem;
919 *bpos = cp;
920 } else
921 *bpos = mtod(mp, caddr_t)+mp->m_len;
922 *mq = mp;
923 return (0);
924 }
925
926 /*
927 * Get at least "siz" bytes of correctly aligned data.
928 * When called the mbuf pointers are not necessarily correct,
929 * dsosp points to what ought to be in m_data and left contains
930 * what ought to be in m_len.
931 * This is used by the macros nfsm_dissect and nfsm_dissecton for tough
932 * cases. (The macros use the vars. dpos and dpos2)
933 */
934 int
935 nfsm_disct(mdp, dposp, siz, left, cp2)
936 struct mbuf **mdp;
937 caddr_t *dposp;
938 int siz;
939 int left;
940 caddr_t *cp2;
941 {
942 register struct mbuf *m1, *m2;
943 struct mbuf *havebuf = NULL;
944 caddr_t src = *dposp;
945 caddr_t dst;
946 int len;
947
948 #ifdef DEBUG
949 if (left < 0)
950 panic("nfsm_disct: left < 0");
951 #endif
952 m1 = *mdp;
953 /*
954 * Skip through the mbuf chain looking for an mbuf with
955 * some data. If the first mbuf found has enough data
956 * and it is correctly aligned return it.
957 */
958 while (left == 0) {
959 havebuf = m1;
960 *mdp = m1 = m1->m_next;
961 if (m1 == NULL)
962 return (EBADRPC);
963 src = mtod(m1, caddr_t);
964 left = m1->m_len;
965 /*
966 * If we start a new mbuf and it is big enough
967 * and correctly aligned just return it, don't
968 * do any pull up.
969 */
970 if (left >= siz && nfsm_aligned(src)) {
971 *cp2 = src;
972 *dposp = src + siz;
973 return (0);
974 }
975 }
976 if (m1->m_flags & M_EXT) {
977 if (havebuf) {
978 /* If the first mbuf with data has external data
979 * and there is a previous empty mbuf use it
980 * to move the data into.
981 */
982 m2 = m1;
983 *mdp = m1 = havebuf;
984 if (m1->m_flags & M_EXT) {
985 MEXTREMOVE(m1);
986 }
987 } else {
988 /*
989 * If the first mbuf has a external data
990 * and there is no previous empty mbuf
991 * allocate a new mbuf and move the external
992 * data to the new mbuf. Also make the first
993 * mbuf look empty.
994 */
995 m2 = m_get(M_WAIT, MT_DATA);
996 m2->m_ext = m1->m_ext;
997 m2->m_data = src;
998 m2->m_len = left;
999 MCLADDREFERENCE(m1, m2);
1000 MEXTREMOVE(m1);
1001 m2->m_next = m1->m_next;
1002 m1->m_next = m2;
1003 }
1004 m1->m_len = 0;
1005 dst = m1->m_dat;
1006 } else {
1007 /*
1008 * If the first mbuf has no external data
1009 * move the data to the front of the mbuf.
1010 */
1011 if ((dst = m1->m_dat) != src)
1012 memmove(dst, src, left);
1013 dst += left;
1014 m1->m_len = left;
1015 m2 = m1->m_next;
1016 }
1017 m1->m_flags &= ~M_PKTHDR;
1018 *cp2 = m1->m_data = m1->m_dat; /* data is at beginning of buffer */
1019 *dposp = mtod(m1, caddr_t) + siz;
1020 /*
1021 * Loop through mbufs pulling data up into first mbuf until
1022 * the first mbuf is full or there is no more data to
1023 * pullup.
1024 */
1025 while ((len = (MLEN - m1->m_len)) != 0 && m2) {
1026 if ((len = min(len, m2->m_len)) != 0)
1027 memcpy(dst, m2->m_data, len);
1028 m1->m_len += len;
1029 dst += len;
1030 m2->m_data += len;
1031 m2->m_len -= len;
1032 m2 = m2->m_next;
1033 }
1034 if (m1->m_len < siz)
1035 return (EBADRPC);
1036 return (0);
1037 }
1038
1039 /*
1040 * Advance the position in the mbuf chain.
1041 */
1042 int
1043 nfs_adv(mdp, dposp, offs, left)
1044 struct mbuf **mdp;
1045 caddr_t *dposp;
1046 int offs;
1047 int left;
1048 {
1049 register struct mbuf *m;
1050 register int s;
1051
1052 m = *mdp;
1053 s = left;
1054 while (s < offs) {
1055 offs -= s;
1056 m = m->m_next;
1057 if (m == NULL)
1058 return (EBADRPC);
1059 s = m->m_len;
1060 }
1061 *mdp = m;
1062 *dposp = mtod(m, caddr_t)+offs;
1063 return (0);
1064 }
1065
1066 /*
1067 * Copy a string into mbufs for the hard cases...
1068 */
1069 int
1070 nfsm_strtmbuf(mb, bpos, cp, siz)
1071 struct mbuf **mb;
1072 char **bpos;
1073 const char *cp;
1074 long siz;
1075 {
1076 register struct mbuf *m1 = NULL, *m2;
1077 long left, xfer, len, tlen;
1078 u_int32_t *tl;
1079 int putsize;
1080
1081 putsize = 1;
1082 m2 = *mb;
1083 left = M_TRAILINGSPACE(m2);
1084 if (left > 0) {
1085 tl = ((u_int32_t *)(*bpos));
1086 *tl++ = txdr_unsigned(siz);
1087 putsize = 0;
1088 left -= NFSX_UNSIGNED;
1089 m2->m_len += NFSX_UNSIGNED;
1090 if (left > 0) {
1091 memcpy((caddr_t) tl, cp, left);
1092 siz -= left;
1093 cp += left;
1094 m2->m_len += left;
1095 left = 0;
1096 }
1097 }
1098 /* Loop around adding mbufs */
1099 while (siz > 0) {
1100 MGET(m1, M_WAIT, MT_DATA);
1101 if (siz > MLEN)
1102 MCLGET(m1, M_WAIT);
1103 m1->m_len = NFSMSIZ(m1);
1104 m2->m_next = m1;
1105 m2 = m1;
1106 tl = mtod(m1, u_int32_t *);
1107 tlen = 0;
1108 if (putsize) {
1109 *tl++ = txdr_unsigned(siz);
1110 m1->m_len -= NFSX_UNSIGNED;
1111 tlen = NFSX_UNSIGNED;
1112 putsize = 0;
1113 }
1114 if (siz < m1->m_len) {
1115 len = nfsm_rndup(siz);
1116 xfer = siz;
1117 if (xfer < len)
1118 *(tl+(xfer>>2)) = 0;
1119 } else {
1120 xfer = len = m1->m_len;
1121 }
1122 memcpy((caddr_t) tl, cp, xfer);
1123 m1->m_len = len+tlen;
1124 siz -= xfer;
1125 cp += xfer;
1126 }
1127 *mb = m1;
1128 *bpos = mtod(m1, caddr_t)+m1->m_len;
1129 return (0);
1130 }
1131
1132 /*
1133 * Directory caching routines. They work as follows:
1134 * - a cache is maintained per VDIR nfsnode.
1135 * - for each offset cookie that is exported to userspace, and can
1136 * thus be thrown back at us as an offset to VOP_READDIR, store
1137 * information in the cache.
1138 * - cached are:
1139 * - cookie itself
1140 * - blocknumber (essentially just a search key in the buffer cache)
1141 * - entry number in block.
1142 * - offset cookie of block in which this entry is stored
1143 * - 32 bit cookie if NFSMNT_XLATECOOKIE is used.
1144 * - entries are looked up in a hash table
1145 * - also maintained is an LRU list of entries, used to determine
1146 * which ones to delete if the cache grows too large.
1147 * - if 32 <-> 64 translation mode is requested for a filesystem,
1148 * the cache also functions as a translation table
1149 * - in the translation case, invalidating the cache does not mean
1150 * flushing it, but just marking entries as invalid, except for
1151 * the <64bit cookie, 32bitcookie> pair which is still valid, to
1152 * still be able to use the cache as a translation table.
1153 * - 32 bit cookies are uniquely created by combining the hash table
1154 * entry value, and one generation count per hash table entry,
1155 * incremented each time an entry is appended to the chain.
1156 * - the cache is invalidated each time a direcory is modified
1157 * - sanity checks are also done; if an entry in a block turns
1158 * out not to have a matching cookie, the cache is invalidated
1159 * and a new block starting from the wanted offset is fetched from
1160 * the server.
1161 * - directory entries as read from the server are extended to contain
1162 * the 64bit and, optionally, the 32bit cookies, for sanity checking
1163 * the cache and exporting them to userspace through the cookie
1164 * argument to VOP_READDIR.
1165 */
1166
1167 u_long
1168 nfs_dirhash(off)
1169 off_t off;
1170 {
1171 int i;
1172 char *cp = (char *)&off;
1173 u_long sum = 0L;
1174
1175 for (i = 0 ; i < sizeof (off); i++)
1176 sum += *cp++;
1177
1178 return sum;
1179 }
1180
1181 void
1182 nfs_initdircache(vp)
1183 struct vnode *vp;
1184 {
1185 struct nfsnode *np = VTONFS(vp);
1186 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1187
1188 np->n_dircachesize = 0;
1189 np->n_dblkno = 1;
1190 np->n_dircache =
1191 hashinit(NFS_DIRHASHSIZ, M_NFSDIROFF, M_WAITOK, &nfsdirhashmask);
1192 TAILQ_INIT(&np->n_dirchain);
1193 if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
1194 MALLOC(np->n_dirgens, unsigned *,
1195 NFS_DIRHASHSIZ * sizeof (unsigned), M_NFSDIROFF,
1196 M_WAITOK);
1197 memset((caddr_t)np->n_dirgens, 0,
1198 NFS_DIRHASHSIZ * sizeof (unsigned));
1199 }
1200 }
1201
1202 static struct nfsdircache dzero = {0, 0, {0, 0}, {0, 0}, 0, 0, 0};
1203
1204 struct nfsdircache *
1205 nfs_searchdircache(vp, off, do32, hashent)
1206 struct vnode *vp;
1207 off_t off;
1208 int do32;
1209 int *hashent;
1210 {
1211 struct nfsdirhashhead *ndhp;
1212 struct nfsdircache *ndp = NULL;
1213 struct nfsnode *np = VTONFS(vp);
1214 unsigned ent;
1215
1216 /*
1217 * Zero is always a valid cookie.
1218 */
1219 if (off == 0)
1220 return &dzero;
1221
1222 /*
1223 * We use a 32bit cookie as search key, directly reconstruct
1224 * the hashentry. Else use the hashfunction.
1225 */
1226 if (do32) {
1227 ent = (u_int32_t)off >> 24;
1228 if (ent >= NFS_DIRHASHSIZ)
1229 return NULL;
1230 ndhp = &np->n_dircache[ent];
1231 } else {
1232 ndhp = NFSDIRHASH(np, off);
1233 }
1234
1235 if (hashent)
1236 *hashent = (int)(ndhp - np->n_dircache);
1237 if (do32) {
1238 for (ndp = ndhp->lh_first; ndp; ndp = ndp->dc_hash.le_next) {
1239 if (ndp->dc_cookie32 == (u_int32_t)off) {
1240 /*
1241 * An invalidated entry will become the
1242 * start of a new block fetched from
1243 * the server.
1244 */
1245 if (ndp->dc_blkno == -1) {
1246 ndp->dc_blkcookie = ndp->dc_cookie;
1247 ndp->dc_blkno = np->n_dblkno++;
1248 ndp->dc_entry = 0;
1249 }
1250 break;
1251 }
1252 }
1253 } else {
1254 for (ndp = ndhp->lh_first; ndp; ndp = ndp->dc_hash.le_next)
1255 if (ndp->dc_cookie == off)
1256 break;
1257 }
1258 return ndp;
1259 }
1260
1261
1262 struct nfsdircache *
1263 nfs_enterdircache(vp, off, blkoff, en, blkno)
1264 struct vnode *vp;
1265 off_t off, blkoff;
1266 daddr_t blkno;
1267 int en;
1268 {
1269 struct nfsnode *np = VTONFS(vp);
1270 struct nfsdirhashhead *ndhp;
1271 struct nfsdircache *ndp = NULL, *first;
1272 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1273 int hashent, gen, overwrite;
1274
1275 if (!np->n_dircache)
1276 /*
1277 * XXX would like to do this in nfs_nget but vtype
1278 * isn't known at that time.
1279 */
1280 nfs_initdircache(vp);
1281
1282 /*
1283 * XXX refuse entries for offset 0. amd(8) erroneously sets
1284 * cookie 0 for the '.' entry, making this necessary. This
1285 * isn't so bad, as 0 is a special case anyway.
1286 */
1287 if (off == 0)
1288 return &dzero;
1289
1290 ndp = nfs_searchdircache(vp, off, 0, &hashent);
1291
1292 if (ndp && ndp->dc_blkno != -1) {
1293 /*
1294 * Overwriting an old entry. Check if it's the same.
1295 * If so, just return. If not, remove the old entry.
1296 */
1297 if (ndp->dc_blkcookie == blkoff && ndp->dc_entry == en)
1298 return ndp;
1299 TAILQ_REMOVE(&np->n_dirchain, ndp, dc_chain);
1300 LIST_REMOVE(ndp, dc_hash);
1301 FREE(ndp, M_NFSDIROFF);
1302 ndp = 0;
1303 }
1304
1305 ndhp = &np->n_dircache[hashent];
1306
1307 if (!ndp) {
1308 MALLOC(ndp, struct nfsdircache *, sizeof (*ndp), M_NFSDIROFF,
1309 M_WAITOK);
1310 overwrite = 0;
1311 if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
1312 /*
1313 * We're allocating a new entry, so bump the
1314 * generation number.
1315 */
1316 gen = ++np->n_dirgens[hashent];
1317 if (gen == 0) {
1318 np->n_dirgens[hashent]++;
1319 gen++;
1320 }
1321 ndp->dc_cookie32 = (hashent << 24) | (gen & 0xffffff);
1322 }
1323 } else
1324 overwrite = 1;
1325
1326 /*
1327 * If the entry number is 0, we are at the start of a new block, so
1328 * allocate a new blocknumber.
1329 */
1330 if (en == 0)
1331 ndp->dc_blkno = np->n_dblkno++;
1332 else
1333 ndp->dc_blkno = blkno;
1334
1335 ndp->dc_cookie = off;
1336 ndp->dc_blkcookie = blkoff;
1337 ndp->dc_entry = en;
1338
1339 if (overwrite)
1340 return ndp;
1341
1342 /*
1343 * If the maximum directory cookie cache size has been reached
1344 * for this node, take one off the front. The idea is that
1345 * directories are typically read front-to-back once, so that
1346 * the oldest entries can be thrown away without much performance
1347 * loss.
1348 */
1349 if (np->n_dircachesize == NFS_MAXDIRCACHE) {
1350 first = np->n_dirchain.tqh_first;
1351 TAILQ_REMOVE(&np->n_dirchain, first, dc_chain);
1352 LIST_REMOVE(first, dc_hash);
1353 FREE(first, M_NFSDIROFF);
1354 } else
1355 np->n_dircachesize++;
1356
1357 LIST_INSERT_HEAD(ndhp, ndp, dc_hash);
1358 TAILQ_INSERT_TAIL(&np->n_dirchain, ndp, dc_chain);
1359 return ndp;
1360 }
1361
1362 void
1363 nfs_invaldircache(vp, forcefree)
1364 struct vnode *vp;
1365 int forcefree;
1366 {
1367 struct nfsnode *np = VTONFS(vp);
1368 struct nfsdircache *ndp = NULL;
1369 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1370
1371 #ifdef DIAGNOSTIC
1372 if (vp->v_type != VDIR)
1373 panic("nfs: invaldircache: not dir");
1374 #endif
1375
1376 if (!np->n_dircache)
1377 return;
1378
1379 if (!(nmp->nm_flag & NFSMNT_XLATECOOKIE) || forcefree) {
1380 while ((ndp = np->n_dirchain.tqh_first)) {
1381 TAILQ_REMOVE(&np->n_dirchain, ndp, dc_chain);
1382 LIST_REMOVE(ndp, dc_hash);
1383 FREE(ndp, M_NFSDIROFF);
1384 }
1385 np->n_dircachesize = 0;
1386 if (forcefree && np->n_dirgens) {
1387 FREE(np->n_dirgens, M_NFSDIROFF);
1388 }
1389 } else {
1390 for (ndp = np->n_dirchain.tqh_first; ndp;
1391 ndp = ndp->dc_chain.tqe_next)
1392 ndp->dc_blkno = -1;
1393 }
1394
1395 np->n_dblkno = 1;
1396 }
1397
1398 /*
1399 * Called once before VFS init to initialize shared and
1400 * server-specific data structures.
1401 */
1402 void
1403 nfs_init()
1404 {
1405
1406 #if !defined(alpha) && defined(DIAGNOSTIC)
1407 /*
1408 * Check to see if major data structures haven't bloated.
1409 */
1410 if (sizeof (struct nfsnode) > NFS_NODEALLOC) {
1411 printf("struct nfsnode bloated (> %dbytes)\n", NFS_NODEALLOC);
1412 printf("Try reducing NFS_SMALLFH\n");
1413 }
1414 if (sizeof (struct nfssvc_sock) > NFS_SVCALLOC) {
1415 printf("struct nfssvc_sock bloated (> %dbytes)\n",NFS_SVCALLOC);
1416 printf("Try reducing NFS_UIDHASHSIZ\n");
1417 }
1418 if (sizeof (struct nfsuid) > NFS_UIDALLOC) {
1419 printf("struct nfsuid bloated (> %dbytes)\n",NFS_UIDALLOC);
1420 printf("Try unionizing the nu_nickname and nu_flag fields\n");
1421 }
1422 #endif
1423
1424 nfsrtt.pos = 0;
1425 rpc_vers = txdr_unsigned(RPC_VER2);
1426 rpc_call = txdr_unsigned(RPC_CALL);
1427 rpc_reply = txdr_unsigned(RPC_REPLY);
1428 rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED);
1429 rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED);
1430 rpc_mismatch = txdr_unsigned(RPC_MISMATCH);
1431 rpc_autherr = txdr_unsigned(RPC_AUTHERR);
1432 rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX);
1433 rpc_auth_kerb = txdr_unsigned(RPCAUTH_KERB4);
1434 nfs_prog = txdr_unsigned(NFS_PROG);
1435 nqnfs_prog = txdr_unsigned(NQNFS_PROG);
1436 nfs_true = txdr_unsigned(TRUE);
1437 nfs_false = txdr_unsigned(FALSE);
1438 nfs_xdrneg1 = txdr_unsigned(-1);
1439 nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000;
1440 if (nfs_ticks < 1)
1441 nfs_ticks = 1;
1442 #ifdef NFSSERVER
1443 nfsrv_init(0); /* Init server data structures */
1444 nfsrv_initcache(); /* Init the server request cache */
1445 #endif /* NFSSERVER */
1446
1447 /*
1448 * Initialize the nqnfs data structures.
1449 */
1450 if (nqnfsstarttime == 0) {
1451 nqnfsstarttime = boottime.tv_sec + nqsrv_maxlease
1452 + nqsrv_clockskew + nqsrv_writeslack;
1453 NQLOADNOVRAM(nqnfsstarttime);
1454 CIRCLEQ_INIT(&nqtimerhead);
1455 nqfhhashtbl = hashinit(NQLCHSZ, M_NQLEASE, M_WAITOK, &nqfhhash);
1456 }
1457
1458 /*
1459 * Initialize reply list and start timer
1460 */
1461 TAILQ_INIT(&nfs_reqq);
1462 nfs_timer(NULL);
1463 }
1464
1465 #ifdef NFS
1466 /*
1467 * Called once at VFS init to initialize client-specific data structures.
1468 */
1469 void
1470 nfs_vfs_init()
1471 {
1472 register int i;
1473
1474 /* Ensure async daemons disabled */
1475 for (i = 0; i < NFS_MAXASYNCDAEMON; i++) {
1476 nfs_iodwant[i] = (struct proc *)0;
1477 nfs_iodmount[i] = (struct nfsmount *)0;
1478 }
1479 nfs_nhinit(); /* Init the nfsnode table */
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 register 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 register struct vnode *vp = *vpp;
1524 register 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 register 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_char, fp->fa3_rdev.specdata1),
1538 fxdr_unsigned(u_char, 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_vnlock = vp->v_vnlock;
1579 vp->v_vnlock = NULL;
1580 nvp->v_data = vp->v_data;
1581 vp->v_data = NULL;
1582 vp->v_op = spec_vnodeop_p;
1583 vrele(vp);
1584 vgone(vp);
1585 /*
1586 * Reinitialize aliased node.
1587 */
1588 np->n_vnode = nvp;
1589 *vpp = vp = nvp;
1590 }
1591 }
1592 np->n_mtime = mtime.tv_sec;
1593 }
1594 vap = np->n_vattr;
1595 vap->va_type = vtyp;
1596 vap->va_mode = vmode & ALLPERMS;
1597 vap->va_rdev = (dev_t)rdev;
1598 vap->va_mtime = mtime;
1599 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
1600 if (v3) {
1601 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1602 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
1603 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
1604 vap->va_size = fxdr_hyper(&fp->fa3_size);
1605 if (vtyp == VDIR)
1606 vap->va_blocksize = NFS_DIRFRAGSIZ;
1607 else
1608 vap->va_blocksize = NFS_FABLKSIZE;
1609 vap->va_bytes = fxdr_hyper(&fp->fa3_used);
1610 vap->va_fileid = fxdr_unsigned(int32_t,
1611 fp->fa3_fileid.nfsuquad[1]);
1612 fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime);
1613 fxdr_nfsv3time(&fp->fa3_ctime, &vap->va_ctime);
1614 vap->va_flags = 0;
1615 vap->va_filerev = 0;
1616 } else {
1617 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1618 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
1619 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
1620 vap->va_size = fxdr_unsigned(u_int32_t, fp->fa2_size);
1621 if (vtyp == VDIR)
1622 vap->va_blocksize = NFS_DIRFRAGSIZ;
1623 else
1624 vap->va_blocksize =
1625 fxdr_unsigned(int32_t, fp->fa2_blocksize);
1626 vap->va_bytes = fxdr_unsigned(int32_t, fp->fa2_blocks)
1627 * NFS_FABLKSIZE;
1628 vap->va_fileid = fxdr_unsigned(int32_t, fp->fa2_fileid);
1629 fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime);
1630 vap->va_flags = 0;
1631 vap->va_ctime.tv_sec = fxdr_unsigned(u_int32_t,
1632 fp->fa2_ctime.nfsv2_sec);
1633 vap->va_ctime.tv_nsec = 0;
1634 vap->va_gen = fxdr_unsigned(u_int32_t,fp->fa2_ctime.nfsv2_usec);
1635 vap->va_filerev = 0;
1636 }
1637 if (vap->va_size != np->n_size) {
1638 if (vap->va_type == VREG) {
1639 if (np->n_flag & NMODIFIED) {
1640 if (vap->va_size < np->n_size)
1641 vap->va_size = np->n_size;
1642 else
1643 np->n_size = vap->va_size;
1644 } else
1645 np->n_size = vap->va_size;
1646 #if defined(UVM)
1647 uvm_vnp_setsize(vp, np->n_size);
1648 #else
1649 vnode_pager_setsize(vp, np->n_size);
1650 #endif
1651 } else
1652 np->n_size = vap->va_size;
1653 }
1654 np->n_attrstamp = time.tv_sec;
1655 if (vaper != NULL) {
1656 memcpy((caddr_t)vaper, (caddr_t)vap, sizeof(*vap));
1657 if (np->n_flag & NCHG) {
1658 if (np->n_flag & NACC)
1659 vaper->va_atime = np->n_atim;
1660 if (np->n_flag & NUPD)
1661 vaper->va_mtime = np->n_mtim;
1662 }
1663 }
1664 return (0);
1665 }
1666
1667 /*
1668 * Check the time stamp
1669 * If the cache is valid, copy contents to *vap and return 0
1670 * otherwise return an error
1671 */
1672 int
1673 nfs_getattrcache(vp, vaper)
1674 register struct vnode *vp;
1675 struct vattr *vaper;
1676 {
1677 register struct nfsnode *np = VTONFS(vp);
1678 register struct vattr *vap;
1679
1680 if ((time.tv_sec - np->n_attrstamp) >= NFS_ATTRTIMEO(np)) {
1681 nfsstats.attrcache_misses++;
1682 return (ENOENT);
1683 }
1684 nfsstats.attrcache_hits++;
1685 vap = np->n_vattr;
1686 if (vap->va_size != np->n_size) {
1687 if (vap->va_type == VREG) {
1688 if (np->n_flag & NMODIFIED) {
1689 if (vap->va_size < np->n_size)
1690 vap->va_size = np->n_size;
1691 else
1692 np->n_size = vap->va_size;
1693 } else
1694 np->n_size = vap->va_size;
1695 #if defined(UVM)
1696 uvm_vnp_setsize(vp, np->n_size);
1697 #else
1698 vnode_pager_setsize(vp, np->n_size);
1699 #endif
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 register 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 register int i, rem;
1806 register struct mbuf *md;
1807 register 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 register int len;
2029 int nul;
2030 {
2031 register struct mbuf *m;
2032 register int count, i;
2033 register 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 register struct vattr *before_vap;
2092 int after_ret;
2093 struct vattr *after_vap;
2094 struct mbuf **mbp;
2095 char **bposp;
2096 {
2097 register struct mbuf *mb = *mbp, *mb2;
2098 register char *bpos = *bposp;
2099 register 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 register struct mbuf *mb = *mbp, *mb2;
2127 register char *bpos = *bposp;
2128 register u_int32_t *tl;
2129 register 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 register struct nfsrv_descript *nfsd;
2147 register struct vattr *vap;
2148 register 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 register struct mount *mp;
2205 register 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 }
2236 /*
2237 * Check/setup credentials.
2238 */
2239 if (exflags & MNT_EXKERB) {
2240 if (!kerbflag) {
2241 vput(*vpp);
2242 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
2243 }
2244 } else if (kerbflag) {
2245 vput(*vpp);
2246 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
2247 } else if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
2248 cred->cr_uid = credanon->cr_uid;
2249 cred->cr_gid = credanon->cr_gid;
2250 for (i = 0; i < credanon->cr_ngroups && i < NGROUPS; i++)
2251 cred->cr_groups[i] = credanon->cr_groups[i];
2252 cred->cr_ngroups = i;
2253 }
2254 if (exflags & MNT_EXRDONLY)
2255 *rdonlyp = 1;
2256 else
2257 *rdonlyp = 0;
2258 if (!lockflag)
2259 VOP_UNLOCK(*vpp, 0);
2260 return (0);
2261 }
2262
2263 /*
2264 * WebNFS: check if a filehandle is a public filehandle. For v3, this
2265 * means a length of 0, for v2 it means all zeroes. nfsm_srvmtofh has
2266 * transformed this to all zeroes in both cases, so check for it.
2267 */
2268 int
2269 nfs_ispublicfh(fhp)
2270 fhandle_t *fhp;
2271 {
2272 char *cp = (char *)fhp;
2273 int i;
2274
2275 for (i = 0; i < NFSX_V3FH; i++)
2276 if (*cp++ != 0)
2277 return (FALSE);
2278 return (TRUE);
2279 }
2280
2281 /*
2282 * This function compares two net addresses by family and returns TRUE
2283 * if they are the same host.
2284 * If there is any doubt, return FALSE.
2285 * The AF_INET family is handled as a special case so that address mbufs
2286 * don't need to be saved to store "struct in_addr", which is only 4 bytes.
2287 */
2288 int
2289 netaddr_match(family, haddr, nam)
2290 int family;
2291 union nethostaddr *haddr;
2292 struct mbuf *nam;
2293 {
2294 register struct sockaddr_in *inetaddr;
2295
2296 switch (family) {
2297 case AF_INET:
2298 inetaddr = mtod(nam, struct sockaddr_in *);
2299 if (inetaddr->sin_family == AF_INET &&
2300 inetaddr->sin_addr.s_addr == haddr->had_inetaddr)
2301 return (1);
2302 break;
2303 #ifdef ISO
2304 case AF_ISO:
2305 {
2306 register struct sockaddr_iso *isoaddr1, *isoaddr2;
2307
2308 isoaddr1 = mtod(nam, struct sockaddr_iso *);
2309 isoaddr2 = mtod(haddr->had_nam, struct sockaddr_iso *);
2310 if (isoaddr1->siso_family == AF_ISO &&
2311 isoaddr1->siso_nlen > 0 &&
2312 isoaddr1->siso_nlen == isoaddr2->siso_nlen &&
2313 SAME_ISOADDR(isoaddr1, isoaddr2))
2314 return (1);
2315 break;
2316 }
2317 #endif /* ISO */
2318 default:
2319 break;
2320 };
2321 return (0);
2322 }
2323
2324
2325 /*
2326 * The write verifier has changed (probably due to a server reboot), so all
2327 * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
2328 * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
2329 * flag. Once done the new write verifier can be set for the mount point.
2330 */
2331 void
2332 nfs_clearcommit(mp)
2333 struct mount *mp;
2334 {
2335 register struct vnode *vp, *nvp;
2336 register struct buf *bp, *nbp;
2337 int s;
2338
2339 s = splbio();
2340 loop:
2341 for (vp = mp->mnt_vnodelist.lh_first; vp; vp = nvp) {
2342 if (vp->v_mount != mp) /* Paranoia */
2343 goto loop;
2344 nvp = vp->v_mntvnodes.le_next;
2345 for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = nbp) {
2346 nbp = bp->b_vnbufs.le_next;
2347 if ((bp->b_flags & (B_BUSY | B_DELWRI | B_NEEDCOMMIT))
2348 == (B_DELWRI | B_NEEDCOMMIT))
2349 bp->b_flags &= ~B_NEEDCOMMIT;
2350 }
2351 }
2352 splx(s);
2353 }
2354
2355 /*
2356 * Map errnos to NFS error numbers. For Version 3 also filter out error
2357 * numbers not specified for the associated procedure.
2358 */
2359 int
2360 nfsrv_errmap(nd, err)
2361 struct nfsrv_descript *nd;
2362 register int err;
2363 {
2364 register short *defaulterrp, *errp;
2365
2366 if (nd->nd_flag & ND_NFSV3) {
2367 if (nd->nd_procnum <= NFSPROC_COMMIT) {
2368 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
2369 while (*++errp) {
2370 if (*errp == err)
2371 return (err);
2372 else if (*errp > err)
2373 break;
2374 }
2375 return ((int)*defaulterrp);
2376 } else
2377 return (err & 0xffff);
2378 }
2379 if (err <= ELAST)
2380 return ((int)nfsrv_v2errmap[err - 1]);
2381 return (NFSERR_IO);
2382 }
2383
2384 /*
2385 * Sort the group list in increasing numerical order.
2386 * (Insertion sort by Chris Torek, who was grossed out by the bubble sort
2387 * that used to be here.)
2388 */
2389 void
2390 nfsrvw_sort(list, num)
2391 register gid_t *list;
2392 register int num;
2393 {
2394 register int i, j;
2395 gid_t v;
2396
2397 /* Insertion sort. */
2398 for (i = 1; i < num; i++) {
2399 v = list[i];
2400 /* find correct slot for value v, moving others up */
2401 for (j = i; --j >= 0 && v < list[j];)
2402 list[j + 1] = list[j];
2403 list[j + 1] = v;
2404 }
2405 }
2406
2407 /*
2408 * copy credentials making sure that the result can be compared with memcmp().
2409 */
2410 void
2411 nfsrv_setcred(incred, outcred)
2412 register struct ucred *incred, *outcred;
2413 {
2414 register int i;
2415
2416 memset((caddr_t)outcred, 0, sizeof (struct ucred));
2417 outcred->cr_ref = 1;
2418 outcred->cr_uid = incred->cr_uid;
2419 outcred->cr_gid = incred->cr_gid;
2420 outcred->cr_ngroups = incred->cr_ngroups;
2421 for (i = 0; i < incred->cr_ngroups; i++)
2422 outcred->cr_groups[i] = incred->cr_groups[i];
2423 nfsrvw_sort(outcred->cr_groups, outcred->cr_ngroups);
2424 }
2425