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