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