/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2009 Rick Macklem, University of Guelph4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/2728#ifndef _NFS_NFSRVSTATE_H_29#define _NFS_NFSRVSTATE_H_3031#if defined(_KERNEL) || defined(KERNEL)32/*33* Definitions for NFS V4 server state handling.34*/3536/*37* List heads for nfsclient, nfsstate and nfslockfile.38* (Some systems seem to like to dynamically size these things, but I39* don't see any point in doing so for these ones.)40*/41LIST_HEAD(nfsclienthashhead, nfsclient);42LIST_HEAD(nfsstatehead, nfsstate);43LIST_HEAD(nfslockhead, nfslock);44LIST_HEAD(nfslockhashhead, nfslockfile);45LIST_HEAD(nfssessionhead, nfsdsession);46LIST_HEAD(nfssessionhashhead, nfsdsession);47TAILQ_HEAD(nfslayouthead, nfslayout);48SLIST_HEAD(nfsdsdirhead, nfsdsdir);49TAILQ_HEAD(nfsdevicehead, nfsdevice);50LIST_HEAD(nfsdontlisthead, nfsdontlist);5152/*53* List head for nfsusrgrp.54*/55TAILQ_HEAD(nfsuserhashhead, nfsusrgrp);5657#define NFSCLIENTHASH(id) \58(&NFSD_VNET(nfsclienthash)[(id).lval[1] % nfsrv_clienthashsize])59#define NFSSTATEHASH(clp, id) \60(&((clp)->lc_stateid[(id).other[2] % nfsrv_statehashsize]))61#define NFSUSERHASH(id) \62(&NFSD_VNET(nfsuserhash)[(id) % nfsrv_lughashsize])63#define NFSUSERNAMEHASH(p, l) \64(&NFSD_VNET(nfsusernamehash)[((l)>=4?(*(p)+*((p)+1)+*((p)+2)+*((p)+3)):*(p)) \65% nfsrv_lughashsize])66#define NFSGROUPHASH(id) \67(&NFSD_VNET(nfsgrouphash)[(id) % nfsrv_lughashsize])68#define NFSGROUPNAMEHASH(p, l) \69(&NFSD_VNET(nfsgroupnamehash)[((l)>=4?(*(p)+*((p)+1)+*((p)+2)+*((p)+3)):*(p)) \70% nfsrv_lughashsize])7172struct nfssessionhash {73struct mtx mtx;74struct nfssessionhashhead list;75};76#define NFSSESSIONHASH(f) \77(&NFSD_VNET(nfssessionhash)[nfsrv_hashsessionid(f) % \78nfsrv_sessionhashsize])7980struct nfslayouthash {81struct mtx mtx;82struct nfslayouthead list;83};84#define NFSLAYOUTHASH(f) \85(&nfslayouthash[nfsrv_hashfh(f) % nfsrv_layouthashsize])8687/*88* Client server structure for V4. It is doubly linked into two lists.89* The first is a hash table based on the clientid and the second is a90* list of all clients maintained in LRU order.91* The actual size malloc'd is large enough to accommodate the id string.92*/93struct nfsclient {94LIST_ENTRY(nfsclient) lc_hash; /* Clientid hash list */95struct nfsstatehead *lc_stateid; /* Stateid hash */96struct nfsstatehead lc_open; /* Open owner list */97struct nfsstatehead lc_deleg; /* Delegations */98struct nfsstatehead lc_olddeleg; /* and old delegations */99struct nfssessionhead lc_session; /* List of NFSv4.1 sessions */100uint64_t lc_prevsess; /* CreateSession cache */101time_t lc_expiry; /* Expiry time (sec) */102time_t lc_delegtime; /* Old deleg expiry (sec) */103nfsquad_t lc_clientid; /* 64 bit clientid */104nfsquad_t lc_confirm; /* 64 bit confirm value */105nfsopbit_t lc_mustops; /* Must ops SP4_MACH_CRED */106nfsopbit_t lc_allowops; /* Allowed ops SP4_MACH_CRED */107u_int32_t lc_program; /* RPC Program # */108u_int32_t lc_callback; /* Callback id */109u_int32_t lc_stateindex; /* Current state index# */110u_int32_t lc_statemaxindex; /* Max state index# */111u_int32_t lc_cbref; /* Cnt of callbacks */112uid_t lc_uid; /* User credential */113gid_t lc_gid;114u_int16_t lc_idlen; /* Client ID and len */115u_int16_t lc_namelen; /* plus GSS principal and len */116u_char *lc_name;117struct nfssockreq lc_req; /* Callback info */118u_int32_t lc_flags; /* LCL_ flag bits */119u_char lc_verf[NFSX_VERF]; /* client verifier */120u_char lc_id[1]; /* Malloc'd correct size */121};122123#define CLOPS_CONFIRM 0x0001124#define CLOPS_RENEW 0x0002125#define CLOPS_RENEWOP 0x0004126127/*128* Structure for NFSv4.1 Layouts.129* Malloc'd to correct size for the lay_xdr.130*/131struct nfslayout {132TAILQ_ENTRY(nfslayout) lay_list;133nfsv4stateid_t lay_stateid;134nfsquad_t lay_clientid;135fhandle_t lay_fh;136char lay_deviceid[NFSX_V4DEVICEID];137fsid_t lay_fsid;138uint32_t lay_layoutlen;139uint16_t lay_mirrorcnt;140uint16_t lay_trycnt;141uint16_t lay_type;142uint16_t lay_flags;143uint32_t lay_xdr[0];144};145146/* Flags for lay_flags. */147#define NFSLAY_READ 0x0001148#define NFSLAY_RW 0x0002149#define NFSLAY_RECALL 0x0004150#define NFSLAY_RETURNED 0x0008151#define NFSLAY_CALLB 0x0010152#define NFSLAY_NOSPC 0x0020153154/*155* Structure for an NFSv4.1 session.156* Locking rules for this structure.157* To add/delete one of these structures from the lists, you must lock158* both: NFSLOCKSTATE() and NFSLOCKSESSION(session hashhead) in that order.159* To traverse the lists looking for one of these, you must hold one160* of these two locks.161* The exception is if the thread holds the exclusive root sleep lock.162* In this case, all other nfsd threads are blocked, so locking the163* mutexes isn't required.164* When manipulating sess_refcnt, NFSLOCKSTATE() must be locked.165* When manipulating the fields withinsess_cbsess except nfsess_xprt,166* sess_cbsess.nfsess_mtx must be locked.167* When manipulating sess_slots and sess_cbsess.nfsess_xprt,168* NFSLOCKSESSION(session hashhead) must be locked.169*/170struct nfsdsession {171uint64_t sess_refcnt; /* Reference count. */172LIST_ENTRY(nfsdsession) sess_hash; /* Hash list of sessions. */173LIST_ENTRY(nfsdsession) sess_list; /* List of client sessions. */174struct nfsslot sess_slots[NFSV4_SLOTS];175struct nfsclient *sess_clp; /* Associated clientid. */176uint32_t sess_crflags;177uint32_t sess_cbprogram;178uint32_t sess_maxreq;179uint32_t sess_maxresp;180uint32_t sess_maxrespcached;181uint32_t sess_maxops;182uint32_t sess_maxslots;183uint32_t sess_cbmaxreq;184uint32_t sess_cbmaxresp;185uint32_t sess_cbmaxrespcached;186uint32_t sess_cbmaxops;187uint8_t sess_sessionid[NFSX_V4SESSIONID];188struct nfsclsession sess_cbsess; /* Callback session. */189};190191/*192* Nfs state structure. I couldn't resist overloading this one, since193* it makes cleanup, etc. simpler. These structures are used in four ways:194* - open_owner structures chained off of nfsclient195* - open file structures chained off an open_owner structure196* - lock_owner structures chained off an open file structure197* - delegated file structures chained off of nfsclient and nfslockfile198* - the ls_list field is used for the chain it is in199* - the ls_head structure is used to chain off the sibling structure200* (it is a union between an nfsstate and nfslock structure head)201* If it is a lockowner stateid, nfslock structures hang off it.202* For the open file and lockowner cases, it is in the hash table in203* nfsclient for stateid.204*/205struct nfsstate {206LIST_ENTRY(nfsstate) ls_hash; /* Hash list entry */207LIST_ENTRY(nfsstate) ls_list; /* List of opens/delegs */208LIST_ENTRY(nfsstate) ls_file; /* Opens/Delegs for a file */209union {210struct nfsstatehead open; /* Opens list */211struct nfslockhead lock; /* Locks list */212} ls_head;213nfsv4stateid_t ls_stateid; /* The state id */214u_int32_t ls_seq; /* seq id */215uid_t ls_uid; /* uid of locker */216u_int32_t ls_flags; /* Type of lock, etc. */217union {218struct nfsstate *openowner; /* Open only */219u_int32_t opentolockseq; /* Lock call only */220u_int32_t noopens; /* Openowner only */221struct {222u_quad_t filerev; /* Delegations only */223time_t expiry;224time_t limit;225u_int64_t compref;226time_t last;227} deleg;228} ls_un;229struct nfslockfile *ls_lfp; /* Back pointer */230struct nfsrvcache *ls_op; /* Op cache reference */231struct nfsclient *ls_clp; /* Back pointer */232u_short ls_ownerlen; /* Length of ls_owner */233u_char ls_owner[1]; /* malloc'd the correct size */234};235#define ls_lock ls_head.lock236#define ls_open ls_head.open237#define ls_opentolockseq ls_un.opentolockseq238#define ls_openowner ls_un.openowner239#define ls_openstp ls_un.openowner240#define ls_noopens ls_un.noopens241#define ls_filerev ls_un.deleg.filerev242#define ls_delegtime ls_un.deleg.expiry243#define ls_delegtimelimit ls_un.deleg.limit244#define ls_compref ls_un.deleg.compref245#define ls_lastrecall ls_un.deleg.last246247/*248* Nfs lock structure.249* This structure is chained off of the nfsstate (the lockowner) and250* nfslockfile (the file) structures, for the file and owner it251* refers to. It holds flags and a byte range.252* It also has back pointers to the associated lock_owner and lockfile.253*/254struct nfslock {255LIST_ENTRY(nfslock) lo_lckowner;256LIST_ENTRY(nfslock) lo_lckfile;257struct nfsstate *lo_stp;258struct nfslockfile *lo_lfp;259u_int64_t lo_first;260u_int64_t lo_end;261u_int32_t lo_flags;262};263264/*265* Structure used to return a conflicting lock. (Must be large266* enough for the largest lock owner we can have.)267*/268struct nfslockconflict {269nfsquad_t cl_clientid;270u_int64_t cl_first;271u_int64_t cl_end;272u_int32_t cl_flags;273u_short cl_ownerlen;274u_char cl_owner[NFSV4_OPAQUELIMIT];275};276277/*278* This structure is used to keep track of local locks that might need279* to be rolled back.280*/281struct nfsrollback {282LIST_ENTRY(nfsrollback) rlck_list;283uint64_t rlck_first;284uint64_t rlck_end;285int rlck_type;286};287288/*289* This structure refers to a file for which lock(s) and/or open(s) exist.290* Searched via hash table on file handle or found via the back pointer from an291* open or lock owner.292*/293struct nfslockfile {294LIST_HEAD(, nfsstate) lf_open; /* Open list */295LIST_HEAD(, nfsstate) lf_deleg; /* Delegation list */296LIST_HEAD(, nfslock) lf_lock; /* Lock list */297LIST_HEAD(, nfslock) lf_locallock; /* Local lock list */298LIST_HEAD(, nfsrollback) lf_rollback; /* Local lock rollback list */299LIST_ENTRY(nfslockfile) lf_hash; /* Hash list entry */300fhandle_t lf_fh; /* The file handle */301struct nfsv4lock lf_locallock_lck; /* serialize local locking */302int lf_usecount; /* Ref count for locking */303};304305/*306* This structure is malloc'd an chained off hash lists for user/group307* names.308*/309struct nfsusrgrp {310TAILQ_ENTRY(nfsusrgrp) lug_numhash; /* Hash by id# */311TAILQ_ENTRY(nfsusrgrp) lug_namehash; /* and by name */312time_t lug_expiry; /* Expiry time in sec */313union {314uid_t un_uid; /* id# */315gid_t un_gid;316} lug_un;317struct ucred *lug_cred; /* Cred. with groups list */318int lug_namelen; /* Name length */319u_char lug_name[1]; /* malloc'd correct length */320};321#define lug_uid lug_un.un_uid322#define lug_gid lug_un.un_gid323324/*325* These structures are used for the stable storage restart stuff.326*/327/*328* Record at beginning of file.329*/330struct nfsf_rec {331u_int32_t lease; /* Lease duration */332u_int32_t numboots; /* Number of boottimes */333};334335void nfsrv_cleanclient(struct nfsclient *, NFSPROC_T *, bool, SVCXPRT **);336void nfsrv_freedeleglist(struct nfsstatehead *);337338/*339* This structure is used to create the list of device info entries for340* a GetDeviceInfo operation and stores the DS server info.341* The nfsdev_addrandhost field has the fully qualified host domain name342* followed by the network address in XDR.343* It is allocated with nfsrv_dsdirsize nfsdev_dsdir[] entries.344*/345struct nfsdevice {346TAILQ_ENTRY(nfsdevice) nfsdev_list;347vnode_t nfsdev_dvp;348struct nfsmount *nfsdev_nmp;349char nfsdev_deviceid[NFSX_V4DEVICEID];350uint16_t nfsdev_hostnamelen;351uint16_t nfsdev_fileaddrlen;352uint16_t nfsdev_flexaddrlen;353uint16_t nfsdev_mdsisset;354char *nfsdev_fileaddr;355char *nfsdev_flexaddr;356char *nfsdev_host;357fsid_t nfsdev_mdsfsid;358uint32_t nfsdev_nextdir;359bool nfsdev_nospc;360vnode_t nfsdev_dsdir[0];361};362363/*364* This structure holds the va_size, va_filerev, va_atime, va_mtime and365* va_bytes for the DS file and is stored in the metadata file's extended366* attribute pnfsd.dsattr.367* opnfsdsattr was missing the va_bytes field and, as such, it was updated.368*/369struct opnfsdsattr {370uint64_t dsa_filerev;371uint64_t dsa_size;372struct timespec dsa_atime;373struct timespec dsa_mtime;374};375376struct pnfsdsattr {377uint64_t dsa_filerev;378uint64_t dsa_size;379struct timespec dsa_atime;380struct timespec dsa_mtime;381uint64_t dsa_bytes;382};383384/*385* This structure is a list element for a list the pNFS server uses to386* mark that the recovery of a mirror file is in progress.387*/388struct nfsdontlist {389LIST_ENTRY(nfsdontlist) nfsmr_list;390uint32_t nfsmr_flags;391fhandle_t nfsmr_fh;392};393394/* nfsmr_flags bits. */395#define NFSMR_DONTLAYOUT 0x00000001396397#endif /* defined(_KERNEL) || defined(KERNEL) */398399/*400* This structure holds the information about the DS file and is stored401* in the metadata file's extended attribute called pnfsd.dsfile.402*/403#define PNFS_FILENAME_LEN (2 * sizeof(fhandle_t))404struct pnfsdsfile {405fhandle_t dsf_fh;406uint32_t dsf_dir;407union {408struct sockaddr_in sin;409struct sockaddr_in6 sin6;410} dsf_nam;411char dsf_filename[PNFS_FILENAME_LEN + 1];412};413#define dsf_sin dsf_nam.sin414#define dsf_sin6 dsf_nam.sin6415416#endif /* _NFS_NFSRVSTATE_H_ */417418419