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 #include <linux/err.h> 9 10 #include "glob.h" 11 #include "oplock.h" 12 13 #include "smb_common.h" 14 #include "../common/smb2status.h" 15 #include "connection.h" 16 #include "mgmt/user_session.h" 17 #include "mgmt/share_config.h" 18 #include "mgmt/tree_connect.h" 19 #include "server.h" 20 21 static LIST_HEAD(lease_table_list); 22 static DEFINE_RWLOCK(lease_list_lock); 23 static DEFINE_MUTEX(lease_break_timer_lock); 24 static unsigned long lease_break_timer_next; 25 static bool lease_break_timer_pending; 26 static void lease_break_timer(struct work_struct *work); 27 static DECLARE_DELAYED_WORK(lease_break_timer_work, lease_break_timer); 28 29 #define SMB2_LEASE_STATE_MASK_LE (SMB2_LEASE_READ_CACHING_LE | \ 30 SMB2_LEASE_HANDLE_CACHING_LE | \ 31 SMB2_LEASE_WRITE_CACHING_LE) 32 33 static bool lease_state_valid(__le32 state) 34 { 35 return !(state & ~SMB2_LEASE_STATE_MASK_LE); 36 } 37 38 static __le32 lease_state_grantable(__le32 state) 39 { 40 if (state == SMB2_LEASE_READ_CACHING_LE || 41 state == (SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_HANDLE_CACHING_LE) || 42 state == (SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_WRITE_CACHING_LE) || 43 state == SMB2_LEASE_STATE_MASK_LE) 44 return state; 45 46 return 0; 47 } 48 49 static bool lease_v2_flags_valid(__le32 flags) 50 { 51 return !(flags & ~SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET_LE); 52 } 53 54 static bool lease_has_parent_key(struct lease *lease) 55 { 56 return lease->flags & SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET_LE; 57 } 58 59 static bool lease_break_in_progress(struct lease *lease) 60 { 61 struct oplock_info *opinfo; 62 bool ret = false; 63 64 spin_lock(&lease->lock); 65 list_for_each_entry(opinfo, &lease->open_list, lease_entry) { 66 if (opinfo->op_state == OPLOCK_ACK_WAIT) { 67 ret = true; 68 break; 69 } 70 } 71 spin_unlock(&lease->lock); 72 73 return ret; 74 } 75 76 /** 77 * alloc_opinfo() - allocate a new opinfo object for oplock info 78 * @work: smb work 79 * @id: fid of open file 80 * @Tid: tree id of connection 81 * 82 * Return: allocated opinfo object on success, otherwise NULL 83 */ 84 static struct oplock_info *alloc_opinfo(struct ksmbd_work *work, 85 u64 id, __u16 Tid) 86 { 87 struct ksmbd_session *sess = work->sess; 88 struct oplock_info *opinfo; 89 90 opinfo = kzalloc_obj(struct oplock_info, KSMBD_DEFAULT_GFP); 91 if (!opinfo) 92 return NULL; 93 94 opinfo->sess = sess; 95 opinfo->session_id = sess->id; 96 opinfo->conn = ksmbd_conn_get(work->conn); 97 opinfo->level = SMB2_OPLOCK_LEVEL_NONE; 98 opinfo->op_state = OPLOCK_STATE_NONE; 99 spin_lock_init(&opinfo->state_lock); 100 opinfo->pending_break = 0; 101 opinfo->fid = id; 102 opinfo->Tid = Tid; 103 INIT_LIST_HEAD(&opinfo->op_entry); 104 INIT_LIST_HEAD(&opinfo->lease_entry); 105 init_waitqueue_head(&opinfo->oplock_q); 106 init_waitqueue_head(&opinfo->oplock_brk); 107 atomic_set(&opinfo->refcount, 1); 108 atomic_set(&opinfo->breaking_cnt, 0); 109 110 return opinfo; 111 } 112 113 static void lease_get(struct lease *lease) 114 { 115 atomic_inc(&lease->refcount); 116 } 117 118 static void lease_put(struct lease *lease) 119 { 120 if (lease && atomic_dec_and_test(&lease->refcount)) 121 kfree(lease); 122 } 123 124 /* The caller holds lease->lock. */ 125 static void lease_break_to_none_locked(struct lease *lease) 126 { 127 struct oplock_info *opinfo; 128 bool wake; 129 130 lease->state = SMB2_LEASE_NONE_LE; 131 lease->new_state = SMB2_LEASE_NONE_LE; 132 lease->breaking = false; 133 lease->break_timeout_set = false; 134 list_for_each_entry(opinfo, &lease->open_list, lease_entry) { 135 spin_lock(&opinfo->state_lock); 136 opinfo->level = SMB2_OPLOCK_LEVEL_NONE; 137 wake = opinfo->op_state == OPLOCK_ACK_WAIT; 138 if (wake) { 139 opinfo->op_state = OPLOCK_STATE_NONE; 140 opinfo->oplock_timed_out = true; 141 atomic_set(&opinfo->breaking_cnt, 0); 142 } 143 spin_unlock(&opinfo->state_lock); 144 if (wake) { 145 wake_up_all(&opinfo->oplock_q); 146 wake_up_interruptible_all(&opinfo->oplock_brk); 147 } 148 } 149 } 150 151 static void lease_break_timer(struct work_struct *work) 152 { 153 struct lease_table *lb; 154 struct lease *lease; 155 unsigned long next = 0, now; 156 bool pending = false; 157 158 mutex_lock(&lease_break_timer_lock); 159 now = jiffies; 160 read_lock(&lease_list_lock); 161 list_for_each_entry(lb, &lease_table_list, l_entry) { 162 list_for_each_entry(lease, &lb->lease_list, l_entry) { 163 spin_lock(&lease->lock); 164 if (lease->breaking && lease->break_timeout_set) { 165 if (time_after_eq(now, lease->break_timeout)) { 166 lease_break_to_none_locked(lease); 167 } else if (!pending || 168 time_before(lease->break_timeout, next)) { 169 next = lease->break_timeout; 170 pending = true; 171 } 172 } 173 spin_unlock(&lease->lock); 174 } 175 } 176 read_unlock(&lease_list_lock); 177 178 lease_break_timer_pending = pending; 179 if (pending) { 180 lease_break_timer_next = next; 181 mod_delayed_work(system_percpu_wq, &lease_break_timer_work, 182 time_after_eq(jiffies, next) ? 0 : next - jiffies); 183 } 184 mutex_unlock(&lease_break_timer_lock); 185 } 186 187 static void schedule_lease_break_timer(unsigned long timeout) 188 { 189 mutex_lock(&lease_break_timer_lock); 190 if (!lease_break_timer_pending || 191 time_before(timeout, lease_break_timer_next)) { 192 lease_break_timer_pending = true; 193 lease_break_timer_next = timeout; 194 mod_delayed_work(system_percpu_wq, &lease_break_timer_work, 195 time_after_eq(jiffies, timeout) ? 0 : timeout - jiffies); 196 } 197 mutex_unlock(&lease_break_timer_lock); 198 } 199 200 static void lease_add_table(struct lease *lease, struct lease_table *lb) 201 { 202 lease_get(lease); 203 lease->l_lb = lb; 204 spin_lock(&lb->lb_lock); 205 list_add_rcu(&lease->l_entry, &lb->lease_list); 206 spin_unlock(&lb->lb_lock); 207 } 208 209 static void free_lease_table(struct lease_table *lb) 210 { 211 kfree(lb); 212 } 213 214 /* The caller holds lease_list_lock for write. */ 215 static void lease_del_table(struct lease *lease) 216 { 217 struct lease_table *lb = lease->l_lb; 218 219 if (!lb) 220 return; 221 222 spin_lock(&lb->lb_lock); 223 if (list_empty(&lease->l_entry)) { 224 spin_unlock(&lb->lb_lock); 225 return; 226 } 227 228 list_del_init(&lease->l_entry); 229 lease->l_lb = NULL; 230 spin_unlock(&lb->lb_lock); 231 232 lease_put(lease); 233 if (list_empty(&lb->lease_list)) { 234 list_del(&lb->l_entry); 235 free_lease_table(lb); 236 } 237 } 238 239 static struct lease_table *alloc_lease_table(struct oplock_info *opinfo) 240 { 241 struct lease_table *lb; 242 243 lb = kmalloc_obj(struct lease_table, KSMBD_DEFAULT_GFP); 244 if (!lb) 245 return NULL; 246 247 memcpy(lb->client_guid, opinfo->conn->ClientGUID, 248 SMB2_CLIENT_GUID_SIZE); 249 INIT_LIST_HEAD(&lb->lease_list); 250 spin_lock_init(&lb->lb_lock); 251 return lb; 252 } 253 254 static struct lease *alloc_lease(struct lease_ctx_info *lctx, 255 struct ksmbd_inode *ci) 256 { 257 struct lease *lease; 258 259 lease = kmalloc_obj(struct lease, KSMBD_DEFAULT_GFP); 260 if (!lease) 261 return NULL; 262 263 memcpy(lease->lease_key, lctx->lease_key, SMB2_LEASE_KEY_SIZE); 264 lease->state = lctx->req_state; 265 lease->new_state = 0; 266 lease->flags = lctx->flags; 267 lease->duration = lctx->duration; 268 lease->is_dir = lctx->is_dir; 269 memcpy(lease->parent_lease_key, lctx->parent_lease_key, SMB2_LEASE_KEY_SIZE); 270 lease->version = lctx->version; 271 lease->epoch = lctx->version == 2 ? le16_to_cpu(lctx->epoch) + 1 : 0; 272 lease->ci = ci; 273 lease->reuse_epoch = false; 274 lease->breaking = false; 275 lease->break_timeout_set = false; 276 lease->l_lb = NULL; 277 INIT_LIST_HEAD(&lease->l_entry); 278 INIT_LIST_HEAD(&lease->open_list); 279 spin_lock_init(&lease->lock); 280 atomic_set(&lease->refcount, 1); 281 282 return lease; 283 } 284 285 static void lease_add_open(struct lease *lease, struct oplock_info *opinfo) 286 { 287 spin_lock(&lease->lock); 288 list_add(&opinfo->lease_entry, &lease->open_list); 289 spin_unlock(&lease->lock); 290 } 291 292 static void lease_del_open(struct oplock_info *opinfo) 293 { 294 struct lease *lease = opinfo->o_lease; 295 bool remove_table = false; 296 297 if (!lease) 298 return; 299 300 spin_lock(&lease->lock); 301 if (!list_empty(&opinfo->lease_entry)) { 302 list_del_init(&opinfo->lease_entry); 303 remove_table = list_empty(&lease->open_list); 304 } 305 spin_unlock(&lease->lock); 306 307 if (remove_table) { 308 write_lock(&lease_list_lock); 309 lease_del_table(lease); 310 write_unlock(&lease_list_lock); 311 } 312 } 313 314 static void free_lease(struct oplock_info *opinfo) 315 { 316 lease_put(opinfo->o_lease); 317 } 318 319 static void __free_opinfo(struct oplock_info *opinfo) 320 { 321 if (opinfo->is_lease) 322 free_lease(opinfo); 323 ksmbd_conn_put(opinfo->conn); 324 kfree(opinfo); 325 } 326 327 static void free_opinfo_rcu(struct rcu_head *rcu) 328 { 329 struct oplock_info *opinfo = container_of(rcu, struct oplock_info, rcu); 330 331 __free_opinfo(opinfo); 332 } 333 334 static void free_opinfo(struct oplock_info *opinfo) 335 { 336 call_rcu(&opinfo->rcu, free_opinfo_rcu); 337 } 338 339 void lease_update_oplock_levels(struct lease *lease) 340 { 341 struct oplock_info *opinfo; 342 __u8 level; 343 344 if (!lease) 345 return; 346 347 level = smb2_map_lease_to_oplock(lease->state); 348 spin_lock(&lease->lock); 349 list_for_each_entry(opinfo, &lease->open_list, lease_entry) 350 opinfo->level = level; 351 spin_unlock(&lease->lock); 352 } 353 354 struct oplock_info *opinfo_get(struct ksmbd_file *fp) 355 { 356 struct oplock_info *opinfo; 357 358 rcu_read_lock(); 359 opinfo = rcu_dereference(fp->f_opinfo); 360 if (opinfo && !atomic_inc_not_zero(&opinfo->refcount)) 361 opinfo = NULL; 362 rcu_read_unlock(); 363 364 return opinfo; 365 } 366 367 struct oplock_snapshot { 368 bool durable_open; 369 bool durable_detached; 370 unsigned long long fid; 371 }; 372 373 static struct oplock_info *opinfo_get_list(struct ksmbd_inode *ci, 374 struct ksmbd_file *skip_fp, 375 struct oplock_snapshot *snapshot) 376 { 377 struct oplock_info *opinfo; 378 379 if (snapshot) { 380 snapshot->durable_open = false; 381 snapshot->durable_detached = false; 382 snapshot->fid = KSMBD_NO_FID; 383 } 384 385 down_read(&ci->m_lock); 386 opinfo = list_first_entry_or_null(&ci->m_op_list, struct oplock_info, 387 op_entry); 388 if (opinfo) { 389 /* A live open may still be bound to a disconnected channel. */ 390 if (!atomic_inc_not_zero(&opinfo->refcount)) 391 opinfo = NULL; 392 393 if (opinfo && snapshot && opinfo->o_fp && 394 opinfo->o_fp != skip_fp && 395 READ_ONCE(opinfo->o_fp->is_durable)) { 396 snapshot->durable_open = true; 397 snapshot->durable_detached = 398 !READ_ONCE(opinfo->o_fp->conn) || 399 !READ_ONCE(opinfo->o_fp->tcon); 400 snapshot->fid = opinfo->fid; 401 } 402 } 403 up_read(&ci->m_lock); 404 405 return opinfo; 406 } 407 408 void opinfo_put(struct oplock_info *opinfo) 409 { 410 if (!opinfo) 411 return; 412 413 if (!atomic_dec_and_test(&opinfo->refcount)) 414 return; 415 416 free_opinfo(opinfo); 417 } 418 419 static bool ksmbd_inode_has_lease(struct ksmbd_inode *ci) 420 { 421 struct oplock_info *opinfo = opinfo_get_list(ci, NULL, NULL); 422 bool is_lease; 423 424 if (!opinfo) 425 return false; 426 is_lease = opinfo->is_lease; 427 opinfo_put(opinfo); 428 return is_lease; 429 } 430 431 static void opinfo_add(struct oplock_info *opinfo, struct ksmbd_file *fp) 432 { 433 struct ksmbd_inode *ci = fp->f_ci; 434 435 down_write(&ci->m_lock); 436 list_add(&opinfo->op_entry, &ci->m_op_list); 437 up_write(&ci->m_lock); 438 } 439 440 static void opinfo_del(struct oplock_info *opinfo) 441 { 442 struct ksmbd_inode *ci = opinfo->o_fp->f_ci; 443 444 if (opinfo->is_lease) 445 lease_del_open(opinfo); 446 447 down_write(&ci->m_lock); 448 list_del_init(&opinfo->op_entry); 449 up_write(&ci->m_lock); 450 } 451 452 static unsigned long opinfo_count(struct ksmbd_file *fp) 453 { 454 if (ksmbd_stream_fd(fp)) 455 return atomic_read(&fp->f_ci->sop_count); 456 else 457 return atomic_read(&fp->f_ci->op_count); 458 } 459 460 static void opinfo_count_inc(struct ksmbd_file *fp) 461 { 462 if (ksmbd_stream_fd(fp)) 463 return atomic_inc(&fp->f_ci->sop_count); 464 else 465 return atomic_inc(&fp->f_ci->op_count); 466 } 467 468 static void opinfo_count_dec(struct ksmbd_file *fp) 469 { 470 if (ksmbd_stream_fd(fp)) 471 return atomic_dec(&fp->f_ci->sop_count); 472 else 473 return atomic_dec(&fp->f_ci->op_count); 474 } 475 476 /** 477 * opinfo_write_to_read() - convert a write oplock to read oplock 478 * @opinfo: current oplock info 479 * 480 * Return: 0 on success, otherwise -EINVAL 481 */ 482 int opinfo_write_to_read(struct oplock_info *opinfo) 483 { 484 struct lease *lease = opinfo->o_lease; 485 486 if (!(opinfo->level == SMB2_OPLOCK_LEVEL_BATCH || 487 opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE)) { 488 pr_err("bad oplock(0x%x)\n", opinfo->level); 489 if (opinfo->is_lease) 490 pr_err("lease state(0x%x)\n", lease->state); 491 return -EINVAL; 492 } 493 opinfo->level = SMB2_OPLOCK_LEVEL_II; 494 495 if (opinfo->is_lease) { 496 lease->state = lease->new_state; 497 lease_update_oplock_levels(lease); 498 } 499 return 0; 500 } 501 502 /** 503 * opinfo_read_handle_to_read() - convert a read/handle oplock to read oplock 504 * @opinfo: current oplock info 505 * 506 * Return: 0 on success, otherwise -EINVAL 507 */ 508 int opinfo_read_handle_to_read(struct oplock_info *opinfo) 509 { 510 struct lease *lease = opinfo->o_lease; 511 512 lease->state = lease->new_state; 513 lease_update_oplock_levels(lease); 514 return 0; 515 } 516 517 /** 518 * opinfo_write_to_none() - convert a write oplock to none 519 * @opinfo: current oplock info 520 * 521 * Return: 0 on success, otherwise -EINVAL 522 */ 523 int opinfo_write_to_none(struct oplock_info *opinfo) 524 { 525 struct lease *lease = opinfo->o_lease; 526 527 if (!(opinfo->level == SMB2_OPLOCK_LEVEL_BATCH || 528 opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE)) { 529 pr_err("bad oplock(0x%x)\n", opinfo->level); 530 if (opinfo->is_lease) 531 pr_err("lease state(0x%x)\n", lease->state); 532 return -EINVAL; 533 } 534 opinfo->level = SMB2_OPLOCK_LEVEL_NONE; 535 if (opinfo->is_lease) { 536 lease->state = lease->new_state; 537 lease_update_oplock_levels(lease); 538 } 539 return 0; 540 } 541 542 /** 543 * opinfo_read_to_none() - convert a write read to none 544 * @opinfo: current oplock info 545 * 546 * Return: 0 on success, otherwise -EINVAL 547 */ 548 int opinfo_read_to_none(struct oplock_info *opinfo) 549 { 550 struct lease *lease = opinfo->o_lease; 551 552 if (opinfo->level != SMB2_OPLOCK_LEVEL_II) { 553 pr_err("bad oplock(0x%x)\n", opinfo->level); 554 if (opinfo->is_lease) 555 pr_err("lease state(0x%x)\n", lease->state); 556 return -EINVAL; 557 } 558 opinfo->level = SMB2_OPLOCK_LEVEL_NONE; 559 if (opinfo->is_lease) { 560 lease->state = lease->new_state; 561 lease_update_oplock_levels(lease); 562 } 563 return 0; 564 } 565 566 /** 567 * lease_read_to_write() - upgrade lease state from read to write 568 * @opinfo: current lease info 569 * 570 * Return: 0 on success, otherwise -EINVAL 571 */ 572 int lease_read_to_write(struct oplock_info *opinfo) 573 { 574 struct lease *lease = opinfo->o_lease; 575 576 if (!(lease->state & SMB2_LEASE_READ_CACHING_LE)) { 577 ksmbd_debug(OPLOCK, "bad lease state(0x%x)\n", lease->state); 578 return -EINVAL; 579 } 580 581 lease->new_state = SMB2_LEASE_NONE_LE; 582 lease->state |= SMB2_LEASE_WRITE_CACHING_LE; 583 lease_update_oplock_levels(lease); 584 return 0; 585 } 586 587 /** 588 * lease_none_upgrade() - upgrade lease state from none 589 * @opinfo: current lease info 590 * @new_state: new lease state 591 * 592 * Return: 0 on success, otherwise -EINVAL 593 */ 594 static int lease_none_upgrade(struct oplock_info *opinfo, __le32 new_state) 595 { 596 struct lease *lease = opinfo->o_lease; 597 598 if (!(lease->state == SMB2_LEASE_NONE_LE)) { 599 ksmbd_debug(OPLOCK, "bad lease state(0x%x)\n", lease->state); 600 return -EINVAL; 601 } 602 603 lease->new_state = SMB2_LEASE_NONE_LE; 604 lease->state = new_state; 605 lease_update_oplock_levels(lease); 606 607 return 0; 608 } 609 610 /** 611 * close_id_del_oplock() - release oplock object at file close time 612 * @fp: ksmbd file pointer 613 */ 614 void close_id_del_oplock(struct ksmbd_file *fp) 615 { 616 struct oplock_info *opinfo; 617 618 if (fp->reserve_lease_break) 619 smb_lazy_parent_lease_break_close(fp); 620 621 opinfo = opinfo_get(fp); 622 if (!opinfo) 623 return; 624 625 opinfo_del(opinfo); 626 627 rcu_assign_pointer(fp->f_opinfo, NULL); 628 spin_lock(&opinfo->state_lock); 629 if (opinfo->op_state == OPLOCK_ACK_WAIT && opinfo->is_lease) 630 atomic_set(&opinfo->breaking_cnt, 0); 631 /* 632 * An opinfo that has been removed from the inode list is terminal. Keep 633 * this transition and releasing pending_break under state_lock. a breaker 634 * takes the same lock before it acquires pending_break or sets ACK_WAIT. 635 */ 636 opinfo->op_state = OPLOCK_CLOSING; 637 opinfo->oplock_timeout_set = false; 638 clear_bit_unlock(0, &opinfo->pending_break); 639 spin_unlock(&opinfo->state_lock); 640 wake_up_all(&opinfo->oplock_q); 641 if (opinfo->is_lease) 642 wake_up_interruptible_all(&opinfo->oplock_brk); 643 /* memory barrier is needed for wake_up_bit() */ 644 smp_mb__after_atomic(); 645 wake_up_bit(&opinfo->pending_break, 0); 646 647 opinfo_count_dec(fp); 648 atomic_dec(&opinfo->refcount); 649 opinfo_put(opinfo); 650 } 651 652 /** 653 * grant_write_oplock() - grant exclusive/batch oplock or write lease 654 * @opinfo_new: new oplock info object 655 * @req_oplock: request oplock 656 * @lctx: lease context information 657 * 658 * Return: 0 659 */ 660 static void grant_write_oplock(struct oplock_info *opinfo_new, int req_oplock, 661 struct lease_ctx_info *lctx) 662 { 663 struct lease *lease = opinfo_new->o_lease; 664 665 if (req_oplock == SMB2_OPLOCK_LEVEL_BATCH) 666 opinfo_new->level = SMB2_OPLOCK_LEVEL_BATCH; 667 else 668 opinfo_new->level = SMB2_OPLOCK_LEVEL_EXCLUSIVE; 669 670 if (lctx) { 671 lease->state = lctx->req_state; 672 memcpy(lease->lease_key, lctx->lease_key, SMB2_LEASE_KEY_SIZE); 673 } 674 } 675 676 /** 677 * grant_read_oplock() - grant level2 oplock or read lease 678 * @opinfo_new: new oplock info object 679 * @lctx: lease context information 680 * 681 * Return: 0 682 */ 683 static void grant_read_oplock(struct oplock_info *opinfo_new, 684 struct lease_ctx_info *lctx) 685 { 686 struct lease *lease = opinfo_new->o_lease; 687 688 opinfo_new->level = SMB2_OPLOCK_LEVEL_II; 689 690 if (lctx) { 691 lease->state = SMB2_LEASE_READ_CACHING_LE; 692 if (lctx->req_state & SMB2_LEASE_HANDLE_CACHING_LE) 693 lease->state |= SMB2_LEASE_HANDLE_CACHING_LE; 694 memcpy(lease->lease_key, lctx->lease_key, SMB2_LEASE_KEY_SIZE); 695 } 696 } 697 698 /** 699 * grant_none_oplock() - grant none oplock or none lease 700 * @opinfo_new: new oplock info object 701 * @lctx: lease context information 702 * 703 * Return: 0 704 */ 705 static void grant_none_oplock(struct oplock_info *opinfo_new, 706 struct lease_ctx_info *lctx) 707 { 708 struct lease *lease = opinfo_new->o_lease; 709 710 opinfo_new->level = SMB2_OPLOCK_LEVEL_NONE; 711 712 if (lctx) { 713 lease->state = 0; 714 memcpy(lease->lease_key, lctx->lease_key, SMB2_LEASE_KEY_SIZE); 715 } 716 } 717 718 static inline int compare_guid_key(struct oplock_info *opinfo, 719 const char *guid1, const char *key1) 720 { 721 struct lease *lease = opinfo->o_lease; 722 struct lease_table *lb; 723 struct ksmbd_conn *conn; 724 int ret = 0; 725 726 if (memcmp(key1, lease->lease_key, SMB2_LEASE_KEY_SIZE)) 727 return 0; 728 729 /* The caller holds ci->m_lock, which protects opinfo->conn. */ 730 conn = READ_ONCE(opinfo->conn); 731 if (conn) 732 return !memcmp(guid1, conn->ClientGUID, SMB2_CLIENT_GUID_SIZE); 733 734 /* A detached durable lease retains its client identity in the table. */ 735 read_lock(&lease_list_lock); 736 lb = lease->l_lb; 737 if (lb) 738 ret = !memcmp(guid1, lb->client_guid, SMB2_CLIENT_GUID_SIZE); 739 read_unlock(&lease_list_lock); 740 741 return ret; 742 } 743 744 /** 745 * same_client_has_lease() - check whether current lease request is 746 * from lease owner of file 747 * @ci: master file pointer 748 * @client_guid: Client GUID 749 * @lctx: lease context information 750 * 751 * Return: oplock(lease) object on success, otherwise NULL 752 */ 753 static struct oplock_info *same_client_has_lease(struct ksmbd_inode *ci, 754 const char *client_guid, 755 struct lease_ctx_info *lctx) 756 { 757 int ret; 758 struct lease *lease; 759 struct oplock_info *opinfo; 760 struct oplock_info *m_opinfo = NULL; 761 762 if (!lctx) 763 return NULL; 764 765 /* 766 * Compare lease key and client_guid to know request from same owner 767 * of same client 768 */ 769 down_read(&ci->m_lock); 770 list_for_each_entry(opinfo, &ci->m_op_list, op_entry) { 771 if (!opinfo->is_lease) 772 continue; 773 lease = opinfo->o_lease; 774 775 ret = compare_guid_key(opinfo, client_guid, lctx->lease_key); 776 if (ret) { 777 if (!atomic_inc_not_zero(&opinfo->refcount)) 778 continue; 779 if (m_opinfo) 780 opinfo_put(m_opinfo); 781 m_opinfo = opinfo; 782 783 /* skip upgrading lease about breaking lease */ 784 if (atomic_read(&opinfo->breaking_cnt)) 785 continue; 786 787 /* upgrading lease */ 788 if ((atomic_read(&ci->op_count) + 789 atomic_read(&ci->sop_count)) == 1) { 790 if (lease->state != SMB2_LEASE_NONE_LE && 791 lease->state == (lctx->req_state & lease->state)) { 792 lease->epoch++; 793 lease->state |= lctx->req_state; 794 if (lctx->req_state & 795 SMB2_LEASE_WRITE_CACHING_LE) 796 lease_read_to_write(opinfo); 797 798 } 799 } else if ((atomic_read(&ci->op_count) + 800 atomic_read(&ci->sop_count)) > 1) { 801 if (lctx->req_state == 802 (SMB2_LEASE_READ_CACHING_LE | 803 SMB2_LEASE_HANDLE_CACHING_LE)) { 804 if (lease->state != lctx->req_state) { 805 lease->epoch++; 806 lease->state = lctx->req_state; 807 lease_update_oplock_levels(lease); 808 } 809 } 810 } 811 812 if (lctx->req_state && lease->state == 813 SMB2_LEASE_NONE_LE) { 814 lease->epoch++; 815 lease_none_upgrade(opinfo, lctx->req_state); 816 } 817 } 818 } 819 up_read(&ci->m_lock); 820 821 return m_opinfo; 822 } 823 824 static bool oplock_break_set_ack_wait(struct oplock_info *opinfo) 825 { 826 bool ret = false; 827 828 spin_lock(&opinfo->state_lock); 829 if (opinfo->op_state != OPLOCK_CLOSING) { 830 opinfo->op_state = OPLOCK_ACK_WAIT; 831 opinfo->oplock_timeout_set = false; 832 opinfo->oplock_timed_out = false; 833 ret = true; 834 } 835 spin_unlock(&opinfo->state_lock); 836 837 return ret; 838 } 839 840 static int oplock_break_pending(struct oplock_info *opinfo, int req_op_level) 841 { 842 for (;;) { 843 bool closing; 844 845 spin_lock(&opinfo->state_lock); 846 closing = opinfo->op_state == OPLOCK_CLOSING; 847 if (!closing && !test_and_set_bit(0, &opinfo->pending_break)) { 848 spin_unlock(&opinfo->state_lock); 849 break; 850 } 851 spin_unlock(&opinfo->state_lock); 852 if (closing) 853 return -ENOENT; 854 855 if (opinfo->is_lease) 856 opinfo->o_lease->reuse_epoch = true; 857 858 wait_on_bit(&opinfo->pending_break, 0, TASK_UNINTERRUPTIBLE); 859 860 /* Not immediately break to none. */ 861 opinfo->open_trunc = 0; 862 863 spin_lock(&opinfo->state_lock); 864 closing = opinfo->op_state == OPLOCK_CLOSING; 865 spin_unlock(&opinfo->state_lock); 866 if (closing) 867 return -ENOENT; 868 if (opinfo->level <= req_op_level) { 869 if (opinfo->is_lease == false) 870 return 1; 871 872 if (opinfo->o_lease->state != 873 (SMB2_LEASE_HANDLE_CACHING_LE | 874 SMB2_LEASE_READ_CACHING_LE)) 875 return 1; 876 } 877 } 878 879 if (opinfo->level <= req_op_level) { 880 if (opinfo->is_lease == false) { 881 clear_and_wake_up_bit(0, &opinfo->pending_break); 882 return 1; 883 } 884 if (opinfo->o_lease->state != 885 (SMB2_LEASE_HANDLE_CACHING_LE | 886 SMB2_LEASE_READ_CACHING_LE)) { 887 clear_and_wake_up_bit(0, &opinfo->pending_break); 888 return 1; 889 } 890 } 891 return 0; 892 } 893 894 static bool lease_break_needed(struct oplock_info *opinfo, int req_op_level, 895 bool open_trunc) 896 { 897 struct lease *lease = opinfo->o_lease; 898 899 if (open_trunc) 900 return lease->state != SMB2_LEASE_NONE_LE; 901 902 return opinfo->level > req_op_level; 903 } 904 905 static void smb2_break_start_timer(struct oplock_info *opinfo, 906 unsigned long timeout) 907 { 908 struct lease *lease = opinfo->o_lease; 909 struct oplock_info *open; 910 bool start = false, waiting; 911 912 if (opinfo->is_lease) { 913 read_lock(&lease_list_lock); 914 spin_lock(&lease->lock); 915 if (!lease->l_lb) { 916 lease_break_to_none_locked(lease); 917 } else if (!lease->breaking) { 918 list_for_each_entry(open, &lease->open_list, lease_entry) { 919 spin_lock(&open->state_lock); 920 waiting = open->op_state == OPLOCK_ACK_WAIT; 921 spin_unlock(&open->state_lock); 922 if (!waiting) 923 continue; 924 lease->break_timeout = timeout; 925 lease->break_timeout_set = true; 926 lease->breaking = true; 927 start = true; 928 break; 929 } 930 } 931 spin_unlock(&lease->lock); 932 read_unlock(&lease_list_lock); 933 if (start) 934 schedule_lease_break_timer(timeout); 935 return; 936 } 937 938 spin_lock(&opinfo->state_lock); 939 if (opinfo->op_state == OPLOCK_ACK_WAIT && !opinfo->oplock_timeout_set) { 940 opinfo->oplock_timeout = timeout; 941 opinfo->oplock_timeout_set = true; 942 start = true; 943 } 944 spin_unlock(&opinfo->state_lock); 945 if (start) 946 ksmbd_schedule_oplock_break_timer(timeout); 947 } 948 949 struct smb2_break_channel { 950 struct list_head list; 951 struct ksmbd_conn *conn; 952 struct ksmbd_tx_tracking tracking; 953 bool queued; 954 bool delivered; 955 bool failed; 956 }; 957 958 static bool smb2_break_complete(struct oplock_info *opinfo, bool expect_ack) 959 { 960 bool complete; 961 962 spin_lock(&opinfo->state_lock); 963 complete = opinfo->op_state == OPLOCK_CLOSING || 964 (expect_ack && opinfo->op_state == OPLOCK_STATE_NONE); 965 spin_unlock(&opinfo->state_lock); 966 return complete; 967 } 968 969 static int smb2_break_failed(struct oplock_info *opinfo, bool expect_ack) 970 { 971 struct lease *lease = opinfo->o_lease; 972 bool timed_out; 973 974 if (opinfo->is_lease) 975 spin_lock(&lease->lock); 976 spin_lock(&opinfo->state_lock); 977 /* An ACK or close which won the race has already completed the break. */ 978 if (opinfo->op_state == OPLOCK_CLOSING || 979 (expect_ack && opinfo->op_state == OPLOCK_STATE_NONE)) { 980 timed_out = opinfo->oplock_timed_out; 981 spin_unlock(&opinfo->state_lock); 982 if (opinfo->is_lease) 983 spin_unlock(&lease->lock); 984 return timed_out ? ksmbd_invalidate_durable_fd(opinfo->fid) : 0; 985 } 986 if (opinfo->is_lease) { 987 spin_unlock(&opinfo->state_lock); 988 lease_break_to_none_locked(lease); 989 spin_unlock(&lease->lock); 990 } else { 991 opinfo->level = SMB2_OPLOCK_LEVEL_NONE; 992 opinfo->op_state = OPLOCK_STATE_NONE; 993 opinfo->oplock_timeout_set = false; 994 opinfo->oplock_timed_out = true; 995 spin_unlock(&opinfo->state_lock); 996 wake_up_all(&opinfo->oplock_q); 997 } 998 wake_up_interruptible_all(&opinfo->oplock_brk); 999 return ksmbd_invalidate_durable_fd(opinfo->fid); 1000 } 1001 1002 static int smb2_break_add_channel(struct list_head *head, struct ksmbd_conn *conn) 1003 { 1004 struct smb2_break_channel *channel; 1005 1006 channel = kzalloc_obj(*channel, KSMBD_DEFAULT_GFP); 1007 if (!channel) 1008 return -ENOMEM; 1009 channel->conn = ksmbd_conn_get(conn); 1010 list_add_tail(&channel->list, head); 1011 return 0; 1012 } 1013 1014 /* 1015 * Snapshot connections before sending. References keep their transports alive, 1016 * while r_count is taken separately for each write, under request_lock. 1017 */ 1018 static int smb2_break_collect_channels(struct oplock_info *opinfo, 1019 struct ksmbd_inode *ci, 1020 struct smb2_break_channel *owner, 1021 struct list_head *head, 1022 struct smb2_oplock_break *rsp) 1023 { 1024 struct ksmbd_session *sess = NULL; 1025 struct ksmbd_conn *session_conns[KSMBD_MAX_CHANNELS]; 1026 struct ksmbd_file_table *ft; 1027 struct ksmbd_file *fp; 1028 struct ksmbd_conn *conn; 1029 struct lease_table *lb; 1030 char guid[SMB2_CLIENT_GUID_SIZE]; 1031 u64 session_id = 0; 1032 unsigned int nr_conns, i; 1033 bool have_guid = false; 1034 int bkt, ret = 0; 1035 1036 /* m_lock protects membership and binding, but not the session's lifetime. */ 1037 down_read(&ci->m_lock); 1038 if (!list_empty(&opinfo->op_entry)) { 1039 if (!opinfo->is_lease) { 1040 rsp->PersistentFid = READ_ONCE(opinfo->o_fp->persistent_id); 1041 rsp->VolatileFid = READ_ONCE(opinfo->o_fp->volatile_id); 1042 if (!has_file_id(rsp->PersistentFid) || 1043 !has_file_id(rsp->VolatileFid)) { 1044 /* A detached oplock must take the failed-break path. */ 1045 ret = opinfo->conn ? -ENOENT : -ENOTCONN; 1046 goto unlock; 1047 } 1048 session_id = opinfo->session_id; 1049 } 1050 conn = opinfo->conn; 1051 if (conn) { 1052 owner->conn = ksmbd_conn_get(conn); 1053 memcpy(guid, conn->ClientGUID, sizeof(guid)); 1054 have_guid = true; 1055 } 1056 } else { 1057 ret = -ENOENT; 1058 } 1059 unlock: 1060 up_read(&ci->m_lock); 1061 if (ret) 1062 return ret; 1063 1064 if (opinfo->is_lease && !have_guid) { 1065 read_lock(&lease_list_lock); 1066 lb = opinfo->o_lease->l_lb; 1067 if (lb) { 1068 memcpy(guid, lb->client_guid, sizeof(guid)); 1069 have_guid = true; 1070 } 1071 read_unlock(&lease_list_lock); 1072 } 1073 if (owner->conn) 1074 list_add_tail(&owner->list, head); 1075 1076 if (!opinfo->is_lease) { 1077 /* Session teardown takes m_lock while holding the session table lock. */ 1078 sess = ksmbd_session_lookup_slowpath(session_id); 1079 if (!sess) 1080 return -ENOENT; 1081 1082 /* Revalidate the binding and IDs after taking a safe session reference. */ 1083 down_read(&ci->m_lock); 1084 if (list_empty(&opinfo->op_entry) || opinfo->sess != sess) { 1085 ret = -ENOENT; 1086 } else { 1087 ft = &sess->file_table; 1088 read_lock(&ft->lock); 1089 fp = opinfo->o_fp; 1090 if (fp->f_state == FP_CLOSED || 1091 !has_file_id(fp->volatile_id) || 1092 idr_find(ft->idr, fp->volatile_id) != fp) { 1093 ret = -ENOENT; 1094 } else { 1095 rsp->PersistentFid = READ_ONCE(fp->persistent_id); 1096 rsp->VolatileFid = fp->volatile_id; 1097 } 1098 read_unlock(&ft->lock); 1099 } 1100 up_read(&ci->m_lock); 1101 if (ret) { 1102 ksmbd_user_session_put(sess); 1103 return ret; 1104 } 1105 } 1106 1107 if (opinfo->is_lease && have_guid) { 1108 down_read(&conn_list_lock); 1109 hash_for_each(conn_list, bkt, conn, hlist) { 1110 if (conn == owner->conn || !ksmbd_conn_good(conn) || 1111 memcmp(conn->ClientGUID, guid, sizeof(guid))) 1112 continue; 1113 ret = smb2_break_add_channel(head, conn); 1114 if (ret) 1115 break; 1116 } 1117 up_read(&conn_list_lock); 1118 } else if (!opinfo->is_lease && sess && sess->dialect >= SMB30_PROT_ID) { 1119 nr_conns = ksmbd_session_get_channels(sess, session_conns, 1120 ARRAY_SIZE(session_conns)); 1121 for (i = 0; i < nr_conns; i++) { 1122 conn = session_conns[i]; 1123 if (!ret && conn && conn != owner->conn && ksmbd_conn_good(conn)) 1124 ret = smb2_break_add_channel(head, conn); 1125 /* Drop all snapshot references, also on allocation failure. */ 1126 ksmbd_conn_put(conn); 1127 } 1128 } 1129 ksmbd_user_session_put(sess); 1130 1131 /* Allocation pressure may limit retries, but must not discard the owner. */ 1132 return list_empty(head) ? (ret ?: -ENOTCONN) : 0; 1133 } 1134 1135 static bool smb2_break_delivered(struct list_head *head, bool require_live) 1136 { 1137 struct smb2_break_channel *channel; 1138 struct ksmbd_conn *conn; 1139 1140 list_for_each_entry(channel, head, list) { 1141 conn = channel->conn; 1142 if (!channel->queued || (require_live && !ksmbd_conn_good(conn))) 1143 continue; 1144 /* A synchronous send needs no separate delivery probe. */ 1145 if (channel->delivered || !channel->tracking.valid || 1146 conn->transport->ops->is_acked(conn->transport, 1147 &channel->tracking)) { 1148 channel->delivered = true; 1149 return true; 1150 } 1151 } 1152 return false; 1153 } 1154 1155 static bool smb2_break_has_unsent_channels(struct list_head *head) 1156 { 1157 struct smb2_break_channel *channel; 1158 1159 list_for_each_entry(channel, head, list) { 1160 if (!channel->queued && !channel->failed) 1161 return true; 1162 } 1163 return false; 1164 } 1165 1166 static int smb2_break_wait(struct oplock_info *opinfo, struct list_head *head, 1167 struct smb2_break_channel *sent_channel, 1168 bool expect_ack, bool wait_ack, bool can_retry, 1169 unsigned long retry_at, unsigned long deadline) 1170 { 1171 unsigned long remaining; 1172 long ret; 1173 1174 for (;;) { 1175 if (smb2_break_complete(opinfo, expect_ack)) 1176 return 0; 1177 if (!wait_ack && smb2_break_delivered(head, false)) 1178 return 0; 1179 if (time_after_eq(jiffies, deadline)) 1180 return -ETIMEDOUT; 1181 /* TCP delivery followed by a slow SMB ACK is not a send failure. */ 1182 if (can_retry && !smb2_break_delivered(head, true) && 1183 ((sent_channel && !ksmbd_conn_good(sent_channel->conn)) || 1184 time_after_eq(jiffies, retry_at))) 1185 return -EAGAIN; 1186 1187 remaining = min_t(unsigned long, deadline - jiffies, 1188 max_t(unsigned long, 1, HZ / 10)); 1189 ret = wait_event_interruptible_timeout(opinfo->oplock_q, 1190 smb2_break_complete(opinfo, expect_ack), 1191 remaining); 1192 if (ret < 0) 1193 return ret; 1194 } 1195 } 1196 1197 static void smb2_break_init_header(struct smb2_hdr *hdr) 1198 { 1199 memset(hdr, 0, sizeof(*hdr)); 1200 hdr->ProtocolId = SMB2_PROTO_NUMBER; 1201 hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE; 1202 hdr->Command = SMB2_OPLOCK_BREAK; 1203 hdr->Flags = SMB2_FLAGS_SERVER_TO_REDIR; 1204 hdr->MessageId = cpu_to_le64(-1); 1205 } 1206 1207 /* Build one notification and reuse its payload and epoch on each connection. */ 1208 static int smb2_break_notify(struct oplock_info *opinfo, struct ksmbd_inode *ci, 1209 bool expect_ack, bool wait_ack, bool inc_epoch, 1210 struct work_struct *interim_work) 1211 { 1212 struct smb2_break_channel owner = {}, *channel, *tmp, *last = NULL; 1213 struct ksmbd_work *work; 1214 struct smb2_oplock_break *oplock_rsp; 1215 struct smb2_lease_break *lease_rsp; 1216 struct lease *lease = opinfo->o_lease; 1217 struct oplock_info *open; 1218 unsigned long deadline, retry_at; 1219 int ret, size; 1220 LIST_HEAD(channels); 1221 1222 if (smb2_break_complete(opinfo, expect_ack)) { 1223 ret = 0; 1224 goto out_interim; 1225 } 1226 wait_ack = wait_ack && expect_ack; 1227 work = ksmbd_alloc_work_struct(); 1228 if (!work) { 1229 ret = smb2_break_failed(opinfo, expect_ack); 1230 goto out_interim; 1231 } 1232 ret = allocate_interim_rsp_buf(work); 1233 if (ret) 1234 goto failed; 1235 oplock_rsp = smb_get_msg(work->response_buf); 1236 smb2_break_init_header(&oplock_rsp->hdr); 1237 1238 ret = smb2_break_collect_channels(opinfo, ci, &owner, &channels, oplock_rsp); 1239 if (ret == -ENOENT) 1240 goto out; 1241 if (ret) 1242 goto failed; 1243 1244 if (opinfo->is_lease) 1245 spin_lock(&lease->lock); 1246 spin_lock(&opinfo->state_lock); 1247 if (opinfo->op_state == OPLOCK_CLOSING || 1248 (expect_ack && opinfo->op_state == OPLOCK_STATE_NONE)) { 1249 spin_unlock(&opinfo->state_lock); 1250 if (opinfo->is_lease) 1251 spin_unlock(&lease->lock); 1252 ret = 0; 1253 goto out; 1254 } 1255 if (opinfo->is_lease) { 1256 lease_rsp = smb_get_msg(work->response_buf); 1257 lease_rsp->StructureSize = cpu_to_le16(44); 1258 if (lease->version == 2) { 1259 if (inc_epoch) 1260 lease->epoch++; 1261 lease_rsp->Epoch = cpu_to_le16(lease->epoch); 1262 } 1263 if (lease->state & (SMB2_LEASE_WRITE_CACHING_LE | 1264 SMB2_LEASE_HANDLE_CACHING_LE)) 1265 lease_rsp->Flags = SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED; 1266 memcpy(lease_rsp->LeaseKey, lease->lease_key, SMB2_LEASE_KEY_SIZE); 1267 lease_rsp->CurrentLeaseState = lease->state; 1268 lease_rsp->NewLeaseState = lease->new_state; 1269 size = sizeof(*lease_rsp); 1270 } else { 1271 oplock_rsp->StructureSize = cpu_to_le16(24); 1272 oplock_rsp->OplockLevel = !opinfo->open_trunc && 1273 (opinfo->level == SMB2_OPLOCK_LEVEL_BATCH || 1274 opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE) ? 1275 SMB2_OPLOCK_LEVEL_II : SMB2_OPLOCK_LEVEL_NONE; 1276 /* 1277 * A level II break needs no ACK. Publish NONE before sending so 1278 * an unsolicited ACK cannot observe II while delivery is pending. 1279 */ 1280 if (!expect_ack) 1281 opinfo->level = SMB2_OPLOCK_LEVEL_NONE; 1282 size = sizeof(*oplock_rsp); 1283 } 1284 spin_unlock(&opinfo->state_lock); 1285 if (opinfo->is_lease) 1286 spin_unlock(&lease->lock); 1287 ret = ksmbd_iov_pin_rsp(work, oplock_rsp, size); 1288 if (ret) 1289 goto failed; 1290 1291 deadline = jiffies + OPLOCK_WAIT_TIME; 1292 retry: 1293 list_for_each_entry(channel, &channels, list) { 1294 if (channel->queued || channel->failed) 1295 continue; 1296 if (smb2_break_complete(opinfo, expect_ack)) { 1297 ret = 0; 1298 goto out; 1299 } 1300 if (time_after_eq(jiffies, deadline)) { 1301 ret = -ETIMEDOUT; 1302 break; 1303 } 1304 if (!ksmbd_conn_r_count_inc_if_good(channel->conn)) { 1305 channel->failed = true; 1306 ret = -ENOTCONN; 1307 continue; 1308 } 1309 work->conn = channel->conn; 1310 ret = ksmbd_conn_write_tracked(work, &channel->tracking); 1311 ksmbd_conn_r_count_dec(channel->conn); 1312 if (ret) { 1313 /* Only a nonblocking send guarantees no bytes on -EAGAIN. */ 1314 if (ret != -EAGAIN || !channel->conn->transport->ops->is_acked) 1315 channel->failed = true; 1316 continue; 1317 } 1318 channel->queued = true; 1319 if (expect_ack) 1320 smb2_break_start_timer(opinfo, deadline); 1321 if (interim_work) { 1322 ksmbd_queue_io_work(interim_work); 1323 interim_work = NULL; 1324 } 1325 last = channel; 1326 retry_at = jiffies + channel->tracking.retry_after; 1327 ret = smb2_break_wait(opinfo, &channels, channel, expect_ack, 1328 wait_ack, smb2_break_has_unsent_channels(&channels), 1329 retry_at, deadline); 1330 if (ret != -EAGAIN) 1331 goto done; 1332 } 1333 if (smb2_break_has_unsent_channels(&channels)) { 1334 /* Allow busy writers to make progress before another nonblocking pass. */ 1335 retry_at = jiffies + max_t(unsigned long, 1, HZ / 10); 1336 ret = smb2_break_wait(opinfo, &channels, NULL, expect_ack, 1337 wait_ack, true, retry_at, deadline); 1338 if (ret == -EAGAIN) 1339 goto retry; 1340 } else if (last) { 1341 /* Give already queued notifications the rest of the shared wait. */ 1342 ret = smb2_break_wait(opinfo, &channels, last, expect_ack, 1343 wait_ack, false, deadline, deadline); 1344 } 1345 1346 done: 1347 if (ret) 1348 goto failed; 1349 if (!expect_ack && opinfo->is_lease) { 1350 spin_lock(&lease->lock); 1351 if (READ_ONCE(opinfo->op_state) != OPLOCK_CLOSING) { 1352 lease->state = lease->new_state; 1353 list_for_each_entry(open, &lease->open_list, lease_entry) { 1354 spin_lock(&open->state_lock); 1355 open->level = smb2_map_lease_to_oplock(lease->state); 1356 spin_unlock(&open->state_lock); 1357 } 1358 } 1359 spin_unlock(&lease->lock); 1360 } 1361 goto out; 1362 failed: 1363 ret = smb2_break_failed(opinfo, expect_ack); 1364 out: 1365 if (expect_ack && READ_ONCE(opinfo->oplock_timed_out)) 1366 ret = ksmbd_invalidate_durable_fd(opinfo->fid); 1367 ksmbd_free_work_struct(work); 1368 list_for_each_entry_safe(channel, tmp, &channels, list) { 1369 list_del(&channel->list); 1370 ksmbd_conn_put(channel->conn); 1371 if (channel != &owner) 1372 kfree(channel); 1373 } 1374 out_interim: 1375 if (interim_work) 1376 ksmbd_queue_io_work(interim_work); 1377 return ret; 1378 } 1379 1380 struct smb2_interim_work { 1381 struct work_struct work; 1382 struct ksmbd_work *in_work; 1383 }; 1384 1385 static void smb2_break_interim_work(struct work_struct *work) 1386 { 1387 struct smb2_interim_work *iw = container_of(work, struct smb2_interim_work, work); 1388 1389 if (!setup_async_work(iw->in_work, NULL, NULL)) { 1390 smb2_send_interim_resp(iw->in_work, STATUS_PENDING); 1391 release_async_work(iw->in_work); 1392 } 1393 } 1394 1395 struct smb2_break_work { 1396 struct work_struct work; 1397 struct oplock_info *opinfo; 1398 struct ksmbd_inode *ci; 1399 bool expect_ack; 1400 bool wait_ack; 1401 bool inc_epoch; 1402 int ret; 1403 }; 1404 1405 static void smb2_break_notify_work(struct work_struct *work) 1406 { 1407 struct smb2_break_work *bw = container_of(work, struct smb2_break_work, work); 1408 1409 bw->ret = smb2_break_notify(bw->opinfo, bw->ci, bw->expect_ack, 1410 bw->wait_ack, bw->inc_epoch, NULL); 1411 } 1412 1413 static int smb2_break_notify_with_interim(struct oplock_info *opinfo, 1414 struct ksmbd_inode *ci, 1415 bool expect_ack, bool wait_ack, 1416 bool inc_epoch, struct ksmbd_work *in_work, 1417 bool notify_first) 1418 { 1419 struct smb2_break_work bw = { 1420 .opinfo = opinfo, 1421 .ci = ci, 1422 .expect_ack = expect_ack, 1423 .wait_ack = wait_ack, 1424 .inc_epoch = inc_epoch, 1425 }; 1426 1427 if (!in_work) 1428 return smb2_break_notify(opinfo, ci, expect_ack, wait_ack, inc_epoch, NULL); 1429 1430 if (notify_first) { 1431 struct smb2_interim_work iw = { .in_work = in_work }; 1432 int ret; 1433 1434 /* 1435 * Rename sends the first notification in the request context. 1436 * Queue the interim writer after that send so a blocked response 1437 * cannot stop notification retries or the break ACK wait. 1438 */ 1439 INIT_WORK_ONSTACK(&iw.work, smb2_break_interim_work); 1440 ret = smb2_break_notify(opinfo, ci, expect_ack, wait_ack, 1441 inc_epoch, &iw.work); 1442 flush_work(&iw.work); 1443 destroy_work_on_stack(&iw.work); 1444 return ret; 1445 } 1446 1447 /* A blocked interim writer must not stop notification sends or retries. */ 1448 INIT_WORK_ONSTACK(&bw.work, smb2_break_notify_work); 1449 ksmbd_queue_break_work(&bw.work); 1450 if (!setup_async_work(in_work, NULL, NULL)) { 1451 smb2_send_interim_resp(in_work, STATUS_PENDING); 1452 release_async_work(in_work); 1453 } 1454 /* The caller keeps opinfo and ci alive until the notifier has finished. */ 1455 flush_work(&bw.work); 1456 destroy_work_on_stack(&bw.work); 1457 return bw.ret; 1458 } 1459 1460 static void wait_lease_breaking(struct oplock_info *opinfo) 1461 { 1462 if (!opinfo->is_lease) 1463 return; 1464 1465 wake_up_interruptible_all(&opinfo->oplock_brk); 1466 if (atomic_read(&opinfo->breaking_cnt)) { 1467 int ret = 0; 1468 1469 ret = wait_event_interruptible_timeout(opinfo->oplock_brk, 1470 atomic_read(&opinfo->breaking_cnt) == 0, 1471 HZ); 1472 if (!ret) 1473 atomic_set(&opinfo->breaking_cnt, 0); 1474 } 1475 } 1476 1477 static int oplock_break(struct oplock_info *brk_opinfo, struct ksmbd_inode *ci, 1478 int req_op_level, struct ksmbd_work *in_work, 1479 bool share_break, bool notify_first) 1480 { 1481 int err = 0; 1482 bool sent_interim = false; 1483 bool expect_ack = false; 1484 1485 /* Need to break exclusive/batch oplock, write lease or overwrite_if */ 1486 ksmbd_debug(OPLOCK, 1487 "request to send oplock(level : 0x%x) break notification\n", 1488 brk_opinfo->level); 1489 1490 if (brk_opinfo->is_lease) { 1491 struct lease *lease = brk_opinfo->o_lease; 1492 bool open_trunc = brk_opinfo->open_trunc; 1493 bool was_pending = test_bit(0, &brk_opinfo->pending_break); 1494 bool wait_ack; 1495 bool inc_epoch = true; 1496 1497 if (in_work && was_pending) { 1498 setup_async_work(in_work, NULL, NULL); 1499 smb2_send_interim_resp(in_work, STATUS_PENDING); 1500 release_async_work(in_work); 1501 sent_interim = true; 1502 } 1503 1504 err = oplock_break_pending(brk_opinfo, req_op_level); 1505 if (err) 1506 return err < 0 ? err : 0; 1507 if (was_pending) 1508 open_trunc = brk_opinfo->open_trunc; 1509 1510 again: 1511 spin_lock(&lease->lock); 1512 atomic_inc(&brk_opinfo->breaking_cnt); 1513 if (open_trunc) { 1514 /* 1515 * Create overwrite break trigger the lease break to 1516 * none. 1517 */ 1518 lease->new_state = SMB2_LEASE_NONE_LE; 1519 } else if (share_break && 1520 lease->state & SMB2_LEASE_HANDLE_CACHING_LE) { 1521 lease->new_state = 1522 lease->state & ~SMB2_LEASE_HANDLE_CACHING_LE; 1523 } else { 1524 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE) { 1525 if (lease->state & SMB2_LEASE_HANDLE_CACHING_LE) 1526 lease->new_state = 1527 SMB2_LEASE_READ_CACHING_LE | 1528 SMB2_LEASE_HANDLE_CACHING_LE; 1529 else 1530 lease->new_state = 1531 SMB2_LEASE_READ_CACHING_LE; 1532 } else { 1533 if (lease->state & SMB2_LEASE_HANDLE_CACHING_LE && 1534 !lease->is_dir) 1535 lease->new_state = 1536 SMB2_LEASE_READ_CACHING_LE; 1537 else 1538 lease->new_state = SMB2_LEASE_NONE_LE; 1539 } 1540 } 1541 1542 expect_ack = lease->state & (SMB2_LEASE_WRITE_CACHING_LE | 1543 SMB2_LEASE_HANDLE_CACHING_LE); 1544 if (expect_ack) { 1545 if (!oplock_break_set_ack_wait(brk_opinfo)) { 1546 spin_unlock(&lease->lock); 1547 atomic_dec_if_positive(&brk_opinfo->breaking_cnt); 1548 clear_and_wake_up_bit(0, &brk_opinfo->pending_break); 1549 return -ENOENT; 1550 } 1551 } else 1552 atomic_dec(&brk_opinfo->breaking_cnt); 1553 1554 wait_ack = !(open_trunc && 1555 lease->state == (SMB2_LEASE_READ_CACHING_LE | 1556 SMB2_LEASE_HANDLE_CACHING_LE)); 1557 if (lease->reuse_epoch) { 1558 inc_epoch = false; 1559 lease->reuse_epoch = false; 1560 } 1561 spin_unlock(&lease->lock); 1562 err = smb2_break_notify_with_interim(brk_opinfo, ci, expect_ack, 1563 wait_ack, inc_epoch, 1564 sent_interim ? NULL : in_work, 1565 notify_first); 1566 sent_interim = true; 1567 inc_epoch = false; 1568 1569 ksmbd_debug(OPLOCK, "oplock granted = %d\n", brk_opinfo->level); 1570 if (brk_opinfo->op_state == OPLOCK_CLOSING) 1571 err = -ENOENT; 1572 1573 if (wait_ack) 1574 wait_lease_breaking(brk_opinfo); 1575 /* 1576 * A share-mode conflict break only drops the conflicting 1577 * caching bit; the triggering open fails with a sharing 1578 * violation, so keep it to a single break. 1579 * 1580 * Otherwise chain another break while the lease is still 1581 * incompatible with this open (req_op_level), or while a 1582 * truncating waiter that arrived during the break still needs 1583 * the lease dropped to none. open_trunc snapshotted for this 1584 * break stays cleared, so the next state is computed from the 1585 * lease state and the cascade steps down (e.g. RH->R->none) 1586 * instead of collapsing straight to none. 1587 */ 1588 if (wait_ack && !err && !share_break && 1589 (lease_break_needed(brk_opinfo, req_op_level, open_trunc) || 1590 (brk_opinfo->open_trunc && 1591 lease->state != SMB2_LEASE_NONE_LE))) 1592 goto again; 1593 1594 clear_and_wake_up_bit(0, &brk_opinfo->pending_break); 1595 return err; 1596 } else { 1597 err = oplock_break_pending(brk_opinfo, req_op_level); 1598 if (err) 1599 return err < 0 ? err : 0; 1600 1601 if (brk_opinfo->level == SMB2_OPLOCK_LEVEL_BATCH || 1602 brk_opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE) { 1603 expect_ack = true; 1604 if (!oplock_break_set_ack_wait(brk_opinfo)) { 1605 clear_and_wake_up_bit(0, &brk_opinfo->pending_break); 1606 return -ENOENT; 1607 } 1608 } 1609 } 1610 1611 err = smb2_break_notify_with_interim(brk_opinfo, ci, expect_ack, 1612 true, false, in_work, false); 1613 1614 ksmbd_debug(OPLOCK, "oplock granted = %d\n", brk_opinfo->level); 1615 if (brk_opinfo->op_state == OPLOCK_CLOSING) 1616 err = -EAGAIN; 1617 clear_and_wake_up_bit(0, &brk_opinfo->pending_break); 1618 1619 return err; 1620 } 1621 1622 struct oplock_break_entry { 1623 struct list_head list; 1624 struct oplock_info *opinfo; 1625 }; 1626 1627 static int oplock_break_add(struct list_head *head, struct oplock_info *opinfo) 1628 { 1629 struct oplock_break_entry *ent; 1630 1631 ent = kmalloc_obj(struct oplock_break_entry, KSMBD_DEFAULT_GFP); 1632 if (!ent) 1633 return -ENOMEM; 1634 1635 ent->opinfo = opinfo; 1636 list_add_tail(&ent->list, head); 1637 return 0; 1638 } 1639 1640 static void oplock_break_drain_none(struct list_head *head, 1641 struct ksmbd_inode *ci) 1642 { 1643 struct oplock_break_entry *ent, *tmp; 1644 1645 list_for_each_entry_safe(ent, tmp, head, list) { 1646 oplock_break(ent->opinfo, ci, SMB2_OPLOCK_LEVEL_NONE, NULL, 1647 false, false); 1648 list_del(&ent->list); 1649 opinfo_put(ent->opinfo); 1650 kfree(ent); 1651 } 1652 } 1653 1654 void destroy_lease_table(void) 1655 { 1656 struct lease_table *lb; 1657 struct lease *lease; 1658 1659 cancel_delayed_work_sync(&lease_break_timer_work); 1660 mutex_lock(&lease_break_timer_lock); 1661 lease_break_timer_pending = false; 1662 mutex_unlock(&lease_break_timer_lock); 1663 1664 write_lock(&lease_list_lock); 1665 while (!list_empty(&lease_table_list)) { 1666 lb = list_first_entry(&lease_table_list, struct lease_table, 1667 l_entry); 1668 lease = list_first_entry(&lb->lease_list, struct lease, l_entry); 1669 spin_lock(&lease->lock); 1670 if (lease->breaking) 1671 lease_break_to_none_locked(lease); 1672 spin_unlock(&lease->lock); 1673 lease_del_table(lease); 1674 } 1675 write_unlock(&lease_list_lock); 1676 } 1677 1678 int find_same_lease_key(struct ksmbd_conn *conn, struct ksmbd_inode *ci, 1679 struct lease_ctx_info *lctx) 1680 { 1681 struct lease *lease; 1682 int err = 0; 1683 struct lease_table *lb; 1684 1685 if (!lctx) 1686 return err; 1687 1688 read_lock(&lease_list_lock); 1689 if (list_empty(&lease_table_list)) { 1690 read_unlock(&lease_list_lock); 1691 return 0; 1692 } 1693 1694 list_for_each_entry(lb, &lease_table_list, l_entry) { 1695 if (!memcmp(lb->client_guid, conn->ClientGUID, 1696 SMB2_CLIENT_GUID_SIZE)) 1697 goto found; 1698 } 1699 read_unlock(&lease_list_lock); 1700 1701 return 0; 1702 1703 found: 1704 list_for_each_entry(lease, &lb->lease_list, l_entry) { 1705 if (lease->ci == ci) 1706 continue; 1707 if (!memcmp(lease->lease_key, lctx->lease_key, 1708 SMB2_LEASE_KEY_SIZE)) { 1709 err = -EINVAL; 1710 ksmbd_debug(OPLOCK, 1711 "found same lease key is already used in other files\n"); 1712 goto out; 1713 } 1714 } 1715 1716 out: 1717 read_unlock(&lease_list_lock); 1718 return err; 1719 } 1720 1721 static void add_lease_global_list(struct lease *lease, struct ksmbd_conn *conn, 1722 struct lease_table *new_lb) 1723 { 1724 struct lease_table *lb; 1725 1726 write_lock(&lease_list_lock); 1727 list_for_each_entry(lb, &lease_table_list, l_entry) { 1728 if (!memcmp(lb->client_guid, conn->ClientGUID, 1729 SMB2_CLIENT_GUID_SIZE)) { 1730 lease_add_table(lease, lb); 1731 write_unlock(&lease_list_lock); 1732 free_lease_table(new_lb); 1733 return; 1734 } 1735 } 1736 1737 lease_add_table(lease, new_lb); 1738 list_add(&new_lb->l_entry, &lease_table_list); 1739 write_unlock(&lease_list_lock); 1740 } 1741 1742 static void set_oplock_level(struct oplock_info *opinfo, int level, 1743 struct lease_ctx_info *lctx) 1744 { 1745 switch (level) { 1746 case SMB2_OPLOCK_LEVEL_BATCH: 1747 case SMB2_OPLOCK_LEVEL_EXCLUSIVE: 1748 grant_write_oplock(opinfo, level, lctx); 1749 break; 1750 case SMB2_OPLOCK_LEVEL_II: 1751 grant_read_oplock(opinfo, lctx); 1752 break; 1753 default: 1754 grant_none_oplock(opinfo, lctx); 1755 break; 1756 } 1757 } 1758 1759 void smb_send_parent_lease_break_noti(struct ksmbd_file *fp, 1760 struct lease_ctx_info *lctx) 1761 { 1762 struct oplock_info *opinfo; 1763 struct ksmbd_inode *p_ci = NULL; 1764 LIST_HEAD(brk_list); 1765 1766 if (lctx && lctx->version != 2) 1767 return; 1768 1769 p_ci = ksmbd_inode_lookup_lock(fp->filp->f_path.dentry->d_parent); 1770 if (!p_ci) 1771 return; 1772 1773 down_read(&p_ci->m_lock); 1774 list_for_each_entry(opinfo, &p_ci->m_op_list, op_entry) { 1775 if (!opinfo->is_lease) 1776 continue; 1777 1778 if (opinfo->o_lease->state != SMB2_OPLOCK_LEVEL_NONE && 1779 (!lctx || 1780 (!(lctx->flags & SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET_LE) || 1781 !compare_guid_key(opinfo, fp->conn->ClientGUID, 1782 lctx->parent_lease_key)))) { 1783 if (!atomic_inc_not_zero(&opinfo->refcount)) 1784 continue; 1785 1786 if (oplock_break_add(&brk_list, opinfo)) 1787 opinfo_put(opinfo); 1788 } 1789 } 1790 up_read(&p_ci->m_lock); 1791 1792 oplock_break_drain_none(&brk_list, p_ci); 1793 1794 ksmbd_inode_put(p_ci); 1795 } 1796 1797 void smb_lazy_parent_lease_break_close(struct ksmbd_file *fp) 1798 { 1799 struct oplock_info *opinfo; 1800 struct ksmbd_inode *p_ci = NULL; 1801 LIST_HEAD(brk_list); 1802 1803 rcu_read_lock(); 1804 opinfo = rcu_dereference(fp->f_opinfo); 1805 1806 if (!opinfo || !opinfo->is_lease || opinfo->o_lease->version != 2) { 1807 rcu_read_unlock(); 1808 return; 1809 } 1810 rcu_read_unlock(); 1811 1812 p_ci = ksmbd_inode_lookup_lock(fp->filp->f_path.dentry->d_parent); 1813 if (!p_ci) 1814 return; 1815 1816 down_read(&p_ci->m_lock); 1817 list_for_each_entry(opinfo, &p_ci->m_op_list, op_entry) { 1818 if (!opinfo->is_lease) 1819 continue; 1820 1821 if (opinfo->o_lease->state != SMB2_OPLOCK_LEVEL_NONE) { 1822 if (!atomic_inc_not_zero(&opinfo->refcount)) 1823 continue; 1824 1825 if (oplock_break_add(&brk_list, opinfo)) 1826 opinfo_put(opinfo); 1827 } 1828 } 1829 up_read(&p_ci->m_lock); 1830 1831 oplock_break_drain_none(&brk_list, p_ci); 1832 1833 ksmbd_inode_put(p_ci); 1834 } 1835 1836 /** 1837 * smb_grant_oplock() - handle oplock/lease request on file open 1838 * @work: smb work 1839 * @req_op_level: oplock level 1840 * @pid: id of open file 1841 * @fp: ksmbd file pointer 1842 * @tid: Tree id of connection 1843 * @lctx: lease context information on file open 1844 * @share_ret: share mode 1845 * @replay: whether this is a replayed CREATE request 1846 * 1847 * Return: 0 on success, otherwise error 1848 */ 1849 int smb_grant_oplock(struct ksmbd_work *work, int req_op_level, u64 pid, 1850 struct ksmbd_file *fp, __u16 tid, 1851 struct lease_ctx_info *lctx, int share_ret, bool replay) 1852 { 1853 int err = 0; 1854 int break_level = SMB2_OPLOCK_LEVEL_II; 1855 struct oplock_info *opinfo = NULL, *prev_opinfo = NULL; 1856 struct ksmbd_inode *ci = fp->f_ci; 1857 struct lease_table *new_lb = NULL; 1858 struct oplock_snapshot prev_op_snapshot; 1859 bool prev_op_has_lease; 1860 bool prev_durable_open = false; 1861 bool prev_durable_detached = false; 1862 unsigned long long prev_fid = KSMBD_NO_FID; 1863 bool new_lease = false; 1864 bool break_needed; 1865 __le32 prev_op_state = 0; 1866 1867 /* Only v2 leases handle the directory */ 1868 if (S_ISDIR(file_inode(fp->filp)->i_mode)) { 1869 if (!lctx || lctx->version != 2) 1870 return 0; 1871 } 1872 1873 opinfo = alloc_opinfo(work, pid, tid); 1874 if (!opinfo) 1875 return -ENOMEM; 1876 1877 if (lctx) { 1878 opinfo->o_lease = alloc_lease(lctx, ci); 1879 if (!opinfo->o_lease) { 1880 err = -ENOMEM; 1881 goto err_out; 1882 } 1883 opinfo->is_lease = 1; 1884 new_lease = true; 1885 } 1886 1887 /* ci does not have any oplock */ 1888 if (!opinfo_count(fp)) 1889 goto set_lev; 1890 1891 /* 1892 * A stat open that only requests metadata access must not break the 1893 * existing caching state. READ_CONTROL (reading the security 1894 * descriptor) does not conflict with a lease, but it does conflict 1895 * with an oplock, so only treat a read-control-only open as a stat 1896 * open when the existing holder is a lease. 1897 */ 1898 if (fp->cdoption != FILE_OVERWRITE_IF_LE && 1899 fp->cdoption != FILE_OVERWRITE_LE && 1900 fp->cdoption != FILE_SUPERSEDE_LE && 1901 (fp->attrib_only || 1902 (!(fp->daccess & ~(FILE_READ_ATTRIBUTES_LE | 1903 FILE_WRITE_ATTRIBUTES_LE | 1904 FILE_SYNCHRONIZE_LE | 1905 FILE_READ_CONTROL_LE)) && 1906 ksmbd_inode_has_lease(ci)))) { 1907 req_op_level = SMB2_OPLOCK_LEVEL_NONE; 1908 goto set_lev; 1909 } 1910 1911 if (lctx) { 1912 struct oplock_info *m_opinfo; 1913 1914 /* is lease already granted ? */ 1915 m_opinfo = same_client_has_lease(ci, work->conn->ClientGUID, 1916 lctx); 1917 if (m_opinfo) { 1918 lease_put(opinfo->o_lease); 1919 lease_get(m_opinfo->o_lease); 1920 opinfo->o_lease = m_opinfo->o_lease; 1921 opinfo->level = m_opinfo->level; 1922 new_lease = false; 1923 opinfo_put(m_opinfo); 1924 goto out; 1925 } 1926 } 1927 prev_opinfo = opinfo_get_list(ci, fp, &prev_op_snapshot); 1928 if (!prev_opinfo || 1929 (prev_opinfo->level == SMB2_OPLOCK_LEVEL_NONE && lctx)) { 1930 opinfo_put(prev_opinfo); 1931 goto set_lev; 1932 } 1933 prev_op_has_lease = prev_opinfo->is_lease; 1934 if (prev_op_has_lease) 1935 prev_op_state = prev_opinfo->o_lease->state; 1936 /* 1937 * A replay received while this open is waiting for an oplock or lease 1938 * break must not observe an intermediate level and proceed as a new 1939 * open. This check has to precede break_needed. an oplock may already 1940 * have been downgraded from Batch to II while its acknowledgement is 1941 * still pending. 1942 */ 1943 if (replay && 1944 (test_bit(0, &prev_opinfo->pending_break) || 1945 prev_opinfo->op_state == OPLOCK_ACK_WAIT)) { 1946 err = -EINPROGRESS; 1947 opinfo_put(prev_opinfo); 1948 goto err_out; 1949 } 1950 1951 if (share_ret < 0 && 1952 prev_opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE) { 1953 err = share_ret; 1954 opinfo_put(prev_opinfo); 1955 goto err_out; 1956 } 1957 1958 break_needed = prev_opinfo->level == SMB2_OPLOCK_LEVEL_BATCH || 1959 prev_opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE || 1960 (share_ret < 0 && prev_op_has_lease && 1961 (prev_op_state & SMB2_LEASE_HANDLE_CACHING_LE)); 1962 if (!break_needed) { 1963 opinfo_put(prev_opinfo); 1964 goto op_break_not_needed; 1965 } 1966 1967 prev_durable_open = prev_op_snapshot.durable_open; 1968 prev_durable_detached = prev_op_snapshot.durable_detached; 1969 prev_fid = prev_op_snapshot.fid; 1970 1971 err = oplock_break(prev_opinfo, ci, break_level, work, 1972 share_ret < 0 && prev_opinfo->is_lease, false); 1973 /* Keep a detached durable lease if its break retains handle caching. */ 1974 if ((prev_durable_detached && 1975 (!prev_op_has_lease || 1976 !(prev_opinfo->o_lease->state & SMB2_LEASE_HANDLE_CACHING_LE))) || 1977 (prev_durable_open && err == -ENOENT)) 1978 ksmbd_invalidate_durable_fd(prev_fid); 1979 opinfo_put(prev_opinfo); 1980 if (err == -EAGAIN) { 1981 share_ret = ksmbd_smb_check_shared_mode(fp->filp, fp); 1982 if (share_ret < 0) { 1983 err = share_ret; 1984 goto err_out; 1985 } 1986 goto set_lev; 1987 } 1988 if (err == -ENOENT) { 1989 /* 1990 * A pending durable CREATE can lose the previous oplock when 1991 * its holder closes the file. In that case grant the original 1992 * request its full caching state. Other opens still need the 1993 * normal shared-open downgrade below. 1994 */ 1995 if (!prev_durable_open && 1996 req_op_level != SMB2_OPLOCK_LEVEL_NONE) 1997 req_op_level = SMB2_OPLOCK_LEVEL_II; 1998 goto set_lev; 1999 } 2000 /* Check all oplock was freed by close */ 2001 else if (err < 0) 2002 goto err_out; 2003 2004 op_break_not_needed: 2005 if (share_ret < 0) { 2006 err = share_ret; 2007 goto err_out; 2008 } 2009 2010 if (req_op_level != SMB2_OPLOCK_LEVEL_NONE) 2011 req_op_level = SMB2_OPLOCK_LEVEL_II; 2012 2013 /* grant fixed oplock on stacked locking between lease and oplock */ 2014 if (prev_op_has_lease && !lctx) 2015 if (prev_op_state & SMB2_LEASE_HANDLE_CACHING_LE) 2016 req_op_level = SMB2_OPLOCK_LEVEL_NONE; 2017 2018 if (!prev_op_has_lease && lctx) { 2019 req_op_level = SMB2_OPLOCK_LEVEL_II; 2020 lctx->req_state = SMB2_LEASE_READ_CACHING_LE; 2021 } 2022 2023 set_lev: 2024 set_oplock_level(opinfo, req_op_level, lctx); 2025 2026 out: 2027 /* 2028 * Keep the original publication order so concurrent opens can 2029 * still observe the in-flight grant via ci->m_op_list, but make 2030 * everything after opinfo_add() no-fail by preallocating any new 2031 * lease_table first. 2032 */ 2033 opinfo->o_fp = fp; 2034 if (new_lease) { 2035 new_lb = alloc_lease_table(opinfo); 2036 if (!new_lb) { 2037 err = -ENOMEM; 2038 goto err_out; 2039 } 2040 } 2041 2042 opinfo_count_inc(fp); 2043 opinfo_add(opinfo, fp); 2044 2045 if (new_lease) 2046 add_lease_global_list(opinfo->o_lease, opinfo->conn, new_lb); 2047 if (opinfo->is_lease) 2048 lease_add_open(opinfo->o_lease, opinfo); 2049 2050 rcu_assign_pointer(fp->f_opinfo, opinfo); 2051 2052 return 0; 2053 err_out: 2054 kfree(new_lb); 2055 opinfo_put(opinfo); 2056 return err; 2057 } 2058 2059 /** 2060 * smb_break_all_write_oplock() - break batch/exclusive oplock to level2 2061 * @work: smb work 2062 * @fp: ksmbd file pointer 2063 * @is_trunc: truncate on open 2064 */ 2065 static bool smb_break_all_write_oplock(struct ksmbd_work *work, 2066 struct ksmbd_file *fp, int is_trunc) 2067 { 2068 struct oplock_info *brk_opinfo; 2069 bool sent_break = false; 2070 2071 brk_opinfo = opinfo_get_list(fp->f_ci, NULL, NULL); 2072 if (!brk_opinfo) 2073 return false; 2074 if (brk_opinfo->level != SMB2_OPLOCK_LEVEL_BATCH && 2075 brk_opinfo->level != SMB2_OPLOCK_LEVEL_EXCLUSIVE) { 2076 opinfo_put(brk_opinfo); 2077 return false; 2078 } 2079 2080 brk_opinfo->open_trunc = is_trunc; 2081 oplock_break(brk_opinfo, fp->f_ci, SMB2_OPLOCK_LEVEL_II, work, false, false); 2082 sent_break = true; 2083 opinfo_put(brk_opinfo); 2084 2085 return sent_break; 2086 } 2087 2088 /** 2089 * __smb_break_all_levII_oplock() - send level2 oplock or read lease break command 2090 * from server to client 2091 * @work: smb work 2092 * @fp: ksmbd file pointer 2093 * @is_trunc: truncate on open 2094 * @send_interim: send interim response to the client 2095 * @send_oplock_break: send oplock break notification to the client 2096 * @notify_first: send the lease break notification before the interim response 2097 */ 2098 static void __smb_break_all_levII_oplock(struct ksmbd_work *work, 2099 struct ksmbd_file *fp, int is_trunc, 2100 bool send_interim, bool send_oplock_break, 2101 bool notify_first) 2102 { 2103 struct oplock_info *op, *brk_op; 2104 struct oplock_break_entry *ent, *tmp; 2105 struct ksmbd_inode *ci; 2106 struct ksmbd_conn *conn = work->conn; 2107 bool sent_interim = false; 2108 LIST_HEAD(brk_list); 2109 2110 if (!test_share_config_flag(work->tcon->share_conf, 2111 KSMBD_SHARE_FLAG_OPLOCKS)) 2112 return; 2113 2114 ci = fp->f_ci; 2115 op = opinfo_get(fp); 2116 2117 down_read(&ci->m_lock); 2118 list_for_each_entry(brk_op, &ci->m_op_list, op_entry) { 2119 if (!atomic_inc_not_zero(&brk_op->refcount)) 2120 continue; 2121 2122 if (!brk_op->is_lease && 2123 brk_op->level != SMB2_OPLOCK_LEVEL_II) { 2124 ksmbd_debug(OPLOCK, "unexpected oplock(0x%x)\n", 2125 brk_op->level); 2126 goto next; 2127 } 2128 2129 /* Skip oplock being break to none */ 2130 if (brk_op->is_lease && 2131 brk_op->o_lease->new_state == SMB2_LEASE_NONE_LE && 2132 atomic_read(&brk_op->breaking_cnt)) 2133 goto next; 2134 2135 if (op && op->is_lease && brk_op->is_lease && 2136 compare_guid_key(brk_op, conn->ClientGUID, 2137 op->o_lease->lease_key)) 2138 goto next; 2139 brk_op->open_trunc = is_trunc; 2140 2141 /* 2142 * Defer the break until ci->m_lock is released: oplock_break() 2143 * may block waiting for the lease break acknowledgment, and the 2144 * close that wakes that wait needs ci->m_lock for write. 2145 */ 2146 if (!oplock_break_add(&brk_list, brk_op)) 2147 continue; 2148 next: 2149 opinfo_put(brk_op); 2150 } 2151 up_read(&ci->m_lock); 2152 2153 list_for_each_entry_safe(ent, tmp, &brk_list, list) { 2154 brk_op = ent->opinfo; 2155 2156 if (!brk_op->is_lease && !send_oplock_break) { 2157 brk_op->level = SMB2_OPLOCK_LEVEL_NONE; 2158 spin_lock(&brk_op->state_lock); 2159 if (brk_op->op_state != OPLOCK_CLOSING) 2160 brk_op->op_state = OPLOCK_STATE_NONE; 2161 spin_unlock(&brk_op->state_lock); 2162 } else { 2163 oplock_break(brk_op, ci, 2164 brk_op->is_lease && !is_trunc ? 2165 SMB2_OPLOCK_LEVEL_II : SMB2_OPLOCK_LEVEL_NONE, 2166 send_interim && !sent_interim ? work : NULL, 2167 false, notify_first); 2168 } 2169 sent_interim = true; 2170 list_del(&ent->list); 2171 opinfo_put(brk_op); 2172 kfree(ent); 2173 } 2174 2175 if (op) 2176 opinfo_put(op); 2177 } 2178 2179 void smb_break_all_levII_oplock(struct ksmbd_work *work, struct ksmbd_file *fp, 2180 int is_trunc) 2181 { 2182 __smb_break_all_levII_oplock(work, fp, is_trunc, true, true, false); 2183 } 2184 2185 void smb_break_all_levII_oplock_rename(struct ksmbd_work *work, struct ksmbd_file *fp) 2186 { 2187 __smb_break_all_levII_oplock(work, fp, 0, true, true, true); 2188 } 2189 2190 void smb_break_all_levII_oplock_no_interim(struct ksmbd_work *work, 2191 struct ksmbd_file *fp, int is_trunc) 2192 { 2193 __smb_break_all_levII_oplock(work, fp, is_trunc, false, true, false); 2194 } 2195 2196 void smb_break_all_levII_oplock_for_delete(struct ksmbd_work *work, 2197 struct ksmbd_file *fp) 2198 { 2199 __smb_break_all_levII_oplock(work, fp, 0, false, false, false); 2200 } 2201 2202 /** 2203 * smb_break_all_oplock() - break both batch/exclusive and level2 oplock 2204 * @work: smb work 2205 * @fp: ksmbd file pointer 2206 */ 2207 void smb_break_all_oplock(struct ksmbd_work *work, struct ksmbd_file *fp) 2208 { 2209 bool sent_break; 2210 2211 if (!test_share_config_flag(work->tcon->share_conf, 2212 KSMBD_SHARE_FLAG_OPLOCKS)) 2213 return; 2214 2215 sent_break = smb_break_all_write_oplock(work, fp, 1); 2216 __smb_break_all_levII_oplock(work, fp, 1, !sent_break, true, false); 2217 } 2218 2219 /** 2220 * smb2_map_lease_to_oplock() - map lease state to corresponding oplock type 2221 * @lease_state: lease type 2222 * 2223 * Return: 0 if no mapping, otherwise corresponding oplock type 2224 */ 2225 __u8 smb2_map_lease_to_oplock(__le32 lease_state) 2226 { 2227 if ((lease_state & SMB2_LEASE_WRITE_CACHING_LE) && 2228 (lease_state & SMB2_LEASE_HANDLE_CACHING_LE)) { 2229 return SMB2_OPLOCK_LEVEL_BATCH; 2230 } else if (lease_state & SMB2_LEASE_WRITE_CACHING_LE) { 2231 return SMB2_OPLOCK_LEVEL_EXCLUSIVE; 2232 } else if (lease_state & (SMB2_LEASE_READ_CACHING_LE | 2233 SMB2_LEASE_HANDLE_CACHING_LE)) { 2234 return SMB2_OPLOCK_LEVEL_II; 2235 } 2236 return 0; 2237 } 2238 2239 /** 2240 * create_lease_buf() - create lease context for open cmd response 2241 * @rbuf: buffer to create lease context response 2242 * @lease: buffer to stored parsed lease state information 2243 */ 2244 void create_lease_buf(u8 *rbuf, struct lease *lease) 2245 { 2246 if (lease->version == 2) { 2247 struct create_lease_v2 *buf = (struct create_lease_v2 *)rbuf; 2248 __le32 flags = 0; 2249 2250 memset(buf, 0, sizeof(struct create_lease_v2)); 2251 memcpy(buf->lcontext.LeaseKey, lease->lease_key, 2252 SMB2_LEASE_KEY_SIZE); 2253 if (lease_has_parent_key(lease)) 2254 flags |= SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET_LE; 2255 if (lease_break_in_progress(lease)) 2256 flags |= SMB2_LEASE_FLAG_BREAK_IN_PROGRESS_LE; 2257 buf->lcontext.LeaseFlags = flags; 2258 buf->lcontext.Epoch = cpu_to_le16(lease->epoch); 2259 buf->lcontext.LeaseState = lease->state; 2260 if (lease_has_parent_key(lease)) 2261 memcpy(buf->lcontext.ParentLeaseKey, lease->parent_lease_key, 2262 SMB2_LEASE_KEY_SIZE); 2263 buf->ccontext.DataOffset = cpu_to_le16(offsetof 2264 (struct create_lease_v2, lcontext)); 2265 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2)); 2266 buf->ccontext.NameOffset = cpu_to_le16(offsetof 2267 (struct create_lease_v2, Name)); 2268 buf->ccontext.NameLength = cpu_to_le16(4); 2269 buf->Name[0] = 'R'; 2270 buf->Name[1] = 'q'; 2271 buf->Name[2] = 'L'; 2272 buf->Name[3] = 's'; 2273 } else { 2274 struct create_lease *buf = (struct create_lease *)rbuf; 2275 2276 memset(buf, 0, sizeof(struct create_lease)); 2277 memcpy(buf->lcontext.LeaseKey, lease->lease_key, SMB2_LEASE_KEY_SIZE); 2278 if (lease_break_in_progress(lease)) 2279 buf->lcontext.LeaseFlags = 2280 SMB2_LEASE_FLAG_BREAK_IN_PROGRESS_LE; 2281 buf->lcontext.LeaseState = lease->state; 2282 buf->ccontext.DataOffset = cpu_to_le16(offsetof 2283 (struct create_lease, lcontext)); 2284 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context)); 2285 buf->ccontext.NameOffset = cpu_to_le16(offsetof 2286 (struct create_lease, Name)); 2287 buf->ccontext.NameLength = cpu_to_le16(4); 2288 buf->Name[0] = 'R'; 2289 buf->Name[1] = 'q'; 2290 buf->Name[2] = 'L'; 2291 buf->Name[3] = 's'; 2292 } 2293 } 2294 2295 /** 2296 * parse_lease_state() - parse lease context contained in file open request 2297 * @open_req: buffer containing smb2 file open(create) request 2298 * 2299 * Return: allocated lease context object on success, otherwise NULL 2300 */ 2301 struct lease_ctx_info *parse_lease_state(void *open_req) 2302 { 2303 struct create_context *cc; 2304 struct smb2_create_req *req = (struct smb2_create_req *)open_req; 2305 struct lease_ctx_info *lreq; 2306 2307 cc = smb2_find_context_vals(req, SMB2_CREATE_REQUEST_LEASE, 4); 2308 if (IS_ERR(cc)) 2309 return ERR_CAST(cc); 2310 if (!cc) 2311 return NULL; 2312 2313 lreq = kzalloc_obj(struct lease_ctx_info, KSMBD_DEFAULT_GFP); 2314 if (!lreq) 2315 return ERR_PTR(-ENOMEM); 2316 2317 if (sizeof(struct lease_context_v2) == le32_to_cpu(cc->DataLength)) { 2318 struct create_lease_v2 *lc = (struct create_lease_v2 *)cc; 2319 2320 if (le16_to_cpu(cc->DataOffset) + le32_to_cpu(cc->DataLength) < 2321 sizeof(struct create_lease_v2) - 4) 2322 goto err_out; 2323 2324 memcpy(lreq->lease_key, lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE); 2325 lreq->req_state = lc->lcontext.LeaseState; 2326 lreq->flags = lc->lcontext.LeaseFlags; 2327 lreq->epoch = lc->lcontext.Epoch; 2328 lreq->duration = lc->lcontext.LeaseDuration; 2329 if (!lease_state_valid(lreq->req_state) || 2330 !lease_v2_flags_valid(lreq->flags)) 2331 goto err_out; 2332 lreq->req_state = lease_state_grantable(lreq->req_state); 2333 if (lreq->flags == SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET_LE) 2334 memcpy(lreq->parent_lease_key, lc->lcontext.ParentLeaseKey, 2335 SMB2_LEASE_KEY_SIZE); 2336 lreq->version = 2; 2337 } else if (sizeof(struct lease_context) == le32_to_cpu(cc->DataLength)) { 2338 struct create_lease *lc = (struct create_lease *)cc; 2339 2340 if (le16_to_cpu(cc->DataOffset) + le32_to_cpu(cc->DataLength) < 2341 sizeof(struct create_lease)) 2342 goto err_out; 2343 2344 memcpy(lreq->lease_key, lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE); 2345 lreq->req_state = lc->lcontext.LeaseState; 2346 lreq->flags = 0; 2347 lreq->duration = lc->lcontext.LeaseDuration; 2348 if (!lease_state_valid(lreq->req_state)) 2349 goto err_out; 2350 lreq->req_state = lease_state_grantable(lreq->req_state); 2351 lreq->version = 1; 2352 } else 2353 goto err_out; 2354 return lreq; 2355 err_out: 2356 kfree(lreq); 2357 return ERR_PTR(-EINVAL); 2358 } 2359 2360 /** 2361 * smb2_find_context_vals() - find a particular context info in open request 2362 * @open_req: buffer containing smb2 file open(create) request 2363 * @tag: context name to search for 2364 * @tag_len: the length of tag 2365 * 2366 * Return: pointer to requested context, NULL if @str context not found 2367 * or error pointer if name length is invalid. 2368 */ 2369 struct create_context *smb2_find_context_vals(void *open_req, const char *tag, int tag_len) 2370 { 2371 struct create_context *cc; 2372 unsigned int next = 0; 2373 char *name; 2374 struct create_context *found = NULL; 2375 bool posix_tag = tag_len == POSIX_CTXT_DATA_LEN && 2376 !memcmp(tag, SMB2_CREATE_TAG_POSIX, POSIX_CTXT_DATA_LEN); 2377 struct smb2_create_req *req = (struct smb2_create_req *)open_req; 2378 unsigned int remain_len, name_off, name_len, value_off, value_len, 2379 cc_len; 2380 2381 /* 2382 * CreateContextsOffset and CreateContextsLength are guaranteed to 2383 * be valid because of ksmbd_smb2_check_message(). 2384 */ 2385 if (!req->CreateContextsOffset || !req->CreateContextsLength) 2386 return NULL; 2387 2388 cc = (struct create_context *)((char *)req + 2389 le32_to_cpu(req->CreateContextsOffset)); 2390 remain_len = le32_to_cpu(req->CreateContextsLength); 2391 do { 2392 cc = (struct create_context *)((char *)cc + next); 2393 if (remain_len < offsetof(struct create_context, Buffer)) 2394 return ERR_PTR(-EINVAL); 2395 2396 next = le32_to_cpu(cc->Next); 2397 name_off = le16_to_cpu(cc->NameOffset); 2398 name_len = le16_to_cpu(cc->NameLength); 2399 value_off = le16_to_cpu(cc->DataOffset); 2400 value_len = le32_to_cpu(cc->DataLength); 2401 cc_len = next ? next : remain_len; 2402 2403 if ((next & 0x7) != 0 || 2404 next > remain_len || 2405 name_off != offsetof(struct create_context, Buffer) || 2406 name_len < 4 || 2407 name_off + name_len > cc_len || 2408 (value_off & 0x7) != 0 || 2409 (value_len && value_off < name_off + (name_len < 8 ? 8 : name_len)) || 2410 ((u64)value_off + value_len > cc_len)) 2411 return ERR_PTR(-EINVAL); 2412 2413 name = (char *)cc + name_off; 2414 if (name_len == tag_len && !memcmp(name, tag, name_len)) { 2415 if (!posix_tag) 2416 return cc; 2417 if (found) 2418 return ERR_PTR(-EINVAL); 2419 found = cc; 2420 } 2421 2422 remain_len -= next; 2423 } while (next != 0); 2424 2425 return found; 2426 } 2427 2428 /** 2429 * create_durable_rsp_buf() - create durable handle context 2430 * @cc: buffer to create durable context response 2431 */ 2432 void create_durable_rsp_buf(char *cc) 2433 { 2434 struct create_durable_rsp *buf; 2435 2436 buf = (struct create_durable_rsp *)cc; 2437 memset(buf, 0, sizeof(struct create_durable_rsp)); 2438 buf->ccontext.DataOffset = cpu_to_le16(offsetof 2439 (struct create_durable_rsp, Data)); 2440 buf->ccontext.DataLength = cpu_to_le32(8); 2441 buf->ccontext.NameOffset = cpu_to_le16(offsetof 2442 (struct create_durable_rsp, Name)); 2443 buf->ccontext.NameLength = cpu_to_le16(4); 2444 /* SMB2_CREATE_DURABLE_HANDLE_RESPONSE is "DHnQ" */ 2445 buf->Name[0] = 'D'; 2446 buf->Name[1] = 'H'; 2447 buf->Name[2] = 'n'; 2448 buf->Name[3] = 'Q'; 2449 } 2450 2451 /** 2452 * create_durable_v2_rsp_buf() - create durable handle v2 context 2453 * @cc: buffer to create durable context response 2454 * @fp: ksmbd file pointer 2455 */ 2456 void create_durable_v2_rsp_buf(char *cc, struct ksmbd_file *fp) 2457 { 2458 struct create_durable_rsp_v2 *buf; 2459 2460 buf = (struct create_durable_rsp_v2 *)cc; 2461 memset(buf, 0, sizeof(*buf)); 2462 buf->ccontext.DataOffset = cpu_to_le16(offsetof 2463 (struct create_durable_rsp_v2, dcontext)); 2464 buf->ccontext.DataLength = cpu_to_le32(8); 2465 buf->ccontext.NameOffset = cpu_to_le16(offsetof 2466 (struct create_durable_rsp_v2, Name)); 2467 buf->ccontext.NameLength = cpu_to_le16(4); 2468 /* SMB2_CREATE_DURABLE_HANDLE_RESPONSE_V2 is "DH2Q" */ 2469 buf->Name[0] = 'D'; 2470 buf->Name[1] = 'H'; 2471 buf->Name[2] = '2'; 2472 buf->Name[3] = 'Q'; 2473 2474 buf->dcontext.Timeout = cpu_to_le32(fp->durable_timeout); 2475 if (fp->is_persistent) 2476 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT); 2477 } 2478 2479 /** 2480 * create_mxac_rsp_buf() - create query maximal access context 2481 * @cc: buffer to create maximal access context response 2482 * @maximal_access: maximal access 2483 */ 2484 void create_mxac_rsp_buf(char *cc, int maximal_access) 2485 { 2486 struct create_mxac_rsp *buf; 2487 2488 buf = (struct create_mxac_rsp *)cc; 2489 memset(buf, 0, sizeof(struct create_mxac_rsp)); 2490 buf->ccontext.DataOffset = cpu_to_le16(offsetof 2491 (struct create_mxac_rsp, QueryStatus)); 2492 buf->ccontext.DataLength = cpu_to_le32(8); 2493 buf->ccontext.NameOffset = cpu_to_le16(offsetof 2494 (struct create_mxac_rsp, Name)); 2495 buf->ccontext.NameLength = cpu_to_le16(4); 2496 /* SMB2_CREATE_QUERY_MAXIMAL_ACCESS_RESPONSE is "MxAc" */ 2497 buf->Name[0] = 'M'; 2498 buf->Name[1] = 'x'; 2499 buf->Name[2] = 'A'; 2500 buf->Name[3] = 'c'; 2501 2502 buf->QueryStatus = STATUS_SUCCESS; 2503 buf->MaximalAccess = cpu_to_le32(maximal_access); 2504 } 2505 2506 void create_disk_id_rsp_buf(char *cc, __u64 file_id, __u64 vol_id) 2507 { 2508 struct create_disk_id_rsp *buf; 2509 2510 buf = (struct create_disk_id_rsp *)cc; 2511 memset(buf, 0, sizeof(struct create_disk_id_rsp)); 2512 buf->ccontext.DataOffset = cpu_to_le16(offsetof 2513 (struct create_disk_id_rsp, DiskFileId)); 2514 buf->ccontext.DataLength = cpu_to_le32(32); 2515 buf->ccontext.NameOffset = cpu_to_le16(offsetof 2516 (struct create_mxac_rsp, Name)); 2517 buf->ccontext.NameLength = cpu_to_le16(4); 2518 /* SMB2_CREATE_QUERY_ON_DISK_ID_RESPONSE is "QFid" */ 2519 buf->Name[0] = 'Q'; 2520 buf->Name[1] = 'F'; 2521 buf->Name[2] = 'i'; 2522 buf->Name[3] = 'd'; 2523 2524 buf->DiskFileId = cpu_to_le64(file_id); 2525 buf->VolumeId = cpu_to_le64(vol_id); 2526 } 2527 2528 /** 2529 * create_posix_rsp_buf() - create posix extension context 2530 * @cc: buffer to create posix on posix response 2531 * @fp: ksmbd file pointer 2532 */ 2533 void create_posix_rsp_buf(char *cc, struct ksmbd_file *fp) 2534 { 2535 struct create_posix_rsp *buf; 2536 struct inode *inode = file_inode(fp->filp); 2537 const struct mnt_idmap *idmap = file_mnt_idmap(fp->filp); 2538 vfsuid_t vfsuid = i_uid_into_vfsuid(idmap, inode); 2539 vfsgid_t vfsgid = i_gid_into_vfsgid(idmap, inode); 2540 2541 buf = (struct create_posix_rsp *)cc; 2542 memset(buf, 0, sizeof(struct create_posix_rsp)); 2543 buf->ccontext.DataOffset = cpu_to_le16(offsetof 2544 (struct create_posix_rsp, nlink)); 2545 /* 2546 * DataLength = nlink(4) + reparse_tag(4) + mode(4) + 2547 * domain sid(28) + unix group sid(16). 2548 */ 2549 buf->ccontext.DataLength = cpu_to_le32(56); 2550 buf->ccontext.NameOffset = cpu_to_le16(offsetof 2551 (struct create_posix_rsp, Name)); 2552 buf->ccontext.NameLength = cpu_to_le16(POSIX_CTXT_DATA_LEN); 2553 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */ 2554 buf->Name[0] = 0x93; 2555 buf->Name[1] = 0xAD; 2556 buf->Name[2] = 0x25; 2557 buf->Name[3] = 0x50; 2558 buf->Name[4] = 0x9C; 2559 buf->Name[5] = 0xB4; 2560 buf->Name[6] = 0x11; 2561 buf->Name[7] = 0xE7; 2562 buf->Name[8] = 0xB4; 2563 buf->Name[9] = 0x23; 2564 buf->Name[10] = 0x83; 2565 buf->Name[11] = 0xDE; 2566 buf->Name[12] = 0x96; 2567 buf->Name[13] = 0x8B; 2568 buf->Name[14] = 0xCD; 2569 buf->Name[15] = 0x7C; 2570 2571 buf->nlink = cpu_to_le32(inode->i_nlink); 2572 buf->reparse_tag = smb2_get_reparse_tag_special_file(inode->i_mode); 2573 buf->mode = cpu_to_le32(inode->i_mode & 07777); 2574 switch (inode->i_mode & S_IFMT) { 2575 case S_IFDIR: 2576 buf->mode |= cpu_to_le32(POSIX_TYPE_DIR << POSIX_FILETYPE_SHIFT); 2577 break; 2578 case S_IFLNK: 2579 buf->mode |= cpu_to_le32(POSIX_TYPE_SYMLINK << POSIX_FILETYPE_SHIFT); 2580 break; 2581 case S_IFCHR: 2582 buf->mode |= cpu_to_le32(POSIX_TYPE_CHARDEV << POSIX_FILETYPE_SHIFT); 2583 break; 2584 case S_IFBLK: 2585 buf->mode |= cpu_to_le32(POSIX_TYPE_BLKDEV << POSIX_FILETYPE_SHIFT); 2586 break; 2587 case S_IFIFO: 2588 buf->mode |= cpu_to_le32(POSIX_TYPE_FIFO << POSIX_FILETYPE_SHIFT); 2589 break; 2590 case S_IFSOCK: 2591 buf->mode |= cpu_to_le32(POSIX_TYPE_SOCKET << POSIX_FILETYPE_SHIFT); 2592 } 2593 /* 2594 * SidBuffer(44) contain two sids(Domain sid(28), UNIX group sid(16)). 2595 * Domain sid(28) = revision(1) + num_subauth(1) + authority(6) + 2596 * sub_auth(4 * 4(num_subauth)) + RID(4). 2597 * UNIX group id(16) = revision(1) + num_subauth(1) + authority(6) + 2598 * sub_auth(4 * 1(num_subauth)) + RID(4). 2599 */ 2600 id_to_sid(from_kuid_munged(&init_user_ns, vfsuid_into_kuid(vfsuid)), 2601 SIDOWNER, (struct smb_sid *)&buf->SidBuffer[0]); 2602 id_to_sid(from_kgid_munged(&init_user_ns, vfsgid_into_kgid(vfsgid)), 2603 SIDUNIX_GROUP, (struct smb_sid *)&buf->SidBuffer[28]); 2604 } 2605 2606 /** 2607 * create_aapl_rsp_buf() - build AAPL kAAPL_SERVER_QUERY response 2608 * @cc: buffer to write the create context into (AAPL_RSP_MAX_SIZE bytes) 2609 * @vol_caps: volume capability flags (SMB2_CRTCTX_AAPL_* volume bits) 2610 * @req_bitmap: the client's request bitmap, echoed back in reply_bitmap 2611 * 2612 * Response format follows the layout observed from macOS's own smbd, and 2613 * matches the client-side parsing in AAPL's published public client kernel 2614 * source (public client behavior reference, kAAPL_SERVER_QUERY 2615 * case): reply_bitmap, then server_caps/vol_caps/model-info fields present 2616 * only when their reply_bitmap bit is set: 2617 * reply_bitmap = req_bitmap masked to the fields we support 2618 * server_caps = AAPL_SERVER_CAPS_KSMBD when requested 2619 * vol_caps = caller-supplied 2620 * model string = server_conf.aapl_model (default "Xserve") in UTF-16LE, 2621 * when SMB2_CRTCTX_AAPL_MODEL_INFO requested 2622 * 2623 * Sending reply_bitmap with MODEL_INFO set but no model string causes 2624 * smbfs.kext to enter a broken disconnect path requiring a macOS reboot. 2625 * @readdir_attr_v2: advertise SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR_V2 2626 * instead of the V1 bit 2627 */ 2628 void create_aapl_rsp_buf(char *cc, __u64 vol_caps, __u64 req_bitmap, 2629 bool readdir_attr_v2) 2630 { 2631 struct create_aapl_rsp *buf; 2632 u64 reply_bitmap; 2633 u64 server_caps; 2634 u32 data_len; 2635 2636 buf = (struct create_aapl_rsp *)cc; 2637 memset(buf, 0, AAPL_RSP_MAX_SIZE); 2638 2639 reply_bitmap = req_bitmap & (SMB2_CRTCTX_AAPL_SERVER_CAPS | 2640 SMB2_CRTCTX_AAPL_VOLUME_CAPS | 2641 SMB2_CRTCTX_AAPL_MODEL_INFO); 2642 2643 /* base data: cmd(4)+reserved(4)+reply_bitmap(8)+server_caps(8)+vol_caps(8) */ 2644 data_len = 32; 2645 if (reply_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) 2646 data_len += 4 + 4 + AAPL_MODEL_UTF16_BYTES; /* pad2+model_bytes+string */ 2647 2648 buf->ccontext.DataOffset = cpu_to_le16(offsetof(struct create_aapl_rsp, cmd)); 2649 buf->ccontext.DataLength = cpu_to_le32(data_len); 2650 buf->ccontext.NameOffset = cpu_to_le16(offsetof(struct create_aapl_rsp, Name)); 2651 buf->ccontext.NameLength = cpu_to_le16(SMB2_CREATE_AAPL_LEN); 2652 buf->Name[0] = 'A'; 2653 buf->Name[1] = 'A'; 2654 buf->Name[2] = 'P'; 2655 buf->Name[3] = 'L'; 2656 2657 buf->cmd = cpu_to_le32(SMB2_CRTCTX_AAPL_SERVER_QUERY); 2658 buf->reply_bitmap = cpu_to_le64(reply_bitmap); 2659 server_caps = AAPL_SERVER_CAPS_KSMBD; 2660 if (readdir_attr_v2) 2661 server_caps = (server_caps & ~SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR) | 2662 SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR_V2; 2663 buf->server_caps = (reply_bitmap & SMB2_CRTCTX_AAPL_SERVER_CAPS) ? 2664 cpu_to_le64(server_caps) : 0; 2665 buf->vol_caps = (reply_bitmap & SMB2_CRTCTX_AAPL_VOLUME_CAPS) ? 2666 cpu_to_le64(vol_caps) : 0; 2667 2668 if (reply_bitmap & SMB2_CRTCTX_AAPL_MODEL_INFO) { 2669 __le32 *p = (__le32 *)((u8 *)buf + sizeof(*buf)); 2670 __le16 *model_str = (__le16 *)(p + 2); 2671 const char *src = server_conf.aapl_model[0] ? 2672 server_conf.aapl_model : "Xserve"; 2673 int i, model_bytes = 0; 2674 2675 /* Convert ASCII model string to UTF-16LE in-place */ 2676 for (i = 0; src[i] && i < AAPL_MODEL_MAX_CHARS; i++) { 2677 model_str[i] = cpu_to_le16((unsigned char)src[i]); 2678 model_bytes += 2; 2679 } 2680 2681 p[0] = 0; /* pad2 */ 2682 p[1] = cpu_to_le32(model_bytes); 2683 2684 /* Update DataLength to reflect actual model string size */ 2685 buf->ccontext.DataLength = 2686 cpu_to_le32(data_len - AAPL_MODEL_UTF16_BYTES + model_bytes); 2687 } 2688 } 2689 2690 /* 2691 * Find lease object(opinfo) for given lease key/fid from lease 2692 * break/file close path. 2693 */ 2694 /** 2695 * lookup_lease_in_table() - find a matching lease info object 2696 * @conn: connection instance 2697 * @lease_key: lease key to be searched for 2698 * 2699 * Return: opinfo if found matching opinfo, otherwise NULL 2700 */ 2701 struct oplock_info *lookup_lease_in_table(struct ksmbd_conn *conn, 2702 char *lease_key) 2703 { 2704 struct oplock_info *opinfo = NULL, *ret_op = NULL; 2705 struct lease *lease; 2706 struct lease_table *lt; 2707 2708 read_lock(&lease_list_lock); 2709 list_for_each_entry(lt, &lease_table_list, l_entry) { 2710 if (!memcmp(lt->client_guid, conn->ClientGUID, 2711 SMB2_CLIENT_GUID_SIZE)) 2712 goto found; 2713 } 2714 2715 read_unlock(&lease_list_lock); 2716 return NULL; 2717 2718 found: 2719 list_for_each_entry(lease, <->lease_list, l_entry) { 2720 if (memcmp(lease->lease_key, lease_key, SMB2_LEASE_KEY_SIZE)) 2721 continue; 2722 spin_lock(&lease->lock); 2723 if (!(lease->state & (SMB2_LEASE_HANDLE_CACHING_LE | 2724 SMB2_LEASE_WRITE_CACHING_LE))) { 2725 spin_unlock(&lease->lock); 2726 break; 2727 } 2728 2729 list_for_each_entry(opinfo, &lease->open_list, lease_entry) { 2730 if (!opinfo->op_state || 2731 opinfo->op_state == OPLOCK_CLOSING) 2732 continue; 2733 if (!atomic_inc_not_zero(&opinfo->refcount)) 2734 continue; 2735 ret_op = opinfo; 2736 break; 2737 } 2738 spin_unlock(&lease->lock); 2739 if (ret_op) { 2740 ksmbd_debug(OPLOCK, "found opinfo\n"); 2741 goto out; 2742 } 2743 break; 2744 } 2745 2746 out: 2747 read_unlock(&lease_list_lock); 2748 return ret_op; 2749 } 2750 2751 int smb2_check_durable_oplock(struct ksmbd_conn *conn, 2752 struct ksmbd_share_config *share, 2753 struct ksmbd_file *fp, 2754 struct lease_ctx_info *lctx, 2755 struct ksmbd_user *user, 2756 char *name) 2757 { 2758 struct oplock_info *opinfo = opinfo_get(fp); 2759 int ret = 0; 2760 2761 if (!opinfo) 2762 return 0; 2763 2764 if (ksmbd_has_other_active_fd(fp)) { 2765 ksmbd_debug(SMB, "Durable handle reconnect failed: competing open\n"); 2766 ret = -EBADF; 2767 goto out; 2768 } 2769 2770 if (ksmbd_vfs_compare_durable_owner(fp, user) == false) { 2771 ksmbd_debug(SMB, "Durable handle reconnect failed: owner mismatch\n"); 2772 ret = -EBADF; 2773 goto out; 2774 } 2775 2776 if (opinfo->is_lease == false) { 2777 if (lctx) { 2778 pr_err("create context include lease\n"); 2779 ret = -EBADF; 2780 goto out; 2781 } 2782 2783 if (opinfo->level != SMB2_OPLOCK_LEVEL_BATCH) { 2784 pr_err("oplock level is not equal to SMB2_OPLOCK_LEVEL_BATCH\n"); 2785 ret = -EBADF; 2786 } 2787 2788 goto out; 2789 } 2790 2791 if (memcmp(conn->ClientGUID, fp->client_guid, 2792 SMB2_CLIENT_GUID_SIZE)) { 2793 ksmbd_debug(SMB, "Client guid of fp is not equal to the one of connection\n"); 2794 ret = -EBADF; 2795 goto out; 2796 } 2797 2798 if (!lctx) { 2799 ksmbd_debug(SMB, "create context does not include lease\n"); 2800 ret = -EBADF; 2801 goto out; 2802 } 2803 2804 if (memcmp(opinfo->o_lease->lease_key, lctx->lease_key, 2805 SMB2_LEASE_KEY_SIZE)) { 2806 ksmbd_debug(SMB, 2807 "lease key of fp does not match lease key in create context\n"); 2808 ret = -EBADF; 2809 goto out; 2810 } 2811 2812 if (!(opinfo->o_lease->state & SMB2_LEASE_HANDLE_CACHING_LE)) { 2813 ksmbd_debug(SMB, "lease state does not contain SMB2_LEASE_HANDLE_CACHING\n"); 2814 ret = -EBADF; 2815 goto out; 2816 } 2817 2818 if (opinfo->o_lease->version != lctx->version) { 2819 ksmbd_debug(SMB, 2820 "lease version of fp does not match the one in create context\n"); 2821 ret = -EBADF; 2822 goto out; 2823 } 2824 2825 if (!ksmbd_inode_pending_delete(fp)) 2826 ret = ksmbd_validate_name_reconnect(share, fp, name); 2827 out: 2828 opinfo_put(opinfo); 2829 return ret; 2830 } 2831