]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/msdosfs/msdosfs_vfsops.c
Include <sys/mutex.h>'s prerequisite <sys/lock.h> instead of depending on
[FreeBSD/FreeBSD.git] / sys / fs / msdosfs / msdosfs_vfsops.c
1 /* $FreeBSD$ */
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/lock.h>
55 #include <sys/mutex.h>
56 #include <sys/namei.h>
57 #include <sys/priv.h>
58 #include <sys/proc.h>
59 #include <sys/kernel.h>
60 #include <sys/vnode.h>
61 #include <sys/mount.h>
62 #include <sys/buf.h>
63 #include <sys/malloc.h>
64 #include <sys/stat.h>                           /* defines ALLPERMS */
65 #include <sys/iconv.h>
66
67 #include <fs/msdosfs/bpb.h>
68 #include <fs/msdosfs/bootsect.h>
69 #include <fs/msdosfs/msdosfsmount.h>
70 #include <fs/msdosfs/direntry.h>
71 #include <fs/msdosfs/denode.h>
72 #include <fs/msdosfs/fat.h>
73
74 #include <geom/geom.h>
75 #include <geom/geom_vfs.h>
76
77 /* List of mount options we support */
78 static const char *msdosfs_opts[] = {
79         "from",
80         "atime", "export", "force", "sync",
81         "uid", "gid", "mask", "dirmask",
82         "shortname", "shortnames", "longname", "longnames", "nowin95", "win95",
83         "kiconv", "cs_win", "cs_dos", "cs_local", "large",
84         NULL
85 };
86
87 #if 1 /*def PC98*/
88 /*
89  * XXX - The boot signature formatted by NEC PC-98 DOS looks like a
90  *       garbage or a random value :-{
91  *       If you want to use that broken-signatured media, define the
92  *       following symbol even though PC/AT.
93  *       (ex. mount PC-98 DOS formatted FD on PC/AT)
94  */
95 #define MSDOSFS_NOCHECKSIG
96 #endif
97
98 MALLOC_DEFINE(M_MSDOSFSMNT, "msdosfs_mount", "MSDOSFS mount structure");
99 static MALLOC_DEFINE(M_MSDOSFSFAT, "msdosfs_fat", "MSDOSFS file allocation table");
100
101 struct iconv_functions *msdosfs_iconv = NULL;
102
103 static int      update_mp(struct mount *mp, struct thread *td);
104 static int      mountmsdosfs(struct vnode *devvp, struct mount *mp,
105                     struct thread *td);
106 static vfs_fhtovp_t     msdosfs_fhtovp;
107 static vfs_mount_t      msdosfs_mount;
108 static vfs_root_t       msdosfs_root;
109 static vfs_statfs_t     msdosfs_statfs;
110 static vfs_sync_t       msdosfs_sync;
111 static vfs_unmount_t    msdosfs_unmount;
112
113 /* Maximum length of a character set name (arbitrary). */
114 #define MAXCSLEN        64
115
116 static int
117 update_mp(struct mount *mp, struct thread *td)
118 {
119         struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
120         void *dos, *win, *local;
121         int error, v;
122
123         if (!vfs_getopt(mp->mnt_optnew, "kiconv", NULL, NULL)) {
124                 if (msdosfs_iconv != NULL) {
125                         error = vfs_getopt(mp->mnt_optnew,
126                             "cs_win", &win, NULL);
127                         if (!error)
128                                 error = vfs_getopt(mp->mnt_optnew,
129                                     "cs_local", &local, NULL);
130                         if (!error)
131                                 error = vfs_getopt(mp->mnt_optnew,
132                                     "cs_dos", &dos, NULL);
133                         if (!error) {
134                                 msdosfs_iconv->open(win, local, &pmp->pm_u2w);
135                                 msdosfs_iconv->open(local, win, &pmp->pm_w2u);
136                                 msdosfs_iconv->open(dos, local, &pmp->pm_u2d);
137                                 msdosfs_iconv->open(local, dos, &pmp->pm_d2u);
138                         }
139                         if (error != 0)
140                                 return (error);
141                 } else {
142                         pmp->pm_w2u = NULL;
143                         pmp->pm_u2w = NULL;
144                         pmp->pm_d2u = NULL;
145                         pmp->pm_u2d = NULL;
146                 }
147         }
148
149         if (1 == vfs_scanopt(mp->mnt_optnew, "gid", "%d", &v))
150                 pmp->pm_gid = v;
151         if (1 == vfs_scanopt(mp->mnt_optnew, "uid", "%d", &v))
152                 pmp->pm_uid = v;
153         if (1 == vfs_scanopt(mp->mnt_optnew, "mask", "%d", &v))
154                 pmp->pm_mask = v & ALLPERMS;
155         if (1 == vfs_scanopt(mp->mnt_optnew, "dirmask", "%d", &v))
156                 pmp->pm_dirmask = v & ALLPERMS;
157         vfs_flagopt(mp->mnt_optnew, "shortname",
158             &pmp->pm_flags, MSDOSFSMNT_SHORTNAME);
159         vfs_flagopt(mp->mnt_optnew, "shortnames",
160             &pmp->pm_flags, MSDOSFSMNT_SHORTNAME);
161         vfs_flagopt(mp->mnt_optnew, "longname",
162             &pmp->pm_flags, MSDOSFSMNT_LONGNAME);
163         vfs_flagopt(mp->mnt_optnew, "longnames",
164             &pmp->pm_flags, MSDOSFSMNT_LONGNAME);
165         vfs_flagopt(mp->mnt_optnew, "kiconv",
166             &pmp->pm_flags, MSDOSFSMNT_KICONV);
167
168         if (vfs_getopt(mp->mnt_optnew, "nowin95", NULL, NULL) == 0)
169                 pmp->pm_flags |= MSDOSFSMNT_NOWIN95;
170         else
171                 pmp->pm_flags &= ~MSDOSFSMNT_NOWIN95;
172
173         if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
174                 pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
175         else if (!(pmp->pm_flags &
176             (MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) {
177                 struct vnode *rootvp;
178
179                 /*
180                  * Try to divine whether to support Win'95 long filenames
181                  */
182                 if (FAT32(pmp))
183                         pmp->pm_flags |= MSDOSFSMNT_LONGNAME;
184                 else {
185                         if ((error =
186                             msdosfs_root(mp, LK_EXCLUSIVE, &rootvp, td)) != 0)
187                                 return error;
188                         pmp->pm_flags |= findwin95(VTODE(rootvp))
189                                 ? MSDOSFSMNT_LONGNAME
190                                         : MSDOSFSMNT_SHORTNAME;
191                         vput(rootvp);
192                 }
193         }
194         return 0;
195 }
196
197 static int
198 msdosfs_cmount(struct mntarg *ma, void *data, int flags, struct thread *td)
199 {
200         struct msdosfs_args args;
201         int error;
202
203         if (data == NULL)
204                 return (EINVAL);
205         error = copyin(data, &args, sizeof args);
206         if (error)
207                 return (error);
208
209         ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
210         ma = mount_arg(ma, "export", &args.export, sizeof args.export);
211         ma = mount_argf(ma, "uid", "%d", args.uid);
212         ma = mount_argf(ma, "gid", "%d", args.gid);
213         ma = mount_argf(ma, "mask", "%d", args.mask);
214         ma = mount_argf(ma, "dirmask", "%d", args.dirmask);
215
216         ma = mount_argb(ma, args.flags & MSDOSFSMNT_SHORTNAME, "noshortname");
217         ma = mount_argb(ma, args.flags & MSDOSFSMNT_LONGNAME, "nolongname");
218         ma = mount_argb(ma, !(args.flags & MSDOSFSMNT_NOWIN95), "nowin95");
219         ma = mount_argb(ma, args.flags & MSDOSFSMNT_KICONV, "nokiconv");
220
221         ma = mount_argsu(ma, "cs_win", args.cs_win, MAXCSLEN);
222         ma = mount_argsu(ma, "cs_dos", args.cs_dos, MAXCSLEN);
223         ma = mount_argsu(ma, "cs_local", args.cs_local, MAXCSLEN);
224
225         error = kernel_mount(ma, flags);
226
227         return (error);
228 }
229
230 /*
231  * mp - path - addr in user space of mount point (ie /usr or whatever)
232  * data - addr in user space of mount params including the name of the block
233  * special file to treat as a filesystem.
234  */
235 static int
236 msdosfs_mount(struct mount *mp, struct thread *td)
237 {
238         struct vnode *devvp;      /* vnode for blk device to mount */
239         /* msdosfs specific mount control block */
240         struct msdosfsmount *pmp = NULL;
241         struct nameidata ndp;
242         int error, flags;
243         mode_t accessmode;
244         char *from;
245
246         if (vfs_filteropt(mp->mnt_optnew, msdosfs_opts))
247                 return (EINVAL);
248
249         /*
250          * If updating, check whether changing from read-only to
251          * read/write; if there is no device name, that's all we do.
252          */
253         if (mp->mnt_flag & MNT_UPDATE) {
254                 int ro_to_rw = 0;
255                 pmp = VFSTOMSDOSFS(mp);
256
257                 if (vfs_flagopt(mp->mnt_optnew, "export", NULL, 0)) {
258                         /*
259                          * Forbid export requests if filesystem has
260                          * MSDOSFS_LARGEFS flag set.
261                          */
262                         if ((pmp->pm_flags & MSDOSFS_LARGEFS) != 0) {
263                                 vfs_mount_error(mp,
264                                     "MSDOSFS_LARGEFS flag set, cannot export");
265                                 return (EOPNOTSUPP);
266                         }
267                 }
268                 if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) &&
269                     vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
270                         error = VFS_SYNC(mp, MNT_WAIT, td);
271                         if (error)
272                                 return (error);
273                         flags = WRITECLOSE;
274                         if (mp->mnt_flag & MNT_FORCE)
275                                 flags |= FORCECLOSE;
276                         error = vflush(mp, 0, flags, td);
277                         if (error)
278                                 return (error);
279                         DROP_GIANT();
280                         g_topology_lock();
281                         error = g_access(pmp->pm_cp, 0, -1, 0);
282                         g_topology_unlock();
283                         PICKUP_GIANT();
284                         if (error)
285                                 return (error);
286
287                         /* Now the volume is clean. Mark it. */
288                         error = markvoldirty(pmp, 0);
289                         if (error && (flags & FORCECLOSE) == 0)
290                                 return (error);
291                 } else if ((pmp->pm_flags & MSDOSFSMNT_RONLY) &&
292                     !vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
293                         /*
294                          * If upgrade to read-write by non-root, then verify
295                          * that user has necessary permissions on the device.
296                          */
297                         devvp = pmp->pm_devvp;
298                         vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
299                         error = VOP_ACCESS(devvp, VREAD | VWRITE,
300                            td->td_ucred, td);
301                         if (error)
302                                 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
303                         if (error) {
304                                 VOP_UNLOCK(devvp, 0, td);
305                                 return (error);
306                         }
307                         VOP_UNLOCK(devvp, 0, td);
308                         DROP_GIANT();
309                         g_topology_lock();
310                         error = g_access(pmp->pm_cp, 0, 1, 0);
311                         g_topology_unlock();
312                         PICKUP_GIANT();
313                         if (error)
314                                 return (error);
315
316                         ro_to_rw = 1;
317                 }
318                 vfs_flagopt(mp->mnt_optnew, "ro",
319                     &pmp->pm_flags, MSDOSFSMNT_RONLY);
320                 vfs_flagopt(mp->mnt_optnew, "ro",
321                     &mp->mnt_flag, MNT_RDONLY);
322
323                 if (ro_to_rw) {
324                         /* Now that the volume is modifiable, mark it dirty. */
325                         error = markvoldirty(pmp, 1);
326                         if (error)
327                                 return (error);
328                 }
329         }
330         /*
331          * Not an update, or updating the name: look up the name
332          * and verify that it refers to a sensible disk device.
333          */
334         if (vfs_getopt(mp->mnt_optnew, "from", (void **)&from, NULL))
335                 return (EINVAL);
336         NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, from, td);
337         error = namei(&ndp);
338         if (error)
339                 return (error);
340         devvp = ndp.ni_vp;
341         NDFREE(&ndp, NDF_ONLY_PNBUF);
342
343         if (!vn_isdisk(devvp, &error)) {
344                 vput(devvp);
345                 return (error);
346         }
347         /*
348          * If mount by non-root, then verify that user has necessary
349          * permissions on the device.
350          */
351         accessmode = VREAD;
352         if ((mp->mnt_flag & MNT_RDONLY) == 0)
353                 accessmode |= VWRITE;
354         error = VOP_ACCESS(devvp, accessmode, td->td_ucred, td);
355         if (error)
356                 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
357         if (error) {
358                 vput(devvp);
359                 return (error);
360         }
361         if ((mp->mnt_flag & MNT_UPDATE) == 0) {
362                 error = mountmsdosfs(devvp, mp, td);
363 #ifdef MSDOSFS_DEBUG            /* only needed for the printf below */
364                 pmp = VFSTOMSDOSFS(mp);
365 #endif
366         } else {
367                 if (devvp != pmp->pm_devvp)
368                         error = EINVAL; /* XXX needs translation */
369                 else
370                         vput(devvp);
371         }
372         if (error) {
373                 vrele(devvp);
374                 return (error);
375         }
376
377         error = update_mp(mp, td);
378         if (error) {
379                 if ((mp->mnt_flag & MNT_UPDATE) == 0)
380                         msdosfs_unmount(mp, MNT_FORCE, td);
381                 return error;
382         }
383         
384         vfs_mountedfrom(mp, from);
385 #ifdef MSDOSFS_DEBUG
386         printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap);
387 #endif
388         return (0);
389 }
390
391 static int
392 mountmsdosfs(struct vnode *devvp, struct mount *mp, struct thread *td)
393 {
394         struct msdosfsmount *pmp;
395         struct buf *bp;
396         struct cdev *dev = devvp->v_rdev;
397         union bootsector *bsp;
398         struct byte_bpb33 *b33;
399         struct byte_bpb50 *b50;
400         struct byte_bpb710 *b710;
401         u_int8_t SecPerClust;
402         u_long clusters;
403         int     ronly, error;
404         struct g_consumer *cp;
405         struct bufobj *bo;
406
407         ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
408         /* XXX: use VOP_ACCESS to check FS perms */
409         DROP_GIANT();
410         g_topology_lock();
411         error = g_vfs_open(devvp, &cp, "msdosfs", ronly ? 0 : 1);
412         g_topology_unlock();
413         PICKUP_GIANT();
414         VOP_UNLOCK(devvp, 0, td);
415         if (error)
416                 return (error);
417
418         bo = &devvp->v_bufobj;
419         bp  = NULL; /* both used in error_exit */
420         pmp = NULL;
421
422         /*
423          * Read the boot sector of the filesystem, and then check the
424          * boot signature.  If not a dos boot sector then error out.
425          *
426          * NOTE: 8192 is a magic size that works for ffs.
427          */
428         error = bread(devvp, 0, 8192, NOCRED, &bp);
429         if (error)
430                 goto error_exit;
431         bp->b_flags |= B_AGE;
432         bsp = (union bootsector *)bp->b_data;
433         b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
434         b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
435         b710 = (struct byte_bpb710 *)bsp->bs710.bsBPB;
436
437 #ifndef MSDOSFS_NOCHECKSIG
438         if (bsp->bs50.bsBootSectSig0 != BOOTSIG0
439             || bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
440                 error = EINVAL;
441                 goto error_exit;
442         }
443 #endif
444
445         pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK | M_ZERO);
446         pmp->pm_mountp = mp;
447         pmp->pm_cp = cp;
448         pmp->pm_bo = bo;
449
450         /*
451          * Initialize ownerships and permissions, since nothing else will
452          * initialize them iff we are mounting root.
453          */
454         pmp->pm_uid = UID_ROOT;
455         pmp->pm_gid = GID_WHEEL;
456         pmp->pm_mask = pmp->pm_dirmask = S_IXUSR | S_IXGRP | S_IXOTH |
457             S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR;
458
459         /*
460          * Experimental support for large MS-DOS filesystems.
461          * WARNING: This uses at least 32 bytes of kernel memory (which is not
462          * reclaimed until the FS is unmounted) for each file on disk to map
463          * between the 32-bit inode numbers used by VFS and the 64-bit
464          * pseudo-inode numbers used internally by msdosfs. This is only
465          * safe to use in certain controlled situations (e.g. read-only FS
466          * with less than 1 million files).
467          * Since the mappings do not persist across unmounts (or reboots), these
468          * filesystems are not suitable for exporting through NFS, or any other
469          * application that requires fixed inode numbers.
470          */
471         vfs_flagopt(mp->mnt_optnew, "large", &pmp->pm_flags,
472           MSDOSFS_LARGEFS);
473
474         /*
475          * Compute several useful quantities from the bpb in the
476          * bootsector.  Copy in the dos 5 variant of the bpb then fix up
477          * the fields that are different between dos 5 and dos 3.3.
478          */
479         SecPerClust = b50->bpbSecPerClust;
480         pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
481         if (pmp->pm_BytesPerSec < DEV_BSIZE) {
482                 error = EINVAL;
483                 goto error_exit;
484         }
485         pmp->pm_ResSectors = getushort(b50->bpbResSectors);
486         pmp->pm_FATs = b50->bpbFATs;
487         pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
488         pmp->pm_Sectors = getushort(b50->bpbSectors);
489         pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
490         pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
491         pmp->pm_Heads = getushort(b50->bpbHeads);
492         pmp->pm_Media = b50->bpbMedia;
493
494         /* calculate the ratio of sector size to DEV_BSIZE */
495         pmp->pm_BlkPerSec = pmp->pm_BytesPerSec / DEV_BSIZE;
496
497         /* XXX - We should probably check more values here */
498         if (!pmp->pm_BytesPerSec || !SecPerClust
499                 || !pmp->pm_Heads
500 #ifdef PC98
501                 || !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 255) {
502 #else
503                 || !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 63) {
504 #endif
505                 error = EINVAL;
506                 goto error_exit;
507         }
508
509         if (pmp->pm_Sectors == 0) {
510                 pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
511                 pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
512         } else {
513                 pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
514                 pmp->pm_HugeSectors = pmp->pm_Sectors;
515         }
516         if (!(pmp->pm_flags & MSDOSFS_LARGEFS)) {
517                 if (pmp->pm_HugeSectors > 0xffffffff / 
518                     (pmp->pm_BytesPerSec / sizeof(struct direntry)) + 1) {
519                         /*
520                          * We cannot deal currently with this size of disk
521                          * due to fileid limitations (see msdosfs_getattr and
522                          * msdosfs_readdir)
523                          */
524                         error = EINVAL;
525                         vfs_mount_error(mp,
526                             "Disk too big, try '-o large' mount option");
527                         goto error_exit;
528                 }
529         }
530
531         if (pmp->pm_RootDirEnts == 0) {
532                 if (pmp->pm_Sectors
533                     || pmp->pm_FATsecs
534                     || getushort(b710->bpbFSVers)) {
535                         error = EINVAL;
536                         printf("mountmsdosfs(): bad FAT32 filesystem\n");
537                         goto error_exit;
538                 }
539                 pmp->pm_fatmask = FAT32_MASK;
540                 pmp->pm_fatmult = 4;
541                 pmp->pm_fatdiv = 1;
542                 pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
543                 if (getushort(b710->bpbExtFlags) & FATMIRROR)
544                         pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
545                 else
546                         pmp->pm_flags |= MSDOSFS_FATMIRROR;
547         } else
548                 pmp->pm_flags |= MSDOSFS_FATMIRROR;
549
550         /*
551          * Check a few values (could do some more):
552          * - logical sector size: power of 2, >= block size
553          * - sectors per cluster: power of 2, >= 1
554          * - number of sectors:   >= 1, <= size of partition
555          * - number of FAT sectors: >= 1
556          */
557         if ( (SecPerClust == 0)
558           || (SecPerClust & (SecPerClust - 1))
559           || (pmp->pm_BytesPerSec < DEV_BSIZE)
560           || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
561           || (pmp->pm_HugeSectors == 0)
562           || (pmp->pm_FATsecs == 0)
563         ) {
564                 error = EINVAL;
565                 goto error_exit;
566         }
567
568         pmp->pm_HugeSectors *= pmp->pm_BlkPerSec;
569         pmp->pm_HiddenSects *= pmp->pm_BlkPerSec; /* XXX not used? */
570         pmp->pm_FATsecs     *= pmp->pm_BlkPerSec;
571         SecPerClust         *= pmp->pm_BlkPerSec;
572
573         pmp->pm_fatblk = pmp->pm_ResSectors * pmp->pm_BlkPerSec;
574
575         if (FAT32(pmp)) {
576                 pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
577                 pmp->pm_firstcluster = pmp->pm_fatblk
578                         + (pmp->pm_FATs * pmp->pm_FATsecs);
579                 pmp->pm_fsinfo = getushort(b710->bpbFSInfo) * pmp->pm_BlkPerSec;
580         } else {
581                 pmp->pm_rootdirblk = pmp->pm_fatblk +
582                         (pmp->pm_FATs * pmp->pm_FATsecs);
583                 pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
584                                        + DEV_BSIZE - 1)
585                         / DEV_BSIZE; /* in blocks */
586                 pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
587         }
588
589         pmp->pm_maxcluster = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
590             SecPerClust + 1;
591         pmp->pm_fatsize = pmp->pm_FATsecs * DEV_BSIZE; /* XXX not used? */
592
593         if (pmp->pm_fatmask == 0) {
594                 if (pmp->pm_maxcluster
595                     <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
596                         /*
597                          * This will usually be a floppy disk. This size makes
598                          * sure that one fat entry will not be split across
599                          * multiple blocks.
600                          */
601                         pmp->pm_fatmask = FAT12_MASK;
602                         pmp->pm_fatmult = 3;
603                         pmp->pm_fatdiv = 2;
604                 } else {
605                         pmp->pm_fatmask = FAT16_MASK;
606                         pmp->pm_fatmult = 2;
607                         pmp->pm_fatdiv = 1;
608                 }
609         }
610
611         clusters = (pmp->pm_fatsize / pmp->pm_fatmult) * pmp->pm_fatdiv;
612         if (pmp->pm_maxcluster >= clusters) {
613                 printf("Warning: number of clusters (%ld) exceeds FAT "
614                     "capacity (%ld)\n", pmp->pm_maxcluster + 1, clusters);
615                 pmp->pm_maxcluster = clusters - 1;
616         }
617
618         if (FAT12(pmp))
619                 pmp->pm_fatblocksize = 3 * 512;
620         else
621                 pmp->pm_fatblocksize = PAGE_SIZE;
622         pmp->pm_fatblocksize = roundup(pmp->pm_fatblocksize,
623             pmp->pm_BytesPerSec);
624         pmp->pm_fatblocksec = pmp->pm_fatblocksize / DEV_BSIZE;
625         pmp->pm_bnshift = ffs(DEV_BSIZE) - 1;
626
627         /*
628          * Compute mask and shift value for isolating cluster relative byte
629          * offsets and cluster numbers from a file offset.
630          */
631         pmp->pm_bpcluster = SecPerClust * DEV_BSIZE;
632         pmp->pm_crbomask = pmp->pm_bpcluster - 1;
633         pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
634
635         /*
636          * Check for valid cluster size
637          * must be a power of 2
638          */
639         if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
640                 error = EINVAL;
641                 goto error_exit;
642         }
643
644         /*
645          * Release the bootsector buffer.
646          */
647         brelse(bp);
648         bp = NULL;
649
650         /*
651          * Check the fsinfo sector if we have one.  Silently fix up our
652          * in-core copy of fp->fsinxtfree if it is unknown (0xffffffff)
653          * or too large.  Ignore fp->fsinfree for now, since we need to
654          * read the entire FAT anyway to fill the inuse map.
655          */
656         if (pmp->pm_fsinfo) {
657                 struct fsinfo *fp;
658
659                 if ((error = bread(devvp, pmp->pm_fsinfo, pmp->pm_BytesPerSec,
660                     NOCRED, &bp)) != 0)
661                         goto error_exit;
662                 fp = (struct fsinfo *)bp->b_data;
663                 if (!bcmp(fp->fsisig1, "RRaA", 4)
664                     && !bcmp(fp->fsisig2, "rrAa", 4)
665                     && !bcmp(fp->fsisig3, "\0\0\125\252", 4)) {
666                         pmp->pm_nxtfree = getulong(fp->fsinxtfree);
667                         if (pmp->pm_nxtfree > pmp->pm_maxcluster)
668                                 pmp->pm_nxtfree = CLUST_FIRST;
669                 } else
670                         pmp->pm_fsinfo = 0;
671                 brelse(bp);
672                 bp = NULL;
673         }
674
675         /*
676          * Finish initializing pmp->pm_nxtfree (just in case the first few
677          * sectors aren't properly reserved in the FAT).  This completes
678          * the fixup for fp->fsinxtfree, and fixes up the zero-initialized
679          * value if there is no fsinfo.  We will use pmp->pm_nxtfree
680          * internally even if there is no fsinfo.
681          */
682         if (pmp->pm_nxtfree < CLUST_FIRST)
683                 pmp->pm_nxtfree = CLUST_FIRST;
684
685         /*
686          * Allocate memory for the bitmap of allocated clusters, and then
687          * fill it in.
688          */
689         pmp->pm_inusemap = malloc(howmany(pmp->pm_maxcluster + 1, N_INUSEBITS)
690                                   * sizeof(*pmp->pm_inusemap),
691                                   M_MSDOSFSFAT, M_WAITOK);
692
693         /*
694          * fillinusemap() needs pm_devvp.
695          */
696         pmp->pm_devvp = devvp;
697
698         /*
699          * Have the inuse map filled in.
700          */
701         if ((error = fillinusemap(pmp)) != 0)
702                 goto error_exit;
703
704         /*
705          * If they want fat updates to be synchronous then let them suffer
706          * the performance degradation in exchange for the on disk copy of
707          * the fat being correct just about all the time.  I suppose this
708          * would be a good thing to turn on if the kernel is still flakey.
709          */
710         if (mp->mnt_flag & MNT_SYNCHRONOUS)
711                 pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
712
713         /*
714          * Finish up.
715          */
716         if (ronly)
717                 pmp->pm_flags |= MSDOSFSMNT_RONLY;
718         else {
719                 /* Mark the volume dirty while it is mounted read/write. */
720                 if ((error = markvoldirty(pmp, 1)) != 0)
721                         goto error_exit;
722                 pmp->pm_fmod = 1;
723         }
724         mp->mnt_data = (qaddr_t) pmp;
725         mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
726         mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
727         MNT_ILOCK(mp);
728         mp->mnt_flag |= MNT_LOCAL;
729         MNT_IUNLOCK(mp);
730
731         if (pmp->pm_flags & MSDOSFS_LARGEFS)
732                 msdosfs_fileno_init(mp);
733
734         return 0;
735
736 error_exit:
737         if (bp)
738                 brelse(bp);
739         if (cp != NULL) {
740                 DROP_GIANT();
741                 g_topology_lock();
742                 g_vfs_close(cp, td);
743                 g_topology_unlock();
744                 PICKUP_GIANT();
745         }
746         if (pmp) {
747                 if (pmp->pm_inusemap)
748                         free(pmp->pm_inusemap, M_MSDOSFSFAT);
749                 free(pmp, M_MSDOSFSMNT);
750                 mp->mnt_data = (qaddr_t)0;
751         }
752         return (error);
753 }
754
755 /*
756  * Unmount the filesystem described by mp.
757  */
758 static int
759 msdosfs_unmount(struct mount *mp, int mntflags, struct thread *td)
760 {
761         struct msdosfsmount *pmp;
762         int error, flags;
763
764         flags = 0;
765         if (mntflags & MNT_FORCE)
766                 flags |= FORCECLOSE;
767         error = vflush(mp, 0, flags, td);
768         if (error)
769                 return error;
770         pmp = VFSTOMSDOSFS(mp);
771         if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
772                 if (pmp->pm_w2u)
773                         msdosfs_iconv->close(pmp->pm_w2u);
774                 if (pmp->pm_u2w)
775                         msdosfs_iconv->close(pmp->pm_u2w);
776                 if (pmp->pm_d2u)
777                         msdosfs_iconv->close(pmp->pm_d2u);
778                 if (pmp->pm_u2d)
779                         msdosfs_iconv->close(pmp->pm_u2d);
780         }
781
782         /* If the volume was mounted read/write, mark it clean now. */
783         if ((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0) {
784                 error = markvoldirty(pmp, 0);
785                 if (error && (flags & FORCECLOSE) == 0)
786                         return (error);
787         }
788 #ifdef MSDOSFS_DEBUG
789         {
790                 struct vnode *vp = pmp->pm_devvp;
791
792                 VI_LOCK(vp);
793                 vn_printf(vp,
794                     "msdosfs_umount(): just before calling VOP_CLOSE()\n");
795                 printf("freef %p, freeb %p, mount %p\n",
796                     TAILQ_NEXT(vp, v_freelist), vp->v_freelist.tqe_prev,
797                     vp->v_mount);
798                 printf("cleanblkhd %p, dirtyblkhd %p, numoutput %ld, type %d\n",
799                     TAILQ_FIRST(&vp->v_bufobj.bo_clean.bv_hd),
800                     TAILQ_FIRST(&vp->v_bufobj.bo_dirty.bv_hd),
801                     vp->v_bufobj.bo_numoutput, vp->v_type);
802                 VI_UNLOCK(vp);
803         }
804 #endif
805         DROP_GIANT();
806         g_topology_lock();
807         g_vfs_close(pmp->pm_cp, td);
808         g_topology_unlock();
809         PICKUP_GIANT();
810         vrele(pmp->pm_devvp);
811         free(pmp->pm_inusemap, M_MSDOSFSFAT);
812         if (pmp->pm_flags & MSDOSFS_LARGEFS) {
813                 msdosfs_fileno_free(mp);
814         }
815         free(pmp, M_MSDOSFSMNT);
816         mp->mnt_data = (qaddr_t)0;
817         MNT_ILOCK(mp);
818         mp->mnt_flag &= ~MNT_LOCAL;
819         MNT_IUNLOCK(mp);
820         return (error);
821 }
822
823 static int
824 msdosfs_root(struct mount *mp, int flags, struct vnode **vpp, struct thread *td)
825 {
826         struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
827         struct denode *ndep;
828         int error;
829
830 #ifdef MSDOSFS_DEBUG
831         printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
832 #endif
833         error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep);
834         if (error)
835                 return (error);
836         *vpp = DETOV(ndep);
837         return (0);
838 }
839
840 static int
841 msdosfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
842 {
843         struct msdosfsmount *pmp;
844
845         pmp = VFSTOMSDOSFS(mp);
846         sbp->f_bsize = pmp->pm_bpcluster;
847         sbp->f_iosize = pmp->pm_bpcluster;
848         sbp->f_blocks = pmp->pm_maxcluster + 1;
849         sbp->f_bfree = pmp->pm_freeclustercount;
850         sbp->f_bavail = pmp->pm_freeclustercount;
851         sbp->f_files = pmp->pm_RootDirEnts;                     /* XXX */
852         sbp->f_ffree = 0;       /* what to put in here? */
853         return (0);
854 }
855
856 static int
857 msdosfs_sync(struct mount *mp, int waitfor, struct thread *td)
858 {
859         struct vnode *vp, *nvp;
860         struct denode *dep;
861         struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
862         int error, allerror = 0;
863
864         /*
865          * If we ever switch to not updating all of the fats all the time,
866          * this would be the place to update them from the first one.
867          */
868         if (pmp->pm_fmod != 0) {
869                 if (pmp->pm_flags & MSDOSFSMNT_RONLY)
870                         panic("msdosfs_sync: rofs mod");
871                 else {
872                         /* update fats here */
873                 }
874         }
875         /*
876          * Write back each (modified) denode.
877          */
878         MNT_ILOCK(mp);
879 loop:
880         MNT_VNODE_FOREACH(vp, mp, nvp) {
881                 VI_LOCK(vp);
882                 if (vp->v_type == VNON || (vp->v_iflag & VI_DOOMED)) {
883                         VI_UNLOCK(vp);
884                         continue;
885                 }
886                 MNT_IUNLOCK(mp);
887                 dep = VTODE(vp);
888                 if ((dep->de_flag &
889                     (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 &&
890                     (vp->v_bufobj.bo_dirty.bv_cnt == 0 ||
891                     waitfor == MNT_LAZY)) {
892                         VI_UNLOCK(vp);
893                         MNT_ILOCK(mp);
894                         continue;
895                 }
896                 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, td);
897                 if (error) {
898                         MNT_ILOCK(mp);
899                         if (error == ENOENT)
900                                 goto loop;
901                         continue;
902                 }
903                 error = VOP_FSYNC(vp, waitfor, td);
904                 if (error)
905                         allerror = error;
906                 VOP_UNLOCK(vp, 0, td);
907                 vrele(vp);
908                 MNT_ILOCK(mp);
909         }
910         MNT_IUNLOCK(mp);
911
912         /*
913          * Flush filesystem control info.
914          */
915         if (waitfor != MNT_LAZY) {
916                 vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY, td);
917                 error = VOP_FSYNC(pmp->pm_devvp, waitfor, td);
918                 if (error)
919                         allerror = error;
920                 VOP_UNLOCK(pmp->pm_devvp, 0, td);
921         }
922         return (allerror);
923 }
924
925 static int
926 msdosfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
927 {
928         struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
929         struct defid *defhp = (struct defid *) fhp;
930         struct denode *dep;
931         int error;
932
933         error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep);
934         if (error) {
935                 *vpp = NULLVP;
936                 return (error);
937         }
938         *vpp = DETOV(dep);
939         vnode_create_vobject(*vpp, dep->de_FileSize, curthread);
940         return (0);
941 }
942
943 static struct vfsops msdosfs_vfsops = {
944         .vfs_fhtovp =           msdosfs_fhtovp,
945         .vfs_mount =            msdosfs_mount,
946         .vfs_cmount =           msdosfs_cmount,
947         .vfs_root =             msdosfs_root,
948         .vfs_statfs =           msdosfs_statfs,
949         .vfs_sync =             msdosfs_sync,
950         .vfs_unmount =          msdosfs_unmount,
951 };
952
953 VFS_SET(msdosfs_vfsops, msdosfs, 0);
954 MODULE_VERSION(msdosfs, 1);