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