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