1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2009 Rick Macklem, University of Guelph 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * $FreeBSD$ 29 */ 30 31 #ifndef _NFS_NFSCLSTATE_H_ 32 #define _NFS_NFSCLSTATE_H_ 33 34 /* 35 * Definitions for NFS V4 client state handling. 36 */ 37 LIST_HEAD(nfsclopenhead, nfsclopen); 38 LIST_HEAD(nfscllockownerhead, nfscllockowner); 39 SLIST_HEAD(nfscllockownerfhhead, nfscllockownerfh); 40 LIST_HEAD(nfscllockhead, nfscllock); 41 LIST_HEAD(nfsclhead, nfsclclient); 42 LIST_HEAD(nfsclownerhead, nfsclowner); 43 TAILQ_HEAD(nfscldeleghead, nfscldeleg); 44 LIST_HEAD(nfscldeleghash, nfscldeleg); 45 TAILQ_HEAD(nfscllayouthead, nfscllayout); 46 LIST_HEAD(nfscllayouthash, nfscllayout); 47 LIST_HEAD(nfsclflayouthead, nfsclflayout); 48 LIST_HEAD(nfscldevinfohead, nfscldevinfo); 49 LIST_HEAD(nfsclrecalllayouthead, nfsclrecalllayout); 50 #define NFSCLDELEGHASHSIZE 256 51 #define NFSCLDELEGHASH(c, f, l) \ 52 (&((c)->nfsc_deleghash[ncl_hash((f), (l)) % NFSCLDELEGHASHSIZE])) 53 #define NFSCLLAYOUTHASHSIZE 256 54 #define NFSCLLAYOUTHASH(c, f, l) \ 55 (&((c)->nfsc_layouthash[ncl_hash((f), (l)) % NFSCLLAYOUTHASHSIZE])) 56 57 /* Structure for NFSv4.1 session stuff. */ 58 struct nfsclsession { 59 struct mtx nfsess_mtx; 60 struct nfsslot nfsess_cbslots[NFSV4_CBSLOTS]; 61 nfsquad_t nfsess_clientid; 62 SVCXPRT *nfsess_xprt; /* For backchannel callback */ 63 uint32_t nfsess_slotseq[64]; /* Max for 64bit nm_slots */ 64 uint64_t nfsess_slots; 65 uint32_t nfsess_sequenceid; 66 uint32_t nfsess_maxcache; /* Max size for cached reply. */ 67 uint16_t nfsess_foreslots; 68 uint16_t nfsess_backslots; 69 uint8_t nfsess_sessionid[NFSX_V4SESSIONID]; 70 uint8_t nfsess_defunct; /* Non-zero for old sessions */ 71 }; 72 73 /* 74 * This structure holds the session, clientid and related information 75 * needed for an NFSv4.1 Meta Data Server (MDS) or Data Server (DS). 76 * It is malloc'd to the correct length. 77 */ 78 struct nfsclds { 79 TAILQ_ENTRY(nfsclds) nfsclds_list; 80 struct nfsclsession nfsclds_sess; 81 struct mtx nfsclds_mtx; 82 struct nfssockreq *nfsclds_sockp; 83 time_t nfsclds_expire; 84 uint16_t nfsclds_flags; 85 uint16_t nfsclds_servownlen; 86 uint8_t nfsclds_verf[NFSX_VERF]; 87 uint8_t nfsclds_serverown[0]; 88 }; 89 90 /* 91 * Flags for nfsclds_flags. 92 */ 93 #define NFSCLDS_HASWRITEVERF 0x0001 94 #define NFSCLDS_MDS 0x0002 95 #define NFSCLDS_DS 0x0004 96 #define NFSCLDS_CLOSED 0x0008 97 #define NFSCLDS_SAMECONN 0x0010 98 99 struct nfsclclient { 100 LIST_ENTRY(nfsclclient) nfsc_list; 101 struct nfsclownerhead nfsc_owner; 102 struct nfscldeleghead nfsc_deleg; 103 struct nfscldeleghash nfsc_deleghash[NFSCLDELEGHASHSIZE]; 104 struct nfscllayouthead nfsc_layout; 105 struct nfscllayouthash nfsc_layouthash[NFSCLLAYOUTHASHSIZE]; 106 struct nfscldevinfohead nfsc_devinfo; 107 struct nfsv4lock nfsc_lock; 108 struct proc *nfsc_renewthread; 109 struct nfsmount *nfsc_nmp; 110 time_t nfsc_expire; 111 u_int32_t nfsc_clientidrev; 112 u_int32_t nfsc_rev; 113 u_int32_t nfsc_renew; 114 u_int32_t nfsc_cbident; 115 u_int16_t nfsc_flags; 116 u_int16_t nfsc_idlen; 117 u_int8_t nfsc_id[1]; /* Malloc'd to correct length */ 118 }; 119 120 /* 121 * Bits for nfsc_flags. 122 */ 123 #define NFSCLFLAGS_INITED 0x0001 124 #define NFSCLFLAGS_HASCLIENTID 0x0002 125 #define NFSCLFLAGS_RECOVER 0x0004 126 #define NFSCLFLAGS_UMOUNT 0x0008 127 #define NFSCLFLAGS_HASTHREAD 0x0010 128 #define NFSCLFLAGS_AFINET6 0x0020 129 #define NFSCLFLAGS_EXPIREIT 0x0040 130 #define NFSCLFLAGS_FIRSTDELEG 0x0080 131 #define NFSCLFLAGS_GOTDELEG 0x0100 132 #define NFSCLFLAGS_RECVRINPROG 0x0200 133 134 struct nfsclowner { 135 LIST_ENTRY(nfsclowner) nfsow_list; 136 struct nfsclopenhead nfsow_open; 137 struct nfsclclient *nfsow_clp; 138 u_int32_t nfsow_seqid; 139 u_int32_t nfsow_defunct; 140 struct nfsv4lock nfsow_rwlock; 141 u_int8_t nfsow_owner[NFSV4CL_LOCKNAMELEN]; 142 }; 143 144 /* 145 * MALLOC'd to the correct length to accommodate the file handle. 146 */ 147 struct nfscldeleg { 148 TAILQ_ENTRY(nfscldeleg) nfsdl_list; 149 LIST_ENTRY(nfscldeleg) nfsdl_hash; 150 struct nfsclownerhead nfsdl_owner; /* locally issued state */ 151 struct nfscllockownerhead nfsdl_lock; 152 nfsv4stateid_t nfsdl_stateid; 153 struct acl_entry nfsdl_ace; /* Delegation ace */ 154 struct nfsclclient *nfsdl_clp; 155 struct nfsv4lock nfsdl_rwlock; /* for active I/O ops */ 156 struct nfscred nfsdl_cred; /* Cred. used for Open */ 157 time_t nfsdl_timestamp; /* used for stale cleanup */ 158 u_int64_t nfsdl_sizelimit; /* Limit for file growth */ 159 u_int64_t nfsdl_size; /* saved copy of file size */ 160 u_int64_t nfsdl_change; /* and change attribute */ 161 struct timespec nfsdl_modtime; /* local modify time */ 162 u_int16_t nfsdl_fhlen; 163 u_int8_t nfsdl_flags; 164 u_int8_t nfsdl_fh[1]; /* must be last */ 165 }; 166 167 /* 168 * nfsdl_flags bits. 169 */ 170 #define NFSCLDL_READ 0x01 171 #define NFSCLDL_WRITE 0x02 172 #define NFSCLDL_RECALL 0x04 173 #define NFSCLDL_NEEDRECLAIM 0x08 174 #define NFSCLDL_ZAPPED 0x10 175 #define NFSCLDL_MODTIMESET 0x20 176 #define NFSCLDL_DELEGRET 0x40 177 178 /* 179 * MALLOC'd to the correct length to accommodate the file handle. 180 */ 181 struct nfsclopen { 182 LIST_ENTRY(nfsclopen) nfso_list; 183 struct nfscllockownerhead nfso_lock; 184 nfsv4stateid_t nfso_stateid; 185 struct nfsclowner *nfso_own; 186 struct nfscred nfso_cred; /* Cred. used for Open */ 187 u_int32_t nfso_mode; 188 u_int32_t nfso_opencnt; 189 u_int16_t nfso_fhlen; 190 u_int8_t nfso_posixlock; /* 1 for POSIX type locking */ 191 u_int8_t nfso_fh[1]; /* must be last */ 192 }; 193 194 /* 195 * Return values for nfscl_open(). NFSCLOPEN_OK must == 0. 196 */ 197 #define NFSCLOPEN_OK 0 198 #define NFSCLOPEN_DOOPEN 1 199 #define NFSCLOPEN_DOOPENDOWNGRADE 2 200 #define NFSCLOPEN_SETCRED 3 201 202 struct nfscllockowner { 203 LIST_ENTRY(nfscllockowner) nfsl_list; 204 struct nfscllockhead nfsl_lock; 205 struct nfsclopen *nfsl_open; 206 NFSPROC_T *nfsl_inprog; 207 nfsv4stateid_t nfsl_stateid; 208 int nfsl_lockflags; 209 u_int32_t nfsl_seqid; 210 struct nfsv4lock nfsl_rwlock; 211 u_int8_t nfsl_owner[NFSV4CL_LOCKNAMELEN]; 212 u_int8_t nfsl_openowner[NFSV4CL_LOCKNAMELEN]; 213 }; 214 215 /* 216 * Byte range entry for the above lock owner. 217 */ 218 struct nfscllock { 219 LIST_ENTRY(nfscllock) nfslo_list; 220 u_int64_t nfslo_first; 221 u_int64_t nfslo_end; 222 short nfslo_type; 223 }; 224 225 /* This structure is used to collect a list of lockowners to free up. */ 226 struct nfscllockownerfh { 227 SLIST_ENTRY(nfscllockownerfh) nfslfh_list; 228 struct nfscllockownerhead nfslfh_lock; 229 int nfslfh_len; 230 uint8_t nfslfh_fh[NFSX_V4FHMAX]; 231 }; 232 233 /* 234 * MALLOC'd to the correct length to accommodate the file handle. 235 */ 236 struct nfscllayout { 237 TAILQ_ENTRY(nfscllayout) nfsly_list; 238 LIST_ENTRY(nfscllayout) nfsly_hash; 239 nfsv4stateid_t nfsly_stateid; 240 struct nfsv4lock nfsly_lock; 241 uint64_t nfsly_filesid[2]; 242 uint64_t nfsly_lastbyte; 243 struct nfsclflayouthead nfsly_flayread; 244 struct nfsclflayouthead nfsly_flayrw; 245 struct nfsclrecalllayouthead nfsly_recall; 246 time_t nfsly_timestamp; 247 struct nfsclclient *nfsly_clp; 248 uint16_t nfsly_flags; 249 uint16_t nfsly_fhlen; 250 uint8_t nfsly_fh[1]; 251 }; 252 253 /* 254 * Flags for nfsly_flags. 255 */ 256 #define NFSLY_FILES 0x0001 257 #define NFSLY_BLOCK 0x0002 258 #define NFSLY_OBJECT 0x0004 259 #define NFSLY_RECALL 0x0008 260 #define NFSLY_RECALLFILE 0x0010 261 #define NFSLY_RECALLFSID 0x0020 262 #define NFSLY_RECALLALL 0x0040 263 #define NFSLY_RETONCLOSE 0x0080 264 #define NFSLY_WRITTEN 0x0100 /* Has been used to write to a DS. */ 265 #define NFSLY_FLEXFILE 0x0200 266 267 /* 268 * Flex file layout mirror specific stuff for nfsclflayout. 269 */ 270 struct nfsffm { 271 nfsv4stateid_t st; 272 struct nfscldevinfo *devp; 273 char dev[NFSX_V4DEVICEID]; 274 uint32_t eff; 275 uid_t user; 276 gid_t group; 277 struct nfsfh *fh[NFSDEV_MAXVERS]; 278 uint16_t fhcnt; 279 }; 280 281 /* 282 * MALLOC'd to the correct length to accommodate the file handle list for File 283 * layout and the list of mirrors for the Flex File Layout. 284 * These hang off of nfsly_flayread and nfsly_flayrw, sorted in increasing 285 * offset order. 286 * The nfsly_flayread list holds the ones with iomode == NFSLAYOUTIOMODE_READ, 287 * whereas the nfsly_flayrw holds the ones with iomode == NFSLAYOUTIOMODE_RW. 288 */ 289 struct nfsclflayout { 290 LIST_ENTRY(nfsclflayout) nfsfl_list; 291 uint64_t nfsfl_off; 292 uint64_t nfsfl_end; 293 uint32_t nfsfl_iomode; 294 uint16_t nfsfl_flags; 295 union { 296 struct { 297 uint64_t patoff; 298 uint32_t util; 299 uint32_t stripe1; 300 uint8_t dev[NFSX_V4DEVICEID]; 301 uint16_t fhcnt; 302 struct nfscldevinfo *devp; 303 } fl; 304 struct { 305 uint64_t stripeunit; 306 uint32_t fflags; 307 uint32_t statshint; 308 uint16_t mirrorcnt; 309 } ff; 310 } nfsfl_un; 311 union { 312 struct nfsfh *fh[0]; /* FH list for DS File layout */ 313 struct nfsffm ffm[0]; /* Mirror list for Flex File */ 314 } nfsfl_un2; /* Must be last. Malloc'd to correct array length */ 315 }; 316 #define nfsfl_patoff nfsfl_un.fl.patoff 317 #define nfsfl_util nfsfl_un.fl.util 318 #define nfsfl_stripe1 nfsfl_un.fl.stripe1 319 #define nfsfl_dev nfsfl_un.fl.dev 320 #define nfsfl_fhcnt nfsfl_un.fl.fhcnt 321 #define nfsfl_devp nfsfl_un.fl.devp 322 #define nfsfl_stripeunit nfsfl_un.ff.stripeunit 323 #define nfsfl_fflags nfsfl_un.ff.fflags 324 #define nfsfl_statshint nfsfl_un.ff.statshint 325 #define nfsfl_mirrorcnt nfsfl_un.ff.mirrorcnt 326 #define nfsfl_fh nfsfl_un2.fh 327 #define nfsfl_ffm nfsfl_un2.ffm 328 329 /* 330 * Flags for nfsfl_flags. 331 */ 332 #define NFSFL_RECALL 0x0001 /* File layout has been recalled */ 333 #define NFSFL_FILE 0x0002 /* File layout */ 334 #define NFSFL_FLEXFILE 0x0004 /* Flex File layout */ 335 336 /* 337 * Structure that is used to store a LAYOUTRECALL. 338 */ 339 struct nfsclrecalllayout { 340 LIST_ENTRY(nfsclrecalllayout) nfsrecly_list; 341 uint64_t nfsrecly_off; 342 uint64_t nfsrecly_len; 343 int nfsrecly_recalltype; 344 uint32_t nfsrecly_iomode; 345 uint32_t nfsrecly_stateseqid; 346 uint32_t nfsrecly_stat; 347 uint32_t nfsrecly_op; 348 char nfsrecly_devid[NFSX_V4DEVICEID]; 349 }; 350 351 /* 352 * Stores the NFSv4.1 Device Info. Malloc'd to the correct length to 353 * store the list of network connections and list of indices. 354 * nfsdi_data[] is allocated the following way: 355 * - nfsdi_addrcnt * struct nfsclds 356 * - stripe indices, each stored as one byte, since there can be many 357 * of them. (This implies a limit of 256 on nfsdi_addrcnt, since the 358 * indices select which address.) 359 * For Flex File, the addrcnt is always one and no stripe indices exist. 360 */ 361 struct nfscldevinfo { 362 LIST_ENTRY(nfscldevinfo) nfsdi_list; 363 uint8_t nfsdi_deviceid[NFSX_V4DEVICEID]; 364 struct nfsclclient *nfsdi_clp; 365 uint32_t nfsdi_refcnt; 366 uint32_t nfsdi_layoutrefs; 367 union { 368 struct { 369 uint16_t stripecnt; 370 } fl; 371 struct { 372 int versindex; 373 uint32_t vers; 374 uint32_t minorvers; 375 uint32_t rsize; 376 uint32_t wsize; 377 } ff; 378 } nfsdi_un; 379 uint16_t nfsdi_addrcnt; 380 uint16_t nfsdi_flags; 381 struct nfsclds *nfsdi_data[0]; 382 }; 383 #define nfsdi_stripecnt nfsdi_un.fl.stripecnt 384 #define nfsdi_versindex nfsdi_un.ff.versindex 385 #define nfsdi_vers nfsdi_un.ff.vers 386 #define nfsdi_minorvers nfsdi_un.ff.minorvers 387 #define nfsdi_rsize nfsdi_un.ff.rsize 388 #define nfsdi_wsize nfsdi_un.ff.wsize 389 390 /* Flags for nfsdi_flags. */ 391 #define NFSDI_FILELAYOUT 0x0001 392 #define NFSDI_FLEXFILE 0x0002 393 #define NFSDI_TIGHTCOUPLED 0X0004 394 395 /* These inline functions return values from nfsdi_data[]. */ 396 /* 397 * Return a pointer to the address at "pos". 398 */ 399 static __inline struct nfsclds ** 400 nfsfldi_addr(struct nfscldevinfo *ndi, int pos) 401 { 402 403 if (pos >= ndi->nfsdi_addrcnt) 404 return (NULL); 405 return (&ndi->nfsdi_data[pos]); 406 } 407 408 /* 409 * Return the Nth ("pos") stripe index. 410 */ 411 static __inline int 412 nfsfldi_stripeindex(struct nfscldevinfo *ndi, int pos) 413 { 414 uint8_t *valp; 415 416 if (pos >= ndi->nfsdi_stripecnt) 417 return (-1); 418 valp = (uint8_t *)&ndi->nfsdi_data[ndi->nfsdi_addrcnt]; 419 valp += pos; 420 return ((int)*valp); 421 } 422 423 /* 424 * Set the Nth ("pos") stripe index to "val". 425 */ 426 static __inline void 427 nfsfldi_setstripeindex(struct nfscldevinfo *ndi, int pos, uint8_t val) 428 { 429 uint8_t *valp; 430 431 if (pos >= ndi->nfsdi_stripecnt) 432 return; 433 valp = (uint8_t *)&ndi->nfsdi_data[ndi->nfsdi_addrcnt]; 434 valp += pos; 435 *valp = val; 436 } 437 438 /* 439 * Macro for incrementing the seqid#. 440 */ 441 #define NFSCL_INCRSEQID(s, n) do { \ 442 if (((n)->nd_flag & ND_INCRSEQID)) \ 443 (s)++; \ 444 } while (0) 445 446 #endif /* _NFS_NFSCLSTATE_H_ */ 447