xref: /freebsd/sys/fs/smbfs/smbfs_vfsops.c (revision c697fb7f)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2000-2001 Boris Popov
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/proc.h>
34 #include <sys/bio.h>
35 #include <sys/buf.h>
36 #include <sys/kernel.h>
37 #include <sys/sysctl.h>
38 #include <sys/vnode.h>
39 #include <sys/mount.h>
40 #include <sys/stat.h>
41 #include <sys/malloc.h>
42 #include <sys/module.h>
43 #include <sys/sx.h>
44 
45 
46 #include <netsmb/smb.h>
47 #include <netsmb/smb_conn.h>
48 #include <netsmb/smb_subr.h>
49 #include <netsmb/smb_dev.h>
50 
51 #include <fs/smbfs/smbfs.h>
52 #include <fs/smbfs/smbfs_node.h>
53 #include <fs/smbfs/smbfs_subr.h>
54 
55 static int smbfs_debuglevel = 0;
56 
57 static int smbfs_version = SMBFS_VERSION;
58 
59 SYSCTL_NODE(_vfs, OID_AUTO, smbfs, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
60     "SMB/CIFS filesystem");
61 SYSCTL_INT(_vfs_smbfs, OID_AUTO, version, CTLFLAG_RD, &smbfs_version, 0, "");
62 SYSCTL_INT(_vfs_smbfs, OID_AUTO, debuglevel, CTLFLAG_RW, &smbfs_debuglevel, 0, "");
63 
64 static vfs_init_t       smbfs_init;
65 static vfs_uninit_t     smbfs_uninit;
66 static vfs_cmount_t     smbfs_cmount;
67 static vfs_mount_t      smbfs_mount;
68 static vfs_root_t       smbfs_root;
69 static vfs_quotactl_t   smbfs_quotactl;
70 static vfs_statfs_t     smbfs_statfs;
71 static vfs_unmount_t    smbfs_unmount;
72 
73 static struct vfsops smbfs_vfsops = {
74 	.vfs_init =		smbfs_init,
75 	.vfs_cmount =		smbfs_cmount,
76 	.vfs_mount =		smbfs_mount,
77 	.vfs_quotactl =		smbfs_quotactl,
78 	.vfs_root =		smbfs_root,
79 	.vfs_statfs =		smbfs_statfs,
80 	.vfs_sync =		vfs_stdsync,
81 	.vfs_uninit =		smbfs_uninit,
82 	.vfs_unmount =		smbfs_unmount,
83 };
84 
85 
86 VFS_SET(smbfs_vfsops, smbfs, VFCF_NETWORK);
87 
88 MODULE_DEPEND(smbfs, netsmb, NSMB_VERSION, NSMB_VERSION, NSMB_VERSION);
89 MODULE_DEPEND(smbfs, libiconv, 1, 1, 2);
90 MODULE_DEPEND(smbfs, libmchain, 1, 1, 1);
91 
92 uma_zone_t smbfs_pbuf_zone;
93 
94 static int
95 smbfs_cmount(struct mntarg *ma, void * data, uint64_t flags)
96 {
97 	struct smbfs_args args;
98 	int error;
99 
100 	error = copyin(data, &args, sizeof(struct smbfs_args));
101 	if (error)
102 		return error;
103 
104 	if (args.version != SMBFS_VERSION) {
105 		printf("mount version mismatch: kernel=%d, mount=%d\n",
106 		    SMBFS_VERSION, args.version);
107 		return EINVAL;
108 	}
109 	ma = mount_argf(ma, "dev", "%d", args.dev);
110 	ma = mount_argb(ma, args.flags & SMBFS_MOUNT_SOFT, "nosoft");
111 	ma = mount_argb(ma, args.flags & SMBFS_MOUNT_INTR, "nointr");
112 	ma = mount_argb(ma, args.flags & SMBFS_MOUNT_STRONG, "nostrong");
113 	ma = mount_argb(ma, args.flags & SMBFS_MOUNT_HAVE_NLS, "nohave_nls");
114 	ma = mount_argb(ma, !(args.flags & SMBFS_MOUNT_NO_LONG), "nolong");
115 	ma = mount_arg(ma, "rootpath", args.root_path, -1);
116 	ma = mount_argf(ma, "uid", "%d", args.uid);
117 	ma = mount_argf(ma, "gid", "%d", args.gid);
118 	ma = mount_argf(ma, "file_mode", "%d", args.file_mode);
119 	ma = mount_argf(ma, "dir_mode", "%d", args.dir_mode);
120 	ma = mount_argf(ma, "caseopt", "%d", args.caseopt);
121 
122 	error = kernel_mount(ma, flags);
123 
124 	return (error);
125 }
126 
127 static const char *smbfs_opts[] = {
128 	"fd", "soft", "intr", "strong", "have_nls", "long",
129 	"mountpoint", "rootpath", "uid", "gid", "file_mode", "dir_mode",
130 	"caseopt", "errmsg", NULL
131 };
132 
133 static int
134 smbfs_mount(struct mount *mp)
135 {
136 	struct smbmount *smp = NULL;
137 	struct smb_vc *vcp;
138 	struct smb_share *ssp = NULL;
139 	struct vnode *vp;
140 	struct thread *td;
141 	struct smb_dev *dev;
142 	struct smb_cred *scred;
143 	int error, v;
144 	char *pc, *pe;
145 
146 	dev = NULL;
147 	td = curthread;
148 	if (mp->mnt_flag & (MNT_UPDATE | MNT_ROOTFS))
149 		return EOPNOTSUPP;
150 
151 	if (vfs_filteropt(mp->mnt_optnew, smbfs_opts)) {
152 		vfs_mount_error(mp, "%s", "Invalid option");
153 		return (EINVAL);
154 	}
155 
156 	scred = smbfs_malloc_scred();
157 	smb_makescred(scred, td, td->td_ucred);
158 
159 	/* Ask userspace of `fd`, the file descriptor of this session */
160 	if (1 != vfs_scanopt(mp->mnt_optnew, "fd", "%d", &v)) {
161 		vfs_mount_error(mp, "No fd option");
162 		smbfs_free_scred(scred);
163 		return (EINVAL);
164 	}
165 	error = smb_dev2share(v, SMBM_EXEC, scred, &ssp, &dev);
166 	smp = malloc(sizeof(*smp), M_SMBFSDATA, M_WAITOK | M_ZERO);
167 	if (error) {
168 		printf("invalid device handle %d (%d)\n", v, error);
169 		vfs_mount_error(mp, "invalid device handle %d %d\n", v, error);
170 		smbfs_free_scred(scred);
171 		free(smp, M_SMBFSDATA);
172 		return error;
173 	}
174 	vcp = SSTOVC(ssp);
175 	smb_share_unlock(ssp);
176 	mp->mnt_stat.f_iosize = SSTOVC(ssp)->vc_txmax;
177 	mp->mnt_data = smp;
178 	smp->sm_share = ssp;
179 	smp->sm_root = NULL;
180 	smp->sm_dev = dev;
181 	if (1 != vfs_scanopt(mp->mnt_optnew,
182 	    "caseopt", "%d", &smp->sm_caseopt)) {
183 		vfs_mount_error(mp, "Invalid caseopt");
184 		error = EINVAL;
185 		goto bad;
186 	}
187 	if (1 != vfs_scanopt(mp->mnt_optnew, "uid", "%d", &v)) {
188 		vfs_mount_error(mp, "Invalid uid");
189 		error = EINVAL;
190 		goto bad;
191 	}
192 	smp->sm_uid = v;
193 
194 	if (1 != vfs_scanopt(mp->mnt_optnew, "gid", "%d", &v)) {
195 		vfs_mount_error(mp, "Invalid gid");
196 		error = EINVAL;
197 		goto bad;
198 	}
199 	smp->sm_gid = v;
200 
201 	if (1 != vfs_scanopt(mp->mnt_optnew, "file_mode", "%d", &v)) {
202 		vfs_mount_error(mp, "Invalid file_mode");
203 		error = EINVAL;
204 		goto bad;
205 	}
206 	smp->sm_file_mode = (v & (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFREG;
207 
208 	if (1 != vfs_scanopt(mp->mnt_optnew, "dir_mode", "%d", &v)) {
209 		vfs_mount_error(mp, "Invalid dir_mode");
210 		error = EINVAL;
211 		goto bad;
212 	}
213 	smp->sm_dir_mode  = (v & (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFDIR;
214 
215 	vfs_flagopt(mp->mnt_optnew,
216 	    "nolong", &smp->sm_flags, SMBFS_MOUNT_NO_LONG);
217 
218 	pc = mp->mnt_stat.f_mntfromname;
219 	pe = pc + sizeof(mp->mnt_stat.f_mntfromname);
220 	bzero(pc, MNAMELEN);
221 	*pc++ = '/';
222 	*pc++ = '/';
223 	pc = strchr(strncpy(pc, vcp->vc_username, pe - pc - 2), 0);
224 	if (pc < pe-1) {
225 		*(pc++) = '@';
226 		pc = strchr(strncpy(pc, vcp->vc_srvname, pe - pc - 2), 0);
227 		if (pc < pe - 1) {
228 			*(pc++) = '/';
229 			strncpy(pc, ssp->ss_name, pe - pc - 2);
230 		}
231 	}
232 	vfs_getnewfsid(mp);
233 	error = smbfs_root(mp, LK_EXCLUSIVE, &vp);
234 	if (error) {
235 		vfs_mount_error(mp, "smbfs_root error: %d", error);
236 		goto bad;
237 	}
238 	VOP_UNLOCK(vp);
239 	SMBVDEBUG("root.v_usecount = %d\n", vrefcnt(vp));
240 
241 #ifdef DIAGNOSTIC
242 	SMBERROR("mp=%p\n", mp);
243 #endif
244 	smbfs_free_scred(scred);
245 	return error;
246 bad:
247 	if (ssp)
248 		smb_share_put(ssp, scred);
249 	smbfs_free_scred(scred);
250 	SMB_LOCK();
251 	if (error && smp->sm_dev == dev) {
252 		smp->sm_dev = NULL;
253 		sdp_trydestroy(dev);
254 	}
255 	SMB_UNLOCK();
256 	free(smp, M_SMBFSDATA);
257 	return error;
258 }
259 
260 /* Unmount the filesystem described by mp. */
261 static int
262 smbfs_unmount(struct mount *mp, int mntflags)
263 {
264 	struct thread *td;
265 	struct smbmount *smp = VFSTOSMBFS(mp);
266 	struct smb_cred *scred;
267 	struct smb_dev *dev;
268 	int error, flags;
269 
270 	SMBVDEBUG("smbfs_unmount: flags=%04x\n", mntflags);
271 	td = curthread;
272 	flags = 0;
273 	if (mntflags & MNT_FORCE)
274 		flags |= FORCECLOSE;
275 	/*
276 	 * Keep trying to flush the vnode list for the mount while
277 	 * some are still busy and we are making progress towards
278 	 * making them not busy. This is needed because smbfs vnodes
279 	 * reference their parent directory but may appear after their
280 	 * parent in the list; one pass over the vnode list is not
281 	 * sufficient in this case.
282 	 */
283 	do {
284 		smp->sm_didrele = 0;
285 		/* There is 1 extra root vnode reference from smbfs_mount(). */
286 		error = vflush(mp, 1, flags, td);
287 	} while (error == EBUSY && smp->sm_didrele != 0);
288 	if (error)
289 		return error;
290 	scred = smbfs_malloc_scred();
291 	smb_makescred(scred, td, td->td_ucred);
292 	error = smb_share_lock(smp->sm_share);
293 	if (error)
294 		goto out;
295 	smb_share_put(smp->sm_share, scred);
296 	SMB_LOCK();
297 	dev = smp->sm_dev;
298 	if (!dev)
299 		panic("No private data for mount point");
300 	sdp_trydestroy(dev);
301 	mp->mnt_data = NULL;
302 	SMB_UNLOCK();
303 	free(smp, M_SMBFSDATA);
304 	MNT_ILOCK(mp);
305 	mp->mnt_flag &= ~MNT_LOCAL;
306 	MNT_IUNLOCK(mp);
307 out:
308 	smbfs_free_scred(scred);
309 	return error;
310 }
311 
312 /*
313  * Return locked root vnode of a filesystem
314  */
315 static int
316 smbfs_root(struct mount *mp, int flags, struct vnode **vpp)
317 {
318 	struct smbmount *smp = VFSTOSMBFS(mp);
319 	struct vnode *vp;
320 	struct smbnode *np;
321 	struct smbfattr fattr;
322 	struct thread *td;
323 	struct ucred *cred;
324 	struct smb_cred *scred;
325 	int error;
326 
327 	td = curthread;
328 	cred = td->td_ucred;
329 
330 	if (smp->sm_root) {
331 		*vpp = SMBTOV(smp->sm_root);
332 		return vget(*vpp, LK_EXCLUSIVE | LK_RETRY, td);
333 	}
334 	scred = smbfs_malloc_scred();
335 	smb_makescred(scred, td, cred);
336 	error = smbfs_smb_lookup(NULL, NULL, 0, &fattr, scred);
337 	if (error)
338 		goto out;
339 	error = smbfs_nget(mp, NULL, NULL, 0, &fattr, &vp);
340 	if (error)
341 		goto out;
342 	ASSERT_VOP_LOCKED(vp, "smbfs_root");
343 	vp->v_vflag |= VV_ROOT;
344 	np = VTOSMB(vp);
345 	smp->sm_root = np;
346 	*vpp = vp;
347 out:
348 	smbfs_free_scred(scred);
349 	return error;
350 }
351 
352 /*
353  * Do operations associated with quotas, not supported
354  */
355 /* ARGSUSED */
356 static int
357 smbfs_quotactl(mp, cmd, uid, arg)
358 	struct mount *mp;
359 	int cmd;
360 	uid_t uid;
361 	void *arg;
362 {
363 	SMBVDEBUG("return EOPNOTSUPP\n");
364 	return EOPNOTSUPP;
365 }
366 
367 /*ARGSUSED*/
368 int
369 smbfs_init(struct vfsconf *vfsp)
370 {
371 
372 	smbfs_pbuf_zone = pbuf_zsecond_create("smbpbuf", nswbuf / 2);
373 	SMBVDEBUG("done.\n");
374 	return 0;
375 }
376 
377 /*ARGSUSED*/
378 int
379 smbfs_uninit(struct vfsconf *vfsp)
380 {
381 
382 	uma_zdestroy(smbfs_pbuf_zone);
383 	SMBVDEBUG("done.\n");
384 	return 0;
385 }
386 
387 /*
388  * smbfs_statfs call
389  */
390 int
391 smbfs_statfs(struct mount *mp, struct statfs *sbp)
392 {
393 	struct thread *td = curthread;
394 	struct smbmount *smp = VFSTOSMBFS(mp);
395 	struct smbnode *np = smp->sm_root;
396 	struct smb_share *ssp = smp->sm_share;
397 	struct smb_cred *scred;
398 	int error;
399 
400 	if (np == NULL) {
401 		vfs_mount_error(mp, "np == NULL");
402 		return EINVAL;
403 	}
404 
405 	sbp->f_iosize = SSTOVC(ssp)->vc_txmax;		/* optimal transfer block size */
406 	scred = smbfs_malloc_scred();
407 	smb_makescred(scred, td, td->td_ucred);
408 	error = smbfs_smb_statfs(ssp, sbp, scred);
409 	smbfs_free_scred(scred);
410 	return (error);
411 }
412