blob: 9fb7ea74e86c67ce6ab82e60d2ee1bc43aa44e1d [file] [log] [blame]
Namjae Jeone2f34482021-03-16 10:49:09 +09001/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*
3 * Copyright (C) 2016 Namjae Jeon <linkinjeon@kernel.org>
4 * Copyright (C) 2018 Samsung Electronics Co., Ltd.
5 */
6
7#ifndef __KSMBD_OPLOCK_H
8#define __KSMBD_OPLOCK_H
9
10#include "smb_common.h"
11
Namjae Jeon64b39f42021-03-30 14:25:35 +090012#define OPLOCK_WAIT_TIME (35 * HZ)
Namjae Jeone2f34482021-03-16 10:49:09 +090013
14/* SMB Oplock levels */
15#define OPLOCK_NONE 0
16#define OPLOCK_EXCLUSIVE 1
17#define OPLOCK_BATCH 2
18#define OPLOCK_READ 3 /* level 2 oplock */
19
20/* SMB2 Oplock levels */
21#define SMB2_OPLOCK_LEVEL_NONE 0x00
22#define SMB2_OPLOCK_LEVEL_II 0x01
23#define SMB2_OPLOCK_LEVEL_EXCLUSIVE 0x08
24#define SMB2_OPLOCK_LEVEL_BATCH 0x09
25#define SMB2_OPLOCK_LEVEL_LEASE 0xFF
26
27/* Oplock states */
28#define OPLOCK_STATE_NONE 0x00
29#define OPLOCK_ACK_WAIT 0x01
30#define OPLOCK_CLOSING 0x02
31
32#define OPLOCK_WRITE_TO_READ 0x01
33#define OPLOCK_READ_HANDLE_TO_READ 0x02
34#define OPLOCK_WRITE_TO_NONE 0x04
35#define OPLOCK_READ_TO_NONE 0x08
36
37#define SMB2_LEASE_KEY_SIZE 16
38
39struct lease_ctx_info {
Namjae Jeonade62d82021-06-07 09:22:22 +090040 __u8 lease_key[SMB2_LEASE_KEY_SIZE];
41 __le32 req_state;
42 __le32 flags;
43 __le64 duration;
44 __u8 parent_lease_key[SMB2_LEASE_KEY_SIZE];
45 int version;
Namjae Jeone2f34482021-03-16 10:49:09 +090046};
47
48struct lease_table {
49 char client_guid[SMB2_CLIENT_GUID_SIZE];
50 struct list_head lease_list;
51 struct list_head l_entry;
52 spinlock_t lb_lock;
53};
54
55struct lease {
56 __u8 lease_key[SMB2_LEASE_KEY_SIZE];
57 __le32 state;
58 __le32 new_state;
59 __le32 flags;
60 __le64 duration;
Namjae Jeonade62d82021-06-07 09:22:22 +090061 __u8 parent_lease_key[SMB2_LEASE_KEY_SIZE];
62 int version;
63 unsigned short epoch;
Namjae Jeone2f34482021-03-16 10:49:09 +090064 struct lease_table *l_lb;
65};
66
67struct oplock_info {
68 struct ksmbd_conn *conn;
69 struct ksmbd_session *sess;
70 struct ksmbd_work *work;
71 struct ksmbd_file *o_fp;
72 int level;
73 int op_state;
74 unsigned long pending_break;
Namjae Jeon64b39f42021-03-30 14:25:35 +090075 u64 fid;
Namjae Jeone2f34482021-03-16 10:49:09 +090076 atomic_t breaking_cnt;
77 atomic_t refcount;
78 __u16 Tid;
79 bool is_lease;
80 bool open_trunc; /* truncate on open */
81 struct lease *o_lease;
82 struct list_head interim_list;
83 struct list_head op_entry;
84 struct list_head lease_entry;
85 wait_queue_head_t oplock_q; /* Other server threads */
86 wait_queue_head_t oplock_brk; /* oplock breaking wait */
87 struct rcu_head rcu_head;
88};
89
90struct lease_break_info {
91 __le32 curr_state;
92 __le32 new_state;
Namjae Jeonade62d82021-06-07 09:22:22 +090093 __le16 epoch;
Namjae Jeone2f34482021-03-16 10:49:09 +090094 char lease_key[SMB2_LEASE_KEY_SIZE];
95};
96
97struct oplock_break_info {
98 int level;
99 int open_trunc;
100 int fid;
101};
102
Namjae Jeon64b39f42021-03-30 14:25:35 +0900103int smb_grant_oplock(struct ksmbd_work *work, int req_op_level,
Namjae Jeon070fb212021-05-26 17:57:12 +0900104 u64 pid, struct ksmbd_file *fp, __u16 tid,
105 struct lease_ctx_info *lctx, int share_ret);
Namjae Jeon64b39f42021-03-30 14:25:35 +0900106void smb_break_all_levII_oplock(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +0900107 struct ksmbd_file *fp, int is_trunc);
Namjae Jeone2f34482021-03-16 10:49:09 +0900108int opinfo_write_to_read(struct oplock_info *opinfo);
109int opinfo_read_handle_to_read(struct oplock_info *opinfo);
110int opinfo_write_to_none(struct oplock_info *opinfo);
111int opinfo_read_to_none(struct oplock_info *opinfo);
112void close_id_del_oplock(struct ksmbd_file *fp);
113void smb_break_all_oplock(struct ksmbd_work *work, struct ksmbd_file *fp);
114struct oplock_info *opinfo_get(struct ksmbd_file *fp);
115void opinfo_put(struct oplock_info *opinfo);
116
117/* Lease related functions */
118void create_lease_buf(u8 *rbuf, struct lease *lease);
119struct lease_ctx_info *parse_lease_state(void *open_req);
120__u8 smb2_map_lease_to_oplock(__le32 lease_state);
121int lease_read_to_write(struct oplock_info *opinfo);
122
123/* Durable related functions */
124void create_durable_rsp_buf(char *cc);
125void create_durable_v2_rsp_buf(char *cc, struct ksmbd_file *fp);
126void create_mxac_rsp_buf(char *cc, int maximal_access);
127void create_disk_id_rsp_buf(char *cc, __u64 file_id, __u64 vol_id);
128void create_posix_rsp_buf(char *cc, struct ksmbd_file *fp);
129struct create_context *smb2_find_context_vals(void *open_req, const char *str);
Namjae Jeone2f34482021-03-16 10:49:09 +0900130struct oplock_info *lookup_lease_in_table(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +0900131 char *lease_key);
Namjae Jeone2f34482021-03-16 10:49:09 +0900132int find_same_lease_key(struct ksmbd_session *sess, struct ksmbd_inode *ci,
Namjae Jeon070fb212021-05-26 17:57:12 +0900133 struct lease_ctx_info *lctx);
Namjae Jeone2f34482021-03-16 10:49:09 +0900134void destroy_lease_table(struct ksmbd_conn *conn);
135int smb2_check_durable_oplock(struct ksmbd_file *fp,
Namjae Jeon070fb212021-05-26 17:57:12 +0900136 struct lease_ctx_info *lctx, char *name);
Namjae Jeone2f34482021-03-16 10:49:09 +0900137#endif /* __KSMBD_OPLOCK_H */