nfs_srvsubs.c revision 83366
1219820Sjeff/* 2219820Sjeff * Copyright (c) 1989, 1993 3219820Sjeff * The Regents of the University of California. All rights reserved. 4219820Sjeff * 5294839Shselasky * This code is derived from software contributed to Berkeley by 6289580Shselasky * Rick Macklem at The University of Guelph. 7219820Sjeff * 8219820Sjeff * Redistribution and use in source and binary forms, with or without 9219820Sjeff * modification, are permitted provided that the following conditions 10219820Sjeff * are met: 11219820Sjeff * 1. Redistributions of source code must retain the above copyright 12219820Sjeff * notice, this list of conditions and the following disclaimer. 13219820Sjeff * 2. Redistributions in binary form must reproduce the above copyright 14219820Sjeff * notice, this list of conditions and the following disclaimer in the 15219820Sjeff * documentation and/or other materials provided with the distribution. 16219820Sjeff * 3. All advertising materials mentioning features or use of this software 17219820Sjeff * must display the following acknowledgement: 18219820Sjeff * This product includes software developed by the University of 19219820Sjeff * California, Berkeley and its contributors. 20219820Sjeff * 4. Neither the name of the University nor the names of its contributors 21219820Sjeff * may be used to endorse or promote products derived from this software 22219820Sjeff * without specific prior written permission. 23219820Sjeff * 24219820Sjeff * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25219820Sjeff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26219820Sjeff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27219820Sjeff * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28219820Sjeff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29289644Shselasky * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30289644Shselasky * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31219820Sjeff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32219820Sjeff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33219820Sjeff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34219820Sjeff * SUCH DAMAGE. 35277396Shselasky * 36277396Shselasky * @(#)nfs_subs.c 8.8 (Berkeley) 5/22/95 37219820Sjeff * $FreeBSD: head/sys/nfsserver/nfs_srvsubs.c 83366 2001-09-12 08:38:13Z julian $ 38219820Sjeff */ 39219820Sjeff 40219820Sjeff/* 41219820Sjeff * These functions support the macros and help fiddle mbuf chains for 42270710Shselasky * the nfs op functions. They do things like create the rpc header and 43276749Shselasky * copy data between mbuf chains and uio lists. 44328653Shselasky */ 45219820Sjeff#include <sys/param.h> 46219820Sjeff#include <sys/systm.h> 47219820Sjeff#include <sys/kernel.h> 48219820Sjeff#include <sys/bio.h> 49328653Shselasky#include <sys/buf.h> 50219820Sjeff#include <sys/proc.h> 51219820Sjeff#include <sys/mount.h> 52331756Semaste#include <sys/vnode.h> 53335433Shselasky#include <sys/namei.h> 54219820Sjeff#include <sys/mbuf.h> 55335433Shselasky#include <sys/socket.h> 56219820Sjeff#include <sys/stat.h> 57300500Shselasky#include <sys/malloc.h> 58300500Shselasky#include <sys/sysent.h> 59255932Salfred#include <sys/syscall.h> 60219820Sjeff 61219820Sjeff#include <vm/vm.h> 62219820Sjeff#include <vm/vm_object.h> 63219820Sjeff#include <vm/vm_extern.h> 64219820Sjeff#include <vm/vm_zone.h> 65219820Sjeff 66219820Sjeff#include <nfs/rpcv2.h> 67219820Sjeff#include <nfs/nfsproto.h> 68219820Sjeff#include <nfs/nfs.h> 69299653Shselasky#include <nfs/nfsnode.h> 70299653Shselasky#include <nfs/xdr_subs.h> 71299653Shselasky#include <nfs/nfsm_subs.h> 72299653Shselasky#include <nfs/nfsmount.h> 73299653Shselasky#include <nfs/nqnfs.h> 74299653Shselasky#include <nfs/nfsrtt.h> 75299653Shselasky 76299653Shselasky#include <netinet/in.h> 77299653Shselasky 78299653Shselasky/* 79299653Shselasky * Data items converted to xdr at startup, since they are constant 80299653Shselasky * This is kinda hokey, but may save a little time doing byte swaps 81299653Shselasky */ 82299653Shselaskyu_int32_t nfs_xdrneg1; 83299653Shselaskyu_int32_t rpc_call, rpc_vers, rpc_reply, rpc_msgdenied, rpc_autherr, 84299653Shselasky rpc_mismatch, rpc_auth_unix, rpc_msgaccepted, 85299653Shselasky rpc_auth_kerb; 86299653Shselaskyu_int32_t nfs_prog, nqnfs_prog, nfs_true, nfs_false; 87299653Shselasky 88299653Shselasky/* And other global data */ 89299653Shselaskystatic u_int32_t nfs_xid = 0; 90299653Shselaskystatic enum vtype nv2tov_type[8]= { 91330858Shselasky VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON, VNON 92328653Shselasky}; 93328653Shselaskyenum vtype nv3tov_type[8]= { 94328653Shselasky VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO 95329980Shselasky}; 96277396Shselasky 97362315Shselaskyint nfs_ticks; 98362315Shselaskyint nfs_pbuf_freecnt = -1; /* start out unlimited */ 99362315Shselasky 100299653Shselaskystruct nfs_reqq nfs_reqq; 101299653Shselaskystruct nfssvc_sockhead nfssvc_sockhead; 102299653Shselaskyint nfssvc_sockhead_flag; 103299653Shselaskystruct nfsd_head nfsd_head; 104299653Shselaskyint nfsd_head_flag; 105299653Shselaskystruct nfs_bufq nfs_bufq; 106299653Shselaskystruct nqtimerhead nqtimerhead; 107219820Sjeffstruct nqfhhashhead *nqfhhashtbl; 108299653Shselaskyu_long nqfhhash; 109299653Shselasky 110299653Shselaskystatic void (*nfs_prev_lease_updatetime) __P((int)); 111299653Shselaskystatic int nfs_prev_nfssvc_sy_narg; 112299653Shselaskystatic sy_call_t *nfs_prev_nfssvc_sy_call; 113299653Shselasky 114299653Shselasky#ifndef NFS_NOSERVER 115299653Shselasky 116299653Shselaskystatic vop_t *nfs_prev_vop_lease_check; 117299653Shselasky 118299653Shselasky/* 119299653Shselasky * Mapping of old NFS Version 2 RPC numbers to generic numbers. 120299653Shselasky */ 121299653Shselaskyint nfsv3_procid[NFS_NPROCS] = { 122299653Shselasky NFSPROC_NULL, 123299653Shselasky NFSPROC_GETATTR, 124299653Shselasky NFSPROC_SETATTR, 125299653Shselasky NFSPROC_NOOP, 126299653Shselasky NFSPROC_LOOKUP, 127299653Shselasky NFSPROC_READLINK, 128299653Shselasky NFSPROC_READ, 129299653Shselasky NFSPROC_NOOP, 130328653Shselasky NFSPROC_WRITE, 131328653Shselasky NFSPROC_CREATE, 132219820Sjeff NFSPROC_REMOVE, 133219820Sjeff NFSPROC_RENAME, 134279587Shselasky NFSPROC_LINK, 135279587Shselasky NFSPROC_SYMLINK, 136289580Shselasky NFSPROC_MKDIR, 137339997Shselasky NFSPROC_RMDIR, 138289580Shselasky NFSPROC_READDIR, 139278681Shselasky NFSPROC_FSSTAT, 140219820Sjeff NFSPROC_NOOP, 141300500Shselasky NFSPROC_NOOP, 142300500Shselasky NFSPROC_NOOP, 143276749Shselasky NFSPROC_NOOP, 144340945Shselasky NFSPROC_NOOP, 145340945Shselasky NFSPROC_NOOP, 146340003Shselasky NFSPROC_NOOP, 147340003Shselasky NFSPROC_NOOP 148340003Shselasky}; 149300500Shselasky 150300500Shselasky#endif /* NFS_NOSERVER */ 151300500Shselasky/* 152300500Shselasky * and the reverse mapping from generic to Version 2 procedure numbers 153300500Shselasky */ 154300500Shselaskyint nfsv2_procid[NFS_NPROCS] = { 155300500Shselasky NFSV2PROC_NULL, 156300500Shselasky NFSV2PROC_GETATTR, 157300500Shselasky NFSV2PROC_SETATTR, 158300500Shselasky NFSV2PROC_LOOKUP, 159300500Shselasky NFSV2PROC_NOOP, 160300500Shselasky NFSV2PROC_READLINK, 161300500Shselasky NFSV2PROC_READ, 162300500Shselasky NFSV2PROC_WRITE, 163300500Shselasky NFSV2PROC_CREATE, 164300500Shselasky NFSV2PROC_MKDIR, 165300500Shselasky NFSV2PROC_SYMLINK, 166300500Shselasky NFSV2PROC_CREATE, 167300500Shselasky NFSV2PROC_REMOVE, 168300500Shselasky NFSV2PROC_RMDIR, 169300500Shselasky NFSV2PROC_RENAME, 170300500Shselasky NFSV2PROC_LINK, 171300500Shselasky NFSV2PROC_READDIR, 172300500Shselasky NFSV2PROC_NOOP, 173300500Shselasky NFSV2PROC_STATFS, 174300500Shselasky NFSV2PROC_NOOP, 175300500Shselasky NFSV2PROC_NOOP, 176300500Shselasky NFSV2PROC_NOOP, 177300500Shselasky NFSV2PROC_NOOP, 178276749Shselasky NFSV2PROC_NOOP, 179276749Shselasky NFSV2PROC_NOOP, 180276749Shselasky NFSV2PROC_NOOP, 181276749Shselasky}; 182276749Shselasky 183347791Shselasky#ifndef NFS_NOSERVER 184347791Shselasky/* 185347791Shselasky * Maps errno values to nfs error numbers. 186347791Shselasky * Use NFSERR_IO as the catch all for ones not specifically defined in 187347791Shselasky * RFC 1094. 188347791Shselasky */ 189276749Shselaskystatic u_char nfsrv_v2errmap[ELAST] = { 190276749Shselasky NFSERR_PERM, NFSERR_NOENT, NFSERR_IO, NFSERR_IO, NFSERR_IO, 191276749Shselasky NFSERR_NXIO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 192276749Shselasky NFSERR_IO, NFSERR_IO, NFSERR_ACCES, NFSERR_IO, NFSERR_IO, 193331756Semaste NFSERR_IO, NFSERR_EXIST, NFSERR_IO, NFSERR_NODEV, NFSERR_NOTDIR, 194276749Shselasky NFSERR_ISDIR, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 195276749Shselasky NFSERR_IO, NFSERR_FBIG, NFSERR_NOSPC, NFSERR_IO, NFSERR_ROFS, 196276749Shselasky NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 197276749Shselasky NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 198255932Salfred NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 199255932Salfred NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 200255932Salfred NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 201255932Salfred NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 202255932Salfred NFSERR_IO, NFSERR_IO, NFSERR_NAMETOL, NFSERR_IO, NFSERR_IO, 203255932Salfred NFSERR_NOTEMPTY, NFSERR_IO, NFSERR_IO, NFSERR_DQUOT, NFSERR_STALE, 204255932Salfred NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 205276749Shselasky NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 206276749Shselasky NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 207276749Shselasky NFSERR_IO /* << Last is 86 */ 208276749Shselasky}; 209276749Shselasky 210276749Shselasky/* 211276749Shselasky * Maps errno values to nfs error numbers. 212276749Shselasky * Although it is not obvious whether or not NFS clients really care if 213255932Salfred * a returned error value is in the specified list for the procedure, the 214276749Shselasky * safest thing to do is filter them appropriately. For Version 2, the 215276749Shselasky * X/Open XNFS document is the only specification that defines error values 216276749Shselasky * for each RPC (The RFC simply lists all possible error values for all RPCs), 217276749Shselasky * so I have decided to not do this for Version 2. 218276749Shselasky * The first entry is the default error return and the rest are the valid 219276749Shselasky * errors for that RPC in increasing numeric order. 220299653Shselasky */ 221276749Shselaskystatic short nfsv3err_null[] = { 222276749Shselasky 0, 223276749Shselasky 0, 224276749Shselasky}; 225255932Salfred 226255932Salfredstatic short nfsv3err_getattr[] = { 227276749Shselasky NFSERR_IO, 228255932Salfred NFSERR_IO, 229276749Shselasky NFSERR_STALE, 230255932Salfred NFSERR_BADHANDLE, 231276749Shselasky NFSERR_SERVERFAULT, 232255932Salfred 0, 233276749Shselasky}; 234370003Semaste 235370003Semastestatic short nfsv3err_setattr[] = { 236255932Salfred NFSERR_IO, 237276749Shselasky NFSERR_PERM, 238299653Shselasky NFSERR_IO, 239299653Shselasky NFSERR_ACCES, 240299653Shselasky NFSERR_INVAL, 241299653Shselasky NFSERR_NOSPC, 242255932Salfred NFSERR_ROFS, 243276749Shselasky NFSERR_DQUOT, 244255932Salfred NFSERR_STALE, 245276749Shselasky NFSERR_BADHANDLE, 246270710Shselasky NFSERR_NOT_SYNC, 247276749Shselasky NFSERR_SERVERFAULT, 248255932Salfred 0, 249276749Shselasky}; 250328653Shselasky 251328653Shselaskystatic short nfsv3err_lookup[] = { 252328653Shselasky NFSERR_IO, 253328653Shselasky NFSERR_NOENT, 254328653Shselasky NFSERR_IO, 255255932Salfred NFSERR_ACCES, 256255932Salfred NFSERR_NOTDIR, 257299653Shselasky NFSERR_NAMETOL, 258331756Semaste NFSERR_STALE, 259331756Semaste NFSERR_BADHANDLE, 260331756Semaste NFSERR_SERVERFAULT, 261331756Semaste 0, 262255932Salfred}; 263255932Salfred 264255932Salfredstatic short nfsv3err_access[] = { 265299653Shselasky NFSERR_IO, 266299653Shselasky NFSERR_IO, 267331756Semaste NFSERR_STALE, 268331756Semaste NFSERR_BADHANDLE, 269331756Semaste NFSERR_SERVERFAULT, 270331756Semaste 0, 271299653Shselasky}; 272299653Shselasky 273299653Shselaskystatic short nfsv3err_readlink[] = { 274219820Sjeff NFSERR_IO, 275219820Sjeff NFSERR_IO, 276328653Shselasky NFSERR_ACCES, 277328653Shselasky NFSERR_INVAL, 278219820Sjeff NFSERR_STALE, 279331756Semaste NFSERR_BADHANDLE, 280219820Sjeff NFSERR_NOTSUPP, 281219820Sjeff NFSERR_SERVERFAULT, 282328653Shselasky 0, 283219820Sjeff}; 284328653Shselasky 285328653Shselaskystatic short nfsv3err_read[] = { 286328653Shselasky NFSERR_IO, 287328653Shselasky NFSERR_IO, 288328653Shselasky NFSERR_NXIO, 289328653Shselasky NFSERR_ACCES, 290328653Shselasky NFSERR_INVAL, 291328653Shselasky NFSERR_STALE, 292328653Shselasky NFSERR_BADHANDLE, 293328653Shselasky NFSERR_SERVERFAULT, 294328653Shselasky 0, 295328653Shselasky}; 296328653Shselasky 297328653Shselaskystatic short nfsv3err_write[] = { 298328653Shselasky NFSERR_IO, 299328653Shselasky NFSERR_IO, 300328653Shselasky NFSERR_ACCES, 301328653Shselasky NFSERR_INVAL, 302328653Shselasky NFSERR_FBIG, 303328653Shselasky NFSERR_NOSPC, 304328653Shselasky NFSERR_ROFS, 305328653Shselasky NFSERR_DQUOT, 306328653Shselasky NFSERR_STALE, 307328653Shselasky NFSERR_BADHANDLE, 308328653Shselasky NFSERR_SERVERFAULT, 309328653Shselasky 0, 310328653Shselasky}; 311328653Shselasky 312328653Shselaskystatic short nfsv3err_create[] = { 313328653Shselasky NFSERR_IO, 314328653Shselasky NFSERR_IO, 315331799Shselasky NFSERR_ACCES, 316331799Shselasky NFSERR_EXIST, 317331799Shselasky NFSERR_NOTDIR, 318328653Shselasky NFSERR_NOSPC, 319328653Shselasky NFSERR_ROFS, 320328653Shselasky NFSERR_NAMETOL, 321328653Shselasky NFSERR_DQUOT, 322328653Shselasky NFSERR_STALE, 323328653Shselasky NFSERR_BADHANDLE, 324328653Shselasky NFSERR_NOTSUPP, 325328653Shselasky NFSERR_SERVERFAULT, 326328653Shselasky 0, 327328653Shselasky}; 328328653Shselasky 329328653Shselaskystatic short nfsv3err_mkdir[] = { 330331799Shselasky NFSERR_IO, 331331799Shselasky NFSERR_IO, 332331799Shselasky NFSERR_ACCES, 333328653Shselasky NFSERR_EXIST, 334328653Shselasky NFSERR_NOTDIR, 335328653Shselasky NFSERR_NOSPC, 336328653Shselasky NFSERR_ROFS, 337328653Shselasky NFSERR_NAMETOL, 338328653Shselasky NFSERR_DQUOT, 339328653Shselasky NFSERR_STALE, 340328653Shselasky NFSERR_BADHANDLE, 341328653Shselasky NFSERR_NOTSUPP, 342328653Shselasky NFSERR_SERVERFAULT, 343328653Shselasky 0, 344328653Shselasky}; 345328653Shselasky 346331799Shselaskystatic short nfsv3err_symlink[] = { 347331799Shselasky NFSERR_IO, 348331799Shselasky NFSERR_IO, 349328653Shselasky NFSERR_ACCES, 350328653Shselasky NFSERR_EXIST, 351328653Shselasky NFSERR_NOTDIR, 352328653Shselasky NFSERR_NOSPC, 353328653Shselasky NFSERR_ROFS, 354328653Shselasky NFSERR_NAMETOL, 355328653Shselasky NFSERR_DQUOT, 356328653Shselasky NFSERR_STALE, 357328653Shselasky NFSERR_BADHANDLE, 358328653Shselasky NFSERR_NOTSUPP, 359328653Shselasky NFSERR_SERVERFAULT, 360328653Shselasky 0, 361328653Shselasky}; 362328653Shselasky 363328653Shselaskystatic short nfsv3err_mknod[] = { 364331799Shselasky NFSERR_IO, 365331799Shselasky NFSERR_IO, 366331799Shselasky NFSERR_ACCES, 367328653Shselasky NFSERR_EXIST, 368328653Shselasky NFSERR_NOTDIR, 369328653Shselasky NFSERR_NOSPC, 370328653Shselasky NFSERR_ROFS, 371328653Shselasky NFSERR_NAMETOL, 372328653Shselasky NFSERR_DQUOT, 373328653Shselasky NFSERR_STALE, 374328653Shselasky NFSERR_BADHANDLE, 375328653Shselasky NFSERR_NOTSUPP, 376328653Shselasky NFSERR_SERVERFAULT, 377328653Shselasky NFSERR_BADTYPE, 378328653Shselasky 0, 379328653Shselasky}; 380328653Shselasky 381328653Shselaskystatic short nfsv3err_remove[] = { 382331799Shselasky NFSERR_IO, 383331799Shselasky NFSERR_NOENT, 384331799Shselasky NFSERR_IO, 385328653Shselasky NFSERR_ACCES, 386328653Shselasky NFSERR_NOTDIR, 387328653Shselasky NFSERR_ROFS, 388328653Shselasky NFSERR_NAMETOL, 389328653Shselasky NFSERR_STALE, 390328653Shselasky NFSERR_BADHANDLE, 391328653Shselasky NFSERR_SERVERFAULT, 392335433Shselasky 0, 393361173Shselasky}; 394361173Shselasky 395361173Shselaskystatic short nfsv3err_rmdir[] = { 396361173Shselasky NFSERR_IO, 397361173Shselasky NFSERR_NOENT, 398361173Shselasky NFSERR_IO, 399361173Shselasky NFSERR_ACCES, 400361173Shselasky NFSERR_EXIST, 401361173Shselasky NFSERR_NOTDIR, 402361173Shselasky NFSERR_INVAL, 403361173Shselasky NFSERR_ROFS, 404361173Shselasky NFSERR_NAMETOL, 405361173Shselasky NFSERR_NOTEMPTY, 406361173Shselasky NFSERR_STALE, 407361173Shselasky NFSERR_BADHANDLE, 408335433Shselasky NFSERR_NOTSUPP, 409335433Shselasky NFSERR_SERVERFAULT, 410335433Shselasky 0, 411335433Shselasky}; 412335433Shselasky 413335433Shselaskystatic short nfsv3err_rename[] = { 414335433Shselasky NFSERR_IO, 415335433Shselasky NFSERR_NOENT, 416335433Shselasky NFSERR_IO, 417335433Shselasky NFSERR_ACCES, 418335433Shselasky NFSERR_EXIST, 419335433Shselasky NFSERR_XDEV, 420335433Shselasky NFSERR_NOTDIR, 421335433Shselasky NFSERR_ISDIR, 422335433Shselasky NFSERR_INVAL, 423335433Shselasky NFSERR_NOSPC, 424335433Shselasky NFSERR_ROFS, 425335433Shselasky NFSERR_MLINK, 426335433Shselasky NFSERR_NAMETOL, 427335433Shselasky NFSERR_NOTEMPTY, 428335433Shselasky NFSERR_DQUOT, 429335433Shselasky NFSERR_STALE, 430335433Shselasky NFSERR_BADHANDLE, 431335433Shselasky NFSERR_NOTSUPP, 432335433Shselasky NFSERR_SERVERFAULT, 433335433Shselasky 0, 434335433Shselasky}; 435335433Shselasky 436335433Shselaskystatic short nfsv3err_link[] = { 437335433Shselasky NFSERR_IO, 438335433Shselasky NFSERR_IO, 439335433Shselasky NFSERR_ACCES, 440335433Shselasky NFSERR_EXIST, 441335433Shselasky NFSERR_XDEV, 442335433Shselasky NFSERR_NOTDIR, 443335433Shselasky NFSERR_INVAL, 444335433Shselasky NFSERR_NOSPC, 445335433Shselasky NFSERR_ROFS, 446335433Shselasky NFSERR_MLINK, 447276749Shselasky NFSERR_NAMETOL, 448276749Shselasky NFSERR_DQUOT, 449289580Shselasky NFSERR_STALE, 450289580Shselasky NFSERR_BADHANDLE, 451289580Shselasky NFSERR_NOTSUPP, 452289580Shselasky NFSERR_SERVERFAULT, 453299653Shselasky 0, 454299653Shselasky}; 455299653Shselasky 456299653Shselaskystatic short nfsv3err_readdir[] = { 457219820Sjeff NFSERR_IO, 458299653Shselasky NFSERR_IO, 459299653Shselasky NFSERR_ACCES, 460299653Shselasky NFSERR_NOTDIR, 461299653Shselasky NFSERR_STALE, 462299653Shselasky NFSERR_BADHANDLE, 463289580Shselasky NFSERR_BAD_COOKIE, 464289580Shselasky NFSERR_TOOSMALL, 465299653Shselasky NFSERR_SERVERFAULT, 466289580Shselasky 0, 467255932Salfred}; 468255932Salfred 469255932Salfredstatic short nfsv3err_readdirplus[] = { 470255932Salfred NFSERR_IO, 471255932Salfred NFSERR_IO, 472255932Salfred NFSERR_ACCES, 473255932Salfred NFSERR_NOTDIR, 474255932Salfred NFSERR_STALE, 475255932Salfred NFSERR_BADHANDLE, 476255932Salfred NFSERR_BAD_COOKIE, 477294839Shselasky NFSERR_NOTSUPP, 478219820Sjeff NFSERR_TOOSMALL, 479278681Shselasky NFSERR_SERVERFAULT, 480219820Sjeff 0, 481299930Shselasky}; 482299930Shselasky 483299930Shselaskystatic short nfsv3err_fsstat[] = { 484299930Shselasky NFSERR_IO, 485299930Shselasky NFSERR_IO, 486255932Salfred NFSERR_STALE, 487331756Semaste NFSERR_BADHANDLE, 488255932Salfred NFSERR_SERVERFAULT, 489255932Salfred 0, 490289580Shselasky}; 491289580Shselasky 492289580Shselaskystatic short nfsv3err_fsinfo[] = { 493289580Shselasky NFSERR_STALE, 494289580Shselasky NFSERR_STALE, 495289580Shselasky NFSERR_BADHANDLE, 496289580Shselasky NFSERR_SERVERFAULT, 497289580Shselasky 0, 498289580Shselasky}; 499299653Shselasky 500299653Shselaskystatic short nfsv3err_pathconf[] = { 501299653Shselasky NFSERR_STALE, 502299653Shselasky NFSERR_STALE, 503299653Shselasky NFSERR_BADHANDLE, 504299653Shselasky NFSERR_SERVERFAULT, 505299653Shselasky 0, 506289580Shselasky}; 507289580Shselasky 508289580Shselaskystatic short nfsv3err_commit[] = { 509289580Shselasky NFSERR_IO, 510289580Shselasky NFSERR_IO, 511289580Shselasky NFSERR_STALE, 512289580Shselasky NFSERR_BADHANDLE, 513289580Shselasky NFSERR_SERVERFAULT, 514289580Shselasky 0, 515289580Shselasky}; 516289580Shselasky 517289580Shselaskystatic short *nfsrv_v3errmap[] = { 518289580Shselasky nfsv3err_null, 519289580Shselasky nfsv3err_getattr, 520289580Shselasky nfsv3err_setattr, 521328653Shselasky nfsv3err_lookup, 522328653Shselasky nfsv3err_access, 523328653Shselasky nfsv3err_readlink, 524328653Shselasky nfsv3err_read, 525328653Shselasky nfsv3err_write, 526328653Shselasky nfsv3err_create, 527328653Shselasky nfsv3err_mkdir, 528328653Shselasky nfsv3err_symlink, 529328653Shselasky nfsv3err_mknod, 530328653Shselasky nfsv3err_remove, 531328653Shselasky nfsv3err_rmdir, 532362317Shselasky nfsv3err_rename, 533362317Shselasky nfsv3err_link, 534362317Shselasky nfsv3err_readdir, 535362317Shselasky nfsv3err_readdirplus, 536362317Shselasky nfsv3err_fsstat, 537362317Shselasky nfsv3err_fsinfo, 538362320Shselasky nfsv3err_pathconf, 539362320Shselasky nfsv3err_commit, 540362320Shselasky}; 541363155Shselasky 542363155Shselasky#endif /* NFS_NOSERVER */ 543363155Shselasky 544363155Shselaskyextern struct nfsrtt nfsrtt; 545363155Shselaskyextern time_t nqnfsstarttime; 546363155Shselaskyextern int nqsrv_clockskew; 547363155Shselaskyextern int nqsrv_writeslack; 548363155Shselaskyextern int nqsrv_maxlease; 549363155Shselaskyextern struct nfsstats nfsstats; 550363155Shselaskyextern int nqnfs_piggy[NFS_NPROCS]; 551363155Shselaskyextern nfstype nfsv2_type[9]; 552363155Shselaskyextern nfstype nfsv3_type[9]; 553363155Shselaskyextern struct nfsnodehashhead *nfsnodehashtbl; 554363155Shselaskyextern u_long nfsnodehash; 555363155Shselasky 556363155Shselaskystruct nfssvc_args; 557363155Shselaskyextern int nfssvc(struct thread *, struct nfssvc_args *, int *); 558363155Shselasky 559363155ShselaskyLIST_HEAD(nfsnodehashhead, nfsnode); 560363155Shselasky 561363155Shselaskyint nfs_webnamei __P((struct nameidata *, struct vnode *, struct thread *)); 562363155Shselasky 563363155Shselaskyu_quad_t 564363155Shselaskynfs_curusec() 565363155Shselasky{ 566363155Shselasky struct timeval tv; 567363155Shselasky 568363155Shselasky getmicrotime(&tv); 569363155Shselasky return ((u_quad_t)tv.tv_sec * 1000000 + (u_quad_t)tv.tv_usec); 570219820Sjeff} 571 572/* 573 * Create the header for an rpc request packet 574 * The hsiz is the size of the rest of the nfs request header. 575 * (just used to decide if a cluster is a good idea) 576 */ 577struct mbuf * 578nfsm_reqh(vp, procid, hsiz, bposp) 579 struct vnode *vp; 580 u_long procid; 581 int hsiz; 582 caddr_t *bposp; 583{ 584 register struct mbuf *mb; 585 register u_int32_t *tl; 586 register caddr_t bpos; 587 struct mbuf *mb2; 588 struct nfsmount *nmp; 589 int nqflag; 590 591 MGET(mb, M_TRYWAIT, MT_DATA); 592 if (hsiz >= MINCLSIZE) 593 MCLGET(mb, M_TRYWAIT); 594 mb->m_len = 0; 595 bpos = mtod(mb, caddr_t); 596 597 /* 598 * For NQNFS, add lease request. 599 */ 600 if (vp) { 601 nmp = VFSTONFS(vp->v_mount); 602 if (nmp->nm_flag & NFSMNT_NQNFS) { 603 nqflag = NQNFS_NEEDLEASE(vp, procid); 604 if (nqflag) { 605 nfsm_build(tl, u_int32_t *, 2*NFSX_UNSIGNED); 606 *tl++ = txdr_unsigned(nqflag); 607 *tl = txdr_unsigned(nmp->nm_leaseterm); 608 } else { 609 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); 610 *tl = 0; 611 } 612 } 613 } 614 /* Finally, return values */ 615 *bposp = bpos; 616 return (mb); 617} 618 619/* 620 * Build the RPC header and fill in the authorization info. 621 * The authorization string argument is only used when the credentials 622 * come from outside of the kernel. 623 * Returns the head of the mbuf list. 624 */ 625struct mbuf * 626nfsm_rpchead(cr, nmflag, procid, auth_type, auth_len, auth_str, verf_len, 627 verf_str, mrest, mrest_len, mbp, xidp) 628 register struct ucred *cr; 629 int nmflag; 630 int procid; 631 int auth_type; 632 int auth_len; 633 char *auth_str; 634 int verf_len; 635 char *verf_str; 636 struct mbuf *mrest; 637 int mrest_len; 638 struct mbuf **mbp; 639 u_int32_t *xidp; 640{ 641 register struct mbuf *mb; 642 register u_int32_t *tl; 643 register caddr_t bpos; 644 register int i; 645 struct mbuf *mreq, *mb2; 646 int siz, grpsiz, authsiz; 647 648 authsiz = nfsm_rndup(auth_len); 649 MGETHDR(mb, M_TRYWAIT, MT_DATA); 650 if ((authsiz + 10 * NFSX_UNSIGNED) >= MINCLSIZE) { 651 MCLGET(mb, M_TRYWAIT); 652 } else if ((authsiz + 10 * NFSX_UNSIGNED) < MHLEN) { 653 MH_ALIGN(mb, authsiz + 10 * NFSX_UNSIGNED); 654 } else { 655 MH_ALIGN(mb, 8 * NFSX_UNSIGNED); 656 } 657 mb->m_len = 0; 658 mreq = mb; 659 bpos = mtod(mb, caddr_t); 660 661 /* 662 * First the RPC header. 663 */ 664 nfsm_build(tl, u_int32_t *, 8 * NFSX_UNSIGNED); 665 666 /* Get a pretty random xid to start with */ 667 if (!nfs_xid) 668 nfs_xid = random(); 669 /* 670 * Skip zero xid if it should ever happen. 671 */ 672 if (++nfs_xid == 0) 673 nfs_xid++; 674 675 *tl++ = *xidp = txdr_unsigned(nfs_xid); 676 *tl++ = rpc_call; 677 *tl++ = rpc_vers; 678 if (nmflag & NFSMNT_NQNFS) { 679 *tl++ = txdr_unsigned(NQNFS_PROG); 680 *tl++ = txdr_unsigned(NQNFS_VER3); 681 } else { 682 *tl++ = txdr_unsigned(NFS_PROG); 683 if (nmflag & NFSMNT_NFSV3) 684 *tl++ = txdr_unsigned(NFS_VER3); 685 else 686 *tl++ = txdr_unsigned(NFS_VER2); 687 } 688 if (nmflag & NFSMNT_NFSV3) 689 *tl++ = txdr_unsigned(procid); 690 else 691 *tl++ = txdr_unsigned(nfsv2_procid[procid]); 692 693 /* 694 * And then the authorization cred. 695 */ 696 *tl++ = txdr_unsigned(auth_type); 697 *tl = txdr_unsigned(authsiz); 698 switch (auth_type) { 699 case RPCAUTH_UNIX: 700 nfsm_build(tl, u_int32_t *, auth_len); 701 *tl++ = 0; /* stamp ?? */ 702 *tl++ = 0; /* NULL hostname */ 703 *tl++ = txdr_unsigned(cr->cr_uid); 704 *tl++ = txdr_unsigned(cr->cr_groups[0]); 705 grpsiz = (auth_len >> 2) - 5; 706 *tl++ = txdr_unsigned(grpsiz); 707 for (i = 1; i <= grpsiz; i++) 708 *tl++ = txdr_unsigned(cr->cr_groups[i]); 709 break; 710 case RPCAUTH_KERB4: 711 siz = auth_len; 712 while (siz > 0) { 713 if (M_TRAILINGSPACE(mb) == 0) { 714 MGET(mb2, M_TRYWAIT, MT_DATA); 715 if (siz >= MINCLSIZE) 716 MCLGET(mb2, M_TRYWAIT); 717 mb->m_next = mb2; 718 mb = mb2; 719 mb->m_len = 0; 720 bpos = mtod(mb, caddr_t); 721 } 722 i = min(siz, M_TRAILINGSPACE(mb)); 723 bcopy(auth_str, bpos, i); 724 mb->m_len += i; 725 auth_str += i; 726 bpos += i; 727 siz -= i; 728 } 729 if ((siz = (nfsm_rndup(auth_len) - auth_len)) > 0) { 730 for (i = 0; i < siz; i++) 731 *bpos++ = '\0'; 732 mb->m_len += siz; 733 } 734 break; 735 }; 736 737 /* 738 * And the verifier... 739 */ 740 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 741 if (verf_str) { 742 *tl++ = txdr_unsigned(RPCAUTH_KERB4); 743 *tl = txdr_unsigned(verf_len); 744 siz = verf_len; 745 while (siz > 0) { 746 if (M_TRAILINGSPACE(mb) == 0) { 747 MGET(mb2, M_TRYWAIT, MT_DATA); 748 if (siz >= MINCLSIZE) 749 MCLGET(mb2, M_TRYWAIT); 750 mb->m_next = mb2; 751 mb = mb2; 752 mb->m_len = 0; 753 bpos = mtod(mb, caddr_t); 754 } 755 i = min(siz, M_TRAILINGSPACE(mb)); 756 bcopy(verf_str, bpos, i); 757 mb->m_len += i; 758 verf_str += i; 759 bpos += i; 760 siz -= i; 761 } 762 if ((siz = (nfsm_rndup(verf_len) - verf_len)) > 0) { 763 for (i = 0; i < siz; i++) 764 *bpos++ = '\0'; 765 mb->m_len += siz; 766 } 767 } else { 768 *tl++ = txdr_unsigned(RPCAUTH_NULL); 769 *tl = 0; 770 } 771 mb->m_next = mrest; 772 mreq->m_pkthdr.len = authsiz + 10 * NFSX_UNSIGNED + mrest_len; 773 mreq->m_pkthdr.rcvif = (struct ifnet *)0; 774 *mbp = mb; 775 return (mreq); 776} 777 778/* 779 * copies mbuf chain to the uio scatter/gather list 780 */ 781int 782nfsm_mbuftouio(mrep, uiop, siz, dpos) 783 struct mbuf **mrep; 784 register struct uio *uiop; 785 int siz; 786 caddr_t *dpos; 787{ 788 register char *mbufcp, *uiocp; 789 register int xfer, left, len; 790 register struct mbuf *mp; 791 long uiosiz, rem; 792 int error = 0; 793 794 mp = *mrep; 795 mbufcp = *dpos; 796 len = mtod(mp, caddr_t)+mp->m_len-mbufcp; 797 rem = nfsm_rndup(siz)-siz; 798 while (siz > 0) { 799 if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL) 800 return (EFBIG); 801 left = uiop->uio_iov->iov_len; 802 uiocp = uiop->uio_iov->iov_base; 803 if (left > siz) 804 left = siz; 805 uiosiz = left; 806 while (left > 0) { 807 while (len == 0) { 808 mp = mp->m_next; 809 if (mp == NULL) 810 return (EBADRPC); 811 mbufcp = mtod(mp, caddr_t); 812 len = mp->m_len; 813 } 814 xfer = (left > len) ? len : left; 815#ifdef notdef 816 /* Not Yet.. */ 817 if (uiop->uio_iov->iov_op != NULL) 818 (*(uiop->uio_iov->iov_op)) 819 (mbufcp, uiocp, xfer); 820 else 821#endif 822 if (uiop->uio_segflg == UIO_SYSSPACE) 823 bcopy(mbufcp, uiocp, xfer); 824 else 825 copyout(mbufcp, uiocp, xfer); 826 left -= xfer; 827 len -= xfer; 828 mbufcp += xfer; 829 uiocp += xfer; 830 uiop->uio_offset += xfer; 831 uiop->uio_resid -= xfer; 832 } 833 if (uiop->uio_iov->iov_len <= siz) { 834 uiop->uio_iovcnt--; 835 uiop->uio_iov++; 836 } else { 837 uiop->uio_iov->iov_base += uiosiz; 838 uiop->uio_iov->iov_len -= uiosiz; 839 } 840 siz -= uiosiz; 841 } 842 *dpos = mbufcp; 843 *mrep = mp; 844 if (rem > 0) { 845 if (len < rem) 846 error = nfs_adv(mrep, dpos, rem, len); 847 else 848 *dpos += rem; 849 } 850 return (error); 851} 852 853/* 854 * copies a uio scatter/gather list to an mbuf chain. 855 * NOTE: can ony handle iovcnt == 1 856 */ 857int 858nfsm_uiotombuf(uiop, mq, siz, bpos) 859 register struct uio *uiop; 860 struct mbuf **mq; 861 int siz; 862 caddr_t *bpos; 863{ 864 register char *uiocp; 865 register struct mbuf *mp, *mp2; 866 register int xfer, left, mlen; 867 int uiosiz, clflg, rem; 868 char *cp; 869 870#ifdef DIAGNOSTIC 871 if (uiop->uio_iovcnt != 1) 872 panic("nfsm_uiotombuf: iovcnt != 1"); 873#endif 874 875 if (siz > MLEN) /* or should it >= MCLBYTES ?? */ 876 clflg = 1; 877 else 878 clflg = 0; 879 rem = nfsm_rndup(siz)-siz; 880 mp = mp2 = *mq; 881 while (siz > 0) { 882 left = uiop->uio_iov->iov_len; 883 uiocp = uiop->uio_iov->iov_base; 884 if (left > siz) 885 left = siz; 886 uiosiz = left; 887 while (left > 0) { 888 mlen = M_TRAILINGSPACE(mp); 889 if (mlen == 0) { 890 MGET(mp, M_TRYWAIT, MT_DATA); 891 if (clflg) 892 MCLGET(mp, M_TRYWAIT); 893 mp->m_len = 0; 894 mp2->m_next = mp; 895 mp2 = mp; 896 mlen = M_TRAILINGSPACE(mp); 897 } 898 xfer = (left > mlen) ? mlen : left; 899#ifdef notdef 900 /* Not Yet.. */ 901 if (uiop->uio_iov->iov_op != NULL) 902 (*(uiop->uio_iov->iov_op)) 903 (uiocp, mtod(mp, caddr_t)+mp->m_len, xfer); 904 else 905#endif 906 if (uiop->uio_segflg == UIO_SYSSPACE) 907 bcopy(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer); 908 else 909 copyin(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer); 910 mp->m_len += xfer; 911 left -= xfer; 912 uiocp += xfer; 913 uiop->uio_offset += xfer; 914 uiop->uio_resid -= xfer; 915 } 916 uiop->uio_iov->iov_base += uiosiz; 917 uiop->uio_iov->iov_len -= uiosiz; 918 siz -= uiosiz; 919 } 920 if (rem > 0) { 921 if (rem > M_TRAILINGSPACE(mp)) { 922 MGET(mp, M_TRYWAIT, MT_DATA); 923 mp->m_len = 0; 924 mp2->m_next = mp; 925 } 926 cp = mtod(mp, caddr_t)+mp->m_len; 927 for (left = 0; left < rem; left++) 928 *cp++ = '\0'; 929 mp->m_len += rem; 930 *bpos = cp; 931 } else 932 *bpos = mtod(mp, caddr_t)+mp->m_len; 933 *mq = mp; 934 return (0); 935} 936 937/* 938 * Help break down an mbuf chain by setting the first siz bytes contiguous 939 * pointed to by returned val. 940 * This is used by the macros nfsm_dissect and nfsm_dissecton for tough 941 * cases. (The macros use the vars. dpos and dpos2) 942 */ 943int 944nfsm_disct(mdp, dposp, siz, left, cp2) 945 struct mbuf **mdp; 946 caddr_t *dposp; 947 int siz; 948 int left; 949 caddr_t *cp2; 950{ 951 register struct mbuf *mp, *mp2; 952 register int siz2, xfer; 953 register caddr_t ptr; 954 955 mp = *mdp; 956 while (left == 0) { 957 *mdp = mp = mp->m_next; 958 if (mp == NULL) 959 return (EBADRPC); 960 left = mp->m_len; 961 *dposp = mtod(mp, caddr_t); 962 } 963 if (left >= siz) { 964 *cp2 = *dposp; 965 *dposp += siz; 966 } else if (mp->m_next == NULL) { 967 return (EBADRPC); 968 } else if (siz > MHLEN) { 969 panic("nfs S too big"); 970 } else { 971 MGET(mp2, M_TRYWAIT, MT_DATA); 972 mp2->m_next = mp->m_next; 973 mp->m_next = mp2; 974 mp->m_len -= left; 975 mp = mp2; 976 *cp2 = ptr = mtod(mp, caddr_t); 977 bcopy(*dposp, ptr, left); /* Copy what was left */ 978 siz2 = siz-left; 979 ptr += left; 980 mp2 = mp->m_next; 981 /* Loop around copying up the siz2 bytes */ 982 while (siz2 > 0) { 983 if (mp2 == NULL) 984 return (EBADRPC); 985 xfer = (siz2 > mp2->m_len) ? mp2->m_len : siz2; 986 if (xfer > 0) { 987 bcopy(mtod(mp2, caddr_t), ptr, xfer); 988 NFSMADV(mp2, xfer); 989 mp2->m_len -= xfer; 990 ptr += xfer; 991 siz2 -= xfer; 992 } 993 if (siz2 > 0) 994 mp2 = mp2->m_next; 995 } 996 mp->m_len = siz; 997 *mdp = mp2; 998 *dposp = mtod(mp2, caddr_t); 999 } 1000 return (0); 1001} 1002 1003/* 1004 * Advance the position in the mbuf chain. 1005 */ 1006int 1007nfs_adv(mdp, dposp, offs, left) 1008 struct mbuf **mdp; 1009 caddr_t *dposp; 1010 int offs; 1011 int left; 1012{ 1013 register struct mbuf *m; 1014 register int s; 1015 1016 m = *mdp; 1017 s = left; 1018 while (s < offs) { 1019 offs -= s; 1020 m = m->m_next; 1021 if (m == NULL) 1022 return (EBADRPC); 1023 s = m->m_len; 1024 } 1025 *mdp = m; 1026 *dposp = mtod(m, caddr_t)+offs; 1027 return (0); 1028} 1029 1030/* 1031 * Copy a string into mbufs for the hard cases... 1032 */ 1033int 1034nfsm_strtmbuf(mb, bpos, cp, siz) 1035 struct mbuf **mb; 1036 char **bpos; 1037 const char *cp; 1038 long siz; 1039{ 1040 register struct mbuf *m1 = NULL, *m2; 1041 long left, xfer, len, tlen; 1042 u_int32_t *tl; 1043 int putsize; 1044 1045 putsize = 1; 1046 m2 = *mb; 1047 left = M_TRAILINGSPACE(m2); 1048 if (left > 0) { 1049 tl = ((u_int32_t *)(*bpos)); 1050 *tl++ = txdr_unsigned(siz); 1051 putsize = 0; 1052 left -= NFSX_UNSIGNED; 1053 m2->m_len += NFSX_UNSIGNED; 1054 if (left > 0) { 1055 bcopy(cp, (caddr_t) tl, left); 1056 siz -= left; 1057 cp += left; 1058 m2->m_len += left; 1059 left = 0; 1060 } 1061 } 1062 /* Loop around adding mbufs */ 1063 while (siz > 0) { 1064 MGET(m1, M_TRYWAIT, MT_DATA); 1065 if (siz > MLEN) 1066 MCLGET(m1, M_TRYWAIT); 1067 m1->m_len = NFSMSIZ(m1); 1068 m2->m_next = m1; 1069 m2 = m1; 1070 tl = mtod(m1, u_int32_t *); 1071 tlen = 0; 1072 if (putsize) { 1073 *tl++ = txdr_unsigned(siz); 1074 m1->m_len -= NFSX_UNSIGNED; 1075 tlen = NFSX_UNSIGNED; 1076 putsize = 0; 1077 } 1078 if (siz < m1->m_len) { 1079 len = nfsm_rndup(siz); 1080 xfer = siz; 1081 if (xfer < len) 1082 *(tl+(xfer>>2)) = 0; 1083 } else { 1084 xfer = len = m1->m_len; 1085 } 1086 bcopy(cp, (caddr_t) tl, xfer); 1087 m1->m_len = len+tlen; 1088 siz -= xfer; 1089 cp += xfer; 1090 } 1091 *mb = m1; 1092 *bpos = mtod(m1, caddr_t)+m1->m_len; 1093 return (0); 1094} 1095 1096/* 1097 * Called once to initialize data structures... 1098 */ 1099int 1100nfs_init(vfsp) 1101 struct vfsconf *vfsp; 1102{ 1103 register int i; 1104 1105 nfsmount_zone = zinit("NFSMOUNT", sizeof(struct nfsmount), 0, 0, 1); 1106 1107 nfs_mount_type = vfsp->vfc_typenum; 1108 nfsrtt.pos = 0; 1109 rpc_vers = txdr_unsigned(RPC_VER2); 1110 rpc_call = txdr_unsigned(RPC_CALL); 1111 rpc_reply = txdr_unsigned(RPC_REPLY); 1112 rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED); 1113 rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED); 1114 rpc_mismatch = txdr_unsigned(RPC_MISMATCH); 1115 rpc_autherr = txdr_unsigned(RPC_AUTHERR); 1116 rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX); 1117 rpc_auth_kerb = txdr_unsigned(RPCAUTH_KERB4); 1118 nfs_prog = txdr_unsigned(NFS_PROG); 1119 nqnfs_prog = txdr_unsigned(NQNFS_PROG); 1120 nfs_true = txdr_unsigned(TRUE); 1121 nfs_false = txdr_unsigned(FALSE); 1122 nfs_xdrneg1 = txdr_unsigned(-1); 1123 nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000; 1124 if (nfs_ticks < 1) 1125 nfs_ticks = 1; 1126 /* Ensure async daemons disabled */ 1127 for (i = 0; i < NFS_MAXASYNCDAEMON; i++) { 1128 nfs_iodwant[i] = (struct proc *)0; 1129 nfs_iodmount[i] = (struct nfsmount *)0; 1130 } 1131 nfs_nhinit(); /* Init the nfsnode table */ 1132#ifndef NFS_NOSERVER 1133 nfsrv_init(0); /* Init server data structures */ 1134 nfsrv_initcache(); /* Init the server request cache */ 1135#endif 1136 1137 /* 1138 * Initialize the nqnfs server stuff. 1139 */ 1140 if (nqnfsstarttime == 0) { 1141 nqnfsstarttime = boottime.tv_sec + nqsrv_maxlease 1142 + nqsrv_clockskew + nqsrv_writeslack; 1143 NQLOADNOVRAM(nqnfsstarttime); 1144 TAILQ_INIT(&nqtimerhead); 1145 nqfhhashtbl = hashinit(NQLCHSZ, M_NQLEASE, &nqfhhash); 1146 } 1147 1148 /* 1149 * Initialize reply list and start timer 1150 */ 1151 TAILQ_INIT(&nfs_reqq); 1152 1153 nfs_timer(0); 1154 1155 /* 1156 * Set up lease_check and lease_updatetime so that other parts 1157 * of the system can call us, if we are loadable. 1158 */ 1159#ifndef NFS_NOSERVER 1160 nfs_prev_vop_lease_check = default_vnodeop_p[VOFFSET(vop_lease)]; 1161 default_vnodeop_p[VOFFSET(vop_lease)] = (vop_t *)nqnfs_vop_lease_check; 1162#endif 1163 nfs_prev_lease_updatetime = lease_updatetime; 1164 lease_updatetime = nfs_lease_updatetime; 1165 nfs_prev_nfssvc_sy_narg = sysent[SYS_nfssvc].sy_narg; 1166 sysent[SYS_nfssvc].sy_narg = 2; 1167 nfs_prev_nfssvc_sy_call = sysent[SYS_nfssvc].sy_call; 1168 sysent[SYS_nfssvc].sy_call = (sy_call_t *)nfssvc; 1169 1170 nfs_pbuf_freecnt = nswbuf / 2 + 1; 1171 1172 return (0); 1173} 1174 1175int 1176nfs_uninit(vfsp) 1177 struct vfsconf *vfsp; 1178{ 1179 1180 untimeout(nfs_timer, (void *)NULL, nfs_timer_handle); 1181 nfs_mount_type = -1; 1182#ifndef NFS_NOSERVER 1183 default_vnodeop_p[VOFFSET(vop_lease)] = nfs_prev_vop_lease_check; 1184#endif 1185 lease_updatetime = nfs_prev_lease_updatetime; 1186 sysent[SYS_nfssvc].sy_narg = nfs_prev_nfssvc_sy_narg; 1187 sysent[SYS_nfssvc].sy_call = nfs_prev_nfssvc_sy_call; 1188 return (0); 1189} 1190 1191/* 1192 * Attribute cache routines. 1193 * nfs_loadattrcache() - loads or updates the cache contents from attributes 1194 * that are on the mbuf list 1195 * nfs_getattrcache() - returns valid attributes if found in cache, returns 1196 * error otherwise 1197 */ 1198 1199/* 1200 * Load the attribute cache (that lives in the nfsnode entry) with 1201 * the values on the mbuf list and 1202 * Iff vap not NULL 1203 * copy the attributes to *vaper 1204 */ 1205int 1206nfs_loadattrcache(vpp, mdp, dposp, vaper, dontshrink) 1207 struct vnode **vpp; 1208 struct mbuf **mdp; 1209 caddr_t *dposp; 1210 struct vattr *vaper; 1211 int dontshrink; 1212{ 1213 register struct vnode *vp = *vpp; 1214 register struct vattr *vap; 1215 register struct nfs_fattr *fp; 1216 register struct nfsnode *np; 1217 register int32_t t1; 1218 caddr_t cp2; 1219 int error = 0, rdev; 1220 struct mbuf *md; 1221 enum vtype vtyp; 1222 u_short vmode; 1223 struct timespec mtime; 1224 int v3 = NFS_ISV3(vp); 1225 1226 md = *mdp; 1227 t1 = (mtod(md, caddr_t) + md->m_len) - *dposp; 1228 if ((error = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, &cp2)) != 0) 1229 return (error); 1230 fp = (struct nfs_fattr *)cp2; 1231 if (v3) { 1232 vtyp = nfsv3tov_type(fp->fa_type); 1233 vmode = fxdr_unsigned(u_short, fp->fa_mode); 1234 rdev = makeudev(fxdr_unsigned(int, fp->fa3_rdev.specdata1), 1235 fxdr_unsigned(int, fp->fa3_rdev.specdata2)); 1236 fxdr_nfsv3time(&fp->fa3_mtime, &mtime); 1237 } else { 1238 vtyp = nfsv2tov_type(fp->fa_type); 1239 vmode = fxdr_unsigned(u_short, fp->fa_mode); 1240 /* 1241 * XXX 1242 * 1243 * The duplicate information returned in fa_type and fa_mode 1244 * is an ambiguity in the NFS version 2 protocol. 1245 * 1246 * VREG should be taken literally as a regular file. If a 1247 * server intents to return some type information differently 1248 * in the upper bits of the mode field (e.g. for sockets, or 1249 * FIFOs), NFSv2 mandates fa_type to be VNON. Anyway, we 1250 * leave the examination of the mode bits even in the VREG 1251 * case to avoid breakage for bogus servers, but we make sure 1252 * that there are actually type bits set in the upper part of 1253 * fa_mode (and failing that, trust the va_type field). 1254 * 1255 * NFSv3 cleared the issue, and requires fa_mode to not 1256 * contain any type information (while also introduing sockets 1257 * and FIFOs for fa_type). 1258 */ 1259 if (vtyp == VNON || (vtyp == VREG && (vmode & S_IFMT) != 0)) 1260 vtyp = IFTOVT(vmode); 1261 rdev = fxdr_unsigned(int32_t, fp->fa2_rdev); 1262 fxdr_nfsv2time(&fp->fa2_mtime, &mtime); 1263 1264 /* 1265 * Really ugly NFSv2 kludge. 1266 */ 1267 if (vtyp == VCHR && rdev == 0xffffffff) 1268 vtyp = VFIFO; 1269 } 1270 1271 /* 1272 * If v_type == VNON it is a new node, so fill in the v_type, 1273 * n_mtime fields. Check to see if it represents a special 1274 * device, and if so, check for a possible alias. Once the 1275 * correct vnode has been obtained, fill in the rest of the 1276 * information. 1277 */ 1278 np = VTONFS(vp); 1279 if (vp->v_type != vtyp) { 1280 vp->v_type = vtyp; 1281 if (vp->v_type == VFIFO) { 1282 vp->v_op = fifo_nfsv2nodeop_p; 1283 } 1284 if (vp->v_type == VCHR || vp->v_type == VBLK) { 1285 vp->v_op = spec_nfsv2nodeop_p; 1286 vp = addaliasu(vp, rdev); 1287 np->n_vnode = vp; 1288 } 1289 np->n_mtime = mtime.tv_sec; 1290 } 1291 vap = &np->n_vattr; 1292 vap->va_type = vtyp; 1293 vap->va_mode = (vmode & 07777); 1294 vap->va_rdev = rdev; 1295 vap->va_mtime = mtime; 1296 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0]; 1297 if (v3) { 1298 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink); 1299 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid); 1300 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid); 1301 vap->va_size = fxdr_hyper(&fp->fa3_size); 1302 vap->va_blocksize = NFS_FABLKSIZE; 1303 vap->va_bytes = fxdr_hyper(&fp->fa3_used); 1304 vap->va_fileid = fxdr_unsigned(int32_t, 1305 fp->fa3_fileid.nfsuquad[1]); 1306 fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime); 1307 fxdr_nfsv3time(&fp->fa3_ctime, &vap->va_ctime); 1308 vap->va_flags = 0; 1309 vap->va_filerev = 0; 1310 } else { 1311 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink); 1312 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid); 1313 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid); 1314 vap->va_size = fxdr_unsigned(u_int32_t, fp->fa2_size); 1315 vap->va_blocksize = fxdr_unsigned(int32_t, fp->fa2_blocksize); 1316 vap->va_bytes = (u_quad_t)fxdr_unsigned(int32_t, fp->fa2_blocks) 1317 * NFS_FABLKSIZE; 1318 vap->va_fileid = fxdr_unsigned(int32_t, fp->fa2_fileid); 1319 fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime); 1320 vap->va_flags = 0; 1321 vap->va_ctime.tv_sec = fxdr_unsigned(u_int32_t, 1322 fp->fa2_ctime.nfsv2_sec); 1323 vap->va_ctime.tv_nsec = 0; 1324 vap->va_gen = fxdr_unsigned(u_int32_t,fp->fa2_ctime.nfsv2_usec); 1325 vap->va_filerev = 0; 1326 } 1327 np->n_attrstamp = time_second; 1328 if (vap->va_size != np->n_size) { 1329 if (vap->va_type == VREG) { 1330 if (dontshrink && vap->va_size < np->n_size) { 1331 /* 1332 * We've been told not to shrink the file; 1333 * zero np->n_attrstamp to indicate that 1334 * the attributes are stale. 1335 */ 1336 vap->va_size = np->n_size; 1337 np->n_attrstamp = 0; 1338 } else if (np->n_flag & NMODIFIED) { 1339 if (vap->va_size < np->n_size) 1340 vap->va_size = np->n_size; 1341 else 1342 np->n_size = vap->va_size; 1343 } else { 1344 np->n_size = vap->va_size; 1345 } 1346 vnode_pager_setsize(vp, np->n_size); 1347 } else { 1348 np->n_size = vap->va_size; 1349 } 1350 } 1351 if (vaper != NULL) { 1352 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap)); 1353 if (np->n_flag & NCHG) { 1354 if (np->n_flag & NACC) 1355 vaper->va_atime = np->n_atim; 1356 if (np->n_flag & NUPD) 1357 vaper->va_mtime = np->n_mtim; 1358 } 1359 } 1360 return (0); 1361} 1362 1363#ifdef NFS_ACDEBUG 1364#include <sys/sysctl.h> 1365SYSCTL_DECL(_vfs_nfs); 1366static int nfs_acdebug; 1367SYSCTL_INT(_vfs_nfs, OID_AUTO, acdebug, CTLFLAG_RW, &nfs_acdebug, 0, ""); 1368#endif 1369 1370/* 1371 * Check the time stamp 1372 * If the cache is valid, copy contents to *vap and return 0 1373 * otherwise return an error 1374 */ 1375int 1376nfs_getattrcache(vp, vaper) 1377 register struct vnode *vp; 1378 struct vattr *vaper; 1379{ 1380 register struct nfsnode *np; 1381 register struct vattr *vap; 1382 struct nfsmount *nmp; 1383 int timeo; 1384 1385 np = VTONFS(vp); 1386 vap = &np->n_vattr; 1387 nmp = VFSTONFS(vp->v_mount); 1388 /* XXX n_mtime doesn't seem to be updated on a miss-and-reload */ 1389 timeo = (time_second - np->n_mtime) / 10; 1390 1391#ifdef NFS_ACDEBUG 1392 if (nfs_acdebug>1) 1393 printf("nfs_getattrcache: initial timeo = %d\n", timeo); 1394#endif 1395 1396 if (vap->va_type == VDIR) { 1397 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acdirmin) 1398 timeo = nmp->nm_acdirmin; 1399 else if (timeo > nmp->nm_acdirmax) 1400 timeo = nmp->nm_acdirmax; 1401 } else { 1402 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acregmin) 1403 timeo = nmp->nm_acregmin; 1404 else if (timeo > nmp->nm_acregmax) 1405 timeo = nmp->nm_acregmax; 1406 } 1407 1408#ifdef NFS_ACDEBUG 1409 if (nfs_acdebug > 2) 1410 printf("acregmin %d; acregmax %d; acdirmin %d; acdirmax %d\n", 1411 nmp->nm_acregmin, nmp->nm_acregmax, 1412 nmp->nm_acdirmin, nmp->nm_acdirmax); 1413 1414 if (nfs_acdebug) 1415 printf("nfs_getattrcache: age = %d; final timeo = %d\n", 1416 (time_second - np->n_attrstamp), timeo); 1417#endif 1418 1419 if ((time_second - np->n_attrstamp) >= timeo) { 1420 nfsstats.attrcache_misses++; 1421 return (ENOENT); 1422 } 1423 nfsstats.attrcache_hits++; 1424 if (vap->va_size != np->n_size) { 1425 if (vap->va_type == VREG) { 1426 if (np->n_flag & NMODIFIED) { 1427 if (vap->va_size < np->n_size) 1428 vap->va_size = np->n_size; 1429 else 1430 np->n_size = vap->va_size; 1431 } else { 1432 np->n_size = vap->va_size; 1433 } 1434 vnode_pager_setsize(vp, np->n_size); 1435 } else { 1436 np->n_size = vap->va_size; 1437 } 1438 } 1439 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr)); 1440 if (np->n_flag & NCHG) { 1441 if (np->n_flag & NACC) 1442 vaper->va_atime = np->n_atim; 1443 if (np->n_flag & NUPD) 1444 vaper->va_mtime = np->n_mtim; 1445 } 1446 return (0); 1447} 1448 1449#ifndef NFS_NOSERVER 1450/* 1451 * Set up nameidata for a lookup() call and do it. 1452 * 1453 * If pubflag is set, this call is done for a lookup operation on the 1454 * public filehandle. In that case we allow crossing mountpoints and 1455 * absolute pathnames. However, the caller is expected to check that 1456 * the lookup result is within the public fs, and deny access if 1457 * it is not. 1458 * 1459 * nfs_namei() clears out garbage fields that namei() might leave garbage. 1460 * This is mainly ni_vp and ni_dvp when an error occurs, and ni_dvp when no 1461 * error occurs but the parent was not requested. 1462 * 1463 * dirp may be set whether an error is returned or not, and must be 1464 * released by the caller. 1465 */ 1466int 1467nfs_namei(ndp, fhp, len, slp, nam, mdp, dposp, retdirp, td, kerbflag, pubflag) 1468 register struct nameidata *ndp; 1469 fhandle_t *fhp; 1470 int len; 1471 struct nfssvc_sock *slp; 1472 struct sockaddr *nam; 1473 struct mbuf **mdp; 1474 caddr_t *dposp; 1475 struct vnode **retdirp; 1476 struct thread *td; 1477 int kerbflag, pubflag; 1478{ 1479 register int i, rem; 1480 register struct mbuf *md; 1481 register char *fromcp, *tocp, *cp; 1482 struct iovec aiov; 1483 struct uio auio; 1484 struct vnode *dp; 1485 int error, rdonly, linklen; 1486 struct componentname *cnp = &ndp->ni_cnd; 1487 1488 *retdirp = (struct vnode *)0; 1489 cnp->cn_pnbuf = zalloc(namei_zone); 1490 1491 /* 1492 * Copy the name from the mbuf list to ndp->ni_pnbuf 1493 * and set the various ndp fields appropriately. 1494 */ 1495 fromcp = *dposp; 1496 tocp = cnp->cn_pnbuf; 1497 md = *mdp; 1498 rem = mtod(md, caddr_t) + md->m_len - fromcp; 1499 for (i = 0; i < len; i++) { 1500 while (rem == 0) { 1501 md = md->m_next; 1502 if (md == NULL) { 1503 error = EBADRPC; 1504 goto out; 1505 } 1506 fromcp = mtod(md, caddr_t); 1507 rem = md->m_len; 1508 } 1509 if (*fromcp == '\0' || (!pubflag && *fromcp == '/')) { 1510 error = EACCES; 1511 goto out; 1512 } 1513 *tocp++ = *fromcp++; 1514 rem--; 1515 } 1516 *tocp = '\0'; 1517 *mdp = md; 1518 *dposp = fromcp; 1519 len = nfsm_rndup(len)-len; 1520 if (len > 0) { 1521 if (rem >= len) 1522 *dposp += len; 1523 else if ((error = nfs_adv(mdp, dposp, len, rem)) != 0) 1524 goto out; 1525 } 1526 1527 /* 1528 * Extract and set starting directory. 1529 */ 1530 error = nfsrv_fhtovp(fhp, FALSE, &dp, ndp->ni_cnd.cn_cred, slp, 1531 nam, &rdonly, kerbflag, pubflag); 1532 if (error) 1533 goto out; 1534 if (dp->v_type != VDIR) { 1535 vrele(dp); 1536 error = ENOTDIR; 1537 goto out; 1538 } 1539 1540 if (rdonly) 1541 cnp->cn_flags |= RDONLY; 1542 1543 /* 1544 * Set return directory. Reference to dp is implicitly transfered 1545 * to the returned pointer 1546 */ 1547 *retdirp = dp; 1548 1549 if (pubflag) { 1550 /* 1551 * Oh joy. For WebNFS, handle those pesky '%' escapes, 1552 * and the 'native path' indicator. 1553 */ 1554 cp = zalloc(namei_zone); 1555 fromcp = cnp->cn_pnbuf; 1556 tocp = cp; 1557 if ((unsigned char)*fromcp >= WEBNFS_SPECCHAR_START) { 1558 switch ((unsigned char)*fromcp) { 1559 case WEBNFS_NATIVE_CHAR: 1560 /* 1561 * 'Native' path for us is the same 1562 * as a path according to the NFS spec, 1563 * just skip the escape char. 1564 */ 1565 fromcp++; 1566 break; 1567 /* 1568 * More may be added in the future, range 0x80-0xff 1569 */ 1570 default: 1571 error = EIO; 1572 zfree(namei_zone, cp); 1573 goto out; 1574 } 1575 } 1576 /* 1577 * Translate the '%' escapes, URL-style. 1578 */ 1579 while (*fromcp != '\0') { 1580 if (*fromcp == WEBNFS_ESC_CHAR) { 1581 if (fromcp[1] != '\0' && fromcp[2] != '\0') { 1582 fromcp++; 1583 *tocp++ = HEXSTRTOI(fromcp); 1584 fromcp += 2; 1585 continue; 1586 } else { 1587 error = ENOENT; 1588 zfree(namei_zone, cp); 1589 goto out; 1590 } 1591 } else 1592 *tocp++ = *fromcp++; 1593 } 1594 *tocp = '\0'; 1595 zfree(namei_zone, cnp->cn_pnbuf); 1596 cnp->cn_pnbuf = cp; 1597 } 1598 1599 ndp->ni_pathlen = (tocp - cnp->cn_pnbuf) + 1; 1600 ndp->ni_segflg = UIO_SYSSPACE; 1601 1602 if (pubflag) { 1603 ndp->ni_rootdir = rootvnode; 1604 ndp->ni_loopcnt = 0; 1605 if (cnp->cn_pnbuf[0] == '/') 1606 dp = rootvnode; 1607 } else { 1608 cnp->cn_flags |= NOCROSSMOUNT; 1609 } 1610 1611 /* 1612 * Initialize for scan, set ni_startdir and bump ref on dp again 1613 * becuase lookup() will dereference ni_startdir. 1614 */ 1615 1616 cnp->cn_thread = td; 1617 VREF(dp); 1618 ndp->ni_startdir = dp; 1619 1620 for (;;) { 1621 cnp->cn_nameptr = cnp->cn_pnbuf; 1622 /* 1623 * Call lookup() to do the real work. If an error occurs, 1624 * ndp->ni_vp and ni_dvp are left uninitialized or NULL and 1625 * we do not have to dereference anything before returning. 1626 * In either case ni_startdir will be dereferenced and NULLed 1627 * out. 1628 */ 1629 error = lookup(ndp); 1630 if (error) 1631 break; 1632 1633 /* 1634 * Check for encountering a symbolic link. Trivial 1635 * termination occurs if no symlink encountered. 1636 * Note: zfree is safe because error is 0, so we will 1637 * not zfree it again when we break. 1638 */ 1639 if ((cnp->cn_flags & ISSYMLINK) == 0) { 1640 nfsrv_object_create(ndp->ni_vp); 1641 if (cnp->cn_flags & (SAVENAME | SAVESTART)) 1642 cnp->cn_flags |= HASBUF; 1643 else 1644 zfree(namei_zone, cnp->cn_pnbuf); 1645 break; 1646 } 1647 1648 /* 1649 * Validate symlink 1650 */ 1651 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1) 1652 VOP_UNLOCK(ndp->ni_dvp, 0, td); 1653 if (!pubflag) { 1654 error = EINVAL; 1655 goto badlink2; 1656 } 1657 1658 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) { 1659 error = ELOOP; 1660 goto badlink2; 1661 } 1662 if (ndp->ni_pathlen > 1) 1663 cp = zalloc(namei_zone); 1664 else 1665 cp = cnp->cn_pnbuf; 1666 aiov.iov_base = cp; 1667 aiov.iov_len = MAXPATHLEN; 1668 auio.uio_iov = &aiov; 1669 auio.uio_iovcnt = 1; 1670 auio.uio_offset = 0; 1671 auio.uio_rw = UIO_READ; 1672 auio.uio_segflg = UIO_SYSSPACE; 1673 auio.uio_td = (struct thread *)0; 1674 auio.uio_resid = MAXPATHLEN; 1675 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred); 1676 if (error) { 1677 badlink1: 1678 if (ndp->ni_pathlen > 1) 1679 zfree(namei_zone, cp); 1680 badlink2: 1681 vrele(ndp->ni_dvp); 1682 vput(ndp->ni_vp); 1683 break; 1684 } 1685 linklen = MAXPATHLEN - auio.uio_resid; 1686 if (linklen == 0) { 1687 error = ENOENT; 1688 goto badlink1; 1689 } 1690 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) { 1691 error = ENAMETOOLONG; 1692 goto badlink1; 1693 } 1694 1695 /* 1696 * Adjust or replace path 1697 */ 1698 if (ndp->ni_pathlen > 1) { 1699 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen); 1700 zfree(namei_zone, cnp->cn_pnbuf); 1701 cnp->cn_pnbuf = cp; 1702 } else 1703 cnp->cn_pnbuf[linklen] = '\0'; 1704 ndp->ni_pathlen += linklen; 1705 1706 /* 1707 * Cleanup refs for next loop and check if root directory 1708 * should replace current directory. Normally ni_dvp 1709 * becomes the new base directory and is cleaned up when 1710 * we loop. Explicitly null pointers after invalidation 1711 * to clarify operation. 1712 */ 1713 vput(ndp->ni_vp); 1714 ndp->ni_vp = NULL; 1715 1716 if (cnp->cn_pnbuf[0] == '/') { 1717 vrele(ndp->ni_dvp); 1718 ndp->ni_dvp = ndp->ni_rootdir; 1719 VREF(ndp->ni_dvp); 1720 } 1721 ndp->ni_startdir = ndp->ni_dvp; 1722 ndp->ni_dvp = NULL; 1723 } 1724 1725 /* 1726 * nfs_namei() guarentees that fields will not contain garbage 1727 * whether an error occurs or not. This allows the caller to track 1728 * cleanup state trivially. 1729 */ 1730out: 1731 if (error) { 1732 zfree(namei_zone, cnp->cn_pnbuf); 1733 ndp->ni_vp = NULL; 1734 ndp->ni_dvp = NULL; 1735 ndp->ni_startdir = NULL; 1736 cnp->cn_flags &= ~HASBUF; 1737 } else if ((ndp->ni_cnd.cn_flags & (WANTPARENT|LOCKPARENT)) == 0) { 1738 ndp->ni_dvp = NULL; 1739 } 1740 return (error); 1741} 1742 1743/* 1744 * A fiddled version of m_adj() that ensures null fill to a long 1745 * boundary and only trims off the back end 1746 */ 1747void 1748nfsm_adj(mp, len, nul) 1749 struct mbuf *mp; 1750 register int len; 1751 int nul; 1752{ 1753 register struct mbuf *m; 1754 register int count, i; 1755 register char *cp; 1756 1757 /* 1758 * Trim from tail. Scan the mbuf chain, 1759 * calculating its length and finding the last mbuf. 1760 * If the adjustment only affects this mbuf, then just 1761 * adjust and return. Otherwise, rescan and truncate 1762 * after the remaining size. 1763 */ 1764 count = 0; 1765 m = mp; 1766 for (;;) { 1767 count += m->m_len; 1768 if (m->m_next == (struct mbuf *)0) 1769 break; 1770 m = m->m_next; 1771 } 1772 if (m->m_len > len) { 1773 m->m_len -= len; 1774 if (nul > 0) { 1775 cp = mtod(m, caddr_t)+m->m_len-nul; 1776 for (i = 0; i < nul; i++) 1777 *cp++ = '\0'; 1778 } 1779 return; 1780 } 1781 count -= len; 1782 if (count < 0) 1783 count = 0; 1784 /* 1785 * Correct length for chain is "count". 1786 * Find the mbuf with last data, adjust its length, 1787 * and toss data from remaining mbufs on chain. 1788 */ 1789 for (m = mp; m; m = m->m_next) { 1790 if (m->m_len >= count) { 1791 m->m_len = count; 1792 if (nul > 0) { 1793 cp = mtod(m, caddr_t)+m->m_len-nul; 1794 for (i = 0; i < nul; i++) 1795 *cp++ = '\0'; 1796 } 1797 break; 1798 } 1799 count -= m->m_len; 1800 } 1801 for (m = m->m_next;m;m = m->m_next) 1802 m->m_len = 0; 1803} 1804 1805/* 1806 * Make these functions instead of macros, so that the kernel text size 1807 * doesn't get too big... 1808 */ 1809void 1810nfsm_srvwcc(nfsd, before_ret, before_vap, after_ret, after_vap, mbp, bposp) 1811 struct nfsrv_descript *nfsd; 1812 int before_ret; 1813 register struct vattr *before_vap; 1814 int after_ret; 1815 struct vattr *after_vap; 1816 struct mbuf **mbp; 1817 char **bposp; 1818{ 1819 register struct mbuf *mb = *mbp, *mb2; 1820 register char *bpos = *bposp; 1821 register u_int32_t *tl; 1822 1823 if (before_ret) { 1824 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); 1825 *tl = nfs_false; 1826 } else { 1827 nfsm_build(tl, u_int32_t *, 7 * NFSX_UNSIGNED); 1828 *tl++ = nfs_true; 1829 txdr_hyper(before_vap->va_size, tl); 1830 tl += 2; 1831 txdr_nfsv3time(&(before_vap->va_mtime), tl); 1832 tl += 2; 1833 txdr_nfsv3time(&(before_vap->va_ctime), tl); 1834 } 1835 *bposp = bpos; 1836 *mbp = mb; 1837 nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp); 1838} 1839 1840void 1841nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp) 1842 struct nfsrv_descript *nfsd; 1843 int after_ret; 1844 struct vattr *after_vap; 1845 struct mbuf **mbp; 1846 char **bposp; 1847{ 1848 register struct mbuf *mb = *mbp, *mb2; 1849 register char *bpos = *bposp; 1850 register u_int32_t *tl; 1851 register struct nfs_fattr *fp; 1852 1853 if (after_ret) { 1854 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); 1855 *tl = nfs_false; 1856 } else { 1857 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FATTR); 1858 *tl++ = nfs_true; 1859 fp = (struct nfs_fattr *)tl; 1860 nfsm_srvfattr(nfsd, after_vap, fp); 1861 } 1862 *mbp = mb; 1863 *bposp = bpos; 1864} 1865 1866void 1867nfsm_srvfattr(nfsd, vap, fp) 1868 register struct nfsrv_descript *nfsd; 1869 register struct vattr *vap; 1870 register struct nfs_fattr *fp; 1871{ 1872 1873 fp->fa_nlink = txdr_unsigned(vap->va_nlink); 1874 fp->fa_uid = txdr_unsigned(vap->va_uid); 1875 fp->fa_gid = txdr_unsigned(vap->va_gid); 1876 if (nfsd->nd_flag & ND_NFSV3) { 1877 fp->fa_type = vtonfsv3_type(vap->va_type); 1878 fp->fa_mode = vtonfsv3_mode(vap->va_mode); 1879 txdr_hyper(vap->va_size, &fp->fa3_size); 1880 txdr_hyper(vap->va_bytes, &fp->fa3_used); 1881 fp->fa3_rdev.specdata1 = txdr_unsigned(umajor(vap->va_rdev)); 1882 fp->fa3_rdev.specdata2 = txdr_unsigned(uminor(vap->va_rdev)); 1883 fp->fa3_fsid.nfsuquad[0] = 0; 1884 fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(vap->va_fsid); 1885 fp->fa3_fileid.nfsuquad[0] = 0; 1886 fp->fa3_fileid.nfsuquad[1] = txdr_unsigned(vap->va_fileid); 1887 txdr_nfsv3time(&vap->va_atime, &fp->fa3_atime); 1888 txdr_nfsv3time(&vap->va_mtime, &fp->fa3_mtime); 1889 txdr_nfsv3time(&vap->va_ctime, &fp->fa3_ctime); 1890 } else { 1891 fp->fa_type = vtonfsv2_type(vap->va_type); 1892 fp->fa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode); 1893 fp->fa2_size = txdr_unsigned(vap->va_size); 1894 fp->fa2_blocksize = txdr_unsigned(vap->va_blocksize); 1895 if (vap->va_type == VFIFO) 1896 fp->fa2_rdev = 0xffffffff; 1897 else 1898 fp->fa2_rdev = txdr_unsigned(vap->va_rdev); 1899 fp->fa2_blocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE); 1900 fp->fa2_fsid = txdr_unsigned(vap->va_fsid); 1901 fp->fa2_fileid = txdr_unsigned(vap->va_fileid); 1902 txdr_nfsv2time(&vap->va_atime, &fp->fa2_atime); 1903 txdr_nfsv2time(&vap->va_mtime, &fp->fa2_mtime); 1904 txdr_nfsv2time(&vap->va_ctime, &fp->fa2_ctime); 1905 } 1906} 1907 1908/* 1909 * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked) 1910 * - look up fsid in mount list (if not found ret error) 1911 * - get vp and export rights by calling VFS_FHTOVP() 1912 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon 1913 * - if not lockflag unlock it with VOP_UNLOCK() 1914 */ 1915int 1916nfsrv_fhtovp(fhp, lockflag, vpp, cred, slp, nam, rdonlyp, kerbflag, pubflag) 1917 fhandle_t *fhp; 1918 int lockflag; 1919 struct vnode **vpp; 1920 struct ucred *cred; 1921 struct nfssvc_sock *slp; 1922 struct sockaddr *nam; 1923 int *rdonlyp; 1924 int kerbflag; 1925 int pubflag; 1926{ 1927 struct thread *td = curthread; /* XXX */ 1928 register struct mount *mp; 1929 register int i; 1930 struct ucred *credanon; 1931 int error, exflags; 1932#ifdef MNT_EXNORESPORT /* XXX needs mountd and /etc/exports help yet */ 1933 struct sockaddr_int *saddr; 1934#endif 1935 1936 *vpp = (struct vnode *)0; 1937 1938 if (nfs_ispublicfh(fhp)) { 1939 if (!pubflag || !nfs_pub.np_valid) 1940 return (ESTALE); 1941 fhp = &nfs_pub.np_handle; 1942 } 1943 1944 mp = vfs_getvfs(&fhp->fh_fsid); 1945 if (!mp) 1946 return (ESTALE); 1947 error = VFS_CHECKEXP(mp, nam, &exflags, &credanon); 1948 if (error) 1949 return (error); 1950 error = VFS_FHTOVP(mp, &fhp->fh_fid, vpp); 1951 if (error) 1952 return (error); 1953#ifdef MNT_EXNORESPORT 1954 if (!(exflags & (MNT_EXNORESPORT|MNT_EXPUBLIC))) { 1955 saddr = (struct sockaddr_in *)nam; 1956 if (saddr->sin_family == AF_INET && 1957 ntohs(saddr->sin_port) >= IPPORT_RESERVED) { 1958 vput(*vpp); 1959 *vpp = NULL; 1960 return (NFSERR_AUTHERR | AUTH_TOOWEAK); 1961 } 1962 } 1963#endif 1964 /* 1965 * Check/setup credentials. 1966 */ 1967 if (exflags & MNT_EXKERB) { 1968 if (!kerbflag) { 1969 vput(*vpp); 1970 *vpp = NULL; 1971 return (NFSERR_AUTHERR | AUTH_TOOWEAK); 1972 } 1973 } else if (kerbflag) { 1974 vput(*vpp); 1975 *vpp = NULL; 1976 return (NFSERR_AUTHERR | AUTH_TOOWEAK); 1977 } else if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) { 1978 cred->cr_uid = credanon->cr_uid; 1979 for (i = 0; i < credanon->cr_ngroups && i < NGROUPS; i++) 1980 cred->cr_groups[i] = credanon->cr_groups[i]; 1981 cred->cr_ngroups = i; 1982 } 1983 if (exflags & MNT_EXRDONLY) 1984 *rdonlyp = 1; 1985 else 1986 *rdonlyp = 0; 1987 1988 nfsrv_object_create(*vpp); 1989 1990 if (!lockflag) 1991 VOP_UNLOCK(*vpp, 0, td); 1992 return (0); 1993} 1994 1995 1996/* 1997 * WebNFS: check if a filehandle is a public filehandle. For v3, this 1998 * means a length of 0, for v2 it means all zeroes. nfsm_srvmtofh has 1999 * transformed this to all zeroes in both cases, so check for it. 2000 */ 2001int 2002nfs_ispublicfh(fhp) 2003 fhandle_t *fhp; 2004{ 2005 char *cp = (char *)fhp; 2006 int i; 2007 2008 for (i = 0; i < NFSX_V3FH; i++) 2009 if (*cp++ != 0) 2010 return (FALSE); 2011 return (TRUE); 2012} 2013 2014#endif /* NFS_NOSERVER */ 2015/* 2016 * This function compares two net addresses by family and returns TRUE 2017 * if they are the same host. 2018 * If there is any doubt, return FALSE. 2019 * The AF_INET family is handled as a special case so that address mbufs 2020 * don't need to be saved to store "struct in_addr", which is only 4 bytes. 2021 */ 2022int 2023netaddr_match(family, haddr, nam) 2024 int family; 2025 union nethostaddr *haddr; 2026 struct sockaddr *nam; 2027{ 2028 register struct sockaddr_in *inetaddr; 2029 2030 switch (family) { 2031 case AF_INET: 2032 inetaddr = (struct sockaddr_in *)nam; 2033 if (inetaddr->sin_family == AF_INET && 2034 inetaddr->sin_addr.s_addr == haddr->had_inetaddr) 2035 return (1); 2036 break; 2037 default: 2038 break; 2039 }; 2040 return (0); 2041} 2042 2043static nfsuint64 nfs_nullcookie = { { 0, 0 } }; 2044/* 2045 * This function finds the directory cookie that corresponds to the 2046 * logical byte offset given. 2047 */ 2048nfsuint64 * 2049nfs_getcookie(np, off, add) 2050 register struct nfsnode *np; 2051 off_t off; 2052 int add; 2053{ 2054 register struct nfsdmap *dp, *dp2; 2055 register int pos; 2056 2057 pos = (uoff_t)off / NFS_DIRBLKSIZ; 2058 if (pos == 0 || off < 0) { 2059#ifdef DIAGNOSTIC 2060 if (add) 2061 panic("nfs getcookie add at <= 0"); 2062#endif 2063 return (&nfs_nullcookie); 2064 } 2065 pos--; 2066 dp = np->n_cookies.lh_first; 2067 if (!dp) { 2068 if (add) { 2069 MALLOC(dp, struct nfsdmap *, sizeof (struct nfsdmap), 2070 M_NFSDIROFF, M_WAITOK); 2071 dp->ndm_eocookie = 0; 2072 LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list); 2073 } else 2074 return ((nfsuint64 *)0); 2075 } 2076 while (pos >= NFSNUMCOOKIES) { 2077 pos -= NFSNUMCOOKIES; 2078 if (dp->ndm_list.le_next) { 2079 if (!add && dp->ndm_eocookie < NFSNUMCOOKIES && 2080 pos >= dp->ndm_eocookie) 2081 return ((nfsuint64 *)0); 2082 dp = dp->ndm_list.le_next; 2083 } else if (add) { 2084 MALLOC(dp2, struct nfsdmap *, sizeof (struct nfsdmap), 2085 M_NFSDIROFF, M_WAITOK); 2086 dp2->ndm_eocookie = 0; 2087 LIST_INSERT_AFTER(dp, dp2, ndm_list); 2088 dp = dp2; 2089 } else 2090 return ((nfsuint64 *)0); 2091 } 2092 if (pos >= dp->ndm_eocookie) { 2093 if (add) 2094 dp->ndm_eocookie = pos + 1; 2095 else 2096 return ((nfsuint64 *)0); 2097 } 2098 return (&dp->ndm_cookies[pos]); 2099} 2100 2101/* 2102 * Invalidate cached directory information, except for the actual directory 2103 * blocks (which are invalidated separately). 2104 * Done mainly to avoid the use of stale offset cookies. 2105 */ 2106void 2107nfs_invaldir(vp) 2108 register struct vnode *vp; 2109{ 2110 register struct nfsnode *np = VTONFS(vp); 2111 2112#ifdef DIAGNOSTIC 2113 if (vp->v_type != VDIR) 2114 panic("nfs: invaldir not dir"); 2115#endif 2116 np->n_direofoffset = 0; 2117 np->n_cookieverf.nfsuquad[0] = 0; 2118 np->n_cookieverf.nfsuquad[1] = 0; 2119 if (np->n_cookies.lh_first) 2120 np->n_cookies.lh_first->ndm_eocookie = 0; 2121} 2122 2123/* 2124 * The write verifier has changed (probably due to a server reboot), so all 2125 * B_NEEDCOMMIT blocks will have to be written again. Since they are on the 2126 * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT 2127 * and B_CLUSTEROK flags. Once done the new write verifier can be set for the 2128 * mount point. 2129 * 2130 * B_CLUSTEROK must be cleared along with B_NEEDCOMMIT because stage 1 data 2131 * writes are not clusterable. 2132 */ 2133void 2134nfs_clearcommit(mp) 2135 struct mount *mp; 2136{ 2137 register struct vnode *vp, *nvp; 2138 register struct buf *bp, *nbp; 2139 int s; 2140 2141 GIANT_REQUIRED; 2142 2143 s = splbio(); 2144 mtx_lock(&mntvnode_mtx); 2145loop: 2146 for (vp = LIST_FIRST(&mp->mnt_vnodelist); vp; vp = nvp) { 2147 if (vp->v_mount != mp) /* Paranoia */ 2148 goto loop; 2149 nvp = LIST_NEXT(vp, v_mntvnodes); 2150 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) { 2151 nbp = TAILQ_NEXT(bp, b_vnbufs); 2152 if (BUF_REFCNT(bp) == 0 && 2153 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) 2154 == (B_DELWRI | B_NEEDCOMMIT)) 2155 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK); 2156 } 2157 } 2158 mtx_unlock(&mntvnode_mtx); 2159 splx(s); 2160} 2161 2162#ifndef NFS_NOSERVER 2163/* 2164 * Map errnos to NFS error numbers. For Version 3 also filter out error 2165 * numbers not specified for the associated procedure. 2166 */ 2167int 2168nfsrv_errmap(nd, err) 2169 struct nfsrv_descript *nd; 2170 register int err; 2171{ 2172 register short *defaulterrp, *errp; 2173 2174 if (nd->nd_flag & ND_NFSV3) { 2175 if (nd->nd_procnum <= NFSPROC_COMMIT) { 2176 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum]; 2177 while (*++errp) { 2178 if (*errp == err) 2179 return (err); 2180 else if (*errp > err) 2181 break; 2182 } 2183 return ((int)*defaulterrp); 2184 } else 2185 return (err & 0xffff); 2186 } 2187 if (err <= ELAST) 2188 return ((int)nfsrv_v2errmap[err - 1]); 2189 return (NFSERR_IO); 2190} 2191 2192int 2193nfsrv_object_create(vp) 2194 struct vnode *vp; 2195{ 2196 2197 if (vp == NULL || vp->v_type != VREG) 2198 return (1); 2199 return (vfs_object_create(vp, curthread, 2200 curthread ? curthread->td_proc->p_ucred : NULL)); 2201} 2202 2203/* 2204 * Sort the group list in increasing numerical order. 2205 * (Insertion sort by Chris Torek, who was grossed out by the bubble sort 2206 * that used to be here.) 2207 */ 2208void 2209nfsrvw_sort(list, num) 2210 register gid_t *list; 2211 register int num; 2212{ 2213 register int i, j; 2214 gid_t v; 2215 2216 /* Insertion sort. */ 2217 for (i = 1; i < num; i++) { 2218 v = list[i]; 2219 /* find correct slot for value v, moving others up */ 2220 for (j = i; --j >= 0 && v < list[j];) 2221 list[j + 1] = list[j]; 2222 list[j + 1] = v; 2223 } 2224} 2225 2226/* 2227 * copy credentials making sure that the result can be compared with bcmp(). 2228 */ 2229void 2230nfsrv_setcred(incred, outcred) 2231 register struct ucred *incred, *outcred; 2232{ 2233 register int i; 2234 2235 bzero((caddr_t)outcred, sizeof (struct ucred)); 2236 outcred->cr_ref = 1; 2237 outcred->cr_uid = incred->cr_uid; 2238 outcred->cr_ngroups = incred->cr_ngroups; 2239 for (i = 0; i < incred->cr_ngroups; i++) 2240 outcred->cr_groups[i] = incred->cr_groups[i]; 2241 nfsrvw_sort(outcred->cr_groups, outcred->cr_ngroups); 2242} 2243#endif /* NFS_NOSERVER */ 2244