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