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/bio.h> 60 #include <sys/buf.h> 61 #include <sys/fcntl.h> 62 #include <sys/malloc.h> 63 #include <sys/stat.h> /* defines ALLPERMS */ 64 #include <sys/mutex.h> 65 66 #include <fs/msdosfs/bpb.h> 67 #include <fs/msdosfs/bootsect.h> 68 #include <fs/msdosfs/direntry.h> 69 #include <fs/msdosfs/denode.h> 70 #include <fs/msdosfs/msdosfsmount.h> 71 #include <fs/msdosfs/fat.h> 72 73 #define MSDOSFS_DFLTBSIZE 4096 74 75 #if 1 /*def PC98*/ 76 /* 77 * XXX - The boot signature formatted by NEC PC-98 DOS looks like a 78 * garbage or a random value :-{ 79 * If you want to use that broken-signatured media, define the 80 * following symbol even though PC/AT. 81 * (ex. mount PC-98 DOS formatted FD on PC/AT) 82 */ 83 #define MSDOSFS_NOCHECKSIG 84 #endif 85 86 MALLOC_DEFINE(M_MSDOSFSMNT, "MSDOSFS mount", "MSDOSFS mount structure"); 87 static MALLOC_DEFINE(M_MSDOSFSFAT, "MSDOSFS FAT", "MSDOSFS file allocation table"); 88 89 static int update_mp __P((struct mount *mp, struct msdosfs_args *argp)); 90 static int mountmsdosfs __P((struct vnode *devvp, struct mount *mp, 91 struct proc *p, struct msdosfs_args *argp)); 92 static int msdosfs_fhtovp __P((struct mount *, struct fid *, 93 struct vnode **)); 94 static int msdosfs_mount __P((struct mount *, char *, caddr_t, 95 struct nameidata *, struct proc *)); 96 static int msdosfs_root __P((struct mount *, struct vnode **)); 97 static int msdosfs_statfs __P((struct mount *, struct statfs *, 98 struct proc *)); 99 static int msdosfs_sync __P((struct mount *, int, struct ucred *, 100 struct proc *)); 101 static int msdosfs_unmount __P((struct mount *, int, struct proc *)); 102 static int msdosfs_vptofh __P((struct vnode *, struct fid *)); 103 104 static int 105 update_mp(mp, argp) 106 struct mount *mp; 107 struct msdosfs_args *argp; 108 { 109 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 110 int error; 111 112 pmp->pm_gid = argp->gid; 113 pmp->pm_uid = argp->uid; 114 pmp->pm_mask = argp->mask & ALLPERMS; 115 pmp->pm_flags |= argp->flags & MSDOSFSMNT_MNTOPT; 116 if (pmp->pm_flags & MSDOSFSMNT_U2WTABLE) { 117 bcopy(argp->u2w, pmp->pm_u2w, sizeof(pmp->pm_u2w)); 118 bcopy(argp->d2u, pmp->pm_d2u, sizeof(pmp->pm_d2u)); 119 bcopy(argp->u2d, pmp->pm_u2d, sizeof(pmp->pm_u2d)); 120 } 121 if (pmp->pm_flags & MSDOSFSMNT_ULTABLE) { 122 bcopy(argp->ul, pmp->pm_ul, sizeof(pmp->pm_ul)); 123 bcopy(argp->lu, pmp->pm_lu, sizeof(pmp->pm_lu)); 124 } 125 126 #ifndef __FreeBSD__ 127 /* 128 * GEMDOS knows nothing (yet) about win95 129 */ 130 if (pmp->pm_flags & MSDOSFSMNT_GEMDOSFS) 131 pmp->pm_flags |= MSDOSFSMNT_NOWIN95; 132 #endif 133 134 if (pmp->pm_flags & MSDOSFSMNT_NOWIN95) 135 pmp->pm_flags |= MSDOSFSMNT_SHORTNAME; 136 else if (!(pmp->pm_flags & 137 (MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) { 138 struct vnode *rootvp; 139 140 /* 141 * Try to divine whether to support Win'95 long filenames 142 */ 143 if (FAT32(pmp)) 144 pmp->pm_flags |= MSDOSFSMNT_LONGNAME; 145 else { 146 if ((error = msdosfs_root(mp, &rootvp)) != 0) 147 return error; 148 pmp->pm_flags |= findwin95(VTODE(rootvp)) 149 ? MSDOSFSMNT_LONGNAME 150 : MSDOSFSMNT_SHORTNAME; 151 vput(rootvp); 152 } 153 } 154 return 0; 155 } 156 157 #ifndef __FreeBSD__ 158 int 159 msdosfs_mountroot() 160 { 161 register struct mount *mp; 162 struct proc *p = curproc; /* XXX */ 163 size_t size; 164 int error; 165 struct msdosfs_args args; 166 167 if (root_device->dv_class != DV_DISK) 168 return (ENODEV); 169 170 /* 171 * Get vnodes for swapdev and rootdev. 172 */ 173 if (bdevvp(rootdev, &rootvp)) 174 panic("msdosfs_mountroot: can't setup rootvp"); 175 176 mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK | M_ZERO); 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, 0, 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, &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 (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 347 &size); 348 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); 349 (void) msdosfs_statfs(mp, &mp->mnt_stat, p); 350 #ifdef MSDOSFS_DEBUG 351 printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap); 352 #endif 353 return (0); 354 } 355 356 static int 357 mountmsdosfs(devvp, mp, p, argp) 358 struct vnode *devvp; 359 struct mount *mp; 360 struct proc *p; 361 struct msdosfs_args *argp; 362 { 363 struct msdosfsmount *pmp; 364 struct buf *bp; 365 dev_t dev = devvp->v_rdev; 366 #ifndef __FreeBSD__ 367 struct partinfo dpart; 368 int bsize = 0, dtype = 0, tmp; 369 #endif 370 union bootsector *bsp; 371 struct byte_bpb33 *b33; 372 struct byte_bpb50 *b50; 373 struct byte_bpb710 *b710; 374 u_int8_t SecPerClust; 375 u_long clusters; 376 int ronly, error; 377 378 /* 379 * Disallow multiple mounts of the same device. 380 * Disallow mounting of a device that is currently in use 381 * (except for root, which might share swap device for miniroot). 382 * Flush out any old buffers remaining from a previous use. 383 */ 384 error = vfs_mountedon(devvp); 385 if (error) 386 return (error); 387 if (vcount(devvp) > 1 && devvp != rootvp) 388 return (EBUSY); 389 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p); 390 error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0); 391 VOP_UNLOCK(devvp, 0, p); 392 if (error) 393 return (error); 394 395 ronly = (mp->mnt_flag & MNT_RDONLY) != 0; 396 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p); 397 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p); 398 VOP_UNLOCK(devvp, 0, p); 399 if (error) 400 return (error); 401 402 bp = NULL; /* both used in error_exit */ 403 pmp = NULL; 404 405 #ifndef __FreeBSD__ 406 if (argp->flags & MSDOSFSMNT_GEMDOSFS) { 407 /* 408 * We need the disklabel to calculate the size of a FAT entry 409 * later on. Also make sure the partition contains a filesystem 410 * of type FS_MSDOS. This doesn't work for floppies, so we have 411 * to check for them too. 412 * 413 * At least some parts of the msdos fs driver seem to assume 414 * that the size of a disk block will always be 512 bytes. 415 * Let's check it... 416 */ 417 error = VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, 418 FREAD, NOCRED, p); 419 if (error) 420 goto error_exit; 421 tmp = dpart.part->p_fstype; 422 dtype = dpart.disklab->d_type; 423 bsize = dpart.disklab->d_secsize; 424 if (bsize != 512 || (dtype!=DTYPE_FLOPPY && tmp!=FS_MSDOS)) { 425 error = EINVAL; 426 goto error_exit; 427 } 428 } 429 #endif 430 431 /* 432 * Read the boot sector of the filesystem, and then check the 433 * boot signature. If not a dos boot sector then error out. 434 * 435 * NOTE: 2048 is a maximum sector size in current... 436 */ 437 error = bread(devvp, 0, 2048, NOCRED, &bp); 438 if (error) 439 goto error_exit; 440 bp->b_flags |= B_AGE; 441 bsp = (union bootsector *)bp->b_data; 442 b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB; 443 b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB; 444 b710 = (struct byte_bpb710 *)bsp->bs710.bsPBP; 445 446 #ifndef __FreeBSD__ 447 if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) { 448 #endif 449 #ifndef MSDOSFS_NOCHECKSIG 450 if (bsp->bs50.bsBootSectSig0 != BOOTSIG0 451 || bsp->bs50.bsBootSectSig1 != BOOTSIG1) { 452 error = EINVAL; 453 goto error_exit; 454 } 455 #endif 456 #ifndef __FreeBSD__ 457 } 458 #endif 459 460 pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK | M_ZERO); 461 pmp->pm_mountp = mp; 462 463 /* 464 * Compute several useful quantities from the bpb in the 465 * bootsector. Copy in the dos 5 variant of the bpb then fix up 466 * the fields that are different between dos 5 and dos 3.3. 467 */ 468 SecPerClust = b50->bpbSecPerClust; 469 pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec); 470 pmp->pm_ResSectors = getushort(b50->bpbResSectors); 471 pmp->pm_FATs = b50->bpbFATs; 472 pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts); 473 pmp->pm_Sectors = getushort(b50->bpbSectors); 474 pmp->pm_FATsecs = getushort(b50->bpbFATsecs); 475 pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack); 476 pmp->pm_Heads = getushort(b50->bpbHeads); 477 pmp->pm_Media = b50->bpbMedia; 478 479 /* calculate the ratio of sector size to DEV_BSIZE */ 480 pmp->pm_BlkPerSec = pmp->pm_BytesPerSec / DEV_BSIZE; 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 /* 541 * Check a few values (could do some more): 542 * - logical sector size: power of 2, >= block size 543 * - sectors per cluster: power of 2, >= 1 544 * - number of sectors: >= 1, <= size of partition 545 */ 546 if ( (SecPerClust == 0) 547 || (SecPerClust & (SecPerClust - 1)) 548 || (pmp->pm_BytesPerSec < DEV_BSIZE) 549 || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1)) 550 || (pmp->pm_HugeSectors == 0) 551 ) { 552 error = EINVAL; 553 goto error_exit; 554 } 555 556 pmp->pm_HugeSectors *= pmp->pm_BlkPerSec; 557 pmp->pm_HiddenSects *= pmp->pm_BlkPerSec; /* XXX not used? */ 558 pmp->pm_FATsecs *= pmp->pm_BlkPerSec; 559 SecPerClust *= pmp->pm_BlkPerSec; 560 561 pmp->pm_fatblk = pmp->pm_ResSectors * pmp->pm_BlkPerSec; 562 563 if (FAT32(pmp)) { 564 pmp->pm_rootdirblk = getulong(b710->bpbRootClust); 565 pmp->pm_firstcluster = pmp->pm_fatblk 566 + (pmp->pm_FATs * pmp->pm_FATsecs); 567 pmp->pm_fsinfo = getushort(b710->bpbFSInfo) * pmp->pm_BlkPerSec; 568 } else { 569 pmp->pm_rootdirblk = pmp->pm_fatblk + 570 (pmp->pm_FATs * pmp->pm_FATsecs); 571 pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry) 572 + DEV_BSIZE - 1) 573 / DEV_BSIZE; /* in blocks */ 574 pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize; 575 } 576 577 pmp->pm_maxcluster = (pmp->pm_HugeSectors - pmp->pm_firstcluster) / 578 SecPerClust + 1; 579 pmp->pm_fatsize = pmp->pm_FATsecs * DEV_BSIZE; /* XXX not used? */ 580 581 #ifndef __FreeBSD__ 582 if (argp->flags & MSDOSFSMNT_GEMDOSFS) { 583 if ((pmp->pm_maxcluster <= (0xff0 - 2)) 584 && ((dtype == DTYPE_FLOPPY) || ((dtype == DTYPE_VNODE) 585 && ((pmp->pm_Heads == 1) || (pmp->pm_Heads == 2)))) 586 ) { 587 pmp->pm_fatmask = FAT12_MASK; 588 pmp->pm_fatmult = 3; 589 pmp->pm_fatdiv = 2; 590 } else { 591 pmp->pm_fatmask = FAT16_MASK; 592 pmp->pm_fatmult = 2; 593 pmp->pm_fatdiv = 1; 594 } 595 } else 596 #endif 597 if (pmp->pm_fatmask == 0) { 598 if (pmp->pm_maxcluster 599 <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) { 600 /* 601 * This will usually be a floppy disk. This size makes 602 * sure that one fat entry will not be split across 603 * multiple blocks. 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 } 614 615 clusters = (pmp->pm_fatsize / pmp->pm_fatmult) * pmp->pm_fatdiv; 616 if (pmp->pm_maxcluster >= clusters) { 617 printf("Warning: number of clusters (%ld) exceeds FAT " 618 "capacity (%ld)\n", pmp->pm_maxcluster + 1, clusters); 619 pmp->pm_maxcluster = clusters - 1; 620 } 621 622 623 if (FAT12(pmp)) 624 pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec; 625 else 626 pmp->pm_fatblocksize = MSDOSFS_DFLTBSIZE; 627 628 pmp->pm_fatblocksec = pmp->pm_fatblocksize / DEV_BSIZE; 629 pmp->pm_bnshift = ffs(DEV_BSIZE) - 1; 630 631 /* 632 * Compute mask and shift value for isolating cluster relative byte 633 * offsets and cluster numbers from a file offset. 634 */ 635 pmp->pm_bpcluster = SecPerClust * DEV_BSIZE; 636 pmp->pm_crbomask = pmp->pm_bpcluster - 1; 637 pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1; 638 639 /* 640 * Check for valid cluster size 641 * must be a power of 2 642 */ 643 if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) { 644 error = EINVAL; 645 goto error_exit; 646 } 647 648 /* 649 * Release the bootsector buffer. 650 */ 651 brelse(bp); 652 bp = NULL; 653 654 /* 655 * Check FSInfo. 656 */ 657 if (pmp->pm_fsinfo) { 658 struct fsinfo *fp; 659 660 if ((error = bread(devvp, pmp->pm_fsinfo, fsi_size(pmp), 661 NOCRED, &bp)) != 0) 662 goto error_exit; 663 fp = (struct fsinfo *)bp->b_data; 664 if (!bcmp(fp->fsisig1, "RRaA", 4) 665 && !bcmp(fp->fsisig2, "rrAa", 4) 666 && !bcmp(fp->fsisig3, "\0\0\125\252", 4) 667 && !bcmp(fp->fsisig4, "\0\0\125\252", 4)) 668 pmp->pm_nxtfree = getulong(fp->fsinxtfree); 669 else 670 pmp->pm_fsinfo = 0; 671 brelse(bp); 672 bp = NULL; 673 } 674 675 /* 676 * Check and validate (or perhaps invalidate?) the fsinfo structure? XXX 677 */ 678 679 /* 680 * Allocate memory for the bitmap of allocated clusters, and then 681 * fill it in. 682 */ 683 pmp->pm_inusemap = malloc(((pmp->pm_maxcluster + N_INUSEBITS - 1) 684 / N_INUSEBITS) 685 * sizeof(*pmp->pm_inusemap), 686 M_MSDOSFSFAT, M_WAITOK); 687 688 /* 689 * fillinusemap() needs pm_devvp. 690 */ 691 pmp->pm_dev = dev; 692 pmp->pm_devvp = devvp; 693 694 /* 695 * Have the inuse map filled in. 696 */ 697 if ((error = fillinusemap(pmp)) != 0) 698 goto error_exit; 699 700 /* 701 * If they want fat updates to be synchronous then let them suffer 702 * the performance degradation in exchange for the on disk copy of 703 * the fat being correct just about all the time. I suppose this 704 * would be a good thing to turn on if the kernel is still flakey. 705 */ 706 if (mp->mnt_flag & MNT_SYNCHRONOUS) 707 pmp->pm_flags |= MSDOSFSMNT_WAITONFAT; 708 709 /* 710 * Finish up. 711 */ 712 if (ronly) 713 pmp->pm_flags |= MSDOSFSMNT_RONLY; 714 else 715 pmp->pm_fmod = 1; 716 mp->mnt_data = (qaddr_t) pmp; 717 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev); 718 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum; 719 mp->mnt_flag |= MNT_LOCAL; 720 devvp->v_rdev->si_mountpoint = mp; 721 722 return 0; 723 724 error_exit: 725 if (bp) 726 brelse(bp); 727 (void) VOP_CLOSE(devvp, ronly ? FREAD : FREAD | FWRITE, NOCRED, p); 728 if (pmp) { 729 if (pmp->pm_inusemap) 730 free(pmp->pm_inusemap, M_MSDOSFSFAT); 731 free(pmp, M_MSDOSFSMNT); 732 mp->mnt_data = (qaddr_t)0; 733 } 734 return (error); 735 } 736 737 /* 738 * Unmount the filesystem described by mp. 739 */ 740 static int 741 msdosfs_unmount(mp, mntflags, p) 742 struct mount *mp; 743 int mntflags; 744 struct proc *p; 745 { 746 struct msdosfsmount *pmp; 747 int error, flags; 748 749 flags = 0; 750 if (mntflags & MNT_FORCE) 751 flags |= FORCECLOSE; 752 error = vflush(mp, 0, flags); 753 if (error) 754 return error; 755 pmp = VFSTOMSDOSFS(mp); 756 pmp->pm_devvp->v_rdev->si_mountpoint = NULL; 757 #ifdef MSDOSFS_DEBUG 758 { 759 struct vnode *vp = pmp->pm_devvp; 760 761 printf("msdosfs_umount(): just before calling VOP_CLOSE()\n"); 762 printf("flag %08lx, usecount %d, writecount %d, holdcnt %ld\n", 763 vp->v_flag, vp->v_usecount, vp->v_writecount, vp->v_holdcnt); 764 printf("id %lu, mount %p, op %p\n", 765 vp->v_id, vp->v_mount, vp->v_op); 766 printf("freef %p, freeb %p, mount %p\n", 767 TAILQ_NEXT(vp, v_freelist), vp->v_freelist.tqe_prev, 768 vp->v_mount); 769 printf("cleanblkhd %p, dirtyblkhd %p, numoutput %ld, type %d\n", 770 TAILQ_FIRST(&vp->v_cleanblkhd), 771 TAILQ_FIRST(&vp->v_dirtyblkhd), 772 vp->v_numoutput, vp->v_type); 773 printf("union %p, tag %d, data[0] %08x, data[1] %08x\n", 774 vp->v_socket, vp->v_tag, 775 ((u_int *)vp->v_data)[0], 776 ((u_int *)vp->v_data)[1]); 777 } 778 #endif 779 error = VOP_CLOSE(pmp->pm_devvp, 780 (pmp->pm_flags&MSDOSFSMNT_RONLY) ? FREAD : FREAD | FWRITE, 781 NOCRED, p); 782 vrele(pmp->pm_devvp); 783 free(pmp->pm_inusemap, M_MSDOSFSFAT); 784 free(pmp, M_MSDOSFSMNT); 785 mp->mnt_data = (qaddr_t)0; 786 mp->mnt_flag &= ~MNT_LOCAL; 787 return (error); 788 } 789 790 static int 791 msdosfs_root(mp, vpp) 792 struct mount *mp; 793 struct vnode **vpp; 794 { 795 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 796 struct denode *ndep; 797 int error; 798 799 #ifdef MSDOSFS_DEBUG 800 printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp); 801 #endif 802 error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep); 803 if (error) 804 return (error); 805 *vpp = DETOV(ndep); 806 return (0); 807 } 808 809 static int 810 msdosfs_statfs(mp, sbp, p) 811 struct mount *mp; 812 struct statfs *sbp; 813 struct proc *p; 814 { 815 struct msdosfsmount *pmp; 816 817 pmp = VFSTOMSDOSFS(mp); 818 sbp->f_bsize = pmp->pm_bpcluster; 819 sbp->f_iosize = pmp->pm_bpcluster; 820 sbp->f_blocks = pmp->pm_maxcluster + 1; 821 sbp->f_bfree = pmp->pm_freeclustercount; 822 sbp->f_bavail = pmp->pm_freeclustercount; 823 sbp->f_files = pmp->pm_RootDirEnts; /* XXX */ 824 sbp->f_ffree = 0; /* what to put in here? */ 825 if (sbp != &mp->mnt_stat) { 826 sbp->f_type = mp->mnt_vfc->vfc_typenum; 827 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN); 828 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN); 829 } 830 strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN); 831 return (0); 832 } 833 834 static int 835 msdosfs_sync(mp, waitfor, cred, p) 836 struct mount *mp; 837 int waitfor; 838 struct ucred *cred; 839 struct proc *p; 840 { 841 struct vnode *vp, *nvp; 842 struct denode *dep; 843 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 844 int error, allerror = 0; 845 846 /* 847 * If we ever switch to not updating all of the fats all the time, 848 * this would be the place to update them from the first one. 849 */ 850 if (pmp->pm_fmod != 0) { 851 if (pmp->pm_flags & MSDOSFSMNT_RONLY) 852 panic("msdosfs_sync: rofs mod"); 853 else { 854 /* update fats here */ 855 } 856 } 857 /* 858 * Write back each (modified) denode. 859 */ 860 mtx_lock(&mntvnode_mtx); 861 loop: 862 for (vp = LIST_FIRST(&mp->mnt_vnodelist); vp != NULL; vp = nvp) { 863 /* 864 * If the vnode that we are about to sync is no longer 865 * associated with this mount point, start over. 866 */ 867 if (vp->v_mount != mp) 868 goto loop; 869 nvp = LIST_NEXT(vp, v_mntvnodes); 870 871 mtx_unlock(&mntvnode_mtx); 872 mtx_lock(&vp->v_interlock); 873 dep = VTODE(vp); 874 if (vp->v_type == VNON || 875 ((dep->de_flag & 876 (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 && 877 (TAILQ_EMPTY(&vp->v_dirtyblkhd) || waitfor == MNT_LAZY))) { 878 mtx_unlock(&vp->v_interlock); 879 mtx_lock(&mntvnode_mtx); 880 continue; 881 } 882 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, p); 883 if (error) { 884 mtx_lock(&mntvnode_mtx); 885 if (error == ENOENT) 886 goto loop; 887 continue; 888 } 889 error = VOP_FSYNC(vp, cred, waitfor, p); 890 if (error) 891 allerror = error; 892 VOP_UNLOCK(vp, 0, p); 893 vrele(vp); 894 mtx_lock(&mntvnode_mtx); 895 } 896 mtx_unlock(&mntvnode_mtx); 897 898 /* 899 * Flush filesystem control info. 900 */ 901 if (waitfor != MNT_LAZY) { 902 vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY, p); 903 error = VOP_FSYNC(pmp->pm_devvp, cred, waitfor, p); 904 if (error) 905 allerror = error; 906 VOP_UNLOCK(pmp->pm_devvp, 0, p); 907 } 908 return (allerror); 909 } 910 911 static int 912 msdosfs_fhtovp(mp, fhp, vpp) 913 struct mount *mp; 914 struct fid *fhp; 915 struct vnode **vpp; 916 { 917 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 918 struct defid *defhp = (struct defid *) fhp; 919 struct denode *dep; 920 int error; 921 922 error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep); 923 if (error) { 924 *vpp = NULLVP; 925 return (error); 926 } 927 *vpp = DETOV(dep); 928 return (0); 929 } 930 931 static int 932 msdosfs_vptofh(vp, fhp) 933 struct vnode *vp; 934 struct fid *fhp; 935 { 936 struct denode *dep; 937 struct defid *defhp; 938 939 dep = VTODE(vp); 940 defhp = (struct defid *)fhp; 941 defhp->defid_len = sizeof(struct defid); 942 defhp->defid_dirclust = dep->de_dirclust; 943 defhp->defid_dirofs = dep->de_diroffset; 944 /* defhp->defid_gen = dep->de_gen; */ 945 return (0); 946 } 947 948 static struct vfsops msdosfs_vfsops = { 949 msdosfs_mount, 950 vfs_stdstart, 951 msdosfs_unmount, 952 msdosfs_root, 953 vfs_stdquotactl, 954 msdosfs_statfs, 955 msdosfs_sync, 956 vfs_stdvget, 957 msdosfs_fhtovp, 958 vfs_stdcheckexp, 959 msdosfs_vptofh, 960 msdosfs_init, 961 msdosfs_uninit, 962 vfs_stdextattrctl, 963 }; 964 965 VFS_SET(msdosfs_vfsops, msdosfs, 0); 966