xref: /freebsd/sys/fs/msdosfs/msdosfsmount.h (revision fe6060f10f634930ff71b7c50291ddc610da2475)
1 /* $FreeBSD$ */
2 /*	$NetBSD: msdosfsmount.h,v 1.17 1997/11/17 15:37:07 ws Exp $	*/
3 
4 /*-
5  * SPDX-License-Identifier: BSD-4-Clause
6  *
7  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
8  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
9  * All rights reserved.
10  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by TooLs GmbH.
23  * 4. The name of TooLs GmbH may not be used to endorse or promote products
24  *    derived from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
27  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
32  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37 /*-
38  * Written by Paul Popelka (paulp@uts.amdahl.com)
39  *
40  * You can do anything you want with this software, just don't say you wrote
41  * it, and don't remove this notice.
42  *
43  * This software is provided "as is".
44  *
45  * The author supplies this software to be publicly redistributed on the
46  * understanding that the author is not responsible for the correct
47  * functioning of this software in any circumstances and is not liable for
48  * any damages caused by this software.
49  *
50  * October 1992
51  */
52 
53 #ifndef _MSDOSFS_MSDOSFSMOUNT_H_
54 #define	_MSDOSFS_MSDOSFSMOUNT_H_
55 
56 #if defined (_KERNEL) || defined(MAKEFS)
57 
58 #include <sys/types.h>
59 #ifndef MAKEFS
60 #include <sys/lock.h>
61 #include <sys/lockmgr.h>
62 #endif
63 #include <sys/tree.h>
64 
65 #ifdef MALLOC_DECLARE
66 MALLOC_DECLARE(M_MSDOSFSMNT);
67 #endif
68 
69 struct msdosfs_fileno;
70 
71 /*
72  * Layout of the mount control block for a MSDOSFS filesystem.
73  */
74 struct msdosfsmount {
75 	struct mount *pm_mountp;/* vfs mount struct for this fs */
76 	struct g_consumer *pm_cp;
77 	struct bufobj *pm_bo;
78 	uid_t pm_uid;		/* uid to set as owner of the files */
79 	gid_t pm_gid;		/* gid to set as owner of the files */
80 	mode_t pm_mask;		/* mask to and with file protection bits
81 				   for files */
82 	mode_t pm_dirmask;	/* mask to and with file protection bits
83 				   for directories */
84 	struct vnode *pm_devvp;	/* vnode for character device mounted */
85 	struct cdev *pm_dev;	/* character device mounted */
86 	struct bpb50 pm_bpb;	/* BIOS parameter blk for this fs */
87 	u_long pm_BlkPerSec;	/* How many DEV_BSIZE blocks fit inside a physical sector */
88 	u_long pm_FATsecs;	/* actual number of FAT sectors */
89 	u_long pm_fatblk;	/* block # of first FAT */
90 	u_long pm_rootdirblk;	/* block # (cluster # for FAT32) of root directory number */
91 	u_long pm_rootdirsize;	/* size in blocks (not clusters) */
92 	u_long pm_firstcluster;	/* block number of first cluster */
93 	u_long pm_maxcluster;	/* maximum cluster number */
94 	u_long pm_freeclustercount;	/* number of free clusters */
95 	u_long pm_cnshift;	/* shift file offset right this amount to get a cluster number */
96 	u_long pm_crbomask;	/* and a file offset with this mask to get cluster rel offset */
97 	u_long pm_bnshift;	/* shift file offset right this amount to get a block number */
98 	u_long pm_bpcluster;	/* bytes per cluster */
99 	u_long pm_fmod;		/* ~0 if fs is modified, this can rollover to 0	*/
100 	u_long pm_fatblocksize;	/* size of FAT blocks in bytes */
101 	u_long pm_fatblocksec;	/* size of FAT blocks in sectors */
102 	u_long pm_fatsize;	/* size of FAT in bytes */
103 	uint32_t pm_fatmask;	/* mask to use for FAT numbers */
104 	u_long pm_fsinfo;	/* fsinfo block number */
105 	u_long pm_nxtfree;	/* next place to search for a free cluster */
106 	u_int pm_fatmult;	/* these 2 values are used in FAT */
107 	u_int pm_fatdiv;	/*	offset computation */
108 	u_int pm_curfat;	/* current FAT for FAT32 (0 otherwise) */
109 	u_int *pm_inusemap;	/* ptr to bitmap of in-use clusters */
110 	uint64_t pm_flags;	/* see below */
111 	void *pm_u2w;	/* Local->Unicode iconv handle */
112 	void *pm_w2u;	/* Unicode->Local iconv handle */
113 	void *pm_u2d;	/* Unicode->DOS iconv handle */
114 	void *pm_d2u;	/* DOS->Local iconv handle */
115 #ifndef MAKEFS
116 	struct lock pm_fatlock;	/* lockmgr protecting allocations */
117 	struct lock pm_checkpath_lock; /* protects doscheckpath result */
118 #endif
119 };
120 
121 /*
122  * A 64-bit file number and the 32-bit file number to which it is mapped,
123  * in a red-black tree node.
124  */
125 struct msdosfs_fileno {
126 	RB_ENTRY(msdosfs_fileno)	mf_tree;
127 	uint32_t			mf_fileno32;
128 	uint64_t			mf_fileno64;
129 };
130 
131 /* Byte offset in FAT on filesystem pmp, cluster cn */
132 #define	FATOFS(pmp, cn)	((cn) * (pmp)->pm_fatmult / (pmp)->pm_fatdiv)
133 
134 #define	VFSTOMSDOSFS(mp)	((struct msdosfsmount *)mp->mnt_data)
135 
136 /* Number of bits in one pm_inusemap item: */
137 #define	N_INUSEBITS	(8 * sizeof(u_int))
138 
139 /*
140  * Shorthand for fields in the bpb contained in the msdosfsmount structure.
141  */
142 #define	pm_BytesPerSec	pm_bpb.bpbBytesPerSec
143 #define	pm_ResSectors	pm_bpb.bpbResSectors
144 #define	pm_FATs		pm_bpb.bpbFATs
145 #define	pm_RootDirEnts	pm_bpb.bpbRootDirEnts
146 #define	pm_Sectors	pm_bpb.bpbSectors
147 #define	pm_Media	pm_bpb.bpbMedia
148 #define	pm_SecPerTrack	pm_bpb.bpbSecPerTrack
149 #define	pm_Heads	pm_bpb.bpbHeads
150 #define	pm_HiddenSects	pm_bpb.bpbHiddenSecs
151 #define	pm_HugeSectors	pm_bpb.bpbHugeSectors
152 
153 /*
154  * Convert pointer to buffer -> pointer to direntry
155  */
156 #define	bptoep(pmp, bp, dirofs) \
157 	((struct direntry *)(((bp)->b_data)	\
158 	 + ((dirofs) & (pmp)->pm_crbomask)))
159 
160 /*
161  * Convert block number to cluster number
162  */
163 #define	de_bn2cn(pmp, bn) \
164 	((bn) >> ((pmp)->pm_cnshift - (pmp)->pm_bnshift))
165 
166 /*
167  * Convert cluster number to block number
168  */
169 #define	de_cn2bn(pmp, cn) \
170 	((cn) << ((pmp)->pm_cnshift - (pmp)->pm_bnshift))
171 
172 /*
173  * Convert file offset to cluster number
174  */
175 #define de_cluster(pmp, off) \
176 	((off) >> (pmp)->pm_cnshift)
177 
178 /*
179  * Clusters required to hold size bytes
180  */
181 #define	de_clcount(pmp, size) \
182 	(((size) + (pmp)->pm_bpcluster - 1) >> (pmp)->pm_cnshift)
183 
184 /*
185  * Convert file offset to block number
186  */
187 #define de_blk(pmp, off) \
188 	(de_cn2bn(pmp, de_cluster((pmp), (off))))
189 
190 /*
191  * Convert cluster number to file offset
192  */
193 #define	de_cn2off(pmp, cn) \
194 	((cn) << (pmp)->pm_cnshift)
195 
196 /*
197  * Convert block number to file offset
198  */
199 #define	de_bn2off(pmp, bn) \
200 	((bn) << (pmp)->pm_bnshift)
201 /*
202  * Map a cluster number into a filesystem relative block number.
203  */
204 #define	cntobn(pmp, cn) \
205 	(de_cn2bn((pmp), (cn)-CLUST_FIRST) + (pmp)->pm_firstcluster)
206 
207 /*
208  * Calculate block number for directory entry in root dir, offset dirofs
209  */
210 #define	roottobn(pmp, dirofs) \
211 	(de_blk((pmp), (dirofs)) + (pmp)->pm_rootdirblk)
212 
213 /*
214  * Calculate block number for directory entry at cluster dirclu, offset
215  * dirofs
216  */
217 #define	detobn(pmp, dirclu, dirofs) \
218 	((dirclu) == MSDOSFSROOT \
219 	 ? roottobn((pmp), (dirofs)) \
220 	 : cntobn((pmp), (dirclu)))
221 
222 #define	MSDOSFS_LOCK_MP(pmp) \
223 	lockmgr(&(pmp)->pm_fatlock, LK_EXCLUSIVE, NULL)
224 #define	MSDOSFS_UNLOCK_MP(pmp) \
225 	lockmgr(&(pmp)->pm_fatlock, LK_RELEASE, NULL)
226 #define	MSDOSFS_ASSERT_MP_LOCKED(pmp) \
227 	lockmgr_assert(&(pmp)->pm_fatlock, KA_XLOCKED)
228 
229 #endif /* _KERNEL || MAKEFS */
230 
231 #ifndef MAKEFS
232 /*
233  *  Arguments to mount MSDOS filesystems.
234  */
235 struct msdosfs_args {
236 	char	*fspec;		/* blocks special holding the fs to mount */
237 	struct	oexport_args export;	/* network export information */
238 	uid_t	uid;		/* uid that owns msdosfs files */
239 	gid_t	gid;		/* gid that owns msdosfs files */
240 	mode_t	mask;		/* file mask to be applied for msdosfs perms */
241 	int	flags;		/* see below */
242 	int	unused1;	/* unused, was version number */
243 	uint16_t unused2[128];	/* no longer used, was Local->Unicode table */
244 	char	*cs_win;	/* Windows(Unicode) Charset */
245 	char	*cs_dos;	/* DOS Charset */
246 	char	*cs_local;	/* Local Charset */
247 	mode_t	dirmask;	/* dir  mask to be applied for msdosfs perms */
248 };
249 #endif /* MAKEFS */
250 
251 /*
252  * Msdosfs mount options:
253  */
254 #define	MSDOSFSMNT_SHORTNAME	1	/* Force old DOS short names only */
255 #define	MSDOSFSMNT_LONGNAME	2	/* Force Win'95 long names */
256 #define	MSDOSFSMNT_NOWIN95	4	/* Completely ignore Win95 entries */
257 #define	MSDOSFSMNT_KICONV	0x10    /* Use libiconv to convert chars */
258 /* All flags above: */
259 #define	MSDOSFSMNT_MNTOPT \
260 	(MSDOSFSMNT_SHORTNAME|MSDOSFSMNT_LONGNAME|MSDOSFSMNT_NOWIN95 \
261 	 |MSDOSFSMNT_KICONV)
262 #define	MSDOSFSMNT_RONLY	0x80000000	/* mounted read-only	*/
263 #define	MSDOSFSMNT_WAITONFAT	0x40000000	/* mounted synchronous	*/
264 #define	MSDOSFS_FATMIRROR	0x20000000	/* FAT is mirrored */
265 #define	MSDOSFS_FSIMOD		0x01000000
266 
267 #endif /* !_MSDOSFS_MSDOSFSMOUNT_H_ */
268