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