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