Remove unused defines
[dragonfly.git] / sys / vfs / nfs / nfs_subs.c
... / ...
CommitLineData
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
37 * $FreeBSD: /repoman/r/ncvs/src/sys/nfsclient/nfs_subs.c,v 1.128 2004/04/14 23:23:55 peadar Exp $
38 * $DragonFly: src/sys/vfs/nfs/nfs_subs.c,v 1.20 2004/08/17 18:57:34 dillon Exp $
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>
53#include <sys/namei.h>
54#include <sys/mbuf.h>
55#include <sys/socket.h>
56#include <sys/stat.h>
57#include <sys/malloc.h>
58#include <sys/sysent.h>
59#include <sys/syscall.h>
60#include <sys/conf.h>
61
62#include <vm/vm.h>
63#include <vm/vm_object.h>
64#include <vm/vm_extern.h>
65#include <vm/vm_zone.h>
66
67#include <sys/buf2.h>
68
69#include "rpcv2.h"
70#include "nfsproto.h"
71#include "nfs.h"
72#include "nfsmount.h"
73#include "nfsnode.h"
74#include "xdr_subs.h"
75#include "nfsm_subs.h"
76#include "nqnfs.h"
77#include "nfsrtt.h"
78
79#include <netinet/in.h>
80
81/*
82 * Data items converted to xdr at startup, since they are constant
83 * This is kinda hokey, but may save a little time doing byte swaps
84 */
85u_int32_t nfs_xdrneg1;
86u_int32_t rpc_call, rpc_vers, rpc_reply, rpc_msgdenied, rpc_autherr,
87 rpc_mismatch, rpc_auth_unix, rpc_msgaccepted,
88 rpc_auth_kerb;
89u_int32_t nfs_prog, nqnfs_prog, nfs_true, nfs_false;
90
91/* And other global data */
92static u_int32_t nfs_xid = 0;
93static enum vtype nv2tov_type[8]= {
94 VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON, VNON
95};
96enum vtype nv3tov_type[8]= {
97 VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO
98};
99
100int nfs_ticks;
101int nfs_pbuf_freecnt = -1; /* start out unlimited */
102
103struct nfs_reqq nfs_reqq;
104struct nfssvc_sockhead nfssvc_sockhead;
105int nfssvc_sockhead_flag;
106struct nfsd_head nfsd_head;
107int nfsd_head_flag;
108struct nfs_bufq nfs_bufq;
109struct nqtimerhead nqtimerhead;
110struct nqfhhashhead *nqfhhashtbl;
111u_long nqfhhash;
112
113static void (*nfs_prev_lease_updatetime) (int);
114static int nfs_prev_nfssvc_sy_narg;
115static sy_call_t *nfs_prev_nfssvc_sy_call;
116
117#ifndef NFS_NOSERVER
118
119static int (*nfs_prev_vop_lease_check)(struct vop_lease_args *);
120
121/*
122 * Mapping of old NFS Version 2 RPC numbers to generic numbers.
123 */
124int nfsv3_procid[NFS_NPROCS] = {
125 NFSPROC_NULL,
126 NFSPROC_GETATTR,
127 NFSPROC_SETATTR,
128 NFSPROC_NOOP,
129 NFSPROC_LOOKUP,
130 NFSPROC_READLINK,
131 NFSPROC_READ,
132 NFSPROC_NOOP,
133 NFSPROC_WRITE,
134 NFSPROC_CREATE,
135 NFSPROC_REMOVE,
136 NFSPROC_RENAME,
137 NFSPROC_LINK,
138 NFSPROC_SYMLINK,
139 NFSPROC_MKDIR,
140 NFSPROC_RMDIR,
141 NFSPROC_READDIR,
142 NFSPROC_FSSTAT,
143 NFSPROC_NOOP,
144 NFSPROC_NOOP,
145 NFSPROC_NOOP,
146 NFSPROC_NOOP,
147 NFSPROC_NOOP,
148 NFSPROC_NOOP,
149 NFSPROC_NOOP,
150 NFSPROC_NOOP
151};
152
153#endif /* NFS_NOSERVER */
154/*
155 * and the reverse mapping from generic to Version 2 procedure numbers
156 */
157int nfsv2_procid[NFS_NPROCS] = {
158 NFSV2PROC_NULL,
159 NFSV2PROC_GETATTR,
160 NFSV2PROC_SETATTR,
161 NFSV2PROC_LOOKUP,
162 NFSV2PROC_NOOP,
163 NFSV2PROC_READLINK,
164 NFSV2PROC_READ,
165 NFSV2PROC_WRITE,
166 NFSV2PROC_CREATE,
167 NFSV2PROC_MKDIR,
168 NFSV2PROC_SYMLINK,
169 NFSV2PROC_CREATE,
170 NFSV2PROC_REMOVE,
171 NFSV2PROC_RMDIR,
172 NFSV2PROC_RENAME,
173 NFSV2PROC_LINK,
174 NFSV2PROC_READDIR,
175 NFSV2PROC_NOOP,
176 NFSV2PROC_STATFS,
177 NFSV2PROC_NOOP,
178 NFSV2PROC_NOOP,
179 NFSV2PROC_NOOP,
180 NFSV2PROC_NOOP,
181 NFSV2PROC_NOOP,
182 NFSV2PROC_NOOP,
183 NFSV2PROC_NOOP,
184};
185
186#ifndef NFS_NOSERVER
187/*
188 * Maps errno values to nfs error numbers.
189 * Use NFSERR_IO as the catch all for ones not specifically defined in
190 * RFC 1094.
191 */
192static u_char nfsrv_v2errmap[ELAST] = {
193 NFSERR_PERM, NFSERR_NOENT, NFSERR_IO, NFSERR_IO, NFSERR_IO,
194 NFSERR_NXIO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
195 NFSERR_IO, NFSERR_IO, NFSERR_ACCES, NFSERR_IO, NFSERR_IO,
196 NFSERR_IO, NFSERR_EXIST, NFSERR_IO, NFSERR_NODEV, NFSERR_NOTDIR,
197 NFSERR_ISDIR, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
198 NFSERR_IO, NFSERR_FBIG, NFSERR_NOSPC, NFSERR_IO, NFSERR_ROFS,
199 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
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_NAMETOL, NFSERR_IO, NFSERR_IO,
206 NFSERR_NOTEMPTY, NFSERR_IO, NFSERR_IO, NFSERR_DQUOT, NFSERR_STALE,
207 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
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 /* << Last is 86 */
211};
212
213/*
214 * Maps errno values to nfs error numbers.
215 * Although it is not obvious whether or not NFS clients really care if
216 * a returned error value is in the specified list for the procedure, the
217 * safest thing to do is filter them appropriately. For Version 2, the
218 * X/Open XNFS document is the only specification that defines error values
219 * for each RPC (The RFC simply lists all possible error values for all RPCs),
220 * so I have decided to not do this for Version 2.
221 * The first entry is the default error return and the rest are the valid
222 * errors for that RPC in increasing numeric order.
223 */
224static short nfsv3err_null[] = {
225 0,
226 0,
227};
228
229static short nfsv3err_getattr[] = {
230 NFSERR_IO,
231 NFSERR_IO,
232 NFSERR_STALE,
233 NFSERR_BADHANDLE,
234 NFSERR_SERVERFAULT,
235 0,
236};
237
238static short nfsv3err_setattr[] = {
239 NFSERR_IO,
240 NFSERR_PERM,
241 NFSERR_IO,
242 NFSERR_ACCES,
243 NFSERR_INVAL,
244 NFSERR_NOSPC,
245 NFSERR_ROFS,
246 NFSERR_DQUOT,
247 NFSERR_STALE,
248 NFSERR_BADHANDLE,
249 NFSERR_NOT_SYNC,
250 NFSERR_SERVERFAULT,
251 0,
252};
253
254static short nfsv3err_lookup[] = {
255 NFSERR_IO,
256 NFSERR_NOENT,
257 NFSERR_IO,
258 NFSERR_ACCES,
259 NFSERR_NOTDIR,
260 NFSERR_NAMETOL,
261 NFSERR_STALE,
262 NFSERR_BADHANDLE,
263 NFSERR_SERVERFAULT,
264 0,
265};
266
267static short nfsv3err_access[] = {
268 NFSERR_IO,
269 NFSERR_IO,
270 NFSERR_STALE,
271 NFSERR_BADHANDLE,
272 NFSERR_SERVERFAULT,
273 0,
274};
275
276static short nfsv3err_readlink[] = {
277 NFSERR_IO,
278 NFSERR_IO,
279 NFSERR_ACCES,
280 NFSERR_INVAL,
281 NFSERR_STALE,
282 NFSERR_BADHANDLE,
283 NFSERR_NOTSUPP,
284 NFSERR_SERVERFAULT,
285 0,
286};
287
288static short nfsv3err_read[] = {
289 NFSERR_IO,
290 NFSERR_IO,
291 NFSERR_NXIO,
292 NFSERR_ACCES,
293 NFSERR_INVAL,
294 NFSERR_STALE,
295 NFSERR_BADHANDLE,
296 NFSERR_SERVERFAULT,
297 0,
298};
299
300static short nfsv3err_write[] = {
301 NFSERR_IO,
302 NFSERR_IO,
303 NFSERR_ACCES,
304 NFSERR_INVAL,
305 NFSERR_FBIG,
306 NFSERR_NOSPC,
307 NFSERR_ROFS,
308 NFSERR_DQUOT,
309 NFSERR_STALE,
310 NFSERR_BADHANDLE,
311 NFSERR_SERVERFAULT,
312 0,
313};
314
315static short nfsv3err_create[] = {
316 NFSERR_IO,
317 NFSERR_IO,
318 NFSERR_ACCES,
319 NFSERR_EXIST,
320 NFSERR_NOTDIR,
321 NFSERR_NOSPC,
322 NFSERR_ROFS,
323 NFSERR_NAMETOL,
324 NFSERR_DQUOT,
325 NFSERR_STALE,
326 NFSERR_BADHANDLE,
327 NFSERR_NOTSUPP,
328 NFSERR_SERVERFAULT,
329 0,
330};
331
332static short nfsv3err_mkdir[] = {
333 NFSERR_IO,
334 NFSERR_IO,
335 NFSERR_ACCES,
336 NFSERR_EXIST,
337 NFSERR_NOTDIR,
338 NFSERR_NOSPC,
339 NFSERR_ROFS,
340 NFSERR_NAMETOL,
341 NFSERR_DQUOT,
342 NFSERR_STALE,
343 NFSERR_BADHANDLE,
344 NFSERR_NOTSUPP,
345 NFSERR_SERVERFAULT,
346 0,
347};
348
349static short nfsv3err_symlink[] = {
350 NFSERR_IO,
351 NFSERR_IO,
352 NFSERR_ACCES,
353 NFSERR_EXIST,
354 NFSERR_NOTDIR,
355 NFSERR_NOSPC,
356 NFSERR_ROFS,
357 NFSERR_NAMETOL,
358 NFSERR_DQUOT,
359 NFSERR_STALE,
360 NFSERR_BADHANDLE,
361 NFSERR_NOTSUPP,
362 NFSERR_SERVERFAULT,
363 0,
364};
365
366static short nfsv3err_mknod[] = {
367 NFSERR_IO,
368 NFSERR_IO,
369 NFSERR_ACCES,
370 NFSERR_EXIST,
371 NFSERR_NOTDIR,
372 NFSERR_NOSPC,
373 NFSERR_ROFS,
374 NFSERR_NAMETOL,
375 NFSERR_DQUOT,
376 NFSERR_STALE,
377 NFSERR_BADHANDLE,
378 NFSERR_NOTSUPP,
379 NFSERR_SERVERFAULT,
380 NFSERR_BADTYPE,
381 0,
382};
383
384static short nfsv3err_remove[] = {
385 NFSERR_IO,
386 NFSERR_NOENT,
387 NFSERR_IO,
388 NFSERR_ACCES,
389 NFSERR_NOTDIR,
390 NFSERR_ROFS,
391 NFSERR_NAMETOL,
392 NFSERR_STALE,
393 NFSERR_BADHANDLE,
394 NFSERR_SERVERFAULT,
395 0,
396};
397
398static short nfsv3err_rmdir[] = {
399 NFSERR_IO,
400 NFSERR_NOENT,
401 NFSERR_IO,
402 NFSERR_ACCES,
403 NFSERR_EXIST,
404 NFSERR_NOTDIR,
405 NFSERR_INVAL,
406 NFSERR_ROFS,
407 NFSERR_NAMETOL,
408 NFSERR_NOTEMPTY,
409 NFSERR_STALE,
410 NFSERR_BADHANDLE,
411 NFSERR_NOTSUPP,
412 NFSERR_SERVERFAULT,
413 0,
414};
415
416static short nfsv3err_rename[] = {
417 NFSERR_IO,
418 NFSERR_NOENT,
419 NFSERR_IO,
420 NFSERR_ACCES,
421 NFSERR_EXIST,
422 NFSERR_XDEV,
423 NFSERR_NOTDIR,
424 NFSERR_ISDIR,
425 NFSERR_INVAL,
426 NFSERR_NOSPC,
427 NFSERR_ROFS,
428 NFSERR_MLINK,
429 NFSERR_NAMETOL,
430 NFSERR_NOTEMPTY,
431 NFSERR_DQUOT,
432 NFSERR_STALE,
433 NFSERR_BADHANDLE,
434 NFSERR_NOTSUPP,
435 NFSERR_SERVERFAULT,
436 0,
437};
438
439static short nfsv3err_link[] = {
440 NFSERR_IO,
441 NFSERR_IO,
442 NFSERR_ACCES,
443 NFSERR_EXIST,
444 NFSERR_XDEV,
445 NFSERR_NOTDIR,
446 NFSERR_INVAL,
447 NFSERR_NOSPC,
448 NFSERR_ROFS,
449 NFSERR_MLINK,
450 NFSERR_NAMETOL,
451 NFSERR_DQUOT,
452 NFSERR_STALE,
453 NFSERR_BADHANDLE,
454 NFSERR_NOTSUPP,
455 NFSERR_SERVERFAULT,
456 0,
457};
458
459static short nfsv3err_readdir[] = {
460 NFSERR_IO,
461 NFSERR_IO,
462 NFSERR_ACCES,
463 NFSERR_NOTDIR,
464 NFSERR_STALE,
465 NFSERR_BADHANDLE,
466 NFSERR_BAD_COOKIE,
467 NFSERR_TOOSMALL,
468 NFSERR_SERVERFAULT,
469 0,
470};
471
472static short nfsv3err_readdirplus[] = {
473 NFSERR_IO,
474 NFSERR_IO,
475 NFSERR_ACCES,
476 NFSERR_NOTDIR,
477 NFSERR_STALE,
478 NFSERR_BADHANDLE,
479 NFSERR_BAD_COOKIE,
480 NFSERR_NOTSUPP,
481 NFSERR_TOOSMALL,
482 NFSERR_SERVERFAULT,
483 0,
484};
485
486static short nfsv3err_fsstat[] = {
487 NFSERR_IO,
488 NFSERR_IO,
489 NFSERR_STALE,
490 NFSERR_BADHANDLE,
491 NFSERR_SERVERFAULT,
492 0,
493};
494
495static short nfsv3err_fsinfo[] = {
496 NFSERR_STALE,
497 NFSERR_STALE,
498 NFSERR_BADHANDLE,
499 NFSERR_SERVERFAULT,
500 0,
501};
502
503static short nfsv3err_pathconf[] = {
504 NFSERR_STALE,
505 NFSERR_STALE,
506 NFSERR_BADHANDLE,
507 NFSERR_SERVERFAULT,
508 0,
509};
510
511static short nfsv3err_commit[] = {
512 NFSERR_IO,
513 NFSERR_IO,
514 NFSERR_STALE,
515 NFSERR_BADHANDLE,
516 NFSERR_SERVERFAULT,
517 0,
518};
519
520static short *nfsrv_v3errmap[] = {
521 nfsv3err_null,
522 nfsv3err_getattr,
523 nfsv3err_setattr,
524 nfsv3err_lookup,
525 nfsv3err_access,
526 nfsv3err_readlink,
527 nfsv3err_read,
528 nfsv3err_write,
529 nfsv3err_create,
530 nfsv3err_mkdir,
531 nfsv3err_symlink,
532 nfsv3err_mknod,
533 nfsv3err_remove,
534 nfsv3err_rmdir,
535 nfsv3err_rename,
536 nfsv3err_link,
537 nfsv3err_readdir,
538 nfsv3err_readdirplus,
539 nfsv3err_fsstat,
540 nfsv3err_fsinfo,
541 nfsv3err_pathconf,
542 nfsv3err_commit,
543};
544
545#endif /* NFS_NOSERVER */
546
547extern struct nfsrtt nfsrtt;
548extern time_t nqnfsstarttime;
549extern int nqsrv_clockskew;
550extern int nqsrv_writeslack;
551extern int nqsrv_maxlease;
552extern struct nfsstats nfsstats;
553extern int nqnfs_piggy[NFS_NPROCS];
554extern nfstype nfsv2_type[9];
555extern nfstype nfsv3_type[9];
556extern struct nfsnodehashhead *nfsnodehashtbl;
557extern u_long nfsnodehash;
558
559struct nfssvc_args;
560extern int nfssvc(struct proc *, struct nfssvc_args *, int *);
561
562LIST_HEAD(nfsnodehashhead, nfsnode);
563
564int nfs_webnamei (struct nameidata *, struct vnode *, struct proc *);
565
566u_quad_t
567nfs_curusec(void)
568{
569 struct timeval tv;
570
571 getmicrotime(&tv);
572 return ((u_quad_t)tv.tv_sec * 1000000 + (u_quad_t)tv.tv_usec);
573}
574
575/*
576 * Create the header for an rpc request packet
577 * The hsiz is the size of the rest of the nfs request header.
578 * (just used to decide if a cluster is a good idea)
579 */
580struct mbuf *
581nfsm_reqh(struct vnode *vp, u_long procid, int hsiz, caddr_t *bposp)
582{
583 struct mbuf *mb;
584 u_int32_t *tl;
585 caddr_t bpos;
586 struct mbuf *mb2;
587 struct nfsmount *nmp;
588 int nqflag;
589
590 MGET(mb, MB_WAIT, MT_DATA);
591 if (hsiz >= MINCLSIZE)
592 MCLGET(mb, MB_WAIT);
593 mb->m_len = 0;
594 bpos = mtod(mb, caddr_t);
595
596 /*
597 * For NQNFS, add lease request.
598 */
599 if (vp) {
600 nmp = VFSTONFS(vp->v_mount);
601 if (nmp->nm_flag & NFSMNT_NQNFS) {
602 nqflag = NQNFS_NEEDLEASE(vp, procid);
603 if (nqflag) {
604 nfsm_build(tl, u_int32_t *, 2*NFSX_UNSIGNED);
605 *tl++ = txdr_unsigned(nqflag);
606 *tl = txdr_unsigned(nmp->nm_leaseterm);
607 } else {
608 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
609 *tl = 0;
610 }
611 }
612 }
613 /* Finally, return values */
614 *bposp = bpos;
615 return (mb);
616}
617
618/*
619 * Build the RPC header and fill in the authorization info.
620 * The authorization string argument is only used when the credentials
621 * come from outside of the kernel.
622 * Returns the head of the mbuf list.
623 */
624struct mbuf *
625nfsm_rpchead(struct ucred *cr, int nmflag, int procid, int auth_type,
626 int auth_len, char *auth_str, int verf_len, char *verf_str,
627 struct mbuf *mrest, int mrest_len, struct mbuf **mbp,
628 u_int32_t *xidp)
629{
630 struct mbuf *mb;
631 u_int32_t *tl;
632 caddr_t bpos;
633 int i;
634 struct mbuf *mreq, *mb2;
635 int siz, grpsiz, authsiz;
636
637 authsiz = nfsm_rndup(auth_len);
638 MGETHDR(mb, MB_WAIT, MT_DATA);
639 if ((authsiz + 10 * NFSX_UNSIGNED) >= MINCLSIZE) {
640 MCLGET(mb, MB_WAIT);
641 } else if ((authsiz + 10 * NFSX_UNSIGNED) < MHLEN) {
642 MH_ALIGN(mb, authsiz + 10 * NFSX_UNSIGNED);
643 } else {
644 MH_ALIGN(mb, 8 * NFSX_UNSIGNED);
645 }
646 mb->m_len = 0;
647 mreq = mb;
648 bpos = mtod(mb, caddr_t);
649
650 /*
651 * First the RPC header.
652 */
653 nfsm_build(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
654
655 /* Get a pretty random xid to start with */
656 if (!nfs_xid)
657 nfs_xid = random();
658 /*
659 * Skip zero xid if it should ever happen.
660 */
661 if (++nfs_xid == 0)
662 nfs_xid++;
663
664 *tl++ = *xidp = txdr_unsigned(nfs_xid);
665 *tl++ = rpc_call;
666 *tl++ = rpc_vers;
667 if (nmflag & NFSMNT_NQNFS) {
668 *tl++ = txdr_unsigned(NQNFS_PROG);
669 *tl++ = txdr_unsigned(NQNFS_VER3);
670 } else {
671 *tl++ = txdr_unsigned(NFS_PROG);
672 if (nmflag & NFSMNT_NFSV3)
673 *tl++ = txdr_unsigned(NFS_VER3);
674 else
675 *tl++ = txdr_unsigned(NFS_VER2);
676 }
677 if (nmflag & NFSMNT_NFSV3)
678 *tl++ = txdr_unsigned(procid);
679 else
680 *tl++ = txdr_unsigned(nfsv2_procid[procid]);
681
682 /*
683 * And then the authorization cred.
684 */
685 *tl++ = txdr_unsigned(auth_type);
686 *tl = txdr_unsigned(authsiz);
687 switch (auth_type) {
688 case RPCAUTH_UNIX:
689 nfsm_build(tl, u_int32_t *, auth_len);
690 *tl++ = 0; /* stamp ?? */
691 *tl++ = 0; /* NULL hostname */
692 *tl++ = txdr_unsigned(cr->cr_uid);
693 *tl++ = txdr_unsigned(cr->cr_groups[0]);
694 grpsiz = (auth_len >> 2) - 5;
695 *tl++ = txdr_unsigned(grpsiz);
696 for (i = 1; i <= grpsiz; i++)
697 *tl++ = txdr_unsigned(cr->cr_groups[i]);
698 break;
699 case RPCAUTH_KERB4:
700 siz = auth_len;
701 while (siz > 0) {
702 if (M_TRAILINGSPACE(mb) == 0) {
703 MGET(mb2, MB_WAIT, MT_DATA);
704 if (siz >= MINCLSIZE)
705 MCLGET(mb2, MB_WAIT);
706 mb->m_next = mb2;
707 mb = mb2;
708 mb->m_len = 0;
709 bpos = mtod(mb, caddr_t);
710 }
711 i = min(siz, M_TRAILINGSPACE(mb));
712 bcopy(auth_str, bpos, i);
713 mb->m_len += i;
714 auth_str += i;
715 bpos += i;
716 siz -= i;
717 }
718 if ((siz = (nfsm_rndup(auth_len) - auth_len)) > 0) {
719 for (i = 0; i < siz; i++)
720 *bpos++ = '\0';
721 mb->m_len += siz;
722 }
723 break;
724 };
725
726 /*
727 * And the verifier...
728 */
729 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
730 if (verf_str) {
731 *tl++ = txdr_unsigned(RPCAUTH_KERB4);
732 *tl = txdr_unsigned(verf_len);
733 siz = verf_len;
734 while (siz > 0) {
735 if (M_TRAILINGSPACE(mb) == 0) {
736 MGET(mb2, MB_WAIT, MT_DATA);
737 if (siz >= MINCLSIZE)
738 MCLGET(mb2, MB_WAIT);
739 mb->m_next = mb2;
740 mb = mb2;
741 mb->m_len = 0;
742 bpos = mtod(mb, caddr_t);
743 }
744 i = min(siz, M_TRAILINGSPACE(mb));
745 bcopy(verf_str, bpos, i);
746 mb->m_len += i;
747 verf_str += i;
748 bpos += i;
749 siz -= i;
750 }
751 if ((siz = (nfsm_rndup(verf_len) - verf_len)) > 0) {
752 for (i = 0; i < siz; i++)
753 *bpos++ = '\0';
754 mb->m_len += siz;
755 }
756 } else {
757 *tl++ = txdr_unsigned(RPCAUTH_NULL);
758 *tl = 0;
759 }
760 mb->m_next = mrest;
761 mreq->m_pkthdr.len = authsiz + 10 * NFSX_UNSIGNED + mrest_len;
762 mreq->m_pkthdr.rcvif = (struct ifnet *)0;
763 *mbp = mb;
764 return (mreq);
765}
766
767/*
768 * copies mbuf chain to the uio scatter/gather list
769 */
770int
771nfsm_mbuftouio(struct mbuf **mrep, struct uio *uiop, int siz, caddr_t *dpos)
772{
773 char *mbufcp, *uiocp;
774 int xfer, left, len;
775 struct mbuf *mp;
776 long uiosiz, rem;
777 int error = 0;
778
779 mp = *mrep;
780 mbufcp = *dpos;
781 len = mtod(mp, caddr_t)+mp->m_len-mbufcp;
782 rem = nfsm_rndup(siz)-siz;
783 while (siz > 0) {
784 if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
785 return (EFBIG);
786 left = uiop->uio_iov->iov_len;
787 uiocp = uiop->uio_iov->iov_base;
788 if (left > siz)
789 left = siz;
790 uiosiz = left;
791 while (left > 0) {
792 while (len == 0) {
793 mp = mp->m_next;
794 if (mp == NULL)
795 return (EBADRPC);
796 mbufcp = mtod(mp, caddr_t);
797 len = mp->m_len;
798 }
799 xfer = (left > len) ? len : left;
800#ifdef notdef
801 /* Not Yet.. */
802 if (uiop->uio_iov->iov_op != NULL)
803 (*(uiop->uio_iov->iov_op))
804 (mbufcp, uiocp, xfer);
805 else
806#endif
807 if (uiop->uio_segflg == UIO_SYSSPACE)
808 bcopy(mbufcp, uiocp, xfer);
809 else
810 copyout(mbufcp, uiocp, xfer);
811 left -= xfer;
812 len -= xfer;
813 mbufcp += xfer;
814 uiocp += xfer;
815 uiop->uio_offset += xfer;
816 uiop->uio_resid -= xfer;
817 }
818 if (uiop->uio_iov->iov_len <= siz) {
819 uiop->uio_iovcnt--;
820 uiop->uio_iov++;
821 } else {
822 uiop->uio_iov->iov_base += uiosiz;
823 uiop->uio_iov->iov_len -= uiosiz;
824 }
825 siz -= uiosiz;
826 }
827 *dpos = mbufcp;
828 *mrep = mp;
829 if (rem > 0) {
830 if (len < rem)
831 error = nfs_adv(mrep, dpos, rem, len);
832 else
833 *dpos += rem;
834 }
835 return (error);
836}
837
838/*
839 * copies a uio scatter/gather list to an mbuf chain.
840 * NOTE: can ony handle iovcnt == 1
841 */
842int
843nfsm_uiotombuf(struct uio *uiop, struct mbuf **mq, int siz, caddr_t *bpos)
844{
845 char *uiocp;
846 struct mbuf *mp, *mp2;
847 int xfer, left, mlen;
848 int uiosiz, clflg, rem;
849 char *cp;
850
851#ifdef DIAGNOSTIC
852 if (uiop->uio_iovcnt != 1)
853 panic("nfsm_uiotombuf: iovcnt != 1");
854#endif
855
856 if (siz > MLEN) /* or should it >= MCLBYTES ?? */
857 clflg = 1;
858 else
859 clflg = 0;
860 rem = nfsm_rndup(siz)-siz;
861 mp = mp2 = *mq;
862 while (siz > 0) {
863 left = uiop->uio_iov->iov_len;
864 uiocp = uiop->uio_iov->iov_base;
865 if (left > siz)
866 left = siz;
867 uiosiz = left;
868 while (left > 0) {
869 mlen = M_TRAILINGSPACE(mp);
870 if (mlen == 0) {
871 MGET(mp, MB_WAIT, MT_DATA);
872 if (clflg)
873 MCLGET(mp, MB_WAIT);
874 mp->m_len = 0;
875 mp2->m_next = mp;
876 mp2 = mp;
877 mlen = M_TRAILINGSPACE(mp);
878 }
879 xfer = (left > mlen) ? mlen : left;
880#ifdef notdef
881 /* Not Yet.. */
882 if (uiop->uio_iov->iov_op != NULL)
883 (*(uiop->uio_iov->iov_op))
884 (uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
885 else
886#endif
887 if (uiop->uio_segflg == UIO_SYSSPACE)
888 bcopy(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
889 else
890 copyin(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
891 mp->m_len += xfer;
892 left -= xfer;
893 uiocp += xfer;
894 uiop->uio_offset += xfer;
895 uiop->uio_resid -= xfer;
896 }
897 uiop->uio_iov->iov_base += uiosiz;
898 uiop->uio_iov->iov_len -= uiosiz;
899 siz -= uiosiz;
900 }
901 if (rem > 0) {
902 if (rem > M_TRAILINGSPACE(mp)) {
903 MGET(mp, MB_WAIT, MT_DATA);
904 mp->m_len = 0;
905 mp2->m_next = mp;
906 }
907 cp = mtod(mp, caddr_t)+mp->m_len;
908 for (left = 0; left < rem; left++)
909 *cp++ = '\0';
910 mp->m_len += rem;
911 *bpos = cp;
912 } else
913 *bpos = mtod(mp, caddr_t)+mp->m_len;
914 *mq = mp;
915 return (0);
916}
917
918/*
919 * Help break down an mbuf chain by setting the first siz bytes contiguous
920 * pointed to by returned val.
921 * This is used by the macros nfsm_dissect and nfsm_dissecton for tough
922 * cases. (The macros use the vars. dpos and dpos2)
923 */
924int
925nfsm_disct(struct mbuf **mdp, caddr_t *dposp, int siz, int left, caddr_t *cp2)
926{
927 struct mbuf *mp, *mp2;
928 int siz2, xfer;
929 caddr_t p;
930
931 mp = *mdp;
932 while (left == 0) {
933 *mdp = mp = mp->m_next;
934 if (mp == NULL)
935 return (EBADRPC);
936 left = mp->m_len;
937 *dposp = mtod(mp, caddr_t);
938 }
939 if (left >= siz) {
940 *cp2 = *dposp;
941 *dposp += siz;
942 } else if (mp->m_next == NULL) {
943 return (EBADRPC);
944 } else if (siz > MHLEN) {
945 panic("nfs S too big");
946 } else {
947 MGET(mp2, MB_WAIT, MT_DATA);
948 mp2->m_next = mp->m_next;
949 mp->m_next = mp2;
950 mp->m_len -= left;
951 mp = mp2;
952 *cp2 = p = mtod(mp, caddr_t);
953 bcopy(*dposp, p, left); /* Copy what was left */
954 siz2 = siz-left;
955 p += left;
956 mp2 = mp->m_next;
957 /* Loop around copying up the siz2 bytes */
958 while (siz2 > 0) {
959 if (mp2 == NULL)
960 return (EBADRPC);
961 xfer = (siz2 > mp2->m_len) ? mp2->m_len : siz2;
962 if (xfer > 0) {
963 bcopy(mtod(mp2, caddr_t), p, xfer);
964 NFSMADV(mp2, xfer);
965 mp2->m_len -= xfer;
966 p += xfer;
967 siz2 -= xfer;
968 }
969 if (siz2 > 0)
970 mp2 = mp2->m_next;
971 }
972 mp->m_len = siz;
973 *mdp = mp2;
974 *dposp = mtod(mp2, caddr_t);
975 }
976 return (0);
977}
978
979/*
980 * Advance the position in the mbuf chain.
981 */
982int
983nfs_adv(struct mbuf **mdp, caddr_t *dposp, int offs, int left)
984{
985 struct mbuf *m;
986 int s;
987
988 m = *mdp;
989 s = left;
990 while (s < offs) {
991 offs -= s;
992 m = m->m_next;
993 if (m == NULL)
994 return (EBADRPC);
995 s = m->m_len;
996 }
997 *mdp = m;
998 *dposp = mtod(m, caddr_t)+offs;
999 return (0);
1000}
1001
1002/*
1003 * Copy a string into mbufs for the hard cases...
1004 */
1005int
1006nfsm_strtmbuf(struct mbuf **mb, char **bpos, const char *cp, long siz)
1007{
1008 struct mbuf *m1 = NULL, *m2;
1009 long left, xfer, len, tlen;
1010 u_int32_t *tl;
1011 int putsize;
1012
1013 putsize = 1;
1014 m2 = *mb;
1015 left = M_TRAILINGSPACE(m2);
1016 if (left > 0) {
1017 tl = ((u_int32_t *)(*bpos));
1018 *tl++ = txdr_unsigned(siz);
1019 putsize = 0;
1020 left -= NFSX_UNSIGNED;
1021 m2->m_len += NFSX_UNSIGNED;
1022 if (left > 0) {
1023 bcopy(cp, (caddr_t) tl, left);
1024 siz -= left;
1025 cp += left;
1026 m2->m_len += left;
1027 left = 0;
1028 }
1029 }
1030 /* Loop around adding mbufs */
1031 while (siz > 0) {
1032 MGET(m1, MB_WAIT, MT_DATA);
1033 if (siz > MLEN)
1034 MCLGET(m1, MB_WAIT);
1035 m1->m_len = NFSMSIZ(m1);
1036 m2->m_next = m1;
1037 m2 = m1;
1038 tl = mtod(m1, u_int32_t *);
1039 tlen = 0;
1040 if (putsize) {
1041 *tl++ = txdr_unsigned(siz);
1042 m1->m_len -= NFSX_UNSIGNED;
1043 tlen = NFSX_UNSIGNED;
1044 putsize = 0;
1045 }
1046 if (siz < m1->m_len) {
1047 len = nfsm_rndup(siz);
1048 xfer = siz;
1049 if (xfer < len)
1050 *(tl+(xfer>>2)) = 0;
1051 } else {
1052 xfer = len = m1->m_len;
1053 }
1054 bcopy(cp, (caddr_t) tl, xfer);
1055 m1->m_len = len+tlen;
1056 siz -= xfer;
1057 cp += xfer;
1058 }
1059 *mb = m1;
1060 *bpos = mtod(m1, caddr_t)+m1->m_len;
1061 return (0);
1062}
1063
1064/*
1065 * Called once to initialize data structures...
1066 */
1067int
1068nfs_init(struct vfsconf *vfsp)
1069{
1070 int i;
1071
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++) {
1095 nfs_iodwant[i] = NULL;
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
1127 nfs_prev_vop_lease_check = default_vnode_vops->vop_lease;
1128 default_vnode_vops->vop_lease = nqnfs_vop_lease_check;
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
1143nfs_uninit(struct vfsconf *vfsp)
1144{
1145
1146 untimeout(nfs_timer, (void *)NULL, nfs_timer_handle);
1147 nfs_mount_type = -1;
1148#ifndef NFS_NOSERVER
1149 default_vnode_vops->vop_lease = nfs_prev_vop_lease_check;
1150#endif
1151 lease_updatetime = nfs_prev_lease_updatetime;
1152 sysent[SYS_nfssvc].sy_narg = nfs_prev_nfssvc_sy_narg;
1153 sysent[SYS_nfssvc].sy_call = nfs_prev_nfssvc_sy_call;
1154 return (0);
1155}
1156
1157/*
1158 * Attribute cache routines.
1159 * nfs_loadattrcache() - loads or updates the cache contents from attributes
1160 * that are on the mbuf list
1161 * nfs_getattrcache() - returns valid attributes if found in cache, returns
1162 * error otherwise
1163 */
1164
1165/*
1166 * Load the attribute cache (that lives in the nfsnode entry) with
1167 * the values on the mbuf list and
1168 * Iff vap not NULL
1169 * copy the attributes to *vaper
1170 */
1171int
1172nfs_loadattrcache(struct vnode **vpp, struct mbuf **mdp, caddr_t *dposp,
1173 struct vattr *vaper, int dontshrink)
1174{
1175 struct vnode *vp = *vpp;
1176 struct vattr *vap;
1177 struct nfs_fattr *fp;
1178 struct nfsnode *np;
1179 int32_t t1;
1180 caddr_t cp2;
1181 int error = 0;
1182 udev_t rdev;
1183 struct mbuf *md;
1184 enum vtype vtyp;
1185 u_short vmode;
1186 struct timespec mtime;
1187 int v3 = NFS_ISV3(vp);
1188
1189 md = *mdp;
1190 t1 = (mtod(md, caddr_t) + md->m_len) - *dposp;
1191 if ((error = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, &cp2)) != 0)
1192 return (error);
1193 fp = (struct nfs_fattr *)cp2;
1194 if (v3) {
1195 vtyp = nfsv3tov_type(fp->fa_type);
1196 vmode = fxdr_unsigned(u_short, fp->fa_mode);
1197 rdev = makeudev(fxdr_unsigned(int, fp->fa3_rdev.specdata1),
1198 fxdr_unsigned(int, fp->fa3_rdev.specdata2));
1199 fxdr_nfsv3time(&fp->fa3_mtime, &mtime);
1200 } else {
1201 vtyp = nfsv2tov_type(fp->fa_type);
1202 vmode = fxdr_unsigned(u_short, fp->fa_mode);
1203 /*
1204 * XXX
1205 *
1206 * The duplicate information returned in fa_type and fa_mode
1207 * is an ambiguity in the NFS version 2 protocol.
1208 *
1209 * VREG should be taken literally as a regular file. If a
1210 * server intents to return some type information differently
1211 * in the upper bits of the mode field (e.g. for sockets, or
1212 * FIFOs), NFSv2 mandates fa_type to be VNON. Anyway, we
1213 * leave the examination of the mode bits even in the VREG
1214 * case to avoid breakage for bogus servers, but we make sure
1215 * that there are actually type bits set in the upper part of
1216 * fa_mode (and failing that, trust the va_type field).
1217 *
1218 * NFSv3 cleared the issue, and requires fa_mode to not
1219 * contain any type information (while also introduing sockets
1220 * and FIFOs for fa_type).
1221 */
1222 if (vtyp == VNON || (vtyp == VREG && (vmode & S_IFMT) != 0))
1223 vtyp = IFTOVT(vmode);
1224 rdev = fxdr_unsigned(int32_t, fp->fa2_rdev);
1225 fxdr_nfsv2time(&fp->fa2_mtime, &mtime);
1226
1227 /*
1228 * Really ugly NFSv2 kludge.
1229 */
1230 if (vtyp == VCHR && rdev == (udev_t)0xffffffff)
1231 vtyp = VFIFO;
1232 }
1233
1234 /*
1235 * If v_type == VNON it is a new node, so fill in the v_type,
1236 * n_mtime fields. Check to see if it represents a special
1237 * device, and if so, check for a possible alias. Once the
1238 * correct vnode has been obtained, fill in the rest of the
1239 * information.
1240 */
1241 np = VTONFS(vp);
1242 if (vp->v_type != vtyp) {
1243 vp->v_type = vtyp;
1244 if (vp->v_type == VFIFO) {
1245 vp->v_ops = vp->v_mount->mnt_vn_fifo_ops;
1246 } else if (vp->v_type == VCHR || vp->v_type == VBLK) {
1247 vp->v_ops = vp->v_mount->mnt_vn_spec_ops;
1248 addaliasu(vp, rdev);
1249 } else {
1250 vp->v_ops = vp->v_mount->mnt_vn_ops;
1251 }
1252 np->n_mtime = mtime.tv_sec;
1253 }
1254 vap = &np->n_vattr;
1255 vap->va_type = vtyp;
1256 vap->va_mode = (vmode & 07777);
1257 vap->va_rdev = rdev;
1258 vap->va_mtime = mtime;
1259 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
1260 if (v3) {
1261 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1262 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
1263 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
1264 vap->va_size = fxdr_hyper(&fp->fa3_size);
1265 vap->va_blocksize = NFS_FABLKSIZE;
1266 vap->va_bytes = fxdr_hyper(&fp->fa3_used);
1267 vap->va_fileid = fxdr_unsigned(int32_t,
1268 fp->fa3_fileid.nfsuquad[1]);
1269 fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime);
1270 fxdr_nfsv3time(&fp->fa3_ctime, &vap->va_ctime);
1271 vap->va_flags = 0;
1272 vap->va_filerev = 0;
1273 } else {
1274 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1275 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
1276 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
1277 vap->va_size = fxdr_unsigned(u_int32_t, fp->fa2_size);
1278 vap->va_blocksize = fxdr_unsigned(int32_t, fp->fa2_blocksize);
1279 vap->va_bytes = (u_quad_t)fxdr_unsigned(int32_t, fp->fa2_blocks)
1280 * NFS_FABLKSIZE;
1281 vap->va_fileid = fxdr_unsigned(int32_t, fp->fa2_fileid);
1282 fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime);
1283 vap->va_flags = 0;
1284 vap->va_ctime.tv_sec = fxdr_unsigned(u_int32_t,
1285 fp->fa2_ctime.nfsv2_sec);
1286 vap->va_ctime.tv_nsec = 0;
1287 vap->va_gen = fxdr_unsigned(u_int32_t,fp->fa2_ctime.nfsv2_usec);
1288 vap->va_filerev = 0;
1289 }
1290 np->n_attrstamp = time_second;
1291 if (vap->va_size != np->n_size) {
1292 if (vap->va_type == VREG) {
1293 if (dontshrink && vap->va_size < np->n_size) {
1294 /*
1295 * We've been told not to shrink the file;
1296 * zero np->n_attrstamp to indicate that
1297 * the attributes are stale.
1298 */
1299 vap->va_size = np->n_size;
1300 np->n_attrstamp = 0;
1301 } else if (np->n_flag & NMODIFIED) {
1302 /*
1303 * We've modified the file: Use the larger
1304 * of our size, and the server's size.
1305 */
1306 if (vap->va_size < np->n_size) {
1307 vap->va_size = np->n_size;
1308 } else {
1309 np->n_size = vap->va_size;
1310 np->n_flag |= NSIZECHANGED;
1311 }
1312 } else {
1313 np->n_size = vap->va_size;
1314 np->n_flag |= NSIZECHANGED;
1315 }
1316 vnode_pager_setsize(vp, np->n_size);
1317 } else {
1318 np->n_size = vap->va_size;
1319 }
1320 }
1321 if (vaper != NULL) {
1322 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap));
1323 if (np->n_flag & NCHG) {
1324 if (np->n_flag & NACC)
1325 vaper->va_atime = np->n_atim;
1326 if (np->n_flag & NUPD)
1327 vaper->va_mtime = np->n_mtim;
1328 }
1329 }
1330 return (0);
1331}
1332
1333#ifdef NFS_ACDEBUG
1334#include <sys/sysctl.h>
1335SYSCTL_DECL(_vfs_nfs);
1336static int nfs_acdebug;
1337SYSCTL_INT(_vfs_nfs, OID_AUTO, acdebug, CTLFLAG_RW, &nfs_acdebug, 0, "");
1338#endif
1339
1340/*
1341 * Check the time stamp
1342 * If the cache is valid, copy contents to *vap and return 0
1343 * otherwise return an error
1344 */
1345int
1346nfs_getattrcache(struct vnode *vp, struct vattr *vaper)
1347{
1348 struct nfsnode *np;
1349 struct vattr *vap;
1350 struct nfsmount *nmp;
1351 int timeo;
1352
1353 np = VTONFS(vp);
1354 vap = &np->n_vattr;
1355 nmp = VFSTONFS(vp->v_mount);
1356 /* XXX n_mtime doesn't seem to be updated on a miss-and-reload */
1357 timeo = (time_second - np->n_mtime) / 10;
1358
1359#ifdef NFS_ACDEBUG
1360 if (nfs_acdebug>1)
1361 printf("nfs_getattrcache: initial timeo = %d\n", timeo);
1362#endif
1363
1364 if (vap->va_type == VDIR) {
1365 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acdirmin)
1366 timeo = nmp->nm_acdirmin;
1367 else if (timeo > nmp->nm_acdirmax)
1368 timeo = nmp->nm_acdirmax;
1369 } else {
1370 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acregmin)
1371 timeo = nmp->nm_acregmin;
1372 else if (timeo > nmp->nm_acregmax)
1373 timeo = nmp->nm_acregmax;
1374 }
1375
1376#ifdef NFS_ACDEBUG
1377 if (nfs_acdebug > 2)
1378 printf("acregmin %d; acregmax %d; acdirmin %d; acdirmax %d\n",
1379 nmp->nm_acregmin, nmp->nm_acregmax,
1380 nmp->nm_acdirmin, nmp->nm_acdirmax);
1381
1382 if (nfs_acdebug)
1383 printf("nfs_getattrcache: age = %d; final timeo = %d\n",
1384 (time_second - np->n_attrstamp), timeo);
1385#endif
1386
1387 if ((time_second - np->n_attrstamp) >= timeo) {
1388 nfsstats.attrcache_misses++;
1389 return (ENOENT);
1390 }
1391 nfsstats.attrcache_hits++;
1392 if (vap->va_size != np->n_size) {
1393 if (vap->va_type == VREG) {
1394 if (np->n_flag & NMODIFIED) {
1395 if (vap->va_size < np->n_size)
1396 vap->va_size = np->n_size;
1397 else
1398 np->n_size = vap->va_size;
1399 } else {
1400 np->n_size = vap->va_size;
1401 }
1402 vnode_pager_setsize(vp, np->n_size);
1403 } else {
1404 np->n_size = vap->va_size;
1405 }
1406 }
1407 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr));
1408 if (np->n_flag & NCHG) {
1409 if (np->n_flag & NACC)
1410 vaper->va_atime = np->n_atim;
1411 if (np->n_flag & NUPD)
1412 vaper->va_mtime = np->n_mtim;
1413 }
1414 return (0);
1415}
1416
1417#ifndef NFS_NOSERVER
1418/*
1419 * Set up nameidata for a lookup() call and do it.
1420 *
1421 * If pubflag is set, this call is done for a lookup operation on the
1422 * public filehandle. In that case we allow crossing mountpoints and
1423 * absolute pathnames. However, the caller is expected to check that
1424 * the lookup result is within the public fs, and deny access if
1425 * it is not.
1426 *
1427 * nfs_namei() clears out garbage fields that namei() might leave garbage.
1428 * This is mainly ni_vp and ni_dvp when an error occurs, and ni_dvp when no
1429 * error occurs but the parent was not requested.
1430 *
1431 * dirp may be set whether an error is returned or not, and must be
1432 * released by the caller.
1433 */
1434int
1435nfs_namei(struct nameidata *ndp, fhandle_t *fhp, int len,
1436 struct nfssvc_sock *slp, struct sockaddr *nam, struct mbuf **mdp,
1437 caddr_t *dposp, struct vnode **retdirp, struct thread *td,
1438 int kerbflag, int pubflag)
1439{
1440 int i, rem;
1441 struct mbuf *md;
1442 char *fromcp, *tocp, *cp;
1443 struct iovec aiov;
1444 struct uio auio;
1445 struct vnode *dp;
1446 int error, rdonly, linklen;
1447 struct componentname *cnp = &ndp->ni_cnd;
1448
1449 *retdirp = (struct vnode *)0;
1450 cnp->cn_pnbuf = zalloc(namei_zone);
1451
1452 /*
1453 * Copy the name from the mbuf list to ndp->ni_pnbuf
1454 * and set the various ndp fields appropriately.
1455 */
1456 fromcp = *dposp;
1457 tocp = cnp->cn_pnbuf;
1458 md = *mdp;
1459 rem = mtod(md, caddr_t) + md->m_len - fromcp;
1460 for (i = 0; i < len; i++) {
1461 while (rem == 0) {
1462 md = md->m_next;
1463 if (md == NULL) {
1464 error = EBADRPC;
1465 goto out;
1466 }
1467 fromcp = mtod(md, caddr_t);
1468 rem = md->m_len;
1469 }
1470 if (*fromcp == '\0' || (!pubflag && *fromcp == '/')) {
1471 error = EACCES;
1472 goto out;
1473 }
1474 *tocp++ = *fromcp++;
1475 rem--;
1476 }
1477 *tocp = '\0';
1478 *mdp = md;
1479 *dposp = fromcp;
1480 len = nfsm_rndup(len)-len;
1481 if (len > 0) {
1482 if (rem >= len)
1483 *dposp += len;
1484 else if ((error = nfs_adv(mdp, dposp, len, rem)) != 0)
1485 goto out;
1486 }
1487
1488 /*
1489 * Extract and set starting directory.
1490 */
1491 error = nfsrv_fhtovp(fhp, FALSE, &dp, ndp->ni_cnd.cn_cred, slp,
1492 nam, &rdonly, kerbflag, pubflag);
1493 if (error)
1494 goto out;
1495 if (dp->v_type != VDIR) {
1496 vrele(dp);
1497 error = ENOTDIR;
1498 goto out;
1499 }
1500
1501 if (rdonly)
1502 cnp->cn_flags |= CNP_RDONLY;
1503
1504 /*
1505 * Set return directory. Reference to dp is implicitly transfered
1506 * to the returned pointer
1507 */
1508 *retdirp = dp;
1509
1510 if (pubflag) {
1511 /*
1512 * Oh joy. For WebNFS, handle those pesky '%' escapes,
1513 * and the 'native path' indicator.
1514 */
1515 cp = zalloc(namei_zone);
1516 fromcp = cnp->cn_pnbuf;
1517 tocp = cp;
1518 if ((unsigned char)*fromcp >= WEBNFS_SPECCHAR_START) {
1519 switch ((unsigned char)*fromcp) {
1520 case WEBNFS_NATIVE_CHAR:
1521 /*
1522 * 'Native' path for us is the same
1523 * as a path according to the NFS spec,
1524 * just skip the escape char.
1525 */
1526 fromcp++;
1527 break;
1528 /*
1529 * More may be added in the future, range 0x80-0xff
1530 */
1531 default:
1532 error = EIO;
1533 zfree(namei_zone, cp);
1534 goto out;
1535 }
1536 }
1537 /*
1538 * Translate the '%' escapes, URL-style.
1539 */
1540 while (*fromcp != '\0') {
1541 if (*fromcp == WEBNFS_ESC_CHAR) {
1542 if (fromcp[1] != '\0' && fromcp[2] != '\0') {
1543 fromcp++;
1544 *tocp++ = HEXSTRTOI(fromcp);
1545 fromcp += 2;
1546 continue;
1547 } else {
1548 error = ENOENT;
1549 zfree(namei_zone, cp);
1550 goto out;
1551 }
1552 } else
1553 *tocp++ = *fromcp++;
1554 }
1555 *tocp = '\0';
1556 zfree(namei_zone, cnp->cn_pnbuf);
1557 cnp->cn_pnbuf = cp;
1558 }
1559
1560 ndp->ni_pathlen = (tocp - cnp->cn_pnbuf) + 1;
1561 ndp->ni_segflg = UIO_SYSSPACE;
1562
1563 if (pubflag) {
1564 ndp->ni_rootdir = rootvnode;
1565 ndp->ni_loopcnt = 0;
1566 if (cnp->cn_pnbuf[0] == '/')
1567 dp = rootvnode;
1568 } else {
1569 cnp->cn_flags |= CNP_NOCROSSMOUNT;
1570 }
1571
1572 /*
1573 * Initialize for scan, set ni_startdir and bump ref on dp again
1574 * because lookup() will dereference ni_startdir.
1575 */
1576
1577 cnp->cn_td = td;
1578 vref(dp);
1579 ndp->ni_startdir = dp;
1580
1581 for (;;) {
1582 cnp->cn_nameptr = cnp->cn_pnbuf;
1583 /*
1584 * Call lookup() to do the real work. If an error occurs,
1585 * ndp->ni_vp and ni_dvp are left uninitialized or NULL and
1586 * we do not have to dereference anything before returning.
1587 * In either case ni_startdir will be dereferenced and NULLed
1588 * out.
1589 */
1590 error = lookup(ndp);
1591 if (error)
1592 break;
1593
1594 /*
1595 * Check for encountering a symbolic link. Trivial
1596 * termination occurs if no symlink encountered.
1597 * Note: zfree is safe because error is 0, so we will
1598 * not zfree it again when we break.
1599 */
1600 if ((cnp->cn_flags & CNP_ISSYMLINK) == 0) {
1601 nfsrv_object_create(ndp->ni_vp);
1602 if (cnp->cn_flags & (CNP_SAVENAME | CNP_SAVESTART))
1603 cnp->cn_flags |= CNP_HASBUF;
1604 else
1605 zfree(namei_zone, cnp->cn_pnbuf);
1606 break;
1607 }
1608
1609 /*
1610 * Validate symlink
1611 */
1612 if ((cnp->cn_flags & CNP_LOCKPARENT) && ndp->ni_pathlen == 1)
1613 VOP_UNLOCK(ndp->ni_dvp, NULL, 0, td);
1614 if (!pubflag) {
1615 error = EINVAL;
1616 goto badlink2;
1617 }
1618
1619 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
1620 error = ELOOP;
1621 goto badlink2;
1622 }
1623 if (ndp->ni_pathlen > 1)
1624 cp = zalloc(namei_zone);
1625 else
1626 cp = cnp->cn_pnbuf;
1627 aiov.iov_base = cp;
1628 aiov.iov_len = MAXPATHLEN;
1629 auio.uio_iov = &aiov;
1630 auio.uio_iovcnt = 1;
1631 auio.uio_offset = 0;
1632 auio.uio_rw = UIO_READ;
1633 auio.uio_segflg = UIO_SYSSPACE;
1634 auio.uio_td = NULL;
1635 auio.uio_resid = MAXPATHLEN;
1636 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
1637 if (error) {
1638 badlink1:
1639 if (ndp->ni_pathlen > 1)
1640 zfree(namei_zone, cp);
1641 badlink2:
1642 vrele(ndp->ni_dvp);
1643 vput(ndp->ni_vp);
1644 break;
1645 }
1646 linklen = MAXPATHLEN - auio.uio_resid;
1647 if (linklen == 0) {
1648 error = ENOENT;
1649 goto badlink1;
1650 }
1651 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
1652 error = ENAMETOOLONG;
1653 goto badlink1;
1654 }
1655
1656 /*
1657 * Adjust or replace path
1658 */
1659 if (ndp->ni_pathlen > 1) {
1660 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
1661 zfree(namei_zone, cnp->cn_pnbuf);
1662 cnp->cn_pnbuf = cp;
1663 } else
1664 cnp->cn_pnbuf[linklen] = '\0';
1665 ndp->ni_pathlen += linklen;
1666
1667 /*
1668 * Cleanup refs for next loop and check if root directory
1669 * should replace current directory. Normally ni_dvp
1670 * becomes the new base directory and is cleaned up when
1671 * we loop. Explicitly null pointers after invalidation
1672 * to clarify operation.
1673 */
1674 vput(ndp->ni_vp);
1675 ndp->ni_vp = NULL;
1676
1677 if (cnp->cn_pnbuf[0] == '/') {
1678 vrele(ndp->ni_dvp);
1679 ndp->ni_dvp = ndp->ni_rootdir;
1680 vref(ndp->ni_dvp);
1681 }
1682 ndp->ni_startdir = ndp->ni_dvp;
1683 ndp->ni_dvp = NULL;
1684 }
1685
1686 /*
1687 * nfs_namei() guarentees that fields will not contain garbage
1688 * whether an error occurs or not. This allows the caller to track
1689 * cleanup state trivially.
1690 */
1691out:
1692 if (error) {
1693 zfree(namei_zone, cnp->cn_pnbuf);
1694 ndp->ni_vp = NULL;
1695 ndp->ni_dvp = NULL;
1696 ndp->ni_startdir = NULL;
1697 cnp->cn_flags &= ~CNP_HASBUF;
1698 } else if ((ndp->ni_cnd.cn_flags & (CNP_WANTPARENT|CNP_LOCKPARENT)) == 0) {
1699 ndp->ni_dvp = NULL;
1700 }
1701 return (error);
1702}
1703
1704/*
1705 * A fiddled version of m_adj() that ensures null fill to a long
1706 * boundary and only trims off the back end
1707 */
1708void
1709nfsm_adj(struct mbuf *mp, int len, int nul)
1710{
1711 struct mbuf *m;
1712 int count, i;
1713 char *cp;
1714
1715 /*
1716 * Trim from tail. Scan the mbuf chain,
1717 * calculating its length and finding the last mbuf.
1718 * If the adjustment only affects this mbuf, then just
1719 * adjust and return. Otherwise, rescan and truncate
1720 * after the remaining size.
1721 */
1722 count = 0;
1723 m = mp;
1724 for (;;) {
1725 count += m->m_len;
1726 if (m->m_next == (struct mbuf *)0)
1727 break;
1728 m = m->m_next;
1729 }
1730 if (m->m_len > len) {
1731 m->m_len -= len;
1732 if (nul > 0) {
1733 cp = mtod(m, caddr_t)+m->m_len-nul;
1734 for (i = 0; i < nul; i++)
1735 *cp++ = '\0';
1736 }
1737 return;
1738 }
1739 count -= len;
1740 if (count < 0)
1741 count = 0;
1742 /*
1743 * Correct length for chain is "count".
1744 * Find the mbuf with last data, adjust its length,
1745 * and toss data from remaining mbufs on chain.
1746 */
1747 for (m = mp; m; m = m->m_next) {
1748 if (m->m_len >= count) {
1749 m->m_len = count;
1750 if (nul > 0) {
1751 cp = mtod(m, caddr_t)+m->m_len-nul;
1752 for (i = 0; i < nul; i++)
1753 *cp++ = '\0';
1754 }
1755 break;
1756 }
1757 count -= m->m_len;
1758 }
1759 for (m = m->m_next;m;m = m->m_next)
1760 m->m_len = 0;
1761}
1762
1763/*
1764 * Make these functions instead of macros, so that the kernel text size
1765 * doesn't get too big...
1766 */
1767void
1768nfsm_srvwcc(struct nfsrv_descript *nfsd, int before_ret,
1769 struct vattr *before_vap, int after_ret, struct vattr *after_vap,
1770 struct mbuf **mbp, char **bposp)
1771{
1772 struct mbuf *mb = *mbp, *mb2;
1773 char *bpos = *bposp;
1774 u_int32_t *tl;
1775
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
1794nfsm_srvpostopattr(struct nfsrv_descript *nfsd, int after_ret,
1795 struct vattr *after_vap, struct mbuf **mbp, char **bposp)
1796{
1797 struct mbuf *mb = *mbp, *mb2;
1798 char *bpos = *bposp;
1799 u_int32_t *tl;
1800 struct nfs_fattr *fp;
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
1816nfsm_srvfattr(struct nfsrv_descript *nfsd, struct vattr *vap,
1817 struct nfs_fattr *fp)
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
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)
1866{
1867 struct thread *td = curthread; /* XXX */
1868 struct mount *mp;
1869 int i;
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)
1931 VOP_UNLOCK(*vpp, NULL, 0, td);
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
1942nfs_ispublicfh(fhandle_t *fhp)
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
1962netaddr_match(int family, union nethostaddr *haddr, struct sockaddr *nam)
1963{
1964 struct sockaddr_in *inetaddr;
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 *
1985nfs_getcookie(struct nfsnode *np, off_t off, int add)
1986{
1987 struct nfsdmap *dp, *dp2;
1988 int pos;
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
2040nfs_invaldir(struct vnode *vp)
2041{
2042 struct nfsnode *np = VTONFS(vp);
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 */
2065void
2066nfs_clearcommit(struct mount *mp)
2067{
2068 struct vnode *vp, *nvp;
2069 struct buf *bp, *nbp;
2070 lwkt_tokref ilock;
2071 int s;
2072
2073 lwkt_gettoken(&ilock, &mntvnode_token);
2074 s = splbio();
2075 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp; vp = nvp) {
2076 nvp = TAILQ_NEXT(vp, v_nmntvnodes); /* ZZZ */
2077 if (vp->v_flag & VPLACEMARKER)
2078 continue;
2079 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2080 nbp = TAILQ_NEXT(bp, b_vnbufs);
2081 if (BUF_REFCNT(bp) == 0 &&
2082 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2083 == (B_DELWRI | B_NEEDCOMMIT)) {
2084 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
2085 }
2086 }
2087 }
2088 splx(s);
2089 lwkt_reltoken(&ilock);
2090}
2091
2092#ifndef NFS_NOSERVER
2093/*
2094 * Map errnos to NFS error numbers. For Version 3 also filter out error
2095 * numbers not specified for the associated procedure.
2096 */
2097int
2098nfsrv_errmap(struct nfsrv_descript *nd, int err)
2099{
2100 short *defaulterrp, *errp;
2101
2102 if (nd->nd_flag & ND_NFSV3) {
2103 if (nd->nd_procnum <= NFSPROC_COMMIT) {
2104 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
2105 while (*++errp) {
2106 if (*errp == err)
2107 return (err);
2108 else if (*errp > err)
2109 break;
2110 }
2111 return ((int)*defaulterrp);
2112 } else
2113 return (err & 0xffff);
2114 }
2115 if (err <= ELAST)
2116 return ((int)nfsrv_v2errmap[err - 1]);
2117 return (NFSERR_IO);
2118}
2119
2120int
2121nfsrv_object_create(struct vnode *vp)
2122{
2123 struct thread *td = curthread;
2124
2125 if (vp == NULL || vp->v_type != VREG)
2126 return (1);
2127 return (vfs_object_create(vp, td));
2128}
2129
2130/*
2131 * Sort the group list in increasing numerical order.
2132 * (Insertion sort by Chris Torek, who was grossed out by the bubble sort
2133 * that used to be here.)
2134 */
2135void
2136nfsrvw_sort(gid_t *list, int num)
2137{
2138 int i, j;
2139 gid_t v;
2140
2141 /* Insertion sort. */
2142 for (i = 1; i < num; i++) {
2143 v = list[i];
2144 /* find correct slot for value v, moving others up */
2145 for (j = i; --j >= 0 && v < list[j];)
2146 list[j + 1] = list[j];
2147 list[j + 1] = v;
2148 }
2149}
2150
2151/*
2152 * copy credentials making sure that the result can be compared with bcmp().
2153 */
2154void
2155nfsrv_setcred(struct ucred *incred, struct ucred *outcred)
2156{
2157 int i;
2158
2159 bzero((caddr_t)outcred, sizeof (struct ucred));
2160 outcred->cr_ref = 1;
2161 outcred->cr_uid = incred->cr_uid;
2162 outcred->cr_ngroups = incred->cr_ngroups;
2163 for (i = 0; i < incred->cr_ngroups; i++)
2164 outcred->cr_groups[i] = incred->cr_groups[i];
2165 nfsrvw_sort(outcred->cr_groups, outcred->cr_ngroups);
2166}
2167#endif /* NFS_NOSERVER */