xref: /onnv-gate/usr/src/uts/common/fs/smbsrv/smb_vfs.c (revision 5331:3047ad28a67b)
1*5331Samw /*
2*5331Samw  * CDDL HEADER START
3*5331Samw  *
4*5331Samw  * The contents of this file are subject to the terms of the
5*5331Samw  * Common Development and Distribution License (the "License").
6*5331Samw  * You may not use this file except in compliance with the License.
7*5331Samw  *
8*5331Samw  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*5331Samw  * or http://www.opensolaris.org/os/licensing.
10*5331Samw  * See the License for the specific language governing permissions
11*5331Samw  * and limitations under the License.
12*5331Samw  *
13*5331Samw  * When distributing Covered Code, include this CDDL HEADER in each
14*5331Samw  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*5331Samw  * If applicable, add the following below this CDDL HEADER, with the
16*5331Samw  * fields enclosed by brackets "[]" replaced with your own identifying
17*5331Samw  * information: Portions Copyright [yyyy] [name of copyright owner]
18*5331Samw  *
19*5331Samw  * CDDL HEADER END
20*5331Samw  */
21*5331Samw /*
22*5331Samw  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23*5331Samw  * Use is subject to license terms.
24*5331Samw  */
25*5331Samw 
26*5331Samw #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*5331Samw 
28*5331Samw #include <smbsrv/smb_incl.h>
29*5331Samw #include <smbsrv/smb_fsops.h>
30*5331Samw #include <sys/vfs.h>
31*5331Samw 
32*5331Samw static smb_vfs_t *smb_vfs_lookup(vnode_t *);
33*5331Samw 
34*5331Samw /*
35*5331Samw  * smb_vfs_hold
36*5331Samw  *
37*5331Samw  * Increments the reference count of the fs passed in. If no smb_vfs_t structure
38*5331Samw  * has been created yet for the fs passed in it is created.
39*5331Samw  */
40*5331Samw boolean_t
41*5331Samw smb_vfs_hold(vfs_t *vfsp)
42*5331Samw {
43*5331Samw 	smb_vfs_t	*smb_vfs;
44*5331Samw 	vnode_t 	*rootvp;
45*5331Samw 
46*5331Samw 	if ((vfsp == NULL) || VFS_ROOT(vfsp, &rootvp))
47*5331Samw 		return (B_FALSE);
48*5331Samw 
49*5331Samw 	smb_llist_enter(&smb_info.si_vfs_list, RW_WRITER);
50*5331Samw 	smb_vfs = smb_vfs_lookup(rootvp);
51*5331Samw 	if (smb_vfs) {
52*5331Samw 		DTRACE_PROBE1(smb_vfs_hold_hit, smb_vfs_t *, smb_vfs);
53*5331Samw 		smb_llist_exit(&smb_info.si_vfs_list);
54*5331Samw 		VN_RELE(rootvp);
55*5331Samw 		return (B_TRUE);
56*5331Samw 	}
57*5331Samw 	smb_vfs = kmem_cache_alloc(smb_info.si_cache_vfs, KM_SLEEP);
58*5331Samw 
59*5331Samw 	bzero(smb_vfs, sizeof (smb_vfs_t));
60*5331Samw 
61*5331Samw 	smb_vfs->sv_magic = SMB_VFS_MAGIC;
62*5331Samw 	smb_vfs->sv_refcnt = 1;
63*5331Samw 	smb_vfs->sv_vfsp = vfsp;
64*5331Samw 	/*
65*5331Samw 	 * We have a hold on the root vnode of the file system
66*5331Samw 	 * from the VFS_ROOT call above.
67*5331Samw 	 */
68*5331Samw 	smb_vfs->sv_rootvp = rootvp;
69*5331Samw 	smb_llist_insert_head(&smb_info.si_vfs_list, smb_vfs);
70*5331Samw 	DTRACE_PROBE1(smb_vfs_hold_miss, smb_vfs_t *, smb_vfs);
71*5331Samw 	smb_llist_exit(&smb_info.si_vfs_list);
72*5331Samw 	return (B_TRUE);
73*5331Samw }
74*5331Samw 
75*5331Samw /*
76*5331Samw  * smb_vfs_rele
77*5331Samw  *
78*5331Samw  * Decrements the reference count of the fs passed in. If the reference count
79*5331Samw  * drops to zero the smb_vfs_t structure associated with the fs is freed.
80*5331Samw  */
81*5331Samw void
82*5331Samw smb_vfs_rele(vfs_t *vfsp)
83*5331Samw {
84*5331Samw 	smb_vfs_t	*smb_vfs;
85*5331Samw 	vnode_t		*rootvp;
86*5331Samw 
87*5331Samw 	ASSERT(vfsp);
88*5331Samw 
89*5331Samw 	if (VFS_ROOT(vfsp, &rootvp))
90*5331Samw 		return;
91*5331Samw 
92*5331Samw 	smb_llist_enter(&smb_info.si_vfs_list, RW_WRITER);
93*5331Samw 	smb_vfs = smb_vfs_lookup(rootvp);
94*5331Samw 	DTRACE_PROBE2(smb_vfs_release, smb_vfs_t *, smb_vfs, vnode_t *, rootvp);
95*5331Samw 	VN_RELE(rootvp);
96*5331Samw 	if (smb_vfs) {
97*5331Samw 		--smb_vfs->sv_refcnt;
98*5331Samw 		ASSERT(smb_vfs->sv_refcnt);
99*5331Samw 		if (--smb_vfs->sv_refcnt == 0) {
100*5331Samw 			smb_llist_remove(&smb_info.si_vfs_list, smb_vfs);
101*5331Samw 			smb_llist_exit(&smb_info.si_vfs_list);
102*5331Samw 			ASSERT(rootvp == smb_vfs->sv_rootvp);
103*5331Samw 			VN_RELE(smb_vfs->sv_rootvp);
104*5331Samw 			smb_vfs->sv_magic = (uint32_t)~SMB_VFS_MAGIC;
105*5331Samw 			kmem_cache_free(smb_info.si_cache_vfs, smb_vfs);
106*5331Samw 			return;
107*5331Samw 		}
108*5331Samw 	}
109*5331Samw 	smb_llist_exit(&smb_info.si_vfs_list);
110*5331Samw }
111*5331Samw 
112*5331Samw /*
113*5331Samw  * smb_vfs_rele_all()
114*5331Samw  *
115*5331Samw  * Release all holds on root vnodes of file systems which were taken
116*5331Samw  * due to the existence of at least one enabled share on the file system.
117*5331Samw  * Called at driver close time.
118*5331Samw  */
119*5331Samw void
120*5331Samw smb_vfs_rele_all()
121*5331Samw {
122*5331Samw 	smb_vfs_t	*smb_vfs;
123*5331Samw 
124*5331Samw 	smb_llist_enter(&smb_info.si_vfs_list, RW_WRITER);
125*5331Samw 	while ((smb_vfs = smb_llist_head(&smb_info.si_vfs_list)) != NULL) {
126*5331Samw 
127*5331Samw 		ASSERT(smb_vfs->sv_magic == SMB_VFS_MAGIC);
128*5331Samw 		DTRACE_PROBE1(smb_vfs_rele_all_hit, smb_vfs_t *, smb_vfs);
129*5331Samw 		smb_llist_remove(&smb_info.si_vfs_list, smb_vfs);
130*5331Samw 		VN_RELE(smb_vfs->sv_rootvp);
131*5331Samw 		kmem_cache_free(smb_info.si_cache_vfs, smb_vfs);
132*5331Samw 	}
133*5331Samw 	smb_llist_exit(&smb_info.si_vfs_list);
134*5331Samw }
135*5331Samw 
136*5331Samw /*
137*5331Samw  * smb_vfs_lookup
138*5331Samw  *
139*5331Samw  * Goes through the list of smb_vfs_t structure and returns the one matching
140*5331Samw  * the vnode passed in. If no match is found a NULL pointer is returned.
141*5331Samw  *
142*5331Samw  * The list of smb_vfs_t structures has to have been entered prior calling
143*5331Samw  * this function.
144*5331Samw  */
145*5331Samw static smb_vfs_t *
146*5331Samw smb_vfs_lookup(vnode_t *rootvp)
147*5331Samw {
148*5331Samw 	smb_vfs_t	*smb_vfs;
149*5331Samw 
150*5331Samw 	smb_vfs = smb_llist_head(&smb_info.si_vfs_list);
151*5331Samw 	while (smb_vfs) {
152*5331Samw 		ASSERT(smb_vfs->sv_magic == SMB_VFS_MAGIC);
153*5331Samw 		if (smb_vfs->sv_rootvp == rootvp) {
154*5331Samw 			smb_vfs->sv_refcnt++;
155*5331Samw 			ASSERT(smb_vfs->sv_refcnt);
156*5331Samw 			return (smb_vfs);
157*5331Samw 		}
158*5331Samw 		smb_vfs = smb_llist_next(&smb_info.si_vfs_list, smb_vfs);
159*5331Samw 	}
160*5331Samw 	return (NULL);
161*5331Samw }
162