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