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