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