msdosfs_vfsops.c revision 302408
1/* $FreeBSD: stable/11/sys/fs/msdosfs/msdosfs_vfsops.c 300371 2016-05-21 11:40:41Z kib $ */
2/*	$NetBSD: msdosfs_vfsops.c,v 1.51 1997/11/17 15:36:58 ws Exp $	*/
3
4/*-
5 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
6 * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
7 * All rights reserved.
8 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *	This product includes software developed by TooLs GmbH.
21 * 4. The name of TooLs GmbH may not be used to endorse or promote products
22 *    derived from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
30 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35/*-
36 * Written by Paul Popelka (paulp@uts.amdahl.com)
37 *
38 * You can do anything you want with this software, just don't say you wrote
39 * it, and don't remove this notice.
40 *
41 * This software is provided "as is".
42 *
43 * The author supplies this software to be publicly redistributed on the
44 * understanding that the author is not responsible for the correct
45 * functioning of this software in any circumstances and is not liable for
46 * any damages caused by this software.
47 *
48 * October 1992
49 */
50
51#include <sys/param.h>
52#include <sys/systm.h>
53#include <sys/buf.h>
54#include <sys/conf.h>
55#include <sys/fcntl.h>
56#include <sys/iconv.h>
57#include <sys/kernel.h>
58#include <sys/lock.h>
59#include <sys/malloc.h>
60#include <sys/mount.h>
61#include <sys/mutex.h>
62#include <sys/namei.h>
63#include <sys/priv.h>
64#include <sys/proc.h>
65#include <sys/stat.h>
66#include <sys/vnode.h>
67
68#include <geom/geom.h>
69#include <geom/geom_vfs.h>
70
71#include <fs/msdosfs/bootsect.h>
72#include <fs/msdosfs/bpb.h>
73#include <fs/msdosfs/direntry.h>
74#include <fs/msdosfs/denode.h>
75#include <fs/msdosfs/fat.h>
76#include <fs/msdosfs/msdosfsmount.h>
77
78static const char msdosfs_lock_msg[] = "fatlk";
79
80/* Mount options that we support. */
81static const char *msdosfs_opts[] = {
82	"async", "noatime", "noclusterr", "noclusterw",
83	"export", "force", "from", "sync",
84	"cs_dos", "cs_local", "cs_win", "dirmask",
85	"gid", "kiconv", "large", "longname",
86	"longnames", "mask", "shortname", "shortnames",
87	"uid", "win95", "nowin95",
88	NULL
89};
90
91#if 1 /*def PC98*/
92/*
93 * XXX - The boot signature formatted by NEC PC-98 DOS looks like a
94 *       garbage or a random value :-{
95 *       If you want to use that broken-signatured media, define the
96 *       following symbol even though PC/AT.
97 *       (ex. mount PC-98 DOS formatted FD on PC/AT)
98 */
99#define	MSDOSFS_NOCHECKSIG
100#endif
101
102MALLOC_DEFINE(M_MSDOSFSMNT, "msdosfs_mount", "MSDOSFS mount structure");
103static MALLOC_DEFINE(M_MSDOSFSFAT, "msdosfs_fat", "MSDOSFS file allocation table");
104
105struct iconv_functions *msdosfs_iconv;
106
107static int	update_mp(struct mount *mp, struct thread *td);
108static int	mountmsdosfs(struct vnode *devvp, struct mount *mp);
109static vfs_fhtovp_t	msdosfs_fhtovp;
110static vfs_mount_t	msdosfs_mount;
111static vfs_root_t	msdosfs_root;
112static vfs_statfs_t	msdosfs_statfs;
113static vfs_sync_t	msdosfs_sync;
114static vfs_unmount_t	msdosfs_unmount;
115
116/* Maximum length of a character set name (arbitrary). */
117#define	MAXCSLEN	64
118
119static int
120update_mp(struct mount *mp, struct thread *td)
121{
122	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
123	void *dos, *win, *local;
124	int error, v;
125
126	if (!vfs_getopt(mp->mnt_optnew, "kiconv", NULL, NULL)) {
127		if (msdosfs_iconv != NULL) {
128			error = vfs_getopt(mp->mnt_optnew,
129			    "cs_win", &win, NULL);
130			if (!error)
131				error = vfs_getopt(mp->mnt_optnew,
132				    "cs_local", &local, NULL);
133			if (!error)
134				error = vfs_getopt(mp->mnt_optnew,
135				    "cs_dos", &dos, NULL);
136			if (!error) {
137				msdosfs_iconv->open(win, local, &pmp->pm_u2w);
138				msdosfs_iconv->open(local, win, &pmp->pm_w2u);
139				msdosfs_iconv->open(dos, local, &pmp->pm_u2d);
140				msdosfs_iconv->open(local, dos, &pmp->pm_d2u);
141			}
142			if (error != 0)
143				return (error);
144		} else {
145			pmp->pm_w2u = NULL;
146			pmp->pm_u2w = NULL;
147			pmp->pm_d2u = NULL;
148			pmp->pm_u2d = NULL;
149		}
150	}
151
152	if (vfs_scanopt(mp->mnt_optnew, "gid", "%d", &v) == 1)
153		pmp->pm_gid = v;
154	if (vfs_scanopt(mp->mnt_optnew, "uid", "%d", &v) == 1)
155		pmp->pm_uid = v;
156	if (vfs_scanopt(mp->mnt_optnew, "mask", "%d", &v) == 1)
157		pmp->pm_mask = v & ALLPERMS;
158	if (vfs_scanopt(mp->mnt_optnew, "dirmask", "%d", &v) == 1)
159		pmp->pm_dirmask = v & ALLPERMS;
160	vfs_flagopt(mp->mnt_optnew, "shortname",
161	    &pmp->pm_flags, MSDOSFSMNT_SHORTNAME);
162	vfs_flagopt(mp->mnt_optnew, "shortnames",
163	    &pmp->pm_flags, MSDOSFSMNT_SHORTNAME);
164	vfs_flagopt(mp->mnt_optnew, "longname",
165	    &pmp->pm_flags, MSDOSFSMNT_LONGNAME);
166	vfs_flagopt(mp->mnt_optnew, "longnames",
167	    &pmp->pm_flags, MSDOSFSMNT_LONGNAME);
168	vfs_flagopt(mp->mnt_optnew, "kiconv",
169	    &pmp->pm_flags, MSDOSFSMNT_KICONV);
170
171	if (vfs_getopt(mp->mnt_optnew, "nowin95", NULL, NULL) == 0)
172		pmp->pm_flags |= MSDOSFSMNT_NOWIN95;
173	else
174		pmp->pm_flags &= ~MSDOSFSMNT_NOWIN95;
175
176	if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
177		pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
178	else if (!(pmp->pm_flags &
179	    (MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) {
180		struct vnode *rootvp;
181
182		/*
183		 * Try to divine whether to support Win'95 long filenames
184		 */
185		if (FAT32(pmp))
186			pmp->pm_flags |= MSDOSFSMNT_LONGNAME;
187		else {
188			if ((error =
189			    msdosfs_root(mp, LK_EXCLUSIVE, &rootvp)) != 0)
190				return error;
191			pmp->pm_flags |= findwin95(VTODE(rootvp)) ?
192			    MSDOSFSMNT_LONGNAME : MSDOSFSMNT_SHORTNAME;
193			vput(rootvp);
194		}
195	}
196	return 0;
197}
198
199static int
200msdosfs_cmount(struct mntarg *ma, void *data, uint64_t flags)
201{
202	struct msdosfs_args args;
203	struct export_args exp;
204	int error;
205
206	if (data == NULL)
207		return (EINVAL);
208	error = copyin(data, &args, sizeof args);
209	if (error)
210		return (error);
211	vfs_oexport_conv(&args.export, &exp);
212
213	ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
214	ma = mount_arg(ma, "export", &exp, sizeof(exp));
215	ma = mount_argf(ma, "uid", "%d", args.uid);
216	ma = mount_argf(ma, "gid", "%d", args.gid);
217	ma = mount_argf(ma, "mask", "%d", args.mask);
218	ma = mount_argf(ma, "dirmask", "%d", args.dirmask);
219
220	ma = mount_argb(ma, args.flags & MSDOSFSMNT_SHORTNAME, "noshortname");
221	ma = mount_argb(ma, args.flags & MSDOSFSMNT_LONGNAME, "nolongname");
222	ma = mount_argb(ma, !(args.flags & MSDOSFSMNT_NOWIN95), "nowin95");
223	ma = mount_argb(ma, args.flags & MSDOSFSMNT_KICONV, "nokiconv");
224
225	ma = mount_argsu(ma, "cs_win", args.cs_win, MAXCSLEN);
226	ma = mount_argsu(ma, "cs_dos", args.cs_dos, MAXCSLEN);
227	ma = mount_argsu(ma, "cs_local", args.cs_local, MAXCSLEN);
228
229	error = kernel_mount(ma, flags);
230
231	return (error);
232}
233
234/*
235 * mp - path - addr in user space of mount point (ie /usr or whatever)
236 * data - addr in user space of mount params including the name of the block
237 * special file to treat as a filesystem.
238 */
239static int
240msdosfs_mount(struct mount *mp)
241{
242	struct vnode *devvp;	  /* vnode for blk device to mount */
243	struct thread *td;
244	/* msdosfs specific mount control block */
245	struct msdosfsmount *pmp = NULL;
246	struct nameidata ndp;
247	int error, flags;
248	accmode_t accmode;
249	char *from;
250
251	td = curthread;
252	if (vfs_filteropt(mp->mnt_optnew, msdosfs_opts))
253		return (EINVAL);
254
255	/*
256	 * If updating, check whether changing from read-only to
257	 * read/write; if there is no device name, that's all we do.
258	 */
259	if (mp->mnt_flag & MNT_UPDATE) {
260		pmp = VFSTOMSDOSFS(mp);
261		if (vfs_flagopt(mp->mnt_optnew, "export", NULL, 0)) {
262			/*
263			 * Forbid export requests if filesystem has
264			 * MSDOSFS_LARGEFS flag set.
265			 */
266			if ((pmp->pm_flags & MSDOSFS_LARGEFS) != 0) {
267				vfs_mount_error(mp,
268				    "MSDOSFS_LARGEFS flag set, cannot export");
269				return (EOPNOTSUPP);
270			}
271		}
272		if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) &&
273		    vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
274			error = VFS_SYNC(mp, MNT_WAIT);
275			if (error)
276				return (error);
277			flags = WRITECLOSE;
278			if (mp->mnt_flag & MNT_FORCE)
279				flags |= FORCECLOSE;
280			error = vflush(mp, 0, flags, td);
281			if (error)
282				return (error);
283
284			/*
285			 * Now the volume is clean.  Mark it so while the
286			 * device is still rw.
287			 */
288			error = markvoldirty(pmp, 0);
289			if (error) {
290				(void)markvoldirty(pmp, 1);
291				return (error);
292			}
293
294			/* Downgrade the device from rw to ro. */
295			g_topology_lock();
296			error = g_access(pmp->pm_cp, 0, -1, 0);
297			g_topology_unlock();
298			if (error) {
299				(void)markvoldirty(pmp, 1);
300				return (error);
301			}
302
303			/*
304			 * Backing out after an error was painful in the
305			 * above.  Now we are committed to succeeding.
306			 */
307			pmp->pm_fmod = 0;
308			pmp->pm_flags |= MSDOSFSMNT_RONLY;
309			MNT_ILOCK(mp);
310			mp->mnt_flag |= MNT_RDONLY;
311			MNT_IUNLOCK(mp);
312		} else if ((pmp->pm_flags & MSDOSFSMNT_RONLY) &&
313		    !vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
314			/*
315			 * If upgrade to read-write by non-root, then verify
316			 * that user has necessary permissions on the device.
317			 */
318			devvp = pmp->pm_devvp;
319			vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
320			error = VOP_ACCESS(devvp, VREAD | VWRITE,
321			    td->td_ucred, td);
322			if (error)
323				error = priv_check(td, PRIV_VFS_MOUNT_PERM);
324			if (error) {
325				VOP_UNLOCK(devvp, 0);
326				return (error);
327			}
328			VOP_UNLOCK(devvp, 0);
329			g_topology_lock();
330			error = g_access(pmp->pm_cp, 0, 1, 0);
331			g_topology_unlock();
332			if (error)
333				return (error);
334
335			pmp->pm_fmod = 1;
336			pmp->pm_flags &= ~MSDOSFSMNT_RONLY;
337			MNT_ILOCK(mp);
338			mp->mnt_flag &= ~MNT_RDONLY;
339			MNT_IUNLOCK(mp);
340
341			/* Now that the volume is modifiable, mark it dirty. */
342			error = markvoldirty(pmp, 1);
343			if (error)
344				return (error);
345		}
346	}
347	/*
348	 * Not an update, or updating the name: look up the name
349	 * and verify that it refers to a sensible disk device.
350	 */
351	if (vfs_getopt(mp->mnt_optnew, "from", (void **)&from, NULL))
352		return (EINVAL);
353	NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, from, td);
354	error = namei(&ndp);
355	if (error)
356		return (error);
357	devvp = ndp.ni_vp;
358	NDFREE(&ndp, NDF_ONLY_PNBUF);
359
360	if (!vn_isdisk(devvp, &error)) {
361		vput(devvp);
362		return (error);
363	}
364	/*
365	 * If mount by non-root, then verify that user has necessary
366	 * permissions on the device.
367	 */
368	accmode = VREAD;
369	if ((mp->mnt_flag & MNT_RDONLY) == 0)
370		accmode |= VWRITE;
371	error = VOP_ACCESS(devvp, accmode, td->td_ucred, td);
372	if (error)
373		error = priv_check(td, PRIV_VFS_MOUNT_PERM);
374	if (error) {
375		vput(devvp);
376		return (error);
377	}
378	if ((mp->mnt_flag & MNT_UPDATE) == 0) {
379		error = mountmsdosfs(devvp, mp);
380#ifdef MSDOSFS_DEBUG		/* only needed for the printf below */
381		pmp = VFSTOMSDOSFS(mp);
382#endif
383	} else {
384		vput(devvp);
385		if (devvp != pmp->pm_devvp)
386			return (EINVAL);	/* XXX needs translation */
387	}
388	if (error) {
389		vrele(devvp);
390		return (error);
391	}
392
393	error = update_mp(mp, td);
394	if (error) {
395		if ((mp->mnt_flag & MNT_UPDATE) == 0)
396			msdosfs_unmount(mp, MNT_FORCE);
397		return error;
398	}
399
400	vfs_mountedfrom(mp, from);
401#ifdef MSDOSFS_DEBUG
402	printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap);
403#endif
404	return (0);
405}
406
407static int
408mountmsdosfs(struct vnode *devvp, struct mount *mp)
409{
410	struct msdosfsmount *pmp;
411	struct buf *bp;
412	struct cdev *dev;
413	union bootsector *bsp;
414	struct byte_bpb33 *b33;
415	struct byte_bpb50 *b50;
416	struct byte_bpb710 *b710;
417	u_int8_t SecPerClust;
418	u_long clusters;
419	int ronly, error;
420	struct g_consumer *cp;
421	struct bufobj *bo;
422
423	bp = NULL;		/* This and pmp both used in error_exit. */
424	pmp = NULL;
425	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
426
427	dev = devvp->v_rdev;
428	if (atomic_cmpset_acq_ptr((uintptr_t *)&dev->si_mountpt, 0,
429	    (uintptr_t)mp) == 0) {
430		VOP_UNLOCK(devvp, 0);
431		return (EBUSY);
432	}
433	g_topology_lock();
434	error = g_vfs_open(devvp, &cp, "msdosfs", ronly ? 0 : 1);
435	g_topology_unlock();
436	if (error != 0) {
437		atomic_store_rel_ptr((uintptr_t *)&dev->si_mountpt, 0);
438		VOP_UNLOCK(devvp, 0);
439		return (error);
440	}
441	dev_ref(dev);
442	VOP_UNLOCK(devvp, 0);
443
444	bo = &devvp->v_bufobj;
445
446	/*
447	 * Read the boot sector of the filesystem, and then check the
448	 * boot signature.  If not a dos boot sector then error out.
449	 *
450	 * NOTE: 8192 is a magic size that works for ffs.
451	 */
452	error = bread(devvp, 0, 8192, NOCRED, &bp);
453	if (error)
454		goto error_exit;
455	bp->b_flags |= B_AGE;
456	bsp = (union bootsector *)bp->b_data;
457	b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
458	b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
459	b710 = (struct byte_bpb710 *)bsp->bs710.bsBPB;
460
461#ifndef MSDOSFS_NOCHECKSIG
462	if (bsp->bs50.bsBootSectSig0 != BOOTSIG0
463	    || bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
464		error = EINVAL;
465		goto error_exit;
466	}
467#endif
468
469	pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK | M_ZERO);
470	pmp->pm_mountp = mp;
471	pmp->pm_cp = cp;
472	pmp->pm_bo = bo;
473
474	lockinit(&pmp->pm_fatlock, 0, msdosfs_lock_msg, 0, 0);
475
476	/*
477	 * Initialize ownerships and permissions, since nothing else will
478	 * initialize them iff we are mounting root.
479	 */
480	pmp->pm_uid = UID_ROOT;
481	pmp->pm_gid = GID_WHEEL;
482	pmp->pm_mask = pmp->pm_dirmask = S_IXUSR | S_IXGRP | S_IXOTH |
483	    S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR;
484
485	/*
486	 * Experimental support for large MS-DOS filesystems.
487	 * WARNING: This uses at least 32 bytes of kernel memory (which is not
488	 * reclaimed until the FS is unmounted) for each file on disk to map
489	 * between the 32-bit inode numbers used by VFS and the 64-bit
490	 * pseudo-inode numbers used internally by msdosfs. This is only
491	 * safe to use in certain controlled situations (e.g. read-only FS
492	 * with less than 1 million files).
493	 * Since the mappings do not persist across unmounts (or reboots), these
494	 * filesystems are not suitable for exporting through NFS, or any other
495	 * application that requires fixed inode numbers.
496	 */
497	vfs_flagopt(mp->mnt_optnew, "large", &pmp->pm_flags, MSDOSFS_LARGEFS);
498
499	/*
500	 * Compute several useful quantities from the bpb in the
501	 * bootsector.  Copy in the dos 5 variant of the bpb then fix up
502	 * the fields that are different between dos 5 and dos 3.3.
503	 */
504	SecPerClust = b50->bpbSecPerClust;
505	pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
506	if (pmp->pm_BytesPerSec < DEV_BSIZE) {
507		error = EINVAL;
508		goto error_exit;
509	}
510	pmp->pm_ResSectors = getushort(b50->bpbResSectors);
511	pmp->pm_FATs = b50->bpbFATs;
512	pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
513	pmp->pm_Sectors = getushort(b50->bpbSectors);
514	pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
515	pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
516	pmp->pm_Heads = getushort(b50->bpbHeads);
517	pmp->pm_Media = b50->bpbMedia;
518
519	/* calculate the ratio of sector size to DEV_BSIZE */
520	pmp->pm_BlkPerSec = pmp->pm_BytesPerSec / DEV_BSIZE;
521
522	/*
523	 * We don't check pm_Heads nor pm_SecPerTrack, because
524	 * these may not be set for EFI file systems. We don't
525	 * use these anyway, so we're unaffected if they are
526	 * invalid.
527	 */
528	if (!pmp->pm_BytesPerSec || !SecPerClust) {
529		error = EINVAL;
530		goto error_exit;
531	}
532
533	if (pmp->pm_Sectors == 0) {
534		pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
535		pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
536	} else {
537		pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
538		pmp->pm_HugeSectors = pmp->pm_Sectors;
539	}
540	if (!(pmp->pm_flags & MSDOSFS_LARGEFS)) {
541		if (pmp->pm_HugeSectors > 0xffffffff /
542		    (pmp->pm_BytesPerSec / sizeof(struct direntry)) + 1) {
543			/*
544			 * We cannot deal currently with this size of disk
545			 * due to fileid limitations (see msdosfs_getattr and
546			 * msdosfs_readdir)
547			 */
548			error = EINVAL;
549			vfs_mount_error(mp,
550			    "Disk too big, try '-o large' mount option");
551			goto error_exit;
552		}
553	}
554
555	if (pmp->pm_RootDirEnts == 0) {
556		if (pmp->pm_FATsecs
557		    || getushort(b710->bpbFSVers)) {
558			error = EINVAL;
559#ifdef MSDOSFS_DEBUG
560			printf("mountmsdosfs(): bad FAT32 filesystem\n");
561#endif
562			goto error_exit;
563		}
564		pmp->pm_fatmask = FAT32_MASK;
565		pmp->pm_fatmult = 4;
566		pmp->pm_fatdiv = 1;
567		pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
568		if (getushort(b710->bpbExtFlags) & FATMIRROR)
569			pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
570		else
571			pmp->pm_flags |= MSDOSFS_FATMIRROR;
572	} else
573		pmp->pm_flags |= MSDOSFS_FATMIRROR;
574
575	/*
576	 * Check a few values (could do some more):
577	 * - logical sector size: power of 2, >= block size
578	 * - sectors per cluster: power of 2, >= 1
579	 * - number of sectors:   >= 1, <= size of partition
580	 * - number of FAT sectors: >= 1
581	 */
582	if ( (SecPerClust == 0)
583	  || (SecPerClust & (SecPerClust - 1))
584	  || (pmp->pm_BytesPerSec < DEV_BSIZE)
585	  || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
586	  || (pmp->pm_HugeSectors == 0)
587	  || (pmp->pm_FATsecs == 0)
588	  || (SecPerClust * pmp->pm_BlkPerSec > MAXBSIZE / DEV_BSIZE)
589	) {
590		error = EINVAL;
591		goto error_exit;
592	}
593
594	pmp->pm_HugeSectors *= pmp->pm_BlkPerSec;
595	pmp->pm_HiddenSects *= pmp->pm_BlkPerSec;	/* XXX not used? */
596	pmp->pm_FATsecs     *= pmp->pm_BlkPerSec;
597	SecPerClust         *= pmp->pm_BlkPerSec;
598
599	pmp->pm_fatblk = pmp->pm_ResSectors * pmp->pm_BlkPerSec;
600
601	if (FAT32(pmp)) {
602		pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
603		pmp->pm_firstcluster = pmp->pm_fatblk
604			+ (pmp->pm_FATs * pmp->pm_FATsecs);
605		pmp->pm_fsinfo = getushort(b710->bpbFSInfo) * pmp->pm_BlkPerSec;
606	} else {
607		pmp->pm_rootdirblk = pmp->pm_fatblk +
608			(pmp->pm_FATs * pmp->pm_FATsecs);
609		pmp->pm_rootdirsize = howmany(pmp->pm_RootDirEnts *
610			sizeof(struct direntry), DEV_BSIZE); /* in blocks */
611		pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
612	}
613
614	pmp->pm_maxcluster = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
615	    SecPerClust + 1;
616	pmp->pm_fatsize = pmp->pm_FATsecs * DEV_BSIZE;	/* XXX not used? */
617
618	if (pmp->pm_fatmask == 0) {
619		if (pmp->pm_maxcluster
620		    <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
621			/*
622			 * This will usually be a floppy disk. This size makes
623			 * sure that one fat entry will not be split across
624			 * multiple blocks.
625			 */
626			pmp->pm_fatmask = FAT12_MASK;
627			pmp->pm_fatmult = 3;
628			pmp->pm_fatdiv = 2;
629		} else {
630			pmp->pm_fatmask = FAT16_MASK;
631			pmp->pm_fatmult = 2;
632			pmp->pm_fatdiv = 1;
633		}
634	}
635
636	clusters = (pmp->pm_fatsize / pmp->pm_fatmult) * pmp->pm_fatdiv;
637	if (pmp->pm_maxcluster >= clusters) {
638#ifdef MSDOSFS_DEBUG
639		printf("Warning: number of clusters (%ld) exceeds FAT "
640		    "capacity (%ld)\n", pmp->pm_maxcluster + 1, clusters);
641#endif
642		pmp->pm_maxcluster = clusters - 1;
643	}
644
645	if (FAT12(pmp))
646		pmp->pm_fatblocksize = 3 * 512;
647	else
648		pmp->pm_fatblocksize = PAGE_SIZE;
649	pmp->pm_fatblocksize = roundup(pmp->pm_fatblocksize,
650	    pmp->pm_BytesPerSec);
651	pmp->pm_fatblocksec = pmp->pm_fatblocksize / DEV_BSIZE;
652	pmp->pm_bnshift = ffs(DEV_BSIZE) - 1;
653
654	/*
655	 * Compute mask and shift value for isolating cluster relative byte
656	 * offsets and cluster numbers from a file offset.
657	 */
658	pmp->pm_bpcluster = SecPerClust * DEV_BSIZE;
659	pmp->pm_crbomask = pmp->pm_bpcluster - 1;
660	pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
661
662	/*
663	 * Check for valid cluster size
664	 * must be a power of 2
665	 */
666	if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
667		error = EINVAL;
668		goto error_exit;
669	}
670
671	/*
672	 * Release the bootsector buffer.
673	 */
674	brelse(bp);
675	bp = NULL;
676
677	/*
678	 * Check the fsinfo sector if we have one.  Silently fix up our
679	 * in-core copy of fp->fsinxtfree if it is unknown (0xffffffff)
680	 * or too large.  Ignore fp->fsinfree for now, since we need to
681	 * read the entire FAT anyway to fill the inuse map.
682	 */
683	if (pmp->pm_fsinfo) {
684		struct fsinfo *fp;
685
686		if ((error = bread(devvp, pmp->pm_fsinfo, pmp->pm_BytesPerSec,
687		    NOCRED, &bp)) != 0)
688			goto error_exit;
689		fp = (struct fsinfo *)bp->b_data;
690		if (!bcmp(fp->fsisig1, "RRaA", 4)
691		    && !bcmp(fp->fsisig2, "rrAa", 4)
692		    && !bcmp(fp->fsisig3, "\0\0\125\252", 4)) {
693			pmp->pm_nxtfree = getulong(fp->fsinxtfree);
694			if (pmp->pm_nxtfree > pmp->pm_maxcluster)
695				pmp->pm_nxtfree = CLUST_FIRST;
696		} else
697			pmp->pm_fsinfo = 0;
698		brelse(bp);
699		bp = NULL;
700	}
701
702	/*
703	 * Finish initializing pmp->pm_nxtfree (just in case the first few
704	 * sectors aren't properly reserved in the FAT).  This completes
705	 * the fixup for fp->fsinxtfree, and fixes up the zero-initialized
706	 * value if there is no fsinfo.  We will use pmp->pm_nxtfree
707	 * internally even if there is no fsinfo.
708	 */
709	if (pmp->pm_nxtfree < CLUST_FIRST)
710		pmp->pm_nxtfree = CLUST_FIRST;
711
712	/*
713	 * Allocate memory for the bitmap of allocated clusters, and then
714	 * fill it in.
715	 */
716	pmp->pm_inusemap = malloc(howmany(pmp->pm_maxcluster + 1, N_INUSEBITS)
717				  * sizeof(*pmp->pm_inusemap),
718				  M_MSDOSFSFAT, M_WAITOK);
719
720	/*
721	 * fillinusemap() needs pm_devvp.
722	 */
723	pmp->pm_devvp = devvp;
724	pmp->pm_dev = dev;
725
726	/*
727	 * Have the inuse map filled in.
728	 */
729	MSDOSFS_LOCK_MP(pmp);
730	error = fillinusemap(pmp);
731	MSDOSFS_UNLOCK_MP(pmp);
732	if (error != 0)
733		goto error_exit;
734
735	/*
736	 * If they want fat updates to be synchronous then let them suffer
737	 * the performance degradation in exchange for the on disk copy of
738	 * the fat being correct just about all the time.  I suppose this
739	 * would be a good thing to turn on if the kernel is still flakey.
740	 */
741	if (mp->mnt_flag & MNT_SYNCHRONOUS)
742		pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
743
744	/*
745	 * Finish up.
746	 */
747	if (ronly)
748		pmp->pm_flags |= MSDOSFSMNT_RONLY;
749	else {
750		if ((error = markvoldirty(pmp, 1)) != 0) {
751			(void)markvoldirty(pmp, 0);
752			goto error_exit;
753		}
754		pmp->pm_fmod = 1;
755	}
756	mp->mnt_data =  pmp;
757	mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
758	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
759	MNT_ILOCK(mp);
760	mp->mnt_flag |= MNT_LOCAL;
761	mp->mnt_kern_flag |= MNTK_USES_BCACHE;
762	MNT_IUNLOCK(mp);
763
764	if (pmp->pm_flags & MSDOSFS_LARGEFS)
765		msdosfs_fileno_init(mp);
766
767	return 0;
768
769error_exit:
770	if (bp)
771		brelse(bp);
772	if (cp != NULL) {
773		g_topology_lock();
774		g_vfs_close(cp);
775		g_topology_unlock();
776	}
777	if (pmp) {
778		lockdestroy(&pmp->pm_fatlock);
779		if (pmp->pm_inusemap)
780			free(pmp->pm_inusemap, M_MSDOSFSFAT);
781		free(pmp, M_MSDOSFSMNT);
782		mp->mnt_data = NULL;
783	}
784	atomic_store_rel_ptr((uintptr_t *)&dev->si_mountpt, 0);
785	dev_rel(dev);
786	return (error);
787}
788
789/*
790 * Unmount the filesystem described by mp.
791 */
792static int
793msdosfs_unmount(struct mount *mp, int mntflags)
794{
795	struct msdosfsmount *pmp;
796	int error, flags;
797
798	error = flags = 0;
799	pmp = VFSTOMSDOSFS(mp);
800	if ((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0)
801		error = msdosfs_sync(mp, MNT_WAIT);
802	if ((mntflags & MNT_FORCE) != 0)
803		flags |= FORCECLOSE;
804	else if (error != 0)
805		return (error);
806	error = vflush(mp, 0, flags, curthread);
807	if (error != 0 && error != ENXIO)
808		return (error);
809	if ((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0) {
810		error = markvoldirty(pmp, 0);
811		if (error && error != ENXIO) {
812			(void)markvoldirty(pmp, 1);
813			return (error);
814		}
815	}
816	if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
817		if (pmp->pm_w2u)
818			msdosfs_iconv->close(pmp->pm_w2u);
819		if (pmp->pm_u2w)
820			msdosfs_iconv->close(pmp->pm_u2w);
821		if (pmp->pm_d2u)
822			msdosfs_iconv->close(pmp->pm_d2u);
823		if (pmp->pm_u2d)
824			msdosfs_iconv->close(pmp->pm_u2d);
825	}
826
827#ifdef MSDOSFS_DEBUG
828	{
829		struct vnode *vp = pmp->pm_devvp;
830		struct bufobj *bo;
831
832		bo = &vp->v_bufobj;
833		BO_LOCK(bo);
834		VI_LOCK(vp);
835		vn_printf(vp,
836		    "msdosfs_umount(): just before calling VOP_CLOSE()\n");
837		printf("freef %p, freeb %p, mount %p\n",
838		    TAILQ_NEXT(vp, v_actfreelist), vp->v_actfreelist.tqe_prev,
839		    vp->v_mount);
840		printf("cleanblkhd %p, dirtyblkhd %p, numoutput %ld, type %d\n",
841		    TAILQ_FIRST(&vp->v_bufobj.bo_clean.bv_hd),
842		    TAILQ_FIRST(&vp->v_bufobj.bo_dirty.bv_hd),
843		    vp->v_bufobj.bo_numoutput, vp->v_type);
844		VI_UNLOCK(vp);
845		BO_UNLOCK(bo);
846	}
847#endif
848	g_topology_lock();
849	g_vfs_close(pmp->pm_cp);
850	g_topology_unlock();
851	atomic_store_rel_ptr((uintptr_t *)&pmp->pm_dev->si_mountpt, 0);
852	vrele(pmp->pm_devvp);
853	dev_rel(pmp->pm_dev);
854	free(pmp->pm_inusemap, M_MSDOSFSFAT);
855	if (pmp->pm_flags & MSDOSFS_LARGEFS)
856		msdosfs_fileno_free(mp);
857	lockdestroy(&pmp->pm_fatlock);
858	free(pmp, M_MSDOSFSMNT);
859	mp->mnt_data = NULL;
860	MNT_ILOCK(mp);
861	mp->mnt_flag &= ~MNT_LOCAL;
862	MNT_IUNLOCK(mp);
863	return (error);
864}
865
866static int
867msdosfs_root(struct mount *mp, int flags, struct vnode **vpp)
868{
869	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
870	struct denode *ndep;
871	int error;
872
873#ifdef MSDOSFS_DEBUG
874	printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
875#endif
876	error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep);
877	if (error)
878		return (error);
879	*vpp = DETOV(ndep);
880	return (0);
881}
882
883static int
884msdosfs_statfs(struct mount *mp, struct statfs *sbp)
885{
886	struct msdosfsmount *pmp;
887
888	pmp = VFSTOMSDOSFS(mp);
889	sbp->f_bsize = pmp->pm_bpcluster;
890	sbp->f_iosize = pmp->pm_bpcluster;
891	sbp->f_blocks = pmp->pm_maxcluster + 1;
892	sbp->f_bfree = pmp->pm_freeclustercount;
893	sbp->f_bavail = pmp->pm_freeclustercount;
894	sbp->f_files = pmp->pm_RootDirEnts;	/* XXX */
895	sbp->f_ffree = 0;	/* what to put in here? */
896	return (0);
897}
898
899/*
900 * If we have an FSInfo block, update it.
901 */
902static int
903msdosfs_fsiflush(struct msdosfsmount *pmp, int waitfor)
904{
905	struct fsinfo *fp;
906	struct buf *bp;
907	int error;
908
909	MSDOSFS_LOCK_MP(pmp);
910	if (pmp->pm_fsinfo == 0 || (pmp->pm_flags & MSDOSFS_FSIMOD) == 0) {
911		error = 0;
912		goto unlock;
913	}
914	error = bread(pmp->pm_devvp, pmp->pm_fsinfo, pmp->pm_BytesPerSec,
915	    NOCRED, &bp);
916	if (error != 0) {
917		brelse(bp);
918		goto unlock;
919	}
920	fp = (struct fsinfo *)bp->b_data;
921	putulong(fp->fsinfree, pmp->pm_freeclustercount);
922	putulong(fp->fsinxtfree, pmp->pm_nxtfree);
923	pmp->pm_flags &= ~MSDOSFS_FSIMOD;
924	if (waitfor == MNT_WAIT)
925		error = bwrite(bp);
926	else
927		bawrite(bp);
928unlock:
929	MSDOSFS_UNLOCK_MP(pmp);
930	return (error);
931}
932
933static int
934msdosfs_sync(struct mount *mp, int waitfor)
935{
936	struct vnode *vp, *nvp;
937	struct thread *td;
938	struct denode *dep;
939	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
940	int error, allerror = 0;
941
942	td = curthread;
943
944	/*
945	 * If we ever switch to not updating all of the fats all the time,
946	 * this would be the place to update them from the first one.
947	 */
948	if (pmp->pm_fmod != 0) {
949		if (pmp->pm_flags & MSDOSFSMNT_RONLY)
950			panic("msdosfs_sync: rofs mod");
951		else {
952			/* update fats here */
953		}
954	}
955	/*
956	 * Write back each (modified) denode.
957	 */
958loop:
959	MNT_VNODE_FOREACH_ALL(vp, mp, nvp) {
960		if (vp->v_type == VNON) {
961			VI_UNLOCK(vp);
962			continue;
963		}
964		dep = VTODE(vp);
965		if ((dep->de_flag &
966		    (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 &&
967		    (vp->v_bufobj.bo_dirty.bv_cnt == 0 ||
968		    waitfor == MNT_LAZY)) {
969			VI_UNLOCK(vp);
970			continue;
971		}
972		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, td);
973		if (error) {
974			if (error == ENOENT)
975				goto loop;
976			continue;
977		}
978		error = VOP_FSYNC(vp, waitfor, td);
979		if (error)
980			allerror = error;
981		VOP_UNLOCK(vp, 0);
982		vrele(vp);
983	}
984
985	/*
986	 * Flush filesystem control info.
987	 */
988	if (waitfor != MNT_LAZY) {
989		vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY);
990		error = VOP_FSYNC(pmp->pm_devvp, waitfor, td);
991		if (error)
992			allerror = error;
993		VOP_UNLOCK(pmp->pm_devvp, 0);
994	}
995
996	error = msdosfs_fsiflush(pmp, waitfor);
997	if (error != 0)
998		allerror = error;
999	return (allerror);
1000}
1001
1002static int
1003msdosfs_fhtovp(struct mount *mp, struct fid *fhp, int flags, struct vnode **vpp)
1004{
1005	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
1006	struct defid *defhp = (struct defid *) fhp;
1007	struct denode *dep;
1008	int error;
1009
1010	error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep);
1011	if (error) {
1012		*vpp = NULLVP;
1013		return (error);
1014	}
1015	*vpp = DETOV(dep);
1016	vnode_create_vobject(*vpp, dep->de_FileSize, curthread);
1017	return (0);
1018}
1019
1020static struct vfsops msdosfs_vfsops = {
1021	.vfs_fhtovp =		msdosfs_fhtovp,
1022	.vfs_mount =		msdosfs_mount,
1023	.vfs_cmount =		msdosfs_cmount,
1024	.vfs_root =		msdosfs_root,
1025	.vfs_statfs =		msdosfs_statfs,
1026	.vfs_sync =		msdosfs_sync,
1027	.vfs_unmount =		msdosfs_unmount,
1028};
1029
1030VFS_SET(msdosfs_vfsops, msdosfs, 0);
1031MODULE_VERSION(msdosfs, 1);
1032