xref: /onnv-gate/usr/src/uts/common/fs/smbsrv/smb_node.c (revision 10504:ee04788f8605)
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 *);
14310001SJoyce.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*10504SKeyur.Desai@Sun.COM 
149*10504SKeyur.Desai@Sun.COM static void smb_node_init_cached_data(smb_node_t *);
150*10504SKeyur.Desai@Sun.COM static void smb_node_clear_cached_data(smb_node_t *);
151*10504SKeyur.Desai@Sun.COM 
152*10504SKeyur.Desai@Sun.COM static void smb_node_init_cached_timestamps(smb_node_t *, smb_attr_t *);
15310001SJoyce.McIntosh@Sun.COM static void smb_node_clear_cached_timestamps(smb_node_t *);
15410001SJoyce.McIntosh@Sun.COM static void smb_node_get_cached_timestamps(smb_node_t *, smb_attr_t *);
15510001SJoyce.McIntosh@Sun.COM static void smb_node_set_cached_timestamps(smb_node_t *, smb_attr_t *);
1565331Samw 
157*10504SKeyur.Desai@Sun.COM static void smb_node_init_cached_allocsz(smb_node_t *, smb_attr_t *);
158*10504SKeyur.Desai@Sun.COM static void smb_node_clear_cached_allocsz(smb_node_t *);
159*10504SKeyur.Desai@Sun.COM static void smb_node_get_cached_allocsz(smb_node_t *, smb_attr_t *);
160*10504SKeyur.Desai@Sun.COM static void smb_node_set_cached_allocsz(smb_node_t *, smb_attr_t *);
161*10504SKeyur.Desai@Sun.COM 
1625331Samw #define	VALIDATE_DIR_NODE(_dir_, _node_) \
1635331Samw     ASSERT((_dir_)->n_magic == SMB_NODE_MAGIC); \
1645331Samw     ASSERT(((_dir_)->vp->v_xattrdir) || ((_dir_)->vp->v_type == VDIR)); \
16510122SJordan.Brown@Sun.COM     ASSERT((_dir_)->n_dnode != (_node_));
1665331Samw 
167*10504SKeyur.Desai@Sun.COM /* round sz to DEV_BSIZE block */
168*10504SKeyur.Desai@Sun.COM #define	SMB_ALLOCSZ(sz)	(((sz) + DEV_BSIZE-1) & ~(DEV_BSIZE-1))
169*10504SKeyur.Desai@Sun.COM 
1708934SJose.Borrego@Sun.COM static kmem_cache_t	*smb_node_cache = NULL;
1716139Sjb150015 static boolean_t	smb_node_initialized = B_FALSE;
1726139Sjb150015 static smb_llist_t	smb_node_hash_table[SMBND_HASH_MASK+1];
1736139Sjb150015 
1746139Sjb150015 /*
1756139Sjb150015  * smb_node_init
1766139Sjb150015  *
1776139Sjb150015  * Initialization of the SMB node layer.
1786139Sjb150015  *
1796139Sjb150015  * This function is not multi-thread safe. The caller must make sure only one
1806139Sjb150015  * thread makes the call.
1816139Sjb150015  */
1826139Sjb150015 int
1836139Sjb150015 smb_node_init(void)
1846139Sjb150015 {
1856139Sjb150015 	int	i;
1866139Sjb150015 
1876139Sjb150015 	if (smb_node_initialized)
1886139Sjb150015 		return (0);
1898934SJose.Borrego@Sun.COM 	smb_node_cache = kmem_cache_create(SMBSRV_KSTAT_NODE_CACHE,
1908934SJose.Borrego@Sun.COM 	    sizeof (smb_node_t), 8, smb_node_constructor, smb_node_destructor,
1918934SJose.Borrego@Sun.COM 	    NULL, NULL, NULL, 0);
1926139Sjb150015 
1936139Sjb150015 	for (i = 0; i <= SMBND_HASH_MASK; i++) {
1946139Sjb150015 		smb_llist_constructor(&smb_node_hash_table[i],
1956139Sjb150015 		    sizeof (smb_node_t), offsetof(smb_node_t, n_lnd));
1966139Sjb150015 	}
1976139Sjb150015 	smb_node_initialized = B_TRUE;
1986139Sjb150015 	return (0);
1996139Sjb150015 }
2006139Sjb150015 
2016139Sjb150015 /*
2026139Sjb150015  * smb_node_fini
2036139Sjb150015  *
2046139Sjb150015  * This function is not multi-thread safe. The caller must make sure only one
2056139Sjb150015  * thread makes the call.
2066139Sjb150015  */
2076139Sjb150015 void
2086139Sjb150015 smb_node_fini(void)
2096139Sjb150015 {
2106139Sjb150015 	int	i;
2116139Sjb150015 
2126139Sjb150015 	if (!smb_node_initialized)
2136139Sjb150015 		return;
2146139Sjb150015 
2156139Sjb150015 #ifdef DEBUG
2166139Sjb150015 	for (i = 0; i <= SMBND_HASH_MASK; i++) {
2176139Sjb150015 		smb_node_t	*node;
2186139Sjb150015 
2196139Sjb150015 		/*
2206139Sjb150015 		 * The following sequence is just intended for sanity check.
2216139Sjb150015 		 * This will have to be modified when the code goes into
2226139Sjb150015 		 * production.
2236139Sjb150015 		 *
2246139Sjb150015 		 * The SMB node hash table should be emtpy at this point. If the
2256139Sjb150015 		 * hash table is not empty a panic will be triggered.
2266139Sjb150015 		 *
2276139Sjb150015 		 * The reason why SMB nodes are still remaining in the hash
2286139Sjb150015 		 * table is problably due to a mismatch between calls to
2296139Sjb150015 		 * smb_node_lookup() and smb_node_release(). You must track that
2306139Sjb150015 		 * down.
2316139Sjb150015 		 */
2326139Sjb150015 		node = smb_llist_head(&smb_node_hash_table[i]);
2336139Sjb150015 		ASSERT(node == NULL);
2346139Sjb150015 	}
2356139Sjb150015 #endif
2366139Sjb150015 
2376139Sjb150015 	for (i = 0; i <= SMBND_HASH_MASK; i++) {
2386139Sjb150015 		smb_llist_destructor(&smb_node_hash_table[i]);
2396139Sjb150015 	}
2408934SJose.Borrego@Sun.COM 	kmem_cache_destroy(smb_node_cache);
2418934SJose.Borrego@Sun.COM 	smb_node_cache = NULL;
2426139Sjb150015 	smb_node_initialized = B_FALSE;
2436139Sjb150015 }
2446139Sjb150015 
2455331Samw /*
2465331Samw  * smb_node_lookup()
2475331Samw  *
2485331Samw  * NOTE: This routine should only be called by the file system interface layer,
2495331Samw  * and not by SMB.
2505331Samw  *
2515331Samw  * smb_node_lookup() is called upon successful lookup, mkdir, and create
2525331Samw  * (for both non-streams and streams).  In each of these cases, a held vnode is
2538670SJose.Borrego@Sun.COM  * passed into this routine.  If a new smb_node is created it will take its
2548670SJose.Borrego@Sun.COM  * own hold on the vnode.  The caller's hold therefore still belongs to, and
2558670SJose.Borrego@Sun.COM  * should be released by, the caller.
2565331Samw  *
2575331Samw  * A reference is taken on the smb_node whether found in the hash table
2585331Samw  * or newly created.
2595331Samw  *
2605331Samw  * If an smb_node needs to be created, a reference is also taken on the
26110122SJordan.Brown@Sun.COM  * dnode (if passed in).
2625331Samw  *
2635331Samw  * See smb_node_release() for details on the release of these references.
2645331Samw  */
2655331Samw 
2665331Samw /*ARGSUSED*/
2675331Samw smb_node_t *
2685331Samw smb_node_lookup(
2695331Samw     struct smb_request	*sr,
2705331Samw     struct open_param	*op,
2715331Samw     cred_t		*cred,
2725331Samw     vnode_t		*vp,
2735331Samw     char		*od_name,
27410122SJordan.Brown@Sun.COM     smb_node_t		*dnode,
27510122SJordan.Brown@Sun.COM     smb_node_t		*unode)
2765331Samw {
2775331Samw 	smb_llist_t		*node_hdr;
2785331Samw 	smb_node_t		*node;
27910001SJoyce.McIntosh@Sun.COM 	smb_attr_t		attr;
2805331Samw 	uint32_t		hashkey = 0;
2817348SJose.Borrego@Sun.COM 	fsid_t			fsid;
2825331Samw 	int			error;
2835331Samw 	krw_t			lock_mode;
2845331Samw 	vnode_t			*unnamed_vp = NULL;
2855331Samw 
2865331Samw 	/*
2875331Samw 	 * smb_vop_getattr() is called here instead of smb_fsop_getattr(),
2885331Samw 	 * because the node may not yet exist.  We also do not want to call
2895331Samw 	 * it with the list lock held.
2905331Samw 	 */
2915331Samw 
29210122SJordan.Brown@Sun.COM 	if (unode)
29310122SJordan.Brown@Sun.COM 		unnamed_vp = unode->vp;
2945331Samw 
2955331Samw 	/*
2965331Samw 	 * This getattr is performed on behalf of the server
2975331Samw 	 * that's why kcred is used not the user's cred
2985331Samw 	 */
29910001SJoyce.McIntosh@Sun.COM 	attr.sa_mask = SMB_AT_ALL;
30010001SJoyce.McIntosh@Sun.COM 	error = smb_vop_getattr(vp, unnamed_vp, &attr, 0, kcred);
3015331Samw 	if (error)
3025331Samw 		return (NULL);
3035331Samw 
3047348SJose.Borrego@Sun.COM 	if (sr && sr->tid_tree) {
3057348SJose.Borrego@Sun.COM 		/*
3067348SJose.Borrego@Sun.COM 		 * The fsid for a file is that of the tree, even
3077348SJose.Borrego@Sun.COM 		 * if the file resides in a different mountpoint
3087348SJose.Borrego@Sun.COM 		 * under the share.
3097348SJose.Borrego@Sun.COM 		 */
3107348SJose.Borrego@Sun.COM 		fsid = SMB_TREE_FSID(sr->tid_tree);
3115331Samw 	} else {
3127348SJose.Borrego@Sun.COM 		/*
3137348SJose.Borrego@Sun.COM 		 * This should be getting executed only for the
3147348SJose.Borrego@Sun.COM 		 * tree root smb_node.
3157348SJose.Borrego@Sun.COM 		 */
3167348SJose.Borrego@Sun.COM 		fsid = vp->v_vfsp->vfs_fsid;
3175331Samw 	}
3185331Samw 
31910001SJoyce.McIntosh@Sun.COM 	node_hdr = smb_node_get_hash(&fsid, &attr, &hashkey);
3205331Samw 	lock_mode = RW_READER;
3215331Samw 
3225331Samw 	smb_llist_enter(node_hdr, lock_mode);
3235331Samw 	for (;;) {
3245331Samw 		node = list_head(&node_hdr->ll_list);
3255331Samw 		while (node) {
3265331Samw 			ASSERT(node->n_magic == SMB_NODE_MAGIC);
3275331Samw 			ASSERT(node->n_hash_bucket == node_hdr);
3285331Samw 			if ((node->n_hashkey == hashkey) && (node->vp == vp)) {
3298934SJose.Borrego@Sun.COM 				mutex_enter(&node->n_mutex);
3305331Samw 				DTRACE_PROBE1(smb_node_lookup_hit,
3315331Samw 				    smb_node_t *, node);
3325331Samw 				switch (node->n_state) {
3338934SJose.Borrego@Sun.COM 				case SMB_NODE_STATE_OPLOCK_GRANTED:
3348934SJose.Borrego@Sun.COM 				case SMB_NODE_STATE_OPLOCK_BREAKING:
3355331Samw 				case SMB_NODE_STATE_AVAILABLE:
3365331Samw 					/* The node was found. */
3375331Samw 					node->n_refcnt++;
33810122SJordan.Brown@Sun.COM 					if ((node->n_dnode == NULL) &&
33910122SJordan.Brown@Sun.COM 					    (dnode != NULL) &&
3405331Samw 					    (strcmp(od_name, "..") != 0) &&
3415331Samw 					    (strcmp(od_name, ".") != 0)) {
34210122SJordan.Brown@Sun.COM 						VALIDATE_DIR_NODE(dnode, node);
34310122SJordan.Brown@Sun.COM 						node->n_dnode = dnode;
34410122SJordan.Brown@Sun.COM 						smb_node_ref(dnode);
3455331Samw 					}
3465331Samw 
3478934SJose.Borrego@Sun.COM 					smb_node_audit(node);
3488934SJose.Borrego@Sun.COM 					mutex_exit(&node->n_mutex);
3495331Samw 					smb_llist_exit(node_hdr);
3505331Samw 					return (node);
3515331Samw 
3525331Samw 				case SMB_NODE_STATE_DESTROYING:
3535331Samw 					/*
3545331Samw 					 * Although the node exists it is about
3555331Samw 					 * to be destroyed. We act as it hasn't
3565331Samw 					 * been found.
3575331Samw 					 */
3588934SJose.Borrego@Sun.COM 					mutex_exit(&node->n_mutex);
3595331Samw 					break;
3605331Samw 				default:
3615331Samw 					/*
3625331Samw 					 * Although the node exists it is in an
3635331Samw 					 * unknown state. We act as it hasn't
3645331Samw 					 * been found.
3655331Samw 					 */
3665331Samw 					ASSERT(0);
3678934SJose.Borrego@Sun.COM 					mutex_exit(&node->n_mutex);
3685331Samw 					break;
3695331Samw 				}
3705331Samw 			}
3715331Samw 			node = smb_llist_next(node_hdr, node);
3725331Samw 		}
3735331Samw 		if ((lock_mode == RW_READER) && smb_llist_upgrade(node_hdr)) {
3745331Samw 			lock_mode = RW_WRITER;
3755331Samw 			continue;
3765331Samw 		}
3775331Samw 		break;
3785331Samw 	}
37910001SJoyce.McIntosh@Sun.COM 	node = smb_node_alloc(od_name, vp, node_hdr, hashkey);
3808934SJose.Borrego@Sun.COM 	node->n_orig_uid = crgetuid(sr->user_cr);
3815331Samw 
3825331Samw 	if (op)
3839343SAfshin.Ardakani@Sun.COM 		node->flags |= smb_is_executable(op->fqi.fq_last_comp);
3845331Samw 
38510122SJordan.Brown@Sun.COM 	if (dnode) {
38610122SJordan.Brown@Sun.COM 		smb_node_ref(dnode);
38710122SJordan.Brown@Sun.COM 		node->n_dnode = dnode;
38810122SJordan.Brown@Sun.COM 		ASSERT(dnode->n_dnode != node);
38910122SJordan.Brown@Sun.COM 		ASSERT((dnode->vp->v_xattrdir) ||
39010122SJordan.Brown@Sun.COM 		    (dnode->vp->v_type == VDIR));
3915331Samw 	}
3925331Samw 
39310122SJordan.Brown@Sun.COM 	if (unode) {
39410122SJordan.Brown@Sun.COM 		smb_node_ref(unode);
39510122SJordan.Brown@Sun.COM 		node->n_unode = unode;
3965331Samw 	}
3975331Samw 
3985331Samw 	DTRACE_PROBE1(smb_node_lookup_miss, smb_node_t *, node);
3998934SJose.Borrego@Sun.COM 	smb_node_audit(node);
4005331Samw 	smb_llist_insert_head(node_hdr, node);
4015331Samw 	smb_llist_exit(node_hdr);
4025331Samw 	return (node);
4035331Samw }
4045331Samw 
4055331Samw /*
4065331Samw  * smb_stream_node_lookup()
4075331Samw  *
4085331Samw  * Note: stream_name (the name that will be stored in the "od_name" field
4095331Samw  * of a stream's smb_node) is the same as the on-disk name for the stream
4105331Samw  * except that it does not have SMB_STREAM_PREFIX prepended.
4115331Samw  */
4125331Samw 
4135331Samw smb_node_t *
4148934SJose.Borrego@Sun.COM smb_stream_node_lookup(smb_request_t *sr, cred_t *cr, smb_node_t *fnode,
41510001SJoyce.McIntosh@Sun.COM     vnode_t *xattrdirvp, vnode_t *vp, char *stream_name)
4165331Samw {
4175331Samw 	smb_node_t	*xattrdir_node;
4185331Samw 	smb_node_t	*snode;
4195331Samw 
4205331Samw 	xattrdir_node = smb_node_lookup(sr, NULL, cr, xattrdirvp, XATTR_DIR,
42110001SJoyce.McIntosh@Sun.COM 	    fnode, NULL);
4225331Samw 
4235331Samw 	if (xattrdir_node == NULL)
4245331Samw 		return (NULL);
4255331Samw 
4265331Samw 	snode = smb_node_lookup(sr, NULL, cr, vp, stream_name, xattrdir_node,
42710001SJoyce.McIntosh@Sun.COM 	    fnode);
4285331Samw 
4295331Samw 	(void) smb_node_release(xattrdir_node);
4305331Samw 	return (snode);
4315331Samw }
4325331Samw 
4335331Samw 
4345331Samw /*
4355331Samw  * This function should be called whenever a reference is needed on an
4365331Samw  * smb_node pointer.  The copy of an smb_node pointer from one non-local
4375331Samw  * data structure to another requires a reference to be taken on the smb_node
4385331Samw  * (unless the usage is localized).  Each data structure deallocation routine
4395331Samw  * will call smb_node_release() on its smb_node pointers.
4405331Samw  *
4415331Samw  * In general, an smb_node pointer residing in a structure should never be
4425331Samw  * stale.  A node pointer may be NULL, however, and care should be taken
4435331Samw  * prior to calling smb_node_ref(), which ASSERTs that the pointer is valid.
4445331Samw  * Care also needs to be taken with respect to racing deallocations of a
4455331Samw  * structure.
4465331Samw  */
4475331Samw void
4485331Samw smb_node_ref(smb_node_t *node)
4495331Samw {
4508934SJose.Borrego@Sun.COM 	SMB_NODE_VALID(node);
4515331Samw 
4528934SJose.Borrego@Sun.COM 	mutex_enter(&node->n_mutex);
4538934SJose.Borrego@Sun.COM 	switch (node->n_state) {
4548934SJose.Borrego@Sun.COM 	case SMB_NODE_STATE_AVAILABLE:
4558934SJose.Borrego@Sun.COM 	case SMB_NODE_STATE_OPLOCK_GRANTED:
4568934SJose.Borrego@Sun.COM 	case SMB_NODE_STATE_OPLOCK_BREAKING:
4578934SJose.Borrego@Sun.COM 		node->n_refcnt++;
4588934SJose.Borrego@Sun.COM 		ASSERT(node->n_refcnt);
4598934SJose.Borrego@Sun.COM 		DTRACE_PROBE1(smb_node_ref_exit, smb_node_t *, node);
4608934SJose.Borrego@Sun.COM 		smb_node_audit(node);
4618934SJose.Borrego@Sun.COM 		break;
4628934SJose.Borrego@Sun.COM 	default:
4638934SJose.Borrego@Sun.COM 		SMB_PANIC();
4648934SJose.Borrego@Sun.COM 	}
4658934SJose.Borrego@Sun.COM 	mutex_exit(&node->n_mutex);
4665331Samw }
4675331Samw 
4685331Samw /*
4695331Samw  * smb_node_lookup() takes a hold on an smb_node, whether found in the
4705331Samw  * hash table or newly created.  This hold is expected to be released
4715331Samw  * in the following manner.
4725331Samw  *
4735331Samw  * smb_node_lookup() takes an address of an smb_node pointer.  This should
4745331Samw  * be getting passed down via a lookup (whether path name or component), mkdir,
4755331Samw  * create.  If the original smb_node pointer resides in a data structure, then
4765331Samw  * the deallocation routine for the data structure is responsible for calling
4775331Samw  * smb_node_release() on the smb_node pointer.  Alternatively,
4785331Samw  * smb_node_release() can be called as soon as the smb_node pointer is no longer
4795331Samw  * needed.  In this case, callers are responsible for setting an embedded
4805331Samw  * pointer to NULL if it is known that the last reference is being released.
4815331Samw  *
4825331Samw  * If the passed-in address of the smb_node pointer belongs to a local variable,
4835331Samw  * then the caller with the local variable should call smb_node_release()
4845331Samw  * directly.
4855331Samw  *
48610122SJordan.Brown@Sun.COM  * smb_node_release() itself will call smb_node_release() on a node's n_dnode,
48710122SJordan.Brown@Sun.COM  * as smb_node_lookup() takes a hold on dnode.
4885331Samw  */
4895331Samw void
4905331Samw smb_node_release(smb_node_t *node)
4915331Samw {
4928934SJose.Borrego@Sun.COM 	SMB_NODE_VALID(node);
4935331Samw 
4948934SJose.Borrego@Sun.COM 	mutex_enter(&node->n_mutex);
4955331Samw 	ASSERT(node->n_refcnt);
4965331Samw 	DTRACE_PROBE1(smb_node_release, smb_node_t *, node);
4975331Samw 	if (--node->n_refcnt == 0) {
4985331Samw 		switch (node->n_state) {
4995331Samw 
5005331Samw 		case SMB_NODE_STATE_AVAILABLE:
5015331Samw 			node->n_state = SMB_NODE_STATE_DESTROYING;
5028934SJose.Borrego@Sun.COM 			mutex_exit(&node->n_mutex);
5035331Samw 
5045331Samw 			smb_llist_enter(node->n_hash_bucket, RW_WRITER);
5055331Samw 			smb_llist_remove(node->n_hash_bucket, node);
5065331Samw 			smb_llist_exit(node->n_hash_bucket);
5075331Samw 
5085331Samw 			/*
5095331Samw 			 * Check if the file was deleted
5105331Samw 			 */
5115331Samw 			smb_node_delete_on_close(node);
5125331Samw 
51310122SJordan.Brown@Sun.COM 			if (node->n_dnode) {
51410122SJordan.Brown@Sun.COM 				ASSERT(node->n_dnode->n_magic ==
5155331Samw 				    SMB_NODE_MAGIC);
51610122SJordan.Brown@Sun.COM 				smb_node_release(node->n_dnode);
5175331Samw 			}
5185331Samw 
51910122SJordan.Brown@Sun.COM 			if (node->n_unode) {
52010122SJordan.Brown@Sun.COM 				ASSERT(node->n_unode->n_magic ==
5215331Samw 				    SMB_NODE_MAGIC);
52210122SJordan.Brown@Sun.COM 				smb_node_release(node->n_unode);
5235331Samw 			}
5245331Samw 
5258934SJose.Borrego@Sun.COM 			smb_node_free(node);
5265331Samw 			return;
5275331Samw 
5285331Samw 		default:
5298934SJose.Borrego@Sun.COM 			SMB_PANIC();
5305331Samw 		}
5315331Samw 	}
5328934SJose.Borrego@Sun.COM 	smb_node_audit(node);
5338934SJose.Borrego@Sun.COM 	mutex_exit(&node->n_mutex);
5345331Samw }
5355331Samw 
5365331Samw static void
5375331Samw smb_node_delete_on_close(smb_node_t *node)
5385331Samw {
5395331Samw 	smb_node_t	*d_snode;
5405331Samw 	int		rc = 0;
5419231SAfshin.Ardakani@Sun.COM 	uint32_t	flags = 0;
5425331Samw 
54310122SJordan.Brown@Sun.COM 	d_snode = node->n_dnode;
5445331Samw 	if (node->flags & NODE_FLAGS_DELETE_ON_CLOSE) {
5459231SAfshin.Ardakani@Sun.COM 		node->flags &= ~NODE_FLAGS_DELETE_ON_CLOSE;
5469231SAfshin.Ardakani@Sun.COM 		flags = node->n_delete_on_close_flags;
5479231SAfshin.Ardakani@Sun.COM 		ASSERT(node->od_name != NULL);
5485331Samw 
54910001SJoyce.McIntosh@Sun.COM 		if (node->vp->v_type == VDIR)
5505331Samw 			rc = smb_fsop_rmdir(0, node->delete_on_close_cred,
5519231SAfshin.Ardakani@Sun.COM 			    d_snode, node->od_name, flags);
5525331Samw 		else
5535331Samw 			rc = smb_fsop_remove(0, node->delete_on_close_cred,
5549231SAfshin.Ardakani@Sun.COM 			    d_snode, node->od_name, flags);
5555331Samw 		smb_cred_rele(node->delete_on_close_cred);
5565331Samw 	}
5575331Samw 	if (rc != 0)
5585331Samw 		cmn_err(CE_WARN, "File %s could not be removed, rc=%d\n",
5595331Samw 		    node->od_name, rc);
5605331Samw 	DTRACE_PROBE2(smb_node_delete_on_close, int, rc, smb_node_t *, node);
5615331Samw }
5625331Samw 
5635331Samw /*
5645331Samw  * smb_node_rename()
5655331Samw  *
5665331Samw  */
5678934SJose.Borrego@Sun.COM void
5685331Samw smb_node_rename(
5698934SJose.Borrego@Sun.COM     smb_node_t	*from_dnode,
5708934SJose.Borrego@Sun.COM     smb_node_t	*ret_node,
5718934SJose.Borrego@Sun.COM     smb_node_t	*to_dnode,
5725331Samw     char	*to_name)
5735331Samw {
5748934SJose.Borrego@Sun.COM 	SMB_NODE_VALID(from_dnode);
5758934SJose.Borrego@Sun.COM 	SMB_NODE_VALID(to_dnode);
5768934SJose.Borrego@Sun.COM 	SMB_NODE_VALID(ret_node);
5775331Samw 
5788934SJose.Borrego@Sun.COM 	smb_node_ref(to_dnode);
5798934SJose.Borrego@Sun.COM 	mutex_enter(&ret_node->n_mutex);
5808934SJose.Borrego@Sun.COM 	switch (ret_node->n_state) {
5818934SJose.Borrego@Sun.COM 	case SMB_NODE_STATE_AVAILABLE:
5828934SJose.Borrego@Sun.COM 	case SMB_NODE_STATE_OPLOCK_GRANTED:
5838934SJose.Borrego@Sun.COM 	case SMB_NODE_STATE_OPLOCK_BREAKING:
58410122SJordan.Brown@Sun.COM 		ret_node->n_dnode = to_dnode;
5858934SJose.Borrego@Sun.COM 		mutex_exit(&ret_node->n_mutex);
58610122SJordan.Brown@Sun.COM 		ASSERT(to_dnode->n_dnode != ret_node);
5878934SJose.Borrego@Sun.COM 		ASSERT((to_dnode->vp->v_xattrdir) ||
5888934SJose.Borrego@Sun.COM 		    (to_dnode->vp->v_type == VDIR));
5898934SJose.Borrego@Sun.COM 		smb_node_release(from_dnode);
5908934SJose.Borrego@Sun.COM 		(void) strcpy(ret_node->od_name, to_name);
5918934SJose.Borrego@Sun.COM 		/*
5928934SJose.Borrego@Sun.COM 		 * XXX Need to update attributes?
5938934SJose.Borrego@Sun.COM 		 */
5948934SJose.Borrego@Sun.COM 		break;
5958934SJose.Borrego@Sun.COM 	default:
5968934SJose.Borrego@Sun.COM 		SMB_PANIC();
5978934SJose.Borrego@Sun.COM 	}
5985331Samw }
5995331Samw 
6005331Samw int
6016139Sjb150015 smb_node_root_init(vnode_t *vp, smb_server_t *sv, smb_node_t **root)
6025331Samw {
60310001SJoyce.McIntosh@Sun.COM 	smb_attr_t	attr;
6046139Sjb150015 	int		error;
6056139Sjb150015 	uint32_t	hashkey;
6066139Sjb150015 	smb_llist_t	*node_hdr;
6076139Sjb150015 	smb_node_t	*node;
6085331Samw 
60910001SJoyce.McIntosh@Sun.COM 	attr.sa_mask = SMB_AT_ALL;
61010001SJoyce.McIntosh@Sun.COM 	error = smb_vop_getattr(vp, NULL, &attr, 0, kcred);
6116139Sjb150015 	if (error) {
6126139Sjb150015 		VN_RELE(vp);
6136139Sjb150015 		return (error);
6146139Sjb150015 	}
6156139Sjb150015 
61610001SJoyce.McIntosh@Sun.COM 	node_hdr = smb_node_get_hash(&vp->v_vfsp->vfs_fsid, &attr, &hashkey);
6175331Samw 
61810001SJoyce.McIntosh@Sun.COM 	node = smb_node_alloc(ROOTVOL, vp, node_hdr, hashkey);
6196139Sjb150015 
6206139Sjb150015 	sv->si_root_smb_node = node;
6218934SJose.Borrego@Sun.COM 	smb_node_audit(node);
6226139Sjb150015 	smb_llist_enter(node_hdr, RW_WRITER);
6236139Sjb150015 	smb_llist_insert_head(node_hdr, node);
6246139Sjb150015 	smb_llist_exit(node_hdr);
6256139Sjb150015 	*root = node;
6266139Sjb150015 	return (0);
6275331Samw }
6285331Samw 
6295331Samw /*
6309231SAfshin.Ardakani@Sun.COM  * When DeleteOnClose is set on an smb_node, the common open code will
6319231SAfshin.Ardakani@Sun.COM  * reject subsequent open requests for the file. Observation of Windows
6329231SAfshin.Ardakani@Sun.COM  * 2000 indicates that subsequent opens should be allowed (assuming
6339231SAfshin.Ardakani@Sun.COM  * there would be no sharing violation) until the file is closed using
6349231SAfshin.Ardakani@Sun.COM  * the fid on which the DeleteOnClose was requested.
6359231SAfshin.Ardakani@Sun.COM  *
6369231SAfshin.Ardakani@Sun.COM  * If there are multiple opens with delete-on-close create options,
6379231SAfshin.Ardakani@Sun.COM  * whichever the first file handle is closed will trigger the node to be
6389231SAfshin.Ardakani@Sun.COM  * marked as delete-on-close. The credentials of that ofile will be used
6399231SAfshin.Ardakani@Sun.COM  * as the delete-on-close credentials of the node.
6409231SAfshin.Ardakani@Sun.COM  */
6415331Samw int
6429231SAfshin.Ardakani@Sun.COM smb_node_set_delete_on_close(smb_node_t *node, cred_t *cr, uint32_t flags)
6435331Samw {
64410001SJoyce.McIntosh@Sun.COM 	int rc;
64510001SJoyce.McIntosh@Sun.COM 	smb_attr_t attr;
6465331Samw 
6478934SJose.Borrego@Sun.COM 	mutex_enter(&node->n_mutex);
64810001SJoyce.McIntosh@Sun.COM 
64910001SJoyce.McIntosh@Sun.COM 	if ((node->flags & NODE_FLAGS_DELETE_ON_CLOSE) ||
65010001SJoyce.McIntosh@Sun.COM 	    (node->readonly_creator)) {
65110001SJoyce.McIntosh@Sun.COM 		mutex_exit(&node->n_mutex);
65210001SJoyce.McIntosh@Sun.COM 		return (-1);
6535331Samw 	}
65410001SJoyce.McIntosh@Sun.COM 
65510001SJoyce.McIntosh@Sun.COM 	bzero(&attr, sizeof (smb_attr_t));
65610001SJoyce.McIntosh@Sun.COM 	attr.sa_mask = SMB_AT_DOSATTR;
65710001SJoyce.McIntosh@Sun.COM 	rc = smb_fsop_getattr(NULL, kcred, node, &attr);
65810001SJoyce.McIntosh@Sun.COM 	if ((rc != 0) || (attr.sa_dosattr & FILE_ATTRIBUTE_READONLY)) {
65910001SJoyce.McIntosh@Sun.COM 		mutex_exit(&node->n_mutex);
66010001SJoyce.McIntosh@Sun.COM 		return (-1);
66110001SJoyce.McIntosh@Sun.COM 	}
66210001SJoyce.McIntosh@Sun.COM 
66310001SJoyce.McIntosh@Sun.COM 	crhold(cr);
66410001SJoyce.McIntosh@Sun.COM 	node->delete_on_close_cred = cr;
66510001SJoyce.McIntosh@Sun.COM 	node->n_delete_on_close_flags = flags;
66610001SJoyce.McIntosh@Sun.COM 	node->flags |= NODE_FLAGS_DELETE_ON_CLOSE;
6678934SJose.Borrego@Sun.COM 	mutex_exit(&node->n_mutex);
66810001SJoyce.McIntosh@Sun.COM 	return (0);
6695331Samw }
6705331Samw 
6715331Samw void
6725331Samw smb_node_reset_delete_on_close(smb_node_t *node)
6735331Samw {
6748934SJose.Borrego@Sun.COM 	mutex_enter(&node->n_mutex);
6755331Samw 	if (node->flags & NODE_FLAGS_DELETE_ON_CLOSE) {
6765331Samw 		node->flags &= ~NODE_FLAGS_DELETE_ON_CLOSE;
6775331Samw 		crfree(node->delete_on_close_cred);
6785331Samw 		node->delete_on_close_cred = NULL;
6799231SAfshin.Ardakani@Sun.COM 		node->n_delete_on_close_flags = 0;
6805331Samw 	}
6818934SJose.Borrego@Sun.COM 	mutex_exit(&node->n_mutex);
6825331Samw }
6835772Sas200622 
6845772Sas200622 /*
6856771Sjb150015  * smb_node_open_check
6865772Sas200622  *
6875772Sas200622  * check file sharing rules for current open request
6885772Sas200622  * against all existing opens for a file.
6895772Sas200622  *
6905772Sas200622  * Returns NT_STATUS_SHARING_VIOLATION if there is any
6915772Sas200622  * sharing conflict, otherwise returns NT_STATUS_SUCCESS.
6925772Sas200622  */
6935772Sas200622 uint32_t
6948934SJose.Borrego@Sun.COM smb_node_open_check(
6958934SJose.Borrego@Sun.COM     smb_node_t	*node,
6968934SJose.Borrego@Sun.COM     cred_t	*cr,
6978934SJose.Borrego@Sun.COM     uint32_t	desired_access,
6988934SJose.Borrego@Sun.COM     uint32_t	share_access)
6995772Sas200622 {
7005772Sas200622 	smb_ofile_t *of;
7015772Sas200622 	uint32_t status;
7025772Sas200622 
7038934SJose.Borrego@Sun.COM 	SMB_NODE_VALID(node);
7045772Sas200622 
7055772Sas200622 	smb_llist_enter(&node->n_ofile_list, RW_READER);
7065772Sas200622 	of = smb_llist_head(&node->n_ofile_list);
7075772Sas200622 	while (of) {
7086771Sjb150015 		status = smb_ofile_open_check(of, cr, desired_access,
7096771Sjb150015 		    share_access);
7106771Sjb150015 
7116771Sjb150015 		switch (status) {
7126771Sjb150015 		case NT_STATUS_INVALID_HANDLE:
7136771Sjb150015 		case NT_STATUS_SUCCESS:
7146771Sjb150015 			of = smb_llist_next(&node->n_ofile_list, of);
7156771Sjb150015 			break;
7166771Sjb150015 		default:
7176771Sjb150015 			ASSERT(status == NT_STATUS_SHARING_VIOLATION);
7185772Sas200622 			smb_llist_exit(&node->n_ofile_list);
7195772Sas200622 			return (status);
7205772Sas200622 		}
7215772Sas200622 	}
7226771Sjb150015 
7235772Sas200622 	smb_llist_exit(&node->n_ofile_list);
7245772Sas200622 	return (NT_STATUS_SUCCESS);
7255772Sas200622 }
7265772Sas200622 
7275772Sas200622 uint32_t
7288934SJose.Borrego@Sun.COM smb_node_rename_check(smb_node_t *node)
7295772Sas200622 {
7308934SJose.Borrego@Sun.COM 	smb_ofile_t	*of;
7318934SJose.Borrego@Sun.COM 	uint32_t	status;
7325772Sas200622 
7338934SJose.Borrego@Sun.COM 	SMB_NODE_VALID(node);
7345772Sas200622 
7355772Sas200622 	/*
7365772Sas200622 	 * Intra-CIFS check
7375772Sas200622 	 */
7385772Sas200622 	smb_llist_enter(&node->n_ofile_list, RW_READER);
7396771Sjb150015 	of = smb_llist_head(&node->n_ofile_list);
7406771Sjb150015 	while (of) {
7416771Sjb150015 		status = smb_ofile_rename_check(of);
7425772Sas200622 
7436771Sjb150015 		switch (status) {
7446771Sjb150015 		case NT_STATUS_INVALID_HANDLE:
7456771Sjb150015 		case NT_STATUS_SUCCESS:
7466771Sjb150015 			of = smb_llist_next(&node->n_ofile_list, of);
7476771Sjb150015 			break;
7486771Sjb150015 		default:
7496771Sjb150015 			ASSERT(status == NT_STATUS_SHARING_VIOLATION);
7506771Sjb150015 			smb_llist_exit(&node->n_ofile_list);
7516771Sjb150015 			return (status);
7525772Sas200622 		}
7535772Sas200622 	}
7545772Sas200622 	smb_llist_exit(&node->n_ofile_list);
7555772Sas200622 
7565772Sas200622 	/*
7575772Sas200622 	 * system-wide share check
7585772Sas200622 	 */
7595772Sas200622 	if (nbl_share_conflict(node->vp, NBL_RENAME, NULL))
7605772Sas200622 		return (NT_STATUS_SHARING_VIOLATION);
7615772Sas200622 	else
7625772Sas200622 		return (NT_STATUS_SUCCESS);
7635772Sas200622 }
7645772Sas200622 
7656771Sjb150015 uint32_t
7665772Sas200622 smb_node_delete_check(smb_node_t *node)
7675772Sas200622 {
7688934SJose.Borrego@Sun.COM 	smb_ofile_t	*of;
7698934SJose.Borrego@Sun.COM 	uint32_t	status;
7705772Sas200622 
7718934SJose.Borrego@Sun.COM 	SMB_NODE_VALID(node);
7725772Sas200622 
77310001SJoyce.McIntosh@Sun.COM 	if (node->vp->v_type == VDIR)
7745772Sas200622 		return (NT_STATUS_SUCCESS);
7755772Sas200622 
7765772Sas200622 	/*
7775772Sas200622 	 * intra-CIFS check
7785772Sas200622 	 */
7795772Sas200622 	smb_llist_enter(&node->n_ofile_list, RW_READER);
7806771Sjb150015 	of = smb_llist_head(&node->n_ofile_list);
7816771Sjb150015 	while (of) {
7826771Sjb150015 		status = smb_ofile_delete_check(of);
7836771Sjb150015 
7846771Sjb150015 		switch (status) {
7856771Sjb150015 		case NT_STATUS_INVALID_HANDLE:
7866771Sjb150015 		case NT_STATUS_SUCCESS:
7876771Sjb150015 			of = smb_llist_next(&node->n_ofile_list, of);
7886771Sjb150015 			break;
7896771Sjb150015 		default:
7906771Sjb150015 			ASSERT(status == NT_STATUS_SHARING_VIOLATION);
7915772Sas200622 			smb_llist_exit(&node->n_ofile_list);
7926771Sjb150015 			return (status);
7935772Sas200622 		}
7945772Sas200622 	}
7955772Sas200622 	smb_llist_exit(&node->n_ofile_list);
7965772Sas200622 
7975772Sas200622 	/*
7985772Sas200622 	 * system-wide share check
7995772Sas200622 	 */
8005772Sas200622 	if (nbl_share_conflict(node->vp, NBL_REMOVE, NULL))
8015772Sas200622 		return (NT_STATUS_SHARING_VIOLATION);
8025772Sas200622 	else
8035772Sas200622 		return (NT_STATUS_SUCCESS);
8045772Sas200622 }
8055772Sas200622 
8069914Samw@Sun.COM void
8079914Samw@Sun.COM smb_node_notify_change(smb_node_t *node)
8089914Samw@Sun.COM {
8099914Samw@Sun.COM 	SMB_NODE_VALID(node);
8109914Samw@Sun.COM 
8119914Samw@Sun.COM 	if (node->flags & NODE_FLAGS_NOTIFY_CHANGE) {
8129914Samw@Sun.COM 		node->flags |= NODE_FLAGS_CHANGED;
8139914Samw@Sun.COM 		smb_process_node_notify_change_queue(node);
8149914Samw@Sun.COM 	}
8159914Samw@Sun.COM }
8169914Samw@Sun.COM 
8175772Sas200622 /*
8185772Sas200622  * smb_node_start_crit()
8195772Sas200622  *
8205772Sas200622  * Enter critical region for share reservations.
8215772Sas200622  * See comments above smb_fsop_shrlock().
8225772Sas200622  */
8235772Sas200622 
8245772Sas200622 void
8255772Sas200622 smb_node_start_crit(smb_node_t *node, krw_t mode)
8265772Sas200622 {
8278934SJose.Borrego@Sun.COM 	rw_enter(&node->n_lock, mode);
8285772Sas200622 	nbl_start_crit(node->vp, mode);
8295772Sas200622 }
8305772Sas200622 
8315772Sas200622 /*
8325772Sas200622  * smb_node_end_crit()
8335772Sas200622  *
8345772Sas200622  * Exit critical region for share reservations.
8355772Sas200622  */
8365772Sas200622 
8375772Sas200622 void
8385772Sas200622 smb_node_end_crit(smb_node_t *node)
8395772Sas200622 {
8405772Sas200622 	nbl_end_crit(node->vp);
8418934SJose.Borrego@Sun.COM 	rw_exit(&node->n_lock);
8425772Sas200622 }
8435772Sas200622 
8445772Sas200622 int
8455772Sas200622 smb_node_in_crit(smb_node_t *node)
8465772Sas200622 {
8478934SJose.Borrego@Sun.COM 	return (nbl_in_crit(node->vp) && RW_LOCK_HELD(&node->n_lock));
8488934SJose.Borrego@Sun.COM }
8498934SJose.Borrego@Sun.COM 
8508934SJose.Borrego@Sun.COM void
8518934SJose.Borrego@Sun.COM smb_node_rdlock(smb_node_t *node)
8528934SJose.Borrego@Sun.COM {
8538934SJose.Borrego@Sun.COM 	rw_enter(&node->n_lock, RW_READER);
8548934SJose.Borrego@Sun.COM }
8558934SJose.Borrego@Sun.COM 
8568934SJose.Borrego@Sun.COM void
8578934SJose.Borrego@Sun.COM smb_node_wrlock(smb_node_t *node)
8588934SJose.Borrego@Sun.COM {
8598934SJose.Borrego@Sun.COM 	rw_enter(&node->n_lock, RW_WRITER);
8608934SJose.Borrego@Sun.COM }
8618934SJose.Borrego@Sun.COM 
8628934SJose.Borrego@Sun.COM void
8638934SJose.Borrego@Sun.COM smb_node_unlock(smb_node_t *node)
8648934SJose.Borrego@Sun.COM {
8658934SJose.Borrego@Sun.COM 	rw_exit(&node->n_lock);
8668934SJose.Borrego@Sun.COM }
8678934SJose.Borrego@Sun.COM 
8688934SJose.Borrego@Sun.COM uint32_t
8698934SJose.Borrego@Sun.COM smb_node_get_ofile_count(smb_node_t *node)
8708934SJose.Borrego@Sun.COM {
8718934SJose.Borrego@Sun.COM 	uint32_t	cntr;
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_READER);
8768934SJose.Borrego@Sun.COM 	cntr = smb_llist_get_count(&node->n_ofile_list);
8778934SJose.Borrego@Sun.COM 	smb_llist_exit(&node->n_ofile_list);
8788934SJose.Borrego@Sun.COM 	return (cntr);
8798934SJose.Borrego@Sun.COM }
8808934SJose.Borrego@Sun.COM 
8818934SJose.Borrego@Sun.COM void
8828934SJose.Borrego@Sun.COM smb_node_add_ofile(smb_node_t *node, smb_ofile_t *of)
8838934SJose.Borrego@Sun.COM {
8848934SJose.Borrego@Sun.COM 	SMB_NODE_VALID(node);
8858934SJose.Borrego@Sun.COM 
8868934SJose.Borrego@Sun.COM 	smb_llist_enter(&node->n_ofile_list, RW_WRITER);
8878934SJose.Borrego@Sun.COM 	smb_llist_insert_tail(&node->n_ofile_list, of);
8888934SJose.Borrego@Sun.COM 	smb_llist_exit(&node->n_ofile_list);
8898934SJose.Borrego@Sun.COM }
8908934SJose.Borrego@Sun.COM 
8918934SJose.Borrego@Sun.COM void
8928934SJose.Borrego@Sun.COM smb_node_rem_ofile(smb_node_t *node, smb_ofile_t *of)
8938934SJose.Borrego@Sun.COM {
8948934SJose.Borrego@Sun.COM 	SMB_NODE_VALID(node);
8958934SJose.Borrego@Sun.COM 
8968934SJose.Borrego@Sun.COM 	smb_llist_enter(&node->n_ofile_list, RW_WRITER);
8978934SJose.Borrego@Sun.COM 	smb_llist_remove(&node->n_ofile_list, of);
8988934SJose.Borrego@Sun.COM 	smb_llist_exit(&node->n_ofile_list);
8998934SJose.Borrego@Sun.COM }
9008934SJose.Borrego@Sun.COM 
90110001SJoyce.McIntosh@Sun.COM /*
90210001SJoyce.McIntosh@Sun.COM  * smb_node_inc_open_ofiles
90310001SJoyce.McIntosh@Sun.COM  */
9048934SJose.Borrego@Sun.COM void
9058934SJose.Borrego@Sun.COM smb_node_inc_open_ofiles(smb_node_t *node)
9068934SJose.Borrego@Sun.COM {
9078934SJose.Borrego@Sun.COM 	SMB_NODE_VALID(node);
9088934SJose.Borrego@Sun.COM 
9098934SJose.Borrego@Sun.COM 	mutex_enter(&node->n_mutex);
9108934SJose.Borrego@Sun.COM 	node->n_open_count++;
9118934SJose.Borrego@Sun.COM 	mutex_exit(&node->n_mutex);
91210001SJoyce.McIntosh@Sun.COM 
913*10504SKeyur.Desai@Sun.COM 	smb_node_init_cached_data(node);
9148934SJose.Borrego@Sun.COM }
9158934SJose.Borrego@Sun.COM 
91610001SJoyce.McIntosh@Sun.COM /*
91710001SJoyce.McIntosh@Sun.COM  * smb_node_dec_open_ofiles
91810001SJoyce.McIntosh@Sun.COM  */
9198934SJose.Borrego@Sun.COM void
9208934SJose.Borrego@Sun.COM smb_node_dec_open_ofiles(smb_node_t *node)
9218934SJose.Borrego@Sun.COM {
9228934SJose.Borrego@Sun.COM 	SMB_NODE_VALID(node);
9238934SJose.Borrego@Sun.COM 
9248934SJose.Borrego@Sun.COM 	mutex_enter(&node->n_mutex);
9258934SJose.Borrego@Sun.COM 	node->n_open_count--;
9268934SJose.Borrego@Sun.COM 	mutex_exit(&node->n_mutex);
92710001SJoyce.McIntosh@Sun.COM 
928*10504SKeyur.Desai@Sun.COM 	smb_node_clear_cached_data(node);
9298934SJose.Borrego@Sun.COM }
9308934SJose.Borrego@Sun.COM 
9318934SJose.Borrego@Sun.COM uint32_t
9328934SJose.Borrego@Sun.COM smb_node_get_open_ofiles(smb_node_t *node)
9338934SJose.Borrego@Sun.COM {
9348934SJose.Borrego@Sun.COM 	uint32_t	cnt;
9358934SJose.Borrego@Sun.COM 
9368934SJose.Borrego@Sun.COM 	SMB_NODE_VALID(node);
9378934SJose.Borrego@Sun.COM 
9388934SJose.Borrego@Sun.COM 	mutex_enter(&node->n_mutex);
9398934SJose.Borrego@Sun.COM 	cnt = node->n_open_count;
9408934SJose.Borrego@Sun.COM 	mutex_exit(&node->n_mutex);
9418934SJose.Borrego@Sun.COM 	return (cnt);
9425772Sas200622 }
9438934SJose.Borrego@Sun.COM 
9448934SJose.Borrego@Sun.COM /*
9458934SJose.Borrego@Sun.COM  * smb_node_alloc
9468934SJose.Borrego@Sun.COM  */
9478934SJose.Borrego@Sun.COM static smb_node_t *
9488934SJose.Borrego@Sun.COM smb_node_alloc(
9498934SJose.Borrego@Sun.COM     char	*od_name,
9508934SJose.Borrego@Sun.COM     vnode_t	*vp,
9518934SJose.Borrego@Sun.COM     smb_llist_t	*bucket,
9528934SJose.Borrego@Sun.COM     uint32_t	hashkey)
9538934SJose.Borrego@Sun.COM {
9548934SJose.Borrego@Sun.COM 	smb_node_t	*node;
9558934SJose.Borrego@Sun.COM 
9568934SJose.Borrego@Sun.COM 	node = kmem_cache_alloc(smb_node_cache, KM_SLEEP);
9578934SJose.Borrego@Sun.COM 
9588934SJose.Borrego@Sun.COM 	if (node->n_audit_buf != NULL)
9598934SJose.Borrego@Sun.COM 		node->n_audit_buf->anb_index = 0;
9608934SJose.Borrego@Sun.COM 
96110001SJoyce.McIntosh@Sun.COM 	node->flags = 0;
9628934SJose.Borrego@Sun.COM 	VN_HOLD(vp);
9638934SJose.Borrego@Sun.COM 	node->vp = vp;
9648934SJose.Borrego@Sun.COM 	node->n_refcnt = 1;
9658934SJose.Borrego@Sun.COM 	node->n_hash_bucket = bucket;
9668934SJose.Borrego@Sun.COM 	node->n_hashkey = hashkey;
9678934SJose.Borrego@Sun.COM 	node->n_orig_uid = 0;
9688934SJose.Borrego@Sun.COM 	node->readonly_creator = NULL;
9698934SJose.Borrego@Sun.COM 	node->waiting_event = 0;
9708934SJose.Borrego@Sun.COM 	node->n_open_count = 0;
97110122SJordan.Brown@Sun.COM 	node->n_dnode = NULL;
97210122SJordan.Brown@Sun.COM 	node->n_unode = NULL;
9738934SJose.Borrego@Sun.COM 	node->delete_on_close_cred = NULL;
9749231SAfshin.Ardakani@Sun.COM 	node->n_delete_on_close_flags = 0;
9758934SJose.Borrego@Sun.COM 
9768934SJose.Borrego@Sun.COM 	(void) strlcpy(node->od_name, od_name, sizeof (node->od_name));
9778934SJose.Borrego@Sun.COM 	if (strcmp(od_name, XATTR_DIR) == 0)
9788934SJose.Borrego@Sun.COM 		node->flags |= NODE_XATTR_DIR;
9798934SJose.Borrego@Sun.COM 
9808934SJose.Borrego@Sun.COM 	node->n_state = SMB_NODE_STATE_AVAILABLE;
9818934SJose.Borrego@Sun.COM 	node->n_magic = SMB_NODE_MAGIC;
9828934SJose.Borrego@Sun.COM 	return (node);
9838934SJose.Borrego@Sun.COM }
9848934SJose.Borrego@Sun.COM 
9858934SJose.Borrego@Sun.COM /*
9868934SJose.Borrego@Sun.COM  * smb_node_free
9878934SJose.Borrego@Sun.COM  */
9888934SJose.Borrego@Sun.COM static void
9898934SJose.Borrego@Sun.COM smb_node_free(smb_node_t *node)
9908934SJose.Borrego@Sun.COM {
9918934SJose.Borrego@Sun.COM 	SMB_NODE_VALID(node);
9928934SJose.Borrego@Sun.COM 
9938934SJose.Borrego@Sun.COM 	node->n_magic = 0;
9948934SJose.Borrego@Sun.COM 	VERIFY(!list_link_active(&node->n_lnd));
9958934SJose.Borrego@Sun.COM 	VERIFY(node->n_lock_list.ll_count == 0);
9968934SJose.Borrego@Sun.COM 	VERIFY(node->n_ofile_list.ll_count == 0);
9978934SJose.Borrego@Sun.COM 	VERIFY(node->n_oplock.ol_xthread == NULL);
9989021Samw@Sun.COM 	VERIFY(mutex_owner(&node->n_mutex) == NULL);
9999021Samw@Sun.COM 	VERIFY(!RW_LOCK_HELD(&node->n_lock));
10008934SJose.Borrego@Sun.COM 	VN_RELE(node->vp);
10018934SJose.Borrego@Sun.COM 	kmem_cache_free(smb_node_cache, node);
10028934SJose.Borrego@Sun.COM }
10038934SJose.Borrego@Sun.COM 
10048934SJose.Borrego@Sun.COM /*
10058934SJose.Borrego@Sun.COM  * smb_node_constructor
10068934SJose.Borrego@Sun.COM  */
10078934SJose.Borrego@Sun.COM static int
10088934SJose.Borrego@Sun.COM smb_node_constructor(void *buf, void *un, int kmflags)
10098934SJose.Borrego@Sun.COM {
10108934SJose.Borrego@Sun.COM 	_NOTE(ARGUNUSED(kmflags, un))
10118934SJose.Borrego@Sun.COM 
10128934SJose.Borrego@Sun.COM 	smb_node_t	*node = (smb_node_t *)buf;
10138934SJose.Borrego@Sun.COM 
10148934SJose.Borrego@Sun.COM 	bzero(node, sizeof (smb_node_t));
10158934SJose.Borrego@Sun.COM 
10168934SJose.Borrego@Sun.COM 	smb_llist_constructor(&node->n_ofile_list, sizeof (smb_ofile_t),
10178934SJose.Borrego@Sun.COM 	    offsetof(smb_ofile_t, f_nnd));
10188934SJose.Borrego@Sun.COM 	smb_llist_constructor(&node->n_lock_list, sizeof (smb_lock_t),
10198934SJose.Borrego@Sun.COM 	    offsetof(smb_lock_t, l_lnd));
10208934SJose.Borrego@Sun.COM 	cv_init(&node->n_oplock.ol_cv, NULL, CV_DEFAULT, NULL);
10218934SJose.Borrego@Sun.COM 	rw_init(&node->n_lock, NULL, RW_DEFAULT, NULL);
10228934SJose.Borrego@Sun.COM 	mutex_init(&node->n_mutex, NULL, MUTEX_DEFAULT, NULL);
10238934SJose.Borrego@Sun.COM 	smb_node_create_audit_buf(node, kmflags);
10248934SJose.Borrego@Sun.COM 	return (0);
10258934SJose.Borrego@Sun.COM }
10268934SJose.Borrego@Sun.COM 
10278934SJose.Borrego@Sun.COM /*
10288934SJose.Borrego@Sun.COM  * smb_node_destructor
10298934SJose.Borrego@Sun.COM  */
10308934SJose.Borrego@Sun.COM static void
10318934SJose.Borrego@Sun.COM smb_node_destructor(void *buf, void *un)
10328934SJose.Borrego@Sun.COM {
10338934SJose.Borrego@Sun.COM 	_NOTE(ARGUNUSED(un))
10348934SJose.Borrego@Sun.COM 
10358934SJose.Borrego@Sun.COM 	smb_node_t	*node = (smb_node_t *)buf;
10368934SJose.Borrego@Sun.COM 
10378934SJose.Borrego@Sun.COM 	smb_node_destroy_audit_buf(node);
10388934SJose.Borrego@Sun.COM 	mutex_destroy(&node->n_mutex);
10398934SJose.Borrego@Sun.COM 	rw_destroy(&node->n_lock);
10408934SJose.Borrego@Sun.COM 	cv_destroy(&node->n_oplock.ol_cv);
10418934SJose.Borrego@Sun.COM 	smb_llist_destructor(&node->n_lock_list);
10428934SJose.Borrego@Sun.COM 	smb_llist_destructor(&node->n_ofile_list);
10438934SJose.Borrego@Sun.COM }
10448934SJose.Borrego@Sun.COM 
10458934SJose.Borrego@Sun.COM /*
10468934SJose.Borrego@Sun.COM  * smb_node_create_audit_buf
10478934SJose.Borrego@Sun.COM  */
10488934SJose.Borrego@Sun.COM static void
10498934SJose.Borrego@Sun.COM smb_node_create_audit_buf(smb_node_t *node, int kmflags)
10508934SJose.Borrego@Sun.COM {
10518934SJose.Borrego@Sun.COM 	smb_audit_buf_node_t	*abn;
10528934SJose.Borrego@Sun.COM 
10538934SJose.Borrego@Sun.COM 	if (smb_audit_flags & SMB_AUDIT_NODE) {
10548934SJose.Borrego@Sun.COM 		abn = kmem_zalloc(sizeof (smb_audit_buf_node_t), kmflags);
10558934SJose.Borrego@Sun.COM 		abn->anb_max_index = SMB_AUDIT_BUF_MAX_REC - 1;
10568934SJose.Borrego@Sun.COM 		node->n_audit_buf = abn;
10578934SJose.Borrego@Sun.COM 	}
10588934SJose.Borrego@Sun.COM }
10598934SJose.Borrego@Sun.COM 
10608934SJose.Borrego@Sun.COM /*
10618934SJose.Borrego@Sun.COM  * smb_node_destroy_audit_buf
10628934SJose.Borrego@Sun.COM  */
10638934SJose.Borrego@Sun.COM static void
10648934SJose.Borrego@Sun.COM smb_node_destroy_audit_buf(smb_node_t *node)
10658934SJose.Borrego@Sun.COM {
10668934SJose.Borrego@Sun.COM 	if (node->n_audit_buf != NULL) {
10678934SJose.Borrego@Sun.COM 		kmem_free(node->n_audit_buf, sizeof (smb_audit_buf_node_t));
10688934SJose.Borrego@Sun.COM 		node->n_audit_buf = NULL;
10698934SJose.Borrego@Sun.COM 	}
10708934SJose.Borrego@Sun.COM }
10718934SJose.Borrego@Sun.COM 
10728934SJose.Borrego@Sun.COM /*
10738934SJose.Borrego@Sun.COM  * smb_node_audit
10748934SJose.Borrego@Sun.COM  *
10758934SJose.Borrego@Sun.COM  * This function saves the calling stack in the audit buffer of the node passed
10768934SJose.Borrego@Sun.COM  * in.
10778934SJose.Borrego@Sun.COM  */
10788934SJose.Borrego@Sun.COM static void
10798934SJose.Borrego@Sun.COM smb_node_audit(smb_node_t *node)
10808934SJose.Borrego@Sun.COM {
10818934SJose.Borrego@Sun.COM 	smb_audit_buf_node_t	*abn;
10828934SJose.Borrego@Sun.COM 	smb_audit_record_node_t	*anr;
10838934SJose.Borrego@Sun.COM 
10848934SJose.Borrego@Sun.COM 	if (node->n_audit_buf) {
10858934SJose.Borrego@Sun.COM 		abn = node->n_audit_buf;
10868934SJose.Borrego@Sun.COM 		anr = abn->anb_records;
10878934SJose.Borrego@Sun.COM 		anr += abn->anb_index;
10888934SJose.Borrego@Sun.COM 		abn->anb_index++;
10898934SJose.Borrego@Sun.COM 		abn->anb_index &= abn->anb_max_index;
10908934SJose.Borrego@Sun.COM 		anr->anr_refcnt = node->n_refcnt;
10918934SJose.Borrego@Sun.COM 		anr->anr_depth = getpcstack(anr->anr_stack,
10928934SJose.Borrego@Sun.COM 		    SMB_AUDIT_STACK_DEPTH);
10938934SJose.Borrego@Sun.COM 	}
10948934SJose.Borrego@Sun.COM }
10958934SJose.Borrego@Sun.COM 
10968934SJose.Borrego@Sun.COM static smb_llist_t *
10978934SJose.Borrego@Sun.COM smb_node_get_hash(fsid_t *fsid, smb_attr_t *attr, uint32_t *phashkey)
10988934SJose.Borrego@Sun.COM {
10998934SJose.Borrego@Sun.COM 	uint32_t	hashkey;
11008934SJose.Borrego@Sun.COM 
11018934SJose.Borrego@Sun.COM 	hashkey = fsid->val[0] + attr->sa_vattr.va_nodeid;
11028934SJose.Borrego@Sun.COM 	hashkey += (hashkey >> 24) + (hashkey >> 16) + (hashkey >> 8);
11038934SJose.Borrego@Sun.COM 	*phashkey = hashkey;
11048934SJose.Borrego@Sun.COM 	return (&smb_node_hash_table[(hashkey & SMBND_HASH_MASK)]);
11058934SJose.Borrego@Sun.COM }
110610001SJoyce.McIntosh@Sun.COM 
110710001SJoyce.McIntosh@Sun.COM boolean_t
110810001SJoyce.McIntosh@Sun.COM smb_node_is_dir(smb_node_t *node)
110910001SJoyce.McIntosh@Sun.COM {
111010001SJoyce.McIntosh@Sun.COM 	SMB_NODE_VALID(node);
111110001SJoyce.McIntosh@Sun.COM 	return (node->vp->v_type == VDIR);
111210001SJoyce.McIntosh@Sun.COM }
111310001SJoyce.McIntosh@Sun.COM 
111410001SJoyce.McIntosh@Sun.COM boolean_t
111510001SJoyce.McIntosh@Sun.COM smb_node_is_link(smb_node_t *node)
111610001SJoyce.McIntosh@Sun.COM {
111710001SJoyce.McIntosh@Sun.COM 	SMB_NODE_VALID(node);
111810001SJoyce.McIntosh@Sun.COM 	return (node->vp->v_type == VLNK);
111910001SJoyce.McIntosh@Sun.COM }
112010001SJoyce.McIntosh@Sun.COM 
112110001SJoyce.McIntosh@Sun.COM /*
112210001SJoyce.McIntosh@Sun.COM  * smb_node_file_is_readonly
112310001SJoyce.McIntosh@Sun.COM  *
112410001SJoyce.McIntosh@Sun.COM  * Checks if the file (which node represents) is marked readonly
112510001SJoyce.McIntosh@Sun.COM  * in the filesystem. No account is taken of any pending readonly
112610001SJoyce.McIntosh@Sun.COM  * in the node, which must be handled by the callers.
112710001SJoyce.McIntosh@Sun.COM  * (See SMB_OFILE_IS_READONLY and SMB_PATHFILE_IS_READONLY)
112810001SJoyce.McIntosh@Sun.COM  */
112910001SJoyce.McIntosh@Sun.COM boolean_t
113010001SJoyce.McIntosh@Sun.COM smb_node_file_is_readonly(smb_node_t *node)
113110001SJoyce.McIntosh@Sun.COM {
113210001SJoyce.McIntosh@Sun.COM 	smb_attr_t attr;
113310001SJoyce.McIntosh@Sun.COM 
113410001SJoyce.McIntosh@Sun.COM 	if (node == NULL)
113510001SJoyce.McIntosh@Sun.COM 		return (B_FALSE);
113610001SJoyce.McIntosh@Sun.COM 
113710001SJoyce.McIntosh@Sun.COM 	bzero(&attr, sizeof (smb_attr_t));
113810001SJoyce.McIntosh@Sun.COM 	attr.sa_mask = SMB_AT_DOSATTR;
113910001SJoyce.McIntosh@Sun.COM 	(void) smb_fsop_getattr(NULL, kcred, node, &attr);
114010001SJoyce.McIntosh@Sun.COM 	return ((attr.sa_dosattr & FILE_ATTRIBUTE_READONLY) != 0);
114110001SJoyce.McIntosh@Sun.COM }
114210001SJoyce.McIntosh@Sun.COM 
114310001SJoyce.McIntosh@Sun.COM /*
114410001SJoyce.McIntosh@Sun.COM  * smb_node_setattr
114510001SJoyce.McIntosh@Sun.COM  *
114610001SJoyce.McIntosh@Sun.COM  * The sr may be NULL, for example when closing an ofile.
114710001SJoyce.McIntosh@Sun.COM  * The ofile may be NULL, for example when a client request
114810001SJoyce.McIntosh@Sun.COM  * specifies the file by pathname.
114910001SJoyce.McIntosh@Sun.COM  *
1150*10504SKeyur.Desai@Sun.COM  * Timestamps
115110001SJoyce.McIntosh@Sun.COM  * When attributes are set on an ofile, any pending timestamps
115210001SJoyce.McIntosh@Sun.COM  * from a write request on the ofile are implicitly set to "now".
115310001SJoyce.McIntosh@Sun.COM  * For compatibility with windows the following timestamps are
115410001SJoyce.McIntosh@Sun.COM  * also implicitly set to now:
115510001SJoyce.McIntosh@Sun.COM  * - if any attribute is being explicitly set, set ctime to now
115610001SJoyce.McIntosh@Sun.COM  * - if file size is being explicitly set, set atime & ctime to now
115710001SJoyce.McIntosh@Sun.COM  *
1158*10504SKeyur.Desai@Sun.COM  * Any timestamp that is being explicitly set, or has previously
115910001SJoyce.McIntosh@Sun.COM  * been explicitly set on the ofile, is excluded from implicit
116010001SJoyce.McIntosh@Sun.COM  * (now) setting.
116110001SJoyce.McIntosh@Sun.COM  *
116210001SJoyce.McIntosh@Sun.COM  * Updates the node's cached timestamp values.
116310001SJoyce.McIntosh@Sun.COM  * Updates the ofile's explicit times flag.
116410001SJoyce.McIntosh@Sun.COM  *
1165*10504SKeyur.Desai@Sun.COM  * File allocation size
1166*10504SKeyur.Desai@Sun.COM  * When the file allocation size is set it is first rounded up
1167*10504SKeyur.Desai@Sun.COM  * to block size. If the file size is smaller than the allocation
1168*10504SKeyur.Desai@Sun.COM  * size the file is truncated by setting the filesize to allocsz.
1169*10504SKeyur.Desai@Sun.COM  * If there are open ofiles, the allocsz is cached on the node.
1170*10504SKeyur.Desai@Sun.COM  *
1171*10504SKeyur.Desai@Sun.COM  * Updates the node's cached allocsz value.
1172*10504SKeyur.Desai@Sun.COM  *
117310001SJoyce.McIntosh@Sun.COM  * Returns: errno
117410001SJoyce.McIntosh@Sun.COM  */
117510001SJoyce.McIntosh@Sun.COM int
117610001SJoyce.McIntosh@Sun.COM smb_node_setattr(smb_request_t *sr, smb_node_t *node,
117710001SJoyce.McIntosh@Sun.COM     cred_t *cr, smb_ofile_t *of, smb_attr_t *attr)
117810001SJoyce.McIntosh@Sun.COM {
117910001SJoyce.McIntosh@Sun.COM 	int rc;
1180*10504SKeyur.Desai@Sun.COM 	uint32_t pending_times = 0;
1181*10504SKeyur.Desai@Sun.COM 	uint32_t explicit_times = 0;
118210001SJoyce.McIntosh@Sun.COM 	timestruc_t now;
1183*10504SKeyur.Desai@Sun.COM 	smb_attr_t tmp_attr;
118410001SJoyce.McIntosh@Sun.COM 
118510001SJoyce.McIntosh@Sun.COM 	ASSERT(attr);
118610001SJoyce.McIntosh@Sun.COM 	SMB_NODE_VALID(node);
118710001SJoyce.McIntosh@Sun.COM 
1188*10504SKeyur.Desai@Sun.COM 	/* set attributes specified in attr */
1189*10504SKeyur.Desai@Sun.COM 	if (attr->sa_mask != 0) {
1190*10504SKeyur.Desai@Sun.COM 		/* if allocation size is < file size, truncate the file */
1191*10504SKeyur.Desai@Sun.COM 		if (attr->sa_mask & SMB_AT_ALLOCSZ) {
1192*10504SKeyur.Desai@Sun.COM 			attr->sa_allocsz = SMB_ALLOCSZ(attr->sa_allocsz);
119310001SJoyce.McIntosh@Sun.COM 
1194*10504SKeyur.Desai@Sun.COM 			bzero(&tmp_attr, sizeof (smb_attr_t));
1195*10504SKeyur.Desai@Sun.COM 			tmp_attr.sa_mask = SMB_AT_SIZE;
1196*10504SKeyur.Desai@Sun.COM 			(void) smb_fsop_getattr(NULL, kcred, node, &tmp_attr);
119710001SJoyce.McIntosh@Sun.COM 
1198*10504SKeyur.Desai@Sun.COM 			if (tmp_attr.sa_vattr.va_size > attr->sa_allocsz) {
1199*10504SKeyur.Desai@Sun.COM 				attr->sa_vattr.va_size = attr->sa_allocsz;
1200*10504SKeyur.Desai@Sun.COM 				attr->sa_mask |= SMB_AT_SIZE;
1201*10504SKeyur.Desai@Sun.COM 			}
120210001SJoyce.McIntosh@Sun.COM 		}
1203*10504SKeyur.Desai@Sun.COM 
1204*10504SKeyur.Desai@Sun.COM 		rc = smb_fsop_setattr(sr, cr, node, attr);
1205*10504SKeyur.Desai@Sun.COM 		if (rc != 0)
1206*10504SKeyur.Desai@Sun.COM 			return (rc);
1207*10504SKeyur.Desai@Sun.COM 
1208*10504SKeyur.Desai@Sun.COM 		smb_node_set_cached_allocsz(node, attr);
1209*10504SKeyur.Desai@Sun.COM 		smb_node_set_cached_timestamps(node, attr);
1210*10504SKeyur.Desai@Sun.COM 		if (of) {
1211*10504SKeyur.Desai@Sun.COM 			smb_ofile_set_explicit_times(of,
1212*10504SKeyur.Desai@Sun.COM 			    (attr->sa_mask & SMB_AT_TIMES));
121310001SJoyce.McIntosh@Sun.COM 		}
121410001SJoyce.McIntosh@Sun.COM 	}
121510001SJoyce.McIntosh@Sun.COM 
1216*10504SKeyur.Desai@Sun.COM 	/*
1217*10504SKeyur.Desai@Sun.COM 	 * Determine which timestamps to implicitly set to "now".
1218*10504SKeyur.Desai@Sun.COM 	 * Don't overwrite timestamps already explicitly set.
1219*10504SKeyur.Desai@Sun.COM 	 */
1220*10504SKeyur.Desai@Sun.COM 	bzero(&tmp_attr, sizeof (smb_attr_t));
1221*10504SKeyur.Desai@Sun.COM 	gethrestime(&now);
1222*10504SKeyur.Desai@Sun.COM 	tmp_attr.sa_vattr.va_atime = now;
1223*10504SKeyur.Desai@Sun.COM 	tmp_attr.sa_vattr.va_mtime = now;
1224*10504SKeyur.Desai@Sun.COM 	tmp_attr.sa_vattr.va_ctime = now;
122510001SJoyce.McIntosh@Sun.COM 
1226*10504SKeyur.Desai@Sun.COM 	/* pending write timestamps */
1227*10504SKeyur.Desai@Sun.COM 	if (of) {
1228*10504SKeyur.Desai@Sun.COM 		if (smb_ofile_write_time_pending(of)) {
1229*10504SKeyur.Desai@Sun.COM 			pending_times |=
1230*10504SKeyur.Desai@Sun.COM 			    (SMB_AT_MTIME | SMB_AT_CTIME | SMB_AT_ATIME);
1231*10504SKeyur.Desai@Sun.COM 		}
1232*10504SKeyur.Desai@Sun.COM 		explicit_times |= (smb_ofile_explicit_times(of));
1233*10504SKeyur.Desai@Sun.COM 	}
1234*10504SKeyur.Desai@Sun.COM 	explicit_times |= (attr->sa_mask & SMB_AT_TIMES);
1235*10504SKeyur.Desai@Sun.COM 	pending_times &= ~explicit_times;
123610001SJoyce.McIntosh@Sun.COM 
1237*10504SKeyur.Desai@Sun.COM 	if (pending_times) {
1238*10504SKeyur.Desai@Sun.COM 		tmp_attr.sa_mask = pending_times;
1239*10504SKeyur.Desai@Sun.COM 		(void) smb_fsop_setattr(NULL, kcred, node, &tmp_attr);
1240*10504SKeyur.Desai@Sun.COM 	}
124110001SJoyce.McIntosh@Sun.COM 
1242*10504SKeyur.Desai@Sun.COM 	/* additional timestamps to update in cache */
1243*10504SKeyur.Desai@Sun.COM 	if (attr->sa_mask)
1244*10504SKeyur.Desai@Sun.COM 		tmp_attr.sa_mask |= SMB_AT_CTIME;
1245*10504SKeyur.Desai@Sun.COM 	if (attr->sa_mask & (SMB_AT_SIZE | SMB_AT_ALLOCSZ))
1246*10504SKeyur.Desai@Sun.COM 		tmp_attr.sa_mask |= SMB_AT_MTIME;
1247*10504SKeyur.Desai@Sun.COM 	tmp_attr.sa_mask &= ~explicit_times;
1248*10504SKeyur.Desai@Sun.COM 
1249*10504SKeyur.Desai@Sun.COM 	if (tmp_attr.sa_mask)
1250*10504SKeyur.Desai@Sun.COM 		smb_node_set_cached_timestamps(node, &tmp_attr);
125110001SJoyce.McIntosh@Sun.COM 
125210001SJoyce.McIntosh@Sun.COM 	return (0);
125310001SJoyce.McIntosh@Sun.COM }
125410001SJoyce.McIntosh@Sun.COM 
125510001SJoyce.McIntosh@Sun.COM /*
125610001SJoyce.McIntosh@Sun.COM  * smb_node_getattr
125710001SJoyce.McIntosh@Sun.COM  *
125810001SJoyce.McIntosh@Sun.COM  * Get attributes from the file system and apply any smb-specific
125910001SJoyce.McIntosh@Sun.COM  * overrides for size, dos attributes and timestamps
126010001SJoyce.McIntosh@Sun.COM  *
126110001SJoyce.McIntosh@Sun.COM  * node->readonly_creator reflects whether a readonly set is pending
126210001SJoyce.McIntosh@Sun.COM  * from a readonly create. The readonly attribute should be visible to
126310001SJoyce.McIntosh@Sun.COM  * all clients even though the readonly creator fid is immune to the
126410001SJoyce.McIntosh@Sun.COM  * readonly bit until close.
126510001SJoyce.McIntosh@Sun.COM  *
126610001SJoyce.McIntosh@Sun.COM  * Returns: errno
126710001SJoyce.McIntosh@Sun.COM  */
126810001SJoyce.McIntosh@Sun.COM int
126910001SJoyce.McIntosh@Sun.COM smb_node_getattr(smb_request_t *sr, smb_node_t *node, smb_attr_t *attr)
127010001SJoyce.McIntosh@Sun.COM {
127110001SJoyce.McIntosh@Sun.COM 	int rc;
127210001SJoyce.McIntosh@Sun.COM 
127310001SJoyce.McIntosh@Sun.COM 	SMB_NODE_VALID(node);
127410001SJoyce.McIntosh@Sun.COM 
127510001SJoyce.McIntosh@Sun.COM 	bzero(attr, sizeof (smb_attr_t));
127610001SJoyce.McIntosh@Sun.COM 	attr->sa_mask = SMB_AT_ALL;
127710001SJoyce.McIntosh@Sun.COM 	rc = smb_fsop_getattr(sr, kcred, node, attr);
127810001SJoyce.McIntosh@Sun.COM 	if (rc != 0)
127910001SJoyce.McIntosh@Sun.COM 		return (rc);
128010001SJoyce.McIntosh@Sun.COM 
128110001SJoyce.McIntosh@Sun.COM 	mutex_enter(&node->n_mutex);
128210001SJoyce.McIntosh@Sun.COM 
1283*10504SKeyur.Desai@Sun.COM 	if (node->vp->v_type == VDIR) {
128410001SJoyce.McIntosh@Sun.COM 		attr->sa_vattr.va_size = 0;
1285*10504SKeyur.Desai@Sun.COM 		attr->sa_allocsz = 0;
1286*10504SKeyur.Desai@Sun.COM 	}
128710001SJoyce.McIntosh@Sun.COM 
128810001SJoyce.McIntosh@Sun.COM 	if (node->readonly_creator)
128910001SJoyce.McIntosh@Sun.COM 		attr->sa_dosattr |= FILE_ATTRIBUTE_READONLY;
129010001SJoyce.McIntosh@Sun.COM 	if (attr->sa_dosattr == 0)
129110001SJoyce.McIntosh@Sun.COM 		attr->sa_dosattr = FILE_ATTRIBUTE_NORMAL;
129210001SJoyce.McIntosh@Sun.COM 
1293*10504SKeyur.Desai@Sun.COM 
129410001SJoyce.McIntosh@Sun.COM 	mutex_exit(&node->n_mutex);
129510001SJoyce.McIntosh@Sun.COM 
1296*10504SKeyur.Desai@Sun.COM 	smb_node_get_cached_allocsz(node, attr);
129710001SJoyce.McIntosh@Sun.COM 	smb_node_get_cached_timestamps(node, attr);
1298*10504SKeyur.Desai@Sun.COM 
129910001SJoyce.McIntosh@Sun.COM 	return (0);
130010001SJoyce.McIntosh@Sun.COM }
130110001SJoyce.McIntosh@Sun.COM 
130210001SJoyce.McIntosh@Sun.COM /*
1303*10504SKeyur.Desai@Sun.COM  * smb_node_init_cached_data
1304*10504SKeyur.Desai@Sun.COM  */
1305*10504SKeyur.Desai@Sun.COM static void
1306*10504SKeyur.Desai@Sun.COM smb_node_init_cached_data(smb_node_t *node)
1307*10504SKeyur.Desai@Sun.COM {
1308*10504SKeyur.Desai@Sun.COM 	smb_attr_t attr;
1309*10504SKeyur.Desai@Sun.COM 
1310*10504SKeyur.Desai@Sun.COM 	bzero(&attr, sizeof (smb_attr_t));
1311*10504SKeyur.Desai@Sun.COM 	attr.sa_mask = SMB_AT_ALL;
1312*10504SKeyur.Desai@Sun.COM 	(void) smb_fsop_getattr(NULL, kcred, node, &attr);
1313*10504SKeyur.Desai@Sun.COM 
1314*10504SKeyur.Desai@Sun.COM 	smb_node_init_cached_allocsz(node, &attr);
1315*10504SKeyur.Desai@Sun.COM 	smb_node_init_cached_timestamps(node, &attr);
1316*10504SKeyur.Desai@Sun.COM }
1317*10504SKeyur.Desai@Sun.COM 
1318*10504SKeyur.Desai@Sun.COM /*
1319*10504SKeyur.Desai@Sun.COM  * smb_node_clear_cached_data
1320*10504SKeyur.Desai@Sun.COM  */
1321*10504SKeyur.Desai@Sun.COM static void
1322*10504SKeyur.Desai@Sun.COM smb_node_clear_cached_data(smb_node_t *node)
1323*10504SKeyur.Desai@Sun.COM {
1324*10504SKeyur.Desai@Sun.COM 	smb_node_clear_cached_allocsz(node);
1325*10504SKeyur.Desai@Sun.COM 	smb_node_clear_cached_timestamps(node);
1326*10504SKeyur.Desai@Sun.COM }
1327*10504SKeyur.Desai@Sun.COM 
1328*10504SKeyur.Desai@Sun.COM /*
1329*10504SKeyur.Desai@Sun.COM  * File allocation size (allocsz) caching
1330*10504SKeyur.Desai@Sun.COM  *
1331*10504SKeyur.Desai@Sun.COM  * When there are open ofiles on the node, the file allocsz is cached.
1332*10504SKeyur.Desai@Sun.COM  * The cached value (n_allocsz) is initialized when the first ofile is
1333*10504SKeyur.Desai@Sun.COM  * opened and cleared when the last is closed. Allocsz calculated from
1334*10504SKeyur.Desai@Sun.COM  * the filesize (rounded up to block size).
1335*10504SKeyur.Desai@Sun.COM  * When the allocation size is queried, if the cached allocsz is less
1336*10504SKeyur.Desai@Sun.COM  * than the filesize, it is recalculated from the filesize.
1337*10504SKeyur.Desai@Sun.COM  */
1338*10504SKeyur.Desai@Sun.COM 
1339*10504SKeyur.Desai@Sun.COM /*
1340*10504SKeyur.Desai@Sun.COM  * smb_node_init_cached_allocsz
1341*10504SKeyur.Desai@Sun.COM  *
1342*10504SKeyur.Desai@Sun.COM  * If there are open ofiles, cache the allocsz in the node.
1343*10504SKeyur.Desai@Sun.COM  * Calculate the allocsz from the filesizes.
1344*10504SKeyur.Desai@Sun.COM  * block size).
1345*10504SKeyur.Desai@Sun.COM  */
1346*10504SKeyur.Desai@Sun.COM static void
1347*10504SKeyur.Desai@Sun.COM smb_node_init_cached_allocsz(smb_node_t *node, smb_attr_t *attr)
1348*10504SKeyur.Desai@Sun.COM {
1349*10504SKeyur.Desai@Sun.COM 	mutex_enter(&node->n_mutex);
1350*10504SKeyur.Desai@Sun.COM 	if (node->n_open_count == 1)
1351*10504SKeyur.Desai@Sun.COM 		node->n_allocsz = SMB_ALLOCSZ(attr->sa_vattr.va_size);
1352*10504SKeyur.Desai@Sun.COM 	mutex_exit(&node->n_mutex);
1353*10504SKeyur.Desai@Sun.COM }
1354*10504SKeyur.Desai@Sun.COM 
1355*10504SKeyur.Desai@Sun.COM /*
1356*10504SKeyur.Desai@Sun.COM  * smb_node_clear_cached_allocsz
1357*10504SKeyur.Desai@Sun.COM  */
1358*10504SKeyur.Desai@Sun.COM static void
1359*10504SKeyur.Desai@Sun.COM smb_node_clear_cached_allocsz(smb_node_t *node)
1360*10504SKeyur.Desai@Sun.COM {
1361*10504SKeyur.Desai@Sun.COM 	mutex_enter(&node->n_mutex);
1362*10504SKeyur.Desai@Sun.COM 	if (node->n_open_count == 0)
1363*10504SKeyur.Desai@Sun.COM 		node->n_allocsz = 0;
1364*10504SKeyur.Desai@Sun.COM 	mutex_exit(&node->n_mutex);
1365*10504SKeyur.Desai@Sun.COM }
1366*10504SKeyur.Desai@Sun.COM 
1367*10504SKeyur.Desai@Sun.COM /*
1368*10504SKeyur.Desai@Sun.COM  * smb_node_get_cached_allocsz
1369*10504SKeyur.Desai@Sun.COM  *
1370*10504SKeyur.Desai@Sun.COM  * If there is no cached allocsz (no open files), calculate
1371*10504SKeyur.Desai@Sun.COM  * allocsz from the filesize.
1372*10504SKeyur.Desai@Sun.COM  * If the allocsz is cached but is smaller than the filesize
1373*10504SKeyur.Desai@Sun.COM  * recalculate the cached allocsz from the filesize.
1374*10504SKeyur.Desai@Sun.COM  *
1375*10504SKeyur.Desai@Sun.COM  * Return allocs in attr->sa_allocsz.
1376*10504SKeyur.Desai@Sun.COM  */
1377*10504SKeyur.Desai@Sun.COM static void
1378*10504SKeyur.Desai@Sun.COM smb_node_get_cached_allocsz(smb_node_t *node, smb_attr_t *attr)
1379*10504SKeyur.Desai@Sun.COM {
1380*10504SKeyur.Desai@Sun.COM 	if (node->vp->v_type == VDIR)
1381*10504SKeyur.Desai@Sun.COM 		return;
1382*10504SKeyur.Desai@Sun.COM 
1383*10504SKeyur.Desai@Sun.COM 	mutex_enter(&node->n_mutex);
1384*10504SKeyur.Desai@Sun.COM 	if (node->n_open_count == 0) {
1385*10504SKeyur.Desai@Sun.COM 		attr->sa_allocsz = SMB_ALLOCSZ(attr->sa_vattr.va_size);
1386*10504SKeyur.Desai@Sun.COM 	} else {
1387*10504SKeyur.Desai@Sun.COM 		if (node->n_allocsz < attr->sa_vattr.va_size)
1388*10504SKeyur.Desai@Sun.COM 			node->n_allocsz = SMB_ALLOCSZ(attr->sa_vattr.va_size);
1389*10504SKeyur.Desai@Sun.COM 		attr->sa_allocsz = node->n_allocsz;
1390*10504SKeyur.Desai@Sun.COM 	}
1391*10504SKeyur.Desai@Sun.COM 	mutex_exit(&node->n_mutex);
1392*10504SKeyur.Desai@Sun.COM }
1393*10504SKeyur.Desai@Sun.COM 
1394*10504SKeyur.Desai@Sun.COM /*
1395*10504SKeyur.Desai@Sun.COM  * smb_node_set_cached_allocsz
1396*10504SKeyur.Desai@Sun.COM  *
1397*10504SKeyur.Desai@Sun.COM  * attr->sa_allocsz has already been rounded to block size by
1398*10504SKeyur.Desai@Sun.COM  * the caller.
1399*10504SKeyur.Desai@Sun.COM  */
1400*10504SKeyur.Desai@Sun.COM static void
1401*10504SKeyur.Desai@Sun.COM smb_node_set_cached_allocsz(smb_node_t *node, smb_attr_t *attr)
1402*10504SKeyur.Desai@Sun.COM {
1403*10504SKeyur.Desai@Sun.COM 	mutex_enter(&node->n_mutex);
1404*10504SKeyur.Desai@Sun.COM 	if (attr->sa_mask & SMB_AT_ALLOCSZ) {
1405*10504SKeyur.Desai@Sun.COM 		if (node->n_open_count > 0)
1406*10504SKeyur.Desai@Sun.COM 			node->n_allocsz = attr->sa_allocsz;
1407*10504SKeyur.Desai@Sun.COM 	}
1408*10504SKeyur.Desai@Sun.COM 	mutex_exit(&node->n_mutex);
1409*10504SKeyur.Desai@Sun.COM }
1410*10504SKeyur.Desai@Sun.COM 
1411*10504SKeyur.Desai@Sun.COM 
1412*10504SKeyur.Desai@Sun.COM /*
141310001SJoyce.McIntosh@Sun.COM  * Timestamp caching
141410001SJoyce.McIntosh@Sun.COM  *
141510001SJoyce.McIntosh@Sun.COM  * Solaris file systems handle timestamps different from NTFS. For
141610001SJoyce.McIntosh@Sun.COM  * example when file data is written NTFS doesn't update the timestamps
141710001SJoyce.McIntosh@Sun.COM  * until the file is closed, and then only if they haven't been explicity
141810001SJoyce.McIntosh@Sun.COM  * set via a set attribute request. In order to provide a more similar
141910001SJoyce.McIntosh@Sun.COM  * view of an open file's timestamps, we cache the timestamps in the
142010001SJoyce.McIntosh@Sun.COM  * node and manipulate them in a manner more consistent with windows.
142110001SJoyce.McIntosh@Sun.COM  * (See handling of explicit times and pending timestamps from a write
142210001SJoyce.McIntosh@Sun.COM  * request in smb_node_getattr and smb_node_setattr above.)
142310001SJoyce.McIntosh@Sun.COM  * Timestamps remain cached while there are open ofiles for the node.
142410001SJoyce.McIntosh@Sun.COM  * This includes open ofiles for named streams.
142510001SJoyce.McIntosh@Sun.COM  * n_open_ofiles cannot be used as this doesn't include ofiles opened
142610001SJoyce.McIntosh@Sun.COM  * for the node's named streams. Thus n_timestamps contains a count
142710001SJoyce.McIntosh@Sun.COM  * of open ofiles (t_open_ofiles), including named streams' ofiles,
142810001SJoyce.McIntosh@Sun.COM  * to be used to control timestamp caching.
142910001SJoyce.McIntosh@Sun.COM  *
143010001SJoyce.McIntosh@Sun.COM  * If a node represents a named stream the associated unnamed streams
143110001SJoyce.McIntosh@Sun.COM  * cached timestamps are used instead.
143210001SJoyce.McIntosh@Sun.COM  */
143310001SJoyce.McIntosh@Sun.COM 
143410001SJoyce.McIntosh@Sun.COM /*
143510001SJoyce.McIntosh@Sun.COM  * smb_node_init_cached_timestamps
143610001SJoyce.McIntosh@Sun.COM  *
143710001SJoyce.McIntosh@Sun.COM  * Increment count of open ofiles which are using the cached timestamps.
143810001SJoyce.McIntosh@Sun.COM  * If this is the first open ofile, init the cached timestamps from the
143910001SJoyce.McIntosh@Sun.COM  * file system values.
144010001SJoyce.McIntosh@Sun.COM  */
144110001SJoyce.McIntosh@Sun.COM static void
1442*10504SKeyur.Desai@Sun.COM smb_node_init_cached_timestamps(smb_node_t *node, smb_attr_t *attr)
144310001SJoyce.McIntosh@Sun.COM {
144410001SJoyce.McIntosh@Sun.COM 	smb_node_t *unode;
144510001SJoyce.McIntosh@Sun.COM 
144610001SJoyce.McIntosh@Sun.COM 	if ((unode = SMB_IS_STREAM(node)) != NULL)
144710001SJoyce.McIntosh@Sun.COM 		node = unode;
144810001SJoyce.McIntosh@Sun.COM 
144910001SJoyce.McIntosh@Sun.COM 	mutex_enter(&node->n_mutex);
145010001SJoyce.McIntosh@Sun.COM 	++(node->n_timestamps.t_open_ofiles);
145110001SJoyce.McIntosh@Sun.COM 	if (node->n_timestamps.t_open_ofiles == 1) {
1452*10504SKeyur.Desai@Sun.COM 		node->n_timestamps.t_mtime = attr->sa_vattr.va_mtime;
1453*10504SKeyur.Desai@Sun.COM 		node->n_timestamps.t_atime = attr->sa_vattr.va_atime;
1454*10504SKeyur.Desai@Sun.COM 		node->n_timestamps.t_ctime = attr->sa_vattr.va_ctime;
1455*10504SKeyur.Desai@Sun.COM 		node->n_timestamps.t_crtime = attr->sa_crtime;
145610001SJoyce.McIntosh@Sun.COM 		node->n_timestamps.t_cached = B_TRUE;
145710001SJoyce.McIntosh@Sun.COM 	}
145810001SJoyce.McIntosh@Sun.COM 	mutex_exit(&node->n_mutex);
145910001SJoyce.McIntosh@Sun.COM }
146010001SJoyce.McIntosh@Sun.COM 
146110001SJoyce.McIntosh@Sun.COM /*
146210001SJoyce.McIntosh@Sun.COM  * smb_node_clear_cached_timestamps
146310001SJoyce.McIntosh@Sun.COM  *
146410001SJoyce.McIntosh@Sun.COM  * Decrement count of open ofiles using the cached timestamps.
146510001SJoyce.McIntosh@Sun.COM  * If the decremented count is zero, clear the cached timestamps.
146610001SJoyce.McIntosh@Sun.COM  */
146710001SJoyce.McIntosh@Sun.COM static void
146810001SJoyce.McIntosh@Sun.COM smb_node_clear_cached_timestamps(smb_node_t *node)
146910001SJoyce.McIntosh@Sun.COM {
147010001SJoyce.McIntosh@Sun.COM 	smb_node_t *unode;
147110001SJoyce.McIntosh@Sun.COM 
147210001SJoyce.McIntosh@Sun.COM 	if ((unode = SMB_IS_STREAM(node)) != NULL)
147310001SJoyce.McIntosh@Sun.COM 		node = unode;
147410001SJoyce.McIntosh@Sun.COM 
147510001SJoyce.McIntosh@Sun.COM 	mutex_enter(&node->n_mutex);
147610001SJoyce.McIntosh@Sun.COM 	ASSERT(node->n_timestamps.t_open_ofiles > 0);
147710001SJoyce.McIntosh@Sun.COM 	--(node->n_timestamps.t_open_ofiles);
147810001SJoyce.McIntosh@Sun.COM 	if (node->n_timestamps.t_open_ofiles == 0)
147910001SJoyce.McIntosh@Sun.COM 		bzero(&node->n_timestamps, sizeof (smb_times_t));
148010001SJoyce.McIntosh@Sun.COM 	mutex_exit(&node->n_mutex);
148110001SJoyce.McIntosh@Sun.COM }
148210001SJoyce.McIntosh@Sun.COM 
148310001SJoyce.McIntosh@Sun.COM /*
148410001SJoyce.McIntosh@Sun.COM  * smb_node_get_cached_timestamps
148510001SJoyce.McIntosh@Sun.COM  *
148610001SJoyce.McIntosh@Sun.COM  * Overwrite timestamps in attr with those cached in node.
148710001SJoyce.McIntosh@Sun.COM  */
148810001SJoyce.McIntosh@Sun.COM static void
148910001SJoyce.McIntosh@Sun.COM smb_node_get_cached_timestamps(smb_node_t *node, smb_attr_t *attr)
149010001SJoyce.McIntosh@Sun.COM {
149110001SJoyce.McIntosh@Sun.COM 	smb_node_t *unode;
149210001SJoyce.McIntosh@Sun.COM 
149310001SJoyce.McIntosh@Sun.COM 	if ((unode = SMB_IS_STREAM(node)) != NULL)
149410001SJoyce.McIntosh@Sun.COM 		node = unode;
149510001SJoyce.McIntosh@Sun.COM 
149610001SJoyce.McIntosh@Sun.COM 	mutex_enter(&node->n_mutex);
149710001SJoyce.McIntosh@Sun.COM 	if (node->n_timestamps.t_cached) {
149810001SJoyce.McIntosh@Sun.COM 		attr->sa_vattr.va_mtime = node->n_timestamps.t_mtime;
149910001SJoyce.McIntosh@Sun.COM 		attr->sa_vattr.va_atime = node->n_timestamps.t_atime;
150010001SJoyce.McIntosh@Sun.COM 		attr->sa_vattr.va_ctime = node->n_timestamps.t_ctime;
150110001SJoyce.McIntosh@Sun.COM 		attr->sa_crtime = node->n_timestamps.t_crtime;
150210001SJoyce.McIntosh@Sun.COM 	}
150310001SJoyce.McIntosh@Sun.COM 	mutex_exit(&node->n_mutex);
150410001SJoyce.McIntosh@Sun.COM }
150510001SJoyce.McIntosh@Sun.COM 
150610001SJoyce.McIntosh@Sun.COM /*
150710001SJoyce.McIntosh@Sun.COM  * smb_node_set_cached_timestamps
150810001SJoyce.McIntosh@Sun.COM  *
150910001SJoyce.McIntosh@Sun.COM  * Update the node's cached timestamps with values from attr.
151010001SJoyce.McIntosh@Sun.COM  */
151110001SJoyce.McIntosh@Sun.COM static void
151210001SJoyce.McIntosh@Sun.COM smb_node_set_cached_timestamps(smb_node_t *node, smb_attr_t *attr)
151310001SJoyce.McIntosh@Sun.COM {
151410001SJoyce.McIntosh@Sun.COM 	smb_node_t *unode;
151510001SJoyce.McIntosh@Sun.COM 
151610001SJoyce.McIntosh@Sun.COM 	if ((unode = SMB_IS_STREAM(node)) != NULL)
151710001SJoyce.McIntosh@Sun.COM 		node = unode;
151810001SJoyce.McIntosh@Sun.COM 
151910001SJoyce.McIntosh@Sun.COM 	mutex_enter(&node->n_mutex);
152010001SJoyce.McIntosh@Sun.COM 	if (node->n_timestamps.t_cached) {
152110001SJoyce.McIntosh@Sun.COM 		if (attr->sa_mask & SMB_AT_MTIME)
152210001SJoyce.McIntosh@Sun.COM 			node->n_timestamps.t_mtime = attr->sa_vattr.va_mtime;
152310001SJoyce.McIntosh@Sun.COM 		if (attr->sa_mask & SMB_AT_ATIME)
152410001SJoyce.McIntosh@Sun.COM 			node->n_timestamps.t_atime = attr->sa_vattr.va_atime;
152510001SJoyce.McIntosh@Sun.COM 		if (attr->sa_mask & SMB_AT_CTIME)
152610001SJoyce.McIntosh@Sun.COM 			node->n_timestamps.t_ctime = attr->sa_vattr.va_ctime;
152710001SJoyce.McIntosh@Sun.COM 		if (attr->sa_mask & SMB_AT_CRTIME)
152810001SJoyce.McIntosh@Sun.COM 			node->n_timestamps.t_crtime = attr->sa_crtime;
152910001SJoyce.McIntosh@Sun.COM 	}
153010001SJoyce.McIntosh@Sun.COM 	mutex_exit(&node->n_mutex);
153110001SJoyce.McIntosh@Sun.COM }
1532