proc->thread stage 4: rework the VFS and DEVICE subsystems to take thread
[dragonfly.git] / sys / vfs / msdosfs / msdosfs_vfsops.c
CommitLineData
984263bc 1/* $FreeBSD: src/sys/msdosfs/msdosfs_vfsops.c,v 1.60.2.6 2002/09/12 21:33:38 trhodes Exp $ */
dadab5e9 2/* $DragonFly: src/sys/vfs/msdosfs/msdosfs_vfsops.c,v 1.3 2003/06/25 03:56:01 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>
dadab5e9 56#include <sys/namei.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
66#include <msdosfs/bpb.h>
67#include <msdosfs/bootsect.h>
68#include <msdosfs/direntry.h>
69#include <msdosfs/denode.h>
70#include <msdosfs/msdosfsmount.h>
71#include <msdosfs/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 __P((struct mount *mp, struct msdosfs_args *argp));
90static int mountmsdosfs __P((struct vnode *devvp, struct mount *mp,
dadab5e9 91 struct thread *td, struct msdosfs_args *argp));
984263bc
MD
92static int msdosfs_fhtovp __P((struct mount *, struct fid *,
93 struct vnode **));
94static int msdosfs_checkexp __P((struct mount *, struct sockaddr *,
95 int *, struct ucred **));
96static int msdosfs_mount __P((struct mount *, char *, caddr_t,
dadab5e9 97 struct nameidata *, struct thread *));
984263bc
MD
98static int msdosfs_root __P((struct mount *, struct vnode **));
99static int msdosfs_statfs __P((struct mount *, struct statfs *,
dadab5e9 100 struct thread *));
984263bc 101static int msdosfs_sync __P((struct mount *, int, struct ucred *,
dadab5e9
MD
102 struct thread *));
103static int msdosfs_unmount __P((struct mount *, int, struct thread *));
984263bc
MD
104static int msdosfs_vptofh __P((struct vnode *, struct fid *));
105
106static int
107update_mp(mp, argp)
108 struct mount *mp;
109 struct msdosfs_args *argp;
110{
111 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
112 int error;
113
114 pmp->pm_gid = argp->gid;
115 pmp->pm_uid = argp->uid;
116 pmp->pm_mask = argp->mask & ALLPERMS;
117 pmp->pm_flags |= argp->flags & MSDOSFSMNT_MNTOPT;
118 if (pmp->pm_flags & MSDOSFSMNT_U2WTABLE) {
119 bcopy(argp->u2w, pmp->pm_u2w, sizeof(pmp->pm_u2w));
120 bcopy(argp->d2u, pmp->pm_d2u, sizeof(pmp->pm_d2u));
121 bcopy(argp->u2d, pmp->pm_u2d, sizeof(pmp->pm_u2d));
122 }
123 if (pmp->pm_flags & MSDOSFSMNT_ULTABLE) {
124 bcopy(argp->ul, pmp->pm_ul, sizeof(pmp->pm_ul));
125 bcopy(argp->lu, pmp->pm_lu, sizeof(pmp->pm_lu));
126 }
127
128#ifndef __FreeBSD__
129 /*
130 * GEMDOS knows nothing (yet) about win95
131 */
132 if (pmp->pm_flags & MSDOSFSMNT_GEMDOSFS)
133 pmp->pm_flags |= MSDOSFSMNT_NOWIN95;
134#endif
135
136 if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
137 pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
138 else if (!(pmp->pm_flags &
139 (MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) {
140 struct vnode *rootvp;
141
142 /*
143 * Try to divine whether to support Win'95 long filenames
144 */
145 if (FAT32(pmp))
146 pmp->pm_flags |= MSDOSFSMNT_LONGNAME;
147 else {
148 if ((error = msdosfs_root(mp, &rootvp)) != 0)
149 return error;
150 pmp->pm_flags |= findwin95(VTODE(rootvp))
151 ? MSDOSFSMNT_LONGNAME
152 : MSDOSFSMNT_SHORTNAME;
153 vput(rootvp);
154 }
155 }
156 return 0;
157}
158
159#ifndef __FreeBSD__
160int
161msdosfs_mountroot()
162{
163 register struct mount *mp;
dadab5e9 164 struct thread *td = curthread; /* XXX */
984263bc
MD
165 size_t size;
166 int error;
167 struct msdosfs_args args;
168
169 if (root_device->dv_class != DV_DISK)
170 return (ENODEV);
171
172 /*
173 * Get vnodes for swapdev and rootdev.
174 */
175 if (bdevvp(rootdev, &rootvp))
176 panic("msdosfs_mountroot: can't setup rootvp");
177
178 mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK);
179 bzero((char *)mp, (u_long)sizeof(struct mount));
180 mp->mnt_op = &msdosfs_vfsops;
181 mp->mnt_flag = 0;
182 TAILQ_INIT(&mp->mnt_nvnodelist);
183 TAILQ_INIT(&mp->mnt_reservedvnlist);
184
185 args.flags = 0;
186 args.uid = 0;
187 args.gid = 0;
188 args.mask = 0777;
189
190 if ((error = mountmsdosfs(rootvp, mp, p, &args)) != 0) {
191 free(mp, M_MOUNT);
192 return (error);
193 }
194
195 if ((error = update_mp(mp, &args)) != 0) {
196 (void)msdosfs_unmount(mp, 0, p);
197 free(mp, M_MOUNT);
198 return (error);
199 }
200
201 if ((error = vfs_lock(mp)) != 0) {
202 (void)msdosfs_unmount(mp, 0, p);
203 free(mp, M_MOUNT);
204 return (error);
205 }
206
207 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
208 mp->mnt_vnodecovered = NULLVP;
209 (void) copystr("/", mp->mnt_stat.f_mntonname, MNAMELEN - 1,
210 &size);
211 bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
212 (void) copystr(ROOTNAME, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
213 &size);
214 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
215 (void)msdosfs_statfs(mp, &mp->mnt_stat, p);
216 vfs_unlock(mp);
217 return (0);
218}
219#endif
220
221/*
222 * mp - path - addr in user space of mount point (ie /usr or whatever)
223 * data - addr in user space of mount params including the name of the block
224 * special file to treat as a filesystem.
225 */
226static int
dadab5e9 227msdosfs_mount(mp, path, data, ndp, td)
984263bc
MD
228 struct mount *mp;
229 char *path;
230 caddr_t data;
231 struct nameidata *ndp;
dadab5e9 232 struct thread *td;
984263bc
MD
233{
234 struct vnode *devvp; /* vnode for blk device to mount */
235 struct msdosfs_args args; /* will hold data from mount request */
236 /* msdosfs specific mount control block */
237 struct msdosfsmount *pmp = NULL;
238 size_t size;
239 int error, flags;
240 mode_t accessmode;
dadab5e9
MD
241 struct proc *p = td->td_proc;
242
243 KKASSERT(p);
984263bc
MD
244
245 error = copyin(data, (caddr_t)&args, sizeof(struct msdosfs_args));
246 if (error)
247 return (error);
248 if (args.magic != MSDOSFS_ARGSMAGIC)
249 args.flags = 0;
250 /*
251 * If updating, check whether changing from read-only to
252 * read/write; if there is no device name, that's all we do.
253 */
254 if (mp->mnt_flag & MNT_UPDATE) {
255 pmp = VFSTOMSDOSFS(mp);
256 error = 0;
257 if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_flag & MNT_RDONLY)) {
258 flags = WRITECLOSE;
259 if (mp->mnt_flag & MNT_FORCE)
260 flags |= FORCECLOSE;
261 error = vflush(mp, 0, flags);
262 }
263 if (!error && (mp->mnt_flag & MNT_RELOAD))
264 /* not yet implemented */
265 error = EOPNOTSUPP;
266 if (error)
267 return (error);
268 if ((pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
269 /*
270 * If upgrade to read-write by non-root, then verify
271 * that user has necessary permissions on the device.
272 */
273 if (p->p_ucred->cr_uid != 0) {
274 devvp = pmp->pm_devvp;
dadab5e9 275 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
984263bc 276 error = VOP_ACCESS(devvp, VREAD | VWRITE,
dadab5e9 277 p->p_ucred, td);
984263bc 278 if (error) {
dadab5e9 279 VOP_UNLOCK(devvp, 0, td);
984263bc
MD
280 return (error);
281 }
dadab5e9 282 VOP_UNLOCK(devvp, 0, td);
984263bc
MD
283 }
284 pmp->pm_flags &= ~MSDOSFSMNT_RONLY;
285 }
286 if (args.fspec == 0) {
287#ifdef __notyet__ /* doesn't work correctly with current mountd XXX */
288 if (args.flags & MSDOSFSMNT_MNTOPT) {
289 pmp->pm_flags &= ~MSDOSFSMNT_MNTOPT;
290 pmp->pm_flags |= args.flags & MSDOSFSMNT_MNTOPT;
291 if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
292 pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
293 }
294#endif
295 /*
296 * Process export requests.
297 */
298 return (vfs_export(mp, &pmp->pm_export, &args.export));
299 }
300 }
301 /*
302 * Not an update, or updating the name: look up the name
303 * and verify that it refers to a sensible block device.
304 */
dadab5e9 305 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, td);
984263bc
MD
306 error = namei(ndp);
307 if (error)
308 return (error);
309 devvp = ndp->ni_vp;
310 NDFREE(ndp, NDF_ONLY_PNBUF);
311
312 if (!vn_isdisk(devvp, &error)) {
313 vrele(devvp);
314 return (error);
315 }
316 /*
317 * If mount by non-root, then verify that user has necessary
318 * permissions on the device.
319 */
320 if (p->p_ucred->cr_uid != 0) {
321 accessmode = VREAD;
322 if ((mp->mnt_flag & MNT_RDONLY) == 0)
323 accessmode |= VWRITE;
dadab5e9
MD
324 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
325 error = VOP_ACCESS(devvp, accessmode, p->p_ucred, td);
984263bc
MD
326 if (error) {
327 vput(devvp);
328 return (error);
329 }
dadab5e9 330 VOP_UNLOCK(devvp, 0, td);
984263bc
MD
331 }
332 if ((mp->mnt_flag & MNT_UPDATE) == 0) {
dadab5e9 333 error = mountmsdosfs(devvp, mp, td, &args);
984263bc
MD
334#ifdef MSDOSFS_DEBUG /* only needed for the printf below */
335 pmp = VFSTOMSDOSFS(mp);
336#endif
337 } else {
338 if (devvp != pmp->pm_devvp)
339 error = EINVAL; /* XXX needs translation */
340 else
341 vrele(devvp);
342 }
343 if (error) {
344 vrele(devvp);
345 return (error);
346 }
347
348 error = update_mp(mp, &args);
349 if (error) {
dadab5e9 350 msdosfs_unmount(mp, MNT_FORCE, td);
984263bc
MD
351 return error;
352 }
353
354 (void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
355 bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
356 (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
357 &size);
358 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
dadab5e9 359 (void) msdosfs_statfs(mp, &mp->mnt_stat, td);
984263bc
MD
360#ifdef MSDOSFS_DEBUG
361 printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap);
362#endif
363 return (0);
364}
365
366static int
dadab5e9 367mountmsdosfs(devvp, mp, td, argp)
984263bc
MD
368 struct vnode *devvp;
369 struct mount *mp;
dadab5e9 370 struct thread *td;
984263bc
MD
371 struct msdosfs_args *argp;
372{
373 struct msdosfsmount *pmp;
374 struct buf *bp;
375 dev_t dev = devvp->v_rdev;
376#ifndef __FreeBSD__
377 struct partinfo dpart;
378 int bsize = 0, dtype = 0, tmp;
379#endif
380 union bootsector *bsp;
381 struct byte_bpb33 *b33;
382 struct byte_bpb50 *b50;
383 struct byte_bpb710 *b710;
384 u_int8_t SecPerClust;
385 u_long clusters;
386 int ronly, error;
dadab5e9
MD
387 struct proc *p = td->td_proc;
388
389 KKASSERT(p);
984263bc
MD
390
391 /*
392 * Disallow multiple mounts of the same device.
393 * Disallow mounting of a device that is currently in use
394 * (except for root, which might share swap device for miniroot).
395 * Flush out any old buffers remaining from a previous use.
396 */
397 error = vfs_mountedon(devvp);
398 if (error)
399 return (error);
400 if (vcount(devvp) > 1 && devvp != rootvp)
401 return (EBUSY);
dadab5e9
MD
402 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
403 error = vinvalbuf(devvp, V_SAVE, p->p_ucred, td, 0, 0);
404 VOP_UNLOCK(devvp, 0, td);
984263bc
MD
405 if (error)
406 return (error);
407
408 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
dadab5e9
MD
409 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
410 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, td);
411 VOP_UNLOCK(devvp, 0, td);
984263bc
MD
412 if (error)
413 return (error);
414
415 bp = NULL; /* both used in error_exit */
416 pmp = NULL;
417
418#ifndef __FreeBSD__
419 if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
420 /*
421 * We need the disklabel to calculate the size of a FAT entry
422 * later on. Also make sure the partition contains a filesystem
423 * of type FS_MSDOS. This doesn't work for floppies, so we have
424 * to check for them too.
425 *
426 * At least some parts of the msdos fs driver seem to assume
427 * that the size of a disk block will always be 512 bytes.
428 * Let's check it...
429 */
430 error = VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart,
dadab5e9 431 FREAD, NOCRED, td);
984263bc
MD
432 if (error)
433 goto error_exit;
434 tmp = dpart.part->p_fstype;
435 dtype = dpart.disklab->d_type;
436 bsize = dpart.disklab->d_secsize;
437 if (bsize != 512 || (dtype!=DTYPE_FLOPPY && tmp!=FS_MSDOS)) {
438 error = EINVAL;
439 goto error_exit;
440 }
441 }
442#endif
443
444 /*
445 * Read the boot sector of the filesystem, and then check the
446 * boot signature. If not a dos boot sector then error out.
447 *
448 * NOTE: 2048 is a maximum sector size in current...
449 */
450 error = bread(devvp, 0, 2048, NOCRED, &bp);
451 if (error)
452 goto error_exit;
453 bp->b_flags |= B_AGE;
454 bsp = (union bootsector *)bp->b_data;
455 b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
456 b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
457 b710 = (struct byte_bpb710 *)bsp->bs710.bsPBP;
458
459#ifndef __FreeBSD__
460 if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
461#endif
462#ifndef MSDOSFS_NOCHECKSIG
463 if (bsp->bs50.bsBootSectSig0 != BOOTSIG0
464 || bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
465 error = EINVAL;
466 goto error_exit;
467 }
468#endif
469#ifndef __FreeBSD__
470 }
471#endif
472
473 pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK);
474 bzero((caddr_t)pmp, sizeof *pmp);
475 pmp->pm_mountp = mp;
476
477 /*
478 * Compute several useful quantities from the bpb in the
479 * bootsector. Copy in the dos 5 variant of the bpb then fix up
480 * the fields that are different between dos 5 and dos 3.3.
481 */
482 SecPerClust = b50->bpbSecPerClust;
483 pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
484 pmp->pm_ResSectors = getushort(b50->bpbResSectors);
485 pmp->pm_FATs = b50->bpbFATs;
486 pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
487 pmp->pm_Sectors = getushort(b50->bpbSectors);
488 pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
489 pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
490 pmp->pm_Heads = getushort(b50->bpbHeads);
491 pmp->pm_Media = b50->bpbMedia;
492
493 /* calculate the ratio of sector size to DEV_BSIZE */
494 pmp->pm_BlkPerSec = pmp->pm_BytesPerSec / DEV_BSIZE;
495
496#ifndef __FreeBSD__
497 if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
498#endif
499 /* XXX - We should probably check more values here */
500 if (!pmp->pm_BytesPerSec || !SecPerClust
501 || !pmp->pm_Heads || pmp->pm_Heads > 255
502#ifdef PC98
503 || !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 255) {
504#else
505 || !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 63) {
506#endif
507 error = EINVAL;
508 goto error_exit;
509 }
510#ifndef __FreeBSD__
511 }
512#endif
513
514 if (pmp->pm_Sectors == 0) {
515 pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
516 pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
517 } else {
518 pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
519 pmp->pm_HugeSectors = pmp->pm_Sectors;
520 }
521 if (pmp->pm_HugeSectors > 0xffffffff /
522 (pmp->pm_BytesPerSec / sizeof(struct direntry)) + 1) {
523 /*
524 * We cannot deal currently with this size of disk
525 * due to fileid limitations (see msdosfs_getattr and
526 * msdosfs_readdir)
527 */
528 error = EINVAL;
529 printf("mountmsdosfs(): disk too big, sorry\n");
530 goto error_exit;
531 }
532
533 if (pmp->pm_RootDirEnts == 0) {
534 if (bsp->bs710.bsBootSectSig2 != BOOTSIG2
535 || bsp->bs710.bsBootSectSig3 != BOOTSIG3
536 || pmp->pm_Sectors
537 || pmp->pm_FATsecs
538 || getushort(b710->bpbFSVers)) {
539 error = EINVAL;
540 printf("mountmsdosfs(): bad FAT32 filesystem\n");
541 goto error_exit;
542 }
543 pmp->pm_fatmask = FAT32_MASK;
544 pmp->pm_fatmult = 4;
545 pmp->pm_fatdiv = 1;
546 pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
547 if (getushort(b710->bpbExtFlags) & FATMIRROR)
548 pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
549 else
550 pmp->pm_flags |= MSDOSFS_FATMIRROR;
551 } else
552 pmp->pm_flags |= MSDOSFS_FATMIRROR;
553
554 /*
555 * Check a few values (could do some more):
556 * - logical sector size: power of 2, >= block size
557 * - sectors per cluster: power of 2, >= 1
558 * - number of sectors: >= 1, <= size of partition
559 */
560 if ( (SecPerClust == 0)
561 || (SecPerClust & (SecPerClust - 1))
562 || (pmp->pm_BytesPerSec < DEV_BSIZE)
563 || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
564 || (pmp->pm_HugeSectors == 0)
565 ) {
566 error = EINVAL;
567 goto error_exit;
568 }
569
570 pmp->pm_HugeSectors *= pmp->pm_BlkPerSec;
571 pmp->pm_HiddenSects *= pmp->pm_BlkPerSec; /* XXX not used? */
572 pmp->pm_FATsecs *= pmp->pm_BlkPerSec;
573 SecPerClust *= pmp->pm_BlkPerSec;
574
575 pmp->pm_fatblk = pmp->pm_ResSectors * pmp->pm_BlkPerSec;
576
577 if (FAT32(pmp)) {
578 pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
579 pmp->pm_firstcluster = pmp->pm_fatblk
580 + (pmp->pm_FATs * pmp->pm_FATsecs);
581 pmp->pm_fsinfo = getushort(b710->bpbFSInfo) * pmp->pm_BlkPerSec;
582 } else {
583 pmp->pm_rootdirblk = pmp->pm_fatblk +
584 (pmp->pm_FATs * pmp->pm_FATsecs);
585 pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
586 + DEV_BSIZE - 1)
587 / DEV_BSIZE; /* in blocks */
588 pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
589 }
590
591 pmp->pm_maxcluster = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
592 SecPerClust + 1;
593 pmp->pm_fatsize = pmp->pm_FATsecs * DEV_BSIZE; /* XXX not used? */
594
595#ifndef __FreeBSD__
596 if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
597 if ((pmp->pm_maxcluster <= (0xff0 - 2))
598 && ((dtype == DTYPE_FLOPPY) || ((dtype == DTYPE_VNODE)
599 && ((pmp->pm_Heads == 1) || (pmp->pm_Heads == 2))))
600 ) {
601 pmp->pm_fatmask = FAT12_MASK;
602 pmp->pm_fatmult = 3;
603 pmp->pm_fatdiv = 2;
604 } else {
605 pmp->pm_fatmask = FAT16_MASK;
606 pmp->pm_fatmult = 2;
607 pmp->pm_fatdiv = 1;
608 }
609 } else
610#endif
611 if (pmp->pm_fatmask == 0) {
612 if (pmp->pm_maxcluster
613 <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
614 /*
615 * This will usually be a floppy disk. This size makes
616 * sure that one fat entry will not be split across
617 * multiple blocks.
618 */
619 pmp->pm_fatmask = FAT12_MASK;
620 pmp->pm_fatmult = 3;
621 pmp->pm_fatdiv = 2;
622 } else {
623 pmp->pm_fatmask = FAT16_MASK;
624 pmp->pm_fatmult = 2;
625 pmp->pm_fatdiv = 1;
626 }
627 }
628
629 clusters = (pmp->pm_fatsize / pmp->pm_fatmult) * pmp->pm_fatdiv;
630 if (pmp->pm_maxcluster >= clusters) {
631 printf("Warning: number of clusters (%ld) exceeds FAT "
632 "capacity (%ld)\n", pmp->pm_maxcluster + 1, clusters);
633 pmp->pm_maxcluster = clusters - 1;
634 }
635
636
637 if (FAT12(pmp))
638 pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec;
639 else
640 pmp->pm_fatblocksize = MSDOSFS_DFLTBSIZE;
641
642 pmp->pm_fatblocksec = pmp->pm_fatblocksize / DEV_BSIZE;
643 pmp->pm_bnshift = ffs(DEV_BSIZE) - 1;
644
645 /*
646 * Compute mask and shift value for isolating cluster relative byte
647 * offsets and cluster numbers from a file offset.
648 */
649 pmp->pm_bpcluster = SecPerClust * DEV_BSIZE;
650 pmp->pm_crbomask = pmp->pm_bpcluster - 1;
651 pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
652
653 /*
654 * Check for valid cluster size
655 * must be a power of 2
656 */
657 if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
658 error = EINVAL;
659 goto error_exit;
660 }
661
662 /*
663 * Release the bootsector buffer.
664 */
665 brelse(bp);
666 bp = NULL;
667
668 /*
669 * Check FSInfo.
670 */
671 if (pmp->pm_fsinfo) {
672 struct fsinfo *fp;
673
674 if ((error = bread(devvp, pmp->pm_fsinfo, fsi_size(pmp),
675 NOCRED, &bp)) != 0)
676 goto error_exit;
677 fp = (struct fsinfo *)bp->b_data;
678 if (!bcmp(fp->fsisig1, "RRaA", 4)
679 && !bcmp(fp->fsisig2, "rrAa", 4)
680 && !bcmp(fp->fsisig3, "\0\0\125\252", 4)
681 && !bcmp(fp->fsisig4, "\0\0\125\252", 4))
682 pmp->pm_nxtfree = getulong(fp->fsinxtfree);
683 else
684 pmp->pm_fsinfo = 0;
685 brelse(bp);
686 bp = NULL;
687 }
688
689 /*
690 * Check and validate (or perhaps invalidate?) the fsinfo structure?
691 */
692 if (pmp->pm_fsinfo && pmp->pm_nxtfree > pmp->pm_maxcluster) {
693 printf(
694 "Next free cluster in FSInfo (%lu) exceeds maxcluster (%lu)\n",
695 pmp->pm_nxtfree, pmp->pm_maxcluster);
696 error = EINVAL;
697 goto error_exit;
698 }
699
700 /*
701 * Allocate memory for the bitmap of allocated clusters, and then
702 * fill it in.
703 */
704 pmp->pm_inusemap = malloc(((pmp->pm_maxcluster + N_INUSEBITS - 1)
705 / N_INUSEBITS)
706 * sizeof(*pmp->pm_inusemap),
707 M_MSDOSFSFAT, M_WAITOK);
708
709 /*
710 * fillinusemap() needs pm_devvp.
711 */
712 pmp->pm_dev = dev;
713 pmp->pm_devvp = devvp;
714
715 /*
716 * Have the inuse map filled in.
717 */
718 if ((error = fillinusemap(pmp)) != 0)
719 goto error_exit;
720
721 /*
722 * If they want fat updates to be synchronous then let them suffer
723 * the performance degradation in exchange for the on disk copy of
724 * the fat being correct just about all the time. I suppose this
725 * would be a good thing to turn on if the kernel is still flakey.
726 */
727 if (mp->mnt_flag & MNT_SYNCHRONOUS)
728 pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
729
730 /*
731 * Finish up.
732 */
733 if (ronly)
734 pmp->pm_flags |= MSDOSFSMNT_RONLY;
735 else
736 pmp->pm_fmod = 1;
737 mp->mnt_data = (qaddr_t) pmp;
738 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
739 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
740 mp->mnt_flag |= MNT_LOCAL;
741 devvp->v_specmountpoint = mp;
742
743 return 0;
744
745error_exit:
746 if (bp)
747 brelse(bp);
dadab5e9 748 (void) VOP_CLOSE(devvp, ronly ? FREAD : FREAD | FWRITE, NOCRED, td);
984263bc
MD
749 if (pmp) {
750 if (pmp->pm_inusemap)
751 free(pmp->pm_inusemap, M_MSDOSFSFAT);
752 free(pmp, M_MSDOSFSMNT);
753 mp->mnt_data = (qaddr_t)0;
754 }
755 return (error);
756}
757
758/*
759 * Unmount the filesystem described by mp.
760 */
761static int
dadab5e9 762msdosfs_unmount(mp, mntflags, td)
984263bc
MD
763 struct mount *mp;
764 int mntflags;
dadab5e9 765 struct thread *td;
984263bc
MD
766{
767 struct msdosfsmount *pmp;
768 int error, flags;
769
770 flags = 0;
771 if (mntflags & MNT_FORCE)
772 flags |= FORCECLOSE;
773 error = vflush(mp, 0, flags);
774 if (error)
775 return error;
776 pmp = VFSTOMSDOSFS(mp);
777 pmp->pm_devvp->v_specmountpoint = NULL;
778#ifdef MSDOSFS_DEBUG
779 {
780 struct vnode *vp = pmp->pm_devvp;
781
782 printf("msdosfs_umount(): just before calling VOP_CLOSE()\n");
783 printf("flag %08lx, usecount %d, writecount %d, holdcnt %ld\n",
784 vp->v_flag, vp->v_usecount, vp->v_writecount, vp->v_holdcnt);
785 printf("id %lu, mount %p, op %p\n",
786 vp->v_id, vp->v_mount, vp->v_op);
787 printf("freef %p, freeb %p, mount %p\n",
788 vp->v_freelist.tqe_next, vp->v_freelist.tqe_prev,
789 vp->v_mount);
790 printf("cleanblkhd %p, dirtyblkhd %p, numoutput %ld, type %d\n",
791 TAILQ_FIRST(&vp->v_cleanblkhd),
792 TAILQ_FIRST(&vp->v_dirtyblkhd),
793 vp->v_numoutput, vp->v_type);
794 printf("union %p, tag %d, data[0] %08x, data[1] %08x\n",
795 vp->v_socket, vp->v_tag,
796 ((u_int *)vp->v_data)[0],
797 ((u_int *)vp->v_data)[1]);
798 }
799#endif
800 error = VOP_CLOSE(pmp->pm_devvp,
801 (pmp->pm_flags&MSDOSFSMNT_RONLY) ? FREAD : FREAD | FWRITE,
dadab5e9 802 NOCRED, td);
984263bc
MD
803 vrele(pmp->pm_devvp);
804 free(pmp->pm_inusemap, M_MSDOSFSFAT);
805 free(pmp, M_MSDOSFSMNT);
806 mp->mnt_data = (qaddr_t)0;
807 mp->mnt_flag &= ~MNT_LOCAL;
808 return (error);
809}
810
811static int
812msdosfs_root(mp, vpp)
813 struct mount *mp;
814 struct vnode **vpp;
815{
816 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
817 struct denode *ndep;
818 int error;
819
820#ifdef MSDOSFS_DEBUG
821 printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
822#endif
823 error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep);
824 if (error)
825 return (error);
826 *vpp = DETOV(ndep);
827 return (0);
828}
829
830static int
dadab5e9 831msdosfs_statfs(mp, sbp, td)
984263bc
MD
832 struct mount *mp;
833 struct statfs *sbp;
dadab5e9 834 struct thread *td;
984263bc
MD
835{
836 struct msdosfsmount *pmp;
837
838 pmp = VFSTOMSDOSFS(mp);
839 sbp->f_bsize = pmp->pm_bpcluster;
840 sbp->f_iosize = pmp->pm_bpcluster;
841 sbp->f_blocks = pmp->pm_maxcluster + 1;
842 sbp->f_bfree = pmp->pm_freeclustercount;
843 sbp->f_bavail = pmp->pm_freeclustercount;
844 sbp->f_files = pmp->pm_RootDirEnts; /* XXX */
845 sbp->f_ffree = 0; /* what to put in here? */
846 if (sbp != &mp->mnt_stat) {
847 sbp->f_type = mp->mnt_vfc->vfc_typenum;
848 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
849 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
850 }
851 strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
852 return (0);
853}
854
855static int
dadab5e9 856msdosfs_sync(mp, waitfor, cred, td)
984263bc
MD
857 struct mount *mp;
858 int waitfor;
859 struct ucred *cred;
dadab5e9 860 struct thread *td;
984263bc
MD
861{
862 struct vnode *vp, *nvp;
863 struct denode *dep;
864 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
865 int error, allerror = 0;
866
867 /*
868 * If we ever switch to not updating all of the fats all the time,
869 * this would be the place to update them from the first one.
870 */
871 if (pmp->pm_fmod != 0) {
872 if (pmp->pm_flags & MSDOSFSMNT_RONLY)
873 panic("msdosfs_sync: rofs mod");
874 else {
875 /* update fats here */
876 }
877 }
878 /*
879 * Write back each (modified) denode.
880 */
881 simple_lock(&mntvnode_slock);
882loop:
883 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp != NULL; vp = nvp) {
884 /*
885 * If the vnode that we are about to sync is no longer
886 * associated with this mount point, start over.
887 */
888 if (vp->v_mount != mp)
889 goto loop;
890
891 simple_lock(&vp->v_interlock);
892 nvp = TAILQ_NEXT(vp, v_nmntvnodes);
893 dep = VTODE(vp);
894 if (vp->v_type == VNON ||
895 ((dep->de_flag &
896 (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 &&
897 (TAILQ_EMPTY(&vp->v_dirtyblkhd) || waitfor == MNT_LAZY))) {
898 simple_unlock(&vp->v_interlock);
899 continue;
900 }
901 simple_unlock(&mntvnode_slock);
dadab5e9 902 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, td);
984263bc
MD
903 if (error) {
904 simple_lock(&mntvnode_slock);
905 if (error == ENOENT)
906 goto loop;
907 continue;
908 }
dadab5e9 909 error = VOP_FSYNC(vp, cred, waitfor, td);
984263bc
MD
910 if (error)
911 allerror = error;
dadab5e9 912 VOP_UNLOCK(vp, 0, td);
984263bc
MD
913 vrele(vp);
914 simple_lock(&mntvnode_slock);
915 }
916 simple_unlock(&mntvnode_slock);
917
918 /*
919 * Flush filesystem control info.
920 */
921 if (waitfor != MNT_LAZY) {
dadab5e9
MD
922 vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY, td);
923 error = VOP_FSYNC(pmp->pm_devvp, cred, waitfor, td);
984263bc
MD
924 if (error)
925 allerror = error;
dadab5e9 926 VOP_UNLOCK(pmp->pm_devvp, 0, td);
984263bc
MD
927 }
928 return (allerror);
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);