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