nfs_common.c revision 51138
1226031Sstas/* 2226031Sstas * Copyright (c) 1989, 1993 3226031Sstas * The Regents of the University of California. All rights reserved. 4226031Sstas * 5226031Sstas * This code is derived from software contributed to Berkeley by 6226031Sstas * Rick Macklem at The University of Guelph. 7226031Sstas * 8226031Sstas * Redistribution and use in source and binary forms, with or without 9226031Sstas * modification, are permitted provided that the following conditions 10226031Sstas * are met: 11226031Sstas * 1. Redistributions of source code must retain the above copyright 12226031Sstas * notice, this list of conditions and the following disclaimer. 13226031Sstas * 2. Redistributions in binary form must reproduce the above copyright 14226031Sstas * notice, this list of conditions and the following disclaimer in the 15226031Sstas * documentation and/or other materials provided with the distribution. 16226031Sstas * 3. All advertising materials mentioning features or use of this software 17226031Sstas * must display the following acknowledgement: 18226031Sstas * This product includes software developed by the University of 19226031Sstas * California, Berkeley and its contributors. 20226031Sstas * 4. Neither the name of the University nor the names of its contributors 21226031Sstas * may be used to endorse or promote products derived from this software 22226031Sstas * without specific prior written permission. 23226031Sstas * 24226031Sstas * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25226031Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26226031Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27226031Sstas * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28226031Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29226031Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30226031Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31226031Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32226031Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33226031Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34226031Sstas * SUCH DAMAGE. 35226031Sstas * 36226031Sstas * @(#)nfs_subs.c 8.8 (Berkeley) 5/22/95 37226031Sstas * $FreeBSD: head/sys/nfs/nfs_common.c 51138 1999-09-11 00:46:08Z alfred $ 38226031Sstas */ 39226031Sstas 40226031Sstas/* 41226031Sstas * These functions support the macros and help fiddle mbuf chains for 42226031Sstas * the nfs op functions. They do things like create the rpc header and 43226031Sstas * copy data between mbuf chains and uio lists. 44226031Sstas */ 45226031Sstas#include <sys/param.h> 46226031Sstas#include <sys/systm.h> 47226031Sstas#include <sys/kernel.h> 48226031Sstas#include <sys/buf.h> 49226031Sstas#include <sys/proc.h> 50226031Sstas#include <sys/mount.h> 51226031Sstas#include <sys/vnode.h> 52226031Sstas#include <sys/namei.h> 53226031Sstas#include <sys/mbuf.h> 54226031Sstas#include <sys/socket.h> 55226031Sstas#include <sys/stat.h> 56226031Sstas#include <sys/malloc.h> 57226031Sstas#include <sys/sysent.h> 58226031Sstas#include <sys/syscall.h> 59226031Sstas#include <sys/conf.h> 60226031Sstas 61226031Sstas#include <vm/vm.h> 62226031Sstas#include <vm/vm_object.h> 63226031Sstas#include <vm/vm_extern.h> 64226031Sstas#include <vm/vm_zone.h> 65226031Sstas 66226031Sstas#include <nfs/rpcv2.h> 67226031Sstas#include <nfs/nfsproto.h> 68226031Sstas#include <nfs/nfs.h> 69226031Sstas#include <nfs/nfsnode.h> 70226031Sstas#include <nfs/xdr_subs.h> 71226031Sstas#include <nfs/nfsm_subs.h> 72226031Sstas#include <nfs/nfsmount.h> 73226031Sstas#include <nfs/nqnfs.h> 74226031Sstas#include <nfs/nfsrtt.h> 75226031Sstas 76226031Sstas#include <netinet/in.h> 77226031Sstas#ifdef ISO 78226031Sstas#include <netiso/iso.h> 79226031Sstas#endif 80226031Sstas 81226031Sstas/* 82226031Sstas * Data items converted to xdr at startup, since they are constant 83226031Sstas * This is kinda hokey, but may save a little time doing byte swaps 84226031Sstas */ 85226031Sstasu_int32_t nfs_xdrneg1; 86226031Sstasu_int32_t rpc_call, rpc_vers, rpc_reply, rpc_msgdenied, rpc_autherr, 87226031Sstas rpc_mismatch, rpc_auth_unix, rpc_msgaccepted, 88226031Sstas rpc_auth_kerb; 89226031Sstasu_int32_t nfs_prog, nqnfs_prog, nfs_true, nfs_false; 90226031Sstas 91226031Sstas/* And other global data */ 92226031Sstasstatic u_int32_t nfs_xid = 0; 93226031Sstasstatic enum vtype nv2tov_type[8]= { 94226031Sstas VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON, VNON 95226031Sstas}; 96226031Sstasenum vtype nv3tov_type[8]= { 97226031Sstas VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO 98226031Sstas}; 99226031Sstas 100226031Sstasint nfs_ticks; 101226031Sstasint nfs_pbuf_freecnt = -1; /* start out unlimited */ 102226031Sstas 103226031Sstasstruct nfs_reqq nfs_reqq; 104226031Sstasstruct nfssvc_sockhead nfssvc_sockhead; 105226031Sstasint nfssvc_sockhead_flag; 106226031Sstasstruct nfsd_head nfsd_head; 107226031Sstasint nfsd_head_flag; 108226031Sstasstruct nfs_bufq nfs_bufq; 109226031Sstasstruct nqtimerhead nqtimerhead; 110226031Sstasstruct nqfhhashhead *nqfhhashtbl; 111226031Sstasu_long nqfhhash; 112226031Sstas 113226031Sstasstatic void (*nfs_prev_lease_updatetime) __P((int)); 114226031Sstasstatic int nfs_prev_nfssvc_sy_narg; 115226031Sstasstatic sy_call_t *nfs_prev_nfssvc_sy_call; 116226031Sstas 117226031Sstas#ifndef NFS_NOSERVER 118226031Sstas 119226031Sstasstatic vop_t *nfs_prev_vop_lease_check; 120226031Sstasstatic int nfs_prev_getfh_sy_narg; 121226031Sstasstatic sy_call_t *nfs_prev_getfh_sy_call; 122226031Sstas 123226031Sstas/* 124226031Sstas * Mapping of old NFS Version 2 RPC numbers to generic numbers. 125226031Sstas */ 126226031Sstasint nfsv3_procid[NFS_NPROCS] = { 127226031Sstas NFSPROC_NULL, 128226031Sstas NFSPROC_GETATTR, 129226031Sstas NFSPROC_SETATTR, 130226031Sstas NFSPROC_NOOP, 131226031Sstas NFSPROC_LOOKUP, 132226031Sstas NFSPROC_READLINK, 133226031Sstas NFSPROC_READ, 134226031Sstas NFSPROC_NOOP, 135226031Sstas NFSPROC_WRITE, 136226031Sstas NFSPROC_CREATE, 137226031Sstas NFSPROC_REMOVE, 138226031Sstas NFSPROC_RENAME, 139226031Sstas NFSPROC_LINK, 140226031Sstas NFSPROC_SYMLINK, 141226031Sstas NFSPROC_MKDIR, 142226031Sstas NFSPROC_RMDIR, 143226031Sstas NFSPROC_READDIR, 144226031Sstas NFSPROC_FSSTAT, 145226031Sstas NFSPROC_NOOP, 146226031Sstas NFSPROC_NOOP, 147226031Sstas NFSPROC_NOOP, 148226031Sstas NFSPROC_NOOP, 149226031Sstas NFSPROC_NOOP, 150226031Sstas NFSPROC_NOOP, 151226031Sstas NFSPROC_NOOP, 152226031Sstas NFSPROC_NOOP 153226031Sstas}; 154226031Sstas 155226031Sstas#endif /* NFS_NOSERVER */ 156226031Sstas/* 157226031Sstas * and the reverse mapping from generic to Version 2 procedure numbers 158226031Sstas */ 159226031Sstasint nfsv2_procid[NFS_NPROCS] = { 160226031Sstas NFSV2PROC_NULL, 161226031Sstas NFSV2PROC_GETATTR, 162226031Sstas NFSV2PROC_SETATTR, 163226031Sstas NFSV2PROC_LOOKUP, 164226031Sstas NFSV2PROC_NOOP, 165226031Sstas NFSV2PROC_READLINK, 166226031Sstas NFSV2PROC_READ, 167226031Sstas NFSV2PROC_WRITE, 168226031Sstas NFSV2PROC_CREATE, 169226031Sstas NFSV2PROC_MKDIR, 170226031Sstas NFSV2PROC_SYMLINK, 171226031Sstas NFSV2PROC_CREATE, 172226031Sstas NFSV2PROC_REMOVE, 173226031Sstas NFSV2PROC_RMDIR, 174226031Sstas NFSV2PROC_RENAME, 175226031Sstas NFSV2PROC_LINK, 176226031Sstas NFSV2PROC_READDIR, 177226031Sstas NFSV2PROC_NOOP, 178226031Sstas NFSV2PROC_STATFS, 179226031Sstas NFSV2PROC_NOOP, 180226031Sstas NFSV2PROC_NOOP, 181226031Sstas NFSV2PROC_NOOP, 182226031Sstas NFSV2PROC_NOOP, 183226031Sstas NFSV2PROC_NOOP, 184226031Sstas NFSV2PROC_NOOP, 185226031Sstas NFSV2PROC_NOOP, 186226031Sstas}; 187226031Sstas 188226031Sstas#ifndef NFS_NOSERVER 189226031Sstas/* 190226031Sstas * Maps errno values to nfs error numbers. 191226031Sstas * Use NFSERR_IO as the catch all for ones not specifically defined in 192226031Sstas * RFC 1094. 193226031Sstas */ 194226031Sstasstatic u_char nfsrv_v2errmap[ELAST] = { 195226031Sstas NFSERR_PERM, NFSERR_NOENT, NFSERR_IO, NFSERR_IO, NFSERR_IO, 196226031Sstas NFSERR_NXIO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 197226031Sstas NFSERR_IO, NFSERR_IO, NFSERR_ACCES, NFSERR_IO, NFSERR_IO, 198226031Sstas NFSERR_IO, NFSERR_EXIST, NFSERR_IO, NFSERR_NODEV, NFSERR_NOTDIR, 199226031Sstas NFSERR_ISDIR, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 200226031Sstas NFSERR_IO, NFSERR_FBIG, NFSERR_NOSPC, NFSERR_IO, NFSERR_ROFS, 201226031Sstas NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 202226031Sstas NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 203226031Sstas NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 204226031Sstas NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 205226031Sstas NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 206226031Sstas NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 207226031Sstas NFSERR_IO, NFSERR_IO, NFSERR_NAMETOL, NFSERR_IO, NFSERR_IO, 208226031Sstas NFSERR_NOTEMPTY, NFSERR_IO, NFSERR_IO, NFSERR_DQUOT, NFSERR_STALE, 209226031Sstas NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 210226031Sstas NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 211226031Sstas NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 212226031Sstas NFSERR_IO /* << Last is 86 */ 213226031Sstas}; 214226031Sstas 215226031Sstas/* 216226031Sstas * Maps errno values to nfs error numbers. 217226031Sstas * Although it is not obvious whether or not NFS clients really care if 218226031Sstas * a returned error value is in the specified list for the procedure, the 219226031Sstas * safest thing to do is filter them appropriately. For Version 2, the 220226031Sstas * X/Open XNFS document is the only specification that defines error values 221226031Sstas * for each RPC (The RFC simply lists all possible error values for all RPCs), 222226031Sstas * so I have decided to not do this for Version 2. 223226031Sstas * The first entry is the default error return and the rest are the valid 224226031Sstas * errors for that RPC in increasing numeric order. 225226031Sstas */ 226226031Sstasstatic short nfsv3err_null[] = { 227226031Sstas 0, 228226031Sstas 0, 229226031Sstas}; 230226031Sstas 231226031Sstasstatic short nfsv3err_getattr[] = { 232226031Sstas NFSERR_IO, 233226031Sstas NFSERR_IO, 234226031Sstas NFSERR_STALE, 235226031Sstas NFSERR_BADHANDLE, 236226031Sstas NFSERR_SERVERFAULT, 237226031Sstas 0, 238226031Sstas}; 239226031Sstas 240226031Sstasstatic short nfsv3err_setattr[] = { 241226031Sstas NFSERR_IO, 242226031Sstas NFSERR_PERM, 243226031Sstas NFSERR_IO, 244226031Sstas NFSERR_ACCES, 245226031Sstas NFSERR_INVAL, 246226031Sstas NFSERR_NOSPC, 247226031Sstas NFSERR_ROFS, 248226031Sstas NFSERR_DQUOT, 249226031Sstas NFSERR_STALE, 250226031Sstas NFSERR_BADHANDLE, 251226031Sstas NFSERR_NOT_SYNC, 252226031Sstas NFSERR_SERVERFAULT, 253226031Sstas 0, 254226031Sstas}; 255226031Sstas 256226031Sstasstatic short nfsv3err_lookup[] = { 257226031Sstas NFSERR_IO, 258226031Sstas NFSERR_NOENT, 259226031Sstas NFSERR_IO, 260226031Sstas NFSERR_ACCES, 261226031Sstas NFSERR_NOTDIR, 262226031Sstas NFSERR_NAMETOL, 263226031Sstas NFSERR_STALE, 264226031Sstas NFSERR_BADHANDLE, 265226031Sstas NFSERR_SERVERFAULT, 266226031Sstas 0, 267226031Sstas}; 268226031Sstas 269226031Sstasstatic short nfsv3err_access[] = { 270226031Sstas NFSERR_IO, 271226031Sstas NFSERR_IO, 272226031Sstas NFSERR_STALE, 273226031Sstas NFSERR_BADHANDLE, 274226031Sstas NFSERR_SERVERFAULT, 275226031Sstas 0, 276226031Sstas}; 277226031Sstas 278226031Sstasstatic short nfsv3err_readlink[] = { 279226031Sstas NFSERR_IO, 280226031Sstas NFSERR_IO, 281226031Sstas NFSERR_ACCES, 282226031Sstas NFSERR_INVAL, 283226031Sstas NFSERR_STALE, 284226031Sstas NFSERR_BADHANDLE, 285226031Sstas NFSERR_NOTSUPP, 286226031Sstas NFSERR_SERVERFAULT, 287226031Sstas 0, 288226031Sstas}; 289226031Sstas 290226031Sstasstatic short nfsv3err_read[] = { 291226031Sstas NFSERR_IO, 292226031Sstas NFSERR_IO, 293226031Sstas NFSERR_NXIO, 294226031Sstas NFSERR_ACCES, 295226031Sstas NFSERR_INVAL, 296226031Sstas NFSERR_STALE, 297226031Sstas NFSERR_BADHANDLE, 298226031Sstas NFSERR_SERVERFAULT, 299226031Sstas 0, 300226031Sstas}; 301226031Sstas 302226031Sstasstatic short nfsv3err_write[] = { 303226031Sstas NFSERR_IO, 304226031Sstas NFSERR_IO, 305226031Sstas NFSERR_ACCES, 306226031Sstas NFSERR_INVAL, 307226031Sstas NFSERR_FBIG, 308226031Sstas NFSERR_NOSPC, 309226031Sstas NFSERR_ROFS, 310226031Sstas NFSERR_DQUOT, 311226031Sstas NFSERR_STALE, 312226031Sstas NFSERR_BADHANDLE, 313226031Sstas NFSERR_SERVERFAULT, 314226031Sstas 0, 315226031Sstas}; 316226031Sstas 317226031Sstasstatic short nfsv3err_create[] = { 318226031Sstas NFSERR_IO, 319226031Sstas NFSERR_IO, 320226031Sstas NFSERR_ACCES, 321226031Sstas NFSERR_EXIST, 322226031Sstas NFSERR_NOTDIR, 323226031Sstas NFSERR_NOSPC, 324226031Sstas NFSERR_ROFS, 325226031Sstas NFSERR_NAMETOL, 326226031Sstas NFSERR_DQUOT, 327226031Sstas NFSERR_STALE, 328226031Sstas NFSERR_BADHANDLE, 329226031Sstas NFSERR_NOTSUPP, 330226031Sstas NFSERR_SERVERFAULT, 331226031Sstas 0, 332226031Sstas}; 333226031Sstas 334226031Sstasstatic short nfsv3err_mkdir[] = { 335226031Sstas NFSERR_IO, 336226031Sstas NFSERR_IO, 337226031Sstas NFSERR_ACCES, 338226031Sstas NFSERR_EXIST, 339226031Sstas NFSERR_NOTDIR, 340226031Sstas NFSERR_NOSPC, 341226031Sstas NFSERR_ROFS, 342226031Sstas NFSERR_NAMETOL, 343226031Sstas NFSERR_DQUOT, 344226031Sstas NFSERR_STALE, 345226031Sstas NFSERR_BADHANDLE, 346226031Sstas NFSERR_NOTSUPP, 347226031Sstas NFSERR_SERVERFAULT, 348226031Sstas 0, 349226031Sstas}; 350226031Sstas 351226031Sstasstatic short nfsv3err_symlink[] = { 352226031Sstas NFSERR_IO, 353226031Sstas NFSERR_IO, 354226031Sstas NFSERR_ACCES, 355226031Sstas NFSERR_EXIST, 356226031Sstas NFSERR_NOTDIR, 357226031Sstas NFSERR_NOSPC, 358226031Sstas NFSERR_ROFS, 359226031Sstas NFSERR_NAMETOL, 360226031Sstas NFSERR_DQUOT, 361226031Sstas NFSERR_STALE, 362226031Sstas NFSERR_BADHANDLE, 363226031Sstas NFSERR_NOTSUPP, 364226031Sstas NFSERR_SERVERFAULT, 365226031Sstas 0, 366226031Sstas}; 367226031Sstas 368226031Sstasstatic short nfsv3err_mknod[] = { 369226031Sstas NFSERR_IO, 370226031Sstas NFSERR_IO, 371226031Sstas NFSERR_ACCES, 372226031Sstas NFSERR_EXIST, 373226031Sstas NFSERR_NOTDIR, 374226031Sstas NFSERR_NOSPC, 375226031Sstas NFSERR_ROFS, 376226031Sstas NFSERR_NAMETOL, 377226031Sstas NFSERR_DQUOT, 378226031Sstas NFSERR_STALE, 379226031Sstas NFSERR_BADHANDLE, 380226031Sstas NFSERR_NOTSUPP, 381226031Sstas NFSERR_SERVERFAULT, 382226031Sstas NFSERR_BADTYPE, 383226031Sstas 0, 384226031Sstas}; 385226031Sstas 386226031Sstasstatic short nfsv3err_remove[] = { 387226031Sstas NFSERR_IO, 388226031Sstas NFSERR_NOENT, 389226031Sstas NFSERR_IO, 390226031Sstas NFSERR_ACCES, 391226031Sstas NFSERR_NOTDIR, 392226031Sstas NFSERR_ROFS, 393226031Sstas NFSERR_NAMETOL, 394226031Sstas NFSERR_STALE, 395226031Sstas NFSERR_BADHANDLE, 396226031Sstas NFSERR_SERVERFAULT, 397226031Sstas 0, 398226031Sstas}; 399226031Sstas 400226031Sstasstatic short nfsv3err_rmdir[] = { 401226031Sstas NFSERR_IO, 402226031Sstas NFSERR_NOENT, 403226031Sstas NFSERR_IO, 404226031Sstas NFSERR_ACCES, 405226031Sstas NFSERR_EXIST, 406226031Sstas NFSERR_NOTDIR, 407226031Sstas NFSERR_INVAL, 408226031Sstas NFSERR_ROFS, 409226031Sstas NFSERR_NAMETOL, 410226031Sstas NFSERR_NOTEMPTY, 411226031Sstas NFSERR_STALE, 412226031Sstas NFSERR_BADHANDLE, 413226031Sstas NFSERR_NOTSUPP, 414226031Sstas NFSERR_SERVERFAULT, 415226031Sstas 0, 416226031Sstas}; 417226031Sstas 418226031Sstasstatic short nfsv3err_rename[] = { 419226031Sstas NFSERR_IO, 420226031Sstas NFSERR_NOENT, 421226031Sstas NFSERR_IO, 422226031Sstas NFSERR_ACCES, 423226031Sstas NFSERR_EXIST, 424226031Sstas NFSERR_XDEV, 425226031Sstas NFSERR_NOTDIR, 426226031Sstas NFSERR_ISDIR, 427226031Sstas NFSERR_INVAL, 428226031Sstas NFSERR_NOSPC, 429226031Sstas NFSERR_ROFS, 430226031Sstas NFSERR_MLINK, 431226031Sstas NFSERR_NAMETOL, 432226031Sstas NFSERR_NOTEMPTY, 433226031Sstas NFSERR_DQUOT, 434226031Sstas NFSERR_STALE, 435226031Sstas NFSERR_BADHANDLE, 436226031Sstas NFSERR_NOTSUPP, 437226031Sstas NFSERR_SERVERFAULT, 438226031Sstas 0, 439226031Sstas}; 440226031Sstas 441226031Sstasstatic short nfsv3err_link[] = { 442226031Sstas NFSERR_IO, 443226031Sstas NFSERR_IO, 444226031Sstas NFSERR_ACCES, 445226031Sstas NFSERR_EXIST, 446226031Sstas NFSERR_XDEV, 447226031Sstas NFSERR_NOTDIR, 448226031Sstas NFSERR_INVAL, 449226031Sstas NFSERR_NOSPC, 450226031Sstas NFSERR_ROFS, 451226031Sstas NFSERR_MLINK, 452226031Sstas NFSERR_NAMETOL, 453226031Sstas NFSERR_DQUOT, 454226031Sstas NFSERR_STALE, 455226031Sstas NFSERR_BADHANDLE, 456226031Sstas NFSERR_NOTSUPP, 457226031Sstas NFSERR_SERVERFAULT, 458226031Sstas 0, 459226031Sstas}; 460226031Sstas 461226031Sstasstatic short nfsv3err_readdir[] = { 462226031Sstas NFSERR_IO, 463226031Sstas NFSERR_IO, 464226031Sstas NFSERR_ACCES, 465226031Sstas NFSERR_NOTDIR, 466226031Sstas NFSERR_STALE, 467226031Sstas NFSERR_BADHANDLE, 468226031Sstas NFSERR_BAD_COOKIE, 469226031Sstas NFSERR_TOOSMALL, 470226031Sstas NFSERR_SERVERFAULT, 471226031Sstas 0, 472226031Sstas}; 473226031Sstas 474226031Sstasstatic short nfsv3err_readdirplus[] = { 475226031Sstas NFSERR_IO, 476226031Sstas NFSERR_IO, 477226031Sstas NFSERR_ACCES, 478226031Sstas NFSERR_NOTDIR, 479226031Sstas NFSERR_STALE, 480226031Sstas NFSERR_BADHANDLE, 481226031Sstas NFSERR_BAD_COOKIE, 482226031Sstas NFSERR_NOTSUPP, 483226031Sstas NFSERR_TOOSMALL, 484226031Sstas NFSERR_SERVERFAULT, 485226031Sstas 0, 486226031Sstas}; 487226031Sstas 488226031Sstasstatic short nfsv3err_fsstat[] = { 489226031Sstas NFSERR_IO, 490226031Sstas NFSERR_IO, 491226031Sstas NFSERR_STALE, 492226031Sstas NFSERR_BADHANDLE, 493226031Sstas NFSERR_SERVERFAULT, 494226031Sstas 0, 495226031Sstas}; 496226031Sstas 497226031Sstasstatic short nfsv3err_fsinfo[] = { 498226031Sstas NFSERR_STALE, 499226031Sstas NFSERR_STALE, 500226031Sstas NFSERR_BADHANDLE, 501226031Sstas NFSERR_SERVERFAULT, 502226031Sstas 0, 503226031Sstas}; 504226031Sstas 505226031Sstasstatic short nfsv3err_pathconf[] = { 506226031Sstas NFSERR_STALE, 507226031Sstas NFSERR_STALE, 508226031Sstas NFSERR_BADHANDLE, 509226031Sstas NFSERR_SERVERFAULT, 510226031Sstas 0, 511226031Sstas}; 512226031Sstas 513226031Sstasstatic short nfsv3err_commit[] = { 514226031Sstas NFSERR_IO, 515226031Sstas NFSERR_IO, 516226031Sstas NFSERR_STALE, 517226031Sstas NFSERR_BADHANDLE, 518226031Sstas NFSERR_SERVERFAULT, 519226031Sstas 0, 520226031Sstas}; 521226031Sstas 522226031Sstasstatic short *nfsrv_v3errmap[] = { 523226031Sstas nfsv3err_null, 524226031Sstas nfsv3err_getattr, 525226031Sstas nfsv3err_setattr, 526226031Sstas nfsv3err_lookup, 527226031Sstas nfsv3err_access, 528226031Sstas nfsv3err_readlink, 529226031Sstas nfsv3err_read, 530226031Sstas nfsv3err_write, 531226031Sstas nfsv3err_create, 532226031Sstas nfsv3err_mkdir, 533226031Sstas nfsv3err_symlink, 534226031Sstas nfsv3err_mknod, 535226031Sstas nfsv3err_remove, 536226031Sstas nfsv3err_rmdir, 537226031Sstas nfsv3err_rename, 538226031Sstas nfsv3err_link, 539226031Sstas nfsv3err_readdir, 540226031Sstas nfsv3err_readdirplus, 541226031Sstas nfsv3err_fsstat, 542226031Sstas nfsv3err_fsinfo, 543226031Sstas nfsv3err_pathconf, 544226031Sstas nfsv3err_commit, 545226031Sstas}; 546226031Sstas 547226031Sstas#endif /* NFS_NOSERVER */ 548226031Sstas 549226031Sstasextern struct nfsrtt nfsrtt; 550226031Sstasextern time_t nqnfsstarttime; 551226031Sstasextern int nqsrv_clockskew; 552226031Sstasextern int nqsrv_writeslack; 553226031Sstasextern int nqsrv_maxlease; 554226031Sstasextern struct nfsstats nfsstats; 555226031Sstasextern int nqnfs_piggy[NFS_NPROCS]; 556226031Sstasextern nfstype nfsv2_type[9]; 557226031Sstasextern nfstype nfsv3_type[9]; 558226031Sstasextern struct nfsnodehashhead *nfsnodehashtbl; 559226031Sstasextern 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 nfs_pbuf_freecnt = nswbuf / 2 + 1; 1195 1196 return (0); 1197} 1198 1199int 1200nfs_uninit(vfsp) 1201 struct vfsconf *vfsp; 1202{ 1203 1204 untimeout(nfs_timer, (void *)NULL, nfs_timer_handle); 1205 nfs_mount_type = -1; 1206#ifndef NFS_NOSERVER 1207 default_vnodeop_p[VOFFSET(vop_lease)] = nfs_prev_vop_lease_check; 1208#endif 1209 lease_updatetime = nfs_prev_lease_updatetime; 1210 sysent[SYS_nfssvc].sy_narg = nfs_prev_nfssvc_sy_narg; 1211 sysent[SYS_nfssvc].sy_call = nfs_prev_nfssvc_sy_call; 1212#ifndef NFS_NOSERVER 1213 sysent[SYS_getfh].sy_narg = nfs_prev_getfh_sy_narg; 1214 sysent[SYS_getfh].sy_call = nfs_prev_getfh_sy_call; 1215#endif 1216 return (0); 1217} 1218 1219/* 1220 * Attribute cache routines. 1221 * nfs_loadattrcache() - loads or updates the cache contents from attributes 1222 * that are on the mbuf list 1223 * nfs_getattrcache() - returns valid attributes if found in cache, returns 1224 * error otherwise 1225 */ 1226 1227/* 1228 * Load the attribute cache (that lives in the nfsnode entry) with 1229 * the values on the mbuf list and 1230 * Iff vap not NULL 1231 * copy the attributes to *vaper 1232 */ 1233int 1234nfs_loadattrcache(vpp, mdp, dposp, vaper) 1235 struct vnode **vpp; 1236 struct mbuf **mdp; 1237 caddr_t *dposp; 1238 struct vattr *vaper; 1239{ 1240 register struct vnode *vp = *vpp; 1241 register struct vattr *vap; 1242 register struct nfs_fattr *fp; 1243 register struct nfsnode *np; 1244 register int32_t t1; 1245 caddr_t cp2; 1246 int error = 0, rdev; 1247 struct mbuf *md; 1248 enum vtype vtyp; 1249 u_short vmode; 1250 struct timespec mtime; 1251 int v3 = NFS_ISV3(vp); 1252 1253 md = *mdp; 1254 t1 = (mtod(md, caddr_t) + md->m_len) - *dposp; 1255 if ((error = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, &cp2)) != 0) 1256 return (error); 1257 fp = (struct nfs_fattr *)cp2; 1258 if (v3) { 1259 vtyp = nfsv3tov_type(fp->fa_type); 1260 vmode = fxdr_unsigned(u_short, fp->fa_mode); 1261 rdev = makeudev(fxdr_unsigned(int, fp->fa3_rdev.specdata1), 1262 fxdr_unsigned(int, fp->fa3_rdev.specdata2)); 1263 fxdr_nfsv3time(&fp->fa3_mtime, &mtime); 1264 } else { 1265 vtyp = nfsv2tov_type(fp->fa_type); 1266 vmode = fxdr_unsigned(u_short, fp->fa_mode); 1267 /* 1268 * XXX 1269 * 1270 * The duplicate information returned in fa_type and fa_mode 1271 * is an ambiguity in the NFS version 2 protocol. 1272 * 1273 * VREG should be taken literally as a regular file. If a 1274 * server intents to return some type information differently 1275 * in the upper bits of the mode field (e.g. for sockets, or 1276 * FIFOs), NFSv2 mandates fa_type to be VNON. Anyway, we 1277 * leave the examination of the mode bits even in the VREG 1278 * case to avoid breakage for bogus servers, but we make sure 1279 * that there are actually type bits set in the upper part of 1280 * fa_mode (and failing that, trust the va_type field). 1281 * 1282 * NFSv3 cleared the issue, and requires fa_mode to not 1283 * contain any type information (while also introduing sockets 1284 * and FIFOs for fa_type). 1285 */ 1286 if (vtyp == VNON || (vtyp == VREG && (vmode & S_IFMT) != 0)) 1287 vtyp = IFTOVT(vmode); 1288 rdev = fxdr_unsigned(int32_t, fp->fa2_rdev); 1289 fxdr_nfsv2time(&fp->fa2_mtime, &mtime); 1290 1291 /* 1292 * Really ugly NFSv2 kludge. 1293 */ 1294 if (vtyp == VCHR && rdev == 0xffffffff) 1295 vtyp = VFIFO; 1296 } 1297 1298 /* 1299 * If v_type == VNON it is a new node, so fill in the v_type, 1300 * n_mtime fields. Check to see if it represents a special 1301 * device, and if so, check for a possible alias. Once the 1302 * correct vnode has been obtained, fill in the rest of the 1303 * information. 1304 */ 1305 np = VTONFS(vp); 1306 if (vp->v_type != vtyp) { 1307 vp->v_type = vtyp; 1308 if (vp->v_type == VFIFO) { 1309 vp->v_op = fifo_nfsv2nodeop_p; 1310 } 1311 if (vp->v_type == VCHR || vp->v_type == VBLK) { 1312 vp->v_op = spec_nfsv2nodeop_p; 1313 addaliasu(vp, rdev); 1314 } 1315 np->n_mtime = mtime.tv_sec; 1316 } 1317 vap = &np->n_vattr; 1318 vap->va_type = vtyp; 1319 vap->va_mode = (vmode & 07777); 1320 vap->va_rdev = rdev; 1321 vap->va_mtime = mtime; 1322 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0]; 1323 if (v3) { 1324 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink); 1325 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid); 1326 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid); 1327 vap->va_size = fxdr_hyper(&fp->fa3_size); 1328 vap->va_blocksize = NFS_FABLKSIZE; 1329 vap->va_bytes = fxdr_hyper(&fp->fa3_used); 1330 vap->va_fileid = fxdr_unsigned(int32_t, 1331 fp->fa3_fileid.nfsuquad[1]); 1332 fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime); 1333 fxdr_nfsv3time(&fp->fa3_ctime, &vap->va_ctime); 1334 vap->va_flags = 0; 1335 vap->va_filerev = 0; 1336 } else { 1337 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink); 1338 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid); 1339 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid); 1340 vap->va_size = fxdr_unsigned(u_int32_t, fp->fa2_size); 1341 vap->va_blocksize = fxdr_unsigned(int32_t, fp->fa2_blocksize); 1342 vap->va_bytes = (u_quad_t)fxdr_unsigned(int32_t, fp->fa2_blocks) 1343 * NFS_FABLKSIZE; 1344 vap->va_fileid = fxdr_unsigned(int32_t, fp->fa2_fileid); 1345 fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime); 1346 vap->va_flags = 0; 1347 vap->va_ctime.tv_sec = fxdr_unsigned(u_int32_t, 1348 fp->fa2_ctime.nfsv2_sec); 1349 vap->va_ctime.tv_nsec = 0; 1350 vap->va_gen = fxdr_unsigned(u_int32_t,fp->fa2_ctime.nfsv2_usec); 1351 vap->va_filerev = 0; 1352 } 1353 if (vap->va_size != np->n_size) { 1354 if (vap->va_type == VREG) { 1355 if (np->n_flag & NMODIFIED) { 1356 if (vap->va_size < np->n_size) 1357 vap->va_size = np->n_size; 1358 else 1359 np->n_size = vap->va_size; 1360 } else 1361 np->n_size = vap->va_size; 1362 vnode_pager_setsize(vp, np->n_size); 1363 } else 1364 np->n_size = vap->va_size; 1365 } 1366 np->n_attrstamp = time_second; 1367 if (vaper != NULL) { 1368 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap)); 1369 if (np->n_flag & NCHG) { 1370 if (np->n_flag & NACC) 1371 vaper->va_atime = np->n_atim; 1372 if (np->n_flag & NUPD) 1373 vaper->va_mtime = np->n_mtim; 1374 } 1375 } 1376 return (0); 1377} 1378 1379#ifdef NFS_ACDEBUG 1380#include <sys/sysctl.h> 1381SYSCTL_DECL(_vfs_nfs); 1382static int nfs_acdebug; 1383SYSCTL_INT(_vfs_nfs, OID_AUTO, acdebug, CTLFLAG_RW, &nfs_acdebug, 0, ""); 1384#endif 1385 1386/* 1387 * Check the time stamp 1388 * If the cache is valid, copy contents to *vap and return 0 1389 * otherwise return an error 1390 */ 1391int 1392nfs_getattrcache(vp, vaper) 1393 register struct vnode *vp; 1394 struct vattr *vaper; 1395{ 1396 register struct nfsnode *np; 1397 register struct vattr *vap; 1398 struct nfsmount *nmp; 1399 int timeo; 1400 1401 np = VTONFS(vp); 1402 vap = &np->n_vattr; 1403 nmp = VFSTONFS(vp->v_mount); 1404 /* XXX n_mtime doesn't seem to be updated on a miss-and-reload */ 1405 timeo = (time_second - np->n_mtime) / 10; 1406 1407#ifdef NFS_ACDEBUG 1408 if (nfs_acdebug>1) 1409 printf("nfs_getattrcache: initial timeo = %d\n", timeo); 1410#endif 1411 1412 if (vap->va_type == VDIR) { 1413 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acdirmin) 1414 timeo = nmp->nm_acdirmin; 1415 else if (timeo > nmp->nm_acdirmax) 1416 timeo = nmp->nm_acdirmax; 1417 } else { 1418 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acregmin) 1419 timeo = nmp->nm_acregmin; 1420 else if (timeo > nmp->nm_acregmax) 1421 timeo = nmp->nm_acregmax; 1422 } 1423 1424#ifdef NFS_ACDEBUG 1425 if (nfs_acdebug > 2) 1426 printf("acregmin %d; acregmax %d; acdirmin %d; acdirmax %d\n", 1427 nmp->nm_acregmin, nmp->nm_acregmax, 1428 nmp->nm_acdirmin, nmp->nm_acdirmax); 1429 1430 if (nfs_acdebug) 1431 printf("nfs_getattrcache: age = %d; final timeo = %d\n", 1432 (time_second - np->n_attrstamp), timeo); 1433#endif 1434 1435 if ((time_second - np->n_attrstamp) >= timeo) { 1436 nfsstats.attrcache_misses++; 1437 return (ENOENT); 1438 } 1439 nfsstats.attrcache_hits++; 1440 if (vap->va_size != np->n_size) { 1441 if (vap->va_type == VREG) { 1442 if (np->n_flag & NMODIFIED) { 1443 if (vap->va_size < np->n_size) 1444 vap->va_size = np->n_size; 1445 else 1446 np->n_size = vap->va_size; 1447 } else 1448 np->n_size = vap->va_size; 1449 vnode_pager_setsize(vp, np->n_size); 1450 } else 1451 np->n_size = vap->va_size; 1452 } 1453 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr)); 1454 if (np->n_flag & NCHG) { 1455 if (np->n_flag & NACC) 1456 vaper->va_atime = np->n_atim; 1457 if (np->n_flag & NUPD) 1458 vaper->va_mtime = np->n_mtim; 1459 } 1460 return (0); 1461} 1462 1463#ifndef NFS_NOSERVER 1464/* 1465 * Set up nameidata for a lookup() call and do it. 1466 * 1467 * If pubflag is set, this call is done for a lookup operation on the 1468 * public filehandle. In that case we allow crossing mountpoints and 1469 * absolute pathnames. However, the caller is expected to check that 1470 * the lookup result is within the public fs, and deny access if 1471 * it is not. 1472 * 1473 * nfs_namei() clears out garbage fields that namei() might leave garbage. 1474 * This is mainly ni_vp and ni_dvp when an error occurs, and ni_dvp when no 1475 * error occurs but the parent was not requested. 1476 * 1477 * dirp may be set whether an error is returned or not, and must be 1478 * released by the caller. 1479 */ 1480int 1481nfs_namei(ndp, fhp, len, slp, nam, mdp, dposp, retdirp, p, kerbflag, pubflag) 1482 register struct nameidata *ndp; 1483 fhandle_t *fhp; 1484 int len; 1485 struct nfssvc_sock *slp; 1486 struct sockaddr *nam; 1487 struct mbuf **mdp; 1488 caddr_t *dposp; 1489 struct vnode **retdirp; 1490 struct proc *p; 1491 int kerbflag, pubflag; 1492{ 1493 register int i, rem; 1494 register struct mbuf *md; 1495 register char *fromcp, *tocp, *cp; 1496 struct iovec aiov; 1497 struct uio auio; 1498 struct vnode *dp; 1499 int error, rdonly, linklen; 1500 struct componentname *cnp = &ndp->ni_cnd; 1501 1502 *retdirp = (struct vnode *)0; 1503 cnp->cn_pnbuf = zalloc(namei_zone); 1504 1505 /* 1506 * Copy the name from the mbuf list to ndp->ni_pnbuf 1507 * and set the various ndp fields appropriately. 1508 */ 1509 fromcp = *dposp; 1510 tocp = cnp->cn_pnbuf; 1511 md = *mdp; 1512 rem = mtod(md, caddr_t) + md->m_len - fromcp; 1513 cnp->cn_hash = 0; 1514 for (i = 0; i < len; i++) { 1515 while (rem == 0) { 1516 md = md->m_next; 1517 if (md == NULL) { 1518 error = EBADRPC; 1519 goto out; 1520 } 1521 fromcp = mtod(md, caddr_t); 1522 rem = md->m_len; 1523 } 1524 if (*fromcp == '\0' || (!pubflag && *fromcp == '/')) { 1525 error = EACCES; 1526 goto out; 1527 } 1528 cnp->cn_hash += (unsigned char)*fromcp; 1529 *tocp++ = *fromcp++; 1530 rem--; 1531 } 1532 *tocp = '\0'; 1533 *mdp = md; 1534 *dposp = fromcp; 1535 len = nfsm_rndup(len)-len; 1536 if (len > 0) { 1537 if (rem >= len) 1538 *dposp += len; 1539 else if ((error = nfs_adv(mdp, dposp, len, rem)) != 0) 1540 goto out; 1541 } 1542 1543 /* 1544 * Extract and set starting directory. 1545 */ 1546 error = nfsrv_fhtovp(fhp, FALSE, &dp, ndp->ni_cnd.cn_cred, slp, 1547 nam, &rdonly, kerbflag, pubflag); 1548 if (error) 1549 goto out; 1550 if (dp->v_type != VDIR) { 1551 vrele(dp); 1552 error = ENOTDIR; 1553 goto out; 1554 } 1555 1556 if (rdonly) 1557 cnp->cn_flags |= RDONLY; 1558 1559 /* 1560 * Set return directory. Reference to dp is implicitly transfered 1561 * to the returned pointer 1562 */ 1563 *retdirp = dp; 1564 1565 if (pubflag) { 1566 /* 1567 * Oh joy. For WebNFS, handle those pesky '%' escapes, 1568 * and the 'native path' indicator. 1569 */ 1570 cp = zalloc(namei_zone); 1571 fromcp = cnp->cn_pnbuf; 1572 tocp = cp; 1573 if ((unsigned char)*fromcp >= WEBNFS_SPECCHAR_START) { 1574 switch ((unsigned char)*fromcp) { 1575 case WEBNFS_NATIVE_CHAR: 1576 /* 1577 * 'Native' path for us is the same 1578 * as a path according to the NFS spec, 1579 * just skip the escape char. 1580 */ 1581 fromcp++; 1582 break; 1583 /* 1584 * More may be added in the future, range 0x80-0xff 1585 */ 1586 default: 1587 error = EIO; 1588 zfree(namei_zone, cp); 1589 goto out; 1590 } 1591 } 1592 /* 1593 * Translate the '%' escapes, URL-style. 1594 */ 1595 while (*fromcp != '\0') { 1596 if (*fromcp == WEBNFS_ESC_CHAR) { 1597 if (fromcp[1] != '\0' && fromcp[2] != '\0') { 1598 fromcp++; 1599 *tocp++ = HEXSTRTOI(fromcp); 1600 fromcp += 2; 1601 continue; 1602 } else { 1603 error = ENOENT; 1604 zfree(namei_zone, cp); 1605 goto out; 1606 } 1607 } else 1608 *tocp++ = *fromcp++; 1609 } 1610 *tocp = '\0'; 1611 zfree(namei_zone, cnp->cn_pnbuf); 1612 cnp->cn_pnbuf = cp; 1613 } 1614 1615 ndp->ni_pathlen = (tocp - cnp->cn_pnbuf) + 1; 1616 ndp->ni_segflg = UIO_SYSSPACE; 1617 1618 if (pubflag) { 1619 ndp->ni_rootdir = rootvnode; 1620 ndp->ni_loopcnt = 0; 1621 if (cnp->cn_pnbuf[0] == '/') 1622 dp = rootvnode; 1623 } else { 1624 cnp->cn_flags |= NOCROSSMOUNT; 1625 } 1626 1627 /* 1628 * Initialize for scan, set ni_startdir and bump ref on dp again 1629 * becuase lookup() will dereference ni_startdir. 1630 */ 1631 1632 cnp->cn_proc = p; 1633 VREF(dp); 1634 ndp->ni_startdir = dp; 1635 1636 for (;;) { 1637 cnp->cn_nameptr = cnp->cn_pnbuf; 1638 /* 1639 * Call lookup() to do the real work. If an error occurs, 1640 * ndp->ni_vp and ni_dvp are left uninitialized or NULL and 1641 * we do not have to dereference anything before returning. 1642 * In either case ni_startdir will be dereferenced and NULLed 1643 * out. 1644 */ 1645 error = lookup(ndp); 1646 if (error) 1647 break; 1648 1649 /* 1650 * Check for encountering a symbolic link. Trivial 1651 * termination occurs if no symlink encountered. 1652 * Note: zfree is safe because error is 0, so we will 1653 * not zfree it again when we break. 1654 */ 1655 if ((cnp->cn_flags & ISSYMLINK) == 0) { 1656 nfsrv_object_create(ndp->ni_vp); 1657 if (cnp->cn_flags & (SAVENAME | SAVESTART)) 1658 cnp->cn_flags |= HASBUF; 1659 else 1660 zfree(namei_zone, cnp->cn_pnbuf); 1661 break; 1662 } 1663 1664 /* 1665 * Validate symlink 1666 */ 1667 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1) 1668 VOP_UNLOCK(ndp->ni_dvp, 0, p); 1669 if (!pubflag) { 1670 error = EINVAL; 1671 goto badlink2; 1672 } 1673 1674 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) { 1675 error = ELOOP; 1676 goto badlink2; 1677 } 1678 if (ndp->ni_pathlen > 1) 1679 cp = zalloc(namei_zone); 1680 else 1681 cp = cnp->cn_pnbuf; 1682 aiov.iov_base = cp; 1683 aiov.iov_len = MAXPATHLEN; 1684 auio.uio_iov = &aiov; 1685 auio.uio_iovcnt = 1; 1686 auio.uio_offset = 0; 1687 auio.uio_rw = UIO_READ; 1688 auio.uio_segflg = UIO_SYSSPACE; 1689 auio.uio_procp = (struct proc *)0; 1690 auio.uio_resid = MAXPATHLEN; 1691 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred); 1692 if (error) { 1693 badlink1: 1694 if (ndp->ni_pathlen > 1) 1695 zfree(namei_zone, cp); 1696 badlink2: 1697 vrele(ndp->ni_dvp); 1698 vput(ndp->ni_vp); 1699 break; 1700 } 1701 linklen = MAXPATHLEN - auio.uio_resid; 1702 if (linklen == 0) { 1703 error = ENOENT; 1704 goto badlink1; 1705 } 1706 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) { 1707 error = ENAMETOOLONG; 1708 goto badlink1; 1709 } 1710 1711 /* 1712 * Adjust or replace path 1713 */ 1714 if (ndp->ni_pathlen > 1) { 1715 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen); 1716 zfree(namei_zone, cnp->cn_pnbuf); 1717 cnp->cn_pnbuf = cp; 1718 } else 1719 cnp->cn_pnbuf[linklen] = '\0'; 1720 ndp->ni_pathlen += linklen; 1721 1722 /* 1723 * Cleanup refs for next loop and check if root directory 1724 * should replace current directory. Normally ni_dvp 1725 * becomes the new base directory and is cleaned up when 1726 * we loop. Explicitly null pointers after invalidation 1727 * to clarify operation. 1728 */ 1729 vput(ndp->ni_vp); 1730 ndp->ni_vp = NULL; 1731 1732 if (cnp->cn_pnbuf[0] == '/') { 1733 vrele(ndp->ni_dvp); 1734 ndp->ni_dvp = ndp->ni_rootdir; 1735 VREF(ndp->ni_dvp); 1736 } 1737 ndp->ni_startdir = ndp->ni_dvp; 1738 ndp->ni_dvp = NULL; 1739 } 1740 1741 /* 1742 * nfs_namei() guarentees that fields will not contain garbage 1743 * whether an error occurs or not. This allows the caller to track 1744 * cleanup state trivially. 1745 */ 1746out: 1747 if (error) { 1748 zfree(namei_zone, cnp->cn_pnbuf); 1749 ndp->ni_vp = NULL; 1750 ndp->ni_dvp = NULL; 1751 ndp->ni_startdir = NULL; 1752 cnp->cn_flags &= ~HASBUF; 1753 } else if ((ndp->ni_cnd.cn_flags & (WANTPARENT|LOCKPARENT)) == 0) { 1754 ndp->ni_dvp = NULL; 1755 } 1756 return (error); 1757} 1758 1759/* 1760 * A fiddled version of m_adj() that ensures null fill to a long 1761 * boundary and only trims off the back end 1762 */ 1763void 1764nfsm_adj(mp, len, nul) 1765 struct mbuf *mp; 1766 register int len; 1767 int nul; 1768{ 1769 register struct mbuf *m; 1770 register int count, i; 1771 register char *cp; 1772 1773 /* 1774 * Trim from tail. Scan the mbuf chain, 1775 * calculating its length and finding the last mbuf. 1776 * If the adjustment only affects this mbuf, then just 1777 * adjust and return. Otherwise, rescan and truncate 1778 * after the remaining size. 1779 */ 1780 count = 0; 1781 m = mp; 1782 for (;;) { 1783 count += m->m_len; 1784 if (m->m_next == (struct mbuf *)0) 1785 break; 1786 m = m->m_next; 1787 } 1788 if (m->m_len > len) { 1789 m->m_len -= len; 1790 if (nul > 0) { 1791 cp = mtod(m, caddr_t)+m->m_len-nul; 1792 for (i = 0; i < nul; i++) 1793 *cp++ = '\0'; 1794 } 1795 return; 1796 } 1797 count -= len; 1798 if (count < 0) 1799 count = 0; 1800 /* 1801 * Correct length for chain is "count". 1802 * Find the mbuf with last data, adjust its length, 1803 * and toss data from remaining mbufs on chain. 1804 */ 1805 for (m = mp; m; m = m->m_next) { 1806 if (m->m_len >= count) { 1807 m->m_len = count; 1808 if (nul > 0) { 1809 cp = mtod(m, caddr_t)+m->m_len-nul; 1810 for (i = 0; i < nul; i++) 1811 *cp++ = '\0'; 1812 } 1813 break; 1814 } 1815 count -= m->m_len; 1816 } 1817 for (m = m->m_next;m;m = m->m_next) 1818 m->m_len = 0; 1819} 1820 1821/* 1822 * Make these functions instead of macros, so that the kernel text size 1823 * doesn't get too big... 1824 */ 1825void 1826nfsm_srvwcc(nfsd, before_ret, before_vap, after_ret, after_vap, mbp, bposp) 1827 struct nfsrv_descript *nfsd; 1828 int before_ret; 1829 register struct vattr *before_vap; 1830 int after_ret; 1831 struct vattr *after_vap; 1832 struct mbuf **mbp; 1833 char **bposp; 1834{ 1835 register struct mbuf *mb = *mbp, *mb2; 1836 register char *bpos = *bposp; 1837 register u_int32_t *tl; 1838 1839 if (before_ret) { 1840 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); 1841 *tl = nfs_false; 1842 } else { 1843 nfsm_build(tl, u_int32_t *, 7 * NFSX_UNSIGNED); 1844 *tl++ = nfs_true; 1845 txdr_hyper(before_vap->va_size, tl); 1846 tl += 2; 1847 txdr_nfsv3time(&(before_vap->va_mtime), tl); 1848 tl += 2; 1849 txdr_nfsv3time(&(before_vap->va_ctime), tl); 1850 } 1851 *bposp = bpos; 1852 *mbp = mb; 1853 nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp); 1854} 1855 1856void 1857nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp) 1858 struct nfsrv_descript *nfsd; 1859 int after_ret; 1860 struct vattr *after_vap; 1861 struct mbuf **mbp; 1862 char **bposp; 1863{ 1864 register struct mbuf *mb = *mbp, *mb2; 1865 register char *bpos = *bposp; 1866 register u_int32_t *tl; 1867 register struct nfs_fattr *fp; 1868 1869 if (after_ret) { 1870 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); 1871 *tl = nfs_false; 1872 } else { 1873 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FATTR); 1874 *tl++ = nfs_true; 1875 fp = (struct nfs_fattr *)tl; 1876 nfsm_srvfattr(nfsd, after_vap, fp); 1877 } 1878 *mbp = mb; 1879 *bposp = bpos; 1880} 1881 1882void 1883nfsm_srvfattr(nfsd, vap, fp) 1884 register struct nfsrv_descript *nfsd; 1885 register struct vattr *vap; 1886 register struct nfs_fattr *fp; 1887{ 1888 1889 fp->fa_nlink = txdr_unsigned(vap->va_nlink); 1890 fp->fa_uid = txdr_unsigned(vap->va_uid); 1891 fp->fa_gid = txdr_unsigned(vap->va_gid); 1892 if (nfsd->nd_flag & ND_NFSV3) { 1893 fp->fa_type = vtonfsv3_type(vap->va_type); 1894 fp->fa_mode = vtonfsv3_mode(vap->va_mode); 1895 txdr_hyper(vap->va_size, &fp->fa3_size); 1896 txdr_hyper(vap->va_bytes, &fp->fa3_used); 1897 fp->fa3_rdev.specdata1 = txdr_unsigned(umajor(vap->va_rdev)); 1898 fp->fa3_rdev.specdata2 = txdr_unsigned(uminor(vap->va_rdev)); 1899 fp->fa3_fsid.nfsuquad[0] = 0; 1900 fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(vap->va_fsid); 1901 fp->fa3_fileid.nfsuquad[0] = 0; 1902 fp->fa3_fileid.nfsuquad[1] = txdr_unsigned(vap->va_fileid); 1903 txdr_nfsv3time(&vap->va_atime, &fp->fa3_atime); 1904 txdr_nfsv3time(&vap->va_mtime, &fp->fa3_mtime); 1905 txdr_nfsv3time(&vap->va_ctime, &fp->fa3_ctime); 1906 } else { 1907 fp->fa_type = vtonfsv2_type(vap->va_type); 1908 fp->fa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode); 1909 fp->fa2_size = txdr_unsigned(vap->va_size); 1910 fp->fa2_blocksize = txdr_unsigned(vap->va_blocksize); 1911 if (vap->va_type == VFIFO) 1912 fp->fa2_rdev = 0xffffffff; 1913 else 1914 fp->fa2_rdev = txdr_unsigned(vap->va_rdev); 1915 fp->fa2_blocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE); 1916 fp->fa2_fsid = txdr_unsigned(vap->va_fsid); 1917 fp->fa2_fileid = txdr_unsigned(vap->va_fileid); 1918 txdr_nfsv2time(&vap->va_atime, &fp->fa2_atime); 1919 txdr_nfsv2time(&vap->va_mtime, &fp->fa2_mtime); 1920 txdr_nfsv2time(&vap->va_ctime, &fp->fa2_ctime); 1921 } 1922} 1923 1924/* 1925 * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked) 1926 * - look up fsid in mount list (if not found ret error) 1927 * - get vp and export rights by calling VFS_FHTOVP() 1928 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon 1929 * - if not lockflag unlock it with VOP_UNLOCK() 1930 */ 1931int 1932nfsrv_fhtovp(fhp, lockflag, vpp, cred, slp, nam, rdonlyp, kerbflag, pubflag) 1933 fhandle_t *fhp; 1934 int lockflag; 1935 struct vnode **vpp; 1936 struct ucred *cred; 1937 struct nfssvc_sock *slp; 1938 struct sockaddr *nam; 1939 int *rdonlyp; 1940 int kerbflag; 1941 int pubflag; 1942{ 1943 struct proc *p = curproc; /* XXX */ 1944 register struct mount *mp; 1945 register int i; 1946 struct ucred *credanon; 1947 int error, exflags; 1948#ifdef MNT_EXNORESPORT /* XXX needs mountd and /etc/exports help yet */ 1949 struct sockaddr_int *saddr; 1950#endif 1951 1952 *vpp = (struct vnode *)0; 1953 1954 if (nfs_ispublicfh(fhp)) { 1955 if (!pubflag || !nfs_pub.np_valid) 1956 return (ESTALE); 1957 fhp = &nfs_pub.np_handle; 1958 } 1959 1960 mp = vfs_getvfs(&fhp->fh_fsid); 1961 if (!mp) 1962 return (ESTALE); 1963 error = VFS_CHECKEXP(mp, nam, &exflags, &credanon); 1964 if (error) 1965 return (error); 1966 error = VFS_FHTOVP(mp, &fhp->fh_fid, vpp); 1967 if (error) 1968 return (error); 1969#ifdef MNT_EXNORESPORT 1970 if (!(exflags & (MNT_EXNORESPORT|MNT_EXPUBLIC))) { 1971 saddr = (struct sockaddr_in *)nam; 1972 if (saddr->sin_family == AF_INET && 1973 ntohs(saddr->sin_port) >= IPPORT_RESERVED) { 1974 vput(*vpp); 1975 return (NFSERR_AUTHERR | AUTH_TOOWEAK); 1976 } 1977 } 1978#endif 1979 /* 1980 * Check/setup credentials. 1981 */ 1982 if (exflags & MNT_EXKERB) { 1983 if (!kerbflag) { 1984 vput(*vpp); 1985 return (NFSERR_AUTHERR | AUTH_TOOWEAK); 1986 } 1987 } else if (kerbflag) { 1988 vput(*vpp); 1989 return (NFSERR_AUTHERR | AUTH_TOOWEAK); 1990 } else if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) { 1991 cred->cr_uid = credanon->cr_uid; 1992 for (i = 0; i < credanon->cr_ngroups && i < NGROUPS; i++) 1993 cred->cr_groups[i] = credanon->cr_groups[i]; 1994 cred->cr_ngroups = i; 1995 } 1996 if (exflags & MNT_EXRDONLY) 1997 *rdonlyp = 1; 1998 else 1999 *rdonlyp = 0; 2000 2001 nfsrv_object_create(*vpp); 2002 2003 if (!lockflag) 2004 VOP_UNLOCK(*vpp, 0, p); 2005 return (0); 2006} 2007 2008 2009/* 2010 * WebNFS: check if a filehandle is a public filehandle. For v3, this 2011 * means a length of 0, for v2 it means all zeroes. nfsm_srvmtofh has 2012 * transformed this to all zeroes in both cases, so check for it. 2013 */ 2014int 2015nfs_ispublicfh(fhp) 2016 fhandle_t *fhp; 2017{ 2018 char *cp = (char *)fhp; 2019 int i; 2020 2021 for (i = 0; i < NFSX_V3FH; i++) 2022 if (*cp++ != 0) 2023 return (FALSE); 2024 return (TRUE); 2025} 2026 2027#endif /* NFS_NOSERVER */ 2028/* 2029 * This function compares two net addresses by family and returns TRUE 2030 * if they are the same host. 2031 * If there is any doubt, return FALSE. 2032 * The AF_INET family is handled as a special case so that address mbufs 2033 * don't need to be saved to store "struct in_addr", which is only 4 bytes. 2034 */ 2035int 2036netaddr_match(family, haddr, nam) 2037 int family; 2038 union nethostaddr *haddr; 2039 struct sockaddr *nam; 2040{ 2041 register struct sockaddr_in *inetaddr; 2042 2043 switch (family) { 2044 case AF_INET: 2045 inetaddr = (struct sockaddr_in *)nam; 2046 if (inetaddr->sin_family == AF_INET && 2047 inetaddr->sin_addr.s_addr == haddr->had_inetaddr) 2048 return (1); 2049 break; 2050#ifdef ISO 2051 case AF_ISO: 2052 { 2053 register struct sockaddr_iso *isoaddr1, *isoaddr2; 2054 2055 isoaddr1 = (struct sockaddr_iso *)nam; 2056 isoaddr2 = (struct sockaddr_iso *)haddr->had_nam; 2057 if (isoaddr1->siso_family == AF_ISO && 2058 isoaddr1->siso_nlen > 0 && 2059 isoaddr1->siso_nlen == isoaddr2->siso_nlen && 2060 SAME_ISOADDR(isoaddr1, isoaddr2)) 2061 return (1); 2062 break; 2063 } 2064#endif /* ISO */ 2065 default: 2066 break; 2067 }; 2068 return (0); 2069} 2070 2071static nfsuint64 nfs_nullcookie = { { 0, 0 } }; 2072/* 2073 * This function finds the directory cookie that corresponds to the 2074 * logical byte offset given. 2075 */ 2076nfsuint64 * 2077nfs_getcookie(np, off, add) 2078 register struct nfsnode *np; 2079 off_t off; 2080 int add; 2081{ 2082 register struct nfsdmap *dp, *dp2; 2083 register int pos; 2084 2085 pos = (uoff_t)off / NFS_DIRBLKSIZ; 2086 if (pos == 0 || off < 0) { 2087#ifdef DIAGNOSTIC 2088 if (add) 2089 panic("nfs getcookie add at <= 0"); 2090#endif 2091 return (&nfs_nullcookie); 2092 } 2093 pos--; 2094 dp = np->n_cookies.lh_first; 2095 if (!dp) { 2096 if (add) { 2097 MALLOC(dp, struct nfsdmap *, sizeof (struct nfsdmap), 2098 M_NFSDIROFF, M_WAITOK); 2099 dp->ndm_eocookie = 0; 2100 LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list); 2101 } else 2102 return ((nfsuint64 *)0); 2103 } 2104 while (pos >= NFSNUMCOOKIES) { 2105 pos -= NFSNUMCOOKIES; 2106 if (dp->ndm_list.le_next) { 2107 if (!add && dp->ndm_eocookie < NFSNUMCOOKIES && 2108 pos >= dp->ndm_eocookie) 2109 return ((nfsuint64 *)0); 2110 dp = dp->ndm_list.le_next; 2111 } else if (add) { 2112 MALLOC(dp2, struct nfsdmap *, sizeof (struct nfsdmap), 2113 M_NFSDIROFF, M_WAITOK); 2114 dp2->ndm_eocookie = 0; 2115 LIST_INSERT_AFTER(dp, dp2, ndm_list); 2116 dp = dp2; 2117 } else 2118 return ((nfsuint64 *)0); 2119 } 2120 if (pos >= dp->ndm_eocookie) { 2121 if (add) 2122 dp->ndm_eocookie = pos + 1; 2123 else 2124 return ((nfsuint64 *)0); 2125 } 2126 return (&dp->ndm_cookies[pos]); 2127} 2128 2129/* 2130 * Invalidate cached directory information, except for the actual directory 2131 * blocks (which are invalidated separately). 2132 * Done mainly to avoid the use of stale offset cookies. 2133 */ 2134void 2135nfs_invaldir(vp) 2136 register struct vnode *vp; 2137{ 2138 register struct nfsnode *np = VTONFS(vp); 2139 2140#ifdef DIAGNOSTIC 2141 if (vp->v_type != VDIR) 2142 panic("nfs: invaldir not dir"); 2143#endif 2144 np->n_direofoffset = 0; 2145 np->n_cookieverf.nfsuquad[0] = 0; 2146 np->n_cookieverf.nfsuquad[1] = 0; 2147 if (np->n_cookies.lh_first) 2148 np->n_cookies.lh_first->ndm_eocookie = 0; 2149} 2150 2151/* 2152 * The write verifier has changed (probably due to a server reboot), so all 2153 * B_NEEDCOMMIT blocks will have to be written again. Since they are on the 2154 * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT 2155 * flag. Once done the new write verifier can be set for the mount point. 2156 */ 2157void 2158nfs_clearcommit(mp) 2159 struct mount *mp; 2160{ 2161 register struct vnode *vp, *nvp; 2162 register struct buf *bp, *nbp; 2163 int s; 2164 2165 s = splbio(); 2166loop: 2167 for (vp = mp->mnt_vnodelist.lh_first; vp; vp = nvp) { 2168 if (vp->v_mount != mp) /* Paranoia */ 2169 goto loop; 2170 nvp = vp->v_mntvnodes.le_next; 2171 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) { 2172 nbp = TAILQ_NEXT(bp, b_vnbufs); 2173 if (BUF_REFCNT(bp) == 0 && 2174 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) 2175 == (B_DELWRI | B_NEEDCOMMIT)) 2176 bp->b_flags &= ~B_NEEDCOMMIT; 2177 } 2178 } 2179 splx(s); 2180} 2181 2182#ifndef NFS_NOSERVER 2183/* 2184 * Map errnos to NFS error numbers. For Version 3 also filter out error 2185 * numbers not specified for the associated procedure. 2186 */ 2187int 2188nfsrv_errmap(nd, err) 2189 struct nfsrv_descript *nd; 2190 register int err; 2191{ 2192 register short *defaulterrp, *errp; 2193 2194 if (nd->nd_flag & ND_NFSV3) { 2195 if (nd->nd_procnum <= NFSPROC_COMMIT) { 2196 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum]; 2197 while (*++errp) { 2198 if (*errp == err) 2199 return (err); 2200 else if (*errp > err) 2201 break; 2202 } 2203 return ((int)*defaulterrp); 2204 } else 2205 return (err & 0xffff); 2206 } 2207 if (err <= ELAST) 2208 return ((int)nfsrv_v2errmap[err - 1]); 2209 return (NFSERR_IO); 2210} 2211 2212int 2213nfsrv_object_create(vp) 2214 struct vnode *vp; 2215{ 2216 2217 if (vp == NULL || vp->v_type != VREG) 2218 return (1); 2219 return (vfs_object_create(vp, curproc, 2220 curproc ? curproc->p_ucred : NULL)); 2221} 2222 2223/* 2224 * Sort the group list in increasing numerical order. 2225 * (Insertion sort by Chris Torek, who was grossed out by the bubble sort 2226 * that used to be here.) 2227 */ 2228void 2229nfsrvw_sort(list, num) 2230 register gid_t *list; 2231 register int num; 2232{ 2233 register int i, j; 2234 gid_t v; 2235 2236 /* Insertion sort. */ 2237 for (i = 1; i < num; i++) { 2238 v = list[i]; 2239 /* find correct slot for value v, moving others up */ 2240 for (j = i; --j >= 0 && v < list[j];) 2241 list[j + 1] = list[j]; 2242 list[j + 1] = v; 2243 } 2244} 2245 2246/* 2247 * copy credentials making sure that the result can be compared with bcmp(). 2248 */ 2249void 2250nfsrv_setcred(incred, outcred) 2251 register struct ucred *incred, *outcred; 2252{ 2253 register int i; 2254 2255 bzero((caddr_t)outcred, sizeof (struct ucred)); 2256 outcred->cr_ref = 1; 2257 outcred->cr_uid = incred->cr_uid; 2258 outcred->cr_ngroups = incred->cr_ngroups; 2259 for (i = 0; i < incred->cr_ngroups; i++) 2260 outcred->cr_groups[i] = incred->cr_groups[i]; 2261 nfsrvw_sort(outcred->cr_groups, outcred->cr_ngroups); 2262} 2263#endif /* NFS_NOSERVER */ 2264