xref: /haiku/src/add-ons/kernel/file_systems/fat/dosfs.h (revision 1deede7388b04dbeec5af85cae7164735ea9e70d)
1 /*
2 	Copyright 1999-2001, Be Incorporated.   All Rights Reserved.
3 	This file may be used under the terms of the Be Sample Code License.
4 */
5 #ifndef _DOSFS_H_
6 #define _DOSFS_H_
7 
8 
9 #include "system_dependencies.h"
10 
11 
12 //#define DEBUG 1
13 
14 
15 #define	LOCK(l)		recursive_lock_lock(&l);
16 #define	UNLOCK(l)	recursive_lock_unlock(&l);
17 
18 
19 /* Unfortunately, ino_t's are defined as signed. This causes problems with
20  * programs (notably cp) that use the modulo of a ino_t as a
21  * hash function to index an array. This means the high bit of every ino_t
22  * is off-limits. Luckily, FAT32 is actually FAT28, so dosfs can make do with
23  * only 63 bits.
24  */
25 #define ARTIFICIAL_VNID_BITS	(0x6LL << 60)
26 #define DIR_CLUSTER_VNID_BITS	(0x4LL << 60)
27 #define DIR_INDEX_VNID_BITS		0
28 #define INVALID_VNID_BITS_MASK	(0x9LL << 60)
29 
30 #define IS_DIR_CLUSTER_VNID(vnid) \
31 	(((vnid) & ARTIFICIAL_VNID_BITS) == DIR_CLUSTER_VNID_BITS)
32 
33 #define IS_DIR_INDEX_VNID(vnid) \
34 	(((vnid) & ARTIFICIAL_VNID_BITS) == DIR_INDEX_VNID_BITS)
35 
36 #define IS_ARTIFICIAL_VNID(vnid) \
37 	(((vnid) & ARTIFICIAL_VNID_BITS) == ARTIFICIAL_VNID_BITS)
38 
39 #define IS_INVALID_VNID(vnid) \
40 	((!IS_DIR_CLUSTER_VNID((vnid)) && \
41 			!IS_DIR_INDEX_VNID((vnid)) && \
42 			!IS_ARTIFICIAL_VNID((vnid))) || \
43 		((vnid) & INVALID_VNID_BITS_MASK))
44 
45 #define GENERATE_DIR_INDEX_VNID(dircluster, index) \
46 	(DIR_INDEX_VNID_BITS | ((ino_t)(dircluster) << 32) | (index))
47 
48 #define GENERATE_DIR_CLUSTER_VNID(dircluster, filecluster) \
49 	(DIR_CLUSTER_VNID_BITS | ((ino_t)(dircluster) << 32) | (filecluster))
50 
51 #define CLUSTER_OF_DIR_CLUSTER_VNID(vnid) \
52 	((uint32)((vnid) & 0xffffffff))
53 
54 #define INDEX_OF_DIR_INDEX_VNID(vnid) \
55 	((uint32)((vnid) & 0xffffffff))
56 
57 #define DIR_OF_VNID(vnid) \
58 	((uint32)(((vnid) >> 32) & ~0xf0000000))
59 
60 #define VNODE_PARENT_DIR_CLUSTER(vnode) \
61 	CLUSTER_OF_DIR_CLUSTER_VNID((vnode)->dir_vnid)
62 
63 
64 typedef struct vnode {
65 	ino_t		vnid;			// self id
66 	ino_t		dir_vnid;		// parent vnode id (directory containing entry)
67 	void		*cache;
68 	void		*file_map;
69 
70 	uint32		disk_image;		// 0 = no, 1 = BEOS, 2 = IMAGE.BE
71 
72 	/* iteration is incremented each time the fat chain changes. it's used by
73 	 * the file read/write code to determine if it needs to retraverse the
74 	 * fat chain
75 	 */
76 	uint32		iteration;
77 
78 	/* any changes to this block of information should immediately be reflected
79 	 * on the disk (or at least in the cache) so that get_next_dirent continues
80 	 * to function properly
81 	 */
82 	uint32		sindex, eindex;	// starting and ending index of directory entry
83 	uint32		cluster;		// starting cluster of the data
84 	uint32		mode;			// dos-style attributes
85 	off_t		st_size;		// in bytes
86 	time_t		st_time;
87 	time_t		st_crtim;
88 
89 	uint32		end_cluster;	// last cluster of the data
90 
91 	const char *mime;			// mime type (null if none)
92 
93 	bool		dirty;			// track if vnode had been written to
94 
95 	char		*filename;
96 } vnode;
97 
98 // mode bits
99 #define FAT_READ_ONLY	1
100 #define FAT_HIDDEN		2
101 #define FAT_SYSTEM		4
102 #define FAT_VOLUME		8
103 #define FAT_SUBDIR		16
104 #define FAT_ARCHIVE		32
105 
106 
107 struct vcache_entry;
108 
109 typedef struct _nspace {
110 	fs_volume		*volume;		// fs_volume passed in to fs_mount
111 	dev_t			id;
112 	int				fd;				// File descriptor
113 	char			device[256];
114 	uint32			flags;			// see <fcntl.be.h> for modes
115 	void			*fBlockCache;
116 
117 	// info from bpb
118 	uint32	bytes_per_sector;
119 	uint32	sectors_per_cluster;
120 	uint32	reserved_sectors;
121 	uint32	fat_count;
122 	uint32	root_entries_count;
123 	uint32	total_sectors;
124 	uint32	sectors_per_fat;
125 	uint8	media_descriptor;
126 	uint16	fsinfo_sector;
127 
128 	uint32	total_clusters;			// data clusters, that is
129 	uint32	free_clusters;
130 	uint8	fat_bits;
131 	bool	fat_mirrored;			// true if fat mirroring on
132 	uint8	active_fat;
133 
134 	uint32	root_start;				// for fat12 + fat16 only
135 	uint32	root_sectors;			// for fat12 + fat16 only
136 	vnode	root_vnode;				// root directory
137 	int32	vol_entry;				// index in root directory
138 	char	vol_label[12];			// lfn's need not apply
139 
140 	uint32	data_start;
141 	uint32	last_allocated;			// last allocated cluster
142 
143 	ino_t	beos_vnid;				// vnid of \BEOS directory
144 	bool	respect_disk_image;
145 
146 	int		fs_flags;				// flags for this mount
147 
148 	recursive_lock	vlock;					// volume lock
149 
150 	// vcache state
151 	struct {
152 		rw_lock	lock;
153 		ino_t	cur_vnid;
154 		uint32	cache_size;
155 		struct vcache_entry **by_vnid, **by_loc;
156 	} vcache;
157 
158 	struct {
159 		uint32	entries;
160 		uint32	allocated;
161 		ino_t	*vnid_list;
162 	} dlist;
163 } nspace;
164 
165 #define FS_FLAGS_OP_SYNC		0x1
166 #define FS_FLAGS_LOCK_DOOR		0x2
167 
168 #define LOCK_VOL(vol) \
169 	if (vol == NULL) { dprintf("null vol\n"); return EINVAL; } else LOCK((vol)->vlock)
170 
171 #define UNLOCK_VOL(vol) \
172 	UNLOCK((vol)->vlock)
173 
174 #define TOUCH(x) ((void)(x))
175 
176 extern fs_vnode_ops gFATVnodeOps;
177 extern fs_volume_ops gFATVolumeOps;
178 
179 /* debug levels */
180 extern int debug_attr, debug_dir, debug_dlist, debug_dosfs, debug_encodings,
181 		debug_fat, debug_file, debug_iter, debug_vcache;
182 
183 status_t _dosfs_sync(nspace *vol);
184 
185 #endif
186