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