msdosfs_vfsops.c revision 1.58 1 /* $NetBSD: msdosfs_vfsops.c,v 1.58 2008/01/04 14:58:16 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.58 2008/01/04 14:58:16 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 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 (void *)eopnotsupp, /* vfs_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 vfs_destroy(mp);
215 return (error);
216 }
217
218 if ((error = update_mp(mp, &args)) != 0) {
219 (void)msdosfs_unmount(mp, 0);
220 vfs_unbusy(mp);
221 vfs_destroy(mp);
222 vrele(rootvp);
223 return (error);
224 }
225
226 mutex_enter(&mountlist_lock);
227 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
228 mutex_exit(&mountlist_lock);
229 (void)msdosfs_statvfs(mp, &mp->mnt_stat);
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)
241 struct mount *mp;
242 const char *path;
243 void *data;
244 size_t *data_len;
245 {
246 struct lwp *l = curlwp;
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);
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);
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);
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);
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);
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);
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);
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, NOCRED);
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 bsp = (union bootsector *)bp->b_data;
517 b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
518 b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
519 b710 = (struct byte_bpb710 *)bsp->bs710.bsBPB;
520
521 if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
522 if (bsp->bs50.bsBootSectSig0 != BOOTSIG0
523 || bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
524 DPRINTF(("bootsig0 %d bootsig1 %d\n",
525 bsp->bs50.bsBootSectSig0,
526 bsp->bs50.bsBootSectSig1));
527 error = EINVAL;
528 goto error_exit;
529 }
530 }
531
532 pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK);
533 memset(pmp, 0, sizeof *pmp);
534 pmp->pm_mountp = mp;
535
536 /*
537 * Compute several useful quantities from the bpb in the
538 * bootsector. Copy in the dos 5 variant of the bpb then fix up
539 * the fields that are different between dos 5 and dos 3.3.
540 */
541 SecPerClust = b50->bpbSecPerClust;
542 pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
543 pmp->pm_ResSectors = getushort(b50->bpbResSectors);
544 pmp->pm_FATs = b50->bpbFATs;
545 pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
546 pmp->pm_Sectors = getushort(b50->bpbSectors);
547 pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
548 pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
549 pmp->pm_Heads = getushort(b50->bpbHeads);
550 pmp->pm_Media = b50->bpbMedia;
551
552 if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
553 /* XXX - We should probably check more values here */
554 if (!pmp->pm_BytesPerSec || !SecPerClust
555 || pmp->pm_Heads > 255 || pmp->pm_SecPerTrack > 63) {
556 DPRINTF(("bytespersec %d secperclust %d "
557 "heads %d secpertrack %d\n",
558 pmp->pm_BytesPerSec, SecPerClust,
559 pmp->pm_Heads, pmp->pm_SecPerTrack));
560 error = EINVAL;
561 goto error_exit;
562 }
563 }
564
565 if (pmp->pm_Sectors == 0) {
566 pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
567 pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
568 } else {
569 pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
570 pmp->pm_HugeSectors = pmp->pm_Sectors;
571 }
572
573 if (pmp->pm_RootDirEnts == 0) {
574 unsigned short vers = getushort(b710->bpbFSVers);
575 /*
576 * Some say that bsBootSectSig[23] must be zero, but
577 * Windows does not require this and some digital cameras
578 * do not set these to zero. Therefore, do not insist.
579 */
580 if (pmp->pm_Sectors || pmp->pm_FATsecs || vers) {
581 DPRINTF(("sectors %d fatsecs %lu vers %d\n",
582 pmp->pm_Sectors, pmp->pm_FATsecs, vers));
583 error = EINVAL;
584 goto error_exit;
585 }
586 pmp->pm_fatmask = FAT32_MASK;
587 pmp->pm_fatmult = 4;
588 pmp->pm_fatdiv = 1;
589 pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
590
591 /* mirrorring is enabled if the FATMIRROR bit is not set */
592 if ((getushort(b710->bpbExtFlags) & FATMIRROR) == 0)
593 pmp->pm_flags |= MSDOSFS_FATMIRROR;
594 else
595 pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
596 } else
597 pmp->pm_flags |= MSDOSFS_FATMIRROR;
598
599 if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
600 if (FAT32(pmp)) {
601 DPRINTF(("fat32 for gemdos\n"));
602 /*
603 * GEMDOS doesn't know fat32.
604 */
605 error = EINVAL;
606 goto error_exit;
607 }
608
609 /*
610 * Check a few values (could do some more):
611 * - logical sector size: power of 2, >= block size
612 * - sectors per cluster: power of 2, >= 1
613 * - number of sectors: >= 1, <= size of partition
614 */
615 if ( (SecPerClust == 0)
616 || (SecPerClust & (SecPerClust - 1))
617 || (pmp->pm_BytesPerSec < bsize)
618 || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
619 || (pmp->pm_HugeSectors == 0)
620 || (pmp->pm_HugeSectors * (pmp->pm_BytesPerSec / bsize)
621 > psize)) {
622 DPRINTF(("consistency checks for gemdos\n"));
623 error = EINVAL;
624 goto error_exit;
625 }
626 /*
627 * XXX - Many parts of the msdos fs driver seem to assume that
628 * the number of bytes per logical sector (BytesPerSec) will
629 * always be the same as the number of bytes per disk block
630 * Let's pretend it is.
631 */
632 tmp = pmp->pm_BytesPerSec / bsize;
633 pmp->pm_BytesPerSec = bsize;
634 pmp->pm_HugeSectors *= tmp;
635 pmp->pm_HiddenSects *= tmp;
636 pmp->pm_ResSectors *= tmp;
637 pmp->pm_Sectors *= tmp;
638 pmp->pm_FATsecs *= tmp;
639 SecPerClust *= tmp;
640 }
641
642 /* Check that fs has nonzero FAT size */
643 if (pmp->pm_FATsecs == 0) {
644 DPRINTF(("FATsecs is 0\n"));
645 error = EINVAL;
646 goto error_exit;
647 }
648
649 pmp->pm_fatblk = pmp->pm_ResSectors;
650 if (FAT32(pmp)) {
651 pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
652 pmp->pm_firstcluster = pmp->pm_fatblk
653 + (pmp->pm_FATs * pmp->pm_FATsecs);
654 pmp->pm_fsinfo = getushort(b710->bpbFSInfo);
655 } else {
656 pmp->pm_rootdirblk = pmp->pm_fatblk +
657 (pmp->pm_FATs * pmp->pm_FATsecs);
658 pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
659 + pmp->pm_BytesPerSec - 1)
660 / pmp->pm_BytesPerSec;/* in sectors */
661 pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
662 }
663
664 pmp->pm_nmbrofclusters = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
665 SecPerClust;
666 pmp->pm_maxcluster = pmp->pm_nmbrofclusters + 1;
667 pmp->pm_fatsize = pmp->pm_FATsecs * pmp->pm_BytesPerSec;
668
669 if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
670 if (pmp->pm_nmbrofclusters <= (0xff0 - 2)
671 && (dtype == DTYPE_FLOPPY
672 || (dtype == DTYPE_VND
673 && (pmp->pm_Heads == 1 || pmp->pm_Heads == 2)))
674 ) {
675 pmp->pm_fatmask = FAT12_MASK;
676 pmp->pm_fatmult = 3;
677 pmp->pm_fatdiv = 2;
678 } else {
679 pmp->pm_fatmask = FAT16_MASK;
680 pmp->pm_fatmult = 2;
681 pmp->pm_fatdiv = 1;
682 }
683 } else if (pmp->pm_fatmask == 0) {
684 if (pmp->pm_maxcluster
685 <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
686 /*
687 * This will usually be a floppy disk. This size makes
688 * sure that one fat entry will not be split across
689 * multiple blocks.
690 */
691 pmp->pm_fatmask = FAT12_MASK;
692 pmp->pm_fatmult = 3;
693 pmp->pm_fatdiv = 2;
694 } else {
695 pmp->pm_fatmask = FAT16_MASK;
696 pmp->pm_fatmult = 2;
697 pmp->pm_fatdiv = 1;
698 }
699 }
700 if (FAT12(pmp))
701 pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec;
702 else
703 pmp->pm_fatblocksize = MAXBSIZE;
704
705 pmp->pm_fatblocksec = pmp->pm_fatblocksize / pmp->pm_BytesPerSec;
706 pmp->pm_bnshift = ffs(pmp->pm_BytesPerSec) - 1;
707
708 /*
709 * Compute mask and shift value for isolating cluster relative byte
710 * offsets and cluster numbers from a file offset.
711 */
712 pmp->pm_bpcluster = SecPerClust * pmp->pm_BytesPerSec;
713 pmp->pm_crbomask = pmp->pm_bpcluster - 1;
714 pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
715
716 /*
717 * Check for valid cluster size
718 * must be a power of 2
719 */
720 if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
721 DPRINTF(("bpcluster %lu cnshift %lu\n",
722 pmp->pm_bpcluster, pmp->pm_cnshift));
723 error = EINVAL;
724 goto error_exit;
725 }
726
727 /*
728 * Release the bootsector buffer.
729 */
730 brelse(bp, BC_AGE);
731 bp = NULL;
732
733 /*
734 * Check FSInfo.
735 */
736 if (pmp->pm_fsinfo) {
737 struct fsinfo *fp;
738
739 /*
740 * XXX If the fsinfo block is stored on media with
741 * 2KB or larger sectors, is the fsinfo structure
742 * padded at the end or in the middle?
743 */
744 if ((error = bread(devvp, de_bn2kb(pmp, pmp->pm_fsinfo),
745 pmp->pm_BytesPerSec, NOCRED, &bp)) != 0)
746 goto error_exit;
747 fp = (struct fsinfo *)bp->b_data;
748 if (!memcmp(fp->fsisig1, "RRaA", 4)
749 && !memcmp(fp->fsisig2, "rrAa", 4)
750 && !memcmp(fp->fsisig3, "\0\0\125\252", 4)
751 && !memcmp(fp->fsisig4, "\0\0\125\252", 4))
752 pmp->pm_nxtfree = getulong(fp->fsinxtfree);
753 else
754 pmp->pm_fsinfo = 0;
755 brelse(bp, 0);
756 bp = NULL;
757 }
758
759 /*
760 * Check and validate (or perhaps invalidate?) the fsinfo structure?
761 * XXX
762 */
763 if (pmp->pm_fsinfo) {
764 if (pmp->pm_nxtfree == (u_long)-1)
765 pmp->pm_fsinfo = 0;
766 }
767
768 /*
769 * Allocate memory for the bitmap of allocated clusters, and then
770 * fill it in.
771 */
772 pmp->pm_inusemap = malloc(((pmp->pm_maxcluster + N_INUSEBITS - 1)
773 / N_INUSEBITS)
774 * sizeof(*pmp->pm_inusemap),
775 M_MSDOSFSFAT, M_WAITOK);
776
777 /*
778 * fillinusemap() needs pm_devvp.
779 */
780 pmp->pm_dev = dev;
781 pmp->pm_devvp = devvp;
782
783 /*
784 * Have the inuse map filled in.
785 */
786 if ((error = fillinusemap(pmp)) != 0) {
787 DPRINTF(("fillinusemap %d\n", error));
788 goto error_exit;
789 }
790
791 /*
792 * If they want fat updates to be synchronous then let them suffer
793 * the performance degradation in exchange for the on disk copy of
794 * the fat being correct just about all the time. I suppose this
795 * would be a good thing to turn on if the kernel is still flakey.
796 */
797 if (mp->mnt_flag & MNT_SYNCHRONOUS)
798 pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
799
800 /*
801 * Finish up.
802 */
803 if (ronly)
804 pmp->pm_flags |= MSDOSFSMNT_RONLY;
805 else
806 pmp->pm_fmod = 1;
807 mp->mnt_data = pmp;
808 mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
809 mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_MSDOS);
810 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
811 mp->mnt_stat.f_namemax = MSDOSFS_NAMEMAX(pmp);
812 mp->mnt_flag |= MNT_LOCAL;
813 mp->mnt_dev_bshift = pmp->pm_bnshift;
814 mp->mnt_fs_bshift = pmp->pm_cnshift;
815
816 #ifdef QUOTA
817 /*
818 * If we ever do quotas for DOS filesystems this would be a place
819 * to fill in the info in the msdosfsmount structure. You dolt,
820 * quotas on dos filesystems make no sense because files have no
821 * owners on dos filesystems. of course there is some empty space
822 * in the directory entry where we could put uid's and gid's.
823 */
824 #endif
825 devvp->v_specmountpoint = mp;
826
827 return (0);
828
829 error_exit:;
830 if (bp)
831 brelse(bp, BC_AGE);
832 if (pmp) {
833 if (pmp->pm_inusemap)
834 free(pmp->pm_inusemap, M_MSDOSFSFAT);
835 free(pmp, M_MSDOSFSMNT);
836 mp->mnt_data = NULL;
837 }
838 return (error);
839 }
840
841 int
842 msdosfs_start(struct mount *mp, int flags)
843 {
844
845 return (0);
846 }
847
848 /*
849 * Unmount the filesystem described by mp.
850 */
851 int
852 msdosfs_unmount(mp, mntflags)
853 struct mount *mp;
854 int mntflags;
855 {
856 struct msdosfsmount *pmp;
857 int error, flags;
858
859 flags = 0;
860 if (mntflags & MNT_FORCE)
861 flags |= FORCECLOSE;
862 #ifdef QUOTA
863 #endif
864 if ((error = vflush(mp, NULLVP, flags)) != 0)
865 return (error);
866 pmp = VFSTOMSDOSFS(mp);
867 if (pmp->pm_devvp->v_type != VBAD)
868 pmp->pm_devvp->v_specmountpoint = NULL;
869 #ifdef MSDOSFS_DEBUG
870 {
871 struct vnode *vp = pmp->pm_devvp;
872
873 printf("msdosfs_umount(): just before calling VOP_CLOSE()\n");
874 printf("flag %08x, usecount %d, writecount %ld, holdcnt %ld\n",
875 vp->v_vflag | vp->v_iflag | vp->v_uflag, vp->v_usecount,
876 vp->v_writecount, vp->v_holdcnt);
877 printf("mount %p, op %p\n",
878 vp->v_mount, vp->v_op);
879 printf("freef %p, freeb %p, mount %p\n",
880 vp->v_freelist.tqe_next, vp->v_freelist.tqe_prev,
881 vp->v_mount);
882 printf("cleanblkhd %p, dirtyblkhd %p, numoutput %d, type %d\n",
883 vp->v_cleanblkhd.lh_first,
884 vp->v_dirtyblkhd.lh_first,
885 vp->v_numoutput, vp->v_type);
886 printf("union %p, tag %d, data[0] %08x, data[1] %08x\n",
887 vp->v_socket, vp->v_tag,
888 ((u_int *)vp->v_data)[0],
889 ((u_int *)vp->v_data)[1]);
890 }
891 #endif
892 vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY);
893 error = VOP_CLOSE(pmp->pm_devvp,
894 pmp->pm_flags & MSDOSFSMNT_RONLY ? FREAD : FREAD|FWRITE, NOCRED);
895 vput(pmp->pm_devvp);
896 free(pmp->pm_inusemap, M_MSDOSFSFAT);
897 free(pmp, M_MSDOSFSMNT);
898 mp->mnt_data = NULL;
899 mp->mnt_flag &= ~MNT_LOCAL;
900 return (error);
901 }
902
903 int
904 msdosfs_root(mp, vpp)
905 struct mount *mp;
906 struct vnode **vpp;
907 {
908 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
909 struct denode *ndep;
910 int error;
911
912 #ifdef MSDOSFS_DEBUG
913 printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
914 #endif
915 if ((error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep)) != 0)
916 return (error);
917 *vpp = DETOV(ndep);
918 return (0);
919 }
920
921 int
922 msdosfs_statvfs(struct mount *mp, struct statvfs *sbp)
923 {
924 struct msdosfsmount *pmp;
925
926 pmp = VFSTOMSDOSFS(mp);
927 sbp->f_bsize = pmp->pm_bpcluster;
928 sbp->f_frsize = sbp->f_bsize;
929 sbp->f_iosize = pmp->pm_bpcluster;
930 sbp->f_blocks = pmp->pm_nmbrofclusters;
931 sbp->f_bfree = pmp->pm_freeclustercount;
932 sbp->f_bavail = pmp->pm_freeclustercount;
933 sbp->f_bresvd = 0;
934 sbp->f_files = pmp->pm_RootDirEnts; /* XXX */
935 sbp->f_ffree = 0; /* what to put in here? */
936 sbp->f_favail = 0; /* what to put in here? */
937 sbp->f_fresvd = 0;
938 copy_statvfs_info(sbp, mp);
939 return (0);
940 }
941
942 int
943 msdosfs_sync(mp, waitfor, cred)
944 struct mount *mp;
945 int waitfor;
946 kauth_cred_t cred;
947 {
948 struct vnode *vp, *mvp;
949 struct denode *dep;
950 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
951 int error, allerror = 0;
952
953 /*
954 * If we ever switch to not updating all of the fats all the time,
955 * this would be the place to update them from the first one.
956 */
957 if (pmp->pm_fmod != 0) {
958 if (pmp->pm_flags & MSDOSFSMNT_RONLY)
959 panic("msdosfs_sync: rofs mod");
960 else {
961 /* update fats here */
962 }
963 }
964 /* Allocate a marker vnode. */
965 if ((mvp = vnalloc(mp)) == NULL)
966 return ENOMEM;
967 /*
968 * Write back each (modified) denode.
969 */
970 mutex_enter(&mntvnode_lock);
971 loop:
972 for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = vunmark(mvp)) {
973 vmark(mvp, vp);
974 if (vp->v_mount != mp || vismarker(vp))
975 continue;
976 mutex_enter(&vp->v_interlock);
977 dep = VTODE(vp);
978 if (waitfor == MNT_LAZY || vp->v_type == VNON ||
979 (((dep->de_flag &
980 (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0) &&
981 (LIST_EMPTY(&vp->v_dirtyblkhd) &&
982 UVM_OBJ_IS_CLEAN(&vp->v_uobj)))) {
983 mutex_exit(&vp->v_interlock);
984 continue;
985 }
986 mutex_exit(&mntvnode_lock);
987 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
988 if (error) {
989 mutex_enter(&mntvnode_lock);
990 if (error == ENOENT) {
991 (void)vunmark(mvp);
992 goto loop;
993 }
994 continue;
995 }
996 if ((error = VOP_FSYNC(vp, cred,
997 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0)) != 0)
998 allerror = error;
999 vput(vp);
1000 mutex_enter(&mntvnode_lock);
1001 }
1002 mutex_exit(&mntvnode_lock);
1003 vnfree(mvp);
1004
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)) != 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