xref: /freebsd/sys/fs/msdosfs/msdosfsmount.h (revision 71625ec9ad2a9bc8c09784fbd23b759830e0ee5f)
1952a6212SJordan K. Hubbard /*	$NetBSD: msdosfsmount.h,v 1.17 1997/11/17 15:37:07 ws Exp $	*/
227a0bc89SDoug Rabson 
327a0bc89SDoug Rabson /*-
4d63027b6SPedro F. Giffuni  * SPDX-License-Identifier: BSD-4-Clause
5d63027b6SPedro F. Giffuni  *
6952a6212SJordan K. Hubbard  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
7952a6212SJordan K. Hubbard  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
827a0bc89SDoug Rabson  * All rights reserved.
927a0bc89SDoug Rabson  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
1027a0bc89SDoug Rabson  *
1127a0bc89SDoug Rabson  * Redistribution and use in source and binary forms, with or without
1227a0bc89SDoug Rabson  * modification, are permitted provided that the following conditions
1327a0bc89SDoug Rabson  * are met:
1427a0bc89SDoug Rabson  * 1. Redistributions of source code must retain the above copyright
1527a0bc89SDoug Rabson  *    notice, this list of conditions and the following disclaimer.
1627a0bc89SDoug Rabson  * 2. Redistributions in binary form must reproduce the above copyright
1727a0bc89SDoug Rabson  *    notice, this list of conditions and the following disclaimer in the
1827a0bc89SDoug Rabson  *    documentation and/or other materials provided with the distribution.
1927a0bc89SDoug Rabson  * 3. All advertising materials mentioning features or use of this software
2027a0bc89SDoug Rabson  *    must display the following acknowledgement:
2127a0bc89SDoug Rabson  *	This product includes software developed by TooLs GmbH.
2227a0bc89SDoug Rabson  * 4. The name of TooLs GmbH may not be used to endorse or promote products
2327a0bc89SDoug Rabson  *    derived from this software without specific prior written permission.
2427a0bc89SDoug Rabson  *
2527a0bc89SDoug Rabson  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
2627a0bc89SDoug Rabson  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2727a0bc89SDoug Rabson  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2827a0bc89SDoug Rabson  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2927a0bc89SDoug Rabson  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
3027a0bc89SDoug Rabson  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
3127a0bc89SDoug Rabson  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
3227a0bc89SDoug Rabson  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
3327a0bc89SDoug Rabson  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
3427a0bc89SDoug Rabson  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3527a0bc89SDoug Rabson  */
36d167cf6fSWarner Losh /*-
3727a0bc89SDoug Rabson  * Written by Paul Popelka (paulp@uts.amdahl.com)
3827a0bc89SDoug Rabson  *
3927a0bc89SDoug Rabson  * You can do anything you want with this software, just don't say you wrote
4027a0bc89SDoug Rabson  * it, and don't remove this notice.
4127a0bc89SDoug Rabson  *
4227a0bc89SDoug Rabson  * This software is provided "as is".
4327a0bc89SDoug Rabson  *
4427a0bc89SDoug Rabson  * The author supplies this software to be publicly redistributed on the
4527a0bc89SDoug Rabson  * understanding that the author is not responsible for the correct
4627a0bc89SDoug Rabson  * functioning of this software in any circumstances and is not liable for
4727a0bc89SDoug Rabson  * any damages caused by this software.
4827a0bc89SDoug Rabson  *
4927a0bc89SDoug Rabson  * October 1992
5027a0bc89SDoug Rabson  */
5127a0bc89SDoug Rabson 
5260e0f7e2SBruce Evans #ifndef _MSDOSFS_MSDOSFSMOUNT_H_
5360e0f7e2SBruce Evans #define	_MSDOSFS_MSDOSFSMOUNT_H_
5460e0f7e2SBruce Evans 
55840aca28SEd Maste #if defined (_KERNEL) || defined(MAKEFS)
5660e0f7e2SBruce Evans 
5723b6c230SKonstantin Belousov #include <sys/types.h>
58162ae9c8SAlex Richardson #ifndef MAKEFS
5923b6c230SKonstantin Belousov #include <sys/lock.h>
6023b6c230SKonstantin Belousov #include <sys/lockmgr.h>
61b2e4b635SKonstantin Belousov #include <sys/_task.h>
62162ae9c8SAlex Richardson #endif
633bc482ecSTim J. Robbins #include <sys/tree.h>
643bc482ecSTim J. Robbins 
65a1c995b6SPoul-Henning Kamp #ifdef MALLOC_DECLARE
66a1c995b6SPoul-Henning Kamp MALLOC_DECLARE(M_MSDOSFSMNT);
67a1c995b6SPoul-Henning Kamp #endif
68a1c995b6SPoul-Henning Kamp 
693bc482ecSTim J. Robbins struct msdosfs_fileno;
703bc482ecSTim J. Robbins 
7127a0bc89SDoug Rabson /*
729287dbaaSEd Maste  * Layout of the mount control block for a MSDOSFS filesystem.
7327a0bc89SDoug Rabson  */
7427a0bc89SDoug Rabson struct msdosfsmount {
7527a0bc89SDoug Rabson 	struct mount *pm_mountp;/* vfs mount struct for this fs */
769a135592SPoul-Henning Kamp 	struct g_consumer *pm_cp;
779a135592SPoul-Henning Kamp 	struct bufobj *pm_bo;
7827a0bc89SDoug Rabson 	uid_t pm_uid;		/* uid to set as owner of the files */
7927a0bc89SDoug Rabson 	gid_t pm_gid;		/* gid to set as owner of the files */
80c98a31caSTom Rhodes 	mode_t pm_mask;		/* mask to and with file protection bits
81c98a31caSTom Rhodes 				   for files */
82c98a31caSTom Rhodes 	mode_t pm_dirmask;	/* mask to and with file protection bits
83c98a31caSTom Rhodes 				   for directories */
84c72ae142SJohn Baldwin 	struct vnode *pm_devvp;	/* vnode for character device mounted */
85aaaa4fb5SKonstantin Belousov 	struct vnode *pm_odevvp;/* real devfs vnode */
86c72ae142SJohn Baldwin 	struct cdev *pm_dev;	/* character device mounted */
8727a0bc89SDoug Rabson 	struct bpb50 pm_bpb;	/* BIOS parameter blk for this fs */
8801f6cfbaSYoshihiro Takahashi 	u_long pm_BlkPerSec;	/* How many DEV_BSIZE blocks fit inside a physical sector */
899287dbaaSEd Maste 	u_long pm_FATsecs;	/* actual number of FAT sectors */
9027a0bc89SDoug Rabson 	u_long pm_fatblk;	/* block # of first FAT */
91952a6212SJordan K. Hubbard 	u_long pm_rootdirblk;	/* block # (cluster # for FAT32) of root directory number */
9227a0bc89SDoug Rabson 	u_long pm_rootdirsize;	/* size in blocks (not clusters) */
9327a0bc89SDoug Rabson 	u_long pm_firstcluster;	/* block number of first cluster */
9427a0bc89SDoug Rabson 	u_long pm_maxcluster;	/* maximum cluster number */
9527a0bc89SDoug Rabson 	u_long pm_freeclustercount;	/* number of free clusters */
9627a0bc89SDoug Rabson 	u_long pm_cnshift;	/* shift file offset right this amount to get a cluster number */
9727a0bc89SDoug Rabson 	u_long pm_crbomask;	/* and a file offset with this mask to get cluster rel offset */
98952a6212SJordan K. Hubbard 	u_long pm_bnshift;	/* shift file offset right this amount to get a block number */
9927a0bc89SDoug Rabson 	u_long pm_bpcluster;	/* bytes per cluster */
10027a0bc89SDoug Rabson 	u_long pm_fmod;		/* ~0 if fs is modified, this can rollover to 0	*/
1019287dbaaSEd Maste 	u_long pm_fatblocksize;	/* size of FAT blocks in bytes */
1029287dbaaSEd Maste 	u_long pm_fatblocksec;	/* size of FAT blocks in sectors */
1039287dbaaSEd Maste 	u_long pm_fatsize;	/* size of FAT in bytes */
1049287dbaaSEd Maste 	uint32_t pm_fatmask;	/* mask to use for FAT numbers */
105952a6212SJordan K. Hubbard 	u_long pm_fsinfo;	/* fsinfo block number */
1068e55bfafSBruce Evans 	u_long pm_nxtfree;	/* next place to search for a free cluster */
1079287dbaaSEd Maste 	u_int pm_fatmult;	/* these 2 values are used in FAT */
108952a6212SJordan K. Hubbard 	u_int pm_fatdiv;	/*	offset computation */
1099287dbaaSEd Maste 	u_int pm_curfat;	/* current FAT for FAT32 (0 otherwise) */
110*c33db74bSStefan Eßer 	int pm_rootdirfree;	/* number of free slots in FAT12/16 root directory */
11127a0bc89SDoug Rabson 	u_int *pm_inusemap;	/* ptr to bitmap of in-use clusters */
1126beb3bb4SKirk McKusick 	uint64_t pm_flags;	/* see below */
113c4f02a89SMax Khon 	void *pm_u2w;	/* Local->Unicode iconv handle */
114c4f02a89SMax Khon 	void *pm_w2u;	/* Unicode->Local iconv handle */
115c4f02a89SMax Khon 	void *pm_u2d;	/* Unicode->DOS iconv handle */
116c4f02a89SMax Khon 	void *pm_d2u;	/* DOS->Local iconv handle */
117162ae9c8SAlex Richardson #ifndef MAKEFS
11840373cf5SKonstantin Belousov 	struct lock pm_fatlock;	/* lockmgr protecting allocations */
1196ae13c0fSKonstantin Belousov 	struct lock pm_checkpath_lock; /* protects doscheckpath result */
120b2e4b635SKonstantin Belousov 	struct task pm_rw2ro_task; /* context for emergency remount ro */
121162ae9c8SAlex Richardson #endif
12227a0bc89SDoug Rabson };
1233bc482ecSTim J. Robbins 
1243bc482ecSTim J. Robbins /*
1253bc482ecSTim J. Robbins  * A 64-bit file number and the 32-bit file number to which it is mapped,
1263bc482ecSTim J. Robbins  * in a red-black tree node.
1273bc482ecSTim J. Robbins  */
1283bc482ecSTim J. Robbins struct msdosfs_fileno {
1293bc482ecSTim J. Robbins 	RB_ENTRY(msdosfs_fileno)	mf_tree;
1303bc482ecSTim J. Robbins 	uint32_t			mf_fileno32;
1313bc482ecSTim J. Robbins 	uint64_t			mf_fileno64;
1323bc482ecSTim J. Robbins };
1333bc482ecSTim J. Robbins 
134952a6212SJordan K. Hubbard /* Byte offset in FAT on filesystem pmp, cluster cn */
135952a6212SJordan K. Hubbard #define	FATOFS(pmp, cn)	((cn) * (pmp)->pm_fatmult / (pmp)->pm_fatdiv)
136952a6212SJordan K. Hubbard 
137952a6212SJordan K. Hubbard #define	VFSTOMSDOSFS(mp)	((struct msdosfsmount *)mp->mnt_data)
13827a0bc89SDoug Rabson 
13927a0bc89SDoug Rabson /* Number of bits in one pm_inusemap item: */
14027a0bc89SDoug Rabson #define	N_INUSEBITS	(8 * sizeof(u_int))
14127a0bc89SDoug Rabson 
14227a0bc89SDoug Rabson /*
14327a0bc89SDoug Rabson  * Shorthand for fields in the bpb contained in the msdosfsmount structure.
14427a0bc89SDoug Rabson  */
14527a0bc89SDoug Rabson #define	pm_BytesPerSec	pm_bpb.bpbBytesPerSec
14627a0bc89SDoug Rabson #define	pm_ResSectors	pm_bpb.bpbResSectors
14727a0bc89SDoug Rabson #define	pm_FATs		pm_bpb.bpbFATs
14827a0bc89SDoug Rabson #define	pm_RootDirEnts	pm_bpb.bpbRootDirEnts
14927a0bc89SDoug Rabson #define	pm_Sectors	pm_bpb.bpbSectors
15027a0bc89SDoug Rabson #define	pm_Media	pm_bpb.bpbMedia
15127a0bc89SDoug Rabson #define	pm_SecPerTrack	pm_bpb.bpbSecPerTrack
15227a0bc89SDoug Rabson #define	pm_Heads	pm_bpb.bpbHeads
15327a0bc89SDoug Rabson #define	pm_HiddenSects	pm_bpb.bpbHiddenSecs
15427a0bc89SDoug Rabson #define	pm_HugeSectors	pm_bpb.bpbHugeSectors
15527a0bc89SDoug Rabson 
15627a0bc89SDoug Rabson /*
157952a6212SJordan K. Hubbard  * Convert pointer to buffer -> pointer to direntry
158952a6212SJordan K. Hubbard  */
159952a6212SJordan K. Hubbard #define	bptoep(pmp, bp, dirofs) \
160952a6212SJordan K. Hubbard 	((struct direntry *)(((bp)->b_data)	\
161952a6212SJordan K. Hubbard 	 + ((dirofs) & (pmp)->pm_crbomask)))
162952a6212SJordan K. Hubbard 
163952a6212SJordan K. Hubbard /*
164952a6212SJordan K. Hubbard  * Convert block number to cluster number
165952a6212SJordan K. Hubbard  */
166952a6212SJordan K. Hubbard #define	de_bn2cn(pmp, bn) \
167952a6212SJordan K. Hubbard 	((bn) >> ((pmp)->pm_cnshift - (pmp)->pm_bnshift))
168952a6212SJordan K. Hubbard 
169952a6212SJordan K. Hubbard /*
170952a6212SJordan K. Hubbard  * Convert cluster number to block number
171952a6212SJordan K. Hubbard  */
172952a6212SJordan K. Hubbard #define	de_cn2bn(pmp, cn) \
173952a6212SJordan K. Hubbard 	((cn) << ((pmp)->pm_cnshift - (pmp)->pm_bnshift))
174952a6212SJordan K. Hubbard 
175952a6212SJordan K. Hubbard /*
176952a6212SJordan K. Hubbard  * Convert file offset to cluster number
177952a6212SJordan K. Hubbard  */
178952a6212SJordan K. Hubbard #define de_cluster(pmp, off) \
179952a6212SJordan K. Hubbard 	((off) >> (pmp)->pm_cnshift)
180952a6212SJordan K. Hubbard 
181952a6212SJordan K. Hubbard /*
182952a6212SJordan K. Hubbard  * Clusters required to hold size bytes
183952a6212SJordan K. Hubbard  */
184952a6212SJordan K. Hubbard #define	de_clcount(pmp, size) \
185952a6212SJordan K. Hubbard 	(((size) + (pmp)->pm_bpcluster - 1) >> (pmp)->pm_cnshift)
186952a6212SJordan K. Hubbard 
187952a6212SJordan K. Hubbard /*
188952a6212SJordan K. Hubbard  * Convert file offset to block number
189952a6212SJordan K. Hubbard  */
190952a6212SJordan K. Hubbard #define de_blk(pmp, off) \
191952a6212SJordan K. Hubbard 	(de_cn2bn(pmp, de_cluster((pmp), (off))))
192952a6212SJordan K. Hubbard 
193952a6212SJordan K. Hubbard /*
194952a6212SJordan K. Hubbard  * Convert cluster number to file offset
195952a6212SJordan K. Hubbard  */
196952a6212SJordan K. Hubbard #define	de_cn2off(pmp, cn) \
197952a6212SJordan K. Hubbard 	((cn) << (pmp)->pm_cnshift)
198952a6212SJordan K. Hubbard 
199952a6212SJordan K. Hubbard /*
200952a6212SJordan K. Hubbard  * Convert block number to file offset
201952a6212SJordan K. Hubbard  */
202952a6212SJordan K. Hubbard #define	de_bn2off(pmp, bn) \
203952a6212SJordan K. Hubbard 	((bn) << (pmp)->pm_bnshift)
204952a6212SJordan K. Hubbard /*
20527a0bc89SDoug Rabson  * Map a cluster number into a filesystem relative block number.
20627a0bc89SDoug Rabson  */
20727a0bc89SDoug Rabson #define	cntobn(pmp, cn) \
208952a6212SJordan K. Hubbard 	(de_cn2bn((pmp), (cn)-CLUST_FIRST) + (pmp)->pm_firstcluster)
20927a0bc89SDoug Rabson 
21027a0bc89SDoug Rabson /*
21127a0bc89SDoug Rabson  * Calculate block number for directory entry in root dir, offset dirofs
21227a0bc89SDoug Rabson  */
21327a0bc89SDoug Rabson #define	roottobn(pmp, dirofs) \
214952a6212SJordan K. Hubbard 	(de_blk((pmp), (dirofs)) + (pmp)->pm_rootdirblk)
21527a0bc89SDoug Rabson 
21627a0bc89SDoug Rabson /*
21727a0bc89SDoug Rabson  * Calculate block number for directory entry at cluster dirclu, offset
21827a0bc89SDoug Rabson  * dirofs
21927a0bc89SDoug Rabson  */
22027a0bc89SDoug Rabson #define	detobn(pmp, dirclu, dirofs) \
22127a0bc89SDoug Rabson 	((dirclu) == MSDOSFSROOT \
22227a0bc89SDoug Rabson 	 ? roottobn((pmp), (dirofs)) \
22327a0bc89SDoug Rabson 	 : cntobn((pmp), (dirclu)))
22427a0bc89SDoug Rabson 
225*c33db74bSStefan Eßer /*
226*c33db74bSStefan Eßer  * Increment the number of used entries in a fixed size FAT12/16 root
227*c33db74bSStefan Eßer  * directory
228*c33db74bSStefan Eßer  */
229*c33db74bSStefan Eßer #define rootde_alloced(dep)			   \
230*c33db74bSStefan Eßer 	if ((dep)->de_StartCluster == MSDOSFSROOT) \
231*c33db74bSStefan Eßer 		(dep)->de_pmp->pm_rootdirfree--;
232*c33db74bSStefan Eßer 
233*c33db74bSStefan Eßer /*
234*c33db74bSStefan Eßer  * Decrement the number of used entries in a fixed size FAT12/16 root
235*c33db74bSStefan Eßer  * directory
236*c33db74bSStefan Eßer  */
237*c33db74bSStefan Eßer #define rootde_freed(dep)			   \
238*c33db74bSStefan Eßer 	if ((dep)->de_StartCluster == MSDOSFSROOT) \
239*c33db74bSStefan Eßer 		(dep)->de_pmp->pm_rootdirfree++;
240*c33db74bSStefan Eßer 
24123b6c230SKonstantin Belousov #define	MSDOSFS_LOCK_MP(pmp) \
24223b6c230SKonstantin Belousov 	lockmgr(&(pmp)->pm_fatlock, LK_EXCLUSIVE, NULL)
24323b6c230SKonstantin Belousov #define	MSDOSFS_UNLOCK_MP(pmp) \
24423b6c230SKonstantin Belousov 	lockmgr(&(pmp)->pm_fatlock, LK_RELEASE, NULL)
24523b6c230SKonstantin Belousov #define	MSDOSFS_ASSERT_MP_LOCKED(pmp) \
24623b6c230SKonstantin Belousov 	lockmgr_assert(&(pmp)->pm_fatlock, KA_XLOCKED)
24723b6c230SKonstantin Belousov 
2484f0372f8SEd Maste #endif /* _KERNEL || MAKEFS */
24960e0f7e2SBruce Evans 
250840aca28SEd Maste #ifndef MAKEFS
251996c772fSJohn Dyson /*
252996c772fSJohn Dyson  *  Arguments to mount MSDOS filesystems.
253996c772fSJohn Dyson  */
254996c772fSJohn Dyson struct msdosfs_args {
255996c772fSJohn Dyson 	char	*fspec;		/* blocks special holding the fs to mount */
256d0cc54f3SKonstantin Belousov 	struct	oexport_args export;	/* network export information */
257996c772fSJohn Dyson 	uid_t	uid;		/* uid that owns msdosfs files */
258996c772fSJohn Dyson 	gid_t	gid;		/* gid that owns msdosfs files */
259c98a31caSTom Rhodes 	mode_t	mask;		/* file mask to be applied for msdosfs perms */
260952a6212SJordan K. Hubbard 	int	flags;		/* see below */
261c66d17c7SKonstantin Belousov 	int	unused1;	/* unused, was version number */
26223c53312SEd Maste 	uint16_t unused2[128];	/* no longer used, was Local->Unicode table */
263c4f02a89SMax Khon 	char	*cs_win;	/* Windows(Unicode) Charset */
264c4f02a89SMax Khon 	char	*cs_dos;	/* DOS Charset */
265c4f02a89SMax Khon 	char	*cs_local;	/* Local Charset */
26601ba334cSTom Rhodes 	mode_t	dirmask;	/* dir  mask to be applied for msdosfs perms */
267996c772fSJohn Dyson };
268840aca28SEd Maste #endif /* MAKEFS */
269996c772fSJohn Dyson 
270952a6212SJordan K. Hubbard /*
271952a6212SJordan K. Hubbard  * Msdosfs mount options:
272952a6212SJordan K. Hubbard  */
273952a6212SJordan K. Hubbard #define	MSDOSFSMNT_SHORTNAME	1	/* Force old DOS short names only */
274952a6212SJordan K. Hubbard #define	MSDOSFSMNT_LONGNAME	2	/* Force Win'95 long names */
275952a6212SJordan K. Hubbard #define	MSDOSFSMNT_NOWIN95	4	/* Completely ignore Win95 entries */
276c4f02a89SMax Khon #define	MSDOSFSMNT_KICONV	0x10    /* Use libiconv to convert chars */
277952a6212SJordan K. Hubbard /* All flags above: */
278952a6212SJordan K. Hubbard #define	MSDOSFSMNT_MNTOPT \
279952a6212SJordan K. Hubbard 	(MSDOSFSMNT_SHORTNAME|MSDOSFSMNT_LONGNAME|MSDOSFSMNT_NOWIN95 \
280c4f02a89SMax Khon 	 |MSDOSFSMNT_KICONV)
281952a6212SJordan K. Hubbard #define	MSDOSFSMNT_RONLY	0x80000000	/* mounted read-only	*/
282952a6212SJordan K. Hubbard #define	MSDOSFSMNT_WAITONFAT	0x40000000	/* mounted synchronous	*/
283952a6212SJordan K. Hubbard #define	MSDOSFS_FATMIRROR	0x20000000	/* FAT is mirrored */
284bb7ca822SKonstantin Belousov #define	MSDOSFS_FSIMOD		0x01000000
285b2e4b635SKonstantin Belousov #define	MSDOSFS_ERR_RO		0x00800000	/* remouning ro due to error */
286b2e4b635SKonstantin Belousov 
287b2e4b635SKonstantin Belousov #ifdef _KERNEL
288b2e4b635SKonstantin Belousov void msdosfs_integrity_error(struct msdosfsmount *pmp);
289b2e4b635SKonstantin Belousov #endif
290952a6212SJordan K. Hubbard 
29160e0f7e2SBruce Evans #endif /* !_MSDOSFS_MSDOSFSMOUNT_H_ */
292