msdosfs_vfsops.c revision 1.75 1 /* $NetBSD: msdosfs_vfsops.c,v 1.75 2009/04/25 21:26:20 elad 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.75 2009/04/25 21:26:20 elad 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(struct mount *mp, struct msdosfs_args *argp)
191 {
192 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
193 int error;
194
195 pmp->pm_gid = argp->gid;
196 pmp->pm_uid = argp->uid;
197 pmp->pm_mask = argp->mask & ALLPERMS;
198 pmp->pm_dirmask = argp->dirmask & ALLPERMS;
199 pmp->pm_gmtoff = argp->gmtoff;
200 pmp->pm_flags |= argp->flags & MSDOSFSMNT_MNTOPT;
201
202 /*
203 * GEMDOS knows nothing about win95 long filenames
204 */
205 if (pmp->pm_flags & MSDOSFSMNT_GEMDOSFS)
206 pmp->pm_flags |= MSDOSFSMNT_NOWIN95;
207
208 if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
209 pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
210 else if (!(pmp->pm_flags &
211 (MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) {
212 struct vnode *rtvp;
213
214 /*
215 * Try to divine whether to support Win'95 long filenames
216 */
217 if (FAT32(pmp))
218 pmp->pm_flags |= MSDOSFSMNT_LONGNAME;
219 else {
220 if ((error = msdosfs_root(mp, &rtvp)) != 0)
221 return error;
222 pmp->pm_flags |= findwin95(VTODE(rtvp))
223 ? MSDOSFSMNT_LONGNAME
224 : MSDOSFSMNT_SHORTNAME;
225 vput(rtvp);
226 }
227 }
228
229 mp->mnt_stat.f_namemax = MSDOSFS_NAMEMAX(pmp);
230
231 return 0;
232 }
233
234 int
235 msdosfs_mountroot(void)
236 {
237 struct mount *mp;
238 struct lwp *l = curlwp; /* XXX */
239 int error;
240 struct msdosfs_args args;
241
242 if (device_class(root_device) != DV_DISK)
243 return (ENODEV);
244
245 if ((error = vfs_rootmountalloc(MOUNT_MSDOS, "root_device", &mp))) {
246 vrele(rootvp);
247 return (error);
248 }
249
250 args.flags = MSDOSFSMNT_VERSIONED;
251 args.uid = 0;
252 args.gid = 0;
253 args.mask = 0777;
254 args.version = MSDOSFSMNT_VERSION;
255 args.dirmask = 0777;
256
257 if ((error = msdosfs_mountfs(rootvp, mp, l, &args)) != 0) {
258 vfs_unbusy(mp, false, NULL);
259 vfs_destroy(mp);
260 return (error);
261 }
262
263 if ((error = update_mp(mp, &args)) != 0) {
264 (void)msdosfs_unmount(mp, 0);
265 vfs_unbusy(mp, false, NULL);
266 vfs_destroy(mp);
267 vrele(rootvp);
268 return (error);
269 }
270
271 mutex_enter(&mountlist_lock);
272 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
273 mutex_exit(&mountlist_lock);
274 (void)msdosfs_statvfs(mp, &mp->mnt_stat);
275 vfs_unbusy(mp, false, NULL);
276 return (0);
277 }
278
279 /*
280 * mp - path - addr in user space of mount point (ie /usr or whatever)
281 * data - addr in user space of mount params including the name of the block
282 * special file to treat as a filesystem.
283 */
284 int
285 msdosfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
286 {
287 struct lwp *l = curlwp;
288 struct nameidata nd;
289 struct vnode *devvp; /* vnode for blk device to mount */
290 struct msdosfs_args *args = data; /* holds data from mount request */
291 /* msdosfs specific mount control block */
292 struct msdosfsmount *pmp = NULL;
293 int error, flags;
294 mode_t accessmode;
295
296 if (*data_len < sizeof *args)
297 return EINVAL;
298
299 if (mp->mnt_flag & MNT_GETARGS) {
300 pmp = VFSTOMSDOSFS(mp);
301 if (pmp == NULL)
302 return EIO;
303 args->fspec = NULL;
304 args->uid = pmp->pm_uid;
305 args->gid = pmp->pm_gid;
306 args->mask = pmp->pm_mask;
307 args->flags = pmp->pm_flags;
308 args->version = MSDOSFSMNT_VERSION;
309 args->dirmask = pmp->pm_dirmask;
310 args->gmtoff = pmp->pm_gmtoff;
311 *data_len = sizeof *args;
312 return 0;
313 }
314
315 /*
316 * If not versioned (i.e. using old mount_msdos(8)), fill in
317 * the additional structure items with suitable defaults.
318 */
319 if ((args->flags & MSDOSFSMNT_VERSIONED) == 0) {
320 args->version = 1;
321 args->dirmask = args->mask;
322 }
323
324 /*
325 * Reset GMT offset for pre-v3 mount structure args.
326 */
327 if (args->version < 3)
328 args->gmtoff = 0;
329
330 /*
331 * If updating, check whether changing from read-only to
332 * read/write; if there is no device name, that's all we do.
333 */
334 if (mp->mnt_flag & MNT_UPDATE) {
335 pmp = VFSTOMSDOSFS(mp);
336 error = 0;
337 if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) &&
338 (mp->mnt_flag & MNT_RDONLY)) {
339 flags = WRITECLOSE;
340 if (mp->mnt_flag & MNT_FORCE)
341 flags |= FORCECLOSE;
342 error = vflush(mp, NULLVP, flags);
343 }
344 if (!error && (mp->mnt_flag & MNT_RELOAD))
345 /* not yet implemented */
346 error = EOPNOTSUPP;
347 if (error) {
348 DPRINTF(("vflush %d\n", error));
349 return (error);
350 }
351 if ((pmp->pm_flags & MSDOSFSMNT_RONLY) &&
352 (mp->mnt_iflag & IMNT_WANTRDWR)) {
353 /*
354 * If upgrade to read-write by non-root, then verify
355 * that user has necessary permissions on the device.
356 *
357 * Permission to update a mount is checked higher, so
358 * here we presume updating the mount is okay (for
359 * example, as far as securelevel goes) which leaves us
360 * with the normal check.
361 */
362 devvp = pmp->pm_devvp;
363 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
364 error = genfs_can_mount(devvp, VREAD | VWRITE,
365 l->l_cred);
366 VOP_UNLOCK(devvp, 0);
367 DPRINTF(("genfs_can_mount %d\n", error));
368 if (error)
369 return (error);
370
371 pmp->pm_flags &= ~MSDOSFSMNT_RONLY;
372 }
373 if (args->fspec == NULL) {
374 DPRINTF(("missing fspec\n"));
375 return EINVAL;
376 }
377 }
378 /*
379 * Not an update, or updating the name: look up the name
380 * and verify that it refers to a sensible block device.
381 */
382 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, args->fspec);
383 if ((error = namei(&nd)) != 0) {
384 DPRINTF(("namei %d\n", error));
385 return (error);
386 }
387 devvp = nd.ni_vp;
388
389 if (devvp->v_type != VBLK) {
390 DPRINTF(("not block\n"));
391 vrele(devvp);
392 return (ENOTBLK);
393 }
394 if (bdevsw_lookup(devvp->v_rdev) == NULL) {
395 DPRINTF(("no block switch\n"));
396 vrele(devvp);
397 return (ENXIO);
398 }
399 /*
400 * If mount by non-root, then verify that user has necessary
401 * permissions on the device.
402 */
403 accessmode = VREAD;
404 if ((mp->mnt_flag & MNT_RDONLY) == 0)
405 accessmode |= VWRITE;
406 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
407 error = genfs_can_mount(devvp, accessmode, l->l_cred);
408 VOP_UNLOCK(devvp, 0);
409 if (error) {
410 DPRINTF(("genfs_can_mount %d\n", error));
411 vrele(devvp);
412 return (error);
413 }
414 if ((mp->mnt_flag & MNT_UPDATE) == 0) {
415 int xflags;
416
417 if (mp->mnt_flag & MNT_RDONLY)
418 xflags = FREAD;
419 else
420 xflags = FREAD|FWRITE;
421 error = VOP_OPEN(devvp, xflags, FSCRED);
422 if (error) {
423 DPRINTF(("VOP_OPEN %d\n", error));
424 goto fail;
425 }
426 error = msdosfs_mountfs(devvp, mp, l, args);
427 if (error) {
428 DPRINTF(("msdosfs_mountfs %d\n", error));
429 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
430 (void) VOP_CLOSE(devvp, xflags, NOCRED);
431 VOP_UNLOCK(devvp, 0);
432 goto fail;
433 }
434 #ifdef MSDOSFS_DEBUG /* only needed for the printf below */
435 pmp = VFSTOMSDOSFS(mp);
436 #endif
437 } else {
438 vrele(devvp);
439 if (devvp != pmp->pm_devvp) {
440 DPRINTF(("devvp %p pmp %p\n",
441 devvp, pmp->pm_devvp));
442 return (EINVAL); /* needs translation */
443 }
444 }
445 if ((error = update_mp(mp, args)) != 0) {
446 msdosfs_unmount(mp, MNT_FORCE);
447 DPRINTF(("update_mp %d\n", error));
448 return error;
449 }
450
451 #ifdef MSDOSFS_DEBUG
452 printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap);
453 #endif
454 return set_statvfs_info(path, UIO_USERSPACE, args->fspec, UIO_USERSPACE,
455 mp->mnt_op->vfs_name, mp, l);
456
457 fail:
458 vrele(devvp);
459 return (error);
460 }
461
462 int
463 msdosfs_mountfs(struct vnode *devvp, struct mount *mp, struct lwp *l, struct msdosfs_args *argp)
464 {
465 struct msdosfsmount *pmp;
466 struct buf *bp;
467 dev_t dev = devvp->v_rdev;
468 struct partinfo dpart;
469 union bootsector *bsp;
470 struct byte_bpb33 *b33;
471 struct byte_bpb50 *b50;
472 struct byte_bpb710 *b710;
473 u_int8_t SecPerClust;
474 int ronly, error, tmp;
475 int bsize, dtype, fstype, secsize;
476 u_int64_t psize;
477
478 /* Flush out any old buffers remaining from a previous use. */
479 if ((error = vinvalbuf(devvp, V_SAVE, l->l_cred, l, 0, 0)) != 0)
480 return (error);
481
482 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
483
484 bp = NULL; /* both used in error_exit */
485 pmp = NULL;
486
487 /*
488 * We need the disklabel to calculate the size of a FAT entry
489 * later on. Also make sure the partition contains a filesystem
490 * of type FS_MSDOS. This doesn't work for floppies, so we have
491 * to check for them too.
492 *
493 * There might still be parts of the msdos fs driver which assume
494 * that the size of a disk block will always be 512 bytes.
495 * Let's root them out...
496 */
497 error = VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, NOCRED);
498 if (error == 0) {
499 secsize = dpart.disklab->d_secsize;
500 dtype = dpart.disklab->d_type;
501 fstype = dpart.part->p_fstype;
502 psize = dpart.part->p_size;
503 } else {
504 struct dkwedge_info dkw;
505 error = VOP_IOCTL(devvp, DIOCGWEDGEINFO, &dkw, FREAD, NOCRED);
506 secsize = 512; /* XXX */
507 dtype = DTYPE_FLOPPY; /* XXX */
508 fstype = FS_MSDOS;
509 psize = -1;
510 if (error) {
511 if (error != ENOTTY) {
512 DPRINTF(("Error getting partition info %d\n",
513 error));
514 goto error_exit;
515 }
516 } else {
517 fstype = strcmp(dkw.dkw_ptype, DKW_PTYPE_FAT) == 0 ?
518 FS_MSDOS : -1;
519 psize = dkw.dkw_size;
520 }
521 }
522 if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
523 bsize = secsize;
524 if (bsize != 512) {
525 DPRINTF(("bsize %d dtype %d fstype %d\n", bsize, dtype,
526 fstype));
527 error = EINVAL;
528 goto error_exit;
529 }
530 } else
531 bsize = 0;
532
533 /*
534 * Read the boot sector of the filesystem, and then check the
535 * boot signature. If not a dos boot sector then error out.
536 */
537 if ((error = bread(devvp, 0, secsize, NOCRED, 0, &bp)) != 0)
538 goto error_exit;
539 bsp = (union bootsector *)bp->b_data;
540 b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
541 b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
542 b710 = (struct byte_bpb710 *)bsp->bs710.bsBPB;
543
544 if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
545 if (bsp->bs50.bsBootSectSig0 != BOOTSIG0
546 || bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
547 DPRINTF(("bootsig0 %d bootsig1 %d\n",
548 bsp->bs50.bsBootSectSig0,
549 bsp->bs50.bsBootSectSig1));
550 error = EINVAL;
551 goto error_exit;
552 }
553 }
554
555 pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK);
556 memset(pmp, 0, sizeof *pmp);
557 pmp->pm_mountp = mp;
558
559 /*
560 * Compute several useful quantities from the bpb in the
561 * bootsector. Copy in the dos 5 variant of the bpb then fix up
562 * the fields that are different between dos 5 and dos 3.3.
563 */
564 SecPerClust = b50->bpbSecPerClust;
565 pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
566 pmp->pm_ResSectors = getushort(b50->bpbResSectors);
567 pmp->pm_FATs = b50->bpbFATs;
568 pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
569 pmp->pm_Sectors = getushort(b50->bpbSectors);
570 pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
571 pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
572 pmp->pm_Heads = getushort(b50->bpbHeads);
573 pmp->pm_Media = b50->bpbMedia;
574
575 if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
576 /* XXX - We should probably check more values here */
577 if (!pmp->pm_BytesPerSec || !SecPerClust
578 || pmp->pm_SecPerTrack > 63) {
579 DPRINTF(("bytespersec %d secperclust %d "
580 "secpertrack %d\n",
581 pmp->pm_BytesPerSec, SecPerClust,
582 pmp->pm_SecPerTrack));
583 error = EINVAL;
584 goto error_exit;
585 }
586 }
587
588 if (pmp->pm_Sectors == 0) {
589 pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
590 pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
591 } else {
592 pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
593 pmp->pm_HugeSectors = pmp->pm_Sectors;
594 }
595
596 if (pmp->pm_RootDirEnts == 0) {
597 unsigned short vers = getushort(b710->bpbFSVers);
598 /*
599 * Some say that bsBootSectSig[23] must be zero, but
600 * Windows does not require this and some digital cameras
601 * do not set these to zero. Therefore, do not insist.
602 */
603 if (pmp->pm_Sectors || pmp->pm_FATsecs || vers) {
604 DPRINTF(("sectors %d fatsecs %lu vers %d\n",
605 pmp->pm_Sectors, pmp->pm_FATsecs, vers));
606 error = EINVAL;
607 goto error_exit;
608 }
609 pmp->pm_fatmask = FAT32_MASK;
610 pmp->pm_fatmult = 4;
611 pmp->pm_fatdiv = 1;
612 pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
613
614 /* mirrorring is enabled if the FATMIRROR bit is not set */
615 if ((getushort(b710->bpbExtFlags) & FATMIRROR) == 0)
616 pmp->pm_flags |= MSDOSFS_FATMIRROR;
617 else
618 pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
619 } else
620 pmp->pm_flags |= MSDOSFS_FATMIRROR;
621
622 if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
623 if (FAT32(pmp)) {
624 DPRINTF(("fat32 for gemdos\n"));
625 /*
626 * GEMDOS doesn't know fat32.
627 */
628 error = EINVAL;
629 goto error_exit;
630 }
631
632 /*
633 * Check a few values (could do some more):
634 * - logical sector size: power of 2, >= block size
635 * - sectors per cluster: power of 2, >= 1
636 * - number of sectors: >= 1, <= size of partition
637 */
638 if ( (SecPerClust == 0)
639 || (SecPerClust & (SecPerClust - 1))
640 || (pmp->pm_BytesPerSec < bsize)
641 || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
642 || (pmp->pm_HugeSectors == 0)
643 || (pmp->pm_HugeSectors * (pmp->pm_BytesPerSec / bsize)
644 > psize)) {
645 DPRINTF(("consistency checks for gemdos\n"));
646 error = EINVAL;
647 goto error_exit;
648 }
649 /*
650 * XXX - Many parts of the msdos fs driver seem to assume that
651 * the number of bytes per logical sector (BytesPerSec) will
652 * always be the same as the number of bytes per disk block
653 * Let's pretend it is.
654 */
655 tmp = pmp->pm_BytesPerSec / bsize;
656 pmp->pm_BytesPerSec = bsize;
657 pmp->pm_HugeSectors *= tmp;
658 pmp->pm_HiddenSects *= tmp;
659 pmp->pm_ResSectors *= tmp;
660 pmp->pm_Sectors *= tmp;
661 pmp->pm_FATsecs *= tmp;
662 SecPerClust *= tmp;
663 }
664
665 /* Check that fs has nonzero FAT size */
666 if (pmp->pm_FATsecs == 0) {
667 DPRINTF(("FATsecs is 0\n"));
668 error = EINVAL;
669 goto error_exit;
670 }
671
672 pmp->pm_fatblk = pmp->pm_ResSectors;
673 if (FAT32(pmp)) {
674 pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
675 pmp->pm_firstcluster = pmp->pm_fatblk
676 + (pmp->pm_FATs * pmp->pm_FATsecs);
677 pmp->pm_fsinfo = getushort(b710->bpbFSInfo);
678 } else {
679 pmp->pm_rootdirblk = pmp->pm_fatblk +
680 (pmp->pm_FATs * pmp->pm_FATsecs);
681 pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
682 + pmp->pm_BytesPerSec - 1)
683 / pmp->pm_BytesPerSec;/* in sectors */
684 pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
685 }
686
687 pmp->pm_nmbrofclusters = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
688 SecPerClust;
689 pmp->pm_maxcluster = pmp->pm_nmbrofclusters + 1;
690 pmp->pm_fatsize = pmp->pm_FATsecs * pmp->pm_BytesPerSec;
691
692 if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
693 if (pmp->pm_nmbrofclusters <= (0xff0 - 2)
694 && (dtype == DTYPE_FLOPPY
695 || (dtype == DTYPE_VND
696 && (pmp->pm_Heads == 1 || pmp->pm_Heads == 2)))
697 ) {
698 pmp->pm_fatmask = FAT12_MASK;
699 pmp->pm_fatmult = 3;
700 pmp->pm_fatdiv = 2;
701 } else {
702 pmp->pm_fatmask = FAT16_MASK;
703 pmp->pm_fatmult = 2;
704 pmp->pm_fatdiv = 1;
705 }
706 } else if (pmp->pm_fatmask == 0) {
707 if (pmp->pm_maxcluster
708 <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
709 /*
710 * This will usually be a floppy disk. This size makes
711 * sure that one fat entry will not be split across
712 * multiple blocks.
713 */
714 pmp->pm_fatmask = FAT12_MASK;
715 pmp->pm_fatmult = 3;
716 pmp->pm_fatdiv = 2;
717 } else {
718 pmp->pm_fatmask = FAT16_MASK;
719 pmp->pm_fatmult = 2;
720 pmp->pm_fatdiv = 1;
721 }
722 }
723 if (FAT12(pmp))
724 pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec;
725 else
726 pmp->pm_fatblocksize = MAXBSIZE;
727
728 pmp->pm_fatblocksec = pmp->pm_fatblocksize / pmp->pm_BytesPerSec;
729 pmp->pm_bnshift = ffs(pmp->pm_BytesPerSec) - 1;
730
731 /*
732 * Compute mask and shift value for isolating cluster relative byte
733 * offsets and cluster numbers from a file offset.
734 */
735 pmp->pm_bpcluster = SecPerClust * pmp->pm_BytesPerSec;
736 pmp->pm_crbomask = pmp->pm_bpcluster - 1;
737 pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
738
739 /*
740 * Check for valid cluster size
741 * must be a power of 2
742 */
743 if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
744 DPRINTF(("bpcluster %lu cnshift %lu\n",
745 pmp->pm_bpcluster, pmp->pm_cnshift));
746 error = EINVAL;
747 goto error_exit;
748 }
749
750 /*
751 * Release the bootsector buffer.
752 */
753 brelse(bp, BC_AGE);
754 bp = NULL;
755
756 /*
757 * Check FSInfo.
758 */
759 if (pmp->pm_fsinfo) {
760 struct fsinfo *fp;
761
762 /*
763 * XXX If the fsinfo block is stored on media with
764 * 2KB or larger sectors, is the fsinfo structure
765 * padded at the end or in the middle?
766 */
767 if ((error = bread(devvp, de_bn2kb(pmp, pmp->pm_fsinfo),
768 pmp->pm_BytesPerSec, NOCRED, 0, &bp)) != 0)
769 goto error_exit;
770 fp = (struct fsinfo *)bp->b_data;
771 if (!memcmp(fp->fsisig1, "RRaA", 4)
772 && !memcmp(fp->fsisig2, "rrAa", 4)
773 && !memcmp(fp->fsisig3, "\0\0\125\252", 4)
774 && !memcmp(fp->fsisig4, "\0\0\125\252", 4))
775 pmp->pm_nxtfree = getulong(fp->fsinxtfree);
776 else
777 pmp->pm_fsinfo = 0;
778 brelse(bp, 0);
779 bp = NULL;
780 }
781
782 /*
783 * Check and validate (or perhaps invalidate?) the fsinfo structure?
784 * XXX
785 */
786 if (pmp->pm_fsinfo) {
787 if (pmp->pm_nxtfree == (u_long)-1)
788 pmp->pm_fsinfo = 0;
789 }
790
791 /*
792 * Allocate memory for the bitmap of allocated clusters, and then
793 * fill it in.
794 */
795 pmp->pm_inusemap = malloc(((pmp->pm_maxcluster + N_INUSEBITS - 1)
796 / N_INUSEBITS)
797 * sizeof(*pmp->pm_inusemap),
798 M_MSDOSFSFAT, M_WAITOK);
799
800 /*
801 * fillinusemap() needs pm_devvp.
802 */
803 pmp->pm_dev = dev;
804 pmp->pm_devvp = devvp;
805
806 /*
807 * Have the inuse map filled in.
808 */
809 if ((error = fillinusemap(pmp)) != 0) {
810 DPRINTF(("fillinusemap %d\n", error));
811 goto error_exit;
812 }
813
814 /*
815 * If they want fat updates to be synchronous then let them suffer
816 * the performance degradation in exchange for the on disk copy of
817 * the fat being correct just about all the time. I suppose this
818 * would be a good thing to turn on if the kernel is still flakey.
819 */
820 if (mp->mnt_flag & MNT_SYNCHRONOUS)
821 pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
822
823 /*
824 * Finish up.
825 */
826 if (ronly)
827 pmp->pm_flags |= MSDOSFSMNT_RONLY;
828 else
829 pmp->pm_fmod = 1;
830 mp->mnt_data = pmp;
831 mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
832 mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_MSDOS);
833 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
834 mp->mnt_stat.f_namemax = MSDOSFS_NAMEMAX(pmp);
835 mp->mnt_flag |= MNT_LOCAL;
836 mp->mnt_dev_bshift = pmp->pm_bnshift;
837 mp->mnt_fs_bshift = pmp->pm_cnshift;
838
839 /*
840 * If we ever do quotas for DOS filesystems this would be a place
841 * to fill in the info in the msdosfsmount structure. You dolt,
842 * quotas on dos filesystems make no sense because files have no
843 * owners on dos filesystems. of course there is some empty space
844 * in the directory entry where we could put uid's and gid's.
845 */
846
847 devvp->v_specmountpoint = mp;
848
849 return (0);
850
851 error_exit:;
852 if (bp)
853 brelse(bp, BC_AGE);
854 if (pmp) {
855 if (pmp->pm_inusemap)
856 free(pmp->pm_inusemap, M_MSDOSFSFAT);
857 free(pmp, M_MSDOSFSMNT);
858 mp->mnt_data = NULL;
859 }
860 return (error);
861 }
862
863 int
864 msdosfs_start(struct mount *mp, int flags)
865 {
866
867 return (0);
868 }
869
870 /*
871 * Unmount the filesystem described by mp.
872 */
873 int
874 msdosfs_unmount(struct mount *mp, int mntflags)
875 {
876 struct msdosfsmount *pmp;
877 int error, flags;
878
879 flags = 0;
880 if (mntflags & MNT_FORCE)
881 flags |= FORCECLOSE;
882 if ((error = vflush(mp, NULLVP, flags)) != 0)
883 return (error);
884 pmp = VFSTOMSDOSFS(mp);
885 if (pmp->pm_devvp->v_type != VBAD)
886 pmp->pm_devvp->v_specmountpoint = NULL;
887 #ifdef MSDOSFS_DEBUG
888 {
889 struct vnode *vp = pmp->pm_devvp;
890
891 printf("msdosfs_umount(): just before calling VOP_CLOSE()\n");
892 printf("flag %08x, usecount %d, writecount %d, holdcnt %d\n",
893 vp->v_vflag | vp->v_iflag | vp->v_uflag, vp->v_usecount,
894 vp->v_writecount, vp->v_holdcnt);
895 printf("mount %p, op %p\n",
896 vp->v_mount, vp->v_op);
897 printf("freef %p, freeb %p, mount %p\n",
898 vp->v_freelist.tqe_next, vp->v_freelist.tqe_prev,
899 vp->v_mount);
900 printf("cleanblkhd %p, dirtyblkhd %p, numoutput %d, type %d\n",
901 vp->v_cleanblkhd.lh_first,
902 vp->v_dirtyblkhd.lh_first,
903 vp->v_numoutput, vp->v_type);
904 printf("union %p, tag %d, data[0] %08x, data[1] %08x\n",
905 vp->v_socket, vp->v_tag,
906 ((u_int *)vp->v_data)[0],
907 ((u_int *)vp->v_data)[1]);
908 }
909 #endif
910 vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY);
911 error = VOP_CLOSE(pmp->pm_devvp,
912 pmp->pm_flags & MSDOSFSMNT_RONLY ? FREAD : FREAD|FWRITE, NOCRED);
913 vput(pmp->pm_devvp);
914 free(pmp->pm_inusemap, M_MSDOSFSFAT);
915 free(pmp, M_MSDOSFSMNT);
916 mp->mnt_data = NULL;
917 mp->mnt_flag &= ~MNT_LOCAL;
918 return (error);
919 }
920
921 int
922 msdosfs_root(struct mount *mp, struct vnode **vpp)
923 {
924 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
925 struct denode *ndep;
926 int error;
927
928 #ifdef MSDOSFS_DEBUG
929 printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
930 #endif
931 if ((error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep)) != 0)
932 return (error);
933 *vpp = DETOV(ndep);
934 return (0);
935 }
936
937 int
938 msdosfs_statvfs(struct mount *mp, struct statvfs *sbp)
939 {
940 struct msdosfsmount *pmp;
941
942 pmp = VFSTOMSDOSFS(mp);
943 sbp->f_bsize = pmp->pm_bpcluster;
944 sbp->f_frsize = sbp->f_bsize;
945 sbp->f_iosize = pmp->pm_bpcluster;
946 sbp->f_blocks = pmp->pm_nmbrofclusters;
947 sbp->f_bfree = pmp->pm_freeclustercount;
948 sbp->f_bavail = pmp->pm_freeclustercount;
949 sbp->f_bresvd = 0;
950 sbp->f_files = pmp->pm_RootDirEnts; /* XXX */
951 sbp->f_ffree = 0; /* what to put in here? */
952 sbp->f_favail = 0; /* what to put in here? */
953 sbp->f_fresvd = 0;
954 copy_statvfs_info(sbp, mp);
955 return (0);
956 }
957
958 int
959 msdosfs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
960 {
961 struct vnode *vp, *mvp;
962 struct denode *dep;
963 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
964 int error, allerror = 0;
965
966 /*
967 * If we ever switch to not updating all of the fats all the time,
968 * this would be the place to update them from the first one.
969 */
970 if (pmp->pm_fmod != 0) {
971 if (pmp->pm_flags & MSDOSFSMNT_RONLY)
972 panic("msdosfs_sync: rofs mod");
973 else {
974 /* update fats here */
975 }
976 }
977 /* Allocate a marker vnode. */
978 if ((mvp = vnalloc(mp)) == NULL)
979 return ENOMEM;
980 /*
981 * Write back each (modified) denode.
982 */
983 mutex_enter(&mntvnode_lock);
984 loop:
985 for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = vunmark(mvp)) {
986 vmark(mvp, vp);
987 if (vp->v_mount != mp || vismarker(vp))
988 continue;
989 mutex_enter(&vp->v_interlock);
990 dep = VTODE(vp);
991 if (waitfor == MNT_LAZY || vp->v_type == VNON ||
992 (((dep->de_flag &
993 (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0) &&
994 (LIST_EMPTY(&vp->v_dirtyblkhd) &&
995 UVM_OBJ_IS_CLEAN(&vp->v_uobj)))) {
996 mutex_exit(&vp->v_interlock);
997 continue;
998 }
999 mutex_exit(&mntvnode_lock);
1000 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
1001 if (error) {
1002 mutex_enter(&mntvnode_lock);
1003 if (error == ENOENT) {
1004 (void)vunmark(mvp);
1005 goto loop;
1006 }
1007 continue;
1008 }
1009 if ((error = VOP_FSYNC(vp, cred,
1010 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0)) != 0)
1011 allerror = error;
1012 vput(vp);
1013 mutex_enter(&mntvnode_lock);
1014 }
1015 mutex_exit(&mntvnode_lock);
1016 vnfree(mvp);
1017
1018 /*
1019 * Force stale file system control information to be flushed.
1020 */
1021 if ((error = VOP_FSYNC(pmp->pm_devvp, cred,
1022 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0)) != 0)
1023 allerror = error;
1024 return (allerror);
1025 }
1026
1027 int
1028 msdosfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
1029 {
1030 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
1031 struct defid defh;
1032 struct denode *dep;
1033 int error;
1034
1035 if (fhp->fid_len != sizeof(struct defid)) {
1036 DPRINTF(("fid_len %d %zd\n", fhp->fid_len,
1037 sizeof(struct defid)));
1038 return EINVAL;
1039 }
1040
1041 memcpy(&defh, fhp, sizeof(defh));
1042 error = deget(pmp, defh.defid_dirclust, defh.defid_dirofs, &dep);
1043 if (error) {
1044 DPRINTF(("deget %d\n", error));
1045 *vpp = NULLVP;
1046 return (error);
1047 }
1048 *vpp = DETOV(dep);
1049 return (0);
1050 }
1051
1052 int
1053 msdosfs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
1054 {
1055 struct denode *dep;
1056 struct defid defh;
1057
1058 if (*fh_size < sizeof(struct defid)) {
1059 *fh_size = sizeof(struct defid);
1060 return E2BIG;
1061 }
1062 *fh_size = sizeof(struct defid);
1063 dep = VTODE(vp);
1064 memset(&defh, 0, sizeof(defh));
1065 defh.defid_len = sizeof(struct defid);
1066 defh.defid_dirclust = dep->de_dirclust;
1067 defh.defid_dirofs = dep->de_diroffset;
1068 /* defh.defid_gen = dep->de_gen; */
1069 memcpy(fhp, &defh, sizeof(defh));
1070 return (0);
1071 }
1072
1073 int
1074 msdosfs_vget(struct mount *mp, ino_t ino,
1075 struct vnode **vpp)
1076 {
1077
1078 return (EOPNOTSUPP);
1079 }
1080