nfs_srvsubs.c revision 41796
154359Sroberto/* 254359Sroberto * Copyright (c) 1989, 1993 354359Sroberto * The Regents of the University of California. All rights reserved. 454359Sroberto * 554359Sroberto * This code is derived from software contributed to Berkeley by 654359Sroberto * Rick Macklem at The University of Guelph. 754359Sroberto * 854359Sroberto * Redistribution and use in source and binary forms, with or without 954359Sroberto * modification, are permitted provided that the following conditions 1054359Sroberto * are met: 1154359Sroberto * 1. Redistributions of source code must retain the above copyright 1254359Sroberto * notice, this list of conditions and the following disclaimer. 1354359Sroberto * 2. Redistributions in binary form must reproduce the above copyright 1454359Sroberto * notice, this list of conditions and the following disclaimer in the 1554359Sroberto * documentation and/or other materials provided with the distribution. 1654359Sroberto * 3. All advertising materials mentioning features or use of this software 1754359Sroberto * must display the following acknowledgement: 1854359Sroberto * This product includes software developed by the University of 1954359Sroberto * California, Berkeley and its contributors. 2054359Sroberto * 4. Neither the name of the University nor the names of its contributors 2154359Sroberto * may be used to endorse or promote products derived from this software 2254359Sroberto * without specific prior written permission. 2354359Sroberto * 2454359Sroberto * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2554359Sroberto * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2654359Sroberto * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2754359Sroberto * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2854359Sroberto * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2954359Sroberto * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3054359Sroberto * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3154359Sroberto * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3254359Sroberto * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3354359Sroberto * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3454359Sroberto * SUCH DAMAGE. 3554359Sroberto * 3654359Sroberto * @(#)nfs_subs.c 8.8 (Berkeley) 5/22/95 3754359Sroberto * $Id: nfs_subs.c,v 1.68 1998/12/07 21:58:44 archie Exp $ 3854359Sroberto */ 3954359Sroberto 4054359Sroberto/* 4154359Sroberto * These functions support the macros and help fiddle mbuf chains for 4254359Sroberto * the nfs op functions. They do things like create the rpc header and 4354359Sroberto * copy data between mbuf chains and uio lists. 4454359Sroberto */ 4554359Sroberto#include <sys/param.h> 4654359Sroberto#include <sys/buf.h> 4754359Sroberto#include <sys/proc.h> 4854359Sroberto#include <sys/systm.h> 4954359Sroberto#include <sys/kernel.h> 5054359Sroberto#include <sys/mount.h> 5154359Sroberto#include <sys/vnode.h> 5254359Sroberto#include <sys/namei.h> 5354359Sroberto#include <sys/mbuf.h> 5454359Sroberto#include <sys/socket.h> 5554359Sroberto#include <sys/stat.h> 5654359Sroberto#include <sys/malloc.h> 5754359Sroberto#include <sys/sysent.h> 5854359Sroberto#include <sys/syscall.h> 5954359Sroberto 6054359Sroberto#include <vm/vm.h> 6154359Sroberto#include <vm/vm_object.h> 6254359Sroberto#include <vm/vm_extern.h> 6354359Sroberto#include <vm/vm_zone.h> 6454359Sroberto 6554359Sroberto#include <nfs/rpcv2.h> 6654359Sroberto#include <nfs/nfsproto.h> 6754359Sroberto#include <nfs/nfs.h> 6854359Sroberto#include <nfs/nfsnode.h> 6954359Sroberto#include <nfs/xdr_subs.h> 7054359Sroberto#include <nfs/nfsm_subs.h> 7154359Sroberto#include <nfs/nfsmount.h> 7254359Sroberto#include <nfs/nqnfs.h> 7354359Sroberto#include <nfs/nfsrtt.h> 7454359Sroberto 7554359Sroberto#include <miscfs/specfs/specdev.h> 7654359Sroberto 7754359Sroberto#include <netinet/in.h> 7854359Sroberto#ifdef ISO 7954359Sroberto#include <netiso/iso.h> 8054359Sroberto#endif 8154359Sroberto 8254359Sroberto/* 8354359Sroberto * Data items converted to xdr at startup, since they are constant 8454359Sroberto * This is kinda hokey, but may save a little time doing byte swaps 8554359Sroberto */ 8654359Srobertou_int32_t nfs_xdrneg1; 8754359Srobertou_int32_t rpc_call, rpc_vers, rpc_reply, rpc_msgdenied, rpc_autherr, 8854359Sroberto rpc_mismatch, rpc_auth_unix, rpc_msgaccepted, 8954359Sroberto rpc_auth_kerb; 9054359Srobertou_int32_t nfs_prog, nqnfs_prog, nfs_true, nfs_false; 9154359Sroberto 9254359Sroberto/* And other global data */ 9354359Srobertostatic u_int32_t nfs_xid = 0; 9454359Srobertostatic enum vtype nv2tov_type[8]= { 9554359Sroberto VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON, VNON 9654359Sroberto}; 9754359Srobertoenum vtype nv3tov_type[8]= { 9854359Sroberto VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO 9954359Sroberto}; 10054359Sroberto 10154359Srobertoint nfs_ticks; 10254359Sroberto 10354359Srobertostruct nfs_reqq nfs_reqq; 10454359Srobertostruct nfssvc_sockhead nfssvc_sockhead; 10554359Srobertoint nfssvc_sockhead_flag; 10654359Srobertostruct nfsd_head nfsd_head; 10754359Srobertoint nfsd_head_flag; 10854359Srobertostruct nfs_bufq nfs_bufq; 10954359Srobertostruct nqtimerhead nqtimerhead; 11054359Srobertostruct nqfhhashhead *nqfhhashtbl; 11154359Srobertou_long nqfhhash; 11254359Sroberto 11354359Srobertostatic void (*nfs_prev_lease_updatetime) __P((int)); 11454359Srobertostatic int nfs_prev_nfssvc_sy_narg; 11554359Srobertostatic sy_call_t *nfs_prev_nfssvc_sy_call; 11654359Sroberto 11754359Sroberto#ifndef NFS_NOSERVER 11854359Sroberto 11954359Srobertostatic vop_t *nfs_prev_vop_lease_check; 12054359Srobertostatic int nfs_prev_getfh_sy_narg; 12154359Srobertostatic sy_call_t *nfs_prev_getfh_sy_call; 12254359Sroberto 12354359Sroberto/* 12454359Sroberto * Mapping of old NFS Version 2 RPC numbers to generic numbers. 12554359Sroberto */ 12654359Srobertoint nfsv3_procid[NFS_NPROCS] = { 12754359Sroberto NFSPROC_NULL, 12854359Sroberto NFSPROC_GETATTR, 12954359Sroberto NFSPROC_SETATTR, 13054359Sroberto NFSPROC_NOOP, 13154359Sroberto NFSPROC_LOOKUP, 13254359Sroberto NFSPROC_READLINK, 13354359Sroberto NFSPROC_READ, 13454359Sroberto NFSPROC_NOOP, 13554359Sroberto NFSPROC_WRITE, 13654359Sroberto NFSPROC_CREATE, 13754359Sroberto NFSPROC_REMOVE, 13854359Sroberto NFSPROC_RENAME, 13954359Sroberto NFSPROC_LINK, 14054359Sroberto NFSPROC_SYMLINK, 14154359Sroberto NFSPROC_MKDIR, 14254359Sroberto NFSPROC_RMDIR, 14354359Sroberto NFSPROC_READDIR, 14454359Sroberto NFSPROC_FSSTAT, 14554359Sroberto NFSPROC_NOOP, 14654359Sroberto NFSPROC_NOOP, 14754359Sroberto NFSPROC_NOOP, 14854359Sroberto NFSPROC_NOOP, 14954359Sroberto NFSPROC_NOOP, 15054359Sroberto NFSPROC_NOOP, 15154359Sroberto NFSPROC_NOOP, 15254359Sroberto NFSPROC_NOOP 15354359Sroberto}; 15454359Sroberto 15554359Sroberto#endif /* NFS_NOSERVER */ 15654359Sroberto/* 15754359Sroberto * and the reverse mapping from generic to Version 2 procedure numbers 15854359Sroberto */ 15954359Srobertoint nfsv2_procid[NFS_NPROCS] = { 16054359Sroberto NFSV2PROC_NULL, 16154359Sroberto NFSV2PROC_GETATTR, 16254359Sroberto NFSV2PROC_SETATTR, 16354359Sroberto NFSV2PROC_LOOKUP, 16454359Sroberto NFSV2PROC_NOOP, 16554359Sroberto NFSV2PROC_READLINK, 16654359Sroberto NFSV2PROC_READ, 16754359Sroberto NFSV2PROC_WRITE, 16854359Sroberto NFSV2PROC_CREATE, 16954359Sroberto NFSV2PROC_MKDIR, 17054359Sroberto NFSV2PROC_SYMLINK, 17154359Sroberto NFSV2PROC_CREATE, 17254359Sroberto NFSV2PROC_REMOVE, 17354359Sroberto NFSV2PROC_RMDIR, 17454359Sroberto NFSV2PROC_RENAME, 17554359Sroberto NFSV2PROC_LINK, 17654359Sroberto NFSV2PROC_READDIR, 17754359Sroberto NFSV2PROC_NOOP, 17854359Sroberto NFSV2PROC_STATFS, 17954359Sroberto NFSV2PROC_NOOP, 18054359Sroberto NFSV2PROC_NOOP, 18154359Sroberto NFSV2PROC_NOOP, 18254359Sroberto NFSV2PROC_NOOP, 18354359Sroberto NFSV2PROC_NOOP, 18454359Sroberto NFSV2PROC_NOOP, 18554359Sroberto NFSV2PROC_NOOP, 18654359Sroberto}; 18754359Sroberto 18854359Sroberto#ifndef NFS_NOSERVER 18954359Sroberto/* 19054359Sroberto * Maps errno values to nfs error numbers. 19154359Sroberto * Use NFSERR_IO as the catch all for ones not specifically defined in 19254359Sroberto * RFC 1094. 19354359Sroberto */ 19454359Srobertostatic u_char nfsrv_v2errmap[ELAST] = { 19554359Sroberto NFSERR_PERM, NFSERR_NOENT, NFSERR_IO, NFSERR_IO, NFSERR_IO, 19654359Sroberto NFSERR_NXIO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 19754359Sroberto NFSERR_IO, NFSERR_IO, NFSERR_ACCES, NFSERR_IO, NFSERR_IO, 19854359Sroberto NFSERR_IO, NFSERR_EXIST, NFSERR_IO, NFSERR_NODEV, NFSERR_NOTDIR, 19954359Sroberto NFSERR_ISDIR, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 20054359Sroberto NFSERR_IO, NFSERR_FBIG, NFSERR_NOSPC, NFSERR_IO, NFSERR_ROFS, 20154359Sroberto NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 20254359Sroberto NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 20354359Sroberto NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 20454359Sroberto NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 20554359Sroberto NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 20654359Sroberto NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 20754359Sroberto NFSERR_IO, NFSERR_IO, NFSERR_NAMETOL, NFSERR_IO, NFSERR_IO, 20854359Sroberto NFSERR_NOTEMPTY, NFSERR_IO, NFSERR_IO, NFSERR_DQUOT, NFSERR_STALE, 20954359Sroberto NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 21054359Sroberto NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 21154359Sroberto NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 21254359Sroberto NFSERR_IO /* << Last is 86 */ 21354359Sroberto}; 21454359Sroberto 21554359Sroberto/* 21654359Sroberto * Maps errno values to nfs error numbers. 21754359Sroberto * Although it is not obvious whether or not NFS clients really care if 21854359Sroberto * a returned error value is in the specified list for the procedure, the 21954359Sroberto * safest thing to do is filter them appropriately. For Version 2, the 22054359Sroberto * X/Open XNFS document is the only specification that defines error values 22154359Sroberto * for each RPC (The RFC simply lists all possible error values for all RPCs), 22254359Sroberto * so I have decided to not do this for Version 2. 22354359Sroberto * The first entry is the default error return and the rest are the valid 22454359Sroberto * errors for that RPC in increasing numeric order. 22554359Sroberto */ 22654359Srobertostatic short nfsv3err_null[] = { 22754359Sroberto 0, 22854359Sroberto 0, 22954359Sroberto}; 23054359Sroberto 23154359Srobertostatic short nfsv3err_getattr[] = { 23254359Sroberto NFSERR_IO, 23354359Sroberto NFSERR_IO, 23454359Sroberto NFSERR_STALE, 23554359Sroberto NFSERR_BADHANDLE, 23654359Sroberto NFSERR_SERVERFAULT, 23754359Sroberto 0, 23854359Sroberto}; 23954359Sroberto 24054359Srobertostatic short nfsv3err_setattr[] = { 24154359Sroberto NFSERR_IO, 24254359Sroberto NFSERR_PERM, 24354359Sroberto NFSERR_IO, 24454359Sroberto NFSERR_ACCES, 24554359Sroberto NFSERR_INVAL, 24654359Sroberto NFSERR_NOSPC, 24754359Sroberto NFSERR_ROFS, 24854359Sroberto NFSERR_DQUOT, 24954359Sroberto NFSERR_STALE, 25054359Sroberto NFSERR_BADHANDLE, 25154359Sroberto NFSERR_NOT_SYNC, 25254359Sroberto NFSERR_SERVERFAULT, 25354359Sroberto 0, 25454359Sroberto}; 25554359Sroberto 25654359Srobertostatic short nfsv3err_lookup[] = { 25754359Sroberto NFSERR_IO, 25854359Sroberto NFSERR_NOENT, 25954359Sroberto NFSERR_IO, 26054359Sroberto NFSERR_ACCES, 26154359Sroberto NFSERR_NOTDIR, 26254359Sroberto NFSERR_NAMETOL, 26354359Sroberto NFSERR_STALE, 26454359Sroberto NFSERR_BADHANDLE, 26554359Sroberto NFSERR_SERVERFAULT, 26654359Sroberto 0, 26754359Sroberto}; 26854359Sroberto 26954359Srobertostatic short nfsv3err_access[] = { 27054359Sroberto NFSERR_IO, 27154359Sroberto NFSERR_IO, 27254359Sroberto NFSERR_STALE, 27354359Sroberto NFSERR_BADHANDLE, 27454359Sroberto NFSERR_SERVERFAULT, 27554359Sroberto 0, 27654359Sroberto}; 27754359Sroberto 27854359Srobertostatic short nfsv3err_readlink[] = { 27954359Sroberto NFSERR_IO, 28054359Sroberto NFSERR_IO, 28154359Sroberto NFSERR_ACCES, 28254359Sroberto NFSERR_INVAL, 28354359Sroberto NFSERR_STALE, 28454359Sroberto NFSERR_BADHANDLE, 28554359Sroberto NFSERR_NOTSUPP, 28654359Sroberto NFSERR_SERVERFAULT, 28754359Sroberto 0, 28854359Sroberto}; 28954359Sroberto 29054359Srobertostatic short nfsv3err_read[] = { 29154359Sroberto NFSERR_IO, 29254359Sroberto NFSERR_IO, 29354359Sroberto NFSERR_NXIO, 29454359Sroberto NFSERR_ACCES, 29554359Sroberto NFSERR_INVAL, 29654359Sroberto NFSERR_STALE, 29754359Sroberto NFSERR_BADHANDLE, 29854359Sroberto NFSERR_SERVERFAULT, 29954359Sroberto 0, 30054359Sroberto}; 30154359Sroberto 30254359Srobertostatic short nfsv3err_write[] = { 30354359Sroberto NFSERR_IO, 30454359Sroberto NFSERR_IO, 30554359Sroberto NFSERR_ACCES, 30654359Sroberto NFSERR_INVAL, 30754359Sroberto NFSERR_FBIG, 30854359Sroberto NFSERR_NOSPC, 30954359Sroberto NFSERR_ROFS, 310 NFSERR_DQUOT, 311 NFSERR_STALE, 312 NFSERR_BADHANDLE, 313 NFSERR_SERVERFAULT, 314 0, 315}; 316 317static short nfsv3err_create[] = { 318 NFSERR_IO, 319 NFSERR_IO, 320 NFSERR_ACCES, 321 NFSERR_EXIST, 322 NFSERR_NOTDIR, 323 NFSERR_NOSPC, 324 NFSERR_ROFS, 325 NFSERR_NAMETOL, 326 NFSERR_DQUOT, 327 NFSERR_STALE, 328 NFSERR_BADHANDLE, 329 NFSERR_NOTSUPP, 330 NFSERR_SERVERFAULT, 331 0, 332}; 333 334static short nfsv3err_mkdir[] = { 335 NFSERR_IO, 336 NFSERR_IO, 337 NFSERR_ACCES, 338 NFSERR_EXIST, 339 NFSERR_NOTDIR, 340 NFSERR_NOSPC, 341 NFSERR_ROFS, 342 NFSERR_NAMETOL, 343 NFSERR_DQUOT, 344 NFSERR_STALE, 345 NFSERR_BADHANDLE, 346 NFSERR_NOTSUPP, 347 NFSERR_SERVERFAULT, 348 0, 349}; 350 351static short nfsv3err_symlink[] = { 352 NFSERR_IO, 353 NFSERR_IO, 354 NFSERR_ACCES, 355 NFSERR_EXIST, 356 NFSERR_NOTDIR, 357 NFSERR_NOSPC, 358 NFSERR_ROFS, 359 NFSERR_NAMETOL, 360 NFSERR_DQUOT, 361 NFSERR_STALE, 362 NFSERR_BADHANDLE, 363 NFSERR_NOTSUPP, 364 NFSERR_SERVERFAULT, 365 0, 366}; 367 368static short nfsv3err_mknod[] = { 369 NFSERR_IO, 370 NFSERR_IO, 371 NFSERR_ACCES, 372 NFSERR_EXIST, 373 NFSERR_NOTDIR, 374 NFSERR_NOSPC, 375 NFSERR_ROFS, 376 NFSERR_NAMETOL, 377 NFSERR_DQUOT, 378 NFSERR_STALE, 379 NFSERR_BADHANDLE, 380 NFSERR_NOTSUPP, 381 NFSERR_SERVERFAULT, 382 NFSERR_BADTYPE, 383 0, 384}; 385 386static short nfsv3err_remove[] = { 387 NFSERR_IO, 388 NFSERR_NOENT, 389 NFSERR_IO, 390 NFSERR_ACCES, 391 NFSERR_NOTDIR, 392 NFSERR_ROFS, 393 NFSERR_NAMETOL, 394 NFSERR_STALE, 395 NFSERR_BADHANDLE, 396 NFSERR_SERVERFAULT, 397 0, 398}; 399 400static short nfsv3err_rmdir[] = { 401 NFSERR_IO, 402 NFSERR_NOENT, 403 NFSERR_IO, 404 NFSERR_ACCES, 405 NFSERR_EXIST, 406 NFSERR_NOTDIR, 407 NFSERR_INVAL, 408 NFSERR_ROFS, 409 NFSERR_NAMETOL, 410 NFSERR_NOTEMPTY, 411 NFSERR_STALE, 412 NFSERR_BADHANDLE, 413 NFSERR_NOTSUPP, 414 NFSERR_SERVERFAULT, 415 0, 416}; 417 418static short nfsv3err_rename[] = { 419 NFSERR_IO, 420 NFSERR_NOENT, 421 NFSERR_IO, 422 NFSERR_ACCES, 423 NFSERR_EXIST, 424 NFSERR_XDEV, 425 NFSERR_NOTDIR, 426 NFSERR_ISDIR, 427 NFSERR_INVAL, 428 NFSERR_NOSPC, 429 NFSERR_ROFS, 430 NFSERR_MLINK, 431 NFSERR_NAMETOL, 432 NFSERR_NOTEMPTY, 433 NFSERR_DQUOT, 434 NFSERR_STALE, 435 NFSERR_BADHANDLE, 436 NFSERR_NOTSUPP, 437 NFSERR_SERVERFAULT, 438 0, 439}; 440 441static short nfsv3err_link[] = { 442 NFSERR_IO, 443 NFSERR_IO, 444 NFSERR_ACCES, 445 NFSERR_EXIST, 446 NFSERR_XDEV, 447 NFSERR_NOTDIR, 448 NFSERR_INVAL, 449 NFSERR_NOSPC, 450 NFSERR_ROFS, 451 NFSERR_MLINK, 452 NFSERR_NAMETOL, 453 NFSERR_DQUOT, 454 NFSERR_STALE, 455 NFSERR_BADHANDLE, 456 NFSERR_NOTSUPP, 457 NFSERR_SERVERFAULT, 458 0, 459}; 460 461static short nfsv3err_readdir[] = { 462 NFSERR_IO, 463 NFSERR_IO, 464 NFSERR_ACCES, 465 NFSERR_NOTDIR, 466 NFSERR_STALE, 467 NFSERR_BADHANDLE, 468 NFSERR_BAD_COOKIE, 469 NFSERR_TOOSMALL, 470 NFSERR_SERVERFAULT, 471 0, 472}; 473 474static short nfsv3err_readdirplus[] = { 475 NFSERR_IO, 476 NFSERR_IO, 477 NFSERR_ACCES, 478 NFSERR_NOTDIR, 479 NFSERR_STALE, 480 NFSERR_BADHANDLE, 481 NFSERR_BAD_COOKIE, 482 NFSERR_NOTSUPP, 483 NFSERR_TOOSMALL, 484 NFSERR_SERVERFAULT, 485 0, 486}; 487 488static short nfsv3err_fsstat[] = { 489 NFSERR_IO, 490 NFSERR_IO, 491 NFSERR_STALE, 492 NFSERR_BADHANDLE, 493 NFSERR_SERVERFAULT, 494 0, 495}; 496 497static short nfsv3err_fsinfo[] = { 498 NFSERR_STALE, 499 NFSERR_STALE, 500 NFSERR_BADHANDLE, 501 NFSERR_SERVERFAULT, 502 0, 503}; 504 505static short nfsv3err_pathconf[] = { 506 NFSERR_STALE, 507 NFSERR_STALE, 508 NFSERR_BADHANDLE, 509 NFSERR_SERVERFAULT, 510 0, 511}; 512 513static short nfsv3err_commit[] = { 514 NFSERR_IO, 515 NFSERR_IO, 516 NFSERR_STALE, 517 NFSERR_BADHANDLE, 518 NFSERR_SERVERFAULT, 519 0, 520}; 521 522static short *nfsrv_v3errmap[] = { 523 nfsv3err_null, 524 nfsv3err_getattr, 525 nfsv3err_setattr, 526 nfsv3err_lookup, 527 nfsv3err_access, 528 nfsv3err_readlink, 529 nfsv3err_read, 530 nfsv3err_write, 531 nfsv3err_create, 532 nfsv3err_mkdir, 533 nfsv3err_symlink, 534 nfsv3err_mknod, 535 nfsv3err_remove, 536 nfsv3err_rmdir, 537 nfsv3err_rename, 538 nfsv3err_link, 539 nfsv3err_readdir, 540 nfsv3err_readdirplus, 541 nfsv3err_fsstat, 542 nfsv3err_fsinfo, 543 nfsv3err_pathconf, 544 nfsv3err_commit, 545}; 546 547#endif /* NFS_NOSERVER */ 548 549extern struct nfsrtt nfsrtt; 550extern time_t nqnfsstarttime; 551extern int nqsrv_clockskew; 552extern int nqsrv_writeslack; 553extern int nqsrv_maxlease; 554extern struct nfsstats nfsstats; 555extern int nqnfs_piggy[NFS_NPROCS]; 556extern nfstype nfsv2_type[9]; 557extern nfstype nfsv3_type[9]; 558extern struct nfsnodehashhead *nfsnodehashtbl; 559extern u_long nfsnodehash; 560 561struct getfh_args; 562extern int getfh(struct proc *, struct getfh_args *, int *); 563struct nfssvc_args; 564extern int nfssvc(struct proc *, struct nfssvc_args *, int *); 565 566LIST_HEAD(nfsnodehashhead, nfsnode); 567 568int nfs_webnamei __P((struct nameidata *, struct vnode *, struct proc *)); 569 570u_quad_t 571nfs_curusec() 572{ 573 struct timeval tv; 574 575 getmicrotime(&tv); 576 return ((u_quad_t)tv.tv_sec * 1000000 + (u_quad_t)tv.tv_usec); 577} 578 579/* 580 * Create the header for an rpc request packet 581 * The hsiz is the size of the rest of the nfs request header. 582 * (just used to decide if a cluster is a good idea) 583 */ 584struct mbuf * 585nfsm_reqh(vp, procid, hsiz, bposp) 586 struct vnode *vp; 587 u_long procid; 588 int hsiz; 589 caddr_t *bposp; 590{ 591 register struct mbuf *mb; 592 register u_int32_t *tl; 593 register caddr_t bpos; 594 struct mbuf *mb2; 595 struct nfsmount *nmp; 596 int nqflag; 597 598 MGET(mb, M_WAIT, MT_DATA); 599 if (hsiz >= MINCLSIZE) 600 MCLGET(mb, M_WAIT); 601 mb->m_len = 0; 602 bpos = mtod(mb, caddr_t); 603 604 /* 605 * For NQNFS, add lease request. 606 */ 607 if (vp) { 608 nmp = VFSTONFS(vp->v_mount); 609 if (nmp->nm_flag & NFSMNT_NQNFS) { 610 nqflag = NQNFS_NEEDLEASE(vp, procid); 611 if (nqflag) { 612 nfsm_build(tl, u_int32_t *, 2*NFSX_UNSIGNED); 613 *tl++ = txdr_unsigned(nqflag); 614 *tl = txdr_unsigned(nmp->nm_leaseterm); 615 } else { 616 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); 617 *tl = 0; 618 } 619 } 620 } 621 /* Finally, return values */ 622 *bposp = bpos; 623 return (mb); 624} 625 626/* 627 * Build the RPC header and fill in the authorization info. 628 * The authorization string argument is only used when the credentials 629 * come from outside of the kernel. 630 * Returns the head of the mbuf list. 631 */ 632struct mbuf * 633nfsm_rpchead(cr, nmflag, procid, auth_type, auth_len, auth_str, verf_len, 634 verf_str, mrest, mrest_len, mbp, xidp) 635 register struct ucred *cr; 636 int nmflag; 637 int procid; 638 int auth_type; 639 int auth_len; 640 char *auth_str; 641 int verf_len; 642 char *verf_str; 643 struct mbuf *mrest; 644 int mrest_len; 645 struct mbuf **mbp; 646 u_int32_t *xidp; 647{ 648 register struct mbuf *mb; 649 register u_int32_t *tl; 650 register caddr_t bpos; 651 register int i; 652 struct mbuf *mreq, *mb2; 653 int siz, grpsiz, authsiz; 654 655 authsiz = nfsm_rndup(auth_len); 656 MGETHDR(mb, M_WAIT, MT_DATA); 657 if ((authsiz + 10 * NFSX_UNSIGNED) >= MINCLSIZE) { 658 MCLGET(mb, M_WAIT); 659 } else if ((authsiz + 10 * NFSX_UNSIGNED) < MHLEN) { 660 MH_ALIGN(mb, authsiz + 10 * NFSX_UNSIGNED); 661 } else { 662 MH_ALIGN(mb, 8 * NFSX_UNSIGNED); 663 } 664 mb->m_len = 0; 665 mreq = mb; 666 bpos = mtod(mb, caddr_t); 667 668 /* 669 * First the RPC header. 670 */ 671 nfsm_build(tl, u_int32_t *, 8 * NFSX_UNSIGNED); 672 673 /* Get a pretty random xid to start with */ 674 if (!nfs_xid) 675 nfs_xid = random(); 676 /* 677 * Skip zero xid if it should ever happen. 678 */ 679 if (++nfs_xid == 0) 680 nfs_xid++; 681 682 *tl++ = *xidp = txdr_unsigned(nfs_xid); 683 *tl++ = rpc_call; 684 *tl++ = rpc_vers; 685 if (nmflag & NFSMNT_NQNFS) { 686 *tl++ = txdr_unsigned(NQNFS_PROG); 687 *tl++ = txdr_unsigned(NQNFS_VER3); 688 } else { 689 *tl++ = txdr_unsigned(NFS_PROG); 690 if (nmflag & NFSMNT_NFSV3) 691 *tl++ = txdr_unsigned(NFS_VER3); 692 else 693 *tl++ = txdr_unsigned(NFS_VER2); 694 } 695 if (nmflag & NFSMNT_NFSV3) 696 *tl++ = txdr_unsigned(procid); 697 else 698 *tl++ = txdr_unsigned(nfsv2_procid[procid]); 699 700 /* 701 * And then the authorization cred. 702 */ 703 *tl++ = txdr_unsigned(auth_type); 704 *tl = txdr_unsigned(authsiz); 705 switch (auth_type) { 706 case RPCAUTH_UNIX: 707 nfsm_build(tl, u_int32_t *, auth_len); 708 *tl++ = 0; /* stamp ?? */ 709 *tl++ = 0; /* NULL hostname */ 710 *tl++ = txdr_unsigned(cr->cr_uid); 711 *tl++ = txdr_unsigned(cr->cr_groups[0]); 712 grpsiz = (auth_len >> 2) - 5; 713 *tl++ = txdr_unsigned(grpsiz); 714 for (i = 1; i <= grpsiz; i++) 715 *tl++ = txdr_unsigned(cr->cr_groups[i]); 716 break; 717 case RPCAUTH_KERB4: 718 siz = auth_len; 719 while (siz > 0) { 720 if (M_TRAILINGSPACE(mb) == 0) { 721 MGET(mb2, M_WAIT, MT_DATA); 722 if (siz >= MINCLSIZE) 723 MCLGET(mb2, M_WAIT); 724 mb->m_next = mb2; 725 mb = mb2; 726 mb->m_len = 0; 727 bpos = mtod(mb, caddr_t); 728 } 729 i = min(siz, M_TRAILINGSPACE(mb)); 730 bcopy(auth_str, bpos, i); 731 mb->m_len += i; 732 auth_str += i; 733 bpos += i; 734 siz -= i; 735 } 736 if ((siz = (nfsm_rndup(auth_len) - auth_len)) > 0) { 737 for (i = 0; i < siz; i++) 738 *bpos++ = '\0'; 739 mb->m_len += siz; 740 } 741 break; 742 }; 743 744 /* 745 * And the verifier... 746 */ 747 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 748 if (verf_str) { 749 *tl++ = txdr_unsigned(RPCAUTH_KERB4); 750 *tl = txdr_unsigned(verf_len); 751 siz = verf_len; 752 while (siz > 0) { 753 if (M_TRAILINGSPACE(mb) == 0) { 754 MGET(mb2, M_WAIT, MT_DATA); 755 if (siz >= MINCLSIZE) 756 MCLGET(mb2, M_WAIT); 757 mb->m_next = mb2; 758 mb = mb2; 759 mb->m_len = 0; 760 bpos = mtod(mb, caddr_t); 761 } 762 i = min(siz, M_TRAILINGSPACE(mb)); 763 bcopy(verf_str, bpos, i); 764 mb->m_len += i; 765 verf_str += i; 766 bpos += i; 767 siz -= i; 768 } 769 if ((siz = (nfsm_rndup(verf_len) - verf_len)) > 0) { 770 for (i = 0; i < siz; i++) 771 *bpos++ = '\0'; 772 mb->m_len += siz; 773 } 774 } else { 775 *tl++ = txdr_unsigned(RPCAUTH_NULL); 776 *tl = 0; 777 } 778 mb->m_next = mrest; 779 mreq->m_pkthdr.len = authsiz + 10 * NFSX_UNSIGNED + mrest_len; 780 mreq->m_pkthdr.rcvif = (struct ifnet *)0; 781 *mbp = mb; 782 return (mreq); 783} 784 785/* 786 * copies mbuf chain to the uio scatter/gather list 787 */ 788int 789nfsm_mbuftouio(mrep, uiop, siz, dpos) 790 struct mbuf **mrep; 791 register struct uio *uiop; 792 int siz; 793 caddr_t *dpos; 794{ 795 register char *mbufcp, *uiocp; 796 register int xfer, left, len; 797 register struct mbuf *mp; 798 long uiosiz, rem; 799 int error = 0; 800 801 mp = *mrep; 802 mbufcp = *dpos; 803 len = mtod(mp, caddr_t)+mp->m_len-mbufcp; 804 rem = nfsm_rndup(siz)-siz; 805 while (siz > 0) { 806 if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL) 807 return (EFBIG); 808 left = uiop->uio_iov->iov_len; 809 uiocp = uiop->uio_iov->iov_base; 810 if (left > siz) 811 left = siz; 812 uiosiz = left; 813 while (left > 0) { 814 while (len == 0) { 815 mp = mp->m_next; 816 if (mp == NULL) 817 return (EBADRPC); 818 mbufcp = mtod(mp, caddr_t); 819 len = mp->m_len; 820 } 821 xfer = (left > len) ? len : left; 822#ifdef notdef 823 /* Not Yet.. */ 824 if (uiop->uio_iov->iov_op != NULL) 825 (*(uiop->uio_iov->iov_op)) 826 (mbufcp, uiocp, xfer); 827 else 828#endif 829 if (uiop->uio_segflg == UIO_SYSSPACE) 830 bcopy(mbufcp, uiocp, xfer); 831 else 832 copyout(mbufcp, uiocp, xfer); 833 left -= xfer; 834 len -= xfer; 835 mbufcp += xfer; 836 uiocp += xfer; 837 uiop->uio_offset += xfer; 838 uiop->uio_resid -= xfer; 839 } 840 if (uiop->uio_iov->iov_len <= siz) { 841 uiop->uio_iovcnt--; 842 uiop->uio_iov++; 843 } else { 844 uiop->uio_iov->iov_base += uiosiz; 845 uiop->uio_iov->iov_len -= uiosiz; 846 } 847 siz -= uiosiz; 848 } 849 *dpos = mbufcp; 850 *mrep = mp; 851 if (rem > 0) { 852 if (len < rem) 853 error = nfs_adv(mrep, dpos, rem, len); 854 else 855 *dpos += rem; 856 } 857 return (error); 858} 859 860/* 861 * copies a uio scatter/gather list to an mbuf chain. 862 * NOTE: can ony handle iovcnt == 1 863 */ 864int 865nfsm_uiotombuf(uiop, mq, siz, bpos) 866 register struct uio *uiop; 867 struct mbuf **mq; 868 int siz; 869 caddr_t *bpos; 870{ 871 register char *uiocp; 872 register struct mbuf *mp, *mp2; 873 register int xfer, left, mlen; 874 int uiosiz, clflg, rem; 875 char *cp; 876 877#ifdef DIAGNOSTIC 878 if (uiop->uio_iovcnt != 1) 879 panic("nfsm_uiotombuf: iovcnt != 1"); 880#endif 881 882 if (siz > MLEN) /* or should it >= MCLBYTES ?? */ 883 clflg = 1; 884 else 885 clflg = 0; 886 rem = nfsm_rndup(siz)-siz; 887 mp = mp2 = *mq; 888 while (siz > 0) { 889 left = uiop->uio_iov->iov_len; 890 uiocp = uiop->uio_iov->iov_base; 891 if (left > siz) 892 left = siz; 893 uiosiz = left; 894 while (left > 0) { 895 mlen = M_TRAILINGSPACE(mp); 896 if (mlen == 0) { 897 MGET(mp, M_WAIT, MT_DATA); 898 if (clflg) 899 MCLGET(mp, M_WAIT); 900 mp->m_len = 0; 901 mp2->m_next = mp; 902 mp2 = mp; 903 mlen = M_TRAILINGSPACE(mp); 904 } 905 xfer = (left > mlen) ? mlen : left; 906#ifdef notdef 907 /* Not Yet.. */ 908 if (uiop->uio_iov->iov_op != NULL) 909 (*(uiop->uio_iov->iov_op)) 910 (uiocp, mtod(mp, caddr_t)+mp->m_len, xfer); 911 else 912#endif 913 if (uiop->uio_segflg == UIO_SYSSPACE) 914 bcopy(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer); 915 else 916 copyin(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer); 917 mp->m_len += xfer; 918 left -= xfer; 919 uiocp += xfer; 920 uiop->uio_offset += xfer; 921 uiop->uio_resid -= xfer; 922 } 923 uiop->uio_iov->iov_base += uiosiz; 924 uiop->uio_iov->iov_len -= uiosiz; 925 siz -= uiosiz; 926 } 927 if (rem > 0) { 928 if (rem > M_TRAILINGSPACE(mp)) { 929 MGET(mp, M_WAIT, MT_DATA); 930 mp->m_len = 0; 931 mp2->m_next = mp; 932 } 933 cp = mtod(mp, caddr_t)+mp->m_len; 934 for (left = 0; left < rem; left++) 935 *cp++ = '\0'; 936 mp->m_len += rem; 937 *bpos = cp; 938 } else 939 *bpos = mtod(mp, caddr_t)+mp->m_len; 940 *mq = mp; 941 return (0); 942} 943 944/* 945 * Help break down an mbuf chain by setting the first siz bytes contiguous 946 * pointed to by returned val. 947 * This is used by the macros nfsm_dissect and nfsm_dissecton for tough 948 * cases. (The macros use the vars. dpos and dpos2) 949 */ 950int 951nfsm_disct(mdp, dposp, siz, left, cp2) 952 struct mbuf **mdp; 953 caddr_t *dposp; 954 int siz; 955 int left; 956 caddr_t *cp2; 957{ 958 register struct mbuf *mp, *mp2; 959 register int siz2, xfer; 960 register caddr_t p; 961 962 mp = *mdp; 963 while (left == 0) { 964 *mdp = mp = mp->m_next; 965 if (mp == NULL) 966 return (EBADRPC); 967 left = mp->m_len; 968 *dposp = mtod(mp, caddr_t); 969 } 970 if (left >= siz) { 971 *cp2 = *dposp; 972 *dposp += siz; 973 } else if (mp->m_next == NULL) { 974 return (EBADRPC); 975 } else if (siz > MHLEN) { 976 panic("nfs S too big"); 977 } else { 978 MGET(mp2, M_WAIT, MT_DATA); 979 mp2->m_next = mp->m_next; 980 mp->m_next = mp2; 981 mp->m_len -= left; 982 mp = mp2; 983 *cp2 = p = mtod(mp, caddr_t); 984 bcopy(*dposp, p, left); /* Copy what was left */ 985 siz2 = siz-left; 986 p += left; 987 mp2 = mp->m_next; 988 /* Loop around copying up the siz2 bytes */ 989 while (siz2 > 0) { 990 if (mp2 == NULL) 991 return (EBADRPC); 992 xfer = (siz2 > mp2->m_len) ? mp2->m_len : siz2; 993 if (xfer > 0) { 994 bcopy(mtod(mp2, caddr_t), p, xfer); 995 NFSMADV(mp2, xfer); 996 mp2->m_len -= xfer; 997 p += xfer; 998 siz2 -= xfer; 999 } 1000 if (siz2 > 0) 1001 mp2 = mp2->m_next; 1002 } 1003 mp->m_len = siz; 1004 *mdp = mp2; 1005 *dposp = mtod(mp2, caddr_t); 1006 } 1007 return (0); 1008} 1009 1010/* 1011 * Advance the position in the mbuf chain. 1012 */ 1013int 1014nfs_adv(mdp, dposp, offs, left) 1015 struct mbuf **mdp; 1016 caddr_t *dposp; 1017 int offs; 1018 int left; 1019{ 1020 register struct mbuf *m; 1021 register int s; 1022 1023 m = *mdp; 1024 s = left; 1025 while (s < offs) { 1026 offs -= s; 1027 m = m->m_next; 1028 if (m == NULL) 1029 return (EBADRPC); 1030 s = m->m_len; 1031 } 1032 *mdp = m; 1033 *dposp = mtod(m, caddr_t)+offs; 1034 return (0); 1035} 1036 1037/* 1038 * Copy a string into mbufs for the hard cases... 1039 */ 1040int 1041nfsm_strtmbuf(mb, bpos, cp, siz) 1042 struct mbuf **mb; 1043 char **bpos; 1044 const char *cp; 1045 long siz; 1046{ 1047 register struct mbuf *m1 = NULL, *m2; 1048 long left, xfer, len, tlen; 1049 u_int32_t *tl; 1050 int putsize; 1051 1052 putsize = 1; 1053 m2 = *mb; 1054 left = M_TRAILINGSPACE(m2); 1055 if (left > 0) { 1056 tl = ((u_int32_t *)(*bpos)); 1057 *tl++ = txdr_unsigned(siz); 1058 putsize = 0; 1059 left -= NFSX_UNSIGNED; 1060 m2->m_len += NFSX_UNSIGNED; 1061 if (left > 0) { 1062 bcopy(cp, (caddr_t) tl, left); 1063 siz -= left; 1064 cp += left; 1065 m2->m_len += left; 1066 left = 0; 1067 } 1068 } 1069 /* Loop around adding mbufs */ 1070 while (siz > 0) { 1071 MGET(m1, M_WAIT, MT_DATA); 1072 if (siz > MLEN) 1073 MCLGET(m1, M_WAIT); 1074 m1->m_len = NFSMSIZ(m1); 1075 m2->m_next = m1; 1076 m2 = m1; 1077 tl = mtod(m1, u_int32_t *); 1078 tlen = 0; 1079 if (putsize) { 1080 *tl++ = txdr_unsigned(siz); 1081 m1->m_len -= NFSX_UNSIGNED; 1082 tlen = NFSX_UNSIGNED; 1083 putsize = 0; 1084 } 1085 if (siz < m1->m_len) { 1086 len = nfsm_rndup(siz); 1087 xfer = siz; 1088 if (xfer < len) 1089 *(tl+(xfer>>2)) = 0; 1090 } else { 1091 xfer = len = m1->m_len; 1092 } 1093 bcopy(cp, (caddr_t) tl, xfer); 1094 m1->m_len = len+tlen; 1095 siz -= xfer; 1096 cp += xfer; 1097 } 1098 *mb = m1; 1099 *bpos = mtod(m1, caddr_t)+m1->m_len; 1100 return (0); 1101} 1102 1103/* 1104 * Called once to initialize data structures... 1105 */ 1106int 1107nfs_init(vfsp) 1108 struct vfsconf *vfsp; 1109{ 1110 register int i; 1111 1112 nfsmount_zone = zinit("NFSMOUNT", sizeof(struct nfsmount), 0, 0, 1); 1113 1114 /* 1115 * Check to see if major data structures haven't bloated. 1116 */ 1117 if (sizeof (struct nfssvc_sock) > NFS_SVCALLOC) { 1118 printf("struct nfssvc_sock bloated (> %dbytes)\n",NFS_SVCALLOC); 1119 printf("Try reducing NFS_UIDHASHSIZ\n"); 1120 } 1121 if (sizeof (struct nfsuid) > NFS_UIDALLOC) { 1122 printf("struct nfsuid bloated (> %dbytes)\n",NFS_UIDALLOC); 1123 printf("Try unionizing the nu_nickname and nu_flag fields\n"); 1124 } 1125 nfs_mount_type = vfsp->vfc_typenum; 1126 nfsrtt.pos = 0; 1127 rpc_vers = txdr_unsigned(RPC_VER2); 1128 rpc_call = txdr_unsigned(RPC_CALL); 1129 rpc_reply = txdr_unsigned(RPC_REPLY); 1130 rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED); 1131 rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED); 1132 rpc_mismatch = txdr_unsigned(RPC_MISMATCH); 1133 rpc_autherr = txdr_unsigned(RPC_AUTHERR); 1134 rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX); 1135 rpc_auth_kerb = txdr_unsigned(RPCAUTH_KERB4); 1136 nfs_prog = txdr_unsigned(NFS_PROG); 1137 nqnfs_prog = txdr_unsigned(NQNFS_PROG); 1138 nfs_true = txdr_unsigned(TRUE); 1139 nfs_false = txdr_unsigned(FALSE); 1140 nfs_xdrneg1 = txdr_unsigned(-1); 1141 nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000; 1142 if (nfs_ticks < 1) 1143 nfs_ticks = 1; 1144 /* Ensure async daemons disabled */ 1145 for (i = 0; i < NFS_MAXASYNCDAEMON; i++) { 1146 nfs_iodwant[i] = (struct proc *)0; 1147 nfs_iodmount[i] = (struct nfsmount *)0; 1148 } 1149 nfs_nhinit(); /* Init the nfsnode table */ 1150#ifndef NFS_NOSERVER 1151 nfsrv_init(0); /* Init server data structures */ 1152 nfsrv_initcache(); /* Init the server request cache */ 1153#endif 1154 1155 /* 1156 * Initialize the nqnfs server stuff. 1157 */ 1158 if (nqnfsstarttime == 0) { 1159 nqnfsstarttime = boottime.tv_sec + nqsrv_maxlease 1160 + nqsrv_clockskew + nqsrv_writeslack; 1161 NQLOADNOVRAM(nqnfsstarttime); 1162 CIRCLEQ_INIT(&nqtimerhead); 1163 nqfhhashtbl = hashinit(NQLCHSZ, M_NQLEASE, &nqfhhash); 1164 } 1165 1166 /* 1167 * Initialize reply list and start timer 1168 */ 1169 TAILQ_INIT(&nfs_reqq); 1170 1171 nfs_timer(0); 1172 1173 /* 1174 * Set up lease_check and lease_updatetime so that other parts 1175 * of the system can call us, if we are loadable. 1176 */ 1177#ifndef NFS_NOSERVER 1178 nfs_prev_vop_lease_check = default_vnodeop_p[VOFFSET(vop_lease)]; 1179 default_vnodeop_p[VOFFSET(vop_lease)] = (vop_t *)nqnfs_vop_lease_check; 1180#endif 1181 nfs_prev_lease_updatetime = lease_updatetime; 1182 lease_updatetime = nfs_lease_updatetime; 1183 nfs_prev_nfssvc_sy_narg = sysent[SYS_nfssvc].sy_narg; 1184 sysent[SYS_nfssvc].sy_narg = 2; 1185 nfs_prev_nfssvc_sy_call = sysent[SYS_nfssvc].sy_call; 1186 sysent[SYS_nfssvc].sy_call = (sy_call_t *)nfssvc; 1187#ifndef NFS_NOSERVER 1188 nfs_prev_getfh_sy_narg = sysent[SYS_getfh].sy_narg; 1189 sysent[SYS_getfh].sy_narg = 2; 1190 nfs_prev_getfh_sy_call = sysent[SYS_getfh].sy_call; 1191 sysent[SYS_getfh].sy_call = (sy_call_t *)getfh; 1192#endif 1193 1194 return (0); 1195} 1196 1197int 1198nfs_uninit(vfsp) 1199 struct vfsconf *vfsp; 1200{ 1201 1202 untimeout(nfs_timer, (void *)NULL, nfs_timer_handle); 1203 nfs_mount_type = -1; 1204#ifndef NFS_NOSERVER 1205 default_vnodeop_p[VOFFSET(vop_lease)] = nfs_prev_vop_lease_check; 1206#endif 1207 lease_updatetime = nfs_prev_lease_updatetime; 1208 sysent[SYS_nfssvc].sy_narg = nfs_prev_nfssvc_sy_narg; 1209 sysent[SYS_nfssvc].sy_call = nfs_prev_nfssvc_sy_call; 1210#ifndef NFS_NOSERVER 1211 sysent[SYS_getfh].sy_narg = nfs_prev_getfh_sy_narg; 1212 sysent[SYS_getfh].sy_call = nfs_prev_getfh_sy_call; 1213#endif 1214 return (0); 1215} 1216 1217/* 1218 * Attribute cache routines. 1219 * nfs_loadattrcache() - loads or updates the cache contents from attributes 1220 * that are on the mbuf list 1221 * nfs_getattrcache() - returns valid attributes if found in cache, returns 1222 * error otherwise 1223 */ 1224 1225/* 1226 * Load the attribute cache (that lives in the nfsnode entry) with 1227 * the values on the mbuf list and 1228 * Iff vap not NULL 1229 * copy the attributes to *vaper 1230 */ 1231int 1232nfs_loadattrcache(vpp, mdp, dposp, vaper) 1233 struct vnode **vpp; 1234 struct mbuf **mdp; 1235 caddr_t *dposp; 1236 struct vattr *vaper; 1237{ 1238 register struct vnode *vp = *vpp; 1239 register struct vattr *vap; 1240 register struct nfs_fattr *fp; 1241 register struct nfsnode *np; 1242 register int32_t t1; 1243 caddr_t cp2; 1244 int error = 0, rdev; 1245 struct mbuf *md; 1246 enum vtype vtyp; 1247 u_short vmode; 1248 struct timespec mtime; 1249 struct vnode *nvp; 1250 int v3 = NFS_ISV3(vp); 1251 1252 md = *mdp; 1253 t1 = (mtod(md, caddr_t) + md->m_len) - *dposp; 1254 if (error = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, &cp2)) 1255 return (error); 1256 fp = (struct nfs_fattr *)cp2; 1257 if (v3) { 1258 vtyp = nfsv3tov_type(fp->fa_type); 1259 vmode = fxdr_unsigned(u_short, fp->fa_mode); 1260 rdev = makedev(fxdr_unsigned(int, fp->fa3_rdev.specdata1), 1261 fxdr_unsigned(int, fp->fa3_rdev.specdata2)); 1262 fxdr_nfsv3time(&fp->fa3_mtime, &mtime); 1263 } else { 1264 vtyp = nfsv2tov_type(fp->fa_type); 1265 vmode = fxdr_unsigned(u_short, fp->fa_mode); 1266 /* 1267 * XXX 1268 * 1269 * The duplicate information returned in fa_type and fa_mode 1270 * is an ambiguity in the NFS version 2 protocol. 1271 * 1272 * VREG should be taken literally as a regular file. If a 1273 * server intents to return some type information differently 1274 * in the upper bits of the mode field (e.g. for sockets, or 1275 * FIFOs), NFSv2 mandates fa_type to be VNON. Anyway, we 1276 * leave the examination of the mode bits even in the VREG 1277 * case to avoid breakage for bogus servers, but we make sure 1278 * that there are actually type bits set in the upper part of 1279 * fa_mode (and failing that, trust the va_type field). 1280 * 1281 * NFSv3 cleared the issue, and requires fa_mode to not 1282 * contain any type information (while also introduing sockets 1283 * and FIFOs for fa_type). 1284 */ 1285 if (vtyp == VNON || (vtyp == VREG && (vmode & S_IFMT) != 0)) 1286 vtyp = IFTOVT(vmode); 1287 rdev = fxdr_unsigned(int32_t, fp->fa2_rdev); 1288 fxdr_nfsv2time(&fp->fa2_mtime, &mtime); 1289 1290 /* 1291 * Really ugly NFSv2 kludge. 1292 */ 1293 if (vtyp == VCHR && rdev == 0xffffffff) 1294 vtyp = VFIFO; 1295 } 1296 1297 /* 1298 * If v_type == VNON it is a new node, so fill in the v_type, 1299 * n_mtime fields. Check to see if it represents a special 1300 * device, and if so, check for a possible alias. Once the 1301 * correct vnode has been obtained, fill in the rest of the 1302 * information. 1303 */ 1304 np = VTONFS(vp); 1305 if (vp->v_type != vtyp) { 1306 vp->v_type = vtyp; 1307 if (vp->v_type == VFIFO) { 1308 vp->v_op = fifo_nfsv2nodeop_p; 1309 } 1310 if (vp->v_type == VCHR || vp->v_type == VBLK) { 1311 vp->v_op = spec_nfsv2nodeop_p; 1312 nvp = checkalias(vp, (dev_t)rdev, vp->v_mount); 1313 if (nvp) { 1314 /* 1315 * Discard unneeded vnode, but save its nfsnode. 1316 * Since the nfsnode does not have a lock, its 1317 * vnode lock has to be carried over. 1318 */ 1319 nvp->v_vnlock = vp->v_vnlock; 1320 vp->v_vnlock = NULL; 1321 nvp->v_data = vp->v_data; 1322 vp->v_data = NULL; 1323 vp->v_op = spec_vnodeop_p; 1324 vrele(vp); 1325 vgone(vp); 1326 /* 1327 * Reinitialize aliased node. 1328 */ 1329 np->n_vnode = nvp; 1330 *vpp = vp = nvp; 1331 } 1332 } 1333 np->n_mtime = mtime.tv_sec; 1334 } 1335 vap = &np->n_vattr; 1336 vap->va_type = vtyp; 1337 vap->va_mode = (vmode & 07777); 1338 vap->va_rdev = (dev_t)rdev; 1339 vap->va_mtime = mtime; 1340 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0]; 1341 if (v3) { 1342 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink); 1343 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid); 1344 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid); 1345 fxdr_hyper(&fp->fa3_size, &vap->va_size); 1346 vap->va_blocksize = NFS_FABLKSIZE; 1347 fxdr_hyper(&fp->fa3_used, &vap->va_bytes); 1348 vap->va_fileid = fxdr_unsigned(int32_t, 1349 fp->fa3_fileid.nfsuquad[1]); 1350 fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime); 1351 fxdr_nfsv3time(&fp->fa3_ctime, &vap->va_ctime); 1352 vap->va_flags = 0; 1353 vap->va_filerev = 0; 1354 } else { 1355 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink); 1356 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid); 1357 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid); 1358 vap->va_size = fxdr_unsigned(u_int32_t, fp->fa2_size); 1359 vap->va_blocksize = fxdr_unsigned(int32_t, fp->fa2_blocksize); 1360 vap->va_bytes = fxdr_unsigned(int32_t, fp->fa2_blocks) 1361 * NFS_FABLKSIZE; 1362 vap->va_fileid = fxdr_unsigned(int32_t, fp->fa2_fileid); 1363 fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime); 1364 vap->va_flags = 0; 1365 vap->va_ctime.tv_sec = fxdr_unsigned(u_int32_t, 1366 fp->fa2_ctime.nfsv2_sec); 1367 vap->va_ctime.tv_nsec = 0; 1368 vap->va_gen = fxdr_unsigned(u_int32_t,fp->fa2_ctime.nfsv2_usec); 1369 vap->va_filerev = 0; 1370 } 1371 if (vap->va_size != np->n_size) { 1372 if (vap->va_type == VREG) { 1373 if (np->n_flag & NMODIFIED) { 1374 if (vap->va_size < np->n_size) 1375 vap->va_size = np->n_size; 1376 else 1377 np->n_size = vap->va_size; 1378 } else 1379 np->n_size = vap->va_size; 1380 vnode_pager_setsize(vp, np->n_size); 1381 } else 1382 np->n_size = vap->va_size; 1383 } 1384 np->n_attrstamp = time_second; 1385 if (vaper != NULL) { 1386 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap)); 1387 if (np->n_flag & NCHG) { 1388 if (np->n_flag & NACC) 1389 vaper->va_atime = np->n_atim; 1390 if (np->n_flag & NUPD) 1391 vaper->va_mtime = np->n_mtim; 1392 } 1393 } 1394 return (0); 1395} 1396 1397#ifdef NFS_ACDEBUG 1398#include <sys/sysctl.h> 1399static int nfs_acdebug; 1400SYSCTL_INT(_vfs_nfs, OID_AUTO, acdebug, CTLFLAG_RW, &nfs_acdebug, 0, ""); 1401#endif 1402 1403/* 1404 * Check the time stamp 1405 * If the cache is valid, copy contents to *vap and return 0 1406 * otherwise return an error 1407 */ 1408int 1409nfs_getattrcache(vp, vaper) 1410 register struct vnode *vp; 1411 struct vattr *vaper; 1412{ 1413 register struct nfsnode *np; 1414 register struct vattr *vap; 1415 struct nfsmount *nmp; 1416 int timeo; 1417 1418 np = VTONFS(vp); 1419 vap = &np->n_vattr; 1420 nmp = VFSTONFS(vp->v_mount); 1421 /* XXX n_mtime doesn't seem to be updated on a miss-and-reload */ 1422 timeo = (time_second - np->n_mtime) / 10; 1423 1424#ifdef NFS_ACDEBUG 1425 if (nfs_acdebug>1) 1426 printf("nfs_getattrcache: initial timeo = %d\n", timeo); 1427#endif 1428 1429 if (vap->va_type == VDIR) { 1430 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acdirmin) 1431 timeo = nmp->nm_acdirmin; 1432 else if (timeo > nmp->nm_acdirmax) 1433 timeo = nmp->nm_acdirmax; 1434 } else { 1435 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acregmin) 1436 timeo = nmp->nm_acregmin; 1437 else if (timeo > nmp->nm_acregmax) 1438 timeo = nmp->nm_acregmax; 1439 } 1440 1441#ifdef NFS_ACDEBUG 1442 if (nfs_acdebug > 2) 1443 printf("acregmin %d; acregmax %d; acdirmin %d; acdirmax %d\n", 1444 nmp->nm_acregmin, nmp->nm_acregmax, 1445 nmp->nm_acdirmin, nmp->nm_acdirmax); 1446 1447 if (nfs_acdebug) 1448 printf("nfs_getattrcache: age = %d; final timeo = %d\n", 1449 (time_second - np->n_attrstamp), timeo); 1450#endif 1451 1452 if ((time_second - np->n_attrstamp) >= timeo) { 1453 nfsstats.attrcache_misses++; 1454 return (ENOENT); 1455 } 1456 nfsstats.attrcache_hits++; 1457 if (vap->va_size != np->n_size) { 1458 if (vap->va_type == VREG) { 1459 if (np->n_flag & NMODIFIED) { 1460 if (vap->va_size < np->n_size) 1461 vap->va_size = np->n_size; 1462 else 1463 np->n_size = vap->va_size; 1464 } else 1465 np->n_size = vap->va_size; 1466 vnode_pager_setsize(vp, np->n_size); 1467 } else 1468 np->n_size = vap->va_size; 1469 } 1470 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr)); 1471 if (np->n_flag & NCHG) { 1472 if (np->n_flag & NACC) 1473 vaper->va_atime = np->n_atim; 1474 if (np->n_flag & NUPD) 1475 vaper->va_mtime = np->n_mtim; 1476 } 1477 return (0); 1478} 1479 1480#ifndef NFS_NOSERVER 1481/* 1482 * Set up nameidata for a lookup() call and do it. 1483 * 1484 * If pubflag is set, this call is done for a lookup operation on the 1485 * public filehandle. In that case we allow crossing mountpoints and 1486 * absolute pathnames. However, the caller is expected to check that 1487 * the lookup result is within the public fs, and deny access if 1488 * it is not. 1489 */ 1490int 1491nfs_namei(ndp, fhp, len, slp, nam, mdp, dposp, retdirp, p, kerbflag, pubflag) 1492 register struct nameidata *ndp; 1493 fhandle_t *fhp; 1494 int len; 1495 struct nfssvc_sock *slp; 1496 struct sockaddr *nam; 1497 struct mbuf **mdp; 1498 caddr_t *dposp; 1499 struct vnode **retdirp; 1500 struct proc *p; 1501 int kerbflag, pubflag; 1502{ 1503 register int i, rem; 1504 register struct mbuf *md; 1505 register char *fromcp, *tocp, *cp; 1506 struct iovec aiov; 1507 struct uio auio; 1508 struct vnode *dp; 1509 int error, rdonly, linklen; 1510 struct componentname *cnp = &ndp->ni_cnd; 1511 1512 *retdirp = (struct vnode *)0; 1513 cnp->cn_pnbuf = zalloc(namei_zone); 1514 1515 /* 1516 * Copy the name from the mbuf list to ndp->ni_pnbuf 1517 * and set the various ndp fields appropriately. 1518 */ 1519 fromcp = *dposp; 1520 tocp = cnp->cn_pnbuf; 1521 md = *mdp; 1522 rem = mtod(md, caddr_t) + md->m_len - fromcp; 1523 cnp->cn_hash = 0; 1524 for (i = 0; i < len; i++) { 1525 while (rem == 0) { 1526 md = md->m_next; 1527 if (md == NULL) { 1528 error = EBADRPC; 1529 goto out; 1530 } 1531 fromcp = mtod(md, caddr_t); 1532 rem = md->m_len; 1533 } 1534 if (*fromcp == '\0' || (!pubflag && *fromcp == '/')) { 1535 error = EACCES; 1536 goto out; 1537 } 1538 cnp->cn_hash += (unsigned char)*fromcp; 1539 *tocp++ = *fromcp++; 1540 rem--; 1541 } 1542 *tocp = '\0'; 1543 *mdp = md; 1544 *dposp = fromcp; 1545 len = nfsm_rndup(len)-len; 1546 if (len > 0) { 1547 if (rem >= len) 1548 *dposp += len; 1549 else if ((error = nfs_adv(mdp, dposp, len, rem)) != 0) 1550 goto out; 1551 } 1552 1553 /* 1554 * Extract and set starting directory. 1555 */ 1556 error = nfsrv_fhtovp(fhp, FALSE, &dp, ndp->ni_cnd.cn_cred, slp, 1557 nam, &rdonly, kerbflag, pubflag); 1558 if (error) 1559 goto out; 1560 if (dp->v_type != VDIR) { 1561 vrele(dp); 1562 error = ENOTDIR; 1563 goto out; 1564 } 1565 1566 if (rdonly) 1567 cnp->cn_flags |= RDONLY; 1568 1569 *retdirp = dp; 1570 1571 if (pubflag) { 1572 /* 1573 * Oh joy. For WebNFS, handle those pesky '%' escapes, 1574 * and the 'native path' indicator. 1575 */ 1576 cp = zalloc(namei_zone); 1577 fromcp = cnp->cn_pnbuf; 1578 tocp = cp; 1579 if ((unsigned char)*fromcp >= WEBNFS_SPECCHAR_START) { 1580 switch ((unsigned char)*fromcp) { 1581 case WEBNFS_NATIVE_CHAR: 1582 /* 1583 * 'Native' path for us is the same 1584 * as a path according to the NFS spec, 1585 * just skip the escape char. 1586 */ 1587 fromcp++; 1588 break; 1589 /* 1590 * More may be added in the future, range 0x80-0xff 1591 */ 1592 default: 1593 error = EIO; 1594 zfree(namei_zone, cp); 1595 goto out; 1596 } 1597 } 1598 /* 1599 * Translate the '%' escapes, URL-style. 1600 */ 1601 while (*fromcp != '\0') { 1602 if (*fromcp == WEBNFS_ESC_CHAR) { 1603 if (fromcp[1] != '\0' && fromcp[2] != '\0') { 1604 fromcp++; 1605 *tocp++ = HEXSTRTOI(fromcp); 1606 fromcp += 2; 1607 continue; 1608 } else { 1609 error = ENOENT; 1610 zfree(namei_zone, cp); 1611 goto out; 1612 } 1613 } else 1614 *tocp++ = *fromcp++; 1615 } 1616 *tocp = '\0'; 1617 zfree(namei_zone, cnp->cn_pnbuf); 1618 cnp->cn_pnbuf = cp; 1619 } 1620 1621 ndp->ni_pathlen = (tocp - cnp->cn_pnbuf) + 1; 1622 ndp->ni_segflg = UIO_SYSSPACE; 1623 1624 if (pubflag) { 1625 ndp->ni_rootdir = rootvnode; 1626 ndp->ni_loopcnt = 0; 1627 if (cnp->cn_pnbuf[0] == '/') 1628 dp = rootvnode; 1629 } else { 1630 cnp->cn_flags |= NOCROSSMOUNT; 1631 } 1632 1633 cnp->cn_proc = p; 1634 VREF(dp); 1635 1636 for (;;) { 1637 cnp->cn_nameptr = cnp->cn_pnbuf; 1638 ndp->ni_startdir = dp; 1639 /* 1640 * And call lookup() to do the real work 1641 */ 1642 error = lookup(ndp); 1643 if (error) 1644 break; 1645 /* 1646 * Check for encountering a symbolic link 1647 */ 1648 if ((cnp->cn_flags & ISSYMLINK) == 0) { 1649 nfsrv_object_create(ndp->ni_vp); 1650 if (cnp->cn_flags & (SAVENAME | SAVESTART)) { 1651 cnp->cn_flags |= HASBUF; 1652 return (0); 1653 } 1654 break; 1655 } else { 1656 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1) 1657 VOP_UNLOCK(ndp->ni_dvp, 0, p); 1658 if (!pubflag) { 1659 vrele(ndp->ni_dvp); 1660 vput(ndp->ni_vp); 1661 ndp->ni_vp = NULL; 1662 error = EINVAL; 1663 break; 1664 } 1665 1666 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) { 1667 error = ELOOP; 1668 break; 1669 } 1670 if (ndp->ni_pathlen > 1) 1671 cp = zalloc(namei_zone); 1672 else 1673 cp = cnp->cn_pnbuf; 1674 aiov.iov_base = cp; 1675 aiov.iov_len = MAXPATHLEN; 1676 auio.uio_iov = &aiov; 1677 auio.uio_iovcnt = 1; 1678 auio.uio_offset = 0; 1679 auio.uio_rw = UIO_READ; 1680 auio.uio_segflg = UIO_SYSSPACE; 1681 auio.uio_procp = (struct proc *)0; 1682 auio.uio_resid = MAXPATHLEN; 1683 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred); 1684 if (error) { 1685 badlink: 1686 if (ndp->ni_pathlen > 1) 1687 zfree(namei_zone, cp); 1688 break; 1689 } 1690 linklen = MAXPATHLEN - auio.uio_resid; 1691 if (linklen == 0) { 1692 error = ENOENT; 1693 goto badlink; 1694 } 1695 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) { 1696 error = ENAMETOOLONG; 1697 goto badlink; 1698 } 1699 if (ndp->ni_pathlen > 1) { 1700 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen); 1701 zfree(namei_zone, cnp->cn_pnbuf); 1702 cnp->cn_pnbuf = cp; 1703 } else 1704 cnp->cn_pnbuf[linklen] = '\0'; 1705 ndp->ni_pathlen += linklen; 1706 vput(ndp->ni_vp); 1707 dp = ndp->ni_dvp; 1708 /* 1709 * Check if root directory should replace current directory. 1710 */ 1711 if (cnp->cn_pnbuf[0] == '/') { 1712 vrele(dp); 1713 dp = ndp->ni_rootdir; 1714 VREF(dp); 1715 } 1716 } 1717 } 1718out: 1719 zfree(namei_zone, cnp->cn_pnbuf); 1720 return (error); 1721} 1722 1723/* 1724 * A fiddled version of m_adj() that ensures null fill to a long 1725 * boundary and only trims off the back end 1726 */ 1727void 1728nfsm_adj(mp, len, nul) 1729 struct mbuf *mp; 1730 register int len; 1731 int nul; 1732{ 1733 register struct mbuf *m; 1734 register int count, i; 1735 register char *cp; 1736 1737 /* 1738 * Trim from tail. Scan the mbuf chain, 1739 * calculating its length and finding the last mbuf. 1740 * If the adjustment only affects this mbuf, then just 1741 * adjust and return. Otherwise, rescan and truncate 1742 * after the remaining size. 1743 */ 1744 count = 0; 1745 m = mp; 1746 for (;;) { 1747 count += m->m_len; 1748 if (m->m_next == (struct mbuf *)0) 1749 break; 1750 m = m->m_next; 1751 } 1752 if (m->m_len > len) { 1753 m->m_len -= len; 1754 if (nul > 0) { 1755 cp = mtod(m, caddr_t)+m->m_len-nul; 1756 for (i = 0; i < nul; i++) 1757 *cp++ = '\0'; 1758 } 1759 return; 1760 } 1761 count -= len; 1762 if (count < 0) 1763 count = 0; 1764 /* 1765 * Correct length for chain is "count". 1766 * Find the mbuf with last data, adjust its length, 1767 * and toss data from remaining mbufs on chain. 1768 */ 1769 for (m = mp; m; m = m->m_next) { 1770 if (m->m_len >= count) { 1771 m->m_len = count; 1772 if (nul > 0) { 1773 cp = mtod(m, caddr_t)+m->m_len-nul; 1774 for (i = 0; i < nul; i++) 1775 *cp++ = '\0'; 1776 } 1777 break; 1778 } 1779 count -= m->m_len; 1780 } 1781 for (m = m->m_next;m;m = m->m_next) 1782 m->m_len = 0; 1783} 1784 1785/* 1786 * Make these functions instead of macros, so that the kernel text size 1787 * doesn't get too big... 1788 */ 1789void 1790nfsm_srvwcc(nfsd, before_ret, before_vap, after_ret, after_vap, mbp, bposp) 1791 struct nfsrv_descript *nfsd; 1792 int before_ret; 1793 register struct vattr *before_vap; 1794 int after_ret; 1795 struct vattr *after_vap; 1796 struct mbuf **mbp; 1797 char **bposp; 1798{ 1799 register struct mbuf *mb = *mbp, *mb2; 1800 register char *bpos = *bposp; 1801 register u_int32_t *tl; 1802 1803 if (before_ret) { 1804 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); 1805 *tl = nfs_false; 1806 } else { 1807 nfsm_build(tl, u_int32_t *, 7 * NFSX_UNSIGNED); 1808 *tl++ = nfs_true; 1809 txdr_hyper(&(before_vap->va_size), tl); 1810 tl += 2; 1811 txdr_nfsv3time(&(before_vap->va_mtime), tl); 1812 tl += 2; 1813 txdr_nfsv3time(&(before_vap->va_ctime), tl); 1814 } 1815 *bposp = bpos; 1816 *mbp = mb; 1817 nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp); 1818} 1819 1820void 1821nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp) 1822 struct nfsrv_descript *nfsd; 1823 int after_ret; 1824 struct vattr *after_vap; 1825 struct mbuf **mbp; 1826 char **bposp; 1827{ 1828 register struct mbuf *mb = *mbp, *mb2; 1829 register char *bpos = *bposp; 1830 register u_int32_t *tl; 1831 register struct nfs_fattr *fp; 1832 1833 if (after_ret) { 1834 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); 1835 *tl = nfs_false; 1836 } else { 1837 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FATTR); 1838 *tl++ = nfs_true; 1839 fp = (struct nfs_fattr *)tl; 1840 nfsm_srvfattr(nfsd, after_vap, fp); 1841 } 1842 *mbp = mb; 1843 *bposp = bpos; 1844} 1845 1846void 1847nfsm_srvfattr(nfsd, vap, fp) 1848 register struct nfsrv_descript *nfsd; 1849 register struct vattr *vap; 1850 register struct nfs_fattr *fp; 1851{ 1852 1853 fp->fa_nlink = txdr_unsigned(vap->va_nlink); 1854 fp->fa_uid = txdr_unsigned(vap->va_uid); 1855 fp->fa_gid = txdr_unsigned(vap->va_gid); 1856 if (nfsd->nd_flag & ND_NFSV3) { 1857 fp->fa_type = vtonfsv3_type(vap->va_type); 1858 fp->fa_mode = vtonfsv3_mode(vap->va_mode); 1859 txdr_hyper(&vap->va_size, &fp->fa3_size); 1860 txdr_hyper(&vap->va_bytes, &fp->fa3_used); 1861 fp->fa3_rdev.specdata1 = txdr_unsigned(major(vap->va_rdev)); 1862 fp->fa3_rdev.specdata2 = txdr_unsigned(minor(vap->va_rdev)); 1863 fp->fa3_fsid.nfsuquad[0] = 0; 1864 fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(vap->va_fsid); 1865 fp->fa3_fileid.nfsuquad[0] = 0; 1866 fp->fa3_fileid.nfsuquad[1] = txdr_unsigned(vap->va_fileid); 1867 txdr_nfsv3time(&vap->va_atime, &fp->fa3_atime); 1868 txdr_nfsv3time(&vap->va_mtime, &fp->fa3_mtime); 1869 txdr_nfsv3time(&vap->va_ctime, &fp->fa3_ctime); 1870 } else { 1871 fp->fa_type = vtonfsv2_type(vap->va_type); 1872 fp->fa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode); 1873 fp->fa2_size = txdr_unsigned(vap->va_size); 1874 fp->fa2_blocksize = txdr_unsigned(vap->va_blocksize); 1875 if (vap->va_type == VFIFO) 1876 fp->fa2_rdev = 0xffffffff; 1877 else 1878 fp->fa2_rdev = txdr_unsigned(vap->va_rdev); 1879 fp->fa2_blocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE); 1880 fp->fa2_fsid = txdr_unsigned(vap->va_fsid); 1881 fp->fa2_fileid = txdr_unsigned(vap->va_fileid); 1882 txdr_nfsv2time(&vap->va_atime, &fp->fa2_atime); 1883 txdr_nfsv2time(&vap->va_mtime, &fp->fa2_mtime); 1884 txdr_nfsv2time(&vap->va_ctime, &fp->fa2_ctime); 1885 } 1886} 1887 1888/* 1889 * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked) 1890 * - look up fsid in mount list (if not found ret error) 1891 * - get vp and export rights by calling VFS_FHTOVP() 1892 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon 1893 * - if not lockflag unlock it with VOP_UNLOCK() 1894 */ 1895int 1896nfsrv_fhtovp(fhp, lockflag, vpp, cred, slp, nam, rdonlyp, kerbflag, pubflag) 1897 fhandle_t *fhp; 1898 int lockflag; 1899 struct vnode **vpp; 1900 struct ucred *cred; 1901 struct nfssvc_sock *slp; 1902 struct sockaddr *nam; 1903 int *rdonlyp; 1904 int kerbflag; 1905 int pubflag; 1906{ 1907 struct proc *p = curproc; /* XXX */ 1908 register struct mount *mp; 1909 register int i; 1910 struct ucred *credanon; 1911 int error, exflags; 1912#ifdef MNT_EXNORESPORT /* XXX needs mountd and /etc/exports help yet */ 1913 struct sockaddr_int *saddr; 1914#endif 1915 1916 *vpp = (struct vnode *)0; 1917 1918 if (nfs_ispublicfh(fhp)) { 1919 if (!pubflag || !nfs_pub.np_valid) 1920 return (ESTALE); 1921 fhp = &nfs_pub.np_handle; 1922 } 1923 1924 mp = vfs_getvfs(&fhp->fh_fsid); 1925 if (!mp) 1926 return (ESTALE); 1927 error = VFS_FHTOVP(mp, &fhp->fh_fid, nam, vpp, &exflags, &credanon); 1928 if (error) 1929 return (error); 1930#ifdef MNT_EXNORESPORT 1931 if (!(exflags & (MNT_EXNORESPORT|MNT_EXPUBLIC))) { 1932 saddr = (struct sockaddr_in *)nam; 1933 if (saddr->sin_family == AF_INET && 1934 ntohs(saddr->sin_port) >= IPPORT_RESERVED) { 1935 vput(*vpp); 1936 return (NFSERR_AUTHERR | AUTH_TOOWEAK); 1937 } 1938 } 1939#endif 1940 /* 1941 * Check/setup credentials. 1942 */ 1943 if (exflags & MNT_EXKERB) { 1944 if (!kerbflag) { 1945 vput(*vpp); 1946 return (NFSERR_AUTHERR | AUTH_TOOWEAK); 1947 } 1948 } else if (kerbflag) { 1949 vput(*vpp); 1950 return (NFSERR_AUTHERR | AUTH_TOOWEAK); 1951 } else if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) { 1952 cred->cr_uid = credanon->cr_uid; 1953 for (i = 0; i < credanon->cr_ngroups && i < NGROUPS; i++) 1954 cred->cr_groups[i] = credanon->cr_groups[i]; 1955 cred->cr_ngroups = i; 1956 } 1957 if (exflags & MNT_EXRDONLY) 1958 *rdonlyp = 1; 1959 else 1960 *rdonlyp = 0; 1961 1962 nfsrv_object_create(*vpp); 1963 1964 if (!lockflag) 1965 VOP_UNLOCK(*vpp, 0, p); 1966 return (0); 1967} 1968 1969 1970/* 1971 * WebNFS: check if a filehandle is a public filehandle. For v3, this 1972 * means a length of 0, for v2 it means all zeroes. nfsm_srvmtofh has 1973 * transformed this to all zeroes in both cases, so check for it. 1974 */ 1975int 1976nfs_ispublicfh(fhp) 1977 fhandle_t *fhp; 1978{ 1979 char *cp = (char *)fhp; 1980 int i; 1981 1982 for (i = 0; i < NFSX_V3FH; i++) 1983 if (*cp++ != 0) 1984 return (FALSE); 1985 return (TRUE); 1986} 1987 1988#endif /* NFS_NOSERVER */ 1989/* 1990 * This function compares two net addresses by family and returns TRUE 1991 * if they are the same host. 1992 * If there is any doubt, return FALSE. 1993 * The AF_INET family is handled as a special case so that address mbufs 1994 * don't need to be saved to store "struct in_addr", which is only 4 bytes. 1995 */ 1996int 1997netaddr_match(family, haddr, nam) 1998 int family; 1999 union nethostaddr *haddr; 2000 struct sockaddr *nam; 2001{ 2002 register struct sockaddr_in *inetaddr; 2003 2004 switch (family) { 2005 case AF_INET: 2006 inetaddr = (struct sockaddr_in *)nam; 2007 if (inetaddr->sin_family == AF_INET && 2008 inetaddr->sin_addr.s_addr == haddr->had_inetaddr) 2009 return (1); 2010 break; 2011#ifdef ISO 2012 case AF_ISO: 2013 { 2014 register struct sockaddr_iso *isoaddr1, *isoaddr2; 2015 2016 isoaddr1 = (struct sockaddr_iso *)nam; 2017 isoaddr2 = (struct sockaddr_iso *)haddr->had_nam; 2018 if (isoaddr1->siso_family == AF_ISO && 2019 isoaddr1->siso_nlen > 0 && 2020 isoaddr1->siso_nlen == isoaddr2->siso_nlen && 2021 SAME_ISOADDR(isoaddr1, isoaddr2)) 2022 return (1); 2023 break; 2024 } 2025#endif /* ISO */ 2026 default: 2027 break; 2028 }; 2029 return (0); 2030} 2031 2032static nfsuint64 nfs_nullcookie = { 0, 0 }; 2033/* 2034 * This function finds the directory cookie that corresponds to the 2035 * logical byte offset given. 2036 */ 2037nfsuint64 * 2038nfs_getcookie(np, off, add) 2039 register struct nfsnode *np; 2040 off_t off; 2041 int add; 2042{ 2043 register struct nfsdmap *dp, *dp2; 2044 register int pos; 2045 2046 pos = (uoff_t)off / NFS_DIRBLKSIZ; 2047 if (pos == 0 || off < 0) { 2048#ifdef DIAGNOSTIC 2049 if (add) 2050 panic("nfs getcookie add at <= 0"); 2051#endif 2052 return (&nfs_nullcookie); 2053 } 2054 pos--; 2055 dp = np->n_cookies.lh_first; 2056 if (!dp) { 2057 if (add) { 2058 MALLOC(dp, struct nfsdmap *, sizeof (struct nfsdmap), 2059 M_NFSDIROFF, M_WAITOK); 2060 dp->ndm_eocookie = 0; 2061 LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list); 2062 } else 2063 return ((nfsuint64 *)0); 2064 } 2065 while (pos >= NFSNUMCOOKIES) { 2066 pos -= NFSNUMCOOKIES; 2067 if (dp->ndm_list.le_next) { 2068 if (!add && dp->ndm_eocookie < NFSNUMCOOKIES && 2069 pos >= dp->ndm_eocookie) 2070 return ((nfsuint64 *)0); 2071 dp = dp->ndm_list.le_next; 2072 } else if (add) { 2073 MALLOC(dp2, struct nfsdmap *, sizeof (struct nfsdmap), 2074 M_NFSDIROFF, M_WAITOK); 2075 dp2->ndm_eocookie = 0; 2076 LIST_INSERT_AFTER(dp, dp2, ndm_list); 2077 dp = dp2; 2078 } else 2079 return ((nfsuint64 *)0); 2080 } 2081 if (pos >= dp->ndm_eocookie) { 2082 if (add) 2083 dp->ndm_eocookie = pos + 1; 2084 else 2085 return ((nfsuint64 *)0); 2086 } 2087 return (&dp->ndm_cookies[pos]); 2088} 2089 2090/* 2091 * Invalidate cached directory information, except for the actual directory 2092 * blocks (which are invalidated separately). 2093 * Done mainly to avoid the use of stale offset cookies. 2094 */ 2095void 2096nfs_invaldir(vp) 2097 register struct vnode *vp; 2098{ 2099 register struct nfsnode *np = VTONFS(vp); 2100 2101#ifdef DIAGNOSTIC 2102 if (vp->v_type != VDIR) 2103 panic("nfs: invaldir not dir"); 2104#endif 2105 np->n_direofoffset = 0; 2106 np->n_cookieverf.nfsuquad[0] = 0; 2107 np->n_cookieverf.nfsuquad[1] = 0; 2108 if (np->n_cookies.lh_first) 2109 np->n_cookies.lh_first->ndm_eocookie = 0; 2110} 2111 2112/* 2113 * The write verifier has changed (probably due to a server reboot), so all 2114 * B_NEEDCOMMIT blocks will have to be written again. Since they are on the 2115 * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT 2116 * flag. Once done the new write verifier can be set for the mount point. 2117 */ 2118void 2119nfs_clearcommit(mp) 2120 struct mount *mp; 2121{ 2122 register struct vnode *vp, *nvp; 2123 register struct buf *bp, *nbp; 2124 int s; 2125 2126 s = splbio(); 2127loop: 2128 for (vp = mp->mnt_vnodelist.lh_first; vp; vp = nvp) { 2129 if (vp->v_mount != mp) /* Paranoia */ 2130 goto loop; 2131 nvp = vp->v_mntvnodes.le_next; 2132 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) { 2133 nbp = TAILQ_NEXT(bp, b_vnbufs); 2134 if ((bp->b_flags & (B_BUSY | B_DELWRI | B_NEEDCOMMIT)) 2135 == (B_DELWRI | B_NEEDCOMMIT)) 2136 bp->b_flags &= ~B_NEEDCOMMIT; 2137 } 2138 } 2139 splx(s); 2140} 2141 2142#ifndef NFS_NOSERVER 2143/* 2144 * Map errnos to NFS error numbers. For Version 3 also filter out error 2145 * numbers not specified for the associated procedure. 2146 */ 2147int 2148nfsrv_errmap(nd, err) 2149 struct nfsrv_descript *nd; 2150 register int err; 2151{ 2152 register short *defaulterrp, *errp; 2153 2154 if (nd->nd_flag & ND_NFSV3) { 2155 if (nd->nd_procnum <= NFSPROC_COMMIT) { 2156 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum]; 2157 while (*++errp) { 2158 if (*errp == err) 2159 return (err); 2160 else if (*errp > err) 2161 break; 2162 } 2163 return ((int)*defaulterrp); 2164 } else 2165 return (err & 0xffff); 2166 } 2167 if (err <= ELAST) 2168 return ((int)nfsrv_v2errmap[err - 1]); 2169 return (NFSERR_IO); 2170} 2171 2172int 2173nfsrv_object_create(vp) 2174 struct vnode *vp; 2175{ 2176 2177 if (vp == NULL || vp->v_type != VREG) 2178 return (1); 2179 return (vfs_object_create(vp, curproc, 2180 curproc ? curproc->p_ucred : NULL, 1)); 2181} 2182 2183/* 2184 * Sort the group list in increasing numerical order. 2185 * (Insertion sort by Chris Torek, who was grossed out by the bubble sort 2186 * that used to be here.) 2187 */ 2188void 2189nfsrvw_sort(list, num) 2190 register gid_t *list; 2191 register int num; 2192{ 2193 register int i, j; 2194 gid_t v; 2195 2196 /* Insertion sort. */ 2197 for (i = 1; i < num; i++) { 2198 v = list[i]; 2199 /* find correct slot for value v, moving others up */ 2200 for (j = i; --j >= 0 && v < list[j];) 2201 list[j + 1] = list[j]; 2202 list[j + 1] = v; 2203 } 2204} 2205 2206/* 2207 * copy credentials making sure that the result can be compared with bcmp(). 2208 */ 2209void 2210nfsrv_setcred(incred, outcred) 2211 register struct ucred *incred, *outcred; 2212{ 2213 register int i; 2214 2215 bzero((caddr_t)outcred, sizeof (struct ucred)); 2216 outcred->cr_ref = 1; 2217 outcred->cr_uid = incred->cr_uid; 2218 outcred->cr_ngroups = incred->cr_ngroups; 2219 for (i = 0; i < incred->cr_ngroups; i++) 2220 outcred->cr_groups[i] = incred->cr_groups[i]; 2221 nfsrvw_sort(outcred->cr_groups, outcred->cr_ngroups); 2222} 2223#endif /* NFS_NOSERVER */ 2224