xref: /illumos-gate/usr/src/uts/common/fs/smbsrv/smb_odir.c (revision 56a2adb63cd7b5ba73bb3f0fb3930b2d2730513e)
1da6c28aaSamw /*
2da6c28aaSamw  * CDDL HEADER START
3da6c28aaSamw  *
4da6c28aaSamw  * The contents of this file are subject to the terms of the
5da6c28aaSamw  * Common Development and Distribution License (the "License").
6da6c28aaSamw  * You may not use this file except in compliance with the License.
7da6c28aaSamw  *
8da6c28aaSamw  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9da6c28aaSamw  * or http://www.opensolaris.org/os/licensing.
10da6c28aaSamw  * See the License for the specific language governing permissions
11da6c28aaSamw  * and limitations under the License.
12da6c28aaSamw  *
13da6c28aaSamw  * When distributing Covered Code, include this CDDL HEADER in each
14da6c28aaSamw  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15da6c28aaSamw  * If applicable, add the following below this CDDL HEADER, with the
16da6c28aaSamw  * fields enclosed by brackets "[]" replaced with your own identifying
17da6c28aaSamw  * information: Portions Copyright [yyyy] [name of copyright owner]
18da6c28aaSamw  *
19da6c28aaSamw  * CDDL HEADER END
20da6c28aaSamw  */
21da6c28aaSamw /*
22148c5f43SAlan Wright  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
23*56a2adb6SPrashanth Badari  * Copyright 2022 Tintri by DDN, Inc. All rights reserved.
24da6c28aaSamw  */
25da6c28aaSamw 
26da6c28aaSamw /*
27da6c28aaSamw  * General Structures Layout
28da6c28aaSamw  * -------------------------
29da6c28aaSamw  *
30da6c28aaSamw  * This is a simplified diagram showing the relationship between most of the
31da6c28aaSamw  * main structures.
32da6c28aaSamw  *
33da6c28aaSamw  * +-------------------+
34da6c28aaSamw  * |     SMB_INFO      |
35da6c28aaSamw  * +-------------------+
36da6c28aaSamw  *          |
37da6c28aaSamw  *          |
38da6c28aaSamw  *          v
39da6c28aaSamw  * +-------------------+       +-------------------+      +-------------------+
40da6c28aaSamw  * |     SESSION       |<----->|     SESSION       |......|      SESSION      |
41da6c28aaSamw  * +-------------------+       +-------------------+      +-------------------+
423b13a1efSThomas Keiser  *   |          |
433b13a1efSThomas Keiser  *   |          |
443b13a1efSThomas Keiser  *   |          v
453b13a1efSThomas Keiser  *   |  +-------------------+     +-------------------+   +-------------------+
463b13a1efSThomas Keiser  *   |  |       USER        |<--->|       USER        |...|       USER        |
473b13a1efSThomas Keiser  *   |  +-------------------+     +-------------------+   +-------------------+
48da6c28aaSamw  *   |
49da6c28aaSamw  *   |
50da6c28aaSamw  *   v
51da6c28aaSamw  * +-------------------+       +-------------------+      +-------------------+
52da6c28aaSamw  * |       TREE        |<----->|       TREE        |......|       TREE        |
53da6c28aaSamw  * +-------------------+       +-------------------+      +-------------------+
54da6c28aaSamw  *      |         |
55da6c28aaSamw  *      |         |
56da6c28aaSamw  *      |         v
57da6c28aaSamw  *      |     +-------+       +-------+      +-------+
58da6c28aaSamw  *      |     | OFILE |<----->| OFILE |......| OFILE |
59da6c28aaSamw  *      |     +-------+       +-------+      +-------+
60da6c28aaSamw  *      |
61da6c28aaSamw  *      |
62da6c28aaSamw  *      v
63da6c28aaSamw  *  +-------+       +------+      +------+
64da6c28aaSamw  *  | ODIR  |<----->| ODIR |......| ODIR |
65da6c28aaSamw  *  +-------+       +------+      +------+
66da6c28aaSamw  *
67da6c28aaSamw  *
68da6c28aaSamw  * Odir State Machine
69da6c28aaSamw  * ------------------
70da6c28aaSamw  *
717f667e74Sjose borrego  *    +-------------------------+
727f667e74Sjose borrego  *    |  SMB_ODIR_STATE_OPEN    |<----------- open / creation
73da6c28aaSamw  *    +-------------------------+
74a1511e6bSjoyce mcintosh  *	    |            ^
75a1511e6bSjoyce mcintosh  *	    | (first)    | (last)
76a1511e6bSjoyce mcintosh  *	    | lookup     | release
77a1511e6bSjoyce mcintosh  *	    v            |
78a1511e6bSjoyce mcintosh  *    +-------------------------+
79a1511e6bSjoyce mcintosh  *    | SMB_ODIR_STATE_IN_USE   |----
80a1511e6bSjoyce mcintosh  *    +-------------------------+   | lookup / release / read
81a1511e6bSjoyce mcintosh  *	    |                ^-------
827f667e74Sjose borrego  *	    | close
83da6c28aaSamw  *	    |
84da6c28aaSamw  *	    v
85da6c28aaSamw  *    +-------------------------+
86a1511e6bSjoyce mcintosh  *    | SMB_ODIR_STATE_CLOSING  |----
87a1511e6bSjoyce mcintosh  *    +-------------------------+   | close / release / read
88a1511e6bSjoyce mcintosh  *	    |                ^-------
89a1511e6bSjoyce mcintosh  *	    | (last) release
90da6c28aaSamw  *	    |
91da6c28aaSamw  *	    v
927f667e74Sjose borrego  *    +-------------------------+
937f667e74Sjose borrego  *    | SMB_ODIR_STATE_CLOSED   |----------> deletion
94da6c28aaSamw  *    +-------------------------+
95da6c28aaSamw  *
96da6c28aaSamw  *
977f667e74Sjose borrego  * SMB_ODIR_STATE_OPEN
98a1511e6bSjoyce mcintosh  * - the odir exists in the list of odirs of its tree
99a1511e6bSjoyce mcintosh  * - lookup is valid in this state. It will place a hold on the odir
100a1511e6bSjoyce mcintosh  *   by incrementing the reference count and the odir will transition
101a1511e6bSjoyce mcintosh  *   to SMB_ODIR_STATE_IN_USE
102a1511e6bSjoyce mcintosh  * - read/close/release not valid in this state
103a1511e6bSjoyce mcintosh  *
104a1511e6bSjoyce mcintosh  * SMB_ODIR_STATE_IN_USE
1057f667e74Sjose borrego  * - the odir exists in the list of odirs of its tree.
106a1511e6bSjoyce mcintosh  * - lookup is valid in this state. It will place a hold on the odir
107a1511e6bSjoyce mcintosh  *   by incrementing the reference count.
108a1511e6bSjoyce mcintosh  * - if the last hold is released the odir will transition
109a1511e6bSjoyce mcintosh  *   back to SMB_ODIR_STATE_OPEN
1107f667e74Sjose borrego  * - if a close is received the odir will transition to
1117f667e74Sjose borrego  *   SMB_ODIR_STATE_CLOSING.
112da6c28aaSamw  *
113da6c28aaSamw  * SMB_ODIR_STATE_CLOSING
1147f667e74Sjose borrego  * - the odir exists in the list of odirs of its tree.
115a1511e6bSjoyce mcintosh  * - lookup will fail in this state.
116a1511e6bSjoyce mcintosh  * - when the last hold is released the odir will transition
117a1511e6bSjoyce mcintosh  *   to SMB_ODIR_STATE_CLOSED.
118da6c28aaSamw  *
119da6c28aaSamw  * SMB_ODIR_STATE_CLOSED
1207f667e74Sjose borrego  * - the odir exists in the list of odirs of its tree.
1217f667e74Sjose borrego  * - there are no users of the odir (refcnt == 0)
1227f667e74Sjose borrego  * - the odir is being removed from the tree's list and deleted.
123a1511e6bSjoyce mcintosh  * - lookup will fail in this state.
124a1511e6bSjoyce mcintosh  * - read/close/release not valid in this state
125da6c28aaSamw  *
126da6c28aaSamw  * Comments
127da6c28aaSamw  * --------
128da6c28aaSamw  *    The state machine of the odir structures is controlled by 3 elements:
129da6c28aaSamw  *      - The list of odirs of the tree it belongs to.
130da6c28aaSamw  *      - The mutex embedded in the structure itself.
131da6c28aaSamw  *      - The reference count.
132da6c28aaSamw  *
133da6c28aaSamw  *    There's a mutex embedded in the odir structure used to protect its fields
134da6c28aaSamw  *    and there's a lock embedded in the list of odirs of a tree. To
135da6c28aaSamw  *    increment or to decrement the reference count the mutex must be entered.
136da6c28aaSamw  *    To insert the odir into the list of odirs of the tree and to remove
137da6c28aaSamw  *    the odir from it, the lock must be entered in RW_WRITER mode.
138da6c28aaSamw  *
1397f667e74Sjose borrego  *    In order to avoid deadlocks, when both (mutex and lock of the odir
140da6c28aaSamw  *    list) have to be entered, the lock must be entered first.
141da6c28aaSamw  *
142da6c28aaSamw  *
1437f667e74Sjose borrego  * Odir Interface
1447f667e74Sjose borrego  * ---------------
145a90cf9f2SGordon Ross  * smb_odir_open(char *pathname)
1467f667e74Sjose borrego  *	Create an odir representing the directory specified in pathname and
1477f667e74Sjose borrego  *	add it into the tree's list of odirs.
148a90cf9f2SGordon Ross  *	Returns NT status.
149a90cf9f2SGordon Ross  *
150a90cf9f2SGordon Ross  * smb_odir_openfh(smb_ofile_t *of)
151a90cf9f2SGordon Ross  *	Create an odir representing the directory specified by the
152a90cf9f2SGordon Ross  *	existing open handle (from a prior open of the directory).
153a90cf9f2SGordon Ross  *	Returns NT status.
154da6c28aaSamw  *
1557f667e74Sjose borrego  * smb_odir_openat(smb_node_t *unode)
1567f667e74Sjose borrego  *	Create an odir representing the extended attribute directory
1577f667e74Sjose borrego  *	associated with the file (or directory) represented by unode
1587f667e74Sjose borrego  *	and add it into the tree's list of odirs.
159a90cf9f2SGordon Ross  *	Returns NT status.
160da6c28aaSamw  *
1613b13a1efSThomas Keiser  * smb_odir_t *odir = smb_tree_lookup_odir(..., odid)
1627f667e74Sjose borrego  *	Find the odir corresponding to the specified odid in the tree's
163a1511e6bSjoyce mcintosh  *	list of odirs. Place a hold on the odir.
164da6c28aaSamw  *
1657f667e74Sjose borrego  * smb_odir_read(..., smb_odirent_t *odirent)
1667f667e74Sjose borrego  *	Find the next directory entry in the odir and return it in odirent.
1677f667e74Sjose borrego  *
1687f667e74Sjose borrego  * smb_odir_read_fileinfo(..., smb_fileinfo_t *)
1697f667e74Sjose borrego  *	Find the next directory entry in the odir. Return the details of
1707f667e74Sjose borrego  *	the directory entry in smb_fileinfo_t. (See odir internals below)
1717f667e74Sjose borrego  *
172037cac00Sjoyce mcintosh  * smb_odir_read_streaminfo(..., smb_streaminfo_t *)
1737f667e74Sjose borrego  *	Find the next named stream entry in the odir. Return the details of
1747f667e74Sjose borrego  *	the named stream in smb_streaminfo_t.
1757f667e74Sjose borrego  *
176a1511e6bSjoyce mcintosh  * smb_odir_close(smb_odir_t *odir)
177a1511e6bSjoyce mcintosh  *  Close the odir.
178a1511e6bSjoyce mcintosh  *  The caller of close must have a hold on the odir being closed.
179a1511e6bSjoyce mcintosh  *  The hold should be released after closing.
180a1511e6bSjoyce mcintosh  *
1817f667e74Sjose borrego  * smb_odir_release(smb_odir_t *odir)
1827f667e74Sjose borrego  *	Release the hold on the odir, obtained by lookup.
1837f667e74Sjose borrego  *
1847f667e74Sjose borrego  *
1857f667e74Sjose borrego  * Odir Internals
1867f667e74Sjose borrego  * --------------
1877f667e74Sjose borrego  * The odir object represent an open directory search. Each read operation
1887f667e74Sjose borrego  * provides the caller with a structure containing information  pertaining
1897f667e74Sjose borrego  * to the next directory entry that matches the search criteria, namely
1907f667e74Sjose borrego  * the filename or match pattern and, in the case of smb_odir_read_fileinfo(),
1917f667e74Sjose borrego  * the search attributes.
1927f667e74Sjose borrego  *
1937f667e74Sjose borrego  * The odir maintains a buffer (d_buf) of directory entries read from
1947f667e74Sjose borrego  * the filesystem via a vop_readdir. The buffer is populated when a read
1957f667e74Sjose borrego  * request (smb_odir_next_odirent) finds that the buffer is empty or that
1967f667e74Sjose borrego  * the end of the buffer has been reached, and also when a new client request
1977f667e74Sjose borrego  * (find next) begins.
1987f667e74Sjose borrego  *
1997f667e74Sjose borrego  * The data in d_buf (that which is returned from the file system) can
2007f667e74Sjose borrego  * be in one of two formats. If the file system supports extended directory
2017f667e74Sjose borrego  * entries we request that the data be returned as edirent_t structures. If
2027f667e74Sjose borrego  * it does not the data will be returned as dirent64_t structures. For
2037f667e74Sjose borrego  * convenience, when the next directory entry is read from d_buf by
2047f667e74Sjose borrego  * smb_odir_next_odirent it is translated into an smb_odirent_t.
2057f667e74Sjose borrego  *
2067f667e74Sjose borrego  * smb_odir_read_fileinfo
2077f667e74Sjose borrego  * The processing required to obtain the information to populate the caller's
2087f667e74Sjose borrego  * smb_fileinfo_t differs depending upon whether the directory search is for a
2097f667e74Sjose borrego  * single specified filename or for multiple files matching a search pattern.
2107f667e74Sjose borrego  * Thus smb_odir_read_fileinfo uses two static functions:
2117f667e74Sjose borrego  * smb_odir_single_fileinfo - obtains the smb_fileinfo_t info for the single
2127f667e74Sjose borrego  * filename as specified in smb_odir_open request.
2137f667e74Sjose borrego  * smb_odir_wildcard_fileinfo - obtains the smb_fileinfo_t info for the filename
2147f667e74Sjose borrego  * returned from the smb_odir_next_odirent. This is called in a loop until
2157f667e74Sjose borrego  * an entry matching the search criteria is found or no more entries exist.
2167f667e74Sjose borrego  *
2177f667e74Sjose borrego  * If a directory entry is a VLNK, the name returned in the smb_fileinfo_t
2187f667e74Sjose borrego  * is the name of the directory entry but the attributes are the attribites
2197f667e74Sjose borrego  * of the file that is the target of the link. If the link target cannot
2207f667e74Sjose borrego  * be found the attributes returned are the attributes of the link itself.
2217f667e74Sjose borrego  *
222037cac00Sjoyce mcintosh  * smb_odir_read_streaminfo
2237f667e74Sjose borrego  * In order for an odir to provide information about stream files it
2247f667e74Sjose borrego  * must be opened with smb_odir_openat(). smb_odir_read_streaminfo() can
2257f667e74Sjose borrego  * then be used to obtain the name and size of named stream files.
2267f667e74Sjose borrego  *
2277f667e74Sjose borrego  * Resuming a Search
2287f667e74Sjose borrego  * -----------------
2297f667e74Sjose borrego  * A directory search often consists of multiple client requests: an initial
2307f667e74Sjose borrego  * find_first request followed by zero or more find_next requests and a
2317f667e74Sjose borrego  * find_close request.
2327f667e74Sjose borrego  * The find_first request will open and lookup the odir, read its desired
2337f667e74Sjose borrego  * number of entries from the odir, then release the odir and return.
2347f667e74Sjose borrego  * A find_next request will lookup the odir and read its desired number of
2357f667e74Sjose borrego  * entries from the odir, then release the odir and return.
2367f667e74Sjose borrego  * At the end of the search the find_close request will close the odir.
2377f667e74Sjose borrego  *
2387f667e74Sjose borrego  * In order to be able to resume a directory search (find_next) the odir
2397f667e74Sjose borrego  * provides the capability for the caller to save one or more resume points
2407f667e74Sjose borrego  * (cookies) at the end of a request, and to specify which resume point
2417f667e74Sjose borrego  * (cookie) to restart from at the beginning of the next search.
2427f667e74Sjose borrego  *	smb_odir_save_cookie(..., cookie)
2437f667e74Sjose borrego  *	smb_odir_resume_at(smb_odir_resume_t *resume)
2447f667e74Sjose borrego  * A search can be resumed at a specified resume point (cookie), the resume
2457f667e74Sjose borrego  * point (cookie) stored at a specified index in the d_cookies array, or
2467f667e74Sjose borrego  * a specified filename. The latter (specified filename) is not yet supported.
2477f667e74Sjose borrego  *
2487f667e74Sjose borrego  * See smb_search, smb_find, smb_find_unique, and smb_trans2_find for details
249da6c28aaSamw  */
2507f667e74Sjose borrego 
251da6c28aaSamw #include <smbsrv/smb_kproto.h>
252da6c28aaSamw #include <smbsrv/smb_fsops.h>
253e3f2c991SKeyur Desai #include <smbsrv/smb_share.h>
2547f667e74Sjose borrego #include <sys/extdirent.h>
255da6c28aaSamw 
2567f667e74Sjose borrego /* static functions */
257a90cf9f2SGordon Ross static smb_odir_t *smb_odir_create(smb_request_t *, smb_node_t *,
258a90cf9f2SGordon Ross     const char *, uint16_t, uint16_t, cred_t *);
2597f667e74Sjose borrego static int smb_odir_single_fileinfo(smb_request_t *, smb_odir_t *,
2607f667e74Sjose borrego     smb_fileinfo_t *);
2617f667e74Sjose borrego static int smb_odir_wildcard_fileinfo(smb_request_t *, smb_odir_t *,
2627f667e74Sjose borrego     smb_odirent_t *, smb_fileinfo_t *);
2637f667e74Sjose borrego static int smb_odir_next_odirent(smb_odir_t *, smb_odirent_t *);
264037cac00Sjoyce mcintosh static boolean_t smb_odir_lookup_link(smb_request_t *, smb_odir_t *,
265037cac00Sjoyce mcintosh     char *, smb_node_t **);
266cb174861Sjoyce mcintosh static boolean_t smb_odir_match_name(smb_odir_t *, smb_odirent_t *);
267811599a4SMatt Barden static void smb_odir_delete(void *);
268da6c28aaSamw 
269da6c28aaSamw 
270da6c28aaSamw /*
271a90cf9f2SGordon Ross  * smb_odir_openpath
2727f667e74Sjose borrego  *
2737f667e74Sjose borrego  * Create an odir representing the directory specified in pathname.
2747f667e74Sjose borrego  *
2757f667e74Sjose borrego  * Returns:
276a90cf9f2SGordon Ross  *    NT Status
277da6c28aaSamw  */
278a90cf9f2SGordon Ross uint32_t
279a90cf9f2SGordon Ross smb_odir_openpath(smb_request_t *sr, char *path, uint16_t sattr,
280a90cf9f2SGordon Ross     uint32_t flags, smb_odir_t **odp)
281da6c28aaSamw {
2827f667e74Sjose borrego 	int		rc;
2837f667e74Sjose borrego 	smb_tree_t	*tree;
2847f667e74Sjose borrego 	smb_node_t	*dnode;
2857f667e74Sjose borrego 	char		pattern[MAXNAMELEN];
286cb174861Sjoyce mcintosh 	uint16_t	odid;
287eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 	cred_t		*cr;
288da6c28aaSamw 
2897f667e74Sjose borrego 	ASSERT(sr);
2907f667e74Sjose borrego 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
2917f667e74Sjose borrego 	ASSERT(sr->tid_tree);
2927f667e74Sjose borrego 	ASSERT(sr->tid_tree->t_magic == SMB_TREE_MAGIC);
293a90cf9f2SGordon Ross 	*odp = NULL;
294da6c28aaSamw 
2957f667e74Sjose borrego 	tree = sr->tid_tree;
2967f667e74Sjose borrego 
297c13be35aSGordon Ross 	if (sr->session->dialect < NT_LM_0_12)
298fe1c642dSBill Krier 		smb_convert_wildcards(path);
299fe1c642dSBill Krier 
3007f667e74Sjose borrego 	rc = smb_pathname_reduce(sr, sr->user_cr, path,
3017f667e74Sjose borrego 	    tree->t_snode, tree->t_snode, &dnode, pattern);
302a90cf9f2SGordon Ross 	if (rc != 0)
303a90cf9f2SGordon Ross 		return (smb_errno2status(rc));
304da6c28aaSamw 
3059fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	if (!smb_node_is_dir(dnode)) {
3067f667e74Sjose borrego 		smb_node_release(dnode);
307a90cf9f2SGordon Ross 		return (NT_STATUS_OBJECT_PATH_NOT_FOUND);
308da6c28aaSamw 	}
309da6c28aaSamw 
3107f667e74Sjose borrego 	if (smb_fsop_access(sr, sr->user_cr, dnode, FILE_LIST_DIRECTORY) != 0) {
3117f667e74Sjose borrego 		smb_node_release(dnode);
312a90cf9f2SGordon Ross 		return (NT_STATUS_ACCESS_DENIED);
313a90cf9f2SGordon Ross 	}
314a90cf9f2SGordon Ross 
315a90cf9f2SGordon Ross 	if (smb_idpool_alloc(&tree->t_odid_pool, &odid)) {
316a90cf9f2SGordon Ross 		smb_node_release(dnode);
317a90cf9f2SGordon Ross 		return (NT_STATUS_TOO_MANY_OPENED_FILES);
3187f667e74Sjose borrego 	}
319da6c28aaSamw 
320eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (flags & SMB_ODIR_OPENF_BACKUP_INTENT)
3213b13a1efSThomas Keiser 		cr = smb_user_getprivcred(sr->uid_user);
322eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 	else
3233b13a1efSThomas Keiser 		cr = sr->uid_user->u_cred;
324eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 
325a90cf9f2SGordon Ross 	*odp = smb_odir_create(sr, dnode, pattern, sattr, odid, cr);
3267f667e74Sjose borrego 	smb_node_release(dnode);
327a90cf9f2SGordon Ross 
328a90cf9f2SGordon Ross 	return (0);
329a90cf9f2SGordon Ross }
330a90cf9f2SGordon Ross 
331a90cf9f2SGordon Ross /*
332a90cf9f2SGordon Ross  * smb_odir_openfh
333a90cf9f2SGordon Ross  *
334a90cf9f2SGordon Ross  * Create an odir representing the directory already opened on "of".
335a90cf9f2SGordon Ross  *
336a90cf9f2SGordon Ross  * Returns:
337a90cf9f2SGordon Ross  *    NT status
338a90cf9f2SGordon Ross  */
339a90cf9f2SGordon Ross uint32_t
340a90cf9f2SGordon Ross smb_odir_openfh(smb_request_t *sr, const char *pattern, uint16_t sattr,
341a90cf9f2SGordon Ross     smb_odir_t **odp)
342a90cf9f2SGordon Ross {
343a90cf9f2SGordon Ross 	smb_ofile_t	*of = sr->fid_ofile;
344a90cf9f2SGordon Ross 
345a90cf9f2SGordon Ross 	*odp = NULL;
346a90cf9f2SGordon Ross 
347a90cf9f2SGordon Ross 	if (of->f_node == NULL || !smb_node_is_dir(of->f_node))
348a90cf9f2SGordon Ross 		return (NT_STATUS_INVALID_PARAMETER);
349a90cf9f2SGordon Ross 
350a90cf9f2SGordon Ross 	if ((of->f_granted_access & FILE_LIST_DIRECTORY) == 0)
351a90cf9f2SGordon Ross 		return (NT_STATUS_ACCESS_DENIED);
352a90cf9f2SGordon Ross 
353a90cf9f2SGordon Ross 	*odp = smb_odir_create(sr, of->f_node, pattern, sattr, 0, of->f_cr);
354a90cf9f2SGordon Ross 
355a90cf9f2SGordon Ross 	return (0);
3567f667e74Sjose borrego }
3577f667e74Sjose borrego 
3587f667e74Sjose borrego /*
3597f667e74Sjose borrego  * smb_odir_openat
3607f667e74Sjose borrego  *
3617f667e74Sjose borrego  * Create an odir representing the extended attribute directory
3627f667e74Sjose borrego  * associated with the file (or directory) represented by unode.
3637f667e74Sjose borrego  *
3647f667e74Sjose borrego  * Returns:
365a90cf9f2SGordon Ross  *    NT status
3667f667e74Sjose borrego  */
367a90cf9f2SGordon Ross uint32_t
368*56a2adb6SPrashanth Badari smb_odir_openat(smb_request_t *sr, smb_node_t *unode, smb_odir_t **odp,
369*56a2adb6SPrashanth Badari     boolean_t restricted)
3707f667e74Sjose borrego {
3717f667e74Sjose borrego 	char		pattern[SMB_STREAM_PREFIX_LEN + 2];
372a90cf9f2SGordon Ross 	vnode_t		*xattr_dvp;
373a90cf9f2SGordon Ross 	cred_t		*cr;
3747f667e74Sjose borrego 	smb_node_t	*xattr_dnode;
375a90cf9f2SGordon Ross 	int		rc;
3767f667e74Sjose borrego 
3777f667e74Sjose borrego 	ASSERT(sr);
3787f667e74Sjose borrego 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
3797f667e74Sjose borrego 	ASSERT(unode);
3807f667e74Sjose borrego 	ASSERT(unode->n_magic == SMB_NODE_MAGIC);
381a90cf9f2SGordon Ross 	*odp = NULL;
3827f667e74Sjose borrego 
383743a77edSAlan Wright 	if (SMB_TREE_CONTAINS_NODE(sr, unode) == 0 ||
384a90cf9f2SGordon Ross 	    SMB_TREE_HAS_ACCESS(sr, ACE_LIST_DIRECTORY) == 0)
385a90cf9f2SGordon Ross 		return (NT_STATUS_ACCESS_DENIED);
386a90cf9f2SGordon Ross 
3878622ec45SGordon Ross 	cr = zone_kcred();
3887f667e74Sjose borrego 
3897f667e74Sjose borrego 	/* find the xattrdir vnode */
3907f667e74Sjose borrego 	rc = smb_vop_lookup_xattrdir(unode->vp, &xattr_dvp, LOOKUP_XATTR, cr);
391a90cf9f2SGordon Ross 	if (rc != 0)
392a90cf9f2SGordon Ross 		return (smb_errno2status(rc));
3937f667e74Sjose borrego 
3947f667e74Sjose borrego 	/* lookup the xattrdir's smb_node */
3957f667e74Sjose borrego 	xattr_dnode = smb_node_lookup(sr, NULL, cr, xattr_dvp, XATTR_DIR,
396037cac00Sjoyce mcintosh 	    unode, NULL);
3977f667e74Sjose borrego 	VN_RELE(xattr_dvp);
398a90cf9f2SGordon Ross 	if (xattr_dnode == NULL)
399a90cf9f2SGordon Ross 		return (NT_STATUS_NO_MEMORY);
4007f667e74Sjose borrego 
4017f667e74Sjose borrego 	(void) snprintf(pattern, sizeof (pattern), "%s*", SMB_STREAM_PREFIX);
402a90cf9f2SGordon Ross 	*odp = smb_odir_create(sr, xattr_dnode, pattern,
403a90cf9f2SGordon Ross 	    SMB_SEARCH_ATTRIBUTES, 0, cr);
404a90cf9f2SGordon Ross 
405*56a2adb6SPrashanth Badari 	/* Causes restricted stream names to be hidden from the caller */
406*56a2adb6SPrashanth Badari 	if (restricted)
407*56a2adb6SPrashanth Badari 		(*odp)->d_flags |= SMB_ODIR_FLAG_RESTRICTED;
408*56a2adb6SPrashanth Badari 
4097f667e74Sjose borrego 	smb_node_release(xattr_dnode);
410a90cf9f2SGordon Ross 	return (0);
4117f667e74Sjose borrego }
4127f667e74Sjose borrego 
4137f667e74Sjose borrego /*
4147f667e74Sjose borrego  * smb_odir_hold
415a1511e6bSjoyce mcintosh  *
416a1511e6bSjoyce mcintosh  * A hold will only be granted if the odir is open or in_use.
4177f667e74Sjose borrego  */
4187f667e74Sjose borrego boolean_t
4197f667e74Sjose borrego smb_odir_hold(smb_odir_t *od)
4207f667e74Sjose borrego {
4217f667e74Sjose borrego 	ASSERT(od);
4227f667e74Sjose borrego 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
4237f667e74Sjose borrego 
4247f667e74Sjose borrego 	mutex_enter(&od->d_mutex);
425a1511e6bSjoyce mcintosh 
426a1511e6bSjoyce mcintosh 	switch (od->d_state) {
427a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_OPEN:
428a1511e6bSjoyce mcintosh 		od->d_refcnt++;
429a1511e6bSjoyce mcintosh 		od->d_state = SMB_ODIR_STATE_IN_USE;
430a1511e6bSjoyce mcintosh 		break;
431a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_IN_USE:
432a1511e6bSjoyce mcintosh 		od->d_refcnt++;
433a1511e6bSjoyce mcintosh 		break;
434a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_CLOSING:
435a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_CLOSED:
436a1511e6bSjoyce mcintosh 	default:
4377f667e74Sjose borrego 		mutex_exit(&od->d_mutex);
4387f667e74Sjose borrego 		return (B_FALSE);
4397f667e74Sjose borrego 	}
4407f667e74Sjose borrego 
4417f667e74Sjose borrego 	mutex_exit(&od->d_mutex);
4427f667e74Sjose borrego 	return (B_TRUE);
4437f667e74Sjose borrego }
4447f667e74Sjose borrego 
4457f667e74Sjose borrego /*
4469fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * If the odir is in SMB_ODIR_STATE_CLOSING and this release results in
4479fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * a refcnt of 0, change the state to SMB_ODIR_STATE_CLOSED and post the
4489fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * object for deletion.  Object deletion is deferred to avoid modifying
4499fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * a list while an iteration may be in progress.
4507f667e74Sjose borrego  */
4517f667e74Sjose borrego void
4527f667e74Sjose borrego smb_odir_release(smb_odir_t *od)
4537f667e74Sjose borrego {
454811599a4SMatt Barden 	smb_tree_t *tree = od->d_tree;
455811599a4SMatt Barden 
4569fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	SMB_ODIR_VALID(od);
4577f667e74Sjose borrego 
4587f667e74Sjose borrego 	mutex_enter(&od->d_mutex);
459a1511e6bSjoyce mcintosh 	ASSERT(od->d_refcnt > 0);
4607f667e74Sjose borrego 
4617f667e74Sjose borrego 	switch (od->d_state) {
4627f667e74Sjose borrego 	case SMB_ODIR_STATE_OPEN:
463a1511e6bSjoyce mcintosh 		break;
464a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_IN_USE:
4657f667e74Sjose borrego 		od->d_refcnt--;
466a1511e6bSjoyce mcintosh 		if (od->d_refcnt == 0)
467a1511e6bSjoyce mcintosh 			od->d_state = SMB_ODIR_STATE_OPEN;
4687f667e74Sjose borrego 		break;
4697f667e74Sjose borrego 	case SMB_ODIR_STATE_CLOSING:
4707f667e74Sjose borrego 		od->d_refcnt--;
4717f667e74Sjose borrego 		if (od->d_refcnt == 0) {
4727f667e74Sjose borrego 			od->d_state = SMB_ODIR_STATE_CLOSED;
473811599a4SMatt Barden 			smb_llist_post(&tree->t_odir_list, od,
474811599a4SMatt Barden 			    smb_odir_delete);
4757f667e74Sjose borrego 		}
4767f667e74Sjose borrego 		break;
4777f667e74Sjose borrego 	case SMB_ODIR_STATE_CLOSED:
4787f667e74Sjose borrego 	default:
4797f667e74Sjose borrego 		break;
4807f667e74Sjose borrego 	}
4817f667e74Sjose borrego 
4827f667e74Sjose borrego 	mutex_exit(&od->d_mutex);
483da6c28aaSamw }
484da6c28aaSamw 
485da6c28aaSamw /*
486da6c28aaSamw  * smb_odir_close
487da6c28aaSamw  */
488da6c28aaSamw void
4897f667e74Sjose borrego smb_odir_close(smb_odir_t *od)
490da6c28aaSamw {
491da6c28aaSamw 	ASSERT(od);
492a1511e6bSjoyce mcintosh 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
493da6c28aaSamw 
494da6c28aaSamw 	mutex_enter(&od->d_mutex);
495a1511e6bSjoyce mcintosh 	ASSERT(od->d_refcnt > 0);
496a1511e6bSjoyce mcintosh 	switch (od->d_state) {
497a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_OPEN:
498a1511e6bSjoyce mcintosh 		break;
499a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_IN_USE:
5007f667e74Sjose borrego 		od->d_state = SMB_ODIR_STATE_CLOSING;
501a1511e6bSjoyce mcintosh 		break;
502a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_CLOSING:
503a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_CLOSED:
504a1511e6bSjoyce mcintosh 	default:
505a1511e6bSjoyce mcintosh 		break;
506a1511e6bSjoyce mcintosh 	}
507da6c28aaSamw 	mutex_exit(&od->d_mutex);
5087f667e74Sjose borrego }
509da6c28aaSamw 
510da6c28aaSamw /*
5117f667e74Sjose borrego  * smb_odir_read
512da6c28aaSamw  *
5137f667e74Sjose borrego  * Find the next directory entry matching the search pattern.
5147f667e74Sjose borrego  * No search attribute matching is performed.
5157f667e74Sjose borrego  *
5167f667e74Sjose borrego  * Returns:
5177f667e74Sjose borrego  *  0 - success.
5187f667e74Sjose borrego  *      - If a matching entry was found eof will be B_FALSE and
5197f667e74Sjose borrego  *        odirent will be populated.
520a90cf9f2SGordon Ross  * ENOENT
521a90cf9f2SGordon Ross  *      - If we've scanned to the end, eof will be B_TRUE.
522a90cf9f2SGordon Ross  * errno - other errors
523da6c28aaSamw  */
5247f667e74Sjose borrego int
5257f667e74Sjose borrego smb_odir_read(smb_request_t *sr, smb_odir_t *od,
5267f667e74Sjose borrego     smb_odirent_t *odirent, boolean_t *eof)
527da6c28aaSamw {
5287f667e74Sjose borrego 	int		rc;
529da6c28aaSamw 
5307f667e74Sjose borrego 	ASSERT(sr);
5317f667e74Sjose borrego 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
532da6c28aaSamw 	ASSERT(od);
533da6c28aaSamw 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
5347f667e74Sjose borrego 	ASSERT(odirent);
535da6c28aaSamw 
536da6c28aaSamw 	mutex_enter(&od->d_mutex);
537a1511e6bSjoyce mcintosh 	ASSERT(od->d_refcnt > 0);
5387f667e74Sjose borrego 
539a1511e6bSjoyce mcintosh 	switch (od->d_state) {
540a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_IN_USE:
541a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_CLOSING:
542a1511e6bSjoyce mcintosh 		break;
543a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_OPEN:
544a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_CLOSED:
545a1511e6bSjoyce mcintosh 	default:
546da6c28aaSamw 		mutex_exit(&od->d_mutex);
547a90cf9f2SGordon Ross 		return (EBADF);
5487f667e74Sjose borrego 	}
5497f667e74Sjose borrego 
5507f667e74Sjose borrego 	for (;;) {
5517f667e74Sjose borrego 		if ((rc = smb_odir_next_odirent(od, odirent)) != 0)
552da6c28aaSamw 			break;
553cb174861Sjoyce mcintosh 		if (smb_odir_match_name(od, odirent))
554da6c28aaSamw 			break;
555da6c28aaSamw 	}
5567f667e74Sjose borrego 
5577f667e74Sjose borrego 	mutex_exit(&od->d_mutex);
5587f667e74Sjose borrego 
5597f667e74Sjose borrego 	switch (rc) {
5607f667e74Sjose borrego 	case 0:
5617f667e74Sjose borrego 		*eof = B_FALSE;
5627f667e74Sjose borrego 		return (0);
5637f667e74Sjose borrego 	case ENOENT:
5647f667e74Sjose borrego 		*eof = B_TRUE;
565a90cf9f2SGordon Ross 		/* FALLTHROUGH */
5667f667e74Sjose borrego 	default:
567a90cf9f2SGordon Ross 		return (rc);
5687f667e74Sjose borrego 	}
5697f667e74Sjose borrego }
5707f667e74Sjose borrego 
5717f667e74Sjose borrego /*
5727f667e74Sjose borrego  * smb_odir_read_fileinfo
5737f667e74Sjose borrego  *
5747f667e74Sjose borrego  * Find the next directory entry matching the search pattern
5757f667e74Sjose borrego  * and attributes: od->d_pattern and od->d_sattr.
5767f667e74Sjose borrego  *
5777f667e74Sjose borrego  * If the search pattern specifies a single filename call
5787f667e74Sjose borrego  * smb_odir_single_fileinfo to get the file attributes and
5797f667e74Sjose borrego  * populate the caller's smb_fileinfo_t.
5807f667e74Sjose borrego  *
5817f667e74Sjose borrego  * If the search pattern contains wildcards call smb_odir_next_odirent
5827f667e74Sjose borrego  * to get the next directory entry then. Repeat until a matching
5837f667e74Sjose borrego  * filename is found. Call smb_odir_wildcard_fileinfo to get the
5847f667e74Sjose borrego  * file attributes and populate the caller's smb_fileinfo_t.
5857f667e74Sjose borrego  * This is repeated until a file matching the search criteria is found.
5867f667e74Sjose borrego  *
5877f667e74Sjose borrego  * Returns:
5887f667e74Sjose borrego  *  0 - success.
5897f667e74Sjose borrego  *      - If a matching entry was found eof will be B_FALSE and
5907f667e74Sjose borrego  *        fileinfo will be populated.
591a90cf9f2SGordon Ross  * ENOENT
592a90cf9f2SGordon Ross  *      - If at end of dir, eof will be B_TRUE.
593a90cf9f2SGordon Ross  * errno - other error
5947f667e74Sjose borrego  */
5957f667e74Sjose borrego int
5967f667e74Sjose borrego smb_odir_read_fileinfo(smb_request_t *sr, smb_odir_t *od,
597bfbce3c1SGordon Ross     smb_fileinfo_t *fileinfo, uint16_t *eof)
5987f667e74Sjose borrego {
599bbf6f00cSJordan Brown 	int		rc, errnum;
6007f667e74Sjose borrego 	smb_odirent_t	*odirent;
6017f667e74Sjose borrego 
6027f667e74Sjose borrego 	ASSERT(sr);
6037f667e74Sjose borrego 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
6047f667e74Sjose borrego 	ASSERT(od);
6057f667e74Sjose borrego 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
6067f667e74Sjose borrego 	ASSERT(fileinfo);
6077f667e74Sjose borrego 
6087f667e74Sjose borrego 	mutex_enter(&od->d_mutex);
609a1511e6bSjoyce mcintosh 	ASSERT(od->d_refcnt > 0);
6107f667e74Sjose borrego 
611a1511e6bSjoyce mcintosh 	switch (od->d_state) {
612a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_IN_USE:
613a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_CLOSING:
614a1511e6bSjoyce mcintosh 		break;
615a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_OPEN:
616a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_CLOSED:
617a1511e6bSjoyce mcintosh 	default:
6187f667e74Sjose borrego 		mutex_exit(&od->d_mutex);
619a90cf9f2SGordon Ross 		return (EBADF);
6207f667e74Sjose borrego 	}
6217f667e74Sjose borrego 
622bfbce3c1SGordon Ross 	if ((od->d_flags & SMB_ODIR_FLAG_WILDCARDS) == 0) {
6237f667e74Sjose borrego 		if (od->d_eof)
6247f667e74Sjose borrego 			rc = ENOENT;
6257f667e74Sjose borrego 		else
6267f667e74Sjose borrego 			rc = smb_odir_single_fileinfo(sr, od, fileinfo);
6277f667e74Sjose borrego 		od->d_eof = B_TRUE;
6287f667e74Sjose borrego 	} else {
6297f667e74Sjose borrego 		odirent = kmem_alloc(sizeof (smb_odirent_t), KM_SLEEP);
6307f667e74Sjose borrego 		for (;;) {
6317f667e74Sjose borrego 			bzero(fileinfo, sizeof (smb_fileinfo_t));
6327f667e74Sjose borrego 			if ((rc = smb_odir_next_odirent(od, odirent)) != 0)
6337f667e74Sjose borrego 				break;
6347f667e74Sjose borrego 
635bbf6f00cSJordan Brown 			/* skip non utf8 filename */
636bbf6f00cSJordan Brown 			if (u8_validate(odirent->od_name,
637bbf6f00cSJordan Brown 			    strlen(odirent->od_name), NULL,
638bbf6f00cSJordan Brown 			    U8_VALIDATE_ENTIRE, &errnum) < 0)
639bbf6f00cSJordan Brown 				continue;
640bbf6f00cSJordan Brown 
641cb174861Sjoyce mcintosh 			if (!smb_odir_match_name(od, odirent))
6427f667e74Sjose borrego 				continue;
6437f667e74Sjose borrego 
6447f667e74Sjose borrego 			rc = smb_odir_wildcard_fileinfo(sr, od, odirent,
6457f667e74Sjose borrego 			    fileinfo);
6467f667e74Sjose borrego 			if (rc == 0)
6477f667e74Sjose borrego 				break;
6487f667e74Sjose borrego 		}
6497f667e74Sjose borrego 		kmem_free(odirent, sizeof (smb_odirent_t));
6507f667e74Sjose borrego 	}
6517f667e74Sjose borrego 	mutex_exit(&od->d_mutex);
6527f667e74Sjose borrego 
6537f667e74Sjose borrego 	switch (rc) {
6547f667e74Sjose borrego 	case 0:
655bfbce3c1SGordon Ross 		*eof = 0;
6567f667e74Sjose borrego 		return (0);
6577f667e74Sjose borrego 	case ENOENT:
658bfbce3c1SGordon Ross 		*eof = 1;	/* per. FindFirst, FindNext spec. */
659a90cf9f2SGordon Ross 		/* FALLTHROUGH */
6607f667e74Sjose borrego 	default:
661a90cf9f2SGordon Ross 		return (rc);
6627f667e74Sjose borrego 	}
6637f667e74Sjose borrego }
6647f667e74Sjose borrego 
6657f667e74Sjose borrego /*
6667f667e74Sjose borrego  * smb_odir_read_streaminfo
6677f667e74Sjose borrego  *
6687f667e74Sjose borrego  * Find the next directory entry whose name begins with SMB_STREAM_PREFIX,
6697f667e74Sjose borrego  * and thus represents an NTFS named stream.
6707f667e74Sjose borrego  * No search attribute matching is performed.
6718b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States  * No case conflict name mangling is required for NTFS named stream names.
6727f667e74Sjose borrego  *
6737f667e74Sjose borrego  * Returns:
6747f667e74Sjose borrego  *  0 - success.
6757f667e74Sjose borrego  *      - If a matching entry was found eof will be B_FALSE and
6767f667e74Sjose borrego  *        sinfo will be populated.
6777f667e74Sjose borrego  *      - If there are no matching entries eof will be B_TRUE.
678a90cf9f2SGordon Ross  * errno - error
6797f667e74Sjose borrego  */
6807f667e74Sjose borrego int
6817f667e74Sjose borrego smb_odir_read_streaminfo(smb_request_t *sr, smb_odir_t *od,
6827f667e74Sjose borrego     smb_streaminfo_t *sinfo, boolean_t *eof)
6837f667e74Sjose borrego {
6847f667e74Sjose borrego 	int		rc;
685a90cf9f2SGordon Ross 	cred_t		*kcr;
6867f667e74Sjose borrego 	smb_odirent_t	*odirent;
687e3f2c991SKeyur Desai 	smb_node_t	*fnode;
6887f667e74Sjose borrego 	smb_attr_t	attr;
6897f667e74Sjose borrego 
6907f667e74Sjose borrego 	ASSERT(sr);
6917f667e74Sjose borrego 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
6927f667e74Sjose borrego 	ASSERT(od);
6937f667e74Sjose borrego 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
6947f667e74Sjose borrego 	ASSERT(sinfo);
6957f667e74Sjose borrego 
696a90cf9f2SGordon Ross 	kcr = zone_kcred();
697a90cf9f2SGordon Ross 
6987f667e74Sjose borrego 	mutex_enter(&od->d_mutex);
699a1511e6bSjoyce mcintosh 	ASSERT(od->d_refcnt > 0);
7007f667e74Sjose borrego 
701a1511e6bSjoyce mcintosh 	switch (od->d_state) {
702a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_IN_USE:
703a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_CLOSING:
704a1511e6bSjoyce mcintosh 		break;
705a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_OPEN:
706a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_CLOSED:
707a1511e6bSjoyce mcintosh 	default:
7087f667e74Sjose borrego 		mutex_exit(&od->d_mutex);
709a90cf9f2SGordon Ross 		return (EBADF);
7107f667e74Sjose borrego 	}
7117f667e74Sjose borrego 
7127f667e74Sjose borrego 	/* Check that odir represents an xattr directory */
7138b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (!(od->d_flags & SMB_ODIR_FLAG_XATTR)) {
7147f667e74Sjose borrego 		*eof = B_TRUE;
7157f667e74Sjose borrego 		mutex_exit(&od->d_mutex);
7167f667e74Sjose borrego 		return (0);
7177f667e74Sjose borrego 	}
7187f667e74Sjose borrego 
7197f667e74Sjose borrego 	odirent = kmem_alloc(sizeof (smb_odirent_t), KM_SLEEP);
7205fd03bc0SGordon Ross 	bzero(&attr, sizeof (attr));
7217f667e74Sjose borrego 
7227f667e74Sjose borrego 	for (;;) {
7237f667e74Sjose borrego 		bzero(sinfo, sizeof (smb_streaminfo_t));
7247f667e74Sjose borrego 		if ((rc = smb_odir_next_odirent(od, odirent)) != 0)
7257f667e74Sjose borrego 			break;
7267f667e74Sjose borrego 
7277f667e74Sjose borrego 		if (strncmp(odirent->od_name, SMB_STREAM_PREFIX,
7287f667e74Sjose borrego 		    SMB_STREAM_PREFIX_LEN)) {
7297f667e74Sjose borrego 			continue;
7307f667e74Sjose borrego 		}
7317f667e74Sjose borrego 
732*56a2adb6SPrashanth Badari 		/*
733*56a2adb6SPrashanth Badari 		 * Hide streams that would be restricted if the caller
734*56a2adb6SPrashanth Badari 		 * is also restricted.
735*56a2adb6SPrashanth Badari 		 */
736*56a2adb6SPrashanth Badari 		if ((od->d_flags & SMB_ODIR_FLAG_RESTRICTED) != 0 &&
737*56a2adb6SPrashanth Badari 		    smb_strname_restricted(odirent->od_name))
738*56a2adb6SPrashanth Badari 			continue;
739*56a2adb6SPrashanth Badari 
740e3f2c991SKeyur Desai 		rc = smb_fsop_lookup(sr, od->d_cred, 0, od->d_tree->t_snode,
741e3f2c991SKeyur Desai 		    od->d_dnode, odirent->od_name, &fnode);
7427f667e74Sjose borrego 		if (rc == 0) {
743a90cf9f2SGordon Ross 			/*
744a90cf9f2SGordon Ross 			 * We just need the file sizes, and don't want
745a90cf9f2SGordon Ross 			 * EACCES failures here, so use kcred and pass
746a90cf9f2SGordon Ross 			 * NULL as the sr to skip sr->fid-ofile checks.
747a90cf9f2SGordon Ross 			 */
7485fd03bc0SGordon Ross 			attr.sa_mask = SMB_AT_SIZE | SMB_AT_ALLOCSZ;
749a90cf9f2SGordon Ross 			rc = smb_node_getattr(NULL, fnode, kcr, NULL, &attr);
750e3f2c991SKeyur Desai 			smb_node_release(fnode);
7517f667e74Sjose borrego 		}
7527f667e74Sjose borrego 
7537f667e74Sjose borrego 		if (rc == 0) {
7547f667e74Sjose borrego 			(void) strlcpy(sinfo->si_name,
7557f667e74Sjose borrego 			    odirent->od_name + SMB_STREAM_PREFIX_LEN,
7567f667e74Sjose borrego 			    sizeof (sinfo->si_name));
7577f667e74Sjose borrego 			sinfo->si_size = attr.sa_vattr.va_size;
758e3f2c991SKeyur Desai 			sinfo->si_alloc_size = attr.sa_allocsz;
7597f667e74Sjose borrego 			break;
7607f667e74Sjose borrego 		}
7617f667e74Sjose borrego 	}
7627f667e74Sjose borrego 	mutex_exit(&od->d_mutex);
7637f667e74Sjose borrego 
7647f667e74Sjose borrego 	kmem_free(odirent, sizeof (smb_odirent_t));
7657f667e74Sjose borrego 
7667f667e74Sjose borrego 	switch (rc) {
7677f667e74Sjose borrego 	case 0:
7687f667e74Sjose borrego 		*eof = B_FALSE;
7697f667e74Sjose borrego 		return (0);
7707f667e74Sjose borrego 	case ENOENT:
7717f667e74Sjose borrego 		*eof = B_TRUE;
7727f667e74Sjose borrego 		return (0);
7737f667e74Sjose borrego 	default:
774a90cf9f2SGordon Ross 		return (rc);
7757f667e74Sjose borrego 	}
7767f667e74Sjose borrego }
7777f667e74Sjose borrego 
7787f667e74Sjose borrego /*
7797f667e74Sjose borrego  * smb_odir_save_cookie
7807f667e74Sjose borrego  *
7817f667e74Sjose borrego  * Callers can save up to SMB_MAX_SEARCH cookies in the odir
7827f667e74Sjose borrego  * to be used as resume points for a 'find next' request.
7837f667e74Sjose borrego  */
7847f667e74Sjose borrego void
7857f667e74Sjose borrego smb_odir_save_cookie(smb_odir_t *od, int idx, uint32_t cookie)
7867f667e74Sjose borrego {
7877f667e74Sjose borrego 	ASSERT(od);
7887f667e74Sjose borrego 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
7897f667e74Sjose borrego 	ASSERT(idx >= 0 && idx < SMB_MAX_SEARCH);
7907f667e74Sjose borrego 
7917f667e74Sjose borrego 	mutex_enter(&od->d_mutex);
7927f667e74Sjose borrego 	od->d_cookies[idx] = cookie;
7937f667e74Sjose borrego 	mutex_exit(&od->d_mutex);
7947f667e74Sjose borrego }
7957f667e74Sjose borrego 
7967f667e74Sjose borrego /*
797bfbce3c1SGordon Ross  * smb_odir_save_fname
798bfbce3c1SGordon Ross  *
799bfbce3c1SGordon Ross  * Save a filename / offset pair, which are basically a
800bfbce3c1SGordon Ross  * one entry cache.  See smb_com_trans2_find_next2.
801bfbce3c1SGordon Ross  */
802bfbce3c1SGordon Ross void
803bfbce3c1SGordon Ross smb_odir_save_fname(smb_odir_t *od, uint32_t cookie, const char *fname)
804bfbce3c1SGordon Ross {
805bfbce3c1SGordon Ross 	ASSERT(od);
806bfbce3c1SGordon Ross 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
807bfbce3c1SGordon Ross 
808bfbce3c1SGordon Ross 	mutex_enter(&od->d_mutex);
809bfbce3c1SGordon Ross 
810bfbce3c1SGordon Ross 	od->d_last_cookie = cookie;
811bfbce3c1SGordon Ross 	bzero(od->d_last_name, MAXNAMELEN);
812bfbce3c1SGordon Ross 	if (fname != NULL)
813bfbce3c1SGordon Ross 		(void) strlcpy(od->d_last_name, fname, MAXNAMELEN);
814bfbce3c1SGordon Ross 
815bfbce3c1SGordon Ross 	mutex_exit(&od->d_mutex);
816bfbce3c1SGordon Ross }
817bfbce3c1SGordon Ross 
818bfbce3c1SGordon Ross /*
8197f667e74Sjose borrego  * smb_odir_resume_at
8207f667e74Sjose borrego  *
821a90cf9f2SGordon Ross  * If SMB_ODIR_FLAG_WILDCARDS is not set, and we're rewinding,
822a90cf9f2SGordon Ross  * assume we're no longer at EOF.
823eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States  *
824eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States  * Wildcard searching can be resumed from:
8257f667e74Sjose borrego  * - the cookie saved at a specified index (SMBsearch, SMBfind).
8267f667e74Sjose borrego  * - a specified cookie (SMB_trans2_find)
8277f667e74Sjose borrego  * - a specified filename (SMB_trans2_find) - NOT SUPPORTED.
8287f667e74Sjose borrego  *   Defaults to continuing from where the last search ended.
8297f667e74Sjose borrego  *
8307f667e74Sjose borrego  * Continuation from where the last search ended (SMB_trans2_find)
8317f667e74Sjose borrego  * is implemented by saving the last cookie at a specific index (0)
8327f667e74Sjose borrego  * smb_odir_resume_at indicates a new request, so reset od->d_bufptr
8337f667e74Sjose borrego  * and d_eof to force a vop_readdir.
8347f667e74Sjose borrego  */
8357f667e74Sjose borrego void
8367f667e74Sjose borrego smb_odir_resume_at(smb_odir_t *od, smb_odir_resume_t *resume)
8377f667e74Sjose borrego {
838a90cf9f2SGordon Ross 	uint64_t save_offset;
839a90cf9f2SGordon Ross 
8407f667e74Sjose borrego 	ASSERT(od);
8417f667e74Sjose borrego 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
8427f667e74Sjose borrego 	ASSERT(resume);
8437f667e74Sjose borrego 
844eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 	if ((od->d_flags & SMB_ODIR_FLAG_WILDCARDS) == 0) {
845a90cf9f2SGordon Ross 		if (resume->or_type == SMB_ODIR_RESUME_COOKIE)
846a90cf9f2SGordon Ross 			od->d_eof = B_FALSE;
847eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 		return;
848eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 	}
849a90cf9f2SGordon Ross 	mutex_enter(&od->d_mutex);
850eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 
851a90cf9f2SGordon Ross 	save_offset = od->d_offset;
8527f667e74Sjose borrego 	switch (resume->or_type) {
853bfbce3c1SGordon Ross 
854bfbce3c1SGordon Ross 	default:
855bfbce3c1SGordon Ross 	case SMB_ODIR_RESUME_CONT:
856bfbce3c1SGordon Ross 		/* Continue where we left off. */
857bfbce3c1SGordon Ross 		break;
858bfbce3c1SGordon Ross 
8597f667e74Sjose borrego 	case SMB_ODIR_RESUME_IDX:
860bfbce3c1SGordon Ross 		/*
861bfbce3c1SGordon Ross 		 * This is used only by the (ancient) SMB_SEARCH.
862bfbce3c1SGordon Ross 		 * Modern clients use trans2 FindFirst, FindNext.
863bfbce3c1SGordon Ross 		 */
8647f667e74Sjose borrego 		ASSERT(resume->or_idx >= 0);
8657f667e74Sjose borrego 		ASSERT(resume->or_idx < SMB_MAX_SEARCH);
8667f667e74Sjose borrego 
8677f667e74Sjose borrego 		if ((resume->or_idx < 0) ||
8687f667e74Sjose borrego 		    (resume->or_idx >= SMB_MAX_SEARCH)) {
8697f667e74Sjose borrego 			resume->or_idx = 0;
8707f667e74Sjose borrego 		}
8717f667e74Sjose borrego 		od->d_offset = od->d_cookies[resume->or_idx];
8727f667e74Sjose borrego 		break;
873bfbce3c1SGordon Ross 
8747f667e74Sjose borrego 	case SMB_ODIR_RESUME_COOKIE:
8757f667e74Sjose borrego 		od->d_offset = resume->or_cookie;
8767f667e74Sjose borrego 		break;
877bfbce3c1SGordon Ross 
8787f667e74Sjose borrego 	case SMB_ODIR_RESUME_FNAME:
879bfbce3c1SGordon Ross 		/*
880bfbce3c1SGordon Ross 		 * If the name matches the last one saved,
881bfbce3c1SGordon Ross 		 * use the offset that was saved with it in
882bfbce3c1SGordon Ross 		 * the odir.  Otherwise use the cookie value
883bfbce3c1SGordon Ross 		 * in the resume data from the client.
884bfbce3c1SGordon Ross 		 */
885bfbce3c1SGordon Ross 		if (strcmp(resume->or_fname, od->d_last_name) &&
886bfbce3c1SGordon Ross 		    od->d_last_cookie != 0) {
887bfbce3c1SGordon Ross 			od->d_offset = od->d_last_cookie;
888bfbce3c1SGordon Ross 		} else if (resume->or_cookie != 0) {
889bfbce3c1SGordon Ross 			od->d_offset = resume->or_cookie;
890bfbce3c1SGordon Ross 		} /* else continue where we left off */
8917f667e74Sjose borrego 		break;
8927f667e74Sjose borrego 	}
8937f667e74Sjose borrego 
894a90cf9f2SGordon Ross 	if (od->d_offset != save_offset) {
8957f667e74Sjose borrego 		/* Force a vop_readdir to refresh d_buf */
8967f667e74Sjose borrego 		od->d_bufptr = NULL;
8977f667e74Sjose borrego 		od->d_eof = B_FALSE;
898a90cf9f2SGordon Ross 	}
8997f667e74Sjose borrego 
9007f667e74Sjose borrego 	mutex_exit(&od->d_mutex);
9017f667e74Sjose borrego }
9027f667e74Sjose borrego 
9037f667e74Sjose borrego 
9047f667e74Sjose borrego /* *** static functions *** */
9057f667e74Sjose borrego 
9067f667e74Sjose borrego /*
9077f667e74Sjose borrego  * smb_odir_create
9087f667e74Sjose borrego  * Allocate and populate an odir obect and add it to the tree's list.
9097f667e74Sjose borrego  */
910a90cf9f2SGordon Ross static smb_odir_t *
9117f667e74Sjose borrego smb_odir_create(smb_request_t *sr, smb_node_t *dnode,
912a90cf9f2SGordon Ross     const char *pattern, uint16_t sattr, uint16_t odid, cred_t *cr)
9137f667e74Sjose borrego {
9147f667e74Sjose borrego 	smb_odir_t	*od;
9157f667e74Sjose borrego 	smb_tree_t	*tree;
9167f667e74Sjose borrego 
9177f667e74Sjose borrego 	ASSERT(sr);
9187f667e74Sjose borrego 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
9197f667e74Sjose borrego 	ASSERT(sr->tid_tree);
9207f667e74Sjose borrego 	ASSERT(sr->tid_tree->t_magic == SMB_TREE_MAGIC);
9217f667e74Sjose borrego 	ASSERT(dnode);
9227f667e74Sjose borrego 	ASSERT(dnode->n_magic == SMB_NODE_MAGIC);
9237f667e74Sjose borrego 
9247f667e74Sjose borrego 	tree = sr->tid_tree;
9257f667e74Sjose borrego 
9268622ec45SGordon Ross 	od = kmem_cache_alloc(smb_cache_odir, KM_SLEEP);
9277f667e74Sjose borrego 	bzero(od, sizeof (smb_odir_t));
9287f667e74Sjose borrego 
9297f667e74Sjose borrego 	mutex_init(&od->d_mutex, NULL, MUTEX_DEFAULT, NULL);
930a90cf9f2SGordon Ross 
931a90cf9f2SGordon Ross 	/*
932a90cf9f2SGordon Ross 	 * Return this to the caller as if they had done
933a90cf9f2SGordon Ross 	 * smb_tree_lookup_odir() to obtain the odir.
934a90cf9f2SGordon Ross 	 */
935a90cf9f2SGordon Ross 	od->d_refcnt = 1;
936a90cf9f2SGordon Ross 	od->d_state = SMB_ODIR_STATE_IN_USE;
9377f667e74Sjose borrego 	od->d_magic = SMB_ODIR_MAGIC;
9387f667e74Sjose borrego 	od->d_opened_by_pid = sr->smb_pid;
9397f667e74Sjose borrego 	od->d_session = tree->t_session;
940eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 	od->d_cred = cr;
9413b13a1efSThomas Keiser 	/*
9423b13a1efSThomas Keiser 	 * grab a ref for od->d_user
9433b13a1efSThomas Keiser 	 * released in  smb_odir_delete()
9443b13a1efSThomas Keiser 	 */
9453b13a1efSThomas Keiser 	smb_user_hold_internal(sr->uid_user);
9463b13a1efSThomas Keiser 	od->d_user = sr->uid_user;
9477f667e74Sjose borrego 	od->d_tree = tree;
9487f667e74Sjose borrego 	od->d_dnode = dnode;
9497f667e74Sjose borrego 	smb_node_ref(dnode);
9507f667e74Sjose borrego 	od->d_odid = odid;
9517f667e74Sjose borrego 	od->d_sattr = sattr;
9527f667e74Sjose borrego 	(void) strlcpy(od->d_pattern, pattern, sizeof (od->d_pattern));
9538b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	od->d_flags = 0;
954fe1c642dSBill Krier 	if (smb_contains_wildcards(od->d_pattern))
9558b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		od->d_flags |= SMB_ODIR_FLAG_WILDCARDS;
9568b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (vfs_has_feature(dnode->vp->v_vfsp, VFSFT_DIRENTFLAGS))
9578b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		od->d_flags |= SMB_ODIR_FLAG_EDIRENT;
9588b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (smb_tree_has_feature(tree, SMB_TREE_CASEINSENSITIVE))
9598b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		od->d_flags |= SMB_ODIR_FLAG_IGNORE_CASE;
960cb174861Sjoyce mcintosh 	if (smb_tree_has_feature(tree, SMB_TREE_SHORTNAMES))
961cb174861Sjoyce mcintosh 		od->d_flags |= SMB_ODIR_FLAG_SHORTNAMES;
9628b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (SMB_TREE_SUPPORTS_CATIA(sr))
9638b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		od->d_flags |= SMB_ODIR_FLAG_CATIA;
964e3f2c991SKeyur Desai 	if (SMB_TREE_SUPPORTS_ABE(sr))
965e3f2c991SKeyur Desai 		od->d_flags |= SMB_ODIR_FLAG_ABE;
966cb174861Sjoyce mcintosh 	if (dnode->flags & NODE_XATTR_DIR)
967cb174861Sjoyce mcintosh 		od->d_flags |= SMB_ODIR_FLAG_XATTR;
9687f667e74Sjose borrego 	od->d_eof = B_FALSE;
9697f667e74Sjose borrego 
9707f667e74Sjose borrego 	smb_llist_enter(&tree->t_odir_list, RW_WRITER);
9717f667e74Sjose borrego 	smb_llist_insert_tail(&tree->t_odir_list, od);
9727f667e74Sjose borrego 	smb_llist_exit(&tree->t_odir_list);
9737f667e74Sjose borrego 
9747f667e74Sjose borrego 	atomic_inc_32(&tree->t_session->s_dir_cnt);
975a90cf9f2SGordon Ross 	return (od);
976a90cf9f2SGordon Ross }
977a90cf9f2SGordon Ross 
978a90cf9f2SGordon Ross /*
979a90cf9f2SGordon Ross  * Set a new pattern, attributes, and rewind.
980a90cf9f2SGordon Ross  */
981a90cf9f2SGordon Ross void
982a90cf9f2SGordon Ross smb_odir_reopen(smb_odir_t *od, const char *pattern, uint16_t sattr)
983a90cf9f2SGordon Ross {
984a90cf9f2SGordon Ross 
985a90cf9f2SGordon Ross 	SMB_ODIR_VALID(od);
986a90cf9f2SGordon Ross 
987a90cf9f2SGordon Ross 	mutex_enter(&od->d_mutex);
988a90cf9f2SGordon Ross 	od->d_sattr = sattr;
989a90cf9f2SGordon Ross 	(void) strlcpy(od->d_pattern, pattern, sizeof (od->d_pattern));
990a90cf9f2SGordon Ross 	if (smb_contains_wildcards(od->d_pattern))
991a90cf9f2SGordon Ross 		od->d_flags |= SMB_ODIR_FLAG_WILDCARDS;
992a90cf9f2SGordon Ross 	else
993a90cf9f2SGordon Ross 		od->d_flags &= ~SMB_ODIR_FLAG_WILDCARDS;
994a90cf9f2SGordon Ross 
995a90cf9f2SGordon Ross 	/* Internal smb_odir_resume_at */
996a90cf9f2SGordon Ross 	od->d_offset = 0;
997a90cf9f2SGordon Ross 	od->d_bufptr = NULL;
998a90cf9f2SGordon Ross 	od->d_eof = B_FALSE;
999a90cf9f2SGordon Ross 
1000a90cf9f2SGordon Ross 	mutex_exit(&od->d_mutex);
1001da6c28aaSamw }
1002da6c28aaSamw 
1003da6c28aaSamw /*
10049fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * Delete an odir.
10057f667e74Sjose borrego  *
10069fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * Remove the odir from the tree list before freeing resources
10079fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * associated with the odir.
1008da6c28aaSamw  */
1009811599a4SMatt Barden static void
10109fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States smb_odir_delete(void *arg)
1011da6c28aaSamw {
10129fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	smb_tree_t	*tree;
10139fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	smb_odir_t	*od = (smb_odir_t *)arg;
10149fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
10159fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	SMB_ODIR_VALID(od);
10169fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	ASSERT(od->d_refcnt == 0);
1017da6c28aaSamw 	ASSERT(od->d_state == SMB_ODIR_STATE_CLOSED);
1018da6c28aaSamw 
10199fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	tree = od->d_tree;
10209fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	smb_llist_enter(&tree->t_odir_list, RW_WRITER);
10219fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	smb_llist_remove(&tree->t_odir_list, od);
1022a90cf9f2SGordon Ross 	if (od->d_odid != 0)
10239fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		smb_idpool_free(&tree->t_odid_pool, od->d_odid);
10249fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	atomic_dec_32(&tree->t_session->s_dir_cnt);
10259fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	smb_llist_exit(&tree->t_odir_list);
10269fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
1027811599a4SMatt Barden 	/*
1028811599a4SMatt Barden 	 * This odir is no longer on t_odir_list, however...
1029811599a4SMatt Barden 	 *
1030811599a4SMatt Barden 	 * This is called via smb_llist_post, which means it may run
1031811599a4SMatt Barden 	 * BEFORE smb_odir_release drops d_mutex (if another thread
1032811599a4SMatt Barden 	 * flushes the delete queue before we do).  Synchronize.
1033811599a4SMatt Barden 	 */
10349fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	mutex_enter(&od->d_mutex);
10359fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	mutex_exit(&od->d_mutex);
1036da6c28aaSamw 
10377f667e74Sjose borrego 	od->d_magic = 0;
10387f667e74Sjose borrego 	smb_node_release(od->d_dnode);
10393b13a1efSThomas Keiser 	smb_user_release(od->d_user);
1040da6c28aaSamw 	mutex_destroy(&od->d_mutex);
10418622ec45SGordon Ross 	kmem_cache_free(smb_cache_odir, od);
1042da6c28aaSamw }
10437f667e74Sjose borrego 
10447f667e74Sjose borrego /*
10457f667e74Sjose borrego  * smb_odir_next_odirent
10467f667e74Sjose borrego  *
10477f667e74Sjose borrego  * Find the next directory entry in d_buf. If d_bufptr is NULL (buffer
10487f667e74Sjose borrego  * is empty or we've reached the end of it), read the next set of
10497f667e74Sjose borrego  * entries from the file system (vop_readdir).
10507f667e74Sjose borrego  *
10517f667e74Sjose borrego  * File systems which support VFSFT_EDIRENT_FLAGS will return the
10527f667e74Sjose borrego  * directory entries as a buffer of edirent_t structure. Others will
10537f667e74Sjose borrego  * return a buffer of dirent64_t structures.  For simplicity translate
10547f667e74Sjose borrego  * the data into an smb_odirent_t structure.
10557f667e74Sjose borrego  * The ed_name/d_name in d_buf is NULL terminated by the file system.
10567f667e74Sjose borrego  *
10577f667e74Sjose borrego  * Some file systems can have directories larger than SMB_MAXDIRSIZE.
10588b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States  * If the odirent offset >= SMB_MAXDIRSIZE return ENOENT and set d_eof
10598b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States  * to true to stop subsequent calls to smb_vop_readdir.
10607f667e74Sjose borrego  *
10617f667e74Sjose borrego  * Returns:
10627f667e74Sjose borrego  *      0 - success. odirent is populated with the next directory entry
10637f667e74Sjose borrego  * ENOENT - no more directory entries
10647f667e74Sjose borrego  *  errno - error
10657f667e74Sjose borrego  */
10667f667e74Sjose borrego static int
10677f667e74Sjose borrego smb_odir_next_odirent(smb_odir_t *od, smb_odirent_t *odirent)
10687f667e74Sjose borrego {
10697f667e74Sjose borrego 	int		rc;
10707f667e74Sjose borrego 	int		reclen;
10717f667e74Sjose borrego 	int		eof;
10727f667e74Sjose borrego 	dirent64_t	*dp;
10737f667e74Sjose borrego 	edirent_t	*edp;
10748b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	char		*np;
1075d2488fe8SGordon Ross 	uint32_t	rddir_flags = 0;
10767f667e74Sjose borrego 
10777f667e74Sjose borrego 	ASSERT(MUTEX_HELD(&od->d_mutex));
10787f667e74Sjose borrego 
107948557149Sjoyce mcintosh 	bzero(odirent, sizeof (smb_odirent_t));
108048557149Sjoyce mcintosh 
1081d2488fe8SGordon Ross 	if (od->d_flags & SMB_ODIR_FLAG_ABE)
1082d2488fe8SGordon Ross 		rddir_flags |= SMB_ABE;
1083d2488fe8SGordon Ross 	if (od->d_flags & SMB_ODIR_FLAG_EDIRENT)
1084d2488fe8SGordon Ross 		rddir_flags |= SMB_EDIRENT;
1085d2488fe8SGordon Ross 
10867f667e74Sjose borrego 	if (od->d_bufptr != NULL) {
10878b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		if (od->d_flags & SMB_ODIR_FLAG_EDIRENT)
10888b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 			reclen = od->d_edp->ed_reclen;
10898b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		else
10908b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 			reclen = od->d_dp->d_reclen;
10917f667e74Sjose borrego 
10927f667e74Sjose borrego 		if (reclen == 0) {
10937f667e74Sjose borrego 			od->d_bufptr = NULL;
10947f667e74Sjose borrego 		} else {
10957f667e74Sjose borrego 			od->d_bufptr += reclen;
10967f667e74Sjose borrego 			if (od->d_bufptr >= od->d_buf + od->d_bufsize)
10977f667e74Sjose borrego 				od->d_bufptr = NULL;
10987f667e74Sjose borrego 		}
10997f667e74Sjose borrego 	}
11007f667e74Sjose borrego 
11017f667e74Sjose borrego 	if (od->d_bufptr == NULL) {
11027f667e74Sjose borrego 		if (od->d_eof)
11037f667e74Sjose borrego 			return (ENOENT);
11047f667e74Sjose borrego 
11057f667e74Sjose borrego 		od->d_bufsize = sizeof (od->d_buf);
11067f667e74Sjose borrego 
11077f667e74Sjose borrego 		rc = smb_vop_readdir(od->d_dnode->vp, od->d_offset,
1108d2488fe8SGordon Ross 		    od->d_buf, &od->d_bufsize, &eof, rddir_flags, od->d_cred);
11097f667e74Sjose borrego 
11107f667e74Sjose borrego 		if ((rc == 0) && (od->d_bufsize == 0))
11117f667e74Sjose borrego 			rc = ENOENT;
11127f667e74Sjose borrego 
11137f667e74Sjose borrego 		if (rc != 0) {
11147f667e74Sjose borrego 			od->d_bufptr = NULL;
11157f667e74Sjose borrego 			od->d_bufsize = 0;
11167f667e74Sjose borrego 			return (rc);
11177f667e74Sjose borrego 		}
11187f667e74Sjose borrego 
11197f667e74Sjose borrego 		od->d_eof = (eof != 0);
11207f667e74Sjose borrego 		od->d_bufptr = od->d_buf;
11217f667e74Sjose borrego 	}
11227f667e74Sjose borrego 
11238b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (od->d_flags & SMB_ODIR_FLAG_EDIRENT)
11248b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		od->d_offset = od->d_edp->ed_off;
11258b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	else
11268b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		od->d_offset = od->d_dp->d_off;
11278b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 
11287f667e74Sjose borrego 	if (od->d_offset >= SMB_MAXDIRSIZE) {
11297f667e74Sjose borrego 		od->d_bufptr = NULL;
11307f667e74Sjose borrego 		od->d_bufsize = 0;
11318b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		od->d_eof = B_TRUE;
11327f667e74Sjose borrego 		return (ENOENT);
11337f667e74Sjose borrego 	}
11347f667e74Sjose borrego 
11358b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (od->d_flags & SMB_ODIR_FLAG_EDIRENT) {
11367f667e74Sjose borrego 		edp = od->d_edp;
11377f667e74Sjose borrego 		odirent->od_ino = edp->ed_ino;
11387f667e74Sjose borrego 		odirent->od_eflags = edp->ed_eflags;
11398b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		np = edp->ed_name;
11407f667e74Sjose borrego 	} else {
11417f667e74Sjose borrego 		dp = od->d_dp;
11427f667e74Sjose borrego 		odirent->od_ino = dp->d_ino;
11437f667e74Sjose borrego 		odirent->od_eflags = 0;
11448b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		np =  dp->d_name;
11458b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	}
11468b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 
11478b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	if ((od->d_flags & SMB_ODIR_FLAG_CATIA) &&
11488b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	    ((od->d_flags & SMB_ODIR_FLAG_XATTR) == 0)) {
11498b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		smb_vop_catia_v4tov5(np, odirent->od_name,
11508b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		    sizeof (odirent->od_name));
11518b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	} else {
11528b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		(void) strlcpy(odirent->od_name, np,
11537f667e74Sjose borrego 		    sizeof (odirent->od_name));
11547f667e74Sjose borrego 	}
11557f667e74Sjose borrego 
11567f667e74Sjose borrego 	return (0);
11577f667e74Sjose borrego }
11587f667e74Sjose borrego 
11597f667e74Sjose borrego /*
11607f667e74Sjose borrego  * smb_odir_single_fileinfo
11617f667e74Sjose borrego  *
11627f667e74Sjose borrego  * Lookup the file identified by od->d_pattern.
11637f667e74Sjose borrego  *
11647f667e74Sjose borrego  * If the looked up file is a link, we attempt to lookup the link target
11657f667e74Sjose borrego  * to use its attributes in place of those of the files's.
11667f667e74Sjose borrego  * If we fail to lookup the target of the link we use the original
11677f667e74Sjose borrego  * file's attributes.
11687f667e74Sjose borrego  * Check if the attributes match the search attributes.
11697f667e74Sjose borrego  *
11707f667e74Sjose borrego  * Returns: 0 - success
11717f667e74Sjose borrego  *     ENOENT - no match
11727f667e74Sjose borrego  *      errno - error
11737f667e74Sjose borrego  */
11747f667e74Sjose borrego static int
11757f667e74Sjose borrego smb_odir_single_fileinfo(smb_request_t *sr, smb_odir_t *od,
11767f667e74Sjose borrego     smb_fileinfo_t *fileinfo)
11777f667e74Sjose borrego {
11787f667e74Sjose borrego 	int		rc;
11797f667e74Sjose borrego 	smb_node_t	*fnode, *tgt_node;
1180037cac00Sjoyce mcintosh 	smb_attr_t	attr;
1181cb174861Sjoyce mcintosh 	ino64_t		fid;
11827f667e74Sjose borrego 	char		*name;
11838b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	boolean_t	case_conflict = B_FALSE;
11848b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	int		lookup_flags, flags = 0;
11858b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	vnode_t		*vp;
11867f667e74Sjose borrego 
11877f667e74Sjose borrego 	ASSERT(sr);
11887f667e74Sjose borrego 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
11897f667e74Sjose borrego 	ASSERT(od);
11907f667e74Sjose borrego 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
11917f667e74Sjose borrego 
11927f667e74Sjose borrego 	ASSERT(MUTEX_HELD(&od->d_mutex));
11937f667e74Sjose borrego 	bzero(fileinfo, sizeof (smb_fileinfo_t));
11947f667e74Sjose borrego 
1195eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 	rc = smb_fsop_lookup(sr, od->d_cred, 0, od->d_tree->t_snode,
1196037cac00Sjoyce mcintosh 	    od->d_dnode, od->d_pattern, &fnode);
11977f667e74Sjose borrego 	if (rc != 0)
11987f667e74Sjose borrego 		return (rc);
11997f667e74Sjose borrego 
12008b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	/*
12018b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	 * If case sensitive, do a case insensitive smb_vop_lookup to
12028b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	 * check for case conflict
12038b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	 */
12048b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (od->d_flags & SMB_ODIR_FLAG_IGNORE_CASE) {
12058b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		lookup_flags = SMB_IGNORE_CASE;
12068b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		if (od->d_flags & SMB_ODIR_FLAG_CATIA)
12078b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 			lookup_flags |= SMB_CATIA;
12087f667e74Sjose borrego 
12098b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		rc = smb_vop_lookup(od->d_dnode->vp, fnode->od_name, &vp,
12108b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		    NULL, lookup_flags, &flags, od->d_tree->t_snode->vp,
12119fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		    NULL, od->d_cred);
12128b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		if (rc != 0)
12138b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 			return (rc);
12148b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		VN_RELE(vp);
12158b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 
12168b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		if (flags & ED_CASE_CONFLICT)
12178b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 			case_conflict = B_TRUE;
12188b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	}
12198b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 
12205fd03bc0SGordon Ross 	bzero(&attr, sizeof (attr));
12215fd03bc0SGordon Ross 	attr.sa_mask = SMB_AT_ALL;
1222a90cf9f2SGordon Ross 	rc = smb_node_getattr(NULL, fnode, zone_kcred(), NULL, &attr);
12235fd03bc0SGordon Ross 	if (rc != 0) {
1224037cac00Sjoyce mcintosh 		smb_node_release(fnode);
1225037cac00Sjoyce mcintosh 		return (rc);
1226037cac00Sjoyce mcintosh 	}
1227037cac00Sjoyce mcintosh 
12287f667e74Sjose borrego 
12297f667e74Sjose borrego 	/* follow link to get target node & attr */
12309fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	if (smb_node_is_symlink(fnode) &&
12319fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	    smb_odir_lookup_link(sr, od, fnode->od_name, &tgt_node)) {
12327f667e74Sjose borrego 		smb_node_release(fnode);
12337f667e74Sjose borrego 		fnode = tgt_node;
12345fd03bc0SGordon Ross 		attr.sa_mask = SMB_AT_ALL;
1235a90cf9f2SGordon Ross 		rc = smb_node_getattr(NULL, fnode, zone_kcred(), NULL, &attr);
12365fd03bc0SGordon Ross 		if (rc != 0) {
1237037cac00Sjoyce mcintosh 			smb_node_release(fnode);
1238037cac00Sjoyce mcintosh 			return (rc);
1239037cac00Sjoyce mcintosh 		}
12407f667e74Sjose borrego 	}
12417f667e74Sjose borrego 
12427f667e74Sjose borrego 	/* check search attributes */
1243037cac00Sjoyce mcintosh 	if (!smb_sattr_check(attr.sa_dosattr, od->d_sattr)) {
12447f667e74Sjose borrego 		smb_node_release(fnode);
12457f667e74Sjose borrego 		return (ENOENT);
12467f667e74Sjose borrego 	}
12477f667e74Sjose borrego 
1248148c5f43SAlan Wright 	name = fnode->od_name;
1249cb174861Sjoyce mcintosh 	if (od->d_flags & SMB_ODIR_FLAG_SHORTNAMES) {
1250cb174861Sjoyce mcintosh 		fid = attr.sa_vattr.va_nodeid;
1251cb174861Sjoyce mcintosh 		if (case_conflict || smb_needs_mangled(name)) {
1252cb174861Sjoyce mcintosh 			smb_mangle(name, fid, fileinfo->fi_shortname,
1253cb174861Sjoyce mcintosh 			    SMB_SHORTNAMELEN);
1254cb174861Sjoyce mcintosh 		}
1255148c5f43SAlan Wright 		if (case_conflict)
1256148c5f43SAlan Wright 			name = fileinfo->fi_shortname;
1257cb174861Sjoyce mcintosh 	}
1258148c5f43SAlan Wright 
1259148c5f43SAlan Wright 	(void) strlcpy(fileinfo->fi_name, name, sizeof (fileinfo->fi_name));
1260148c5f43SAlan Wright 
1261037cac00Sjoyce mcintosh 	fileinfo->fi_dosattr = attr.sa_dosattr;
1262037cac00Sjoyce mcintosh 	fileinfo->fi_nodeid = attr.sa_vattr.va_nodeid;
1263037cac00Sjoyce mcintosh 	fileinfo->fi_size = attr.sa_vattr.va_size;
1264e3f2c991SKeyur Desai 	fileinfo->fi_alloc_size = attr.sa_allocsz;
1265037cac00Sjoyce mcintosh 	fileinfo->fi_atime = attr.sa_vattr.va_atime;
1266037cac00Sjoyce mcintosh 	fileinfo->fi_mtime = attr.sa_vattr.va_mtime;
1267037cac00Sjoyce mcintosh 	fileinfo->fi_ctime = attr.sa_vattr.va_ctime;
1268037cac00Sjoyce mcintosh 	if (attr.sa_crtime.tv_sec)
1269037cac00Sjoyce mcintosh 		fileinfo->fi_crtime = attr.sa_crtime;
12707f667e74Sjose borrego 	else
1271037cac00Sjoyce mcintosh 		fileinfo->fi_crtime = attr.sa_vattr.va_mtime;
12727f667e74Sjose borrego 
12737f667e74Sjose borrego 	smb_node_release(fnode);
12747f667e74Sjose borrego 	return (0);
12757f667e74Sjose borrego }
12767f667e74Sjose borrego 
12777f667e74Sjose borrego /*
12787f667e74Sjose borrego  * smb_odir_wildcard_fileinfo
12797f667e74Sjose borrego  *
12807f667e74Sjose borrego  * odirent contains a directory entry, obtained from a vop_readdir.
12817f667e74Sjose borrego  * If a case conflict is identified the filename is mangled and the
1282148c5f43SAlan Wright  * shortname is used as 'name', in place of odirent->od_name.
12837f667e74Sjose borrego  *
12847f667e74Sjose borrego  * If the looked up file is a link, we attempt to lookup the link target
12857f667e74Sjose borrego  * to use its attributes in place of those of the files's.
12867f667e74Sjose borrego  * If we fail to lookup the target of the link we use the original
12877f667e74Sjose borrego  * file's attributes.
12887f667e74Sjose borrego  * Check if the attributes match the search attributes.
12897f667e74Sjose borrego  *
12907f667e74Sjose borrego  * Although some file systems can have directories larger than
12917f667e74Sjose borrego  * SMB_MAXDIRSIZE smb_odir_next_odirent ensures that no offset larger
12927f667e74Sjose borrego  * than SMB_MAXDIRSIZE is returned.  It is therefore safe to use the
12937f667e74Sjose borrego  * offset as the cookie (uint32_t).
12947f667e74Sjose borrego  *
12957f667e74Sjose borrego  * Returns: 0 - success
12967f667e74Sjose borrego  *     ENOENT - no match, proceed to next entry
12977f667e74Sjose borrego  *      errno - error
12987f667e74Sjose borrego  */
12997f667e74Sjose borrego static int
13007f667e74Sjose borrego smb_odir_wildcard_fileinfo(smb_request_t *sr, smb_odir_t *od,
13017f667e74Sjose borrego     smb_odirent_t *odirent, smb_fileinfo_t *fileinfo)
13027f667e74Sjose borrego {
13037f667e74Sjose borrego 	int		rc;
13045a485655SKevin Crowe 	cred_t		*cr;
13057f667e74Sjose borrego 	smb_node_t	*fnode, *tgt_node;
1306037cac00Sjoyce mcintosh 	smb_attr_t	attr;
13077f667e74Sjose borrego 	char		*name;
13087f667e74Sjose borrego 	boolean_t	case_conflict;
13097f667e74Sjose borrego 
13107f667e74Sjose borrego 	ASSERT(sr);
13117f667e74Sjose borrego 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
13127f667e74Sjose borrego 	ASSERT(od);
13137f667e74Sjose borrego 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
13147f667e74Sjose borrego 
13157f667e74Sjose borrego 	ASSERT(MUTEX_HELD(&od->d_mutex));
13167f667e74Sjose borrego 	bzero(fileinfo, sizeof (smb_fileinfo_t));
13177f667e74Sjose borrego 
1318148c5f43SAlan Wright 	rc = smb_fsop_lookup(sr, od->d_cred, SMB_CASE_SENSITIVE,
1319148c5f43SAlan Wright 	    od->d_tree->t_snode, od->d_dnode, odirent->od_name, &fnode);
13207f667e74Sjose borrego 	if (rc != 0)
13217f667e74Sjose borrego 		return (rc);
13227f667e74Sjose borrego 
13237f667e74Sjose borrego 	/* follow link to get target node & attr */
13249fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	if (smb_node_is_symlink(fnode) &&
1325148c5f43SAlan Wright 	    smb_odir_lookup_link(sr, od, odirent->od_name, &tgt_node)) {
13267f667e74Sjose borrego 		smb_node_release(fnode);
13277f667e74Sjose borrego 		fnode = tgt_node;
1328037cac00Sjoyce mcintosh 	}
1329037cac00Sjoyce mcintosh 
13309fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	/* skip system files */
13319fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	if (smb_node_is_system(fnode)) {
13329fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		smb_node_release(fnode);
13339fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		return (ENOENT);
13349fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	}
13359fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
13365a485655SKevin Crowe 	/*
13375a485655SKevin Crowe 	 * Windows directory listings return not only names, but
13385a485655SKevin Crowe 	 * also some attributes.  In Unix, you need some access to
13395a485655SKevin Crowe 	 * get those attributes.  Which credential should we use to
13405a485655SKevin Crowe 	 * get those?  If we're doing Access Based Enumeration (ABE)
13415a485655SKevin Crowe 	 * we want this getattr to fail, which will cause the caller
13425a485655SKevin Crowe 	 * to skip this entry.  If we're NOT doing ABE, we normally
13435a485655SKevin Crowe 	 * want to show all the directory entries (including their
13445a485655SKevin Crowe 	 * attributes) so we want this getattr to succeed!
13455a485655SKevin Crowe 	 */
13465a485655SKevin Crowe 	if (smb_tree_has_feature(od->d_tree, SMB_TREE_ABE))
13475a485655SKevin Crowe 		cr = od->d_cred;
13485a485655SKevin Crowe 	else
13495a485655SKevin Crowe 		cr = zone_kcred();
13505a485655SKevin Crowe 
13515fd03bc0SGordon Ross 	bzero(&attr, sizeof (attr));
13525fd03bc0SGordon Ross 	attr.sa_mask = SMB_AT_ALL;
13535a485655SKevin Crowe 	rc = smb_node_getattr(NULL, fnode, cr, NULL, &attr);
13545fd03bc0SGordon Ross 	if (rc != 0) {
1355037cac00Sjoyce mcintosh 		smb_node_release(fnode);
1356037cac00Sjoyce mcintosh 		return (rc);
13577f667e74Sjose borrego 	}
13587f667e74Sjose borrego 
13597f667e74Sjose borrego 	/* check search attributes */
1360037cac00Sjoyce mcintosh 	if (!smb_sattr_check(attr.sa_dosattr, od->d_sattr)) {
13617f667e74Sjose borrego 		smb_node_release(fnode);
13627f667e74Sjose borrego 		return (ENOENT);
13637f667e74Sjose borrego 	}
13647f667e74Sjose borrego 
1365cb174861Sjoyce mcintosh 	name = odirent->od_name;
1366cb174861Sjoyce mcintosh 	if (od->d_flags & SMB_ODIR_FLAG_SHORTNAMES) {
1367148c5f43SAlan Wright 		case_conflict = ((od->d_flags & SMB_ODIR_FLAG_IGNORE_CASE) &&
1368148c5f43SAlan Wright 		    (odirent->od_eflags & ED_CASE_CONFLICT));
1369cb174861Sjoyce mcintosh 		if (case_conflict || smb_needs_mangled(name)) {
1370cb174861Sjoyce mcintosh 			smb_mangle(name, odirent->od_ino,
1371148c5f43SAlan Wright 			    fileinfo->fi_shortname, SMB_SHORTNAMELEN);
1372148c5f43SAlan Wright 		}
1373cb174861Sjoyce mcintosh 		if (case_conflict)
1374cb174861Sjoyce mcintosh 			name = fileinfo->fi_shortname;
1375cb174861Sjoyce mcintosh 	}
1376148c5f43SAlan Wright 
1377148c5f43SAlan Wright 	(void) strlcpy(fileinfo->fi_name, name, sizeof (fileinfo->fi_name));
1378148c5f43SAlan Wright 
13797f667e74Sjose borrego 	fileinfo->fi_cookie = (uint32_t)od->d_offset;
1380037cac00Sjoyce mcintosh 	fileinfo->fi_dosattr = attr.sa_dosattr;
1381037cac00Sjoyce mcintosh 	fileinfo->fi_nodeid = attr.sa_vattr.va_nodeid;
1382037cac00Sjoyce mcintosh 	fileinfo->fi_size = attr.sa_vattr.va_size;
1383e3f2c991SKeyur Desai 	fileinfo->fi_alloc_size = attr.sa_allocsz;
1384037cac00Sjoyce mcintosh 	fileinfo->fi_atime = attr.sa_vattr.va_atime;
1385037cac00Sjoyce mcintosh 	fileinfo->fi_mtime = attr.sa_vattr.va_mtime;
1386037cac00Sjoyce mcintosh 	fileinfo->fi_ctime = attr.sa_vattr.va_ctime;
1387037cac00Sjoyce mcintosh 	if (attr.sa_crtime.tv_sec)
1388037cac00Sjoyce mcintosh 		fileinfo->fi_crtime = attr.sa_crtime;
13897f667e74Sjose borrego 	else
1390037cac00Sjoyce mcintosh 		fileinfo->fi_crtime = attr.sa_vattr.va_mtime;
13917f667e74Sjose borrego 
13927f667e74Sjose borrego 	smb_node_release(fnode);
13937f667e74Sjose borrego 	return (0);
13947f667e74Sjose borrego }
13957f667e74Sjose borrego 
13967f667e74Sjose borrego /*
13977f667e74Sjose borrego  * smb_odir_lookup_link
13987f667e74Sjose borrego  *
13997f667e74Sjose borrego  * If the file is a symlink we lookup the object to which the
14007f667e74Sjose borrego  * symlink refers so that we can return its attributes.
14017f667e74Sjose borrego  * This can cause a problem if a symlink in a sub-directory
14027f667e74Sjose borrego  * points to a parent directory (some UNIX GUI's create a symlink
14037f667e74Sjose borrego  * in $HOME/.desktop that points to the user's home directory).
14047f667e74Sjose borrego  * Some Windows applications (e.g. virus scanning) loop/hang
14057f667e74Sjose borrego  * trying to follow this recursive path and there is little
14067f667e74Sjose borrego  * we can do because the path is constructed on the client.
14077f667e74Sjose borrego  * smb_dirsymlink_enable allows an end-user to disable
14087f667e74Sjose borrego  * symlinks to directories. Symlinks to other object types
14097f667e74Sjose borrego  * should be unaffected.
14107f667e74Sjose borrego  *
14117f667e74Sjose borrego  * Returns: B_TRUE  - followed link. tgt_node and tgt_attr set
14127f667e74Sjose borrego  *          B_FALSE - link not followed
14137f667e74Sjose borrego  */
14147f667e74Sjose borrego static boolean_t
14157f667e74Sjose borrego smb_odir_lookup_link(smb_request_t *sr, smb_odir_t *od,
1416037cac00Sjoyce mcintosh     char *fname, smb_node_t **tgt_node)
14177f667e74Sjose borrego {
14187f667e74Sjose borrego 	int rc;
1419148c5f43SAlan Wright 	uint32_t flags = SMB_FOLLOW_LINKS | SMB_CASE_SENSITIVE;
14207f667e74Sjose borrego 
1421148c5f43SAlan Wright 	rc = smb_fsop_lookup(sr, od->d_cred, flags,
1422037cac00Sjoyce mcintosh 	    od->d_tree->t_snode, od->d_dnode, fname, tgt_node);
14237f667e74Sjose borrego 	if (rc != 0) {
14247f667e74Sjose borrego 		*tgt_node = NULL;
14257f667e74Sjose borrego 		return (B_FALSE);
14267f667e74Sjose borrego 	}
14277f667e74Sjose borrego 
1428037cac00Sjoyce mcintosh 	if (smb_node_is_dir(*tgt_node) && (!smb_dirsymlink_enable)) {
14297f667e74Sjose borrego 		smb_node_release(*tgt_node);
14307f667e74Sjose borrego 		*tgt_node = NULL;
14317f667e74Sjose borrego 		return (B_FALSE);
14327f667e74Sjose borrego 	}
14337f667e74Sjose borrego 
14347f667e74Sjose borrego 	return (B_TRUE);
14357f667e74Sjose borrego }
1436cb174861Sjoyce mcintosh 
1437cb174861Sjoyce mcintosh /*
1438cb174861Sjoyce mcintosh  * smb_odir_match_name
1439cb174861Sjoyce mcintosh  *
1440cb174861Sjoyce mcintosh  * Check if the directory entry name matches the search pattern:
1441cb174861Sjoyce mcintosh  * - Don't match reserved dos filenames.
1442cb174861Sjoyce mcintosh  * - Check if odirent->od_name matches od->d_pattern.
1443cb174861Sjoyce mcintosh  * - If shortnames are supported, generate the shortname from
1444cb174861Sjoyce mcintosh  *   odirent->od_name and check if it matches od->d_pattern.
1445cb174861Sjoyce mcintosh  */
1446c13be35aSGordon Ross static boolean_t
1447cb174861Sjoyce mcintosh smb_odir_match_name(smb_odir_t *od, smb_odirent_t *odirent)
1448cb174861Sjoyce mcintosh {
1449cb174861Sjoyce mcintosh 	char	*name = odirent->od_name;
1450cb174861Sjoyce mcintosh 	char	shortname[SMB_SHORTNAMELEN];
1451cb174861Sjoyce mcintosh 	ino64_t	ino = odirent->od_ino;
1452c13be35aSGordon Ross 	boolean_t ci = (od->d_flags & SMB_ODIR_FLAG_IGNORE_CASE) != 0;
1453cb174861Sjoyce mcintosh 
1454cb174861Sjoyce mcintosh 	if (smb_is_reserved_dos_name(name))
1455cb174861Sjoyce mcintosh 		return (B_FALSE);
1456cb174861Sjoyce mcintosh 
1457c13be35aSGordon Ross 	if (smb_match(od->d_pattern, name, ci))
1458cb174861Sjoyce mcintosh 		return (B_TRUE);
1459cb174861Sjoyce mcintosh 
1460cb174861Sjoyce mcintosh 	if (od->d_flags & SMB_ODIR_FLAG_SHORTNAMES) {
1461cb174861Sjoyce mcintosh 		smb_mangle(name, ino, shortname, SMB_SHORTNAMELEN);
1462c13be35aSGordon Ross 		if (smb_match(od->d_pattern, shortname, ci))
1463cb174861Sjoyce mcintosh 			return (B_TRUE);
1464cb174861Sjoyce mcintosh 	}
1465cb174861Sjoyce mcintosh 
1466cb174861Sjoyce mcintosh 	return (B_FALSE);
1467cb174861Sjoyce mcintosh }
1468