ksmbd: set epoch in create context v2 lease
[platform/kernel/linux-starfive.git] / fs / smb / server / oplock.h
1 /* 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
12 #define OPLOCK_WAIT_TIME        (35 * HZ)
13
14 /* SMB2 Oplock levels */
15 #define SMB2_OPLOCK_LEVEL_NONE          0x00
16 #define SMB2_OPLOCK_LEVEL_II            0x01
17 #define SMB2_OPLOCK_LEVEL_EXCLUSIVE     0x08
18 #define SMB2_OPLOCK_LEVEL_BATCH         0x09
19 #define SMB2_OPLOCK_LEVEL_LEASE         0xFF
20
21 /* Oplock states */
22 #define OPLOCK_STATE_NONE       0x00
23 #define OPLOCK_ACK_WAIT         0x01
24 #define OPLOCK_CLOSING          0x02
25
26 #define OPLOCK_WRITE_TO_READ            0x01
27 #define OPLOCK_READ_HANDLE_TO_READ      0x02
28 #define OPLOCK_WRITE_TO_NONE            0x04
29 #define OPLOCK_READ_TO_NONE             0x08
30
31 struct lease_ctx_info {
32         __u8                    lease_key[SMB2_LEASE_KEY_SIZE];
33         __le32                  req_state;
34         __le32                  flags;
35         __le64                  duration;
36         __u8                    parent_lease_key[SMB2_LEASE_KEY_SIZE];
37         __le16                  epoch;
38         int                     version;
39 };
40
41 struct lease_table {
42         char                    client_guid[SMB2_CLIENT_GUID_SIZE];
43         struct list_head        lease_list;
44         struct list_head        l_entry;
45         spinlock_t              lb_lock;
46 };
47
48 struct lease {
49         __u8                    lease_key[SMB2_LEASE_KEY_SIZE];
50         __le32                  state;
51         __le32                  new_state;
52         __le32                  flags;
53         __le64                  duration;
54         __u8                    parent_lease_key[SMB2_LEASE_KEY_SIZE];
55         int                     version;
56         unsigned short          epoch;
57         struct lease_table      *l_lb;
58 };
59
60 struct oplock_info {
61         struct ksmbd_conn       *conn;
62         struct ksmbd_session    *sess;
63         struct ksmbd_work       *work;
64         struct ksmbd_file       *o_fp;
65         int                     level;
66         int                     op_state;
67         unsigned long           pending_break;
68         u64                     fid;
69         atomic_t                breaking_cnt;
70         atomic_t                refcount;
71         __u16                   Tid;
72         bool                    is_lease;
73         bool                    open_trunc;     /* truncate on open */
74         struct lease            *o_lease;
75         struct list_head        interim_list;
76         struct list_head        op_entry;
77         struct list_head        lease_entry;
78         wait_queue_head_t oplock_q; /* Other server threads */
79         wait_queue_head_t oplock_brk; /* oplock breaking wait */
80         struct rcu_head         rcu_head;
81 };
82
83 struct lease_break_info {
84         __le32                  curr_state;
85         __le32                  new_state;
86         __le16                  epoch;
87         char                    lease_key[SMB2_LEASE_KEY_SIZE];
88 };
89
90 struct oplock_break_info {
91         int level;
92         int open_trunc;
93         int fid;
94 };
95
96 int smb_grant_oplock(struct ksmbd_work *work, int req_op_level,
97                      u64 pid, struct ksmbd_file *fp, __u16 tid,
98                      struct lease_ctx_info *lctx, int share_ret);
99 void smb_break_all_levII_oplock(struct ksmbd_work *work,
100                                 struct ksmbd_file *fp, int is_trunc);
101 int opinfo_write_to_read(struct oplock_info *opinfo);
102 int opinfo_read_handle_to_read(struct oplock_info *opinfo);
103 int opinfo_write_to_none(struct oplock_info *opinfo);
104 int opinfo_read_to_none(struct oplock_info *opinfo);
105 void close_id_del_oplock(struct ksmbd_file *fp);
106 void smb_break_all_oplock(struct ksmbd_work *work, struct ksmbd_file *fp);
107 struct oplock_info *opinfo_get(struct ksmbd_file *fp);
108 void opinfo_put(struct oplock_info *opinfo);
109
110 /* Lease related functions */
111 void create_lease_buf(u8 *rbuf, struct lease *lease);
112 struct lease_ctx_info *parse_lease_state(void *open_req);
113 __u8 smb2_map_lease_to_oplock(__le32 lease_state);
114 int lease_read_to_write(struct oplock_info *opinfo);
115
116 /* Durable related functions */
117 void create_durable_rsp_buf(char *cc);
118 void create_durable_v2_rsp_buf(char *cc, struct ksmbd_file *fp);
119 void create_mxac_rsp_buf(char *cc, int maximal_access);
120 void create_disk_id_rsp_buf(char *cc, __u64 file_id, __u64 vol_id);
121 void create_posix_rsp_buf(char *cc, struct ksmbd_file *fp);
122 struct create_context *smb2_find_context_vals(void *open_req, const char *tag, int tag_len);
123 struct oplock_info *lookup_lease_in_table(struct ksmbd_conn *conn,
124                                           char *lease_key);
125 int find_same_lease_key(struct ksmbd_session *sess, struct ksmbd_inode *ci,
126                         struct lease_ctx_info *lctx);
127 void destroy_lease_table(struct ksmbd_conn *conn);
128 #endif /* __KSMBD_OPLOCK_H */