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