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