1  /* SPDX-License-Identifier: GPL-2.0 */
2  /*
3   * fs/nfs/nfs4session.h
4   *
5   * Copyright (c) 2012 Trond Myklebust <Trond.Myklebust@netapp.com>
6   *
7   */
8  #ifndef __LINUX_FS_NFS_NFS4SESSION_H
9  #define __LINUX_FS_NFS_NFS4SESSION_H
10  
11  /* maximum number of slots to use */
12  #define NFS4_DEF_SLOT_TABLE_SIZE (64U)
13  #define NFS4_DEF_CB_SLOT_TABLE_SIZE (16U)
14  #define NFS4_MAX_SLOT_TABLE (1024U)
15  #define NFS4_MAX_SLOTID (NFS4_MAX_SLOT_TABLE - 1U)
16  #define NFS4_NO_SLOT ((u32)-1)
17  
18  #if IS_ENABLED(CONFIG_NFS_V4)
19  
20  /* Sessions slot seqid */
21  struct nfs4_slot {
22  	struct nfs4_slot_table	*table;
23  	struct nfs4_slot	*next;
24  	unsigned long		generation;
25  	u32			slot_nr;
26  	u32		 	seq_nr;
27  	u32		 	seq_nr_last_acked;
28  	u32		 	seq_nr_highest_sent;
29  	unsigned int		privileged : 1,
30  				seq_done : 1;
31  };
32  
33  /* Sessions */
34  enum nfs4_slot_tbl_state {
35  	NFS4_SLOT_TBL_DRAINING,
36  };
37  
38  #define SLOT_TABLE_SZ DIV_ROUND_UP(NFS4_MAX_SLOT_TABLE, BITS_PER_LONG)
39  struct nfs4_slot_table {
40  	struct nfs4_session *session;		/* Parent session */
41  	struct nfs4_slot *slots;		/* seqid per slot */
42  	unsigned long   used_slots[SLOT_TABLE_SZ]; /* used/unused bitmap */
43  	spinlock_t	slot_tbl_lock;
44  	struct rpc_wait_queue	slot_tbl_waitq;	/* allocators may wait here */
45  	wait_queue_head_t	slot_waitq;	/* Completion wait on slot */
46  	u32		max_slots;		/* # slots in table */
47  	u32		max_slotid;		/* Max allowed slotid value */
48  	u32		highest_used_slotid;	/* sent to server on each SEQ.
49  						 * op for dynamic resizing */
50  	u32		target_highest_slotid;	/* Server max_slot target */
51  	u32		server_highest_slotid;	/* Server highest slotid */
52  	s32		d_target_highest_slotid; /* Derivative */
53  	s32		d2_target_highest_slotid; /* 2nd derivative */
54  	unsigned long	generation;		/* Generation counter for
55  						   target_highest_slotid */
56  	struct completion complete;
57  	unsigned long	slot_tbl_state;
58  };
59  
60  /*
61   * Session related parameters
62   */
63  struct nfs4_session {
64  	struct nfs4_sessionid		sess_id;
65  	u32				flags;
66  	unsigned long			session_state;
67  	u32				hash_alg;
68  	u32				ssv_len;
69  
70  	/* The fore and back channel */
71  	struct nfs4_channel_attrs	fc_attrs;
72  	struct nfs4_slot_table		fc_slot_table;
73  	struct nfs4_channel_attrs	bc_attrs;
74  	struct nfs4_slot_table		bc_slot_table;
75  	struct nfs_client		*clp;
76  };
77  
78  enum nfs4_session_state {
79  	NFS4_SESSION_INITING,
80  	NFS4_SESSION_ESTABLISHED,
81  };
82  
83  extern int nfs4_setup_slot_table(struct nfs4_slot_table *tbl,
84  		unsigned int max_reqs, const char *queue);
85  extern void nfs4_shutdown_slot_table(struct nfs4_slot_table *tbl);
86  extern struct nfs4_slot *nfs4_alloc_slot(struct nfs4_slot_table *tbl);
87  extern struct nfs4_slot *nfs4_lookup_slot(struct nfs4_slot_table *tbl, u32 slotid);
88  extern int nfs4_slot_wait_on_seqid(struct nfs4_slot_table *tbl,
89  		u32 slotid, u32 seq_nr,
90  		unsigned long timeout);
91  extern bool nfs4_try_to_lock_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *slot);
92  extern void nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *slot);
93  extern void nfs4_slot_tbl_drain_complete(struct nfs4_slot_table *tbl);
94  bool nfs41_wake_and_assign_slot(struct nfs4_slot_table *tbl,
95  		struct nfs4_slot *slot);
96  void nfs41_wake_slot_table(struct nfs4_slot_table *tbl);
97  
nfs4_slot_tbl_draining(struct nfs4_slot_table * tbl)98  static inline bool nfs4_slot_tbl_draining(struct nfs4_slot_table *tbl)
99  {
100  	return !!test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state);
101  }
102  
nfs4_test_locked_slot(const struct nfs4_slot_table * tbl,u32 slotid)103  static inline bool nfs4_test_locked_slot(const struct nfs4_slot_table *tbl,
104  		u32 slotid)
105  {
106  	return !!test_bit(slotid, tbl->used_slots);
107  }
108  
nfs4_get_session(const struct nfs_client * clp)109  static inline struct nfs4_session *nfs4_get_session(const struct nfs_client *clp)
110  {
111  	return clp->cl_session;
112  }
113  
114  #if defined(CONFIG_NFS_V4_1)
115  extern void nfs41_set_target_slotid(struct nfs4_slot_table *tbl,
116  		u32 target_highest_slotid);
117  extern void nfs41_update_target_slotid(struct nfs4_slot_table *tbl,
118  		struct nfs4_slot *slot,
119  		struct nfs4_sequence_res *res);
120  
121  extern int nfs4_setup_session_slot_tables(struct nfs4_session *ses);
122  
123  extern struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp);
124  extern void nfs4_destroy_session(struct nfs4_session *session);
125  extern int nfs4_init_session(struct nfs_client *clp);
126  extern int nfs4_init_ds_session(struct nfs_client *, unsigned long);
127  
128  /*
129   * Determine if sessions are in use.
130   */
nfs4_has_session(const struct nfs_client * clp)131  static inline int nfs4_has_session(const struct nfs_client *clp)
132  {
133  	if (clp->cl_session)
134  		return 1;
135  	return 0;
136  }
137  
nfs4_has_persistent_session(const struct nfs_client * clp)138  static inline int nfs4_has_persistent_session(const struct nfs_client *clp)
139  {
140  	if (nfs4_has_session(clp))
141  		return (clp->cl_session->flags & SESSION4_PERSIST);
142  	return 0;
143  }
144  
nfs4_copy_sessionid(struct nfs4_sessionid * dst,const struct nfs4_sessionid * src)145  static inline void nfs4_copy_sessionid(struct nfs4_sessionid *dst,
146  		const struct nfs4_sessionid *src)
147  {
148  	memcpy(dst->data, src->data, NFS4_MAX_SESSIONID_LEN);
149  }
150  
151  #ifdef CONFIG_CRC32
152  /*
153   * nfs_session_id_hash - calculate the crc32 hash for the session id
154   * @session - pointer to session
155   */
156  #define nfs_session_id_hash(sess_id) \
157  	(~crc32_le(0xFFFFFFFF, &(sess_id)->data[0], sizeof((sess_id)->data)))
158  #else
159  #define nfs_session_id_hash(session) (0)
160  #endif
161  #else /* defined(CONFIG_NFS_V4_1) */
162  
nfs4_init_session(struct nfs_client * clp)163  static inline int nfs4_init_session(struct nfs_client *clp)
164  {
165  	return 0;
166  }
167  
168  /*
169   * Determine if sessions are in use.
170   */
nfs4_has_session(const struct nfs_client * clp)171  static inline int nfs4_has_session(const struct nfs_client *clp)
172  {
173  	return 0;
174  }
175  
nfs4_has_persistent_session(const struct nfs_client * clp)176  static inline int nfs4_has_persistent_session(const struct nfs_client *clp)
177  {
178  	return 0;
179  }
180  
181  #define nfs_session_id_hash(session) (0)
182  
183  #endif /* defined(CONFIG_NFS_V4_1) */
184  #endif /* IS_ENABLED(CONFIG_NFS_V4) */
185  #endif /* __LINUX_FS_NFS_NFS4SESSION_H */
186