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