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