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 = devvp->v_rdev; 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 ronly = (mp->mnt_flag & MNT_RDONLY) != 0; 423 /* XXX: use VOP_ACCESS to check FS perms */ 424 DROP_GIANT(); 425 g_topology_lock(); 426 error = g_vfs_open(devvp, &cp, "msdosfs", ronly ? 0 : 1); 427 g_topology_unlock(); 428 PICKUP_GIANT(); 429 VOP_UNLOCK(devvp, 0); 430 if (error) 431 return (error); 432 433 bo = &devvp->v_bufobj; 434 bp = NULL; /* This and pmp both used in error_exit. */ 435 pmp = NULL; 436 437 /* 438 * Read the boot sector of the filesystem, and then check the 439 * boot signature. If not a dos boot sector then error out. 440 * 441 * NOTE: 8192 is a magic size that works for ffs. 442 */ 443 error = bread(devvp, 0, 8192, NOCRED, &bp); 444 if (error) 445 goto error_exit; 446 bp->b_flags |= B_AGE; 447 bsp = (union bootsector *)bp->b_data; 448 b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB; 449 b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB; 450 b710 = (struct byte_bpb710 *)bsp->bs710.bsBPB; 451 452 #ifndef MSDOSFS_NOCHECKSIG 453 if (bsp->bs50.bsBootSectSig0 != BOOTSIG0 454 || bsp->bs50.bsBootSectSig1 != BOOTSIG1) { 455 error = EINVAL; 456 goto error_exit; 457 } 458 #endif 459 460 pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK | M_ZERO); 461 pmp->pm_mountp = mp; 462 pmp->pm_cp = cp; 463 pmp->pm_bo = bo; 464 465 /* 466 * Initialize ownerships and permissions, since nothing else will 467 * initialize them iff we are mounting root. 468 */ 469 pmp->pm_uid = UID_ROOT; 470 pmp->pm_gid = GID_WHEEL; 471 pmp->pm_mask = pmp->pm_dirmask = S_IXUSR | S_IXGRP | S_IXOTH | 472 S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR; 473 474 /* 475 * Experimental support for large MS-DOS filesystems. 476 * WARNING: This uses at least 32 bytes of kernel memory (which is not 477 * reclaimed until the FS is unmounted) for each file on disk to map 478 * between the 32-bit inode numbers used by VFS and the 64-bit 479 * pseudo-inode numbers used internally by msdosfs. This is only 480 * safe to use in certain controlled situations (e.g. read-only FS 481 * with less than 1 million files). 482 * Since the mappings do not persist across unmounts (or reboots), these 483 * filesystems are not suitable for exporting through NFS, or any other 484 * application that requires fixed inode numbers. 485 */ 486 vfs_flagopt(mp->mnt_optnew, "large", &pmp->pm_flags, MSDOSFS_LARGEFS); 487 488 /* 489 * Compute several useful quantities from the bpb in the 490 * bootsector. Copy in the dos 5 variant of the bpb then fix up 491 * the fields that are different between dos 5 and dos 3.3. 492 */ 493 SecPerClust = b50->bpbSecPerClust; 494 pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec); 495 if (pmp->pm_BytesPerSec < DEV_BSIZE) { 496 error = EINVAL; 497 goto error_exit; 498 } 499 pmp->pm_ResSectors = getushort(b50->bpbResSectors); 500 pmp->pm_FATs = b50->bpbFATs; 501 pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts); 502 pmp->pm_Sectors = getushort(b50->bpbSectors); 503 pmp->pm_FATsecs = getushort(b50->bpbFATsecs); 504 pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack); 505 pmp->pm_Heads = getushort(b50->bpbHeads); 506 pmp->pm_Media = b50->bpbMedia; 507 508 /* calculate the ratio of sector size to DEV_BSIZE */ 509 pmp->pm_BlkPerSec = pmp->pm_BytesPerSec / DEV_BSIZE; 510 511 /* 512 * We don't check pm_Heads nor pm_SecPerTrack, because 513 * these may not be set for EFI file systems. We don't 514 * use these anyway, so we're unaffected if they are 515 * invalid. 516 */ 517 if (!pmp->pm_BytesPerSec || !SecPerClust) { 518 error = EINVAL; 519 goto error_exit; 520 } 521 522 if (pmp->pm_Sectors == 0) { 523 pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs); 524 pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors); 525 } else { 526 pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs); 527 pmp->pm_HugeSectors = pmp->pm_Sectors; 528 } 529 if (!(pmp->pm_flags & MSDOSFS_LARGEFS)) { 530 if (pmp->pm_HugeSectors > 0xffffffff / 531 (pmp->pm_BytesPerSec / sizeof(struct direntry)) + 1) { 532 /* 533 * We cannot deal currently with this size of disk 534 * due to fileid limitations (see msdosfs_getattr and 535 * msdosfs_readdir) 536 */ 537 error = EINVAL; 538 vfs_mount_error(mp, 539 "Disk too big, try '-o large' mount option"); 540 goto error_exit; 541 } 542 } 543 544 if (pmp->pm_RootDirEnts == 0) { 545 if (pmp->pm_Sectors 546 || pmp->pm_FATsecs 547 || getushort(b710->bpbFSVers)) { 548 error = EINVAL; 549 printf("mountmsdosfs(): bad FAT32 filesystem\n"); 550 goto error_exit; 551 } 552 pmp->pm_fatmask = FAT32_MASK; 553 pmp->pm_fatmult = 4; 554 pmp->pm_fatdiv = 1; 555 pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs); 556 if (getushort(b710->bpbExtFlags) & FATMIRROR) 557 pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM; 558 else 559 pmp->pm_flags |= MSDOSFS_FATMIRROR; 560 } else 561 pmp->pm_flags |= MSDOSFS_FATMIRROR; 562 563 /* 564 * Check a few values (could do some more): 565 * - logical sector size: power of 2, >= block size 566 * - sectors per cluster: power of 2, >= 1 567 * - number of sectors: >= 1, <= size of partition 568 * - number of FAT sectors: >= 1 569 */ 570 if ( (SecPerClust == 0) 571 || (SecPerClust & (SecPerClust - 1)) 572 || (pmp->pm_BytesPerSec < DEV_BSIZE) 573 || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1)) 574 || (pmp->pm_HugeSectors == 0) 575 || (pmp->pm_FATsecs == 0) 576 ) { 577 error = EINVAL; 578 goto error_exit; 579 } 580 581 pmp->pm_HugeSectors *= pmp->pm_BlkPerSec; 582 pmp->pm_HiddenSects *= pmp->pm_BlkPerSec; /* XXX not used? */ 583 pmp->pm_FATsecs *= pmp->pm_BlkPerSec; 584 SecPerClust *= pmp->pm_BlkPerSec; 585 586 pmp->pm_fatblk = pmp->pm_ResSectors * pmp->pm_BlkPerSec; 587 588 if (FAT32(pmp)) { 589 pmp->pm_rootdirblk = getulong(b710->bpbRootClust); 590 pmp->pm_firstcluster = pmp->pm_fatblk 591 + (pmp->pm_FATs * pmp->pm_FATsecs); 592 pmp->pm_fsinfo = getushort(b710->bpbFSInfo) * pmp->pm_BlkPerSec; 593 } else { 594 pmp->pm_rootdirblk = pmp->pm_fatblk + 595 (pmp->pm_FATs * pmp->pm_FATsecs); 596 pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry) 597 + DEV_BSIZE - 1) 598 / DEV_BSIZE; /* in blocks */ 599 pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize; 600 } 601 602 pmp->pm_maxcluster = (pmp->pm_HugeSectors - pmp->pm_firstcluster) / 603 SecPerClust + 1; 604 pmp->pm_fatsize = pmp->pm_FATsecs * DEV_BSIZE; /* XXX not used? */ 605 606 if (pmp->pm_fatmask == 0) { 607 if (pmp->pm_maxcluster 608 <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) { 609 /* 610 * This will usually be a floppy disk. This size makes 611 * sure that one fat entry will not be split across 612 * multiple blocks. 613 */ 614 pmp->pm_fatmask = FAT12_MASK; 615 pmp->pm_fatmult = 3; 616 pmp->pm_fatdiv = 2; 617 } else { 618 pmp->pm_fatmask = FAT16_MASK; 619 pmp->pm_fatmult = 2; 620 pmp->pm_fatdiv = 1; 621 } 622 } 623 624 clusters = (pmp->pm_fatsize / pmp->pm_fatmult) * pmp->pm_fatdiv; 625 if (pmp->pm_maxcluster >= clusters) { 626 printf("Warning: number of clusters (%ld) exceeds FAT " 627 "capacity (%ld)\n", pmp->pm_maxcluster + 1, clusters); 628 pmp->pm_maxcluster = clusters - 1; 629 } 630 631 if (FAT12(pmp)) 632 pmp->pm_fatblocksize = 3 * 512; 633 else 634 pmp->pm_fatblocksize = PAGE_SIZE; 635 pmp->pm_fatblocksize = roundup(pmp->pm_fatblocksize, 636 pmp->pm_BytesPerSec); 637 pmp->pm_fatblocksec = pmp->pm_fatblocksize / DEV_BSIZE; 638 pmp->pm_bnshift = ffs(DEV_BSIZE) - 1; 639 640 /* 641 * Compute mask and shift value for isolating cluster relative byte 642 * offsets and cluster numbers from a file offset. 643 */ 644 pmp->pm_bpcluster = SecPerClust * DEV_BSIZE; 645 pmp->pm_crbomask = pmp->pm_bpcluster - 1; 646 pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1; 647 648 /* 649 * Check for valid cluster size 650 * must be a power of 2 651 */ 652 if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) { 653 error = EINVAL; 654 goto error_exit; 655 } 656 657 /* 658 * Release the bootsector buffer. 659 */ 660 brelse(bp); 661 bp = NULL; 662 663 /* 664 * Check the fsinfo sector if we have one. Silently fix up our 665 * in-core copy of fp->fsinxtfree if it is unknown (0xffffffff) 666 * or too large. Ignore fp->fsinfree for now, since we need to 667 * read the entire FAT anyway to fill the inuse map. 668 */ 669 if (pmp->pm_fsinfo) { 670 struct fsinfo *fp; 671 672 if ((error = bread(devvp, pmp->pm_fsinfo, pmp->pm_BytesPerSec, 673 NOCRED, &bp)) != 0) 674 goto error_exit; 675 fp = (struct fsinfo *)bp->b_data; 676 if (!bcmp(fp->fsisig1, "RRaA", 4) 677 && !bcmp(fp->fsisig2, "rrAa", 4) 678 && !bcmp(fp->fsisig3, "\0\0\125\252", 4)) { 679 pmp->pm_nxtfree = getulong(fp->fsinxtfree); 680 if (pmp->pm_nxtfree > pmp->pm_maxcluster) 681 pmp->pm_nxtfree = CLUST_FIRST; 682 } else 683 pmp->pm_fsinfo = 0; 684 brelse(bp); 685 bp = NULL; 686 } 687 688 /* 689 * Finish initializing pmp->pm_nxtfree (just in case the first few 690 * sectors aren't properly reserved in the FAT). This completes 691 * the fixup for fp->fsinxtfree, and fixes up the zero-initialized 692 * value if there is no fsinfo. We will use pmp->pm_nxtfree 693 * internally even if there is no fsinfo. 694 */ 695 if (pmp->pm_nxtfree < CLUST_FIRST) 696 pmp->pm_nxtfree = CLUST_FIRST; 697 698 /* 699 * Allocate memory for the bitmap of allocated clusters, and then 700 * fill it in. 701 */ 702 pmp->pm_inusemap = malloc(howmany(pmp->pm_maxcluster + 1, N_INUSEBITS) 703 * sizeof(*pmp->pm_inusemap), 704 M_MSDOSFSFAT, M_WAITOK); 705 706 /* 707 * fillinusemap() needs pm_devvp. 708 */ 709 pmp->pm_devvp = devvp; 710 711 /* 712 * Have the inuse map filled in. 713 */ 714 if ((error = fillinusemap(pmp)) != 0) 715 goto error_exit; 716 717 /* 718 * If they want fat updates to be synchronous then let them suffer 719 * the performance degradation in exchange for the on disk copy of 720 * the fat being correct just about all the time. I suppose this 721 * would be a good thing to turn on if the kernel is still flakey. 722 */ 723 if (mp->mnt_flag & MNT_SYNCHRONOUS) 724 pmp->pm_flags |= MSDOSFSMNT_WAITONFAT; 725 726 /* 727 * Finish up. 728 */ 729 if (ronly) 730 pmp->pm_flags |= MSDOSFSMNT_RONLY; 731 else { 732 if ((error = markvoldirty(pmp, 1)) != 0) { 733 (void)markvoldirty(pmp, 0); 734 goto error_exit; 735 } 736 pmp->pm_fmod = 1; 737 } 738 mp->mnt_data = pmp; 739 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev); 740 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum; 741 MNT_ILOCK(mp); 742 mp->mnt_flag |= MNT_LOCAL; 743 MNT_IUNLOCK(mp); 744 745 if (pmp->pm_flags & MSDOSFS_LARGEFS) 746 msdosfs_fileno_init(mp); 747 748 return 0; 749 750 error_exit: 751 if (bp) 752 brelse(bp); 753 if (cp != NULL) { 754 DROP_GIANT(); 755 g_topology_lock(); 756 g_vfs_close(cp); 757 g_topology_unlock(); 758 PICKUP_GIANT(); 759 } 760 if (pmp) { 761 if (pmp->pm_inusemap) 762 free(pmp->pm_inusemap, M_MSDOSFSFAT); 763 free(pmp, M_MSDOSFSMNT); 764 mp->mnt_data = NULL; 765 } 766 return (error); 767 } 768 769 /* 770 * Unmount the filesystem described by mp. 771 */ 772 static int 773 msdosfs_unmount(struct mount *mp, int mntflags, struct thread *td) 774 { 775 struct msdosfsmount *pmp; 776 int error, flags; 777 778 flags = 0; 779 if (mntflags & MNT_FORCE) 780 flags |= FORCECLOSE; 781 error = vflush(mp, 0, flags, td); 782 if (error) 783 return error; 784 pmp = VFSTOMSDOSFS(mp); 785 if ((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0) { 786 error = markvoldirty(pmp, 0); 787 if (error) { 788 (void)markvoldirty(pmp, 1); 789 return (error); 790 } 791 } 792 if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) { 793 if (pmp->pm_w2u) 794 msdosfs_iconv->close(pmp->pm_w2u); 795 if (pmp->pm_u2w) 796 msdosfs_iconv->close(pmp->pm_u2w); 797 if (pmp->pm_d2u) 798 msdosfs_iconv->close(pmp->pm_d2u); 799 if (pmp->pm_u2d) 800 msdosfs_iconv->close(pmp->pm_u2d); 801 } 802 803 #ifdef MSDOSFS_DEBUG 804 { 805 struct vnode *vp = pmp->pm_devvp; 806 struct bufobj *bo; 807 808 bo = &vp->v_bufobj; 809 BO_LOCK(bo); 810 VI_LOCK(vp); 811 vn_printf(vp, 812 "msdosfs_umount(): just before calling VOP_CLOSE()\n"); 813 printf("freef %p, freeb %p, mount %p\n", 814 TAILQ_NEXT(vp, v_freelist), vp->v_freelist.tqe_prev, 815 vp->v_mount); 816 printf("cleanblkhd %p, dirtyblkhd %p, numoutput %ld, type %d\n", 817 TAILQ_FIRST(&vp->v_bufobj.bo_clean.bv_hd), 818 TAILQ_FIRST(&vp->v_bufobj.bo_dirty.bv_hd), 819 vp->v_bufobj.bo_numoutput, vp->v_type); 820 VI_UNLOCK(vp); 821 BO_UNLOCK(bo); 822 } 823 #endif 824 DROP_GIANT(); 825 g_topology_lock(); 826 g_vfs_close(pmp->pm_cp); 827 g_topology_unlock(); 828 PICKUP_GIANT(); 829 vrele(pmp->pm_devvp); 830 free(pmp->pm_inusemap, M_MSDOSFSFAT); 831 if (pmp->pm_flags & MSDOSFS_LARGEFS) 832 msdosfs_fileno_free(mp); 833 free(pmp, M_MSDOSFSMNT); 834 mp->mnt_data = NULL; 835 MNT_ILOCK(mp); 836 mp->mnt_flag &= ~MNT_LOCAL; 837 MNT_IUNLOCK(mp); 838 return (0); 839 } 840 841 static int 842 msdosfs_root(struct mount *mp, int flags, struct vnode **vpp, struct thread *td) 843 { 844 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 845 struct denode *ndep; 846 int error; 847 848 #ifdef MSDOSFS_DEBUG 849 printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp); 850 #endif 851 error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep); 852 if (error) 853 return (error); 854 *vpp = DETOV(ndep); 855 return (0); 856 } 857 858 static int 859 msdosfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td) 860 { 861 struct msdosfsmount *pmp; 862 863 pmp = VFSTOMSDOSFS(mp); 864 sbp->f_bsize = pmp->pm_bpcluster; 865 sbp->f_iosize = pmp->pm_bpcluster; 866 sbp->f_blocks = pmp->pm_maxcluster + 1; 867 sbp->f_bfree = pmp->pm_freeclustercount; 868 sbp->f_bavail = pmp->pm_freeclustercount; 869 sbp->f_files = pmp->pm_RootDirEnts; /* XXX */ 870 sbp->f_ffree = 0; /* what to put in here? */ 871 return (0); 872 } 873 874 static int 875 msdosfs_sync(struct mount *mp, int waitfor, struct thread *td) 876 { 877 struct vnode *vp, *nvp; 878 struct denode *dep; 879 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 880 int error, allerror = 0; 881 882 /* 883 * If we ever switch to not updating all of the fats all the time, 884 * this would be the place to update them from the first one. 885 */ 886 if (pmp->pm_fmod != 0) { 887 if (pmp->pm_flags & MSDOSFSMNT_RONLY) 888 panic("msdosfs_sync: rofs mod"); 889 else { 890 /* update fats here */ 891 } 892 } 893 /* 894 * Write back each (modified) denode. 895 */ 896 MNT_ILOCK(mp); 897 loop: 898 MNT_VNODE_FOREACH(vp, mp, nvp) { 899 VI_LOCK(vp); 900 if (vp->v_type == VNON || (vp->v_iflag & VI_DOOMED)) { 901 VI_UNLOCK(vp); 902 continue; 903 } 904 MNT_IUNLOCK(mp); 905 dep = VTODE(vp); 906 if ((dep->de_flag & 907 (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 && 908 (vp->v_bufobj.bo_dirty.bv_cnt == 0 || 909 waitfor == MNT_LAZY)) { 910 VI_UNLOCK(vp); 911 MNT_ILOCK(mp); 912 continue; 913 } 914 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, td); 915 if (error) { 916 MNT_ILOCK(mp); 917 if (error == ENOENT) 918 goto loop; 919 continue; 920 } 921 error = VOP_FSYNC(vp, waitfor, td); 922 if (error) 923 allerror = error; 924 VOP_UNLOCK(vp, 0); 925 vrele(vp); 926 MNT_ILOCK(mp); 927 } 928 MNT_IUNLOCK(mp); 929 930 /* 931 * Flush filesystem control info. 932 */ 933 if (waitfor != MNT_LAZY) { 934 vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY); 935 error = VOP_FSYNC(pmp->pm_devvp, waitfor, td); 936 if (error) 937 allerror = error; 938 VOP_UNLOCK(pmp->pm_devvp, 0); 939 } 940 return (allerror); 941 } 942 943 static int 944 msdosfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp) 945 { 946 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 947 struct defid *defhp = (struct defid *) fhp; 948 struct denode *dep; 949 int error; 950 951 error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep); 952 if (error) { 953 *vpp = NULLVP; 954 return (error); 955 } 956 *vpp = DETOV(dep); 957 vnode_create_vobject(*vpp, dep->de_FileSize, curthread); 958 return (0); 959 } 960 961 static struct vfsops msdosfs_vfsops = { 962 .vfs_fhtovp = msdosfs_fhtovp, 963 .vfs_mount = msdosfs_mount, 964 .vfs_cmount = msdosfs_cmount, 965 .vfs_root = msdosfs_root, 966 .vfs_statfs = msdosfs_statfs, 967 .vfs_sync = msdosfs_sync, 968 .vfs_unmount = msdosfs_unmount, 969 }; 970 971 VFS_SET(msdosfs_vfsops, msdosfs, 0); 972 MODULE_VERSION(msdosfs, 1); 973