xref: /freebsd/sys/fs/msdosfs/fat.h (revision 6be3386466ab79a84b48429ae66244f21526d3df)
1 /* $FreeBSD$ */
2 /*	$NetBSD: fat.h,v 1.12 1997/11/17 15:36:36 ws Exp $	*/
3 
4 /*-
5  * SPDX-License-Identifier: BSD-4-Clause
6  *
7  * Copyright (C) 1994, 1997 Wolfgang Solfrank.
8  * Copyright (C) 1994, 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 _FS_MSDOSFS_FAT_H_
54 #define	_FS_MSDOSFS_FAT_H_
55 /*
56  * Some useful cluster numbers.
57  */
58 #define	MSDOSFSROOT	0		/* cluster 0 means the root dir */
59 #define	CLUST_FREE	0		/* cluster 0 also means a free cluster */
60 #define	MSDOSFSFREE	CLUST_FREE
61 #define	CLUST_FIRST	2		/* first legal cluster number */
62 #define	CLUST_RSRVD	0xfffffff6	/* reserved cluster range */
63 #define	CLUST_BAD	0xfffffff7	/* a cluster with a defect */
64 #define	CLUST_EOFS	0xfffffff8	/* start of eof cluster range */
65 #define	CLUST_EOFE	0xffffffff	/* end of eof cluster range */
66 
67 #define	FAT12_MASK	0x00000fff	/* mask for 12 bit cluster numbers */
68 #define	FAT16_MASK	0x0000ffff	/* mask for 16 bit cluster numbers */
69 #define	FAT32_MASK	0x0fffffff	/* mask for FAT32 cluster numbers */
70 
71 /*
72  * MSDOSFS:
73  * Return true if filesystem uses 12 bit FATs. Microsoft Programmer's
74  * Reference says if the maximum cluster number in a filesystem is greater
75  * than 4084 ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK) then we've got a
76  * 16 bit FAT filesystem. While mounting, the result of this test is stored
77  * in pm_fatentrysize.
78  */
79 #define	FAT12(pmp)	(pmp->pm_fatmask == FAT12_MASK)
80 #define	FAT16(pmp)	(pmp->pm_fatmask == FAT16_MASK)
81 #define	FAT32(pmp)	(pmp->pm_fatmask == FAT32_MASK)
82 
83 #define	MSDOSFSEOF(pmp, cn)	((((cn) | ~(pmp)->pm_fatmask) & CLUST_EOFS) == CLUST_EOFS)
84 
85 #if defined (_KERNEL) || defined(MAKEFS)
86 /*
87  * These are the values for the function argument to the function
88  * fatentry().
89  */
90 #define	FAT_GET		0x0001	/* get a FAT entry */
91 #define	FAT_SET		0x0002	/* set a FAT entry */
92 #define	FAT_GET_AND_SET	(FAT_GET | FAT_SET)
93 
94 /*
95  * Flags to extendfile:
96  */
97 #define	DE_CLEAR	1	/* Zero out the blocks allocated */
98 
99 int pcbmap(struct denode *dep, u_long findcn, daddr_t *bnp, u_long *cnp, int* sp);
100 int clusterfree(struct msdosfsmount *pmp, u_long cn, u_long *oldcnp);
101 int clusteralloc(struct msdosfsmount *pmp, u_long start, u_long count, u_long fillwith, u_long *retcluster, u_long *got);
102 int fatentry(int function, struct msdosfsmount *pmp, u_long cluster, u_long *oldcontents, u_long newcontents);
103 int freeclusterchain(struct msdosfsmount *pmp, u_long startchain);
104 int extendfile(struct denode *dep, u_long count, struct buf **bpp, u_long *ncp, int flags);
105 void fc_purge(struct denode *dep, u_int frcn);
106 int markvoldirty_upgrade(struct msdosfsmount *pmp, bool dirty, bool rw_upgrade);
107 
108 static inline int
109 markvoldirty(struct msdosfsmount *pmp, bool dirty)
110 {
111 	return (markvoldirty_upgrade(pmp, dirty, false));
112 }
113 
114 #endif	/* _KERNEL || MAKEFS */
115 #endif	/* !_FS_MSDOSFS_FAT_H_ */
116