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