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/buf.h> 54 #include <sys/conf.h> 55 #include <sys/fcntl.h> 56 #include <sys/iconv.h> 57 #include <sys/kernel.h> 58 #include <sys/lock.h> 59 #include <sys/malloc.h> 60 #include <sys/mount.h> 61 #include <sys/mutex.h> 62 #include <sys/namei.h> 63 #include <sys/priv.h> 64 #include <sys/proc.h> 65 #include <sys/stat.h> 66 #include <sys/vnode.h> 67 68 #include <geom/geom.h> 69 #include <geom/geom_vfs.h> 70 71 #include <fs/msdosfs/bootsect.h> 72 #include <fs/msdosfs/bpb.h> 73 #include <fs/msdosfs/direntry.h> 74 #include <fs/msdosfs/denode.h> 75 #include <fs/msdosfs/fat.h> 76 #include <fs/msdosfs/msdosfsmount.h> 77 78 /* Mount options that we support. */ 79 static const char *msdosfs_opts[] = { 80 "async", "noatime", "noclusterr", "noclusterw", 81 "export", "force", "from", "sync", 82 "cs_dos", "cs_local", "cs_win", "dirmask", 83 "gid", "kiconv", "large", "longname", 84 "longnames", "mask", "shortname", "shortnames", 85 "uid", "win95", "nowin95", 86 NULL 87 }; 88 89 #if 1 /*def PC98*/ 90 /* 91 * XXX - The boot signature formatted by NEC PC-98 DOS looks like a 92 * garbage or a random value :-{ 93 * If you want to use that broken-signatured media, define the 94 * following symbol even though PC/AT. 95 * (ex. mount PC-98 DOS formatted FD on PC/AT) 96 */ 97 #define MSDOSFS_NOCHECKSIG 98 #endif 99 100 MALLOC_DEFINE(M_MSDOSFSMNT, "msdosfs_mount", "MSDOSFS mount structure"); 101 static MALLOC_DEFINE(M_MSDOSFSFAT, "msdosfs_fat", "MSDOSFS file allocation table"); 102 103 struct iconv_functions *msdosfs_iconv; 104 105 static int update_mp(struct mount *mp, struct thread *td); 106 static int mountmsdosfs(struct vnode *devvp, struct mount *mp); 107 static vfs_fhtovp_t msdosfs_fhtovp; 108 static vfs_mount_t msdosfs_mount; 109 static vfs_root_t msdosfs_root; 110 static vfs_statfs_t msdosfs_statfs; 111 static vfs_sync_t msdosfs_sync; 112 static vfs_unmount_t msdosfs_unmount; 113 114 /* Maximum length of a character set name (arbitrary). */ 115 #define MAXCSLEN 64 116 117 static int 118 update_mp(struct mount *mp, struct thread *td) 119 { 120 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 121 void *dos, *win, *local; 122 int error, v; 123 124 if (!vfs_getopt(mp->mnt_optnew, "kiconv", NULL, NULL)) { 125 if (msdosfs_iconv != NULL) { 126 error = vfs_getopt(mp->mnt_optnew, 127 "cs_win", &win, NULL); 128 if (!error) 129 error = vfs_getopt(mp->mnt_optnew, 130 "cs_local", &local, NULL); 131 if (!error) 132 error = vfs_getopt(mp->mnt_optnew, 133 "cs_dos", &dos, NULL); 134 if (!error) { 135 msdosfs_iconv->open(win, local, &pmp->pm_u2w); 136 msdosfs_iconv->open(local, win, &pmp->pm_w2u); 137 msdosfs_iconv->open(dos, local, &pmp->pm_u2d); 138 msdosfs_iconv->open(local, dos, &pmp->pm_d2u); 139 } 140 if (error != 0) 141 return (error); 142 } else { 143 pmp->pm_w2u = NULL; 144 pmp->pm_u2w = NULL; 145 pmp->pm_d2u = NULL; 146 pmp->pm_u2d = NULL; 147 } 148 } 149 150 if (1 == vfs_scanopt(mp->mnt_optnew, "gid", "%d", &v)) 151 pmp->pm_gid = v; 152 if (1 == vfs_scanopt(mp->mnt_optnew, "uid", "%d", &v)) 153 pmp->pm_uid = v; 154 if (1 == vfs_scanopt(mp->mnt_optnew, "mask", "%d", &v)) 155 pmp->pm_mask = v & ALLPERMS; 156 if (1 == vfs_scanopt(mp->mnt_optnew, "dirmask", "%d", &v)) 157 pmp->pm_dirmask = v & ALLPERMS; 158 vfs_flagopt(mp->mnt_optnew, "shortname", 159 &pmp->pm_flags, MSDOSFSMNT_SHORTNAME); 160 vfs_flagopt(mp->mnt_optnew, "shortnames", 161 &pmp->pm_flags, MSDOSFSMNT_SHORTNAME); 162 vfs_flagopt(mp->mnt_optnew, "longname", 163 &pmp->pm_flags, MSDOSFSMNT_LONGNAME); 164 vfs_flagopt(mp->mnt_optnew, "longnames", 165 &pmp->pm_flags, MSDOSFSMNT_LONGNAME); 166 vfs_flagopt(mp->mnt_optnew, "kiconv", 167 &pmp->pm_flags, MSDOSFSMNT_KICONV); 168 169 if (vfs_getopt(mp->mnt_optnew, "nowin95", NULL, NULL) == 0) 170 pmp->pm_flags |= MSDOSFSMNT_NOWIN95; 171 else 172 pmp->pm_flags &= ~MSDOSFSMNT_NOWIN95; 173 174 if (pmp->pm_flags & MSDOSFSMNT_NOWIN95) 175 pmp->pm_flags |= MSDOSFSMNT_SHORTNAME; 176 else if (!(pmp->pm_flags & 177 (MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) { 178 struct vnode *rootvp; 179 180 /* 181 * Try to divine whether to support Win'95 long filenames 182 */ 183 if (FAT32(pmp)) 184 pmp->pm_flags |= MSDOSFSMNT_LONGNAME; 185 else { 186 if ((error = 187 msdosfs_root(mp, LK_EXCLUSIVE, &rootvp, td)) != 0) 188 return error; 189 pmp->pm_flags |= findwin95(VTODE(rootvp)) ? 190 MSDOSFSMNT_LONGNAME : MSDOSFSMNT_SHORTNAME; 191 vput(rootvp); 192 } 193 } 194 return 0; 195 } 196 197 static int 198 msdosfs_cmount(struct mntarg *ma, void *data, int flags, struct thread *td) 199 { 200 struct msdosfs_args args; 201 int error; 202 203 if (data == NULL) 204 return (EINVAL); 205 error = copyin(data, &args, sizeof args); 206 if (error) 207 return (error); 208 209 ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN); 210 ma = mount_arg(ma, "export", &args.export, sizeof args.export); 211 ma = mount_argf(ma, "uid", "%d", args.uid); 212 ma = mount_argf(ma, "gid", "%d", args.gid); 213 ma = mount_argf(ma, "mask", "%d", args.mask); 214 ma = mount_argf(ma, "dirmask", "%d", args.dirmask); 215 216 ma = mount_argb(ma, args.flags & MSDOSFSMNT_SHORTNAME, "noshortname"); 217 ma = mount_argb(ma, args.flags & MSDOSFSMNT_LONGNAME, "nolongname"); 218 ma = mount_argb(ma, !(args.flags & MSDOSFSMNT_NOWIN95), "nowin95"); 219 ma = mount_argb(ma, args.flags & MSDOSFSMNT_KICONV, "nokiconv"); 220 221 ma = mount_argsu(ma, "cs_win", args.cs_win, MAXCSLEN); 222 ma = mount_argsu(ma, "cs_dos", args.cs_dos, MAXCSLEN); 223 ma = mount_argsu(ma, "cs_local", args.cs_local, MAXCSLEN); 224 225 error = kernel_mount(ma, flags); 226 227 return (error); 228 } 229 230 /* 231 * mp - path - addr in user space of mount point (ie /usr or whatever) 232 * data - addr in user space of mount params including the name of the block 233 * special file to treat as a filesystem. 234 */ 235 static int 236 msdosfs_mount(struct mount *mp, struct thread *td) 237 { 238 struct vnode *devvp; /* vnode for blk device to mount */ 239 /* msdosfs specific mount control block */ 240 struct msdosfsmount *pmp = NULL; 241 struct nameidata ndp; 242 int error, flags; 243 accmode_t accmode; 244 char *from; 245 246 if (vfs_filteropt(mp->mnt_optnew, msdosfs_opts)) 247 return (EINVAL); 248 249 /* 250 * If updating, check whether changing from read-only to 251 * read/write; if there is no device name, that's all we do. 252 */ 253 if (mp->mnt_flag & MNT_UPDATE) { 254 pmp = VFSTOMSDOSFS(mp); 255 if (vfs_flagopt(mp->mnt_optnew, "export", NULL, 0)) { 256 /* 257 * Forbid export requests if filesystem has 258 * MSDOSFS_LARGEFS flag set. 259 */ 260 if ((pmp->pm_flags & MSDOSFS_LARGEFS) != 0) { 261 vfs_mount_error(mp, 262 "MSDOSFS_LARGEFS flag set, cannot export"); 263 return (EOPNOTSUPP); 264 } 265 } 266 if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) && 267 vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) { 268 error = VFS_SYNC(mp, MNT_WAIT, td); 269 if (error) 270 return (error); 271 flags = WRITECLOSE; 272 if (mp->mnt_flag & MNT_FORCE) 273 flags |= FORCECLOSE; 274 error = vflush(mp, 0, flags, td); 275 if (error) 276 return (error); 277 278 /* 279 * Now the volume is clean. Mark it so while the 280 * device is still rw. 281 */ 282 error = markvoldirty(pmp, 0); 283 if (error) { 284 (void)markvoldirty(pmp, 1); 285 return (error); 286 } 287 288 /* Downgrade the device from rw to ro. */ 289 DROP_GIANT(); 290 g_topology_lock(); 291 error = g_access(pmp->pm_cp, 0, -1, 0); 292 g_topology_unlock(); 293 PICKUP_GIANT(); 294 if (error) { 295 (void)markvoldirty(pmp, 1); 296 return (error); 297 } 298 299 /* 300 * Backing out after an error was painful in the 301 * above. Now we are committed to succeeding. 302 */ 303 pmp->pm_fmod = 0; 304 pmp->pm_flags |= MSDOSFSMNT_RONLY; 305 MNT_ILOCK(mp); 306 mp->mnt_flag |= MNT_RDONLY; 307 MNT_IUNLOCK(mp); 308 } else if ((pmp->pm_flags & MSDOSFSMNT_RONLY) && 309 !vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) { 310 /* 311 * If upgrade to read-write by non-root, then verify 312 * that user has necessary permissions on the device. 313 */ 314 devvp = pmp->pm_devvp; 315 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 316 error = VOP_ACCESS(devvp, VREAD | VWRITE, 317 td->td_ucred, td); 318 if (error) 319 error = priv_check(td, PRIV_VFS_MOUNT_PERM); 320 if (error) { 321 VOP_UNLOCK(devvp, 0); 322 return (error); 323 } 324 VOP_UNLOCK(devvp, 0); 325 DROP_GIANT(); 326 g_topology_lock(); 327 error = g_access(pmp->pm_cp, 0, 1, 0); 328 g_topology_unlock(); 329 PICKUP_GIANT(); 330 if (error) 331 return (error); 332 333 pmp->pm_fmod = 1; 334 pmp->pm_flags &= ~MSDOSFSMNT_RONLY; 335 MNT_ILOCK(mp); 336 mp->mnt_flag &= ~MNT_RDONLY; 337 MNT_IUNLOCK(mp); 338 339 /* Now that the volume is modifiable, mark it dirty. */ 340 error = markvoldirty(pmp, 1); 341 if (error) 342 return (error); 343 } 344 } 345 /* 346 * Not an update, or updating the name: look up the name 347 * and verify that it refers to a sensible disk device. 348 */ 349 if (vfs_getopt(mp->mnt_optnew, "from", (void **)&from, NULL)) 350 return (EINVAL); 351 NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, from, td); 352 error = namei(&ndp); 353 if (error) 354 return (error); 355 devvp = ndp.ni_vp; 356 NDFREE(&ndp, NDF_ONLY_PNBUF); 357 358 if (!vn_isdisk(devvp, &error)) { 359 vput(devvp); 360 return (error); 361 } 362 /* 363 * If mount by non-root, then verify that user has necessary 364 * permissions on the device. 365 */ 366 accmode = VREAD; 367 if ((mp->mnt_flag & MNT_RDONLY) == 0) 368 accmode |= VWRITE; 369 error = VOP_ACCESS(devvp, accmode, td->td_ucred, td); 370 if (error) 371 error = priv_check(td, PRIV_VFS_MOUNT_PERM); 372 if (error) { 373 vput(devvp); 374 return (error); 375 } 376 if ((mp->mnt_flag & MNT_UPDATE) == 0) { 377 error = mountmsdosfs(devvp, mp); 378 #ifdef MSDOSFS_DEBUG /* only needed for the printf below */ 379 pmp = VFSTOMSDOSFS(mp); 380 #endif 381 } else { 382 if (devvp != pmp->pm_devvp) 383 error = EINVAL; /* XXX needs translation */ 384 else 385 vput(devvp); 386 } 387 if (error) { 388 vrele(devvp); 389 return (error); 390 } 391 392 error = update_mp(mp, td); 393 if (error) { 394 if ((mp->mnt_flag & MNT_UPDATE) == 0) 395 msdosfs_unmount(mp, MNT_FORCE, td); 396 return error; 397 } 398 399 vfs_mountedfrom(mp, from); 400 #ifdef MSDOSFS_DEBUG 401 printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap); 402 #endif 403 return (0); 404 } 405 406 static int 407 mountmsdosfs(struct vnode *devvp, struct mount *mp) 408 { 409 struct msdosfsmount *pmp; 410 struct buf *bp; 411 struct cdev *dev; 412 union bootsector *bsp; 413 struct byte_bpb33 *b33; 414 struct byte_bpb50 *b50; 415 struct byte_bpb710 *b710; 416 u_int8_t SecPerClust; 417 u_long clusters; 418 int ronly, error; 419 struct g_consumer *cp; 420 struct bufobj *bo; 421 422 bp = NULL; /* This and pmp both used in error_exit. */ 423 pmp = NULL; 424 ronly = (mp->mnt_flag & MNT_RDONLY) != 0; 425 426 dev = devvp->v_rdev; 427 dev_ref(dev); 428 DROP_GIANT(); 429 g_topology_lock(); 430 error = g_vfs_open(devvp, &cp, "msdosfs", ronly ? 0 : 1); 431 g_topology_unlock(); 432 PICKUP_GIANT(); 433 VOP_UNLOCK(devvp, 0); 434 if (error) 435 goto error_exit; 436 437 bo = &devvp->v_bufobj; 438 439 /* 440 * Read the boot sector of the filesystem, and then check the 441 * boot signature. If not a dos boot sector then error out. 442 * 443 * NOTE: 8192 is a magic size that works for ffs. 444 */ 445 error = bread(devvp, 0, 8192, NOCRED, &bp); 446 if (error) 447 goto error_exit; 448 bp->b_flags |= B_AGE; 449 bsp = (union bootsector *)bp->b_data; 450 b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB; 451 b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB; 452 b710 = (struct byte_bpb710 *)bsp->bs710.bsBPB; 453 454 #ifndef MSDOSFS_NOCHECKSIG 455 if (bsp->bs50.bsBootSectSig0 != BOOTSIG0 456 || bsp->bs50.bsBootSectSig1 != BOOTSIG1) { 457 error = EINVAL; 458 goto error_exit; 459 } 460 #endif 461 462 pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK | M_ZERO); 463 pmp->pm_mountp = mp; 464 pmp->pm_cp = cp; 465 pmp->pm_bo = bo; 466 467 /* 468 * Initialize ownerships and permissions, since nothing else will 469 * initialize them iff we are mounting root. 470 */ 471 pmp->pm_uid = UID_ROOT; 472 pmp->pm_gid = GID_WHEEL; 473 pmp->pm_mask = pmp->pm_dirmask = S_IXUSR | S_IXGRP | S_IXOTH | 474 S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR; 475 476 /* 477 * Experimental support for large MS-DOS filesystems. 478 * WARNING: This uses at least 32 bytes of kernel memory (which is not 479 * reclaimed until the FS is unmounted) for each file on disk to map 480 * between the 32-bit inode numbers used by VFS and the 64-bit 481 * pseudo-inode numbers used internally by msdosfs. This is only 482 * safe to use in certain controlled situations (e.g. read-only FS 483 * with less than 1 million files). 484 * Since the mappings do not persist across unmounts (or reboots), these 485 * filesystems are not suitable for exporting through NFS, or any other 486 * application that requires fixed inode numbers. 487 */ 488 vfs_flagopt(mp->mnt_optnew, "large", &pmp->pm_flags, MSDOSFS_LARGEFS); 489 490 /* 491 * Compute several useful quantities from the bpb in the 492 * bootsector. Copy in the dos 5 variant of the bpb then fix up 493 * the fields that are different between dos 5 and dos 3.3. 494 */ 495 SecPerClust = b50->bpbSecPerClust; 496 pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec); 497 if (pmp->pm_BytesPerSec < DEV_BSIZE) { 498 error = EINVAL; 499 goto error_exit; 500 } 501 pmp->pm_ResSectors = getushort(b50->bpbResSectors); 502 pmp->pm_FATs = b50->bpbFATs; 503 pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts); 504 pmp->pm_Sectors = getushort(b50->bpbSectors); 505 pmp->pm_FATsecs = getushort(b50->bpbFATsecs); 506 pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack); 507 pmp->pm_Heads = getushort(b50->bpbHeads); 508 pmp->pm_Media = b50->bpbMedia; 509 510 /* calculate the ratio of sector size to DEV_BSIZE */ 511 pmp->pm_BlkPerSec = pmp->pm_BytesPerSec / DEV_BSIZE; 512 513 /* 514 * We don't check pm_Heads nor pm_SecPerTrack, because 515 * these may not be set for EFI file systems. We don't 516 * use these anyway, so we're unaffected if they are 517 * invalid. 518 */ 519 if (!pmp->pm_BytesPerSec || !SecPerClust) { 520 error = EINVAL; 521 goto error_exit; 522 } 523 524 if (pmp->pm_Sectors == 0) { 525 pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs); 526 pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors); 527 } else { 528 pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs); 529 pmp->pm_HugeSectors = pmp->pm_Sectors; 530 } 531 if (!(pmp->pm_flags & MSDOSFS_LARGEFS)) { 532 if (pmp->pm_HugeSectors > 0xffffffff / 533 (pmp->pm_BytesPerSec / sizeof(struct direntry)) + 1) { 534 /* 535 * We cannot deal currently with this size of disk 536 * due to fileid limitations (see msdosfs_getattr and 537 * msdosfs_readdir) 538 */ 539 error = EINVAL; 540 vfs_mount_error(mp, 541 "Disk too big, try '-o large' mount option"); 542 goto error_exit; 543 } 544 } 545 546 if (pmp->pm_RootDirEnts == 0) { 547 if (pmp->pm_Sectors 548 || pmp->pm_FATsecs 549 || getushort(b710->bpbFSVers)) { 550 error = EINVAL; 551 printf("mountmsdosfs(): bad FAT32 filesystem\n"); 552 goto error_exit; 553 } 554 pmp->pm_fatmask = FAT32_MASK; 555 pmp->pm_fatmult = 4; 556 pmp->pm_fatdiv = 1; 557 pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs); 558 if (getushort(b710->bpbExtFlags) & FATMIRROR) 559 pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM; 560 else 561 pmp->pm_flags |= MSDOSFS_FATMIRROR; 562 } else 563 pmp->pm_flags |= MSDOSFS_FATMIRROR; 564 565 /* 566 * Check a few values (could do some more): 567 * - logical sector size: power of 2, >= block size 568 * - sectors per cluster: power of 2, >= 1 569 * - number of sectors: >= 1, <= size of partition 570 * - number of FAT sectors: >= 1 571 */ 572 if ( (SecPerClust == 0) 573 || (SecPerClust & (SecPerClust - 1)) 574 || (pmp->pm_BytesPerSec < DEV_BSIZE) 575 || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1)) 576 || (pmp->pm_HugeSectors == 0) 577 || (pmp->pm_FATsecs == 0) 578 ) { 579 error = EINVAL; 580 goto error_exit; 581 } 582 583 pmp->pm_HugeSectors *= pmp->pm_BlkPerSec; 584 pmp->pm_HiddenSects *= pmp->pm_BlkPerSec; /* XXX not used? */ 585 pmp->pm_FATsecs *= pmp->pm_BlkPerSec; 586 SecPerClust *= pmp->pm_BlkPerSec; 587 588 pmp->pm_fatblk = pmp->pm_ResSectors * pmp->pm_BlkPerSec; 589 590 if (FAT32(pmp)) { 591 pmp->pm_rootdirblk = getulong(b710->bpbRootClust); 592 pmp->pm_firstcluster = pmp->pm_fatblk 593 + (pmp->pm_FATs * pmp->pm_FATsecs); 594 pmp->pm_fsinfo = getushort(b710->bpbFSInfo) * pmp->pm_BlkPerSec; 595 } else { 596 pmp->pm_rootdirblk = pmp->pm_fatblk + 597 (pmp->pm_FATs * pmp->pm_FATsecs); 598 pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry) 599 + DEV_BSIZE - 1) 600 / DEV_BSIZE; /* in blocks */ 601 pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize; 602 } 603 604 pmp->pm_maxcluster = (pmp->pm_HugeSectors - pmp->pm_firstcluster) / 605 SecPerClust + 1; 606 pmp->pm_fatsize = pmp->pm_FATsecs * DEV_BSIZE; /* XXX not used? */ 607 608 if (pmp->pm_fatmask == 0) { 609 if (pmp->pm_maxcluster 610 <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) { 611 /* 612 * This will usually be a floppy disk. This size makes 613 * sure that one fat entry will not be split across 614 * multiple blocks. 615 */ 616 pmp->pm_fatmask = FAT12_MASK; 617 pmp->pm_fatmult = 3; 618 pmp->pm_fatdiv = 2; 619 } else { 620 pmp->pm_fatmask = FAT16_MASK; 621 pmp->pm_fatmult = 2; 622 pmp->pm_fatdiv = 1; 623 } 624 } 625 626 clusters = (pmp->pm_fatsize / pmp->pm_fatmult) * pmp->pm_fatdiv; 627 if (pmp->pm_maxcluster >= clusters) { 628 printf("Warning: number of clusters (%ld) exceeds FAT " 629 "capacity (%ld)\n", pmp->pm_maxcluster + 1, clusters); 630 pmp->pm_maxcluster = clusters - 1; 631 } 632 633 if (FAT12(pmp)) 634 pmp->pm_fatblocksize = 3 * 512; 635 else 636 pmp->pm_fatblocksize = PAGE_SIZE; 637 pmp->pm_fatblocksize = roundup(pmp->pm_fatblocksize, 638 pmp->pm_BytesPerSec); 639 pmp->pm_fatblocksec = pmp->pm_fatblocksize / DEV_BSIZE; 640 pmp->pm_bnshift = ffs(DEV_BSIZE) - 1; 641 642 /* 643 * Compute mask and shift value for isolating cluster relative byte 644 * offsets and cluster numbers from a file offset. 645 */ 646 pmp->pm_bpcluster = SecPerClust * DEV_BSIZE; 647 pmp->pm_crbomask = pmp->pm_bpcluster - 1; 648 pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1; 649 650 /* 651 * Check for valid cluster size 652 * must be a power of 2 653 */ 654 if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) { 655 error = EINVAL; 656 goto error_exit; 657 } 658 659 /* 660 * Release the bootsector buffer. 661 */ 662 brelse(bp); 663 bp = NULL; 664 665 /* 666 * Check the fsinfo sector if we have one. Silently fix up our 667 * in-core copy of fp->fsinxtfree if it is unknown (0xffffffff) 668 * or too large. Ignore fp->fsinfree for now, since we need to 669 * read the entire FAT anyway to fill the inuse map. 670 */ 671 if (pmp->pm_fsinfo) { 672 struct fsinfo *fp; 673 674 if ((error = bread(devvp, pmp->pm_fsinfo, pmp->pm_BytesPerSec, 675 NOCRED, &bp)) != 0) 676 goto error_exit; 677 fp = (struct fsinfo *)bp->b_data; 678 if (!bcmp(fp->fsisig1, "RRaA", 4) 679 && !bcmp(fp->fsisig2, "rrAa", 4) 680 && !bcmp(fp->fsisig3, "\0\0\125\252", 4)) { 681 pmp->pm_nxtfree = getulong(fp->fsinxtfree); 682 if (pmp->pm_nxtfree > pmp->pm_maxcluster) 683 pmp->pm_nxtfree = CLUST_FIRST; 684 } else 685 pmp->pm_fsinfo = 0; 686 brelse(bp); 687 bp = NULL; 688 } 689 690 /* 691 * Finish initializing pmp->pm_nxtfree (just in case the first few 692 * sectors aren't properly reserved in the FAT). This completes 693 * the fixup for fp->fsinxtfree, and fixes up the zero-initialized 694 * value if there is no fsinfo. We will use pmp->pm_nxtfree 695 * internally even if there is no fsinfo. 696 */ 697 if (pmp->pm_nxtfree < CLUST_FIRST) 698 pmp->pm_nxtfree = CLUST_FIRST; 699 700 /* 701 * Allocate memory for the bitmap of allocated clusters, and then 702 * fill it in. 703 */ 704 pmp->pm_inusemap = malloc(howmany(pmp->pm_maxcluster + 1, N_INUSEBITS) 705 * sizeof(*pmp->pm_inusemap), 706 M_MSDOSFSFAT, M_WAITOK); 707 708 /* 709 * fillinusemap() needs pm_devvp. 710 */ 711 pmp->pm_devvp = devvp; 712 pmp->pm_dev = dev; 713 714 /* 715 * Have the inuse map filled in. 716 */ 717 if ((error = fillinusemap(pmp)) != 0) 718 goto error_exit; 719 720 /* 721 * If they want fat updates to be synchronous then let them suffer 722 * the performance degradation in exchange for the on disk copy of 723 * the fat being correct just about all the time. I suppose this 724 * would be a good thing to turn on if the kernel is still flakey. 725 */ 726 if (mp->mnt_flag & MNT_SYNCHRONOUS) 727 pmp->pm_flags |= MSDOSFSMNT_WAITONFAT; 728 729 /* 730 * Finish up. 731 */ 732 if (ronly) 733 pmp->pm_flags |= MSDOSFSMNT_RONLY; 734 else { 735 if ((error = markvoldirty(pmp, 1)) != 0) { 736 (void)markvoldirty(pmp, 0); 737 goto error_exit; 738 } 739 pmp->pm_fmod = 1; 740 } 741 mp->mnt_data = pmp; 742 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev); 743 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum; 744 MNT_ILOCK(mp); 745 mp->mnt_flag |= MNT_LOCAL; 746 MNT_IUNLOCK(mp); 747 748 if (pmp->pm_flags & MSDOSFS_LARGEFS) 749 msdosfs_fileno_init(mp); 750 751 return 0; 752 753 error_exit: 754 if (bp) 755 brelse(bp); 756 if (cp != NULL) { 757 DROP_GIANT(); 758 g_topology_lock(); 759 g_vfs_close(cp); 760 g_topology_unlock(); 761 PICKUP_GIANT(); 762 } 763 if (pmp) { 764 if (pmp->pm_inusemap) 765 free(pmp->pm_inusemap, M_MSDOSFSFAT); 766 free(pmp, M_MSDOSFSMNT); 767 mp->mnt_data = NULL; 768 } 769 dev_rel(dev); 770 return (error); 771 } 772 773 /* 774 * Unmount the filesystem described by mp. 775 */ 776 static int 777 msdosfs_unmount(struct mount *mp, int mntflags, struct thread *td) 778 { 779 struct msdosfsmount *pmp; 780 int error, flags; 781 782 flags = 0; 783 if (mntflags & MNT_FORCE) 784 flags |= FORCECLOSE; 785 error = vflush(mp, 0, flags, td); 786 if (error && error != ENXIO) 787 return error; 788 pmp = VFSTOMSDOSFS(mp); 789 if ((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0) { 790 error = markvoldirty(pmp, 0); 791 if (error && error != ENXIO) { 792 (void)markvoldirty(pmp, 1); 793 return (error); 794 } 795 } 796 if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) { 797 if (pmp->pm_w2u) 798 msdosfs_iconv->close(pmp->pm_w2u); 799 if (pmp->pm_u2w) 800 msdosfs_iconv->close(pmp->pm_u2w); 801 if (pmp->pm_d2u) 802 msdosfs_iconv->close(pmp->pm_d2u); 803 if (pmp->pm_u2d) 804 msdosfs_iconv->close(pmp->pm_u2d); 805 } 806 807 #ifdef MSDOSFS_DEBUG 808 { 809 struct vnode *vp = pmp->pm_devvp; 810 struct bufobj *bo; 811 812 bo = &vp->v_bufobj; 813 BO_LOCK(bo); 814 VI_LOCK(vp); 815 vn_printf(vp, 816 "msdosfs_umount(): just before calling VOP_CLOSE()\n"); 817 printf("freef %p, freeb %p, mount %p\n", 818 TAILQ_NEXT(vp, v_freelist), vp->v_freelist.tqe_prev, 819 vp->v_mount); 820 printf("cleanblkhd %p, dirtyblkhd %p, numoutput %ld, type %d\n", 821 TAILQ_FIRST(&vp->v_bufobj.bo_clean.bv_hd), 822 TAILQ_FIRST(&vp->v_bufobj.bo_dirty.bv_hd), 823 vp->v_bufobj.bo_numoutput, vp->v_type); 824 VI_UNLOCK(vp); 825 BO_UNLOCK(bo); 826 } 827 #endif 828 DROP_GIANT(); 829 g_topology_lock(); 830 g_vfs_close(pmp->pm_cp); 831 g_topology_unlock(); 832 PICKUP_GIANT(); 833 vrele(pmp->pm_devvp); 834 dev_rel(pmp->pm_dev); 835 free(pmp->pm_inusemap, M_MSDOSFSFAT); 836 if (pmp->pm_flags & MSDOSFS_LARGEFS) 837 msdosfs_fileno_free(mp); 838 free(pmp, M_MSDOSFSMNT); 839 mp->mnt_data = NULL; 840 MNT_ILOCK(mp); 841 mp->mnt_flag &= ~MNT_LOCAL; 842 MNT_IUNLOCK(mp); 843 return (error); 844 } 845 846 static int 847 msdosfs_root(struct mount *mp, int flags, struct vnode **vpp, struct thread *td) 848 { 849 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 850 struct denode *ndep; 851 int error; 852 853 #ifdef MSDOSFS_DEBUG 854 printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp); 855 #endif 856 error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep); 857 if (error) 858 return (error); 859 *vpp = DETOV(ndep); 860 return (0); 861 } 862 863 static int 864 msdosfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td) 865 { 866 struct msdosfsmount *pmp; 867 868 pmp = VFSTOMSDOSFS(mp); 869 sbp->f_bsize = pmp->pm_bpcluster; 870 sbp->f_iosize = pmp->pm_bpcluster; 871 sbp->f_blocks = pmp->pm_maxcluster + 1; 872 sbp->f_bfree = pmp->pm_freeclustercount; 873 sbp->f_bavail = pmp->pm_freeclustercount; 874 sbp->f_files = pmp->pm_RootDirEnts; /* XXX */ 875 sbp->f_ffree = 0; /* what to put in here? */ 876 return (0); 877 } 878 879 static int 880 msdosfs_sync(struct mount *mp, int waitfor, struct thread *td) 881 { 882 struct vnode *vp, *nvp; 883 struct denode *dep; 884 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 885 int error, allerror = 0; 886 887 /* 888 * If we ever switch to not updating all of the fats all the time, 889 * this would be the place to update them from the first one. 890 */ 891 if (pmp->pm_fmod != 0) { 892 if (pmp->pm_flags & MSDOSFSMNT_RONLY) 893 panic("msdosfs_sync: rofs mod"); 894 else { 895 /* update fats here */ 896 } 897 } 898 /* 899 * Write back each (modified) denode. 900 */ 901 MNT_ILOCK(mp); 902 loop: 903 MNT_VNODE_FOREACH(vp, mp, nvp) { 904 VI_LOCK(vp); 905 if (vp->v_type == VNON || (vp->v_iflag & VI_DOOMED)) { 906 VI_UNLOCK(vp); 907 continue; 908 } 909 MNT_IUNLOCK(mp); 910 dep = VTODE(vp); 911 if ((dep->de_flag & 912 (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 && 913 (vp->v_bufobj.bo_dirty.bv_cnt == 0 || 914 waitfor == MNT_LAZY)) { 915 VI_UNLOCK(vp); 916 MNT_ILOCK(mp); 917 continue; 918 } 919 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, td); 920 if (error) { 921 MNT_ILOCK(mp); 922 if (error == ENOENT) 923 goto loop; 924 continue; 925 } 926 error = VOP_FSYNC(vp, waitfor, td); 927 if (error) 928 allerror = error; 929 VOP_UNLOCK(vp, 0); 930 vrele(vp); 931 MNT_ILOCK(mp); 932 } 933 MNT_IUNLOCK(mp); 934 935 /* 936 * Flush filesystem control info. 937 */ 938 if (waitfor != MNT_LAZY) { 939 vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY); 940 error = VOP_FSYNC(pmp->pm_devvp, waitfor, td); 941 if (error) 942 allerror = error; 943 VOP_UNLOCK(pmp->pm_devvp, 0); 944 } 945 return (allerror); 946 } 947 948 static int 949 msdosfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp) 950 { 951 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 952 struct defid *defhp = (struct defid *) fhp; 953 struct denode *dep; 954 int error; 955 956 error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep); 957 if (error) { 958 *vpp = NULLVP; 959 return (error); 960 } 961 *vpp = DETOV(dep); 962 vnode_create_vobject(*vpp, dep->de_FileSize, curthread); 963 return (0); 964 } 965 966 static struct vfsops msdosfs_vfsops = { 967 .vfs_fhtovp = msdosfs_fhtovp, 968 .vfs_mount = msdosfs_mount, 969 .vfs_cmount = msdosfs_cmount, 970 .vfs_root = msdosfs_root, 971 .vfs_statfs = msdosfs_statfs, 972 .vfs_sync = msdosfs_sync, 973 .vfs_unmount = msdosfs_unmount, 974 }; 975 976 VFS_SET(msdosfs_vfsops, msdosfs, 0); 977 MODULE_VERSION(msdosfs, 1); 978