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