msdosfs_vfsops.c revision 1.69 1 /* $NetBSD: msdosfs_vfsops.c,v 1.69 2008/12/16 16:18:25 pooka Exp $ */
2
3 /*-
4 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
5 * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
6 * All rights reserved.
7 * Original code by Paul Popelka (paulp (at) uts.amdahl.com) (see below).
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by TooLs GmbH.
20 * 4. The name of TooLs GmbH may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34 /*
35 * Written by Paul Popelka (paulp (at) uts.amdahl.com)
36 *
37 * You can do anything you want with this software, just don't say you wrote
38 * it, and don't remove this notice.
39 *
40 * This software is provided "as is".
41 *
42 * The author supplies this software to be publicly redistributed on the
43 * understanding that the author is not responsible for the correct
44 * functioning of this software in any circumstances and is not liable for
45 * any damages caused by this software.
46 *
47 * October 1992
48 */
49
50 #include <sys/cdefs.h>
51 __KERNEL_RCSID(0, "$NetBSD: msdosfs_vfsops.c,v 1.69 2008/12/16 16:18:25 pooka Exp $");
52
53 #if defined(_KERNEL_OPT)
54 #include "opt_compat_netbsd.h"
55 #endif
56
57 #include <sys/param.h>
58 #include <sys/systm.h>
59 #include <sys/sysctl.h>
60 #include <sys/namei.h>
61 #include <sys/proc.h>
62 #include <sys/kernel.h>
63 #include <sys/vnode.h>
64 #include <miscfs/genfs/genfs.h>
65 #include <miscfs/specfs/specdev.h> /* XXX */ /* defines v_rdev */
66 #include <sys/mount.h>
67 #include <sys/buf.h>
68 #include <sys/file.h>
69 #include <sys/device.h>
70 #include <sys/disklabel.h>
71 #include <sys/disk.h>
72 #include <sys/ioctl.h>
73 #include <sys/malloc.h>
74 #include <sys/dirent.h>
75 #include <sys/stat.h>
76 #include <sys/conf.h>
77 #include <sys/kauth.h>
78 #include <sys/module.h>
79
80 #include <fs/msdosfs/bpb.h>
81 #include <fs/msdosfs/bootsect.h>
82 #include <fs/msdosfs/direntry.h>
83 #include <fs/msdosfs/denode.h>
84 #include <fs/msdosfs/msdosfsmount.h>
85 #include <fs/msdosfs/fat.h>
86
87 MODULE(MODULE_CLASS_VFS, msdosfs, NULL);
88
89 #ifdef MSDOSFS_DEBUG
90 #define DPRINTF(a) uprintf a
91 #else
92 #define DPRINTF(a)
93 #endif
94
95 #define MSDOSFS_NAMEMAX(pmp) \
96 (pmp)->pm_flags & MSDOSFSMNT_LONGNAME ? WIN_MAXLEN : 12
97
98 VFS_PROTOS(msdosfs);
99
100 int msdosfs_mountfs(struct vnode *, struct mount *, struct lwp *,
101 struct msdosfs_args *);
102
103 static int update_mp(struct mount *, struct msdosfs_args *);
104
105 MALLOC_JUSTDEFINE(M_MSDOSFSMNT, "MSDOSFS mount", "MSDOS FS mount structure");
106 MALLOC_JUSTDEFINE(M_MSDOSFSFAT, "MSDOSFS fat", "MSDOS FS fat table");
107 MALLOC_JUSTDEFINE(M_MSDOSFSTMP, "MSDOSFS temp", "MSDOS FS temp. structures");
108
109 #define ROOTNAME "root_device"
110
111 static struct sysctllog *msdosfs_sysctl_log;
112
113 extern const struct vnodeopv_desc msdosfs_vnodeop_opv_desc;
114
115 const struct vnodeopv_desc * const msdosfs_vnodeopv_descs[] = {
116 &msdosfs_vnodeop_opv_desc,
117 NULL,
118 };
119
120 struct vfsops msdosfs_vfsops = {
121 MOUNT_MSDOS,
122 sizeof (struct msdosfs_args),
123 msdosfs_mount,
124 msdosfs_start,
125 msdosfs_unmount,
126 msdosfs_root,
127 (void *)eopnotsupp, /* vfs_quotactl */
128 msdosfs_statvfs,
129 msdosfs_sync,
130 msdosfs_vget,
131 msdosfs_fhtovp,
132 msdosfs_vptofh,
133 msdosfs_init,
134 msdosfs_reinit,
135 msdosfs_done,
136 msdosfs_mountroot,
137 (int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
138 vfs_stdextattrctl,
139 (void *)eopnotsupp, /* vfs_suspendctl */
140 genfs_renamelock_enter,
141 genfs_renamelock_exit,
142 (void *)eopnotsupp,
143 msdosfs_vnodeopv_descs,
144 0,
145 { NULL, NULL },
146 };
147
148 static int
149 msdosfs_modcmd(modcmd_t cmd, void *arg)
150 {
151 int error;
152
153 switch (cmd) {
154 case MODULE_CMD_INIT:
155 error = vfs_attach(&msdosfs_vfsops);
156 if (error != 0)
157 break;
158 sysctl_createv(&msdosfs_sysctl_log, 0, NULL, NULL,
159 CTLFLAG_PERMANENT,
160 CTLTYPE_NODE, "vfs", NULL,
161 NULL, 0, NULL, 0,
162 CTL_VFS, CTL_EOL);
163 sysctl_createv(&msdosfs_sysctl_log, 0, NULL, NULL,
164 CTLFLAG_PERMANENT,
165 CTLTYPE_NODE, "msdosfs",
166 SYSCTL_DESCR("MS-DOS file system"),
167 NULL, 0, NULL, 0,
168 CTL_VFS, 4, CTL_EOL);
169 /*
170 * XXX the "4" above could be dynamic, thereby eliminating one
171 * more instance of the "number to vfs" mapping problem, but
172 * "4" is the order as taken from sys/mount.h
173 */
174 break;
175 case MODULE_CMD_FINI:
176 error = vfs_detach(&msdosfs_vfsops);
177 if (error != 0)
178 break;
179 sysctl_teardown(&msdosfs_sysctl_log);
180 break;
181 default:
182 error = ENOTTY;
183 break;
184 }
185
186 return (error);
187 }
188
189 static int
190 update_mp(mp, argp)
191 struct mount *mp;
192 struct msdosfs_args *argp;
193 {
194 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
195 int error;
196
197 pmp->pm_gid = argp->gid;
198 pmp->pm_uid = argp->uid;
199 pmp->pm_mask = argp->mask & ALLPERMS;
200 pmp->pm_dirmask = argp->dirmask & ALLPERMS;
201 pmp->pm_gmtoff = argp->gmtoff;
202 pmp->pm_flags |= argp->flags & MSDOSFSMNT_MNTOPT;
203
204 /*
205 * GEMDOS knows nothing (yet) about win95
206 */
207 if (pmp->pm_flags & MSDOSFSMNT_GEMDOSFS)
208 pmp->pm_flags |= MSDOSFSMNT_NOWIN95;
209
210 if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
211 pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
212 else if (!(pmp->pm_flags &
213 (MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) {
214 struct vnode *rtvp;
215
216 /*
217 * Try to divine whether to support Win'95 long filenames
218 */
219 if (FAT32(pmp))
220 pmp->pm_flags |= MSDOSFSMNT_LONGNAME;
221 else {
222 if ((error = msdosfs_root(mp, &rtvp)) != 0)
223 return error;
224 pmp->pm_flags |= findwin95(VTODE(rtvp))
225 ? MSDOSFSMNT_LONGNAME
226 : MSDOSFSMNT_SHORTNAME;
227 vput(rtvp);
228 }
229 }
230
231 mp->mnt_stat.f_namemax = MSDOSFS_NAMEMAX(pmp);
232
233 return 0;
234 }
235
236 int
237 msdosfs_mountroot()
238 {
239 struct mount *mp;
240 struct lwp *l = curlwp; /* XXX */
241 int error;
242 struct msdosfs_args args;
243
244 if (device_class(root_device) != DV_DISK)
245 return (ENODEV);
246
247 if ((error = vfs_rootmountalloc(MOUNT_MSDOS, "root_device", &mp))) {
248 vrele(rootvp);
249 return (error);
250 }
251
252 args.flags = MSDOSFSMNT_VERSIONED;
253 args.uid = 0;
254 args.gid = 0;
255 args.mask = 0777;
256 args.version = MSDOSFSMNT_VERSION;
257 args.dirmask = 0777;
258
259 if ((error = msdosfs_mountfs(rootvp, mp, l, &args)) != 0) {
260 vfs_unbusy(mp, false, NULL);
261 vfs_destroy(mp);
262 return (error);
263 }
264
265 if ((error = update_mp(mp, &args)) != 0) {
266 (void)msdosfs_unmount(mp, 0);
267 vfs_unbusy(mp, false, NULL);
268 vfs_destroy(mp);
269 vrele(rootvp);
270 return (error);
271 }
272
273 mutex_enter(&mountlist_lock);
274 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
275 mutex_exit(&mountlist_lock);
276 (void)msdosfs_statvfs(mp, &mp->mnt_stat);
277 vfs_unbusy(mp, false, NULL);
278 return (0);
279 }
280
281 /*
282 * mp - path - addr in user space of mount point (ie /usr or whatever)
283 * data - addr in user space of mount params including the name of the block
284 * special file to treat as a filesystem.
285 */
286 int
287 msdosfs_mount(mp, path, data, data_len)
288 struct mount *mp;
289 const char *path;
290 void *data;
291 size_t *data_len;
292 {
293 struct lwp *l = curlwp;
294 struct nameidata nd;
295 struct vnode *devvp; /* vnode for blk device to mount */
296 struct msdosfs_args *args = data; /* holds data from mount request */
297 /* msdosfs specific mount control block */
298 struct msdosfsmount *pmp = NULL;
299 int error, flags;
300 mode_t accessmode;
301
302 if (*data_len < sizeof *args)
303 return EINVAL;
304
305 if (mp->mnt_flag & MNT_GETARGS) {
306 pmp = VFSTOMSDOSFS(mp);
307 if (pmp == NULL)
308 return EIO;
309 args->fspec = NULL;
310 args->uid = pmp->pm_uid;
311 args->gid = pmp->pm_gid;
312 args->mask = pmp->pm_mask;
313 args->flags = pmp->pm_flags;
314 args->version = MSDOSFSMNT_VERSION;
315 args->dirmask = pmp->pm_dirmask;
316 args->gmtoff = pmp->pm_gmtoff;
317 *data_len = sizeof *args;
318 return 0;
319 }
320
321 /*
322 * If not versioned (i.e. using old mount_msdos(8)), fill in
323 * the additional structure items with suitable defaults.
324 */
325 if ((args->flags & MSDOSFSMNT_VERSIONED) == 0) {
326 args->version = 1;
327 args->dirmask = args->mask;
328 }
329
330 /*
331 * Reset GMT offset for pre-v3 mount structure args.
332 */
333 if (args->version < 3)
334 args->gmtoff = 0;
335
336 /*
337 * If updating, check whether changing from read-only to
338 * read/write; if there is no device name, that's all we do.
339 */
340 if (mp->mnt_flag & MNT_UPDATE) {
341 pmp = VFSTOMSDOSFS(mp);
342 error = 0;
343 if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_flag & MNT_RDONLY)) {
344 flags = WRITECLOSE;
345 if (mp->mnt_flag & MNT_FORCE)
346 flags |= FORCECLOSE;
347 error = vflush(mp, NULLVP, flags);
348 }
349 if (!error && (mp->mnt_flag & MNT_RELOAD))
350 /* not yet implemented */
351 error = EOPNOTSUPP;
352 if (error) {
353 DPRINTF(("vflush %d\n", error));
354 return (error);
355 }
356 if ((pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_iflag & IMNT_WANTRDWR)) {
357 /*
358 * If upgrade to read-write by non-root, then verify
359 * that user has necessary permissions on the device.
360 */
361 if (kauth_authorize_generic(l->l_cred,
362 KAUTH_GENERIC_ISSUSER, NULL) != 0) {
363 devvp = pmp->pm_devvp;
364 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
365 error = VOP_ACCESS(devvp, VREAD | VWRITE,
366 l->l_cred);
367 VOP_UNLOCK(devvp, 0);
368 DPRINTF(("VOP_ACCESS %d\n", error));
369 if (error)
370 return (error);
371 }
372 pmp->pm_flags &= ~MSDOSFSMNT_RONLY;
373 }
374 if (args->fspec == NULL) {
375 DPRINTF(("missing fspec\n"));
376 return EINVAL;
377 }
378 }
379 /*
380 * Not an update, or updating the name: look up the name
381 * and verify that it refers to a sensible block device.
382 */
383 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, args->fspec);
384 if ((error = namei(&nd)) != 0) {
385 DPRINTF(("namei %d\n", error));
386 return (error);
387 }
388 devvp = nd.ni_vp;
389
390 if (devvp->v_type != VBLK) {
391 DPRINTF(("not block\n"));
392 vrele(devvp);
393 return (ENOTBLK);
394 }
395 if (bdevsw_lookup(devvp->v_rdev) == NULL) {
396 DPRINTF(("no block switch\n"));
397 vrele(devvp);
398 return (ENXIO);
399 }
400 /*
401 * If mount by non-root, then verify that user has necessary
402 * permissions on the device.
403 */
404 if (kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER, NULL) != 0) {
405 accessmode = VREAD;
406 if ((mp->mnt_flag & MNT_RDONLY) == 0)
407 accessmode |= VWRITE;
408 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
409 error = VOP_ACCESS(devvp, accessmode, l->l_cred);
410 VOP_UNLOCK(devvp, 0);
411 if (error) {
412 DPRINTF(("VOP_ACCESS2 %d\n", error));
413 vrele(devvp);
414 return (error);
415 }
416 }
417 if ((mp->mnt_flag & MNT_UPDATE) == 0) {
418 int xflags;
419
420 if (mp->mnt_flag & MNT_RDONLY)
421 xflags = FREAD;
422 else
423 xflags = FREAD|FWRITE;
424 error = VOP_OPEN(devvp, xflags, FSCRED);
425 if (error) {
426 DPRINTF(("VOP_OPEN %d\n", error));
427 goto fail;
428 }
429 error = msdosfs_mountfs(devvp, mp, l, args);
430 if (error) {
431 DPRINTF(("msdosfs_mountfs %d\n", error));
432 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
433 (void) VOP_CLOSE(devvp, xflags, NOCRED);
434 VOP_UNLOCK(devvp, 0);
435 goto fail;
436 }
437 #ifdef MSDOSFS_DEBUG /* only needed for the printf below */
438 pmp = VFSTOMSDOSFS(mp);
439 #endif
440 } else {
441 vrele(devvp);
442 if (devvp != pmp->pm_devvp) {
443 DPRINTF(("devvp %p pmp %p\n",
444 devvp, pmp->pm_devvp));
445 return (EINVAL); /* needs translation */
446 }
447 }
448 if ((error = update_mp(mp, args)) != 0) {
449 msdosfs_unmount(mp, MNT_FORCE);
450 DPRINTF(("update_mp %d\n", error));
451 return error;
452 }
453
454 #ifdef MSDOSFS_DEBUG
455 printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap);
456 #endif
457 return set_statvfs_info(path, UIO_USERSPACE, args->fspec, UIO_USERSPACE,
458 mp->mnt_op->vfs_name, mp, l);
459
460 fail:
461 vrele(devvp);
462 return (error);
463 }
464
465 int
466 msdosfs_mountfs(devvp, mp, l, argp)
467 struct vnode *devvp;
468 struct mount *mp;
469 struct lwp *l;
470 struct msdosfs_args *argp;
471 {
472 struct msdosfsmount *pmp;
473 struct buf *bp;
474 dev_t dev = devvp->v_rdev;
475 struct partinfo dpart;
476 union bootsector *bsp;
477 struct byte_bpb33 *b33;
478 struct byte_bpb50 *b50;
479 struct byte_bpb710 *b710;
480 u_int8_t SecPerClust;
481 int ronly, error, tmp;
482 int bsize, dtype, fstype, secsize;
483 u_int64_t psize;
484
485 /* Flush out any old buffers remaining from a previous use. */
486 if ((error = vinvalbuf(devvp, V_SAVE, l->l_cred, l, 0, 0)) != 0)
487 return (error);
488
489 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
490
491 bp = NULL; /* both used in error_exit */
492 pmp = NULL;
493
494 /*
495 * We need the disklabel to calculate the size of a FAT entry
496 * later on. Also make sure the partition contains a filesystem
497 * of type FS_MSDOS. This doesn't work for floppies, so we have
498 * to check for them too.
499 *
500 * There might still be parts of the msdos fs driver which assume
501 * that the size of a disk block will always be 512 bytes.
502 * Let's root them out...
503 */
504 error = VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, NOCRED);
505 if (error == 0) {
506 secsize = dpart.disklab->d_secsize;
507 dtype = dpart.disklab->d_type;
508 fstype = dpart.part->p_fstype;
509 psize = dpart.part->p_size;
510 } else {
511 struct dkwedge_info dkw;
512 error = VOP_IOCTL(devvp, DIOCGWEDGEINFO, &dkw, FREAD, NOCRED);
513 secsize = 512; /* XXX */
514 dtype = DTYPE_FLOPPY; /* XXX */
515 fstype = FS_MSDOS;
516 psize = -1;
517 if (error) {
518 if (error != ENOTTY) {
519 DPRINTF(("Error getting partition info %d\n",
520 error));
521 goto error_exit;
522 }
523 } else {
524 fstype = strcmp(dkw.dkw_ptype, DKW_PTYPE_FAT) == 0 ?
525 FS_MSDOS : -1;
526 psize = dkw.dkw_size;
527 }
528 }
529 if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
530 bsize = secsize;
531 if (bsize != 512 ||
532 (dtype != DTYPE_FLOPPY && fstype != FS_MSDOS)) {
533 DPRINTF(("bsize %d dtype %d fstype %d\n", bsize, dtype,
534 fstype));
535 error = EINVAL;
536 goto error_exit;
537 }
538 } else
539 bsize = 0;
540
541 /*
542 * Read the boot sector of the filesystem, and then check the
543 * boot signature. If not a dos boot sector then error out.
544 */
545 if ((error = bread(devvp, 0, secsize, NOCRED, 0, &bp)) != 0)
546 goto error_exit;
547 bsp = (union bootsector *)bp->b_data;
548 b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
549 b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
550 b710 = (struct byte_bpb710 *)bsp->bs710.bsBPB;
551
552 if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
553 if (bsp->bs50.bsBootSectSig0 != BOOTSIG0
554 || bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
555 DPRINTF(("bootsig0 %d bootsig1 %d\n",
556 bsp->bs50.bsBootSectSig0,
557 bsp->bs50.bsBootSectSig1));
558 error = EINVAL;
559 goto error_exit;
560 }
561 }
562
563 pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK);
564 memset(pmp, 0, sizeof *pmp);
565 pmp->pm_mountp = mp;
566
567 /*
568 * Compute several useful quantities from the bpb in the
569 * bootsector. Copy in the dos 5 variant of the bpb then fix up
570 * the fields that are different between dos 5 and dos 3.3.
571 */
572 SecPerClust = b50->bpbSecPerClust;
573 pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
574 pmp->pm_ResSectors = getushort(b50->bpbResSectors);
575 pmp->pm_FATs = b50->bpbFATs;
576 pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
577 pmp->pm_Sectors = getushort(b50->bpbSectors);
578 pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
579 pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
580 pmp->pm_Heads = getushort(b50->bpbHeads);
581 pmp->pm_Media = b50->bpbMedia;
582
583 if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
584 /* XXX - We should probably check more values here */
585 if (!pmp->pm_BytesPerSec || !SecPerClust
586 || pmp->pm_Heads > 255 || pmp->pm_SecPerTrack > 63) {
587 DPRINTF(("bytespersec %d secperclust %d "
588 "heads %d secpertrack %d\n",
589 pmp->pm_BytesPerSec, SecPerClust,
590 pmp->pm_Heads, pmp->pm_SecPerTrack));
591 error = EINVAL;
592 goto error_exit;
593 }
594 }
595
596 if (pmp->pm_Sectors == 0) {
597 pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
598 pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
599 } else {
600 pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
601 pmp->pm_HugeSectors = pmp->pm_Sectors;
602 }
603
604 if (pmp->pm_RootDirEnts == 0) {
605 unsigned short vers = getushort(b710->bpbFSVers);
606 /*
607 * Some say that bsBootSectSig[23] must be zero, but
608 * Windows does not require this and some digital cameras
609 * do not set these to zero. Therefore, do not insist.
610 */
611 if (pmp->pm_Sectors || pmp->pm_FATsecs || vers) {
612 DPRINTF(("sectors %d fatsecs %lu vers %d\n",
613 pmp->pm_Sectors, pmp->pm_FATsecs, vers));
614 error = EINVAL;
615 goto error_exit;
616 }
617 pmp->pm_fatmask = FAT32_MASK;
618 pmp->pm_fatmult = 4;
619 pmp->pm_fatdiv = 1;
620 pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
621
622 /* mirrorring is enabled if the FATMIRROR bit is not set */
623 if ((getushort(b710->bpbExtFlags) & FATMIRROR) == 0)
624 pmp->pm_flags |= MSDOSFS_FATMIRROR;
625 else
626 pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
627 } else
628 pmp->pm_flags |= MSDOSFS_FATMIRROR;
629
630 if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
631 if (FAT32(pmp)) {
632 DPRINTF(("fat32 for gemdos\n"));
633 /*
634 * GEMDOS doesn't know fat32.
635 */
636 error = EINVAL;
637 goto error_exit;
638 }
639
640 /*
641 * Check a few values (could do some more):
642 * - logical sector size: power of 2, >= block size
643 * - sectors per cluster: power of 2, >= 1
644 * - number of sectors: >= 1, <= size of partition
645 */
646 if ( (SecPerClust == 0)
647 || (SecPerClust & (SecPerClust - 1))
648 || (pmp->pm_BytesPerSec < bsize)
649 || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
650 || (pmp->pm_HugeSectors == 0)
651 || (pmp->pm_HugeSectors * (pmp->pm_BytesPerSec / bsize)
652 > psize)) {
653 DPRINTF(("consistency checks for gemdos\n"));
654 error = EINVAL;
655 goto error_exit;
656 }
657 /*
658 * XXX - Many parts of the msdos fs driver seem to assume that
659 * the number of bytes per logical sector (BytesPerSec) will
660 * always be the same as the number of bytes per disk block
661 * Let's pretend it is.
662 */
663 tmp = pmp->pm_BytesPerSec / bsize;
664 pmp->pm_BytesPerSec = bsize;
665 pmp->pm_HugeSectors *= tmp;
666 pmp->pm_HiddenSects *= tmp;
667 pmp->pm_ResSectors *= tmp;
668 pmp->pm_Sectors *= tmp;
669 pmp->pm_FATsecs *= tmp;
670 SecPerClust *= tmp;
671 }
672
673 /* Check that fs has nonzero FAT size */
674 if (pmp->pm_FATsecs == 0) {
675 DPRINTF(("FATsecs is 0\n"));
676 error = EINVAL;
677 goto error_exit;
678 }
679
680 pmp->pm_fatblk = pmp->pm_ResSectors;
681 if (FAT32(pmp)) {
682 pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
683 pmp->pm_firstcluster = pmp->pm_fatblk
684 + (pmp->pm_FATs * pmp->pm_FATsecs);
685 pmp->pm_fsinfo = getushort(b710->bpbFSInfo);
686 } else {
687 pmp->pm_rootdirblk = pmp->pm_fatblk +
688 (pmp->pm_FATs * pmp->pm_FATsecs);
689 pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
690 + pmp->pm_BytesPerSec - 1)
691 / pmp->pm_BytesPerSec;/* in sectors */
692 pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
693 }
694
695 pmp->pm_nmbrofclusters = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
696 SecPerClust;
697 pmp->pm_maxcluster = pmp->pm_nmbrofclusters + 1;
698 pmp->pm_fatsize = pmp->pm_FATsecs * pmp->pm_BytesPerSec;
699
700 if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
701 if (pmp->pm_nmbrofclusters <= (0xff0 - 2)
702 && (dtype == DTYPE_FLOPPY
703 || (dtype == DTYPE_VND
704 && (pmp->pm_Heads == 1 || pmp->pm_Heads == 2)))
705 ) {
706 pmp->pm_fatmask = FAT12_MASK;
707 pmp->pm_fatmult = 3;
708 pmp->pm_fatdiv = 2;
709 } else {
710 pmp->pm_fatmask = FAT16_MASK;
711 pmp->pm_fatmult = 2;
712 pmp->pm_fatdiv = 1;
713 }
714 } else if (pmp->pm_fatmask == 0) {
715 if (pmp->pm_maxcluster
716 <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
717 /*
718 * This will usually be a floppy disk. This size makes
719 * sure that one fat entry will not be split across
720 * multiple blocks.
721 */
722 pmp->pm_fatmask = FAT12_MASK;
723 pmp->pm_fatmult = 3;
724 pmp->pm_fatdiv = 2;
725 } else {
726 pmp->pm_fatmask = FAT16_MASK;
727 pmp->pm_fatmult = 2;
728 pmp->pm_fatdiv = 1;
729 }
730 }
731 if (FAT12(pmp))
732 pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec;
733 else
734 pmp->pm_fatblocksize = MAXBSIZE;
735
736 pmp->pm_fatblocksec = pmp->pm_fatblocksize / pmp->pm_BytesPerSec;
737 pmp->pm_bnshift = ffs(pmp->pm_BytesPerSec) - 1;
738
739 /*
740 * Compute mask and shift value for isolating cluster relative byte
741 * offsets and cluster numbers from a file offset.
742 */
743 pmp->pm_bpcluster = SecPerClust * pmp->pm_BytesPerSec;
744 pmp->pm_crbomask = pmp->pm_bpcluster - 1;
745 pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
746
747 /*
748 * Check for valid cluster size
749 * must be a power of 2
750 */
751 if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
752 DPRINTF(("bpcluster %lu cnshift %lu\n",
753 pmp->pm_bpcluster, pmp->pm_cnshift));
754 error = EINVAL;
755 goto error_exit;
756 }
757
758 /*
759 * Release the bootsector buffer.
760 */
761 brelse(bp, BC_AGE);
762 bp = NULL;
763
764 /*
765 * Check FSInfo.
766 */
767 if (pmp->pm_fsinfo) {
768 struct fsinfo *fp;
769
770 /*
771 * XXX If the fsinfo block is stored on media with
772 * 2KB or larger sectors, is the fsinfo structure
773 * padded at the end or in the middle?
774 */
775 if ((error = bread(devvp, de_bn2kb(pmp, pmp->pm_fsinfo),
776 pmp->pm_BytesPerSec, NOCRED, 0, &bp)) != 0)
777 goto error_exit;
778 fp = (struct fsinfo *)bp->b_data;
779 if (!memcmp(fp->fsisig1, "RRaA", 4)
780 && !memcmp(fp->fsisig2, "rrAa", 4)
781 && !memcmp(fp->fsisig3, "\0\0\125\252", 4)
782 && !memcmp(fp->fsisig4, "\0\0\125\252", 4))
783 pmp->pm_nxtfree = getulong(fp->fsinxtfree);
784 else
785 pmp->pm_fsinfo = 0;
786 brelse(bp, 0);
787 bp = NULL;
788 }
789
790 /*
791 * Check and validate (or perhaps invalidate?) the fsinfo structure?
792 * XXX
793 */
794 if (pmp->pm_fsinfo) {
795 if (pmp->pm_nxtfree == (u_long)-1)
796 pmp->pm_fsinfo = 0;
797 }
798
799 /*
800 * Allocate memory for the bitmap of allocated clusters, and then
801 * fill it in.
802 */
803 pmp->pm_inusemap = malloc(((pmp->pm_maxcluster + N_INUSEBITS - 1)
804 / N_INUSEBITS)
805 * sizeof(*pmp->pm_inusemap),
806 M_MSDOSFSFAT, M_WAITOK);
807
808 /*
809 * fillinusemap() needs pm_devvp.
810 */
811 pmp->pm_dev = dev;
812 pmp->pm_devvp = devvp;
813
814 /*
815 * Have the inuse map filled in.
816 */
817 if ((error = fillinusemap(pmp)) != 0) {
818 DPRINTF(("fillinusemap %d\n", error));
819 goto error_exit;
820 }
821
822 /*
823 * If they want fat updates to be synchronous then let them suffer
824 * the performance degradation in exchange for the on disk copy of
825 * the fat being correct just about all the time. I suppose this
826 * would be a good thing to turn on if the kernel is still flakey.
827 */
828 if (mp->mnt_flag & MNT_SYNCHRONOUS)
829 pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
830
831 /*
832 * Finish up.
833 */
834 if (ronly)
835 pmp->pm_flags |= MSDOSFSMNT_RONLY;
836 else
837 pmp->pm_fmod = 1;
838 mp->mnt_data = pmp;
839 mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
840 mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_MSDOS);
841 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
842 mp->mnt_stat.f_namemax = MSDOSFS_NAMEMAX(pmp);
843 mp->mnt_flag |= MNT_LOCAL;
844 mp->mnt_dev_bshift = pmp->pm_bnshift;
845 mp->mnt_fs_bshift = pmp->pm_cnshift;
846
847 /*
848 * If we ever do quotas for DOS filesystems this would be a place
849 * to fill in the info in the msdosfsmount structure. You dolt,
850 * quotas on dos filesystems make no sense because files have no
851 * owners on dos filesystems. of course there is some empty space
852 * in the directory entry where we could put uid's and gid's.
853 */
854
855 devvp->v_specmountpoint = mp;
856
857 return (0);
858
859 error_exit:;
860 if (bp)
861 brelse(bp, BC_AGE);
862 if (pmp) {
863 if (pmp->pm_inusemap)
864 free(pmp->pm_inusemap, M_MSDOSFSFAT);
865 free(pmp, M_MSDOSFSMNT);
866 mp->mnt_data = NULL;
867 }
868 return (error);
869 }
870
871 int
872 msdosfs_start(struct mount *mp, int flags)
873 {
874
875 return (0);
876 }
877
878 /*
879 * Unmount the filesystem described by mp.
880 */
881 int
882 msdosfs_unmount(mp, mntflags)
883 struct mount *mp;
884 int mntflags;
885 {
886 struct msdosfsmount *pmp;
887 int error, flags;
888
889 flags = 0;
890 if (mntflags & MNT_FORCE)
891 flags |= FORCECLOSE;
892 if ((error = vflush(mp, NULLVP, flags)) != 0)
893 return (error);
894 pmp = VFSTOMSDOSFS(mp);
895 if (pmp->pm_devvp->v_type != VBAD)
896 pmp->pm_devvp->v_specmountpoint = NULL;
897 #ifdef MSDOSFS_DEBUG
898 {
899 struct vnode *vp = pmp->pm_devvp;
900
901 printf("msdosfs_umount(): just before calling VOP_CLOSE()\n");
902 printf("flag %08x, usecount %d, writecount %d, holdcnt %d\n",
903 vp->v_vflag | vp->v_iflag | vp->v_uflag, vp->v_usecount,
904 vp->v_writecount, vp->v_holdcnt);
905 printf("mount %p, op %p\n",
906 vp->v_mount, vp->v_op);
907 printf("freef %p, freeb %p, mount %p\n",
908 vp->v_freelist.tqe_next, vp->v_freelist.tqe_prev,
909 vp->v_mount);
910 printf("cleanblkhd %p, dirtyblkhd %p, numoutput %d, type %d\n",
911 vp->v_cleanblkhd.lh_first,
912 vp->v_dirtyblkhd.lh_first,
913 vp->v_numoutput, vp->v_type);
914 printf("union %p, tag %d, data[0] %08x, data[1] %08x\n",
915 vp->v_socket, vp->v_tag,
916 ((u_int *)vp->v_data)[0],
917 ((u_int *)vp->v_data)[1]);
918 }
919 #endif
920 vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY);
921 error = VOP_CLOSE(pmp->pm_devvp,
922 pmp->pm_flags & MSDOSFSMNT_RONLY ? FREAD : FREAD|FWRITE, NOCRED);
923 vput(pmp->pm_devvp);
924 free(pmp->pm_inusemap, M_MSDOSFSFAT);
925 free(pmp, M_MSDOSFSMNT);
926 mp->mnt_data = NULL;
927 mp->mnt_flag &= ~MNT_LOCAL;
928 return (error);
929 }
930
931 int
932 msdosfs_root(mp, vpp)
933 struct mount *mp;
934 struct vnode **vpp;
935 {
936 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
937 struct denode *ndep;
938 int error;
939
940 #ifdef MSDOSFS_DEBUG
941 printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
942 #endif
943 if ((error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep)) != 0)
944 return (error);
945 *vpp = DETOV(ndep);
946 return (0);
947 }
948
949 int
950 msdosfs_statvfs(struct mount *mp, struct statvfs *sbp)
951 {
952 struct msdosfsmount *pmp;
953
954 pmp = VFSTOMSDOSFS(mp);
955 sbp->f_bsize = pmp->pm_bpcluster;
956 sbp->f_frsize = sbp->f_bsize;
957 sbp->f_iosize = pmp->pm_bpcluster;
958 sbp->f_blocks = pmp->pm_nmbrofclusters;
959 sbp->f_bfree = pmp->pm_freeclustercount;
960 sbp->f_bavail = pmp->pm_freeclustercount;
961 sbp->f_bresvd = 0;
962 sbp->f_files = pmp->pm_RootDirEnts; /* XXX */
963 sbp->f_ffree = 0; /* what to put in here? */
964 sbp->f_favail = 0; /* what to put in here? */
965 sbp->f_fresvd = 0;
966 copy_statvfs_info(sbp, mp);
967 return (0);
968 }
969
970 int
971 msdosfs_sync(mp, waitfor, cred)
972 struct mount *mp;
973 int waitfor;
974 kauth_cred_t cred;
975 {
976 struct vnode *vp, *mvp;
977 struct denode *dep;
978 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
979 int error, allerror = 0;
980
981 /*
982 * If we ever switch to not updating all of the fats all the time,
983 * this would be the place to update them from the first one.
984 */
985 if (pmp->pm_fmod != 0) {
986 if (pmp->pm_flags & MSDOSFSMNT_RONLY)
987 panic("msdosfs_sync: rofs mod");
988 else {
989 /* update fats here */
990 }
991 }
992 /* Allocate a marker vnode. */
993 if ((mvp = vnalloc(mp)) == NULL)
994 return ENOMEM;
995 /*
996 * Write back each (modified) denode.
997 */
998 mutex_enter(&mntvnode_lock);
999 loop:
1000 for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = vunmark(mvp)) {
1001 vmark(mvp, vp);
1002 if (vp->v_mount != mp || vismarker(vp))
1003 continue;
1004 mutex_enter(&vp->v_interlock);
1005 dep = VTODE(vp);
1006 if (waitfor == MNT_LAZY || vp->v_type == VNON ||
1007 (((dep->de_flag &
1008 (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0) &&
1009 (LIST_EMPTY(&vp->v_dirtyblkhd) &&
1010 UVM_OBJ_IS_CLEAN(&vp->v_uobj)))) {
1011 mutex_exit(&vp->v_interlock);
1012 continue;
1013 }
1014 mutex_exit(&mntvnode_lock);
1015 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
1016 if (error) {
1017 mutex_enter(&mntvnode_lock);
1018 if (error == ENOENT) {
1019 (void)vunmark(mvp);
1020 goto loop;
1021 }
1022 continue;
1023 }
1024 if ((error = VOP_FSYNC(vp, cred,
1025 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0)) != 0)
1026 allerror = error;
1027 vput(vp);
1028 mutex_enter(&mntvnode_lock);
1029 }
1030 mutex_exit(&mntvnode_lock);
1031 vnfree(mvp);
1032
1033 /*
1034 * Force stale file system control information to be flushed.
1035 */
1036 if ((error = VOP_FSYNC(pmp->pm_devvp, cred,
1037 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0)) != 0)
1038 allerror = error;
1039 return (allerror);
1040 }
1041
1042 int
1043 msdosfs_fhtovp(mp, fhp, vpp)
1044 struct mount *mp;
1045 struct fid *fhp;
1046 struct vnode **vpp;
1047 {
1048 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
1049 struct defid defh;
1050 struct denode *dep;
1051 int error;
1052
1053 if (fhp->fid_len != sizeof(struct defid)) {
1054 DPRINTF(("fid_len %d %zd\n", fhp->fid_len,
1055 sizeof(struct defid)));
1056 return EINVAL;
1057 }
1058
1059 memcpy(&defh, fhp, sizeof(defh));
1060 error = deget(pmp, defh.defid_dirclust, defh.defid_dirofs, &dep);
1061 if (error) {
1062 DPRINTF(("deget %d\n", error));
1063 *vpp = NULLVP;
1064 return (error);
1065 }
1066 *vpp = DETOV(dep);
1067 return (0);
1068 }
1069
1070 int
1071 msdosfs_vptofh(vp, fhp, fh_size)
1072 struct vnode *vp;
1073 struct fid *fhp;
1074 size_t *fh_size;
1075 {
1076 struct denode *dep;
1077 struct defid defh;
1078
1079 if (*fh_size < sizeof(struct defid)) {
1080 *fh_size = sizeof(struct defid);
1081 return E2BIG;
1082 }
1083 *fh_size = sizeof(struct defid);
1084 dep = VTODE(vp);
1085 memset(&defh, 0, sizeof(defh));
1086 defh.defid_len = sizeof(struct defid);
1087 defh.defid_dirclust = dep->de_dirclust;
1088 defh.defid_dirofs = dep->de_diroffset;
1089 /* defh.defid_gen = dep->de_gen; */
1090 memcpy(fhp, &defh, sizeof(defh));
1091 return (0);
1092 }
1093
1094 int
1095 msdosfs_vget(struct mount *mp, ino_t ino,
1096 struct vnode **vpp)
1097 {
1098
1099 return (EOPNOTSUPP);
1100 }
1101