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