1/* $Id: msdosfs_vfsops.c,v 1.41 1999/03/28 23:00:33 dt Exp $ */
2/* $NetBSD: msdosfs_vfsops.c,v 1.51 1997/11/17 15:36:58 ws Exp $ */
3
4/*-
5 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
6 * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
7 * All rights reserved.
8 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by TooLs GmbH.
21 * 4. The name of TooLs GmbH may not be used to endorse or promote products
22 * derived from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
30 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35/*
36 * Written by Paul Popelka (paulp@uts.amdahl.com)
37 *
38 * You can do anything you want with this software, just don't say you wrote
39 * it, and don't remove this notice.
40 *
41 * This software is provided "as is".
42 *
43 * The author supplies this software to be publicly redistributed on the
44 * understanding that the author is not responsible for the correct
45 * functioning of this software in any circumstances and is not liable for
46 * any damages caused by this software.
47 *
48 * October 1992
49 */
50
51#include <sys/param.h>
52#include <sys/systm.h>
53#include <sys/conf.h>
54#include <sys/namei.h>
55#include <sys/proc.h>
56#include <sys/kernel.h>
57#include <sys/vnode.h>
58#include <miscfs/specfs/specdev.h> /* XXX */ /* defines v_rdev */
59#include <sys/mount.h>
60#include <sys/buf.h>
61#include <sys/fcntl.h>
62#include <sys/malloc.h>
63#include <sys/stat.h> /* defines ALLPERMS */
64
65#include <msdosfs/bpb.h>
66#include <msdosfs/bootsect.h>
67#include <msdosfs/direntry.h>
68#include <msdosfs/denode.h>
69#include <msdosfs/msdosfsmount.h>
70#include <msdosfs/fat.h>
71
72MALLOC_DEFINE(M_MSDOSFSMNT, "MSDOSFS mount", "MSDOSFS mount structure");
73static MALLOC_DEFINE(M_MSDOSFSFAT, "MSDOSFS FAT", "MSDOSFS file allocation table");
74
75static int update_mp __P((struct mount *mp, struct msdosfs_args *argp));
76static int mountmsdosfs __P((struct vnode *devvp, struct mount *mp,
77 struct proc *p, struct msdosfs_args *argp));
78static int msdosfs_fhtovp __P((struct mount *, struct fid *,
79 struct sockaddr *, struct vnode **, int *,
80 struct ucred **));
81static int msdosfs_mount __P((struct mount *, char *, caddr_t,
82 struct nameidata *, struct proc *));
83static int msdosfs_quotactl __P((struct mount *, int, uid_t, caddr_t,
84 struct proc *));
85static int msdosfs_root __P((struct mount *, struct vnode **));
86static int msdosfs_start __P((struct mount *, int, struct proc *));
87static int msdosfs_statfs __P((struct mount *, struct statfs *,
88 struct proc *));
89static int msdosfs_sync __P((struct mount *, int, struct ucred *,
90 struct proc *));
91static int msdosfs_unmount __P((struct mount *, int, struct proc *));
92static int msdosfs_vget __P((struct mount *mp, ino_t ino,
93 struct vnode **vpp));
94static int msdosfs_vptofh __P((struct vnode *, struct fid *));
95
96static int
97update_mp(mp, argp)
98 struct mount *mp;
99 struct msdosfs_args *argp;
100{
101 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
102 int error;
103
104 pmp->pm_gid = argp->gid;
105 pmp->pm_uid = argp->uid;
106 pmp->pm_mask = argp->mask & ALLPERMS;
107 pmp->pm_flags |= argp->flags & MSDOSFSMNT_MNTOPT;
108 if (pmp->pm_flags & MSDOSFSMNT_U2WTABLE) {
109 bcopy(argp->u2w, pmp->pm_u2w, sizeof(pmp->pm_u2w));
110 bcopy(argp->d2u, pmp->pm_d2u, sizeof(pmp->pm_d2u));
111 bcopy(argp->u2d, pmp->pm_u2d, sizeof(pmp->pm_u2d));
112 }
113 if (pmp->pm_flags & MSDOSFSMNT_ULTABLE) {
114 bcopy(argp->ul, pmp->pm_ul, sizeof(pmp->pm_ul));
115 bcopy(argp->lu, pmp->pm_lu, sizeof(pmp->pm_lu));
116 }
117
118#ifndef __FreeBSD__
119 /*
120 * GEMDOS knows nothing (yet) about win95
121 */
122 if (pmp->pm_flags & MSDOSFSMNT_GEMDOSFS)
123 pmp->pm_flags |= MSDOSFSMNT_NOWIN95;
124#endif
125
126 if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
127 pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
128 else if (!(pmp->pm_flags &
129 (MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) {
130 struct vnode *rootvp;
131
132 /*
133 * Try to divine whether to support Win'95 long filenames
134 */
135 if (FAT32(pmp))
136 pmp->pm_flags |= MSDOSFSMNT_LONGNAME;
137 else {
138 if ((error = msdosfs_root(mp, &rootvp)) != 0)
139 return error;
140 pmp->pm_flags |= findwin95(VTODE(rootvp))
141 ? MSDOSFSMNT_LONGNAME
142 : MSDOSFSMNT_SHORTNAME;
143 vput(rootvp);
144 }
145 }
146 return 0;
147}
148
149#ifndef __FreeBSD__
150int
151msdosfs_mountroot()
152{
153 register struct mount *mp;
154 struct proc *p = curproc; /* XXX */
155 size_t size;
156 int error;
157 struct msdosfs_args args;
158
159 if (root_device->dv_class != DV_DISK)
160 return (ENODEV);
161
162 /*
163 * Get vnodes for swapdev and rootdev.
164 */
165 if (bdevvp(rootdev, &rootvp))
166 panic("msdosfs_mountroot: can't setup rootvp");
167
168 mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK);
169 bzero((char *)mp, (u_long)sizeof(struct mount));
170 mp->mnt_op = &msdosfs_vfsops;
171 mp->mnt_flag = 0;
172 LIST_INIT(&mp->mnt_vnodelist);
173
174 args.flags = 0;
175 args.uid = 0;
176 args.gid = 0;
177 args.mask = 0777;
178
179 if ((error = mountmsdosfs(rootvp, mp, p, &args)) != 0) {
180 free(mp, M_MOUNT);
181 return (error);
182 }
183
184 if ((error = update_mp(mp, &args)) != 0) {
185 (void)msdosfs_unmount(mp, 0, p);
186 free(mp, M_MOUNT);
187 return (error);
188 }
189
190 if ((error = vfs_lock(mp)) != 0) {
191 (void)msdosfs_unmount(mp, 0, p);
192 free(mp, M_MOUNT);
193 return (error);
194 }
195
196 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
197 mp->mnt_vnodecovered = NULLVP;
198 (void) copystr("/", mp->mnt_stat.f_mntonname, MNAMELEN - 1,
199 &size);
200 bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
201 (void) copystr(ROOTNAME, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
202 &size);
203 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
204 (void)msdosfs_statfs(mp, &mp->mnt_stat, p);
205 vfs_unlock(mp);
206 return (0);
207}
208#endif
209
210/*
211 * mp - path - addr in user space of mount point (ie /usr or whatever)
212 * data - addr in user space of mount params including the name of the block
213 * special file to treat as a filesystem.
214 */
215static int
216msdosfs_mount(mp, path, data, ndp, p)
217 struct mount *mp;
218 char *path;
219 caddr_t data;
220 struct nameidata *ndp;
221 struct proc *p;
222{
223 struct vnode *devvp; /* vnode for blk device to mount */
224 struct msdosfs_args args; /* will hold data from mount request */
225 /* msdosfs specific mount control block */
226 struct msdosfsmount *pmp = NULL;
227 size_t size;
228 int error, flags;
229 mode_t accessmode;
230
231 error = copyin(data, (caddr_t)&args, sizeof(struct msdosfs_args));
232 if (error)
233 return (error);
234 if (args.magic != MSDOSFS_ARGSMAGIC)
235 args.flags = 0;
236 /*
237 * If updating, check whether changing from read-only to
238 * read/write; if there is no device name, that's all we do.
239 */
240 if (mp->mnt_flag & MNT_UPDATE) {
241 pmp = VFSTOMSDOSFS(mp);
242 error = 0;
243 if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_flag & MNT_RDONLY)) {
244 flags = WRITECLOSE;
245 if (mp->mnt_flag & MNT_FORCE)
246 flags |= FORCECLOSE;
247 error = vflush(mp, NULLVP, flags);
248 }
249 if (!error && (mp->mnt_flag & MNT_RELOAD))
250 /* not yet implemented */
251 error = EOPNOTSUPP;
252 if (error)
253 return (error);
254 if ((pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
255 /*
256 * If upgrade to read-write by non-root, then verify
257 * that user has necessary permissions on the device.
258 */
259 if (p->p_ucred->cr_uid != 0) {
260 devvp = pmp->pm_devvp;
261 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
262 error = VOP_ACCESS(devvp, VREAD | VWRITE,
263 p->p_ucred, p);
264 if (error) {
265 VOP_UNLOCK(devvp, 0, p);
266 return (error);
267 }
268 VOP_UNLOCK(devvp, 0, p);
269 }
270 pmp->pm_flags &= ~MSDOSFSMNT_RONLY;
271 }
272 if (args.fspec == 0) {
273#ifdef __notyet__ /* doesn't work correctly with current mountd XXX */
274 if (args.flags & MSDOSFSMNT_MNTOPT) {
275 pmp->pm_flags &= ~MSDOSFSMNT_MNTOPT;
276 pmp->pm_flags |= args.flags & MSDOSFSMNT_MNTOPT;
277 if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
278 pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
279 }
280#endif
281 /*
282 * Process export requests.
283 */
284 return (vfs_export(mp, &pmp->pm_export, &args.export));
285 }
286 }
287 /*
288 * Not an update, or updating the name: look up the name
289 * and verify that it refers to a sensible block device.
290 */
291 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
292 error = namei(ndp);
293 if (error)
294 return (error);
295 devvp = ndp->ni_vp;
296
297 if (devvp->v_type != VBLK) {
298 vrele(devvp);
299 return (ENOTBLK);
300 }
301 if (major(devvp->v_rdev) >= nblkdev ||
302 bdevsw[major(devvp->v_rdev)] == NULL) {
303 vrele(devvp);
304 return (ENXIO);
305 }
306 /*
307 * If mount by non-root, then verify that user has necessary
308 * permissions on the device.
309 */
310 if (p->p_ucred->cr_uid != 0) {
311 accessmode = VREAD;
312 if ((mp->mnt_flag & MNT_RDONLY) == 0)
313 accessmode |= VWRITE;
314 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
315 error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
316 if (error) {
317 vput(devvp);
318 return (error);
319 }
320 VOP_UNLOCK(devvp, 0, p);
321 }
322 if ((mp->mnt_flag & MNT_UPDATE) == 0) {
323 error = mountmsdosfs(devvp, mp, p, &args);
324#ifdef MSDOSFS_DEBUG /* only needed for the printf below */
325 pmp = VFSTOMSDOSFS(mp);
326#endif
327 } else {
328 if (devvp != pmp->pm_devvp)
329 error = EINVAL; /* XXX needs translation */
330 else
331 vrele(devvp);
332 }
333 if (error) {
334 vrele(devvp);
335 return (error);
336 }
337
338 error = update_mp(mp, &args);
339 if (error) {
340 msdosfs_unmount(mp, MNT_FORCE, p);
341 return error;
342 }
343
344 (void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
345 bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
346 (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
347 &size);
348 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
349 (void) msdosfs_statfs(mp, &mp->mnt_stat, p);
350#ifdef MSDOSFS_DEBUG
351 printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap);
352#endif
353 return (0);
354}
355
356static int
357mountmsdosfs(devvp, mp, p, argp)
358 struct vnode *devvp;
359 struct mount *mp;
360 struct proc *p;
361 struct msdosfs_args *argp;
362{
363 struct msdosfsmount *pmp;
364 struct buf *bp;
365 dev_t dev = devvp->v_rdev;
366#ifndef __FreeBSD__
367 struct partinfo dpart;
368 int bsize = 0, dtype = 0, tmp;
369#endif
370 union bootsector *bsp;
371 struct byte_bpb33 *b33;
372 struct byte_bpb50 *b50;
373 struct byte_bpb710 *b710;
374 u_int8_t SecPerClust;
375 int ronly, error;
376
377 /*
378 * Disallow multiple mounts of the same device.
379 * Disallow mounting of a device that is currently in use
380 * (except for root, which might share swap device for miniroot).
381 * Flush out any old buffers remaining from a previous use.
382 */
383 error = vfs_mountedon(devvp);
384 if (error)
385 return (error);
386 if (vcount(devvp) > 1 && devvp != rootvp)
387 return (EBUSY);
388 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
389 error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0);
390 VOP_UNLOCK(devvp, 0, p);
391 if (error)
392 return (error);
393
394 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
395 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
396 if (error)
397 return (error);
398
399 bp = NULL; /* both used in error_exit */
400 pmp = NULL;
401
402#ifndef __FreeBSD__
403 if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
404 /*
405 * We need the disklabel to calculate the size of a FAT entry
406 * later on. Also make sure the partition contains a filesystem
407 * of type FS_MSDOS. This doesn't work for floppies, so we have
408 * to check for them too.
409 *
410 * At least some parts of the msdos fs driver seem to assume
411 * that the size of a disk block will always be 512 bytes.
412 * Let's check it...
413 */
414 error = VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart,
415 FREAD, NOCRED, p);
416 if (error)
417 goto error_exit;
418 tmp = dpart.part->p_fstype;
419 dtype = dpart.disklab->d_type;
420 bsize = dpart.disklab->d_secsize;
421 if (bsize != 512 || (dtype!=DTYPE_FLOPPY && tmp!=FS_MSDOS)) {
422 error = EINVAL;
423 goto error_exit;
424 }
425 }
426#endif
427
428 /*
429 * Read the boot sector of the filesystem, and then check the
430 * boot signature. If not a dos boot sector then error out.
431 */
432#ifdef PC98
433 error = bread(devvp, 0, 1024, NOCRED, &bp);
434#else
435 error = bread(devvp, 0, 512, NOCRED, &bp);
436#endif
437 if (error)
438 goto error_exit;
439 bp->b_flags |= B_AGE;
440 bsp = (union bootsector *)bp->b_data;
441 b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
442 b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
443 b710 = (struct byte_bpb710 *)bsp->bs710.bsPBP;
444
445#ifndef __FreeBSD__
446 if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
447#endif
448#ifdef PC98
449 if ((bsp->bs50.bsBootSectSig0 != BOOTSIG0
450 || bsp->bs50.bsBootSectSig1 != BOOTSIG1)
451 && (bsp->bs50.bsBootSectSig0 != 0 /* PC98 DOS 3.3x */
452 || bsp->bs50.bsBootSectSig1 != 0)
453 && (bsp->bs50.bsBootSectSig0 != 0x90 /* PC98 DOS 5.0 */
454 || bsp->bs50.bsBootSectSig1 != 0x3d)
455 && (bsp->bs50.bsBootSectSig0 != 0x46 /* PC98 DOS 3.3B */
456 || bsp->bs50.bsBootSectSig1 != 0xfa)) {
457#else
458 if (bsp->bs50.bsBootSectSig0 != BOOTSIG0
459 || bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
460#endif
461 error = EINVAL;
462 goto error_exit;
463 }
464#ifndef __FreeBSD__
465 }
466#endif
467
468 pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK);
469 bzero((caddr_t)pmp, sizeof *pmp);
470 pmp->pm_mountp = mp;
471
472 /*
473 * Compute several useful quantities from the bpb in the
474 * bootsector. Copy in the dos 5 variant of the bpb then fix up
475 * the fields that are different between dos 5 and dos 3.3.
476 */
477 SecPerClust = b50->bpbSecPerClust;
478 pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
479 pmp->pm_ResSectors = getushort(b50->bpbResSectors);
480 pmp->pm_FATs = b50->bpbFATs;
481 pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
482 pmp->pm_Sectors = getushort(b50->bpbSectors);
483 pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
484 pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
485 pmp->pm_Heads = getushort(b50->bpbHeads);
486 pmp->pm_Media = b50->bpbMedia;
487
488#ifndef __FreeBSD__
489 if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
490#endif
491 /* XXX - We should probably check more values here */
492 if (!pmp->pm_BytesPerSec || !SecPerClust
493 || !pmp->pm_Heads || pmp->pm_Heads > 255
494#ifdef PC98
495 || !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 255) {
496#else
497 || !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 63) {
498#endif
499 error = EINVAL;
500 goto error_exit;
501 }
502#ifndef __FreeBSD__
503 }
504#endif
505
506 if (pmp->pm_Sectors == 0) {
507 pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
508 pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
509 } else {
510 pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
511 pmp->pm_HugeSectors = pmp->pm_Sectors;
512 }
513 if (pmp->pm_HugeSectors > 0xffffffff /
514 (pmp->pm_BytesPerSec / sizeof(struct direntry)) + 1) {
515 /*
516 * We cannot deal currently with this size of disk
517 * due to fileid limitations (see msdosfs_getattr and
518 * msdosfs_readdir)
519 */
520 error = EINVAL;
521 printf("mountmsdosfs(): disk too big, sorry\n");
522 goto error_exit;
523 }
524
525 if (pmp->pm_RootDirEnts == 0) {
526 if (bsp->bs710.bsBootSectSig2 != BOOTSIG2
527 || bsp->bs710.bsBootSectSig3 != BOOTSIG3
528 || pmp->pm_Sectors
529 || pmp->pm_FATsecs
530 || getushort(b710->bpbFSVers)) {
531 error = EINVAL;
532 printf("mountmsdosfs(): bad FAT32 filesystem\n");
533 goto error_exit;
534 }
535 pmp->pm_fatmask = FAT32_MASK;
536 pmp->pm_fatmult = 4;
537 pmp->pm_fatdiv = 1;
538 pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
539 if (getushort(b710->bpbExtFlags) & FATMIRROR)
540 pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
541 else
542 pmp->pm_flags |= MSDOSFS_FATMIRROR;
543 } else
544 pmp->pm_flags |= MSDOSFS_FATMIRROR;
545
546#ifndef __FreeBSD__
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#endif
589 pmp->pm_fatblk = pmp->pm_ResSectors;
590 if (FAT32(pmp)) {
591 pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
592 pmp->pm_firstcluster = pmp->pm_fatblk
593 + (pmp->pm_FATs * pmp->pm_FATsecs);
594 pmp->pm_fsinfo = getushort(b710->bpbFSInfo);
595 } else {
596 pmp->pm_rootdirblk = pmp->pm_fatblk +
597 (pmp->pm_FATs * pmp->pm_FATsecs);
598 pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
599 + pmp->pm_BytesPerSec - 1)
600 / pmp->pm_BytesPerSec;/* in sectors */
601 pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
602 }
603
604 pmp->pm_nmbrofclusters = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
605 SecPerClust;
606 pmp->pm_maxcluster = pmp->pm_nmbrofclusters + 1;
607 pmp->pm_fatsize = pmp->pm_FATsecs * pmp->pm_BytesPerSec;
608
609#ifndef __FreeBSD__
610 if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
611 if ((pmp->pm_nmbrofclusters <= (0xff0 - 2))
612 && ((dtype == DTYPE_FLOPPY) || ((dtype == DTYPE_VNODE)
613 && ((pmp->pm_Heads == 1) || (pmp->pm_Heads == 2))))
614 ) {
615 pmp->pm_fatmask = FAT12_MASK;
616 pmp->pm_fatmult = 3;
617 pmp->pm_fatdiv = 2;
618 } else {
619 pmp->pm_fatmask = FAT16_MASK;
620 pmp->pm_fatmult = 2;
621 pmp->pm_fatdiv = 1;
622 }
623 } else
624#endif
625 if (pmp->pm_fatmask == 0) {
626 if (pmp->pm_maxcluster
627 <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
628 /*
629 * This will usually be a floppy disk. This size makes
630 * sure that one fat entry will not be split across
631 * multiple blocks.
632 */
633 pmp->pm_fatmask = FAT12_MASK;
634 pmp->pm_fatmult = 3;
635 pmp->pm_fatdiv = 2;
636 } else {
637 pmp->pm_fatmask = FAT16_MASK;
638 pmp->pm_fatmult = 2;
639 pmp->pm_fatdiv = 1;
640 }
641 }
642 if (FAT12(pmp))
643 pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec;
644 else
645 pmp->pm_fatblocksize = DFLTBSIZE;
646
647 pmp->pm_fatblocksec = pmp->pm_fatblocksize / pmp->pm_BytesPerSec;
648 pmp->pm_bnshift = ffs(pmp->pm_BytesPerSec) - 1;
649
650 /*
651 * Compute mask and shift value for isolating cluster relative byte
652 * offsets and cluster numbers from a file offset.
653 */
654 pmp->pm_bpcluster = SecPerClust * pmp->pm_BytesPerSec;
655 pmp->pm_crbomask = pmp->pm_bpcluster - 1;
656 pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
657
658 /*
659 * Check for valid cluster size
660 * must be a power of 2
661 */
662 if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
663 error = EINVAL;
664 goto error_exit;
665 }
666
667 /*
668 * Release the bootsector buffer.
669 */
670 brelse(bp);
671 bp = NULL;
672
673 /*
674 * Check FSInfo.
675 */
676 if (pmp->pm_fsinfo) {
677 struct fsinfo *fp;
678
679 if ((error = bread(devvp, pmp->pm_fsinfo, 1024, NOCRED, &bp)) != 0)
680 goto error_exit;
681 fp = (struct fsinfo *)bp->b_data;
682 if (!bcmp(fp->fsisig1, "RRaA", 4)
683 && !bcmp(fp->fsisig2, "rrAa", 4)
684 && !bcmp(fp->fsisig3, "\0\0\125\252", 4)
685 && !bcmp(fp->fsisig4, "\0\0\125\252", 4))
686 pmp->pm_nxtfree = getulong(fp->fsinxtfree);
687 else
688 pmp->pm_fsinfo = 0;
689 brelse(bp);
690 bp = NULL;
691 }
692
693 /*
694 * Check and validate (or perhaps invalidate?) the fsinfo structure? XXX
695 */
696
697 /*
698 * Allocate memory for the bitmap of allocated clusters, and then
699 * fill it in.
700 */
701 pmp->pm_inusemap = malloc(((pmp->pm_maxcluster + N_INUSEBITS - 1)
702 / N_INUSEBITS)
703 * sizeof(*pmp->pm_inusemap),
704 M_MSDOSFSFAT, M_WAITOK);
705
706 /*
707 * fillinusemap() needs pm_devvp.
708 */
709 pmp->pm_dev = dev;
710 pmp->pm_devvp = devvp;
711
712 /*
713 * Have the inuse map filled in.
714 */
715 if ((error = fillinusemap(pmp)) != 0)
716 goto error_exit;
717
718 /*
719 * If they want fat updates to be synchronous then let them suffer
720 * the performance degradation in exchange for the on disk copy of
721 * the fat being correct just about all the time. I suppose this
722 * would be a good thing to turn on if the kernel is still flakey.
723 */
724 if (mp->mnt_flag & MNT_SYNCHRONOUS)
725 pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
726
727 /*
728 * Finish up.
729 */
730 if (ronly)
731 pmp->pm_flags |= MSDOSFSMNT_RONLY;
732 else
733 pmp->pm_fmod = 1;
734 mp->mnt_data = (qaddr_t) pmp;
735 mp->mnt_stat.f_fsid.val[0] = (long)dev;
736 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
737 mp->mnt_flag |= MNT_LOCAL;
738 devvp->v_specmountpoint = mp;
739
740 return 0;
741
742error_exit:
743 if (bp)
744 brelse(bp);
745 (void) VOP_CLOSE(devvp, ronly ? FREAD : FREAD | FWRITE, NOCRED, p);
746 if (pmp) {
747 if (pmp->pm_inusemap)
748 free(pmp->pm_inusemap, M_MSDOSFSFAT);
749 free(pmp, M_MSDOSFSMNT);
750 mp->mnt_data = (qaddr_t)0;
751 }
752 return (error);
753}
754
755static int
756msdosfs_start(mp, flags, p)
757 struct mount *mp;
758 int flags;
759 struct proc *p;
760{
761
762 return (0);
763}
764
765/*
766 * Unmount the filesystem described by mp.
767 */
768static int
769msdosfs_unmount(mp, mntflags, p)
770 struct mount *mp;
771 int mntflags;
772 struct proc *p;
773{
774 struct msdosfsmount *pmp;
775 int error, flags;
776
777 flags = 0;
778 if (mntflags & MNT_FORCE)
779 flags |= FORCECLOSE;
780 error = vflush(mp, NULLVP, flags);
781 if (error)
782 return error;
783 pmp = VFSTOMSDOSFS(mp);
784 pmp->pm_devvp->v_specmountpoint = NULL;
785#ifdef MSDOSFS_DEBUG
786 {
787 struct vnode *vp = pmp->pm_devvp;
788
789 printf("msdosfs_umount(): just before calling VOP_CLOSE()\n");
790 printf("flag %08lx, usecount %d, writecount %d, holdcnt %ld\n",
791 vp->v_flag, vp->v_usecount, vp->v_writecount, vp->v_holdcnt);
792 printf("lastr %d, id %lu, mount %p, op %p\n",
793 vp->v_lastr, vp->v_id, vp->v_mount, vp->v_op);
794 printf("freef %p, freeb %p, mount %p\n",
795 vp->v_freelist.tqe_next, vp->v_freelist.tqe_prev,
796 vp->v_mount);
797 printf("cleanblkhd %p, dirtyblkhd %p, numoutput %ld, type %d\n",
798 TAILQ_FIRST(&vp->v_cleanblkhd),
799 TAILQ_FIRST(&vp->v_dirtyblkhd),
800 vp->v_numoutput, vp->v_type);
801 printf("union %p, tag %d, data[0] %08x, data[1] %08x\n",
802 vp->v_socket, vp->v_tag,
803 ((u_int *)vp->v_data)[0],
804 ((u_int *)vp->v_data)[1]);
805 }
806#endif
807 error = VOP_CLOSE(pmp->pm_devvp,
808 (pmp->pm_flags&MSDOSFSMNT_RONLY) ? FREAD : FREAD | FWRITE,
809 NOCRED, p);
810 vrele(pmp->pm_devvp);
811 free(pmp->pm_inusemap, M_MSDOSFSFAT);
812 free(pmp, M_MSDOSFSMNT);
813 mp->mnt_data = (qaddr_t)0;
814 mp->mnt_flag &= ~MNT_LOCAL;
815 return (error);
816}
817
818static int
819msdosfs_root(mp, vpp)
820 struct mount *mp;
821 struct vnode **vpp;
822{
823 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
824 struct denode *ndep;
825 int error;
826
827#ifdef MSDOSFS_DEBUG
828 printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
829#endif
830 error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep);
831 if (error)
832 return (error);
833 *vpp = DETOV(ndep);
834 return (0);
835}
836
837static int
838msdosfs_quotactl(mp, cmds, uid, arg, p)
839 struct mount *mp;
840 int cmds;
841 uid_t uid;
842 caddr_t arg;
843 struct proc *p;
844{
845 return EOPNOTSUPP;
846}
847
848static int
849msdosfs_statfs(mp, sbp, p)
850 struct mount *mp;
851 struct statfs *sbp;
852 struct proc *p;
853{
854 struct msdosfsmount *pmp;
855
856 pmp = VFSTOMSDOSFS(mp);
857 sbp->f_bsize = pmp->pm_bpcluster;
858 sbp->f_iosize = pmp->pm_bpcluster;
859 sbp->f_blocks = pmp->pm_nmbrofclusters;
860 sbp->f_bfree = pmp->pm_freeclustercount;
861 sbp->f_bavail = pmp->pm_freeclustercount;
862 sbp->f_files = pmp->pm_RootDirEnts; /* XXX */
863 sbp->f_ffree = 0; /* what to put in here? */
864 if (sbp != &mp->mnt_stat) {
865 sbp->f_type = mp->mnt_vfc->vfc_typenum;
866 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
867 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
868 }
869 strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
870 return (0);
871}
872
873static int
874msdosfs_sync(mp, waitfor, cred, p)
875 struct mount *mp;
876 int waitfor;
877 struct ucred *cred;
878 struct proc *p;
879{
880 struct vnode *vp, *nvp;
881 struct denode *dep;
882 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
883 int error, allerror = 0;
884
885 /*
886 * If we ever switch to not updating all of the fats all the time,
887 * this would be the place to update them from the first one.
888 */
889 if (pmp->pm_fmod != 0) {
890 if (pmp->pm_flags & MSDOSFSMNT_RONLY)
891 panic("msdosfs_sync: rofs mod");
892 else {
893 /* update fats here */
894 }
895 }
896 /*
897 * Write back each (modified) denode.
898 */
899 simple_lock(&mntvnode_slock);
900loop:
901 for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
902 /*
903 * If the vnode that we are about to sync is no longer
904 * associated with this mount point, start over.
905 */
906 if (vp->v_mount != mp)
907 goto loop;
908
909 simple_lock(&vp->v_interlock);
910 nvp = vp->v_mntvnodes.le_next;
911 dep = VTODE(vp);
912 if (vp->v_type == VNON ||
913 ((dep->de_flag &
914 (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 &&
915 (TAILQ_EMPTY(&vp->v_dirtyblkhd) || waitfor == MNT_LAZY))) {
916 simple_unlock(&vp->v_interlock);
917 continue;
918 }
919 simple_unlock(&mntvnode_slock);
920 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, p);
921 if (error) {
922 simple_lock(&mntvnode_slock);
923 if (error == ENOENT)
924 goto loop;
925 continue;
926 }
927 error = VOP_FSYNC(vp, cred, waitfor, p);
928 if (error)
929 allerror = error;
930 VOP_UNLOCK(vp, 0, p);
931 vrele(vp);
932 simple_lock(&mntvnode_slock);
933 }
934 simple_unlock(&mntvnode_slock);
935
936 /*
937 * Flush filesystem control info.
938 */
939 if (waitfor != MNT_LAZY) {
940 vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY, p);
941 error = VOP_FSYNC(pmp->pm_devvp, cred, waitfor, p);
942 if (error)
943 allerror = error;
944 VOP_UNLOCK(pmp->pm_devvp, 0, p);
945 }
946 return (allerror);
947}
948
949static int
950msdosfs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
951 struct mount *mp;
952 struct fid *fhp;
953 struct sockaddr *nam;
954 struct vnode **vpp;
955 int *exflagsp;
956 struct ucred **credanonp;
957{
958 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
959 struct defid *defhp = (struct defid *) fhp;
960 struct denode *dep;
961 struct netcred *np;
962 int error;
963
964 np = vfs_export_lookup(mp, &pmp->pm_export, nam);
965 if (np == NULL)
966 return (EACCES);
967 error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep);
968 if (error) {
969 *vpp = NULLVP;
970 return (error);
971 }
972 *vpp = DETOV(dep);
973 *exflagsp = np->netc_exflags;
974 *credanonp = &np->netc_anon;
975 return (0);
976}
977
978static int
979msdosfs_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
995static int
996msdosfs_vget(mp, ino, vpp)
997 struct mount *mp;
998 ino_t ino;
999 struct vnode **vpp;
1000{
1001 return EOPNOTSUPP;
1002}
1003
1004static struct vfsops msdosfs_vfsops = {
1005 msdosfs_mount,
1006 msdosfs_start,
1007 msdosfs_unmount,
1008 msdosfs_root,
1009 msdosfs_quotactl,
1010 msdosfs_statfs,
1011 msdosfs_sync,
1012 msdosfs_vget,
1013 msdosfs_fhtovp,
1014 msdosfs_vptofh,
1015 msdosfs_init
1016};
1017
1018VFS_SET(msdosfs_vfsops, msdos, 0);
2/* $NetBSD: msdosfs_vfsops.c,v 1.51 1997/11/17 15:36:58 ws Exp $ */
3
4/*-
5 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
6 * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
7 * All rights reserved.
8 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by TooLs GmbH.
21 * 4. The name of TooLs GmbH may not be used to endorse or promote products
22 * derived from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
30 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35/*
36 * Written by Paul Popelka (paulp@uts.amdahl.com)
37 *
38 * You can do anything you want with this software, just don't say you wrote
39 * it, and don't remove this notice.
40 *
41 * This software is provided "as is".
42 *
43 * The author supplies this software to be publicly redistributed on the
44 * understanding that the author is not responsible for the correct
45 * functioning of this software in any circumstances and is not liable for
46 * any damages caused by this software.
47 *
48 * October 1992
49 */
50
51#include <sys/param.h>
52#include <sys/systm.h>
53#include <sys/conf.h>
54#include <sys/namei.h>
55#include <sys/proc.h>
56#include <sys/kernel.h>
57#include <sys/vnode.h>
58#include <miscfs/specfs/specdev.h> /* XXX */ /* defines v_rdev */
59#include <sys/mount.h>
60#include <sys/buf.h>
61#include <sys/fcntl.h>
62#include <sys/malloc.h>
63#include <sys/stat.h> /* defines ALLPERMS */
64
65#include <msdosfs/bpb.h>
66#include <msdosfs/bootsect.h>
67#include <msdosfs/direntry.h>
68#include <msdosfs/denode.h>
69#include <msdosfs/msdosfsmount.h>
70#include <msdosfs/fat.h>
71
72MALLOC_DEFINE(M_MSDOSFSMNT, "MSDOSFS mount", "MSDOSFS mount structure");
73static MALLOC_DEFINE(M_MSDOSFSFAT, "MSDOSFS FAT", "MSDOSFS file allocation table");
74
75static int update_mp __P((struct mount *mp, struct msdosfs_args *argp));
76static int mountmsdosfs __P((struct vnode *devvp, struct mount *mp,
77 struct proc *p, struct msdosfs_args *argp));
78static int msdosfs_fhtovp __P((struct mount *, struct fid *,
79 struct sockaddr *, struct vnode **, int *,
80 struct ucred **));
81static int msdosfs_mount __P((struct mount *, char *, caddr_t,
82 struct nameidata *, struct proc *));
83static int msdosfs_quotactl __P((struct mount *, int, uid_t, caddr_t,
84 struct proc *));
85static int msdosfs_root __P((struct mount *, struct vnode **));
86static int msdosfs_start __P((struct mount *, int, struct proc *));
87static int msdosfs_statfs __P((struct mount *, struct statfs *,
88 struct proc *));
89static int msdosfs_sync __P((struct mount *, int, struct ucred *,
90 struct proc *));
91static int msdosfs_unmount __P((struct mount *, int, struct proc *));
92static int msdosfs_vget __P((struct mount *mp, ino_t ino,
93 struct vnode **vpp));
94static int msdosfs_vptofh __P((struct vnode *, struct fid *));
95
96static int
97update_mp(mp, argp)
98 struct mount *mp;
99 struct msdosfs_args *argp;
100{
101 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
102 int error;
103
104 pmp->pm_gid = argp->gid;
105 pmp->pm_uid = argp->uid;
106 pmp->pm_mask = argp->mask & ALLPERMS;
107 pmp->pm_flags |= argp->flags & MSDOSFSMNT_MNTOPT;
108 if (pmp->pm_flags & MSDOSFSMNT_U2WTABLE) {
109 bcopy(argp->u2w, pmp->pm_u2w, sizeof(pmp->pm_u2w));
110 bcopy(argp->d2u, pmp->pm_d2u, sizeof(pmp->pm_d2u));
111 bcopy(argp->u2d, pmp->pm_u2d, sizeof(pmp->pm_u2d));
112 }
113 if (pmp->pm_flags & MSDOSFSMNT_ULTABLE) {
114 bcopy(argp->ul, pmp->pm_ul, sizeof(pmp->pm_ul));
115 bcopy(argp->lu, pmp->pm_lu, sizeof(pmp->pm_lu));
116 }
117
118#ifndef __FreeBSD__
119 /*
120 * GEMDOS knows nothing (yet) about win95
121 */
122 if (pmp->pm_flags & MSDOSFSMNT_GEMDOSFS)
123 pmp->pm_flags |= MSDOSFSMNT_NOWIN95;
124#endif
125
126 if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
127 pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
128 else if (!(pmp->pm_flags &
129 (MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) {
130 struct vnode *rootvp;
131
132 /*
133 * Try to divine whether to support Win'95 long filenames
134 */
135 if (FAT32(pmp))
136 pmp->pm_flags |= MSDOSFSMNT_LONGNAME;
137 else {
138 if ((error = msdosfs_root(mp, &rootvp)) != 0)
139 return error;
140 pmp->pm_flags |= findwin95(VTODE(rootvp))
141 ? MSDOSFSMNT_LONGNAME
142 : MSDOSFSMNT_SHORTNAME;
143 vput(rootvp);
144 }
145 }
146 return 0;
147}
148
149#ifndef __FreeBSD__
150int
151msdosfs_mountroot()
152{
153 register struct mount *mp;
154 struct proc *p = curproc; /* XXX */
155 size_t size;
156 int error;
157 struct msdosfs_args args;
158
159 if (root_device->dv_class != DV_DISK)
160 return (ENODEV);
161
162 /*
163 * Get vnodes for swapdev and rootdev.
164 */
165 if (bdevvp(rootdev, &rootvp))
166 panic("msdosfs_mountroot: can't setup rootvp");
167
168 mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK);
169 bzero((char *)mp, (u_long)sizeof(struct mount));
170 mp->mnt_op = &msdosfs_vfsops;
171 mp->mnt_flag = 0;
172 LIST_INIT(&mp->mnt_vnodelist);
173
174 args.flags = 0;
175 args.uid = 0;
176 args.gid = 0;
177 args.mask = 0777;
178
179 if ((error = mountmsdosfs(rootvp, mp, p, &args)) != 0) {
180 free(mp, M_MOUNT);
181 return (error);
182 }
183
184 if ((error = update_mp(mp, &args)) != 0) {
185 (void)msdosfs_unmount(mp, 0, p);
186 free(mp, M_MOUNT);
187 return (error);
188 }
189
190 if ((error = vfs_lock(mp)) != 0) {
191 (void)msdosfs_unmount(mp, 0, p);
192 free(mp, M_MOUNT);
193 return (error);
194 }
195
196 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
197 mp->mnt_vnodecovered = NULLVP;
198 (void) copystr("/", mp->mnt_stat.f_mntonname, MNAMELEN - 1,
199 &size);
200 bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
201 (void) copystr(ROOTNAME, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
202 &size);
203 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
204 (void)msdosfs_statfs(mp, &mp->mnt_stat, p);
205 vfs_unlock(mp);
206 return (0);
207}
208#endif
209
210/*
211 * mp - path - addr in user space of mount point (ie /usr or whatever)
212 * data - addr in user space of mount params including the name of the block
213 * special file to treat as a filesystem.
214 */
215static int
216msdosfs_mount(mp, path, data, ndp, p)
217 struct mount *mp;
218 char *path;
219 caddr_t data;
220 struct nameidata *ndp;
221 struct proc *p;
222{
223 struct vnode *devvp; /* vnode for blk device to mount */
224 struct msdosfs_args args; /* will hold data from mount request */
225 /* msdosfs specific mount control block */
226 struct msdosfsmount *pmp = NULL;
227 size_t size;
228 int error, flags;
229 mode_t accessmode;
230
231 error = copyin(data, (caddr_t)&args, sizeof(struct msdosfs_args));
232 if (error)
233 return (error);
234 if (args.magic != MSDOSFS_ARGSMAGIC)
235 args.flags = 0;
236 /*
237 * If updating, check whether changing from read-only to
238 * read/write; if there is no device name, that's all we do.
239 */
240 if (mp->mnt_flag & MNT_UPDATE) {
241 pmp = VFSTOMSDOSFS(mp);
242 error = 0;
243 if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_flag & MNT_RDONLY)) {
244 flags = WRITECLOSE;
245 if (mp->mnt_flag & MNT_FORCE)
246 flags |= FORCECLOSE;
247 error = vflush(mp, NULLVP, flags);
248 }
249 if (!error && (mp->mnt_flag & MNT_RELOAD))
250 /* not yet implemented */
251 error = EOPNOTSUPP;
252 if (error)
253 return (error);
254 if ((pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
255 /*
256 * If upgrade to read-write by non-root, then verify
257 * that user has necessary permissions on the device.
258 */
259 if (p->p_ucred->cr_uid != 0) {
260 devvp = pmp->pm_devvp;
261 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
262 error = VOP_ACCESS(devvp, VREAD | VWRITE,
263 p->p_ucred, p);
264 if (error) {
265 VOP_UNLOCK(devvp, 0, p);
266 return (error);
267 }
268 VOP_UNLOCK(devvp, 0, p);
269 }
270 pmp->pm_flags &= ~MSDOSFSMNT_RONLY;
271 }
272 if (args.fspec == 0) {
273#ifdef __notyet__ /* doesn't work correctly with current mountd XXX */
274 if (args.flags & MSDOSFSMNT_MNTOPT) {
275 pmp->pm_flags &= ~MSDOSFSMNT_MNTOPT;
276 pmp->pm_flags |= args.flags & MSDOSFSMNT_MNTOPT;
277 if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
278 pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
279 }
280#endif
281 /*
282 * Process export requests.
283 */
284 return (vfs_export(mp, &pmp->pm_export, &args.export));
285 }
286 }
287 /*
288 * Not an update, or updating the name: look up the name
289 * and verify that it refers to a sensible block device.
290 */
291 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
292 error = namei(ndp);
293 if (error)
294 return (error);
295 devvp = ndp->ni_vp;
296
297 if (devvp->v_type != VBLK) {
298 vrele(devvp);
299 return (ENOTBLK);
300 }
301 if (major(devvp->v_rdev) >= nblkdev ||
302 bdevsw[major(devvp->v_rdev)] == NULL) {
303 vrele(devvp);
304 return (ENXIO);
305 }
306 /*
307 * If mount by non-root, then verify that user has necessary
308 * permissions on the device.
309 */
310 if (p->p_ucred->cr_uid != 0) {
311 accessmode = VREAD;
312 if ((mp->mnt_flag & MNT_RDONLY) == 0)
313 accessmode |= VWRITE;
314 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
315 error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
316 if (error) {
317 vput(devvp);
318 return (error);
319 }
320 VOP_UNLOCK(devvp, 0, p);
321 }
322 if ((mp->mnt_flag & MNT_UPDATE) == 0) {
323 error = mountmsdosfs(devvp, mp, p, &args);
324#ifdef MSDOSFS_DEBUG /* only needed for the printf below */
325 pmp = VFSTOMSDOSFS(mp);
326#endif
327 } else {
328 if (devvp != pmp->pm_devvp)
329 error = EINVAL; /* XXX needs translation */
330 else
331 vrele(devvp);
332 }
333 if (error) {
334 vrele(devvp);
335 return (error);
336 }
337
338 error = update_mp(mp, &args);
339 if (error) {
340 msdosfs_unmount(mp, MNT_FORCE, p);
341 return error;
342 }
343
344 (void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
345 bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
346 (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
347 &size);
348 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
349 (void) msdosfs_statfs(mp, &mp->mnt_stat, p);
350#ifdef MSDOSFS_DEBUG
351 printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap);
352#endif
353 return (0);
354}
355
356static int
357mountmsdosfs(devvp, mp, p, argp)
358 struct vnode *devvp;
359 struct mount *mp;
360 struct proc *p;
361 struct msdosfs_args *argp;
362{
363 struct msdosfsmount *pmp;
364 struct buf *bp;
365 dev_t dev = devvp->v_rdev;
366#ifndef __FreeBSD__
367 struct partinfo dpart;
368 int bsize = 0, dtype = 0, tmp;
369#endif
370 union bootsector *bsp;
371 struct byte_bpb33 *b33;
372 struct byte_bpb50 *b50;
373 struct byte_bpb710 *b710;
374 u_int8_t SecPerClust;
375 int ronly, error;
376
377 /*
378 * Disallow multiple mounts of the same device.
379 * Disallow mounting of a device that is currently in use
380 * (except for root, which might share swap device for miniroot).
381 * Flush out any old buffers remaining from a previous use.
382 */
383 error = vfs_mountedon(devvp);
384 if (error)
385 return (error);
386 if (vcount(devvp) > 1 && devvp != rootvp)
387 return (EBUSY);
388 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
389 error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0);
390 VOP_UNLOCK(devvp, 0, p);
391 if (error)
392 return (error);
393
394 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
395 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
396 if (error)
397 return (error);
398
399 bp = NULL; /* both used in error_exit */
400 pmp = NULL;
401
402#ifndef __FreeBSD__
403 if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
404 /*
405 * We need the disklabel to calculate the size of a FAT entry
406 * later on. Also make sure the partition contains a filesystem
407 * of type FS_MSDOS. This doesn't work for floppies, so we have
408 * to check for them too.
409 *
410 * At least some parts of the msdos fs driver seem to assume
411 * that the size of a disk block will always be 512 bytes.
412 * Let's check it...
413 */
414 error = VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart,
415 FREAD, NOCRED, p);
416 if (error)
417 goto error_exit;
418 tmp = dpart.part->p_fstype;
419 dtype = dpart.disklab->d_type;
420 bsize = dpart.disklab->d_secsize;
421 if (bsize != 512 || (dtype!=DTYPE_FLOPPY && tmp!=FS_MSDOS)) {
422 error = EINVAL;
423 goto error_exit;
424 }
425 }
426#endif
427
428 /*
429 * Read the boot sector of the filesystem, and then check the
430 * boot signature. If not a dos boot sector then error out.
431 */
432#ifdef PC98
433 error = bread(devvp, 0, 1024, NOCRED, &bp);
434#else
435 error = bread(devvp, 0, 512, NOCRED, &bp);
436#endif
437 if (error)
438 goto error_exit;
439 bp->b_flags |= B_AGE;
440 bsp = (union bootsector *)bp->b_data;
441 b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
442 b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
443 b710 = (struct byte_bpb710 *)bsp->bs710.bsPBP;
444
445#ifndef __FreeBSD__
446 if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
447#endif
448#ifdef PC98
449 if ((bsp->bs50.bsBootSectSig0 != BOOTSIG0
450 || bsp->bs50.bsBootSectSig1 != BOOTSIG1)
451 && (bsp->bs50.bsBootSectSig0 != 0 /* PC98 DOS 3.3x */
452 || bsp->bs50.bsBootSectSig1 != 0)
453 && (bsp->bs50.bsBootSectSig0 != 0x90 /* PC98 DOS 5.0 */
454 || bsp->bs50.bsBootSectSig1 != 0x3d)
455 && (bsp->bs50.bsBootSectSig0 != 0x46 /* PC98 DOS 3.3B */
456 || bsp->bs50.bsBootSectSig1 != 0xfa)) {
457#else
458 if (bsp->bs50.bsBootSectSig0 != BOOTSIG0
459 || bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
460#endif
461 error = EINVAL;
462 goto error_exit;
463 }
464#ifndef __FreeBSD__
465 }
466#endif
467
468 pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK);
469 bzero((caddr_t)pmp, sizeof *pmp);
470 pmp->pm_mountp = mp;
471
472 /*
473 * Compute several useful quantities from the bpb in the
474 * bootsector. Copy in the dos 5 variant of the bpb then fix up
475 * the fields that are different between dos 5 and dos 3.3.
476 */
477 SecPerClust = b50->bpbSecPerClust;
478 pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
479 pmp->pm_ResSectors = getushort(b50->bpbResSectors);
480 pmp->pm_FATs = b50->bpbFATs;
481 pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
482 pmp->pm_Sectors = getushort(b50->bpbSectors);
483 pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
484 pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
485 pmp->pm_Heads = getushort(b50->bpbHeads);
486 pmp->pm_Media = b50->bpbMedia;
487
488#ifndef __FreeBSD__
489 if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
490#endif
491 /* XXX - We should probably check more values here */
492 if (!pmp->pm_BytesPerSec || !SecPerClust
493 || !pmp->pm_Heads || pmp->pm_Heads > 255
494#ifdef PC98
495 || !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 255) {
496#else
497 || !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 63) {
498#endif
499 error = EINVAL;
500 goto error_exit;
501 }
502#ifndef __FreeBSD__
503 }
504#endif
505
506 if (pmp->pm_Sectors == 0) {
507 pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
508 pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
509 } else {
510 pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
511 pmp->pm_HugeSectors = pmp->pm_Sectors;
512 }
513 if (pmp->pm_HugeSectors > 0xffffffff /
514 (pmp->pm_BytesPerSec / sizeof(struct direntry)) + 1) {
515 /*
516 * We cannot deal currently with this size of disk
517 * due to fileid limitations (see msdosfs_getattr and
518 * msdosfs_readdir)
519 */
520 error = EINVAL;
521 printf("mountmsdosfs(): disk too big, sorry\n");
522 goto error_exit;
523 }
524
525 if (pmp->pm_RootDirEnts == 0) {
526 if (bsp->bs710.bsBootSectSig2 != BOOTSIG2
527 || bsp->bs710.bsBootSectSig3 != BOOTSIG3
528 || pmp->pm_Sectors
529 || pmp->pm_FATsecs
530 || getushort(b710->bpbFSVers)) {
531 error = EINVAL;
532 printf("mountmsdosfs(): bad FAT32 filesystem\n");
533 goto error_exit;
534 }
535 pmp->pm_fatmask = FAT32_MASK;
536 pmp->pm_fatmult = 4;
537 pmp->pm_fatdiv = 1;
538 pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
539 if (getushort(b710->bpbExtFlags) & FATMIRROR)
540 pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
541 else
542 pmp->pm_flags |= MSDOSFS_FATMIRROR;
543 } else
544 pmp->pm_flags |= MSDOSFS_FATMIRROR;
545
546#ifndef __FreeBSD__
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#endif
589 pmp->pm_fatblk = pmp->pm_ResSectors;
590 if (FAT32(pmp)) {
591 pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
592 pmp->pm_firstcluster = pmp->pm_fatblk
593 + (pmp->pm_FATs * pmp->pm_FATsecs);
594 pmp->pm_fsinfo = getushort(b710->bpbFSInfo);
595 } else {
596 pmp->pm_rootdirblk = pmp->pm_fatblk +
597 (pmp->pm_FATs * pmp->pm_FATsecs);
598 pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
599 + pmp->pm_BytesPerSec - 1)
600 / pmp->pm_BytesPerSec;/* in sectors */
601 pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
602 }
603
604 pmp->pm_nmbrofclusters = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
605 SecPerClust;
606 pmp->pm_maxcluster = pmp->pm_nmbrofclusters + 1;
607 pmp->pm_fatsize = pmp->pm_FATsecs * pmp->pm_BytesPerSec;
608
609#ifndef __FreeBSD__
610 if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
611 if ((pmp->pm_nmbrofclusters <= (0xff0 - 2))
612 && ((dtype == DTYPE_FLOPPY) || ((dtype == DTYPE_VNODE)
613 && ((pmp->pm_Heads == 1) || (pmp->pm_Heads == 2))))
614 ) {
615 pmp->pm_fatmask = FAT12_MASK;
616 pmp->pm_fatmult = 3;
617 pmp->pm_fatdiv = 2;
618 } else {
619 pmp->pm_fatmask = FAT16_MASK;
620 pmp->pm_fatmult = 2;
621 pmp->pm_fatdiv = 1;
622 }
623 } else
624#endif
625 if (pmp->pm_fatmask == 0) {
626 if (pmp->pm_maxcluster
627 <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
628 /*
629 * This will usually be a floppy disk. This size makes
630 * sure that one fat entry will not be split across
631 * multiple blocks.
632 */
633 pmp->pm_fatmask = FAT12_MASK;
634 pmp->pm_fatmult = 3;
635 pmp->pm_fatdiv = 2;
636 } else {
637 pmp->pm_fatmask = FAT16_MASK;
638 pmp->pm_fatmult = 2;
639 pmp->pm_fatdiv = 1;
640 }
641 }
642 if (FAT12(pmp))
643 pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec;
644 else
645 pmp->pm_fatblocksize = DFLTBSIZE;
646
647 pmp->pm_fatblocksec = pmp->pm_fatblocksize / pmp->pm_BytesPerSec;
648 pmp->pm_bnshift = ffs(pmp->pm_BytesPerSec) - 1;
649
650 /*
651 * Compute mask and shift value for isolating cluster relative byte
652 * offsets and cluster numbers from a file offset.
653 */
654 pmp->pm_bpcluster = SecPerClust * pmp->pm_BytesPerSec;
655 pmp->pm_crbomask = pmp->pm_bpcluster - 1;
656 pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
657
658 /*
659 * Check for valid cluster size
660 * must be a power of 2
661 */
662 if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
663 error = EINVAL;
664 goto error_exit;
665 }
666
667 /*
668 * Release the bootsector buffer.
669 */
670 brelse(bp);
671 bp = NULL;
672
673 /*
674 * Check FSInfo.
675 */
676 if (pmp->pm_fsinfo) {
677 struct fsinfo *fp;
678
679 if ((error = bread(devvp, pmp->pm_fsinfo, 1024, NOCRED, &bp)) != 0)
680 goto error_exit;
681 fp = (struct fsinfo *)bp->b_data;
682 if (!bcmp(fp->fsisig1, "RRaA", 4)
683 && !bcmp(fp->fsisig2, "rrAa", 4)
684 && !bcmp(fp->fsisig3, "\0\0\125\252", 4)
685 && !bcmp(fp->fsisig4, "\0\0\125\252", 4))
686 pmp->pm_nxtfree = getulong(fp->fsinxtfree);
687 else
688 pmp->pm_fsinfo = 0;
689 brelse(bp);
690 bp = NULL;
691 }
692
693 /*
694 * Check and validate (or perhaps invalidate?) the fsinfo structure? XXX
695 */
696
697 /*
698 * Allocate memory for the bitmap of allocated clusters, and then
699 * fill it in.
700 */
701 pmp->pm_inusemap = malloc(((pmp->pm_maxcluster + N_INUSEBITS - 1)
702 / N_INUSEBITS)
703 * sizeof(*pmp->pm_inusemap),
704 M_MSDOSFSFAT, M_WAITOK);
705
706 /*
707 * fillinusemap() needs pm_devvp.
708 */
709 pmp->pm_dev = dev;
710 pmp->pm_devvp = devvp;
711
712 /*
713 * Have the inuse map filled in.
714 */
715 if ((error = fillinusemap(pmp)) != 0)
716 goto error_exit;
717
718 /*
719 * If they want fat updates to be synchronous then let them suffer
720 * the performance degradation in exchange for the on disk copy of
721 * the fat being correct just about all the time. I suppose this
722 * would be a good thing to turn on if the kernel is still flakey.
723 */
724 if (mp->mnt_flag & MNT_SYNCHRONOUS)
725 pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
726
727 /*
728 * Finish up.
729 */
730 if (ronly)
731 pmp->pm_flags |= MSDOSFSMNT_RONLY;
732 else
733 pmp->pm_fmod = 1;
734 mp->mnt_data = (qaddr_t) pmp;
735 mp->mnt_stat.f_fsid.val[0] = (long)dev;
736 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
737 mp->mnt_flag |= MNT_LOCAL;
738 devvp->v_specmountpoint = mp;
739
740 return 0;
741
742error_exit:
743 if (bp)
744 brelse(bp);
745 (void) VOP_CLOSE(devvp, ronly ? FREAD : FREAD | FWRITE, NOCRED, p);
746 if (pmp) {
747 if (pmp->pm_inusemap)
748 free(pmp->pm_inusemap, M_MSDOSFSFAT);
749 free(pmp, M_MSDOSFSMNT);
750 mp->mnt_data = (qaddr_t)0;
751 }
752 return (error);
753}
754
755static int
756msdosfs_start(mp, flags, p)
757 struct mount *mp;
758 int flags;
759 struct proc *p;
760{
761
762 return (0);
763}
764
765/*
766 * Unmount the filesystem described by mp.
767 */
768static int
769msdosfs_unmount(mp, mntflags, p)
770 struct mount *mp;
771 int mntflags;
772 struct proc *p;
773{
774 struct msdosfsmount *pmp;
775 int error, flags;
776
777 flags = 0;
778 if (mntflags & MNT_FORCE)
779 flags |= FORCECLOSE;
780 error = vflush(mp, NULLVP, flags);
781 if (error)
782 return error;
783 pmp = VFSTOMSDOSFS(mp);
784 pmp->pm_devvp->v_specmountpoint = NULL;
785#ifdef MSDOSFS_DEBUG
786 {
787 struct vnode *vp = pmp->pm_devvp;
788
789 printf("msdosfs_umount(): just before calling VOP_CLOSE()\n");
790 printf("flag %08lx, usecount %d, writecount %d, holdcnt %ld\n",
791 vp->v_flag, vp->v_usecount, vp->v_writecount, vp->v_holdcnt);
792 printf("lastr %d, id %lu, mount %p, op %p\n",
793 vp->v_lastr, vp->v_id, vp->v_mount, vp->v_op);
794 printf("freef %p, freeb %p, mount %p\n",
795 vp->v_freelist.tqe_next, vp->v_freelist.tqe_prev,
796 vp->v_mount);
797 printf("cleanblkhd %p, dirtyblkhd %p, numoutput %ld, type %d\n",
798 TAILQ_FIRST(&vp->v_cleanblkhd),
799 TAILQ_FIRST(&vp->v_dirtyblkhd),
800 vp->v_numoutput, vp->v_type);
801 printf("union %p, tag %d, data[0] %08x, data[1] %08x\n",
802 vp->v_socket, vp->v_tag,
803 ((u_int *)vp->v_data)[0],
804 ((u_int *)vp->v_data)[1]);
805 }
806#endif
807 error = VOP_CLOSE(pmp->pm_devvp,
808 (pmp->pm_flags&MSDOSFSMNT_RONLY) ? FREAD : FREAD | FWRITE,
809 NOCRED, p);
810 vrele(pmp->pm_devvp);
811 free(pmp->pm_inusemap, M_MSDOSFSFAT);
812 free(pmp, M_MSDOSFSMNT);
813 mp->mnt_data = (qaddr_t)0;
814 mp->mnt_flag &= ~MNT_LOCAL;
815 return (error);
816}
817
818static int
819msdosfs_root(mp, vpp)
820 struct mount *mp;
821 struct vnode **vpp;
822{
823 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
824 struct denode *ndep;
825 int error;
826
827#ifdef MSDOSFS_DEBUG
828 printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
829#endif
830 error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep);
831 if (error)
832 return (error);
833 *vpp = DETOV(ndep);
834 return (0);
835}
836
837static int
838msdosfs_quotactl(mp, cmds, uid, arg, p)
839 struct mount *mp;
840 int cmds;
841 uid_t uid;
842 caddr_t arg;
843 struct proc *p;
844{
845 return EOPNOTSUPP;
846}
847
848static int
849msdosfs_statfs(mp, sbp, p)
850 struct mount *mp;
851 struct statfs *sbp;
852 struct proc *p;
853{
854 struct msdosfsmount *pmp;
855
856 pmp = VFSTOMSDOSFS(mp);
857 sbp->f_bsize = pmp->pm_bpcluster;
858 sbp->f_iosize = pmp->pm_bpcluster;
859 sbp->f_blocks = pmp->pm_nmbrofclusters;
860 sbp->f_bfree = pmp->pm_freeclustercount;
861 sbp->f_bavail = pmp->pm_freeclustercount;
862 sbp->f_files = pmp->pm_RootDirEnts; /* XXX */
863 sbp->f_ffree = 0; /* what to put in here? */
864 if (sbp != &mp->mnt_stat) {
865 sbp->f_type = mp->mnt_vfc->vfc_typenum;
866 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
867 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
868 }
869 strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
870 return (0);
871}
872
873static int
874msdosfs_sync(mp, waitfor, cred, p)
875 struct mount *mp;
876 int waitfor;
877 struct ucred *cred;
878 struct proc *p;
879{
880 struct vnode *vp, *nvp;
881 struct denode *dep;
882 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
883 int error, allerror = 0;
884
885 /*
886 * If we ever switch to not updating all of the fats all the time,
887 * this would be the place to update them from the first one.
888 */
889 if (pmp->pm_fmod != 0) {
890 if (pmp->pm_flags & MSDOSFSMNT_RONLY)
891 panic("msdosfs_sync: rofs mod");
892 else {
893 /* update fats here */
894 }
895 }
896 /*
897 * Write back each (modified) denode.
898 */
899 simple_lock(&mntvnode_slock);
900loop:
901 for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
902 /*
903 * If the vnode that we are about to sync is no longer
904 * associated with this mount point, start over.
905 */
906 if (vp->v_mount != mp)
907 goto loop;
908
909 simple_lock(&vp->v_interlock);
910 nvp = vp->v_mntvnodes.le_next;
911 dep = VTODE(vp);
912 if (vp->v_type == VNON ||
913 ((dep->de_flag &
914 (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 &&
915 (TAILQ_EMPTY(&vp->v_dirtyblkhd) || waitfor == MNT_LAZY))) {
916 simple_unlock(&vp->v_interlock);
917 continue;
918 }
919 simple_unlock(&mntvnode_slock);
920 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, p);
921 if (error) {
922 simple_lock(&mntvnode_slock);
923 if (error == ENOENT)
924 goto loop;
925 continue;
926 }
927 error = VOP_FSYNC(vp, cred, waitfor, p);
928 if (error)
929 allerror = error;
930 VOP_UNLOCK(vp, 0, p);
931 vrele(vp);
932 simple_lock(&mntvnode_slock);
933 }
934 simple_unlock(&mntvnode_slock);
935
936 /*
937 * Flush filesystem control info.
938 */
939 if (waitfor != MNT_LAZY) {
940 vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY, p);
941 error = VOP_FSYNC(pmp->pm_devvp, cred, waitfor, p);
942 if (error)
943 allerror = error;
944 VOP_UNLOCK(pmp->pm_devvp, 0, p);
945 }
946 return (allerror);
947}
948
949static int
950msdosfs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
951 struct mount *mp;
952 struct fid *fhp;
953 struct sockaddr *nam;
954 struct vnode **vpp;
955 int *exflagsp;
956 struct ucred **credanonp;
957{
958 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
959 struct defid *defhp = (struct defid *) fhp;
960 struct denode *dep;
961 struct netcred *np;
962 int error;
963
964 np = vfs_export_lookup(mp, &pmp->pm_export, nam);
965 if (np == NULL)
966 return (EACCES);
967 error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep);
968 if (error) {
969 *vpp = NULLVP;
970 return (error);
971 }
972 *vpp = DETOV(dep);
973 *exflagsp = np->netc_exflags;
974 *credanonp = &np->netc_anon;
975 return (0);
976}
977
978static int
979msdosfs_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
995static int
996msdosfs_vget(mp, ino, vpp)
997 struct mount *mp;
998 ino_t ino;
999 struct vnode **vpp;
1000{
1001 return EOPNOTSUPP;
1002}
1003
1004static struct vfsops msdosfs_vfsops = {
1005 msdosfs_mount,
1006 msdosfs_start,
1007 msdosfs_unmount,
1008 msdosfs_root,
1009 msdosfs_quotactl,
1010 msdosfs_statfs,
1011 msdosfs_sync,
1012 msdosfs_vget,
1013 msdosfs_fhtovp,
1014 msdosfs_vptofh,
1015 msdosfs_init
1016};
1017
1018VFS_SET(msdosfs_vfsops, msdos, 0);