1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 /*
28 * Copyright 2018 Nexenta Systems, Inc.
29 * Copyright 2019 Nexenta by DDN, Inc.
30 * Copyright 2025 RackTop Systems, Inc.
31 */
32
33 #ifndef _NFS4_H
34 #define _NFS4_H
35
36 #include <sys/types.h>
37 #include <sys/vnode.h>
38 #include <sys/fem.h>
39 #include <rpc/rpc.h>
40 #include <nfs/nfs.h>
41
42 #ifdef _KERNEL
43 #include <nfs/nfs4_kprot.h>
44 #include <sys/nvpair.h>
45 #else
46 #include <rpcsvc/nfs4_prot.h>
47 #endif
48 #include <nfs/nfs4_attr.h>
49 #include <sys/acl.h>
50 #include <sys/list.h>
51 #include <nfs/nfs4x.h>
52
53 #ifdef __cplusplus
54 extern "C" {
55 #endif
56
57 #define NFS4_MAX_SECOID4 65536
58 #define NFS4_MAX_UTF8STRING 65536
59 #define NFS4_MAX_LINKTEXT4 65536
60 #define NFS4_MAX_PATHNAME4 65536
61
62 struct nfs_fsl_info {
63 uint_t netbuf_len;
64 uint_t netnm_len;
65 uint_t knconf_len;
66 char *netname;
67 struct netbuf *addr;
68 struct knetconfig *knconf;
69 };
70
71 #ifdef _KERNEL
72
73 typedef struct nfs4_fhandle {
74 int fh_len;
75 char fh_buf[NFS4_FHSIZE];
76 } nfs4_fhandle_t;
77
78 typedef uint8_t nfs4_minor_t;
79
80 #define NFS4_MINORVERSION 0
81 #define CB4_MINORVERSION 0
82
83 #define FIRST_NFS4_OP OP_ACCESS
84 #define LAST_NFS40_OP OP_RELEASE_LOCKOWNER
85 #define LAST_NFS41_OP OP_RECLAIM_COMPLETE
86 #define LAST_NFS42_OP OP_CLONE
87 #define LAST_NFS4_OP LAST_NFS42_OP
88
89 /*
90 * Set the fattr4_change variable using a time struct. Note that change
91 * is 64 bits, but timestruc_t is 128 bits in a 64-bit kernel.
92 */
93 #define NFS4_SET_FATTR4_CHANGE(change, ts) \
94 { \
95 change = (ts).tv_sec; \
96 change <<= 32; \
97 change |= (uint32_t)((ts).tv_nsec); \
98 }
99
100 /*
101 * Server lease period. Value is in seconds; Also used for grace period
102 */
103 extern time_t rfs4_lease_time;
104
105 /*
106 * This set of typedefs and interfaces represent the core or base set
107 * of functionality that backs the NFSv4 server's state related data
108 * structures. Since the NFSv4 server needs inter-RPC state to be
109 * available that is unrelated to the filesystem (in other words,
110 * soft-state), this functionality is needed to maintain that and is
111 * written to be somewhat flexible to adapt to the various types of
112 * data structures contained within the server.
113 *
114 * The basic structure at this level is that the server maintains a
115 * global "database" which consists of a set of tables. Each table
116 * contains a set of like data structures. Each table is indexed by
117 * at least one hash function and in most cases two hashes. Each
118 * table's characteristics is set when it is created at run-time via
119 * rfs4_table_create(). All table creation and related functions are
120 * located in nfs4_state.c. The generic database functionality is
121 * located in nfs4_db.c.
122 */
123
124 typedef struct rfs4_dbe rfs4_dbe_t; /* basic opaque db entry */
125 typedef struct rfs4_table rfs4_table_t; /* basic table type */
126 typedef struct rfs4_index rfs4_index_t; /* index */
127 typedef struct rfs4_database rfs4_database_t; /* and database */
128
129 typedef struct { /* opaque entry type for later use */
130 rfs4_dbe_t *dbe;
131 } *rfs4_entry_t;
132
133 /*
134 * NFSv4 server state databases
135 *
136 * Initialized when the module is loaded and used by NFSv4 state tables.
137 * These kmem_cache free pools are used globally, the NFSv4 state tables
138 * which make use of these kmem_cache free pools are per zone.
139 */
140 extern kmem_cache_t *rfs4_client_mem_cache;
141 extern kmem_cache_t *rfs4_clntIP_mem_cache;
142 extern kmem_cache_t *rfs4_openown_mem_cache;
143 extern kmem_cache_t *rfs4_openstID_mem_cache;
144 extern kmem_cache_t *rfs4_lockstID_mem_cache;
145 extern kmem_cache_t *rfs4_lockown_mem_cache;
146 extern kmem_cache_t *rfs4_file_mem_cache;
147 extern kmem_cache_t *rfs4_delegstID_mem_cache;
148 extern kmem_cache_t *rfs4_session_mem_cache;
149
150 /* database, table, index creation entry points */
151 extern rfs4_database_t *rfs4_database_create(uint32_t);
152 extern void rfs4_database_shutdown(rfs4_database_t *);
153 extern void rfs4_database_destroy(rfs4_database_t *);
154
155 extern void rfs4_database_destroy(rfs4_database_t *);
156
157 extern kmem_cache_t *nfs4_init_mem_cache(char *, uint32_t, uint32_t,
158 uint32_t);
159 extern rfs4_table_t *rfs4_table_create(rfs4_database_t *, char *,
160 time_t, uint32_t,
161 bool_t (*create)(rfs4_entry_t, void *),
162 void (*destroy)(rfs4_entry_t),
163 bool_t (*expiry)(rfs4_entry_t),
164 uint32_t, uint32_t, uint32_t, id_t);
165 extern void rfs4_table_destroy(rfs4_database_t *, rfs4_table_t *);
166 extern rfs4_index_t *rfs4_index_create(rfs4_table_t *, char *,
167 uint32_t (*hash)(void *),
168 bool_t (compare)(rfs4_entry_t, void *),
169 void *(*mkkey)(rfs4_entry_t), bool_t);
170 extern void rfs4_index_destroy(rfs4_index_t *);
171
172 /* Type used to direct rfs4_dbsearch() in what types of records to inspect */
173 typedef enum {RFS4_DBS_VALID, RFS4_DBS_INVALID} rfs4_dbsearch_type_t;
174 /* search and db entry manipulation entry points */
175 extern rfs4_entry_t rfs4_dbsearch(rfs4_index_t *, void *,
176 bool_t *, void *, rfs4_dbsearch_type_t);
177 extern void rfs4_dbe_lock(rfs4_dbe_t *);
178 extern void rfs4_dbe_unlock(rfs4_dbe_t *);
179 extern clock_t rfs4_dbe_twait(rfs4_dbe_t *, clock_t);
180 extern void rfs4_dbe_cv_broadcast(rfs4_dbe_t *);
181 extern void rfs4_dbe_hold(rfs4_dbe_t *);
182 extern void rfs4_dbe_hold_nolock(rfs4_dbe_t *);
183 extern void rfs4_dbe_rele_nolock(rfs4_dbe_t *);
184 extern void rfs4_dbe_rele(rfs4_dbe_t *);
185 extern uint32_t rfs4_dbe_refcnt(rfs4_dbe_t *);
186 extern id_t rfs4_dbe_getid(rfs4_dbe_t *);
187 extern void rfs4_dbe_invalidate(rfs4_dbe_t *);
188 extern bool_t rfs4_dbe_is_invalid(rfs4_dbe_t *);
189 extern time_t rfs4_dbe_get_timerele(rfs4_dbe_t *);
190 extern void rfs4_dbe_hide(rfs4_dbe_t *);
191 extern void rfs4_dbe_unhide(rfs4_dbe_t *);
192 #ifdef DEBUG
193 extern bool_t rfs4_dbe_islocked(rfs4_dbe_t *);
194 #endif
195 extern void rfs4_dbe_walk(rfs4_table_t *,
196 void (*callout)(rfs4_entry_t, void *), void *);
197 extern void rfs4_dbsearch_cb(rfs4_index_t *idx, void *key,
198 int maxcount, void (*callout)(rfs4_entry_t));
199
200 /*
201 * Minimal server stable storage.
202 *
203 * Currently the NFSv4 server will only save the client
204 * ID (the long version) so that it will be able to
205 * grant possible reclaim requests during the infamous
206 * grace_period.
207 */
208
209 #define RFS4_SS_DIRSIZE 64 * 1024
210 #define NFS4_SS_VERSION 1
211
212 /* handy pathname structure */
213 typedef struct ss_pn {
214 char *leaf;
215 char pn[MAXPATHLEN];
216 } rfs4_ss_pn_t;
217
218 /*
219 * The server will build this link list on startup. It represents the
220 * clients that have had valid state on the server in a prior instance.
221 *
222 */
223 typedef struct rfs4_oldstate {
224 struct rfs4_oldstate *next;
225 struct rfs4_oldstate *prev;
226 rfs4_ss_pn_t *ss_pn;
227 nfs_client_id4 cl_id4;
228 } rfs4_oldstate_t;
229
230 /*
231 * This union is used to overlay the server's internal treatment of
232 * the protocols stateid4 datatype. Therefore, "bits" must not exceed
233 * the size of stateid4 and more importantly should match the size of
234 * stateid4. The chgseq field must the first entry since it overlays
235 * stateid4.seqid.
236 */
237 typedef union {
238 stateid4 stateid;
239 struct {
240 uint32_t chgseq; /* State changes / protocol's seqid */
241 uint32_t boottime; /* boot time */
242 uint32_t type:2; /* stateid_type_t as define below */
243 uint32_t clnodeid:8; /* cluster server nodeid */
244 uint32_t ident:22; /* 2^22-1 openowner x fhs */
245 pid_t pid; /* pid of corresponding lock owner */
246 } bits;
247 } stateid_t;
248 /*
249 * Note that the way the type field above is defined, this enum must
250 * not have more than 4 members.
251 */
252 typedef enum {OPENID, LOCKID, DELEGID} stateid_type_t;
253
254 /*
255 * "wait" struct for use in the open open and lock owner state
256 * structures to provide serialization between server threads that are
257 * handling requests for the same open owner or lock stateid. This
258 * way only one thread will be updating things like sequence ids,
259 * replay cache and stateid at a time.
260 */
261 typedef struct rfs4_state_wait {
262 uint32_t sw_active;
263 uint32_t sw_wait_count;
264 kmutex_t sw_cv_lock[1];
265 kcondvar_t sw_cv[1];
266 } rfs4_state_wait_t;
267
268 extern void rfs4_sw_enter(rfs4_state_wait_t *);
269 extern void rfs4_sw_exit(rfs4_state_wait_t *);
270
271 /*
272 * This enum and the following rfs4_cbinfo_t struct are used to
273 * maintain information about the callback path used from the server
274 * to client for operations like CB_GETATTR and CB_RECALL. The
275 * rfs4_cbinfo_t struct is meant to be encompassed in the client
276 * struct and managed within that structure's locking scheme.
277 *
278 * The various states of the callback path are used by the server to
279 * determine if delegations should initially be provided to a client
280 * and then later on if connectivity has been lost and delegations
281 * should be revoked.
282 */
283
284 /*
285 * CB_NOCHANGE - Special value used for interfaces within the delegation
286 * code to signify that "no change" has occurred to the
287 * callback path
288 * CB_UNINIT - No callback info provided by the client
289 * CB_NONE - Callback info provided but CB_NULL call
290 * has yet to be attempted
291 * CB_OK - Callback path tested with CB_NULL with success
292 * CB_INPROG - Callback path currently being tested with CB_NULL
293 * CB_FAILED - Callback path was == CB_OK but has failed
294 * with timeout/rpc error
295 * CB_BAD - Callback info provided but CB_NULL failed
296 */
297 typedef enum {
298 CB_NOCHANGE = 0,
299 CB_UNINIT = 1,
300 CB_NONE = 2,
301 CB_OK = 3,
302 CB_INPROG = 4,
303 CB_FAILED = 5,
304 CB_BAD = 6
305 } rfs4_cbstate_t;
306
307 #define RFS4_CBCH_MAX 10 /* size callback client handle cache */
308 /*
309 * Callback info for a client.
310 * Client only provides: cb_client4 and cb_ident
311 * The rest of the information is used to track callback path status
312 * and usage.
313 *
314 * cb_state - used as comments for the rfs4_cbstate_t enum indicate
315 * cb_notified_of_cb_path_down - if the callback path was once CB_OK and
316 * has hence CB_FAILED, the client needs to be notified via RENEW.
317 * cb_timefailed - current time when cb_state transitioned from
318 * CB_OK -> CB_FAILED. Meant for observability. When did that happen?
319 * cb_chc_free/cb_chc - cache of client handles for the callback path
320 * cb_ident - SETCLIENTID provided callback_ident value
321 * callback - SETCLIENTID provided cb_client4 value
322 * cb_refcnt - current number of users of this structure's content
323 * protected by cb_lock
324 * cb_badbehavior - how many times did a client do something we didn't like?
325 * cb_lock - lock for contents of cbinfo
326 * cb_cv - used to allow threads to wait on CB_NULL completion
327 * cb_nullcaller - is there a thread currently taking care of
328 * new callback information?
329 * cb_cv_nullcaller - used by the thread doing CB_NULL to wait on
330 * threads that may be using client handles of the current
331 * client handle cache.
332 * newer - new callback info provided by a client and awaiting
333 * CB_NULL testing and move to regular cbinfo.
334 */
335 typedef struct {
336 rfs4_cbstate_t cb_state;
337 unsigned cb_notified_of_cb_path_down:1;
338 time_t cb_timefailed;
339 int cb_chc_free;
340 CLIENT *cb_chc[RFS4_CBCH_MAX];
341 uint32_t cb_ident;
342 cb_client4 cb_callback;
343 uint32_t cb_refcnt;
344 uint32_t cb_badbehavior;
345 kmutex_t cb_lock[1];
346 kcondvar_t cb_cv[1];
347 bool_t cb_nullcaller;
348 kcondvar_t cb_cv_nullcaller[1];
349 struct {
350 bool_t cb_new;
351 bool_t cb_confirmed;
352 uint32_t cb_ident;
353 cb_client4 cb_callback;
354 } cb_newer;
355 } rfs4_cbinfo_t;
356
357 /*
358 * A server instance. We can associate sets of clients - via a pointer in
359 * rfs4_client_t - with a given server instance, allowing us to treat clients
360 * in the set differently to clients in other sets.
361 *
362 * Currently used only for Sun Cluster HA-NFS support, to group clients
363 * on NFS resource failover so each set of clients gets its own dedicated
364 * grace period and distributed stable storage data.
365 */
366 typedef struct rfs4_servinst {
367 int dss_npaths;
368 krwlock_t rwlock;
369 krwlock_t oldstate_lock;
370 time_t start_time;
371 time_t grace_period;
372 uint_t nreclaim; /* number reclaim clients */
373 rfs4_oldstate_t *oldstate;
374 struct rfs4_dss_path **dss_paths;
375 struct rfs4_servinst *next;
376 struct rfs4_servinst *prev;
377 } rfs4_servinst_t;
378
379 /*
380 * DSS: distributed stable storage
381 */
382
383 typedef struct rfs4_dss_path {
384 struct rfs4_dss_path *next; /* for insque/remque */
385 struct rfs4_dss_path *prev; /* for insque/remque */
386 char *path;
387 struct rfs4_servinst *sip;
388 unsigned index; /* offset in servinst's array */
389 } rfs4_dss_path_t;
390
391 /* array of paths passed-in from nfsd command-line; stored in nvlist */
392 extern char **rfs4_dss_newpaths;
393 extern uint_t rfs4_dss_numnewpaths;
394
395 /* nvlists of all DSS paths: current, and before last warmstart */
396 extern nvlist_t *rfs4_dss_paths, *rfs4_dss_oldpaths;
397
398 /*
399 * The server maintains a set of state on a per client basis that
400 * matches that of the protocol requirements. A client's state is
401 * rooted with the rfs4_client_t struct of which there is one per
402 * client and is created when SETCLIENTID/SETCLIENTID_CONFIRM are
403 * received. From there, the server then creates rfs4_openowner_t
404 * structs for each new open owner from that client and are initiated
405 * at OPEN/OPEN_CONFIRM (when the open owner is new to the server).
406 * At OPEN, at least two other structures are created, and potentially a
407 * third. rfs4_state_t is created to track the association between an
408 * open owner and a particular file. An rfs4_file_t struct may be
409 * created (if the file is not already open) at OPEN as well. The
410 * rfs4_file_t struct is the only one that is per server and not per
411 * client. The rfs4_deleg_state_t struct is created in the
412 * instance that the server is going to provide a delegation for the
413 * file being OPENed. Finally, the rfs4_lockowner_t is created at the
414 * first use of a lock owner at the server and is a result of the LOCK
415 * operation. The rfs4_lo_state_t struct is then created to represent
416 * the relation between the lock owner and the file.
417 *
418 */
419 /*
420 * The following ascii art represents each of these data structs and
421 * their references to each other. Note: "<-(x)->" represents the
422 * doubly link lists (list_t).
423 *
424 * ____________________
425 * | |
426 * | rfs4_client_t |
427 * ->| (1) |<-
428 * / |____________________| \
429 * / ^ \
430 * / | \
431 * ____________________ ____________________ ____________________
432 * | | | | | |
433 * | rfs4_lockowner_t | | rfs4_openowner_t | | rfs4_deleg_state_t |
434 * | | | (3) <-(1)-> | | <-(2)-> |
435 * |____________________| |____________________| |____________________|
436 * ^ ^ |
437 * | | V
438 * ____________________ ____________________ ____________________
439 * | | | | | |
440 * | rfs4_lo_state_t |->| rfs4_state_t |->| rfs4_file_t |
441 * | <-(4)-> | | (4) <-(3)-> | | (2) |
442 * |____________________| |____________________| |____________________|
443 */
444 /*
445 * Each of these data types are kept in a separate rfs4_table_t and is
446 * actually encapsulated within a rfs4_dbe_t struct. The various
447 * tables and their construction is done in nfs4_state.c but
448 * documented here to completeness.
449 *
450 * Table Data struct stored Indexed by
451 * ----- ------------------ ----------
452 * rfs4_client_tab rfs4_client_t nfs_client_id4
453 * clientid4
454 *
455 * rfs4_openowner_tab rfs4_openowner_t open_owner4
456 *
457 * rfs4_state_tab rfs4_state_t open_owner4 | file
458 * stateid
459 *
460 * rfs4_lo_state_tab rfs4_lo_state_t lockowner | stateid
461 * lock_stateid
462 *
463 * rfs4_lockowner_tab rfs4_lockowner_t lockowner
464 * pid
465 *
466 * rfs4_file_tab rfs4_file_t filehandle
467 *
468 * rfs4_deleg_state_tab rfs4_deleg_state_t clientid4 | file
469 * deleg_stateid
470 */
471
472 /*
473 * The client struct, it is the root of all state for a particular
474 * client. The client is identified by the nfs_client_id4 via
475 * SETCLIENTID and the server returns the clientid4 as short hand reference
476 */
477 /*
478 * Client struct - as mentioned above it is the root of all state for
479 * a single client as identified by the client supplied nfs_client_id4
480 *
481 * dbe - encapsulation struct
482 * clientid - server assigned short hand reference to client
483 * nfs_client - client supplied identifier for itself
484 * confirm_verf - the value provided to the client for SETCLIENTID_CONFIRM
485 * need_confirm - does this client need to be SETCLIENTID_CONFIRMed?
486 *
487 * unlksys_completed - has an F_UNLKSYS been done for this client which
488 * says that the use of cleanlocks() on individual files
489 * is not required?
490 * can_reclaim - indicates if client is allowed to reclaim after server
491 * start-up (client had previous state at server)
492 * ss_remove - indicates that the rfs4_client_destroy function should
493 * clean up stable storage file.
494 * forced_expire - set if the sysadmin has used clear_locks for this client.
495 * no_referrals - set if the client is Solaris and pre-dates referrals
496 * deleg_revoked - how many delegations have been revoked for this client?
497 *
498 * cp_confirmed - this refers to a confirmed client struct that has
499 * the same nfs_client_id4 as this client struct. When/if this client
500 * struct is confirmed via SETCLINETID_CONFIRM, the previously
501 * confirmed client struct will be "closed" and hence this reference.
502 *
503 * last_access - used to determine if the client has let its lease expire
504 * cbinfo - struct containing all callback related information
505 * cr_set - credentials used for the SETCLIENTID/SETCLIENTID_CONFIRM pair
506 * sysid - the lock manager sysid allocated for this client's file locks
507 * openownerlist - root of openowners list associated with this client
508 * ss_pn - Pathname to the stable storage file.
509 * cl_addr - Clients network address.
510 * server_instance - pointer to the currently associated server instance
511 */
512 typedef struct rfs4_client {
513 rfs4_dbe_t *rc_dbe;
514 clientid4 rc_clientid;
515 nfs_client_id4 rc_nfs_client;
516 verifier4 rc_confirm_verf;
517 unsigned rc_need_confirm:1;
518 unsigned rc_unlksys_completed:1;
519 unsigned rc_can_reclaim:1;
520 unsigned rc_ss_remove:1;
521 unsigned rc_forced_expire:1;
522 unsigned rc_reclaim_completed:1;
523 uint_t rc_deleg_revoked;
524 struct rfs4_client *rc_cp_confirmed;
525 time_t rc_last_access;
526 rfs4_cbinfo_t rc_cbinfo;
527 cred_set_t rc_cr_set;
528 sysid_t rc_sysidt;
529 list_t rc_openownerlist;
530 rfs4_ss_pn_t *rc_ss_pn;
531 struct sockaddr_storage rc_addr;
532 rfs4_servinst_t *rc_server_instance;
533
534 /* nfsv4.1 */
535 rfs41_csr_t rc_contrived;
536 rfs41_sprot_t rc_state_prot;
537 list_t rc_sessions;
538 unsigned rc_destroying:1; /* flag: going to destroy */
539 } rfs4_client_t;
540
541 /*
542 * ClntIP struct - holds the diagnosis about whether the client
543 * cannot support referrals. Set to true for old Solaris clients.
544 */
545
546 typedef struct rfs4_clntip {
547 rfs4_dbe_t *ri_dbe;
548 struct sockaddr_storage ri_addr;
549 unsigned ri_no_referrals:1;
550 } rfs4_clntip_t;
551
552 /*
553 * The openowner contains the client supplied open_owner4 as well as
554 * the matching sequence id and is used to track the client's usage of
555 * the open_owner4. Note that a reply is saved here as well for
556 * processing of retransmissions.
557 *
558 * dbe - encapsulation struct
559 * client - reference to rfs4_client_t for this openowner
560 * owner - actual client supplied open_owner4
561 * need_confirm - does this openowner need to be OPEN_CONFIRMed
562 * postpone_confirm - set if error received on first use of open_owner
563 * state2confirm - what stateid4 should be used on the OPEN_CONFIRM
564 * open_seqid - what is the next open_seqid expected for this openowner
565 * oo_sw - used to serialize access to the open seqid/reply handling
566 * statelist - root of state struct list associated with this openowner
567 * node - node for client struct list of openowners
568 * reply_fh - open replay processing needs the filehandle so that it is
569 * able to reset the current filehandle for appropriate compound
570 * processing and reply.
571 * reply - last reply sent in relation to this openowner
572 */
573 typedef struct rfs4_openowner {
574 rfs4_dbe_t *ro_dbe;
575 rfs4_client_t *ro_client;
576 open_owner4 ro_owner;
577 unsigned ro_need_confirm:1;
578 unsigned ro_postpone_confirm:1;
579 seqid4 ro_open_seqid;
580 rfs4_state_wait_t ro_sw;
581 list_t ro_statelist;
582 list_node_t ro_node;
583 nfs_fh4 ro_reply_fh;
584 nfs_resop4 ro_reply;
585 } rfs4_openowner_t;
586
587 /*
588 * This state struct represents the association between an openowner
589 * and a file that has been OPENed by that openowner.
590 *
591 * dbe - encapsulation struct
592 * stateid - server provided stateid
593 * owner - reference back to the openowner for this state
594 * finfo - reference to the open file for this state
595 * open_access - how did the openowner OPEN the file (access)
596 * open_deny - how did the openowner OPEN the file (deny)
597 * share_access - what share reservation is on the file (access)
598 * share_deny - what share reservation is on the file (deny)
599 * closed - has this file been closed?
600 * lostatelist - root of list of lo_state associated with this state/file
601 * node - node for state struct list of states
602 */
603 typedef struct rfs4_state {
604 rfs4_dbe_t *rs_dbe;
605 stateid_t rs_stateid;
606 rfs4_openowner_t *rs_owner;
607 struct rfs4_file *rs_finfo;
608 uint32_t rs_open_access;
609 uint32_t rs_open_deny;
610 uint32_t rs_share_access;
611 uint32_t rs_share_deny;
612 unsigned rs_closed:1;
613 list_t rs_lostatelist;
614 list_node_t rs_node;
615 } rfs4_state_t;
616
617 /*
618 * Lockowner - track the lockowner and its related info
619 *
620 * dbe - encapsulation struct
621 * client - reference to the client
622 * owner - lockowner supplied by the client
623 * pid - local identifier used for file locking
624 */
625 typedef struct rfs4_lockowner {
626 rfs4_dbe_t *rl_dbe;
627 rfs4_client_t *rl_client;
628 lock_owner4 rl_owner;
629 pid_t rl_pid;
630 } rfs4_lockowner_t;
631
632 /*
633 * Lockowner_state associated with a state struct and lockowner
634 *
635 * dbe - encapsulation struct
636 * state - reference back to state struct for open file
637 * lockid - stateid for this lockowner/state
638 * locker - reference to lockowner
639 * seqid - sequence id for this lockowner/state
640 * skip_seqid_check - used on initialization of struct
641 * locks_cleaned - have all locks been released for this lockowner/file?
642 * lock_completed - successful LOCK with lockowner/file?
643 * ls_sw - used to serialize update seqid/reply/stateid handling
644 * node - node for state struct list of lo_states
645 * reply - last reply sent in relation to this lockowner/state
646 */
647 typedef struct rfs4_lo_state {
648 rfs4_dbe_t *rls_dbe;
649 rfs4_state_t *rls_state;
650 stateid_t rls_lockid;
651 rfs4_lockowner_t *rls_locker;
652 seqid4 rls_seqid;
653 unsigned rls_skip_seqid_check:1;
654 unsigned rls_locks_cleaned:1;
655 unsigned rls_lock_completed:1;
656 rfs4_state_wait_t rls_sw;
657 list_node_t rls_node;
658 nfs_resop4 rls_reply;
659 } rfs4_lo_state_t;
660
661 /*
662 * Delegation state - per client
663 *
664 * dbe - encapsulation struct
665 * dtype - type of delegation (NONE, READ, WRITE)
666 * delegid - stateid for this delegation
667 * time_granted - time this delegation was assigned to client
668 * time_recalled - time when the server started recall process
669 * time_revoked - if revoked, time that the revoke occurred
670 * finfo - reference to the file associated with this delegation
671 * client - reference to client for which this delegation is associated
672 * node - list of delegations for the file (WRITE == 1, READ == )
673 */
674 typedef struct rfs4_deleg_state {
675 rfs4_dbe_t *rds_dbe;
676 open_delegation_type4 rds_dtype;
677 stateid_t rds_delegid;
678 time_t rds_time_granted;
679 time_t rds_time_recalled;
680 time_t rds_time_revoked;
681 struct rfs4_file *rds_finfo;
682 rfs4_client_t *rds_client;
683 list_node_t rds_node;
684 } rfs4_deleg_state_t;
685
686 /*
687 * Delegation info associated with the file
688 *
689 * dtype - type of delegation for file (NONE, READ, WRITE)
690 * time_returned - time that last delegation was returned for file
691 * time_recalled - time that recall sequence started
692 * time_lastgrant - time that last delegation was provided to a client
693 * time_lastwrite - time of last write to use the delegation stateid
694 * time_rm_delayed - time of last remove/rename which was DELAYed
695 * rdgrants - how many read delegations have been provided for this file
696 * wrgrants - how many write delegations provided (can only be one)
697 * recall_count - how many recall threads are outstanding
698 * recall_lock - lock to protect contents of this struct
699 * recall_cv - condition var for the "parent" thread to wait upon
700 * deleg_change_grant - value for change attribute at time of write grant
701 * deleg_change - most recent value of change obtained from client
702 * deleg_change_ts - time of last deleg_change update
703 * ever_recalled - has this particular delegation ever been recalled?
704 * dont_grant - file deletion is impending, don't grant a delegation
705 * conflicted_client - clientid of the client that caused a CB_RECALL
706 * to occur. This is used for delegation policy (should a delegation
707 * be granted shortly after it has been returned?)
708 */
709 typedef struct rfs4_dinfo {
710 open_delegation_type4 rd_dtype;
711 time_t rd_time_returned;
712 time_t rd_time_recalled;
713 time_t rd_time_lastgrant;
714 time_t rd_time_lastwrite;
715 time_t rd_time_rm_delayed;
716 uint32_t rd_rdgrants;
717 uint32_t rd_wrgrants;
718 int32_t rd_recall_count;
719 kmutex_t rd_recall_lock[1];
720 kcondvar_t rd_recall_cv[1];
721 bool_t rd_ever_recalled;
722 uint32_t rd_hold_grant;
723 clientid4 rd_conflicted_client;
724 } rfs4_dinfo_t;
725
726 /*
727 * File
728 *
729 * dbe - encapsulation struct
730 * vp - vnode for the file that is open or has a delegation
731 * filehandle - the filehandle generated by the server for this file
732 * delegstatelist - root of delegation list for this file
733 * dinfo - see struct definition above
734 * share_deny - union of all deny modes on file
735 * share_access - union of all access modes on file
736 * access_read - count of read access
737 * access_write - count of write access
738 * deny_read - count of deny reads
739 * deny_write - count of deny writes
740 * file_rwlock - lock for serializing the removal of a file while
741 * the state structures are active within the server
742 *
743 * The only requirement for locking file_rwlock is that the
744 * caller have a reference to the containing rfs4_file. The dbe
745 * lock may or may not be held for lock/unlock of file_rwlock.
746 * As mentioned above, the file_rwlock is used for serialization
747 * of file removal and more specifically reference to the held
748 * vnode (e.g. vp).
749 */
750 typedef struct rfs4_file {
751 rfs4_dbe_t *rf_dbe;
752 vnode_t *rf_vp;
753 nfs_fh4 rf_filehandle;
754 list_t rf_delegstatelist;
755 rfs4_dinfo_t rf_dinfo;
756 uint32_t rf_share_deny;
757 uint32_t rf_share_access;
758 uint32_t rf_access_read;
759 uint32_t rf_access_write;
760 uint32_t rf_deny_read;
761 uint32_t rf_deny_write;
762 krwlock_t rf_file_rwlock;
763 } rfs4_file_t;
764
765 /*
766 * nfs4_deleg_policy is used to signify the server's global delegation
767 * policy. The default is to NEVER delegate files and the
768 * administrator must configure the server to enable delegations.
769 *
770 * The disable/enable delegation functions are used to eliminate a
771 * race with exclusive creates.
772 */
773 typedef enum {
774 SRV_NEVER_DELEGATE = 0,
775 SRV_NORMAL_DELEGATE = 1
776 } srv_deleg_policy_t;
777
778 extern void rfs4_disable_delegation(void), rfs4_enable_delegation(void);
779
780 /*
781 * Request types for delegation. These correspond with
782 * open_delegation_type4 with the addition of a new value, DELEG_ANY,
783 * to reqequest any delegation.
784 */
785 typedef enum {
786 DELEG_NONE = 0, /* Corresponds to OPEN_DELEG_NONE */
787 DELEG_READ = 1, /* Corresponds to OPEN_DELEG_READ */
788 DELEG_WRITE = 2, /* Corresponds to OPEN_DELEG_WRITE */
789 DELEG_ANY = -1 /* New value to request any delegation type */
790 } delegreq_t;
791
792 #define NFS4_DELEG4TYPE2REQTYPE(x) (delegreq_t)(x)
793
794 /*
795 * Zone global variables of NFSv4 server
796 */
797 typedef struct nfs4_srv {
798 /* Unique write verifier */
799 verifier4 write4verf;
800 /* Delegation lock */
801 kmutex_t deleg_lock;
802 /* Used to serialize create/destroy of nfs4_server_state database */
803 kmutex_t state_lock;
804 rfs4_database_t *nfs4_server_state;
805 /* Used to manage access to server instance linked list */
806 kmutex_t servinst_lock;
807 rfs4_servinst_t *nfs4_cur_servinst;
808 /* Used to manage access to nfs4_deleg_policy */
809 krwlock_t deleg_policy_lock;
810 srv_deleg_policy_t nfs4_deleg_policy;
811 /* Allowed minors, nfs4 minors start from 0, which is always allowed */
812 nfs4_minor_t nfs4_minor_max;
813 /* Set first time we see one */
814 int seen_first_compound;
815 /*
816 * Circular double-linked list of paths for currently-served RGs.
817 * No locking required -- only changed on server start.
818 * Managed with insque/remque.
819 */
820 rfs4_dss_path_t *dss_pathlist;
821 /* Duplicate request cache */
822 struct rfs4_drc *nfs4_drc;
823 /* nfsv4 server start time */
824 time_t rfs4_start_time;
825 /* Used to serialize lookups of clientids */
826 krwlock_t rfs4_findclient_lock;
827
828 /* NFSv4 server state client tables */
829 /* table expiry times */
830 time_t rfs4_client_cache_time;
831 time_t rfs4_openowner_cache_time;
832 time_t rfs4_state_cache_time;
833 time_t rfs4_lo_state_cache_time;
834 time_t rfs4_lockowner_cache_time;
835 time_t rfs4_file_cache_time;
836 time_t rfs4_deleg_state_cache_time;
837 time_t rfs4_clntip_cache_time;
838 /* tables and indexes */
839 /* client table */
840 rfs4_table_t *rfs4_client_tab;
841 rfs4_index_t *rfs4_clientid_idx;
842 rfs4_index_t *rfs4_nfsclnt_idx;
843 /* client IP table */
844 rfs4_table_t *rfs4_clntip_tab;
845 rfs4_index_t *rfs4_clntip_idx;
846 /* Open Owner table */
847 rfs4_table_t *rfs4_openowner_tab;
848 rfs4_index_t *rfs4_openowner_idx;
849 /* Open State ID table */
850 rfs4_table_t *rfs4_state_tab;
851 rfs4_index_t *rfs4_state_idx;
852 rfs4_index_t *rfs4_state_owner_file_idx;
853 rfs4_index_t *rfs4_state_file_idx;
854 /* Lock State ID table */
855 rfs4_table_t *rfs4_lo_state_tab;
856 rfs4_index_t *rfs4_lo_state_idx;
857 rfs4_index_t *rfs4_lo_state_owner_idx;
858 /* Lock owner table */
859 rfs4_table_t *rfs4_lockowner_tab;
860 rfs4_index_t *rfs4_lockowner_idx;
861 rfs4_index_t *rfs4_lockowner_pid_idx;
862 /* File table */
863 rfs4_table_t *rfs4_file_tab;
864 rfs4_index_t *rfs4_file_idx;
865 /* Deleg State table */
866 rfs4_table_t *rfs4_deleg_state_tab;
867 rfs4_index_t *rfs4_deleg_idx;
868 rfs4_index_t *rfs4_deleg_state_idx;
869
870 /* nfs4.x */
871 rfs4_table_t *rfs4_session_tab;
872 rfs4_index_t *rfs4_session_idx;
873
874 /* client stable storage */
875 int rfs4_ss_enabled;
876 } nfs4_srv_t;
877
878 /*
879 * max length of the NFSv4 server database name
880 */
881 #define RFS4_MAX_MEM_CACHE_NAME 48
882
883 /*
884 * global NFSv4 server kmem caches
885 * r_db_name - The name of the state database and the table that will use it
886 * These tables are defined in nfs4_srv_t
887 * r_db_mem_cache - The kmem cache associated with the state database name
888 */
889 typedef struct rfs4_db_mem_cache {
890 char r_db_name[RFS4_MAX_MEM_CACHE_NAME];
891 kmem_cache_t *r_db_mem_cache;
892 } rfs4_db_mem_cache_t;
893
894 #define RFS4_DB_MEM_CACHE_NUM 9
895
896 extern rfs4_db_mem_cache_t rfs4_db_mem_cache_table[RFS4_DB_MEM_CACHE_NUM];
897
898 extern srv_deleg_policy_t nfs4_get_deleg_policy();
899
900 extern void rfs4_servinst_create(nfs4_srv_t *, int, int, char **);
901 extern void rfs4_servinst_destroy_all(nfs4_srv_t *);
902 extern void rfs4_servinst_assign(nfs4_srv_t *, rfs4_client_t *,
903 rfs4_servinst_t *);
904 extern rfs4_servinst_t *rfs4_servinst(rfs4_client_t *);
905 extern int rfs4_clnt_in_grace(rfs4_client_t *);
906 extern int rfs4_servinst_in_grace(rfs4_servinst_t *);
907 extern int rfs4_servinst_grace_new(rfs4_servinst_t *);
908 extern void rfs4_grace_start(rfs4_servinst_t *);
909 extern void rfs4_grace_start_new(nfs4_srv_t *);
910 extern void rfs4_grace_reset_all(nfs4_srv_t *);
911 extern void rfs4_dss_readstate(nfs4_srv_t *, int, char **);
912
913 /*
914 * Various interfaces to manipulate the state structures introduced
915 * above
916 */
917 extern void rfs4_free_reply(nfs_resop4 *);
918 extern void rfs4_copy_reply(nfs_resop4 *, nfs_resop4 *);
919
920 /* rfs4_client_t handling */
921 extern rfs4_client_t *rfs4_findclient(nfs_client_id4 *,
922 bool_t *, rfs4_client_t *);
923 extern rfs4_client_t *rfs4_findclient_by_id(clientid4, bool_t);
924 extern rfs4_client_t *rfs4_findclient_by_addr(struct sockaddr *);
925 extern void rfs4_client_rele(rfs4_client_t *);
926 extern void rfs4_client_close(rfs4_client_t *);
927 extern void rfs4_client_state_remove(rfs4_client_t *);
928 extern void rfs4_client_scv_next(rfs4_client_t *);
929 extern void rfs4_update_lease(rfs4_client_t *);
930 extern bool_t rfs4_lease_expired(rfs4_client_t *);
931 extern nfsstat4 rfs4_check_clientid(clientid4 *, int);
932
933 /* rfs4_clntip_t handling */
934 extern rfs4_clntip_t *rfs4_find_clntip(struct sockaddr *, bool_t *);
935 extern void rfs4_invalidate_clntip(struct sockaddr *);
936
937 /* rfs4_openowner_t handling */
938 extern rfs4_openowner_t *rfs4_findopenowner(open_owner4 *, bool_t *, seqid4);
939 extern void rfs4_update_open_sequence(rfs4_openowner_t *);
940 extern void rfs4_update_open_resp(rfs4_openowner_t *,
941 nfs_resop4 *, nfs_fh4 *);
942 extern void rfs4_openowner_rele(rfs4_openowner_t *);
943 extern void rfs4_free_opens(rfs4_openowner_t *, bool_t, bool_t);
944
945 /* rfs4_lockowner_t handling */
946 extern rfs4_lockowner_t *rfs4_findlockowner(lock_owner4 *, bool_t *);
947 extern rfs4_lockowner_t *rfs4_findlockowner_by_pid(pid_t);
948 extern void rfs4_lockowner_rele(rfs4_lockowner_t *);
949
950 /* rfs4_state_t handling */
951 extern rfs4_state_t *rfs4_findstate_by_owner_file(rfs4_openowner_t *,
952 rfs4_file_t *, bool_t *);
953 extern void rfs4_state_rele(rfs4_state_t *);
954 extern void rfs4_state_close(rfs4_state_t *, bool_t,
955 bool_t, cred_t *);
956 extern void rfs4_release_share_lock_state(rfs4_state_t *,
957 cred_t *, bool_t);
958 extern void rfs4_close_all_state(rfs4_file_t *);
959
960 /* rfs4_lo_state_t handling */
961 extern rfs4_lo_state_t *rfs4_findlo_state_by_owner(rfs4_lockowner_t *,
962 rfs4_state_t *, bool_t *);
963 extern void rfs4_lo_state_rele(rfs4_lo_state_t *, bool_t);
964 extern void rfs4_update_lock_sequence(rfs4_lo_state_t *);
965 extern void rfs4_update_lock_resp(rfs4_lo_state_t *,
966 nfs_resop4 *);
967
968 /* rfs4_file_t handling */
969 extern rfs4_file_t *rfs4_findfile(vnode_t *, nfs_fh4 *, bool_t *);
970 extern rfs4_file_t *rfs4_findfile_withlock(vnode_t *, nfs_fh4 *,
971 bool_t *);
972 extern void rfs4_file_rele(rfs4_file_t *);
973
974 /* General collection of "get state" functions */
975 extern nfsstat4 rfs4_get_state(stateid4 *, rfs4_state_t **,
976 rfs4_dbsearch_type_t);
977 extern nfsstat4 rfs4_get_state_nolock(stateid4 *, rfs4_state_t **,
978 rfs4_dbsearch_type_t);
979 extern void rfs4_state_rele_nounlock(rfs4_state_t *);
980
981 extern nfsstat4 rfs4_get_deleg_state(stateid4 *,
982 rfs4_deleg_state_t **);
983 extern nfsstat4 rfs4_get_lo_state(stateid4 *, rfs4_lo_state_t **,
984 bool_t);
985 struct compound_state;
986 extern nfsstat4 rfs4_check_stateid(int, vnode_t *, stateid4 *,
987 bool_t, bool_t *, bool_t,
988 caller_context_t *,
989 struct compound_state *);
990 extern int rfs4_check_stateid_seqid(rfs4_state_t *, stateid4 *,
991 const struct compound_state *);
992 extern int rfs4_check_lo_stateid_seqid(rfs4_lo_state_t *,
993 stateid4 *,
994 const struct compound_state *);
995
996 /* return values for rfs4_check_stateid_seqid() */
997 #define NFS4_CHECK_STATEID_OKAY 1
998 #define NFS4_CHECK_STATEID_OLD 2
999 #define NFS4_CHECK_STATEID_BAD 3
1000 #define NFS4_CHECK_STATEID_EXPIRED 4
1001 #define NFS4_CHECK_STATEID_REPLAY 5
1002 #define NFS4_CHECK_STATEID_CLOSED 6
1003 #define NFS4_CHECK_STATEID_UNCONFIRMED 7
1004
1005 /* delay() time that server is willing to briefly wait for a delegreturn */
1006 #define NFS4_DELEGATION_CONFLICT_DELAY (hz/10)
1007
1008 /*
1009 * Interfaces for handling of callback's client handle cache and
1010 * callback interfaces themselves.
1011 */
1012 extern void rfs4_cbinfo_free(rfs4_cbinfo_t *);
1013 extern void rfs4_client_setcb(rfs4_client_t *, cb_client4 *,
1014 uint32_t);
1015 extern void rfs4_deleg_cb_check(rfs4_client_t *);
1016 extern nfsstat4 rfs4_vop_getattr(vnode_t *, vattr_t *, int, cred_t *);
1017
1018 /* rfs4_deleg_state_t handling and other delegation interfaces */
1019 extern rfs4_deleg_state_t *rfs4_finddeleg(rfs4_state_t *, bool_t *);
1020 extern rfs4_deleg_state_t *rfs4_finddelegstate(stateid_t *);
1021 extern bool_t rfs4_check_recall(rfs4_state_t *, uint32_t);
1022 extern void rfs4_recall_deleg(rfs4_file_t *,
1023 bool_t, rfs4_client_t *);
1024 extern int rfs4_get_deleg(rfs4_state_t *, open_delegation_type4,
1025 open_delegation_type4 (*policy)(rfs4_state_t *,
1026 open_delegation_type4 dtype));
1027 extern rfs4_deleg_state_t *rfs4_grant_delegation(delegreq_t, rfs4_state_t *,
1028 int *);
1029 extern void rfs4_set_deleg_response(rfs4_deleg_state_t *,
1030 open_delegation4 *, nfsace4 *, int);
1031 extern void rfs4_return_deleg(rfs4_deleg_state_t *, bool_t);
1032 extern bool_t rfs4_is_deleg(rfs4_state_t *);
1033 extern void rfs4_deleg_state_rele(rfs4_deleg_state_t *);
1034 extern bool_t rfs4_check_delegated_byfp(int, rfs4_file_t *,
1035 bool_t, bool_t, bool_t, clientid4 *);
1036 extern void rfs4_clear_dont_grant(rfs4_file_t *);
1037
1038 /*
1039 * nfs4 monitored operations.
1040 */
1041 extern int deleg_rd_open(femarg_t *, int, cred_t *, caller_context_t *);
1042 extern int deleg_wr_open(femarg_t *, int, cred_t *, caller_context_t *);
1043 extern int deleg_wr_read(femarg_t *, uio_t *, int, cred_t *,
1044 caller_context_t *);
1045 extern int deleg_rd_write(femarg_t *, uio_t *, int, cred_t *,
1046 caller_context_t *);
1047 extern int deleg_wr_write(femarg_t *, uio_t *, int, cred_t *,
1048 caller_context_t *);
1049 extern int deleg_rd_setattr(femarg_t *, vattr_t *, int, cred_t *,
1050 caller_context_t *);
1051 extern int deleg_wr_setattr(femarg_t *, vattr_t *, int, cred_t *,
1052 caller_context_t *);
1053 extern int deleg_rd_rwlock(femarg_t *, int, caller_context_t *);
1054 extern int deleg_wr_rwlock(femarg_t *, int, caller_context_t *);
1055 extern int deleg_rd_space(femarg_t *, int, flock64_t *, int, offset_t, cred_t *,
1056 caller_context_t *);
1057 extern int deleg_wr_space(femarg_t *, int, flock64_t *, int, offset_t, cred_t *,
1058 caller_context_t *);
1059 extern int deleg_rd_setsecattr(femarg_t *, vsecattr_t *, int, cred_t *,
1060 caller_context_t *);
1061 extern int deleg_wr_setsecattr(femarg_t *, vsecattr_t *, int, cred_t *,
1062 caller_context_t *);
1063 extern int deleg_rd_vnevent(femarg_t *, vnevent_t, vnode_t *, char *,
1064 caller_context_t *);
1065 extern int deleg_wr_vnevent(femarg_t *, vnevent_t, vnode_t *, char *,
1066 caller_context_t *);
1067
1068 extern void rfs4_mon_hold(void *);
1069 extern void rfs4_mon_rele(void *);
1070
1071 extern fem_t *deleg_rdops;
1072 extern fem_t *deleg_wrops;
1073
1074 extern int rfs4_share(rfs4_state_t *, uint32_t, uint32_t);
1075 extern int rfs4_unshare(rfs4_state_t *);
1076 extern void rfs4_set_deleg_policy(nfs4_srv_t *, srv_deleg_policy_t);
1077 extern void rfs4_hold_deleg_policy(nfs4_srv_t *);
1078 extern void rfs4_rele_deleg_policy(nfs4_srv_t *);
1079
1080 #ifdef DEBUG
1081 #define NFS4_DEBUG(var, args) if (var) cmn_err args
1082
1083 extern int rfs4_debug;
1084 extern int nfs4_client_attr_debug;
1085 extern int nfs4_client_state_debug;
1086 extern int nfs4_client_shadow_debug;
1087 extern int nfs4_client_lock_debug;
1088 extern int nfs4_client_lease_debug;
1089 extern int nfs4_seqid_sync;
1090 extern int nfs4_client_map_debug;
1091 extern int nfs4_client_inactive_debug;
1092 extern int nfs4_client_recov_debug;
1093 extern int nfs4_client_failover_debug;
1094 extern int nfs4_client_call_debug;
1095 extern int nfs4_client_foo_debug;
1096 extern int nfs4_client_zone_debug;
1097 extern int nfs4_lost_rqst_debug;
1098 extern int nfs4_open_stream_debug;
1099 extern int nfs4_client_open_dg;
1100 extern int nfs4_srvmnt_debug;
1101 extern int nfs4_utf8_debug;
1102
1103 void rfs4_dbe_debug(rfs4_dbe_t *e);
1104
1105 #ifdef NFS4_DEBUG_MUTEX
1106 void nfs4_debug_mutex_enter(kmutex_t *, char *, int);
1107 void nfs4_debug_mutex_exit(kmutex_t *, char *, int);
1108
1109 #define mutex_enter(m) nfs4_debug_mutex_enter((m), __FILE__, __LINE__)
1110 #define mutex_exit(m) nfs4_debug_mutex_exit((m), __FILE__, __LINE__)
1111 #endif /* NFS4_DEBUG_MUTEX */
1112
1113 #else /* ! DEBUG */
1114 #define NFS4_DEBUG(var, args)
1115 #endif /* DEBUG */
1116
1117 /*
1118 * XXX - temporary for testing of volatile fh
1119 */
1120
1121 #ifdef VOLATILE_FH_TEST
1122
1123 struct nfs_fh4_fmt {
1124 fhandle4_t fh4_i;
1125 uint32_t fh4_flag;
1126 uint32_t fh4_volatile_id;
1127 };
1128
1129 #else /* VOLATILE_FH_TEST */
1130
1131 struct nfs_fh4_fmt {
1132 fhandle4_t fh4_i;
1133 uint32_t fh4_flag;
1134 };
1135
1136 #endif /* VOLATILE_FH_TEST */
1137
1138 #define FH4_NAMEDATTR 1
1139 #define FH4_ATTRDIR 2
1140
1141 #define fh4_fsid fh4_i.fhx_fsid
1142 #define fh4_len fh4_i.fhx_len /* fid length */
1143 #define fh4_data fh4_i.fhx_data /* fid bytes */
1144 #define fh4_xlen fh4_i.fhx_xlen
1145 #define fh4_xdata fh4_i.fhx_xdata
1146 typedef struct nfs_fh4_fmt nfs_fh4_fmt_t;
1147
1148 #define fh4_to_fmt4(fh4p) ((nfs_fh4_fmt_t *)(fh4p)->nfs_fh4_val)
1149 #define get_fh4_flag(fh4p, flag) ((fh4_to_fmt4(fh4p)->fh4_flag) & (flag))
1150 #define set_fh4_flag(fh4p, flag) ((fh4_to_fmt4(fh4p)->fh4_flag) |= (flag))
1151 #define clr_fh4_flag(fh4p, flag) ((fh4_to_fmt4(fh4p)->fh4_flag) &= ~(flag))
1152
1153 #define NFS_FH4_LEN sizeof (nfs_fh4_fmt_t)
1154
1155 /*
1156 * Copy fields from external (fhandle_t) to in-memory (nfs_fh4_fmt_t)
1157 * format to support export info checking. It does not copy over
1158 * the complete filehandle, just the fsid, xlen and xdata. It may
1159 * need to be changed to be used in other places.
1160 *
1161 * NOTE: The macro expects the space to be pre-allocated for
1162 * the contents of nfs_fh4_fmt_t.
1163 */
1164 #define FH_TO_FMT4(exifh, nfs_fmt) { \
1165 bzero((nfs_fmt), NFS_FH4_LEN); \
1166 (nfs_fmt)->fh4_fsid = (exifh)->fh_fsid; \
1167 (nfs_fmt)->fh4_xlen = (exifh)->fh_xlen; \
1168 bcopy((exifh)->fh_xdata, (nfs_fmt)->fh4_xdata, \
1169 (exifh)->fh_xlen); \
1170 }
1171
1172 /*
1173 * A few definitions of repeatedly used constructs for nfsv4
1174 */
1175 #define UTF8STRING_FREE(str) { \
1176 kmem_free((str).utf8string_val, (str).utf8string_len); \
1177 (str).utf8string_val = NULL; \
1178 (str).utf8string_len = 0; \
1179 }
1180
1181 /*
1182 * NFS4_VOLATILE_FH yields non-zero if the filesystem uses non-persistent
1183 * filehandles.
1184 */
1185 #define NFS4_VOLATILE_FH(mi) \
1186 ((mi)->mi_fh_expire_type & \
1187 (FH4_VOLATILE_ANY | FH4_VOL_MIGRATION | FH4_VOL_RENAME))
1188
1189 /*
1190 * NFS_IS_DOTNAME checks if the name given represents a dot or dotdot entry
1191 */
1192 #define NFS_IS_DOTNAME(name) \
1193 (((name)[0] == '.') && \
1194 (((name)[1] == '\0') || (((name)[1] == '.') && ((name)[2] == '\0'))))
1195
1196 /*
1197 * Define the number of bits in a bitmap word (uint32)
1198 */
1199 #define NFS4_BITMAP4_BITSPERWORD (sizeof (uint32_t) * 8)
1200
1201 /*
1202 * Define the value for the access field of the compound_state structure
1203 * based on the result of nfsauth access checking.
1204 */
1205 #define CS_ACCESS_OK 0x1
1206 #define CS_ACCESS_DENIED 0x2
1207 #define CS_ACCESS_LIMITED 0x4
1208
1209 /*
1210 * compound state in nfsv4 server
1211 */
1212 struct compound_state {
1213 struct exportinfo *exi;
1214 struct exportinfo *saved_exi; /* export struct for saved_vp */
1215 cred_t *basecr; /* UNIX cred: only RPC request */
1216 caddr_t principal;
1217 int nfsflavor;
1218 cred_t *cr; /* UNIX cred: RPC request and */
1219 /* target export */
1220 bool_t cont;
1221 uint_t access; /* access perm on vp per request */
1222 bool_t deleg; /* TRUE if current fh has */
1223 /* write delegated */
1224 vnode_t *vp; /* modified by PUTFH, and by ops that */
1225 /* input to GETFH */
1226 bool_t mandlock; /* Is mandatory locking in effect */
1227 /* for vp */
1228 vnode_t *saved_vp; /* modified by SAVEFH, copied to */
1229 /* vp by RESTOREFH */
1230 nfsstat4 *statusp;
1231 nfs_fh4 fh; /* ditto. valid only if vp != NULL */
1232 nfs_fh4 saved_fh; /* ditto. valid only if */
1233 /* saved_vp != NULL */
1234 struct svc_req *req;
1235 char fhbuf[NFS4_FHSIZE];
1236
1237 /* NFSv4.1 */
1238 uint8_t minorversion; /* NFS4 minor version */
1239 rfs4_session_t *sp; /* OP_SEQUENCE set it */
1240 slotid4 slotno;
1241 rfs4_slot_t *slot;
1242 rfs4_client_t *client;
1243 uint16_t op_pos;
1244 uint16_t op_len; /* number operations in compound req */
1245 #define RFS4_DISPATCH_DONE (1 << 0)
1246 #define RFS4_CURRENT_STATEID (1 << 1)
1247 #define RFS4_SAVED_STATEID (1 << 2)
1248 uint8_t cs_flags;
1249 bool_t cachethis;
1250 COMPOUND4res *cmpresp;
1251 stateid4 current_stateid;
1252 stateid4 save_stateid;
1253 };
1254
1255 typedef struct compound_state compound_state_t;
1256
1257 static inline bool_t
rfs4_has_session(const compound_state_t * cs)1258 rfs4_has_session(const compound_state_t *cs)
1259 {
1260 return (cs->slot != NULL);
1261 }
1262
1263 extern stateid4 invalid_stateid;
1264
1265 #define INVALID_STATEID(x) (!memcmp((x), &invalid_stateid, sizeof (stateid4))
1266
1267 /*
1268 * Conversion commands for nfsv4 server attr checking
1269 */
1270 enum nfs4_attr_cmd {
1271 NFS4ATTR_SUPPORTED = 0, /* check which attrs supported */
1272 NFS4ATTR_GETIT = 1, /* getattr - sys to fattr4 (r) */
1273 NFS4ATTR_SETIT = 2, /* setattr - fattr4 to sys (w) */
1274 NFS4ATTR_VERIT = 3, /* verify - fattr4 to sys (r) */
1275 NFS4ATTR_FREEIT = 4 /* free any alloc'd space for attr */
1276 };
1277
1278 typedef enum nfs4_attr_cmd nfs4_attr_cmd_t;
1279
1280 struct nfs4_svgetit_arg {
1281 nfs4_attr_cmd_t op; /* getit or setit */
1282 struct compound_state *cs;
1283 struct statvfs64 *sbp;
1284 uint_t flag; /* VOP_GETATTR/VOP_SETATTR flag */
1285 uint_t xattr; /* object is xattr */
1286 bool_t rdattr_error_req; /* if readdir & client wants */
1287 /* rdattr_error */
1288 nfsstat4 rdattr_error; /* used for per-entry status */
1289 /* (if rdattr_err) */
1290 bool_t is_referral; /* because sometimes we tell lies */
1291 bool_t mntdfid_set;
1292
1293 fattr4_mounted_on_fileid
1294 mounted_on_fileid;
1295 /* readdir op can always return */
1296 /* d_ino from server fs dirent */
1297 /* for mounted_on_fileid attr. */
1298 /* This field holds d_ino so */
1299 /* srv attr conv code can avoid */
1300 /* doing an untraverse. */
1301 vattr_t vap[1];
1302 };
1303
1304 struct nfs4_ntov_map {
1305 bitmap4 fbit; /* FATTR4_XXX_MASKY */
1306 uint_t vbit; /* AT_XXX */
1307 bool_t vfsstat;
1308 bool_t mandatory; /* attribute mandatory to implement? */
1309 uint_t nval;
1310 int xdr_size; /* Size of XDR'd attr */
1311 xdrproc_t xfunc;
1312 int (*sv_getit)(nfs4_attr_cmd_t, struct nfs4_svgetit_arg *,
1313 union nfs4_attr_u *); /* subroutine for getting attr. */
1314 char *prtstr; /* string attr for printing */
1315 };
1316
1317 struct nfs4attr_to_vattr {
1318 vnode_t *vp;
1319 vattr_t *vap;
1320 nfs_fh4 *fhp;
1321 nfsstat4 rdattr_error;
1322 uint32_t flag;
1323 fattr4_change change;
1324 fattr4_fsid srv_fsid;
1325 fattr4_mounted_on_fileid mntd_fid;
1326 };
1327
1328 typedef struct nfs4attr_to_vattr ntov4_t;
1329
1330 /*
1331 * nfs4attr_to_vattr flags
1332 */
1333 #define NTOV_FHP_VALID 0x01
1334 #define NTOV_RDATTR_ERROR_VALID 0x02
1335 #define NTOV_CHANGE_VALID 0x04
1336 #define NTOV_SUPP_VALID 0x08
1337 #define NTOV_SRV_FSID_VALID 0x10
1338 #define NTOV_MOUNTED_ON_FILEID_VALID 0x20
1339
1340
1341 #define FATTR4_MANDATTR_MASK ( \
1342 FATTR4_SUPPORTED_ATTRS_MASK | \
1343 FATTR4_TYPE_MASK | \
1344 FATTR4_FH_EXPIRE_TYPE_MASK | \
1345 FATTR4_CHANGE_MASK | \
1346 FATTR4_SIZE_MASK | \
1347 FATTR4_LINK_SUPPORT_MASK | \
1348 FATTR4_SYMLINK_SUPPORT_MASK | \
1349 FATTR4_NAMED_ATTR_MASK | \
1350 FATTR4_FSID_MASK | \
1351 FATTR4_UNIQUE_HANDLES_MASK | \
1352 FATTR4_LEASE_TIME_MASK | \
1353 FATTR4_RDATTR_ERROR_MASK | \
1354 FATTR4_FILEHANDLE_MASK)
1355
1356
1357 struct nfs4attr_to_osattr {
1358 void *attrconv_arg;
1359 uint_t mask;
1360 };
1361
1362 struct mntinfo4;
1363
1364 /*
1365 * lkp4_attr_setup lists the different options for attributes when calling
1366 * nfs4lookup_setup - either no attributes (just lookups - e.g., secinfo),
1367 * one component only (normal component lookup), get attributes for the
1368 * last component (e.g., mount), attributes for each component (e.g.,
1369 * failovers later), just the filehandle for the last component (e.g.,
1370 * volatile filehandle recovery), or stuff that needs OPENATTR (e.g.
1371 * looking up a named attribute or it's hidden directory).
1372 */
1373 enum lkp4_attr_setup {
1374 LKP4_NO_ATTRIBUTES = 0, /* no attrs or filehandles */
1375 LKP4_ALL_ATTRIBUTES = 3, /* multi-comp: attrs for all comps */
1376 LKP4_LAST_NAMED_ATTR = 5, /* multi-comp: named attr & attrdir */
1377 LKP4_LAST_ATTRDIR = 6, /* multi-comp: just attrdir */
1378 LKP4_ALL_ATTR_SECINFO = 7 /* multi-comp: attrs for all comp and */
1379 /* secinfo for last comp */
1380 };
1381
1382 /*
1383 * lookup4_param a set of parameters to nfs4lookup_setup -
1384 * used to setup a path lookup compound request.
1385 */
1386 typedef struct lookup4_param {
1387 enum lkp4_attr_setup l4_getattrs; /* (in) get attrs in the lookup? */
1388 int header_len; /* (in) num ops before first lookup */
1389 int trailer_len; /* (in) num ops after last */
1390 /* Lookup/Getattr */
1391 bitmap4 ga_bits; /* (in) Which attributes for Getattr */
1392 COMPOUND4args_clnt *argsp; /* (in/out) args for compound struct */
1393 COMPOUND4res_clnt *resp; /* (in/out) res for compound struct */
1394 int arglen; /* (out) argop buffer alloc'd length */
1395 struct mntinfo4 *mi;
1396 } lookup4_param_t;
1397
1398
1399 #define NFS4_FATTR4_FINISH -1 /* fattr4 index indicating finish */
1400
1401 typedef int (*nfs4attr_to_os_t)(int, union nfs4_attr_u *,
1402 struct nfs4attr_to_osattr *);
1403
1404 /*
1405 * The nfs4_error_t is the basic structure to return error values
1406 * from rfs4call. It encapsulates the unix errno
1407 * value, the nfsstat4 value and the rpc status value into a single
1408 * structure.
1409 *
1410 * If error is set, then stat is ignored and rpc_status may be
1411 * set if the error occurred as the result of a CLNT_CALL. If
1412 * stat is set, then rpc request succeeded, error and
1413 * rpc_status are set to 0 and stat contains the result of
1414 * operation, NFS4_OK or one of the NFS4ERR_* values.
1415 *
1416 * Functions which want to generate errors independently from
1417 * rfs4call should set error to the desired errno value and
1418 * set stat and rpc_status to 0. nfs4_error_init() is a
1419 * convenient function to do this.
1420 */
1421 typedef struct {
1422 int error;
1423 nfsstat4 stat;
1424 enum clnt_stat rpc_status;
1425 } nfs4_error_t;
1426
1427 /*
1428 * Shared functions
1429 */
1430 extern void rfs4_op_readdir(nfs_argop4 *, nfs_resop4 *,
1431 struct svc_req *, struct compound_state *);
1432 extern void nfs_fh4_copy(nfs_fh4 *, nfs_fh4 *);
1433
1434 extern void nfs4_fattr4_free(fattr4 *);
1435
1436 extern int nfs4lookup_setup(char *, lookup4_param_t *, int);
1437 extern void nfs4_getattr_otw_norecovery(vnode_t *,
1438 nfs4_ga_res_t *, nfs4_error_t *, cred_t *, int);
1439 extern int nfs4_getattr_otw(vnode_t *, nfs4_ga_res_t *, cred_t *, int);
1440 extern int nfs4cmpfh(const nfs_fh4 *, const nfs_fh4 *);
1441 extern int nfs4cmpfhandle(nfs4_fhandle_t *, nfs4_fhandle_t *);
1442 extern int nfs4getattr(vnode_t *, struct vattr *, cred_t *);
1443 extern int nfs4_waitfor_purge_complete(vnode_t *);
1444 extern int nfs4_validate_caches(vnode_t *, cred_t *);
1445 extern int nfs4init(int, char *);
1446 extern void nfs4fini(void);
1447 extern int nfs4_vfsinit(void);
1448 extern void nfs4_vfsfini(void);
1449
1450 extern void nfs4_vnops_init(void);
1451 extern void nfs4_vnops_fini(void);
1452 extern void nfs_idmap_init(void);
1453 extern void nfs_idmap_flush(int);
1454 extern void nfs_idmap_fini(void);
1455 extern int nfs4_rnode_init(void);
1456 extern int nfs4_rnode_fini(void);
1457 extern int nfs4_shadow_init(void);
1458 extern int nfs4_shadow_fini(void);
1459 extern int nfs4_acache_init(void);
1460 extern int nfs4_acache_fini(void);
1461 extern int nfs4_subr_init(void);
1462 extern int nfs4_subr_fini(void);
1463 extern void nfs4_acl_init(void);
1464 extern void nfs4_acl_free_cache(vsecattr_t *);
1465
1466 extern int geterrno4(nfsstat4);
1467 extern nfsstat4 puterrno4(int);
1468 extern int nfs4_need_to_bump_seqid(COMPOUND4res_clnt *);
1469 extern int nfs4tsize(void);
1470 extern int checkauth4(struct compound_state *, struct svc_req *);
1471 extern nfsstat4 call_checkauth4(struct compound_state *, struct svc_req *);
1472 extern int is_exported_sec(int, struct exportinfo *);
1473 extern void nfs4_vmask_to_nmask(uint_t, bitmap4 *);
1474 extern void nfs4_vmask_to_nmask_set(uint_t, bitmap4 *);
1475 extern int nfs_idmap_str_uid(utf8string *u8s, uid_t *, bool_t);
1476 extern int nfs_idmap_str_gid(utf8string *u8s, gid_t *, bool_t);
1477 extern int nfs_idmap_uid_str(uid_t, utf8string *u8s, bool_t);
1478 extern int nfs_idmap_gid_str(gid_t gid, utf8string *u8s, bool_t);
1479 extern int nfs4_time_ntov(nfstime4 *, timestruc_t *);
1480 extern int nfs4_time_vton(timestruc_t *, nfstime4 *);
1481 extern char *utf8_to_str(utf8string *, uint_t *, char *);
1482 extern char *utf8_to_fn(utf8string *, uint_t *, char *);
1483 extern utf8string *str_to_utf8(char *, utf8string *);
1484 extern utf8string *utf8_copy(utf8string *, utf8string *);
1485 extern int utf8_compare(const utf8string *, const utf8string *);
1486 extern nfsstat4 utf8_dir_verify(utf8string *);
1487 extern char *utf8_strchr(utf8string *, const char);
1488 extern int ln_ace4_cmp(nfsace4 *, nfsace4 *, int);
1489 extern int vs_aent_to_ace4(vsecattr_t *, vsecattr_t *, int, int);
1490 extern int vs_ace4_to_aent(vsecattr_t *, vsecattr_t *, uid_t, gid_t,
1491 int, int);
1492 extern int vs_ace4_to_acet(vsecattr_t *, vsecattr_t *, uid_t, gid_t,
1493 int);
1494 extern int vs_acet_to_ace4(vsecattr_t *, vsecattr_t *, int);
1495 extern void vs_acet_destroy(vsecattr_t *);
1496 extern void vs_ace4_destroy(vsecattr_t *);
1497 extern void vs_aent_destroy(vsecattr_t *);
1498
1499 extern int vn_find_nfs_record(vnode_t *, nvlist_t **, char **, char **);
1500 extern int vn_is_nfs_reparse(vnode_t *, cred_t *);
1501 extern fs_locations4 *fetch_referral(vnode_t *, cred_t *);
1502 extern char *build_symlink(vnode_t *, cred_t *, size_t *);
1503
1504 extern int stateid4_cmp(stateid4 *, stateid4 *);
1505
1506 extern vtype_t nf4_to_vt[];
1507
1508 extern struct nfs4_ntov_map nfs4_ntov_map[];
1509 extern uint_t nfs4_ntov_map_size;
1510
1511 extern struct vfsops *nfs4_vfsops;
1512 extern struct vnodeops *nfs4_vnodeops;
1513 extern const struct fs_operation_def nfs4_vnodeops_template[];
1514 extern vnodeops_t *nfs4_trigger_vnodeops;
1515 extern const struct fs_operation_def nfs4_trigger_vnodeops_template[];
1516
1517 extern uint_t nfs4_tsize(struct knetconfig *);
1518 extern uint_t rfs4_tsize(struct svc_req *);
1519
1520 extern bool_t xdr_inline_decode_nfs_fh4(uint32_t *, nfs_fh4_fmt_t *,
1521 uint32_t);
1522 extern bool_t xdr_inline_encode_nfs_fh4(uint32_t **, uint32_t *,
1523 nfs_fh4_fmt_t *);
1524
1525 #ifdef DEBUG
1526 extern int rfs4_do_pre_op_attr;
1527 extern int rfs4_do_post_op_attr;
1528 #endif
1529
1530 extern stateid4 clnt_special0;
1531 extern stateid4 clnt_special1;
1532 #define CLNT_ISSPECIAL(id) (stateid4_cmp(id, &clnt_special0) || \
1533 stateid4_cmp(id, &clnt_special1))
1534
1535 /* State's functions */
1536 extern void rfs4_ss_clid(nfs4_srv_t *nsrv4, rfs4_client_t *);
1537 extern void rfs4_ss_chkclid(nfs4_srv_t *nsrv4, rfs4_client_t *);
1538
1539 /* Declarations for nfs4.x */
1540 nfsstat4 do_rfs4_op_secinfo(struct compound_state *, char *, SECINFO4res *);
1541
1542 /*
1543 * The NFS Version 4 service procedures.
1544 */
1545
1546 extern void rfs4_do_server_start(int, int, nfs4_minor_t, int);
1547 extern void rfs4_compound(COMPOUND4args *, COMPOUND4res *,
1548 compound_state_t *, struct svc_req *, int *);
1549 extern void rfs4_init_compound_state(struct compound_state *);
1550 extern void rfs4_fini_compound_state(struct compound_state *);
1551
1552 struct rpcdisp;
1553 extern int rfs4_dispatch(struct rpcdisp *, struct svc_req *, SVCXPRT *, char *);
1554 extern void rfs4_compound_free(COMPOUND4res *);
1555 extern bool_t rfs4_idempotent_req(const COMPOUND4args *);
1556
1557 extern void rfs4_srvrinit(void);
1558 extern void rfs4_srvrfini(void);
1559 extern void rfs4_srv_zone_init(nfs_globals_t *);
1560 extern void rfs4_srv_zone_fini(nfs_globals_t *);
1561 extern void rfs4_state_g_init(void);
1562 extern void rfs4_state_zone_init(nfs4_srv_t *);
1563 extern void rfs4_state_g_fini(void);
1564 extern void rfs4_state_zone_fini(void);
1565 extern nfs4_srv_t *nfs4_get_srv(void);
1566
1567 void put_stateid4(struct compound_state *, stateid4 *);
1568 void get_stateid4(struct compound_state *, stateid4 *);
1569
1570 #endif
1571 #ifdef __cplusplus
1572 }
1573 #endif
1574
1575 #endif /* _NFS4_H */
1576