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