1 /* $NetBSD: msdosfs_vfsops.c,v 1.103 2013/11/23 13:35:36 christos Exp $ */ 2 3 /*- 4 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank. 5 * Copyright (C) 1994, 1995, 1997 TooLs GmbH. 6 * All rights reserved. 7 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below). 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by TooLs GmbH. 20 * 4. The name of TooLs GmbH may not be used to endorse or promote products 21 * derived from this software without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 28 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 29 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 31 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 32 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 /* 35 * Written by Paul Popelka (paulp@uts.amdahl.com) 36 * 37 * You can do anything you want with this software, just don't say you wrote 38 * it, and don't remove this notice. 39 * 40 * This software is provided "as is". 41 * 42 * The author supplies this software to be publicly redistributed on the 43 * understanding that the author is not responsible for the correct 44 * functioning of this software in any circumstances and is not liable for 45 * any damages caused by this software. 46 * 47 * October 1992 48 */ 49 50 #include <sys/cdefs.h> 51 __KERNEL_RCSID(0, "$NetBSD: msdosfs_vfsops.c,v 1.103 2013/11/23 13:35:36 christos Exp $"); 52 53 #if defined(_KERNEL_OPT) 54 #include "opt_compat_netbsd.h" 55 #endif 56 57 #include <sys/param.h> 58 #include <sys/systm.h> 59 #include <sys/sysctl.h> 60 #include <sys/namei.h> 61 #include <sys/proc.h> 62 #include <sys/kernel.h> 63 #include <sys/vnode.h> 64 #include <miscfs/genfs/genfs.h> 65 #include <miscfs/specfs/specdev.h> /* XXX */ /* defines v_rdev */ 66 #include <sys/mount.h> 67 #include <sys/buf.h> 68 #include <sys/file.h> 69 #include <sys/device.h> 70 #include <sys/disklabel.h> 71 #include <sys/disk.h> 72 #include <sys/fstrans.h> 73 #include <sys/ioctl.h> 74 #include <sys/malloc.h> 75 #include <sys/dirent.h> 76 #include <sys/stat.h> 77 #include <sys/conf.h> 78 #include <sys/kauth.h> 79 #include <sys/module.h> 80 81 #include <fs/msdosfs/bpb.h> 82 #include <fs/msdosfs/bootsect.h> 83 #include <fs/msdosfs/direntry.h> 84 #include <fs/msdosfs/denode.h> 85 #include <fs/msdosfs/msdosfsmount.h> 86 #include <fs/msdosfs/fat.h> 87 88 MODULE(MODULE_CLASS_VFS, msdos, NULL); 89 90 #ifdef MSDOSFS_DEBUG 91 #define DPRINTF(a) uprintf a 92 #else 93 #define DPRINTF(a) 94 #endif 95 96 #define MSDOSFS_NAMEMAX(pmp) \ 97 (pmp)->pm_flags & MSDOSFSMNT_LONGNAME ? WIN_MAXLEN : 12 98 99 VFS_PROTOS(msdosfs); 100 101 int msdosfs_mountfs(struct vnode *, struct mount *, struct lwp *, 102 struct msdosfs_args *); 103 104 static int update_mp(struct mount *, struct msdosfs_args *); 105 106 MALLOC_JUSTDEFINE(M_MSDOSFSMNT, "MSDOSFS mount", "MSDOS FS mount structure"); 107 MALLOC_JUSTDEFINE(M_MSDOSFSFAT, "MSDOSFS FAT", "MSDOS FS FAT table"); 108 MALLOC_JUSTDEFINE(M_MSDOSFSTMP, "MSDOSFS temp", "MSDOS FS temp. structures"); 109 110 #define ROOTNAME "root_device" 111 112 static struct sysctllog *msdosfs_sysctl_log; 113 114 extern const struct vnodeopv_desc msdosfs_vnodeop_opv_desc; 115 116 const struct vnodeopv_desc * const msdosfs_vnodeopv_descs[] = { 117 &msdosfs_vnodeop_opv_desc, 118 NULL, 119 }; 120 121 struct vfsops msdosfs_vfsops = { 122 MOUNT_MSDOS, 123 sizeof (struct msdosfs_args), 124 msdosfs_mount, 125 msdosfs_start, 126 msdosfs_unmount, 127 msdosfs_root, 128 (void *)eopnotsupp, /* vfs_quotactl */ 129 msdosfs_statvfs, 130 msdosfs_sync, 131 msdosfs_vget, 132 msdosfs_fhtovp, 133 msdosfs_vptofh, 134 msdosfs_init, 135 msdosfs_reinit, 136 msdosfs_done, 137 msdosfs_mountroot, 138 (int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp, 139 vfs_stdextattrctl, 140 msdosfs_suspendctl, 141 genfs_renamelock_enter, 142 genfs_renamelock_exit, 143 (void *)eopnotsupp, 144 msdosfs_vnodeopv_descs, 145 0, 146 { NULL, NULL }, 147 }; 148 149 static int 150 msdos_modcmd(modcmd_t cmd, void *arg) 151 { 152 int error; 153 154 switch (cmd) { 155 case MODULE_CMD_INIT: 156 error = vfs_attach(&msdosfs_vfsops); 157 if (error != 0) 158 break; 159 sysctl_createv(&msdosfs_sysctl_log, 0, NULL, NULL, 160 CTLFLAG_PERMANENT, 161 CTLTYPE_NODE, "vfs", NULL, 162 NULL, 0, NULL, 0, 163 CTL_VFS, CTL_EOL); 164 sysctl_createv(&msdosfs_sysctl_log, 0, NULL, NULL, 165 CTLFLAG_PERMANENT, 166 CTLTYPE_NODE, "msdosfs", 167 SYSCTL_DESCR("MS-DOS file system"), 168 NULL, 0, NULL, 0, 169 CTL_VFS, 4, CTL_EOL); 170 /* 171 * XXX the "4" above could be dynamic, thereby eliminating one 172 * more instance of the "number to vfs" mapping problem, but 173 * "4" is the order as taken from sys/mount.h 174 */ 175 break; 176 case MODULE_CMD_FINI: 177 error = vfs_detach(&msdosfs_vfsops); 178 if (error != 0) 179 break; 180 sysctl_teardown(&msdosfs_sysctl_log); 181 break; 182 default: 183 error = ENOTTY; 184 break; 185 } 186 187 return (error); 188 } 189 190 static int 191 update_mp(struct mount *mp, struct msdosfs_args *argp) 192 { 193 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 194 int error; 195 196 pmp->pm_gid = argp->gid; 197 pmp->pm_uid = argp->uid; 198 pmp->pm_mask = argp->mask & ALLPERMS; 199 pmp->pm_dirmask = argp->dirmask & ALLPERMS; 200 pmp->pm_gmtoff = argp->gmtoff; 201 pmp->pm_flags |= argp->flags & MSDOSFSMNT_MNTOPT; 202 203 /* 204 * GEMDOS knows nothing about win95 long filenames 205 */ 206 if (pmp->pm_flags & MSDOSFSMNT_GEMDOSFS) 207 pmp->pm_flags |= MSDOSFSMNT_NOWIN95; 208 209 if (pmp->pm_flags & MSDOSFSMNT_NOWIN95) 210 pmp->pm_flags |= MSDOSFSMNT_SHORTNAME; 211 else if (!(pmp->pm_flags & 212 (MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) { 213 struct vnode *rtvp; 214 215 /* 216 * Try to divine whether to support Win'95 long filenames 217 */ 218 if (FAT32(pmp)) 219 pmp->pm_flags |= MSDOSFSMNT_LONGNAME; 220 else { 221 if ((error = msdosfs_root(mp, &rtvp)) != 0) 222 return error; 223 pmp->pm_flags |= findwin95(VTODE(rtvp)) 224 ? MSDOSFSMNT_LONGNAME 225 : MSDOSFSMNT_SHORTNAME; 226 vput(rtvp); 227 } 228 } 229 230 mp->mnt_stat.f_namemax = MSDOSFS_NAMEMAX(pmp); 231 232 return 0; 233 } 234 235 int 236 msdosfs_mountroot(void) 237 { 238 struct mount *mp; 239 struct lwp *l = curlwp; /* XXX */ 240 int error; 241 struct msdosfs_args args; 242 243 if (device_class(root_device) != DV_DISK) 244 return (ENODEV); 245 246 if ((error = vfs_rootmountalloc(MOUNT_MSDOS, "root_device", &mp))) { 247 vrele(rootvp); 248 return (error); 249 } 250 251 args.flags = MSDOSFSMNT_VERSIONED; 252 args.uid = 0; 253 args.gid = 0; 254 args.mask = 0777; 255 args.version = MSDOSFSMNT_VERSION; 256 args.dirmask = 0777; 257 258 if ((error = msdosfs_mountfs(rootvp, mp, l, &args)) != 0) { 259 vfs_unbusy(mp, false, NULL); 260 vfs_destroy(mp); 261 return (error); 262 } 263 264 if ((error = update_mp(mp, &args)) != 0) { 265 (void)msdosfs_unmount(mp, 0); 266 vfs_unbusy(mp, false, NULL); 267 vfs_destroy(mp); 268 vrele(rootvp); 269 return (error); 270 } 271 272 mountlist_append(mp); 273 (void)msdosfs_statvfs(mp, &mp->mnt_stat); 274 vfs_unbusy(mp, false, NULL); 275 return (0); 276 } 277 278 /* 279 * mp - path - addr in user space of mount point (ie /usr or whatever) 280 * data - addr in user space of mount params including the name of the block 281 * special file to treat as a filesystem. 282 */ 283 int 284 msdosfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len) 285 { 286 struct lwp *l = curlwp; 287 struct vnode *devvp; /* vnode for blk device to mount */ 288 struct msdosfs_args *args = data; /* holds data from mount request */ 289 /* msdosfs specific mount control block */ 290 struct msdosfsmount *pmp = NULL; 291 int error, flags; 292 mode_t accessmode; 293 294 if (*data_len < sizeof *args) 295 return EINVAL; 296 297 if (mp->mnt_flag & MNT_GETARGS) { 298 pmp = VFSTOMSDOSFS(mp); 299 if (pmp == NULL) 300 return EIO; 301 args->fspec = NULL; 302 args->uid = pmp->pm_uid; 303 args->gid = pmp->pm_gid; 304 args->mask = pmp->pm_mask; 305 args->flags = pmp->pm_flags; 306 args->version = MSDOSFSMNT_VERSION; 307 args->dirmask = pmp->pm_dirmask; 308 args->gmtoff = pmp->pm_gmtoff; 309 *data_len = sizeof *args; 310 return 0; 311 } 312 313 /* 314 * If not versioned (i.e. using old mount_msdos(8)), fill in 315 * the additional structure items with suitable defaults. 316 */ 317 if ((args->flags & MSDOSFSMNT_VERSIONED) == 0) { 318 args->version = 1; 319 args->dirmask = args->mask; 320 } 321 322 /* 323 * Reset GMT offset for pre-v3 mount structure args. 324 */ 325 if (args->version < 3) 326 args->gmtoff = 0; 327 328 /* 329 * If updating, check whether changing from read-only to 330 * read/write; if there is no device name, that's all we do. 331 */ 332 if (mp->mnt_flag & MNT_UPDATE) { 333 pmp = VFSTOMSDOSFS(mp); 334 error = 0; 335 if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) && 336 (mp->mnt_flag & MNT_RDONLY)) { 337 flags = WRITECLOSE; 338 if (mp->mnt_flag & MNT_FORCE) 339 flags |= FORCECLOSE; 340 error = vflush(mp, NULLVP, flags); 341 } 342 if (!error && (mp->mnt_flag & MNT_RELOAD)) 343 /* not yet implemented */ 344 error = EOPNOTSUPP; 345 if (error) { 346 DPRINTF(("vflush %d\n", error)); 347 return (error); 348 } 349 if ((pmp->pm_flags & MSDOSFSMNT_RONLY) && 350 (mp->mnt_iflag & IMNT_WANTRDWR)) { 351 /* 352 * If upgrade to read-write by non-root, then verify 353 * that user has necessary permissions on the device. 354 * 355 * Permission to update a mount is checked higher, so 356 * here we presume updating the mount is okay (for 357 * example, as far as securelevel goes) which leaves us 358 * with the normal check. 359 */ 360 devvp = pmp->pm_devvp; 361 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 362 error = kauth_authorize_system(l->l_cred, 363 KAUTH_SYSTEM_MOUNT, KAUTH_REQ_SYSTEM_MOUNT_DEVICE, 364 mp, devvp, KAUTH_ARG(VREAD | VWRITE)); 365 VOP_UNLOCK(devvp); 366 DPRINTF(("KAUTH_REQ_SYSTEM_MOUNT_DEVICE %d\n", error)); 367 if (error) 368 return (error); 369 370 pmp->pm_flags &= ~MSDOSFSMNT_RONLY; 371 } 372 if (args->fspec == NULL) { 373 DPRINTF(("missing fspec\n")); 374 return EINVAL; 375 } 376 } 377 /* 378 * Not an update, or updating the name: look up the name 379 * and verify that it refers to a sensible block device. 380 */ 381 error = namei_simple_user(args->fspec, 382 NSM_FOLLOW_NOEMULROOT, &devvp); 383 if (error != 0) { 384 DPRINTF(("namei %d\n", error)); 385 return (error); 386 } 387 388 if (devvp->v_type != VBLK) { 389 DPRINTF(("not block\n")); 390 vrele(devvp); 391 return (ENOTBLK); 392 } 393 if (bdevsw_lookup(devvp->v_rdev) == NULL) { 394 DPRINTF(("no block switch\n")); 395 vrele(devvp); 396 return (ENXIO); 397 } 398 /* 399 * If mount by non-root, then verify that user has necessary 400 * permissions on the device. 401 */ 402 accessmode = VREAD; 403 if ((mp->mnt_flag & MNT_RDONLY) == 0) 404 accessmode |= VWRITE; 405 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 406 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT, 407 KAUTH_REQ_SYSTEM_MOUNT_DEVICE, mp, devvp, KAUTH_ARG(accessmode)); 408 VOP_UNLOCK(devvp); 409 if (error) { 410 DPRINTF(("KAUTH_REQ_SYSTEM_MOUNT_DEVICE %d\n", error)); 411 vrele(devvp); 412 return (error); 413 } 414 if ((mp->mnt_flag & MNT_UPDATE) == 0) { 415 int xflags; 416 417 if (mp->mnt_flag & MNT_RDONLY) 418 xflags = FREAD; 419 else 420 xflags = FREAD|FWRITE; 421 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 422 error = VOP_OPEN(devvp, xflags, FSCRED); 423 VOP_UNLOCK(devvp); 424 if (error) { 425 DPRINTF(("VOP_OPEN %d\n", error)); 426 goto fail; 427 } 428 error = msdosfs_mountfs(devvp, mp, l, args); 429 if (error) { 430 DPRINTF(("msdosfs_mountfs %d\n", error)); 431 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 432 (void) VOP_CLOSE(devvp, xflags, NOCRED); 433 VOP_UNLOCK(devvp); 434 goto fail; 435 } 436 #ifdef MSDOSFS_DEBUG /* only needed for the printf below */ 437 pmp = VFSTOMSDOSFS(mp); 438 #endif 439 } else { 440 vrele(devvp); 441 if (devvp != pmp->pm_devvp) { 442 DPRINTF(("devvp %p pmp %p\n", 443 devvp, pmp->pm_devvp)); 444 return (EINVAL); /* needs translation */ 445 } 446 } 447 if ((error = update_mp(mp, args)) != 0) { 448 msdosfs_unmount(mp, MNT_FORCE); 449 DPRINTF(("update_mp %d\n", error)); 450 return error; 451 } 452 453 #ifdef MSDOSFS_DEBUG 454 printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap); 455 #endif 456 return set_statvfs_info(path, UIO_USERSPACE, args->fspec, UIO_USERSPACE, 457 mp->mnt_op->vfs_name, mp, l); 458 459 fail: 460 vrele(devvp); 461 return (error); 462 } 463 464 int 465 msdosfs_mountfs(struct vnode *devvp, struct mount *mp, struct lwp *l, struct msdosfs_args *argp) 466 { 467 struct msdosfsmount *pmp; 468 struct buf *bp; 469 dev_t dev = devvp->v_rdev; 470 union bootsector *bsp; 471 struct byte_bpb33 *b33; 472 struct byte_bpb50 *b50; 473 struct byte_bpb710 *b710; 474 uint8_t SecPerClust; 475 int ronly, error, tmp; 476 int bsize; 477 uint64_t psize; 478 unsigned secsize; 479 480 /* Flush out any old buffers remaining from a previous use. */ 481 if ((error = vinvalbuf(devvp, V_SAVE, l->l_cred, l, 0, 0)) != 0) 482 return (error); 483 484 ronly = (mp->mnt_flag & MNT_RDONLY) != 0; 485 486 bp = NULL; /* both used in error_exit */ 487 pmp = NULL; 488 489 error = fstrans_mount(mp); 490 if (error) 491 goto error_exit; 492 493 error = getdisksize(devvp, &psize, &secsize); 494 if (error) { 495 if (argp->flags & MSDOSFSMNT_GEMDOSFS) 496 goto error_exit; 497 498 /* ok, so it failed. we most likely don't need the info */ 499 secsize = DEV_BSIZE; 500 psize = 0; 501 error = 0; 502 } 503 504 if (argp->flags & MSDOSFSMNT_GEMDOSFS) { 505 bsize = secsize; 506 if (bsize != 512) { 507 DPRINTF(("Invalid block bsize %d for GEMDOS\n", bsize)); 508 error = EINVAL; 509 goto error_exit; 510 } 511 } else 512 bsize = 0; 513 514 /* 515 * Read the boot sector of the filesystem, and then check the 516 * boot signature. If not a dos boot sector then error out. 517 */ 518 if ((error = bread(devvp, 0, secsize, NOCRED, 0, &bp)) != 0) 519 goto error_exit; 520 bsp = (union bootsector *)bp->b_data; 521 b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB; 522 b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB; 523 b710 = (struct byte_bpb710 *)bsp->bs710.bsBPB; 524 525 if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) { 526 if (bsp->bs50.bsBootSectSig0 != BOOTSIG0 527 || bsp->bs50.bsBootSectSig1 != BOOTSIG1) { 528 DPRINTF(("bootsig0 %d bootsig1 %d\n", 529 bsp->bs50.bsBootSectSig0, 530 bsp->bs50.bsBootSectSig1)); 531 error = EINVAL; 532 goto error_exit; 533 } 534 } 535 536 pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK); 537 memset(pmp, 0, sizeof *pmp); 538 pmp->pm_mountp = mp; 539 540 /* 541 * Compute several useful quantities from the bpb in the 542 * bootsector. Copy in the dos 5 variant of the bpb then fix up 543 * the fields that are different between dos 5 and dos 3.3. 544 */ 545 SecPerClust = b50->bpbSecPerClust; 546 pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec); 547 pmp->pm_ResSectors = getushort(b50->bpbResSectors); 548 pmp->pm_FATs = b50->bpbFATs; 549 pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts); 550 pmp->pm_Sectors = getushort(b50->bpbSectors); 551 pmp->pm_FATsecs = getushort(b50->bpbFATsecs); 552 pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack); 553 pmp->pm_Heads = getushort(b50->bpbHeads); 554 pmp->pm_Media = b50->bpbMedia; 555 556 if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) { 557 /* XXX - We should probably check more values here */ 558 if (!pmp->pm_BytesPerSec || !SecPerClust 559 || pmp->pm_SecPerTrack > 63) { 560 DPRINTF(("bytespersec %d secperclust %d " 561 "secpertrack %d\n", 562 pmp->pm_BytesPerSec, SecPerClust, 563 pmp->pm_SecPerTrack)); 564 error = EINVAL; 565 goto error_exit; 566 } 567 } 568 569 if (pmp->pm_Sectors == 0) { 570 pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs); 571 pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors); 572 } else { 573 pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs); 574 pmp->pm_HugeSectors = pmp->pm_Sectors; 575 } 576 577 if (pmp->pm_RootDirEnts == 0) { 578 unsigned short vers = getushort(b710->bpbFSVers); 579 /* 580 * Some say that bsBootSectSig[23] must be zero, but 581 * Windows does not require this and some digital cameras 582 * do not set these to zero. Therefore, do not insist. 583 */ 584 if (pmp->pm_Sectors || pmp->pm_FATsecs || vers) { 585 DPRINTF(("sectors %d fatsecs %lu vers %d\n", 586 pmp->pm_Sectors, pmp->pm_FATsecs, vers)); 587 error = EINVAL; 588 goto error_exit; 589 } 590 pmp->pm_fatmask = FAT32_MASK; 591 pmp->pm_fatmult = 4; 592 pmp->pm_fatdiv = 1; 593 pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs); 594 595 /* mirrorring is enabled if the FATMIRROR bit is not set */ 596 if ((getushort(b710->bpbExtFlags) & FATMIRROR) == 0) 597 pmp->pm_flags |= MSDOSFS_FATMIRROR; 598 else 599 pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM; 600 } else 601 pmp->pm_flags |= MSDOSFS_FATMIRROR; 602 603 if (argp->flags & MSDOSFSMNT_GEMDOSFS) { 604 if (FAT32(pmp)) { 605 DPRINTF(("FAT32 for GEMDOS\n")); 606 /* 607 * GEMDOS doesn't know FAT32. 608 */ 609 error = EINVAL; 610 goto error_exit; 611 } 612 613 /* 614 * Check a few values (could do some more): 615 * - logical sector size: power of 2, >= block size 616 * - sectors per cluster: power of 2, >= 1 617 * - number of sectors: >= 1, <= size of partition 618 */ 619 if ( (SecPerClust == 0) 620 || (SecPerClust & (SecPerClust - 1)) 621 || (pmp->pm_BytesPerSec < bsize) 622 || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1)) 623 || (pmp->pm_HugeSectors == 0) 624 || (pmp->pm_HugeSectors * (pmp->pm_BytesPerSec / bsize) 625 > psize)) { 626 DPRINTF(("consistency checks for GEMDOS\n")); 627 error = EINVAL; 628 goto error_exit; 629 } 630 /* 631 * XXX - Many parts of the msdosfs driver seem to assume that 632 * the number of bytes per logical sector (BytesPerSec) will 633 * always be the same as the number of bytes per disk block 634 * Let's pretend it is. 635 */ 636 tmp = pmp->pm_BytesPerSec / bsize; 637 pmp->pm_BytesPerSec = bsize; 638 pmp->pm_HugeSectors *= tmp; 639 pmp->pm_HiddenSects *= tmp; 640 pmp->pm_ResSectors *= tmp; 641 pmp->pm_Sectors *= tmp; 642 pmp->pm_FATsecs *= tmp; 643 SecPerClust *= tmp; 644 } 645 646 /* Check that fs has nonzero FAT size */ 647 if (pmp->pm_FATsecs == 0) { 648 DPRINTF(("FATsecs is 0\n")); 649 error = EINVAL; 650 goto error_exit; 651 } 652 653 pmp->pm_fatblk = pmp->pm_ResSectors; 654 if (FAT32(pmp)) { 655 pmp->pm_rootdirblk = getulong(b710->bpbRootClust); 656 pmp->pm_firstcluster = pmp->pm_fatblk 657 + (pmp->pm_FATs * pmp->pm_FATsecs); 658 pmp->pm_fsinfo = getushort(b710->bpbFSInfo); 659 } else { 660 pmp->pm_rootdirblk = pmp->pm_fatblk + 661 (pmp->pm_FATs * pmp->pm_FATsecs); 662 pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry) 663 + pmp->pm_BytesPerSec - 1) 664 / pmp->pm_BytesPerSec;/* in sectors */ 665 pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize; 666 } 667 668 pmp->pm_nmbrofclusters = (pmp->pm_HugeSectors - pmp->pm_firstcluster) / 669 SecPerClust; 670 pmp->pm_maxcluster = pmp->pm_nmbrofclusters + 1; 671 pmp->pm_fatsize = pmp->pm_FATsecs * pmp->pm_BytesPerSec; 672 673 if (argp->flags & MSDOSFSMNT_GEMDOSFS) { 674 if (pmp->pm_nmbrofclusters <= (0xff0 - 2)) { 675 pmp->pm_fatmask = FAT12_MASK; 676 pmp->pm_fatmult = 3; 677 pmp->pm_fatdiv = 2; 678 } else { 679 pmp->pm_fatmask = FAT16_MASK; 680 pmp->pm_fatmult = 2; 681 pmp->pm_fatdiv = 1; 682 } 683 } else if (pmp->pm_fatmask == 0) { 684 if (pmp->pm_maxcluster 685 <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) { 686 /* 687 * This will usually be a floppy disk. This size makes 688 * sure that one FAT entry will not be split across 689 * multiple blocks. 690 */ 691 pmp->pm_fatmask = FAT12_MASK; 692 pmp->pm_fatmult = 3; 693 pmp->pm_fatdiv = 2; 694 } else { 695 pmp->pm_fatmask = FAT16_MASK; 696 pmp->pm_fatmult = 2; 697 pmp->pm_fatdiv = 1; 698 } 699 } 700 if (FAT12(pmp)) 701 pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec; 702 else 703 pmp->pm_fatblocksize = MAXBSIZE; 704 705 pmp->pm_fatblocksec = pmp->pm_fatblocksize / pmp->pm_BytesPerSec; 706 pmp->pm_bnshift = ffs(pmp->pm_BytesPerSec) - 1; 707 708 /* 709 * Compute mask and shift value for isolating cluster relative byte 710 * offsets and cluster numbers from a file offset. 711 */ 712 pmp->pm_bpcluster = SecPerClust * pmp->pm_BytesPerSec; 713 pmp->pm_crbomask = pmp->pm_bpcluster - 1; 714 pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1; 715 716 /* 717 * Check for valid cluster size 718 * must be a power of 2 719 */ 720 if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) { 721 DPRINTF(("bpcluster %lu cnshift %lu\n", 722 pmp->pm_bpcluster, pmp->pm_cnshift)); 723 error = EINVAL; 724 goto error_exit; 725 } 726 727 /* 728 * Cluster size must be within limit of MAXBSIZE. 729 * Many FAT filesystems will not have clusters larger than 730 * 32KiB due to limits in Windows versions before Vista. 731 */ 732 if (pmp->pm_bpcluster > MAXBSIZE) { 733 DPRINTF(("bpcluster %lu > MAXBSIZE %d\n", 734 pmp->pm_bpcluster, MAXBSIZE)); 735 error = EINVAL; 736 goto error_exit; 737 } 738 739 /* 740 * Release the bootsector buffer. 741 */ 742 brelse(bp, BC_AGE); 743 bp = NULL; 744 745 /* 746 * Check FSInfo. 747 */ 748 if (pmp->pm_fsinfo) { 749 struct fsinfo *fp; 750 751 /* 752 * XXX If the fsinfo block is stored on media with 753 * 2KB or larger sectors, is the fsinfo structure 754 * padded at the end or in the middle? 755 */ 756 if ((error = bread(devvp, de_bn2kb(pmp, pmp->pm_fsinfo), 757 pmp->pm_BytesPerSec, NOCRED, 0, &bp)) != 0) 758 goto error_exit; 759 fp = (struct fsinfo *)bp->b_data; 760 if (!memcmp(fp->fsisig1, "RRaA", 4) 761 && !memcmp(fp->fsisig2, "rrAa", 4) 762 && !memcmp(fp->fsisig3, "\0\0\125\252", 4) 763 && !memcmp(fp->fsisig4, "\0\0\125\252", 4)) 764 pmp->pm_nxtfree = getulong(fp->fsinxtfree); 765 else 766 pmp->pm_fsinfo = 0; 767 brelse(bp, 0); 768 bp = NULL; 769 } 770 771 /* 772 * Check and validate (or perhaps invalidate?) the fsinfo structure? 773 * XXX 774 */ 775 if (pmp->pm_fsinfo) { 776 if ((pmp->pm_nxtfree == 0xffffffffUL) || 777 (pmp->pm_nxtfree > pmp->pm_maxcluster)) 778 pmp->pm_fsinfo = 0; 779 } 780 781 /* 782 * Allocate memory for the bitmap of allocated clusters, and then 783 * fill it in. 784 */ 785 pmp->pm_inusemap = malloc(((pmp->pm_maxcluster + N_INUSEBITS) 786 / N_INUSEBITS) 787 * sizeof(*pmp->pm_inusemap), 788 M_MSDOSFSFAT, M_WAITOK); 789 790 /* 791 * fillinusemap() needs pm_devvp. 792 */ 793 pmp->pm_dev = dev; 794 pmp->pm_devvp = devvp; 795 796 /* 797 * Have the inuse map filled in. 798 */ 799 if ((error = fillinusemap(pmp)) != 0) { 800 DPRINTF(("fillinusemap %d\n", error)); 801 goto error_exit; 802 } 803 804 /* 805 * If they want FAT updates to be synchronous then let them suffer 806 * the performance degradation in exchange for the on disk copy of 807 * the FAT being correct just about all the time. I suppose this 808 * would be a good thing to turn on if the kernel is still flakey. 809 */ 810 if (mp->mnt_flag & MNT_SYNCHRONOUS) 811 pmp->pm_flags |= MSDOSFSMNT_WAITONFAT; 812 813 /* 814 * Finish up. 815 */ 816 if (ronly) 817 pmp->pm_flags |= MSDOSFSMNT_RONLY; 818 else 819 pmp->pm_fmod = 1; 820 mp->mnt_data = pmp; 821 mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev; 822 mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_MSDOS); 823 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0]; 824 mp->mnt_stat.f_namemax = MSDOSFS_NAMEMAX(pmp); 825 mp->mnt_flag |= MNT_LOCAL; 826 mp->mnt_dev_bshift = pmp->pm_bnshift; 827 mp->mnt_fs_bshift = pmp->pm_cnshift; 828 829 /* 830 * If we ever do quotas for DOS filesystems this would be a place 831 * to fill in the info in the msdosfsmount structure. You dolt, 832 * quotas on dos filesystems make no sense because files have no 833 * owners on dos filesystems. of course there is some empty space 834 * in the directory entry where we could put uid's and gid's. 835 */ 836 837 spec_node_setmountedfs(devvp, mp); 838 839 return (0); 840 841 error_exit: 842 fstrans_unmount(mp); 843 if (bp) 844 brelse(bp, BC_AGE); 845 if (pmp) { 846 if (pmp->pm_inusemap) 847 free(pmp->pm_inusemap, M_MSDOSFSFAT); 848 free(pmp, M_MSDOSFSMNT); 849 mp->mnt_data = NULL; 850 } 851 return (error); 852 } 853 854 int 855 msdosfs_start(struct mount *mp, int flags) 856 { 857 858 return (0); 859 } 860 861 /* 862 * Unmount the filesystem described by mp. 863 */ 864 int 865 msdosfs_unmount(struct mount *mp, int mntflags) 866 { 867 struct msdosfsmount *pmp; 868 int error, flags; 869 870 flags = 0; 871 if (mntflags & MNT_FORCE) 872 flags |= FORCECLOSE; 873 if ((error = vflush(mp, NULLVP, flags)) != 0) 874 return (error); 875 pmp = VFSTOMSDOSFS(mp); 876 if (pmp->pm_devvp->v_type != VBAD) 877 spec_node_setmountedfs(pmp->pm_devvp, NULL); 878 #ifdef MSDOSFS_DEBUG 879 { 880 struct vnode *vp = pmp->pm_devvp; 881 882 printf("msdosfs_umount(): just before calling VOP_CLOSE()\n"); 883 printf("flag %08x, usecount %d, writecount %d, holdcnt %d\n", 884 vp->v_vflag | vp->v_iflag | vp->v_uflag, vp->v_usecount, 885 vp->v_writecount, vp->v_holdcnt); 886 printf("mount %p, op %p\n", 887 vp->v_mount, vp->v_op); 888 printf("freef %p, freeb %p, mount %p\n", 889 vp->v_freelist.tqe_next, vp->v_freelist.tqe_prev, 890 vp->v_mount); 891 printf("cleanblkhd %p, dirtyblkhd %p, numoutput %d, type %d\n", 892 vp->v_cleanblkhd.lh_first, 893 vp->v_dirtyblkhd.lh_first, 894 vp->v_numoutput, vp->v_type); 895 printf("union %p, tag %d, data[0] %08x, data[1] %08x\n", 896 vp->v_socket, vp->v_tag, 897 ((u_int *)vp->v_data)[0], 898 ((u_int *)vp->v_data)[1]); 899 } 900 #endif 901 vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY); 902 (void) VOP_CLOSE(pmp->pm_devvp, 903 pmp->pm_flags & MSDOSFSMNT_RONLY ? FREAD : FREAD|FWRITE, NOCRED); 904 vput(pmp->pm_devvp); 905 msdosfs_fh_destroy(pmp); 906 free(pmp->pm_inusemap, M_MSDOSFSFAT); 907 free(pmp, M_MSDOSFSMNT); 908 mp->mnt_data = NULL; 909 mp->mnt_flag &= ~MNT_LOCAL; 910 fstrans_unmount(mp); 911 return (0); 912 } 913 914 int 915 msdosfs_root(struct mount *mp, struct vnode **vpp) 916 { 917 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 918 struct denode *ndep; 919 int error; 920 921 #ifdef MSDOSFS_DEBUG 922 printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp); 923 #endif 924 if ((error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep)) != 0) 925 return (error); 926 *vpp = DETOV(ndep); 927 return (0); 928 } 929 930 int 931 msdosfs_statvfs(struct mount *mp, struct statvfs *sbp) 932 { 933 struct msdosfsmount *pmp; 934 935 pmp = VFSTOMSDOSFS(mp); 936 sbp->f_bsize = pmp->pm_bpcluster; 937 sbp->f_frsize = sbp->f_bsize; 938 sbp->f_iosize = pmp->pm_bpcluster; 939 sbp->f_blocks = pmp->pm_nmbrofclusters; 940 sbp->f_bfree = pmp->pm_freeclustercount; 941 sbp->f_bavail = pmp->pm_freeclustercount; 942 sbp->f_bresvd = 0; 943 sbp->f_files = pmp->pm_RootDirEnts; /* XXX */ 944 sbp->f_ffree = 0; /* what to put in here? */ 945 sbp->f_favail = 0; /* what to put in here? */ 946 sbp->f_fresvd = 0; 947 copy_statvfs_info(sbp, mp); 948 return (0); 949 } 950 951 int 952 msdosfs_sync(struct mount *mp, int waitfor, kauth_cred_t cred) 953 { 954 struct vnode *vp, *mvp; 955 struct denode *dep; 956 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 957 int error, allerror = 0; 958 959 /* 960 * If we ever switch to not updating all of the FATs all the time, 961 * this would be the place to update them from the first one. 962 */ 963 if (pmp->pm_fmod != 0) { 964 if (pmp->pm_flags & MSDOSFSMNT_RONLY) 965 panic("msdosfs_sync: rofs mod"); 966 else { 967 /* update FATs here */ 968 } 969 } 970 /* Allocate a marker vnode. */ 971 mvp = vnalloc(mp); 972 fstrans_start(mp, FSTRANS_SHARED); 973 /* 974 * Write back each (modified) denode. 975 */ 976 mutex_enter(&mntvnode_lock); 977 loop: 978 for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = vunmark(mvp)) { 979 vmark(mvp, vp); 980 if (vp->v_mount != mp || vismarker(vp)) 981 continue; 982 mutex_enter(vp->v_interlock); 983 dep = VTODE(vp); 984 if (waitfor == MNT_LAZY || vp->v_type == VNON || 985 dep == NULL || (((dep->de_flag & 986 (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0) && 987 (LIST_EMPTY(&vp->v_dirtyblkhd) && 988 UVM_OBJ_IS_CLEAN(&vp->v_uobj)))) { 989 mutex_exit(vp->v_interlock); 990 continue; 991 } 992 mutex_exit(&mntvnode_lock); 993 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT); 994 if (error) { 995 mutex_enter(&mntvnode_lock); 996 if (error == ENOENT) { 997 (void)vunmark(mvp); 998 goto loop; 999 } 1000 continue; 1001 } 1002 if ((error = VOP_FSYNC(vp, cred, 1003 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0)) != 0) 1004 allerror = error; 1005 vput(vp); 1006 mutex_enter(&mntvnode_lock); 1007 } 1008 mutex_exit(&mntvnode_lock); 1009 vnfree(mvp); 1010 1011 /* 1012 * Force stale file system control information to be flushed. 1013 */ 1014 if ((error = VOP_FSYNC(pmp->pm_devvp, cred, 1015 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0)) != 0) 1016 allerror = error; 1017 fstrans_done(mp); 1018 return (allerror); 1019 } 1020 1021 int 1022 msdosfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp) 1023 { 1024 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 1025 struct defid defh; 1026 struct denode *dep; 1027 uint32_t gen; 1028 int error; 1029 1030 if (fhp->fid_len != sizeof(struct defid)) { 1031 DPRINTF(("fid_len %d %zd\n", fhp->fid_len, 1032 sizeof(struct defid))); 1033 return EINVAL; 1034 } 1035 memcpy(&defh, fhp, sizeof(defh)); 1036 error = msdosfs_fh_lookup(pmp, defh.defid_dirclust, defh.defid_dirofs, 1037 &gen); 1038 if (error == 0 && gen != defh.defid_gen) 1039 error = ESTALE; 1040 if (error) { 1041 *vpp = NULLVP; 1042 return error; 1043 } 1044 error = deget(pmp, defh.defid_dirclust, defh.defid_dirofs, &dep); 1045 if (error) { 1046 DPRINTF(("deget %d\n", error)); 1047 *vpp = NULLVP; 1048 return (error); 1049 } 1050 *vpp = DETOV(dep); 1051 return (0); 1052 } 1053 1054 int 1055 msdosfs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size) 1056 { 1057 struct msdosfsmount *pmp = VFSTOMSDOSFS(vp->v_mount); 1058 struct denode *dep; 1059 struct defid defh; 1060 int error; 1061 1062 if (*fh_size < sizeof(struct defid)) { 1063 *fh_size = sizeof(struct defid); 1064 return E2BIG; 1065 } 1066 *fh_size = sizeof(struct defid); 1067 dep = VTODE(vp); 1068 memset(&defh, 0, sizeof(defh)); 1069 defh.defid_len = sizeof(struct defid); 1070 defh.defid_dirclust = dep->de_dirclust; 1071 defh.defid_dirofs = dep->de_diroffset; 1072 error = msdosfs_fh_enter(pmp, dep->de_dirclust, dep->de_diroffset, 1073 &defh.defid_gen); 1074 if (error == 0) 1075 memcpy(fhp, &defh, sizeof(defh)); 1076 return error; 1077 } 1078 1079 int 1080 msdosfs_vget(struct mount *mp, ino_t ino, 1081 struct vnode **vpp) 1082 { 1083 1084 return (EOPNOTSUPP); 1085 } 1086 1087 int 1088 msdosfs_suspendctl(struct mount *mp, int cmd) 1089 { 1090 int error; 1091 struct lwp *l = curlwp; 1092 1093 switch (cmd) { 1094 case SUSPEND_SUSPEND: 1095 if ((error = fstrans_setstate(mp, FSTRANS_SUSPENDING)) != 0) 1096 return error; 1097 error = msdosfs_sync(mp, MNT_WAIT, l->l_proc->p_cred); 1098 if (error == 0) 1099 error = fstrans_setstate(mp, FSTRANS_SUSPENDED); 1100 if (error != 0) { 1101 (void) fstrans_setstate(mp, FSTRANS_NORMAL); 1102 return error; 1103 } 1104 return 0; 1105 1106 case SUSPEND_RESUME: 1107 return fstrans_setstate(mp, FSTRANS_NORMAL); 1108 1109 default: 1110 return EINVAL; 1111 } 1112 } 1113