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