1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 /* 27 * General Structures Layout 28 * ------------------------- 29 * 30 * This is a simplified diagram showing the relationship between most of the 31 * main structures. 32 * 33 * +-------------------+ 34 * | SMB_INFO | 35 * +-------------------+ 36 * | 37 * | 38 * v 39 * +-------------------+ +-------------------+ +-------------------+ 40 * | SESSION |<----->| SESSION |......| SESSION | 41 * +-------------------+ +-------------------+ +-------------------+ 42 * | 43 * | 44 * v 45 * +-------------------+ +-------------------+ +-------------------+ 46 * | USER |<----->| USER |......| USER | 47 * +-------------------+ +-------------------+ +-------------------+ 48 * | 49 * | 50 * v 51 * +-------------------+ +-------------------+ +-------------------+ 52 * | TREE |<----->| TREE |......| TREE | 53 * +-------------------+ +-------------------+ +-------------------+ 54 * | | 55 * | | 56 * | v 57 * | +-------+ +-------+ +-------+ 58 * | | OFILE |<----->| OFILE |......| OFILE | 59 * | +-------+ +-------+ +-------+ 60 * | 61 * | 62 * v 63 * +-------+ +------+ +------+ 64 * | ODIR |<----->| ODIR |......| ODIR | 65 * +-------+ +------+ +------+ 66 * 67 * 68 * Ofile State Machine 69 * ------------------ 70 * 71 * +-------------------------+ T0 72 * | SMB_OFILE_STATE_OPEN |<----------- Creation/Allocation 73 * +-------------------------+ 74 * | 75 * | T1 76 * | 77 * v 78 * +-------------------------+ 79 * | SMB_OFILE_STATE_CLOSING | 80 * +-------------------------+ 81 * | 82 * | T2 83 * | 84 * v 85 * +-------------------------+ T3 86 * | SMB_OFILE_STATE_CLOSED |----------> Deletion/Free 87 * +-------------------------+ 88 * 89 * SMB_OFILE_STATE_OPEN 90 * 91 * While in this state: 92 * - The ofile is queued in the list of ofiles of its tree. 93 * - References will be given out if the ofile is looked up. 94 * 95 * SMB_OFILE_STATE_CLOSING 96 * 97 * While in this state: 98 * - The ofile is queued in the list of ofiles of its tree. 99 * - References will not be given out if the ofile is looked up. 100 * - The file is closed and the locks held are being released. 101 * - The resources associated with the ofile remain. 102 * 103 * SMB_OFILE_STATE_CLOSED 104 * 105 * While in this state: 106 * - The ofile is queued in the list of ofiles of its tree. 107 * - References will not be given out if the ofile is looked up. 108 * - The resources associated with the ofile remain. 109 * 110 * Transition T0 111 * 112 * This transition occurs in smb_ofile_open(). A new ofile is created and 113 * added to the list of ofiles of a tree. 114 * 115 * Transition T1 116 * 117 * This transition occurs in smb_ofile_close(). 118 * 119 * Transition T2 120 * 121 * This transition occurs in smb_ofile_release(). The resources associated 122 * with the ofile are freed as well as the ofile structure. For the 123 * transition to occur, the ofile must be in the SMB_OFILE_STATE_CLOSED 124 * state and the reference count be zero. 125 * 126 * Comments 127 * -------- 128 * 129 * The state machine of the ofile structures is controlled by 3 elements: 130 * - The list of ofiles of the tree it belongs to. 131 * - The mutex embedded in the structure itself. 132 * - The reference count. 133 * 134 * There's a mutex embedded in the ofile structure used to protect its fields 135 * and there's a lock embedded in the list of ofiles of a tree. To 136 * increment or to decrement the reference count the mutex must be entered. 137 * To insert the ofile into the list of ofiles of the tree and to remove 138 * the ofile from it, the lock must be entered in RW_WRITER mode. 139 * 140 * Rules of access to a ofile structure: 141 * 142 * 1) In order to avoid deadlocks, when both (mutex and lock of the ofile 143 * list) have to be entered, the lock must be entered first. 144 * 145 * 2) All actions applied to an ofile require a reference count. 146 * 147 * 3) There are 2 ways of getting a reference count. One is when the ofile 148 * is opened. The other one when the ofile is looked up. This translates 149 * into 2 functions: smb_ofile_open() and smb_ofile_lookup_by_fid(). 150 * 151 * It should be noted that the reference count of an ofile registers the 152 * number of references to the ofile in other structures (such as an smb 153 * request). The reference count is not incremented in these 2 instances: 154 * 155 * 1) The ofile is open. An ofile is anchored by his state. If there's 156 * no activity involving an ofile currently open, the reference count 157 * of that ofile is zero. 158 * 159 * 2) The ofile is queued in the list of ofiles of its tree. The fact of 160 * being queued in that list is NOT registered by incrementing the 161 * reference count. 162 */ 163 #include <smbsrv/smb_kproto.h> 164 #include <smbsrv/smb_fsops.h> 165 166 static boolean_t smb_ofile_is_open_locked(smb_ofile_t *); 167 static smb_ofile_t *smb_ofile_close_and_next(smb_ofile_t *); 168 static void smb_ofile_set_close_attrs(smb_ofile_t *, uint32_t); 169 static int smb_ofile_netinfo_encode(smb_ofile_t *, uint8_t *, size_t, 170 uint32_t *); 171 static int smb_ofile_netinfo_init(smb_ofile_t *, smb_netfileinfo_t *); 172 static void smb_ofile_netinfo_fini(smb_netfileinfo_t *); 173 174 /* 175 * smb_ofile_open 176 */ 177 smb_ofile_t * 178 smb_ofile_open( 179 smb_tree_t *tree, 180 smb_node_t *node, 181 uint16_t pid, 182 struct open_param *op, 183 uint16_t ftype, 184 uint32_t uniqid, 185 smb_error_t *err) 186 { 187 smb_ofile_t *of; 188 uint16_t fid; 189 smb_attr_t attr; 190 191 if (smb_idpool_alloc(&tree->t_fid_pool, &fid)) { 192 err->status = NT_STATUS_TOO_MANY_OPENED_FILES; 193 err->errcls = ERRDOS; 194 err->errcode = ERROR_TOO_MANY_OPEN_FILES; 195 return (NULL); 196 } 197 198 of = kmem_cache_alloc(tree->t_server->si_cache_ofile, KM_SLEEP); 199 bzero(of, sizeof (smb_ofile_t)); 200 of->f_magic = SMB_OFILE_MAGIC; 201 of->f_refcnt = 1; 202 of->f_fid = fid; 203 of->f_uniqid = uniqid; 204 of->f_opened_by_pid = pid; 205 of->f_granted_access = op->desired_access; 206 of->f_share_access = op->share_access; 207 of->f_create_options = op->create_options; 208 of->f_cr = (op->create_options & FILE_OPEN_FOR_BACKUP_INTENT) ? 209 smb_user_getprivcred(tree->t_user) : tree->t_user->u_cred; 210 crhold(of->f_cr); 211 of->f_ftype = ftype; 212 of->f_server = tree->t_server; 213 of->f_session = tree->t_user->u_session; 214 of->f_user = tree->t_user; 215 of->f_tree = tree; 216 of->f_node = node; 217 of->f_explicit_times = 0; 218 mutex_init(&of->f_mutex, NULL, MUTEX_DEFAULT, NULL); 219 of->f_state = SMB_OFILE_STATE_OPEN; 220 221 222 if (ftype == SMB_FTYPE_MESG_PIPE) { 223 of->f_pipe = smb_opipe_alloc(tree->t_server); 224 } else { 225 ASSERT(ftype == SMB_FTYPE_DISK); /* Regular file, not a pipe */ 226 ASSERT(node); 227 228 if (of->f_granted_access == FILE_EXECUTE) 229 of->f_flags |= SMB_OFLAGS_EXECONLY; 230 231 bzero(&attr, sizeof (smb_attr_t)); 232 attr.sa_mask |= SMB_AT_UID; 233 if (smb_fsop_getattr(NULL, kcred, node, &attr) != 0) { 234 of->f_magic = 0; 235 mutex_destroy(&of->f_mutex); 236 crfree(of->f_cr); 237 smb_idpool_free(&tree->t_fid_pool, of->f_fid); 238 kmem_cache_free(tree->t_server->si_cache_ofile, of); 239 err->status = NT_STATUS_INTERNAL_ERROR; 240 err->errcls = ERRDOS; 241 err->errcode = ERROR_INTERNAL_ERROR; 242 return (NULL); 243 } 244 if (crgetuid(of->f_cr) == attr.sa_vattr.va_uid) { 245 /* 246 * Add this bit for the file's owner even if it's not 247 * specified in the request (Windows behavior). 248 */ 249 of->f_granted_access |= FILE_READ_ATTRIBUTES; 250 } 251 252 if (smb_node_is_file(node)) { 253 of->f_mode = 254 smb_fsop_amask_to_omode(of->f_granted_access); 255 if (smb_fsop_open(node, of->f_mode, of->f_cr) != 0) { 256 of->f_magic = 0; 257 mutex_destroy(&of->f_mutex); 258 crfree(of->f_cr); 259 smb_idpool_free(&tree->t_fid_pool, of->f_fid); 260 kmem_cache_free(tree->t_server->si_cache_ofile, 261 of); 262 err->status = NT_STATUS_ACCESS_DENIED; 263 err->errcls = ERRDOS; 264 err->errcode = ERROR_ACCESS_DENIED; 265 return (NULL); 266 } 267 } 268 269 if (tree->t_flags & SMB_TREE_READONLY) 270 of->f_flags |= SMB_OFLAGS_READONLY; 271 272 if (op->created_readonly) 273 node->readonly_creator = of; 274 275 smb_node_inc_open_ofiles(node); 276 smb_node_add_ofile(node, of); 277 smb_node_ref(node); 278 } 279 smb_llist_enter(&tree->t_ofile_list, RW_WRITER); 280 smb_llist_insert_tail(&tree->t_ofile_list, of); 281 smb_llist_exit(&tree->t_ofile_list); 282 atomic_inc_32(&tree->t_open_files); 283 atomic_inc_32(&tree->t_server->sv_open_files); 284 atomic_inc_32(&of->f_session->s_file_cnt); 285 286 return (of); 287 } 288 289 /* 290 * smb_ofile_close 291 */ 292 void 293 smb_ofile_close(smb_ofile_t *of, uint32_t last_wtime) 294 { 295 ASSERT(of); 296 ASSERT(of->f_magic == SMB_OFILE_MAGIC); 297 uint32_t flags = 0; 298 299 mutex_enter(&of->f_mutex); 300 ASSERT(of->f_refcnt); 301 switch (of->f_state) { 302 case SMB_OFILE_STATE_OPEN: { 303 304 of->f_state = SMB_OFILE_STATE_CLOSING; 305 mutex_exit(&of->f_mutex); 306 307 if (of->f_ftype == SMB_FTYPE_MESG_PIPE) { 308 smb_opipe_close(of); 309 } else { 310 smb_ofile_set_close_attrs(of, last_wtime); 311 312 if (of->f_flags & SMB_OFLAGS_SET_DELETE_ON_CLOSE) { 313 if (smb_tree_has_feature(of->f_tree, 314 SMB_TREE_CATIA)) { 315 flags |= SMB_CATIA; 316 } 317 (void) smb_node_set_delete_on_close(of->f_node, 318 of->f_cr, flags); 319 } 320 smb_fsop_unshrlock(of->f_cr, of->f_node, of->f_uniqid); 321 smb_node_destroy_lock_by_ofile(of->f_node, of); 322 323 if (smb_node_is_file(of->f_node)) 324 (void) smb_fsop_close(of->f_node, of->f_mode, 325 of->f_cr); 326 327 /* 328 * Cancel any notify change requests related 329 * to this open instance. 330 */ 331 if (of->f_node->flags & NODE_FLAGS_NOTIFY_CHANGE) 332 smb_process_file_notify_change_queue(of); 333 } 334 atomic_dec_32(&of->f_tree->t_open_files); 335 atomic_dec_32(&of->f_tree->t_server->sv_open_files); 336 337 mutex_enter(&of->f_mutex); 338 ASSERT(of->f_refcnt); 339 ASSERT(of->f_state == SMB_OFILE_STATE_CLOSING); 340 of->f_state = SMB_OFILE_STATE_CLOSED; 341 mutex_exit(&of->f_mutex); 342 if (of->f_node != NULL) { 343 smb_node_dec_open_ofiles(of->f_node); 344 if (of->f_oplock_granted) { 345 smb_oplock_release(of->f_node, of); 346 of->f_oplock_granted = B_FALSE; 347 } 348 } 349 return; 350 } 351 case SMB_OFILE_STATE_CLOSED: 352 case SMB_OFILE_STATE_CLOSING: 353 break; 354 355 default: 356 ASSERT(0); 357 break; 358 } 359 mutex_exit(&of->f_mutex); 360 } 361 362 /* 363 * smb_ofile_close_all 364 * 365 * 366 */ 367 void 368 smb_ofile_close_all( 369 smb_tree_t *tree) 370 { 371 smb_ofile_t *of; 372 373 ASSERT(tree); 374 ASSERT(tree->t_magic == SMB_TREE_MAGIC); 375 376 smb_llist_enter(&tree->t_ofile_list, RW_READER); 377 of = smb_llist_head(&tree->t_ofile_list); 378 while (of) { 379 ASSERT(of->f_magic == SMB_OFILE_MAGIC); 380 ASSERT(of->f_tree == tree); 381 of = smb_ofile_close_and_next(of); 382 } 383 smb_llist_exit(&tree->t_ofile_list); 384 } 385 386 /* 387 * smb_ofiles_close_by_pid 388 * 389 * 390 */ 391 void 392 smb_ofile_close_all_by_pid( 393 smb_tree_t *tree, 394 uint16_t pid) 395 { 396 smb_ofile_t *of; 397 398 ASSERT(tree); 399 ASSERT(tree->t_magic == SMB_TREE_MAGIC); 400 401 smb_llist_enter(&tree->t_ofile_list, RW_READER); 402 of = smb_llist_head(&tree->t_ofile_list); 403 while (of) { 404 ASSERT(of->f_magic == SMB_OFILE_MAGIC); 405 ASSERT(of->f_tree == tree); 406 if (of->f_opened_by_pid == pid) { 407 of = smb_ofile_close_and_next(of); 408 } else { 409 of = smb_llist_next(&tree->t_ofile_list, of); 410 } 411 } 412 smb_llist_exit(&tree->t_ofile_list); 413 } 414 415 /* 416 * If the enumeration request is for ofile data, handle it here. 417 * Otherwise, return. 418 * 419 * This function should be called with a hold on the ofile. 420 */ 421 int 422 smb_ofile_enum(smb_ofile_t *of, smb_svcenum_t *svcenum) 423 { 424 uint8_t *pb; 425 uint_t nbytes; 426 int rc; 427 428 ASSERT(of); 429 ASSERT(of->f_magic == SMB_OFILE_MAGIC); 430 ASSERT(of->f_refcnt); 431 432 if (svcenum->se_type != SMB_SVCENUM_TYPE_FILE) 433 return (0); 434 435 if (svcenum->se_nskip > 0) { 436 svcenum->se_nskip--; 437 return (0); 438 } 439 440 if (svcenum->se_nitems >= svcenum->se_nlimit) { 441 svcenum->se_nitems = svcenum->se_nlimit; 442 return (0); 443 } 444 445 pb = &svcenum->se_buf[svcenum->se_bused]; 446 447 rc = smb_ofile_netinfo_encode(of, pb, svcenum->se_bavail, 448 &nbytes); 449 if (rc == 0) { 450 svcenum->se_bavail -= nbytes; 451 svcenum->se_bused += nbytes; 452 svcenum->se_nitems++; 453 } 454 455 return (rc); 456 } 457 458 /* 459 * Take a reference on an open file. 460 */ 461 boolean_t 462 smb_ofile_hold(smb_ofile_t *of) 463 { 464 ASSERT(of); 465 ASSERT(of->f_magic == SMB_OFILE_MAGIC); 466 467 mutex_enter(&of->f_mutex); 468 469 if (smb_ofile_is_open_locked(of)) { 470 of->f_refcnt++; 471 mutex_exit(&of->f_mutex); 472 return (B_TRUE); 473 } 474 475 mutex_exit(&of->f_mutex); 476 return (B_FALSE); 477 } 478 479 /* 480 * Release a reference on a file. If the reference count falls to 481 * zero and the file has been closed, post the object for deletion. 482 * Object deletion is deferred to avoid modifying a list while an 483 * iteration may be in progress. 484 */ 485 void 486 smb_ofile_release(smb_ofile_t *of) 487 { 488 boolean_t rb; 489 490 SMB_OFILE_VALID(of); 491 492 mutex_enter(&of->f_mutex); 493 if (of->f_oplock_exit) { 494 mutex_exit(&of->f_mutex); 495 rb = smb_oplock_broadcast(of->f_node); 496 mutex_enter(&of->f_mutex); 497 if (rb) 498 of->f_oplock_exit = B_FALSE; 499 } 500 ASSERT(of->f_refcnt); 501 of->f_refcnt--; 502 switch (of->f_state) { 503 case SMB_OFILE_STATE_OPEN: 504 case SMB_OFILE_STATE_CLOSING: 505 break; 506 507 case SMB_OFILE_STATE_CLOSED: 508 if (of->f_refcnt == 0) 509 smb_tree_post_ofile(of->f_tree, of); 510 break; 511 512 default: 513 ASSERT(0); 514 break; 515 } 516 mutex_exit(&of->f_mutex); 517 } 518 519 /* 520 * smb_ofile_lookup_by_fid 521 * 522 * Find the open file whose fid matches the one specified in the request. 523 * If we can't find the fid or the shares (trees) don't match, we have a 524 * bad fid. 525 */ 526 smb_ofile_t * 527 smb_ofile_lookup_by_fid( 528 smb_tree_t *tree, 529 uint16_t fid) 530 { 531 smb_llist_t *of_list; 532 smb_ofile_t *of; 533 534 ASSERT(tree->t_magic == SMB_TREE_MAGIC); 535 536 of_list = &tree->t_ofile_list; 537 538 smb_llist_enter(of_list, RW_READER); 539 of = smb_llist_head(of_list); 540 while (of) { 541 ASSERT(of->f_magic == SMB_OFILE_MAGIC); 542 ASSERT(of->f_tree == tree); 543 if (of->f_fid == fid) { 544 mutex_enter(&of->f_mutex); 545 if (of->f_state != SMB_OFILE_STATE_OPEN) { 546 mutex_exit(&of->f_mutex); 547 smb_llist_exit(of_list); 548 return (NULL); 549 } 550 of->f_refcnt++; 551 mutex_exit(&of->f_mutex); 552 break; 553 } 554 of = smb_llist_next(of_list, of); 555 } 556 smb_llist_exit(of_list); 557 return (of); 558 } 559 560 /* 561 * smb_ofile_lookup_by_uniqid 562 * 563 * Find the open file whose uniqid matches the one specified in the request. 564 */ 565 smb_ofile_t * 566 smb_ofile_lookup_by_uniqid(smb_tree_t *tree, uint32_t uniqid) 567 { 568 smb_llist_t *of_list; 569 smb_ofile_t *of; 570 571 ASSERT(tree->t_magic == SMB_TREE_MAGIC); 572 573 of_list = &tree->t_ofile_list; 574 smb_llist_enter(of_list, RW_READER); 575 of = smb_llist_head(of_list); 576 577 while (of) { 578 ASSERT(of->f_magic == SMB_OFILE_MAGIC); 579 ASSERT(of->f_tree == tree); 580 581 if (of->f_uniqid == uniqid) { 582 if (smb_ofile_hold(of)) { 583 smb_llist_exit(of_list); 584 return (of); 585 } 586 } 587 588 of = smb_llist_next(of_list, of); 589 } 590 591 smb_llist_exit(of_list); 592 return (NULL); 593 } 594 595 /* 596 * Disallow NetFileClose on certain ofiles to avoid side-effects. 597 * Closing a tree root is not allowed: use NetSessionDel or NetShareDel. 598 * Closing SRVSVC connections is not allowed because this NetFileClose 599 * request may depend on this ofile. 600 */ 601 boolean_t 602 smb_ofile_disallow_fclose(smb_ofile_t *of) 603 { 604 ASSERT(of); 605 ASSERT(of->f_magic == SMB_OFILE_MAGIC); 606 ASSERT(of->f_refcnt); 607 608 switch (of->f_ftype) { 609 case SMB_FTYPE_DISK: 610 ASSERT(of->f_tree); 611 return (of->f_node == of->f_tree->t_snode); 612 613 case SMB_FTYPE_MESG_PIPE: 614 ASSERT(of->f_pipe); 615 if (smb_strcasecmp(of->f_pipe->p_name, "SRVSVC", 0) == 0) 616 return (B_TRUE); 617 break; 618 default: 619 break; 620 } 621 622 return (B_FALSE); 623 } 624 625 /* 626 * smb_ofile_set_flags 627 * 628 * Return value: 629 * 630 * Current flags value 631 * 632 */ 633 void 634 smb_ofile_set_flags( 635 smb_ofile_t *of, 636 uint32_t flags) 637 { 638 ASSERT(of); 639 ASSERT(of->f_magic == SMB_OFILE_MAGIC); 640 ASSERT(of->f_refcnt); 641 642 mutex_enter(&of->f_mutex); 643 of->f_flags |= flags; 644 mutex_exit(&of->f_mutex); 645 } 646 647 /* 648 * smb_ofile_seek 649 * 650 * Return value: 651 * 652 * 0 Success 653 * EINVAL Unknown mode 654 * EOVERFLOW offset too big 655 * 656 */ 657 int 658 smb_ofile_seek( 659 smb_ofile_t *of, 660 ushort_t mode, 661 int32_t off, 662 uint32_t *retoff) 663 { 664 u_offset_t newoff = 0; 665 int rc = 0; 666 smb_attr_t attr; 667 668 ASSERT(of); 669 ASSERT(of->f_magic == SMB_OFILE_MAGIC); 670 ASSERT(of->f_refcnt); 671 672 mutex_enter(&of->f_mutex); 673 switch (mode) { 674 case SMB_SEEK_SET: 675 if (off < 0) 676 newoff = 0; 677 else 678 newoff = (u_offset_t)off; 679 break; 680 681 case SMB_SEEK_CUR: 682 if (off < 0 && (-off) > of->f_seek_pos) 683 newoff = 0; 684 else 685 newoff = of->f_seek_pos + (u_offset_t)off; 686 break; 687 688 case SMB_SEEK_END: 689 bzero(&attr, sizeof (smb_attr_t)); 690 attr.sa_mask |= SMB_AT_SIZE; 691 rc = smb_fsop_getattr(NULL, kcred, of->f_node, &attr); 692 if (rc != 0) { 693 mutex_exit(&of->f_mutex); 694 return (rc); 695 } 696 if (off < 0 && (-off) > attr.sa_vattr.va_size) 697 newoff = 0; 698 else 699 newoff = attr.sa_vattr.va_size + (u_offset_t)off; 700 break; 701 702 default: 703 mutex_exit(&of->f_mutex); 704 return (EINVAL); 705 } 706 707 /* 708 * See comments at the beginning of smb_seek.c. 709 * If the offset is greater than UINT_MAX, we will return an error. 710 */ 711 712 if (newoff > UINT_MAX) { 713 rc = EOVERFLOW; 714 } else { 715 of->f_seek_pos = newoff; 716 *retoff = (uint32_t)newoff; 717 } 718 mutex_exit(&of->f_mutex); 719 return (rc); 720 } 721 722 /* 723 * smb_ofile_is_open 724 */ 725 boolean_t 726 smb_ofile_is_open(smb_ofile_t *of) 727 { 728 boolean_t rc; 729 730 SMB_OFILE_VALID(of); 731 732 mutex_enter(&of->f_mutex); 733 rc = smb_ofile_is_open_locked(of); 734 mutex_exit(&of->f_mutex); 735 return (rc); 736 } 737 738 void 739 smb_ofile_set_oplock_granted(smb_ofile_t *of) 740 { 741 SMB_OFILE_VALID(of); 742 mutex_enter(&of->f_mutex); 743 ASSERT(!of->f_oplock_granted); 744 of->f_oplock_granted = B_TRUE; 745 of->f_oplock_exit = B_TRUE; 746 mutex_exit(&of->f_mutex); 747 } 748 749 /* 750 * smb_ofile_pending_write_time 751 * 752 * Flag write times as pending - to be set on close, setattr 753 * or delayed write timer. 754 */ 755 void 756 smb_ofile_set_write_time_pending(smb_ofile_t *of) 757 { 758 SMB_OFILE_VALID(of); 759 mutex_enter(&of->f_mutex); 760 of->f_flags |= SMB_OFLAGS_TIMESTAMPS_PENDING; 761 mutex_exit(&of->f_mutex); 762 } 763 764 /* 765 * smb_ofile_write_time_pending 766 * 767 * Get and reset the write times pending flag. 768 */ 769 boolean_t 770 smb_ofile_write_time_pending(smb_ofile_t *of) 771 { 772 boolean_t rc = B_FALSE; 773 774 SMB_OFILE_VALID(of); 775 mutex_enter(&of->f_mutex); 776 if (of->f_flags & SMB_OFLAGS_TIMESTAMPS_PENDING) { 777 rc = B_TRUE; 778 of->f_flags &= ~SMB_OFLAGS_TIMESTAMPS_PENDING; 779 } 780 mutex_exit(&of->f_mutex); 781 782 return (rc); 783 } 784 785 /* 786 * smb_ofile_set_explicit_time_flag 787 * 788 * Note the timestamps specified in "what", as having been 789 * explicity set for the ofile. 790 */ 791 void 792 smb_ofile_set_explicit_times(smb_ofile_t *of, uint32_t what) 793 { 794 SMB_OFILE_VALID(of); 795 mutex_enter(&of->f_mutex); 796 of->f_explicit_times |= (what & SMB_AT_TIMES); 797 mutex_exit(&of->f_mutex); 798 } 799 800 uint32_t 801 smb_ofile_explicit_times(smb_ofile_t *of) 802 { 803 uint32_t rc; 804 805 SMB_OFILE_VALID(of); 806 mutex_enter(&of->f_mutex); 807 rc = of->f_explicit_times; 808 mutex_exit(&of->f_mutex); 809 810 return (rc); 811 } 812 813 /* *************************** Static Functions ***************************** */ 814 815 /* 816 * Determine whether or not an ofile is open. 817 * This function must be called with the mutex held. 818 */ 819 static boolean_t 820 smb_ofile_is_open_locked(smb_ofile_t *of) 821 { 822 switch (of->f_state) { 823 case SMB_OFILE_STATE_OPEN: 824 return (B_TRUE); 825 826 case SMB_OFILE_STATE_CLOSING: 827 case SMB_OFILE_STATE_CLOSED: 828 return (B_FALSE); 829 830 default: 831 ASSERT(0); 832 return (B_FALSE); 833 } 834 } 835 836 /* 837 * smb_ofile_set_close_attrs 838 * 839 * Updates timestamps, size and readonly bit. 840 * The last_wtime is specified in the request received 841 * from the client. If it is neither 0 nor -1, this time 842 * should be used as the file's mtime. It must first be 843 * converted from the server's localtime (as received in 844 * the client's request) to GMT. 845 * 846 * Call smb_node_setattr even if no attributes are being 847 * explicitly set, to set any pending attributes. 848 */ 849 static void 850 smb_ofile_set_close_attrs(smb_ofile_t *of, uint32_t last_wtime) 851 { 852 smb_node_t *node = of->f_node; 853 smb_attr_t attr; 854 855 bzero(&attr, sizeof (smb_attr_t)); 856 857 /* For files created readonly, propagate readonly bit */ 858 if (node->readonly_creator == of) { 859 attr.sa_mask |= SMB_AT_DOSATTR; 860 if (smb_fsop_getattr(NULL, kcred, node, &attr) && 861 (attr.sa_dosattr & FILE_ATTRIBUTE_READONLY)) { 862 attr.sa_mask = 0; 863 } else { 864 attr.sa_dosattr |= FILE_ATTRIBUTE_READONLY; 865 } 866 867 node->readonly_creator = NULL; 868 } 869 870 /* apply last_wtime if specified */ 871 if (last_wtime != 0 && last_wtime != 0xFFFFFFFF) { 872 attr.sa_vattr.va_mtime.tv_sec = 873 last_wtime + of->f_server->si_gmtoff; 874 attr.sa_mask |= SMB_AT_MTIME; 875 } 876 877 (void) smb_node_setattr(NULL, node, of->f_cr, of, &attr); 878 } 879 880 /* 881 * This function closes the file passed in (if appropriate) and returns the 882 * next open file in the list of open files of the tree of the open file passed 883 * in. It requires that the list of open files of the tree be entered in 884 * RW_READER mode before being called. 885 */ 886 static smb_ofile_t * 887 smb_ofile_close_and_next(smb_ofile_t *of) 888 { 889 smb_ofile_t *next_of; 890 smb_tree_t *tree; 891 892 ASSERT(of); 893 ASSERT(of->f_magic == SMB_OFILE_MAGIC); 894 895 mutex_enter(&of->f_mutex); 896 switch (of->f_state) { 897 case SMB_OFILE_STATE_OPEN: 898 /* The file is still open. */ 899 of->f_refcnt++; 900 ASSERT(of->f_refcnt); 901 tree = of->f_tree; 902 mutex_exit(&of->f_mutex); 903 smb_llist_exit(&of->f_tree->t_ofile_list); 904 smb_ofile_close(of, 0); 905 smb_ofile_release(of); 906 smb_llist_enter(&tree->t_ofile_list, RW_READER); 907 next_of = smb_llist_head(&tree->t_ofile_list); 908 break; 909 case SMB_OFILE_STATE_CLOSING: 910 case SMB_OFILE_STATE_CLOSED: 911 /* 912 * The ofile exists but is closed or 913 * in the process being closed. 914 */ 915 mutex_exit(&of->f_mutex); 916 next_of = smb_llist_next(&of->f_tree->t_ofile_list, of); 917 break; 918 default: 919 ASSERT(0); 920 mutex_exit(&of->f_mutex); 921 next_of = smb_llist_next(&of->f_tree->t_ofile_list, of); 922 break; 923 } 924 return (next_of); 925 } 926 927 /* 928 * Delete an ofile. 929 * 930 * Remove the ofile from the tree list before freeing resources 931 * associated with the odir. 932 */ 933 void 934 smb_ofile_delete(void *arg) 935 { 936 smb_tree_t *tree; 937 smb_ofile_t *of = (smb_ofile_t *)arg; 938 939 SMB_OFILE_VALID(of); 940 ASSERT(of->f_refcnt == 0); 941 ASSERT(of->f_state == SMB_OFILE_STATE_CLOSED); 942 943 tree = of->f_tree; 944 smb_llist_enter(&tree->t_ofile_list, RW_WRITER); 945 smb_llist_remove(&tree->t_ofile_list, of); 946 smb_idpool_free(&tree->t_fid_pool, of->f_fid); 947 atomic_dec_32(&tree->t_session->s_file_cnt); 948 smb_llist_exit(&tree->t_ofile_list); 949 950 mutex_enter(&of->f_mutex); 951 mutex_exit(&of->f_mutex); 952 953 if (of->f_ftype == SMB_FTYPE_MESG_PIPE) { 954 smb_opipe_dealloc(of->f_pipe); 955 of->f_pipe = NULL; 956 } else { 957 ASSERT(of->f_ftype == SMB_FTYPE_DISK); 958 ASSERT(of->f_node != NULL); 959 smb_node_rem_ofile(of->f_node, of); 960 smb_node_release(of->f_node); 961 } 962 963 of->f_magic = (uint32_t)~SMB_OFILE_MAGIC; 964 mutex_destroy(&of->f_mutex); 965 crfree(of->f_cr); 966 kmem_cache_free(of->f_tree->t_server->si_cache_ofile, of); 967 } 968 969 /* 970 * smb_ofile_access 971 * 972 * This function will check to see if the access requested is granted. 973 * Returns NT status codes. 974 */ 975 uint32_t 976 smb_ofile_access(smb_ofile_t *of, cred_t *cr, uint32_t access) 977 { 978 979 if ((of == NULL) || (cr == kcred)) 980 return (NT_STATUS_SUCCESS); 981 982 /* 983 * If the request is for something 984 * I don't grant it is an error 985 */ 986 if (~(of->f_granted_access) & access) { 987 if (!(of->f_granted_access & ACCESS_SYSTEM_SECURITY) && 988 (access & ACCESS_SYSTEM_SECURITY)) { 989 return (NT_STATUS_PRIVILEGE_NOT_HELD); 990 } 991 return (NT_STATUS_ACCESS_DENIED); 992 } 993 994 return (NT_STATUS_SUCCESS); 995 } 996 997 998 /* 999 * check file sharing rules for current open request 1000 * against existing open instances of the same file 1001 * 1002 * Returns NT_STATUS_SHARING_VIOLATION if there is any 1003 * sharing conflict, otherwise returns NT_STATUS_SUCCESS. 1004 */ 1005 uint32_t 1006 smb_ofile_open_check(smb_ofile_t *of, uint32_t desired_access, 1007 uint32_t share_access) 1008 { 1009 ASSERT(of->f_magic == SMB_OFILE_MAGIC); 1010 1011 mutex_enter(&of->f_mutex); 1012 1013 if (of->f_state != SMB_OFILE_STATE_OPEN) { 1014 mutex_exit(&of->f_mutex); 1015 return (NT_STATUS_INVALID_HANDLE); 1016 } 1017 1018 /* if it's just meta data */ 1019 if ((of->f_granted_access & FILE_DATA_ALL) == 0) { 1020 mutex_exit(&of->f_mutex); 1021 return (NT_STATUS_SUCCESS); 1022 } 1023 1024 /* 1025 * Check requested share access against the 1026 * open granted (desired) access 1027 */ 1028 if (SMB_DENY_DELETE(share_access) && (of->f_granted_access & DELETE)) { 1029 mutex_exit(&of->f_mutex); 1030 return (NT_STATUS_SHARING_VIOLATION); 1031 } 1032 1033 if (SMB_DENY_READ(share_access) && 1034 (of->f_granted_access & (FILE_READ_DATA | FILE_EXECUTE))) { 1035 mutex_exit(&of->f_mutex); 1036 return (NT_STATUS_SHARING_VIOLATION); 1037 } 1038 1039 if (SMB_DENY_WRITE(share_access) && 1040 (of->f_granted_access & (FILE_WRITE_DATA | FILE_APPEND_DATA))) { 1041 mutex_exit(&of->f_mutex); 1042 return (NT_STATUS_SHARING_VIOLATION); 1043 } 1044 1045 /* check requested desired access against the open share access */ 1046 if (SMB_DENY_DELETE(of->f_share_access) && (desired_access & DELETE)) { 1047 mutex_exit(&of->f_mutex); 1048 return (NT_STATUS_SHARING_VIOLATION); 1049 } 1050 1051 if (SMB_DENY_READ(of->f_share_access) && 1052 (desired_access & (FILE_READ_DATA | FILE_EXECUTE))) { 1053 mutex_exit(&of->f_mutex); 1054 return (NT_STATUS_SHARING_VIOLATION); 1055 } 1056 1057 if (SMB_DENY_WRITE(of->f_share_access) && 1058 (desired_access & (FILE_WRITE_DATA | FILE_APPEND_DATA))) { 1059 mutex_exit(&of->f_mutex); 1060 return (NT_STATUS_SHARING_VIOLATION); 1061 } 1062 1063 mutex_exit(&of->f_mutex); 1064 return (NT_STATUS_SUCCESS); 1065 } 1066 1067 /* 1068 * smb_ofile_rename_check 1069 * 1070 * An open file can be renamed if 1071 * 1072 * 1. isn't opened for data writing or deleting 1073 * 1074 * 2. Opened with "Deny Delete" share mode 1075 * But not opened for data reading or executing 1076 * (opened for accessing meta data) 1077 */ 1078 1079 uint32_t 1080 smb_ofile_rename_check(smb_ofile_t *of) 1081 { 1082 ASSERT(of->f_magic == SMB_OFILE_MAGIC); 1083 1084 mutex_enter(&of->f_mutex); 1085 1086 if (of->f_state != SMB_OFILE_STATE_OPEN) { 1087 mutex_exit(&of->f_mutex); 1088 return (NT_STATUS_INVALID_HANDLE); 1089 } 1090 1091 if (of->f_granted_access & 1092 (FILE_WRITE_DATA | FILE_APPEND_DATA | DELETE)) { 1093 mutex_exit(&of->f_mutex); 1094 return (NT_STATUS_SHARING_VIOLATION); 1095 } 1096 1097 if ((of->f_share_access & FILE_SHARE_DELETE) == 0) { 1098 if (of->f_granted_access & 1099 (FILE_READ_DATA | FILE_EXECUTE)) { 1100 mutex_exit(&of->f_mutex); 1101 return (NT_STATUS_SHARING_VIOLATION); 1102 } 1103 } 1104 1105 mutex_exit(&of->f_mutex); 1106 return (NT_STATUS_SUCCESS); 1107 } 1108 1109 /* 1110 * smb_ofile_delete_check 1111 * 1112 * An open file can be deleted only if opened for 1113 * accessing meta data. Share modes aren't important 1114 * in this case. 1115 * 1116 * NOTE: there is another mechanism for deleting an 1117 * open file that NT clients usually use. 1118 * That's setting "Delete on close" flag for an open 1119 * file. In this way the file will be deleted after 1120 * last close. This flag can be set by SmbTrans2SetFileInfo 1121 * with FILE_DISPOSITION_INFO information level. 1122 * For setting this flag, the file should be opened by 1123 * DELETE access in the FID that is passed in the Trans2 1124 * request. 1125 */ 1126 1127 uint32_t 1128 smb_ofile_delete_check(smb_ofile_t *of) 1129 { 1130 ASSERT(of->f_magic == SMB_OFILE_MAGIC); 1131 1132 mutex_enter(&of->f_mutex); 1133 1134 if (of->f_state != SMB_OFILE_STATE_OPEN) { 1135 mutex_exit(&of->f_mutex); 1136 return (NT_STATUS_INVALID_HANDLE); 1137 } 1138 1139 if (of->f_granted_access & 1140 (FILE_READ_DATA | FILE_WRITE_DATA | 1141 FILE_APPEND_DATA | FILE_EXECUTE | DELETE)) { 1142 mutex_exit(&of->f_mutex); 1143 return (NT_STATUS_SHARING_VIOLATION); 1144 } 1145 1146 mutex_exit(&of->f_mutex); 1147 return (NT_STATUS_SUCCESS); 1148 } 1149 1150 cred_t * 1151 smb_ofile_getcred(smb_ofile_t *of) 1152 { 1153 return (of->f_cr); 1154 } 1155 1156 /* 1157 * smb_ofile_set_delete_on_close 1158 * 1159 * Set the DeleteOnClose flag on the smb file. When the file is closed, 1160 * the flag will be transferred to the smb node, which will commit the 1161 * delete operation and inhibit subsequent open requests. 1162 * 1163 * When DeleteOnClose is set on an smb_node, the common open code will 1164 * reject subsequent open requests for the file. Observation of Windows 1165 * 2000 indicates that subsequent opens should be allowed (assuming 1166 * there would be no sharing violation) until the file is closed using 1167 * the fid on which the DeleteOnClose was requested. 1168 */ 1169 void 1170 smb_ofile_set_delete_on_close(smb_ofile_t *of) 1171 { 1172 mutex_enter(&of->f_mutex); 1173 of->f_flags |= SMB_OFLAGS_SET_DELETE_ON_CLOSE; 1174 mutex_exit(&of->f_mutex); 1175 } 1176 1177 /* 1178 * Encode open file information into a buffer; needed in user space to 1179 * support RPC requests. 1180 */ 1181 static int 1182 smb_ofile_netinfo_encode(smb_ofile_t *of, uint8_t *buf, size_t buflen, 1183 uint32_t *nbytes) 1184 { 1185 smb_netfileinfo_t fi; 1186 int rc; 1187 1188 rc = smb_ofile_netinfo_init(of, &fi); 1189 if (rc == 0) { 1190 rc = smb_netfileinfo_encode(&fi, buf, buflen, nbytes); 1191 smb_ofile_netinfo_fini(&fi); 1192 } 1193 1194 return (rc); 1195 } 1196 1197 static int 1198 smb_ofile_netinfo_init(smb_ofile_t *of, smb_netfileinfo_t *fi) 1199 { 1200 smb_user_t *user; 1201 smb_tree_t *tree; 1202 smb_node_t *node; 1203 char *path; 1204 char *buf; 1205 int rc; 1206 1207 ASSERT(of); 1208 user = of->f_user; 1209 tree = of->f_tree; 1210 ASSERT(user); 1211 ASSERT(tree); 1212 1213 buf = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 1214 1215 switch (of->f_ftype) { 1216 case SMB_FTYPE_DISK: 1217 node = of->f_node; 1218 ASSERT(node); 1219 1220 fi->fi_permissions = of->f_granted_access; 1221 fi->fi_numlocks = smb_lock_get_lock_count(node); 1222 1223 path = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 1224 1225 if (node != tree->t_snode) { 1226 rc = vnodetopath(tree->t_snode->vp, node->vp, path, 1227 MAXPATHLEN, kcred); 1228 if (rc == 0) 1229 (void) strsubst(path, '/', '\\'); 1230 else 1231 (void) strlcpy(path, node->od_name, MAXPATHLEN); 1232 } 1233 1234 (void) snprintf(buf, MAXPATHLEN, "%s:%s", tree->t_sharename, 1235 path); 1236 kmem_free(path, MAXPATHLEN); 1237 break; 1238 1239 case SMB_FTYPE_MESG_PIPE: 1240 ASSERT(of->f_pipe); 1241 1242 fi->fi_permissions = FILE_READ_DATA | FILE_WRITE_DATA | 1243 FILE_EXECUTE; 1244 fi->fi_numlocks = 0; 1245 (void) snprintf(buf, MAXPATHLEN, "\\PIPE\\%s", 1246 of->f_pipe->p_name); 1247 break; 1248 1249 default: 1250 kmem_free(buf, MAXPATHLEN); 1251 return (-1); 1252 } 1253 1254 fi->fi_fid = of->f_fid; 1255 fi->fi_uniqid = of->f_uniqid; 1256 fi->fi_pathlen = strlen(buf) + 1; 1257 fi->fi_path = smb_mem_strdup(buf); 1258 kmem_free(buf, MAXPATHLEN); 1259 1260 fi->fi_namelen = user->u_domain_len + user->u_name_len + 2; 1261 fi->fi_username = kmem_alloc(fi->fi_namelen, KM_SLEEP); 1262 (void) snprintf(fi->fi_username, fi->fi_namelen, "%s\\%s", 1263 user->u_domain, user->u_name); 1264 return (0); 1265 } 1266 1267 static void 1268 smb_ofile_netinfo_fini(smb_netfileinfo_t *fi) 1269 { 1270 if (fi == NULL) 1271 return; 1272 1273 if (fi->fi_path) 1274 smb_mem_free(fi->fi_path); 1275 if (fi->fi_username) 1276 kmem_free(fi->fi_username, fi->fi_namelen); 1277 1278 bzero(fi, sizeof (smb_netfileinfo_t)); 1279 } 1280 1281 /* 1282 * A query of user and group quotas may span multiple requests. 1283 * f_quota_resume is used to determine where the query should 1284 * be resumed, in a subsequent request. f_quota_resume contains 1285 * the SID of the last quota entry returned to the client. 1286 */ 1287 void 1288 smb_ofile_set_quota_resume(smb_ofile_t *ofile, char *resume) 1289 { 1290 ASSERT(ofile); 1291 mutex_enter(&ofile->f_mutex); 1292 if (resume == NULL) 1293 bzero(ofile->f_quota_resume, SMB_SID_STRSZ); 1294 else 1295 (void) strlcpy(ofile->f_quota_resume, resume, SMB_SID_STRSZ); 1296 mutex_exit(&ofile->f_mutex); 1297 } 1298 1299 void 1300 smb_ofile_get_quota_resume(smb_ofile_t *ofile, char *buf, int bufsize) 1301 { 1302 ASSERT(ofile); 1303 mutex_enter(&ofile->f_mutex); 1304 (void) strlcpy(buf, ofile->f_quota_resume, bufsize); 1305 mutex_exit(&ofile->f_mutex); 1306 } 1307