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