nfs_subs.c revision 1.54 1 /* $NetBSD: nfs_subs.c,v 1.54 1998/02/10 14:10:16 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 "opt_uvm.h"
42
43 /*
44 * These functions support the macros and help fiddle mbuf chains for
45 * the nfs op functions. They do things like create the rpc header and
46 * copy data between mbuf chains and uio lists.
47 */
48 #include <sys/param.h>
49 #include <sys/proc.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
52 #include <sys/mount.h>
53 #include <sys/vnode.h>
54 #include <sys/namei.h>
55 #include <sys/mbuf.h>
56 #include <sys/socket.h>
57 #include <sys/stat.h>
58 #include <sys/malloc.h>
59 #include <sys/time.h>
60 #include <sys/dirent.h>
61
62 #include <vm/vm.h>
63
64 #if defined(UVM)
65 #include <uvm/uvm_extern.h>
66 #endif
67
68 #include <nfs/rpcv2.h>
69 #include <nfs/nfsproto.h>
70 #include <nfs/nfsnode.h>
71 #include <nfs/nfs.h>
72 #include <nfs/xdr_subs.h>
73 #include <nfs/nfsm_subs.h>
74 #include <nfs/nfsmount.h>
75 #include <nfs/nqnfs.h>
76 #include <nfs/nfsrtt.h>
77 #include <nfs/nfs_var.h>
78
79 #include <miscfs/specfs/specdev.h>
80
81 #include <vm/vm.h>
82
83 #include <netinet/in.h>
84 #ifdef ISO
85 #include <netiso/iso.h>
86 #endif
87
88 /*
89 * Data items converted to xdr at startup, since they are constant
90 * This is kinda hokey, but may save a little time doing byte swaps
91 */
92 u_int32_t nfs_xdrneg1;
93 u_int32_t rpc_call, rpc_vers, rpc_reply, rpc_msgdenied, rpc_autherr,
94 rpc_mismatch, rpc_auth_unix, rpc_msgaccepted,
95 rpc_auth_kerb;
96 u_int32_t nfs_prog, nqnfs_prog, nfs_true, nfs_false;
97
98 /* And other global data */
99 static u_int32_t nfs_xid = 0;
100 nfstype nfsv2_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFNON,
101 NFCHR, NFNON };
102 nfstype nfsv3_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFSOCK,
103 NFFIFO, NFNON };
104 enum vtype nv2tov_type[8] = { VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON, VNON };
105 enum vtype nv3tov_type[8]={ VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO };
106 int nfs_ticks;
107 extern struct nfs_public nfs_pub;
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 0,
286 };
287
288 static short nfsv3err_write[] = {
289 NFSERR_IO,
290 NFSERR_IO,
291 NFSERR_ACCES,
292 NFSERR_INVAL,
293 NFSERR_FBIG,
294 NFSERR_NOSPC,
295 NFSERR_ROFS,
296 NFSERR_DQUOT,
297 NFSERR_STALE,
298 NFSERR_BADHANDLE,
299 NFSERR_SERVERFAULT,
300 0,
301 };
302
303 static short nfsv3err_create[] = {
304 NFSERR_IO,
305 NFSERR_IO,
306 NFSERR_ACCES,
307 NFSERR_EXIST,
308 NFSERR_NOTDIR,
309 NFSERR_NOSPC,
310 NFSERR_ROFS,
311 NFSERR_NAMETOL,
312 NFSERR_DQUOT,
313 NFSERR_STALE,
314 NFSERR_BADHANDLE,
315 NFSERR_NOTSUPP,
316 NFSERR_SERVERFAULT,
317 0,
318 };
319
320 static short nfsv3err_mkdir[] = {
321 NFSERR_IO,
322 NFSERR_IO,
323 NFSERR_ACCES,
324 NFSERR_EXIST,
325 NFSERR_NOTDIR,
326 NFSERR_NOSPC,
327 NFSERR_ROFS,
328 NFSERR_NAMETOL,
329 NFSERR_DQUOT,
330 NFSERR_STALE,
331 NFSERR_BADHANDLE,
332 NFSERR_NOTSUPP,
333 NFSERR_SERVERFAULT,
334 0,
335 };
336
337 static short nfsv3err_symlink[] = {
338 NFSERR_IO,
339 NFSERR_IO,
340 NFSERR_ACCES,
341 NFSERR_EXIST,
342 NFSERR_NOTDIR,
343 NFSERR_NOSPC,
344 NFSERR_ROFS,
345 NFSERR_NAMETOL,
346 NFSERR_DQUOT,
347 NFSERR_STALE,
348 NFSERR_BADHANDLE,
349 NFSERR_NOTSUPP,
350 NFSERR_SERVERFAULT,
351 0,
352 };
353
354 static short nfsv3err_mknod[] = {
355 NFSERR_IO,
356 NFSERR_IO,
357 NFSERR_ACCES,
358 NFSERR_EXIST,
359 NFSERR_NOTDIR,
360 NFSERR_NOSPC,
361 NFSERR_ROFS,
362 NFSERR_NAMETOL,
363 NFSERR_DQUOT,
364 NFSERR_STALE,
365 NFSERR_BADHANDLE,
366 NFSERR_NOTSUPP,
367 NFSERR_SERVERFAULT,
368 NFSERR_BADTYPE,
369 0,
370 };
371
372 static short nfsv3err_remove[] = {
373 NFSERR_IO,
374 NFSERR_NOENT,
375 NFSERR_IO,
376 NFSERR_ACCES,
377 NFSERR_NOTDIR,
378 NFSERR_ROFS,
379 NFSERR_NAMETOL,
380 NFSERR_STALE,
381 NFSERR_BADHANDLE,
382 NFSERR_SERVERFAULT,
383 0,
384 };
385
386 static short nfsv3err_rmdir[] = {
387 NFSERR_IO,
388 NFSERR_NOENT,
389 NFSERR_IO,
390 NFSERR_ACCES,
391 NFSERR_EXIST,
392 NFSERR_NOTDIR,
393 NFSERR_INVAL,
394 NFSERR_ROFS,
395 NFSERR_NAMETOL,
396 NFSERR_NOTEMPTY,
397 NFSERR_STALE,
398 NFSERR_BADHANDLE,
399 NFSERR_NOTSUPP,
400 NFSERR_SERVERFAULT,
401 0,
402 };
403
404 static short nfsv3err_rename[] = {
405 NFSERR_IO,
406 NFSERR_NOENT,
407 NFSERR_IO,
408 NFSERR_ACCES,
409 NFSERR_EXIST,
410 NFSERR_XDEV,
411 NFSERR_NOTDIR,
412 NFSERR_ISDIR,
413 NFSERR_INVAL,
414 NFSERR_NOSPC,
415 NFSERR_ROFS,
416 NFSERR_MLINK,
417 NFSERR_NAMETOL,
418 NFSERR_NOTEMPTY,
419 NFSERR_DQUOT,
420 NFSERR_STALE,
421 NFSERR_BADHANDLE,
422 NFSERR_NOTSUPP,
423 NFSERR_SERVERFAULT,
424 0,
425 };
426
427 static short nfsv3err_link[] = {
428 NFSERR_IO,
429 NFSERR_IO,
430 NFSERR_ACCES,
431 NFSERR_EXIST,
432 NFSERR_XDEV,
433 NFSERR_NOTDIR,
434 NFSERR_INVAL,
435 NFSERR_NOSPC,
436 NFSERR_ROFS,
437 NFSERR_MLINK,
438 NFSERR_NAMETOL,
439 NFSERR_DQUOT,
440 NFSERR_STALE,
441 NFSERR_BADHANDLE,
442 NFSERR_NOTSUPP,
443 NFSERR_SERVERFAULT,
444 0,
445 };
446
447 static short nfsv3err_readdir[] = {
448 NFSERR_IO,
449 NFSERR_IO,
450 NFSERR_ACCES,
451 NFSERR_NOTDIR,
452 NFSERR_STALE,
453 NFSERR_BADHANDLE,
454 NFSERR_BAD_COOKIE,
455 NFSERR_TOOSMALL,
456 NFSERR_SERVERFAULT,
457 0,
458 };
459
460 static short nfsv3err_readdirplus[] = {
461 NFSERR_IO,
462 NFSERR_IO,
463 NFSERR_ACCES,
464 NFSERR_NOTDIR,
465 NFSERR_STALE,
466 NFSERR_BADHANDLE,
467 NFSERR_BAD_COOKIE,
468 NFSERR_NOTSUPP,
469 NFSERR_TOOSMALL,
470 NFSERR_SERVERFAULT,
471 0,
472 };
473
474 static short nfsv3err_fsstat[] = {
475 NFSERR_IO,
476 NFSERR_IO,
477 NFSERR_STALE,
478 NFSERR_BADHANDLE,
479 NFSERR_SERVERFAULT,
480 0,
481 };
482
483 static short nfsv3err_fsinfo[] = {
484 NFSERR_STALE,
485 NFSERR_STALE,
486 NFSERR_BADHANDLE,
487 NFSERR_SERVERFAULT,
488 0,
489 };
490
491 static short nfsv3err_pathconf[] = {
492 NFSERR_STALE,
493 NFSERR_STALE,
494 NFSERR_BADHANDLE,
495 NFSERR_SERVERFAULT,
496 0,
497 };
498
499 static short nfsv3err_commit[] = {
500 NFSERR_IO,
501 NFSERR_IO,
502 NFSERR_STALE,
503 NFSERR_BADHANDLE,
504 NFSERR_SERVERFAULT,
505 0,
506 };
507
508 static short *nfsrv_v3errmap[] = {
509 nfsv3err_null,
510 nfsv3err_getattr,
511 nfsv3err_setattr,
512 nfsv3err_lookup,
513 nfsv3err_access,
514 nfsv3err_readlink,
515 nfsv3err_read,
516 nfsv3err_write,
517 nfsv3err_create,
518 nfsv3err_mkdir,
519 nfsv3err_symlink,
520 nfsv3err_mknod,
521 nfsv3err_remove,
522 nfsv3err_rmdir,
523 nfsv3err_rename,
524 nfsv3err_link,
525 nfsv3err_readdir,
526 nfsv3err_readdirplus,
527 nfsv3err_fsstat,
528 nfsv3err_fsinfo,
529 nfsv3err_pathconf,
530 nfsv3err_commit,
531 };
532
533 extern struct nfsrtt nfsrtt;
534 extern time_t nqnfsstarttime;
535 extern int nqsrv_clockskew;
536 extern int nqsrv_writeslack;
537 extern int nqsrv_maxlease;
538 extern int nqnfs_piggy[NFS_NPROCS];
539 extern nfstype nfsv2_type[9];
540 extern nfstype nfsv3_type[9];
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 register struct mbuf *mb;
562 register u_int32_t *tl;
563 register 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 register 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 register struct mbuf *mb;
619 register u_int32_t *tl;
620 register caddr_t bpos;
621 register 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 bcopy(auth_str, bpos, 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 bcopy(verf_str, bpos, 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 register struct uio *uiop;
770 int siz;
771 caddr_t *dpos;
772 {
773 register char *mbufcp, *uiocp;
774 register int xfer, left, len;
775 register 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 bcopy(mbufcp, uiocp, 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 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 register struct uio *uiop;
845 struct mbuf **mq;
846 int siz;
847 caddr_t *bpos;
848 {
849 register char *uiocp;
850 register struct mbuf *mp, *mp2;
851 register 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 bcopy(uiocp, mtod(mp, caddr_t)+mp->m_len, 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 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 register 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 ovbcopy(src, dst, 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 bcopy(m2->m_data, dst, 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 register struct mbuf *m;
1046 register 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 register 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 bcopy(cp, (caddr_t) tl, 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 bcopy(cp, (caddr_t) tl, 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 bzero((caddr_t)np->n_dirgens,
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(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 register 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 /*
1479 * Attribute cache routines.
1480 * nfs_loadattrcache() - loads or updates the cache contents from attributes
1481 * that are on the mbuf list
1482 * nfs_getattrcache() - returns valid attributes if found in cache, returns
1483 * error otherwise
1484 */
1485
1486 /*
1487 * Load the attribute cache (that lives in the nfsnode entry) with
1488 * the values on the mbuf list and
1489 * Iff vap not NULL
1490 * copy the attributes to *vaper
1491 */
1492 int
1493 nfsm_loadattrcache(vpp, mdp, dposp, vaper)
1494 struct vnode **vpp;
1495 struct mbuf **mdp;
1496 caddr_t *dposp;
1497 struct vattr *vaper;
1498 {
1499 register int32_t t1;
1500 caddr_t cp2;
1501 int error = 0;
1502 struct mbuf *md;
1503 int v3 = NFS_ISV3(*vpp);
1504
1505 md = *mdp;
1506 t1 = (mtod(md, caddr_t) + md->m_len) - *dposp;
1507 error = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, &cp2);
1508 if (error)
1509 return (error);
1510 return nfs_loadattrcache(vpp, (struct nfs_fattr *)cp2, vaper);
1511 }
1512
1513 int
1514 nfs_loadattrcache(vpp, fp, vaper)
1515 struct vnode **vpp;
1516 struct nfs_fattr *fp;
1517 struct vattr *vaper;
1518 {
1519 register struct vnode *vp = *vpp;
1520 register struct vattr *vap;
1521 int v3 = NFS_ISV3(vp);
1522 enum vtype vtyp;
1523 u_short vmode;
1524 struct timespec mtime;
1525 struct vnode *nvp;
1526 int32_t rdev;
1527 register struct nfsnode *np;
1528 extern int (**spec_nfsv2nodeop_p) __P((void *));
1529
1530 if (v3) {
1531 vtyp = nfsv3tov_type(fp->fa_type);
1532 vmode = fxdr_unsigned(u_short, fp->fa_mode);
1533 rdev = makedev(fxdr_unsigned(u_char, fp->fa3_rdev.specdata1),
1534 fxdr_unsigned(u_char, fp->fa3_rdev.specdata2));
1535 fxdr_nfsv3time(&fp->fa3_mtime, &mtime);
1536 } else {
1537 vtyp = nfsv2tov_type(fp->fa_type);
1538 vmode = fxdr_unsigned(u_short, fp->fa_mode);
1539 if (vtyp == VNON || vtyp == VREG)
1540 vtyp = IFTOVT(vmode);
1541 rdev = fxdr_unsigned(int32_t, fp->fa2_rdev);
1542 fxdr_nfsv2time(&fp->fa2_mtime, &mtime);
1543
1544 /*
1545 * Really ugly NFSv2 kludge.
1546 */
1547 if (vtyp == VCHR && rdev == 0xffffffff)
1548 vtyp = VFIFO;
1549 }
1550
1551 /*
1552 * If v_type == VNON it is a new node, so fill in the v_type,
1553 * n_mtime fields. Check to see if it represents a special
1554 * device, and if so, check for a possible alias. Once the
1555 * correct vnode has been obtained, fill in the rest of the
1556 * information.
1557 */
1558 np = VTONFS(vp);
1559 if (vp->v_type != vtyp) {
1560 vp->v_type = vtyp;
1561 if (vp->v_type == VFIFO) {
1562 #ifndef FIFO
1563 return (EOPNOTSUPP);
1564 #else
1565 extern int (**fifo_nfsv2nodeop_p) __P((void *));
1566 vp->v_op = fifo_nfsv2nodeop_p;
1567 #endif /* FIFO */
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 #ifdef Lite2_integrated
1579 nvp->v_vnlock = vp->v_vnlock;
1580 vp->v_vnlock = NULL;
1581 #endif
1582 nvp->v_data = vp->v_data;
1583 vp->v_data = NULL;
1584 vp->v_op = spec_vnodeop_p;
1585 vrele(vp);
1586 vgone(vp);
1587 /*
1588 * Reinitialize aliased node.
1589 */
1590 np->n_vnode = nvp;
1591 *vpp = vp = nvp;
1592 }
1593 }
1594 np->n_mtime = mtime.tv_sec;
1595 }
1596 vap = np->n_vattr;
1597 vap->va_type = vtyp;
1598 vap->va_mode = vmode & ALLPERMS;
1599 vap->va_rdev = (dev_t)rdev;
1600 vap->va_mtime = mtime;
1601 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
1602 if (v3) {
1603 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1604 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
1605 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
1606 fxdr_hyper(&fp->fa3_size, &vap->va_size);
1607 if (vtyp == VDIR)
1608 vap->va_blocksize = NFS_DIRFRAGSIZ;
1609 else
1610 vap->va_blocksize = NFS_FABLKSIZE;
1611 fxdr_hyper(&fp->fa3_used, &vap->va_bytes);
1612 vap->va_fileid = fxdr_unsigned(int32_t,
1613 fp->fa3_fileid.nfsuquad[1]);
1614 fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime);
1615 fxdr_nfsv3time(&fp->fa3_ctime, &vap->va_ctime);
1616 vap->va_flags = 0;
1617 vap->va_filerev = 0;
1618 } else {
1619 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1620 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
1621 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
1622 vap->va_size = fxdr_unsigned(u_int32_t, fp->fa2_size);
1623 if (vtyp == VDIR)
1624 vap->va_blocksize = NFS_DIRFRAGSIZ;
1625 else
1626 vap->va_blocksize =
1627 fxdr_unsigned(int32_t, fp->fa2_blocksize);
1628 vap->va_bytes = fxdr_unsigned(int32_t, fp->fa2_blocks)
1629 * NFS_FABLKSIZE;
1630 vap->va_fileid = fxdr_unsigned(int32_t, fp->fa2_fileid);
1631 fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime);
1632 vap->va_flags = 0;
1633 vap->va_ctime.tv_sec = fxdr_unsigned(u_int32_t,
1634 fp->fa2_ctime.nfsv2_sec);
1635 vap->va_ctime.tv_nsec = 0;
1636 vap->va_gen = fxdr_unsigned(u_int32_t,fp->fa2_ctime.nfsv2_usec);
1637 vap->va_filerev = 0;
1638 }
1639 if (vap->va_size != np->n_size) {
1640 if (vap->va_type == VREG) {
1641 if (np->n_flag & NMODIFIED) {
1642 if (vap->va_size < np->n_size)
1643 vap->va_size = np->n_size;
1644 else
1645 np->n_size = vap->va_size;
1646 } else
1647 np->n_size = vap->va_size;
1648 #if defined(UVM)
1649 uvm_vnp_setsize(vp, np->n_size);
1650 #else
1651 vnode_pager_setsize(vp, np->n_size);
1652 #endif
1653 } else
1654 np->n_size = vap->va_size;
1655 }
1656 np->n_attrstamp = time.tv_sec;
1657 if (vaper != NULL) {
1658 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap));
1659 if (np->n_flag & NCHG) {
1660 if (np->n_flag & NACC)
1661 vaper->va_atime = np->n_atim;
1662 if (np->n_flag & NUPD)
1663 vaper->va_mtime = np->n_mtim;
1664 }
1665 }
1666 return (0);
1667 }
1668
1669 /*
1670 * Check the time stamp
1671 * If the cache is valid, copy contents to *vap and return 0
1672 * otherwise return an error
1673 */
1674 int
1675 nfs_getattrcache(vp, vaper)
1676 register struct vnode *vp;
1677 struct vattr *vaper;
1678 {
1679 register struct nfsnode *np = VTONFS(vp);
1680 register struct vattr *vap;
1681
1682 if ((time.tv_sec - np->n_attrstamp) >= NFS_ATTRTIMEO(np)) {
1683 nfsstats.attrcache_misses++;
1684 return (ENOENT);
1685 }
1686 nfsstats.attrcache_hits++;
1687 vap = np->n_vattr;
1688 if (vap->va_size != np->n_size) {
1689 if (vap->va_type == VREG) {
1690 if (np->n_flag & NMODIFIED) {
1691 if (vap->va_size < np->n_size)
1692 vap->va_size = np->n_size;
1693 else
1694 np->n_size = vap->va_size;
1695 } else
1696 np->n_size = vap->va_size;
1697 #if defined(UVM)
1698 uvm_vnp_setsize(vp, np->n_size);
1699 #else
1700 vnode_pager_setsize(vp, np->n_size);
1701 #endif
1702 } else
1703 np->n_size = vap->va_size;
1704 }
1705 bcopy((caddr_t)vap, (caddr_t)vaper, 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, *cop;
1737 int error, eof, nc, len;
1738
1739 nc = NFS_DIRFRAGSIZ / 16;
1740 MALLOC(buf, caddr_t, NFS_DIRFRAGSIZ, M_TEMP, M_WAITOK);
1741 MALLOC(cookies, off_t *, nc * sizeof (off_t), M_TEMP, M_WAITOK);
1742
1743 aiov.iov_base = buf;
1744 aiov.iov_len = NFS_DIRFRAGSIZ;
1745 auio.uio_iov = &aiov;
1746 auio.uio_iovcnt = 1;
1747 auio.uio_rw = UIO_READ;
1748 auio.uio_segflg = UIO_SYSSPACE;
1749 auio.uio_procp = p;
1750 auio.uio_resid = NFS_DIRFRAGSIZ;
1751 auio.uio_offset = 0;
1752
1753 error = VOP_READDIR(vp, &auio, cred, &eof, cookies, nc);
1754
1755 len = NFS_DIRFRAGSIZ - auio.uio_resid;
1756 if (error || len == 0) {
1757 FREE(buf, M_TEMP);
1758 FREE(cookies, M_TEMP);
1759 return;
1760 }
1761
1762 /*
1763 * Find the first valid entry and look at its offset cookie.
1764 */
1765
1766 cp = buf;
1767 for (cop = cookies; len > 0; len -= dp->d_reclen) {
1768 dp = (struct dirent *)cp;
1769 if (dp->d_fileno != 0 && len >= dp->d_reclen) {
1770 if ((*cop >> 32) != 0 && (*cop & 0xffffffffLL) == 0) {
1771 *flagp |= NFSMNT_SWAPCOOKIE;
1772 nfs_invaldircache(vp, 0);
1773 nfs_vinvalbuf(vp, 0, cred, p, 1);
1774 }
1775 break;
1776 }
1777 cop++;
1778 cp += dp->d_reclen;
1779 }
1780
1781 FREE(buf, M_TEMP);
1782 FREE(cookies, M_TEMP);
1783 }
1784 #endif /* NFS */
1785
1786 /*
1787 * Set up nameidata for a lookup() call and do it.
1788 *
1789 * If pubflag is set, this call is done for a lookup operation on the
1790 * public filehandle. In that case we allow crossing mountpoints and
1791 * absolute pathnames. However, the caller is expected to check that
1792 * the lookup result is within the public fs, and deny access if
1793 * it is not.
1794 */
1795 int
1796 nfs_namei(ndp, fhp, len, slp, nam, mdp, dposp, retdirp, p, kerbflag, pubflag)
1797 register struct nameidata *ndp;
1798 fhandle_t *fhp;
1799 int len;
1800 struct nfssvc_sock *slp;
1801 struct mbuf *nam;
1802 struct mbuf **mdp;
1803 caddr_t *dposp;
1804 struct vnode **retdirp;
1805 struct proc *p;
1806 int kerbflag, pubflag;
1807 {
1808 register int i, rem;
1809 register struct mbuf *md;
1810 register char *fromcp, *tocp, *cp;
1811 struct iovec aiov;
1812 struct uio auio;
1813 struct vnode *dp;
1814 int error, rdonly, linklen;
1815 struct componentname *cnp = &ndp->ni_cnd;
1816
1817 *retdirp = (struct vnode *)0;
1818 MALLOC(cnp->cn_pnbuf, char *, len + 1, M_NAMEI, M_WAITOK);
1819 /*
1820 * Copy the name from the mbuf list to ndp->ni_pnbuf
1821 * and set the various ndp fields appropriately.
1822 */
1823 fromcp = *dposp;
1824 tocp = cnp->cn_pnbuf;
1825 md = *mdp;
1826 rem = mtod(md, caddr_t) + md->m_len - fromcp;
1827 for (i = 0; i < len; i++) {
1828 while (rem == 0) {
1829 md = md->m_next;
1830 if (md == NULL) {
1831 error = EBADRPC;
1832 goto out;
1833 }
1834 fromcp = mtod(md, caddr_t);
1835 rem = md->m_len;
1836 }
1837 if (*fromcp == '\0' || (!pubflag && *fromcp == '/')) {
1838 error = EACCES;
1839 goto out;
1840 }
1841 *tocp++ = *fromcp++;
1842 rem--;
1843 }
1844 *tocp = '\0';
1845 *mdp = md;
1846 *dposp = fromcp;
1847 len = nfsm_rndup(len)-len;
1848 if (len > 0) {
1849 if (rem >= len)
1850 *dposp += len;
1851 else if ((error = nfs_adv(mdp, dposp, len, rem)) != 0)
1852 goto out;
1853 }
1854
1855 /*
1856 * Extract and set starting directory.
1857 */
1858 error = nfsrv_fhtovp(fhp, FALSE, &dp, ndp->ni_cnd.cn_cred, slp,
1859 nam, &rdonly, kerbflag, pubflag);
1860 if (error)
1861 goto out;
1862 if (dp->v_type != VDIR) {
1863 vrele(dp);
1864 error = ENOTDIR;
1865 goto out;
1866 }
1867
1868 if (rdonly)
1869 cnp->cn_flags |= RDONLY;
1870
1871 *retdirp = dp;
1872
1873 if (pubflag) {
1874 /*
1875 * Oh joy. For WebNFS, handle those pesky '%' escapes,
1876 * and the 'native path' indicator.
1877 */
1878 MALLOC(cp, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
1879 fromcp = cnp->cn_pnbuf;
1880 tocp = cp;
1881 if ((unsigned char)*fromcp >= WEBNFS_SPECCHAR_START) {
1882 switch ((unsigned char)*fromcp) {
1883 case WEBNFS_NATIVE_CHAR:
1884 /*
1885 * 'Native' path for us is the same
1886 * as a path according to the NFS spec,
1887 * just skip the escape char.
1888 */
1889 fromcp++;
1890 break;
1891 /*
1892 * More may be added in the future, range 0x80-0xff
1893 */
1894 default:
1895 error = EIO;
1896 FREE(cp, M_NAMEI);
1897 goto out;
1898 }
1899 }
1900 /*
1901 * Translate the '%' escapes, URL-style.
1902 */
1903 while (*fromcp != '\0') {
1904 if (*fromcp == WEBNFS_ESC_CHAR) {
1905 if (fromcp[1] != '\0' && fromcp[2] != '\0') {
1906 fromcp++;
1907 *tocp++ = HEXSTRTOI(fromcp);
1908 fromcp += 2;
1909 continue;
1910 } else {
1911 error = ENOENT;
1912 FREE(cp, M_NAMEI);
1913 goto out;
1914 }
1915 } else
1916 *tocp++ = *fromcp++;
1917 }
1918 *tocp = '\0';
1919 FREE(cnp->cn_pnbuf, M_NAMEI);
1920 cnp->cn_pnbuf = cp;
1921 }
1922
1923 ndp->ni_pathlen = (tocp - cnp->cn_pnbuf) + 1;
1924 ndp->ni_segflg = UIO_SYSSPACE;
1925
1926 if (pubflag) {
1927 ndp->ni_rootdir = rootvnode;
1928 ndp->ni_loopcnt = 0;
1929 if (cnp->cn_pnbuf[0] == '/')
1930 dp = rootvnode;
1931 } else {
1932 cnp->cn_flags |= NOCROSSMOUNT;
1933 }
1934
1935 cnp->cn_proc = p;
1936 VREF(dp);
1937
1938 for (;;) {
1939 cnp->cn_nameptr = cnp->cn_pnbuf;
1940 ndp->ni_startdir = dp;
1941 /*
1942 * And call lookup() to do the real work
1943 */
1944 error = lookup(ndp);
1945 if (error)
1946 break;
1947 /*
1948 * Check for encountering a symbolic link
1949 */
1950 if ((cnp->cn_flags & ISSYMLINK) == 0) {
1951 if (cnp->cn_flags & (SAVENAME | SAVESTART)) {
1952 cnp->cn_flags |= HASBUF;
1953 return (0);
1954 }
1955 break;
1956 } else {
1957 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
1958 VOP_UNLOCK(ndp->ni_dvp);
1959 if (!pubflag) {
1960 vrele(ndp->ni_dvp);
1961 vput(ndp->ni_vp);
1962 ndp->ni_vp = NULL;
1963 error = EINVAL;
1964 break;
1965 }
1966
1967 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
1968 error = ELOOP;
1969 break;
1970 }
1971 if (ndp->ni_pathlen > 1)
1972 MALLOC(cp, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
1973 else
1974 cp = cnp->cn_pnbuf;
1975 aiov.iov_base = cp;
1976 aiov.iov_len = MAXPATHLEN;
1977 auio.uio_iov = &aiov;
1978 auio.uio_iovcnt = 1;
1979 auio.uio_offset = 0;
1980 auio.uio_rw = UIO_READ;
1981 auio.uio_segflg = UIO_SYSSPACE;
1982 auio.uio_procp = (struct proc *)0;
1983 auio.uio_resid = MAXPATHLEN;
1984 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
1985 if (error) {
1986 badlink:
1987 if (ndp->ni_pathlen > 1)
1988 FREE(cp, M_NAMEI);
1989 break;
1990 }
1991 linklen = MAXPATHLEN - auio.uio_resid;
1992 if (linklen == 0) {
1993 error = ENOENT;
1994 goto badlink;
1995 }
1996 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
1997 error = ENAMETOOLONG;
1998 goto badlink;
1999 }
2000 if (ndp->ni_pathlen > 1) {
2001 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
2002 FREE(cnp->cn_pnbuf, M_NAMEI);
2003 cnp->cn_pnbuf = cp;
2004 } else
2005 cnp->cn_pnbuf[linklen] = '\0';
2006 ndp->ni_pathlen += linklen;
2007 vput(ndp->ni_vp);
2008 dp = ndp->ni_dvp;
2009 /*
2010 * Check if root directory should replace current directory.
2011 */
2012 if (cnp->cn_pnbuf[0] == '/') {
2013 vrele(dp);
2014 dp = ndp->ni_rootdir;
2015 VREF(dp);
2016 }
2017 }
2018 }
2019 out:
2020 FREE(cnp->cn_pnbuf, M_NAMEI);
2021 return (error);
2022 }
2023
2024 /*
2025 * A fiddled version of m_adj() that ensures null fill to a long
2026 * boundary and only trims off the back end
2027 */
2028 void
2029 nfsm_adj(mp, len, nul)
2030 struct mbuf *mp;
2031 register int len;
2032 int nul;
2033 {
2034 register struct mbuf *m;
2035 register int count, i;
2036 register char *cp;
2037
2038 /*
2039 * Trim from tail. Scan the mbuf chain,
2040 * calculating its length and finding the last mbuf.
2041 * If the adjustment only affects this mbuf, then just
2042 * adjust and return. Otherwise, rescan and truncate
2043 * after the remaining size.
2044 */
2045 count = 0;
2046 m = mp;
2047 for (;;) {
2048 count += m->m_len;
2049 if (m->m_next == (struct mbuf *)0)
2050 break;
2051 m = m->m_next;
2052 }
2053 if (m->m_len > len) {
2054 m->m_len -= len;
2055 if (nul > 0) {
2056 cp = mtod(m, caddr_t)+m->m_len-nul;
2057 for (i = 0; i < nul; i++)
2058 *cp++ = '\0';
2059 }
2060 return;
2061 }
2062 count -= len;
2063 if (count < 0)
2064 count = 0;
2065 /*
2066 * Correct length for chain is "count".
2067 * Find the mbuf with last data, adjust its length,
2068 * and toss data from remaining mbufs on chain.
2069 */
2070 for (m = mp; m; m = m->m_next) {
2071 if (m->m_len >= count) {
2072 m->m_len = count;
2073 if (nul > 0) {
2074 cp = mtod(m, caddr_t)+m->m_len-nul;
2075 for (i = 0; i < nul; i++)
2076 *cp++ = '\0';
2077 }
2078 break;
2079 }
2080 count -= m->m_len;
2081 }
2082 for (m = m->m_next;m;m = m->m_next)
2083 m->m_len = 0;
2084 }
2085
2086 /*
2087 * Make these functions instead of macros, so that the kernel text size
2088 * doesn't get too big...
2089 */
2090 void
2091 nfsm_srvwcc(nfsd, before_ret, before_vap, after_ret, after_vap, mbp, bposp)
2092 struct nfsrv_descript *nfsd;
2093 int before_ret;
2094 register struct vattr *before_vap;
2095 int after_ret;
2096 struct vattr *after_vap;
2097 struct mbuf **mbp;
2098 char **bposp;
2099 {
2100 register struct mbuf *mb = *mbp, *mb2;
2101 register char *bpos = *bposp;
2102 register u_int32_t *tl;
2103
2104 if (before_ret) {
2105 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
2106 *tl = nfs_false;
2107 } else {
2108 nfsm_build(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
2109 *tl++ = nfs_true;
2110 txdr_hyper(&(before_vap->va_size), tl);
2111 tl += 2;
2112 txdr_nfsv3time(&(before_vap->va_mtime), tl);
2113 tl += 2;
2114 txdr_nfsv3time(&(before_vap->va_ctime), tl);
2115 }
2116 *bposp = bpos;
2117 *mbp = mb;
2118 nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp);
2119 }
2120
2121 void
2122 nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp)
2123 struct nfsrv_descript *nfsd;
2124 int after_ret;
2125 struct vattr *after_vap;
2126 struct mbuf **mbp;
2127 char **bposp;
2128 {
2129 register struct mbuf *mb = *mbp, *mb2;
2130 register char *bpos = *bposp;
2131 register u_int32_t *tl;
2132 register struct nfs_fattr *fp;
2133
2134 if (after_ret) {
2135 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
2136 *tl = nfs_false;
2137 } else {
2138 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FATTR);
2139 *tl++ = nfs_true;
2140 fp = (struct nfs_fattr *)tl;
2141 nfsm_srvfattr(nfsd, after_vap, fp);
2142 }
2143 *mbp = mb;
2144 *bposp = bpos;
2145 }
2146
2147 void
2148 nfsm_srvfattr(nfsd, vap, fp)
2149 register struct nfsrv_descript *nfsd;
2150 register struct vattr *vap;
2151 register struct nfs_fattr *fp;
2152 {
2153
2154 fp->fa_nlink = txdr_unsigned(vap->va_nlink);
2155 fp->fa_uid = txdr_unsigned(vap->va_uid);
2156 fp->fa_gid = txdr_unsigned(vap->va_gid);
2157 if (nfsd->nd_flag & ND_NFSV3) {
2158 fp->fa_type = vtonfsv3_type(vap->va_type);
2159 fp->fa_mode = vtonfsv3_mode(vap->va_mode);
2160 txdr_hyper(&vap->va_size, &fp->fa3_size);
2161 txdr_hyper(&vap->va_bytes, &fp->fa3_used);
2162 fp->fa3_rdev.specdata1 = txdr_unsigned(major(vap->va_rdev));
2163 fp->fa3_rdev.specdata2 = txdr_unsigned(minor(vap->va_rdev));
2164 fp->fa3_fsid.nfsuquad[0] = 0;
2165 fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(vap->va_fsid);
2166 fp->fa3_fileid.nfsuquad[0] = 0;
2167 fp->fa3_fileid.nfsuquad[1] = txdr_unsigned(vap->va_fileid);
2168 txdr_nfsv3time(&vap->va_atime, &fp->fa3_atime);
2169 txdr_nfsv3time(&vap->va_mtime, &fp->fa3_mtime);
2170 txdr_nfsv3time(&vap->va_ctime, &fp->fa3_ctime);
2171 } else {
2172 fp->fa_type = vtonfsv2_type(vap->va_type);
2173 fp->fa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
2174 fp->fa2_size = txdr_unsigned(vap->va_size);
2175 fp->fa2_blocksize = txdr_unsigned(vap->va_blocksize);
2176 if (vap->va_type == VFIFO)
2177 fp->fa2_rdev = 0xffffffff;
2178 else
2179 fp->fa2_rdev = txdr_unsigned(vap->va_rdev);
2180 fp->fa2_blocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE);
2181 fp->fa2_fsid = txdr_unsigned(vap->va_fsid);
2182 fp->fa2_fileid = txdr_unsigned(vap->va_fileid);
2183 txdr_nfsv2time(&vap->va_atime, &fp->fa2_atime);
2184 txdr_nfsv2time(&vap->va_mtime, &fp->fa2_mtime);
2185 txdr_nfsv2time(&vap->va_ctime, &fp->fa2_ctime);
2186 }
2187 }
2188
2189 /*
2190 * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked)
2191 * - look up fsid in mount list (if not found ret error)
2192 * - get vp and export rights by calling VFS_FHTOVP()
2193 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
2194 * - if not lockflag unlock it with VOP_UNLOCK()
2195 */
2196 int
2197 nfsrv_fhtovp(fhp, lockflag, vpp, cred, slp, nam, rdonlyp, kerbflag, pubflag)
2198 fhandle_t *fhp;
2199 int lockflag;
2200 struct vnode **vpp;
2201 struct ucred *cred;
2202 struct nfssvc_sock *slp;
2203 struct mbuf *nam;
2204 int *rdonlyp;
2205 int kerbflag;
2206 {
2207 #ifdef Lite2_integrated
2208 struct proc *p = curproc; /* XXX */
2209 #endif
2210 register struct mount *mp;
2211 register int i;
2212 struct ucred *credanon;
2213 int error, exflags;
2214 struct sockaddr_in *saddr;
2215
2216 *vpp = (struct vnode *)0;
2217
2218 if (nfs_ispublicfh(fhp)) {
2219 if (!pubflag || !nfs_pub.np_valid)
2220 return (ESTALE);
2221 fhp = &nfs_pub.np_handle;
2222 }
2223
2224 #ifdef Lite2_integrated
2225 mp = vfs_getvfs(&fhp->fh_fsid);
2226 #else
2227 mp = getvfs(&fhp->fh_fsid);
2228 #endif
2229 if (!mp)
2230 return (ESTALE);
2231 error = VFS_FHTOVP(mp, &fhp->fh_fid, nam, vpp, &exflags, &credanon);
2232 if (error)
2233 return (error);
2234
2235 if (!(exflags & (MNT_EXNORESPORT|MNT_EXPUBLIC))) {
2236 saddr = mtod(nam, struct sockaddr_in *);
2237 if (saddr->sin_family == AF_INET &&
2238 ntohs(saddr->sin_port) >= IPPORT_RESERVED) {
2239 vput(*vpp);
2240 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
2241 }
2242 }
2243 /*
2244 * Check/setup credentials.
2245 */
2246 if (exflags & MNT_EXKERB) {
2247 if (!kerbflag) {
2248 vput(*vpp);
2249 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
2250 }
2251 } else if (kerbflag) {
2252 vput(*vpp);
2253 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
2254 } else if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
2255 cred->cr_uid = credanon->cr_uid;
2256 cred->cr_gid = credanon->cr_gid;
2257 for (i = 0; i < credanon->cr_ngroups && i < NGROUPS; i++)
2258 cred->cr_groups[i] = credanon->cr_groups[i];
2259 cred->cr_ngroups = i;
2260 }
2261 if (exflags & MNT_EXRDONLY)
2262 *rdonlyp = 1;
2263 else
2264 *rdonlyp = 0;
2265 if (!lockflag)
2266 #ifdef Lite2_integrated
2267 VOP_UNLOCK(*vpp, 0, p);
2268 #else
2269 VOP_UNLOCK(*vpp);
2270 #endif
2271 return (0);
2272 }
2273
2274 /*
2275 * WebNFS: check if a filehandle is a public filehandle. For v3, this
2276 * means a length of 0, for v2 it means all zeroes. nfsm_srvmtofh has
2277 * transformed this to all zeroes in both cases, so check for it.
2278 */
2279 int
2280 nfs_ispublicfh(fhp)
2281 fhandle_t *fhp;
2282 {
2283 char *cp = (char *)fhp;
2284 int i;
2285
2286 for (i = 0; i < NFSX_V3FH; i++)
2287 if (*cp++ != 0)
2288 return (FALSE);
2289 return (TRUE);
2290 }
2291
2292 /*
2293 * This function compares two net addresses by family and returns TRUE
2294 * if they are the same host.
2295 * If there is any doubt, return FALSE.
2296 * The AF_INET family is handled as a special case so that address mbufs
2297 * don't need to be saved to store "struct in_addr", which is only 4 bytes.
2298 */
2299 int
2300 netaddr_match(family, haddr, nam)
2301 int family;
2302 union nethostaddr *haddr;
2303 struct mbuf *nam;
2304 {
2305 register struct sockaddr_in *inetaddr;
2306
2307 switch (family) {
2308 case AF_INET:
2309 inetaddr = mtod(nam, struct sockaddr_in *);
2310 if (inetaddr->sin_family == AF_INET &&
2311 inetaddr->sin_addr.s_addr == haddr->had_inetaddr)
2312 return (1);
2313 break;
2314 #ifdef ISO
2315 case AF_ISO:
2316 {
2317 register struct sockaddr_iso *isoaddr1, *isoaddr2;
2318
2319 isoaddr1 = mtod(nam, struct sockaddr_iso *);
2320 isoaddr2 = mtod(haddr->had_nam, struct sockaddr_iso *);
2321 if (isoaddr1->siso_family == AF_ISO &&
2322 isoaddr1->siso_nlen > 0 &&
2323 isoaddr1->siso_nlen == isoaddr2->siso_nlen &&
2324 SAME_ISOADDR(isoaddr1, isoaddr2))
2325 return (1);
2326 break;
2327 }
2328 #endif /* ISO */
2329 default:
2330 break;
2331 };
2332 return (0);
2333 }
2334
2335
2336 /*
2337 * The write verifier has changed (probably due to a server reboot), so all
2338 * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
2339 * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
2340 * flag. Once done the new write verifier can be set for the mount point.
2341 */
2342 void
2343 nfs_clearcommit(mp)
2344 struct mount *mp;
2345 {
2346 register struct vnode *vp, *nvp;
2347 register struct buf *bp, *nbp;
2348 int s;
2349
2350 s = splbio();
2351 loop:
2352 for (vp = mp->mnt_vnodelist.lh_first; vp; vp = nvp) {
2353 if (vp->v_mount != mp) /* Paranoia */
2354 goto loop;
2355 nvp = vp->v_mntvnodes.le_next;
2356 for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = nbp) {
2357 nbp = bp->b_vnbufs.le_next;
2358 if ((bp->b_flags & (B_BUSY | B_DELWRI | B_NEEDCOMMIT))
2359 == (B_DELWRI | B_NEEDCOMMIT))
2360 bp->b_flags &= ~B_NEEDCOMMIT;
2361 }
2362 }
2363 splx(s);
2364 }
2365
2366 /*
2367 * Map errnos to NFS error numbers. For Version 3 also filter out error
2368 * numbers not specified for the associated procedure.
2369 */
2370 int
2371 nfsrv_errmap(nd, err)
2372 struct nfsrv_descript *nd;
2373 register int err;
2374 {
2375 register short *defaulterrp, *errp;
2376
2377 if (nd->nd_flag & ND_NFSV3) {
2378 if (nd->nd_procnum <= NFSPROC_COMMIT) {
2379 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
2380 while (*++errp) {
2381 if (*errp == err)
2382 return (err);
2383 else if (*errp > err)
2384 break;
2385 }
2386 return ((int)*defaulterrp);
2387 } else
2388 return (err & 0xffff);
2389 }
2390 if (err <= ELAST)
2391 return ((int)nfsrv_v2errmap[err - 1]);
2392 return (NFSERR_IO);
2393 }
2394
2395 /*
2396 * Sort the group list in increasing numerical order.
2397 * (Insertion sort by Chris Torek, who was grossed out by the bubble sort
2398 * that used to be here.)
2399 */
2400 void
2401 nfsrvw_sort(list, num)
2402 register gid_t *list;
2403 register int num;
2404 {
2405 register int i, j;
2406 gid_t v;
2407
2408 /* Insertion sort. */
2409 for (i = 1; i < num; i++) {
2410 v = list[i];
2411 /* find correct slot for value v, moving others up */
2412 for (j = i; --j >= 0 && v < list[j];)
2413 list[j + 1] = list[j];
2414 list[j + 1] = v;
2415 }
2416 }
2417
2418 /*
2419 * copy credentials making sure that the result can be compared with bcmp().
2420 */
2421 void
2422 nfsrv_setcred(incred, outcred)
2423 register struct ucred *incred, *outcred;
2424 {
2425 register int i;
2426
2427 bzero((caddr_t)outcred, sizeof (struct ucred));
2428 outcred->cr_ref = 1;
2429 outcred->cr_uid = incred->cr_uid;
2430 outcred->cr_gid = incred->cr_gid;
2431 outcred->cr_ngroups = incred->cr_ngroups;
2432 for (i = 0; i < incred->cr_ngroups; i++)
2433 outcred->cr_groups[i] = incred->cr_groups[i];
2434 nfsrvw_sort(outcred->cr_groups, outcred->cr_ngroups);
2435 }
2436