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