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