msdosfs_vfsops.c revision 1.44.4.3 1 /* $NetBSD: msdosfs_vfsops.c,v 1.44.4.3 2007/06/17 21:31:08 ad 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.44.4.3 2007/06/17 21:31:08 ad 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 mutex_enter(&mountlist_lock);
236 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
237 mutex_exit(&mountlist_lock);
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 secsize = 512; /* XXX */
493 dtype = DTYPE_FLOPPY; /* XXX */
494 fstype = FS_MSDOS;
495 psize = -1;
496 if (error) {
497 if (error != ENOTTY) {
498 DPRINTF(("Error getting partition info %d\n",
499 error));
500 goto error_exit;
501 }
502 } else {
503 fstype = strcmp(dkw.dkw_ptype, DKW_PTYPE_FAT) == 0 ?
504 FS_MSDOS : -1;
505 psize = dkw.dkw_size;
506 }
507 }
508 if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
509 bsize = secsize;
510 if (bsize != 512 ||
511 (dtype != DTYPE_FLOPPY && fstype != FS_MSDOS)) {
512 DPRINTF(("bsize %d dtype %d fstype %d\n", bsize, dtype,
513 fstype));
514 error = EINVAL;
515 goto error_exit;
516 }
517 } else
518 bsize = 0;
519
520 /*
521 * Read the boot sector of the filesystem, and then check the
522 * boot signature. If not a dos boot sector then error out.
523 */
524 if ((error = bread(devvp, 0, secsize, NOCRED, &bp)) != 0)
525 goto error_exit;
526 bp->b_flags |= B_AGE;
527 bsp = (union bootsector *)bp->b_data;
528 b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
529 b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
530 b710 = (struct byte_bpb710 *)bsp->bs710.bsBPB;
531
532 if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
533 if (bsp->bs50.bsBootSectSig0 != BOOTSIG0
534 || bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
535 DPRINTF(("bootsig0 %d bootsig1 %d\n",
536 bsp->bs50.bsBootSectSig0,
537 bsp->bs50.bsBootSectSig1));
538 error = EINVAL;
539 goto error_exit;
540 }
541 }
542
543 pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK);
544 memset(pmp, 0, sizeof *pmp);
545 pmp->pm_mountp = mp;
546
547 /*
548 * Compute several useful quantities from the bpb in the
549 * bootsector. Copy in the dos 5 variant of the bpb then fix up
550 * the fields that are different between dos 5 and dos 3.3.
551 */
552 SecPerClust = b50->bpbSecPerClust;
553 pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
554 pmp->pm_ResSectors = getushort(b50->bpbResSectors);
555 pmp->pm_FATs = b50->bpbFATs;
556 pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
557 pmp->pm_Sectors = getushort(b50->bpbSectors);
558 pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
559 pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
560 pmp->pm_Heads = getushort(b50->bpbHeads);
561 pmp->pm_Media = b50->bpbMedia;
562
563 if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
564 /* XXX - We should probably check more values here */
565 if (!pmp->pm_BytesPerSec || !SecPerClust
566 || pmp->pm_Heads > 255 || pmp->pm_SecPerTrack > 63) {
567 DPRINTF(("bytespersec %d secperclust %d "
568 "heads %d secpertrack %d\n",
569 pmp->pm_BytesPerSec, SecPerClust,
570 pmp->pm_Heads, pmp->pm_SecPerTrack));
571 error = EINVAL;
572 goto error_exit;
573 }
574 }
575
576 if (pmp->pm_Sectors == 0) {
577 pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
578 pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
579 } else {
580 pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
581 pmp->pm_HugeSectors = pmp->pm_Sectors;
582 }
583
584 if (pmp->pm_RootDirEnts == 0) {
585 unsigned short vers = getushort(b710->bpbFSVers);
586 /*
587 * Some say that bsBootSectSig[23] must be zero, but
588 * Windows does not require this and some digital cameras
589 * do not set these to zero. Therefore, do not insist.
590 */
591 if (pmp->pm_Sectors || pmp->pm_FATsecs || vers) {
592 DPRINTF(("sectors %d fatsecs %lu vers %d\n",
593 pmp->pm_Sectors, pmp->pm_FATsecs, vers));
594 error = EINVAL;
595 goto error_exit;
596 }
597 pmp->pm_fatmask = FAT32_MASK;
598 pmp->pm_fatmult = 4;
599 pmp->pm_fatdiv = 1;
600 pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
601
602 /* mirrorring is enabled if the FATMIRROR bit is not set */
603 if ((getushort(b710->bpbExtFlags) & FATMIRROR) == 0)
604 pmp->pm_flags |= MSDOSFS_FATMIRROR;
605 else
606 pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
607 } else
608 pmp->pm_flags |= MSDOSFS_FATMIRROR;
609
610 if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
611 if (FAT32(pmp)) {
612 DPRINTF(("fat32 for gemdos\n"));
613 /*
614 * GEMDOS doesn't know fat32.
615 */
616 error = EINVAL;
617 goto error_exit;
618 }
619
620 /*
621 * Check a few values (could do some more):
622 * - logical sector size: power of 2, >= block size
623 * - sectors per cluster: power of 2, >= 1
624 * - number of sectors: >= 1, <= size of partition
625 */
626 if ( (SecPerClust == 0)
627 || (SecPerClust & (SecPerClust - 1))
628 || (pmp->pm_BytesPerSec < bsize)
629 || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
630 || (pmp->pm_HugeSectors == 0)
631 || (pmp->pm_HugeSectors * (pmp->pm_BytesPerSec / bsize)
632 > psize)) {
633 DPRINTF(("consistency checks for gemdos\n"));
634 error = EINVAL;
635 goto error_exit;
636 }
637 /*
638 * XXX - Many parts of the msdos fs driver seem to assume that
639 * the number of bytes per logical sector (BytesPerSec) will
640 * always be the same as the number of bytes per disk block
641 * Let's pretend it is.
642 */
643 tmp = pmp->pm_BytesPerSec / bsize;
644 pmp->pm_BytesPerSec = bsize;
645 pmp->pm_HugeSectors *= tmp;
646 pmp->pm_HiddenSects *= tmp;
647 pmp->pm_ResSectors *= tmp;
648 pmp->pm_Sectors *= tmp;
649 pmp->pm_FATsecs *= tmp;
650 SecPerClust *= tmp;
651 }
652 pmp->pm_fatblk = pmp->pm_ResSectors;
653 if (FAT32(pmp)) {
654 pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
655 pmp->pm_firstcluster = pmp->pm_fatblk
656 + (pmp->pm_FATs * pmp->pm_FATsecs);
657 pmp->pm_fsinfo = getushort(b710->bpbFSInfo);
658 } else {
659 pmp->pm_rootdirblk = pmp->pm_fatblk +
660 (pmp->pm_FATs * pmp->pm_FATsecs);
661 pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
662 + pmp->pm_BytesPerSec - 1)
663 / pmp->pm_BytesPerSec;/* in sectors */
664 pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
665 }
666
667 pmp->pm_nmbrofclusters = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
668 SecPerClust;
669 pmp->pm_maxcluster = pmp->pm_nmbrofclusters + 1;
670 pmp->pm_fatsize = pmp->pm_FATsecs * pmp->pm_BytesPerSec;
671
672 if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
673 if (pmp->pm_nmbrofclusters <= (0xff0 - 2)
674 && (dtype == DTYPE_FLOPPY
675 || (dtype == DTYPE_VND
676 && (pmp->pm_Heads == 1 || pmp->pm_Heads == 2)))
677 ) {
678 pmp->pm_fatmask = FAT12_MASK;
679 pmp->pm_fatmult = 3;
680 pmp->pm_fatdiv = 2;
681 } else {
682 pmp->pm_fatmask = FAT16_MASK;
683 pmp->pm_fatmult = 2;
684 pmp->pm_fatdiv = 1;
685 }
686 } else if (pmp->pm_fatmask == 0) {
687 if (pmp->pm_maxcluster
688 <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
689 /*
690 * This will usually be a floppy disk. This size makes
691 * sure that one fat entry will not be split across
692 * multiple blocks.
693 */
694 pmp->pm_fatmask = FAT12_MASK;
695 pmp->pm_fatmult = 3;
696 pmp->pm_fatdiv = 2;
697 } else {
698 pmp->pm_fatmask = FAT16_MASK;
699 pmp->pm_fatmult = 2;
700 pmp->pm_fatdiv = 1;
701 }
702 }
703 if (FAT12(pmp))
704 pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec;
705 else
706 pmp->pm_fatblocksize = MAXBSIZE;
707
708 pmp->pm_fatblocksec = pmp->pm_fatblocksize / pmp->pm_BytesPerSec;
709 pmp->pm_bnshift = ffs(pmp->pm_BytesPerSec) - 1;
710
711 /*
712 * Compute mask and shift value for isolating cluster relative byte
713 * offsets and cluster numbers from a file offset.
714 */
715 pmp->pm_bpcluster = SecPerClust * pmp->pm_BytesPerSec;
716 pmp->pm_crbomask = pmp->pm_bpcluster - 1;
717 pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
718
719 /*
720 * Check for valid cluster size
721 * must be a power of 2
722 */
723 if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
724 DPRINTF(("bpcluster %lu cnshift %lu\n",
725 pmp->pm_bpcluster, pmp->pm_cnshift));
726 error = EINVAL;
727 goto error_exit;
728 }
729
730 /*
731 * Release the bootsector buffer.
732 */
733 brelse(bp, 0);
734 bp = NULL;
735
736 /*
737 * Check FSInfo.
738 */
739 if (pmp->pm_fsinfo) {
740 struct fsinfo *fp;
741
742 /*
743 * XXX If the fsinfo block is stored on media with
744 * 2KB or larger sectors, is the fsinfo structure
745 * padded at the end or in the middle?
746 */
747 if ((error = bread(devvp, de_bn2kb(pmp, pmp->pm_fsinfo),
748 pmp->pm_BytesPerSec, NOCRED, &bp)) != 0)
749 goto error_exit;
750 fp = (struct fsinfo *)bp->b_data;
751 if (!memcmp(fp->fsisig1, "RRaA", 4)
752 && !memcmp(fp->fsisig2, "rrAa", 4)
753 && !memcmp(fp->fsisig3, "\0\0\125\252", 4)
754 && !memcmp(fp->fsisig4, "\0\0\125\252", 4))
755 pmp->pm_nxtfree = getulong(fp->fsinxtfree);
756 else
757 pmp->pm_fsinfo = 0;
758 brelse(bp, 0);
759 bp = NULL;
760 }
761
762 /*
763 * Check and validate (or perhaps invalidate?) the fsinfo structure?
764 * XXX
765 */
766 if (pmp->pm_fsinfo) {
767 if (pmp->pm_nxtfree == (u_long)-1)
768 pmp->pm_fsinfo = 0;
769 }
770
771 /*
772 * Allocate memory for the bitmap of allocated clusters, and then
773 * fill it in.
774 */
775 pmp->pm_inusemap = malloc(((pmp->pm_maxcluster + N_INUSEBITS - 1)
776 / N_INUSEBITS)
777 * sizeof(*pmp->pm_inusemap),
778 M_MSDOSFSFAT, M_WAITOK);
779
780 /*
781 * fillinusemap() needs pm_devvp.
782 */
783 pmp->pm_dev = dev;
784 pmp->pm_devvp = devvp;
785
786 /*
787 * Have the inuse map filled in.
788 */
789 if ((error = fillinusemap(pmp)) != 0) {
790 DPRINTF(("fillinusemap %d\n", error));
791 goto error_exit;
792 }
793
794 /*
795 * If they want fat updates to be synchronous then let them suffer
796 * the performance degradation in exchange for the on disk copy of
797 * the fat being correct just about all the time. I suppose this
798 * would be a good thing to turn on if the kernel is still flakey.
799 */
800 if (mp->mnt_flag & MNT_SYNCHRONOUS)
801 pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
802
803 /*
804 * Finish up.
805 */
806 if (ronly)
807 pmp->pm_flags |= MSDOSFSMNT_RONLY;
808 else
809 pmp->pm_fmod = 1;
810 mp->mnt_data = pmp;
811 mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
812 mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_MSDOS);
813 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
814 mp->mnt_stat.f_namemax = MSDOSFS_NAMEMAX(pmp);
815 mp->mnt_flag |= MNT_LOCAL;
816 mp->mnt_dev_bshift = pmp->pm_bnshift;
817 mp->mnt_fs_bshift = pmp->pm_cnshift;
818
819 #ifdef QUOTA
820 /*
821 * If we ever do quotas for DOS filesystems this would be a place
822 * to fill in the info in the msdosfsmount structure. You dolt,
823 * quotas on dos filesystems make no sense because files have no
824 * owners on dos filesystems. of course there is some empty space
825 * in the directory entry where we could put uid's and gid's.
826 */
827 #endif
828 devvp->v_specmountpoint = mp;
829
830 return (0);
831
832 error_exit:;
833 if (bp)
834 brelse(bp, 0);
835 if (pmp) {
836 if (pmp->pm_inusemap)
837 free(pmp->pm_inusemap, M_MSDOSFSFAT);
838 free(pmp, M_MSDOSFSMNT);
839 mp->mnt_data = NULL;
840 }
841 return (error);
842 }
843
844 int
845 msdosfs_start(struct mount *mp, int flags,
846 struct lwp *l)
847 {
848
849 return (0);
850 }
851
852 /*
853 * Unmount the filesystem described by mp.
854 */
855 int
856 msdosfs_unmount(mp, mntflags, l)
857 struct mount *mp;
858 int mntflags;
859 struct lwp *l;
860 {
861 struct msdosfsmount *pmp;
862 int error, flags;
863
864 flags = 0;
865 if (mntflags & MNT_FORCE)
866 flags |= FORCECLOSE;
867 #ifdef QUOTA
868 #endif
869 if ((error = vflush(mp, NULLVP, flags)) != 0)
870 return (error);
871 pmp = VFSTOMSDOSFS(mp);
872 if (pmp->pm_devvp->v_type != VBAD)
873 pmp->pm_devvp->v_specmountpoint = NULL;
874 #ifdef MSDOSFS_DEBUG
875 {
876 struct vnode *vp = pmp->pm_devvp;
877
878 printf("msdosfs_umount(): just before calling VOP_CLOSE()\n");
879 printf("flag %08x, usecount %d, writecount %ld, holdcnt %ld\n",
880 vp->v_vflag | vp->v_iflag | vp->v_uflag, vp->v_usecount,
881 vp->v_writecount, vp->v_holdcnt);
882 printf("mount %p, op %p\n",
883 vp->v_mount, vp->v_op);
884 printf("freef %p, freeb %p, mount %p\n",
885 vp->v_freelist.tqe_next, vp->v_freelist.tqe_prev,
886 vp->v_mount);
887 printf("cleanblkhd %p, dirtyblkhd %p, numoutput %d, type %d\n",
888 vp->v_cleanblkhd.lh_first,
889 vp->v_dirtyblkhd.lh_first,
890 vp->v_numoutput, vp->v_type);
891 printf("union %p, tag %d, data[0] %08x, data[1] %08x\n",
892 vp->v_socket, vp->v_tag,
893 ((u_int *)vp->v_data)[0],
894 ((u_int *)vp->v_data)[1]);
895 }
896 #endif
897 vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY);
898 error = VOP_CLOSE(pmp->pm_devvp,
899 pmp->pm_flags & MSDOSFSMNT_RONLY ? FREAD : FREAD|FWRITE, NOCRED, l);
900 vput(pmp->pm_devvp);
901 free(pmp->pm_inusemap, M_MSDOSFSFAT);
902 free(pmp, M_MSDOSFSMNT);
903 mp->mnt_data = NULL;
904 mp->mnt_flag &= ~MNT_LOCAL;
905 return (error);
906 }
907
908 int
909 msdosfs_root(mp, vpp)
910 struct mount *mp;
911 struct vnode **vpp;
912 {
913 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
914 struct denode *ndep;
915 int error;
916
917 #ifdef MSDOSFS_DEBUG
918 printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
919 #endif
920 if ((error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep)) != 0)
921 return (error);
922 *vpp = DETOV(ndep);
923 return (0);
924 }
925
926 int
927 msdosfs_quotactl(struct mount *mp, int cmds,
928 uid_t uid, void *arg, struct lwp *l)
929 {
930
931 return (EOPNOTSUPP);
932 }
933
934 int
935 msdosfs_statvfs(struct mount *mp, struct statvfs *sbp, struct lwp *l)
936 {
937 struct msdosfsmount *pmp;
938
939 pmp = VFSTOMSDOSFS(mp);
940 sbp->f_bsize = pmp->pm_bpcluster;
941 sbp->f_frsize = sbp->f_bsize;
942 sbp->f_iosize = pmp->pm_bpcluster;
943 sbp->f_blocks = pmp->pm_nmbrofclusters;
944 sbp->f_bfree = pmp->pm_freeclustercount;
945 sbp->f_bavail = pmp->pm_freeclustercount;
946 sbp->f_bresvd = 0;
947 sbp->f_files = pmp->pm_RootDirEnts; /* XXX */
948 sbp->f_ffree = 0; /* what to put in here? */
949 sbp->f_favail = 0; /* what to put in here? */
950 sbp->f_fresvd = 0;
951 copy_statvfs_info(sbp, mp);
952 return (0);
953 }
954
955 int
956 msdosfs_sync(mp, waitfor, cred, l)
957 struct mount *mp;
958 int waitfor;
959 kauth_cred_t cred;
960 struct lwp *l;
961 {
962 struct vnode *vp, *nvp;
963 struct denode *dep;
964 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
965 int error, allerror = 0;
966
967 /*
968 * If we ever switch to not updating all of the fats all the time,
969 * this would be the place to update them from the first one.
970 */
971 if (pmp->pm_fmod != 0) {
972 if (pmp->pm_flags & MSDOSFSMNT_RONLY)
973 panic("msdosfs_sync: rofs mod");
974 else {
975 /* update fats here */
976 }
977 }
978 /*
979 * Write back each (modified) denode.
980 */
981 mutex_enter(&mntvnode_lock);
982 loop:
983 for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = nvp) {
984 /*
985 * If the vnode that we are about to sync is no longer
986 * associated with this mount point, start over.
987 */
988 if (vp->v_mount != mp)
989 goto loop;
990 mutex_enter(&vp->v_interlock);
991 nvp = TAILQ_NEXT(vp, v_mntvnodes);
992 dep = VTODE(vp);
993 if (waitfor == MNT_LAZY || vp->v_type == VNON ||
994 (((dep->de_flag &
995 (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0) &&
996 (LIST_EMPTY(&vp->v_dirtyblkhd) &&
997 vp->v_uobj.uo_npages == 0))) {
998 mutex_exit(&vp->v_interlock);
999 continue;
1000 }
1001 mutex_exit(&mntvnode_lock);
1002 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
1003 if (error) {
1004 mutex_enter(&mntvnode_lock);
1005 if (error == ENOENT)
1006 goto loop;
1007 continue;
1008 }
1009 if ((error = VOP_FSYNC(vp, cred,
1010 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, l)) != 0)
1011 allerror = error;
1012 vput(vp);
1013 mutex_exit(&mntvnode_lock);
1014 }
1015 mutex_exit(&mntvnode_lock);
1016 /*
1017 * Force stale file system control information to be flushed.
1018 */
1019 if ((error = VOP_FSYNC(pmp->pm_devvp, cred,
1020 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, l)) != 0)
1021 allerror = error;
1022 #ifdef QUOTA
1023 /* qsync(mp); */
1024 #endif
1025 return (allerror);
1026 }
1027
1028 int
1029 msdosfs_fhtovp(mp, fhp, vpp)
1030 struct mount *mp;
1031 struct fid *fhp;
1032 struct vnode **vpp;
1033 {
1034 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
1035 struct defid defh;
1036 struct denode *dep;
1037 int error;
1038
1039 if (fhp->fid_len != sizeof(struct defid)) {
1040 DPRINTF(("fid_len %d %zd\n", fhp->fid_len,
1041 sizeof(struct defid)));
1042 return EINVAL;
1043 }
1044
1045 memcpy(&defh, fhp, sizeof(defh));
1046 error = deget(pmp, defh.defid_dirclust, defh.defid_dirofs, &dep);
1047 if (error) {
1048 DPRINTF(("deget %d\n", error));
1049 *vpp = NULLVP;
1050 return (error);
1051 }
1052 *vpp = DETOV(dep);
1053 return (0);
1054 }
1055
1056 int
1057 msdosfs_vptofh(vp, fhp, fh_size)
1058 struct vnode *vp;
1059 struct fid *fhp;
1060 size_t *fh_size;
1061 {
1062 struct denode *dep;
1063 struct defid defh;
1064
1065 if (*fh_size < sizeof(struct defid)) {
1066 *fh_size = sizeof(struct defid);
1067 return E2BIG;
1068 }
1069 *fh_size = sizeof(struct defid);
1070 dep = VTODE(vp);
1071 memset(&defh, 0, sizeof(defh));
1072 defh.defid_len = sizeof(struct defid);
1073 defh.defid_dirclust = dep->de_dirclust;
1074 defh.defid_dirofs = dep->de_diroffset;
1075 /* defh.defid_gen = dep->de_gen; */
1076 memcpy(fhp, &defh, sizeof(defh));
1077 return (0);
1078 }
1079
1080 int
1081 msdosfs_vget(struct mount *mp, ino_t ino,
1082 struct vnode **vpp)
1083 {
1084
1085 return (EOPNOTSUPP);
1086 }
1087
1088 SYSCTL_SETUP(sysctl_vfs_msdosfs_setup, "sysctl vfs.msdosfs subtree setup")
1089 {
1090
1091 sysctl_createv(clog, 0, NULL, NULL,
1092 CTLFLAG_PERMANENT,
1093 CTLTYPE_NODE, "vfs", NULL,
1094 NULL, 0, NULL, 0,
1095 CTL_VFS, CTL_EOL);
1096 sysctl_createv(clog, 0, NULL, NULL,
1097 CTLFLAG_PERMANENT,
1098 CTLTYPE_NODE, "msdosfs",
1099 SYSCTL_DESCR("MS-DOS file system"),
1100 NULL, 0, NULL, 0,
1101 CTL_VFS, 4, CTL_EOL);
1102 /*
1103 * XXX the "4" above could be dynamic, thereby eliminating one
1104 * more instance of the "number to vfs" mapping problem, but
1105 * "4" is the order as taken from sys/mount.h
1106 */
1107 }
1108