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