xref: /freebsd/sys/fs/smbfs/smbfs_vfsops.c (revision 6fd05b64b5b65dd4ba9b86482a0634a5f0b96c29)
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$
33  */
34 #include "opt_netsmb.h"
35 #ifndef NETSMB
36 #error "SMBFS requires option NETSMB"
37 #endif
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/proc.h>
42 #include <sys/bio.h>
43 #include <sys/buf.h>
44 #include <sys/kernel.h>
45 #include <sys/sysctl.h>
46 #include <sys/vnode.h>
47 #include <sys/mount.h>
48 #include <sys/stat.h>
49 #include <sys/malloc.h>
50 #include <sys/module.h>
51 
52 
53 #include <netsmb/smb.h>
54 #include <netsmb/smb_conn.h>
55 #include <netsmb/smb_subr.h>
56 #include <netsmb/smb_dev.h>
57 
58 #include <fs/smbfs/smbfs.h>
59 #include <fs/smbfs/smbfs_node.h>
60 #include <fs/smbfs/smbfs_subr.h>
61 
62 int smbfs_debuglevel = 0;
63 
64 static int smbfs_version = SMBFS_VERSION;
65 
66 #ifdef SMBFS_USEZONE
67 #include <vm/vm.h>
68 #include <vm/vm_extern.h>
69 
70 vm_zone_t smbfsmount_zone;
71 #endif
72 
73 SYSCTL_NODE(_vfs, OID_AUTO, smbfs, CTLFLAG_RW, 0, "SMB/CIFS filesystem");
74 SYSCTL_INT(_vfs_smbfs, OID_AUTO, version, CTLFLAG_RD, &smbfs_version, 0, "");
75 SYSCTL_INT(_vfs_smbfs, OID_AUTO, debuglevel, CTLFLAG_RW, &smbfs_debuglevel, 0, "");
76 
77 static MALLOC_DEFINE(M_SMBFSHASH, "SMBFS hash", "SMBFS hash table");
78 
79 static vfs_init_t       smbfs_init;
80 static vfs_uninit_t     smbfs_uninit;
81 static vfs_mount_t      smbfs_mount;
82 static vfs_start_t      smbfs_start;
83 static vfs_root_t       smbfs_root;
84 static vfs_quotactl_t   smbfs_quotactl;
85 static vfs_statfs_t     smbfs_statfs;
86 static vfs_unmount_t    smbfs_unmount;
87 
88 static struct vfsops smbfs_vfsops = {
89 	.vfs_init =		smbfs_init,
90 	.vfs_mount =		smbfs_mount,
91 	.vfs_quotactl =		smbfs_quotactl,
92 	.vfs_root =		smbfs_root,
93 	.vfs_start =		smbfs_start,
94 	.vfs_statfs =		smbfs_statfs,
95 	.vfs_sync =		vfs_stdsync,
96 	.vfs_uninit =		smbfs_uninit,
97 	.vfs_unmount =		smbfs_unmount,
98 };
99 
100 
101 VFS_SET(smbfs_vfsops, smbfs, VFCF_NETWORK);
102 
103 MODULE_DEPEND(smbfs, netsmb, NSMB_VERSION, NSMB_VERSION, NSMB_VERSION);
104 MODULE_DEPEND(smbfs, libiconv, 1, 1, 2);
105 MODULE_DEPEND(smbfs, libmchain, 1, 1, 1);
106 
107 int smbfs_pbuf_freecnt = -1;	/* start out unlimited */
108 
109 static int
110 smbfs_mount(struct mount *mp, char *path, caddr_t data,
111 	struct nameidata *ndp, struct thread *td)
112 {
113 	struct smbfs_args args; 	  /* will hold data from mount request */
114 	struct smbmount *smp = NULL;
115 	struct smb_vc *vcp;
116 	struct smb_share *ssp = NULL;
117 	struct vnode *vp;
118 	struct smb_cred scred;
119 	int error;
120 	char *pc, *pe;
121 
122 	if (data == NULL) {
123 		printf("missing data argument\n");
124 		return EINVAL;
125 	}
126 	if (mp->mnt_flag & MNT_UPDATE) {
127 		printf("MNT_UPDATE not implemented");
128 		return EOPNOTSUPP;
129 	}
130 	error = copyin(data, (caddr_t)&args, sizeof(struct smbfs_args));
131 	if (error)
132 		return error;
133 	if (args.version != SMBFS_VERSION) {
134 		printf("mount version mismatch: kernel=%d, mount=%d\n",
135 		    SMBFS_VERSION, args.version);
136 		return EINVAL;
137 	}
138 	smb_makescred(&scred, td, td->td_ucred);
139 	error = smb_dev2share(args.dev, SMBM_EXEC, &scred, &ssp);
140 	if (error) {
141 		printf("invalid device handle %d (%d)\n", args.dev, error);
142 		return error;
143 	}
144 	vcp = SSTOVC(ssp);
145 	smb_share_unlock(ssp, 0, td);
146 	mp->mnt_stat.f_iosize = SSTOVC(ssp)->vc_txmax;
147 
148 #ifdef SMBFS_USEZONE
149 	smp = zalloc(smbfsmount_zone);
150 #else
151 	MALLOC(smp, struct smbmount*, sizeof(*smp), M_SMBFSDATA,
152 	    M_WAITOK|M_USE_RESERVE);
153 #endif
154         if (smp == NULL) {
155                 printf("could not alloc smbmount\n");
156                 error = ENOMEM;
157 		goto bad;
158         }
159 	bzero(smp, sizeof(*smp));
160         mp->mnt_data = (qaddr_t)smp;
161 	smp->sm_hash = hashinit(desiredvnodes, M_SMBFSHASH, &smp->sm_hashlen);
162 	if (smp->sm_hash == NULL)
163 		goto bad;
164 	lockinit(&smp->sm_hashlock, PVFS, "smbfsh", 0, 0);
165 	smp->sm_share = ssp;
166 	smp->sm_root = NULL;
167         smp->sm_args = args;
168 	smp->sm_caseopt = args.caseopt;
169 	smp->sm_args.file_mode = (smp->sm_args.file_mode &
170 			    (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFREG;
171 	smp->sm_args.dir_mode  = (smp->sm_args.dir_mode &
172 			    (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFDIR;
173 
174 /*	simple_lock_init(&smp->sm_npslock);*/
175 	pc = mp->mnt_stat.f_mntfromname;
176 	pe = pc + sizeof(mp->mnt_stat.f_mntfromname);
177 	bzero(pc, MNAMELEN);
178 	*pc++ = '/';
179 	*pc++ = '/';
180 	pc=index(strncpy(pc, vcp->vc_username, pe - pc - 2), 0);
181 	if (pc < pe-1) {
182 		*(pc++) = '@';
183 		pc = index(strncpy(pc, vcp->vc_srvname, pe - pc - 2), 0);
184 		if (pc < pe - 1) {
185 			*(pc++) = '/';
186 			strncpy(pc, ssp->ss_name, pe - pc - 2);
187 		}
188 	}
189 	/* protect against invalid mount points */
190 	smp->sm_args.mount_point[sizeof(smp->sm_args.mount_point) - 1] = '\0';
191 	vfs_getnewfsid(mp);
192 	error = smbfs_root(mp, &vp, td);
193 	if (error)
194 		goto bad;
195 	VOP_UNLOCK(vp, 0, td);
196 	SMBVDEBUG("root.v_usecount = %d\n", vrefcnt(vp));
197 
198 #ifdef DIAGNOSTICS
199 	SMBERROR("mp=%p\n", mp);
200 #endif
201 	return error;
202 bad:
203         if (smp) {
204 		if (smp->sm_hash)
205 			free(smp->sm_hash, M_SMBFSHASH);
206 		lockdestroy(&smp->sm_hashlock);
207 #ifdef SMBFS_USEZONE
208 		zfree(smbfsmount_zone, smp);
209 #else
210 		free(smp, M_SMBFSDATA);
211 #endif
212 	}
213 	if (ssp)
214 		smb_share_put(ssp, &scred);
215         return error;
216 }
217 
218 /* Unmount the filesystem described by mp. */
219 static int
220 smbfs_unmount(struct mount *mp, int mntflags, struct thread *td)
221 {
222 	struct smbmount *smp = VFSTOSMBFS(mp);
223 	struct smb_cred scred;
224 	int error, flags;
225 
226 	SMBVDEBUG("smbfs_unmount: flags=%04x\n", mntflags);
227 	flags = 0;
228 	if (mntflags & MNT_FORCE)
229 		flags |= FORCECLOSE;
230 	/*
231 	 * Keep trying to flush the vnode list for the mount while
232 	 * some are still busy and we are making progress towards
233 	 * making them not busy. This is needed because smbfs vnodes
234 	 * reference their parent directory but may appear after their
235 	 * parent in the list; one pass over the vnode list is not
236 	 * sufficient in this case.
237 	 */
238 	do {
239 		smp->sm_didrele = 0;
240 		/* There is 1 extra root vnode reference from smbfs_mount(). */
241 		error = vflush(mp, 1, flags, td);
242 	} while (error == EBUSY && smp->sm_didrele != 0);
243 	if (error)
244 		return error;
245 	smb_makescred(&scred, td, td->td_ucred);
246 	smb_share_put(smp->sm_share, &scred);
247 	mp->mnt_data = (qaddr_t)0;
248 
249 	if (smp->sm_hash)
250 		free(smp->sm_hash, M_SMBFSHASH);
251 	lockdestroy(&smp->sm_hashlock);
252 #ifdef SMBFS_USEZONE
253 	zfree(smbfsmount_zone, smp);
254 #else
255 	free(smp, M_SMBFSDATA);
256 #endif
257 	mp->mnt_flag &= ~MNT_LOCAL;
258 	return error;
259 }
260 
261 /*
262  * Return locked root vnode of a filesystem
263  */
264 static int
265 smbfs_root(struct mount *mp, struct vnode **vpp, struct thread *td)
266 {
267 	struct smbmount *smp = VFSTOSMBFS(mp);
268 	struct vnode *vp;
269 	struct smbnode *np;
270 	struct smbfattr fattr;
271 	struct ucred *cred = td->td_ucred;
272 	struct smb_cred scred;
273 	int error;
274 
275 	if (smp == NULL) {
276 		SMBERROR("smp == NULL (bug in umount)\n");
277 		return EINVAL;
278 	}
279 	if (smp->sm_root) {
280 		*vpp = SMBTOV(smp->sm_root);
281 		return vget(*vpp, LK_EXCLUSIVE | LK_RETRY, td);
282 	}
283 	smb_makescred(&scred, td, cred);
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;
290 	ASSERT_VOP_LOCKED(vp, "smbfs_root");
291 	vp->v_vflag |= VV_ROOT;
292 	np = VTOSMB(vp);
293 	smp->sm_root = np;
294 	*vpp = vp;
295 	return 0;
296 }
297 
298 /*
299  * Vfs start routine, a no-op.
300  */
301 /* ARGSUSED */
302 static int
303 smbfs_start(mp, flags, td)
304 	struct mount *mp;
305 	int flags;
306 	struct thread *td;
307 {
308 	SMBVDEBUG("flags=%04x\n", flags);
309 	return 0;
310 }
311 
312 /*
313  * Do operations associated with quotas, not supported
314  */
315 /* ARGSUSED */
316 static int
317 smbfs_quotactl(mp, cmd, uid, arg, td)
318 	struct mount *mp;
319 	int cmd;
320 	uid_t uid;
321 	caddr_t arg;
322 	struct thread *td;
323 {
324 	SMBVDEBUG("return EOPNOTSUPP\n");
325 	return EOPNOTSUPP;
326 }
327 
328 /*ARGSUSED*/
329 int
330 smbfs_init(struct vfsconf *vfsp)
331 {
332 #ifdef SMBFS_USEZONE
333 	smbfsmount_zone = zinit("SMBFSMOUNT", sizeof(struct smbmount), 0, 0, 1);
334 #endif
335 	smbfs_pbuf_freecnt = nswbuf / 2 + 1;
336 	SMBVDEBUG("done.\n");
337 	return 0;
338 }
339 
340 /*ARGSUSED*/
341 int
342 smbfs_uninit(struct vfsconf *vfsp)
343 {
344 
345 	SMBVDEBUG("done.\n");
346 	return 0;
347 }
348 
349 /*
350  * smbfs_statfs call
351  */
352 int
353 smbfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
354 {
355 	struct smbmount *smp = VFSTOSMBFS(mp);
356 	struct smbnode *np = smp->sm_root;
357 	struct smb_share *ssp = smp->sm_share;
358 	struct smb_cred scred;
359 	int error = 0;
360 
361 	if (np == NULL)
362 		return EINVAL;
363 
364 	sbp->f_iosize = SSTOVC(ssp)->vc_txmax;		/* optimal transfer block size */
365 	smb_makescred(&scred, td, td->td_ucred);
366 
367 	if (SMB_DIALECT(SSTOVC(ssp)) >= SMB_DIALECT_LANMAN2_0)
368 		error = smbfs_smb_statfs2(ssp, sbp, &scred);
369 	else
370 		error = smbfs_smb_statfs(ssp, sbp, &scred);
371 	if (error)
372 		return error;
373 	sbp->f_flags = 0;		/* copy of mount exported flags */
374 	if (sbp != &mp->mnt_stat) {
375 		sbp->f_fsid = mp->mnt_stat.f_fsid;	/* filesystem id */
376 		sbp->f_owner = mp->mnt_stat.f_owner;	/* user that mounted the filesystem */
377 		sbp->f_type = mp->mnt_vfc->vfc_typenum;	/* type of filesystem */
378 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
379 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
380 	}
381 	strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
382 	return 0;
383 }
384