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