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