1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
273e39aaaSTrond Myklebust /*
373e39aaaSTrond Myklebust * fs/nfs/nfs4session.h
473e39aaaSTrond Myklebust *
573e39aaaSTrond Myklebust * Copyright (c) 2012 Trond Myklebust <Trond.Myklebust@netapp.com>
673e39aaaSTrond Myklebust *
773e39aaaSTrond Myklebust */
873e39aaaSTrond Myklebust #ifndef __LINUX_FS_NFS_NFS4SESSION_H
973e39aaaSTrond Myklebust #define __LINUX_FS_NFS_NFS4SESSION_H
1073e39aaaSTrond Myklebust
1176e697baSTrond Myklebust /* maximum number of slots to use */
12bc4b2a86SAndy Adamson #define NFS4_DEF_SLOT_TABLE_SIZE (64U)
13067c4696STrond Myklebust #define NFS4_DEF_CB_SLOT_TABLE_SIZE (16U)
140ca3f482STrond Myklebust #define NFS4_MAX_SLOT_TABLE (1024U)
15*f96f8cc4STrond Myklebust #define NFS4_MAX_SLOTID (NFS4_MAX_SLOT_TABLE - 1U)
1676e697baSTrond Myklebust #define NFS4_NO_SLOT ((u32)-1)
1776e697baSTrond Myklebust
1876e697baSTrond Myklebust #if IS_ENABLED(CONFIG_NFS_V4)
1976e697baSTrond Myklebust
2076e697baSTrond Myklebust /* Sessions slot seqid */
2176e697baSTrond Myklebust struct nfs4_slot {
2276e697baSTrond Myklebust struct nfs4_slot_table *table;
2376e697baSTrond Myklebust struct nfs4_slot *next;
2476e697baSTrond Myklebust unsigned long generation;
2576e697baSTrond Myklebust u32 slot_nr;
2676e697baSTrond Myklebust u32 seq_nr;
273453d570STrond Myklebust u32 seq_nr_last_acked;
283453d570STrond Myklebust u32 seq_nr_highest_sent;
293453d570STrond Myklebust unsigned int privileged : 1,
3007e8dcbdSTrond Myklebust seq_done : 1;
3176e697baSTrond Myklebust };
3276e697baSTrond Myklebust
3376e697baSTrond Myklebust /* Sessions */
34774d5f14SAndy Adamson enum nfs4_slot_tbl_state {
35774d5f14SAndy Adamson NFS4_SLOT_TBL_DRAINING,
36774d5f14SAndy Adamson };
37774d5f14SAndy Adamson
381f70ea70SGeliang Tang #define SLOT_TABLE_SZ DIV_ROUND_UP(NFS4_MAX_SLOT_TABLE, BITS_PER_LONG)
3976e697baSTrond Myklebust struct nfs4_slot_table {
4076e697baSTrond Myklebust struct nfs4_session *session; /* Parent session */
4176e697baSTrond Myklebust struct nfs4_slot *slots; /* seqid per slot */
4276e697baSTrond Myklebust unsigned long used_slots[SLOT_TABLE_SZ]; /* used/unused bitmap */
4376e697baSTrond Myklebust spinlock_t slot_tbl_lock;
4476e697baSTrond Myklebust struct rpc_wait_queue slot_tbl_waitq; /* allocators may wait here */
45045d2a6dSTrond Myklebust wait_queue_head_t slot_waitq; /* Completion wait on slot */
4676e697baSTrond Myklebust u32 max_slots; /* # slots in table */
4776e697baSTrond Myklebust u32 max_slotid; /* Max allowed slotid value */
4876e697baSTrond Myklebust u32 highest_used_slotid; /* sent to server on each SEQ.
4976e697baSTrond Myklebust * op for dynamic resizing */
5076e697baSTrond Myklebust u32 target_highest_slotid; /* Server max_slot target */
5176e697baSTrond Myklebust u32 server_highest_slotid; /* Server highest slotid */
521fa80644STrond Myklebust s32 d_target_highest_slotid; /* Derivative */
531fa80644STrond Myklebust s32 d2_target_highest_slotid; /* 2nd derivative */
5476e697baSTrond Myklebust unsigned long generation; /* Generation counter for
5576e697baSTrond Myklebust target_highest_slotid */
5676e697baSTrond Myklebust struct completion complete;
57774d5f14SAndy Adamson unsigned long slot_tbl_state;
5876e697baSTrond Myklebust };
5976e697baSTrond Myklebust
6076e697baSTrond Myklebust /*
6176e697baSTrond Myklebust * Session related parameters
6276e697baSTrond Myklebust */
6376e697baSTrond Myklebust struct nfs4_session {
6476e697baSTrond Myklebust struct nfs4_sessionid sess_id;
6576e697baSTrond Myklebust u32 flags;
6676e697baSTrond Myklebust unsigned long session_state;
6776e697baSTrond Myklebust u32 hash_alg;
6876e697baSTrond Myklebust u32 ssv_len;
6976e697baSTrond Myklebust
7076e697baSTrond Myklebust /* The fore and back channel */
7176e697baSTrond Myklebust struct nfs4_channel_attrs fc_attrs;
7276e697baSTrond Myklebust struct nfs4_slot_table fc_slot_table;
7376e697baSTrond Myklebust struct nfs4_channel_attrs bc_attrs;
7476e697baSTrond Myklebust struct nfs4_slot_table bc_slot_table;
7576e697baSTrond Myklebust struct nfs_client *clp;
7676e697baSTrond Myklebust };
7776e697baSTrond Myklebust
7876e697baSTrond Myklebust enum nfs4_session_state {
7976e697baSTrond Myklebust NFS4_SESSION_INITING,
80e11259f9STrond Myklebust NFS4_SESSION_ESTABLISHED,
8176e697baSTrond Myklebust };
8276e697baSTrond Myklebust
83744aa525SChuck Lever extern int nfs4_setup_slot_table(struct nfs4_slot_table *tbl,
84744aa525SChuck Lever unsigned int max_reqs, const char *queue);
8520b9a902STrond Myklebust extern void nfs4_shutdown_slot_table(struct nfs4_slot_table *tbl);
8673e39aaaSTrond Myklebust extern struct nfs4_slot *nfs4_alloc_slot(struct nfs4_slot_table *tbl);
87810d82e6STrond Myklebust extern struct nfs4_slot *nfs4_lookup_slot(struct nfs4_slot_table *tbl, u32 slotid);
88045d2a6dSTrond Myklebust extern int nfs4_slot_wait_on_seqid(struct nfs4_slot_table *tbl,
89045d2a6dSTrond Myklebust u32 slotid, u32 seq_nr,
90045d2a6dSTrond Myklebust unsigned long timeout);
91810d82e6STrond Myklebust extern bool nfs4_try_to_lock_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *slot);
9273e39aaaSTrond Myklebust extern void nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *slot);
939d33059cSChuck Lever extern void nfs4_slot_tbl_drain_complete(struct nfs4_slot_table *tbl);
949d33059cSChuck Lever bool nfs41_wake_and_assign_slot(struct nfs4_slot_table *tbl,
959d33059cSChuck Lever struct nfs4_slot *slot);
969d33059cSChuck Lever void nfs41_wake_slot_table(struct nfs4_slot_table *tbl);
979d33059cSChuck Lever
nfs4_slot_tbl_draining(struct nfs4_slot_table * tbl)989d33059cSChuck Lever static inline bool nfs4_slot_tbl_draining(struct nfs4_slot_table *tbl)
999d33059cSChuck Lever {
1009d33059cSChuck Lever return !!test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state);
1019d33059cSChuck Lever }
1029d33059cSChuck Lever
nfs4_test_locked_slot(const struct nfs4_slot_table * tbl,u32 slotid)103810d82e6STrond Myklebust static inline bool nfs4_test_locked_slot(const struct nfs4_slot_table *tbl,
104810d82e6STrond Myklebust u32 slotid)
105810d82e6STrond Myklebust {
106810d82e6STrond Myklebust return !!test_bit(slotid, tbl->used_slots);
107810d82e6STrond Myklebust }
108810d82e6STrond Myklebust
nfs4_get_session(const struct nfs_client * clp)109172d9de1SAnna Schumaker static inline struct nfs4_session *nfs4_get_session(const struct nfs_client *clp)
110efc6f4aaSAnna Schumaker {
111172d9de1SAnna Schumaker return clp->cl_session;
112efc6f4aaSAnna Schumaker }
113efc6f4aaSAnna Schumaker
1149d33059cSChuck Lever #if defined(CONFIG_NFS_V4_1)
1159d33059cSChuck Lever extern void nfs41_set_target_slotid(struct nfs4_slot_table *tbl,
1169d33059cSChuck Lever u32 target_highest_slotid);
1171cec16abSChuck Lever extern void nfs41_update_target_slotid(struct nfs4_slot_table *tbl,
1181cec16abSChuck Lever struct nfs4_slot *slot,
1191cec16abSChuck Lever struct nfs4_sequence_res *res);
12073e39aaaSTrond Myklebust
12173e39aaaSTrond Myklebust extern int nfs4_setup_session_slot_tables(struct nfs4_session *ses);
12273e39aaaSTrond Myklebust
12373e39aaaSTrond Myklebust extern struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp);
12473e39aaaSTrond Myklebust extern void nfs4_destroy_session(struct nfs4_session *session);
12518aad3d5SAndy Adamson extern int nfs4_init_session(struct nfs_client *clp);
12673e39aaaSTrond Myklebust extern int nfs4_init_ds_session(struct nfs_client *, unsigned long);
12773e39aaaSTrond Myklebust
12876e697baSTrond Myklebust /*
12976e697baSTrond Myklebust * Determine if sessions are in use.
13076e697baSTrond Myklebust */
nfs4_has_session(const struct nfs_client * clp)13176e697baSTrond Myklebust static inline int nfs4_has_session(const struct nfs_client *clp)
13276e697baSTrond Myklebust {
13376e697baSTrond Myklebust if (clp->cl_session)
13476e697baSTrond Myklebust return 1;
13576e697baSTrond Myklebust return 0;
13676e697baSTrond Myklebust }
13776e697baSTrond Myklebust
nfs4_has_persistent_session(const struct nfs_client * clp)13876e697baSTrond Myklebust static inline int nfs4_has_persistent_session(const struct nfs_client *clp)
13976e697baSTrond Myklebust {
14076e697baSTrond Myklebust if (nfs4_has_session(clp))
14176e697baSTrond Myklebust return (clp->cl_session->flags & SESSION4_PERSIST);
14276e697baSTrond Myklebust return 0;
14376e697baSTrond Myklebust }
14476e697baSTrond Myklebust
nfs4_copy_sessionid(struct nfs4_sessionid * dst,const struct nfs4_sessionid * src)14579969dd1STrond Myklebust static inline void nfs4_copy_sessionid(struct nfs4_sessionid *dst,
14679969dd1STrond Myklebust const struct nfs4_sessionid *src)
14779969dd1STrond Myklebust {
14879969dd1STrond Myklebust memcpy(dst->data, src->data, NFS4_MAX_SESSIONID_LEN);
14979969dd1STrond Myklebust }
15079969dd1STrond Myklebust
1512f92ae34STrond Myklebust #ifdef CONFIG_CRC32
1522f92ae34STrond Myklebust /*
1532f92ae34STrond Myklebust * nfs_session_id_hash - calculate the crc32 hash for the session id
1542f92ae34STrond Myklebust * @session - pointer to session
1552f92ae34STrond Myklebust */
1562f92ae34STrond Myklebust #define nfs_session_id_hash(sess_id) \
1572f92ae34STrond Myklebust (~crc32_le(0xFFFFFFFF, &(sess_id)->data[0], sizeof((sess_id)->data)))
1582f92ae34STrond Myklebust #else
1592f92ae34STrond Myklebust #define nfs_session_id_hash(session) (0)
1602f92ae34STrond Myklebust #endif
16173e39aaaSTrond Myklebust #else /* defined(CONFIG_NFS_V4_1) */
16273e39aaaSTrond Myklebust
nfs4_init_session(struct nfs_client * clp)16318aad3d5SAndy Adamson static inline int nfs4_init_session(struct nfs_client *clp)
16473e39aaaSTrond Myklebust {
16573e39aaaSTrond Myklebust return 0;
16673e39aaaSTrond Myklebust }
16773e39aaaSTrond Myklebust
16876e697baSTrond Myklebust /*
16976e697baSTrond Myklebust * Determine if sessions are in use.
17076e697baSTrond Myklebust */
nfs4_has_session(const struct nfs_client * clp)17176e697baSTrond Myklebust static inline int nfs4_has_session(const struct nfs_client *clp)
17276e697baSTrond Myklebust {
17376e697baSTrond Myklebust return 0;
17476e697baSTrond Myklebust }
17576e697baSTrond Myklebust
nfs4_has_persistent_session(const struct nfs_client * clp)17676e697baSTrond Myklebust static inline int nfs4_has_persistent_session(const struct nfs_client *clp)
17776e697baSTrond Myklebust {
17876e697baSTrond Myklebust return 0;
17976e697baSTrond Myklebust }
18076e697baSTrond Myklebust
181ad05cc0fSAnna Schumaker #define nfs_session_id_hash(session) (0)
182ad05cc0fSAnna Schumaker
18373e39aaaSTrond Myklebust #endif /* defined(CONFIG_NFS_V4_1) */
18476e697baSTrond Myklebust #endif /* IS_ENABLED(CONFIG_NFS_V4) */
18573e39aaaSTrond Myklebust #endif /* __LINUX_FS_NFS_NFS4SESSION_H */
186