Device layer rollup commit.
[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.17 2004/05/19 22:53:05 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 vop_t *nfs_prev_vop_lease_check;
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, M_WAIT, MT_DATA);
591 if (hsiz >= MINCLSIZE)
592 MCLGET(mb, M_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, M_WAIT, MT_DATA);
639 if ((authsiz + 10 * NFSX_UNSIGNED) >= MINCLSIZE) {
640 MCLGET(mb, M_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, M_WAIT, MT_DATA);
704 if (siz >= MINCLSIZE)
705 MCLGET(mb2, M_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, M_WAIT, MT_DATA);
737 if (siz >= MINCLSIZE)
738 MCLGET(mb2, M_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, M_WAIT, MT_DATA);
872 if (clflg)
873 MCLGET(mp, M_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, M_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, M_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, M_WAIT, MT_DATA);
1033 if (siz > MLEN)
1034 MCLGET(m1, M_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_vnodeop_p[VOFFSET(vop_lease)];
1128 default_vnodeop_p[VOFFSET(vop_lease)] = (vop_t *)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_vnodeop_p[VOFFSET(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_op = fifo_nfsv2nodeop_p;
1246 }
1247 if (vp->v_type == VCHR || vp->v_type == VBLK) {
1248 vp->v_op = spec_nfsv2nodeop_p;
1249 addaliasu(vp, rdev);
1250 }
1251 np->n_mtime = mtime.tv_sec;
1252 }
1253 vap = &np->n_vattr;
1254 vap->va_type = vtyp;
1255 vap->va_mode = (vmode & 07777);
1256 vap->va_rdev = rdev;
1257 vap->va_mtime = mtime;
1258 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
1259 if (v3) {
1260 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1261 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
1262 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
1263 vap->va_size = fxdr_hyper(&fp->fa3_size);
1264 vap->va_blocksize = NFS_FABLKSIZE;
1265 vap->va_bytes = fxdr_hyper(&fp->fa3_used);
1266 vap->va_fileid = fxdr_unsigned(int32_t,
1267 fp->fa3_fileid.nfsuquad[1]);
1268 fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime);
1269 fxdr_nfsv3time(&fp->fa3_ctime, &vap->va_ctime);
1270 vap->va_flags = 0;
1271 vap->va_filerev = 0;
1272 } else {
1273 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1274 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
1275 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
1276 vap->va_size = fxdr_unsigned(u_int32_t, fp->fa2_size);
1277 vap->va_blocksize = fxdr_unsigned(int32_t, fp->fa2_blocksize);
1278 vap->va_bytes = (u_quad_t)fxdr_unsigned(int32_t, fp->fa2_blocks)
1279 * NFS_FABLKSIZE;
1280 vap->va_fileid = fxdr_unsigned(int32_t, fp->fa2_fileid);
1281 fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime);
1282 vap->va_flags = 0;
1283 vap->va_ctime.tv_sec = fxdr_unsigned(u_int32_t,
1284 fp->fa2_ctime.nfsv2_sec);
1285 vap->va_ctime.tv_nsec = 0;
1286 vap->va_gen = fxdr_unsigned(u_int32_t,fp->fa2_ctime.nfsv2_usec);
1287 vap->va_filerev = 0;
1288 }
1289 np->n_attrstamp = time_second;
1290 if (vap->va_size != np->n_size) {
1291 if (vap->va_type == VREG) {
1292 if (dontshrink && vap->va_size < np->n_size) {
1293 /*
1294 * We've been told not to shrink the file;
1295 * zero np->n_attrstamp to indicate that
1296 * the attributes are stale.
1297 */
1298 vap->va_size = np->n_size;
1299 np->n_attrstamp = 0;
1300 } else if (np->n_flag & NMODIFIED) {
1301 /*
1302 * We've modified the file: Use the larger
1303 * of our size, and the server's size.
1304 */
1305 if (vap->va_size < np->n_size) {
1306 vap->va_size = np->n_size;
1307 } else {
1308 np->n_size = vap->va_size;
1309 np->n_flag |= NSIZECHANGED;
1310 }
1311 } else {
1312 np->n_size = vap->va_size;
1313 np->n_flag |= NSIZECHANGED;
1314 }
1315 vnode_pager_setsize(vp, np->n_size);
1316 } else {
1317 np->n_size = vap->va_size;
1318 }
1319 }
1320 if (vaper != NULL) {
1321 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap));
1322 if (np->n_flag & NCHG) {
1323 if (np->n_flag & NACC)
1324 vaper->va_atime = np->n_atim;
1325 if (np->n_flag & NUPD)
1326 vaper->va_mtime = np->n_mtim;
1327 }
1328 }
1329 return (0);
1330}
1331
1332#ifdef NFS_ACDEBUG
1333#include <sys/sysctl.h>
1334SYSCTL_DECL(_vfs_nfs);
1335static int nfs_acdebug;
1336SYSCTL_INT(_vfs_nfs, OID_AUTO, acdebug, CTLFLAG_RW, &nfs_acdebug, 0, "");
1337#endif
1338
1339/*
1340 * Check the time stamp
1341 * If the cache is valid, copy contents to *vap and return 0
1342 * otherwise return an error
1343 */
1344int
1345nfs_getattrcache(struct vnode *vp, struct vattr *vaper)
1346{
1347 struct nfsnode *np;
1348 struct vattr *vap;
1349 struct nfsmount *nmp;
1350 int timeo;
1351
1352 np = VTONFS(vp);
1353 vap = &np->n_vattr;
1354 nmp = VFSTONFS(vp->v_mount);
1355 /* XXX n_mtime doesn't seem to be updated on a miss-and-reload */
1356 timeo = (time_second - np->n_mtime) / 10;
1357
1358#ifdef NFS_ACDEBUG
1359 if (nfs_acdebug>1)
1360 printf("nfs_getattrcache: initial timeo = %d\n", timeo);
1361#endif
1362
1363 if (vap->va_type == VDIR) {
1364 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acdirmin)
1365 timeo = nmp->nm_acdirmin;
1366 else if (timeo > nmp->nm_acdirmax)
1367 timeo = nmp->nm_acdirmax;
1368 } else {
1369 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acregmin)
1370 timeo = nmp->nm_acregmin;
1371 else if (timeo > nmp->nm_acregmax)
1372 timeo = nmp->nm_acregmax;
1373 }
1374
1375#ifdef NFS_ACDEBUG
1376 if (nfs_acdebug > 2)
1377 printf("acregmin %d; acregmax %d; acdirmin %d; acdirmax %d\n",
1378 nmp->nm_acregmin, nmp->nm_acregmax,
1379 nmp->nm_acdirmin, nmp->nm_acdirmax);
1380
1381 if (nfs_acdebug)
1382 printf("nfs_getattrcache: age = %d; final timeo = %d\n",
1383 (time_second - np->n_attrstamp), timeo);
1384#endif
1385
1386 if ((time_second - np->n_attrstamp) >= timeo) {
1387 nfsstats.attrcache_misses++;
1388 return (ENOENT);
1389 }
1390 nfsstats.attrcache_hits++;
1391 if (vap->va_size != np->n_size) {
1392 if (vap->va_type == VREG) {
1393 if (np->n_flag & NMODIFIED) {
1394 if (vap->va_size < np->n_size)
1395 vap->va_size = np->n_size;
1396 else
1397 np->n_size = vap->va_size;
1398 } else {
1399 np->n_size = vap->va_size;
1400 }
1401 vnode_pager_setsize(vp, np->n_size);
1402 } else {
1403 np->n_size = vap->va_size;
1404 }
1405 }
1406 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr));
1407 if (np->n_flag & NCHG) {
1408 if (np->n_flag & NACC)
1409 vaper->va_atime = np->n_atim;
1410 if (np->n_flag & NUPD)
1411 vaper->va_mtime = np->n_mtim;
1412 }
1413 return (0);
1414}
1415
1416#ifndef NFS_NOSERVER
1417/*
1418 * Set up nameidata for a lookup() call and do it.
1419 *
1420 * If pubflag is set, this call is done for a lookup operation on the
1421 * public filehandle. In that case we allow crossing mountpoints and
1422 * absolute pathnames. However, the caller is expected to check that
1423 * the lookup result is within the public fs, and deny access if
1424 * it is not.
1425 *
1426 * nfs_namei() clears out garbage fields that namei() might leave garbage.
1427 * This is mainly ni_vp and ni_dvp when an error occurs, and ni_dvp when no
1428 * error occurs but the parent was not requested.
1429 *
1430 * dirp may be set whether an error is returned or not, and must be
1431 * released by the caller.
1432 */
1433int
1434nfs_namei(struct nameidata *ndp, fhandle_t *fhp, int len,
1435 struct nfssvc_sock *slp, struct sockaddr *nam, struct mbuf **mdp,
1436 caddr_t *dposp, struct vnode **retdirp, struct thread *td,
1437 int kerbflag, int pubflag)
1438{
1439 int i, rem;
1440 struct mbuf *md;
1441 char *fromcp, *tocp, *cp;
1442 struct iovec aiov;
1443 struct uio auio;
1444 struct vnode *dp;
1445 int error, rdonly, linklen;
1446 struct componentname *cnp = &ndp->ni_cnd;
1447
1448 *retdirp = (struct vnode *)0;
1449 cnp->cn_pnbuf = zalloc(namei_zone);
1450
1451 /*
1452 * Copy the name from the mbuf list to ndp->ni_pnbuf
1453 * and set the various ndp fields appropriately.
1454 */
1455 fromcp = *dposp;
1456 tocp = cnp->cn_pnbuf;
1457 md = *mdp;
1458 rem = mtod(md, caddr_t) + md->m_len - fromcp;
1459 for (i = 0; i < len; i++) {
1460 while (rem == 0) {
1461 md = md->m_next;
1462 if (md == NULL) {
1463 error = EBADRPC;
1464 goto out;
1465 }
1466 fromcp = mtod(md, caddr_t);
1467 rem = md->m_len;
1468 }
1469 if (*fromcp == '\0' || (!pubflag && *fromcp == '/')) {
1470 error = EACCES;
1471 goto out;
1472 }
1473 *tocp++ = *fromcp++;
1474 rem--;
1475 }
1476 *tocp = '\0';
1477 *mdp = md;
1478 *dposp = fromcp;
1479 len = nfsm_rndup(len)-len;
1480 if (len > 0) {
1481 if (rem >= len)
1482 *dposp += len;
1483 else if ((error = nfs_adv(mdp, dposp, len, rem)) != 0)
1484 goto out;
1485 }
1486
1487 /*
1488 * Extract and set starting directory.
1489 */
1490 error = nfsrv_fhtovp(fhp, FALSE, &dp, ndp->ni_cnd.cn_cred, slp,
1491 nam, &rdonly, kerbflag, pubflag);
1492 if (error)
1493 goto out;
1494 if (dp->v_type != VDIR) {
1495 vrele(dp);
1496 error = ENOTDIR;
1497 goto out;
1498 }
1499
1500 if (rdonly)
1501 cnp->cn_flags |= CNP_RDONLY;
1502
1503 /*
1504 * Set return directory. Reference to dp is implicitly transfered
1505 * to the returned pointer
1506 */
1507 *retdirp = dp;
1508
1509 if (pubflag) {
1510 /*
1511 * Oh joy. For WebNFS, handle those pesky '%' escapes,
1512 * and the 'native path' indicator.
1513 */
1514 cp = zalloc(namei_zone);
1515 fromcp = cnp->cn_pnbuf;
1516 tocp = cp;
1517 if ((unsigned char)*fromcp >= WEBNFS_SPECCHAR_START) {
1518 switch ((unsigned char)*fromcp) {
1519 case WEBNFS_NATIVE_CHAR:
1520 /*
1521 * 'Native' path for us is the same
1522 * as a path according to the NFS spec,
1523 * just skip the escape char.
1524 */
1525 fromcp++;
1526 break;
1527 /*
1528 * More may be added in the future, range 0x80-0xff
1529 */
1530 default:
1531 error = EIO;
1532 zfree(namei_zone, cp);
1533 goto out;
1534 }
1535 }
1536 /*
1537 * Translate the '%' escapes, URL-style.
1538 */
1539 while (*fromcp != '\0') {
1540 if (*fromcp == WEBNFS_ESC_CHAR) {
1541 if (fromcp[1] != '\0' && fromcp[2] != '\0') {
1542 fromcp++;
1543 *tocp++ = HEXSTRTOI(fromcp);
1544 fromcp += 2;
1545 continue;
1546 } else {
1547 error = ENOENT;
1548 zfree(namei_zone, cp);
1549 goto out;
1550 }
1551 } else
1552 *tocp++ = *fromcp++;
1553 }
1554 *tocp = '\0';
1555 zfree(namei_zone, cnp->cn_pnbuf);
1556 cnp->cn_pnbuf = cp;
1557 }
1558
1559 ndp->ni_pathlen = (tocp - cnp->cn_pnbuf) + 1;
1560 ndp->ni_segflg = UIO_SYSSPACE;
1561
1562 if (pubflag) {
1563 ndp->ni_rootdir = rootvnode;
1564 ndp->ni_loopcnt = 0;
1565 if (cnp->cn_pnbuf[0] == '/')
1566 dp = rootvnode;
1567 } else {
1568 cnp->cn_flags |= CNP_NOCROSSMOUNT;
1569 }
1570
1571 /*
1572 * Initialize for scan, set ni_startdir and bump ref on dp again
1573 * because lookup() will dereference ni_startdir.
1574 */
1575
1576 cnp->cn_td = td;
1577 vref(dp);
1578 ndp->ni_startdir = dp;
1579
1580 for (;;) {
1581 cnp->cn_nameptr = cnp->cn_pnbuf;
1582 /*
1583 * Call lookup() to do the real work. If an error occurs,
1584 * ndp->ni_vp and ni_dvp are left uninitialized or NULL and
1585 * we do not have to dereference anything before returning.
1586 * In either case ni_startdir will be dereferenced and NULLed
1587 * out.
1588 */
1589 error = lookup(ndp);
1590 if (error)
1591 break;
1592
1593 /*
1594 * Check for encountering a symbolic link. Trivial
1595 * termination occurs if no symlink encountered.
1596 * Note: zfree is safe because error is 0, so we will
1597 * not zfree it again when we break.
1598 */
1599 if ((cnp->cn_flags & CNP_ISSYMLINK) == 0) {
1600 nfsrv_object_create(ndp->ni_vp);
1601 if (cnp->cn_flags & (CNP_SAVENAME | CNP_SAVESTART))
1602 cnp->cn_flags |= CNP_HASBUF;
1603 else
1604 zfree(namei_zone, cnp->cn_pnbuf);
1605 break;
1606 }
1607
1608 /*
1609 * Validate symlink
1610 */
1611 if ((cnp->cn_flags & CNP_LOCKPARENT) && ndp->ni_pathlen == 1)
1612 VOP_UNLOCK(ndp->ni_dvp, NULL, 0, td);
1613 if (!pubflag) {
1614 error = EINVAL;
1615 goto badlink2;
1616 }
1617
1618 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
1619 error = ELOOP;
1620 goto badlink2;
1621 }
1622 if (ndp->ni_pathlen > 1)
1623 cp = zalloc(namei_zone);
1624 else
1625 cp = cnp->cn_pnbuf;
1626 aiov.iov_base = cp;
1627 aiov.iov_len = MAXPATHLEN;
1628 auio.uio_iov = &aiov;
1629 auio.uio_iovcnt = 1;
1630 auio.uio_offset = 0;
1631 auio.uio_rw = UIO_READ;
1632 auio.uio_segflg = UIO_SYSSPACE;
1633 auio.uio_td = NULL;
1634 auio.uio_resid = MAXPATHLEN;
1635 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
1636 if (error) {
1637 badlink1:
1638 if (ndp->ni_pathlen > 1)
1639 zfree(namei_zone, cp);
1640 badlink2:
1641 vrele(ndp->ni_dvp);
1642 vput(ndp->ni_vp);
1643 break;
1644 }
1645 linklen = MAXPATHLEN - auio.uio_resid;
1646 if (linklen == 0) {
1647 error = ENOENT;
1648 goto badlink1;
1649 }
1650 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
1651 error = ENAMETOOLONG;
1652 goto badlink1;
1653 }
1654
1655 /*
1656 * Adjust or replace path
1657 */
1658 if (ndp->ni_pathlen > 1) {
1659 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
1660 zfree(namei_zone, cnp->cn_pnbuf);
1661 cnp->cn_pnbuf = cp;
1662 } else
1663 cnp->cn_pnbuf[linklen] = '\0';
1664 ndp->ni_pathlen += linklen;
1665
1666 /*
1667 * Cleanup refs for next loop and check if root directory
1668 * should replace current directory. Normally ni_dvp
1669 * becomes the new base directory and is cleaned up when
1670 * we loop. Explicitly null pointers after invalidation
1671 * to clarify operation.
1672 */
1673 vput(ndp->ni_vp);
1674 ndp->ni_vp = NULL;
1675
1676 if (cnp->cn_pnbuf[0] == '/') {
1677 vrele(ndp->ni_dvp);
1678 ndp->ni_dvp = ndp->ni_rootdir;
1679 vref(ndp->ni_dvp);
1680 }
1681 ndp->ni_startdir = ndp->ni_dvp;
1682 ndp->ni_dvp = NULL;
1683 }
1684
1685 /*
1686 * nfs_namei() guarentees that fields will not contain garbage
1687 * whether an error occurs or not. This allows the caller to track
1688 * cleanup state trivially.
1689 */
1690out:
1691 if (error) {
1692 zfree(namei_zone, cnp->cn_pnbuf);
1693 ndp->ni_vp = NULL;
1694 ndp->ni_dvp = NULL;
1695 ndp->ni_startdir = NULL;
1696 cnp->cn_flags &= ~CNP_HASBUF;
1697 } else if ((ndp->ni_cnd.cn_flags & (CNP_WANTPARENT|CNP_LOCKPARENT)) == 0) {
1698 ndp->ni_dvp = NULL;
1699 }
1700 return (error);
1701}
1702
1703/*
1704 * A fiddled version of m_adj() that ensures null fill to a long
1705 * boundary and only trims off the back end
1706 */
1707void
1708nfsm_adj(struct mbuf *mp, int len, int nul)
1709{
1710 struct mbuf *m;
1711 int count, i;
1712 char *cp;
1713
1714 /*
1715 * Trim from tail. Scan the mbuf chain,
1716 * calculating its length and finding the last mbuf.
1717 * If the adjustment only affects this mbuf, then just
1718 * adjust and return. Otherwise, rescan and truncate
1719 * after the remaining size.
1720 */
1721 count = 0;
1722 m = mp;
1723 for (;;) {
1724 count += m->m_len;
1725 if (m->m_next == (struct mbuf *)0)
1726 break;
1727 m = m->m_next;
1728 }
1729 if (m->m_len > len) {
1730 m->m_len -= len;
1731 if (nul > 0) {
1732 cp = mtod(m, caddr_t)+m->m_len-nul;
1733 for (i = 0; i < nul; i++)
1734 *cp++ = '\0';
1735 }
1736 return;
1737 }
1738 count -= len;
1739 if (count < 0)
1740 count = 0;
1741 /*
1742 * Correct length for chain is "count".
1743 * Find the mbuf with last data, adjust its length,
1744 * and toss data from remaining mbufs on chain.
1745 */
1746 for (m = mp; m; m = m->m_next) {
1747 if (m->m_len >= count) {
1748 m->m_len = count;
1749 if (nul > 0) {
1750 cp = mtod(m, caddr_t)+m->m_len-nul;
1751 for (i = 0; i < nul; i++)
1752 *cp++ = '\0';
1753 }
1754 break;
1755 }
1756 count -= m->m_len;
1757 }
1758 for (m = m->m_next;m;m = m->m_next)
1759 m->m_len = 0;
1760}
1761
1762/*
1763 * Make these functions instead of macros, so that the kernel text size
1764 * doesn't get too big...
1765 */
1766void
1767nfsm_srvwcc(struct nfsrv_descript *nfsd, int before_ret,
1768 struct vattr *before_vap, int after_ret, struct vattr *after_vap,
1769 struct mbuf **mbp, char **bposp)
1770{
1771 struct mbuf *mb = *mbp, *mb2;
1772 char *bpos = *bposp;
1773 u_int32_t *tl;
1774
1775 if (before_ret) {
1776 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1777 *tl = nfs_false;
1778 } else {
1779 nfsm_build(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
1780 *tl++ = nfs_true;
1781 txdr_hyper(before_vap->va_size, tl);
1782 tl += 2;
1783 txdr_nfsv3time(&(before_vap->va_mtime), tl);
1784 tl += 2;
1785 txdr_nfsv3time(&(before_vap->va_ctime), tl);
1786 }
1787 *bposp = bpos;
1788 *mbp = mb;
1789 nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp);
1790}
1791
1792void
1793nfsm_srvpostopattr(struct nfsrv_descript *nfsd, int after_ret,
1794 struct vattr *after_vap, struct mbuf **mbp, char **bposp)
1795{
1796 struct mbuf *mb = *mbp, *mb2;
1797 char *bpos = *bposp;
1798 u_int32_t *tl;
1799 struct nfs_fattr *fp;
1800
1801 if (after_ret) {
1802 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1803 *tl = nfs_false;
1804 } else {
1805 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FATTR);
1806 *tl++ = nfs_true;
1807 fp = (struct nfs_fattr *)tl;
1808 nfsm_srvfattr(nfsd, after_vap, fp);
1809 }
1810 *mbp = mb;
1811 *bposp = bpos;
1812}
1813
1814void
1815nfsm_srvfattr(struct nfsrv_descript *nfsd, struct vattr *vap,
1816 struct nfs_fattr *fp)
1817{
1818
1819 fp->fa_nlink = txdr_unsigned(vap->va_nlink);
1820 fp->fa_uid = txdr_unsigned(vap->va_uid);
1821 fp->fa_gid = txdr_unsigned(vap->va_gid);
1822 if (nfsd->nd_flag & ND_NFSV3) {
1823 fp->fa_type = vtonfsv3_type(vap->va_type);
1824 fp->fa_mode = vtonfsv3_mode(vap->va_mode);
1825 txdr_hyper(vap->va_size, &fp->fa3_size);
1826 txdr_hyper(vap->va_bytes, &fp->fa3_used);
1827 fp->fa3_rdev.specdata1 = txdr_unsigned(umajor(vap->va_rdev));
1828 fp->fa3_rdev.specdata2 = txdr_unsigned(uminor(vap->va_rdev));
1829 fp->fa3_fsid.nfsuquad[0] = 0;
1830 fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(vap->va_fsid);
1831 fp->fa3_fileid.nfsuquad[0] = 0;
1832 fp->fa3_fileid.nfsuquad[1] = txdr_unsigned(vap->va_fileid);
1833 txdr_nfsv3time(&vap->va_atime, &fp->fa3_atime);
1834 txdr_nfsv3time(&vap->va_mtime, &fp->fa3_mtime);
1835 txdr_nfsv3time(&vap->va_ctime, &fp->fa3_ctime);
1836 } else {
1837 fp->fa_type = vtonfsv2_type(vap->va_type);
1838 fp->fa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1839 fp->fa2_size = txdr_unsigned(vap->va_size);
1840 fp->fa2_blocksize = txdr_unsigned(vap->va_blocksize);
1841 if (vap->va_type == VFIFO)
1842 fp->fa2_rdev = 0xffffffff;
1843 else
1844 fp->fa2_rdev = txdr_unsigned(vap->va_rdev);
1845 fp->fa2_blocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE);
1846 fp->fa2_fsid = txdr_unsigned(vap->va_fsid);
1847 fp->fa2_fileid = txdr_unsigned(vap->va_fileid);
1848 txdr_nfsv2time(&vap->va_atime, &fp->fa2_atime);
1849 txdr_nfsv2time(&vap->va_mtime, &fp->fa2_mtime);
1850 txdr_nfsv2time(&vap->va_ctime, &fp->fa2_ctime);
1851 }
1852}
1853
1854/*
1855 * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked)
1856 * - look up fsid in mount list (if not found ret error)
1857 * - get vp and export rights by calling VFS_FHTOVP()
1858 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
1859 * - if not lockflag unlock it with VOP_UNLOCK()
1860 */
1861int
1862nfsrv_fhtovp(fhandle_t *fhp, int lockflag, struct vnode **vpp,
1863 struct ucred *cred, struct nfssvc_sock *slp, struct sockaddr *nam,
1864 int *rdonlyp, int kerbflag, int pubflag)
1865{
1866 struct thread *td = curthread; /* XXX */
1867 struct mount *mp;
1868 int i;
1869 struct ucred *credanon;
1870 int error, exflags;
1871#ifdef MNT_EXNORESPORT /* XXX needs mountd and /etc/exports help yet */
1872 struct sockaddr_int *saddr;
1873#endif
1874
1875 *vpp = (struct vnode *)0;
1876
1877 if (nfs_ispublicfh(fhp)) {
1878 if (!pubflag || !nfs_pub.np_valid)
1879 return (ESTALE);
1880 fhp = &nfs_pub.np_handle;
1881 }
1882
1883 mp = vfs_getvfs(&fhp->fh_fsid);
1884 if (!mp)
1885 return (ESTALE);
1886 error = VFS_CHECKEXP(mp, nam, &exflags, &credanon);
1887 if (error)
1888 return (error);
1889 error = VFS_FHTOVP(mp, &fhp->fh_fid, vpp);
1890 if (error)
1891 return (error);
1892#ifdef MNT_EXNORESPORT
1893 if (!(exflags & (MNT_EXNORESPORT|MNT_EXPUBLIC))) {
1894 saddr = (struct sockaddr_in *)nam;
1895 if (saddr->sin_family == AF_INET &&
1896 ntohs(saddr->sin_port) >= IPPORT_RESERVED) {
1897 vput(*vpp);
1898 *vpp = NULL;
1899 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1900 }
1901 }
1902#endif
1903 /*
1904 * Check/setup credentials.
1905 */
1906 if (exflags & MNT_EXKERB) {
1907 if (!kerbflag) {
1908 vput(*vpp);
1909 *vpp = NULL;
1910 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1911 }
1912 } else if (kerbflag) {
1913 vput(*vpp);
1914 *vpp = NULL;
1915 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1916 } else if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
1917 cred->cr_uid = credanon->cr_uid;
1918 for (i = 0; i < credanon->cr_ngroups && i < NGROUPS; i++)
1919 cred->cr_groups[i] = credanon->cr_groups[i];
1920 cred->cr_ngroups = i;
1921 }
1922 if (exflags & MNT_EXRDONLY)
1923 *rdonlyp = 1;
1924 else
1925 *rdonlyp = 0;
1926
1927 nfsrv_object_create(*vpp);
1928
1929 if (!lockflag)
1930 VOP_UNLOCK(*vpp, NULL, 0, td);
1931 return (0);
1932}
1933
1934
1935/*
1936 * WebNFS: check if a filehandle is a public filehandle. For v3, this
1937 * means a length of 0, for v2 it means all zeroes. nfsm_srvmtofh has
1938 * transformed this to all zeroes in both cases, so check for it.
1939 */
1940int
1941nfs_ispublicfh(fhandle_t *fhp)
1942{
1943 char *cp = (char *)fhp;
1944 int i;
1945
1946 for (i = 0; i < NFSX_V3FH; i++)
1947 if (*cp++ != 0)
1948 return (FALSE);
1949 return (TRUE);
1950}
1951
1952#endif /* NFS_NOSERVER */
1953/*
1954 * This function compares two net addresses by family and returns TRUE
1955 * if they are the same host.
1956 * If there is any doubt, return FALSE.
1957 * The AF_INET family is handled as a special case so that address mbufs
1958 * don't need to be saved to store "struct in_addr", which is only 4 bytes.
1959 */
1960int
1961netaddr_match(int family, union nethostaddr *haddr, struct sockaddr *nam)
1962{
1963 struct sockaddr_in *inetaddr;
1964
1965 switch (family) {
1966 case AF_INET:
1967 inetaddr = (struct sockaddr_in *)nam;
1968 if (inetaddr->sin_family == AF_INET &&
1969 inetaddr->sin_addr.s_addr == haddr->had_inetaddr)
1970 return (1);
1971 break;
1972 default:
1973 break;
1974 };
1975 return (0);
1976}
1977
1978static nfsuint64 nfs_nullcookie = { { 0, 0 } };
1979/*
1980 * This function finds the directory cookie that corresponds to the
1981 * logical byte offset given.
1982 */
1983nfsuint64 *
1984nfs_getcookie(struct nfsnode *np, off_t off, int add)
1985{
1986 struct nfsdmap *dp, *dp2;
1987 int pos;
1988
1989 pos = (uoff_t)off / NFS_DIRBLKSIZ;
1990 if (pos == 0 || off < 0) {
1991#ifdef DIAGNOSTIC
1992 if (add)
1993 panic("nfs getcookie add at <= 0");
1994#endif
1995 return (&nfs_nullcookie);
1996 }
1997 pos--;
1998 dp = np->n_cookies.lh_first;
1999 if (!dp) {
2000 if (add) {
2001 MALLOC(dp, struct nfsdmap *, sizeof (struct nfsdmap),
2002 M_NFSDIROFF, M_WAITOK);
2003 dp->ndm_eocookie = 0;
2004 LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list);
2005 } else
2006 return ((nfsuint64 *)0);
2007 }
2008 while (pos >= NFSNUMCOOKIES) {
2009 pos -= NFSNUMCOOKIES;
2010 if (dp->ndm_list.le_next) {
2011 if (!add && dp->ndm_eocookie < NFSNUMCOOKIES &&
2012 pos >= dp->ndm_eocookie)
2013 return ((nfsuint64 *)0);
2014 dp = dp->ndm_list.le_next;
2015 } else if (add) {
2016 MALLOC(dp2, struct nfsdmap *, sizeof (struct nfsdmap),
2017 M_NFSDIROFF, M_WAITOK);
2018 dp2->ndm_eocookie = 0;
2019 LIST_INSERT_AFTER(dp, dp2, ndm_list);
2020 dp = dp2;
2021 } else
2022 return ((nfsuint64 *)0);
2023 }
2024 if (pos >= dp->ndm_eocookie) {
2025 if (add)
2026 dp->ndm_eocookie = pos + 1;
2027 else
2028 return ((nfsuint64 *)0);
2029 }
2030 return (&dp->ndm_cookies[pos]);
2031}
2032
2033/*
2034 * Invalidate cached directory information, except for the actual directory
2035 * blocks (which are invalidated separately).
2036 * Done mainly to avoid the use of stale offset cookies.
2037 */
2038void
2039nfs_invaldir(struct vnode *vp)
2040{
2041 struct nfsnode *np = VTONFS(vp);
2042
2043#ifdef DIAGNOSTIC
2044 if (vp->v_type != VDIR)
2045 panic("nfs: invaldir not dir");
2046#endif
2047 np->n_direofoffset = 0;
2048 np->n_cookieverf.nfsuquad[0] = 0;
2049 np->n_cookieverf.nfsuquad[1] = 0;
2050 if (np->n_cookies.lh_first)
2051 np->n_cookies.lh_first->ndm_eocookie = 0;
2052}
2053
2054/*
2055 * The write verifier has changed (probably due to a server reboot), so all
2056 * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
2057 * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
2058 * and B_CLUSTEROK flags. Once done the new write verifier can be set for the
2059 * mount point.
2060 *
2061 * B_CLUSTEROK must be cleared along with B_NEEDCOMMIT because stage 1 data
2062 * writes are not clusterable.
2063 */
2064void
2065nfs_clearcommit(struct mount *mp)
2066{
2067 struct vnode *vp, *nvp;
2068 struct buf *bp, *nbp;
2069 lwkt_tokref ilock;
2070 int s;
2071
2072 lwkt_gettoken(&ilock, &mntvnode_token);
2073 s = splbio();
2074 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp; vp = nvp) {
2075 nvp = TAILQ_NEXT(vp, v_nmntvnodes); /* ZZZ */
2076 if (vp->v_flag & VPLACEMARKER)
2077 continue;
2078 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2079 nbp = TAILQ_NEXT(bp, b_vnbufs);
2080 if (BUF_REFCNT(bp) == 0 &&
2081 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2082 == (B_DELWRI | B_NEEDCOMMIT)) {
2083 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
2084 }
2085 }
2086 }
2087 splx(s);
2088 lwkt_reltoken(&ilock);
2089}
2090
2091#ifndef NFS_NOSERVER
2092/*
2093 * Map errnos to NFS error numbers. For Version 3 also filter out error
2094 * numbers not specified for the associated procedure.
2095 */
2096int
2097nfsrv_errmap(struct nfsrv_descript *nd, int err)
2098{
2099 short *defaulterrp, *errp;
2100
2101 if (nd->nd_flag & ND_NFSV3) {
2102 if (nd->nd_procnum <= NFSPROC_COMMIT) {
2103 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
2104 while (*++errp) {
2105 if (*errp == err)
2106 return (err);
2107 else if (*errp > err)
2108 break;
2109 }
2110 return ((int)*defaulterrp);
2111 } else
2112 return (err & 0xffff);
2113 }
2114 if (err <= ELAST)
2115 return ((int)nfsrv_v2errmap[err - 1]);
2116 return (NFSERR_IO);
2117}
2118
2119int
2120nfsrv_object_create(struct vnode *vp)
2121{
2122 struct thread *td = curthread;
2123
2124 if (vp == NULL || vp->v_type != VREG)
2125 return (1);
2126 return (vfs_object_create(vp, td));
2127}
2128
2129/*
2130 * Sort the group list in increasing numerical order.
2131 * (Insertion sort by Chris Torek, who was grossed out by the bubble sort
2132 * that used to be here.)
2133 */
2134void
2135nfsrvw_sort(gid_t *list, int num)
2136{
2137 int i, j;
2138 gid_t v;
2139
2140 /* Insertion sort. */
2141 for (i = 1; i < num; i++) {
2142 v = list[i];
2143 /* find correct slot for value v, moving others up */
2144 for (j = i; --j >= 0 && v < list[j];)
2145 list[j + 1] = list[j];
2146 list[j + 1] = v;
2147 }
2148}
2149
2150/*
2151 * copy credentials making sure that the result can be compared with bcmp().
2152 */
2153void
2154nfsrv_setcred(struct ucred *incred, struct ucred *outcred)
2155{
2156 int i;
2157
2158 bzero((caddr_t)outcred, sizeof (struct ucred));
2159 outcred->cr_ref = 1;
2160 outcred->cr_uid = incred->cr_uid;
2161 outcred->cr_ngroups = incred->cr_ngroups;
2162 for (i = 0; i < incred->cr_ngroups; i++)
2163 outcred->cr_groups[i] = incred->cr_groups[i];
2164 nfsrvw_sort(outcred->cr_groups, outcred->cr_ngroups);
2165}
2166#endif /* NFS_NOSERVER */