xref: /illumos-gate/usr/src/uts/common/fs/smbsrv/smb_odir.c (revision cdf7958941f2584da228c9f288fe0ba88e45eda4)
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 2022 Tintri by DDN, Inc. All rights reserved.
24  * Copyright 2022 RackTop Systems, Inc.
25  */
26 
27 /*
28  * General Structures Layout
29  * -------------------------
30  *
31  * This is a simplified diagram showing the relationship between most of the
32  * main structures.
33  *
34  * +-------------------+
35  * |     SMB_INFO      |
36  * +-------------------+
37  *          |
38  *          |
39  *          v
40  * +-------------------+       +-------------------+      +-------------------+
41  * |     SESSION       |<----->|     SESSION       |......|      SESSION      |
42  * +-------------------+       +-------------------+      +-------------------+
43  *   |          |
44  *   |          |
45  *   |          v
46  *   |  +-------------------+     +-------------------+   +-------------------+
47  *   |  |       USER        |<--->|       USER        |...|       USER        |
48  *   |  +-------------------+     +-------------------+   +-------------------+
49  *   |
50  *   |
51  *   v
52  * +-------------------+       +-------------------+      +-------------------+
53  * |       TREE        |<----->|       TREE        |......|       TREE        |
54  * +-------------------+       +-------------------+      +-------------------+
55  *      |         |
56  *      |         |
57  *      |         v
58  *      |     +-------+       +-------+      +-------+
59  *      |     | OFILE |<----->| OFILE |......| OFILE |
60  *      |     +-------+       +-------+      +-------+
61  *      |
62  *      |
63  *      v
64  *  +-------+       +------+      +------+
65  *  | ODIR  |<----->| ODIR |......| ODIR |
66  *  +-------+       +------+      +------+
67  *
68  *
69  * Odir State Machine
70  * ------------------
71  *
72  *    +-------------------------+
73  *    |  SMB_ODIR_STATE_OPEN    |<----------- open / creation
74  *    +-------------------------+
75  *	    |            ^
76  *	    | (first)    | (last)
77  *	    | lookup     | release
78  *	    v            |
79  *    +-------------------------+
80  *    | SMB_ODIR_STATE_IN_USE   |----
81  *    +-------------------------+   | lookup / release / read
82  *	    |                ^-------
83  *	    | close
84  *	    |
85  *	    v
86  *    +-------------------------+
87  *    | SMB_ODIR_STATE_CLOSING  |----
88  *    +-------------------------+   | close / release / read
89  *	    |                ^-------
90  *	    | (last) release
91  *	    |
92  *	    v
93  *    +-------------------------+
94  *    | SMB_ODIR_STATE_CLOSED   |----------> deletion
95  *    +-------------------------+
96  *
97  *
98  * SMB_ODIR_STATE_OPEN
99  * - the odir exists in the list of odirs of its tree
100  * - lookup is valid in this state. It will place a hold on the odir
101  *   by incrementing the reference count and the odir will transition
102  *   to SMB_ODIR_STATE_IN_USE
103  * - read/close/release not valid in this state
104  *
105  * SMB_ODIR_STATE_IN_USE
106  * - the odir exists in the list of odirs of its tree.
107  * - lookup is valid in this state. It will place a hold on the odir
108  *   by incrementing the reference count.
109  * - if the last hold is released the odir will transition
110  *   back to SMB_ODIR_STATE_OPEN
111  * - if a close is received the odir will transition to
112  *   SMB_ODIR_STATE_CLOSING.
113  *
114  * SMB_ODIR_STATE_CLOSING
115  * - the odir exists in the list of odirs of its tree.
116  * - lookup will fail in this state.
117  * - when the last hold is released the odir will transition
118  *   to SMB_ODIR_STATE_CLOSED.
119  *
120  * SMB_ODIR_STATE_CLOSED
121  * - the odir exists in the list of odirs of its tree.
122  * - there are no users of the odir (refcnt == 0)
123  * - the odir is being removed from the tree's list and deleted.
124  * - lookup will fail in this state.
125  * - read/close/release not valid in this state
126  *
127  * Comments
128  * --------
129  *    The state machine of the odir structures is controlled by 3 elements:
130  *      - The list of odirs of the tree it belongs to.
131  *      - The mutex embedded in the structure itself.
132  *      - The reference count.
133  *
134  *    There's a mutex embedded in the odir structure used to protect its fields
135  *    and there's a lock embedded in the list of odirs of a tree. To
136  *    increment or to decrement the reference count the mutex must be entered.
137  *    To insert the odir into the list of odirs of the tree and to remove
138  *    the odir from it, the lock must be entered in RW_WRITER mode.
139  *
140  *    In order to avoid deadlocks, when both (mutex and lock of the odir
141  *    list) have to be entered, the lock must be entered first.
142  *
143  *
144  * Odir Interface
145  * ---------------
146  * smb_odir_open(char *pathname)
147  *	Create an odir representing the directory specified in pathname and
148  *	add it into the tree's list of odirs.
149  *	Returns NT status.
150  *
151  * smb_odir_openfh(smb_ofile_t *of)
152  *	Create an odir representing the directory specified by the
153  *	existing open handle (from a prior open of the directory).
154  *	Returns NT status.
155  *
156  * smb_odir_openat(smb_node_t *unode)
157  *	Create an odir representing the extended attribute directory
158  *	associated with the file (or directory) represented by unode
159  *	and add it into the tree's list of odirs.
160  *	Returns NT status.
161  *
162  * smb_odir_t *odir = smb_tree_lookup_odir(..., odid)
163  *	Find the odir corresponding to the specified odid in the tree's
164  *	list of odirs. Place a hold on the odir.
165  *
166  * smb_odir_read(..., smb_odirent_t *odirent)
167  *	Find the next directory entry in the odir and return it in odirent.
168  *
169  * smb_odir_read_fileinfo(..., smb_fileinfo_t *)
170  *	Find the next directory entry in the odir. Return the details of
171  *	the directory entry in smb_fileinfo_t. (See odir internals below)
172  *
173  * smb_odir_read_streaminfo(..., smb_streaminfo_t *)
174  *	Find the next named stream entry in the odir. Return the details of
175  *	the named stream in smb_streaminfo_t.
176  *
177  * smb_odir_close(smb_odir_t *odir)
178  *  Close the odir.
179  *  The caller of close must have a hold on the odir being closed.
180  *  The hold should be released after closing.
181  *
182  * smb_odir_release(smb_odir_t *odir)
183  *	Release the hold on the odir, obtained by lookup.
184  *
185  *
186  * Odir Internals
187  * --------------
188  * The odir object represent an open directory search. Each read operation
189  * provides the caller with a structure containing information  pertaining
190  * to the next directory entry that matches the search criteria, namely
191  * the filename or match pattern and, in the case of smb_odir_read_fileinfo(),
192  * the search attributes.
193  *
194  * The odir maintains a buffer (d_buf) of directory entries read from
195  * the filesystem via a vop_readdir. The buffer is populated when a read
196  * request (smb_odir_next_odirent) finds that the buffer is empty or that
197  * the end of the buffer has been reached, and also when a new client request
198  * (find next) begins.
199  *
200  * The data in d_buf (that which is returned from the file system) can
201  * be in one of two formats. If the file system supports extended directory
202  * entries we request that the data be returned as edirent_t structures. If
203  * it does not the data will be returned as dirent64_t structures. For
204  * convenience, when the next directory entry is read from d_buf by
205  * smb_odir_next_odirent it is translated into an smb_odirent_t.
206  *
207  * smb_odir_read_fileinfo
208  * The processing required to obtain the information to populate the caller's
209  * smb_fileinfo_t differs depending upon whether the directory search is for a
210  * single specified filename or for multiple files matching a search pattern.
211  * Thus smb_odir_read_fileinfo uses two static functions:
212  * smb_odir_single_fileinfo - obtains the smb_fileinfo_t info for the single
213  * filename as specified in smb_odir_open request.
214  * smb_odir_wildcard_fileinfo - obtains the smb_fileinfo_t info for the filename
215  * returned from the smb_odir_next_odirent. This is called in a loop until
216  * an entry matching the search criteria is found or no more entries exist.
217  *
218  * If a directory entry is a VLNK, the name returned in the smb_fileinfo_t
219  * is the name of the directory entry but the attributes are the attribites
220  * of the file that is the target of the link. If the link target cannot
221  * be found the attributes returned are the attributes of the link itself.
222  *
223  * smb_odir_read_streaminfo
224  * In order for an odir to provide information about stream files it
225  * must be opened with smb_odir_openat(). smb_odir_read_streaminfo() can
226  * then be used to obtain the name and size of named stream files.
227  *
228  * Resuming a Search
229  * -----------------
230  * A directory search often consists of multiple client requests: an initial
231  * find_first request followed by zero or more find_next requests and a
232  * find_close request.
233  * The find_first request will open and lookup the odir, read its desired
234  * number of entries from the odir, then release the odir and return.
235  * A find_next request will lookup the odir and read its desired number of
236  * entries from the odir, then release the odir and return.
237  * At the end of the search the find_close request will close the odir.
238  *
239  * In order to be able to resume a directory search (find_next) the odir
240  * provides the capability for the caller to save one or more resume points
241  * (cookies) at the end of a request, and to specify which resume point
242  * (cookie) to restart from at the beginning of the next search.
243  *	smb_odir_save_cookie(..., cookie)
244  *	smb_odir_resume_at(smb_odir_resume_t *resume)
245  * A search can be resumed at a specified resume point (cookie), the resume
246  * point (cookie) stored at a specified index in the d_cookies array, or
247  * a specified filename. The latter (specified filename) is not yet supported.
248  *
249  * See smb_search, smb_find, smb_find_unique, and smb_trans2_find for details
250  */
251 
252 #include <smbsrv/smb_kproto.h>
253 #include <smbsrv/smb_fsops.h>
254 #include <smbsrv/smb_share.h>
255 #include <sys/extdirent.h>
256 
257 /* static functions */
258 static smb_odir_t *smb_odir_create(smb_request_t *, smb_node_t *,
259     const char *, uint16_t, uint16_t, cred_t *);
260 static int smb_odir_single_fileinfo(smb_request_t *, smb_odir_t *,
261     smb_fileinfo_t *);
262 static int smb_odir_wildcard_fileinfo(smb_request_t *, smb_odir_t *,
263     smb_odirent_t *, smb_fileinfo_t *);
264 static int smb_odir_next_odirent(smb_odir_t *, smb_odirent_t *);
265 static boolean_t smb_odir_lookup_link(smb_request_t *, smb_odir_t *,
266     char *, smb_node_t **);
267 static boolean_t smb_odir_match_name(smb_odir_t *, smb_odirent_t *);
268 static void smb_odir_delete(void *);
269 
270 
271 /*
272  * smb_odir_openpath
273  *
274  * Create an odir representing the directory specified in pathname.
275  *
276  * Returns:
277  *    NT Status
278  */
279 uint32_t
280 smb_odir_openpath(smb_request_t *sr, char *path, uint16_t sattr,
281     uint32_t flags, smb_odir_t **odp)
282 {
283 	int		rc;
284 	smb_tree_t	*tree;
285 	smb_node_t	*dnode;
286 	char		pattern[MAXNAMELEN];
287 	uint16_t	odid;
288 	cred_t		*cr;
289 
290 	ASSERT(sr);
291 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
292 	ASSERT(sr->tid_tree);
293 	ASSERT(sr->tid_tree->t_magic == SMB_TREE_MAGIC);
294 	*odp = NULL;
295 
296 	tree = sr->tid_tree;
297 
298 	if (sr->session->dialect < NT_LM_0_12)
299 		smb_convert_wildcards(path);
300 
301 	rc = smb_pathname_reduce(sr, sr->user_cr, path,
302 	    tree->t_snode, tree->t_snode, &dnode, pattern);
303 	if (rc != 0)
304 		return (smb_errno2status(rc));
305 
306 	if (!smb_node_is_dir(dnode)) {
307 		smb_node_release(dnode);
308 		return (NT_STATUS_OBJECT_PATH_NOT_FOUND);
309 	}
310 
311 	if (smb_fsop_access(sr, sr->user_cr, dnode, FILE_LIST_DIRECTORY) != 0) {
312 		smb_node_release(dnode);
313 		return (NT_STATUS_ACCESS_DENIED);
314 	}
315 
316 	if (smb_idpool_alloc(&tree->t_odid_pool, &odid)) {
317 		smb_node_release(dnode);
318 		return (NT_STATUS_TOO_MANY_OPENED_FILES);
319 	}
320 
321 	if (flags & SMB_ODIR_OPENF_BACKUP_INTENT)
322 		cr = smb_user_getprivcred(sr->uid_user);
323 	else
324 		cr = sr->uid_user->u_cred;
325 
326 	*odp = smb_odir_create(sr, dnode, pattern, sattr, odid, cr);
327 	smb_node_release(dnode);
328 
329 	return (0);
330 }
331 
332 /*
333  * smb_odir_openfh
334  *
335  * Create an odir representing the directory already opened on "of".
336  *
337  * Returns:
338  *    NT status
339  */
340 uint32_t
341 smb_odir_openfh(smb_request_t *sr, const char *pattern, uint16_t sattr,
342     smb_odir_t **odp)
343 {
344 	smb_ofile_t	*of = sr->fid_ofile;
345 
346 	*odp = NULL;
347 
348 	if (of->f_node == NULL || !smb_node_is_dir(of->f_node))
349 		return (NT_STATUS_INVALID_PARAMETER);
350 
351 	if ((of->f_granted_access & FILE_LIST_DIRECTORY) == 0)
352 		return (NT_STATUS_ACCESS_DENIED);
353 
354 	*odp = smb_odir_create(sr, of->f_node, pattern, sattr, 0, of->f_cr);
355 
356 	return (0);
357 }
358 
359 /*
360  * smb_odir_openat
361  *
362  * Create an odir representing the extended attribute directory
363  * associated with the file (or directory) represented by unode.
364  *
365  * Returns:
366  *    NT status
367  */
368 uint32_t
369 smb_odir_openat(smb_request_t *sr, smb_node_t *unode, smb_odir_t **odp,
370     boolean_t restricted)
371 {
372 	char		pattern[SMB_STREAM_PREFIX_LEN + 2];
373 	vnode_t		*xattr_dvp;
374 	cred_t		*cr;
375 	smb_node_t	*xattr_dnode;
376 	int		rc;
377 
378 	ASSERT(sr);
379 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
380 	ASSERT(unode);
381 	ASSERT(unode->n_magic == SMB_NODE_MAGIC);
382 	*odp = NULL;
383 
384 	if (SMB_TREE_CONTAINS_NODE(sr, unode) == 0 ||
385 	    SMB_TREE_HAS_ACCESS(sr, ACE_LIST_DIRECTORY) == 0)
386 		return (NT_STATUS_ACCESS_DENIED);
387 
388 	cr = zone_kcred();
389 
390 	/* find the xattrdir vnode */
391 	rc = smb_vop_lookup_xattrdir(unode->vp, &xattr_dvp, LOOKUP_XATTR, cr);
392 	if (rc != 0)
393 		return (smb_errno2status(rc));
394 
395 	/* lookup the xattrdir's smb_node */
396 	xattr_dnode = smb_node_lookup(sr, NULL, cr, xattr_dvp, XATTR_DIR,
397 	    unode, NULL);
398 	VN_RELE(xattr_dvp);
399 	if (xattr_dnode == NULL)
400 		return (NT_STATUS_NO_MEMORY);
401 
402 	(void) snprintf(pattern, sizeof (pattern), "%s*", SMB_STREAM_PREFIX);
403 	*odp = smb_odir_create(sr, xattr_dnode, pattern,
404 	    SMB_SEARCH_ATTRIBUTES, 0, cr);
405 
406 	/* Causes restricted stream names to be hidden from the caller */
407 	if (restricted)
408 		(*odp)->d_flags |= SMB_ODIR_FLAG_RESTRICTED;
409 
410 	smb_node_release(xattr_dnode);
411 	return (0);
412 }
413 
414 /*
415  * smb_odir_hold
416  *
417  * A hold will only be granted if the odir is open or in_use.
418  */
419 boolean_t
420 smb_odir_hold(smb_odir_t *od)
421 {
422 	ASSERT(od);
423 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
424 
425 	mutex_enter(&od->d_mutex);
426 
427 	switch (od->d_state) {
428 	case SMB_ODIR_STATE_OPEN:
429 		od->d_refcnt++;
430 		od->d_state = SMB_ODIR_STATE_IN_USE;
431 		break;
432 	case SMB_ODIR_STATE_IN_USE:
433 		od->d_refcnt++;
434 		break;
435 	case SMB_ODIR_STATE_CLOSING:
436 	case SMB_ODIR_STATE_CLOSED:
437 	default:
438 		mutex_exit(&od->d_mutex);
439 		return (B_FALSE);
440 	}
441 
442 	mutex_exit(&od->d_mutex);
443 	return (B_TRUE);
444 }
445 
446 /*
447  * If the odir is in SMB_ODIR_STATE_CLOSING and this release results in
448  * a refcnt of 0, change the state to SMB_ODIR_STATE_CLOSED and post the
449  * object for deletion.  Object deletion is deferred to avoid modifying
450  * a list while an iteration may be in progress.
451  */
452 void
453 smb_odir_release(smb_odir_t *od)
454 {
455 	smb_tree_t *tree = od->d_tree;
456 
457 	SMB_ODIR_VALID(od);
458 
459 	mutex_enter(&od->d_mutex);
460 	ASSERT(od->d_refcnt > 0);
461 
462 	switch (od->d_state) {
463 	case SMB_ODIR_STATE_OPEN:
464 		break;
465 	case SMB_ODIR_STATE_IN_USE:
466 		od->d_refcnt--;
467 		if (od->d_refcnt == 0)
468 			od->d_state = SMB_ODIR_STATE_OPEN;
469 		break;
470 	case SMB_ODIR_STATE_CLOSING:
471 		od->d_refcnt--;
472 		if (od->d_refcnt == 0) {
473 			od->d_state = SMB_ODIR_STATE_CLOSED;
474 			smb_llist_post(&tree->t_odir_list, od,
475 			    smb_odir_delete);
476 		}
477 		break;
478 	case SMB_ODIR_STATE_CLOSED:
479 	default:
480 		break;
481 	}
482 
483 	mutex_exit(&od->d_mutex);
484 }
485 
486 /*
487  * smb_odir_close
488  */
489 void
490 smb_odir_close(smb_odir_t *od)
491 {
492 	ASSERT(od);
493 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
494 
495 	mutex_enter(&od->d_mutex);
496 	ASSERT(od->d_refcnt > 0);
497 	switch (od->d_state) {
498 	case SMB_ODIR_STATE_OPEN:
499 		break;
500 	case SMB_ODIR_STATE_IN_USE:
501 		od->d_state = SMB_ODIR_STATE_CLOSING;
502 		break;
503 	case SMB_ODIR_STATE_CLOSING:
504 	case SMB_ODIR_STATE_CLOSED:
505 	default:
506 		break;
507 	}
508 	mutex_exit(&od->d_mutex);
509 }
510 
511 /*
512  * smb_odir_read
513  *
514  * Find the next directory entry matching the search pattern.
515  * No search attribute matching is performed.
516  *
517  * Returns:
518  *  0 - success.
519  *      - If a matching entry was found eof will be B_FALSE and
520  *        odirent will be populated.
521  * ENOENT
522  *      - If we've scanned to the end, eof will be B_TRUE.
523  * errno - other errors
524  */
525 int
526 smb_odir_read(smb_request_t *sr, smb_odir_t *od,
527     smb_odirent_t *odirent, boolean_t *eof)
528 {
529 	int		rc;
530 
531 	ASSERT(sr);
532 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
533 	ASSERT(od);
534 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
535 	ASSERT(odirent);
536 
537 	mutex_enter(&od->d_mutex);
538 	ASSERT(od->d_refcnt > 0);
539 
540 	switch (od->d_state) {
541 	case SMB_ODIR_STATE_IN_USE:
542 	case SMB_ODIR_STATE_CLOSING:
543 		break;
544 	case SMB_ODIR_STATE_OPEN:
545 	case SMB_ODIR_STATE_CLOSED:
546 	default:
547 		mutex_exit(&od->d_mutex);
548 		return (EBADF);
549 	}
550 
551 	for (;;) {
552 		if ((rc = smb_odir_next_odirent(od, odirent)) != 0)
553 			break;
554 		if (smb_odir_match_name(od, odirent))
555 			break;
556 	}
557 
558 	mutex_exit(&od->d_mutex);
559 
560 	switch (rc) {
561 	case 0:
562 		*eof = B_FALSE;
563 		return (0);
564 	case ENOENT:
565 		*eof = B_TRUE;
566 		/* FALLTHROUGH */
567 	default:
568 		return (rc);
569 	}
570 }
571 
572 /*
573  * smb_odir_read_fileinfo
574  *
575  * Find the next directory entry matching the search pattern
576  * and attributes: od->d_pattern and od->d_sattr.
577  *
578  * If the search pattern specifies a single filename call
579  * smb_odir_single_fileinfo to get the file attributes and
580  * populate the caller's smb_fileinfo_t.
581  *
582  * If the search pattern contains wildcards call smb_odir_next_odirent
583  * to get the next directory entry then. Repeat until a matching
584  * filename is found. Call smb_odir_wildcard_fileinfo to get the
585  * file attributes and populate the caller's smb_fileinfo_t.
586  * This is repeated until a file matching the search criteria is found.
587  *
588  * Returns:
589  *  0 - success.
590  *      - If a matching entry was found eof will be B_FALSE and
591  *        fileinfo will be populated.
592  * ENOENT
593  *      - If at end of dir, eof will be B_TRUE.
594  * errno - other error
595  */
596 int
597 smb_odir_read_fileinfo(smb_request_t *sr, smb_odir_t *od,
598     smb_fileinfo_t *fileinfo, uint16_t *eof)
599 {
600 	int		rc, errnum;
601 	smb_odirent_t	*odirent;
602 
603 	ASSERT(sr);
604 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
605 	ASSERT(od);
606 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
607 	ASSERT(fileinfo);
608 
609 	mutex_enter(&od->d_mutex);
610 	ASSERT(od->d_refcnt > 0);
611 
612 	switch (od->d_state) {
613 	case SMB_ODIR_STATE_IN_USE:
614 	case SMB_ODIR_STATE_CLOSING:
615 		break;
616 	case SMB_ODIR_STATE_OPEN:
617 	case SMB_ODIR_STATE_CLOSED:
618 	default:
619 		mutex_exit(&od->d_mutex);
620 		return (EBADF);
621 	}
622 
623 	if ((od->d_flags & SMB_ODIR_FLAG_WILDCARDS) == 0) {
624 		if (od->d_eof)
625 			rc = ENOENT;
626 		else
627 			rc = smb_odir_single_fileinfo(sr, od, fileinfo);
628 		od->d_eof = B_TRUE;
629 	} else {
630 		odirent = kmem_alloc(sizeof (smb_odirent_t), KM_SLEEP);
631 		for (;;) {
632 			bzero(fileinfo, sizeof (smb_fileinfo_t));
633 			if ((rc = smb_odir_next_odirent(od, odirent)) != 0)
634 				break;
635 
636 			/* skip non utf8 filename */
637 			if (u8_validate(odirent->od_name,
638 			    strlen(odirent->od_name), NULL,
639 			    U8_VALIDATE_ENTIRE, &errnum) < 0)
640 				continue;
641 
642 			if (!smb_odir_match_name(od, odirent))
643 				continue;
644 
645 			rc = smb_odir_wildcard_fileinfo(sr, od, odirent,
646 			    fileinfo);
647 			if (rc == 0)
648 				break;
649 		}
650 		kmem_free(odirent, sizeof (smb_odirent_t));
651 	}
652 	mutex_exit(&od->d_mutex);
653 
654 	switch (rc) {
655 	case 0:
656 		*eof = 0;
657 		return (0);
658 	case ENOENT:
659 		*eof = 1;	/* per. FindFirst, FindNext spec. */
660 		/* FALLTHROUGH */
661 	default:
662 		return (rc);
663 	}
664 }
665 
666 /*
667  * smb_odir_read_streaminfo
668  *
669  * Find the next directory entry whose name begins with SMB_STREAM_PREFIX,
670  * and thus represents an NTFS named stream.
671  * No search attribute matching is performed.
672  * No case conflict name mangling is required for NTFS named stream names.
673  *
674  * Returns:
675  *  0 - success.
676  *      - If a matching entry was found eof will be B_FALSE and
677  *        sinfo will be populated.
678  *      - If there are no matching entries eof will be B_TRUE.
679  * errno - error
680  */
681 int
682 smb_odir_read_streaminfo(smb_request_t *sr, smb_odir_t *od,
683     smb_streaminfo_t *sinfo, boolean_t *eof)
684 {
685 	int		rc;
686 	cred_t		*kcr;
687 	smb_odirent_t	*odirent;
688 	smb_node_t	*fnode;
689 	smb_attr_t	attr;
690 
691 	ASSERT(sr);
692 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
693 	ASSERT(od);
694 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
695 	ASSERT(sinfo);
696 
697 	kcr = zone_kcred();
698 
699 	mutex_enter(&od->d_mutex);
700 	ASSERT(od->d_refcnt > 0);
701 
702 	switch (od->d_state) {
703 	case SMB_ODIR_STATE_IN_USE:
704 	case SMB_ODIR_STATE_CLOSING:
705 		break;
706 	case SMB_ODIR_STATE_OPEN:
707 	case SMB_ODIR_STATE_CLOSED:
708 	default:
709 		mutex_exit(&od->d_mutex);
710 		return (EBADF);
711 	}
712 
713 	/* Check that odir represents an xattr directory */
714 	if (!(od->d_flags & SMB_ODIR_FLAG_XATTR)) {
715 		*eof = B_TRUE;
716 		mutex_exit(&od->d_mutex);
717 		return (0);
718 	}
719 
720 	odirent = kmem_alloc(sizeof (smb_odirent_t), KM_SLEEP);
721 	bzero(&attr, sizeof (attr));
722 
723 	for (;;) {
724 		bzero(sinfo, sizeof (smb_streaminfo_t));
725 		if ((rc = smb_odir_next_odirent(od, odirent)) != 0)
726 			break;
727 
728 		if (strncmp(odirent->od_name, SMB_STREAM_PREFIX,
729 		    SMB_STREAM_PREFIX_LEN)) {
730 			continue;
731 		}
732 
733 		/*
734 		 * Hide streams that would be restricted if the caller
735 		 * is also restricted.
736 		 */
737 		if ((od->d_flags & SMB_ODIR_FLAG_RESTRICTED) != 0 &&
738 		    smb_strname_restricted(odirent->od_name))
739 			continue;
740 
741 		rc = smb_fsop_lookup(sr, od->d_cred, 0, od->d_tree->t_snode,
742 		    od->d_dnode, odirent->od_name, &fnode);
743 		if (rc == 0) {
744 			/*
745 			 * We just need the file sizes, and don't want
746 			 * EACCES failures here, so use kcred and pass
747 			 * NULL as the sr to skip sr->fid-ofile checks.
748 			 */
749 			attr.sa_mask = SMB_AT_SIZE | SMB_AT_ALLOCSZ;
750 			rc = smb_node_getattr(NULL, fnode, kcr, NULL, &attr);
751 			smb_node_release(fnode);
752 		}
753 
754 		if (rc == 0) {
755 			(void) strlcpy(sinfo->si_name,
756 			    odirent->od_name + SMB_STREAM_PREFIX_LEN,
757 			    sizeof (sinfo->si_name));
758 			sinfo->si_size = attr.sa_vattr.va_size;
759 			sinfo->si_alloc_size = attr.sa_allocsz;
760 			break;
761 		}
762 	}
763 	mutex_exit(&od->d_mutex);
764 
765 	kmem_free(odirent, sizeof (smb_odirent_t));
766 
767 	switch (rc) {
768 	case 0:
769 		*eof = B_FALSE;
770 		return (0);
771 	case ENOENT:
772 		*eof = B_TRUE;
773 		return (0);
774 	default:
775 		return (rc);
776 	}
777 }
778 
779 /*
780  * smb_odir_save_cookie
781  *
782  * Callers can save up to SMB_MAX_SEARCH cookies in the odir
783  * to be used as resume points for a 'find next' request.
784  */
785 void
786 smb_odir_save_cookie(smb_odir_t *od, int idx, uint32_t cookie)
787 {
788 	ASSERT(od);
789 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
790 	ASSERT(idx >= 0 && idx < SMB_MAX_SEARCH);
791 
792 	mutex_enter(&od->d_mutex);
793 	od->d_cookies[idx] = cookie;
794 	mutex_exit(&od->d_mutex);
795 }
796 
797 /*
798  * smb_odir_save_fname
799  *
800  * Save a filename / offset pair, which are basically a
801  * one entry cache.  See smb_com_trans2_find_next2.
802  */
803 void
804 smb_odir_save_fname(smb_odir_t *od, uint32_t cookie, const char *fname)
805 {
806 	ASSERT(od);
807 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
808 
809 	mutex_enter(&od->d_mutex);
810 
811 	od->d_last_cookie = cookie;
812 	bzero(od->d_last_name, MAXNAMELEN);
813 	if (fname != NULL)
814 		(void) strlcpy(od->d_last_name, fname, MAXNAMELEN);
815 
816 	mutex_exit(&od->d_mutex);
817 }
818 
819 /*
820  * smb_odir_resume_at
821  *
822  * If SMB_ODIR_FLAG_WILDCARDS is not set, and we're rewinding,
823  * assume we're no longer at EOF.
824  *
825  * Wildcard searching can be resumed from:
826  * - the cookie saved at a specified index (SMBsearch, SMBfind).
827  * - a specified cookie (SMB_trans2_find)
828  * - a specified filename (SMB_trans2_find) - NOT SUPPORTED.
829  *   Defaults to continuing from where the last search ended.
830  *
831  * Continuation from where the last search ended (SMB_trans2_find)
832  * is implemented by saving the last cookie at a specific index (0)
833  * smb_odir_resume_at indicates a new request, so reset od->d_bufptr
834  * and d_eof to force a vop_readdir.
835  */
836 void
837 smb_odir_resume_at(smb_odir_t *od, smb_odir_resume_t *resume)
838 {
839 	uint64_t save_offset;
840 
841 	ASSERT(od);
842 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
843 	ASSERT(resume);
844 
845 	if ((od->d_flags & SMB_ODIR_FLAG_WILDCARDS) == 0) {
846 		if (resume->or_type == SMB_ODIR_RESUME_COOKIE)
847 			od->d_eof = B_FALSE;
848 		return;
849 	}
850 	mutex_enter(&od->d_mutex);
851 
852 	save_offset = od->d_offset;
853 	switch (resume->or_type) {
854 
855 	default:
856 	case SMB_ODIR_RESUME_CONT:
857 		/* Continue where we left off. */
858 		break;
859 
860 	case SMB_ODIR_RESUME_IDX:
861 		/*
862 		 * This is used only by the (ancient) SMB_SEARCH.
863 		 * Modern clients use trans2 FindFirst, FindNext.
864 		 */
865 		ASSERT(resume->or_idx >= 0);
866 		ASSERT(resume->or_idx < SMB_MAX_SEARCH);
867 
868 		if ((resume->or_idx < 0) ||
869 		    (resume->or_idx >= SMB_MAX_SEARCH)) {
870 			resume->or_idx = 0;
871 		}
872 		od->d_offset = od->d_cookies[resume->or_idx];
873 		break;
874 
875 	case SMB_ODIR_RESUME_COOKIE:
876 		od->d_offset = resume->or_cookie;
877 		break;
878 
879 	case SMB_ODIR_RESUME_FNAME:
880 		/*
881 		 * If the name matches the last one saved,
882 		 * use the offset that was saved with it in
883 		 * the odir.  Otherwise use the cookie value
884 		 * in the resume data from the client.
885 		 */
886 		if (strcmp(resume->or_fname, od->d_last_name) &&
887 		    od->d_last_cookie != 0) {
888 			od->d_offset = od->d_last_cookie;
889 		} else if (resume->or_cookie != 0) {
890 			od->d_offset = resume->or_cookie;
891 		} /* else continue where we left off */
892 		break;
893 	}
894 
895 	if (od->d_offset != save_offset) {
896 		/* Force a vop_readdir to refresh d_buf */
897 		od->d_bufptr = NULL;
898 		od->d_eof = B_FALSE;
899 	}
900 
901 	mutex_exit(&od->d_mutex);
902 }
903 
904 
905 /* *** static functions *** */
906 
907 /*
908  * smb_odir_create
909  * Allocate and populate an odir obect and add it to the tree's list.
910  */
911 static smb_odir_t *
912 smb_odir_create(smb_request_t *sr, smb_node_t *dnode,
913     const char *pattern, uint16_t sattr, uint16_t odid, cred_t *cr)
914 {
915 	smb_odir_t	*od;
916 	smb_tree_t	*tree;
917 
918 	ASSERT(sr);
919 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
920 	ASSERT(sr->tid_tree);
921 	ASSERT(sr->tid_tree->t_magic == SMB_TREE_MAGIC);
922 	ASSERT(dnode);
923 	ASSERT(dnode->n_magic == SMB_NODE_MAGIC);
924 
925 	tree = sr->tid_tree;
926 
927 	od = kmem_cache_alloc(smb_cache_odir, KM_SLEEP);
928 	bzero(od, sizeof (smb_odir_t));
929 
930 	mutex_init(&od->d_mutex, NULL, MUTEX_DEFAULT, NULL);
931 
932 	/*
933 	 * Return this to the caller as if they had done
934 	 * smb_tree_lookup_odir() to obtain the odir.
935 	 */
936 	od->d_refcnt = 1;
937 	od->d_state = SMB_ODIR_STATE_IN_USE;
938 	od->d_magic = SMB_ODIR_MAGIC;
939 	od->d_opened_by_pid = sr->smb_pid;
940 	od->d_session = tree->t_session;
941 	od->d_cred = cr;
942 	/*
943 	 * grab a ref for od->d_user
944 	 * released in  smb_odir_delete()
945 	 */
946 	smb_user_hold_internal(sr->uid_user);
947 	od->d_user = sr->uid_user;
948 	od->d_tree = tree;
949 	od->d_dnode = dnode;
950 	smb_node_ref(dnode);
951 	od->d_odid = odid;
952 	od->d_sattr = sattr;
953 	(void) strlcpy(od->d_pattern, pattern, sizeof (od->d_pattern));
954 	od->d_flags = 0;
955 	if (smb_contains_wildcards(od->d_pattern))
956 		od->d_flags |= SMB_ODIR_FLAG_WILDCARDS;
957 	if (vfs_has_feature(dnode->vp->v_vfsp, VFSFT_DIRENTFLAGS))
958 		od->d_flags |= SMB_ODIR_FLAG_EDIRENT;
959 	if (smb_tree_has_feature(tree, SMB_TREE_CASEINSENSITIVE))
960 		od->d_flags |= SMB_ODIR_FLAG_IGNORE_CASE;
961 	if (smb_tree_has_feature(tree, SMB_TREE_SHORTNAMES))
962 		od->d_flags |= SMB_ODIR_FLAG_SHORTNAMES;
963 	if (smb_tree_has_feature(tree, SMB_TREE_ACEMASKONACCESS))
964 		od->d_flags |= SMB_ODIR_FLAG_ACEACCESS;
965 	if (SMB_TREE_SUPPORTS_CATIA(sr))
966 		od->d_flags |= SMB_ODIR_FLAG_CATIA;
967 	if (SMB_TREE_SUPPORTS_ABE(sr))
968 		od->d_flags |= SMB_ODIR_FLAG_ABE;
969 	if (dnode->flags & NODE_XATTR_DIR)
970 		od->d_flags |= SMB_ODIR_FLAG_XATTR;
971 	od->d_eof = B_FALSE;
972 
973 	smb_llist_enter(&tree->t_odir_list, RW_WRITER);
974 	smb_llist_insert_tail(&tree->t_odir_list, od);
975 	smb_llist_exit(&tree->t_odir_list);
976 
977 	atomic_inc_32(&tree->t_session->s_dir_cnt);
978 	return (od);
979 }
980 
981 /*
982  * Set a new pattern, attributes, and rewind.
983  */
984 void
985 smb_odir_reopen(smb_odir_t *od, const char *pattern, uint16_t sattr)
986 {
987 
988 	SMB_ODIR_VALID(od);
989 
990 	mutex_enter(&od->d_mutex);
991 	od->d_sattr = sattr;
992 	(void) strlcpy(od->d_pattern, pattern, sizeof (od->d_pattern));
993 	if (smb_contains_wildcards(od->d_pattern))
994 		od->d_flags |= SMB_ODIR_FLAG_WILDCARDS;
995 	else
996 		od->d_flags &= ~SMB_ODIR_FLAG_WILDCARDS;
997 
998 	/* Internal smb_odir_resume_at */
999 	od->d_offset = 0;
1000 	od->d_bufptr = NULL;
1001 	od->d_eof = B_FALSE;
1002 
1003 	mutex_exit(&od->d_mutex);
1004 }
1005 
1006 /*
1007  * Delete an odir.
1008  *
1009  * Remove the odir from the tree list before freeing resources
1010  * associated with the odir.
1011  */
1012 static void
1013 smb_odir_delete(void *arg)
1014 {
1015 	smb_tree_t	*tree;
1016 	smb_odir_t	*od = (smb_odir_t *)arg;
1017 
1018 	SMB_ODIR_VALID(od);
1019 	ASSERT(od->d_refcnt == 0);
1020 	ASSERT(od->d_state == SMB_ODIR_STATE_CLOSED);
1021 
1022 	tree = od->d_tree;
1023 	smb_llist_enter(&tree->t_odir_list, RW_WRITER);
1024 	smb_llist_remove(&tree->t_odir_list, od);
1025 	if (od->d_odid != 0)
1026 		smb_idpool_free(&tree->t_odid_pool, od->d_odid);
1027 	atomic_dec_32(&tree->t_session->s_dir_cnt);
1028 	smb_llist_exit(&tree->t_odir_list);
1029 
1030 	/*
1031 	 * This odir is no longer on t_odir_list, however...
1032 	 *
1033 	 * This is called via smb_llist_post, which means it may run
1034 	 * BEFORE smb_odir_release drops d_mutex (if another thread
1035 	 * flushes the delete queue before we do).  Synchronize.
1036 	 */
1037 	mutex_enter(&od->d_mutex);
1038 	mutex_exit(&od->d_mutex);
1039 
1040 	od->d_magic = 0;
1041 	smb_node_release(od->d_dnode);
1042 	smb_user_release(od->d_user);
1043 	mutex_destroy(&od->d_mutex);
1044 	kmem_cache_free(smb_cache_odir, od);
1045 }
1046 
1047 /*
1048  * smb_odir_next_odirent
1049  *
1050  * Find the next directory entry in d_buf. If d_bufptr is NULL (buffer
1051  * is empty or we've reached the end of it), read the next set of
1052  * entries from the file system (vop_readdir).
1053  *
1054  * File systems which support VFSFT_EDIRENT_FLAGS will return the
1055  * directory entries as a buffer of edirent_t structure. Others will
1056  * return a buffer of dirent64_t structures.  For simplicity translate
1057  * the data into an smb_odirent_t structure.
1058  * The ed_name/d_name in d_buf is NULL terminated by the file system.
1059  *
1060  * Some file systems can have directories larger than SMB_MAXDIRSIZE.
1061  * If the odirent offset >= SMB_MAXDIRSIZE return ENOENT and set d_eof
1062  * to true to stop subsequent calls to smb_vop_readdir.
1063  *
1064  * Returns:
1065  *      0 - success. odirent is populated with the next directory entry
1066  * ENOENT - no more directory entries
1067  *  errno - error
1068  */
1069 static int
1070 smb_odir_next_odirent(smb_odir_t *od, smb_odirent_t *odirent)
1071 {
1072 	int		rc;
1073 	int		reclen;
1074 	int		eof;
1075 	dirent64_t	*dp;
1076 	edirent_t	*edp;
1077 	char		*np;
1078 	uint32_t	rddir_flags = 0;
1079 
1080 	ASSERT(MUTEX_HELD(&od->d_mutex));
1081 
1082 	bzero(odirent, sizeof (smb_odirent_t));
1083 
1084 	if (od->d_flags & SMB_ODIR_FLAG_ABE)
1085 		rddir_flags |= SMB_ABE;
1086 	if (od->d_flags & SMB_ODIR_FLAG_EDIRENT)
1087 		rddir_flags |= SMB_EDIRENT;
1088 
1089 	if (od->d_bufptr != NULL) {
1090 		if (od->d_flags & SMB_ODIR_FLAG_EDIRENT)
1091 			reclen = od->d_edp->ed_reclen;
1092 		else
1093 			reclen = od->d_dp->d_reclen;
1094 
1095 		if (reclen == 0) {
1096 			od->d_bufptr = NULL;
1097 		} else {
1098 			od->d_bufptr += reclen;
1099 			if (od->d_bufptr >= od->d_buf + od->d_bufsize)
1100 				od->d_bufptr = NULL;
1101 		}
1102 	}
1103 
1104 	if (od->d_bufptr == NULL) {
1105 		if (od->d_eof)
1106 			return (ENOENT);
1107 
1108 		od->d_bufsize = sizeof (od->d_buf);
1109 
1110 		rc = smb_vop_readdir(od->d_dnode->vp, od->d_offset,
1111 		    od->d_buf, &od->d_bufsize, &eof, rddir_flags, od->d_cred);
1112 
1113 		if ((rc == 0) && (od->d_bufsize == 0))
1114 			rc = ENOENT;
1115 
1116 		if (rc != 0) {
1117 			od->d_bufptr = NULL;
1118 			od->d_bufsize = 0;
1119 			return (rc);
1120 		}
1121 
1122 		od->d_eof = (eof != 0);
1123 		od->d_bufptr = od->d_buf;
1124 	}
1125 
1126 	if (od->d_flags & SMB_ODIR_FLAG_EDIRENT)
1127 		od->d_offset = od->d_edp->ed_off;
1128 	else
1129 		od->d_offset = od->d_dp->d_off;
1130 
1131 	if (od->d_offset >= SMB_MAXDIRSIZE) {
1132 		od->d_bufptr = NULL;
1133 		od->d_bufsize = 0;
1134 		od->d_eof = B_TRUE;
1135 		return (ENOENT);
1136 	}
1137 
1138 	if (od->d_flags & SMB_ODIR_FLAG_EDIRENT) {
1139 		edp = od->d_edp;
1140 		odirent->od_ino = edp->ed_ino;
1141 		odirent->od_eflags = edp->ed_eflags;
1142 		np = edp->ed_name;
1143 	} else {
1144 		dp = od->d_dp;
1145 		odirent->od_ino = dp->d_ino;
1146 		odirent->od_eflags = 0;
1147 		np =  dp->d_name;
1148 	}
1149 
1150 	if ((od->d_flags & SMB_ODIR_FLAG_CATIA) &&
1151 	    ((od->d_flags & SMB_ODIR_FLAG_XATTR) == 0)) {
1152 		smb_vop_catia_v4tov5(np, odirent->od_name,
1153 		    sizeof (odirent->od_name));
1154 	} else {
1155 		(void) strlcpy(odirent->od_name, np,
1156 		    sizeof (odirent->od_name));
1157 	}
1158 
1159 	return (0);
1160 }
1161 
1162 /*
1163  * smb_odir_single_fileinfo
1164  *
1165  * Lookup the file identified by od->d_pattern.
1166  *
1167  * If the looked up file is a link, we attempt to lookup the link target
1168  * to use its attributes in place of those of the files's.
1169  * If we fail to lookup the target of the link we use the original
1170  * file's attributes.
1171  * Check if the attributes match the search attributes.
1172  *
1173  * Returns: 0 - success
1174  *     ENOENT - no match
1175  *      errno - error
1176  */
1177 static int
1178 smb_odir_single_fileinfo(smb_request_t *sr, smb_odir_t *od,
1179     smb_fileinfo_t *fileinfo)
1180 {
1181 	int		rc;
1182 	smb_node_t	*fnode, *tgt_node;
1183 	smb_attr_t	attr;
1184 	ino64_t		fid;
1185 	char		*name;
1186 	boolean_t	case_conflict = B_FALSE;
1187 	int		lookup_flags, flags = 0;
1188 	vnode_t		*vp;
1189 
1190 	ASSERT(sr);
1191 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
1192 	ASSERT(od);
1193 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
1194 
1195 	ASSERT(MUTEX_HELD(&od->d_mutex));
1196 	bzero(fileinfo, sizeof (smb_fileinfo_t));
1197 
1198 	rc = smb_fsop_lookup(sr, od->d_cred, 0, od->d_tree->t_snode,
1199 	    od->d_dnode, od->d_pattern, &fnode);
1200 	if (rc != 0)
1201 		return (rc);
1202 
1203 	/*
1204 	 * If case sensitive, do a case insensitive smb_vop_lookup to
1205 	 * check for case conflict
1206 	 */
1207 	if (od->d_flags & SMB_ODIR_FLAG_IGNORE_CASE) {
1208 		lookup_flags = SMB_IGNORE_CASE;
1209 		if (od->d_flags & SMB_ODIR_FLAG_CATIA)
1210 			lookup_flags |= SMB_CATIA;
1211 
1212 		rc = smb_vop_lookup(od->d_dnode->vp, fnode->od_name, &vp,
1213 		    NULL, lookup_flags, &flags, od->d_tree->t_snode->vp,
1214 		    NULL, od->d_cred);
1215 		if (rc != 0)
1216 			return (rc);
1217 		VN_RELE(vp);
1218 
1219 		if (flags & ED_CASE_CONFLICT)
1220 			case_conflict = B_TRUE;
1221 	}
1222 
1223 	bzero(&attr, sizeof (attr));
1224 	attr.sa_mask = SMB_AT_ALL;
1225 	rc = smb_node_getattr(NULL, fnode, zone_kcred(), NULL, &attr);
1226 	if (rc != 0) {
1227 		smb_node_release(fnode);
1228 		return (rc);
1229 	}
1230 
1231 
1232 	/* follow link to get target node & attr */
1233 	if (smb_node_is_symlink(fnode) &&
1234 	    smb_odir_lookup_link(sr, od, fnode->od_name, &tgt_node)) {
1235 		smb_node_release(fnode);
1236 		fnode = tgt_node;
1237 		attr.sa_mask = SMB_AT_ALL;
1238 		rc = smb_node_getattr(NULL, fnode, zone_kcred(), NULL, &attr);
1239 		if (rc != 0) {
1240 			smb_node_release(fnode);
1241 			return (rc);
1242 		}
1243 	}
1244 
1245 	/* check search attributes */
1246 	if (!smb_sattr_check(attr.sa_dosattr, od->d_sattr)) {
1247 		smb_node_release(fnode);
1248 		return (ENOENT);
1249 	}
1250 
1251 	name = fnode->od_name;
1252 	if (od->d_flags & SMB_ODIR_FLAG_SHORTNAMES) {
1253 		fid = attr.sa_vattr.va_nodeid;
1254 		if (case_conflict || smb_needs_mangled(name)) {
1255 			smb_mangle(name, fid, fileinfo->fi_shortname,
1256 			    SMB_SHORTNAMELEN);
1257 		}
1258 		if (case_conflict)
1259 			name = fileinfo->fi_shortname;
1260 	}
1261 
1262 	(void) strlcpy(fileinfo->fi_name, name, sizeof (fileinfo->fi_name));
1263 
1264 	fileinfo->fi_dosattr = attr.sa_dosattr;
1265 	fileinfo->fi_nodeid = attr.sa_vattr.va_nodeid;
1266 	fileinfo->fi_size = attr.sa_vattr.va_size;
1267 	fileinfo->fi_alloc_size = attr.sa_allocsz;
1268 	fileinfo->fi_atime = attr.sa_vattr.va_atime;
1269 	fileinfo->fi_mtime = attr.sa_vattr.va_mtime;
1270 	fileinfo->fi_ctime = attr.sa_vattr.va_ctime;
1271 	if (attr.sa_crtime.tv_sec)
1272 		fileinfo->fi_crtime = attr.sa_crtime;
1273 	else
1274 		fileinfo->fi_crtime = attr.sa_vattr.va_mtime;
1275 
1276 	smb_node_release(fnode);
1277 	return (0);
1278 }
1279 
1280 /*
1281  * smb_odir_wildcard_fileinfo
1282  *
1283  * odirent contains a directory entry, obtained from a vop_readdir.
1284  * If a case conflict is identified the filename is mangled and the
1285  * shortname is used as 'name', in place of odirent->od_name.
1286  *
1287  * If the looked up file is a link, we attempt to lookup the link target
1288  * to use its attributes in place of those of the files's.
1289  * If we fail to lookup the target of the link we use the original
1290  * file's attributes.
1291  * Check if the attributes match the search attributes.
1292  *
1293  * Although some file systems can have directories larger than
1294  * SMB_MAXDIRSIZE smb_odir_next_odirent ensures that no offset larger
1295  * than SMB_MAXDIRSIZE is returned.  It is therefore safe to use the
1296  * offset as the cookie (uint32_t).
1297  *
1298  * Returns: 0 - success (return this entry)
1299  *  any errno - no match, proceed to next entry
1300  */
1301 static int
1302 smb_odir_wildcard_fileinfo(smb_request_t *sr, smb_odir_t *od,
1303     smb_odirent_t *odirent, smb_fileinfo_t *fileinfo)
1304 {
1305 	smb_attr_t	attr;
1306 	char		*name;
1307 	vnode_t		*fvp = NULL;
1308 	int		de_flags;
1309 	int		rc;
1310 	boolean_t	case_conflict;
1311 
1312 	ASSERT(sr);
1313 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
1314 	ASSERT(od);
1315 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
1316 
1317 	ASSERT(MUTEX_HELD(&od->d_mutex));
1318 	bzero(fileinfo, sizeof (smb_fileinfo_t));
1319 
1320 	bzero(&attr, sizeof (attr));
1321 	attr.sa_mask = SMB_AT_ALL;
1322 
1323 	/*
1324 	 * Lookup the vnode and get attributes.  We have the real
1325 	 * (on disk) name here from readdir, so CS lookup.
1326 	 * NB the getattr is done with kcred.
1327 	 */
1328 	rc = smb_vop_lookup(od->d_dnode->vp, odirent->od_name, &fvp,
1329 	    NULL, SMB_CASE_SENSITIVE, &de_flags,
1330 	    od->d_tree->t_snode->vp, &attr, od->d_cred);
1331 
1332 	if (rc != 0)
1333 		return (rc);
1334 
1335 	/*
1336 	 * follow link to get target node & attr
1337 	 */
1338 	if (attr.sa_vattr.va_type == VLNK &&
1339 	    (attr.sa_dosattr & FILE_ATTRIBUTE_REPARSE_POINT) == 0) {
1340 		/*
1341 		 * Follow the symlink (lookup again w/ follow)
1342 		 * Like smb_odir_lookup_link
1343 		 *
1344 		 * Could avoid creating an smb_node_t here,
1345 		 * but symlinks are not so common.
1346 		 */
1347 		smb_node_t *tnode = NULL;
1348 		vnode_t *tvp = NULL;
1349 
1350 		if (smb_odir_lookup_link(sr, od, odirent->od_name, &tnode)) {
1351 
1352 			attr.sa_mask = SMB_AT_ALL;
1353 			rc = smb_fsop_getattr(NULL, zone_kcred(), tnode, &attr);
1354 			if (rc != 0) {
1355 				smb_node_release(tnode);
1356 				VN_RELE(fvp);
1357 				return (rc);
1358 			}
1359 
1360 			/* Use the link target as fvp */
1361 			tvp = tnode->vp;
1362 			VN_HOLD(tvp);
1363 			smb_node_release(tnode);
1364 
1365 			VN_RELE(fvp);
1366 			fvp = tvp;
1367 		}
1368 	}
1369 
1370 	/*
1371 	 * skip system files
1372 	 * Like smb_node_is_system(fnode)
1373 	 */
1374 	if (smb_node_is_vfsroot(od->d_dnode) &&
1375 	    (strcasecmp(odirent->od_name, ".$EXTEND") == 0)) {
1376 		VN_RELE(fvp);
1377 		return (ENOENT);
1378 	}
1379 
1380 	/*
1381 	 * check search attributes
1382 	 */
1383 	if (!smb_sattr_check(attr.sa_dosattr, od->d_sattr)) {
1384 		VN_RELE(fvp);
1385 		return (ENOENT);
1386 	}
1387 
1388 	/*
1389 	 * Access Based Enumeration (ABE) checks
1390 	 * Skip this entry if the user can't read it.
1391 	 */
1392 	if ((od->d_flags & SMB_ODIR_FLAG_ABE) != 0) {
1393 		int atype;
1394 		int aflags;
1395 
1396 		if ((od->d_flags & SMB_ODIR_FLAG_ACEACCESS) != 0) {
1397 			atype = V_ACE_MASK;
1398 			aflags = ACE_READ_DATA;
1399 		} else {
1400 			atype = 0;
1401 			aflags = S_IRUSR;
1402 		}
1403 		rc = smb_vop_access(fvp, aflags, atype,
1404 		    od->d_dnode->vp, od->d_cred);
1405 		if (rc != 0) {
1406 			/* skip this entry */
1407 			VN_RELE(fvp);
1408 			return (rc);
1409 		}
1410 	}
1411 
1412 	/*
1413 	 * Deal with names that have conflicts (other names
1414 	 * that match this one if compared case-insensitive)
1415 	 */
1416 	name = odirent->od_name;
1417 	if (od->d_flags & SMB_ODIR_FLAG_SHORTNAMES) {
1418 		case_conflict = ((od->d_flags & SMB_ODIR_FLAG_IGNORE_CASE) &&
1419 		    (odirent->od_eflags & ED_CASE_CONFLICT));
1420 		if (case_conflict || smb_needs_mangled(name)) {
1421 			smb_mangle(name, odirent->od_ino,
1422 			    fileinfo->fi_shortname, SMB_SHORTNAMELEN);
1423 		}
1424 		if (case_conflict)
1425 			name = fileinfo->fi_shortname;
1426 	}
1427 
1428 	(void) strlcpy(fileinfo->fi_name, name, sizeof (fileinfo->fi_name));
1429 
1430 	fileinfo->fi_cookie = (uint32_t)od->d_offset;
1431 	fileinfo->fi_dosattr = attr.sa_dosattr;
1432 	fileinfo->fi_nodeid = attr.sa_vattr.va_nodeid;
1433 	fileinfo->fi_size = attr.sa_vattr.va_size;
1434 	fileinfo->fi_alloc_size = attr.sa_allocsz;
1435 	fileinfo->fi_atime = attr.sa_vattr.va_atime;
1436 	fileinfo->fi_mtime = attr.sa_vattr.va_mtime;
1437 	fileinfo->fi_ctime = attr.sa_vattr.va_ctime;
1438 	if (attr.sa_crtime.tv_sec)
1439 		fileinfo->fi_crtime = attr.sa_crtime;
1440 	else
1441 		fileinfo->fi_crtime = attr.sa_vattr.va_mtime;
1442 
1443 
1444 	VN_RELE(fvp);
1445 
1446 	return (0);
1447 }
1448 
1449 /*
1450  * smb_odir_lookup_link
1451  *
1452  * If the file is a symlink we lookup the object to which the
1453  * symlink refers so that we can return its attributes.
1454  * This can cause a problem if a symlink in a sub-directory
1455  * points to a parent directory (some UNIX GUI's create a symlink
1456  * in $HOME/.desktop that points to the user's home directory).
1457  * Some Windows applications (e.g. virus scanning) loop/hang
1458  * trying to follow this recursive path and there is little
1459  * we can do because the path is constructed on the client.
1460  * smb_dirsymlink_enable allows an end-user to disable
1461  * symlinks to directories. Symlinks to other object types
1462  * should be unaffected.
1463  *
1464  * Returns: B_TRUE  - followed link. tgt_node and tgt_attr set
1465  *          B_FALSE - link not followed
1466  */
1467 static boolean_t
1468 smb_odir_lookup_link(smb_request_t *sr, smb_odir_t *od,
1469     char *fname, smb_node_t **tgt_node)
1470 {
1471 	int rc;
1472 	uint32_t flags = SMB_FOLLOW_LINKS | SMB_CASE_SENSITIVE;
1473 
1474 	rc = smb_fsop_lookup(sr, od->d_cred, flags,
1475 	    od->d_tree->t_snode, od->d_dnode, fname, tgt_node);
1476 	if (rc != 0) {
1477 		*tgt_node = NULL;
1478 		return (B_FALSE);
1479 	}
1480 
1481 	if (smb_node_is_dir(*tgt_node) && (!smb_dirsymlink_enable)) {
1482 		smb_node_release(*tgt_node);
1483 		*tgt_node = NULL;
1484 		return (B_FALSE);
1485 	}
1486 
1487 	return (B_TRUE);
1488 }
1489 
1490 /*
1491  * smb_odir_match_name
1492  *
1493  * Check if the directory entry name matches the search pattern:
1494  * - Don't match reserved dos filenames.
1495  * - Check if odirent->od_name matches od->d_pattern.
1496  * - If shortnames are supported, generate the shortname from
1497  *   odirent->od_name and check if it matches od->d_pattern.
1498  */
1499 static boolean_t
1500 smb_odir_match_name(smb_odir_t *od, smb_odirent_t *odirent)
1501 {
1502 	char	*name = odirent->od_name;
1503 	char	shortname[SMB_SHORTNAMELEN];
1504 	ino64_t	ino = odirent->od_ino;
1505 	boolean_t ci = (od->d_flags & SMB_ODIR_FLAG_IGNORE_CASE) != 0;
1506 
1507 	if (smb_is_reserved_dos_name(name))
1508 		return (B_FALSE);
1509 
1510 	if (smb_match(od->d_pattern, name, ci))
1511 		return (B_TRUE);
1512 
1513 	if (od->d_flags & SMB_ODIR_FLAG_SHORTNAMES) {
1514 		smb_mangle(name, ino, shortname, SMB_SHORTNAMELEN);
1515 		if (smb_match(od->d_pattern, shortname, ci))
1516 			return (B_TRUE);
1517 	}
1518 
1519 	return (B_FALSE);
1520 }
1521