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