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