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