xref: /onnv-gate/usr/src/uts/common/fs/smbsrv/smb_odir.c (revision 9635:2f6c15d38a84)
15331Samw /*
25331Samw  * CDDL HEADER START
35331Samw  *
45331Samw  * The contents of this file are subject to the terms of the
55331Samw  * Common Development and Distribution License (the "License").
65331Samw  * You may not use this file except in compliance with the License.
75331Samw  *
85331Samw  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
95331Samw  * or http://www.opensolaris.org/os/licensing.
105331Samw  * See the License for the specific language governing permissions
115331Samw  * and limitations under the License.
125331Samw  *
135331Samw  * When distributing Covered Code, include this CDDL HEADER in each
145331Samw  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155331Samw  * If applicable, add the following below this CDDL HEADER, with the
165331Samw  * fields enclosed by brackets "[]" replaced with your own identifying
175331Samw  * information: Portions Copyright [yyyy] [name of copyright owner]
185331Samw  *
195331Samw  * CDDL HEADER END
205331Samw  */
215331Samw /*
228670SJose.Borrego@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
235331Samw  * Use is subject to license terms.
245331Samw  */
255331Samw 
265331Samw /*
275331Samw  * General Structures Layout
285331Samw  * -------------------------
295331Samw  *
305331Samw  * This is a simplified diagram showing the relationship between most of the
315331Samw  * main structures.
325331Samw  *
335331Samw  * +-------------------+
345331Samw  * |     SMB_INFO      |
355331Samw  * +-------------------+
365331Samw  *          |
375331Samw  *          |
385331Samw  *          v
395331Samw  * +-------------------+       +-------------------+      +-------------------+
405331Samw  * |     SESSION       |<----->|     SESSION       |......|      SESSION      |
415331Samw  * +-------------------+       +-------------------+      +-------------------+
425331Samw  *          |
435331Samw  *          |
445331Samw  *          v
455331Samw  * +-------------------+       +-------------------+      +-------------------+
465331Samw  * |       USER        |<----->|       USER        |......|       USER        |
475331Samw  * +-------------------+       +-------------------+      +-------------------+
485331Samw  *          |
495331Samw  *          |
505331Samw  *          v
515331Samw  * +-------------------+       +-------------------+      +-------------------+
525331Samw  * |       TREE        |<----->|       TREE        |......|       TREE        |
535331Samw  * +-------------------+       +-------------------+      +-------------------+
545331Samw  *      |         |
555331Samw  *      |         |
565331Samw  *      |         v
575331Samw  *      |     +-------+       +-------+      +-------+
585331Samw  *      |     | OFILE |<----->| OFILE |......| OFILE |
595331Samw  *      |     +-------+       +-------+      +-------+
605331Samw  *      |
615331Samw  *      |
625331Samw  *      v
635331Samw  *  +-------+       +------+      +------+
645331Samw  *  | ODIR  |<----->| ODIR |......| ODIR |
655331Samw  *  +-------+       +------+      +------+
665331Samw  *
675331Samw  *
685331Samw  * Odir State Machine
695331Samw  * ------------------
705331Samw  *
718670SJose.Borrego@Sun.COM  *    +-------------------------+
728670SJose.Borrego@Sun.COM  *    |  SMB_ODIR_STATE_OPEN    |<----------- open / creation
735331Samw  *    +-------------------------+
74*9635SJoyce.McIntosh@Sun.COM  *	    |            ^
75*9635SJoyce.McIntosh@Sun.COM  *	    | (first)    | (last)
76*9635SJoyce.McIntosh@Sun.COM  *	    | lookup     | release
77*9635SJoyce.McIntosh@Sun.COM  *	    v            |
788670SJose.Borrego@Sun.COM  *    +-------------------------+
79*9635SJoyce.McIntosh@Sun.COM  *    | SMB_ODIR_STATE_IN_USE   |----
80*9635SJoyce.McIntosh@Sun.COM  *    +-------------------------+   | lookup / release / read
81*9635SJoyce.McIntosh@Sun.COM  *	    |                ^-------
82*9635SJoyce.McIntosh@Sun.COM  *	    | close
83*9635SJoyce.McIntosh@Sun.COM  *	    |
84*9635SJoyce.McIntosh@Sun.COM  *	    v
858670SJose.Borrego@Sun.COM  *    +-------------------------+
86*9635SJoyce.McIntosh@Sun.COM  *    | SMB_ODIR_STATE_CLOSING  |----
87*9635SJoyce.McIntosh@Sun.COM  *    +-------------------------+   | close / release / read
88*9635SJoyce.McIntosh@Sun.COM  *	    |                ^-------
89*9635SJoyce.McIntosh@Sun.COM  *	    | (last) release
90*9635SJoyce.McIntosh@Sun.COM  *	    |
91*9635SJoyce.McIntosh@Sun.COM  *	    v
925331Samw  *    +-------------------------+
938670SJose.Borrego@Sun.COM  *    | SMB_ODIR_STATE_CLOSED   |----------> deletion
945331Samw  *    +-------------------------+
955331Samw  *
968670SJose.Borrego@Sun.COM  *
975331Samw  * SMB_ODIR_STATE_OPEN
98*9635SJoyce.McIntosh@Sun.COM  * - the odir exists in the list of odirs of its tree
99*9635SJoyce.McIntosh@Sun.COM  * - lookup is valid in this state. It will place a hold on the odir
100*9635SJoyce.McIntosh@Sun.COM  *   by incrementing the reference count and the odir will transition
101*9635SJoyce.McIntosh@Sun.COM  *   to SMB_ODIR_STATE_IN_USE
102*9635SJoyce.McIntosh@Sun.COM  * - read/close/release not valid in this state
103*9635SJoyce.McIntosh@Sun.COM  *
104*9635SJoyce.McIntosh@Sun.COM  * SMB_ODIR_STATE_IN_USE
1058670SJose.Borrego@Sun.COM  * - the odir exists in the list of odirs of its tree.
106*9635SJoyce.McIntosh@Sun.COM  * - lookup is valid in this state. It will place a hold on the odir
107*9635SJoyce.McIntosh@Sun.COM  *   by incrementing the reference count.
108*9635SJoyce.McIntosh@Sun.COM  * - if the last hold is released the odir will transition
109*9635SJoyce.McIntosh@Sun.COM  *   back to SMB_ODIR_STATE_OPEN
1108670SJose.Borrego@Sun.COM  * - if a close is received the odir will transition to
1118670SJose.Borrego@Sun.COM  *   SMB_ODIR_STATE_CLOSING.
1125331Samw  *
1135331Samw  * SMB_ODIR_STATE_CLOSING
1148670SJose.Borrego@Sun.COM  * - the odir exists in the list of odirs of its tree.
115*9635SJoyce.McIntosh@Sun.COM  * - lookup will fail in this state.
116*9635SJoyce.McIntosh@Sun.COM  * - when the last hold is released the odir will transition
117*9635SJoyce.McIntosh@Sun.COM  *   to SMB_ODIR_STATE_CLOSED.
1185331Samw  *
1195331Samw  * SMB_ODIR_STATE_CLOSED
1208670SJose.Borrego@Sun.COM  * - the odir exists in the list of odirs of its tree.
1218670SJose.Borrego@Sun.COM  * - there are no users of the odir (refcnt == 0)
1228670SJose.Borrego@Sun.COM  * - the odir is being removed from the tree's list and deleted.
123*9635SJoyce.McIntosh@Sun.COM  * - lookup will fail in this state.
124*9635SJoyce.McIntosh@Sun.COM  * - read/close/release not valid in this state
1255331Samw  *
1265331Samw  * Comments
1275331Samw  * --------
1285331Samw  *    The state machine of the odir structures is controlled by 3 elements:
1295331Samw  *      - The list of odirs of the tree it belongs to.
1305331Samw  *      - The mutex embedded in the structure itself.
1315331Samw  *      - The reference count.
1325331Samw  *
1335331Samw  *    There's a mutex embedded in the odir structure used to protect its fields
1345331Samw  *    and there's a lock embedded in the list of odirs of a tree. To
1355331Samw  *    increment or to decrement the reference count the mutex must be entered.
1365331Samw  *    To insert the odir into the list of odirs of the tree and to remove
1375331Samw  *    the odir from it, the lock must be entered in RW_WRITER mode.
1385331Samw  *
1398670SJose.Borrego@Sun.COM  *    In order to avoid deadlocks, when both (mutex and lock of the odir
1408670SJose.Borrego@Sun.COM  *    list) have to be entered, the lock must be entered first.
1418670SJose.Borrego@Sun.COM  *
1428670SJose.Borrego@Sun.COM  *
1438670SJose.Borrego@Sun.COM  * Odir Interface
1448670SJose.Borrego@Sun.COM  * ---------------
1458670SJose.Borrego@Sun.COM  * odid = smb_odir_open(pathname)
1468670SJose.Borrego@Sun.COM  *	Create an odir representing the directory specified in pathname and
1478670SJose.Borrego@Sun.COM  *	add it into the tree's list of odirs.
1488670SJose.Borrego@Sun.COM  *	Return an identifier (odid) uniquely identifying the created odir.
1495331Samw  *
1508670SJose.Borrego@Sun.COM  * smb_odir_openat(smb_node_t *unode)
1518670SJose.Borrego@Sun.COM  *	Create an odir representing the extended attribute directory
1528670SJose.Borrego@Sun.COM  *	associated with the file (or directory) represented by unode
1538670SJose.Borrego@Sun.COM  *	and add it into the tree's list of odirs.
1548670SJose.Borrego@Sun.COM  *	Return an identifier (odid) uniquely identifying the created odir.
1558670SJose.Borrego@Sun.COM  *
1568670SJose.Borrego@Sun.COM  * smb_odir_t *odir = smb_tree_lookup_odir(odid)
1578670SJose.Borrego@Sun.COM  *	Find the odir corresponding to the specified odid in the tree's
158*9635SJoyce.McIntosh@Sun.COM  *	list of odirs. Place a hold on the odir.
1598670SJose.Borrego@Sun.COM  *
1608670SJose.Borrego@Sun.COM  * smb_odir_read(..., smb_odirent_t *odirent)
1618670SJose.Borrego@Sun.COM  *	Find the next directory entry in the odir and return it in odirent.
1625331Samw  *
1638670SJose.Borrego@Sun.COM  * smb_odir_read_fileinfo(..., smb_fileinfo_t *)
1648670SJose.Borrego@Sun.COM  *	Find the next directory entry in the odir. Return the details of
1658670SJose.Borrego@Sun.COM  *	the directory entry in smb_fileinfo_t. (See odir internals below)
1668670SJose.Borrego@Sun.COM  *
1678670SJose.Borrego@Sun.COM  * smb_odir_read_stream_info(..., smb_streaminfo_t *)
1688670SJose.Borrego@Sun.COM  *	Find the next named stream entry in the odir. Return the details of
1698670SJose.Borrego@Sun.COM  *	the named stream in smb_streaminfo_t.
1708670SJose.Borrego@Sun.COM  *
171*9635SJoyce.McIntosh@Sun.COM  * smb_odir_close(smb_odir_t *odir)
172*9635SJoyce.McIntosh@Sun.COM  *  Close the odir.
173*9635SJoyce.McIntosh@Sun.COM  *  The caller of close must have a hold on the odir being closed.
174*9635SJoyce.McIntosh@Sun.COM  *  The hold should be released after closing.
175*9635SJoyce.McIntosh@Sun.COM  *
1768670SJose.Borrego@Sun.COM  * smb_odir_release(smb_odir_t *odir)
1778670SJose.Borrego@Sun.COM  *	Release the hold on the odir, obtained by lookup.
1788670SJose.Borrego@Sun.COM  *
1798670SJose.Borrego@Sun.COM  *
1808670SJose.Borrego@Sun.COM  * Odir Internals
1818670SJose.Borrego@Sun.COM  * --------------
1828670SJose.Borrego@Sun.COM  * The odir object represent an open directory search. Each read operation
1838670SJose.Borrego@Sun.COM  * provides the caller with a structure containing information  pertaining
1848670SJose.Borrego@Sun.COM  * to the next directory entry that matches the search criteria, namely
1858670SJose.Borrego@Sun.COM  * the filename or match pattern and, in the case of smb_odir_read_fileinfo(),
1868670SJose.Borrego@Sun.COM  * the search attributes.
1878670SJose.Borrego@Sun.COM  *
1888670SJose.Borrego@Sun.COM  * The odir maintains a buffer (d_buf) of directory entries read from
1898670SJose.Borrego@Sun.COM  * the filesystem via a vop_readdir. The buffer is populated when a read
1908670SJose.Borrego@Sun.COM  * request (smb_odir_next_odirent) finds that the buffer is empty or that
1918670SJose.Borrego@Sun.COM  * the end of the buffer has been reached, and also when a new client request
1928670SJose.Borrego@Sun.COM  * (find next) begins.
1935331Samw  *
1948670SJose.Borrego@Sun.COM  * The data in d_buf (that which is returned from the file system) can
1958670SJose.Borrego@Sun.COM  * be in one of two formats. If the file system supports extended directory
1968670SJose.Borrego@Sun.COM  * entries we request that the data be returned as edirent_t structures. If
1978670SJose.Borrego@Sun.COM  * it does not the data will be returned as dirent64_t structures. For
1988670SJose.Borrego@Sun.COM  * convenience, when the next directory entry is read from d_buf by
1998670SJose.Borrego@Sun.COM  * smb_odir_next_odirent it is translated into an smb_odirent_t.
2008670SJose.Borrego@Sun.COM  *
2018670SJose.Borrego@Sun.COM  * smb_odir_read_fileinfo
2028670SJose.Borrego@Sun.COM  * The processing required to obtain the information to populate the caller's
2038670SJose.Borrego@Sun.COM  * smb_fileinfo_t differs depending upon whether the directory search is for a
2048670SJose.Borrego@Sun.COM  * single specified filename or for multiple files matching a search pattern.
2058670SJose.Borrego@Sun.COM  * Thus smb_odir_read_fileinfo uses two static functions:
2068670SJose.Borrego@Sun.COM  * smb_odir_single_fileinfo - obtains the smb_fileinfo_t info for the single
2078670SJose.Borrego@Sun.COM  * filename as specified in smb_odir_open request.
2088670SJose.Borrego@Sun.COM  * smb_odir_wildcard_fileinfo - obtains the smb_fileinfo_t info for the filename
2098670SJose.Borrego@Sun.COM  * returned from the smb_odir_next_odirent. This is called in a loop until
2108670SJose.Borrego@Sun.COM  * an entry matching the search criteria is found or no more entries exist.
2118670SJose.Borrego@Sun.COM  *
2128670SJose.Borrego@Sun.COM  * If a directory entry is a VLNK, the name returned in the smb_fileinfo_t
2138670SJose.Borrego@Sun.COM  * is the name of the directory entry but the attributes are the attribites
2148670SJose.Borrego@Sun.COM  * of the file that is the target of the link. If the link target cannot
2158670SJose.Borrego@Sun.COM  * be found the attributes returned are the attributes of the link itself.
2165331Samw  *
2178670SJose.Borrego@Sun.COM  * smb_odir_read_stream_info
2188670SJose.Borrego@Sun.COM  * In order for an odir to provide information about stream files it
2198670SJose.Borrego@Sun.COM  * must be opened with smb_odir_openat(). smb_odir_read_streaminfo() can
2208670SJose.Borrego@Sun.COM  * then be used to obtain the name and size of named stream files.
2215331Samw  *
2228670SJose.Borrego@Sun.COM  * Resuming a Search
2238670SJose.Borrego@Sun.COM  * -----------------
2248670SJose.Borrego@Sun.COM  * A directory search often consists of multiple client requests: an initial
2258670SJose.Borrego@Sun.COM  * find_first request followed by zero or more find_next requests and a
2268670SJose.Borrego@Sun.COM  * find_close request.
2278670SJose.Borrego@Sun.COM  * The find_first request will open and lookup the odir, read its desired
2288670SJose.Borrego@Sun.COM  * number of entries from the odir, then release the odir and return.
2298670SJose.Borrego@Sun.COM  * A find_next request will lookup the odir and read its desired number of
2308670SJose.Borrego@Sun.COM  * entries from the odir, then release the odir and return.
2318670SJose.Borrego@Sun.COM  * At the end of the search the find_close request will close the odir.
2328670SJose.Borrego@Sun.COM  *
2338670SJose.Borrego@Sun.COM  * In order to be able to resume a directory search (find_next) the odir
2348670SJose.Borrego@Sun.COM  * provides the capability for the caller to save one or more resume points
2358670SJose.Borrego@Sun.COM  * (cookies) at the end of a request, and to specify which resume point
2368670SJose.Borrego@Sun.COM  * (cookie) to restart from at the beginning of the next search.
2378670SJose.Borrego@Sun.COM  *	smb_odir_save_cookie(..., cookie)
2388670SJose.Borrego@Sun.COM  *	smb_odir_resume_at(smb_odir_resume_t *resume)
2398670SJose.Borrego@Sun.COM  * A search can be resumed at a specified resume point (cookie), the resume
2408670SJose.Borrego@Sun.COM  * point (cookie) stored at a specified index in the d_cookies array, or
2418670SJose.Borrego@Sun.COM  * a specified filename. The latter (specified filename) is not yet supported.
2428670SJose.Borrego@Sun.COM  *
2438670SJose.Borrego@Sun.COM  * See smb_search, smb_find, smb_find_unique, and smb_trans2_find for details
2445331Samw  */
2458670SJose.Borrego@Sun.COM 
2465331Samw #include <smbsrv/smb_incl.h>
2475331Samw #include <smbsrv/smb_kproto.h>
2485331Samw #include <smbsrv/smb_fsops.h>
2498670SJose.Borrego@Sun.COM #include <sys/extdirent.h>
2505331Samw 
2518670SJose.Borrego@Sun.COM /* static functions */
2528670SJose.Borrego@Sun.COM static smb_odir_t *smb_odir_create(smb_request_t *, smb_node_t *,
2539343SAfshin.Ardakani@Sun.COM     char *, uint16_t, cred_t *);
2548670SJose.Borrego@Sun.COM static void smb_odir_delete(smb_odir_t *);
2558670SJose.Borrego@Sun.COM static int smb_odir_single_fileinfo(smb_request_t *, smb_odir_t *,
2568670SJose.Borrego@Sun.COM     smb_fileinfo_t *);
2578670SJose.Borrego@Sun.COM static int smb_odir_wildcard_fileinfo(smb_request_t *, smb_odir_t *,
2588670SJose.Borrego@Sun.COM     smb_odirent_t *, smb_fileinfo_t *);
2598670SJose.Borrego@Sun.COM static int smb_odir_next_odirent(smb_odir_t *, smb_odirent_t *);
2608670SJose.Borrego@Sun.COM static boolean_t smb_odir_lookup_link(smb_request_t *, smb_odir_t *, char *,
2618670SJose.Borrego@Sun.COM     smb_node_t **, smb_attr_t *);
2625331Samw 
2635331Samw 
2645331Samw /*
2655331Samw  * smb_odir_open
2668670SJose.Borrego@Sun.COM  *
2678670SJose.Borrego@Sun.COM  * Create an odir representing the directory specified in pathname.
2688670SJose.Borrego@Sun.COM  *
2698670SJose.Borrego@Sun.COM  * Returns:
2708670SJose.Borrego@Sun.COM  * odid - Unique identifier of newly created odir.
2718670SJose.Borrego@Sun.COM  *    0 - error, error details set in sr.
2725331Samw  */
2738670SJose.Borrego@Sun.COM uint16_t
2749343SAfshin.Ardakani@Sun.COM smb_odir_open(smb_request_t *sr, char *path, uint16_t sattr, uint32_t flags)
2755331Samw {
2768670SJose.Borrego@Sun.COM 	int		rc;
2778670SJose.Borrego@Sun.COM 	smb_tree_t	*tree;
2788670SJose.Borrego@Sun.COM 	smb_node_t	*dnode;
2798670SJose.Borrego@Sun.COM 	char		pattern[MAXNAMELEN];
2808670SJose.Borrego@Sun.COM 	smb_odir_t 	*od;
2819343SAfshin.Ardakani@Sun.COM 	cred_t		*cr;
2825331Samw 
2838670SJose.Borrego@Sun.COM 	ASSERT(sr);
2848670SJose.Borrego@Sun.COM 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
2858670SJose.Borrego@Sun.COM 	ASSERT(sr->tid_tree);
2868670SJose.Borrego@Sun.COM 	ASSERT(sr->tid_tree->t_magic == SMB_TREE_MAGIC);
2875331Samw 
2888670SJose.Borrego@Sun.COM 	tree = sr->tid_tree;
2898670SJose.Borrego@Sun.COM 
2908670SJose.Borrego@Sun.COM 	rc = smb_pathname_reduce(sr, sr->user_cr, path,
2918670SJose.Borrego@Sun.COM 	    tree->t_snode, tree->t_snode, &dnode, pattern);
2928670SJose.Borrego@Sun.COM 	if (rc != 0) {
2938670SJose.Borrego@Sun.COM 		smbsr_errno(sr, rc);
2948670SJose.Borrego@Sun.COM 		return (0);
2955331Samw 	}
2965331Samw 
2978670SJose.Borrego@Sun.COM 	if (dnode->vp->v_type != VDIR) {
2988670SJose.Borrego@Sun.COM 		smbsr_error(sr, NT_STATUS_OBJECT_PATH_NOT_FOUND,
2998670SJose.Borrego@Sun.COM 		    ERRDOS, ERROR_PATH_NOT_FOUND);
3008670SJose.Borrego@Sun.COM 		smb_node_release(dnode);
3018670SJose.Borrego@Sun.COM 		return (0);
3025331Samw 	}
3035331Samw 
3048670SJose.Borrego@Sun.COM 	if (smb_fsop_access(sr, sr->user_cr, dnode, FILE_LIST_DIRECTORY) != 0) {
3058670SJose.Borrego@Sun.COM 		smbsr_error(sr, NT_STATUS_ACCESS_DENIED,
3068670SJose.Borrego@Sun.COM 		    ERRDOS, ERROR_ACCESS_DENIED);
3078670SJose.Borrego@Sun.COM 		smb_node_release(dnode);
3088670SJose.Borrego@Sun.COM 		return (0);
3098670SJose.Borrego@Sun.COM 	}
3105331Samw 
3119343SAfshin.Ardakani@Sun.COM 	if (flags & SMB_ODIR_OPENF_BACKUP_INTENT)
3129343SAfshin.Ardakani@Sun.COM 		cr = smb_user_getprivcred(tree->t_user);
3139343SAfshin.Ardakani@Sun.COM 	else
3149343SAfshin.Ardakani@Sun.COM 		cr = tree->t_user->u_cred;
3159343SAfshin.Ardakani@Sun.COM 
3169343SAfshin.Ardakani@Sun.COM 	od = smb_odir_create(sr, dnode, pattern, sattr, cr);
3178670SJose.Borrego@Sun.COM 	smb_node_release(dnode);
3188670SJose.Borrego@Sun.COM 	return (od ? od->d_odid : 0);
3195331Samw }
3205331Samw 
3215331Samw /*
3228670SJose.Borrego@Sun.COM  * smb_odir_openat
3238670SJose.Borrego@Sun.COM  *
3248670SJose.Borrego@Sun.COM  * Create an odir representing the extended attribute directory
3258670SJose.Borrego@Sun.COM  * associated with the file (or directory) represented by unode.
3268670SJose.Borrego@Sun.COM  *
3278670SJose.Borrego@Sun.COM  * Returns:
3288670SJose.Borrego@Sun.COM  * odid - Unique identifier of newly created odir.
3298670SJose.Borrego@Sun.COM  *    0 - error, error details set in sr.
3308670SJose.Borrego@Sun.COM  */
3318670SJose.Borrego@Sun.COM uint16_t
3328670SJose.Borrego@Sun.COM smb_odir_openat(smb_request_t *sr, smb_node_t *unode)
3338670SJose.Borrego@Sun.COM {
3348670SJose.Borrego@Sun.COM 	int		rc;
3358670SJose.Borrego@Sun.COM 	vnode_t		*xattr_dvp;
3368670SJose.Borrego@Sun.COM 	smb_odir_t	*od;
3378670SJose.Borrego@Sun.COM 	cred_t		*cr;
3388670SJose.Borrego@Sun.COM 	char		pattern[SMB_STREAM_PREFIX_LEN + 2];
3398670SJose.Borrego@Sun.COM 
3408670SJose.Borrego@Sun.COM 	smb_node_t	*xattr_dnode;
3418670SJose.Borrego@Sun.COM 	smb_attr_t	tmp_attr;
3428670SJose.Borrego@Sun.COM 
3438670SJose.Borrego@Sun.COM 	ASSERT(sr);
3448670SJose.Borrego@Sun.COM 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
3458670SJose.Borrego@Sun.COM 	ASSERT(unode);
3468670SJose.Borrego@Sun.COM 	ASSERT(unode->n_magic == SMB_NODE_MAGIC);
3478670SJose.Borrego@Sun.COM 
3488845Samw@Sun.COM 	if (SMB_TREE_CONTAINS_NODE(sr, unode) == 0 ||
3498845Samw@Sun.COM 	    SMB_TREE_HAS_ACCESS(sr, ACE_LIST_DIRECTORY) == 0) {
3508670SJose.Borrego@Sun.COM 		smbsr_error(sr, NT_STATUS_ACCESS_DENIED,
3518670SJose.Borrego@Sun.COM 		    ERRDOS, ERROR_ACCESS_DENIED);
3528670SJose.Borrego@Sun.COM 		return (0);
3538670SJose.Borrego@Sun.COM 	}
3549343SAfshin.Ardakani@Sun.COM 	cr = kcred;
3558670SJose.Borrego@Sun.COM 
3568670SJose.Borrego@Sun.COM 	/* find the xattrdir vnode */
3578670SJose.Borrego@Sun.COM 	rc = smb_vop_lookup_xattrdir(unode->vp, &xattr_dvp, LOOKUP_XATTR, cr);
3588670SJose.Borrego@Sun.COM 	if (rc != 0) {
3598670SJose.Borrego@Sun.COM 		smbsr_errno(sr, rc);
3608670SJose.Borrego@Sun.COM 		return (0);
3618670SJose.Borrego@Sun.COM 	}
3628670SJose.Borrego@Sun.COM 
3638670SJose.Borrego@Sun.COM 	/* lookup the xattrdir's smb_node */
3648670SJose.Borrego@Sun.COM 	xattr_dnode = smb_node_lookup(sr, NULL, cr, xattr_dvp, XATTR_DIR,
3658670SJose.Borrego@Sun.COM 	    unode, NULL, &tmp_attr);
3668670SJose.Borrego@Sun.COM 	VN_RELE(xattr_dvp);
3678670SJose.Borrego@Sun.COM 	if (xattr_dnode == NULL) {
3688670SJose.Borrego@Sun.COM 		smbsr_error(sr, NT_STATUS_NO_MEMORY,
3698670SJose.Borrego@Sun.COM 		    ERRDOS, ERROR_NOT_ENOUGH_MEMORY);
3708670SJose.Borrego@Sun.COM 		return (0);
3718670SJose.Borrego@Sun.COM 	}
3728670SJose.Borrego@Sun.COM 
3738670SJose.Borrego@Sun.COM 	(void) snprintf(pattern, sizeof (pattern), "%s*", SMB_STREAM_PREFIX);
3749343SAfshin.Ardakani@Sun.COM 	od = smb_odir_create(sr, xattr_dnode, pattern, SMB_SEARCH_ATTRIBUTES,
3759343SAfshin.Ardakani@Sun.COM 	    cr);
3768670SJose.Borrego@Sun.COM 	smb_node_release(xattr_dnode);
3778670SJose.Borrego@Sun.COM 	if (od == NULL)
3788670SJose.Borrego@Sun.COM 		return (0);
3798670SJose.Borrego@Sun.COM 
3809231SAfshin.Ardakani@Sun.COM 	od->d_flags |= SMB_ODIR_FLAG_XATTR;
3818670SJose.Borrego@Sun.COM 	return (od->d_odid);
3828670SJose.Borrego@Sun.COM }
3838670SJose.Borrego@Sun.COM 
3848670SJose.Borrego@Sun.COM /*
3858670SJose.Borrego@Sun.COM  * smb_odir_hold
386*9635SJoyce.McIntosh@Sun.COM  *
387*9635SJoyce.McIntosh@Sun.COM  * A hold will only be granted if the odir is open or in_use.
3888670SJose.Borrego@Sun.COM  */
3898670SJose.Borrego@Sun.COM boolean_t
3908670SJose.Borrego@Sun.COM smb_odir_hold(smb_odir_t *od)
3918670SJose.Borrego@Sun.COM {
3928670SJose.Borrego@Sun.COM 	ASSERT(od);
3938670SJose.Borrego@Sun.COM 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
3948670SJose.Borrego@Sun.COM 
3958670SJose.Borrego@Sun.COM 	mutex_enter(&od->d_mutex);
396*9635SJoyce.McIntosh@Sun.COM 
397*9635SJoyce.McIntosh@Sun.COM 	switch (od->d_state) {
398*9635SJoyce.McIntosh@Sun.COM 	case SMB_ODIR_STATE_OPEN:
399*9635SJoyce.McIntosh@Sun.COM 		od->d_refcnt++;
400*9635SJoyce.McIntosh@Sun.COM 		od->d_state = SMB_ODIR_STATE_IN_USE;
401*9635SJoyce.McIntosh@Sun.COM 		break;
402*9635SJoyce.McIntosh@Sun.COM 	case SMB_ODIR_STATE_IN_USE:
403*9635SJoyce.McIntosh@Sun.COM 		od->d_refcnt++;
404*9635SJoyce.McIntosh@Sun.COM 		break;
405*9635SJoyce.McIntosh@Sun.COM 	case SMB_ODIR_STATE_CLOSING:
406*9635SJoyce.McIntosh@Sun.COM 	case SMB_ODIR_STATE_CLOSED:
407*9635SJoyce.McIntosh@Sun.COM 	default:
4088670SJose.Borrego@Sun.COM 		mutex_exit(&od->d_mutex);
4098670SJose.Borrego@Sun.COM 		return (B_FALSE);
4108670SJose.Borrego@Sun.COM 	}
4118670SJose.Borrego@Sun.COM 
4128670SJose.Borrego@Sun.COM 	mutex_exit(&od->d_mutex);
4138670SJose.Borrego@Sun.COM 	return (B_TRUE);
4148670SJose.Borrego@Sun.COM }
4158670SJose.Borrego@Sun.COM 
4168670SJose.Borrego@Sun.COM /*
4178670SJose.Borrego@Sun.COM  * smb_odir_release
4188670SJose.Borrego@Sun.COM  *
4198670SJose.Borrego@Sun.COM  * If the odir is in SMB_ODIR_STATE_CLOSING and this release
4208670SJose.Borrego@Sun.COM  * results in a refcnt of 0, the odir may be removed from
4218670SJose.Borrego@Sun.COM  * the tree's list of odirs and deleted.  The odir's state is
4228670SJose.Borrego@Sun.COM  * set to SMB_ODIR_STATE_CLOSED prior to exiting the mutex and
423*9635SJoyce.McIntosh@Sun.COM  * deleting the odir.
4245331Samw  */
4255331Samw void
4268670SJose.Borrego@Sun.COM smb_odir_release(smb_odir_t *od)
4275331Samw {
4285331Samw 	ASSERT(od);
4295331Samw 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
4305331Samw 
4315331Samw 	mutex_enter(&od->d_mutex);
432*9635SJoyce.McIntosh@Sun.COM 	ASSERT(od->d_refcnt > 0);
4338670SJose.Borrego@Sun.COM 
4345331Samw 	switch (od->d_state) {
4355331Samw 	case SMB_ODIR_STATE_OPEN:
436*9635SJoyce.McIntosh@Sun.COM 		break;
437*9635SJoyce.McIntosh@Sun.COM 	case SMB_ODIR_STATE_IN_USE:
4388670SJose.Borrego@Sun.COM 		od->d_refcnt--;
439*9635SJoyce.McIntosh@Sun.COM 		if (od->d_refcnt == 0)
440*9635SJoyce.McIntosh@Sun.COM 			od->d_state = SMB_ODIR_STATE_OPEN;
4415331Samw 		break;
4425331Samw 	case SMB_ODIR_STATE_CLOSING:
4438670SJose.Borrego@Sun.COM 		od->d_refcnt--;
4448670SJose.Borrego@Sun.COM 		if (od->d_refcnt == 0) {
4458670SJose.Borrego@Sun.COM 			od->d_state = SMB_ODIR_STATE_CLOSED;
4468670SJose.Borrego@Sun.COM 			mutex_exit(&od->d_mutex);
4478670SJose.Borrego@Sun.COM 			smb_odir_delete(od);
4488670SJose.Borrego@Sun.COM 			return;
4498670SJose.Borrego@Sun.COM 		}
4508670SJose.Borrego@Sun.COM 		break;
4515331Samw 	case SMB_ODIR_STATE_CLOSED:
4525331Samw 	default:
4535331Samw 		break;
4545331Samw 	}
4555331Samw 
4565331Samw 	mutex_exit(&od->d_mutex);
4575331Samw }
4585331Samw 
4595331Samw /*
4608670SJose.Borrego@Sun.COM  * smb_odir_close
4615331Samw  */
4628670SJose.Borrego@Sun.COM void
4638670SJose.Borrego@Sun.COM smb_odir_close(smb_odir_t *od)
4645331Samw {
4658670SJose.Borrego@Sun.COM 	ASSERT(od);
466*9635SJoyce.McIntosh@Sun.COM 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
4675331Samw 
4688670SJose.Borrego@Sun.COM 	mutex_enter(&od->d_mutex);
469*9635SJoyce.McIntosh@Sun.COM 	ASSERT(od->d_refcnt > 0);
470*9635SJoyce.McIntosh@Sun.COM 	switch (od->d_state) {
471*9635SJoyce.McIntosh@Sun.COM 	case SMB_ODIR_STATE_OPEN:
472*9635SJoyce.McIntosh@Sun.COM 		break;
473*9635SJoyce.McIntosh@Sun.COM 	case SMB_ODIR_STATE_IN_USE:
474*9635SJoyce.McIntosh@Sun.COM 		od->d_state = SMB_ODIR_STATE_CLOSING;
475*9635SJoyce.McIntosh@Sun.COM 		break;
476*9635SJoyce.McIntosh@Sun.COM 	case SMB_ODIR_STATE_CLOSING:
477*9635SJoyce.McIntosh@Sun.COM 	case SMB_ODIR_STATE_CLOSED:
478*9635SJoyce.McIntosh@Sun.COM 	default:
479*9635SJoyce.McIntosh@Sun.COM 		break;
4808670SJose.Borrego@Sun.COM 	}
4818670SJose.Borrego@Sun.COM 	mutex_exit(&od->d_mutex);
4828670SJose.Borrego@Sun.COM }
4835331Samw 
4848670SJose.Borrego@Sun.COM /*
4858670SJose.Borrego@Sun.COM  * smb_odir_read
4868670SJose.Borrego@Sun.COM  *
4878670SJose.Borrego@Sun.COM  * Find the next directory entry matching the search pattern.
4888670SJose.Borrego@Sun.COM  * No search attribute matching is performed.
4898670SJose.Borrego@Sun.COM  *
4908670SJose.Borrego@Sun.COM  * Returns:
4918670SJose.Borrego@Sun.COM  *  0 - success.
4928670SJose.Borrego@Sun.COM  *      - If a matching entry was found eof will be B_FALSE and
4938670SJose.Borrego@Sun.COM  *        odirent will be populated.
4948670SJose.Borrego@Sun.COM  *      - If there are no matching entries eof will be B_TRUE.
4958670SJose.Borrego@Sun.COM  * -1 - error, error details set in sr.
4968670SJose.Borrego@Sun.COM  */
4978670SJose.Borrego@Sun.COM int
4988670SJose.Borrego@Sun.COM smb_odir_read(smb_request_t *sr, smb_odir_t *od,
4998670SJose.Borrego@Sun.COM     smb_odirent_t *odirent, boolean_t *eof)
5008670SJose.Borrego@Sun.COM {
5019231SAfshin.Ardakani@Sun.COM 	int		rc;
5029231SAfshin.Ardakani@Sun.COM 	boolean_t	ignore_case;
5038670SJose.Borrego@Sun.COM 
5048670SJose.Borrego@Sun.COM 	ASSERT(sr);
5058670SJose.Borrego@Sun.COM 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
5068670SJose.Borrego@Sun.COM 	ASSERT(od);
5078670SJose.Borrego@Sun.COM 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
5088670SJose.Borrego@Sun.COM 	ASSERT(odirent);
5098670SJose.Borrego@Sun.COM 
5108670SJose.Borrego@Sun.COM 	mutex_enter(&od->d_mutex);
511*9635SJoyce.McIntosh@Sun.COM 	ASSERT(od->d_refcnt > 0);
5128670SJose.Borrego@Sun.COM 
513*9635SJoyce.McIntosh@Sun.COM 	switch (od->d_state) {
514*9635SJoyce.McIntosh@Sun.COM 	case SMB_ODIR_STATE_IN_USE:
515*9635SJoyce.McIntosh@Sun.COM 	case SMB_ODIR_STATE_CLOSING:
516*9635SJoyce.McIntosh@Sun.COM 		break;
517*9635SJoyce.McIntosh@Sun.COM 	case SMB_ODIR_STATE_OPEN:
518*9635SJoyce.McIntosh@Sun.COM 	case SMB_ODIR_STATE_CLOSED:
519*9635SJoyce.McIntosh@Sun.COM 	default:
5208670SJose.Borrego@Sun.COM 		mutex_exit(&od->d_mutex);
5218670SJose.Borrego@Sun.COM 		return (-1);
5228670SJose.Borrego@Sun.COM 	}
5238670SJose.Borrego@Sun.COM 
5249231SAfshin.Ardakani@Sun.COM 	ignore_case = (od->d_flags & SMB_ODIR_FLAG_IGNORE_CASE);
5259231SAfshin.Ardakani@Sun.COM 
5268670SJose.Borrego@Sun.COM 	for (;;) {
5278670SJose.Borrego@Sun.COM 		if ((rc = smb_odir_next_odirent(od, odirent)) != 0)
5285331Samw 			break;
5298670SJose.Borrego@Sun.COM 		if (smb_match_name(odirent->od_ino, odirent->od_name,
5309231SAfshin.Ardakani@Sun.COM 		    od->d_pattern, ignore_case))
5318670SJose.Borrego@Sun.COM 			break;
5325331Samw 	}
5338670SJose.Borrego@Sun.COM 
5348670SJose.Borrego@Sun.COM 	mutex_exit(&od->d_mutex);
5358670SJose.Borrego@Sun.COM 
5368670SJose.Borrego@Sun.COM 	switch (rc) {
5378670SJose.Borrego@Sun.COM 	case 0:
5388670SJose.Borrego@Sun.COM 		*eof = B_FALSE;
5398670SJose.Borrego@Sun.COM 		return (0);
5408670SJose.Borrego@Sun.COM 	case ENOENT:
5418670SJose.Borrego@Sun.COM 		*eof = B_TRUE;
5428670SJose.Borrego@Sun.COM 		return (0);
5438670SJose.Borrego@Sun.COM 	default:
5448670SJose.Borrego@Sun.COM 		smbsr_errno(sr, rc);
5458670SJose.Borrego@Sun.COM 		return (-1);
5468670SJose.Borrego@Sun.COM 	}
5475331Samw }
5485331Samw 
5498670SJose.Borrego@Sun.COM /*
5508670SJose.Borrego@Sun.COM  * smb_odir_read_fileinfo
5518670SJose.Borrego@Sun.COM  *
5528670SJose.Borrego@Sun.COM  * Find the next directory entry matching the search pattern
5538670SJose.Borrego@Sun.COM  * and attributes: od->d_pattern and od->d_sattr.
5548670SJose.Borrego@Sun.COM  *
5558670SJose.Borrego@Sun.COM  * If the search pattern specifies a single filename call
5568670SJose.Borrego@Sun.COM  * smb_odir_single_fileinfo to get the file attributes and
5578670SJose.Borrego@Sun.COM  * populate the caller's smb_fileinfo_t.
5588670SJose.Borrego@Sun.COM  *
5598670SJose.Borrego@Sun.COM  * If the search pattern contains wildcards call smb_odir_next_odirent
5608670SJose.Borrego@Sun.COM  * to get the next directory entry then. Repeat until a matching
5618670SJose.Borrego@Sun.COM  * filename is found. Call smb_odir_wildcard_fileinfo to get the
5628670SJose.Borrego@Sun.COM  * file attributes and populate the caller's smb_fileinfo_t.
5638670SJose.Borrego@Sun.COM  * This is repeated until a file matching the search criteria is found.
5648670SJose.Borrego@Sun.COM  *
5658670SJose.Borrego@Sun.COM  * Returns:
5668670SJose.Borrego@Sun.COM  *  0 - success.
5678670SJose.Borrego@Sun.COM  *      - If a matching entry was found eof will be B_FALSE and
5688670SJose.Borrego@Sun.COM  *        fileinfo will be populated.
5698670SJose.Borrego@Sun.COM  *      - If there are no matching entries eof will be B_TRUE.
5708670SJose.Borrego@Sun.COM  * -1 - error, error details set in sr.
5718670SJose.Borrego@Sun.COM  */
5728670SJose.Borrego@Sun.COM int
5738670SJose.Borrego@Sun.COM smb_odir_read_fileinfo(smb_request_t *sr, smb_odir_t *od,
5748670SJose.Borrego@Sun.COM     smb_fileinfo_t *fileinfo, boolean_t *eof)
5758670SJose.Borrego@Sun.COM {
5768670SJose.Borrego@Sun.COM 	int		rc;
5778670SJose.Borrego@Sun.COM 	smb_odirent_t	*odirent;
5789231SAfshin.Ardakani@Sun.COM 	boolean_t	ignore_case;
5798670SJose.Borrego@Sun.COM 
5808670SJose.Borrego@Sun.COM 	ASSERT(sr);
5818670SJose.Borrego@Sun.COM 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
5828670SJose.Borrego@Sun.COM 	ASSERT(od);
5838670SJose.Borrego@Sun.COM 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
5848670SJose.Borrego@Sun.COM 	ASSERT(fileinfo);
5858670SJose.Borrego@Sun.COM 
5868670SJose.Borrego@Sun.COM 	mutex_enter(&od->d_mutex);
587*9635SJoyce.McIntosh@Sun.COM 	ASSERT(od->d_refcnt > 0);
5888670SJose.Borrego@Sun.COM 
589*9635SJoyce.McIntosh@Sun.COM 	switch (od->d_state) {
590*9635SJoyce.McIntosh@Sun.COM 	case SMB_ODIR_STATE_IN_USE:
591*9635SJoyce.McIntosh@Sun.COM 	case SMB_ODIR_STATE_CLOSING:
592*9635SJoyce.McIntosh@Sun.COM 		break;
593*9635SJoyce.McIntosh@Sun.COM 	case SMB_ODIR_STATE_OPEN:
594*9635SJoyce.McIntosh@Sun.COM 	case SMB_ODIR_STATE_CLOSED:
595*9635SJoyce.McIntosh@Sun.COM 	default:
5968670SJose.Borrego@Sun.COM 		mutex_exit(&od->d_mutex);
5978670SJose.Borrego@Sun.COM 		return (-1);
5988670SJose.Borrego@Sun.COM 	}
5998670SJose.Borrego@Sun.COM 
6009231SAfshin.Ardakani@Sun.COM 	ignore_case = (od->d_flags & SMB_ODIR_FLAG_IGNORE_CASE);
6019231SAfshin.Ardakani@Sun.COM 
6029231SAfshin.Ardakani@Sun.COM 	if (!(od->d_flags & SMB_ODIR_FLAG_WILDCARDS)) {
6038670SJose.Borrego@Sun.COM 		if (od->d_eof)
6048670SJose.Borrego@Sun.COM 			rc = ENOENT;
6058670SJose.Borrego@Sun.COM 		else
6068670SJose.Borrego@Sun.COM 			rc = smb_odir_single_fileinfo(sr, od, fileinfo);
6078670SJose.Borrego@Sun.COM 		od->d_eof = B_TRUE;
6088670SJose.Borrego@Sun.COM 	} else {
6098670SJose.Borrego@Sun.COM 		odirent = kmem_alloc(sizeof (smb_odirent_t), KM_SLEEP);
6108670SJose.Borrego@Sun.COM 		for (;;) {
6118670SJose.Borrego@Sun.COM 			bzero(fileinfo, sizeof (smb_fileinfo_t));
6128670SJose.Borrego@Sun.COM 			if ((rc = smb_odir_next_odirent(od, odirent)) != 0)
6138670SJose.Borrego@Sun.COM 				break;
6148670SJose.Borrego@Sun.COM 
6158670SJose.Borrego@Sun.COM 			if (!smb_match_name(odirent->od_ino, odirent->od_name,
6169231SAfshin.Ardakani@Sun.COM 			    od->d_pattern, ignore_case))
6178670SJose.Borrego@Sun.COM 				continue;
6188670SJose.Borrego@Sun.COM 
6198670SJose.Borrego@Sun.COM 			rc = smb_odir_wildcard_fileinfo(sr, od, odirent,
6208670SJose.Borrego@Sun.COM 			    fileinfo);
6218670SJose.Borrego@Sun.COM 			if (rc == 0)
6228670SJose.Borrego@Sun.COM 				break;
6238670SJose.Borrego@Sun.COM 		}
6248670SJose.Borrego@Sun.COM 		kmem_free(odirent, sizeof (smb_odirent_t));
6258670SJose.Borrego@Sun.COM 	}
6268670SJose.Borrego@Sun.COM 	mutex_exit(&od->d_mutex);
6278670SJose.Borrego@Sun.COM 
6288670SJose.Borrego@Sun.COM 	switch (rc) {
6298670SJose.Borrego@Sun.COM 	case 0:
6308670SJose.Borrego@Sun.COM 		*eof = B_FALSE;
6318670SJose.Borrego@Sun.COM 		return (0);
6328670SJose.Borrego@Sun.COM 	case ENOENT:
6338670SJose.Borrego@Sun.COM 		*eof = B_TRUE;
6348670SJose.Borrego@Sun.COM 		return (0);
6358670SJose.Borrego@Sun.COM 	default:
6368670SJose.Borrego@Sun.COM 		smbsr_errno(sr, rc);
6378670SJose.Borrego@Sun.COM 		return (-1);
6388670SJose.Borrego@Sun.COM 	}
6398670SJose.Borrego@Sun.COM }
6408670SJose.Borrego@Sun.COM 
6415331Samw 
6425331Samw /*
6438670SJose.Borrego@Sun.COM  * smb_odir_read_streaminfo
6448670SJose.Borrego@Sun.COM  *
6458670SJose.Borrego@Sun.COM  * Find the next directory entry whose name begins with SMB_STREAM_PREFIX,
6468670SJose.Borrego@Sun.COM  * and thus represents an NTFS named stream.
6478670SJose.Borrego@Sun.COM  * No search attribute matching is performed.
6489231SAfshin.Ardakani@Sun.COM  * No case conflict name mangling is required for NTFS named stream names.
6495331Samw  *
6508670SJose.Borrego@Sun.COM  * Returns:
6518670SJose.Borrego@Sun.COM  *  0 - success.
6528670SJose.Borrego@Sun.COM  *      - If a matching entry was found eof will be B_FALSE and
6538670SJose.Borrego@Sun.COM  *        sinfo will be populated.
6548670SJose.Borrego@Sun.COM  *      - If there are no matching entries eof will be B_TRUE.
6558670SJose.Borrego@Sun.COM  * -1 - error, error details set in sr.
6565331Samw  */
6578670SJose.Borrego@Sun.COM int
6588670SJose.Borrego@Sun.COM smb_odir_read_streaminfo(smb_request_t *sr, smb_odir_t *od,
6598670SJose.Borrego@Sun.COM     smb_streaminfo_t *sinfo, boolean_t *eof)
6605331Samw {
6618670SJose.Borrego@Sun.COM 	int		rc;
6628670SJose.Borrego@Sun.COM 	smb_odirent_t	*odirent;
6638670SJose.Borrego@Sun.COM 	vnode_t		*vp;
6648670SJose.Borrego@Sun.COM 	smb_attr_t	attr;
6659231SAfshin.Ardakani@Sun.COM 	int		tmpflg;
6665331Samw 
6678670SJose.Borrego@Sun.COM 	ASSERT(sr);
6688670SJose.Borrego@Sun.COM 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
6695331Samw 	ASSERT(od);
6705331Samw 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
6718670SJose.Borrego@Sun.COM 	ASSERT(sinfo);
6728670SJose.Borrego@Sun.COM 
6738670SJose.Borrego@Sun.COM 	mutex_enter(&od->d_mutex);
674*9635SJoyce.McIntosh@Sun.COM 	ASSERT(od->d_refcnt > 0);
6758670SJose.Borrego@Sun.COM 
676*9635SJoyce.McIntosh@Sun.COM 	switch (od->d_state) {
677*9635SJoyce.McIntosh@Sun.COM 	case SMB_ODIR_STATE_IN_USE:
678*9635SJoyce.McIntosh@Sun.COM 	case SMB_ODIR_STATE_CLOSING:
679*9635SJoyce.McIntosh@Sun.COM 		break;
680*9635SJoyce.McIntosh@Sun.COM 	case SMB_ODIR_STATE_OPEN:
681*9635SJoyce.McIntosh@Sun.COM 	case SMB_ODIR_STATE_CLOSED:
682*9635SJoyce.McIntosh@Sun.COM 	default:
6838670SJose.Borrego@Sun.COM 		mutex_exit(&od->d_mutex);
6848670SJose.Borrego@Sun.COM 		return (-1);
6858670SJose.Borrego@Sun.COM 	}
6868670SJose.Borrego@Sun.COM 
6878670SJose.Borrego@Sun.COM 	/* Check that odir represents an xattr directory */
6889231SAfshin.Ardakani@Sun.COM 	if (!(od->d_flags & SMB_ODIR_FLAG_XATTR)) {
6898670SJose.Borrego@Sun.COM 		*eof = B_TRUE;
6908670SJose.Borrego@Sun.COM 		mutex_exit(&od->d_mutex);
6918670SJose.Borrego@Sun.COM 		return (0);
6928670SJose.Borrego@Sun.COM 	}
6938670SJose.Borrego@Sun.COM 
6948670SJose.Borrego@Sun.COM 	odirent = kmem_alloc(sizeof (smb_odirent_t), KM_SLEEP);
6958670SJose.Borrego@Sun.COM 
6968670SJose.Borrego@Sun.COM 	for (;;) {
6978670SJose.Borrego@Sun.COM 		bzero(sinfo, sizeof (smb_streaminfo_t));
6988670SJose.Borrego@Sun.COM 		if ((rc = smb_odir_next_odirent(od, odirent)) != 0)
6998670SJose.Borrego@Sun.COM 			break;
7008670SJose.Borrego@Sun.COM 
7018670SJose.Borrego@Sun.COM 		if (strncmp(odirent->od_name, SMB_STREAM_PREFIX,
7028670SJose.Borrego@Sun.COM 		    SMB_STREAM_PREFIX_LEN)) {
7038670SJose.Borrego@Sun.COM 			continue;
7048670SJose.Borrego@Sun.COM 		}
7058670SJose.Borrego@Sun.COM 
7068670SJose.Borrego@Sun.COM 		/*
7078670SJose.Borrego@Sun.COM 		 * since we only care about the size attribute we don't need to
7088670SJose.Borrego@Sun.COM 		 * pass the vp of the unnamed stream file to smb_vop_getattr
7098670SJose.Borrego@Sun.COM 		 */
7108670SJose.Borrego@Sun.COM 		rc = smb_vop_lookup(od->d_dnode->vp, odirent->od_name, &vp,
7119343SAfshin.Ardakani@Sun.COM 		    NULL, 0, &tmpflg, od->d_tree->t_snode->vp, od->d_cred);
7128670SJose.Borrego@Sun.COM 		if (rc == 0) {
7139343SAfshin.Ardakani@Sun.COM 			rc = smb_vop_getattr(vp, NULL, &attr, 0, od->d_cred);
7148670SJose.Borrego@Sun.COM 			VN_RELE(vp);
7158670SJose.Borrego@Sun.COM 		}
7168670SJose.Borrego@Sun.COM 
7178670SJose.Borrego@Sun.COM 		if (rc == 0) {
7188670SJose.Borrego@Sun.COM 			(void) strlcpy(sinfo->si_name,
7198670SJose.Borrego@Sun.COM 			    odirent->od_name + SMB_STREAM_PREFIX_LEN,
7208670SJose.Borrego@Sun.COM 			    sizeof (sinfo->si_name));
7218670SJose.Borrego@Sun.COM 			sinfo->si_size = attr.sa_vattr.va_size;
7228670SJose.Borrego@Sun.COM 			break;
7238670SJose.Borrego@Sun.COM 		}
7248670SJose.Borrego@Sun.COM 	}
7258670SJose.Borrego@Sun.COM 	mutex_exit(&od->d_mutex);
7268670SJose.Borrego@Sun.COM 
7278670SJose.Borrego@Sun.COM 	kmem_free(odirent, sizeof (smb_odirent_t));
7288670SJose.Borrego@Sun.COM 
7298670SJose.Borrego@Sun.COM 	switch (rc) {
7308670SJose.Borrego@Sun.COM 	case 0:
7318670SJose.Borrego@Sun.COM 		*eof = B_FALSE;
7328670SJose.Borrego@Sun.COM 		return (0);
7338670SJose.Borrego@Sun.COM 	case ENOENT:
7348670SJose.Borrego@Sun.COM 		*eof = B_TRUE;
7358670SJose.Borrego@Sun.COM 		return (0);
7368670SJose.Borrego@Sun.COM 	default:
7378670SJose.Borrego@Sun.COM 		smbsr_errno(sr, rc);
7388670SJose.Borrego@Sun.COM 		return (-1);
7398670SJose.Borrego@Sun.COM 	}
7408670SJose.Borrego@Sun.COM }
7418670SJose.Borrego@Sun.COM 
7428670SJose.Borrego@Sun.COM /*
7438670SJose.Borrego@Sun.COM  * smb_odir_save_cookie
7448670SJose.Borrego@Sun.COM  *
7458670SJose.Borrego@Sun.COM  * Callers can save up to SMB_MAX_SEARCH cookies in the odir
7468670SJose.Borrego@Sun.COM  * to be used as resume points for a 'find next' request.
7478670SJose.Borrego@Sun.COM  */
7488670SJose.Borrego@Sun.COM void
7498670SJose.Borrego@Sun.COM smb_odir_save_cookie(smb_odir_t *od, int idx, uint32_t cookie)
7508670SJose.Borrego@Sun.COM {
7518670SJose.Borrego@Sun.COM 	ASSERT(od);
7528670SJose.Borrego@Sun.COM 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
7538670SJose.Borrego@Sun.COM 	ASSERT(idx >= 0 && idx < SMB_MAX_SEARCH);
7545331Samw 
7555331Samw 	mutex_enter(&od->d_mutex);
7568670SJose.Borrego@Sun.COM 	od->d_cookies[idx] = cookie;
7578670SJose.Borrego@Sun.COM 	mutex_exit(&od->d_mutex);
7588670SJose.Borrego@Sun.COM }
7598670SJose.Borrego@Sun.COM 
7608670SJose.Borrego@Sun.COM /*
7618670SJose.Borrego@Sun.COM  * smb_odir_resume_at
7628670SJose.Borrego@Sun.COM  *
7639343SAfshin.Ardakani@Sun.COM  * If SMB_ODIR_FLAG_WILDCARDS is not set the search is for a single
7649343SAfshin.Ardakani@Sun.COM  * file and should not be resumed.
7659343SAfshin.Ardakani@Sun.COM  *
7669343SAfshin.Ardakani@Sun.COM  * Wildcard searching can be resumed from:
7678670SJose.Borrego@Sun.COM  * - the cookie saved at a specified index (SMBsearch, SMBfind).
7688670SJose.Borrego@Sun.COM  * - a specified cookie (SMB_trans2_find)
7698670SJose.Borrego@Sun.COM  * - a specified filename (SMB_trans2_find) - NOT SUPPORTED.
7708670SJose.Borrego@Sun.COM  *   Defaults to continuing from where the last search ended.
7718670SJose.Borrego@Sun.COM  *
7728670SJose.Borrego@Sun.COM  * Continuation from where the last search ended (SMB_trans2_find)
7738670SJose.Borrego@Sun.COM  * is implemented by saving the last cookie at a specific index (0)
7748670SJose.Borrego@Sun.COM  * smb_odir_resume_at indicates a new request, so reset od->d_bufptr
7758670SJose.Borrego@Sun.COM  * and d_eof to force a vop_readdir.
7768670SJose.Borrego@Sun.COM  */
7778670SJose.Borrego@Sun.COM void
7788670SJose.Borrego@Sun.COM smb_odir_resume_at(smb_odir_t *od, smb_odir_resume_t *resume)
7798670SJose.Borrego@Sun.COM {
7808670SJose.Borrego@Sun.COM 	ASSERT(od);
7818670SJose.Borrego@Sun.COM 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
7828670SJose.Borrego@Sun.COM 	ASSERT(resume);
7838670SJose.Borrego@Sun.COM 
7848670SJose.Borrego@Sun.COM 	mutex_enter(&od->d_mutex);
7858670SJose.Borrego@Sun.COM 
7869343SAfshin.Ardakani@Sun.COM 	if ((od->d_flags & SMB_ODIR_FLAG_WILDCARDS) == 0) {
7879343SAfshin.Ardakani@Sun.COM 		od->d_eof = B_TRUE;
7889343SAfshin.Ardakani@Sun.COM 		mutex_exit(&od->d_mutex);
7899343SAfshin.Ardakani@Sun.COM 		return;
7909343SAfshin.Ardakani@Sun.COM 	}
7919343SAfshin.Ardakani@Sun.COM 
7928670SJose.Borrego@Sun.COM 	switch (resume->or_type) {
7938670SJose.Borrego@Sun.COM 		case SMB_ODIR_RESUME_IDX:
7948670SJose.Borrego@Sun.COM 			ASSERT(resume->or_idx >= 0);
7958670SJose.Borrego@Sun.COM 			ASSERT(resume->or_idx < SMB_MAX_SEARCH);
7968670SJose.Borrego@Sun.COM 
7978670SJose.Borrego@Sun.COM 			if ((resume->or_idx < 0) ||
7988670SJose.Borrego@Sun.COM 			    (resume->or_idx >= SMB_MAX_SEARCH)) {
7998670SJose.Borrego@Sun.COM 				resume->or_idx = 0;
8008670SJose.Borrego@Sun.COM 			}
8018670SJose.Borrego@Sun.COM 			od->d_offset = od->d_cookies[resume->or_idx];
8028670SJose.Borrego@Sun.COM 			break;
8038670SJose.Borrego@Sun.COM 		case SMB_ODIR_RESUME_COOKIE:
8048670SJose.Borrego@Sun.COM 			od->d_offset = resume->or_cookie;
8058670SJose.Borrego@Sun.COM 			break;
8068670SJose.Borrego@Sun.COM 		case SMB_ODIR_RESUME_FNAME:
8078670SJose.Borrego@Sun.COM 		default:
8088670SJose.Borrego@Sun.COM 			od->d_offset = od->d_cookies[0];
8098670SJose.Borrego@Sun.COM 			break;
8105331Samw 	}
8118670SJose.Borrego@Sun.COM 
8128670SJose.Borrego@Sun.COM 	/* Force a vop_readdir to refresh d_buf */
8138670SJose.Borrego@Sun.COM 	od->d_bufptr = NULL;
8148670SJose.Borrego@Sun.COM 	od->d_eof = B_FALSE;
8158670SJose.Borrego@Sun.COM 
8168670SJose.Borrego@Sun.COM 	mutex_exit(&od->d_mutex);
8178670SJose.Borrego@Sun.COM }
8188670SJose.Borrego@Sun.COM 
8198670SJose.Borrego@Sun.COM 
8208670SJose.Borrego@Sun.COM /* *** static functions *** */
8218670SJose.Borrego@Sun.COM 
8228670SJose.Borrego@Sun.COM /*
8238670SJose.Borrego@Sun.COM  * smb_odir_create
8248670SJose.Borrego@Sun.COM  * Allocate and populate an odir obect and add it to the tree's list.
8258670SJose.Borrego@Sun.COM  */
8268670SJose.Borrego@Sun.COM static smb_odir_t *
8278670SJose.Borrego@Sun.COM smb_odir_create(smb_request_t *sr, smb_node_t *dnode,
8289343SAfshin.Ardakani@Sun.COM     char *pattern, uint16_t sattr, cred_t *cr)
8298670SJose.Borrego@Sun.COM {
8308670SJose.Borrego@Sun.COM 	smb_odir_t	*od;
8318670SJose.Borrego@Sun.COM 	smb_tree_t	*tree;
8328670SJose.Borrego@Sun.COM 	uint16_t	odid;
8338670SJose.Borrego@Sun.COM 
8348670SJose.Borrego@Sun.COM 	ASSERT(sr);
8358670SJose.Borrego@Sun.COM 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
8368670SJose.Borrego@Sun.COM 	ASSERT(sr->tid_tree);
8378670SJose.Borrego@Sun.COM 	ASSERT(sr->tid_tree->t_magic == SMB_TREE_MAGIC);
8388670SJose.Borrego@Sun.COM 	ASSERT(dnode);
8398670SJose.Borrego@Sun.COM 	ASSERT(dnode->n_magic == SMB_NODE_MAGIC);
8408670SJose.Borrego@Sun.COM 
8418670SJose.Borrego@Sun.COM 	tree = sr->tid_tree;
8428670SJose.Borrego@Sun.COM 
8438670SJose.Borrego@Sun.COM 	if (smb_idpool_alloc(&tree->t_odid_pool, &odid)) {
8448670SJose.Borrego@Sun.COM 		smbsr_error(sr, NT_STATUS_TOO_MANY_OPENED_FILES,
8458670SJose.Borrego@Sun.COM 		    ERRDOS, ERROR_TOO_MANY_OPEN_FILES);
8468670SJose.Borrego@Sun.COM 		return (NULL);
8478670SJose.Borrego@Sun.COM 	}
8488670SJose.Borrego@Sun.COM 
8498670SJose.Borrego@Sun.COM 	od = kmem_cache_alloc(tree->t_server->si_cache_odir, KM_SLEEP);
8508670SJose.Borrego@Sun.COM 	bzero(od, sizeof (smb_odir_t));
8518670SJose.Borrego@Sun.COM 
8528670SJose.Borrego@Sun.COM 	mutex_init(&od->d_mutex, NULL, MUTEX_DEFAULT, NULL);
853*9635SJoyce.McIntosh@Sun.COM 	od->d_refcnt = 0;
8548670SJose.Borrego@Sun.COM 	od->d_state = SMB_ODIR_STATE_OPEN;
8558670SJose.Borrego@Sun.COM 	od->d_magic = SMB_ODIR_MAGIC;
8568670SJose.Borrego@Sun.COM 	od->d_opened_by_pid = sr->smb_pid;
8578670SJose.Borrego@Sun.COM 	od->d_session = tree->t_session;
8589343SAfshin.Ardakani@Sun.COM 	od->d_cred = cr;
8598670SJose.Borrego@Sun.COM 	od->d_tree = tree;
8608670SJose.Borrego@Sun.COM 	od->d_dnode = dnode;
8618670SJose.Borrego@Sun.COM 	smb_node_ref(dnode);
8628670SJose.Borrego@Sun.COM 	od->d_odid = odid;
8638670SJose.Borrego@Sun.COM 	od->d_sattr = sattr;
8648670SJose.Borrego@Sun.COM 	(void) strlcpy(od->d_pattern, pattern, sizeof (od->d_pattern));
8659231SAfshin.Ardakani@Sun.COM 	od->d_flags = 0;
8669231SAfshin.Ardakani@Sun.COM 	if (smb_convert_wildcards(od->d_pattern) != 0)
8679231SAfshin.Ardakani@Sun.COM 		od->d_flags |= SMB_ODIR_FLAG_WILDCARDS;
8689231SAfshin.Ardakani@Sun.COM 	if (vfs_has_feature(dnode->vp->v_vfsp, VFSFT_DIRENTFLAGS))
8699231SAfshin.Ardakani@Sun.COM 		od->d_flags |= SMB_ODIR_FLAG_EDIRENT;
8709231SAfshin.Ardakani@Sun.COM 	if (smb_tree_has_feature(tree, SMB_TREE_CASEINSENSITIVE))
8719231SAfshin.Ardakani@Sun.COM 		od->d_flags |= SMB_ODIR_FLAG_IGNORE_CASE;
8729231SAfshin.Ardakani@Sun.COM 	if (SMB_TREE_SUPPORTS_CATIA(sr))
8739231SAfshin.Ardakani@Sun.COM 		od->d_flags |= SMB_ODIR_FLAG_CATIA;
8748670SJose.Borrego@Sun.COM 	od->d_eof = B_FALSE;
8758670SJose.Borrego@Sun.COM 
8768670SJose.Borrego@Sun.COM 	smb_llist_enter(&tree->t_odir_list, RW_WRITER);
8778670SJose.Borrego@Sun.COM 	smb_llist_insert_tail(&tree->t_odir_list, od);
8788670SJose.Borrego@Sun.COM 	smb_llist_exit(&tree->t_odir_list);
8798670SJose.Borrego@Sun.COM 
8808670SJose.Borrego@Sun.COM 	atomic_inc_32(&tree->t_session->s_dir_cnt);
8818670SJose.Borrego@Sun.COM 	return (od);
8825331Samw }
8835331Samw 
8845331Samw /*
8855331Samw  * smb_odir_delete
8868670SJose.Borrego@Sun.COM  *
8878670SJose.Borrego@Sun.COM  * Removal of the odir from the tree's list of odirs must be
8888670SJose.Borrego@Sun.COM  * done before any resources associated with the odir are
8898670SJose.Borrego@Sun.COM  * released.
8905331Samw  */
8915331Samw static void
8928670SJose.Borrego@Sun.COM smb_odir_delete(smb_odir_t *od)
8935331Samw {
8945331Samw 	ASSERT(od);
8955331Samw 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
8965331Samw 	ASSERT(od->d_state == SMB_ODIR_STATE_CLOSED);
8975331Samw 
8985331Samw 	smb_llist_enter(&od->d_tree->t_odir_list, RW_WRITER);
8995331Samw 	smb_llist_remove(&od->d_tree->t_odir_list, od);
9005331Samw 	smb_llist_exit(&od->d_tree->t_odir_list);
9018670SJose.Borrego@Sun.COM 
9026139Sjb150015 	od->d_magic = 0;
9035331Samw 	atomic_dec_32(&od->d_tree->t_session->s_dir_cnt);
9048670SJose.Borrego@Sun.COM 	smb_node_release(od->d_dnode);
9058670SJose.Borrego@Sun.COM 	smb_idpool_free(&od->d_tree->t_odid_pool, od->d_odid);
9065331Samw 	mutex_destroy(&od->d_mutex);
9076139Sjb150015 	kmem_cache_free(od->d_tree->t_server->si_cache_odir, od);
9085331Samw }
9098670SJose.Borrego@Sun.COM 
9108670SJose.Borrego@Sun.COM /*
9118670SJose.Borrego@Sun.COM  * smb_odir_next_odirent
9128670SJose.Borrego@Sun.COM  *
9138670SJose.Borrego@Sun.COM  * Find the next directory entry in d_buf. If d_bufptr is NULL (buffer
9148670SJose.Borrego@Sun.COM  * is empty or we've reached the end of it), read the next set of
9158670SJose.Borrego@Sun.COM  * entries from the file system (vop_readdir).
9168670SJose.Borrego@Sun.COM  *
9178670SJose.Borrego@Sun.COM  * File systems which support VFSFT_EDIRENT_FLAGS will return the
9188670SJose.Borrego@Sun.COM  * directory entries as a buffer of edirent_t structure. Others will
9198670SJose.Borrego@Sun.COM  * return a buffer of dirent64_t structures.  For simplicity translate
9208670SJose.Borrego@Sun.COM  * the data into an smb_odirent_t structure.
9218670SJose.Borrego@Sun.COM  * The ed_name/d_name in d_buf is NULL terminated by the file system.
9228670SJose.Borrego@Sun.COM  *
9238670SJose.Borrego@Sun.COM  * Some file systems can have directories larger than SMB_MAXDIRSIZE.
9249231SAfshin.Ardakani@Sun.COM  * If the odirent offset >= SMB_MAXDIRSIZE return ENOENT and set d_eof
9259231SAfshin.Ardakani@Sun.COM  * to true to stop subsequent calls to smb_vop_readdir.
9268670SJose.Borrego@Sun.COM  *
9278670SJose.Borrego@Sun.COM  * Returns:
9288670SJose.Borrego@Sun.COM  *      0 - success. odirent is populated with the next directory entry
9298670SJose.Borrego@Sun.COM  * ENOENT - no more directory entries
9308670SJose.Borrego@Sun.COM  *  errno - error
9318670SJose.Borrego@Sun.COM  */
9328670SJose.Borrego@Sun.COM static int
9338670SJose.Borrego@Sun.COM smb_odir_next_odirent(smb_odir_t *od, smb_odirent_t *odirent)
9348670SJose.Borrego@Sun.COM {
9358670SJose.Borrego@Sun.COM 	int		rc;
9368670SJose.Borrego@Sun.COM 	int		reclen;
9378670SJose.Borrego@Sun.COM 	int		eof;
9388670SJose.Borrego@Sun.COM 	dirent64_t	*dp;
9398670SJose.Borrego@Sun.COM 	edirent_t	*edp;
9409231SAfshin.Ardakani@Sun.COM 	char		*np;
9418670SJose.Borrego@Sun.COM 
9428670SJose.Borrego@Sun.COM 	ASSERT(MUTEX_HELD(&od->d_mutex));
9438670SJose.Borrego@Sun.COM 
9449437SJoyce.McIntosh@Sun.COM 	bzero(odirent, sizeof (smb_odirent_t));
9459437SJoyce.McIntosh@Sun.COM 
9468670SJose.Borrego@Sun.COM 	if (od->d_bufptr != NULL) {
9479231SAfshin.Ardakani@Sun.COM 		if (od->d_flags & SMB_ODIR_FLAG_EDIRENT)
9489231SAfshin.Ardakani@Sun.COM 			reclen = od->d_edp->ed_reclen;
9499231SAfshin.Ardakani@Sun.COM 		else
9509231SAfshin.Ardakani@Sun.COM 			reclen = od->d_dp->d_reclen;
9518670SJose.Borrego@Sun.COM 
9528670SJose.Borrego@Sun.COM 		if (reclen == 0) {
9538670SJose.Borrego@Sun.COM 			od->d_bufptr = NULL;
9548670SJose.Borrego@Sun.COM 		} else {
9558670SJose.Borrego@Sun.COM 			od->d_bufptr += reclen;
9568670SJose.Borrego@Sun.COM 			if (od->d_bufptr >= od->d_buf + od->d_bufsize)
9578670SJose.Borrego@Sun.COM 				od->d_bufptr = NULL;
9588670SJose.Borrego@Sun.COM 		}
9598670SJose.Borrego@Sun.COM 	}
9608670SJose.Borrego@Sun.COM 
9618670SJose.Borrego@Sun.COM 	if (od->d_bufptr == NULL) {
9628670SJose.Borrego@Sun.COM 		if (od->d_eof)
9638670SJose.Borrego@Sun.COM 			return (ENOENT);
9648670SJose.Borrego@Sun.COM 
9658670SJose.Borrego@Sun.COM 		od->d_bufsize = sizeof (od->d_buf);
9668670SJose.Borrego@Sun.COM 
9678670SJose.Borrego@Sun.COM 		rc = smb_vop_readdir(od->d_dnode->vp, od->d_offset,
9689343SAfshin.Ardakani@Sun.COM 		    od->d_buf, &od->d_bufsize, &eof, od->d_cred);
9698670SJose.Borrego@Sun.COM 
9708670SJose.Borrego@Sun.COM 		if ((rc == 0) && (od->d_bufsize == 0))
9718670SJose.Borrego@Sun.COM 			rc = ENOENT;
9728670SJose.Borrego@Sun.COM 
9738670SJose.Borrego@Sun.COM 		if (rc != 0) {
9748670SJose.Borrego@Sun.COM 			od->d_bufptr = NULL;
9758670SJose.Borrego@Sun.COM 			od->d_bufsize = 0;
9768670SJose.Borrego@Sun.COM 			return (rc);
9778670SJose.Borrego@Sun.COM 		}
9788670SJose.Borrego@Sun.COM 
9798670SJose.Borrego@Sun.COM 		od->d_eof = (eof != 0);
9808670SJose.Borrego@Sun.COM 		od->d_bufptr = od->d_buf;
9818670SJose.Borrego@Sun.COM 	}
9828670SJose.Borrego@Sun.COM 
9839231SAfshin.Ardakani@Sun.COM 	if (od->d_flags & SMB_ODIR_FLAG_EDIRENT)
9849231SAfshin.Ardakani@Sun.COM 		od->d_offset = od->d_edp->ed_off;
9859231SAfshin.Ardakani@Sun.COM 	else
9869231SAfshin.Ardakani@Sun.COM 		od->d_offset = od->d_dp->d_off;
9879231SAfshin.Ardakani@Sun.COM 
9888670SJose.Borrego@Sun.COM 	if (od->d_offset >= SMB_MAXDIRSIZE) {
9898670SJose.Borrego@Sun.COM 		od->d_bufptr = NULL;
9908670SJose.Borrego@Sun.COM 		od->d_bufsize = 0;
9919231SAfshin.Ardakani@Sun.COM 		od->d_eof = B_TRUE;
9928670SJose.Borrego@Sun.COM 		return (ENOENT);
9938670SJose.Borrego@Sun.COM 	}
9948670SJose.Borrego@Sun.COM 
9959231SAfshin.Ardakani@Sun.COM 	if (od->d_flags & SMB_ODIR_FLAG_EDIRENT) {
9968670SJose.Borrego@Sun.COM 		edp = od->d_edp;
9978670SJose.Borrego@Sun.COM 		odirent->od_ino = edp->ed_ino;
9988670SJose.Borrego@Sun.COM 		odirent->od_eflags = edp->ed_eflags;
9999231SAfshin.Ardakani@Sun.COM 		np = edp->ed_name;
10008670SJose.Borrego@Sun.COM 	} else {
10018670SJose.Borrego@Sun.COM 		dp = od->d_dp;
10028670SJose.Borrego@Sun.COM 		odirent->od_ino = dp->d_ino;
10038670SJose.Borrego@Sun.COM 		odirent->od_eflags = 0;
10049231SAfshin.Ardakani@Sun.COM 		np =  dp->d_name;
10059231SAfshin.Ardakani@Sun.COM 	}
10069231SAfshin.Ardakani@Sun.COM 
10079231SAfshin.Ardakani@Sun.COM 	if ((od->d_flags & SMB_ODIR_FLAG_CATIA) &&
10089231SAfshin.Ardakani@Sun.COM 	    ((od->d_flags & SMB_ODIR_FLAG_XATTR) == 0)) {
10099231SAfshin.Ardakani@Sun.COM 		smb_vop_catia_v4tov5(np, odirent->od_name,
10109231SAfshin.Ardakani@Sun.COM 		    sizeof (odirent->od_name));
10119231SAfshin.Ardakani@Sun.COM 	} else {
10129231SAfshin.Ardakani@Sun.COM 		(void) strlcpy(odirent->od_name, np,
10138670SJose.Borrego@Sun.COM 		    sizeof (odirent->od_name));
10148670SJose.Borrego@Sun.COM 	}
10158670SJose.Borrego@Sun.COM 
10168670SJose.Borrego@Sun.COM 	return (0);
10178670SJose.Borrego@Sun.COM }
10188670SJose.Borrego@Sun.COM 
10198670SJose.Borrego@Sun.COM /*
10208670SJose.Borrego@Sun.COM  * smb_odir_single_fileinfo
10218670SJose.Borrego@Sun.COM  *
10228670SJose.Borrego@Sun.COM  * Lookup the file identified by od->d_pattern.
10238670SJose.Borrego@Sun.COM  *
10248670SJose.Borrego@Sun.COM  * If the looked up file is a link, we attempt to lookup the link target
10258670SJose.Borrego@Sun.COM  * to use its attributes in place of those of the files's.
10268670SJose.Borrego@Sun.COM  * If we fail to lookup the target of the link we use the original
10278670SJose.Borrego@Sun.COM  * file's attributes.
10288670SJose.Borrego@Sun.COM  * Check if the attributes match the search attributes.
10298670SJose.Borrego@Sun.COM  *
10308670SJose.Borrego@Sun.COM  * Returns: 0 - success
10318670SJose.Borrego@Sun.COM  *     ENOENT - no match
10328670SJose.Borrego@Sun.COM  *      errno - error
10338670SJose.Borrego@Sun.COM  */
10348670SJose.Borrego@Sun.COM static int
10358670SJose.Borrego@Sun.COM smb_odir_single_fileinfo(smb_request_t *sr, smb_odir_t *od,
10368670SJose.Borrego@Sun.COM     smb_fileinfo_t *fileinfo)
10378670SJose.Borrego@Sun.COM {
10388670SJose.Borrego@Sun.COM 	int		rc;
10398670SJose.Borrego@Sun.COM 	smb_node_t	*fnode, *tgt_node;
10408670SJose.Borrego@Sun.COM 	smb_attr_t	attr, tgt_attr, *fattr;
10418670SJose.Borrego@Sun.COM 	ino64_t		ino;
10428670SJose.Borrego@Sun.COM 	char		*name;
10438670SJose.Borrego@Sun.COM 	uint32_t	dosattr;
10449231SAfshin.Ardakani@Sun.COM 	boolean_t	case_conflict = B_FALSE;
10459231SAfshin.Ardakani@Sun.COM 	int		lookup_flags, flags = 0;
10469231SAfshin.Ardakani@Sun.COM 	vnode_t		*vp;
10478670SJose.Borrego@Sun.COM 
10488670SJose.Borrego@Sun.COM 	ASSERT(sr);
10498670SJose.Borrego@Sun.COM 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
10508670SJose.Borrego@Sun.COM 	ASSERT(od);
10518670SJose.Borrego@Sun.COM 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
10528670SJose.Borrego@Sun.COM 
10538670SJose.Borrego@Sun.COM 	ASSERT(MUTEX_HELD(&od->d_mutex));
10548670SJose.Borrego@Sun.COM 	bzero(fileinfo, sizeof (smb_fileinfo_t));
10558670SJose.Borrego@Sun.COM 
10569343SAfshin.Ardakani@Sun.COM 	rc = smb_fsop_lookup(sr, od->d_cred, 0, od->d_tree->t_snode,
10579231SAfshin.Ardakani@Sun.COM 	    od->d_dnode, od->d_pattern, &fnode, &attr);
10588670SJose.Borrego@Sun.COM 	if (rc != 0)
10598670SJose.Borrego@Sun.COM 		return (rc);
10608670SJose.Borrego@Sun.COM 
10619231SAfshin.Ardakani@Sun.COM 	/*
10629231SAfshin.Ardakani@Sun.COM 	 * If case sensitive, do a case insensitive smb_vop_lookup to
10639231SAfshin.Ardakani@Sun.COM 	 * check for case conflict
10649231SAfshin.Ardakani@Sun.COM 	 */
10659231SAfshin.Ardakani@Sun.COM 	if (od->d_flags & SMB_ODIR_FLAG_IGNORE_CASE) {
10669231SAfshin.Ardakani@Sun.COM 		lookup_flags = SMB_IGNORE_CASE;
10679231SAfshin.Ardakani@Sun.COM 		if (od->d_flags & SMB_ODIR_FLAG_CATIA)
10689231SAfshin.Ardakani@Sun.COM 			lookup_flags |= SMB_CATIA;
10698670SJose.Borrego@Sun.COM 
10709231SAfshin.Ardakani@Sun.COM 		rc = smb_vop_lookup(od->d_dnode->vp, fnode->od_name, &vp,
10719231SAfshin.Ardakani@Sun.COM 		    NULL, lookup_flags, &flags, od->d_tree->t_snode->vp,
10729343SAfshin.Ardakani@Sun.COM 		    od->d_cred);
10739231SAfshin.Ardakani@Sun.COM 		if (rc != 0)
10749231SAfshin.Ardakani@Sun.COM 			return (rc);
10759231SAfshin.Ardakani@Sun.COM 		VN_RELE(vp);
10769231SAfshin.Ardakani@Sun.COM 
10779231SAfshin.Ardakani@Sun.COM 		if (flags & ED_CASE_CONFLICT)
10789231SAfshin.Ardakani@Sun.COM 			case_conflict = B_TRUE;
10799231SAfshin.Ardakani@Sun.COM 	}
10809231SAfshin.Ardakani@Sun.COM 
10819231SAfshin.Ardakani@Sun.COM 	ino = attr.sa_vattr.va_nodeid;
10829231SAfshin.Ardakani@Sun.COM 	(void) smb_mangle_name(ino, fnode->od_name,
10839231SAfshin.Ardakani@Sun.COM 	    fileinfo->fi_shortname, fileinfo->fi_name83, case_conflict);
10849231SAfshin.Ardakani@Sun.COM 	name = (case_conflict) ? fileinfo->fi_shortname : fnode->od_name;
10858670SJose.Borrego@Sun.COM 	(void) strlcpy(fileinfo->fi_name, name, sizeof (fileinfo->fi_name));
10868670SJose.Borrego@Sun.COM 
10878670SJose.Borrego@Sun.COM 	/* follow link to get target node & attr */
10888670SJose.Borrego@Sun.COM 	if ((fnode->vp->v_type == VLNK) &&
10899231SAfshin.Ardakani@Sun.COM 	    (smb_odir_lookup_link(sr, od, fnode->od_name,
10909231SAfshin.Ardakani@Sun.COM 	    &tgt_node, &tgt_attr))) {
10918670SJose.Borrego@Sun.COM 		smb_node_release(fnode);
10928670SJose.Borrego@Sun.COM 		fnode = tgt_node;
10938670SJose.Borrego@Sun.COM 		fattr = &tgt_attr;
10948670SJose.Borrego@Sun.COM 	} else {
10958670SJose.Borrego@Sun.COM 		fattr = &attr;
10968670SJose.Borrego@Sun.COM 	}
10978670SJose.Borrego@Sun.COM 
10988670SJose.Borrego@Sun.COM 	/* check search attributes */
10998670SJose.Borrego@Sun.COM 	dosattr = smb_node_get_dosattr(fnode);
11008934SJose.Borrego@Sun.COM 	if (!smb_sattr_check(dosattr, od->d_sattr)) {
11018670SJose.Borrego@Sun.COM 		smb_node_release(fnode);
11028670SJose.Borrego@Sun.COM 		return (ENOENT);
11038670SJose.Borrego@Sun.COM 	}
11048670SJose.Borrego@Sun.COM 
11058670SJose.Borrego@Sun.COM 	fileinfo->fi_dosattr = dosattr;
11068670SJose.Borrego@Sun.COM 	fileinfo->fi_nodeid = fattr->sa_vattr.va_nodeid;
11078670SJose.Borrego@Sun.COM 	fileinfo->fi_size = smb_node_get_size(fnode, fattr);
11088670SJose.Borrego@Sun.COM 	fileinfo->fi_alloc_size = fattr->sa_vattr.va_nblocks * DEV_BSIZE;
11098670SJose.Borrego@Sun.COM 	fileinfo->fi_atime = fattr->sa_vattr.va_atime;
11108670SJose.Borrego@Sun.COM 	fileinfo->fi_mtime = fattr->sa_vattr.va_mtime;
11118670SJose.Borrego@Sun.COM 	fileinfo->fi_ctime = fattr->sa_vattr.va_ctime;
11128670SJose.Borrego@Sun.COM 	if (fattr->sa_crtime.tv_sec)
11138670SJose.Borrego@Sun.COM 		fileinfo->fi_crtime = fattr->sa_crtime;
11148670SJose.Borrego@Sun.COM 	else
11158670SJose.Borrego@Sun.COM 		fileinfo->fi_crtime = fattr->sa_vattr.va_mtime;
11168670SJose.Borrego@Sun.COM 
11178670SJose.Borrego@Sun.COM 	smb_node_release(fnode);
11188670SJose.Borrego@Sun.COM 	return (0);
11198670SJose.Borrego@Sun.COM }
11208670SJose.Borrego@Sun.COM 
11218670SJose.Borrego@Sun.COM /*
11228670SJose.Borrego@Sun.COM  * smb_odir_wildcard_fileinfo
11238670SJose.Borrego@Sun.COM  *
11248670SJose.Borrego@Sun.COM  * odirent contains a directory entry, obtained from a vop_readdir.
11258670SJose.Borrego@Sun.COM  * If a case conflict is identified the filename is mangled and the
11268670SJose.Borrego@Sun.COM  * shortname is used as 'name', in place of odirent->od_name. This
11278670SJose.Borrego@Sun.COM  * name will be used in the smb_fsop_lookup because smb_fsop_lookup
11288670SJose.Borrego@Sun.COM  * performs a case insensitive lookup if the tree is case insesitive,
11298670SJose.Borrego@Sun.COM  * so the mangled name is required in the case conflict scenario to
11308670SJose.Borrego@Sun.COM  * ensure the correct match.
11318670SJose.Borrego@Sun.COM  *
11328670SJose.Borrego@Sun.COM  * If the looked up file is a link, we attempt to lookup the link target
11338670SJose.Borrego@Sun.COM  * to use its attributes in place of those of the files's.
11348670SJose.Borrego@Sun.COM  * If we fail to lookup the target of the link we use the original
11358670SJose.Borrego@Sun.COM  * file's attributes.
11368670SJose.Borrego@Sun.COM  * Check if the attributes match the search attributes.
11378670SJose.Borrego@Sun.COM  *
11388670SJose.Borrego@Sun.COM  * Although some file systems can have directories larger than
11398670SJose.Borrego@Sun.COM  * SMB_MAXDIRSIZE smb_odir_next_odirent ensures that no offset larger
11408670SJose.Borrego@Sun.COM  * than SMB_MAXDIRSIZE is returned.  It is therefore safe to use the
11418670SJose.Borrego@Sun.COM  * offset as the cookie (uint32_t).
11428670SJose.Borrego@Sun.COM  *
11438670SJose.Borrego@Sun.COM  * Returns: 0 - success
11448670SJose.Borrego@Sun.COM  *     ENOENT - no match, proceed to next entry
11458670SJose.Borrego@Sun.COM  *      errno - error
11468670SJose.Borrego@Sun.COM  */
11478670SJose.Borrego@Sun.COM static int
11488670SJose.Borrego@Sun.COM smb_odir_wildcard_fileinfo(smb_request_t *sr, smb_odir_t *od,
11498670SJose.Borrego@Sun.COM     smb_odirent_t *odirent, smb_fileinfo_t *fileinfo)
11508670SJose.Borrego@Sun.COM {
11518670SJose.Borrego@Sun.COM 	int		rc;
11528670SJose.Borrego@Sun.COM 	smb_node_t	*fnode, *tgt_node;
11538670SJose.Borrego@Sun.COM 	smb_attr_t	attr, tgt_attr, *fattr;
11548670SJose.Borrego@Sun.COM 	char		*name;
11558670SJose.Borrego@Sun.COM 	uint32_t	dosattr;
11568670SJose.Borrego@Sun.COM 	boolean_t	case_conflict;
11578670SJose.Borrego@Sun.COM 
11588670SJose.Borrego@Sun.COM 	ASSERT(sr);
11598670SJose.Borrego@Sun.COM 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
11608670SJose.Borrego@Sun.COM 	ASSERT(od);
11618670SJose.Borrego@Sun.COM 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
11628670SJose.Borrego@Sun.COM 
11638670SJose.Borrego@Sun.COM 	ASSERT(MUTEX_HELD(&od->d_mutex));
11648670SJose.Borrego@Sun.COM 	bzero(fileinfo, sizeof (smb_fileinfo_t));
11658670SJose.Borrego@Sun.COM 
11669231SAfshin.Ardakani@Sun.COM 	case_conflict = ((od->d_flags & SMB_ODIR_FLAG_IGNORE_CASE) &&
11678670SJose.Borrego@Sun.COM 	    (odirent->od_eflags & ED_CASE_CONFLICT));
11688670SJose.Borrego@Sun.COM 	(void) smb_mangle_name(odirent->od_ino, odirent->od_name,
11698670SJose.Borrego@Sun.COM 	    fileinfo->fi_shortname, fileinfo->fi_name83, case_conflict);
11708670SJose.Borrego@Sun.COM 	name = (case_conflict) ? fileinfo->fi_shortname : odirent->od_name;
11718670SJose.Borrego@Sun.COM 	(void) strlcpy(fileinfo->fi_name, name, sizeof (fileinfo->fi_name));
11728670SJose.Borrego@Sun.COM 
11739343SAfshin.Ardakani@Sun.COM 	rc = smb_fsop_lookup(sr, od->d_cred, 0, od->d_tree->t_snode,
11749231SAfshin.Ardakani@Sun.COM 	    od->d_dnode, name, &fnode, &attr);
11758670SJose.Borrego@Sun.COM 	if (rc != 0)
11768670SJose.Borrego@Sun.COM 		return (rc);
11778670SJose.Borrego@Sun.COM 
11788670SJose.Borrego@Sun.COM 	/* follow link to get target node & attr */
11798670SJose.Borrego@Sun.COM 	if ((fnode->vp->v_type == VLNK) &&
11808670SJose.Borrego@Sun.COM 	    (smb_odir_lookup_link(sr, od, name, &tgt_node, &tgt_attr))) {
11818670SJose.Borrego@Sun.COM 		smb_node_release(fnode);
11828670SJose.Borrego@Sun.COM 		fnode = tgt_node;
11838670SJose.Borrego@Sun.COM 		fattr = &tgt_attr;
11848670SJose.Borrego@Sun.COM 	} else {
11858670SJose.Borrego@Sun.COM 		fattr = &attr;
11868670SJose.Borrego@Sun.COM 	}
11878670SJose.Borrego@Sun.COM 
11888670SJose.Borrego@Sun.COM 	/* check search attributes */
11898670SJose.Borrego@Sun.COM 	dosattr = smb_node_get_dosattr(fnode);
11908934SJose.Borrego@Sun.COM 	if (!smb_sattr_check(dosattr, od->d_sattr)) {
11918670SJose.Borrego@Sun.COM 		smb_node_release(fnode);
11928670SJose.Borrego@Sun.COM 		return (ENOENT);
11938670SJose.Borrego@Sun.COM 	}
11948670SJose.Borrego@Sun.COM 
11958670SJose.Borrego@Sun.COM 	fileinfo->fi_cookie = (uint32_t)od->d_offset;
11968670SJose.Borrego@Sun.COM 	fileinfo->fi_dosattr = dosattr;
11978670SJose.Borrego@Sun.COM 	fileinfo->fi_nodeid = fattr->sa_vattr.va_nodeid;
11988670SJose.Borrego@Sun.COM 	fileinfo->fi_size = smb_node_get_size(fnode, fattr);
11998670SJose.Borrego@Sun.COM 	fileinfo->fi_alloc_size = fattr->sa_vattr.va_nblocks * DEV_BSIZE;
12008670SJose.Borrego@Sun.COM 	fileinfo->fi_atime = fattr->sa_vattr.va_atime;
12018670SJose.Borrego@Sun.COM 	fileinfo->fi_mtime = fattr->sa_vattr.va_mtime;
12028670SJose.Borrego@Sun.COM 	fileinfo->fi_ctime = fattr->sa_vattr.va_ctime;
12038670SJose.Borrego@Sun.COM 	if (fattr->sa_crtime.tv_sec)
12048670SJose.Borrego@Sun.COM 		fileinfo->fi_crtime = fattr->sa_crtime;
12058670SJose.Borrego@Sun.COM 	else
12068670SJose.Borrego@Sun.COM 		fileinfo->fi_crtime = fattr->sa_vattr.va_mtime;
12078670SJose.Borrego@Sun.COM 
12088670SJose.Borrego@Sun.COM 	smb_node_release(fnode);
12098670SJose.Borrego@Sun.COM 	return (0);
12108670SJose.Borrego@Sun.COM }
12118670SJose.Borrego@Sun.COM 
12128670SJose.Borrego@Sun.COM /*
12138670SJose.Borrego@Sun.COM  * smb_odir_lookup_link
12148670SJose.Borrego@Sun.COM  *
12158670SJose.Borrego@Sun.COM  * If the file is a symlink we lookup the object to which the
12168670SJose.Borrego@Sun.COM  * symlink refers so that we can return its attributes.
12178670SJose.Borrego@Sun.COM  * This can cause a problem if a symlink in a sub-directory
12188670SJose.Borrego@Sun.COM  * points to a parent directory (some UNIX GUI's create a symlink
12198670SJose.Borrego@Sun.COM  * in $HOME/.desktop that points to the user's home directory).
12208670SJose.Borrego@Sun.COM  * Some Windows applications (e.g. virus scanning) loop/hang
12218670SJose.Borrego@Sun.COM  * trying to follow this recursive path and there is little
12228670SJose.Borrego@Sun.COM  * we can do because the path is constructed on the client.
12238670SJose.Borrego@Sun.COM  * smb_dirsymlink_enable allows an end-user to disable
12248670SJose.Borrego@Sun.COM  * symlinks to directories. Symlinks to other object types
12258670SJose.Borrego@Sun.COM  * should be unaffected.
12268670SJose.Borrego@Sun.COM  *
12278670SJose.Borrego@Sun.COM  * Returns:  B_TRUE - followed link. tgt_node and tgt_attr set
12288670SJose.Borrego@Sun.COM  *          B_FALSE - link not followed
12298670SJose.Borrego@Sun.COM  */
12308670SJose.Borrego@Sun.COM static boolean_t
12318670SJose.Borrego@Sun.COM smb_odir_lookup_link(smb_request_t *sr, smb_odir_t *od,
12328670SJose.Borrego@Sun.COM     char *fname, smb_node_t **tgt_node, smb_attr_t *tgt_attr)
12338670SJose.Borrego@Sun.COM {
12348670SJose.Borrego@Sun.COM 	int rc;
12358670SJose.Borrego@Sun.COM 
12369343SAfshin.Ardakani@Sun.COM 	rc = smb_fsop_lookup(sr, od->d_cred, SMB_FOLLOW_LINKS,
12379231SAfshin.Ardakani@Sun.COM 	    od->d_tree->t_snode, od->d_dnode, fname, tgt_node, tgt_attr);
12388670SJose.Borrego@Sun.COM 	if (rc != 0) {
12398670SJose.Borrego@Sun.COM 		*tgt_node = NULL;
12408670SJose.Borrego@Sun.COM 		return (B_FALSE);
12418670SJose.Borrego@Sun.COM 	}
12428670SJose.Borrego@Sun.COM 
12438670SJose.Borrego@Sun.COM 	if ((tgt_attr->sa_vattr.va_type == VDIR) && (!smb_dirsymlink_enable)) {
12448670SJose.Borrego@Sun.COM 		smb_node_release(*tgt_node);
12458670SJose.Borrego@Sun.COM 		*tgt_node = NULL;
12468670SJose.Borrego@Sun.COM 		return (B_FALSE);
12478670SJose.Borrego@Sun.COM 	}
12488670SJose.Borrego@Sun.COM 
12498670SJose.Borrego@Sun.COM 	return (B_TRUE);
12508670SJose.Borrego@Sun.COM }
1251