msdosfs_vfsops.c revision 1.34 1 /* $NetBSD: msdosfs_vfsops.c,v 1.34 2006/08/14 14:06:26 gdt 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.34 2006/08/14 14:06:26 gdt 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 /*
521 * Some say that bsBootSectSig[23] must be zero, but
522 * Windows does not require this and some digital cameras
523 * do not set these to zero. Therefore, do not insist.
524 */
525 if (pmp->pm_Sectors
526 || pmp->pm_FATsecs
527 || getushort(b710->bpbFSVers)) {
528 error = EINVAL;
529 goto error_exit;
530 }
531 pmp->pm_fatmask = FAT32_MASK;
532 pmp->pm_fatmult = 4;
533 pmp->pm_fatdiv = 1;
534 pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
535
536 /* mirrorring is enabled if the FATMIRROR bit is not set */
537 if ((getushort(b710->bpbExtFlags) & FATMIRROR) == 0)
538 pmp->pm_flags |= MSDOSFS_FATMIRROR;
539 else
540 pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
541 } else
542 pmp->pm_flags |= MSDOSFS_FATMIRROR;
543
544 if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
545 if (FAT32(pmp)) {
546 /*
547 * GEMDOS doesn't know fat32.
548 */
549 error = EINVAL;
550 goto error_exit;
551 }
552
553 /*
554 * Check a few values (could do some more):
555 * - logical sector size: power of 2, >= block size
556 * - sectors per cluster: power of 2, >= 1
557 * - number of sectors: >= 1, <= size of partition
558 */
559 if ( (SecPerClust == 0)
560 || (SecPerClust & (SecPerClust - 1))
561 || (pmp->pm_BytesPerSec < bsize)
562 || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
563 || (pmp->pm_HugeSectors == 0)
564 || (pmp->pm_HugeSectors * (pmp->pm_BytesPerSec / bsize)
565 > dpart.part->p_size)
566 ) {
567 error = EINVAL;
568 goto error_exit;
569 }
570 /*
571 * XXX - Many parts of the msdos fs driver seem to assume that
572 * the number of bytes per logical sector (BytesPerSec) will
573 * always be the same as the number of bytes per disk block
574 * Let's pretend it is.
575 */
576 tmp = pmp->pm_BytesPerSec / bsize;
577 pmp->pm_BytesPerSec = bsize;
578 pmp->pm_HugeSectors *= tmp;
579 pmp->pm_HiddenSects *= tmp;
580 pmp->pm_ResSectors *= tmp;
581 pmp->pm_Sectors *= tmp;
582 pmp->pm_FATsecs *= tmp;
583 SecPerClust *= tmp;
584 }
585 pmp->pm_fatblk = pmp->pm_ResSectors;
586 if (FAT32(pmp)) {
587 pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
588 pmp->pm_firstcluster = pmp->pm_fatblk
589 + (pmp->pm_FATs * pmp->pm_FATsecs);
590 pmp->pm_fsinfo = getushort(b710->bpbFSInfo);
591 } else {
592 pmp->pm_rootdirblk = pmp->pm_fatblk +
593 (pmp->pm_FATs * pmp->pm_FATsecs);
594 pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
595 + pmp->pm_BytesPerSec - 1)
596 / pmp->pm_BytesPerSec;/* in sectors */
597 pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
598 }
599
600 pmp->pm_nmbrofclusters = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
601 SecPerClust;
602 pmp->pm_maxcluster = pmp->pm_nmbrofclusters + 1;
603 pmp->pm_fatsize = pmp->pm_FATsecs * pmp->pm_BytesPerSec;
604
605 if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
606 if (pmp->pm_nmbrofclusters <= (0xff0 - 2)
607 && (dtype == DTYPE_FLOPPY
608 || (dtype == DTYPE_VND
609 && (pmp->pm_Heads == 1 || pmp->pm_Heads == 2)))
610 ) {
611 pmp->pm_fatmask = FAT12_MASK;
612 pmp->pm_fatmult = 3;
613 pmp->pm_fatdiv = 2;
614 } else {
615 pmp->pm_fatmask = FAT16_MASK;
616 pmp->pm_fatmult = 2;
617 pmp->pm_fatdiv = 1;
618 }
619 } else if (pmp->pm_fatmask == 0) {
620 if (pmp->pm_maxcluster
621 <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
622 /*
623 * This will usually be a floppy disk. This size makes
624 * sure that one fat entry will not be split across
625 * multiple blocks.
626 */
627 pmp->pm_fatmask = FAT12_MASK;
628 pmp->pm_fatmult = 3;
629 pmp->pm_fatdiv = 2;
630 } else {
631 pmp->pm_fatmask = FAT16_MASK;
632 pmp->pm_fatmult = 2;
633 pmp->pm_fatdiv = 1;
634 }
635 }
636 if (FAT12(pmp))
637 pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec;
638 else
639 pmp->pm_fatblocksize = MAXBSIZE;
640
641 pmp->pm_fatblocksec = pmp->pm_fatblocksize / pmp->pm_BytesPerSec;
642 pmp->pm_bnshift = ffs(pmp->pm_BytesPerSec) - 1;
643
644 /*
645 * Compute mask and shift value for isolating cluster relative byte
646 * offsets and cluster numbers from a file offset.
647 */
648 pmp->pm_bpcluster = SecPerClust * pmp->pm_BytesPerSec;
649 pmp->pm_crbomask = pmp->pm_bpcluster - 1;
650 pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
651
652 /*
653 * Check for valid cluster size
654 * must be a power of 2
655 */
656 if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
657 error = EINVAL;
658 goto error_exit;
659 }
660
661 /*
662 * Release the bootsector buffer.
663 */
664 brelse(bp);
665 bp = NULL;
666
667 /*
668 * Check FSInfo.
669 */
670 if (pmp->pm_fsinfo) {
671 struct fsinfo *fp;
672
673 if ((error = bread(devvp, pmp->pm_fsinfo, 1024, NOCRED, &bp)) != 0)
674 goto error_exit;
675 fp = (struct fsinfo *)bp->b_data;
676 if (!memcmp(fp->fsisig1, "RRaA", 4)
677 && !memcmp(fp->fsisig2, "rrAa", 4)
678 && !memcmp(fp->fsisig3, "\0\0\125\252", 4)
679 && !memcmp(fp->fsisig4, "\0\0\125\252", 4))
680 pmp->pm_nxtfree = getulong(fp->fsinxtfree);
681 else
682 pmp->pm_fsinfo = 0;
683 brelse(bp);
684 bp = NULL;
685 }
686
687 /*
688 * Check and validate (or perhaps invalidate?) the fsinfo structure?
689 * XXX
690 */
691 if (pmp->pm_fsinfo) {
692 if (pmp->pm_nxtfree == (u_long)-1)
693 pmp->pm_fsinfo = 0;
694 }
695
696 /*
697 * Allocate memory for the bitmap of allocated clusters, and then
698 * fill it in.
699 */
700 pmp->pm_inusemap = malloc(((pmp->pm_maxcluster + N_INUSEBITS - 1)
701 / N_INUSEBITS)
702 * sizeof(*pmp->pm_inusemap),
703 M_MSDOSFSFAT, M_WAITOK);
704
705 /*
706 * fillinusemap() needs pm_devvp.
707 */
708 pmp->pm_dev = dev;
709 pmp->pm_devvp = devvp;
710
711 /*
712 * Have the inuse map filled in.
713 */
714 if ((error = fillinusemap(pmp)) != 0)
715 goto error_exit;
716
717 /*
718 * If they want fat updates to be synchronous then let them suffer
719 * the performance degradation in exchange for the on disk copy of
720 * the fat being correct just about all the time. I suppose this
721 * would be a good thing to turn on if the kernel is still flakey.
722 */
723 if (mp->mnt_flag & MNT_SYNCHRONOUS)
724 pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
725
726 /*
727 * Finish up.
728 */
729 if (ronly)
730 pmp->pm_flags |= MSDOSFSMNT_RONLY;
731 else
732 pmp->pm_fmod = 1;
733 mp->mnt_data = pmp;
734 mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
735 mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_MSDOS);
736 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
737 mp->mnt_stat.f_namemax = MSDOSFS_NAMEMAX(pmp);
738 mp->mnt_flag |= MNT_LOCAL;
739 mp->mnt_dev_bshift = pmp->pm_bnshift;
740 mp->mnt_fs_bshift = pmp->pm_cnshift;
741
742 #ifdef QUOTA
743 /*
744 * If we ever do quotas for DOS filesystems this would be a place
745 * to fill in the info in the msdosfsmount structure. You dolt,
746 * quotas on dos filesystems make no sense because files have no
747 * owners on dos filesystems. of course there is some empty space
748 * in the directory entry where we could put uid's and gid's.
749 */
750 #endif
751 devvp->v_specmountpoint = mp;
752
753 return (0);
754
755 error_exit:;
756 if (bp)
757 brelse(bp);
758 if (pmp) {
759 if (pmp->pm_inusemap)
760 free(pmp->pm_inusemap, M_MSDOSFSFAT);
761 free(pmp, M_MSDOSFSMNT);
762 mp->mnt_data = NULL;
763 }
764 return (error);
765 }
766
767 int
768 msdosfs_start(mp, flags, l)
769 struct mount *mp;
770 int flags;
771 struct lwp *l;
772 {
773
774 return (0);
775 }
776
777 /*
778 * Unmount the filesystem described by mp.
779 */
780 int
781 msdosfs_unmount(mp, mntflags, l)
782 struct mount *mp;
783 int mntflags;
784 struct lwp *l;
785 {
786 struct msdosfsmount *pmp;
787 int error, flags;
788
789 flags = 0;
790 if (mntflags & MNT_FORCE)
791 flags |= FORCECLOSE;
792 #ifdef QUOTA
793 #endif
794 if ((error = vflush(mp, NULLVP, flags)) != 0)
795 return (error);
796 pmp = VFSTOMSDOSFS(mp);
797 if (pmp->pm_devvp->v_type != VBAD)
798 pmp->pm_devvp->v_specmountpoint = NULL;
799 #ifdef MSDOSFS_DEBUG
800 {
801 struct vnode *vp = pmp->pm_devvp;
802
803 printf("msdosfs_umount(): just before calling VOP_CLOSE()\n");
804 printf("flag %08x, usecount %d, writecount %ld, holdcnt %ld\n",
805 vp->v_flag, vp->v_usecount, vp->v_writecount, vp->v_holdcnt);
806 printf("mount %p, op %p\n",
807 vp->v_mount, vp->v_op);
808 printf("freef %p, freeb %p, mount %p\n",
809 vp->v_freelist.tqe_next, vp->v_freelist.tqe_prev,
810 vp->v_mount);
811 printf("cleanblkhd %p, dirtyblkhd %p, numoutput %d, type %d\n",
812 vp->v_cleanblkhd.lh_first,
813 vp->v_dirtyblkhd.lh_first,
814 vp->v_numoutput, vp->v_type);
815 printf("union %p, tag %d, data[0] %08x, data[1] %08x\n",
816 vp->v_socket, vp->v_tag,
817 ((u_int *)vp->v_data)[0],
818 ((u_int *)vp->v_data)[1]);
819 }
820 #endif
821 vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY);
822 error = VOP_CLOSE(pmp->pm_devvp,
823 pmp->pm_flags & MSDOSFSMNT_RONLY ? FREAD : FREAD|FWRITE, NOCRED, l);
824 vput(pmp->pm_devvp);
825 free(pmp->pm_inusemap, M_MSDOSFSFAT);
826 free(pmp, M_MSDOSFSMNT);
827 mp->mnt_data = NULL;
828 mp->mnt_flag &= ~MNT_LOCAL;
829 return (error);
830 }
831
832 int
833 msdosfs_root(mp, vpp)
834 struct mount *mp;
835 struct vnode **vpp;
836 {
837 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
838 struct denode *ndep;
839 int error;
840
841 #ifdef MSDOSFS_DEBUG
842 printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
843 #endif
844 if ((error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep)) != 0)
845 return (error);
846 *vpp = DETOV(ndep);
847 return (0);
848 }
849
850 int
851 msdosfs_quotactl(mp, cmds, uid, arg, l)
852 struct mount *mp;
853 int cmds;
854 uid_t uid;
855 void *arg;
856 struct lwp *l;
857 {
858
859 #ifdef QUOTA
860 return (EOPNOTSUPP);
861 #else
862 return (EOPNOTSUPP);
863 #endif
864 }
865
866 int
867 msdosfs_statvfs(mp, sbp, l)
868 struct mount *mp;
869 struct statvfs *sbp;
870 struct lwp *l;
871 {
872 struct msdosfsmount *pmp;
873
874 pmp = VFSTOMSDOSFS(mp);
875 sbp->f_bsize = pmp->pm_bpcluster;
876 sbp->f_frsize = sbp->f_bsize;
877 sbp->f_iosize = pmp->pm_bpcluster;
878 sbp->f_blocks = pmp->pm_nmbrofclusters;
879 sbp->f_bfree = pmp->pm_freeclustercount;
880 sbp->f_bavail = pmp->pm_freeclustercount;
881 sbp->f_bresvd = 0;
882 sbp->f_files = pmp->pm_RootDirEnts; /* XXX */
883 sbp->f_ffree = 0; /* what to put in here? */
884 sbp->f_favail = 0; /* what to put in here? */
885 sbp->f_fresvd = 0;
886 copy_statvfs_info(sbp, mp);
887 return (0);
888 }
889
890 int
891 msdosfs_sync(mp, waitfor, cred, l)
892 struct mount *mp;
893 int waitfor;
894 kauth_cred_t cred;
895 struct lwp *l;
896 {
897 struct vnode *vp, *nvp;
898 struct denode *dep;
899 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
900 int error, allerror = 0;
901
902 /*
903 * If we ever switch to not updating all of the fats all the time,
904 * this would be the place to update them from the first one.
905 */
906 if (pmp->pm_fmod != 0) {
907 if (pmp->pm_flags & MSDOSFSMNT_RONLY)
908 panic("msdosfs_sync: rofs mod");
909 else {
910 /* update fats here */
911 }
912 }
913 /*
914 * Write back each (modified) denode.
915 */
916 simple_lock(&mntvnode_slock);
917 loop:
918 for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
919 /*
920 * If the vnode that we are about to sync is no longer
921 * assoicated with this mount point, start over.
922 */
923 if (vp->v_mount != mp)
924 goto loop;
925 simple_lock(&vp->v_interlock);
926 nvp = vp->v_mntvnodes.le_next;
927 dep = VTODE(vp);
928 if (waitfor == MNT_LAZY || vp->v_type == VNON ||
929 (((dep->de_flag &
930 (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0) &&
931 (LIST_EMPTY(&vp->v_dirtyblkhd) &&
932 vp->v_uobj.uo_npages == 0))) {
933 simple_unlock(&vp->v_interlock);
934 continue;
935 }
936 simple_unlock(&mntvnode_slock);
937 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
938 if (error) {
939 simple_lock(&mntvnode_slock);
940 if (error == ENOENT)
941 goto loop;
942 continue;
943 }
944 if ((error = VOP_FSYNC(vp, cred,
945 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, l)) != 0)
946 allerror = error;
947 vput(vp);
948 simple_lock(&mntvnode_slock);
949 }
950 simple_unlock(&mntvnode_slock);
951 /*
952 * Force stale file system control information to be flushed.
953 */
954 if ((error = VOP_FSYNC(pmp->pm_devvp, cred,
955 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, l)) != 0)
956 allerror = error;
957 #ifdef QUOTA
958 /* qsync(mp); */
959 #endif
960 return (allerror);
961 }
962
963 int
964 msdosfs_fhtovp(mp, fhp, vpp)
965 struct mount *mp;
966 struct fid *fhp;
967 struct vnode **vpp;
968 {
969 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
970 struct defid defh;
971 struct denode *dep;
972 int error;
973
974 if (fhp->fid_len != sizeof(struct defid))
975 return EINVAL;
976
977 memcpy(&defh, fhp, sizeof(defh));
978 error = deget(pmp, defh.defid_dirclust, defh.defid_dirofs, &dep);
979 if (error) {
980 *vpp = NULLVP;
981 return (error);
982 }
983 *vpp = DETOV(dep);
984 return (0);
985 }
986
987 int
988 msdosfs_vptofh(vp, fhp, fh_size)
989 struct vnode *vp;
990 struct fid *fhp;
991 size_t *fh_size;
992 {
993 struct denode *dep;
994 struct defid defh;
995
996 if (*fh_size < sizeof(struct defid)) {
997 *fh_size = sizeof(struct defid);
998 return E2BIG;
999 }
1000 *fh_size = sizeof(struct defid);
1001 dep = VTODE(vp);
1002 memset(&defh, 0, sizeof(defh));
1003 defh.defid_len = sizeof(struct defid);
1004 defh.defid_dirclust = dep->de_dirclust;
1005 defh.defid_dirofs = dep->de_diroffset;
1006 /* defh.defid_gen = dep->de_gen; */
1007 memcpy(fhp, &defh, sizeof(defh));
1008 return (0);
1009 }
1010
1011 int
1012 msdosfs_vget(mp, ino, vpp)
1013 struct mount *mp;
1014 ino_t ino;
1015 struct vnode **vpp;
1016 {
1017
1018 return (EOPNOTSUPP);
1019 }
1020
1021 SYSCTL_SETUP(sysctl_vfs_msdosfs_setup, "sysctl vfs.msdosfs subtree setup")
1022 {
1023
1024 sysctl_createv(clog, 0, NULL, NULL,
1025 CTLFLAG_PERMANENT,
1026 CTLTYPE_NODE, "vfs", NULL,
1027 NULL, 0, NULL, 0,
1028 CTL_VFS, CTL_EOL);
1029 sysctl_createv(clog, 0, NULL, NULL,
1030 CTLFLAG_PERMANENT,
1031 CTLTYPE_NODE, "msdosfs",
1032 SYSCTL_DESCR("MS-DOS file system"),
1033 NULL, 0, NULL, 0,
1034 CTL_VFS, 4, CTL_EOL);
1035 /*
1036 * XXX the "4" above could be dynamic, thereby eliminating one
1037 * more instance of the "number to vfs" mapping problem, but
1038 * "4" is the order as taken from sys/mount.h
1039 */
1040 }
1041