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