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