msdosfs_vfsops.c revision 1.93.6.4 1 /* $NetBSD: msdosfs_vfsops.c,v 1.93.6.4 2017/10/13 08:05:30 snj 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.93.6.4 2017/10/13 08:05:30 snj 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/fstrans.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, msdos, NULL);
89
90 #ifdef MSDOSFS_DEBUG
91 #define DPRINTF(a) uprintf a
92 #else
93 #define DPRINTF(a)
94 #endif
95
96 #define GEMDOSFS_BSIZE 512
97
98 #define MSDOSFS_NAMEMAX(pmp) \
99 (pmp)->pm_flags & MSDOSFSMNT_LONGNAME ? WIN_MAXLEN : 12
100
101 VFS_PROTOS(msdosfs);
102
103 int msdosfs_mountfs(struct vnode *, struct mount *, struct lwp *,
104 struct msdosfs_args *);
105
106 static int update_mp(struct mount *, struct msdosfs_args *);
107
108 MALLOC_JUSTDEFINE(M_MSDOSFSMNT, "MSDOSFS mount", "MSDOS FS mount structure");
109 MALLOC_JUSTDEFINE(M_MSDOSFSFAT, "MSDOSFS fat", "MSDOS FS fat table");
110 MALLOC_JUSTDEFINE(M_MSDOSFSTMP, "MSDOSFS temp", "MSDOS FS temp. structures");
111
112 #define ROOTNAME "root_device"
113
114 static struct sysctllog *msdosfs_sysctl_log;
115
116 extern const struct vnodeopv_desc msdosfs_vnodeop_opv_desc;
117
118 const struct vnodeopv_desc * const msdosfs_vnodeopv_descs[] = {
119 &msdosfs_vnodeop_opv_desc,
120 NULL,
121 };
122
123 struct vfsops msdosfs_vfsops = {
124 MOUNT_MSDOS,
125 sizeof (struct msdosfs_args),
126 msdosfs_mount,
127 msdosfs_start,
128 msdosfs_unmount,
129 msdosfs_root,
130 (void *)eopnotsupp, /* vfs_quotactl */
131 msdosfs_statvfs,
132 msdosfs_sync,
133 msdosfs_vget,
134 msdosfs_fhtovp,
135 msdosfs_vptofh,
136 msdosfs_init,
137 msdosfs_reinit,
138 msdosfs_done,
139 msdosfs_mountroot,
140 (int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
141 vfs_stdextattrctl,
142 msdosfs_suspendctl,
143 genfs_renamelock_enter,
144 genfs_renamelock_exit,
145 (void *)eopnotsupp,
146 msdosfs_vnodeopv_descs,
147 0,
148 { NULL, NULL },
149 };
150
151 static int
152 msdos_modcmd(modcmd_t cmd, void *arg)
153 {
154 int error;
155
156 switch (cmd) {
157 case MODULE_CMD_INIT:
158 error = vfs_attach(&msdosfs_vfsops);
159 if (error != 0)
160 break;
161 sysctl_createv(&msdosfs_sysctl_log, 0, NULL, NULL,
162 CTLFLAG_PERMANENT,
163 CTLTYPE_NODE, "vfs", NULL,
164 NULL, 0, NULL, 0,
165 CTL_VFS, CTL_EOL);
166 sysctl_createv(&msdosfs_sysctl_log, 0, NULL, NULL,
167 CTLFLAG_PERMANENT,
168 CTLTYPE_NODE, "msdosfs",
169 SYSCTL_DESCR("MS-DOS file system"),
170 NULL, 0, NULL, 0,
171 CTL_VFS, 4, CTL_EOL);
172 /*
173 * XXX the "4" above could be dynamic, thereby eliminating one
174 * more instance of the "number to vfs" mapping problem, but
175 * "4" is the order as taken from sys/mount.h
176 */
177 break;
178 case MODULE_CMD_FINI:
179 error = vfs_detach(&msdosfs_vfsops);
180 if (error != 0)
181 break;
182 sysctl_teardown(&msdosfs_sysctl_log);
183 break;
184 default:
185 error = ENOTTY;
186 break;
187 }
188
189 return (error);
190 }
191
192 static int
193 update_mp(struct mount *mp, 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(void)
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(struct mount *mp, const char *path, void *data, size_t *data_len)
289 {
290 struct lwp *l = curlwp;
291 struct vnode *devvp; /* vnode for blk device to mount */
292 struct msdosfs_args *args = data; /* holds data from mount request */
293 /* msdosfs specific mount control block */
294 struct msdosfsmount *pmp = NULL;
295 int error, flags;
296 mode_t accessmode;
297
298 if (args == NULL)
299 return EINVAL;
300 if (*data_len < sizeof *args)
301 return EINVAL;
302
303 if (mp->mnt_flag & MNT_GETARGS) {
304 pmp = VFSTOMSDOSFS(mp);
305 if (pmp == NULL)
306 return EIO;
307 args->fspec = NULL;
308 args->uid = pmp->pm_uid;
309 args->gid = pmp->pm_gid;
310 args->mask = pmp->pm_mask;
311 args->flags = pmp->pm_flags;
312 args->version = MSDOSFSMNT_VERSION;
313 args->dirmask = pmp->pm_dirmask;
314 args->gmtoff = pmp->pm_gmtoff;
315 *data_len = sizeof *args;
316 return 0;
317 }
318
319 /*
320 * If not versioned (i.e. using old mount_msdos(8)), fill in
321 * the additional structure items with suitable defaults.
322 */
323 if ((args->flags & MSDOSFSMNT_VERSIONED) == 0) {
324 args->version = 1;
325 args->dirmask = args->mask;
326 }
327
328 /*
329 * Reset GMT offset for pre-v3 mount structure args.
330 */
331 if (args->version < 3)
332 args->gmtoff = 0;
333
334 /*
335 * If updating, check whether changing from read-only to
336 * read/write; if there is no device name, that's all we do.
337 */
338 if (mp->mnt_flag & MNT_UPDATE) {
339 pmp = VFSTOMSDOSFS(mp);
340 error = 0;
341 if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) &&
342 (mp->mnt_flag & MNT_RDONLY)) {
343 flags = WRITECLOSE;
344 if (mp->mnt_flag & MNT_FORCE)
345 flags |= FORCECLOSE;
346 error = vflush(mp, NULLVP, flags);
347 }
348 if (!error && (mp->mnt_flag & MNT_RELOAD))
349 /* not yet implemented */
350 error = EOPNOTSUPP;
351 if (error) {
352 DPRINTF(("vflush %d\n", error));
353 return (error);
354 }
355 if ((pmp->pm_flags & MSDOSFSMNT_RONLY) &&
356 (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 * Permission to update a mount is checked higher, so
362 * here we presume updating the mount is okay (for
363 * example, as far as securelevel goes) which leaves us
364 * with the normal check.
365 */
366 devvp = pmp->pm_devvp;
367 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
368 error = genfs_can_mount(devvp, VREAD | VWRITE,
369 l->l_cred);
370 VOP_UNLOCK(devvp);
371 DPRINTF(("genfs_can_mount %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 error = namei_simple_user(args->fspec,
387 NSM_FOLLOW_NOEMULROOT, &devvp);
388 if (error != 0) {
389 DPRINTF(("namei %d\n", error));
390 return (error);
391 }
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 accessmode = VREAD;
408 if ((mp->mnt_flag & MNT_RDONLY) == 0)
409 accessmode |= VWRITE;
410 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
411 error = genfs_can_mount(devvp, accessmode, l->l_cred);
412 VOP_UNLOCK(devvp);
413 if (error) {
414 DPRINTF(("genfs_can_mount %d\n", error));
415 vrele(devvp);
416 return (error);
417 }
418 if ((mp->mnt_flag & MNT_UPDATE) == 0) {
419 int xflags;
420
421 if (mp->mnt_flag & MNT_RDONLY)
422 xflags = FREAD;
423 else
424 xflags = FREAD|FWRITE;
425 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
426 error = VOP_OPEN(devvp, xflags, FSCRED);
427 VOP_UNLOCK(devvp);
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);
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(struct vnode *devvp, struct mount *mp, struct lwp *l, struct msdosfs_args *argp)
470 {
471 struct msdosfsmount *pmp;
472 struct buf *bp;
473 dev_t dev = devvp->v_rdev;
474 union bootsector *bsp;
475 struct byte_bpb33 *b33;
476 struct byte_bpb50 *b50;
477 struct byte_bpb710 *b710;
478 uint8_t SecPerClust;
479 int ronly, error, BlkPerSec;
480 uint64_t psize;
481 unsigned secsize;
482 u_long fatbytes, fatblocksecs;
483
484 /* Flush out any old buffers remaining from a previous use. */
485 if ((error = vinvalbuf(devvp, V_SAVE, l->l_cred, l, 0, 0)) != 0)
486 return (error);
487
488 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
489
490 bp = NULL; /* both used in error_exit */
491 pmp = NULL;
492
493 error = fstrans_mount(mp);
494 if (error)
495 goto error_exit;
496
497 error = getdisksize(devvp, &psize, &secsize);
498 if (error) {
499 if (argp->flags & MSDOSFSMNT_GEMDOSFS)
500 goto error_exit;
501
502 /* ok, so it failed. we most likely don't need the info */
503 secsize = DEV_BSIZE;
504 psize = 0;
505 error = 0;
506 }
507 if (secsize < DEV_BSIZE) {
508 DPRINTF(("Invalid block secsize (%d < DEV_BSIZE)", secsize));
509 error = EINVAL;
510 goto error_exit;
511 }
512
513 if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
514 if (secsize != GEMDOSFS_BSIZE) {
515 DPRINTF(("Invalid block secsize %d for GEMDOS\n", secsize));
516 error = EINVAL;
517 goto error_exit;
518 }
519 }
520
521 /*
522 * Read the boot sector of the filesystem, and then check the
523 * boot signature. If not a dos boot sector then error out.
524 */
525 if ((error = bread(devvp, 0, secsize, NOCRED, 0, &bp)) != 0)
526 goto error_exit;
527 bsp = (union bootsector *)bp->b_data;
528 b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
529 b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
530 b710 = (struct byte_bpb710 *)bsp->bs710.bsBPB;
531
532 if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
533 if (bsp->bs50.bsBootSectSig0 != BOOTSIG0
534 || bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
535 DPRINTF(("bootsig0 %d bootsig1 %d\n",
536 bsp->bs50.bsBootSectSig0,
537 bsp->bs50.bsBootSectSig1));
538 error = EINVAL;
539 goto error_exit;
540 }
541 }
542
543 pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK);
544 memset(pmp, 0, sizeof *pmp);
545 pmp->pm_mountp = mp;
546
547 /*
548 * Compute several useful quantities from the bpb in the
549 * bootsector. Copy in the dos 5 variant of the bpb then fix up
550 * the fields that are different between dos 5 and dos 3.3.
551 */
552 SecPerClust = b50->bpbSecPerClust;
553 pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
554 pmp->pm_ResSectors = getushort(b50->bpbResSectors);
555 pmp->pm_FATs = b50->bpbFATs;
556 pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
557 pmp->pm_Sectors = getushort(b50->bpbSectors);
558 pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
559 pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
560 pmp->pm_Heads = getushort(b50->bpbHeads);
561 pmp->pm_Media = b50->bpbMedia;
562
563 if (pmp->pm_Sectors == 0) {
564 pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
565 pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
566 } else {
567 pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
568 pmp->pm_HugeSectors = pmp->pm_Sectors;
569 }
570
571 /*
572 * Sanity checks, from the FAT specification:
573 * - sectors per cluster: >= 1, power of 2
574 * - logical sector size: >= 1, power of 2
575 * - cluster size: <= max FS block size
576 * - number of sectors: >= 1
577 */
578 if ((SecPerClust == 0) || !powerof2(SecPerClust) ||
579 (pmp->pm_BytesPerSec == 0) || !powerof2(pmp->pm_BytesPerSec) ||
580 (SecPerClust * pmp->pm_BytesPerSec > MAXBSIZE) ||
581 (pmp->pm_HugeSectors == 0)) {
582 DPRINTF(("consistency checks\n"));
583 error = EINVAL;
584 goto error_exit;
585 }
586
587 if (!(argp->flags & MSDOSFSMNT_GEMDOSFS) &&
588 (pmp->pm_SecPerTrack > 63)) {
589 DPRINTF(("SecPerTrack %d\n", pmp->pm_SecPerTrack));
590 error = EINVAL;
591 goto error_exit;
592 }
593
594 if (pmp->pm_RootDirEnts == 0) {
595 unsigned short vers = getushort(b710->bpbFSVers);
596 /*
597 * Some say that bsBootSectSig[23] must be zero, but
598 * Windows does not require this and some digital cameras
599 * do not set these to zero. Therefore, do not insist.
600 */
601 if (pmp->pm_Sectors || pmp->pm_FATsecs || vers) {
602 DPRINTF(("sectors %d fatsecs %lu vers %d\n",
603 pmp->pm_Sectors, pmp->pm_FATsecs, vers));
604 error = EINVAL;
605 goto error_exit;
606 }
607 pmp->pm_fatmask = FAT32_MASK;
608 pmp->pm_fatmult = 4;
609 pmp->pm_fatdiv = 1;
610 pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
611
612 /* mirrorring is enabled if the FATMIRROR bit is not set */
613 if ((getushort(b710->bpbExtFlags) & FATMIRROR) == 0)
614 pmp->pm_flags |= MSDOSFS_FATMIRROR;
615 else
616 pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
617 } else
618 pmp->pm_flags |= MSDOSFS_FATMIRROR;
619
620 if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
621 if (FAT32(pmp)) {
622 DPRINTF(("fat32 for gemdos\n"));
623 /*
624 * GEMDOS doesn't know fat32.
625 */
626 error = EINVAL;
627 goto error_exit;
628 }
629
630 /*
631 * Check a few values (could do some more):
632 * - logical sector size: >= block size
633 * - number of sectors: <= size of partition
634 */
635 if ((pmp->pm_BytesPerSec < GEMDOSFS_BSIZE) ||
636 (pmp->pm_HugeSectors *
637 (pmp->pm_BytesPerSec / GEMDOSFS_BSIZE) > psize)) {
638 DPRINTF(("consistency checks for gemdos\n"));
639 error = EINVAL;
640 goto error_exit;
641 }
642 /*
643 * XXX - Many parts of the msdos fs driver seem to assume that
644 * the number of bytes per logical sector (BytesPerSec) will
645 * always be the same as the number of bytes per disk block
646 * Let's pretend it is.
647 */
648 BlkPerSec = pmp->pm_BytesPerSec / GEMDOSFS_BSIZE;
649 pmp->pm_BytesPerSec = GEMDOSFS_BSIZE;
650 pmp->pm_HugeSectors *= BlkPerSec;
651 pmp->pm_HiddenSects *= BlkPerSec;
652 pmp->pm_ResSectors *= BlkPerSec;
653 pmp->pm_Sectors *= BlkPerSec;
654 pmp->pm_FATsecs *= BlkPerSec;
655 SecPerClust *= BlkPerSec;
656 }
657
658 /* Check that fs has nonzero FAT size */
659 if (pmp->pm_FATsecs == 0) {
660 DPRINTF(("FATsecs is 0\n"));
661 error = EINVAL;
662 goto error_exit;
663 }
664
665 pmp->pm_fatblk = pmp->pm_ResSectors;
666 if (FAT32(pmp)) {
667 pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
668 pmp->pm_firstcluster = pmp->pm_fatblk
669 + (pmp->pm_FATs * pmp->pm_FATsecs);
670 pmp->pm_fsinfo = getushort(b710->bpbFSInfo);
671 } else {
672 pmp->pm_rootdirblk = pmp->pm_fatblk +
673 (pmp->pm_FATs * pmp->pm_FATsecs);
674 pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
675 + pmp->pm_BytesPerSec - 1)
676 / pmp->pm_BytesPerSec;/* in sectors */
677 pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
678 }
679
680 pmp->pm_nmbrofclusters = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
681 SecPerClust;
682 pmp->pm_maxcluster = pmp->pm_nmbrofclusters + 1;
683 pmp->pm_fatsize = pmp->pm_FATsecs * pmp->pm_BytesPerSec;
684
685 if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
686 if (pmp->pm_nmbrofclusters <= (0xff0 - 2)) {
687 pmp->pm_fatmask = FAT12_MASK;
688 pmp->pm_fatmult = 3;
689 pmp->pm_fatdiv = 2;
690 } else {
691 pmp->pm_fatmask = FAT16_MASK;
692 pmp->pm_fatmult = 2;
693 pmp->pm_fatdiv = 1;
694 }
695 } else if (pmp->pm_fatmask == 0) {
696 if (pmp->pm_maxcluster
697 <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
698 /*
699 * This will usually be a floppy disk. This size makes
700 * sure that one fat entry will not be split across
701 * multiple blocks.
702 */
703 pmp->pm_fatmask = FAT12_MASK;
704 pmp->pm_fatmult = 3;
705 pmp->pm_fatdiv = 2;
706 } else {
707 pmp->pm_fatmask = FAT16_MASK;
708 pmp->pm_fatmult = 2;
709 pmp->pm_fatdiv = 1;
710 }
711 }
712
713 /* validate cluster count against FAT */
714 if ((pmp->pm_maxcluster & pmp->pm_fatmask) != pmp->pm_maxcluster) {
715 DPRINTF("maxcluster %lu outside of mask %#lx\n",
716 pmp->pm_maxcluster, pmp->pm_fatmask);
717 error = EINVAL;
718 goto error_exit;
719 }
720
721 /* validate FAT size */
722 fatbytes = (pmp->pm_maxcluster+1) * pmp->pm_fatmult / pmp->pm_fatdiv;
723 fatblocksecs = howmany(fatbytes, pmp->pm_BytesPerSec);
724
725 if (pmp->pm_FATsecs != fatblocksecs) {
726 DPRINTF("FATsecs %lu != real %lu\n", pmp->pm_FATsecs,
727 fatblocksecs);
728 error = EINVAL;
729 goto error_exit;
730 }
731
732 if (FAT12(pmp)) {
733 /*
734 * limit block size to what is needed to read a FAT block
735 * to not exceed MAXBSIZE
736 */
737 pmp->pm_fatblocksec = min(3, fatblocksecs);
738 pmp->pm_fatblocksize = pmp->pm_fatblocksec
739 * pmp->pm_BytesPerSec;
740 } else {
741 pmp->pm_fatblocksize = MAXBSIZE;
742 pmp->pm_fatblocksec = pmp->pm_fatblocksize
743 / pmp->pm_BytesPerSec;
744 }
745
746 pmp->pm_bnshift = ffs(pmp->pm_BytesPerSec) - 1;
747
748 /*
749 * Compute mask and shift value for isolating cluster relative byte
750 * offsets and cluster numbers from a file offset.
751 */
752 pmp->pm_bpcluster = SecPerClust * pmp->pm_BytesPerSec;
753 pmp->pm_crbomask = pmp->pm_bpcluster - 1;
754 pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
755
756 /*
757 * Check for valid cluster size
758 * must be a power of 2
759 */
760 if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
761 DPRINTF(("bpcluster %lu cnshift %lu\n",
762 pmp->pm_bpcluster, pmp->pm_cnshift));
763 error = EINVAL;
764 goto error_exit;
765 }
766
767 /*
768 * Release the bootsector buffer.
769 */
770 brelse(bp, BC_AGE);
771 bp = NULL;
772
773 /*
774 * Check FSInfo.
775 */
776 if (pmp->pm_fsinfo) {
777 struct fsinfo *fp;
778
779 /*
780 * XXX If the fsinfo block is stored on media with
781 * 2KB or larger sectors, is the fsinfo structure
782 * padded at the end or in the middle?
783 */
784 if ((error = bread(devvp, de_bn2kb(pmp, pmp->pm_fsinfo),
785 pmp->pm_BytesPerSec, NOCRED, 0, &bp)) != 0)
786 goto error_exit;
787 fp = (struct fsinfo *)bp->b_data;
788 if (!memcmp(fp->fsisig1, "RRaA", 4)
789 && !memcmp(fp->fsisig2, "rrAa", 4)
790 && !memcmp(fp->fsisig3, "\0\0\125\252", 4)
791 && !memcmp(fp->fsisig4, "\0\0\125\252", 4))
792 pmp->pm_nxtfree = getulong(fp->fsinxtfree);
793 else
794 pmp->pm_fsinfo = 0;
795 brelse(bp, 0);
796 bp = NULL;
797 }
798
799 /*
800 * Check and validate (or perhaps invalidate?) the fsinfo structure?
801 * XXX
802 */
803 if (pmp->pm_fsinfo) {
804 if (pmp->pm_nxtfree == (u_long)-1)
805 pmp->pm_fsinfo = 0;
806 }
807
808 /*
809 * Allocate memory for the bitmap of allocated clusters, and then
810 * fill it in.
811 */
812 pmp->pm_inusemap = malloc(((pmp->pm_maxcluster + N_INUSEBITS - 1)
813 / N_INUSEBITS)
814 * sizeof(*pmp->pm_inusemap),
815 M_MSDOSFSFAT, M_WAITOK);
816
817 /*
818 * fillinusemap() needs pm_devvp.
819 */
820 pmp->pm_dev = dev;
821 pmp->pm_devvp = devvp;
822
823 /*
824 * Have the inuse map filled in.
825 */
826 if ((error = fillinusemap(pmp)) != 0) {
827 DPRINTF(("fillinusemap %d\n", error));
828 goto error_exit;
829 }
830
831 /*
832 * If they want fat updates to be synchronous then let them suffer
833 * the performance degradation in exchange for the on disk copy of
834 * the fat being correct just about all the time. I suppose this
835 * would be a good thing to turn on if the kernel is still flakey.
836 */
837 if (mp->mnt_flag & MNT_SYNCHRONOUS)
838 pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
839
840 /*
841 * Finish up.
842 */
843 if (ronly)
844 pmp->pm_flags |= MSDOSFSMNT_RONLY;
845 else
846 pmp->pm_fmod = 1;
847 mp->mnt_data = pmp;
848 mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
849 mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_MSDOS);
850 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
851 mp->mnt_stat.f_namemax = MSDOSFS_NAMEMAX(pmp);
852 mp->mnt_flag |= MNT_LOCAL;
853 mp->mnt_dev_bshift = pmp->pm_bnshift;
854 mp->mnt_fs_bshift = pmp->pm_cnshift;
855
856 /*
857 * If we ever do quotas for DOS filesystems this would be a place
858 * to fill in the info in the msdosfsmount structure. You dolt,
859 * quotas on dos filesystems make no sense because files have no
860 * owners on dos filesystems. of course there is some empty space
861 * in the directory entry where we could put uid's and gid's.
862 */
863
864 devvp->v_specmountpoint = mp;
865
866 return (0);
867
868 error_exit:
869 fstrans_unmount(mp);
870 if (bp)
871 brelse(bp, BC_AGE);
872 if (pmp) {
873 if (pmp->pm_inusemap)
874 free(pmp->pm_inusemap, M_MSDOSFSFAT);
875 free(pmp, M_MSDOSFSMNT);
876 mp->mnt_data = NULL;
877 }
878 return (error);
879 }
880
881 int
882 msdosfs_start(struct mount *mp, int flags)
883 {
884
885 return (0);
886 }
887
888 /*
889 * Unmount the filesystem described by mp.
890 */
891 int
892 msdosfs_unmount(struct mount *mp, int mntflags)
893 {
894 struct msdosfsmount *pmp;
895 int error, flags;
896
897 flags = 0;
898 if (mntflags & MNT_FORCE)
899 flags |= FORCECLOSE;
900 if ((error = vflush(mp, NULLVP, flags)) != 0)
901 return (error);
902 pmp = VFSTOMSDOSFS(mp);
903 if (pmp->pm_devvp->v_type != VBAD)
904 pmp->pm_devvp->v_specmountpoint = NULL;
905 #ifdef MSDOSFS_DEBUG
906 {
907 struct vnode *vp = pmp->pm_devvp;
908
909 printf("msdosfs_umount(): just before calling VOP_CLOSE()\n");
910 printf("flag %08x, usecount %d, writecount %d, holdcnt %d\n",
911 vp->v_vflag | vp->v_iflag | vp->v_uflag, vp->v_usecount,
912 vp->v_writecount, vp->v_holdcnt);
913 printf("mount %p, op %p\n",
914 vp->v_mount, vp->v_op);
915 printf("freef %p, freeb %p, mount %p\n",
916 vp->v_freelist.tqe_next, vp->v_freelist.tqe_prev,
917 vp->v_mount);
918 printf("cleanblkhd %p, dirtyblkhd %p, numoutput %d, type %d\n",
919 vp->v_cleanblkhd.lh_first,
920 vp->v_dirtyblkhd.lh_first,
921 vp->v_numoutput, vp->v_type);
922 printf("union %p, tag %d, data[0] %08x, data[1] %08x\n",
923 vp->v_socket, vp->v_tag,
924 ((u_int *)vp->v_data)[0],
925 ((u_int *)vp->v_data)[1]);
926 }
927 #endif
928 vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY);
929 (void) VOP_CLOSE(pmp->pm_devvp,
930 pmp->pm_flags & MSDOSFSMNT_RONLY ? FREAD : FREAD|FWRITE, NOCRED);
931 vput(pmp->pm_devvp);
932 msdosfs_fh_destroy(pmp);
933 free(pmp->pm_inusemap, M_MSDOSFSFAT);
934 free(pmp, M_MSDOSFSMNT);
935 mp->mnt_data = NULL;
936 mp->mnt_flag &= ~MNT_LOCAL;
937 fstrans_unmount(mp);
938 return (0);
939 }
940
941 int
942 msdosfs_root(struct mount *mp, struct vnode **vpp)
943 {
944 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
945 struct denode *ndep;
946 int error;
947
948 #ifdef MSDOSFS_DEBUG
949 printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
950 #endif
951 if ((error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep)) != 0)
952 return (error);
953 *vpp = DETOV(ndep);
954 return (0);
955 }
956
957 int
958 msdosfs_statvfs(struct mount *mp, struct statvfs *sbp)
959 {
960 struct msdosfsmount *pmp;
961
962 pmp = VFSTOMSDOSFS(mp);
963 sbp->f_bsize = pmp->pm_bpcluster;
964 sbp->f_frsize = sbp->f_bsize;
965 sbp->f_iosize = pmp->pm_bpcluster;
966 sbp->f_blocks = pmp->pm_nmbrofclusters;
967 sbp->f_bfree = pmp->pm_freeclustercount;
968 sbp->f_bavail = pmp->pm_freeclustercount;
969 sbp->f_bresvd = 0;
970 sbp->f_files = pmp->pm_RootDirEnts; /* XXX */
971 sbp->f_ffree = 0; /* what to put in here? */
972 sbp->f_favail = 0; /* what to put in here? */
973 sbp->f_fresvd = 0;
974 copy_statvfs_info(sbp, mp);
975 return (0);
976 }
977
978 int
979 msdosfs_sync(struct mount *mp, int waitfor, 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 mvp = vnalloc(mp);
999 fstrans_start(mp, FSTRANS_SHARED);
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 == NULL || (((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);
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 fstrans_done(mp);
1045 return (allerror);
1046 }
1047
1048 int
1049 msdosfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
1050 {
1051 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
1052 struct defid defh;
1053 struct denode *dep;
1054 uint32_t gen;
1055 int error;
1056
1057 if (fhp->fid_len != sizeof(struct defid)) {
1058 DPRINTF(("fid_len %d %zd\n", fhp->fid_len,
1059 sizeof(struct defid)));
1060 return EINVAL;
1061 }
1062 memcpy(&defh, fhp, sizeof(defh));
1063 error = msdosfs_fh_lookup(pmp, defh.defid_dirclust, defh.defid_dirofs,
1064 &gen);
1065 if (error == 0 && gen != defh.defid_gen)
1066 error = ESTALE;
1067 if (error) {
1068 *vpp = NULLVP;
1069 return error;
1070 }
1071 error = deget(pmp, defh.defid_dirclust, defh.defid_dirofs, &dep);
1072 if (error) {
1073 DPRINTF(("deget %d\n", error));
1074 *vpp = NULLVP;
1075 return (error);
1076 }
1077 *vpp = DETOV(dep);
1078 return (0);
1079 }
1080
1081 int
1082 msdosfs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
1083 {
1084 struct msdosfsmount *pmp = VFSTOMSDOSFS(vp->v_mount);
1085 struct denode *dep;
1086 struct defid defh;
1087 int error;
1088
1089 if (*fh_size < sizeof(struct defid)) {
1090 *fh_size = sizeof(struct defid);
1091 return E2BIG;
1092 }
1093 *fh_size = sizeof(struct defid);
1094 dep = VTODE(vp);
1095 memset(&defh, 0, sizeof(defh));
1096 defh.defid_len = sizeof(struct defid);
1097 defh.defid_dirclust = dep->de_dirclust;
1098 defh.defid_dirofs = dep->de_diroffset;
1099 error = msdosfs_fh_enter(pmp, dep->de_dirclust, dep->de_diroffset,
1100 &defh.defid_gen);
1101 if (error == 0)
1102 memcpy(fhp, &defh, sizeof(defh));
1103 return error;
1104 }
1105
1106 int
1107 msdosfs_vget(struct mount *mp, ino_t ino,
1108 struct vnode **vpp)
1109 {
1110
1111 return (EOPNOTSUPP);
1112 }
1113
1114 int
1115 msdosfs_suspendctl(struct mount *mp, int cmd)
1116 {
1117 int error;
1118 struct lwp *l = curlwp;
1119
1120 switch (cmd) {
1121 case SUSPEND_SUSPEND:
1122 if ((error = fstrans_setstate(mp, FSTRANS_SUSPENDING)) != 0)
1123 return error;
1124 error = msdosfs_sync(mp, MNT_WAIT, l->l_proc->p_cred);
1125 if (error == 0)
1126 error = fstrans_setstate(mp, FSTRANS_SUSPENDED);
1127 if (error != 0) {
1128 (void) fstrans_setstate(mp, FSTRANS_NORMAL);
1129 return error;
1130 }
1131 return 0;
1132
1133 case SUSPEND_RESUME:
1134 return fstrans_setstate(mp, FSTRANS_NORMAL);
1135
1136 default:
1137 return EINVAL;
1138 }
1139 }
1140