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