1 /* $FreeBSD$ */ 2 /* $NetBSD: msdosfs_vfsops.c,v 1.51 1997/11/17 15:36:58 ws Exp $ */ 3 4 /*- 5 * SPDX-License-Identifier: BSD-4-Clause 6 * 7 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank. 8 * Copyright (C) 1994, 1995, 1997 TooLs GmbH. 9 * All rights reserved. 10 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below). 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by TooLs GmbH. 23 * 4. The name of TooLs GmbH may not be used to endorse or promote products 24 * derived from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 29 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 31 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 32 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 33 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 34 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36 */ 37 /*- 38 * Written by Paul Popelka (paulp@uts.amdahl.com) 39 * 40 * You can do anything you want with this software, just don't say you wrote 41 * it, and don't remove this notice. 42 * 43 * This software is provided "as is". 44 * 45 * The author supplies this software to be publicly redistributed on the 46 * understanding that the author is not responsible for the correct 47 * functioning of this software in any circumstances and is not liable for 48 * any damages caused by this software. 49 * 50 * October 1992 51 */ 52 53 #include <sys/param.h> 54 #include <sys/systm.h> 55 #include <sys/conf.h> 56 #include <sys/proc.h> 57 #include <sys/nlookup.h> 58 #include <sys/kernel.h> 59 #include <sys/vnode.h> 60 #include <sys/iconv.h> 61 #include <sys/mount.h> 62 #include <sys/buf.h> 63 #include <sys/fcntl.h> 64 #include <sys/malloc.h> 65 #include <sys/stat.h> /* defines ALLPERMS */ 66 #include <vm/vm_zone.h> 67 68 #include <sys/buf2.h> 69 70 #include "bpb.h" 71 #include "bootsect.h" 72 #include "direntry.h" 73 #include "denode.h" 74 #include "msdosfsmount.h" 75 #include "fat.h" 76 77 extern struct vop_ops msdosfs_vnode_vops; 78 struct iconv_functions *msdosfs_iconv; 79 80 #define MSDOSFS_DFLTBSIZE 4096 81 #define ENCODING_UNICODE "UTF-16BE" 82 #if 1 /*def PC98*/ 83 /* 84 * XXX - The boot signature formatted by NEC PC-98 DOS looks like a 85 * garbage or a random value :-{ 86 * If you want to use that broken-signatured media, define the 87 * following symbol even though PC/AT. 88 * (ex. mount PC-98 DOS formatted FD on PC/AT) 89 */ 90 #define MSDOSFS_NOCHECKSIG 91 #endif 92 93 MALLOC_DEFINE(M_MSDOSFSMNT, "MSDOSFS mount", "MSDOSFS mount structure"); 94 static MALLOC_DEFINE(M_MSDOSFSFAT, "MSDOSFS FAT", "MSDOSFS file allocation table"); 95 96 static int mountmsdosfs(struct vnode *devvp, struct mount *mp, 97 struct msdosfs_args *argp); 98 static int msdosfs_root(struct mount *, struct vnode **); 99 static int msdosfs_statfs(struct mount *, struct statfs *, struct ucred *); 100 static int msdosfs_unmount(struct mount *, int); 101 102 static int 103 update_mp(struct mount *mp, struct msdosfs_args *argp) 104 { 105 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 106 int error; 107 char cs_local[ICONV_CSNMAXLEN]; 108 char cs_dos[ICONV_CSNMAXLEN]; 109 110 pmp->pm_gid = argp->gid; 111 pmp->pm_uid = argp->uid; 112 pmp->pm_mask = argp->mask & ALLPERMS; 113 pmp->pm_dirmask = argp->dirmask & ALLPERMS; 114 pmp->pm_flags |= argp->flags & MSDOSFSMNT_MNTOPT; 115 116 if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) { 117 memcpy(cs_local, argp->cs_local, sizeof(cs_local)); 118 memcpy(cs_dos, argp->cs_dos, sizeof(cs_dos)); 119 kprintf("local: %s dos: %s\n",argp->cs_local, argp->cs_dos); 120 error = msdosfs_iconv->open(cs_local, ENCODING_UNICODE, 121 &pmp->pm_w2u); 122 if(error) 123 return error; 124 error = msdosfs_iconv->open(ENCODING_UNICODE, cs_local, 125 &pmp->pm_u2w); 126 if(error) 127 return error; 128 error = msdosfs_iconv->open(cs_dos, cs_local, &pmp->pm_u2d); 129 if(error) 130 return error; 131 error = msdosfs_iconv->open(cs_local, cs_dos, &pmp->pm_d2u); 132 if(error) 133 return error; 134 } 135 136 if (pmp->pm_flags & MSDOSFSMNT_NOWIN95) 137 pmp->pm_flags |= MSDOSFSMNT_SHORTNAME; 138 else 139 pmp->pm_flags |= MSDOSFSMNT_LONGNAME; 140 return 0; 141 } 142 143 /* 144 * mp - path - addr in user space of mount point (ie /usr or whatever) 145 * data - addr in user space of mount params including the name of the block 146 * special file to treat as a filesystem. 147 */ 148 static int 149 msdosfs_mount(struct mount *mp, char *path, caddr_t data, struct ucred *cred) 150 { 151 struct vnode *devvp; /* vnode for blk device to mount */ 152 struct msdosfs_args args; /* will hold data from mount request */ 153 /* msdosfs specific mount control block */ 154 struct msdosfsmount *pmp = NULL; 155 size_t size; 156 int error, flags; 157 mode_t accessmode; 158 struct nlookupdata nd; 159 160 error = copyin(data, (caddr_t)&args, sizeof(struct msdosfs_args)); 161 if (error) 162 return (error); 163 /* 164 * If updating, check whether changing from read-only to 165 * read/write; if there is no device name, that's all we do. 166 */ 167 if (mp->mnt_flag & MNT_UPDATE) { 168 pmp = VFSTOMSDOSFS(mp); 169 error = 0; 170 if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) && 171 (mp->mnt_flag & MNT_RDONLY)) { 172 flags = WRITECLOSE; 173 if (mp->mnt_flag & MNT_FORCE) 174 flags |= FORCECLOSE; 175 error = vflush(mp, 0, flags); 176 if (error == 0) { 177 devvp = pmp->pm_devvp; 178 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 179 VOP_OPEN(devvp, FREAD, FSCRED, NULL); 180 VOP_CLOSE(devvp, FREAD|FWRITE, NULL); 181 vn_unlock(devvp); 182 pmp->pm_flags |= MSDOSFSMNT_RONLY; 183 } 184 } 185 if (!error && (mp->mnt_flag & MNT_RELOAD)) 186 error = EOPNOTSUPP; /* not yet implemented */ 187 if (error) 188 return (error); 189 if ((pmp->pm_flags & MSDOSFSMNT_RONLY) && 190 (mp->mnt_kern_flag & MNTK_WANTRDWR)) { 191 /* 192 * If upgrade to read-write by non-root, then verify 193 * that user has necessary permissions on the device. 194 */ 195 devvp = pmp->pm_devvp; 196 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 197 if (cred->cr_uid != 0) { 198 error = VOP_EACCESS(devvp, VREAD | VWRITE, 199 cred); 200 if (error) { 201 vn_unlock(devvp); 202 return (error); 203 } 204 } 205 VOP_OPEN(devvp, FREAD|FWRITE, FSCRED, NULL); 206 VOP_CLOSE(devvp, FREAD, NULL); 207 vn_unlock(devvp); 208 pmp->pm_flags &= ~MSDOSFSMNT_RONLY; 209 } 210 if (args.fspec == NULL) { 211 #ifdef __notyet__ /* doesn't work correctly with current mountd XXX */ 212 if (args.flags & MSDOSFSMNT_MNTOPT) { 213 pmp->pm_flags &= ~MSDOSFSMNT_MNTOPT; 214 pmp->pm_flags |= args.flags & MSDOSFSMNT_MNTOPT; 215 if (pmp->pm_flags & MSDOSFSMNT_NOWIN95) 216 pmp->pm_flags |= MSDOSFSMNT_SHORTNAME; 217 } 218 #endif 219 /* 220 * Process export requests. 221 */ 222 return (vfs_export(mp, &pmp->pm_export, &args.export)); 223 } 224 } 225 /* 226 * Not an update, or updating the name: look up the name 227 * and verify that it refers to a sensible disk device. 228 */ 229 devvp = NULL; 230 error = nlookup_init(&nd, args.fspec, UIO_USERSPACE, NLC_FOLLOW); 231 if (error == 0) 232 error = nlookup(&nd); 233 if (error == 0) 234 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp); 235 nlookup_done(&nd); 236 if (error) 237 return (error); 238 239 if (!vn_isdisk(devvp, &error)) { 240 vrele(devvp); 241 return (error); 242 } 243 /* 244 * If mount by non-root, then verify that user has necessary 245 * permissions on the device. 246 */ 247 if (cred->cr_uid != 0) { 248 accessmode = VREAD; 249 if ((mp->mnt_flag & MNT_RDONLY) == 0) 250 accessmode |= VWRITE; 251 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 252 error = VOP_EACCESS(devvp, accessmode, cred); 253 if (error) { 254 vput(devvp); 255 return (error); 256 } 257 vn_unlock(devvp); 258 } 259 if ((mp->mnt_flag & MNT_UPDATE) == 0) { 260 error = mountmsdosfs(devvp, mp, &args); 261 #ifdef MSDOSFS_DEBUG /* only needed for the kprintf below */ 262 pmp = VFSTOMSDOSFS(mp); 263 #endif 264 } else { 265 if (devvp != pmp->pm_devvp) 266 error = EINVAL; /* XXX needs translation */ 267 else 268 vrele(devvp); 269 } 270 if (error) { 271 vrele(devvp); 272 return (error); 273 } 274 275 error = update_mp(mp, &args); 276 if (error) { 277 msdosfs_unmount(mp, MNT_FORCE); 278 return error; 279 } 280 281 copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size); 282 memset(mp->mnt_stat.f_mntfromname + size, 0, MNAMELEN - size); 283 msdosfs_statfs(mp, &mp->mnt_stat, cred); 284 #ifdef MSDOSFS_DEBUG 285 kprintf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", 286 mp, pmp, pmp->pm_inusemap); 287 #endif 288 return (0); 289 } 290 291 static int 292 mountmsdosfs(struct vnode *devvp, struct mount *mp, struct msdosfs_args *argp) 293 { 294 struct msdosfsmount *pmp; 295 struct buf *bp; 296 cdev_t dev; 297 union bootsector *bsp; 298 struct byte_bpb33 *b33; 299 struct byte_bpb50 *b50; 300 struct byte_bpb710 *b710; 301 uint8_t SecPerClust; 302 u_long clusters; 303 int ronly, error; 304 305 /* 306 * Disallow multiple mounts of the same device. 307 * Disallow mounting of a device that is currently in use 308 * Flush out any old buffers remaining from a previous use. 309 */ 310 error = vfs_mountedon(devvp); 311 if (error) 312 return (error); 313 if (vcount(devvp) > 0) 314 return (EBUSY); 315 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 316 error = vinvalbuf(devvp, V_SAVE, 0, 0); 317 vn_unlock(devvp); 318 if (error) 319 return (error); 320 321 ronly = (mp->mnt_flag & MNT_RDONLY) != 0; 322 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 323 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, NULL); 324 vn_unlock(devvp); 325 if (error) 326 return (error); 327 dev = devvp->v_rdev; 328 bp = NULL; /* both used in error_exit */ 329 pmp = NULL; 330 331 /* 332 * Read the boot sector of the filesystem, and then check the 333 * boot signature. If not a dos boot sector then error out. 334 * 335 * NOTE: 2048 is a maximum sector size in current... 336 */ 337 error = bread(devvp, 0, 2048, &bp); 338 if (error) 339 goto error_exit; 340 bp->b_flags |= B_AGE; 341 bsp = (union bootsector *)bp->b_data; 342 b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB; 343 b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB; 344 b710 = (struct byte_bpb710 *)bsp->bs710.bsBPB; 345 346 #ifndef MSDOSFS_NOCHECKSIG 347 if (bsp->bs50.bsBootSectSig0 != BOOTSIG0 348 || bsp->bs50.bsBootSectSig1 != BOOTSIG1) { 349 error = EINVAL; 350 goto error_exit; 351 } 352 #endif 353 354 pmp = kmalloc(sizeof(*pmp), M_MSDOSFSMNT, M_WAITOK | M_ZERO); 355 pmp->pm_mountp = mp; 356 357 /* 358 * Compute several useful quantities from the bpb in the 359 * bootsector. Copy in the dos 5 variant of the bpb then fix up 360 * the fields that are different between dos 5 and dos 3.3. 361 */ 362 SecPerClust = b50->bpbSecPerClust; 363 pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec); 364 if (pmp->pm_BytesPerSec < DEV_BSIZE) { 365 error = EINVAL; 366 goto error_exit; 367 } 368 pmp->pm_ResSectors = getushort(b50->bpbResSectors); 369 pmp->pm_FATs = b50->bpbFATs; 370 pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts); 371 pmp->pm_Sectors = getushort(b50->bpbSectors); 372 pmp->pm_FATsecs = getushort(b50->bpbFATsecs); 373 pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack); 374 pmp->pm_Heads = getushort(b50->bpbHeads); 375 pmp->pm_Media = b50->bpbMedia; 376 377 /* calculate the ratio of sector size to DEV_BSIZE */ 378 pmp->pm_BlkPerSec = pmp->pm_BytesPerSec / DEV_BSIZE; 379 380 /* 381 * We don't check pm_Heads nor pm_SecPerTrack, because 382 * these may not be set for EFI file systems. We don't 383 * use these anyway, so we're unaffected if they are 384 * invalid. 385 */ 386 if (!pmp->pm_BytesPerSec || !SecPerClust) { 387 error = EINVAL; 388 goto error_exit; 389 } 390 391 if (pmp->pm_Sectors == 0) { 392 pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs); 393 pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors); 394 } else { 395 pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs); 396 pmp->pm_HugeSectors = pmp->pm_Sectors; 397 } 398 399 if (pmp->pm_RootDirEnts == 0) { 400 if (pmp->pm_Sectors 401 || pmp->pm_FATsecs 402 || getushort(b710->bpbFSVers)) { 403 error = EINVAL; 404 kprintf("mountmsdosfs(): bad FAT32 filesystem\n"); 405 goto error_exit; 406 } 407 pmp->pm_fatmask = FAT32_MASK; 408 pmp->pm_fatmult = 4; 409 pmp->pm_fatdiv = 1; 410 pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs); 411 if (getushort(b710->bpbExtFlags) & FATMIRROR) 412 pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM; 413 else 414 pmp->pm_flags |= MSDOSFS_FATMIRROR; 415 } else 416 pmp->pm_flags |= MSDOSFS_FATMIRROR; 417 418 /* 419 * Check a few values (could do some more): 420 * - logical sector size: power of 2, >= block size 421 * - sectors per cluster: power of 2, >= 1 422 * - number of sectors: >= 1, <= size of partition 423 * - number of FAT sectors: >= 1 424 */ 425 if ( (SecPerClust == 0) 426 || (SecPerClust & (SecPerClust - 1)) 427 || (pmp->pm_BytesPerSec < DEV_BSIZE) 428 || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1)) 429 || (pmp->pm_HugeSectors == 0) 430 || (pmp->pm_FATsecs == 0) 431 || (SecPerClust * pmp->pm_BlkPerSec > MAXBSIZE / DEV_BSIZE)) { 432 error = EINVAL; 433 goto error_exit; 434 } 435 436 pmp->pm_HugeSectors *= pmp->pm_BlkPerSec; 437 pmp->pm_HiddenSects *= pmp->pm_BlkPerSec; /* XXX not used? */ 438 pmp->pm_FATsecs *= pmp->pm_BlkPerSec; 439 SecPerClust *= pmp->pm_BlkPerSec; 440 441 pmp->pm_fatblk = pmp->pm_ResSectors * pmp->pm_BlkPerSec; 442 443 if (FAT32(pmp)) { 444 pmp->pm_rootdirblk = getulong(b710->bpbRootClust); 445 pmp->pm_firstcluster = pmp->pm_fatblk 446 + (pmp->pm_FATs * pmp->pm_FATsecs); 447 pmp->pm_fsinfo = getushort(b710->bpbFSInfo) * pmp->pm_BlkPerSec; 448 } else { 449 pmp->pm_rootdirblk = pmp->pm_fatblk + 450 (pmp->pm_FATs * pmp->pm_FATsecs); 451 pmp->pm_rootdirsize = howmany(pmp->pm_RootDirEnts * 452 sizeof(struct direntry), DEV_BSIZE); /* in blocks */ 453 pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize; 454 } 455 456 pmp->pm_maxcluster = (pmp->pm_HugeSectors - pmp->pm_firstcluster) / 457 SecPerClust + 1; 458 459 if (pmp->pm_fatmask == 0) { 460 if (pmp->pm_maxcluster 461 <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) { 462 /* 463 * This will usually be a floppy disk. This size makes 464 * sure that one FAT entry will not be split across 465 * multiple blocks. 466 */ 467 pmp->pm_fatmask = FAT12_MASK; 468 pmp->pm_fatmult = 3; 469 pmp->pm_fatdiv = 2; 470 } else { 471 pmp->pm_fatmask = FAT16_MASK; 472 pmp->pm_fatmult = 2; 473 pmp->pm_fatdiv = 1; 474 } 475 } 476 477 clusters = ((pmp->pm_FATsecs * DEV_BSIZE) / pmp->pm_fatmult) * 478 pmp->pm_fatdiv; 479 if (pmp->pm_maxcluster >= clusters) { 480 kprintf("Warning: number of clusters (%ld) exceeds FAT " 481 "capacity (%ld)\n", pmp->pm_maxcluster + 1, clusters); 482 pmp->pm_maxcluster = clusters - 1; 483 } 484 485 if (FAT12(pmp)) 486 pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec; 487 else 488 pmp->pm_fatblocksize = MSDOSFS_DFLTBSIZE; 489 490 pmp->pm_fatblocksec = pmp->pm_fatblocksize / DEV_BSIZE; 491 pmp->pm_bnshift = DEV_BSHIFT; 492 493 /* 494 * Compute mask and shift value for isolating cluster relative byte 495 * offsets and cluster numbers from a file offset. 496 */ 497 pmp->pm_bpcluster = SecPerClust * DEV_BSIZE; 498 pmp->pm_crbomask = pmp->pm_bpcluster - 1; 499 pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1; 500 501 /* 502 * Check for valid cluster size 503 * must be a power of 2 504 */ 505 if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) { 506 error = EINVAL; 507 goto error_exit; 508 } 509 510 /* 511 * Release the bootsector buffer. 512 */ 513 bp->b_flags |= B_RELBUF; 514 brelse(bp); 515 bp = NULL; 516 517 /* 518 * Check the fsinfo sector if we have one. Silently fix up our 519 * in-core copy of fp->fsinxtfree if it is unknown (0xffffffff) 520 * or too large. Ignore fp->fsinfree for now, since we need to 521 * read the entire FAT anyway to fill the inuse map. 522 */ 523 if (pmp->pm_fsinfo) { 524 struct fsinfo *fp; 525 526 if ((error = bread(devvp, de_bn2doff(pmp, pmp->pm_fsinfo), 527 pmp->pm_BytesPerSec, &bp)) != 0) 528 goto error_exit; 529 fp = (struct fsinfo *)bp->b_data; 530 if (!memcmp(fp->fsisig1, "RRaA", 4) && 531 !memcmp(fp->fsisig2, "rrAa", 4) && 532 !memcmp(fp->fsisig3, "\0\0\125\252", 4)) { 533 pmp->pm_nxtfree = getulong(fp->fsinxtfree); 534 if (pmp->pm_nxtfree > pmp->pm_maxcluster) 535 pmp->pm_nxtfree = CLUST_FIRST; 536 } else 537 pmp->pm_fsinfo = 0; 538 bp->b_flags |= B_RELBUF; 539 brelse(bp); 540 bp = NULL; 541 } 542 543 /* 544 * Finish initializing pmp->pm_nxtfree (just in case the first few 545 * sectors aren't properly reserved in the FAT). This completes 546 * the fixup for fp->fsinxtfree, and fixes up the zero-initialized 547 * value if there is no fsinfo. We will use pmp->pm_nxtfree 548 * internally even if there is no fsinfo. 549 */ 550 if (pmp->pm_nxtfree < CLUST_FIRST) 551 pmp->pm_nxtfree = CLUST_FIRST; 552 553 /* 554 * Allocate memory for the bitmap of allocated clusters, and then 555 * fill it in. 556 */ 557 pmp->pm_inusemap = kmalloc(howmany(pmp->pm_maxcluster + 1, N_INUSEBITS) 558 * sizeof(*pmp->pm_inusemap), 559 M_MSDOSFSFAT, M_WAITOK); 560 561 /* 562 * fillinusemap() needs pm_devvp. 563 */ 564 pmp->pm_devvp = devvp; 565 pmp->pm_dev = dev; 566 567 /* 568 * Have the inuse map filled in. 569 */ 570 MSDOSFS_LOCK_MP(pmp); 571 error = fillinusemap(pmp); 572 MSDOSFS_UNLOCK_MP(pmp); 573 if (error != 0) 574 goto error_exit; 575 576 /* 577 * If they want FAT updates to be synchronous then let them suffer 578 * the performance degradation in exchange for the on disk copy of 579 * the FAT being correct just about all the time. I suppose this 580 * would be a good thing to turn on if the kernel is still flakey. 581 */ 582 if (mp->mnt_flag & MNT_SYNCHRONOUS) 583 pmp->pm_flags |= MSDOSFSMNT_WAITONFAT; 584 585 /* 586 * Finish up. 587 */ 588 if (ronly) 589 pmp->pm_flags |= MSDOSFSMNT_RONLY; 590 else 591 pmp->pm_fmod = 1; 592 mp->mnt_data = (qaddr_t) pmp; 593 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev); 594 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum; 595 mp->mnt_flag |= MNT_LOCAL; 596 vfs_add_vnodeops(mp, &msdosfs_vnode_vops, &mp->mnt_vn_norm_ops); 597 dev->si_mountpoint = mp; 598 599 return (0); 600 601 error_exit: 602 if (bp) { 603 bp->b_flags |= B_RELBUF; 604 brelse(bp); 605 } 606 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 607 VOP_CLOSE(devvp, ronly ? FREAD : FREAD | FWRITE, NULL); 608 vn_unlock(devvp); 609 if (pmp) { 610 if (pmp->pm_inusemap) 611 kfree(pmp->pm_inusemap, M_MSDOSFSFAT); 612 kfree(pmp, M_MSDOSFSMNT); 613 mp->mnt_data = (qaddr_t)0; 614 } 615 return (error); 616 } 617 618 /* 619 * Unmount the filesystem described by mp. 620 */ 621 static int 622 msdosfs_unmount(struct mount *mp, int mntflags) 623 { 624 struct msdosfsmount *pmp; 625 int error, flags; 626 #ifdef MSDOSFS_DEBUG 627 struct vnode *vp; 628 #endif 629 flags = 0; 630 if (mntflags & MNT_FORCE) 631 flags |= FORCECLOSE; 632 error = vflush(mp, 0, flags); 633 if (error) 634 return error; 635 636 pmp = VFSTOMSDOSFS(mp); 637 pmp->pm_devvp->v_rdev->si_mountpoint = NULL; 638 639 if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) { 640 if (pmp->pm_w2u) 641 msdosfs_iconv->close(pmp->pm_w2u); 642 if (pmp->pm_u2w) 643 msdosfs_iconv->close(pmp->pm_u2w); 644 if (pmp->pm_d2u) 645 msdosfs_iconv->close(pmp->pm_d2u); 646 if (pmp->pm_u2d) 647 msdosfs_iconv->close(pmp->pm_u2d); 648 } 649 650 #ifdef MSDOSFS_DEBUG 651 vp = pmp->pm_devvp; 652 kprintf("msdosfs_umount(): just before calling VOP_CLOSE()\n"); 653 kprintf("flag %08x, refcnt 0x%08x, writecount %d, auxrefs 0x%08x\n", 654 vp->v_flag, vp->v_refcnt, vp->v_writecount, vp->v_auxrefs); 655 kprintf("mount %p, op %p\n", vp->v_mount, vp->v_ops); 656 kprintf("mount %p\n", vp->v_mount); 657 kprintf("cleanblkhd %p, dirtyblkhd %p, numoutput %d, type %d\n", 658 RB_ROOT(&vp->v_rbclean_tree), RB_ROOT(&vp->v_rbdirty_tree), 659 bio_track_active(&vp->v_track_write), vp->v_type); 660 kprintf("union %p, tag %d, data[0] %08x, data[1] %08x\n", 661 vp->v_socket, vp->v_tag, ((u_int *)vp->v_data)[0], 662 ((u_int *)vp->v_data)[1]); 663 #endif 664 665 vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY); 666 error = VOP_CLOSE(pmp->pm_devvp, 667 pmp->pm_flags & MSDOSFSMNT_RONLY ? FREAD : FREAD | FWRITE, 668 NULL); 669 vn_unlock(pmp->pm_devvp); 670 vrele(pmp->pm_devvp); 671 kfree(pmp->pm_inusemap, M_MSDOSFSFAT); 672 kfree(pmp, M_MSDOSFSMNT); 673 mp->mnt_data = (qaddr_t)0; 674 mp->mnt_flag &= ~MNT_LOCAL; 675 return (error); 676 } 677 678 static int 679 msdosfs_root(struct mount *mp, struct vnode **vpp) 680 { 681 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 682 struct denode *ndep; 683 int error; 684 685 mprintf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp); 686 error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep); 687 if (error) 688 return (error); 689 *vpp = DETOV(ndep); 690 return (0); 691 } 692 693 static int 694 msdosfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred) 695 { 696 struct msdosfsmount *pmp; 697 698 pmp = VFSTOMSDOSFS(mp); 699 sbp->f_bsize = pmp->pm_bpcluster; 700 sbp->f_iosize = pmp->pm_bpcluster; 701 sbp->f_blocks = pmp->pm_maxcluster + 1; 702 sbp->f_bfree = pmp->pm_freeclustercount; 703 sbp->f_bavail = pmp->pm_freeclustercount; 704 sbp->f_files = pmp->pm_RootDirEnts; /* XXX */ 705 sbp->f_ffree = 0; /* what to put in here? */ 706 if (sbp != &mp->mnt_stat) { 707 sbp->f_type = mp->mnt_vfc->vfc_typenum; 708 memcpy(sbp->f_mntfromname, mp->mnt_stat.f_mntfromname, MNAMELEN); 709 } 710 strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN); 711 return (0); 712 } 713 714 struct scaninfo { 715 int rescan; 716 int allerror; 717 int waitfor; 718 }; 719 720 static int msdosfs_sync_scan(struct mount *mp, struct vnode *vp, void *data); 721 722 /* 723 * If we have an FSInfo block, update it. 724 */ 725 static int 726 msdosfs_fsiflush(struct msdosfsmount *pmp, int waitfor) 727 { 728 struct fsinfo *fp; 729 struct buf *bp; 730 int error; 731 732 MSDOSFS_LOCK_MP(pmp); 733 if (pmp->pm_fsinfo == 0 || (pmp->pm_flags & MSDOSFS_FSIMOD) == 0) { 734 error = 0; 735 goto unlock; 736 } 737 error = bread(pmp->pm_devvp, de_bn2doff(pmp, pmp->pm_fsinfo), 738 pmp->pm_BytesPerSec, &bp); 739 if (error != 0) { 740 brelse(bp); 741 goto unlock; 742 } 743 744 fp = (struct fsinfo *)bp->b_data; 745 putulong(fp->fsinfree, pmp->pm_freeclustercount); 746 putulong(fp->fsinxtfree, pmp->pm_nxtfree); 747 pmp->pm_flags &= ~MSDOSFS_FSIMOD; 748 if (waitfor == MNT_WAIT) 749 error = bwrite(bp); 750 else 751 bawrite(bp); 752 unlock: 753 MSDOSFS_UNLOCK_MP(pmp); 754 return (error); 755 } 756 757 static int 758 msdosfs_sync(struct mount *mp, int waitfor) 759 { 760 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 761 struct scaninfo scaninfo; 762 int error; 763 764 /* 765 * If we ever switch to not updating all of the FATs all the time, 766 * this would be the place to update them from the first one. 767 */ 768 if (pmp->pm_fmod != 0) { 769 if (pmp->pm_flags & MSDOSFSMNT_RONLY) { 770 panic("msdosfs_sync: rofs mod"); 771 } else { 772 /* update FATs here */ 773 } 774 } 775 /* 776 * Write back each (modified) denode. 777 */ 778 scaninfo.allerror = 0; 779 scaninfo.rescan = 1; 780 while (scaninfo.rescan) { 781 scaninfo.rescan = 0; 782 vmntvnodescan(mp, VMSC_GETVP|VMSC_NOWAIT, NULL, 783 msdosfs_sync_scan, &scaninfo); 784 } 785 786 /* 787 * Flush filesystem control info. 788 */ 789 if ((waitfor & MNT_LAZY) == 0) { 790 vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY); 791 if ((error = VOP_FSYNC(pmp->pm_devvp, waitfor, 0)) != 0) 792 scaninfo.allerror = error; 793 vn_unlock(pmp->pm_devvp); 794 } 795 796 error = msdosfs_fsiflush(pmp, waitfor); 797 if (error != 0) 798 scaninfo.allerror = error; 799 return (scaninfo.allerror); 800 } 801 802 static int 803 msdosfs_sync_scan(struct mount *mp, struct vnode *vp, void *data) 804 { 805 struct scaninfo *info = data; 806 struct denode *dep; 807 int error; 808 809 dep = VTODE(vp); 810 if (vp->v_type == VNON || vp->v_type == VBAD || 811 ((dep->de_flag & 812 (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 && 813 (RB_EMPTY(&vp->v_rbdirty_tree) || (info->waitfor & MNT_LAZY)))) { 814 return(0); 815 } 816 if ((error = VOP_FSYNC(vp, info->waitfor, 0)) != 0) 817 info->allerror = error; 818 return(0); 819 } 820 821 static int 822 msdosfs_fhtovp(struct mount *mp, struct vnode *rootvp, 823 struct fid *fhp, struct vnode **vpp) 824 { 825 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 826 struct defid *defhp = (struct defid *) fhp; 827 struct denode *dep; 828 int error; 829 830 error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep); 831 if (error) { 832 *vpp = NULLVP; 833 return (error); 834 } 835 *vpp = DETOV(dep); 836 return (0); 837 } 838 839 static int 840 msdosfs_checkexp(struct mount *mp, struct sockaddr *nam, int *exflagsp, 841 struct ucred **credanonp) 842 { 843 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 844 struct netcred *np; 845 846 np = vfs_export_lookup(mp, &pmp->pm_export, nam); 847 if (np == NULL) 848 return (EACCES); 849 *exflagsp = np->netc_exflags; 850 *credanonp = &np->netc_anon; 851 return (0); 852 } 853 854 static int 855 msdosfs_vptofh(struct vnode *vp, struct fid *fhp) 856 { 857 struct denode *dep; 858 struct defid *defhp; 859 860 dep = VTODE(vp); 861 defhp = (struct defid *)fhp; 862 defhp->defid_len = sizeof(struct defid); 863 defhp->defid_dirclust = dep->de_dirclust; 864 defhp->defid_dirofs = dep->de_diroffset; 865 /* defhp->defid_gen = dep->de_gen; */ 866 return (0); 867 } 868 869 static struct vfsops msdosfs_vfsops = { 870 .vfs_mount = msdosfs_mount, 871 .vfs_unmount = msdosfs_unmount, 872 .vfs_root = msdosfs_root, 873 .vfs_statfs = msdosfs_statfs, 874 .vfs_sync = msdosfs_sync, 875 .vfs_fhtovp = msdosfs_fhtovp, 876 .vfs_checkexp = msdosfs_checkexp, 877 .vfs_vptofh = msdosfs_vptofh, 878 .vfs_init = msdosfs_init, 879 .vfs_uninit = msdosfs_uninit 880 }; 881 882 VFS_SET(msdosfs_vfsops, msdos, 0); 883 MODULE_VERSION(msdos, 1); 884