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_kproto.h> 247 #include <smbsrv/smb_fsops.h> 248 #include <smbsrv/smb_share.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 smb_convert_wildcards(path); 291 292 rc = smb_pathname_reduce(sr, sr->user_cr, path, 293 tree->t_snode, tree->t_snode, &dnode, pattern); 294 if (rc != 0) { 295 smbsr_errno(sr, rc); 296 return (0); 297 } 298 299 if (dnode->vp->v_type != VDIR) { 300 smbsr_error(sr, NT_STATUS_OBJECT_PATH_NOT_FOUND, 301 ERRDOS, ERROR_PATH_NOT_FOUND); 302 smb_node_release(dnode); 303 return (0); 304 } 305 306 if (smb_fsop_access(sr, sr->user_cr, dnode, FILE_LIST_DIRECTORY) != 0) { 307 smbsr_error(sr, NT_STATUS_ACCESS_DENIED, 308 ERRDOS, ERROR_ACCESS_DENIED); 309 smb_node_release(dnode); 310 return (0); 311 } 312 313 if (flags & SMB_ODIR_OPENF_BACKUP_INTENT) 314 cr = smb_user_getprivcred(tree->t_user); 315 else 316 cr = tree->t_user->u_cred; 317 318 od = smb_odir_create(sr, dnode, pattern, sattr, cr); 319 smb_node_release(dnode); 320 return (od ? od->d_odid : 0); 321 } 322 323 /* 324 * smb_odir_openat 325 * 326 * Create an odir representing the extended attribute directory 327 * associated with the file (or directory) represented by unode. 328 * 329 * Returns: 330 * odid - Unique identifier of newly created odir. 331 * 0 - error, error details set in sr. 332 */ 333 uint16_t 334 smb_odir_openat(smb_request_t *sr, smb_node_t *unode) 335 { 336 int rc; 337 vnode_t *xattr_dvp; 338 smb_odir_t *od; 339 cred_t *cr; 340 char pattern[SMB_STREAM_PREFIX_LEN + 2]; 341 342 smb_node_t *xattr_dnode; 343 344 ASSERT(sr); 345 ASSERT(sr->sr_magic == SMB_REQ_MAGIC); 346 ASSERT(unode); 347 ASSERT(unode->n_magic == SMB_NODE_MAGIC); 348 349 if (SMB_TREE_CONTAINS_NODE(sr, unode) == 0 || 350 SMB_TREE_HAS_ACCESS(sr, ACE_LIST_DIRECTORY) == 0) { 351 smbsr_error(sr, NT_STATUS_ACCESS_DENIED, 352 ERRDOS, ERROR_ACCESS_DENIED); 353 return (0); 354 } 355 cr = kcred; 356 357 /* find the xattrdir vnode */ 358 rc = smb_vop_lookup_xattrdir(unode->vp, &xattr_dvp, LOOKUP_XATTR, cr); 359 if (rc != 0) { 360 smbsr_errno(sr, rc); 361 return (0); 362 } 363 364 /* lookup the xattrdir's smb_node */ 365 xattr_dnode = smb_node_lookup(sr, NULL, cr, xattr_dvp, XATTR_DIR, 366 unode, NULL); 367 VN_RELE(xattr_dvp); 368 if (xattr_dnode == NULL) { 369 smbsr_error(sr, NT_STATUS_NO_MEMORY, 370 ERRDOS, ERROR_NOT_ENOUGH_MEMORY); 371 return (0); 372 } 373 374 (void) snprintf(pattern, sizeof (pattern), "%s*", SMB_STREAM_PREFIX); 375 od = smb_odir_create(sr, xattr_dnode, pattern, SMB_SEARCH_ATTRIBUTES, 376 cr); 377 smb_node_release(xattr_dnode); 378 if (od == NULL) 379 return (0); 380 381 od->d_flags |= SMB_ODIR_FLAG_XATTR; 382 return (od->d_odid); 383 } 384 385 /* 386 * smb_odir_hold 387 * 388 * A hold will only be granted if the odir is open or in_use. 389 */ 390 boolean_t 391 smb_odir_hold(smb_odir_t *od) 392 { 393 ASSERT(od); 394 ASSERT(od->d_magic == SMB_ODIR_MAGIC); 395 396 mutex_enter(&od->d_mutex); 397 398 switch (od->d_state) { 399 case SMB_ODIR_STATE_OPEN: 400 od->d_refcnt++; 401 od->d_state = SMB_ODIR_STATE_IN_USE; 402 break; 403 case SMB_ODIR_STATE_IN_USE: 404 od->d_refcnt++; 405 break; 406 case SMB_ODIR_STATE_CLOSING: 407 case SMB_ODIR_STATE_CLOSED: 408 default: 409 mutex_exit(&od->d_mutex); 410 return (B_FALSE); 411 } 412 413 mutex_exit(&od->d_mutex); 414 return (B_TRUE); 415 } 416 417 /* 418 * smb_odir_release 419 * 420 * If the odir is in SMB_ODIR_STATE_CLOSING and this release 421 * results in a refcnt of 0, the odir may be removed from 422 * the tree's list of odirs and deleted. The odir's state is 423 * set to SMB_ODIR_STATE_CLOSED prior to exiting the mutex and 424 * deleting the odir. 425 */ 426 void 427 smb_odir_release(smb_odir_t *od) 428 { 429 ASSERT(od); 430 ASSERT(od->d_magic == SMB_ODIR_MAGIC); 431 432 mutex_enter(&od->d_mutex); 433 ASSERT(od->d_refcnt > 0); 434 435 switch (od->d_state) { 436 case SMB_ODIR_STATE_OPEN: 437 break; 438 case SMB_ODIR_STATE_IN_USE: 439 od->d_refcnt--; 440 if (od->d_refcnt == 0) 441 od->d_state = SMB_ODIR_STATE_OPEN; 442 break; 443 case SMB_ODIR_STATE_CLOSING: 444 od->d_refcnt--; 445 if (od->d_refcnt == 0) { 446 od->d_state = SMB_ODIR_STATE_CLOSED; 447 mutex_exit(&od->d_mutex); 448 smb_odir_delete(od); 449 return; 450 } 451 break; 452 case SMB_ODIR_STATE_CLOSED: 453 default: 454 break; 455 } 456 457 mutex_exit(&od->d_mutex); 458 } 459 460 /* 461 * smb_odir_close 462 */ 463 void 464 smb_odir_close(smb_odir_t *od) 465 { 466 ASSERT(od); 467 ASSERT(od->d_magic == SMB_ODIR_MAGIC); 468 469 mutex_enter(&od->d_mutex); 470 ASSERT(od->d_refcnt > 0); 471 switch (od->d_state) { 472 case SMB_ODIR_STATE_OPEN: 473 break; 474 case SMB_ODIR_STATE_IN_USE: 475 od->d_state = SMB_ODIR_STATE_CLOSING; 476 break; 477 case SMB_ODIR_STATE_CLOSING: 478 case SMB_ODIR_STATE_CLOSED: 479 default: 480 break; 481 } 482 mutex_exit(&od->d_mutex); 483 } 484 485 /* 486 * smb_odir_read 487 * 488 * Find the next directory entry matching the search pattern. 489 * No search attribute matching is performed. 490 * 491 * Returns: 492 * 0 - success. 493 * - If a matching entry was found eof will be B_FALSE and 494 * odirent will be populated. 495 * - If there are no matching entries eof will be B_TRUE. 496 * -1 - error, error details set in sr. 497 */ 498 int 499 smb_odir_read(smb_request_t *sr, smb_odir_t *od, 500 smb_odirent_t *odirent, boolean_t *eof) 501 { 502 int rc; 503 boolean_t ignore_case; 504 505 ASSERT(sr); 506 ASSERT(sr->sr_magic == SMB_REQ_MAGIC); 507 ASSERT(od); 508 ASSERT(od->d_magic == SMB_ODIR_MAGIC); 509 ASSERT(odirent); 510 511 mutex_enter(&od->d_mutex); 512 ASSERT(od->d_refcnt > 0); 513 514 switch (od->d_state) { 515 case SMB_ODIR_STATE_IN_USE: 516 case SMB_ODIR_STATE_CLOSING: 517 break; 518 case SMB_ODIR_STATE_OPEN: 519 case SMB_ODIR_STATE_CLOSED: 520 default: 521 mutex_exit(&od->d_mutex); 522 return (-1); 523 } 524 525 ignore_case = (od->d_flags & SMB_ODIR_FLAG_IGNORE_CASE); 526 527 for (;;) { 528 if ((rc = smb_odir_next_odirent(od, odirent)) != 0) 529 break; 530 if (smb_match_name(odirent->od_ino, odirent->od_name, 531 od->d_pattern, ignore_case)) 532 break; 533 } 534 535 mutex_exit(&od->d_mutex); 536 537 switch (rc) { 538 case 0: 539 *eof = B_FALSE; 540 return (0); 541 case ENOENT: 542 *eof = B_TRUE; 543 return (0); 544 default: 545 smbsr_errno(sr, rc); 546 return (-1); 547 } 548 } 549 550 /* 551 * smb_odir_read_fileinfo 552 * 553 * Find the next directory entry matching the search pattern 554 * and attributes: od->d_pattern and od->d_sattr. 555 * 556 * If the search pattern specifies a single filename call 557 * smb_odir_single_fileinfo to get the file attributes and 558 * populate the caller's smb_fileinfo_t. 559 * 560 * If the search pattern contains wildcards call smb_odir_next_odirent 561 * to get the next directory entry then. Repeat until a matching 562 * filename is found. Call smb_odir_wildcard_fileinfo to get the 563 * file attributes and populate the caller's smb_fileinfo_t. 564 * This is repeated until a file matching the search criteria is found. 565 * 566 * Returns: 567 * 0 - success. 568 * - If a matching entry was found eof will be B_FALSE and 569 * fileinfo will be populated. 570 * - If there are no matching entries eof will be B_TRUE. 571 * -1 - error, error details set in sr. 572 */ 573 int 574 smb_odir_read_fileinfo(smb_request_t *sr, smb_odir_t *od, 575 smb_fileinfo_t *fileinfo, boolean_t *eof) 576 { 577 int rc, errnum; 578 smb_odirent_t *odirent; 579 boolean_t ignore_case; 580 581 ASSERT(sr); 582 ASSERT(sr->sr_magic == SMB_REQ_MAGIC); 583 ASSERT(od); 584 ASSERT(od->d_magic == SMB_ODIR_MAGIC); 585 ASSERT(fileinfo); 586 587 mutex_enter(&od->d_mutex); 588 ASSERT(od->d_refcnt > 0); 589 590 switch (od->d_state) { 591 case SMB_ODIR_STATE_IN_USE: 592 case SMB_ODIR_STATE_CLOSING: 593 break; 594 case SMB_ODIR_STATE_OPEN: 595 case SMB_ODIR_STATE_CLOSED: 596 default: 597 mutex_exit(&od->d_mutex); 598 return (-1); 599 } 600 601 ignore_case = (od->d_flags & SMB_ODIR_FLAG_IGNORE_CASE); 602 603 if (!(od->d_flags & SMB_ODIR_FLAG_WILDCARDS)) { 604 if (od->d_eof) 605 rc = ENOENT; 606 else 607 rc = smb_odir_single_fileinfo(sr, od, fileinfo); 608 od->d_eof = B_TRUE; 609 } else { 610 odirent = kmem_alloc(sizeof (smb_odirent_t), KM_SLEEP); 611 for (;;) { 612 bzero(fileinfo, sizeof (smb_fileinfo_t)); 613 if ((rc = smb_odir_next_odirent(od, odirent)) != 0) 614 break; 615 616 /* skip non utf8 filename */ 617 if (u8_validate(odirent->od_name, 618 strlen(odirent->od_name), NULL, 619 U8_VALIDATE_ENTIRE, &errnum) < 0) 620 continue; 621 622 if (!smb_match_name(odirent->od_ino, odirent->od_name, 623 od->d_pattern, ignore_case)) 624 continue; 625 626 rc = smb_odir_wildcard_fileinfo(sr, od, odirent, 627 fileinfo); 628 if (rc == 0) 629 break; 630 } 631 kmem_free(odirent, sizeof (smb_odirent_t)); 632 } 633 mutex_exit(&od->d_mutex); 634 635 switch (rc) { 636 case 0: 637 *eof = B_FALSE; 638 return (0); 639 case ENOENT: 640 *eof = B_TRUE; 641 return (0); 642 default: 643 smbsr_errno(sr, rc); 644 return (-1); 645 } 646 } 647 648 649 /* 650 * smb_odir_read_streaminfo 651 * 652 * Find the next directory entry whose name begins with SMB_STREAM_PREFIX, 653 * and thus represents an NTFS named stream. 654 * No search attribute matching is performed. 655 * No case conflict name mangling is required for NTFS named stream names. 656 * 657 * Returns: 658 * 0 - success. 659 * - If a matching entry was found eof will be B_FALSE and 660 * sinfo will be populated. 661 * - If there are no matching entries eof will be B_TRUE. 662 * -1 - error, error details set in sr. 663 */ 664 int 665 smb_odir_read_streaminfo(smb_request_t *sr, smb_odir_t *od, 666 smb_streaminfo_t *sinfo, boolean_t *eof) 667 { 668 int rc; 669 smb_odirent_t *odirent; 670 smb_node_t *fnode; 671 smb_attr_t attr; 672 673 ASSERT(sr); 674 ASSERT(sr->sr_magic == SMB_REQ_MAGIC); 675 ASSERT(od); 676 ASSERT(od->d_magic == SMB_ODIR_MAGIC); 677 ASSERT(sinfo); 678 679 mutex_enter(&od->d_mutex); 680 ASSERT(od->d_refcnt > 0); 681 682 switch (od->d_state) { 683 case SMB_ODIR_STATE_IN_USE: 684 case SMB_ODIR_STATE_CLOSING: 685 break; 686 case SMB_ODIR_STATE_OPEN: 687 case SMB_ODIR_STATE_CLOSED: 688 default: 689 mutex_exit(&od->d_mutex); 690 return (-1); 691 } 692 693 /* Check that odir represents an xattr directory */ 694 if (!(od->d_flags & SMB_ODIR_FLAG_XATTR)) { 695 *eof = B_TRUE; 696 mutex_exit(&od->d_mutex); 697 return (0); 698 } 699 700 odirent = kmem_alloc(sizeof (smb_odirent_t), KM_SLEEP); 701 702 for (;;) { 703 bzero(sinfo, sizeof (smb_streaminfo_t)); 704 if ((rc = smb_odir_next_odirent(od, odirent)) != 0) 705 break; 706 707 if (strncmp(odirent->od_name, SMB_STREAM_PREFIX, 708 SMB_STREAM_PREFIX_LEN)) { 709 continue; 710 } 711 712 rc = smb_fsop_lookup(sr, od->d_cred, 0, od->d_tree->t_snode, 713 od->d_dnode, odirent->od_name, &fnode); 714 if (rc == 0) { 715 rc = smb_node_getattr(sr, fnode, &attr); 716 smb_node_release(fnode); 717 } 718 719 if (rc == 0) { 720 (void) strlcpy(sinfo->si_name, 721 odirent->od_name + SMB_STREAM_PREFIX_LEN, 722 sizeof (sinfo->si_name)); 723 sinfo->si_size = attr.sa_vattr.va_size; 724 sinfo->si_alloc_size = attr.sa_allocsz; 725 break; 726 } 727 } 728 mutex_exit(&od->d_mutex); 729 730 kmem_free(odirent, sizeof (smb_odirent_t)); 731 732 switch (rc) { 733 case 0: 734 *eof = B_FALSE; 735 return (0); 736 case ENOENT: 737 *eof = B_TRUE; 738 return (0); 739 default: 740 smbsr_errno(sr, rc); 741 return (-1); 742 } 743 } 744 745 /* 746 * smb_odir_save_cookie 747 * 748 * Callers can save up to SMB_MAX_SEARCH cookies in the odir 749 * to be used as resume points for a 'find next' request. 750 */ 751 void 752 smb_odir_save_cookie(smb_odir_t *od, int idx, uint32_t cookie) 753 { 754 ASSERT(od); 755 ASSERT(od->d_magic == SMB_ODIR_MAGIC); 756 ASSERT(idx >= 0 && idx < SMB_MAX_SEARCH); 757 758 mutex_enter(&od->d_mutex); 759 od->d_cookies[idx] = cookie; 760 mutex_exit(&od->d_mutex); 761 } 762 763 /* 764 * smb_odir_resume_at 765 * 766 * If SMB_ODIR_FLAG_WILDCARDS is not set the search is for a single 767 * file and should not be resumed. 768 * 769 * Wildcard searching can be resumed from: 770 * - the cookie saved at a specified index (SMBsearch, SMBfind). 771 * - a specified cookie (SMB_trans2_find) 772 * - a specified filename (SMB_trans2_find) - NOT SUPPORTED. 773 * Defaults to continuing from where the last search ended. 774 * 775 * Continuation from where the last search ended (SMB_trans2_find) 776 * is implemented by saving the last cookie at a specific index (0) 777 * smb_odir_resume_at indicates a new request, so reset od->d_bufptr 778 * and d_eof to force a vop_readdir. 779 */ 780 void 781 smb_odir_resume_at(smb_odir_t *od, smb_odir_resume_t *resume) 782 { 783 ASSERT(od); 784 ASSERT(od->d_magic == SMB_ODIR_MAGIC); 785 ASSERT(resume); 786 787 mutex_enter(&od->d_mutex); 788 789 if ((od->d_flags & SMB_ODIR_FLAG_WILDCARDS) == 0) { 790 od->d_eof = B_TRUE; 791 mutex_exit(&od->d_mutex); 792 return; 793 } 794 795 switch (resume->or_type) { 796 case SMB_ODIR_RESUME_IDX: 797 ASSERT(resume->or_idx >= 0); 798 ASSERT(resume->or_idx < SMB_MAX_SEARCH); 799 800 if ((resume->or_idx < 0) || 801 (resume->or_idx >= SMB_MAX_SEARCH)) { 802 resume->or_idx = 0; 803 } 804 od->d_offset = od->d_cookies[resume->or_idx]; 805 break; 806 case SMB_ODIR_RESUME_COOKIE: 807 od->d_offset = resume->or_cookie; 808 break; 809 case SMB_ODIR_RESUME_FNAME: 810 default: 811 od->d_offset = od->d_cookies[0]; 812 break; 813 } 814 815 /* Force a vop_readdir to refresh d_buf */ 816 od->d_bufptr = NULL; 817 od->d_eof = B_FALSE; 818 819 mutex_exit(&od->d_mutex); 820 } 821 822 823 /* *** static functions *** */ 824 825 /* 826 * smb_odir_create 827 * Allocate and populate an odir obect and add it to the tree's list. 828 */ 829 static smb_odir_t * 830 smb_odir_create(smb_request_t *sr, smb_node_t *dnode, 831 char *pattern, uint16_t sattr, cred_t *cr) 832 { 833 smb_odir_t *od; 834 smb_tree_t *tree; 835 uint16_t odid; 836 837 ASSERT(sr); 838 ASSERT(sr->sr_magic == SMB_REQ_MAGIC); 839 ASSERT(sr->tid_tree); 840 ASSERT(sr->tid_tree->t_magic == SMB_TREE_MAGIC); 841 ASSERT(dnode); 842 ASSERT(dnode->n_magic == SMB_NODE_MAGIC); 843 844 tree = sr->tid_tree; 845 846 if (smb_idpool_alloc(&tree->t_odid_pool, &odid)) { 847 smbsr_error(sr, NT_STATUS_TOO_MANY_OPENED_FILES, 848 ERRDOS, ERROR_TOO_MANY_OPEN_FILES); 849 return (NULL); 850 } 851 852 od = kmem_cache_alloc(tree->t_server->si_cache_odir, KM_SLEEP); 853 bzero(od, sizeof (smb_odir_t)); 854 855 mutex_init(&od->d_mutex, NULL, MUTEX_DEFAULT, NULL); 856 od->d_refcnt = 0; 857 od->d_state = SMB_ODIR_STATE_OPEN; 858 od->d_magic = SMB_ODIR_MAGIC; 859 od->d_opened_by_pid = sr->smb_pid; 860 od->d_session = tree->t_session; 861 od->d_cred = cr; 862 od->d_tree = tree; 863 od->d_dnode = dnode; 864 smb_node_ref(dnode); 865 od->d_odid = odid; 866 od->d_sattr = sattr; 867 (void) strlcpy(od->d_pattern, pattern, sizeof (od->d_pattern)); 868 od->d_flags = 0; 869 if (smb_contains_wildcards(od->d_pattern)) 870 od->d_flags |= SMB_ODIR_FLAG_WILDCARDS; 871 if (vfs_has_feature(dnode->vp->v_vfsp, VFSFT_DIRENTFLAGS)) 872 od->d_flags |= SMB_ODIR_FLAG_EDIRENT; 873 if (smb_tree_has_feature(tree, SMB_TREE_CASEINSENSITIVE)) 874 od->d_flags |= SMB_ODIR_FLAG_IGNORE_CASE; 875 if (SMB_TREE_SUPPORTS_CATIA(sr)) 876 od->d_flags |= SMB_ODIR_FLAG_CATIA; 877 if (SMB_TREE_SUPPORTS_ABE(sr)) 878 od->d_flags |= SMB_ODIR_FLAG_ABE; 879 od->d_eof = B_FALSE; 880 881 smb_llist_enter(&tree->t_odir_list, RW_WRITER); 882 smb_llist_insert_tail(&tree->t_odir_list, od); 883 smb_llist_exit(&tree->t_odir_list); 884 885 atomic_inc_32(&tree->t_session->s_dir_cnt); 886 return (od); 887 } 888 889 /* 890 * smb_odir_delete 891 * 892 * Removal of the odir from the tree's list of odirs must be 893 * done before any resources associated with the odir are 894 * released. 895 */ 896 static void 897 smb_odir_delete(smb_odir_t *od) 898 { 899 ASSERT(od); 900 ASSERT(od->d_magic == SMB_ODIR_MAGIC); 901 ASSERT(od->d_state == SMB_ODIR_STATE_CLOSED); 902 903 smb_llist_enter(&od->d_tree->t_odir_list, RW_WRITER); 904 smb_llist_remove(&od->d_tree->t_odir_list, od); 905 smb_llist_exit(&od->d_tree->t_odir_list); 906 907 od->d_magic = 0; 908 atomic_dec_32(&od->d_tree->t_session->s_dir_cnt); 909 smb_node_release(od->d_dnode); 910 smb_idpool_free(&od->d_tree->t_odid_pool, od->d_odid); 911 mutex_destroy(&od->d_mutex); 912 kmem_cache_free(od->d_tree->t_server->si_cache_odir, od); 913 } 914 915 /* 916 * smb_odir_next_odirent 917 * 918 * Find the next directory entry in d_buf. If d_bufptr is NULL (buffer 919 * is empty or we've reached the end of it), read the next set of 920 * entries from the file system (vop_readdir). 921 * 922 * File systems which support VFSFT_EDIRENT_FLAGS will return the 923 * directory entries as a buffer of edirent_t structure. Others will 924 * return a buffer of dirent64_t structures. For simplicity translate 925 * the data into an smb_odirent_t structure. 926 * The ed_name/d_name in d_buf is NULL terminated by the file system. 927 * 928 * Some file systems can have directories larger than SMB_MAXDIRSIZE. 929 * If the odirent offset >= SMB_MAXDIRSIZE return ENOENT and set d_eof 930 * to true to stop subsequent calls to smb_vop_readdir. 931 * 932 * Returns: 933 * 0 - success. odirent is populated with the next directory entry 934 * ENOENT - no more directory entries 935 * errno - error 936 */ 937 static int 938 smb_odir_next_odirent(smb_odir_t *od, smb_odirent_t *odirent) 939 { 940 int rc; 941 int reclen; 942 int eof; 943 dirent64_t *dp; 944 edirent_t *edp; 945 char *np; 946 uint32_t abe_flag = 0; 947 948 ASSERT(MUTEX_HELD(&od->d_mutex)); 949 950 bzero(odirent, sizeof (smb_odirent_t)); 951 952 if (od->d_bufptr != NULL) { 953 if (od->d_flags & SMB_ODIR_FLAG_EDIRENT) 954 reclen = od->d_edp->ed_reclen; 955 else 956 reclen = od->d_dp->d_reclen; 957 958 if (reclen == 0) { 959 od->d_bufptr = NULL; 960 } else { 961 od->d_bufptr += reclen; 962 if (od->d_bufptr >= od->d_buf + od->d_bufsize) 963 od->d_bufptr = NULL; 964 } 965 } 966 967 if (od->d_bufptr == NULL) { 968 if (od->d_eof) 969 return (ENOENT); 970 971 od->d_bufsize = sizeof (od->d_buf); 972 973 if (od->d_flags & SMB_ODIR_FLAG_ABE) 974 abe_flag = SMB_ABE; 975 976 rc = smb_vop_readdir(od->d_dnode->vp, od->d_offset, 977 od->d_buf, &od->d_bufsize, &eof, abe_flag, od->d_cred); 978 979 if ((rc == 0) && (od->d_bufsize == 0)) 980 rc = ENOENT; 981 982 if (rc != 0) { 983 od->d_bufptr = NULL; 984 od->d_bufsize = 0; 985 return (rc); 986 } 987 988 od->d_eof = (eof != 0); 989 od->d_bufptr = od->d_buf; 990 } 991 992 if (od->d_flags & SMB_ODIR_FLAG_EDIRENT) 993 od->d_offset = od->d_edp->ed_off; 994 else 995 od->d_offset = od->d_dp->d_off; 996 997 if (od->d_offset >= SMB_MAXDIRSIZE) { 998 od->d_bufptr = NULL; 999 od->d_bufsize = 0; 1000 od->d_eof = B_TRUE; 1001 return (ENOENT); 1002 } 1003 1004 if (od->d_flags & SMB_ODIR_FLAG_EDIRENT) { 1005 edp = od->d_edp; 1006 odirent->od_ino = edp->ed_ino; 1007 odirent->od_eflags = edp->ed_eflags; 1008 np = edp->ed_name; 1009 } else { 1010 dp = od->d_dp; 1011 odirent->od_ino = dp->d_ino; 1012 odirent->od_eflags = 0; 1013 np = dp->d_name; 1014 } 1015 1016 if ((od->d_flags & SMB_ODIR_FLAG_CATIA) && 1017 ((od->d_flags & SMB_ODIR_FLAG_XATTR) == 0)) { 1018 smb_vop_catia_v4tov5(np, odirent->od_name, 1019 sizeof (odirent->od_name)); 1020 } else { 1021 (void) strlcpy(odirent->od_name, np, 1022 sizeof (odirent->od_name)); 1023 } 1024 1025 return (0); 1026 } 1027 1028 /* 1029 * smb_odir_single_fileinfo 1030 * 1031 * Lookup the file identified by od->d_pattern. 1032 * 1033 * If the looked up file is a link, we attempt to lookup the link target 1034 * to use its attributes in place of those of the files's. 1035 * If we fail to lookup the target of the link we use the original 1036 * file's attributes. 1037 * Check if the attributes match the search attributes. 1038 * 1039 * Returns: 0 - success 1040 * ENOENT - no match 1041 * errno - error 1042 */ 1043 static int 1044 smb_odir_single_fileinfo(smb_request_t *sr, smb_odir_t *od, 1045 smb_fileinfo_t *fileinfo) 1046 { 1047 int rc; 1048 smb_node_t *fnode, *tgt_node; 1049 smb_attr_t attr; 1050 ino64_t ino; 1051 char *name; 1052 boolean_t case_conflict = B_FALSE; 1053 int lookup_flags, flags = 0; 1054 vnode_t *vp; 1055 1056 ASSERT(sr); 1057 ASSERT(sr->sr_magic == SMB_REQ_MAGIC); 1058 ASSERT(od); 1059 ASSERT(od->d_magic == SMB_ODIR_MAGIC); 1060 1061 ASSERT(MUTEX_HELD(&od->d_mutex)); 1062 bzero(fileinfo, sizeof (smb_fileinfo_t)); 1063 1064 rc = smb_fsop_lookup(sr, od->d_cred, 0, od->d_tree->t_snode, 1065 od->d_dnode, od->d_pattern, &fnode); 1066 if (rc != 0) 1067 return (rc); 1068 1069 /* 1070 * If case sensitive, do a case insensitive smb_vop_lookup to 1071 * check for case conflict 1072 */ 1073 if (od->d_flags & SMB_ODIR_FLAG_IGNORE_CASE) { 1074 lookup_flags = SMB_IGNORE_CASE; 1075 if (od->d_flags & SMB_ODIR_FLAG_CATIA) 1076 lookup_flags |= SMB_CATIA; 1077 1078 rc = smb_vop_lookup(od->d_dnode->vp, fnode->od_name, &vp, 1079 NULL, lookup_flags, &flags, od->d_tree->t_snode->vp, 1080 od->d_cred); 1081 if (rc != 0) 1082 return (rc); 1083 VN_RELE(vp); 1084 1085 if (flags & ED_CASE_CONFLICT) 1086 case_conflict = B_TRUE; 1087 } 1088 1089 if ((rc = smb_node_getattr(sr, fnode, &attr)) != 0) { 1090 smb_node_release(fnode); 1091 return (rc); 1092 } 1093 1094 ino = attr.sa_vattr.va_nodeid; 1095 (void) smb_mangle_name(ino, fnode->od_name, 1096 fileinfo->fi_shortname, fileinfo->fi_name83, case_conflict); 1097 name = (case_conflict) ? fileinfo->fi_shortname : fnode->od_name; 1098 (void) strlcpy(fileinfo->fi_name, name, sizeof (fileinfo->fi_name)); 1099 1100 /* follow link to get target node & attr */ 1101 if ((fnode->vp->v_type == VLNK) && 1102 (smb_odir_lookup_link(sr, od, fnode->od_name, &tgt_node))) { 1103 smb_node_release(fnode); 1104 fnode = tgt_node; 1105 if ((rc = smb_node_getattr(sr, fnode, &attr)) != 0) { 1106 smb_node_release(fnode); 1107 return (rc); 1108 } 1109 } 1110 1111 /* check search attributes */ 1112 if (!smb_sattr_check(attr.sa_dosattr, od->d_sattr)) { 1113 smb_node_release(fnode); 1114 return (ENOENT); 1115 } 1116 1117 fileinfo->fi_dosattr = attr.sa_dosattr; 1118 fileinfo->fi_nodeid = attr.sa_vattr.va_nodeid; 1119 fileinfo->fi_size = attr.sa_vattr.va_size; 1120 fileinfo->fi_alloc_size = attr.sa_allocsz; 1121 fileinfo->fi_atime = attr.sa_vattr.va_atime; 1122 fileinfo->fi_mtime = attr.sa_vattr.va_mtime; 1123 fileinfo->fi_ctime = attr.sa_vattr.va_ctime; 1124 if (attr.sa_crtime.tv_sec) 1125 fileinfo->fi_crtime = attr.sa_crtime; 1126 else 1127 fileinfo->fi_crtime = attr.sa_vattr.va_mtime; 1128 1129 smb_node_release(fnode); 1130 return (0); 1131 } 1132 1133 /* 1134 * smb_odir_wildcard_fileinfo 1135 * 1136 * odirent contains a directory entry, obtained from a vop_readdir. 1137 * If a case conflict is identified the filename is mangled and the 1138 * shortname is used as 'name', in place of odirent->od_name. This 1139 * name will be used in the smb_fsop_lookup because smb_fsop_lookup 1140 * performs a case insensitive lookup if the tree is case insesitive, 1141 * so the mangled name is required in the case conflict scenario to 1142 * ensure the correct match. 1143 * 1144 * If the looked up file is a link, we attempt to lookup the link target 1145 * to use its attributes in place of those of the files's. 1146 * If we fail to lookup the target of the link we use the original 1147 * file's attributes. 1148 * Check if the attributes match the search attributes. 1149 * 1150 * Although some file systems can have directories larger than 1151 * SMB_MAXDIRSIZE smb_odir_next_odirent ensures that no offset larger 1152 * than SMB_MAXDIRSIZE is returned. It is therefore safe to use the 1153 * offset as the cookie (uint32_t). 1154 * 1155 * Returns: 0 - success 1156 * ENOENT - no match, proceed to next entry 1157 * errno - error 1158 */ 1159 static int 1160 smb_odir_wildcard_fileinfo(smb_request_t *sr, smb_odir_t *od, 1161 smb_odirent_t *odirent, smb_fileinfo_t *fileinfo) 1162 { 1163 int rc; 1164 smb_node_t *fnode, *tgt_node; 1165 smb_attr_t attr; 1166 char *name; 1167 boolean_t case_conflict; 1168 1169 ASSERT(sr); 1170 ASSERT(sr->sr_magic == SMB_REQ_MAGIC); 1171 ASSERT(od); 1172 ASSERT(od->d_magic == SMB_ODIR_MAGIC); 1173 1174 ASSERT(MUTEX_HELD(&od->d_mutex)); 1175 bzero(fileinfo, sizeof (smb_fileinfo_t)); 1176 1177 case_conflict = ((od->d_flags & SMB_ODIR_FLAG_IGNORE_CASE) && 1178 (odirent->od_eflags & ED_CASE_CONFLICT)); 1179 (void) smb_mangle_name(odirent->od_ino, odirent->od_name, 1180 fileinfo->fi_shortname, fileinfo->fi_name83, case_conflict); 1181 name = (case_conflict) ? fileinfo->fi_shortname : odirent->od_name; 1182 (void) strlcpy(fileinfo->fi_name, name, sizeof (fileinfo->fi_name)); 1183 1184 rc = smb_fsop_lookup(sr, od->d_cred, 0, od->d_tree->t_snode, 1185 od->d_dnode, name, &fnode); 1186 if (rc != 0) 1187 return (rc); 1188 1189 /* follow link to get target node & attr */ 1190 if ((fnode->vp->v_type == VLNK) && 1191 (smb_odir_lookup_link(sr, od, name, &tgt_node))) { 1192 smb_node_release(fnode); 1193 fnode = tgt_node; 1194 } 1195 1196 if ((rc = smb_node_getattr(sr, fnode, &attr)) != 0) { 1197 smb_node_release(fnode); 1198 return (rc); 1199 } 1200 1201 /* check search attributes */ 1202 if (!smb_sattr_check(attr.sa_dosattr, od->d_sattr)) { 1203 smb_node_release(fnode); 1204 return (ENOENT); 1205 } 1206 1207 fileinfo->fi_cookie = (uint32_t)od->d_offset; 1208 fileinfo->fi_dosattr = attr.sa_dosattr; 1209 fileinfo->fi_nodeid = attr.sa_vattr.va_nodeid; 1210 fileinfo->fi_size = attr.sa_vattr.va_size; 1211 fileinfo->fi_alloc_size = attr.sa_allocsz; 1212 fileinfo->fi_atime = attr.sa_vattr.va_atime; 1213 fileinfo->fi_mtime = attr.sa_vattr.va_mtime; 1214 fileinfo->fi_ctime = attr.sa_vattr.va_ctime; 1215 if (attr.sa_crtime.tv_sec) 1216 fileinfo->fi_crtime = attr.sa_crtime; 1217 else 1218 fileinfo->fi_crtime = attr.sa_vattr.va_mtime; 1219 1220 smb_node_release(fnode); 1221 return (0); 1222 } 1223 1224 /* 1225 * smb_odir_lookup_link 1226 * 1227 * If the file is a symlink we lookup the object to which the 1228 * symlink refers so that we can return its attributes. 1229 * This can cause a problem if a symlink in a sub-directory 1230 * points to a parent directory (some UNIX GUI's create a symlink 1231 * in $HOME/.desktop that points to the user's home directory). 1232 * Some Windows applications (e.g. virus scanning) loop/hang 1233 * trying to follow this recursive path and there is little 1234 * we can do because the path is constructed on the client. 1235 * smb_dirsymlink_enable allows an end-user to disable 1236 * symlinks to directories. Symlinks to other object types 1237 * should be unaffected. 1238 * 1239 * Returns: B_TRUE - followed link. tgt_node and tgt_attr set 1240 * B_FALSE - link not followed 1241 */ 1242 static boolean_t 1243 smb_odir_lookup_link(smb_request_t *sr, smb_odir_t *od, 1244 char *fname, smb_node_t **tgt_node) 1245 { 1246 int rc; 1247 1248 rc = smb_fsop_lookup(sr, od->d_cred, SMB_FOLLOW_LINKS, 1249 od->d_tree->t_snode, od->d_dnode, fname, tgt_node); 1250 if (rc != 0) { 1251 *tgt_node = NULL; 1252 return (B_FALSE); 1253 } 1254 1255 if (smb_node_is_dir(*tgt_node) && (!smb_dirsymlink_enable)) { 1256 smb_node_release(*tgt_node); 1257 *tgt_node = NULL; 1258 return (B_FALSE); 1259 } 1260 1261 return (B_TRUE); 1262 } 1263