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