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