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