1 /* $FreeBSD: /usr/local/www/cvsroot/FreeBSD/src/sys/msdosfs/Attic/msdosfs_vfsops.c,v 1.60.2.8 2004/03/02 09:43:04 tjr Exp $ */ 2 /* $DragonFly: src/sys/vfs/msdosfs/msdosfs_vfsops.c,v 1.22 2004/12/17 00:18:26 dillon Exp $ */ 3 /* $NetBSD: msdosfs_vfsops.c,v 1.51 1997/11/17 15:36:58 ws Exp $ */ 4 5 /*- 6 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank. 7 * Copyright (C) 1994, 1995, 1997 TooLs GmbH. 8 * All rights reserved. 9 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below). 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by TooLs GmbH. 22 * 4. The name of TooLs GmbH may not be used to endorse or promote products 23 * derived from this software without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 28 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 31 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 32 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 33 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 34 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 */ 36 /* 37 * Written by Paul Popelka (paulp@uts.amdahl.com) 38 * 39 * You can do anything you want with this software, just don't say you wrote 40 * it, and don't remove this notice. 41 * 42 * This software is provided "as is". 43 * 44 * The author supplies this software to be publicly redistributed on the 45 * understanding that the author is not responsible for the correct 46 * functioning of this software in any circumstances and is not liable for 47 * any damages caused by this software. 48 * 49 * October 1992 50 */ 51 52 #include <sys/param.h> 53 #include <sys/systm.h> 54 #include <sys/conf.h> 55 #include <sys/proc.h> 56 #include <sys/nlookup.h> 57 #include <sys/kernel.h> 58 #include <sys/vnode.h> 59 #include <sys/mount.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 <vm/vm_zone.h> 65 66 #include "bpb.h" 67 #include "bootsect.h" 68 #include "direntry.h" 69 #include "denode.h" 70 #include "msdosfsmount.h" 71 #include "fat.h" 72 73 extern struct vnodeopv_entry_desc msdosfs_vnodeop_entries[]; 74 75 #define MSDOSFS_DFLTBSIZE 4096 76 77 #if 1 /*def PC98*/ 78 /* 79 * XXX - The boot signature formatted by NEC PC-98 DOS looks like a 80 * garbage or a random value :-{ 81 * If you want to use that broken-signatured media, define the 82 * following symbol even though PC/AT. 83 * (ex. mount PC-98 DOS formatted FD on PC/AT) 84 */ 85 #define MSDOSFS_NOCHECKSIG 86 #endif 87 88 MALLOC_DEFINE(M_MSDOSFSMNT, "MSDOSFS mount", "MSDOSFS mount structure"); 89 static MALLOC_DEFINE(M_MSDOSFSFAT, "MSDOSFS FAT", "MSDOSFS file allocation table"); 90 91 static int update_mp (struct mount *mp, struct msdosfs_args *argp); 92 static int mountmsdosfs (struct vnode *devvp, struct mount *mp, 93 struct thread *td, struct msdosfs_args *argp); 94 static int msdosfs_fhtovp (struct mount *, struct fid *, 95 struct vnode **); 96 static int msdosfs_checkexp (struct mount *, struct sockaddr *, 97 int *, struct ucred **); 98 static int msdosfs_mount (struct mount *, char *, caddr_t, 99 struct thread *); 100 static int msdosfs_root (struct mount *, struct vnode **); 101 static int msdosfs_statfs (struct mount *, struct statfs *, 102 struct thread *); 103 static int msdosfs_sync (struct mount *, int, struct thread *); 104 static int msdosfs_unmount (struct mount *, int, struct thread *); 105 static int msdosfs_vptofh (struct vnode *, struct fid *); 106 107 static int 108 update_mp(struct mount *mp, struct msdosfs_args *argp) 109 { 110 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 111 int error; 112 113 pmp->pm_gid = argp->gid; 114 pmp->pm_uid = argp->uid; 115 pmp->pm_mask = argp->mask & ALLPERMS; 116 pmp->pm_flags |= argp->flags & MSDOSFSMNT_MNTOPT; 117 if (pmp->pm_flags & MSDOSFSMNT_U2WTABLE) { 118 bcopy(argp->u2w, pmp->pm_u2w, sizeof(pmp->pm_u2w)); 119 bcopy(argp->d2u, pmp->pm_d2u, sizeof(pmp->pm_d2u)); 120 bcopy(argp->u2d, pmp->pm_u2d, sizeof(pmp->pm_u2d)); 121 } 122 if (pmp->pm_flags & MSDOSFSMNT_ULTABLE) { 123 bcopy(argp->ul, pmp->pm_ul, sizeof(pmp->pm_ul)); 124 bcopy(argp->lu, pmp->pm_lu, sizeof(pmp->pm_lu)); 125 } 126 127 #ifndef __DragonFly__ 128 /* 129 * GEMDOS knows nothing (yet) about win95 130 */ 131 if (pmp->pm_flags & MSDOSFSMNT_GEMDOSFS) 132 pmp->pm_flags |= MSDOSFSMNT_NOWIN95; 133 #endif 134 135 if (pmp->pm_flags & MSDOSFSMNT_NOWIN95) 136 pmp->pm_flags |= MSDOSFSMNT_SHORTNAME; 137 else if (!(pmp->pm_flags & 138 (MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) { 139 struct vnode *rootvp; 140 141 /* 142 * Try to divine whether to support Win'95 long filenames 143 */ 144 if (FAT32(pmp)) 145 pmp->pm_flags |= MSDOSFSMNT_LONGNAME; 146 else { 147 if ((error = msdosfs_root(mp, &rootvp)) != 0) 148 return error; 149 pmp->pm_flags |= findwin95(VTODE(rootvp)) 150 ? MSDOSFSMNT_LONGNAME 151 : MSDOSFSMNT_SHORTNAME; 152 vput(rootvp); 153 } 154 } 155 return 0; 156 } 157 158 #ifndef __DragonFly__ 159 int 160 msdosfs_mountroot(void) 161 { 162 struct mount *mp; 163 struct thread *td = curthread; /* XXX */ 164 size_t size; 165 int error; 166 struct msdosfs_args args; 167 struct vnode *rootvp; 168 169 if (root_device->dv_class != DV_DISK) 170 return (ENODEV); 171 172 /* 173 * Get vnodes for swapdev and rootdev. 174 */ 175 if (bdevvp(rootdev, &rootvp)) 176 panic("msdosfs_mountroot: can't setup rootvp"); 177 178 mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK); 179 bzero((char *)mp, (u_long)sizeof(struct mount)); 180 mp->mnt_op = &msdosfs_vfsops; 181 mp->mnt_flag = 0; 182 TAILQ_INIT(&mp->mnt_nvnodelist); 183 TAILQ_INIT(&mp->mnt_reservedvnlist); 184 185 args.flags = 0; 186 args.uid = 0; 187 args.gid = 0; 188 args.mask = 0777; 189 190 if ((error = mountmsdosfs(rootvp, mp, p, &args)) != 0) { 191 free(mp, M_MOUNT); 192 return (error); 193 } 194 195 if ((error = update_mp(mp, &args)) != 0) { 196 (void)msdosfs_unmount(mp, 0, p); 197 free(mp, M_MOUNT); 198 return (error); 199 } 200 201 if ((error = vfs_lock(mp)) != 0) { 202 (void)msdosfs_unmount(mp, 0, p); 203 free(mp, M_MOUNT); 204 return (error); 205 } 206 207 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list); 208 mp->mnt_vnodecovered = NULLVP; 209 (void) copystr("/", mp->mnt_stat.f_mntonname, MNAMELEN - 1, 210 &size); 211 bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size); 212 (void) copystr(ROOTNAME, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 213 &size); 214 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); 215 216 (void)msdosfs_statfs(mp, &mp->mnt_stat, p); 217 vfs_unlock(mp); 218 return (0); 219 } 220 #endif 221 222 /* 223 * mp - path - addr in user space of mount point (ie /usr or whatever) 224 * data - addr in user space of mount params including the name of the block 225 * special file to treat as a filesystem. 226 */ 227 static int 228 msdosfs_mount(struct mount *mp, char *path, caddr_t data, struct thread *td) 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 struct proc *p = td->td_proc; 238 struct nlookupdata nd; 239 240 KKASSERT(p); 241 242 error = copyin(data, (caddr_t)&args, sizeof(struct msdosfs_args)); 243 if (error) 244 return (error); 245 if (args.magic != MSDOSFS_ARGSMAGIC) 246 args.flags = 0; 247 /* 248 * If updating, check whether changing from read-only to 249 * read/write; if there is no device name, that's all we do. 250 */ 251 if (mp->mnt_flag & MNT_UPDATE) { 252 pmp = VFSTOMSDOSFS(mp); 253 error = 0; 254 if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_flag & MNT_RDONLY)) { 255 flags = WRITECLOSE; 256 if (mp->mnt_flag & MNT_FORCE) 257 flags |= FORCECLOSE; 258 error = vflush(mp, 0, flags); 259 } 260 if (!error && (mp->mnt_flag & MNT_RELOAD)) 261 /* not yet implemented */ 262 error = EOPNOTSUPP; 263 if (error) 264 return (error); 265 if ((pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_kern_flag & MNTK_WANTRDWR)) { 266 /* 267 * If upgrade to read-write by non-root, then verify 268 * that user has necessary permissions on the device. 269 */ 270 if (p->p_ucred->cr_uid != 0) { 271 devvp = pmp->pm_devvp; 272 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td); 273 error = VOP_ACCESS(devvp, VREAD | VWRITE, 274 p->p_ucred, td); 275 if (error) { 276 VOP_UNLOCK(devvp, 0, td); 277 return (error); 278 } 279 VOP_UNLOCK(devvp, 0, td); 280 } 281 pmp->pm_flags &= ~MSDOSFSMNT_RONLY; 282 } 283 if (args.fspec == 0) { 284 #ifdef __notyet__ /* doesn't work correctly with current mountd XXX */ 285 if (args.flags & MSDOSFSMNT_MNTOPT) { 286 pmp->pm_flags &= ~MSDOSFSMNT_MNTOPT; 287 pmp->pm_flags |= args.flags & MSDOSFSMNT_MNTOPT; 288 if (pmp->pm_flags & MSDOSFSMNT_NOWIN95) 289 pmp->pm_flags |= MSDOSFSMNT_SHORTNAME; 290 } 291 #endif 292 /* 293 * Process export requests. 294 */ 295 return (vfs_export(mp, &pmp->pm_export, &args.export)); 296 } 297 } 298 /* 299 * Not an update, or updating the name: look up the name 300 * and verify that it refers to a sensible block device. 301 */ 302 devvp = NULL; 303 error = nlookup_init(&nd, args.fspec, UIO_USERSPACE, NLC_FOLLOW); 304 if (error == 0) 305 error = nlookup(&nd); 306 if (error == 0) 307 error = cache_vref(nd.nl_ncp, nd.nl_cred, &devvp); 308 nlookup_done(&nd); 309 if (error) 310 return (error); 311 312 if (!vn_isdisk(devvp, &error)) { 313 vrele(devvp); 314 return (error); 315 } 316 /* 317 * If mount by non-root, then verify that user has necessary 318 * permissions on the device. 319 */ 320 if (p->p_ucred->cr_uid != 0) { 321 accessmode = VREAD; 322 if ((mp->mnt_flag & MNT_RDONLY) == 0) 323 accessmode |= VWRITE; 324 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td); 325 error = VOP_ACCESS(devvp, accessmode, p->p_ucred, td); 326 if (error) { 327 vput(devvp); 328 return (error); 329 } 330 VOP_UNLOCK(devvp, 0, td); 331 } 332 if ((mp->mnt_flag & MNT_UPDATE) == 0) { 333 error = mountmsdosfs(devvp, mp, td, &args); 334 #ifdef MSDOSFS_DEBUG /* only needed for the printf below */ 335 pmp = VFSTOMSDOSFS(mp); 336 #endif 337 } else { 338 if (devvp != pmp->pm_devvp) 339 error = EINVAL; /* XXX needs translation */ 340 else 341 vrele(devvp); 342 } 343 if (error) { 344 vrele(devvp); 345 return (error); 346 } 347 348 error = update_mp(mp, &args); 349 if (error) { 350 msdosfs_unmount(mp, MNT_FORCE, td); 351 return error; 352 } 353 354 (void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size); 355 bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size); 356 (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 357 &size); 358 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); 359 (void) msdosfs_statfs(mp, &mp->mnt_stat, td); 360 #ifdef MSDOSFS_DEBUG 361 printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap); 362 #endif 363 return (0); 364 } 365 366 static int 367 mountmsdosfs(struct vnode *devvp, struct mount *mp, struct thread *td, 368 struct msdosfs_args *argp) 369 { 370 struct msdosfsmount *pmp; 371 struct buf *bp; 372 dev_t dev; 373 #ifndef __DragonFly__ 374 struct partinfo dpart; 375 int bsize = 0, dtype = 0, tmp; 376 #endif 377 union bootsector *bsp; 378 struct byte_bpb33 *b33; 379 struct byte_bpb50 *b50; 380 struct byte_bpb710 *b710; 381 u_int8_t SecPerClust; 382 u_long clusters; 383 int ronly, error; 384 385 /* 386 * Disallow multiple mounts of the same device. 387 * Disallow mounting of a device that is currently in use 388 * Flush out any old buffers remaining from a previous use. 389 */ 390 error = vfs_mountedon(devvp); 391 if (error) 392 return (error); 393 if (count_udev(devvp->v_udev) > 0) 394 return (EBUSY); 395 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td); 396 error = vinvalbuf(devvp, V_SAVE, td, 0, 0); 397 VOP_UNLOCK(devvp, 0, td); 398 if (error) 399 return (error); 400 401 ronly = (mp->mnt_flag & MNT_RDONLY) != 0; 402 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td); 403 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, NULL, td); 404 VOP_UNLOCK(devvp, 0, td); 405 if (error) 406 return (error); 407 dev = devvp->v_rdev; 408 bp = NULL; /* both used in error_exit */ 409 pmp = NULL; 410 411 #ifndef __DragonFly__ 412 if (argp->flags & MSDOSFSMNT_GEMDOSFS) { 413 /* 414 * We need the disklabel to calculate the size of a FAT entry 415 * later on. Also make sure the partition contains a filesystem 416 * of type FS_MSDOS. This doesn't work for floppies, so we have 417 * to check for them too. 418 * 419 * At least some parts of the msdos fs driver seem to assume 420 * that the size of a disk block will always be 512 bytes. 421 * Let's check it... 422 */ 423 error = VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, td); 424 if (error) 425 goto error_exit; 426 tmp = dpart.part->p_fstype; 427 dtype = dpart.disklab->d_type; 428 bsize = dpart.disklab->d_secsize; 429 if (bsize != 512 || (dtype!=DTYPE_FLOPPY && tmp!=FS_MSDOS)) { 430 error = EINVAL; 431 goto error_exit; 432 } 433 } 434 #endif 435 436 /* 437 * Read the boot sector of the filesystem, and then check the 438 * boot signature. If not a dos boot sector then error out. 439 * 440 * NOTE: 2048 is a maximum sector size in current... 441 */ 442 error = bread(devvp, 0, 2048, &bp); 443 if (error) 444 goto error_exit; 445 bp->b_flags |= B_AGE; 446 bsp = (union bootsector *)bp->b_data; 447 b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB; 448 b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB; 449 b710 = (struct byte_bpb710 *)bsp->bs710.bsPBP; 450 451 #ifndef __DragonFly__ 452 if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) { 453 #endif 454 #ifndef MSDOSFS_NOCHECKSIG 455 if (bsp->bs50.bsBootSectSig0 != BOOTSIG0 456 || bsp->bs50.bsBootSectSig1 != BOOTSIG1) { 457 error = EINVAL; 458 goto error_exit; 459 } 460 #endif 461 #ifndef __DragonFly__ 462 } 463 #endif 464 465 pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK); 466 bzero((caddr_t)pmp, sizeof *pmp); 467 pmp->pm_mountp = mp; 468 469 /* 470 * Compute several useful quantities from the bpb in the 471 * bootsector. Copy in the dos 5 variant of the bpb then fix up 472 * the fields that are different between dos 5 and dos 3.3. 473 */ 474 SecPerClust = b50->bpbSecPerClust; 475 pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec); 476 pmp->pm_ResSectors = getushort(b50->bpbResSectors); 477 pmp->pm_FATs = b50->bpbFATs; 478 pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts); 479 pmp->pm_Sectors = getushort(b50->bpbSectors); 480 pmp->pm_FATsecs = getushort(b50->bpbFATsecs); 481 pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack); 482 pmp->pm_Heads = getushort(b50->bpbHeads); 483 pmp->pm_Media = b50->bpbMedia; 484 485 /* calculate the ratio of sector size to DEV_BSIZE */ 486 pmp->pm_BlkPerSec = pmp->pm_BytesPerSec / DEV_BSIZE; 487 488 #ifndef __DragonFly__ 489 if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) { 490 #endif 491 /* XXX - We should probably check more values here */ 492 if (!pmp->pm_BytesPerSec || !SecPerClust 493 || !pmp->pm_Heads 494 #ifdef PC98 495 || !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 255) { 496 #else 497 || !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 63) { 498 #endif 499 error = EINVAL; 500 goto error_exit; 501 } 502 #ifndef __DragonFly__ 503 } 504 #endif 505 506 if (pmp->pm_Sectors == 0) { 507 pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs); 508 pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors); 509 } else { 510 pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs); 511 pmp->pm_HugeSectors = pmp->pm_Sectors; 512 } 513 if (pmp->pm_HugeSectors > 0xffffffff / 514 (pmp->pm_BytesPerSec / sizeof(struct direntry)) + 1) { 515 /* 516 * We cannot deal currently with this size of disk 517 * due to fileid limitations (see msdosfs_getattr and 518 * msdosfs_readdir) 519 */ 520 error = EINVAL; 521 printf("mountmsdosfs(): disk too big, sorry\n"); 522 goto error_exit; 523 } 524 525 if (pmp->pm_RootDirEnts == 0) { 526 if (bsp->bs710.bsBootSectSig2 != BOOTSIG2 527 || bsp->bs710.bsBootSectSig3 != BOOTSIG3 528 || pmp->pm_Sectors 529 || pmp->pm_FATsecs 530 || getushort(b710->bpbFSVers)) { 531 error = EINVAL; 532 printf("mountmsdosfs(): bad FAT32 filesystem\n"); 533 goto error_exit; 534 } 535 pmp->pm_fatmask = FAT32_MASK; 536 pmp->pm_fatmult = 4; 537 pmp->pm_fatdiv = 1; 538 pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs); 539 if (getushort(b710->bpbExtFlags) & FATMIRROR) 540 pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM; 541 else 542 pmp->pm_flags |= MSDOSFS_FATMIRROR; 543 } else 544 pmp->pm_flags |= MSDOSFS_FATMIRROR; 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 < DEV_BSIZE) 555 || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1)) 556 || (pmp->pm_HugeSectors == 0) 557 ) { 558 error = EINVAL; 559 goto error_exit; 560 } 561 562 pmp->pm_HugeSectors *= pmp->pm_BlkPerSec; 563 pmp->pm_HiddenSects *= pmp->pm_BlkPerSec; /* XXX not used? */ 564 pmp->pm_FATsecs *= pmp->pm_BlkPerSec; 565 SecPerClust *= pmp->pm_BlkPerSec; 566 567 pmp->pm_fatblk = pmp->pm_ResSectors * pmp->pm_BlkPerSec; 568 569 if (FAT32(pmp)) { 570 pmp->pm_rootdirblk = getulong(b710->bpbRootClust); 571 pmp->pm_firstcluster = pmp->pm_fatblk 572 + (pmp->pm_FATs * pmp->pm_FATsecs); 573 pmp->pm_fsinfo = getushort(b710->bpbFSInfo) * pmp->pm_BlkPerSec; 574 } else { 575 pmp->pm_rootdirblk = pmp->pm_fatblk + 576 (pmp->pm_FATs * pmp->pm_FATsecs); 577 pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry) 578 + DEV_BSIZE - 1) 579 / DEV_BSIZE; /* in blocks */ 580 pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize; 581 } 582 583 pmp->pm_maxcluster = (pmp->pm_HugeSectors - pmp->pm_firstcluster) / 584 SecPerClust + 1; 585 pmp->pm_fatsize = pmp->pm_FATsecs * DEV_BSIZE; /* XXX not used? */ 586 587 #ifndef __DragonFly__ 588 if (argp->flags & MSDOSFSMNT_GEMDOSFS) { 589 if ((pmp->pm_maxcluster <= (0xff0 - 2)) 590 && ((dtype == DTYPE_FLOPPY) || ((dtype == DTYPE_VNODE) 591 && ((pmp->pm_Heads == 1) || (pmp->pm_Heads == 2)))) 592 ) { 593 pmp->pm_fatmask = FAT12_MASK; 594 pmp->pm_fatmult = 3; 595 pmp->pm_fatdiv = 2; 596 } else { 597 pmp->pm_fatmask = FAT16_MASK; 598 pmp->pm_fatmult = 2; 599 pmp->pm_fatdiv = 1; 600 } 601 } else 602 #endif 603 if (pmp->pm_fatmask == 0) { 604 if (pmp->pm_maxcluster 605 <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) { 606 /* 607 * This will usually be a floppy disk. This size makes 608 * sure that one fat entry will not be split across 609 * multiple blocks. 610 */ 611 pmp->pm_fatmask = FAT12_MASK; 612 pmp->pm_fatmult = 3; 613 pmp->pm_fatdiv = 2; 614 } else { 615 pmp->pm_fatmask = FAT16_MASK; 616 pmp->pm_fatmult = 2; 617 pmp->pm_fatdiv = 1; 618 } 619 } 620 621 clusters = (pmp->pm_fatsize / pmp->pm_fatmult) * pmp->pm_fatdiv; 622 if (pmp->pm_maxcluster >= clusters) { 623 printf("Warning: number of clusters (%ld) exceeds FAT " 624 "capacity (%ld)\n", pmp->pm_maxcluster + 1, clusters); 625 pmp->pm_maxcluster = clusters - 1; 626 } 627 628 629 if (FAT12(pmp)) 630 pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec; 631 else 632 pmp->pm_fatblocksize = MSDOSFS_DFLTBSIZE; 633 634 pmp->pm_fatblocksec = pmp->pm_fatblocksize / DEV_BSIZE; 635 pmp->pm_bnshift = ffs(DEV_BSIZE) - 1; 636 637 /* 638 * Compute mask and shift value for isolating cluster relative byte 639 * offsets and cluster numbers from a file offset. 640 */ 641 pmp->pm_bpcluster = SecPerClust * DEV_BSIZE; 642 pmp->pm_crbomask = pmp->pm_bpcluster - 1; 643 pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1; 644 645 /* 646 * Check for valid cluster size 647 * must be a power of 2 648 */ 649 if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) { 650 error = EINVAL; 651 goto error_exit; 652 } 653 654 /* 655 * Release the bootsector buffer. 656 */ 657 brelse(bp); 658 bp = NULL; 659 660 /* 661 * Check FSInfo. 662 */ 663 if (pmp->pm_fsinfo) { 664 struct fsinfo *fp; 665 666 if ((error = bread(devvp, pmp->pm_fsinfo, fsi_size(pmp), &bp)) != 0) 667 goto error_exit; 668 fp = (struct fsinfo *)bp->b_data; 669 if (!bcmp(fp->fsisig1, "RRaA", 4) 670 && !bcmp(fp->fsisig2, "rrAa", 4) 671 && !bcmp(fp->fsisig3, "\0\0\125\252", 4) 672 && !bcmp(fp->fsisig4, "\0\0\125\252", 4)) { 673 pmp->pm_nxtfree = getulong(fp->fsinxtfree); 674 if (pmp->pm_nxtfree == 0xffffffff) 675 pmp->pm_nxtfree = CLUST_FIRST; 676 } else 677 pmp->pm_fsinfo = 0; 678 brelse(bp); 679 bp = NULL; 680 } 681 682 /* 683 * Check and validate (or perhaps invalidate?) the fsinfo structure? 684 */ 685 if (pmp->pm_fsinfo && pmp->pm_nxtfree > pmp->pm_maxcluster) { 686 printf( 687 "Next free cluster in FSInfo (%lu) exceeds maxcluster (%lu)\n", 688 pmp->pm_nxtfree, pmp->pm_maxcluster); 689 error = EINVAL; 690 goto error_exit; 691 } 692 693 /* 694 * Allocate memory for the bitmap of allocated clusters, and then 695 * fill it in. 696 */ 697 pmp->pm_inusemap = malloc(((pmp->pm_maxcluster + N_INUSEBITS - 1) 698 / N_INUSEBITS) 699 * sizeof(*pmp->pm_inusemap), 700 M_MSDOSFSFAT, M_WAITOK); 701 702 /* 703 * fillinusemap() needs pm_devvp. 704 */ 705 pmp->pm_dev = dev; 706 pmp->pm_devvp = devvp; 707 708 /* 709 * Have the inuse map filled in. 710 */ 711 if ((error = fillinusemap(pmp)) != 0) 712 goto error_exit; 713 714 /* 715 * If they want fat updates to be synchronous then let them suffer 716 * the performance degradation in exchange for the on disk copy of 717 * the fat being correct just about all the time. I suppose this 718 * would be a good thing to turn on if the kernel is still flakey. 719 */ 720 if (mp->mnt_flag & MNT_SYNCHRONOUS) 721 pmp->pm_flags |= MSDOSFSMNT_WAITONFAT; 722 723 /* 724 * Finish up. 725 */ 726 if (ronly) 727 pmp->pm_flags |= MSDOSFSMNT_RONLY; 728 else 729 pmp->pm_fmod = 1; 730 mp->mnt_data = (qaddr_t) pmp; 731 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev); 732 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum; 733 mp->mnt_flag |= MNT_LOCAL; 734 vfs_add_vnodeops(mp, &mp->mnt_vn_norm_ops, msdosfs_vnodeop_entries); 735 dev->si_mountpoint = mp; 736 737 return 0; 738 739 error_exit: 740 if (bp) 741 brelse(bp); 742 (void) VOP_CLOSE(devvp, ronly ? FREAD : FREAD | FWRITE, td); 743 if (pmp) { 744 if (pmp->pm_inusemap) 745 free(pmp->pm_inusemap, M_MSDOSFSFAT); 746 free(pmp, M_MSDOSFSMNT); 747 mp->mnt_data = (qaddr_t)0; 748 } 749 return (error); 750 } 751 752 /* 753 * Unmount the filesystem described by mp. 754 */ 755 static int 756 msdosfs_unmount(struct mount *mp, int mntflags, struct thread *td) 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, 0, flags); 765 if (error) 766 return error; 767 pmp = VFSTOMSDOSFS(mp); 768 pmp->pm_devvp->v_rdev->si_mountpoint = 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 TAILQ_NEXT(vp, v_freelist), TAILQ_PREV(vp, v_freelist), 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 td); 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(struct mount *mp, struct vnode **vpp) 804 { 805 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 806 struct denode *ndep; 807 int error; 808 809 #ifdef MSDOSFS_DEBUG 810 printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp); 811 #endif 812 error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep); 813 if (error) 814 return (error); 815 *vpp = DETOV(ndep); 816 return (0); 817 } 818 819 static int 820 msdosfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td) 821 { 822 struct msdosfsmount *pmp; 823 824 pmp = VFSTOMSDOSFS(mp); 825 sbp->f_bsize = pmp->pm_bpcluster; 826 sbp->f_iosize = pmp->pm_bpcluster; 827 sbp->f_blocks = pmp->pm_maxcluster + 1; 828 sbp->f_bfree = pmp->pm_freeclustercount; 829 sbp->f_bavail = pmp->pm_freeclustercount; 830 sbp->f_files = pmp->pm_RootDirEnts; /* XXX */ 831 sbp->f_ffree = 0; /* what to put in here? */ 832 if (sbp != &mp->mnt_stat) { 833 sbp->f_type = mp->mnt_vfc->vfc_typenum; 834 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN); 835 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN); 836 } 837 strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN); 838 return (0); 839 } 840 841 struct scaninfo { 842 int rescan; 843 int allerror; 844 int waitfor; 845 thread_t td; 846 }; 847 848 static int msdosfs_sync_scan(struct mount *mp, struct vnode *vp, void *data); 849 850 static int 851 msdosfs_sync(struct mount *mp, int waitfor, struct thread *td) 852 { 853 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 854 struct scaninfo scaninfo; 855 int error; 856 857 /* 858 * If we ever switch to not updating all of the fats all the time, 859 * this would be the place to update them from the first one. 860 */ 861 if (pmp->pm_fmod != 0) { 862 if (pmp->pm_flags & MSDOSFSMNT_RONLY) 863 panic("msdosfs_sync: rofs mod"); 864 else { 865 /* update fats here */ 866 } 867 } 868 /* 869 * Write back each (modified) denode. 870 */ 871 scaninfo.allerror = 0; 872 scaninfo.rescan = 1; 873 scaninfo.td = td; 874 while (scaninfo.rescan) { 875 scaninfo.rescan = 0; 876 vmntvnodescan(mp, VMSC_GETVP|VMSC_NOWAIT, NULL, msdosfs_sync_scan, &scaninfo); 877 } 878 879 /* 880 * Flush filesystem control info. 881 */ 882 if (waitfor != MNT_LAZY) { 883 vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY, td); 884 if ((error = VOP_FSYNC(pmp->pm_devvp, waitfor, td)) != 0) 885 scaninfo.allerror = error; 886 VOP_UNLOCK(pmp->pm_devvp, 0, td); 887 } 888 return (scaninfo.allerror); 889 } 890 891 static int 892 msdosfs_sync_scan(struct mount *mp, struct vnode *vp, void *data) 893 { 894 struct scaninfo *info = data; 895 struct denode *dep; 896 int error; 897 898 dep = VTODE(vp); 899 if (vp->v_type == VNON || 900 ((dep->de_flag & 901 (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 && 902 (TAILQ_EMPTY(&vp->v_dirtyblkhd) || info->waitfor == MNT_LAZY))) { 903 return(0); 904 } 905 if ((error = VOP_FSYNC(vp, info->waitfor, info->td)) != 0) 906 info->allerror = error; 907 return(0); 908 } 909 910 static int 911 msdosfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp) 912 { 913 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 914 struct defid *defhp = (struct defid *) fhp; 915 struct denode *dep; 916 int error; 917 918 error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep); 919 if (error) { 920 *vpp = NULLVP; 921 return (error); 922 } 923 *vpp = DETOV(dep); 924 return (0); 925 } 926 927 static int 928 msdosfs_checkexp(struct mount *mp, struct sockaddr *nam, int *exflagsp, 929 struct ucred **credanonp) 930 { 931 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 932 struct netcred *np; 933 934 np = vfs_export_lookup(mp, &pmp->pm_export, nam); 935 if (np == NULL) 936 return (EACCES); 937 *exflagsp = np->netc_exflags; 938 *credanonp = &np->netc_anon; 939 return (0); 940 } 941 942 static int 943 msdosfs_vptofh(struct vnode *vp, struct fid *fhp) 944 { 945 struct denode *dep; 946 struct defid *defhp; 947 948 dep = VTODE(vp); 949 defhp = (struct defid *)fhp; 950 defhp->defid_len = sizeof(struct defid); 951 defhp->defid_dirclust = dep->de_dirclust; 952 defhp->defid_dirofs = dep->de_diroffset; 953 /* defhp->defid_gen = dep->de_gen; */ 954 return (0); 955 } 956 957 static struct vfsops msdosfs_vfsops = { 958 msdosfs_mount, 959 vfs_stdstart, 960 msdosfs_unmount, 961 msdosfs_root, 962 vfs_stdquotactl, 963 msdosfs_statfs, 964 msdosfs_sync, 965 vfs_stdvget, 966 msdosfs_fhtovp, 967 msdosfs_checkexp, 968 msdosfs_vptofh, 969 msdosfs_init, 970 msdosfs_uninit, 971 vfs_stdextattrctl, 972 }; 973 974 VFS_SET(msdosfs_vfsops, msdos, 0); 975