xref: /onnv-gate/usr/src/uts/common/fs/smbsrv/smb_odir.c (revision 9437:89120cfa73eb)
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  *    +-------------------------+
745331Samw  *		    |
758670SJose.Borrego@Sun.COM  *		    | close
768670SJose.Borrego@Sun.COM  *		    |
778670SJose.Borrego@Sun.COM  *		    v
788670SJose.Borrego@Sun.COM  *    +-------------------------+
798670SJose.Borrego@Sun.COM  *    | SMB_ODIR_STATE_CLOSING  |
808670SJose.Borrego@Sun.COM  *    +-------------------------+
818670SJose.Borrego@Sun.COM  *		    |
828670SJose.Borrego@Sun.COM  *		    | last release
835331Samw  *		    |
845331Samw  *		    v
855331Samw  *    +-------------------------+
868670SJose.Borrego@Sun.COM  *    | SMB_ODIR_STATE_CLOSED   |----------> deletion
875331Samw  *    +-------------------------+
885331Samw  *
898670SJose.Borrego@Sun.COM  *
905331Samw  * SMB_ODIR_STATE_OPEN
918670SJose.Borrego@Sun.COM  * - the odir exists in the list of odirs of its tree.
928670SJose.Borrego@Sun.COM  * - references will be given out if the odir is looked up
938670SJose.Borrego@Sun.COM  * - if a close is received the odir will transition to
948670SJose.Borrego@Sun.COM  *   SMB_ODIR_STATE_CLOSING.
955331Samw  *
965331Samw  * SMB_ODIR_STATE_CLOSING
978670SJose.Borrego@Sun.COM  * - the odir exists in the list of odirs of its tree.
988670SJose.Borrego@Sun.COM  * - references will NOT be given out if the odir is looked up.
998670SJose.Borrego@Sun.COM  * - when the last reference is released (refcnt == 0) the
1008670SJose.Borrego@Sun.COM  *   odir will transition to SMB_ODIR_STATE_CLOSED.
1015331Samw  *
1025331Samw  * SMB_ODIR_STATE_CLOSED
1038670SJose.Borrego@Sun.COM  * - the odir exists in the list of odirs of its tree.
1048670SJose.Borrego@Sun.COM  * - there are no users of the odir (refcnt == 0)
1058670SJose.Borrego@Sun.COM  * - references will NOT be given out if the odir is looked up.
1068670SJose.Borrego@Sun.COM  * - the odir is being removed from the tree's list and deleted.
1075331Samw  *
1085331Samw  * Comments
1095331Samw  * --------
1105331Samw  *    The state machine of the odir structures is controlled by 3 elements:
1115331Samw  *      - The list of odirs of the tree it belongs to.
1125331Samw  *      - The mutex embedded in the structure itself.
1135331Samw  *      - The reference count.
1145331Samw  *
1155331Samw  *    There's a mutex embedded in the odir structure used to protect its fields
1165331Samw  *    and there's a lock embedded in the list of odirs of a tree. To
1175331Samw  *    increment or to decrement the reference count the mutex must be entered.
1185331Samw  *    To insert the odir into the list of odirs of the tree and to remove
1195331Samw  *    the odir from it, the lock must be entered in RW_WRITER mode.
1205331Samw  *
1218670SJose.Borrego@Sun.COM  *    In order to avoid deadlocks, when both (mutex and lock of the odir
1228670SJose.Borrego@Sun.COM  *    list) have to be entered, the lock must be entered first.
1238670SJose.Borrego@Sun.COM  *
1248670SJose.Borrego@Sun.COM  *
1258670SJose.Borrego@Sun.COM  * Odir Interface
1268670SJose.Borrego@Sun.COM  * ---------------
1278670SJose.Borrego@Sun.COM  * odid = smb_odir_open(pathname)
1288670SJose.Borrego@Sun.COM  *	Create an odir representing the directory specified in pathname and
1298670SJose.Borrego@Sun.COM  *	add it into the tree's list of odirs.
1308670SJose.Borrego@Sun.COM  *	Return an identifier (odid) uniquely identifying the created odir.
1315331Samw  *
1328670SJose.Borrego@Sun.COM  * smb_odir_openat(smb_node_t *unode)
1338670SJose.Borrego@Sun.COM  *	Create an odir representing the extended attribute directory
1348670SJose.Borrego@Sun.COM  *	associated with the file (or directory) represented by unode
1358670SJose.Borrego@Sun.COM  *	and add it into the tree's list of odirs.
1368670SJose.Borrego@Sun.COM  *	Return an identifier (odid) uniquely identifying the created odir.
1378670SJose.Borrego@Sun.COM  *
1388670SJose.Borrego@Sun.COM  * smb_odir_t *odir = smb_tree_lookup_odir(odid)
1398670SJose.Borrego@Sun.COM  *	Find the odir corresponding to the specified odid in the tree's
1408670SJose.Borrego@Sun.COM  *	list of odirs.
1418670SJose.Borrego@Sun.COM  *
1428670SJose.Borrego@Sun.COM  * smb_odir_read(..., smb_odirent_t *odirent)
1438670SJose.Borrego@Sun.COM  *	Find the next directory entry in the odir and return it in odirent.
1445331Samw  *
1458670SJose.Borrego@Sun.COM  * smb_odir_read_fileinfo(..., smb_fileinfo_t *)
1468670SJose.Borrego@Sun.COM  *	Find the next directory entry in the odir. Return the details of
1478670SJose.Borrego@Sun.COM  *	the directory entry in smb_fileinfo_t. (See odir internals below)
1488670SJose.Borrego@Sun.COM  *
1498670SJose.Borrego@Sun.COM  * smb_odir_read_stream_info(..., smb_streaminfo_t *)
1508670SJose.Borrego@Sun.COM  *	Find the next named stream entry in the odir. Return the details of
1518670SJose.Borrego@Sun.COM  *	the named stream in smb_streaminfo_t.
1528670SJose.Borrego@Sun.COM  *
1538670SJose.Borrego@Sun.COM  * smb_odir_release(smb_odir_t *odir)
1548670SJose.Borrego@Sun.COM  *	Release the hold on the odir, obtained by lookup.
1558670SJose.Borrego@Sun.COM  *
1568670SJose.Borrego@Sun.COM  * smb_odir_close(smb_odir_t *odir)
1578670SJose.Borrego@Sun.COM  *	Close the odir and remove it from the tree's list of odirs.
1585331Samw  *
1598670SJose.Borrego@Sun.COM  *
1608670SJose.Borrego@Sun.COM  * Odir Internals
1618670SJose.Borrego@Sun.COM  * --------------
1628670SJose.Borrego@Sun.COM  * The odir object represent an open directory search. Each read operation
1638670SJose.Borrego@Sun.COM  * provides the caller with a structure containing information  pertaining
1648670SJose.Borrego@Sun.COM  * to the next directory entry that matches the search criteria, namely
1658670SJose.Borrego@Sun.COM  * the filename or match pattern and, in the case of smb_odir_read_fileinfo(),
1668670SJose.Borrego@Sun.COM  * the search attributes.
1678670SJose.Borrego@Sun.COM  *
1688670SJose.Borrego@Sun.COM  * The odir maintains a buffer (d_buf) of directory entries read from
1698670SJose.Borrego@Sun.COM  * the filesystem via a vop_readdir. The buffer is populated when a read
1708670SJose.Borrego@Sun.COM  * request (smb_odir_next_odirent) finds that the buffer is empty or that
1718670SJose.Borrego@Sun.COM  * the end of the buffer has been reached, and also when a new client request
1728670SJose.Borrego@Sun.COM  * (find next) begins.
1735331Samw  *
1748670SJose.Borrego@Sun.COM  * The data in d_buf (that which is returned from the file system) can
1758670SJose.Borrego@Sun.COM  * be in one of two formats. If the file system supports extended directory
1768670SJose.Borrego@Sun.COM  * entries we request that the data be returned as edirent_t structures. If
1778670SJose.Borrego@Sun.COM  * it does not the data will be returned as dirent64_t structures. For
1788670SJose.Borrego@Sun.COM  * convenience, when the next directory entry is read from d_buf by
1798670SJose.Borrego@Sun.COM  * smb_odir_next_odirent it is translated into an smb_odirent_t.
1808670SJose.Borrego@Sun.COM  *
1818670SJose.Borrego@Sun.COM  * smb_odir_read_fileinfo
1828670SJose.Borrego@Sun.COM  * The processing required to obtain the information to populate the caller's
1838670SJose.Borrego@Sun.COM  * smb_fileinfo_t differs depending upon whether the directory search is for a
1848670SJose.Borrego@Sun.COM  * single specified filename or for multiple files matching a search pattern.
1858670SJose.Borrego@Sun.COM  * Thus smb_odir_read_fileinfo uses two static functions:
1868670SJose.Borrego@Sun.COM  * smb_odir_single_fileinfo - obtains the smb_fileinfo_t info for the single
1878670SJose.Borrego@Sun.COM  * filename as specified in smb_odir_open request.
1888670SJose.Borrego@Sun.COM  * smb_odir_wildcard_fileinfo - obtains the smb_fileinfo_t info for the filename
1898670SJose.Borrego@Sun.COM  * returned from the smb_odir_next_odirent. This is called in a loop until
1908670SJose.Borrego@Sun.COM  * an entry matching the search criteria is found or no more entries exist.
1918670SJose.Borrego@Sun.COM  *
1928670SJose.Borrego@Sun.COM  * If a directory entry is a VLNK, the name returned in the smb_fileinfo_t
1938670SJose.Borrego@Sun.COM  * is the name of the directory entry but the attributes are the attribites
1948670SJose.Borrego@Sun.COM  * of the file that is the target of the link. If the link target cannot
1958670SJose.Borrego@Sun.COM  * be found the attributes returned are the attributes of the link itself.
1965331Samw  *
1978670SJose.Borrego@Sun.COM  * smb_odir_read_stream_info
1988670SJose.Borrego@Sun.COM  * In order for an odir to provide information about stream files it
1998670SJose.Borrego@Sun.COM  * must be opened with smb_odir_openat(). smb_odir_read_streaminfo() can
2008670SJose.Borrego@Sun.COM  * then be used to obtain the name and size of named stream files.
2015331Samw  *
2028670SJose.Borrego@Sun.COM  * Resuming a Search
2038670SJose.Borrego@Sun.COM  * -----------------
2048670SJose.Borrego@Sun.COM  * A directory search often consists of multiple client requests: an initial
2058670SJose.Borrego@Sun.COM  * find_first request followed by zero or more find_next requests and a
2068670SJose.Borrego@Sun.COM  * find_close request.
2078670SJose.Borrego@Sun.COM  * The find_first request will open and lookup the odir, read its desired
2088670SJose.Borrego@Sun.COM  * number of entries from the odir, then release the odir and return.
2098670SJose.Borrego@Sun.COM  * A find_next request will lookup the odir and read its desired number of
2108670SJose.Borrego@Sun.COM  * entries from the odir, then release the odir and return.
2118670SJose.Borrego@Sun.COM  * At the end of the search the find_close request will close the odir.
2128670SJose.Borrego@Sun.COM  *
2138670SJose.Borrego@Sun.COM  * In order to be able to resume a directory search (find_next) the odir
2148670SJose.Borrego@Sun.COM  * provides the capability for the caller to save one or more resume points
2158670SJose.Borrego@Sun.COM  * (cookies) at the end of a request, and to specify which resume point
2168670SJose.Borrego@Sun.COM  * (cookie) to restart from at the beginning of the next search.
2178670SJose.Borrego@Sun.COM  *	smb_odir_save_cookie(..., cookie)
2188670SJose.Borrego@Sun.COM  *	smb_odir_resume_at(smb_odir_resume_t *resume)
2198670SJose.Borrego@Sun.COM  * A search can be resumed at a specified resume point (cookie), the resume
2208670SJose.Borrego@Sun.COM  * point (cookie) stored at a specified index in the d_cookies array, or
2218670SJose.Borrego@Sun.COM  * a specified filename. The latter (specified filename) is not yet supported.
2228670SJose.Borrego@Sun.COM  *
2238670SJose.Borrego@Sun.COM  * See smb_search, smb_find, smb_find_unique, and smb_trans2_find for details
2245331Samw  */
2258670SJose.Borrego@Sun.COM 
2265331Samw #include <smbsrv/smb_incl.h>
2275331Samw #include <smbsrv/smb_kproto.h>
2285331Samw #include <smbsrv/smb_fsops.h>
2298670SJose.Borrego@Sun.COM #include <sys/extdirent.h>
2305331Samw 
2318670SJose.Borrego@Sun.COM /* static functions */
2328670SJose.Borrego@Sun.COM static smb_odir_t *smb_odir_create(smb_request_t *, smb_node_t *,
2339343SAfshin.Ardakani@Sun.COM     char *, uint16_t, cred_t *);
2348670SJose.Borrego@Sun.COM static void smb_odir_delete(smb_odir_t *);
2358670SJose.Borrego@Sun.COM static int smb_odir_single_fileinfo(smb_request_t *, smb_odir_t *,
2368670SJose.Borrego@Sun.COM     smb_fileinfo_t *);
2378670SJose.Borrego@Sun.COM static int smb_odir_wildcard_fileinfo(smb_request_t *, smb_odir_t *,
2388670SJose.Borrego@Sun.COM     smb_odirent_t *, smb_fileinfo_t *);
2398670SJose.Borrego@Sun.COM static int smb_odir_next_odirent(smb_odir_t *, smb_odirent_t *);
2408670SJose.Borrego@Sun.COM static boolean_t smb_odir_lookup_link(smb_request_t *, smb_odir_t *, char *,
2418670SJose.Borrego@Sun.COM     smb_node_t **, smb_attr_t *);
2425331Samw 
2435331Samw 
2445331Samw /*
2455331Samw  * smb_odir_open
2468670SJose.Borrego@Sun.COM  *
2478670SJose.Borrego@Sun.COM  * Create an odir representing the directory specified in pathname.
2488670SJose.Borrego@Sun.COM  *
2498670SJose.Borrego@Sun.COM  * Returns:
2508670SJose.Borrego@Sun.COM  * odid - Unique identifier of newly created odir.
2518670SJose.Borrego@Sun.COM  *    0 - error, error details set in sr.
2525331Samw  */
2538670SJose.Borrego@Sun.COM uint16_t
2549343SAfshin.Ardakani@Sun.COM smb_odir_open(smb_request_t *sr, char *path, uint16_t sattr, uint32_t flags)
2555331Samw {
2568670SJose.Borrego@Sun.COM 	int		rc;
2578670SJose.Borrego@Sun.COM 	smb_tree_t	*tree;
2588670SJose.Borrego@Sun.COM 	smb_node_t	*dnode;
2598670SJose.Borrego@Sun.COM 	char		pattern[MAXNAMELEN];
2608670SJose.Borrego@Sun.COM 	smb_odir_t 	*od;
2619343SAfshin.Ardakani@Sun.COM 	cred_t		*cr;
2625331Samw 
2638670SJose.Borrego@Sun.COM 	ASSERT(sr);
2648670SJose.Borrego@Sun.COM 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
2658670SJose.Borrego@Sun.COM 	ASSERT(sr->tid_tree);
2668670SJose.Borrego@Sun.COM 	ASSERT(sr->tid_tree->t_magic == SMB_TREE_MAGIC);
2675331Samw 
2688670SJose.Borrego@Sun.COM 	tree = sr->tid_tree;
2698670SJose.Borrego@Sun.COM 
2708670SJose.Borrego@Sun.COM 	rc = smb_pathname_reduce(sr, sr->user_cr, path,
2718670SJose.Borrego@Sun.COM 	    tree->t_snode, tree->t_snode, &dnode, pattern);
2728670SJose.Borrego@Sun.COM 	if (rc != 0) {
2738670SJose.Borrego@Sun.COM 		smbsr_errno(sr, rc);
2748670SJose.Borrego@Sun.COM 		return (0);
2755331Samw 	}
2765331Samw 
2778670SJose.Borrego@Sun.COM 	if (dnode->vp->v_type != VDIR) {
2788670SJose.Borrego@Sun.COM 		smbsr_error(sr, NT_STATUS_OBJECT_PATH_NOT_FOUND,
2798670SJose.Borrego@Sun.COM 		    ERRDOS, ERROR_PATH_NOT_FOUND);
2808670SJose.Borrego@Sun.COM 		smb_node_release(dnode);
2818670SJose.Borrego@Sun.COM 		return (0);
2825331Samw 	}
2835331Samw 
2848670SJose.Borrego@Sun.COM 	if (smb_fsop_access(sr, sr->user_cr, dnode, FILE_LIST_DIRECTORY) != 0) {
2858670SJose.Borrego@Sun.COM 		smbsr_error(sr, NT_STATUS_ACCESS_DENIED,
2868670SJose.Borrego@Sun.COM 		    ERRDOS, ERROR_ACCESS_DENIED);
2878670SJose.Borrego@Sun.COM 		smb_node_release(dnode);
2888670SJose.Borrego@Sun.COM 		return (0);
2898670SJose.Borrego@Sun.COM 	}
2905331Samw 
2919343SAfshin.Ardakani@Sun.COM 	if (flags & SMB_ODIR_OPENF_BACKUP_INTENT)
2929343SAfshin.Ardakani@Sun.COM 		cr = smb_user_getprivcred(tree->t_user);
2939343SAfshin.Ardakani@Sun.COM 	else
2949343SAfshin.Ardakani@Sun.COM 		cr = tree->t_user->u_cred;
2959343SAfshin.Ardakani@Sun.COM 
2969343SAfshin.Ardakani@Sun.COM 	od = smb_odir_create(sr, dnode, pattern, sattr, cr);
2978670SJose.Borrego@Sun.COM 	smb_node_release(dnode);
2988670SJose.Borrego@Sun.COM 	return (od ? od->d_odid : 0);
2995331Samw }
3005331Samw 
3015331Samw /*
3028670SJose.Borrego@Sun.COM  * smb_odir_openat
3038670SJose.Borrego@Sun.COM  *
3048670SJose.Borrego@Sun.COM  * Create an odir representing the extended attribute directory
3058670SJose.Borrego@Sun.COM  * associated with the file (or directory) represented by unode.
3068670SJose.Borrego@Sun.COM  *
3078670SJose.Borrego@Sun.COM  * Returns:
3088670SJose.Borrego@Sun.COM  * odid - Unique identifier of newly created odir.
3098670SJose.Borrego@Sun.COM  *    0 - error, error details set in sr.
3108670SJose.Borrego@Sun.COM  */
3118670SJose.Borrego@Sun.COM uint16_t
3128670SJose.Borrego@Sun.COM smb_odir_openat(smb_request_t *sr, smb_node_t *unode)
3138670SJose.Borrego@Sun.COM {
3148670SJose.Borrego@Sun.COM 	int		rc;
3158670SJose.Borrego@Sun.COM 	vnode_t		*xattr_dvp;
3168670SJose.Borrego@Sun.COM 	smb_odir_t	*od;
3178670SJose.Borrego@Sun.COM 	cred_t		*cr;
3188670SJose.Borrego@Sun.COM 	char		pattern[SMB_STREAM_PREFIX_LEN + 2];
3198670SJose.Borrego@Sun.COM 
3208670SJose.Borrego@Sun.COM 	smb_node_t	*xattr_dnode;
3218670SJose.Borrego@Sun.COM 	smb_attr_t	tmp_attr;
3228670SJose.Borrego@Sun.COM 
3238670SJose.Borrego@Sun.COM 	ASSERT(sr);
3248670SJose.Borrego@Sun.COM 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
3258670SJose.Borrego@Sun.COM 	ASSERT(unode);
3268670SJose.Borrego@Sun.COM 	ASSERT(unode->n_magic == SMB_NODE_MAGIC);
3278670SJose.Borrego@Sun.COM 
3288845Samw@Sun.COM 	if (SMB_TREE_CONTAINS_NODE(sr, unode) == 0 ||
3298845Samw@Sun.COM 	    SMB_TREE_HAS_ACCESS(sr, ACE_LIST_DIRECTORY) == 0) {
3308670SJose.Borrego@Sun.COM 		smbsr_error(sr, NT_STATUS_ACCESS_DENIED,
3318670SJose.Borrego@Sun.COM 		    ERRDOS, ERROR_ACCESS_DENIED);
3328670SJose.Borrego@Sun.COM 		return (0);
3338670SJose.Borrego@Sun.COM 	}
3349343SAfshin.Ardakani@Sun.COM 	cr = kcred;
3358670SJose.Borrego@Sun.COM 
3368670SJose.Borrego@Sun.COM 	/* find the xattrdir vnode */
3378670SJose.Borrego@Sun.COM 	rc = smb_vop_lookup_xattrdir(unode->vp, &xattr_dvp, LOOKUP_XATTR, cr);
3388670SJose.Borrego@Sun.COM 	if (rc != 0) {
3398670SJose.Borrego@Sun.COM 		smbsr_errno(sr, rc);
3408670SJose.Borrego@Sun.COM 		return (0);
3418670SJose.Borrego@Sun.COM 	}
3428670SJose.Borrego@Sun.COM 
3438670SJose.Borrego@Sun.COM 	/* lookup the xattrdir's smb_node */
3448670SJose.Borrego@Sun.COM 	xattr_dnode = smb_node_lookup(sr, NULL, cr, xattr_dvp, XATTR_DIR,
3458670SJose.Borrego@Sun.COM 	    unode, NULL, &tmp_attr);
3468670SJose.Borrego@Sun.COM 	VN_RELE(xattr_dvp);
3478670SJose.Borrego@Sun.COM 	if (xattr_dnode == NULL) {
3488670SJose.Borrego@Sun.COM 		smbsr_error(sr, NT_STATUS_NO_MEMORY,
3498670SJose.Borrego@Sun.COM 		    ERRDOS, ERROR_NOT_ENOUGH_MEMORY);
3508670SJose.Borrego@Sun.COM 		return (0);
3518670SJose.Borrego@Sun.COM 	}
3528670SJose.Borrego@Sun.COM 
3538670SJose.Borrego@Sun.COM 	(void) snprintf(pattern, sizeof (pattern), "%s*", SMB_STREAM_PREFIX);
3549343SAfshin.Ardakani@Sun.COM 	od = smb_odir_create(sr, xattr_dnode, pattern, SMB_SEARCH_ATTRIBUTES,
3559343SAfshin.Ardakani@Sun.COM 	    cr);
3568670SJose.Borrego@Sun.COM 	smb_node_release(xattr_dnode);
3578670SJose.Borrego@Sun.COM 	if (od == NULL)
3588670SJose.Borrego@Sun.COM 		return (0);
3598670SJose.Borrego@Sun.COM 
3609231SAfshin.Ardakani@Sun.COM 	od->d_flags |= SMB_ODIR_FLAG_XATTR;
3618670SJose.Borrego@Sun.COM 	return (od->d_odid);
3628670SJose.Borrego@Sun.COM }
3638670SJose.Borrego@Sun.COM 
3648670SJose.Borrego@Sun.COM /*
3658670SJose.Borrego@Sun.COM  * smb_odir_hold
3668670SJose.Borrego@Sun.COM  */
3678670SJose.Borrego@Sun.COM boolean_t
3688670SJose.Borrego@Sun.COM smb_odir_hold(smb_odir_t *od)
3698670SJose.Borrego@Sun.COM {
3708670SJose.Borrego@Sun.COM 	ASSERT(od);
3718670SJose.Borrego@Sun.COM 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
3728670SJose.Borrego@Sun.COM 
3738670SJose.Borrego@Sun.COM 	mutex_enter(&od->d_mutex);
3748670SJose.Borrego@Sun.COM 	if (od->d_state != SMB_ODIR_STATE_OPEN) {
3758670SJose.Borrego@Sun.COM 		mutex_exit(&od->d_mutex);
3768670SJose.Borrego@Sun.COM 		return (B_FALSE);
3778670SJose.Borrego@Sun.COM 	}
3788670SJose.Borrego@Sun.COM 
3798670SJose.Borrego@Sun.COM 	od->d_refcnt++;
3808670SJose.Borrego@Sun.COM 	mutex_exit(&od->d_mutex);
3818670SJose.Borrego@Sun.COM 	return (B_TRUE);
3828670SJose.Borrego@Sun.COM }
3838670SJose.Borrego@Sun.COM 
3848670SJose.Borrego@Sun.COM /*
3858670SJose.Borrego@Sun.COM  * smb_odir_release
3868670SJose.Borrego@Sun.COM  *
3878670SJose.Borrego@Sun.COM  * If the odir is in SMB_ODIR_STATE_CLOSING and this release
3888670SJose.Borrego@Sun.COM  * results in a refcnt of 0, the odir may be removed from
3898670SJose.Borrego@Sun.COM  * the tree's list of odirs and deleted.  The odir's state is
3908670SJose.Borrego@Sun.COM  * set to SMB_ODIR_STATE_CLOSED prior to exiting the mutex and
3918670SJose.Borrego@Sun.COM  * deleting it. This ensure that nobody else can ontain a reference
3928670SJose.Borrego@Sun.COM  * to it while we are deleting it.
3935331Samw  */
3945331Samw void
3958670SJose.Borrego@Sun.COM smb_odir_release(smb_odir_t *od)
3965331Samw {
3975331Samw 	ASSERT(od);
3985331Samw 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
3995331Samw 
4005331Samw 	mutex_enter(&od->d_mutex);
4015331Samw 	ASSERT(od->d_refcnt);
4028670SJose.Borrego@Sun.COM 
4035331Samw 	switch (od->d_state) {
4045331Samw 	case SMB_ODIR_STATE_OPEN:
4058670SJose.Borrego@Sun.COM 		od->d_refcnt--;
4065331Samw 		break;
4075331Samw 	case SMB_ODIR_STATE_CLOSING:
4088670SJose.Borrego@Sun.COM 		od->d_refcnt--;
4098670SJose.Borrego@Sun.COM 		if (od->d_refcnt == 0) {
4108670SJose.Borrego@Sun.COM 			od->d_state = SMB_ODIR_STATE_CLOSED;
4118670SJose.Borrego@Sun.COM 			mutex_exit(&od->d_mutex);
4128670SJose.Borrego@Sun.COM 			smb_odir_delete(od);
4138670SJose.Borrego@Sun.COM 			return;
4148670SJose.Borrego@Sun.COM 		}
4158670SJose.Borrego@Sun.COM 		break;
4165331Samw 	case SMB_ODIR_STATE_CLOSED:
4175331Samw 		break;
4185331Samw 	default:
4195331Samw 		ASSERT(0);
4205331Samw 		break;
4215331Samw 	}
4225331Samw 
4235331Samw 	mutex_exit(&od->d_mutex);
4245331Samw }
4255331Samw 
4265331Samw /*
4278670SJose.Borrego@Sun.COM  * smb_odir_close
4285331Samw  */
4298670SJose.Borrego@Sun.COM void
4308670SJose.Borrego@Sun.COM smb_odir_close(smb_odir_t *od)
4315331Samw {
4328670SJose.Borrego@Sun.COM 	ASSERT(od);
4338670SJose.Borrego@Sun.COM 	ASSERT(od->d_refcnt);
4345331Samw 
4358670SJose.Borrego@Sun.COM 	mutex_enter(&od->d_mutex);
4368670SJose.Borrego@Sun.COM 	if (od->d_state != SMB_ODIR_STATE_OPEN) {
4378670SJose.Borrego@Sun.COM 		mutex_exit(&od->d_mutex);
4388670SJose.Borrego@Sun.COM 		return;
4398670SJose.Borrego@Sun.COM 	}
4408670SJose.Borrego@Sun.COM 	od->d_state = SMB_ODIR_STATE_CLOSING;
4418670SJose.Borrego@Sun.COM 	mutex_exit(&od->d_mutex);
4425331Samw 
4438670SJose.Borrego@Sun.COM 	smb_odir_release(od);
4448670SJose.Borrego@Sun.COM }
4455331Samw 
4468670SJose.Borrego@Sun.COM /*
4478670SJose.Borrego@Sun.COM  * smb_odir_read
4488670SJose.Borrego@Sun.COM  *
4498670SJose.Borrego@Sun.COM  * Find the next directory entry matching the search pattern.
4508670SJose.Borrego@Sun.COM  * No search attribute matching is performed.
4518670SJose.Borrego@Sun.COM  *
4528670SJose.Borrego@Sun.COM  * Returns:
4538670SJose.Borrego@Sun.COM  *  0 - success.
4548670SJose.Borrego@Sun.COM  *      - If a matching entry was found eof will be B_FALSE and
4558670SJose.Borrego@Sun.COM  *        odirent will be populated.
4568670SJose.Borrego@Sun.COM  *      - If there are no matching entries eof will be B_TRUE.
4578670SJose.Borrego@Sun.COM  * -1 - error, error details set in sr.
4588670SJose.Borrego@Sun.COM  */
4598670SJose.Borrego@Sun.COM int
4608670SJose.Borrego@Sun.COM smb_odir_read(smb_request_t *sr, smb_odir_t *od,
4618670SJose.Borrego@Sun.COM     smb_odirent_t *odirent, boolean_t *eof)
4628670SJose.Borrego@Sun.COM {
4639231SAfshin.Ardakani@Sun.COM 	int		rc;
4649231SAfshin.Ardakani@Sun.COM 	boolean_t	ignore_case;
4658670SJose.Borrego@Sun.COM 
4668670SJose.Borrego@Sun.COM 	ASSERT(sr);
4678670SJose.Borrego@Sun.COM 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
4688670SJose.Borrego@Sun.COM 	ASSERT(od);
4698670SJose.Borrego@Sun.COM 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
4708670SJose.Borrego@Sun.COM 	ASSERT(odirent);
4718670SJose.Borrego@Sun.COM 
4728670SJose.Borrego@Sun.COM 	mutex_enter(&od->d_mutex);
4738670SJose.Borrego@Sun.COM 
4748670SJose.Borrego@Sun.COM 	ASSERT(od->d_state == SMB_ODIR_STATE_OPEN);
4758670SJose.Borrego@Sun.COM 	if (od->d_state != SMB_ODIR_STATE_OPEN) {
4768670SJose.Borrego@Sun.COM 		mutex_exit(&od->d_mutex);
4778670SJose.Borrego@Sun.COM 		return (-1);
4788670SJose.Borrego@Sun.COM 	}
4798670SJose.Borrego@Sun.COM 
4809231SAfshin.Ardakani@Sun.COM 	ignore_case = (od->d_flags & SMB_ODIR_FLAG_IGNORE_CASE);
4819231SAfshin.Ardakani@Sun.COM 
4828670SJose.Borrego@Sun.COM 	for (;;) {
4838670SJose.Borrego@Sun.COM 		if ((rc = smb_odir_next_odirent(od, odirent)) != 0)
4845331Samw 			break;
4858670SJose.Borrego@Sun.COM 		if (smb_match_name(odirent->od_ino, odirent->od_name,
4869231SAfshin.Ardakani@Sun.COM 		    od->d_pattern, ignore_case))
4878670SJose.Borrego@Sun.COM 			break;
4885331Samw 	}
4898670SJose.Borrego@Sun.COM 
4908670SJose.Borrego@Sun.COM 	mutex_exit(&od->d_mutex);
4918670SJose.Borrego@Sun.COM 
4928670SJose.Borrego@Sun.COM 	switch (rc) {
4938670SJose.Borrego@Sun.COM 	case 0:
4948670SJose.Borrego@Sun.COM 		*eof = B_FALSE;
4958670SJose.Borrego@Sun.COM 		return (0);
4968670SJose.Borrego@Sun.COM 	case ENOENT:
4978670SJose.Borrego@Sun.COM 		*eof = B_TRUE;
4988670SJose.Borrego@Sun.COM 		return (0);
4998670SJose.Borrego@Sun.COM 	default:
5008670SJose.Borrego@Sun.COM 		smbsr_errno(sr, rc);
5018670SJose.Borrego@Sun.COM 		return (-1);
5028670SJose.Borrego@Sun.COM 	}
5035331Samw }
5045331Samw 
5058670SJose.Borrego@Sun.COM /*
5068670SJose.Borrego@Sun.COM  * smb_odir_read_fileinfo
5078670SJose.Borrego@Sun.COM  *
5088670SJose.Borrego@Sun.COM  * Find the next directory entry matching the search pattern
5098670SJose.Borrego@Sun.COM  * and attributes: od->d_pattern and od->d_sattr.
5108670SJose.Borrego@Sun.COM  *
5118670SJose.Borrego@Sun.COM  * If the search pattern specifies a single filename call
5128670SJose.Borrego@Sun.COM  * smb_odir_single_fileinfo to get the file attributes and
5138670SJose.Borrego@Sun.COM  * populate the caller's smb_fileinfo_t.
5148670SJose.Borrego@Sun.COM  *
5158670SJose.Borrego@Sun.COM  * If the search pattern contains wildcards call smb_odir_next_odirent
5168670SJose.Borrego@Sun.COM  * to get the next directory entry then. Repeat until a matching
5178670SJose.Borrego@Sun.COM  * filename is found. Call smb_odir_wildcard_fileinfo to get the
5188670SJose.Borrego@Sun.COM  * file attributes and populate the caller's smb_fileinfo_t.
5198670SJose.Borrego@Sun.COM  * This is repeated until a file matching the search criteria is found.
5208670SJose.Borrego@Sun.COM  *
5218670SJose.Borrego@Sun.COM  * Returns:
5228670SJose.Borrego@Sun.COM  *  0 - success.
5238670SJose.Borrego@Sun.COM  *      - If a matching entry was found eof will be B_FALSE and
5248670SJose.Borrego@Sun.COM  *        fileinfo will be populated.
5258670SJose.Borrego@Sun.COM  *      - If there are no matching entries eof will be B_TRUE.
5268670SJose.Borrego@Sun.COM  * -1 - error, error details set in sr.
5278670SJose.Borrego@Sun.COM  */
5288670SJose.Borrego@Sun.COM int
5298670SJose.Borrego@Sun.COM smb_odir_read_fileinfo(smb_request_t *sr, smb_odir_t *od,
5308670SJose.Borrego@Sun.COM     smb_fileinfo_t *fileinfo, boolean_t *eof)
5318670SJose.Borrego@Sun.COM {
5328670SJose.Borrego@Sun.COM 	int		rc;
5338670SJose.Borrego@Sun.COM 	smb_odirent_t	*odirent;
5349231SAfshin.Ardakani@Sun.COM 	boolean_t	ignore_case;
5358670SJose.Borrego@Sun.COM 
5368670SJose.Borrego@Sun.COM 	ASSERT(sr);
5378670SJose.Borrego@Sun.COM 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
5388670SJose.Borrego@Sun.COM 	ASSERT(od);
5398670SJose.Borrego@Sun.COM 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
5408670SJose.Borrego@Sun.COM 	ASSERT(fileinfo);
5418670SJose.Borrego@Sun.COM 
5428670SJose.Borrego@Sun.COM 	mutex_enter(&od->d_mutex);
5438670SJose.Borrego@Sun.COM 
5448670SJose.Borrego@Sun.COM 	ASSERT(od->d_state == SMB_ODIR_STATE_OPEN);
5458670SJose.Borrego@Sun.COM 	if (od->d_state != SMB_ODIR_STATE_OPEN) {
5468670SJose.Borrego@Sun.COM 		mutex_exit(&od->d_mutex);
5478670SJose.Borrego@Sun.COM 		return (-1);
5488670SJose.Borrego@Sun.COM 	}
5498670SJose.Borrego@Sun.COM 
5509231SAfshin.Ardakani@Sun.COM 	ignore_case = (od->d_flags & SMB_ODIR_FLAG_IGNORE_CASE);
5519231SAfshin.Ardakani@Sun.COM 
5529231SAfshin.Ardakani@Sun.COM 	if (!(od->d_flags & SMB_ODIR_FLAG_WILDCARDS)) {
5538670SJose.Borrego@Sun.COM 		if (od->d_eof)
5548670SJose.Borrego@Sun.COM 			rc = ENOENT;
5558670SJose.Borrego@Sun.COM 		else
5568670SJose.Borrego@Sun.COM 			rc = smb_odir_single_fileinfo(sr, od, fileinfo);
5578670SJose.Borrego@Sun.COM 		od->d_eof = B_TRUE;
5588670SJose.Borrego@Sun.COM 	} else {
5598670SJose.Borrego@Sun.COM 		odirent = kmem_alloc(sizeof (smb_odirent_t), KM_SLEEP);
5608670SJose.Borrego@Sun.COM 		for (;;) {
5618670SJose.Borrego@Sun.COM 			bzero(fileinfo, sizeof (smb_fileinfo_t));
5628670SJose.Borrego@Sun.COM 			if ((rc = smb_odir_next_odirent(od, odirent)) != 0)
5638670SJose.Borrego@Sun.COM 				break;
5648670SJose.Borrego@Sun.COM 
5658670SJose.Borrego@Sun.COM 			if (!smb_match_name(odirent->od_ino, odirent->od_name,
5669231SAfshin.Ardakani@Sun.COM 			    od->d_pattern, ignore_case))
5678670SJose.Borrego@Sun.COM 				continue;
5688670SJose.Borrego@Sun.COM 
5698670SJose.Borrego@Sun.COM 			rc = smb_odir_wildcard_fileinfo(sr, od, odirent,
5708670SJose.Borrego@Sun.COM 			    fileinfo);
5718670SJose.Borrego@Sun.COM 			if (rc == 0)
5728670SJose.Borrego@Sun.COM 				break;
5738670SJose.Borrego@Sun.COM 		}
5748670SJose.Borrego@Sun.COM 		kmem_free(odirent, sizeof (smb_odirent_t));
5758670SJose.Borrego@Sun.COM 	}
5768670SJose.Borrego@Sun.COM 	mutex_exit(&od->d_mutex);
5778670SJose.Borrego@Sun.COM 
5788670SJose.Borrego@Sun.COM 	switch (rc) {
5798670SJose.Borrego@Sun.COM 	case 0:
5808670SJose.Borrego@Sun.COM 		*eof = B_FALSE;
5818670SJose.Borrego@Sun.COM 		return (0);
5828670SJose.Borrego@Sun.COM 	case ENOENT:
5838670SJose.Borrego@Sun.COM 		*eof = B_TRUE;
5848670SJose.Borrego@Sun.COM 		return (0);
5858670SJose.Borrego@Sun.COM 	default:
5868670SJose.Borrego@Sun.COM 		smbsr_errno(sr, rc);
5878670SJose.Borrego@Sun.COM 		return (-1);
5888670SJose.Borrego@Sun.COM 	}
5898670SJose.Borrego@Sun.COM }
5908670SJose.Borrego@Sun.COM 
5915331Samw 
5925331Samw /*
5938670SJose.Borrego@Sun.COM  * smb_odir_read_streaminfo
5948670SJose.Borrego@Sun.COM  *
5958670SJose.Borrego@Sun.COM  * Find the next directory entry whose name begins with SMB_STREAM_PREFIX,
5968670SJose.Borrego@Sun.COM  * and thus represents an NTFS named stream.
5978670SJose.Borrego@Sun.COM  * No search attribute matching is performed.
5989231SAfshin.Ardakani@Sun.COM  * No case conflict name mangling is required for NTFS named stream names.
5995331Samw  *
6008670SJose.Borrego@Sun.COM  * Returns:
6018670SJose.Borrego@Sun.COM  *  0 - success.
6028670SJose.Borrego@Sun.COM  *      - If a matching entry was found eof will be B_FALSE and
6038670SJose.Borrego@Sun.COM  *        sinfo will be populated.
6048670SJose.Borrego@Sun.COM  *      - If there are no matching entries eof will be B_TRUE.
6058670SJose.Borrego@Sun.COM  * -1 - error, error details set in sr.
6065331Samw  */
6078670SJose.Borrego@Sun.COM int
6088670SJose.Borrego@Sun.COM smb_odir_read_streaminfo(smb_request_t *sr, smb_odir_t *od,
6098670SJose.Borrego@Sun.COM     smb_streaminfo_t *sinfo, boolean_t *eof)
6105331Samw {
6118670SJose.Borrego@Sun.COM 	int		rc;
6128670SJose.Borrego@Sun.COM 	smb_odirent_t	*odirent;
6138670SJose.Borrego@Sun.COM 	vnode_t		*vp;
6148670SJose.Borrego@Sun.COM 	smb_attr_t	attr;
6159231SAfshin.Ardakani@Sun.COM 	int		tmpflg;
6165331Samw 
6178670SJose.Borrego@Sun.COM 	ASSERT(sr);
6188670SJose.Borrego@Sun.COM 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
6195331Samw 	ASSERT(od);
6205331Samw 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
6218670SJose.Borrego@Sun.COM 	ASSERT(sinfo);
6228670SJose.Borrego@Sun.COM 
6238670SJose.Borrego@Sun.COM 	mutex_enter(&od->d_mutex);
6248670SJose.Borrego@Sun.COM 
6258670SJose.Borrego@Sun.COM 	ASSERT(od->d_state == SMB_ODIR_STATE_OPEN);
6268670SJose.Borrego@Sun.COM 	if (od->d_state != SMB_ODIR_STATE_OPEN) {
6278670SJose.Borrego@Sun.COM 		mutex_exit(&od->d_mutex);
6288670SJose.Borrego@Sun.COM 		return (-1);
6298670SJose.Borrego@Sun.COM 	}
6308670SJose.Borrego@Sun.COM 
6318670SJose.Borrego@Sun.COM 	/* Check that odir represents an xattr directory */
6329231SAfshin.Ardakani@Sun.COM 	if (!(od->d_flags & SMB_ODIR_FLAG_XATTR)) {
6338670SJose.Borrego@Sun.COM 		*eof = B_TRUE;
6348670SJose.Borrego@Sun.COM 		mutex_exit(&od->d_mutex);
6358670SJose.Borrego@Sun.COM 		return (0);
6368670SJose.Borrego@Sun.COM 	}
6378670SJose.Borrego@Sun.COM 
6388670SJose.Borrego@Sun.COM 	odirent = kmem_alloc(sizeof (smb_odirent_t), KM_SLEEP);
6398670SJose.Borrego@Sun.COM 
6408670SJose.Borrego@Sun.COM 	for (;;) {
6418670SJose.Borrego@Sun.COM 		bzero(sinfo, sizeof (smb_streaminfo_t));
6428670SJose.Borrego@Sun.COM 		if ((rc = smb_odir_next_odirent(od, odirent)) != 0)
6438670SJose.Borrego@Sun.COM 			break;
6448670SJose.Borrego@Sun.COM 
6458670SJose.Borrego@Sun.COM 		if (strncmp(odirent->od_name, SMB_STREAM_PREFIX,
6468670SJose.Borrego@Sun.COM 		    SMB_STREAM_PREFIX_LEN)) {
6478670SJose.Borrego@Sun.COM 			continue;
6488670SJose.Borrego@Sun.COM 		}
6498670SJose.Borrego@Sun.COM 
6508670SJose.Borrego@Sun.COM 		/*
6518670SJose.Borrego@Sun.COM 		 * since we only care about the size attribute we don't need to
6528670SJose.Borrego@Sun.COM 		 * pass the vp of the unnamed stream file to smb_vop_getattr
6538670SJose.Borrego@Sun.COM 		 */
6548670SJose.Borrego@Sun.COM 		rc = smb_vop_lookup(od->d_dnode->vp, odirent->od_name, &vp,
6559343SAfshin.Ardakani@Sun.COM 		    NULL, 0, &tmpflg, od->d_tree->t_snode->vp, od->d_cred);
6568670SJose.Borrego@Sun.COM 		if (rc == 0) {
6579343SAfshin.Ardakani@Sun.COM 			rc = smb_vop_getattr(vp, NULL, &attr, 0, od->d_cred);
6588670SJose.Borrego@Sun.COM 			VN_RELE(vp);
6598670SJose.Borrego@Sun.COM 		}
6608670SJose.Borrego@Sun.COM 
6618670SJose.Borrego@Sun.COM 		if (rc == 0) {
6628670SJose.Borrego@Sun.COM 			(void) strlcpy(sinfo->si_name,
6638670SJose.Borrego@Sun.COM 			    odirent->od_name + SMB_STREAM_PREFIX_LEN,
6648670SJose.Borrego@Sun.COM 			    sizeof (sinfo->si_name));
6658670SJose.Borrego@Sun.COM 			sinfo->si_size = attr.sa_vattr.va_size;
6668670SJose.Borrego@Sun.COM 			break;
6678670SJose.Borrego@Sun.COM 		}
6688670SJose.Borrego@Sun.COM 	}
6698670SJose.Borrego@Sun.COM 	mutex_exit(&od->d_mutex);
6708670SJose.Borrego@Sun.COM 
6718670SJose.Borrego@Sun.COM 	kmem_free(odirent, sizeof (smb_odirent_t));
6728670SJose.Borrego@Sun.COM 
6738670SJose.Borrego@Sun.COM 	switch (rc) {
6748670SJose.Borrego@Sun.COM 	case 0:
6758670SJose.Borrego@Sun.COM 		*eof = B_FALSE;
6768670SJose.Borrego@Sun.COM 		return (0);
6778670SJose.Borrego@Sun.COM 	case ENOENT:
6788670SJose.Borrego@Sun.COM 		*eof = B_TRUE;
6798670SJose.Borrego@Sun.COM 		return (0);
6808670SJose.Borrego@Sun.COM 	default:
6818670SJose.Borrego@Sun.COM 		smbsr_errno(sr, rc);
6828670SJose.Borrego@Sun.COM 		return (-1);
6838670SJose.Borrego@Sun.COM 	}
6848670SJose.Borrego@Sun.COM }
6858670SJose.Borrego@Sun.COM 
6868670SJose.Borrego@Sun.COM /*
6878670SJose.Borrego@Sun.COM  * smb_odir_save_cookie
6888670SJose.Borrego@Sun.COM  *
6898670SJose.Borrego@Sun.COM  * Callers can save up to SMB_MAX_SEARCH cookies in the odir
6908670SJose.Borrego@Sun.COM  * to be used as resume points for a 'find next' request.
6918670SJose.Borrego@Sun.COM  */
6928670SJose.Borrego@Sun.COM void
6938670SJose.Borrego@Sun.COM smb_odir_save_cookie(smb_odir_t *od, int idx, uint32_t cookie)
6948670SJose.Borrego@Sun.COM {
6958670SJose.Borrego@Sun.COM 	ASSERT(od);
6968670SJose.Borrego@Sun.COM 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
6978670SJose.Borrego@Sun.COM 	ASSERT(idx >= 0 && idx < SMB_MAX_SEARCH);
6985331Samw 
6995331Samw 	mutex_enter(&od->d_mutex);
7008670SJose.Borrego@Sun.COM 	od->d_cookies[idx] = cookie;
7018670SJose.Borrego@Sun.COM 	mutex_exit(&od->d_mutex);
7028670SJose.Borrego@Sun.COM }
7038670SJose.Borrego@Sun.COM 
7048670SJose.Borrego@Sun.COM /*
7058670SJose.Borrego@Sun.COM  * smb_odir_resume_at
7068670SJose.Borrego@Sun.COM  *
7079343SAfshin.Ardakani@Sun.COM  * If SMB_ODIR_FLAG_WILDCARDS is not set the search is for a single
7089343SAfshin.Ardakani@Sun.COM  * file and should not be resumed.
7099343SAfshin.Ardakani@Sun.COM  *
7109343SAfshin.Ardakani@Sun.COM  * Wildcard searching can be resumed from:
7118670SJose.Borrego@Sun.COM  * - the cookie saved at a specified index (SMBsearch, SMBfind).
7128670SJose.Borrego@Sun.COM  * - a specified cookie (SMB_trans2_find)
7138670SJose.Borrego@Sun.COM  * - a specified filename (SMB_trans2_find) - NOT SUPPORTED.
7148670SJose.Borrego@Sun.COM  *   Defaults to continuing from where the last search ended.
7158670SJose.Borrego@Sun.COM  *
7168670SJose.Borrego@Sun.COM  * Continuation from where the last search ended (SMB_trans2_find)
7178670SJose.Borrego@Sun.COM  * is implemented by saving the last cookie at a specific index (0)
7188670SJose.Borrego@Sun.COM  * smb_odir_resume_at indicates a new request, so reset od->d_bufptr
7198670SJose.Borrego@Sun.COM  * and d_eof to force a vop_readdir.
7208670SJose.Borrego@Sun.COM  */
7218670SJose.Borrego@Sun.COM void
7228670SJose.Borrego@Sun.COM smb_odir_resume_at(smb_odir_t *od, smb_odir_resume_t *resume)
7238670SJose.Borrego@Sun.COM {
7248670SJose.Borrego@Sun.COM 	ASSERT(od);
7258670SJose.Borrego@Sun.COM 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
7268670SJose.Borrego@Sun.COM 	ASSERT(resume);
7278670SJose.Borrego@Sun.COM 
7288670SJose.Borrego@Sun.COM 	mutex_enter(&od->d_mutex);
7298670SJose.Borrego@Sun.COM 
7309343SAfshin.Ardakani@Sun.COM 	if ((od->d_flags & SMB_ODIR_FLAG_WILDCARDS) == 0) {
7319343SAfshin.Ardakani@Sun.COM 		od->d_eof = B_TRUE;
7329343SAfshin.Ardakani@Sun.COM 		mutex_exit(&od->d_mutex);
7339343SAfshin.Ardakani@Sun.COM 		return;
7349343SAfshin.Ardakani@Sun.COM 	}
7359343SAfshin.Ardakani@Sun.COM 
7368670SJose.Borrego@Sun.COM 	switch (resume->or_type) {
7378670SJose.Borrego@Sun.COM 		case SMB_ODIR_RESUME_IDX:
7388670SJose.Borrego@Sun.COM 			ASSERT(resume->or_idx >= 0);
7398670SJose.Borrego@Sun.COM 			ASSERT(resume->or_idx < SMB_MAX_SEARCH);
7408670SJose.Borrego@Sun.COM 
7418670SJose.Borrego@Sun.COM 			if ((resume->or_idx < 0) ||
7428670SJose.Borrego@Sun.COM 			    (resume->or_idx >= SMB_MAX_SEARCH)) {
7438670SJose.Borrego@Sun.COM 				resume->or_idx = 0;
7448670SJose.Borrego@Sun.COM 			}
7458670SJose.Borrego@Sun.COM 			od->d_offset = od->d_cookies[resume->or_idx];
7468670SJose.Borrego@Sun.COM 			break;
7478670SJose.Borrego@Sun.COM 		case SMB_ODIR_RESUME_COOKIE:
7488670SJose.Borrego@Sun.COM 			od->d_offset = resume->or_cookie;
7498670SJose.Borrego@Sun.COM 			break;
7508670SJose.Borrego@Sun.COM 		case SMB_ODIR_RESUME_FNAME:
7518670SJose.Borrego@Sun.COM 		default:
7528670SJose.Borrego@Sun.COM 			od->d_offset = od->d_cookies[0];
7538670SJose.Borrego@Sun.COM 			break;
7545331Samw 	}
7558670SJose.Borrego@Sun.COM 
7568670SJose.Borrego@Sun.COM 	/* Force a vop_readdir to refresh d_buf */
7578670SJose.Borrego@Sun.COM 	od->d_bufptr = NULL;
7588670SJose.Borrego@Sun.COM 	od->d_eof = B_FALSE;
7598670SJose.Borrego@Sun.COM 
7608670SJose.Borrego@Sun.COM 	mutex_exit(&od->d_mutex);
7618670SJose.Borrego@Sun.COM }
7628670SJose.Borrego@Sun.COM 
7638670SJose.Borrego@Sun.COM 
7648670SJose.Borrego@Sun.COM /* *** static functions *** */
7658670SJose.Borrego@Sun.COM 
7668670SJose.Borrego@Sun.COM /*
7678670SJose.Borrego@Sun.COM  * smb_odir_create
7688670SJose.Borrego@Sun.COM  * Allocate and populate an odir obect and add it to the tree's list.
7698670SJose.Borrego@Sun.COM  */
7708670SJose.Borrego@Sun.COM static smb_odir_t *
7718670SJose.Borrego@Sun.COM smb_odir_create(smb_request_t *sr, smb_node_t *dnode,
7729343SAfshin.Ardakani@Sun.COM     char *pattern, uint16_t sattr, cred_t *cr)
7738670SJose.Borrego@Sun.COM {
7748670SJose.Borrego@Sun.COM 	smb_odir_t	*od;
7758670SJose.Borrego@Sun.COM 	smb_tree_t	*tree;
7768670SJose.Borrego@Sun.COM 	uint16_t	odid;
7778670SJose.Borrego@Sun.COM 
7788670SJose.Borrego@Sun.COM 	ASSERT(sr);
7798670SJose.Borrego@Sun.COM 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
7808670SJose.Borrego@Sun.COM 	ASSERT(sr->tid_tree);
7818670SJose.Borrego@Sun.COM 	ASSERT(sr->tid_tree->t_magic == SMB_TREE_MAGIC);
7828670SJose.Borrego@Sun.COM 	ASSERT(dnode);
7838670SJose.Borrego@Sun.COM 	ASSERT(dnode->n_magic == SMB_NODE_MAGIC);
7848670SJose.Borrego@Sun.COM 
7858670SJose.Borrego@Sun.COM 	tree = sr->tid_tree;
7868670SJose.Borrego@Sun.COM 
7878670SJose.Borrego@Sun.COM 	if (smb_idpool_alloc(&tree->t_odid_pool, &odid)) {
7888670SJose.Borrego@Sun.COM 		smbsr_error(sr, NT_STATUS_TOO_MANY_OPENED_FILES,
7898670SJose.Borrego@Sun.COM 		    ERRDOS, ERROR_TOO_MANY_OPEN_FILES);
7908670SJose.Borrego@Sun.COM 		return (NULL);
7918670SJose.Borrego@Sun.COM 	}
7928670SJose.Borrego@Sun.COM 
7938670SJose.Borrego@Sun.COM 	od = kmem_cache_alloc(tree->t_server->si_cache_odir, KM_SLEEP);
7948670SJose.Borrego@Sun.COM 	bzero(od, sizeof (smb_odir_t));
7958670SJose.Borrego@Sun.COM 
7968670SJose.Borrego@Sun.COM 	mutex_init(&od->d_mutex, NULL, MUTEX_DEFAULT, NULL);
7978670SJose.Borrego@Sun.COM 	od->d_refcnt = 1;
7988670SJose.Borrego@Sun.COM 	od->d_state = SMB_ODIR_STATE_OPEN;
7998670SJose.Borrego@Sun.COM 	od->d_magic = SMB_ODIR_MAGIC;
8008670SJose.Borrego@Sun.COM 	od->d_opened_by_pid = sr->smb_pid;
8018670SJose.Borrego@Sun.COM 	od->d_session = tree->t_session;
8029343SAfshin.Ardakani@Sun.COM 	od->d_cred = cr;
8038670SJose.Borrego@Sun.COM 	od->d_tree = tree;
8048670SJose.Borrego@Sun.COM 	od->d_dnode = dnode;
8058670SJose.Borrego@Sun.COM 	smb_node_ref(dnode);
8068670SJose.Borrego@Sun.COM 	od->d_odid = odid;
8078670SJose.Borrego@Sun.COM 	od->d_sattr = sattr;
8088670SJose.Borrego@Sun.COM 	(void) strlcpy(od->d_pattern, pattern, sizeof (od->d_pattern));
8099231SAfshin.Ardakani@Sun.COM 	od->d_flags = 0;
8109231SAfshin.Ardakani@Sun.COM 	if (smb_convert_wildcards(od->d_pattern) != 0)
8119231SAfshin.Ardakani@Sun.COM 		od->d_flags |= SMB_ODIR_FLAG_WILDCARDS;
8129231SAfshin.Ardakani@Sun.COM 	if (vfs_has_feature(dnode->vp->v_vfsp, VFSFT_DIRENTFLAGS))
8139231SAfshin.Ardakani@Sun.COM 		od->d_flags |= SMB_ODIR_FLAG_EDIRENT;
8149231SAfshin.Ardakani@Sun.COM 	if (smb_tree_has_feature(tree, SMB_TREE_CASEINSENSITIVE))
8159231SAfshin.Ardakani@Sun.COM 		od->d_flags |= SMB_ODIR_FLAG_IGNORE_CASE;
8169231SAfshin.Ardakani@Sun.COM 	if (SMB_TREE_SUPPORTS_CATIA(sr))
8179231SAfshin.Ardakani@Sun.COM 		od->d_flags |= SMB_ODIR_FLAG_CATIA;
8188670SJose.Borrego@Sun.COM 	od->d_eof = B_FALSE;
8198670SJose.Borrego@Sun.COM 
8208670SJose.Borrego@Sun.COM 	smb_llist_enter(&tree->t_odir_list, RW_WRITER);
8218670SJose.Borrego@Sun.COM 	smb_llist_insert_tail(&tree->t_odir_list, od);
8228670SJose.Borrego@Sun.COM 	smb_llist_exit(&tree->t_odir_list);
8238670SJose.Borrego@Sun.COM 
8248670SJose.Borrego@Sun.COM 	atomic_inc_32(&tree->t_session->s_dir_cnt);
8258670SJose.Borrego@Sun.COM 	return (od);
8265331Samw }
8275331Samw 
8285331Samw /*
8295331Samw  * smb_odir_delete
8308670SJose.Borrego@Sun.COM  *
8318670SJose.Borrego@Sun.COM  * Removal of the odir from the tree's list of odirs must be
8328670SJose.Borrego@Sun.COM  * done before any resources associated with the odir are
8338670SJose.Borrego@Sun.COM  * released.
8345331Samw  */
8355331Samw static void
8368670SJose.Borrego@Sun.COM smb_odir_delete(smb_odir_t *od)
8375331Samw {
8385331Samw 	ASSERT(od);
8395331Samw 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
8405331Samw 	ASSERT(od->d_state == SMB_ODIR_STATE_CLOSED);
8415331Samw 	ASSERT(od->d_refcnt == 0);
8425331Samw 
8435331Samw 	smb_llist_enter(&od->d_tree->t_odir_list, RW_WRITER);
8445331Samw 	smb_llist_remove(&od->d_tree->t_odir_list, od);
8455331Samw 	smb_llist_exit(&od->d_tree->t_odir_list);
8468670SJose.Borrego@Sun.COM 
8476139Sjb150015 	od->d_magic = 0;
8485331Samw 	atomic_dec_32(&od->d_tree->t_session->s_dir_cnt);
8498670SJose.Borrego@Sun.COM 	smb_node_release(od->d_dnode);
8508670SJose.Borrego@Sun.COM 	smb_idpool_free(&od->d_tree->t_odid_pool, od->d_odid);
8515331Samw 	mutex_destroy(&od->d_mutex);
8526139Sjb150015 	kmem_cache_free(od->d_tree->t_server->si_cache_odir, od);
8535331Samw }
8548670SJose.Borrego@Sun.COM 
8558670SJose.Borrego@Sun.COM /*
8568670SJose.Borrego@Sun.COM  * smb_odir_next_odirent
8578670SJose.Borrego@Sun.COM  *
8588670SJose.Borrego@Sun.COM  * Find the next directory entry in d_buf. If d_bufptr is NULL (buffer
8598670SJose.Borrego@Sun.COM  * is empty or we've reached the end of it), read the next set of
8608670SJose.Borrego@Sun.COM  * entries from the file system (vop_readdir).
8618670SJose.Borrego@Sun.COM  *
8628670SJose.Borrego@Sun.COM  * File systems which support VFSFT_EDIRENT_FLAGS will return the
8638670SJose.Borrego@Sun.COM  * directory entries as a buffer of edirent_t structure. Others will
8648670SJose.Borrego@Sun.COM  * return a buffer of dirent64_t structures.  For simplicity translate
8658670SJose.Borrego@Sun.COM  * the data into an smb_odirent_t structure.
8668670SJose.Borrego@Sun.COM  * The ed_name/d_name in d_buf is NULL terminated by the file system.
8678670SJose.Borrego@Sun.COM  *
8688670SJose.Borrego@Sun.COM  * Some file systems can have directories larger than SMB_MAXDIRSIZE.
8699231SAfshin.Ardakani@Sun.COM  * If the odirent offset >= SMB_MAXDIRSIZE return ENOENT and set d_eof
8709231SAfshin.Ardakani@Sun.COM  * to true to stop subsequent calls to smb_vop_readdir.
8718670SJose.Borrego@Sun.COM  *
8728670SJose.Borrego@Sun.COM  * Returns:
8738670SJose.Borrego@Sun.COM  *      0 - success. odirent is populated with the next directory entry
8748670SJose.Borrego@Sun.COM  * ENOENT - no more directory entries
8758670SJose.Borrego@Sun.COM  *  errno - error
8768670SJose.Borrego@Sun.COM  */
8778670SJose.Borrego@Sun.COM static int
8788670SJose.Borrego@Sun.COM smb_odir_next_odirent(smb_odir_t *od, smb_odirent_t *odirent)
8798670SJose.Borrego@Sun.COM {
8808670SJose.Borrego@Sun.COM 	int		rc;
8818670SJose.Borrego@Sun.COM 	int		reclen;
8828670SJose.Borrego@Sun.COM 	int		eof;
8838670SJose.Borrego@Sun.COM 	dirent64_t	*dp;
8848670SJose.Borrego@Sun.COM 	edirent_t	*edp;
8859231SAfshin.Ardakani@Sun.COM 	char		*np;
8868670SJose.Borrego@Sun.COM 
8878670SJose.Borrego@Sun.COM 	ASSERT(MUTEX_HELD(&od->d_mutex));
8888670SJose.Borrego@Sun.COM 
889*9437SJoyce.McIntosh@Sun.COM 	bzero(odirent, sizeof (smb_odirent_t));
890*9437SJoyce.McIntosh@Sun.COM 
8918670SJose.Borrego@Sun.COM 	if (od->d_bufptr != NULL) {
8929231SAfshin.Ardakani@Sun.COM 		if (od->d_flags & SMB_ODIR_FLAG_EDIRENT)
8939231SAfshin.Ardakani@Sun.COM 			reclen = od->d_edp->ed_reclen;
8949231SAfshin.Ardakani@Sun.COM 		else
8959231SAfshin.Ardakani@Sun.COM 			reclen = od->d_dp->d_reclen;
8968670SJose.Borrego@Sun.COM 
8978670SJose.Borrego@Sun.COM 		if (reclen == 0) {
8988670SJose.Borrego@Sun.COM 			od->d_bufptr = NULL;
8998670SJose.Borrego@Sun.COM 		} else {
9008670SJose.Borrego@Sun.COM 			od->d_bufptr += reclen;
9018670SJose.Borrego@Sun.COM 			if (od->d_bufptr >= od->d_buf + od->d_bufsize)
9028670SJose.Borrego@Sun.COM 				od->d_bufptr = NULL;
9038670SJose.Borrego@Sun.COM 		}
9048670SJose.Borrego@Sun.COM 	}
9058670SJose.Borrego@Sun.COM 
9068670SJose.Borrego@Sun.COM 	if (od->d_bufptr == NULL) {
9078670SJose.Borrego@Sun.COM 		if (od->d_eof)
9088670SJose.Borrego@Sun.COM 			return (ENOENT);
9098670SJose.Borrego@Sun.COM 
9108670SJose.Borrego@Sun.COM 		od->d_bufsize = sizeof (od->d_buf);
9118670SJose.Borrego@Sun.COM 
9128670SJose.Borrego@Sun.COM 		rc = smb_vop_readdir(od->d_dnode->vp, od->d_offset,
9139343SAfshin.Ardakani@Sun.COM 		    od->d_buf, &od->d_bufsize, &eof, od->d_cred);
9148670SJose.Borrego@Sun.COM 
9158670SJose.Borrego@Sun.COM 		if ((rc == 0) && (od->d_bufsize == 0))
9168670SJose.Borrego@Sun.COM 			rc = ENOENT;
9178670SJose.Borrego@Sun.COM 
9188670SJose.Borrego@Sun.COM 		if (rc != 0) {
9198670SJose.Borrego@Sun.COM 			od->d_bufptr = NULL;
9208670SJose.Borrego@Sun.COM 			od->d_bufsize = 0;
9218670SJose.Borrego@Sun.COM 			return (rc);
9228670SJose.Borrego@Sun.COM 		}
9238670SJose.Borrego@Sun.COM 
9248670SJose.Borrego@Sun.COM 		od->d_eof = (eof != 0);
9258670SJose.Borrego@Sun.COM 		od->d_bufptr = od->d_buf;
9268670SJose.Borrego@Sun.COM 	}
9278670SJose.Borrego@Sun.COM 
9289231SAfshin.Ardakani@Sun.COM 	if (od->d_flags & SMB_ODIR_FLAG_EDIRENT)
9299231SAfshin.Ardakani@Sun.COM 		od->d_offset = od->d_edp->ed_off;
9309231SAfshin.Ardakani@Sun.COM 	else
9319231SAfshin.Ardakani@Sun.COM 		od->d_offset = od->d_dp->d_off;
9329231SAfshin.Ardakani@Sun.COM 
9338670SJose.Borrego@Sun.COM 	if (od->d_offset >= SMB_MAXDIRSIZE) {
9348670SJose.Borrego@Sun.COM 		od->d_bufptr = NULL;
9358670SJose.Borrego@Sun.COM 		od->d_bufsize = 0;
9369231SAfshin.Ardakani@Sun.COM 		od->d_eof = B_TRUE;
9378670SJose.Borrego@Sun.COM 		return (ENOENT);
9388670SJose.Borrego@Sun.COM 	}
9398670SJose.Borrego@Sun.COM 
9409231SAfshin.Ardakani@Sun.COM 	if (od->d_flags & SMB_ODIR_FLAG_EDIRENT) {
9418670SJose.Borrego@Sun.COM 		edp = od->d_edp;
9428670SJose.Borrego@Sun.COM 		odirent->od_ino = edp->ed_ino;
9438670SJose.Borrego@Sun.COM 		odirent->od_eflags = edp->ed_eflags;
9449231SAfshin.Ardakani@Sun.COM 		np = edp->ed_name;
9458670SJose.Borrego@Sun.COM 	} else {
9468670SJose.Borrego@Sun.COM 		dp = od->d_dp;
9478670SJose.Borrego@Sun.COM 		odirent->od_ino = dp->d_ino;
9488670SJose.Borrego@Sun.COM 		odirent->od_eflags = 0;
9499231SAfshin.Ardakani@Sun.COM 		np =  dp->d_name;
9509231SAfshin.Ardakani@Sun.COM 	}
9519231SAfshin.Ardakani@Sun.COM 
9529231SAfshin.Ardakani@Sun.COM 	if ((od->d_flags & SMB_ODIR_FLAG_CATIA) &&
9539231SAfshin.Ardakani@Sun.COM 	    ((od->d_flags & SMB_ODIR_FLAG_XATTR) == 0)) {
9549231SAfshin.Ardakani@Sun.COM 		smb_vop_catia_v4tov5(np, odirent->od_name,
9559231SAfshin.Ardakani@Sun.COM 		    sizeof (odirent->od_name));
9569231SAfshin.Ardakani@Sun.COM 	} else {
9579231SAfshin.Ardakani@Sun.COM 		(void) strlcpy(odirent->od_name, np,
9588670SJose.Borrego@Sun.COM 		    sizeof (odirent->od_name));
9598670SJose.Borrego@Sun.COM 	}
9608670SJose.Borrego@Sun.COM 
9618670SJose.Borrego@Sun.COM 	return (0);
9628670SJose.Borrego@Sun.COM }
9638670SJose.Borrego@Sun.COM 
9648670SJose.Borrego@Sun.COM /*
9658670SJose.Borrego@Sun.COM  * smb_odir_single_fileinfo
9668670SJose.Borrego@Sun.COM  *
9678670SJose.Borrego@Sun.COM  * Lookup the file identified by od->d_pattern.
9688670SJose.Borrego@Sun.COM  *
9698670SJose.Borrego@Sun.COM  * If the looked up file is a link, we attempt to lookup the link target
9708670SJose.Borrego@Sun.COM  * to use its attributes in place of those of the files's.
9718670SJose.Borrego@Sun.COM  * If we fail to lookup the target of the link we use the original
9728670SJose.Borrego@Sun.COM  * file's attributes.
9738670SJose.Borrego@Sun.COM  * Check if the attributes match the search attributes.
9748670SJose.Borrego@Sun.COM  *
9758670SJose.Borrego@Sun.COM  * Returns: 0 - success
9768670SJose.Borrego@Sun.COM  *     ENOENT - no match
9778670SJose.Borrego@Sun.COM  *      errno - error
9788670SJose.Borrego@Sun.COM  */
9798670SJose.Borrego@Sun.COM static int
9808670SJose.Borrego@Sun.COM smb_odir_single_fileinfo(smb_request_t *sr, smb_odir_t *od,
9818670SJose.Borrego@Sun.COM     smb_fileinfo_t *fileinfo)
9828670SJose.Borrego@Sun.COM {
9838670SJose.Borrego@Sun.COM 	int		rc;
9848670SJose.Borrego@Sun.COM 	smb_node_t	*fnode, *tgt_node;
9858670SJose.Borrego@Sun.COM 	smb_attr_t	attr, tgt_attr, *fattr;
9868670SJose.Borrego@Sun.COM 	ino64_t		ino;
9878670SJose.Borrego@Sun.COM 	char		*name;
9888670SJose.Borrego@Sun.COM 	uint32_t	dosattr;
9899231SAfshin.Ardakani@Sun.COM 	boolean_t	case_conflict = B_FALSE;
9909231SAfshin.Ardakani@Sun.COM 	int		lookup_flags, flags = 0;
9919231SAfshin.Ardakani@Sun.COM 	vnode_t		*vp;
9928670SJose.Borrego@Sun.COM 
9938670SJose.Borrego@Sun.COM 	ASSERT(sr);
9948670SJose.Borrego@Sun.COM 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
9958670SJose.Borrego@Sun.COM 	ASSERT(od);
9968670SJose.Borrego@Sun.COM 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
9978670SJose.Borrego@Sun.COM 
9988670SJose.Borrego@Sun.COM 	ASSERT(MUTEX_HELD(&od->d_mutex));
9998670SJose.Borrego@Sun.COM 	bzero(fileinfo, sizeof (smb_fileinfo_t));
10008670SJose.Borrego@Sun.COM 
10019343SAfshin.Ardakani@Sun.COM 	rc = smb_fsop_lookup(sr, od->d_cred, 0, od->d_tree->t_snode,
10029231SAfshin.Ardakani@Sun.COM 	    od->d_dnode, od->d_pattern, &fnode, &attr);
10038670SJose.Borrego@Sun.COM 	if (rc != 0)
10048670SJose.Borrego@Sun.COM 		return (rc);
10058670SJose.Borrego@Sun.COM 
10069231SAfshin.Ardakani@Sun.COM 	/*
10079231SAfshin.Ardakani@Sun.COM 	 * If case sensitive, do a case insensitive smb_vop_lookup to
10089231SAfshin.Ardakani@Sun.COM 	 * check for case conflict
10099231SAfshin.Ardakani@Sun.COM 	 */
10109231SAfshin.Ardakani@Sun.COM 	if (od->d_flags & SMB_ODIR_FLAG_IGNORE_CASE) {
10119231SAfshin.Ardakani@Sun.COM 		lookup_flags = SMB_IGNORE_CASE;
10129231SAfshin.Ardakani@Sun.COM 		if (od->d_flags & SMB_ODIR_FLAG_CATIA)
10139231SAfshin.Ardakani@Sun.COM 			lookup_flags |= SMB_CATIA;
10148670SJose.Borrego@Sun.COM 
10159231SAfshin.Ardakani@Sun.COM 		rc = smb_vop_lookup(od->d_dnode->vp, fnode->od_name, &vp,
10169231SAfshin.Ardakani@Sun.COM 		    NULL, lookup_flags, &flags, od->d_tree->t_snode->vp,
10179343SAfshin.Ardakani@Sun.COM 		    od->d_cred);
10189231SAfshin.Ardakani@Sun.COM 		if (rc != 0)
10199231SAfshin.Ardakani@Sun.COM 			return (rc);
10209231SAfshin.Ardakani@Sun.COM 		VN_RELE(vp);
10219231SAfshin.Ardakani@Sun.COM 
10229231SAfshin.Ardakani@Sun.COM 		if (flags & ED_CASE_CONFLICT)
10239231SAfshin.Ardakani@Sun.COM 			case_conflict = B_TRUE;
10249231SAfshin.Ardakani@Sun.COM 	}
10259231SAfshin.Ardakani@Sun.COM 
10269231SAfshin.Ardakani@Sun.COM 	ino = attr.sa_vattr.va_nodeid;
10279231SAfshin.Ardakani@Sun.COM 	(void) smb_mangle_name(ino, fnode->od_name,
10289231SAfshin.Ardakani@Sun.COM 	    fileinfo->fi_shortname, fileinfo->fi_name83, case_conflict);
10299231SAfshin.Ardakani@Sun.COM 	name = (case_conflict) ? fileinfo->fi_shortname : fnode->od_name;
10308670SJose.Borrego@Sun.COM 	(void) strlcpy(fileinfo->fi_name, name, sizeof (fileinfo->fi_name));
10318670SJose.Borrego@Sun.COM 
10328670SJose.Borrego@Sun.COM 	/* follow link to get target node & attr */
10338670SJose.Borrego@Sun.COM 	if ((fnode->vp->v_type == VLNK) &&
10349231SAfshin.Ardakani@Sun.COM 	    (smb_odir_lookup_link(sr, od, fnode->od_name,
10359231SAfshin.Ardakani@Sun.COM 	    &tgt_node, &tgt_attr))) {
10368670SJose.Borrego@Sun.COM 		smb_node_release(fnode);
10378670SJose.Borrego@Sun.COM 		fnode = tgt_node;
10388670SJose.Borrego@Sun.COM 		fattr = &tgt_attr;
10398670SJose.Borrego@Sun.COM 	} else {
10408670SJose.Borrego@Sun.COM 		fattr = &attr;
10418670SJose.Borrego@Sun.COM 	}
10428670SJose.Borrego@Sun.COM 
10438670SJose.Borrego@Sun.COM 	/* check search attributes */
10448670SJose.Borrego@Sun.COM 	dosattr = smb_node_get_dosattr(fnode);
10458934SJose.Borrego@Sun.COM 	if (!smb_sattr_check(dosattr, od->d_sattr)) {
10468670SJose.Borrego@Sun.COM 		smb_node_release(fnode);
10478670SJose.Borrego@Sun.COM 		return (ENOENT);
10488670SJose.Borrego@Sun.COM 	}
10498670SJose.Borrego@Sun.COM 
10508670SJose.Borrego@Sun.COM 	fileinfo->fi_dosattr = dosattr;
10518670SJose.Borrego@Sun.COM 	fileinfo->fi_nodeid = fattr->sa_vattr.va_nodeid;
10528670SJose.Borrego@Sun.COM 	fileinfo->fi_size = smb_node_get_size(fnode, fattr);
10538670SJose.Borrego@Sun.COM 	fileinfo->fi_alloc_size = fattr->sa_vattr.va_nblocks * DEV_BSIZE;
10548670SJose.Borrego@Sun.COM 	fileinfo->fi_atime = fattr->sa_vattr.va_atime;
10558670SJose.Borrego@Sun.COM 	fileinfo->fi_mtime = fattr->sa_vattr.va_mtime;
10568670SJose.Borrego@Sun.COM 	fileinfo->fi_ctime = fattr->sa_vattr.va_ctime;
10578670SJose.Borrego@Sun.COM 	if (fattr->sa_crtime.tv_sec)
10588670SJose.Borrego@Sun.COM 		fileinfo->fi_crtime = fattr->sa_crtime;
10598670SJose.Borrego@Sun.COM 	else
10608670SJose.Borrego@Sun.COM 		fileinfo->fi_crtime = fattr->sa_vattr.va_mtime;
10618670SJose.Borrego@Sun.COM 
10628670SJose.Borrego@Sun.COM 	smb_node_release(fnode);
10638670SJose.Borrego@Sun.COM 	return (0);
10648670SJose.Borrego@Sun.COM }
10658670SJose.Borrego@Sun.COM 
10668670SJose.Borrego@Sun.COM /*
10678670SJose.Borrego@Sun.COM  * smb_odir_wildcard_fileinfo
10688670SJose.Borrego@Sun.COM  *
10698670SJose.Borrego@Sun.COM  * odirent contains a directory entry, obtained from a vop_readdir.
10708670SJose.Borrego@Sun.COM  * If a case conflict is identified the filename is mangled and the
10718670SJose.Borrego@Sun.COM  * shortname is used as 'name', in place of odirent->od_name. This
10728670SJose.Borrego@Sun.COM  * name will be used in the smb_fsop_lookup because smb_fsop_lookup
10738670SJose.Borrego@Sun.COM  * performs a case insensitive lookup if the tree is case insesitive,
10748670SJose.Borrego@Sun.COM  * so the mangled name is required in the case conflict scenario to
10758670SJose.Borrego@Sun.COM  * ensure the correct match.
10768670SJose.Borrego@Sun.COM  *
10778670SJose.Borrego@Sun.COM  * If the looked up file is a link, we attempt to lookup the link target
10788670SJose.Borrego@Sun.COM  * to use its attributes in place of those of the files's.
10798670SJose.Borrego@Sun.COM  * If we fail to lookup the target of the link we use the original
10808670SJose.Borrego@Sun.COM  * file's attributes.
10818670SJose.Borrego@Sun.COM  * Check if the attributes match the search attributes.
10828670SJose.Borrego@Sun.COM  *
10838670SJose.Borrego@Sun.COM  * Although some file systems can have directories larger than
10848670SJose.Borrego@Sun.COM  * SMB_MAXDIRSIZE smb_odir_next_odirent ensures that no offset larger
10858670SJose.Borrego@Sun.COM  * than SMB_MAXDIRSIZE is returned.  It is therefore safe to use the
10868670SJose.Borrego@Sun.COM  * offset as the cookie (uint32_t).
10878670SJose.Borrego@Sun.COM  *
10888670SJose.Borrego@Sun.COM  * Returns: 0 - success
10898670SJose.Borrego@Sun.COM  *     ENOENT - no match, proceed to next entry
10908670SJose.Borrego@Sun.COM  *      errno - error
10918670SJose.Borrego@Sun.COM  */
10928670SJose.Borrego@Sun.COM static int
10938670SJose.Borrego@Sun.COM smb_odir_wildcard_fileinfo(smb_request_t *sr, smb_odir_t *od,
10948670SJose.Borrego@Sun.COM     smb_odirent_t *odirent, smb_fileinfo_t *fileinfo)
10958670SJose.Borrego@Sun.COM {
10968670SJose.Borrego@Sun.COM 	int		rc;
10978670SJose.Borrego@Sun.COM 	smb_node_t	*fnode, *tgt_node;
10988670SJose.Borrego@Sun.COM 	smb_attr_t	attr, tgt_attr, *fattr;
10998670SJose.Borrego@Sun.COM 	char		*name;
11008670SJose.Borrego@Sun.COM 	uint32_t	dosattr;
11018670SJose.Borrego@Sun.COM 	boolean_t	case_conflict;
11028670SJose.Borrego@Sun.COM 
11038670SJose.Borrego@Sun.COM 	ASSERT(sr);
11048670SJose.Borrego@Sun.COM 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
11058670SJose.Borrego@Sun.COM 	ASSERT(od);
11068670SJose.Borrego@Sun.COM 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
11078670SJose.Borrego@Sun.COM 
11088670SJose.Borrego@Sun.COM 	ASSERT(MUTEX_HELD(&od->d_mutex));
11098670SJose.Borrego@Sun.COM 	bzero(fileinfo, sizeof (smb_fileinfo_t));
11108670SJose.Borrego@Sun.COM 
11119231SAfshin.Ardakani@Sun.COM 	case_conflict = ((od->d_flags & SMB_ODIR_FLAG_IGNORE_CASE) &&
11128670SJose.Borrego@Sun.COM 	    (odirent->od_eflags & ED_CASE_CONFLICT));
11138670SJose.Borrego@Sun.COM 	(void) smb_mangle_name(odirent->od_ino, odirent->od_name,
11148670SJose.Borrego@Sun.COM 	    fileinfo->fi_shortname, fileinfo->fi_name83, case_conflict);
11158670SJose.Borrego@Sun.COM 	name = (case_conflict) ? fileinfo->fi_shortname : odirent->od_name;
11168670SJose.Borrego@Sun.COM 	(void) strlcpy(fileinfo->fi_name, name, sizeof (fileinfo->fi_name));
11178670SJose.Borrego@Sun.COM 
11189343SAfshin.Ardakani@Sun.COM 	rc = smb_fsop_lookup(sr, od->d_cred, 0, od->d_tree->t_snode,
11199231SAfshin.Ardakani@Sun.COM 	    od->d_dnode, name, &fnode, &attr);
11208670SJose.Borrego@Sun.COM 	if (rc != 0)
11218670SJose.Borrego@Sun.COM 		return (rc);
11228670SJose.Borrego@Sun.COM 
11238670SJose.Borrego@Sun.COM 	/* follow link to get target node & attr */
11248670SJose.Borrego@Sun.COM 	if ((fnode->vp->v_type == VLNK) &&
11258670SJose.Borrego@Sun.COM 	    (smb_odir_lookup_link(sr, od, name, &tgt_node, &tgt_attr))) {
11268670SJose.Borrego@Sun.COM 		smb_node_release(fnode);
11278670SJose.Borrego@Sun.COM 		fnode = tgt_node;
11288670SJose.Borrego@Sun.COM 		fattr = &tgt_attr;
11298670SJose.Borrego@Sun.COM 	} else {
11308670SJose.Borrego@Sun.COM 		fattr = &attr;
11318670SJose.Borrego@Sun.COM 	}
11328670SJose.Borrego@Sun.COM 
11338670SJose.Borrego@Sun.COM 	/* check search attributes */
11348670SJose.Borrego@Sun.COM 	dosattr = smb_node_get_dosattr(fnode);
11358934SJose.Borrego@Sun.COM 	if (!smb_sattr_check(dosattr, od->d_sattr)) {
11368670SJose.Borrego@Sun.COM 		smb_node_release(fnode);
11378670SJose.Borrego@Sun.COM 		return (ENOENT);
11388670SJose.Borrego@Sun.COM 	}
11398670SJose.Borrego@Sun.COM 
11408670SJose.Borrego@Sun.COM 	fileinfo->fi_cookie = (uint32_t)od->d_offset;
11418670SJose.Borrego@Sun.COM 	fileinfo->fi_dosattr = dosattr;
11428670SJose.Borrego@Sun.COM 	fileinfo->fi_nodeid = fattr->sa_vattr.va_nodeid;
11438670SJose.Borrego@Sun.COM 	fileinfo->fi_size = smb_node_get_size(fnode, fattr);
11448670SJose.Borrego@Sun.COM 	fileinfo->fi_alloc_size = fattr->sa_vattr.va_nblocks * DEV_BSIZE;
11458670SJose.Borrego@Sun.COM 	fileinfo->fi_atime = fattr->sa_vattr.va_atime;
11468670SJose.Borrego@Sun.COM 	fileinfo->fi_mtime = fattr->sa_vattr.va_mtime;
11478670SJose.Borrego@Sun.COM 	fileinfo->fi_ctime = fattr->sa_vattr.va_ctime;
11488670SJose.Borrego@Sun.COM 	if (fattr->sa_crtime.tv_sec)
11498670SJose.Borrego@Sun.COM 		fileinfo->fi_crtime = fattr->sa_crtime;
11508670SJose.Borrego@Sun.COM 	else
11518670SJose.Borrego@Sun.COM 		fileinfo->fi_crtime = fattr->sa_vattr.va_mtime;
11528670SJose.Borrego@Sun.COM 
11538670SJose.Borrego@Sun.COM 	smb_node_release(fnode);
11548670SJose.Borrego@Sun.COM 	return (0);
11558670SJose.Borrego@Sun.COM }
11568670SJose.Borrego@Sun.COM 
11578670SJose.Borrego@Sun.COM /*
11588670SJose.Borrego@Sun.COM  * smb_odir_lookup_link
11598670SJose.Borrego@Sun.COM  *
11608670SJose.Borrego@Sun.COM  * If the file is a symlink we lookup the object to which the
11618670SJose.Borrego@Sun.COM  * symlink refers so that we can return its attributes.
11628670SJose.Borrego@Sun.COM  * This can cause a problem if a symlink in a sub-directory
11638670SJose.Borrego@Sun.COM  * points to a parent directory (some UNIX GUI's create a symlink
11648670SJose.Borrego@Sun.COM  * in $HOME/.desktop that points to the user's home directory).
11658670SJose.Borrego@Sun.COM  * Some Windows applications (e.g. virus scanning) loop/hang
11668670SJose.Borrego@Sun.COM  * trying to follow this recursive path and there is little
11678670SJose.Borrego@Sun.COM  * we can do because the path is constructed on the client.
11688670SJose.Borrego@Sun.COM  * smb_dirsymlink_enable allows an end-user to disable
11698670SJose.Borrego@Sun.COM  * symlinks to directories. Symlinks to other object types
11708670SJose.Borrego@Sun.COM  * should be unaffected.
11718670SJose.Borrego@Sun.COM  *
11728670SJose.Borrego@Sun.COM  * Returns:  B_TRUE - followed link. tgt_node and tgt_attr set
11738670SJose.Borrego@Sun.COM  *          B_FALSE - link not followed
11748670SJose.Borrego@Sun.COM  */
11758670SJose.Borrego@Sun.COM static boolean_t
11768670SJose.Borrego@Sun.COM smb_odir_lookup_link(smb_request_t *sr, smb_odir_t *od,
11778670SJose.Borrego@Sun.COM     char *fname, smb_node_t **tgt_node, smb_attr_t *tgt_attr)
11788670SJose.Borrego@Sun.COM {
11798670SJose.Borrego@Sun.COM 	int rc;
11808670SJose.Borrego@Sun.COM 
11819343SAfshin.Ardakani@Sun.COM 	rc = smb_fsop_lookup(sr, od->d_cred, SMB_FOLLOW_LINKS,
11829231SAfshin.Ardakani@Sun.COM 	    od->d_tree->t_snode, od->d_dnode, fname, tgt_node, tgt_attr);
11838670SJose.Borrego@Sun.COM 	if (rc != 0) {
11848670SJose.Borrego@Sun.COM 		*tgt_node = NULL;
11858670SJose.Borrego@Sun.COM 		return (B_FALSE);
11868670SJose.Borrego@Sun.COM 	}
11878670SJose.Borrego@Sun.COM 
11888670SJose.Borrego@Sun.COM 	if ((tgt_attr->sa_vattr.va_type == VDIR) && (!smb_dirsymlink_enable)) {
11898670SJose.Borrego@Sun.COM 		smb_node_release(*tgt_node);
11908670SJose.Borrego@Sun.COM 		*tgt_node = NULL;
11918670SJose.Borrego@Sun.COM 		return (B_FALSE);
11928670SJose.Borrego@Sun.COM 	}
11938670SJose.Borrego@Sun.COM 
11948670SJose.Borrego@Sun.COM 	return (B_TRUE);
11958670SJose.Borrego@Sun.COM }
1196