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