xref: /illumos-gate/usr/src/uts/common/fs/smbsrv/smb_odir.c (revision a90cf9f29973990687fa61de9f1f6ea22e924e40)
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.
235a485655SKevin Crowe  * Copyright 2015 Nexenta Systems, 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  * ---------------
145*a90cf9f2SGordon 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.
148*a90cf9f2SGordon Ross  *	Returns NT status.
149*a90cf9f2SGordon Ross  *
150*a90cf9f2SGordon Ross  * smb_odir_openfh(smb_ofile_t *of)
151*a90cf9f2SGordon Ross  *	Create an odir representing the directory specified by the
152*a90cf9f2SGordon Ross  *	existing open handle (from a prior open of the directory).
153*a90cf9f2SGordon 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.
159*a90cf9f2SGordon 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 */
257*a90cf9f2SGordon Ross static smb_odir_t *smb_odir_create(smb_request_t *, smb_node_t *,
258*a90cf9f2SGordon 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 *);
267da6c28aaSamw 
268da6c28aaSamw 
269da6c28aaSamw /*
270*a90cf9f2SGordon Ross  * smb_odir_openpath
2717f667e74Sjose borrego  *
2727f667e74Sjose borrego  * Create an odir representing the directory specified in pathname.
2737f667e74Sjose borrego  *
2747f667e74Sjose borrego  * Returns:
275*a90cf9f2SGordon Ross  *    NT Status
276da6c28aaSamw  */
277*a90cf9f2SGordon Ross uint32_t
278*a90cf9f2SGordon Ross smb_odir_openpath(smb_request_t *sr, char *path, uint16_t sattr,
279*a90cf9f2SGordon Ross 	uint32_t flags, smb_odir_t **odp)
280da6c28aaSamw {
2817f667e74Sjose borrego 	int		rc;
2827f667e74Sjose borrego 	smb_tree_t	*tree;
2837f667e74Sjose borrego 	smb_node_t	*dnode;
2847f667e74Sjose borrego 	char		pattern[MAXNAMELEN];
285cb174861Sjoyce mcintosh 	uint16_t 	odid;
286eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 	cred_t		*cr;
287da6c28aaSamw 
2887f667e74Sjose borrego 	ASSERT(sr);
2897f667e74Sjose borrego 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
2907f667e74Sjose borrego 	ASSERT(sr->tid_tree);
2917f667e74Sjose borrego 	ASSERT(sr->tid_tree->t_magic == SMB_TREE_MAGIC);
292*a90cf9f2SGordon Ross 	*odp = NULL;
293da6c28aaSamw 
2947f667e74Sjose borrego 	tree = sr->tid_tree;
2957f667e74Sjose borrego 
296c13be35aSGordon Ross 	if (sr->session->dialect < NT_LM_0_12)
297fe1c642dSBill Krier 		smb_convert_wildcards(path);
298fe1c642dSBill Krier 
2997f667e74Sjose borrego 	rc = smb_pathname_reduce(sr, sr->user_cr, path,
3007f667e74Sjose borrego 	    tree->t_snode, tree->t_snode, &dnode, pattern);
301*a90cf9f2SGordon Ross 	if (rc != 0)
302*a90cf9f2SGordon Ross 		return (smb_errno2status(rc));
303da6c28aaSamw 
3049fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	if (!smb_node_is_dir(dnode)) {
3057f667e74Sjose borrego 		smb_node_release(dnode);
306*a90cf9f2SGordon Ross 		return (NT_STATUS_OBJECT_PATH_NOT_FOUND);
307da6c28aaSamw 	}
308da6c28aaSamw 
3097f667e74Sjose borrego 	if (smb_fsop_access(sr, sr->user_cr, dnode, FILE_LIST_DIRECTORY) != 0) {
3107f667e74Sjose borrego 		smb_node_release(dnode);
311*a90cf9f2SGordon Ross 		return (NT_STATUS_ACCESS_DENIED);
312*a90cf9f2SGordon Ross 	}
313*a90cf9f2SGordon Ross 
314*a90cf9f2SGordon Ross 	if (smb_idpool_alloc(&tree->t_odid_pool, &odid)) {
315*a90cf9f2SGordon Ross 		smb_node_release(dnode);
316*a90cf9f2SGordon Ross 		return (NT_STATUS_TOO_MANY_OPENED_FILES);
3177f667e74Sjose borrego 	}
318da6c28aaSamw 
319eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (flags & SMB_ODIR_OPENF_BACKUP_INTENT)
3203b13a1efSThomas Keiser 		cr = smb_user_getprivcred(sr->uid_user);
321eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 	else
3223b13a1efSThomas Keiser 		cr = sr->uid_user->u_cred;
323eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 
324*a90cf9f2SGordon Ross 	*odp = smb_odir_create(sr, dnode, pattern, sattr, odid, cr);
3257f667e74Sjose borrego 	smb_node_release(dnode);
326*a90cf9f2SGordon Ross 
327*a90cf9f2SGordon Ross 	return (0);
328*a90cf9f2SGordon Ross }
329*a90cf9f2SGordon Ross 
330*a90cf9f2SGordon Ross /*
331*a90cf9f2SGordon Ross  * smb_odir_openfh
332*a90cf9f2SGordon Ross  *
333*a90cf9f2SGordon Ross  * Create an odir representing the directory already opened on "of".
334*a90cf9f2SGordon Ross  *
335*a90cf9f2SGordon Ross  * Returns:
336*a90cf9f2SGordon Ross  *    NT status
337*a90cf9f2SGordon Ross  */
338*a90cf9f2SGordon Ross uint32_t
339*a90cf9f2SGordon Ross smb_odir_openfh(smb_request_t *sr, const char *pattern, uint16_t sattr,
340*a90cf9f2SGordon Ross 	smb_odir_t **odp)
341*a90cf9f2SGordon Ross {
342*a90cf9f2SGordon Ross 	smb_ofile_t	*of = sr->fid_ofile;
343*a90cf9f2SGordon Ross 
344*a90cf9f2SGordon Ross 	*odp = NULL;
345*a90cf9f2SGordon Ross 
346*a90cf9f2SGordon Ross 	if (of->f_node == NULL || !smb_node_is_dir(of->f_node))
347*a90cf9f2SGordon Ross 		return (NT_STATUS_INVALID_PARAMETER);
348*a90cf9f2SGordon Ross 
349*a90cf9f2SGordon Ross 	if ((of->f_granted_access & FILE_LIST_DIRECTORY) == 0)
350*a90cf9f2SGordon Ross 		return (NT_STATUS_ACCESS_DENIED);
351*a90cf9f2SGordon Ross 
352*a90cf9f2SGordon Ross 	*odp = smb_odir_create(sr, of->f_node, pattern, sattr, 0, of->f_cr);
353*a90cf9f2SGordon Ross 
354*a90cf9f2SGordon Ross 	return (0);
3557f667e74Sjose borrego }
3567f667e74Sjose borrego 
3577f667e74Sjose borrego /*
3587f667e74Sjose borrego  * smb_odir_openat
3597f667e74Sjose borrego  *
3607f667e74Sjose borrego  * Create an odir representing the extended attribute directory
3617f667e74Sjose borrego  * associated with the file (or directory) represented by unode.
3627f667e74Sjose borrego  *
3637f667e74Sjose borrego  * Returns:
364*a90cf9f2SGordon Ross  *    NT status
3657f667e74Sjose borrego  */
366*a90cf9f2SGordon Ross uint32_t
367*a90cf9f2SGordon Ross smb_odir_openat(smb_request_t *sr, smb_node_t *unode, smb_odir_t **odp)
3687f667e74Sjose borrego {
3697f667e74Sjose borrego 	char		pattern[SMB_STREAM_PREFIX_LEN + 2];
370*a90cf9f2SGordon Ross 	vnode_t		*xattr_dvp;
371*a90cf9f2SGordon Ross 	cred_t		*cr;
3727f667e74Sjose borrego 	smb_node_t	*xattr_dnode;
373*a90cf9f2SGordon Ross 	int		rc;
3747f667e74Sjose borrego 
3757f667e74Sjose borrego 	ASSERT(sr);
3767f667e74Sjose borrego 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
3777f667e74Sjose borrego 	ASSERT(unode);
3787f667e74Sjose borrego 	ASSERT(unode->n_magic == SMB_NODE_MAGIC);
379*a90cf9f2SGordon Ross 	*odp = NULL;
3807f667e74Sjose borrego 
381743a77edSAlan Wright 	if (SMB_TREE_CONTAINS_NODE(sr, unode) == 0 ||
382*a90cf9f2SGordon Ross 	    SMB_TREE_HAS_ACCESS(sr, ACE_LIST_DIRECTORY) == 0)
383*a90cf9f2SGordon Ross 		return (NT_STATUS_ACCESS_DENIED);
384*a90cf9f2SGordon Ross 
3858622ec45SGordon Ross 	cr = zone_kcred();
3867f667e74Sjose borrego 
3877f667e74Sjose borrego 	/* find the xattrdir vnode */
3887f667e74Sjose borrego 	rc = smb_vop_lookup_xattrdir(unode->vp, &xattr_dvp, LOOKUP_XATTR, cr);
389*a90cf9f2SGordon Ross 	if (rc != 0)
390*a90cf9f2SGordon Ross 		return (smb_errno2status(rc));
3917f667e74Sjose borrego 
3927f667e74Sjose borrego 	/* lookup the xattrdir's smb_node */
3937f667e74Sjose borrego 	xattr_dnode = smb_node_lookup(sr, NULL, cr, xattr_dvp, XATTR_DIR,
394037cac00Sjoyce mcintosh 	    unode, NULL);
3957f667e74Sjose borrego 	VN_RELE(xattr_dvp);
396*a90cf9f2SGordon Ross 	if (xattr_dnode == NULL)
397*a90cf9f2SGordon Ross 		return (NT_STATUS_NO_MEMORY);
3987f667e74Sjose borrego 
3997f667e74Sjose borrego 	(void) snprintf(pattern, sizeof (pattern), "%s*", SMB_STREAM_PREFIX);
400*a90cf9f2SGordon Ross 	*odp = smb_odir_create(sr, xattr_dnode, pattern,
401*a90cf9f2SGordon Ross 	    SMB_SEARCH_ATTRIBUTES, 0, cr);
402*a90cf9f2SGordon Ross 
4037f667e74Sjose borrego 	smb_node_release(xattr_dnode);
404*a90cf9f2SGordon Ross 	return (0);
4057f667e74Sjose borrego }
4067f667e74Sjose borrego 
4077f667e74Sjose borrego /*
4087f667e74Sjose borrego  * smb_odir_hold
409a1511e6bSjoyce mcintosh  *
410a1511e6bSjoyce mcintosh  * A hold will only be granted if the odir is open or in_use.
4117f667e74Sjose borrego  */
4127f667e74Sjose borrego boolean_t
4137f667e74Sjose borrego smb_odir_hold(smb_odir_t *od)
4147f667e74Sjose borrego {
4157f667e74Sjose borrego 	ASSERT(od);
4167f667e74Sjose borrego 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
4177f667e74Sjose borrego 
4187f667e74Sjose borrego 	mutex_enter(&od->d_mutex);
419a1511e6bSjoyce mcintosh 
420a1511e6bSjoyce mcintosh 	switch (od->d_state) {
421a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_OPEN:
422a1511e6bSjoyce mcintosh 		od->d_refcnt++;
423a1511e6bSjoyce mcintosh 		od->d_state = SMB_ODIR_STATE_IN_USE;
424a1511e6bSjoyce mcintosh 		break;
425a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_IN_USE:
426a1511e6bSjoyce mcintosh 		od->d_refcnt++;
427a1511e6bSjoyce mcintosh 		break;
428a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_CLOSING:
429a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_CLOSED:
430a1511e6bSjoyce mcintosh 	default:
4317f667e74Sjose borrego 		mutex_exit(&od->d_mutex);
4327f667e74Sjose borrego 		return (B_FALSE);
4337f667e74Sjose borrego 	}
4347f667e74Sjose borrego 
4357f667e74Sjose borrego 	mutex_exit(&od->d_mutex);
4367f667e74Sjose borrego 	return (B_TRUE);
4377f667e74Sjose borrego }
4387f667e74Sjose borrego 
4397f667e74Sjose borrego /*
4409fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * If the odir is in SMB_ODIR_STATE_CLOSING and this release results in
4419fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * a refcnt of 0, change the state to SMB_ODIR_STATE_CLOSED and post the
4429fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * object for deletion.  Object deletion is deferred to avoid modifying
4439fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * a list while an iteration may be in progress.
4447f667e74Sjose borrego  */
4457f667e74Sjose borrego void
4467f667e74Sjose borrego smb_odir_release(smb_odir_t *od)
4477f667e74Sjose borrego {
4489fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	SMB_ODIR_VALID(od);
4497f667e74Sjose borrego 
4507f667e74Sjose borrego 	mutex_enter(&od->d_mutex);
451a1511e6bSjoyce mcintosh 	ASSERT(od->d_refcnt > 0);
4527f667e74Sjose borrego 
4537f667e74Sjose borrego 	switch (od->d_state) {
4547f667e74Sjose borrego 	case SMB_ODIR_STATE_OPEN:
455a1511e6bSjoyce mcintosh 		break;
456a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_IN_USE:
4577f667e74Sjose borrego 		od->d_refcnt--;
458a1511e6bSjoyce mcintosh 		if (od->d_refcnt == 0)
459a1511e6bSjoyce mcintosh 			od->d_state = SMB_ODIR_STATE_OPEN;
4607f667e74Sjose borrego 		break;
4617f667e74Sjose borrego 	case SMB_ODIR_STATE_CLOSING:
4627f667e74Sjose borrego 		od->d_refcnt--;
4637f667e74Sjose borrego 		if (od->d_refcnt == 0) {
4647f667e74Sjose borrego 			od->d_state = SMB_ODIR_STATE_CLOSED;
4659fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 			smb_tree_post_odir(od->d_tree, od);
4667f667e74Sjose borrego 		}
4677f667e74Sjose borrego 		break;
4687f667e74Sjose borrego 	case SMB_ODIR_STATE_CLOSED:
4697f667e74Sjose borrego 	default:
4707f667e74Sjose borrego 		break;
4717f667e74Sjose borrego 	}
4727f667e74Sjose borrego 
4737f667e74Sjose borrego 	mutex_exit(&od->d_mutex);
474da6c28aaSamw }
475da6c28aaSamw 
476da6c28aaSamw /*
477da6c28aaSamw  * smb_odir_close
478da6c28aaSamw  */
479da6c28aaSamw void
4807f667e74Sjose borrego smb_odir_close(smb_odir_t *od)
481da6c28aaSamw {
482da6c28aaSamw 	ASSERT(od);
483a1511e6bSjoyce mcintosh 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
484da6c28aaSamw 
485da6c28aaSamw 	mutex_enter(&od->d_mutex);
486a1511e6bSjoyce mcintosh 	ASSERT(od->d_refcnt > 0);
487a1511e6bSjoyce mcintosh 	switch (od->d_state) {
488a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_OPEN:
489a1511e6bSjoyce mcintosh 		break;
490a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_IN_USE:
4917f667e74Sjose borrego 		od->d_state = SMB_ODIR_STATE_CLOSING;
492a1511e6bSjoyce mcintosh 		break;
493a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_CLOSING:
494a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_CLOSED:
495a1511e6bSjoyce mcintosh 	default:
496a1511e6bSjoyce mcintosh 		break;
497a1511e6bSjoyce mcintosh 	}
498da6c28aaSamw 	mutex_exit(&od->d_mutex);
4997f667e74Sjose borrego }
500da6c28aaSamw 
501da6c28aaSamw /*
5027f667e74Sjose borrego  * smb_odir_read
503da6c28aaSamw  *
5047f667e74Sjose borrego  * Find the next directory entry matching the search pattern.
5057f667e74Sjose borrego  * No search attribute matching is performed.
5067f667e74Sjose borrego  *
5077f667e74Sjose borrego  * Returns:
5087f667e74Sjose borrego  *  0 - success.
5097f667e74Sjose borrego  *      - If a matching entry was found eof will be B_FALSE and
5107f667e74Sjose borrego  *        odirent will be populated.
511*a90cf9f2SGordon Ross  * ENOENT
512*a90cf9f2SGordon Ross  *      - If we've scanned to the end, eof will be B_TRUE.
513*a90cf9f2SGordon Ross  * errno - other errors
514da6c28aaSamw  */
5157f667e74Sjose borrego int
5167f667e74Sjose borrego smb_odir_read(smb_request_t *sr, smb_odir_t *od,
5177f667e74Sjose borrego     smb_odirent_t *odirent, boolean_t *eof)
518da6c28aaSamw {
5197f667e74Sjose borrego 	int		rc;
520da6c28aaSamw 
5217f667e74Sjose borrego 	ASSERT(sr);
5227f667e74Sjose borrego 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
523da6c28aaSamw 	ASSERT(od);
524da6c28aaSamw 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
5257f667e74Sjose borrego 	ASSERT(odirent);
526da6c28aaSamw 
527da6c28aaSamw 	mutex_enter(&od->d_mutex);
528a1511e6bSjoyce mcintosh 	ASSERT(od->d_refcnt > 0);
5297f667e74Sjose borrego 
530a1511e6bSjoyce mcintosh 	switch (od->d_state) {
531a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_IN_USE:
532a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_CLOSING:
533a1511e6bSjoyce mcintosh 		break;
534a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_OPEN:
535a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_CLOSED:
536a1511e6bSjoyce mcintosh 	default:
537da6c28aaSamw 		mutex_exit(&od->d_mutex);
538*a90cf9f2SGordon Ross 		return (EBADF);
5397f667e74Sjose borrego 	}
5407f667e74Sjose borrego 
5417f667e74Sjose borrego 	for (;;) {
5427f667e74Sjose borrego 		if ((rc = smb_odir_next_odirent(od, odirent)) != 0)
543da6c28aaSamw 			break;
544cb174861Sjoyce mcintosh 		if (smb_odir_match_name(od, odirent))
545da6c28aaSamw 			break;
546da6c28aaSamw 	}
5477f667e74Sjose borrego 
5487f667e74Sjose borrego 	mutex_exit(&od->d_mutex);
5497f667e74Sjose borrego 
5507f667e74Sjose borrego 	switch (rc) {
5517f667e74Sjose borrego 	case 0:
5527f667e74Sjose borrego 		*eof = B_FALSE;
5537f667e74Sjose borrego 		return (0);
5547f667e74Sjose borrego 	case ENOENT:
5557f667e74Sjose borrego 		*eof = B_TRUE;
556*a90cf9f2SGordon Ross 		/* FALLTHROUGH */
5577f667e74Sjose borrego 	default:
558*a90cf9f2SGordon Ross 		return (rc);
5597f667e74Sjose borrego 	}
5607f667e74Sjose borrego }
5617f667e74Sjose borrego 
5627f667e74Sjose borrego /*
5637f667e74Sjose borrego  * smb_odir_read_fileinfo
5647f667e74Sjose borrego  *
5657f667e74Sjose borrego  * Find the next directory entry matching the search pattern
5667f667e74Sjose borrego  * and attributes: od->d_pattern and od->d_sattr.
5677f667e74Sjose borrego  *
5687f667e74Sjose borrego  * If the search pattern specifies a single filename call
5697f667e74Sjose borrego  * smb_odir_single_fileinfo to get the file attributes and
5707f667e74Sjose borrego  * populate the caller's smb_fileinfo_t.
5717f667e74Sjose borrego  *
5727f667e74Sjose borrego  * If the search pattern contains wildcards call smb_odir_next_odirent
5737f667e74Sjose borrego  * to get the next directory entry then. Repeat until a matching
5747f667e74Sjose borrego  * filename is found. Call smb_odir_wildcard_fileinfo to get the
5757f667e74Sjose borrego  * file attributes and populate the caller's smb_fileinfo_t.
5767f667e74Sjose borrego  * This is repeated until a file matching the search criteria is found.
5777f667e74Sjose borrego  *
5787f667e74Sjose borrego  * Returns:
5797f667e74Sjose borrego  *  0 - success.
5807f667e74Sjose borrego  *      - If a matching entry was found eof will be B_FALSE and
5817f667e74Sjose borrego  *        fileinfo will be populated.
582*a90cf9f2SGordon Ross  * ENOENT
583*a90cf9f2SGordon Ross  *      - If at end of dir, eof will be B_TRUE.
584*a90cf9f2SGordon Ross  * errno - other error
5857f667e74Sjose borrego  */
5867f667e74Sjose borrego int
5877f667e74Sjose borrego smb_odir_read_fileinfo(smb_request_t *sr, smb_odir_t *od,
588bfbce3c1SGordon Ross     smb_fileinfo_t *fileinfo, uint16_t *eof)
5897f667e74Sjose borrego {
590bbf6f00cSJordan Brown 	int		rc, errnum;
5917f667e74Sjose borrego 	smb_odirent_t	*odirent;
5927f667e74Sjose borrego 
5937f667e74Sjose borrego 	ASSERT(sr);
5947f667e74Sjose borrego 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
5957f667e74Sjose borrego 	ASSERT(od);
5967f667e74Sjose borrego 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
5977f667e74Sjose borrego 	ASSERT(fileinfo);
5987f667e74Sjose borrego 
5997f667e74Sjose borrego 	mutex_enter(&od->d_mutex);
600a1511e6bSjoyce mcintosh 	ASSERT(od->d_refcnt > 0);
6017f667e74Sjose borrego 
602a1511e6bSjoyce mcintosh 	switch (od->d_state) {
603a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_IN_USE:
604a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_CLOSING:
605a1511e6bSjoyce mcintosh 		break;
606a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_OPEN:
607a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_CLOSED:
608a1511e6bSjoyce mcintosh 	default:
6097f667e74Sjose borrego 		mutex_exit(&od->d_mutex);
610*a90cf9f2SGordon Ross 		return (EBADF);
6117f667e74Sjose borrego 	}
6127f667e74Sjose borrego 
613bfbce3c1SGordon Ross 	if ((od->d_flags & SMB_ODIR_FLAG_WILDCARDS) == 0) {
6147f667e74Sjose borrego 		if (od->d_eof)
6157f667e74Sjose borrego 			rc = ENOENT;
6167f667e74Sjose borrego 		else
6177f667e74Sjose borrego 			rc = smb_odir_single_fileinfo(sr, od, fileinfo);
6187f667e74Sjose borrego 		od->d_eof = B_TRUE;
6197f667e74Sjose borrego 	} else {
6207f667e74Sjose borrego 		odirent = kmem_alloc(sizeof (smb_odirent_t), KM_SLEEP);
6217f667e74Sjose borrego 		for (;;) {
6227f667e74Sjose borrego 			bzero(fileinfo, sizeof (smb_fileinfo_t));
6237f667e74Sjose borrego 			if ((rc = smb_odir_next_odirent(od, odirent)) != 0)
6247f667e74Sjose borrego 				break;
6257f667e74Sjose borrego 
626bbf6f00cSJordan Brown 			/* skip non utf8 filename */
627bbf6f00cSJordan Brown 			if (u8_validate(odirent->od_name,
628bbf6f00cSJordan Brown 			    strlen(odirent->od_name), NULL,
629bbf6f00cSJordan Brown 			    U8_VALIDATE_ENTIRE, &errnum) < 0)
630bbf6f00cSJordan Brown 				continue;
631bbf6f00cSJordan Brown 
632cb174861Sjoyce mcintosh 			if (!smb_odir_match_name(od, odirent))
6337f667e74Sjose borrego 				continue;
6347f667e74Sjose borrego 
6357f667e74Sjose borrego 			rc = smb_odir_wildcard_fileinfo(sr, od, odirent,
6367f667e74Sjose borrego 			    fileinfo);
6377f667e74Sjose borrego 			if (rc == 0)
6387f667e74Sjose borrego 				break;
6397f667e74Sjose borrego 		}
6407f667e74Sjose borrego 		kmem_free(odirent, sizeof (smb_odirent_t));
6417f667e74Sjose borrego 	}
6427f667e74Sjose borrego 	mutex_exit(&od->d_mutex);
6437f667e74Sjose borrego 
6447f667e74Sjose borrego 	switch (rc) {
6457f667e74Sjose borrego 	case 0:
646bfbce3c1SGordon Ross 		*eof = 0;
6477f667e74Sjose borrego 		return (0);
6487f667e74Sjose borrego 	case ENOENT:
649bfbce3c1SGordon Ross 		*eof = 1;	/* per. FindFirst, FindNext spec. */
650*a90cf9f2SGordon Ross 		/* FALLTHROUGH */
6517f667e74Sjose borrego 	default:
652*a90cf9f2SGordon Ross 		return (rc);
6537f667e74Sjose borrego 	}
6547f667e74Sjose borrego }
6557f667e74Sjose borrego 
6567f667e74Sjose borrego /*
6577f667e74Sjose borrego  * smb_odir_read_streaminfo
6587f667e74Sjose borrego  *
6597f667e74Sjose borrego  * Find the next directory entry whose name begins with SMB_STREAM_PREFIX,
6607f667e74Sjose borrego  * and thus represents an NTFS named stream.
6617f667e74Sjose borrego  * No search attribute matching is performed.
6628b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States  * No case conflict name mangling is required for NTFS named stream names.
6637f667e74Sjose borrego  *
6647f667e74Sjose borrego  * Returns:
6657f667e74Sjose borrego  *  0 - success.
6667f667e74Sjose borrego  *      - If a matching entry was found eof will be B_FALSE and
6677f667e74Sjose borrego  *        sinfo will be populated.
6687f667e74Sjose borrego  *      - If there are no matching entries eof will be B_TRUE.
669*a90cf9f2SGordon Ross  * errno - error
6707f667e74Sjose borrego  */
6717f667e74Sjose borrego int
6727f667e74Sjose borrego smb_odir_read_streaminfo(smb_request_t *sr, smb_odir_t *od,
6737f667e74Sjose borrego     smb_streaminfo_t *sinfo, boolean_t *eof)
6747f667e74Sjose borrego {
6757f667e74Sjose borrego 	int		rc;
676*a90cf9f2SGordon Ross 	cred_t		*kcr;
6777f667e74Sjose borrego 	smb_odirent_t	*odirent;
678e3f2c991SKeyur Desai 	smb_node_t	*fnode;
6797f667e74Sjose borrego 	smb_attr_t	attr;
6807f667e74Sjose borrego 
6817f667e74Sjose borrego 	ASSERT(sr);
6827f667e74Sjose borrego 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
6837f667e74Sjose borrego 	ASSERT(od);
6847f667e74Sjose borrego 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
6857f667e74Sjose borrego 	ASSERT(sinfo);
6867f667e74Sjose borrego 
687*a90cf9f2SGordon Ross 	kcr = zone_kcred();
688*a90cf9f2SGordon Ross 
6897f667e74Sjose borrego 	mutex_enter(&od->d_mutex);
690a1511e6bSjoyce mcintosh 	ASSERT(od->d_refcnt > 0);
6917f667e74Sjose borrego 
692a1511e6bSjoyce mcintosh 	switch (od->d_state) {
693a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_IN_USE:
694a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_CLOSING:
695a1511e6bSjoyce mcintosh 		break;
696a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_OPEN:
697a1511e6bSjoyce mcintosh 	case SMB_ODIR_STATE_CLOSED:
698a1511e6bSjoyce mcintosh 	default:
6997f667e74Sjose borrego 		mutex_exit(&od->d_mutex);
700*a90cf9f2SGordon Ross 		return (EBADF);
7017f667e74Sjose borrego 	}
7027f667e74Sjose borrego 
7037f667e74Sjose borrego 	/* Check that odir represents an xattr directory */
7048b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (!(od->d_flags & SMB_ODIR_FLAG_XATTR)) {
7057f667e74Sjose borrego 		*eof = B_TRUE;
7067f667e74Sjose borrego 		mutex_exit(&od->d_mutex);
7077f667e74Sjose borrego 		return (0);
7087f667e74Sjose borrego 	}
7097f667e74Sjose borrego 
7107f667e74Sjose borrego 	odirent = kmem_alloc(sizeof (smb_odirent_t), KM_SLEEP);
7115fd03bc0SGordon Ross 	bzero(&attr, sizeof (attr));
7127f667e74Sjose borrego 
7137f667e74Sjose borrego 	for (;;) {
7147f667e74Sjose borrego 		bzero(sinfo, sizeof (smb_streaminfo_t));
7157f667e74Sjose borrego 		if ((rc = smb_odir_next_odirent(od, odirent)) != 0)
7167f667e74Sjose borrego 			break;
7177f667e74Sjose borrego 
7187f667e74Sjose borrego 		if (strncmp(odirent->od_name, SMB_STREAM_PREFIX,
7197f667e74Sjose borrego 		    SMB_STREAM_PREFIX_LEN)) {
7207f667e74Sjose borrego 			continue;
7217f667e74Sjose borrego 		}
7227f667e74Sjose borrego 
723e3f2c991SKeyur Desai 		rc = smb_fsop_lookup(sr, od->d_cred, 0, od->d_tree->t_snode,
724e3f2c991SKeyur Desai 		    od->d_dnode, odirent->od_name, &fnode);
7257f667e74Sjose borrego 		if (rc == 0) {
726*a90cf9f2SGordon Ross 			/*
727*a90cf9f2SGordon Ross 			 * We just need the file sizes, and don't want
728*a90cf9f2SGordon Ross 			 * EACCES failures here, so use kcred and pass
729*a90cf9f2SGordon Ross 			 * NULL as the sr to skip sr->fid-ofile checks.
730*a90cf9f2SGordon Ross 			 */
7315fd03bc0SGordon Ross 			attr.sa_mask = SMB_AT_SIZE | SMB_AT_ALLOCSZ;
732*a90cf9f2SGordon Ross 			rc = smb_node_getattr(NULL, fnode, kcr, NULL, &attr);
733e3f2c991SKeyur Desai 			smb_node_release(fnode);
7347f667e74Sjose borrego 		}
7357f667e74Sjose borrego 
7367f667e74Sjose borrego 		if (rc == 0) {
7377f667e74Sjose borrego 			(void) strlcpy(sinfo->si_name,
7387f667e74Sjose borrego 			    odirent->od_name + SMB_STREAM_PREFIX_LEN,
7397f667e74Sjose borrego 			    sizeof (sinfo->si_name));
7407f667e74Sjose borrego 			sinfo->si_size = attr.sa_vattr.va_size;
741e3f2c991SKeyur Desai 			sinfo->si_alloc_size = attr.sa_allocsz;
7427f667e74Sjose borrego 			break;
7437f667e74Sjose borrego 		}
7447f667e74Sjose borrego 	}
7457f667e74Sjose borrego 	mutex_exit(&od->d_mutex);
7467f667e74Sjose borrego 
7477f667e74Sjose borrego 	kmem_free(odirent, sizeof (smb_odirent_t));
7487f667e74Sjose borrego 
7497f667e74Sjose borrego 	switch (rc) {
7507f667e74Sjose borrego 	case 0:
7517f667e74Sjose borrego 		*eof = B_FALSE;
7527f667e74Sjose borrego 		return (0);
7537f667e74Sjose borrego 	case ENOENT:
7547f667e74Sjose borrego 		*eof = B_TRUE;
7557f667e74Sjose borrego 		return (0);
7567f667e74Sjose borrego 	default:
757*a90cf9f2SGordon Ross 		return (rc);
7587f667e74Sjose borrego 	}
7597f667e74Sjose borrego }
7607f667e74Sjose borrego 
7617f667e74Sjose borrego /*
7627f667e74Sjose borrego  * smb_odir_save_cookie
7637f667e74Sjose borrego  *
7647f667e74Sjose borrego  * Callers can save up to SMB_MAX_SEARCH cookies in the odir
7657f667e74Sjose borrego  * to be used as resume points for a 'find next' request.
7667f667e74Sjose borrego  */
7677f667e74Sjose borrego void
7687f667e74Sjose borrego smb_odir_save_cookie(smb_odir_t *od, int idx, uint32_t cookie)
7697f667e74Sjose borrego {
7707f667e74Sjose borrego 	ASSERT(od);
7717f667e74Sjose borrego 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
7727f667e74Sjose borrego 	ASSERT(idx >= 0 && idx < SMB_MAX_SEARCH);
7737f667e74Sjose borrego 
7747f667e74Sjose borrego 	mutex_enter(&od->d_mutex);
7757f667e74Sjose borrego 	od->d_cookies[idx] = cookie;
7767f667e74Sjose borrego 	mutex_exit(&od->d_mutex);
7777f667e74Sjose borrego }
7787f667e74Sjose borrego 
7797f667e74Sjose borrego /*
780bfbce3c1SGordon Ross  * smb_odir_save_fname
781bfbce3c1SGordon Ross  *
782bfbce3c1SGordon Ross  * Save a filename / offset pair, which are basically a
783bfbce3c1SGordon Ross  * one entry cache.  See smb_com_trans2_find_next2.
784bfbce3c1SGordon Ross  */
785bfbce3c1SGordon Ross void
786bfbce3c1SGordon Ross smb_odir_save_fname(smb_odir_t *od, uint32_t cookie, const char *fname)
787bfbce3c1SGordon Ross {
788bfbce3c1SGordon Ross 	ASSERT(od);
789bfbce3c1SGordon Ross 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
790bfbce3c1SGordon Ross 
791bfbce3c1SGordon Ross 	mutex_enter(&od->d_mutex);
792bfbce3c1SGordon Ross 
793bfbce3c1SGordon Ross 	od->d_last_cookie = cookie;
794bfbce3c1SGordon Ross 	bzero(od->d_last_name, MAXNAMELEN);
795bfbce3c1SGordon Ross 	if (fname != NULL)
796bfbce3c1SGordon Ross 		(void) strlcpy(od->d_last_name, fname, MAXNAMELEN);
797bfbce3c1SGordon Ross 
798bfbce3c1SGordon Ross 	mutex_exit(&od->d_mutex);
799bfbce3c1SGordon Ross }
800bfbce3c1SGordon Ross 
801bfbce3c1SGordon Ross /*
8027f667e74Sjose borrego  * smb_odir_resume_at
8037f667e74Sjose borrego  *
804*a90cf9f2SGordon Ross  * If SMB_ODIR_FLAG_WILDCARDS is not set, and we're rewinding,
805*a90cf9f2SGordon Ross  * assume we're no longer at EOF.
806eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States  *
807eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States  * Wildcard searching can be resumed from:
8087f667e74Sjose borrego  * - the cookie saved at a specified index (SMBsearch, SMBfind).
8097f667e74Sjose borrego  * - a specified cookie (SMB_trans2_find)
8107f667e74Sjose borrego  * - a specified filename (SMB_trans2_find) - NOT SUPPORTED.
8117f667e74Sjose borrego  *   Defaults to continuing from where the last search ended.
8127f667e74Sjose borrego  *
8137f667e74Sjose borrego  * Continuation from where the last search ended (SMB_trans2_find)
8147f667e74Sjose borrego  * is implemented by saving the last cookie at a specific index (0)
8157f667e74Sjose borrego  * smb_odir_resume_at indicates a new request, so reset od->d_bufptr
8167f667e74Sjose borrego  * and d_eof to force a vop_readdir.
8177f667e74Sjose borrego  */
8187f667e74Sjose borrego void
8197f667e74Sjose borrego smb_odir_resume_at(smb_odir_t *od, smb_odir_resume_t *resume)
8207f667e74Sjose borrego {
821*a90cf9f2SGordon Ross 	uint64_t save_offset;
822*a90cf9f2SGordon Ross 
8237f667e74Sjose borrego 	ASSERT(od);
8247f667e74Sjose borrego 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
8257f667e74Sjose borrego 	ASSERT(resume);
8267f667e74Sjose borrego 
827eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 	if ((od->d_flags & SMB_ODIR_FLAG_WILDCARDS) == 0) {
828*a90cf9f2SGordon Ross 		if (resume->or_type == SMB_ODIR_RESUME_COOKIE)
829*a90cf9f2SGordon Ross 			od->d_eof = B_FALSE;
830eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 		return;
831eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 	}
832*a90cf9f2SGordon Ross 	mutex_enter(&od->d_mutex);
833eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 
834*a90cf9f2SGordon Ross 	save_offset = od->d_offset;
8357f667e74Sjose borrego 	switch (resume->or_type) {
836bfbce3c1SGordon Ross 
837bfbce3c1SGordon Ross 	default:
838bfbce3c1SGordon Ross 	case SMB_ODIR_RESUME_CONT:
839bfbce3c1SGordon Ross 		/* Continue where we left off. */
840bfbce3c1SGordon Ross 		break;
841bfbce3c1SGordon Ross 
8427f667e74Sjose borrego 	case SMB_ODIR_RESUME_IDX:
843bfbce3c1SGordon Ross 		/*
844bfbce3c1SGordon Ross 		 * This is used only by the (ancient) SMB_SEARCH.
845bfbce3c1SGordon Ross 		 * Modern clients use trans2 FindFirst, FindNext.
846bfbce3c1SGordon Ross 		 */
8477f667e74Sjose borrego 		ASSERT(resume->or_idx >= 0);
8487f667e74Sjose borrego 		ASSERT(resume->or_idx < SMB_MAX_SEARCH);
8497f667e74Sjose borrego 
8507f667e74Sjose borrego 		if ((resume->or_idx < 0) ||
8517f667e74Sjose borrego 		    (resume->or_idx >= SMB_MAX_SEARCH)) {
8527f667e74Sjose borrego 			resume->or_idx = 0;
8537f667e74Sjose borrego 		}
8547f667e74Sjose borrego 		od->d_offset = od->d_cookies[resume->or_idx];
8557f667e74Sjose borrego 		break;
856bfbce3c1SGordon Ross 
8577f667e74Sjose borrego 	case SMB_ODIR_RESUME_COOKIE:
8587f667e74Sjose borrego 		od->d_offset = resume->or_cookie;
8597f667e74Sjose borrego 		break;
860bfbce3c1SGordon Ross 
8617f667e74Sjose borrego 	case SMB_ODIR_RESUME_FNAME:
862bfbce3c1SGordon Ross 		/*
863bfbce3c1SGordon Ross 		 * If the name matches the last one saved,
864bfbce3c1SGordon Ross 		 * use the offset that was saved with it in
865bfbce3c1SGordon Ross 		 * the odir.  Otherwise use the cookie value
866bfbce3c1SGordon Ross 		 * in the resume data from the client.
867bfbce3c1SGordon Ross 		 */
868bfbce3c1SGordon Ross 		if (strcmp(resume->or_fname, od->d_last_name) &&
869bfbce3c1SGordon Ross 		    od->d_last_cookie != 0) {
870bfbce3c1SGordon Ross 			od->d_offset = od->d_last_cookie;
871bfbce3c1SGordon Ross 		} else if (resume->or_cookie != 0) {
872bfbce3c1SGordon Ross 			od->d_offset = resume->or_cookie;
873bfbce3c1SGordon Ross 		} /* else continue where we left off */
8747f667e74Sjose borrego 		break;
8757f667e74Sjose borrego 	}
8767f667e74Sjose borrego 
877*a90cf9f2SGordon Ross 	if (od->d_offset != save_offset) {
8787f667e74Sjose borrego 		/* Force a vop_readdir to refresh d_buf */
8797f667e74Sjose borrego 		od->d_bufptr = NULL;
8807f667e74Sjose borrego 		od->d_eof = B_FALSE;
881*a90cf9f2SGordon Ross 	}
8827f667e74Sjose borrego 
8837f667e74Sjose borrego 	mutex_exit(&od->d_mutex);
8847f667e74Sjose borrego }
8857f667e74Sjose borrego 
8867f667e74Sjose borrego 
8877f667e74Sjose borrego /* *** static functions *** */
8887f667e74Sjose borrego 
8897f667e74Sjose borrego /*
8907f667e74Sjose borrego  * smb_odir_create
8917f667e74Sjose borrego  * Allocate and populate an odir obect and add it to the tree's list.
8927f667e74Sjose borrego  */
893*a90cf9f2SGordon Ross static smb_odir_t *
8947f667e74Sjose borrego smb_odir_create(smb_request_t *sr, smb_node_t *dnode,
895*a90cf9f2SGordon Ross 	const char *pattern, uint16_t sattr, uint16_t odid, cred_t *cr)
8967f667e74Sjose borrego {
8977f667e74Sjose borrego 	smb_odir_t	*od;
8987f667e74Sjose borrego 	smb_tree_t	*tree;
8997f667e74Sjose borrego 
9007f667e74Sjose borrego 	ASSERT(sr);
9017f667e74Sjose borrego 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
9027f667e74Sjose borrego 	ASSERT(sr->tid_tree);
9037f667e74Sjose borrego 	ASSERT(sr->tid_tree->t_magic == SMB_TREE_MAGIC);
9047f667e74Sjose borrego 	ASSERT(dnode);
9057f667e74Sjose borrego 	ASSERT(dnode->n_magic == SMB_NODE_MAGIC);
9067f667e74Sjose borrego 
9077f667e74Sjose borrego 	tree = sr->tid_tree;
9087f667e74Sjose borrego 
9098622ec45SGordon Ross 	od = kmem_cache_alloc(smb_cache_odir, KM_SLEEP);
9107f667e74Sjose borrego 	bzero(od, sizeof (smb_odir_t));
9117f667e74Sjose borrego 
9127f667e74Sjose borrego 	mutex_init(&od->d_mutex, NULL, MUTEX_DEFAULT, NULL);
913*a90cf9f2SGordon Ross 
914*a90cf9f2SGordon Ross 	/*
915*a90cf9f2SGordon Ross 	 * Return this to the caller as if they had done
916*a90cf9f2SGordon Ross 	 * smb_tree_lookup_odir() to obtain the odir.
917*a90cf9f2SGordon Ross 	 */
918*a90cf9f2SGordon Ross 	od->d_refcnt = 1;
919*a90cf9f2SGordon Ross 	od->d_state = SMB_ODIR_STATE_IN_USE;
9207f667e74Sjose borrego 	od->d_magic = SMB_ODIR_MAGIC;
9217f667e74Sjose borrego 	od->d_opened_by_pid = sr->smb_pid;
9227f667e74Sjose borrego 	od->d_session = tree->t_session;
923eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 	od->d_cred = cr;
9243b13a1efSThomas Keiser 	/*
9253b13a1efSThomas Keiser 	 * grab a ref for od->d_user
9263b13a1efSThomas Keiser 	 * released in  smb_odir_delete()
9273b13a1efSThomas Keiser 	 */
9283b13a1efSThomas Keiser 	smb_user_hold_internal(sr->uid_user);
9293b13a1efSThomas Keiser 	od->d_user = sr->uid_user;
9307f667e74Sjose borrego 	od->d_tree = tree;
9317f667e74Sjose borrego 	od->d_dnode = dnode;
9327f667e74Sjose borrego 	smb_node_ref(dnode);
9337f667e74Sjose borrego 	od->d_odid = odid;
9347f667e74Sjose borrego 	od->d_sattr = sattr;
9357f667e74Sjose borrego 	(void) strlcpy(od->d_pattern, pattern, sizeof (od->d_pattern));
9368b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	od->d_flags = 0;
937fe1c642dSBill Krier 	if (smb_contains_wildcards(od->d_pattern))
9388b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		od->d_flags |= SMB_ODIR_FLAG_WILDCARDS;
9398b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (vfs_has_feature(dnode->vp->v_vfsp, VFSFT_DIRENTFLAGS))
9408b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		od->d_flags |= SMB_ODIR_FLAG_EDIRENT;
9418b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (smb_tree_has_feature(tree, SMB_TREE_CASEINSENSITIVE))
9428b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		od->d_flags |= SMB_ODIR_FLAG_IGNORE_CASE;
943cb174861Sjoyce mcintosh 	if (smb_tree_has_feature(tree, SMB_TREE_SHORTNAMES))
944cb174861Sjoyce mcintosh 		od->d_flags |= SMB_ODIR_FLAG_SHORTNAMES;
9458b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (SMB_TREE_SUPPORTS_CATIA(sr))
9468b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		od->d_flags |= SMB_ODIR_FLAG_CATIA;
947e3f2c991SKeyur Desai 	if (SMB_TREE_SUPPORTS_ABE(sr))
948e3f2c991SKeyur Desai 		od->d_flags |= SMB_ODIR_FLAG_ABE;
949cb174861Sjoyce mcintosh 	if (dnode->flags & NODE_XATTR_DIR)
950cb174861Sjoyce mcintosh 		od->d_flags |= SMB_ODIR_FLAG_XATTR;
9517f667e74Sjose borrego 	od->d_eof = B_FALSE;
9527f667e74Sjose borrego 
9537f667e74Sjose borrego 	smb_llist_enter(&tree->t_odir_list, RW_WRITER);
9547f667e74Sjose borrego 	smb_llist_insert_tail(&tree->t_odir_list, od);
9557f667e74Sjose borrego 	smb_llist_exit(&tree->t_odir_list);
9567f667e74Sjose borrego 
9577f667e74Sjose borrego 	atomic_inc_32(&tree->t_session->s_dir_cnt);
958*a90cf9f2SGordon Ross 	return (od);
959*a90cf9f2SGordon Ross }
960*a90cf9f2SGordon Ross 
961*a90cf9f2SGordon Ross /*
962*a90cf9f2SGordon Ross  * Set a new pattern, attributes, and rewind.
963*a90cf9f2SGordon Ross  */
964*a90cf9f2SGordon Ross void
965*a90cf9f2SGordon Ross smb_odir_reopen(smb_odir_t *od, const char *pattern, uint16_t sattr)
966*a90cf9f2SGordon Ross {
967*a90cf9f2SGordon Ross 
968*a90cf9f2SGordon Ross 	SMB_ODIR_VALID(od);
969*a90cf9f2SGordon Ross 
970*a90cf9f2SGordon Ross 	mutex_enter(&od->d_mutex);
971*a90cf9f2SGordon Ross 	od->d_sattr = sattr;
972*a90cf9f2SGordon Ross 	(void) strlcpy(od->d_pattern, pattern, sizeof (od->d_pattern));
973*a90cf9f2SGordon Ross 	if (smb_contains_wildcards(od->d_pattern))
974*a90cf9f2SGordon Ross 		od->d_flags |= SMB_ODIR_FLAG_WILDCARDS;
975*a90cf9f2SGordon Ross 	else
976*a90cf9f2SGordon Ross 		od->d_flags &= ~SMB_ODIR_FLAG_WILDCARDS;
977*a90cf9f2SGordon Ross 
978*a90cf9f2SGordon Ross 	/* Internal smb_odir_resume_at */
979*a90cf9f2SGordon Ross 	od->d_offset = 0;
980*a90cf9f2SGordon Ross 	od->d_bufptr = NULL;
981*a90cf9f2SGordon Ross 	od->d_eof = B_FALSE;
982*a90cf9f2SGordon Ross 
983*a90cf9f2SGordon Ross 	mutex_exit(&od->d_mutex);
984da6c28aaSamw }
985da6c28aaSamw 
986da6c28aaSamw /*
9879fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * Delete an odir.
9887f667e74Sjose borrego  *
9899fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * Remove the odir from the tree list before freeing resources
9909fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * associated with the odir.
991da6c28aaSamw  */
9929fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States void
9939fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States smb_odir_delete(void *arg)
994da6c28aaSamw {
9959fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	smb_tree_t	*tree;
9969fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	smb_odir_t	*od = (smb_odir_t *)arg;
9979fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
9989fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	SMB_ODIR_VALID(od);
9999fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	ASSERT(od->d_refcnt == 0);
1000da6c28aaSamw 	ASSERT(od->d_state == SMB_ODIR_STATE_CLOSED);
1001da6c28aaSamw 
10029fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	tree = od->d_tree;
10039fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	smb_llist_enter(&tree->t_odir_list, RW_WRITER);
10049fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	smb_llist_remove(&tree->t_odir_list, od);
1005*a90cf9f2SGordon Ross 	if (od->d_odid != 0)
10069fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		smb_idpool_free(&tree->t_odid_pool, od->d_odid);
10079fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	atomic_dec_32(&tree->t_session->s_dir_cnt);
10089fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	smb_llist_exit(&tree->t_odir_list);
10099fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
10109fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	mutex_enter(&od->d_mutex);
10119fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	mutex_exit(&od->d_mutex);
1012da6c28aaSamw 
10137f667e74Sjose borrego 	od->d_magic = 0;
10147f667e74Sjose borrego 	smb_node_release(od->d_dnode);
10153b13a1efSThomas Keiser 	smb_user_release(od->d_user);
1016da6c28aaSamw 	mutex_destroy(&od->d_mutex);
10178622ec45SGordon Ross 	kmem_cache_free(smb_cache_odir, od);
1018da6c28aaSamw }
10197f667e74Sjose borrego 
10207f667e74Sjose borrego /*
10217f667e74Sjose borrego  * smb_odir_next_odirent
10227f667e74Sjose borrego  *
10237f667e74Sjose borrego  * Find the next directory entry in d_buf. If d_bufptr is NULL (buffer
10247f667e74Sjose borrego  * is empty or we've reached the end of it), read the next set of
10257f667e74Sjose borrego  * entries from the file system (vop_readdir).
10267f667e74Sjose borrego  *
10277f667e74Sjose borrego  * File systems which support VFSFT_EDIRENT_FLAGS will return the
10287f667e74Sjose borrego  * directory entries as a buffer of edirent_t structure. Others will
10297f667e74Sjose borrego  * return a buffer of dirent64_t structures.  For simplicity translate
10307f667e74Sjose borrego  * the data into an smb_odirent_t structure.
10317f667e74Sjose borrego  * The ed_name/d_name in d_buf is NULL terminated by the file system.
10327f667e74Sjose borrego  *
10337f667e74Sjose borrego  * Some file systems can have directories larger than SMB_MAXDIRSIZE.
10348b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States  * If the odirent offset >= SMB_MAXDIRSIZE return ENOENT and set d_eof
10358b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States  * to true to stop subsequent calls to smb_vop_readdir.
10367f667e74Sjose borrego  *
10377f667e74Sjose borrego  * Returns:
10387f667e74Sjose borrego  *      0 - success. odirent is populated with the next directory entry
10397f667e74Sjose borrego  * ENOENT - no more directory entries
10407f667e74Sjose borrego  *  errno - error
10417f667e74Sjose borrego  */
10427f667e74Sjose borrego static int
10437f667e74Sjose borrego smb_odir_next_odirent(smb_odir_t *od, smb_odirent_t *odirent)
10447f667e74Sjose borrego {
10457f667e74Sjose borrego 	int		rc;
10467f667e74Sjose borrego 	int		reclen;
10477f667e74Sjose borrego 	int		eof;
10487f667e74Sjose borrego 	dirent64_t	*dp;
10497f667e74Sjose borrego 	edirent_t	*edp;
10508b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	char		*np;
1051e3f2c991SKeyur Desai 	uint32_t	abe_flag = 0;
10527f667e74Sjose borrego 
10537f667e74Sjose borrego 	ASSERT(MUTEX_HELD(&od->d_mutex));
10547f667e74Sjose borrego 
105548557149Sjoyce mcintosh 	bzero(odirent, sizeof (smb_odirent_t));
105648557149Sjoyce mcintosh 
10577f667e74Sjose borrego 	if (od->d_bufptr != NULL) {
10588b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		if (od->d_flags & SMB_ODIR_FLAG_EDIRENT)
10598b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 			reclen = od->d_edp->ed_reclen;
10608b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		else
10618b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 			reclen = od->d_dp->d_reclen;
10627f667e74Sjose borrego 
10637f667e74Sjose borrego 		if (reclen == 0) {
10647f667e74Sjose borrego 			od->d_bufptr = NULL;
10657f667e74Sjose borrego 		} else {
10667f667e74Sjose borrego 			od->d_bufptr += reclen;
10677f667e74Sjose borrego 			if (od->d_bufptr >= od->d_buf + od->d_bufsize)
10687f667e74Sjose borrego 				od->d_bufptr = NULL;
10697f667e74Sjose borrego 		}
10707f667e74Sjose borrego 	}
10717f667e74Sjose borrego 
10727f667e74Sjose borrego 	if (od->d_bufptr == NULL) {
10737f667e74Sjose borrego 		if (od->d_eof)
10747f667e74Sjose borrego 			return (ENOENT);
10757f667e74Sjose borrego 
10767f667e74Sjose borrego 		od->d_bufsize = sizeof (od->d_buf);
10777f667e74Sjose borrego 
1078e3f2c991SKeyur Desai 		if (od->d_flags & SMB_ODIR_FLAG_ABE)
1079e3f2c991SKeyur Desai 			abe_flag = SMB_ABE;
1080e3f2c991SKeyur Desai 
10817f667e74Sjose borrego 		rc = smb_vop_readdir(od->d_dnode->vp, od->d_offset,
1082e3f2c991SKeyur Desai 		    od->d_buf, &od->d_bufsize, &eof, abe_flag, od->d_cred);
10837f667e74Sjose borrego 
10847f667e74Sjose borrego 		if ((rc == 0) && (od->d_bufsize == 0))
10857f667e74Sjose borrego 			rc = ENOENT;
10867f667e74Sjose borrego 
10877f667e74Sjose borrego 		if (rc != 0) {
10887f667e74Sjose borrego 			od->d_bufptr = NULL;
10897f667e74Sjose borrego 			od->d_bufsize = 0;
10907f667e74Sjose borrego 			return (rc);
10917f667e74Sjose borrego 		}
10927f667e74Sjose borrego 
10937f667e74Sjose borrego 		od->d_eof = (eof != 0);
10947f667e74Sjose borrego 		od->d_bufptr = od->d_buf;
10957f667e74Sjose borrego 	}
10967f667e74Sjose borrego 
10978b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (od->d_flags & SMB_ODIR_FLAG_EDIRENT)
10988b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		od->d_offset = od->d_edp->ed_off;
10998b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	else
11008b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		od->d_offset = od->d_dp->d_off;
11018b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 
11027f667e74Sjose borrego 	if (od->d_offset >= SMB_MAXDIRSIZE) {
11037f667e74Sjose borrego 		od->d_bufptr = NULL;
11047f667e74Sjose borrego 		od->d_bufsize = 0;
11058b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		od->d_eof = B_TRUE;
11067f667e74Sjose borrego 		return (ENOENT);
11077f667e74Sjose borrego 	}
11087f667e74Sjose borrego 
11098b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (od->d_flags & SMB_ODIR_FLAG_EDIRENT) {
11107f667e74Sjose borrego 		edp = od->d_edp;
11117f667e74Sjose borrego 		odirent->od_ino = edp->ed_ino;
11127f667e74Sjose borrego 		odirent->od_eflags = edp->ed_eflags;
11138b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		np = edp->ed_name;
11147f667e74Sjose borrego 	} else {
11157f667e74Sjose borrego 		dp = od->d_dp;
11167f667e74Sjose borrego 		odirent->od_ino = dp->d_ino;
11177f667e74Sjose borrego 		odirent->od_eflags = 0;
11188b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		np =  dp->d_name;
11198b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	}
11208b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 
11218b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	if ((od->d_flags & SMB_ODIR_FLAG_CATIA) &&
11228b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	    ((od->d_flags & SMB_ODIR_FLAG_XATTR) == 0)) {
11238b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		smb_vop_catia_v4tov5(np, odirent->od_name,
11248b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		    sizeof (odirent->od_name));
11258b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	} else {
11268b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		(void) strlcpy(odirent->od_name, np,
11277f667e74Sjose borrego 		    sizeof (odirent->od_name));
11287f667e74Sjose borrego 	}
11297f667e74Sjose borrego 
11307f667e74Sjose borrego 	return (0);
11317f667e74Sjose borrego }
11327f667e74Sjose borrego 
11337f667e74Sjose borrego /*
11347f667e74Sjose borrego  * smb_odir_single_fileinfo
11357f667e74Sjose borrego  *
11367f667e74Sjose borrego  * Lookup the file identified by od->d_pattern.
11377f667e74Sjose borrego  *
11387f667e74Sjose borrego  * If the looked up file is a link, we attempt to lookup the link target
11397f667e74Sjose borrego  * to use its attributes in place of those of the files's.
11407f667e74Sjose borrego  * If we fail to lookup the target of the link we use the original
11417f667e74Sjose borrego  * file's attributes.
11427f667e74Sjose borrego  * Check if the attributes match the search attributes.
11437f667e74Sjose borrego  *
11447f667e74Sjose borrego  * Returns: 0 - success
11457f667e74Sjose borrego  *     ENOENT - no match
11467f667e74Sjose borrego  *      errno - error
11477f667e74Sjose borrego  */
11487f667e74Sjose borrego static int
11497f667e74Sjose borrego smb_odir_single_fileinfo(smb_request_t *sr, smb_odir_t *od,
11507f667e74Sjose borrego     smb_fileinfo_t *fileinfo)
11517f667e74Sjose borrego {
11527f667e74Sjose borrego 	int		rc;
11537f667e74Sjose borrego 	smb_node_t	*fnode, *tgt_node;
1154037cac00Sjoyce mcintosh 	smb_attr_t	attr;
1155cb174861Sjoyce mcintosh 	ino64_t		fid;
11567f667e74Sjose borrego 	char		*name;
11578b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	boolean_t	case_conflict = B_FALSE;
11588b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	int		lookup_flags, flags = 0;
11598b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	vnode_t		*vp;
11607f667e74Sjose borrego 
11617f667e74Sjose borrego 	ASSERT(sr);
11627f667e74Sjose borrego 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
11637f667e74Sjose borrego 	ASSERT(od);
11647f667e74Sjose borrego 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
11657f667e74Sjose borrego 
11667f667e74Sjose borrego 	ASSERT(MUTEX_HELD(&od->d_mutex));
11677f667e74Sjose borrego 	bzero(fileinfo, sizeof (smb_fileinfo_t));
11687f667e74Sjose borrego 
1169eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 	rc = smb_fsop_lookup(sr, od->d_cred, 0, od->d_tree->t_snode,
1170037cac00Sjoyce mcintosh 	    od->d_dnode, od->d_pattern, &fnode);
11717f667e74Sjose borrego 	if (rc != 0)
11727f667e74Sjose borrego 		return (rc);
11737f667e74Sjose borrego 
11748b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	/*
11758b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	 * If case sensitive, do a case insensitive smb_vop_lookup to
11768b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	 * check for case conflict
11778b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	 */
11788b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (od->d_flags & SMB_ODIR_FLAG_IGNORE_CASE) {
11798b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		lookup_flags = SMB_IGNORE_CASE;
11808b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		if (od->d_flags & SMB_ODIR_FLAG_CATIA)
11818b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 			lookup_flags |= SMB_CATIA;
11827f667e74Sjose borrego 
11838b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		rc = smb_vop_lookup(od->d_dnode->vp, fnode->od_name, &vp,
11848b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		    NULL, lookup_flags, &flags, od->d_tree->t_snode->vp,
11859fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		    NULL, od->d_cred);
11868b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		if (rc != 0)
11878b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 			return (rc);
11888b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		VN_RELE(vp);
11898b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 
11908b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		if (flags & ED_CASE_CONFLICT)
11918b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 			case_conflict = B_TRUE;
11928b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	}
11938b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 
11945fd03bc0SGordon Ross 	bzero(&attr, sizeof (attr));
11955fd03bc0SGordon Ross 	attr.sa_mask = SMB_AT_ALL;
1196*a90cf9f2SGordon Ross 	rc = smb_node_getattr(NULL, fnode, zone_kcred(), NULL, &attr);
11975fd03bc0SGordon Ross 	if (rc != 0) {
1198037cac00Sjoyce mcintosh 		smb_node_release(fnode);
1199037cac00Sjoyce mcintosh 		return (rc);
1200037cac00Sjoyce mcintosh 	}
1201037cac00Sjoyce mcintosh 
12027f667e74Sjose borrego 
12037f667e74Sjose borrego 	/* follow link to get target node & attr */
12049fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	if (smb_node_is_symlink(fnode) &&
12059fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	    smb_odir_lookup_link(sr, od, fnode->od_name, &tgt_node)) {
12067f667e74Sjose borrego 		smb_node_release(fnode);
12077f667e74Sjose borrego 		fnode = tgt_node;
12085fd03bc0SGordon Ross 		attr.sa_mask = SMB_AT_ALL;
1209*a90cf9f2SGordon Ross 		rc = smb_node_getattr(NULL, fnode, zone_kcred(), NULL, &attr);
12105fd03bc0SGordon Ross 		if (rc != 0) {
1211037cac00Sjoyce mcintosh 			smb_node_release(fnode);
1212037cac00Sjoyce mcintosh 			return (rc);
1213037cac00Sjoyce mcintosh 		}
12147f667e74Sjose borrego 	}
12157f667e74Sjose borrego 
12167f667e74Sjose borrego 	/* check search attributes */
1217037cac00Sjoyce mcintosh 	if (!smb_sattr_check(attr.sa_dosattr, od->d_sattr)) {
12187f667e74Sjose borrego 		smb_node_release(fnode);
12197f667e74Sjose borrego 		return (ENOENT);
12207f667e74Sjose borrego 	}
12217f667e74Sjose borrego 
1222148c5f43SAlan Wright 	name = fnode->od_name;
1223cb174861Sjoyce mcintosh 	if (od->d_flags & SMB_ODIR_FLAG_SHORTNAMES) {
1224cb174861Sjoyce mcintosh 		fid = attr.sa_vattr.va_nodeid;
1225cb174861Sjoyce mcintosh 		if (case_conflict || smb_needs_mangled(name)) {
1226cb174861Sjoyce mcintosh 			smb_mangle(name, fid, fileinfo->fi_shortname,
1227cb174861Sjoyce mcintosh 			    SMB_SHORTNAMELEN);
1228cb174861Sjoyce mcintosh 		}
1229148c5f43SAlan Wright 		if (case_conflict)
1230148c5f43SAlan Wright 			name = fileinfo->fi_shortname;
1231cb174861Sjoyce mcintosh 	}
1232148c5f43SAlan Wright 
1233148c5f43SAlan Wright 	(void) strlcpy(fileinfo->fi_name, name, sizeof (fileinfo->fi_name));
1234148c5f43SAlan Wright 
1235037cac00Sjoyce mcintosh 	fileinfo->fi_dosattr = attr.sa_dosattr;
1236037cac00Sjoyce mcintosh 	fileinfo->fi_nodeid = attr.sa_vattr.va_nodeid;
1237037cac00Sjoyce mcintosh 	fileinfo->fi_size = attr.sa_vattr.va_size;
1238e3f2c991SKeyur Desai 	fileinfo->fi_alloc_size = attr.sa_allocsz;
1239037cac00Sjoyce mcintosh 	fileinfo->fi_atime = attr.sa_vattr.va_atime;
1240037cac00Sjoyce mcintosh 	fileinfo->fi_mtime = attr.sa_vattr.va_mtime;
1241037cac00Sjoyce mcintosh 	fileinfo->fi_ctime = attr.sa_vattr.va_ctime;
1242037cac00Sjoyce mcintosh 	if (attr.sa_crtime.tv_sec)
1243037cac00Sjoyce mcintosh 		fileinfo->fi_crtime = attr.sa_crtime;
12447f667e74Sjose borrego 	else
1245037cac00Sjoyce mcintosh 		fileinfo->fi_crtime = attr.sa_vattr.va_mtime;
12467f667e74Sjose borrego 
12477f667e74Sjose borrego 	smb_node_release(fnode);
12487f667e74Sjose borrego 	return (0);
12497f667e74Sjose borrego }
12507f667e74Sjose borrego 
12517f667e74Sjose borrego /*
12527f667e74Sjose borrego  * smb_odir_wildcard_fileinfo
12537f667e74Sjose borrego  *
12547f667e74Sjose borrego  * odirent contains a directory entry, obtained from a vop_readdir.
12557f667e74Sjose borrego  * If a case conflict is identified the filename is mangled and the
1256148c5f43SAlan Wright  * shortname is used as 'name', in place of odirent->od_name.
12577f667e74Sjose borrego  *
12587f667e74Sjose borrego  * If the looked up file is a link, we attempt to lookup the link target
12597f667e74Sjose borrego  * to use its attributes in place of those of the files's.
12607f667e74Sjose borrego  * If we fail to lookup the target of the link we use the original
12617f667e74Sjose borrego  * file's attributes.
12627f667e74Sjose borrego  * Check if the attributes match the search attributes.
12637f667e74Sjose borrego  *
12647f667e74Sjose borrego  * Although some file systems can have directories larger than
12657f667e74Sjose borrego  * SMB_MAXDIRSIZE smb_odir_next_odirent ensures that no offset larger
12667f667e74Sjose borrego  * than SMB_MAXDIRSIZE is returned.  It is therefore safe to use the
12677f667e74Sjose borrego  * offset as the cookie (uint32_t).
12687f667e74Sjose borrego  *
12697f667e74Sjose borrego  * Returns: 0 - success
12707f667e74Sjose borrego  *     ENOENT - no match, proceed to next entry
12717f667e74Sjose borrego  *      errno - error
12727f667e74Sjose borrego  */
12737f667e74Sjose borrego static int
12747f667e74Sjose borrego smb_odir_wildcard_fileinfo(smb_request_t *sr, smb_odir_t *od,
12757f667e74Sjose borrego     smb_odirent_t *odirent, smb_fileinfo_t *fileinfo)
12767f667e74Sjose borrego {
12777f667e74Sjose borrego 	int		rc;
12785a485655SKevin Crowe 	cred_t		*cr;
12797f667e74Sjose borrego 	smb_node_t	*fnode, *tgt_node;
1280037cac00Sjoyce mcintosh 	smb_attr_t	attr;
12817f667e74Sjose borrego 	char		*name;
12827f667e74Sjose borrego 	boolean_t	case_conflict;
12837f667e74Sjose borrego 
12847f667e74Sjose borrego 	ASSERT(sr);
12857f667e74Sjose borrego 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
12867f667e74Sjose borrego 	ASSERT(od);
12877f667e74Sjose borrego 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
12887f667e74Sjose borrego 
12897f667e74Sjose borrego 	ASSERT(MUTEX_HELD(&od->d_mutex));
12907f667e74Sjose borrego 	bzero(fileinfo, sizeof (smb_fileinfo_t));
12917f667e74Sjose borrego 
1292148c5f43SAlan Wright 	rc = smb_fsop_lookup(sr, od->d_cred, SMB_CASE_SENSITIVE,
1293148c5f43SAlan Wright 	    od->d_tree->t_snode, od->d_dnode, odirent->od_name, &fnode);
12947f667e74Sjose borrego 	if (rc != 0)
12957f667e74Sjose borrego 		return (rc);
12967f667e74Sjose borrego 
12977f667e74Sjose borrego 	/* follow link to get target node & attr */
12989fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	if (smb_node_is_symlink(fnode) &&
1299148c5f43SAlan Wright 	    smb_odir_lookup_link(sr, od, odirent->od_name, &tgt_node)) {
13007f667e74Sjose borrego 		smb_node_release(fnode);
13017f667e74Sjose borrego 		fnode = tgt_node;
1302037cac00Sjoyce mcintosh 	}
1303037cac00Sjoyce mcintosh 
13049fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	/* skip system files */
13059fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	if (smb_node_is_system(fnode)) {
13069fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		smb_node_release(fnode);
13079fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		return (ENOENT);
13089fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	}
13099fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
13105a485655SKevin Crowe 	/*
13115a485655SKevin Crowe 	 * Windows directory listings return not only names, but
13125a485655SKevin Crowe 	 * also some attributes.  In Unix, you need some access to
13135a485655SKevin Crowe 	 * get those attributes.  Which credential should we use to
13145a485655SKevin Crowe 	 * get those?  If we're doing Access Based Enumeration (ABE)
13155a485655SKevin Crowe 	 * we want this getattr to fail, which will cause the caller
13165a485655SKevin Crowe 	 * to skip this entry.  If we're NOT doing ABE, we normally
13175a485655SKevin Crowe 	 * want to show all the directory entries (including their
13185a485655SKevin Crowe 	 * attributes) so we want this getattr to succeed!
13195a485655SKevin Crowe 	 */
13205a485655SKevin Crowe 	if (smb_tree_has_feature(od->d_tree, SMB_TREE_ABE))
13215a485655SKevin Crowe 		cr = od->d_cred;
13225a485655SKevin Crowe 	else
13235a485655SKevin Crowe 		cr = zone_kcred();
13245a485655SKevin Crowe 
13255fd03bc0SGordon Ross 	bzero(&attr, sizeof (attr));
13265fd03bc0SGordon Ross 	attr.sa_mask = SMB_AT_ALL;
13275a485655SKevin Crowe 	rc = smb_node_getattr(NULL, fnode, cr, NULL, &attr);
13285fd03bc0SGordon Ross 	if (rc != 0) {
1329037cac00Sjoyce mcintosh 		smb_node_release(fnode);
1330037cac00Sjoyce mcintosh 		return (rc);
13317f667e74Sjose borrego 	}
13327f667e74Sjose borrego 
13337f667e74Sjose borrego 	/* check search attributes */
1334037cac00Sjoyce mcintosh 	if (!smb_sattr_check(attr.sa_dosattr, od->d_sattr)) {
13357f667e74Sjose borrego 		smb_node_release(fnode);
13367f667e74Sjose borrego 		return (ENOENT);
13377f667e74Sjose borrego 	}
13387f667e74Sjose borrego 
1339cb174861Sjoyce mcintosh 	name = odirent->od_name;
1340cb174861Sjoyce mcintosh 	if (od->d_flags & SMB_ODIR_FLAG_SHORTNAMES) {
1341148c5f43SAlan Wright 		case_conflict = ((od->d_flags & SMB_ODIR_FLAG_IGNORE_CASE) &&
1342148c5f43SAlan Wright 		    (odirent->od_eflags & ED_CASE_CONFLICT));
1343cb174861Sjoyce mcintosh 		if (case_conflict || smb_needs_mangled(name)) {
1344cb174861Sjoyce mcintosh 			smb_mangle(name, odirent->od_ino,
1345148c5f43SAlan Wright 			    fileinfo->fi_shortname, SMB_SHORTNAMELEN);
1346148c5f43SAlan Wright 		}
1347cb174861Sjoyce mcintosh 		if (case_conflict)
1348cb174861Sjoyce mcintosh 			name = fileinfo->fi_shortname;
1349cb174861Sjoyce mcintosh 	}
1350148c5f43SAlan Wright 
1351148c5f43SAlan Wright 	(void) strlcpy(fileinfo->fi_name, name, sizeof (fileinfo->fi_name));
1352148c5f43SAlan Wright 
13537f667e74Sjose borrego 	fileinfo->fi_cookie = (uint32_t)od->d_offset;
1354037cac00Sjoyce mcintosh 	fileinfo->fi_dosattr = attr.sa_dosattr;
1355037cac00Sjoyce mcintosh 	fileinfo->fi_nodeid = attr.sa_vattr.va_nodeid;
1356037cac00Sjoyce mcintosh 	fileinfo->fi_size = attr.sa_vattr.va_size;
1357e3f2c991SKeyur Desai 	fileinfo->fi_alloc_size = attr.sa_allocsz;
1358037cac00Sjoyce mcintosh 	fileinfo->fi_atime = attr.sa_vattr.va_atime;
1359037cac00Sjoyce mcintosh 	fileinfo->fi_mtime = attr.sa_vattr.va_mtime;
1360037cac00Sjoyce mcintosh 	fileinfo->fi_ctime = attr.sa_vattr.va_ctime;
1361037cac00Sjoyce mcintosh 	if (attr.sa_crtime.tv_sec)
1362037cac00Sjoyce mcintosh 		fileinfo->fi_crtime = attr.sa_crtime;
13637f667e74Sjose borrego 	else
1364037cac00Sjoyce mcintosh 		fileinfo->fi_crtime = attr.sa_vattr.va_mtime;
13657f667e74Sjose borrego 
13667f667e74Sjose borrego 	smb_node_release(fnode);
13677f667e74Sjose borrego 	return (0);
13687f667e74Sjose borrego }
13697f667e74Sjose borrego 
13707f667e74Sjose borrego /*
13717f667e74Sjose borrego  * smb_odir_lookup_link
13727f667e74Sjose borrego  *
13737f667e74Sjose borrego  * If the file is a symlink we lookup the object to which the
13747f667e74Sjose borrego  * symlink refers so that we can return its attributes.
13757f667e74Sjose borrego  * This can cause a problem if a symlink in a sub-directory
13767f667e74Sjose borrego  * points to a parent directory (some UNIX GUI's create a symlink
13777f667e74Sjose borrego  * in $HOME/.desktop that points to the user's home directory).
13787f667e74Sjose borrego  * Some Windows applications (e.g. virus scanning) loop/hang
13797f667e74Sjose borrego  * trying to follow this recursive path and there is little
13807f667e74Sjose borrego  * we can do because the path is constructed on the client.
13817f667e74Sjose borrego  * smb_dirsymlink_enable allows an end-user to disable
13827f667e74Sjose borrego  * symlinks to directories. Symlinks to other object types
13837f667e74Sjose borrego  * should be unaffected.
13847f667e74Sjose borrego  *
13857f667e74Sjose borrego  * Returns: B_TRUE  - followed link. tgt_node and tgt_attr set
13867f667e74Sjose borrego  *          B_FALSE - link not followed
13877f667e74Sjose borrego  */
13887f667e74Sjose borrego static boolean_t
13897f667e74Sjose borrego smb_odir_lookup_link(smb_request_t *sr, smb_odir_t *od,
1390037cac00Sjoyce mcintosh     char *fname, smb_node_t **tgt_node)
13917f667e74Sjose borrego {
13927f667e74Sjose borrego 	int rc;
1393148c5f43SAlan Wright 	uint32_t flags = SMB_FOLLOW_LINKS | SMB_CASE_SENSITIVE;
13947f667e74Sjose borrego 
1395148c5f43SAlan Wright 	rc = smb_fsop_lookup(sr, od->d_cred, flags,
1396037cac00Sjoyce mcintosh 	    od->d_tree->t_snode, od->d_dnode, fname, tgt_node);
13977f667e74Sjose borrego 	if (rc != 0) {
13987f667e74Sjose borrego 		*tgt_node = NULL;
13997f667e74Sjose borrego 		return (B_FALSE);
14007f667e74Sjose borrego 	}
14017f667e74Sjose borrego 
1402037cac00Sjoyce mcintosh 	if (smb_node_is_dir(*tgt_node) && (!smb_dirsymlink_enable)) {
14037f667e74Sjose borrego 		smb_node_release(*tgt_node);
14047f667e74Sjose borrego 		*tgt_node = NULL;
14057f667e74Sjose borrego 		return (B_FALSE);
14067f667e74Sjose borrego 	}
14077f667e74Sjose borrego 
14087f667e74Sjose borrego 	return (B_TRUE);
14097f667e74Sjose borrego }
1410cb174861Sjoyce mcintosh 
1411cb174861Sjoyce mcintosh /*
1412cb174861Sjoyce mcintosh  * smb_odir_match_name
1413cb174861Sjoyce mcintosh  *
1414cb174861Sjoyce mcintosh  * Check if the directory entry name matches the search pattern:
1415cb174861Sjoyce mcintosh  * - Don't match reserved dos filenames.
1416cb174861Sjoyce mcintosh  * - Check if odirent->od_name matches od->d_pattern.
1417cb174861Sjoyce mcintosh  * - If shortnames are supported, generate the shortname from
1418cb174861Sjoyce mcintosh  *   odirent->od_name and check if it matches od->d_pattern.
1419cb174861Sjoyce mcintosh  */
1420c13be35aSGordon Ross static boolean_t
1421cb174861Sjoyce mcintosh smb_odir_match_name(smb_odir_t *od, smb_odirent_t *odirent)
1422cb174861Sjoyce mcintosh {
1423cb174861Sjoyce mcintosh 	char	*name = odirent->od_name;
1424cb174861Sjoyce mcintosh 	char	shortname[SMB_SHORTNAMELEN];
1425cb174861Sjoyce mcintosh 	ino64_t	ino = odirent->od_ino;
1426c13be35aSGordon Ross 	boolean_t ci = (od->d_flags & SMB_ODIR_FLAG_IGNORE_CASE) != 0;
1427cb174861Sjoyce mcintosh 
1428cb174861Sjoyce mcintosh 	if (smb_is_reserved_dos_name(name))
1429cb174861Sjoyce mcintosh 		return (B_FALSE);
1430cb174861Sjoyce mcintosh 
1431c13be35aSGordon Ross 	if (smb_match(od->d_pattern, name, ci))
1432cb174861Sjoyce mcintosh 		return (B_TRUE);
1433cb174861Sjoyce mcintosh 
1434cb174861Sjoyce mcintosh 	if (od->d_flags & SMB_ODIR_FLAG_SHORTNAMES) {
1435cb174861Sjoyce mcintosh 		smb_mangle(name, ino, shortname, SMB_SHORTNAMELEN);
1436c13be35aSGordon Ross 		if (smb_match(od->d_pattern, shortname, ci))
1437cb174861Sjoyce mcintosh 			return (B_TRUE);
1438cb174861Sjoyce mcintosh 	}
1439cb174861Sjoyce mcintosh 
1440cb174861Sjoyce mcintosh 	return (B_FALSE);
1441cb174861Sjoyce mcintosh }
1442