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