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