1 /* $FreeBSD$ */ 2 /* $NetBSD: msdosfs_vfsops.c,v 1.51 1997/11/17 15:36:58 ws Exp $ */ 3 4 /*- 5 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank. 6 * Copyright (C) 1994, 1995, 1997 TooLs GmbH. 7 * All rights reserved. 8 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below). 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by TooLs GmbH. 21 * 4. The name of TooLs GmbH may not be used to endorse or promote products 22 * derived from this software without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 27 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 30 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 */ 35 /* 36 * Written by Paul Popelka (paulp@uts.amdahl.com) 37 * 38 * You can do anything you want with this software, just don't say you wrote 39 * it, and don't remove this notice. 40 * 41 * This software is provided "as is". 42 * 43 * The author supplies this software to be publicly redistributed on the 44 * understanding that the author is not responsible for the correct 45 * functioning of this software in any circumstances and is not liable for 46 * any damages caused by this software. 47 * 48 * October 1992 49 */ 50 51 #include <sys/param.h> 52 #include <sys/systm.h> 53 #include <sys/conf.h> 54 #include <sys/namei.h> 55 #include <sys/proc.h> 56 #include <sys/kernel.h> 57 #include <sys/vnode.h> 58 #include <sys/mount.h> 59 #include <sys/bio.h> 60 #include <sys/buf.h> 61 #include <sys/fcntl.h> 62 #include <sys/malloc.h> 63 #include <sys/stat.h> /* defines ALLPERMS */ 64 #include <sys/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 87 MALLOC_DEFINE(M_MSDOSFSMNT, "MSDOSFS mount", "MSDOSFS mount structure"); 88 static MALLOC_DEFINE(M_MSDOSFSFAT, "MSDOSFS FAT", "MSDOSFS file allocation table"); 89 90 struct iconv_functions *msdosfs_iconv = NULL; 91 92 static int update_mp(struct mount *mp, struct msdosfs_args *argp); 93 static int mountmsdosfs(struct vnode *devvp, struct mount *mp, 94 struct thread *td, struct msdosfs_args *argp); 95 static vfs_fhtovp_t msdosfs_fhtovp; 96 static vfs_mount_t msdosfs_mount; 97 static vfs_root_t msdosfs_root; 98 static vfs_statfs_t msdosfs_statfs; 99 static vfs_sync_t msdosfs_sync; 100 static vfs_unmount_t msdosfs_unmount; 101 static vfs_vptofh_t msdosfs_vptofh; 102 103 static int 104 update_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 */ 156 static int 157 msdosfs_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 295 static int 296 mountmsdosfs(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 struct cdev *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 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(howmany(pmp->pm_maxcluster + 1, N_INUSEBITS) 590 * sizeof(*pmp->pm_inusemap), 591 M_MSDOSFSFAT, M_WAITOK); 592 593 /* 594 * fillinusemap() needs pm_devvp. 595 */ 596 pmp->pm_dev = dev; 597 pmp->pm_devvp = devvp; 598 599 /* 600 * Have the inuse map filled in. 601 */ 602 if ((error = fillinusemap(pmp)) != 0) 603 goto error_exit; 604 605 /* 606 * If they want fat updates to be synchronous then let them suffer 607 * the performance degradation in exchange for the on disk copy of 608 * the fat being correct just about all the time. I suppose this 609 * would be a good thing to turn on if the kernel is still flakey. 610 */ 611 if (mp->mnt_flag & MNT_SYNCHRONOUS) 612 pmp->pm_flags |= MSDOSFSMNT_WAITONFAT; 613 614 /* 615 * Finish up. 616 */ 617 if (ronly) 618 pmp->pm_flags |= MSDOSFSMNT_RONLY; 619 else { 620 /* Mark the volume dirty while it is mounted read/write. */ 621 if ((error = markvoldirty(pmp, 1)) != 0) 622 goto error_exit; 623 pmp->pm_fmod = 1; 624 } 625 mp->mnt_data = (qaddr_t) pmp; 626 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev); 627 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum; 628 mp->mnt_flag |= MNT_LOCAL; 629 devvp->v_rdev->si_mountpoint = mp; 630 631 return 0; 632 633 error_exit: 634 if (bp) 635 brelse(bp); 636 /* XXX: see comment above VOP_OPEN. */ 637 #ifdef notyet 638 (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD | FWRITE, NOCRED, td); 639 #else 640 (void)VOP_CLOSE(devvp, FREAD | FWRITE, NOCRED, td); 641 #endif 642 if (pmp) { 643 if (pmp->pm_inusemap) 644 free(pmp->pm_inusemap, M_MSDOSFSFAT); 645 free(pmp, M_MSDOSFSMNT); 646 mp->mnt_data = (qaddr_t)0; 647 } 648 return (error); 649 } 650 651 /* 652 * Unmount the filesystem described by mp. 653 */ 654 static int 655 msdosfs_unmount(mp, mntflags, td) 656 struct mount *mp; 657 int mntflags; 658 struct thread *td; 659 { 660 struct msdosfsmount *pmp; 661 int error, flags; 662 663 flags = 0; 664 if (mntflags & MNT_FORCE) 665 flags |= FORCECLOSE; 666 error = vflush(mp, 0, flags); 667 if (error) 668 return error; 669 pmp = VFSTOMSDOSFS(mp); 670 if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) { 671 if (pmp->pm_w2u) 672 msdosfs_iconv->close(pmp->pm_w2u); 673 if (pmp->pm_u2w) 674 msdosfs_iconv->close(pmp->pm_u2w); 675 if (pmp->pm_d2u) 676 msdosfs_iconv->close(pmp->pm_d2u); 677 if (pmp->pm_u2d) 678 msdosfs_iconv->close(pmp->pm_u2d); 679 } 680 pmp->pm_devvp->v_rdev->si_mountpoint = NULL; 681 682 /* If the volume was mounted read/write, mark it clean now. */ 683 if ((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0) { 684 error = markvoldirty(pmp, 0); 685 if (error && (flags & FORCECLOSE) == 0) 686 return (error); 687 } 688 #ifdef MSDOSFS_DEBUG 689 { 690 struct vnode *vp = pmp->pm_devvp; 691 692 VI_LOCK(vp); 693 printf("msdosfs_umount(): just before calling VOP_CLOSE()\n"); 694 printf("iflag %08lx, usecount %d, writecount %d, holdcnt %ld\n", 695 vp->vi_flag, vp->v_usecount, vp->v_writecount, 696 vp->v_holdcnt); 697 printf("id %lu, mount %p, op %p\n", 698 vp->v_id, vp->v_mount, vp->v_op); 699 printf("freef %p, freeb %p, mount %p\n", 700 TAILQ_NEXT(vp, v_freelist), vp->v_freelist.tqe_prev, 701 vp->v_mount); 702 printf("cleanblkhd %p, dirtyblkhd %p, numoutput %ld, type %d\n", 703 TAILQ_FIRST(&vp->v_cleanblkhd), 704 TAILQ_FIRST(&vp->v_dirtyblkhd), 705 vp->v_numoutput, vp->v_type); 706 printf("union %p, tag %s, data[0] %08x, data[1] %08x\n", 707 vp->v_socket, vp->v_tag, 708 ((u_int *)vp->v_data)[0], 709 ((u_int *)vp->v_data)[1]); 710 VI_UNLOCK(vp); 711 } 712 #endif 713 /* XXX: see comment above VOP_OPEN. */ 714 #ifdef notyet 715 error = VOP_CLOSE(pmp->pm_devvp, 716 (pmp->pm_flags & MSDOSFSMNT_RONLY) ? FREAD : FREAD | FWRITE, 717 NOCRED, td); 718 #else 719 error = VOP_CLOSE(pmp->pm_devvp, FREAD | FWRITE, NOCRED, td); 720 #endif 721 vrele(pmp->pm_devvp); 722 free(pmp->pm_inusemap, M_MSDOSFSFAT); 723 free(pmp, M_MSDOSFSMNT); 724 mp->mnt_data = (qaddr_t)0; 725 mp->mnt_flag &= ~MNT_LOCAL; 726 return (error); 727 } 728 729 static int 730 msdosfs_root(mp, vpp) 731 struct mount *mp; 732 struct vnode **vpp; 733 { 734 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 735 struct denode *ndep; 736 int error; 737 738 #ifdef MSDOSFS_DEBUG 739 printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp); 740 #endif 741 error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep); 742 if (error) 743 return (error); 744 *vpp = DETOV(ndep); 745 return (0); 746 } 747 748 static int 749 msdosfs_statfs(mp, sbp, td) 750 struct mount *mp; 751 struct statfs *sbp; 752 struct thread *td; 753 { 754 struct msdosfsmount *pmp; 755 756 pmp = VFSTOMSDOSFS(mp); 757 sbp->f_bsize = pmp->pm_bpcluster; 758 sbp->f_iosize = pmp->pm_bpcluster; 759 sbp->f_blocks = pmp->pm_maxcluster + 1; 760 sbp->f_bfree = pmp->pm_freeclustercount; 761 sbp->f_bavail = pmp->pm_freeclustercount; 762 sbp->f_files = pmp->pm_RootDirEnts; /* XXX */ 763 sbp->f_ffree = 0; /* what to put in here? */ 764 if (sbp != &mp->mnt_stat) { 765 sbp->f_type = mp->mnt_vfc->vfc_typenum; 766 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN); 767 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN); 768 } 769 strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN); 770 return (0); 771 } 772 773 static int 774 msdosfs_sync(mp, waitfor, cred, td) 775 struct mount *mp; 776 int waitfor; 777 struct ucred *cred; 778 struct thread *td; 779 { 780 struct vnode *vp, *nvp; 781 struct denode *dep; 782 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 783 int error, allerror = 0; 784 785 /* 786 * If we ever switch to not updating all of the fats all the time, 787 * this would be the place to update them from the first one. 788 */ 789 if (pmp->pm_fmod != 0) { 790 if (pmp->pm_flags & MSDOSFSMNT_RONLY) 791 panic("msdosfs_sync: rofs mod"); 792 else { 793 /* update fats here */ 794 } 795 } 796 /* 797 * Write back each (modified) denode. 798 */ 799 MNT_ILOCK(mp); 800 loop: 801 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp != NULL; vp = nvp) { 802 /* 803 * If the vnode that we are about to sync is no longer 804 * associated with this mount point, start over. 805 */ 806 if (vp->v_mount != mp) 807 goto loop; 808 nvp = TAILQ_NEXT(vp, v_nmntvnodes); 809 810 VI_LOCK(vp); 811 if (vp->v_iflag & VI_XLOCK) { 812 VI_UNLOCK(vp); 813 continue; 814 } 815 MNT_IUNLOCK(mp); 816 dep = VTODE(vp); 817 if (vp->v_type == VNON || 818 ((dep->de_flag & 819 (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 && 820 (TAILQ_EMPTY(&vp->v_dirtyblkhd) || waitfor == MNT_LAZY))) { 821 VI_UNLOCK(vp); 822 MNT_ILOCK(mp); 823 continue; 824 } 825 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, td); 826 if (error) { 827 MNT_ILOCK(mp); 828 if (error == ENOENT) 829 goto loop; 830 continue; 831 } 832 error = VOP_FSYNC(vp, cred, waitfor, td); 833 if (error) 834 allerror = error; 835 VOP_UNLOCK(vp, 0, td); 836 vrele(vp); 837 MNT_ILOCK(mp); 838 } 839 MNT_IUNLOCK(mp); 840 841 /* 842 * Flush filesystem control info. 843 */ 844 if (waitfor != MNT_LAZY) { 845 vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY, td); 846 error = VOP_FSYNC(pmp->pm_devvp, cred, waitfor, td); 847 if (error) 848 allerror = error; 849 VOP_UNLOCK(pmp->pm_devvp, 0, td); 850 } 851 return (allerror); 852 } 853 854 static int 855 msdosfs_fhtovp(mp, fhp, vpp) 856 struct mount *mp; 857 struct fid *fhp; 858 struct vnode **vpp; 859 { 860 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 861 struct defid *defhp = (struct defid *) fhp; 862 struct denode *dep; 863 int error; 864 865 error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep); 866 if (error) { 867 *vpp = NULLVP; 868 return (error); 869 } 870 *vpp = DETOV(dep); 871 return (0); 872 } 873 874 static int 875 msdosfs_vptofh(vp, fhp) 876 struct vnode *vp; 877 struct fid *fhp; 878 { 879 struct denode *dep; 880 struct defid *defhp; 881 882 dep = VTODE(vp); 883 defhp = (struct defid *)fhp; 884 defhp->defid_len = sizeof(struct defid); 885 defhp->defid_dirclust = dep->de_dirclust; 886 defhp->defid_dirofs = dep->de_diroffset; 887 /* defhp->defid_gen = dep->de_gen; */ 888 return (0); 889 } 890 891 static struct vfsops msdosfs_vfsops = { 892 .vfs_fhtovp = msdosfs_fhtovp, 893 .vfs_init = msdosfs_init, 894 .vfs_mount = msdosfs_mount, 895 .vfs_root = msdosfs_root, 896 .vfs_statfs = msdosfs_statfs, 897 .vfs_sync = msdosfs_sync, 898 .vfs_uninit = msdosfs_uninit, 899 .vfs_unmount = msdosfs_unmount, 900 .vfs_vptofh = msdosfs_vptofh, 901 }; 902 903 VFS_SET(msdosfs_vfsops, msdosfs, 0); 904 MODULE_VERSION(msdosfs, 1); 905