162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci *   Copyright (C) 2016 Namjae Jeon <linkinjeon@kernel.org>
462306a36Sopenharmony_ci *   Copyright (C) 2018 Samsung Electronics Co., Ltd.
562306a36Sopenharmony_ci */
662306a36Sopenharmony_ci
762306a36Sopenharmony_ci#ifndef __KSMBD_OPLOCK_H
862306a36Sopenharmony_ci#define __KSMBD_OPLOCK_H
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#include "smb_common.h"
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci#define OPLOCK_WAIT_TIME	(35 * HZ)
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci/* SMB2 Oplock levels */
1562306a36Sopenharmony_ci#define SMB2_OPLOCK_LEVEL_NONE          0x00
1662306a36Sopenharmony_ci#define SMB2_OPLOCK_LEVEL_II            0x01
1762306a36Sopenharmony_ci#define SMB2_OPLOCK_LEVEL_EXCLUSIVE     0x08
1862306a36Sopenharmony_ci#define SMB2_OPLOCK_LEVEL_BATCH         0x09
1962306a36Sopenharmony_ci#define SMB2_OPLOCK_LEVEL_LEASE         0xFF
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci/* Oplock states */
2262306a36Sopenharmony_ci#define OPLOCK_STATE_NONE	0x00
2362306a36Sopenharmony_ci#define OPLOCK_ACK_WAIT		0x01
2462306a36Sopenharmony_ci#define OPLOCK_CLOSING		0x02
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci#define OPLOCK_WRITE_TO_READ		0x01
2762306a36Sopenharmony_ci#define OPLOCK_READ_HANDLE_TO_READ	0x02
2862306a36Sopenharmony_ci#define OPLOCK_WRITE_TO_NONE		0x04
2962306a36Sopenharmony_ci#define OPLOCK_READ_TO_NONE		0x08
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_cistruct lease_ctx_info {
3262306a36Sopenharmony_ci	__u8			lease_key[SMB2_LEASE_KEY_SIZE];
3362306a36Sopenharmony_ci	__le32			req_state;
3462306a36Sopenharmony_ci	__le32			flags;
3562306a36Sopenharmony_ci	__le64			duration;
3662306a36Sopenharmony_ci	__u8			parent_lease_key[SMB2_LEASE_KEY_SIZE];
3762306a36Sopenharmony_ci	__le16			epoch;
3862306a36Sopenharmony_ci	int			version;
3962306a36Sopenharmony_ci	bool			is_dir;
4062306a36Sopenharmony_ci};
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_cistruct lease_table {
4362306a36Sopenharmony_ci	char			client_guid[SMB2_CLIENT_GUID_SIZE];
4462306a36Sopenharmony_ci	struct list_head	lease_list;
4562306a36Sopenharmony_ci	struct list_head	l_entry;
4662306a36Sopenharmony_ci	spinlock_t		lb_lock;
4762306a36Sopenharmony_ci};
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_cistruct lease {
5062306a36Sopenharmony_ci	__u8			lease_key[SMB2_LEASE_KEY_SIZE];
5162306a36Sopenharmony_ci	__le32			state;
5262306a36Sopenharmony_ci	__le32			new_state;
5362306a36Sopenharmony_ci	__le32			flags;
5462306a36Sopenharmony_ci	__le64			duration;
5562306a36Sopenharmony_ci	__u8			parent_lease_key[SMB2_LEASE_KEY_SIZE];
5662306a36Sopenharmony_ci	int			version;
5762306a36Sopenharmony_ci	unsigned short		epoch;
5862306a36Sopenharmony_ci	bool			is_dir;
5962306a36Sopenharmony_ci	struct lease_table	*l_lb;
6062306a36Sopenharmony_ci};
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_cistruct oplock_info {
6362306a36Sopenharmony_ci	struct ksmbd_conn	*conn;
6462306a36Sopenharmony_ci	struct ksmbd_session	*sess;
6562306a36Sopenharmony_ci	struct ksmbd_work	*work;
6662306a36Sopenharmony_ci	struct ksmbd_file	*o_fp;
6762306a36Sopenharmony_ci	int                     level;
6862306a36Sopenharmony_ci	int                     op_state;
6962306a36Sopenharmony_ci	unsigned long		pending_break;
7062306a36Sopenharmony_ci	u64			fid;
7162306a36Sopenharmony_ci	atomic_t		breaking_cnt;
7262306a36Sopenharmony_ci	atomic_t		refcount;
7362306a36Sopenharmony_ci	__u16                   Tid;
7462306a36Sopenharmony_ci	bool			is_lease;
7562306a36Sopenharmony_ci	bool			open_trunc;	/* truncate on open */
7662306a36Sopenharmony_ci	struct lease		*o_lease;
7762306a36Sopenharmony_ci	struct list_head        interim_list;
7862306a36Sopenharmony_ci	struct list_head        op_entry;
7962306a36Sopenharmony_ci	struct list_head        lease_entry;
8062306a36Sopenharmony_ci	wait_queue_head_t oplock_q; /* Other server threads */
8162306a36Sopenharmony_ci	wait_queue_head_t oplock_brk; /* oplock breaking wait */
8262306a36Sopenharmony_ci	struct rcu_head		rcu_head;
8362306a36Sopenharmony_ci};
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_cistruct lease_break_info {
8662306a36Sopenharmony_ci	__le32			curr_state;
8762306a36Sopenharmony_ci	__le32			new_state;
8862306a36Sopenharmony_ci	__le16			epoch;
8962306a36Sopenharmony_ci	char			lease_key[SMB2_LEASE_KEY_SIZE];
9062306a36Sopenharmony_ci};
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_cistruct oplock_break_info {
9362306a36Sopenharmony_ci	int level;
9462306a36Sopenharmony_ci	int open_trunc;
9562306a36Sopenharmony_ci	int fid;
9662306a36Sopenharmony_ci};
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ciint smb_grant_oplock(struct ksmbd_work *work, int req_op_level,
9962306a36Sopenharmony_ci		     u64 pid, struct ksmbd_file *fp, __u16 tid,
10062306a36Sopenharmony_ci		     struct lease_ctx_info *lctx, int share_ret);
10162306a36Sopenharmony_civoid smb_break_all_levII_oplock(struct ksmbd_work *work,
10262306a36Sopenharmony_ci				struct ksmbd_file *fp, int is_trunc);
10362306a36Sopenharmony_ciint opinfo_write_to_read(struct oplock_info *opinfo);
10462306a36Sopenharmony_ciint opinfo_read_handle_to_read(struct oplock_info *opinfo);
10562306a36Sopenharmony_ciint opinfo_write_to_none(struct oplock_info *opinfo);
10662306a36Sopenharmony_ciint opinfo_read_to_none(struct oplock_info *opinfo);
10762306a36Sopenharmony_civoid close_id_del_oplock(struct ksmbd_file *fp);
10862306a36Sopenharmony_civoid smb_break_all_oplock(struct ksmbd_work *work, struct ksmbd_file *fp);
10962306a36Sopenharmony_cistruct oplock_info *opinfo_get(struct ksmbd_file *fp);
11062306a36Sopenharmony_civoid opinfo_put(struct oplock_info *opinfo);
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci/* Lease related functions */
11362306a36Sopenharmony_civoid create_lease_buf(u8 *rbuf, struct lease *lease);
11462306a36Sopenharmony_cistruct lease_ctx_info *parse_lease_state(void *open_req, bool is_dir);
11562306a36Sopenharmony_ci__u8 smb2_map_lease_to_oplock(__le32 lease_state);
11662306a36Sopenharmony_ciint lease_read_to_write(struct oplock_info *opinfo);
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ci/* Durable related functions */
11962306a36Sopenharmony_civoid create_durable_rsp_buf(char *cc);
12062306a36Sopenharmony_civoid create_durable_v2_rsp_buf(char *cc, struct ksmbd_file *fp);
12162306a36Sopenharmony_civoid create_mxac_rsp_buf(char *cc, int maximal_access);
12262306a36Sopenharmony_civoid create_disk_id_rsp_buf(char *cc, __u64 file_id, __u64 vol_id);
12362306a36Sopenharmony_civoid create_posix_rsp_buf(char *cc, struct ksmbd_file *fp);
12462306a36Sopenharmony_cistruct create_context *smb2_find_context_vals(void *open_req, const char *tag, int tag_len);
12562306a36Sopenharmony_cistruct oplock_info *lookup_lease_in_table(struct ksmbd_conn *conn,
12662306a36Sopenharmony_ci					  char *lease_key);
12762306a36Sopenharmony_ciint find_same_lease_key(struct ksmbd_session *sess, struct ksmbd_inode *ci,
12862306a36Sopenharmony_ci			struct lease_ctx_info *lctx);
12962306a36Sopenharmony_civoid destroy_lease_table(struct ksmbd_conn *conn);
13062306a36Sopenharmony_civoid smb_send_parent_lease_break_noti(struct ksmbd_file *fp,
13162306a36Sopenharmony_ci				      struct lease_ctx_info *lctx);
13262306a36Sopenharmony_civoid smb_lazy_parent_lease_break_close(struct ksmbd_file *fp);
13362306a36Sopenharmony_ci#endif /* __KSMBD_OPLOCK_H */
134