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