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