nrelease: Install security/ca_root_nss explicitly (non-automatic).
[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.
dc71b7ab 16 * 3. Neither the name of the University nor the names of its contributors
984263bc
MD
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * @(#)nfs_subs.c 8.8 (Berkeley) 5/22/95
79e5012e 33 * $FreeBSD: /repoman/r/ncvs/src/sys/nfsclient/nfs_subs.c,v 1.128 2004/04/14 23:23:55 peadar Exp $
984263bc
MD
34 */
35
36/*
37 * These functions support the macros and help fiddle mbuf chains for
38 * the nfs op functions. They do things like create the rpc header and
39 * copy data between mbuf chains and uio lists.
40 */
41#include <sys/param.h>
42#include <sys/systm.h>
43#include <sys/kernel.h>
44#include <sys/buf.h>
45#include <sys/proc.h>
46#include <sys/mount.h>
47#include <sys/vnode.h>
fad57d0e 48#include <sys/nlookup.h>
984263bc
MD
49#include <sys/namei.h>
50#include <sys/mbuf.h>
51#include <sys/socket.h>
52#include <sys/stat.h>
53#include <sys/malloc.h>
54#include <sys/sysent.h>
55#include <sys/syscall.h>
e06bda7f 56#include <sys/sysproto.h>
984263bc 57#include <sys/conf.h>
70aac194 58#include <sys/objcache.h>
984263bc
MD
59
60#include <vm/vm.h>
61#include <vm/vm_object.h>
62#include <vm/vm_extern.h>
984263bc 63
3020e3be
MD
64#include <sys/buf2.h>
65
1f2de5d4
MD
66#include "rpcv2.h"
67#include "nfsproto.h"
68#include "nfs.h"
c1cf1e59 69#include "nfsmount.h"
1f2de5d4
MD
70#include "nfsnode.h"
71#include "xdr_subs.h"
72#include "nfsm_subs.h"
1f2de5d4 73#include "nfsrtt.h"
984263bc
MD
74
75#include <netinet/in.h>
76
0704a975
SG
77MALLOC_DEFINE(M_NFSMOUNT, "NFS mount", "NFS mount");
78
984263bc
MD
79/*
80 * Data items converted to xdr at startup, since they are constant
81 * This is kinda hokey, but may save a little time doing byte swaps
82 */
83u_int32_t nfs_xdrneg1;
e97453f3
MD
84u_int32_t rpc_reply, rpc_msgdenied, rpc_mismatch, rpc_vers;
85u_int32_t rpc_auth_unix, rpc_msgaccepted, rpc_call, rpc_autherr;
86u_int32_t rpc_auth_kerb;
e07fef60 87u_int32_t nfs_prog, nfs_true, nfs_false;
984263bc
MD
88
89/* And other global data */
984263bc
MD
90static enum vtype nv2tov_type[8]= {
91 VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON, VNON
92};
93enum vtype nv3tov_type[8]= {
94 VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO
95};
96
97int nfs_ticks;
e97453f3 98
c6b43e93
MD
99/*
100 * Protect master lists only. Primary protection uses the per-mount
101 * and per nfssvc_sock tokens.
102 */
a3c18566 103struct lwkt_token nfs_token = LWKT_TOKEN_INITIALIZER(unp_token);
c6b43e93 104
74d62460 105static long nfs_pbuf_freecnt = -1; /* start out unlimited */
984263bc 106
49433307 107struct nfsmount_head nfs_mountq = TAILQ_HEAD_INITIALIZER(nfs_mountq);
984263bc
MD
108struct nfssvc_sockhead nfssvc_sockhead;
109int nfssvc_sockhead_flag;
110struct nfsd_head nfsd_head;
111int nfsd_head_flag;
112struct nfs_bufq nfs_bufq;
984263bc
MD
113struct nqfhhashhead *nqfhhashtbl;
114u_long nqfhhash;
115
984263bc
MD
116static int nfs_prev_nfssvc_sy_narg;
117static sy_call_t *nfs_prev_nfssvc_sy_call;
118
119#ifndef NFS_NOSERVER
120
984263bc
MD
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
2cb16efc
MD
547/*
548 * This needs to return a monotonically increasing or close to monotonically
549 * increasing result, otherwise the write gathering queues won't work
550 * properly.
551 */
984263bc 552u_quad_t
e851b29e 553nfs_curusec(void)
984263bc
MD
554{
555 struct timeval tv;
556
2cb16efc 557 getmicrouptime(&tv);
984263bc
MD
558 return ((u_quad_t)tv.tv_sec * 1000000 + (u_quad_t)tv.tv_usec);
559}
560
984263bc
MD
561/*
562 * Called once to initialize data structures...
563 */
564int
e851b29e 565nfs_init(struct vfsconf *vfsp)
984263bc 566{
481d12aa 567 callout_init_mp(&nfs_timer_handle);
0704a975 568 nfsmount_objcache = objcache_create_simple(M_NFSMOUNT, sizeof(struct nfsmount));
984263bc
MD
569
570 nfs_mount_type = vfsp->vfc_typenum;
571 nfsrtt.pos = 0;
572 rpc_vers = txdr_unsigned(RPC_VER2);
573 rpc_call = txdr_unsigned(RPC_CALL);
574 rpc_reply = txdr_unsigned(RPC_REPLY);
575 rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED);
576 rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED);
577 rpc_mismatch = txdr_unsigned(RPC_MISMATCH);
578 rpc_autherr = txdr_unsigned(RPC_AUTHERR);
579 rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX);
580 rpc_auth_kerb = txdr_unsigned(RPCAUTH_KERB4);
581 nfs_prog = txdr_unsigned(NFS_PROG);
984263bc
MD
582 nfs_true = txdr_unsigned(TRUE);
583 nfs_false = txdr_unsigned(FALSE);
584 nfs_xdrneg1 = txdr_unsigned(-1);
585 nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000;
586 if (nfs_ticks < 1)
587 nfs_ticks = 1;
984263bc
MD
588 nfs_nhinit(); /* Init the nfsnode table */
589#ifndef NFS_NOSERVER
590 nfsrv_init(0); /* Init server data structures */
591 nfsrv_initcache(); /* Init the server request cache */
592#endif
593
b9a7a2bd
MD
594 /*
595 * Mainly for vkernel operation. If memory is severely limited
596 */
597 if (nfs_maxasyncbio > nmbclusters * MCLBYTES / NFS_MAXDATA / 3)
598 nfs_maxasyncbio = nmbclusters * MCLBYTES / NFS_MAXDATA / 3;
599 if (nfs_maxasyncbio < 4)
600 nfs_maxasyncbio = 4;
601
984263bc
MD
602 /*
603 * Initialize reply list and start timer
604 */
c6b43e93 605 nfs_timer_callout(0);
984263bc 606
e06bda7f 607#if 1 /* XXX this isn't really needed */
984263bc
MD
608 nfs_prev_nfssvc_sy_narg = sysent[SYS_nfssvc].sy_narg;
609 sysent[SYS_nfssvc].sy_narg = 2;
e06bda7f 610#endif
984263bc 611 nfs_prev_nfssvc_sy_call = sysent[SYS_nfssvc].sy_call;
753fd850 612 sysent[SYS_nfssvc].sy_call = (sy_call_t *)sys_nfssvc;
984263bc 613
2f0acc22 614 nfs_pbuf_freecnt = nswbuf_kva / 2 + 1;
984263bc
MD
615
616 return (0);
617}
618
619int
e851b29e 620nfs_uninit(struct vfsconf *vfsp)
984263bc 621{
f786cc86 622 callout_stop(&nfs_timer_handle);
984263bc 623 nfs_mount_type = -1;
984263bc
MD
624 sysent[SYS_nfssvc].sy_narg = nfs_prev_nfssvc_sy_narg;
625 sysent[SYS_nfssvc].sy_call = nfs_prev_nfssvc_sy_call;
f4aeef67
IV
626 nfs_nhdestroy(); /* Destroy the nfsnode table */
627#ifndef NFS_NOSERVER
628 nfsrv_destroycache(); /* Destroy the server request cache */
629#endif
630 objcache_destroy(nfsmount_objcache);
984263bc
MD
631 return (0);
632}
633
634/*
635 * Attribute cache routines.
636 * nfs_loadattrcache() - loads or updates the cache contents from attributes
637 * that are on the mbuf list
638 * nfs_getattrcache() - returns valid attributes if found in cache, returns
639 * error otherwise
640 */
641
642/*
643 * Load the attribute cache (that lives in the nfsnode entry) with
5a9187cb
MD
644 * the values on the mbuf list. Load *vaper with the attributes. vaper
645 * may be NULL.
646 *
647 * As a side effect n_mtime, which we use to determine if the file was
648 * modified by some other host, is set to the attribute timestamp and
649 * NRMODIFIED is set if the two values differ.
650 *
651 * WARNING: the mtime loaded into vaper does not necessarily represent
652 * n_mtime or n_attr.mtime due to NACC and NUPD.
984263bc
MD
653 */
654int
42edf14f 655nfs_loadattrcache(struct vnode *vp, struct mbuf **mdp, caddr_t *dposp,
5a9187cb 656 struct vattr *vaper, int lattr_flags)
984263bc 657{
40393ded
RG
658 struct vattr *vap;
659 struct nfs_fattr *fp;
660 struct nfsnode *np;
661 int32_t t1;
984263bc 662 caddr_t cp2;
e4c9c0c8 663 int error = 0;
0e9b9130 664 int rmajor, rminor;
e4c9c0c8 665 udev_t rdev;
984263bc
MD
666 struct mbuf *md;
667 enum vtype vtyp;
668 u_short vmode;
669 struct timespec mtime;
670 int v3 = NFS_ISV3(vp);
671
672 md = *mdp;
673 t1 = (mtod(md, caddr_t) + md->m_len) - *dposp;
674 if ((error = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, &cp2)) != 0)
675 return (error);
676 fp = (struct nfs_fattr *)cp2;
677 if (v3) {
678 vtyp = nfsv3tov_type(fp->fa_type);
679 vmode = fxdr_unsigned(u_short, fp->fa_mode);
0e9b9130
MD
680 rmajor = (int)fxdr_unsigned(int, fp->fa3_rdev.specdata1);
681 rminor = (int)fxdr_unsigned(int, fp->fa3_rdev.specdata2);
984263bc
MD
682 fxdr_nfsv3time(&fp->fa3_mtime, &mtime);
683 } else {
684 vtyp = nfsv2tov_type(fp->fa_type);
685 vmode = fxdr_unsigned(u_short, fp->fa_mode);
686 /*
687 * XXX
688 *
689 * The duplicate information returned in fa_type and fa_mode
690 * is an ambiguity in the NFS version 2 protocol.
691 *
692 * VREG should be taken literally as a regular file. If a
693 * server intents to return some type information differently
694 * in the upper bits of the mode field (e.g. for sockets, or
695 * FIFOs), NFSv2 mandates fa_type to be VNON. Anyway, we
696 * leave the examination of the mode bits even in the VREG
697 * case to avoid breakage for bogus servers, but we make sure
698 * that there are actually type bits set in the upper part of
699 * fa_mode (and failing that, trust the va_type field).
700 *
701 * NFSv3 cleared the issue, and requires fa_mode to not
702 * contain any type information (while also introduing sockets
703 * and FIFOs for fa_type).
704 */
705 if (vtyp == VNON || (vtyp == VREG && (vmode & S_IFMT) != 0))
706 vtyp = IFTOVT(vmode);
707 rdev = fxdr_unsigned(int32_t, fp->fa2_rdev);
0e9b9130
MD
708 rmajor = umajor(rdev);
709 rminor = uminor(rdev);
984263bc
MD
710 fxdr_nfsv2time(&fp->fa2_mtime, &mtime);
711
712 /*
713 * Really ugly NFSv2 kludge.
714 */
e4c9c0c8 715 if (vtyp == VCHR && rdev == (udev_t)0xffffffff)
984263bc
MD
716 vtyp = VFIFO;
717 }
718
719 /*
720 * If v_type == VNON it is a new node, so fill in the v_type,
721 * n_mtime fields. Check to see if it represents a special
722 * device, and if so, check for a possible alias. Once the
723 * correct vnode has been obtained, fill in the rest of the
724 * information.
725 */
726 np = VTONFS(vp);
727 if (vp->v_type != vtyp) {
1c843a13 728 nfs_setvtype(vp, vtyp);
984263bc 729 if (vp->v_type == VFIFO) {
6ddb7618 730 vp->v_ops = &vp->v_mount->mnt_vn_fifo_ops;
0961aa92 731 } else if (vp->v_type == VCHR || vp->v_type == VBLK) {
6ddb7618 732 vp->v_ops = &vp->v_mount->mnt_vn_spec_ops;
0e9b9130 733 addaliasu(vp, rmajor, rminor);
0961aa92 734 } else {
6ddb7618 735 vp->v_ops = &vp->v_mount->mnt_vn_use_ops;
984263bc
MD
736 }
737 np->n_mtime = mtime.tv_sec;
5a9187cb 738 } else if (np->n_mtime != mtime.tv_sec) {
9793c819 739 /*
5a9187cb
MD
740 * If we haven't modified the file locally and the server
741 * timestamp does not match, then the server probably
742 * modified the file. We must flag this condition so
743 * the proper syncnronization can be done. We do not
744 * try to synchronize the state here because that
745 * could lead to an endless recursion.
746 *
747 * XXX loadattrcache can be set during the reply to a write,
748 * before the write timestamp is properly processed. To
749 * avoid unconditionally setting the rmodified bit (which
750 * has the effect of flushing the cache), we only do this
751 * check if the lmodified bit is not set.
9793c819
MD
752 */
753 np->n_mtime = mtime.tv_sec;
5a9187cb
MD
754 if ((lattr_flags & NFS_LATTR_NOMTIMECHECK) == 0)
755 np->n_flag |= NRMODIFIED;
984263bc
MD
756 }
757 vap = &np->n_vattr;
758 vap->va_type = vtyp;
759 vap->va_mode = (vmode & 07777);
0e9b9130
MD
760 vap->va_rmajor = rmajor;
761 vap->va_rminor = rminor;
984263bc
MD
762 vap->va_mtime = mtime;
763 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
764 if (v3) {
765 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
766 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
767 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
768 vap->va_size = fxdr_hyper(&fp->fa3_size);
769 vap->va_blocksize = NFS_FABLKSIZE;
770 vap->va_bytes = fxdr_hyper(&fp->fa3_used);
50626622 771 vap->va_fileid = fxdr_hyper(&fp->fa3_fileid);
984263bc
MD
772 fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime);
773 fxdr_nfsv3time(&fp->fa3_ctime, &vap->va_ctime);
774 vap->va_flags = 0;
775 vap->va_filerev = 0;
776 } else {
777 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
778 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
779 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
780 vap->va_size = fxdr_unsigned(u_int32_t, fp->fa2_size);
781 vap->va_blocksize = fxdr_unsigned(int32_t, fp->fa2_blocksize);
782 vap->va_bytes = (u_quad_t)fxdr_unsigned(int32_t, fp->fa2_blocks)
783 * NFS_FABLKSIZE;
784 vap->va_fileid = fxdr_unsigned(int32_t, fp->fa2_fileid);
785 fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime);
786 vap->va_flags = 0;
787 vap->va_ctime.tv_sec = fxdr_unsigned(u_int32_t,
788 fp->fa2_ctime.nfsv2_sec);
789 vap->va_ctime.tv_nsec = 0;
790 vap->va_gen = fxdr_unsigned(u_int32_t,fp->fa2_ctime.nfsv2_usec);
791 vap->va_filerev = 0;
792 }
cec73927 793 np->n_attrstamp = time_uptime;
984263bc
MD
794 if (vap->va_size != np->n_size) {
795 if (vap->va_type == VREG) {
8452310f
MD
796 /*
797 * Get rid of all the junk we had before and just
798 * set NRMODIFIED if NLMODIFIED is 0. Depend on
799 * occassionally flushing our dirty buffers to
800 * clear both the NLMODIFIED and NRMODIFIED flags.
801 */
802 if ((np->n_flag & NLMODIFIED) == 0)
803 np->n_flag |= NRMODIFIED;
804#if 0
5a9187cb
MD
805 if ((lattr_flags & NFS_LATTR_NOSHRINK) &&
806 vap->va_size < np->n_size) {
984263bc
MD
807 /*
808 * We've been told not to shrink the file;
809 * zero np->n_attrstamp to indicate that
810 * the attributes are stale.
5a9187cb
MD
811 *
812 * This occurs primarily due to recursive
813 * NFS ops that are executed during periods
814 * where we cannot safely reduce the size of
815 * the file.
816 *
817 * Additionally, write rpcs are broken down
818 * into buffers and np->n_size is
819 * pre-extended. Setting NRMODIFIED here
820 * can result in n_size getting reset to a
821 * lower value, which is NOT what we want.
822 * XXX this needs to be cleaned up a lot
823 * more.
984263bc
MD
824 */
825 vap->va_size = np->n_size;
826 np->n_attrstamp = 0;
5a9187cb
MD
827 if ((np->n_flag & NLMODIFIED) == 0)
828 np->n_flag |= NRMODIFIED;
829 } else if (np->n_flag & NLMODIFIED) {
79e5012e
MD
830 /*
831 * We've modified the file: Use the larger
5a9187cb
MD
832 * of our size, and the server's size. At
833 * this point the cache coherency is all
834 * shot to hell. To try to handle multiple
835 * clients appending to the file at the same
836 * time mark that the server has changed
837 * the file if the server's notion of the
838 * file size is larger then our notion.
839 *
840 * XXX this needs work.
79e5012e
MD
841 */
842 if (vap->va_size < np->n_size) {
984263bc 843 vap->va_size = np->n_size;
79e5012e 844 } else {
984263bc 845 np->n_size = vap->va_size;
5a9187cb 846 np->n_flag |= NRMODIFIED;
79e5012e 847 }
984263bc 848 } else {
5a9187cb
MD
849 /*
850 * Someone changed the file's size on the
851 * server and there are no local changes
852 * to get in the way, set the size and mark
853 * it.
854 */
984263bc 855 np->n_size = vap->va_size;
5a9187cb 856 np->n_flag |= NRMODIFIED;
984263bc 857 }
3bb7eedb 858 nvnode_pager_setsize(vp, np->n_size, XXX);
8452310f 859#endif
984263bc
MD
860 } else {
861 np->n_size = vap->va_size;
862 }
863 }
864 if (vaper != NULL) {
865 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap));
866 if (np->n_flag & NCHG) {
867 if (np->n_flag & NACC)
868 vaper->va_atime = np->n_atim;
869 if (np->n_flag & NUPD)
870 vaper->va_mtime = np->n_mtim;
871 }
872 }
873 return (0);
874}
875
876#ifdef NFS_ACDEBUG
877#include <sys/sysctl.h>
878SYSCTL_DECL(_vfs_nfs);
879static int nfs_acdebug;
880SYSCTL_INT(_vfs_nfs, OID_AUTO, acdebug, CTLFLAG_RW, &nfs_acdebug, 0, "");
881#endif
882
883/*
884 * Check the time stamp
885 * If the cache is valid, copy contents to *vap and return 0
886 * otherwise return an error
887 */
888int
e851b29e 889nfs_getattrcache(struct vnode *vp, struct vattr *vaper)
984263bc 890{
40393ded
RG
891 struct nfsnode *np;
892 struct vattr *vap;
984263bc
MD
893 struct nfsmount *nmp;
894 int timeo;
895
896 np = VTONFS(vp);
897 vap = &np->n_vattr;
898 nmp = VFSTONFS(vp->v_mount);
fad57d0e
MD
899
900 /*
901 * Dynamic timeout based on how recently the file was modified.
5a9187cb 902 * n_mtime is always valid.
fad57d0e 903 */
97100839 904 timeo = (get_approximate_time_t() - np->n_mtime) / 60;
984263bc
MD
905
906#ifdef NFS_ACDEBUG
907 if (nfs_acdebug>1)
086c1d7e 908 kprintf("nfs_getattrcache: initial timeo = %d\n", timeo);
984263bc
MD
909#endif
910
911 if (vap->va_type == VDIR) {
5a9187cb 912 if ((np->n_flag & NLMODIFIED) || timeo < nmp->nm_acdirmin)
984263bc
MD
913 timeo = nmp->nm_acdirmin;
914 else if (timeo > nmp->nm_acdirmax)
915 timeo = nmp->nm_acdirmax;
916 } else {
5a9187cb 917 if ((np->n_flag & NLMODIFIED) || timeo < nmp->nm_acregmin)
984263bc
MD
918 timeo = nmp->nm_acregmin;
919 else if (timeo > nmp->nm_acregmax)
920 timeo = nmp->nm_acregmax;
921 }
922
923#ifdef NFS_ACDEBUG
924 if (nfs_acdebug > 2)
086c1d7e 925 kprintf("acregmin %d; acregmax %d; acdirmin %d; acdirmax %d\n",
984263bc
MD
926 nmp->nm_acregmin, nmp->nm_acregmax,
927 nmp->nm_acdirmin, nmp->nm_acdirmax);
928
929 if (nfs_acdebug)
086c1d7e 930 kprintf("nfs_getattrcache: age = %d; final timeo = %d\n",
cec73927 931 (int)(time_uptime - np->n_attrstamp), timeo);
984263bc
MD
932#endif
933
cec73927 934 if (np->n_attrstamp == 0 || (time_uptime - np->n_attrstamp) >= timeo) {
984263bc
MD
935 nfsstats.attrcache_misses++;
936 return (ENOENT);
937 }
938 nfsstats.attrcache_hits++;
5a9187cb
MD
939
940 /*
941 * Our attribute cache can be stale due to modifications made on
942 * this host. XXX this is a bad hack. We need a more deterministic
943 * means of finding out which np fields are valid verses attr cache
944 * fields. We really should update the vattr info on the fly when
945 * making local changes.
946 */
984263bc
MD
947 if (vap->va_size != np->n_size) {
948 if (vap->va_type == VREG) {
8452310f
MD
949 if (np->n_flag & NLMODIFIED)
950 vap->va_size = np->n_size;
951 nfs_meta_setsize(vp, curthread, vap->va_size, 0);
984263bc
MD
952 } else {
953 np->n_size = vap->va_size;
954 }
955 }
956 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr));
957 if (np->n_flag & NCHG) {
958 if (np->n_flag & NACC)
959 vaper->va_atime = np->n_atim;
960 if (np->n_flag & NUPD)
961 vaper->va_mtime = np->n_mtim;
962 }
963 return (0);
964}
965
966#ifndef NFS_NOSERVER
fad57d0e 967
984263bc
MD
968/*
969 * Set up nameidata for a lookup() call and do it.
970 *
971 * If pubflag is set, this call is done for a lookup operation on the
972 * public filehandle. In that case we allow crossing mountpoints and
973 * absolute pathnames. However, the caller is expected to check that
974 * the lookup result is within the public fs, and deny access if
975 * it is not.
976 *
984263bc
MD
977 * dirp may be set whether an error is returned or not, and must be
978 * released by the caller.
fad57d0e 979 *
28623bf9 980 * On return nd->nl_nch usually points to the target ncp, which may represent
fad57d0e
MD
981 * a negative hit.
982 *
983 * NOTE: the caller must call nlookup_done(nd) unconditionally on return
984 * to cleanup.
984263bc
MD
985 */
986int
3a907475 987nfs_namei(struct nlookupdata *nd, struct ucred *cred, int nflags,
fad57d0e
MD
988 struct vnode **dvpp, struct vnode **vpp,
989 fhandle_t *fhp, int len,
990 struct nfssvc_sock *slp, struct sockaddr *nam, struct mbuf **mdp,
991 caddr_t *dposp, struct vnode **dirpp, struct thread *td,
992 int kerbflag, int pubflag)
984263bc 993{
40393ded
RG
994 int i, rem;
995 struct mbuf *md;
996 char *fromcp, *tocp, *cp;
fad57d0e 997 char *namebuf;
28623bf9 998 struct nchandle nch;
984263bc 999 struct vnode *dp;
67863d04 1000 struct mount *mp;
fad57d0e 1001 int error, rdonly;
984263bc 1002
70aac194 1003 namebuf = objcache_get(namei_oc, M_WAITOK);
fad57d0e 1004 *dirpp = NULL;
984263bc
MD
1005
1006 /*
fad57d0e 1007 * Copy the name from the mbuf list to namebuf.
984263bc
MD
1008 */
1009 fromcp = *dposp;
fad57d0e 1010 tocp = namebuf;
984263bc
MD
1011 md = *mdp;
1012 rem = mtod(md, caddr_t) + md->m_len - fromcp;
1013 for (i = 0; i < len; i++) {
1014 while (rem == 0) {
1015 md = md->m_next;
1016 if (md == NULL) {
1017 error = EBADRPC;
1018 goto out;
1019 }
1020 fromcp = mtod(md, caddr_t);
1021 rem = md->m_len;
1022 }
1023 if (*fromcp == '\0' || (!pubflag && *fromcp == '/')) {
1024 error = EACCES;
1025 goto out;
1026 }
1027 *tocp++ = *fromcp++;
1028 rem--;
1029 }
1030 *tocp = '\0';
1031 *mdp = md;
1032 *dposp = fromcp;
1033 len = nfsm_rndup(len)-len;
1034 if (len > 0) {
1035 if (rem >= len)
1036 *dposp += len;
1037 else if ((error = nfs_adv(mdp, dposp, len, rem)) != 0)
1038 goto out;
1039 }
1040
1041 /*
fad57d0e
MD
1042 * Extract and set starting directory. The returned dp is refd
1043 * but not locked.
984263bc 1044 */
67863d04 1045 error = nfsrv_fhtovp(fhp, FALSE, &mp, &dp, cred, slp,
fad57d0e 1046 nam, &rdonly, kerbflag, pubflag);
984263bc
MD
1047 if (error)
1048 goto out;
1049 if (dp->v_type != VDIR) {
1050 vrele(dp);
1051 error = ENOTDIR;
1052 goto out;
1053 }
1054
984263bc
MD
1055 /*
1056 * Set return directory. Reference to dp is implicitly transfered
fad57d0e
MD
1057 * to the returned pointer. This must be set before we potentially
1058 * goto out below.
984263bc 1059 */
fad57d0e 1060 *dirpp = dp;
984263bc 1061
d64fd6d3
MD
1062 /*
1063 * read-only - NLC_DELETE, NLC_RENAME_DST are disallowed. NLC_CREATE
1064 * is passed through to nlookup() and will be disallowed
1065 * if the file does not already exist.
1066 */
1067 if (rdonly) {
1068 nflags |= NLC_NFS_RDONLY;
1069 if (nflags & (NLC_DELETE | NLC_RENAME_DST)) {
1070 error = EROFS;
1071 goto out;
1072 }
1073 }
1074
1075 /*
1076 * Oh joy. For WebNFS, handle those pesky '%' escapes,
1077 * and the 'native path' indicator.
1078 */
984263bc 1079 if (pubflag) {
70aac194 1080 cp = objcache_get(namei_oc, M_WAITOK);
fad57d0e 1081 fromcp = namebuf;
984263bc
MD
1082 tocp = cp;
1083 if ((unsigned char)*fromcp >= WEBNFS_SPECCHAR_START) {
1084 switch ((unsigned char)*fromcp) {
1085 case WEBNFS_NATIVE_CHAR:
1086 /*
1087 * 'Native' path for us is the same
1088 * as a path according to the NFS spec,
1089 * just skip the escape char.
1090 */
1091 fromcp++;
1092 break;
1093 /*
1094 * More may be added in the future, range 0x80-0xff
1095 */
1096 default:
1097 error = EIO;
70aac194 1098 objcache_put(namei_oc, cp);
984263bc
MD
1099 goto out;
1100 }
1101 }
1102 /*
1103 * Translate the '%' escapes, URL-style.
1104 */
1105 while (*fromcp != '\0') {
1106 if (*fromcp == WEBNFS_ESC_CHAR) {
1107 if (fromcp[1] != '\0' && fromcp[2] != '\0') {
1108 fromcp++;
1109 *tocp++ = HEXSTRTOI(fromcp);
1110 fromcp += 2;
1111 continue;
1112 } else {
1113 error = ENOENT;
70aac194 1114 objcache_put(namei_oc, cp);
984263bc
MD
1115 goto out;
1116 }
1117 } else
1118 *tocp++ = *fromcp++;
1119 }
1120 *tocp = '\0';
70aac194 1121 objcache_put(namei_oc, namebuf);
fad57d0e 1122 namebuf = cp;
984263bc
MD
1123 }
1124
fad57d0e
MD
1125 /*
1126 * Setup for search. We need to get a start directory from dp. Note
1127 * that dp is ref'd, but we no longer 'own' the ref (*dirpp owns it).
1128 */
1129 if (pubflag == 0) {
3a907475
MD
1130 nflags |= NLC_NFS_NOSOFTLINKTRAV;
1131 nflags |= NLC_NOCROSSMOUNT;
984263bc
MD
1132 }
1133
1134 /*
fad57d0e
MD
1135 * We need a starting ncp from the directory vnode dp. dp must not
1136 * be locked. The returned ncp will be refd but not locked.
1137 *
1138 * If no suitable ncp is found we instruct cache_fromdvp() to create
1139 * one. If this fails the directory has probably been removed while
1140 * the target was chdir'd into it and any further lookup will fail.
984263bc 1141 */
28623bf9 1142 if ((error = cache_fromdvp(dp, cred, 1, &nch)) != 0)
fad57d0e 1143 goto out;
3a907475 1144 nlookup_init_raw(nd, namebuf, UIO_SYSSPACE, nflags, cred, &nch);
28623bf9 1145 cache_drop(&nch);
984263bc 1146
fad57d0e
MD
1147 /*
1148 * Ok, do the lookup.
1149 */
1150 error = nlookup(nd);
984263bc 1151
fad57d0e
MD
1152 /*
1153 * If no error occured return the requested dvpp and vpp. If
28623bf9 1154 * NLC_CREATE was specified nd->nl_nch may represent a negative
fad57d0e
MD
1155 * cache hit in which case we do not attempt to obtain the vp.
1156 */
1157 if (error == 0) {
fad57d0e 1158 if (dvpp) {
443472eb 1159 if (nd->nl_nch.ncp->nc_parent) {
28623bf9
MD
1160 nch = nd->nl_nch;
1161 nch.ncp = nch.ncp->nc_parent;
2247fe02
MD
1162 cache_hold(&nch);
1163 cache_lock(&nch);
28623bf9
MD
1164 error = cache_vget(&nch, nd->nl_cred,
1165 LK_EXCLUSIVE, dvpp);
2247fe02 1166 cache_put(&nch);
fad57d0e
MD
1167 } else {
1168 error = ENXIO;
1169 }
984263bc 1170 }
28623bf9
MD
1171 if (vpp && nd->nl_nch.ncp->nc_vp) {
1172 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, vpp);
984263bc 1173 }
984263bc 1174 if (error) {
fad57d0e
MD
1175 if (dvpp && *dvpp) {
1176 vput(*dvpp);
1177 *dvpp = NULL;
1178 }
1179 if (vpp && *vpp) {
1180 vput(*vpp);
1181 *vpp = NULL;
1182 }
984263bc 1183 }
984263bc
MD
1184 }
1185
1186 /*
fad57d0e 1187 * Finish up.
984263bc
MD
1188 */
1189out:
70aac194 1190 objcache_put(namei_oc, namebuf);
984263bc
MD
1191 return (error);
1192}
1193
984263bc
MD
1194/*
1195 * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked)
1196 * - look up fsid in mount list (if not found ret error)
1197 * - get vp and export rights by calling VFS_FHTOVP()
1198 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
a11aaa81 1199 * - if not lockflag unlock it with vn_unlock()
984263bc
MD
1200 */
1201int
67863d04
MD
1202nfsrv_fhtovp(fhandle_t *fhp, int lockflag,
1203 struct mount **mpp, struct vnode **vpp,
e851b29e
CP
1204 struct ucred *cred, struct nfssvc_sock *slp, struct sockaddr *nam,
1205 int *rdonlyp, int kerbflag, int pubflag)
984263bc 1206{
40393ded
RG
1207 struct mount *mp;
1208 int i;
984263bc
MD
1209 struct ucred *credanon;
1210 int error, exflags;
1211#ifdef MNT_EXNORESPORT /* XXX needs mountd and /etc/exports help yet */
1212 struct sockaddr_int *saddr;
1213#endif
1214
67863d04
MD
1215 *vpp = NULL;
1216 *mpp = NULL;
984263bc
MD
1217
1218 if (nfs_ispublicfh(fhp)) {
1219 if (!pubflag || !nfs_pub.np_valid)
1220 return (ESTALE);
1221 fhp = &nfs_pub.np_handle;
1222 }
1223
67863d04
MD
1224 mp = *mpp = vfs_getvfs(&fhp->fh_fsid);
1225 if (mp == NULL)
984263bc
MD
1226 return (ESTALE);
1227 error = VFS_CHECKEXP(mp, nam, &exflags, &credanon);
fb578eac
MD
1228 if (error) {
1229 mount_drop(mp);
984263bc 1230 return (error);
fb578eac 1231 }
67863d04 1232 error = VFS_FHTOVP(mp, NULL, &fhp->fh_fid, vpp);
fb578eac 1233 mount_drop(mp);
984263bc 1234 if (error)
c1076681 1235 return (ESTALE);
984263bc
MD
1236#ifdef MNT_EXNORESPORT
1237 if (!(exflags & (MNT_EXNORESPORT|MNT_EXPUBLIC))) {
1238 saddr = (struct sockaddr_in *)nam;
1239 if (saddr->sin_family == AF_INET &&
1240 ntohs(saddr->sin_port) >= IPPORT_RESERVED) {
1241 vput(*vpp);
1242 *vpp = NULL;
1243 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1244 }
1245 }
1246#endif
1247 /*
1248 * Check/setup credentials.
1249 */
1250 if (exflags & MNT_EXKERB) {
1251 if (!kerbflag) {
1252 vput(*vpp);
1253 *vpp = NULL;
1254 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1255 }
1256 } else if (kerbflag) {
1257 vput(*vpp);
1258 *vpp = NULL;
1259 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1260 } else if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
1261 cred->cr_uid = credanon->cr_uid;
1262 for (i = 0; i < credanon->cr_ngroups && i < NGROUPS; i++)
1263 cred->cr_groups[i] = credanon->cr_groups[i];
1264 cred->cr_ngroups = i;
1265 }
1266 if (exflags & MNT_EXRDONLY)
1267 *rdonlyp = 1;
1268 else
1269 *rdonlyp = 0;
1270
984263bc 1271 if (!lockflag)
a11aaa81 1272 vn_unlock(*vpp);
984263bc
MD
1273 return (0);
1274}
1275
984263bc
MD
1276/*
1277 * WebNFS: check if a filehandle is a public filehandle. For v3, this
1278 * means a length of 0, for v2 it means all zeroes. nfsm_srvmtofh has
1279 * transformed this to all zeroes in both cases, so check for it.
1280 */
1281int
e851b29e 1282nfs_ispublicfh(fhandle_t *fhp)
984263bc
MD
1283{
1284 char *cp = (char *)fhp;
1285 int i;
1286
1287 for (i = 0; i < NFSX_V3FH; i++)
1288 if (*cp++ != 0)
1289 return (FALSE);
1290 return (TRUE);
1291}
1292
1293#endif /* NFS_NOSERVER */
1294/*
1295 * This function compares two net addresses by family and returns TRUE
1296 * if they are the same host.
1297 * If there is any doubt, return FALSE.
1298 * The AF_INET family is handled as a special case so that address mbufs
1299 * don't need to be saved to store "struct in_addr", which is only 4 bytes.
1300 */
1301int
e851b29e 1302netaddr_match(int family, union nethostaddr *haddr, struct sockaddr *nam)
984263bc 1303{
40393ded 1304 struct sockaddr_in *inetaddr;
984263bc
MD
1305
1306 switch (family) {
1307 case AF_INET:
1308 inetaddr = (struct sockaddr_in *)nam;
1309 if (inetaddr->sin_family == AF_INET &&
1310 inetaddr->sin_addr.s_addr == haddr->had_inetaddr)
1311 return (1);
1312 break;
1313 default:
1314 break;
0fdb7d01 1315 }
984263bc
MD
1316 return (0);
1317}
1318
1319static nfsuint64 nfs_nullcookie = { { 0, 0 } };
1320/*
1321 * This function finds the directory cookie that corresponds to the
1322 * logical byte offset given.
1323 */
1324nfsuint64 *
e851b29e 1325nfs_getcookie(struct nfsnode *np, off_t off, int add)
984263bc 1326{
40393ded
RG
1327 struct nfsdmap *dp, *dp2;
1328 int pos;
984263bc
MD
1329
1330 pos = (uoff_t)off / NFS_DIRBLKSIZ;
1331 if (pos == 0 || off < 0) {
1332#ifdef DIAGNOSTIC
1333 if (add)
1334 panic("nfs getcookie add at <= 0");
1335#endif
1336 return (&nfs_nullcookie);
1337 }
1338 pos--;
1339 dp = np->n_cookies.lh_first;
1340 if (!dp) {
1341 if (add) {
884717e1
SW
1342 dp = kmalloc(sizeof(struct nfsdmap), M_NFSDIROFF,
1343 M_WAITOK);
984263bc
MD
1344 dp->ndm_eocookie = 0;
1345 LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list);
1346 } else
60233e58 1347 return (NULL);
984263bc
MD
1348 }
1349 while (pos >= NFSNUMCOOKIES) {
1350 pos -= NFSNUMCOOKIES;
1351 if (dp->ndm_list.le_next) {
1352 if (!add && dp->ndm_eocookie < NFSNUMCOOKIES &&
1353 pos >= dp->ndm_eocookie)
60233e58 1354 return (NULL);
984263bc
MD
1355 dp = dp->ndm_list.le_next;
1356 } else if (add) {
884717e1
SW
1357 dp2 = kmalloc(sizeof(struct nfsdmap), M_NFSDIROFF,
1358 M_WAITOK);
984263bc
MD
1359 dp2->ndm_eocookie = 0;
1360 LIST_INSERT_AFTER(dp, dp2, ndm_list);
1361 dp = dp2;
1362 } else
60233e58 1363 return (NULL);
984263bc
MD
1364 }
1365 if (pos >= dp->ndm_eocookie) {
1366 if (add)
1367 dp->ndm_eocookie = pos + 1;
1368 else
60233e58 1369 return (NULL);
984263bc
MD
1370 }
1371 return (&dp->ndm_cookies[pos]);
1372}
1373
1374/*
1375 * Invalidate cached directory information, except for the actual directory
1376 * blocks (which are invalidated separately).
1377 * Done mainly to avoid the use of stale offset cookies.
1378 */
1379void
e851b29e 1380nfs_invaldir(struct vnode *vp)
984263bc 1381{
40393ded 1382 struct nfsnode *np = VTONFS(vp);
984263bc
MD
1383
1384#ifdef DIAGNOSTIC
1385 if (vp->v_type != VDIR)
1386 panic("nfs: invaldir not dir");
1387#endif
1388 np->n_direofoffset = 0;
1389 np->n_cookieverf.nfsuquad[0] = 0;
1390 np->n_cookieverf.nfsuquad[1] = 0;
1391 if (np->n_cookies.lh_first)
1392 np->n_cookies.lh_first->ndm_eocookie = 0;
1393}
1394
1c843a13
MD
1395/*
1396 * Set the v_type field for an NFS client's vnode and initialize for
1397 * buffer cache operations if necessary.
1398 */
1399void
1400nfs_setvtype(struct vnode *vp, enum vtype vtyp)
1401{
1402 vp->v_type = vtyp;
1403
1404 switch(vtyp) {
1405 case VREG:
1406 case VDIR:
1407 case VLNK:
b0d18f7d
MD
1408 /*
1409 * Needs VMIO, size not yet known, and blocksize
1410 * is not really relevant if we are passing a
1411 * filesize of 0.
1412 */
1413 vinitvmio(vp, 0, PAGE_SIZE, -1);
1c843a13
MD
1414 break;
1415 default:
1416 break;
1417 }
1418}
1419
984263bc
MD
1420/*
1421 * The write verifier has changed (probably due to a server reboot), so all
1422 * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
1423 * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
1424 * and B_CLUSTEROK flags. Once done the new write verifier can be set for the
1425 * mount point.
1426 *
1427 * B_CLUSTEROK must be cleared along with B_NEEDCOMMIT because stage 1 data
1428 * writes are not clusterable.
1429 */
6bae6177
MD
1430
1431static int nfs_clearcommit_bp(struct buf *bp, void *data __unused);
0202303b
MD
1432static int nfs_clearcommit_callback(struct mount *mp, struct vnode *vp,
1433 void *data __unused);
6bae6177 1434
984263bc 1435void
e851b29e 1436nfs_clearcommit(struct mount *mp)
984263bc 1437{
fd2da346 1438 vsyncscan(mp, VMSC_NOWAIT, nfs_clearcommit_callback, NULL);
0202303b
MD
1439}
1440
1441static int
1442nfs_clearcommit_callback(struct mount *mp, struct vnode *vp,
1443 void *data __unused)
1444{
3b998fa9 1445 lwkt_gettoken(&vp->v_token);
0202303b
MD
1446 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
1447 nfs_clearcommit_bp, NULL);
3b998fa9 1448 lwkt_reltoken(&vp->v_token);
fd2da346 1449
0202303b 1450 return(0);
984263bc
MD
1451}
1452
6bae6177
MD
1453static int
1454nfs_clearcommit_bp(struct buf *bp, void *data __unused)
1455{
3b6a19b2 1456 if (BUF_LOCKINUSE(bp) == 0 &&
6bae6177
MD
1457 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
1458 == (B_DELWRI | B_NEEDCOMMIT)) {
1459 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1460 }
1461 return(0);
1462}
1463
984263bc
MD
1464#ifndef NFS_NOSERVER
1465/*
1466 * Map errnos to NFS error numbers. For Version 3 also filter out error
1467 * numbers not specified for the associated procedure.
1468 */
1469int
e851b29e 1470nfsrv_errmap(struct nfsrv_descript *nd, int err)
984263bc 1471{
40393ded 1472 short *defaulterrp, *errp;
984263bc
MD
1473
1474 if (nd->nd_flag & ND_NFSV3) {
1475 if (nd->nd_procnum <= NFSPROC_COMMIT) {
1476 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
1477 while (*++errp) {
1478 if (*errp == err)
1479 return (err);
1480 else if (*errp > err)
1481 break;
1482 }
1483 return ((int)*defaulterrp);
1484 } else
1485 return (err & 0xffff);
1486 }
1487 if (err <= ELAST)
1488 return ((int)nfsrv_v2errmap[err - 1]);
1489 return (NFSERR_IO);
1490}
1491
984263bc
MD
1492/*
1493 * Sort the group list in increasing numerical order.
1494 * (Insertion sort by Chris Torek, who was grossed out by the bubble sort
1495 * that used to be here.)
1496 */
1497void
e851b29e 1498nfsrvw_sort(gid_t *list, int num)
984263bc 1499{
40393ded 1500 int i, j;
984263bc
MD
1501 gid_t v;
1502
1503 /* Insertion sort. */
1504 for (i = 1; i < num; i++) {
1505 v = list[i];
1506 /* find correct slot for value v, moving others up */
1507 for (j = i; --j >= 0 && v < list[j];)
1508 list[j + 1] = list[j];
1509 list[j + 1] = v;
1510 }
1511}
1512
1513/*
1514 * copy credentials making sure that the result can be compared with bcmp().
1515 */
1516void
e851b29e 1517nfsrv_setcred(struct ucred *incred, struct ucred *outcred)
984263bc 1518{
40393ded 1519 int i;
984263bc
MD
1520
1521 bzero((caddr_t)outcred, sizeof (struct ucred));
1522 outcred->cr_ref = 1;
1523 outcred->cr_uid = incred->cr_uid;
1524 outcred->cr_ngroups = incred->cr_ngroups;
1525 for (i = 0; i < incred->cr_ngroups; i++)
1526 outcred->cr_groups[i] = incred->cr_groups[i];
1527 nfsrvw_sort(outcred->cr_groups, outcred->cr_ngroups);
1528}
1529#endif /* NFS_NOSERVER */