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