bsd-family-tree: Sync with FreeBSD.
[dragonfly.git] / sys / vfs / smbfs / smbfs_vfsops.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 2000-2001, Boris Popov
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Boris Popov.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * $FreeBSD: src/sys/fs/smbfs/smbfs_vfsops.c,v 1.2.2.5 2003/01/17 08:20:26 tjr Exp $
33 */
4705d921
SW
34
35#ifndef KLD_MODULE
984263bc
MD
36#include "opt_netsmb.h"
37#ifndef NETSMB
38#error "SMBFS requires option NETSMB"
39#endif
4705d921 40#endif
984263bc
MD
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/proc.h>
45#include <sys/kernel.h>
46#include <sys/sysctl.h>
47#include <sys/vnode.h>
48#include <sys/mount.h>
49#include <sys/stat.h>
50#include <sys/malloc.h>
51#include <sys/module.h>
52
53
d2438d69
MD
54#include <netproto/smb/smb.h>
55#include <netproto/smb/smb_conn.h>
56#include <netproto/smb/smb_subr.h>
57#include <netproto/smb/smb_dev.h>
984263bc 58
1f2de5d4
MD
59#include "smbfs.h"
60#include "smbfs_node.h"
61#include "smbfs_subr.h"
984263bc
MD
62
63#include <sys/buf.h>
64
66a1ddf5 65extern struct vop_ops smbfs_vnode_vops;
0961aa92 66
984263bc
MD
67int smbfs_debuglevel = 0;
68
69static int smbfs_version = SMBFS_VERSION;
70
984263bc
MD
71SYSCTL_NODE(_vfs, OID_AUTO, smbfs, CTLFLAG_RW, 0, "SMB/CIFS file system");
72SYSCTL_INT(_vfs_smbfs, OID_AUTO, version, CTLFLAG_RD, &smbfs_version, 0, "");
73SYSCTL_INT(_vfs_smbfs, OID_AUTO, debuglevel, CTLFLAG_RW, &smbfs_debuglevel, 0, "");
74
75static MALLOC_DEFINE(M_SMBFSHASH, "SMBFS hash", "SMBFS hash table");
76
77
acde96db 78static int smbfs_mount(struct mount *, char *, caddr_t, struct ucred *);
984263bc 79static int smbfs_root(struct mount *, struct vnode **);
acde96db 80static int smbfs_statfs(struct mount *, struct statfs *, struct ucred *);
87de5057 81static int smbfs_sync(struct mount *, int);
acde96db 82static int smbfs_unmount(struct mount *, int);
984263bc
MD
83static int smbfs_init(struct vfsconf *vfsp);
84static int smbfs_uninit(struct vfsconf *vfsp);
85
984263bc 86static struct vfsops smbfs_vfsops = {
00369c4a 87 .vfs_flags = 0,
43c45e8f
HP
88 .vfs_mount = smbfs_mount,
89 .vfs_unmount = smbfs_unmount,
90 .vfs_root = smbfs_root,
91 .vfs_statfs = smbfs_statfs,
92 .vfs_sync = smbfs_sync,
93 .vfs_init = smbfs_init,
94 .vfs_uninit = smbfs_uninit
984263bc
MD
95};
96
97
98VFS_SET(smbfs_vfsops, smbfs, VFCF_NETWORK);
e5e63c20 99MODULE_VERSION(smbfs, 1);
984263bc
MD
100
101MODULE_DEPEND(smbfs, netsmb, NSMB_VERSION, NSMB_VERSION, NSMB_VERSION);
6288c7ec 102MODULE_DEPEND(smbfs, libiconv, 1, 1, 2);
984263bc
MD
103MODULE_DEPEND(smbfs, libmchain, 1, 1, 1);
104
4168cfe8 105int smbfs_pbuf_freecnt = -1; /* start out unlimited */
984263bc
MD
106
107static int
acde96db 108smbfs_mount(struct mount *mp, char *path, caddr_t data, struct ucred *cred)
984263bc
MD
109{
110 struct smbfs_args args; /* will hold data from mount request */
111 struct smbmount *smp = NULL;
112 struct smb_vc *vcp;
113 struct smb_share *ssp = NULL;
114 struct vnode *vp;
115 struct smb_cred scred;
984263bc 116 int error;
fd74079f 117 int hsize;
984263bc
MD
118 char *pc, *pe;
119
120 if (data == NULL) {
086c1d7e 121 kprintf("missing data argument\n");
984263bc
MD
122 return EINVAL;
123 }
124 if (mp->mnt_flag & MNT_UPDATE) {
086c1d7e 125 kprintf("MNT_UPDATE not implemented");
984263bc
MD
126 return EOPNOTSUPP;
127 }
128 error = copyin(data, (caddr_t)&args, sizeof(struct smbfs_args));
129 if (error)
130 return error;
131 if (args.version != SMBFS_VERSION) {
086c1d7e 132 kprintf("mount version mismatch: kernel=%d, mount=%d\n",
984263bc
MD
133 SMBFS_VERSION, args.version);
134 return EINVAL;
135 }
acde96db 136 smb_makescred(&scred, curthread, cred);
984263bc
MD
137 error = smb_dev2share(args.dev, SMBM_EXEC, &scred, &ssp);
138 if (error) {
086c1d7e 139 kprintf("invalid device handle %d (%d)\n", args.dev, error);
984263bc
MD
140 return error;
141 }
142 vcp = SSTOVC(ssp);
fef8985e 143 smb_share_unlock(ssp, 0);
984263bc
MD
144 mp->mnt_stat.f_iosize = SSTOVC(ssp)->vc_txmax;
145
2f3e94be
SW
146 smp = kmalloc(sizeof(*smp), M_SMBFSDATA, M_WAITOK | M_USE_RESERVE | M_ZERO);
147 mp->mnt_data = (qaddr_t)smp;
acde96db 148 smp->sm_cred = crhold(cred);
fd74079f
MD
149
150 hsize = vfs_inodehashsize();
151 smp->sm_hash = hashinit(hsize, M_SMBFSHASH, &smp->sm_hashlen);
984263bc
MD
152 if (smp->sm_hash == NULL)
153 goto bad;
f2770c70 154 lockinit(&smp->sm_hashlock, "smbfsh", 0, 0);
984263bc
MD
155 smp->sm_share = ssp;
156 smp->sm_root = NULL;
2f3e94be 157 smp->sm_args = args;
984263bc
MD
158 smp->sm_caseopt = args.caseopt;
159 smp->sm_args.file_mode = (smp->sm_args.file_mode &
160 (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFREG;
161 smp->sm_args.dir_mode = (smp->sm_args.dir_mode &
162 (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFDIR;
163
164/* simple_lock_init(&smp->sm_npslock);*/
984263bc
MD
165 pc = mp->mnt_stat.f_mntfromname;
166 pe = pc + sizeof(mp->mnt_stat.f_mntfromname);
167 bzero(pc, MNAMELEN);
168 *pc++ = '/';
169 *pc++ = '/';
170 pc=index(strncpy(pc, vcp->vc_username, pe - pc - 2), 0);
171 if (pc < pe-1) {
172 *(pc++) = '@';
173 pc = index(strncpy(pc, vcp->vc_srvname, pe - pc - 2), 0);
174 if (pc < pe - 1) {
175 *(pc++) = '/';
176 strncpy(pc, ssp->ss_name, pe - pc - 2);
177 }
178 }
179 /* protect against invalid mount points */
180 smp->sm_args.mount_point[sizeof(smp->sm_args.mount_point) - 1] = '\0';
181 vfs_getnewfsid(mp);
0961aa92 182
66a1ddf5 183 vfs_add_vnodeops(mp, &smbfs_vnode_vops, &mp->mnt_vn_norm_ops);
0961aa92 184
984263bc
MD
185 error = smbfs_root(mp, &vp);
186 if (error)
187 goto bad;
a11aaa81 188 vn_unlock(vp);
ee173d09 189 SMBVDEBUG("root.v_refcnt = %08x\n", vp->v_refcnt);
984263bc
MD
190
191#ifdef DIAGNOSTICS
192 SMBERROR("mp=%p\n", mp);
193#endif
194 return error;
195bad:
2f3e94be 196 if (smp) {
acde96db
MD
197 if (smp->sm_cred)
198 crfree(smp->sm_cred);
984263bc 199 if (smp->sm_hash)
e46d128d
SW
200 hashdestroy(smp->sm_hash, M_SMBFSHASH,
201 smp->sm_hashlen);
984263bc 202 lockdestroy(&smp->sm_hashlock);
efda3bd0 203 kfree(smp, M_SMBFSDATA);
984263bc
MD
204 }
205 if (ssp)
206 smb_share_put(ssp, &scred);
2f3e94be 207 return error;
984263bc
MD
208}
209
210/* Unmount the filesystem described by mp. */
211static int
acde96db 212smbfs_unmount(struct mount *mp, int mntflags)
984263bc
MD
213{
214 struct smbmount *smp = VFSTOSMBFS(mp);
215 struct smb_cred scred;
216 int error, flags;
217
218 SMBVDEBUG("smbfs_unmount: flags=%04x\n", mntflags);
219 flags = 0;
220 if (mntflags & MNT_FORCE)
221 flags |= FORCECLOSE;
222 /*
223 * Keep trying to flush the vnode list for the mount while
224 * some are still busy and we are making progress towards
225 * making them not busy. This is needed because smbfs vnodes
226 * reference their parent directory but may appear after their
227 * parent in the list; one pass over the vnode list is not
228 * sufficient in this case.
229 */
230 do {
231 smp->sm_didrele = 0;
232 /* There is 1 extra root vnode reference from smbfs_mount(). */
233 error = vflush(mp, 1, flags);
234 } while (error == EBUSY && smp->sm_didrele != 0);
235 if (error)
236 return error;
acde96db 237 smb_makescred(&scred, curthread, smp->sm_cred);
d8b10ae3
MD
238 error = smb_share_lock(smp->sm_share, LK_EXCLUSIVE);
239 if (error)
240 goto out;
984263bc
MD
241 smb_share_put(smp->sm_share, &scred);
242 mp->mnt_data = (qaddr_t)0;
243
acde96db
MD
244 if (smp->sm_cred)
245 crfree(smp->sm_cred);
984263bc 246 if (smp->sm_hash)
e46d128d 247 hashdestroy(smp->sm_hash, M_SMBFSHASH, smp->sm_hashlen);
984263bc 248 lockdestroy(&smp->sm_hashlock);
efda3bd0 249 kfree(smp, M_SMBFSDATA);
984263bc 250 mp->mnt_flag &= ~MNT_LOCAL;
d8b10ae3 251out:
984263bc
MD
252 return error;
253}
254
255/*
256 * Return locked root vnode of a filesystem
257 */
258static int
259smbfs_root(struct mount *mp, struct vnode **vpp)
260{
dadab5e9 261 struct thread *td = curthread; /* XXX */
984263bc
MD
262 struct smbmount *smp = VFSTOSMBFS(mp);
263 struct vnode *vp;
264 struct smbnode *np;
265 struct smbfattr fattr;
dadab5e9 266 struct ucred *cred;
984263bc
MD
267 struct smb_cred scred;
268 int error;
269
270 if (smp == NULL) {
271 SMBERROR("smp == NULL (bug in umount)\n");
272 return EINVAL;
273 }
274 if (smp->sm_root) {
275 *vpp = SMBTOV(smp->sm_root);
87de5057 276 return vget(*vpp, LK_EXCLUSIVE | LK_RETRY);
984263bc 277 }
61670a01
MD
278 if (td->td_proc)
279 cred = td->td_proc->p_ucred;
280 else
281 cred = proc0.p_ucred;
282
dadab5e9 283 smb_makescred(&scred, td, cred);
984263bc
MD
284 error = smbfs_smb_lookup(NULL, NULL, 0, &fattr, &scred);
285 if (error)
286 return error;
287 error = smbfs_nget(mp, NULL, "TheRooT", 7, &fattr, &vp);
288 if (error)
289 return error;
2247fe02 290 vsetflags(vp, VROOT);
984263bc
MD
291 np = VTOSMB(vp);
292 smp->sm_root = np;
293 *vpp = vp;
294 return 0;
295}
296
984263bc
MD
297/*ARGSUSED*/
298int
299smbfs_init(struct vfsconf *vfsp)
300{
2f0acc22 301 smbfs_pbuf_freecnt = nswbuf_kva / 2 + 1;
984263bc
MD
302 SMBVDEBUG("done.\n");
303 return 0;
304}
305
306/*ARGSUSED*/
307int
308smbfs_uninit(struct vfsconf *vfsp)
309{
984263bc
MD
310 SMBVDEBUG("done.\n");
311 return 0;
312}
313
314/*
315 * smbfs_statfs call
316 */
317int
acde96db 318smbfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
984263bc
MD
319{
320 struct smbmount *smp = VFSTOSMBFS(mp);
321 struct smbnode *np = smp->sm_root;
322 struct smb_share *ssp = smp->sm_share;
323 struct smb_cred scred;
324 int error = 0;
325
326 if (np == NULL)
327 return EINVAL;
328
329 sbp->f_iosize = SSTOVC(ssp)->vc_txmax; /* optimal transfer block size */
330 sbp->f_spare2 = 0; /* placeholder */
acde96db 331 smb_makescred(&scred, curthread, cred);
984263bc
MD
332
333 if (SMB_DIALECT(SSTOVC(ssp)) >= SMB_DIALECT_LANMAN2_0)
334 error = smbfs_smb_statfs2(ssp, sbp, &scred);
335 else
336 error = smbfs_smb_statfs(ssp, sbp, &scred);
337 if (error)
338 return error;
339 sbp->f_flags = 0; /* copy of mount exported flags */
340 if (sbp != &mp->mnt_stat) {
341 sbp->f_fsid = mp->mnt_stat.f_fsid; /* file system id */
342 sbp->f_owner = mp->mnt_stat.f_owner; /* user that mounted the filesystem */
343 sbp->f_type = mp->mnt_vfc->vfc_typenum; /* type of filesystem */
984263bc
MD
344 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
345 }
346 strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
347 return 0;
348}
349
350/*
351 * Flush out the buffer cache
352 */
353/* ARGSUSED */
354static int
87de5057 355smbfs_sync(struct mount *mp, int waitfor)
984263bc
MD
356{
357 struct vnode *vp;
358 int error, allerror = 0;
359 /*
360 * Force stale buffer cache information to be flushed.
361 */
362loop:
363 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
364 vp != NULL;
365 vp = TAILQ_NEXT(vp, v_nmntvnodes)) {
366 /*
367 * If the vnode that we are about to sync is no longer
368 * associated with this mount point, start over.
369 */
370 if (vp->v_mount != mp)
371 goto loop;
a11aaa81 372 if (vn_islocked(vp) || RB_EMPTY(&vp->v_rbdirty_tree) ||
28271622 373 (waitfor & MNT_LAZY))
984263bc 374 continue;
87de5057 375 if (vget(vp, LK_EXCLUSIVE))
984263bc 376 goto loop;
52174f71 377 error = VOP_FSYNC(vp, waitfor, 0);
984263bc
MD
378 if (error)
379 allerror = error;
380 vput(vp);
381 }
382 return (allerror);
383}