proc->thread stage 4: rework the VFS and DEVICE subsystems to take thread
[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 $
dadab5e9 33 * $DragonFly: src/sys/vfs/smbfs/smbfs_vfsops.c,v 1.3 2003/06/25 03:55:52 dillon Exp $
984263bc
MD
34 */
35#include "opt_netsmb.h"
36#ifndef NETSMB
37#error "SMBFS requires option NETSMB"
38#endif
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/proc.h>
43#include <sys/kernel.h>
44#include <sys/sysctl.h>
45#include <sys/vnode.h>
46#include <sys/mount.h>
47#include <sys/stat.h>
48#include <sys/malloc.h>
49#include <sys/module.h>
50
51
52#include <netsmb/smb.h>
53#include <netsmb/smb_conn.h>
54#include <netsmb/smb_subr.h>
55#include <netsmb/smb_dev.h>
56
57#include <fs/smbfs/smbfs.h>
58#include <fs/smbfs/smbfs_node.h>
59#include <fs/smbfs/smbfs_subr.h>
60
61#include <sys/buf.h>
62
63int smbfs_debuglevel = 0;
64
65static int smbfs_version = SMBFS_VERSION;
66
67#ifdef SMBFS_USEZONE
68#include <vm/vm.h>
69#include <vm/vm_extern.h>
70#include <vm/vm_zone.h>
71
72vm_zone_t smbfsmount_zone;
73#endif
74
75SYSCTL_NODE(_vfs, OID_AUTO, smbfs, CTLFLAG_RW, 0, "SMB/CIFS file system");
76SYSCTL_INT(_vfs_smbfs, OID_AUTO, version, CTLFLAG_RD, &smbfs_version, 0, "");
77SYSCTL_INT(_vfs_smbfs, OID_AUTO, debuglevel, CTLFLAG_RW, &smbfs_debuglevel, 0, "");
78
79static MALLOC_DEFINE(M_SMBFSHASH, "SMBFS hash", "SMBFS hash table");
80
81
82static int smbfs_mount(struct mount *, char *, caddr_t,
dadab5e9
MD
83 struct nameidata *, struct thread *);
84static int smbfs_quotactl(struct mount *, int, uid_t, caddr_t, struct thread *);
984263bc 85static int smbfs_root(struct mount *, struct vnode **);
dadab5e9
MD
86static int smbfs_start(struct mount *, int, struct thread *);
87static int smbfs_statfs(struct mount *, struct statfs *, struct thread *);
88static int smbfs_sync(struct mount *, int, struct ucred *, struct thread *);
89static int smbfs_unmount(struct mount *, int, struct thread *);
984263bc
MD
90static int smbfs_init(struct vfsconf *vfsp);
91static int smbfs_uninit(struct vfsconf *vfsp);
92
93#if __FreeBSD_version < 400009
94static int smbfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp);
95static int smbfs_fhtovp(struct mount *, struct fid *,
96 struct sockaddr *, struct vnode **, int *,
97 struct ucred **);
98static int smbfs_vptofh(struct vnode *, struct fid *);
99#endif
100
101static struct vfsops smbfs_vfsops = {
102 smbfs_mount,
103 smbfs_start,
104 smbfs_unmount,
105 smbfs_root,
106 smbfs_quotactl,
107 smbfs_statfs,
108 smbfs_sync,
109#if __FreeBSD_version > 400008
110 vfs_stdvget,
111 vfs_stdfhtovp, /* shouldn't happen */
112 vfs_stdcheckexp,
113 vfs_stdvptofh, /* shouldn't happen */
114#else
115 smbfs_vget,
116 smbfs_fhtovp,
117 smbfs_vptofh,
118#endif
119 smbfs_init,
120 smbfs_uninit,
121 vfs_stdextattrctl
122};
123
124
125VFS_SET(smbfs_vfsops, smbfs, VFCF_NETWORK);
126
127MODULE_DEPEND(smbfs, netsmb, NSMB_VERSION, NSMB_VERSION, NSMB_VERSION);
128MODULE_DEPEND(smbfs, libiconv, 1, 1, 1);
129MODULE_DEPEND(smbfs, libmchain, 1, 1, 1);
130
131int smbfs_pbuf_freecnt = -1; /* start out unlimited */
132
133static int
134smbfs_mount(struct mount *mp, char *path, caddr_t data,
dadab5e9 135 struct nameidata *ndp, struct thread *td)
984263bc
MD
136{
137 struct smbfs_args args; /* will hold data from mount request */
138 struct smbmount *smp = NULL;
139 struct smb_vc *vcp;
140 struct smb_share *ssp = NULL;
141 struct vnode *vp;
142 struct smb_cred scred;
dadab5e9 143 struct ucred *cred;
984263bc
MD
144 size_t size;
145 int error;
146 char *pc, *pe;
147
dadab5e9
MD
148 KKASSERT(td->td_proc);
149 cred = td->td_proc->p_ucred;
150
984263bc
MD
151 if (data == NULL) {
152 printf("missing data argument\n");
153 return EINVAL;
154 }
155 if (mp->mnt_flag & MNT_UPDATE) {
156 printf("MNT_UPDATE not implemented");
157 return EOPNOTSUPP;
158 }
159 error = copyin(data, (caddr_t)&args, sizeof(struct smbfs_args));
160 if (error)
161 return error;
162 if (args.version != SMBFS_VERSION) {
163 printf("mount version mismatch: kernel=%d, mount=%d\n",
164 SMBFS_VERSION, args.version);
165 return EINVAL;
166 }
dadab5e9 167 smb_makescred(&scred, td, cred);
984263bc
MD
168 error = smb_dev2share(args.dev, SMBM_EXEC, &scred, &ssp);
169 if (error) {
170 printf("invalid device handle %d (%d)\n", args.dev, error);
171 return error;
172 }
173 vcp = SSTOVC(ssp);
dadab5e9 174 smb_share_unlock(ssp, 0, td);
984263bc
MD
175 mp->mnt_stat.f_iosize = SSTOVC(ssp)->vc_txmax;
176
177#ifdef SMBFS_USEZONE
178 smp = zalloc(smbfsmount_zone);
179#else
180 MALLOC(smp, struct smbmount*, sizeof(*smp), M_SMBFSDATA, M_USE_RESERVE);
181#endif
182 if (smp == NULL) {
183 printf("could not alloc smbmount\n");
184 error = ENOMEM;
185 goto bad;
186 }
187 bzero(smp, sizeof(*smp));
188 mp->mnt_data = (qaddr_t)smp;
189 smp->sm_hash = hashinit(desiredvnodes, M_SMBFSHASH, &smp->sm_hashlen);
190 if (smp->sm_hash == NULL)
191 goto bad;
192 lockinit(&smp->sm_hashlock, PVFS, "smbfsh", 0, 0);
193 smp->sm_share = ssp;
194 smp->sm_root = NULL;
195 smp->sm_args = args;
196 smp->sm_caseopt = args.caseopt;
197 smp->sm_args.file_mode = (smp->sm_args.file_mode &
198 (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFREG;
199 smp->sm_args.dir_mode = (smp->sm_args.dir_mode &
200 (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFDIR;
201
202/* simple_lock_init(&smp->sm_npslock);*/
203 error = copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
204 if (error)
205 goto bad;
206 bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
207 pc = mp->mnt_stat.f_mntfromname;
208 pe = pc + sizeof(mp->mnt_stat.f_mntfromname);
209 bzero(pc, MNAMELEN);
210 *pc++ = '/';
211 *pc++ = '/';
212 pc=index(strncpy(pc, vcp->vc_username, pe - pc - 2), 0);
213 if (pc < pe-1) {
214 *(pc++) = '@';
215 pc = index(strncpy(pc, vcp->vc_srvname, pe - pc - 2), 0);
216 if (pc < pe - 1) {
217 *(pc++) = '/';
218 strncpy(pc, ssp->ss_name, pe - pc - 2);
219 }
220 }
221 /* protect against invalid mount points */
222 smp->sm_args.mount_point[sizeof(smp->sm_args.mount_point) - 1] = '\0';
223 vfs_getnewfsid(mp);
224 error = smbfs_root(mp, &vp);
225 if (error)
226 goto bad;
dadab5e9 227 VOP_UNLOCK(vp, 0, td);
984263bc
MD
228 SMBVDEBUG("root.v_usecount = %d\n", vp->v_usecount);
229
230#ifdef DIAGNOSTICS
231 SMBERROR("mp=%p\n", mp);
232#endif
233 return error;
234bad:
235 if (smp) {
236 if (smp->sm_hash)
237 free(smp->sm_hash, M_SMBFSHASH);
238 lockdestroy(&smp->sm_hashlock);
239#ifdef SMBFS_USEZONE
240 zfree(smbfsmount_zone, smp);
241#else
242 free(smp, M_SMBFSDATA);
243#endif
244 }
245 if (ssp)
246 smb_share_put(ssp, &scred);
247 return error;
248}
249
250/* Unmount the filesystem described by mp. */
251static int
dadab5e9 252smbfs_unmount(struct mount *mp, int mntflags, struct thread *td)
984263bc
MD
253{
254 struct smbmount *smp = VFSTOSMBFS(mp);
255 struct smb_cred scred;
dadab5e9 256 struct ucred *cred;
984263bc
MD
257 int error, flags;
258
dadab5e9
MD
259 KKASSERT(td->td_proc);
260 cred = td->td_proc->p_ucred;
261
984263bc
MD
262 SMBVDEBUG("smbfs_unmount: flags=%04x\n", mntflags);
263 flags = 0;
264 if (mntflags & MNT_FORCE)
265 flags |= FORCECLOSE;
266 /*
267 * Keep trying to flush the vnode list for the mount while
268 * some are still busy and we are making progress towards
269 * making them not busy. This is needed because smbfs vnodes
270 * reference their parent directory but may appear after their
271 * parent in the list; one pass over the vnode list is not
272 * sufficient in this case.
273 */
274 do {
275 smp->sm_didrele = 0;
276 /* There is 1 extra root vnode reference from smbfs_mount(). */
277 error = vflush(mp, 1, flags);
278 } while (error == EBUSY && smp->sm_didrele != 0);
279 if (error)
280 return error;
dadab5e9 281 smb_makescred(&scred, td, cred);
984263bc
MD
282 smb_share_put(smp->sm_share, &scred);
283 mp->mnt_data = (qaddr_t)0;
284
285 if (smp->sm_hash)
286 free(smp->sm_hash, M_SMBFSHASH);
287 lockdestroy(&smp->sm_hashlock);
288#ifdef SMBFS_USEZONE
289 zfree(smbfsmount_zone, smp);
290#else
291 free(smp, M_SMBFSDATA);
292#endif
293 mp->mnt_flag &= ~MNT_LOCAL;
294 return error;
295}
296
297/*
298 * Return locked root vnode of a filesystem
299 */
300static int
301smbfs_root(struct mount *mp, struct vnode **vpp)
302{
dadab5e9 303 struct thread *td = curthread; /* XXX */
984263bc
MD
304 struct smbmount *smp = VFSTOSMBFS(mp);
305 struct vnode *vp;
306 struct smbnode *np;
307 struct smbfattr fattr;
dadab5e9 308 struct ucred *cred;
984263bc
MD
309 struct smb_cred scred;
310 int error;
311
dadab5e9
MD
312 KKASSERT(td->td_proc);
313 cred = td->td_proc->p_ucred;
314
984263bc
MD
315 if (smp == NULL) {
316 SMBERROR("smp == NULL (bug in umount)\n");
317 return EINVAL;
318 }
319 if (smp->sm_root) {
320 *vpp = SMBTOV(smp->sm_root);
dadab5e9 321 return vget(*vpp, LK_EXCLUSIVE | LK_RETRY, td);
984263bc 322 }
dadab5e9 323 smb_makescred(&scred, td, cred);
984263bc
MD
324 error = smbfs_smb_lookup(NULL, NULL, 0, &fattr, &scred);
325 if (error)
326 return error;
327 error = smbfs_nget(mp, NULL, "TheRooT", 7, &fattr, &vp);
328 if (error)
329 return error;
330 vp->v_flag |= VROOT;
331 np = VTOSMB(vp);
332 smp->sm_root = np;
333 *vpp = vp;
334 return 0;
335}
336
337/*
338 * Vfs start routine, a no-op.
339 */
340/* ARGSUSED */
341static int
dadab5e9 342smbfs_start(mp, flags, td)
984263bc
MD
343 struct mount *mp;
344 int flags;
dadab5e9 345 struct thread *td;
984263bc
MD
346{
347 SMBVDEBUG("flags=%04x\n", flags);
348 return 0;
349}
350
351/*
352 * Do operations associated with quotas, not supported
353 */
354/* ARGSUSED */
355static int
dadab5e9 356smbfs_quotactl(mp, cmd, uid, arg, td)
984263bc
MD
357 struct mount *mp;
358 int cmd;
359 uid_t uid;
360 caddr_t arg;
dadab5e9 361 struct thread *td;
984263bc
MD
362{
363 SMBVDEBUG("return EOPNOTSUPP\n");
364 return EOPNOTSUPP;
365}
366
367/*ARGSUSED*/
368int
369smbfs_init(struct vfsconf *vfsp)
370{
371 smb_checksmp();
372
373#ifdef SMBFS_USEZONE
374 smbfsmount_zone = zinit("SMBFSMOUNT", sizeof(struct smbmount), 0, 0, 1);
375#endif
376 smbfs_pbuf_freecnt = nswbuf / 2 + 1;
377 SMBVDEBUG("done.\n");
378 return 0;
379}
380
381/*ARGSUSED*/
382int
383smbfs_uninit(struct vfsconf *vfsp)
384{
385
386 SMBVDEBUG("done.\n");
387 return 0;
388}
389
390/*
391 * smbfs_statfs call
392 */
393int
dadab5e9 394smbfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
984263bc
MD
395{
396 struct smbmount *smp = VFSTOSMBFS(mp);
397 struct smbnode *np = smp->sm_root;
398 struct smb_share *ssp = smp->sm_share;
399 struct smb_cred scred;
dadab5e9 400 struct ucred *cred;
984263bc
MD
401 int error = 0;
402
dadab5e9
MD
403 KKASSERT(td->td_proc);
404 cred = td->td_proc->p_ucred;
405
984263bc
MD
406 if (np == NULL)
407 return EINVAL;
408
409 sbp->f_iosize = SSTOVC(ssp)->vc_txmax; /* optimal transfer block size */
410 sbp->f_spare2 = 0; /* placeholder */
dadab5e9 411 smb_makescred(&scred, td, cred);
984263bc
MD
412
413 if (SMB_DIALECT(SSTOVC(ssp)) >= SMB_DIALECT_LANMAN2_0)
414 error = smbfs_smb_statfs2(ssp, sbp, &scred);
415 else
416 error = smbfs_smb_statfs(ssp, sbp, &scred);
417 if (error)
418 return error;
419 sbp->f_flags = 0; /* copy of mount exported flags */
420 if (sbp != &mp->mnt_stat) {
421 sbp->f_fsid = mp->mnt_stat.f_fsid; /* file system id */
422 sbp->f_owner = mp->mnt_stat.f_owner; /* user that mounted the filesystem */
423 sbp->f_type = mp->mnt_vfc->vfc_typenum; /* type of filesystem */
424 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
425 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
426 }
427 strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
428 return 0;
429}
430
431/*
432 * Flush out the buffer cache
433 */
434/* ARGSUSED */
435static int
dadab5e9 436smbfs_sync(mp, waitfor, cred, td)
984263bc
MD
437 struct mount *mp;
438 int waitfor;
439 struct ucred *cred;
dadab5e9 440 struct thread *td;
984263bc
MD
441{
442 struct vnode *vp;
443 int error, allerror = 0;
444 /*
445 * Force stale buffer cache information to be flushed.
446 */
447loop:
448 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
449 vp != NULL;
450 vp = TAILQ_NEXT(vp, v_nmntvnodes)) {
451 /*
452 * If the vnode that we are about to sync is no longer
453 * associated with this mount point, start over.
454 */
455 if (vp->v_mount != mp)
456 goto loop;
457 if (VOP_ISLOCKED(vp, NULL) || TAILQ_EMPTY(&vp->v_dirtyblkhd) ||
458 waitfor == MNT_LAZY)
459 continue;
dadab5e9 460 if (vget(vp, LK_EXCLUSIVE, td))
984263bc 461 goto loop;
dadab5e9 462 error = VOP_FSYNC(vp, cred, waitfor, td);
984263bc
MD
463 if (error)
464 allerror = error;
465 vput(vp);
466 }
467 return (allerror);
468}
469
470#if __FreeBSD_version < 400009
471/*
472 * smbfs flat namespace lookup. Unsupported.
473 */
474/* ARGSUSED */
475static int smbfs_vget(mp, ino, vpp)
476 struct mount *mp;
477 ino_t ino;
478 struct vnode **vpp;
479{
480 return (EOPNOTSUPP);
481}
482
483/* ARGSUSED */
484static int smbfs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
485 struct mount *mp;
486 struct fid *fhp;
487 struct sockaddr *nam;
488 struct vnode **vpp;
489 int *exflagsp;
490 struct ucred **credanonp;
491{
492 return (EINVAL);
493}
494
495/*
496 * Vnode pointer to File handle, should never happen either
497 */
498/* ARGSUSED */
499static int
500smbfs_vptofh(vp, fhp)
501 struct vnode *vp;
502 struct fid *fhp;
503{
504 return (EINVAL);
505}
506
507#endif /* __FreeBSD_version < 400009 */