sys/vfs/msdosfs: Whitespace cleanups
[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 /*      $NetBSD: msdosfs_vfsops.c,v 1.51 1997/11/17 15:36:58 ws Exp $   */
3
4 /*-
5  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
6  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
7  * All rights reserved.
8  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by TooLs GmbH.
21  * 4. The name of TooLs GmbH may not be used to endorse or promote products
22  *    derived from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
30  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 /*
36  * Written by Paul Popelka (paulp@uts.amdahl.com)
37  *
38  * You can do anything you want with this software, just don't say you wrote
39  * it, and don't remove this notice.
40  *
41  * This software is provided "as is".
42  *
43  * The author supplies this software to be publicly redistributed on the
44  * understanding that the author is not responsible for the correct
45  * functioning of this software in any circumstances and is not liable for
46  * any damages caused by this software.
47  *
48  * October 1992
49  */
50
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/conf.h>
54 #include <sys/proc.h>
55 #include <sys/nlookup.h>
56 #include <sys/kernel.h>
57 #include <sys/vnode.h>
58 #include <sys/iconv.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 <sys/buf2.h>
67
68 #include "bpb.h"
69 #include "bootsect.h"
70 #include "direntry.h"
71 #include "denode.h"
72 #include "msdosfsmount.h"
73 #include "fat.h"
74
75 extern struct vop_ops msdosfs_vnode_vops;
76 struct iconv_functions *msdos_iconv;
77
78 #define MSDOSFS_DFLTBSIZE       4096
79 #define ENCODING_UNICODE        "UTF-16BE"
80 #if 1 /*def PC98*/
81 /*
82  * XXX - The boot signature formatted by NEC PC-98 DOS looks like a
83  *       garbage or a random value :-{
84  *       If you want to use that broken-signatured media, define the
85  *       following symbol even though PC/AT.
86  *       (ex. mount PC-98 DOS formatted FD on PC/AT)
87  */
88 #define MSDOSFS_NOCHECKSIG
89 #endif
90
91 MALLOC_DEFINE(M_MSDOSFSMNT, "MSDOSFS mount", "MSDOSFS mount structure");
92 static MALLOC_DEFINE(M_MSDOSFSFAT, "MSDOSFS FAT", "MSDOSFS file allocation table");
93
94 static int mountmsdosfs(struct vnode *devvp, struct mount *mp,
95     struct msdosfs_args *argp);
96 static int msdosfs_root(struct mount *, struct vnode **);
97 static int msdosfs_statfs(struct mount *, struct statfs *, struct ucred *);
98 static int msdosfs_unmount(struct mount *, int);
99
100 static int
101 update_mp(struct mount *mp, struct msdosfs_args *argp)
102 {
103         struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
104         int error;
105         char cs_local[ICONV_CSNMAXLEN];
106         char cs_dos[ICONV_CSNMAXLEN];
107
108         pmp->pm_gid = argp->gid;
109         pmp->pm_uid = argp->uid;
110         pmp->pm_mask = argp->mask & ALLPERMS;
111         pmp->pm_flags |= argp->flags & MSDOSFSMNT_MNTOPT;
112         if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdos_iconv) {
113                 bcopy(argp->cs_local, cs_local, sizeof(cs_local));
114                 bcopy(argp->cs_dos, cs_dos, sizeof(cs_dos));
115                 kprintf("local: %s dos: %s\n",argp->cs_local, argp->cs_dos);
116                 error = msdos_iconv->open(cs_local, ENCODING_UNICODE,
117                                           &pmp->pm_w2u);
118                 if(error)
119                         return error;
120                 error = msdos_iconv->open(ENCODING_UNICODE, cs_local,
121                                           &pmp->pm_u2w);
122                 if(error)
123                         return error;
124                 error = msdos_iconv->open(cs_dos, cs_local, &pmp->pm_u2d);
125                 if(error)
126                         return error;
127                 error = msdos_iconv->open(cs_local, cs_dos, &pmp->pm_d2u);
128                 if(error)
129                         return error;
130         }
131
132         if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
133                 pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
134         else if (!(pmp->pm_flags &
135             (MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) {
136                 struct vnode *rootvp;
137
138                 /*
139                  * Try to divine whether to support Win'95 long filenames
140                  */
141                 if (FAT32(pmp))
142                         pmp->pm_flags |= MSDOSFSMNT_LONGNAME;
143                 else {
144                         if ((error = msdosfs_root(mp, &rootvp)) != 0)
145                                 return error;
146                         pmp->pm_flags |= findwin95(VTODE(rootvp))
147                                 ? MSDOSFSMNT_LONGNAME : MSDOSFSMNT_SHORTNAME;
148                         vput(rootvp);
149                 }
150         }
151         return 0;
152 }
153
154 /*
155  * mp - path - addr in user space of mount point (ie /usr or whatever)
156  * data - addr in user space of mount params including the name of the block
157  * special file to treat as a filesystem.
158  */
159 static int
160 msdosfs_mount(struct mount *mp, char *path, caddr_t data, struct ucred *cred)
161 {
162         struct vnode *devvp;      /* vnode for blk device to mount */
163         struct msdosfs_args args; /* will hold data from mount request */
164         /* msdosfs specific mount control block */
165         struct msdosfsmount *pmp = NULL;
166         size_t size;
167         int error, flags;
168         mode_t accessmode;
169         struct nlookupdata nd;
170
171         error = copyin(data, (caddr_t)&args, sizeof(struct msdosfs_args));
172         if (error)
173                 return (error);
174         if (args.magic != MSDOSFS_ARGSMAGIC)
175                 args.flags = 0;
176         /*
177          * If updating, check whether changing from read-only to
178          * read/write; if there is no device name, that's all we do.
179          */
180         if (mp->mnt_flag & MNT_UPDATE) {
181                 pmp = VFSTOMSDOSFS(mp);
182                 error = 0;
183                 if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_flag & MNT_RDONLY)) {
184                         flags = WRITECLOSE;
185                         if (mp->mnt_flag & MNT_FORCE)
186                                 flags |= FORCECLOSE;
187                         error = vflush(mp, 0, flags);
188                         if (error == 0) {
189                                 devvp = pmp->pm_devvp;
190                                 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
191                                 VOP_OPEN(devvp, FREAD, FSCRED, NULL);
192                                 VOP_CLOSE(devvp, FREAD|FWRITE, NULL);
193                                 vn_unlock(devvp);
194                                 pmp->pm_flags |= MSDOSFSMNT_RONLY;
195                         }
196                 }
197                 if (!error && (mp->mnt_flag & MNT_RELOAD))
198                         /* not yet implemented */
199                         error = EOPNOTSUPP;
200                 if (error)
201                         return (error);
202                 if ((pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
203                         /*
204                          * If upgrade to read-write by non-root, then verify
205                          * that user has necessary permissions on the device.
206                          */
207                         devvp = pmp->pm_devvp;
208                         vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
209                         if (cred->cr_uid != 0) {
210                                 error = VOP_EACCESS(devvp, VREAD | VWRITE, cred);
211                                 if (error) {
212                                         vn_unlock(devvp);
213                                         return (error);
214                                 }
215                         }
216                         VOP_OPEN(devvp, FREAD|FWRITE, FSCRED, NULL);
217                         VOP_CLOSE(devvp, FREAD, NULL);
218                         vn_unlock(devvp);
219                         pmp->pm_flags &= ~MSDOSFSMNT_RONLY;
220                 }
221                 if (args.fspec == NULL) {
222 #ifdef  __notyet__              /* doesn't work correctly with current mountd   XXX */
223                         if (args.flags & MSDOSFSMNT_MNTOPT) {
224                                 pmp->pm_flags &= ~MSDOSFSMNT_MNTOPT;
225                                 pmp->pm_flags |= args.flags & MSDOSFSMNT_MNTOPT;
226                                 if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
227                                         pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
228                         }
229 #endif
230                         /*
231                          * Process export requests.
232                          */
233                         return (vfs_export(mp, &pmp->pm_export, &args.export));
234                 }
235         }
236         /*
237          * Not an update, or updating the name: look up the name
238          * and verify that it refers to a sensible block device.
239          */
240         devvp = NULL;
241         error = nlookup_init(&nd, args.fspec, UIO_USERSPACE, NLC_FOLLOW);
242         if (error == 0)
243                 error = nlookup(&nd);
244         if (error == 0)
245                 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
246         nlookup_done(&nd);
247         if (error)
248                 return (error);
249
250         if (!vn_isdisk(devvp, &error)) {
251                 vrele(devvp);
252                 return (error);
253         }
254         /*
255          * If mount by non-root, then verify that user has necessary
256          * permissions on the device.
257          */
258         if (cred->cr_uid != 0) {
259                 accessmode = VREAD;
260                 if ((mp->mnt_flag & MNT_RDONLY) == 0)
261                         accessmode |= VWRITE;
262                 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
263                 error = VOP_EACCESS(devvp, accessmode, cred);
264                 if (error) {
265                         vput(devvp);
266                         return (error);
267                 }
268                 vn_unlock(devvp);
269         }
270         if ((mp->mnt_flag & MNT_UPDATE) == 0) {
271                 error = mountmsdosfs(devvp, mp, &args);
272 #ifdef MSDOSFS_DEBUG            /* only needed for the kprintf below */
273                 pmp = VFSTOMSDOSFS(mp);
274 #endif
275         } else {
276                 if (devvp != pmp->pm_devvp)
277                         error = EINVAL; /* XXX needs translation */
278                 else
279                         vrele(devvp);
280         }
281         if (error) {
282                 vrele(devvp);
283                 return (error);
284         }
285
286         error = update_mp(mp, &args);
287         if (error) {
288                 msdosfs_unmount(mp, MNT_FORCE);
289                 return error;
290         }
291
292         copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
293         bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
294         msdosfs_statfs(mp, &mp->mnt_stat, cred);
295 #ifdef MSDOSFS_DEBUG
296         kprintf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap);
297 #endif
298         return (0);
299 }
300
301 static int
302 mountmsdosfs(struct vnode *devvp, struct mount *mp, struct msdosfs_args *argp)
303 {
304         struct msdosfsmount *pmp;
305         struct buf *bp;
306         cdev_t dev;
307         union bootsector *bsp;
308         struct byte_bpb33 *b33;
309         struct byte_bpb50 *b50;
310         struct byte_bpb710 *b710;
311         u_int8_t SecPerClust;
312         u_long clusters;
313         int     ronly, error;
314
315         /*
316          * Disallow multiple mounts of the same device.
317          * Disallow mounting of a device that is currently in use
318          * Flush out any old buffers remaining from a previous use.
319          */
320         error = vfs_mountedon(devvp);
321         if (error)
322                 return (error);
323         if (vcount(devvp) > 0)
324                 return (EBUSY);
325         vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
326         error = vinvalbuf(devvp, V_SAVE, 0, 0);
327         vn_unlock(devvp);
328         if (error)
329                 return (error);
330
331         ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
332         vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
333         error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, NULL);
334         vn_unlock(devvp);
335         if (error)
336                 return (error);
337         dev = devvp->v_rdev;
338         bp  = NULL; /* both used in error_exit */
339         pmp = NULL;
340
341         /*
342          * Read the boot sector of the filesystem, and then check the
343          * boot signature.  If not a dos boot sector then error out.
344          *
345          * NOTE: 2048 is a maximum sector size in current...
346          */
347         error = bread(devvp, 0, 2048, &bp);
348         if (error)
349                 goto error_exit;
350         bp->b_flags |= B_AGE;
351         bsp = (union bootsector *)bp->b_data;
352         b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
353         b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
354         b710 = (struct byte_bpb710 *)bsp->bs710.bsPBP;
355
356 #ifndef MSDOSFS_NOCHECKSIG
357         if (bsp->bs50.bsBootSectSig0 != BOOTSIG0
358             || bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
359                 error = EINVAL;
360                 goto error_exit;
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         if (pmp->pm_BytesPerSec < DEV_BSIZE) {
375                 error = EINVAL;
376                 goto error_exit;
377         }
378         pmp->pm_ResSectors = getushort(b50->bpbResSectors);
379         pmp->pm_FATs = b50->bpbFATs;
380         pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
381         pmp->pm_Sectors = getushort(b50->bpbSectors);
382         pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
383         pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
384         pmp->pm_Heads = getushort(b50->bpbHeads);
385         pmp->pm_Media = b50->bpbMedia;
386
387         /* calculate the ratio of sector size to DEV_BSIZE */
388         pmp->pm_BlkPerSec = pmp->pm_BytesPerSec / DEV_BSIZE;
389
390         /*
391          * We don't check pm_Heads nor pm_SecPerTrack, because
392          * these may not be set for EFI file systems. We don't
393          * use these anyway, so we're unaffected if they are
394          * invalid.
395          */
396         if (!pmp->pm_BytesPerSec || !SecPerClust) {
397                 error = EINVAL;
398                 goto error_exit;
399         }
400
401         if (pmp->pm_Sectors == 0) {
402                 pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
403                 pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
404         } else {
405                 pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
406                 pmp->pm_HugeSectors = pmp->pm_Sectors;
407         }
408
409         if (pmp->pm_RootDirEnts == 0) {
410                 if (pmp->pm_Sectors
411                     || pmp->pm_FATsecs
412                     || getushort(b710->bpbFSVers)) {
413                         error = EINVAL;
414                         kprintf("mountmsdosfs(): bad FAT32 filesystem\n");
415                         goto error_exit;
416                 }
417                 pmp->pm_fatmask = FAT32_MASK;
418                 pmp->pm_fatmult = 4;
419                 pmp->pm_fatdiv = 1;
420                 pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
421                 if (getushort(b710->bpbExtFlags) & FATMIRROR)
422                         pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
423                 else
424                         pmp->pm_flags |= MSDOSFS_FATMIRROR;
425         } else
426                 pmp->pm_flags |= MSDOSFS_FATMIRROR;
427
428         /*
429          * Check a few values (could do some more):
430          * - logical sector size: power of 2, >= block size
431          * - sectors per cluster: power of 2, >= 1
432          * - number of sectors:   >= 1, <= size of partition
433          * - number of FAT sectors: >= 1
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           || (pmp->pm_FATsecs == 0)
441           || (SecPerClust * pmp->pm_BlkPerSec > MAXBSIZE / DEV_BSIZE)) {
442                 error = EINVAL;
443                 goto error_exit;
444         }
445
446         pmp->pm_HugeSectors *= pmp->pm_BlkPerSec;
447         pmp->pm_HiddenSects *= pmp->pm_BlkPerSec; /* XXX not used? */
448         pmp->pm_FATsecs     *= pmp->pm_BlkPerSec;
449         SecPerClust         *= pmp->pm_BlkPerSec;
450
451         pmp->pm_fatblk = pmp->pm_ResSectors * pmp->pm_BlkPerSec;
452
453         if (FAT32(pmp)) {
454                 pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
455                 pmp->pm_firstcluster = pmp->pm_fatblk
456                         + (pmp->pm_FATs * pmp->pm_FATsecs);
457                 pmp->pm_fsinfo = getushort(b710->bpbFSInfo) * pmp->pm_BlkPerSec;
458         } else {
459                 pmp->pm_rootdirblk = pmp->pm_fatblk +
460                         (pmp->pm_FATs * pmp->pm_FATsecs);
461                 pmp->pm_rootdirsize = (pmp->pm_RootDirEnts *
462                         sizeof(struct direntry) + DEV_BSIZE - 1)
463                         / DEV_BSIZE; /* in blocks */
464                 pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
465         }
466
467         pmp->pm_maxcluster = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
468             SecPerClust + 1;
469         pmp->pm_fatsize = pmp->pm_FATsecs * DEV_BSIZE; /* XXX not used? */
470
471         if (pmp->pm_fatmask == 0) {
472                 if (pmp->pm_maxcluster
473                     <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
474                         /*
475                          * This will usually be a floppy disk. This size makes
476                          * sure that one fat entry will not be split across
477                          * multiple blocks.
478                          */
479                         pmp->pm_fatmask = FAT12_MASK;
480                         pmp->pm_fatmult = 3;
481                         pmp->pm_fatdiv = 2;
482                 } else {
483                         pmp->pm_fatmask = FAT16_MASK;
484                         pmp->pm_fatmult = 2;
485                         pmp->pm_fatdiv = 1;
486                 }
487         }
488
489         clusters = (pmp->pm_fatsize / pmp->pm_fatmult) * pmp->pm_fatdiv;
490         if (pmp->pm_maxcluster >= clusters) {
491                 kprintf("Warning: number of clusters (%ld) exceeds FAT "
492                     "capacity (%ld)\n", pmp->pm_maxcluster + 1, clusters);
493                 pmp->pm_maxcluster = clusters - 1;
494         }
495
496         if (FAT12(pmp))
497                 pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec;
498         else
499                 pmp->pm_fatblocksize = MSDOSFS_DFLTBSIZE;
500
501         pmp->pm_fatblocksec = pmp->pm_fatblocksize / DEV_BSIZE;
502         pmp->pm_bnshift = DEV_BSHIFT;
503
504         /*
505          * Compute mask and shift value for isolating cluster relative byte
506          * offsets and cluster numbers from a file offset.
507          */
508         pmp->pm_bpcluster = SecPerClust * DEV_BSIZE;
509         pmp->pm_crbomask = pmp->pm_bpcluster - 1;
510         pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
511
512         /*
513          * Check for valid cluster size
514          * must be a power of 2
515          */
516         if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
517                 error = EINVAL;
518                 goto error_exit;
519         }
520
521         /*
522          * Release the bootsector buffer.
523          */
524         bp->b_flags |= B_RELBUF;
525         brelse(bp);
526         bp = NULL;
527
528         /*
529          * Check FSInfo.
530          */
531         if (pmp->pm_fsinfo) {
532                 struct fsinfo *fp;
533
534                 if ((error = bread(devvp, de_bntodoff(pmp, pmp->pm_fsinfo),
535                     fsi_size(pmp), &bp)) != 0)
536                         goto error_exit;
537                 fp = (struct fsinfo *)bp->b_data;
538                 if (!bcmp(fp->fsisig1, "RRaA", 4) &&
539                     !bcmp(fp->fsisig2, "rrAa", 4) &&
540                     !bcmp(fp->fsisig3, "\0\0\125\252", 4) &&
541                     !bcmp(fp->fsisig4, "\0\0\125\252", 4)) {
542                         pmp->pm_nxtfree = getulong(fp->fsinxtfree);
543                         if (pmp->pm_nxtfree == (u_long)-1)
544                                 pmp->pm_nxtfree = CLUST_FIRST;
545                         if (pmp->pm_nxtfree == (u_int)-1) {
546                                 kprintf("msdosfs_mount(): "
547                                         "ignoring illegal nxtfree 0x%016jx\n",
548                                         (uintmax_t)pmp->pm_nxtfree);
549                                 pmp->pm_nxtfree = CLUST_FIRST;
550                         }
551                 } else
552                         pmp->pm_fsinfo = 0;
553                 bp->b_flags |= B_RELBUF;
554                 brelse(bp);
555                 bp = NULL;
556         }
557
558         /*
559          * Check and validate (or perhaps invalidate?) the fsinfo structure?
560          */
561         if (pmp->pm_fsinfo && pmp->pm_nxtfree > pmp->pm_maxcluster) {
562                 kprintf(
563                 "Next free cluster in FSInfo (%lu) exceeds maxcluster (%lu)\n",
564                     pmp->pm_nxtfree, pmp->pm_maxcluster);
565                 error = EINVAL;
566                 goto error_exit;
567         }
568
569         /*
570          * Allocate memory for the bitmap of allocated clusters, and then
571          * fill it in.
572          */
573         pmp->pm_inusemap = kmalloc(((pmp->pm_maxcluster + N_INUSEBITS - 1)
574                                    / N_INUSEBITS) * sizeof(*pmp->pm_inusemap),
575                                    M_MSDOSFSFAT, M_WAITOK);
576
577         /*
578          * fillinusemap() needs pm_devvp.
579          */
580         pmp->pm_dev = dev;
581         pmp->pm_devvp = devvp;
582
583         /*
584          * Have the inuse map filled in.
585          */
586         if ((error = fillinusemap(pmp)) != 0)
587                 goto error_exit;
588
589         /*
590          * If they want fat updates to be synchronous then let them suffer
591          * the performance degradation in exchange for the on disk copy of
592          * the fat being correct just about all the time.  I suppose this
593          * would be a good thing to turn on if the kernel is still flakey.
594          */
595         if (mp->mnt_flag & MNT_SYNCHRONOUS)
596                 pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
597
598         /*
599          * Finish up.
600          */
601         if (ronly)
602                 pmp->pm_flags |= MSDOSFSMNT_RONLY;
603         else
604                 pmp->pm_fmod = 1;
605         mp->mnt_data = (qaddr_t) pmp;
606         mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
607         mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
608         mp->mnt_flag |= MNT_LOCAL;
609         vfs_add_vnodeops(mp, &msdosfs_vnode_vops, &mp->mnt_vn_norm_ops);
610         dev->si_mountpoint = mp;
611
612         return 0;
613
614 error_exit:
615         if (bp) {
616                 bp->b_flags |= B_RELBUF;
617                 brelse(bp);
618         }
619         vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
620         VOP_CLOSE(devvp, ronly ? FREAD : FREAD | FWRITE, NULL);
621         vn_unlock(devvp);
622         if (pmp) {
623                 if (pmp->pm_inusemap)
624                         kfree(pmp->pm_inusemap, M_MSDOSFSFAT);
625                 kfree(pmp, M_MSDOSFSMNT);
626                 mp->mnt_data = (qaddr_t)0;
627         }
628         return (error);
629 }
630
631 /*
632  * Unmount the filesystem described by mp.
633  */
634 static int
635 msdosfs_unmount(struct mount *mp, int mntflags)
636 {
637         struct msdosfsmount *pmp;
638         int error, flags;
639
640         flags = 0;
641         if (mntflags & MNT_FORCE)
642                 flags |= FORCECLOSE;
643         error = vflush(mp, 0, flags);
644         if (error)
645                 return error;
646         pmp = VFSTOMSDOSFS(mp);
647         pmp->pm_devvp->v_rdev->si_mountpoint = NULL;
648         if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdos_iconv) {
649                 if(pmp->pm_w2u)
650                         msdos_iconv->close(pmp->pm_w2u);
651                 if(pmp->pm_u2w)
652                         msdos_iconv->close(pmp->pm_u2w);
653                 if(pmp->pm_d2u)
654                         msdos_iconv->close(pmp->pm_d2u);
655                 if(pmp->pm_u2d)
656                         msdos_iconv->close(pmp->pm_u2d);
657         }
658 #ifdef MSDOSFS_DEBUG
659         {
660                 struct vnode *vp = pmp->pm_devvp;
661
662                 kprintf("msdosfs_umount(): just before calling VOP_CLOSE()\n");
663                 kprintf("flag %08x, refcnt 0x%08x, writecount %d, "
664                         "auxrefs 0x%08x\n",
665                         vp->v_flag, vp->v_refcnt,
666                         vp->v_writecount, vp->v_auxrefs);
667                 kprintf("mount %p, op %p\n", vp->v_mount, vp->v_ops);
668                 kprintf("mount %p\n", vp->v_mount);
669                 kprintf("cleanblkhd %p, dirtyblkhd %p, numoutput %d, type %d\n",
670                     RB_ROOT(&vp->v_rbclean_tree),
671                     RB_ROOT(&vp->v_rbdirty_tree),
672                     bio_track_active(&vp->v_track_write),
673                     vp->v_type);
674                 kprintf("union %p, tag %d, data[0] %08x, data[1] %08x\n",
675                     vp->v_socket, vp->v_tag,
676                     ((u_int *)vp->v_data)[0],
677                     ((u_int *)vp->v_data)[1]);
678         }
679 #endif
680         vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY);
681         error = VOP_CLOSE(pmp->pm_devvp,
682                     (pmp->pm_flags&MSDOSFSMNT_RONLY) ? FREAD : FREAD | FWRITE,
683                     NULL);
684         vn_unlock(pmp->pm_devvp);
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,
765                               msdosfs_sync_scan, &scaninfo);
766         }
767
768         /*
769          * Flush filesystem control info.
770          */
771         if ((waitfor & MNT_LAZY) == 0) {
772                 vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY);
773                 if ((error = VOP_FSYNC(pmp->pm_devvp, waitfor, 0)) != 0)
774                         scaninfo.allerror = error;
775                 vn_unlock(pmp->pm_devvp);
776         }
777         return (scaninfo.allerror);
778 }
779
780 static int
781 msdosfs_sync_scan(struct mount *mp, struct vnode *vp, void *data)
782 {
783         struct scaninfo *info = data;
784         struct denode *dep;
785         int error;
786
787         dep = VTODE(vp);
788         if (vp->v_type == VNON || vp->v_type == VBAD ||
789             ((dep->de_flag &
790             (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 &&
791             (RB_EMPTY(&vp->v_rbdirty_tree) || (info->waitfor & MNT_LAZY)))) {
792                 return(0);
793         }
794         if ((error = VOP_FSYNC(vp, info->waitfor, 0)) != 0)
795                 info->allerror = error;
796         return(0);
797 }
798
799 static int
800 msdosfs_fhtovp(struct mount *mp, struct vnode *rootvp,
801                struct fid *fhp, struct vnode **vpp)
802 {
803         struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
804         struct defid *defhp = (struct defid *) fhp;
805         struct denode *dep;
806         int error;
807
808         error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep);
809         if (error) {
810                 *vpp = NULLVP;
811                 return (error);
812         }
813         *vpp = DETOV(dep);
814         return (0);
815 }
816
817 static int
818 msdosfs_checkexp(struct mount *mp, struct sockaddr *nam, int *exflagsp,
819                  struct ucred **credanonp)
820 {
821         struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
822         struct netcred *np;
823
824         np = vfs_export_lookup(mp, &pmp->pm_export, nam);
825         if (np == NULL)
826                 return (EACCES);
827         *exflagsp = np->netc_exflags;
828         *credanonp = &np->netc_anon;
829         return (0);
830 }
831
832 static int
833 msdosfs_vptofh(struct vnode *vp, struct fid *fhp)
834 {
835         struct denode *dep;
836         struct defid *defhp;
837
838         dep = VTODE(vp);
839         defhp = (struct defid *)fhp;
840         defhp->defid_len = sizeof(struct defid);
841         defhp->defid_dirclust = dep->de_dirclust;
842         defhp->defid_dirofs = dep->de_diroffset;
843         /* defhp->defid_gen = dep->de_gen; */
844         return (0);
845 }
846
847 static struct vfsops msdosfs_vfsops = {
848         .vfs_mount =            msdosfs_mount,
849         .vfs_unmount =          msdosfs_unmount,
850         .vfs_root =             msdosfs_root,
851         .vfs_statfs =           msdosfs_statfs,
852         .vfs_sync =             msdosfs_sync,
853         .vfs_fhtovp =           msdosfs_fhtovp,
854         .vfs_checkexp =         msdosfs_checkexp,
855         .vfs_vptofh =           msdosfs_vptofh,
856         .vfs_init =             msdosfs_init,
857         .vfs_uninit =           msdosfs_uninit
858 };
859
860 VFS_SET(msdosfs_vfsops, msdos, 0);
861 MODULE_VERSION(msdos, 1);