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