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