Don't check pm_Heads nor pm_SecPerTrack.
[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.52 2008/09/17 21:44:24 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 vop_ops msdosfs_vnode_vops;
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 msdosfs_args *argp);
94 static int      msdosfs_fhtovp (struct mount *, struct vnode *,
95                                 struct fid *, 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 ucred *);
100 static int      msdosfs_root (struct mount *, struct vnode **);
101 static int      msdosfs_statfs (struct mount *, struct statfs *,
102                                 struct ucred *);
103 static int      msdosfs_sync (struct mount *, int);
104 static int      msdosfs_unmount (struct mount *, int);
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 ucred *cred)
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 nlookupdata nd;
174
175         error = copyin(data, (caddr_t)&args, sizeof(struct msdosfs_args));
176         if (error)
177                 return (error);
178         if (args.magic != MSDOSFS_ARGSMAGIC)
179                 args.flags = 0;
180         /*
181          * If updating, check whether changing from read-only to
182          * read/write; if there is no device name, that's all we do.
183          */
184         if (mp->mnt_flag & MNT_UPDATE) {
185                 pmp = VFSTOMSDOSFS(mp);
186                 error = 0;
187                 if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_flag & MNT_RDONLY)) {
188                         flags = WRITECLOSE;
189                         if (mp->mnt_flag & MNT_FORCE)
190                                 flags |= FORCECLOSE;
191                         error = vflush(mp, 0, flags);
192                         if (error == 0) {
193                                 devvp = pmp->pm_devvp;
194                                 VOP_OPEN(devvp, FREAD, FSCRED, NULL);
195                                 VOP_CLOSE(devvp, FREAD|FWRITE);
196                                 pmp->pm_flags |= MSDOSFSMNT_RONLY;
197                         }
198                 }
199                 if (!error && (mp->mnt_flag & MNT_RELOAD))
200                         /* not yet implemented */
201                         error = EOPNOTSUPP;
202                 if (error)
203                         return (error);
204                 if ((pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
205                         /*
206                          * If upgrade to read-write by non-root, then verify
207                          * that user has necessary permissions on the device.
208                          */
209                         devvp = pmp->pm_devvp;
210                         vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
211                         if (cred->cr_uid != 0) {
212                                 error = VOP_ACCESS(devvp, VREAD | VWRITE, cred);
213                                 if (error) {
214                                         vn_unlock(devvp);
215                                         return (error);
216                                 }
217                         }
218                         VOP_OPEN(devvp, FREAD|FWRITE, FSCRED, NULL);
219                         VOP_CLOSE(devvp, FREAD);
220                         vn_unlock(devvp);
221                         pmp->pm_flags &= ~MSDOSFSMNT_RONLY;
222                 }
223                 if (args.fspec == 0) {
224 #ifdef  __notyet__              /* doesn't work correctly with current mountd   XXX */
225                         if (args.flags & MSDOSFSMNT_MNTOPT) {
226                                 pmp->pm_flags &= ~MSDOSFSMNT_MNTOPT;
227                                 pmp->pm_flags |= args.flags & MSDOSFSMNT_MNTOPT;
228                                 if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
229                                         pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
230                         }
231 #endif
232                         /*
233                          * Process export requests.
234                          */
235                         return (vfs_export(mp, &pmp->pm_export, &args.export));
236                 }
237         }
238         /*
239          * Not an update, or updating the name: look up the name
240          * and verify that it refers to a sensible block device.
241          */
242         devvp = NULL;
243         error = nlookup_init(&nd, args.fspec, UIO_USERSPACE, NLC_FOLLOW);
244         if (error == 0)
245                 error = nlookup(&nd);
246         if (error == 0)
247                 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
248         nlookup_done(&nd);
249         if (error)
250                 return (error);
251
252         if (!vn_isdisk(devvp, &error)) {
253                 vrele(devvp);
254                 return (error);
255         }
256         /*
257          * If mount by non-root, then verify that user has necessary
258          * permissions on the device.
259          */
260         if (cred->cr_uid != 0) {
261                 accessmode = VREAD;
262                 if ((mp->mnt_flag & MNT_RDONLY) == 0)
263                         accessmode |= VWRITE;
264                 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
265                 error = VOP_ACCESS(devvp, accessmode, cred);
266                 if (error) {
267                         vput(devvp);
268                         return (error);
269                 }
270                 vn_unlock(devvp);
271         }
272         if ((mp->mnt_flag & MNT_UPDATE) == 0) {
273                 error = mountmsdosfs(devvp, mp, &args);
274 #ifdef MSDOSFS_DEBUG            /* only needed for the kprintf below */
275                 pmp = VFSTOMSDOSFS(mp);
276 #endif
277         } else {
278                 if (devvp != pmp->pm_devvp)
279                         error = EINVAL; /* XXX needs translation */
280                 else
281                         vrele(devvp);
282         }
283         if (error) {
284                 vrele(devvp);
285                 return (error);
286         }
287
288         error = update_mp(mp, &args);
289         if (error) {
290                 msdosfs_unmount(mp, MNT_FORCE);
291                 return error;
292         }
293
294         copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
295         bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
296         msdosfs_statfs(mp, &mp->mnt_stat, cred);
297 #ifdef MSDOSFS_DEBUG
298         kprintf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap);
299 #endif
300         return (0);
301 }
302
303 static int
304 mountmsdosfs(struct vnode *devvp, struct mount *mp, struct msdosfs_args *argp)
305 {
306         struct msdosfsmount *pmp;
307         struct buf *bp;
308         cdev_t dev;
309         union bootsector *bsp;
310         struct byte_bpb33 *b33;
311         struct byte_bpb50 *b50;
312         struct byte_bpb710 *b710;
313         u_int8_t SecPerClust;
314         u_long clusters;
315         int     ronly, error;
316
317         /*
318          * Disallow multiple mounts of the same device.
319          * Disallow mounting of a device that is currently in use
320          * Flush out any old buffers remaining from a previous use.
321          */
322         error = vfs_mountedon(devvp);
323         if (error)
324                 return (error);
325         if (count_udev(devvp->v_umajor, devvp->v_uminor) > 0)
326                 return (EBUSY);
327         vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
328         error = vinvalbuf(devvp, V_SAVE, 0, 0);
329         vn_unlock(devvp);
330         if (error)
331                 return (error);
332
333         ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
334         vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
335         error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, NULL);
336         vn_unlock(devvp);
337         if (error)
338                 return (error);
339         dev = devvp->v_rdev;
340         bp  = NULL; /* both used in error_exit */
341         pmp = NULL;
342
343         /*
344          * Read the boot sector of the filesystem, and then check the
345          * boot signature.  If not a dos boot sector then error out.
346          *
347          * NOTE: 2048 is a maximum sector size in current...
348          */
349         error = bread(devvp, 0, 2048, &bp);
350         if (error)
351                 goto error_exit;
352         bp->b_flags |= B_AGE;
353         bsp = (union bootsector *)bp->b_data;
354         b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
355         b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
356         b710 = (struct byte_bpb710 *)bsp->bs710.bsPBP;
357
358 #ifndef __DragonFly__
359         if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
360 #endif
361 #ifndef MSDOSFS_NOCHECKSIG
362                 if (bsp->bs50.bsBootSectSig0 != BOOTSIG0
363                     || bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
364                         error = EINVAL;
365                         goto error_exit;
366                 }
367 #endif
368 #ifndef __DragonFly__
369         }
370 #endif
371
372         pmp = kmalloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK | M_ZERO);
373         pmp->pm_mountp = mp;
374
375         /*
376          * Compute several useful quantities from the bpb in the
377          * bootsector.  Copy in the dos 5 variant of the bpb then fix up
378          * the fields that are different between dos 5 and dos 3.3.
379          */
380         SecPerClust = b50->bpbSecPerClust;
381         pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
382         pmp->pm_ResSectors = getushort(b50->bpbResSectors);
383         pmp->pm_FATs = b50->bpbFATs;
384         pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
385         pmp->pm_Sectors = getushort(b50->bpbSectors);
386         pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
387         pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
388         pmp->pm_Heads = getushort(b50->bpbHeads);
389         pmp->pm_Media = b50->bpbMedia;
390
391         /* calculate the ratio of sector size to DEV_BSIZE */
392         pmp->pm_BlkPerSec = pmp->pm_BytesPerSec / DEV_BSIZE;
393
394 #ifndef __DragonFly__
395         if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
396 #endif
397                 /*
398                  * We don't check pm_Heads nor pm_SecPerTrack, because
399                  * these may not be set for EFI file systems. We don't
400                  * use these anyway, so we're unaffected if they are
401                  * invalid.
402                  */
403                 if (!pmp->pm_BytesPerSec || !SecPerClust) {
404                         error = EINVAL;
405                         goto error_exit;
406                 }
407 #ifndef __DragonFly__
408         }
409 #endif
410
411         if (pmp->pm_Sectors == 0) {
412                 pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
413                 pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
414         } else {
415                 pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
416                 pmp->pm_HugeSectors = pmp->pm_Sectors;
417         }
418
419         if (pmp->pm_RootDirEnts == 0) {
420                 if (bsp->bs710.bsBootSectSig2 != BOOTSIG2
421                     || bsp->bs710.bsBootSectSig3 != BOOTSIG3
422                     || pmp->pm_Sectors
423                     || pmp->pm_FATsecs
424                     || getushort(b710->bpbFSVers)) {
425                         error = EINVAL;
426                         kprintf("mountmsdosfs(): bad FAT32 filesystem\n");
427                         goto error_exit;
428                 }
429                 pmp->pm_fatmask = FAT32_MASK;
430                 pmp->pm_fatmult = 4;
431                 pmp->pm_fatdiv = 1;
432                 pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
433                 if (getushort(b710->bpbExtFlags) & FATMIRROR)
434                         pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
435                 else
436                         pmp->pm_flags |= MSDOSFS_FATMIRROR;
437         } else
438                 pmp->pm_flags |= MSDOSFS_FATMIRROR;
439
440         /*
441          * Check a few values (could do some more):
442          * - logical sector size: power of 2, >= block size
443          * - sectors per cluster: power of 2, >= 1
444          * - number of sectors:   >= 1, <= size of partition
445          */
446         if ( (SecPerClust == 0)
447           || (SecPerClust & (SecPerClust - 1))
448           || (pmp->pm_BytesPerSec < DEV_BSIZE)
449           || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
450           || (pmp->pm_HugeSectors == 0)
451         ) {
452                 error = EINVAL;
453                 goto error_exit;
454         }
455
456         pmp->pm_HugeSectors *= pmp->pm_BlkPerSec;
457         pmp->pm_HiddenSects *= pmp->pm_BlkPerSec; /* XXX not used? */
458         pmp->pm_FATsecs     *= pmp->pm_BlkPerSec;
459         SecPerClust         *= pmp->pm_BlkPerSec;
460
461         pmp->pm_fatblk = pmp->pm_ResSectors * pmp->pm_BlkPerSec;
462
463         if (FAT32(pmp)) {
464                 pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
465                 pmp->pm_firstcluster = pmp->pm_fatblk
466                         + (pmp->pm_FATs * pmp->pm_FATsecs);
467                 pmp->pm_fsinfo = getushort(b710->bpbFSInfo) * pmp->pm_BlkPerSec;
468         } else {
469                 pmp->pm_rootdirblk = pmp->pm_fatblk +
470                         (pmp->pm_FATs * pmp->pm_FATsecs);
471                 pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
472                                        + DEV_BSIZE - 1)
473                         / DEV_BSIZE; /* in blocks */
474                 pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
475         }
476
477         pmp->pm_maxcluster = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
478             SecPerClust + 1;
479         pmp->pm_fatsize = pmp->pm_FATsecs * DEV_BSIZE; /* XXX not used? */
480
481 #ifndef __DragonFly__
482         if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
483                 if ((pmp->pm_maxcluster <= (0xff0 - 2))
484                       && ((dtype == DTYPE_FLOPPY) || ((dtype == DTYPE_VNODE)
485                       && ((pmp->pm_Heads == 1) || (pmp->pm_Heads == 2))))
486                     ) {
487                         pmp->pm_fatmask = FAT12_MASK;
488                         pmp->pm_fatmult = 3;
489                         pmp->pm_fatdiv = 2;
490                 } else {
491                         pmp->pm_fatmask = FAT16_MASK;
492                         pmp->pm_fatmult = 2;
493                         pmp->pm_fatdiv = 1;
494                 }
495         } else 
496 #endif
497         if (pmp->pm_fatmask == 0) {
498                 if (pmp->pm_maxcluster
499                     <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
500                         /*
501                          * This will usually be a floppy disk. This size makes
502                          * sure that one fat entry will not be split across
503                          * multiple blocks.
504                          */
505                         pmp->pm_fatmask = FAT12_MASK;
506                         pmp->pm_fatmult = 3;
507                         pmp->pm_fatdiv = 2;
508                 } else {
509                         pmp->pm_fatmask = FAT16_MASK;
510                         pmp->pm_fatmult = 2;
511                         pmp->pm_fatdiv = 1;
512                 }
513         }
514
515         clusters = (pmp->pm_fatsize / pmp->pm_fatmult) * pmp->pm_fatdiv;
516         if (pmp->pm_maxcluster >= clusters) {
517                 kprintf("Warning: number of clusters (%ld) exceeds FAT "
518                     "capacity (%ld)\n", pmp->pm_maxcluster + 1, clusters);
519                 pmp->pm_maxcluster = clusters - 1;
520         }
521
522
523         if (FAT12(pmp))
524                 pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec;
525         else
526                 pmp->pm_fatblocksize = MSDOSFS_DFLTBSIZE;
527
528         pmp->pm_fatblocksec = pmp->pm_fatblocksize / DEV_BSIZE;
529         pmp->pm_bnshift = DEV_BSHIFT;
530
531         /*
532          * Compute mask and shift value for isolating cluster relative byte
533          * offsets and cluster numbers from a file offset.
534          */
535         pmp->pm_bpcluster = SecPerClust * DEV_BSIZE;
536         pmp->pm_crbomask = pmp->pm_bpcluster - 1;
537         pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
538
539         /*
540          * Check for valid cluster size
541          * must be a power of 2
542          */
543         if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
544                 error = EINVAL;
545                 goto error_exit;
546         }
547
548         /*
549          * Release the bootsector buffer.
550          */
551         brelse(bp);
552         bp = NULL;
553
554         /*
555          * Check FSInfo.
556          */
557         if (pmp->pm_fsinfo) {
558                 struct fsinfo *fp;
559
560                 if ((error = bread(devvp, de_bntodoff(pmp, pmp->pm_fsinfo), fsi_size(pmp), &bp)) != 0)
561                         goto error_exit;
562                 fp = (struct fsinfo *)bp->b_data;
563                 if (!bcmp(fp->fsisig1, "RRaA", 4)
564                     && !bcmp(fp->fsisig2, "rrAa", 4)
565                     && !bcmp(fp->fsisig3, "\0\0\125\252", 4)
566                     && !bcmp(fp->fsisig4, "\0\0\125\252", 4)) {
567                         pmp->pm_nxtfree = getulong(fp->fsinxtfree);
568                         if (pmp->pm_nxtfree == 0xffffffff)
569                                 pmp->pm_nxtfree = CLUST_FIRST;
570                 } else
571                         pmp->pm_fsinfo = 0;
572                 brelse(bp);
573                 bp = NULL;
574         }
575
576         /*
577          * Check and validate (or perhaps invalidate?) the fsinfo structure?
578          */
579         if (pmp->pm_fsinfo && pmp->pm_nxtfree > pmp->pm_maxcluster) {
580                 kprintf(
581                 "Next free cluster in FSInfo (%lu) exceeds maxcluster (%lu)\n",
582                     pmp->pm_nxtfree, pmp->pm_maxcluster);
583                 error = EINVAL;
584                 goto error_exit;
585         }
586
587         /*
588          * Allocate memory for the bitmap of allocated clusters, and then
589          * fill it in.
590          */
591         pmp->pm_inusemap = kmalloc(((pmp->pm_maxcluster + N_INUSEBITS - 1)
592                                    / N_INUSEBITS)
593                                   * sizeof(*pmp->pm_inusemap),
594                                   M_MSDOSFSFAT, M_WAITOK);
595
596         /*
597          * fillinusemap() needs pm_devvp.
598          */
599         pmp->pm_dev = dev;
600         pmp->pm_devvp = devvp;
601
602         /*
603          * Have the inuse map filled in.
604          */
605         if ((error = fillinusemap(pmp)) != 0)
606                 goto error_exit;
607
608         /*
609          * If they want fat updates to be synchronous then let them suffer
610          * the performance degradation in exchange for the on disk copy of
611          * the fat being correct just about all the time.  I suppose this
612          * would be a good thing to turn on if the kernel is still flakey.
613          */
614         if (mp->mnt_flag & MNT_SYNCHRONOUS)
615                 pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
616
617         /*
618          * Finish up.
619          */
620         if (ronly)
621                 pmp->pm_flags |= MSDOSFSMNT_RONLY;
622         else
623                 pmp->pm_fmod = 1;
624         mp->mnt_data = (qaddr_t) pmp;
625         mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
626         mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
627         mp->mnt_flag |= MNT_LOCAL;
628         vfs_add_vnodeops(mp, &msdosfs_vnode_vops, &mp->mnt_vn_norm_ops);
629         dev->si_mountpoint = mp;
630
631         return 0;
632
633 error_exit:
634         if (bp)
635                 brelse(bp);
636         VOP_CLOSE(devvp, ronly ? FREAD : FREAD | FWRITE);
637         if (pmp) {
638                 if (pmp->pm_inusemap)
639                         kfree(pmp->pm_inusemap, M_MSDOSFSFAT);
640                 kfree(pmp, M_MSDOSFSMNT);
641                 mp->mnt_data = (qaddr_t)0;
642         }
643         return (error);
644 }
645
646 /*
647  * Unmount the filesystem described by mp.
648  */
649 static int
650 msdosfs_unmount(struct mount *mp, int mntflags)
651 {
652         struct msdosfsmount *pmp;
653         int error, flags;
654
655         flags = 0;
656         if (mntflags & MNT_FORCE)
657                 flags |= FORCECLOSE;
658         error = vflush(mp, 0, flags);
659         if (error)
660                 return error;
661         pmp = VFSTOMSDOSFS(mp);
662         pmp->pm_devvp->v_rdev->si_mountpoint = NULL;
663 #ifdef MSDOSFS_DEBUG
664         {
665                 struct vnode *vp = pmp->pm_devvp;
666
667                 kprintf("msdosfs_umount(): just before calling VOP_CLOSE()\n");
668                 kprintf("flag %08x, sysrefs %d, writecount %d, auxrefs %d\n",
669                     vp->v_flag, vp->v_sysref.refcnt,
670                     vp->v_writecount, vp->v_auxrefs);
671                 kprintf("mount %p, op %p\n", vp->v_mount, vp->v_ops);
672                 kprintf("mount %p\n", vp->v_mount);
673                 kprintf("cleanblkhd %p, dirtyblkhd %p, numoutput %d, type %d\n",
674                     RB_ROOT(&vp->v_rbclean_tree),
675                     RB_ROOT(&vp->v_rbdirty_tree),
676                     vp->v_track_write.bk_active, vp->v_type);
677                 kprintf("union %p, tag %d, data[0] %08x, data[1] %08x\n",
678                     vp->v_socket, vp->v_tag,
679                     ((u_int *)vp->v_data)[0],
680                     ((u_int *)vp->v_data)[1]);
681         }
682 #endif
683         error = VOP_CLOSE(pmp->pm_devvp,
684                     (pmp->pm_flags&MSDOSFSMNT_RONLY) ? FREAD : FREAD | FWRITE);
685         vrele(pmp->pm_devvp);
686         kfree(pmp->pm_inusemap, M_MSDOSFSFAT);
687         kfree(pmp, M_MSDOSFSMNT);
688         mp->mnt_data = (qaddr_t)0;
689         mp->mnt_flag &= ~MNT_LOCAL;
690         return (error);
691 }
692
693 static int
694 msdosfs_root(struct mount *mp, struct vnode **vpp)
695 {
696         struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
697         struct denode *ndep;
698         int error;
699
700 #ifdef MSDOSFS_DEBUG
701         kprintf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
702 #endif
703         error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep);
704         if (error)
705                 return (error);
706         *vpp = DETOV(ndep);
707         return (0);
708 }
709
710 static int
711 msdosfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
712 {
713         struct msdosfsmount *pmp;
714
715         pmp = VFSTOMSDOSFS(mp);
716         sbp->f_bsize = pmp->pm_bpcluster;
717         sbp->f_iosize = pmp->pm_bpcluster;
718         sbp->f_blocks = pmp->pm_maxcluster + 1;
719         sbp->f_bfree = pmp->pm_freeclustercount;
720         sbp->f_bavail = pmp->pm_freeclustercount;
721         sbp->f_files = pmp->pm_RootDirEnts;                     /* XXX */
722         sbp->f_ffree = 0;       /* what to put in here? */
723         if (sbp != &mp->mnt_stat) {
724                 sbp->f_type = mp->mnt_vfc->vfc_typenum;
725                 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
726         }
727         strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
728         return (0);
729 }
730
731 struct scaninfo {
732         int rescan;
733         int allerror;
734         int waitfor;
735 };
736
737 static int msdosfs_sync_scan(struct mount *mp, struct vnode *vp, void *data);
738
739 static int
740 msdosfs_sync(struct mount *mp, int waitfor)
741 {
742         struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
743         struct scaninfo scaninfo;
744         int error;
745
746         /*
747          * If we ever switch to not updating all of the fats all the time,
748          * this would be the place to update them from the first one.
749          */
750         if (pmp->pm_fmod != 0) {
751                 if (pmp->pm_flags & MSDOSFSMNT_RONLY)
752                         panic("msdosfs_sync: rofs mod");
753                 else {
754                         /* update fats here */
755                 }
756         }
757         /*
758          * Write back each (modified) denode.
759          */
760         scaninfo.allerror = 0;
761         scaninfo.rescan = 1;
762         while (scaninfo.rescan) {
763                 scaninfo.rescan = 0;
764                 vmntvnodescan(mp, VMSC_GETVP|VMSC_NOWAIT, NULL, msdosfs_sync_scan, &scaninfo);
765         }
766
767         /*
768          * Flush filesystem control info.
769          */
770         if (waitfor != MNT_LAZY) {
771                 vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY);
772                 if ((error = VOP_FSYNC(pmp->pm_devvp, waitfor)) != 0)
773                         scaninfo.allerror = error;
774                 vn_unlock(pmp->pm_devvp);
775         }
776         return (scaninfo.allerror);
777 }
778
779 static int
780 msdosfs_sync_scan(struct mount *mp, struct vnode *vp, void *data)
781 {
782         struct scaninfo *info = data;
783         struct denode *dep;
784         int error;
785
786         dep = VTODE(vp);
787         if (vp->v_type == VNON || vp->v_type == VBAD ||
788             ((dep->de_flag &
789             (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 &&
790             (RB_EMPTY(&vp->v_rbdirty_tree) || info->waitfor == MNT_LAZY))) {
791                 return(0);
792         }
793         if ((error = VOP_FSYNC(vp, info->waitfor)) != 0)
794                 info->allerror = error;
795         return(0);
796 }
797
798 static int
799 msdosfs_fhtovp(struct mount *mp, struct vnode *rootvp,
800                struct fid *fhp, struct vnode **vpp)
801 {
802         struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
803         struct defid *defhp = (struct defid *) fhp;
804         struct denode *dep;
805         int error;
806
807         error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep);
808         if (error) {
809                 *vpp = NULLVP;
810                 return (error);
811         }
812         *vpp = DETOV(dep);
813         return (0);
814 }
815
816 static int
817 msdosfs_checkexp(struct mount *mp, struct sockaddr *nam, int *exflagsp,
818                  struct ucred **credanonp)
819 {
820         struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
821         struct netcred *np;
822
823         np = vfs_export_lookup(mp, &pmp->pm_export, nam);
824         if (np == NULL)
825                 return (EACCES);
826         *exflagsp = np->netc_exflags;
827         *credanonp = &np->netc_anon;
828         return (0);
829 }
830
831 static int
832 msdosfs_vptofh(struct vnode *vp, struct fid *fhp)
833 {
834         struct denode *dep;
835         struct defid *defhp;
836
837         dep = VTODE(vp);
838         defhp = (struct defid *)fhp;
839         defhp->defid_len = sizeof(struct defid);
840         defhp->defid_dirclust = dep->de_dirclust;
841         defhp->defid_dirofs = dep->de_diroffset;
842         /* defhp->defid_gen = dep->de_gen; */
843         return (0);
844 }
845
846 static struct vfsops msdosfs_vfsops = {
847         .vfs_mount =            msdosfs_mount,
848         .vfs_unmount =          msdosfs_unmount,
849         .vfs_root =             msdosfs_root,
850         .vfs_statfs =           msdosfs_statfs,
851         .vfs_sync =             msdosfs_sync,
852         .vfs_fhtovp =           msdosfs_fhtovp,
853         .vfs_checkexp =         msdosfs_checkexp,
854         .vfs_vptofh =           msdosfs_vptofh,
855         .vfs_init =             msdosfs_init,
856         .vfs_uninit =           msdosfs_uninit
857 };
858
859 VFS_SET(msdosfs_vfsops, msdos, 0);