xref: /freebsd/sys/fs/smbfs/smbfs_vfsops.c (revision 52267f7411adcc76ede961420e08c0e42f42d415)
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 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/proc.h>
38 #include <sys/bio.h>
39 #include <sys/buf.h>
40 #include <sys/kernel.h>
41 #include <sys/sysctl.h>
42 #include <sys/vnode.h>
43 #include <sys/mount.h>
44 #include <sys/stat.h>
45 #include <sys/malloc.h>
46 #include <sys/module.h>
47 #include <sys/sx.h>
48 
49 
50 #include <netsmb/smb.h>
51 #include <netsmb/smb_conn.h>
52 #include <netsmb/smb_subr.h>
53 #include <netsmb/smb_dev.h>
54 
55 #include <fs/smbfs/smbfs.h>
56 #include <fs/smbfs/smbfs_node.h>
57 #include <fs/smbfs/smbfs_subr.h>
58 
59 static int smbfs_debuglevel = 0;
60 
61 static int smbfs_version = SMBFS_VERSION;
62 
63 #ifdef SMBFS_USEZONE
64 #include <vm/vm.h>
65 #include <vm/vm_extern.h>
66 
67 vm_zone_t smbfsmount_zone;
68 #endif
69 
70 SYSCTL_NODE(_vfs, OID_AUTO, smbfs, CTLFLAG_RW, 0, "SMB/CIFS filesystem");
71 SYSCTL_INT(_vfs_smbfs, OID_AUTO, version, CTLFLAG_RD, &smbfs_version, 0, "");
72 SYSCTL_INT(_vfs_smbfs, OID_AUTO, debuglevel, CTLFLAG_RW, &smbfs_debuglevel, 0, "");
73 
74 static MALLOC_DEFINE(M_SMBFSHASH, "smbfs_hash", "SMBFS hash table");
75 
76 static vfs_init_t       smbfs_init;
77 static vfs_uninit_t     smbfs_uninit;
78 static vfs_cmount_t     smbfs_cmount;
79 static vfs_mount_t      smbfs_mount;
80 static vfs_root_t       smbfs_root;
81 static vfs_quotactl_t   smbfs_quotactl;
82 static vfs_statfs_t     smbfs_statfs;
83 static vfs_unmount_t    smbfs_unmount;
84 
85 static struct vfsops smbfs_vfsops = {
86 	.vfs_init =		smbfs_init,
87 	.vfs_cmount =		smbfs_cmount,
88 	.vfs_mount =		smbfs_mount,
89 	.vfs_quotactl =		smbfs_quotactl,
90 	.vfs_root =		smbfs_root,
91 	.vfs_statfs =		smbfs_statfs,
92 	.vfs_sync =		vfs_stdsync,
93 	.vfs_uninit =		smbfs_uninit,
94 	.vfs_unmount =		smbfs_unmount,
95 };
96 
97 
98 VFS_SET(smbfs_vfsops, smbfs, VFCF_NETWORK);
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_cmount(struct mntarg *ma, void * data, int flags, struct thread *td)
108 {
109 	struct smbfs_args args;
110 	int error;
111 
112 	error = copyin(data, &args, sizeof(struct smbfs_args));
113 	if (error)
114 		return error;
115 
116 	if (args.version != SMBFS_VERSION) {
117 		printf("mount version mismatch: kernel=%d, mount=%d\n",
118 		    SMBFS_VERSION, args.version);
119 		return EINVAL;
120 	}
121 	ma = mount_argf(ma, "dev", "%d", args.dev);
122 	ma = mount_argb(ma, args.flags & SMBFS_MOUNT_SOFT, "nosoft");
123 	ma = mount_argb(ma, args.flags & SMBFS_MOUNT_INTR, "nointr");
124 	ma = mount_argb(ma, args.flags & SMBFS_MOUNT_STRONG, "nostrong");
125 	ma = mount_argb(ma, args.flags & SMBFS_MOUNT_HAVE_NLS, "nohave_nls");
126 	ma = mount_argb(ma, !(args.flags & SMBFS_MOUNT_NO_LONG), "nolong");
127 	ma = mount_arg(ma, "rootpath", args.root_path, -1);
128 	ma = mount_argf(ma, "uid", "%d", args.uid);
129 	ma = mount_argf(ma, "gid", "%d", args.gid);
130 	ma = mount_argf(ma, "file_mode", "%d", args.file_mode);
131 	ma = mount_argf(ma, "dir_mode", "%d", args.dir_mode);
132 	ma = mount_argf(ma, "caseopt", "%d", args.caseopt);
133 
134 	error = kernel_mount(ma, flags);
135 
136 	return (error);
137 }
138 
139 static const char *smbfs_opts[] = {
140 	"dev", "soft", "intr", "strong", "have_nls", "long",
141 	"mountpoint", "rootpath", "uid", "gid", "file_mode", "dir_mode",
142 	"caseopt", "errmsg", NULL
143 };
144 
145 static int
146 smbfs_mount(struct mount *mp, struct thread *td)
147 {
148 	struct smbmount *smp = NULL;
149 	struct smb_vc *vcp;
150 	struct smb_share *ssp = NULL;
151 	struct vnode *vp;
152 	struct smb_cred scred;
153 	int error, v;
154 	char *pc, *pe;
155 
156 	if (mp->mnt_flag & (MNT_UPDATE | MNT_ROOTFS))
157 		return EOPNOTSUPP;
158 
159 	if (vfs_filteropt(mp->mnt_optnew, smbfs_opts)) {
160 		vfs_mount_error(mp, "%s", "Invalid option");
161 		return (EINVAL);
162 	}
163 
164 	smb_makescred(&scred, td, td->td_ucred);
165 	if (1 != vfs_scanopt(mp->mnt_optnew, "dev", "%d", &v)) {
166 		vfs_mount_error(mp, "No dev option");
167 		return (EINVAL);
168 	}
169 	error = smb_dev2share(v, SMBM_EXEC, &scred, &ssp);
170 	if (error) {
171 		printf("invalid device handle %d (%d)\n", v, error);
172 		vfs_mount_error(mp, "invalid device handle %d (%d)\n", v, error);
173 		return error;
174 	}
175 	vcp = SSTOVC(ssp);
176 	smb_share_unlock(ssp, 0);
177 	mp->mnt_stat.f_iosize = SSTOVC(ssp)->vc_txmax;
178 
179 #ifdef SMBFS_USEZONE
180 	smp = zalloc(smbfsmount_zone);
181 #else
182 	smp = malloc(sizeof(*smp), M_SMBFSDATA,
183 	    M_WAITOK|M_USE_RESERVE);
184 #endif
185         if (smp == NULL) {
186 		printf("could not alloc smbmount\n");
187 		vfs_mount_error(mp, "could not alloc smbmount", v, error);
188 		error = ENOMEM;
189 		goto bad;
190         }
191 	bzero(smp, sizeof(*smp));
192         mp->mnt_data = smp;
193 	smp->sm_hash = hashinit(desiredvnodes, M_SMBFSHASH, &smp->sm_hashlen);
194 	if (smp->sm_hash == NULL)
195 		goto bad;
196 	sx_init(&smp->sm_hashlock, "smbfsh");
197 	smp->sm_share = ssp;
198 	smp->sm_root = NULL;
199 	if (1 != vfs_scanopt(mp->mnt_optnew,
200 	    "caseopt", "%d", &smp->sm_caseopt)) {
201 		vfs_mount_error(mp, "Invalid caseopt");
202 		error = EINVAL;
203 		goto bad;
204 	}
205 	if (1 != vfs_scanopt(mp->mnt_optnew, "uid", "%d", &v)) {
206 		vfs_mount_error(mp, "Invalid uid");
207 		error = EINVAL;
208 		goto bad;
209 	}
210 	smp->sm_uid = v;
211 
212 	if (1 != vfs_scanopt(mp->mnt_optnew, "gid", "%d", &v)) {
213 		vfs_mount_error(mp, "Invalid gid");
214 		error = EINVAL;
215 		goto bad;
216 	}
217 	smp->sm_gid = v;
218 
219 	if (1 != vfs_scanopt(mp->mnt_optnew, "file_mode", "%d", &v)) {
220 		vfs_mount_error(mp, "Invalid file_mode");
221 		error = EINVAL;
222 		goto bad;
223 	}
224 	smp->sm_file_mode = (v & (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFREG;
225 
226 	if (1 != vfs_scanopt(mp->mnt_optnew, "dir_mode", "%d", &v)) {
227 		vfs_mount_error(mp, "Invalid dir_mode");
228 		error = EINVAL;
229 		goto bad;
230 	}
231 	smp->sm_dir_mode  = (v & (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFDIR;
232 
233 	vfs_flagopt(mp->mnt_optnew,
234 	    "nolong", &smp->sm_flags, SMBFS_MOUNT_NO_LONG);
235 
236 /*	simple_lock_init(&smp->sm_npslock);*/
237 	pc = mp->mnt_stat.f_mntfromname;
238 	pe = pc + sizeof(mp->mnt_stat.f_mntfromname);
239 	bzero(pc, MNAMELEN);
240 	*pc++ = '/';
241 	*pc++ = '/';
242 	pc=index(strncpy(pc, vcp->vc_username, pe - pc - 2), 0);
243 	if (pc < pe-1) {
244 		*(pc++) = '@';
245 		pc = index(strncpy(pc, vcp->vc_srvname, pe - pc - 2), 0);
246 		if (pc < pe - 1) {
247 			*(pc++) = '/';
248 			strncpy(pc, ssp->ss_name, pe - pc - 2);
249 		}
250 	}
251 	vfs_getnewfsid(mp);
252 	error = smbfs_root(mp, LK_EXCLUSIVE, &vp, td);
253 	if (error) {
254 		vfs_mount_error(mp, "smbfs_root error: %d", error);
255 		goto bad;
256 	}
257 	VOP_UNLOCK(vp, 0);
258 	SMBVDEBUG("root.v_usecount = %d\n", vrefcnt(vp));
259 
260 #ifdef DIAGNOSTICS
261 	SMBERROR("mp=%p\n", mp);
262 #endif
263 	return error;
264 bad:
265         if (smp) {
266 		if (smp->sm_hash)
267 			free(smp->sm_hash, M_SMBFSHASH);
268 		sx_destroy(&smp->sm_hashlock);
269 #ifdef SMBFS_USEZONE
270 		zfree(smbfsmount_zone, smp);
271 #else
272 		free(smp, M_SMBFSDATA);
273 #endif
274 	}
275 	if (ssp)
276 		smb_share_put(ssp, &scred);
277         return error;
278 }
279 
280 /* Unmount the filesystem described by mp. */
281 static int
282 smbfs_unmount(struct mount *mp, int mntflags, struct thread *td)
283 {
284 	struct smbmount *smp = VFSTOSMBFS(mp);
285 	struct smb_cred scred;
286 	int error, flags;
287 
288 	SMBVDEBUG("smbfs_unmount: flags=%04x\n", mntflags);
289 	flags = 0;
290 	if (mntflags & MNT_FORCE)
291 		flags |= FORCECLOSE;
292 	/*
293 	 * Keep trying to flush the vnode list for the mount while
294 	 * some are still busy and we are making progress towards
295 	 * making them not busy. This is needed because smbfs vnodes
296 	 * reference their parent directory but may appear after their
297 	 * parent in the list; one pass over the vnode list is not
298 	 * sufficient in this case.
299 	 */
300 	do {
301 		smp->sm_didrele = 0;
302 		/* There is 1 extra root vnode reference from smbfs_mount(). */
303 		error = vflush(mp, 1, flags, td);
304 	} while (error == EBUSY && smp->sm_didrele != 0);
305 	if (error)
306 		return error;
307 	smb_makescred(&scred, td, td->td_ucred);
308 	error = smb_share_lock(smp->sm_share, LK_EXCLUSIVE);
309 	if (error)
310 		return error;
311 	smb_share_put(smp->sm_share, &scred);
312 	mp->mnt_data = NULL;
313 
314 	if (smp->sm_hash)
315 		free(smp->sm_hash, M_SMBFSHASH);
316 	sx_destroy(&smp->sm_hashlock);
317 #ifdef SMBFS_USEZONE
318 	zfree(smbfsmount_zone, smp);
319 #else
320 	free(smp, M_SMBFSDATA);
321 #endif
322 	MNT_ILOCK(mp);
323 	mp->mnt_flag &= ~MNT_LOCAL;
324 	MNT_IUNLOCK(mp);
325 	return error;
326 }
327 
328 /*
329  * Return locked root vnode of a filesystem
330  */
331 static int
332 smbfs_root(struct mount *mp, int flags, struct vnode **vpp, struct thread *td)
333 {
334 	struct smbmount *smp = VFSTOSMBFS(mp);
335 	struct vnode *vp;
336 	struct smbnode *np;
337 	struct smbfattr fattr;
338 	struct ucred *cred = td->td_ucred;
339 	struct smb_cred scred;
340 	int error;
341 
342 	if (smp == NULL) {
343 		SMBERROR("smp == NULL (bug in umount)\n");
344 		vfs_mount_error(mp, "smp == NULL (bug in umount)");
345 		return EINVAL;
346 	}
347 	if (smp->sm_root) {
348 		*vpp = SMBTOV(smp->sm_root);
349 		return vget(*vpp, LK_EXCLUSIVE | LK_RETRY, td);
350 	}
351 	smb_makescred(&scred, td, cred);
352 	error = smbfs_smb_lookup(NULL, NULL, 0, &fattr, &scred);
353 	if (error)
354 		return error;
355 	error = smbfs_nget(mp, NULL, "TheRooT", 7, &fattr, &vp);
356 	if (error)
357 		return error;
358 	ASSERT_VOP_LOCKED(vp, "smbfs_root");
359 	vp->v_vflag |= VV_ROOT;
360 	np = VTOSMB(vp);
361 	smp->sm_root = np;
362 	*vpp = vp;
363 	return 0;
364 }
365 
366 /*
367  * Do operations associated with quotas, not supported
368  */
369 /* ARGSUSED */
370 static int
371 smbfs_quotactl(mp, cmd, uid, arg, td)
372 	struct mount *mp;
373 	int cmd;
374 	uid_t uid;
375 	void *arg;
376 	struct thread *td;
377 {
378 	SMBVDEBUG("return EOPNOTSUPP\n");
379 	return EOPNOTSUPP;
380 }
381 
382 /*ARGSUSED*/
383 int
384 smbfs_init(struct vfsconf *vfsp)
385 {
386 #ifdef SMBFS_USEZONE
387 	smbfsmount_zone = zinit("SMBFSMOUNT", sizeof(struct smbmount), 0, 0, 1);
388 #endif
389 	smbfs_pbuf_freecnt = nswbuf / 2 + 1;
390 	SMBVDEBUG("done.\n");
391 	return 0;
392 }
393 
394 /*ARGSUSED*/
395 int
396 smbfs_uninit(struct vfsconf *vfsp)
397 {
398 
399 	SMBVDEBUG("done.\n");
400 	return 0;
401 }
402 
403 /*
404  * smbfs_statfs call
405  */
406 int
407 smbfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
408 {
409 	struct smbmount *smp = VFSTOSMBFS(mp);
410 	struct smbnode *np = smp->sm_root;
411 	struct smb_share *ssp = smp->sm_share;
412 	struct smb_cred scred;
413 	int error = 0;
414 
415 	if (np == NULL) {
416 		vfs_mount_error(mp, "np == NULL");
417 		return EINVAL;
418 	}
419 
420 	sbp->f_iosize = SSTOVC(ssp)->vc_txmax;		/* optimal transfer block size */
421 	smb_makescred(&scred, td, td->td_ucred);
422 
423 	if (SMB_DIALECT(SSTOVC(ssp)) >= SMB_DIALECT_LANMAN2_0)
424 		error = smbfs_smb_statfs2(ssp, sbp, &scred);
425 	else
426 		error = smbfs_smb_statfs(ssp, sbp, &scred);
427 	if (error)
428 		return error;
429 	sbp->f_flags = 0;		/* copy of mount exported flags */
430 	return 0;
431 }
432