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