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