Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / vfs / msdosfs / msdosfs_vfsops.c
1 /* $FreeBSD: src/sys/msdosfs/msdosfs_vfsops.c,v 1.60.2.6 2002/09/12 21:33:38 trhodes Exp $ */
2 /*      $NetBSD: msdosfs_vfsops.c,v 1.51 1997/11/17 15:36:58 ws Exp $   */
3
4 /*-
5  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
6  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
7  * All rights reserved.
8  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by TooLs GmbH.
21  * 4. The name of TooLs GmbH may not be used to endorse or promote products
22  *    derived from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
30  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 /*
36  * Written by Paul Popelka (paulp@uts.amdahl.com)
37  *
38  * You can do anything you want with this software, just don't say you wrote
39  * it, and don't remove this notice.
40  *
41  * This software is provided "as is".
42  *
43  * The author supplies this software to be publicly redistributed on the
44  * understanding that the author is not responsible for the correct
45  * functioning of this software in any circumstances and is not liable for
46  * any damages caused by this software.
47  *
48  * October 1992
49  */
50
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/conf.h>
54 #include <sys/namei.h>
55 #include <sys/proc.h>
56 #include <sys/kernel.h>
57 #include <sys/vnode.h>
58 #include <sys/mount.h>
59 #include <sys/buf.h>
60 #include <sys/fcntl.h>
61 #include <sys/malloc.h>
62 #include <sys/stat.h>                           /* defines ALLPERMS */
63 #include <vm/vm_zone.h>
64
65 #include <msdosfs/bpb.h>
66 #include <msdosfs/bootsect.h>
67 #include <msdosfs/direntry.h>
68 #include <msdosfs/denode.h>
69 #include <msdosfs/msdosfsmount.h>
70 #include <msdosfs/fat.h>
71
72 #define MSDOSFS_DFLTBSIZE       4096
73
74 #if 1 /*def PC98*/
75 /*
76  * XXX - The boot signature formatted by NEC PC-98 DOS looks like a
77  *       garbage or a random value :-{
78  *       If you want to use that broken-signatured media, define the
79  *       following symbol even though PC/AT.
80  *       (ex. mount PC-98 DOS formatted FD on PC/AT)
81  */
82 #define MSDOSFS_NOCHECKSIG
83 #endif
84
85 MALLOC_DEFINE(M_MSDOSFSMNT, "MSDOSFS mount", "MSDOSFS mount structure");
86 static MALLOC_DEFINE(M_MSDOSFSFAT, "MSDOSFS FAT", "MSDOSFS file allocation table");
87
88 static int      update_mp __P((struct mount *mp, struct msdosfs_args *argp));
89 static int      mountmsdosfs __P((struct vnode *devvp, struct mount *mp,
90                                   struct proc *p, struct msdosfs_args *argp));
91 static int      msdosfs_fhtovp __P((struct mount *, struct fid *,
92                                     struct vnode **));
93 static int      msdosfs_checkexp __P((struct mount *, struct sockaddr *, 
94                                     int *, struct ucred **));
95 static int      msdosfs_mount __P((struct mount *, char *, caddr_t,
96                                    struct nameidata *, struct proc *));
97 static int      msdosfs_root __P((struct mount *, struct vnode **));
98 static int      msdosfs_statfs __P((struct mount *, struct statfs *,
99                                     struct proc *));
100 static int      msdosfs_sync __P((struct mount *, int, struct ucred *,
101                                   struct proc *));
102 static int      msdosfs_unmount __P((struct mount *, int, struct proc *));
103 static int      msdosfs_vptofh __P((struct vnode *, struct fid *));
104
105 static int
106 update_mp(mp, argp)
107         struct mount *mp;
108         struct msdosfs_args *argp;
109 {
110         struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
111         int error;
112
113         pmp->pm_gid = argp->gid;
114         pmp->pm_uid = argp->uid;
115         pmp->pm_mask = argp->mask & ALLPERMS;
116         pmp->pm_flags |= argp->flags & MSDOSFSMNT_MNTOPT;
117         if (pmp->pm_flags & MSDOSFSMNT_U2WTABLE) {
118                 bcopy(argp->u2w, pmp->pm_u2w, sizeof(pmp->pm_u2w));
119                 bcopy(argp->d2u, pmp->pm_d2u, sizeof(pmp->pm_d2u));
120                 bcopy(argp->u2d, pmp->pm_u2d, sizeof(pmp->pm_u2d));
121         }
122         if (pmp->pm_flags & MSDOSFSMNT_ULTABLE) {
123                 bcopy(argp->ul, pmp->pm_ul, sizeof(pmp->pm_ul));
124                 bcopy(argp->lu, pmp->pm_lu, sizeof(pmp->pm_lu));
125         }
126
127 #ifndef __FreeBSD__
128         /*
129          * GEMDOS knows nothing (yet) about win95
130          */
131         if (pmp->pm_flags & MSDOSFSMNT_GEMDOSFS)
132                 pmp->pm_flags |= MSDOSFSMNT_NOWIN95;
133 #endif
134
135         if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
136                 pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
137         else if (!(pmp->pm_flags &
138             (MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) {
139                 struct vnode *rootvp;
140
141                 /*
142                  * Try to divine whether to support Win'95 long filenames
143                  */
144                 if (FAT32(pmp))
145                         pmp->pm_flags |= MSDOSFSMNT_LONGNAME;
146                 else {
147                         if ((error = msdosfs_root(mp, &rootvp)) != 0)
148                                 return error;
149                         pmp->pm_flags |= findwin95(VTODE(rootvp))
150                                 ? MSDOSFSMNT_LONGNAME
151                                         : MSDOSFSMNT_SHORTNAME;
152                         vput(rootvp);
153                 }
154         }
155         return 0;
156 }
157
158 #ifndef __FreeBSD__
159 int
160 msdosfs_mountroot()
161 {
162         register struct mount *mp;
163         struct proc *p = curproc;       /* XXX */
164         size_t size;
165         int error;
166         struct msdosfs_args args;
167
168         if (root_device->dv_class != DV_DISK)
169                 return (ENODEV);
170
171         /*
172          * Get vnodes for swapdev and rootdev.
173          */
174         if (bdevvp(rootdev, &rootvp))
175                 panic("msdosfs_mountroot: can't setup rootvp");
176
177         mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK);
178         bzero((char *)mp, (u_long)sizeof(struct mount));
179         mp->mnt_op = &msdosfs_vfsops;
180         mp->mnt_flag = 0;
181         TAILQ_INIT(&mp->mnt_nvnodelist);
182         TAILQ_INIT(&mp->mnt_reservedvnlist);
183
184         args.flags = 0;
185         args.uid = 0;
186         args.gid = 0;
187         args.mask = 0777;
188
189         if ((error = mountmsdosfs(rootvp, mp, p, &args)) != 0) {
190                 free(mp, M_MOUNT);
191                 return (error);
192         }
193
194         if ((error = update_mp(mp, &args)) != 0) {
195                 (void)msdosfs_unmount(mp, 0, p);
196                 free(mp, M_MOUNT);
197                 return (error);
198         }
199
200         if ((error = vfs_lock(mp)) != 0) {
201                 (void)msdosfs_unmount(mp, 0, p);
202                 free(mp, M_MOUNT);
203                 return (error);
204         }
205
206         TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
207         mp->mnt_vnodecovered = NULLVP;
208         (void) copystr("/", mp->mnt_stat.f_mntonname, MNAMELEN - 1,
209             &size);
210         bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
211         (void) copystr(ROOTNAME, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
212             &size);
213         bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
214         (void)msdosfs_statfs(mp, &mp->mnt_stat, p);
215         vfs_unlock(mp);
216         return (0);
217 }
218 #endif
219
220 /*
221  * mp - path - addr in user space of mount point (ie /usr or whatever)
222  * data - addr in user space of mount params including the name of the block
223  * special file to treat as a filesystem.
224  */
225 static int
226 msdosfs_mount(mp, path, data, ndp, p)
227         struct mount *mp;
228         char *path;
229         caddr_t data;
230         struct nameidata *ndp;
231         struct proc *p;
232 {
233         struct vnode *devvp;      /* vnode for blk device to mount */
234         struct msdosfs_args args; /* will hold data from mount request */
235         /* msdosfs specific mount control block */
236         struct msdosfsmount *pmp = NULL;
237         size_t size;
238         int error, flags;
239         mode_t accessmode;
240
241         error = copyin(data, (caddr_t)&args, sizeof(struct msdosfs_args));
242         if (error)
243                 return (error);
244         if (args.magic != MSDOSFS_ARGSMAGIC)
245                 args.flags = 0;
246         /*
247          * If updating, check whether changing from read-only to
248          * read/write; if there is no device name, that's all we do.
249          */
250         if (mp->mnt_flag & MNT_UPDATE) {
251                 pmp = VFSTOMSDOSFS(mp);
252                 error = 0;
253                 if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_flag & MNT_RDONLY)) {
254                         flags = WRITECLOSE;
255                         if (mp->mnt_flag & MNT_FORCE)
256                                 flags |= FORCECLOSE;
257                         error = vflush(mp, 0, flags);
258                 }
259                 if (!error && (mp->mnt_flag & MNT_RELOAD))
260                         /* not yet implemented */
261                         error = EOPNOTSUPP;
262                 if (error)
263                         return (error);
264                 if ((pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
265                         /*
266                          * If upgrade to read-write by non-root, then verify
267                          * that user has necessary permissions on the device.
268                          */
269                         if (p->p_ucred->cr_uid != 0) {
270                                 devvp = pmp->pm_devvp;
271                                 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
272                                 error = VOP_ACCESS(devvp, VREAD | VWRITE,
273                                                    p->p_ucred, p);
274                                 if (error) {
275                                         VOP_UNLOCK(devvp, 0, p);
276                                         return (error);
277                                 }
278                                 VOP_UNLOCK(devvp, 0, p);
279                         }
280                         pmp->pm_flags &= ~MSDOSFSMNT_RONLY;
281                 }
282                 if (args.fspec == 0) {
283 #ifdef  __notyet__              /* doesn't work correctly with current mountd   XXX */
284                         if (args.flags & MSDOSFSMNT_MNTOPT) {
285                                 pmp->pm_flags &= ~MSDOSFSMNT_MNTOPT;
286                                 pmp->pm_flags |= args.flags & MSDOSFSMNT_MNTOPT;
287                                 if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
288                                         pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
289                         }
290 #endif
291                         /*
292                          * Process export requests.
293                          */
294                         return (vfs_export(mp, &pmp->pm_export, &args.export));
295                 }
296         }
297         /*
298          * Not an update, or updating the name: look up the name
299          * and verify that it refers to a sensible block device.
300          */
301         NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
302         error = namei(ndp);
303         if (error)
304                 return (error);
305         devvp = ndp->ni_vp;
306         NDFREE(ndp, NDF_ONLY_PNBUF);
307
308         if (!vn_isdisk(devvp, &error)) {
309                 vrele(devvp);
310                 return (error);
311         }
312         /*
313          * If mount by non-root, then verify that user has necessary
314          * permissions on the device.
315          */
316         if (p->p_ucred->cr_uid != 0) {
317                 accessmode = VREAD;
318                 if ((mp->mnt_flag & MNT_RDONLY) == 0)
319                         accessmode |= VWRITE;
320                 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
321                 error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
322                 if (error) {
323                         vput(devvp);
324                         return (error);
325                 }
326                 VOP_UNLOCK(devvp, 0, p);
327         }
328         if ((mp->mnt_flag & MNT_UPDATE) == 0) {
329                 error = mountmsdosfs(devvp, mp, p, &args);
330 #ifdef MSDOSFS_DEBUG            /* only needed for the printf below */
331                 pmp = VFSTOMSDOSFS(mp);
332 #endif
333         } else {
334                 if (devvp != pmp->pm_devvp)
335                         error = EINVAL; /* XXX needs translation */
336                 else
337                         vrele(devvp);
338         }
339         if (error) {
340                 vrele(devvp);
341                 return (error);
342         }
343
344         error = update_mp(mp, &args);
345         if (error) {
346                 msdosfs_unmount(mp, MNT_FORCE, p);
347                 return error;
348         }
349
350         (void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
351         bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
352         (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
353             &size);
354         bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
355         (void) msdosfs_statfs(mp, &mp->mnt_stat, p);
356 #ifdef MSDOSFS_DEBUG
357         printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap);
358 #endif
359         return (0);
360 }
361
362 static int
363 mountmsdosfs(devvp, mp, p, argp)
364         struct vnode *devvp;
365         struct mount *mp;
366         struct proc *p;
367         struct msdosfs_args *argp;
368 {
369         struct msdosfsmount *pmp;
370         struct buf *bp;
371         dev_t dev = devvp->v_rdev;
372 #ifndef __FreeBSD__
373         struct partinfo dpart;
374         int bsize = 0, dtype = 0, tmp;
375 #endif
376         union bootsector *bsp;
377         struct byte_bpb33 *b33;
378         struct byte_bpb50 *b50;
379         struct byte_bpb710 *b710;
380         u_int8_t SecPerClust;
381         u_long clusters;
382         int     ronly, error;
383
384         /*
385          * Disallow multiple mounts of the same device.
386          * Disallow mounting of a device that is currently in use
387          * (except for root, which might share swap device for miniroot).
388          * Flush out any old buffers remaining from a previous use.
389          */
390         error = vfs_mountedon(devvp);
391         if (error)
392                 return (error);
393         if (vcount(devvp) > 1 && devvp != rootvp)
394                 return (EBUSY);
395         vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
396         error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0);
397         VOP_UNLOCK(devvp, 0, p);
398         if (error)
399                 return (error);
400
401         ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
402         vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
403         error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
404         VOP_UNLOCK(devvp, 0, p);
405         if (error)
406                 return (error);
407
408         bp  = NULL; /* both used in error_exit */
409         pmp = NULL;
410
411 #ifndef __FreeBSD__
412         if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
413                 /*
414                  * We need the disklabel to calculate the size of a FAT entry
415                  * later on. Also make sure the partition contains a filesystem
416                  * of type FS_MSDOS. This doesn't work for floppies, so we have
417                  * to check for them too.
418                  *
419                  * At least some parts of the msdos fs driver seem to assume
420                  * that the size of a disk block will always be 512 bytes.
421                  * Let's check it...
422                  */
423                 error = VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart,
424                                   FREAD, NOCRED, p);
425                 if (error)
426                         goto error_exit;
427                 tmp   = dpart.part->p_fstype;
428                 dtype = dpart.disklab->d_type;
429                 bsize = dpart.disklab->d_secsize;
430                 if (bsize != 512 || (dtype!=DTYPE_FLOPPY && tmp!=FS_MSDOS)) {
431                         error = EINVAL;
432                         goto error_exit;
433                 }
434         }
435 #endif
436
437         /*
438          * Read the boot sector of the filesystem, and then check the
439          * boot signature.  If not a dos boot sector then error out.
440          *
441          * NOTE: 2048 is a maximum sector size in current...
442          */
443         error = bread(devvp, 0, 2048, NOCRED, &bp);
444         if (error)
445                 goto error_exit;
446         bp->b_flags |= B_AGE;
447         bsp = (union bootsector *)bp->b_data;
448         b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
449         b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
450         b710 = (struct byte_bpb710 *)bsp->bs710.bsPBP;
451
452 #ifndef __FreeBSD__
453         if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
454 #endif
455 #ifndef MSDOSFS_NOCHECKSIG
456                 if (bsp->bs50.bsBootSectSig0 != BOOTSIG0
457                     || bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
458                         error = EINVAL;
459                         goto error_exit;
460                 }
461 #endif
462 #ifndef __FreeBSD__
463         }
464 #endif
465
466         pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK);
467         bzero((caddr_t)pmp, sizeof *pmp);
468         pmp->pm_mountp = mp;
469
470         /*
471          * Compute several useful quantities from the bpb in the
472          * bootsector.  Copy in the dos 5 variant of the bpb then fix up
473          * the fields that are different between dos 5 and dos 3.3.
474          */
475         SecPerClust = b50->bpbSecPerClust;
476         pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
477         pmp->pm_ResSectors = getushort(b50->bpbResSectors);
478         pmp->pm_FATs = b50->bpbFATs;
479         pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
480         pmp->pm_Sectors = getushort(b50->bpbSectors);
481         pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
482         pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
483         pmp->pm_Heads = getushort(b50->bpbHeads);
484         pmp->pm_Media = b50->bpbMedia;
485
486         /* calculate the ratio of sector size to DEV_BSIZE */
487         pmp->pm_BlkPerSec = pmp->pm_BytesPerSec / DEV_BSIZE;
488
489 #ifndef __FreeBSD__
490         if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
491 #endif
492                 /* XXX - We should probably check more values here */
493                 if (!pmp->pm_BytesPerSec || !SecPerClust
494                         || !pmp->pm_Heads || pmp->pm_Heads > 255
495 #ifdef PC98
496                         || !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 255) {
497 #else
498                         || !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 63) {
499 #endif
500                         error = EINVAL;
501                         goto error_exit;
502                 }
503 #ifndef __FreeBSD__
504         }
505 #endif
506
507         if (pmp->pm_Sectors == 0) {
508                 pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
509                 pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
510         } else {
511                 pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
512                 pmp->pm_HugeSectors = pmp->pm_Sectors;
513         }
514         if (pmp->pm_HugeSectors > 0xffffffff / 
515             (pmp->pm_BytesPerSec / sizeof(struct direntry)) + 1) {
516                 /*
517                  * We cannot deal currently with this size of disk
518                  * due to fileid limitations (see msdosfs_getattr and
519                  * msdosfs_readdir)
520                  */
521                 error = EINVAL;
522                 printf("mountmsdosfs(): disk too big, sorry\n");
523                 goto error_exit;
524         }
525
526         if (pmp->pm_RootDirEnts == 0) {
527                 if (bsp->bs710.bsBootSectSig2 != BOOTSIG2
528                     || bsp->bs710.bsBootSectSig3 != BOOTSIG3
529                     || pmp->pm_Sectors
530                     || pmp->pm_FATsecs
531                     || getushort(b710->bpbFSVers)) {
532                         error = EINVAL;
533                         printf("mountmsdosfs(): bad FAT32 filesystem\n");
534                         goto error_exit;
535                 }
536                 pmp->pm_fatmask = FAT32_MASK;
537                 pmp->pm_fatmult = 4;
538                 pmp->pm_fatdiv = 1;
539                 pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
540                 if (getushort(b710->bpbExtFlags) & FATMIRROR)
541                         pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
542                 else
543                         pmp->pm_flags |= MSDOSFS_FATMIRROR;
544         } else
545                 pmp->pm_flags |= MSDOSFS_FATMIRROR;
546
547         /*
548          * Check a few values (could do some more):
549          * - logical sector size: power of 2, >= block size
550          * - sectors per cluster: power of 2, >= 1
551          * - number of sectors:   >= 1, <= size of partition
552          */
553         if ( (SecPerClust == 0)
554           || (SecPerClust & (SecPerClust - 1))
555           || (pmp->pm_BytesPerSec < DEV_BSIZE)
556           || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
557           || (pmp->pm_HugeSectors == 0)
558         ) {
559                 error = EINVAL;
560                 goto error_exit;
561         }
562
563         pmp->pm_HugeSectors *= pmp->pm_BlkPerSec;
564         pmp->pm_HiddenSects *= pmp->pm_BlkPerSec; /* XXX not used? */
565         pmp->pm_FATsecs     *= pmp->pm_BlkPerSec;
566         SecPerClust         *= pmp->pm_BlkPerSec;
567
568         pmp->pm_fatblk = pmp->pm_ResSectors * pmp->pm_BlkPerSec;
569
570         if (FAT32(pmp)) {
571                 pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
572                 pmp->pm_firstcluster = pmp->pm_fatblk
573                         + (pmp->pm_FATs * pmp->pm_FATsecs);
574                 pmp->pm_fsinfo = getushort(b710->bpbFSInfo) * pmp->pm_BlkPerSec;
575         } else {
576                 pmp->pm_rootdirblk = pmp->pm_fatblk +
577                         (pmp->pm_FATs * pmp->pm_FATsecs);
578                 pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
579                                        + DEV_BSIZE - 1)
580                         / DEV_BSIZE; /* in blocks */
581                 pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
582         }
583
584         pmp->pm_maxcluster = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
585             SecPerClust + 1;
586         pmp->pm_fatsize = pmp->pm_FATsecs * DEV_BSIZE; /* XXX not used? */
587
588 #ifndef __FreeBSD__
589         if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
590                 if ((pmp->pm_maxcluster <= (0xff0 - 2))
591                       && ((dtype == DTYPE_FLOPPY) || ((dtype == DTYPE_VNODE)
592                       && ((pmp->pm_Heads == 1) || (pmp->pm_Heads == 2))))
593                     ) {
594                         pmp->pm_fatmask = FAT12_MASK;
595                         pmp->pm_fatmult = 3;
596                         pmp->pm_fatdiv = 2;
597                 } else {
598                         pmp->pm_fatmask = FAT16_MASK;
599                         pmp->pm_fatmult = 2;
600                         pmp->pm_fatdiv = 1;
601                 }
602         } else 
603 #endif
604         if (pmp->pm_fatmask == 0) {
605                 if (pmp->pm_maxcluster
606                     <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
607                         /*
608                          * This will usually be a floppy disk. This size makes
609                          * sure that one fat entry will not be split across
610                          * multiple blocks.
611                          */
612                         pmp->pm_fatmask = FAT12_MASK;
613                         pmp->pm_fatmult = 3;
614                         pmp->pm_fatdiv = 2;
615                 } else {
616                         pmp->pm_fatmask = FAT16_MASK;
617                         pmp->pm_fatmult = 2;
618                         pmp->pm_fatdiv = 1;
619                 }
620         }
621
622         clusters = (pmp->pm_fatsize / pmp->pm_fatmult) * pmp->pm_fatdiv;
623         if (pmp->pm_maxcluster >= clusters) {
624                 printf("Warning: number of clusters (%ld) exceeds FAT "
625                     "capacity (%ld)\n", pmp->pm_maxcluster + 1, clusters);
626                 pmp->pm_maxcluster = clusters - 1;
627         }
628
629
630         if (FAT12(pmp))
631                 pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec;
632         else
633                 pmp->pm_fatblocksize = MSDOSFS_DFLTBSIZE;
634
635         pmp->pm_fatblocksec = pmp->pm_fatblocksize / DEV_BSIZE;
636         pmp->pm_bnshift = ffs(DEV_BSIZE) - 1;
637
638         /*
639          * Compute mask and shift value for isolating cluster relative byte
640          * offsets and cluster numbers from a file offset.
641          */
642         pmp->pm_bpcluster = SecPerClust * DEV_BSIZE;
643         pmp->pm_crbomask = pmp->pm_bpcluster - 1;
644         pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
645
646         /*
647          * Check for valid cluster size
648          * must be a power of 2
649          */
650         if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
651                 error = EINVAL;
652                 goto error_exit;
653         }
654
655         /*
656          * Release the bootsector buffer.
657          */
658         brelse(bp);
659         bp = NULL;
660
661         /*
662          * Check FSInfo.
663          */
664         if (pmp->pm_fsinfo) {
665                 struct fsinfo *fp;
666
667                 if ((error = bread(devvp, pmp->pm_fsinfo, fsi_size(pmp),
668                     NOCRED, &bp)) != 0)
669                         goto error_exit;
670                 fp = (struct fsinfo *)bp->b_data;
671                 if (!bcmp(fp->fsisig1, "RRaA", 4)
672                     && !bcmp(fp->fsisig2, "rrAa", 4)
673                     && !bcmp(fp->fsisig3, "\0\0\125\252", 4)
674                     && !bcmp(fp->fsisig4, "\0\0\125\252", 4))
675                         pmp->pm_nxtfree = getulong(fp->fsinxtfree);
676                 else
677                         pmp->pm_fsinfo = 0;
678                 brelse(bp);
679                 bp = NULL;
680         }
681
682         /*
683          * Check and validate (or perhaps invalidate?) the fsinfo structure?
684          */
685         if (pmp->pm_fsinfo && pmp->pm_nxtfree > pmp->pm_maxcluster) {
686                 printf(
687                 "Next free cluster in FSInfo (%lu) exceeds maxcluster (%lu)\n",
688                     pmp->pm_nxtfree, pmp->pm_maxcluster);
689                 error = EINVAL;
690                 goto error_exit;
691         }
692
693         /*
694          * Allocate memory for the bitmap of allocated clusters, and then
695          * fill it in.
696          */
697         pmp->pm_inusemap = malloc(((pmp->pm_maxcluster + N_INUSEBITS - 1)
698                                    / N_INUSEBITS)
699                                   * sizeof(*pmp->pm_inusemap),
700                                   M_MSDOSFSFAT, M_WAITOK);
701
702         /*
703          * fillinusemap() needs pm_devvp.
704          */
705         pmp->pm_dev = dev;
706         pmp->pm_devvp = devvp;
707
708         /*
709          * Have the inuse map filled in.
710          */
711         if ((error = fillinusemap(pmp)) != 0)
712                 goto error_exit;
713
714         /*
715          * If they want fat updates to be synchronous then let them suffer
716          * the performance degradation in exchange for the on disk copy of
717          * the fat being correct just about all the time.  I suppose this
718          * would be a good thing to turn on if the kernel is still flakey.
719          */
720         if (mp->mnt_flag & MNT_SYNCHRONOUS)
721                 pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
722
723         /*
724          * Finish up.
725          */
726         if (ronly)
727                 pmp->pm_flags |= MSDOSFSMNT_RONLY;
728         else
729                 pmp->pm_fmod = 1;
730         mp->mnt_data = (qaddr_t) pmp;
731         mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
732         mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
733         mp->mnt_flag |= MNT_LOCAL;
734         devvp->v_specmountpoint = mp;
735
736         return 0;
737
738 error_exit:
739         if (bp)
740                 brelse(bp);
741         (void) VOP_CLOSE(devvp, ronly ? FREAD : FREAD | FWRITE, NOCRED, p);
742         if (pmp) {
743                 if (pmp->pm_inusemap)
744                         free(pmp->pm_inusemap, M_MSDOSFSFAT);
745                 free(pmp, M_MSDOSFSMNT);
746                 mp->mnt_data = (qaddr_t)0;
747         }
748         return (error);
749 }
750
751 /*
752  * Unmount the filesystem described by mp.
753  */
754 static int
755 msdosfs_unmount(mp, mntflags, p)
756         struct mount *mp;
757         int mntflags;
758         struct proc *p;
759 {
760         struct msdosfsmount *pmp;
761         int error, flags;
762
763         flags = 0;
764         if (mntflags & MNT_FORCE)
765                 flags |= FORCECLOSE;
766         error = vflush(mp, 0, flags);
767         if (error)
768                 return error;
769         pmp = VFSTOMSDOSFS(mp);
770         pmp->pm_devvp->v_specmountpoint = NULL;
771 #ifdef MSDOSFS_DEBUG
772         {
773                 struct vnode *vp = pmp->pm_devvp;
774
775                 printf("msdosfs_umount(): just before calling VOP_CLOSE()\n");
776                 printf("flag %08lx, usecount %d, writecount %d, holdcnt %ld\n",
777                     vp->v_flag, vp->v_usecount, vp->v_writecount, vp->v_holdcnt);
778                 printf("id %lu, mount %p, op %p\n",
779                     vp->v_id, vp->v_mount, vp->v_op);
780                 printf("freef %p, freeb %p, mount %p\n",
781                     vp->v_freelist.tqe_next, vp->v_freelist.tqe_prev,
782                     vp->v_mount);
783                 printf("cleanblkhd %p, dirtyblkhd %p, numoutput %ld, type %d\n",
784                     TAILQ_FIRST(&vp->v_cleanblkhd),
785                     TAILQ_FIRST(&vp->v_dirtyblkhd),
786                     vp->v_numoutput, vp->v_type);
787                 printf("union %p, tag %d, data[0] %08x, data[1] %08x\n",
788                     vp->v_socket, vp->v_tag,
789                     ((u_int *)vp->v_data)[0],
790                     ((u_int *)vp->v_data)[1]);
791         }
792 #endif
793         error = VOP_CLOSE(pmp->pm_devvp,
794                     (pmp->pm_flags&MSDOSFSMNT_RONLY) ? FREAD : FREAD | FWRITE,
795                     NOCRED, p);
796         vrele(pmp->pm_devvp);
797         free(pmp->pm_inusemap, M_MSDOSFSFAT);
798         free(pmp, M_MSDOSFSMNT);
799         mp->mnt_data = (qaddr_t)0;
800         mp->mnt_flag &= ~MNT_LOCAL;
801         return (error);
802 }
803
804 static int
805 msdosfs_root(mp, vpp)
806         struct mount *mp;
807         struct vnode **vpp;
808 {
809         struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
810         struct denode *ndep;
811         int error;
812
813 #ifdef MSDOSFS_DEBUG
814         printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
815 #endif
816         error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep);
817         if (error)
818                 return (error);
819         *vpp = DETOV(ndep);
820         return (0);
821 }
822
823 static int
824 msdosfs_statfs(mp, sbp, p)
825         struct mount *mp;
826         struct statfs *sbp;
827         struct proc *p;
828 {
829         struct msdosfsmount *pmp;
830
831         pmp = VFSTOMSDOSFS(mp);
832         sbp->f_bsize = pmp->pm_bpcluster;
833         sbp->f_iosize = pmp->pm_bpcluster;
834         sbp->f_blocks = pmp->pm_maxcluster + 1;
835         sbp->f_bfree = pmp->pm_freeclustercount;
836         sbp->f_bavail = pmp->pm_freeclustercount;
837         sbp->f_files = pmp->pm_RootDirEnts;                     /* XXX */
838         sbp->f_ffree = 0;       /* what to put in here? */
839         if (sbp != &mp->mnt_stat) {
840                 sbp->f_type = mp->mnt_vfc->vfc_typenum;
841                 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
842                 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
843         }
844         strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
845         return (0);
846 }
847
848 static int
849 msdosfs_sync(mp, waitfor, cred, p)
850         struct mount *mp;
851         int waitfor;
852         struct ucred *cred;
853         struct proc *p;
854 {
855         struct vnode *vp, *nvp;
856         struct denode *dep;
857         struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
858         int error, allerror = 0;
859
860         /*
861          * If we ever switch to not updating all of the fats all the time,
862          * this would be the place to update them from the first one.
863          */
864         if (pmp->pm_fmod != 0) {
865                 if (pmp->pm_flags & MSDOSFSMNT_RONLY)
866                         panic("msdosfs_sync: rofs mod");
867                 else {
868                         /* update fats here */
869                 }
870         }
871         /*
872          * Write back each (modified) denode.
873          */
874         simple_lock(&mntvnode_slock);
875 loop:
876         for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp != NULL; vp = nvp) {
877                 /*
878                  * If the vnode that we are about to sync is no longer
879                  * associated with this mount point, start over.
880                  */
881                 if (vp->v_mount != mp)
882                         goto loop;
883
884                 simple_lock(&vp->v_interlock);
885                 nvp = TAILQ_NEXT(vp, v_nmntvnodes);
886                 dep = VTODE(vp);
887                 if (vp->v_type == VNON ||
888                     ((dep->de_flag &
889                     (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 &&
890                     (TAILQ_EMPTY(&vp->v_dirtyblkhd) || waitfor == MNT_LAZY))) {
891                         simple_unlock(&vp->v_interlock);
892                         continue;
893                 }
894                 simple_unlock(&mntvnode_slock);
895                 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, p);
896                 if (error) {
897                         simple_lock(&mntvnode_slock);
898                         if (error == ENOENT)
899                                 goto loop;
900                         continue;
901                 }
902                 error = VOP_FSYNC(vp, cred, waitfor, p);
903                 if (error)
904                         allerror = error;
905                 VOP_UNLOCK(vp, 0, p);
906                 vrele(vp);
907                 simple_lock(&mntvnode_slock);
908         }
909         simple_unlock(&mntvnode_slock);
910
911         /*
912          * Flush filesystem control info.
913          */
914         if (waitfor != MNT_LAZY) {
915                 vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY, p);
916                 error = VOP_FSYNC(pmp->pm_devvp, cred, waitfor, p);
917                 if (error)
918                         allerror = error;
919                 VOP_UNLOCK(pmp->pm_devvp, 0, p);
920         }
921         return (allerror);
922 }
923
924 static int
925 msdosfs_fhtovp(mp, fhp, vpp)
926         struct mount *mp;
927         struct fid *fhp;
928         struct vnode **vpp;
929 {
930         struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
931         struct defid *defhp = (struct defid *) fhp;
932         struct denode *dep;
933         int error;
934
935         error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep);
936         if (error) {
937                 *vpp = NULLVP;
938                 return (error);
939         }
940         *vpp = DETOV(dep);
941         return (0);
942 }
943
944 static int
945 msdosfs_checkexp(mp, nam,  exflagsp, credanonp)
946         struct mount *mp;
947         struct sockaddr *nam;
948         int *exflagsp;
949         struct ucred **credanonp;
950 {
951         struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
952         struct netcred *np;
953
954         np = vfs_export_lookup(mp, &pmp->pm_export, nam);
955         if (np == NULL)
956                 return (EACCES);
957         *exflagsp = np->netc_exflags;
958         *credanonp = &np->netc_anon;
959         return (0);
960 }
961
962 static int
963 msdosfs_vptofh(vp, fhp)
964         struct vnode *vp;
965         struct fid *fhp;
966 {
967         struct denode *dep;
968         struct defid *defhp;
969
970         dep = VTODE(vp);
971         defhp = (struct defid *)fhp;
972         defhp->defid_len = sizeof(struct defid);
973         defhp->defid_dirclust = dep->de_dirclust;
974         defhp->defid_dirofs = dep->de_diroffset;
975         /* defhp->defid_gen = dep->de_gen; */
976         return (0);
977 }
978
979 static struct vfsops msdosfs_vfsops = {
980         msdosfs_mount,
981         vfs_stdstart,
982         msdosfs_unmount,
983         msdosfs_root,
984         vfs_stdquotactl,
985         msdosfs_statfs,
986         msdosfs_sync,
987         vfs_stdvget,
988         msdosfs_fhtovp,
989         msdosfs_checkexp,
990         msdosfs_vptofh,
991         msdosfs_init,
992         msdosfs_uninit,
993         vfs_stdextattrctl,
994 };
995
996 VFS_SET(msdosfs_vfsops, msdos, 0);