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