xref: /dragonfly/sys/vfs/smbfs/smbfs_vfsops.c (revision 59b0b316)
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  */
34 
35 #ifndef KLD_MODULE
36 #include "opt_netsmb.h"
37 #ifndef NETSMB
38 #error "SMBFS requires option NETSMB"
39 #endif
40 #endif
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 
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>
58 
59 #include "smbfs.h"
60 #include "smbfs_node.h"
61 #include "smbfs_subr.h"
62 
63 #include <sys/buf.h>
64 
65 extern struct vop_ops smbfs_vnode_vops;
66 
67 int smbfs_debuglevel = 0;
68 
69 static int smbfs_version = SMBFS_VERSION;
70 
71 SYSCTL_NODE(_vfs, OID_AUTO, smbfs, CTLFLAG_RW, 0, "SMB/CIFS file system");
72 SYSCTL_INT(_vfs_smbfs, OID_AUTO, version, CTLFLAG_RD, &smbfs_version, 0, "");
73 SYSCTL_INT(_vfs_smbfs, OID_AUTO, debuglevel, CTLFLAG_RW, &smbfs_debuglevel, 0, "");
74 
75 static MALLOC_DEFINE(M_SMBFSHASH, "SMBFS hash", "SMBFS hash table");
76 
77 
78 static int smbfs_mount(struct mount *, char *, caddr_t, struct ucred *);
79 static int smbfs_root(struct mount *, struct vnode **);
80 static int smbfs_statfs(struct mount *, struct statfs *, struct ucred *);
81 static int smbfs_sync(struct mount *, int);
82 static int smbfs_unmount(struct mount *, int);
83 static int smbfs_init(struct vfsconf *vfsp);
84 static int smbfs_uninit(struct vfsconf *vfsp);
85 
86 static struct vfsops smbfs_vfsops = {
87 	.vfs_mount =    	smbfs_mount,
88 	.vfs_unmount =    	smbfs_unmount,
89 	.vfs_root =    		smbfs_root,
90 	.vfs_statfs =    	smbfs_statfs,
91 	.vfs_sync =    		smbfs_sync,
92 	.vfs_init =    		smbfs_init,
93 	.vfs_uninit =    	smbfs_uninit
94 };
95 
96 
97 VFS_SET(smbfs_vfsops, smbfs, VFCF_NETWORK);
98 MODULE_VERSION(smbfs, 1);
99 
100 MODULE_DEPEND(smbfs, netsmb, NSMB_VERSION, NSMB_VERSION, NSMB_VERSION);
101 MODULE_DEPEND(smbfs, libiconv, 1, 1, 2);
102 MODULE_DEPEND(smbfs, libmchain, 1, 1, 1);
103 
104 int smbfs_pbuf_freecnt = -1;	/* start out unlimited */
105 
106 static int
107 smbfs_mount(struct mount *mp, char *path, caddr_t data, struct ucred *cred)
108 {
109 	struct smbfs_args args; 	  /* will hold data from mount request */
110 	struct smbmount *smp = NULL;
111 	struct smb_vc *vcp;
112 	struct smb_share *ssp = NULL;
113 	struct vnode *vp;
114 	struct smb_cred scred;
115 	int error;
116 	int hsize;
117 	char *pc, *pe;
118 
119 	if (data == NULL) {
120 		kprintf("missing data argument\n");
121 		return EINVAL;
122 	}
123 	if (mp->mnt_flag & MNT_UPDATE) {
124 		kprintf("MNT_UPDATE not implemented");
125 		return EOPNOTSUPP;
126 	}
127 	error = copyin(data, (caddr_t)&args, sizeof(struct smbfs_args));
128 	if (error)
129 		return error;
130 	if (args.version != SMBFS_VERSION) {
131 		kprintf("mount version mismatch: kernel=%d, mount=%d\n",
132 		    SMBFS_VERSION, args.version);
133 		return EINVAL;
134 	}
135 	smb_makescred(&scred, curthread, cred);
136 	error = smb_dev2share(args.dev, SMBM_EXEC, &scred, &ssp);
137 	if (error) {
138 		kprintf("invalid device handle %d (%d)\n", args.dev, error);
139 		return error;
140 	}
141 	vcp = SSTOVC(ssp);
142 	smb_share_unlock(ssp, 0);
143 	mp->mnt_stat.f_iosize = SSTOVC(ssp)->vc_txmax;
144 
145 	smp = kmalloc(sizeof(*smp), M_SMBFSDATA, M_WAITOK | M_USE_RESERVE | M_ZERO);
146 	mp->mnt_data = (qaddr_t)smp;
147 	smp->sm_cred = crhold(cred);
148 
149 	hsize = vfs_inodehashsize();
150 	smp->sm_hash = hashinit(hsize, M_SMBFSHASH, &smp->sm_hashlen);
151 	if (smp->sm_hash == NULL)
152 		goto bad;
153 	lockinit(&smp->sm_hashlock, "smbfsh", 0, 0);
154 	smp->sm_share = ssp;
155 	smp->sm_root = NULL;
156 	smp->sm_args = args;
157 	smp->sm_caseopt = args.caseopt;
158 	smp->sm_args.file_mode = (smp->sm_args.file_mode &
159 			    (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFREG;
160 	smp->sm_args.dir_mode  = (smp->sm_args.dir_mode &
161 			    (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFDIR;
162 
163 /*	simple_lock_init(&smp->sm_npslock);*/
164 	pc = mp->mnt_stat.f_mntfromname;
165 	pe = pc + sizeof(mp->mnt_stat.f_mntfromname);
166 	bzero(pc, MNAMELEN);
167 	*pc++ = '/';
168 	*pc++ = '/';
169 	pc=index(strncpy(pc, vcp->vc_username, pe - pc - 2), 0);
170 	if (pc < pe-1) {
171 		*(pc++) = '@';
172 		pc = index(strncpy(pc, vcp->vc_srvname, pe - pc - 2), 0);
173 		if (pc < pe - 1) {
174 			*(pc++) = '/';
175 			strncpy(pc, ssp->ss_name, pe - pc - 2);
176 		}
177 	}
178 	/* protect against invalid mount points */
179 	smp->sm_args.mount_point[sizeof(smp->sm_args.mount_point) - 1] = '\0';
180 	vfs_getnewfsid(mp);
181 
182 	vfs_add_vnodeops(mp, &smbfs_vnode_vops, &mp->mnt_vn_norm_ops);
183 
184 	error = smbfs_root(mp, &vp);
185 	if (error)
186 		goto bad;
187 	vn_unlock(vp);
188 	SMBVDEBUG("root.v_refcnt = %08x\n", vp->v_refcnt);
189 
190 #ifdef DIAGNOSTICS
191 	SMBERROR("mp=%p\n", mp);
192 #endif
193 	return error;
194 bad:
195 	if (smp) {
196 		if (smp->sm_cred)
197 			crfree(smp->sm_cred);
198 		if (smp->sm_hash)
199 			hashdestroy(smp->sm_hash, M_SMBFSHASH,
200 			    smp->sm_hashlen);
201 		lockdestroy(&smp->sm_hashlock);
202 		kfree(smp, M_SMBFSDATA);
203 	}
204 	if (ssp)
205 		smb_share_put(ssp, &scred);
206 	return error;
207 }
208 
209 /* Unmount the filesystem described by mp. */
210 static int
211 smbfs_unmount(struct mount *mp, int mntflags)
212 {
213 	struct smbmount *smp = VFSTOSMBFS(mp);
214 	struct smb_cred scred;
215 	int error, flags;
216 
217 	SMBVDEBUG("smbfs_unmount: flags=%04x\n", mntflags);
218 	flags = 0;
219 	if (mntflags & MNT_FORCE)
220 		flags |= FORCECLOSE;
221 	/*
222 	 * Keep trying to flush the vnode list for the mount while
223 	 * some are still busy and we are making progress towards
224 	 * making them not busy. This is needed because smbfs vnodes
225 	 * reference their parent directory but may appear after their
226 	 * parent in the list; one pass over the vnode list is not
227 	 * sufficient in this case.
228 	 */
229 	do {
230 		smp->sm_didrele = 0;
231 		/* There is 1 extra root vnode reference from smbfs_mount(). */
232 		error = vflush(mp, 1, flags);
233 	} while (error == EBUSY && smp->sm_didrele != 0);
234 	if (error)
235 		return error;
236 	smb_makescred(&scred, curthread, smp->sm_cred);
237 	error = smb_share_lock(smp->sm_share, LK_EXCLUSIVE);
238 	if (error)
239 		goto out;
240 	smb_share_put(smp->sm_share, &scred);
241 	mp->mnt_data = (qaddr_t)0;
242 
243 	if (smp->sm_cred)
244 		crfree(smp->sm_cred);
245 	if (smp->sm_hash)
246 		hashdestroy(smp->sm_hash, M_SMBFSHASH, smp->sm_hashlen);
247 	lockdestroy(&smp->sm_hashlock);
248 	kfree(smp, M_SMBFSDATA);
249 	mp->mnt_flag &= ~MNT_LOCAL;
250 out:
251 	return error;
252 }
253 
254 /*
255  * Return locked root vnode of a filesystem
256  */
257 static int
258 smbfs_root(struct mount *mp, struct vnode **vpp)
259 {
260 	struct thread *td = curthread;	/* XXX */
261 	struct smbmount *smp = VFSTOSMBFS(mp);
262 	struct vnode *vp;
263 	struct smbnode *np;
264 	struct smbfattr fattr;
265 	struct ucred *cred;
266 	struct smb_cred scred;
267 	int error;
268 
269 	if (smp == NULL) {
270 		SMBERROR("smp == NULL (bug in umount)\n");
271 		return EINVAL;
272 	}
273 	if (smp->sm_root) {
274 		*vpp = SMBTOV(smp->sm_root);
275 		return vget(*vpp, LK_EXCLUSIVE | LK_RETRY);
276 	}
277 	if (td->td_proc)
278 		cred = td->td_proc->p_ucred;
279 	else
280 		cred = proc0.p_ucred;
281 
282 	smb_makescred(&scred, td, cred);
283 	error = smbfs_smb_lookup(NULL, NULL, 0, &fattr, &scred);
284 	if (error)
285 		return error;
286 	error = smbfs_nget(mp, NULL, "TheRooT", 7, &fattr, &vp);
287 	if (error)
288 		return error;
289 	vsetflags(vp, VROOT);
290 	np = VTOSMB(vp);
291 	smp->sm_root = np;
292 	*vpp = vp;
293 	return 0;
294 }
295 
296 /*ARGSUSED*/
297 int
298 smbfs_init(struct vfsconf *vfsp)
299 {
300 	smbfs_pbuf_freecnt = nswbuf_kva / 2 + 1;
301 	SMBVDEBUG("done.\n");
302 	return 0;
303 }
304 
305 /*ARGSUSED*/
306 int
307 smbfs_uninit(struct vfsconf *vfsp)
308 {
309 	SMBVDEBUG("done.\n");
310 	return 0;
311 }
312 
313 /*
314  * smbfs_statfs call
315  */
316 int
317 smbfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
318 {
319 	struct smbmount *smp = VFSTOSMBFS(mp);
320 	struct smbnode *np = smp->sm_root;
321 	struct smb_share *ssp = smp->sm_share;
322 	struct smb_cred scred;
323 	int error = 0;
324 
325 	if (np == NULL)
326 		return EINVAL;
327 
328 	sbp->f_iosize = SSTOVC(ssp)->vc_txmax;		/* optimal transfer block size */
329 	sbp->f_spare2 = 0;			/* placeholder */
330 	smb_makescred(&scred, curthread, cred);
331 
332 	if (SMB_DIALECT(SSTOVC(ssp)) >= SMB_DIALECT_LANMAN2_0)
333 		error = smbfs_smb_statfs2(ssp, sbp, &scred);
334 	else
335 		error = smbfs_smb_statfs(ssp, sbp, &scred);
336 	if (error)
337 		return error;
338 	sbp->f_flags = 0;		/* copy of mount exported flags */
339 	if (sbp != &mp->mnt_stat) {
340 		sbp->f_fsid = mp->mnt_stat.f_fsid;	/* file system id */
341 		sbp->f_owner = mp->mnt_stat.f_owner;	/* user that mounted the filesystem */
342 		sbp->f_type = mp->mnt_vfc->vfc_typenum;	/* type of filesystem */
343 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
344 	}
345 	strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
346 	return 0;
347 }
348 
349 /*
350  * Flush out the buffer cache
351  */
352 /* ARGSUSED */
353 static int
354 smbfs_sync(struct mount *mp, int waitfor)
355 {
356 	struct vnode *vp;
357 	int error, allerror = 0;
358 	/*
359 	 * Force stale buffer cache information to be flushed.
360 	 */
361 loop:
362 	for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
363 	     vp != NULL;
364 	     vp = TAILQ_NEXT(vp, v_nmntvnodes)) {
365 		/*
366 		 * If the vnode that we are about to sync is no longer
367 		 * associated with this mount point, start over.
368 		 */
369 		if (vp->v_mount != mp)
370 			goto loop;
371 		if (vn_islocked(vp) || RB_EMPTY(&vp->v_rbdirty_tree) ||
372 		    (waitfor & MNT_LAZY))
373 			continue;
374 		if (vget(vp, LK_EXCLUSIVE))
375 			goto loop;
376 		error = VOP_FSYNC(vp, waitfor, 0);
377 		if (error)
378 			allerror = error;
379 		vput(vp);
380 	}
381 	return (allerror);
382 }
383