msdosfs_vfsops.c revision 1.71 1 /* $NetBSD: msdosfs_vfsops.c,v 1.71 2009/02/05 18:39:15 abs 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.71 2009/02/05 18:39:15 abs 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 about win95 long filenames
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 DPRINTF(("bsize %d dtype %d fstype %d\n", bsize, dtype,
533 fstype));
534 error = EINVAL;
535 goto error_exit;
536 }
537 } else
538 bsize = 0;
539
540 /*
541 * Read the boot sector of the filesystem, and then check the
542 * boot signature. If not a dos boot sector then error out.
543 */
544 if ((error = bread(devvp, 0, secsize, NOCRED, 0, &bp)) != 0)
545 goto error_exit;
546 bsp = (union bootsector *)bp->b_data;
547 b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
548 b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
549 b710 = (struct byte_bpb710 *)bsp->bs710.bsBPB;
550
551 if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
552 if (bsp->bs50.bsBootSectSig0 != BOOTSIG0
553 || bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
554 DPRINTF(("bootsig0 %d bootsig1 %d\n",
555 bsp->bs50.bsBootSectSig0,
556 bsp->bs50.bsBootSectSig1));
557 error = EINVAL;
558 goto error_exit;
559 }
560 }
561
562 pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK);
563 memset(pmp, 0, sizeof *pmp);
564 pmp->pm_mountp = mp;
565
566 /*
567 * Compute several useful quantities from the bpb in the
568 * bootsector. Copy in the dos 5 variant of the bpb then fix up
569 * the fields that are different between dos 5 and dos 3.3.
570 */
571 SecPerClust = b50->bpbSecPerClust;
572 pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
573 pmp->pm_ResSectors = getushort(b50->bpbResSectors);
574 pmp->pm_FATs = b50->bpbFATs;
575 pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
576 pmp->pm_Sectors = getushort(b50->bpbSectors);
577 pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
578 pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
579 pmp->pm_Heads = getushort(b50->bpbHeads);
580 pmp->pm_Media = b50->bpbMedia;
581
582 if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
583 /* XXX - We should probably check more values here */
584 if (!pmp->pm_BytesPerSec || !SecPerClust
585 || pmp->pm_SecPerTrack > 63) {
586 DPRINTF(("bytespersec %d secperclust %d "
587 "secpertrack %d\n",
588 pmp->pm_BytesPerSec, SecPerClust,
589 pmp->pm_SecPerTrack));
590 error = EINVAL;
591 goto error_exit;
592 }
593 }
594
595 if (pmp->pm_Sectors == 0) {
596 pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
597 pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
598 } else {
599 pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
600 pmp->pm_HugeSectors = pmp->pm_Sectors;
601 }
602
603 if (pmp->pm_RootDirEnts == 0) {
604 unsigned short vers = getushort(b710->bpbFSVers);
605 /*
606 * Some say that bsBootSectSig[23] must be zero, but
607 * Windows does not require this and some digital cameras
608 * do not set these to zero. Therefore, do not insist.
609 */
610 if (pmp->pm_Sectors || pmp->pm_FATsecs || vers) {
611 DPRINTF(("sectors %d fatsecs %lu vers %d\n",
612 pmp->pm_Sectors, pmp->pm_FATsecs, vers));
613 error = EINVAL;
614 goto error_exit;
615 }
616 pmp->pm_fatmask = FAT32_MASK;
617 pmp->pm_fatmult = 4;
618 pmp->pm_fatdiv = 1;
619 pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
620
621 /* mirrorring is enabled if the FATMIRROR bit is not set */
622 if ((getushort(b710->bpbExtFlags) & FATMIRROR) == 0)
623 pmp->pm_flags |= MSDOSFS_FATMIRROR;
624 else
625 pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
626 } else
627 pmp->pm_flags |= MSDOSFS_FATMIRROR;
628
629 if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
630 if (FAT32(pmp)) {
631 DPRINTF(("fat32 for gemdos\n"));
632 /*
633 * GEMDOS doesn't know fat32.
634 */
635 error = EINVAL;
636 goto error_exit;
637 }
638
639 /*
640 * Check a few values (could do some more):
641 * - logical sector size: power of 2, >= block size
642 * - sectors per cluster: power of 2, >= 1
643 * - number of sectors: >= 1, <= size of partition
644 */
645 if ( (SecPerClust == 0)
646 || (SecPerClust & (SecPerClust - 1))
647 || (pmp->pm_BytesPerSec < bsize)
648 || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
649 || (pmp->pm_HugeSectors == 0)
650 || (pmp->pm_HugeSectors * (pmp->pm_BytesPerSec / bsize)
651 > psize)) {
652 DPRINTF(("consistency checks for gemdos\n"));
653 error = EINVAL;
654 goto error_exit;
655 }
656 /*
657 * XXX - Many parts of the msdos fs driver seem to assume that
658 * the number of bytes per logical sector (BytesPerSec) will
659 * always be the same as the number of bytes per disk block
660 * Let's pretend it is.
661 */
662 tmp = pmp->pm_BytesPerSec / bsize;
663 pmp->pm_BytesPerSec = bsize;
664 pmp->pm_HugeSectors *= tmp;
665 pmp->pm_HiddenSects *= tmp;
666 pmp->pm_ResSectors *= tmp;
667 pmp->pm_Sectors *= tmp;
668 pmp->pm_FATsecs *= tmp;
669 SecPerClust *= tmp;
670 }
671
672 /* Check that fs has nonzero FAT size */
673 if (pmp->pm_FATsecs == 0) {
674 DPRINTF(("FATsecs is 0\n"));
675 error = EINVAL;
676 goto error_exit;
677 }
678
679 pmp->pm_fatblk = pmp->pm_ResSectors;
680 if (FAT32(pmp)) {
681 pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
682 pmp->pm_firstcluster = pmp->pm_fatblk
683 + (pmp->pm_FATs * pmp->pm_FATsecs);
684 pmp->pm_fsinfo = getushort(b710->bpbFSInfo);
685 } else {
686 pmp->pm_rootdirblk = pmp->pm_fatblk +
687 (pmp->pm_FATs * pmp->pm_FATsecs);
688 pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
689 + pmp->pm_BytesPerSec - 1)
690 / pmp->pm_BytesPerSec;/* in sectors */
691 pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
692 }
693
694 pmp->pm_nmbrofclusters = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
695 SecPerClust;
696 pmp->pm_maxcluster = pmp->pm_nmbrofclusters + 1;
697 pmp->pm_fatsize = pmp->pm_FATsecs * pmp->pm_BytesPerSec;
698
699 if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
700 if (pmp->pm_nmbrofclusters <= (0xff0 - 2)
701 && (dtype == DTYPE_FLOPPY
702 || (dtype == DTYPE_VND
703 && (pmp->pm_Heads == 1 || pmp->pm_Heads == 2)))
704 ) {
705 pmp->pm_fatmask = FAT12_MASK;
706 pmp->pm_fatmult = 3;
707 pmp->pm_fatdiv = 2;
708 } else {
709 pmp->pm_fatmask = FAT16_MASK;
710 pmp->pm_fatmult = 2;
711 pmp->pm_fatdiv = 1;
712 }
713 } else if (pmp->pm_fatmask == 0) {
714 if (pmp->pm_maxcluster
715 <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
716 /*
717 * This will usually be a floppy disk. This size makes
718 * sure that one fat entry will not be split across
719 * multiple blocks.
720 */
721 pmp->pm_fatmask = FAT12_MASK;
722 pmp->pm_fatmult = 3;
723 pmp->pm_fatdiv = 2;
724 } else {
725 pmp->pm_fatmask = FAT16_MASK;
726 pmp->pm_fatmult = 2;
727 pmp->pm_fatdiv = 1;
728 }
729 }
730 if (FAT12(pmp))
731 pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec;
732 else
733 pmp->pm_fatblocksize = MAXBSIZE;
734
735 pmp->pm_fatblocksec = pmp->pm_fatblocksize / pmp->pm_BytesPerSec;
736 pmp->pm_bnshift = ffs(pmp->pm_BytesPerSec) - 1;
737
738 /*
739 * Compute mask and shift value for isolating cluster relative byte
740 * offsets and cluster numbers from a file offset.
741 */
742 pmp->pm_bpcluster = SecPerClust * pmp->pm_BytesPerSec;
743 pmp->pm_crbomask = pmp->pm_bpcluster - 1;
744 pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
745
746 /*
747 * Check for valid cluster size
748 * must be a power of 2
749 */
750 if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
751 DPRINTF(("bpcluster %lu cnshift %lu\n",
752 pmp->pm_bpcluster, pmp->pm_cnshift));
753 error = EINVAL;
754 goto error_exit;
755 }
756
757 /*
758 * Release the bootsector buffer.
759 */
760 brelse(bp, BC_AGE);
761 bp = NULL;
762
763 /*
764 * Check FSInfo.
765 */
766 if (pmp->pm_fsinfo) {
767 struct fsinfo *fp;
768
769 /*
770 * XXX If the fsinfo block is stored on media with
771 * 2KB or larger sectors, is the fsinfo structure
772 * padded at the end or in the middle?
773 */
774 if ((error = bread(devvp, de_bn2kb(pmp, pmp->pm_fsinfo),
775 pmp->pm_BytesPerSec, NOCRED, 0, &bp)) != 0)
776 goto error_exit;
777 fp = (struct fsinfo *)bp->b_data;
778 if (!memcmp(fp->fsisig1, "RRaA", 4)
779 && !memcmp(fp->fsisig2, "rrAa", 4)
780 && !memcmp(fp->fsisig3, "\0\0\125\252", 4)
781 && !memcmp(fp->fsisig4, "\0\0\125\252", 4))
782 pmp->pm_nxtfree = getulong(fp->fsinxtfree);
783 else
784 pmp->pm_fsinfo = 0;
785 brelse(bp, 0);
786 bp = NULL;
787 }
788
789 /*
790 * Check and validate (or perhaps invalidate?) the fsinfo structure?
791 * XXX
792 */
793 if (pmp->pm_fsinfo) {
794 if (pmp->pm_nxtfree == (u_long)-1)
795 pmp->pm_fsinfo = 0;
796 }
797
798 /*
799 * Allocate memory for the bitmap of allocated clusters, and then
800 * fill it in.
801 */
802 pmp->pm_inusemap = malloc(((pmp->pm_maxcluster + N_INUSEBITS - 1)
803 / N_INUSEBITS)
804 * sizeof(*pmp->pm_inusemap),
805 M_MSDOSFSFAT, M_WAITOK);
806
807 /*
808 * fillinusemap() needs pm_devvp.
809 */
810 pmp->pm_dev = dev;
811 pmp->pm_devvp = devvp;
812
813 /*
814 * Have the inuse map filled in.
815 */
816 if ((error = fillinusemap(pmp)) != 0) {
817 DPRINTF(("fillinusemap %d\n", error));
818 goto error_exit;
819 }
820
821 /*
822 * If they want fat updates to be synchronous then let them suffer
823 * the performance degradation in exchange for the on disk copy of
824 * the fat being correct just about all the time. I suppose this
825 * would be a good thing to turn on if the kernel is still flakey.
826 */
827 if (mp->mnt_flag & MNT_SYNCHRONOUS)
828 pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
829
830 /*
831 * Finish up.
832 */
833 if (ronly)
834 pmp->pm_flags |= MSDOSFSMNT_RONLY;
835 else
836 pmp->pm_fmod = 1;
837 mp->mnt_data = pmp;
838 mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
839 mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_MSDOS);
840 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
841 mp->mnt_stat.f_namemax = MSDOSFS_NAMEMAX(pmp);
842 mp->mnt_flag |= MNT_LOCAL;
843 mp->mnt_dev_bshift = pmp->pm_bnshift;
844 mp->mnt_fs_bshift = pmp->pm_cnshift;
845
846 /*
847 * If we ever do quotas for DOS filesystems this would be a place
848 * to fill in the info in the msdosfsmount structure. You dolt,
849 * quotas on dos filesystems make no sense because files have no
850 * owners on dos filesystems. of course there is some empty space
851 * in the directory entry where we could put uid's and gid's.
852 */
853
854 devvp->v_specmountpoint = mp;
855
856 return (0);
857
858 error_exit:;
859 if (bp)
860 brelse(bp, BC_AGE);
861 if (pmp) {
862 if (pmp->pm_inusemap)
863 free(pmp->pm_inusemap, M_MSDOSFSFAT);
864 free(pmp, M_MSDOSFSMNT);
865 mp->mnt_data = NULL;
866 }
867 return (error);
868 }
869
870 int
871 msdosfs_start(struct mount *mp, int flags)
872 {
873
874 return (0);
875 }
876
877 /*
878 * Unmount the filesystem described by mp.
879 */
880 int
881 msdosfs_unmount(mp, mntflags)
882 struct mount *mp;
883 int mntflags;
884 {
885 struct msdosfsmount *pmp;
886 int error, flags;
887
888 flags = 0;
889 if (mntflags & MNT_FORCE)
890 flags |= FORCECLOSE;
891 if ((error = vflush(mp, NULLVP, flags)) != 0)
892 return (error);
893 pmp = VFSTOMSDOSFS(mp);
894 if (pmp->pm_devvp->v_type != VBAD)
895 pmp->pm_devvp->v_specmountpoint = NULL;
896 #ifdef MSDOSFS_DEBUG
897 {
898 struct vnode *vp = pmp->pm_devvp;
899
900 printf("msdosfs_umount(): just before calling VOP_CLOSE()\n");
901 printf("flag %08x, usecount %d, writecount %d, holdcnt %d\n",
902 vp->v_vflag | vp->v_iflag | vp->v_uflag, vp->v_usecount,
903 vp->v_writecount, vp->v_holdcnt);
904 printf("mount %p, op %p\n",
905 vp->v_mount, vp->v_op);
906 printf("freef %p, freeb %p, mount %p\n",
907 vp->v_freelist.tqe_next, vp->v_freelist.tqe_prev,
908 vp->v_mount);
909 printf("cleanblkhd %p, dirtyblkhd %p, numoutput %d, type %d\n",
910 vp->v_cleanblkhd.lh_first,
911 vp->v_dirtyblkhd.lh_first,
912 vp->v_numoutput, vp->v_type);
913 printf("union %p, tag %d, data[0] %08x, data[1] %08x\n",
914 vp->v_socket, vp->v_tag,
915 ((u_int *)vp->v_data)[0],
916 ((u_int *)vp->v_data)[1]);
917 }
918 #endif
919 vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY);
920 error = VOP_CLOSE(pmp->pm_devvp,
921 pmp->pm_flags & MSDOSFSMNT_RONLY ? FREAD : FREAD|FWRITE, NOCRED);
922 vput(pmp->pm_devvp);
923 free(pmp->pm_inusemap, M_MSDOSFSFAT);
924 free(pmp, M_MSDOSFSMNT);
925 mp->mnt_data = NULL;
926 mp->mnt_flag &= ~MNT_LOCAL;
927 return (error);
928 }
929
930 int
931 msdosfs_root(mp, vpp)
932 struct mount *mp;
933 struct vnode **vpp;
934 {
935 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
936 struct denode *ndep;
937 int error;
938
939 #ifdef MSDOSFS_DEBUG
940 printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
941 #endif
942 if ((error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep)) != 0)
943 return (error);
944 *vpp = DETOV(ndep);
945 return (0);
946 }
947
948 int
949 msdosfs_statvfs(struct mount *mp, struct statvfs *sbp)
950 {
951 struct msdosfsmount *pmp;
952
953 pmp = VFSTOMSDOSFS(mp);
954 sbp->f_bsize = pmp->pm_bpcluster;
955 sbp->f_frsize = sbp->f_bsize;
956 sbp->f_iosize = pmp->pm_bpcluster;
957 sbp->f_blocks = pmp->pm_nmbrofclusters;
958 sbp->f_bfree = pmp->pm_freeclustercount;
959 sbp->f_bavail = pmp->pm_freeclustercount;
960 sbp->f_bresvd = 0;
961 sbp->f_files = pmp->pm_RootDirEnts; /* XXX */
962 sbp->f_ffree = 0; /* what to put in here? */
963 sbp->f_favail = 0; /* what to put in here? */
964 sbp->f_fresvd = 0;
965 copy_statvfs_info(sbp, mp);
966 return (0);
967 }
968
969 int
970 msdosfs_sync(mp, waitfor, cred)
971 struct mount *mp;
972 int waitfor;
973 kauth_cred_t cred;
974 {
975 struct vnode *vp, *mvp;
976 struct denode *dep;
977 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
978 int error, allerror = 0;
979
980 /*
981 * If we ever switch to not updating all of the fats all the time,
982 * this would be the place to update them from the first one.
983 */
984 if (pmp->pm_fmod != 0) {
985 if (pmp->pm_flags & MSDOSFSMNT_RONLY)
986 panic("msdosfs_sync: rofs mod");
987 else {
988 /* update fats here */
989 }
990 }
991 /* Allocate a marker vnode. */
992 if ((mvp = vnalloc(mp)) == NULL)
993 return ENOMEM;
994 /*
995 * Write back each (modified) denode.
996 */
997 mutex_enter(&mntvnode_lock);
998 loop:
999 for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = vunmark(mvp)) {
1000 vmark(mvp, vp);
1001 if (vp->v_mount != mp || vismarker(vp))
1002 continue;
1003 mutex_enter(&vp->v_interlock);
1004 dep = VTODE(vp);
1005 if (waitfor == MNT_LAZY || vp->v_type == VNON ||
1006 (((dep->de_flag &
1007 (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0) &&
1008 (LIST_EMPTY(&vp->v_dirtyblkhd) &&
1009 UVM_OBJ_IS_CLEAN(&vp->v_uobj)))) {
1010 mutex_exit(&vp->v_interlock);
1011 continue;
1012 }
1013 mutex_exit(&mntvnode_lock);
1014 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
1015 if (error) {
1016 mutex_enter(&mntvnode_lock);
1017 if (error == ENOENT) {
1018 (void)vunmark(mvp);
1019 goto loop;
1020 }
1021 continue;
1022 }
1023 if ((error = VOP_FSYNC(vp, cred,
1024 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0)) != 0)
1025 allerror = error;
1026 vput(vp);
1027 mutex_enter(&mntvnode_lock);
1028 }
1029 mutex_exit(&mntvnode_lock);
1030 vnfree(mvp);
1031
1032 /*
1033 * Force stale file system control information to be flushed.
1034 */
1035 if ((error = VOP_FSYNC(pmp->pm_devvp, cred,
1036 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0)) != 0)
1037 allerror = error;
1038 return (allerror);
1039 }
1040
1041 int
1042 msdosfs_fhtovp(mp, fhp, vpp)
1043 struct mount *mp;
1044 struct fid *fhp;
1045 struct vnode **vpp;
1046 {
1047 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
1048 struct defid defh;
1049 struct denode *dep;
1050 int error;
1051
1052 if (fhp->fid_len != sizeof(struct defid)) {
1053 DPRINTF(("fid_len %d %zd\n", fhp->fid_len,
1054 sizeof(struct defid)));
1055 return EINVAL;
1056 }
1057
1058 memcpy(&defh, fhp, sizeof(defh));
1059 error = deget(pmp, defh.defid_dirclust, defh.defid_dirofs, &dep);
1060 if (error) {
1061 DPRINTF(("deget %d\n", error));
1062 *vpp = NULLVP;
1063 return (error);
1064 }
1065 *vpp = DETOV(dep);
1066 return (0);
1067 }
1068
1069 int
1070 msdosfs_vptofh(vp, fhp, fh_size)
1071 struct vnode *vp;
1072 struct fid *fhp;
1073 size_t *fh_size;
1074 {
1075 struct denode *dep;
1076 struct defid defh;
1077
1078 if (*fh_size < sizeof(struct defid)) {
1079 *fh_size = sizeof(struct defid);
1080 return E2BIG;
1081 }
1082 *fh_size = sizeof(struct defid);
1083 dep = VTODE(vp);
1084 memset(&defh, 0, sizeof(defh));
1085 defh.defid_len = sizeof(struct defid);
1086 defh.defid_dirclust = dep->de_dirclust;
1087 defh.defid_dirofs = dep->de_diroffset;
1088 /* defh.defid_gen = dep->de_gen; */
1089 memcpy(fhp, &defh, sizeof(defh));
1090 return (0);
1091 }
1092
1093 int
1094 msdosfs_vget(struct mount *mp, ino_t ino,
1095 struct vnode **vpp)
1096 {
1097
1098 return (EOPNOTSUPP);
1099 }
1100