Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 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 | #include <linux/moduleparam.h> |
| 8 | |
| 9 | #include "glob.h" |
| 10 | #include "oplock.h" |
| 11 | |
| 12 | #include "smb_common.h" |
| 13 | #include "smbstatus.h" |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 14 | #include "connection.h" |
| 15 | #include "mgmt/user_session.h" |
| 16 | #include "mgmt/share_config.h" |
| 17 | #include "mgmt/tree_connect.h" |
| 18 | |
| 19 | static LIST_HEAD(lease_table_list); |
| 20 | static DEFINE_RWLOCK(lease_list_lock); |
| 21 | |
| 22 | /** |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 23 | * alloc_opinfo() - allocate a new opinfo object for oplock info |
| 24 | * @work: smb work |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 25 | * @id: fid of open file |
| 26 | * @Tid: tree id of connection |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 27 | * |
| 28 | * Return: allocated opinfo object on success, otherwise NULL |
| 29 | */ |
| 30 | static struct oplock_info *alloc_opinfo(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 31 | u64 id, __u16 Tid) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 32 | { |
| 33 | struct ksmbd_session *sess = work->sess; |
| 34 | struct oplock_info *opinfo; |
| 35 | |
| 36 | opinfo = kzalloc(sizeof(struct oplock_info), GFP_KERNEL); |
| 37 | if (!opinfo) |
| 38 | return NULL; |
| 39 | |
| 40 | opinfo->sess = sess; |
| 41 | opinfo->conn = sess->conn; |
Namjae Jeon | 0ae941e | 2021-06-30 09:37:09 +0900 | [diff] [blame] | 42 | opinfo->level = SMB2_OPLOCK_LEVEL_NONE; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 43 | opinfo->op_state = OPLOCK_STATE_NONE; |
| 44 | opinfo->pending_break = 0; |
| 45 | opinfo->fid = id; |
| 46 | opinfo->Tid = Tid; |
| 47 | INIT_LIST_HEAD(&opinfo->op_entry); |
| 48 | INIT_LIST_HEAD(&opinfo->interim_list); |
| 49 | init_waitqueue_head(&opinfo->oplock_q); |
| 50 | init_waitqueue_head(&opinfo->oplock_brk); |
| 51 | atomic_set(&opinfo->refcount, 1); |
| 52 | atomic_set(&opinfo->breaking_cnt, 0); |
| 53 | |
| 54 | return opinfo; |
| 55 | } |
| 56 | |
| 57 | static void lease_add_list(struct oplock_info *opinfo) |
| 58 | { |
| 59 | struct lease_table *lb = opinfo->o_lease->l_lb; |
| 60 | |
| 61 | spin_lock(&lb->lb_lock); |
| 62 | list_add_rcu(&opinfo->lease_entry, &lb->lease_list); |
| 63 | spin_unlock(&lb->lb_lock); |
| 64 | } |
| 65 | |
| 66 | static void lease_del_list(struct oplock_info *opinfo) |
| 67 | { |
| 68 | struct lease_table *lb = opinfo->o_lease->l_lb; |
| 69 | |
| 70 | if (!lb) |
| 71 | return; |
| 72 | |
| 73 | spin_lock(&lb->lb_lock); |
| 74 | if (list_empty(&opinfo->lease_entry)) { |
| 75 | spin_unlock(&lb->lb_lock); |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | list_del_init(&opinfo->lease_entry); |
| 80 | opinfo->o_lease->l_lb = NULL; |
| 81 | spin_unlock(&lb->lb_lock); |
| 82 | } |
| 83 | |
| 84 | static void lb_add(struct lease_table *lb) |
| 85 | { |
| 86 | write_lock(&lease_list_lock); |
| 87 | list_add(&lb->l_entry, &lease_table_list); |
| 88 | write_unlock(&lease_list_lock); |
| 89 | } |
| 90 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 91 | static int alloc_lease(struct oplock_info *opinfo, struct lease_ctx_info *lctx) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 92 | { |
| 93 | struct lease *lease; |
| 94 | |
| 95 | lease = kmalloc(sizeof(struct lease), GFP_KERNEL); |
| 96 | if (!lease) |
| 97 | return -ENOMEM; |
| 98 | |
| 99 | memcpy(lease->lease_key, lctx->lease_key, SMB2_LEASE_KEY_SIZE); |
| 100 | lease->state = lctx->req_state; |
| 101 | lease->new_state = 0; |
| 102 | lease->flags = lctx->flags; |
| 103 | lease->duration = lctx->duration; |
Namjae Jeon | ade62d8 | 2021-06-07 09:22:22 +0900 | [diff] [blame] | 104 | memcpy(lease->parent_lease_key, lctx->parent_lease_key, SMB2_LEASE_KEY_SIZE); |
| 105 | lease->version = lctx->version; |
| 106 | lease->epoch = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 107 | INIT_LIST_HEAD(&opinfo->lease_entry); |
| 108 | opinfo->o_lease = lease; |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | static void free_lease(struct oplock_info *opinfo) |
| 114 | { |
| 115 | struct lease *lease; |
| 116 | |
| 117 | lease = opinfo->o_lease; |
| 118 | kfree(lease); |
| 119 | } |
| 120 | |
| 121 | static void free_opinfo(struct oplock_info *opinfo) |
| 122 | { |
| 123 | if (opinfo->is_lease) |
| 124 | free_lease(opinfo); |
| 125 | kfree(opinfo); |
| 126 | } |
| 127 | |
| 128 | static inline void opinfo_free_rcu(struct rcu_head *rcu_head) |
| 129 | { |
| 130 | struct oplock_info *opinfo; |
| 131 | |
| 132 | opinfo = container_of(rcu_head, struct oplock_info, rcu_head); |
| 133 | free_opinfo(opinfo); |
| 134 | } |
| 135 | |
| 136 | struct oplock_info *opinfo_get(struct ksmbd_file *fp) |
| 137 | { |
| 138 | struct oplock_info *opinfo; |
| 139 | |
| 140 | rcu_read_lock(); |
| 141 | opinfo = rcu_dereference(fp->f_opinfo); |
| 142 | if (opinfo && !atomic_inc_not_zero(&opinfo->refcount)) |
| 143 | opinfo = NULL; |
| 144 | rcu_read_unlock(); |
| 145 | |
| 146 | return opinfo; |
| 147 | } |
| 148 | |
| 149 | static struct oplock_info *opinfo_get_list(struct ksmbd_inode *ci) |
| 150 | { |
| 151 | struct oplock_info *opinfo; |
| 152 | |
| 153 | if (list_empty(&ci->m_op_list)) |
| 154 | return NULL; |
| 155 | |
| 156 | rcu_read_lock(); |
| 157 | opinfo = list_first_or_null_rcu(&ci->m_op_list, struct oplock_info, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 158 | op_entry); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 159 | if (opinfo && !atomic_inc_not_zero(&opinfo->refcount)) |
| 160 | opinfo = NULL; |
| 161 | rcu_read_unlock(); |
| 162 | |
| 163 | return opinfo; |
| 164 | } |
| 165 | |
| 166 | void opinfo_put(struct oplock_info *opinfo) |
| 167 | { |
| 168 | if (!atomic_dec_and_test(&opinfo->refcount)) |
| 169 | return; |
| 170 | |
| 171 | call_rcu(&opinfo->rcu_head, opinfo_free_rcu); |
| 172 | } |
| 173 | |
| 174 | static void opinfo_add(struct oplock_info *opinfo) |
| 175 | { |
| 176 | struct ksmbd_inode *ci = opinfo->o_fp->f_ci; |
| 177 | |
| 178 | write_lock(&ci->m_lock); |
| 179 | list_add_rcu(&opinfo->op_entry, &ci->m_op_list); |
| 180 | write_unlock(&ci->m_lock); |
| 181 | } |
| 182 | |
| 183 | static void opinfo_del(struct oplock_info *opinfo) |
| 184 | { |
| 185 | struct ksmbd_inode *ci = opinfo->o_fp->f_ci; |
| 186 | |
| 187 | if (opinfo->is_lease) { |
| 188 | write_lock(&lease_list_lock); |
| 189 | lease_del_list(opinfo); |
| 190 | write_unlock(&lease_list_lock); |
| 191 | } |
| 192 | write_lock(&ci->m_lock); |
| 193 | list_del_rcu(&opinfo->op_entry); |
| 194 | write_unlock(&ci->m_lock); |
| 195 | } |
| 196 | |
| 197 | static unsigned long opinfo_count(struct ksmbd_file *fp) |
| 198 | { |
| 199 | if (ksmbd_stream_fd(fp)) |
| 200 | return atomic_read(&fp->f_ci->sop_count); |
| 201 | else |
| 202 | return atomic_read(&fp->f_ci->op_count); |
| 203 | } |
| 204 | |
| 205 | static void opinfo_count_inc(struct ksmbd_file *fp) |
| 206 | { |
| 207 | if (ksmbd_stream_fd(fp)) |
| 208 | return atomic_inc(&fp->f_ci->sop_count); |
| 209 | else |
| 210 | return atomic_inc(&fp->f_ci->op_count); |
| 211 | } |
| 212 | |
| 213 | static void opinfo_count_dec(struct ksmbd_file *fp) |
| 214 | { |
| 215 | if (ksmbd_stream_fd(fp)) |
| 216 | return atomic_dec(&fp->f_ci->sop_count); |
| 217 | else |
| 218 | return atomic_dec(&fp->f_ci->op_count); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * opinfo_write_to_read() - convert a write oplock to read oplock |
| 223 | * @opinfo: current oplock info |
| 224 | * |
| 225 | * Return: 0 on success, otherwise -EINVAL |
| 226 | */ |
| 227 | int opinfo_write_to_read(struct oplock_info *opinfo) |
| 228 | { |
| 229 | struct lease *lease = opinfo->o_lease; |
| 230 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 231 | if (!(opinfo->level == SMB2_OPLOCK_LEVEL_BATCH || |
| 232 | opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 233 | pr_err("bad oplock(0x%x)\n", opinfo->level); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 234 | if (opinfo->is_lease) |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 235 | pr_err("lease state(0x%x)\n", lease->state); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 236 | return -EINVAL; |
| 237 | } |
| 238 | opinfo->level = SMB2_OPLOCK_LEVEL_II; |
| 239 | |
| 240 | if (opinfo->is_lease) |
| 241 | lease->state = lease->new_state; |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * opinfo_read_handle_to_read() - convert a read/handle oplock to read oplock |
| 247 | * @opinfo: current oplock info |
| 248 | * |
| 249 | * Return: 0 on success, otherwise -EINVAL |
| 250 | */ |
| 251 | int opinfo_read_handle_to_read(struct oplock_info *opinfo) |
| 252 | { |
| 253 | struct lease *lease = opinfo->o_lease; |
| 254 | |
| 255 | lease->state = lease->new_state; |
| 256 | opinfo->level = SMB2_OPLOCK_LEVEL_II; |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * opinfo_write_to_none() - convert a write oplock to none |
| 262 | * @opinfo: current oplock info |
| 263 | * |
| 264 | * Return: 0 on success, otherwise -EINVAL |
| 265 | */ |
| 266 | int opinfo_write_to_none(struct oplock_info *opinfo) |
| 267 | { |
| 268 | struct lease *lease = opinfo->o_lease; |
| 269 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 270 | if (!(opinfo->level == SMB2_OPLOCK_LEVEL_BATCH || |
| 271 | opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 272 | pr_err("bad oplock(0x%x)\n", opinfo->level); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 273 | if (opinfo->is_lease) |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 274 | pr_err("lease state(0x%x)\n", lease->state); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 275 | return -EINVAL; |
| 276 | } |
| 277 | opinfo->level = SMB2_OPLOCK_LEVEL_NONE; |
| 278 | if (opinfo->is_lease) |
| 279 | lease->state = lease->new_state; |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * opinfo_read_to_none() - convert a write read to none |
| 285 | * @opinfo: current oplock info |
| 286 | * |
| 287 | * Return: 0 on success, otherwise -EINVAL |
| 288 | */ |
| 289 | int opinfo_read_to_none(struct oplock_info *opinfo) |
| 290 | { |
| 291 | struct lease *lease = opinfo->o_lease; |
| 292 | |
| 293 | if (opinfo->level != SMB2_OPLOCK_LEVEL_II) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 294 | pr_err("bad oplock(0x%x)\n", opinfo->level); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 295 | if (opinfo->is_lease) |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 296 | pr_err("lease state(0x%x)\n", lease->state); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 297 | return -EINVAL; |
| 298 | } |
| 299 | opinfo->level = SMB2_OPLOCK_LEVEL_NONE; |
| 300 | if (opinfo->is_lease) |
| 301 | lease->state = lease->new_state; |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * lease_read_to_write() - upgrade lease state from read to write |
| 307 | * @opinfo: current lease info |
| 308 | * |
| 309 | * Return: 0 on success, otherwise -EINVAL |
| 310 | */ |
| 311 | int lease_read_to_write(struct oplock_info *opinfo) |
| 312 | { |
| 313 | struct lease *lease = opinfo->o_lease; |
| 314 | |
| 315 | if (!(lease->state & SMB2_LEASE_READ_CACHING_LE)) { |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 316 | ksmbd_debug(OPLOCK, "bad lease state(0x%x)\n", lease->state); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 317 | return -EINVAL; |
| 318 | } |
| 319 | |
| 320 | lease->new_state = SMB2_LEASE_NONE_LE; |
| 321 | lease->state |= SMB2_LEASE_WRITE_CACHING_LE; |
| 322 | if (lease->state & SMB2_LEASE_HANDLE_CACHING_LE) |
| 323 | opinfo->level = SMB2_OPLOCK_LEVEL_BATCH; |
| 324 | else |
| 325 | opinfo->level = SMB2_OPLOCK_LEVEL_EXCLUSIVE; |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * lease_none_upgrade() - upgrade lease state from none |
| 331 | * @opinfo: current lease info |
| 332 | * @new_state: new lease state |
| 333 | * |
| 334 | * Return: 0 on success, otherwise -EINVAL |
| 335 | */ |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 336 | static int lease_none_upgrade(struct oplock_info *opinfo, __le32 new_state) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 337 | { |
| 338 | struct lease *lease = opinfo->o_lease; |
| 339 | |
| 340 | if (!(lease->state == SMB2_LEASE_NONE_LE)) { |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 341 | ksmbd_debug(OPLOCK, "bad lease state(0x%x)\n", lease->state); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 342 | return -EINVAL; |
| 343 | } |
| 344 | |
| 345 | lease->new_state = SMB2_LEASE_NONE_LE; |
| 346 | lease->state = new_state; |
| 347 | if (lease->state & SMB2_LEASE_HANDLE_CACHING_LE) |
| 348 | if (lease->state & SMB2_LEASE_WRITE_CACHING_LE) |
| 349 | opinfo->level = SMB2_OPLOCK_LEVEL_BATCH; |
| 350 | else |
| 351 | opinfo->level = SMB2_OPLOCK_LEVEL_II; |
| 352 | else if (lease->state & SMB2_LEASE_WRITE_CACHING_LE) |
| 353 | opinfo->level = SMB2_OPLOCK_LEVEL_EXCLUSIVE; |
| 354 | else if (lease->state & SMB2_LEASE_READ_CACHING_LE) |
| 355 | opinfo->level = SMB2_OPLOCK_LEVEL_II; |
| 356 | |
| 357 | return 0; |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * close_id_del_oplock() - release oplock object at file close time |
| 362 | * @fp: ksmbd file pointer |
| 363 | */ |
| 364 | void close_id_del_oplock(struct ksmbd_file *fp) |
| 365 | { |
| 366 | struct oplock_info *opinfo; |
| 367 | |
| 368 | if (S_ISDIR(file_inode(fp->filp)->i_mode)) |
| 369 | return; |
| 370 | |
| 371 | opinfo = opinfo_get(fp); |
| 372 | if (!opinfo) |
| 373 | return; |
| 374 | |
| 375 | opinfo_del(opinfo); |
| 376 | |
| 377 | rcu_assign_pointer(fp->f_opinfo, NULL); |
| 378 | if (opinfo->op_state == OPLOCK_ACK_WAIT) { |
| 379 | opinfo->op_state = OPLOCK_CLOSING; |
| 380 | wake_up_interruptible_all(&opinfo->oplock_q); |
| 381 | if (opinfo->is_lease) { |
| 382 | atomic_set(&opinfo->breaking_cnt, 0); |
| 383 | wake_up_interruptible_all(&opinfo->oplock_brk); |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | opinfo_count_dec(fp); |
| 388 | atomic_dec(&opinfo->refcount); |
| 389 | opinfo_put(opinfo); |
| 390 | } |
| 391 | |
| 392 | /** |
| 393 | * grant_write_oplock() - grant exclusive/batch oplock or write lease |
| 394 | * @opinfo_new: new oplock info object |
| 395 | * @req_oplock: request oplock |
| 396 | * @lctx: lease context information |
| 397 | * |
| 398 | * Return: 0 |
| 399 | */ |
| 400 | static void grant_write_oplock(struct oplock_info *opinfo_new, int req_oplock, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 401 | struct lease_ctx_info *lctx) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 402 | { |
| 403 | struct lease *lease = opinfo_new->o_lease; |
| 404 | |
| 405 | if (req_oplock == SMB2_OPLOCK_LEVEL_BATCH) |
| 406 | opinfo_new->level = SMB2_OPLOCK_LEVEL_BATCH; |
| 407 | else |
| 408 | opinfo_new->level = SMB2_OPLOCK_LEVEL_EXCLUSIVE; |
| 409 | |
| 410 | if (lctx) { |
| 411 | lease->state = lctx->req_state; |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 412 | memcpy(lease->lease_key, lctx->lease_key, SMB2_LEASE_KEY_SIZE); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * grant_read_oplock() - grant level2 oplock or read lease |
| 418 | * @opinfo_new: new oplock info object |
| 419 | * @lctx: lease context information |
| 420 | * |
| 421 | * Return: 0 |
| 422 | */ |
| 423 | static void grant_read_oplock(struct oplock_info *opinfo_new, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 424 | struct lease_ctx_info *lctx) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 425 | { |
| 426 | struct lease *lease = opinfo_new->o_lease; |
| 427 | |
| 428 | opinfo_new->level = SMB2_OPLOCK_LEVEL_II; |
| 429 | |
| 430 | if (lctx) { |
| 431 | lease->state = SMB2_LEASE_READ_CACHING_LE; |
| 432 | if (lctx->req_state & SMB2_LEASE_HANDLE_CACHING_LE) |
| 433 | lease->state |= SMB2_LEASE_HANDLE_CACHING_LE; |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 434 | memcpy(lease->lease_key, lctx->lease_key, SMB2_LEASE_KEY_SIZE); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | |
| 438 | /** |
| 439 | * grant_none_oplock() - grant none oplock or none lease |
| 440 | * @opinfo_new: new oplock info object |
| 441 | * @lctx: lease context information |
| 442 | * |
| 443 | * Return: 0 |
| 444 | */ |
| 445 | static void grant_none_oplock(struct oplock_info *opinfo_new, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 446 | struct lease_ctx_info *lctx) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 447 | { |
| 448 | struct lease *lease = opinfo_new->o_lease; |
| 449 | |
| 450 | opinfo_new->level = SMB2_OPLOCK_LEVEL_NONE; |
| 451 | |
| 452 | if (lctx) { |
| 453 | lease->state = 0; |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 454 | memcpy(lease->lease_key, lctx->lease_key, SMB2_LEASE_KEY_SIZE); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 455 | } |
| 456 | } |
| 457 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 458 | static inline int compare_guid_key(struct oplock_info *opinfo, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 459 | const char *guid1, const char *key1) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 460 | { |
| 461 | const char *guid2, *key2; |
| 462 | |
| 463 | guid2 = opinfo->conn->ClientGUID; |
| 464 | key2 = opinfo->o_lease->lease_key; |
| 465 | if (!memcmp(guid1, guid2, SMB2_CLIENT_GUID_SIZE) && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 466 | !memcmp(key1, key2, SMB2_LEASE_KEY_SIZE)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 467 | return 1; |
| 468 | |
| 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | /** |
| 473 | * same_client_has_lease() - check whether current lease request is |
| 474 | * from lease owner of file |
| 475 | * @ci: master file pointer |
| 476 | * @client_guid: Client GUID |
| 477 | * @lctx: lease context information |
| 478 | * |
| 479 | * Return: oplock(lease) object on success, otherwise NULL |
| 480 | */ |
| 481 | static struct oplock_info *same_client_has_lease(struct ksmbd_inode *ci, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 482 | char *client_guid, |
| 483 | struct lease_ctx_info *lctx) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 484 | { |
| 485 | int ret; |
| 486 | struct lease *lease; |
| 487 | struct oplock_info *opinfo; |
| 488 | struct oplock_info *m_opinfo = NULL; |
| 489 | |
| 490 | if (!lctx) |
| 491 | return NULL; |
| 492 | |
| 493 | /* |
| 494 | * Compare lease key and client_guid to know request from same owner |
| 495 | * of same client |
| 496 | */ |
| 497 | read_lock(&ci->m_lock); |
| 498 | list_for_each_entry(opinfo, &ci->m_op_list, op_entry) { |
| 499 | if (!opinfo->is_lease) |
| 500 | continue; |
| 501 | read_unlock(&ci->m_lock); |
| 502 | lease = opinfo->o_lease; |
| 503 | |
| 504 | ret = compare_guid_key(opinfo, client_guid, lctx->lease_key); |
| 505 | if (ret) { |
| 506 | m_opinfo = opinfo; |
| 507 | /* skip upgrading lease about breaking lease */ |
| 508 | if (atomic_read(&opinfo->breaking_cnt)) { |
| 509 | read_lock(&ci->m_lock); |
| 510 | continue; |
| 511 | } |
| 512 | |
| 513 | /* upgrading lease */ |
| 514 | if ((atomic_read(&ci->op_count) + |
| 515 | atomic_read(&ci->sop_count)) == 1) { |
| 516 | if (lease->state == |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 517 | (lctx->req_state & lease->state)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 518 | lease->state |= lctx->req_state; |
| 519 | if (lctx->req_state & |
| 520 | SMB2_LEASE_WRITE_CACHING_LE) |
| 521 | lease_read_to_write(opinfo); |
| 522 | } |
| 523 | } else if ((atomic_read(&ci->op_count) + |
| 524 | atomic_read(&ci->sop_count)) > 1) { |
| 525 | if (lctx->req_state == |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 526 | (SMB2_LEASE_READ_CACHING_LE | |
| 527 | SMB2_LEASE_HANDLE_CACHING_LE)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 528 | lease->state = lctx->req_state; |
| 529 | } |
| 530 | |
| 531 | if (lctx->req_state && lease->state == |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 532 | SMB2_LEASE_NONE_LE) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 533 | lease_none_upgrade(opinfo, lctx->req_state); |
| 534 | } |
| 535 | read_lock(&ci->m_lock); |
| 536 | } |
| 537 | read_unlock(&ci->m_lock); |
| 538 | |
| 539 | return m_opinfo; |
| 540 | } |
| 541 | |
| 542 | static void wait_for_break_ack(struct oplock_info *opinfo) |
| 543 | { |
| 544 | int rc = 0; |
| 545 | |
| 546 | rc = wait_event_interruptible_timeout(opinfo->oplock_q, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 547 | opinfo->op_state == OPLOCK_STATE_NONE || |
| 548 | opinfo->op_state == OPLOCK_CLOSING, |
| 549 | OPLOCK_WAIT_TIME); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 550 | |
| 551 | /* is this a timeout ? */ |
| 552 | if (!rc) { |
| 553 | if (opinfo->is_lease) |
| 554 | opinfo->o_lease->state = SMB2_LEASE_NONE_LE; |
| 555 | opinfo->level = SMB2_OPLOCK_LEVEL_NONE; |
| 556 | opinfo->op_state = OPLOCK_STATE_NONE; |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | static void wake_up_oplock_break(struct oplock_info *opinfo) |
| 561 | { |
| 562 | clear_bit_unlock(0, &opinfo->pending_break); |
| 563 | /* memory barrier is needed for wake_up_bit() */ |
| 564 | smp_mb__after_atomic(); |
| 565 | wake_up_bit(&opinfo->pending_break, 0); |
| 566 | } |
| 567 | |
| 568 | static int oplock_break_pending(struct oplock_info *opinfo, int req_op_level) |
| 569 | { |
| 570 | while (test_and_set_bit(0, &opinfo->pending_break)) { |
| 571 | wait_on_bit(&opinfo->pending_break, 0, TASK_UNINTERRUPTIBLE); |
| 572 | |
| 573 | /* Not immediately break to none. */ |
| 574 | opinfo->open_trunc = 0; |
| 575 | |
| 576 | if (opinfo->op_state == OPLOCK_CLOSING) |
| 577 | return -ENOENT; |
| 578 | else if (!opinfo->is_lease && opinfo->level <= req_op_level) |
| 579 | return 1; |
| 580 | } |
| 581 | |
| 582 | if (!opinfo->is_lease && opinfo->level <= req_op_level) { |
| 583 | wake_up_oplock_break(opinfo); |
| 584 | return 1; |
| 585 | } |
| 586 | return 0; |
| 587 | } |
| 588 | |
| 589 | static inline int allocate_oplock_break_buf(struct ksmbd_work *work) |
| 590 | { |
Namjae Jeon | 20ea7fd | 2021-03-30 12:40:47 +0900 | [diff] [blame] | 591 | work->response_buf = kzalloc(MAX_CIFS_SMALL_BUFFER_SIZE, GFP_KERNEL); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 592 | if (!work->response_buf) |
| 593 | return -ENOMEM; |
| 594 | work->response_sz = MAX_CIFS_SMALL_BUFFER_SIZE; |
| 595 | return 0; |
| 596 | } |
| 597 | |
| 598 | /** |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 599 | * __smb2_oplock_break_noti() - send smb2 oplock break cmd from conn |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 600 | * to client |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 601 | * @wk: smb work object |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 602 | * |
| 603 | * There are two ways this function can be called. 1- while file open we break |
| 604 | * from exclusive/batch lock to levelII oplock and 2- while file write/truncate |
| 605 | * we break from levelII oplock no oplock. |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 606 | * work->request_buf contains oplock_info. |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 607 | */ |
| 608 | static void __smb2_oplock_break_noti(struct work_struct *wk) |
| 609 | { |
| 610 | struct smb2_oplock_break *rsp = NULL; |
| 611 | struct ksmbd_work *work = container_of(wk, struct ksmbd_work, work); |
| 612 | struct ksmbd_conn *conn = work->conn; |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 613 | struct oplock_break_info *br_info = work->request_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 614 | struct smb2_hdr *rsp_hdr; |
| 615 | struct ksmbd_file *fp; |
| 616 | |
| 617 | fp = ksmbd_lookup_durable_fd(br_info->fid); |
| 618 | if (!fp) { |
| 619 | atomic_dec(&conn->r_count); |
| 620 | ksmbd_free_work_struct(work); |
| 621 | return; |
| 622 | } |
| 623 | |
| 624 | if (allocate_oplock_break_buf(work)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 625 | pr_err("smb2_allocate_rsp_buf failed! "); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 626 | atomic_dec(&conn->r_count); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 627 | ksmbd_fd_put(work, fp); |
Dan Carpenter | a2ba270 | 2021-03-18 16:12:54 +0300 | [diff] [blame] | 628 | ksmbd_free_work_struct(work); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 629 | return; |
| 630 | } |
| 631 | |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 632 | rsp_hdr = work->response_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 633 | memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2); |
Hyunchul Lee | d8fb299 | 2021-06-25 11:53:26 +0900 | [diff] [blame] | 634 | rsp_hdr->smb2_buf_length = |
| 635 | cpu_to_be32(smb2_hdr_size_no_buflen(conn->vals)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 636 | rsp_hdr->ProtocolId = SMB2_PROTO_NUMBER; |
| 637 | rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE; |
| 638 | rsp_hdr->CreditRequest = cpu_to_le16(0); |
| 639 | rsp_hdr->Command = SMB2_OPLOCK_BREAK; |
| 640 | rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR); |
| 641 | rsp_hdr->NextCommand = 0; |
| 642 | rsp_hdr->MessageId = cpu_to_le64(-1); |
| 643 | rsp_hdr->Id.SyncId.ProcessId = 0; |
| 644 | rsp_hdr->Id.SyncId.TreeId = 0; |
| 645 | rsp_hdr->SessionId = 0; |
| 646 | memset(rsp_hdr->Signature, 0, 16); |
| 647 | |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 648 | rsp = work->response_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 649 | |
| 650 | rsp->StructureSize = cpu_to_le16(24); |
| 651 | if (!br_info->open_trunc && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 652 | (br_info->level == SMB2_OPLOCK_LEVEL_BATCH || |
| 653 | br_info->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 654 | rsp->OplockLevel = SMB2_OPLOCK_LEVEL_II; |
| 655 | else |
| 656 | rsp->OplockLevel = SMB2_OPLOCK_LEVEL_NONE; |
| 657 | rsp->Reserved = 0; |
| 658 | rsp->Reserved2 = 0; |
| 659 | rsp->PersistentFid = cpu_to_le64(fp->persistent_id); |
| 660 | rsp->VolatileFid = cpu_to_le64(fp->volatile_id); |
| 661 | |
| 662 | inc_rfc1001_len(rsp, 24); |
| 663 | |
| 664 | ksmbd_debug(OPLOCK, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 665 | "sending oplock break v_id %llu p_id = %llu lock level = %d\n", |
| 666 | rsp->VolatileFid, rsp->PersistentFid, rsp->OplockLevel); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 667 | |
| 668 | ksmbd_fd_put(work, fp); |
| 669 | ksmbd_conn_write(work); |
| 670 | ksmbd_free_work_struct(work); |
| 671 | atomic_dec(&conn->r_count); |
| 672 | } |
| 673 | |
| 674 | /** |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 675 | * smb2_oplock_break_noti() - send smb2 exclusive/batch to level2 oplock |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 676 | * break command from server to client |
| 677 | * @opinfo: oplock info object |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 678 | * |
| 679 | * Return: 0 on success, otherwise error |
| 680 | */ |
| 681 | static int smb2_oplock_break_noti(struct oplock_info *opinfo) |
| 682 | { |
| 683 | struct ksmbd_conn *conn = opinfo->conn; |
| 684 | struct oplock_break_info *br_info; |
| 685 | int ret = 0; |
| 686 | struct ksmbd_work *work = ksmbd_alloc_work_struct(); |
| 687 | |
| 688 | if (!work) |
| 689 | return -ENOMEM; |
| 690 | |
| 691 | br_info = kmalloc(sizeof(struct oplock_break_info), GFP_KERNEL); |
| 692 | if (!br_info) { |
| 693 | ksmbd_free_work_struct(work); |
| 694 | return -ENOMEM; |
| 695 | } |
| 696 | |
| 697 | br_info->level = opinfo->level; |
| 698 | br_info->fid = opinfo->fid; |
| 699 | br_info->open_trunc = opinfo->open_trunc; |
| 700 | |
| 701 | work->request_buf = (char *)br_info; |
| 702 | work->conn = conn; |
| 703 | work->sess = opinfo->sess; |
| 704 | |
| 705 | atomic_inc(&conn->r_count); |
| 706 | if (opinfo->op_state == OPLOCK_ACK_WAIT) { |
| 707 | INIT_WORK(&work->work, __smb2_oplock_break_noti); |
| 708 | ksmbd_queue_work(work); |
| 709 | |
| 710 | wait_for_break_ack(opinfo); |
| 711 | } else { |
| 712 | __smb2_oplock_break_noti(&work->work); |
| 713 | if (opinfo->level == SMB2_OPLOCK_LEVEL_II) |
| 714 | opinfo->level = SMB2_OPLOCK_LEVEL_NONE; |
| 715 | } |
| 716 | return ret; |
| 717 | } |
| 718 | |
| 719 | /** |
| 720 | * __smb2_lease_break_noti() - send lease break command from server |
| 721 | * to client |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 722 | * @wk: smb work object |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 723 | */ |
| 724 | static void __smb2_lease_break_noti(struct work_struct *wk) |
| 725 | { |
| 726 | struct smb2_lease_break *rsp = NULL; |
| 727 | struct ksmbd_work *work = container_of(wk, struct ksmbd_work, work); |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 728 | struct lease_break_info *br_info = work->request_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 729 | struct ksmbd_conn *conn = work->conn; |
| 730 | struct smb2_hdr *rsp_hdr; |
| 731 | |
| 732 | if (allocate_oplock_break_buf(work)) { |
| 733 | ksmbd_debug(OPLOCK, "smb2_allocate_rsp_buf failed! "); |
| 734 | ksmbd_free_work_struct(work); |
| 735 | atomic_dec(&conn->r_count); |
| 736 | return; |
| 737 | } |
| 738 | |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 739 | rsp_hdr = work->response_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 740 | memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2); |
Hyunchul Lee | d8fb299 | 2021-06-25 11:53:26 +0900 | [diff] [blame] | 741 | rsp_hdr->smb2_buf_length = |
| 742 | cpu_to_be32(smb2_hdr_size_no_buflen(conn->vals)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 743 | rsp_hdr->ProtocolId = SMB2_PROTO_NUMBER; |
| 744 | rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE; |
| 745 | rsp_hdr->CreditRequest = cpu_to_le16(0); |
| 746 | rsp_hdr->Command = SMB2_OPLOCK_BREAK; |
| 747 | rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR); |
| 748 | rsp_hdr->NextCommand = 0; |
| 749 | rsp_hdr->MessageId = cpu_to_le64(-1); |
| 750 | rsp_hdr->Id.SyncId.ProcessId = 0; |
| 751 | rsp_hdr->Id.SyncId.TreeId = 0; |
| 752 | rsp_hdr->SessionId = 0; |
| 753 | memset(rsp_hdr->Signature, 0, 16); |
| 754 | |
Namjae Jeon | e506649 | 2021-03-30 12:35:23 +0900 | [diff] [blame] | 755 | rsp = work->response_buf; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 756 | rsp->StructureSize = cpu_to_le16(44); |
Namjae Jeon | ade62d8 | 2021-06-07 09:22:22 +0900 | [diff] [blame] | 757 | rsp->Epoch = br_info->epoch; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 758 | rsp->Flags = 0; |
| 759 | |
| 760 | if (br_info->curr_state & (SMB2_LEASE_WRITE_CACHING_LE | |
| 761 | SMB2_LEASE_HANDLE_CACHING_LE)) |
| 762 | rsp->Flags = SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED; |
| 763 | |
| 764 | memcpy(rsp->LeaseKey, br_info->lease_key, SMB2_LEASE_KEY_SIZE); |
| 765 | rsp->CurrentLeaseState = br_info->curr_state; |
| 766 | rsp->NewLeaseState = br_info->new_state; |
| 767 | rsp->BreakReason = 0; |
| 768 | rsp->AccessMaskHint = 0; |
| 769 | rsp->ShareMaskHint = 0; |
| 770 | |
| 771 | inc_rfc1001_len(rsp, 44); |
| 772 | |
| 773 | ksmbd_conn_write(work); |
| 774 | ksmbd_free_work_struct(work); |
| 775 | atomic_dec(&conn->r_count); |
| 776 | } |
| 777 | |
| 778 | /** |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 779 | * smb2_lease_break_noti() - break lease when a new client request |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 780 | * write lease |
| 781 | * @opinfo: conains lease state information |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 782 | * |
| 783 | * Return: 0 on success, otherwise error |
| 784 | */ |
| 785 | static int smb2_lease_break_noti(struct oplock_info *opinfo) |
| 786 | { |
| 787 | struct ksmbd_conn *conn = opinfo->conn; |
| 788 | struct list_head *tmp, *t; |
| 789 | struct ksmbd_work *work; |
| 790 | struct lease_break_info *br_info; |
| 791 | struct lease *lease = opinfo->o_lease; |
| 792 | |
| 793 | work = ksmbd_alloc_work_struct(); |
| 794 | if (!work) |
| 795 | return -ENOMEM; |
| 796 | |
| 797 | br_info = kmalloc(sizeof(struct lease_break_info), GFP_KERNEL); |
| 798 | if (!br_info) { |
| 799 | ksmbd_free_work_struct(work); |
| 800 | return -ENOMEM; |
| 801 | } |
| 802 | |
| 803 | br_info->curr_state = lease->state; |
| 804 | br_info->new_state = lease->new_state; |
Namjae Jeon | ade62d8 | 2021-06-07 09:22:22 +0900 | [diff] [blame] | 805 | if (lease->version == 2) |
| 806 | br_info->epoch = cpu_to_le16(++lease->epoch); |
| 807 | else |
| 808 | br_info->epoch = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 809 | memcpy(br_info->lease_key, lease->lease_key, SMB2_LEASE_KEY_SIZE); |
| 810 | |
| 811 | work->request_buf = (char *)br_info; |
| 812 | work->conn = conn; |
| 813 | work->sess = opinfo->sess; |
| 814 | |
| 815 | atomic_inc(&conn->r_count); |
| 816 | if (opinfo->op_state == OPLOCK_ACK_WAIT) { |
| 817 | list_for_each_safe(tmp, t, &opinfo->interim_list) { |
| 818 | struct ksmbd_work *in_work; |
| 819 | |
| 820 | in_work = list_entry(tmp, struct ksmbd_work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 821 | interim_entry); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 822 | setup_async_work(in_work, NULL, NULL); |
| 823 | smb2_send_interim_resp(in_work, STATUS_PENDING); |
| 824 | list_del(&in_work->interim_entry); |
| 825 | } |
| 826 | INIT_WORK(&work->work, __smb2_lease_break_noti); |
| 827 | ksmbd_queue_work(work); |
| 828 | wait_for_break_ack(opinfo); |
| 829 | } else { |
| 830 | __smb2_lease_break_noti(&work->work); |
| 831 | if (opinfo->o_lease->new_state == SMB2_LEASE_NONE_LE) { |
| 832 | opinfo->level = SMB2_OPLOCK_LEVEL_NONE; |
| 833 | opinfo->o_lease->state = SMB2_LEASE_NONE_LE; |
| 834 | } |
| 835 | } |
| 836 | return 0; |
| 837 | } |
| 838 | |
| 839 | static void wait_lease_breaking(struct oplock_info *opinfo) |
| 840 | { |
| 841 | if (!opinfo->is_lease) |
| 842 | return; |
| 843 | |
| 844 | wake_up_interruptible_all(&opinfo->oplock_brk); |
| 845 | if (atomic_read(&opinfo->breaking_cnt)) { |
| 846 | int ret = 0; |
| 847 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 848 | ret = wait_event_interruptible_timeout(opinfo->oplock_brk, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 849 | atomic_read(&opinfo->breaking_cnt) == 0, |
| 850 | HZ); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 851 | if (!ret) |
| 852 | atomic_set(&opinfo->breaking_cnt, 0); |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | static int oplock_break(struct oplock_info *brk_opinfo, int req_op_level) |
| 857 | { |
| 858 | int err = 0; |
| 859 | |
| 860 | /* Need to break exclusive/batch oplock, write lease or overwrite_if */ |
| 861 | ksmbd_debug(OPLOCK, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 862 | "request to send oplock(level : 0x%x) break notification\n", |
| 863 | brk_opinfo->level); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 864 | |
| 865 | if (brk_opinfo->is_lease) { |
| 866 | struct lease *lease = brk_opinfo->o_lease; |
| 867 | |
| 868 | atomic_inc(&brk_opinfo->breaking_cnt); |
| 869 | |
| 870 | err = oplock_break_pending(brk_opinfo, req_op_level); |
| 871 | if (err) |
| 872 | return err < 0 ? err : 0; |
| 873 | |
| 874 | if (brk_opinfo->open_trunc) { |
| 875 | /* |
| 876 | * Create overwrite break trigger the lease break to |
| 877 | * none. |
| 878 | */ |
| 879 | lease->new_state = SMB2_LEASE_NONE_LE; |
| 880 | } else { |
| 881 | if (lease->state & SMB2_LEASE_WRITE_CACHING_LE) { |
| 882 | if (lease->state & SMB2_LEASE_HANDLE_CACHING_LE) |
| 883 | lease->new_state = |
| 884 | SMB2_LEASE_READ_CACHING_LE | |
| 885 | SMB2_LEASE_HANDLE_CACHING_LE; |
| 886 | else |
| 887 | lease->new_state = |
| 888 | SMB2_LEASE_READ_CACHING_LE; |
| 889 | } else { |
| 890 | if (lease->state & SMB2_LEASE_HANDLE_CACHING_LE) |
| 891 | lease->new_state = |
| 892 | SMB2_LEASE_READ_CACHING_LE; |
| 893 | else |
| 894 | lease->new_state = SMB2_LEASE_NONE_LE; |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | if (lease->state & (SMB2_LEASE_WRITE_CACHING_LE | |
| 899 | SMB2_LEASE_HANDLE_CACHING_LE)) |
| 900 | brk_opinfo->op_state = OPLOCK_ACK_WAIT; |
| 901 | else |
| 902 | atomic_dec(&brk_opinfo->breaking_cnt); |
| 903 | } else { |
| 904 | err = oplock_break_pending(brk_opinfo, req_op_level); |
| 905 | if (err) |
| 906 | return err < 0 ? err : 0; |
| 907 | |
| 908 | if (brk_opinfo->level == SMB2_OPLOCK_LEVEL_BATCH || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 909 | brk_opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 910 | brk_opinfo->op_state = OPLOCK_ACK_WAIT; |
| 911 | } |
| 912 | |
| 913 | if (brk_opinfo->is_lease) |
| 914 | err = smb2_lease_break_noti(brk_opinfo); |
| 915 | else |
| 916 | err = smb2_oplock_break_noti(brk_opinfo); |
| 917 | |
| 918 | ksmbd_debug(OPLOCK, "oplock granted = %d\n", brk_opinfo->level); |
| 919 | if (brk_opinfo->op_state == OPLOCK_CLOSING) |
| 920 | err = -ENOENT; |
| 921 | wake_up_oplock_break(brk_opinfo); |
| 922 | |
| 923 | wait_lease_breaking(brk_opinfo); |
| 924 | |
| 925 | return err; |
| 926 | } |
| 927 | |
| 928 | void destroy_lease_table(struct ksmbd_conn *conn) |
| 929 | { |
| 930 | struct lease_table *lb, *lbtmp; |
| 931 | struct oplock_info *opinfo; |
| 932 | |
| 933 | write_lock(&lease_list_lock); |
| 934 | if (list_empty(&lease_table_list)) { |
| 935 | write_unlock(&lease_list_lock); |
| 936 | return; |
| 937 | } |
| 938 | |
| 939 | list_for_each_entry_safe(lb, lbtmp, &lease_table_list, l_entry) { |
| 940 | if (conn && memcmp(lb->client_guid, conn->ClientGUID, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 941 | SMB2_CLIENT_GUID_SIZE)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 942 | continue; |
| 943 | again: |
| 944 | rcu_read_lock(); |
| 945 | list_for_each_entry_rcu(opinfo, &lb->lease_list, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 946 | lease_entry) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 947 | rcu_read_unlock(); |
| 948 | lease_del_list(opinfo); |
| 949 | goto again; |
| 950 | } |
| 951 | rcu_read_unlock(); |
| 952 | list_del(&lb->l_entry); |
| 953 | kfree(lb); |
| 954 | } |
| 955 | write_unlock(&lease_list_lock); |
| 956 | } |
| 957 | |
| 958 | int find_same_lease_key(struct ksmbd_session *sess, struct ksmbd_inode *ci, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 959 | struct lease_ctx_info *lctx) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 960 | { |
| 961 | struct oplock_info *opinfo; |
| 962 | int err = 0; |
| 963 | struct lease_table *lb; |
| 964 | |
| 965 | if (!lctx) |
| 966 | return err; |
| 967 | |
| 968 | read_lock(&lease_list_lock); |
| 969 | if (list_empty(&lease_table_list)) { |
| 970 | read_unlock(&lease_list_lock); |
| 971 | return 0; |
| 972 | } |
| 973 | |
| 974 | list_for_each_entry(lb, &lease_table_list, l_entry) { |
| 975 | if (!memcmp(lb->client_guid, sess->conn->ClientGUID, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 976 | SMB2_CLIENT_GUID_SIZE)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 977 | goto found; |
| 978 | } |
| 979 | read_unlock(&lease_list_lock); |
| 980 | |
| 981 | return 0; |
| 982 | |
| 983 | found: |
| 984 | rcu_read_lock(); |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 985 | list_for_each_entry_rcu(opinfo, &lb->lease_list, lease_entry) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 986 | if (!atomic_inc_not_zero(&opinfo->refcount)) |
| 987 | continue; |
| 988 | rcu_read_unlock(); |
| 989 | if (opinfo->o_fp->f_ci == ci) |
| 990 | goto op_next; |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 991 | err = compare_guid_key(opinfo, sess->conn->ClientGUID, |
| 992 | lctx->lease_key); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 993 | if (err) { |
| 994 | err = -EINVAL; |
| 995 | ksmbd_debug(OPLOCK, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 996 | "found same lease key is already used in other files\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 997 | opinfo_put(opinfo); |
| 998 | goto out; |
| 999 | } |
| 1000 | op_next: |
| 1001 | opinfo_put(opinfo); |
| 1002 | rcu_read_lock(); |
| 1003 | } |
| 1004 | rcu_read_unlock(); |
| 1005 | |
| 1006 | out: |
| 1007 | read_unlock(&lease_list_lock); |
| 1008 | return err; |
| 1009 | } |
| 1010 | |
| 1011 | static void copy_lease(struct oplock_info *op1, struct oplock_info *op2) |
| 1012 | { |
| 1013 | struct lease *lease1 = op1->o_lease; |
| 1014 | struct lease *lease2 = op2->o_lease; |
| 1015 | |
| 1016 | op2->level = op1->level; |
| 1017 | lease2->state = lease1->state; |
| 1018 | memcpy(lease2->lease_key, lease1->lease_key, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1019 | SMB2_LEASE_KEY_SIZE); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1020 | lease2->duration = lease1->duration; |
| 1021 | lease2->flags = lease1->flags; |
| 1022 | } |
| 1023 | |
| 1024 | static int add_lease_global_list(struct oplock_info *opinfo) |
| 1025 | { |
| 1026 | struct lease_table *lb; |
| 1027 | |
| 1028 | read_lock(&lease_list_lock); |
| 1029 | list_for_each_entry(lb, &lease_table_list, l_entry) { |
| 1030 | if (!memcmp(lb->client_guid, opinfo->conn->ClientGUID, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1031 | SMB2_CLIENT_GUID_SIZE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1032 | opinfo->o_lease->l_lb = lb; |
| 1033 | lease_add_list(opinfo); |
| 1034 | read_unlock(&lease_list_lock); |
| 1035 | return 0; |
| 1036 | } |
| 1037 | } |
| 1038 | read_unlock(&lease_list_lock); |
| 1039 | |
| 1040 | lb = kmalloc(sizeof(struct lease_table), GFP_KERNEL); |
| 1041 | if (!lb) |
| 1042 | return -ENOMEM; |
| 1043 | |
| 1044 | memcpy(lb->client_guid, opinfo->conn->ClientGUID, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1045 | SMB2_CLIENT_GUID_SIZE); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1046 | INIT_LIST_HEAD(&lb->lease_list); |
| 1047 | spin_lock_init(&lb->lb_lock); |
| 1048 | opinfo->o_lease->l_lb = lb; |
| 1049 | lease_add_list(opinfo); |
| 1050 | lb_add(lb); |
| 1051 | return 0; |
| 1052 | } |
| 1053 | |
| 1054 | static void set_oplock_level(struct oplock_info *opinfo, int level, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1055 | struct lease_ctx_info *lctx) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1056 | { |
| 1057 | switch (level) { |
| 1058 | case SMB2_OPLOCK_LEVEL_BATCH: |
| 1059 | case SMB2_OPLOCK_LEVEL_EXCLUSIVE: |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1060 | grant_write_oplock(opinfo, level, lctx); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1061 | break; |
| 1062 | case SMB2_OPLOCK_LEVEL_II: |
| 1063 | grant_read_oplock(opinfo, lctx); |
| 1064 | break; |
| 1065 | default: |
| 1066 | grant_none_oplock(opinfo, lctx); |
| 1067 | break; |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | /** |
| 1072 | * smb_grant_oplock() - handle oplock/lease request on file open |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 1073 | * @work: smb work |
| 1074 | * @req_op_level: oplock level |
| 1075 | * @pid: id of open file |
| 1076 | * @fp: ksmbd file pointer |
| 1077 | * @tid: Tree id of connection |
| 1078 | * @lctx: lease context information on file open |
| 1079 | * @share_ret: share mode |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1080 | * |
| 1081 | * Return: 0 on success, otherwise error |
| 1082 | */ |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1083 | int smb_grant_oplock(struct ksmbd_work *work, int req_op_level, u64 pid, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1084 | struct ksmbd_file *fp, __u16 tid, |
| 1085 | struct lease_ctx_info *lctx, int share_ret) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1086 | { |
| 1087 | struct ksmbd_session *sess = work->sess; |
| 1088 | int err = 0; |
| 1089 | struct oplock_info *opinfo = NULL, *prev_opinfo = NULL; |
| 1090 | struct ksmbd_inode *ci = fp->f_ci; |
| 1091 | bool prev_op_has_lease; |
| 1092 | __le32 prev_op_state = 0; |
| 1093 | |
| 1094 | /* not support directory lease */ |
Namjae Jeon | ade62d8 | 2021-06-07 09:22:22 +0900 | [diff] [blame] | 1095 | if (S_ISDIR(file_inode(fp->filp)->i_mode)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1096 | return 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1097 | |
| 1098 | opinfo = alloc_opinfo(work, pid, tid); |
| 1099 | if (!opinfo) |
| 1100 | return -ENOMEM; |
| 1101 | |
| 1102 | if (lctx) { |
| 1103 | err = alloc_lease(opinfo, lctx); |
| 1104 | if (err) |
| 1105 | goto err_out; |
| 1106 | opinfo->is_lease = 1; |
| 1107 | } |
| 1108 | |
| 1109 | /* ci does not have any oplock */ |
| 1110 | if (!opinfo_count(fp)) |
| 1111 | goto set_lev; |
| 1112 | |
| 1113 | /* grant none-oplock if second open is trunc */ |
Namjae Jeon | 849fbc5 | 2021-06-29 09:24:31 +0900 | [diff] [blame] | 1114 | if (fp->attrib_only && fp->cdoption != FILE_OVERWRITE_IF_LE && |
| 1115 | fp->cdoption != FILE_OVERWRITE_LE && |
| 1116 | fp->cdoption != FILE_SUPERSEDE_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1117 | req_op_level = SMB2_OPLOCK_LEVEL_NONE; |
| 1118 | goto set_lev; |
| 1119 | } |
| 1120 | |
| 1121 | if (lctx) { |
| 1122 | struct oplock_info *m_opinfo; |
| 1123 | |
| 1124 | /* is lease already granted ? */ |
| 1125 | m_opinfo = same_client_has_lease(ci, sess->conn->ClientGUID, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1126 | lctx); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1127 | if (m_opinfo) { |
| 1128 | copy_lease(m_opinfo, opinfo); |
| 1129 | if (atomic_read(&m_opinfo->breaking_cnt)) |
| 1130 | opinfo->o_lease->flags = |
| 1131 | SMB2_LEASE_FLAG_BREAK_IN_PROGRESS_LE; |
| 1132 | goto out; |
| 1133 | } |
| 1134 | } |
| 1135 | prev_opinfo = opinfo_get_list(ci); |
| 1136 | if (!prev_opinfo || |
| 1137 | (prev_opinfo->level == SMB2_OPLOCK_LEVEL_NONE && lctx)) |
| 1138 | goto set_lev; |
| 1139 | prev_op_has_lease = prev_opinfo->is_lease; |
| 1140 | if (prev_op_has_lease) |
| 1141 | prev_op_state = prev_opinfo->o_lease->state; |
| 1142 | |
| 1143 | if (share_ret < 0 && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1144 | prev_opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1145 | err = share_ret; |
| 1146 | opinfo_put(prev_opinfo); |
| 1147 | goto err_out; |
| 1148 | } |
| 1149 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1150 | if (prev_opinfo->level != SMB2_OPLOCK_LEVEL_BATCH && |
| 1151 | prev_opinfo->level != SMB2_OPLOCK_LEVEL_EXCLUSIVE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1152 | opinfo_put(prev_opinfo); |
| 1153 | goto op_break_not_needed; |
| 1154 | } |
| 1155 | |
| 1156 | list_add(&work->interim_entry, &prev_opinfo->interim_list); |
| 1157 | err = oplock_break(prev_opinfo, SMB2_OPLOCK_LEVEL_II); |
| 1158 | opinfo_put(prev_opinfo); |
| 1159 | if (err == -ENOENT) |
| 1160 | goto set_lev; |
| 1161 | /* Check all oplock was freed by close */ |
| 1162 | else if (err < 0) |
| 1163 | goto err_out; |
| 1164 | |
| 1165 | op_break_not_needed: |
| 1166 | if (share_ret < 0) { |
| 1167 | err = share_ret; |
| 1168 | goto err_out; |
| 1169 | } |
| 1170 | |
| 1171 | if (req_op_level != SMB2_OPLOCK_LEVEL_NONE) |
| 1172 | req_op_level = SMB2_OPLOCK_LEVEL_II; |
| 1173 | |
| 1174 | /* grant fixed oplock on stacked locking between lease and oplock */ |
| 1175 | if (prev_op_has_lease && !lctx) |
| 1176 | if (prev_op_state & SMB2_LEASE_HANDLE_CACHING_LE) |
| 1177 | req_op_level = SMB2_OPLOCK_LEVEL_NONE; |
| 1178 | |
| 1179 | if (!prev_op_has_lease && lctx) { |
| 1180 | req_op_level = SMB2_OPLOCK_LEVEL_II; |
| 1181 | lctx->req_state = SMB2_LEASE_READ_CACHING_LE; |
| 1182 | } |
| 1183 | |
| 1184 | set_lev: |
| 1185 | set_oplock_level(opinfo, req_op_level, lctx); |
| 1186 | |
| 1187 | out: |
| 1188 | rcu_assign_pointer(fp->f_opinfo, opinfo); |
| 1189 | opinfo->o_fp = fp; |
| 1190 | |
| 1191 | opinfo_count_inc(fp); |
| 1192 | opinfo_add(opinfo); |
| 1193 | if (opinfo->is_lease) { |
| 1194 | err = add_lease_global_list(opinfo); |
| 1195 | if (err) |
| 1196 | goto err_out; |
| 1197 | } |
| 1198 | |
| 1199 | return 0; |
| 1200 | err_out: |
| 1201 | free_opinfo(opinfo); |
| 1202 | return err; |
| 1203 | } |
| 1204 | |
| 1205 | /** |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 1206 | * smb_break_all_write_oplock() - break batch/exclusive oplock to level2 |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1207 | * @work: smb work |
| 1208 | * @fp: ksmbd file pointer |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 1209 | * @is_trunc: truncate on open |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1210 | */ |
| 1211 | static void smb_break_all_write_oplock(struct ksmbd_work *work, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1212 | struct ksmbd_file *fp, int is_trunc) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1213 | { |
| 1214 | struct oplock_info *brk_opinfo; |
| 1215 | |
| 1216 | brk_opinfo = opinfo_get_list(fp->f_ci); |
| 1217 | if (!brk_opinfo) |
| 1218 | return; |
| 1219 | if (brk_opinfo->level != SMB2_OPLOCK_LEVEL_BATCH && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1220 | brk_opinfo->level != SMB2_OPLOCK_LEVEL_EXCLUSIVE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1221 | opinfo_put(brk_opinfo); |
| 1222 | return; |
| 1223 | } |
| 1224 | |
| 1225 | brk_opinfo->open_trunc = is_trunc; |
| 1226 | list_add(&work->interim_entry, &brk_opinfo->interim_list); |
| 1227 | oplock_break(brk_opinfo, SMB2_OPLOCK_LEVEL_II); |
| 1228 | opinfo_put(brk_opinfo); |
| 1229 | } |
| 1230 | |
| 1231 | /** |
| 1232 | * smb_break_all_levII_oplock() - send level2 oplock or read lease break command |
| 1233 | * from server to client |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 1234 | * @work: smb work |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1235 | * @fp: ksmbd file pointer |
| 1236 | * @is_trunc: truncate on open |
| 1237 | */ |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1238 | void smb_break_all_levII_oplock(struct ksmbd_work *work, struct ksmbd_file *fp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1239 | int is_trunc) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1240 | { |
| 1241 | struct oplock_info *op, *brk_op; |
| 1242 | struct ksmbd_inode *ci; |
| 1243 | struct ksmbd_conn *conn = work->sess->conn; |
| 1244 | |
| 1245 | if (!test_share_config_flag(work->tcon->share_conf, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1246 | KSMBD_SHARE_FLAG_OPLOCKS)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1247 | return; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1248 | |
| 1249 | ci = fp->f_ci; |
| 1250 | op = opinfo_get(fp); |
| 1251 | |
| 1252 | rcu_read_lock(); |
| 1253 | list_for_each_entry_rcu(brk_op, &ci->m_op_list, op_entry) { |
| 1254 | if (!atomic_inc_not_zero(&brk_op->refcount)) |
| 1255 | continue; |
| 1256 | rcu_read_unlock(); |
| 1257 | if (brk_op->is_lease && (brk_op->o_lease->state & |
| 1258 | (~(SMB2_LEASE_READ_CACHING_LE | |
| 1259 | SMB2_LEASE_HANDLE_CACHING_LE)))) { |
| 1260 | ksmbd_debug(OPLOCK, "unexpected lease state(0x%x)\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1261 | brk_op->o_lease->state); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1262 | goto next; |
| 1263 | } else if (brk_op->level != |
| 1264 | SMB2_OPLOCK_LEVEL_II) { |
| 1265 | ksmbd_debug(OPLOCK, "unexpected oplock(0x%x)\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1266 | brk_op->level); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1267 | goto next; |
| 1268 | } |
| 1269 | |
| 1270 | /* Skip oplock being break to none */ |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1271 | if (brk_op->is_lease && |
Namjae Jeon | c986ed9 | 2021-05-26 17:59:56 +0900 | [diff] [blame] | 1272 | brk_op->o_lease->new_state == SMB2_LEASE_NONE_LE && |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1273 | atomic_read(&brk_op->breaking_cnt)) |
| 1274 | goto next; |
| 1275 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1276 | if (op && op->is_lease && brk_op->is_lease && |
| 1277 | !memcmp(conn->ClientGUID, brk_op->conn->ClientGUID, |
| 1278 | SMB2_CLIENT_GUID_SIZE) && |
| 1279 | !memcmp(op->o_lease->lease_key, brk_op->o_lease->lease_key, |
| 1280 | SMB2_LEASE_KEY_SIZE)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1281 | goto next; |
| 1282 | brk_op->open_trunc = is_trunc; |
| 1283 | oplock_break(brk_op, SMB2_OPLOCK_LEVEL_NONE); |
| 1284 | next: |
| 1285 | opinfo_put(brk_op); |
| 1286 | rcu_read_lock(); |
| 1287 | } |
| 1288 | rcu_read_unlock(); |
| 1289 | |
| 1290 | if (op) |
| 1291 | opinfo_put(op); |
| 1292 | } |
| 1293 | |
| 1294 | /** |
| 1295 | * smb_break_all_oplock() - break both batch/exclusive and level2 oplock |
| 1296 | * @work: smb work |
| 1297 | * @fp: ksmbd file pointer |
| 1298 | */ |
| 1299 | void smb_break_all_oplock(struct ksmbd_work *work, struct ksmbd_file *fp) |
| 1300 | { |
| 1301 | if (!test_share_config_flag(work->tcon->share_conf, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1302 | KSMBD_SHARE_FLAG_OPLOCKS)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1303 | return; |
| 1304 | |
| 1305 | smb_break_all_write_oplock(work, fp, 1); |
| 1306 | smb_break_all_levII_oplock(work, fp, 1); |
| 1307 | } |
| 1308 | |
| 1309 | /** |
| 1310 | * smb2_map_lease_to_oplock() - map lease state to corresponding oplock type |
| 1311 | * @lease_state: lease type |
| 1312 | * |
| 1313 | * Return: 0 if no mapping, otherwise corresponding oplock type |
| 1314 | */ |
| 1315 | __u8 smb2_map_lease_to_oplock(__le32 lease_state) |
| 1316 | { |
| 1317 | if (lease_state == (SMB2_LEASE_HANDLE_CACHING_LE | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1318 | SMB2_LEASE_READ_CACHING_LE | |
| 1319 | SMB2_LEASE_WRITE_CACHING_LE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1320 | return SMB2_OPLOCK_LEVEL_BATCH; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1321 | } else if (lease_state != SMB2_LEASE_WRITE_CACHING_LE && |
| 1322 | lease_state & SMB2_LEASE_WRITE_CACHING_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1323 | if (!(lease_state & SMB2_LEASE_HANDLE_CACHING_LE)) |
| 1324 | return SMB2_OPLOCK_LEVEL_EXCLUSIVE; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1325 | } else if (lease_state & SMB2_LEASE_READ_CACHING_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1326 | return SMB2_OPLOCK_LEVEL_II; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1327 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1328 | return 0; |
| 1329 | } |
| 1330 | |
| 1331 | /** |
| 1332 | * create_lease_buf() - create lease context for open cmd response |
| 1333 | * @rbuf: buffer to create lease context response |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 1334 | * @lease: buffer to stored parsed lease state information |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1335 | */ |
| 1336 | void create_lease_buf(u8 *rbuf, struct lease *lease) |
| 1337 | { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1338 | char *LeaseKey = (char *)&lease->lease_key; |
| 1339 | |
Namjae Jeon | ade62d8 | 2021-06-07 09:22:22 +0900 | [diff] [blame] | 1340 | if (lease->version == 2) { |
| 1341 | struct create_lease_v2 *buf = (struct create_lease_v2 *)rbuf; |
| 1342 | char *ParentLeaseKey = (char *)&lease->parent_lease_key; |
| 1343 | |
| 1344 | memset(buf, 0, sizeof(struct create_lease_v2)); |
| 1345 | buf->lcontext.LeaseKeyLow = *((__le64 *)LeaseKey); |
| 1346 | buf->lcontext.LeaseKeyHigh = *((__le64 *)(LeaseKey + 8)); |
| 1347 | buf->lcontext.LeaseFlags = lease->flags; |
| 1348 | buf->lcontext.LeaseState = lease->state; |
| 1349 | buf->lcontext.ParentLeaseKeyLow = *((__le64 *)ParentLeaseKey); |
| 1350 | buf->lcontext.ParentLeaseKeyHigh = *((__le64 *)(ParentLeaseKey + 8)); |
| 1351 | buf->ccontext.DataOffset = cpu_to_le16(offsetof |
| 1352 | (struct create_lease_v2, lcontext)); |
| 1353 | buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2)); |
| 1354 | buf->ccontext.NameOffset = cpu_to_le16(offsetof |
| 1355 | (struct create_lease_v2, Name)); |
| 1356 | buf->ccontext.NameLength = cpu_to_le16(4); |
| 1357 | buf->Name[0] = 'R'; |
| 1358 | buf->Name[1] = 'q'; |
| 1359 | buf->Name[2] = 'L'; |
| 1360 | buf->Name[3] = 's'; |
| 1361 | } else { |
| 1362 | struct create_lease *buf = (struct create_lease *)rbuf; |
| 1363 | |
| 1364 | memset(buf, 0, sizeof(struct create_lease)); |
| 1365 | buf->lcontext.LeaseKeyLow = *((__le64 *)LeaseKey); |
| 1366 | buf->lcontext.LeaseKeyHigh = *((__le64 *)(LeaseKey + 8)); |
| 1367 | buf->lcontext.LeaseFlags = lease->flags; |
| 1368 | buf->lcontext.LeaseState = lease->state; |
| 1369 | buf->ccontext.DataOffset = cpu_to_le16(offsetof |
| 1370 | (struct create_lease, lcontext)); |
| 1371 | buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context)); |
| 1372 | buf->ccontext.NameOffset = cpu_to_le16(offsetof |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1373 | (struct create_lease, Name)); |
Namjae Jeon | ade62d8 | 2021-06-07 09:22:22 +0900 | [diff] [blame] | 1374 | buf->ccontext.NameLength = cpu_to_le16(4); |
| 1375 | buf->Name[0] = 'R'; |
| 1376 | buf->Name[1] = 'q'; |
| 1377 | buf->Name[2] = 'L'; |
| 1378 | buf->Name[3] = 's'; |
| 1379 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1380 | } |
| 1381 | |
| 1382 | /** |
| 1383 | * parse_lease_state() - parse lease context containted in file open request |
| 1384 | * @open_req: buffer containing smb2 file open(create) request |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1385 | * |
| 1386 | * Return: oplock state, -ENOENT if create lease context not found |
| 1387 | */ |
| 1388 | struct lease_ctx_info *parse_lease_state(void *open_req) |
| 1389 | { |
| 1390 | char *data_offset; |
| 1391 | struct create_context *cc; |
| 1392 | unsigned int next = 0; |
| 1393 | char *name; |
| 1394 | bool found = false; |
| 1395 | struct smb2_create_req *req = (struct smb2_create_req *)open_req; |
| 1396 | struct lease_ctx_info *lreq = kzalloc(sizeof(struct lease_ctx_info), |
| 1397 | GFP_KERNEL); |
| 1398 | if (!lreq) |
| 1399 | return NULL; |
| 1400 | |
| 1401 | data_offset = (char *)req + 4 + le32_to_cpu(req->CreateContextsOffset); |
| 1402 | cc = (struct create_context *)data_offset; |
| 1403 | do { |
| 1404 | cc = (struct create_context *)((char *)cc + next); |
| 1405 | name = le16_to_cpu(cc->NameOffset) + (char *)cc; |
| 1406 | if (le16_to_cpu(cc->NameLength) != 4 || |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1407 | strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1408 | next = le32_to_cpu(cc->Next); |
| 1409 | continue; |
| 1410 | } |
| 1411 | found = true; |
| 1412 | break; |
| 1413 | } while (next != 0); |
| 1414 | |
| 1415 | if (found) { |
Namjae Jeon | ade62d8 | 2021-06-07 09:22:22 +0900 | [diff] [blame] | 1416 | if (sizeof(struct lease_context_v2) == le32_to_cpu(cc->DataLength)) { |
| 1417 | struct create_lease_v2 *lc = (struct create_lease_v2 *)cc; |
| 1418 | |
| 1419 | *((__le64 *)lreq->lease_key) = lc->lcontext.LeaseKeyLow; |
| 1420 | *((__le64 *)(lreq->lease_key + 8)) = lc->lcontext.LeaseKeyHigh; |
| 1421 | lreq->req_state = lc->lcontext.LeaseState; |
| 1422 | lreq->flags = lc->lcontext.LeaseFlags; |
| 1423 | lreq->duration = lc->lcontext.LeaseDuration; |
| 1424 | *((__le64 *)lreq->parent_lease_key) = lc->lcontext.ParentLeaseKeyLow; |
| 1425 | *((__le64 *)(lreq->parent_lease_key + 8)) = lc->lcontext.ParentLeaseKeyHigh; |
| 1426 | lreq->version = 2; |
| 1427 | } else { |
| 1428 | struct create_lease *lc = (struct create_lease *)cc; |
| 1429 | |
| 1430 | *((__le64 *)lreq->lease_key) = lc->lcontext.LeaseKeyLow; |
| 1431 | *((__le64 *)(lreq->lease_key + 8)) = lc->lcontext.LeaseKeyHigh; |
| 1432 | lreq->req_state = lc->lcontext.LeaseState; |
| 1433 | lreq->flags = lc->lcontext.LeaseFlags; |
| 1434 | lreq->duration = lc->lcontext.LeaseDuration; |
| 1435 | lreq->version = 1; |
| 1436 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1437 | return lreq; |
| 1438 | } |
| 1439 | |
| 1440 | kfree(lreq); |
| 1441 | return NULL; |
| 1442 | } |
| 1443 | |
| 1444 | /** |
| 1445 | * smb2_find_context_vals() - find a particular context info in open request |
| 1446 | * @open_req: buffer containing smb2 file open(create) request |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 1447 | * @tag: context name to search for |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1448 | * |
Namjae Jeon | f19b396 | 2021-07-13 09:59:34 +0900 | [diff] [blame^] | 1449 | * Return: pointer to requested context, NULL if @str context not found |
| 1450 | * or error pointer if name length is invalid. |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1451 | */ |
| 1452 | struct create_context *smb2_find_context_vals(void *open_req, const char *tag) |
| 1453 | { |
| 1454 | char *data_offset; |
| 1455 | struct create_context *cc; |
| 1456 | unsigned int next = 0; |
| 1457 | char *name; |
| 1458 | struct smb2_create_req *req = (struct smb2_create_req *)open_req; |
| 1459 | |
| 1460 | data_offset = (char *)req + 4 + le32_to_cpu(req->CreateContextsOffset); |
| 1461 | cc = (struct create_context *)data_offset; |
| 1462 | do { |
| 1463 | int val; |
| 1464 | |
| 1465 | cc = (struct create_context *)((char *)cc + next); |
| 1466 | name = le16_to_cpu(cc->NameOffset) + (char *)cc; |
| 1467 | val = le16_to_cpu(cc->NameLength); |
| 1468 | if (val < 4) |
| 1469 | return ERR_PTR(-EINVAL); |
| 1470 | |
| 1471 | if (memcmp(name, tag, val) == 0) |
| 1472 | return cc; |
| 1473 | next = le32_to_cpu(cc->Next); |
| 1474 | } while (next != 0); |
| 1475 | |
Hyunchul Lee | ce154c3 | 2021-07-09 17:06:33 +0900 | [diff] [blame] | 1476 | return NULL; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1477 | } |
| 1478 | |
| 1479 | /** |
Namjae Jeon | 5365564 | 2021-03-30 14:42:05 +0900 | [diff] [blame] | 1480 | * create_durable_rsp_buf() - create durable handle context |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1481 | * @cc: buffer to create durable context response |
| 1482 | */ |
| 1483 | void create_durable_rsp_buf(char *cc) |
| 1484 | { |
| 1485 | struct create_durable_rsp *buf; |
| 1486 | |
| 1487 | buf = (struct create_durable_rsp *)cc; |
| 1488 | memset(buf, 0, sizeof(struct create_durable_rsp)); |
| 1489 | buf->ccontext.DataOffset = cpu_to_le16(offsetof |
| 1490 | (struct create_durable_rsp, Data)); |
| 1491 | buf->ccontext.DataLength = cpu_to_le32(8); |
| 1492 | buf->ccontext.NameOffset = cpu_to_le16(offsetof |
| 1493 | (struct create_durable_rsp, Name)); |
| 1494 | buf->ccontext.NameLength = cpu_to_le16(4); |
| 1495 | /* SMB2_CREATE_DURABLE_HANDLE_RESPONSE is "DHnQ" */ |
| 1496 | buf->Name[0] = 'D'; |
| 1497 | buf->Name[1] = 'H'; |
| 1498 | buf->Name[2] = 'n'; |
| 1499 | buf->Name[3] = 'Q'; |
| 1500 | } |
| 1501 | |
| 1502 | /** |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 1503 | * create_durable_v2_rsp_buf() - create durable handle v2 context |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1504 | * @cc: buffer to create durable context response |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 1505 | * @fp: ksmbd file pointer |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1506 | */ |
| 1507 | void create_durable_v2_rsp_buf(char *cc, struct ksmbd_file *fp) |
| 1508 | { |
| 1509 | struct create_durable_v2_rsp *buf; |
| 1510 | |
| 1511 | buf = (struct create_durable_v2_rsp *)cc; |
| 1512 | memset(buf, 0, sizeof(struct create_durable_rsp)); |
| 1513 | buf->ccontext.DataOffset = cpu_to_le16(offsetof |
| 1514 | (struct create_durable_rsp, Data)); |
| 1515 | buf->ccontext.DataLength = cpu_to_le32(8); |
| 1516 | buf->ccontext.NameOffset = cpu_to_le16(offsetof |
| 1517 | (struct create_durable_rsp, Name)); |
| 1518 | buf->ccontext.NameLength = cpu_to_le16(4); |
| 1519 | /* SMB2_CREATE_DURABLE_HANDLE_RESPONSE_V2 is "DH2Q" */ |
| 1520 | buf->Name[0] = 'D'; |
| 1521 | buf->Name[1] = 'H'; |
| 1522 | buf->Name[2] = '2'; |
| 1523 | buf->Name[3] = 'Q'; |
| 1524 | |
| 1525 | buf->Timeout = cpu_to_le32(fp->durable_timeout); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1526 | } |
| 1527 | |
| 1528 | /** |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 1529 | * create_mxac_rsp_buf() - create query maximal access context |
| 1530 | * @cc: buffer to create maximal access context response |
| 1531 | * @maximal_access: maximal access |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1532 | */ |
| 1533 | void create_mxac_rsp_buf(char *cc, int maximal_access) |
| 1534 | { |
| 1535 | struct create_mxac_rsp *buf; |
| 1536 | |
| 1537 | buf = (struct create_mxac_rsp *)cc; |
| 1538 | memset(buf, 0, sizeof(struct create_mxac_rsp)); |
| 1539 | buf->ccontext.DataOffset = cpu_to_le16(offsetof |
| 1540 | (struct create_mxac_rsp, QueryStatus)); |
| 1541 | buf->ccontext.DataLength = cpu_to_le32(8); |
| 1542 | buf->ccontext.NameOffset = cpu_to_le16(offsetof |
| 1543 | (struct create_mxac_rsp, Name)); |
| 1544 | buf->ccontext.NameLength = cpu_to_le16(4); |
| 1545 | /* SMB2_CREATE_QUERY_MAXIMAL_ACCESS_RESPONSE is "MxAc" */ |
| 1546 | buf->Name[0] = 'M'; |
| 1547 | buf->Name[1] = 'x'; |
| 1548 | buf->Name[2] = 'A'; |
| 1549 | buf->Name[3] = 'c'; |
| 1550 | |
| 1551 | buf->QueryStatus = STATUS_SUCCESS; |
| 1552 | buf->MaximalAccess = cpu_to_le32(maximal_access); |
| 1553 | } |
| 1554 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1555 | void create_disk_id_rsp_buf(char *cc, __u64 file_id, __u64 vol_id) |
| 1556 | { |
| 1557 | struct create_disk_id_rsp *buf; |
| 1558 | |
| 1559 | buf = (struct create_disk_id_rsp *)cc; |
| 1560 | memset(buf, 0, sizeof(struct create_disk_id_rsp)); |
| 1561 | buf->ccontext.DataOffset = cpu_to_le16(offsetof |
| 1562 | (struct create_disk_id_rsp, DiskFileId)); |
| 1563 | buf->ccontext.DataLength = cpu_to_le32(32); |
| 1564 | buf->ccontext.NameOffset = cpu_to_le16(offsetof |
| 1565 | (struct create_mxac_rsp, Name)); |
| 1566 | buf->ccontext.NameLength = cpu_to_le16(4); |
| 1567 | /* SMB2_CREATE_QUERY_ON_DISK_ID_RESPONSE is "QFid" */ |
| 1568 | buf->Name[0] = 'Q'; |
| 1569 | buf->Name[1] = 'F'; |
| 1570 | buf->Name[2] = 'i'; |
| 1571 | buf->Name[3] = 'd'; |
| 1572 | |
| 1573 | buf->DiskFileId = cpu_to_le64(file_id); |
| 1574 | buf->VolumeId = cpu_to_le64(vol_id); |
| 1575 | } |
| 1576 | |
| 1577 | /** |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 1578 | * create_posix_rsp_buf() - create posix extension context |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1579 | * @cc: buffer to create posix on posix response |
Hyunchul Lee | 95fa1ce | 2021-03-21 17:05:56 +0900 | [diff] [blame] | 1580 | * @fp: ksmbd file pointer |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1581 | */ |
| 1582 | void create_posix_rsp_buf(char *cc, struct ksmbd_file *fp) |
| 1583 | { |
| 1584 | struct create_posix_rsp *buf; |
Namjae Jeon | ab0b263 | 2021-06-29 09:20:13 +0900 | [diff] [blame] | 1585 | struct inode *inode = file_inode(fp->filp); |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 1586 | struct user_namespace *user_ns = file_mnt_user_ns(fp->filp); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1587 | |
| 1588 | buf = (struct create_posix_rsp *)cc; |
| 1589 | memset(buf, 0, sizeof(struct create_posix_rsp)); |
| 1590 | buf->ccontext.DataOffset = cpu_to_le16(offsetof |
| 1591 | (struct create_posix_rsp, nlink)); |
| 1592 | buf->ccontext.DataLength = cpu_to_le32(52); |
| 1593 | buf->ccontext.NameOffset = cpu_to_le16(offsetof |
| 1594 | (struct create_posix_rsp, Name)); |
| 1595 | buf->ccontext.NameLength = cpu_to_le16(POSIX_CTXT_DATA_LEN); |
| 1596 | /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */ |
| 1597 | buf->Name[0] = 0x93; |
| 1598 | buf->Name[1] = 0xAD; |
| 1599 | buf->Name[2] = 0x25; |
| 1600 | buf->Name[3] = 0x50; |
| 1601 | buf->Name[4] = 0x9C; |
| 1602 | buf->Name[5] = 0xB4; |
| 1603 | buf->Name[6] = 0x11; |
| 1604 | buf->Name[7] = 0xE7; |
| 1605 | buf->Name[8] = 0xB4; |
| 1606 | buf->Name[9] = 0x23; |
| 1607 | buf->Name[10] = 0x83; |
| 1608 | buf->Name[11] = 0xDE; |
| 1609 | buf->Name[12] = 0x96; |
| 1610 | buf->Name[13] = 0x8B; |
| 1611 | buf->Name[14] = 0xCD; |
| 1612 | buf->Name[15] = 0x7C; |
| 1613 | |
| 1614 | buf->nlink = cpu_to_le32(inode->i_nlink); |
| 1615 | buf->reparse_tag = cpu_to_le32(fp->volatile_id); |
| 1616 | buf->mode = cpu_to_le32(inode->i_mode); |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 1617 | id_to_sid(from_kuid(user_ns, inode->i_uid), |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1618 | SIDNFS_USER, (struct smb_sid *)&buf->SidBuffer[0]); |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 1619 | id_to_sid(from_kgid(user_ns, inode->i_gid), |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1620 | SIDNFS_GROUP, (struct smb_sid *)&buf->SidBuffer[20]); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1621 | } |
| 1622 | |
| 1623 | /* |
| 1624 | * Find lease object(opinfo) for given lease key/fid from lease |
| 1625 | * break/file close path. |
| 1626 | */ |
| 1627 | /** |
| 1628 | * lookup_lease_in_table() - find a matching lease info object |
| 1629 | * @conn: connection instance |
| 1630 | * @lease_key: lease key to be searched for |
| 1631 | * |
| 1632 | * Return: opinfo if found matching opinfo, otherwise NULL |
| 1633 | */ |
| 1634 | struct oplock_info *lookup_lease_in_table(struct ksmbd_conn *conn, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1635 | char *lease_key) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1636 | { |
| 1637 | struct oplock_info *opinfo = NULL, *ret_op = NULL; |
| 1638 | struct lease_table *lt; |
| 1639 | int ret; |
| 1640 | |
| 1641 | read_lock(&lease_list_lock); |
| 1642 | list_for_each_entry(lt, &lease_table_list, l_entry) { |
| 1643 | if (!memcmp(lt->client_guid, conn->ClientGUID, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1644 | SMB2_CLIENT_GUID_SIZE)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1645 | goto found; |
| 1646 | } |
| 1647 | |
| 1648 | read_unlock(&lease_list_lock); |
| 1649 | return NULL; |
| 1650 | |
| 1651 | found: |
| 1652 | rcu_read_lock(); |
| 1653 | list_for_each_entry_rcu(opinfo, <->lease_list, lease_entry) { |
| 1654 | if (!atomic_inc_not_zero(&opinfo->refcount)) |
| 1655 | continue; |
| 1656 | rcu_read_unlock(); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1657 | if (!opinfo->op_state || opinfo->op_state == OPLOCK_CLOSING) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1658 | goto op_next; |
| 1659 | if (!(opinfo->o_lease->state & |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1660 | (SMB2_LEASE_HANDLE_CACHING_LE | |
| 1661 | SMB2_LEASE_WRITE_CACHING_LE))) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1662 | goto op_next; |
| 1663 | ret = compare_guid_key(opinfo, conn->ClientGUID, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1664 | lease_key); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1665 | if (ret) { |
| 1666 | ksmbd_debug(OPLOCK, "found opinfo\n"); |
| 1667 | ret_op = opinfo; |
| 1668 | goto out; |
| 1669 | } |
| 1670 | op_next: |
| 1671 | opinfo_put(opinfo); |
| 1672 | rcu_read_lock(); |
| 1673 | } |
| 1674 | rcu_read_unlock(); |
| 1675 | |
| 1676 | out: |
| 1677 | read_unlock(&lease_list_lock); |
| 1678 | return ret_op; |
| 1679 | } |
| 1680 | |
| 1681 | int smb2_check_durable_oplock(struct ksmbd_file *fp, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1682 | struct lease_ctx_info *lctx, char *name) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1683 | { |
| 1684 | struct oplock_info *opinfo = opinfo_get(fp); |
| 1685 | int ret = 0; |
| 1686 | |
| 1687 | if (opinfo && opinfo->is_lease) { |
| 1688 | if (!lctx) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 1689 | pr_err("open does not include lease\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1690 | ret = -EBADF; |
| 1691 | goto out; |
| 1692 | } |
| 1693 | if (memcmp(opinfo->o_lease->lease_key, lctx->lease_key, |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1694 | SMB2_LEASE_KEY_SIZE)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 1695 | pr_err("invalid lease key\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1696 | ret = -EBADF; |
| 1697 | goto out; |
| 1698 | } |
| 1699 | if (name && strcmp(fp->filename, name)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 1700 | pr_err("invalid name reconnect %s\n", name); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1701 | ret = -EINVAL; |
| 1702 | goto out; |
| 1703 | } |
| 1704 | } |
| 1705 | out: |
| 1706 | if (opinfo) |
| 1707 | opinfo_put(opinfo); |
| 1708 | return ret; |
| 1709 | } |