Get rid of dead non-DragonFly code.
[dragonfly.git] / sys / vfs / msdosfs / msdosfs_vfsops.c
1 /* $FreeBSD: /usr/local/www/cvsroot/FreeBSD/src/sys/msdosfs/Attic/msdosfs_vfsops.c,v 1.60.2.8 2004/03/02 09:43:04 tjr Exp $ */
2 /* $DragonFly: src/sys/vfs/msdosfs/msdosfs_vfsops.c,v 1.23 2004/12/29 02:42:16 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/proc.h>
56 #include <sys/nlookup.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 "bpb.h"
67 #include "bootsect.h"
68 #include "direntry.h"
69 #include "denode.h"
70 #include "msdosfsmount.h"
71 #include "fat.h"
72
73 extern struct vnodeopv_entry_desc msdosfs_vnodeop_entries[];
74
75 #define MSDOSFS_DFLTBSIZE       4096
76
77 #if 1 /*def PC98*/
78 /*
79  * XXX - The boot signature formatted by NEC PC-98 DOS looks like a
80  *       garbage or a random value :-{
81  *       If you want to use that broken-signatured media, define the
82  *       following symbol even though PC/AT.
83  *       (ex. mount PC-98 DOS formatted FD on PC/AT)
84  */
85 #define MSDOSFS_NOCHECKSIG
86 #endif
87
88 MALLOC_DEFINE(M_MSDOSFSMNT, "MSDOSFS mount", "MSDOSFS mount structure");
89 static MALLOC_DEFINE(M_MSDOSFSFAT, "MSDOSFS FAT", "MSDOSFS file allocation table");
90
91 static int      update_mp (struct mount *mp, struct msdosfs_args *argp);
92 static int      mountmsdosfs (struct vnode *devvp, struct mount *mp,
93                                   struct thread *td, struct msdosfs_args *argp);
94 static int      msdosfs_fhtovp (struct mount *, struct fid *,
95                                     struct vnode **);
96 static int      msdosfs_checkexp (struct mount *, struct sockaddr *, 
97                                     int *, struct ucred **);
98 static int      msdosfs_mount (struct mount *, char *, caddr_t,
99                                    struct thread *);
100 static int      msdosfs_root (struct mount *, struct vnode **);
101 static int      msdosfs_statfs (struct mount *, struct statfs *,
102                                     struct thread *);
103 static int      msdosfs_sync (struct mount *, int, struct thread *);
104 static int      msdosfs_unmount (struct mount *, int, struct thread *);
105 static int      msdosfs_vptofh (struct vnode *, struct fid *);
106
107 static int
108 update_mp(struct mount *mp, 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 __DragonFly__
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 /*
159  * mp - path - addr in user space of mount point (ie /usr or whatever)
160  * data - addr in user space of mount params including the name of the block
161  * special file to treat as a filesystem.
162  */
163 static int
164 msdosfs_mount(struct mount *mp, char *path, caddr_t data, struct thread *td)
165 {
166         struct vnode *devvp;      /* vnode for blk device to mount */
167         struct msdosfs_args args; /* will hold data from mount request */
168         /* msdosfs specific mount control block */
169         struct msdosfsmount *pmp = NULL;
170         size_t size;
171         int error, flags;
172         mode_t accessmode;
173         struct proc *p = td->td_proc;
174         struct nlookupdata nd;
175
176         KKASSERT(p);
177
178         error = copyin(data, (caddr_t)&args, sizeof(struct msdosfs_args));
179         if (error)
180                 return (error);
181         if (args.magic != MSDOSFS_ARGSMAGIC)
182                 args.flags = 0;
183         /*
184          * If updating, check whether changing from read-only to
185          * read/write; if there is no device name, that's all we do.
186          */
187         if (mp->mnt_flag & MNT_UPDATE) {
188                 pmp = VFSTOMSDOSFS(mp);
189                 error = 0;
190                 if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_flag & MNT_RDONLY)) {
191                         flags = WRITECLOSE;
192                         if (mp->mnt_flag & MNT_FORCE)
193                                 flags |= FORCECLOSE;
194                         error = vflush(mp, 0, flags);
195                 }
196                 if (!error && (mp->mnt_flag & MNT_RELOAD))
197                         /* not yet implemented */
198                         error = EOPNOTSUPP;
199                 if (error)
200                         return (error);
201                 if ((pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
202                         /*
203                          * If upgrade to read-write by non-root, then verify
204                          * that user has necessary permissions on the device.
205                          */
206                         if (p->p_ucred->cr_uid != 0) {
207                                 devvp = pmp->pm_devvp;
208                                 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
209                                 error = VOP_ACCESS(devvp, VREAD | VWRITE,
210                                                    p->p_ucred, td);
211                                 if (error) {
212                                         VOP_UNLOCK(devvp, 0, td);
213                                         return (error);
214                                 }
215                                 VOP_UNLOCK(devvp, 0, td);
216                         }
217                         pmp->pm_flags &= ~MSDOSFSMNT_RONLY;
218                 }
219                 if (args.fspec == 0) {
220 #ifdef  __notyet__              /* doesn't work correctly with current mountd   XXX */
221                         if (args.flags & MSDOSFSMNT_MNTOPT) {
222                                 pmp->pm_flags &= ~MSDOSFSMNT_MNTOPT;
223                                 pmp->pm_flags |= args.flags & MSDOSFSMNT_MNTOPT;
224                                 if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
225                                         pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
226                         }
227 #endif
228                         /*
229                          * Process export requests.
230                          */
231                         return (vfs_export(mp, &pmp->pm_export, &args.export));
232                 }
233         }
234         /*
235          * Not an update, or updating the name: look up the name
236          * and verify that it refers to a sensible block device.
237          */
238         devvp = NULL;
239         error = nlookup_init(&nd, args.fspec, UIO_USERSPACE, NLC_FOLLOW);
240         if (error == 0)
241                 error = nlookup(&nd);
242         if (error == 0)
243                 error = cache_vref(nd.nl_ncp, nd.nl_cred, &devvp);
244         nlookup_done(&nd);
245         if (error)
246                 return (error);
247
248         if (!vn_isdisk(devvp, &error)) {
249                 vrele(devvp);
250                 return (error);
251         }
252         /*
253          * If mount by non-root, then verify that user has necessary
254          * permissions on the device.
255          */
256         if (p->p_ucred->cr_uid != 0) {
257                 accessmode = VREAD;
258                 if ((mp->mnt_flag & MNT_RDONLY) == 0)
259                         accessmode |= VWRITE;
260                 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
261                 error = VOP_ACCESS(devvp, accessmode, p->p_ucred, td);
262                 if (error) {
263                         vput(devvp);
264                         return (error);
265                 }
266                 VOP_UNLOCK(devvp, 0, td);
267         }
268         if ((mp->mnt_flag & MNT_UPDATE) == 0) {
269                 error = mountmsdosfs(devvp, mp, td, &args);
270 #ifdef MSDOSFS_DEBUG            /* only needed for the printf below */
271                 pmp = VFSTOMSDOSFS(mp);
272 #endif
273         } else {
274                 if (devvp != pmp->pm_devvp)
275                         error = EINVAL; /* XXX needs translation */
276                 else
277                         vrele(devvp);
278         }
279         if (error) {
280                 vrele(devvp);
281                 return (error);
282         }
283
284         error = update_mp(mp, &args);
285         if (error) {
286                 msdosfs_unmount(mp, MNT_FORCE, td);
287                 return error;
288         }
289
290         (void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
291         bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
292         (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
293             &size);
294         bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
295         (void) msdosfs_statfs(mp, &mp->mnt_stat, td);
296 #ifdef MSDOSFS_DEBUG
297         printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap);
298 #endif
299         return (0);
300 }
301
302 static int
303 mountmsdosfs(struct vnode *devvp, struct mount *mp, struct thread *td,
304              struct msdosfs_args *argp)
305 {
306         struct msdosfsmount *pmp;
307         struct buf *bp;
308         dev_t dev;
309 #ifndef __DragonFly__
310         struct partinfo dpart;
311         int bsize = 0, dtype = 0, tmp;
312 #endif
313         union bootsector *bsp;
314         struct byte_bpb33 *b33;
315         struct byte_bpb50 *b50;
316         struct byte_bpb710 *b710;
317         u_int8_t SecPerClust;
318         u_long clusters;
319         int     ronly, error;
320
321         /*
322          * Disallow multiple mounts of the same device.
323          * Disallow mounting of a device that is currently in use
324          * Flush out any old buffers remaining from a previous use.
325          */
326         error = vfs_mountedon(devvp);
327         if (error)
328                 return (error);
329         if (count_udev(devvp->v_udev) > 0)
330                 return (EBUSY);
331         vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
332         error = vinvalbuf(devvp, V_SAVE, td, 0, 0);
333         VOP_UNLOCK(devvp, 0, td);
334         if (error)
335                 return (error);
336
337         ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
338         vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
339         error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, NULL, td);
340         VOP_UNLOCK(devvp, 0, td);
341         if (error)
342                 return (error);
343         dev = devvp->v_rdev;
344         bp  = NULL; /* both used in error_exit */
345         pmp = NULL;
346
347 #ifndef __DragonFly__
348         if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
349                 /*
350                  * We need the disklabel to calculate the size of a FAT entry
351                  * later on. Also make sure the partition contains a filesystem
352                  * of type FS_MSDOS. This doesn't work for floppies, so we have
353                  * to check for them too.
354                  *
355                  * At least some parts of the msdos fs driver seem to assume
356                  * that the size of a disk block will always be 512 bytes.
357                  * Let's check it...
358                  */
359                 error = VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, td);
360                 if (error)
361                         goto error_exit;
362                 tmp   = dpart.part->p_fstype;
363                 dtype = dpart.disklab->d_type;
364                 bsize = dpart.disklab->d_secsize;
365                 if (bsize != 512 || (dtype!=DTYPE_FLOPPY && tmp!=FS_MSDOS)) {
366                         error = EINVAL;
367                         goto error_exit;
368                 }
369         }
370 #endif
371
372         /*
373          * Read the boot sector of the filesystem, and then check the
374          * boot signature.  If not a dos boot sector then error out.
375          *
376          * NOTE: 2048 is a maximum sector size in current...
377          */
378         error = bread(devvp, 0, 2048, &bp);
379         if (error)
380                 goto error_exit;
381         bp->b_flags |= B_AGE;
382         bsp = (union bootsector *)bp->b_data;
383         b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
384         b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
385         b710 = (struct byte_bpb710 *)bsp->bs710.bsPBP;
386
387 #ifndef __DragonFly__
388         if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
389 #endif
390 #ifndef MSDOSFS_NOCHECKSIG
391                 if (bsp->bs50.bsBootSectSig0 != BOOTSIG0
392                     || bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
393                         error = EINVAL;
394                         goto error_exit;
395                 }
396 #endif
397 #ifndef __DragonFly__
398         }
399 #endif
400
401         pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK);
402         bzero((caddr_t)pmp, sizeof *pmp);
403         pmp->pm_mountp = mp;
404
405         /*
406          * Compute several useful quantities from the bpb in the
407          * bootsector.  Copy in the dos 5 variant of the bpb then fix up
408          * the fields that are different between dos 5 and dos 3.3.
409          */
410         SecPerClust = b50->bpbSecPerClust;
411         pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
412         pmp->pm_ResSectors = getushort(b50->bpbResSectors);
413         pmp->pm_FATs = b50->bpbFATs;
414         pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
415         pmp->pm_Sectors = getushort(b50->bpbSectors);
416         pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
417         pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
418         pmp->pm_Heads = getushort(b50->bpbHeads);
419         pmp->pm_Media = b50->bpbMedia;
420
421         /* calculate the ratio of sector size to DEV_BSIZE */
422         pmp->pm_BlkPerSec = pmp->pm_BytesPerSec / DEV_BSIZE;
423
424 #ifndef __DragonFly__
425         if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
426 #endif
427                 /* XXX - We should probably check more values here */
428                 if (!pmp->pm_BytesPerSec || !SecPerClust
429                         || !pmp->pm_Heads
430 #ifdef PC98
431                         || !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 255) {
432 #else
433                         || !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 63) {
434 #endif
435                         error = EINVAL;
436                         goto error_exit;
437                 }
438 #ifndef __DragonFly__
439         }
440 #endif
441
442         if (pmp->pm_Sectors == 0) {
443                 pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
444                 pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
445         } else {
446                 pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
447                 pmp->pm_HugeSectors = pmp->pm_Sectors;
448         }
449         if (pmp->pm_HugeSectors > 0xffffffff / 
450             (pmp->pm_BytesPerSec / sizeof(struct direntry)) + 1) {
451                 /*
452                  * We cannot deal currently with this size of disk
453                  * due to fileid limitations (see msdosfs_getattr and
454                  * msdosfs_readdir)
455                  */
456                 error = EINVAL;
457                 printf("mountmsdosfs(): disk too big, sorry\n");
458                 goto error_exit;
459         }
460
461         if (pmp->pm_RootDirEnts == 0) {
462                 if (bsp->bs710.bsBootSectSig2 != BOOTSIG2
463                     || bsp->bs710.bsBootSectSig3 != BOOTSIG3
464                     || pmp->pm_Sectors
465                     || pmp->pm_FATsecs
466                     || getushort(b710->bpbFSVers)) {
467                         error = EINVAL;
468                         printf("mountmsdosfs(): bad FAT32 filesystem\n");
469                         goto error_exit;
470                 }
471                 pmp->pm_fatmask = FAT32_MASK;
472                 pmp->pm_fatmult = 4;
473                 pmp->pm_fatdiv = 1;
474                 pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
475                 if (getushort(b710->bpbExtFlags) & FATMIRROR)
476                         pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
477                 else
478                         pmp->pm_flags |= MSDOSFS_FATMIRROR;
479         } else
480                 pmp->pm_flags |= MSDOSFS_FATMIRROR;
481
482         /*
483          * Check a few values (could do some more):
484          * - logical sector size: power of 2, >= block size
485          * - sectors per cluster: power of 2, >= 1
486          * - number of sectors:   >= 1, <= size of partition
487          */
488         if ( (SecPerClust == 0)
489           || (SecPerClust & (SecPerClust - 1))
490           || (pmp->pm_BytesPerSec < DEV_BSIZE)
491           || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
492           || (pmp->pm_HugeSectors == 0)
493         ) {
494                 error = EINVAL;
495                 goto error_exit;
496         }
497
498         pmp->pm_HugeSectors *= pmp->pm_BlkPerSec;
499         pmp->pm_HiddenSects *= pmp->pm_BlkPerSec; /* XXX not used? */
500         pmp->pm_FATsecs     *= pmp->pm_BlkPerSec;
501         SecPerClust         *= pmp->pm_BlkPerSec;
502
503         pmp->pm_fatblk = pmp->pm_ResSectors * pmp->pm_BlkPerSec;
504
505         if (FAT32(pmp)) {
506                 pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
507                 pmp->pm_firstcluster = pmp->pm_fatblk
508                         + (pmp->pm_FATs * pmp->pm_FATsecs);
509                 pmp->pm_fsinfo = getushort(b710->bpbFSInfo) * pmp->pm_BlkPerSec;
510         } else {
511                 pmp->pm_rootdirblk = pmp->pm_fatblk +
512                         (pmp->pm_FATs * pmp->pm_FATsecs);
513                 pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
514                                        + DEV_BSIZE - 1)
515                         / DEV_BSIZE; /* in blocks */
516                 pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
517         }
518
519         pmp->pm_maxcluster = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
520             SecPerClust + 1;
521         pmp->pm_fatsize = pmp->pm_FATsecs * DEV_BSIZE; /* XXX not used? */
522
523 #ifndef __DragonFly__
524         if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
525                 if ((pmp->pm_maxcluster <= (0xff0 - 2))
526                       && ((dtype == DTYPE_FLOPPY) || ((dtype == DTYPE_VNODE)
527                       && ((pmp->pm_Heads == 1) || (pmp->pm_Heads == 2))))
528                     ) {
529                         pmp->pm_fatmask = FAT12_MASK;
530                         pmp->pm_fatmult = 3;
531                         pmp->pm_fatdiv = 2;
532                 } else {
533                         pmp->pm_fatmask = FAT16_MASK;
534                         pmp->pm_fatmult = 2;
535                         pmp->pm_fatdiv = 1;
536                 }
537         } else 
538 #endif
539         if (pmp->pm_fatmask == 0) {
540                 if (pmp->pm_maxcluster
541                     <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
542                         /*
543                          * This will usually be a floppy disk. This size makes
544                          * sure that one fat entry will not be split across
545                          * multiple blocks.
546                          */
547                         pmp->pm_fatmask = FAT12_MASK;
548                         pmp->pm_fatmult = 3;
549                         pmp->pm_fatdiv = 2;
550                 } else {
551                         pmp->pm_fatmask = FAT16_MASK;
552                         pmp->pm_fatmult = 2;
553                         pmp->pm_fatdiv = 1;
554                 }
555         }
556
557         clusters = (pmp->pm_fatsize / pmp->pm_fatmult) * pmp->pm_fatdiv;
558         if (pmp->pm_maxcluster >= clusters) {
559                 printf("Warning: number of clusters (%ld) exceeds FAT "
560                     "capacity (%ld)\n", pmp->pm_maxcluster + 1, clusters);
561                 pmp->pm_maxcluster = clusters - 1;
562         }
563
564
565         if (FAT12(pmp))
566                 pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec;
567         else
568                 pmp->pm_fatblocksize = MSDOSFS_DFLTBSIZE;
569
570         pmp->pm_fatblocksec = pmp->pm_fatblocksize / DEV_BSIZE;
571         pmp->pm_bnshift = ffs(DEV_BSIZE) - 1;
572
573         /*
574          * Compute mask and shift value for isolating cluster relative byte
575          * offsets and cluster numbers from a file offset.
576          */
577         pmp->pm_bpcluster = SecPerClust * DEV_BSIZE;
578         pmp->pm_crbomask = pmp->pm_bpcluster - 1;
579         pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
580
581         /*
582          * Check for valid cluster size
583          * must be a power of 2
584          */
585         if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
586                 error = EINVAL;
587                 goto error_exit;
588         }
589
590         /*
591          * Release the bootsector buffer.
592          */
593         brelse(bp);
594         bp = NULL;
595
596         /*
597          * Check FSInfo.
598          */
599         if (pmp->pm_fsinfo) {
600                 struct fsinfo *fp;
601
602                 if ((error = bread(devvp, pmp->pm_fsinfo, fsi_size(pmp), &bp)) != 0)
603                         goto error_exit;
604                 fp = (struct fsinfo *)bp->b_data;
605                 if (!bcmp(fp->fsisig1, "RRaA", 4)
606                     && !bcmp(fp->fsisig2, "rrAa", 4)
607                     && !bcmp(fp->fsisig3, "\0\0\125\252", 4)
608                     && !bcmp(fp->fsisig4, "\0\0\125\252", 4)) {
609                         pmp->pm_nxtfree = getulong(fp->fsinxtfree);
610                         if (pmp->pm_nxtfree == 0xffffffff)
611                                 pmp->pm_nxtfree = CLUST_FIRST;
612                 } else
613                         pmp->pm_fsinfo = 0;
614                 brelse(bp);
615                 bp = NULL;
616         }
617
618         /*
619          * Check and validate (or perhaps invalidate?) the fsinfo structure?
620          */
621         if (pmp->pm_fsinfo && pmp->pm_nxtfree > pmp->pm_maxcluster) {
622                 printf(
623                 "Next free cluster in FSInfo (%lu) exceeds maxcluster (%lu)\n",
624                     pmp->pm_nxtfree, pmp->pm_maxcluster);
625                 error = EINVAL;
626                 goto error_exit;
627         }
628
629         /*
630          * Allocate memory for the bitmap of allocated clusters, and then
631          * fill it in.
632          */
633         pmp->pm_inusemap = malloc(((pmp->pm_maxcluster + N_INUSEBITS - 1)
634                                    / N_INUSEBITS)
635                                   * sizeof(*pmp->pm_inusemap),
636                                   M_MSDOSFSFAT, M_WAITOK);
637
638         /*
639          * fillinusemap() needs pm_devvp.
640          */
641         pmp->pm_dev = dev;
642         pmp->pm_devvp = devvp;
643
644         /*
645          * Have the inuse map filled in.
646          */
647         if ((error = fillinusemap(pmp)) != 0)
648                 goto error_exit;
649
650         /*
651          * If they want fat updates to be synchronous then let them suffer
652          * the performance degradation in exchange for the on disk copy of
653          * the fat being correct just about all the time.  I suppose this
654          * would be a good thing to turn on if the kernel is still flakey.
655          */
656         if (mp->mnt_flag & MNT_SYNCHRONOUS)
657                 pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
658
659         /*
660          * Finish up.
661          */
662         if (ronly)
663                 pmp->pm_flags |= MSDOSFSMNT_RONLY;
664         else
665                 pmp->pm_fmod = 1;
666         mp->mnt_data = (qaddr_t) pmp;
667         mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
668         mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
669         mp->mnt_flag |= MNT_LOCAL;
670         vfs_add_vnodeops(mp, &mp->mnt_vn_norm_ops, msdosfs_vnodeop_entries);
671         dev->si_mountpoint = mp;
672
673         return 0;
674
675 error_exit:
676         if (bp)
677                 brelse(bp);
678         (void) VOP_CLOSE(devvp, ronly ? FREAD : FREAD | FWRITE, td);
679         if (pmp) {
680                 if (pmp->pm_inusemap)
681                         free(pmp->pm_inusemap, M_MSDOSFSFAT);
682                 free(pmp, M_MSDOSFSMNT);
683                 mp->mnt_data = (qaddr_t)0;
684         }
685         return (error);
686 }
687
688 /*
689  * Unmount the filesystem described by mp.
690  */
691 static int
692 msdosfs_unmount(struct mount *mp, int mntflags, struct thread *td)
693 {
694         struct msdosfsmount *pmp;
695         int error, flags;
696
697         flags = 0;
698         if (mntflags & MNT_FORCE)
699                 flags |= FORCECLOSE;
700         error = vflush(mp, 0, flags);
701         if (error)
702                 return error;
703         pmp = VFSTOMSDOSFS(mp);
704         pmp->pm_devvp->v_rdev->si_mountpoint = NULL;
705 #ifdef MSDOSFS_DEBUG
706         {
707                 struct vnode *vp = pmp->pm_devvp;
708
709                 printf("msdosfs_umount(): just before calling VOP_CLOSE()\n");
710                 printf("flag %08lx, usecount %d, writecount %d, holdcnt %ld\n",
711                     vp->v_flag, vp->v_usecount, vp->v_writecount, vp->v_holdcnt);
712                 printf("id %lu, mount %p, op %p\n",
713                     vp->v_id, vp->v_mount, vp->v_op);
714                 printf("freef %p, freeb %p, mount %p\n",
715                     TAILQ_NEXT(vp, v_freelist), TAILQ_PREV(vp, v_freelist),
716                     vp->v_mount);
717                 printf("cleanblkhd %p, dirtyblkhd %p, numoutput %ld, type %d\n",
718                     TAILQ_FIRST(&vp->v_cleanblkhd),
719                     TAILQ_FIRST(&vp->v_dirtyblkhd),
720                     vp->v_numoutput, vp->v_type);
721                 printf("union %p, tag %d, data[0] %08x, data[1] %08x\n",
722                     vp->v_socket, vp->v_tag,
723                     ((u_int *)vp->v_data)[0],
724                     ((u_int *)vp->v_data)[1]);
725         }
726 #endif
727         error = VOP_CLOSE(pmp->pm_devvp,
728                     (pmp->pm_flags&MSDOSFSMNT_RONLY) ? FREAD : FREAD | FWRITE,
729                     td);
730         vrele(pmp->pm_devvp);
731         free(pmp->pm_inusemap, M_MSDOSFSFAT);
732         free(pmp, M_MSDOSFSMNT);
733         mp->mnt_data = (qaddr_t)0;
734         mp->mnt_flag &= ~MNT_LOCAL;
735         return (error);
736 }
737
738 static int
739 msdosfs_root(struct mount *mp, struct vnode **vpp)
740 {
741         struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
742         struct denode *ndep;
743         int error;
744
745 #ifdef MSDOSFS_DEBUG
746         printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
747 #endif
748         error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep);
749         if (error)
750                 return (error);
751         *vpp = DETOV(ndep);
752         return (0);
753 }
754
755 static int
756 msdosfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
757 {
758         struct msdosfsmount *pmp;
759
760         pmp = VFSTOMSDOSFS(mp);
761         sbp->f_bsize = pmp->pm_bpcluster;
762         sbp->f_iosize = pmp->pm_bpcluster;
763         sbp->f_blocks = pmp->pm_maxcluster + 1;
764         sbp->f_bfree = pmp->pm_freeclustercount;
765         sbp->f_bavail = pmp->pm_freeclustercount;
766         sbp->f_files = pmp->pm_RootDirEnts;                     /* XXX */
767         sbp->f_ffree = 0;       /* what to put in here? */
768         if (sbp != &mp->mnt_stat) {
769                 sbp->f_type = mp->mnt_vfc->vfc_typenum;
770                 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
771                 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
772         }
773         strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
774         return (0);
775 }
776
777 struct scaninfo {
778         int rescan;
779         int allerror;
780         int waitfor;
781         thread_t td;
782 };
783
784 static int msdosfs_sync_scan(struct mount *mp, struct vnode *vp, void *data);
785
786 static int
787 msdosfs_sync(struct mount *mp, int waitfor, struct thread *td)
788 {
789         struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
790         struct scaninfo scaninfo;
791         int error;
792
793         /*
794          * If we ever switch to not updating all of the fats all the time,
795          * this would be the place to update them from the first one.
796          */
797         if (pmp->pm_fmod != 0) {
798                 if (pmp->pm_flags & MSDOSFSMNT_RONLY)
799                         panic("msdosfs_sync: rofs mod");
800                 else {
801                         /* update fats here */
802                 }
803         }
804         /*
805          * Write back each (modified) denode.
806          */
807         scaninfo.allerror = 0;
808         scaninfo.rescan = 1;
809         scaninfo.td = td;
810         while (scaninfo.rescan) {
811                 scaninfo.rescan = 0;
812                 vmntvnodescan(mp, VMSC_GETVP|VMSC_NOWAIT, NULL, msdosfs_sync_scan, &scaninfo);
813         }
814
815         /*
816          * Flush filesystem control info.
817          */
818         if (waitfor != MNT_LAZY) {
819                 vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY, td);
820                 if ((error = VOP_FSYNC(pmp->pm_devvp, waitfor, td)) != 0)
821                         scaninfo.allerror = error;
822                 VOP_UNLOCK(pmp->pm_devvp, 0, td);
823         }
824         return (scaninfo.allerror);
825 }
826
827 static int
828 msdosfs_sync_scan(struct mount *mp, struct vnode *vp, void *data)
829 {
830         struct scaninfo *info = data;
831         struct denode *dep;
832         int error;
833
834         dep = VTODE(vp);
835         if (vp->v_type == VNON ||
836             ((dep->de_flag &
837             (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 &&
838             (TAILQ_EMPTY(&vp->v_dirtyblkhd) || info->waitfor == MNT_LAZY))) {
839                 return(0);
840         }
841         if ((error = VOP_FSYNC(vp, info->waitfor, info->td)) != 0)
842                 info->allerror = error;
843         return(0);
844 }
845
846 static int
847 msdosfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
848 {
849         struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
850         struct defid *defhp = (struct defid *) fhp;
851         struct denode *dep;
852         int error;
853
854         error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep);
855         if (error) {
856                 *vpp = NULLVP;
857                 return (error);
858         }
859         *vpp = DETOV(dep);
860         return (0);
861 }
862
863 static int
864 msdosfs_checkexp(struct mount *mp, struct sockaddr *nam, int *exflagsp,
865                  struct ucred **credanonp)
866 {
867         struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
868         struct netcred *np;
869
870         np = vfs_export_lookup(mp, &pmp->pm_export, nam);
871         if (np == NULL)
872                 return (EACCES);
873         *exflagsp = np->netc_exflags;
874         *credanonp = &np->netc_anon;
875         return (0);
876 }
877
878 static int
879 msdosfs_vptofh(struct vnode *vp, struct fid *fhp)
880 {
881         struct denode *dep;
882         struct defid *defhp;
883
884         dep = VTODE(vp);
885         defhp = (struct defid *)fhp;
886         defhp->defid_len = sizeof(struct defid);
887         defhp->defid_dirclust = dep->de_dirclust;
888         defhp->defid_dirofs = dep->de_diroffset;
889         /* defhp->defid_gen = dep->de_gen; */
890         return (0);
891 }
892
893 static struct vfsops msdosfs_vfsops = {
894         msdosfs_mount,
895         vfs_stdstart,
896         msdosfs_unmount,
897         msdosfs_root,
898         vfs_stdquotactl,
899         msdosfs_statfs,
900         msdosfs_sync,
901         vfs_stdvget,
902         msdosfs_fhtovp,
903         msdosfs_checkexp,
904         msdosfs_vptofh,
905         msdosfs_init,
906         msdosfs_uninit,
907         vfs_stdextattrctl,
908 };
909
910 VFS_SET(msdosfs_vfsops, msdos, 0);