1 /*
2  *  Copyright (c) 2001 The Regents of the University of Michigan.
3  *  All rights reserved.
4  *
5  *  Kendrick Smith <kmsmith@umich.edu>
6  *  Andy Adamson <andros@umich.edu>
7  *
8  *  Redistribution and use in source and binary forms, with or without
9  *  modification, are permitted provided that the following conditions
10  *  are met:
11  *
12  *  1. Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *  2. Redistributions in binary form must reproduce the above copyright
15  *     notice, this list of conditions and the following disclaimer in the
16  *     documentation and/or other materials provided with the distribution.
17  *  3. Neither the name of the University nor the names of its
18  *     contributors may be used to endorse or promote products derived
19  *     from this software without specific prior written permission.
20  *
21  *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24  *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28  *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34 
35 #ifndef _NFSD4_STATE_H
36 #define _NFSD4_STATE_H
37 
38 #include <linux/idr.h>
39 #include <linux/refcount.h>
40 #include <linux/sunrpc/svc_xprt.h>
41 #include "nfsfh.h"
42 #include "nfsd.h"
43 
44 typedef struct {
45 	u32             cl_boot;
46 	u32             cl_id;
47 } clientid_t;
48 
49 typedef struct {
50 	clientid_t	so_clid;
51 	u32		so_id;
52 } stateid_opaque_t;
53 
54 typedef struct {
55 	u32                     si_generation;
56 	stateid_opaque_t        si_opaque;
57 } stateid_t;
58 
59 typedef struct {
60 	stateid_t		cs_stid;
61 #define NFS4_COPY_STID 1
62 #define NFS4_COPYNOTIFY_STID 2
63 	unsigned char		cs_type;
64 	refcount_t		cs_count;
65 } copy_stateid_t;
66 
67 struct nfsd4_referring_call {
68 	struct list_head	__list;
69 
70 	u32			rc_sequenceid;
71 	u32			rc_slotid;
72 };
73 
74 struct nfsd4_referring_call_list {
75 	struct list_head	__list;
76 
77 	struct nfs4_sessionid	rcl_sessionid;
78 	int			__nr_referring_calls;
79 	struct list_head	rcl_referring_calls;
80 };
81 
82 struct nfsd4_callback {
83 	struct nfs4_client *cb_clp;
84 	struct rpc_message cb_msg;
85 #define NFSD4_CALLBACK_RUNNING		(0)
86 #define NFSD4_CALLBACK_WAKE		(1)
87 #define NFSD4_CALLBACK_REQUEUE		(2)
88 	unsigned long cb_flags;
89 	const struct nfsd4_callback_ops *cb_ops;
90 	struct work_struct cb_work;
91 	int cb_seq_status;
92 	int cb_status;
93 	int cb_held_slot;
94 
95 	int cb_nr_referring_call_list;
96 	struct list_head cb_referring_call_list;
97 };
98 
99 struct nfsd4_callback_ops {
100 	void (*prepare)(struct nfsd4_callback *);
101 	int (*done)(struct nfsd4_callback *, struct rpc_task *);
102 	void (*release)(struct nfsd4_callback *);
103 	uint32_t opcode;
104 };
105 
106 /*
107  * A core object that represents a "common" stateid. These are generally
108  * embedded within the different (more specific) stateid objects and contain
109  * fields that are of general use to any stateid.
110  */
111 struct nfs4_stid {
112 	refcount_t		sc_count;
113 
114 	/* A new stateid is added to the cl_stateids idr early before it
115 	 * is fully initialised.  Its sc_type is then zero.  After
116 	 * initialisation the sc_type it set under cl_lock, and then
117 	 * never changes.
118 	 */
119 #define SC_TYPE_OPEN		BIT(0)
120 #define SC_TYPE_LOCK		BIT(1)
121 #define SC_TYPE_DELEG		BIT(2)
122 #define SC_TYPE_LAYOUT		BIT(3)
123 	unsigned short		sc_type;
124 
125 /* state_lock protects sc_status for delegation stateids.
126  * ->cl_lock protects sc_status for open and lock stateids.
127  * ->st_mutex also protect sc_status for open stateids.
128  * ->ls_lock protects sc_status for layout stateids.
129  */
130 /*
131  * For an open stateid kept around *only* to process close replays.
132  * For deleg stateid, kept in idr until last reference is dropped.
133  */
134 #define SC_STATUS_CLOSED	BIT(0)
135 /* For a deleg stateid kept around only to process free_stateid's: */
136 #define SC_STATUS_REVOKED	BIT(1)
137 #define SC_STATUS_ADMIN_REVOKED	BIT(2)
138 #define SC_STATUS_FREEABLE	BIT(3)
139 #define SC_STATUS_FREED		BIT(4)
140 	unsigned short		sc_status;
141 
142 	struct list_head	sc_cp_list;
143 	stateid_t		sc_stateid;
144 	spinlock_t		sc_lock;
145 	struct nfs4_client	*sc_client;
146 	struct nfs4_file	*sc_file;
147 	void			(*sc_free)(struct nfs4_stid *);
148 };
149 
150 /* Keep a list of stateids issued by the COPY_NOTIFY, associate it with the
151  * parent OPEN/LOCK/DELEG stateid.
152  */
153 struct nfs4_cpntf_state {
154 	copy_stateid_t		cp_stateid;
155 	struct list_head	cp_list;	/* per parent nfs4_stid */
156 	stateid_t		cp_p_stateid;	/* copy of parent's stateid */
157 	clientid_t		cp_p_clid;	/* copy of parent's clid */
158 	time64_t		cpntf_time;	/* last time stateid used */
159 };
160 
161 /*
162  * RFC 7862 Section 4.8 states:
163  *
164  * | A copy offload stateid will be valid until either (A) the client
165  * | or server restarts or (B) the client returns the resource by
166  * | issuing an OFFLOAD_CANCEL operation or the client replies to a
167  * | CB_OFFLOAD operation.
168  *
169  * Because a client might not reply to a CB_OFFLOAD, or a reply
170  * might get lost due to connection loss, NFSD purges async copy
171  * state after a short period to prevent it from accumulating
172  * over time.
173  */
174 #define NFSD_COPY_INITIAL_TTL 10
175 
176 struct nfs4_cb_fattr {
177 	struct nfsd4_callback ncf_getattr;
178 	u32 ncf_cb_status;
179 
180 	/* from CB_GETATTR reply */
181 	u64 ncf_cb_change;
182 	u64 ncf_cb_fsize;
183 	struct timespec64 ncf_cb_mtime;
184 	struct timespec64 ncf_cb_atime;
185 
186 	bool ncf_file_modified;
187 	u64 ncf_initial_cinfo;
188 	u64 ncf_cur_fsize;
189 };
190 
191 /*
192  * Represents a delegation stateid. The nfs4_client holds references to these
193  * and they are put when it is being destroyed or when the delegation is
194  * returned by the client:
195  *
196  * o 1 reference as long as a delegation is still in force (taken when it's
197  *   alloc'd, put when it's returned or revoked)
198  *
199  * o 1 reference as long as a recall rpc is in progress (taken when the lease
200  *   is broken, put when the rpc exits)
201  *
202  * o 1 more ephemeral reference for each nfsd thread currently doing something
203  *   with that delegation without holding the cl_lock
204  *
205  * If the server attempts to recall a delegation and the client doesn't do so
206  * before a timeout, the server may also revoke the delegation. In that case,
207  * the object will either be destroyed (v4.0) or moved to a per-client list of
208  * revoked delegations (v4.1+).
209  *
210  * This object is a superset of the nfs4_stid.
211  */
212 struct nfs4_delegation {
213 	struct nfs4_stid	dl_stid; /* must be first field */
214 	struct list_head	dl_perfile;
215 	struct list_head	dl_perclnt;
216 	struct list_head	dl_recall_lru;  /* delegation recalled */
217 	struct nfs4_clnt_odstate *dl_clnt_odstate;
218 	time64_t		dl_time;
219 	u32			dl_type;
220 /* For recall: */
221 	int			dl_retries;
222 	struct nfsd4_callback	dl_recall;
223 	bool			dl_recalled;
224 
225 	/* for CB_GETATTR */
226 	struct nfs4_cb_fattr    dl_cb_fattr;
227 };
228 
229 static inline bool deleg_is_read(u32 dl_type)
230 {
231 	return (dl_type == OPEN_DELEGATE_READ || dl_type == OPEN_DELEGATE_READ_ATTRS_DELEG);
232 }
233 
234 static inline bool deleg_is_write(u32 dl_type)
235 {
236 	return (dl_type == OPEN_DELEGATE_WRITE || dl_type == OPEN_DELEGATE_WRITE_ATTRS_DELEG);
237 }
238 
239 static inline bool deleg_attrs_deleg(u32 dl_type)
240 {
241 	return dl_type == OPEN_DELEGATE_READ_ATTRS_DELEG ||
242 	       dl_type == OPEN_DELEGATE_WRITE_ATTRS_DELEG;
243 }
244 
245 #define cb_to_delegation(cb) \
246 	container_of(cb, struct nfs4_delegation, dl_recall)
247 
248 /* client delegation callback info */
249 struct nfs4_cb_conn {
250 	/* SETCLIENTID info */
251 	struct sockaddr_storage	cb_addr;
252 	struct sockaddr_storage	cb_saddr;
253 	size_t			cb_addrlen;
254 	u32                     cb_prog; /* used only in 4.0 case;
255 					    per-session otherwise */
256 	u32                     cb_ident;	/* minorversion 0 only */
257 	struct svc_xprt		*cb_xprt;	/* minorversion 1 only */
258 };
259 
260 static inline struct nfs4_delegation *delegstateid(struct nfs4_stid *s)
261 {
262 	return container_of(s, struct nfs4_delegation, dl_stid);
263 }
264 
265 /* Maximum number of slots per session.  This is for sanity-check only.
266  * It could be increased if we had a mechanism to shutdown misbehaving clients.
267  * A large number can be needed to get good throughput on high-latency servers.
268  */
269 #define NFSD_MAX_SLOTS_PER_SESSION	2048
270 /* Maximum  session per slot cache size */
271 #define NFSD_SLOT_CACHE_SIZE		2048
272 /* Maximum number of NFSD_SLOT_CACHE_SIZE slots per session */
273 #define NFSD_CACHE_SIZE_SLOTS_PER_SESSION	32
274 #define NFSD_MAX_MEM_PER_SESSION  \
275 		(NFSD_CACHE_SIZE_SLOTS_PER_SESSION * NFSD_SLOT_CACHE_SIZE)
276 
277 struct nfsd4_slot {
278 	u32	sl_seqid;
279 	__be32	sl_status;
280 	struct svc_cred sl_cred;
281 	u32	sl_index;
282 	u32	sl_datalen;
283 	u16	sl_opcnt;
284 	u16	sl_generation;
285 #define NFSD4_SLOT_INUSE	(1 << 0)
286 #define NFSD4_SLOT_CACHETHIS	(1 << 1)
287 #define NFSD4_SLOT_INITIALIZED	(1 << 2)
288 #define NFSD4_SLOT_CACHED	(1 << 3)
289 #define NFSD4_SLOT_REUSED	(1 << 4)
290 	u8	sl_flags;
291 	char	sl_data[];
292 };
293 
294 struct nfsd4_channel_attrs {
295 	u32		headerpadsz;
296 	u32		maxreq_sz;
297 	u32		maxresp_sz;
298 	u32		maxresp_cached;
299 	u32		maxops;
300 	u32		maxreqs;
301 	u32		nr_rdma_attrs;
302 	u32		rdma_attrs;
303 };
304 
305 struct nfsd4_cb_sec {
306 	u32	flavor; /* (u32)(-1) used to mean "no valid flavor" */
307 	kuid_t	uid;
308 	kgid_t	gid;
309 };
310 
311 struct nfsd4_create_session {
312 	clientid_t			clientid;
313 	struct nfs4_sessionid		sessionid;
314 	u32				seqid;
315 	u32				flags;
316 	struct nfsd4_channel_attrs	fore_channel;
317 	struct nfsd4_channel_attrs	back_channel;
318 	u32				callback_prog;
319 	struct nfsd4_cb_sec		cb_sec;
320 };
321 
322 struct nfsd4_backchannel_ctl {
323 	u32	bc_cb_program;
324 	struct nfsd4_cb_sec		bc_cb_sec;
325 };
326 
327 struct nfsd4_bind_conn_to_session {
328 	struct nfs4_sessionid		sessionid;
329 	u32				dir;
330 };
331 
332 /* The single slot clientid cache structure */
333 struct nfsd4_clid_slot {
334 	u32				sl_seqid;
335 	__be32				sl_status;
336 	struct nfsd4_create_session	sl_cr_ses;
337 };
338 
339 struct nfsd4_conn {
340 	struct list_head cn_persession;
341 	struct svc_xprt *cn_xprt;
342 	struct svc_xpt_user cn_xpt_user;
343 	struct nfsd4_session *cn_session;
344 /* CDFC4_FORE, CDFC4_BACK: */
345 	unsigned char cn_flags;
346 };
347 
348 /* Maximum number of slots that nfsd will use in the backchannel */
349 #define NFSD_BC_SLOT_TABLE_SIZE		(sizeof(u32) * 8)
350 
351 /*
352  * Representation of a v4.1+ session. These are refcounted in a similar fashion
353  * to the nfs4_client. References are only taken when the server is actively
354  * working on the object (primarily during the processing of compounds).
355  */
356 struct nfsd4_session {
357 	atomic_t		se_ref;
358 	spinlock_t		se_lock;
359 	u32			se_cb_slot_avail; /* bitmap of available slots */
360 	u32			se_cb_highest_slot;	/* highest slot client wants */
361 	u32			se_cb_prog;
362 	struct list_head	se_hash;	/* hash by sessionid */
363 	struct list_head	se_perclnt;
364 	struct list_head	se_all_sessions;/* global list of sessions */
365 	struct nfs4_client	*se_client;
366 	struct nfs4_sessionid	se_sessionid;
367 	struct nfsd4_channel_attrs se_fchannel;
368 	struct nfsd4_cb_sec	se_cb_sec;
369 	struct list_head	se_conns;
370 	u32			se_cb_seq_nr[NFSD_BC_SLOT_TABLE_SIZE];
371 	struct xarray		se_slots;	/* forward channel slots */
372 	u16			se_slot_gen;
373 	bool			se_dead;
374 	u32			se_target_maxslots;
375 };
376 
377 /* formatted contents of nfs4_sessionid */
378 struct nfsd4_sessionid {
379 	clientid_t	clientid;
380 	u32		sequence;
381 	u32		reserved;
382 };
383 
384 #define HEXDIR_LEN     33 /* hex version of 16 byte md5 of cl_name plus '\0' */
385 
386 /*
387  *       State                Meaning                  Where set
388  * --------------------------------------------------------------------------
389  * | NFSD4_ACTIVE      | Confirmed, active    | Default                     |
390  * |------------------- ----------------------------------------------------|
391  * | NFSD4_COURTESY    | Courtesy state.      | nfs4_get_client_reaplist    |
392  * |                   | Lease/lock/share     |                             |
393  * |                   | reservation conflict |                             |
394  * |                   | can cause Courtesy   |                             |
395  * |                   | client to be expired |                             |
396  * |------------------------------------------------------------------------|
397  * | NFSD4_EXPIRABLE   | Courtesy client to be| nfs4_laundromat             |
398  * |                   | expired by Laundromat| try_to_expire_client        |
399  * |                   | due to conflict      |                             |
400  * |------------------------------------------------------------------------|
401  */
402 enum {
403 	NFSD4_ACTIVE = 0,
404 	NFSD4_COURTESY,
405 	NFSD4_EXPIRABLE,
406 };
407 
408 /*
409  * struct nfs4_client - one per client.  Clientids live here.
410  *
411  * The initial object created by an NFS client using SETCLIENTID (for NFSv4.0)
412  * or EXCHANGE_ID (for NFSv4.1+). These objects are refcounted and timestamped.
413  * Each nfsd_net_ns object contains a set of these and they are tracked via
414  * short and long form clientid. They are hashed and searched for under the
415  * per-nfsd_net client_lock spinlock.
416  *
417  * References to it are only held during the processing of compounds, and in
418  * certain other operations. In their "resting state" they have a refcount of
419  * 0. If they are not renewed within a lease period, they become eligible for
420  * destruction by the laundromat.
421  *
422  * These objects can also be destroyed if the client sends certain forms of
423  * SETCLIENTID or EXCHANGE_ID operations.
424  *
425  * Care is taken *not* to do this however when the objects have an elevated
426  * refcount.
427  *
428  * o Each nfs4_client is hashed by clientid
429  *
430  * o Each nfs4_clients is also hashed by name (the opaque quantity initially
431  *   sent by the client to identify itself).
432  *
433  * o cl_perclient list is used to ensure no dangling stateowner references
434  *   when we expire the nfs4_client
435  */
436 struct nfs4_client {
437 	struct list_head	cl_idhash; 	/* hash by cl_clientid.id */
438 	struct rb_node		cl_namenode;	/* link into by-name trees */
439 	struct list_head	*cl_ownerstr_hashtbl;
440 	struct list_head	cl_openowners;
441 	struct idr		cl_stateids;	/* stateid lookup */
442 	struct list_head	cl_delegations;
443 	struct list_head	cl_revoked;	/* unacknowledged, revoked 4.1 state */
444 	struct list_head        cl_lru;         /* tail queue */
445 #ifdef CONFIG_NFSD_PNFS
446 	struct list_head	cl_lo_states;	/* outstanding layout states */
447 #endif
448 	struct xdr_netobj	cl_name; 	/* id generated by client */
449 	nfs4_verifier		cl_verifier; 	/* generated by client */
450 	time64_t		cl_time;	/* time of last lease renewal */
451 	struct sockaddr_storage	cl_addr; 	/* client ipaddress */
452 	bool			cl_mach_cred;	/* SP4_MACH_CRED in force */
453 	struct svc_cred		cl_cred; 	/* setclientid principal */
454 	clientid_t		cl_clientid;	/* generated by server */
455 	nfs4_verifier		cl_confirm;	/* generated by server */
456 	u32			cl_minorversion;
457 	atomic_t		cl_admin_revoked; /* count of admin-revoked states */
458 	/* NFSv4.1 client implementation id: */
459 	struct xdr_netobj	cl_nii_domain;
460 	struct xdr_netobj	cl_nii_name;
461 	struct timespec64	cl_nii_time;
462 
463 	/* for v4.0 and v4.1 callbacks: */
464 	struct nfs4_cb_conn	cl_cb_conn;
465 #define NFSD4_CLIENT_CB_UPDATE		(0)
466 #define NFSD4_CLIENT_CB_KILL		(1)
467 #define NFSD4_CLIENT_STABLE		(2)	/* client on stable storage */
468 #define NFSD4_CLIENT_RECLAIM_COMPLETE	(3)	/* reclaim_complete done */
469 #define NFSD4_CLIENT_CONFIRMED		(4)	/* client is confirmed */
470 #define NFSD4_CLIENT_UPCALL_LOCK	(5)	/* upcall serialization */
471 #define NFSD4_CLIENT_CB_FLAG_MASK	(1 << NFSD4_CLIENT_CB_UPDATE | \
472 					 1 << NFSD4_CLIENT_CB_KILL)
473 	unsigned long		cl_flags;
474 
475 	struct workqueue_struct *cl_callback_wq;
476 	const struct cred	*cl_cb_cred;
477 	struct rpc_clnt		*cl_cb_client;
478 	u32			cl_cb_ident;
479 #define NFSD4_CB_UP		0
480 #define NFSD4_CB_UNKNOWN	1
481 #define NFSD4_CB_DOWN		2
482 #define NFSD4_CB_FAULT		3
483 	int			cl_cb_state;
484 	struct nfsd4_callback	cl_cb_null;
485 	struct nfsd4_session	*cl_cb_session;
486 
487 	/* for all client information that callback code might need: */
488 	spinlock_t		cl_lock;
489 
490 	/* for nfs41 */
491 	struct list_head	cl_sessions;
492 	struct nfsd4_clid_slot	cl_cs_slot;	/* create_session slot */
493 	u32			cl_exchange_flags;
494 	/* number of rpc's in progress over an associated session: */
495 	atomic_t		cl_rpc_users;
496 	struct nfsdfs_client	cl_nfsdfs;
497 	struct nfs4_op_map      cl_spo_must_allow;
498 
499 	/* debugging info directory under nfsd/clients/ : */
500 	struct dentry		*cl_nfsd_dentry;
501 	/* 'info' file within that directory. Ref is not counted,
502 	 * but will remain valid iff cl_nfsd_dentry != NULL
503 	 */
504 	struct dentry		*cl_nfsd_info_dentry;
505 
506 	struct rpc_wait_queue	cl_cb_waitq;	/* backchannel callers may */
507 						/* wait here for slots */
508 	struct net		*net;
509 	struct list_head	async_copies;	/* list of async copies */
510 	spinlock_t		async_lock;	/* lock for async copies */
511 	atomic_t		cl_cb_inflight;	/* Outstanding callbacks */
512 
513 	unsigned int		cl_state;
514 	atomic_t		cl_delegs_in_recall;
515 
516 	struct nfsd4_cb_recall_any	*cl_ra;
517 	time64_t		cl_ra_time;
518 };
519 
520 /* struct nfs4_client_reset
521  * one per old client. Populates reset_str_hashtbl. Filled from conf_id_hashtbl
522  * upon lease reset, or from upcall to state_daemon (to read in state
523  * from non-volitile storage) upon reboot.
524  */
525 struct nfs4_client_reclaim {
526 	struct list_head	cr_strhash;	/* hash by cr_name */
527 	struct nfs4_client	*cr_clp;	/* pointer to associated clp */
528 	struct xdr_netobj	cr_name;	/* recovery dir name */
529 	struct xdr_netobj	cr_princhash;
530 };
531 
532 /* A reasonable value for REPLAY_ISIZE was estimated as follows:
533  * The OPEN response, typically the largest, requires
534  *   4(status) + 8(stateid) + 20(changeinfo) + 4(rflags) +  8(verifier) +
535  *   4(deleg. type) + 8(deleg. stateid) + 4(deleg. recall flag) +
536  *   20(deleg. space limit) + ~32(deleg. ace) = 112 bytes
537  */
538 
539 #define NFSD4_REPLAY_ISIZE       112
540 
541 /*
542  * Replay buffer, where the result of the last seqid-mutating operation
543  * is cached.
544  */
545 struct nfs4_replay {
546 	__be32			rp_status;
547 	unsigned int		rp_buflen;
548 	char			*rp_buf;
549 	struct knfsd_fh		rp_openfh;
550 	int			rp_locked;
551 	char			rp_ibuf[NFSD4_REPLAY_ISIZE];
552 };
553 
554 struct nfs4_stateowner;
555 
556 struct nfs4_stateowner_operations {
557 	void (*so_unhash)(struct nfs4_stateowner *);
558 	void (*so_free)(struct nfs4_stateowner *);
559 };
560 
561 /*
562  * A core object that represents either an open or lock owner. The object and
563  * lock owner objects have one of these embedded within them. Refcounts and
564  * other fields common to both owner types are contained within these
565  * structures.
566  */
567 struct nfs4_stateowner {
568 	struct list_head			so_strhash;
569 	struct list_head			so_stateids;
570 	struct nfs4_client			*so_client;
571 	const struct nfs4_stateowner_operations	*so_ops;
572 	/* after increment in nfsd4_bump_seqid, represents the next
573 	 * sequence id expected from the client: */
574 	atomic_t				so_count;
575 	u32					so_seqid;
576 	struct xdr_netobj			so_owner; /* open owner name */
577 	struct nfs4_replay			so_replay;
578 	bool					so_is_open_owner;
579 };
580 
581 /*
582  * When a file is opened, the client provides an open state owner opaque string
583  * that indicates the "owner" of that open. These objects are refcounted.
584  * References to it are held by each open state associated with it. This object
585  * is a superset of the nfs4_stateowner struct.
586  */
587 struct nfs4_openowner {
588 	struct nfs4_stateowner	oo_owner; /* must be first field */
589 	struct list_head        oo_perclient;
590 	/*
591 	 * We keep around openowners a little while after last close,
592 	 * which saves clients from having to confirm, and allows us to
593 	 * handle close replays if they come soon enough.  The close_lru
594 	 * is a list of such openowners, to be reaped by the laundromat
595 	 * thread eventually if they remain unused:
596 	 */
597 	struct list_head	oo_close_lru;
598 	struct nfs4_ol_stateid *oo_last_closed_stid;
599 	time64_t		oo_time; /* time of placement on so_close_lru */
600 #define NFS4_OO_CONFIRMED   1
601 	unsigned char		oo_flags;
602 };
603 
604 /*
605  * Represents a generic "lockowner". Similar to an openowner. References to it
606  * are held by the lock stateids that are created on its behalf. This object is
607  * a superset of the nfs4_stateowner struct.
608  */
609 struct nfs4_lockowner {
610 	struct nfs4_stateowner	lo_owner;	/* must be first element */
611 	struct list_head	lo_blocked;	/* blocked file_locks */
612 };
613 
614 static inline struct nfs4_openowner * openowner(struct nfs4_stateowner *so)
615 {
616 	return container_of(so, struct nfs4_openowner, oo_owner);
617 }
618 
619 static inline struct nfs4_lockowner * lockowner(struct nfs4_stateowner *so)
620 {
621 	return container_of(so, struct nfs4_lockowner, lo_owner);
622 }
623 
624 /*
625  * Per-client state indicating no. of opens and outstanding delegations
626  * on a file from a particular client.'od' stands for 'open & delegation'
627  */
628 struct nfs4_clnt_odstate {
629 	struct nfs4_client	*co_client;
630 	struct nfs4_file	*co_file;
631 	struct list_head	co_perfile;
632 	refcount_t		co_odcount;
633 };
634 
635 /*
636  * nfs4_file: a file opened by some number of (open) nfs4_stateowners.
637  *
638  * These objects are global. nfsd keeps one instance of a nfs4_file per
639  * filehandle (though it may keep multiple file descriptors for each). Each
640  * inode can have multiple filehandles associated with it, so there is
641  * (potentially) a many to one relationship between this struct and struct
642  * inode.
643  */
644 struct nfs4_file {
645 	refcount_t		fi_ref;
646 	struct inode *		fi_inode;
647 	bool			fi_aliased;
648 	spinlock_t		fi_lock;
649 	struct rhlist_head	fi_rlist;
650 	struct list_head        fi_stateids;
651 	union {
652 		struct list_head	fi_delegations;
653 		struct rcu_head		fi_rcu;
654 	};
655 	struct list_head	fi_clnt_odstate;
656 	/* One each for O_RDONLY, O_WRONLY, O_RDWR: */
657 	struct nfsd_file	*fi_fds[3];
658 	/*
659 	 * Each open or lock stateid contributes 0-4 to the counts
660 	 * below depending on which bits are set in st_access_bitmap:
661 	 *     1 to fi_access[O_RDONLY] if NFS4_SHARE_ACCES_READ is set
662 	 *   + 1 to fi_access[O_WRONLY] if NFS4_SHARE_ACCESS_WRITE is set
663 	 *   + 1 to both of the above if NFS4_SHARE_ACCESS_BOTH is set.
664 	 */
665 	atomic_t		fi_access[2];
666 	u32			fi_share_deny;
667 	struct nfsd_file	*fi_deleg_file;
668 	int			fi_delegees;
669 	struct knfsd_fh		fi_fhandle;
670 	bool			fi_had_conflict;
671 #ifdef CONFIG_NFSD_PNFS
672 	struct list_head	fi_lo_states;
673 	atomic_t		fi_lo_recalls;
674 #endif
675 };
676 
677 /*
678  * A generic struct representing either a open or lock stateid. The nfs4_client
679  * holds a reference to each of these objects, and they in turn hold a
680  * reference to their respective stateowners. The client's reference is
681  * released in response to a close or unlock (depending on whether it's an open
682  * or lock stateid) or when the client is being destroyed.
683  *
684  * In the case of v4.0 open stateids, these objects are preserved for a little
685  * while after close in order to handle CLOSE replays. Those are eventually
686  * reclaimed via a LRU scheme by the laundromat.
687  *
688  * This object is a superset of the nfs4_stid. "ol" stands for "Open or Lock".
689  * Better suggestions welcome.
690  */
691 struct nfs4_ol_stateid {
692 	struct nfs4_stid		st_stid;
693 	struct list_head		st_perfile;
694 	struct list_head		st_perstateowner;
695 	struct list_head		st_locks;
696 	struct nfs4_stateowner		*st_stateowner;
697 	struct nfs4_clnt_odstate	*st_clnt_odstate;
698 /*
699  * These bitmasks use 3 separate bits for READ, ALLOW, and BOTH; see the
700  * comment above bmap_to_share_mode() for explanation:
701  */
702 	unsigned char			st_access_bmap;
703 	unsigned char			st_deny_bmap;
704 	struct nfs4_ol_stateid		*st_openstp;
705 	struct mutex			st_mutex;
706 };
707 
708 static inline struct nfs4_ol_stateid *openlockstateid(struct nfs4_stid *s)
709 {
710 	return container_of(s, struct nfs4_ol_stateid, st_stid);
711 }
712 
713 struct nfs4_layout_stateid {
714 	struct nfs4_stid		ls_stid;
715 	struct list_head		ls_perclnt;
716 	struct list_head		ls_perfile;
717 	spinlock_t			ls_lock;
718 	struct list_head		ls_layouts;
719 	u32				ls_layout_type;
720 	struct nfsd_file		*ls_file;
721 	struct nfsd4_callback		ls_recall;
722 	stateid_t			ls_recall_sid;
723 	bool				ls_recalled;
724 	struct mutex			ls_mutex;
725 };
726 
727 static inline struct nfs4_layout_stateid *layoutstateid(struct nfs4_stid *s)
728 {
729 	return container_of(s, struct nfs4_layout_stateid, ls_stid);
730 }
731 
732 /* flags for preprocess_seqid_op() */
733 #define RD_STATE	        0x00000010
734 #define WR_STATE	        0x00000020
735 
736 enum nfsd4_cb_op {
737 	NFSPROC4_CLNT_CB_NULL = 0,
738 	NFSPROC4_CLNT_CB_RECALL,
739 	NFSPROC4_CLNT_CB_LAYOUT,
740 	NFSPROC4_CLNT_CB_OFFLOAD,
741 	NFSPROC4_CLNT_CB_SEQUENCE,
742 	NFSPROC4_CLNT_CB_NOTIFY_LOCK,
743 	NFSPROC4_CLNT_CB_RECALL_ANY,
744 	NFSPROC4_CLNT_CB_GETATTR,
745 };
746 
747 /* Returns true iff a is later than b: */
748 static inline bool nfsd4_stateid_generation_after(stateid_t *a, stateid_t *b)
749 {
750 	return (s32)(a->si_generation - b->si_generation) > 0;
751 }
752 
753 /*
754  * When a client tries to get a lock on a file, we set one of these objects
755  * on the blocking lock. When the lock becomes free, we can then issue a
756  * CB_NOTIFY_LOCK to the server.
757  */
758 struct nfsd4_blocked_lock {
759 	struct list_head	nbl_list;
760 	struct list_head	nbl_lru;
761 	time64_t		nbl_time;
762 	struct file_lock	nbl_lock;
763 	struct knfsd_fh		nbl_fh;
764 	struct nfsd4_callback	nbl_cb;
765 	struct kref		nbl_kref;
766 };
767 
768 struct nfsd4_compound_state;
769 struct nfsd_net;
770 struct nfsd4_copy;
771 
772 extern __be32 nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
773 		struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
774 		stateid_t *stateid, int flags, struct nfsd_file **filp,
775 		struct nfs4_stid **cstid);
776 __be32 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
777 			    stateid_t *stateid, unsigned short typemask,
778 			    unsigned short statusmask,
779 			    struct nfs4_stid **s, struct nfsd_net *nn);
780 struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
781 				  void (*sc_free)(struct nfs4_stid *));
782 int nfs4_init_copy_state(struct nfsd_net *nn, struct nfsd4_copy *copy);
783 void nfs4_free_copy_state(struct nfsd4_copy *copy);
784 struct nfs4_cpntf_state *nfs4_alloc_init_cpntf_state(struct nfsd_net *nn,
785 			struct nfs4_stid *p_stid);
786 void nfs4_put_stid(struct nfs4_stid *s);
787 void nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid);
788 void nfs4_remove_reclaim_record(struct nfs4_client_reclaim *, struct nfsd_net *);
789 extern void nfs4_release_reclaim(struct nfsd_net *);
790 extern struct nfs4_client_reclaim *nfsd4_find_reclaim_client(struct xdr_netobj name,
791 							struct nfsd_net *nn);
792 extern __be32 nfs4_check_open_reclaim(struct nfs4_client *);
793 extern void nfsd4_probe_callback(struct nfs4_client *clp);
794 extern void nfsd4_probe_callback_sync(struct nfs4_client *clp);
795 extern void nfsd4_change_callback(struct nfs4_client *clp, struct nfs4_cb_conn *);
796 extern void nfsd41_cb_referring_call(struct nfsd4_callback *cb,
797 				     struct nfs4_sessionid *sessionid,
798 				     u32 slotid, u32 seqno);
799 extern void nfsd41_cb_destroy_referring_call_list(struct nfsd4_callback *cb);
800 extern void nfsd4_init_cb(struct nfsd4_callback *cb, struct nfs4_client *clp,
801 		const struct nfsd4_callback_ops *ops, enum nfsd4_cb_op op);
802 extern bool nfsd4_run_cb(struct nfsd4_callback *cb);
803 
804 static inline void nfsd4_try_run_cb(struct nfsd4_callback *cb)
805 {
806 	if (!test_and_set_bit(NFSD4_CALLBACK_RUNNING, &cb->cb_flags))
807 		WARN_ON_ONCE(!nfsd4_run_cb(cb));
808 }
809 
810 extern void nfsd4_shutdown_callback(struct nfs4_client *);
811 extern void nfsd4_shutdown_copy(struct nfs4_client *clp);
812 void nfsd4_async_copy_reaper(struct nfsd_net *nn);
813 bool nfsd4_has_active_async_copies(struct nfs4_client *clp);
814 extern struct nfs4_client_reclaim *nfs4_client_to_reclaim(struct xdr_netobj name,
815 				struct xdr_netobj princhash, struct nfsd_net *nn);
816 extern bool nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn);
817 
818 void put_nfs4_file(struct nfs4_file *fi);
819 extern void nfs4_put_cpntf_state(struct nfsd_net *nn,
820 				 struct nfs4_cpntf_state *cps);
821 extern __be32 manage_cpntf_state(struct nfsd_net *nn, stateid_t *st,
822 				 struct nfs4_client *clp,
823 				 struct nfs4_cpntf_state **cps);
824 static inline void get_nfs4_file(struct nfs4_file *fi)
825 {
826 	refcount_inc(&fi->fi_ref);
827 }
828 struct nfsd_file *find_any_file(struct nfs4_file *f);
829 
830 #ifdef CONFIG_NFSD_V4
831 void nfsd4_revoke_states(struct net *net, struct super_block *sb);
832 #else
833 static inline void nfsd4_revoke_states(struct net *net, struct super_block *sb)
834 {
835 }
836 #endif
837 
838 /* grace period management */
839 void nfsd4_end_grace(struct nfsd_net *nn);
840 
841 /* nfs4recover operations */
842 extern int nfsd4_client_tracking_init(struct net *net);
843 extern void nfsd4_client_tracking_exit(struct net *net);
844 extern void nfsd4_client_record_create(struct nfs4_client *clp);
845 extern void nfsd4_client_record_remove(struct nfs4_client *clp);
846 extern int nfsd4_client_record_check(struct nfs4_client *clp);
847 extern void nfsd4_record_grace_done(struct nfsd_net *nn);
848 
849 static inline bool try_to_expire_client(struct nfs4_client *clp)
850 {
851 	cmpxchg(&clp->cl_state, NFSD4_COURTESY, NFSD4_EXPIRABLE);
852 	return clp->cl_state == NFSD4_EXPIRABLE;
853 }
854 
855 extern __be32 nfsd4_deleg_getattr_conflict(struct svc_rqst *rqstp,
856 		struct dentry *dentry, struct nfs4_delegation **pdp);
857 #endif   /* NFSD4_STATE_H */
858