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 /*
22*12508Samw@Sun.COM * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
235331Samw */
245331Samw
255331Samw #include <sys/vfs.h>
267348SJose.Borrego@Sun.COM #include <smbsrv/smb_ktypes.h>
277348SJose.Borrego@Sun.COM #include <smbsrv/smb_kproto.h>
285331Samw
29*12508Samw@Sun.COM static smb_vfs_t *smb_vfs_find(smb_export_t *, vfs_t *);
30*12508Samw@Sun.COM static void smb_vfs_destroy(smb_export_t *, smb_vfs_t *);
315331Samw
325331Samw /*
33*12508Samw@Sun.COM * If a hold on the specified VFS has already been taken
34*12508Samw@Sun.COM * then only increment the reference count of the corresponding
35*12508Samw@Sun.COM * smb_vfs_t structure. If no smb_vfs_t structure has been created
36*12508Samw@Sun.COM * yet for the specified VFS then create one and take a hold on
37*12508Samw@Sun.COM * the VFS.
385331Samw */
39*12508Samw@Sun.COM int
smb_vfs_hold(smb_export_t * se,vfs_t * vfsp)40*12508Samw@Sun.COM smb_vfs_hold(smb_export_t *se, vfs_t *vfsp)
415331Samw {
425331Samw smb_vfs_t *smb_vfs;
435331Samw vnode_t *rootvp;
44*12508Samw@Sun.COM int rc;
455331Samw
46*12508Samw@Sun.COM if (se == NULL || vfsp == NULL)
47*12508Samw@Sun.COM return (EINVAL);
48*12508Samw@Sun.COM
49*12508Samw@Sun.COM smb_llist_enter(&se->e_vfs_list, RW_WRITER);
505331Samw
51*12508Samw@Sun.COM if ((smb_vfs = smb_vfs_find(se, vfsp)) != NULL) {
52*12508Samw@Sun.COM smb_vfs->sv_refcnt++;
535331Samw DTRACE_PROBE1(smb_vfs_hold_hit, smb_vfs_t *, smb_vfs);
54*12508Samw@Sun.COM smb_llist_exit(&se->e_vfs_list);
55*12508Samw@Sun.COM return (0);
565331Samw }
57*12508Samw@Sun.COM
58*12508Samw@Sun.COM if ((rc = VFS_ROOT(vfsp, &rootvp)) != 0) {
59*12508Samw@Sun.COM smb_llist_exit(&se->e_vfs_list);
60*12508Samw@Sun.COM return (rc);
61*12508Samw@Sun.COM }
62*12508Samw@Sun.COM
63*12508Samw@Sun.COM smb_vfs = kmem_cache_alloc(se->e_cache_vfs, KM_SLEEP);
645331Samw
655331Samw bzero(smb_vfs, sizeof (smb_vfs_t));
665331Samw
675331Samw smb_vfs->sv_magic = SMB_VFS_MAGIC;
685331Samw smb_vfs->sv_refcnt = 1;
695331Samw smb_vfs->sv_vfsp = vfsp;
705331Samw /*
715331Samw * We have a hold on the root vnode of the file system
725331Samw * from the VFS_ROOT call above.
735331Samw */
745331Samw smb_vfs->sv_rootvp = rootvp;
75*12508Samw@Sun.COM
76*12508Samw@Sun.COM smb_llist_insert_head(&se->e_vfs_list, smb_vfs);
775331Samw DTRACE_PROBE1(smb_vfs_hold_miss, smb_vfs_t *, smb_vfs);
78*12508Samw@Sun.COM smb_llist_exit(&se->e_vfs_list);
79*12508Samw@Sun.COM
80*12508Samw@Sun.COM return (0);
815331Samw }
825331Samw
835331Samw /*
845331Samw * smb_vfs_rele
855331Samw *
865331Samw * Decrements the reference count of the fs passed in. If the reference count
875331Samw * drops to zero the smb_vfs_t structure associated with the fs is freed.
885331Samw */
895331Samw void
smb_vfs_rele(smb_export_t * se,vfs_t * vfsp)90*12508Samw@Sun.COM smb_vfs_rele(smb_export_t *se, vfs_t *vfsp)
915331Samw {
925331Samw smb_vfs_t *smb_vfs;
935331Samw
945331Samw ASSERT(vfsp);
955331Samw
96*12508Samw@Sun.COM smb_llist_enter(&se->e_vfs_list, RW_WRITER);
97*12508Samw@Sun.COM smb_vfs = smb_vfs_find(se, vfsp);
98*12508Samw@Sun.COM DTRACE_PROBE1(smb_vfs_release, smb_vfs_t *, smb_vfs)
995331Samw if (smb_vfs) {
1005331Samw ASSERT(smb_vfs->sv_refcnt);
1015331Samw if (--smb_vfs->sv_refcnt == 0) {
102*12508Samw@Sun.COM smb_llist_remove(&se->e_vfs_list, smb_vfs);
103*12508Samw@Sun.COM smb_llist_exit(&se->e_vfs_list);
104*12508Samw@Sun.COM smb_vfs_destroy(se, smb_vfs);
1055331Samw return;
1065331Samw }
1075331Samw }
108*12508Samw@Sun.COM smb_llist_exit(&se->e_vfs_list);
1095331Samw }
1105331Samw
1115331Samw /*
1125331Samw * smb_vfs_rele_all()
1135331Samw *
1145331Samw * Release all holds on root vnodes of file systems which were taken
1155331Samw * due to the existence of at least one enabled share on the file system.
1165331Samw * Called at driver close time.
1175331Samw */
1185331Samw void
smb_vfs_rele_all(smb_export_t * se)119*12508Samw@Sun.COM smb_vfs_rele_all(smb_export_t *se)
1205331Samw {
1215331Samw smb_vfs_t *smb_vfs;
1225331Samw
123*12508Samw@Sun.COM smb_llist_enter(&se->e_vfs_list, RW_WRITER);
124*12508Samw@Sun.COM while ((smb_vfs = smb_llist_head(&se->e_vfs_list)) != NULL) {
1255331Samw
1265331Samw ASSERT(smb_vfs->sv_magic == SMB_VFS_MAGIC);
1275331Samw DTRACE_PROBE1(smb_vfs_rele_all_hit, smb_vfs_t *, smb_vfs);
128*12508Samw@Sun.COM smb_llist_remove(&se->e_vfs_list, smb_vfs);
129*12508Samw@Sun.COM smb_vfs_destroy(se, smb_vfs);
1305331Samw }
131*12508Samw@Sun.COM smb_llist_exit(&se->e_vfs_list);
1325331Samw }
1335331Samw
1345331Samw /*
1355331Samw * Goes through the list of smb_vfs_t structure and returns the one matching
1365331Samw * the vnode passed in. If no match is found a NULL pointer is returned.
1375331Samw *
1385331Samw * The list of smb_vfs_t structures has to have been entered prior calling
1395331Samw * this function.
1405331Samw */
1415331Samw static smb_vfs_t *
smb_vfs_find(smb_export_t * se,vfs_t * vfsp)142*12508Samw@Sun.COM smb_vfs_find(smb_export_t *se, vfs_t *vfsp)
1435331Samw {
144*12508Samw@Sun.COM smb_vfs_t *smb_vfs;
1455331Samw
146*12508Samw@Sun.COM smb_vfs = smb_llist_head(&se->e_vfs_list);
1475331Samw while (smb_vfs) {
1485331Samw ASSERT(smb_vfs->sv_magic == SMB_VFS_MAGIC);
149*12508Samw@Sun.COM if (smb_vfs->sv_vfsp == vfsp)
1505331Samw return (smb_vfs);
151*12508Samw@Sun.COM smb_vfs = smb_llist_next(&se->e_vfs_list, smb_vfs);
1525331Samw }
153*12508Samw@Sun.COM
1545331Samw return (NULL);
1555331Samw }
1567348SJose.Borrego@Sun.COM
157*12508Samw@Sun.COM static void
smb_vfs_destroy(smb_export_t * se,smb_vfs_t * smb_vfs)158*12508Samw@Sun.COM smb_vfs_destroy(smb_export_t *se, smb_vfs_t *smb_vfs)
1597348SJose.Borrego@Sun.COM {
160*12508Samw@Sun.COM VN_RELE(smb_vfs->sv_rootvp);
161*12508Samw@Sun.COM smb_vfs->sv_magic = (uint32_t)~SMB_VFS_MAGIC;
162*12508Samw@Sun.COM kmem_cache_free(se->e_cache_vfs, smb_vfs);
1637348SJose.Borrego@Sun.COM }
164