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