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/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 86 MALLOC_DEFINE(M_MSDOSFSMNT, "MSDOSFS mount", "MSDOSFS mount structure"); 87 static MALLOC_DEFINE(M_MSDOSFSFAT, "MSDOSFS FAT", "MSDOSFS file allocation table"); 88 89 static int update_mp(struct mount *mp, struct msdosfs_args *argp); 90 static int mountmsdosfs(struct vnode *devvp, struct mount *mp, 91 struct thread *td, struct msdosfs_args *argp); 92 static vfs_fhtovp_t msdosfs_fhtovp; 93 static vfs_mount_t msdosfs_mount; 94 static vfs_root_t msdosfs_root; 95 static vfs_statfs_t msdosfs_statfs; 96 static vfs_sync_t msdosfs_sync; 97 static vfs_unmount_t msdosfs_unmount; 98 static vfs_vptofh_t msdosfs_vptofh; 99 100 static int 101 update_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 */ 150 static int 151 msdosfs_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 284 static int 285 mountmsdosfs(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 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, td); 322 VOP_UNLOCK(devvp, 0, td); 323 if (error) 324 return (error); 325 326 bp = NULL; /* both used in error_exit */ 327 pmp = NULL; 328 329 /* 330 * Read the boot sector of the filesystem, and then check the 331 * boot signature. If not a dos boot sector then error out. 332 * 333 * NOTE: 2048 is a maximum sector size in current... 334 */ 335 error = bread(devvp, 0, 2048, NOCRED, &bp); 336 if (error) 337 goto error_exit; 338 bp->b_flags |= B_AGE; 339 bsp = (union bootsector *)bp->b_data; 340 b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB; 341 b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB; 342 b710 = (struct byte_bpb710 *)bsp->bs710.bsBPB; 343 344 #ifndef MSDOSFS_NOCHECKSIG 345 if (bsp->bs50.bsBootSectSig0 != BOOTSIG0 346 || bsp->bs50.bsBootSectSig1 != BOOTSIG1) { 347 error = EINVAL; 348 goto error_exit; 349 } 350 #endif 351 352 pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK | M_ZERO); 353 pmp->pm_mountp = mp; 354 355 /* 356 * Compute several useful quantities from the bpb in the 357 * bootsector. Copy in the dos 5 variant of the bpb then fix up 358 * the fields that are different between dos 5 and dos 3.3. 359 */ 360 SecPerClust = b50->bpbSecPerClust; 361 pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec); 362 if (pmp->pm_BytesPerSec < DEV_BSIZE) { 363 error = EINVAL; 364 goto error_exit; 365 } 366 pmp->pm_ResSectors = getushort(b50->bpbResSectors); 367 pmp->pm_FATs = b50->bpbFATs; 368 pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts); 369 pmp->pm_Sectors = getushort(b50->bpbSectors); 370 pmp->pm_FATsecs = getushort(b50->bpbFATsecs); 371 pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack); 372 pmp->pm_Heads = getushort(b50->bpbHeads); 373 pmp->pm_Media = b50->bpbMedia; 374 375 /* calculate the ratio of sector size to DEV_BSIZE */ 376 pmp->pm_BlkPerSec = pmp->pm_BytesPerSec / DEV_BSIZE; 377 378 /* XXX - We should probably check more values here */ 379 if (!pmp->pm_BytesPerSec || !SecPerClust 380 || !pmp->pm_Heads || pmp->pm_Heads > 255 381 #ifdef PC98 382 || !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 255) { 383 #else 384 || !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 63) { 385 #endif 386 error = EINVAL; 387 goto error_exit; 388 } 389 390 if (pmp->pm_Sectors == 0) { 391 pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs); 392 pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors); 393 } else { 394 pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs); 395 pmp->pm_HugeSectors = pmp->pm_Sectors; 396 } 397 if (pmp->pm_HugeSectors > 0xffffffff / 398 (pmp->pm_BytesPerSec / sizeof(struct direntry)) + 1) { 399 /* 400 * We cannot deal currently with this size of disk 401 * due to fileid limitations (see msdosfs_getattr and 402 * msdosfs_readdir) 403 */ 404 error = EINVAL; 405 printf("mountmsdosfs(): disk too big, sorry\n"); 406 goto error_exit; 407 } 408 409 if (pmp->pm_RootDirEnts == 0) { 410 if (bsp->bs710.bsBootSectSig2 != BOOTSIG2 411 || bsp->bs710.bsBootSectSig3 != BOOTSIG3 412 || pmp->pm_Sectors 413 || pmp->pm_FATsecs 414 || getushort(b710->bpbFSVers)) { 415 error = EINVAL; 416 printf("mountmsdosfs(): bad FAT32 filesystem\n"); 417 goto error_exit; 418 } 419 pmp->pm_fatmask = FAT32_MASK; 420 pmp->pm_fatmult = 4; 421 pmp->pm_fatdiv = 1; 422 pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs); 423 if (getushort(b710->bpbExtFlags) & FATMIRROR) 424 pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM; 425 else 426 pmp->pm_flags |= MSDOSFS_FATMIRROR; 427 } else 428 pmp->pm_flags |= MSDOSFS_FATMIRROR; 429 430 /* 431 * Check a few values (could do some more): 432 * - logical sector size: power of 2, >= block size 433 * - sectors per cluster: power of 2, >= 1 434 * - number of sectors: >= 1, <= size of partition 435 * - number of FAT sectors: >= 1 436 */ 437 if ( (SecPerClust == 0) 438 || (SecPerClust & (SecPerClust - 1)) 439 || (pmp->pm_BytesPerSec < DEV_BSIZE) 440 || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1)) 441 || (pmp->pm_HugeSectors == 0) 442 || (pmp->pm_FATsecs == 0) 443 ) { 444 error = EINVAL; 445 goto error_exit; 446 } 447 448 pmp->pm_HugeSectors *= pmp->pm_BlkPerSec; 449 pmp->pm_HiddenSects *= pmp->pm_BlkPerSec; /* XXX not used? */ 450 pmp->pm_FATsecs *= pmp->pm_BlkPerSec; 451 SecPerClust *= pmp->pm_BlkPerSec; 452 453 pmp->pm_fatblk = pmp->pm_ResSectors * pmp->pm_BlkPerSec; 454 455 if (FAT32(pmp)) { 456 pmp->pm_rootdirblk = getulong(b710->bpbRootClust); 457 pmp->pm_firstcluster = pmp->pm_fatblk 458 + (pmp->pm_FATs * pmp->pm_FATsecs); 459 pmp->pm_fsinfo = getushort(b710->bpbFSInfo) * pmp->pm_BlkPerSec; 460 } else { 461 pmp->pm_rootdirblk = pmp->pm_fatblk + 462 (pmp->pm_FATs * pmp->pm_FATsecs); 463 pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry) 464 + DEV_BSIZE - 1) 465 / DEV_BSIZE; /* in blocks */ 466 pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize; 467 } 468 469 pmp->pm_maxcluster = (pmp->pm_HugeSectors - pmp->pm_firstcluster) / 470 SecPerClust + 1; 471 pmp->pm_fatsize = pmp->pm_FATsecs * DEV_BSIZE; /* XXX not used? */ 472 473 if (pmp->pm_fatmask == 0) { 474 if (pmp->pm_maxcluster 475 <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) { 476 /* 477 * This will usually be a floppy disk. This size makes 478 * sure that one fat entry will not be split across 479 * multiple blocks. 480 */ 481 pmp->pm_fatmask = FAT12_MASK; 482 pmp->pm_fatmult = 3; 483 pmp->pm_fatdiv = 2; 484 } else { 485 pmp->pm_fatmask = FAT16_MASK; 486 pmp->pm_fatmult = 2; 487 pmp->pm_fatdiv = 1; 488 } 489 } 490 491 clusters = (pmp->pm_fatsize / pmp->pm_fatmult) * pmp->pm_fatdiv; 492 if (pmp->pm_maxcluster >= clusters) { 493 printf("Warning: number of clusters (%ld) exceeds FAT " 494 "capacity (%ld)\n", pmp->pm_maxcluster + 1, clusters); 495 pmp->pm_maxcluster = clusters - 1; 496 } 497 498 499 if (FAT12(pmp)) 500 pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec; 501 else 502 pmp->pm_fatblocksize = MSDOSFS_DFLTBSIZE; 503 504 pmp->pm_fatblocksec = pmp->pm_fatblocksize / DEV_BSIZE; 505 pmp->pm_bnshift = ffs(DEV_BSIZE) - 1; 506 507 /* 508 * Compute mask and shift value for isolating cluster relative byte 509 * offsets and cluster numbers from a file offset. 510 */ 511 pmp->pm_bpcluster = SecPerClust * DEV_BSIZE; 512 pmp->pm_crbomask = pmp->pm_bpcluster - 1; 513 pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1; 514 515 /* 516 * Check for valid cluster size 517 * must be a power of 2 518 */ 519 if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) { 520 error = EINVAL; 521 goto error_exit; 522 } 523 524 /* 525 * Release the bootsector buffer. 526 */ 527 brelse(bp); 528 bp = NULL; 529 530 /* 531 * Check FSInfo. 532 */ 533 if (pmp->pm_fsinfo) { 534 struct fsinfo *fp; 535 536 if ((error = bread(devvp, pmp->pm_fsinfo, fsi_size(pmp), 537 NOCRED, &bp)) != 0) 538 goto error_exit; 539 fp = (struct fsinfo *)bp->b_data; 540 if (!bcmp(fp->fsisig1, "RRaA", 4) 541 && !bcmp(fp->fsisig2, "rrAa", 4) 542 && !bcmp(fp->fsisig3, "\0\0\125\252", 4) 543 && !bcmp(fp->fsisig4, "\0\0\125\252", 4)) 544 pmp->pm_nxtfree = getulong(fp->fsinxtfree); 545 else 546 pmp->pm_fsinfo = 0; 547 brelse(bp); 548 bp = NULL; 549 } 550 551 /* 552 * Check and validate (or perhaps invalidate?) the fsinfo structure? 553 */ 554 if (pmp->pm_fsinfo && pmp->pm_nxtfree > pmp->pm_maxcluster) { 555 printf( 556 "Next free cluster in FSInfo (%lu) exceeds maxcluster (%lu)\n", 557 pmp->pm_nxtfree, pmp->pm_maxcluster); 558 error = EINVAL; 559 goto error_exit; 560 } 561 562 /* 563 * Allocate memory for the bitmap of allocated clusters, and then 564 * fill it in. 565 */ 566 pmp->pm_inusemap = malloc(((pmp->pm_maxcluster + N_INUSEBITS - 1) 567 / N_INUSEBITS) 568 * sizeof(*pmp->pm_inusemap), 569 M_MSDOSFSFAT, M_WAITOK); 570 571 /* 572 * fillinusemap() needs pm_devvp. 573 */ 574 pmp->pm_dev = dev; 575 pmp->pm_devvp = devvp; 576 577 /* 578 * Have the inuse map filled in. 579 */ 580 if ((error = fillinusemap(pmp)) != 0) 581 goto error_exit; 582 583 /* 584 * If they want fat updates to be synchronous then let them suffer 585 * the performance degradation in exchange for the on disk copy of 586 * the fat being correct just about all the time. I suppose this 587 * would be a good thing to turn on if the kernel is still flakey. 588 */ 589 if (mp->mnt_flag & MNT_SYNCHRONOUS) 590 pmp->pm_flags |= MSDOSFSMNT_WAITONFAT; 591 592 /* 593 * Finish up. 594 */ 595 if (ronly) 596 pmp->pm_flags |= MSDOSFSMNT_RONLY; 597 else 598 pmp->pm_fmod = 1; 599 mp->mnt_data = (qaddr_t) pmp; 600 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev); 601 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum; 602 mp->mnt_flag |= MNT_LOCAL; 603 devvp->v_rdev->si_mountpoint = mp; 604 605 return 0; 606 607 error_exit: 608 if (bp) 609 brelse(bp); 610 (void) VOP_CLOSE(devvp, ronly ? FREAD : FREAD | FWRITE, NOCRED, td); 611 if (pmp) { 612 if (pmp->pm_inusemap) 613 free(pmp->pm_inusemap, M_MSDOSFSFAT); 614 free(pmp, M_MSDOSFSMNT); 615 mp->mnt_data = (qaddr_t)0; 616 } 617 return (error); 618 } 619 620 /* 621 * Unmount the filesystem described by mp. 622 */ 623 static int 624 msdosfs_unmount(mp, mntflags, td) 625 struct mount *mp; 626 int mntflags; 627 struct thread *td; 628 { 629 struct msdosfsmount *pmp; 630 int error, flags; 631 632 flags = 0; 633 if (mntflags & MNT_FORCE) 634 flags |= FORCECLOSE; 635 error = vflush(mp, 0, flags); 636 if (error) 637 return error; 638 pmp = VFSTOMSDOSFS(mp); 639 pmp->pm_devvp->v_rdev->si_mountpoint = NULL; 640 #ifdef MSDOSFS_DEBUG 641 { 642 struct vnode *vp = pmp->pm_devvp; 643 644 VI_LOCK(vp); 645 printf("msdosfs_umount(): just before calling VOP_CLOSE()\n"); 646 printf("iflag %08lx, usecount %d, writecount %d, holdcnt %ld\n", 647 vp->vi_flag, vp->v_usecount, vp->v_writecount, 648 vp->v_holdcnt); 649 printf("id %lu, mount %p, op %p\n", 650 vp->v_id, vp->v_mount, vp->v_op); 651 printf("freef %p, freeb %p, mount %p\n", 652 TAILQ_NEXT(vp, v_freelist), vp->v_freelist.tqe_prev, 653 vp->v_mount); 654 printf("cleanblkhd %p, dirtyblkhd %p, numoutput %ld, type %d\n", 655 TAILQ_FIRST(&vp->v_cleanblkhd), 656 TAILQ_FIRST(&vp->v_dirtyblkhd), 657 vp->v_numoutput, vp->v_type); 658 printf("union %p, tag %s, data[0] %08x, data[1] %08x\n", 659 vp->v_socket, vp->v_tag, 660 ((u_int *)vp->v_data)[0], 661 ((u_int *)vp->v_data)[1]); 662 VI_UNLOCK(vp); 663 } 664 #endif 665 error = VOP_CLOSE(pmp->pm_devvp, 666 (pmp->pm_flags&MSDOSFSMNT_RONLY) ? FREAD : FREAD | FWRITE, 667 NOCRED, td); 668 vrele(pmp->pm_devvp); 669 free(pmp->pm_inusemap, M_MSDOSFSFAT); 670 free(pmp, M_MSDOSFSMNT); 671 mp->mnt_data = (qaddr_t)0; 672 mp->mnt_flag &= ~MNT_LOCAL; 673 return (error); 674 } 675 676 static int 677 msdosfs_root(mp, vpp) 678 struct mount *mp; 679 struct vnode **vpp; 680 { 681 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 682 struct denode *ndep; 683 int error; 684 685 #ifdef MSDOSFS_DEBUG 686 printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp); 687 #endif 688 error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep); 689 if (error) 690 return (error); 691 *vpp = DETOV(ndep); 692 return (0); 693 } 694 695 static int 696 msdosfs_statfs(mp, sbp, td) 697 struct mount *mp; 698 struct statfs *sbp; 699 struct thread *td; 700 { 701 struct msdosfsmount *pmp; 702 703 pmp = VFSTOMSDOSFS(mp); 704 sbp->f_bsize = pmp->pm_bpcluster; 705 sbp->f_iosize = pmp->pm_bpcluster; 706 sbp->f_blocks = pmp->pm_maxcluster + 1; 707 sbp->f_bfree = pmp->pm_freeclustercount; 708 sbp->f_bavail = pmp->pm_freeclustercount; 709 sbp->f_files = pmp->pm_RootDirEnts; /* XXX */ 710 sbp->f_ffree = 0; /* what to put in here? */ 711 if (sbp != &mp->mnt_stat) { 712 sbp->f_type = mp->mnt_vfc->vfc_typenum; 713 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN); 714 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN); 715 } 716 strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN); 717 return (0); 718 } 719 720 static int 721 msdosfs_sync(mp, waitfor, cred, td) 722 struct mount *mp; 723 int waitfor; 724 struct ucred *cred; 725 struct thread *td; 726 { 727 struct vnode *vp, *nvp; 728 struct denode *dep; 729 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 730 int error, allerror = 0; 731 732 /* 733 * If we ever switch to not updating all of the fats all the time, 734 * this would be the place to update them from the first one. 735 */ 736 if (pmp->pm_fmod != 0) { 737 if (pmp->pm_flags & MSDOSFSMNT_RONLY) 738 panic("msdosfs_sync: rofs mod"); 739 else { 740 /* update fats here */ 741 } 742 } 743 /* 744 * Write back each (modified) denode. 745 */ 746 mtx_lock(&mntvnode_mtx); 747 loop: 748 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp != NULL; vp = nvp) { 749 /* 750 * If the vnode that we are about to sync is no longer 751 * associated with this mount point, start over. 752 */ 753 if (vp->v_mount != mp) 754 goto loop; 755 nvp = TAILQ_NEXT(vp, v_nmntvnodes); 756 757 mtx_unlock(&mntvnode_mtx); 758 VI_LOCK(vp); 759 dep = VTODE(vp); 760 if (vp->v_type == VNON || 761 ((dep->de_flag & 762 (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 && 763 (TAILQ_EMPTY(&vp->v_dirtyblkhd) || waitfor == MNT_LAZY))) { 764 VI_UNLOCK(vp); 765 mtx_lock(&mntvnode_mtx); 766 continue; 767 } 768 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, td); 769 if (error) { 770 mtx_lock(&mntvnode_mtx); 771 if (error == ENOENT) 772 goto loop; 773 continue; 774 } 775 error = VOP_FSYNC(vp, cred, waitfor, td); 776 if (error) 777 allerror = error; 778 VOP_UNLOCK(vp, 0, td); 779 vrele(vp); 780 mtx_lock(&mntvnode_mtx); 781 } 782 mtx_unlock(&mntvnode_mtx); 783 784 /* 785 * Flush filesystem control info. 786 */ 787 if (waitfor != MNT_LAZY) { 788 vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY, td); 789 error = VOP_FSYNC(pmp->pm_devvp, cred, waitfor, td); 790 if (error) 791 allerror = error; 792 VOP_UNLOCK(pmp->pm_devvp, 0, td); 793 } 794 return (allerror); 795 } 796 797 static int 798 msdosfs_fhtovp(mp, fhp, vpp) 799 struct mount *mp; 800 struct fid *fhp; 801 struct vnode **vpp; 802 { 803 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 804 struct defid *defhp = (struct defid *) fhp; 805 struct denode *dep; 806 int error; 807 808 error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep); 809 if (error) { 810 *vpp = NULLVP; 811 return (error); 812 } 813 *vpp = DETOV(dep); 814 return (0); 815 } 816 817 static int 818 msdosfs_vptofh(vp, fhp) 819 struct vnode *vp; 820 struct fid *fhp; 821 { 822 struct denode *dep; 823 struct defid *defhp; 824 825 dep = VTODE(vp); 826 defhp = (struct defid *)fhp; 827 defhp->defid_len = sizeof(struct defid); 828 defhp->defid_dirclust = dep->de_dirclust; 829 defhp->defid_dirofs = dep->de_diroffset; 830 /* defhp->defid_gen = dep->de_gen; */ 831 return (0); 832 } 833 834 static struct vfsops msdosfs_vfsops = { 835 msdosfs_mount, 836 vfs_stdstart, 837 msdosfs_unmount, 838 msdosfs_root, 839 vfs_stdquotactl, 840 msdosfs_statfs, 841 msdosfs_sync, 842 vfs_stdvget, 843 msdosfs_fhtovp, 844 vfs_stdcheckexp, 845 msdosfs_vptofh, 846 msdosfs_init, 847 msdosfs_uninit, 848 vfs_stdextattrctl, 849 }; 850 851 VFS_SET(msdosfs_vfsops, msdosfs, 0); 852