msdosfs_vfsops.c revision 75858
1183242Ssam/* $FreeBSD: head/sys/fs/msdosfs/msdosfs_vfsops.c 75858 2001-04-23 09:05:15Z grog $ */ 2183242Ssam/* $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 <net/radix.h> 59#include <sys/socket.h> 60#include <sys/mount.h> 61#include <sys/bio.h> 62#include <sys/buf.h> 63#include <sys/fcntl.h> 64#include <sys/malloc.h> 65#include <sys/stat.h> /* defines ALLPERMS */ 66#include <sys/mutex.h> 67 68#include <msdosfs/bpb.h> 69#include <msdosfs/bootsect.h> 70#include <msdosfs/direntry.h> 71#include <msdosfs/denode.h> 72#include <msdosfs/msdosfsmount.h> 73#include <msdosfs/fat.h> 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 88MALLOC_DEFINE(M_MSDOSFSMNT, "MSDOSFS mount", "MSDOSFS mount structure"); 89static MALLOC_DEFINE(M_MSDOSFSFAT, "MSDOSFS FAT", "MSDOSFS file allocation table"); 90 91static int update_mp __P((struct mount *mp, struct msdosfs_args *argp)); 92static int mountmsdosfs __P((struct vnode *devvp, struct mount *mp, 93 struct proc *p, struct msdosfs_args *argp)); 94static int msdosfs_fhtovp __P((struct mount *, struct fid *, 95 struct vnode **)); 96static int msdosfs_checkexp __P((struct mount *, struct sockaddr *, 97 int *, struct ucred **)); 98static int msdosfs_mount __P((struct mount *, char *, caddr_t, 99 struct nameidata *, struct proc *)); 100static int msdosfs_root __P((struct mount *, struct vnode **)); 101static int msdosfs_statfs __P((struct mount *, struct statfs *, 102 struct proc *)); 103static int msdosfs_sync __P((struct mount *, int, struct ucred *, 104 struct proc *)); 105static int msdosfs_unmount __P((struct mount *, int, struct proc *)); 106static int msdosfs_vptofh __P((struct vnode *, struct fid *)); 107 108static int 109update_mp(mp, argp) 110 struct mount *mp; 111 struct msdosfs_args *argp; 112{ 113 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 114 int error; 115 116 pmp->pm_gid = argp->gid; 117 pmp->pm_uid = argp->uid; 118 pmp->pm_mask = argp->mask & ALLPERMS; 119 pmp->pm_flags |= argp->flags & MSDOSFSMNT_MNTOPT; 120 if (pmp->pm_flags & MSDOSFSMNT_U2WTABLE) { 121 bcopy(argp->u2w, pmp->pm_u2w, sizeof(pmp->pm_u2w)); 122 bcopy(argp->d2u, pmp->pm_d2u, sizeof(pmp->pm_d2u)); 123 bcopy(argp->u2d, pmp->pm_u2d, sizeof(pmp->pm_u2d)); 124 } 125 if (pmp->pm_flags & MSDOSFSMNT_ULTABLE) { 126 bcopy(argp->ul, pmp->pm_ul, sizeof(pmp->pm_ul)); 127 bcopy(argp->lu, pmp->pm_lu, sizeof(pmp->pm_lu)); 128 } 129 130#ifndef __FreeBSD__ 131 /* 132 * GEMDOS knows nothing (yet) about win95 133 */ 134 if (pmp->pm_flags & MSDOSFSMNT_GEMDOSFS) 135 pmp->pm_flags |= MSDOSFSMNT_NOWIN95; 136#endif 137 138 if (pmp->pm_flags & MSDOSFSMNT_NOWIN95) 139 pmp->pm_flags |= MSDOSFSMNT_SHORTNAME; 140 else if (!(pmp->pm_flags & 141 (MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) { 142 struct vnode *rootvp; 143 144 /* 145 * Try to divine whether to support Win'95 long filenames 146 */ 147 if (FAT32(pmp)) 148 pmp->pm_flags |= MSDOSFSMNT_LONGNAME; 149 else { 150 if ((error = msdosfs_root(mp, &rootvp)) != 0) 151 return error; 152 pmp->pm_flags |= findwin95(VTODE(rootvp)) 153 ? MSDOSFSMNT_LONGNAME 154 : MSDOSFSMNT_SHORTNAME; 155 vput(rootvp); 156 } 157 } 158 return 0; 159} 160 161#ifndef __FreeBSD__ 162int 163msdosfs_mountroot() 164{ 165 register struct mount *mp; 166 struct proc *p = curproc; /* XXX */ 167 size_t size; 168 int error; 169 struct msdosfs_args args; 170 171 if (root_device->dv_class != DV_DISK) 172 return (ENODEV); 173 174 /* 175 * Get vnodes for swapdev and rootdev. 176 */ 177 if (bdevvp(rootdev, &rootvp)) 178 panic("msdosfs_mountroot: can't setup rootvp"); 179 180 mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK | M_ZERO); 181 mp->mnt_op = &msdosfs_vfsops; 182 mp->mnt_flag = 0; 183 LIST_INIT(&mp->mnt_vnodelist); 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 (void)msdosfs_statfs(mp, &mp->mnt_stat, p); 216 vfs_unlock(mp); 217 return (0); 218} 219#endif 220 221/* 222 * mp - path - addr in user space of mount point (ie /usr or whatever) 223 * data - addr in user space of mount params including the name of the block 224 * special file to treat as a filesystem. 225 */ 226static int 227msdosfs_mount(mp, path, data, ndp, p) 228 struct mount *mp; 229 char *path; 230 caddr_t data; 231 struct nameidata *ndp; 232 struct proc *p; 233{ 234 struct vnode *devvp; /* vnode for blk device to mount */ 235 struct msdosfs_args args; /* will hold data from mount request */ 236 /* msdosfs specific mount control block */ 237 struct msdosfsmount *pmp = NULL; 238 size_t size; 239 int error, flags; 240 mode_t accessmode; 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, NULLVP, 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, p); 273 error = VOP_ACCESS(devvp, VREAD | VWRITE, 274 p->p_ucred, p); 275 if (error) { 276 VOP_UNLOCK(devvp, 0, p); 277 return (error); 278 } 279 VOP_UNLOCK(devvp, 0, p); 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 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p); 303 error = namei(ndp); 304 if (error) 305 return (error); 306 devvp = ndp->ni_vp; 307 NDFREE(ndp, NDF_ONLY_PNBUF); 308 309 if (!vn_isdisk(devvp, &error)) { 310 vrele(devvp); 311 return (error); 312 } 313 /* 314 * If mount by non-root, then verify that user has necessary 315 * permissions on the device. 316 */ 317 if (p->p_ucred->cr_uid != 0) { 318 accessmode = VREAD; 319 if ((mp->mnt_flag & MNT_RDONLY) == 0) 320 accessmode |= VWRITE; 321 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p); 322 error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p); 323 if (error) { 324 vput(devvp); 325 return (error); 326 } 327 VOP_UNLOCK(devvp, 0, p); 328 } 329 if ((mp->mnt_flag & MNT_UPDATE) == 0) { 330 error = mountmsdosfs(devvp, mp, p, &args); 331#ifdef MSDOSFS_DEBUG /* only needed for the printf below */ 332 pmp = VFSTOMSDOSFS(mp); 333#endif 334 } else { 335 if (devvp != pmp->pm_devvp) 336 error = EINVAL; /* XXX needs translation */ 337 else 338 vrele(devvp); 339 } 340 if (error) { 341 vrele(devvp); 342 return (error); 343 } 344 345 error = update_mp(mp, &args); 346 if (error) { 347 msdosfs_unmount(mp, MNT_FORCE, p); 348 return error; 349 } 350 (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 351 &size); 352 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); 353 (void) msdosfs_statfs(mp, &mp->mnt_stat, p); 354#ifdef MSDOSFS_DEBUG 355 printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap); 356#endif 357 return (0); 358} 359 360static int 361mountmsdosfs(devvp, mp, p, argp) 362 struct vnode *devvp; 363 struct mount *mp; 364 struct proc *p; 365 struct msdosfs_args *argp; 366{ 367 struct msdosfsmount *pmp; 368 struct buf *bp; 369 dev_t dev = devvp->v_rdev; 370#ifndef __FreeBSD__ 371 struct partinfo dpart; 372 int bsize = 0, dtype = 0, tmp; 373#endif 374 union bootsector *bsp; 375 struct byte_bpb33 *b33; 376 struct byte_bpb50 *b50; 377 struct byte_bpb710 *b710; 378 u_int8_t SecPerClust; 379 u_long clusters; 380 int ronly, error; 381 382 /* 383 * Disallow multiple mounts of the same device. 384 * Disallow mounting of a device that is currently in use 385 * (except for root, which might share swap device for miniroot). 386 * Flush out any old buffers remaining from a previous use. 387 */ 388 error = vfs_mountedon(devvp); 389 if (error) 390 return (error); 391 if (vcount(devvp) > 1 && devvp != rootvp) 392 return (EBUSY); 393 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p); 394 error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0); 395 VOP_UNLOCK(devvp, 0, p); 396 if (error) 397 return (error); 398 399 ronly = (mp->mnt_flag & MNT_RDONLY) != 0; 400 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p); 401 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p); 402 VOP_UNLOCK(devvp, 0, p); 403 if (error) 404 return (error); 405 406 bp = NULL; /* both used in error_exit */ 407 pmp = NULL; 408 409#ifndef __FreeBSD__ 410 if (argp->flags & MSDOSFSMNT_GEMDOSFS) { 411 /* 412 * We need the disklabel to calculate the size of a FAT entry 413 * later on. Also make sure the partition contains a filesystem 414 * of type FS_MSDOS. This doesn't work for floppies, so we have 415 * to check for them too. 416 * 417 * At least some parts of the msdos fs driver seem to assume 418 * that the size of a disk block will always be 512 bytes. 419 * Let's check it... 420 */ 421 error = VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, 422 FREAD, NOCRED, p); 423 if (error) 424 goto error_exit; 425 tmp = dpart.part->p_fstype; 426 dtype = dpart.disklab->d_type; 427 bsize = dpart.disklab->d_secsize; 428 if (bsize != 512 || (dtype!=DTYPE_FLOPPY && tmp!=FS_MSDOS)) { 429 error = EINVAL; 430 goto error_exit; 431 } 432 } 433#endif 434 435 /* 436 * Read the boot sector of the filesystem, and then check the 437 * boot signature. If not a dos boot sector then error out. 438 * 439 * NOTE: 2048 is a maximum sector size in current... 440 */ 441 error = bread(devvp, 0, 2048, NOCRED, &bp); 442 if (error) 443 goto error_exit; 444 bp->b_flags |= B_AGE; 445 bsp = (union bootsector *)bp->b_data; 446 b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB; 447 b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB; 448 b710 = (struct byte_bpb710 *)bsp->bs710.bsPBP; 449 450#ifndef __FreeBSD__ 451 if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) { 452#endif 453#ifndef MSDOSFS_NOCHECKSIG 454 if (bsp->bs50.bsBootSectSig0 != BOOTSIG0 455 || bsp->bs50.bsBootSectSig1 != BOOTSIG1) { 456 error = EINVAL; 457 goto error_exit; 458 } 459#endif 460#ifndef __FreeBSD__ 461 } 462#endif 463 464 pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK | M_ZERO); 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 = MSDOSFS_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_rdev->si_mountpoint = mp; 725 726 return 0; 727 728error_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 */ 744static int 745msdosfs_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_rdev->si_mountpoint = 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 TAILQ_NEXT(vp, v_freelist), 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 794static int 795msdosfs_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 813static int 814msdosfs_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 838static int 839msdosfs_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 mtx_lock(&mntvnode_mtx); 865loop: 866 for (vp = LIST_FIRST(&mp->mnt_vnodelist); 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 mtx_lock(&vp->v_interlock); 875 nvp = LIST_NEXT(vp, v_mntvnodes); 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 mtx_unlock(&vp->v_interlock); 882 continue; 883 } 884 mtx_unlock(&mntvnode_mtx); 885 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, p); 886 if (error) { 887 mtx_lock(&mntvnode_mtx); 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 mtx_lock(&mntvnode_mtx); 898 } 899 mtx_unlock(&mntvnode_mtx); 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 914static int 915msdosfs_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 934static int 935msdosfs_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 952static int 953msdosfs_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 969static 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 msdosfs_uninit, 983 vfs_stdextattrctl, 984}; 985 986VFS_SET(msdosfs_vfsops, msdos, 0); 987