msdosfs_vfsops.c revision 247685
11558Srgrimes/* $FreeBSD: stable/9/sys/fs/msdosfs/msdosfs_vfsops.c 247685 2013-03-03 07:09:25Z kib $ */
21558Srgrimes/*	$NetBSD: msdosfs_vfsops.c,v 1.51 1997/11/17 15:36:58 ws Exp $	*/
31558Srgrimes
41558Srgrimes/*-
51558Srgrimes * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
61558Srgrimes * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
71558Srgrimes * All rights reserved.
81558Srgrimes * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
91558Srgrimes *
101558Srgrimes * Redistribution and use in source and binary forms, with or without
111558Srgrimes * modification, are permitted provided that the following conditions
121558Srgrimes * are met:
131558Srgrimes * 1. Redistributions of source code must retain the above copyright
141558Srgrimes *    notice, this list of conditions and the following disclaimer.
151558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
161558Srgrimes *    notice, this list of conditions and the following disclaimer in the
171558Srgrimes *    documentation and/or other materials provided with the distribution.
181558Srgrimes * 3. All advertising materials mentioning features or use of this software
191558Srgrimes *    must display the following acknowledgement:
201558Srgrimes *	This product includes software developed by TooLs GmbH.
211558Srgrimes * 4. The name of TooLs GmbH may not be used to endorse or promote products
221558Srgrimes *    derived from this software without specific prior written permission.
231558Srgrimes *
241558Srgrimes * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
251558Srgrimes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
261558Srgrimes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
271558Srgrimes * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
281558Srgrimes * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
291558Srgrimes * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
301558Srgrimes * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
311558Srgrimes * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
321558Srgrimes * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
331558Srgrimes * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
341558Srgrimes */
3538039Scharnier/*-
361558Srgrimes * Written by Paul Popelka (paulp@uts.amdahl.com)
371558Srgrimes *
381558Srgrimes * You can do anything you want with this software, just don't say you wrote
391558Srgrimes * it, and don't remove this notice.
401558Srgrimes *
4138039Scharnier * This software is provided "as is".
421558Srgrimes *
4338039Scharnier * The author supplies this software to be publicly redistributed on the
4438039Scharnier * understanding that the author is not responsible for the correct
4550476Speter * functioning of this software in any circumstances and is not liable for
461558Srgrimes * any damages caused by this software.
471558Srgrimes *
48108375Sdillon * October 1992
49108375Sdillon */
50108375Sdillon
51108375Sdillon#include <sys/param.h>
52108375Sdillon#include <sys/systm.h>
5338039Scharnier#include <sys/buf.h>
5438039Scharnier#include <sys/conf.h>
551558Srgrimes#include <sys/fcntl.h>
561558Srgrimes#include <sys/iconv.h>
5778732Sdd#include <sys/kernel.h>
5838039Scharnier#include <sys/lock.h>
596661Sphk#include <sys/malloc.h>
60108375Sdillon#include <sys/mount.h>
611558Srgrimes#include <sys/mutex.h>
62108375Sdillon#include <sys/namei.h>
63108375Sdillon#include <sys/priv.h>
64108375Sdillon#include <sys/proc.h>
656661Sphk#include <sys/stat.h>
66108375Sdillon#include <sys/vnode.h>
67108375Sdillon
686661Sphk#include <geom/geom.h>
696661Sphk#include <geom/geom_vfs.h>
701558Srgrimes
7192806Sobrien#include <fs/msdosfs/bootsect.h>
72108375Sdillon#include <fs/msdosfs/bpb.h>
7392806Sobrien#include <fs/msdosfs/direntry.h>
741558Srgrimes#include <fs/msdosfs/denode.h>
75108375Sdillon#include <fs/msdosfs/fat.h>
761558Srgrimes#include <fs/msdosfs/msdosfsmount.h>
77108375Sdillon
78108375Sdillonstatic const char msdosfs_lock_msg[] = "fatlk";
79108375Sdillon
80108375Sdillon/* Mount options that we support. */
81108375Sdillonstatic const char *msdosfs_opts[] = {
82108375Sdillon	"async", "noatime", "noclusterr", "noclusterw",
83108375Sdillon	"export", "force", "from", "sync",
84108375Sdillon	"cs_dos", "cs_local", "cs_win", "dirmask",
851558Srgrimes	"gid", "kiconv", "large", "longname",
86108375Sdillon	"longnames", "mask", "shortname", "shortnames",
87108375Sdillon	"uid", "win95", "nowin95",
88108375Sdillon	NULL
89108375Sdillon};
90108375Sdillon
91108375Sdillon#if 1 /*def PC98*/
92108375Sdillon/*
93108375Sdillon * XXX - The boot signature formatted by NEC PC-98 DOS looks like a
94108375Sdillon *       garbage or a random value :-{
95108375Sdillon *       If you want to use that broken-signatured media, define the
961558Srgrimes *       following symbol even though PC/AT.
97108375Sdillon *       (ex. mount PC-98 DOS formatted FD on PC/AT)
98108375Sdillon */
99108375Sdillon#define	MSDOSFS_NOCHECKSIG
100108375Sdillon#endif
1011558Srgrimes
102108375SdillonMALLOC_DEFINE(M_MSDOSFSMNT, "msdosfs_mount", "MSDOSFS mount structure");
103108375Sdillonstatic MALLOC_DEFINE(M_MSDOSFSFAT, "msdosfs_fat", "MSDOSFS file allocation table");
104108375Sdillon
105108375Sdillonstruct iconv_functions *msdosfs_iconv;
106108375Sdillon
107108375Sdillonstatic int	update_mp(struct mount *mp, struct thread *td);
108108375Sdillonstatic int	mountmsdosfs(struct vnode *devvp, struct mount *mp);
109108375Sdillonstatic vfs_fhtovp_t	msdosfs_fhtovp;
110108375Sdillonstatic vfs_mount_t	msdosfs_mount;
111108375Sdillonstatic vfs_root_t	msdosfs_root;
112108375Sdillonstatic vfs_statfs_t	msdosfs_statfs;
113108375Sdillonstatic vfs_sync_t	msdosfs_sync;
114108375Sdillonstatic vfs_unmount_t	msdosfs_unmount;
115108375Sdillon
116108375Sdillon/* Maximum length of a character set name (arbitrary). */
117108375Sdillon#define	MAXCSLEN	64
118108375Sdillon
119108375Sdillonstatic int
120108375Sdillonupdate_mp(struct mount *mp, struct thread *td)
121108375Sdillon{
122108375Sdillon	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
123108375Sdillon	void *dos, *win, *local;
124108375Sdillon	int error, v;
125108375Sdillon
126108375Sdillon	if (!vfs_getopt(mp->mnt_optnew, "kiconv", NULL, NULL)) {
127108375Sdillon		if (msdosfs_iconv != NULL) {
1281558Srgrimes			error = vfs_getopt(mp->mnt_optnew,
1291558Srgrimes			    "cs_win", &win, NULL);
130108375Sdillon			if (!error)
1311558Srgrimes				error = vfs_getopt(mp->mnt_optnew,
132108375Sdillon				    "cs_local", &local, NULL);
1331558Srgrimes			if (!error)
1341558Srgrimes				error = vfs_getopt(mp->mnt_optnew,
1351558Srgrimes				    "cs_dos", &dos, NULL);
136108375Sdillon			if (!error) {
137108375Sdillon				msdosfs_iconv->open(win, local, &pmp->pm_u2w);
138108375Sdillon				msdosfs_iconv->open(local, win, &pmp->pm_w2u);
139108375Sdillon				msdosfs_iconv->open(dos, local, &pmp->pm_u2d);
140108375Sdillon				msdosfs_iconv->open(local, dos, &pmp->pm_d2u);
141108375Sdillon			}
142108375Sdillon			if (error != 0)
143111424Sdas				return (error);
144108375Sdillon		} else {
145108375Sdillon			pmp->pm_w2u = NULL;
146108375Sdillon			pmp->pm_u2w = NULL;
147108375Sdillon			pmp->pm_d2u = NULL;
148108375Sdillon			pmp->pm_u2d = NULL;
149108375Sdillon		}
150108375Sdillon	}
151108375Sdillon
152108375Sdillon	if (vfs_scanopt(mp->mnt_optnew, "gid", "%d", &v) == 1)
153108375Sdillon		pmp->pm_gid = v;
154108375Sdillon	if (vfs_scanopt(mp->mnt_optnew, "uid", "%d", &v) == 1)
155108375Sdillon		pmp->pm_uid = v;
1561558Srgrimes	if (vfs_scanopt(mp->mnt_optnew, "mask", "%d", &v) == 1)
157108375Sdillon		pmp->pm_mask = v & ALLPERMS;
158107913Sdillon	if (vfs_scanopt(mp->mnt_optnew, "dirmask", "%d", &v) == 1)
159108375Sdillon		pmp->pm_dirmask = v & ALLPERMS;
160108375Sdillon	vfs_flagopt(mp->mnt_optnew, "shortname",
161108375Sdillon	    &pmp->pm_flags, MSDOSFSMNT_SHORTNAME);
1621558Srgrimes	vfs_flagopt(mp->mnt_optnew, "shortnames",
163108375Sdillon	    &pmp->pm_flags, MSDOSFSMNT_SHORTNAME);
164108375Sdillon	vfs_flagopt(mp->mnt_optnew, "longname",
165108375Sdillon	    &pmp->pm_flags, MSDOSFSMNT_LONGNAME);
166108375Sdillon	vfs_flagopt(mp->mnt_optnew, "longnames",
167108375Sdillon	    &pmp->pm_flags, MSDOSFSMNT_LONGNAME);
168108375Sdillon	vfs_flagopt(mp->mnt_optnew, "kiconv",
1691558Srgrimes	    &pmp->pm_flags, MSDOSFSMNT_KICONV);
1701558Srgrimes
1711558Srgrimes	if (vfs_getopt(mp->mnt_optnew, "nowin95", NULL, NULL) == 0)
172108375Sdillon		pmp->pm_flags |= MSDOSFSMNT_NOWIN95;
173111424Sdas	else
1741558Srgrimes		pmp->pm_flags &= ~MSDOSFSMNT_NOWIN95;
175108375Sdillon
1761558Srgrimes	if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
1771558Srgrimes		pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
178111424Sdas	else if (!(pmp->pm_flags &
17926740Scharnier	    (MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) {
1801558Srgrimes		struct vnode *rootvp;
181111424Sdas
182111424Sdas		/*
183111424Sdas		 * Try to divine whether to support Win'95 long filenames
184111424Sdas		 */
185111424Sdas		if (FAT32(pmp))
186111424Sdas			pmp->pm_flags |= MSDOSFSMNT_LONGNAME;
1871558Srgrimes		else {
18826740Scharnier			if ((error =
1891558Srgrimes			    msdosfs_root(mp, LK_EXCLUSIVE, &rootvp)) != 0)
1901558Srgrimes				return error;
1911558Srgrimes			pmp->pm_flags |= findwin95(VTODE(rootvp)) ?
1921558Srgrimes			    MSDOSFSMNT_LONGNAME : MSDOSFSMNT_SHORTNAME;
1931558Srgrimes			vput(rootvp);
1941558Srgrimes		}
1951558Srgrimes	}
19626740Scharnier	return 0;
197108375Sdillon}
1981558Srgrimes
199108375Sdillonstatic int
200108375Sdillonmsdosfs_cmount(struct mntarg *ma, void *data, uint64_t flags)
201108375Sdillon{
202108375Sdillon	struct msdosfs_args args;
203108375Sdillon	struct export_args exp;
204108375Sdillon	int error;
205108375Sdillon
206108375Sdillon	if (data == NULL)
207108375Sdillon		return (EINVAL);
208108375Sdillon	error = copyin(data, &args, sizeof args);
209108375Sdillon	if (error)
210108375Sdillon		return (error);
2111558Srgrimes	vfs_oexport_conv(&args.export, &exp);
2121558Srgrimes
213107913Sdillon	ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
214108375Sdillon	ma = mount_arg(ma, "export", &exp, sizeof(exp));
215108375Sdillon	ma = mount_argf(ma, "uid", "%d", args.uid);
216107913Sdillon	ma = mount_argf(ma, "gid", "%d", args.gid);
217108375Sdillon	ma = mount_argf(ma, "mask", "%d", args.mask);
218108375Sdillon	ma = mount_argf(ma, "dirmask", "%d", args.dirmask);
219108425Smike
220108375Sdillon	ma = mount_argb(ma, args.flags & MSDOSFSMNT_SHORTNAME, "noshortname");
221108375Sdillon	ma = mount_argb(ma, args.flags & MSDOSFSMNT_LONGNAME, "nolongname");
222108375Sdillon	ma = mount_argb(ma, !(args.flags & MSDOSFSMNT_NOWIN95), "nowin95");
223108375Sdillon	ma = mount_argb(ma, args.flags & MSDOSFSMNT_KICONV, "nokiconv");
224108375Sdillon
225108375Sdillon	ma = mount_argsu(ma, "cs_win", args.cs_win, MAXCSLEN);
226108375Sdillon	ma = mount_argsu(ma, "cs_dos", args.cs_dos, MAXCSLEN);
227108375Sdillon	ma = mount_argsu(ma, "cs_local", args.cs_local, MAXCSLEN);
228108375Sdillon
229108375Sdillon	error = kernel_mount(ma, flags);
230108375Sdillon
231108375Sdillon	return (error);
232108375Sdillon}
233108375Sdillon
234108375Sdillon/*
235108375Sdillon * mp - path - addr in user space of mount point (ie /usr or whatever)
236108375Sdillon * data - addr in user space of mount params including the name of the block
237108459Smike * special file to treat as a filesystem.
238108375Sdillon */
239108375Sdillonstatic int
240108375Sdillonmsdosfs_mount(struct mount *mp)
241108375Sdillon{
242108375Sdillon	struct vnode *devvp;	  /* vnode for blk device to mount */
243108375Sdillon	struct thread *td;
244108375Sdillon	/* msdosfs specific mount control block */
245108375Sdillon	struct msdosfsmount *pmp = NULL;
246108375Sdillon	struct nameidata ndp;
247108375Sdillon	int error, flags;
248108375Sdillon	accmode_t accmode;
249108375Sdillon	char *from;
250108375Sdillon
251108375Sdillon	td = curthread;
252108375Sdillon	if (vfs_filteropt(mp->mnt_optnew, msdosfs_opts))
253108375Sdillon		return (EINVAL);
254108375Sdillon
255108375Sdillon	/*
256108375Sdillon	 * If updating, check whether changing from read-only to
257108375Sdillon	 * read/write; if there is no device name, that's all we do.
258108375Sdillon	 */
259108375Sdillon	if (mp->mnt_flag & MNT_UPDATE) {
260108375Sdillon		pmp = VFSTOMSDOSFS(mp);
261108375Sdillon		if (vfs_flagopt(mp->mnt_optnew, "export", NULL, 0)) {
262108375Sdillon			/*
263108375Sdillon			 * Forbid export requests if filesystem has
264108375Sdillon			 * MSDOSFS_LARGEFS flag set.
265108375Sdillon			 */
266108375Sdillon			if ((pmp->pm_flags & MSDOSFS_LARGEFS) != 0) {
267108375Sdillon				vfs_mount_error(mp,
268108375Sdillon				    "MSDOSFS_LARGEFS flag set, cannot export");
269108375Sdillon				return (EOPNOTSUPP);
270108375Sdillon			}
271108375Sdillon		}
272108375Sdillon		if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) &&
273108375Sdillon		    vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
274108375Sdillon			error = VFS_SYNC(mp, MNT_WAIT);
275108375Sdillon			if (error)
276108375Sdillon				return (error);
277108375Sdillon			flags = WRITECLOSE;
278108375Sdillon			if (mp->mnt_flag & MNT_FORCE)
279108375Sdillon				flags |= FORCECLOSE;
280108375Sdillon			error = vflush(mp, 0, flags, td);
281108375Sdillon			if (error)
282107913Sdillon				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			DROP_GIANT();
296			g_topology_lock();
297			error = g_access(pmp->pm_cp, 0, -1, 0);
298			g_topology_unlock();
299			PICKUP_GIANT();
300			if (error) {
301				(void)markvoldirty(pmp, 1);
302				return (error);
303			}
304
305			/*
306			 * Backing out after an error was painful in the
307			 * above.  Now we are committed to succeeding.
308			 */
309			pmp->pm_fmod = 0;
310			pmp->pm_flags |= MSDOSFSMNT_RONLY;
311			MNT_ILOCK(mp);
312			mp->mnt_flag |= MNT_RDONLY;
313			MNT_IUNLOCK(mp);
314		} else if ((pmp->pm_flags & MSDOSFSMNT_RONLY) &&
315		    !vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
316			/*
317			 * If upgrade to read-write by non-root, then verify
318			 * that user has necessary permissions on the device.
319			 */
320			devvp = pmp->pm_devvp;
321			vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
322			error = VOP_ACCESS(devvp, VREAD | VWRITE,
323			    td->td_ucred, td);
324			if (error)
325				error = priv_check(td, PRIV_VFS_MOUNT_PERM);
326			if (error) {
327				VOP_UNLOCK(devvp, 0);
328				return (error);
329			}
330			VOP_UNLOCK(devvp, 0);
331			DROP_GIANT();
332			g_topology_lock();
333			error = g_access(pmp->pm_cp, 0, 1, 0);
334			g_topology_unlock();
335			PICKUP_GIANT();
336			if (error)
337				return (error);
338
339			pmp->pm_fmod = 1;
340			pmp->pm_flags &= ~MSDOSFSMNT_RONLY;
341			MNT_ILOCK(mp);
342			mp->mnt_flag &= ~MNT_RDONLY;
343			MNT_IUNLOCK(mp);
344
345			/* Now that the volume is modifiable, mark it dirty. */
346			error = markvoldirty(pmp, 1);
347			if (error)
348				return (error);
349		}
350	}
351	/*
352	 * Not an update, or updating the name: look up the name
353	 * and verify that it refers to a sensible disk device.
354	 */
355	if (vfs_getopt(mp->mnt_optnew, "from", (void **)&from, NULL))
356		return (EINVAL);
357	NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, from, td);
358	error = namei(&ndp);
359	if (error)
360		return (error);
361	devvp = ndp.ni_vp;
362	NDFREE(&ndp, NDF_ONLY_PNBUF);
363
364	if (!vn_isdisk(devvp, &error)) {
365		vput(devvp);
366		return (error);
367	}
368	/*
369	 * If mount by non-root, then verify that user has necessary
370	 * permissions on the device.
371	 */
372	accmode = VREAD;
373	if ((mp->mnt_flag & MNT_RDONLY) == 0)
374		accmode |= VWRITE;
375	error = VOP_ACCESS(devvp, accmode, td->td_ucred, td);
376	if (error)
377		error = priv_check(td, PRIV_VFS_MOUNT_PERM);
378	if (error) {
379		vput(devvp);
380		return (error);
381	}
382	if ((mp->mnt_flag & MNT_UPDATE) == 0) {
383		error = mountmsdosfs(devvp, mp);
384#ifdef MSDOSFS_DEBUG		/* only needed for the printf below */
385		pmp = VFSTOMSDOSFS(mp);
386#endif
387	} else {
388		vput(devvp);
389		if (devvp != pmp->pm_devvp)
390			return (EINVAL);	/* XXX needs translation */
391	}
392	if (error) {
393		vrele(devvp);
394		return (error);
395	}
396
397	error = update_mp(mp, td);
398	if (error) {
399		if ((mp->mnt_flag & MNT_UPDATE) == 0)
400			msdosfs_unmount(mp, MNT_FORCE);
401		return error;
402	}
403
404	if (devvp->v_type == VCHR && devvp->v_rdev != NULL)
405		devvp->v_rdev->si_mountpt = mp;
406	vfs_mountedfrom(mp, from);
407#ifdef MSDOSFS_DEBUG
408	printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap);
409#endif
410	return (0);
411}
412
413static int
414mountmsdosfs(struct vnode *devvp, struct mount *mp)
415{
416	struct msdosfsmount *pmp;
417	struct buf *bp;
418	struct cdev *dev;
419	union bootsector *bsp;
420	struct byte_bpb33 *b33;
421	struct byte_bpb50 *b50;
422	struct byte_bpb710 *b710;
423	u_int8_t SecPerClust;
424	u_long clusters;
425	int ronly, error;
426	struct g_consumer *cp;
427	struct bufobj *bo;
428
429	bp = NULL;		/* This and pmp both used in error_exit. */
430	pmp = NULL;
431	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
432
433	dev = devvp->v_rdev;
434	dev_ref(dev);
435	DROP_GIANT();
436	g_topology_lock();
437	error = g_vfs_open(devvp, &cp, "msdosfs", ronly ? 0 : 1);
438	g_topology_unlock();
439	PICKUP_GIANT();
440	VOP_UNLOCK(devvp, 0);
441	if (error)
442		goto error_exit;
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 = (pmp->pm_RootDirEnts * sizeof(struct direntry)
610				       + DEV_BSIZE - 1)
611			/ DEV_BSIZE; /* in blocks */
612		pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
613	}
614
615	pmp->pm_maxcluster = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
616	    SecPerClust + 1;
617	pmp->pm_fatsize = pmp->pm_FATsecs * DEV_BSIZE;	/* XXX not used? */
618
619	if (pmp->pm_fatmask == 0) {
620		if (pmp->pm_maxcluster
621		    <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
622			/*
623			 * This will usually be a floppy disk. This size makes
624			 * sure that one fat entry will not be split across
625			 * multiple blocks.
626			 */
627			pmp->pm_fatmask = FAT12_MASK;
628			pmp->pm_fatmult = 3;
629			pmp->pm_fatdiv = 2;
630		} else {
631			pmp->pm_fatmask = FAT16_MASK;
632			pmp->pm_fatmult = 2;
633			pmp->pm_fatdiv = 1;
634		}
635	}
636
637	clusters = (pmp->pm_fatsize / pmp->pm_fatmult) * pmp->pm_fatdiv;
638	if (pmp->pm_maxcluster >= clusters) {
639#ifdef MSDOSFS_DEBUG
640		printf("Warning: number of clusters (%ld) exceeds FAT "
641		    "capacity (%ld)\n", pmp->pm_maxcluster + 1, clusters);
642#endif
643		pmp->pm_maxcluster = clusters - 1;
644	}
645
646	if (FAT12(pmp))
647		pmp->pm_fatblocksize = 3 * 512;
648	else
649		pmp->pm_fatblocksize = PAGE_SIZE;
650	pmp->pm_fatblocksize = roundup(pmp->pm_fatblocksize,
651	    pmp->pm_BytesPerSec);
652	pmp->pm_fatblocksec = pmp->pm_fatblocksize / DEV_BSIZE;
653	pmp->pm_bnshift = ffs(DEV_BSIZE) - 1;
654
655	/*
656	 * Compute mask and shift value for isolating cluster relative byte
657	 * offsets and cluster numbers from a file offset.
658	 */
659	pmp->pm_bpcluster = SecPerClust * DEV_BSIZE;
660	pmp->pm_crbomask = pmp->pm_bpcluster - 1;
661	pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
662
663	/*
664	 * Check for valid cluster size
665	 * must be a power of 2
666	 */
667	if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
668		error = EINVAL;
669		goto error_exit;
670	}
671
672	/*
673	 * Release the bootsector buffer.
674	 */
675	brelse(bp);
676	bp = NULL;
677
678	/*
679	 * Check the fsinfo sector if we have one.  Silently fix up our
680	 * in-core copy of fp->fsinxtfree if it is unknown (0xffffffff)
681	 * or too large.  Ignore fp->fsinfree for now, since we need to
682	 * read the entire FAT anyway to fill the inuse map.
683	 */
684	if (pmp->pm_fsinfo) {
685		struct fsinfo *fp;
686
687		if ((error = bread(devvp, pmp->pm_fsinfo, pmp->pm_BytesPerSec,
688		    NOCRED, &bp)) != 0)
689			goto error_exit;
690		fp = (struct fsinfo *)bp->b_data;
691		if (!bcmp(fp->fsisig1, "RRaA", 4)
692		    && !bcmp(fp->fsisig2, "rrAa", 4)
693		    && !bcmp(fp->fsisig3, "\0\0\125\252", 4)) {
694			pmp->pm_nxtfree = getulong(fp->fsinxtfree);
695			if (pmp->pm_nxtfree > pmp->pm_maxcluster)
696				pmp->pm_nxtfree = CLUST_FIRST;
697		} else
698			pmp->pm_fsinfo = 0;
699		brelse(bp);
700		bp = NULL;
701	}
702
703	/*
704	 * Finish initializing pmp->pm_nxtfree (just in case the first few
705	 * sectors aren't properly reserved in the FAT).  This completes
706	 * the fixup for fp->fsinxtfree, and fixes up the zero-initialized
707	 * value if there is no fsinfo.  We will use pmp->pm_nxtfree
708	 * internally even if there is no fsinfo.
709	 */
710	if (pmp->pm_nxtfree < CLUST_FIRST)
711		pmp->pm_nxtfree = CLUST_FIRST;
712
713	/*
714	 * Allocate memory for the bitmap of allocated clusters, and then
715	 * fill it in.
716	 */
717	pmp->pm_inusemap = malloc(howmany(pmp->pm_maxcluster + 1, N_INUSEBITS)
718				  * sizeof(*pmp->pm_inusemap),
719				  M_MSDOSFSFAT, M_WAITOK);
720
721	/*
722	 * fillinusemap() needs pm_devvp.
723	 */
724	pmp->pm_devvp = devvp;
725	pmp->pm_dev = dev;
726
727	/*
728	 * Have the inuse map filled in.
729	 */
730	MSDOSFS_LOCK_MP(pmp);
731	error = fillinusemap(pmp);
732	MSDOSFS_UNLOCK_MP(pmp);
733	if (error != 0)
734		goto error_exit;
735
736	/*
737	 * If they want fat updates to be synchronous then let them suffer
738	 * the performance degradation in exchange for the on disk copy of
739	 * the fat being correct just about all the time.  I suppose this
740	 * would be a good thing to turn on if the kernel is still flakey.
741	 */
742	if (mp->mnt_flag & MNT_SYNCHRONOUS)
743		pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
744
745	/*
746	 * Finish up.
747	 */
748	if (ronly)
749		pmp->pm_flags |= MSDOSFSMNT_RONLY;
750	else {
751		if ((error = markvoldirty(pmp, 1)) != 0) {
752			(void)markvoldirty(pmp, 0);
753			goto error_exit;
754		}
755		pmp->pm_fmod = 1;
756	}
757	mp->mnt_data =  pmp;
758	mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
759	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
760	MNT_ILOCK(mp);
761	mp->mnt_flag |= MNT_LOCAL;
762	mp->mnt_kern_flag |= MNTK_MPSAFE;
763	MNT_IUNLOCK(mp);
764
765	if (pmp->pm_flags & MSDOSFS_LARGEFS)
766		msdosfs_fileno_init(mp);
767
768	return 0;
769
770error_exit:
771	if (bp)
772		brelse(bp);
773	if (cp != NULL) {
774		DROP_GIANT();
775		g_topology_lock();
776		g_vfs_close(cp);
777		g_topology_unlock();
778		PICKUP_GIANT();
779	}
780	if (pmp) {
781		lockdestroy(&pmp->pm_fatlock);
782		if (pmp->pm_inusemap)
783			free(pmp->pm_inusemap, M_MSDOSFSFAT);
784		free(pmp, M_MSDOSFSMNT);
785		mp->mnt_data = NULL;
786	}
787	dev_rel(dev);
788	return (error);
789}
790
791/*
792 * Unmount the filesystem described by mp.
793 */
794static int
795msdosfs_unmount(struct mount *mp, int mntflags)
796{
797	struct msdosfsmount *pmp;
798	int error, flags;
799
800	flags = 0;
801	if (mntflags & MNT_FORCE)
802		flags |= FORCECLOSE;
803	error = vflush(mp, 0, flags, curthread);
804	if (error && error != ENXIO)
805		return error;
806	pmp = VFSTOMSDOSFS(mp);
807	if ((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0) {
808		error = markvoldirty(pmp, 0);
809		if (error && error != ENXIO) {
810			(void)markvoldirty(pmp, 1);
811			return (error);
812		}
813	}
814	if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
815		if (pmp->pm_w2u)
816			msdosfs_iconv->close(pmp->pm_w2u);
817		if (pmp->pm_u2w)
818			msdosfs_iconv->close(pmp->pm_u2w);
819		if (pmp->pm_d2u)
820			msdosfs_iconv->close(pmp->pm_d2u);
821		if (pmp->pm_u2d)
822			msdosfs_iconv->close(pmp->pm_u2d);
823	}
824
825#ifdef MSDOSFS_DEBUG
826	{
827		struct vnode *vp = pmp->pm_devvp;
828		struct bufobj *bo;
829
830		bo = &vp->v_bufobj;
831		BO_LOCK(bo);
832		VI_LOCK(vp);
833		vn_printf(vp,
834		    "msdosfs_umount(): just before calling VOP_CLOSE()\n");
835		printf("freef %p, freeb %p, mount %p\n",
836		    TAILQ_NEXT(vp, v_actfreelist), vp->v_actfreelist.tqe_prev,
837		    vp->v_mount);
838		printf("cleanblkhd %p, dirtyblkhd %p, numoutput %ld, type %d\n",
839		    TAILQ_FIRST(&vp->v_bufobj.bo_clean.bv_hd),
840		    TAILQ_FIRST(&vp->v_bufobj.bo_dirty.bv_hd),
841		    vp->v_bufobj.bo_numoutput, vp->v_type);
842		VI_UNLOCK(vp);
843		BO_UNLOCK(bo);
844	}
845#endif
846	DROP_GIANT();
847	if (pmp->pm_devvp->v_type == VCHR && pmp->pm_devvp->v_rdev != NULL)
848		pmp->pm_devvp->v_rdev->si_mountpt = NULL;
849	g_topology_lock();
850	g_vfs_close(pmp->pm_cp);
851	g_topology_unlock();
852	PICKUP_GIANT();
853	vrele(pmp->pm_devvp);
854	dev_rel(pmp->pm_dev);
855	free(pmp->pm_inusemap, M_MSDOSFSFAT);
856	if (pmp->pm_flags & MSDOSFS_LARGEFS)
857		msdosfs_fileno_free(mp);
858	lockdestroy(&pmp->pm_fatlock);
859	free(pmp, M_MSDOSFSMNT);
860	mp->mnt_data = NULL;
861	MNT_ILOCK(mp);
862	mp->mnt_flag &= ~MNT_LOCAL;
863	MNT_IUNLOCK(mp);
864	return (error);
865}
866
867static int
868msdosfs_root(struct mount *mp, int flags, struct vnode **vpp)
869{
870	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
871	struct denode *ndep;
872	int error;
873
874#ifdef MSDOSFS_DEBUG
875	printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
876#endif
877	error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep);
878	if (error)
879		return (error);
880	*vpp = DETOV(ndep);
881	return (0);
882}
883
884static int
885msdosfs_statfs(struct mount *mp, struct statfs *sbp)
886{
887	struct msdosfsmount *pmp;
888
889	pmp = VFSTOMSDOSFS(mp);
890	sbp->f_bsize = pmp->pm_bpcluster;
891	sbp->f_iosize = pmp->pm_bpcluster;
892	sbp->f_blocks = pmp->pm_maxcluster + 1;
893	sbp->f_bfree = pmp->pm_freeclustercount;
894	sbp->f_bavail = pmp->pm_freeclustercount;
895	sbp->f_files = pmp->pm_RootDirEnts;	/* XXX */
896	sbp->f_ffree = 0;	/* what to put in here? */
897	return (0);
898}
899
900/*
901 * If we have an FSInfo block, update it.
902 */
903static int
904msdosfs_fsiflush(struct msdosfsmount *pmp, int waitfor)
905{
906	struct fsinfo *fp;
907	struct buf *bp;
908	int error;
909
910	MSDOSFS_LOCK_MP(pmp);
911	if (pmp->pm_fsinfo == 0 || (pmp->pm_flags & MSDOSFS_FSIMOD) == 0) {
912		error = 0;
913		goto unlock;
914	}
915	error = bread(pmp->pm_devvp, pmp->pm_fsinfo, pmp->pm_BytesPerSec,
916	    NOCRED, &bp);
917	if (error != 0) {
918		brelse(bp);
919		goto unlock;
920	}
921	fp = (struct fsinfo *)bp->b_data;
922	putulong(fp->fsinfree, pmp->pm_freeclustercount);
923	putulong(fp->fsinxtfree, pmp->pm_nxtfree);
924	pmp->pm_flags &= ~MSDOSFS_FSIMOD;
925	if (waitfor == MNT_WAIT)
926		error = bwrite(bp);
927	else
928		bawrite(bp);
929unlock:
930	MSDOSFS_UNLOCK_MP(pmp);
931	return (error);
932}
933
934static int
935msdosfs_sync(struct mount *mp, int waitfor)
936{
937	struct vnode *vp, *nvp;
938	struct thread *td;
939	struct denode *dep;
940	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
941	int error, allerror = 0;
942
943	td = curthread;
944
945	/*
946	 * If we ever switch to not updating all of the fats all the time,
947	 * this would be the place to update them from the first one.
948	 */
949	if (pmp->pm_fmod != 0) {
950		if (pmp->pm_flags & MSDOSFSMNT_RONLY)
951			panic("msdosfs_sync: rofs mod");
952		else {
953			/* update fats here */
954		}
955	}
956	/*
957	 * Write back each (modified) denode.
958	 */
959loop:
960	MNT_VNODE_FOREACH_ALL(vp, mp, nvp) {
961		if (vp->v_type == VNON) {
962			VI_UNLOCK(vp);
963			continue;
964		}
965		dep = VTODE(vp);
966		if ((dep->de_flag &
967		    (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 &&
968		    (vp->v_bufobj.bo_dirty.bv_cnt == 0 ||
969		    waitfor == MNT_LAZY)) {
970			VI_UNLOCK(vp);
971			continue;
972		}
973		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, td);
974		if (error) {
975			if (error == ENOENT)
976				goto loop;
977			continue;
978		}
979		error = VOP_FSYNC(vp, waitfor, td);
980		if (error)
981			allerror = error;
982		VOP_UNLOCK(vp, 0);
983		vrele(vp);
984	}
985
986	/*
987	 * Flush filesystem control info.
988	 */
989	if (waitfor != MNT_LAZY) {
990		vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY);
991		error = VOP_FSYNC(pmp->pm_devvp, waitfor, td);
992		if (error)
993			allerror = error;
994		VOP_UNLOCK(pmp->pm_devvp, 0);
995	}
996
997	error = msdosfs_fsiflush(pmp, waitfor);
998	if (error != 0)
999		allerror = error;
1000	return (allerror);
1001}
1002
1003static int
1004msdosfs_fhtovp(struct mount *mp, struct fid *fhp, int flags, struct vnode **vpp)
1005{
1006	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
1007	struct defid *defhp = (struct defid *) fhp;
1008	struct denode *dep;
1009	int error;
1010
1011	error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep);
1012	if (error) {
1013		*vpp = NULLVP;
1014		return (error);
1015	}
1016	*vpp = DETOV(dep);
1017	vnode_create_vobject(*vpp, dep->de_FileSize, curthread);
1018	return (0);
1019}
1020
1021static struct vfsops msdosfs_vfsops = {
1022	.vfs_fhtovp =		msdosfs_fhtovp,
1023	.vfs_mount =		msdosfs_mount,
1024	.vfs_cmount =		msdosfs_cmount,
1025	.vfs_root =		msdosfs_root,
1026	.vfs_statfs =		msdosfs_statfs,
1027	.vfs_sync =		msdosfs_sync,
1028	.vfs_unmount =		msdosfs_unmount,
1029};
1030
1031VFS_SET(msdosfs_vfsops, msdosfs, 0);
1032MODULE_VERSION(msdosfs, 1);
1033