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