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