msdosfs_vfsops.c revision 166340
1/* $FreeBSD: head/sys/fs/msdosfs/msdosfs_vfsops.c 166340 2007-01-30 03:11:45Z rodrigc $ */ 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 */ 81static 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", "large", 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 103MALLOC_DEFINE(M_MSDOSFSMNT, "msdosfs_mount", "MSDOSFS mount structure"); 104static MALLOC_DEFINE(M_MSDOSFSFAT, "msdosfs_fat", "MSDOSFS file allocation table"); 105 106struct iconv_functions *msdosfs_iconv = NULL; 107 108static int update_mp(struct mount *mp, struct thread *td); 109static int mountmsdosfs(struct vnode *devvp, struct mount *mp, 110 struct thread *td); 111static vfs_fhtovp_t msdosfs_fhtovp; 112static vfs_mount_t msdosfs_mount; 113static vfs_root_t msdosfs_root; 114static vfs_statfs_t msdosfs_statfs; 115static vfs_sync_t msdosfs_sync; 116static vfs_unmount_t msdosfs_unmount; 117static vfs_vptofh_t msdosfs_vptofh; 118 119/* Maximum length of a character set name (arbitrary). */ 120#define MAXCSLEN 64 121 122static int 123update_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 205static int 206msdosfs_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 */ 243static int 244msdosfs_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 int ro_to_rw = 0; 263 pmp = VFSTOMSDOSFS(mp); 264 265 if (vfs_flagopt(mp->mnt_optnew, "export", NULL, 0)) { 266 /* 267 * Forbid export requests if filesystem has 268 * MSDOSFS_LARGEFS flag set. 269 */ 270 if ((pmp->pm_flags & MSDOSFS_LARGEFS) != 0) { 271 vfs_mount_error(mp, 272 "MSDOSFS_LARGEFS flag set, cannot export"); 273 return (EOPNOTSUPP); 274 } 275 } 276 if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) && 277 vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) { 278 error = VFS_SYNC(mp, MNT_WAIT, td); 279 if (error) 280 return (error); 281 flags = WRITECLOSE; 282 if (mp->mnt_flag & MNT_FORCE) 283 flags |= FORCECLOSE; 284 error = vflush(mp, 0, flags, td); 285 if (error) 286 return (error); 287 DROP_GIANT(); 288 g_topology_lock(); 289 g_access(pmp->pm_cp, 0, -1, 0); 290 g_topology_unlock(); 291 PICKUP_GIANT(); 292 /* Now the volume is clean. Mark it. */ 293 error = markvoldirty(pmp, 0); 294 if (error && (flags & FORCECLOSE) == 0) 295 return (error); 296 } else if ((pmp->pm_flags & MSDOSFSMNT_RONLY) && 297 !vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) { 298 /* 299 * If upgrade to read-write by non-root, then verify 300 * that user has necessary permissions on the device. 301 */ 302 devvp = pmp->pm_devvp; 303 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td); 304 error = VOP_ACCESS(devvp, VREAD | VWRITE, 305 td->td_ucred, td); 306 if (error) 307 error = priv_check(td, PRIV_VFS_MOUNT_PERM); 308 if (error) { 309 VOP_UNLOCK(devvp, 0, td); 310 return (error); 311 } 312 VOP_UNLOCK(devvp, 0, td); 313 DROP_GIANT(); 314 g_topology_lock(); 315 error = g_access(pmp->pm_cp, 0, 1, 0); 316 g_topology_unlock(); 317 PICKUP_GIANT(); 318 if (error) 319 return (error); 320 321 ro_to_rw = 1; 322 } 323 vfs_flagopt(mp->mnt_optnew, "ro", 324 &pmp->pm_flags, MSDOSFSMNT_RONLY); 325 vfs_flagopt(mp->mnt_optnew, "ro", 326 &mp->mnt_flag, MNT_RDONLY); 327 328 if (ro_to_rw) { 329 /* Now that the volume is modifiable, mark it dirty. */ 330 error = markvoldirty(pmp, 1); 331 if (error) 332 return (error); 333 } 334 335 if (vfs_getopt(mp->mnt_optnew, "from", NULL, NULL)) { 336#ifdef __notyet__ /* doesn't work correctly with current mountd XXX */ 337 if (args.flags & MSDOSFSMNT_MNTOPT) { 338 pmp->pm_flags &= ~MSDOSFSMNT_MNTOPT; 339 pmp->pm_flags |= args.flags & MSDOSFSMNT_MNTOPT; 340 if (pmp->pm_flags & MSDOSFSMNT_NOWIN95) 341 pmp->pm_flags |= MSDOSFSMNT_SHORTNAME; 342 } 343#endif 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 407static int 408mountmsdosfs(devvp, mp, td) 409 struct vnode *devvp; 410 struct mount *mp; 411 struct thread *td; 412{ 413 struct msdosfsmount *pmp; 414 struct buf *bp; 415 struct cdev *dev = devvp->v_rdev; 416 union bootsector *bsp; 417 struct byte_bpb33 *b33; 418 struct byte_bpb50 *b50; 419 struct byte_bpb710 *b710; 420 u_int8_t SecPerClust; 421 u_long clusters; 422 int ronly, error; 423 struct g_consumer *cp; 424 struct bufobj *bo; 425 426 ronly = !vfs_getopt(mp->mnt_optnew, "ro", NULL, NULL); 427 /* XXX: use VOP_ACCESS to check FS perms */ 428 DROP_GIANT(); 429 g_topology_lock(); 430 error = g_vfs_open(devvp, &cp, "msdos", ronly ? 0 : 1); 431 g_topology_unlock(); 432 PICKUP_GIANT(); 433 VOP_UNLOCK(devvp, 0, td); 434 if (error) 435 return (error); 436 437 bo = &devvp->v_bufobj; 438 bp = NULL; /* both used in error_exit */ 439 pmp = NULL; 440 441 /* 442 * Read the boot sector of the filesystem, and then check the 443 * boot signature. If not a dos boot sector then error out. 444 * 445 * NOTE: 2048 is a maximum sector size in current... 446 */ 447 error = bread(devvp, 0, 2048, NOCRED, &bp); 448 if (error) 449 goto error_exit; 450 bp->b_flags |= B_AGE; 451 bsp = (union bootsector *)bp->b_data; 452 b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB; 453 b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB; 454 b710 = (struct byte_bpb710 *)bsp->bs710.bsBPB; 455 456#ifndef MSDOSFS_NOCHECKSIG 457 if (bsp->bs50.bsBootSectSig0 != BOOTSIG0 458 || bsp->bs50.bsBootSectSig1 != BOOTSIG1) { 459 error = EINVAL; 460 goto error_exit; 461 } 462#endif 463 464 pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK | M_ZERO); 465 pmp->pm_mountp = mp; 466 pmp->pm_cp = cp; 467 pmp->pm_bo = bo; 468 469 /* 470 * Experimental support for large MS-DOS filesystems. 471 * WARNING: This uses at least 32 bytes of kernel memory (which is not 472 * reclaimed until the FS is unmounted) for each file on disk to map 473 * between the 32-bit inode numbers used by VFS and the 64-bit 474 * pseudo-inode numbers used internally by msdosfs. This is only 475 * safe to use in certain controlled situations (e.g. read-only FS 476 * with less than 1 million files). 477 * Since the mappings do not persist across unmounts (or reboots), these 478 * filesystems are not suitable for exporting through NFS, or any other 479 * application that requires fixed inode numbers. 480 */ 481 vfs_flagopt(mp->mnt_optnew, "large", &pmp->pm_flags, 482 MSDOSFS_LARGEFS); 483 484 /* 485 * Compute several useful quantities from the bpb in the 486 * bootsector. Copy in the dos 5 variant of the bpb then fix up 487 * the fields that are different between dos 5 and dos 3.3. 488 */ 489 SecPerClust = b50->bpbSecPerClust; 490 pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec); 491 if (pmp->pm_BytesPerSec < DEV_BSIZE) { 492 error = EINVAL; 493 goto error_exit; 494 } 495 pmp->pm_ResSectors = getushort(b50->bpbResSectors); 496 pmp->pm_FATs = b50->bpbFATs; 497 pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts); 498 pmp->pm_Sectors = getushort(b50->bpbSectors); 499 pmp->pm_FATsecs = getushort(b50->bpbFATsecs); 500 pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack); 501 pmp->pm_Heads = getushort(b50->bpbHeads); 502 pmp->pm_Media = b50->bpbMedia; 503 504 /* calculate the ratio of sector size to DEV_BSIZE */ 505 pmp->pm_BlkPerSec = pmp->pm_BytesPerSec / DEV_BSIZE; 506 507 /* XXX - We should probably check more values here */ 508 if (!pmp->pm_BytesPerSec || !SecPerClust 509 || !pmp->pm_Heads 510#ifdef PC98 511 || !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 255) { 512#else 513 || !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 63) { 514#endif 515 error = EINVAL; 516 goto error_exit; 517 } 518 519 if (pmp->pm_Sectors == 0) { 520 pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs); 521 pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors); 522 } else { 523 pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs); 524 pmp->pm_HugeSectors = pmp->pm_Sectors; 525 } 526 if (!(pmp->pm_flags & MSDOSFS_LARGEFS)) { 527 if (pmp->pm_HugeSectors > 0xffffffff / 528 (pmp->pm_BytesPerSec / sizeof(struct direntry)) + 1) { 529 /* 530 * We cannot deal currently with this size of disk 531 * due to fileid limitations (see msdosfs_getattr and 532 * msdosfs_readdir) 533 */ 534 error = EINVAL; 535 vfs_mount_error(mp, 536 "Disk too big, try '-o large' mount option"); 537 goto error_exit; 538 } 539 } 540 541 if (pmp->pm_RootDirEnts == 0) { 542 if (pmp->pm_Sectors 543 || pmp->pm_FATsecs 544 || getushort(b710->bpbFSVers)) { 545 error = EINVAL; 546 printf("mountmsdosfs(): bad FAT32 filesystem\n"); 547 goto error_exit; 548 } 549 pmp->pm_fatmask = FAT32_MASK; 550 pmp->pm_fatmult = 4; 551 pmp->pm_fatdiv = 1; 552 pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs); 553 if (getushort(b710->bpbExtFlags) & FATMIRROR) 554 pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM; 555 else 556 pmp->pm_flags |= MSDOSFS_FATMIRROR; 557 } else 558 pmp->pm_flags |= MSDOSFS_FATMIRROR; 559 560 /* 561 * Check a few values (could do some more): 562 * - logical sector size: power of 2, >= block size 563 * - sectors per cluster: power of 2, >= 1 564 * - number of sectors: >= 1, <= size of partition 565 * - number of FAT sectors: >= 1 566 */ 567 if ( (SecPerClust == 0) 568 || (SecPerClust & (SecPerClust - 1)) 569 || (pmp->pm_BytesPerSec < DEV_BSIZE) 570 || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1)) 571 || (pmp->pm_HugeSectors == 0) 572 || (pmp->pm_FATsecs == 0) 573 ) { 574 error = EINVAL; 575 goto error_exit; 576 } 577 578 pmp->pm_HugeSectors *= pmp->pm_BlkPerSec; 579 pmp->pm_HiddenSects *= pmp->pm_BlkPerSec; /* XXX not used? */ 580 pmp->pm_FATsecs *= pmp->pm_BlkPerSec; 581 SecPerClust *= pmp->pm_BlkPerSec; 582 583 pmp->pm_fatblk = pmp->pm_ResSectors * pmp->pm_BlkPerSec; 584 585 if (FAT32(pmp)) { 586 pmp->pm_rootdirblk = getulong(b710->bpbRootClust); 587 pmp->pm_firstcluster = pmp->pm_fatblk 588 + (pmp->pm_FATs * pmp->pm_FATsecs); 589 pmp->pm_fsinfo = getushort(b710->bpbFSInfo) * pmp->pm_BlkPerSec; 590 } else { 591 pmp->pm_rootdirblk = pmp->pm_fatblk + 592 (pmp->pm_FATs * pmp->pm_FATsecs); 593 pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry) 594 + DEV_BSIZE - 1) 595 / DEV_BSIZE; /* in blocks */ 596 pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize; 597 } 598 599 pmp->pm_maxcluster = (pmp->pm_HugeSectors - pmp->pm_firstcluster) / 600 SecPerClust + 1; 601 pmp->pm_fatsize = pmp->pm_FATsecs * DEV_BSIZE; /* XXX not used? */ 602 603 if (pmp->pm_fatmask == 0) { 604 if (pmp->pm_maxcluster 605 <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) { 606 /* 607 * This will usually be a floppy disk. This size makes 608 * sure that one fat entry will not be split across 609 * multiple blocks. 610 */ 611 pmp->pm_fatmask = FAT12_MASK; 612 pmp->pm_fatmult = 3; 613 pmp->pm_fatdiv = 2; 614 } else { 615 pmp->pm_fatmask = FAT16_MASK; 616 pmp->pm_fatmult = 2; 617 pmp->pm_fatdiv = 1; 618 } 619 } 620 621 clusters = (pmp->pm_fatsize / pmp->pm_fatmult) * pmp->pm_fatdiv; 622 if (pmp->pm_maxcluster >= clusters) { 623 printf("Warning: number of clusters (%ld) exceeds FAT " 624 "capacity (%ld)\n", pmp->pm_maxcluster + 1, clusters); 625 pmp->pm_maxcluster = clusters - 1; 626 } 627 628 629 if (FAT12(pmp)) 630 pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec; 631 else 632 pmp->pm_fatblocksize = MSDOSFS_DFLTBSIZE; 633 634 pmp->pm_fatblocksec = pmp->pm_fatblocksize / DEV_BSIZE; 635 pmp->pm_bnshift = ffs(DEV_BSIZE) - 1; 636 637 /* 638 * Compute mask and shift value for isolating cluster relative byte 639 * offsets and cluster numbers from a file offset. 640 */ 641 pmp->pm_bpcluster = SecPerClust * DEV_BSIZE; 642 pmp->pm_crbomask = pmp->pm_bpcluster - 1; 643 pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1; 644 645 /* 646 * Check for valid cluster size 647 * must be a power of 2 648 */ 649 if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) { 650 error = EINVAL; 651 goto error_exit; 652 } 653 654 /* 655 * Release the bootsector buffer. 656 */ 657 brelse(bp); 658 bp = NULL; 659 660 /* 661 * Check FSInfo. 662 */ 663 if (pmp->pm_fsinfo) { 664 struct fsinfo *fp; 665 666 if ((error = bread(devvp, pmp->pm_fsinfo, fsi_size(pmp), 667 NOCRED, &bp)) != 0) 668 goto error_exit; 669 fp = (struct fsinfo *)bp->b_data; 670 if (!bcmp(fp->fsisig1, "RRaA", 4) 671 && !bcmp(fp->fsisig2, "rrAa", 4) 672 && !bcmp(fp->fsisig3, "\0\0\125\252", 4) 673 && !bcmp(fp->fsisig4, "\0\0\125\252", 4)) { 674 pmp->pm_nxtfree = getulong(fp->fsinxtfree); 675 if (pmp->pm_nxtfree == 0xffffffff) 676 pmp->pm_nxtfree = CLUST_FIRST; 677 } else 678 pmp->pm_fsinfo = 0; 679 brelse(bp); 680 bp = NULL; 681 } 682 683 /* 684 * Check and validate (or perhaps invalidate?) the fsinfo structure? 685 */ 686 if (pmp->pm_fsinfo && pmp->pm_nxtfree > pmp->pm_maxcluster) { 687 printf( 688 "Next free cluster in FSInfo (%lu) exceeds maxcluster (%lu)\n", 689 pmp->pm_nxtfree, pmp->pm_maxcluster); 690 error = EINVAL; 691 goto error_exit; 692 } 693 694 /* 695 * Allocate memory for the bitmap of allocated clusters, and then 696 * fill it in. 697 */ 698 pmp->pm_inusemap = malloc(howmany(pmp->pm_maxcluster + 1, N_INUSEBITS) 699 * sizeof(*pmp->pm_inusemap), 700 M_MSDOSFSFAT, M_WAITOK); 701 702 /* 703 * fillinusemap() needs pm_devvp. 704 */ 705 pmp->pm_devvp = devvp; 706 707 /* 708 * Have the inuse map filled in. 709 */ 710 if ((error = fillinusemap(pmp)) != 0) 711 goto error_exit; 712 713 /* 714 * If they want fat updates to be synchronous then let them suffer 715 * the performance degradation in exchange for the on disk copy of 716 * the fat being correct just about all the time. I suppose this 717 * would be a good thing to turn on if the kernel is still flakey. 718 */ 719 if (mp->mnt_flag & MNT_SYNCHRONOUS) 720 pmp->pm_flags |= MSDOSFSMNT_WAITONFAT; 721 722 /* 723 * Finish up. 724 */ 725 if (ronly) 726 pmp->pm_flags |= MSDOSFSMNT_RONLY; 727 else { 728 /* Mark the volume dirty while it is mounted read/write. */ 729 if ((error = markvoldirty(pmp, 1)) != 0) 730 goto error_exit; 731 pmp->pm_fmod = 1; 732 } 733 mp->mnt_data = (qaddr_t) pmp; 734 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev); 735 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum; 736 MNT_ILOCK(mp); 737 mp->mnt_flag |= MNT_LOCAL; 738 MNT_IUNLOCK(mp); 739 740 if (pmp->pm_flags & MSDOSFS_LARGEFS) 741 msdosfs_fileno_init(mp); 742 743 return 0; 744 745error_exit: 746 if (bp) 747 brelse(bp); 748 if (cp != NULL) { 749 DROP_GIANT(); 750 g_topology_lock(); 751 g_vfs_close(cp, td); 752 g_topology_unlock(); 753 PICKUP_GIANT(); 754 } 755 if (pmp) { 756 if (pmp->pm_inusemap) 757 free(pmp->pm_inusemap, M_MSDOSFSFAT); 758 free(pmp, M_MSDOSFSMNT); 759 mp->mnt_data = (qaddr_t)0; 760 } 761 return (error); 762} 763 764/* 765 * Unmount the filesystem described by mp. 766 */ 767static int 768msdosfs_unmount(mp, mntflags, td) 769 struct mount *mp; 770 int mntflags; 771 struct thread *td; 772{ 773 struct msdosfsmount *pmp; 774 int error, flags; 775 776 flags = 0; 777 if (mntflags & MNT_FORCE) 778 flags |= FORCECLOSE; 779 error = vflush(mp, 0, flags, td); 780 if (error) 781 return error; 782 pmp = VFSTOMSDOSFS(mp); 783 if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) { 784 if (pmp->pm_w2u) 785 msdosfs_iconv->close(pmp->pm_w2u); 786 if (pmp->pm_u2w) 787 msdosfs_iconv->close(pmp->pm_u2w); 788 if (pmp->pm_d2u) 789 msdosfs_iconv->close(pmp->pm_d2u); 790 if (pmp->pm_u2d) 791 msdosfs_iconv->close(pmp->pm_u2d); 792 } 793 794 /* If the volume was mounted read/write, mark it clean now. */ 795 if ((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0) { 796 error = markvoldirty(pmp, 0); 797 if (error && (flags & FORCECLOSE) == 0) 798 return (error); 799 } 800#ifdef MSDOSFS_DEBUG 801 { 802 struct vnode *vp = pmp->pm_devvp; 803 804 VI_LOCK(vp); 805 vn_printf(vp, 806 "msdosfs_umount(): just before calling VOP_CLOSE()\n"); 807 printf("freef %p, freeb %p, mount %p\n", 808 TAILQ_NEXT(vp, v_freelist), vp->v_freelist.tqe_prev, 809 vp->v_mount); 810 printf("cleanblkhd %p, dirtyblkhd %p, numoutput %ld, type %d\n", 811 TAILQ_FIRST(&vp->v_bufobj.bo_clean.bv_hd), 812 TAILQ_FIRST(&vp->v_bufobj.bo_dirty.bv_hd), 813 vp->v_bufobj.bo_numoutput, vp->v_type); 814 VI_UNLOCK(vp); 815 } 816#endif 817 DROP_GIANT(); 818 g_topology_lock(); 819 g_vfs_close(pmp->pm_cp, td); 820 g_topology_unlock(); 821 PICKUP_GIANT(); 822 vrele(pmp->pm_devvp); 823 free(pmp->pm_inusemap, M_MSDOSFSFAT); 824 if (pmp->pm_flags & MSDOSFS_LARGEFS) { 825 msdosfs_fileno_free(mp); 826 } 827 free(pmp, M_MSDOSFSMNT); 828 mp->mnt_data = (qaddr_t)0; 829 MNT_ILOCK(mp); 830 mp->mnt_flag &= ~MNT_LOCAL; 831 MNT_IUNLOCK(mp); 832 return (error); 833} 834 835static int 836msdosfs_root(mp, flags, vpp, td) 837 struct mount *mp; 838 int flags; 839 struct vnode **vpp; 840 struct thread *td; 841{ 842 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 843 struct denode *ndep; 844 int error; 845 846#ifdef MSDOSFS_DEBUG 847 printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp); 848#endif 849 error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep); 850 if (error) 851 return (error); 852 *vpp = DETOV(ndep); 853 return (0); 854} 855 856static int 857msdosfs_statfs(mp, sbp, td) 858 struct mount *mp; 859 struct statfs *sbp; 860 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 875static int 876msdosfs_sync(mp, waitfor, td) 877 struct mount *mp; 878 int waitfor; 879 struct thread *td; 880{ 881 struct vnode *vp, *nvp; 882 struct denode *dep; 883 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 884 int error, allerror = 0; 885 886 /* 887 * If we ever switch to not updating all of the fats all the time, 888 * this would be the place to update them from the first one. 889 */ 890 if (pmp->pm_fmod != 0) { 891 if (pmp->pm_flags & MSDOSFSMNT_RONLY) 892 panic("msdosfs_sync: rofs mod"); 893 else { 894 /* update fats here */ 895 } 896 } 897 /* 898 * Write back each (modified) denode. 899 */ 900 MNT_ILOCK(mp); 901loop: 902 MNT_VNODE_FOREACH(vp, mp, nvp) { 903 VI_LOCK(vp); 904 if (vp->v_type == VNON || (vp->v_iflag & VI_DOOMED)) { 905 VI_UNLOCK(vp); 906 continue; 907 } 908 MNT_IUNLOCK(mp); 909 dep = VTODE(vp); 910 if ((dep->de_flag & 911 (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 && 912 (vp->v_bufobj.bo_dirty.bv_cnt == 0 || 913 waitfor == MNT_LAZY)) { 914 VI_UNLOCK(vp); 915 MNT_ILOCK(mp); 916 continue; 917 } 918 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, td); 919 if (error) { 920 MNT_ILOCK(mp); 921 if (error == ENOENT) 922 goto loop; 923 continue; 924 } 925 error = VOP_FSYNC(vp, waitfor, td); 926 if (error) 927 allerror = error; 928 VOP_UNLOCK(vp, 0, td); 929 vrele(vp); 930 MNT_ILOCK(mp); 931 } 932 MNT_IUNLOCK(mp); 933 934 /* 935 * Flush filesystem control info. 936 */ 937 if (waitfor != MNT_LAZY) { 938 vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY, td); 939 error = VOP_FSYNC(pmp->pm_devvp, waitfor, td); 940 if (error) 941 allerror = error; 942 VOP_UNLOCK(pmp->pm_devvp, 0, td); 943 } 944 return (allerror); 945} 946 947static int 948msdosfs_fhtovp(mp, fhp, vpp) 949 struct mount *mp; 950 struct fid *fhp; 951 struct vnode **vpp; 952{ 953 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 954 struct defid *defhp = (struct defid *) fhp; 955 struct denode *dep; 956 int error; 957 958 error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep); 959 if (error) { 960 *vpp = NULLVP; 961 return (error); 962 } 963 *vpp = DETOV(dep); 964 vnode_create_vobject(*vpp, dep->de_FileSize, curthread); 965 return (0); 966} 967 968static int 969msdosfs_vptofh(vp, fhp) 970 struct vnode *vp; 971 struct fid *fhp; 972{ 973 struct denode *dep; 974 struct defid *defhp; 975 976 dep = VTODE(vp); 977 defhp = (struct defid *)fhp; 978 defhp->defid_len = sizeof(struct defid); 979 defhp->defid_dirclust = dep->de_dirclust; 980 defhp->defid_dirofs = dep->de_diroffset; 981 /* defhp->defid_gen = dep->de_gen; */ 982 return (0); 983} 984 985static struct vfsops msdosfs_vfsops = { 986 .vfs_fhtovp = msdosfs_fhtovp, 987 .vfs_mount = msdosfs_mount, 988 .vfs_cmount = msdosfs_cmount, 989 .vfs_root = msdosfs_root, 990 .vfs_statfs = msdosfs_statfs, 991 .vfs_sync = msdosfs_sync, 992 .vfs_unmount = msdosfs_unmount, 993 .vfs_vptofh = msdosfs_vptofh, 994}; 995 996VFS_SET(msdosfs_vfsops, msdosfs, 0); 997MODULE_VERSION(msdosfs, 1); 998