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