1*5b28f239Srillig /* $NetBSD: nfsrvstate.h,v 1.3 2024/09/08 09:36:51 rillig Exp $ */ 26ca35587Sdholland /*- 36ca35587Sdholland * Copyright (c) 2009 Rick Macklem, University of Guelph 46ca35587Sdholland * All rights reserved. 56ca35587Sdholland * 66ca35587Sdholland * Redistribution and use in source and binary forms, with or without 76ca35587Sdholland * modification, are permitted provided that the following conditions 86ca35587Sdholland * are met: 96ca35587Sdholland * 1. Redistributions of source code must retain the above copyright 106ca35587Sdholland * notice, this list of conditions and the following disclaimer. 116ca35587Sdholland * 2. Redistributions in binary form must reproduce the above copyright 126ca35587Sdholland * notice, this list of conditions and the following disclaimer in the 136ca35587Sdholland * documentation and/or other materials provided with the distribution. 146ca35587Sdholland * 156ca35587Sdholland * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 166ca35587Sdholland * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 176ca35587Sdholland * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 186ca35587Sdholland * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 196ca35587Sdholland * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 206ca35587Sdholland * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 216ca35587Sdholland * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 226ca35587Sdholland * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 236ca35587Sdholland * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 246ca35587Sdholland * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 256ca35587Sdholland * SUCH DAMAGE. 266ca35587Sdholland * 27e81f0ea2Spgoyette * FreeBSD: head/sys/fs/nfs/nfsrvstate.h 298788 2016-04-29 16:07:25Z pfg 28*5b28f239Srillig * $NetBSD: nfsrvstate.h,v 1.3 2024/09/08 09:36:51 rillig Exp $ 296ca35587Sdholland */ 306ca35587Sdholland 316ca35587Sdholland #ifndef _NFS_NFSRVSTATE_H_ 326ca35587Sdholland #define _NFS_NFSRVSTATE_H_ 336ca35587Sdholland 346ca35587Sdholland /* 356ca35587Sdholland * Definitions for NFS V4 server state handling. 366ca35587Sdholland */ 376ca35587Sdholland 386ca35587Sdholland /* 396ca35587Sdholland * List heads for nfsclient, nfsstate and nfslockfile. 406ca35587Sdholland * (Some systems seem to like to dynamically size these things, but I 416ca35587Sdholland * don't see any point in doing so for these ones.) 426ca35587Sdholland */ 436ca35587Sdholland LIST_HEAD(nfsclienthashhead, nfsclient); 446ca35587Sdholland LIST_HEAD(nfsstatehead, nfsstate); 456ca35587Sdholland LIST_HEAD(nfslockhead, nfslock); 466ca35587Sdholland LIST_HEAD(nfslockhashhead, nfslockfile); 47e81f0ea2Spgoyette LIST_HEAD(nfssessionhead, nfsdsession); 48e81f0ea2Spgoyette LIST_HEAD(nfssessionhashhead, nfsdsession); 496ca35587Sdholland 506ca35587Sdholland /* 516ca35587Sdholland * List head for nfsusrgrp. 526ca35587Sdholland */ 53e81f0ea2Spgoyette TAILQ_HEAD(nfsuserhashhead, nfsusrgrp); 546ca35587Sdholland 556ca35587Sdholland #define NFSCLIENTHASH(id) \ 56e81f0ea2Spgoyette (&nfsclienthash[(id).lval[1] % nfsrv_clienthashsize]) 576ca35587Sdholland #define NFSSTATEHASH(clp, id) \ 58e81f0ea2Spgoyette (&((clp)->lc_stateid[(id).other[2] % nfsrv_statehashsize])) 596ca35587Sdholland #define NFSUSERHASH(id) \ 60e81f0ea2Spgoyette (&nfsuserhash[(id) % nfsrv_lughashsize]) 616ca35587Sdholland #define NFSUSERNAMEHASH(p, l) \ 626ca35587Sdholland (&nfsusernamehash[((l)>=4?(*(p)+*((p)+1)+*((p)+2)+*((p)+3)):*(p)) \ 63e81f0ea2Spgoyette % nfsrv_lughashsize]) 646ca35587Sdholland #define NFSGROUPHASH(id) \ 65e81f0ea2Spgoyette (&nfsgrouphash[(id) % nfsrv_lughashsize]) 666ca35587Sdholland #define NFSGROUPNAMEHASH(p, l) \ 676ca35587Sdholland (&nfsgroupnamehash[((l)>=4?(*(p)+*((p)+1)+*((p)+2)+*((p)+3)):*(p)) \ 68e81f0ea2Spgoyette % nfsrv_lughashsize]) 69e81f0ea2Spgoyette 70e81f0ea2Spgoyette struct nfssessionhash { 710342d508Spgoyette struct kmutex mtx; 72e81f0ea2Spgoyette struct nfssessionhashhead list; 73e81f0ea2Spgoyette }; 74e81f0ea2Spgoyette #define NFSSESSIONHASH(f) \ 75e81f0ea2Spgoyette (&nfssessionhash[nfsrv_hashsessionid(f) % nfsrv_sessionhashsize]) 766ca35587Sdholland 776ca35587Sdholland /* 786ca35587Sdholland * Client server structure for V4. It is doubly linked into two lists. 796ca35587Sdholland * The first is a hash table based on the clientid and the second is a 806ca35587Sdholland * list of all clients maintained in LRU order. 81e81f0ea2Spgoyette * The actual size malloc'd is large enough to accommodate the id string. 826ca35587Sdholland */ 836ca35587Sdholland struct nfsclient { 846ca35587Sdholland LIST_ENTRY(nfsclient) lc_hash; /* Clientid hash list */ 85e81f0ea2Spgoyette struct nfsstatehead *lc_stateid; /* Stateid hash */ 866ca35587Sdholland struct nfsstatehead lc_open; /* Open owner list */ 876ca35587Sdholland struct nfsstatehead lc_deleg; /* Delegations */ 886ca35587Sdholland struct nfsstatehead lc_olddeleg; /* and old delegations */ 89e81f0ea2Spgoyette struct nfssessionhead lc_session; /* List of NFSv4.1 sessions */ 906ca35587Sdholland time_t lc_expiry; /* Expiry time (sec) */ 916ca35587Sdholland time_t lc_delegtime; /* Old deleg expiry (sec) */ 926ca35587Sdholland nfsquad_t lc_clientid; /* 64 bit clientid */ 936ca35587Sdholland nfsquad_t lc_confirm; /* 64 bit confirm value */ 946ca35587Sdholland u_int32_t lc_program; /* RPC Program # */ 956ca35587Sdholland u_int32_t lc_callback; /* Callback id */ 966ca35587Sdholland u_int32_t lc_stateindex; /* Current state index# */ 976ca35587Sdholland u_int32_t lc_statemaxindex; /* Max state index# */ 986ca35587Sdholland u_int32_t lc_cbref; /* Cnt of callbacks */ 996ca35587Sdholland uid_t lc_uid; /* User credential */ 1006ca35587Sdholland gid_t lc_gid; 101e81f0ea2Spgoyette u_int16_t lc_idlen; /* Client ID and len */ 102e81f0ea2Spgoyette u_int16_t lc_namelen; /* plus GSS principal and len */ 1036ca35587Sdholland u_char *lc_name; 1046ca35587Sdholland struct nfssockreq lc_req; /* Callback info */ 1056ca35587Sdholland u_int32_t lc_flags; /* LCL_ flag bits */ 1066ca35587Sdholland u_char lc_verf[NFSX_VERF]; /* client verifier */ 1076ca35587Sdholland u_char lc_id[1]; /* Malloc'd correct size */ 1086ca35587Sdholland }; 1096ca35587Sdholland 1106ca35587Sdholland #define CLOPS_CONFIRM 0x0001 1116ca35587Sdholland #define CLOPS_RENEW 0x0002 1126ca35587Sdholland #define CLOPS_RENEWOP 0x0004 1136ca35587Sdholland 1146ca35587Sdholland /* 115e81f0ea2Spgoyette * Structure for an NFSv4.1 session. 116e81f0ea2Spgoyette * Locking rules for this structure. 117e81f0ea2Spgoyette * To add/delete one of these structures from the lists, you must lock 118e81f0ea2Spgoyette * both: NFSLOCKSTATE() and NFSLOCKSESSION(session hashhead) in that order. 119e81f0ea2Spgoyette * To traverse the lists looking for one of these, you must hold one 120e81f0ea2Spgoyette * of these two locks. 121e81f0ea2Spgoyette * The exception is if the thread holds the exclusive root sleep lock. 122e81f0ea2Spgoyette * In this case, all other nfsd threads are blocked, so locking the 123e81f0ea2Spgoyette * mutexes isn't required. 124e81f0ea2Spgoyette * When manipulating sess_refcnt, NFSLOCKSTATE() must be locked. 125e81f0ea2Spgoyette * When manipulating the fields withinsess_cbsess except nfsess_xprt, 126e81f0ea2Spgoyette * sess_cbsess.nfsess_mtx must be locked. 127e81f0ea2Spgoyette * When manipulating sess_slots and sess_cbsess.nfsess_xprt, 128e81f0ea2Spgoyette * NFSLOCKSESSION(session hashhead) must be locked. 129e81f0ea2Spgoyette */ 130e81f0ea2Spgoyette struct nfsdsession { 131e81f0ea2Spgoyette uint64_t sess_refcnt; /* Reference count. */ 132e81f0ea2Spgoyette LIST_ENTRY(nfsdsession) sess_hash; /* Hash list of sessions. */ 133e81f0ea2Spgoyette LIST_ENTRY(nfsdsession) sess_list; /* List of client sessions. */ 134e81f0ea2Spgoyette struct nfsslot sess_slots[NFSV4_SLOTS]; 135e81f0ea2Spgoyette struct nfsclient *sess_clp; /* Associated clientid. */ 136e81f0ea2Spgoyette uint32_t sess_crflags; 137e81f0ea2Spgoyette uint32_t sess_cbprogram; 138e81f0ea2Spgoyette uint32_t sess_maxreq; 139e81f0ea2Spgoyette uint32_t sess_maxresp; 140e81f0ea2Spgoyette uint32_t sess_maxrespcached; 141e81f0ea2Spgoyette uint32_t sess_maxops; 142e81f0ea2Spgoyette uint32_t sess_maxslots; 143e81f0ea2Spgoyette uint32_t sess_cbmaxreq; 144e81f0ea2Spgoyette uint32_t sess_cbmaxresp; 145e81f0ea2Spgoyette uint32_t sess_cbmaxrespcached; 146e81f0ea2Spgoyette uint32_t sess_cbmaxops; 147e81f0ea2Spgoyette uint8_t sess_sessionid[NFSX_V4SESSIONID]; 148e81f0ea2Spgoyette struct nfsclsession sess_cbsess; /* Callback session. */ 149e81f0ea2Spgoyette }; 150e81f0ea2Spgoyette 151e81f0ea2Spgoyette /* 1526ca35587Sdholland * Nfs state structure. I couldn't resist overloading this one, since 1536ca35587Sdholland * it makes cleanup, etc. simpler. These structures are used in four ways: 1546ca35587Sdholland * - open_owner structures chained off of nfsclient 1556ca35587Sdholland * - open file structures chained off an open_owner structure 1566ca35587Sdholland * - lock_owner structures chained off an open file structure 1576ca35587Sdholland * - delegated file structures chained off of nfsclient and nfslockfile 1586ca35587Sdholland * - the ls_list field is used for the chain it is in 1596ca35587Sdholland * - the ls_head structure is used to chain off the sibling structure 1606ca35587Sdholland * (it is a union between an nfsstate and nfslock structure head) 1616ca35587Sdholland * If it is a lockowner stateid, nfslock structures hang off it. 1626ca35587Sdholland * For the open file and lockowner cases, it is in the hash table in 1636ca35587Sdholland * nfsclient for stateid. 1646ca35587Sdholland */ 1656ca35587Sdholland struct nfsstate { 1666ca35587Sdholland LIST_ENTRY(nfsstate) ls_hash; /* Hash list entry */ 1676ca35587Sdholland LIST_ENTRY(nfsstate) ls_list; /* List of opens/delegs */ 1686ca35587Sdholland LIST_ENTRY(nfsstate) ls_file; /* Opens/Delegs for a file */ 1696ca35587Sdholland union { 1706ca35587Sdholland struct nfsstatehead open; /* Opens list */ 1716ca35587Sdholland struct nfslockhead lock; /* Locks list */ 1726ca35587Sdholland } ls_head; 1736ca35587Sdholland nfsv4stateid_t ls_stateid; /* The state id */ 1746ca35587Sdholland u_int32_t ls_seq; /* seq id */ 1756ca35587Sdholland uid_t ls_uid; /* uid of locker */ 1766ca35587Sdholland u_int32_t ls_flags; /* Type of lock, etc. */ 1776ca35587Sdholland union { 1786ca35587Sdholland struct nfsstate *openowner; /* Open only */ 1796ca35587Sdholland u_int32_t opentolockseq; /* Lock call only */ 1806ca35587Sdholland u_int32_t noopens; /* Openowner only */ 1816ca35587Sdholland struct { 1826ca35587Sdholland u_quad_t filerev; /* Delegations only */ 1836ca35587Sdholland time_t expiry; 1846ca35587Sdholland time_t limit; 1856ca35587Sdholland u_int64_t compref; 1866ca35587Sdholland } deleg; 1876ca35587Sdholland } ls_un; 1886ca35587Sdholland struct nfslockfile *ls_lfp; /* Back pointer */ 1896ca35587Sdholland struct nfsrvcache *ls_op; /* Op cache reference */ 1906ca35587Sdholland struct nfsclient *ls_clp; /* Back pointer */ 1916ca35587Sdholland u_short ls_ownerlen; /* Length of ls_owner */ 1926ca35587Sdholland u_char ls_owner[1]; /* malloc'd the correct size */ 1936ca35587Sdholland }; 1946ca35587Sdholland #define ls_lock ls_head.lock 1956ca35587Sdholland #define ls_open ls_head.open 1966ca35587Sdholland #define ls_opentolockseq ls_un.opentolockseq 1976ca35587Sdholland #define ls_openowner ls_un.openowner 1986ca35587Sdholland #define ls_openstp ls_un.openowner 1996ca35587Sdholland #define ls_noopens ls_un.noopens 2006ca35587Sdholland #define ls_filerev ls_un.deleg.filerev 2016ca35587Sdholland #define ls_delegtime ls_un.deleg.expiry 2026ca35587Sdholland #define ls_delegtimelimit ls_un.deleg.limit 2036ca35587Sdholland #define ls_compref ls_un.deleg.compref 2046ca35587Sdholland 2056ca35587Sdholland /* 2066ca35587Sdholland * Nfs lock structure. 2076ca35587Sdholland * This structure is chained off of the nfsstate (the lockowner) and 2086ca35587Sdholland * nfslockfile (the file) structures, for the file and owner it 2096ca35587Sdholland * refers to. It holds flags and a byte range. 2106ca35587Sdholland * It also has back pointers to the associated lock_owner and lockfile. 2116ca35587Sdholland */ 2126ca35587Sdholland struct nfslock { 2136ca35587Sdholland LIST_ENTRY(nfslock) lo_lckowner; 2146ca35587Sdholland LIST_ENTRY(nfslock) lo_lckfile; 2156ca35587Sdholland struct nfsstate *lo_stp; 2166ca35587Sdholland struct nfslockfile *lo_lfp; 2176ca35587Sdholland u_int64_t lo_first; 2186ca35587Sdholland u_int64_t lo_end; 2196ca35587Sdholland u_int32_t lo_flags; 2206ca35587Sdholland }; 2216ca35587Sdholland 2226ca35587Sdholland /* 2236ca35587Sdholland * Structure used to return a conflicting lock. (Must be large 2246ca35587Sdholland * enough for the largest lock owner we can have.) 2256ca35587Sdholland */ 2266ca35587Sdholland struct nfslockconflict { 2276ca35587Sdholland nfsquad_t cl_clientid; 2286ca35587Sdholland u_int64_t cl_first; 2296ca35587Sdholland u_int64_t cl_end; 2306ca35587Sdholland u_int32_t cl_flags; 2316ca35587Sdholland u_short cl_ownerlen; 2326ca35587Sdholland u_char cl_owner[NFSV4_OPAQUELIMIT]; 2336ca35587Sdholland }; 2346ca35587Sdholland 2356ca35587Sdholland /* 2366ca35587Sdholland * This structure is used to keep track of local locks that might need 2376ca35587Sdholland * to be rolled back. 2386ca35587Sdholland */ 2396ca35587Sdholland struct nfsrollback { 2406ca35587Sdholland LIST_ENTRY(nfsrollback) rlck_list; 2416ca35587Sdholland uint64_t rlck_first; 2426ca35587Sdholland uint64_t rlck_end; 2436ca35587Sdholland int rlck_type; 2446ca35587Sdholland }; 2456ca35587Sdholland 2466ca35587Sdholland /* 2476ca35587Sdholland * This structure refers to a file for which lock(s) and/or open(s) exist. 2486ca35587Sdholland * Searched via hash table on file handle or found via the back pointer from an 2496ca35587Sdholland * open or lock owner. 2506ca35587Sdholland */ 2516ca35587Sdholland struct nfslockfile { 2526ca35587Sdholland LIST_HEAD(, nfsstate) lf_open; /* Open list */ 2536ca35587Sdholland LIST_HEAD(, nfsstate) lf_deleg; /* Delegation list */ 2546ca35587Sdholland LIST_HEAD(, nfslock) lf_lock; /* Lock list */ 2556ca35587Sdholland LIST_HEAD(, nfslock) lf_locallock; /* Local lock list */ 2566ca35587Sdholland LIST_HEAD(, nfsrollback) lf_rollback; /* Local lock rollback list */ 2576ca35587Sdholland LIST_ENTRY(nfslockfile) lf_hash; /* Hash list entry */ 2586ca35587Sdholland fhandle_t lf_fh; /* The file handle */ 2596ca35587Sdholland struct nfsv4lock lf_locallock_lck; /* serialize local locking */ 2606ca35587Sdholland int lf_usecount; /* Ref count for locking */ 2616ca35587Sdholland }; 2626ca35587Sdholland 2636ca35587Sdholland /* 264*5b28f239Srillig * This structure is malloc'd and chained off hash lists for user/group 2656ca35587Sdholland * names. 2666ca35587Sdholland */ 2676ca35587Sdholland struct nfsusrgrp { 268e81f0ea2Spgoyette TAILQ_ENTRY(nfsusrgrp) lug_numhash; /* Hash by id# */ 269e81f0ea2Spgoyette TAILQ_ENTRY(nfsusrgrp) lug_namehash; /* and by name */ 2706ca35587Sdholland time_t lug_expiry; /* Expiry time in sec */ 2716ca35587Sdholland union { 2726ca35587Sdholland uid_t un_uid; /* id# */ 2736ca35587Sdholland gid_t un_gid; 2746ca35587Sdholland } lug_un; 275e81f0ea2Spgoyette struct ucred *lug_cred; /* Cred. with groups list */ 2766ca35587Sdholland int lug_namelen; /* Name length */ 2776ca35587Sdholland u_char lug_name[1]; /* malloc'd correct length */ 2786ca35587Sdholland }; 2796ca35587Sdholland #define lug_uid lug_un.un_uid 2806ca35587Sdholland #define lug_gid lug_un.un_gid 2816ca35587Sdholland 2826ca35587Sdholland /* 2836ca35587Sdholland * These structures are used for the stable storage restart stuff. 2846ca35587Sdholland */ 2856ca35587Sdholland /* 2866ca35587Sdholland * Record at beginning of file. 2876ca35587Sdholland */ 2886ca35587Sdholland struct nfsf_rec { 2896ca35587Sdholland u_int32_t lease; /* Lease duration */ 2906ca35587Sdholland u_int32_t numboots; /* Number of boottimes */ 2916ca35587Sdholland }; 2926ca35587Sdholland 2936ca35587Sdholland #if defined(_KERNEL) || defined(KERNEL) 2946ca35587Sdholland void nfsrv_cleanclient(struct nfsclient *, NFSPROC_T *); 2956ca35587Sdholland void nfsrv_freedeleglist(struct nfsstatehead *); 2966ca35587Sdholland #endif 2976ca35587Sdholland 2986ca35587Sdholland #endif /* _NFS_NFSRVSTATE_H_ */ 299