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