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