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.
2356a2adb6SPrashanth Badari  * Copyright 2022 Tintri by DDN, Inc. All rights reserved.
24*4b052b94SMatt Barden  * Copyright 2022-2023 RackTop Systems, Inc.
25da6c28aaSamw  */
26da6c28aaSamw 
27da6c28aaSamw /*
28da6c28aaSamw  * General Structures Layout
29da6c28aaSamw  * -------------------------
30da6c28aaSamw  *
31da6c28aaSamw  * This is a simplified diagram showing the relationship between most of the
32da6c28aaSamw  * main structures.
33da6c28aaSamw  *
34da6c28aaSamw  * +-------------------+
35da6c28aaSamw  * |     SMB_INFO      |
36da6c28aaSamw  * +-------------------+
37da6c28aaSamw  *          |
38da6c28aaSamw  *          |
39da6c28aaSamw  *          v
40da6c28aaSamw  * +-------------------+       +-------------------+      +-------------------+
41da6c28aaSamw  * |     SESSION       |<----->|     SESSION       |......|      SESSION      |
42da6c28aaSamw  * +-------------------+       +-------------------+      +-------------------+
433b13a1efSThomas Keiser  *   |          |
443b13a1efSThomas Keiser  *   |          |
453b13a1efSThomas Keiser  *   |          v
463b13a1efSThomas Keiser  *   |  +-------------------+     +-------------------+   +-------------------+
473b13a1efSThomas Keiser  *   |  |       USER        |<--->|       USER        |...|       USER        |
483b13a1efSThomas Keiser  *   |  +-------------------+     +-------------------+   +-------------------+
493b13a1efSThomas Keiser  *   |
503b13a1efSThomas Keiser  *   |
513b13a1efSThomas Keiser  *   v
52da6c28aaSamw  * +-------------------+       +-------------------+      +-------------------+
53da6c28aaSamw  * |       TREE        |<----->|       TREE        |......|       TREE        |
54da6c28aaSamw  * +-------------------+       +-------------------+      +-------------------+
55da6c28aaSamw  *      |         |
56da6c28aaSamw  *      |         |
57da6c28aaSamw  *      |         v
58da6c28aaSamw  *      |     +-------+       +-------+      +-------+
59da6c28aaSamw  *      |     | OFILE |<----->| OFILE |......| OFILE |
60da6c28aaSamw  *      |     +-------+       +-------+      +-------+
61da6c28aaSamw  *      |
62da6c28aaSamw  *      |
63da6c28aaSamw  *      v
64da6c28aaSamw  *  +-------+       +------+      +------+
65da6c28aaSamw  *  | ODIR  |<----->| ODIR |......| ODIR |
66da6c28aaSamw  *  +-------+       +------+      +------+
67da6c28aaSamw  *
68da6c28aaSamw  *
69da6c28aaSamw  * Odir State Machine
70da6c28aaSamw  * ------------------
71da6c28aaSamw  *
727f667e74Sjose borrego  *    +-------------------------+
737f667e74Sjose borrego  *    |  SMB_ODIR_STATE_OPEN    |<----------- open / creation
74da6c28aaSamw  *    +-------------------------+
75a1511e6bSjoyce mcintosh  *	    |            ^
76a1511e6bSjoyce mcintosh  *	    | (first)    | (last)
77a1511e6bSjoyce mcintosh  *	    | lookup     | release
78a1511e6bSjoyce mcintosh  *	    v            |
79da6c28aaSamw  *    +-------------------------+
80a1511e6bSjoyce mcintosh  *    | SMB_ODIR_STATE_IN_USE   |----
81a1511e6bSjoyce mcintosh  *    +-------------------------+   | lookup / release / read
82a1511e6bSjoyce mcintosh  *	    |                ^-------
83a1511e6bSjoyce mcintosh  *	    | close
84a1511e6bSjoyce mcintosh  *	    |
85a1511e6bSjoyce mcintosh  *	    v
86da6c28aaSamw  *    +-------------------------+
87a1511e6bSjoyce mcintosh  *    | SMB_ODIR_STATE_CLOSING  |----
88a1511e6bSjoyce mcintosh  *    +-------------------------+   | close / release / read
89a1511e6bSjoyce mcintosh  *	    |                ^-------
90a1511e6bSjoyce mcintosh  *	    | (last) release
91a1511e6bSjoyce mcintosh  *	    |
92a1511e6bSjoyce mcintosh  *	    v
937f667e74Sjose borrego  *    +-------------------------+
947f667e74Sjose borrego  *    | SMB_ODIR_STATE_CLOSED   |----------> deletion
95da6c28aaSamw  *    +-------------------------+
96da6c28aaSamw  *
97da6c28aaSamw  *
987f667e74Sjose borrego  * SMB_ODIR_STATE_OPEN
99a1511e6bSjoyce mcintosh  * - the odir exists in the list of odirs of its tree
100a1511e6bSjoyce mcintosh  * - lookup is valid in this state. It will place a hold on the odir
101a1511e6bSjoyce mcintosh  *   by incrementing the reference count and the odir will transition
102a1511e6bSjoyce mcintosh  *   to SMB_ODIR_STATE_IN_USE
103a1511e6bSjoyce mcintosh  * - read/close/release not valid in this state
104a1511e6bSjoyce mcintosh  *
105a1511e6bSjoyce mcintosh  * SMB_ODIR_STATE_IN_USE
1067f667e74Sjose borrego  * - the odir exists in the list of odirs of its tree.
107a1511e6bSjoyce mcintosh  * - lookup is valid in this state. It will place a hold on the odir
108a1511e6bSjoyce mcintosh  *   by incrementing the reference count.
109a1511e6bSjoyce mcintosh  * - if the last hold is released the odir will transition
110a1511e6bSjoyce mcintosh  *   back to SMB_ODIR_STATE_OPEN
1117f667e74Sjose borrego  * - if a close is received the odir will transition to
1127f667e74Sjose borrego  *   SMB_ODIR_STATE_CLOSING.
113da6c28aaSamw  *
114da6c28aaSamw  * SMB_ODIR_STATE_CLOSING
1157f667e74Sjose borrego  * - the odir exists in the list of odirs of its tree.
116a1511e6bSjoyce mcintosh  * - lookup will fail in this state.
117a1511e6bSjoyce mcintosh  * - when the last hold is released the odir will transition
118a1511e6bSjoyce mcintosh  *   to SMB_ODIR_STATE_CLOSED.
119da6c28aaSamw  *
120da6c28aaSamw  * SMB_ODIR_STATE_CLOSED
1217f667e74Sjose borrego  * - the odir exists in the list of odirs of its tree.
1227f667e74Sjose borrego  * - there are no users of the odir (refcnt == 0)
1237f667e74Sjose borrego  * - the odir is being removed from the tree's list and deleted.
124a1511e6bSjoyce mcintosh  * - lookup will fail in this state.
125a1511e6bSjoyce mcintosh  * - read/close/release not valid in this state
126da6c28aaSamw  *
127da6c28aaSamw  * Comments
128da6c28aaSamw  * --------
129da6c28aaSamw  *    The state machine of the odir structures is controlled by 3 elements:
130da6c28aaSamw  *      - The list of odirs of the tree it belongs to.
131da6c28aaSamw  *      - The mutex embedded in the structure itself.
132da6c28aaSamw  *      - The reference count.
133da6c28aaSamw  *
134da6c28aaSamw  *    There's a mutex embedded in the odir structure used to protect its fields
135da6c28aaSamw  *    and there's a lock embedded in the list of odirs of a tree. To
136da6c28aaSamw  *    increment or to decrement the reference count the mutex must be entered.
137da6c28aaSamw  *    To insert the odir into the list of odirs of the tree and to remove
138da6c28aaSamw  *    the odir from it, the lock must be entered in RW_WRITER mode.
139da6c28aaSamw  *
1407f667e74Sjose borrego  *    In order to avoid deadlocks, when both (mutex and lock of the odir
1417f667e74Sjose borrego  *    list) have to be entered, the lock must be entered first.
1427f667e74Sjose borrego  *
1437f667e74Sjose borrego  *
1447f667e74Sjose borrego  * Odir Interface
1457f667e74Sjose borrego  * ---------------
146a90cf9f2SGordon Ross  * smb_odir_open(char *pathname)
1477f667e74Sjose borrego  *	Create an odir representing the directory specified in pathname and
1487f667e74Sjose borrego  *	add it into the tree's list of odirs.
149a90cf9f2SGordon Ross  *	Returns NT status.
150a90cf9f2SGordon Ross  *
151a90cf9f2SGordon Ross  * smb_odir_openfh(smb_ofile_t *of)
152a90cf9f2SGordon Ross  *	Create an odir representing the directory specified by the
153a90cf9f2SGordon Ross  *	existing open handle (from a prior open of the directory).
154a90cf9f2SGordon Ross  *	Returns NT status.
1557f667e74Sjose borrego  *
1567f667e74Sjose borrego  * smb_odir_openat(smb_node_t *unode)
1577f667e74Sjose borrego  *	Create an odir representing the extended attribute directory
1587f667e74Sjose borrego  *	associated with the file (or directory) represented by unode
1597f667e74Sjose borrego  *	and add it into the tree's list of odirs.
160a90cf9f2SGordon Ross  *	Returns NT status.
1617f667e74Sjose borrego  *
1623b13a1efSThomas Keiser  * smb_odir_t *odir = smb_tree_lookup_odir(..., odid)
1637f667e74Sjose borrego  *	Find the odir corresponding to the specified odid in the tree's
164a1511e6bSjoyce mcintosh  *	list of odirs. Place a hold on the odir.
1657f667e74Sjose borrego  *
1667f667e74Sjose borrego  * smb_odir_read(..., smb_odirent_t *odirent)
1677f667e74Sjose borrego  *	Find the next directory entry in the odir and return it in odirent.
1687f667e74Sjose borrego  *
1697f667e74Sjose borrego  * smb_odir_read_fileinfo(..., smb_fileinfo_t *)
1707f667e74Sjose borrego  *	Find the next directory entry in the odir. Return the details of
1717f667e74Sjose borrego  *	the directory entry in smb_fileinfo_t. (See odir internals below)
172da6c28aaSamw  *
173037cac00Sjoyce mcintosh  * smb_odir_read_streaminfo(..., smb_streaminfo_t *)
1747f667e74Sjose borrego  *	Find the next named stream entry in the odir. Return the details of
1757f667e74Sjose borrego  *	the named stream in smb_streaminfo_t.
176da6c28aaSamw  *
177a1511e6bSjoyce mcintosh  * smb_odir_close(smb_odir_t *odir)
178a1511e6bSjoyce mcintosh  *  Close the odir.
179a1511e6bSjoyce mcintosh  *  The caller of close must have a hold on the odir being closed.
180a1511e6bSjoyce mcintosh  *  The hold should be released after closing.
181a1511e6bSjoyce mcintosh  *
1827f667e74Sjose borrego  * smb_odir_release(smb_odir_t *odir)
1837f667e74Sjose borrego  *	Release the hold on the odir, obtained by lookup.
184da6c28aaSamw  *
185da6c28aaSamw  *
1867f667e74Sjose borrego  * Odir Internals
1877f667e74Sjose borrego  * --------------
1887f667e74Sjose borrego  * The odir object represent an open directory search. Each read operation
1897f667e74Sjose borrego  * provides the caller with a structure containing information  pertaining
1907f667e74Sjose borrego  * to the next directory entry that matches the search criteria, namely
1917f667e74Sjose borrego  * the filename or match pattern and, in the case of smb_odir_read_fileinfo(),
1927f667e74Sjose borrego  * the search attributes.
193da6c28aaSamw  *
1947f667e74Sjose borrego  * The odir maintains a buffer (d_buf) of directory entries read from
1957f667e74Sjose borrego  * the filesystem via a vop_readdir. The buffer is populated when a read
1967f667e74Sjose borrego  * request (smb_odir_next_odirent) finds that the buffer is empty or that
1977f667e74Sjose borrego  * the end of the buffer has been reached, and also when a new client request
1987f667e74Sjose borrego  * (find next) begins.
1997f667e74Sjose borrego  *
2007f667e74Sjose borrego  * The data in d_buf (that which is returned from the file system) can
2017f667e74Sjose borrego  * be in one of two formats. If the file system supports extended directory
2027f667e74Sjose borrego  * entries we request that the data be returned as edirent_t structures. If
2037f667e74Sjose borrego  * it does not the data will be returned as dirent64_t structures. For
2047f667e74Sjose borrego  * convenience, when the next directory entry is read from d_buf by
2057f667e74Sjose borrego  * smb_odir_next_odirent it is translated into an smb_odirent_t.
2067f667e74Sjose borrego  *
2077f667e74Sjose borrego  * smb_odir_read_fileinfo
2087f667e74Sjose borrego  * The processing required to obtain the information to populate the caller's
2097f667e74Sjose borrego  * smb_fileinfo_t differs depending upon whether the directory search is for a
2107f667e74Sjose borrego  * single specified filename or for multiple files matching a search pattern.
2117f667e74Sjose borrego  * Thus smb_odir_read_fileinfo uses two static functions:
2127f667e74Sjose borrego  * smb_odir_single_fileinfo - obtains the smb_fileinfo_t info for the single
2137f667e74Sjose borrego  * filename as specified in smb_odir_open request.
2147f667e74Sjose borrego  * smb_odir_wildcard_fileinfo - obtains the smb_fileinfo_t info for the filename
2157f667e74Sjose borrego  * returned from the smb_odir_next_odirent. This is called in a loop until
2167f667e74Sjose borrego  * an entry matching the search criteria is found or no more entries exist.
2177f667e74Sjose borrego  *
2187f667e74Sjose borrego  * If a directory entry is a VLNK, the name returned in the smb_fileinfo_t
2197f667e74Sjose borrego  * is the name of the directory entry but the attributes are the attribites
2207f667e74Sjose borrego  * of the file that is the target of the link. If the link target cannot
2217f667e74Sjose borrego  * be found the attributes returned are the attributes of the link itself.
2227f667e74Sjose borrego  *
223037cac00Sjoyce mcintosh  * smb_odir_read_streaminfo
2247f667e74Sjose borrego  * In order for an odir to provide information about stream files it
2257f667e74Sjose borrego  * must be opened with smb_odir_openat(). smb_odir_read_streaminfo() can
2267f667e74Sjose borrego  * then be used to obtain the name and size of named stream files.
2277f667e74Sjose borrego  *
2287f667e74Sjose borrego  * Resuming a Search
2297f667e74Sjose borrego  * -----------------
2307f667e74Sjose borrego  * A directory search often consists of multiple client requests: an initial
2317f667e74Sjose borrego  * find_first request followed by zero or more find_next requests and a
2327f667e74Sjose borrego  * find_close request.
2337f667e74Sjose borrego  * The find_first request will open and lookup the odir, read its desired
2347f667e74Sjose borrego  * number of entries from the odir, then release the odir and return.
2357f667e74Sjose borrego  * A find_next request will lookup the odir and read its desired number of
2367f667e74Sjose borrego  * entries from the odir, then release the odir and return.
2377f667e74Sjose borrego  * At the end of the search the find_close request will close the odir.
2387f667e74Sjose borrego  *
2397f667e74Sjose borrego  * In order to be able to resume a directory search (find_next) the odir
2407f667e74Sjose borrego  * provides the capability for the caller to save one or more resume points
2417f667e74Sjose borrego  * (cookies) at the end of a request, and to specify which resume point
2427f667e74Sjose borrego  * (cookie) to restart from at the beginning of the next search.
2437f667e74Sjose borrego  *	smb_odir_save_cookie(..., cookie)
2447f667e74Sjose borrego  *	smb_odir_resume_at(smb_odir_resume_t *resume)
2457f667e74Sjose borrego  * A search can be resumed at a specified resume point (cookie), the resume
2467f667e74Sjose borrego  * point (cookie) stored at a specified index in the d_cookies array, or
2477f667e74Sjose borrego  * a specified filename. The latter (specified filename) is not yet supported.
2487f667e74Sjose borrego  *
2497f667e74Sjose borrego  * See smb_search, smb_find, smb_find_unique, and smb_trans2_find for details
250da6c28aaSamw  */
2517f667e74Sjose borrego 
252da6c28aaSamw #include <smbsrv/smb_kproto.h>
253da6c28aaSamw #include <smbsrv/smb_fsops.h>
254e3f2c991SKeyur Desai #include <smbsrv/smb_share.h>
2557f667e74Sjose borrego #include <sys/extdirent.h>
256da6c28aaSamw 
2577f667e74Sjose borrego /* static functions */
258a90cf9f2SGordon Ross static smb_odir_t *smb_odir_create(smb_request_t *, smb_node_t *,
259a90cf9f2SGordon Ross     const char *, uint16_t, uint16_t, cred_t *);
2607f667e74Sjose borrego static int smb_odir_single_fileinfo(smb_request_t *, smb_odir_t *,
2617f667e74Sjose borrego     smb_fileinfo_t *);
2627f667e74Sjose borrego static int smb_odir_wildcard_fileinfo(smb_request_t *, smb_odir_t *,
2637f667e74Sjose borrego     smb_odirent_t *, smb_fileinfo_t *);
2647f667e74Sjose borrego static int smb_odir_next_odirent(smb_odir_t *, smb_odirent_t *);
265037cac00Sjoyce mcintosh static boolean_t smb_odir_lookup_link(smb_request_t *, smb_odir_t *,
266037cac00Sjoyce mcintosh     char *, smb_node_t **);
267cb174861Sjoyce mcintosh static boolean_t smb_odir_match_name(smb_odir_t *, smb_odirent_t *);
268811599a4SMatt Barden static void smb_odir_delete(void *);
269da6c28aaSamw 
270da6c28aaSamw 
271da6c28aaSamw /*
272a90cf9f2SGordon Ross  * smb_odir_openpath
2737f667e74Sjose borrego  *
2747f667e74Sjose borrego  * Create an odir representing the directory specified in pathname.
2757f667e74Sjose borrego  *
2767f667e74Sjose borrego  * Returns:
277a90cf9f2SGordon Ross  *    NT Status
278da6c28aaSamw  */
279a90cf9f2SGordon Ross uint32_t
smb_odir_openpath(smb_request_t * sr,char * path,uint16_t sattr,uint32_t flags,smb_odir_t ** odp)280a90cf9f2SGordon Ross smb_odir_openpath(smb_request_t *sr, char *path, uint16_t sattr,
28156a2adb6SPrashanth Badari     uint32_t flags, smb_odir_t **odp)
282da6c28aaSamw {
2837f667e74Sjose borrego 	int		rc;
2847f667e74Sjose borrego 	smb_tree_t	*tree;
2857f667e74Sjose borrego 	smb_node_t	*dnode;
2867f667e74Sjose borrego 	char		pattern[MAXNAMELEN];
287811599a4SMatt Barden 	uint16_t	odid;
288eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 	cred_t		*cr;
289da6c28aaSamw 
2907f667e74Sjose borrego 	ASSERT(sr);
2917f667e74Sjose borrego 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
2927f667e74Sjose borrego 	ASSERT(sr->tid_tree);
2937f667e74Sjose borrego 	ASSERT(sr->tid_tree->t_magic == SMB_TREE_MAGIC);
294a90cf9f2SGordon Ross 	*odp = NULL;
295da6c28aaSamw 
2967f667e74Sjose borrego 	tree = sr->tid_tree;
2977f667e74Sjose borrego 
298c13be35aSGordon Ross 	if (sr->session->dialect < NT_LM_0_12)
299c13be35aSGordon Ross 		smb_convert_wildcards(path);
300fe1c642dSBill Krier 
3017f667e74Sjose borrego 	rc = smb_pathname_reduce(sr, sr->user_cr, path,
3027f667e74Sjose borrego 	    tree->t_snode, tree->t_snode, &dnode, pattern);
303a90cf9f2SGordon Ross 	if (rc != 0)
304a90cf9f2SGordon Ross 		return (smb_errno2status(rc));
305da6c28aaSamw 
3069fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	if (!smb_node_is_dir(dnode)) {
3077f667e74Sjose borrego 		smb_node_release(dnode);
308a90cf9f2SGordon Ross 		return (NT_STATUS_OBJECT_PATH_NOT_FOUND);
309da6c28aaSamw 	}
310da6c28aaSamw 
3117f667e74Sjose borrego 	if (smb_fsop_access(sr, sr->user_cr, dnode, FILE_LIST_DIRECTORY) != 0) {
3127f667e74Sjose borrego 		smb_node_release(dnode);
313a90cf9f2SGordon Ross 		return (NT_STATUS_ACCESS_DENIED);
314a90cf9f2SGordon Ross 	}
315a90cf9f2SGordon Ross 
316a90cf9f2SGordon Ross 	if (smb_idpool_alloc(&tree->t_odid_pool, &odid)) {
317a90cf9f2SGordon Ross 		smb_node_release(dnode);
318a90cf9f2SGordon Ross 		return (NT_STATUS_TOO_MANY_OPENED_FILES);
3197f667e74Sjose borrego 	}
320da6c28aaSamw 
321eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (flags & SMB_ODIR_OPENF_BACKUP_INTENT)
3223b13a1efSThomas Keiser 		cr = smb_user_getprivcred(sr->uid_user);
323eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 	else
3243b13a1efSThomas Keiser 		cr = sr->uid_user->u_cred;
325eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 
326a90cf9f2SGordon Ross 	*odp = smb_odir_create(sr, dnode, pattern, sattr, odid, cr);
3277f667e74Sjose borrego 	smb_node_release(dnode);
328a90cf9f2SGordon Ross 
329a90cf9f2SGordon Ross 	return (0);
330a90cf9f2SGordon Ross }
331a90cf9f2SGordon Ross 
332a90cf9f2SGordon Ross /*
333a90cf9f2SGordon Ross  * smb_odir_openfh
334a90cf9f2SGordon Ross  *
335a90cf9f2SGordon Ross  * Create an odir representing the directory already opened on "of".
336a90cf9f2SGordon Ross  *
337a90cf9f2SGordon Ross  * Returns:
338a90cf9f2SGordon Ross  *    NT status
339a90cf9f2SGordon Ross  */
340a90cf9f2SGordon Ross uint32_t
smb_odir_openfh(smb_request_t * sr,const char * pattern,uint16_t sattr,smb_odir_t ** odp)341a90cf9f2SGordon Ross smb_odir_openfh(smb_request_t *sr, const char *pattern, uint16_t sattr,
34256a2adb6SPrashanth Badari     smb_odir_t **odp)
343a90cf9f2SGordon Ross {
344a90cf9f2SGordon Ross 	smb_ofile_t	*of = sr->fid_ofile;
345a90cf9f2SGordon Ross 
346a90cf9f2SGordon Ross 	*odp = NULL;
347a90cf9f2SGordon Ross 
348a90cf9f2SGordon Ross 	if (of->f_node == NULL || !smb_node_is_dir(of->f_node))
349a90cf9f2SGordon Ross 		return (NT_STATUS_INVALID_PARAMETER);
350a90cf9f2SGordon Ross 
351a90cf9f2SGordon Ross 	if ((of->f_granted_access & FILE_LIST_DIRECTORY) == 0)
352a90cf9f2SGordon Ross 		return (NT_STATUS_ACCESS_DENIED);
353a90cf9f2SGordon Ross 
354a90cf9f2SGordon Ross 	*odp = smb_odir_create(sr, of->f_node, pattern, sattr, 0, of->f_cr);
355a90cf9f2SGordon Ross 
356a90cf9f2SGordon Ross 	return (0);
357da6c28aaSamw }
358da6c28aaSamw 
359da6c28aaSamw /*
3607f667e74Sjose borrego  * smb_odir_openat
3617f667e74Sjose borrego  *
3627f667e74Sjose borrego  * Create an odir representing the extended attribute directory
3637f667e74Sjose borrego  * associated with the file (or directory) represented by unode.
3647f667e74Sjose borrego  *
3657f667e74Sjose borrego  * Returns:
366a90cf9f2SGordon Ross  *    NT status
3677f667e74Sjose borrego  */
368a90cf9f2SGordon Ross uint32_t
smb_odir_openat(smb_request_t * sr,smb_node_t * unode,smb_odir_t ** odp,boolean_t restricted)36956a2adb6SPrashanth Badari smb_odir_openat(smb_request_t *sr, smb_node_t *unode, smb_odir_t **odp,
37056a2adb6SPrashanth Badari     boolean_t restricted)
3717f667e74Sjose borrego {
372a90cf9f2SGordon Ross 	char		pattern[SMB_STREAM_PREFIX_LEN + 2];
3737f667e74Sjose borrego 	vnode_t		*xattr_dvp;
3747f667e74Sjose borrego 	cred_t		*cr;
3757f667e74Sjose borrego 	smb_node_t	*xattr_dnode;
376a90cf9f2SGordon Ross 	int		rc;
3777f667e74Sjose borrego 
3787f667e74Sjose borrego 	ASSERT(sr);
3797f667e74Sjose borrego 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
3807f667e74Sjose borrego 	ASSERT(unode);
3817f667e74Sjose borrego 	ASSERT(unode->n_magic == SMB_NODE_MAGIC);
382a90cf9f2SGordon Ross 	*odp = NULL;
3837f667e74Sjose borrego 
384743a77edSAlan Wright 	if (SMB_TREE_CONTAINS_NODE(sr, unode) == 0 ||
385a90cf9f2SGordon Ross 	    SMB_TREE_HAS_ACCESS(sr, ACE_LIST_DIRECTORY) == 0)
386a90cf9f2SGordon Ross 		return (NT_STATUS_ACCESS_DENIED);
387a90cf9f2SGordon Ross 
3888622ec45SGordon Ross 	cr = zone_kcred();
3897f667e74Sjose borrego 
3907f667e74Sjose borrego 	/* find the xattrdir vnode */
3917f667e74Sjose borrego 	rc = smb_vop_lookup_xattrdir(unode->vp, &xattr_dvp, LOOKUP_XATTR, cr);
392a90cf9f2SGordon Ross 	if (rc != 0)
393a90cf9f2SGordon Ross 		return (smb_errno2status(rc));
3947f667e74Sjose borrego 
3957f667e74Sjose borrego 	/* lookup the xattrdir's smb_node */
3967f667e74Sjose borrego 	xattr_dnode = smb_node_lookup(sr, NULL, cr, xattr_dvp, XATTR_DIR,
397037cac00Sjoyce mcintosh 	    unode, NULL);
3987f667e74Sjose borrego 	VN_RELE(xattr_dvp);
399a90cf9f2SGordon Ross 	if (xattr_dnode == NULL)
400a90cf9f2SGordon Ross 		return (NT_STATUS_NO_MEMORY);
4017f667e74Sjose borrego 
4027f667e74Sjose borrego 	(void) snprintf(pattern, sizeof (pattern), "%s*", SMB_STREAM_PREFIX);
403a90cf9f2SGordon Ross 	*odp = smb_odir_create(sr, xattr_dnode, pattern,
404a90cf9f2SGordon Ross 	    SMB_SEARCH_ATTRIBUTES, 0, cr);
405a90cf9f2SGordon Ross 
40656a2adb6SPrashanth Badari 	/* Causes restricted stream names to be hidden from the caller */
40756a2adb6SPrashanth Badari 	if (restricted)
40856a2adb6SPrashanth Badari 		(*odp)->d_flags |= SMB_ODIR_FLAG_RESTRICTED;
40956a2adb6SPrashanth Badari 
4107f667e74Sjose borrego 	smb_node_release(xattr_dnode);
411a90cf9f2SGordon Ross 	return (0);
4127f667e74Sjose borrego }
4137f667e74Sjose borrego 
4147f667e74Sjose borrego /*
4157f667e74Sjose borrego  * smb_odir_hold
416a1511e6bSjoyce mcintosh  *
417a1511e6bSjoyce mcintosh  * A hold will only be granted if the odir is open or in_use.
4187f667e74Sjose borrego  */
4197f667e74Sjose borrego boolean_t
smb_odir_hold(smb_odir_t * od)4207f667e74Sjose borrego smb_odir_hold(smb_odir_t *od)
4217f667e74Sjose borrego {
4227f667e74Sjose borrego 	ASSERT(od);
4237f667e74Sjose borrego 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
4247f667e74Sjose borrego 
4257f667e74Sjose borrego 	mutex_enter(&od->d_mutex);
426a1511e6bSjoyce mcintosh 
427a1511e6bSjoyce mcintosh 	switch (od->d_state) {
428a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_OPEN:
429a1511e6bSjoyce mcintosh 		od->d_refcnt++;
430a1511e6bSjoyce mcintosh 		od->d_state = SMB_ODIR_STATE_IN_USE;
431a1511e6bSjoyce mcintosh 		break;
432a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_IN_USE:
433a1511e6bSjoyce mcintosh 		od->d_refcnt++;
434a1511e6bSjoyce mcintosh 		break;
435a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_CLOSING:
436a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_CLOSED:
437a1511e6bSjoyce mcintosh 	default:
4387f667e74Sjose borrego 		mutex_exit(&od->d_mutex);
4397f667e74Sjose borrego 		return (B_FALSE);
4407f667e74Sjose borrego 	}
4417f667e74Sjose borrego 
4427f667e74Sjose borrego 	mutex_exit(&od->d_mutex);
4437f667e74Sjose borrego 	return (B_TRUE);
4447f667e74Sjose borrego }
4457f667e74Sjose borrego 
4467f667e74Sjose borrego /*
4479fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * If the odir is in SMB_ODIR_STATE_CLOSING and this release results in
4489fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * a refcnt of 0, change the state to SMB_ODIR_STATE_CLOSED and post the
4499fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * object for deletion.  Object deletion is deferred to avoid modifying
4509fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * a list while an iteration may be in progress.
451da6c28aaSamw  */
452da6c28aaSamw void
smb_odir_release(smb_odir_t * od)4537f667e74Sjose borrego smb_odir_release(smb_odir_t *od)
454da6c28aaSamw {
455811599a4SMatt Barden 	smb_tree_t *tree = od->d_tree;
456811599a4SMatt Barden 
4579fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	SMB_ODIR_VALID(od);
458da6c28aaSamw 
459da6c28aaSamw 	mutex_enter(&od->d_mutex);
460a1511e6bSjoyce mcintosh 	ASSERT(od->d_refcnt > 0);
4617f667e74Sjose borrego 
462da6c28aaSamw 	switch (od->d_state) {
463da6c28aaSamw 	case SMB_ODIR_STATE_OPEN:
464a1511e6bSjoyce mcintosh 		break;
465a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_IN_USE:
4667f667e74Sjose borrego 		od->d_refcnt--;
467a1511e6bSjoyce mcintosh 		if (od->d_refcnt == 0)
468a1511e6bSjoyce mcintosh 			od->d_state = SMB_ODIR_STATE_OPEN;
469da6c28aaSamw 		break;
470da6c28aaSamw 	case SMB_ODIR_STATE_CLOSING:
4717f667e74Sjose borrego 		od->d_refcnt--;
4727f667e74Sjose borrego 		if (od->d_refcnt == 0) {
4737f667e74Sjose borrego 			od->d_state = SMB_ODIR_STATE_CLOSED;
474811599a4SMatt Barden 			smb_llist_post(&tree->t_odir_list, od,
475811599a4SMatt Barden 			    smb_odir_delete);
4767f667e74Sjose borrego 		}
4777f667e74Sjose borrego 		break;
478da6c28aaSamw 	case SMB_ODIR_STATE_CLOSED:
479da6c28aaSamw 	default:
480da6c28aaSamw 		break;
481da6c28aaSamw 	}
4827f667e74Sjose borrego 
4837f667e74Sjose borrego 	mutex_exit(&od->d_mutex);
4847f667e74Sjose borrego }
4857f667e74Sjose borrego 
4867f667e74Sjose borrego /*
4877f667e74Sjose borrego  * smb_odir_close
4887f667e74Sjose borrego  */
4897f667e74Sjose borrego void
smb_odir_close(smb_odir_t * od)4907f667e74Sjose borrego smb_odir_close(smb_odir_t *od)
4917f667e74Sjose borrego {
4927f667e74Sjose borrego 	ASSERT(od);
493a1511e6bSjoyce mcintosh 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
4947f667e74Sjose borrego 
4957f667e74Sjose borrego 	mutex_enter(&od->d_mutex);
496a1511e6bSjoyce mcintosh 	ASSERT(od->d_refcnt > 0);
497a1511e6bSjoyce mcintosh 	switch (od->d_state) {
498a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_OPEN:
499a1511e6bSjoyce mcintosh 		break;
500a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_IN_USE:
501a1511e6bSjoyce mcintosh 		od->d_state = SMB_ODIR_STATE_CLOSING;
502a1511e6bSjoyce mcintosh 		break;
503a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_CLOSING:
504a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_CLOSED:
505a1511e6bSjoyce mcintosh 	default:
506a1511e6bSjoyce mcintosh 		break;
5077f667e74Sjose borrego 	}
508da6c28aaSamw 	mutex_exit(&od->d_mutex);
509da6c28aaSamw }
510da6c28aaSamw 
511da6c28aaSamw /*
5127f667e74Sjose borrego  * smb_odir_read
513da6c28aaSamw  *
5147f667e74Sjose borrego  * Find the next directory entry matching the search pattern.
5157f667e74Sjose borrego  * No search attribute matching is performed.
516da6c28aaSamw  *
5177f667e74Sjose borrego  * Returns:
5187f667e74Sjose borrego  *  0 - success.
5197f667e74Sjose borrego  *      - If a matching entry was found eof will be B_FALSE and
5207f667e74Sjose borrego  *        odirent will be populated.
521a90cf9f2SGordon Ross  * ENOENT
522a90cf9f2SGordon Ross  *      - If we've scanned to the end, eof will be B_TRUE.
523a90cf9f2SGordon Ross  * errno - other errors
524da6c28aaSamw  */
5257f667e74Sjose borrego int
smb_odir_read(smb_request_t * sr,smb_odir_t * od,smb_odirent_t * odirent,boolean_t * eof)5267f667e74Sjose borrego smb_odir_read(smb_request_t *sr, smb_odir_t *od,
5277f667e74Sjose borrego     smb_odirent_t *odirent, boolean_t *eof)
528da6c28aaSamw {
5298b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	int		rc;
530da6c28aaSamw 
5317f667e74Sjose borrego 	ASSERT(sr);
5327f667e74Sjose borrego 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
5337f667e74Sjose borrego 	ASSERT(od);
5347f667e74Sjose borrego 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
5357f667e74Sjose borrego 	ASSERT(odirent);
5367f667e74Sjose borrego 
5377f667e74Sjose borrego 	mutex_enter(&od->d_mutex);
538a1511e6bSjoyce mcintosh 	ASSERT(od->d_refcnt > 0);
5397f667e74Sjose borrego 
540a1511e6bSjoyce mcintosh 	switch (od->d_state) {
541a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_IN_USE:
542a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_CLOSING:
543a1511e6bSjoyce mcintosh 		break;
544a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_OPEN:
545a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_CLOSED:
546a1511e6bSjoyce mcintosh 	default:
5477f667e74Sjose borrego 		mutex_exit(&od->d_mutex);
548a90cf9f2SGordon Ross 		return (EBADF);
5497f667e74Sjose borrego 	}
550da6c28aaSamw 
5517f667e74Sjose borrego 	for (;;) {
5527f667e74Sjose borrego 		if ((rc = smb_odir_next_odirent(od, odirent)) != 0)
5537f667e74Sjose borrego 			break;
554cb174861Sjoyce mcintosh 		if (smb_odir_match_name(od, odirent))
5557f667e74Sjose borrego 			break;
5567f667e74Sjose borrego 	}
5577f667e74Sjose borrego 
5587f667e74Sjose borrego 	mutex_exit(&od->d_mutex);
5597f667e74Sjose borrego 
5607f667e74Sjose borrego 	switch (rc) {
5617f667e74Sjose borrego 	case 0:
5627f667e74Sjose borrego 		*eof = B_FALSE;
5637f667e74Sjose borrego 		return (0);
5647f667e74Sjose borrego 	case ENOENT:
5657f667e74Sjose borrego 		*eof = B_TRUE;
566a90cf9f2SGordon Ross 		/* FALLTHROUGH */
5677f667e74Sjose borrego 	default:
568a90cf9f2SGordon Ross 		return (rc);
569da6c28aaSamw 	}
570da6c28aaSamw }
571da6c28aaSamw 
572da6c28aaSamw /*
5737f667e74Sjose borrego  * smb_odir_read_fileinfo
574da6c28aaSamw  *
5757f667e74Sjose borrego  * Find the next directory entry matching the search pattern
5767f667e74Sjose borrego  * and attributes: od->d_pattern and od->d_sattr.
577da6c28aaSamw  *
5787f667e74Sjose borrego  * If the search pattern specifies a single filename call
5797f667e74Sjose borrego  * smb_odir_single_fileinfo to get the file attributes and
5807f667e74Sjose borrego  * populate the caller's smb_fileinfo_t.
5817f667e74Sjose borrego  *
5827f667e74Sjose borrego  * If the search pattern contains wildcards call smb_odir_next_odirent
5837f667e74Sjose borrego  * to get the next directory entry then. Repeat until a matching
5847f667e74Sjose borrego  * filename is found. Call smb_odir_wildcard_fileinfo to get the
5857f667e74Sjose borrego  * file attributes and populate the caller's smb_fileinfo_t.
5867f667e74Sjose borrego  * This is repeated until a file matching the search criteria is found.
5877f667e74Sjose borrego  *
5887f667e74Sjose borrego  * Returns:
5897f667e74Sjose borrego  *  0 - success.
5907f667e74Sjose borrego  *      - If a matching entry was found eof will be B_FALSE and
5917f667e74Sjose borrego  *        fileinfo will be populated.
592a90cf9f2SGordon Ross  * ENOENT
593a90cf9f2SGordon Ross  *      - If at end of dir, eof will be B_TRUE.
594a90cf9f2SGordon Ross  * errno - other error
595da6c28aaSamw  */
5967f667e74Sjose borrego int
smb_odir_read_fileinfo(smb_request_t * sr,smb_odir_t * od,smb_fileinfo_t * fileinfo,uint16_t * eof)5977f667e74Sjose borrego smb_odir_read_fileinfo(smb_request_t *sr, smb_odir_t *od,
598bfbce3c1SGordon Ross     smb_fileinfo_t *fileinfo, uint16_t *eof)
599da6c28aaSamw {
600bbf6f00cSJordan Brown 	int		rc, errnum;
6017f667e74Sjose borrego 	smb_odirent_t	*odirent;
602da6c28aaSamw 
6037f667e74Sjose borrego 	ASSERT(sr);
6047f667e74Sjose borrego 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
6057f667e74Sjose borrego 	ASSERT(od);
6067f667e74Sjose borrego 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
6077f667e74Sjose borrego 	ASSERT(fileinfo);
608da6c28aaSamw 
6097f667e74Sjose borrego 	mutex_enter(&od->d_mutex);
610a1511e6bSjoyce mcintosh 	ASSERT(od->d_refcnt > 0);
6117f667e74Sjose borrego 
612a1511e6bSjoyce mcintosh 	switch (od->d_state) {
613a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_IN_USE:
614a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_CLOSING:
615a1511e6bSjoyce mcintosh 		break;
616a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_OPEN:
617a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_CLOSED:
618a1511e6bSjoyce mcintosh 	default:
6197f667e74Sjose borrego 		mutex_exit(&od->d_mutex);
620*4b052b94SMatt Barden 		return (SET_ERROR(EBADF));
6217f667e74Sjose borrego 	}
6227f667e74Sjose borrego 
623bfbce3c1SGordon Ross 	if ((od->d_flags & SMB_ODIR_FLAG_WILDCARDS) == 0) {
6247f667e74Sjose borrego 		if (od->d_eof)
625*4b052b94SMatt Barden 			rc = SET_ERROR(ENOENT);
6267f667e74Sjose borrego 		else
6277f667e74Sjose borrego 			rc = smb_odir_single_fileinfo(sr, od, fileinfo);
6287f667e74Sjose borrego 		od->d_eof = B_TRUE;
6297f667e74Sjose borrego 	} else {
6307f667e74Sjose borrego 		odirent = kmem_alloc(sizeof (smb_odirent_t), KM_SLEEP);
6317f667e74Sjose borrego 		for (;;) {
632*4b052b94SMatt Barden 			mutex_enter(&sr->sr_mutex);
633*4b052b94SMatt Barden 			if (sr->sr_state != SMB_REQ_STATE_ACTIVE) {
634*4b052b94SMatt Barden 				mutex_exit(&sr->sr_mutex);
635*4b052b94SMatt Barden 				rc = SET_ERROR(EINTR);
636*4b052b94SMatt Barden 				break;
637*4b052b94SMatt Barden 			}
638*4b052b94SMatt Barden 			mutex_exit(&sr->sr_mutex);
6397f667e74Sjose borrego 			bzero(fileinfo, sizeof (smb_fileinfo_t));
6407f667e74Sjose borrego 			if ((rc = smb_odir_next_odirent(od, odirent)) != 0)
6417f667e74Sjose borrego 				break;
6427f667e74Sjose borrego 
643bbf6f00cSJordan Brown 			/* skip non utf8 filename */
644bbf6f00cSJordan Brown 			if (u8_validate(odirent->od_name,
645bbf6f00cSJordan Brown 			    strlen(odirent->od_name), NULL,
646bbf6f00cSJordan Brown 			    U8_VALIDATE_ENTIRE, &errnum) < 0)
647bbf6f00cSJordan Brown 				continue;
648bbf6f00cSJordan Brown 
649cb174861Sjoyce mcintosh 			if (!smb_odir_match_name(od, odirent))
6507f667e74Sjose borrego 				continue;
6517f667e74Sjose borrego 
6527f667e74Sjose borrego 			rc = smb_odir_wildcard_fileinfo(sr, od, odirent,
6537f667e74Sjose borrego 			    fileinfo);
6547f667e74Sjose borrego 			if (rc == 0)
6557f667e74Sjose borrego 				break;
656da6c28aaSamw 		}
6577f667e74Sjose borrego 		kmem_free(odirent, sizeof (smb_odirent_t));
6587f667e74Sjose borrego 	}
6597f667e74Sjose borrego 	mutex_exit(&od->d_mutex);
6607f667e74Sjose borrego 
6617f667e74Sjose borrego 	switch (rc) {
6627f667e74Sjose borrego 	case 0:
663bfbce3c1SGordon Ross 		*eof = 0;
6647f667e74Sjose borrego 		return (0);
6657f667e74Sjose borrego 	case ENOENT:
666bfbce3c1SGordon Ross 		*eof = 1;	/* per. FindFirst, FindNext spec. */
667a90cf9f2SGordon Ross 		/* FALLTHROUGH */
6687f667e74Sjose borrego 	default:
669a90cf9f2SGordon Ross 		return (rc);
670da6c28aaSamw 	}
671da6c28aaSamw }
672da6c28aaSamw 
673da6c28aaSamw /*
6747f667e74Sjose borrego  * smb_odir_read_streaminfo
6757f667e74Sjose borrego  *
6767f667e74Sjose borrego  * Find the next directory entry whose name begins with SMB_STREAM_PREFIX,
6777f667e74Sjose borrego  * and thus represents an NTFS named stream.
6787f667e74Sjose borrego  * No search attribute matching is performed.
6798b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States  * No case conflict name mangling is required for NTFS named stream names.
6807f667e74Sjose borrego  *
6817f667e74Sjose borrego  * Returns:
6827f667e74Sjose borrego  *  0 - success.
6837f667e74Sjose borrego  *      - If a matching entry was found eof will be B_FALSE and
6847f667e74Sjose borrego  *        sinfo will be populated.
6857f667e74Sjose borrego  *      - If there are no matching entries eof will be B_TRUE.
686a90cf9f2SGordon Ross  * errno - error
687da6c28aaSamw  */
6887f667e74Sjose borrego int
smb_odir_read_streaminfo(smb_request_t * sr,smb_odir_t * od,smb_streaminfo_t * sinfo,boolean_t * eof)6897f667e74Sjose borrego smb_odir_read_streaminfo(smb_request_t *sr, smb_odir_t *od,
6907f667e74Sjose borrego     smb_streaminfo_t *sinfo, boolean_t *eof)
691da6c28aaSamw {
6927f667e74Sjose borrego 	int		rc;
693a90cf9f2SGordon Ross 	cred_t		*kcr;
6947f667e74Sjose borrego 	smb_odirent_t	*odirent;
695e3f2c991SKeyur Desai 	smb_node_t	*fnode;
6967f667e74Sjose borrego 	smb_attr_t	attr;
6977f667e74Sjose borrego 
6987f667e74Sjose borrego 	ASSERT(sr);
6997f667e74Sjose borrego 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
700da6c28aaSamw 	ASSERT(od);
701da6c28aaSamw 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
7027f667e74Sjose borrego 	ASSERT(sinfo);
703da6c28aaSamw 
704a90cf9f2SGordon Ross 	kcr = zone_kcred();
705a90cf9f2SGordon Ross 
706da6c28aaSamw 	mutex_enter(&od->d_mutex);
707a1511e6bSjoyce mcintosh 	ASSERT(od->d_refcnt > 0);
708da6c28aaSamw 
709a1511e6bSjoyce mcintosh 	switch (od->d_state) {
710a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_IN_USE:
711a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_CLOSING:
712a1511e6bSjoyce mcintosh 		break;
713a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_OPEN:
714a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_CLOSED:
715a1511e6bSjoyce mcintosh 	default:
7167f667e74Sjose borrego 		mutex_exit(&od->d_mutex);
717a90cf9f2SGordon Ross 		return (EBADF);
7187f667e74Sjose borrego 	}
7197f667e74Sjose borrego 
7207f667e74Sjose borrego 	/* Check that odir represents an xattr directory */
7218b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (!(od->d_flags & SMB_ODIR_FLAG_XATTR)) {
7227f667e74Sjose borrego 		*eof = B_TRUE;
7237f667e74Sjose borrego 		mutex_exit(&od->d_mutex);
7247f667e74Sjose borrego 		return (0);
7257f667e74Sjose borrego 	}
7267f667e74Sjose borrego 
7277f667e74Sjose borrego 	odirent = kmem_alloc(sizeof (smb_odirent_t), KM_SLEEP);
7285fd03bc0SGordon Ross 	bzero(&attr, sizeof (attr));
7297f667e74Sjose borrego 
7307f667e74Sjose borrego 	for (;;) {
7317f667e74Sjose borrego 		bzero(sinfo, sizeof (smb_streaminfo_t));
7327f667e74Sjose borrego 		if ((rc = smb_odir_next_odirent(od, odirent)) != 0)
7337f667e74Sjose borrego 			break;
7347f667e74Sjose borrego 
7357f667e74Sjose borrego 		if (strncmp(odirent->od_name, SMB_STREAM_PREFIX,
7367f667e74Sjose borrego 		    SMB_STREAM_PREFIX_LEN)) {
7377f667e74Sjose borrego 			continue;
738da6c28aaSamw 		}
739da6c28aaSamw 
74056a2adb6SPrashanth Badari 		/*
74156a2adb6SPrashanth Badari 		 * Hide streams that would be restricted if the caller
74256a2adb6SPrashanth Badari 		 * is also restricted.
74356a2adb6SPrashanth Badari 		 */
74456a2adb6SPrashanth Badari 		if ((od->d_flags & SMB_ODIR_FLAG_RESTRICTED) != 0 &&
74556a2adb6SPrashanth Badari 		    smb_strname_restricted(odirent->od_name))
74656a2adb6SPrashanth Badari 			continue;
74756a2adb6SPrashanth Badari 
748e3f2c991SKeyur Desai 		rc = smb_fsop_lookup(sr, od->d_cred, 0, od->d_tree->t_snode,
749e3f2c991SKeyur Desai 		    od->d_dnode, odirent->od_name, &fnode);
7507f667e74Sjose borrego 		if (rc == 0) {
751a90cf9f2SGordon Ross 			/*
752a90cf9f2SGordon Ross 			 * We just need the file sizes, and don't want
753a90cf9f2SGordon Ross 			 * EACCES failures here, so use kcred and pass
754a90cf9f2SGordon Ross 			 * NULL as the sr to skip sr->fid-ofile checks.
755a90cf9f2SGordon Ross 			 */
7565fd03bc0SGordon Ross 			attr.sa_mask = SMB_AT_SIZE | SMB_AT_ALLOCSZ;
757a90cf9f2SGordon Ross 			rc = smb_node_getattr(NULL, fnode, kcr, NULL, &attr);
758e3f2c991SKeyur Desai 			smb_node_release(fnode);
7597f667e74Sjose borrego 		}
7607f667e74Sjose borrego 
7617f667e74Sjose borrego 		if (rc == 0) {
7627f667e74Sjose borrego 			(void) strlcpy(sinfo->si_name,
7637f667e74Sjose borrego 			    odirent->od_name + SMB_STREAM_PREFIX_LEN,
7647f667e74Sjose borrego 			    sizeof (sinfo->si_name));
7657f667e74Sjose borrego 			sinfo->si_size = attr.sa_vattr.va_size;
766e3f2c991SKeyur Desai 			sinfo->si_alloc_size = attr.sa_allocsz;
7677f667e74Sjose borrego 			break;
7687f667e74Sjose borrego 		}
7697f667e74Sjose borrego 	}
7707f667e74Sjose borrego 	mutex_exit(&od->d_mutex);
7717f667e74Sjose borrego 
7727f667e74Sjose borrego 	kmem_free(odirent, sizeof (smb_odirent_t));
7737f667e74Sjose borrego 
7747f667e74Sjose borrego 	switch (rc) {
7757f667e74Sjose borrego 	case 0:
7767f667e74Sjose borrego 		*eof = B_FALSE;
7777f667e74Sjose borrego 		return (0);
7787f667e74Sjose borrego 	case ENOENT:
7797f667e74Sjose borrego 		*eof = B_TRUE;
7807f667e74Sjose borrego 		return (0);
781da6c28aaSamw 	default:
782a90cf9f2SGordon Ross 		return (rc);
783da6c28aaSamw 	}
7847f667e74Sjose borrego }
7857f667e74Sjose borrego 
7867f667e74Sjose borrego /*
7877f667e74Sjose borrego  * smb_odir_save_cookie
7887f667e74Sjose borrego  *
7897f667e74Sjose borrego  * Callers can save up to SMB_MAX_SEARCH cookies in the odir
7907f667e74Sjose borrego  * to be used as resume points for a 'find next' request.
7917f667e74Sjose borrego  */
7927f667e74Sjose borrego void
smb_odir_save_cookie(smb_odir_t * od,int idx,uint32_t cookie)7937f667e74Sjose borrego smb_odir_save_cookie(smb_odir_t *od, int idx, uint32_t cookie)
7947f667e74Sjose borrego {
7957f667e74Sjose borrego 	ASSERT(od);
7967f667e74Sjose borrego 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
7977f667e74Sjose borrego 	ASSERT(idx >= 0 && idx < SMB_MAX_SEARCH);
7987f667e74Sjose borrego 
7997f667e74Sjose borrego 	mutex_enter(&od->d_mutex);
8007f667e74Sjose borrego 	od->d_cookies[idx] = cookie;
801da6c28aaSamw 	mutex_exit(&od->d_mutex);
802da6c28aaSamw }
803da6c28aaSamw 
804bfbce3c1SGordon Ross /*
805bfbce3c1SGordon Ross  * smb_odir_save_fname
806bfbce3c1SGordon Ross  *
807bfbce3c1SGordon Ross  * Save a filename / offset pair, which are basically a
808bfbce3c1SGordon Ross  * one entry cache.  See smb_com_trans2_find_next2.
809bfbce3c1SGordon Ross  */
810bfbce3c1SGordon Ross void
smb_odir_save_fname(smb_odir_t * od,uint32_t cookie,const char * fname)811bfbce3c1SGordon Ross smb_odir_save_fname(smb_odir_t *od, uint32_t cookie, const char *fname)
812bfbce3c1SGordon Ross {
813bfbce3c1SGordon Ross 	ASSERT(od);
814bfbce3c1SGordon Ross 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
815bfbce3c1SGordon Ross 
816bfbce3c1SGordon Ross 	mutex_enter(&od->d_mutex);
817bfbce3c1SGordon Ross 
818bfbce3c1SGordon Ross 	od->d_last_cookie = cookie;
819bfbce3c1SGordon Ross 	bzero(od->d_last_name, MAXNAMELEN);
820bfbce3c1SGordon Ross 	if (fname != NULL)
821bfbce3c1SGordon Ross 		(void) strlcpy(od->d_last_name, fname, MAXNAMELEN);
822bfbce3c1SGordon Ross 
823bfbce3c1SGordon Ross 	mutex_exit(&od->d_mutex);
824bfbce3c1SGordon Ross }
825bfbce3c1SGordon Ross 
826da6c28aaSamw /*
8277f667e74Sjose borrego  * smb_odir_resume_at
8287f667e74Sjose borrego  *
829a90cf9f2SGordon Ross  * If SMB_ODIR_FLAG_WILDCARDS is not set, and we're rewinding,
830a90cf9f2SGordon Ross  * assume we're no longer at EOF.
831eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States  *
832eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States  * Wildcard searching can be resumed from:
8337f667e74Sjose borrego  * - the cookie saved at a specified index (SMBsearch, SMBfind).
8347f667e74Sjose borrego  * - a specified cookie (SMB_trans2_find)
8357f667e74Sjose borrego  * - a specified filename (SMB_trans2_find) - NOT SUPPORTED.
8367f667e74Sjose borrego  *   Defaults to continuing from where the last search ended.
8377f667e74Sjose borrego  *
8387f667e74Sjose borrego  * Continuation from where the last search ended (SMB_trans2_find)
8397f667e74Sjose borrego  * is implemented by saving the last cookie at a specific index (0)
8407f667e74Sjose borrego  * smb_odir_resume_at indicates a new request, so reset od->d_bufptr
8417f667e74Sjose borrego  * and d_eof to force a vop_readdir.
842da6c28aaSamw  */
8437f667e74Sjose borrego void
smb_odir_resume_at(smb_odir_t * od,smb_odir_resume_t * resume)8447f667e74Sjose borrego smb_odir_resume_at(smb_odir_t *od, smb_odir_resume_t *resume)
845da6c28aaSamw {
846a90cf9f2SGordon Ross 	uint64_t save_offset;
847a90cf9f2SGordon Ross 
8487f667e74Sjose borrego 	ASSERT(od);
8497f667e74Sjose borrego 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
8507f667e74Sjose borrego 	ASSERT(resume);
8517f667e74Sjose borrego 
852eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 	if ((od->d_flags & SMB_ODIR_FLAG_WILDCARDS) == 0) {
853a90cf9f2SGordon Ross 		if (resume->or_type == SMB_ODIR_RESUME_COOKIE)
854a90cf9f2SGordon Ross 			od->d_eof = B_FALSE;
855eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 		return;
856eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 	}
857a90cf9f2SGordon Ross 	mutex_enter(&od->d_mutex);
858eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 
859a90cf9f2SGordon Ross 	save_offset = od->d_offset;
8607f667e74Sjose borrego 	switch (resume->or_type) {
861bfbce3c1SGordon Ross 
862bfbce3c1SGordon Ross 	default:
863bfbce3c1SGordon Ross 	case SMB_ODIR_RESUME_CONT:
864bfbce3c1SGordon Ross 		/* Continue where we left off. */
865bfbce3c1SGordon Ross 		break;
866bfbce3c1SGordon Ross 
867bfbce3c1SGordon Ross 	case SMB_ODIR_RESUME_IDX:
868bfbce3c1SGordon Ross 		/*
869bfbce3c1SGordon Ross 		 * This is used only by the (ancient) SMB_SEARCH.
870bfbce3c1SGordon Ross 		 * Modern clients use trans2 FindFirst, FindNext.
871bfbce3c1SGordon Ross 		 */
872bfbce3c1SGordon Ross 		ASSERT(resume->or_idx >= 0);
873bfbce3c1SGordon Ross 		ASSERT(resume->or_idx < SMB_MAX_SEARCH);
874bfbce3c1SGordon Ross 
875bfbce3c1SGordon Ross 		if ((resume->or_idx < 0) ||
876bfbce3c1SGordon Ross 		    (resume->or_idx >= SMB_MAX_SEARCH)) {
877bfbce3c1SGordon Ross 			resume->or_idx = 0;
878bfbce3c1SGordon Ross 		}
879bfbce3c1SGordon Ross 		od->d_offset = od->d_cookies[resume->or_idx];
880bfbce3c1SGordon Ross 		break;
881bfbce3c1SGordon Ross 
882bfbce3c1SGordon Ross 	case SMB_ODIR_RESUME_COOKIE:
883bfbce3c1SGordon Ross 		od->d_offset = resume->or_cookie;
884bfbce3c1SGordon Ross 		break;
885bfbce3c1SGordon Ross 
886bfbce3c1SGordon Ross 	case SMB_ODIR_RESUME_FNAME:
887bfbce3c1SGordon Ross 		/*
888bfbce3c1SGordon Ross 		 * If the name matches the last one saved,
889bfbce3c1SGordon Ross 		 * use the offset that was saved with it in
890bfbce3c1SGordon Ross 		 * the odir.  Otherwise use the cookie value
891bfbce3c1SGordon Ross 		 * in the resume data from the client.
892bfbce3c1SGordon Ross 		 */
893bfbce3c1SGordon Ross 		if (strcmp(resume->or_fname, od->d_last_name) &&
894bfbce3c1SGordon Ross 		    od->d_last_cookie != 0) {
895bfbce3c1SGordon Ross 			od->d_offset = od->d_last_cookie;
896bfbce3c1SGordon Ross 		} else if (resume->or_cookie != 0) {
8977f667e74Sjose borrego 			od->d_offset = resume->or_cookie;
898bfbce3c1SGordon Ross 		} /* else continue where we left off */
899bfbce3c1SGordon Ross 		break;
900da6c28aaSamw 	}
9017f667e74Sjose borrego 
902a90cf9f2SGordon Ross 	if (od->d_offset != save_offset) {
903a90cf9f2SGordon Ross 		/* Force a vop_readdir to refresh d_buf */
904a90cf9f2SGordon Ross 		od->d_bufptr = NULL;
905a90cf9f2SGordon Ross 		od->d_eof = B_FALSE;
906a90cf9f2SGordon Ross 	}
9077f667e74Sjose borrego 
9087f667e74Sjose borrego 	mutex_exit(&od->d_mutex);
909da6c28aaSamw }
910da6c28aaSamw 
9117f667e74Sjose borrego 
9127f667e74Sjose borrego /* *** static functions *** */
913da6c28aaSamw 
914da6c28aaSamw /*
9157f667e74Sjose borrego  * smb_odir_create
9167f667e74Sjose borrego  * Allocate and populate an odir obect and add it to the tree's list.
917da6c28aaSamw  */
918a90cf9f2SGordon Ross static smb_odir_t *
smb_odir_create(smb_request_t * sr,smb_node_t * dnode,const char * pattern,uint16_t sattr,uint16_t odid,cred_t * cr)9197f667e74Sjose borrego smb_odir_create(smb_request_t *sr, smb_node_t *dnode,
92056a2adb6SPrashanth Badari     const char *pattern, uint16_t sattr, uint16_t odid, cred_t *cr)
921da6c28aaSamw {
9227f667e74Sjose borrego 	smb_odir_t	*od;
923da6c28aaSamw 	smb_tree_t	*tree;
924da6c28aaSamw 
9257f667e74Sjose borrego 	ASSERT(sr);
9267f667e74Sjose borrego 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
9277f667e74Sjose borrego 	ASSERT(sr->tid_tree);
9287f667e74Sjose borrego 	ASSERT(sr->tid_tree->t_magic == SMB_TREE_MAGIC);
9297f667e74Sjose borrego 	ASSERT(dnode);
9307f667e74Sjose borrego 	ASSERT(dnode->n_magic == SMB_NODE_MAGIC);
931da6c28aaSamw 
9327f667e74Sjose borrego 	tree = sr->tid_tree;
9337f667e74Sjose borrego 
9348622ec45SGordon Ross 	od = kmem_cache_alloc(smb_cache_odir, KM_SLEEP);
9357f667e74Sjose borrego 	bzero(od, sizeof (smb_odir_t));
9367f667e74Sjose borrego 
9377f667e74Sjose borrego 	mutex_init(&od->d_mutex, NULL, MUTEX_DEFAULT, NULL);
938a90cf9f2SGordon Ross 
939a90cf9f2SGordon Ross 	/*
940a90cf9f2SGordon Ross 	 * Return this to the caller as if they had done
941a90cf9f2SGordon Ross 	 * smb_tree_lookup_odir() to obtain the odir.
942a90cf9f2SGordon Ross 	 */
943a90cf9f2SGordon Ross 	od->d_refcnt = 1;
944a90cf9f2SGordon Ross 	od->d_state = SMB_ODIR_STATE_IN_USE;
9457f667e74Sjose borrego 	od->d_magic = SMB_ODIR_MAGIC;
9467f667e74Sjose borrego 	od->d_opened_by_pid = sr->smb_pid;
9477f667e74Sjose borrego 	od->d_session = tree->t_session;
948eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 	od->d_cred = cr;
9493b13a1efSThomas Keiser 	/*
9503b13a1efSThomas Keiser 	 * grab a ref for od->d_user
9513b13a1efSThomas Keiser 	 * released in  smb_odir_delete()
9523b13a1efSThomas Keiser 	 */
9533b13a1efSThomas Keiser 	smb_user_hold_internal(sr->uid_user);
9543b13a1efSThomas Keiser 	od->d_user = sr->uid_user;
9557f667e74Sjose borrego 	od->d_tree = tree;
9567f667e74Sjose borrego 	od->d_dnode = dnode;
9577f667e74Sjose borrego 	smb_node_ref(dnode);
9587f667e74Sjose borrego 	od->d_odid = odid;
9597f667e74Sjose borrego 	od->d_sattr = sattr;
9607f667e74Sjose borrego 	(void) strlcpy(od->d_pattern, pattern, sizeof (od->d_pattern));
9618b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	od->d_flags = 0;
962fe1c642dSBill Krier 	if (smb_contains_wildcards(od->d_pattern))
9638b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		od->d_flags |= SMB_ODIR_FLAG_WILDCARDS;
9648b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (vfs_has_feature(dnode->vp->v_vfsp, VFSFT_DIRENTFLAGS))
9658b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		od->d_flags |= SMB_ODIR_FLAG_EDIRENT;
9668b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (smb_tree_has_feature(tree, SMB_TREE_CASEINSENSITIVE))
9678b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		od->d_flags |= SMB_ODIR_FLAG_IGNORE_CASE;
968cb174861Sjoyce mcintosh 	if (smb_tree_has_feature(tree, SMB_TREE_SHORTNAMES))
969cb174861Sjoyce mcintosh 		od->d_flags |= SMB_ODIR_FLAG_SHORTNAMES;
970cdf79589SGordon Ross 	if (smb_tree_has_feature(tree, SMB_TREE_ACEMASKONACCESS))
971cdf79589SGordon Ross 		od->d_flags |= SMB_ODIR_FLAG_ACEACCESS;
9728b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (SMB_TREE_SUPPORTS_CATIA(sr))
9738b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		od->d_flags |= SMB_ODIR_FLAG_CATIA;
974e3f2c991SKeyur Desai 	if (SMB_TREE_SUPPORTS_ABE(sr))
975e3f2c991SKeyur Desai 		od->d_flags |= SMB_ODIR_FLAG_ABE;
976cb174861Sjoyce mcintosh 	if (dnode->flags & NODE_XATTR_DIR)
977cb174861Sjoyce mcintosh 		od->d_flags |= SMB_ODIR_FLAG_XATTR;
9787f667e74Sjose borrego 	od->d_eof = B_FALSE;
9797f667e74Sjose borrego 
9807f667e74Sjose borrego 	smb_llist_enter(&tree->t_odir_list, RW_WRITER);
9817f667e74Sjose borrego 	smb_llist_insert_tail(&tree->t_odir_list, od);
9827f667e74Sjose borrego 	smb_llist_exit(&tree->t_odir_list);
9837f667e74Sjose borrego 
9847f667e74Sjose borrego 	atomic_inc_32(&tree->t_session->s_dir_cnt);
985a90cf9f2SGordon Ross 	return (od);
986a90cf9f2SGordon Ross }
987a90cf9f2SGordon Ross 
988a90cf9f2SGordon Ross /*
989a90cf9f2SGordon Ross  * Set a new pattern, attributes, and rewind.
990a90cf9f2SGordon Ross  */
991a90cf9f2SGordon Ross void
smb_odir_reopen(smb_odir_t * od,const char * pattern,uint16_t sattr)992a90cf9f2SGordon Ross smb_odir_reopen(smb_odir_t *od, const char *pattern, uint16_t sattr)
993a90cf9f2SGordon Ross {
994a90cf9f2SGordon Ross 
995a90cf9f2SGordon Ross 	SMB_ODIR_VALID(od);
996a90cf9f2SGordon Ross 
997a90cf9f2SGordon Ross 	mutex_enter(&od->d_mutex);
998a90cf9f2SGordon Ross 	od->d_sattr = sattr;
999a90cf9f2SGordon Ross 	(void) strlcpy(od->d_pattern, pattern, sizeof (od->d_pattern));
1000a90cf9f2SGordon Ross 	if (smb_contains_wildcards(od->d_pattern))
1001a90cf9f2SGordon Ross 		od->d_flags |= SMB_ODIR_FLAG_WILDCARDS;
1002a90cf9f2SGordon Ross 	else
1003a90cf9f2SGordon Ross 		od->d_flags &= ~SMB_ODIR_FLAG_WILDCARDS;
1004a90cf9f2SGordon Ross 
1005a90cf9f2SGordon Ross 	/* Internal smb_odir_resume_at */
1006a90cf9f2SGordon Ross 	od->d_offset = 0;
1007a90cf9f2SGordon Ross 	od->d_bufptr = NULL;
1008a90cf9f2SGordon Ross 	od->d_eof = B_FALSE;
1009a90cf9f2SGordon Ross 
1010a90cf9f2SGordon Ross 	mutex_exit(&od->d_mutex);
1011da6c28aaSamw }
1012da6c28aaSamw 
1013da6c28aaSamw /*
10149fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * Delete an odir.
10157f667e74Sjose borrego  *
10169fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * Remove the odir from the tree list before freeing resources
10179fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * associated with the odir.
1018da6c28aaSamw  */
1019811599a4SMatt Barden static void
smb_odir_delete(void * arg)10209fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States smb_odir_delete(void *arg)
1021da6c28aaSamw {
10229fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	smb_tree_t	*tree;
10239fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	smb_odir_t	*od = (smb_odir_t *)arg;
10249fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
10259fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	SMB_ODIR_VALID(od);
10269fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	ASSERT(od->d_refcnt == 0);
1027da6c28aaSamw 	ASSERT(od->d_state == SMB_ODIR_STATE_CLOSED);
1028da6c28aaSamw 
10299fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	tree = od->d_tree;
10309fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	smb_llist_enter(&tree->t_odir_list, RW_WRITER);
10319fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	smb_llist_remove(&tree->t_odir_list, od);
1032a90cf9f2SGordon Ross 	if (od->d_odid != 0)
1033a90cf9f2SGordon Ross 		smb_idpool_free(&tree->t_odid_pool, od->d_odid);
10349fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	atomic_dec_32(&tree->t_session->s_dir_cnt);
10359fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	smb_llist_exit(&tree->t_odir_list);
10369fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
1037811599a4SMatt Barden 	/*
1038811599a4SMatt Barden 	 * This odir is no longer on t_odir_list, however...
1039811599a4SMatt Barden 	 *
1040811599a4SMatt Barden 	 * This is called via smb_llist_post, which means it may run
1041811599a4SMatt Barden 	 * BEFORE smb_odir_release drops d_mutex (if another thread
1042811599a4SMatt Barden 	 * flushes the delete queue before we do).  Synchronize.
1043811599a4SMatt Barden 	 */
10449fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	mutex_enter(&od->d_mutex);
10459fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	mutex_exit(&od->d_mutex);
1046da6c28aaSamw 
10477f667e74Sjose borrego 	od->d_magic = 0;
10487f667e74Sjose borrego 	smb_node_release(od->d_dnode);
10493b13a1efSThomas Keiser 	smb_user_release(od->d_user);
1050da6c28aaSamw 	mutex_destroy(&od->d_mutex);
10518622ec45SGordon Ross 	kmem_cache_free(smb_cache_odir, od);
1052da6c28aaSamw }
10537f667e74Sjose borrego 
1054*4b052b94SMatt Barden boolean_t smb_use_fs_abe = B_FALSE;
1055*4b052b94SMatt Barden 
10567f667e74Sjose borrego /*
10577f667e74Sjose borrego  * smb_odir_next_odirent
10587f667e74Sjose borrego  *
10597f667e74Sjose borrego  * Find the next directory entry in d_buf. If d_bufptr is NULL (buffer
10607f667e74Sjose borrego  * is empty or we've reached the end of it), read the next set of
10617f667e74Sjose borrego  * entries from the file system (vop_readdir).
10627f667e74Sjose borrego  *
10637f667e74Sjose borrego  * File systems which support VFSFT_EDIRENT_FLAGS will return the
10647f667e74Sjose borrego  * directory entries as a buffer of edirent_t structure. Others will
10657f667e74Sjose borrego  * return a buffer of dirent64_t structures.  For simplicity translate
10667f667e74Sjose borrego  * the data into an smb_odirent_t structure.
10677f667e74Sjose borrego  * The ed_name/d_name in d_buf is NULL terminated by the file system.
10687f667e74Sjose borrego  *
10697f667e74Sjose borrego  * Some file systems can have directories larger than SMB_MAXDIRSIZE.
10708b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States  * If the odirent offset >= SMB_MAXDIRSIZE return ENOENT and set d_eof
10718b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States  * to true to stop subsequent calls to smb_vop_readdir.
10727f667e74Sjose borrego  *
10737f667e74Sjose borrego  * Returns:
10747f667e74Sjose borrego  *      0 - success. odirent is populated with the next directory entry
10757f667e74Sjose borrego  * ENOENT - no more directory entries
10767f667e74Sjose borrego  *  errno - error
10777f667e74Sjose borrego  */
10787f667e74Sjose borrego static int
smb_odir_next_odirent(smb_odir_t * od,smb_odirent_t * odirent)10797f667e74Sjose borrego smb_odir_next_odirent(smb_odir_t *od, smb_odirent_t *odirent)
10807f667e74Sjose borrego {
10817f667e74Sjose borrego 	int		rc;
10827f667e74Sjose borrego 	int		reclen;
10837f667e74Sjose borrego 	int		eof;
10847f667e74Sjose borrego 	dirent64_t	*dp;
10857f667e74Sjose borrego 	edirent_t	*edp;
10868b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	char		*np;
1087d2488fe8SGordon Ross 	uint32_t	rddir_flags = 0;
10887f667e74Sjose borrego 
10897f667e74Sjose borrego 	ASSERT(MUTEX_HELD(&od->d_mutex));
10907f667e74Sjose borrego 
109148557149Sjoyce mcintosh 	bzero(odirent, sizeof (smb_odirent_t));
109248557149Sjoyce mcintosh 
1093*4b052b94SMatt Barden 	/*
1094*4b052b94SMatt Barden 	 * VOP_READDIR() won't return until either the buffer has filled or the
1095*4b052b94SMatt Barden 	 * end of the directory is reached. As a result, when we pass this flag,
1096*4b052b94SMatt Barden 	 * calls on very large directories can take an unacceptably long time to
1097*4b052b94SMatt Barden 	 * complete when the user doesn't have access to most entries. Until we
1098*4b052b94SMatt Barden 	 * can cap the amount of time spent in this call, callers will manually
1099*4b052b94SMatt Barden 	 * apply ABE to returned entries, instead of passing this flag.
1100*4b052b94SMatt Barden 	 */
1101*4b052b94SMatt Barden 	if (smb_use_fs_abe && (od->d_flags & SMB_ODIR_FLAG_ABE) != 0)
1102d2488fe8SGordon Ross 		rddir_flags |= SMB_ABE;
1103d2488fe8SGordon Ross 	if (od->d_flags & SMB_ODIR_FLAG_EDIRENT)
1104d2488fe8SGordon Ross 		rddir_flags |= SMB_EDIRENT;
1105d2488fe8SGordon Ross 
11067f667e74Sjose borrego 	if (od->d_bufptr != NULL) {
11078b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		if (od->d_flags & SMB_ODIR_FLAG_EDIRENT)
11088b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 			reclen = od->d_edp->ed_reclen;
11098b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		else
11108b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 			reclen = od->d_dp->d_reclen;
11117f667e74Sjose borrego 
11127f667e74Sjose borrego 		if (reclen == 0) {
11137f667e74Sjose borrego 			od->d_bufptr = NULL;
11147f667e74Sjose borrego 		} else {
11157f667e74Sjose borrego 			od->d_bufptr += reclen;
11167f667e74Sjose borrego 			if (od->d_bufptr >= od->d_buf + od->d_bufsize)
11177f667e74Sjose borrego 				od->d_bufptr = NULL;
11187f667e74Sjose borrego 		}
11197f667e74Sjose borrego 	}
11207f667e74Sjose borrego 
11217f667e74Sjose borrego 	if (od->d_bufptr == NULL) {
11227f667e74Sjose borrego 		if (od->d_eof)
11237f667e74Sjose borrego 			return (ENOENT);
11247f667e74Sjose borrego 
11257f667e74Sjose borrego 		od->d_bufsize = sizeof (od->d_buf);
11267f667e74Sjose borrego 
11277f667e74Sjose borrego 		rc = smb_vop_readdir(od->d_dnode->vp, od->d_offset,
1128d2488fe8SGordon Ross 		    od->d_buf, &od->d_bufsize, &eof, rddir_flags, od->d_cred);
11297f667e74Sjose borrego 
11307f667e74Sjose borrego 		if ((rc == 0) && (od->d_bufsize == 0))
11317f667e74Sjose borrego 			rc = ENOENT;
11327f667e74Sjose borrego 
11337f667e74Sjose borrego 		if (rc != 0) {
11347f667e74Sjose borrego 			od->d_bufptr = NULL;
11357f667e74Sjose borrego 			od->d_bufsize = 0;
11367f667e74Sjose borrego 			return (rc);
11377f667e74Sjose borrego 		}
11387f667e74Sjose borrego 
11397f667e74Sjose borrego 		od->d_eof = (eof != 0);
11407f667e74Sjose borrego 		od->d_bufptr = od->d_buf;
11417f667e74Sjose borrego 	}
11427f667e74Sjose borrego 
11438b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (od->d_flags & SMB_ODIR_FLAG_EDIRENT)
11448b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		od->d_offset = od->d_edp->ed_off;
11458b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	else
11468b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		od->d_offset = od->d_dp->d_off;
11478b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 
11487f667e74Sjose borrego 	if (od->d_offset >= SMB_MAXDIRSIZE) {
11497f667e74Sjose borrego 		od->d_bufptr = NULL;
11507f667e74Sjose borrego 		od->d_bufsize = 0;
11518b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		od->d_eof = B_TRUE;
11527f667e74Sjose borrego 		return (ENOENT);
11537f667e74Sjose borrego 	}
11547f667e74Sjose borrego 
11558b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (od->d_flags & SMB_ODIR_FLAG_EDIRENT) {
11567f667e74Sjose borrego 		edp = od->d_edp;
11577f667e74Sjose borrego 		odirent->od_ino = edp->ed_ino;
11587f667e74Sjose borrego 		odirent->od_eflags = edp->ed_eflags;
11598b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		np = edp->ed_name;
11607f667e74Sjose borrego 	} else {
11617f667e74Sjose borrego 		dp = od->d_dp;
11627f667e74Sjose borrego 		odirent->od_ino = dp->d_ino;
11637f667e74Sjose borrego 		odirent->od_eflags = 0;
11648b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		np =  dp->d_name;
11658b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	}
11668b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 
11678b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	if ((od->d_flags & SMB_ODIR_FLAG_CATIA) &&
11688b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	    ((od->d_flags & SMB_ODIR_FLAG_XATTR) == 0)) {
11698b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		smb_vop_catia_v4tov5(np, odirent->od_name,
11708b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		    sizeof (odirent->od_name));
11718b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	} else {
11728b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		(void) strlcpy(odirent->od_name, np,
11737f667e74Sjose borrego 		    sizeof (odirent->od_name));
11747f667e74Sjose borrego 	}
11757f667e74Sjose borrego 
11767f667e74Sjose borrego 	return (0);
11777f667e74Sjose borrego }
11787f667e74Sjose borrego 
11797f667e74Sjose borrego /*
11807f667e74Sjose borrego  * smb_odir_single_fileinfo
11817f667e74Sjose borrego  *
11827f667e74Sjose borrego  * Lookup the file identified by od->d_pattern.
11837f667e74Sjose borrego  *
11847f667e74Sjose borrego  * If the looked up file is a link, we attempt to lookup the link target
11857f667e74Sjose borrego  * to use its attributes in place of those of the files's.
11867f667e74Sjose borrego  * If we fail to lookup the target of the link we use the original
11877f667e74Sjose borrego  * file's attributes.
11887f667e74Sjose borrego  * Check if the attributes match the search attributes.
11897f667e74Sjose borrego  *
11907f667e74Sjose borrego  * Returns: 0 - success
11917f667e74Sjose borrego  *     ENOENT - no match
11927f667e74Sjose borrego  *      errno - error
11937f667e74Sjose borrego  */
11947f667e74Sjose borrego static int
smb_odir_single_fileinfo(smb_request_t * sr,smb_odir_t * od,smb_fileinfo_t * fileinfo)11957f667e74Sjose borrego smb_odir_single_fileinfo(smb_request_t *sr, smb_odir_t *od,
11967f667e74Sjose borrego     smb_fileinfo_t *fileinfo)
11977f667e74Sjose borrego {
11987f667e74Sjose borrego 	int		rc;
11997f667e74Sjose borrego 	smb_node_t	*fnode, *tgt_node;
1200037cac00Sjoyce mcintosh 	smb_attr_t	attr;
1201cb174861Sjoyce mcintosh 	ino64_t		fid;
12027f667e74Sjose borrego 	char		*name;
12038b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	boolean_t	case_conflict = B_FALSE;
12048b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	int		lookup_flags, flags = 0;
12058b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	vnode_t		*vp;
12067f667e74Sjose borrego 
12077f667e74Sjose borrego 	ASSERT(sr);
12087f667e74Sjose borrego 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
12097f667e74Sjose borrego 	ASSERT(od);
12107f667e74Sjose borrego 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
12117f667e74Sjose borrego 
12127f667e74Sjose borrego 	ASSERT(MUTEX_HELD(&od->d_mutex));
12137f667e74Sjose borrego 	bzero(fileinfo, sizeof (smb_fileinfo_t));
12147f667e74Sjose borrego 
1215eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 	rc = smb_fsop_lookup(sr, od->d_cred, 0, od->d_tree->t_snode,
1216037cac00Sjoyce mcintosh 	    od->d_dnode, od->d_pattern, &fnode);
12177f667e74Sjose borrego 	if (rc != 0)
12187f667e74Sjose borrego 		return (rc);
12197f667e74Sjose borrego 
12208b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	/*
12218b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	 * If case sensitive, do a case insensitive smb_vop_lookup to
12228b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	 * check for case conflict
12238b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	 */
12248b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (od->d_flags & SMB_ODIR_FLAG_IGNORE_CASE) {
12258b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		lookup_flags = SMB_IGNORE_CASE;
12268b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		if (od->d_flags & SMB_ODIR_FLAG_CATIA)
12278b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 			lookup_flags |= SMB_CATIA;
12288b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 
12298b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		rc = smb_vop_lookup(od->d_dnode->vp, fnode->od_name, &vp,
12308b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		    NULL, lookup_flags, &flags, od->d_tree->t_snode->vp,
12319fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		    NULL, od->d_cred);
12328b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		if (rc != 0)
12338b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 			return (rc);
12348b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		VN_RELE(vp);
12358b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 
12368b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		if (flags & ED_CASE_CONFLICT)
12378b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 			case_conflict = B_TRUE;
12388b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	}
12397f667e74Sjose borrego 
12405fd03bc0SGordon Ross 	bzero(&attr, sizeof (attr));
12415fd03bc0SGordon Ross 	attr.sa_mask = SMB_AT_ALL;
1242a90cf9f2SGordon Ross 	rc = smb_node_getattr(NULL, fnode, zone_kcred(), NULL, &attr);
12435fd03bc0SGordon Ross 	if (rc != 0) {
1244037cac00Sjoyce mcintosh 		smb_node_release(fnode);
1245037cac00Sjoyce mcintosh 		return (rc);
1246037cac00Sjoyce mcintosh 	}
1247037cac00Sjoyce mcintosh 
12487f667e74Sjose borrego 
12497f667e74Sjose borrego 	/* follow link to get target node & attr */
12509fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	if (smb_node_is_symlink(fnode) &&
12519fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	    smb_odir_lookup_link(sr, od, fnode->od_name, &tgt_node)) {
12527f667e74Sjose borrego 		smb_node_release(fnode);
12537f667e74Sjose borrego 		fnode = tgt_node;
12545fd03bc0SGordon Ross 		attr.sa_mask = SMB_AT_ALL;
1255a90cf9f2SGordon Ross 		rc = smb_node_getattr(NULL, fnode, zone_kcred(), NULL, &attr);
12565fd03bc0SGordon Ross 		if (rc != 0) {
1257037cac00Sjoyce mcintosh 			smb_node_release(fnode);
1258037cac00Sjoyce mcintosh 			return (rc);
1259037cac00Sjoyce mcintosh 		}
12607f667e74Sjose borrego 	}
12617f667e74Sjose borrego 
12627f667e74Sjose borrego 	/* check search attributes */
1263037cac00Sjoyce mcintosh 	if (!smb_sattr_check(attr.sa_dosattr, od->d_sattr)) {
12647f667e74Sjose borrego 		smb_node_release(fnode);
12657f667e74Sjose borrego 		return (ENOENT);
12667f667e74Sjose borrego 	}
12677f667e74Sjose borrego 
1268148c5f43SAlan Wright 	name = fnode->od_name;
1269cb174861Sjoyce mcintosh 	if (od->d_flags & SMB_ODIR_FLAG_SHORTNAMES) {
1270cb174861Sjoyce mcintosh 		fid = attr.sa_vattr.va_nodeid;
1271cb174861Sjoyce mcintosh 		if (case_conflict || smb_needs_mangled(name)) {
1272cb174861Sjoyce mcintosh 			smb_mangle(name, fid, fileinfo->fi_shortname,
1273cb174861Sjoyce mcintosh 			    SMB_SHORTNAMELEN);
1274cb174861Sjoyce mcintosh 		}
1275cb174861Sjoyce mcintosh 		if (case_conflict)
1276cb174861Sjoyce mcintosh 			name = fileinfo->fi_shortname;
1277cb174861Sjoyce mcintosh 	}
1278148c5f43SAlan Wright 
1279148c5f43SAlan Wright 	(void) strlcpy(fileinfo->fi_name, name, sizeof (fileinfo->fi_name));
1280148c5f43SAlan Wright 
1281037cac00Sjoyce mcintosh 	fileinfo->fi_dosattr = attr.sa_dosattr;
1282037cac00Sjoyce mcintosh 	fileinfo->fi_nodeid = attr.sa_vattr.va_nodeid;
1283037cac00Sjoyce mcintosh 	fileinfo->fi_size = attr.sa_vattr.va_size;
1284e3f2c991SKeyur Desai 	fileinfo->fi_alloc_size = attr.sa_allocsz;
1285037cac00Sjoyce mcintosh 	fileinfo->fi_atime = attr.sa_vattr.va_atime;
1286037cac00Sjoyce mcintosh 	fileinfo->fi_mtime = attr.sa_vattr.va_mtime;
1287037cac00Sjoyce mcintosh 	fileinfo->fi_ctime = attr.sa_vattr.va_ctime;
1288037cac00Sjoyce mcintosh 	if (attr.sa_crtime.tv_sec)
1289037cac00Sjoyce mcintosh 		fileinfo->fi_crtime = attr.sa_crtime;
12907f667e74Sjose borrego 	else
1291037cac00Sjoyce mcintosh 		fileinfo->fi_crtime = attr.sa_vattr.va_mtime;
12927f667e74Sjose borrego 
12937f667e74Sjose borrego 	smb_node_release(fnode);
12947f667e74Sjose borrego 	return (0);
12957f667e74Sjose borrego }
12967f667e74Sjose borrego 
12977f667e74Sjose borrego /*
12987f667e74Sjose borrego  * smb_odir_wildcard_fileinfo
12997f667e74Sjose borrego  *
13007f667e74Sjose borrego  * odirent contains a directory entry, obtained from a vop_readdir.
13017f667e74Sjose borrego  * If a case conflict is identified the filename is mangled and the
1302148c5f43SAlan Wright  * shortname is used as 'name', in place of odirent->od_name.
13037f667e74Sjose borrego  *
13047f667e74Sjose borrego  * If the looked up file is a link, we attempt to lookup the link target
13057f667e74Sjose borrego  * to use its attributes in place of those of the files's.
13067f667e74Sjose borrego  * If we fail to lookup the target of the link we use the original
13077f667e74Sjose borrego  * file's attributes.
13087f667e74Sjose borrego  * Check if the attributes match the search attributes.
13097f667e74Sjose borrego  *
13107f667e74Sjose borrego  * Although some file systems can have directories larger than
13117f667e74Sjose borrego  * SMB_MAXDIRSIZE smb_odir_next_odirent ensures that no offset larger
13127f667e74Sjose borrego  * than SMB_MAXDIRSIZE is returned.  It is therefore safe to use the
13137f667e74Sjose borrego  * offset as the cookie (uint32_t).
13147f667e74Sjose borrego  *
1315cdf79589SGordon Ross  * Returns: 0 - success (return this entry)
1316cdf79589SGordon Ross  *  any errno - no match, proceed to next entry
13177f667e74Sjose borrego  */
13187f667e74Sjose borrego static int
smb_odir_wildcard_fileinfo(smb_request_t * sr,smb_odir_t * od,smb_odirent_t * odirent,smb_fileinfo_t * fileinfo)13197f667e74Sjose borrego smb_odir_wildcard_fileinfo(smb_request_t *sr, smb_odir_t *od,
13207f667e74Sjose borrego     smb_odirent_t *odirent, smb_fileinfo_t *fileinfo)
13217f667e74Sjose borrego {
1322037cac00Sjoyce mcintosh 	smb_attr_t	attr;
13237f667e74Sjose borrego 	char		*name;
1324cdf79589SGordon Ross 	vnode_t		*fvp = NULL;
1325cdf79589SGordon Ross 	int		de_flags;
1326cdf79589SGordon Ross 	int		rc;
13277f667e74Sjose borrego 	boolean_t	case_conflict;
13287f667e74Sjose borrego 
13297f667e74Sjose borrego 	ASSERT(sr);
13307f667e74Sjose borrego 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
13317f667e74Sjose borrego 	ASSERT(od);
13327f667e74Sjose borrego 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
13337f667e74Sjose borrego 
13347f667e74Sjose borrego 	ASSERT(MUTEX_HELD(&od->d_mutex));
13357f667e74Sjose borrego 	bzero(fileinfo, sizeof (smb_fileinfo_t));
13367f667e74Sjose borrego 
1337cdf79589SGordon Ross 	bzero(&attr, sizeof (attr));
1338cdf79589SGordon Ross 	attr.sa_mask = SMB_AT_ALL;
1339cdf79589SGordon Ross 
1340cdf79589SGordon Ross 	/*
1341cdf79589SGordon Ross 	 * Lookup the vnode and get attributes.  We have the real
1342cdf79589SGordon Ross 	 * (on disk) name here from readdir, so CS lookup.
1343cdf79589SGordon Ross 	 * NB the getattr is done with kcred.
1344cdf79589SGordon Ross 	 */
1345cdf79589SGordon Ross 	rc = smb_vop_lookup(od->d_dnode->vp, odirent->od_name, &fvp,
1346cdf79589SGordon Ross 	    NULL, SMB_CASE_SENSITIVE, &de_flags,
1347cdf79589SGordon Ross 	    od->d_tree->t_snode->vp, &attr, od->d_cred);
1348cdf79589SGordon Ross 
13497f667e74Sjose borrego 	if (rc != 0)
13507f667e74Sjose borrego 		return (rc);
13517f667e74Sjose borrego 
1352cdf79589SGordon Ross 	/*
1353cdf79589SGordon Ross 	 * follow link to get target node & attr
1354cdf79589SGordon Ross 	 */
1355cdf79589SGordon Ross 	if (attr.sa_vattr.va_type == VLNK &&
1356cdf79589SGordon Ross 	    (attr.sa_dosattr & FILE_ATTRIBUTE_REPARSE_POINT) == 0) {
1357cdf79589SGordon Ross 		/*
1358cdf79589SGordon Ross 		 * Follow the symlink (lookup again w/ follow)
1359cdf79589SGordon Ross 		 * Like smb_odir_lookup_link
1360cdf79589SGordon Ross 		 *
1361cdf79589SGordon Ross 		 * Could avoid creating an smb_node_t here,
1362cdf79589SGordon Ross 		 * but symlinks are not so common.
1363cdf79589SGordon Ross 		 */
1364cdf79589SGordon Ross 		smb_node_t *tnode = NULL;
1365cdf79589SGordon Ross 		vnode_t *tvp = NULL;
1366cdf79589SGordon Ross 
1367cdf79589SGordon Ross 		if (smb_odir_lookup_link(sr, od, odirent->od_name, &tnode)) {
1368cdf79589SGordon Ross 
1369cdf79589SGordon Ross 			attr.sa_mask = SMB_AT_ALL;
1370cdf79589SGordon Ross 			rc = smb_fsop_getattr(NULL, zone_kcred(), tnode, &attr);
1371cdf79589SGordon Ross 			if (rc != 0) {
1372cdf79589SGordon Ross 				smb_node_release(tnode);
1373cdf79589SGordon Ross 				VN_RELE(fvp);
1374cdf79589SGordon Ross 				return (rc);
1375cdf79589SGordon Ross 			}
1376cdf79589SGordon Ross 
1377cdf79589SGordon Ross 			/* Use the link target as fvp */
1378cdf79589SGordon Ross 			tvp = tnode->vp;
1379cdf79589SGordon Ross 			VN_HOLD(tvp);
1380cdf79589SGordon Ross 			smb_node_release(tnode);
1381cdf79589SGordon Ross 
1382cdf79589SGordon Ross 			VN_RELE(fvp);
1383cdf79589SGordon Ross 			fvp = tvp;
1384cdf79589SGordon Ross 		}
1385037cac00Sjoyce mcintosh 	}
1386037cac00Sjoyce mcintosh 
1387cdf79589SGordon Ross 	/*
1388cdf79589SGordon Ross 	 * skip system files
1389cdf79589SGordon Ross 	 * Like smb_node_is_system(fnode)
1390cdf79589SGordon Ross 	 */
1391cdf79589SGordon Ross 	if (smb_node_is_vfsroot(od->d_dnode) &&
1392cdf79589SGordon Ross 	    (strcasecmp(odirent->od_name, ".$EXTEND") == 0)) {
1393cdf79589SGordon Ross 		VN_RELE(fvp);
13949fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		return (ENOENT);
13959fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	}
13969fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
13975a485655SKevin Crowe 	/*
1398cdf79589SGordon Ross 	 * check search attributes
13995a485655SKevin Crowe 	 */
1400037cac00Sjoyce mcintosh 	if (!smb_sattr_check(attr.sa_dosattr, od->d_sattr)) {
1401cdf79589SGordon Ross 		VN_RELE(fvp);
14027f667e74Sjose borrego 		return (ENOENT);
14037f667e74Sjose borrego 	}
14047f667e74Sjose borrego 
1405cdf79589SGordon Ross 	/*
1406cdf79589SGordon Ross 	 * Access Based Enumeration (ABE) checks
1407cdf79589SGordon Ross 	 * Skip this entry if the user can't read it.
1408cdf79589SGordon Ross 	 */
1409cdf79589SGordon Ross 	if ((od->d_flags & SMB_ODIR_FLAG_ABE) != 0) {
1410cdf79589SGordon Ross 		int atype;
1411cdf79589SGordon Ross 		int aflags;
1412cdf79589SGordon Ross 
1413cdf79589SGordon Ross 		if ((od->d_flags & SMB_ODIR_FLAG_ACEACCESS) != 0) {
1414cdf79589SGordon Ross 			atype = V_ACE_MASK;
1415cdf79589SGordon Ross 			aflags = ACE_READ_DATA;
1416cdf79589SGordon Ross 		} else {
1417cdf79589SGordon Ross 			atype = 0;
1418cdf79589SGordon Ross 			aflags = S_IRUSR;
1419cdf79589SGordon Ross 		}
1420cdf79589SGordon Ross 		rc = smb_vop_access(fvp, aflags, atype,
1421cdf79589SGordon Ross 		    od->d_dnode->vp, od->d_cred);
1422cdf79589SGordon Ross 		if (rc != 0) {
1423cdf79589SGordon Ross 			/* skip this entry */
1424cdf79589SGordon Ross 			VN_RELE(fvp);
1425cdf79589SGordon Ross 			return (rc);
1426cdf79589SGordon Ross 		}
1427cdf79589SGordon Ross 	}
1428cdf79589SGordon Ross 
1429cdf79589SGordon Ross 	/*
1430cdf79589SGordon Ross 	 * Deal with names that have conflicts (other names
1431cdf79589SGordon Ross 	 * that match this one if compared case-insensitive)
1432cdf79589SGordon Ross 	 */
1433cb174861Sjoyce mcintosh 	name = odirent->od_name;
1434cb174861Sjoyce mcintosh 	if (od->d_flags & SMB_ODIR_FLAG_SHORTNAMES) {
1435cb174861Sjoyce mcintosh 		case_conflict = ((od->d_flags & SMB_ODIR_FLAG_IGNORE_CASE) &&
1436cb174861Sjoyce mcintosh 		    (odirent->od_eflags & ED_CASE_CONFLICT));
1437cb174861Sjoyce mcintosh 		if (case_conflict || smb_needs_mangled(name)) {
1438cb174861Sjoyce mcintosh 			smb_mangle(name, odirent->od_ino,
1439cb174861Sjoyce mcintosh 			    fileinfo->fi_shortname, SMB_SHORTNAMELEN);
1440cb174861Sjoyce mcintosh 		}
1441cb174861Sjoyce mcintosh 		if (case_conflict)
1442cb174861Sjoyce mcintosh 			name = fileinfo->fi_shortname;
1443148c5f43SAlan Wright 	}
1444148c5f43SAlan Wright 
1445148c5f43SAlan Wright 	(void) strlcpy(fileinfo->fi_name, name, sizeof (fileinfo->fi_name));
1446148c5f43SAlan Wright 
14477f667e74Sjose borrego 	fileinfo->fi_cookie = (uint32_t)od->d_offset;
1448037cac00Sjoyce mcintosh 	fileinfo->fi_dosattr = attr.sa_dosattr;
1449037cac00Sjoyce mcintosh 	fileinfo->fi_nodeid = attr.sa_vattr.va_nodeid;
1450037cac00Sjoyce mcintosh 	fileinfo->fi_size = attr.sa_vattr.va_size;
1451e3f2c991SKeyur Desai 	fileinfo->fi_alloc_size = attr.sa_allocsz;
1452037cac00Sjoyce mcintosh 	fileinfo->fi_atime = attr.sa_vattr.va_atime;
1453037cac00Sjoyce mcintosh 	fileinfo->fi_mtime = attr.sa_vattr.va_mtime;
1454037cac00Sjoyce mcintosh 	fileinfo->fi_ctime = attr.sa_vattr.va_ctime;
1455037cac00Sjoyce mcintosh 	if (attr.sa_crtime.tv_sec)
1456037cac00Sjoyce mcintosh 		fileinfo->fi_crtime = attr.sa_crtime;
14577f667e74Sjose borrego 	else
1458037cac00Sjoyce mcintosh 		fileinfo->fi_crtime = attr.sa_vattr.va_mtime;
14597f667e74Sjose borrego 
1460cdf79589SGordon Ross 
1461cdf79589SGordon Ross 	VN_RELE(fvp);
1462cdf79589SGordon Ross 
14637f667e74Sjose borrego 	return (0);
14647f667e74Sjose borrego }
14657f667e74Sjose borrego 
14667f667e74Sjose borrego /*
14677f667e74Sjose borrego  * smb_odir_lookup_link
14687f667e74Sjose borrego  *
14697f667e74Sjose borrego  * If the file is a symlink we lookup the object to which the
14707f667e74Sjose borrego  * symlink refers so that we can return its attributes.
14717f667e74Sjose borrego  * This can cause a problem if a symlink in a sub-directory
14727f667e74Sjose borrego  * points to a parent directory (some UNIX GUI's create a symlink
14737f667e74Sjose borrego  * in $HOME/.desktop that points to the user's home directory).
14747f667e74Sjose borrego  * Some Windows applications (e.g. virus scanning) loop/hang
14757f667e74Sjose borrego  * trying to follow this recursive path and there is little
14767f667e74Sjose borrego  * we can do because the path is constructed on the client.
14777f667e74Sjose borrego  * smb_dirsymlink_enable allows an end-user to disable
14787f667e74Sjose borrego  * symlinks to directories. Symlinks to other object types
14797f667e74Sjose borrego  * should be unaffected.
14807f667e74Sjose borrego  *
1481148c5f43SAlan Wright  * Returns: B_TRUE  - followed link. tgt_node and tgt_attr set
14827f667e74Sjose borrego  *          B_FALSE - link not followed
14837f667e74Sjose borrego  */
14847f667e74Sjose borrego static boolean_t
smb_odir_lookup_link(smb_request_t * sr,smb_odir_t * od,char * fname,smb_node_t ** tgt_node)14857f667e74Sjose borrego smb_odir_lookup_link(smb_request_t *sr, smb_odir_t *od,
1486037cac00Sjoyce mcintosh     char *fname, smb_node_t **tgt_node)
14877f667e74Sjose borrego {
14887f667e74Sjose borrego 	int rc;
1489148c5f43SAlan Wright 	uint32_t flags = SMB_FOLLOW_LINKS | SMB_CASE_SENSITIVE;
14907f667e74Sjose borrego 
1491148c5f43SAlan Wright 	rc = smb_fsop_lookup(sr, od->d_cred, flags,
1492037cac00Sjoyce mcintosh 	    od->d_tree->t_snode, od->d_dnode, fname, tgt_node);
14937f667e74Sjose borrego 	if (rc != 0) {
14947f667e74Sjose borrego 		*tgt_node = NULL;
14957f667e74Sjose borrego 		return (B_FALSE);
14967f667e74Sjose borrego 	}
14977f667e74Sjose borrego 
1498037cac00Sjoyce mcintosh 	if (smb_node_is_dir(*tgt_node) && (!smb_dirsymlink_enable)) {
14997f667e74Sjose borrego 		smb_node_release(*tgt_node);
15007f667e74Sjose borrego 		*tgt_node = NULL;
15017f667e74Sjose borrego 		return (B_FALSE);
15027f667e74Sjose borrego 	}
15037f667e74Sjose borrego 
15047f667e74Sjose borrego 	return (B_TRUE);
15057f667e74Sjose borrego }
1506cb174861Sjoyce mcintosh 
1507cb174861Sjoyce mcintosh /*
1508cb174861Sjoyce mcintosh  * smb_odir_match_name
1509cb174861Sjoyce mcintosh  *
1510cb174861Sjoyce mcintosh  * Check if the directory entry name matches the search pattern:
1511cb174861Sjoyce mcintosh  * - Don't match reserved dos filenames.
1512cb174861Sjoyce mcintosh  * - Check if odirent->od_name matches od->d_pattern.
1513cb174861Sjoyce mcintosh  * - If shortnames are supported, generate the shortname from
1514cb174861Sjoyce mcintosh  *   odirent->od_name and check if it matches od->d_pattern.
1515cb174861Sjoyce mcintosh  */
1516c13be35aSGordon Ross static boolean_t
smb_odir_match_name(smb_odir_t * od,smb_odirent_t * odirent)1517cb174861Sjoyce mcintosh smb_odir_match_name(smb_odir_t *od, smb_odirent_t *odirent)
1518cb174861Sjoyce mcintosh {
1519cb174861Sjoyce mcintosh 	char	*name = odirent->od_name;
1520cb174861Sjoyce mcintosh 	char	shortname[SMB_SHORTNAMELEN];
1521cb174861Sjoyce mcintosh 	ino64_t	ino = odirent->od_ino;
1522c13be35aSGordon Ross 	boolean_t ci = (od->d_flags & SMB_ODIR_FLAG_IGNORE_CASE) != 0;
1523cb174861Sjoyce mcintosh 
1524cb174861Sjoyce mcintosh 	if (smb_is_reserved_dos_name(name))
1525cb174861Sjoyce mcintosh 		return (B_FALSE);
1526cb174861Sjoyce mcintosh 
1527c13be35aSGordon Ross 	if (smb_match(od->d_pattern, name, ci))
1528cb174861Sjoyce mcintosh 		return (B_TRUE);
1529cb174861Sjoyce mcintosh 
1530cb174861Sjoyce mcintosh 	if (od->d_flags & SMB_ODIR_FLAG_SHORTNAMES) {
1531cb174861Sjoyce mcintosh 		smb_mangle(name, ino, shortname, SMB_SHORTNAMELEN);
1532c13be35aSGordon Ross 		if (smb_match(od->d_pattern, shortname, ci))
1533cb174861Sjoyce mcintosh 			return (B_TRUE);
1534cb174861Sjoyce mcintosh 	}
1535cb174861Sjoyce mcintosh 
1536cb174861Sjoyce mcintosh 	return (B_FALSE);
1537cb174861Sjoyce mcintosh }
1538