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