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