19ec7b004SRick Macklem /*- 29ec7b004SRick Macklem * Copyright (c) 1989, 1993 39ec7b004SRick Macklem * The Regents of the University of California. All rights reserved. 49ec7b004SRick Macklem * 59ec7b004SRick Macklem * This code is derived from software contributed to Berkeley by 69ec7b004SRick Macklem * Rick Macklem at The University of Guelph. 79ec7b004SRick Macklem * 89ec7b004SRick Macklem * Redistribution and use in source and binary forms, with or without 99ec7b004SRick Macklem * modification, are permitted provided that the following conditions 109ec7b004SRick Macklem * are met: 119ec7b004SRick Macklem * 1. Redistributions of source code must retain the above copyright 129ec7b004SRick Macklem * notice, this list of conditions and the following disclaimer. 139ec7b004SRick Macklem * 2. Redistributions in binary form must reproduce the above copyright 149ec7b004SRick Macklem * notice, this list of conditions and the following disclaimer in the 159ec7b004SRick Macklem * documentation and/or other materials provided with the distribution. 169ec7b004SRick Macklem * 4. Neither the name of the University nor the names of its contributors 179ec7b004SRick Macklem * may be used to endorse or promote products derived from this software 189ec7b004SRick Macklem * without specific prior written permission. 199ec7b004SRick Macklem * 209ec7b004SRick Macklem * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 219ec7b004SRick Macklem * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 229ec7b004SRick Macklem * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 239ec7b004SRick Macklem * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 249ec7b004SRick Macklem * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 259ec7b004SRick Macklem * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 269ec7b004SRick Macklem * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 279ec7b004SRick Macklem * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 289ec7b004SRick Macklem * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 299ec7b004SRick Macklem * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 309ec7b004SRick Macklem * SUCH DAMAGE. 319ec7b004SRick Macklem * 329ec7b004SRick Macklem */ 339ec7b004SRick Macklem 349ec7b004SRick Macklem #include <sys/cdefs.h> 359ec7b004SRick Macklem __FBSDID("$FreeBSD$"); 369ec7b004SRick Macklem 379ec7b004SRick Macklem /* 389ec7b004SRick Macklem * Functions that need to be different for different versions of BSD 399ec7b004SRick Macklem * kernel should be kept here, along with any global storage specific 409ec7b004SRick Macklem * to this BSD variant. 419ec7b004SRick Macklem */ 429ec7b004SRick Macklem #include <fs/nfs/nfsport.h> 439ec7b004SRick Macklem #include <sys/sysctl.h> 449ec7b004SRick Macklem #include <vm/vm.h> 459ec7b004SRick Macklem #include <vm/vm_object.h> 469ec7b004SRick Macklem #include <vm/vm_page.h> 479ec7b004SRick Macklem #include <vm/vm_param.h> 489ec7b004SRick Macklem #include <vm/vm_map.h> 499ec7b004SRick Macklem #include <vm/vm_kern.h> 509ec7b004SRick Macklem #include <vm/vm_extern.h> 519ec7b004SRick Macklem #include <vm/uma.h> 529ec7b004SRick Macklem #include <vm/uma_int.h> 539ec7b004SRick Macklem 549ec7b004SRick Macklem extern int nfscl_ticks; 559ec7b004SRick Macklem extern int nfsrv_nfsuserd; 569ec7b004SRick Macklem extern struct nfssockreq nfsrv_nfsuserdsock; 579ec7b004SRick Macklem extern void (*nfsd_call_recall)(struct vnode *, int, struct ucred *, 589ec7b004SRick Macklem struct thread *); 599ec7b004SRick Macklem extern int nfsrv_useacl; 609ec7b004SRick Macklem struct mount nfsv4root_mnt; 619ec7b004SRick Macklem int newnfs_numnfsd = 0; 629ec7b004SRick Macklem struct nfsstats newnfsstats; 639ec7b004SRick Macklem int nfs_numnfscbd = 0; 649ec7b004SRick Macklem char nfsv4_callbackaddr[INET6_ADDRSTRLEN]; 659ec7b004SRick Macklem struct callout newnfsd_callout; 669ec7b004SRick Macklem void (*nfsd_call_servertimer)(void) = NULL; 679ec7b004SRick Macklem void (*ncl_call_invalcaches)(struct vnode *) = NULL; 689ec7b004SRick Macklem 699ec7b004SRick Macklem static int nfs_realign_test; 709ec7b004SRick Macklem static int nfs_realign_count; 719ec7b004SRick Macklem 729ec7b004SRick Macklem SYSCTL_NODE(_vfs, OID_AUTO, newnfs, CTLFLAG_RW, 0, "New NFS filesystem"); 7305c965a2SRick Macklem SYSCTL_INT(_vfs_newnfs, OID_AUTO, newnfs_realign_test, CTLFLAG_RW, &nfs_realign_test, 0, ""); 7405c965a2SRick Macklem SYSCTL_INT(_vfs_newnfs, OID_AUTO, newnfs_realign_count, CTLFLAG_RW, &nfs_realign_count, 0, ""); 759ec7b004SRick Macklem SYSCTL_INT(_vfs_newnfs, OID_AUTO, nfs4acl_enable, CTLFLAG_RW, &nfsrv_useacl, 0, ""); 769ec7b004SRick Macklem SYSCTL_STRING(_vfs_newnfs, OID_AUTO, callback_addr, CTLFLAG_RW, 779ec7b004SRick Macklem nfsv4_callbackaddr, sizeof(nfsv4_callbackaddr), ""); 789ec7b004SRick Macklem 799ec7b004SRick Macklem /* 809ec7b004SRick Macklem * Defines for malloc 819ec7b004SRick Macklem * (Here for FreeBSD, since they allocate storage.) 829ec7b004SRick Macklem */ 839ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSRVCACHE, "NFSD srvcache", "NFSD Server Request Cache"); 849ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSDCLIENT, "NFSD V4client", "NFSD V4 Client Id"); 85a43fcbe3SRick Macklem MALLOC_DEFINE(M_NEWNFSDSTATE, "NFSD V4state", 86a43fcbe3SRick Macklem "NFSD V4 State (Openowner, Open, Lockowner, Delegation"); 879ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSDLOCK, "NFSD V4lock", "NFSD V4 byte range lock"); 889ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSDLOCKFILE, "NFSD lckfile", "NFSD Open/Lock file"); 899ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSSTRING, "NFSD string", "NFSD V4 long string"); 909ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSUSERGROUP, "NFSD usrgroup", "NFSD V4 User/group map"); 919ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSDREQ, "NFS req", "NFS request header"); 929ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSFH, "NFS fh", "NFS file handle"); 939ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSCLOWNER, "NFSCL owner", "NFSCL Open Owner"); 949ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSCLOPEN, "NFSCL open", "NFSCL Open"); 959ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSCLDELEG, "NFSCL deleg", "NFSCL Delegation"); 969ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSCLCLIENT, "NFSCL client", "NFSCL Client"); 979ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSCLLOCKOWNER, "NFSCL lckown", "NFSCL Lock Owner"); 989ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSCLLOCK, "NFSCL lck", "NFSCL Lock"); 999ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSV4NODE, "NEWNFSnode", "New nfs vnode"); 1009ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSDIRECTIO, "NEWdirectio", "New nfs Direct IO buffer"); 101a43fcbe3SRick Macklem MALLOC_DEFINE(M_NEWNFSDIROFF, "NFSCL diroffdiroff", 102a43fcbe3SRick Macklem "New NFS directory offset data"); 103a43fcbe3SRick Macklem MALLOC_DEFINE(M_NEWNFSDROLLBACK, "NFSD rollback", 104a43fcbe3SRick Macklem "New NFS local lock rollback"); 1059ec7b004SRick Macklem 1069ec7b004SRick Macklem /* 1079ec7b004SRick Macklem * Definition of mutex locks. 1089ec7b004SRick Macklem * newnfsd_mtx is used in nfsrvd_nfsd() to protect the nfs socket list 1099ec7b004SRick Macklem * and assorted other nfsd structures. 1109ec7b004SRick Macklem * Giant is used to protect the nfsd list and count, which is just 1119ec7b004SRick Macklem * updated when nfsd's start/stop and is grabbed for nfsrvd_dorpc() 1129ec7b004SRick Macklem * for the VFS ops. 1139ec7b004SRick Macklem */ 1149ec7b004SRick Macklem struct mtx newnfsd_mtx; 1159ec7b004SRick Macklem struct mtx nfs_sockl_mutex; 1169ec7b004SRick Macklem struct mtx nfs_state_mutex; 1179ec7b004SRick Macklem struct mtx nfs_nameid_mutex; 1189ec7b004SRick Macklem struct mtx nfs_req_mutex; 1199ec7b004SRick Macklem struct mtx nfs_slock_mutex; 1209ec7b004SRick Macklem 1219ec7b004SRick Macklem /* local functions */ 1229ec7b004SRick Macklem static int nfssvc_call(struct thread *, struct nfssvc_args *, struct ucred *); 1239ec7b004SRick Macklem 1243dfe81c6SRick Macklem #ifdef __NO_STRICT_ALIGNMENT 1259ec7b004SRick Macklem /* 1269ec7b004SRick Macklem * These architectures don't need re-alignment, so just return. 1279ec7b004SRick Macklem */ 1289ec7b004SRick Macklem void 1299ec7b004SRick Macklem newnfs_realign(struct mbuf **pm) 1309ec7b004SRick Macklem { 1319ec7b004SRick Macklem 1329ec7b004SRick Macklem return; 1339ec7b004SRick Macklem } 1343dfe81c6SRick Macklem #else /* !__NO_STRICT_ALIGNMENT */ 1359ec7b004SRick Macklem /* 13605c965a2SRick Macklem * newnfs_realign: 1379ec7b004SRick Macklem * 1389ec7b004SRick Macklem * Check for badly aligned mbuf data and realign by copying the unaligned 1399ec7b004SRick Macklem * portion of the data into a new mbuf chain and freeing the portions 1409ec7b004SRick Macklem * of the old chain that were replaced. 1419ec7b004SRick Macklem * 1429ec7b004SRick Macklem * We cannot simply realign the data within the existing mbuf chain 1439ec7b004SRick Macklem * because the underlying buffers may contain other rpc commands and 1449ec7b004SRick Macklem * we cannot afford to overwrite them. 1459ec7b004SRick Macklem * 1469ec7b004SRick Macklem * We would prefer to avoid this situation entirely. The situation does 1479ec7b004SRick Macklem * not occur with NFS/UDP and is supposed to only occassionally occur 1489ec7b004SRick Macklem * with TCP. Use vfs.nfs.realign_count and realign_test to check this. 14905c965a2SRick Macklem * 1509ec7b004SRick Macklem */ 1519ec7b004SRick Macklem void 1529ec7b004SRick Macklem newnfs_realign(struct mbuf **pm) 1539ec7b004SRick Macklem { 15405c965a2SRick Macklem struct mbuf *m, *n; 15505c965a2SRick Macklem int off, space; 1569ec7b004SRick Macklem 1579ec7b004SRick Macklem ++nfs_realign_test; 1589ec7b004SRick Macklem while ((m = *pm) != NULL) { 1599ec7b004SRick Macklem if ((m->m_len & 0x3) || (mtod(m, intptr_t) & 0x3)) { 1609ec7b004SRick Macklem /* 16105c965a2SRick Macklem * NB: we can't depend on m_pkthdr.len to help us 16205c965a2SRick Macklem * decide what to do here. May not be worth doing 16305c965a2SRick Macklem * the m_length calculation as m_copyback will 16405c965a2SRick Macklem * expand the mbuf chain below as needed. 1659ec7b004SRick Macklem */ 16605c965a2SRick Macklem space = m_length(m, NULL); 16705c965a2SRick Macklem if (space >= MINCLSIZE) { 16805c965a2SRick Macklem /* NB: m_copyback handles space > MCLBYTES */ 16905c965a2SRick Macklem n = m_getcl(M_WAITOK, MT_DATA, 0); 17005c965a2SRick Macklem } else 17105c965a2SRick Macklem n = m_get(M_WAITOK, MT_DATA); 17205c965a2SRick Macklem if (n == NULL) 17305c965a2SRick Macklem return; 17405c965a2SRick Macklem /* 17505c965a2SRick Macklem * Align the remainder of the mbuf chain. 17605c965a2SRick Macklem */ 17705c965a2SRick Macklem n->m_len = 0; 17805c965a2SRick Macklem off = 0; 17905c965a2SRick Macklem while (m != NULL) { 1809ec7b004SRick Macklem m_copyback(n, off, m->m_len, mtod(m, caddr_t)); 1819ec7b004SRick Macklem off += m->m_len; 1829ec7b004SRick Macklem m = m->m_next; 1839ec7b004SRick Macklem } 1849ec7b004SRick Macklem m_freem(*pm); 1859ec7b004SRick Macklem *pm = n; 18605c965a2SRick Macklem ++nfs_realign_count; 18705c965a2SRick Macklem break; 18805c965a2SRick Macklem } 18905c965a2SRick Macklem pm = &m->m_next; 1909ec7b004SRick Macklem } 1919ec7b004SRick Macklem } 1923dfe81c6SRick Macklem #endif /* __NO_STRICT_ALIGNMENT */ 1939ec7b004SRick Macklem 1949ec7b004SRick Macklem #ifdef notdef 1959ec7b004SRick Macklem static void 1969ec7b004SRick Macklem nfsrv_object_create(struct vnode *vp, struct thread *td) 1979ec7b004SRick Macklem { 1989ec7b004SRick Macklem 1999ec7b004SRick Macklem if (vp == NULL || vp->v_type != VREG) 2009ec7b004SRick Macklem return; 2019ec7b004SRick Macklem (void) vfs_object_create(vp, td, td->td_ucred); 2029ec7b004SRick Macklem } 2039ec7b004SRick Macklem #endif 2049ec7b004SRick Macklem 2059ec7b004SRick Macklem /* 2069ec7b004SRick Macklem * Look up a file name. Basically just initialize stuff and call namei(). 2079ec7b004SRick Macklem */ 2089ec7b004SRick Macklem int 2099ec7b004SRick Macklem nfsrv_lookupfilename(struct nameidata *ndp, char *fname, NFSPROC_T *p) 2109ec7b004SRick Macklem { 2119ec7b004SRick Macklem int error; 2129ec7b004SRick Macklem 21366c0f45aSRick Macklem NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE, UIO_USERSPACE, fname, 21466c0f45aSRick Macklem p); 2159ec7b004SRick Macklem error = namei(ndp); 2169ec7b004SRick Macklem if (!error) { 2179ec7b004SRick Macklem NDFREE(ndp, NDF_ONLY_PNBUF); 2189ec7b004SRick Macklem } 2199ec7b004SRick Macklem return (error); 2209ec7b004SRick Macklem } 2219ec7b004SRick Macklem 2229ec7b004SRick Macklem /* 2239ec7b004SRick Macklem * Copy NFS uid, gids to the cred structure. 2249ec7b004SRick Macklem */ 2259ec7b004SRick Macklem void 2269ec7b004SRick Macklem newnfs_copycred(struct nfscred *nfscr, struct ucred *cr) 2279ec7b004SRick Macklem { 2289ec7b004SRick Macklem 22955909abfSRick Macklem KASSERT(nfscr->nfsc_ngroups >= 0, 23055909abfSRick Macklem ("newnfs_copycred: negative nfsc_ngroups")); 2319ec7b004SRick Macklem cr->cr_uid = nfscr->nfsc_uid; 232838d9858SBrooks Davis crsetgroups(cr, nfscr->nfsc_ngroups, nfscr->nfsc_groups); 2339ec7b004SRick Macklem } 2349ec7b004SRick Macklem 2359ec7b004SRick Macklem /* 2369ec7b004SRick Macklem * Map args from nfsmsleep() to msleep(). 2379ec7b004SRick Macklem */ 2389ec7b004SRick Macklem int 2399ec7b004SRick Macklem nfsmsleep(void *chan, void *mutex, int prio, const char *wmesg, 2409ec7b004SRick Macklem struct timespec *ts) 2419ec7b004SRick Macklem { 2429ec7b004SRick Macklem u_int64_t nsecval; 2439ec7b004SRick Macklem int error, timeo; 2449ec7b004SRick Macklem 2459ec7b004SRick Macklem if (ts) { 2469ec7b004SRick Macklem timeo = hz * ts->tv_sec; 2479ec7b004SRick Macklem nsecval = (u_int64_t)ts->tv_nsec; 2489ec7b004SRick Macklem nsecval = ((nsecval * ((u_int64_t)hz)) + 500000000) / 2499ec7b004SRick Macklem 1000000000; 2509ec7b004SRick Macklem timeo += (int)nsecval; 2519ec7b004SRick Macklem } else { 2529ec7b004SRick Macklem timeo = 0; 2539ec7b004SRick Macklem } 2549ec7b004SRick Macklem error = msleep(chan, (struct mtx *)mutex, prio, wmesg, timeo); 2559ec7b004SRick Macklem return (error); 2569ec7b004SRick Macklem } 2579ec7b004SRick Macklem 2589ec7b004SRick Macklem /* 2599ec7b004SRick Macklem * Get the file system info for the server. For now, just assume FFS. 2609ec7b004SRick Macklem */ 2619ec7b004SRick Macklem void 2629ec7b004SRick Macklem nfsvno_getfs(struct nfsfsinfo *sip, int isdgram) 2639ec7b004SRick Macklem { 2649ec7b004SRick Macklem int pref; 2659ec7b004SRick Macklem 2669ec7b004SRick Macklem /* 2679ec7b004SRick Macklem * XXX 2689ec7b004SRick Macklem * There should be file system VFS OP(s) to get this information. 2699ec7b004SRick Macklem * For now, assume ufs. 2709ec7b004SRick Macklem */ 2719ec7b004SRick Macklem if (isdgram) 2729ec7b004SRick Macklem pref = NFS_MAXDGRAMDATA; 2739ec7b004SRick Macklem else 2749ec7b004SRick Macklem pref = NFS_MAXDATA; 2759ec7b004SRick Macklem sip->fs_rtmax = NFS_MAXDATA; 2769ec7b004SRick Macklem sip->fs_rtpref = pref; 2779ec7b004SRick Macklem sip->fs_rtmult = NFS_FABLKSIZE; 2789ec7b004SRick Macklem sip->fs_wtmax = NFS_MAXDATA; 2799ec7b004SRick Macklem sip->fs_wtpref = pref; 2809ec7b004SRick Macklem sip->fs_wtmult = NFS_FABLKSIZE; 2819ec7b004SRick Macklem sip->fs_dtpref = pref; 2829ec7b004SRick Macklem sip->fs_maxfilesize = 0xffffffffffffffffull; 2839ec7b004SRick Macklem sip->fs_timedelta.tv_sec = 0; 2849ec7b004SRick Macklem sip->fs_timedelta.tv_nsec = 1; 2859ec7b004SRick Macklem sip->fs_properties = (NFSV3FSINFO_LINK | 2869ec7b004SRick Macklem NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS | 2879ec7b004SRick Macklem NFSV3FSINFO_CANSETTIME); 2889ec7b004SRick Macklem } 2899ec7b004SRick Macklem 2909ec7b004SRick Macklem /* Fake nfsrv_atroot. Just return 0 */ 2919ec7b004SRick Macklem int 2929ec7b004SRick Macklem nfsrv_atroot(struct vnode *vp, long *retp) 2939ec7b004SRick Macklem { 2949ec7b004SRick Macklem 2959ec7b004SRick Macklem return (0); 2969ec7b004SRick Macklem } 2979ec7b004SRick Macklem 2989ec7b004SRick Macklem /* 2999ec7b004SRick Macklem * Set the credentials to refer to root. 3009ec7b004SRick Macklem * If only the various BSDen could agree on whether cr_gid is a separate 3019ec7b004SRick Macklem * field or cr_groups[0]... 3029ec7b004SRick Macklem */ 3039ec7b004SRick Macklem void 3049ec7b004SRick Macklem newnfs_setroot(struct ucred *cred) 3059ec7b004SRick Macklem { 3069ec7b004SRick Macklem 3079ec7b004SRick Macklem cred->cr_uid = 0; 3089ec7b004SRick Macklem cred->cr_groups[0] = 0; 3099ec7b004SRick Macklem cred->cr_ngroups = 1; 3109ec7b004SRick Macklem } 3119ec7b004SRick Macklem 3129ec7b004SRick Macklem /* 3139ec7b004SRick Macklem * Get the client credential. Used for Renew and recovery. 3149ec7b004SRick Macklem */ 3159ec7b004SRick Macklem struct ucred * 3169ec7b004SRick Macklem newnfs_getcred(void) 3179ec7b004SRick Macklem { 3189ec7b004SRick Macklem struct ucred *cred; 3199ec7b004SRick Macklem struct thread *td = curthread; 3209ec7b004SRick Macklem 3219ec7b004SRick Macklem cred = crdup(td->td_ucred); 3229ec7b004SRick Macklem newnfs_setroot(cred); 3239ec7b004SRick Macklem return (cred); 3249ec7b004SRick Macklem } 3259ec7b004SRick Macklem 3269ec7b004SRick Macklem /* 3279ec7b004SRick Macklem * Nfs timer routine 3289ec7b004SRick Macklem * Call the nfsd's timer function once/sec. 3299ec7b004SRick Macklem */ 3309ec7b004SRick Macklem void 3319ec7b004SRick Macklem newnfs_timer(void *arg) 3329ec7b004SRick Macklem { 3339ec7b004SRick Macklem static time_t lasttime = 0; 3349ec7b004SRick Macklem /* 3359ec7b004SRick Macklem * Call the server timer, if set up. 3369ec7b004SRick Macklem * The argument indicates if it is the next second and therefore 3379ec7b004SRick Macklem * leases should be checked. 3389ec7b004SRick Macklem */ 3399ec7b004SRick Macklem if (lasttime != NFSD_MONOSEC) { 3409ec7b004SRick Macklem lasttime = NFSD_MONOSEC; 3419ec7b004SRick Macklem if (nfsd_call_servertimer != NULL) 3429ec7b004SRick Macklem (*nfsd_call_servertimer)(); 3439ec7b004SRick Macklem } 3449ec7b004SRick Macklem callout_reset(&newnfsd_callout, nfscl_ticks, newnfs_timer, NULL); 3459ec7b004SRick Macklem } 3469ec7b004SRick Macklem 3479ec7b004SRick Macklem 3489ec7b004SRick Macklem /* 34923f929dfSRick Macklem * Sleep for a short period of time unless errval == NFSERR_GRACE, where 35023f929dfSRick Macklem * the sleep should be for 5 seconds. 3519ec7b004SRick Macklem * Since lbolt doesn't exist in FreeBSD-CURRENT, just use a timeout on 3529ec7b004SRick Macklem * an event that never gets a wakeup. Only return EINTR or 0. 3539ec7b004SRick Macklem */ 3549ec7b004SRick Macklem int 35523f929dfSRick Macklem nfs_catnap(int prio, int errval, const char *wmesg) 3569ec7b004SRick Macklem { 3579ec7b004SRick Macklem static int non_event; 3589ec7b004SRick Macklem int ret; 3599ec7b004SRick Macklem 36023f929dfSRick Macklem if (errval == NFSERR_GRACE) 36123f929dfSRick Macklem ret = tsleep(&non_event, prio, wmesg, 5 * hz); 36223f929dfSRick Macklem else 3639ec7b004SRick Macklem ret = tsleep(&non_event, prio, wmesg, 1); 3649ec7b004SRick Macklem if (ret != EINTR) 3659ec7b004SRick Macklem ret = 0; 3669ec7b004SRick Macklem return (ret); 3679ec7b004SRick Macklem } 3689ec7b004SRick Macklem 3699ec7b004SRick Macklem /* 3709ec7b004SRick Macklem * Get referral. For now, just fail. 3719ec7b004SRick Macklem */ 3729ec7b004SRick Macklem struct nfsreferral * 3739ec7b004SRick Macklem nfsv4root_getreferral(struct vnode *vp, struct vnode *dvp, u_int32_t fileno) 3749ec7b004SRick Macklem { 3759ec7b004SRick Macklem 3769ec7b004SRick Macklem return (NULL); 3779ec7b004SRick Macklem } 3789ec7b004SRick Macklem 3799ec7b004SRick Macklem static int 3809ec7b004SRick Macklem nfssvc_nfscommon(struct thread *td, struct nfssvc_args *uap) 3819ec7b004SRick Macklem { 3829ec7b004SRick Macklem int error; 3839ec7b004SRick Macklem 3849ec7b004SRick Macklem error = nfssvc_call(td, uap, td->td_ucred); 3859ec7b004SRick Macklem return (error); 3869ec7b004SRick Macklem } 3879ec7b004SRick Macklem 3889ec7b004SRick Macklem static int 3899ec7b004SRick Macklem nfssvc_call(struct thread *p, struct nfssvc_args *uap, struct ucred *cred) 3909ec7b004SRick Macklem { 3919ec7b004SRick Macklem int error = EINVAL; 3929ec7b004SRick Macklem struct nfsd_idargs nid; 3939ec7b004SRick Macklem 3949ec7b004SRick Macklem if (uap->flag & NFSSVC_IDNAME) { 3959ec7b004SRick Macklem error = copyin(uap->argp, (caddr_t)&nid, sizeof (nid)); 3969ec7b004SRick Macklem if (error) 3979ec7b004SRick Macklem return (error); 3989ec7b004SRick Macklem error = nfssvc_idname(&nid); 3999ec7b004SRick Macklem return (error); 4009ec7b004SRick Macklem } else if (uap->flag & NFSSVC_GETSTATS) { 4019ec7b004SRick Macklem error = copyout(&newnfsstats, 4029ec7b004SRick Macklem CAST_USER_ADDR_T(uap->argp), sizeof (newnfsstats)); 4039ec7b004SRick Macklem return (error); 4049ec7b004SRick Macklem } else if (uap->flag & NFSSVC_NFSUSERDPORT) { 4059ec7b004SRick Macklem u_short sockport; 4069ec7b004SRick Macklem 4079ec7b004SRick Macklem error = copyin(uap->argp, (caddr_t)&sockport, 4089ec7b004SRick Macklem sizeof (u_short)); 4099ec7b004SRick Macklem if (!error) 4109ec7b004SRick Macklem error = nfsrv_nfsuserdport(sockport, p); 4119ec7b004SRick Macklem } else if (uap->flag & NFSSVC_NFSUSERDDELPORT) { 4129ec7b004SRick Macklem nfsrv_nfsuserddelport(); 4139ec7b004SRick Macklem error = 0; 4149ec7b004SRick Macklem } 4159ec7b004SRick Macklem return (error); 4169ec7b004SRick Macklem } 4179ec7b004SRick Macklem 4189ec7b004SRick Macklem /* 4199ec7b004SRick Macklem * called by all three modevent routines, so that it gets things 4209ec7b004SRick Macklem * initialized soon enough. 4219ec7b004SRick Macklem */ 4229ec7b004SRick Macklem void 4239ec7b004SRick Macklem newnfs_portinit(void) 4249ec7b004SRick Macklem { 4259ec7b004SRick Macklem static int inited = 0; 4269ec7b004SRick Macklem 4279ec7b004SRick Macklem if (inited) 4289ec7b004SRick Macklem return; 4299ec7b004SRick Macklem inited = 1; 4309ec7b004SRick Macklem /* Initialize SMP locks used by both client and server. */ 4319ec7b004SRick Macklem mtx_init(&newnfsd_mtx, "newnfsd_mtx", NULL, MTX_DEF); 4329ec7b004SRick Macklem mtx_init(&nfs_state_mutex, "nfs_state_mutex", NULL, MTX_DEF); 4339ec7b004SRick Macklem } 4349ec7b004SRick Macklem 435ee1c1433SRick Macklem /* 436ee1c1433SRick Macklem * Determine if the file system supports NFSv4 ACLs. 437ee1c1433SRick Macklem * Return 1 if it does, 0 otherwise. 438ee1c1433SRick Macklem */ 439ee1c1433SRick Macklem int 440*a09001a8SRick Macklem nfs_supportsnfsv4acls(struct vnode *vp) 441ee1c1433SRick Macklem { 442*a09001a8SRick Macklem int error; 443*a09001a8SRick Macklem register_t retval; 444ee1c1433SRick Macklem 445*a09001a8SRick Macklem ASSERT_VOP_LOCKED(vp, "nfs supports nfsv4acls"); 446*a09001a8SRick Macklem 447*a09001a8SRick Macklem if (nfsrv_useacl == 0) 448ee1c1433SRick Macklem return (0); 449*a09001a8SRick Macklem error = VOP_PATHCONF(vp, _PC_ACL_NFS4, &retval); 450*a09001a8SRick Macklem if (error == 0 && retval != 0) 451ee1c1433SRick Macklem return (1); 452ee1c1433SRick Macklem return (0); 453ee1c1433SRick Macklem } 454ee1c1433SRick Macklem 4559ec7b004SRick Macklem extern int (*nfsd_call_nfscommon)(struct thread *, struct nfssvc_args *); 4569ec7b004SRick Macklem 4579ec7b004SRick Macklem /* 4589ec7b004SRick Macklem * Called once to initialize data structures... 4599ec7b004SRick Macklem */ 4609ec7b004SRick Macklem static int 4619ec7b004SRick Macklem nfscommon_modevent(module_t mod, int type, void *data) 4629ec7b004SRick Macklem { 4639ec7b004SRick Macklem int error = 0; 4649ec7b004SRick Macklem static int loaded = 0; 4659ec7b004SRick Macklem 4669ec7b004SRick Macklem switch (type) { 4679ec7b004SRick Macklem case MOD_LOAD: 4689ec7b004SRick Macklem if (loaded) 4699ec7b004SRick Macklem return (0); 4709ec7b004SRick Macklem newnfs_portinit(); 4719ec7b004SRick Macklem mtx_init(&nfs_nameid_mutex, "nfs_nameid_mutex", NULL, MTX_DEF); 4729ec7b004SRick Macklem mtx_init(&nfs_sockl_mutex, "nfs_sockl_mutex", NULL, MTX_DEF); 4739ec7b004SRick Macklem mtx_init(&nfs_slock_mutex, "nfs_slock_mutex", NULL, MTX_DEF); 4749ec7b004SRick Macklem mtx_init(&nfs_req_mutex, "nfs_req_mutex", NULL, MTX_DEF); 4759ec7b004SRick Macklem mtx_init(&nfsrv_nfsuserdsock.nr_mtx, "nfsuserd", NULL, 4769ec7b004SRick Macklem MTX_DEF); 4779ec7b004SRick Macklem callout_init(&newnfsd_callout, CALLOUT_MPSAFE); 4789ec7b004SRick Macklem newnfs_init(); 4799ec7b004SRick Macklem nfsd_call_nfscommon = nfssvc_nfscommon; 4809ec7b004SRick Macklem loaded = 1; 4819ec7b004SRick Macklem break; 4829ec7b004SRick Macklem 4839ec7b004SRick Macklem case MOD_UNLOAD: 4849ec7b004SRick Macklem if (newnfs_numnfsd != 0 || nfsrv_nfsuserd != 0 || 4859ec7b004SRick Macklem nfs_numnfscbd != 0) { 4869ec7b004SRick Macklem error = EBUSY; 4879ec7b004SRick Macklem break; 4889ec7b004SRick Macklem } 4899ec7b004SRick Macklem 4909ec7b004SRick Macklem nfsd_call_nfscommon = NULL; 4919ec7b004SRick Macklem callout_drain(&newnfsd_callout); 4929ec7b004SRick Macklem /* and get rid of the mutexes */ 4939ec7b004SRick Macklem mtx_destroy(&nfs_nameid_mutex); 4949ec7b004SRick Macklem mtx_destroy(&newnfsd_mtx); 4959ec7b004SRick Macklem mtx_destroy(&nfs_state_mutex); 4969ec7b004SRick Macklem mtx_destroy(&nfs_sockl_mutex); 4979ec7b004SRick Macklem mtx_destroy(&nfs_slock_mutex); 4989ec7b004SRick Macklem mtx_destroy(&nfs_req_mutex); 4999ec7b004SRick Macklem mtx_destroy(&nfsrv_nfsuserdsock.nr_mtx); 5009ec7b004SRick Macklem loaded = 0; 5019ec7b004SRick Macklem break; 5029ec7b004SRick Macklem default: 5039ec7b004SRick Macklem error = EOPNOTSUPP; 5049ec7b004SRick Macklem break; 5059ec7b004SRick Macklem } 5069ec7b004SRick Macklem return error; 5079ec7b004SRick Macklem } 5089ec7b004SRick Macklem static moduledata_t nfscommon_mod = { 5099ec7b004SRick Macklem "nfscommon", 5109ec7b004SRick Macklem nfscommon_modevent, 5119ec7b004SRick Macklem NULL, 5129ec7b004SRick Macklem }; 5139ec7b004SRick Macklem DECLARE_MODULE(nfscommon, nfscommon_mod, SI_SUB_VFS, SI_ORDER_ANY); 5149ec7b004SRick Macklem 5159ec7b004SRick Macklem /* So that loader and kldload(2) can find us, wherever we are.. */ 5169ec7b004SRick Macklem MODULE_VERSION(nfscommon, 1); 5179ec7b004SRick Macklem MODULE_DEPEND(nfscommon, nfssvc, 1, 1, 1); 5189ec7b004SRick Macklem MODULE_DEPEND(nfscommon, krpc, 1, 1, 1); 5199ec7b004SRick Macklem 520