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