xref: /linux/fs/nfsd/state.h (revision 5d5a6e996e7c36bc2f7b035743c16f2bf03a227d)
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 <crypto/md5.h>
39 
40 #include <linux/filelock.h>
41 #include <linux/idr.h>
42 #include <linux/refcount.h>
43 #include <linux/sunrpc/svc_xprt.h>
44 
45 #include "nfsfh.h"
46 #include "nfsd.h"
47 
48 /*
49  * Before processing a COMPOUND operation, we have to check that there
50  * is enough space in the buffer for XDR encode to succeed.  otherwise,
51  * we might process an operation with side effects, and be unable to
52  * tell the client that the operation succeeded.
53  *
54  * COMPOUND_ERR_SLACK_SPACE - this is the minimum bytes of buffer space
55  * needed to encode an operation which has failed with NFS4ERR_RESOURCE.
56  * care is taken to ensure that we never fall below this level for any
57  * reason.
58  */
59 #define COMPOUND_ERR_SLACK_SPACE	16     /* OP_SETATTR */
60 
61 #define NFSD_LAUNDROMAT_MINTIMEOUT      1   /* seconds */
62 #define	NFSD_CLIENT_MAX_TRIM_PER_RUN	128
63 #define	NFS4_CLIENTS_PER_GB		1024
64 #define NFSD_DELEGRETURN_TIMEOUT	(HZ / 34)	/* 30ms */
65 #define	NFSD_CB_GETATTR_TIMEOUT		NFSD_DELEGRETURN_TIMEOUT
66 
67 typedef struct {
68 	u32             cl_boot;
69 	u32             cl_id;
70 } clientid_t;
71 
72 typedef struct {
73 	clientid_t	so_clid;
74 	u32		so_id;
75 } stateid_opaque_t;
76 
77 typedef struct {
78 	u32                     si_generation;
79 	stateid_opaque_t        si_opaque;
80 } stateid_t;
81 
82 typedef struct {
83 	stateid_t		cs_stid;
84 #define NFS4_COPYNOTIFY_STID 2
85 	unsigned char		cs_type;
86 	refcount_t		cs_count;
87 } copy_stateid_t;
88 
89 struct nfsd4_referring_call {
90 	struct list_head	__list;
91 
92 	u32			rc_sequenceid;
93 	u32			rc_slotid;
94 };
95 
96 struct nfsd4_referring_call_list {
97 	struct list_head	__list;
98 
99 	struct nfs4_sessionid	rcl_sessionid;
100 	int			__nr_referring_calls;
101 	struct list_head	rcl_referring_calls;
102 };
103 
104 struct nfsd4_callback {
105 	struct nfs4_client *cb_clp;
106 	struct rpc_message cb_msg;
107 #define NFSD4_CALLBACK_RUNNING		(0)
108 #define NFSD4_CALLBACK_WAKE		(1)
109 #define NFSD4_CALLBACK_REQUEUE		(2)
110 	unsigned long cb_flags;
111 	const struct nfsd4_callback_ops *cb_ops;
112 	struct work_struct cb_work;
113 	int cb_seq_status;
114 	int cb_status;
115 	int cb_held_slot;
116 
117 	int cb_nr_referring_call_list;
118 	struct list_head cb_referring_call_list;
119 };
120 
121 struct nfsd4_callback_ops {
122 	bool (*prepare)(struct nfsd4_callback *cb);
123 	int (*done)(struct nfsd4_callback *cb, struct rpc_task *task);
124 	void (*release)(struct nfsd4_callback *cb);
125 	uint32_t opcode;
126 };
127 
128 /*
129  * A core object that represents a "common" stateid. These are generally
130  * embedded within the different (more specific) stateid objects and contain
131  * fields that are of general use to any stateid.
132  */
133 struct nfs4_stid {
134 	refcount_t		sc_count;
135 
136 	/* A new stateid is added to the cl_stateids idr early before it
137 	 * is fully initialised.  Its sc_type is then zero.  After
138 	 * initialisation the sc_type it set under cl_lock, and then
139 	 * never changes.
140 	 */
141 #define SC_TYPE_OPEN		BIT(0)
142 #define SC_TYPE_LOCK		BIT(1)
143 #define SC_TYPE_DELEG		BIT(2)
144 #define SC_TYPE_LAYOUT		BIT(3)
145 #define SC_TYPE_COPY		BIT(4)
146 	unsigned short		sc_type;
147 
148 /*
149  * nn->deleg_lock protects sc_status for hashed delegation stateids.
150  * ->cl_lock protects the bits set as one is disposed of
151  * (SC_STATUS_CLOSED, SC_STATUS_FREEABLE, SC_STATUS_FREED) and
152  * sc_status for open and lock stateids. ->st_mutex also protects
153  * sc_status for open stateids. ->ls_lock protects sc_status for
154  * layout stateids.
155  */
156 /*
157  * For an open stateid kept around *only* to process close replays.
158  * For deleg stateid, kept in idr until last reference is dropped.
159  */
160 #define SC_STATUS_CLOSED	BIT(0)
161 /* For a deleg stateid kept around only to process free_stateid's: */
162 #define SC_STATUS_REVOKED	BIT(1)
163 #define SC_STATUS_ADMIN_REVOKED	BIT(2)
164 #define SC_STATUS_FREEABLE	BIT(3)
165 #define SC_STATUS_FREED		BIT(4)
166 	unsigned short		sc_status;
167 
168 	struct list_head	sc_cp_list;
169 	stateid_t		sc_stateid;
170 	spinlock_t		sc_lock;
171 	struct nfs4_client	*sc_client;
172 	struct nfs4_file	*sc_file;
173 	struct svc_export	*sc_export;
174 	void			(*sc_free)(struct nfs4_stid *);
175 };
176 
177 /* Keep a list of stateids issued by the COPY_NOTIFY, associate it with the
178  * parent OPEN/LOCK/DELEG stateid.
179  */
180 struct nfs4_cpntf_state {
181 	copy_stateid_t		cp_stateid;
182 	struct list_head	cp_list;	/* per parent nfs4_stid */
183 	stateid_t		cp_p_stateid;	/* copy of parent's stateid */
184 	clientid_t		cp_p_clid;	/* copy of parent's clid */
185 	time64_t		cpntf_time;	/* last time stateid used */
186 };
187 
188 /*
189  * RFC 7862 Section 4.8 states:
190  *
191  * | A copy offload stateid will be valid until either (A) the client
192  * | or server restarts or (B) the client returns the resource by
193  * | issuing an OFFLOAD_CANCEL operation or the client replies to a
194  * | CB_OFFLOAD operation.
195  *
196  * Because a client might not reply to a CB_OFFLOAD, or a reply
197  * might get lost due to connection loss, NFSD purges async copy
198  * state after a short period to prevent it from accumulating
199  * over time.
200  */
201 #define NFSD_COPY_INITIAL_TTL 10
202 
203 struct nfs4_cb_fattr {
204 	struct nfsd4_callback ncf_getattr;
205 	u32 ncf_cb_status;
206 
207 	/* from CB_GETATTR reply */
208 	u64 ncf_cb_change;
209 	u64 ncf_cb_fsize;
210 	struct timespec64 ncf_cb_mtime;
211 	struct timespec64 ncf_cb_atime;
212 
213 	bool ncf_file_modified;
214 	u64 ncf_initial_cinfo;
215 	u64 ncf_cur_fsize;
216 };
217 
218 /*
219  * FIXME: the current backchannel encoder can't handle a send buffer longer
220  *        than a single page (see bc_malloc/bc_free).
221  */
222 #define NOTIFY4_EVENT_QUEUE_SIZE	3
223 #define NOTIFY4_PAGE_ARRAY_SIZE		1
224 
225 struct nfsd_notify_event {
226 	refcount_t	ne_ref;		// refcount
227 	u32		ne_mask;	// FS_* mask from fsnotify callback
228 	struct dentry	*ne_dentry;	// dentry reference to target
229 	struct inode	*ne_target;	// inode overwritten by rename, or NULL
230 	u32		ne_namelen;	// length of ne_name (old name for a rename)
231 	u32		ne_newnamelen;	// length of new name (rename only), else 0
232 	char		ne_name[];	// entry name, then new name (rename only)
233 };
234 
235 /*
236  * For a rename, the new name is snapshotted at event-alloc time and stored
237  * immediately after the (NUL-terminated) old name in ne_name[]. ne_dentry can
238  * be renamed again before the CB_NOTIFY work runs, so the new name must not be
239  * read from the live dentry at encode time.
240  */
241 static inline char *nfsd_notify_event_newname(struct nfsd_notify_event *ne)
242 {
243 	return ne->ne_name + ne->ne_namelen + 1;
244 }
245 
246 static inline struct nfsd_notify_event *nfsd_notify_event_get(struct nfsd_notify_event *ne)
247 {
248 	refcount_inc(&ne->ne_ref);
249 	return ne;
250 }
251 
252 static inline void nfsd_notify_event_put(struct nfsd_notify_event *ne)
253 {
254 	if (refcount_dec_and_test(&ne->ne_ref)) {
255 		iput(ne->ne_target);
256 		dput(ne->ne_dentry);
257 		kfree(ne);
258 	}
259 }
260 
261 /*
262  * Represents a directory delegation. The callback is for handling CB_NOTIFYs.
263  * As notifications from fsnotify come in, allocate a new event, take the ncn_lock,
264  * and add it to the ncn_evt queue. The CB_NOTIFY prepare handler will take the
265  * lock, clean out the list and process it.
266  */
267 struct nfsd4_cb_notify {
268 	spinlock_t			ncn_lock;	// protects the evt queue and count
269 	int				ncn_evt_cnt;	// count of events in ncn_evt
270 	int				ncn_nf_cnt;	// count of valid entries in ncn_nf
271 	struct nfsd_notify_event	*ncn_evt[NOTIFY4_EVENT_QUEUE_SIZE]; // list of events
272 	struct page			*ncn_pages[NOTIFY4_PAGE_ARRAY_SIZE]; // for encoding
273 	struct notify4			*ncn_nf;	// array of notify4's to be sent
274 	u32				*ncn_masks;	// host-order notify_mask backing for ncn_nf[]
275 	bool				ncn_encode_err;	// did encoding fail?
276 	struct nfsd4_callback		ncn_cb;		// notify4 callback
277 };
278 
279 /*
280  * Represents a delegation stateid. The nfs4_client holds references to these
281  * and they are put when it is being destroyed or when the delegation is
282  * returned by the client:
283  *
284  * o 1 reference as long as a delegation is still in force (taken when it's
285  *   alloc'd, put when it's returned or revoked)
286  *
287  * o 1 reference as long as a recall rpc is in progress (taken when the lease
288  *   is broken, put when the rpc exits)
289  *
290  * o 1 more ephemeral reference for each nfsd thread currently doing something
291  *   with that delegation without holding the cl_lock
292  *
293  * If the server attempts to recall a delegation and the client doesn't do so
294  * before a timeout, the server may also revoke the delegation. In that case,
295  * the object will either be destroyed (v4.0) or moved to a per-client list of
296  * revoked delegations (v4.1+). A v4.1+ client that rejects the recall holds
297  * no record of the delegation, so the object is destroyed rather than listed.
298  *
299  * This object is a superset of the nfs4_stid.
300  */
301 struct nfs4_delegation {
302 	struct nfs4_stid	dl_stid; /* must be first field */
303 	struct list_head	dl_perfile;
304 	struct list_head	dl_perclnt;
305 	struct list_head	dl_recall_lru;  /* delegation recalled */
306 	struct nfs4_clnt_odstate *dl_clnt_odstate;
307 	time64_t		dl_time;
308 	u32			dl_type;
309 	/* For recall: */
310 	int			dl_retries;
311 	struct nfsd4_callback	dl_recall;
312 	bool			dl_recalled;
313 	bool			dl_recall_rejected;
314 	bool			dl_written;
315 	bool			dl_setattr;
316 
317 	/* Forward-channel slot that carried the granting request */
318 	struct {
319 		u32			sessionid_seq;
320 		u32			slotid;
321 		u32			seqid;
322 		bool			valid;
323 		bool			retired_at_send;
324 	} dl_recall_grant;
325 
326 	union {
327 		/* for CB_GETATTR */
328 		struct nfs4_cb_fattr    dl_cb_fattr;
329 		/* for CB_NOTIFY */
330 		struct nfsd4_cb_notify	dl_cb_notify;
331 	};
332 
333 	/* For delegated timestamps */
334 	struct timespec64	dl_atime;
335 	struct timespec64	dl_mtime;
336 	struct timespec64	dl_ctime;
337 
338 	/* For dir delegations */
339 	u32			dl_notify_mask;
340 	u32			dl_child_attrs[2];
341 	u32			dl_dir_attrs[2];
342 };
343 
344 static inline bool deleg_is_read(u32 dl_type)
345 {
346 	return (dl_type == OPEN_DELEGATE_READ || dl_type == OPEN_DELEGATE_READ_ATTRS_DELEG);
347 }
348 
349 static inline bool deleg_is_write(u32 dl_type)
350 {
351 	return (dl_type == OPEN_DELEGATE_WRITE || dl_type == OPEN_DELEGATE_WRITE_ATTRS_DELEG);
352 }
353 
354 static inline bool deleg_attrs_deleg(u32 dl_type)
355 {
356 	return dl_type == OPEN_DELEGATE_READ_ATTRS_DELEG ||
357 	       dl_type == OPEN_DELEGATE_WRITE_ATTRS_DELEG;
358 }
359 
360 bool nfsd4_vet_deleg_time(struct timespec64 *cb, const struct timespec64 *orig,
361 			  const struct timespec64 *now);
362 
363 #define cb_to_delegation(cb) \
364 	container_of(cb, struct nfs4_delegation, dl_recall)
365 
366 /* client delegation callback info */
367 struct nfs4_cb_conn {
368 	/* SETCLIENTID info */
369 	struct sockaddr_storage	cb_addr;
370 	struct sockaddr_storage	cb_saddr;
371 	size_t			cb_addrlen;
372 	u32                     cb_prog; /* used only in 4.0 case;
373 					    per-session otherwise */
374 	u32                     cb_ident;	/* minorversion 0 only */
375 	struct svc_xprt		*cb_xprt;	/* minorversion 1 only */
376 };
377 
378 static inline struct nfs4_delegation *delegstateid(struct nfs4_stid *s)
379 {
380 	return container_of(s, struct nfs4_delegation, dl_stid);
381 }
382 
383 /* Maximum number of slots per session.  This is for sanity-check only.
384  * It could be increased if we had a mechanism to shutdown misbehaving clients.
385  * A large number can be needed to get good throughput on high-latency servers.
386  */
387 #define NFSD_MAX_SLOTS_PER_SESSION	2048
388 /* Maximum  session per slot cache size */
389 #define NFSD_SLOT_CACHE_SIZE		2048
390 /* Maximum number of NFSD_SLOT_CACHE_SIZE slots per session */
391 #define NFSD_CACHE_SIZE_SLOTS_PER_SESSION	32
392 #define NFSD_MAX_MEM_PER_SESSION  \
393 		(NFSD_CACHE_SIZE_SLOTS_PER_SESSION * NFSD_SLOT_CACHE_SIZE)
394 
395 struct nfsd4_slot {
396 	u32	sl_seqid;
397 	__be32	sl_status;
398 	struct svc_cred sl_cred;
399 	u32	sl_index;
400 	u32	sl_datalen;
401 	u16	sl_opcnt;
402 	u16	sl_generation;
403 #define NFSD4_SLOT_INUSE	(1 << 0)
404 #define NFSD4_SLOT_CACHETHIS	(1 << 1)
405 #define NFSD4_SLOT_INITIALIZED	(1 << 2)
406 #define NFSD4_SLOT_CACHED	(1 << 3)
407 #define NFSD4_SLOT_REUSED	(1 << 4)
408 	u8	sl_flags;
409 	char	sl_data[];
410 };
411 
412 struct nfsd4_channel_attrs {
413 	u32		headerpadsz;
414 	u32		maxreq_sz;
415 	u32		maxresp_sz;
416 	u32		maxresp_cached;
417 	u32		maxops;
418 	u32		maxreqs;
419 	u32		nr_rdma_attrs;
420 	u32		rdma_attrs;
421 };
422 
423 struct nfsd4_cb_sec {
424 	u32	flavor; /* (u32)(-1) used to mean "no valid flavor" */
425 	kuid_t	uid;
426 	kgid_t	gid;
427 };
428 
429 struct nfsd4_create_session {
430 	clientid_t			clientid;
431 	struct nfs4_sessionid		sessionid;
432 	u32				seqid;
433 	u32				flags;
434 	struct nfsd4_channel_attrs	fore_channel;
435 	struct nfsd4_channel_attrs	back_channel;
436 	u32				callback_prog;
437 	struct nfsd4_cb_sec		cb_sec;
438 };
439 
440 struct nfsd4_backchannel_ctl {
441 	u32	bc_cb_program;
442 	struct nfsd4_cb_sec		bc_cb_sec;
443 };
444 
445 struct nfsd4_bind_conn_to_session {
446 	struct nfs4_sessionid		sessionid;
447 	u32				dir;
448 };
449 
450 /* The single slot clientid cache structure */
451 struct nfsd4_clid_slot {
452 	u32				sl_seqid;
453 	__be32				sl_status;
454 	struct nfsd4_create_session	sl_cr_ses;
455 };
456 
457 struct nfsd4_conn {
458 	struct list_head cn_persession;
459 	struct svc_xprt *cn_xprt;
460 	struct svc_xpt_user cn_xpt_user;
461 	struct nfsd4_session *cn_session;
462 /* CDFC4_FORE, CDFC4_BACK: */
463 	unsigned char cn_flags;
464 };
465 
466 /* Maximum number of slots that nfsd will use in the backchannel */
467 #define NFSD_BC_SLOT_TABLE_SIZE		(sizeof(u32) * 8)
468 
469 /*
470  * Representation of a v4.1+ session. These are refcounted in a similar fashion
471  * to the nfs4_client. References are only taken when the server is actively
472  * working on the object (primarily during the processing of compounds).
473  */
474 struct nfsd4_session {
475 	atomic_t		se_ref;
476 	spinlock_t		se_lock;
477 	u32			se_cb_slot_avail; /* bitmap of available slots */
478 	u32			se_cb_highest_slot;	/* highest slot client wants */
479 	u32			se_cb_prog;
480 	struct list_head	se_hash;	/* hash by sessionid */
481 	struct list_head	se_perclnt;
482 	struct list_head	se_all_sessions;/* global list of sessions */
483 	struct nfs4_client	*se_client;
484 	struct nfs4_sessionid	se_sessionid;
485 	struct nfsd4_channel_attrs se_fchannel;
486 	struct nfsd4_cb_sec	se_cb_sec;
487 	struct list_head	se_conns;
488 	u32			se_cb_seq_nr[NFSD_BC_SLOT_TABLE_SIZE];
489 	struct xarray		se_slots;	/* forward channel slots */
490 	u16			se_slot_gen;
491 	bool			se_dead;
492 	u32			se_target_maxslots;
493 	struct rcu_head		rcu_head;
494 };
495 
496 /* formatted contents of nfs4_sessionid */
497 struct nfsd4_sessionid {
498 	clientid_t	clientid;
499 	u32		sequence;
500 	u32		reserved;
501 };
502 
503 /* Length of MD5 digest as hex, plus terminating '\0' */
504 #define HEXDIR_LEN	(2 * MD5_DIGEST_SIZE + 1)
505 
506 /*
507  *       State                Meaning                  Where set
508  * --------------------------------------------------------------------------
509  * | NFSD4_ACTIVE      | Confirmed, active    | Default                     |
510  * |------------------- ----------------------------------------------------|
511  * | NFSD4_COURTESY    | Courtesy state.      | nfs4_get_client_reaplist    |
512  * |                   | Lease/lock/share     |                             |
513  * |                   | reservation conflict |                             |
514  * |                   | can cause Courtesy   |                             |
515  * |                   | client to be expired |                             |
516  * |------------------------------------------------------------------------|
517  * | NFSD4_EXPIRABLE   | Courtesy client to be| nfs4_laundromat             |
518  * |                   | expired by Laundromat| try_to_expire_client        |
519  * |                   | due to conflict      |                             |
520  * |------------------------------------------------------------------------|
521  */
522 enum {
523 	NFSD4_ACTIVE = 0,
524 	NFSD4_COURTESY,
525 	NFSD4_EXPIRABLE,
526 };
527 
528 /*
529  * struct nfs4_client - one per client.  Clientids live here.
530  *
531  * The initial object created by an NFS client using SETCLIENTID (for NFSv4.0)
532  * or EXCHANGE_ID (for NFSv4.1+). These objects are refcounted and timestamped.
533  * Each nfsd_net_ns object contains a set of these and they are tracked via
534  * short and long form clientid. They are hashed and searched for under the
535  * per-nfsd_net client_lock spinlock.
536  *
537  * References to it are only held during the processing of compounds, and in
538  * certain other operations. In their "resting state" they have a refcount of
539  * 0. If they are not renewed within a lease period, they become eligible for
540  * destruction by the laundromat.
541  *
542  * These objects can also be destroyed if the client sends certain forms of
543  * SETCLIENTID or EXCHANGE_ID operations.
544  *
545  * Care is taken *not* to do this however when the objects have an elevated
546  * refcount.
547  *
548  * o Each nfs4_client is hashed by clientid
549  *
550  * o Each nfs4_clients is also hashed by name (the opaque quantity initially
551  *   sent by the client to identify itself).
552  *
553  * o cl_perclient list is used to ensure no dangling stateowner references
554  *   when we expire the nfs4_client
555  */
556 struct nfs4_client {
557 	struct list_head	cl_idhash; 	/* hash by cl_clientid.id */
558 	struct rb_node		cl_namenode;	/* link into by-name trees */
559 	struct list_head	*cl_ownerstr_hashtbl;
560 	struct list_head	cl_openowners;
561 	struct idr		cl_stateids;	/* stateid lookup */
562 	struct list_head	cl_delegations;
563 	struct list_head	cl_revoked;	/* unacknowledged, revoked 4.1 state */
564 	struct list_head        cl_lru;         /* tail queue */
565 #ifdef CONFIG_NFSD_PNFS
566 	struct list_head	cl_lo_states;	/* outstanding layout states */
567 	bool			cl_fence_retry_warn;
568 #endif
569 	struct xdr_netobj	cl_name; 	/* id generated by client */
570 	nfs4_verifier		cl_verifier; 	/* generated by client */
571 	time64_t		cl_time;	/* time of last lease renewal */
572 	struct sockaddr_storage	cl_addr; 	/* client ipaddress */
573 	bool			cl_mach_cred;	/* SP4_MACH_CRED in force */
574 	struct svc_cred		cl_cred; 	/* setclientid principal */
575 	clientid_t		cl_clientid;	/* generated by server */
576 	nfs4_verifier		cl_confirm;	/* generated by server */
577 	u32			cl_minorversion;
578 	atomic_t		cl_admin_revoked; /* count of admin-revoked states */
579 	/* NFSv4.1 client implementation id: */
580 	struct xdr_netobj	cl_nii_domain;
581 	struct xdr_netobj	cl_nii_name;
582 	struct timespec64	cl_nii_time;
583 
584 	/* for v4.0 and v4.1 callbacks: */
585 	struct nfs4_cb_conn	cl_cb_conn;
586 #define NFSD4_CLIENT_CB_UPDATE		(0)
587 #define NFSD4_CLIENT_CB_KILL		(1)
588 #define NFSD4_CLIENT_STABLE		(2)	/* client on stable storage */
589 #define NFSD4_CLIENT_RECLAIM_COMPLETE	(3)	/* reclaim_complete done */
590 #define NFSD4_CLIENT_CONFIRMED		(4)	/* client is confirmed */
591 #define NFSD4_CLIENT_UPCALL_LOCK	(5)	/* upcall serialization */
592 #define NFSD4_CLIENT_CB_FLAG_MASK	(1 << NFSD4_CLIENT_CB_UPDATE | \
593 					 1 << NFSD4_CLIENT_CB_KILL)
594 	unsigned long		cl_flags;
595 
596 	struct workqueue_struct *cl_callback_wq;
597 	const struct cred	*cl_cb_cred;
598 	struct rpc_clnt		*cl_cb_client;
599 	u32			cl_cb_ident;
600 #define NFSD4_CB_UP		0
601 #define NFSD4_CB_UNKNOWN	1
602 #define NFSD4_CB_DOWN		2
603 #define NFSD4_CB_FAULT		3
604 	int			cl_cb_state;
605 	struct nfsd4_callback	cl_cb_null;
606 	struct nfsd4_session	__rcu *cl_cb_session;
607 
608 	/* for all client information that callback code might need: */
609 	spinlock_t		cl_lock;
610 
611 	/* for nfs41 */
612 	struct list_head	cl_sessions;
613 	struct nfsd4_clid_slot	cl_cs_slot;	/* create_session slot */
614 	u32			cl_exchange_flags;
615 	/* number of rpc's in progress over an associated session: */
616 	atomic_t		cl_rpc_users;
617 	struct nfsdfs_client	cl_nfsdfs;
618 	struct nfs4_op_map      cl_spo_must_allow;
619 
620 	/* debugging info directory under nfsd/clients/ : */
621 	struct dentry		*cl_nfsd_dentry;
622 	/* 'info' file within that directory. Ref is not counted,
623 	 * but will remain valid iff cl_nfsd_dentry != NULL
624 	 */
625 	struct dentry		*cl_nfsd_info_dentry;
626 
627 	struct rpc_wait_queue	cl_cb_waitq;	/* backchannel callers may */
628 						/* wait here for slots */
629 	struct net		*net;
630 	struct list_head	async_copies;	/* list of async copies */
631 	spinlock_t		async_lock;	/* lock for async copies */
632 	atomic_t		cl_cb_inflight;	/* Outstanding callbacks */
633 
634 	unsigned int		cl_state;
635 	atomic_t		cl_delegs_in_recall;
636 	/* Length of cl_delegations, updated under nn->deleg_lock */
637 	unsigned int		cl_deleg_count;
638 
639 	struct nfsd4_cb_recall_any	*cl_ra;
640 	time64_t		cl_ra_time;
641 #ifdef CONFIG_NFSD_SCSILAYOUT
642 	struct xarray		cl_dev_fences;
643 	struct mutex		cl_fence_mutex;
644 #endif
645 };
646 
647 /* struct nfs4_client_reset
648  * one per old client. Populates reset_str_hashtbl. Filled from conf_id_hashtbl
649  * upon lease reset, or from upcall to state_daemon (to read in state
650  * from non-volitile storage) upon reboot.
651  */
652 struct nfs4_client_reclaim {
653 	struct list_head	cr_strhash;	/* hash by cr_name */
654 	struct nfs4_client	*cr_clp;	/* pointer to associated clp */
655 	struct xdr_netobj	cr_name;	/* recovery dir name */
656 	struct xdr_netobj	cr_princhash;
657 };
658 
659 /*
660  * REPLAY_ISIZE is sized for an OPEN response with delegation:
661  *   4(status) + 8(stateid) + 20(changeinfo) + 4(rflags) +
662  *   8(verifier) + 4(deleg. type) + 8(deleg. stateid) +
663  *   4(deleg. recall flag) + 20(deleg. space limit) +
664  *   ~32(deleg. ace) = 112 bytes
665  *
666  * Some responses can exceed this. A LOCK denial includes the conflicting
667  * lock owner, which can be up to 1024 bytes (NFS4_OPAQUE_LIMIT). When a
668  * response exceeds REPLAY_ISIZE, a buffer is dynamically allocated. If
669  * that allocation fails, only rp_status is saved. Enlarging this constant
670  * increases the size of every nfs4_stateowner.
671  */
672 
673 #define NFSD4_REPLAY_ISIZE       112
674 
675 /*
676  * Replay buffer, where the result of the last seqid-mutating operation
677  * is cached.
678  */
679 struct nfs4_replay {
680 	__be32			rp_status;
681 	unsigned int		rp_buflen;
682 	char			*rp_buf; /* rp_ibuf or kmalloc'd */
683 	struct knfsd_fh		rp_openfh;
684 	int			rp_locked;
685 	char			rp_ibuf[NFSD4_REPLAY_ISIZE];
686 };
687 
688 extern void nfs4_replay_free_cache(struct nfs4_replay *rp);
689 
690 struct nfs4_stateowner;
691 
692 struct nfs4_stateowner_operations {
693 	void (*so_unhash)(struct nfs4_stateowner *);
694 	void (*so_free)(struct nfs4_stateowner *);
695 };
696 
697 /*
698  * A core object that represents either an open or lock owner. The object and
699  * lock owner objects have one of these embedded within them. Refcounts and
700  * other fields common to both owner types are contained within these
701  * structures.
702  */
703 struct nfs4_stateowner {
704 	struct list_head			so_strhash;
705 	struct list_head			so_stateids;
706 	struct nfs4_client			*so_client;
707 	const struct nfs4_stateowner_operations	*so_ops;
708 	/* after increment in nfsd4_bump_seqid, represents the next
709 	 * sequence id expected from the client: */
710 	atomic_t				so_count;
711 	u32					so_seqid;
712 	struct xdr_netobj			so_owner; /* open owner name */
713 	struct nfs4_replay			so_replay;
714 	bool					so_is_open_owner;
715 };
716 
717 /*
718  * When a file is opened, the client provides an open state owner opaque string
719  * that indicates the "owner" of that open. These objects are refcounted.
720  * References to it are held by each open state associated with it. This object
721  * is a superset of the nfs4_stateowner struct.
722  */
723 struct nfs4_openowner {
724 	struct nfs4_stateowner	oo_owner; /* must be first field */
725 	struct list_head        oo_perclient;
726 	/*
727 	 * We keep around openowners a little while after last close,
728 	 * which saves clients from having to confirm, and allows us to
729 	 * handle close replays if they come soon enough.  The close_lru
730 	 * is a list of such openowners, to be reaped by the laundromat
731 	 * thread eventually if they remain unused:
732 	 */
733 	struct list_head	oo_close_lru;
734 	struct nfs4_ol_stateid *oo_last_closed_stid;
735 	time64_t		oo_time; /* time of placement on so_close_lru */
736 #define NFS4_OO_CONFIRMED   1
737 	unsigned char		oo_flags;
738 };
739 
740 /*
741  * Represents a generic "lockowner". Similar to an openowner. References to it
742  * are held by the lock stateids that are created on its behalf. This object is
743  * a superset of the nfs4_stateowner struct.
744  */
745 struct nfs4_lockowner {
746 	struct nfs4_stateowner	lo_owner;	/* must be first element */
747 	struct list_head	lo_blocked;	/* blocked file_locks */
748 };
749 
750 static inline struct nfs4_openowner * openowner(struct nfs4_stateowner *so)
751 {
752 	return container_of(so, struct nfs4_openowner, oo_owner);
753 }
754 
755 static inline struct nfs4_lockowner * lockowner(struct nfs4_stateowner *so)
756 {
757 	return container_of(so, struct nfs4_lockowner, lo_owner);
758 }
759 
760 /*
761  * Per-client state indicating no. of opens and outstanding delegations
762  * on a file from a particular client.'od' stands for 'open & delegation'
763  */
764 struct nfs4_clnt_odstate {
765 	struct nfs4_client	*co_client;
766 	struct nfs4_file	*co_file;
767 	struct list_head	co_perfile;
768 	refcount_t		co_odcount;
769 };
770 
771 /*
772  * nfs4_file: a file opened by some number of (open) nfs4_stateowners.
773  *
774  * These objects are global. nfsd keeps one instance of a nfs4_file per
775  * filehandle (though it may keep multiple file descriptors for each). Each
776  * inode can have multiple filehandles associated with it, so there is
777  * (potentially) a many to one relationship between this struct and struct
778  * inode.
779  */
780 struct nfs4_file {
781 	refcount_t		fi_ref;
782 	struct inode *		fi_inode;
783 	bool			fi_aliased;
784 	spinlock_t		fi_lock;
785 	struct rhlist_head	fi_rlist;
786 	struct list_head        fi_stateids;
787 	union {
788 		struct list_head	fi_delegations;
789 		struct rcu_head		fi_rcu;
790 	};
791 	struct list_head	fi_clnt_odstate;
792 	/* One each for O_RDONLY, O_WRONLY, O_RDWR: */
793 	struct nfsd_file	*fi_fds[3];
794 	/*
795 	 * Each open or lock stateid contributes 0-4 to the counts
796 	 * below depending on which bits are set in st_access_bitmap:
797 	 *     1 to fi_access[O_RDONLY] if NFS4_SHARE_ACCES_READ is set
798 	 *   + 1 to fi_access[O_WRONLY] if NFS4_SHARE_ACCESS_WRITE is set
799 	 *   + 1 to both of the above if NFS4_SHARE_ACCESS_BOTH is set.
800 	 */
801 	atomic_t		fi_access[2];
802 	u32			fi_share_deny;
803 	struct nfsd_file __rcu	*fi_deleg_file;
804 	struct nfsd_file	*fi_rdeleg_file;
805 	int			fi_delegees;
806 	struct knfsd_fh		fi_fhandle;
807 	bool			fi_had_conflict;
808 #ifdef CONFIG_NFSD_PNFS
809 	struct list_head	fi_lo_states;
810 	atomic_t		fi_lo_recalls;
811 #endif
812 };
813 
814 /*
815  * A generic struct representing either a open or lock stateid. The nfs4_client
816  * holds a reference to each of these objects, and they in turn hold a
817  * reference to their respective stateowners. The client's reference is
818  * released in response to a close or unlock (depending on whether it's an open
819  * or lock stateid) or when the client is being destroyed.
820  *
821  * In the case of v4.0 open stateids, these objects are preserved for a little
822  * while after close in order to handle CLOSE replays. Those are eventually
823  * reclaimed via a LRU scheme by the laundromat.
824  *
825  * This object is a superset of the nfs4_stid. "ol" stands for "Open or Lock".
826  * Better suggestions welcome.
827  */
828 struct nfs4_ol_stateid {
829 	struct nfs4_stid		st_stid;
830 	struct list_head		st_perfile;
831 	struct list_head		st_perstateowner;
832 	struct list_head		st_locks;
833 	struct nfs4_stateowner		*st_stateowner;
834 	struct nfs4_clnt_odstate	*st_clnt_odstate;
835 /*
836  * These bitmasks use 3 separate bits for READ, ALLOW, and BOTH; see the
837  * comment above bmap_to_share_mode() for explanation:
838  */
839 	unsigned char			st_access_bmap;
840 	unsigned char			st_deny_bmap;
841 	struct nfs4_ol_stateid		*st_openstp;
842 	struct mutex			st_mutex;
843 };
844 
845 static inline struct nfs4_ol_stateid *openlockstateid(struct nfs4_stid *s)
846 {
847 	return container_of(s, struct nfs4_ol_stateid, st_stid);
848 }
849 
850 struct nfs4_layout_stateid {
851 	struct nfs4_stid		ls_stid;
852 	struct list_head		ls_perclnt;
853 	struct list_head		ls_perfile;
854 	spinlock_t			ls_lock;
855 	struct list_head		ls_layouts;
856 	u32				ls_layout_type;
857 	struct nfsd_file		*ls_file;
858 	struct nfsd4_callback		ls_recall;
859 	stateid_t			ls_recall_sid;
860 	bool				ls_recalled;
861 	struct mutex			ls_mutex;
862 
863 	struct delayed_work		ls_fence_work;
864 	unsigned int			ls_fence_delay;
865 	bool				ls_fenced;
866 	bool				ls_fence_inflight;
867 };
868 
869 static inline struct nfs4_layout_stateid *layoutstateid(struct nfs4_stid *s)
870 {
871 	return container_of(s, struct nfs4_layout_stateid, ls_stid);
872 }
873 
874 /* flags for preprocess_seqid_op() */
875 #define RD_STATE	        0x00000010
876 #define WR_STATE	        0x00000020
877 
878 enum nfsd4_cb_op {
879 	NFSPROC4_CLNT_CB_NULL = 0,
880 	NFSPROC4_CLNT_CB_RECALL,
881 	NFSPROC4_CLNT_CB_LAYOUT,
882 	NFSPROC4_CLNT_CB_OFFLOAD,
883 	NFSPROC4_CLNT_CB_SEQUENCE,
884 	NFSPROC4_CLNT_CB_NOTIFY_LOCK,
885 	NFSPROC4_CLNT_CB_RECALL_ANY,
886 	NFSPROC4_CLNT_CB_GETATTR,
887 	NFSPROC4_CLNT_CB_NOTIFY,
888 };
889 
890 /* Returns true iff a is later than b: */
891 static inline bool nfsd4_stateid_generation_after(stateid_t *a, stateid_t *b)
892 {
893 	return (s32)(a->si_generation - b->si_generation) > 0;
894 }
895 
896 /*
897  * When a client tries to get a lock on a file, we set one of these objects
898  * on the blocking lock. When the lock becomes free, we can then issue a
899  * CB_NOTIFY_LOCK to the server.
900  */
901 struct nfsd4_blocked_lock {
902 	struct list_head	nbl_list;
903 	struct list_head	nbl_lru;
904 	time64_t		nbl_time;
905 	struct file_lock	nbl_lock;
906 	struct knfsd_fh		nbl_fh;
907 	struct nfsd4_callback	nbl_cb;
908 	struct kref		nbl_kref;
909 };
910 
911 struct nfsd4_compound_state;
912 struct nfsd_net;
913 struct nfsd4_copy;
914 struct nfsd4_async_copy;
915 
916 extern __be32 nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
917 		struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
918 		stateid_t *stateid, int flags, struct nfsd_file **filp,
919 		struct nfs4_stid **cstid);
920 __be32 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
921 			    stateid_t *stateid, unsigned short typemask,
922 			    unsigned short statusmask,
923 			    struct nfs4_stid **s, struct nfsd_net *nn);
924 struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
925 				  void (*sc_free)(struct nfs4_stid *));
926 struct nfsd4_async_copy *nfs4_alloc_copy_stid(struct nfs4_client *clp);
927 struct nfs4_cpntf_state *nfs4_alloc_init_cpntf_state(struct nfsd_net *nn,
928 			struct nfs4_stid *p_stid);
929 void nfs4_put_stid(struct nfs4_stid *s);
930 void nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid);
931 void nfs4_remove_reclaim_record(struct nfs4_client_reclaim *, struct nfsd_net *);
932 extern void nfs4_release_reclaim(struct nfsd_net *);
933 extern struct nfs4_client_reclaim *nfsd4_find_reclaim_client(struct xdr_netobj name,
934 							struct nfsd_net *nn);
935 extern __be32 nfs4_check_open_reclaim(struct nfs4_client *);
936 extern void nfsd4_probe_callback(struct nfs4_client *clp);
937 extern void nfsd4_probe_callback_sync(struct nfs4_client *clp);
938 extern void nfsd4_change_callback(struct nfs4_client *clp, struct nfs4_cb_conn *);
939 extern void nfsd41_cb_referring_call(struct nfsd4_callback *cb,
940 				     struct nfs4_sessionid *sessionid,
941 				     u32 slotid, u32 seqno);
942 extern void nfsd41_cb_destroy_referring_call_list(struct nfsd4_callback *cb);
943 extern void nfsd4_init_cb(struct nfsd4_callback *cb, struct nfs4_client *clp,
944 		const struct nfsd4_callback_ops *ops, enum nfsd4_cb_op op);
945 extern bool nfsd4_run_cb(struct nfsd4_callback *cb);
946 
947 static inline void nfsd4_try_run_cb(struct nfsd4_callback *cb)
948 {
949 	if (!test_and_set_bit(NFSD4_CALLBACK_RUNNING, &cb->cb_flags))
950 		WARN_ON_ONCE(!nfsd4_run_cb(cb));
951 }
952 
953 extern void nfsd4_shutdown_callback(struct nfs4_client *);
954 extern void nfsd4_shutdown_copy(struct nfs4_client *clp);
955 void nfsd4_put_client(struct nfs4_client *clp);
956 void nfsd4_async_copy_reaper(struct nfsd_net *nn);
957 bool nfsd4_has_active_async_copies(struct nfs4_client *clp);
958 void nfsd_update_cmtime_attr(struct file *f, unsigned int flags);
959 extern struct nfs4_client_reclaim *nfs4_client_to_reclaim(struct xdr_netobj name,
960 				struct xdr_netobj princhash, struct nfsd_net *nn);
961 extern bool nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn);
962 int nfsd_handle_dir_event(u32 mask, const struct inode *dir, const void *data,
963 			  int data_type, const struct qstr *name);
964 
965 void put_nfs4_file(struct nfs4_file *fi);
966 extern void nfs4_put_cpntf_state(struct nfsd_net *nn,
967 				 struct nfs4_cpntf_state *cps);
968 extern __be32 manage_cpntf_state(struct nfsd_net *nn, stateid_t *st,
969 				 struct nfs4_client *clp,
970 				 struct nfs4_cpntf_state **cps);
971 static inline void get_nfs4_file(struct nfs4_file *fi)
972 {
973 	refcount_inc(&fi->fi_ref);
974 }
975 struct nfsd_file *find_any_file(struct nfs4_file *f);
976 
977 #ifdef CONFIG_NFSD_V4
978 void nfsd4_revoke_states(struct nfsd_net *nn, struct super_block *sb);
979 void nfsd4_revoke_export_states(struct nfsd_net *nn, const struct path *path);
980 void nfsd4_cancel_copy_by_sb(struct net *net, struct super_block *sb);
981 int nfsd_net_cb_init(struct nfsd_net *nn);
982 void nfsd_net_cb_shutdown(struct nfsd_net *nn);
983 #else
984 static inline void nfsd4_revoke_states(struct nfsd_net *nn, struct super_block *sb)
985 {
986 }
987 static inline void nfsd4_revoke_export_states(struct nfsd_net *nn,
988 					      const struct path *path)
989 {
990 }
991 static inline void nfsd4_cancel_copy_by_sb(struct net *net, struct super_block *sb)
992 {
993 }
994 static inline int nfsd_net_cb_init(struct nfsd_net *nn)
995 {
996 	return 0;
997 }
998 static inline void nfsd_net_cb_shutdown(struct nfsd_net *nn)
999 {
1000 }
1001 #endif
1002 
1003 /* grace period management */
1004 bool nfsd4_force_end_grace(struct nfsd_net *nn);
1005 
1006 /* nfs4recover operations */
1007 extern int nfsd4_client_tracking_init(struct net *net);
1008 extern void nfsd4_client_tracking_exit(struct net *net);
1009 extern void nfsd4_client_record_create(struct nfs4_client *clp);
1010 extern void nfsd4_client_record_remove(struct nfs4_client *clp);
1011 extern int nfsd4_client_record_check(struct nfs4_client *clp);
1012 extern void nfsd4_record_grace_done(struct nfsd_net *nn);
1013 
1014 static inline bool try_to_expire_client(struct nfs4_client *clp)
1015 {
1016 	cmpxchg(&clp->cl_state, NFSD4_COURTESY, NFSD4_EXPIRABLE);
1017 	return clp->cl_state == NFSD4_EXPIRABLE;
1018 }
1019 
1020 extern __be32 nfsd4_deleg_getattr_conflict(struct svc_rqst *rqstp,
1021 		struct dentry *dentry, struct nfs4_delegation **pdp);
1022 
1023 struct nfsd4_get_dir_delegation;
1024 struct nfs4_delegation *nfsd_get_dir_deleg(struct nfsd4_compound_state *cstate,
1025 						struct nfsd4_get_dir_delegation *gdd,
1026 						struct nfsd_file *nf);
1027 #endif   /* NFSD4_STATE_H */
1028