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