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