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