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