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