xref: /haiku/src/add-ons/kernel/file_systems/fat/bsd/fs/msdosfs/msdosfsmount.h (revision 342a1b221b5bb385410f758df2c625b70cafdd03)
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(MAKEFS)
56 
57 #include <sys/types.h>
58 #ifndef MAKEFS
59 //#include <sys/lock.h>
60 	// Haiku port:  not needed
61 #include <sys/lockmgr.h>
62 #include <sys/_task.h>
63 #endif
64 //#include <sys/tree.h>
65 	// Haiku port:  not needed
66 
67 #ifdef MALLOC_DECLARE
68 MALLOC_DECLARE(M_MSDOSFSMNT);
69 #endif
70 
71 struct msdosfs_fileno;
72 
73 /*
74  * Layout of the mount control block for a MSDOSFS filesystem.
75  */
76 struct msdosfsmount {
77 	struct mount *pm_mountp;/* vfs mount struct for this fs */
78 	struct g_consumer *pm_cp;
79 	struct bufobj *pm_bo;
80 	uid_t pm_uid;		/* uid to set as owner of the files */
81 	gid_t pm_gid;		/* gid to set as owner of the files */
82 	mode_t pm_mask;		/* mask to and with file protection bits
83 				   for files */
84 	mode_t pm_dirmask;	/* mask to and with file protection bits
85 				   for directories */
86 	struct vnode *pm_devvp;	/* vnode for character device mounted */
87 	struct vnode *pm_odevvp;/* real devfs vnode */
88 	struct cdev *pm_dev;	/* character device mounted */
89 	struct bpb50 pm_bpb;	/* BIOS parameter blk for this fs */
90 	u_long pm_BlkPerSec;	/* How many DEV_BSIZE blocks fit inside a physical sector */
91 	u_long pm_FATsecs;	/* actual number of FAT sectors */
92 	u_long pm_fatblk;	/* block # of first FAT */
93 	u_long pm_rootdirblk;	/* block # (cluster # for FAT32) of root directory number */
94 	u_long pm_rootdirsize;	/* size in blocks (not clusters) */
95 	u_long pm_firstcluster;	/* block number of first cluster */
96 	u_long pm_maxcluster;	/* maximum cluster number */
97 	u_long pm_freeclustercount;	/* number of free clusters */
98 	u_long pm_cnshift;	/* shift file offset right this amount to get a cluster number */
99 	u_long pm_crbomask;	/* and a file offset with this mask to get cluster rel offset */
100 	u_long pm_bnshift;	/* shift file offset right this amount to get a block number */
101 	u_long pm_bpcluster;	/* bytes per cluster */
102 	u_long pm_fmod;		/* ~0 if fs is modified, this can rollover to 0	*/
103 	u_long pm_fatblocksize;	/* size of FAT blocks in bytes */
104 	u_long pm_fatblocksec;	/* size of FAT blocks in sectors */
105 	u_long pm_fatsize;	/* size of FAT in bytes */
106 	uint32_t pm_fatmask;	/* mask to use for FAT numbers */
107 	u_long pm_fsinfo;	/* fsinfo block number */
108 	u_long pm_nxtfree;	/* next place to search for a free cluster */
109 	u_int pm_fatmult;	/* these 2 values are used in FAT */
110 	u_int pm_fatdiv;	/*	offset computation */
111 	u_int pm_curfat;	/* current FAT for FAT32 (0 otherwise) */
112 	int pm_rootdirfree;	/* number of free slots in FAT12/16 root directory */
113 	u_int *pm_inusemap;	/* ptr to bitmap of in-use clusters */
114 	uint64_t pm_flags;	/* see below */
115 	void *pm_u2w;	/* Local->Unicode iconv handle */
116 	void *pm_w2u;	/* Unicode->Local iconv handle */
117 	void *pm_u2d;	/* Unicode->DOS iconv handle */
118 	void *pm_d2u;	/* DOS->Local iconv handle */
119 #ifndef MAKEFS
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 #endif
124 };
125 
126 /*
127  * A 64-bit file number and the 32-bit file number to which it is mapped,
128  * in a red-black tree node.
129  */
130 struct msdosfs_fileno {
131 	//RB_ENTRY(msdosfs_fileno)	mf_tree;
132 		// Haiku port:  not needed
133 	uint32_t			mf_fileno32;
134 	uint64_t			mf_fileno64;
135 };
136 
137 /* Byte offset in FAT on filesystem pmp, cluster cn */
138 #define	FATOFS(pmp, cn)	((cn) * (pmp)->pm_fatmult / (pmp)->pm_fatdiv)
139 
140 #define	VFSTOMSDOSFS(mp)	((struct msdosfsmount *)mp->mnt_data)
141 
142 /* Number of bits in one pm_inusemap item: */
143 #define	N_INUSEBITS	(8 * sizeof(u_int))
144 
145 /*
146  * Shorthand for fields in the bpb contained in the msdosfsmount structure.
147  */
148 #define	pm_BytesPerSec	pm_bpb.bpbBytesPerSec
149 #define	pm_ResSectors	pm_bpb.bpbResSectors
150 #define	pm_FATs		pm_bpb.bpbFATs
151 #define	pm_RootDirEnts	pm_bpb.bpbRootDirEnts
152 #define	pm_Sectors	pm_bpb.bpbSectors
153 #define	pm_Media	pm_bpb.bpbMedia
154 #define	pm_SecPerTrack	pm_bpb.bpbSecPerTrack
155 #define	pm_Heads	pm_bpb.bpbHeads
156 #define	pm_HiddenSects	pm_bpb.bpbHiddenSecs
157 #define	pm_HugeSectors	pm_bpb.bpbHugeSectors
158 
159 /*
160  * Convert pointer to buffer -> pointer to direntry
161  */
162 #define	bptoep(pmp, bp, dirofs) \
163 	((struct direntry *)(((bp)->b_data)	\
164 	 + ((dirofs) & (pmp)->pm_crbomask)))
165 
166 /*
167  * Convert block number to cluster number
168  */
169 #define	de_bn2cn(pmp, bn) \
170 	((bn) >> ((pmp)->pm_cnshift - (pmp)->pm_bnshift))
171 
172 /*
173  * Convert cluster number to block number
174  */
175 #define	de_cn2bn(pmp, cn) \
176 	((cn) << ((pmp)->pm_cnshift - (pmp)->pm_bnshift))
177 
178 /*
179  * Convert file offset to cluster number
180  */
181 #define de_cluster(pmp, off) \
182 	((off) >> (pmp)->pm_cnshift)
183 
184 /*
185  * Clusters required to hold size bytes
186  */
187 #define	de_clcount(pmp, size) \
188 	(((size) + (pmp)->pm_bpcluster - 1) >> (pmp)->pm_cnshift)
189 
190 /*
191  * Convert file offset to block number
192  */
193 #define de_blk(pmp, off) \
194 	(de_cn2bn(pmp, de_cluster((pmp), (off))))
195 
196 /*
197  * Convert cluster number to file offset
198  */
199 #define	de_cn2off(pmp, cn) \
200 	((cn) << (pmp)->pm_cnshift)
201 
202 /*
203  * Convert block number to file offset
204  */
205 #define	de_bn2off(pmp, bn) \
206 	((bn) << (pmp)->pm_bnshift)
207 /*
208  * Map a cluster number into a filesystem relative block number.
209  */
210 #define	cntobn(pmp, cn) \
211 	(de_cn2bn((pmp), (cn)-CLUST_FIRST) + (pmp)->pm_firstcluster)
212 
213 /*
214  * Calculate block number for directory entry in root dir, offset dirofs
215  */
216 #define	roottobn(pmp, dirofs) \
217 	(de_blk((pmp), (dirofs)) + (pmp)->pm_rootdirblk)
218 
219 /*
220  * Calculate block number for directory entry at cluster dirclu, offset
221  * dirofs
222  */
223 #define	detobn(pmp, dirclu, dirofs) \
224 	((dirclu) == MSDOSFSROOT \
225 	 ? roottobn((pmp), (dirofs)) \
226 	 : cntobn((pmp), (dirclu)))
227 
228 /*
229  * Increment the number of used entries in a fixed size FAT12/16 root
230  * directory
231  */
232 #define rootde_alloced(dep)			   \
233 	if ((dep)->de_StartCluster == MSDOSFSROOT) \
234 		(dep)->de_pmp->pm_rootdirfree--;
235 
236 /*
237  * Decrement the number of used entries in a fixed size FAT12/16 root
238  * directory
239  */
240 #define rootde_freed(dep)			   \
241 	if ((dep)->de_StartCluster == MSDOSFSROOT) \
242 		(dep)->de_pmp->pm_rootdirfree++;
243 
244 #define	MSDOSFS_LOCK_MP(pmp) \
245 	lockmgr(&(pmp)->pm_fatlock, LK_EXCLUSIVE, NULL)
246 #define	MSDOSFS_UNLOCK_MP(pmp) \
247 	lockmgr(&(pmp)->pm_fatlock, LK_RELEASE, NULL)
248 #define	MSDOSFS_ASSERT_MP_LOCKED(pmp) \
249 	lockmgr_assert(&(pmp)->pm_fatlock, KA_XLOCKED)
250 
251 #endif /* _KERNEL || MAKEFS */
252 
253 // Haiku port:  not needed
254 #if 0
255 /*
256  *  Arguments to mount MSDOS filesystems.
257  */
258 struct msdosfs_args {
259 	char	*fspec;		/* blocks special holding the fs to mount */
260 	struct	oexport_args export;	/* network export information */
261 	uid_t	uid;		/* uid that owns msdosfs files */
262 	gid_t	gid;		/* gid that owns msdosfs files */
263 	mode_t	mask;		/* file mask to be applied for msdosfs perms */
264 	int	flags;		/* see below */
265 	int	unused1;	/* unused, was version number */
266 	uint16_t unused2[128];	/* no longer used, was Local->Unicode table */
267 	char	*cs_win;	/* Windows(Unicode) Charset */
268 	char	*cs_dos;	/* DOS Charset */
269 	char	*cs_local;	/* Local Charset */
270 	mode_t	dirmask;	/* dir  mask to be applied for msdosfs perms */
271 };
272 #endif /* 0 */
273 
274 /*
275  * Msdosfs mount options:
276  */
277 #define	MSDOSFSMNT_SHORTNAME	1	/* Force old DOS short names only */
278 #define	MSDOSFSMNT_LONGNAME	2	/* Force Win'95 long names */
279 #define	MSDOSFSMNT_NOWIN95	4	/* Completely ignore Win95 entries */
280 #define	MSDOSFSMNT_KICONV	0x10    /* Use libiconv to convert chars */
281 /* All flags above: */
282 #define	MSDOSFSMNT_MNTOPT \
283 	(MSDOSFSMNT_SHORTNAME|MSDOSFSMNT_LONGNAME|MSDOSFSMNT_NOWIN95 \
284 	 |MSDOSFSMNT_KICONV)
285 #define	MSDOSFSMNT_RONLY	0x80000000	/* mounted read-only	*/
286 #define	MSDOSFSMNT_WAITONFAT	0x40000000	/* mounted synchronous	*/
287 #define	MSDOSFS_FATMIRROR	0x20000000	/* FAT is mirrored */
288 #define	MSDOSFS_FSIMOD		0x01000000
289 #define	MSDOSFS_ERR_RO		0x00800000	/* remouning ro due to error */
290 
291 #ifdef _KERNEL
292 void msdosfs_integrity_error(struct msdosfsmount *pmp);
293 #endif
294 
295 #endif /* !_MSDOSFS_MSDOSFSMOUNT_H_ */
296