xref: /onnv-gate/usr/src/uts/common/fs/smbsrv/smb_odir.c (revision 10966:37e5dcdf36d3)
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  *    +-------------------------+
749635SJoyce.McIntosh@Sun.COM  *	    |            ^
759635SJoyce.McIntosh@Sun.COM  *	    | (first)    | (last)
769635SJoyce.McIntosh@Sun.COM  *	    | lookup     | release
779635SJoyce.McIntosh@Sun.COM  *	    v            |
788670SJose.Borrego@Sun.COM  *    +-------------------------+
799635SJoyce.McIntosh@Sun.COM  *    | SMB_ODIR_STATE_IN_USE   |----
809635SJoyce.McIntosh@Sun.COM  *    +-------------------------+   | lookup / release / read
819635SJoyce.McIntosh@Sun.COM  *	    |                ^-------
829635SJoyce.McIntosh@Sun.COM  *	    | close
839635SJoyce.McIntosh@Sun.COM  *	    |
849635SJoyce.McIntosh@Sun.COM  *	    v
858670SJose.Borrego@Sun.COM  *    +-------------------------+
869635SJoyce.McIntosh@Sun.COM  *    | SMB_ODIR_STATE_CLOSING  |----
879635SJoyce.McIntosh@Sun.COM  *    +-------------------------+   | close / release / read
889635SJoyce.McIntosh@Sun.COM  *	    |                ^-------
899635SJoyce.McIntosh@Sun.COM  *	    | (last) release
909635SJoyce.McIntosh@Sun.COM  *	    |
919635SJoyce.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
989635SJoyce.McIntosh@Sun.COM  * - the odir exists in the list of odirs of its tree
999635SJoyce.McIntosh@Sun.COM  * - lookup is valid in this state. It will place a hold on the odir
1009635SJoyce.McIntosh@Sun.COM  *   by incrementing the reference count and the odir will transition
1019635SJoyce.McIntosh@Sun.COM  *   to SMB_ODIR_STATE_IN_USE
1029635SJoyce.McIntosh@Sun.COM  * - read/close/release not valid in this state
1039635SJoyce.McIntosh@Sun.COM  *
1049635SJoyce.McIntosh@Sun.COM  * SMB_ODIR_STATE_IN_USE
1058670SJose.Borrego@Sun.COM  * - the odir exists in the list of odirs of its tree.
1069635SJoyce.McIntosh@Sun.COM  * - lookup is valid in this state. It will place a hold on the odir
1079635SJoyce.McIntosh@Sun.COM  *   by incrementing the reference count.
1089635SJoyce.McIntosh@Sun.COM  * - if the last hold is released the odir will transition
1099635SJoyce.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.
1159635SJoyce.McIntosh@Sun.COM  * - lookup will fail in this state.
1169635SJoyce.McIntosh@Sun.COM  * - when the last hold is released the odir will transition
1179635SJoyce.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.
1239635SJoyce.McIntosh@Sun.COM  * - lookup will fail in this state.
1249635SJoyce.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
1589635SJoyce.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  *
16710001SJoyce.McIntosh@Sun.COM  * smb_odir_read_streaminfo(..., 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  *
1719635SJoyce.McIntosh@Sun.COM  * smb_odir_close(smb_odir_t *odir)
1729635SJoyce.McIntosh@Sun.COM  *  Close the odir.
1739635SJoyce.McIntosh@Sun.COM  *  The caller of close must have a hold on the odir being closed.
1749635SJoyce.McIntosh@Sun.COM  *  The hold should be released after closing.
1759635SJoyce.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  *
21710001SJoyce.McIntosh@Sun.COM  * smb_odir_read_streaminfo
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_kproto.h>
2475331Samw #include <smbsrv/smb_fsops.h>
24810504SKeyur.Desai@Sun.COM #include <smbsrv/smb_share.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 *);
26010001SJoyce.McIntosh@Sun.COM static boolean_t smb_odir_lookup_link(smb_request_t *, smb_odir_t *,
26110001SJoyce.McIntosh@Sun.COM     char *, smb_node_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 
3428670SJose.Borrego@Sun.COM 	ASSERT(sr);
3438670SJose.Borrego@Sun.COM 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
3448670SJose.Borrego@Sun.COM 	ASSERT(unode);
3458670SJose.Borrego@Sun.COM 	ASSERT(unode->n_magic == SMB_NODE_MAGIC);
3468670SJose.Borrego@Sun.COM 
3478845Samw@Sun.COM 	if (SMB_TREE_CONTAINS_NODE(sr, unode) == 0 ||
3488845Samw@Sun.COM 	    SMB_TREE_HAS_ACCESS(sr, ACE_LIST_DIRECTORY) == 0) {
3498670SJose.Borrego@Sun.COM 		smbsr_error(sr, NT_STATUS_ACCESS_DENIED,
3508670SJose.Borrego@Sun.COM 		    ERRDOS, ERROR_ACCESS_DENIED);
3518670SJose.Borrego@Sun.COM 		return (0);
3528670SJose.Borrego@Sun.COM 	}
3539343SAfshin.Ardakani@Sun.COM 	cr = kcred;
3548670SJose.Borrego@Sun.COM 
3558670SJose.Borrego@Sun.COM 	/* find the xattrdir vnode */
3568670SJose.Borrego@Sun.COM 	rc = smb_vop_lookup_xattrdir(unode->vp, &xattr_dvp, LOOKUP_XATTR, cr);
3578670SJose.Borrego@Sun.COM 	if (rc != 0) {
3588670SJose.Borrego@Sun.COM 		smbsr_errno(sr, rc);
3598670SJose.Borrego@Sun.COM 		return (0);
3608670SJose.Borrego@Sun.COM 	}
3618670SJose.Borrego@Sun.COM 
3628670SJose.Borrego@Sun.COM 	/* lookup the xattrdir's smb_node */
3638670SJose.Borrego@Sun.COM 	xattr_dnode = smb_node_lookup(sr, NULL, cr, xattr_dvp, XATTR_DIR,
36410001SJoyce.McIntosh@Sun.COM 	    unode, NULL);
3658670SJose.Borrego@Sun.COM 	VN_RELE(xattr_dvp);
3668670SJose.Borrego@Sun.COM 	if (xattr_dnode == NULL) {
3678670SJose.Borrego@Sun.COM 		smbsr_error(sr, NT_STATUS_NO_MEMORY,
3688670SJose.Borrego@Sun.COM 		    ERRDOS, ERROR_NOT_ENOUGH_MEMORY);
3698670SJose.Borrego@Sun.COM 		return (0);
3708670SJose.Borrego@Sun.COM 	}
3718670SJose.Borrego@Sun.COM 
3728670SJose.Borrego@Sun.COM 	(void) snprintf(pattern, sizeof (pattern), "%s*", SMB_STREAM_PREFIX);
3739343SAfshin.Ardakani@Sun.COM 	od = smb_odir_create(sr, xattr_dnode, pattern, SMB_SEARCH_ATTRIBUTES,
3749343SAfshin.Ardakani@Sun.COM 	    cr);
3758670SJose.Borrego@Sun.COM 	smb_node_release(xattr_dnode);
3768670SJose.Borrego@Sun.COM 	if (od == NULL)
3778670SJose.Borrego@Sun.COM 		return (0);
3788670SJose.Borrego@Sun.COM 
3799231SAfshin.Ardakani@Sun.COM 	od->d_flags |= SMB_ODIR_FLAG_XATTR;
3808670SJose.Borrego@Sun.COM 	return (od->d_odid);
3818670SJose.Borrego@Sun.COM }
3828670SJose.Borrego@Sun.COM 
3838670SJose.Borrego@Sun.COM /*
3848670SJose.Borrego@Sun.COM  * smb_odir_hold
3859635SJoyce.McIntosh@Sun.COM  *
3869635SJoyce.McIntosh@Sun.COM  * A hold will only be granted if the odir is open or in_use.
3878670SJose.Borrego@Sun.COM  */
3888670SJose.Borrego@Sun.COM boolean_t
3898670SJose.Borrego@Sun.COM smb_odir_hold(smb_odir_t *od)
3908670SJose.Borrego@Sun.COM {
3918670SJose.Borrego@Sun.COM 	ASSERT(od);
3928670SJose.Borrego@Sun.COM 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
3938670SJose.Borrego@Sun.COM 
3948670SJose.Borrego@Sun.COM 	mutex_enter(&od->d_mutex);
3959635SJoyce.McIntosh@Sun.COM 
3969635SJoyce.McIntosh@Sun.COM 	switch (od->d_state) {
3979635SJoyce.McIntosh@Sun.COM 	case SMB_ODIR_STATE_OPEN:
3989635SJoyce.McIntosh@Sun.COM 		od->d_refcnt++;
3999635SJoyce.McIntosh@Sun.COM 		od->d_state = SMB_ODIR_STATE_IN_USE;
4009635SJoyce.McIntosh@Sun.COM 		break;
4019635SJoyce.McIntosh@Sun.COM 	case SMB_ODIR_STATE_IN_USE:
4029635SJoyce.McIntosh@Sun.COM 		od->d_refcnt++;
4039635SJoyce.McIntosh@Sun.COM 		break;
4049635SJoyce.McIntosh@Sun.COM 	case SMB_ODIR_STATE_CLOSING:
4059635SJoyce.McIntosh@Sun.COM 	case SMB_ODIR_STATE_CLOSED:
4069635SJoyce.McIntosh@Sun.COM 	default:
4078670SJose.Borrego@Sun.COM 		mutex_exit(&od->d_mutex);
4088670SJose.Borrego@Sun.COM 		return (B_FALSE);
4098670SJose.Borrego@Sun.COM 	}
4108670SJose.Borrego@Sun.COM 
4118670SJose.Borrego@Sun.COM 	mutex_exit(&od->d_mutex);
4128670SJose.Borrego@Sun.COM 	return (B_TRUE);
4138670SJose.Borrego@Sun.COM }
4148670SJose.Borrego@Sun.COM 
4158670SJose.Borrego@Sun.COM /*
4168670SJose.Borrego@Sun.COM  * smb_odir_release
4178670SJose.Borrego@Sun.COM  *
4188670SJose.Borrego@Sun.COM  * If the odir is in SMB_ODIR_STATE_CLOSING and this release
4198670SJose.Borrego@Sun.COM  * results in a refcnt of 0, the odir may be removed from
4208670SJose.Borrego@Sun.COM  * the tree's list of odirs and deleted.  The odir's state is
4218670SJose.Borrego@Sun.COM  * set to SMB_ODIR_STATE_CLOSED prior to exiting the mutex and
4229635SJoyce.McIntosh@Sun.COM  * deleting the odir.
4235331Samw  */
4245331Samw void
4258670SJose.Borrego@Sun.COM smb_odir_release(smb_odir_t *od)
4265331Samw {
4275331Samw 	ASSERT(od);
4285331Samw 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
4295331Samw 
4305331Samw 	mutex_enter(&od->d_mutex);
4319635SJoyce.McIntosh@Sun.COM 	ASSERT(od->d_refcnt > 0);
4328670SJose.Borrego@Sun.COM 
4335331Samw 	switch (od->d_state) {
4345331Samw 	case SMB_ODIR_STATE_OPEN:
4359635SJoyce.McIntosh@Sun.COM 		break;
4369635SJoyce.McIntosh@Sun.COM 	case SMB_ODIR_STATE_IN_USE:
4378670SJose.Borrego@Sun.COM 		od->d_refcnt--;
4389635SJoyce.McIntosh@Sun.COM 		if (od->d_refcnt == 0)
4399635SJoyce.McIntosh@Sun.COM 			od->d_state = SMB_ODIR_STATE_OPEN;
4405331Samw 		break;
4415331Samw 	case SMB_ODIR_STATE_CLOSING:
4428670SJose.Borrego@Sun.COM 		od->d_refcnt--;
4438670SJose.Borrego@Sun.COM 		if (od->d_refcnt == 0) {
4448670SJose.Borrego@Sun.COM 			od->d_state = SMB_ODIR_STATE_CLOSED;
4458670SJose.Borrego@Sun.COM 			mutex_exit(&od->d_mutex);
4468670SJose.Borrego@Sun.COM 			smb_odir_delete(od);
4478670SJose.Borrego@Sun.COM 			return;
4488670SJose.Borrego@Sun.COM 		}
4498670SJose.Borrego@Sun.COM 		break;
4505331Samw 	case SMB_ODIR_STATE_CLOSED:
4515331Samw 	default:
4525331Samw 		break;
4535331Samw 	}
4545331Samw 
4555331Samw 	mutex_exit(&od->d_mutex);
4565331Samw }
4575331Samw 
4585331Samw /*
4598670SJose.Borrego@Sun.COM  * smb_odir_close
4605331Samw  */
4618670SJose.Borrego@Sun.COM void
4628670SJose.Borrego@Sun.COM smb_odir_close(smb_odir_t *od)
4635331Samw {
4648670SJose.Borrego@Sun.COM 	ASSERT(od);
4659635SJoyce.McIntosh@Sun.COM 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
4665331Samw 
4678670SJose.Borrego@Sun.COM 	mutex_enter(&od->d_mutex);
4689635SJoyce.McIntosh@Sun.COM 	ASSERT(od->d_refcnt > 0);
4699635SJoyce.McIntosh@Sun.COM 	switch (od->d_state) {
4709635SJoyce.McIntosh@Sun.COM 	case SMB_ODIR_STATE_OPEN:
4719635SJoyce.McIntosh@Sun.COM 		break;
4729635SJoyce.McIntosh@Sun.COM 	case SMB_ODIR_STATE_IN_USE:
4739635SJoyce.McIntosh@Sun.COM 		od->d_state = SMB_ODIR_STATE_CLOSING;
4749635SJoyce.McIntosh@Sun.COM 		break;
4759635SJoyce.McIntosh@Sun.COM 	case SMB_ODIR_STATE_CLOSING:
4769635SJoyce.McIntosh@Sun.COM 	case SMB_ODIR_STATE_CLOSED:
4779635SJoyce.McIntosh@Sun.COM 	default:
4789635SJoyce.McIntosh@Sun.COM 		break;
4798670SJose.Borrego@Sun.COM 	}
4808670SJose.Borrego@Sun.COM 	mutex_exit(&od->d_mutex);
4818670SJose.Borrego@Sun.COM }
4825331Samw 
4838670SJose.Borrego@Sun.COM /*
4848670SJose.Borrego@Sun.COM  * smb_odir_read
4858670SJose.Borrego@Sun.COM  *
4868670SJose.Borrego@Sun.COM  * Find the next directory entry matching the search pattern.
4878670SJose.Borrego@Sun.COM  * No search attribute matching is performed.
4888670SJose.Borrego@Sun.COM  *
4898670SJose.Borrego@Sun.COM  * Returns:
4908670SJose.Borrego@Sun.COM  *  0 - success.
4918670SJose.Borrego@Sun.COM  *      - If a matching entry was found eof will be B_FALSE and
4928670SJose.Borrego@Sun.COM  *        odirent will be populated.
4938670SJose.Borrego@Sun.COM  *      - If there are no matching entries eof will be B_TRUE.
4948670SJose.Borrego@Sun.COM  * -1 - error, error details set in sr.
4958670SJose.Borrego@Sun.COM  */
4968670SJose.Borrego@Sun.COM int
4978670SJose.Borrego@Sun.COM smb_odir_read(smb_request_t *sr, smb_odir_t *od,
4988670SJose.Borrego@Sun.COM     smb_odirent_t *odirent, boolean_t *eof)
4998670SJose.Borrego@Sun.COM {
5009231SAfshin.Ardakani@Sun.COM 	int		rc;
5019231SAfshin.Ardakani@Sun.COM 	boolean_t	ignore_case;
5028670SJose.Borrego@Sun.COM 
5038670SJose.Borrego@Sun.COM 	ASSERT(sr);
5048670SJose.Borrego@Sun.COM 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
5058670SJose.Borrego@Sun.COM 	ASSERT(od);
5068670SJose.Borrego@Sun.COM 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
5078670SJose.Borrego@Sun.COM 	ASSERT(odirent);
5088670SJose.Borrego@Sun.COM 
5098670SJose.Borrego@Sun.COM 	mutex_enter(&od->d_mutex);
5109635SJoyce.McIntosh@Sun.COM 	ASSERT(od->d_refcnt > 0);
5118670SJose.Borrego@Sun.COM 
5129635SJoyce.McIntosh@Sun.COM 	switch (od->d_state) {
5139635SJoyce.McIntosh@Sun.COM 	case SMB_ODIR_STATE_IN_USE:
5149635SJoyce.McIntosh@Sun.COM 	case SMB_ODIR_STATE_CLOSING:
5159635SJoyce.McIntosh@Sun.COM 		break;
5169635SJoyce.McIntosh@Sun.COM 	case SMB_ODIR_STATE_OPEN:
5179635SJoyce.McIntosh@Sun.COM 	case SMB_ODIR_STATE_CLOSED:
5189635SJoyce.McIntosh@Sun.COM 	default:
5198670SJose.Borrego@Sun.COM 		mutex_exit(&od->d_mutex);
5208670SJose.Borrego@Sun.COM 		return (-1);
5218670SJose.Borrego@Sun.COM 	}
5228670SJose.Borrego@Sun.COM 
5239231SAfshin.Ardakani@Sun.COM 	ignore_case = (od->d_flags & SMB_ODIR_FLAG_IGNORE_CASE);
5249231SAfshin.Ardakani@Sun.COM 
5258670SJose.Borrego@Sun.COM 	for (;;) {
5268670SJose.Borrego@Sun.COM 		if ((rc = smb_odir_next_odirent(od, odirent)) != 0)
5275331Samw 			break;
5288670SJose.Borrego@Sun.COM 		if (smb_match_name(odirent->od_ino, odirent->od_name,
5299231SAfshin.Ardakani@Sun.COM 		    od->d_pattern, ignore_case))
5308670SJose.Borrego@Sun.COM 			break;
5315331Samw 	}
5328670SJose.Borrego@Sun.COM 
5338670SJose.Borrego@Sun.COM 	mutex_exit(&od->d_mutex);
5348670SJose.Borrego@Sun.COM 
5358670SJose.Borrego@Sun.COM 	switch (rc) {
5368670SJose.Borrego@Sun.COM 	case 0:
5378670SJose.Borrego@Sun.COM 		*eof = B_FALSE;
5388670SJose.Borrego@Sun.COM 		return (0);
5398670SJose.Borrego@Sun.COM 	case ENOENT:
5408670SJose.Borrego@Sun.COM 		*eof = B_TRUE;
5418670SJose.Borrego@Sun.COM 		return (0);
5428670SJose.Borrego@Sun.COM 	default:
5438670SJose.Borrego@Sun.COM 		smbsr_errno(sr, rc);
5448670SJose.Borrego@Sun.COM 		return (-1);
5458670SJose.Borrego@Sun.COM 	}
5465331Samw }
5475331Samw 
5488670SJose.Borrego@Sun.COM /*
5498670SJose.Borrego@Sun.COM  * smb_odir_read_fileinfo
5508670SJose.Borrego@Sun.COM  *
5518670SJose.Borrego@Sun.COM  * Find the next directory entry matching the search pattern
5528670SJose.Borrego@Sun.COM  * and attributes: od->d_pattern and od->d_sattr.
5538670SJose.Borrego@Sun.COM  *
5548670SJose.Borrego@Sun.COM  * If the search pattern specifies a single filename call
5558670SJose.Borrego@Sun.COM  * smb_odir_single_fileinfo to get the file attributes and
5568670SJose.Borrego@Sun.COM  * populate the caller's smb_fileinfo_t.
5578670SJose.Borrego@Sun.COM  *
5588670SJose.Borrego@Sun.COM  * If the search pattern contains wildcards call smb_odir_next_odirent
5598670SJose.Borrego@Sun.COM  * to get the next directory entry then. Repeat until a matching
5608670SJose.Borrego@Sun.COM  * filename is found. Call smb_odir_wildcard_fileinfo to get the
5618670SJose.Borrego@Sun.COM  * file attributes and populate the caller's smb_fileinfo_t.
5628670SJose.Borrego@Sun.COM  * This is repeated until a file matching the search criteria is found.
5638670SJose.Borrego@Sun.COM  *
5648670SJose.Borrego@Sun.COM  * Returns:
5658670SJose.Borrego@Sun.COM  *  0 - success.
5668670SJose.Borrego@Sun.COM  *      - If a matching entry was found eof will be B_FALSE and
5678670SJose.Borrego@Sun.COM  *        fileinfo will be populated.
5688670SJose.Borrego@Sun.COM  *      - If there are no matching entries eof will be B_TRUE.
5698670SJose.Borrego@Sun.COM  * -1 - error, error details set in sr.
5708670SJose.Borrego@Sun.COM  */
5718670SJose.Borrego@Sun.COM int
5728670SJose.Borrego@Sun.COM smb_odir_read_fileinfo(smb_request_t *sr, smb_odir_t *od,
5738670SJose.Borrego@Sun.COM     smb_fileinfo_t *fileinfo, boolean_t *eof)
5748670SJose.Borrego@Sun.COM {
575*10966SJordan.Brown@Sun.COM 	int		rc, errnum;
5768670SJose.Borrego@Sun.COM 	smb_odirent_t	*odirent;
5779231SAfshin.Ardakani@Sun.COM 	boolean_t	ignore_case;
5788670SJose.Borrego@Sun.COM 
5798670SJose.Borrego@Sun.COM 	ASSERT(sr);
5808670SJose.Borrego@Sun.COM 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
5818670SJose.Borrego@Sun.COM 	ASSERT(od);
5828670SJose.Borrego@Sun.COM 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
5838670SJose.Borrego@Sun.COM 	ASSERT(fileinfo);
5848670SJose.Borrego@Sun.COM 
5858670SJose.Borrego@Sun.COM 	mutex_enter(&od->d_mutex);
5869635SJoyce.McIntosh@Sun.COM 	ASSERT(od->d_refcnt > 0);
5878670SJose.Borrego@Sun.COM 
5889635SJoyce.McIntosh@Sun.COM 	switch (od->d_state) {
5899635SJoyce.McIntosh@Sun.COM 	case SMB_ODIR_STATE_IN_USE:
5909635SJoyce.McIntosh@Sun.COM 	case SMB_ODIR_STATE_CLOSING:
5919635SJoyce.McIntosh@Sun.COM 		break;
5929635SJoyce.McIntosh@Sun.COM 	case SMB_ODIR_STATE_OPEN:
5939635SJoyce.McIntosh@Sun.COM 	case SMB_ODIR_STATE_CLOSED:
5949635SJoyce.McIntosh@Sun.COM 	default:
5958670SJose.Borrego@Sun.COM 		mutex_exit(&od->d_mutex);
5968670SJose.Borrego@Sun.COM 		return (-1);
5978670SJose.Borrego@Sun.COM 	}
5988670SJose.Borrego@Sun.COM 
5999231SAfshin.Ardakani@Sun.COM 	ignore_case = (od->d_flags & SMB_ODIR_FLAG_IGNORE_CASE);
6009231SAfshin.Ardakani@Sun.COM 
6019231SAfshin.Ardakani@Sun.COM 	if (!(od->d_flags & SMB_ODIR_FLAG_WILDCARDS)) {
6028670SJose.Borrego@Sun.COM 		if (od->d_eof)
6038670SJose.Borrego@Sun.COM 			rc = ENOENT;
6048670SJose.Borrego@Sun.COM 		else
6058670SJose.Borrego@Sun.COM 			rc = smb_odir_single_fileinfo(sr, od, fileinfo);
6068670SJose.Borrego@Sun.COM 		od->d_eof = B_TRUE;
6078670SJose.Borrego@Sun.COM 	} else {
6088670SJose.Borrego@Sun.COM 		odirent = kmem_alloc(sizeof (smb_odirent_t), KM_SLEEP);
6098670SJose.Borrego@Sun.COM 		for (;;) {
6108670SJose.Borrego@Sun.COM 			bzero(fileinfo, sizeof (smb_fileinfo_t));
6118670SJose.Borrego@Sun.COM 			if ((rc = smb_odir_next_odirent(od, odirent)) != 0)
6128670SJose.Borrego@Sun.COM 				break;
6138670SJose.Borrego@Sun.COM 
614*10966SJordan.Brown@Sun.COM 			/* skip non utf8 filename */
615*10966SJordan.Brown@Sun.COM 			if (u8_validate(odirent->od_name,
616*10966SJordan.Brown@Sun.COM 			    strlen(odirent->od_name), NULL,
617*10966SJordan.Brown@Sun.COM 			    U8_VALIDATE_ENTIRE, &errnum) < 0)
618*10966SJordan.Brown@Sun.COM 				continue;
619*10966SJordan.Brown@Sun.COM 
6208670SJose.Borrego@Sun.COM 			if (!smb_match_name(odirent->od_ino, odirent->od_name,
6219231SAfshin.Ardakani@Sun.COM 			    od->d_pattern, ignore_case))
6228670SJose.Borrego@Sun.COM 				continue;
6238670SJose.Borrego@Sun.COM 
6248670SJose.Borrego@Sun.COM 			rc = smb_odir_wildcard_fileinfo(sr, od, odirent,
6258670SJose.Borrego@Sun.COM 			    fileinfo);
6268670SJose.Borrego@Sun.COM 			if (rc == 0)
6278670SJose.Borrego@Sun.COM 				break;
6288670SJose.Borrego@Sun.COM 		}
6298670SJose.Borrego@Sun.COM 		kmem_free(odirent, sizeof (smb_odirent_t));
6308670SJose.Borrego@Sun.COM 	}
6318670SJose.Borrego@Sun.COM 	mutex_exit(&od->d_mutex);
6328670SJose.Borrego@Sun.COM 
6338670SJose.Borrego@Sun.COM 	switch (rc) {
6348670SJose.Borrego@Sun.COM 	case 0:
6358670SJose.Borrego@Sun.COM 		*eof = B_FALSE;
6368670SJose.Borrego@Sun.COM 		return (0);
6378670SJose.Borrego@Sun.COM 	case ENOENT:
6388670SJose.Borrego@Sun.COM 		*eof = B_TRUE;
6398670SJose.Borrego@Sun.COM 		return (0);
6408670SJose.Borrego@Sun.COM 	default:
6418670SJose.Borrego@Sun.COM 		smbsr_errno(sr, rc);
6428670SJose.Borrego@Sun.COM 		return (-1);
6438670SJose.Borrego@Sun.COM 	}
6448670SJose.Borrego@Sun.COM }
6458670SJose.Borrego@Sun.COM 
6465331Samw 
6475331Samw /*
6488670SJose.Borrego@Sun.COM  * smb_odir_read_streaminfo
6498670SJose.Borrego@Sun.COM  *
6508670SJose.Borrego@Sun.COM  * Find the next directory entry whose name begins with SMB_STREAM_PREFIX,
6518670SJose.Borrego@Sun.COM  * and thus represents an NTFS named stream.
6528670SJose.Borrego@Sun.COM  * No search attribute matching is performed.
6539231SAfshin.Ardakani@Sun.COM  * No case conflict name mangling is required for NTFS named stream names.
6545331Samw  *
6558670SJose.Borrego@Sun.COM  * Returns:
6568670SJose.Borrego@Sun.COM  *  0 - success.
6578670SJose.Borrego@Sun.COM  *      - If a matching entry was found eof will be B_FALSE and
6588670SJose.Borrego@Sun.COM  *        sinfo will be populated.
6598670SJose.Borrego@Sun.COM  *      - If there are no matching entries eof will be B_TRUE.
6608670SJose.Borrego@Sun.COM  * -1 - error, error details set in sr.
6615331Samw  */
6628670SJose.Borrego@Sun.COM int
6638670SJose.Borrego@Sun.COM smb_odir_read_streaminfo(smb_request_t *sr, smb_odir_t *od,
6648670SJose.Borrego@Sun.COM     smb_streaminfo_t *sinfo, boolean_t *eof)
6655331Samw {
6668670SJose.Borrego@Sun.COM 	int		rc;
6678670SJose.Borrego@Sun.COM 	smb_odirent_t	*odirent;
66810504SKeyur.Desai@Sun.COM 	smb_node_t	*fnode;
6698670SJose.Borrego@Sun.COM 	smb_attr_t	attr;
6705331Samw 
6718670SJose.Borrego@Sun.COM 	ASSERT(sr);
6728670SJose.Borrego@Sun.COM 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
6735331Samw 	ASSERT(od);
6745331Samw 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
6758670SJose.Borrego@Sun.COM 	ASSERT(sinfo);
6768670SJose.Borrego@Sun.COM 
6778670SJose.Borrego@Sun.COM 	mutex_enter(&od->d_mutex);
6789635SJoyce.McIntosh@Sun.COM 	ASSERT(od->d_refcnt > 0);
6798670SJose.Borrego@Sun.COM 
6809635SJoyce.McIntosh@Sun.COM 	switch (od->d_state) {
6819635SJoyce.McIntosh@Sun.COM 	case SMB_ODIR_STATE_IN_USE:
6829635SJoyce.McIntosh@Sun.COM 	case SMB_ODIR_STATE_CLOSING:
6839635SJoyce.McIntosh@Sun.COM 		break;
6849635SJoyce.McIntosh@Sun.COM 	case SMB_ODIR_STATE_OPEN:
6859635SJoyce.McIntosh@Sun.COM 	case SMB_ODIR_STATE_CLOSED:
6869635SJoyce.McIntosh@Sun.COM 	default:
6878670SJose.Borrego@Sun.COM 		mutex_exit(&od->d_mutex);
6888670SJose.Borrego@Sun.COM 		return (-1);
6898670SJose.Borrego@Sun.COM 	}
6908670SJose.Borrego@Sun.COM 
6918670SJose.Borrego@Sun.COM 	/* Check that odir represents an xattr directory */
6929231SAfshin.Ardakani@Sun.COM 	if (!(od->d_flags & SMB_ODIR_FLAG_XATTR)) {
6938670SJose.Borrego@Sun.COM 		*eof = B_TRUE;
6948670SJose.Borrego@Sun.COM 		mutex_exit(&od->d_mutex);
6958670SJose.Borrego@Sun.COM 		return (0);
6968670SJose.Borrego@Sun.COM 	}
6978670SJose.Borrego@Sun.COM 
6988670SJose.Borrego@Sun.COM 	odirent = kmem_alloc(sizeof (smb_odirent_t), KM_SLEEP);
6998670SJose.Borrego@Sun.COM 
7008670SJose.Borrego@Sun.COM 	for (;;) {
7018670SJose.Borrego@Sun.COM 		bzero(sinfo, sizeof (smb_streaminfo_t));
7028670SJose.Borrego@Sun.COM 		if ((rc = smb_odir_next_odirent(od, odirent)) != 0)
7038670SJose.Borrego@Sun.COM 			break;
7048670SJose.Borrego@Sun.COM 
7058670SJose.Borrego@Sun.COM 		if (strncmp(odirent->od_name, SMB_STREAM_PREFIX,
7068670SJose.Borrego@Sun.COM 		    SMB_STREAM_PREFIX_LEN)) {
7078670SJose.Borrego@Sun.COM 			continue;
7088670SJose.Borrego@Sun.COM 		}
7098670SJose.Borrego@Sun.COM 
71010504SKeyur.Desai@Sun.COM 		rc = smb_fsop_lookup(sr, od->d_cred, 0, od->d_tree->t_snode,
71110504SKeyur.Desai@Sun.COM 		    od->d_dnode, odirent->od_name, &fnode);
7128670SJose.Borrego@Sun.COM 		if (rc == 0) {
71310504SKeyur.Desai@Sun.COM 			rc = smb_node_getattr(sr, fnode, &attr);
71410504SKeyur.Desai@Sun.COM 			smb_node_release(fnode);
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;
72210504SKeyur.Desai@Sun.COM 			sinfo->si_alloc_size = attr.sa_allocsz;
7238670SJose.Borrego@Sun.COM 			break;
7248670SJose.Borrego@Sun.COM 		}
7258670SJose.Borrego@Sun.COM 	}
7268670SJose.Borrego@Sun.COM 	mutex_exit(&od->d_mutex);
7278670SJose.Borrego@Sun.COM 
7288670SJose.Borrego@Sun.COM 	kmem_free(odirent, sizeof (smb_odirent_t));
7298670SJose.Borrego@Sun.COM 
7308670SJose.Borrego@Sun.COM 	switch (rc) {
7318670SJose.Borrego@Sun.COM 	case 0:
7328670SJose.Borrego@Sun.COM 		*eof = B_FALSE;
7338670SJose.Borrego@Sun.COM 		return (0);
7348670SJose.Borrego@Sun.COM 	case ENOENT:
7358670SJose.Borrego@Sun.COM 		*eof = B_TRUE;
7368670SJose.Borrego@Sun.COM 		return (0);
7378670SJose.Borrego@Sun.COM 	default:
7388670SJose.Borrego@Sun.COM 		smbsr_errno(sr, rc);
7398670SJose.Borrego@Sun.COM 		return (-1);
7408670SJose.Borrego@Sun.COM 	}
7418670SJose.Borrego@Sun.COM }
7428670SJose.Borrego@Sun.COM 
7438670SJose.Borrego@Sun.COM /*
7448670SJose.Borrego@Sun.COM  * smb_odir_save_cookie
7458670SJose.Borrego@Sun.COM  *
7468670SJose.Borrego@Sun.COM  * Callers can save up to SMB_MAX_SEARCH cookies in the odir
7478670SJose.Borrego@Sun.COM  * to be used as resume points for a 'find next' request.
7488670SJose.Borrego@Sun.COM  */
7498670SJose.Borrego@Sun.COM void
7508670SJose.Borrego@Sun.COM smb_odir_save_cookie(smb_odir_t *od, int idx, uint32_t cookie)
7518670SJose.Borrego@Sun.COM {
7528670SJose.Borrego@Sun.COM 	ASSERT(od);
7538670SJose.Borrego@Sun.COM 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
7548670SJose.Borrego@Sun.COM 	ASSERT(idx >= 0 && idx < SMB_MAX_SEARCH);
7555331Samw 
7565331Samw 	mutex_enter(&od->d_mutex);
7578670SJose.Borrego@Sun.COM 	od->d_cookies[idx] = cookie;
7588670SJose.Borrego@Sun.COM 	mutex_exit(&od->d_mutex);
7598670SJose.Borrego@Sun.COM }
7608670SJose.Borrego@Sun.COM 
7618670SJose.Borrego@Sun.COM /*
7628670SJose.Borrego@Sun.COM  * smb_odir_resume_at
7638670SJose.Borrego@Sun.COM  *
7649343SAfshin.Ardakani@Sun.COM  * If SMB_ODIR_FLAG_WILDCARDS is not set the search is for a single
7659343SAfshin.Ardakani@Sun.COM  * file and should not be resumed.
7669343SAfshin.Ardakani@Sun.COM  *
7679343SAfshin.Ardakani@Sun.COM  * Wildcard searching can be resumed from:
7688670SJose.Borrego@Sun.COM  * - the cookie saved at a specified index (SMBsearch, SMBfind).
7698670SJose.Borrego@Sun.COM  * - a specified cookie (SMB_trans2_find)
7708670SJose.Borrego@Sun.COM  * - a specified filename (SMB_trans2_find) - NOT SUPPORTED.
7718670SJose.Borrego@Sun.COM  *   Defaults to continuing from where the last search ended.
7728670SJose.Borrego@Sun.COM  *
7738670SJose.Borrego@Sun.COM  * Continuation from where the last search ended (SMB_trans2_find)
7748670SJose.Borrego@Sun.COM  * is implemented by saving the last cookie at a specific index (0)
7758670SJose.Borrego@Sun.COM  * smb_odir_resume_at indicates a new request, so reset od->d_bufptr
7768670SJose.Borrego@Sun.COM  * and d_eof to force a vop_readdir.
7778670SJose.Borrego@Sun.COM  */
7788670SJose.Borrego@Sun.COM void
7798670SJose.Borrego@Sun.COM smb_odir_resume_at(smb_odir_t *od, smb_odir_resume_t *resume)
7808670SJose.Borrego@Sun.COM {
7818670SJose.Borrego@Sun.COM 	ASSERT(od);
7828670SJose.Borrego@Sun.COM 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
7838670SJose.Borrego@Sun.COM 	ASSERT(resume);
7848670SJose.Borrego@Sun.COM 
7858670SJose.Borrego@Sun.COM 	mutex_enter(&od->d_mutex);
7868670SJose.Borrego@Sun.COM 
7879343SAfshin.Ardakani@Sun.COM 	if ((od->d_flags & SMB_ODIR_FLAG_WILDCARDS) == 0) {
7889343SAfshin.Ardakani@Sun.COM 		od->d_eof = B_TRUE;
7899343SAfshin.Ardakani@Sun.COM 		mutex_exit(&od->d_mutex);
7909343SAfshin.Ardakani@Sun.COM 		return;
7919343SAfshin.Ardakani@Sun.COM 	}
7929343SAfshin.Ardakani@Sun.COM 
7938670SJose.Borrego@Sun.COM 	switch (resume->or_type) {
7948670SJose.Borrego@Sun.COM 		case SMB_ODIR_RESUME_IDX:
7958670SJose.Borrego@Sun.COM 			ASSERT(resume->or_idx >= 0);
7968670SJose.Borrego@Sun.COM 			ASSERT(resume->or_idx < SMB_MAX_SEARCH);
7978670SJose.Borrego@Sun.COM 
7988670SJose.Borrego@Sun.COM 			if ((resume->or_idx < 0) ||
7998670SJose.Borrego@Sun.COM 			    (resume->or_idx >= SMB_MAX_SEARCH)) {
8008670SJose.Borrego@Sun.COM 				resume->or_idx = 0;
8018670SJose.Borrego@Sun.COM 			}
8028670SJose.Borrego@Sun.COM 			od->d_offset = od->d_cookies[resume->or_idx];
8038670SJose.Borrego@Sun.COM 			break;
8048670SJose.Borrego@Sun.COM 		case SMB_ODIR_RESUME_COOKIE:
8058670SJose.Borrego@Sun.COM 			od->d_offset = resume->or_cookie;
8068670SJose.Borrego@Sun.COM 			break;
8078670SJose.Borrego@Sun.COM 		case SMB_ODIR_RESUME_FNAME:
8088670SJose.Borrego@Sun.COM 		default:
8098670SJose.Borrego@Sun.COM 			od->d_offset = od->d_cookies[0];
8108670SJose.Borrego@Sun.COM 			break;
8115331Samw 	}
8128670SJose.Borrego@Sun.COM 
8138670SJose.Borrego@Sun.COM 	/* Force a vop_readdir to refresh d_buf */
8148670SJose.Borrego@Sun.COM 	od->d_bufptr = NULL;
8158670SJose.Borrego@Sun.COM 	od->d_eof = B_FALSE;
8168670SJose.Borrego@Sun.COM 
8178670SJose.Borrego@Sun.COM 	mutex_exit(&od->d_mutex);
8188670SJose.Borrego@Sun.COM }
8198670SJose.Borrego@Sun.COM 
8208670SJose.Borrego@Sun.COM 
8218670SJose.Borrego@Sun.COM /* *** static functions *** */
8228670SJose.Borrego@Sun.COM 
8238670SJose.Borrego@Sun.COM /*
8248670SJose.Borrego@Sun.COM  * smb_odir_create
8258670SJose.Borrego@Sun.COM  * Allocate and populate an odir obect and add it to the tree's list.
8268670SJose.Borrego@Sun.COM  */
8278670SJose.Borrego@Sun.COM static smb_odir_t *
8288670SJose.Borrego@Sun.COM smb_odir_create(smb_request_t *sr, smb_node_t *dnode,
8299343SAfshin.Ardakani@Sun.COM     char *pattern, uint16_t sattr, cred_t *cr)
8308670SJose.Borrego@Sun.COM {
8318670SJose.Borrego@Sun.COM 	smb_odir_t	*od;
8328670SJose.Borrego@Sun.COM 	smb_tree_t	*tree;
8338670SJose.Borrego@Sun.COM 	uint16_t	odid;
8348670SJose.Borrego@Sun.COM 
8358670SJose.Borrego@Sun.COM 	ASSERT(sr);
8368670SJose.Borrego@Sun.COM 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
8378670SJose.Borrego@Sun.COM 	ASSERT(sr->tid_tree);
8388670SJose.Borrego@Sun.COM 	ASSERT(sr->tid_tree->t_magic == SMB_TREE_MAGIC);
8398670SJose.Borrego@Sun.COM 	ASSERT(dnode);
8408670SJose.Borrego@Sun.COM 	ASSERT(dnode->n_magic == SMB_NODE_MAGIC);
8418670SJose.Borrego@Sun.COM 
8428670SJose.Borrego@Sun.COM 	tree = sr->tid_tree;
8438670SJose.Borrego@Sun.COM 
8448670SJose.Borrego@Sun.COM 	if (smb_idpool_alloc(&tree->t_odid_pool, &odid)) {
8458670SJose.Borrego@Sun.COM 		smbsr_error(sr, NT_STATUS_TOO_MANY_OPENED_FILES,
8468670SJose.Borrego@Sun.COM 		    ERRDOS, ERROR_TOO_MANY_OPEN_FILES);
8478670SJose.Borrego@Sun.COM 		return (NULL);
8488670SJose.Borrego@Sun.COM 	}
8498670SJose.Borrego@Sun.COM 
8508670SJose.Borrego@Sun.COM 	od = kmem_cache_alloc(tree->t_server->si_cache_odir, KM_SLEEP);
8518670SJose.Borrego@Sun.COM 	bzero(od, sizeof (smb_odir_t));
8528670SJose.Borrego@Sun.COM 
8538670SJose.Borrego@Sun.COM 	mutex_init(&od->d_mutex, NULL, MUTEX_DEFAULT, NULL);
8549635SJoyce.McIntosh@Sun.COM 	od->d_refcnt = 0;
8558670SJose.Borrego@Sun.COM 	od->d_state = SMB_ODIR_STATE_OPEN;
8568670SJose.Borrego@Sun.COM 	od->d_magic = SMB_ODIR_MAGIC;
8578670SJose.Borrego@Sun.COM 	od->d_opened_by_pid = sr->smb_pid;
8588670SJose.Borrego@Sun.COM 	od->d_session = tree->t_session;
8599343SAfshin.Ardakani@Sun.COM 	od->d_cred = cr;
8608670SJose.Borrego@Sun.COM 	od->d_tree = tree;
8618670SJose.Borrego@Sun.COM 	od->d_dnode = dnode;
8628670SJose.Borrego@Sun.COM 	smb_node_ref(dnode);
8638670SJose.Borrego@Sun.COM 	od->d_odid = odid;
8648670SJose.Borrego@Sun.COM 	od->d_sattr = sattr;
8658670SJose.Borrego@Sun.COM 	(void) strlcpy(od->d_pattern, pattern, sizeof (od->d_pattern));
8669231SAfshin.Ardakani@Sun.COM 	od->d_flags = 0;
8679231SAfshin.Ardakani@Sun.COM 	if (smb_convert_wildcards(od->d_pattern) != 0)
8689231SAfshin.Ardakani@Sun.COM 		od->d_flags |= SMB_ODIR_FLAG_WILDCARDS;
8699231SAfshin.Ardakani@Sun.COM 	if (vfs_has_feature(dnode->vp->v_vfsp, VFSFT_DIRENTFLAGS))
8709231SAfshin.Ardakani@Sun.COM 		od->d_flags |= SMB_ODIR_FLAG_EDIRENT;
8719231SAfshin.Ardakani@Sun.COM 	if (smb_tree_has_feature(tree, SMB_TREE_CASEINSENSITIVE))
8729231SAfshin.Ardakani@Sun.COM 		od->d_flags |= SMB_ODIR_FLAG_IGNORE_CASE;
8739231SAfshin.Ardakani@Sun.COM 	if (SMB_TREE_SUPPORTS_CATIA(sr))
8749231SAfshin.Ardakani@Sun.COM 		od->d_flags |= SMB_ODIR_FLAG_CATIA;
87510504SKeyur.Desai@Sun.COM 	if (SMB_TREE_SUPPORTS_ABE(sr))
87610504SKeyur.Desai@Sun.COM 		od->d_flags |= SMB_ODIR_FLAG_ABE;
8778670SJose.Borrego@Sun.COM 	od->d_eof = B_FALSE;
8788670SJose.Borrego@Sun.COM 
8798670SJose.Borrego@Sun.COM 	smb_llist_enter(&tree->t_odir_list, RW_WRITER);
8808670SJose.Borrego@Sun.COM 	smb_llist_insert_tail(&tree->t_odir_list, od);
8818670SJose.Borrego@Sun.COM 	smb_llist_exit(&tree->t_odir_list);
8828670SJose.Borrego@Sun.COM 
8838670SJose.Borrego@Sun.COM 	atomic_inc_32(&tree->t_session->s_dir_cnt);
8848670SJose.Borrego@Sun.COM 	return (od);
8855331Samw }
8865331Samw 
8875331Samw /*
8885331Samw  * smb_odir_delete
8898670SJose.Borrego@Sun.COM  *
8908670SJose.Borrego@Sun.COM  * Removal of the odir from the tree's list of odirs must be
8918670SJose.Borrego@Sun.COM  * done before any resources associated with the odir are
8928670SJose.Borrego@Sun.COM  * released.
8935331Samw  */
8945331Samw static void
8958670SJose.Borrego@Sun.COM smb_odir_delete(smb_odir_t *od)
8965331Samw {
8975331Samw 	ASSERT(od);
8985331Samw 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
8995331Samw 	ASSERT(od->d_state == SMB_ODIR_STATE_CLOSED);
9005331Samw 
9015331Samw 	smb_llist_enter(&od->d_tree->t_odir_list, RW_WRITER);
9025331Samw 	smb_llist_remove(&od->d_tree->t_odir_list, od);
9035331Samw 	smb_llist_exit(&od->d_tree->t_odir_list);
9048670SJose.Borrego@Sun.COM 
9056139Sjb150015 	od->d_magic = 0;
9065331Samw 	atomic_dec_32(&od->d_tree->t_session->s_dir_cnt);
9078670SJose.Borrego@Sun.COM 	smb_node_release(od->d_dnode);
9088670SJose.Borrego@Sun.COM 	smb_idpool_free(&od->d_tree->t_odid_pool, od->d_odid);
9095331Samw 	mutex_destroy(&od->d_mutex);
9106139Sjb150015 	kmem_cache_free(od->d_tree->t_server->si_cache_odir, od);
9115331Samw }
9128670SJose.Borrego@Sun.COM 
9138670SJose.Borrego@Sun.COM /*
9148670SJose.Borrego@Sun.COM  * smb_odir_next_odirent
9158670SJose.Borrego@Sun.COM  *
9168670SJose.Borrego@Sun.COM  * Find the next directory entry in d_buf. If d_bufptr is NULL (buffer
9178670SJose.Borrego@Sun.COM  * is empty or we've reached the end of it), read the next set of
9188670SJose.Borrego@Sun.COM  * entries from the file system (vop_readdir).
9198670SJose.Borrego@Sun.COM  *
9208670SJose.Borrego@Sun.COM  * File systems which support VFSFT_EDIRENT_FLAGS will return the
9218670SJose.Borrego@Sun.COM  * directory entries as a buffer of edirent_t structure. Others will
9228670SJose.Borrego@Sun.COM  * return a buffer of dirent64_t structures.  For simplicity translate
9238670SJose.Borrego@Sun.COM  * the data into an smb_odirent_t structure.
9248670SJose.Borrego@Sun.COM  * The ed_name/d_name in d_buf is NULL terminated by the file system.
9258670SJose.Borrego@Sun.COM  *
9268670SJose.Borrego@Sun.COM  * Some file systems can have directories larger than SMB_MAXDIRSIZE.
9279231SAfshin.Ardakani@Sun.COM  * If the odirent offset >= SMB_MAXDIRSIZE return ENOENT and set d_eof
9289231SAfshin.Ardakani@Sun.COM  * to true to stop subsequent calls to smb_vop_readdir.
9298670SJose.Borrego@Sun.COM  *
9308670SJose.Borrego@Sun.COM  * Returns:
9318670SJose.Borrego@Sun.COM  *      0 - success. odirent is populated with the next directory entry
9328670SJose.Borrego@Sun.COM  * ENOENT - no more directory entries
9338670SJose.Borrego@Sun.COM  *  errno - error
9348670SJose.Borrego@Sun.COM  */
9358670SJose.Borrego@Sun.COM static int
9368670SJose.Borrego@Sun.COM smb_odir_next_odirent(smb_odir_t *od, smb_odirent_t *odirent)
9378670SJose.Borrego@Sun.COM {
9388670SJose.Borrego@Sun.COM 	int		rc;
9398670SJose.Borrego@Sun.COM 	int		reclen;
9408670SJose.Borrego@Sun.COM 	int		eof;
9418670SJose.Borrego@Sun.COM 	dirent64_t	*dp;
9428670SJose.Borrego@Sun.COM 	edirent_t	*edp;
9439231SAfshin.Ardakani@Sun.COM 	char		*np;
94410504SKeyur.Desai@Sun.COM 	uint32_t	abe_flag = 0;
9458670SJose.Borrego@Sun.COM 
9468670SJose.Borrego@Sun.COM 	ASSERT(MUTEX_HELD(&od->d_mutex));
9478670SJose.Borrego@Sun.COM 
9489437SJoyce.McIntosh@Sun.COM 	bzero(odirent, sizeof (smb_odirent_t));
9499437SJoyce.McIntosh@Sun.COM 
9508670SJose.Borrego@Sun.COM 	if (od->d_bufptr != NULL) {
9519231SAfshin.Ardakani@Sun.COM 		if (od->d_flags & SMB_ODIR_FLAG_EDIRENT)
9529231SAfshin.Ardakani@Sun.COM 			reclen = od->d_edp->ed_reclen;
9539231SAfshin.Ardakani@Sun.COM 		else
9549231SAfshin.Ardakani@Sun.COM 			reclen = od->d_dp->d_reclen;
9558670SJose.Borrego@Sun.COM 
9568670SJose.Borrego@Sun.COM 		if (reclen == 0) {
9578670SJose.Borrego@Sun.COM 			od->d_bufptr = NULL;
9588670SJose.Borrego@Sun.COM 		} else {
9598670SJose.Borrego@Sun.COM 			od->d_bufptr += reclen;
9608670SJose.Borrego@Sun.COM 			if (od->d_bufptr >= od->d_buf + od->d_bufsize)
9618670SJose.Borrego@Sun.COM 				od->d_bufptr = NULL;
9628670SJose.Borrego@Sun.COM 		}
9638670SJose.Borrego@Sun.COM 	}
9648670SJose.Borrego@Sun.COM 
9658670SJose.Borrego@Sun.COM 	if (od->d_bufptr == NULL) {
9668670SJose.Borrego@Sun.COM 		if (od->d_eof)
9678670SJose.Borrego@Sun.COM 			return (ENOENT);
9688670SJose.Borrego@Sun.COM 
9698670SJose.Borrego@Sun.COM 		od->d_bufsize = sizeof (od->d_buf);
9708670SJose.Borrego@Sun.COM 
97110504SKeyur.Desai@Sun.COM 		if (od->d_flags & SMB_ODIR_FLAG_ABE)
97210504SKeyur.Desai@Sun.COM 			abe_flag = SMB_ABE;
97310504SKeyur.Desai@Sun.COM 
9748670SJose.Borrego@Sun.COM 		rc = smb_vop_readdir(od->d_dnode->vp, od->d_offset,
97510504SKeyur.Desai@Sun.COM 		    od->d_buf, &od->d_bufsize, &eof, abe_flag, od->d_cred);
9768670SJose.Borrego@Sun.COM 
9778670SJose.Borrego@Sun.COM 		if ((rc == 0) && (od->d_bufsize == 0))
9788670SJose.Borrego@Sun.COM 			rc = ENOENT;
9798670SJose.Borrego@Sun.COM 
9808670SJose.Borrego@Sun.COM 		if (rc != 0) {
9818670SJose.Borrego@Sun.COM 			od->d_bufptr = NULL;
9828670SJose.Borrego@Sun.COM 			od->d_bufsize = 0;
9838670SJose.Borrego@Sun.COM 			return (rc);
9848670SJose.Borrego@Sun.COM 		}
9858670SJose.Borrego@Sun.COM 
9868670SJose.Borrego@Sun.COM 		od->d_eof = (eof != 0);
9878670SJose.Borrego@Sun.COM 		od->d_bufptr = od->d_buf;
9888670SJose.Borrego@Sun.COM 	}
9898670SJose.Borrego@Sun.COM 
9909231SAfshin.Ardakani@Sun.COM 	if (od->d_flags & SMB_ODIR_FLAG_EDIRENT)
9919231SAfshin.Ardakani@Sun.COM 		od->d_offset = od->d_edp->ed_off;
9929231SAfshin.Ardakani@Sun.COM 	else
9939231SAfshin.Ardakani@Sun.COM 		od->d_offset = od->d_dp->d_off;
9949231SAfshin.Ardakani@Sun.COM 
9958670SJose.Borrego@Sun.COM 	if (od->d_offset >= SMB_MAXDIRSIZE) {
9968670SJose.Borrego@Sun.COM 		od->d_bufptr = NULL;
9978670SJose.Borrego@Sun.COM 		od->d_bufsize = 0;
9989231SAfshin.Ardakani@Sun.COM 		od->d_eof = B_TRUE;
9998670SJose.Borrego@Sun.COM 		return (ENOENT);
10008670SJose.Borrego@Sun.COM 	}
10018670SJose.Borrego@Sun.COM 
10029231SAfshin.Ardakani@Sun.COM 	if (od->d_flags & SMB_ODIR_FLAG_EDIRENT) {
10038670SJose.Borrego@Sun.COM 		edp = od->d_edp;
10048670SJose.Borrego@Sun.COM 		odirent->od_ino = edp->ed_ino;
10058670SJose.Borrego@Sun.COM 		odirent->od_eflags = edp->ed_eflags;
10069231SAfshin.Ardakani@Sun.COM 		np = edp->ed_name;
10078670SJose.Borrego@Sun.COM 	} else {
10088670SJose.Borrego@Sun.COM 		dp = od->d_dp;
10098670SJose.Borrego@Sun.COM 		odirent->od_ino = dp->d_ino;
10108670SJose.Borrego@Sun.COM 		odirent->od_eflags = 0;
10119231SAfshin.Ardakani@Sun.COM 		np =  dp->d_name;
10129231SAfshin.Ardakani@Sun.COM 	}
10139231SAfshin.Ardakani@Sun.COM 
10149231SAfshin.Ardakani@Sun.COM 	if ((od->d_flags & SMB_ODIR_FLAG_CATIA) &&
10159231SAfshin.Ardakani@Sun.COM 	    ((od->d_flags & SMB_ODIR_FLAG_XATTR) == 0)) {
10169231SAfshin.Ardakani@Sun.COM 		smb_vop_catia_v4tov5(np, odirent->od_name,
10179231SAfshin.Ardakani@Sun.COM 		    sizeof (odirent->od_name));
10189231SAfshin.Ardakani@Sun.COM 	} else {
10199231SAfshin.Ardakani@Sun.COM 		(void) strlcpy(odirent->od_name, np,
10208670SJose.Borrego@Sun.COM 		    sizeof (odirent->od_name));
10218670SJose.Borrego@Sun.COM 	}
10228670SJose.Borrego@Sun.COM 
10238670SJose.Borrego@Sun.COM 	return (0);
10248670SJose.Borrego@Sun.COM }
10258670SJose.Borrego@Sun.COM 
10268670SJose.Borrego@Sun.COM /*
10278670SJose.Borrego@Sun.COM  * smb_odir_single_fileinfo
10288670SJose.Borrego@Sun.COM  *
10298670SJose.Borrego@Sun.COM  * Lookup the file identified by od->d_pattern.
10308670SJose.Borrego@Sun.COM  *
10318670SJose.Borrego@Sun.COM  * If the looked up file is a link, we attempt to lookup the link target
10328670SJose.Borrego@Sun.COM  * to use its attributes in place of those of the files's.
10338670SJose.Borrego@Sun.COM  * If we fail to lookup the target of the link we use the original
10348670SJose.Borrego@Sun.COM  * file's attributes.
10358670SJose.Borrego@Sun.COM  * Check if the attributes match the search attributes.
10368670SJose.Borrego@Sun.COM  *
10378670SJose.Borrego@Sun.COM  * Returns: 0 - success
10388670SJose.Borrego@Sun.COM  *     ENOENT - no match
10398670SJose.Borrego@Sun.COM  *      errno - error
10408670SJose.Borrego@Sun.COM  */
10418670SJose.Borrego@Sun.COM static int
10428670SJose.Borrego@Sun.COM smb_odir_single_fileinfo(smb_request_t *sr, smb_odir_t *od,
10438670SJose.Borrego@Sun.COM     smb_fileinfo_t *fileinfo)
10448670SJose.Borrego@Sun.COM {
10458670SJose.Borrego@Sun.COM 	int		rc;
10468670SJose.Borrego@Sun.COM 	smb_node_t	*fnode, *tgt_node;
104710001SJoyce.McIntosh@Sun.COM 	smb_attr_t	attr;
10488670SJose.Borrego@Sun.COM 	ino64_t		ino;
10498670SJose.Borrego@Sun.COM 	char		*name;
10509231SAfshin.Ardakani@Sun.COM 	boolean_t	case_conflict = B_FALSE;
10519231SAfshin.Ardakani@Sun.COM 	int		lookup_flags, flags = 0;
10529231SAfshin.Ardakani@Sun.COM 	vnode_t		*vp;
10538670SJose.Borrego@Sun.COM 
10548670SJose.Borrego@Sun.COM 	ASSERT(sr);
10558670SJose.Borrego@Sun.COM 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
10568670SJose.Borrego@Sun.COM 	ASSERT(od);
10578670SJose.Borrego@Sun.COM 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
10588670SJose.Borrego@Sun.COM 
10598670SJose.Borrego@Sun.COM 	ASSERT(MUTEX_HELD(&od->d_mutex));
10608670SJose.Borrego@Sun.COM 	bzero(fileinfo, sizeof (smb_fileinfo_t));
10618670SJose.Borrego@Sun.COM 
10629343SAfshin.Ardakani@Sun.COM 	rc = smb_fsop_lookup(sr, od->d_cred, 0, od->d_tree->t_snode,
106310001SJoyce.McIntosh@Sun.COM 	    od->d_dnode, od->d_pattern, &fnode);
10648670SJose.Borrego@Sun.COM 	if (rc != 0)
10658670SJose.Borrego@Sun.COM 		return (rc);
10668670SJose.Borrego@Sun.COM 
10679231SAfshin.Ardakani@Sun.COM 	/*
10689231SAfshin.Ardakani@Sun.COM 	 * If case sensitive, do a case insensitive smb_vop_lookup to
10699231SAfshin.Ardakani@Sun.COM 	 * check for case conflict
10709231SAfshin.Ardakani@Sun.COM 	 */
10719231SAfshin.Ardakani@Sun.COM 	if (od->d_flags & SMB_ODIR_FLAG_IGNORE_CASE) {
10729231SAfshin.Ardakani@Sun.COM 		lookup_flags = SMB_IGNORE_CASE;
10739231SAfshin.Ardakani@Sun.COM 		if (od->d_flags & SMB_ODIR_FLAG_CATIA)
10749231SAfshin.Ardakani@Sun.COM 			lookup_flags |= SMB_CATIA;
10758670SJose.Borrego@Sun.COM 
10769231SAfshin.Ardakani@Sun.COM 		rc = smb_vop_lookup(od->d_dnode->vp, fnode->od_name, &vp,
10779231SAfshin.Ardakani@Sun.COM 		    NULL, lookup_flags, &flags, od->d_tree->t_snode->vp,
10789343SAfshin.Ardakani@Sun.COM 		    od->d_cred);
10799231SAfshin.Ardakani@Sun.COM 		if (rc != 0)
10809231SAfshin.Ardakani@Sun.COM 			return (rc);
10819231SAfshin.Ardakani@Sun.COM 		VN_RELE(vp);
10829231SAfshin.Ardakani@Sun.COM 
10839231SAfshin.Ardakani@Sun.COM 		if (flags & ED_CASE_CONFLICT)
10849231SAfshin.Ardakani@Sun.COM 			case_conflict = B_TRUE;
10859231SAfshin.Ardakani@Sun.COM 	}
10869231SAfshin.Ardakani@Sun.COM 
108710001SJoyce.McIntosh@Sun.COM 	if ((rc = smb_node_getattr(sr, fnode, &attr)) != 0) {
108810001SJoyce.McIntosh@Sun.COM 		smb_node_release(fnode);
108910001SJoyce.McIntosh@Sun.COM 		return (rc);
109010001SJoyce.McIntosh@Sun.COM 	}
109110001SJoyce.McIntosh@Sun.COM 
10929231SAfshin.Ardakani@Sun.COM 	ino = attr.sa_vattr.va_nodeid;
10939231SAfshin.Ardakani@Sun.COM 	(void) smb_mangle_name(ino, fnode->od_name,
10949231SAfshin.Ardakani@Sun.COM 	    fileinfo->fi_shortname, fileinfo->fi_name83, case_conflict);
10959231SAfshin.Ardakani@Sun.COM 	name = (case_conflict) ? fileinfo->fi_shortname : fnode->od_name;
10968670SJose.Borrego@Sun.COM 	(void) strlcpy(fileinfo->fi_name, name, sizeof (fileinfo->fi_name));
10978670SJose.Borrego@Sun.COM 
10988670SJose.Borrego@Sun.COM 	/* follow link to get target node & attr */
10998670SJose.Borrego@Sun.COM 	if ((fnode->vp->v_type == VLNK) &&
110010001SJoyce.McIntosh@Sun.COM 	    (smb_odir_lookup_link(sr, od, fnode->od_name, &tgt_node))) {
11018670SJose.Borrego@Sun.COM 		smb_node_release(fnode);
11028670SJose.Borrego@Sun.COM 		fnode = tgt_node;
110310001SJoyce.McIntosh@Sun.COM 		if ((rc = smb_node_getattr(sr, fnode, &attr)) != 0) {
110410001SJoyce.McIntosh@Sun.COM 			smb_node_release(fnode);
110510001SJoyce.McIntosh@Sun.COM 			return (rc);
110610001SJoyce.McIntosh@Sun.COM 		}
11078670SJose.Borrego@Sun.COM 	}
11088670SJose.Borrego@Sun.COM 
11098670SJose.Borrego@Sun.COM 	/* check search attributes */
111010001SJoyce.McIntosh@Sun.COM 	if (!smb_sattr_check(attr.sa_dosattr, od->d_sattr)) {
11118670SJose.Borrego@Sun.COM 		smb_node_release(fnode);
11128670SJose.Borrego@Sun.COM 		return (ENOENT);
11138670SJose.Borrego@Sun.COM 	}
11148670SJose.Borrego@Sun.COM 
111510001SJoyce.McIntosh@Sun.COM 	fileinfo->fi_dosattr = attr.sa_dosattr;
111610001SJoyce.McIntosh@Sun.COM 	fileinfo->fi_nodeid = attr.sa_vattr.va_nodeid;
111710001SJoyce.McIntosh@Sun.COM 	fileinfo->fi_size = attr.sa_vattr.va_size;
111810504SKeyur.Desai@Sun.COM 	fileinfo->fi_alloc_size = attr.sa_allocsz;
111910001SJoyce.McIntosh@Sun.COM 	fileinfo->fi_atime = attr.sa_vattr.va_atime;
112010001SJoyce.McIntosh@Sun.COM 	fileinfo->fi_mtime = attr.sa_vattr.va_mtime;
112110001SJoyce.McIntosh@Sun.COM 	fileinfo->fi_ctime = attr.sa_vattr.va_ctime;
112210001SJoyce.McIntosh@Sun.COM 	if (attr.sa_crtime.tv_sec)
112310001SJoyce.McIntosh@Sun.COM 		fileinfo->fi_crtime = attr.sa_crtime;
11248670SJose.Borrego@Sun.COM 	else
112510001SJoyce.McIntosh@Sun.COM 		fileinfo->fi_crtime = attr.sa_vattr.va_mtime;
11268670SJose.Borrego@Sun.COM 
11278670SJose.Borrego@Sun.COM 	smb_node_release(fnode);
11288670SJose.Borrego@Sun.COM 	return (0);
11298670SJose.Borrego@Sun.COM }
11308670SJose.Borrego@Sun.COM 
11318670SJose.Borrego@Sun.COM /*
11328670SJose.Borrego@Sun.COM  * smb_odir_wildcard_fileinfo
11338670SJose.Borrego@Sun.COM  *
11348670SJose.Borrego@Sun.COM  * odirent contains a directory entry, obtained from a vop_readdir.
11358670SJose.Borrego@Sun.COM  * If a case conflict is identified the filename is mangled and the
11368670SJose.Borrego@Sun.COM  * shortname is used as 'name', in place of odirent->od_name. This
11378670SJose.Borrego@Sun.COM  * name will be used in the smb_fsop_lookup because smb_fsop_lookup
11388670SJose.Borrego@Sun.COM  * performs a case insensitive lookup if the tree is case insesitive,
11398670SJose.Borrego@Sun.COM  * so the mangled name is required in the case conflict scenario to
11408670SJose.Borrego@Sun.COM  * ensure the correct match.
11418670SJose.Borrego@Sun.COM  *
11428670SJose.Borrego@Sun.COM  * If the looked up file is a link, we attempt to lookup the link target
11438670SJose.Borrego@Sun.COM  * to use its attributes in place of those of the files's.
11448670SJose.Borrego@Sun.COM  * If we fail to lookup the target of the link we use the original
11458670SJose.Borrego@Sun.COM  * file's attributes.
11468670SJose.Borrego@Sun.COM  * Check if the attributes match the search attributes.
11478670SJose.Borrego@Sun.COM  *
11488670SJose.Borrego@Sun.COM  * Although some file systems can have directories larger than
11498670SJose.Borrego@Sun.COM  * SMB_MAXDIRSIZE smb_odir_next_odirent ensures that no offset larger
11508670SJose.Borrego@Sun.COM  * than SMB_MAXDIRSIZE is returned.  It is therefore safe to use the
11518670SJose.Borrego@Sun.COM  * offset as the cookie (uint32_t).
11528670SJose.Borrego@Sun.COM  *
11538670SJose.Borrego@Sun.COM  * Returns: 0 - success
11548670SJose.Borrego@Sun.COM  *     ENOENT - no match, proceed to next entry
11558670SJose.Borrego@Sun.COM  *      errno - error
11568670SJose.Borrego@Sun.COM  */
11578670SJose.Borrego@Sun.COM static int
11588670SJose.Borrego@Sun.COM smb_odir_wildcard_fileinfo(smb_request_t *sr, smb_odir_t *od,
11598670SJose.Borrego@Sun.COM     smb_odirent_t *odirent, smb_fileinfo_t *fileinfo)
11608670SJose.Borrego@Sun.COM {
11618670SJose.Borrego@Sun.COM 	int		rc;
11628670SJose.Borrego@Sun.COM 	smb_node_t	*fnode, *tgt_node;
116310001SJoyce.McIntosh@Sun.COM 	smb_attr_t	attr;
11648670SJose.Borrego@Sun.COM 	char		*name;
11658670SJose.Borrego@Sun.COM 	boolean_t	case_conflict;
11668670SJose.Borrego@Sun.COM 
11678670SJose.Borrego@Sun.COM 	ASSERT(sr);
11688670SJose.Borrego@Sun.COM 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
11698670SJose.Borrego@Sun.COM 	ASSERT(od);
11708670SJose.Borrego@Sun.COM 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
11718670SJose.Borrego@Sun.COM 
11728670SJose.Borrego@Sun.COM 	ASSERT(MUTEX_HELD(&od->d_mutex));
11738670SJose.Borrego@Sun.COM 	bzero(fileinfo, sizeof (smb_fileinfo_t));
11748670SJose.Borrego@Sun.COM 
11759231SAfshin.Ardakani@Sun.COM 	case_conflict = ((od->d_flags & SMB_ODIR_FLAG_IGNORE_CASE) &&
11768670SJose.Borrego@Sun.COM 	    (odirent->od_eflags & ED_CASE_CONFLICT));
11778670SJose.Borrego@Sun.COM 	(void) smb_mangle_name(odirent->od_ino, odirent->od_name,
11788670SJose.Borrego@Sun.COM 	    fileinfo->fi_shortname, fileinfo->fi_name83, case_conflict);
11798670SJose.Borrego@Sun.COM 	name = (case_conflict) ? fileinfo->fi_shortname : odirent->od_name;
11808670SJose.Borrego@Sun.COM 	(void) strlcpy(fileinfo->fi_name, name, sizeof (fileinfo->fi_name));
11818670SJose.Borrego@Sun.COM 
11829343SAfshin.Ardakani@Sun.COM 	rc = smb_fsop_lookup(sr, od->d_cred, 0, od->d_tree->t_snode,
118310001SJoyce.McIntosh@Sun.COM 	    od->d_dnode, name, &fnode);
11848670SJose.Borrego@Sun.COM 	if (rc != 0)
11858670SJose.Borrego@Sun.COM 		return (rc);
11868670SJose.Borrego@Sun.COM 
11878670SJose.Borrego@Sun.COM 	/* follow link to get target node & attr */
11888670SJose.Borrego@Sun.COM 	if ((fnode->vp->v_type == VLNK) &&
118910001SJoyce.McIntosh@Sun.COM 	    (smb_odir_lookup_link(sr, od, name, &tgt_node))) {
11908670SJose.Borrego@Sun.COM 		smb_node_release(fnode);
11918670SJose.Borrego@Sun.COM 		fnode = tgt_node;
119210001SJoyce.McIntosh@Sun.COM 	}
119310001SJoyce.McIntosh@Sun.COM 
119410001SJoyce.McIntosh@Sun.COM 	if ((rc = smb_node_getattr(sr, fnode, &attr)) != 0) {
119510001SJoyce.McIntosh@Sun.COM 		smb_node_release(fnode);
119610001SJoyce.McIntosh@Sun.COM 		return (rc);
11978670SJose.Borrego@Sun.COM 	}
11988670SJose.Borrego@Sun.COM 
11998670SJose.Borrego@Sun.COM 	/* check search attributes */
120010001SJoyce.McIntosh@Sun.COM 	if (!smb_sattr_check(attr.sa_dosattr, od->d_sattr)) {
12018670SJose.Borrego@Sun.COM 		smb_node_release(fnode);
12028670SJose.Borrego@Sun.COM 		return (ENOENT);
12038670SJose.Borrego@Sun.COM 	}
12048670SJose.Borrego@Sun.COM 
12058670SJose.Borrego@Sun.COM 	fileinfo->fi_cookie = (uint32_t)od->d_offset;
120610001SJoyce.McIntosh@Sun.COM 	fileinfo->fi_dosattr = attr.sa_dosattr;
120710001SJoyce.McIntosh@Sun.COM 	fileinfo->fi_nodeid = attr.sa_vattr.va_nodeid;
120810001SJoyce.McIntosh@Sun.COM 	fileinfo->fi_size = attr.sa_vattr.va_size;
120910504SKeyur.Desai@Sun.COM 	fileinfo->fi_alloc_size = attr.sa_allocsz;
121010001SJoyce.McIntosh@Sun.COM 	fileinfo->fi_atime = attr.sa_vattr.va_atime;
121110001SJoyce.McIntosh@Sun.COM 	fileinfo->fi_mtime = attr.sa_vattr.va_mtime;
121210001SJoyce.McIntosh@Sun.COM 	fileinfo->fi_ctime = attr.sa_vattr.va_ctime;
121310001SJoyce.McIntosh@Sun.COM 	if (attr.sa_crtime.tv_sec)
121410001SJoyce.McIntosh@Sun.COM 		fileinfo->fi_crtime = attr.sa_crtime;
12158670SJose.Borrego@Sun.COM 	else
121610001SJoyce.McIntosh@Sun.COM 		fileinfo->fi_crtime = attr.sa_vattr.va_mtime;
12178670SJose.Borrego@Sun.COM 
12188670SJose.Borrego@Sun.COM 	smb_node_release(fnode);
12198670SJose.Borrego@Sun.COM 	return (0);
12208670SJose.Borrego@Sun.COM }
12218670SJose.Borrego@Sun.COM 
12228670SJose.Borrego@Sun.COM /*
12238670SJose.Borrego@Sun.COM  * smb_odir_lookup_link
12248670SJose.Borrego@Sun.COM  *
12258670SJose.Borrego@Sun.COM  * If the file is a symlink we lookup the object to which the
12268670SJose.Borrego@Sun.COM  * symlink refers so that we can return its attributes.
12278670SJose.Borrego@Sun.COM  * This can cause a problem if a symlink in a sub-directory
12288670SJose.Borrego@Sun.COM  * points to a parent directory (some UNIX GUI's create a symlink
12298670SJose.Borrego@Sun.COM  * in $HOME/.desktop that points to the user's home directory).
12308670SJose.Borrego@Sun.COM  * Some Windows applications (e.g. virus scanning) loop/hang
12318670SJose.Borrego@Sun.COM  * trying to follow this recursive path and there is little
12328670SJose.Borrego@Sun.COM  * we can do because the path is constructed on the client.
12338670SJose.Borrego@Sun.COM  * smb_dirsymlink_enable allows an end-user to disable
12348670SJose.Borrego@Sun.COM  * symlinks to directories. Symlinks to other object types
12358670SJose.Borrego@Sun.COM  * should be unaffected.
12368670SJose.Borrego@Sun.COM  *
12378670SJose.Borrego@Sun.COM  * Returns:  B_TRUE - followed link. tgt_node and tgt_attr set
12388670SJose.Borrego@Sun.COM  *          B_FALSE - link not followed
12398670SJose.Borrego@Sun.COM  */
12408670SJose.Borrego@Sun.COM static boolean_t
12418670SJose.Borrego@Sun.COM smb_odir_lookup_link(smb_request_t *sr, smb_odir_t *od,
124210001SJoyce.McIntosh@Sun.COM     char *fname, smb_node_t **tgt_node)
12438670SJose.Borrego@Sun.COM {
12448670SJose.Borrego@Sun.COM 	int rc;
12458670SJose.Borrego@Sun.COM 
12469343SAfshin.Ardakani@Sun.COM 	rc = smb_fsop_lookup(sr, od->d_cred, SMB_FOLLOW_LINKS,
124710001SJoyce.McIntosh@Sun.COM 	    od->d_tree->t_snode, od->d_dnode, fname, tgt_node);
12488670SJose.Borrego@Sun.COM 	if (rc != 0) {
12498670SJose.Borrego@Sun.COM 		*tgt_node = NULL;
12508670SJose.Borrego@Sun.COM 		return (B_FALSE);
12518670SJose.Borrego@Sun.COM 	}
12528670SJose.Borrego@Sun.COM 
125310001SJoyce.McIntosh@Sun.COM 	if (smb_node_is_dir(*tgt_node) && (!smb_dirsymlink_enable)) {
12548670SJose.Borrego@Sun.COM 		smb_node_release(*tgt_node);
12558670SJose.Borrego@Sun.COM 		*tgt_node = NULL;
12568670SJose.Borrego@Sun.COM 		return (B_FALSE);
12578670SJose.Borrego@Sun.COM 	}
12588670SJose.Borrego@Sun.COM 
12598670SJose.Borrego@Sun.COM 	return (B_TRUE);
12608670SJose.Borrego@Sun.COM }
1261