msdosfs_vfsops.c revision 1.109 1 /* $NetBSD: msdosfs_vfsops.c,v 1.109 2014/07/08 09:21:52 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.109 2014/07/08 09:21:52 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/fstrans.h>
73 #include <sys/ioctl.h>
74 #include <sys/malloc.h>
75 #include <sys/dirent.h>
76 #include <sys/stat.h>
77 #include <sys/conf.h>
78 #include <sys/kauth.h>
79 #include <sys/module.h>
80
81 #include <fs/msdosfs/bpb.h>
82 #include <fs/msdosfs/bootsect.h>
83 #include <fs/msdosfs/direntry.h>
84 #include <fs/msdosfs/denode.h>
85 #include <fs/msdosfs/msdosfsmount.h>
86 #include <fs/msdosfs/fat.h>
87
88 MODULE(MODULE_CLASS_VFS, msdos, NULL);
89
90 #ifdef MSDOSFS_DEBUG
91 #define DPRINTF(a) uprintf a
92 #else
93 #define DPRINTF(a)
94 #endif
95
96 #define MSDOSFS_NAMEMAX(pmp) \
97 (pmp)->pm_flags & MSDOSFSMNT_LONGNAME ? WIN_MAXLEN : 12
98
99 int msdosfs_mountfs(struct vnode *, struct mount *, struct lwp *,
100 struct msdosfs_args *);
101
102 static int update_mp(struct mount *, struct msdosfs_args *);
103
104 MALLOC_JUSTDEFINE(M_MSDOSFSMNT, "MSDOSFS mount", "MSDOS FS mount structure");
105 MALLOC_JUSTDEFINE(M_MSDOSFSFAT, "MSDOSFS FAT", "MSDOS FS FAT table");
106 MALLOC_JUSTDEFINE(M_MSDOSFSTMP, "MSDOSFS temp", "MSDOS FS temp. structures");
107
108 #define ROOTNAME "root_device"
109
110 static struct sysctllog *msdosfs_sysctl_log;
111
112 extern const struct vnodeopv_desc msdosfs_vnodeop_opv_desc;
113
114 const struct vnodeopv_desc * const msdosfs_vnodeopv_descs[] = {
115 &msdosfs_vnodeop_opv_desc,
116 NULL,
117 };
118
119 struct vfsops msdosfs_vfsops = {
120 .vfs_name = MOUNT_MSDOS,
121 .vfs_min_mount_data = sizeof (struct msdosfs_args),
122 .vfs_mount = msdosfs_mount,
123 .vfs_start = msdosfs_start,
124 .vfs_unmount = msdosfs_unmount,
125 .vfs_root = msdosfs_root,
126 .vfs_quotactl = (void *)eopnotsupp,
127 .vfs_statvfs = msdosfs_statvfs,
128 .vfs_sync = msdosfs_sync,
129 .vfs_vget = msdosfs_vget,
130 .vfs_loadvnode = msdosfs_loadvnode,
131 .vfs_fhtovp = msdosfs_fhtovp,
132 .vfs_vptofh = msdosfs_vptofh,
133 .vfs_init = msdosfs_init,
134 .vfs_reinit = msdosfs_reinit,
135 .vfs_done = msdosfs_done,
136 .vfs_mountroot = msdosfs_mountroot,
137 .vfs_snapshot = (void *)eopnotsupp,
138 .vfs_extattrctl = vfs_stdextattrctl,
139 .vfs_suspendctl = msdosfs_suspendctl,
140 .vfs_renamelock_enter = genfs_renamelock_enter,
141 .vfs_renamelock_exit = genfs_renamelock_exit,
142 .vfs_fsync = (void *)eopnotsupp,
143 .vfs_opv_descs = msdosfs_vnodeopv_descs
144 };
145
146 static int
147 msdos_modcmd(modcmd_t cmd, void *arg)
148 {
149 int error;
150
151 switch (cmd) {
152 case MODULE_CMD_INIT:
153 error = vfs_attach(&msdosfs_vfsops);
154 if (error != 0)
155 break;
156 sysctl_createv(&msdosfs_sysctl_log, 0, NULL, NULL,
157 CTLFLAG_PERMANENT,
158 CTLTYPE_NODE, "msdosfs",
159 SYSCTL_DESCR("MS-DOS file system"),
160 NULL, 0, NULL, 0,
161 CTL_VFS, 4, CTL_EOL);
162 /*
163 * XXX the "4" above could be dynamic, thereby eliminating one
164 * more instance of the "number to vfs" mapping problem, but
165 * "4" is the order as taken from sys/mount.h
166 */
167 break;
168 case MODULE_CMD_FINI:
169 error = vfs_detach(&msdosfs_vfsops);
170 if (error != 0)
171 break;
172 sysctl_teardown(&msdosfs_sysctl_log);
173 break;
174 default:
175 error = ENOTTY;
176 break;
177 }
178
179 return (error);
180 }
181
182 static int
183 update_mp(struct mount *mp, struct msdosfs_args *argp)
184 {
185 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
186 int error;
187
188 pmp->pm_gid = argp->gid;
189 pmp->pm_uid = argp->uid;
190 pmp->pm_mask = argp->mask & ALLPERMS;
191 pmp->pm_dirmask = argp->dirmask & ALLPERMS;
192 pmp->pm_gmtoff = argp->gmtoff;
193 pmp->pm_flags |= argp->flags & MSDOSFSMNT_MNTOPT;
194
195 /*
196 * GEMDOS knows nothing about win95 long filenames
197 */
198 if (pmp->pm_flags & MSDOSFSMNT_GEMDOSFS)
199 pmp->pm_flags |= MSDOSFSMNT_NOWIN95;
200
201 if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
202 pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
203 else if (!(pmp->pm_flags &
204 (MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) {
205 struct vnode *rtvp;
206
207 /*
208 * Try to divine whether to support Win'95 long filenames
209 */
210 if (FAT32(pmp))
211 pmp->pm_flags |= MSDOSFSMNT_LONGNAME;
212 else {
213 if ((error = msdosfs_root(mp, &rtvp)) != 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, false, NULL);
252 vfs_destroy(mp);
253 return (error);
254 }
255
256 if ((error = update_mp(mp, &args)) != 0) {
257 (void)msdosfs_unmount(mp, 0);
258 vfs_unbusy(mp, false, NULL);
259 vfs_destroy(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, false, NULL);
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\n", 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\n", error));
361 if (error)
362 return (error);
363
364 pmp->pm_flags &= ~MSDOSFSMNT_RONLY;
365 }
366 if (args->fspec == NULL) {
367 DPRINTF(("missing fspec\n"));
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\n", error));
379 return (error);
380 }
381
382 if (devvp->v_type != VBLK) {
383 DPRINTF(("not block\n"));
384 vrele(devvp);
385 return (ENOTBLK);
386 }
387 if (bdevsw_lookup(devvp->v_rdev) == NULL) {
388 DPRINTF(("no block switch\n"));
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\n", 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\n", error));
420 goto fail;
421 }
422 error = msdosfs_mountfs(devvp, mp, l, args);
423 if (error) {
424 DPRINTF(("msdosfs_mountfs %d\n", 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\n",
437 devvp, pmp->pm_devvp));
438 return (EINVAL); /* needs translation */
439 }
440 }
441 if ((error = update_mp(mp, args)) != 0) {
442 msdosfs_unmount(mp, MNT_FORCE);
443 DPRINTF(("update_mp %d\n", error));
444 return error;
445 }
446
447 #ifdef MSDOSFS_DEBUG
448 printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap);
449 #endif
450 return set_statvfs_info(path, UIO_USERSPACE, args->fspec, UIO_USERSPACE,
451 mp->mnt_op->vfs_name, mp, l);
452
453 fail:
454 vrele(devvp);
455 return (error);
456 }
457
458 int
459 msdosfs_mountfs(struct vnode *devvp, struct mount *mp, struct lwp *l, struct msdosfs_args *argp)
460 {
461 struct msdosfsmount *pmp;
462 struct buf *bp;
463 dev_t dev = devvp->v_rdev;
464 union bootsector *bsp;
465 struct byte_bpb33 *b33;
466 struct byte_bpb50 *b50;
467 struct byte_bpb710 *b710;
468 uint8_t SecPerClust;
469 int ronly, error, tmp;
470 int bsize;
471 uint64_t psize;
472 unsigned secsize;
473
474 /* Flush out any old buffers remaining from a previous use. */
475 if ((error = vinvalbuf(devvp, V_SAVE, l->l_cred, l, 0, 0)) != 0)
476 return (error);
477
478 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
479
480 bp = NULL; /* both used in error_exit */
481 pmp = NULL;
482
483 error = fstrans_mount(mp);
484 if (error)
485 goto error_exit;
486
487 error = getdisksize(devvp, &psize, &secsize);
488 if (error) {
489 if (argp->flags & MSDOSFSMNT_GEMDOSFS)
490 goto error_exit;
491
492 /* ok, so it failed. we most likely don't need the info */
493 secsize = DEV_BSIZE;
494 psize = 0;
495 error = 0;
496 }
497
498 if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
499 bsize = secsize;
500 if (bsize != 512) {
501 DPRINTF(("Invalid block bsize %d for GEMDOS\n", bsize));
502 error = EINVAL;
503 goto error_exit;
504 }
505 } else
506 bsize = 0;
507
508 /*
509 * Read the boot sector of the filesystem, and then check the
510 * boot signature. If not a dos boot sector then error out.
511 */
512 if ((error = bread(devvp, 0, secsize, NOCRED, 0, &bp)) != 0)
513 goto error_exit;
514 bsp = (union bootsector *)bp->b_data;
515 b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
516 b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
517 b710 = (struct byte_bpb710 *)bsp->bs710.bsBPB;
518
519 if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
520 if (bsp->bs50.bsBootSectSig0 != BOOTSIG0
521 || bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
522 DPRINTF(("bootsig0 %d bootsig1 %d\n",
523 bsp->bs50.bsBootSectSig0,
524 bsp->bs50.bsBootSectSig1));
525 error = EINVAL;
526 goto error_exit;
527 }
528 }
529
530 pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK);
531 memset(pmp, 0, sizeof *pmp);
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 (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
551 /* XXX - We should probably check more values here */
552 if (!pmp->pm_BytesPerSec || !SecPerClust
553 || pmp->pm_SecPerTrack > 63) {
554 DPRINTF(("bytespersec %d secperclust %d "
555 "secpertrack %d\n",
556 pmp->pm_BytesPerSec, SecPerClust,
557 pmp->pm_SecPerTrack));
558 error = EINVAL;
559 goto error_exit;
560 }
561 }
562
563 if (pmp->pm_Sectors == 0) {
564 pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
565 pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
566 } else {
567 pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
568 pmp->pm_HugeSectors = pmp->pm_Sectors;
569 }
570
571 if (pmp->pm_RootDirEnts == 0) {
572 unsigned short vers = getushort(b710->bpbFSVers);
573 /*
574 * Some say that bsBootSectSig[23] must be zero, but
575 * Windows does not require this and some digital cameras
576 * do not set these to zero. Therefore, do not insist.
577 */
578 if (pmp->pm_Sectors || pmp->pm_FATsecs || vers) {
579 DPRINTF(("sectors %d fatsecs %lu vers %d\n",
580 pmp->pm_Sectors, pmp->pm_FATsecs, vers));
581 error = EINVAL;
582 goto error_exit;
583 }
584 pmp->pm_fatmask = FAT32_MASK;
585 pmp->pm_fatmult = 4;
586 pmp->pm_fatdiv = 1;
587 pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
588
589 /* mirrorring is enabled if the FATMIRROR bit is not set */
590 if ((getushort(b710->bpbExtFlags) & FATMIRROR) == 0)
591 pmp->pm_flags |= MSDOSFS_FATMIRROR;
592 else
593 pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
594 } else
595 pmp->pm_flags |= MSDOSFS_FATMIRROR;
596
597 if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
598 if (FAT32(pmp)) {
599 DPRINTF(("FAT32 for GEMDOS\n"));
600 /*
601 * GEMDOS doesn't know FAT32.
602 */
603 error = EINVAL;
604 goto error_exit;
605 }
606
607 /*
608 * Check a few values (could do some more):
609 * - logical sector size: power of 2, >= block size
610 * - sectors per cluster: power of 2, >= 1
611 * - number of sectors: >= 1, <= size of partition
612 */
613 if ( (SecPerClust == 0)
614 || (SecPerClust & (SecPerClust - 1))
615 || (pmp->pm_BytesPerSec < bsize)
616 || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
617 || (pmp->pm_HugeSectors == 0)
618 || (pmp->pm_HugeSectors * (pmp->pm_BytesPerSec / bsize)
619 > psize)) {
620 DPRINTF(("consistency checks for GEMDOS\n"));
621 error = EINVAL;
622 goto error_exit;
623 }
624 /*
625 * XXX - Many parts of the msdosfs driver seem to assume that
626 * the number of bytes per logical sector (BytesPerSec) will
627 * always be the same as the number of bytes per disk block
628 * Let's pretend it is.
629 */
630 tmp = pmp->pm_BytesPerSec / bsize;
631 pmp->pm_BytesPerSec = bsize;
632 pmp->pm_HugeSectors *= tmp;
633 pmp->pm_HiddenSects *= tmp;
634 pmp->pm_ResSectors *= tmp;
635 pmp->pm_Sectors *= tmp;
636 pmp->pm_FATsecs *= tmp;
637 SecPerClust *= tmp;
638 }
639
640 /* Check that fs has nonzero FAT size */
641 if (pmp->pm_FATsecs == 0) {
642 DPRINTF(("FATsecs is 0\n"));
643 error = EINVAL;
644 goto error_exit;
645 }
646
647 pmp->pm_fatblk = pmp->pm_ResSectors;
648 if (FAT32(pmp)) {
649 pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
650 pmp->pm_firstcluster = pmp->pm_fatblk
651 + (pmp->pm_FATs * pmp->pm_FATsecs);
652 pmp->pm_fsinfo = getushort(b710->bpbFSInfo);
653 } else {
654 pmp->pm_rootdirblk = pmp->pm_fatblk +
655 (pmp->pm_FATs * pmp->pm_FATsecs);
656 pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
657 + pmp->pm_BytesPerSec - 1)
658 / pmp->pm_BytesPerSec;/* in sectors */
659 pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
660 }
661
662 pmp->pm_nmbrofclusters = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
663 SecPerClust;
664 pmp->pm_maxcluster = pmp->pm_nmbrofclusters + 1;
665 pmp->pm_fatsize = pmp->pm_FATsecs * pmp->pm_BytesPerSec;
666
667 if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
668 if (pmp->pm_nmbrofclusters <= (0xff0 - 2)) {
669 pmp->pm_fatmask = FAT12_MASK;
670 pmp->pm_fatmult = 3;
671 pmp->pm_fatdiv = 2;
672 } else {
673 pmp->pm_fatmask = FAT16_MASK;
674 pmp->pm_fatmult = 2;
675 pmp->pm_fatdiv = 1;
676 }
677 } else if (pmp->pm_fatmask == 0) {
678 if (pmp->pm_maxcluster
679 <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
680 /*
681 * This will usually be a floppy disk. This size makes
682 * sure that one FAT entry will not be split across
683 * multiple blocks.
684 */
685 pmp->pm_fatmask = FAT12_MASK;
686 pmp->pm_fatmult = 3;
687 pmp->pm_fatdiv = 2;
688 } else {
689 pmp->pm_fatmask = FAT16_MASK;
690 pmp->pm_fatmult = 2;
691 pmp->pm_fatdiv = 1;
692 }
693 }
694 if (FAT12(pmp))
695 pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec;
696 else
697 pmp->pm_fatblocksize = MAXBSIZE;
698
699 pmp->pm_fatblocksec = pmp->pm_fatblocksize / pmp->pm_BytesPerSec;
700 pmp->pm_bnshift = ffs(pmp->pm_BytesPerSec) - 1;
701
702 /*
703 * Compute mask and shift value for isolating cluster relative byte
704 * offsets and cluster numbers from a file offset.
705 */
706 pmp->pm_bpcluster = SecPerClust * pmp->pm_BytesPerSec;
707 pmp->pm_crbomask = pmp->pm_bpcluster - 1;
708 pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
709
710 /*
711 * Check for valid cluster size
712 * must be a power of 2
713 */
714 if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
715 DPRINTF(("bpcluster %lu cnshift %lu\n",
716 pmp->pm_bpcluster, pmp->pm_cnshift));
717 error = EINVAL;
718 goto error_exit;
719 }
720
721 /*
722 * Cluster size must be within limit of MAXBSIZE.
723 * Many FAT filesystems will not have clusters larger than
724 * 32KiB due to limits in Windows versions before Vista.
725 */
726 if (pmp->pm_bpcluster > MAXBSIZE) {
727 DPRINTF(("bpcluster %lu > MAXBSIZE %d\n",
728 pmp->pm_bpcluster, MAXBSIZE));
729 error = EINVAL;
730 goto error_exit;
731 }
732
733 /*
734 * Release the bootsector buffer.
735 */
736 brelse(bp, BC_AGE);
737 bp = NULL;
738
739 /*
740 * Check FSInfo.
741 */
742 if (pmp->pm_fsinfo) {
743 struct fsinfo *fp;
744
745 /*
746 * XXX If the fsinfo block is stored on media with
747 * 2KB or larger sectors, is the fsinfo structure
748 * padded at the end or in the middle?
749 */
750 if ((error = bread(devvp, de_bn2kb(pmp, pmp->pm_fsinfo),
751 pmp->pm_BytesPerSec, NOCRED, 0, &bp)) != 0)
752 goto error_exit;
753 fp = (struct fsinfo *)bp->b_data;
754 if (!memcmp(fp->fsisig1, "RRaA", 4)
755 && !memcmp(fp->fsisig2, "rrAa", 4)
756 && !memcmp(fp->fsisig3, "\0\0\125\252", 4)
757 && !memcmp(fp->fsisig4, "\0\0\125\252", 4))
758 pmp->pm_nxtfree = getulong(fp->fsinxtfree);
759 else
760 pmp->pm_fsinfo = 0;
761 brelse(bp, 0);
762 bp = NULL;
763 }
764
765 /*
766 * Check and validate (or perhaps invalidate?) the fsinfo structure?
767 * XXX
768 */
769 if (pmp->pm_fsinfo) {
770 if ((pmp->pm_nxtfree == 0xffffffffUL) ||
771 (pmp->pm_nxtfree > pmp->pm_maxcluster))
772 pmp->pm_fsinfo = 0;
773 }
774
775 /*
776 * Allocate memory for the bitmap of allocated clusters, and then
777 * fill it in.
778 */
779 pmp->pm_inusemap = malloc(((pmp->pm_maxcluster + N_INUSEBITS)
780 / N_INUSEBITS)
781 * sizeof(*pmp->pm_inusemap),
782 M_MSDOSFSFAT, M_WAITOK);
783
784 /*
785 * fillinusemap() needs pm_devvp.
786 */
787 pmp->pm_dev = dev;
788 pmp->pm_devvp = devvp;
789
790 /*
791 * Have the inuse map filled in.
792 */
793 if ((error = fillinusemap(pmp)) != 0) {
794 DPRINTF(("fillinusemap %d\n", error));
795 goto error_exit;
796 }
797
798 /*
799 * If they want FAT updates to be synchronous then let them suffer
800 * the performance degradation in exchange for the on disk copy of
801 * the FAT being correct just about all the time. I suppose this
802 * would be a good thing to turn on if the kernel is still flakey.
803 */
804 if (mp->mnt_flag & MNT_SYNCHRONOUS)
805 pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
806
807 /*
808 * Finish up.
809 */
810 if (ronly)
811 pmp->pm_flags |= MSDOSFSMNT_RONLY;
812 else
813 pmp->pm_fmod = 1;
814 mp->mnt_data = pmp;
815 mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
816 mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_MSDOS);
817 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
818 mp->mnt_stat.f_namemax = MSDOSFS_NAMEMAX(pmp);
819 mp->mnt_flag |= MNT_LOCAL;
820 mp->mnt_dev_bshift = pmp->pm_bnshift;
821 mp->mnt_fs_bshift = pmp->pm_cnshift;
822
823 /*
824 * If we ever do quotas for DOS filesystems this would be a place
825 * to fill in the info in the msdosfsmount structure. You dolt,
826 * quotas on dos filesystems make no sense because files have no
827 * owners on dos filesystems. of course there is some empty space
828 * in the directory entry where we could put uid's and gid's.
829 */
830
831 spec_node_setmountedfs(devvp, mp);
832
833 return (0);
834
835 error_exit:
836 fstrans_unmount(mp);
837 if (bp)
838 brelse(bp, BC_AGE);
839 if (pmp) {
840 if (pmp->pm_inusemap)
841 free(pmp->pm_inusemap, M_MSDOSFSFAT);
842 free(pmp, M_MSDOSFSMNT);
843 mp->mnt_data = NULL;
844 }
845 return (error);
846 }
847
848 int
849 msdosfs_start(struct mount *mp, int flags)
850 {
851
852 return (0);
853 }
854
855 /*
856 * Unmount the filesystem described by mp.
857 */
858 int
859 msdosfs_unmount(struct mount *mp, int mntflags)
860 {
861 struct msdosfsmount *pmp;
862 int error, flags;
863
864 flags = 0;
865 if (mntflags & MNT_FORCE)
866 flags |= FORCECLOSE;
867 if ((error = vflush(mp, NULLVP, flags)) != 0)
868 return (error);
869 pmp = VFSTOMSDOSFS(mp);
870 if (pmp->pm_devvp->v_type != VBAD)
871 spec_node_setmountedfs(pmp->pm_devvp, NULL);
872 #ifdef MSDOSFS_DEBUG
873 {
874 struct vnode *vp = pmp->pm_devvp;
875
876 printf("msdosfs_umount(): just before calling VOP_CLOSE()\n");
877 printf("flag %08x, usecount %d, writecount %d, holdcnt %d\n",
878 vp->v_vflag | vp->v_iflag | vp->v_uflag, vp->v_usecount,
879 vp->v_writecount, vp->v_holdcnt);
880 printf("mount %p, op %p\n",
881 vp->v_mount, vp->v_op);
882 printf("freef %p, freeb %p, mount %p\n",
883 vp->v_freelist.tqe_next, vp->v_freelist.tqe_prev,
884 vp->v_mount);
885 printf("cleanblkhd %p, dirtyblkhd %p, numoutput %d, type %d\n",
886 vp->v_cleanblkhd.lh_first,
887 vp->v_dirtyblkhd.lh_first,
888 vp->v_numoutput, vp->v_type);
889 printf("union %p, tag %d, data[0] %08x, data[1] %08x\n",
890 vp->v_socket, vp->v_tag,
891 ((u_int *)vp->v_data)[0],
892 ((u_int *)vp->v_data)[1]);
893 }
894 #endif
895 vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY);
896 (void) VOP_CLOSE(pmp->pm_devvp,
897 pmp->pm_flags & MSDOSFSMNT_RONLY ? FREAD : FREAD|FWRITE, NOCRED);
898 vput(pmp->pm_devvp);
899 msdosfs_fh_destroy(pmp);
900 free(pmp->pm_inusemap, M_MSDOSFSFAT);
901 free(pmp, M_MSDOSFSMNT);
902 mp->mnt_data = NULL;
903 mp->mnt_flag &= ~MNT_LOCAL;
904 fstrans_unmount(mp);
905 return (0);
906 }
907
908 int
909 msdosfs_root(struct mount *mp, struct vnode **vpp)
910 {
911 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
912 int error;
913
914 #ifdef MSDOSFS_DEBUG
915 printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
916 #endif
917 if ((error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, vpp)) != 0)
918 return error;
919 error = vn_lock(*vpp, LK_EXCLUSIVE);
920 if (error) {
921 vrele(*vpp);
922 *vpp = NULL;
923 return error;
924 }
925 return 0;
926 }
927
928 int
929 msdosfs_statvfs(struct mount *mp, struct statvfs *sbp)
930 {
931 struct msdosfsmount *pmp;
932
933 pmp = VFSTOMSDOSFS(mp);
934 sbp->f_bsize = pmp->pm_bpcluster;
935 sbp->f_frsize = sbp->f_bsize;
936 sbp->f_iosize = pmp->pm_bpcluster;
937 sbp->f_blocks = pmp->pm_nmbrofclusters;
938 sbp->f_bfree = pmp->pm_freeclustercount;
939 sbp->f_bavail = pmp->pm_freeclustercount;
940 sbp->f_bresvd = 0;
941 sbp->f_files = pmp->pm_RootDirEnts; /* XXX */
942 sbp->f_ffree = 0; /* what to put in here? */
943 sbp->f_favail = 0; /* what to put in here? */
944 sbp->f_fresvd = 0;
945 copy_statvfs_info(sbp, mp);
946 return (0);
947 }
948
949 struct msdosfs_sync_ctx {
950 int waitfor;
951 };
952
953 static bool
954 msdosfs_sync_selector(void *cl, struct vnode *vp)
955 {
956 struct msdosfs_sync_ctx *c = cl;
957 struct denode *dep;
958
959 dep = VTODE(vp);
960 if (c->waitfor == MNT_LAZY || vp->v_type == VNON ||
961 dep == NULL || (((dep->de_flag &
962 (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0) &&
963 (LIST_EMPTY(&vp->v_dirtyblkhd) &&
964 UVM_OBJ_IS_CLEAN(&vp->v_uobj))))
965 return false;
966 return true;
967 }
968
969 int
970 msdosfs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
971 {
972 struct vnode *vp;
973 struct vnode_iterator *marker;
974 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
975 int error, allerror = 0;
976 struct msdosfs_sync_ctx ctx;
977
978 /*
979 * If we ever switch to not updating all of the FATs all the time,
980 * this would be the place to update them from the first one.
981 */
982 if (pmp->pm_fmod != 0) {
983 if (pmp->pm_flags & MSDOSFSMNT_RONLY)
984 panic("msdosfs_sync: rofs mod");
985 else {
986 /* update FATs here */
987 }
988 }
989 fstrans_start(mp, FSTRANS_SHARED);
990 /*
991 * Write back each (modified) denode.
992 */
993 vfs_vnode_iterator_init(mp, &marker);
994 ctx.waitfor = waitfor;
995 while ((vp = vfs_vnode_iterator_next(marker, msdosfs_sync_selector,
996 &ctx)))
997 {
998 error = vn_lock(vp, LK_EXCLUSIVE);
999 if (error) {
1000 vrele(vp);
1001 continue;
1002 }
1003 if ((error = VOP_FSYNC(vp, cred,
1004 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0)) != 0)
1005 allerror = error;
1006 vput(vp);
1007 }
1008 vfs_vnode_iterator_destroy(marker);
1009
1010 /*
1011 * Force stale file system control information to be flushed.
1012 */
1013 if ((error = VOP_FSYNC(pmp->pm_devvp, cred,
1014 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0)) != 0)
1015 allerror = error;
1016 fstrans_done(mp);
1017 return (allerror);
1018 }
1019
1020 int
1021 msdosfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
1022 {
1023 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
1024 struct defid defh;
1025 uint32_t gen;
1026 int error;
1027
1028 if (fhp->fid_len != sizeof(struct defid)) {
1029 DPRINTF(("fid_len %d %zd\n", fhp->fid_len,
1030 sizeof(struct defid)));
1031 return EINVAL;
1032 }
1033 memcpy(&defh, fhp, sizeof(defh));
1034 error = msdosfs_fh_lookup(pmp, defh.defid_dirclust, defh.defid_dirofs,
1035 &gen);
1036 if (error == 0 && gen != defh.defid_gen)
1037 error = ESTALE;
1038 if (error) {
1039 *vpp = NULLVP;
1040 return error;
1041 }
1042 error = deget(pmp, defh.defid_dirclust, defh.defid_dirofs, vpp);
1043 if (error) {
1044 DPRINTF(("deget %d\n", error));
1045 *vpp = NULLVP;
1046 return error;
1047 }
1048 error = vn_lock(*vpp, LK_EXCLUSIVE);
1049 if (error) {
1050 vrele(*vpp);
1051 *vpp = NULLVP;
1052 return error;
1053 }
1054 return 0;
1055 }
1056
1057 int
1058 msdosfs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
1059 {
1060 struct msdosfsmount *pmp = VFSTOMSDOSFS(vp->v_mount);
1061 struct denode *dep;
1062 struct defid defh;
1063 int error;
1064
1065 if (*fh_size < sizeof(struct defid)) {
1066 *fh_size = sizeof(struct defid);
1067 return E2BIG;
1068 }
1069 *fh_size = sizeof(struct defid);
1070 dep = VTODE(vp);
1071 memset(&defh, 0, sizeof(defh));
1072 defh.defid_len = sizeof(struct defid);
1073 defh.defid_dirclust = dep->de_dirclust;
1074 defh.defid_dirofs = dep->de_diroffset;
1075 error = msdosfs_fh_enter(pmp, dep->de_dirclust, dep->de_diroffset,
1076 &defh.defid_gen);
1077 if (error == 0)
1078 memcpy(fhp, &defh, sizeof(defh));
1079 return error;
1080 }
1081
1082 int
1083 msdosfs_vget(struct mount *mp, ino_t ino,
1084 struct vnode **vpp)
1085 {
1086
1087 return (EOPNOTSUPP);
1088 }
1089
1090 int
1091 msdosfs_suspendctl(struct mount *mp, int cmd)
1092 {
1093 int error;
1094 struct lwp *l = curlwp;
1095
1096 switch (cmd) {
1097 case SUSPEND_SUSPEND:
1098 if ((error = fstrans_setstate(mp, FSTRANS_SUSPENDING)) != 0)
1099 return error;
1100 error = msdosfs_sync(mp, MNT_WAIT, l->l_proc->p_cred);
1101 if (error == 0)
1102 error = fstrans_setstate(mp, FSTRANS_SUSPENDED);
1103 if (error != 0) {
1104 (void) fstrans_setstate(mp, FSTRANS_NORMAL);
1105 return error;
1106 }
1107 return 0;
1108
1109 case SUSPEND_RESUME:
1110 return fstrans_setstate(mp, FSTRANS_NORMAL);
1111
1112 default:
1113 return EINVAL;
1114 }
1115 }
1116