xref: /onnv-gate/usr/src/uts/common/fs/smbsrv/smb_node.c (revision 10001:d540bbbe2461)
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  * SMB Node State Machine
275331Samw  * ----------------------
285331Samw  *
299021Samw@Sun.COM  *
309021Samw@Sun.COM  *		    +----------- Creation/Allocation
315331Samw  *		    |
329021Samw@Sun.COM  *		    | T0
335331Samw  *		    |
345331Samw  *		    v
359021Samw@Sun.COM  *    +----------------------------+        T1
369021Samw@Sun.COM  *    |  SMB_NODE_STATE_AVAILABLE  |--------------------+
379021Samw@Sun.COM  *    +----------------------------+			|
389021Samw@Sun.COM  *		    |	     ^				|
399021Samw@Sun.COM  *		    |	     |				v
409021Samw@Sun.COM  *		    |	     |	  T2	+-------------------------------+
419021Samw@Sun.COM  *		    |	     |<---------| SMB_NODE_STATE_OPLOCK_GRANTED |
429021Samw@Sun.COM  *		    |	     |		+-------------------------------+
439021Samw@Sun.COM  *		    | T5     |				|
449021Samw@Sun.COM  *		    |	     |				| T3
459021Samw@Sun.COM  *		    |	     |				v
469021Samw@Sun.COM  *		    |	     |	  T4	+--------------------------------+
479021Samw@Sun.COM  *		    |	     +----------| SMB_NODE_STATE_OPLOCK_BREAKING |
489021Samw@Sun.COM  *		    |			+--------------------------------+
499021Samw@Sun.COM  *		    |
509021Samw@Sun.COM  *		    v
515331Samw  *    +-----------------------------+
529021Samw@Sun.COM  *    |  SMB_NODE_STATE_DESTROYING  |
539021Samw@Sun.COM  *    +-----------------------------+
549021Samw@Sun.COM  *		    |
559021Samw@Sun.COM  *		    |
569021Samw@Sun.COM  *		    | T6
579021Samw@Sun.COM  *		    |
589021Samw@Sun.COM  *		    +----------> Deletion/Free
595331Samw  *
605331Samw  * Transition T0
615331Samw  *
625331Samw  *    This transition occurs in smb_node_lookup(). If the node looked for is
635331Samw  *    not found in the has table a new node is created. The reference count is
645331Samw  *    initialized to 1 and the state initialized to SMB_NODE_STATE_AVAILABLE.
655331Samw  *
665331Samw  * Transition T1
675331Samw  *
689021Samw@Sun.COM  *    This transition occurs smb_oplock_acquire() during an OPEN.
699021Samw@Sun.COM  *
709021Samw@Sun.COM  * Transition T2
719021Samw@Sun.COM  *
729021Samw@Sun.COM  *    This transition occurs in smb_oplock_release(). The events triggering
739021Samw@Sun.COM  *    it are:
749021Samw@Sun.COM  *
759021Samw@Sun.COM  *	- LockingAndX sent by the client that was granted the oplock.
769021Samw@Sun.COM  *	- Closing of the file.
779021Samw@Sun.COM  *
789021Samw@Sun.COM  * Transition T3
799021Samw@Sun.COM  *
809021Samw@Sun.COM  *    This transition occurs in smb_oplock_break(). The events triggering
819021Samw@Sun.COM  *    it are:
829021Samw@Sun.COM  *
839021Samw@Sun.COM  *	- Another client wants to open the file.
849021Samw@Sun.COM  *	- A client is trying to delete the file.
859021Samw@Sun.COM  *	- A client is trying to rename the file.
869021Samw@Sun.COM  *	- A client is trying to set/modify  the file attributes.
879021Samw@Sun.COM  *
889021Samw@Sun.COM  * Transition T4
899021Samw@Sun.COM  *
909021Samw@Sun.COM  *    This transition occurs in smb_oplock_release or smb_oplock_break(). The
919021Samw@Sun.COM  *    events triggering it are:
929021Samw@Sun.COM  *
939021Samw@Sun.COM  *	- The client that was granting the oplock releases it (close or
949021Samw@Sun.COM  *	  LockingAndx).
959021Samw@Sun.COM  *	- The time alloted to release the oplock expired.
969021Samw@Sun.COM  *
979021Samw@Sun.COM  * Transition T5
989021Samw@Sun.COM  *
995331Samw  *    This transition occurs in smb_node_release(). If the reference count
1005331Samw  *    drops to zero the state is moved to SMB_NODE_STATE_DESTROYING and no more
1015331Samw  *    reference count will be given out for that node.
1025331Samw  *
1039021Samw@Sun.COM  * Transition T6
1045331Samw  *
1055331Samw  *    This transition occurs in smb_node_release(). The structure is deleted.
1065331Samw  *
1075331Samw  * Comments
1085331Samw  * --------
1095331Samw  *
1105331Samw  *    The reason the smb node has 2 states is the following synchronization
1115331Samw  *    rule:
1125331Samw  *
1135331Samw  *    There's a mutex embedded in the node used to protect its fields and
1145331Samw  *    there's a lock embedded in the bucket of the hash table the node belongs
1155331Samw  *    to. To increment or to decrement the reference count the mutex must be
1165331Samw  *    entered. To insert the node into the bucket and to remove it from the
1175331Samw  *    bucket the lock must be entered in RW_WRITER mode. When both (mutex and
1185331Samw  *    lock) have to be entered, the lock has always to be entered first then
1195331Samw  *    the mutex. This prevents a deadlock between smb_node_lookup() and
1205331Samw  *    smb_node_release() from occurring. However, in smb_node_release() when the
1215331Samw  *    reference count drops to zero and triggers the deletion of the node, the
1225331Samw  *    mutex has to be released before entering the lock of the bucket (to
1235331Samw  *    remove the node). This creates a window during which the node that is
1245331Samw  *    about to be freed could be given out by smb_node_lookup(). To close that
1255331Samw  *    window the node is moved to the state SMB_NODE_STATE_DESTROYING before
1265331Samw  *    releasing the mutex. That way, even if smb_node_lookup() finds it, the
1275331Samw  *    state will indicate that the node should be treated as non existent (of
1285331Samw  *    course the state of the node should be tested/updated under the
1295331Samw  *    protection of the mutex).
1305331Samw  */
1315331Samw #include <smbsrv/smb_incl.h>
1325331Samw #include <smbsrv/smb_fsops.h>
1338934SJose.Borrego@Sun.COM #include <smbsrv/smb_kstat.h>
1345331Samw #include <sys/pathname.h>
1355331Samw #include <sys/sdt.h>
1365772Sas200622 #include <sys/nbmlock.h>
1375331Samw 
1388934SJose.Borrego@Sun.COM uint32_t smb_is_executable(char *);
1398934SJose.Borrego@Sun.COM static void smb_node_delete_on_close(smb_node_t *);
1408934SJose.Borrego@Sun.COM static void smb_node_create_audit_buf(smb_node_t *, int);
1418934SJose.Borrego@Sun.COM static void smb_node_destroy_audit_buf(smb_node_t *);
1428934SJose.Borrego@Sun.COM static void smb_node_audit(smb_node_t *);
143*10001SJoyce.McIntosh@Sun.COM static smb_node_t *smb_node_alloc(char *, vnode_t *, smb_llist_t *, uint32_t);
1448934SJose.Borrego@Sun.COM static void smb_node_free(smb_node_t *);
1458934SJose.Borrego@Sun.COM static int smb_node_constructor(void *, void *, int);
1468934SJose.Borrego@Sun.COM static void smb_node_destructor(void *, void *);
1478934SJose.Borrego@Sun.COM static smb_llist_t *smb_node_get_hash(fsid_t *, smb_attr_t *, uint32_t *);
148*10001SJoyce.McIntosh@Sun.COM static void smb_node_init_cached_timestamps(smb_node_t *);
149*10001SJoyce.McIntosh@Sun.COM static void smb_node_clear_cached_timestamps(smb_node_t *);
150*10001SJoyce.McIntosh@Sun.COM static void smb_node_get_cached_timestamps(smb_node_t *, smb_attr_t *);
151*10001SJoyce.McIntosh@Sun.COM static void smb_node_set_cached_timestamps(smb_node_t *, smb_attr_t *);
1525331Samw 
1535331Samw #define	VALIDATE_DIR_NODE(_dir_, _node_) \
1545331Samw     ASSERT((_dir_)->n_magic == SMB_NODE_MAGIC); \
1555331Samw     ASSERT(((_dir_)->vp->v_xattrdir) || ((_dir_)->vp->v_type == VDIR)); \
1565331Samw     ASSERT((_dir_)->dir_snode != (_node_));
1575331Samw 
1588934SJose.Borrego@Sun.COM static kmem_cache_t	*smb_node_cache = NULL;
1596139Sjb150015 static boolean_t	smb_node_initialized = B_FALSE;
1606139Sjb150015 static smb_llist_t	smb_node_hash_table[SMBND_HASH_MASK+1];
1616139Sjb150015 
1626139Sjb150015 /*
1636139Sjb150015  * smb_node_init
1646139Sjb150015  *
1656139Sjb150015  * Initialization of the SMB node layer.
1666139Sjb150015  *
1676139Sjb150015  * This function is not multi-thread safe. The caller must make sure only one
1686139Sjb150015  * thread makes the call.
1696139Sjb150015  */
1706139Sjb150015 int
1716139Sjb150015 smb_node_init(void)
1726139Sjb150015 {
1736139Sjb150015 	int	i;
1746139Sjb150015 
1756139Sjb150015 	if (smb_node_initialized)
1766139Sjb150015 		return (0);
1778934SJose.Borrego@Sun.COM 	smb_node_cache = kmem_cache_create(SMBSRV_KSTAT_NODE_CACHE,
1788934SJose.Borrego@Sun.COM 	    sizeof (smb_node_t), 8, smb_node_constructor, smb_node_destructor,
1798934SJose.Borrego@Sun.COM 	    NULL, NULL, NULL, 0);
1806139Sjb150015 
1816139Sjb150015 	for (i = 0; i <= SMBND_HASH_MASK; i++) {
1826139Sjb150015 		smb_llist_constructor(&smb_node_hash_table[i],
1836139Sjb150015 		    sizeof (smb_node_t), offsetof(smb_node_t, n_lnd));
1846139Sjb150015 	}
1856139Sjb150015 	smb_node_initialized = B_TRUE;
1866139Sjb150015 	return (0);
1876139Sjb150015 }
1886139Sjb150015 
1896139Sjb150015 /*
1906139Sjb150015  * smb_node_fini
1916139Sjb150015  *
1926139Sjb150015  * This function is not multi-thread safe. The caller must make sure only one
1936139Sjb150015  * thread makes the call.
1946139Sjb150015  */
1956139Sjb150015 void
1966139Sjb150015 smb_node_fini(void)
1976139Sjb150015 {
1986139Sjb150015 	int	i;
1996139Sjb150015 
2006139Sjb150015 	if (!smb_node_initialized)
2016139Sjb150015 		return;
2026139Sjb150015 
2036139Sjb150015 #ifdef DEBUG
2046139Sjb150015 	for (i = 0; i <= SMBND_HASH_MASK; i++) {
2056139Sjb150015 		smb_node_t	*node;
2066139Sjb150015 
2076139Sjb150015 		/*
2086139Sjb150015 		 * The following sequence is just intended for sanity check.
2096139Sjb150015 		 * This will have to be modified when the code goes into
2106139Sjb150015 		 * production.
2116139Sjb150015 		 *
2126139Sjb150015 		 * The SMB node hash table should be emtpy at this point. If the
2136139Sjb150015 		 * hash table is not empty a panic will be triggered.
2146139Sjb150015 		 *
2156139Sjb150015 		 * The reason why SMB nodes are still remaining in the hash
2166139Sjb150015 		 * table is problably due to a mismatch between calls to
2176139Sjb150015 		 * smb_node_lookup() and smb_node_release(). You must track that
2186139Sjb150015 		 * down.
2196139Sjb150015 		 */
2206139Sjb150015 		node = smb_llist_head(&smb_node_hash_table[i]);
2216139Sjb150015 		ASSERT(node == NULL);
2226139Sjb150015 	}
2236139Sjb150015 #endif
2246139Sjb150015 
2256139Sjb150015 	for (i = 0; i <= SMBND_HASH_MASK; i++) {
2266139Sjb150015 		smb_llist_destructor(&smb_node_hash_table[i]);
2276139Sjb150015 	}
2288934SJose.Borrego@Sun.COM 	kmem_cache_destroy(smb_node_cache);
2298934SJose.Borrego@Sun.COM 	smb_node_cache = NULL;
2306139Sjb150015 	smb_node_initialized = B_FALSE;
2316139Sjb150015 }
2326139Sjb150015 
2335331Samw /*
2345331Samw  * smb_node_lookup()
2355331Samw  *
2365331Samw  * NOTE: This routine should only be called by the file system interface layer,
2375331Samw  * and not by SMB.
2385331Samw  *
2395331Samw  * smb_node_lookup() is called upon successful lookup, mkdir, and create
2405331Samw  * (for both non-streams and streams).  In each of these cases, a held vnode is
2418670SJose.Borrego@Sun.COM  * passed into this routine.  If a new smb_node is created it will take its
2428670SJose.Borrego@Sun.COM  * own hold on the vnode.  The caller's hold therefore still belongs to, and
2438670SJose.Borrego@Sun.COM  * should be released by, the caller.
2445331Samw  *
2455331Samw  * A reference is taken on the smb_node whether found in the hash table
2465331Samw  * or newly created.
2475331Samw  *
2485331Samw  * If an smb_node needs to be created, a reference is also taken on the
2495331Samw  * dir_snode (if passed in).
2505331Samw  *
2515331Samw  * See smb_node_release() for details on the release of these references.
2525331Samw  */
2535331Samw 
2545331Samw /*ARGSUSED*/
2555331Samw smb_node_t *
2565331Samw smb_node_lookup(
2575331Samw     struct smb_request	*sr,
2585331Samw     struct open_param	*op,
2595331Samw     cred_t		*cred,
2605331Samw     vnode_t		*vp,
2615331Samw     char		*od_name,
2625331Samw     smb_node_t		*dir_snode,
263*10001SJoyce.McIntosh@Sun.COM     smb_node_t		*unnamed_node)
2645331Samw {
2655331Samw 	smb_llist_t		*node_hdr;
2665331Samw 	smb_node_t		*node;
267*10001SJoyce.McIntosh@Sun.COM 	smb_attr_t		attr;
2685331Samw 	uint32_t		hashkey = 0;
2697348SJose.Borrego@Sun.COM 	fsid_t			fsid;
2705331Samw 	int			error;
2715331Samw 	krw_t			lock_mode;
2725331Samw 	vnode_t			*unnamed_vp = NULL;
2735331Samw 
2745331Samw 	/*
2755331Samw 	 * smb_vop_getattr() is called here instead of smb_fsop_getattr(),
2765331Samw 	 * because the node may not yet exist.  We also do not want to call
2775331Samw 	 * it with the list lock held.
2785331Samw 	 */
2795331Samw 
2805331Samw 	if (unnamed_node)
2815331Samw 		unnamed_vp = unnamed_node->vp;
2825331Samw 
2835331Samw 	/*
2845331Samw 	 * This getattr is performed on behalf of the server
2855331Samw 	 * that's why kcred is used not the user's cred
2865331Samw 	 */
287*10001SJoyce.McIntosh@Sun.COM 	attr.sa_mask = SMB_AT_ALL;
288*10001SJoyce.McIntosh@Sun.COM 	error = smb_vop_getattr(vp, unnamed_vp, &attr, 0, kcred);
2895331Samw 	if (error)
2905331Samw 		return (NULL);
2915331Samw 
2927348SJose.Borrego@Sun.COM 	if (sr && sr->tid_tree) {
2937348SJose.Borrego@Sun.COM 		/*
2947348SJose.Borrego@Sun.COM 		 * The fsid for a file is that of the tree, even
2957348SJose.Borrego@Sun.COM 		 * if the file resides in a different mountpoint
2967348SJose.Borrego@Sun.COM 		 * under the share.
2977348SJose.Borrego@Sun.COM 		 */
2987348SJose.Borrego@Sun.COM 		fsid = SMB_TREE_FSID(sr->tid_tree);
2995331Samw 	} else {
3007348SJose.Borrego@Sun.COM 		/*
3017348SJose.Borrego@Sun.COM 		 * This should be getting executed only for the
3027348SJose.Borrego@Sun.COM 		 * tree root smb_node.
3037348SJose.Borrego@Sun.COM 		 */
3047348SJose.Borrego@Sun.COM 		fsid = vp->v_vfsp->vfs_fsid;
3055331Samw 	}
3065331Samw 
307*10001SJoyce.McIntosh@Sun.COM 	node_hdr = smb_node_get_hash(&fsid, &attr, &hashkey);
3085331Samw 	lock_mode = RW_READER;
3095331Samw 
3105331Samw 	smb_llist_enter(node_hdr, lock_mode);
3115331Samw 	for (;;) {
3125331Samw 		node = list_head(&node_hdr->ll_list);
3135331Samw 		while (node) {
3145331Samw 			ASSERT(node->n_magic == SMB_NODE_MAGIC);
3155331Samw 			ASSERT(node->n_hash_bucket == node_hdr);
3165331Samw 			if ((node->n_hashkey == hashkey) && (node->vp == vp)) {
3178934SJose.Borrego@Sun.COM 				mutex_enter(&node->n_mutex);
3185331Samw 				DTRACE_PROBE1(smb_node_lookup_hit,
3195331Samw 				    smb_node_t *, node);
3205331Samw 				switch (node->n_state) {
3218934SJose.Borrego@Sun.COM 				case SMB_NODE_STATE_OPLOCK_GRANTED:
3228934SJose.Borrego@Sun.COM 				case SMB_NODE_STATE_OPLOCK_BREAKING:
3235331Samw 				case SMB_NODE_STATE_AVAILABLE:
3245331Samw 					/* The node was found. */
3255331Samw 					node->n_refcnt++;
3265331Samw 					if ((node->dir_snode == NULL) &&
3275331Samw 					    (dir_snode != NULL) &&
3285331Samw 					    (strcmp(od_name, "..") != 0) &&
3295331Samw 					    (strcmp(od_name, ".") != 0)) {
3305331Samw 						VALIDATE_DIR_NODE(dir_snode,
3315331Samw 						    node);
3325331Samw 						node->dir_snode = dir_snode;
3335331Samw 						smb_node_ref(dir_snode);
3345331Samw 					}
3355331Samw 
3368934SJose.Borrego@Sun.COM 					smb_node_audit(node);
3378934SJose.Borrego@Sun.COM 					mutex_exit(&node->n_mutex);
3385331Samw 					smb_llist_exit(node_hdr);
3395331Samw 					return (node);
3405331Samw 
3415331Samw 				case SMB_NODE_STATE_DESTROYING:
3425331Samw 					/*
3435331Samw 					 * Although the node exists it is about
3445331Samw 					 * to be destroyed. We act as it hasn't
3455331Samw 					 * been found.
3465331Samw 					 */
3478934SJose.Borrego@Sun.COM 					mutex_exit(&node->n_mutex);
3485331Samw 					break;
3495331Samw 				default:
3505331Samw 					/*
3515331Samw 					 * Although the node exists it is in an
3525331Samw 					 * unknown state. We act as it hasn't
3535331Samw 					 * been found.
3545331Samw 					 */
3555331Samw 					ASSERT(0);
3568934SJose.Borrego@Sun.COM 					mutex_exit(&node->n_mutex);
3575331Samw 					break;
3585331Samw 				}
3595331Samw 			}
3605331Samw 			node = smb_llist_next(node_hdr, node);
3615331Samw 		}
3625331Samw 		if ((lock_mode == RW_READER) && smb_llist_upgrade(node_hdr)) {
3635331Samw 			lock_mode = RW_WRITER;
3645331Samw 			continue;
3655331Samw 		}
3665331Samw 		break;
3675331Samw 	}
368*10001SJoyce.McIntosh@Sun.COM 	node = smb_node_alloc(od_name, vp, node_hdr, hashkey);
3698934SJose.Borrego@Sun.COM 	node->n_orig_uid = crgetuid(sr->user_cr);
3705331Samw 
3715331Samw 	if (op)
3729343SAfshin.Ardakani@Sun.COM 		node->flags |= smb_is_executable(op->fqi.fq_last_comp);
3735331Samw 
3745331Samw 	if (dir_snode) {
3755331Samw 		smb_node_ref(dir_snode);
3765331Samw 		node->dir_snode = dir_snode;
3775331Samw 		ASSERT(dir_snode->dir_snode != node);
3785331Samw 		ASSERT((dir_snode->vp->v_xattrdir) ||
3795331Samw 		    (dir_snode->vp->v_type == VDIR));
3805331Samw 	}
3815331Samw 
3825331Samw 	if (unnamed_node) {
3835331Samw 		smb_node_ref(unnamed_node);
3845331Samw 		node->unnamed_stream_node = unnamed_node;
3855331Samw 	}
3865331Samw 
3875331Samw 	DTRACE_PROBE1(smb_node_lookup_miss, smb_node_t *, node);
3888934SJose.Borrego@Sun.COM 	smb_node_audit(node);
3895331Samw 	smb_llist_insert_head(node_hdr, node);
3905331Samw 	smb_llist_exit(node_hdr);
3915331Samw 	return (node);
3925331Samw }
3935331Samw 
3945331Samw /*
3955331Samw  * smb_stream_node_lookup()
3965331Samw  *
3975331Samw  * Note: stream_name (the name that will be stored in the "od_name" field
3985331Samw  * of a stream's smb_node) is the same as the on-disk name for the stream
3995331Samw  * except that it does not have SMB_STREAM_PREFIX prepended.
4005331Samw  */
4015331Samw 
4025331Samw smb_node_t *
4038934SJose.Borrego@Sun.COM smb_stream_node_lookup(smb_request_t *sr, cred_t *cr, smb_node_t *fnode,
404*10001SJoyce.McIntosh@Sun.COM     vnode_t *xattrdirvp, vnode_t *vp, char *stream_name)
4055331Samw {
4065331Samw 	smb_node_t	*xattrdir_node;
4075331Samw 	smb_node_t	*snode;
4085331Samw 
4095331Samw 	xattrdir_node = smb_node_lookup(sr, NULL, cr, xattrdirvp, XATTR_DIR,
410*10001SJoyce.McIntosh@Sun.COM 	    fnode, NULL);
4115331Samw 
4125331Samw 	if (xattrdir_node == NULL)
4135331Samw 		return (NULL);
4145331Samw 
4155331Samw 	snode = smb_node_lookup(sr, NULL, cr, vp, stream_name, xattrdir_node,
416*10001SJoyce.McIntosh@Sun.COM 	    fnode);
4175331Samw 
4185331Samw 	(void) smb_node_release(xattrdir_node);
4195331Samw 	return (snode);
4205331Samw }
4215331Samw 
4225331Samw 
4235331Samw /*
4245331Samw  * This function should be called whenever a reference is needed on an
4255331Samw  * smb_node pointer.  The copy of an smb_node pointer from one non-local
4265331Samw  * data structure to another requires a reference to be taken on the smb_node
4275331Samw  * (unless the usage is localized).  Each data structure deallocation routine
4285331Samw  * will call smb_node_release() on its smb_node pointers.
4295331Samw  *
4305331Samw  * In general, an smb_node pointer residing in a structure should never be
4315331Samw  * stale.  A node pointer may be NULL, however, and care should be taken
4325331Samw  * prior to calling smb_node_ref(), which ASSERTs that the pointer is valid.
4335331Samw  * Care also needs to be taken with respect to racing deallocations of a
4345331Samw  * structure.
4355331Samw  */
4365331Samw void
4375331Samw smb_node_ref(smb_node_t *node)
4385331Samw {
4398934SJose.Borrego@Sun.COM 	SMB_NODE_VALID(node);
4405331Samw 
4418934SJose.Borrego@Sun.COM 	mutex_enter(&node->n_mutex);
4428934SJose.Borrego@Sun.COM 	switch (node->n_state) {
4438934SJose.Borrego@Sun.COM 	case SMB_NODE_STATE_AVAILABLE:
4448934SJose.Borrego@Sun.COM 	case SMB_NODE_STATE_OPLOCK_GRANTED:
4458934SJose.Borrego@Sun.COM 	case SMB_NODE_STATE_OPLOCK_BREAKING:
4468934SJose.Borrego@Sun.COM 		node->n_refcnt++;
4478934SJose.Borrego@Sun.COM 		ASSERT(node->n_refcnt);
4488934SJose.Borrego@Sun.COM 		DTRACE_PROBE1(smb_node_ref_exit, smb_node_t *, node);
4498934SJose.Borrego@Sun.COM 		smb_node_audit(node);
4508934SJose.Borrego@Sun.COM 		break;
4518934SJose.Borrego@Sun.COM 	default:
4528934SJose.Borrego@Sun.COM 		SMB_PANIC();
4538934SJose.Borrego@Sun.COM 	}
4548934SJose.Borrego@Sun.COM 	mutex_exit(&node->n_mutex);
4555331Samw }
4565331Samw 
4575331Samw /*
4585331Samw  * smb_node_lookup() takes a hold on an smb_node, whether found in the
4595331Samw  * hash table or newly created.  This hold is expected to be released
4605331Samw  * in the following manner.
4615331Samw  *
4625331Samw  * smb_node_lookup() takes an address of an smb_node pointer.  This should
4635331Samw  * be getting passed down via a lookup (whether path name or component), mkdir,
4645331Samw  * create.  If the original smb_node pointer resides in a data structure, then
4655331Samw  * the deallocation routine for the data structure is responsible for calling
4665331Samw  * smb_node_release() on the smb_node pointer.  Alternatively,
4675331Samw  * smb_node_release() can be called as soon as the smb_node pointer is no longer
4685331Samw  * needed.  In this case, callers are responsible for setting an embedded
4695331Samw  * pointer to NULL if it is known that the last reference is being released.
4705331Samw  *
4715331Samw  * If the passed-in address of the smb_node pointer belongs to a local variable,
4725331Samw  * then the caller with the local variable should call smb_node_release()
4735331Samw  * directly.
4745331Samw  *
4755331Samw  * smb_node_release() itself will call smb_node_release() on a node's dir_snode,
4765331Samw  * as smb_node_lookup() takes a hold on dir_snode.
4775331Samw  */
4785331Samw void
4795331Samw smb_node_release(smb_node_t *node)
4805331Samw {
4818934SJose.Borrego@Sun.COM 	SMB_NODE_VALID(node);
4825331Samw 
4838934SJose.Borrego@Sun.COM 	mutex_enter(&node->n_mutex);
4845331Samw 	ASSERT(node->n_refcnt);
4855331Samw 	DTRACE_PROBE1(smb_node_release, smb_node_t *, node);
4865331Samw 	if (--node->n_refcnt == 0) {
4875331Samw 		switch (node->n_state) {
4885331Samw 
4895331Samw 		case SMB_NODE_STATE_AVAILABLE:
4905331Samw 			node->n_state = SMB_NODE_STATE_DESTROYING;
4918934SJose.Borrego@Sun.COM 			mutex_exit(&node->n_mutex);
4925331Samw 
4935331Samw 			smb_llist_enter(node->n_hash_bucket, RW_WRITER);
4945331Samw 			smb_llist_remove(node->n_hash_bucket, node);
4955331Samw 			smb_llist_exit(node->n_hash_bucket);
4965331Samw 
4975331Samw 			/*
4985331Samw 			 * Check if the file was deleted
4995331Samw 			 */
5005331Samw 			smb_node_delete_on_close(node);
5015331Samw 
5025331Samw 			if (node->dir_snode) {
5035331Samw 				ASSERT(node->dir_snode->n_magic ==
5045331Samw 				    SMB_NODE_MAGIC);
5055331Samw 				smb_node_release(node->dir_snode);
5065331Samw 			}
5075331Samw 
5085331Samw 			if (node->unnamed_stream_node) {
5095331Samw 				ASSERT(node->unnamed_stream_node->n_magic ==
5105331Samw 				    SMB_NODE_MAGIC);
5115331Samw 				smb_node_release(node->unnamed_stream_node);
5125331Samw 			}
5135331Samw 
5148934SJose.Borrego@Sun.COM 			smb_node_free(node);
5155331Samw 			return;
5165331Samw 
5175331Samw 		default:
5188934SJose.Borrego@Sun.COM 			SMB_PANIC();
5195331Samw 		}
5205331Samw 	}
5218934SJose.Borrego@Sun.COM 	smb_node_audit(node);
5228934SJose.Borrego@Sun.COM 	mutex_exit(&node->n_mutex);
5235331Samw }
5245331Samw 
5255331Samw static void
5265331Samw smb_node_delete_on_close(smb_node_t *node)
5275331Samw {
5285331Samw 	smb_node_t	*d_snode;
5295331Samw 	int		rc = 0;
5309231SAfshin.Ardakani@Sun.COM 	uint32_t	flags = 0;
5315331Samw 
5325331Samw 	d_snode = node->dir_snode;
5335331Samw 	if (node->flags & NODE_FLAGS_DELETE_ON_CLOSE) {
5349231SAfshin.Ardakani@Sun.COM 		node->flags &= ~NODE_FLAGS_DELETE_ON_CLOSE;
5359231SAfshin.Ardakani@Sun.COM 		flags = node->n_delete_on_close_flags;
5369231SAfshin.Ardakani@Sun.COM 		ASSERT(node->od_name != NULL);
5375331Samw 
538*10001SJoyce.McIntosh@Sun.COM 		if (node->vp->v_type == VDIR)
5395331Samw 			rc = smb_fsop_rmdir(0, node->delete_on_close_cred,
5409231SAfshin.Ardakani@Sun.COM 			    d_snode, node->od_name, flags);
5415331Samw 		else
5425331Samw 			rc = smb_fsop_remove(0, node->delete_on_close_cred,
5439231SAfshin.Ardakani@Sun.COM 			    d_snode, node->od_name, flags);
5445331Samw 		smb_cred_rele(node->delete_on_close_cred);
5455331Samw 	}
5465331Samw 	if (rc != 0)
5475331Samw 		cmn_err(CE_WARN, "File %s could not be removed, rc=%d\n",
5485331Samw 		    node->od_name, rc);
5495331Samw 	DTRACE_PROBE2(smb_node_delete_on_close, int, rc, smb_node_t *, node);
5505331Samw }
5515331Samw 
5525331Samw /*
5535331Samw  * smb_node_rename()
5545331Samw  *
5555331Samw  */
5568934SJose.Borrego@Sun.COM void
5575331Samw smb_node_rename(
5588934SJose.Borrego@Sun.COM     smb_node_t	*from_dnode,
5598934SJose.Borrego@Sun.COM     smb_node_t	*ret_node,
5608934SJose.Borrego@Sun.COM     smb_node_t	*to_dnode,
5615331Samw     char	*to_name)
5625331Samw {
5638934SJose.Borrego@Sun.COM 	SMB_NODE_VALID(from_dnode);
5648934SJose.Borrego@Sun.COM 	SMB_NODE_VALID(to_dnode);
5658934SJose.Borrego@Sun.COM 	SMB_NODE_VALID(ret_node);
5665331Samw 
5678934SJose.Borrego@Sun.COM 	smb_node_ref(to_dnode);
5688934SJose.Borrego@Sun.COM 	mutex_enter(&ret_node->n_mutex);
5698934SJose.Borrego@Sun.COM 	switch (ret_node->n_state) {
5708934SJose.Borrego@Sun.COM 	case SMB_NODE_STATE_AVAILABLE:
5718934SJose.Borrego@Sun.COM 	case SMB_NODE_STATE_OPLOCK_GRANTED:
5728934SJose.Borrego@Sun.COM 	case SMB_NODE_STATE_OPLOCK_BREAKING:
5738934SJose.Borrego@Sun.COM 		ret_node->dir_snode = to_dnode;
5748934SJose.Borrego@Sun.COM 		mutex_exit(&ret_node->n_mutex);
5758934SJose.Borrego@Sun.COM 		ASSERT(to_dnode->dir_snode != ret_node);
5768934SJose.Borrego@Sun.COM 		ASSERT((to_dnode->vp->v_xattrdir) ||
5778934SJose.Borrego@Sun.COM 		    (to_dnode->vp->v_type == VDIR));
5788934SJose.Borrego@Sun.COM 		smb_node_release(from_dnode);
5798934SJose.Borrego@Sun.COM 		(void) strcpy(ret_node->od_name, to_name);
5808934SJose.Borrego@Sun.COM 		/*
5818934SJose.Borrego@Sun.COM 		 * XXX Need to update attributes?
5828934SJose.Borrego@Sun.COM 		 */
5838934SJose.Borrego@Sun.COM 		break;
5848934SJose.Borrego@Sun.COM 	default:
5858934SJose.Borrego@Sun.COM 		SMB_PANIC();
5868934SJose.Borrego@Sun.COM 	}
5875331Samw }
5885331Samw 
5895331Samw int
5906139Sjb150015 smb_node_root_init(vnode_t *vp, smb_server_t *sv, smb_node_t **root)
5915331Samw {
592*10001SJoyce.McIntosh@Sun.COM 	smb_attr_t	attr;
5936139Sjb150015 	int		error;
5946139Sjb150015 	uint32_t	hashkey;
5956139Sjb150015 	smb_llist_t	*node_hdr;
5966139Sjb150015 	smb_node_t	*node;
5975331Samw 
598*10001SJoyce.McIntosh@Sun.COM 	attr.sa_mask = SMB_AT_ALL;
599*10001SJoyce.McIntosh@Sun.COM 	error = smb_vop_getattr(vp, NULL, &attr, 0, kcred);
6006139Sjb150015 	if (error) {
6016139Sjb150015 		VN_RELE(vp);
6026139Sjb150015 		return (error);
6036139Sjb150015 	}
6046139Sjb150015 
605*10001SJoyce.McIntosh@Sun.COM 	node_hdr = smb_node_get_hash(&vp->v_vfsp->vfs_fsid, &attr, &hashkey);
6065331Samw 
607*10001SJoyce.McIntosh@Sun.COM 	node = smb_node_alloc(ROOTVOL, vp, node_hdr, hashkey);
6086139Sjb150015 
6096139Sjb150015 	sv->si_root_smb_node = node;
6108934SJose.Borrego@Sun.COM 	smb_node_audit(node);
6116139Sjb150015 	smb_llist_enter(node_hdr, RW_WRITER);
6126139Sjb150015 	smb_llist_insert_head(node_hdr, node);
6136139Sjb150015 	smb_llist_exit(node_hdr);
6146139Sjb150015 	*root = node;
6156139Sjb150015 	return (0);
6165331Samw }
6175331Samw 
6185331Samw /*
6199231SAfshin.Ardakani@Sun.COM  * When DeleteOnClose is set on an smb_node, the common open code will
6209231SAfshin.Ardakani@Sun.COM  * reject subsequent open requests for the file. Observation of Windows
6219231SAfshin.Ardakani@Sun.COM  * 2000 indicates that subsequent opens should be allowed (assuming
6229231SAfshin.Ardakani@Sun.COM  * there would be no sharing violation) until the file is closed using
6239231SAfshin.Ardakani@Sun.COM  * the fid on which the DeleteOnClose was requested.
6249231SAfshin.Ardakani@Sun.COM  *
6259231SAfshin.Ardakani@Sun.COM  * If there are multiple opens with delete-on-close create options,
6269231SAfshin.Ardakani@Sun.COM  * whichever the first file handle is closed will trigger the node to be
6279231SAfshin.Ardakani@Sun.COM  * marked as delete-on-close. The credentials of that ofile will be used
6289231SAfshin.Ardakani@Sun.COM  * as the delete-on-close credentials of the node.
6299231SAfshin.Ardakani@Sun.COM  */
6305331Samw int
6319231SAfshin.Ardakani@Sun.COM smb_node_set_delete_on_close(smb_node_t *node, cred_t *cr, uint32_t flags)
6325331Samw {
633*10001SJoyce.McIntosh@Sun.COM 	int rc;
634*10001SJoyce.McIntosh@Sun.COM 	smb_attr_t attr;
6355331Samw 
6368934SJose.Borrego@Sun.COM 	mutex_enter(&node->n_mutex);
637*10001SJoyce.McIntosh@Sun.COM 
638*10001SJoyce.McIntosh@Sun.COM 	if ((node->flags & NODE_FLAGS_DELETE_ON_CLOSE) ||
639*10001SJoyce.McIntosh@Sun.COM 	    (node->readonly_creator)) {
640*10001SJoyce.McIntosh@Sun.COM 		mutex_exit(&node->n_mutex);
641*10001SJoyce.McIntosh@Sun.COM 		return (-1);
6425331Samw 	}
643*10001SJoyce.McIntosh@Sun.COM 
644*10001SJoyce.McIntosh@Sun.COM 	bzero(&attr, sizeof (smb_attr_t));
645*10001SJoyce.McIntosh@Sun.COM 	attr.sa_mask = SMB_AT_DOSATTR;
646*10001SJoyce.McIntosh@Sun.COM 	rc = smb_fsop_getattr(NULL, kcred, node, &attr);
647*10001SJoyce.McIntosh@Sun.COM 	if ((rc != 0) || (attr.sa_dosattr & FILE_ATTRIBUTE_READONLY)) {
648*10001SJoyce.McIntosh@Sun.COM 		mutex_exit(&node->n_mutex);
649*10001SJoyce.McIntosh@Sun.COM 		return (-1);
650*10001SJoyce.McIntosh@Sun.COM 	}
651*10001SJoyce.McIntosh@Sun.COM 
652*10001SJoyce.McIntosh@Sun.COM 	crhold(cr);
653*10001SJoyce.McIntosh@Sun.COM 	node->delete_on_close_cred = cr;
654*10001SJoyce.McIntosh@Sun.COM 	node->n_delete_on_close_flags = flags;
655*10001SJoyce.McIntosh@Sun.COM 	node->flags |= NODE_FLAGS_DELETE_ON_CLOSE;
6568934SJose.Borrego@Sun.COM 	mutex_exit(&node->n_mutex);
657*10001SJoyce.McIntosh@Sun.COM 	return (0);
6585331Samw }
6595331Samw 
6605331Samw void
6615331Samw smb_node_reset_delete_on_close(smb_node_t *node)
6625331Samw {
6638934SJose.Borrego@Sun.COM 	mutex_enter(&node->n_mutex);
6645331Samw 	if (node->flags & NODE_FLAGS_DELETE_ON_CLOSE) {
6655331Samw 		node->flags &= ~NODE_FLAGS_DELETE_ON_CLOSE;
6665331Samw 		crfree(node->delete_on_close_cred);
6675331Samw 		node->delete_on_close_cred = NULL;
6689231SAfshin.Ardakani@Sun.COM 		node->n_delete_on_close_flags = 0;
6695331Samw 	}
6708934SJose.Borrego@Sun.COM 	mutex_exit(&node->n_mutex);
6715331Samw }
6725772Sas200622 
6735772Sas200622 /*
6746771Sjb150015  * smb_node_open_check
6755772Sas200622  *
6765772Sas200622  * check file sharing rules for current open request
6775772Sas200622  * against all existing opens for a file.
6785772Sas200622  *
6795772Sas200622  * Returns NT_STATUS_SHARING_VIOLATION if there is any
6805772Sas200622  * sharing conflict, otherwise returns NT_STATUS_SUCCESS.
6815772Sas200622  */
6825772Sas200622 uint32_t
6838934SJose.Borrego@Sun.COM smb_node_open_check(
6848934SJose.Borrego@Sun.COM     smb_node_t	*node,
6858934SJose.Borrego@Sun.COM     cred_t	*cr,
6868934SJose.Borrego@Sun.COM     uint32_t	desired_access,
6878934SJose.Borrego@Sun.COM     uint32_t	share_access)
6885772Sas200622 {
6895772Sas200622 	smb_ofile_t *of;
6905772Sas200622 	uint32_t status;
6915772Sas200622 
6928934SJose.Borrego@Sun.COM 	SMB_NODE_VALID(node);
6935772Sas200622 
6945772Sas200622 	smb_llist_enter(&node->n_ofile_list, RW_READER);
6955772Sas200622 	of = smb_llist_head(&node->n_ofile_list);
6965772Sas200622 	while (of) {
6976771Sjb150015 		status = smb_ofile_open_check(of, cr, desired_access,
6986771Sjb150015 		    share_access);
6996771Sjb150015 
7006771Sjb150015 		switch (status) {
7016771Sjb150015 		case NT_STATUS_INVALID_HANDLE:
7026771Sjb150015 		case NT_STATUS_SUCCESS:
7036771Sjb150015 			of = smb_llist_next(&node->n_ofile_list, of);
7046771Sjb150015 			break;
7056771Sjb150015 		default:
7066771Sjb150015 			ASSERT(status == NT_STATUS_SHARING_VIOLATION);
7075772Sas200622 			smb_llist_exit(&node->n_ofile_list);
7085772Sas200622 			return (status);
7095772Sas200622 		}
7105772Sas200622 	}
7116771Sjb150015 
7125772Sas200622 	smb_llist_exit(&node->n_ofile_list);
7135772Sas200622 	return (NT_STATUS_SUCCESS);
7145772Sas200622 }
7155772Sas200622 
7165772Sas200622 uint32_t
7178934SJose.Borrego@Sun.COM smb_node_rename_check(smb_node_t *node)
7185772Sas200622 {
7198934SJose.Borrego@Sun.COM 	smb_ofile_t	*of;
7208934SJose.Borrego@Sun.COM 	uint32_t	status;
7215772Sas200622 
7228934SJose.Borrego@Sun.COM 	SMB_NODE_VALID(node);
7235772Sas200622 
7245772Sas200622 	/*
7255772Sas200622 	 * Intra-CIFS check
7265772Sas200622 	 */
7275772Sas200622 	smb_llist_enter(&node->n_ofile_list, RW_READER);
7286771Sjb150015 	of = smb_llist_head(&node->n_ofile_list);
7296771Sjb150015 	while (of) {
7306771Sjb150015 		status = smb_ofile_rename_check(of);
7315772Sas200622 
7326771Sjb150015 		switch (status) {
7336771Sjb150015 		case NT_STATUS_INVALID_HANDLE:
7346771Sjb150015 		case NT_STATUS_SUCCESS:
7356771Sjb150015 			of = smb_llist_next(&node->n_ofile_list, of);
7366771Sjb150015 			break;
7376771Sjb150015 		default:
7386771Sjb150015 			ASSERT(status == NT_STATUS_SHARING_VIOLATION);
7396771Sjb150015 			smb_llist_exit(&node->n_ofile_list);
7406771Sjb150015 			return (status);
7415772Sas200622 		}
7425772Sas200622 	}
7435772Sas200622 	smb_llist_exit(&node->n_ofile_list);
7445772Sas200622 
7455772Sas200622 	/*
7465772Sas200622 	 * system-wide share check
7475772Sas200622 	 */
7485772Sas200622 	if (nbl_share_conflict(node->vp, NBL_RENAME, NULL))
7495772Sas200622 		return (NT_STATUS_SHARING_VIOLATION);
7505772Sas200622 	else
7515772Sas200622 		return (NT_STATUS_SUCCESS);
7525772Sas200622 }
7535772Sas200622 
7546771Sjb150015 uint32_t
7555772Sas200622 smb_node_delete_check(smb_node_t *node)
7565772Sas200622 {
7578934SJose.Borrego@Sun.COM 	smb_ofile_t	*of;
7588934SJose.Borrego@Sun.COM 	uint32_t	status;
7595772Sas200622 
7608934SJose.Borrego@Sun.COM 	SMB_NODE_VALID(node);
7615772Sas200622 
762*10001SJoyce.McIntosh@Sun.COM 	if (node->vp->v_type == VDIR)
7635772Sas200622 		return (NT_STATUS_SUCCESS);
7645772Sas200622 
7655772Sas200622 	/*
7665772Sas200622 	 * intra-CIFS check
7675772Sas200622 	 */
7685772Sas200622 	smb_llist_enter(&node->n_ofile_list, RW_READER);
7696771Sjb150015 	of = smb_llist_head(&node->n_ofile_list);
7706771Sjb150015 	while (of) {
7716771Sjb150015 		status = smb_ofile_delete_check(of);
7726771Sjb150015 
7736771Sjb150015 		switch (status) {
7746771Sjb150015 		case NT_STATUS_INVALID_HANDLE:
7756771Sjb150015 		case NT_STATUS_SUCCESS:
7766771Sjb150015 			of = smb_llist_next(&node->n_ofile_list, of);
7776771Sjb150015 			break;
7786771Sjb150015 		default:
7796771Sjb150015 			ASSERT(status == NT_STATUS_SHARING_VIOLATION);
7805772Sas200622 			smb_llist_exit(&node->n_ofile_list);
7816771Sjb150015 			return (status);
7825772Sas200622 		}
7835772Sas200622 	}
7845772Sas200622 	smb_llist_exit(&node->n_ofile_list);
7855772Sas200622 
7865772Sas200622 	/*
7875772Sas200622 	 * system-wide share check
7885772Sas200622 	 */
7895772Sas200622 	if (nbl_share_conflict(node->vp, NBL_REMOVE, NULL))
7905772Sas200622 		return (NT_STATUS_SHARING_VIOLATION);
7915772Sas200622 	else
7925772Sas200622 		return (NT_STATUS_SUCCESS);
7935772Sas200622 }
7945772Sas200622 
7959914Samw@Sun.COM void
7969914Samw@Sun.COM smb_node_notify_change(smb_node_t *node)
7979914Samw@Sun.COM {
7989914Samw@Sun.COM 	SMB_NODE_VALID(node);
7999914Samw@Sun.COM 
8009914Samw@Sun.COM 	if (node->flags & NODE_FLAGS_NOTIFY_CHANGE) {
8019914Samw@Sun.COM 		node->flags |= NODE_FLAGS_CHANGED;
8029914Samw@Sun.COM 		smb_process_node_notify_change_queue(node);
8039914Samw@Sun.COM 	}
8049914Samw@Sun.COM }
8059914Samw@Sun.COM 
8065772Sas200622 /*
8075772Sas200622  * smb_node_start_crit()
8085772Sas200622  *
8095772Sas200622  * Enter critical region for share reservations.
8105772Sas200622  * See comments above smb_fsop_shrlock().
8115772Sas200622  */
8125772Sas200622 
8135772Sas200622 void
8145772Sas200622 smb_node_start_crit(smb_node_t *node, krw_t mode)
8155772Sas200622 {
8168934SJose.Borrego@Sun.COM 	rw_enter(&node->n_lock, mode);
8175772Sas200622 	nbl_start_crit(node->vp, mode);
8185772Sas200622 }
8195772Sas200622 
8205772Sas200622 /*
8215772Sas200622  * smb_node_end_crit()
8225772Sas200622  *
8235772Sas200622  * Exit critical region for share reservations.
8245772Sas200622  */
8255772Sas200622 
8265772Sas200622 void
8275772Sas200622 smb_node_end_crit(smb_node_t *node)
8285772Sas200622 {
8295772Sas200622 	nbl_end_crit(node->vp);
8308934SJose.Borrego@Sun.COM 	rw_exit(&node->n_lock);
8315772Sas200622 }
8325772Sas200622 
8335772Sas200622 int
8345772Sas200622 smb_node_in_crit(smb_node_t *node)
8355772Sas200622 {
8368934SJose.Borrego@Sun.COM 	return (nbl_in_crit(node->vp) && RW_LOCK_HELD(&node->n_lock));
8378934SJose.Borrego@Sun.COM }
8388934SJose.Borrego@Sun.COM 
8398934SJose.Borrego@Sun.COM void
8408934SJose.Borrego@Sun.COM smb_node_rdlock(smb_node_t *node)
8418934SJose.Borrego@Sun.COM {
8428934SJose.Borrego@Sun.COM 	rw_enter(&node->n_lock, RW_READER);
8438934SJose.Borrego@Sun.COM }
8448934SJose.Borrego@Sun.COM 
8458934SJose.Borrego@Sun.COM void
8468934SJose.Borrego@Sun.COM smb_node_wrlock(smb_node_t *node)
8478934SJose.Borrego@Sun.COM {
8488934SJose.Borrego@Sun.COM 	rw_enter(&node->n_lock, RW_WRITER);
8498934SJose.Borrego@Sun.COM }
8508934SJose.Borrego@Sun.COM 
8518934SJose.Borrego@Sun.COM void
8528934SJose.Borrego@Sun.COM smb_node_unlock(smb_node_t *node)
8538934SJose.Borrego@Sun.COM {
8548934SJose.Borrego@Sun.COM 	rw_exit(&node->n_lock);
8558934SJose.Borrego@Sun.COM }
8568934SJose.Borrego@Sun.COM 
8578934SJose.Borrego@Sun.COM uint32_t
8588934SJose.Borrego@Sun.COM smb_node_get_ofile_count(smb_node_t *node)
8598934SJose.Borrego@Sun.COM {
8608934SJose.Borrego@Sun.COM 	uint32_t	cntr;
8618934SJose.Borrego@Sun.COM 
8628934SJose.Borrego@Sun.COM 	SMB_NODE_VALID(node);
8638934SJose.Borrego@Sun.COM 
8648934SJose.Borrego@Sun.COM 	smb_llist_enter(&node->n_ofile_list, RW_READER);
8658934SJose.Borrego@Sun.COM 	cntr = smb_llist_get_count(&node->n_ofile_list);
8668934SJose.Borrego@Sun.COM 	smb_llist_exit(&node->n_ofile_list);
8678934SJose.Borrego@Sun.COM 	return (cntr);
8688934SJose.Borrego@Sun.COM }
8698934SJose.Borrego@Sun.COM 
8708934SJose.Borrego@Sun.COM void
8718934SJose.Borrego@Sun.COM smb_node_add_ofile(smb_node_t *node, smb_ofile_t *of)
8728934SJose.Borrego@Sun.COM {
8738934SJose.Borrego@Sun.COM 	SMB_NODE_VALID(node);
8748934SJose.Borrego@Sun.COM 
8758934SJose.Borrego@Sun.COM 	smb_llist_enter(&node->n_ofile_list, RW_WRITER);
8768934SJose.Borrego@Sun.COM 	smb_llist_insert_tail(&node->n_ofile_list, of);
8778934SJose.Borrego@Sun.COM 	smb_llist_exit(&node->n_ofile_list);
8788934SJose.Borrego@Sun.COM }
8798934SJose.Borrego@Sun.COM 
8808934SJose.Borrego@Sun.COM void
8818934SJose.Borrego@Sun.COM smb_node_rem_ofile(smb_node_t *node, smb_ofile_t *of)
8828934SJose.Borrego@Sun.COM {
8838934SJose.Borrego@Sun.COM 	SMB_NODE_VALID(node);
8848934SJose.Borrego@Sun.COM 
8858934SJose.Borrego@Sun.COM 	smb_llist_enter(&node->n_ofile_list, RW_WRITER);
8868934SJose.Borrego@Sun.COM 	smb_llist_remove(&node->n_ofile_list, of);
8878934SJose.Borrego@Sun.COM 	smb_llist_exit(&node->n_ofile_list);
8888934SJose.Borrego@Sun.COM }
8898934SJose.Borrego@Sun.COM 
890*10001SJoyce.McIntosh@Sun.COM /*
891*10001SJoyce.McIntosh@Sun.COM  * smb_node_inc_open_ofiles
892*10001SJoyce.McIntosh@Sun.COM  */
8938934SJose.Borrego@Sun.COM void
8948934SJose.Borrego@Sun.COM smb_node_inc_open_ofiles(smb_node_t *node)
8958934SJose.Borrego@Sun.COM {
8968934SJose.Borrego@Sun.COM 	SMB_NODE_VALID(node);
8978934SJose.Borrego@Sun.COM 
8988934SJose.Borrego@Sun.COM 	mutex_enter(&node->n_mutex);
8998934SJose.Borrego@Sun.COM 	node->n_open_count++;
9008934SJose.Borrego@Sun.COM 	mutex_exit(&node->n_mutex);
901*10001SJoyce.McIntosh@Sun.COM 
902*10001SJoyce.McIntosh@Sun.COM 	smb_node_init_cached_timestamps(node);
9038934SJose.Borrego@Sun.COM }
9048934SJose.Borrego@Sun.COM 
905*10001SJoyce.McIntosh@Sun.COM /*
906*10001SJoyce.McIntosh@Sun.COM  * smb_node_dec_open_ofiles
907*10001SJoyce.McIntosh@Sun.COM  */
9088934SJose.Borrego@Sun.COM void
9098934SJose.Borrego@Sun.COM smb_node_dec_open_ofiles(smb_node_t *node)
9108934SJose.Borrego@Sun.COM {
9118934SJose.Borrego@Sun.COM 	SMB_NODE_VALID(node);
9128934SJose.Borrego@Sun.COM 
9138934SJose.Borrego@Sun.COM 	mutex_enter(&node->n_mutex);
9148934SJose.Borrego@Sun.COM 	node->n_open_count--;
9158934SJose.Borrego@Sun.COM 	mutex_exit(&node->n_mutex);
916*10001SJoyce.McIntosh@Sun.COM 
917*10001SJoyce.McIntosh@Sun.COM 	smb_node_clear_cached_timestamps(node);
9188934SJose.Borrego@Sun.COM }
9198934SJose.Borrego@Sun.COM 
9208934SJose.Borrego@Sun.COM uint32_t
9218934SJose.Borrego@Sun.COM smb_node_get_open_ofiles(smb_node_t *node)
9228934SJose.Borrego@Sun.COM {
9238934SJose.Borrego@Sun.COM 	uint32_t	cnt;
9248934SJose.Borrego@Sun.COM 
9258934SJose.Borrego@Sun.COM 	SMB_NODE_VALID(node);
9268934SJose.Borrego@Sun.COM 
9278934SJose.Borrego@Sun.COM 	mutex_enter(&node->n_mutex);
9288934SJose.Borrego@Sun.COM 	cnt = node->n_open_count;
9298934SJose.Borrego@Sun.COM 	mutex_exit(&node->n_mutex);
9308934SJose.Borrego@Sun.COM 	return (cnt);
9315772Sas200622 }
9328934SJose.Borrego@Sun.COM 
9338934SJose.Borrego@Sun.COM /*
9348934SJose.Borrego@Sun.COM  * smb_node_alloc
9358934SJose.Borrego@Sun.COM  */
9368934SJose.Borrego@Sun.COM static smb_node_t *
9378934SJose.Borrego@Sun.COM smb_node_alloc(
9388934SJose.Borrego@Sun.COM     char	*od_name,
9398934SJose.Borrego@Sun.COM     vnode_t	*vp,
9408934SJose.Borrego@Sun.COM     smb_llist_t	*bucket,
9418934SJose.Borrego@Sun.COM     uint32_t	hashkey)
9428934SJose.Borrego@Sun.COM {
9438934SJose.Borrego@Sun.COM 	smb_node_t	*node;
9448934SJose.Borrego@Sun.COM 
9458934SJose.Borrego@Sun.COM 	node = kmem_cache_alloc(smb_node_cache, KM_SLEEP);
9468934SJose.Borrego@Sun.COM 
9478934SJose.Borrego@Sun.COM 	if (node->n_audit_buf != NULL)
9488934SJose.Borrego@Sun.COM 		node->n_audit_buf->anb_index = 0;
9498934SJose.Borrego@Sun.COM 
950*10001SJoyce.McIntosh@Sun.COM 	node->flags = 0;
9518934SJose.Borrego@Sun.COM 	VN_HOLD(vp);
9528934SJose.Borrego@Sun.COM 	node->vp = vp;
9538934SJose.Borrego@Sun.COM 	node->n_refcnt = 1;
9548934SJose.Borrego@Sun.COM 	node->n_hash_bucket = bucket;
9558934SJose.Borrego@Sun.COM 	node->n_hashkey = hashkey;
9568934SJose.Borrego@Sun.COM 	node->n_orig_uid = 0;
9578934SJose.Borrego@Sun.COM 	node->readonly_creator = NULL;
9588934SJose.Borrego@Sun.COM 	node->waiting_event = 0;
9598934SJose.Borrego@Sun.COM 	node->what = 0;
9608934SJose.Borrego@Sun.COM 	node->n_open_count = 0;
9618934SJose.Borrego@Sun.COM 	node->dir_snode = NULL;
9628934SJose.Borrego@Sun.COM 	node->unnamed_stream_node = NULL;
9638934SJose.Borrego@Sun.COM 	node->delete_on_close_cred = NULL;
9649231SAfshin.Ardakani@Sun.COM 	node->n_delete_on_close_flags = 0;
9658934SJose.Borrego@Sun.COM 
9668934SJose.Borrego@Sun.COM 	(void) strlcpy(node->od_name, od_name, sizeof (node->od_name));
9678934SJose.Borrego@Sun.COM 	if (strcmp(od_name, XATTR_DIR) == 0)
9688934SJose.Borrego@Sun.COM 		node->flags |= NODE_XATTR_DIR;
9698934SJose.Borrego@Sun.COM 
9708934SJose.Borrego@Sun.COM 	node->n_state = SMB_NODE_STATE_AVAILABLE;
9718934SJose.Borrego@Sun.COM 	node->n_magic = SMB_NODE_MAGIC;
9728934SJose.Borrego@Sun.COM 	return (node);
9738934SJose.Borrego@Sun.COM }
9748934SJose.Borrego@Sun.COM 
9758934SJose.Borrego@Sun.COM /*
9768934SJose.Borrego@Sun.COM  * smb_node_free
9778934SJose.Borrego@Sun.COM  */
9788934SJose.Borrego@Sun.COM static void
9798934SJose.Borrego@Sun.COM smb_node_free(smb_node_t *node)
9808934SJose.Borrego@Sun.COM {
9818934SJose.Borrego@Sun.COM 	SMB_NODE_VALID(node);
9828934SJose.Borrego@Sun.COM 
9838934SJose.Borrego@Sun.COM 	node->n_magic = 0;
9848934SJose.Borrego@Sun.COM 	VERIFY(!list_link_active(&node->n_lnd));
9858934SJose.Borrego@Sun.COM 	VERIFY(node->n_lock_list.ll_count == 0);
9868934SJose.Borrego@Sun.COM 	VERIFY(node->n_ofile_list.ll_count == 0);
9878934SJose.Borrego@Sun.COM 	VERIFY(node->n_oplock.ol_xthread == NULL);
9889021Samw@Sun.COM 	VERIFY(mutex_owner(&node->n_mutex) == NULL);
9899021Samw@Sun.COM 	VERIFY(!RW_LOCK_HELD(&node->n_lock));
9908934SJose.Borrego@Sun.COM 	VN_RELE(node->vp);
9918934SJose.Borrego@Sun.COM 	kmem_cache_free(smb_node_cache, node);
9928934SJose.Borrego@Sun.COM }
9938934SJose.Borrego@Sun.COM 
9948934SJose.Borrego@Sun.COM /*
9958934SJose.Borrego@Sun.COM  * smb_node_constructor
9968934SJose.Borrego@Sun.COM  */
9978934SJose.Borrego@Sun.COM static int
9988934SJose.Borrego@Sun.COM smb_node_constructor(void *buf, void *un, int kmflags)
9998934SJose.Borrego@Sun.COM {
10008934SJose.Borrego@Sun.COM 	_NOTE(ARGUNUSED(kmflags, un))
10018934SJose.Borrego@Sun.COM 
10028934SJose.Borrego@Sun.COM 	smb_node_t	*node = (smb_node_t *)buf;
10038934SJose.Borrego@Sun.COM 
10048934SJose.Borrego@Sun.COM 	bzero(node, sizeof (smb_node_t));
10058934SJose.Borrego@Sun.COM 
10068934SJose.Borrego@Sun.COM 	smb_llist_constructor(&node->n_ofile_list, sizeof (smb_ofile_t),
10078934SJose.Borrego@Sun.COM 	    offsetof(smb_ofile_t, f_nnd));
10088934SJose.Borrego@Sun.COM 	smb_llist_constructor(&node->n_lock_list, sizeof (smb_lock_t),
10098934SJose.Borrego@Sun.COM 	    offsetof(smb_lock_t, l_lnd));
10108934SJose.Borrego@Sun.COM 	cv_init(&node->n_oplock.ol_cv, NULL, CV_DEFAULT, NULL);
10118934SJose.Borrego@Sun.COM 	rw_init(&node->n_lock, NULL, RW_DEFAULT, NULL);
10128934SJose.Borrego@Sun.COM 	mutex_init(&node->n_mutex, NULL, MUTEX_DEFAULT, NULL);
10138934SJose.Borrego@Sun.COM 	smb_node_create_audit_buf(node, kmflags);
10148934SJose.Borrego@Sun.COM 	return (0);
10158934SJose.Borrego@Sun.COM }
10168934SJose.Borrego@Sun.COM 
10178934SJose.Borrego@Sun.COM /*
10188934SJose.Borrego@Sun.COM  * smb_node_destructor
10198934SJose.Borrego@Sun.COM  */
10208934SJose.Borrego@Sun.COM static void
10218934SJose.Borrego@Sun.COM smb_node_destructor(void *buf, void *un)
10228934SJose.Borrego@Sun.COM {
10238934SJose.Borrego@Sun.COM 	_NOTE(ARGUNUSED(un))
10248934SJose.Borrego@Sun.COM 
10258934SJose.Borrego@Sun.COM 	smb_node_t	*node = (smb_node_t *)buf;
10268934SJose.Borrego@Sun.COM 
10278934SJose.Borrego@Sun.COM 	smb_node_destroy_audit_buf(node);
10288934SJose.Borrego@Sun.COM 	mutex_destroy(&node->n_mutex);
10298934SJose.Borrego@Sun.COM 	rw_destroy(&node->n_lock);
10308934SJose.Borrego@Sun.COM 	cv_destroy(&node->n_oplock.ol_cv);
10318934SJose.Borrego@Sun.COM 	smb_llist_destructor(&node->n_lock_list);
10328934SJose.Borrego@Sun.COM 	smb_llist_destructor(&node->n_ofile_list);
10338934SJose.Borrego@Sun.COM }
10348934SJose.Borrego@Sun.COM 
10358934SJose.Borrego@Sun.COM /*
10368934SJose.Borrego@Sun.COM  * smb_node_create_audit_buf
10378934SJose.Borrego@Sun.COM  */
10388934SJose.Borrego@Sun.COM static void
10398934SJose.Borrego@Sun.COM smb_node_create_audit_buf(smb_node_t *node, int kmflags)
10408934SJose.Borrego@Sun.COM {
10418934SJose.Borrego@Sun.COM 	smb_audit_buf_node_t	*abn;
10428934SJose.Borrego@Sun.COM 
10438934SJose.Borrego@Sun.COM 	if (smb_audit_flags & SMB_AUDIT_NODE) {
10448934SJose.Borrego@Sun.COM 		abn = kmem_zalloc(sizeof (smb_audit_buf_node_t), kmflags);
10458934SJose.Borrego@Sun.COM 		abn->anb_max_index = SMB_AUDIT_BUF_MAX_REC - 1;
10468934SJose.Borrego@Sun.COM 		node->n_audit_buf = abn;
10478934SJose.Borrego@Sun.COM 	}
10488934SJose.Borrego@Sun.COM }
10498934SJose.Borrego@Sun.COM 
10508934SJose.Borrego@Sun.COM /*
10518934SJose.Borrego@Sun.COM  * smb_node_destroy_audit_buf
10528934SJose.Borrego@Sun.COM  */
10538934SJose.Borrego@Sun.COM static void
10548934SJose.Borrego@Sun.COM smb_node_destroy_audit_buf(smb_node_t *node)
10558934SJose.Borrego@Sun.COM {
10568934SJose.Borrego@Sun.COM 	if (node->n_audit_buf != NULL) {
10578934SJose.Borrego@Sun.COM 		kmem_free(node->n_audit_buf, sizeof (smb_audit_buf_node_t));
10588934SJose.Borrego@Sun.COM 		node->n_audit_buf = NULL;
10598934SJose.Borrego@Sun.COM 	}
10608934SJose.Borrego@Sun.COM }
10618934SJose.Borrego@Sun.COM 
10628934SJose.Borrego@Sun.COM /*
10638934SJose.Borrego@Sun.COM  * smb_node_audit
10648934SJose.Borrego@Sun.COM  *
10658934SJose.Borrego@Sun.COM  * This function saves the calling stack in the audit buffer of the node passed
10668934SJose.Borrego@Sun.COM  * in.
10678934SJose.Borrego@Sun.COM  */
10688934SJose.Borrego@Sun.COM static void
10698934SJose.Borrego@Sun.COM smb_node_audit(smb_node_t *node)
10708934SJose.Borrego@Sun.COM {
10718934SJose.Borrego@Sun.COM 	smb_audit_buf_node_t	*abn;
10728934SJose.Borrego@Sun.COM 	smb_audit_record_node_t	*anr;
10738934SJose.Borrego@Sun.COM 
10748934SJose.Borrego@Sun.COM 	if (node->n_audit_buf) {
10758934SJose.Borrego@Sun.COM 		abn = node->n_audit_buf;
10768934SJose.Borrego@Sun.COM 		anr = abn->anb_records;
10778934SJose.Borrego@Sun.COM 		anr += abn->anb_index;
10788934SJose.Borrego@Sun.COM 		abn->anb_index++;
10798934SJose.Borrego@Sun.COM 		abn->anb_index &= abn->anb_max_index;
10808934SJose.Borrego@Sun.COM 		anr->anr_refcnt = node->n_refcnt;
10818934SJose.Borrego@Sun.COM 		anr->anr_depth = getpcstack(anr->anr_stack,
10828934SJose.Borrego@Sun.COM 		    SMB_AUDIT_STACK_DEPTH);
10838934SJose.Borrego@Sun.COM 	}
10848934SJose.Borrego@Sun.COM }
10858934SJose.Borrego@Sun.COM 
10868934SJose.Borrego@Sun.COM static smb_llist_t *
10878934SJose.Borrego@Sun.COM smb_node_get_hash(fsid_t *fsid, smb_attr_t *attr, uint32_t *phashkey)
10888934SJose.Borrego@Sun.COM {
10898934SJose.Borrego@Sun.COM 	uint32_t	hashkey;
10908934SJose.Borrego@Sun.COM 
10918934SJose.Borrego@Sun.COM 	hashkey = fsid->val[0] + attr->sa_vattr.va_nodeid;
10928934SJose.Borrego@Sun.COM 	hashkey += (hashkey >> 24) + (hashkey >> 16) + (hashkey >> 8);
10938934SJose.Borrego@Sun.COM 	*phashkey = hashkey;
10948934SJose.Borrego@Sun.COM 	return (&smb_node_hash_table[(hashkey & SMBND_HASH_MASK)]);
10958934SJose.Borrego@Sun.COM }
1096*10001SJoyce.McIntosh@Sun.COM 
1097*10001SJoyce.McIntosh@Sun.COM boolean_t
1098*10001SJoyce.McIntosh@Sun.COM smb_node_is_dir(smb_node_t *node)
1099*10001SJoyce.McIntosh@Sun.COM {
1100*10001SJoyce.McIntosh@Sun.COM 	SMB_NODE_VALID(node);
1101*10001SJoyce.McIntosh@Sun.COM 	return (node->vp->v_type == VDIR);
1102*10001SJoyce.McIntosh@Sun.COM }
1103*10001SJoyce.McIntosh@Sun.COM 
1104*10001SJoyce.McIntosh@Sun.COM boolean_t
1105*10001SJoyce.McIntosh@Sun.COM smb_node_is_link(smb_node_t *node)
1106*10001SJoyce.McIntosh@Sun.COM {
1107*10001SJoyce.McIntosh@Sun.COM 	SMB_NODE_VALID(node);
1108*10001SJoyce.McIntosh@Sun.COM 	return (node->vp->v_type == VLNK);
1109*10001SJoyce.McIntosh@Sun.COM }
1110*10001SJoyce.McIntosh@Sun.COM 
1111*10001SJoyce.McIntosh@Sun.COM /*
1112*10001SJoyce.McIntosh@Sun.COM  * smb_node_file_is_readonly
1113*10001SJoyce.McIntosh@Sun.COM  *
1114*10001SJoyce.McIntosh@Sun.COM  * Checks if the file (which node represents) is marked readonly
1115*10001SJoyce.McIntosh@Sun.COM  * in the filesystem. No account is taken of any pending readonly
1116*10001SJoyce.McIntosh@Sun.COM  * in the node, which must be handled by the callers.
1117*10001SJoyce.McIntosh@Sun.COM  * (See SMB_OFILE_IS_READONLY and SMB_PATHFILE_IS_READONLY)
1118*10001SJoyce.McIntosh@Sun.COM  */
1119*10001SJoyce.McIntosh@Sun.COM boolean_t
1120*10001SJoyce.McIntosh@Sun.COM smb_node_file_is_readonly(smb_node_t *node)
1121*10001SJoyce.McIntosh@Sun.COM {
1122*10001SJoyce.McIntosh@Sun.COM 	smb_attr_t attr;
1123*10001SJoyce.McIntosh@Sun.COM 
1124*10001SJoyce.McIntosh@Sun.COM 	if (node == NULL)
1125*10001SJoyce.McIntosh@Sun.COM 		return (B_FALSE);
1126*10001SJoyce.McIntosh@Sun.COM 
1127*10001SJoyce.McIntosh@Sun.COM 	bzero(&attr, sizeof (smb_attr_t));
1128*10001SJoyce.McIntosh@Sun.COM 	attr.sa_mask = SMB_AT_DOSATTR;
1129*10001SJoyce.McIntosh@Sun.COM 	(void) smb_fsop_getattr(NULL, kcred, node, &attr);
1130*10001SJoyce.McIntosh@Sun.COM 	return ((attr.sa_dosattr & FILE_ATTRIBUTE_READONLY) != 0);
1131*10001SJoyce.McIntosh@Sun.COM }
1132*10001SJoyce.McIntosh@Sun.COM 
1133*10001SJoyce.McIntosh@Sun.COM /*
1134*10001SJoyce.McIntosh@Sun.COM  * smb_node_setattr
1135*10001SJoyce.McIntosh@Sun.COM  *
1136*10001SJoyce.McIntosh@Sun.COM  * The sr may be NULL, for example when closing an ofile.
1137*10001SJoyce.McIntosh@Sun.COM  * The ofile may be NULL, for example when a client request
1138*10001SJoyce.McIntosh@Sun.COM  * specifies the file by pathname.
1139*10001SJoyce.McIntosh@Sun.COM  *
1140*10001SJoyce.McIntosh@Sun.COM  * When attributes are set on an ofile, any pending timestamps
1141*10001SJoyce.McIntosh@Sun.COM  * from a write request on the ofile are implicitly set to "now".
1142*10001SJoyce.McIntosh@Sun.COM  * For compatibility with windows the following timestamps are
1143*10001SJoyce.McIntosh@Sun.COM  * also implicitly set to now:
1144*10001SJoyce.McIntosh@Sun.COM  * - if any attribute is being explicitly set, set ctime to now
1145*10001SJoyce.McIntosh@Sun.COM  * - if file size is being explicitly set, set atime & ctime to now
1146*10001SJoyce.McIntosh@Sun.COM  *
1147*10001SJoyce.McIntosh@Sun.COM  * Any attribute that is being explicitly set, or has previously
1148*10001SJoyce.McIntosh@Sun.COM  * been explicitly set on the ofile, is excluded from implicit
1149*10001SJoyce.McIntosh@Sun.COM  * (now) setting.
1150*10001SJoyce.McIntosh@Sun.COM  *
1151*10001SJoyce.McIntosh@Sun.COM  * Updates the node's cached timestamp values.
1152*10001SJoyce.McIntosh@Sun.COM  * Updates the ofile's explicit times flag.
1153*10001SJoyce.McIntosh@Sun.COM  *
1154*10001SJoyce.McIntosh@Sun.COM  * Returns: errno
1155*10001SJoyce.McIntosh@Sun.COM  */
1156*10001SJoyce.McIntosh@Sun.COM int
1157*10001SJoyce.McIntosh@Sun.COM smb_node_setattr(smb_request_t *sr, smb_node_t *node,
1158*10001SJoyce.McIntosh@Sun.COM     cred_t *cr, smb_ofile_t *of, smb_attr_t *attr)
1159*10001SJoyce.McIntosh@Sun.COM {
1160*10001SJoyce.McIntosh@Sun.COM 	int rc;
1161*10001SJoyce.McIntosh@Sun.COM 	uint32_t what;
1162*10001SJoyce.McIntosh@Sun.COM 	uint32_t now_times = 0;
1163*10001SJoyce.McIntosh@Sun.COM 	timestruc_t now;
1164*10001SJoyce.McIntosh@Sun.COM 
1165*10001SJoyce.McIntosh@Sun.COM 	ASSERT(attr);
1166*10001SJoyce.McIntosh@Sun.COM 	SMB_NODE_VALID(node);
1167*10001SJoyce.McIntosh@Sun.COM 
1168*10001SJoyce.McIntosh@Sun.COM 	what = attr->sa_mask;
1169*10001SJoyce.McIntosh@Sun.COM 
1170*10001SJoyce.McIntosh@Sun.COM 	/* determine which timestamps to implicitly set to "now" */
1171*10001SJoyce.McIntosh@Sun.COM 	if (what)
1172*10001SJoyce.McIntosh@Sun.COM 		now_times |= SMB_AT_CTIME;
1173*10001SJoyce.McIntosh@Sun.COM 	if (what & SMB_AT_SIZE)
1174*10001SJoyce.McIntosh@Sun.COM 		now_times |= (SMB_AT_MTIME | SMB_AT_CTIME);
1175*10001SJoyce.McIntosh@Sun.COM 	if (of) {
1176*10001SJoyce.McIntosh@Sun.COM 		if (smb_ofile_write_time_pending(of))
1177*10001SJoyce.McIntosh@Sun.COM 			now_times |=
1178*10001SJoyce.McIntosh@Sun.COM 			    (SMB_AT_MTIME | SMB_AT_CTIME | SMB_AT_ATIME);
1179*10001SJoyce.McIntosh@Sun.COM 		now_times &= ~(smb_ofile_explicit_times(of));
1180*10001SJoyce.McIntosh@Sun.COM 	}
1181*10001SJoyce.McIntosh@Sun.COM 	now_times &= ~what;
1182*10001SJoyce.McIntosh@Sun.COM 
1183*10001SJoyce.McIntosh@Sun.COM 	if (now_times) {
1184*10001SJoyce.McIntosh@Sun.COM 		gethrestime(&now);
1185*10001SJoyce.McIntosh@Sun.COM 
1186*10001SJoyce.McIntosh@Sun.COM 		if (now_times & SMB_AT_ATIME) {
1187*10001SJoyce.McIntosh@Sun.COM 			attr->sa_vattr.va_atime = now;
1188*10001SJoyce.McIntosh@Sun.COM 			attr->sa_mask |= SMB_AT_ATIME;
1189*10001SJoyce.McIntosh@Sun.COM 		}
1190*10001SJoyce.McIntosh@Sun.COM 		if (now_times & SMB_AT_MTIME) {
1191*10001SJoyce.McIntosh@Sun.COM 			attr->sa_vattr.va_mtime = now;
1192*10001SJoyce.McIntosh@Sun.COM 			attr->sa_mask |= SMB_AT_MTIME;
1193*10001SJoyce.McIntosh@Sun.COM 		}
1194*10001SJoyce.McIntosh@Sun.COM 		if (now_times & SMB_AT_CTIME) {
1195*10001SJoyce.McIntosh@Sun.COM 			attr->sa_vattr.va_ctime = now;
1196*10001SJoyce.McIntosh@Sun.COM 			attr->sa_mask |= SMB_AT_CTIME;
1197*10001SJoyce.McIntosh@Sun.COM 		}
1198*10001SJoyce.McIntosh@Sun.COM 	}
1199*10001SJoyce.McIntosh@Sun.COM 
1200*10001SJoyce.McIntosh@Sun.COM 	if (attr->sa_mask == 0)
1201*10001SJoyce.McIntosh@Sun.COM 		return (0);
1202*10001SJoyce.McIntosh@Sun.COM 
1203*10001SJoyce.McIntosh@Sun.COM 	rc = smb_fsop_setattr(sr, cr, node, attr);
1204*10001SJoyce.McIntosh@Sun.COM 	if (rc != 0)
1205*10001SJoyce.McIntosh@Sun.COM 		return (rc);
1206*10001SJoyce.McIntosh@Sun.COM 
1207*10001SJoyce.McIntosh@Sun.COM 	smb_node_set_cached_timestamps(node, attr);
1208*10001SJoyce.McIntosh@Sun.COM 
1209*10001SJoyce.McIntosh@Sun.COM 	if (of)
1210*10001SJoyce.McIntosh@Sun.COM 		smb_ofile_set_explicit_times(of, (what & SMB_AT_TIMES));
1211*10001SJoyce.McIntosh@Sun.COM 
1212*10001SJoyce.McIntosh@Sun.COM 	return (0);
1213*10001SJoyce.McIntosh@Sun.COM }
1214*10001SJoyce.McIntosh@Sun.COM 
1215*10001SJoyce.McIntosh@Sun.COM /*
1216*10001SJoyce.McIntosh@Sun.COM  * smb_node_getattr
1217*10001SJoyce.McIntosh@Sun.COM  *
1218*10001SJoyce.McIntosh@Sun.COM  * Get attributes from the file system and apply any smb-specific
1219*10001SJoyce.McIntosh@Sun.COM  * overrides for size, dos attributes and timestamps
1220*10001SJoyce.McIntosh@Sun.COM  *
1221*10001SJoyce.McIntosh@Sun.COM  * node->readonly_creator reflects whether a readonly set is pending
1222*10001SJoyce.McIntosh@Sun.COM  * from a readonly create. The readonly attribute should be visible to
1223*10001SJoyce.McIntosh@Sun.COM  * all clients even though the readonly creator fid is immune to the
1224*10001SJoyce.McIntosh@Sun.COM  * readonly bit until close.
1225*10001SJoyce.McIntosh@Sun.COM  *
1226*10001SJoyce.McIntosh@Sun.COM  * Returns: errno
1227*10001SJoyce.McIntosh@Sun.COM  */
1228*10001SJoyce.McIntosh@Sun.COM int
1229*10001SJoyce.McIntosh@Sun.COM smb_node_getattr(smb_request_t *sr, smb_node_t *node, smb_attr_t *attr)
1230*10001SJoyce.McIntosh@Sun.COM {
1231*10001SJoyce.McIntosh@Sun.COM 	int rc;
1232*10001SJoyce.McIntosh@Sun.COM 
1233*10001SJoyce.McIntosh@Sun.COM 	SMB_NODE_VALID(node);
1234*10001SJoyce.McIntosh@Sun.COM 
1235*10001SJoyce.McIntosh@Sun.COM 	bzero(attr, sizeof (smb_attr_t));
1236*10001SJoyce.McIntosh@Sun.COM 	attr->sa_mask = SMB_AT_ALL;
1237*10001SJoyce.McIntosh@Sun.COM 	rc = smb_fsop_getattr(sr, kcred, node, attr);
1238*10001SJoyce.McIntosh@Sun.COM 	if (rc != 0)
1239*10001SJoyce.McIntosh@Sun.COM 		return (rc);
1240*10001SJoyce.McIntosh@Sun.COM 
1241*10001SJoyce.McIntosh@Sun.COM 	mutex_enter(&node->n_mutex);
1242*10001SJoyce.McIntosh@Sun.COM 
1243*10001SJoyce.McIntosh@Sun.COM 	if (node->vp->v_type == VDIR)
1244*10001SJoyce.McIntosh@Sun.COM 		attr->sa_vattr.va_size = 0;
1245*10001SJoyce.McIntosh@Sun.COM 
1246*10001SJoyce.McIntosh@Sun.COM 	if (node->readonly_creator)
1247*10001SJoyce.McIntosh@Sun.COM 		attr->sa_dosattr |= FILE_ATTRIBUTE_READONLY;
1248*10001SJoyce.McIntosh@Sun.COM 	if (attr->sa_dosattr == 0)
1249*10001SJoyce.McIntosh@Sun.COM 		attr->sa_dosattr = FILE_ATTRIBUTE_NORMAL;
1250*10001SJoyce.McIntosh@Sun.COM 
1251*10001SJoyce.McIntosh@Sun.COM 	mutex_exit(&node->n_mutex);
1252*10001SJoyce.McIntosh@Sun.COM 
1253*10001SJoyce.McIntosh@Sun.COM 	smb_node_get_cached_timestamps(node, attr);
1254*10001SJoyce.McIntosh@Sun.COM 	return (0);
1255*10001SJoyce.McIntosh@Sun.COM }
1256*10001SJoyce.McIntosh@Sun.COM 
1257*10001SJoyce.McIntosh@Sun.COM /*
1258*10001SJoyce.McIntosh@Sun.COM  * Timestamp caching
1259*10001SJoyce.McIntosh@Sun.COM  *
1260*10001SJoyce.McIntosh@Sun.COM  * Solaris file systems handle timestamps different from NTFS. For
1261*10001SJoyce.McIntosh@Sun.COM  * example when file data is written NTFS doesn't update the timestamps
1262*10001SJoyce.McIntosh@Sun.COM  * until the file is closed, and then only if they haven't been explicity
1263*10001SJoyce.McIntosh@Sun.COM  * set via a set attribute request. In order to provide a more similar
1264*10001SJoyce.McIntosh@Sun.COM  * view of an open file's timestamps, we cache the timestamps in the
1265*10001SJoyce.McIntosh@Sun.COM  * node and manipulate them in a manner more consistent with windows.
1266*10001SJoyce.McIntosh@Sun.COM  * (See handling of explicit times and pending timestamps from a write
1267*10001SJoyce.McIntosh@Sun.COM  * request in smb_node_getattr and smb_node_setattr above.)
1268*10001SJoyce.McIntosh@Sun.COM  * Timestamps remain cached while there are open ofiles for the node.
1269*10001SJoyce.McIntosh@Sun.COM  * This includes open ofiles for named streams.
1270*10001SJoyce.McIntosh@Sun.COM  * n_open_ofiles cannot be used as this doesn't include ofiles opened
1271*10001SJoyce.McIntosh@Sun.COM  * for the node's named streams. Thus n_timestamps contains a count
1272*10001SJoyce.McIntosh@Sun.COM  * of open ofiles (t_open_ofiles), including named streams' ofiles,
1273*10001SJoyce.McIntosh@Sun.COM  * to be used to control timestamp caching.
1274*10001SJoyce.McIntosh@Sun.COM  *
1275*10001SJoyce.McIntosh@Sun.COM  * If a node represents a named stream the associated unnamed streams
1276*10001SJoyce.McIntosh@Sun.COM  * cached timestamps are used instead.
1277*10001SJoyce.McIntosh@Sun.COM  */
1278*10001SJoyce.McIntosh@Sun.COM 
1279*10001SJoyce.McIntosh@Sun.COM /*
1280*10001SJoyce.McIntosh@Sun.COM  * smb_node_init_cached_timestamps
1281*10001SJoyce.McIntosh@Sun.COM  *
1282*10001SJoyce.McIntosh@Sun.COM  * Increment count of open ofiles which are using the cached timestamps.
1283*10001SJoyce.McIntosh@Sun.COM  * If this is the first open ofile, init the cached timestamps from the
1284*10001SJoyce.McIntosh@Sun.COM  * file system values.
1285*10001SJoyce.McIntosh@Sun.COM  */
1286*10001SJoyce.McIntosh@Sun.COM static void
1287*10001SJoyce.McIntosh@Sun.COM smb_node_init_cached_timestamps(smb_node_t *node)
1288*10001SJoyce.McIntosh@Sun.COM {
1289*10001SJoyce.McIntosh@Sun.COM 	smb_node_t *unode;
1290*10001SJoyce.McIntosh@Sun.COM 	smb_attr_t attr;
1291*10001SJoyce.McIntosh@Sun.COM 
1292*10001SJoyce.McIntosh@Sun.COM 	if ((unode = SMB_IS_STREAM(node)) != NULL)
1293*10001SJoyce.McIntosh@Sun.COM 		node = unode;
1294*10001SJoyce.McIntosh@Sun.COM 
1295*10001SJoyce.McIntosh@Sun.COM 	mutex_enter(&node->n_mutex);
1296*10001SJoyce.McIntosh@Sun.COM 	++(node->n_timestamps.t_open_ofiles);
1297*10001SJoyce.McIntosh@Sun.COM 	if (node->n_timestamps.t_open_ofiles == 1) {
1298*10001SJoyce.McIntosh@Sun.COM 		bzero(&attr, sizeof (smb_attr_t));
1299*10001SJoyce.McIntosh@Sun.COM 		attr.sa_mask = SMB_AT_TIMES;
1300*10001SJoyce.McIntosh@Sun.COM 		(void) smb_fsop_getattr(NULL, kcred, node, &attr);
1301*10001SJoyce.McIntosh@Sun.COM 		node->n_timestamps.t_mtime = attr.sa_vattr.va_mtime;
1302*10001SJoyce.McIntosh@Sun.COM 		node->n_timestamps.t_atime = attr.sa_vattr.va_atime;
1303*10001SJoyce.McIntosh@Sun.COM 		node->n_timestamps.t_ctime = attr.sa_vattr.va_ctime;
1304*10001SJoyce.McIntosh@Sun.COM 		node->n_timestamps.t_crtime = attr.sa_crtime;
1305*10001SJoyce.McIntosh@Sun.COM 		node->n_timestamps.t_cached = B_TRUE;
1306*10001SJoyce.McIntosh@Sun.COM 	}
1307*10001SJoyce.McIntosh@Sun.COM 	mutex_exit(&node->n_mutex);
1308*10001SJoyce.McIntosh@Sun.COM }
1309*10001SJoyce.McIntosh@Sun.COM 
1310*10001SJoyce.McIntosh@Sun.COM /*
1311*10001SJoyce.McIntosh@Sun.COM  * smb_node_clear_cached_timestamps
1312*10001SJoyce.McIntosh@Sun.COM  *
1313*10001SJoyce.McIntosh@Sun.COM  * Decrement count of open ofiles using the cached timestamps.
1314*10001SJoyce.McIntosh@Sun.COM  * If the decremented count is zero, clear the cached timestamps.
1315*10001SJoyce.McIntosh@Sun.COM  */
1316*10001SJoyce.McIntosh@Sun.COM static void
1317*10001SJoyce.McIntosh@Sun.COM smb_node_clear_cached_timestamps(smb_node_t *node)
1318*10001SJoyce.McIntosh@Sun.COM {
1319*10001SJoyce.McIntosh@Sun.COM 	smb_node_t *unode;
1320*10001SJoyce.McIntosh@Sun.COM 
1321*10001SJoyce.McIntosh@Sun.COM 	if ((unode = SMB_IS_STREAM(node)) != NULL)
1322*10001SJoyce.McIntosh@Sun.COM 		node = unode;
1323*10001SJoyce.McIntosh@Sun.COM 
1324*10001SJoyce.McIntosh@Sun.COM 	mutex_enter(&node->n_mutex);
1325*10001SJoyce.McIntosh@Sun.COM 	ASSERT(node->n_timestamps.t_open_ofiles > 0);
1326*10001SJoyce.McIntosh@Sun.COM 	--(node->n_timestamps.t_open_ofiles);
1327*10001SJoyce.McIntosh@Sun.COM 	if (node->n_timestamps.t_open_ofiles == 0)
1328*10001SJoyce.McIntosh@Sun.COM 		bzero(&node->n_timestamps, sizeof (smb_times_t));
1329*10001SJoyce.McIntosh@Sun.COM 	mutex_exit(&node->n_mutex);
1330*10001SJoyce.McIntosh@Sun.COM }
1331*10001SJoyce.McIntosh@Sun.COM 
1332*10001SJoyce.McIntosh@Sun.COM /*
1333*10001SJoyce.McIntosh@Sun.COM  * smb_node_get_cached_timestamps
1334*10001SJoyce.McIntosh@Sun.COM  *
1335*10001SJoyce.McIntosh@Sun.COM  * Overwrite timestamps in attr with those cached in node.
1336*10001SJoyce.McIntosh@Sun.COM  */
1337*10001SJoyce.McIntosh@Sun.COM static void
1338*10001SJoyce.McIntosh@Sun.COM smb_node_get_cached_timestamps(smb_node_t *node, smb_attr_t *attr)
1339*10001SJoyce.McIntosh@Sun.COM {
1340*10001SJoyce.McIntosh@Sun.COM 	smb_node_t *unode;
1341*10001SJoyce.McIntosh@Sun.COM 
1342*10001SJoyce.McIntosh@Sun.COM 	if ((unode = SMB_IS_STREAM(node)) != NULL)
1343*10001SJoyce.McIntosh@Sun.COM 		node = unode;
1344*10001SJoyce.McIntosh@Sun.COM 
1345*10001SJoyce.McIntosh@Sun.COM 	mutex_enter(&node->n_mutex);
1346*10001SJoyce.McIntosh@Sun.COM 	if (node->n_timestamps.t_cached) {
1347*10001SJoyce.McIntosh@Sun.COM 		attr->sa_vattr.va_mtime = node->n_timestamps.t_mtime;
1348*10001SJoyce.McIntosh@Sun.COM 		attr->sa_vattr.va_atime = node->n_timestamps.t_atime;
1349*10001SJoyce.McIntosh@Sun.COM 		attr->sa_vattr.va_ctime = node->n_timestamps.t_ctime;
1350*10001SJoyce.McIntosh@Sun.COM 		attr->sa_crtime = node->n_timestamps.t_crtime;
1351*10001SJoyce.McIntosh@Sun.COM 	}
1352*10001SJoyce.McIntosh@Sun.COM 	mutex_exit(&node->n_mutex);
1353*10001SJoyce.McIntosh@Sun.COM }
1354*10001SJoyce.McIntosh@Sun.COM 
1355*10001SJoyce.McIntosh@Sun.COM /*
1356*10001SJoyce.McIntosh@Sun.COM  * smb_node_set_cached_timestamps
1357*10001SJoyce.McIntosh@Sun.COM  *
1358*10001SJoyce.McIntosh@Sun.COM  * Update the node's cached timestamps with values from attr.
1359*10001SJoyce.McIntosh@Sun.COM  */
1360*10001SJoyce.McIntosh@Sun.COM static void
1361*10001SJoyce.McIntosh@Sun.COM smb_node_set_cached_timestamps(smb_node_t *node, smb_attr_t *attr)
1362*10001SJoyce.McIntosh@Sun.COM {
1363*10001SJoyce.McIntosh@Sun.COM 	smb_node_t *unode;
1364*10001SJoyce.McIntosh@Sun.COM 
1365*10001SJoyce.McIntosh@Sun.COM 	if ((unode = SMB_IS_STREAM(node)) != NULL)
1366*10001SJoyce.McIntosh@Sun.COM 		node = unode;
1367*10001SJoyce.McIntosh@Sun.COM 
1368*10001SJoyce.McIntosh@Sun.COM 	mutex_enter(&node->n_mutex);
1369*10001SJoyce.McIntosh@Sun.COM 	if (node->n_timestamps.t_cached) {
1370*10001SJoyce.McIntosh@Sun.COM 		if (attr->sa_mask & SMB_AT_MTIME)
1371*10001SJoyce.McIntosh@Sun.COM 			node->n_timestamps.t_mtime = attr->sa_vattr.va_mtime;
1372*10001SJoyce.McIntosh@Sun.COM 		if (attr->sa_mask & SMB_AT_ATIME)
1373*10001SJoyce.McIntosh@Sun.COM 			node->n_timestamps.t_atime = attr->sa_vattr.va_atime;
1374*10001SJoyce.McIntosh@Sun.COM 		if (attr->sa_mask & SMB_AT_CTIME)
1375*10001SJoyce.McIntosh@Sun.COM 			node->n_timestamps.t_ctime = attr->sa_vattr.va_ctime;
1376*10001SJoyce.McIntosh@Sun.COM 		if (attr->sa_mask & SMB_AT_CRTIME)
1377*10001SJoyce.McIntosh@Sun.COM 			node->n_timestamps.t_crtime = attr->sa_crtime;
1378*10001SJoyce.McIntosh@Sun.COM 	}
1379*10001SJoyce.McIntosh@Sun.COM 	mutex_exit(&node->n_mutex);
1380*10001SJoyce.McIntosh@Sun.COM }
1381