msdosfs_vfsops.c revision 171408
1/* $FreeBSD: head/sys/fs/msdosfs/msdosfs_vfsops.c 171408 2007-07-12 17:17:47Z bde $ */ 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/* List of mount options we support */ 79static const char *msdosfs_opts[] = { 80 "from", 81 "atime", "export", "force", "sync", 82 "uid", "gid", "mask", "dirmask", 83 "shortname", "shortnames", "longname", "longnames", "nowin95", "win95", 84 "kiconv", "cs_win", "cs_dos", "cs_local", "large", 85 NULL 86}; 87 88#if 1 /*def PC98*/ 89/* 90 * XXX - The boot signature formatted by NEC PC-98 DOS looks like a 91 * garbage or a random value :-{ 92 * If you want to use that broken-signatured media, define the 93 * following symbol even though PC/AT. 94 * (ex. mount PC-98 DOS formatted FD on PC/AT) 95 */ 96#define MSDOSFS_NOCHECKSIG 97#endif 98 99MALLOC_DEFINE(M_MSDOSFSMNT, "msdosfs_mount", "MSDOSFS mount structure"); 100static MALLOC_DEFINE(M_MSDOSFSFAT, "msdosfs_fat", "MSDOSFS file allocation table"); 101 102struct iconv_functions *msdosfs_iconv = NULL; 103 104static int update_mp(struct mount *mp, struct thread *td); 105static int mountmsdosfs(struct vnode *devvp, struct mount *mp, 106 struct thread *td); 107static vfs_fhtovp_t msdosfs_fhtovp; 108static vfs_mount_t msdosfs_mount; 109static vfs_root_t msdosfs_root; 110static vfs_statfs_t msdosfs_statfs; 111static vfs_sync_t msdosfs_sync; 112static vfs_unmount_t msdosfs_unmount; 113 114/* Maximum length of a character set name (arbitrary). */ 115#define MAXCSLEN 64 116 117static int 118update_mp(struct mount *mp, struct thread *td) 119{ 120 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 121 void *dos, *win, *local; 122 int error, v; 123 124 if (!vfs_getopt(mp->mnt_optnew, "kiconv", NULL, NULL)) { 125 if (msdosfs_iconv != NULL) { 126 error = vfs_getopt(mp->mnt_optnew, 127 "cs_win", &win, NULL); 128 if (!error) 129 error = vfs_getopt(mp->mnt_optnew, 130 "cs_local", &local, NULL); 131 if (!error) 132 error = vfs_getopt(mp->mnt_optnew, 133 "cs_dos", &dos, NULL); 134 if (!error) { 135 msdosfs_iconv->open(win, local, &pmp->pm_u2w); 136 msdosfs_iconv->open(local, win, &pmp->pm_w2u); 137 msdosfs_iconv->open(dos, local, &pmp->pm_u2d); 138 msdosfs_iconv->open(local, dos, &pmp->pm_d2u); 139 } 140 if (error != 0) 141 return (error); 142 } else { 143 pmp->pm_w2u = NULL; 144 pmp->pm_u2w = NULL; 145 pmp->pm_d2u = NULL; 146 pmp->pm_u2d = NULL; 147 } 148 } 149 150 if (1 == vfs_scanopt(mp->mnt_optnew, "gid", "%d", &v)) 151 pmp->pm_gid = v; 152 if (1 == vfs_scanopt(mp->mnt_optnew, "uid", "%d", &v)) 153 pmp->pm_uid = v; 154 if (1 == vfs_scanopt(mp->mnt_optnew, "mask", "%d", &v)) 155 pmp->pm_mask = v & ALLPERMS; 156 if (1 == vfs_scanopt(mp->mnt_optnew, "dirmask", "%d", &v)) 157 pmp->pm_dirmask = v & ALLPERMS; 158 vfs_flagopt(mp->mnt_optnew, "shortname", 159 &pmp->pm_flags, MSDOSFSMNT_SHORTNAME); 160 vfs_flagopt(mp->mnt_optnew, "shortnames", 161 &pmp->pm_flags, MSDOSFSMNT_SHORTNAME); 162 vfs_flagopt(mp->mnt_optnew, "longname", 163 &pmp->pm_flags, MSDOSFSMNT_LONGNAME); 164 vfs_flagopt(mp->mnt_optnew, "longnames", 165 &pmp->pm_flags, MSDOSFSMNT_LONGNAME); 166 vfs_flagopt(mp->mnt_optnew, "kiconv", 167 &pmp->pm_flags, MSDOSFSMNT_KICONV); 168 169 if (vfs_getopt(mp->mnt_optnew, "nowin95", NULL, NULL) == 0) 170 pmp->pm_flags |= MSDOSFSMNT_NOWIN95; 171 else 172 pmp->pm_flags &= ~MSDOSFSMNT_NOWIN95; 173 174 if (pmp->pm_flags & MSDOSFSMNT_NOWIN95) 175 pmp->pm_flags |= MSDOSFSMNT_SHORTNAME; 176 else if (!(pmp->pm_flags & 177 (MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) { 178 struct vnode *rootvp; 179 180 /* 181 * Try to divine whether to support Win'95 long filenames 182 */ 183 if (FAT32(pmp)) 184 pmp->pm_flags |= MSDOSFSMNT_LONGNAME; 185 else { 186 if ((error = 187 msdosfs_root(mp, LK_EXCLUSIVE, &rootvp, td)) != 0) 188 return error; 189 pmp->pm_flags |= findwin95(VTODE(rootvp)) 190 ? MSDOSFSMNT_LONGNAME 191 : MSDOSFSMNT_SHORTNAME; 192 vput(rootvp); 193 } 194 } 195 return 0; 196} 197 198static int 199msdosfs_cmount(struct mntarg *ma, void *data, int flags, struct thread *td) 200{ 201 struct msdosfs_args args; 202 int error; 203 204 if (data == NULL) 205 return (EINVAL); 206 error = copyin(data, &args, sizeof args); 207 if (error) 208 return (error); 209 210 ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN); 211 ma = mount_arg(ma, "export", &args.export, sizeof args.export); 212 ma = mount_argf(ma, "uid", "%d", args.uid); 213 ma = mount_argf(ma, "gid", "%d", args.gid); 214 ma = mount_argf(ma, "mask", "%d", args.mask); 215 ma = mount_argf(ma, "dirmask", "%d", args.dirmask); 216 217 ma = mount_argb(ma, args.flags & MSDOSFSMNT_SHORTNAME, "noshortname"); 218 ma = mount_argb(ma, args.flags & MSDOSFSMNT_LONGNAME, "nolongname"); 219 ma = mount_argb(ma, !(args.flags & MSDOSFSMNT_NOWIN95), "nowin95"); 220 ma = mount_argb(ma, args.flags & MSDOSFSMNT_KICONV, "nokiconv"); 221 222 ma = mount_argsu(ma, "cs_win", args.cs_win, MAXCSLEN); 223 ma = mount_argsu(ma, "cs_dos", args.cs_dos, MAXCSLEN); 224 ma = mount_argsu(ma, "cs_local", args.cs_local, MAXCSLEN); 225 226 error = kernel_mount(ma, flags); 227 228 return (error); 229} 230 231/* 232 * mp - path - addr in user space of mount point (ie /usr or whatever) 233 * data - addr in user space of mount params including the name of the block 234 * special file to treat as a filesystem. 235 */ 236static int 237msdosfs_mount(struct mount *mp, struct thread *td) 238{ 239 struct vnode *devvp; /* vnode for blk device to mount */ 240 /* msdosfs specific mount control block */ 241 struct msdosfsmount *pmp = NULL; 242 struct nameidata ndp; 243 int error, flags; 244 mode_t accessmode; 245 char *from; 246 247 if (vfs_filteropt(mp->mnt_optnew, msdosfs_opts)) 248 return (EINVAL); 249 250 /* 251 * If updating, check whether changing from read-only to 252 * read/write; if there is no device name, that's all we do. 253 */ 254 if (mp->mnt_flag & MNT_UPDATE) { 255 int ro_to_rw = 0; 256 pmp = VFSTOMSDOSFS(mp); 257 258 if (vfs_flagopt(mp->mnt_optnew, "export", NULL, 0)) { 259 /* 260 * Forbid export requests if filesystem has 261 * MSDOSFS_LARGEFS flag set. 262 */ 263 if ((pmp->pm_flags & MSDOSFS_LARGEFS) != 0) { 264 vfs_mount_error(mp, 265 "MSDOSFS_LARGEFS flag set, cannot export"); 266 return (EOPNOTSUPP); 267 } 268 } 269 if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) && 270 vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) { 271 error = VFS_SYNC(mp, MNT_WAIT, td); 272 if (error) 273 return (error); 274 flags = WRITECLOSE; 275 if (mp->mnt_flag & MNT_FORCE) 276 flags |= FORCECLOSE; 277 error = vflush(mp, 0, flags, td); 278 if (error) 279 return (error); 280 DROP_GIANT(); 281 g_topology_lock(); 282 g_access(pmp->pm_cp, 0, -1, 0); 283 g_topology_unlock(); 284 PICKUP_GIANT(); 285 /* Now the volume is clean. Mark it. */ 286 error = markvoldirty(pmp, 0); 287 if (error && (flags & FORCECLOSE) == 0) 288 return (error); 289 } else if ((pmp->pm_flags & MSDOSFSMNT_RONLY) && 290 !vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) { 291 /* 292 * If upgrade to read-write by non-root, then verify 293 * that user has necessary permissions on the device. 294 */ 295 devvp = pmp->pm_devvp; 296 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td); 297 error = VOP_ACCESS(devvp, VREAD | VWRITE, 298 td->td_ucred, td); 299 if (error) 300 error = priv_check(td, PRIV_VFS_MOUNT_PERM); 301 if (error) { 302 VOP_UNLOCK(devvp, 0, td); 303 return (error); 304 } 305 VOP_UNLOCK(devvp, 0, td); 306 DROP_GIANT(); 307 g_topology_lock(); 308 error = g_access(pmp->pm_cp, 0, 1, 0); 309 g_topology_unlock(); 310 PICKUP_GIANT(); 311 if (error) 312 return (error); 313 314 ro_to_rw = 1; 315 } 316 vfs_flagopt(mp->mnt_optnew, "ro", 317 &pmp->pm_flags, MSDOSFSMNT_RONLY); 318 vfs_flagopt(mp->mnt_optnew, "ro", 319 &mp->mnt_flag, MNT_RDONLY); 320 321 if (ro_to_rw) { 322 /* Now that the volume is modifiable, mark it dirty. */ 323 error = markvoldirty(pmp, 1); 324 if (error) 325 return (error); 326 } 327 } 328 /* 329 * Not an update, or updating the name: look up the name 330 * and verify that it refers to a sensible disk device. 331 */ 332 if (vfs_getopt(mp->mnt_optnew, "from", (void **)&from, NULL)) 333 return (EINVAL); 334 NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, from, td); 335 error = namei(&ndp); 336 if (error) 337 return (error); 338 devvp = ndp.ni_vp; 339 NDFREE(&ndp, NDF_ONLY_PNBUF); 340 341 if (!vn_isdisk(devvp, &error)) { 342 vput(devvp); 343 return (error); 344 } 345 /* 346 * If mount by non-root, then verify that user has necessary 347 * permissions on the device. 348 */ 349 accessmode = VREAD; 350 if ((mp->mnt_flag & MNT_RDONLY) == 0) 351 accessmode |= VWRITE; 352 error = VOP_ACCESS(devvp, accessmode, td->td_ucred, td); 353 if (error) 354 error = priv_check(td, PRIV_VFS_MOUNT_PERM); 355 if (error) { 356 vput(devvp); 357 return (error); 358 } 359 if ((mp->mnt_flag & MNT_UPDATE) == 0) { 360 error = mountmsdosfs(devvp, mp, td); 361#ifdef MSDOSFS_DEBUG /* only needed for the printf below */ 362 pmp = VFSTOMSDOSFS(mp); 363#endif 364 } else { 365 if (devvp != pmp->pm_devvp) 366 error = EINVAL; /* XXX needs translation */ 367 else 368 vput(devvp); 369 } 370 if (error) { 371 vrele(devvp); 372 return (error); 373 } 374 375 error = update_mp(mp, td); 376 if (error) { 377 if ((mp->mnt_flag & MNT_UPDATE) == 0) 378 msdosfs_unmount(mp, MNT_FORCE, td); 379 return error; 380 } 381 382 vfs_mountedfrom(mp, from); 383#ifdef MSDOSFS_DEBUG 384 printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap); 385#endif 386 return (0); 387} 388 389static int 390mountmsdosfs(struct vnode *devvp, struct mount *mp, struct thread *td) 391{ 392 struct msdosfsmount *pmp; 393 struct buf *bp; 394 struct cdev *dev = devvp->v_rdev; 395 union bootsector *bsp; 396 struct byte_bpb33 *b33; 397 struct byte_bpb50 *b50; 398 struct byte_bpb710 *b710; 399 u_int8_t SecPerClust; 400 u_long clusters; 401 int ronly, error; 402 struct g_consumer *cp; 403 struct bufobj *bo; 404 405 ronly = !vfs_getopt(mp->mnt_optnew, "ro", NULL, NULL); 406 /* XXX: use VOP_ACCESS to check FS perms */ 407 DROP_GIANT(); 408 g_topology_lock(); 409 error = g_vfs_open(devvp, &cp, "msdos", ronly ? 0 : 1); 410 g_topology_unlock(); 411 PICKUP_GIANT(); 412 VOP_UNLOCK(devvp, 0, td); 413 if (error) 414 return (error); 415 416 bo = &devvp->v_bufobj; 417 bp = NULL; /* both used in error_exit */ 418 pmp = NULL; 419 420 /* 421 * Read the boot sector of the filesystem, and then check the 422 * boot signature. If not a dos boot sector then error out. 423 * 424 * NOTE: 8192 is a magic size that works for ffs. 425 */ 426 error = bread(devvp, 0, 8192, NOCRED, &bp); 427 if (error) 428 goto error_exit; 429 bp->b_flags |= B_AGE; 430 bsp = (union bootsector *)bp->b_data; 431 b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB; 432 b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB; 433 b710 = (struct byte_bpb710 *)bsp->bs710.bsBPB; 434 435#ifndef MSDOSFS_NOCHECKSIG 436 if (bsp->bs50.bsBootSectSig0 != BOOTSIG0 437 || bsp->bs50.bsBootSectSig1 != BOOTSIG1) { 438 error = EINVAL; 439 goto error_exit; 440 } 441#endif 442 443 pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK | M_ZERO); 444 pmp->pm_mountp = mp; 445 pmp->pm_cp = cp; 446 pmp->pm_bo = bo; 447 448 /* 449 * Experimental support for large MS-DOS filesystems. 450 * WARNING: This uses at least 32 bytes of kernel memory (which is not 451 * reclaimed until the FS is unmounted) for each file on disk to map 452 * between the 32-bit inode numbers used by VFS and the 64-bit 453 * pseudo-inode numbers used internally by msdosfs. This is only 454 * safe to use in certain controlled situations (e.g. read-only FS 455 * with less than 1 million files). 456 * Since the mappings do not persist across unmounts (or reboots), these 457 * filesystems are not suitable for exporting through NFS, or any other 458 * application that requires fixed inode numbers. 459 */ 460 vfs_flagopt(mp->mnt_optnew, "large", &pmp->pm_flags, 461 MSDOSFS_LARGEFS); 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 if (!(pmp->pm_flags & MSDOSFS_LARGEFS)) { 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 vfs_mount_error(mp, 515 "Disk too big, try '-o large' mount option"); 516 goto error_exit; 517 } 518 } 519 520 if (pmp->pm_RootDirEnts == 0) { 521 if (pmp->pm_Sectors 522 || pmp->pm_FATsecs 523 || getushort(b710->bpbFSVers)) { 524 error = EINVAL; 525 printf("mountmsdosfs(): bad FAT32 filesystem\n"); 526 goto error_exit; 527 } 528 pmp->pm_fatmask = FAT32_MASK; 529 pmp->pm_fatmult = 4; 530 pmp->pm_fatdiv = 1; 531 pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs); 532 if (getushort(b710->bpbExtFlags) & FATMIRROR) 533 pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM; 534 else 535 pmp->pm_flags |= MSDOSFS_FATMIRROR; 536 } else 537 pmp->pm_flags |= MSDOSFS_FATMIRROR; 538 539 /* 540 * Check a few values (could do some more): 541 * - logical sector size: power of 2, >= block size 542 * - sectors per cluster: power of 2, >= 1 543 * - number of sectors: >= 1, <= size of partition 544 * - number of FAT sectors: >= 1 545 */ 546 if ( (SecPerClust == 0) 547 || (SecPerClust & (SecPerClust - 1)) 548 || (pmp->pm_BytesPerSec < DEV_BSIZE) 549 || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1)) 550 || (pmp->pm_HugeSectors == 0) 551 || (pmp->pm_FATsecs == 0) 552 ) { 553 error = EINVAL; 554 goto error_exit; 555 } 556 557 pmp->pm_HugeSectors *= pmp->pm_BlkPerSec; 558 pmp->pm_HiddenSects *= pmp->pm_BlkPerSec; /* XXX not used? */ 559 pmp->pm_FATsecs *= pmp->pm_BlkPerSec; 560 SecPerClust *= pmp->pm_BlkPerSec; 561 562 pmp->pm_fatblk = pmp->pm_ResSectors * pmp->pm_BlkPerSec; 563 564 if (FAT32(pmp)) { 565 pmp->pm_rootdirblk = getulong(b710->bpbRootClust); 566 pmp->pm_firstcluster = pmp->pm_fatblk 567 + (pmp->pm_FATs * pmp->pm_FATsecs); 568 pmp->pm_fsinfo = getushort(b710->bpbFSInfo) * pmp->pm_BlkPerSec; 569 } else { 570 pmp->pm_rootdirblk = pmp->pm_fatblk + 571 (pmp->pm_FATs * pmp->pm_FATsecs); 572 pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry) 573 + DEV_BSIZE - 1) 574 / DEV_BSIZE; /* in blocks */ 575 pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize; 576 } 577 578 pmp->pm_maxcluster = (pmp->pm_HugeSectors - pmp->pm_firstcluster) / 579 SecPerClust + 1; 580 pmp->pm_fatsize = pmp->pm_FATsecs * DEV_BSIZE; /* XXX not used? */ 581 582 if (pmp->pm_fatmask == 0) { 583 if (pmp->pm_maxcluster 584 <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) { 585 /* 586 * This will usually be a floppy disk. This size makes 587 * sure that one fat entry will not be split across 588 * multiple blocks. 589 */ 590 pmp->pm_fatmask = FAT12_MASK; 591 pmp->pm_fatmult = 3; 592 pmp->pm_fatdiv = 2; 593 } else { 594 pmp->pm_fatmask = FAT16_MASK; 595 pmp->pm_fatmult = 2; 596 pmp->pm_fatdiv = 1; 597 } 598 } 599 600 clusters = (pmp->pm_fatsize / pmp->pm_fatmult) * pmp->pm_fatdiv; 601 if (pmp->pm_maxcluster >= clusters) { 602 printf("Warning: number of clusters (%ld) exceeds FAT " 603 "capacity (%ld)\n", pmp->pm_maxcluster + 1, clusters); 604 pmp->pm_maxcluster = clusters - 1; 605 } 606 607 if (FAT12(pmp)) 608 pmp->pm_fatblocksize = 3 * 512; 609 else 610 pmp->pm_fatblocksize = PAGE_SIZE; 611 pmp->pm_fatblocksize = roundup(pmp->pm_fatblocksize, 612 pmp->pm_BytesPerSec); 613 pmp->pm_fatblocksec = pmp->pm_fatblocksize / DEV_BSIZE; 614 pmp->pm_bnshift = ffs(DEV_BSIZE) - 1; 615 616 /* 617 * Compute mask and shift value for isolating cluster relative byte 618 * offsets and cluster numbers from a file offset. 619 */ 620 pmp->pm_bpcluster = SecPerClust * DEV_BSIZE; 621 pmp->pm_crbomask = pmp->pm_bpcluster - 1; 622 pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1; 623 624 /* 625 * Check for valid cluster size 626 * must be a power of 2 627 */ 628 if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) { 629 error = EINVAL; 630 goto error_exit; 631 } 632 633 /* 634 * Release the bootsector buffer. 635 */ 636 brelse(bp); 637 bp = NULL; 638 639 /* 640 * Check FSInfo. 641 */ 642 if (pmp->pm_fsinfo) { 643 struct fsinfo *fp; 644 645 if ((error = bread(devvp, pmp->pm_fsinfo, pmp->pm_bpcluster, 646 NOCRED, &bp)) != 0) 647 goto error_exit; 648 fp = (struct fsinfo *)bp->b_data; 649 if (!bcmp(fp->fsisig1, "RRaA", 4) 650 && !bcmp(fp->fsisig2, "rrAa", 4) 651 && !bcmp(fp->fsisig3, "\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 if (pmp->pm_flags & MSDOSFS_LARGEFS) 719 msdosfs_fileno_init(mp); 720 721 return 0; 722 723error_exit: 724 if (bp) 725 brelse(bp); 726 if (cp != NULL) { 727 DROP_GIANT(); 728 g_topology_lock(); 729 g_vfs_close(cp, td); 730 g_topology_unlock(); 731 PICKUP_GIANT(); 732 } 733 if (pmp) { 734 if (pmp->pm_inusemap) 735 free(pmp->pm_inusemap, M_MSDOSFSFAT); 736 free(pmp, M_MSDOSFSMNT); 737 mp->mnt_data = (qaddr_t)0; 738 } 739 return (error); 740} 741 742/* 743 * Unmount the filesystem described by mp. 744 */ 745static int 746msdosfs_unmount(struct mount *mp, int mntflags, struct thread *td) 747{ 748 struct msdosfsmount *pmp; 749 int error, flags; 750 751 flags = 0; 752 if (mntflags & MNT_FORCE) 753 flags |= FORCECLOSE; 754 error = vflush(mp, 0, flags, td); 755 if (error) 756 return error; 757 pmp = VFSTOMSDOSFS(mp); 758 if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) { 759 if (pmp->pm_w2u) 760 msdosfs_iconv->close(pmp->pm_w2u); 761 if (pmp->pm_u2w) 762 msdosfs_iconv->close(pmp->pm_u2w); 763 if (pmp->pm_d2u) 764 msdosfs_iconv->close(pmp->pm_d2u); 765 if (pmp->pm_u2d) 766 msdosfs_iconv->close(pmp->pm_u2d); 767 } 768 769 /* If the volume was mounted read/write, mark it clean now. */ 770 if ((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0) { 771 error = markvoldirty(pmp, 0); 772 if (error && (flags & FORCECLOSE) == 0) 773 return (error); 774 } 775#ifdef MSDOSFS_DEBUG 776 { 777 struct vnode *vp = pmp->pm_devvp; 778 779 VI_LOCK(vp); 780 vn_printf(vp, 781 "msdosfs_umount(): just before calling VOP_CLOSE()\n"); 782 printf("freef %p, freeb %p, mount %p\n", 783 TAILQ_NEXT(vp, v_freelist), vp->v_freelist.tqe_prev, 784 vp->v_mount); 785 printf("cleanblkhd %p, dirtyblkhd %p, numoutput %ld, type %d\n", 786 TAILQ_FIRST(&vp->v_bufobj.bo_clean.bv_hd), 787 TAILQ_FIRST(&vp->v_bufobj.bo_dirty.bv_hd), 788 vp->v_bufobj.bo_numoutput, vp->v_type); 789 VI_UNLOCK(vp); 790 } 791#endif 792 DROP_GIANT(); 793 g_topology_lock(); 794 g_vfs_close(pmp->pm_cp, td); 795 g_topology_unlock(); 796 PICKUP_GIANT(); 797 vrele(pmp->pm_devvp); 798 free(pmp->pm_inusemap, M_MSDOSFSFAT); 799 if (pmp->pm_flags & MSDOSFS_LARGEFS) { 800 msdosfs_fileno_free(mp); 801 } 802 free(pmp, M_MSDOSFSMNT); 803 mp->mnt_data = (qaddr_t)0; 804 MNT_ILOCK(mp); 805 mp->mnt_flag &= ~MNT_LOCAL; 806 MNT_IUNLOCK(mp); 807 return (error); 808} 809 810static int 811msdosfs_root(struct mount *mp, int flags, struct vnode **vpp, 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 827static int 828msdosfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td) 829{ 830 struct msdosfsmount *pmp; 831 832 pmp = VFSTOMSDOSFS(mp); 833 sbp->f_bsize = pmp->pm_bpcluster; 834 sbp->f_iosize = pmp->pm_bpcluster; 835 sbp->f_blocks = pmp->pm_maxcluster + 1; 836 sbp->f_bfree = pmp->pm_freeclustercount; 837 sbp->f_bavail = pmp->pm_freeclustercount; 838 sbp->f_files = pmp->pm_RootDirEnts; /* XXX */ 839 sbp->f_ffree = 0; /* what to put in here? */ 840 return (0); 841} 842 843static int 844msdosfs_sync(struct mount *mp, int waitfor, struct thread *td) 845{ 846 struct vnode *vp, *nvp; 847 struct denode *dep; 848 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 849 int error, allerror = 0; 850 851 /* 852 * If we ever switch to not updating all of the fats all the time, 853 * this would be the place to update them from the first one. 854 */ 855 if (pmp->pm_fmod != 0) { 856 if (pmp->pm_flags & MSDOSFSMNT_RONLY) 857 panic("msdosfs_sync: rofs mod"); 858 else { 859 /* update fats here */ 860 } 861 } 862 /* 863 * Write back each (modified) denode. 864 */ 865 MNT_ILOCK(mp); 866loop: 867 MNT_VNODE_FOREACH(vp, mp, nvp) { 868 VI_LOCK(vp); 869 if (vp->v_type == VNON || (vp->v_iflag & VI_DOOMED)) { 870 VI_UNLOCK(vp); 871 continue; 872 } 873 MNT_IUNLOCK(mp); 874 dep = VTODE(vp); 875 if ((dep->de_flag & 876 (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 && 877 (vp->v_bufobj.bo_dirty.bv_cnt == 0 || 878 waitfor == MNT_LAZY)) { 879 VI_UNLOCK(vp); 880 MNT_ILOCK(mp); 881 continue; 882 } 883 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, td); 884 if (error) { 885 MNT_ILOCK(mp); 886 if (error == ENOENT) 887 goto loop; 888 continue; 889 } 890 error = VOP_FSYNC(vp, waitfor, td); 891 if (error) 892 allerror = error; 893 VOP_UNLOCK(vp, 0, td); 894 vrele(vp); 895 MNT_ILOCK(mp); 896 } 897 MNT_IUNLOCK(mp); 898 899 /* 900 * Flush filesystem control info. 901 */ 902 if (waitfor != MNT_LAZY) { 903 vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY, td); 904 error = VOP_FSYNC(pmp->pm_devvp, waitfor, td); 905 if (error) 906 allerror = error; 907 VOP_UNLOCK(pmp->pm_devvp, 0, td); 908 } 909 return (allerror); 910} 911 912static int 913msdosfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp) 914{ 915 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 916 struct defid *defhp = (struct defid *) fhp; 917 struct denode *dep; 918 int error; 919 920 error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep); 921 if (error) { 922 *vpp = NULLVP; 923 return (error); 924 } 925 *vpp = DETOV(dep); 926 vnode_create_vobject(*vpp, dep->de_FileSize, curthread); 927 return (0); 928} 929 930static struct vfsops msdosfs_vfsops = { 931 .vfs_fhtovp = msdosfs_fhtovp, 932 .vfs_mount = msdosfs_mount, 933 .vfs_cmount = msdosfs_cmount, 934 .vfs_root = msdosfs_root, 935 .vfs_statfs = msdosfs_statfs, 936 .vfs_sync = msdosfs_sync, 937 .vfs_unmount = msdosfs_unmount, 938}; 939 940VFS_SET(msdosfs_vfsops, msdosfs, 0); 941MODULE_VERSION(msdosfs, 1); 942