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 2009 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 * Odir State Machine 69 * ------------------ 70 * 71 * +-------------------------+ 72 * | SMB_ODIR_STATE_OPEN |<----------- open / creation 73 * +-------------------------+ 74 * | ^ 75 * | (first) | (last) 76 * | lookup | release 77 * v | 78 * +-------------------------+ 79 * | SMB_ODIR_STATE_IN_USE |---- 80 * +-------------------------+ | lookup / release / read 81 * | ^------- 82 * | close 83 * | 84 * v 85 * +-------------------------+ 86 * | SMB_ODIR_STATE_CLOSING |---- 87 * +-------------------------+ | close / release / read 88 * | ^------- 89 * | (last) release 90 * | 91 * v 92 * +-------------------------+ 93 * | SMB_ODIR_STATE_CLOSED |----------> deletion 94 * +-------------------------+ 95 * 96 * 97 * SMB_ODIR_STATE_OPEN 98 * - the odir exists in the list of odirs of its tree 99 * - lookup is valid in this state. It will place a hold on the odir 100 * by incrementing the reference count and the odir will transition 101 * to SMB_ODIR_STATE_IN_USE 102 * - read/close/release not valid in this state 103 * 104 * SMB_ODIR_STATE_IN_USE 105 * - the odir exists in the list of odirs of its tree. 106 * - lookup is valid in this state. It will place a hold on the odir 107 * by incrementing the reference count. 108 * - if the last hold is released the odir will transition 109 * back to SMB_ODIR_STATE_OPEN 110 * - if a close is received the odir will transition to 111 * SMB_ODIR_STATE_CLOSING. 112 * 113 * SMB_ODIR_STATE_CLOSING 114 * - the odir exists in the list of odirs of its tree. 115 * - lookup will fail in this state. 116 * - when the last hold is released the odir will transition 117 * to SMB_ODIR_STATE_CLOSED. 118 * 119 * SMB_ODIR_STATE_CLOSED 120 * - the odir exists in the list of odirs of its tree. 121 * - there are no users of the odir (refcnt == 0) 122 * - the odir is being removed from the tree's list and deleted. 123 * - lookup will fail in this state. 124 * - read/close/release not valid in this state 125 * 126 * Comments 127 * -------- 128 * The state machine of the odir structures is controlled by 3 elements: 129 * - The list of odirs of the tree it belongs to. 130 * - The mutex embedded in the structure itself. 131 * - The reference count. 132 * 133 * There's a mutex embedded in the odir structure used to protect its fields 134 * and there's a lock embedded in the list of odirs of a tree. To 135 * increment or to decrement the reference count the mutex must be entered. 136 * To insert the odir into the list of odirs of the tree and to remove 137 * the odir from it, the lock must be entered in RW_WRITER mode. 138 * 139 * In order to avoid deadlocks, when both (mutex and lock of the odir 140 * list) have to be entered, the lock must be entered first. 141 * 142 * 143 * Odir Interface 144 * --------------- 145 * odid = smb_odir_open(pathname) 146 * Create an odir representing the directory specified in pathname and 147 * add it into the tree's list of odirs. 148 * Return an identifier (odid) uniquely identifying the created odir. 149 * 150 * smb_odir_openat(smb_node_t *unode) 151 * Create an odir representing the extended attribute directory 152 * associated with the file (or directory) represented by unode 153 * and add it into the tree's list of odirs. 154 * Return an identifier (odid) uniquely identifying the created odir. 155 * 156 * smb_odir_t *odir = smb_tree_lookup_odir(odid) 157 * Find the odir corresponding to the specified odid in the tree's 158 * list of odirs. Place a hold on the odir. 159 * 160 * smb_odir_read(..., smb_odirent_t *odirent) 161 * Find the next directory entry in the odir and return it in odirent. 162 * 163 * smb_odir_read_fileinfo(..., smb_fileinfo_t *) 164 * Find the next directory entry in the odir. Return the details of 165 * the directory entry in smb_fileinfo_t. (See odir internals below) 166 * 167 * smb_odir_read_streaminfo(..., smb_streaminfo_t *) 168 * Find the next named stream entry in the odir. Return the details of 169 * the named stream in smb_streaminfo_t. 170 * 171 * smb_odir_close(smb_odir_t *odir) 172 * Close the odir. 173 * The caller of close must have a hold on the odir being closed. 174 * The hold should be released after closing. 175 * 176 * smb_odir_release(smb_odir_t *odir) 177 * Release the hold on the odir, obtained by lookup. 178 * 179 * 180 * Odir Internals 181 * -------------- 182 * The odir object represent an open directory search. Each read operation 183 * provides the caller with a structure containing information pertaining 184 * to the next directory entry that matches the search criteria, namely 185 * the filename or match pattern and, in the case of smb_odir_read_fileinfo(), 186 * the search attributes. 187 * 188 * The odir maintains a buffer (d_buf) of directory entries read from 189 * the filesystem via a vop_readdir. The buffer is populated when a read 190 * request (smb_odir_next_odirent) finds that the buffer is empty or that 191 * the end of the buffer has been reached, and also when a new client request 192 * (find next) begins. 193 * 194 * The data in d_buf (that which is returned from the file system) can 195 * be in one of two formats. If the file system supports extended directory 196 * entries we request that the data be returned as edirent_t structures. If 197 * it does not the data will be returned as dirent64_t structures. For 198 * convenience, when the next directory entry is read from d_buf by 199 * smb_odir_next_odirent it is translated into an smb_odirent_t. 200 * 201 * smb_odir_read_fileinfo 202 * The processing required to obtain the information to populate the caller's 203 * smb_fileinfo_t differs depending upon whether the directory search is for a 204 * single specified filename or for multiple files matching a search pattern. 205 * Thus smb_odir_read_fileinfo uses two static functions: 206 * smb_odir_single_fileinfo - obtains the smb_fileinfo_t info for the single 207 * filename as specified in smb_odir_open request. 208 * smb_odir_wildcard_fileinfo - obtains the smb_fileinfo_t info for the filename 209 * returned from the smb_odir_next_odirent. This is called in a loop until 210 * an entry matching the search criteria is found or no more entries exist. 211 * 212 * If a directory entry is a VLNK, the name returned in the smb_fileinfo_t 213 * is the name of the directory entry but the attributes are the attribites 214 * of the file that is the target of the link. If the link target cannot 215 * be found the attributes returned are the attributes of the link itself. 216 * 217 * smb_odir_read_streaminfo 218 * In order for an odir to provide information about stream files it 219 * must be opened with smb_odir_openat(). smb_odir_read_streaminfo() can 220 * then be used to obtain the name and size of named stream files. 221 * 222 * Resuming a Search 223 * ----------------- 224 * A directory search often consists of multiple client requests: an initial 225 * find_first request followed by zero or more find_next requests and a 226 * find_close request. 227 * The find_first request will open and lookup the odir, read its desired 228 * number of entries from the odir, then release the odir and return. 229 * A find_next request will lookup the odir and read its desired number of 230 * entries from the odir, then release the odir and return. 231 * At the end of the search the find_close request will close the odir. 232 * 233 * In order to be able to resume a directory search (find_next) the odir 234 * provides the capability for the caller to save one or more resume points 235 * (cookies) at the end of a request, and to specify which resume point 236 * (cookie) to restart from at the beginning of the next search. 237 * smb_odir_save_cookie(..., cookie) 238 * smb_odir_resume_at(smb_odir_resume_t *resume) 239 * A search can be resumed at a specified resume point (cookie), the resume 240 * point (cookie) stored at a specified index in the d_cookies array, or 241 * a specified filename. The latter (specified filename) is not yet supported. 242 * 243 * See smb_search, smb_find, smb_find_unique, and smb_trans2_find for details 244 */ 245 246 #include <smbsrv/smb_incl.h> 247 #include <smbsrv/smb_kproto.h> 248 #include <smbsrv/smb_fsops.h> 249 #include <sys/extdirent.h> 250 251 /* static functions */ 252 static smb_odir_t *smb_odir_create(smb_request_t *, smb_node_t *, 253 char *, uint16_t, cred_t *); 254 static void smb_odir_delete(smb_odir_t *); 255 static int smb_odir_single_fileinfo(smb_request_t *, smb_odir_t *, 256 smb_fileinfo_t *); 257 static int smb_odir_wildcard_fileinfo(smb_request_t *, smb_odir_t *, 258 smb_odirent_t *, smb_fileinfo_t *); 259 static int smb_odir_next_odirent(smb_odir_t *, smb_odirent_t *); 260 static boolean_t smb_odir_lookup_link(smb_request_t *, smb_odir_t *, 261 char *, smb_node_t **); 262 263 264 /* 265 * smb_odir_open 266 * 267 * Create an odir representing the directory specified in pathname. 268 * 269 * Returns: 270 * odid - Unique identifier of newly created odir. 271 * 0 - error, error details set in sr. 272 */ 273 uint16_t 274 smb_odir_open(smb_request_t *sr, char *path, uint16_t sattr, uint32_t flags) 275 { 276 int rc; 277 smb_tree_t *tree; 278 smb_node_t *dnode; 279 char pattern[MAXNAMELEN]; 280 smb_odir_t *od; 281 cred_t *cr; 282 283 ASSERT(sr); 284 ASSERT(sr->sr_magic == SMB_REQ_MAGIC); 285 ASSERT(sr->tid_tree); 286 ASSERT(sr->tid_tree->t_magic == SMB_TREE_MAGIC); 287 288 tree = sr->tid_tree; 289 290 rc = smb_pathname_reduce(sr, sr->user_cr, path, 291 tree->t_snode, tree->t_snode, &dnode, pattern); 292 if (rc != 0) { 293 smbsr_errno(sr, rc); 294 return (0); 295 } 296 297 if (dnode->vp->v_type != VDIR) { 298 smbsr_error(sr, NT_STATUS_OBJECT_PATH_NOT_FOUND, 299 ERRDOS, ERROR_PATH_NOT_FOUND); 300 smb_node_release(dnode); 301 return (0); 302 } 303 304 if (smb_fsop_access(sr, sr->user_cr, dnode, FILE_LIST_DIRECTORY) != 0) { 305 smbsr_error(sr, NT_STATUS_ACCESS_DENIED, 306 ERRDOS, ERROR_ACCESS_DENIED); 307 smb_node_release(dnode); 308 return (0); 309 } 310 311 if (flags & SMB_ODIR_OPENF_BACKUP_INTENT) 312 cr = smb_user_getprivcred(tree->t_user); 313 else 314 cr = tree->t_user->u_cred; 315 316 od = smb_odir_create(sr, dnode, pattern, sattr, cr); 317 smb_node_release(dnode); 318 return (od ? od->d_odid : 0); 319 } 320 321 /* 322 * smb_odir_openat 323 * 324 * Create an odir representing the extended attribute directory 325 * associated with the file (or directory) represented by unode. 326 * 327 * Returns: 328 * odid - Unique identifier of newly created odir. 329 * 0 - error, error details set in sr. 330 */ 331 uint16_t 332 smb_odir_openat(smb_request_t *sr, smb_node_t *unode) 333 { 334 int rc; 335 vnode_t *xattr_dvp; 336 smb_odir_t *od; 337 cred_t *cr; 338 char pattern[SMB_STREAM_PREFIX_LEN + 2]; 339 340 smb_node_t *xattr_dnode; 341 342 ASSERT(sr); 343 ASSERT(sr->sr_magic == SMB_REQ_MAGIC); 344 ASSERT(unode); 345 ASSERT(unode->n_magic == SMB_NODE_MAGIC); 346 347 if (SMB_TREE_CONTAINS_NODE(sr, unode) == 0 || 348 SMB_TREE_HAS_ACCESS(sr, ACE_LIST_DIRECTORY) == 0) { 349 smbsr_error(sr, NT_STATUS_ACCESS_DENIED, 350 ERRDOS, ERROR_ACCESS_DENIED); 351 return (0); 352 } 353 cr = kcred; 354 355 /* find the xattrdir vnode */ 356 rc = smb_vop_lookup_xattrdir(unode->vp, &xattr_dvp, LOOKUP_XATTR, cr); 357 if (rc != 0) { 358 smbsr_errno(sr, rc); 359 return (0); 360 } 361 362 /* lookup the xattrdir's smb_node */ 363 xattr_dnode = smb_node_lookup(sr, NULL, cr, xattr_dvp, XATTR_DIR, 364 unode, NULL); 365 VN_RELE(xattr_dvp); 366 if (xattr_dnode == NULL) { 367 smbsr_error(sr, NT_STATUS_NO_MEMORY, 368 ERRDOS, ERROR_NOT_ENOUGH_MEMORY); 369 return (0); 370 } 371 372 (void) snprintf(pattern, sizeof (pattern), "%s*", SMB_STREAM_PREFIX); 373 od = smb_odir_create(sr, xattr_dnode, pattern, SMB_SEARCH_ATTRIBUTES, 374 cr); 375 smb_node_release(xattr_dnode); 376 if (od == NULL) 377 return (0); 378 379 od->d_flags |= SMB_ODIR_FLAG_XATTR; 380 return (od->d_odid); 381 } 382 383 /* 384 * smb_odir_hold 385 * 386 * A hold will only be granted if the odir is open or in_use. 387 */ 388 boolean_t 389 smb_odir_hold(smb_odir_t *od) 390 { 391 ASSERT(od); 392 ASSERT(od->d_magic == SMB_ODIR_MAGIC); 393 394 mutex_enter(&od->d_mutex); 395 396 switch (od->d_state) { 397 case SMB_ODIR_STATE_OPEN: 398 od->d_refcnt++; 399 od->d_state = SMB_ODIR_STATE_IN_USE; 400 break; 401 case SMB_ODIR_STATE_IN_USE: 402 od->d_refcnt++; 403 break; 404 case SMB_ODIR_STATE_CLOSING: 405 case SMB_ODIR_STATE_CLOSED: 406 default: 407 mutex_exit(&od->d_mutex); 408 return (B_FALSE); 409 } 410 411 mutex_exit(&od->d_mutex); 412 return (B_TRUE); 413 } 414 415 /* 416 * smb_odir_release 417 * 418 * If the odir is in SMB_ODIR_STATE_CLOSING and this release 419 * results in a refcnt of 0, the odir may be removed from 420 * the tree's list of odirs and deleted. The odir's state is 421 * set to SMB_ODIR_STATE_CLOSED prior to exiting the mutex and 422 * deleting the odir. 423 */ 424 void 425 smb_odir_release(smb_odir_t *od) 426 { 427 ASSERT(od); 428 ASSERT(od->d_magic == SMB_ODIR_MAGIC); 429 430 mutex_enter(&od->d_mutex); 431 ASSERT(od->d_refcnt > 0); 432 433 switch (od->d_state) { 434 case SMB_ODIR_STATE_OPEN: 435 break; 436 case SMB_ODIR_STATE_IN_USE: 437 od->d_refcnt--; 438 if (od->d_refcnt == 0) 439 od->d_state = SMB_ODIR_STATE_OPEN; 440 break; 441 case SMB_ODIR_STATE_CLOSING: 442 od->d_refcnt--; 443 if (od->d_refcnt == 0) { 444 od->d_state = SMB_ODIR_STATE_CLOSED; 445 mutex_exit(&od->d_mutex); 446 smb_odir_delete(od); 447 return; 448 } 449 break; 450 case SMB_ODIR_STATE_CLOSED: 451 default: 452 break; 453 } 454 455 mutex_exit(&od->d_mutex); 456 } 457 458 /* 459 * smb_odir_close 460 */ 461 void 462 smb_odir_close(smb_odir_t *od) 463 { 464 ASSERT(od); 465 ASSERT(od->d_magic == SMB_ODIR_MAGIC); 466 467 mutex_enter(&od->d_mutex); 468 ASSERT(od->d_refcnt > 0); 469 switch (od->d_state) { 470 case SMB_ODIR_STATE_OPEN: 471 break; 472 case SMB_ODIR_STATE_IN_USE: 473 od->d_state = SMB_ODIR_STATE_CLOSING; 474 break; 475 case SMB_ODIR_STATE_CLOSING: 476 case SMB_ODIR_STATE_CLOSED: 477 default: 478 break; 479 } 480 mutex_exit(&od->d_mutex); 481 } 482 483 /* 484 * smb_odir_read 485 * 486 * Find the next directory entry matching the search pattern. 487 * No search attribute matching is performed. 488 * 489 * Returns: 490 * 0 - success. 491 * - If a matching entry was found eof will be B_FALSE and 492 * odirent will be populated. 493 * - If there are no matching entries eof will be B_TRUE. 494 * -1 - error, error details set in sr. 495 */ 496 int 497 smb_odir_read(smb_request_t *sr, smb_odir_t *od, 498 smb_odirent_t *odirent, boolean_t *eof) 499 { 500 int rc; 501 boolean_t ignore_case; 502 503 ASSERT(sr); 504 ASSERT(sr->sr_magic == SMB_REQ_MAGIC); 505 ASSERT(od); 506 ASSERT(od->d_magic == SMB_ODIR_MAGIC); 507 ASSERT(odirent); 508 509 mutex_enter(&od->d_mutex); 510 ASSERT(od->d_refcnt > 0); 511 512 switch (od->d_state) { 513 case SMB_ODIR_STATE_IN_USE: 514 case SMB_ODIR_STATE_CLOSING: 515 break; 516 case SMB_ODIR_STATE_OPEN: 517 case SMB_ODIR_STATE_CLOSED: 518 default: 519 mutex_exit(&od->d_mutex); 520 return (-1); 521 } 522 523 ignore_case = (od->d_flags & SMB_ODIR_FLAG_IGNORE_CASE); 524 525 for (;;) { 526 if ((rc = smb_odir_next_odirent(od, odirent)) != 0) 527 break; 528 if (smb_match_name(odirent->od_ino, odirent->od_name, 529 od->d_pattern, ignore_case)) 530 break; 531 } 532 533 mutex_exit(&od->d_mutex); 534 535 switch (rc) { 536 case 0: 537 *eof = B_FALSE; 538 return (0); 539 case ENOENT: 540 *eof = B_TRUE; 541 return (0); 542 default: 543 smbsr_errno(sr, rc); 544 return (-1); 545 } 546 } 547 548 /* 549 * smb_odir_read_fileinfo 550 * 551 * Find the next directory entry matching the search pattern 552 * and attributes: od->d_pattern and od->d_sattr. 553 * 554 * If the search pattern specifies a single filename call 555 * smb_odir_single_fileinfo to get the file attributes and 556 * populate the caller's smb_fileinfo_t. 557 * 558 * If the search pattern contains wildcards call smb_odir_next_odirent 559 * to get the next directory entry then. Repeat until a matching 560 * filename is found. Call smb_odir_wildcard_fileinfo to get the 561 * file attributes and populate the caller's smb_fileinfo_t. 562 * This is repeated until a file matching the search criteria is found. 563 * 564 * Returns: 565 * 0 - success. 566 * - If a matching entry was found eof will be B_FALSE and 567 * fileinfo will be populated. 568 * - If there are no matching entries eof will be B_TRUE. 569 * -1 - error, error details set in sr. 570 */ 571 int 572 smb_odir_read_fileinfo(smb_request_t *sr, smb_odir_t *od, 573 smb_fileinfo_t *fileinfo, boolean_t *eof) 574 { 575 int rc; 576 smb_odirent_t *odirent; 577 boolean_t ignore_case; 578 579 ASSERT(sr); 580 ASSERT(sr->sr_magic == SMB_REQ_MAGIC); 581 ASSERT(od); 582 ASSERT(od->d_magic == SMB_ODIR_MAGIC); 583 ASSERT(fileinfo); 584 585 mutex_enter(&od->d_mutex); 586 ASSERT(od->d_refcnt > 0); 587 588 switch (od->d_state) { 589 case SMB_ODIR_STATE_IN_USE: 590 case SMB_ODIR_STATE_CLOSING: 591 break; 592 case SMB_ODIR_STATE_OPEN: 593 case SMB_ODIR_STATE_CLOSED: 594 default: 595 mutex_exit(&od->d_mutex); 596 return (-1); 597 } 598 599 ignore_case = (od->d_flags & SMB_ODIR_FLAG_IGNORE_CASE); 600 601 if (!(od->d_flags & SMB_ODIR_FLAG_WILDCARDS)) { 602 if (od->d_eof) 603 rc = ENOENT; 604 else 605 rc = smb_odir_single_fileinfo(sr, od, fileinfo); 606 od->d_eof = B_TRUE; 607 } else { 608 odirent = kmem_alloc(sizeof (smb_odirent_t), KM_SLEEP); 609 for (;;) { 610 bzero(fileinfo, sizeof (smb_fileinfo_t)); 611 if ((rc = smb_odir_next_odirent(od, odirent)) != 0) 612 break; 613 614 if (!smb_match_name(odirent->od_ino, odirent->od_name, 615 od->d_pattern, ignore_case)) 616 continue; 617 618 rc = smb_odir_wildcard_fileinfo(sr, od, odirent, 619 fileinfo); 620 if (rc == 0) 621 break; 622 } 623 kmem_free(odirent, sizeof (smb_odirent_t)); 624 } 625 mutex_exit(&od->d_mutex); 626 627 switch (rc) { 628 case 0: 629 *eof = B_FALSE; 630 return (0); 631 case ENOENT: 632 *eof = B_TRUE; 633 return (0); 634 default: 635 smbsr_errno(sr, rc); 636 return (-1); 637 } 638 } 639 640 641 /* 642 * smb_odir_read_streaminfo 643 * 644 * Find the next directory entry whose name begins with SMB_STREAM_PREFIX, 645 * and thus represents an NTFS named stream. 646 * No search attribute matching is performed. 647 * No case conflict name mangling is required for NTFS named stream names. 648 * 649 * Returns: 650 * 0 - success. 651 * - If a matching entry was found eof will be B_FALSE and 652 * sinfo will be populated. 653 * - If there are no matching entries eof will be B_TRUE. 654 * -1 - error, error details set in sr. 655 */ 656 int 657 smb_odir_read_streaminfo(smb_request_t *sr, smb_odir_t *od, 658 smb_streaminfo_t *sinfo, boolean_t *eof) 659 { 660 int rc; 661 smb_odirent_t *odirent; 662 vnode_t *vp; 663 smb_attr_t attr; 664 int tmpflg; 665 666 ASSERT(sr); 667 ASSERT(sr->sr_magic == SMB_REQ_MAGIC); 668 ASSERT(od); 669 ASSERT(od->d_magic == SMB_ODIR_MAGIC); 670 ASSERT(sinfo); 671 672 mutex_enter(&od->d_mutex); 673 ASSERT(od->d_refcnt > 0); 674 675 switch (od->d_state) { 676 case SMB_ODIR_STATE_IN_USE: 677 case SMB_ODIR_STATE_CLOSING: 678 break; 679 case SMB_ODIR_STATE_OPEN: 680 case SMB_ODIR_STATE_CLOSED: 681 default: 682 mutex_exit(&od->d_mutex); 683 return (-1); 684 } 685 686 /* Check that odir represents an xattr directory */ 687 if (!(od->d_flags & SMB_ODIR_FLAG_XATTR)) { 688 *eof = B_TRUE; 689 mutex_exit(&od->d_mutex); 690 return (0); 691 } 692 693 odirent = kmem_alloc(sizeof (smb_odirent_t), KM_SLEEP); 694 695 for (;;) { 696 bzero(sinfo, sizeof (smb_streaminfo_t)); 697 if ((rc = smb_odir_next_odirent(od, odirent)) != 0) 698 break; 699 700 if (strncmp(odirent->od_name, SMB_STREAM_PREFIX, 701 SMB_STREAM_PREFIX_LEN)) { 702 continue; 703 } 704 705 /* 706 * since we only care about the size attributes we don't need 707 * to pass the vp of the unnamed stream file to smb_vop_getattr 708 */ 709 rc = smb_vop_lookup(od->d_dnode->vp, odirent->od_name, &vp, 710 NULL, 0, &tmpflg, od->d_tree->t_snode->vp, od->d_cred); 711 if (rc == 0) { 712 rc = smb_vop_getattr(vp, NULL, &attr, 0, od->d_cred); 713 VN_RELE(vp); 714 } 715 716 if (rc == 0) { 717 (void) strlcpy(sinfo->si_name, 718 odirent->od_name + SMB_STREAM_PREFIX_LEN, 719 sizeof (sinfo->si_name)); 720 sinfo->si_size = attr.sa_vattr.va_size; 721 sinfo->si_alloc_size = 722 attr.sa_vattr.va_nblocks * DEV_BSIZE; 723 break; 724 } 725 } 726 mutex_exit(&od->d_mutex); 727 728 kmem_free(odirent, sizeof (smb_odirent_t)); 729 730 switch (rc) { 731 case 0: 732 *eof = B_FALSE; 733 return (0); 734 case ENOENT: 735 *eof = B_TRUE; 736 return (0); 737 default: 738 smbsr_errno(sr, rc); 739 return (-1); 740 } 741 } 742 743 /* 744 * smb_odir_save_cookie 745 * 746 * Callers can save up to SMB_MAX_SEARCH cookies in the odir 747 * to be used as resume points for a 'find next' request. 748 */ 749 void 750 smb_odir_save_cookie(smb_odir_t *od, int idx, uint32_t cookie) 751 { 752 ASSERT(od); 753 ASSERT(od->d_magic == SMB_ODIR_MAGIC); 754 ASSERT(idx >= 0 && idx < SMB_MAX_SEARCH); 755 756 mutex_enter(&od->d_mutex); 757 od->d_cookies[idx] = cookie; 758 mutex_exit(&od->d_mutex); 759 } 760 761 /* 762 * smb_odir_resume_at 763 * 764 * If SMB_ODIR_FLAG_WILDCARDS is not set the search is for a single 765 * file and should not be resumed. 766 * 767 * Wildcard searching can be resumed from: 768 * - the cookie saved at a specified index (SMBsearch, SMBfind). 769 * - a specified cookie (SMB_trans2_find) 770 * - a specified filename (SMB_trans2_find) - NOT SUPPORTED. 771 * Defaults to continuing from where the last search ended. 772 * 773 * Continuation from where the last search ended (SMB_trans2_find) 774 * is implemented by saving the last cookie at a specific index (0) 775 * smb_odir_resume_at indicates a new request, so reset od->d_bufptr 776 * and d_eof to force a vop_readdir. 777 */ 778 void 779 smb_odir_resume_at(smb_odir_t *od, smb_odir_resume_t *resume) 780 { 781 ASSERT(od); 782 ASSERT(od->d_magic == SMB_ODIR_MAGIC); 783 ASSERT(resume); 784 785 mutex_enter(&od->d_mutex); 786 787 if ((od->d_flags & SMB_ODIR_FLAG_WILDCARDS) == 0) { 788 od->d_eof = B_TRUE; 789 mutex_exit(&od->d_mutex); 790 return; 791 } 792 793 switch (resume->or_type) { 794 case SMB_ODIR_RESUME_IDX: 795 ASSERT(resume->or_idx >= 0); 796 ASSERT(resume->or_idx < SMB_MAX_SEARCH); 797 798 if ((resume->or_idx < 0) || 799 (resume->or_idx >= SMB_MAX_SEARCH)) { 800 resume->or_idx = 0; 801 } 802 od->d_offset = od->d_cookies[resume->or_idx]; 803 break; 804 case SMB_ODIR_RESUME_COOKIE: 805 od->d_offset = resume->or_cookie; 806 break; 807 case SMB_ODIR_RESUME_FNAME: 808 default: 809 od->d_offset = od->d_cookies[0]; 810 break; 811 } 812 813 /* Force a vop_readdir to refresh d_buf */ 814 od->d_bufptr = NULL; 815 od->d_eof = B_FALSE; 816 817 mutex_exit(&od->d_mutex); 818 } 819 820 821 /* *** static functions *** */ 822 823 /* 824 * smb_odir_create 825 * Allocate and populate an odir obect and add it to the tree's list. 826 */ 827 static smb_odir_t * 828 smb_odir_create(smb_request_t *sr, smb_node_t *dnode, 829 char *pattern, uint16_t sattr, cred_t *cr) 830 { 831 smb_odir_t *od; 832 smb_tree_t *tree; 833 uint16_t odid; 834 835 ASSERT(sr); 836 ASSERT(sr->sr_magic == SMB_REQ_MAGIC); 837 ASSERT(sr->tid_tree); 838 ASSERT(sr->tid_tree->t_magic == SMB_TREE_MAGIC); 839 ASSERT(dnode); 840 ASSERT(dnode->n_magic == SMB_NODE_MAGIC); 841 842 tree = sr->tid_tree; 843 844 if (smb_idpool_alloc(&tree->t_odid_pool, &odid)) { 845 smbsr_error(sr, NT_STATUS_TOO_MANY_OPENED_FILES, 846 ERRDOS, ERROR_TOO_MANY_OPEN_FILES); 847 return (NULL); 848 } 849 850 od = kmem_cache_alloc(tree->t_server->si_cache_odir, KM_SLEEP); 851 bzero(od, sizeof (smb_odir_t)); 852 853 mutex_init(&od->d_mutex, NULL, MUTEX_DEFAULT, NULL); 854 od->d_refcnt = 0; 855 od->d_state = SMB_ODIR_STATE_OPEN; 856 od->d_magic = SMB_ODIR_MAGIC; 857 od->d_opened_by_pid = sr->smb_pid; 858 od->d_session = tree->t_session; 859 od->d_cred = cr; 860 od->d_tree = tree; 861 od->d_dnode = dnode; 862 smb_node_ref(dnode); 863 od->d_odid = odid; 864 od->d_sattr = sattr; 865 (void) strlcpy(od->d_pattern, pattern, sizeof (od->d_pattern)); 866 od->d_flags = 0; 867 if (smb_convert_wildcards(od->d_pattern) != 0) 868 od->d_flags |= SMB_ODIR_FLAG_WILDCARDS; 869 if (vfs_has_feature(dnode->vp->v_vfsp, VFSFT_DIRENTFLAGS)) 870 od->d_flags |= SMB_ODIR_FLAG_EDIRENT; 871 if (smb_tree_has_feature(tree, SMB_TREE_CASEINSENSITIVE)) 872 od->d_flags |= SMB_ODIR_FLAG_IGNORE_CASE; 873 if (SMB_TREE_SUPPORTS_CATIA(sr)) 874 od->d_flags |= SMB_ODIR_FLAG_CATIA; 875 od->d_eof = B_FALSE; 876 877 smb_llist_enter(&tree->t_odir_list, RW_WRITER); 878 smb_llist_insert_tail(&tree->t_odir_list, od); 879 smb_llist_exit(&tree->t_odir_list); 880 881 atomic_inc_32(&tree->t_session->s_dir_cnt); 882 return (od); 883 } 884 885 /* 886 * smb_odir_delete 887 * 888 * Removal of the odir from the tree's list of odirs must be 889 * done before any resources associated with the odir are 890 * released. 891 */ 892 static void 893 smb_odir_delete(smb_odir_t *od) 894 { 895 ASSERT(od); 896 ASSERT(od->d_magic == SMB_ODIR_MAGIC); 897 ASSERT(od->d_state == SMB_ODIR_STATE_CLOSED); 898 899 smb_llist_enter(&od->d_tree->t_odir_list, RW_WRITER); 900 smb_llist_remove(&od->d_tree->t_odir_list, od); 901 smb_llist_exit(&od->d_tree->t_odir_list); 902 903 od->d_magic = 0; 904 atomic_dec_32(&od->d_tree->t_session->s_dir_cnt); 905 smb_node_release(od->d_dnode); 906 smb_idpool_free(&od->d_tree->t_odid_pool, od->d_odid); 907 mutex_destroy(&od->d_mutex); 908 kmem_cache_free(od->d_tree->t_server->si_cache_odir, od); 909 } 910 911 /* 912 * smb_odir_next_odirent 913 * 914 * Find the next directory entry in d_buf. If d_bufptr is NULL (buffer 915 * is empty or we've reached the end of it), read the next set of 916 * entries from the file system (vop_readdir). 917 * 918 * File systems which support VFSFT_EDIRENT_FLAGS will return the 919 * directory entries as a buffer of edirent_t structure. Others will 920 * return a buffer of dirent64_t structures. For simplicity translate 921 * the data into an smb_odirent_t structure. 922 * The ed_name/d_name in d_buf is NULL terminated by the file system. 923 * 924 * Some file systems can have directories larger than SMB_MAXDIRSIZE. 925 * If the odirent offset >= SMB_MAXDIRSIZE return ENOENT and set d_eof 926 * to true to stop subsequent calls to smb_vop_readdir. 927 * 928 * Returns: 929 * 0 - success. odirent is populated with the next directory entry 930 * ENOENT - no more directory entries 931 * errno - error 932 */ 933 static int 934 smb_odir_next_odirent(smb_odir_t *od, smb_odirent_t *odirent) 935 { 936 int rc; 937 int reclen; 938 int eof; 939 dirent64_t *dp; 940 edirent_t *edp; 941 char *np; 942 943 ASSERT(MUTEX_HELD(&od->d_mutex)); 944 945 bzero(odirent, sizeof (smb_odirent_t)); 946 947 if (od->d_bufptr != NULL) { 948 if (od->d_flags & SMB_ODIR_FLAG_EDIRENT) 949 reclen = od->d_edp->ed_reclen; 950 else 951 reclen = od->d_dp->d_reclen; 952 953 if (reclen == 0) { 954 od->d_bufptr = NULL; 955 } else { 956 od->d_bufptr += reclen; 957 if (od->d_bufptr >= od->d_buf + od->d_bufsize) 958 od->d_bufptr = NULL; 959 } 960 } 961 962 if (od->d_bufptr == NULL) { 963 if (od->d_eof) 964 return (ENOENT); 965 966 od->d_bufsize = sizeof (od->d_buf); 967 968 rc = smb_vop_readdir(od->d_dnode->vp, od->d_offset, 969 od->d_buf, &od->d_bufsize, &eof, od->d_cred); 970 971 if ((rc == 0) && (od->d_bufsize == 0)) 972 rc = ENOENT; 973 974 if (rc != 0) { 975 od->d_bufptr = NULL; 976 od->d_bufsize = 0; 977 return (rc); 978 } 979 980 od->d_eof = (eof != 0); 981 od->d_bufptr = od->d_buf; 982 } 983 984 if (od->d_flags & SMB_ODIR_FLAG_EDIRENT) 985 od->d_offset = od->d_edp->ed_off; 986 else 987 od->d_offset = od->d_dp->d_off; 988 989 if (od->d_offset >= SMB_MAXDIRSIZE) { 990 od->d_bufptr = NULL; 991 od->d_bufsize = 0; 992 od->d_eof = B_TRUE; 993 return (ENOENT); 994 } 995 996 if (od->d_flags & SMB_ODIR_FLAG_EDIRENT) { 997 edp = od->d_edp; 998 odirent->od_ino = edp->ed_ino; 999 odirent->od_eflags = edp->ed_eflags; 1000 np = edp->ed_name; 1001 } else { 1002 dp = od->d_dp; 1003 odirent->od_ino = dp->d_ino; 1004 odirent->od_eflags = 0; 1005 np = dp->d_name; 1006 } 1007 1008 if ((od->d_flags & SMB_ODIR_FLAG_CATIA) && 1009 ((od->d_flags & SMB_ODIR_FLAG_XATTR) == 0)) { 1010 smb_vop_catia_v4tov5(np, odirent->od_name, 1011 sizeof (odirent->od_name)); 1012 } else { 1013 (void) strlcpy(odirent->od_name, np, 1014 sizeof (odirent->od_name)); 1015 } 1016 1017 return (0); 1018 } 1019 1020 /* 1021 * smb_odir_single_fileinfo 1022 * 1023 * Lookup the file identified by od->d_pattern. 1024 * 1025 * If the looked up file is a link, we attempt to lookup the link target 1026 * to use its attributes in place of those of the files's. 1027 * If we fail to lookup the target of the link we use the original 1028 * file's attributes. 1029 * Check if the attributes match the search attributes. 1030 * 1031 * Returns: 0 - success 1032 * ENOENT - no match 1033 * errno - error 1034 */ 1035 static int 1036 smb_odir_single_fileinfo(smb_request_t *sr, smb_odir_t *od, 1037 smb_fileinfo_t *fileinfo) 1038 { 1039 int rc; 1040 smb_node_t *fnode, *tgt_node; 1041 smb_attr_t attr; 1042 ino64_t ino; 1043 char *name; 1044 boolean_t case_conflict = B_FALSE; 1045 int lookup_flags, flags = 0; 1046 vnode_t *vp; 1047 1048 ASSERT(sr); 1049 ASSERT(sr->sr_magic == SMB_REQ_MAGIC); 1050 ASSERT(od); 1051 ASSERT(od->d_magic == SMB_ODIR_MAGIC); 1052 1053 ASSERT(MUTEX_HELD(&od->d_mutex)); 1054 bzero(fileinfo, sizeof (smb_fileinfo_t)); 1055 1056 rc = smb_fsop_lookup(sr, od->d_cred, 0, od->d_tree->t_snode, 1057 od->d_dnode, od->d_pattern, &fnode); 1058 if (rc != 0) 1059 return (rc); 1060 1061 /* 1062 * If case sensitive, do a case insensitive smb_vop_lookup to 1063 * check for case conflict 1064 */ 1065 if (od->d_flags & SMB_ODIR_FLAG_IGNORE_CASE) { 1066 lookup_flags = SMB_IGNORE_CASE; 1067 if (od->d_flags & SMB_ODIR_FLAG_CATIA) 1068 lookup_flags |= SMB_CATIA; 1069 1070 rc = smb_vop_lookup(od->d_dnode->vp, fnode->od_name, &vp, 1071 NULL, lookup_flags, &flags, od->d_tree->t_snode->vp, 1072 od->d_cred); 1073 if (rc != 0) 1074 return (rc); 1075 VN_RELE(vp); 1076 1077 if (flags & ED_CASE_CONFLICT) 1078 case_conflict = B_TRUE; 1079 } 1080 1081 if ((rc = smb_node_getattr(sr, fnode, &attr)) != 0) { 1082 smb_node_release(fnode); 1083 return (rc); 1084 } 1085 1086 ino = attr.sa_vattr.va_nodeid; 1087 (void) smb_mangle_name(ino, fnode->od_name, 1088 fileinfo->fi_shortname, fileinfo->fi_name83, case_conflict); 1089 name = (case_conflict) ? fileinfo->fi_shortname : fnode->od_name; 1090 (void) strlcpy(fileinfo->fi_name, name, sizeof (fileinfo->fi_name)); 1091 1092 /* follow link to get target node & attr */ 1093 if ((fnode->vp->v_type == VLNK) && 1094 (smb_odir_lookup_link(sr, od, fnode->od_name, &tgt_node))) { 1095 smb_node_release(fnode); 1096 fnode = tgt_node; 1097 if ((rc = smb_node_getattr(sr, fnode, &attr)) != 0) { 1098 smb_node_release(fnode); 1099 return (rc); 1100 } 1101 } 1102 1103 /* check search attributes */ 1104 if (!smb_sattr_check(attr.sa_dosattr, od->d_sattr)) { 1105 smb_node_release(fnode); 1106 return (ENOENT); 1107 } 1108 1109 fileinfo->fi_dosattr = attr.sa_dosattr; 1110 fileinfo->fi_nodeid = attr.sa_vattr.va_nodeid; 1111 fileinfo->fi_size = attr.sa_vattr.va_size; 1112 fileinfo->fi_alloc_size = attr.sa_vattr.va_nblocks * DEV_BSIZE; 1113 fileinfo->fi_atime = attr.sa_vattr.va_atime; 1114 fileinfo->fi_mtime = attr.sa_vattr.va_mtime; 1115 fileinfo->fi_ctime = attr.sa_vattr.va_ctime; 1116 if (attr.sa_crtime.tv_sec) 1117 fileinfo->fi_crtime = attr.sa_crtime; 1118 else 1119 fileinfo->fi_crtime = attr.sa_vattr.va_mtime; 1120 1121 smb_node_release(fnode); 1122 return (0); 1123 } 1124 1125 /* 1126 * smb_odir_wildcard_fileinfo 1127 * 1128 * odirent contains a directory entry, obtained from a vop_readdir. 1129 * If a case conflict is identified the filename is mangled and the 1130 * shortname is used as 'name', in place of odirent->od_name. This 1131 * name will be used in the smb_fsop_lookup because smb_fsop_lookup 1132 * performs a case insensitive lookup if the tree is case insesitive, 1133 * so the mangled name is required in the case conflict scenario to 1134 * ensure the correct match. 1135 * 1136 * If the looked up file is a link, we attempt to lookup the link target 1137 * to use its attributes in place of those of the files's. 1138 * If we fail to lookup the target of the link we use the original 1139 * file's attributes. 1140 * Check if the attributes match the search attributes. 1141 * 1142 * Although some file systems can have directories larger than 1143 * SMB_MAXDIRSIZE smb_odir_next_odirent ensures that no offset larger 1144 * than SMB_MAXDIRSIZE is returned. It is therefore safe to use the 1145 * offset as the cookie (uint32_t). 1146 * 1147 * Returns: 0 - success 1148 * ENOENT - no match, proceed to next entry 1149 * errno - error 1150 */ 1151 static int 1152 smb_odir_wildcard_fileinfo(smb_request_t *sr, smb_odir_t *od, 1153 smb_odirent_t *odirent, smb_fileinfo_t *fileinfo) 1154 { 1155 int rc; 1156 smb_node_t *fnode, *tgt_node; 1157 smb_attr_t attr; 1158 char *name; 1159 boolean_t case_conflict; 1160 1161 ASSERT(sr); 1162 ASSERT(sr->sr_magic == SMB_REQ_MAGIC); 1163 ASSERT(od); 1164 ASSERT(od->d_magic == SMB_ODIR_MAGIC); 1165 1166 ASSERT(MUTEX_HELD(&od->d_mutex)); 1167 bzero(fileinfo, sizeof (smb_fileinfo_t)); 1168 1169 case_conflict = ((od->d_flags & SMB_ODIR_FLAG_IGNORE_CASE) && 1170 (odirent->od_eflags & ED_CASE_CONFLICT)); 1171 (void) smb_mangle_name(odirent->od_ino, odirent->od_name, 1172 fileinfo->fi_shortname, fileinfo->fi_name83, case_conflict); 1173 name = (case_conflict) ? fileinfo->fi_shortname : odirent->od_name; 1174 (void) strlcpy(fileinfo->fi_name, name, sizeof (fileinfo->fi_name)); 1175 1176 rc = smb_fsop_lookup(sr, od->d_cred, 0, od->d_tree->t_snode, 1177 od->d_dnode, name, &fnode); 1178 if (rc != 0) 1179 return (rc); 1180 1181 /* follow link to get target node & attr */ 1182 if ((fnode->vp->v_type == VLNK) && 1183 (smb_odir_lookup_link(sr, od, name, &tgt_node))) { 1184 smb_node_release(fnode); 1185 fnode = tgt_node; 1186 } 1187 1188 if ((rc = smb_node_getattr(sr, fnode, &attr)) != 0) { 1189 smb_node_release(fnode); 1190 return (rc); 1191 } 1192 1193 /* check search attributes */ 1194 if (!smb_sattr_check(attr.sa_dosattr, od->d_sattr)) { 1195 smb_node_release(fnode); 1196 return (ENOENT); 1197 } 1198 1199 fileinfo->fi_cookie = (uint32_t)od->d_offset; 1200 fileinfo->fi_dosattr = attr.sa_dosattr; 1201 fileinfo->fi_nodeid = attr.sa_vattr.va_nodeid; 1202 fileinfo->fi_size = attr.sa_vattr.va_size; 1203 fileinfo->fi_alloc_size = attr.sa_vattr.va_nblocks * DEV_BSIZE; 1204 fileinfo->fi_atime = attr.sa_vattr.va_atime; 1205 fileinfo->fi_mtime = attr.sa_vattr.va_mtime; 1206 fileinfo->fi_ctime = attr.sa_vattr.va_ctime; 1207 if (attr.sa_crtime.tv_sec) 1208 fileinfo->fi_crtime = attr.sa_crtime; 1209 else 1210 fileinfo->fi_crtime = attr.sa_vattr.va_mtime; 1211 1212 smb_node_release(fnode); 1213 return (0); 1214 } 1215 1216 /* 1217 * smb_odir_lookup_link 1218 * 1219 * If the file is a symlink we lookup the object to which the 1220 * symlink refers so that we can return its attributes. 1221 * This can cause a problem if a symlink in a sub-directory 1222 * points to a parent directory (some UNIX GUI's create a symlink 1223 * in $HOME/.desktop that points to the user's home directory). 1224 * Some Windows applications (e.g. virus scanning) loop/hang 1225 * trying to follow this recursive path and there is little 1226 * we can do because the path is constructed on the client. 1227 * smb_dirsymlink_enable allows an end-user to disable 1228 * symlinks to directories. Symlinks to other object types 1229 * should be unaffected. 1230 * 1231 * Returns: B_TRUE - followed link. tgt_node and tgt_attr set 1232 * B_FALSE - link not followed 1233 */ 1234 static boolean_t 1235 smb_odir_lookup_link(smb_request_t *sr, smb_odir_t *od, 1236 char *fname, smb_node_t **tgt_node) 1237 { 1238 int rc; 1239 1240 rc = smb_fsop_lookup(sr, od->d_cred, SMB_FOLLOW_LINKS, 1241 od->d_tree->t_snode, od->d_dnode, fname, tgt_node); 1242 if (rc != 0) { 1243 *tgt_node = NULL; 1244 return (B_FALSE); 1245 } 1246 1247 if (smb_node_is_dir(*tgt_node) && (!smb_dirsymlink_enable)) { 1248 smb_node_release(*tgt_node); 1249 *tgt_node = NULL; 1250 return (B_FALSE); 1251 } 1252 1253 return (B_TRUE); 1254 } 1255