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