nfs_subs.c revision 139823
1139823Simp/*- 21541Srgrimes * Copyright (c) 1989, 1993 31541Srgrimes * The Regents of the University of California. All rights reserved. 41541Srgrimes * 51541Srgrimes * This code is derived from software contributed to Berkeley by 61541Srgrimes * Rick Macklem at The University of Guelph. 71541Srgrimes * 81541Srgrimes * Redistribution and use in source and binary forms, with or without 91541Srgrimes * modification, are permitted provided that the following conditions 101541Srgrimes * are met: 111541Srgrimes * 1. Redistributions of source code must retain the above copyright 121541Srgrimes * notice, this list of conditions and the following disclaimer. 131541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141541Srgrimes * notice, this list of conditions and the following disclaimer in the 151541Srgrimes * documentation and/or other materials provided with the distribution. 161541Srgrimes * 4. Neither the name of the University nor the names of its contributors 171541Srgrimes * may be used to endorse or promote products derived from this software 181541Srgrimes * without specific prior written permission. 191541Srgrimes * 201541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 211541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 221541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 231541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 241541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 251541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 261541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 271541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 281541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 291541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 301541Srgrimes * SUCH DAMAGE. 311541Srgrimes * 3236503Speter * @(#)nfs_subs.c 8.8 (Berkeley) 5/22/95 331541Srgrimes */ 341541Srgrimes 3583651Speter#include <sys/cdefs.h> 3683651Speter__FBSDID("$FreeBSD: head/sys/nfsclient/nfs_subs.c 139823 2005-01-07 01:45:51Z imp $"); 3783651Speter 381541Srgrimes/* 391541Srgrimes * These functions support the macros and help fiddle mbuf chains for 401541Srgrimes * the nfs op functions. They do things like create the rpc header and 411541Srgrimes * copy data between mbuf chains and uio lists. 421541Srgrimes */ 4383651Speter 441541Srgrimes#include <sys/param.h> 4548274Speter#include <sys/systm.h> 4648274Speter#include <sys/kernel.h> 4760041Sphk#include <sys/bio.h> 4831886Sbde#include <sys/buf.h> 491541Srgrimes#include <sys/proc.h> 501541Srgrimes#include <sys/mount.h> 511541Srgrimes#include <sys/vnode.h> 521541Srgrimes#include <sys/namei.h> 531541Srgrimes#include <sys/mbuf.h> 541541Srgrimes#include <sys/socket.h> 551541Srgrimes#include <sys/stat.h> 569336Sdfr#include <sys/malloc.h> 572997Swollman#include <sys/sysent.h> 582997Swollman#include <sys/syscall.h> 5983651Speter#include <sys/sysproto.h> 601541Srgrimes 613305Sphk#include <vm/vm.h> 6212662Sdg#include <vm/vm_object.h> 6312662Sdg#include <vm/vm_extern.h> 6492783Sjeff#include <vm/uma.h> 653305Sphk 66122698Salfred#include <rpc/rpcclnt.h> 67122698Salfred 681541Srgrimes#include <nfs/rpcv2.h> 699336Sdfr#include <nfs/nfsproto.h> 7083651Speter#include <nfsclient/nfs.h> 7183651Speter#include <nfsclient/nfsnode.h> 721541Srgrimes#include <nfs/xdr_subs.h> 7383651Speter#include <nfsclient/nfsm_subs.h> 7483651Speter#include <nfsclient/nfsmount.h> 751541Srgrimes 761541Srgrimes#include <netinet/in.h> 771541Srgrimes 781541Srgrimes/* 791541Srgrimes * Data items converted to xdr at startup, since they are constant 801541Srgrimes * This is kinda hokey, but may save a little time doing byte swaps 811541Srgrimes */ 8283651Speteru_int32_t nfs_xdrneg1; 8383651Speteru_int32_t rpc_call, rpc_vers, rpc_reply, rpc_msgdenied, rpc_autherr, 8483651Speter rpc_mismatch, rpc_auth_unix, rpc_msgaccepted; 8583651Speteru_int32_t nfs_true, nfs_false; 861541Srgrimes 871541Srgrimes/* And other global data */ 8836541Speterstatic u_int32_t nfs_xid = 0; 8912911Sphkstatic enum vtype nv2tov_type[8]= { 9083651Speter VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON, VNON 9112911Sphk}; 9212911Sphk 9383651Speterint nfs_ticks; 9483651Speterint nfs_pbuf_freecnt = -1; /* start out unlimited */ 959336Sdfr 9683651Speterstruct nfs_reqq nfs_reqq; 97138496Spsstruct mtx nfs_reqq_mtx; 98138496Spsstruct mtx nfs_reply_mtx; 9983651Speterstruct nfs_bufq nfs_bufq; 1009759Sbde 1019336Sdfr/* 1029336Sdfr * and the reverse mapping from generic to Version 2 procedure numbers 1039336Sdfr */ 1049336Sdfrint nfsv2_procid[NFS_NPROCS] = { 1059336Sdfr NFSV2PROC_NULL, 1069336Sdfr NFSV2PROC_GETATTR, 1079336Sdfr NFSV2PROC_SETATTR, 1089336Sdfr NFSV2PROC_LOOKUP, 1099336Sdfr NFSV2PROC_NOOP, 1109336Sdfr NFSV2PROC_READLINK, 1119336Sdfr NFSV2PROC_READ, 1129336Sdfr NFSV2PROC_WRITE, 1139336Sdfr NFSV2PROC_CREATE, 1149336Sdfr NFSV2PROC_MKDIR, 1159336Sdfr NFSV2PROC_SYMLINK, 1169336Sdfr NFSV2PROC_CREATE, 1179336Sdfr NFSV2PROC_REMOVE, 1189336Sdfr NFSV2PROC_RMDIR, 1199336Sdfr NFSV2PROC_RENAME, 1209336Sdfr NFSV2PROC_LINK, 1219336Sdfr NFSV2PROC_READDIR, 1229336Sdfr NFSV2PROC_NOOP, 1239336Sdfr NFSV2PROC_STATFS, 1249336Sdfr NFSV2PROC_NOOP, 1259336Sdfr NFSV2PROC_NOOP, 1269336Sdfr NFSV2PROC_NOOP, 1279336Sdfr NFSV2PROC_NOOP, 1289336Sdfr}; 1299336Sdfr 13060938SjakeLIST_HEAD(nfsnodehashhead, nfsnode); 1313664Sphk 1321541Srgrimes/* 1331541Srgrimes * Create the header for an rpc request packet 1341541Srgrimes * The hsiz is the size of the rest of the nfs request header. 1351541Srgrimes * (just used to decide if a cluster is a good idea) 1361541Srgrimes */ 1371541Srgrimesstruct mbuf * 13883651Speternfsm_reqhead(struct vnode *vp, u_long procid, int hsiz) 1391541Srgrimes{ 14083651Speter struct mbuf *mb; 1411541Srgrimes 142111119Simp MGET(mb, M_TRYWAIT, MT_DATA); 1431541Srgrimes if (hsiz >= MINCLSIZE) 144111119Simp MCLGET(mb, M_TRYWAIT); 1451541Srgrimes mb->m_len = 0; 1461541Srgrimes return (mb); 1471541Srgrimes} 1481541Srgrimes 1491541Srgrimes/* 1501541Srgrimes * Build the RPC header and fill in the authorization info. 1511541Srgrimes * The authorization string argument is only used when the credentials 1521541Srgrimes * come from outside of the kernel. 1531541Srgrimes * Returns the head of the mbuf list. 1541541Srgrimes */ 1551541Srgrimesstruct mbuf * 15683651Speternfsm_rpchead(struct ucred *cr, int nmflag, int procid, int auth_type, 15783651Speter int auth_len, struct mbuf *mrest, int mrest_len, struct mbuf **mbp, 15883651Speter u_int32_t *xidp) 1591541Srgrimes{ 16083651Speter struct mbuf *mb; 16183651Speter u_int32_t *tl; 16283651Speter caddr_t bpos; 16383651Speter int i; 16483651Speter struct mbuf *mreq; 16583651Speter int grpsiz, authsiz; 1661541Srgrimes 1671541Srgrimes authsiz = nfsm_rndup(auth_len); 168111119Simp MGETHDR(mb, M_TRYWAIT, MT_DATA); 1699336Sdfr if ((authsiz + 10 * NFSX_UNSIGNED) >= MINCLSIZE) { 170111119Simp MCLGET(mb, M_TRYWAIT); 1719336Sdfr } else if ((authsiz + 10 * NFSX_UNSIGNED) < MHLEN) { 1729336Sdfr MH_ALIGN(mb, authsiz + 10 * NFSX_UNSIGNED); 1731541Srgrimes } else { 1749336Sdfr MH_ALIGN(mb, 8 * NFSX_UNSIGNED); 1751541Srgrimes } 1761541Srgrimes mb->m_len = 0; 1771541Srgrimes mreq = mb; 1781541Srgrimes bpos = mtod(mb, caddr_t); 1791541Srgrimes 1801541Srgrimes /* 1811541Srgrimes * First the RPC header. 1821541Srgrimes */ 18384002Speter tl = nfsm_build(u_int32_t *, 8 * NFSX_UNSIGNED); 18417186Sdfr 18535066Sphk /* Get a pretty random xid to start with */ 18683651Speter if (!nfs_xid) 18735066Sphk nfs_xid = random(); 18817186Sdfr /* 18917186Sdfr * Skip zero xid if it should ever happen. 19017186Sdfr */ 1911541Srgrimes if (++nfs_xid == 0) 1921541Srgrimes nfs_xid++; 19317186Sdfr 1941541Srgrimes *tl++ = *xidp = txdr_unsigned(nfs_xid); 1951541Srgrimes *tl++ = rpc_call; 1961541Srgrimes *tl++ = rpc_vers; 19783651Speter *tl++ = txdr_unsigned(NFS_PROG); 19883651Speter if (nmflag & NFSMNT_NFSV3) { 19983651Speter *tl++ = txdr_unsigned(NFS_VER3); 20083651Speter *tl++ = txdr_unsigned(procid); 2011541Srgrimes } else { 20283651Speter *tl++ = txdr_unsigned(NFS_VER2); 20383651Speter *tl++ = txdr_unsigned(nfsv2_procid[procid]); 2041541Srgrimes } 2051541Srgrimes 2061541Srgrimes /* 2071541Srgrimes * And then the authorization cred. 2081541Srgrimes */ 2091541Srgrimes *tl++ = txdr_unsigned(auth_type); 2101541Srgrimes *tl = txdr_unsigned(authsiz); 2111541Srgrimes switch (auth_type) { 2121541Srgrimes case RPCAUTH_UNIX: 21384002Speter tl = nfsm_build(u_int32_t *, auth_len); 2141541Srgrimes *tl++ = 0; /* stamp ?? */ 2151541Srgrimes *tl++ = 0; /* NULL hostname */ 2161541Srgrimes *tl++ = txdr_unsigned(cr->cr_uid); 2171541Srgrimes *tl++ = txdr_unsigned(cr->cr_groups[0]); 2181541Srgrimes grpsiz = (auth_len >> 2) - 5; 2191541Srgrimes *tl++ = txdr_unsigned(grpsiz); 2201541Srgrimes for (i = 1; i <= grpsiz; i++) 2211541Srgrimes *tl++ = txdr_unsigned(cr->cr_groups[i]); 2221541Srgrimes break; 22383651Speter } 2249336Sdfr 2259336Sdfr /* 2269336Sdfr * And the verifier... 2279336Sdfr */ 22884002Speter tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED); 22983651Speter *tl++ = txdr_unsigned(RPCAUTH_NULL); 23083651Speter *tl = 0; 2311541Srgrimes mb->m_next = mrest; 2329336Sdfr mreq->m_pkthdr.len = authsiz + 10 * NFSX_UNSIGNED + mrest_len; 23399797Sdillon mreq->m_pkthdr.rcvif = NULL; 2341541Srgrimes *mbp = mb; 2351541Srgrimes return (mreq); 2361541Srgrimes} 2371541Srgrimes 2381541Srgrimes/* 23917186Sdfr * copies a uio scatter/gather list to an mbuf chain. 24017186Sdfr * NOTE: can ony handle iovcnt == 1 2411541Srgrimes */ 2421549Srgrimesint 24383651Speternfsm_uiotombuf(struct uio *uiop, struct mbuf **mq, int siz, caddr_t *bpos) 2441541Srgrimes{ 24583651Speter char *uiocp; 24683651Speter struct mbuf *mp, *mp2; 24783651Speter int xfer, left, mlen; 2481541Srgrimes int uiosiz, clflg, rem; 2491541Srgrimes char *cp; 2501541Srgrimes 25136519Speter#ifdef DIAGNOSTIC 25217186Sdfr if (uiop->uio_iovcnt != 1) 25317186Sdfr panic("nfsm_uiotombuf: iovcnt != 1"); 25436519Speter#endif 25517186Sdfr 2561541Srgrimes if (siz > MLEN) /* or should it >= MCLBYTES ?? */ 2571541Srgrimes clflg = 1; 2581541Srgrimes else 2591541Srgrimes clflg = 0; 2601541Srgrimes rem = nfsm_rndup(siz)-siz; 2611541Srgrimes mp = mp2 = *mq; 2621541Srgrimes while (siz > 0) { 2631541Srgrimes left = uiop->uio_iov->iov_len; 2641541Srgrimes uiocp = uiop->uio_iov->iov_base; 2651541Srgrimes if (left > siz) 2661541Srgrimes left = siz; 2671541Srgrimes uiosiz = left; 2681541Srgrimes while (left > 0) { 2691541Srgrimes mlen = M_TRAILINGSPACE(mp); 2701541Srgrimes if (mlen == 0) { 271111119Simp MGET(mp, M_TRYWAIT, MT_DATA); 2721541Srgrimes if (clflg) 273111119Simp MCLGET(mp, M_TRYWAIT); 2741541Srgrimes mp->m_len = 0; 2751541Srgrimes mp2->m_next = mp; 2761541Srgrimes mp2 = mp; 2771541Srgrimes mlen = M_TRAILINGSPACE(mp); 2781541Srgrimes } 2791541Srgrimes xfer = (left > mlen) ? mlen : left; 2801541Srgrimes#ifdef notdef 2811541Srgrimes /* Not Yet.. */ 2821541Srgrimes if (uiop->uio_iov->iov_op != NULL) 2831541Srgrimes (*(uiop->uio_iov->iov_op)) 2841541Srgrimes (uiocp, mtod(mp, caddr_t)+mp->m_len, xfer); 2851541Srgrimes else 2861541Srgrimes#endif 2871541Srgrimes if (uiop->uio_segflg == UIO_SYSSPACE) 2881541Srgrimes bcopy(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer); 2891541Srgrimes else 2901541Srgrimes copyin(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer); 2911541Srgrimes mp->m_len += xfer; 2921541Srgrimes left -= xfer; 2931541Srgrimes uiocp += xfer; 2941541Srgrimes uiop->uio_offset += xfer; 2951541Srgrimes uiop->uio_resid -= xfer; 2961541Srgrimes } 297104908Smike uiop->uio_iov->iov_base = 298104908Smike (char *)uiop->uio_iov->iov_base + uiosiz; 29917186Sdfr uiop->uio_iov->iov_len -= uiosiz; 3001541Srgrimes siz -= uiosiz; 3011541Srgrimes } 3021541Srgrimes if (rem > 0) { 3031541Srgrimes if (rem > M_TRAILINGSPACE(mp)) { 304111119Simp MGET(mp, M_TRYWAIT, MT_DATA); 3051541Srgrimes mp->m_len = 0; 3061541Srgrimes mp2->m_next = mp; 3071541Srgrimes } 3081541Srgrimes cp = mtod(mp, caddr_t)+mp->m_len; 3091541Srgrimes for (left = 0; left < rem; left++) 3101541Srgrimes *cp++ = '\0'; 3111541Srgrimes mp->m_len += rem; 3121541Srgrimes *bpos = cp; 3131541Srgrimes } else 3141541Srgrimes *bpos = mtod(mp, caddr_t)+mp->m_len; 3151541Srgrimes *mq = mp; 3161541Srgrimes return (0); 3171541Srgrimes} 3181541Srgrimes 3191541Srgrimes/* 3201541Srgrimes * Copy a string into mbufs for the hard cases... 3211541Srgrimes */ 3221549Srgrimesint 32383651Speternfsm_strtmbuf(struct mbuf **mb, char **bpos, const char *cp, long siz) 3241541Srgrimes{ 32583651Speter struct mbuf *m1 = NULL, *m2; 3261541Srgrimes long left, xfer, len, tlen; 32736541Speter u_int32_t *tl; 3281541Srgrimes int putsize; 3291541Srgrimes 3301541Srgrimes putsize = 1; 3311541Srgrimes m2 = *mb; 3321541Srgrimes left = M_TRAILINGSPACE(m2); 3331541Srgrimes if (left > 0) { 33436541Speter tl = ((u_int32_t *)(*bpos)); 3351541Srgrimes *tl++ = txdr_unsigned(siz); 3361541Srgrimes putsize = 0; 3371541Srgrimes left -= NFSX_UNSIGNED; 3381541Srgrimes m2->m_len += NFSX_UNSIGNED; 3391541Srgrimes if (left > 0) { 3401541Srgrimes bcopy(cp, (caddr_t) tl, left); 3411541Srgrimes siz -= left; 3421541Srgrimes cp += left; 3431541Srgrimes m2->m_len += left; 3441541Srgrimes left = 0; 3451541Srgrimes } 3461541Srgrimes } 3471541Srgrimes /* Loop around adding mbufs */ 3481541Srgrimes while (siz > 0) { 349111119Simp MGET(m1, M_TRYWAIT, MT_DATA); 3501541Srgrimes if (siz > MLEN) 351111119Simp MCLGET(m1, M_TRYWAIT); 3521541Srgrimes m1->m_len = NFSMSIZ(m1); 3531541Srgrimes m2->m_next = m1; 3541541Srgrimes m2 = m1; 35536541Speter tl = mtod(m1, u_int32_t *); 3561541Srgrimes tlen = 0; 3571541Srgrimes if (putsize) { 3581541Srgrimes *tl++ = txdr_unsigned(siz); 3591541Srgrimes m1->m_len -= NFSX_UNSIGNED; 3601541Srgrimes tlen = NFSX_UNSIGNED; 3611541Srgrimes putsize = 0; 3621541Srgrimes } 3631541Srgrimes if (siz < m1->m_len) { 3641541Srgrimes len = nfsm_rndup(siz); 3651541Srgrimes xfer = siz; 3661541Srgrimes if (xfer < len) 3671541Srgrimes *(tl+(xfer>>2)) = 0; 3681541Srgrimes } else { 3691541Srgrimes xfer = len = m1->m_len; 3701541Srgrimes } 3711541Srgrimes bcopy(cp, (caddr_t) tl, xfer); 3721541Srgrimes m1->m_len = len+tlen; 3731541Srgrimes siz -= xfer; 3741541Srgrimes cp += xfer; 3751541Srgrimes } 3761541Srgrimes *mb = m1; 3771541Srgrimes *bpos = mtod(m1, caddr_t)+m1->m_len; 3781541Srgrimes return (0); 3791541Srgrimes} 3801541Srgrimes 3811541Srgrimes/* 3821541Srgrimes * Called once to initialize data structures... 3831541Srgrimes */ 3841549Srgrimesint 38583651Speternfs_init(struct vfsconf *vfsp) 3861541Srgrimes{ 38783651Speter int i; 3881541Srgrimes 38992783Sjeff nfsmount_zone = uma_zcreate("NFSMOUNT", sizeof(struct nfsmount), 39092783Sjeff NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 3911541Srgrimes rpc_vers = txdr_unsigned(RPC_VER2); 3921541Srgrimes rpc_call = txdr_unsigned(RPC_CALL); 3931541Srgrimes rpc_reply = txdr_unsigned(RPC_REPLY); 3941541Srgrimes rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED); 3951541Srgrimes rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED); 3961541Srgrimes rpc_mismatch = txdr_unsigned(RPC_MISMATCH); 3971541Srgrimes rpc_autherr = txdr_unsigned(RPC_AUTHERR); 3981541Srgrimes rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX); 3991541Srgrimes nfs_true = txdr_unsigned(TRUE); 4001541Srgrimes nfs_false = txdr_unsigned(FALSE); 4013664Sphk nfs_xdrneg1 = txdr_unsigned(-1); 4029336Sdfr nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000; 4039336Sdfr if (nfs_ticks < 1) 4049336Sdfr nfs_ticks = 1; 4051541Srgrimes /* Ensure async daemons disabled */ 40619449Sdfr for (i = 0; i < NFS_MAXASYNCDAEMON; i++) { 40799797Sdillon nfs_iodwant[i] = NULL; 40899797Sdillon nfs_iodmount[i] = NULL; 40919449Sdfr } 4101541Srgrimes nfs_nhinit(); /* Init the nfsnode table */ 4111541Srgrimes 4121541Srgrimes /* 4131541Srgrimes * Initialize reply list and start timer 4141541Srgrimes */ 4153664Sphk TAILQ_INIT(&nfs_reqq); 416127421Srees callout_init(&nfs_callout, 0); 417138496Sps mtx_init(&nfs_reqq_mtx, "NFS reqq lock", NULL, MTX_DEF); 418138496Sps mtx_init(&nfs_reply_mtx, "Synch NFS reply posting", NULL, MTX_DEF); 41916365Sphk 42042957Sdillon nfs_pbuf_freecnt = nswbuf / 2 + 1; 42142957Sdillon 4221549Srgrimes return (0); 4231541Srgrimes} 4241541Srgrimes 42538894Sbdeint 42683651Speternfs_uninit(struct vfsconf *vfsp) 42738894Sbde{ 428128111Speadar int i; 42938894Sbde 430127421Srees callout_stop(&nfs_callout); 431128111Speadar 432128126Smarcel KASSERT(TAILQ_EMPTY(&nfs_reqq), 433128111Speadar ("nfs_uninit: request queue not empty")); 434128111Speadar 435128111Speadar /* 436128111Speadar * Tell all nfsiod processes to exit. Clear nfs_iodmax, and wakeup 437128111Speadar * any sleeping nfsiods so they check nfs_iodmax and exit. 438128111Speadar */ 439128111Speadar nfs_iodmax = 0; 440128111Speadar for (i = 0; i < nfs_numasync; i++) 441128111Speadar if (nfs_iodwant[i]) 442128111Speadar wakeup(&nfs_iodwant[i]); 443128111Speadar /* The last nfsiod to exit will wake us up when nfs_numasync hits 0 */ 444128111Speadar while (nfs_numasync) 445128111Speadar tsleep(&nfs_numasync, PWAIT, "ioddie", 0); 446128111Speadar 447128111Speadar nfs_nhuninit(); 448128111Speadar uma_zdestroy(nfsmount_zone); 44938894Sbde return (0); 45038894Sbde} 45138894Sbde 4521541Srgrimes/* 4531541Srgrimes * Attribute cache routines. 4541541Srgrimes * nfs_loadattrcache() - loads or updates the cache contents from attributes 4551541Srgrimes * that are on the mbuf list 4561541Srgrimes * nfs_getattrcache() - returns valid attributes if found in cache, returns 4571541Srgrimes * error otherwise 4581541Srgrimes */ 4591541Srgrimes 4601541Srgrimes/* 4611541Srgrimes * Load the attribute cache (that lives in the nfsnode entry) with 4621541Srgrimes * the values on the mbuf list and 4631541Srgrimes * Iff vap not NULL 4641541Srgrimes * copy the attributes to *vaper 4651541Srgrimes */ 4661549Srgrimesint 46783651Speternfs_loadattrcache(struct vnode **vpp, struct mbuf **mdp, caddr_t *dposp, 46883651Speter struct vattr *vaper, int dontshrink) 4691541Srgrimes{ 47083651Speter struct vnode *vp = *vpp; 47183651Speter struct vattr *vap; 47283651Speter struct nfs_fattr *fp; 47383651Speter struct nfsnode *np; 47483651Speter int32_t t1; 4759336Sdfr caddr_t cp2; 47684057Speter int rdev; 4771541Srgrimes struct mbuf *md; 4781541Srgrimes enum vtype vtyp; 4791541Srgrimes u_short vmode; 4801541Srgrimes struct timespec mtime; 4819336Sdfr int v3 = NFS_ISV3(vp); 4821541Srgrimes 4831541Srgrimes md = *mdp; 4849336Sdfr t1 = (mtod(md, caddr_t) + md->m_len) - *dposp; 485138463Sps cp2 = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, M_TRYWAIT); 48684057Speter if (cp2 == NULL) 48784057Speter return EBADRPC; 4889336Sdfr fp = (struct nfs_fattr *)cp2; 4899336Sdfr if (v3) { 4909336Sdfr vtyp = nfsv3tov_type(fp->fa_type); 4919336Sdfr vmode = fxdr_unsigned(u_short, fp->fa_mode); 492130640Sphk rdev = makedev(fxdr_unsigned(int, fp->fa3_rdev.specdata1), 49316634Sbde fxdr_unsigned(int, fp->fa3_rdev.specdata2)); 4949336Sdfr fxdr_nfsv3time(&fp->fa3_mtime, &mtime); 4951541Srgrimes } else { 4969336Sdfr vtyp = nfsv2tov_type(fp->fa_type); 4979336Sdfr vmode = fxdr_unsigned(u_short, fp->fa_mode); 4989336Sdfr /* 4999336Sdfr * XXX 5009336Sdfr * 5019336Sdfr * The duplicate information returned in fa_type and fa_mode 5029336Sdfr * is an ambiguity in the NFS version 2 protocol. 5039336Sdfr * 5049336Sdfr * VREG should be taken literally as a regular file. If a 5059336Sdfr * server intents to return some type information differently 5069336Sdfr * in the upper bits of the mode field (e.g. for sockets, or 5079336Sdfr * FIFOs), NFSv2 mandates fa_type to be VNON. Anyway, we 5089336Sdfr * leave the examination of the mode bits even in the VREG 5099336Sdfr * case to avoid breakage for bogus servers, but we make sure 5109336Sdfr * that there are actually type bits set in the upper part of 5119336Sdfr * fa_mode (and failing that, trust the va_type field). 5129336Sdfr * 5139336Sdfr * NFSv3 cleared the issue, and requires fa_mode to not 5149336Sdfr * contain any type information (while also introduing sockets 5159336Sdfr * and FIFOs for fa_type). 5169336Sdfr */ 5179336Sdfr if (vtyp == VNON || (vtyp == VREG && (vmode & S_IFMT) != 0)) 5189336Sdfr vtyp = IFTOVT(vmode); 51936541Speter rdev = fxdr_unsigned(int32_t, fp->fa2_rdev); 5209336Sdfr fxdr_nfsv2time(&fp->fa2_mtime, &mtime); 5219336Sdfr 5229336Sdfr /* 5239336Sdfr * Really ugly NFSv2 kludge. 5249336Sdfr */ 5259336Sdfr if (vtyp == VCHR && rdev == 0xffffffff) 5269336Sdfr vtyp = VFIFO; 5271541Srgrimes } 5289336Sdfr 5291541Srgrimes /* 5301541Srgrimes * If v_type == VNON it is a new node, so fill in the v_type, 5318876Srgrimes * n_mtime fields. Check to see if it represents a special 5321541Srgrimes * device, and if so, check for a possible alias. Once the 5331541Srgrimes * correct vnode has been obtained, fill in the rest of the 5341541Srgrimes * information. 5351541Srgrimes */ 5361541Srgrimes np = VTONFS(vp); 53710219Sdfr if (vp->v_type != vtyp) { 5389336Sdfr vp->v_type = vtyp; 539126851Sphk if (vp->v_type == VFIFO) 540138290Sphk vp->v_op = &nfs_fifoops; 541138473Sps np->n_mtime = mtime; 5421541Srgrimes } 5431541Srgrimes vap = &np->n_vattr; 5441541Srgrimes vap->va_type = vtyp; 5451541Srgrimes vap->va_mode = (vmode & 07777); 54647028Sphk vap->va_rdev = rdev; 5471541Srgrimes vap->va_mtime = mtime; 5481541Srgrimes vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0]; 5499336Sdfr if (v3) { 5509336Sdfr vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink); 5519336Sdfr vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid); 5529336Sdfr vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid); 55347751Speter vap->va_size = fxdr_hyper(&fp->fa3_size); 5549336Sdfr vap->va_blocksize = NFS_FABLKSIZE; 55547751Speter vap->va_bytes = fxdr_hyper(&fp->fa3_used); 55636541Speter vap->va_fileid = fxdr_unsigned(int32_t, 55736541Speter fp->fa3_fileid.nfsuquad[1]); 5589336Sdfr fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime); 5599336Sdfr fxdr_nfsv3time(&fp->fa3_ctime, &vap->va_ctime); 5609336Sdfr vap->va_flags = 0; 5619336Sdfr vap->va_filerev = 0; 5621541Srgrimes } else { 5639336Sdfr vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink); 5649336Sdfr vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid); 5659336Sdfr vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid); 56636541Speter vap->va_size = fxdr_unsigned(u_int32_t, fp->fa2_size); 56736541Speter vap->va_blocksize = fxdr_unsigned(int32_t, fp->fa2_blocksize); 56847751Speter vap->va_bytes = (u_quad_t)fxdr_unsigned(int32_t, fp->fa2_blocks) 56936541Speter * NFS_FABLKSIZE; 57036541Speter vap->va_fileid = fxdr_unsigned(int32_t, fp->fa2_fileid); 5719336Sdfr fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime); 5721541Srgrimes vap->va_flags = 0; 57336541Speter vap->va_ctime.tv_sec = fxdr_unsigned(u_int32_t, 57436541Speter fp->fa2_ctime.nfsv2_sec); 57518397Snate vap->va_ctime.tv_nsec = 0; 57683651Speter vap->va_gen = fxdr_unsigned(u_int32_t, fp->fa2_ctime.nfsv2_usec); 5771541Srgrimes vap->va_filerev = 0; 5781541Srgrimes } 57967486Sdwmalone np->n_attrstamp = time_second; 5801541Srgrimes if (vap->va_size != np->n_size) { 5811541Srgrimes if (vap->va_type == VREG) { 58267486Sdwmalone if (dontshrink && vap->va_size < np->n_size) { 58367486Sdwmalone /* 58467486Sdwmalone * We've been told not to shrink the file; 58567486Sdwmalone * zero np->n_attrstamp to indicate that 58667486Sdwmalone * the attributes are stale. 58767486Sdwmalone */ 58867486Sdwmalone vap->va_size = np->n_size; 58967486Sdwmalone np->n_attrstamp = 0; 59067486Sdwmalone } else if (np->n_flag & NMODIFIED) { 591128263Speadar /* 592128263Speadar * We've modified the file: Use the larger 593128263Speadar * of our size, and the server's size. 594128263Speadar */ 595128263Speadar if (vap->va_size < np->n_size) { 5961541Srgrimes vap->va_size = np->n_size; 597128263Speadar } else { 5981541Srgrimes np->n_size = vap->va_size; 599128263Speadar np->n_flag |= NSIZECHANGED; 600128263Speadar } 60154480Sdillon } else { 6021541Srgrimes np->n_size = vap->va_size; 603128263Speadar np->n_flag |= NSIZECHANGED; 60454480Sdillon } 60541026Speter vnode_pager_setsize(vp, np->n_size); 60654480Sdillon } else { 6071541Srgrimes np->n_size = vap->va_size; 60854480Sdillon } 6091541Srgrimes } 6101541Srgrimes if (vaper != NULL) { 6111541Srgrimes bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap)); 6121541Srgrimes if (np->n_flag & NCHG) { 6139336Sdfr if (np->n_flag & NACC) 6149336Sdfr vaper->va_atime = np->n_atim; 6159336Sdfr if (np->n_flag & NUPD) 6169336Sdfr vaper->va_mtime = np->n_mtim; 6171541Srgrimes } 6181541Srgrimes } 6191541Srgrimes return (0); 6201541Srgrimes} 6211541Srgrimes 62236176Speter#ifdef NFS_ACDEBUG 62336176Speter#include <sys/sysctl.h> 62444101SbdeSYSCTL_DECL(_vfs_nfs); 62536176Speterstatic int nfs_acdebug; 62636176SpeterSYSCTL_INT(_vfs_nfs, OID_AUTO, acdebug, CTLFLAG_RW, &nfs_acdebug, 0, ""); 62736176Speter#endif 62836176Speter 6291541Srgrimes/* 6301541Srgrimes * Check the time stamp 6311541Srgrimes * If the cache is valid, copy contents to *vap and return 0 6321541Srgrimes * otherwise return an error 6331541Srgrimes */ 6341549Srgrimesint 63583651Speternfs_getattrcache(struct vnode *vp, struct vattr *vaper) 6361541Srgrimes{ 63783651Speter struct nfsnode *np; 63883651Speter struct vattr *vap; 63936176Speter struct nfsmount *nmp; 64036176Speter int timeo; 6411541Srgrimes 64236176Speter np = VTONFS(vp); 64336176Speter vap = &np->n_vattr; 64436176Speter nmp = VFSTONFS(vp->v_mount); 64536176Speter /* XXX n_mtime doesn't seem to be updated on a miss-and-reload */ 646138473Sps timeo = (time_second - np->n_mtime.tv_sec) / 10; 64736176Speter 64836176Speter#ifdef NFS_ACDEBUG 64936176Speter if (nfs_acdebug>1) 65036176Speter printf("nfs_getattrcache: initial timeo = %d\n", timeo); 65136176Speter#endif 65236176Speter 65336176Speter if (vap->va_type == VDIR) { 65436176Speter if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acdirmin) 65536176Speter timeo = nmp->nm_acdirmin; 65636176Speter else if (timeo > nmp->nm_acdirmax) 65736176Speter timeo = nmp->nm_acdirmax; 65836176Speter } else { 65936176Speter if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acregmin) 66036176Speter timeo = nmp->nm_acregmin; 66136176Speter else if (timeo > nmp->nm_acregmax) 66236176Speter timeo = nmp->nm_acregmax; 66336176Speter } 66436176Speter 66536176Speter#ifdef NFS_ACDEBUG 66636176Speter if (nfs_acdebug > 2) 66736176Speter printf("acregmin %d; acregmax %d; acdirmin %d; acdirmax %d\n", 66836176Speter nmp->nm_acregmin, nmp->nm_acregmax, 66936176Speter nmp->nm_acdirmin, nmp->nm_acdirmax); 67036176Speter 67136176Speter if (nfs_acdebug) 67237089Sbde printf("nfs_getattrcache: age = %d; final timeo = %d\n", 67336176Speter (time_second - np->n_attrstamp), timeo); 67436176Speter#endif 67536176Speter 67636176Speter if ((time_second - np->n_attrstamp) >= timeo) { 6771541Srgrimes nfsstats.attrcache_misses++; 6781541Srgrimes return (ENOENT); 6791541Srgrimes } 6801541Srgrimes nfsstats.attrcache_hits++; 6811541Srgrimes if (vap->va_size != np->n_size) { 6821541Srgrimes if (vap->va_type == VREG) { 6831541Srgrimes if (np->n_flag & NMODIFIED) { 6841541Srgrimes if (vap->va_size < np->n_size) 6851541Srgrimes vap->va_size = np->n_size; 6861541Srgrimes else 6871541Srgrimes np->n_size = vap->va_size; 68854480Sdillon } else { 6891541Srgrimes np->n_size = vap->va_size; 69054480Sdillon } 69141026Speter vnode_pager_setsize(vp, np->n_size); 69254480Sdillon } else { 6931541Srgrimes np->n_size = vap->va_size; 69454480Sdillon } 6951541Srgrimes } 6961541Srgrimes bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr)); 6971541Srgrimes if (np->n_flag & NCHG) { 6989336Sdfr if (np->n_flag & NACC) 6999336Sdfr vaper->va_atime = np->n_atim; 7009336Sdfr if (np->n_flag & NUPD) 7019336Sdfr vaper->va_mtime = np->n_mtim; 7021541Srgrimes } 7031541Srgrimes return (0); 7041541Srgrimes} 7051541Srgrimes 70643305Sdillonstatic nfsuint64 nfs_nullcookie = { { 0, 0 } }; 7079336Sdfr/* 7089336Sdfr * This function finds the directory cookie that corresponds to the 7099336Sdfr * logical byte offset given. 7109336Sdfr */ 7119336Sdfrnfsuint64 * 71283651Speternfs_getcookie(struct nfsnode *np, off_t off, int add) 7139336Sdfr{ 71483651Speter struct nfsdmap *dp, *dp2; 71583651Speter int pos; 7169336Sdfr 71736979Sbde pos = (uoff_t)off / NFS_DIRBLKSIZ; 71836979Sbde if (pos == 0 || off < 0) { 7199336Sdfr#ifdef DIAGNOSTIC 7209336Sdfr if (add) 72136979Sbde panic("nfs getcookie add at <= 0"); 7229336Sdfr#endif 7239336Sdfr return (&nfs_nullcookie); 7249336Sdfr } 7259336Sdfr pos--; 72683651Speter dp = LIST_FIRST(&np->n_cookies); 7279336Sdfr if (!dp) { 7289336Sdfr if (add) { 7299336Sdfr MALLOC(dp, struct nfsdmap *, sizeof (struct nfsdmap), 730111119Simp M_NFSDIROFF, M_WAITOK); 7319336Sdfr dp->ndm_eocookie = 0; 7329336Sdfr LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list); 7339336Sdfr } else 73499797Sdillon return (NULL); 7359336Sdfr } 7369336Sdfr while (pos >= NFSNUMCOOKIES) { 7379336Sdfr pos -= NFSNUMCOOKIES; 73883651Speter if (LIST_NEXT(dp, ndm_list)) { 7399336Sdfr if (!add && dp->ndm_eocookie < NFSNUMCOOKIES && 7409336Sdfr pos >= dp->ndm_eocookie) 74199797Sdillon return (NULL); 74283651Speter dp = LIST_NEXT(dp, ndm_list); 7439336Sdfr } else if (add) { 7449336Sdfr MALLOC(dp2, struct nfsdmap *, sizeof (struct nfsdmap), 745111119Simp M_NFSDIROFF, M_WAITOK); 7469336Sdfr dp2->ndm_eocookie = 0; 7479336Sdfr LIST_INSERT_AFTER(dp, dp2, ndm_list); 7489336Sdfr dp = dp2; 7499336Sdfr } else 75099797Sdillon return (NULL); 7519336Sdfr } 7529336Sdfr if (pos >= dp->ndm_eocookie) { 7539336Sdfr if (add) 7549336Sdfr dp->ndm_eocookie = pos + 1; 7559336Sdfr else 75699797Sdillon return (NULL); 7579336Sdfr } 7589336Sdfr return (&dp->ndm_cookies[pos]); 7599336Sdfr} 7609336Sdfr 7619336Sdfr/* 7629336Sdfr * Invalidate cached directory information, except for the actual directory 7639336Sdfr * blocks (which are invalidated separately). 7649336Sdfr * Done mainly to avoid the use of stale offset cookies. 7659336Sdfr */ 7669336Sdfrvoid 76783651Speternfs_invaldir(struct vnode *vp) 7689336Sdfr{ 76983651Speter struct nfsnode *np = VTONFS(vp); 7709336Sdfr 7719336Sdfr#ifdef DIAGNOSTIC 7729336Sdfr if (vp->v_type != VDIR) 7739336Sdfr panic("nfs: invaldir not dir"); 7749336Sdfr#endif 7759336Sdfr np->n_direofoffset = 0; 7769336Sdfr np->n_cookieverf.nfsuquad[0] = 0; 7779336Sdfr np->n_cookieverf.nfsuquad[1] = 0; 77883651Speter if (LIST_FIRST(&np->n_cookies)) 77983651Speter LIST_FIRST(&np->n_cookies)->ndm_eocookie = 0; 7809336Sdfr} 7819336Sdfr 7829336Sdfr/* 7839336Sdfr * The write verifier has changed (probably due to a server reboot), so all 7849336Sdfr * B_NEEDCOMMIT blocks will have to be written again. Since they are on the 7859336Sdfr * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT 78654480Sdillon * and B_CLUSTEROK flags. Once done the new write verifier can be set for the 78754480Sdillon * mount point. 78854480Sdillon * 78983651Speter * B_CLUSTEROK must be cleared along with B_NEEDCOMMIT because stage 1 data 79054480Sdillon * writes are not clusterable. 7919336Sdfr */ 7929336Sdfrvoid 79383651Speternfs_clearcommit(struct mount *mp) 7949336Sdfr{ 79583651Speter struct vnode *vp, *nvp; 79683651Speter struct buf *bp, *nbp; 7979336Sdfr int s; 7989336Sdfr 79979224Sdillon GIANT_REQUIRED; 80079224Sdillon 8019336Sdfr s = splbio(); 802122091Skan MNT_ILOCK(mp); 803131551Sphk MNT_VNODE_FOREACH(vp, mp, nvp) { 804103939Sjeff VI_LOCK(vp); 805120787Sjeff if (vp->v_iflag & VI_XLOCK) { 806120787Sjeff VI_UNLOCK(vp); 807120787Sjeff continue; 808120787Sjeff } 809122091Skan MNT_IUNLOCK(mp); 810136767Sphk TAILQ_FOREACH_SAFE(bp, &vp->v_bufobj.bo_dirty.bv_hd, b_bobufs, nbp) { 81148225Smckusick if (BUF_REFCNT(bp) == 0 && 81248225Smckusick (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) 8139336Sdfr == (B_DELWRI | B_NEEDCOMMIT)) 81454480Sdillon bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK); 8159336Sdfr } 816103939Sjeff VI_UNLOCK(vp); 817122091Skan MNT_ILOCK(mp); 8189336Sdfr } 819122091Skan MNT_IUNLOCK(mp); 8209336Sdfr splx(s); 8219336Sdfr} 8229336Sdfr 8239336Sdfr/* 82483651Speter * Helper functions for former macros. Some of these should be 82583651Speter * moved to their callers. 8269336Sdfr */ 82783651Speter 8285455Sdgint 82983651Speternfsm_mtofh_xx(struct vnode *d, struct vnode **v, int v3, int *f, 83088091Siedowse struct mbuf **md, caddr_t *dpos) 8319336Sdfr{ 83283651Speter struct nfsnode *ttnp; 83383651Speter struct vnode *ttvp; 83483651Speter nfsfh_t *ttfhp; 83588091Siedowse u_int32_t *tl; 83683651Speter int ttfhsize; 83783651Speter int t1; 8389336Sdfr 83983651Speter if (v3) { 84088091Siedowse tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos); 84188091Siedowse if (tl == NULL) 84284057Speter return EBADRPC; 84388091Siedowse *f = fxdr_unsigned(int, *tl); 84483651Speter } else 84583651Speter *f = 1; 84683651Speter if (*f) { 84788091Siedowse t1 = nfsm_getfh_xx(&ttfhp, &ttfhsize, (v3), md, dpos); 84883651Speter if (t1 != 0) 84983651Speter return t1; 85083651Speter t1 = nfs_nget(d->v_mount, ttfhp, ttfhsize, &ttnp); 85183651Speter if (t1 != 0) 85283651Speter return t1; 85383651Speter *v = NFSTOV(ttnp); 85483651Speter } 85583651Speter if (v3) { 85688091Siedowse tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos); 85788091Siedowse if (tl == NULL) 85884057Speter return EBADRPC; 85983651Speter if (*f) 86088091Siedowse *f = fxdr_unsigned(int, *tl); 86188091Siedowse else if (fxdr_unsigned(int, *tl)) 86288091Siedowse nfsm_adv_xx(NFSX_V3FATTR, md, dpos); 86383651Speter } 86483651Speter if (*f) { 86583651Speter ttvp = *v; 86699797Sdillon t1 = nfs_loadattrcache(&ttvp, md, dpos, NULL, 0); 86783651Speter if (t1) 86883651Speter return t1; 86983651Speter *v = ttvp; 87083651Speter } 87183651Speter return 0; 87283651Speter} 87383651Speter 87483651Speterint 87588091Siedowsenfsm_getfh_xx(nfsfh_t **f, int *s, int v3, struct mbuf **md, caddr_t *dpos) 87683651Speter{ 87788091Siedowse u_int32_t *tl; 87883651Speter 87983651Speter if (v3) { 88088091Siedowse tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos); 88188091Siedowse if (tl == NULL) 88284057Speter return EBADRPC; 88388091Siedowse *s = fxdr_unsigned(int, *tl); 88484057Speter if (*s <= 0 || *s > NFSX_V3FHMAX) 88583651Speter return EBADRPC; 88683651Speter } else 88783651Speter *s = NFSX_V2FH; 88884057Speter *f = nfsm_dissect_xx(nfsm_rndup(*s), md, dpos); 88984057Speter if (*f == NULL) 89084057Speter return EBADRPC; 89184057Speter else 89284057Speter return 0; 89383651Speter} 89483651Speter 89583651Speter 89683651Speterint 89788091Siedowsenfsm_loadattr_xx(struct vnode **v, struct vattr *va, struct mbuf **md, 89888091Siedowse caddr_t *dpos) 89983651Speter{ 90083651Speter int t1; 90183651Speter 90283651Speter struct vnode *ttvp = *v; 90383651Speter t1 = nfs_loadattrcache(&ttvp, md, dpos, va, 0); 90483651Speter if (t1 != 0) 90583651Speter return t1; 90683651Speter *v = ttvp; 90783651Speter return 0; 90883651Speter} 90983651Speter 91083651Speterint 91188091Siedowsenfsm_postop_attr_xx(struct vnode **v, int *f, struct mbuf **md, 91288091Siedowse caddr_t *dpos) 91383651Speter{ 91488091Siedowse u_int32_t *tl; 91583651Speter int t1; 91683651Speter 91783651Speter struct vnode *ttvp = *v; 91888091Siedowse tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos); 91988091Siedowse if (tl == NULL) 92084057Speter return EBADRPC; 92188091Siedowse *f = fxdr_unsigned(int, *tl); 92283914Siedowse if (*f != 0) { 92399797Sdillon t1 = nfs_loadattrcache(&ttvp, md, dpos, NULL, 1); 92483651Speter if (t1 != 0) { 92583651Speter *f = 0; 92683651Speter return t1; 92783651Speter } 92883651Speter *v = ttvp; 9299336Sdfr } 93083651Speter return 0; 9319336Sdfr} 9329336Sdfr 9339336Sdfrint 93488091Siedowsenfsm_wcc_data_xx(struct vnode **v, int *f, struct mbuf **md, caddr_t *dpos) 93531886Sbde{ 93688091Siedowse u_int32_t *tl; 93783651Speter int ttattrf, ttretf = 0; 93883651Speter int t1; 9395455Sdg 94088091Siedowse tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos); 94188091Siedowse if (tl == NULL) 94284057Speter return EBADRPC; 94388091Siedowse if (*tl == nfs_true) { 94488091Siedowse tl = nfsm_dissect_xx(6 * NFSX_UNSIGNED, md, dpos); 94588091Siedowse if (tl == NULL) 94684057Speter return EBADRPC; 94783651Speter if (*f) 948138473Sps ttretf = (VTONFS(*v)->n_mtime.tv_sec == fxdr_unsigned(u_int32_t, *(tl + 2)) && 949138473Sps VTONFS(*v)->n_mtime.tv_nsec == fxdr_unsigned(u_int32_t, *(tl + 3))); 95083651Speter } 95188091Siedowse t1 = nfsm_postop_attr_xx(v, &ttattrf, md, dpos); 95283651Speter if (t1) 95383651Speter return t1; 95483651Speter if (*f) 95583651Speter *f = ttretf; 95683651Speter else 95783651Speter *f = ttattrf; 95883651Speter return 0; 9595455Sdg} 96036503Speter 96183651Speterint 96288091Siedowsenfsm_strtom_xx(const char *a, int s, int m, struct mbuf **mb, caddr_t *bpos) 96336503Speter{ 96488091Siedowse u_int32_t *tl; 96583651Speter int t1; 96636503Speter 96783651Speter if (s > m) 96883651Speter return ENAMETOOLONG; 96983651Speter t1 = nfsm_rndup(s) + NFSX_UNSIGNED; 97083651Speter if (t1 <= M_TRAILINGSPACE(*mb)) { 97188091Siedowse tl = nfsm_build_xx(t1, mb, bpos); 97288091Siedowse *tl++ = txdr_unsigned(s); 97388091Siedowse *(tl + ((t1 >> 2) - 2)) = 0; 97488091Siedowse bcopy(a, tl, s); 97583651Speter } else { 97683651Speter t1 = nfsm_strtmbuf(mb, bpos, a, s); 97783651Speter if (t1 != 0) 97883651Speter return t1; 97936503Speter } 98083651Speter return 0; 98136503Speter} 98236503Speter 98383651Speterint 98488091Siedowsenfsm_fhtom_xx(struct vnode *v, int v3, struct mbuf **mb, caddr_t *bpos) 98583651Speter{ 98688091Siedowse u_int32_t *tl; 98783651Speter int t1; 98883651Speter caddr_t cp; 98983651Speter 99083651Speter if (v3) { 99183651Speter t1 = nfsm_rndup(VTONFS(v)->n_fhsize) + NFSX_UNSIGNED; 99283651Speter if (t1 < M_TRAILINGSPACE(*mb)) { 99388091Siedowse tl = nfsm_build_xx(t1, mb, bpos); 99488091Siedowse *tl++ = txdr_unsigned(VTONFS(v)->n_fhsize); 99588091Siedowse *(tl + ((t1 >> 2) - 2)) = 0; 99688091Siedowse bcopy(VTONFS(v)->n_fhp, tl, VTONFS(v)->n_fhsize); 99783651Speter } else { 99883651Speter t1 = nfsm_strtmbuf(mb, bpos, 99983651Speter (const char *)VTONFS(v)->n_fhp, 100083651Speter VTONFS(v)->n_fhsize); 100183651Speter if (t1 != 0) 100283651Speter return t1; 100383651Speter } 100483651Speter } else { 100584002Speter cp = nfsm_build_xx(NFSX_V2FH, mb, bpos); 100683651Speter bcopy(VTONFS(v)->n_fhp, cp, NFSX_V2FH); 100783651Speter } 100883651Speter return 0; 100983651Speter} 101083651Speter 101136503Spetervoid 101288091Siedowsenfsm_v3attrbuild_xx(struct vattr *va, int full, struct mbuf **mb, 101388091Siedowse caddr_t *bpos) 101436503Speter{ 101588091Siedowse u_int32_t *tl; 101636503Speter 101783651Speter if (va->va_mode != (mode_t)VNOVAL) { 101888091Siedowse tl = nfsm_build_xx(2 * NFSX_UNSIGNED, mb, bpos); 101988091Siedowse *tl++ = nfs_true; 102088091Siedowse *tl = txdr_unsigned(va->va_mode); 102183651Speter } else { 102288091Siedowse tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos); 102388091Siedowse *tl = nfs_false; 102483651Speter } 102583651Speter if (full && va->va_uid != (uid_t)VNOVAL) { 102688091Siedowse tl = nfsm_build_xx(2 * NFSX_UNSIGNED, mb, bpos); 102788091Siedowse *tl++ = nfs_true; 102888091Siedowse *tl = txdr_unsigned(va->va_uid); 102983651Speter } else { 103088091Siedowse tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos); 103188091Siedowse *tl = nfs_false; 103283651Speter } 103383651Speter if (full && va->va_gid != (gid_t)VNOVAL) { 103488091Siedowse tl = nfsm_build_xx(2 * NFSX_UNSIGNED, mb, bpos); 103588091Siedowse *tl++ = nfs_true; 103688091Siedowse *tl = txdr_unsigned(va->va_gid); 103783651Speter } else { 103888091Siedowse tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos); 103988091Siedowse *tl = nfs_false; 104083651Speter } 104183651Speter if (full && va->va_size != VNOVAL) { 104288091Siedowse tl = nfsm_build_xx(3 * NFSX_UNSIGNED, mb, bpos); 104388091Siedowse *tl++ = nfs_true; 104488091Siedowse txdr_hyper(va->va_size, tl); 104583651Speter } else { 104688091Siedowse tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos); 104788091Siedowse *tl = nfs_false; 104883651Speter } 104983651Speter if (va->va_atime.tv_sec != VNOVAL) { 105083651Speter if (va->va_atime.tv_sec != time_second) { 105188091Siedowse tl = nfsm_build_xx(3 * NFSX_UNSIGNED, mb, bpos); 105288091Siedowse *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT); 105388091Siedowse txdr_nfsv3time(&va->va_atime, tl); 105483651Speter } else { 105588091Siedowse tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos); 105688091Siedowse *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER); 105783651Speter } 105883651Speter } else { 105988091Siedowse tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos); 106088091Siedowse *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE); 106183651Speter } 106283651Speter if (va->va_mtime.tv_sec != VNOVAL) { 106383651Speter if (va->va_mtime.tv_sec != time_second) { 106488091Siedowse tl = nfsm_build_xx(3 * NFSX_UNSIGNED, mb, bpos); 106588091Siedowse *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT); 106688091Siedowse txdr_nfsv3time(&va->va_mtime, tl); 106783651Speter } else { 106888091Siedowse tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos); 106988091Siedowse *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER); 107083651Speter } 107183651Speter } else { 107288091Siedowse tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos); 107388091Siedowse *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE); 107483651Speter } 107536503Speter} 1076