Use ether_crc32_be.
[dragonfly.git] / sys / vfs / nfs / nfs_subs.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)nfs_subs.c 8.8 (Berkeley) 5/22/95
79e5012e 37 * $FreeBSD: /repoman/r/ncvs/src/sys/nfsclient/nfs_subs.c,v 1.128 2004/04/14 23:23:55 peadar Exp $
be6c08cb 38 * $DragonFly: src/sys/vfs/nfs/nfs_subs.c,v 1.29 2005/04/20 17:01:54 dillon Exp $
984263bc
MD
39 */
40
41/*
42 * These functions support the macros and help fiddle mbuf chains for
43 * the nfs op functions. They do things like create the rpc header and
44 * copy data between mbuf chains and uio lists.
45 */
46#include <sys/param.h>
47#include <sys/systm.h>
48#include <sys/kernel.h>
49#include <sys/buf.h>
50#include <sys/proc.h>
51#include <sys/mount.h>
52#include <sys/vnode.h>
fad57d0e 53#include <sys/nlookup.h>
984263bc
MD
54#include <sys/namei.h>
55#include <sys/mbuf.h>
56#include <sys/socket.h>
57#include <sys/stat.h>
58#include <sys/malloc.h>
59#include <sys/sysent.h>
60#include <sys/syscall.h>
61#include <sys/conf.h>
62
63#include <vm/vm.h>
64#include <vm/vm_object.h>
65#include <vm/vm_extern.h>
66#include <vm/vm_zone.h>
67
3020e3be
MD
68#include <sys/buf2.h>
69
1f2de5d4
MD
70#include "rpcv2.h"
71#include "nfsproto.h"
72#include "nfs.h"
c1cf1e59 73#include "nfsmount.h"
1f2de5d4
MD
74#include "nfsnode.h"
75#include "xdr_subs.h"
76#include "nfsm_subs.h"
1f2de5d4
MD
77#include "nqnfs.h"
78#include "nfsrtt.h"
984263bc
MD
79
80#include <netinet/in.h>
81
82/*
83 * Data items converted to xdr at startup, since they are constant
84 * This is kinda hokey, but may save a little time doing byte swaps
85 */
86u_int32_t nfs_xdrneg1;
87u_int32_t rpc_call, rpc_vers, rpc_reply, rpc_msgdenied, rpc_autherr,
88 rpc_mismatch, rpc_auth_unix, rpc_msgaccepted,
89 rpc_auth_kerb;
90u_int32_t nfs_prog, nqnfs_prog, nfs_true, nfs_false;
91
92/* And other global data */
93static u_int32_t nfs_xid = 0;
94static enum vtype nv2tov_type[8]= {
95 VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON, VNON
96};
97enum vtype nv3tov_type[8]= {
98 VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO
99};
100
101int nfs_ticks;
102int nfs_pbuf_freecnt = -1; /* start out unlimited */
103
104struct nfs_reqq nfs_reqq;
105struct nfssvc_sockhead nfssvc_sockhead;
106int nfssvc_sockhead_flag;
107struct nfsd_head nfsd_head;
108int nfsd_head_flag;
109struct nfs_bufq nfs_bufq;
110struct nqtimerhead nqtimerhead;
111struct nqfhhashhead *nqfhhashtbl;
112u_long nqfhhash;
113
a6ee311a 114static void (*nfs_prev_lease_updatetime) (int);
984263bc
MD
115static int nfs_prev_nfssvc_sy_narg;
116static sy_call_t *nfs_prev_nfssvc_sy_call;
117
118#ifndef NFS_NOSERVER
119
2d3e977e 120static int (*nfs_prev_vop_lease_check)(struct vop_lease_args *);
984263bc
MD
121
122/*
123 * Mapping of old NFS Version 2 RPC numbers to generic numbers.
124 */
125int nfsv3_procid[NFS_NPROCS] = {
126 NFSPROC_NULL,
127 NFSPROC_GETATTR,
128 NFSPROC_SETATTR,
129 NFSPROC_NOOP,
130 NFSPROC_LOOKUP,
131 NFSPROC_READLINK,
132 NFSPROC_READ,
133 NFSPROC_NOOP,
134 NFSPROC_WRITE,
135 NFSPROC_CREATE,
136 NFSPROC_REMOVE,
137 NFSPROC_RENAME,
138 NFSPROC_LINK,
139 NFSPROC_SYMLINK,
140 NFSPROC_MKDIR,
141 NFSPROC_RMDIR,
142 NFSPROC_READDIR,
143 NFSPROC_FSSTAT,
144 NFSPROC_NOOP,
145 NFSPROC_NOOP,
146 NFSPROC_NOOP,
147 NFSPROC_NOOP,
148 NFSPROC_NOOP,
149 NFSPROC_NOOP,
150 NFSPROC_NOOP,
151 NFSPROC_NOOP
152};
153
154#endif /* NFS_NOSERVER */
155/*
156 * and the reverse mapping from generic to Version 2 procedure numbers
157 */
158int nfsv2_procid[NFS_NPROCS] = {
159 NFSV2PROC_NULL,
160 NFSV2PROC_GETATTR,
161 NFSV2PROC_SETATTR,
162 NFSV2PROC_LOOKUP,
163 NFSV2PROC_NOOP,
164 NFSV2PROC_READLINK,
165 NFSV2PROC_READ,
166 NFSV2PROC_WRITE,
167 NFSV2PROC_CREATE,
168 NFSV2PROC_MKDIR,
169 NFSV2PROC_SYMLINK,
170 NFSV2PROC_CREATE,
171 NFSV2PROC_REMOVE,
172 NFSV2PROC_RMDIR,
173 NFSV2PROC_RENAME,
174 NFSV2PROC_LINK,
175 NFSV2PROC_READDIR,
176 NFSV2PROC_NOOP,
177 NFSV2PROC_STATFS,
178 NFSV2PROC_NOOP,
179 NFSV2PROC_NOOP,
180 NFSV2PROC_NOOP,
181 NFSV2PROC_NOOP,
182 NFSV2PROC_NOOP,
183 NFSV2PROC_NOOP,
184 NFSV2PROC_NOOP,
185};
186
187#ifndef NFS_NOSERVER
188/*
189 * Maps errno values to nfs error numbers.
190 * Use NFSERR_IO as the catch all for ones not specifically defined in
191 * RFC 1094.
192 */
193static u_char nfsrv_v2errmap[ELAST] = {
194 NFSERR_PERM, NFSERR_NOENT, NFSERR_IO, NFSERR_IO, NFSERR_IO,
195 NFSERR_NXIO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
196 NFSERR_IO, NFSERR_IO, NFSERR_ACCES, NFSERR_IO, NFSERR_IO,
197 NFSERR_IO, NFSERR_EXIST, NFSERR_IO, NFSERR_NODEV, NFSERR_NOTDIR,
198 NFSERR_ISDIR, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
199 NFSERR_IO, NFSERR_FBIG, NFSERR_NOSPC, NFSERR_IO, NFSERR_ROFS,
200 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
201 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
202 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
203 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
204 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
205 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
206 NFSERR_IO, NFSERR_IO, NFSERR_NAMETOL, NFSERR_IO, NFSERR_IO,
207 NFSERR_NOTEMPTY, NFSERR_IO, NFSERR_IO, NFSERR_DQUOT, NFSERR_STALE,
208 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
209 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
210 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
211 NFSERR_IO /* << Last is 86 */
212};
213
214/*
215 * Maps errno values to nfs error numbers.
216 * Although it is not obvious whether or not NFS clients really care if
217 * a returned error value is in the specified list for the procedure, the
218 * safest thing to do is filter them appropriately. For Version 2, the
219 * X/Open XNFS document is the only specification that defines error values
220 * for each RPC (The RFC simply lists all possible error values for all RPCs),
221 * so I have decided to not do this for Version 2.
222 * The first entry is the default error return and the rest are the valid
223 * errors for that RPC in increasing numeric order.
224 */
225static short nfsv3err_null[] = {
226 0,
227 0,
228};
229
230static short nfsv3err_getattr[] = {
231 NFSERR_IO,
232 NFSERR_IO,
233 NFSERR_STALE,
234 NFSERR_BADHANDLE,
235 NFSERR_SERVERFAULT,
236 0,
237};
238
239static short nfsv3err_setattr[] = {
240 NFSERR_IO,
241 NFSERR_PERM,
242 NFSERR_IO,
243 NFSERR_ACCES,
244 NFSERR_INVAL,
245 NFSERR_NOSPC,
246 NFSERR_ROFS,
247 NFSERR_DQUOT,
248 NFSERR_STALE,
249 NFSERR_BADHANDLE,
250 NFSERR_NOT_SYNC,
251 NFSERR_SERVERFAULT,
252 0,
253};
254
255static short nfsv3err_lookup[] = {
256 NFSERR_IO,
257 NFSERR_NOENT,
258 NFSERR_IO,
259 NFSERR_ACCES,
260 NFSERR_NOTDIR,
261 NFSERR_NAMETOL,
262 NFSERR_STALE,
263 NFSERR_BADHANDLE,
264 NFSERR_SERVERFAULT,
265 0,
266};
267
268static short nfsv3err_access[] = {
269 NFSERR_IO,
270 NFSERR_IO,
271 NFSERR_STALE,
272 NFSERR_BADHANDLE,
273 NFSERR_SERVERFAULT,
274 0,
275};
276
277static short nfsv3err_readlink[] = {
278 NFSERR_IO,
279 NFSERR_IO,
280 NFSERR_ACCES,
281 NFSERR_INVAL,
282 NFSERR_STALE,
283 NFSERR_BADHANDLE,
284 NFSERR_NOTSUPP,
285 NFSERR_SERVERFAULT,
286 0,
287};
288
289static short nfsv3err_read[] = {
290 NFSERR_IO,
291 NFSERR_IO,
292 NFSERR_NXIO,
293 NFSERR_ACCES,
294 NFSERR_INVAL,
295 NFSERR_STALE,
296 NFSERR_BADHANDLE,
297 NFSERR_SERVERFAULT,
298 0,
299};
300
301static short nfsv3err_write[] = {
302 NFSERR_IO,
303 NFSERR_IO,
304 NFSERR_ACCES,
305 NFSERR_INVAL,
306 NFSERR_FBIG,
307 NFSERR_NOSPC,
308 NFSERR_ROFS,
309 NFSERR_DQUOT,
310 NFSERR_STALE,
311 NFSERR_BADHANDLE,
312 NFSERR_SERVERFAULT,
313 0,
314};
315
316static short nfsv3err_create[] = {
317 NFSERR_IO,
318 NFSERR_IO,
319 NFSERR_ACCES,
320 NFSERR_EXIST,
321 NFSERR_NOTDIR,
322 NFSERR_NOSPC,
323 NFSERR_ROFS,
324 NFSERR_NAMETOL,
325 NFSERR_DQUOT,
326 NFSERR_STALE,
327 NFSERR_BADHANDLE,
328 NFSERR_NOTSUPP,
329 NFSERR_SERVERFAULT,
330 0,
331};
332
333static short nfsv3err_mkdir[] = {
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
350static short nfsv3err_symlink[] = {
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
367static short nfsv3err_mknod[] = {
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 NFSERR_BADTYPE,
382 0,
383};
384
385static short nfsv3err_remove[] = {
386 NFSERR_IO,
387 NFSERR_NOENT,
388 NFSERR_IO,
389 NFSERR_ACCES,
390 NFSERR_NOTDIR,
391 NFSERR_ROFS,
392 NFSERR_NAMETOL,
393 NFSERR_STALE,
394 NFSERR_BADHANDLE,
395 NFSERR_SERVERFAULT,
396 0,
397};
398
399static short nfsv3err_rmdir[] = {
400 NFSERR_IO,
401 NFSERR_NOENT,
402 NFSERR_IO,
403 NFSERR_ACCES,
404 NFSERR_EXIST,
405 NFSERR_NOTDIR,
406 NFSERR_INVAL,
407 NFSERR_ROFS,
408 NFSERR_NAMETOL,
409 NFSERR_NOTEMPTY,
410 NFSERR_STALE,
411 NFSERR_BADHANDLE,
412 NFSERR_NOTSUPP,
413 NFSERR_SERVERFAULT,
414 0,
415};
416
417static short nfsv3err_rename[] = {
418 NFSERR_IO,
419 NFSERR_NOENT,
420 NFSERR_IO,
421 NFSERR_ACCES,
422 NFSERR_EXIST,
423 NFSERR_XDEV,
424 NFSERR_NOTDIR,
425 NFSERR_ISDIR,
426 NFSERR_INVAL,
427 NFSERR_NOSPC,
428 NFSERR_ROFS,
429 NFSERR_MLINK,
430 NFSERR_NAMETOL,
431 NFSERR_NOTEMPTY,
432 NFSERR_DQUOT,
433 NFSERR_STALE,
434 NFSERR_BADHANDLE,
435 NFSERR_NOTSUPP,
436 NFSERR_SERVERFAULT,
437 0,
438};
439
440static short nfsv3err_link[] = {
441 NFSERR_IO,
442 NFSERR_IO,
443 NFSERR_ACCES,
444 NFSERR_EXIST,
445 NFSERR_XDEV,
446 NFSERR_NOTDIR,
447 NFSERR_INVAL,
448 NFSERR_NOSPC,
449 NFSERR_ROFS,
450 NFSERR_MLINK,
451 NFSERR_NAMETOL,
452 NFSERR_DQUOT,
453 NFSERR_STALE,
454 NFSERR_BADHANDLE,
455 NFSERR_NOTSUPP,
456 NFSERR_SERVERFAULT,
457 0,
458};
459
460static short nfsv3err_readdir[] = {
461 NFSERR_IO,
462 NFSERR_IO,
463 NFSERR_ACCES,
464 NFSERR_NOTDIR,
465 NFSERR_STALE,
466 NFSERR_BADHANDLE,
467 NFSERR_BAD_COOKIE,
468 NFSERR_TOOSMALL,
469 NFSERR_SERVERFAULT,
470 0,
471};
472
473static short nfsv3err_readdirplus[] = {
474 NFSERR_IO,
475 NFSERR_IO,
476 NFSERR_ACCES,
477 NFSERR_NOTDIR,
478 NFSERR_STALE,
479 NFSERR_BADHANDLE,
480 NFSERR_BAD_COOKIE,
481 NFSERR_NOTSUPP,
482 NFSERR_TOOSMALL,
483 NFSERR_SERVERFAULT,
484 0,
485};
486
487static short nfsv3err_fsstat[] = {
488 NFSERR_IO,
489 NFSERR_IO,
490 NFSERR_STALE,
491 NFSERR_BADHANDLE,
492 NFSERR_SERVERFAULT,
493 0,
494};
495
496static short nfsv3err_fsinfo[] = {
497 NFSERR_STALE,
498 NFSERR_STALE,
499 NFSERR_BADHANDLE,
500 NFSERR_SERVERFAULT,
501 0,
502};
503
504static short nfsv3err_pathconf[] = {
505 NFSERR_STALE,
506 NFSERR_STALE,
507 NFSERR_BADHANDLE,
508 NFSERR_SERVERFAULT,
509 0,
510};
511
512static short nfsv3err_commit[] = {
513 NFSERR_IO,
514 NFSERR_IO,
515 NFSERR_STALE,
516 NFSERR_BADHANDLE,
517 NFSERR_SERVERFAULT,
518 0,
519};
520
521static short *nfsrv_v3errmap[] = {
522 nfsv3err_null,
523 nfsv3err_getattr,
524 nfsv3err_setattr,
525 nfsv3err_lookup,
526 nfsv3err_access,
527 nfsv3err_readlink,
528 nfsv3err_read,
529 nfsv3err_write,
530 nfsv3err_create,
531 nfsv3err_mkdir,
532 nfsv3err_symlink,
533 nfsv3err_mknod,
534 nfsv3err_remove,
535 nfsv3err_rmdir,
536 nfsv3err_rename,
537 nfsv3err_link,
538 nfsv3err_readdir,
539 nfsv3err_readdirplus,
540 nfsv3err_fsstat,
541 nfsv3err_fsinfo,
542 nfsv3err_pathconf,
543 nfsv3err_commit,
544};
545
546#endif /* NFS_NOSERVER */
547
548extern struct nfsrtt nfsrtt;
549extern time_t nqnfsstarttime;
550extern int nqsrv_clockskew;
551extern int nqsrv_writeslack;
552extern int nqsrv_maxlease;
553extern struct nfsstats nfsstats;
554extern int nqnfs_piggy[NFS_NPROCS];
555extern nfstype nfsv2_type[9];
556extern nfstype nfsv3_type[9];
557extern struct nfsnodehashhead *nfsnodehashtbl;
558extern u_long nfsnodehash;
559
560struct nfssvc_args;
561extern int nfssvc(struct proc *, struct nfssvc_args *, int *);
562
563LIST_HEAD(nfsnodehashhead, nfsnode);
564
984263bc 565u_quad_t
e851b29e 566nfs_curusec(void)
984263bc
MD
567{
568 struct timeval tv;
569
570 getmicrotime(&tv);
571 return ((u_quad_t)tv.tv_sec * 1000000 + (u_quad_t)tv.tv_usec);
572}
573
574/*
575 * Create the header for an rpc request packet
576 * The hsiz is the size of the rest of the nfs request header.
577 * (just used to decide if a cluster is a good idea)
578 */
579struct mbuf *
e851b29e 580nfsm_reqh(struct vnode *vp, u_long procid, int hsiz, caddr_t *bposp)
984263bc 581{
40393ded
RG
582 struct mbuf *mb;
583 u_int32_t *tl;
584 caddr_t bpos;
984263bc
MD
585 struct mbuf *mb2;
586 struct nfsmount *nmp;
587 int nqflag;
588
74f1caca 589 MGET(mb, MB_WAIT, MT_DATA);
984263bc 590 if (hsiz >= MINCLSIZE)
74f1caca 591 MCLGET(mb, MB_WAIT);
984263bc
MD
592 mb->m_len = 0;
593 bpos = mtod(mb, caddr_t);
594
595 /*
596 * For NQNFS, add lease request.
597 */
598 if (vp) {
599 nmp = VFSTONFS(vp->v_mount);
600 if (nmp->nm_flag & NFSMNT_NQNFS) {
601 nqflag = NQNFS_NEEDLEASE(vp, procid);
602 if (nqflag) {
603 nfsm_build(tl, u_int32_t *, 2*NFSX_UNSIGNED);
604 *tl++ = txdr_unsigned(nqflag);
605 *tl = txdr_unsigned(nmp->nm_leaseterm);
606 } else {
607 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
608 *tl = 0;
609 }
610 }
611 }
612 /* Finally, return values */
613 *bposp = bpos;
614 return (mb);
615}
616
617/*
618 * Build the RPC header and fill in the authorization info.
619 * The authorization string argument is only used when the credentials
620 * come from outside of the kernel.
621 * Returns the head of the mbuf list.
622 */
623struct mbuf *
e851b29e
CP
624nfsm_rpchead(struct ucred *cr, int nmflag, int procid, int auth_type,
625 int auth_len, char *auth_str, int verf_len, char *verf_str,
626 struct mbuf *mrest, int mrest_len, struct mbuf **mbp,
627 u_int32_t *xidp)
984263bc 628{
40393ded
RG
629 struct mbuf *mb;
630 u_int32_t *tl;
631 caddr_t bpos;
632 int i;
984263bc
MD
633 struct mbuf *mreq, *mb2;
634 int siz, grpsiz, authsiz;
635
636 authsiz = nfsm_rndup(auth_len);
74f1caca 637 MGETHDR(mb, MB_WAIT, MT_DATA);
984263bc 638 if ((authsiz + 10 * NFSX_UNSIGNED) >= MINCLSIZE) {
74f1caca 639 MCLGET(mb, MB_WAIT);
984263bc
MD
640 } else if ((authsiz + 10 * NFSX_UNSIGNED) < MHLEN) {
641 MH_ALIGN(mb, authsiz + 10 * NFSX_UNSIGNED);
642 } else {
643 MH_ALIGN(mb, 8 * NFSX_UNSIGNED);
644 }
645 mb->m_len = 0;
646 mreq = mb;
647 bpos = mtod(mb, caddr_t);
648
649 /*
650 * First the RPC header.
651 */
652 nfsm_build(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
653
654 /* Get a pretty random xid to start with */
655 if (!nfs_xid)
656 nfs_xid = random();
657 /*
658 * Skip zero xid if it should ever happen.
659 */
660 if (++nfs_xid == 0)
661 nfs_xid++;
662
663 *tl++ = *xidp = txdr_unsigned(nfs_xid);
664 *tl++ = rpc_call;
665 *tl++ = rpc_vers;
666 if (nmflag & NFSMNT_NQNFS) {
667 *tl++ = txdr_unsigned(NQNFS_PROG);
668 *tl++ = txdr_unsigned(NQNFS_VER3);
669 } else {
670 *tl++ = txdr_unsigned(NFS_PROG);
671 if (nmflag & NFSMNT_NFSV3)
672 *tl++ = txdr_unsigned(NFS_VER3);
673 else
674 *tl++ = txdr_unsigned(NFS_VER2);
675 }
676 if (nmflag & NFSMNT_NFSV3)
677 *tl++ = txdr_unsigned(procid);
678 else
679 *tl++ = txdr_unsigned(nfsv2_procid[procid]);
680
681 /*
682 * And then the authorization cred.
683 */
684 *tl++ = txdr_unsigned(auth_type);
685 *tl = txdr_unsigned(authsiz);
686 switch (auth_type) {
687 case RPCAUTH_UNIX:
688 nfsm_build(tl, u_int32_t *, auth_len);
689 *tl++ = 0; /* stamp ?? */
690 *tl++ = 0; /* NULL hostname */
691 *tl++ = txdr_unsigned(cr->cr_uid);
692 *tl++ = txdr_unsigned(cr->cr_groups[0]);
693 grpsiz = (auth_len >> 2) - 5;
694 *tl++ = txdr_unsigned(grpsiz);
695 for (i = 1; i <= grpsiz; i++)
696 *tl++ = txdr_unsigned(cr->cr_groups[i]);
697 break;
698 case RPCAUTH_KERB4:
699 siz = auth_len;
700 while (siz > 0) {
701 if (M_TRAILINGSPACE(mb) == 0) {
74f1caca 702 MGET(mb2, MB_WAIT, MT_DATA);
984263bc 703 if (siz >= MINCLSIZE)
74f1caca 704 MCLGET(mb2, MB_WAIT);
984263bc
MD
705 mb->m_next = mb2;
706 mb = mb2;
707 mb->m_len = 0;
708 bpos = mtod(mb, caddr_t);
709 }
710 i = min(siz, M_TRAILINGSPACE(mb));
711 bcopy(auth_str, bpos, i);
712 mb->m_len += i;
713 auth_str += i;
714 bpos += i;
715 siz -= i;
716 }
717 if ((siz = (nfsm_rndup(auth_len) - auth_len)) > 0) {
718 for (i = 0; i < siz; i++)
719 *bpos++ = '\0';
720 mb->m_len += siz;
721 }
722 break;
723 };
724
725 /*
726 * And the verifier...
727 */
728 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
729 if (verf_str) {
730 *tl++ = txdr_unsigned(RPCAUTH_KERB4);
731 *tl = txdr_unsigned(verf_len);
732 siz = verf_len;
733 while (siz > 0) {
734 if (M_TRAILINGSPACE(mb) == 0) {
74f1caca 735 MGET(mb2, MB_WAIT, MT_DATA);
984263bc 736 if (siz >= MINCLSIZE)
74f1caca 737 MCLGET(mb2, MB_WAIT);
984263bc
MD
738 mb->m_next = mb2;
739 mb = mb2;
740 mb->m_len = 0;
741 bpos = mtod(mb, caddr_t);
742 }
743 i = min(siz, M_TRAILINGSPACE(mb));
744 bcopy(verf_str, bpos, i);
745 mb->m_len += i;
746 verf_str += i;
747 bpos += i;
748 siz -= i;
749 }
750 if ((siz = (nfsm_rndup(verf_len) - verf_len)) > 0) {
751 for (i = 0; i < siz; i++)
752 *bpos++ = '\0';
753 mb->m_len += siz;
754 }
755 } else {
756 *tl++ = txdr_unsigned(RPCAUTH_NULL);
757 *tl = 0;
758 }
759 mb->m_next = mrest;
760 mreq->m_pkthdr.len = authsiz + 10 * NFSX_UNSIGNED + mrest_len;
761 mreq->m_pkthdr.rcvif = (struct ifnet *)0;
762 *mbp = mb;
763 return (mreq);
764}
765
766/*
767 * copies mbuf chain to the uio scatter/gather list
768 */
769int
e851b29e 770nfsm_mbuftouio(struct mbuf **mrep, struct uio *uiop, int siz, caddr_t *dpos)
984263bc 771{
40393ded
RG
772 char *mbufcp, *uiocp;
773 int xfer, left, len;
774 struct mbuf *mp;
984263bc
MD
775 long uiosiz, rem;
776 int error = 0;
777
778 mp = *mrep;
779 mbufcp = *dpos;
780 len = mtod(mp, caddr_t)+mp->m_len-mbufcp;
781 rem = nfsm_rndup(siz)-siz;
782 while (siz > 0) {
783 if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
784 return (EFBIG);
785 left = uiop->uio_iov->iov_len;
786 uiocp = uiop->uio_iov->iov_base;
787 if (left > siz)
788 left = siz;
789 uiosiz = left;
790 while (left > 0) {
791 while (len == 0) {
792 mp = mp->m_next;
793 if (mp == NULL)
794 return (EBADRPC);
795 mbufcp = mtod(mp, caddr_t);
796 len = mp->m_len;
797 }
798 xfer = (left > len) ? len : left;
799#ifdef notdef
800 /* Not Yet.. */
801 if (uiop->uio_iov->iov_op != NULL)
802 (*(uiop->uio_iov->iov_op))
803 (mbufcp, uiocp, xfer);
804 else
805#endif
806 if (uiop->uio_segflg == UIO_SYSSPACE)
807 bcopy(mbufcp, uiocp, xfer);
808 else
809 copyout(mbufcp, uiocp, xfer);
810 left -= xfer;
811 len -= xfer;
812 mbufcp += xfer;
813 uiocp += xfer;
814 uiop->uio_offset += xfer;
815 uiop->uio_resid -= xfer;
816 }
817 if (uiop->uio_iov->iov_len <= siz) {
818 uiop->uio_iovcnt--;
819 uiop->uio_iov++;
820 } else {
821 uiop->uio_iov->iov_base += uiosiz;
822 uiop->uio_iov->iov_len -= uiosiz;
823 }
824 siz -= uiosiz;
825 }
826 *dpos = mbufcp;
827 *mrep = mp;
828 if (rem > 0) {
829 if (len < rem)
830 error = nfs_adv(mrep, dpos, rem, len);
831 else
832 *dpos += rem;
833 }
834 return (error);
835}
836
837/*
838 * copies a uio scatter/gather list to an mbuf chain.
839 * NOTE: can ony handle iovcnt == 1
840 */
841int
e851b29e 842nfsm_uiotombuf(struct uio *uiop, struct mbuf **mq, int siz, caddr_t *bpos)
984263bc 843{
40393ded
RG
844 char *uiocp;
845 struct mbuf *mp, *mp2;
846 int xfer, left, mlen;
984263bc
MD
847 int uiosiz, clflg, rem;
848 char *cp;
849
850#ifdef DIAGNOSTIC
851 if (uiop->uio_iovcnt != 1)
852 panic("nfsm_uiotombuf: iovcnt != 1");
853#endif
854
855 if (siz > MLEN) /* or should it >= MCLBYTES ?? */
856 clflg = 1;
857 else
858 clflg = 0;
859 rem = nfsm_rndup(siz)-siz;
860 mp = mp2 = *mq;
861 while (siz > 0) {
862 left = uiop->uio_iov->iov_len;
863 uiocp = uiop->uio_iov->iov_base;
864 if (left > siz)
865 left = siz;
866 uiosiz = left;
867 while (left > 0) {
868 mlen = M_TRAILINGSPACE(mp);
869 if (mlen == 0) {
74f1caca 870 MGET(mp, MB_WAIT, MT_DATA);
984263bc 871 if (clflg)
74f1caca 872 MCLGET(mp, MB_WAIT);
984263bc
MD
873 mp->m_len = 0;
874 mp2->m_next = mp;
875 mp2 = mp;
876 mlen = M_TRAILINGSPACE(mp);
877 }
878 xfer = (left > mlen) ? mlen : left;
879#ifdef notdef
880 /* Not Yet.. */
881 if (uiop->uio_iov->iov_op != NULL)
882 (*(uiop->uio_iov->iov_op))
883 (uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
884 else
885#endif
886 if (uiop->uio_segflg == UIO_SYSSPACE)
887 bcopy(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
888 else
889 copyin(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
890 mp->m_len += xfer;
891 left -= xfer;
892 uiocp += xfer;
893 uiop->uio_offset += xfer;
894 uiop->uio_resid -= xfer;
895 }
896 uiop->uio_iov->iov_base += uiosiz;
897 uiop->uio_iov->iov_len -= uiosiz;
898 siz -= uiosiz;
899 }
900 if (rem > 0) {
901 if (rem > M_TRAILINGSPACE(mp)) {
74f1caca 902 MGET(mp, MB_WAIT, MT_DATA);
984263bc
MD
903 mp->m_len = 0;
904 mp2->m_next = mp;
905 }
906 cp = mtod(mp, caddr_t)+mp->m_len;
907 for (left = 0; left < rem; left++)
908 *cp++ = '\0';
909 mp->m_len += rem;
910 *bpos = cp;
911 } else
912 *bpos = mtod(mp, caddr_t)+mp->m_len;
913 *mq = mp;
914 return (0);
915}
916
917/*
918 * Help break down an mbuf chain by setting the first siz bytes contiguous
919 * pointed to by returned val.
920 * This is used by the macros nfsm_dissect and nfsm_dissecton for tough
921 * cases. (The macros use the vars. dpos and dpos2)
922 */
923int
e851b29e 924nfsm_disct(struct mbuf **mdp, caddr_t *dposp, int siz, int left, caddr_t *cp2)
984263bc 925{
40393ded
RG
926 struct mbuf *mp, *mp2;
927 int siz2, xfer;
928 caddr_t p;
984263bc
MD
929
930 mp = *mdp;
931 while (left == 0) {
932 *mdp = mp = mp->m_next;
933 if (mp == NULL)
934 return (EBADRPC);
935 left = mp->m_len;
936 *dposp = mtod(mp, caddr_t);
937 }
938 if (left >= siz) {
939 *cp2 = *dposp;
940 *dposp += siz;
941 } else if (mp->m_next == NULL) {
942 return (EBADRPC);
943 } else if (siz > MHLEN) {
944 panic("nfs S too big");
945 } else {
74f1caca 946 MGET(mp2, MB_WAIT, MT_DATA);
984263bc
MD
947 mp2->m_next = mp->m_next;
948 mp->m_next = mp2;
949 mp->m_len -= left;
950 mp = mp2;
951 *cp2 = p = mtod(mp, caddr_t);
952 bcopy(*dposp, p, left); /* Copy what was left */
953 siz2 = siz-left;
954 p += left;
955 mp2 = mp->m_next;
956 /* Loop around copying up the siz2 bytes */
957 while (siz2 > 0) {
958 if (mp2 == NULL)
959 return (EBADRPC);
960 xfer = (siz2 > mp2->m_len) ? mp2->m_len : siz2;
961 if (xfer > 0) {
962 bcopy(mtod(mp2, caddr_t), p, xfer);
963 NFSMADV(mp2, xfer);
964 mp2->m_len -= xfer;
965 p += xfer;
966 siz2 -= xfer;
967 }
968 if (siz2 > 0)
969 mp2 = mp2->m_next;
970 }
971 mp->m_len = siz;
972 *mdp = mp2;
973 *dposp = mtod(mp2, caddr_t);
974 }
975 return (0);
976}
977
978/*
979 * Advance the position in the mbuf chain.
980 */
981int
e851b29e 982nfs_adv(struct mbuf **mdp, caddr_t *dposp, int offs, int left)
984263bc 983{
40393ded
RG
984 struct mbuf *m;
985 int s;
984263bc
MD
986
987 m = *mdp;
988 s = left;
989 while (s < offs) {
990 offs -= s;
991 m = m->m_next;
992 if (m == NULL)
993 return (EBADRPC);
994 s = m->m_len;
995 }
996 *mdp = m;
997 *dposp = mtod(m, caddr_t)+offs;
998 return (0);
999}
1000
1001/*
1002 * Copy a string into mbufs for the hard cases...
1003 */
1004int
e851b29e 1005nfsm_strtmbuf(struct mbuf **mb, char **bpos, const char *cp, long siz)
984263bc 1006{
40393ded 1007 struct mbuf *m1 = NULL, *m2;
984263bc
MD
1008 long left, xfer, len, tlen;
1009 u_int32_t *tl;
1010 int putsize;
1011
1012 putsize = 1;
1013 m2 = *mb;
1014 left = M_TRAILINGSPACE(m2);
1015 if (left > 0) {
1016 tl = ((u_int32_t *)(*bpos));
1017 *tl++ = txdr_unsigned(siz);
1018 putsize = 0;
1019 left -= NFSX_UNSIGNED;
1020 m2->m_len += NFSX_UNSIGNED;
1021 if (left > 0) {
1022 bcopy(cp, (caddr_t) tl, left);
1023 siz -= left;
1024 cp += left;
1025 m2->m_len += left;
1026 left = 0;
1027 }
1028 }
1029 /* Loop around adding mbufs */
1030 while (siz > 0) {
74f1caca 1031 MGET(m1, MB_WAIT, MT_DATA);
984263bc 1032 if (siz > MLEN)
74f1caca 1033 MCLGET(m1, MB_WAIT);
984263bc
MD
1034 m1->m_len = NFSMSIZ(m1);
1035 m2->m_next = m1;
1036 m2 = m1;
1037 tl = mtod(m1, u_int32_t *);
1038 tlen = 0;
1039 if (putsize) {
1040 *tl++ = txdr_unsigned(siz);
1041 m1->m_len -= NFSX_UNSIGNED;
1042 tlen = NFSX_UNSIGNED;
1043 putsize = 0;
1044 }
1045 if (siz < m1->m_len) {
1046 len = nfsm_rndup(siz);
1047 xfer = siz;
1048 if (xfer < len)
1049 *(tl+(xfer>>2)) = 0;
1050 } else {
1051 xfer = len = m1->m_len;
1052 }
1053 bcopy(cp, (caddr_t) tl, xfer);
1054 m1->m_len = len+tlen;
1055 siz -= xfer;
1056 cp += xfer;
1057 }
1058 *mb = m1;
1059 *bpos = mtod(m1, caddr_t)+m1->m_len;
1060 return (0);
1061}
1062
1063/*
1064 * Called once to initialize data structures...
1065 */
1066int
e851b29e 1067nfs_init(struct vfsconf *vfsp)
984263bc 1068{
40393ded 1069 int i;
984263bc 1070
f786cc86 1071 callout_init(&nfs_timer_handle);
984263bc
MD
1072 nfsmount_zone = zinit("NFSMOUNT", sizeof(struct nfsmount), 0, 0, 1);
1073
1074 nfs_mount_type = vfsp->vfc_typenum;
1075 nfsrtt.pos = 0;
1076 rpc_vers = txdr_unsigned(RPC_VER2);
1077 rpc_call = txdr_unsigned(RPC_CALL);
1078 rpc_reply = txdr_unsigned(RPC_REPLY);
1079 rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED);
1080 rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED);
1081 rpc_mismatch = txdr_unsigned(RPC_MISMATCH);
1082 rpc_autherr = txdr_unsigned(RPC_AUTHERR);
1083 rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX);
1084 rpc_auth_kerb = txdr_unsigned(RPCAUTH_KERB4);
1085 nfs_prog = txdr_unsigned(NFS_PROG);
1086 nqnfs_prog = txdr_unsigned(NQNFS_PROG);
1087 nfs_true = txdr_unsigned(TRUE);
1088 nfs_false = txdr_unsigned(FALSE);
1089 nfs_xdrneg1 = txdr_unsigned(-1);
1090 nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000;
1091 if (nfs_ticks < 1)
1092 nfs_ticks = 1;
1093 /* Ensure async daemons disabled */
1094 for (i = 0; i < NFS_MAXASYNCDAEMON; i++) {
dadab5e9 1095 nfs_iodwant[i] = NULL;
984263bc
MD
1096 nfs_iodmount[i] = (struct nfsmount *)0;
1097 }
1098 nfs_nhinit(); /* Init the nfsnode table */
1099#ifndef NFS_NOSERVER
1100 nfsrv_init(0); /* Init server data structures */
1101 nfsrv_initcache(); /* Init the server request cache */
1102#endif
1103
1104 /*
1105 * Initialize the nqnfs server stuff.
1106 */
1107 if (nqnfsstarttime == 0) {
1108 nqnfsstarttime = boottime.tv_sec + nqsrv_maxlease
1109 + nqsrv_clockskew + nqsrv_writeslack;
1110 NQLOADNOVRAM(nqnfsstarttime);
1111 CIRCLEQ_INIT(&nqtimerhead);
1112 nqfhhashtbl = hashinit(NQLCHSZ, M_NQLEASE, &nqfhhash);
1113 }
1114
1115 /*
1116 * Initialize reply list and start timer
1117 */
1118 TAILQ_INIT(&nfs_reqq);
1119
1120 nfs_timer(0);
1121
1122 /*
1123 * Set up lease_check and lease_updatetime so that other parts
1124 * of the system can call us, if we are loadable.
1125 */
1126#ifndef NFS_NOSERVER
2d3e977e
MD
1127 nfs_prev_vop_lease_check = default_vnode_vops->vop_lease;
1128 default_vnode_vops->vop_lease = nqnfs_vop_lease_check;
984263bc
MD
1129#endif
1130 nfs_prev_lease_updatetime = lease_updatetime;
1131 lease_updatetime = nfs_lease_updatetime;
1132 nfs_prev_nfssvc_sy_narg = sysent[SYS_nfssvc].sy_narg;
1133 sysent[SYS_nfssvc].sy_narg = 2;
1134 nfs_prev_nfssvc_sy_call = sysent[SYS_nfssvc].sy_call;
1135 sysent[SYS_nfssvc].sy_call = (sy_call_t *)nfssvc;
1136
1137 nfs_pbuf_freecnt = nswbuf / 2 + 1;
1138
1139 return (0);
1140}
1141
1142int
e851b29e 1143nfs_uninit(struct vfsconf *vfsp)
984263bc 1144{
f786cc86 1145 callout_stop(&nfs_timer_handle);
984263bc
MD
1146 nfs_mount_type = -1;
1147#ifndef NFS_NOSERVER
2d3e977e 1148 default_vnode_vops->vop_lease = nfs_prev_vop_lease_check;
984263bc
MD
1149#endif
1150 lease_updatetime = nfs_prev_lease_updatetime;
1151 sysent[SYS_nfssvc].sy_narg = nfs_prev_nfssvc_sy_narg;
1152 sysent[SYS_nfssvc].sy_call = nfs_prev_nfssvc_sy_call;
1153 return (0);
1154}
1155
1156/*
1157 * Attribute cache routines.
1158 * nfs_loadattrcache() - loads or updates the cache contents from attributes
1159 * that are on the mbuf list
1160 * nfs_getattrcache() - returns valid attributes if found in cache, returns
1161 * error otherwise
1162 */
1163
1164/*
1165 * Load the attribute cache (that lives in the nfsnode entry) with
5a9187cb
MD
1166 * the values on the mbuf list. Load *vaper with the attributes. vaper
1167 * may be NULL.
1168 *
1169 * As a side effect n_mtime, which we use to determine if the file was
1170 * modified by some other host, is set to the attribute timestamp and
1171 * NRMODIFIED is set if the two values differ.
1172 *
1173 * WARNING: the mtime loaded into vaper does not necessarily represent
1174 * n_mtime or n_attr.mtime due to NACC and NUPD.
984263bc
MD
1175 */
1176int
e851b29e 1177nfs_loadattrcache(struct vnode **vpp, struct mbuf **mdp, caddr_t *dposp,
5a9187cb 1178 struct vattr *vaper, int lattr_flags)
984263bc 1179{
40393ded
RG
1180 struct vnode *vp = *vpp;
1181 struct vattr *vap;
1182 struct nfs_fattr *fp;
1183 struct nfsnode *np;
1184 int32_t t1;
984263bc 1185 caddr_t cp2;
e4c9c0c8
MD
1186 int error = 0;
1187 udev_t rdev;
984263bc
MD
1188 struct mbuf *md;
1189 enum vtype vtyp;
1190 u_short vmode;
1191 struct timespec mtime;
1192 int v3 = NFS_ISV3(vp);
1193
1194 md = *mdp;
1195 t1 = (mtod(md, caddr_t) + md->m_len) - *dposp;
1196 if ((error = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, &cp2)) != 0)
1197 return (error);
1198 fp = (struct nfs_fattr *)cp2;
1199 if (v3) {
1200 vtyp = nfsv3tov_type(fp->fa_type);
1201 vmode = fxdr_unsigned(u_short, fp->fa_mode);
1202 rdev = makeudev(fxdr_unsigned(int, fp->fa3_rdev.specdata1),
1203 fxdr_unsigned(int, fp->fa3_rdev.specdata2));
1204 fxdr_nfsv3time(&fp->fa3_mtime, &mtime);
1205 } else {
1206 vtyp = nfsv2tov_type(fp->fa_type);
1207 vmode = fxdr_unsigned(u_short, fp->fa_mode);
1208 /*
1209 * XXX
1210 *
1211 * The duplicate information returned in fa_type and fa_mode
1212 * is an ambiguity in the NFS version 2 protocol.
1213 *
1214 * VREG should be taken literally as a regular file. If a
1215 * server intents to return some type information differently
1216 * in the upper bits of the mode field (e.g. for sockets, or
1217 * FIFOs), NFSv2 mandates fa_type to be VNON. Anyway, we
1218 * leave the examination of the mode bits even in the VREG
1219 * case to avoid breakage for bogus servers, but we make sure
1220 * that there are actually type bits set in the upper part of
1221 * fa_mode (and failing that, trust the va_type field).
1222 *
1223 * NFSv3 cleared the issue, and requires fa_mode to not
1224 * contain any type information (while also introduing sockets
1225 * and FIFOs for fa_type).
1226 */
1227 if (vtyp == VNON || (vtyp == VREG && (vmode & S_IFMT) != 0))
1228 vtyp = IFTOVT(vmode);
1229 rdev = fxdr_unsigned(int32_t, fp->fa2_rdev);
1230 fxdr_nfsv2time(&fp->fa2_mtime, &mtime);
1231
1232 /*
1233 * Really ugly NFSv2 kludge.
1234 */
e4c9c0c8 1235 if (vtyp == VCHR && rdev == (udev_t)0xffffffff)
984263bc
MD
1236 vtyp = VFIFO;
1237 }
1238
1239 /*
1240 * If v_type == VNON it is a new node, so fill in the v_type,
1241 * n_mtime fields. Check to see if it represents a special
1242 * device, and if so, check for a possible alias. Once the
1243 * correct vnode has been obtained, fill in the rest of the
1244 * information.
1245 */
1246 np = VTONFS(vp);
1247 if (vp->v_type != vtyp) {
1248 vp->v_type = vtyp;
1249 if (vp->v_type == VFIFO) {
6ddb7618 1250 vp->v_ops = &vp->v_mount->mnt_vn_fifo_ops;
0961aa92 1251 } else if (vp->v_type == VCHR || vp->v_type == VBLK) {
6ddb7618 1252 vp->v_ops = &vp->v_mount->mnt_vn_spec_ops;
984263bc 1253 addaliasu(vp, rdev);
0961aa92 1254 } else {
6ddb7618 1255 vp->v_ops = &vp->v_mount->mnt_vn_use_ops;
984263bc
MD
1256 }
1257 np->n_mtime = mtime.tv_sec;
5a9187cb 1258 } else if (np->n_mtime != mtime.tv_sec) {
9793c819 1259 /*
5a9187cb
MD
1260 * If we haven't modified the file locally and the server
1261 * timestamp does not match, then the server probably
1262 * modified the file. We must flag this condition so
1263 * the proper syncnronization can be done. We do not
1264 * try to synchronize the state here because that
1265 * could lead to an endless recursion.
1266 *
1267 * XXX loadattrcache can be set during the reply to a write,
1268 * before the write timestamp is properly processed. To
1269 * avoid unconditionally setting the rmodified bit (which
1270 * has the effect of flushing the cache), we only do this
1271 * check if the lmodified bit is not set.
9793c819
MD
1272 */
1273 np->n_mtime = mtime.tv_sec;
5a9187cb
MD
1274 if ((lattr_flags & NFS_LATTR_NOMTIMECHECK) == 0)
1275 np->n_flag |= NRMODIFIED;
984263bc
MD
1276 }
1277 vap = &np->n_vattr;
1278 vap->va_type = vtyp;
1279 vap->va_mode = (vmode & 07777);
1280 vap->va_rdev = rdev;
1281 vap->va_mtime = mtime;
1282 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
1283 if (v3) {
1284 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1285 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
1286 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
1287 vap->va_size = fxdr_hyper(&fp->fa3_size);
1288 vap->va_blocksize = NFS_FABLKSIZE;
1289 vap->va_bytes = fxdr_hyper(&fp->fa3_used);
1290 vap->va_fileid = fxdr_unsigned(int32_t,
1291 fp->fa3_fileid.nfsuquad[1]);
1292 fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime);
1293 fxdr_nfsv3time(&fp->fa3_ctime, &vap->va_ctime);
1294 vap->va_flags = 0;
1295 vap->va_filerev = 0;
1296 } else {
1297 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1298 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
1299 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
1300 vap->va_size = fxdr_unsigned(u_int32_t, fp->fa2_size);
1301 vap->va_blocksize = fxdr_unsigned(int32_t, fp->fa2_blocksize);
1302 vap->va_bytes = (u_quad_t)fxdr_unsigned(int32_t, fp->fa2_blocks)
1303 * NFS_FABLKSIZE;
1304 vap->va_fileid = fxdr_unsigned(int32_t, fp->fa2_fileid);
1305 fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime);
1306 vap->va_flags = 0;
1307 vap->va_ctime.tv_sec = fxdr_unsigned(u_int32_t,
1308 fp->fa2_ctime.nfsv2_sec);
1309 vap->va_ctime.tv_nsec = 0;
1310 vap->va_gen = fxdr_unsigned(u_int32_t,fp->fa2_ctime.nfsv2_usec);
1311 vap->va_filerev = 0;
1312 }
1313 np->n_attrstamp = time_second;
1314 if (vap->va_size != np->n_size) {
1315 if (vap->va_type == VREG) {
5a9187cb
MD
1316 if ((lattr_flags & NFS_LATTR_NOSHRINK) &&
1317 vap->va_size < np->n_size) {
984263bc
MD
1318 /*
1319 * We've been told not to shrink the file;
1320 * zero np->n_attrstamp to indicate that
1321 * the attributes are stale.
5a9187cb
MD
1322 *
1323 * This occurs primarily due to recursive
1324 * NFS ops that are executed during periods
1325 * where we cannot safely reduce the size of
1326 * the file.
1327 *
1328 * Additionally, write rpcs are broken down
1329 * into buffers and np->n_size is
1330 * pre-extended. Setting NRMODIFIED here
1331 * can result in n_size getting reset to a
1332 * lower value, which is NOT what we want.
1333 * XXX this needs to be cleaned up a lot
1334 * more.
984263bc
MD
1335 */
1336 vap->va_size = np->n_size;
1337 np->n_attrstamp = 0;
5a9187cb
MD
1338 if ((np->n_flag & NLMODIFIED) == 0)
1339 np->n_flag |= NRMODIFIED;
1340 } else if (np->n_flag & NLMODIFIED) {
79e5012e
MD
1341 /*
1342 * We've modified the file: Use the larger
5a9187cb
MD
1343 * of our size, and the server's size. At
1344 * this point the cache coherency is all
1345 * shot to hell. To try to handle multiple
1346 * clients appending to the file at the same
1347 * time mark that the server has changed
1348 * the file if the server's notion of the
1349 * file size is larger then our notion.
1350 *
1351 * XXX this needs work.
79e5012e
MD
1352 */
1353 if (vap->va_size < np->n_size) {
984263bc 1354 vap->va_size = np->n_size;
79e5012e 1355 } else {
984263bc 1356 np->n_size = vap->va_size;
5a9187cb 1357 np->n_flag |= NRMODIFIED;
79e5012e 1358 }
984263bc 1359 } else {
5a9187cb
MD
1360 /*
1361 * Someone changed the file's size on the
1362 * server and there are no local changes
1363 * to get in the way, set the size and mark
1364 * it.
1365 */
984263bc 1366 np->n_size = vap->va_size;
5a9187cb 1367 np->n_flag |= NRMODIFIED;
984263bc
MD
1368 }
1369 vnode_pager_setsize(vp, np->n_size);
1370 } else {
1371 np->n_size = vap->va_size;
1372 }
1373 }
1374 if (vaper != NULL) {
1375 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap));
1376 if (np->n_flag & NCHG) {
1377 if (np->n_flag & NACC)
1378 vaper->va_atime = np->n_atim;
1379 if (np->n_flag & NUPD)
1380 vaper->va_mtime = np->n_mtim;
1381 }
1382 }
1383 return (0);
1384}
1385
1386#ifdef NFS_ACDEBUG
1387#include <sys/sysctl.h>
1388SYSCTL_DECL(_vfs_nfs);
1389static int nfs_acdebug;
1390SYSCTL_INT(_vfs_nfs, OID_AUTO, acdebug, CTLFLAG_RW, &nfs_acdebug, 0, "");
1391#endif
1392
1393/*
1394 * Check the time stamp
1395 * If the cache is valid, copy contents to *vap and return 0
1396 * otherwise return an error
1397 */
1398int
e851b29e 1399nfs_getattrcache(struct vnode *vp, struct vattr *vaper)
984263bc 1400{
40393ded
RG
1401 struct nfsnode *np;
1402 struct vattr *vap;
984263bc
MD
1403 struct nfsmount *nmp;
1404 int timeo;
1405
1406 np = VTONFS(vp);
1407 vap = &np->n_vattr;
1408 nmp = VFSTONFS(vp->v_mount);
fad57d0e
MD
1409
1410 /*
1411 * Dynamic timeout based on how recently the file was modified.
5a9187cb 1412 * n_mtime is always valid.
fad57d0e 1413 */
9793c819 1414 timeo = (get_approximate_time_t() - np->n_mtime) / 10;
984263bc
MD
1415
1416#ifdef NFS_ACDEBUG
1417 if (nfs_acdebug>1)
1418 printf("nfs_getattrcache: initial timeo = %d\n", timeo);
1419#endif
1420
1421 if (vap->va_type == VDIR) {
5a9187cb 1422 if ((np->n_flag & NLMODIFIED) || timeo < nmp->nm_acdirmin)
984263bc
MD
1423 timeo = nmp->nm_acdirmin;
1424 else if (timeo > nmp->nm_acdirmax)
1425 timeo = nmp->nm_acdirmax;
1426 } else {
5a9187cb 1427 if ((np->n_flag & NLMODIFIED) || timeo < nmp->nm_acregmin)
984263bc
MD
1428 timeo = nmp->nm_acregmin;
1429 else if (timeo > nmp->nm_acregmax)
1430 timeo = nmp->nm_acregmax;
1431 }
1432
1433#ifdef NFS_ACDEBUG
1434 if (nfs_acdebug > 2)
1435 printf("acregmin %d; acregmax %d; acdirmin %d; acdirmax %d\n",
1436 nmp->nm_acregmin, nmp->nm_acregmax,
1437 nmp->nm_acdirmin, nmp->nm_acdirmax);
1438
1439 if (nfs_acdebug)
1440 printf("nfs_getattrcache: age = %d; final timeo = %d\n",
9793c819 1441 (int)(time_second - np->n_attrstamp), timeo);
984263bc
MD
1442#endif
1443
9793c819 1444 if (np->n_attrstamp == 0 || (time_second - np->n_attrstamp) >= timeo) {
984263bc
MD
1445 nfsstats.attrcache_misses++;
1446 return (ENOENT);
1447 }
1448 nfsstats.attrcache_hits++;
5a9187cb
MD
1449
1450 /*
1451 * Our attribute cache can be stale due to modifications made on
1452 * this host. XXX this is a bad hack. We need a more deterministic
1453 * means of finding out which np fields are valid verses attr cache
1454 * fields. We really should update the vattr info on the fly when
1455 * making local changes.
1456 */
984263bc
MD
1457 if (vap->va_size != np->n_size) {
1458 if (vap->va_type == VREG) {
5a9187cb 1459 if (np->n_flag & NLMODIFIED) {
984263bc
MD
1460 if (vap->va_size < np->n_size)
1461 vap->va_size = np->n_size;
1462 else
1463 np->n_size = vap->va_size;
1464 } else {
1465 np->n_size = vap->va_size;
1466 }
1467 vnode_pager_setsize(vp, np->n_size);
1468 } else {
1469 np->n_size = vap->va_size;
1470 }
1471 }
1472 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr));
1473 if (np->n_flag & NCHG) {
1474 if (np->n_flag & NACC)
1475 vaper->va_atime = np->n_atim;
1476 if (np->n_flag & NUPD)
1477 vaper->va_mtime = np->n_mtim;
1478 }
1479 return (0);
1480}
1481
1482#ifndef NFS_NOSERVER
fad57d0e 1483
984263bc
MD
1484/*
1485 * Set up nameidata for a lookup() call and do it.
1486 *
1487 * If pubflag is set, this call is done for a lookup operation on the
1488 * public filehandle. In that case we allow crossing mountpoints and
1489 * absolute pathnames. However, the caller is expected to check that
1490 * the lookup result is within the public fs, and deny access if
1491 * it is not.
1492 *
984263bc
MD
1493 * dirp may be set whether an error is returned or not, and must be
1494 * released by the caller.
fad57d0e
MD
1495 *
1496 * On return nd->nl_ncp usually points to the target ncp, which may represent
1497 * a negative hit.
1498 *
1499 * NOTE: the caller must call nlookup_done(nd) unconditionally on return
1500 * to cleanup.
984263bc
MD
1501 */
1502int
fad57d0e
MD
1503nfs_namei(struct nlookupdata *nd, struct ucred *cred, int nameiop,
1504 struct vnode **dvpp, struct vnode **vpp,
1505 fhandle_t *fhp, int len,
1506 struct nfssvc_sock *slp, struct sockaddr *nam, struct mbuf **mdp,
1507 caddr_t *dposp, struct vnode **dirpp, struct thread *td,
1508 int kerbflag, int pubflag)
984263bc 1509{
40393ded 1510 int i, rem;
fad57d0e 1511 int flags;
40393ded
RG
1512 struct mbuf *md;
1513 char *fromcp, *tocp, *cp;
fad57d0e
MD
1514 char *namebuf;
1515 struct namecache *ncp;
984263bc 1516 struct vnode *dp;
fad57d0e 1517 int error, rdonly;
984263bc 1518
fad57d0e
MD
1519 namebuf = zalloc(namei_zone);
1520 flags = 0;
1521 *dirpp = NULL;
984263bc
MD
1522
1523 /*
fad57d0e 1524 * Copy the name from the mbuf list to namebuf.
984263bc
MD
1525 */
1526 fromcp = *dposp;
fad57d0e 1527 tocp = namebuf;
984263bc
MD
1528 md = *mdp;
1529 rem = mtod(md, caddr_t) + md->m_len - fromcp;
1530 for (i = 0; i < len; i++) {
1531 while (rem == 0) {
1532 md = md->m_next;
1533 if (md == NULL) {
1534 error = EBADRPC;
1535 goto out;
1536 }
1537 fromcp = mtod(md, caddr_t);
1538 rem = md->m_len;
1539 }
1540 if (*fromcp == '\0' || (!pubflag && *fromcp == '/')) {
1541 error = EACCES;
1542 goto out;
1543 }
1544 *tocp++ = *fromcp++;
1545 rem--;
1546 }
1547 *tocp = '\0';
1548 *mdp = md;
1549 *dposp = fromcp;
1550 len = nfsm_rndup(len)-len;
1551 if (len > 0) {
1552 if (rem >= len)
1553 *dposp += len;
1554 else if ((error = nfs_adv(mdp, dposp, len, rem)) != 0)
1555 goto out;
1556 }
1557
1558 /*
fad57d0e
MD
1559 * Extract and set starting directory. The returned dp is refd
1560 * but not locked.
984263bc 1561 */
fad57d0e
MD
1562 error = nfsrv_fhtovp(fhp, FALSE, &dp, cred, slp,
1563 nam, &rdonly, kerbflag, pubflag);
984263bc
MD
1564 if (error)
1565 goto out;
1566 if (dp->v_type != VDIR) {
1567 vrele(dp);
1568 error = ENOTDIR;
1569 goto out;
1570 }
1571
984263bc
MD
1572 /*
1573 * Set return directory. Reference to dp is implicitly transfered
fad57d0e
MD
1574 * to the returned pointer. This must be set before we potentially
1575 * goto out below.
984263bc 1576 */
fad57d0e 1577 *dirpp = dp;
984263bc
MD
1578
1579 if (pubflag) {
1580 /*
1581 * Oh joy. For WebNFS, handle those pesky '%' escapes,
1582 * and the 'native path' indicator.
1583 */
1584 cp = zalloc(namei_zone);
fad57d0e 1585 fromcp = namebuf;
984263bc
MD
1586 tocp = cp;
1587 if ((unsigned char)*fromcp >= WEBNFS_SPECCHAR_START) {
1588 switch ((unsigned char)*fromcp) {
1589 case WEBNFS_NATIVE_CHAR:
1590 /*
1591 * 'Native' path for us is the same
1592 * as a path according to the NFS spec,
1593 * just skip the escape char.
1594 */
1595 fromcp++;
1596 break;
1597 /*
1598 * More may be added in the future, range 0x80-0xff
1599 */
1600 default:
1601 error = EIO;
1602 zfree(namei_zone, cp);
1603 goto out;
1604 }
1605 }
1606 /*
1607 * Translate the '%' escapes, URL-style.
1608 */
1609 while (*fromcp != '\0') {
1610 if (*fromcp == WEBNFS_ESC_CHAR) {
1611 if (fromcp[1] != '\0' && fromcp[2] != '\0') {
1612 fromcp++;
1613 *tocp++ = HEXSTRTOI(fromcp);
1614 fromcp += 2;
1615 continue;
1616 } else {
1617 error = ENOENT;
1618 zfree(namei_zone, cp);
1619 goto out;
1620 }
1621 } else
1622 *tocp++ = *fromcp++;
1623 }
1624 *tocp = '\0';
fad57d0e
MD
1625 zfree(namei_zone, namebuf);
1626 namebuf = cp;
984263bc
MD
1627 }
1628
fad57d0e
MD
1629 /*
1630 * Setup for search. We need to get a start directory from dp. Note
1631 * that dp is ref'd, but we no longer 'own' the ref (*dirpp owns it).
1632 */
1633 if (pubflag == 0) {
1634 flags |= NLC_NFS_NOSOFTLINKTRAV;
1635 flags |= NLC_NOCROSSMOUNT;
984263bc 1636 }
fad57d0e
MD
1637 if (rdonly)
1638 flags |= NLC_NFS_RDONLY;
1639 if (nameiop == NAMEI_CREATE || nameiop == NAMEI_RENAME)
1640 flags |= NLC_CREATE;
984263bc
MD
1641
1642 /*
fad57d0e
MD
1643 * We need a starting ncp from the directory vnode dp. dp must not
1644 * be locked. The returned ncp will be refd but not locked.
1645 *
1646 * If no suitable ncp is found we instruct cache_fromdvp() to create
1647 * one. If this fails the directory has probably been removed while
1648 * the target was chdir'd into it and any further lookup will fail.
984263bc 1649 */
fad57d0e
MD
1650 if ((ncp = cache_fromdvp(dp, cred, 1)) == NULL) {
1651 error = EINVAL;
1652 goto out;
1653 }
1654 nlookup_init_raw(nd, namebuf, UIO_SYSSPACE, flags, cred, ncp);
1655 cache_drop(ncp);
984263bc 1656
fad57d0e
MD
1657 /*
1658 * Ok, do the lookup.
1659 */
1660 error = nlookup(nd);
984263bc 1661
fad57d0e
MD
1662 /*
1663 * If no error occured return the requested dvpp and vpp. If
1664 * NLC_CREATE was specified nd->nl_ncp may represent a negative
1665 * cache hit in which case we do not attempt to obtain the vp.
1666 */
1667 if (error == 0) {
1668 ncp = nd->nl_ncp;
1669 if (dvpp) {
1670 if (ncp->nc_parent &&
1671 ncp->nc_parent->nc_mount == ncp->nc_mount) {
1672 error = cache_vget(ncp->nc_parent, nd->nl_cred,
1673 LK_EXCLUSIVE, dvpp);
1674 } else {
1675 error = ENXIO;
1676 }
984263bc 1677 }
fad57d0e
MD
1678 if (vpp && ncp->nc_vp) {
1679 error = cache_vget(ncp, nd->nl_cred, LK_EXCLUSIVE, vpp);
984263bc 1680 }
984263bc 1681 if (error) {
fad57d0e
MD
1682 if (dvpp && *dvpp) {
1683 vput(*dvpp);
1684 *dvpp = NULL;
1685 }
1686 if (vpp && *vpp) {
1687 vput(*vpp);
1688 *vpp = NULL;
1689 }
984263bc 1690 }
984263bc
MD
1691 }
1692
1693 /*
fad57d0e 1694 * Finish up.
984263bc
MD
1695 */
1696out:
fad57d0e 1697 zfree(namei_zone, namebuf);
984263bc
MD
1698 return (error);
1699}
1700
1701/*
1702 * A fiddled version of m_adj() that ensures null fill to a long
1703 * boundary and only trims off the back end
1704 */
1705void
e851b29e 1706nfsm_adj(struct mbuf *mp, int len, int nul)
984263bc 1707{
40393ded
RG
1708 struct mbuf *m;
1709 int count, i;
1710 char *cp;
984263bc
MD
1711
1712 /*
1713 * Trim from tail. Scan the mbuf chain,
1714 * calculating its length and finding the last mbuf.
1715 * If the adjustment only affects this mbuf, then just
1716 * adjust and return. Otherwise, rescan and truncate
1717 * after the remaining size.
1718 */
1719 count = 0;
1720 m = mp;
1721 for (;;) {
1722 count += m->m_len;
1723 if (m->m_next == (struct mbuf *)0)
1724 break;
1725 m = m->m_next;
1726 }
1727 if (m->m_len > len) {
1728 m->m_len -= len;
1729 if (nul > 0) {
1730 cp = mtod(m, caddr_t)+m->m_len-nul;
1731 for (i = 0; i < nul; i++)
1732 *cp++ = '\0';
1733 }
1734 return;
1735 }
1736 count -= len;
1737 if (count < 0)
1738 count = 0;
1739 /*
1740 * Correct length for chain is "count".
1741 * Find the mbuf with last data, adjust its length,
1742 * and toss data from remaining mbufs on chain.
1743 */
1744 for (m = mp; m; m = m->m_next) {
1745 if (m->m_len >= count) {
1746 m->m_len = count;
1747 if (nul > 0) {
1748 cp = mtod(m, caddr_t)+m->m_len-nul;
1749 for (i = 0; i < nul; i++)
1750 *cp++ = '\0';
1751 }
1752 break;
1753 }
1754 count -= m->m_len;
1755 }
1756 for (m = m->m_next;m;m = m->m_next)
1757 m->m_len = 0;
1758}
1759
1760/*
1761 * Make these functions instead of macros, so that the kernel text size
1762 * doesn't get too big...
1763 */
1764void
e851b29e
CP
1765nfsm_srvwcc(struct nfsrv_descript *nfsd, int before_ret,
1766 struct vattr *before_vap, int after_ret, struct vattr *after_vap,
1767 struct mbuf **mbp, char **bposp)
984263bc 1768{
40393ded
RG
1769 struct mbuf *mb = *mbp, *mb2;
1770 char *bpos = *bposp;
1771 u_int32_t *tl;
984263bc 1772
5a9187cb
MD
1773 /*
1774 * before_ret is 0 if before_vap is valid, non-zero if it isn't.
1775 */
984263bc
MD
1776 if (before_ret) {
1777 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1778 *tl = nfs_false;
1779 } else {
1780 nfsm_build(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
1781 *tl++ = nfs_true;
1782 txdr_hyper(before_vap->va_size, tl);
1783 tl += 2;
1784 txdr_nfsv3time(&(before_vap->va_mtime), tl);
1785 tl += 2;
1786 txdr_nfsv3time(&(before_vap->va_ctime), tl);
1787 }
1788 *bposp = bpos;
1789 *mbp = mb;
1790 nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp);
1791}
1792
1793void
e851b29e
CP
1794nfsm_srvpostopattr(struct nfsrv_descript *nfsd, int after_ret,
1795 struct vattr *after_vap, struct mbuf **mbp, char **bposp)
984263bc 1796{
40393ded
RG
1797 struct mbuf *mb = *mbp, *mb2;
1798 char *bpos = *bposp;
1799 u_int32_t *tl;
1800 struct nfs_fattr *fp;
984263bc
MD
1801
1802 if (after_ret) {
1803 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1804 *tl = nfs_false;
1805 } else {
1806 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FATTR);
1807 *tl++ = nfs_true;
1808 fp = (struct nfs_fattr *)tl;
1809 nfsm_srvfattr(nfsd, after_vap, fp);
1810 }
1811 *mbp = mb;
1812 *bposp = bpos;
1813}
1814
1815void
e851b29e
CP
1816nfsm_srvfattr(struct nfsrv_descript *nfsd, struct vattr *vap,
1817 struct nfs_fattr *fp)
984263bc
MD
1818{
1819
1820 fp->fa_nlink = txdr_unsigned(vap->va_nlink);
1821 fp->fa_uid = txdr_unsigned(vap->va_uid);
1822 fp->fa_gid = txdr_unsigned(vap->va_gid);
1823 if (nfsd->nd_flag & ND_NFSV3) {
1824 fp->fa_type = vtonfsv3_type(vap->va_type);
1825 fp->fa_mode = vtonfsv3_mode(vap->va_mode);
1826 txdr_hyper(vap->va_size, &fp->fa3_size);
1827 txdr_hyper(vap->va_bytes, &fp->fa3_used);
1828 fp->fa3_rdev.specdata1 = txdr_unsigned(umajor(vap->va_rdev));
1829 fp->fa3_rdev.specdata2 = txdr_unsigned(uminor(vap->va_rdev));
1830 fp->fa3_fsid.nfsuquad[0] = 0;
1831 fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(vap->va_fsid);
1832 fp->fa3_fileid.nfsuquad[0] = 0;
1833 fp->fa3_fileid.nfsuquad[1] = txdr_unsigned(vap->va_fileid);
1834 txdr_nfsv3time(&vap->va_atime, &fp->fa3_atime);
1835 txdr_nfsv3time(&vap->va_mtime, &fp->fa3_mtime);
1836 txdr_nfsv3time(&vap->va_ctime, &fp->fa3_ctime);
1837 } else {
1838 fp->fa_type = vtonfsv2_type(vap->va_type);
1839 fp->fa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1840 fp->fa2_size = txdr_unsigned(vap->va_size);
1841 fp->fa2_blocksize = txdr_unsigned(vap->va_blocksize);
1842 if (vap->va_type == VFIFO)
1843 fp->fa2_rdev = 0xffffffff;
1844 else
1845 fp->fa2_rdev = txdr_unsigned(vap->va_rdev);
1846 fp->fa2_blocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE);
1847 fp->fa2_fsid = txdr_unsigned(vap->va_fsid);
1848 fp->fa2_fileid = txdr_unsigned(vap->va_fileid);
1849 txdr_nfsv2time(&vap->va_atime, &fp->fa2_atime);
1850 txdr_nfsv2time(&vap->va_mtime, &fp->fa2_mtime);
1851 txdr_nfsv2time(&vap->va_ctime, &fp->fa2_ctime);
1852 }
1853}
1854
1855/*
1856 * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked)
1857 * - look up fsid in mount list (if not found ret error)
1858 * - get vp and export rights by calling VFS_FHTOVP()
1859 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
1860 * - if not lockflag unlock it with VOP_UNLOCK()
1861 */
1862int
e851b29e
CP
1863nfsrv_fhtovp(fhandle_t *fhp, int lockflag, struct vnode **vpp,
1864 struct ucred *cred, struct nfssvc_sock *slp, struct sockaddr *nam,
1865 int *rdonlyp, int kerbflag, int pubflag)
984263bc 1866{
dadab5e9 1867 struct thread *td = curthread; /* XXX */
40393ded
RG
1868 struct mount *mp;
1869 int i;
984263bc
MD
1870 struct ucred *credanon;
1871 int error, exflags;
1872#ifdef MNT_EXNORESPORT /* XXX needs mountd and /etc/exports help yet */
1873 struct sockaddr_int *saddr;
1874#endif
1875
1876 *vpp = (struct vnode *)0;
1877
1878 if (nfs_ispublicfh(fhp)) {
1879 if (!pubflag || !nfs_pub.np_valid)
1880 return (ESTALE);
1881 fhp = &nfs_pub.np_handle;
1882 }
1883
1884 mp = vfs_getvfs(&fhp->fh_fsid);
1885 if (!mp)
1886 return (ESTALE);
1887 error = VFS_CHECKEXP(mp, nam, &exflags, &credanon);
1888 if (error)
1889 return (error);
1890 error = VFS_FHTOVP(mp, &fhp->fh_fid, vpp);
1891 if (error)
1892 return (error);
1893#ifdef MNT_EXNORESPORT
1894 if (!(exflags & (MNT_EXNORESPORT|MNT_EXPUBLIC))) {
1895 saddr = (struct sockaddr_in *)nam;
1896 if (saddr->sin_family == AF_INET &&
1897 ntohs(saddr->sin_port) >= IPPORT_RESERVED) {
1898 vput(*vpp);
1899 *vpp = NULL;
1900 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1901 }
1902 }
1903#endif
1904 /*
1905 * Check/setup credentials.
1906 */
1907 if (exflags & MNT_EXKERB) {
1908 if (!kerbflag) {
1909 vput(*vpp);
1910 *vpp = NULL;
1911 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1912 }
1913 } else if (kerbflag) {
1914 vput(*vpp);
1915 *vpp = NULL;
1916 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1917 } else if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
1918 cred->cr_uid = credanon->cr_uid;
1919 for (i = 0; i < credanon->cr_ngroups && i < NGROUPS; i++)
1920 cred->cr_groups[i] = credanon->cr_groups[i];
1921 cred->cr_ngroups = i;
1922 }
1923 if (exflags & MNT_EXRDONLY)
1924 *rdonlyp = 1;
1925 else
1926 *rdonlyp = 0;
1927
1928 nfsrv_object_create(*vpp);
1929
1930 if (!lockflag)
5fd012e0 1931 VOP_UNLOCK(*vpp, 0, td);
984263bc
MD
1932 return (0);
1933}
1934
1935
1936/*
1937 * WebNFS: check if a filehandle is a public filehandle. For v3, this
1938 * means a length of 0, for v2 it means all zeroes. nfsm_srvmtofh has
1939 * transformed this to all zeroes in both cases, so check for it.
1940 */
1941int
e851b29e 1942nfs_ispublicfh(fhandle_t *fhp)
984263bc
MD
1943{
1944 char *cp = (char *)fhp;
1945 int i;
1946
1947 for (i = 0; i < NFSX_V3FH; i++)
1948 if (*cp++ != 0)
1949 return (FALSE);
1950 return (TRUE);
1951}
1952
1953#endif /* NFS_NOSERVER */
1954/*
1955 * This function compares two net addresses by family and returns TRUE
1956 * if they are the same host.
1957 * If there is any doubt, return FALSE.
1958 * The AF_INET family is handled as a special case so that address mbufs
1959 * don't need to be saved to store "struct in_addr", which is only 4 bytes.
1960 */
1961int
e851b29e 1962netaddr_match(int family, union nethostaddr *haddr, struct sockaddr *nam)
984263bc 1963{
40393ded 1964 struct sockaddr_in *inetaddr;
984263bc
MD
1965
1966 switch (family) {
1967 case AF_INET:
1968 inetaddr = (struct sockaddr_in *)nam;
1969 if (inetaddr->sin_family == AF_INET &&
1970 inetaddr->sin_addr.s_addr == haddr->had_inetaddr)
1971 return (1);
1972 break;
1973 default:
1974 break;
1975 };
1976 return (0);
1977}
1978
1979static nfsuint64 nfs_nullcookie = { { 0, 0 } };
1980/*
1981 * This function finds the directory cookie that corresponds to the
1982 * logical byte offset given.
1983 */
1984nfsuint64 *
e851b29e 1985nfs_getcookie(struct nfsnode *np, off_t off, int add)
984263bc 1986{
40393ded
RG
1987 struct nfsdmap *dp, *dp2;
1988 int pos;
984263bc
MD
1989
1990 pos = (uoff_t)off / NFS_DIRBLKSIZ;
1991 if (pos == 0 || off < 0) {
1992#ifdef DIAGNOSTIC
1993 if (add)
1994 panic("nfs getcookie add at <= 0");
1995#endif
1996 return (&nfs_nullcookie);
1997 }
1998 pos--;
1999 dp = np->n_cookies.lh_first;
2000 if (!dp) {
2001 if (add) {
2002 MALLOC(dp, struct nfsdmap *, sizeof (struct nfsdmap),
2003 M_NFSDIROFF, M_WAITOK);
2004 dp->ndm_eocookie = 0;
2005 LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list);
2006 } else
2007 return ((nfsuint64 *)0);
2008 }
2009 while (pos >= NFSNUMCOOKIES) {
2010 pos -= NFSNUMCOOKIES;
2011 if (dp->ndm_list.le_next) {
2012 if (!add && dp->ndm_eocookie < NFSNUMCOOKIES &&
2013 pos >= dp->ndm_eocookie)
2014 return ((nfsuint64 *)0);
2015 dp = dp->ndm_list.le_next;
2016 } else if (add) {
2017 MALLOC(dp2, struct nfsdmap *, sizeof (struct nfsdmap),
2018 M_NFSDIROFF, M_WAITOK);
2019 dp2->ndm_eocookie = 0;
2020 LIST_INSERT_AFTER(dp, dp2, ndm_list);
2021 dp = dp2;
2022 } else
2023 return ((nfsuint64 *)0);
2024 }
2025 if (pos >= dp->ndm_eocookie) {
2026 if (add)
2027 dp->ndm_eocookie = pos + 1;
2028 else
2029 return ((nfsuint64 *)0);
2030 }
2031 return (&dp->ndm_cookies[pos]);
2032}
2033
2034/*
2035 * Invalidate cached directory information, except for the actual directory
2036 * blocks (which are invalidated separately).
2037 * Done mainly to avoid the use of stale offset cookies.
2038 */
2039void
e851b29e 2040nfs_invaldir(struct vnode *vp)
984263bc 2041{
40393ded 2042 struct nfsnode *np = VTONFS(vp);
984263bc
MD
2043
2044#ifdef DIAGNOSTIC
2045 if (vp->v_type != VDIR)
2046 panic("nfs: invaldir not dir");
2047#endif
2048 np->n_direofoffset = 0;
2049 np->n_cookieverf.nfsuquad[0] = 0;
2050 np->n_cookieverf.nfsuquad[1] = 0;
2051 if (np->n_cookies.lh_first)
2052 np->n_cookies.lh_first->ndm_eocookie = 0;
2053}
2054
2055/*
2056 * The write verifier has changed (probably due to a server reboot), so all
2057 * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
2058 * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
2059 * and B_CLUSTEROK flags. Once done the new write verifier can be set for the
2060 * mount point.
2061 *
2062 * B_CLUSTEROK must be cleared along with B_NEEDCOMMIT because stage 1 data
2063 * writes are not clusterable.
2064 */
6bae6177
MD
2065
2066static int nfs_clearcommit_bp(struct buf *bp, void *data __unused);
2067
984263bc 2068void
e851b29e 2069nfs_clearcommit(struct mount *mp)
984263bc 2070{
40393ded 2071 struct vnode *vp, *nvp;
b2eb81cd 2072 lwkt_tokref ilock;
984263bc
MD
2073 int s;
2074
b2eb81cd 2075 lwkt_gettoken(&ilock, &mntvnode_token);
984263bc 2076 s = splbio();
984263bc 2077 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp; vp = nvp) {
41a01a4d 2078 nvp = TAILQ_NEXT(vp, v_nmntvnodes); /* ZZZ */
6bae6177
MD
2079 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
2080 nfs_clearcommit_bp, NULL);
984263bc
MD
2081 }
2082 splx(s);
b2eb81cd 2083 lwkt_reltoken(&ilock);
984263bc
MD
2084}
2085
6bae6177
MD
2086static int
2087nfs_clearcommit_bp(struct buf *bp, void *data __unused)
2088{
2089 if (BUF_REFCNT(bp) == 0 &&
2090 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2091 == (B_DELWRI | B_NEEDCOMMIT)) {
2092 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
2093 }
2094 return(0);
2095}
2096
984263bc
MD
2097#ifndef NFS_NOSERVER
2098/*
2099 * Map errnos to NFS error numbers. For Version 3 also filter out error
2100 * numbers not specified for the associated procedure.
2101 */
2102int
e851b29e 2103nfsrv_errmap(struct nfsrv_descript *nd, int err)
984263bc 2104{
40393ded 2105 short *defaulterrp, *errp;
984263bc
MD
2106
2107 if (nd->nd_flag & ND_NFSV3) {
2108 if (nd->nd_procnum <= NFSPROC_COMMIT) {
2109 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
2110 while (*++errp) {
2111 if (*errp == err)
2112 return (err);
2113 else if (*errp > err)
2114 break;
2115 }
2116 return ((int)*defaulterrp);
2117 } else
2118 return (err & 0xffff);
2119 }
2120 if (err <= ELAST)
2121 return ((int)nfsrv_v2errmap[err - 1]);
2122 return (NFSERR_IO);
2123}
2124
2125int
dadab5e9 2126nfsrv_object_create(struct vnode *vp)
984263bc 2127{
dadab5e9 2128 struct thread *td = curthread;
984263bc
MD
2129
2130 if (vp == NULL || vp->v_type != VREG)
2131 return (1);
3b568787 2132 return (vfs_object_create(vp, td));
984263bc
MD
2133}
2134
2135/*
2136 * Sort the group list in increasing numerical order.
2137 * (Insertion sort by Chris Torek, who was grossed out by the bubble sort
2138 * that used to be here.)
2139 */
2140void
e851b29e 2141nfsrvw_sort(gid_t *list, int num)
984263bc 2142{
40393ded 2143 int i, j;
984263bc
MD
2144 gid_t v;
2145
2146 /* Insertion sort. */
2147 for (i = 1; i < num; i++) {
2148 v = list[i];
2149 /* find correct slot for value v, moving others up */
2150 for (j = i; --j >= 0 && v < list[j];)
2151 list[j + 1] = list[j];
2152 list[j + 1] = v;
2153 }
2154}
2155
2156/*
2157 * copy credentials making sure that the result can be compared with bcmp().
2158 */
2159void
e851b29e 2160nfsrv_setcred(struct ucred *incred, struct ucred *outcred)
984263bc 2161{
40393ded 2162 int i;
984263bc
MD
2163
2164 bzero((caddr_t)outcred, sizeof (struct ucred));
2165 outcred->cr_ref = 1;
2166 outcred->cr_uid = incred->cr_uid;
2167 outcred->cr_ngroups = incred->cr_ngroups;
2168 for (i = 0; i < incred->cr_ngroups; i++)
2169 outcred->cr_groups[i] = incred->cr_groups[i];
2170 nfsrvw_sort(outcred->cr_groups, outcred->cr_ngroups);
2171}
2172#endif /* NFS_NOSERVER */