msdosfs_vfsops.c revision 53059
1/* $FreeBSD: head/sys/fs/msdosfs/msdosfs_vfsops.c 53059 1999-11-09 14:15:33Z phk $ */ 2/* $NetBSD: msdosfs_vfsops.c,v 1.51 1997/11/17 15:36:58 ws Exp $ */ 3 4/*- 5 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank. 6 * Copyright (C) 1994, 1995, 1997 TooLs GmbH. 7 * All rights reserved. 8 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below). 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by TooLs GmbH. 21 * 4. The name of TooLs GmbH may not be used to endorse or promote products 22 * derived from this software without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 27 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 30 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 */ 35/* 36 * Written by Paul Popelka (paulp@uts.amdahl.com) 37 * 38 * You can do anything you want with this software, just don't say you wrote 39 * it, and don't remove this notice. 40 * 41 * This software is provided "as is". 42 * 43 * The author supplies this software to be publicly redistributed on the 44 * understanding that the author is not responsible for the correct 45 * functioning of this software in any circumstances and is not liable for 46 * any damages caused by this software. 47 * 48 * October 1992 49 */ 50 51#include <sys/param.h> 52#include <sys/systm.h> 53#include <sys/conf.h> 54#include <sys/namei.h> 55#include <sys/proc.h> 56#include <sys/kernel.h> 57#include <sys/vnode.h> 58#include <sys/mount.h> 59#include <sys/buf.h> 60#include <sys/fcntl.h> 61#include <sys/malloc.h> 62#include <sys/stat.h> /* defines ALLPERMS */ 63 64#include <msdosfs/bpb.h> 65#include <msdosfs/bootsect.h> 66#include <msdosfs/direntry.h> 67#include <msdosfs/denode.h> 68#include <msdosfs/msdosfsmount.h> 69#include <msdosfs/fat.h> 70 71MALLOC_DEFINE(M_MSDOSFSMNT, "MSDOSFS mount", "MSDOSFS mount structure"); 72static MALLOC_DEFINE(M_MSDOSFSFAT, "MSDOSFS FAT", "MSDOSFS file allocation table"); 73 74static int update_mp __P((struct mount *mp, struct msdosfs_args *argp)); 75static int mountmsdosfs __P((struct vnode *devvp, struct mount *mp, 76 struct proc *p, struct msdosfs_args *argp)); 77static int msdosfs_fhtovp __P((struct mount *, struct fid *, 78 struct vnode **)); 79static int msdosfs_checkexp __P((struct mount *, struct sockaddr *, 80 int *, struct ucred **)); 81static int msdosfs_mount __P((struct mount *, char *, caddr_t, 82 struct nameidata *, struct proc *)); 83static int msdosfs_root __P((struct mount *, struct vnode **)); 84static int msdosfs_statfs __P((struct mount *, struct statfs *, 85 struct proc *)); 86static int msdosfs_sync __P((struct mount *, int, struct ucred *, 87 struct proc *)); 88static int msdosfs_unmount __P((struct mount *, int, struct proc *)); 89static int msdosfs_vptofh __P((struct vnode *, struct fid *)); 90 91static int 92update_mp(mp, argp) 93 struct mount *mp; 94 struct msdosfs_args *argp; 95{ 96 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 97 int error; 98 99 pmp->pm_gid = argp->gid; 100 pmp->pm_uid = argp->uid; 101 pmp->pm_mask = argp->mask & ALLPERMS; 102 pmp->pm_flags |= argp->flags & MSDOSFSMNT_MNTOPT; 103 if (pmp->pm_flags & MSDOSFSMNT_U2WTABLE) { 104 bcopy(argp->u2w, pmp->pm_u2w, sizeof(pmp->pm_u2w)); 105 bcopy(argp->d2u, pmp->pm_d2u, sizeof(pmp->pm_d2u)); 106 bcopy(argp->u2d, pmp->pm_u2d, sizeof(pmp->pm_u2d)); 107 } 108 if (pmp->pm_flags & MSDOSFSMNT_ULTABLE) { 109 bcopy(argp->ul, pmp->pm_ul, sizeof(pmp->pm_ul)); 110 bcopy(argp->lu, pmp->pm_lu, sizeof(pmp->pm_lu)); 111 } 112 113#ifndef __FreeBSD__ 114 /* 115 * GEMDOS knows nothing (yet) about win95 116 */ 117 if (pmp->pm_flags & MSDOSFSMNT_GEMDOSFS) 118 pmp->pm_flags |= MSDOSFSMNT_NOWIN95; 119#endif 120 121 if (pmp->pm_flags & MSDOSFSMNT_NOWIN95) 122 pmp->pm_flags |= MSDOSFSMNT_SHORTNAME; 123 else if (!(pmp->pm_flags & 124 (MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) { 125 struct vnode *rootvp; 126 127 /* 128 * Try to divine whether to support Win'95 long filenames 129 */ 130 if (FAT32(pmp)) 131 pmp->pm_flags |= MSDOSFSMNT_LONGNAME; 132 else { 133 if ((error = msdosfs_root(mp, &rootvp)) != 0) 134 return error; 135 pmp->pm_flags |= findwin95(VTODE(rootvp)) 136 ? MSDOSFSMNT_LONGNAME 137 : MSDOSFSMNT_SHORTNAME; 138 vput(rootvp); 139 } 140 } 141 return 0; 142} 143 144#ifndef __FreeBSD__ 145int 146msdosfs_mountroot() 147{ 148 register struct mount *mp; 149 struct proc *p = curproc; /* XXX */ 150 size_t size; 151 int error; 152 struct msdosfs_args args; 153 154 if (root_device->dv_class != DV_DISK) 155 return (ENODEV); 156 157 /* 158 * Get vnodes for swapdev and rootdev. 159 */ 160 if (bdevvp(rootdev, &rootvp)) 161 panic("msdosfs_mountroot: can't setup rootvp"); 162 163 mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK); 164 bzero((char *)mp, (u_long)sizeof(struct mount)); 165 mp->mnt_op = &msdosfs_vfsops; 166 mp->mnt_flag = 0; 167 LIST_INIT(&mp->mnt_vnodelist); 168 169 args.flags = 0; 170 args.uid = 0; 171 args.gid = 0; 172 args.mask = 0777; 173 174 if ((error = mountmsdosfs(rootvp, mp, p, &args)) != 0) { 175 free(mp, M_MOUNT); 176 return (error); 177 } 178 179 if ((error = update_mp(mp, &args)) != 0) { 180 (void)msdosfs_unmount(mp, 0, p); 181 free(mp, M_MOUNT); 182 return (error); 183 } 184 185 if ((error = vfs_lock(mp)) != 0) { 186 (void)msdosfs_unmount(mp, 0, p); 187 free(mp, M_MOUNT); 188 return (error); 189 } 190 191 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list); 192 mp->mnt_vnodecovered = NULLVP; 193 (void) copystr("/", mp->mnt_stat.f_mntonname, MNAMELEN - 1, 194 &size); 195 bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size); 196 (void) copystr(ROOTNAME, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 197 &size); 198 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); 199 (void)msdosfs_statfs(mp, &mp->mnt_stat, p); 200 vfs_unlock(mp); 201 return (0); 202} 203#endif 204 205/* 206 * mp - path - addr in user space of mount point (ie /usr or whatever) 207 * data - addr in user space of mount params including the name of the block 208 * special file to treat as a filesystem. 209 */ 210static int 211msdosfs_mount(mp, path, data, ndp, p) 212 struct mount *mp; 213 char *path; 214 caddr_t data; 215 struct nameidata *ndp; 216 struct proc *p; 217{ 218 struct vnode *devvp; /* vnode for blk device to mount */ 219 struct msdosfs_args args; /* will hold data from mount request */ 220 /* msdosfs specific mount control block */ 221 struct msdosfsmount *pmp = NULL; 222 size_t size; 223 int error, flags; 224 mode_t accessmode; 225 226 error = copyin(data, (caddr_t)&args, sizeof(struct msdosfs_args)); 227 if (error) 228 return (error); 229 if (args.magic != MSDOSFS_ARGSMAGIC) 230 args.flags = 0; 231 /* 232 * If updating, check whether changing from read-only to 233 * read/write; if there is no device name, that's all we do. 234 */ 235 if (mp->mnt_flag & MNT_UPDATE) { 236 pmp = VFSTOMSDOSFS(mp); 237 error = 0; 238 if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_flag & MNT_RDONLY)) { 239 flags = WRITECLOSE; 240 if (mp->mnt_flag & MNT_FORCE) 241 flags |= FORCECLOSE; 242 error = vflush(mp, NULLVP, flags); 243 } 244 if (!error && (mp->mnt_flag & MNT_RELOAD)) 245 /* not yet implemented */ 246 error = EOPNOTSUPP; 247 if (error) 248 return (error); 249 if ((pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_kern_flag & MNTK_WANTRDWR)) { 250 /* 251 * If upgrade to read-write by non-root, then verify 252 * that user has necessary permissions on the device. 253 */ 254 if (p->p_ucred->cr_uid != 0) { 255 devvp = pmp->pm_devvp; 256 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p); 257 error = VOP_ACCESS(devvp, VREAD | VWRITE, 258 p->p_ucred, p); 259 if (error) { 260 VOP_UNLOCK(devvp, 0, p); 261 return (error); 262 } 263 VOP_UNLOCK(devvp, 0, p); 264 } 265 pmp->pm_flags &= ~MSDOSFSMNT_RONLY; 266 } 267 if (args.fspec == 0) { 268#ifdef __notyet__ /* doesn't work correctly with current mountd XXX */ 269 if (args.flags & MSDOSFSMNT_MNTOPT) { 270 pmp->pm_flags &= ~MSDOSFSMNT_MNTOPT; 271 pmp->pm_flags |= args.flags & MSDOSFSMNT_MNTOPT; 272 if (pmp->pm_flags & MSDOSFSMNT_NOWIN95) 273 pmp->pm_flags |= MSDOSFSMNT_SHORTNAME; 274 } 275#endif 276 /* 277 * Process export requests. 278 */ 279 return (vfs_export(mp, &pmp->pm_export, &args.export)); 280 } 281 } 282 /* 283 * Not an update, or updating the name: look up the name 284 * and verify that it refers to a sensible block device. 285 */ 286 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p); 287 error = namei(ndp); 288 if (error) 289 return (error); 290 devvp = ndp->ni_vp; 291 292 if (!vn_isdisk(devvp)) { 293 vrele(devvp); 294 return (ENOTBLK); 295 } 296 /* 297 * If mount by non-root, then verify that user has necessary 298 * permissions on the device. 299 */ 300 if (p->p_ucred->cr_uid != 0) { 301 accessmode = VREAD; 302 if ((mp->mnt_flag & MNT_RDONLY) == 0) 303 accessmode |= VWRITE; 304 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p); 305 error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p); 306 if (error) { 307 vput(devvp); 308 return (error); 309 } 310 VOP_UNLOCK(devvp, 0, p); 311 } 312 if ((mp->mnt_flag & MNT_UPDATE) == 0) { 313 error = mountmsdosfs(devvp, mp, p, &args); 314#ifdef MSDOSFS_DEBUG /* only needed for the printf below */ 315 pmp = VFSTOMSDOSFS(mp); 316#endif 317 } else { 318 if (devvp != pmp->pm_devvp) 319 error = EINVAL; /* XXX needs translation */ 320 else 321 vrele(devvp); 322 } 323 if (error) { 324 vrele(devvp); 325 return (error); 326 } 327 328 error = update_mp(mp, &args); 329 if (error) { 330 msdosfs_unmount(mp, MNT_FORCE, p); 331 return error; 332 } 333 334 (void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size); 335 bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size); 336 (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 337 &size); 338 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); 339 (void) msdosfs_statfs(mp, &mp->mnt_stat, p); 340#ifdef MSDOSFS_DEBUG 341 printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap); 342#endif 343 return (0); 344} 345 346static int 347mountmsdosfs(devvp, mp, p, argp) 348 struct vnode *devvp; 349 struct mount *mp; 350 struct proc *p; 351 struct msdosfs_args *argp; 352{ 353 struct msdosfsmount *pmp; 354 struct buf *bp; 355 dev_t dev = devvp->v_rdev; 356#ifndef __FreeBSD__ 357 struct partinfo dpart; 358 int bsize = 0, dtype = 0, tmp; 359#endif 360 union bootsector *bsp; 361 struct byte_bpb33 *b33; 362 struct byte_bpb50 *b50; 363 struct byte_bpb710 *b710; 364 u_int8_t SecPerClust; 365 int ronly, error; 366 367 /* 368 * Disallow multiple mounts of the same device. 369 * Disallow mounting of a device that is currently in use 370 * (except for root, which might share swap device for miniroot). 371 * Flush out any old buffers remaining from a previous use. 372 */ 373 error = vfs_mountedon(devvp); 374 if (error) 375 return (error); 376 if (vcount(devvp) > 1 && devvp != rootvp) 377 return (EBUSY); 378 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p); 379 error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0); 380 VOP_UNLOCK(devvp, 0, p); 381 if (error) 382 return (error); 383 384 ronly = (mp->mnt_flag & MNT_RDONLY) != 0; 385 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p); 386 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p); 387 VOP_UNLOCK(devvp, 0, p); 388 if (error) 389 return (error); 390 391 bp = NULL; /* both used in error_exit */ 392 pmp = NULL; 393 394#ifndef __FreeBSD__ 395 if (argp->flags & MSDOSFSMNT_GEMDOSFS) { 396 /* 397 * We need the disklabel to calculate the size of a FAT entry 398 * later on. Also make sure the partition contains a filesystem 399 * of type FS_MSDOS. This doesn't work for floppies, so we have 400 * to check for them too. 401 * 402 * At least some parts of the msdos fs driver seem to assume 403 * that the size of a disk block will always be 512 bytes. 404 * Let's check it... 405 */ 406 error = VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, 407 FREAD, NOCRED, p); 408 if (error) 409 goto error_exit; 410 tmp = dpart.part->p_fstype; 411 dtype = dpart.disklab->d_type; 412 bsize = dpart.disklab->d_secsize; 413 if (bsize != 512 || (dtype!=DTYPE_FLOPPY && tmp!=FS_MSDOS)) { 414 error = EINVAL; 415 goto error_exit; 416 } 417 } 418#endif 419 420 /* 421 * Read the boot sector of the filesystem, and then check the 422 * boot signature. If not a dos boot sector then error out. 423 */ 424#ifdef PC98 425 error = bread(devvp, 0, 1024, NOCRED, &bp); 426#else 427 error = bread(devvp, 0, 512, NOCRED, &bp); 428#endif 429 if (error) 430 goto error_exit; 431 bp->b_flags |= B_AGE; 432 bsp = (union bootsector *)bp->b_data; 433 b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB; 434 b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB; 435 b710 = (struct byte_bpb710 *)bsp->bs710.bsPBP; 436 437#ifndef __FreeBSD__ 438 if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) { 439#endif 440#ifdef PC98 441 if ((bsp->bs50.bsBootSectSig0 != BOOTSIG0 442 || bsp->bs50.bsBootSectSig1 != BOOTSIG1) 443 && (bsp->bs50.bsBootSectSig0 != 0 /* PC98 DOS 3.3x */ 444 || bsp->bs50.bsBootSectSig1 != 0) 445 && (bsp->bs50.bsBootSectSig0 != 0x90 /* PC98 DOS 5.0 */ 446 || bsp->bs50.bsBootSectSig1 != 0x3d) 447 && (bsp->bs50.bsBootSectSig0 != 0x46 /* PC98 DOS 3.3B */ 448 || bsp->bs50.bsBootSectSig1 != 0xfa)) { 449#else 450 if (bsp->bs50.bsBootSectSig0 != BOOTSIG0 451 || bsp->bs50.bsBootSectSig1 != BOOTSIG1) { 452#endif 453 error = EINVAL; 454 goto error_exit; 455 } 456#ifndef __FreeBSD__ 457 } 458#endif 459 460 pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK); 461 bzero((caddr_t)pmp, sizeof *pmp); 462 pmp->pm_mountp = mp; 463 464 /* 465 * Compute several useful quantities from the bpb in the 466 * bootsector. Copy in the dos 5 variant of the bpb then fix up 467 * the fields that are different between dos 5 and dos 3.3. 468 */ 469 SecPerClust = b50->bpbSecPerClust; 470 pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec); 471 pmp->pm_ResSectors = getushort(b50->bpbResSectors); 472 pmp->pm_FATs = b50->bpbFATs; 473 pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts); 474 pmp->pm_Sectors = getushort(b50->bpbSectors); 475 pmp->pm_FATsecs = getushort(b50->bpbFATsecs); 476 pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack); 477 pmp->pm_Heads = getushort(b50->bpbHeads); 478 pmp->pm_Media = b50->bpbMedia; 479 480#ifndef __FreeBSD__ 481 if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) { 482#endif 483 /* XXX - We should probably check more values here */ 484 if (!pmp->pm_BytesPerSec || !SecPerClust 485 || !pmp->pm_Heads || pmp->pm_Heads > 255 486#ifdef PC98 487 || !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 255) { 488#else 489 || !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 63) { 490#endif 491 error = EINVAL; 492 goto error_exit; 493 } 494#ifndef __FreeBSD__ 495 } 496#endif 497 498 if (pmp->pm_Sectors == 0) { 499 pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs); 500 pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors); 501 } else { 502 pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs); 503 pmp->pm_HugeSectors = pmp->pm_Sectors; 504 } 505 if (pmp->pm_HugeSectors > 0xffffffff / 506 (pmp->pm_BytesPerSec / sizeof(struct direntry)) + 1) { 507 /* 508 * We cannot deal currently with this size of disk 509 * due to fileid limitations (see msdosfs_getattr and 510 * msdosfs_readdir) 511 */ 512 error = EINVAL; 513 printf("mountmsdosfs(): disk too big, sorry\n"); 514 goto error_exit; 515 } 516 517 if (pmp->pm_RootDirEnts == 0) { 518 if (bsp->bs710.bsBootSectSig2 != BOOTSIG2 519 || bsp->bs710.bsBootSectSig3 != BOOTSIG3 520 || pmp->pm_Sectors 521 || pmp->pm_FATsecs 522 || getushort(b710->bpbFSVers)) { 523 error = EINVAL; 524 printf("mountmsdosfs(): bad FAT32 filesystem\n"); 525 goto error_exit; 526 } 527 pmp->pm_fatmask = FAT32_MASK; 528 pmp->pm_fatmult = 4; 529 pmp->pm_fatdiv = 1; 530 pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs); 531 if (getushort(b710->bpbExtFlags) & FATMIRROR) 532 pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM; 533 else 534 pmp->pm_flags |= MSDOSFS_FATMIRROR; 535 } else 536 pmp->pm_flags |= MSDOSFS_FATMIRROR; 537 538#ifndef __FreeBSD__ 539 if (argp->flags & MSDOSFSMNT_GEMDOSFS) { 540 if (FAT32(pmp)) { 541 /* 542 * GEMDOS doesn't know fat32. 543 */ 544 error = EINVAL; 545 goto error_exit; 546 } 547 548 /* 549 * Check a few values (could do some more): 550 * - logical sector size: power of 2, >= block size 551 * - sectors per cluster: power of 2, >= 1 552 * - number of sectors: >= 1, <= size of partition 553 */ 554 if ( (SecPerClust == 0) 555 || (SecPerClust & (SecPerClust - 1)) 556 || (pmp->pm_BytesPerSec < bsize) 557 || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1)) 558 || (pmp->pm_HugeSectors == 0) 559 || (pmp->pm_HugeSectors * (pmp->pm_BytesPerSec / bsize) 560 > dpart.part->p_size) 561 ) { 562 error = EINVAL; 563 goto error_exit; 564 } 565 /* 566 * XXX - Many parts of the msdos fs driver seem to assume that 567 * the number of bytes per logical sector (BytesPerSec) will 568 * always be the same as the number of bytes per disk block 569 * Let's pretend it is. 570 */ 571 tmp = pmp->pm_BytesPerSec / bsize; 572 pmp->pm_BytesPerSec = bsize; 573 pmp->pm_HugeSectors *= tmp; 574 pmp->pm_HiddenSects *= tmp; 575 pmp->pm_ResSectors *= tmp; 576 pmp->pm_Sectors *= tmp; 577 pmp->pm_FATsecs *= tmp; 578 SecPerClust *= tmp; 579 } 580#endif 581 pmp->pm_fatblk = pmp->pm_ResSectors; 582 if (FAT32(pmp)) { 583 pmp->pm_rootdirblk = getulong(b710->bpbRootClust); 584 pmp->pm_firstcluster = pmp->pm_fatblk 585 + (pmp->pm_FATs * pmp->pm_FATsecs); 586 pmp->pm_fsinfo = getushort(b710->bpbFSInfo); 587 } else { 588 pmp->pm_rootdirblk = pmp->pm_fatblk + 589 (pmp->pm_FATs * pmp->pm_FATsecs); 590 pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry) 591 + pmp->pm_BytesPerSec - 1) 592 / pmp->pm_BytesPerSec;/* in sectors */ 593 pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize; 594 } 595 596 pmp->pm_nmbrofclusters = (pmp->pm_HugeSectors - pmp->pm_firstcluster) / 597 SecPerClust; 598 pmp->pm_maxcluster = pmp->pm_nmbrofclusters + 1; 599 pmp->pm_fatsize = pmp->pm_FATsecs * pmp->pm_BytesPerSec; 600 601#ifndef __FreeBSD__ 602 if (argp->flags & MSDOSFSMNT_GEMDOSFS) { 603 if ((pmp->pm_nmbrofclusters <= (0xff0 - 2)) 604 && ((dtype == DTYPE_FLOPPY) || ((dtype == DTYPE_VNODE) 605 && ((pmp->pm_Heads == 1) || (pmp->pm_Heads == 2)))) 606 ) { 607 pmp->pm_fatmask = FAT12_MASK; 608 pmp->pm_fatmult = 3; 609 pmp->pm_fatdiv = 2; 610 } else { 611 pmp->pm_fatmask = FAT16_MASK; 612 pmp->pm_fatmult = 2; 613 pmp->pm_fatdiv = 1; 614 } 615 } else 616#endif 617 if (pmp->pm_fatmask == 0) { 618 if (pmp->pm_maxcluster 619 <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) { 620 /* 621 * This will usually be a floppy disk. This size makes 622 * sure that one fat entry will not be split across 623 * multiple blocks. 624 */ 625 pmp->pm_fatmask = FAT12_MASK; 626 pmp->pm_fatmult = 3; 627 pmp->pm_fatdiv = 2; 628 } else { 629 pmp->pm_fatmask = FAT16_MASK; 630 pmp->pm_fatmult = 2; 631 pmp->pm_fatdiv = 1; 632 } 633 } 634 if (FAT12(pmp)) 635 pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec; 636 else 637 pmp->pm_fatblocksize = DFLTBSIZE; 638 639 pmp->pm_fatblocksec = pmp->pm_fatblocksize / pmp->pm_BytesPerSec; 640 pmp->pm_bnshift = ffs(pmp->pm_BytesPerSec) - 1; 641 642 /* 643 * Compute mask and shift value for isolating cluster relative byte 644 * offsets and cluster numbers from a file offset. 645 */ 646 pmp->pm_bpcluster = SecPerClust * pmp->pm_BytesPerSec; 647 pmp->pm_crbomask = pmp->pm_bpcluster - 1; 648 pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1; 649 650 /* 651 * Check for valid cluster size 652 * must be a power of 2 653 */ 654 if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) { 655 error = EINVAL; 656 goto error_exit; 657 } 658 659 /* 660 * Release the bootsector buffer. 661 */ 662 brelse(bp); 663 bp = NULL; 664 665 /* 666 * Check FSInfo. 667 */ 668 if (pmp->pm_fsinfo) { 669 struct fsinfo *fp; 670 671 if ((error = bread(devvp, pmp->pm_fsinfo, 1024, NOCRED, &bp)) != 0) 672 goto error_exit; 673 fp = (struct fsinfo *)bp->b_data; 674 if (!bcmp(fp->fsisig1, "RRaA", 4) 675 && !bcmp(fp->fsisig2, "rrAa", 4) 676 && !bcmp(fp->fsisig3, "\0\0\125\252", 4) 677 && !bcmp(fp->fsisig4, "\0\0\125\252", 4)) 678 pmp->pm_nxtfree = getulong(fp->fsinxtfree); 679 else 680 pmp->pm_fsinfo = 0; 681 brelse(bp); 682 bp = NULL; 683 } 684 685 /* 686 * Check and validate (or perhaps invalidate?) the fsinfo structure? XXX 687 */ 688 689 /* 690 * Allocate memory for the bitmap of allocated clusters, and then 691 * fill it in. 692 */ 693 pmp->pm_inusemap = malloc(((pmp->pm_maxcluster + N_INUSEBITS - 1) 694 / N_INUSEBITS) 695 * sizeof(*pmp->pm_inusemap), 696 M_MSDOSFSFAT, M_WAITOK); 697 698 /* 699 * fillinusemap() needs pm_devvp. 700 */ 701 pmp->pm_dev = dev; 702 pmp->pm_devvp = devvp; 703 704 /* 705 * Have the inuse map filled in. 706 */ 707 if ((error = fillinusemap(pmp)) != 0) 708 goto error_exit; 709 710 /* 711 * If they want fat updates to be synchronous then let them suffer 712 * the performance degradation in exchange for the on disk copy of 713 * the fat being correct just about all the time. I suppose this 714 * would be a good thing to turn on if the kernel is still flakey. 715 */ 716 if (mp->mnt_flag & MNT_SYNCHRONOUS) 717 pmp->pm_flags |= MSDOSFSMNT_WAITONFAT; 718 719 /* 720 * Finish up. 721 */ 722 if (ronly) 723 pmp->pm_flags |= MSDOSFSMNT_RONLY; 724 else 725 pmp->pm_fmod = 1; 726 mp->mnt_data = (qaddr_t) pmp; 727 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev); 728 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum; 729 mp->mnt_flag |= MNT_LOCAL; 730 devvp->v_specmountpoint = mp; 731 732 return 0; 733 734error_exit: 735 if (bp) 736 brelse(bp); 737 (void) VOP_CLOSE(devvp, ronly ? FREAD : FREAD | FWRITE, NOCRED, p); 738 if (pmp) { 739 if (pmp->pm_inusemap) 740 free(pmp->pm_inusemap, M_MSDOSFSFAT); 741 free(pmp, M_MSDOSFSMNT); 742 mp->mnt_data = (qaddr_t)0; 743 } 744 return (error); 745} 746 747/* 748 * Unmount the filesystem described by mp. 749 */ 750static int 751msdosfs_unmount(mp, mntflags, p) 752 struct mount *mp; 753 int mntflags; 754 struct proc *p; 755{ 756 struct msdosfsmount *pmp; 757 int error, flags; 758 759 flags = 0; 760 if (mntflags & MNT_FORCE) 761 flags |= FORCECLOSE; 762 error = vflush(mp, NULLVP, flags); 763 if (error) 764 return error; 765 pmp = VFSTOMSDOSFS(mp); 766 pmp->pm_devvp->v_specmountpoint = NULL; 767#ifdef MSDOSFS_DEBUG 768 { 769 struct vnode *vp = pmp->pm_devvp; 770 771 printf("msdosfs_umount(): just before calling VOP_CLOSE()\n"); 772 printf("flag %08lx, usecount %d, writecount %d, holdcnt %ld\n", 773 vp->v_flag, vp->v_usecount, vp->v_writecount, vp->v_holdcnt); 774 printf("id %lu, mount %p, op %p\n", 775 vp->v_id, vp->v_mount, vp->v_op); 776 printf("freef %p, freeb %p, mount %p\n", 777 vp->v_freelist.tqe_next, vp->v_freelist.tqe_prev, 778 vp->v_mount); 779 printf("cleanblkhd %p, dirtyblkhd %p, numoutput %ld, type %d\n", 780 TAILQ_FIRST(&vp->v_cleanblkhd), 781 TAILQ_FIRST(&vp->v_dirtyblkhd), 782 vp->v_numoutput, vp->v_type); 783 printf("union %p, tag %d, data[0] %08x, data[1] %08x\n", 784 vp->v_socket, vp->v_tag, 785 ((u_int *)vp->v_data)[0], 786 ((u_int *)vp->v_data)[1]); 787 } 788#endif 789 error = VOP_CLOSE(pmp->pm_devvp, 790 (pmp->pm_flags&MSDOSFSMNT_RONLY) ? FREAD : FREAD | FWRITE, 791 NOCRED, p); 792 vrele(pmp->pm_devvp); 793 free(pmp->pm_inusemap, M_MSDOSFSFAT); 794 free(pmp, M_MSDOSFSMNT); 795 mp->mnt_data = (qaddr_t)0; 796 mp->mnt_flag &= ~MNT_LOCAL; 797 return (error); 798} 799 800static int 801msdosfs_root(mp, vpp) 802 struct mount *mp; 803 struct vnode **vpp; 804{ 805 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 806 struct denode *ndep; 807 int error; 808 809#ifdef MSDOSFS_DEBUG 810 printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp); 811#endif 812 error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep); 813 if (error) 814 return (error); 815 *vpp = DETOV(ndep); 816 return (0); 817} 818 819static int 820msdosfs_statfs(mp, sbp, p) 821 struct mount *mp; 822 struct statfs *sbp; 823 struct proc *p; 824{ 825 struct msdosfsmount *pmp; 826 827 pmp = VFSTOMSDOSFS(mp); 828 sbp->f_bsize = pmp->pm_bpcluster; 829 sbp->f_iosize = pmp->pm_bpcluster; 830 sbp->f_blocks = pmp->pm_nmbrofclusters; 831 sbp->f_bfree = pmp->pm_freeclustercount; 832 sbp->f_bavail = pmp->pm_freeclustercount; 833 sbp->f_files = pmp->pm_RootDirEnts; /* XXX */ 834 sbp->f_ffree = 0; /* what to put in here? */ 835 if (sbp != &mp->mnt_stat) { 836 sbp->f_type = mp->mnt_vfc->vfc_typenum; 837 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN); 838 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN); 839 } 840 strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN); 841 return (0); 842} 843 844static int 845msdosfs_sync(mp, waitfor, cred, p) 846 struct mount *mp; 847 int waitfor; 848 struct ucred *cred; 849 struct proc *p; 850{ 851 struct vnode *vp, *nvp; 852 struct denode *dep; 853 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 854 int error, allerror = 0; 855 856 /* 857 * If we ever switch to not updating all of the fats all the time, 858 * this would be the place to update them from the first one. 859 */ 860 if (pmp->pm_fmod != 0) { 861 if (pmp->pm_flags & MSDOSFSMNT_RONLY) 862 panic("msdosfs_sync: rofs mod"); 863 else { 864 /* update fats here */ 865 } 866 } 867 /* 868 * Write back each (modified) denode. 869 */ 870 simple_lock(&mntvnode_slock); 871loop: 872 for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) { 873 /* 874 * If the vnode that we are about to sync is no longer 875 * associated with this mount point, start over. 876 */ 877 if (vp->v_mount != mp) 878 goto loop; 879 880 simple_lock(&vp->v_interlock); 881 nvp = vp->v_mntvnodes.le_next; 882 dep = VTODE(vp); 883 if (vp->v_type == VNON || 884 ((dep->de_flag & 885 (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 && 886 (TAILQ_EMPTY(&vp->v_dirtyblkhd) || waitfor == MNT_LAZY))) { 887 simple_unlock(&vp->v_interlock); 888 continue; 889 } 890 simple_unlock(&mntvnode_slock); 891 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, p); 892 if (error) { 893 simple_lock(&mntvnode_slock); 894 if (error == ENOENT) 895 goto loop; 896 continue; 897 } 898 error = VOP_FSYNC(vp, cred, waitfor, p); 899 if (error) 900 allerror = error; 901 VOP_UNLOCK(vp, 0, p); 902 vrele(vp); 903 simple_lock(&mntvnode_slock); 904 } 905 simple_unlock(&mntvnode_slock); 906 907 /* 908 * Flush filesystem control info. 909 */ 910 if (waitfor != MNT_LAZY) { 911 vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY, p); 912 error = VOP_FSYNC(pmp->pm_devvp, cred, waitfor, p); 913 if (error) 914 allerror = error; 915 VOP_UNLOCK(pmp->pm_devvp, 0, p); 916 } 917 return (allerror); 918} 919 920static int 921msdosfs_fhtovp(mp, fhp, vpp) 922 struct mount *mp; 923 struct fid *fhp; 924 struct vnode **vpp; 925{ 926 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 927 struct defid *defhp = (struct defid *) fhp; 928 struct denode *dep; 929 int error; 930 931 error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep); 932 if (error) { 933 *vpp = NULLVP; 934 return (error); 935 } 936 *vpp = DETOV(dep); 937 return (0); 938} 939 940static int 941msdosfs_checkexp(mp, nam, exflagsp, credanonp) 942 struct mount *mp; 943 struct sockaddr *nam; 944 int *exflagsp; 945 struct ucred **credanonp; 946{ 947 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 948 struct netcred *np; 949 950 np = vfs_export_lookup(mp, &pmp->pm_export, nam); 951 if (np == NULL) 952 return (EACCES); 953 *exflagsp = np->netc_exflags; 954 *credanonp = &np->netc_anon; 955 return (0); 956} 957 958static int 959msdosfs_vptofh(vp, fhp) 960 struct vnode *vp; 961 struct fid *fhp; 962{ 963 struct denode *dep; 964 struct defid *defhp; 965 966 dep = VTODE(vp); 967 defhp = (struct defid *)fhp; 968 defhp->defid_len = sizeof(struct defid); 969 defhp->defid_dirclust = dep->de_dirclust; 970 defhp->defid_dirofs = dep->de_diroffset; 971 /* defhp->defid_gen = dep->de_gen; */ 972 return (0); 973} 974 975static struct vfsops msdosfs_vfsops = { 976 msdosfs_mount, 977 vfs_stdstart, 978 msdosfs_unmount, 979 msdosfs_root, 980 vfs_stdquotactl, 981 msdosfs_statfs, 982 msdosfs_sync, 983 vfs_stdvget, 984 msdosfs_fhtovp, 985 msdosfs_checkexp, 986 msdosfs_vptofh, 987 msdosfs_init 988}; 989 990VFS_SET(msdosfs_vfsops, msdos, 0); 991