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