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