nfs_subs.c revision 1.162 1 /* $NetBSD: nfs_subs.c,v 1.162 2006/05/14 21:32:21 elad 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.162 2006/05/14 21:32:21 elad 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) {
996 if (havebuf) {
997 /* If the first mbuf with data has external data
998 * and there is a previous empty mbuf use it
999 * to move the data into.
1000 */
1001 m2 = m1;
1002 *mdp = m1 = havebuf;
1003 if (m1->m_flags & M_EXT) {
1004 MEXTREMOVE(m1);
1005 }
1006 } else {
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 = m_get(M_WAIT, MT_DATA);
1015 m2->m_ext = m1->m_ext;
1016 m2->m_data = src;
1017 m2->m_len = left;
1018 MCLADDREFERENCE(m1, m2);
1019 MEXTREMOVE(m1);
1020 m2->m_next = m1->m_next;
1021 m1->m_next = m2;
1022 }
1023 m1->m_len = 0;
1024 if (m1->m_flags & M_PKTHDR)
1025 dst = m1->m_pktdat;
1026 else
1027 dst = m1->m_dat;
1028 m1->m_data = dst;
1029 } else {
1030 /*
1031 * If the first mbuf has no external data
1032 * move the data to the front of the mbuf.
1033 */
1034 if (m1->m_flags & M_PKTHDR)
1035 dst = m1->m_pktdat;
1036 else
1037 dst = m1->m_dat;
1038 m1->m_data = dst;
1039 if (dst != src)
1040 memmove(dst, src, left);
1041 dst += left;
1042 m1->m_len = left;
1043 m2 = m1->m_next;
1044 }
1045 *cp2 = m1->m_data;
1046 *dposp = mtod(m1, caddr_t) + siz;
1047 /*
1048 * Loop through mbufs pulling data up into first mbuf until
1049 * the first mbuf is full or there is no more data to
1050 * pullup.
1051 */
1052 while ((len = M_TRAILINGSPACE(m1)) != 0 && m2) {
1053 if ((len = min(len, m2->m_len)) != 0)
1054 memcpy(dst, m2->m_data, len);
1055 m1->m_len += len;
1056 dst += len;
1057 m2->m_data += len;
1058 m2->m_len -= len;
1059 m2 = m2->m_next;
1060 }
1061 if (m1->m_len < siz)
1062 return (EBADRPC);
1063 return (0);
1064 }
1065
1066 /*
1067 * Advance the position in the mbuf chain.
1068 */
1069 int
1070 nfs_adv(mdp, dposp, offs, left)
1071 struct mbuf **mdp;
1072 caddr_t *dposp;
1073 int offs;
1074 int left;
1075 {
1076 struct mbuf *m;
1077 int s;
1078
1079 m = *mdp;
1080 s = left;
1081 while (s < offs) {
1082 offs -= s;
1083 m = m->m_next;
1084 if (m == NULL)
1085 return (EBADRPC);
1086 s = m->m_len;
1087 }
1088 *mdp = m;
1089 *dposp = mtod(m, caddr_t)+offs;
1090 return (0);
1091 }
1092
1093 /*
1094 * Copy a string into mbufs for the hard cases...
1095 */
1096 int
1097 nfsm_strtmbuf(mb, bpos, cp, siz)
1098 struct mbuf **mb;
1099 char **bpos;
1100 const char *cp;
1101 long siz;
1102 {
1103 struct mbuf *m1 = NULL, *m2;
1104 long left, xfer, len, tlen;
1105 u_int32_t *tl;
1106 int putsize;
1107
1108 putsize = 1;
1109 m2 = *mb;
1110 left = M_TRAILINGSPACE(m2);
1111 if (left > 0) {
1112 tl = ((u_int32_t *)(*bpos));
1113 *tl++ = txdr_unsigned(siz);
1114 putsize = 0;
1115 left -= NFSX_UNSIGNED;
1116 m2->m_len += NFSX_UNSIGNED;
1117 if (left > 0) {
1118 memcpy((caddr_t) tl, cp, left);
1119 siz -= left;
1120 cp += left;
1121 m2->m_len += left;
1122 left = 0;
1123 }
1124 }
1125 /* Loop around adding mbufs */
1126 while (siz > 0) {
1127 m1 = m_get(M_WAIT, MT_DATA);
1128 MCLAIM(m1, &nfs_mowner);
1129 if (siz > MLEN)
1130 m_clget(m1, M_WAIT);
1131 m1->m_len = NFSMSIZ(m1);
1132 m2->m_next = m1;
1133 m2 = m1;
1134 tl = mtod(m1, u_int32_t *);
1135 tlen = 0;
1136 if (putsize) {
1137 *tl++ = txdr_unsigned(siz);
1138 m1->m_len -= NFSX_UNSIGNED;
1139 tlen = NFSX_UNSIGNED;
1140 putsize = 0;
1141 }
1142 if (siz < m1->m_len) {
1143 len = nfsm_rndup(siz);
1144 xfer = siz;
1145 if (xfer < len)
1146 *(tl+(xfer>>2)) = 0;
1147 } else {
1148 xfer = len = m1->m_len;
1149 }
1150 memcpy((caddr_t) tl, cp, xfer);
1151 m1->m_len = len+tlen;
1152 siz -= xfer;
1153 cp += xfer;
1154 }
1155 *mb = m1;
1156 *bpos = mtod(m1, caddr_t)+m1->m_len;
1157 return (0);
1158 }
1159
1160 /*
1161 * Directory caching routines. They work as follows:
1162 * - a cache is maintained per VDIR nfsnode.
1163 * - for each offset cookie that is exported to userspace, and can
1164 * thus be thrown back at us as an offset to VOP_READDIR, store
1165 * information in the cache.
1166 * - cached are:
1167 * - cookie itself
1168 * - blocknumber (essentially just a search key in the buffer cache)
1169 * - entry number in block.
1170 * - offset cookie of block in which this entry is stored
1171 * - 32 bit cookie if NFSMNT_XLATECOOKIE is used.
1172 * - entries are looked up in a hash table
1173 * - also maintained is an LRU list of entries, used to determine
1174 * which ones to delete if the cache grows too large.
1175 * - if 32 <-> 64 translation mode is requested for a filesystem,
1176 * the cache also functions as a translation table
1177 * - in the translation case, invalidating the cache does not mean
1178 * flushing it, but just marking entries as invalid, except for
1179 * the <64bit cookie, 32bitcookie> pair which is still valid, to
1180 * still be able to use the cache as a translation table.
1181 * - 32 bit cookies are uniquely created by combining the hash table
1182 * entry value, and one generation count per hash table entry,
1183 * incremented each time an entry is appended to the chain.
1184 * - the cache is invalidated each time a direcory is modified
1185 * - sanity checks are also done; if an entry in a block turns
1186 * out not to have a matching cookie, the cache is invalidated
1187 * and a new block starting from the wanted offset is fetched from
1188 * the server.
1189 * - directory entries as read from the server are extended to contain
1190 * the 64bit and, optionally, the 32bit cookies, for sanity checking
1191 * the cache and exporting them to userspace through the cookie
1192 * argument to VOP_READDIR.
1193 */
1194
1195 u_long
1196 nfs_dirhash(off)
1197 off_t off;
1198 {
1199 int i;
1200 char *cp = (char *)&off;
1201 u_long sum = 0L;
1202
1203 for (i = 0 ; i < sizeof (off); i++)
1204 sum += *cp++;
1205
1206 return sum;
1207 }
1208
1209 #define _NFSDC_MTX(np) (&NFSTOV(np)->v_interlock)
1210 #define NFSDC_LOCK(np) simple_lock(_NFSDC_MTX(np))
1211 #define NFSDC_UNLOCK(np) simple_unlock(_NFSDC_MTX(np))
1212 #define NFSDC_ASSERT_LOCKED(np) LOCK_ASSERT(simple_lock_held(_NFSDC_MTX(np)))
1213
1214 void
1215 nfs_initdircache(vp)
1216 struct vnode *vp;
1217 {
1218 struct nfsnode *np = VTONFS(vp);
1219 struct nfsdirhashhead *dircache;
1220
1221 dircache = hashinit(NFS_DIRHASHSIZ, HASH_LIST, M_NFSDIROFF,
1222 M_WAITOK, &nfsdirhashmask);
1223
1224 NFSDC_LOCK(np);
1225 if (np->n_dircache == NULL) {
1226 np->n_dircachesize = 0;
1227 np->n_dircache = dircache;
1228 dircache = NULL;
1229 TAILQ_INIT(&np->n_dirchain);
1230 }
1231 NFSDC_UNLOCK(np);
1232 if (dircache)
1233 hashdone(dircache, M_NFSDIROFF);
1234 }
1235
1236 void
1237 nfs_initdirxlatecookie(vp)
1238 struct vnode *vp;
1239 {
1240 struct nfsnode *np = VTONFS(vp);
1241 unsigned *dirgens;
1242
1243 KASSERT(VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_XLATECOOKIE);
1244
1245 dirgens = malloc(NFS_DIRHASHSIZ * sizeof (unsigned), M_NFSDIROFF,
1246 M_WAITOK|M_ZERO);
1247 NFSDC_LOCK(np);
1248 if (np->n_dirgens == NULL) {
1249 np->n_dirgens = dirgens;
1250 dirgens = NULL;
1251 }
1252 NFSDC_UNLOCK(np);
1253 if (dirgens)
1254 free(dirgens, M_NFSDIROFF);
1255 }
1256
1257 static const struct nfsdircache dzero;
1258
1259 static void nfs_unlinkdircache __P((struct nfsnode *np, struct nfsdircache *));
1260 static void nfs_putdircache_unlocked __P((struct nfsnode *,
1261 struct nfsdircache *));
1262
1263 static void
1264 nfs_unlinkdircache(np, ndp)
1265 struct nfsnode *np;
1266 struct nfsdircache *ndp;
1267 {
1268
1269 NFSDC_ASSERT_LOCKED(np);
1270 KASSERT(ndp != &dzero);
1271
1272 if (LIST_NEXT(ndp, dc_hash) == (void *)-1)
1273 return;
1274
1275 TAILQ_REMOVE(&np->n_dirchain, ndp, dc_chain);
1276 LIST_REMOVE(ndp, dc_hash);
1277 LIST_NEXT(ndp, dc_hash) = (void *)-1; /* mark as unlinked */
1278
1279 nfs_putdircache_unlocked(np, ndp);
1280 }
1281
1282 void
1283 nfs_putdircache(np, ndp)
1284 struct nfsnode *np;
1285 struct nfsdircache *ndp;
1286 {
1287 int ref;
1288
1289 if (ndp == &dzero)
1290 return;
1291
1292 KASSERT(ndp->dc_refcnt > 0);
1293 NFSDC_LOCK(np);
1294 ref = --ndp->dc_refcnt;
1295 NFSDC_UNLOCK(np);
1296
1297 if (ref == 0)
1298 free(ndp, M_NFSDIROFF);
1299 }
1300
1301 static void
1302 nfs_putdircache_unlocked(np, ndp)
1303 struct nfsnode *np;
1304 struct nfsdircache *ndp;
1305 {
1306 int ref;
1307
1308 NFSDC_ASSERT_LOCKED(np);
1309
1310 if (ndp == &dzero)
1311 return;
1312
1313 KASSERT(ndp->dc_refcnt > 0);
1314 ref = --ndp->dc_refcnt;
1315 if (ref == 0)
1316 free(ndp, M_NFSDIROFF);
1317 }
1318
1319 struct nfsdircache *
1320 nfs_searchdircache(vp, off, do32, hashent)
1321 struct vnode *vp;
1322 off_t off;
1323 int do32;
1324 int *hashent;
1325 {
1326 struct nfsdirhashhead *ndhp;
1327 struct nfsdircache *ndp = NULL;
1328 struct nfsnode *np = VTONFS(vp);
1329 unsigned ent;
1330
1331 /*
1332 * Zero is always a valid cookie.
1333 */
1334 if (off == 0)
1335 /* XXXUNCONST */
1336 return (struct nfsdircache *)__UNCONST(&dzero);
1337
1338 if (!np->n_dircache)
1339 return NULL;
1340
1341 /*
1342 * We use a 32bit cookie as search key, directly reconstruct
1343 * the hashentry. Else use the hashfunction.
1344 */
1345 if (do32) {
1346 ent = (u_int32_t)off >> 24;
1347 if (ent >= NFS_DIRHASHSIZ)
1348 return NULL;
1349 ndhp = &np->n_dircache[ent];
1350 } else {
1351 ndhp = NFSDIRHASH(np, off);
1352 }
1353
1354 if (hashent)
1355 *hashent = (int)(ndhp - np->n_dircache);
1356
1357 NFSDC_LOCK(np);
1358 if (do32) {
1359 LIST_FOREACH(ndp, ndhp, dc_hash) {
1360 if (ndp->dc_cookie32 == (u_int32_t)off) {
1361 /*
1362 * An invalidated entry will become the
1363 * start of a new block fetched from
1364 * the server.
1365 */
1366 if (ndp->dc_flags & NFSDC_INVALID) {
1367 ndp->dc_blkcookie = ndp->dc_cookie;
1368 ndp->dc_entry = 0;
1369 ndp->dc_flags &= ~NFSDC_INVALID;
1370 }
1371 break;
1372 }
1373 }
1374 } else {
1375 LIST_FOREACH(ndp, ndhp, dc_hash) {
1376 if (ndp->dc_cookie == off)
1377 break;
1378 }
1379 }
1380 if (ndp != NULL)
1381 ndp->dc_refcnt++;
1382 NFSDC_UNLOCK(np);
1383 return ndp;
1384 }
1385
1386
1387 struct nfsdircache *
1388 nfs_enterdircache(vp, off, blkoff, en, blkno)
1389 struct vnode *vp;
1390 off_t off, blkoff;
1391 int en;
1392 daddr_t blkno;
1393 {
1394 struct nfsnode *np = VTONFS(vp);
1395 struct nfsdirhashhead *ndhp;
1396 struct nfsdircache *ndp = NULL;
1397 struct nfsdircache *newndp = NULL;
1398 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1399 int hashent = 0, gen, overwrite; /* XXX: GCC */
1400
1401 /*
1402 * XXX refuse entries for offset 0. amd(8) erroneously sets
1403 * cookie 0 for the '.' entry, making this necessary. This
1404 * isn't so bad, as 0 is a special case anyway.
1405 */
1406 if (off == 0)
1407 /* XXXUNCONST */
1408 return (struct nfsdircache *)__UNCONST(&dzero);
1409
1410 if (!np->n_dircache)
1411 /*
1412 * XXX would like to do this in nfs_nget but vtype
1413 * isn't known at that time.
1414 */
1415 nfs_initdircache(vp);
1416
1417 if ((nmp->nm_flag & NFSMNT_XLATECOOKIE) && !np->n_dirgens)
1418 nfs_initdirxlatecookie(vp);
1419
1420 retry:
1421 ndp = nfs_searchdircache(vp, off, 0, &hashent);
1422
1423 NFSDC_LOCK(np);
1424 if (ndp && (ndp->dc_flags & NFSDC_INVALID) == 0) {
1425 /*
1426 * Overwriting an old entry. Check if it's the same.
1427 * If so, just return. If not, remove the old entry.
1428 */
1429 if (ndp->dc_blkcookie == blkoff && ndp->dc_entry == en)
1430 goto done;
1431 nfs_unlinkdircache(np, ndp);
1432 nfs_putdircache_unlocked(np, ndp);
1433 ndp = NULL;
1434 }
1435
1436 ndhp = &np->n_dircache[hashent];
1437
1438 if (!ndp) {
1439 if (newndp == NULL) {
1440 NFSDC_UNLOCK(np);
1441 newndp = malloc(sizeof(*ndp), M_NFSDIROFF, M_WAITOK);
1442 newndp->dc_refcnt = 1;
1443 LIST_NEXT(newndp, dc_hash) = (void *)-1;
1444 goto retry;
1445 }
1446 ndp = newndp;
1447 newndp = NULL;
1448 overwrite = 0;
1449 if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
1450 /*
1451 * We're allocating a new entry, so bump the
1452 * generation number.
1453 */
1454 KASSERT(np->n_dirgens);
1455 gen = ++np->n_dirgens[hashent];
1456 if (gen == 0) {
1457 np->n_dirgens[hashent]++;
1458 gen++;
1459 }
1460 ndp->dc_cookie32 = (hashent << 24) | (gen & 0xffffff);
1461 }
1462 } else
1463 overwrite = 1;
1464
1465 ndp->dc_cookie = off;
1466 ndp->dc_blkcookie = blkoff;
1467 ndp->dc_entry = en;
1468 ndp->dc_flags = 0;
1469
1470 if (overwrite)
1471 goto done;
1472
1473 /*
1474 * If the maximum directory cookie cache size has been reached
1475 * for this node, take one off the front. The idea is that
1476 * directories are typically read front-to-back once, so that
1477 * the oldest entries can be thrown away without much performance
1478 * loss.
1479 */
1480 if (np->n_dircachesize == NFS_MAXDIRCACHE) {
1481 nfs_unlinkdircache(np, TAILQ_FIRST(&np->n_dirchain));
1482 } else
1483 np->n_dircachesize++;
1484
1485 KASSERT(ndp->dc_refcnt == 1);
1486 LIST_INSERT_HEAD(ndhp, ndp, dc_hash);
1487 TAILQ_INSERT_TAIL(&np->n_dirchain, ndp, dc_chain);
1488 ndp->dc_refcnt++;
1489 done:
1490 KASSERT(ndp->dc_refcnt > 0);
1491 NFSDC_UNLOCK(np);
1492 if (newndp)
1493 nfs_putdircache(np, newndp);
1494 return ndp;
1495 }
1496
1497 void
1498 nfs_invaldircache(vp, flags)
1499 struct vnode *vp;
1500 int flags;
1501 {
1502 struct nfsnode *np = VTONFS(vp);
1503 struct nfsdircache *ndp = NULL;
1504 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1505 const boolean_t forcefree = flags & NFS_INVALDIRCACHE_FORCE;
1506
1507 #ifdef DIAGNOSTIC
1508 if (vp->v_type != VDIR)
1509 panic("nfs: invaldircache: not dir");
1510 #endif
1511
1512 if ((flags & NFS_INVALDIRCACHE_KEEPEOF) == 0)
1513 np->n_flag &= ~NEOFVALID;
1514
1515 if (!np->n_dircache)
1516 return;
1517
1518 NFSDC_LOCK(np);
1519 if (!(nmp->nm_flag & NFSMNT_XLATECOOKIE) || forcefree) {
1520 while ((ndp = TAILQ_FIRST(&np->n_dirchain)) != NULL) {
1521 KASSERT(!forcefree || ndp->dc_refcnt == 1);
1522 nfs_unlinkdircache(np, ndp);
1523 }
1524 np->n_dircachesize = 0;
1525 if (forcefree && np->n_dirgens) {
1526 FREE(np->n_dirgens, M_NFSDIROFF);
1527 np->n_dirgens = NULL;
1528 }
1529 } else {
1530 TAILQ_FOREACH(ndp, &np->n_dirchain, dc_chain)
1531 ndp->dc_flags |= NFSDC_INVALID;
1532 }
1533
1534 NFSDC_UNLOCK(np);
1535 }
1536
1537 /*
1538 * Called once before VFS init to initialize shared and
1539 * server-specific data structures.
1540 */
1541 static int
1542 nfs_init0(void)
1543 {
1544 nfsrtt.pos = 0;
1545 rpc_vers = txdr_unsigned(RPC_VER2);
1546 rpc_call = txdr_unsigned(RPC_CALL);
1547 rpc_reply = txdr_unsigned(RPC_REPLY);
1548 rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED);
1549 rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED);
1550 rpc_mismatch = txdr_unsigned(RPC_MISMATCH);
1551 rpc_autherr = txdr_unsigned(RPC_AUTHERR);
1552 rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX);
1553 rpc_auth_kerb = txdr_unsigned(RPCAUTH_KERB4);
1554 nfs_prog = txdr_unsigned(NFS_PROG);
1555 nqnfs_prog = txdr_unsigned(NQNFS_PROG);
1556 nfs_true = txdr_unsigned(TRUE);
1557 nfs_false = txdr_unsigned(FALSE);
1558 nfs_xdrneg1 = txdr_unsigned(-1);
1559 nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000;
1560 if (nfs_ticks < 1)
1561 nfs_ticks = 1;
1562 #ifdef NFSSERVER
1563 nfsrv_init(0); /* Init server data structures */
1564 nfsrv_initcache(); /* Init the server request cache */
1565 pool_init(&nfs_srvdesc_pool, sizeof(struct nfsrv_descript),
1566 0, 0, 0, "nfsrvdescpl", &pool_allocator_nointr);
1567 #endif /* NFSSERVER */
1568
1569 #if defined(NFSSERVER) || !defined(NFS_V2_ONLY)
1570 /*
1571 * Initialize the nqnfs data structures.
1572 */
1573 if (nqnfsstarttime == 0) {
1574 nqnfsstarttime = boottime.tv_sec + nqsrv_maxlease
1575 + nqsrv_clockskew + nqsrv_writeslack;
1576 NQLOADNOVRAM(nqnfsstarttime);
1577 CIRCLEQ_INIT(&nqtimerhead);
1578 nqfhhashtbl = hashinit(NQLCHSZ, HASH_LIST, M_NQLEASE,
1579 M_WAITOK, &nqfhhash);
1580 }
1581 #endif
1582
1583 exithook_establish(nfs_exit, NULL);
1584
1585 /*
1586 * Initialize reply list and start timer
1587 */
1588 TAILQ_INIT(&nfs_reqq);
1589 nfs_timer(NULL);
1590 MOWNER_ATTACH(&nfs_mowner);
1591
1592 #ifdef NFS
1593 /* Initialize the kqueue structures */
1594 nfs_kqinit();
1595 /* Initialize the iod structures */
1596 nfs_iodinit();
1597 #endif
1598 return 0;
1599 }
1600
1601 void
1602 nfs_init(void)
1603 {
1604 static ONCE_DECL(nfs_init_once);
1605
1606 RUN_ONCE(&nfs_init_once, nfs_init0);
1607 }
1608
1609 #ifdef NFS
1610 /*
1611 * Called once at VFS init to initialize client-specific data structures.
1612 */
1613 void
1614 nfs_vfs_init()
1615 {
1616 /* Initialize NFS server / client shared data. */
1617 nfs_init();
1618
1619 nfs_nhinit(); /* Init the nfsnode table */
1620 nfs_commitsize = uvmexp.npages << (PAGE_SHIFT - 4);
1621 }
1622
1623 void
1624 nfs_vfs_reinit()
1625 {
1626 nfs_nhreinit();
1627 }
1628
1629 void
1630 nfs_vfs_done()
1631 {
1632 nfs_nhdone();
1633 }
1634
1635 /*
1636 * Attribute cache routines.
1637 * nfs_loadattrcache() - loads or updates the cache contents from attributes
1638 * that are on the mbuf list
1639 * nfs_getattrcache() - returns valid attributes if found in cache, returns
1640 * error otherwise
1641 */
1642
1643 /*
1644 * Load the attribute cache (that lives in the nfsnode entry) with
1645 * the values on the mbuf list and
1646 * Iff vap not NULL
1647 * copy the attributes to *vaper
1648 */
1649 int
1650 nfsm_loadattrcache(vpp, mdp, dposp, vaper, flags)
1651 struct vnode **vpp;
1652 struct mbuf **mdp;
1653 caddr_t *dposp;
1654 struct vattr *vaper;
1655 int flags;
1656 {
1657 int32_t t1;
1658 caddr_t cp2;
1659 int error = 0;
1660 struct mbuf *md;
1661 int v3 = NFS_ISV3(*vpp);
1662
1663 md = *mdp;
1664 t1 = (mtod(md, caddr_t) + md->m_len) - *dposp;
1665 error = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, &cp2);
1666 if (error)
1667 return (error);
1668 return nfs_loadattrcache(vpp, (struct nfs_fattr *)cp2, vaper, flags);
1669 }
1670
1671 int
1672 nfs_loadattrcache(vpp, fp, vaper, flags)
1673 struct vnode **vpp;
1674 struct nfs_fattr *fp;
1675 struct vattr *vaper;
1676 int flags;
1677 {
1678 struct vnode *vp = *vpp;
1679 struct vattr *vap;
1680 int v3 = NFS_ISV3(vp);
1681 enum vtype vtyp;
1682 u_short vmode;
1683 struct timespec mtime;
1684 struct timespec ctime;
1685 struct vnode *nvp;
1686 int32_t rdev;
1687 struct nfsnode *np;
1688 extern int (**spec_nfsv2nodeop_p) __P((void *));
1689 uid_t uid;
1690 gid_t gid;
1691
1692 if (v3) {
1693 vtyp = nfsv3tov_type(fp->fa_type);
1694 vmode = fxdr_unsigned(u_short, fp->fa_mode);
1695 rdev = makedev(fxdr_unsigned(u_int32_t, fp->fa3_rdev.specdata1),
1696 fxdr_unsigned(u_int32_t, fp->fa3_rdev.specdata2));
1697 fxdr_nfsv3time(&fp->fa3_mtime, &mtime);
1698 fxdr_nfsv3time(&fp->fa3_ctime, &ctime);
1699 } else {
1700 vtyp = nfsv2tov_type(fp->fa_type);
1701 vmode = fxdr_unsigned(u_short, fp->fa_mode);
1702 if (vtyp == VNON || vtyp == VREG)
1703 vtyp = IFTOVT(vmode);
1704 rdev = fxdr_unsigned(int32_t, fp->fa2_rdev);
1705 fxdr_nfsv2time(&fp->fa2_mtime, &mtime);
1706 ctime.tv_sec = fxdr_unsigned(u_int32_t,
1707 fp->fa2_ctime.nfsv2_sec);
1708 ctime.tv_nsec = 0;
1709
1710 /*
1711 * Really ugly NFSv2 kludge.
1712 */
1713 if (vtyp == VCHR && rdev == 0xffffffff)
1714 vtyp = VFIFO;
1715 }
1716
1717 vmode &= ALLPERMS;
1718
1719 /*
1720 * If v_type == VNON it is a new node, so fill in the v_type,
1721 * n_mtime fields. Check to see if it represents a special
1722 * device, and if so, check for a possible alias. Once the
1723 * correct vnode has been obtained, fill in the rest of the
1724 * information.
1725 */
1726 np = VTONFS(vp);
1727 if (vp->v_type == VNON) {
1728 vp->v_type = vtyp;
1729 if (vp->v_type == VFIFO) {
1730 extern int (**fifo_nfsv2nodeop_p) __P((void *));
1731 vp->v_op = fifo_nfsv2nodeop_p;
1732 } else if (vp->v_type == VREG) {
1733 lockinit(&np->n_commitlock, PINOD, "nfsclock", 0, 0);
1734 } else if (vp->v_type == VCHR || vp->v_type == VBLK) {
1735 vp->v_op = spec_nfsv2nodeop_p;
1736 nvp = checkalias(vp, (dev_t)rdev, vp->v_mount);
1737 if (nvp) {
1738 /*
1739 * Discard unneeded vnode, but save its nfsnode.
1740 * Since the nfsnode does not have a lock, its
1741 * vnode lock has to be carried over.
1742 */
1743 /*
1744 * XXX is the old node sure to be locked here?
1745 */
1746 KASSERT(lockstatus(&vp->v_lock) ==
1747 LK_EXCLUSIVE);
1748 nvp->v_data = vp->v_data;
1749 vp->v_data = NULL;
1750 VOP_UNLOCK(vp, 0);
1751 vp->v_op = spec_vnodeop_p;
1752 vrele(vp);
1753 vgone(vp);
1754 lockmgr(&nvp->v_lock, LK_EXCLUSIVE,
1755 &nvp->v_interlock);
1756 /*
1757 * Reinitialize aliased node.
1758 */
1759 np->n_vnode = nvp;
1760 *vpp = vp = nvp;
1761 }
1762 }
1763 np->n_mtime = mtime;
1764 }
1765 uid = fxdr_unsigned(uid_t, fp->fa_uid);
1766 gid = fxdr_unsigned(gid_t, fp->fa_gid);
1767 vap = np->n_vattr;
1768
1769 /*
1770 * Invalidate access cache if uid, gid, mode or ctime changed.
1771 */
1772 if (np->n_accstamp != -1 &&
1773 (gid != vap->va_gid || uid != vap->va_uid || vmode != vap->va_mode
1774 || timespeccmp(&ctime, &vap->va_ctime, !=)))
1775 np->n_accstamp = -1;
1776
1777 vap->va_type = vtyp;
1778 vap->va_mode = vmode;
1779 vap->va_rdev = (dev_t)rdev;
1780 vap->va_mtime = mtime;
1781 vap->va_ctime = ctime;
1782 vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
1783 switch (vtyp) {
1784 case VDIR:
1785 vap->va_blocksize = NFS_DIRFRAGSIZ;
1786 break;
1787 case VBLK:
1788 vap->va_blocksize = BLKDEV_IOSIZE;
1789 break;
1790 case VCHR:
1791 vap->va_blocksize = MAXBSIZE;
1792 break;
1793 default:
1794 vap->va_blocksize = v3 ? vp->v_mount->mnt_stat.f_iosize :
1795 fxdr_unsigned(int32_t, fp->fa2_blocksize);
1796 break;
1797 }
1798 if (v3) {
1799 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1800 vap->va_uid = uid;
1801 vap->va_gid = gid;
1802 vap->va_size = fxdr_hyper(&fp->fa3_size);
1803 vap->va_bytes = fxdr_hyper(&fp->fa3_used);
1804 vap->va_fileid = fxdr_hyper(&fp->fa3_fileid);
1805 fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime);
1806 vap->va_flags = 0;
1807 vap->va_filerev = 0;
1808 } else {
1809 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1810 vap->va_uid = uid;
1811 vap->va_gid = gid;
1812 vap->va_size = fxdr_unsigned(u_int32_t, fp->fa2_size);
1813 vap->va_bytes = fxdr_unsigned(int32_t, fp->fa2_blocks)
1814 * NFS_FABLKSIZE;
1815 vap->va_fileid = fxdr_unsigned(int32_t, fp->fa2_fileid);
1816 fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime);
1817 vap->va_flags = 0;
1818 vap->va_gen = fxdr_unsigned(u_int32_t,fp->fa2_ctime.nfsv2_usec);
1819 vap->va_filerev = 0;
1820 }
1821 if (vap->va_size != np->n_size) {
1822 if ((np->n_flag & NMODIFIED) && vap->va_size < np->n_size) {
1823 vap->va_size = np->n_size;
1824 } else {
1825 np->n_size = vap->va_size;
1826 if (vap->va_type == VREG) {
1827 /*
1828 * we can't free pages if NAC_NOTRUNC because
1829 * the pages can be owned by ourselves.
1830 */
1831 if (flags & NAC_NOTRUNC) {
1832 np->n_flag |= NTRUNCDELAYED;
1833 } else {
1834 simple_lock(&vp->v_interlock);
1835 (void)VOP_PUTPAGES(vp, 0,
1836 0, PGO_SYNCIO | PGO_CLEANIT |
1837 PGO_FREE | PGO_ALLPAGES);
1838 uvm_vnp_setsize(vp, np->n_size);
1839 }
1840 }
1841 }
1842 }
1843 np->n_attrstamp = mono_time.tv_sec;
1844 if (vaper != NULL) {
1845 memcpy((caddr_t)vaper, (caddr_t)vap, sizeof(*vap));
1846 if (np->n_flag & NCHG) {
1847 if (np->n_flag & NACC)
1848 vaper->va_atime = np->n_atim;
1849 if (np->n_flag & NUPD)
1850 vaper->va_mtime = np->n_mtim;
1851 }
1852 }
1853 return (0);
1854 }
1855
1856 /*
1857 * Check the time stamp
1858 * If the cache is valid, copy contents to *vap and return 0
1859 * otherwise return an error
1860 */
1861 int
1862 nfs_getattrcache(vp, vaper)
1863 struct vnode *vp;
1864 struct vattr *vaper;
1865 {
1866 struct nfsnode *np = VTONFS(vp);
1867 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1868 struct vattr *vap;
1869
1870 if (np->n_attrstamp == 0 ||
1871 (mono_time.tv_sec - np->n_attrstamp) >= NFS_ATTRTIMEO(nmp, np)) {
1872 nfsstats.attrcache_misses++;
1873 return (ENOENT);
1874 }
1875 nfsstats.attrcache_hits++;
1876 vap = np->n_vattr;
1877 if (vap->va_size != np->n_size) {
1878 if (vap->va_type == VREG) {
1879 if (np->n_flag & NMODIFIED) {
1880 if (vap->va_size < np->n_size)
1881 vap->va_size = np->n_size;
1882 else
1883 np->n_size = vap->va_size;
1884 } else
1885 np->n_size = vap->va_size;
1886 uvm_vnp_setsize(vp, np->n_size);
1887 } else
1888 np->n_size = vap->va_size;
1889 }
1890 memcpy((caddr_t)vaper, (caddr_t)vap, sizeof(struct vattr));
1891 if (np->n_flag & NCHG) {
1892 if (np->n_flag & NACC)
1893 vaper->va_atime = np->n_atim;
1894 if (np->n_flag & NUPD)
1895 vaper->va_mtime = np->n_mtim;
1896 }
1897 return (0);
1898 }
1899
1900 void
1901 nfs_delayedtruncate(vp)
1902 struct vnode *vp;
1903 {
1904 struct nfsnode *np = VTONFS(vp);
1905
1906 if (np->n_flag & NTRUNCDELAYED) {
1907 np->n_flag &= ~NTRUNCDELAYED;
1908 simple_lock(&vp->v_interlock);
1909 (void)VOP_PUTPAGES(vp, 0,
1910 0, PGO_SYNCIO | PGO_CLEANIT | PGO_FREE | PGO_ALLPAGES);
1911 uvm_vnp_setsize(vp, np->n_size);
1912 }
1913 }
1914
1915 #define NFS_WCCKLUDGE_TIMEOUT (24 * 60 * 60) /* 1 day */
1916 #define NFS_WCCKLUDGE(nmp, now) \
1917 (((nmp)->nm_iflag & NFSMNT_WCCKLUDGE) && \
1918 ((now) - (nmp)->nm_wcckludgetime - NFS_WCCKLUDGE_TIMEOUT) < 0)
1919
1920 /*
1921 * nfs_check_wccdata: check inaccurate wcc_data
1922 *
1923 * => return non-zero if we shouldn't trust the wcc_data.
1924 * => NFS_WCCKLUDGE_TIMEOUT is for the case that the server is "fixed".
1925 */
1926
1927 int
1928 nfs_check_wccdata(struct nfsnode *np, const struct timespec *ctime,
1929 struct timespec *mtime, boolean_t docheck)
1930 {
1931 int error = 0;
1932
1933 #if !defined(NFS_V2_ONLY)
1934
1935 if (docheck) {
1936 struct vnode *vp = NFSTOV(np);
1937 struct nfsmount *nmp;
1938 long now = mono_time.tv_sec;
1939 #if defined(DEBUG)
1940 const char *reason = NULL; /* XXX: gcc */
1941 #endif
1942
1943 if (timespeccmp(&np->n_vattr->va_mtime, mtime, <=)) {
1944 #if defined(DEBUG)
1945 reason = "mtime";
1946 #endif
1947 error = EINVAL;
1948 }
1949
1950 if (vp->v_type == VDIR &&
1951 timespeccmp(&np->n_vattr->va_ctime, ctime, <=)) {
1952 #if defined(DEBUG)
1953 reason = "ctime";
1954 #endif
1955 error = EINVAL;
1956 }
1957
1958 nmp = VFSTONFS(vp->v_mount);
1959 if (error) {
1960
1961 /*
1962 * despite of the fact that we've updated the file,
1963 * timestamps of the file were not updated as we
1964 * expected.
1965 * it means that the server has incompatible
1966 * semantics of timestamps or (more likely)
1967 * the server time is not precise enough to
1968 * track each modifications.
1969 * in that case, we disable wcc processing.
1970 *
1971 * yes, strictly speaking, we should disable all
1972 * caching. it's a compromise.
1973 */
1974
1975 simple_lock(&nmp->nm_slock);
1976 #if defined(DEBUG)
1977 if (!NFS_WCCKLUDGE(nmp, now)) {
1978 printf("%s: inaccurate wcc data (%s) detected,"
1979 " disabling wcc\n",
1980 vp->v_mount->mnt_stat.f_mntfromname,
1981 reason);
1982 }
1983 #endif
1984 nmp->nm_iflag |= NFSMNT_WCCKLUDGE;
1985 nmp->nm_wcckludgetime = now;
1986 simple_unlock(&nmp->nm_slock);
1987 } else if (NFS_WCCKLUDGE(nmp, now)) {
1988 error = EPERM; /* XXX */
1989 } else if (nmp->nm_iflag & NFSMNT_WCCKLUDGE) {
1990 simple_lock(&nmp->nm_slock);
1991 if (nmp->nm_iflag & NFSMNT_WCCKLUDGE) {
1992 #if defined(DEBUG)
1993 printf("%s: re-enabling wcc\n",
1994 vp->v_mount->mnt_stat.f_mntfromname);
1995 #endif
1996 nmp->nm_iflag &= ~NFSMNT_WCCKLUDGE;
1997 }
1998 simple_unlock(&nmp->nm_slock);
1999 }
2000 }
2001
2002 #endif /* !defined(NFS_V2_ONLY) */
2003
2004 return error;
2005 }
2006
2007 /*
2008 * Heuristic to see if the server XDR encodes directory cookies or not.
2009 * it is not supposed to, but a lot of servers may do this. Also, since
2010 * most/all servers will implement V2 as well, it is expected that they
2011 * may return just 32 bits worth of cookie information, so we need to
2012 * find out in which 32 bits this information is available. We do this
2013 * to avoid trouble with emulated binaries that can't handle 64 bit
2014 * directory offsets.
2015 */
2016
2017 void
2018 nfs_cookieheuristic(vp, flagp, l, cred)
2019 struct vnode *vp;
2020 int *flagp;
2021 struct lwp *l;
2022 kauth_cred_t cred;
2023 {
2024 struct uio auio;
2025 struct iovec aiov;
2026 caddr_t tbuf, cp;
2027 struct dirent *dp;
2028 off_t *cookies = NULL, *cop;
2029 int error, eof, nc, len;
2030
2031 MALLOC(tbuf, caddr_t, NFS_DIRFRAGSIZ, M_TEMP, M_WAITOK);
2032
2033 aiov.iov_base = tbuf;
2034 aiov.iov_len = NFS_DIRFRAGSIZ;
2035 auio.uio_iov = &aiov;
2036 auio.uio_iovcnt = 1;
2037 auio.uio_rw = UIO_READ;
2038 auio.uio_resid = NFS_DIRFRAGSIZ;
2039 auio.uio_offset = 0;
2040 UIO_SETUP_SYSSPACE(&auio);
2041
2042 error = VOP_READDIR(vp, &auio, cred, &eof, &cookies, &nc);
2043
2044 len = NFS_DIRFRAGSIZ - auio.uio_resid;
2045 if (error || len == 0) {
2046 FREE(tbuf, M_TEMP);
2047 if (cookies)
2048 free(cookies, M_TEMP);
2049 return;
2050 }
2051
2052 /*
2053 * Find the first valid entry and look at its offset cookie.
2054 */
2055
2056 cp = tbuf;
2057 for (cop = cookies; len > 0; len -= dp->d_reclen) {
2058 dp = (struct dirent *)cp;
2059 if (dp->d_fileno != 0 && len >= dp->d_reclen) {
2060 if ((*cop >> 32) != 0 && (*cop & 0xffffffffLL) == 0) {
2061 *flagp |= NFSMNT_SWAPCOOKIE;
2062 nfs_invaldircache(vp, 0);
2063 nfs_vinvalbuf(vp, 0, cred, l, 1);
2064 }
2065 break;
2066 }
2067 cop++;
2068 cp += dp->d_reclen;
2069 }
2070
2071 FREE(tbuf, M_TEMP);
2072 free(cookies, M_TEMP);
2073 }
2074 #endif /* NFS */
2075
2076 #ifdef NFSSERVER
2077 /*
2078 * Set up nameidata for a lookup() call and do it.
2079 *
2080 * If pubflag is set, this call is done for a lookup operation on the
2081 * public filehandle. In that case we allow crossing mountpoints and
2082 * absolute pathnames. However, the caller is expected to check that
2083 * the lookup result is within the public fs, and deny access if
2084 * it is not.
2085 */
2086 int
2087 nfs_namei(ndp, fhp, len, slp, nam, mdp, dposp, retdirp, l, kerbflag, pubflag)
2088 struct nameidata *ndp;
2089 fhandle_t *fhp;
2090 uint32_t len;
2091 struct nfssvc_sock *slp;
2092 struct mbuf *nam;
2093 struct mbuf **mdp;
2094 caddr_t *dposp;
2095 struct vnode **retdirp;
2096 struct lwp *l;
2097 int kerbflag, pubflag;
2098 {
2099 int i, rem;
2100 struct mbuf *md;
2101 char *fromcp, *tocp, *cp;
2102 struct iovec aiov;
2103 struct uio auio;
2104 struct vnode *dp;
2105 int error, rdonly, linklen;
2106 struct componentname *cnp = &ndp->ni_cnd;
2107
2108 *retdirp = (struct vnode *)0;
2109
2110 if ((len + 1) > MAXPATHLEN)
2111 return (ENAMETOOLONG);
2112 if (len == 0)
2113 return (EACCES);
2114 cnp->cn_pnbuf = PNBUF_GET();
2115
2116 /*
2117 * Copy the name from the mbuf list to ndp->ni_pnbuf
2118 * and set the various ndp fields appropriately.
2119 */
2120 fromcp = *dposp;
2121 tocp = cnp->cn_pnbuf;
2122 md = *mdp;
2123 rem = mtod(md, caddr_t) + md->m_len - fromcp;
2124 for (i = 0; i < len; i++) {
2125 while (rem == 0) {
2126 md = md->m_next;
2127 if (md == NULL) {
2128 error = EBADRPC;
2129 goto out;
2130 }
2131 fromcp = mtod(md, caddr_t);
2132 rem = md->m_len;
2133 }
2134 if (*fromcp == '\0' || (!pubflag && *fromcp == '/')) {
2135 error = EACCES;
2136 goto out;
2137 }
2138 *tocp++ = *fromcp++;
2139 rem--;
2140 }
2141 *tocp = '\0';
2142 *mdp = md;
2143 *dposp = fromcp;
2144 len = nfsm_rndup(len)-len;
2145 if (len > 0) {
2146 if (rem >= len)
2147 *dposp += len;
2148 else if ((error = nfs_adv(mdp, dposp, len, rem)) != 0)
2149 goto out;
2150 }
2151
2152 /*
2153 * Extract and set starting directory.
2154 */
2155 error = nfsrv_fhtovp(fhp, FALSE, &dp, ndp->ni_cnd.cn_cred, slp,
2156 nam, &rdonly, kerbflag, pubflag);
2157 if (error)
2158 goto out;
2159 if (dp->v_type != VDIR) {
2160 vrele(dp);
2161 error = ENOTDIR;
2162 goto out;
2163 }
2164
2165 if (rdonly)
2166 cnp->cn_flags |= RDONLY;
2167
2168 *retdirp = dp;
2169
2170 if (pubflag) {
2171 /*
2172 * Oh joy. For WebNFS, handle those pesky '%' escapes,
2173 * and the 'native path' indicator.
2174 */
2175 cp = PNBUF_GET();
2176 fromcp = cnp->cn_pnbuf;
2177 tocp = cp;
2178 if ((unsigned char)*fromcp >= WEBNFS_SPECCHAR_START) {
2179 switch ((unsigned char)*fromcp) {
2180 case WEBNFS_NATIVE_CHAR:
2181 /*
2182 * 'Native' path for us is the same
2183 * as a path according to the NFS spec,
2184 * just skip the escape char.
2185 */
2186 fromcp++;
2187 break;
2188 /*
2189 * More may be added in the future, range 0x80-0xff
2190 */
2191 default:
2192 error = EIO;
2193 PNBUF_PUT(cp);
2194 goto out;
2195 }
2196 }
2197 /*
2198 * Translate the '%' escapes, URL-style.
2199 */
2200 while (*fromcp != '\0') {
2201 if (*fromcp == WEBNFS_ESC_CHAR) {
2202 if (fromcp[1] != '\0' && fromcp[2] != '\0') {
2203 fromcp++;
2204 *tocp++ = HEXSTRTOI(fromcp);
2205 fromcp += 2;
2206 continue;
2207 } else {
2208 error = ENOENT;
2209 PNBUF_PUT(cp);
2210 goto out;
2211 }
2212 } else
2213 *tocp++ = *fromcp++;
2214 }
2215 *tocp = '\0';
2216 PNBUF_PUT(cnp->cn_pnbuf);
2217 cnp->cn_pnbuf = cp;
2218 }
2219
2220 ndp->ni_pathlen = (tocp - cnp->cn_pnbuf) + 1;
2221 ndp->ni_segflg = UIO_SYSSPACE;
2222 ndp->ni_rootdir = rootvnode;
2223
2224 if (pubflag) {
2225 ndp->ni_loopcnt = 0;
2226 if (cnp->cn_pnbuf[0] == '/')
2227 dp = rootvnode;
2228 } else {
2229 cnp->cn_flags |= NOCROSSMOUNT;
2230 }
2231
2232 cnp->cn_lwp = l;
2233 VREF(dp);
2234
2235 for (;;) {
2236 cnp->cn_nameptr = cnp->cn_pnbuf;
2237 ndp->ni_startdir = dp;
2238 /*
2239 * And call lookup() to do the real work
2240 */
2241 error = lookup(ndp);
2242 if (error) {
2243 PNBUF_PUT(cnp->cn_pnbuf);
2244 return (error);
2245 }
2246 /*
2247 * Check for encountering a symbolic link
2248 */
2249 if ((cnp->cn_flags & ISSYMLINK) == 0) {
2250 if (cnp->cn_flags & (SAVENAME | SAVESTART))
2251 cnp->cn_flags |= HASBUF;
2252 else
2253 PNBUF_PUT(cnp->cn_pnbuf);
2254 return (0);
2255 } else {
2256 if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN))
2257 VOP_UNLOCK(ndp->ni_dvp, 0);
2258 if (!pubflag) {
2259 error = EINVAL;
2260 break;
2261 }
2262
2263 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
2264 error = ELOOP;
2265 break;
2266 }
2267 if (ndp->ni_vp->v_mount->mnt_flag & MNT_SYMPERM) {
2268 error = VOP_ACCESS(ndp->ni_vp, VEXEC, cnp->cn_cred,
2269 cnp->cn_lwp);
2270 if (error != 0)
2271 break;
2272 }
2273 if (ndp->ni_pathlen > 1)
2274 cp = PNBUF_GET();
2275 else
2276 cp = cnp->cn_pnbuf;
2277 aiov.iov_base = cp;
2278 aiov.iov_len = MAXPATHLEN;
2279 auio.uio_iov = &aiov;
2280 auio.uio_iovcnt = 1;
2281 auio.uio_offset = 0;
2282 auio.uio_rw = UIO_READ;
2283 auio.uio_resid = MAXPATHLEN;
2284 UIO_SETUP_SYSSPACE(&auio);
2285 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
2286 if (error) {
2287 badlink:
2288 if (ndp->ni_pathlen > 1)
2289 PNBUF_PUT(cp);
2290 break;
2291 }
2292 linklen = MAXPATHLEN - auio.uio_resid;
2293 if (linklen == 0) {
2294 error = ENOENT;
2295 goto badlink;
2296 }
2297 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
2298 error = ENAMETOOLONG;
2299 goto badlink;
2300 }
2301 if (ndp->ni_pathlen > 1) {
2302 memcpy(cp + linklen, ndp->ni_next, ndp->ni_pathlen);
2303 PNBUF_PUT(cnp->cn_pnbuf);
2304 cnp->cn_pnbuf = cp;
2305 } else
2306 cnp->cn_pnbuf[linklen] = '\0';
2307 ndp->ni_pathlen += linklen;
2308 vput(ndp->ni_vp);
2309 dp = ndp->ni_dvp;
2310 /*
2311 * Check if root directory should replace current directory.
2312 */
2313 if (cnp->cn_pnbuf[0] == '/') {
2314 vrele(dp);
2315 dp = ndp->ni_rootdir;
2316 VREF(dp);
2317 }
2318 }
2319 }
2320 vrele(ndp->ni_dvp);
2321 vput(ndp->ni_vp);
2322 ndp->ni_vp = NULL;
2323 out:
2324 PNBUF_PUT(cnp->cn_pnbuf);
2325 return (error);
2326 }
2327 #endif /* NFSSERVER */
2328
2329 /*
2330 * A fiddled version of m_adj() that ensures null fill to a 32-bit
2331 * boundary and only trims off the back end
2332 *
2333 * 1. trim off 'len' bytes as m_adj(mp, -len).
2334 * 2. add zero-padding 'nul' bytes at the end of the mbuf chain.
2335 */
2336 void
2337 nfs_zeropad(mp, len, nul)
2338 struct mbuf *mp;
2339 int len;
2340 int nul;
2341 {
2342 struct mbuf *m;
2343 int count;
2344
2345 /*
2346 * Trim from tail. Scan the mbuf chain,
2347 * calculating its length and finding the last mbuf.
2348 * If the adjustment only affects this mbuf, then just
2349 * adjust and return. Otherwise, rescan and truncate
2350 * after the remaining size.
2351 */
2352 count = 0;
2353 m = mp;
2354 for (;;) {
2355 count += m->m_len;
2356 if (m->m_next == NULL)
2357 break;
2358 m = m->m_next;
2359 }
2360
2361 KDASSERT(count >= len);
2362
2363 if (m->m_len >= len) {
2364 m->m_len -= len;
2365 } else {
2366 count -= len;
2367 /*
2368 * Correct length for chain is "count".
2369 * Find the mbuf with last data, adjust its length,
2370 * and toss data from remaining mbufs on chain.
2371 */
2372 for (m = mp; m; m = m->m_next) {
2373 if (m->m_len >= count) {
2374 m->m_len = count;
2375 break;
2376 }
2377 count -= m->m_len;
2378 }
2379 KASSERT(m && m->m_next);
2380 m_freem(m->m_next);
2381 m->m_next = NULL;
2382 }
2383
2384 KDASSERT(m->m_next == NULL);
2385
2386 /*
2387 * zero-padding.
2388 */
2389 if (nul > 0) {
2390 char *cp;
2391 int i;
2392
2393 if (M_ROMAP(m) || M_TRAILINGSPACE(m) < nul) {
2394 struct mbuf *n;
2395
2396 KDASSERT(MLEN >= nul);
2397 n = m_get(M_WAIT, MT_DATA);
2398 MCLAIM(n, &nfs_mowner);
2399 n->m_len = nul;
2400 n->m_next = NULL;
2401 m->m_next = n;
2402 cp = mtod(n, caddr_t);
2403 } else {
2404 cp = mtod(m, caddr_t) + m->m_len;
2405 m->m_len += nul;
2406 }
2407 for (i = 0; i < nul; i++)
2408 *cp++ = '\0';
2409 }
2410 return;
2411 }
2412
2413 /*
2414 * Make these functions instead of macros, so that the kernel text size
2415 * doesn't get too big...
2416 */
2417 void
2418 nfsm_srvwcc(nfsd, before_ret, before_vap, after_ret, after_vap, mbp, bposp)
2419 struct nfsrv_descript *nfsd;
2420 int before_ret;
2421 struct vattr *before_vap;
2422 int after_ret;
2423 struct vattr *after_vap;
2424 struct mbuf **mbp;
2425 char **bposp;
2426 {
2427 struct mbuf *mb = *mbp;
2428 char *bpos = *bposp;
2429 u_int32_t *tl;
2430
2431 if (before_ret) {
2432 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
2433 *tl = nfs_false;
2434 } else {
2435 nfsm_build(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
2436 *tl++ = nfs_true;
2437 txdr_hyper(before_vap->va_size, tl);
2438 tl += 2;
2439 txdr_nfsv3time(&(before_vap->va_mtime), tl);
2440 tl += 2;
2441 txdr_nfsv3time(&(before_vap->va_ctime), tl);
2442 }
2443 *bposp = bpos;
2444 *mbp = mb;
2445 nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp);
2446 }
2447
2448 void
2449 nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp)
2450 struct nfsrv_descript *nfsd;
2451 int after_ret;
2452 struct vattr *after_vap;
2453 struct mbuf **mbp;
2454 char **bposp;
2455 {
2456 struct mbuf *mb = *mbp;
2457 char *bpos = *bposp;
2458 u_int32_t *tl;
2459 struct nfs_fattr *fp;
2460
2461 if (after_ret) {
2462 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
2463 *tl = nfs_false;
2464 } else {
2465 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FATTR);
2466 *tl++ = nfs_true;
2467 fp = (struct nfs_fattr *)tl;
2468 nfsm_srvfattr(nfsd, after_vap, fp);
2469 }
2470 *mbp = mb;
2471 *bposp = bpos;
2472 }
2473
2474 void
2475 nfsm_srvfattr(nfsd, vap, fp)
2476 struct nfsrv_descript *nfsd;
2477 struct vattr *vap;
2478 struct nfs_fattr *fp;
2479 {
2480
2481 fp->fa_nlink = txdr_unsigned(vap->va_nlink);
2482 fp->fa_uid = txdr_unsigned(vap->va_uid);
2483 fp->fa_gid = txdr_unsigned(vap->va_gid);
2484 if (nfsd->nd_flag & ND_NFSV3) {
2485 fp->fa_type = vtonfsv3_type(vap->va_type);
2486 fp->fa_mode = vtonfsv3_mode(vap->va_mode);
2487 txdr_hyper(vap->va_size, &fp->fa3_size);
2488 txdr_hyper(vap->va_bytes, &fp->fa3_used);
2489 fp->fa3_rdev.specdata1 = txdr_unsigned(major(vap->va_rdev));
2490 fp->fa3_rdev.specdata2 = txdr_unsigned(minor(vap->va_rdev));
2491 fp->fa3_fsid.nfsuquad[0] = 0;
2492 fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(vap->va_fsid);
2493 txdr_hyper(vap->va_fileid, &fp->fa3_fileid);
2494 txdr_nfsv3time(&vap->va_atime, &fp->fa3_atime);
2495 txdr_nfsv3time(&vap->va_mtime, &fp->fa3_mtime);
2496 txdr_nfsv3time(&vap->va_ctime, &fp->fa3_ctime);
2497 } else {
2498 fp->fa_type = vtonfsv2_type(vap->va_type);
2499 fp->fa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
2500 fp->fa2_size = txdr_unsigned(vap->va_size);
2501 fp->fa2_blocksize = txdr_unsigned(vap->va_blocksize);
2502 if (vap->va_type == VFIFO)
2503 fp->fa2_rdev = 0xffffffff;
2504 else
2505 fp->fa2_rdev = txdr_unsigned(vap->va_rdev);
2506 fp->fa2_blocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE);
2507 fp->fa2_fsid = txdr_unsigned(vap->va_fsid);
2508 fp->fa2_fileid = txdr_unsigned(vap->va_fileid);
2509 txdr_nfsv2time(&vap->va_atime, &fp->fa2_atime);
2510 txdr_nfsv2time(&vap->va_mtime, &fp->fa2_mtime);
2511 txdr_nfsv2time(&vap->va_ctime, &fp->fa2_ctime);
2512 }
2513 }
2514
2515 #ifdef NFSSERVER
2516 /*
2517 * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked)
2518 * - look up fsid in mount list (if not found ret error)
2519 * - get vp and export rights by calling VFS_FHTOVP()
2520 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
2521 * - if not lockflag unlock it with VOP_UNLOCK()
2522 */
2523 int
2524 nfsrv_fhtovp(fhp, lockflag, vpp, cred, slp, nam, rdonlyp, kerbflag, pubflag)
2525 fhandle_t *fhp;
2526 int lockflag;
2527 struct vnode **vpp;
2528 kauth_cred_t cred;
2529 struct nfssvc_sock *slp;
2530 struct mbuf *nam;
2531 int *rdonlyp;
2532 int kerbflag;
2533 {
2534 struct mount *mp;
2535 kauth_cred_t credanon;
2536 int error, exflags;
2537 struct sockaddr_in *saddr;
2538
2539 *vpp = (struct vnode *)0;
2540
2541 if (nfs_ispublicfh(fhp)) {
2542 if (!pubflag || !nfs_pub.np_valid)
2543 return (ESTALE);
2544 fhp = &nfs_pub.np_handle;
2545 }
2546
2547 error = netexport_check(&fhp->fh_fsid, nam, &mp, &exflags, &credanon);
2548 if (error) {
2549 return error;
2550 }
2551
2552 error = VFS_FHTOVP(mp, &fhp->fh_fid, vpp);
2553 if (error)
2554 return (error);
2555
2556 if (!(exflags & (MNT_EXNORESPORT|MNT_EXPUBLIC))) {
2557 saddr = mtod(nam, struct sockaddr_in *);
2558 if ((saddr->sin_family == AF_INET) &&
2559 ntohs(saddr->sin_port) >= IPPORT_RESERVED) {
2560 vput(*vpp);
2561 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
2562 }
2563 #ifdef INET6
2564 if ((saddr->sin_family == AF_INET6) &&
2565 ntohs(saddr->sin_port) >= IPV6PORT_RESERVED) {
2566 vput(*vpp);
2567 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
2568 }
2569 #endif
2570 }
2571 /*
2572 * Check/setup credentials.
2573 */
2574 if (exflags & MNT_EXKERB) {
2575 if (!kerbflag) {
2576 vput(*vpp);
2577 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
2578 }
2579 } else if (kerbflag) {
2580 vput(*vpp);
2581 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
2582 } else if (kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER,
2583 NULL) == 0 || (exflags & MNT_EXPORTANON)) {
2584 kauth_cred_clone(credanon, cred);
2585 }
2586 if (exflags & MNT_EXRDONLY)
2587 *rdonlyp = 1;
2588 else
2589 *rdonlyp = 0;
2590 if (!lockflag)
2591 VOP_UNLOCK(*vpp, 0);
2592 return (0);
2593 }
2594
2595 /*
2596 * WebNFS: check if a filehandle is a public filehandle. For v3, this
2597 * means a length of 0, for v2 it means all zeroes. nfsm_srvmtofh has
2598 * transformed this to all zeroes in both cases, so check for it.
2599 */
2600 int
2601 nfs_ispublicfh(fhp)
2602 fhandle_t *fhp;
2603 {
2604 char *cp = (char *)fhp;
2605 int i;
2606
2607 for (i = 0; i < NFSX_V3FH; i++)
2608 if (*cp++ != 0)
2609 return (FALSE);
2610 return (TRUE);
2611 }
2612 #endif /* NFSSERVER */
2613
2614 /*
2615 * This function compares two net addresses by family and returns TRUE
2616 * if they are the same host.
2617 * If there is any doubt, return FALSE.
2618 * The AF_INET family is handled as a special case so that address mbufs
2619 * don't need to be saved to store "struct in_addr", which is only 4 bytes.
2620 */
2621 int
2622 netaddr_match(family, haddr, nam)
2623 int family;
2624 union nethostaddr *haddr;
2625 struct mbuf *nam;
2626 {
2627 struct sockaddr_in *inetaddr;
2628
2629 switch (family) {
2630 case AF_INET:
2631 inetaddr = mtod(nam, struct sockaddr_in *);
2632 if (inetaddr->sin_family == AF_INET &&
2633 inetaddr->sin_addr.s_addr == haddr->had_inetaddr)
2634 return (1);
2635 break;
2636 #ifdef INET6
2637 case AF_INET6:
2638 {
2639 struct sockaddr_in6 *sin6_1, *sin6_2;
2640
2641 sin6_1 = mtod(nam, struct sockaddr_in6 *);
2642 sin6_2 = mtod(haddr->had_nam, struct sockaddr_in6 *);
2643 if (sin6_1->sin6_family == AF_INET6 &&
2644 IN6_ARE_ADDR_EQUAL(&sin6_1->sin6_addr, &sin6_2->sin6_addr))
2645 return 1;
2646 }
2647 #endif
2648 #ifdef ISO
2649 case AF_ISO:
2650 {
2651 struct sockaddr_iso *isoaddr1, *isoaddr2;
2652
2653 isoaddr1 = mtod(nam, struct sockaddr_iso *);
2654 isoaddr2 = mtod(haddr->had_nam, struct sockaddr_iso *);
2655 if (isoaddr1->siso_family == AF_ISO &&
2656 isoaddr1->siso_nlen > 0 &&
2657 isoaddr1->siso_nlen == isoaddr2->siso_nlen &&
2658 SAME_ISOADDR(isoaddr1, isoaddr2))
2659 return (1);
2660 break;
2661 }
2662 #endif /* ISO */
2663 default:
2664 break;
2665 };
2666 return (0);
2667 }
2668
2669 /*
2670 * The write verifier has changed (probably due to a server reboot), so all
2671 * PG_NEEDCOMMIT pages will have to be written again. Since they are marked
2672 * as dirty or are being written out just now, all this takes is clearing
2673 * the PG_NEEDCOMMIT flag. Once done the new write verifier can be set for
2674 * the mount point.
2675 */
2676 void
2677 nfs_clearcommit(mp)
2678 struct mount *mp;
2679 {
2680 struct vnode *vp;
2681 struct nfsnode *np;
2682 struct vm_page *pg;
2683 struct nfsmount *nmp = VFSTONFS(mp);
2684
2685 lockmgr(&nmp->nm_writeverflock, LK_EXCLUSIVE, NULL);
2686
2687 LIST_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
2688 KASSERT(vp->v_mount == mp);
2689 if (vp->v_type != VREG)
2690 continue;
2691 np = VTONFS(vp);
2692 np->n_pushlo = np->n_pushhi = np->n_pushedlo =
2693 np->n_pushedhi = 0;
2694 np->n_commitflags &=
2695 ~(NFS_COMMIT_PUSH_VALID | NFS_COMMIT_PUSHED_VALID);
2696 simple_lock(&vp->v_uobj.vmobjlock);
2697 TAILQ_FOREACH(pg, &vp->v_uobj.memq, listq) {
2698 pg->flags &= ~PG_NEEDCOMMIT;
2699 }
2700 simple_unlock(&vp->v_uobj.vmobjlock);
2701 }
2702 simple_lock(&nmp->nm_slock);
2703 nmp->nm_iflag &= ~NFSMNT_STALEWRITEVERF;
2704 simple_unlock(&nmp->nm_slock);
2705 lockmgr(&nmp->nm_writeverflock, LK_RELEASE, NULL);
2706 }
2707
2708 void
2709 nfs_merge_commit_ranges(vp)
2710 struct vnode *vp;
2711 {
2712 struct nfsnode *np = VTONFS(vp);
2713
2714 KASSERT(np->n_commitflags & NFS_COMMIT_PUSH_VALID);
2715
2716 if (!(np->n_commitflags & NFS_COMMIT_PUSHED_VALID)) {
2717 np->n_pushedlo = np->n_pushlo;
2718 np->n_pushedhi = np->n_pushhi;
2719 np->n_commitflags |= NFS_COMMIT_PUSHED_VALID;
2720 } else {
2721 if (np->n_pushlo < np->n_pushedlo)
2722 np->n_pushedlo = np->n_pushlo;
2723 if (np->n_pushhi > np->n_pushedhi)
2724 np->n_pushedhi = np->n_pushhi;
2725 }
2726
2727 np->n_pushlo = np->n_pushhi = 0;
2728 np->n_commitflags &= ~NFS_COMMIT_PUSH_VALID;
2729
2730 #ifdef NFS_DEBUG_COMMIT
2731 printf("merge: committed: %u - %u\n", (unsigned)np->n_pushedlo,
2732 (unsigned)np->n_pushedhi);
2733 #endif
2734 }
2735
2736 int
2737 nfs_in_committed_range(vp, off, len)
2738 struct vnode *vp;
2739 off_t off, len;
2740 {
2741 struct nfsnode *np = VTONFS(vp);
2742 off_t lo, hi;
2743
2744 if (!(np->n_commitflags & NFS_COMMIT_PUSHED_VALID))
2745 return 0;
2746 lo = off;
2747 hi = lo + len;
2748
2749 return (lo >= np->n_pushedlo && hi <= np->n_pushedhi);
2750 }
2751
2752 int
2753 nfs_in_tobecommitted_range(vp, off, len)
2754 struct vnode *vp;
2755 off_t off, len;
2756 {
2757 struct nfsnode *np = VTONFS(vp);
2758 off_t lo, hi;
2759
2760 if (!(np->n_commitflags & NFS_COMMIT_PUSH_VALID))
2761 return 0;
2762 lo = off;
2763 hi = lo + len;
2764
2765 return (lo >= np->n_pushlo && hi <= np->n_pushhi);
2766 }
2767
2768 void
2769 nfs_add_committed_range(vp, off, len)
2770 struct vnode *vp;
2771 off_t off, len;
2772 {
2773 struct nfsnode *np = VTONFS(vp);
2774 off_t lo, hi;
2775
2776 lo = off;
2777 hi = lo + len;
2778
2779 if (!(np->n_commitflags & NFS_COMMIT_PUSHED_VALID)) {
2780 np->n_pushedlo = lo;
2781 np->n_pushedhi = hi;
2782 np->n_commitflags |= NFS_COMMIT_PUSHED_VALID;
2783 } else {
2784 if (hi > np->n_pushedhi)
2785 np->n_pushedhi = hi;
2786 if (lo < np->n_pushedlo)
2787 np->n_pushedlo = lo;
2788 }
2789 #ifdef NFS_DEBUG_COMMIT
2790 printf("add: committed: %u - %u\n", (unsigned)np->n_pushedlo,
2791 (unsigned)np->n_pushedhi);
2792 #endif
2793 }
2794
2795 void
2796 nfs_del_committed_range(vp, off, len)
2797 struct vnode *vp;
2798 off_t off, len;
2799 {
2800 struct nfsnode *np = VTONFS(vp);
2801 off_t lo, hi;
2802
2803 if (!(np->n_commitflags & NFS_COMMIT_PUSHED_VALID))
2804 return;
2805
2806 lo = off;
2807 hi = lo + len;
2808
2809 if (lo > np->n_pushedhi || hi < np->n_pushedlo)
2810 return;
2811 if (lo <= np->n_pushedlo)
2812 np->n_pushedlo = hi;
2813 else if (hi >= np->n_pushedhi)
2814 np->n_pushedhi = lo;
2815 else {
2816 /*
2817 * XXX There's only one range. If the deleted range
2818 * is in the middle, pick the largest of the
2819 * contiguous ranges that it leaves.
2820 */
2821 if ((np->n_pushedlo - lo) > (hi - np->n_pushedhi))
2822 np->n_pushedhi = lo;
2823 else
2824 np->n_pushedlo = hi;
2825 }
2826 #ifdef NFS_DEBUG_COMMIT
2827 printf("del: committed: %u - %u\n", (unsigned)np->n_pushedlo,
2828 (unsigned)np->n_pushedhi);
2829 #endif
2830 }
2831
2832 void
2833 nfs_add_tobecommitted_range(vp, off, len)
2834 struct vnode *vp;
2835 off_t off, len;
2836 {
2837 struct nfsnode *np = VTONFS(vp);
2838 off_t lo, hi;
2839
2840 lo = off;
2841 hi = lo + len;
2842
2843 if (!(np->n_commitflags & NFS_COMMIT_PUSH_VALID)) {
2844 np->n_pushlo = lo;
2845 np->n_pushhi = hi;
2846 np->n_commitflags |= NFS_COMMIT_PUSH_VALID;
2847 } else {
2848 if (lo < np->n_pushlo)
2849 np->n_pushlo = lo;
2850 if (hi > np->n_pushhi)
2851 np->n_pushhi = hi;
2852 }
2853 #ifdef NFS_DEBUG_COMMIT
2854 printf("add: tobecommitted: %u - %u\n", (unsigned)np->n_pushlo,
2855 (unsigned)np->n_pushhi);
2856 #endif
2857 }
2858
2859 void
2860 nfs_del_tobecommitted_range(vp, off, len)
2861 struct vnode *vp;
2862 off_t off, len;
2863 {
2864 struct nfsnode *np = VTONFS(vp);
2865 off_t lo, hi;
2866
2867 if (!(np->n_commitflags & NFS_COMMIT_PUSH_VALID))
2868 return;
2869
2870 lo = off;
2871 hi = lo + len;
2872
2873 if (lo > np->n_pushhi || hi < np->n_pushlo)
2874 return;
2875
2876 if (lo <= np->n_pushlo)
2877 np->n_pushlo = hi;
2878 else if (hi >= np->n_pushhi)
2879 np->n_pushhi = lo;
2880 else {
2881 /*
2882 * XXX There's only one range. If the deleted range
2883 * is in the middle, pick the largest of the
2884 * contiguous ranges that it leaves.
2885 */
2886 if ((np->n_pushlo - lo) > (hi - np->n_pushhi))
2887 np->n_pushhi = lo;
2888 else
2889 np->n_pushlo = hi;
2890 }
2891 #ifdef NFS_DEBUG_COMMIT
2892 printf("del: tobecommitted: %u - %u\n", (unsigned)np->n_pushlo,
2893 (unsigned)np->n_pushhi);
2894 #endif
2895 }
2896
2897 /*
2898 * Map errnos to NFS error numbers. For Version 3 also filter out error
2899 * numbers not specified for the associated procedure.
2900 */
2901 int
2902 nfsrv_errmap(nd, err)
2903 struct nfsrv_descript *nd;
2904 int err;
2905 {
2906 const short *defaulterrp, *errp;
2907
2908 if (nd->nd_flag & ND_NFSV3) {
2909 if (nd->nd_procnum <= NFSPROC_COMMIT) {
2910 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
2911 while (*++errp) {
2912 if (*errp == err)
2913 return (err);
2914 else if (*errp > err)
2915 break;
2916 }
2917 return ((int)*defaulterrp);
2918 } else
2919 return (err & 0xffff);
2920 }
2921 if (err <= ELAST)
2922 return ((int)nfsrv_v2errmap[err - 1]);
2923 return (NFSERR_IO);
2924 }
2925
2926 /*
2927 * Sort the group list in increasing numerical order.
2928 * (Insertion sort by Chris Torek, who was grossed out by the bubble sort
2929 * that used to be here.)
2930 */
2931 void
2932 nfsrvw_sort(list, num)
2933 gid_t *list;
2934 int num;
2935 {
2936 int i, j;
2937 gid_t v;
2938
2939 /* Insertion sort. */
2940 for (i = 1; i < num; i++) {
2941 v = list[i];
2942 /* find correct slot for value v, moving others up */
2943 for (j = i; --j >= 0 && v < list[j];)
2944 list[j + 1] = list[j];
2945 list[j + 1] = v;
2946 }
2947 }
2948
2949 /*
2950 * copy credentials making sure that the result can be compared with memcmp().
2951 */
2952 void
2953 nfsrv_setcred(incred, outcred)
2954 kauth_cred_t incred, *outcred;
2955 {
2956 /*
2957 * XXX elad: this is another case where the original code just
2958 * messed with the struct members, more specifically,
2959 * set the reference count. if we have more than one
2960 * reference, it means we have a user of these
2961 * credentials that didn't kauth_cred_hold().
2962 */
2963 #ifdef DIAGNOSTIC
2964 if (kauth_cred_getrefcnt(*outcred) > 1)
2965 panic("nfsrv_setcred: possible memory leak");
2966 #endif /* DIAGNOSTIC */
2967
2968 *outcred = kauth_cred_copy(*outcred);
2969 kauth_cred_clone(incred, *outcred);
2970 }
2971
2972 u_int32_t
2973 nfs_getxid()
2974 {
2975 static u_int32_t base;
2976 static u_int32_t nfs_xid = 0;
2977 static struct simplelock nfs_xidlock = SIMPLELOCK_INITIALIZER;
2978 u_int32_t newxid;
2979
2980 simple_lock(&nfs_xidlock);
2981 /*
2982 * derive initial xid from system time
2983 * XXX time is invalid if root not yet mounted
2984 */
2985 if (__predict_false(!base && (rootvp))) {
2986 struct timeval tv;
2987
2988 microtime(&tv);
2989 base = tv.tv_sec << 12;
2990 nfs_xid = base;
2991 }
2992
2993 /*
2994 * Skip zero xid if it should ever happen.
2995 */
2996 if (__predict_false(++nfs_xid == 0))
2997 nfs_xid++;
2998 newxid = nfs_xid;
2999 simple_unlock(&nfs_xidlock);
3000
3001 return txdr_unsigned(newxid);
3002 }
3003
3004 /*
3005 * assign a new xid for existing request.
3006 * used for NFSERR_JUKEBOX handling.
3007 */
3008 void
3009 nfs_renewxid(struct nfsreq *req)
3010 {
3011 u_int32_t xid;
3012 int off;
3013
3014 xid = nfs_getxid();
3015 if (req->r_nmp->nm_sotype == SOCK_STREAM)
3016 off = sizeof(u_int32_t); /* RPC record mark */
3017 else
3018 off = 0;
3019
3020 m_copyback(req->r_mreq, off, sizeof(xid), (void *)&xid);
3021 req->r_xid = xid;
3022 }
3023