xref: /onnv-gate/usr/src/uts/common/fs/smbsrv/smb_node.c (revision 6771:2e6e486314b6)
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 /*
225772Sas200622  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
235331Samw  * Use is subject to license terms.
245331Samw  */
255331Samw 
265331Samw #pragma ident	"%Z%%M%	%I%	%E% SMI"
275331Samw 
285331Samw /*
295331Samw  * SMB Node State Machine
305331Samw  * ----------------------
315331Samw  *
325331Samw  *    +----------------------------+	 T0
335331Samw  *    |  SMB_NODE_STATE_AVAILABLE  |<----------- Creation/Allocation
345331Samw  *    +----------------------------+
355331Samw  *		    |
365331Samw  *		    | T1
375331Samw  *		    |
385331Samw  *		    v
395331Samw  *    +-----------------------------+    T2
405331Samw  *    |  SMB_NODE_STATE_DESTROYING  |----------> Deletion/Free
415331Samw  *    +-----------------------------+
425331Samw  *
435331Samw  * Transition T0
445331Samw  *
455331Samw  *    This transition occurs in smb_node_lookup(). If the node looked for is
465331Samw  *    not found in the has table a new node is created. The reference count is
475331Samw  *    initialized to 1 and the state initialized to SMB_NODE_STATE_AVAILABLE.
485331Samw  *
495331Samw  * Transition T1
505331Samw  *
515331Samw  *    This transition occurs in smb_node_release(). If the reference count
525331Samw  *    drops to zero the state is moved to SMB_NODE_STATE_DESTROYING and no more
535331Samw  *    reference count will be given out for that node.
545331Samw  *
555331Samw  * Transition T2
565331Samw  *
575331Samw  *    This transition occurs in smb_node_release(). The structure is deleted.
585331Samw  *
595331Samw  * Comments
605331Samw  * --------
615331Samw  *
625331Samw  *    The reason the smb node has 2 states is the following synchronization
635331Samw  *    rule:
645331Samw  *
655331Samw  *    There's a mutex embedded in the node used to protect its fields and
665331Samw  *    there's a lock embedded in the bucket of the hash table the node belongs
675331Samw  *    to. To increment or to decrement the reference count the mutex must be
685331Samw  *    entered. To insert the node into the bucket and to remove it from the
695331Samw  *    bucket the lock must be entered in RW_WRITER mode. When both (mutex and
705331Samw  *    lock) have to be entered, the lock has always to be entered first then
715331Samw  *    the mutex. This prevents a deadlock between smb_node_lookup() and
725331Samw  *    smb_node_release() from occurring. However, in smb_node_release() when the
735331Samw  *    reference count drops to zero and triggers the deletion of the node, the
745331Samw  *    mutex has to be released before entering the lock of the bucket (to
755331Samw  *    remove the node). This creates a window during which the node that is
765331Samw  *    about to be freed could be given out by smb_node_lookup(). To close that
775331Samw  *    window the node is moved to the state SMB_NODE_STATE_DESTROYING before
785331Samw  *    releasing the mutex. That way, even if smb_node_lookup() finds it, the
795331Samw  *    state will indicate that the node should be treated as non existent (of
805331Samw  *    course the state of the node should be tested/updated under the
815331Samw  *    protection of the mutex).
825331Samw  */
835331Samw #include <smbsrv/smb_incl.h>
845331Samw #include <smbsrv/smb_fsops.h>
855331Samw #include <sys/pathname.h>
865331Samw #include <sys/sdt.h>
875772Sas200622 #include <sys/nbmlock.h>
885331Samw 
895331Samw uint32_t smb_is_executable(char *path);
905331Samw static void smb_node_delete_on_close(smb_node_t *node);
915331Samw 
925331Samw #define	VALIDATE_DIR_NODE(_dir_, _node_) \
935331Samw     ASSERT((_dir_)->n_magic == SMB_NODE_MAGIC); \
945331Samw     ASSERT(((_dir_)->vp->v_xattrdir) || ((_dir_)->vp->v_type == VDIR)); \
955331Samw     ASSERT((_dir_)->dir_snode != (_node_));
965331Samw 
976139Sjb150015 static boolean_t	smb_node_initialized = B_FALSE;
986139Sjb150015 static smb_llist_t	smb_node_hash_table[SMBND_HASH_MASK+1];
996139Sjb150015 
1006139Sjb150015 /*
1016139Sjb150015  * smb_node_init
1026139Sjb150015  *
1036139Sjb150015  * Initialization of the SMB node layer.
1046139Sjb150015  *
1056139Sjb150015  * This function is not multi-thread safe. The caller must make sure only one
1066139Sjb150015  * thread makes the call.
1076139Sjb150015  */
1086139Sjb150015 int
1096139Sjb150015 smb_node_init(void)
1106139Sjb150015 {
1116139Sjb150015 	int	i;
1126139Sjb150015 
1136139Sjb150015 	if (smb_node_initialized)
1146139Sjb150015 		return (0);
1156139Sjb150015 
1166139Sjb150015 	for (i = 0; i <= SMBND_HASH_MASK; i++) {
1176139Sjb150015 		smb_llist_constructor(&smb_node_hash_table[i],
1186139Sjb150015 		    sizeof (smb_node_t), offsetof(smb_node_t, n_lnd));
1196139Sjb150015 	}
1206139Sjb150015 	smb_node_initialized = B_TRUE;
1216139Sjb150015 	return (0);
1226139Sjb150015 }
1236139Sjb150015 
1246139Sjb150015 /*
1256139Sjb150015  * smb_node_fini
1266139Sjb150015  *
1276139Sjb150015  * This function is not multi-thread safe. The caller must make sure only one
1286139Sjb150015  * thread makes the call.
1296139Sjb150015  */
1306139Sjb150015 void
1316139Sjb150015 smb_node_fini(void)
1326139Sjb150015 {
1336139Sjb150015 	int	i;
1346139Sjb150015 
1356139Sjb150015 	if (!smb_node_initialized)
1366139Sjb150015 		return;
1376139Sjb150015 
1386139Sjb150015 #ifdef DEBUG
1396139Sjb150015 	for (i = 0; i <= SMBND_HASH_MASK; i++) {
1406139Sjb150015 		smb_node_t	*node;
1416139Sjb150015 
1426139Sjb150015 		/*
1436139Sjb150015 		 * The following sequence is just intended for sanity check.
1446139Sjb150015 		 * This will have to be modified when the code goes into
1456139Sjb150015 		 * production.
1466139Sjb150015 		 *
1476139Sjb150015 		 * The SMB node hash table should be emtpy at this point. If the
1486139Sjb150015 		 * hash table is not empty a panic will be triggered.
1496139Sjb150015 		 *
1506139Sjb150015 		 * The reason why SMB nodes are still remaining in the hash
1516139Sjb150015 		 * table is problably due to a mismatch between calls to
1526139Sjb150015 		 * smb_node_lookup() and smb_node_release(). You must track that
1536139Sjb150015 		 * down.
1546139Sjb150015 		 */
1556139Sjb150015 		node = smb_llist_head(&smb_node_hash_table[i]);
1566139Sjb150015 		ASSERT(node == NULL);
1576139Sjb150015 	}
1586139Sjb150015 #endif
1596139Sjb150015 
1606139Sjb150015 	for (i = 0; i <= SMBND_HASH_MASK; i++) {
1616139Sjb150015 		smb_llist_destructor(&smb_node_hash_table[i]);
1626139Sjb150015 	}
1636139Sjb150015 	smb_node_initialized = B_FALSE;
1646139Sjb150015 }
1656139Sjb150015 
1665331Samw /*
1675331Samw  * smb_node_lookup()
1685331Samw  *
1695331Samw  * NOTE: This routine should only be called by the file system interface layer,
1705331Samw  * and not by SMB.
1715331Samw  *
1725331Samw  * smb_node_lookup() is called upon successful lookup, mkdir, and create
1735331Samw  * (for both non-streams and streams).  In each of these cases, a held vnode is
1745331Samw  * passed into this routine.  If an smb_node already exists for this vnode,
1755331Samw  * the vp is released.  Otherwise, a new smb_node will be created and the
1765331Samw  * reference will be held until the refcnt on the node goes to 0 (see
1775331Samw  * smb_node_release()).
1785331Samw  *
1795331Samw  * A reference is taken on the smb_node whether found in the hash table
1805331Samw  * or newly created.
1815331Samw  *
1825331Samw  * If an smb_node needs to be created, a reference is also taken on the
1835331Samw  * dir_snode (if passed in).
1845331Samw  *
1855331Samw  * See smb_node_release() for details on the release of these references.
1865331Samw  */
1875331Samw 
1885331Samw /*ARGSUSED*/
1895331Samw smb_node_t *
1905331Samw smb_node_lookup(
1915331Samw     struct smb_request	*sr,
1925331Samw     struct open_param	*op,
1935331Samw     cred_t		*cred,
1945331Samw     vnode_t		*vp,
1955331Samw     char		*od_name,
1965331Samw     smb_node_t		*dir_snode,
1975331Samw     smb_node_t		*unnamed_node,
1985331Samw     smb_attr_t		*attr)
1995331Samw {
2005331Samw 	smb_llist_t		*node_hdr;
2015331Samw 	smb_node_t		*node;
2025331Samw 	uint32_t		hashkey = 0;
2035331Samw 	fs_desc_t		fsd;
2045331Samw 	int			error;
2055331Samw 	krw_t			lock_mode;
2065331Samw 	vnode_t			*unnamed_vp = NULL;
2075331Samw 
2085331Samw 	/*
2095331Samw 	 * smb_vop_getattr() is called here instead of smb_fsop_getattr(),
2105331Samw 	 * because the node may not yet exist.  We also do not want to call
2115331Samw 	 * it with the list lock held.
2125331Samw 	 */
2135331Samw 
2145331Samw 	if (unnamed_node)
2155331Samw 		unnamed_vp = unnamed_node->vp;
2165331Samw 
2175331Samw 	/*
2185331Samw 	 * This getattr is performed on behalf of the server
2195331Samw 	 * that's why kcred is used not the user's cred
2205331Samw 	 */
2215331Samw 	attr->sa_mask = SMB_AT_ALL;
2225772Sas200622 	error = smb_vop_getattr(vp, unnamed_vp, attr, 0, kcred);
2235331Samw 	if (error)
2245331Samw 		return (NULL);
2255331Samw 
2265331Samw 	if (sr) {
2275331Samw 		if (sr->tid_tree) {
2285331Samw 			/*
2295331Samw 			 * The fsd for a file is that of the tree, even
2305331Samw 			 * if the file resides in a different mountpoint
2315331Samw 			 * under the share.
2325331Samw 			 */
2335331Samw 			fsd = sr->tid_tree->t_fsd;
2345331Samw 		} else {
2355331Samw 			/*
2365331Samw 			 * This should be getting executed only for the
2375331Samw 			 * tree's root smb_node.
2385331Samw 			 */
2395331Samw 			fsd = vp->v_vfsp->vfs_fsid;
2405331Samw 		}
2415331Samw 	} else {
2425331Samw 		fsd = vp->v_vfsp->vfs_fsid;
2435331Samw 	}
2445331Samw 
2455331Samw 	hashkey = fsd.val[0] + attr->sa_vattr.va_nodeid;
2465331Samw 	hashkey += (hashkey >> 24) + (hashkey >> 16) + (hashkey >> 8);
2476139Sjb150015 	node_hdr = &smb_node_hash_table[(hashkey & SMBND_HASH_MASK)];
2485331Samw 	lock_mode = RW_READER;
2495331Samw 
2505331Samw 	smb_llist_enter(node_hdr, lock_mode);
2515331Samw 	for (;;) {
2525331Samw 		node = list_head(&node_hdr->ll_list);
2535331Samw 		while (node) {
2545331Samw 			ASSERT(node->n_magic == SMB_NODE_MAGIC);
2555331Samw 			ASSERT(node->n_hash_bucket == node_hdr);
2565331Samw 			if ((node->n_hashkey == hashkey) && (node->vp == vp)) {
2575331Samw 				smb_rwx_xenter(&node->n_lock);
2585331Samw 				DTRACE_PROBE1(smb_node_lookup_hit,
2595331Samw 				    smb_node_t *, node);
2605331Samw 				switch (node->n_state) {
2615331Samw 				case SMB_NODE_STATE_AVAILABLE:
2625331Samw 					/* The node was found. */
2635331Samw 					node->n_refcnt++;
2645331Samw 					if ((node->dir_snode == NULL) &&
2655331Samw 					    (dir_snode != NULL) &&
2665331Samw 					    (strcmp(od_name, "..") != 0) &&
2675331Samw 					    (strcmp(od_name, ".") != 0)) {
2685331Samw 						VALIDATE_DIR_NODE(dir_snode,
2695331Samw 						    node);
2705331Samw 						node->dir_snode = dir_snode;
2715331Samw 						smb_node_ref(dir_snode);
2725331Samw 					}
2735331Samw 					node->attr = *attr;
2745772Sas200622 					node->n_size = attr->sa_vattr.va_size;
2755331Samw 
2765331Samw 					smb_audit_node(node);
2775331Samw 					smb_rwx_xexit(&node->n_lock);
2785331Samw 					smb_llist_exit(node_hdr);
2795331Samw 					VN_RELE(vp);
2805331Samw 					return (node);
2815331Samw 
2825331Samw 				case SMB_NODE_STATE_DESTROYING:
2835331Samw 					/*
2845331Samw 					 * Although the node exists it is about
2855331Samw 					 * to be destroyed. We act as it hasn't
2865331Samw 					 * been found.
2875331Samw 					 */
2885331Samw 					smb_rwx_xexit(&node->n_lock);
2895331Samw 					break;
2905331Samw 				default:
2915331Samw 					/*
2925331Samw 					 * Although the node exists it is in an
2935331Samw 					 * unknown state. We act as it hasn't
2945331Samw 					 * been found.
2955331Samw 					 */
2965331Samw 					ASSERT(0);
2975331Samw 					smb_rwx_xexit(&node->n_lock);
2985331Samw 					break;
2995331Samw 				}
3005331Samw 			}
3015331Samw 			node = smb_llist_next(node_hdr, node);
3025331Samw 		}
3035331Samw 		if ((lock_mode == RW_READER) && smb_llist_upgrade(node_hdr)) {
3045331Samw 			lock_mode = RW_WRITER;
3055331Samw 			continue;
3065331Samw 		}
3075331Samw 		break;
3085331Samw 	}
3096139Sjb150015 	node = kmem_cache_alloc(sr->sr_server->si_cache_node, KM_SLEEP);
3105331Samw 	bzero(node, sizeof (smb_node_t));
3115331Samw 
3125331Samw 	node->n_state = SMB_NODE_STATE_AVAILABLE;
3135331Samw 	node->n_hash_bucket = node_hdr;
3146139Sjb150015 	node->n_sr = sr;
3156139Sjb150015 	node->vp = vp;
3166139Sjb150015 	node->n_hashkey = hashkey;
3176139Sjb150015 	node->n_refcnt = 1;
3186139Sjb150015 	node->tree_fsd = vp->v_vfsp->vfs_fsid;
3196139Sjb150015 	node->attr = *attr;
3206139Sjb150015 	node->flags |= NODE_FLAGS_ATTR_VALID;
3216139Sjb150015 	node->n_size = node->attr.sa_vattr.va_size;
3226139Sjb150015 	node->n_orig_session_id = sr->session->s_kid;
3236139Sjb150015 	node->n_orig_uid = crgetuid(sr->user_cr);
3246139Sjb150015 	node->n_cache = sr->sr_server->si_cache_node;
3255331Samw 
3266139Sjb150015 	ASSERT(od_name);
3276139Sjb150015 	(void) strlcpy(node->od_name, od_name, sizeof (node->od_name));
3286139Sjb150015 
3296139Sjb150015 	if (fsd_chkcap(&vp->v_vfsp->vfs_fsid, FSOLF_READONLY) > 0)
3305331Samw 		node->flags |= NODE_READ_ONLY;
3315331Samw 
3325331Samw 	smb_llist_constructor(&node->n_ofile_list, sizeof (smb_ofile_t),
3335331Samw 	    offsetof(smb_ofile_t, f_nnd));
3345331Samw 	smb_llist_constructor(&node->n_lock_list, sizeof (smb_lock_t),
3355331Samw 	    offsetof(smb_lock_t, l_lnd));
3365331Samw 
3375331Samw 
3385967Scp160787 	if (strcmp(od_name, XATTR_DIR) == 0)
3395967Scp160787 		node->flags |= NODE_XATTR_DIR;
3405331Samw 	if (op)
3415331Samw 		node->flags |= smb_is_executable(op->fqi.last_comp);
3425331Samw 
3435331Samw 	if (dir_snode) {
3445331Samw 		smb_node_ref(dir_snode);
3455331Samw 		node->dir_snode = dir_snode;
3465331Samw 		ASSERT(dir_snode->dir_snode != node);
3475331Samw 		ASSERT((dir_snode->vp->v_xattrdir) ||
3485331Samw 		    (dir_snode->vp->v_type == VDIR));
3495331Samw 	}
3505331Samw 
3515331Samw 	if (unnamed_node) {
3525331Samw 		smb_node_ref(unnamed_node);
3535331Samw 		node->unnamed_stream_node = unnamed_node;
3545331Samw 	}
3555331Samw 
3565331Samw 	smb_rwx_init(&node->n_lock);
3575331Samw 	node->n_magic = SMB_NODE_MAGIC;
3585331Samw 	smb_audit_buf_node_create(node);
3595331Samw 	DTRACE_PROBE1(smb_node_lookup_miss, smb_node_t *, node);
3605331Samw 	smb_audit_node(node);
3615331Samw 	smb_llist_insert_head(node_hdr, node);
3626139Sjb150015 
3635331Samw 	smb_llist_exit(node_hdr);
3645331Samw 	return (node);
3655331Samw }
3665331Samw 
3675331Samw /*
3685331Samw  * smb_stream_node_lookup()
3695331Samw  *
3705331Samw  * Note: stream_name (the name that will be stored in the "od_name" field
3715331Samw  * of a stream's smb_node) is the same as the on-disk name for the stream
3725331Samw  * except that it does not have SMB_STREAM_PREFIX prepended.
3735331Samw  */
3745331Samw 
3755331Samw smb_node_t *
3765331Samw smb_stream_node_lookup(struct smb_request *sr, cred_t *cr, smb_node_t *fnode,
3775331Samw     vnode_t *xattrdirvp, vnode_t *vp, char *stream_name, smb_attr_t *ret_attr)
3785331Samw {
3795331Samw 	smb_node_t	*xattrdir_node;
3805331Samw 	smb_node_t	*snode;
3815331Samw 	smb_attr_t	tmp_attr;
3825331Samw 
3835331Samw 	xattrdir_node = smb_node_lookup(sr, NULL, cr, xattrdirvp, XATTR_DIR,
3845331Samw 	    fnode, NULL, &tmp_attr);
3855331Samw 
3865331Samw 	if (xattrdir_node == NULL)
3875331Samw 		return (NULL);
3885331Samw 
3895331Samw 	snode = smb_node_lookup(sr, NULL, cr, vp, stream_name, xattrdir_node,
3905331Samw 	    fnode, ret_attr);
3915331Samw 
3925331Samw 	/*
3935331Samw 	 * The following VN_HOLD is necessary because the caller will VN_RELE
3945331Samw 	 * xattrdirvp in the case of an error.  (xattrdir_node has the original
3955331Samw 	 * hold on the vnode, which the smb_node_release() call below will
3965331Samw 	 * release.)
3975331Samw 	 */
3985331Samw 	if (snode == NULL) {
3995331Samw 		VN_HOLD(xattrdirvp);
4005331Samw 	}
4015331Samw 	(void) smb_node_release(xattrdir_node);
4025331Samw 	return (snode);
4035331Samw }
4045331Samw 
4055331Samw 
4065331Samw /*
4075331Samw  * This function should be called whenever a reference is needed on an
4085331Samw  * smb_node pointer.  The copy of an smb_node pointer from one non-local
4095331Samw  * data structure to another requires a reference to be taken on the smb_node
4105331Samw  * (unless the usage is localized).  Each data structure deallocation routine
4115331Samw  * will call smb_node_release() on its smb_node pointers.
4125331Samw  *
4135331Samw  * In general, an smb_node pointer residing in a structure should never be
4145331Samw  * stale.  A node pointer may be NULL, however, and care should be taken
4155331Samw  * prior to calling smb_node_ref(), which ASSERTs that the pointer is valid.
4165331Samw  * Care also needs to be taken with respect to racing deallocations of a
4175331Samw  * structure.
4185331Samw  */
4195331Samw 
4205331Samw void
4215331Samw smb_node_ref(smb_node_t *node)
4225331Samw {
4235331Samw 	ASSERT(node);
4245331Samw 	ASSERT(node->n_magic == SMB_NODE_MAGIC);
4255331Samw 	ASSERT(node->n_state == SMB_NODE_STATE_AVAILABLE);
4265331Samw 
4275331Samw 	smb_rwx_xenter(&node->n_lock);
4285331Samw 	node->n_refcnt++;
4295331Samw 	ASSERT(node->n_refcnt);
4305331Samw 	DTRACE_PROBE1(smb_node_ref_exit, smb_node_t *, node);
4315331Samw 	smb_audit_node(node);
4325331Samw 	smb_rwx_xexit(&node->n_lock);
4335331Samw }
4345331Samw 
4355331Samw /*
4365331Samw  * smb_node_lookup() takes a hold on an smb_node, whether found in the
4375331Samw  * hash table or newly created.  This hold is expected to be released
4385331Samw  * in the following manner.
4395331Samw  *
4405331Samw  * smb_node_lookup() takes an address of an smb_node pointer.  This should
4415331Samw  * be getting passed down via a lookup (whether path name or component), mkdir,
4425331Samw  * create.  If the original smb_node pointer resides in a data structure, then
4435331Samw  * the deallocation routine for the data structure is responsible for calling
4445331Samw  * smb_node_release() on the smb_node pointer.  Alternatively,
4455331Samw  * smb_node_release() can be called as soon as the smb_node pointer is no longer
4465331Samw  * needed.  In this case, callers are responsible for setting an embedded
4475331Samw  * pointer to NULL if it is known that the last reference is being released.
4485331Samw  *
4495331Samw  * If the passed-in address of the smb_node pointer belongs to a local variable,
4505331Samw  * then the caller with the local variable should call smb_node_release()
4515331Samw  * directly.
4525331Samw  *
4535331Samw  * smb_node_release() itself will call smb_node_release() on a node's dir_snode,
4545331Samw  * as smb_node_lookup() takes a hold on dir_snode.
4555331Samw  */
4565331Samw void
4575331Samw smb_node_release(smb_node_t *node)
4585331Samw {
4595331Samw 	ASSERT(node);
4605331Samw 	ASSERT(node->n_magic == SMB_NODE_MAGIC);
4615331Samw 
4625331Samw 	smb_rwx_xenter(&node->n_lock);
4635331Samw 	ASSERT(node->n_refcnt);
4645331Samw 	DTRACE_PROBE1(smb_node_release, smb_node_t *, node);
4655331Samw 	if (--node->n_refcnt == 0) {
4665331Samw 		switch (node->n_state) {
4675331Samw 
4685331Samw 		case SMB_NODE_STATE_AVAILABLE:
4695331Samw 			node->n_state = SMB_NODE_STATE_DESTROYING;
4705331Samw 			smb_rwx_xexit(&node->n_lock);
4715331Samw 
4725331Samw 			smb_llist_enter(node->n_hash_bucket, RW_WRITER);
4735331Samw 			smb_llist_remove(node->n_hash_bucket, node);
4745331Samw 			smb_llist_exit(node->n_hash_bucket);
4755331Samw 
4765331Samw 			/*
4775331Samw 			 * Check if the file was deleted
4785331Samw 			 */
4795331Samw 			smb_node_delete_on_close(node);
4805331Samw 			node->n_magic = (uint32_t)~SMB_NODE_MAGIC;
4815331Samw 
4825331Samw 			/* These lists should be empty. */
4835331Samw 			smb_llist_destructor(&node->n_ofile_list);
4845331Samw 			smb_llist_destructor(&node->n_lock_list);
4855331Samw 
4865331Samw 			if (node->dir_snode) {
4875331Samw 				ASSERT(node->dir_snode->n_magic ==
4885331Samw 				    SMB_NODE_MAGIC);
4895331Samw 				smb_node_release(node->dir_snode);
4905331Samw 			}
4915331Samw 
4925331Samw 			if (node->unnamed_stream_node) {
4935331Samw 				ASSERT(node->unnamed_stream_node->n_magic ==
4945331Samw 				    SMB_NODE_MAGIC);
4955331Samw 				smb_node_release(node->unnamed_stream_node);
4965331Samw 			}
4975331Samw 
4985331Samw 			ASSERT(node->vp);
4995331Samw 			VN_RELE(node->vp);
5005331Samw 
5015331Samw 			smb_audit_buf_node_destroy(node);
5025331Samw 			smb_rwx_destroy(&node->n_lock);
5036139Sjb150015 			kmem_cache_free(node->n_cache, node);
5045331Samw 			return;
5055331Samw 
5065331Samw 		default:
5075331Samw 			ASSERT(0);
5085331Samw 			break;
5095331Samw 		}
5105331Samw 	}
5115331Samw 	smb_audit_node(node);
5125331Samw 	smb_rwx_xexit(&node->n_lock);
5135331Samw }
5145331Samw 
5155331Samw static void
5165331Samw smb_node_delete_on_close(smb_node_t *node)
5175331Samw {
5185331Samw 	smb_node_t	*d_snode;
5195331Samw 	int		rc = 0;
5205331Samw 
5215331Samw 	d_snode = node->dir_snode;
5225331Samw 	if (node->flags & NODE_FLAGS_DELETE_ON_CLOSE) {
5235331Samw 
5245331Samw 		node->flags &= ~NODE_FLAGS_DELETE_ON_CLOSE;
5255331Samw 		ASSERT(node->od_name != NULL);
5265331Samw 		if (node->attr.sa_vattr.va_type == VDIR)
5275331Samw 			rc = smb_fsop_rmdir(0, node->delete_on_close_cred,
5285331Samw 			    d_snode, node->od_name, 1);
5295331Samw 		else
5305331Samw 			rc = smb_fsop_remove(0, node->delete_on_close_cred,
5315331Samw 			    d_snode, node->od_name, 1);
5325331Samw 		smb_cred_rele(node->delete_on_close_cred);
5335331Samw 	}
5345331Samw 	if (rc != 0)
5355331Samw 		cmn_err(CE_WARN, "File %s could not be removed, rc=%d\n",
5365331Samw 		    node->od_name, rc);
5375331Samw 	DTRACE_PROBE2(smb_node_delete_on_close, int, rc, smb_node_t *, node);
5385331Samw }
5395331Samw 
5405331Samw /*
5415331Samw  * smb_node_rename()
5425331Samw  *
5435331Samw  */
5445331Samw int
5455331Samw smb_node_rename(
5465331Samw     smb_node_t	*from_dir_snode,
5475331Samw     smb_node_t	*ret_snode,
5485331Samw     smb_node_t	*to_dir_snode,
5495331Samw     char	*to_name)
5505331Samw {
5515331Samw 	ASSERT(from_dir_snode);
5525331Samw 	ASSERT(to_dir_snode);
5535331Samw 	ASSERT(ret_snode);
5545331Samw 	ASSERT(from_dir_snode->n_magic == SMB_NODE_MAGIC);
5555331Samw 	ASSERT(to_dir_snode->n_magic == SMB_NODE_MAGIC);
5565331Samw 	ASSERT(ret_snode->n_magic == SMB_NODE_MAGIC);
5575331Samw 	ASSERT(from_dir_snode->n_state == SMB_NODE_STATE_AVAILABLE);
5585331Samw 	ASSERT(to_dir_snode->n_state == SMB_NODE_STATE_AVAILABLE);
5595331Samw 	ASSERT(ret_snode->n_state == SMB_NODE_STATE_AVAILABLE);
5605331Samw 
5615331Samw 	smb_node_ref(to_dir_snode);
5625331Samw 	smb_rwx_xenter(&ret_snode->n_lock);
5635331Samw 	ret_snode->dir_snode = to_dir_snode;
5645331Samw 	smb_rwx_xexit(&ret_snode->n_lock);
5655331Samw 	ASSERT(to_dir_snode->dir_snode != ret_snode);
5665331Samw 	ASSERT((to_dir_snode->vp->v_xattrdir) ||
5675331Samw 	    (to_dir_snode->vp->v_type == VDIR));
5685331Samw 	smb_node_release(from_dir_snode);
5695331Samw 
5705331Samw 	(void) strcpy(ret_snode->od_name, to_name);
5715331Samw 
5725331Samw 	/*
5735331Samw 	 * XXX Need to update attributes?
5745331Samw 	 */
5755331Samw 
5765331Samw 	return (0);
5775331Samw }
5785331Samw 
5795331Samw int
5806139Sjb150015 smb_node_root_init(vnode_t *vp, smb_server_t *sv, smb_node_t **root)
5815331Samw {
5826139Sjb150015 	smb_attr_t	va;
5836139Sjb150015 	int		error;
5846139Sjb150015 	uint32_t	hashkey;
5856139Sjb150015 	smb_llist_t	*node_hdr;
5866139Sjb150015 	smb_node_t	*node;
5875331Samw 
5885331Samw 	/*
5895331Samw 	 * Take an explicit hold on rootdir.  This goes with the
5905331Samw 	 * corresponding release in smb_node_root_fini()/smb_node_release().
5915331Samw 	 */
5926139Sjb150015 	VN_HOLD(vp);
5935331Samw 
5946139Sjb150015 	va.sa_mask = SMB_AT_ALL;
5956139Sjb150015 	error = smb_vop_getattr(vp, NULL, &va, 0, kcred);
5966139Sjb150015 	if (error) {
5976139Sjb150015 		VN_RELE(vp);
5986139Sjb150015 		return (error);
5996139Sjb150015 	}
6006139Sjb150015 
6016139Sjb150015 	hashkey = vp->v_vfsp->vfs_fsid.val[0] + va.sa_vattr.va_nodeid;
6026139Sjb150015 	hashkey += (hashkey >> 24) + (hashkey >> 16) + (hashkey >> 8);
6036139Sjb150015 	node_hdr = &smb_node_hash_table[(hashkey & SMBND_HASH_MASK)];
6046139Sjb150015 
6056139Sjb150015 	node = kmem_cache_alloc(sv->si_cache_node, KM_SLEEP);
6066139Sjb150015 	bzero(node, sizeof (smb_node_t));
6075331Samw 
6086139Sjb150015 	node->n_state = SMB_NODE_STATE_AVAILABLE;
6096139Sjb150015 	node->n_hash_bucket = node_hdr;
6106139Sjb150015 	node->vp = vp;
6116139Sjb150015 	node->n_hashkey = hashkey;
6126139Sjb150015 	node->n_refcnt = 1;
6136139Sjb150015 	node->tree_fsd = vp->v_vfsp->vfs_fsid;
6146139Sjb150015 	node->attr = va;
6156139Sjb150015 	node->flags |= NODE_FLAGS_ATTR_VALID;
6166139Sjb150015 	node->n_size = node->attr.sa_vattr.va_size;
6176139Sjb150015 	node->n_cache = sv->si_cache_node;
6186139Sjb150015 	(void) strlcpy(node->od_name, ROOTVOL, sizeof (node->od_name));
6196139Sjb150015 
6206139Sjb150015 	if (fsd_chkcap(&vp->v_vfsp->vfs_fsid, FSOLF_READONLY) > 0)
6216139Sjb150015 		node->flags |= NODE_READ_ONLY;
6225331Samw 
6236139Sjb150015 	smb_llist_constructor(&node->n_ofile_list, sizeof (smb_ofile_t),
6246139Sjb150015 	    offsetof(smb_ofile_t, f_nnd));
6256139Sjb150015 	smb_llist_constructor(&node->n_lock_list, sizeof (smb_lock_t),
6266139Sjb150015 	    offsetof(smb_lock_t, l_lnd));
6276139Sjb150015 
6286139Sjb150015 	smb_rwx_init(&node->n_lock);
6296139Sjb150015 	node->n_magic = SMB_NODE_MAGIC;
6306139Sjb150015 	smb_audit_buf_node_create(node);
6316139Sjb150015 
6326139Sjb150015 	sv->si_root_smb_node = node;
6336139Sjb150015 
6346139Sjb150015 	smb_audit_node(node);
6356139Sjb150015 	smb_llist_enter(node_hdr, RW_WRITER);
6366139Sjb150015 	smb_llist_insert_head(node_hdr, node);
6376139Sjb150015 	smb_llist_exit(node_hdr);
6386139Sjb150015 
6396139Sjb150015 	*root = node;
6406139Sjb150015 
6416139Sjb150015 	return (0);
6425331Samw }
6435331Samw 
6445331Samw /*
6455331Samw  * smb_node_get_size
6465331Samw  */
6476432Sas200622 u_offset_t
6486432Sas200622 smb_node_get_size(smb_node_t *node, smb_attr_t *attr)
6495331Samw {
6506432Sas200622 	u_offset_t size;
6515331Samw 
6525331Samw 	if (attr->sa_vattr.va_type == VDIR)
6535331Samw 		return (0);
6545331Samw 
6555331Samw 	smb_rwx_xenter(&node->n_lock);
6565331Samw 	if (node && (node->flags & NODE_FLAGS_SET_SIZE))
6575331Samw 		size = node->n_size;
6585331Samw 	else
6595331Samw 		size = attr->sa_vattr.va_size;
6605331Samw 	smb_rwx_xexit(&node->n_lock);
6615331Samw 	return (size);
6625331Samw }
6635331Samw 
6645331Samw static int
6655331Samw timeval_cmp(timestruc_t *a, timestruc_t *b)
6665331Samw {
6675331Samw 	if (a->tv_sec < b->tv_sec)
6685331Samw 		return (-1);
6695331Samw 	if (a->tv_sec > b->tv_sec)
6705331Samw 		return (1);
6715331Samw 	/* Seconds are equal compare tv_nsec */
6725331Samw 	if (a->tv_nsec < b->tv_nsec)
6735331Samw 		return (-1);
6745331Samw 	return (a->tv_nsec > b->tv_nsec);
6755331Samw }
6765331Samw 
6775331Samw /*
6785331Samw  * smb_node_set_time
6795331Samw  *
6805331Samw  * This function will update the time stored in the node and
6815331Samw  * set the appropriate flags. If there is nothing to update
6825331Samw  * or the node is readonly, the function would return without
6835331Samw  * any updates. The update is only in the node level and the
6845331Samw  * attribute in the file system will be updated when client
6855331Samw  * close the file.
6865331Samw  */
6875331Samw void
6885331Samw smb_node_set_time(struct smb_node *node, struct timestruc *crtime,
6895331Samw     struct timestruc *mtime, struct timestruc *atime,
6905331Samw     struct timestruc *ctime, unsigned int what)
6915331Samw {
6925331Samw 	smb_rwx_xenter(&node->n_lock);
6935331Samw 	if (node->flags & NODE_READ_ONLY || what == 0) {
6945331Samw 		smb_rwx_xexit(&node->n_lock);
6955331Samw 		return;
6965331Samw 	}
6975331Samw 
6985331Samw 	if ((what & SMB_AT_CRTIME && crtime == 0) ||
6995331Samw 	    (what & SMB_AT_MTIME && mtime == 0) ||
7005331Samw 	    (what & SMB_AT_ATIME && atime == 0) ||
7015331Samw 	    (what & SMB_AT_CTIME && ctime == 0)) {
7025331Samw 		smb_rwx_xexit(&node->n_lock);
7035331Samw 		return;
7045331Samw 	}
7055331Samw 
7065331Samw 	if ((what & SMB_AT_CRTIME) &&
7075331Samw 	    timeval_cmp((timestruc_t *)&node->attr.sa_crtime,
7085331Samw 	    crtime) != 0) {
7095331Samw 		node->what |= SMB_AT_CRTIME;
7105331Samw 		node->attr.sa_crtime = *((timestruc_t *)crtime);
7115331Samw 	}
7125331Samw 
7135331Samw 	if ((what & SMB_AT_MTIME) &&
7145331Samw 	    timeval_cmp((timestruc_t *)&node->attr.sa_vattr.va_mtime,
7155331Samw 	    mtime) != 0) {
7165331Samw 		node->what |= SMB_AT_MTIME;
7175331Samw 		node->attr.sa_vattr.va_mtime = *((timestruc_t *)mtime);
7185331Samw 	}
7195331Samw 
7205331Samw 	if ((what & SMB_AT_ATIME) &&
7215331Samw 	    timeval_cmp((timestruc_t *)&node->attr.sa_vattr.va_atime,
7225331Samw 	    atime) != 0) {
7235331Samw 			node->what |= SMB_AT_ATIME;
7245331Samw 			node->attr.sa_vattr.va_atime = *((timestruc_t *)atime);
7255331Samw 	}
7265331Samw 
7275331Samw 	/*
7285331Samw 	 * The ctime handling is trickier. It has three scenarios.
7295331Samw 	 * 1. Only ctime need to be set and it is the same as the ctime
7305331Samw 	 *    stored in the node. (update not necessary)
7315331Samw 	 * 2. The ctime is the same as the ctime stored in the node but
7325331Samw 	 *    is not the only time need to be set. (update required)
7335331Samw 	 * 3. The ctime need to be set and is not the same as the ctime
7345331Samw 	 *    stored in the node. (update required)
7355331Samw 	 * Unlike other time setting, the ctime needs to be set even when
7365331Samw 	 * it is the same as the ctime in the node if there are other time
7375331Samw 	 * needs to be set (#2). This will ensure the ctime not being
7385331Samw 	 * updated when other times are being updated in the file system.
7395331Samw 	 *
7405331Samw 	 * Retained file rules:
7415331Samw 	 *
7425331Samw 	 * 1. Don't add SMB_AT_CTIME to node->what by default because the
7435331Samw 	 *    request will be rejected by filesystem
7445331Samw 	 * 2. 'what' SMB_AT_CTIME shouldn't be set for retained files, i.e.
7455331Samw 	 *    any request for changing ctime on these files should have
7465331Samw 	 *    been already rejected
7475331Samw 	 */
7485331Samw 	node->what |= SMB_AT_CTIME;
7495331Samw 	if (what & SMB_AT_CTIME) {
7505331Samw 		if ((what == SMB_AT_CTIME) &&
7515331Samw 		    timeval_cmp((timestruc_t *)&node->attr.sa_vattr.va_ctime,
7525331Samw 		    ctime) == 0) {
7535331Samw 			node->what &= ~SMB_AT_CTIME;
7545331Samw 		} else {
7555331Samw 			gethrestime(&node->attr.sa_vattr.va_ctime);
7565331Samw 		}
7575331Samw 	} else {
7585331Samw 		gethrestime(&node->attr.sa_vattr.va_ctime);
7595331Samw 	}
7605331Samw 	smb_rwx_xexit(&node->n_lock);
7615331Samw }
7625331Samw 
7635331Samw 
7645331Samw timestruc_t *
7655331Samw smb_node_get_crtime(smb_node_t *node)
7665331Samw {
7675331Samw 	return ((timestruc_t *)&node->attr.sa_crtime);
7685331Samw }
7695331Samw 
7705331Samw timestruc_t *
7715331Samw smb_node_get_atime(smb_node_t *node)
7725331Samw {
7735331Samw 	return ((timestruc_t *)&node->attr.sa_vattr.va_atime);
7745331Samw }
7755331Samw 
7765331Samw timestruc_t *
7775331Samw smb_node_get_ctime(smb_node_t *node)
7785331Samw {
7795331Samw 	return ((timestruc_t *)&node->attr.sa_vattr.va_ctime);
7805331Samw }
7815331Samw 
7825331Samw timestruc_t *
7835331Samw smb_node_get_mtime(smb_node_t *node)
7845331Samw {
7855331Samw 	return ((timestruc_t *)&node->attr.sa_vattr.va_mtime);
7865331Samw }
7875331Samw 
7885331Samw /*
7895331Samw  * smb_node_set_dosattr
7905331Samw  *
7915331Samw  * Parse the specified DOS attributes and, if they have been modified,
7925331Samw  * update the node cache. This call should be followed by a
7935331Samw  * smb_sync_fsattr() call to write the attribute changes to filesystem.
7945331Samw  */
7955331Samw void
7965331Samw smb_node_set_dosattr(smb_node_t *node, uint32_t dos_attr)
7975331Samw {
7985331Samw 	unsigned int mode;  /* New mode */
7995331Samw 
8005331Samw 	mode = 0;
8015331Samw 
8025331Samw 	/* Handle the archive bit */
8035331Samw 	if (dos_attr & SMB_FA_ARCHIVE)
8045331Samw 		mode |= FILE_ATTRIBUTE_ARCHIVE;
8055331Samw 
8065331Samw 	/* Handle the readonly bit */
8075331Samw 	if (dos_attr & SMB_FA_READONLY)
8085331Samw 		mode |= FILE_ATTRIBUTE_READONLY;
8095331Samw 
8105331Samw 	/* Handle the hidden bit */
8115331Samw 	if (dos_attr & SMB_FA_HIDDEN)
8125331Samw 		mode |= FILE_ATTRIBUTE_HIDDEN;
8135331Samw 
8145331Samw 	/* Handle the system bit */
8155331Samw 	if (dos_attr & SMB_FA_SYSTEM)
8165331Samw 		mode |= FILE_ATTRIBUTE_SYSTEM;
8175331Samw 
8185331Samw 	smb_rwx_xenter(&node->n_lock);
8195331Samw 	if (node->attr.sa_dosattr != mode) {
8205331Samw 		node->attr.sa_dosattr = mode;
8215331Samw 		node->what |= SMB_AT_DOSATTR;
8225331Samw 	}
8235331Samw 	smb_rwx_xexit(&node->n_lock);
8245331Samw }
8255331Samw 
8265331Samw /*
8275331Samw  * smb_node_get_dosattr
8285331Samw  *
8295331Samw  * This function will get dos attribute using the node.
8305331Samw  */
8315331Samw uint32_t
8325331Samw smb_node_get_dosattr(smb_node_t *node)
8335331Samw {
8345331Samw 	return (smb_mode_to_dos_attributes(&node->attr));
8355331Samw }
8365331Samw 
8375331Samw int
8385331Samw smb_node_set_delete_on_close(smb_node_t *node, cred_t *cr)
8395331Samw {
8405331Samw 	int	rc = -1;
8415331Samw 
8425331Samw 	smb_rwx_xenter(&node->n_lock);
8435331Samw 	if (!(node->attr.sa_dosattr & FILE_ATTRIBUTE_READONLY) &&
8445331Samw 	    !(node->flags & NODE_FLAGS_DELETE_ON_CLOSE)) {
8455331Samw 		crhold(cr);
8465331Samw 		node->delete_on_close_cred = cr;
8475331Samw 		node->flags |= NODE_FLAGS_DELETE_ON_CLOSE;
8485331Samw 		rc = 0;
8495331Samw 	}
8505331Samw 	smb_rwx_xexit(&node->n_lock);
8515331Samw 	return (rc);
8525331Samw }
8535331Samw 
8545331Samw void
8555331Samw smb_node_reset_delete_on_close(smb_node_t *node)
8565331Samw {
8575331Samw 	smb_rwx_xenter(&node->n_lock);
8585331Samw 	if (node->flags & NODE_FLAGS_DELETE_ON_CLOSE) {
8595331Samw 		node->flags &= ~NODE_FLAGS_DELETE_ON_CLOSE;
8605331Samw 		crfree(node->delete_on_close_cred);
8615331Samw 		node->delete_on_close_cred = NULL;
8625331Samw 	}
8635331Samw 	smb_rwx_xexit(&node->n_lock);
8645331Samw }
8655772Sas200622 
8665772Sas200622 /*
867*6771Sjb150015  * smb_node_open_check
8685772Sas200622  *
8695772Sas200622  * check file sharing rules for current open request
8705772Sas200622  * against all existing opens for a file.
8715772Sas200622  *
8725772Sas200622  * Returns NT_STATUS_SHARING_VIOLATION if there is any
8735772Sas200622  * sharing conflict, otherwise returns NT_STATUS_SUCCESS.
8745772Sas200622  */
8755772Sas200622 uint32_t
8765772Sas200622 smb_node_open_check(struct smb_node *node, cred_t *cr,
8775772Sas200622     uint32_t desired_access, uint32_t share_access)
8785772Sas200622 {
8795772Sas200622 	smb_ofile_t *of;
8805772Sas200622 	uint32_t status;
8815772Sas200622 
8825772Sas200622 	ASSERT(node);
8835772Sas200622 	ASSERT(node->n_magic == SMB_NODE_MAGIC);
8845772Sas200622 	ASSERT(node->n_state == SMB_NODE_STATE_AVAILABLE);
8855772Sas200622 
8865772Sas200622 	smb_llist_enter(&node->n_ofile_list, RW_READER);
8875772Sas200622 	of = smb_llist_head(&node->n_ofile_list);
8885772Sas200622 	while (of) {
889*6771Sjb150015 		status = smb_ofile_open_check(of, cr, desired_access,
890*6771Sjb150015 		    share_access);
891*6771Sjb150015 
892*6771Sjb150015 		switch (status) {
893*6771Sjb150015 		case NT_STATUS_INVALID_HANDLE:
894*6771Sjb150015 		case NT_STATUS_SUCCESS:
895*6771Sjb150015 			of = smb_llist_next(&node->n_ofile_list, of);
896*6771Sjb150015 			break;
897*6771Sjb150015 		default:
898*6771Sjb150015 			ASSERT(status == NT_STATUS_SHARING_VIOLATION);
8995772Sas200622 			smb_llist_exit(&node->n_ofile_list);
9005772Sas200622 			return (status);
9015772Sas200622 		}
9025772Sas200622 	}
903*6771Sjb150015 
9045772Sas200622 	smb_llist_exit(&node->n_ofile_list);
9055772Sas200622 	return (NT_STATUS_SUCCESS);
9065772Sas200622 }
9075772Sas200622 
9085772Sas200622 uint32_t
9095772Sas200622 smb_node_rename_check(struct smb_node *node)
9105772Sas200622 {
911*6771Sjb150015 	struct smb_ofile *of;
912*6771Sjb150015 	uint32_t status;
9135772Sas200622 
9145772Sas200622 	ASSERT(node);
9155772Sas200622 	ASSERT(node->n_magic == SMB_NODE_MAGIC);
9165772Sas200622 	ASSERT(node->n_state == SMB_NODE_STATE_AVAILABLE);
9175772Sas200622 
9185772Sas200622 	/*
9195772Sas200622 	 * Intra-CIFS check
9205772Sas200622 	 */
9215772Sas200622 
9225772Sas200622 	smb_llist_enter(&node->n_ofile_list, RW_READER);
923*6771Sjb150015 	of = smb_llist_head(&node->n_ofile_list);
924*6771Sjb150015 	while (of) {
925*6771Sjb150015 		status = smb_ofile_rename_check(of);
9265772Sas200622 
927*6771Sjb150015 		switch (status) {
928*6771Sjb150015 		case NT_STATUS_INVALID_HANDLE:
929*6771Sjb150015 		case NT_STATUS_SUCCESS:
930*6771Sjb150015 			of = smb_llist_next(&node->n_ofile_list, of);
931*6771Sjb150015 			break;
932*6771Sjb150015 		default:
933*6771Sjb150015 			ASSERT(status == NT_STATUS_SHARING_VIOLATION);
934*6771Sjb150015 			smb_llist_exit(&node->n_ofile_list);
935*6771Sjb150015 			return (status);
9365772Sas200622 		}
9375772Sas200622 	}
9385772Sas200622 	smb_llist_exit(&node->n_ofile_list);
9395772Sas200622 
9405772Sas200622 	/*
9415772Sas200622 	 * system-wide share check
9425772Sas200622 	 */
9435772Sas200622 
9445772Sas200622 	if (nbl_share_conflict(node->vp, NBL_RENAME, NULL))
9455772Sas200622 		return (NT_STATUS_SHARING_VIOLATION);
9465772Sas200622 	else
9475772Sas200622 		return (NT_STATUS_SUCCESS);
9485772Sas200622 }
9495772Sas200622 
950*6771Sjb150015 uint32_t
9515772Sas200622 smb_node_delete_check(smb_node_t *node)
9525772Sas200622 {
953*6771Sjb150015 	smb_ofile_t *of;
954*6771Sjb150015 	uint32_t status;
9555772Sas200622 
9565772Sas200622 	ASSERT(node);
9575772Sas200622 	ASSERT(node->n_magic == SMB_NODE_MAGIC);
9585772Sas200622 	ASSERT(node->n_state == SMB_NODE_STATE_AVAILABLE);
9595772Sas200622 
9605772Sas200622 	if (node->attr.sa_vattr.va_type == VDIR)
9615772Sas200622 		return (NT_STATUS_SUCCESS);
9625772Sas200622 
9635772Sas200622 	/*
9645772Sas200622 	 * intra-CIFS check
9655772Sas200622 	 */
9665772Sas200622 
9675772Sas200622 	smb_llist_enter(&node->n_ofile_list, RW_READER);
968*6771Sjb150015 	of = smb_llist_head(&node->n_ofile_list);
969*6771Sjb150015 	while (of) {
970*6771Sjb150015 		status = smb_ofile_delete_check(of);
971*6771Sjb150015 
972*6771Sjb150015 		switch (status) {
973*6771Sjb150015 		case NT_STATUS_INVALID_HANDLE:
974*6771Sjb150015 		case NT_STATUS_SUCCESS:
975*6771Sjb150015 			of = smb_llist_next(&node->n_ofile_list, of);
976*6771Sjb150015 			break;
977*6771Sjb150015 		default:
978*6771Sjb150015 			ASSERT(status == NT_STATUS_SHARING_VIOLATION);
9795772Sas200622 			smb_llist_exit(&node->n_ofile_list);
980*6771Sjb150015 			return (status);
9815772Sas200622 		}
9825772Sas200622 	}
9835772Sas200622 	smb_llist_exit(&node->n_ofile_list);
9845772Sas200622 
9855772Sas200622 	/*
9865772Sas200622 	 * system-wide share check
9875772Sas200622 	 */
9885772Sas200622 
9895772Sas200622 	if (nbl_share_conflict(node->vp, NBL_REMOVE, NULL))
9905772Sas200622 		return (NT_STATUS_SHARING_VIOLATION);
9915772Sas200622 	else
9925772Sas200622 		return (NT_STATUS_SUCCESS);
9935772Sas200622 }
9945772Sas200622 
9955772Sas200622 /*
9965772Sas200622  * smb_node_start_crit()
9975772Sas200622  *
9985772Sas200622  * Enter critical region for share reservations.
9995772Sas200622  * See comments above smb_fsop_shrlock().
10005772Sas200622  */
10015772Sas200622 
10025772Sas200622 void
10035772Sas200622 smb_node_start_crit(smb_node_t *node, krw_t mode)
10045772Sas200622 {
10055772Sas200622 	rw_enter(&node->n_share_lock, mode);
10065772Sas200622 	nbl_start_crit(node->vp, mode);
10075772Sas200622 }
10085772Sas200622 
10095772Sas200622 /*
10105772Sas200622  * smb_node_end_crit()
10115772Sas200622  *
10125772Sas200622  * Exit critical region for share reservations.
10135772Sas200622  */
10145772Sas200622 
10155772Sas200622 void
10165772Sas200622 smb_node_end_crit(smb_node_t *node)
10175772Sas200622 {
10185772Sas200622 	nbl_end_crit(node->vp);
10195772Sas200622 	rw_exit(&node->n_share_lock);
10205772Sas200622 }
10215772Sas200622 
10225772Sas200622 int
10235772Sas200622 smb_node_in_crit(smb_node_t *node)
10245772Sas200622 {
10255772Sas200622 	return (nbl_in_crit(node->vp) && RW_LOCK_HELD(&node->n_share_lock));
10265772Sas200622 }
1027