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