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