VNode sequencing and locking - part 2/4.
[dragonfly.git] / sys / vfs / msdosfs / msdosfs_vfsops.c
CommitLineData
50c068cf 1/* $FreeBSD: /usr/local/www/cvsroot/FreeBSD/src/sys/msdosfs/Attic/msdosfs_vfsops.c,v 1.60.2.8 2004/03/02 09:43:04 tjr Exp $ */
66a1ddf5 2/* $DragonFly: src/sys/vfs/msdosfs/msdosfs_vfsops.c,v 1.36 2006/07/18 22:22:15 dillon Exp $ */
984263bc
MD
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>
984263bc 55#include <sys/proc.h>
fad57d0e 56#include <sys/nlookup.h>
984263bc
MD
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
1f2de5d4
MD
66#include "bpb.h"
67#include "bootsect.h"
68#include "direntry.h"
69#include "denode.h"
70#include "msdosfsmount.h"
71#include "fat.h"
984263bc 72
66a1ddf5 73extern struct vop_ops msdosfs_vnode_vops;
0961aa92 74
984263bc
MD
75#define MSDOSFS_DFLTBSIZE 4096
76
77#if 1 /*def PC98*/
78/*
79 * XXX - The boot signature formatted by NEC PC-98 DOS looks like a
80 * garbage or a random value :-{
81 * If you want to use that broken-signatured media, define the
82 * following symbol even though PC/AT.
83 * (ex. mount PC-98 DOS formatted FD on PC/AT)
84 */
85#define MSDOSFS_NOCHECKSIG
86#endif
87
88MALLOC_DEFINE(M_MSDOSFSMNT, "MSDOSFS mount", "MSDOSFS mount structure");
89static MALLOC_DEFINE(M_MSDOSFSFAT, "MSDOSFS FAT", "MSDOSFS file allocation table");
90
a6ee311a
RG
91static int update_mp (struct mount *mp, struct msdosfs_args *argp);
92static int mountmsdosfs (struct vnode *devvp, struct mount *mp,
acde96db 93 struct msdosfs_args *argp);
a6ee311a
RG
94static int msdosfs_fhtovp (struct mount *, struct fid *,
95 struct vnode **);
96static int msdosfs_checkexp (struct mount *, struct sockaddr *,
97 int *, struct ucred **);
98static int msdosfs_mount (struct mount *, char *, caddr_t,
acde96db 99 struct ucred *);
a6ee311a
RG
100static int msdosfs_root (struct mount *, struct vnode **);
101static int msdosfs_statfs (struct mount *, struct statfs *,
acde96db 102 struct ucred *);
87de5057 103static int msdosfs_sync (struct mount *, int);
acde96db 104static int msdosfs_unmount (struct mount *, int);
a6ee311a 105static int msdosfs_vptofh (struct vnode *, struct fid *);
984263bc
MD
106
107static int
4625f023 108update_mp(struct mount *mp, struct msdosfs_args *argp)
984263bc
MD
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
08c23b99 127#ifndef __DragonFly__
984263bc
MD
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
984263bc
MD
158/*
159 * mp - path - addr in user space of mount point (ie /usr or whatever)
160 * data - addr in user space of mount params including the name of the block
161 * special file to treat as a filesystem.
162 */
163static int
acde96db 164msdosfs_mount(struct mount *mp, char *path, caddr_t data, struct ucred *cred)
984263bc
MD
165{
166 struct vnode *devvp; /* vnode for blk device to mount */
167 struct msdosfs_args args; /* will hold data from mount request */
168 /* msdosfs specific mount control block */
169 struct msdosfsmount *pmp = NULL;
170 size_t size;
171 int error, flags;
172 mode_t accessmode;
fad57d0e 173 struct nlookupdata nd;
dadab5e9 174
984263bc
MD
175 error = copyin(data, (caddr_t)&args, sizeof(struct msdosfs_args));
176 if (error)
177 return (error);
178 if (args.magic != MSDOSFS_ARGSMAGIC)
179 args.flags = 0;
180 /*
181 * If updating, check whether changing from read-only to
182 * read/write; if there is no device name, that's all we do.
183 */
184 if (mp->mnt_flag & MNT_UPDATE) {
185 pmp = VFSTOMSDOSFS(mp);
186 error = 0;
187 if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_flag & MNT_RDONLY)) {
188 flags = WRITECLOSE;
189 if (mp->mnt_flag & MNT_FORCE)
190 flags |= FORCECLOSE;
191 error = vflush(mp, 0, flags);
192 }
193 if (!error && (mp->mnt_flag & MNT_RELOAD))
194 /* not yet implemented */
195 error = EOPNOTSUPP;
196 if (error)
197 return (error);
198 if ((pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
199 /*
200 * If upgrade to read-write by non-root, then verify
201 * that user has necessary permissions on the device.
202 */
acde96db 203 if (cred->cr_uid != 0) {
984263bc 204 devvp = pmp->pm_devvp;
ca466bae 205 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
acde96db 206 error = VOP_ACCESS(devvp, VREAD | VWRITE, cred);
984263bc 207 if (error) {
ca466bae 208 VOP_UNLOCK(devvp, 0);
984263bc
MD
209 return (error);
210 }
ca466bae 211 VOP_UNLOCK(devvp, 0);
984263bc
MD
212 }
213 pmp->pm_flags &= ~MSDOSFSMNT_RONLY;
214 }
215 if (args.fspec == 0) {
216#ifdef __notyet__ /* doesn't work correctly with current mountd XXX */
217 if (args.flags & MSDOSFSMNT_MNTOPT) {
218 pmp->pm_flags &= ~MSDOSFSMNT_MNTOPT;
219 pmp->pm_flags |= args.flags & MSDOSFSMNT_MNTOPT;
220 if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
221 pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
222 }
223#endif
224 /*
225 * Process export requests.
226 */
227 return (vfs_export(mp, &pmp->pm_export, &args.export));
228 }
229 }
230 /*
231 * Not an update, or updating the name: look up the name
232 * and verify that it refers to a sensible block device.
233 */
fad57d0e
MD
234 devvp = NULL;
235 error = nlookup_init(&nd, args.fspec, UIO_USERSPACE, NLC_FOLLOW);
236 if (error == 0)
237 error = nlookup(&nd);
238 if (error == 0)
239 error = cache_vref(nd.nl_ncp, nd.nl_cred, &devvp);
240 nlookup_done(&nd);
984263bc
MD
241 if (error)
242 return (error);
984263bc
MD
243
244 if (!vn_isdisk(devvp, &error)) {
245 vrele(devvp);
246 return (error);
247 }
248 /*
249 * If mount by non-root, then verify that user has necessary
250 * permissions on the device.
251 */
acde96db 252 if (cred->cr_uid != 0) {
984263bc
MD
253 accessmode = VREAD;
254 if ((mp->mnt_flag & MNT_RDONLY) == 0)
255 accessmode |= VWRITE;
ca466bae 256 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
acde96db 257 error = VOP_ACCESS(devvp, accessmode, cred);
984263bc
MD
258 if (error) {
259 vput(devvp);
260 return (error);
261 }
ca466bae 262 VOP_UNLOCK(devvp, 0);
984263bc
MD
263 }
264 if ((mp->mnt_flag & MNT_UPDATE) == 0) {
acde96db 265 error = mountmsdosfs(devvp, mp, &args);
984263bc
MD
266#ifdef MSDOSFS_DEBUG /* only needed for the printf below */
267 pmp = VFSTOMSDOSFS(mp);
268#endif
269 } else {
270 if (devvp != pmp->pm_devvp)
271 error = EINVAL; /* XXX needs translation */
272 else
273 vrele(devvp);
274 }
275 if (error) {
276 vrele(devvp);
277 return (error);
278 }
279
280 error = update_mp(mp, &args);
281 if (error) {
acde96db 282 msdosfs_unmount(mp, MNT_FORCE);
984263bc
MD
283 return error;
284 }
285
b1ce5639 286 copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
984263bc 287 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
acde96db 288 msdosfs_statfs(mp, &mp->mnt_stat, cred);
984263bc
MD
289#ifdef MSDOSFS_DEBUG
290 printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap);
291#endif
292 return (0);
293}
294
295static int
acde96db 296mountmsdosfs(struct vnode *devvp, struct mount *mp, struct msdosfs_args *argp)
984263bc
MD
297{
298 struct msdosfsmount *pmp;
299 struct buf *bp;
e4c9c0c8 300 dev_t dev;
08c23b99 301#ifndef __DragonFly__
984263bc
MD
302 struct partinfo dpart;
303 int bsize = 0, dtype = 0, tmp;
304#endif
305 union bootsector *bsp;
306 struct byte_bpb33 *b33;
307 struct byte_bpb50 *b50;
308 struct byte_bpb710 *b710;
309 u_int8_t SecPerClust;
310 u_long clusters;
311 int ronly, error;
312
313 /*
314 * Disallow multiple mounts of the same device.
315 * Disallow mounting of a device that is currently in use
984263bc
MD
316 * Flush out any old buffers remaining from a previous use.
317 */
318 error = vfs_mountedon(devvp);
319 if (error)
320 return (error);
21739618 321 if (count_udev(devvp->v_udev) > 0)
984263bc 322 return (EBUSY);
ca466bae 323 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
87de5057 324 error = vinvalbuf(devvp, V_SAVE, 0, 0);
ca466bae 325 VOP_UNLOCK(devvp, 0);
984263bc
MD
326 if (error)
327 return (error);
328
329 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
ca466bae 330 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
87de5057 331 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, NULL);
ca466bae 332 VOP_UNLOCK(devvp, 0);
984263bc
MD
333 if (error)
334 return (error);
e4c9c0c8 335 dev = devvp->v_rdev;
984263bc
MD
336 bp = NULL; /* both used in error_exit */
337 pmp = NULL;
338
08c23b99 339#ifndef __DragonFly__
984263bc
MD
340 if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
341 /*
342 * We need the disklabel to calculate the size of a FAT entry
343 * later on. Also make sure the partition contains a filesystem
344 * of type FS_MSDOS. This doesn't work for floppies, so we have
345 * to check for them too.
346 *
347 * At least some parts of the msdos fs driver seem to assume
348 * that the size of a disk block will always be 512 bytes.
349 * Let's check it...
350 */
87de5057 351 error = VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD);
984263bc
MD
352 if (error)
353 goto error_exit;
354 tmp = dpart.part->p_fstype;
355 dtype = dpart.disklab->d_type;
356 bsize = dpart.disklab->d_secsize;
357 if (bsize != 512 || (dtype!=DTYPE_FLOPPY && tmp!=FS_MSDOS)) {
358 error = EINVAL;
359 goto error_exit;
360 }
361 }
362#endif
363
364 /*
365 * Read the boot sector of the filesystem, and then check the
366 * boot signature. If not a dos boot sector then error out.
367 *
368 * NOTE: 2048 is a maximum sector size in current...
369 */
3b568787 370 error = bread(devvp, 0, 2048, &bp);
984263bc
MD
371 if (error)
372 goto error_exit;
373 bp->b_flags |= B_AGE;
374 bsp = (union bootsector *)bp->b_data;
375 b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
376 b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
377 b710 = (struct byte_bpb710 *)bsp->bs710.bsPBP;
378
08c23b99 379#ifndef __DragonFly__
984263bc
MD
380 if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
381#endif
382#ifndef MSDOSFS_NOCHECKSIG
383 if (bsp->bs50.bsBootSectSig0 != BOOTSIG0
384 || bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
385 error = EINVAL;
386 goto error_exit;
387 }
388#endif
08c23b99 389#ifndef __DragonFly__
984263bc
MD
390 }
391#endif
392
393 pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK);
394 bzero((caddr_t)pmp, sizeof *pmp);
395 pmp->pm_mountp = mp;
396
397 /*
398 * Compute several useful quantities from the bpb in the
399 * bootsector. Copy in the dos 5 variant of the bpb then fix up
400 * the fields that are different between dos 5 and dos 3.3.
401 */
402 SecPerClust = b50->bpbSecPerClust;
403 pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
404 pmp->pm_ResSectors = getushort(b50->bpbResSectors);
405 pmp->pm_FATs = b50->bpbFATs;
406 pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
407 pmp->pm_Sectors = getushort(b50->bpbSectors);
408 pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
409 pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
410 pmp->pm_Heads = getushort(b50->bpbHeads);
411 pmp->pm_Media = b50->bpbMedia;
412
413 /* calculate the ratio of sector size to DEV_BSIZE */
414 pmp->pm_BlkPerSec = pmp->pm_BytesPerSec / DEV_BSIZE;
415
08c23b99 416#ifndef __DragonFly__
984263bc
MD
417 if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
418#endif
419 /* XXX - We should probably check more values here */
420 if (!pmp->pm_BytesPerSec || !SecPerClust
fab3e130 421 || !pmp->pm_Heads
984263bc 422 || !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 63) {
984263bc
MD
423 error = EINVAL;
424 goto error_exit;
425 }
08c23b99 426#ifndef __DragonFly__
984263bc
MD
427 }
428#endif
429
430 if (pmp->pm_Sectors == 0) {
431 pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
432 pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
433 } else {
434 pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
435 pmp->pm_HugeSectors = pmp->pm_Sectors;
436 }
437 if (pmp->pm_HugeSectors > 0xffffffff /
438 (pmp->pm_BytesPerSec / sizeof(struct direntry)) + 1) {
439 /*
440 * We cannot deal currently with this size of disk
441 * due to fileid limitations (see msdosfs_getattr and
442 * msdosfs_readdir)
443 */
444 error = EINVAL;
445 printf("mountmsdosfs(): disk too big, sorry\n");
446 goto error_exit;
447 }
448
449 if (pmp->pm_RootDirEnts == 0) {
450 if (bsp->bs710.bsBootSectSig2 != BOOTSIG2
451 || bsp->bs710.bsBootSectSig3 != BOOTSIG3
452 || pmp->pm_Sectors
453 || pmp->pm_FATsecs
454 || getushort(b710->bpbFSVers)) {
455 error = EINVAL;
456 printf("mountmsdosfs(): bad FAT32 filesystem\n");
457 goto error_exit;
458 }
459 pmp->pm_fatmask = FAT32_MASK;
460 pmp->pm_fatmult = 4;
461 pmp->pm_fatdiv = 1;
462 pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
463 if (getushort(b710->bpbExtFlags) & FATMIRROR)
464 pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
465 else
466 pmp->pm_flags |= MSDOSFS_FATMIRROR;
467 } else
468 pmp->pm_flags |= MSDOSFS_FATMIRROR;
469
470 /*
471 * Check a few values (could do some more):
472 * - logical sector size: power of 2, >= block size
473 * - sectors per cluster: power of 2, >= 1
474 * - number of sectors: >= 1, <= size of partition
475 */
476 if ( (SecPerClust == 0)
477 || (SecPerClust & (SecPerClust - 1))
478 || (pmp->pm_BytesPerSec < DEV_BSIZE)
479 || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
480 || (pmp->pm_HugeSectors == 0)
481 ) {
482 error = EINVAL;
483 goto error_exit;
484 }
485
486 pmp->pm_HugeSectors *= pmp->pm_BlkPerSec;
487 pmp->pm_HiddenSects *= pmp->pm_BlkPerSec; /* XXX not used? */
488 pmp->pm_FATsecs *= pmp->pm_BlkPerSec;
489 SecPerClust *= pmp->pm_BlkPerSec;
490
491 pmp->pm_fatblk = pmp->pm_ResSectors * pmp->pm_BlkPerSec;
492
493 if (FAT32(pmp)) {
494 pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
495 pmp->pm_firstcluster = pmp->pm_fatblk
496 + (pmp->pm_FATs * pmp->pm_FATsecs);
497 pmp->pm_fsinfo = getushort(b710->bpbFSInfo) * pmp->pm_BlkPerSec;
498 } else {
499 pmp->pm_rootdirblk = pmp->pm_fatblk +
500 (pmp->pm_FATs * pmp->pm_FATsecs);
501 pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
502 + DEV_BSIZE - 1)
503 / DEV_BSIZE; /* in blocks */
504 pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
505 }
506
507 pmp->pm_maxcluster = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
508 SecPerClust + 1;
509 pmp->pm_fatsize = pmp->pm_FATsecs * DEV_BSIZE; /* XXX not used? */
510
08c23b99 511#ifndef __DragonFly__
984263bc
MD
512 if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
513 if ((pmp->pm_maxcluster <= (0xff0 - 2))
514 && ((dtype == DTYPE_FLOPPY) || ((dtype == DTYPE_VNODE)
515 && ((pmp->pm_Heads == 1) || (pmp->pm_Heads == 2))))
516 ) {
517 pmp->pm_fatmask = FAT12_MASK;
518 pmp->pm_fatmult = 3;
519 pmp->pm_fatdiv = 2;
520 } else {
521 pmp->pm_fatmask = FAT16_MASK;
522 pmp->pm_fatmult = 2;
523 pmp->pm_fatdiv = 1;
524 }
525 } else
526#endif
527 if (pmp->pm_fatmask == 0) {
528 if (pmp->pm_maxcluster
529 <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
530 /*
531 * This will usually be a floppy disk. This size makes
532 * sure that one fat entry will not be split across
533 * multiple blocks.
534 */
535 pmp->pm_fatmask = FAT12_MASK;
536 pmp->pm_fatmult = 3;
537 pmp->pm_fatdiv = 2;
538 } else {
539 pmp->pm_fatmask = FAT16_MASK;
540 pmp->pm_fatmult = 2;
541 pmp->pm_fatdiv = 1;
542 }
543 }
544
545 clusters = (pmp->pm_fatsize / pmp->pm_fatmult) * pmp->pm_fatdiv;
546 if (pmp->pm_maxcluster >= clusters) {
547 printf("Warning: number of clusters (%ld) exceeds FAT "
548 "capacity (%ld)\n", pmp->pm_maxcluster + 1, clusters);
549 pmp->pm_maxcluster = clusters - 1;
550 }
551
552
553 if (FAT12(pmp))
554 pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec;
555 else
556 pmp->pm_fatblocksize = MSDOSFS_DFLTBSIZE;
557
558 pmp->pm_fatblocksec = pmp->pm_fatblocksize / DEV_BSIZE;
54078292 559 pmp->pm_bnshift = DEV_BSHIFT;
984263bc
MD
560
561 /*
562 * Compute mask and shift value for isolating cluster relative byte
563 * offsets and cluster numbers from a file offset.
564 */
565 pmp->pm_bpcluster = SecPerClust * DEV_BSIZE;
566 pmp->pm_crbomask = pmp->pm_bpcluster - 1;
567 pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
568
569 /*
570 * Check for valid cluster size
571 * must be a power of 2
572 */
573 if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
574 error = EINVAL;
575 goto error_exit;
576 }
577
578 /*
579 * Release the bootsector buffer.
580 */
581 brelse(bp);
582 bp = NULL;
583
584 /*
585 * Check FSInfo.
586 */
587 if (pmp->pm_fsinfo) {
588 struct fsinfo *fp;
589
54078292 590 if ((error = bread(devvp, de_bntodoff(pmp, pmp->pm_fsinfo), fsi_size(pmp), &bp)) != 0)
984263bc
MD
591 goto error_exit;
592 fp = (struct fsinfo *)bp->b_data;
593 if (!bcmp(fp->fsisig1, "RRaA", 4)
594 && !bcmp(fp->fsisig2, "rrAa", 4)
595 && !bcmp(fp->fsisig3, "\0\0\125\252", 4)
50c068cf 596 && !bcmp(fp->fsisig4, "\0\0\125\252", 4)) {
984263bc 597 pmp->pm_nxtfree = getulong(fp->fsinxtfree);
50c068cf
CP
598 if (pmp->pm_nxtfree == 0xffffffff)
599 pmp->pm_nxtfree = CLUST_FIRST;
600 } else
984263bc
MD
601 pmp->pm_fsinfo = 0;
602 brelse(bp);
603 bp = NULL;
604 }
605
606 /*
607 * Check and validate (or perhaps invalidate?) the fsinfo structure?
608 */
609 if (pmp->pm_fsinfo && pmp->pm_nxtfree > pmp->pm_maxcluster) {
610 printf(
611 "Next free cluster in FSInfo (%lu) exceeds maxcluster (%lu)\n",
612 pmp->pm_nxtfree, pmp->pm_maxcluster);
613 error = EINVAL;
614 goto error_exit;
615 }
616
617 /*
618 * Allocate memory for the bitmap of allocated clusters, and then
619 * fill it in.
620 */
621 pmp->pm_inusemap = malloc(((pmp->pm_maxcluster + N_INUSEBITS - 1)
622 / N_INUSEBITS)
623 * sizeof(*pmp->pm_inusemap),
624 M_MSDOSFSFAT, M_WAITOK);
625
626 /*
627 * fillinusemap() needs pm_devvp.
628 */
629 pmp->pm_dev = dev;
630 pmp->pm_devvp = devvp;
631
632 /*
633 * Have the inuse map filled in.
634 */
635 if ((error = fillinusemap(pmp)) != 0)
636 goto error_exit;
637
638 /*
639 * If they want fat updates to be synchronous then let them suffer
640 * the performance degradation in exchange for the on disk copy of
641 * the fat being correct just about all the time. I suppose this
642 * would be a good thing to turn on if the kernel is still flakey.
643 */
644 if (mp->mnt_flag & MNT_SYNCHRONOUS)
645 pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
646
647 /*
648 * Finish up.
649 */
650 if (ronly)
651 pmp->pm_flags |= MSDOSFSMNT_RONLY;
652 else
653 pmp->pm_fmod = 1;
654 mp->mnt_data = (qaddr_t) pmp;
655 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
656 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
657 mp->mnt_flag |= MNT_LOCAL;
66a1ddf5 658 vfs_add_vnodeops(mp, &msdosfs_vnode_vops, &mp->mnt_vn_norm_ops);
e4c9c0c8 659 dev->si_mountpoint = mp;
984263bc
MD
660
661 return 0;
662
663error_exit:
664 if (bp)
665 brelse(bp);
87de5057 666 VOP_CLOSE(devvp, ronly ? FREAD : FREAD | FWRITE);
984263bc
MD
667 if (pmp) {
668 if (pmp->pm_inusemap)
669 free(pmp->pm_inusemap, M_MSDOSFSFAT);
670 free(pmp, M_MSDOSFSMNT);
671 mp->mnt_data = (qaddr_t)0;
672 }
673 return (error);
674}
675
676/*
677 * Unmount the filesystem described by mp.
678 */
679static int
acde96db 680msdosfs_unmount(struct mount *mp, int mntflags)
984263bc
MD
681{
682 struct msdosfsmount *pmp;
683 int error, flags;
684
685 flags = 0;
686 if (mntflags & MNT_FORCE)
687 flags |= FORCECLOSE;
688 error = vflush(mp, 0, flags);
689 if (error)
690 return error;
691 pmp = VFSTOMSDOSFS(mp);
e4c9c0c8 692 pmp->pm_devvp->v_rdev->si_mountpoint = NULL;
984263bc
MD
693#ifdef MSDOSFS_DEBUG
694 {
695 struct vnode *vp = pmp->pm_devvp;
696
697 printf("msdosfs_umount(): just before calling VOP_CLOSE()\n");
698 printf("flag %08lx, usecount %d, writecount %d, holdcnt %ld\n",
699 vp->v_flag, vp->v_usecount, vp->v_writecount, vp->v_holdcnt);
8a66aac5 700 printf("mount %p, op %p\n", vp->v_mount, vp->v_op);
984263bc 701 printf("freef %p, freeb %p, mount %p\n",
ecd80f47 702 TAILQ_NEXT(vp, v_freelist), TAILQ_PREV(vp, v_freelist),
984263bc
MD
703 vp->v_mount);
704 printf("cleanblkhd %p, dirtyblkhd %p, numoutput %ld, type %d\n",
6bae6177
MD
705 RB_EMPTY(&vp->v_rbclean_tree),
706 RB_EMPTY(&vp->v_rbdirty_tree),
81b5c339 707 vp->v_track_write.bk_active, vp->v_type);
984263bc
MD
708 printf("union %p, tag %d, data[0] %08x, data[1] %08x\n",
709 vp->v_socket, vp->v_tag,
710 ((u_int *)vp->v_data)[0],
711 ((u_int *)vp->v_data)[1]);
712 }
713#endif
714 error = VOP_CLOSE(pmp->pm_devvp,
87de5057 715 (pmp->pm_flags&MSDOSFSMNT_RONLY) ? FREAD : FREAD | FWRITE);
984263bc
MD
716 vrele(pmp->pm_devvp);
717 free(pmp->pm_inusemap, M_MSDOSFSFAT);
718 free(pmp, M_MSDOSFSMNT);
719 mp->mnt_data = (qaddr_t)0;
720 mp->mnt_flag &= ~MNT_LOCAL;
721 return (error);
722}
723
724static int
4625f023 725msdosfs_root(struct mount *mp, struct vnode **vpp)
984263bc
MD
726{
727 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
728 struct denode *ndep;
729 int error;
730
731#ifdef MSDOSFS_DEBUG
732 printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
733#endif
734 error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep);
735 if (error)
736 return (error);
737 *vpp = DETOV(ndep);
738 return (0);
739}
740
741static int
acde96db 742msdosfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
984263bc
MD
743{
744 struct msdosfsmount *pmp;
745
746 pmp = VFSTOMSDOSFS(mp);
747 sbp->f_bsize = pmp->pm_bpcluster;
748 sbp->f_iosize = pmp->pm_bpcluster;
749 sbp->f_blocks = pmp->pm_maxcluster + 1;
750 sbp->f_bfree = pmp->pm_freeclustercount;
751 sbp->f_bavail = pmp->pm_freeclustercount;
752 sbp->f_files = pmp->pm_RootDirEnts; /* XXX */
753 sbp->f_ffree = 0; /* what to put in here? */
754 if (sbp != &mp->mnt_stat) {
755 sbp->f_type = mp->mnt_vfc->vfc_typenum;
984263bc
MD
756 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
757 }
758 strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
759 return (0);
760}
761
41a01a4d
MD
762struct scaninfo {
763 int rescan;
764 int allerror;
765 int waitfor;
41a01a4d
MD
766};
767
5fd012e0 768static int msdosfs_sync_scan(struct mount *mp, struct vnode *vp, void *data);
41a01a4d 769
984263bc 770static int
87de5057 771msdosfs_sync(struct mount *mp, int waitfor)
984263bc 772{
984263bc 773 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
41a01a4d
MD
774 struct scaninfo scaninfo;
775 int error;
984263bc
MD
776
777 /*
778 * If we ever switch to not updating all of the fats all the time,
779 * this would be the place to update them from the first one.
780 */
781 if (pmp->pm_fmod != 0) {
782 if (pmp->pm_flags & MSDOSFSMNT_RONLY)
783 panic("msdosfs_sync: rofs mod");
784 else {
785 /* update fats here */
786 }
787 }
788 /*
789 * Write back each (modified) denode.
790 */
41a01a4d
MD
791 scaninfo.allerror = 0;
792 scaninfo.rescan = 1;
41a01a4d
MD
793 while (scaninfo.rescan) {
794 scaninfo.rescan = 0;
5fd012e0 795 vmntvnodescan(mp, VMSC_GETVP|VMSC_NOWAIT, NULL, msdosfs_sync_scan, &scaninfo);
984263bc 796 }
984263bc
MD
797
798 /*
799 * Flush filesystem control info.
800 */
801 if (waitfor != MNT_LAZY) {
ca466bae 802 vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY);
87de5057 803 if ((error = VOP_FSYNC(pmp->pm_devvp, waitfor)) != 0)
41a01a4d 804 scaninfo.allerror = error;
ca466bae 805 VOP_UNLOCK(pmp->pm_devvp, 0);
41a01a4d
MD
806 }
807 return (scaninfo.allerror);
808}
809
4625f023 810static int
5fd012e0 811msdosfs_sync_scan(struct mount *mp, struct vnode *vp, void *data)
41a01a4d
MD
812{
813 struct scaninfo *info = data;
814 struct denode *dep;
815 int error;
816
817 dep = VTODE(vp);
818 if (vp->v_type == VNON ||
819 ((dep->de_flag &
820 (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 &&
6bae6177 821 (RB_EMPTY(&vp->v_rbdirty_tree) || info->waitfor == MNT_LAZY))) {
41a01a4d 822 return(0);
984263bc 823 }
87de5057 824 if ((error = VOP_FSYNC(vp, info->waitfor)) != 0)
41a01a4d 825 info->allerror = error;
41a01a4d 826 return(0);
984263bc
MD
827}
828
829static int
4625f023 830msdosfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
984263bc
MD
831{
832 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
833 struct defid *defhp = (struct defid *) fhp;
834 struct denode *dep;
835 int error;
836
837 error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep);
838 if (error) {
839 *vpp = NULLVP;
840 return (error);
841 }
842 *vpp = DETOV(dep);
843 return (0);
844}
845
846static int
4625f023
CP
847msdosfs_checkexp(struct mount *mp, struct sockaddr *nam, int *exflagsp,
848 struct ucred **credanonp)
984263bc
MD
849{
850 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
851 struct netcred *np;
852
853 np = vfs_export_lookup(mp, &pmp->pm_export, nam);
854 if (np == NULL)
855 return (EACCES);
856 *exflagsp = np->netc_exflags;
857 *credanonp = &np->netc_anon;
858 return (0);
859}
860
861static int
4625f023 862msdosfs_vptofh(struct vnode *vp, struct fid *fhp)
984263bc
MD
863{
864 struct denode *dep;
865 struct defid *defhp;
866
867 dep = VTODE(vp);
868 defhp = (struct defid *)fhp;
869 defhp->defid_len = sizeof(struct defid);
870 defhp->defid_dirclust = dep->de_dirclust;
871 defhp->defid_dirofs = dep->de_diroffset;
872 /* defhp->defid_gen = dep->de_gen; */
873 return (0);
874}
875
876static struct vfsops msdosfs_vfsops = {
43c45e8f
HP
877 .vfs_mount = msdosfs_mount,
878 .vfs_unmount = msdosfs_unmount,
879 .vfs_root = msdosfs_root,
880 .vfs_statfs = msdosfs_statfs,
881 .vfs_sync = msdosfs_sync,
882 .vfs_fhtovp = msdosfs_fhtovp,
883 .vfs_checkexp = msdosfs_checkexp,
884 .vfs_vptofh = msdosfs_vptofh,
885 .vfs_init = msdosfs_init,
886 .vfs_uninit = msdosfs_uninit
984263bc
MD
887};
888
889VFS_SET(msdosfs_vfsops, msdos, 0);