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