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