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