1/*	$OpenBSD: denode.h,v 1.2 2016/12/17 16:43:30 krw Exp $	*/
2/*	$NetBSD: denode.h,v 1.24 2014/07/08 09:21:52 hannken Exp $	*/
3
4/*-
5 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
6 * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
7 * All rights reserved.
8 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *	This product includes software developed by TooLs GmbH.
21 * 4. The name of TooLs GmbH may not be used to endorse or promote products
22 *    derived from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
30 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35/*
36 * Written by Paul Popelka (paulp@uts.amdahl.com)
37 *
38 * You can do anything you want with this software, just don't say you wrote
39 * it, and don't remove this notice.
40 *
41 * This software is provided "as is".
42 *
43 * The author supplies this software to be publicly redistributed on the
44 * understanding that the author is not responsible for the correct
45 * functioning of this software in any circumstances and is not liable for
46 * any damages caused by this software.
47 *
48 * October 1992
49 */
50#ifndef _MSDOSFS_DENODE_H_
51#define _MSDOSFS_DENODE_H_
52
53struct genfs_node {
54};
55struct mkfsvnode;
56struct msdosfsmount;
57struct mkfsbuf;
58
59/*
60 * This is the pc filesystem specific portion of the mkfsvnode structure.
61 *
62 * To describe a file uniquely the de_dirclust, de_diroffset, and
63 * de_StartCluster fields are used.
64 *
65 * de_dirclust contains the cluster number of the directory cluster
66 *	containing the entry for a file or directory.
67 * de_diroffset is the index into the cluster for the entry describing
68 *	a file or directory.
69 * de_StartCluster is the number of the first cluster of the file or directory.
70 *
71 * Now to describe the quirks of the pc filesystem.
72 * - Clusters 0 and 1 are reserved.
73 * - The first allocatable cluster is 2.
74 * - The root directory is of fixed size and all blocks that make it up
75 *   are contiguous.
76 * - Cluster 0 refers to the root directory when it is found in the
77 *   startcluster field of a directory entry that points to another directory.
78 * - Cluster 0 implies a 0 length file when found in the start cluster field
79 *   of a directory entry that points to a file.
80 * - You can't use the cluster number 0 to derive the address of the root
81 *   directory.
82 * - Multiple directory entries can point to a directory. The entry in the
83 *   parent directory points to a child directory.  Any directories in the
84 *   child directory contain a ".." entry that points back to the parent.
85 *   The child directory itself contains a "." entry that points to itself.
86 * - The root directory does not contain a "." or ".." entry.
87 * - Directory entries for directories are never changed once they are created
88 *   (except when removed).  The size stays 0, and the last modification time
89 *   is never changed.  This is because so many directory entries can point to
90 *   the physical clusters that make up a directory.  It would lead to an
91 *   update nightmare.
92 * - The length field in a directory entry pointing to a directory contains 0
93 *   (always).  The only way to find the end of a directory is to follow the
94 *   cluster chain until the "last cluster" marker is found.
95 *
96 * My extensions to make this house of cards work.  These apply only to the in
97 * memory copy of the directory entry.
98 * - A reference count for each denode will be kept since dos doesn't keep such
99 *   things.
100 */
101
102/*
103 * Internal pseudo-offset for (nonexistent) directory entry for the root
104 * dir in the root dir
105 */
106#define	MSDOSFSROOT_OFS	0x1fffffff
107
108/*
109 * The FAT cache structure. fc_fsrcn is the filesystem relative cluster
110 * number that corresponds to the file relative cluster number in this
111 * structure (fc_frcn).
112 */
113struct fatcache {
114	u_long fc_frcn;		/* file relative cluster number */
115	u_long fc_fsrcn;	/* filesystem relative cluster number */
116};
117
118/*
119 * The FAT entry cache as it stands helps make extending files a "quick"
120 * operation by avoiding having to scan the FAT to discover the last
121 * cluster of the file. The cache also helps sequential reads by
122 * remembering the last cluster read from the file.  This also prevents us
123 * from having to rescan the FAT to find the next cluster to read.  This
124 * cache is probably pretty worthless if a file is opened by multiple
125 * processes.
126 */
127#define	FC_SIZE		3	/* number of entries in the cache */
128#define	FC_LASTMAP	0	/* entry the last call to pcbmap() resolved
129				 * to */
130#define	FC_LASTFC	1	/* entry for the last cluster in the file */
131#define	FC_NEXTTOLASTFC	2	/* entry for a close to the last cluster in the file */
132
133#define	FCE_EMPTY	0xffffffff	/* doesn't represent an actual cluster # */
134
135/*
136 * Set a slot in the FAT cache.
137 */
138#define	fc_setcache(dep, slot, frcn, fsrcn) \
139	(dep)->de_fc[slot].fc_frcn = frcn; \
140	(dep)->de_fc[slot].fc_fsrcn = fsrcn;
141
142#define fc_last_to_nexttolast(dep) \
143	do {  \
144		(dep)->de_fc[FC_NEXTTOLASTFC].fc_frcn = (dep)->de_fc[FC_LASTFC].fc_frcn; \
145		(dep)->de_fc[FC_NEXTTOLASTFC].fc_fsrcn = (dep)->de_fc[FC_LASTFC].fc_fsrcn; \
146	} while (0)
147
148
149/*
150 * This is the in memory variant of a dos directory entry.  It is usually
151 * contained within a mkfsvnode.
152 */
153struct denode_key {
154	u_long dk_dirclust;	/* cluster of the directory file containing this entry */
155	u_long dk_diroffset;	/* offset of this entry in the directory cluster */
156	void *dk_dirgen;	/* non zero and unique for unlinked nodes */
157};
158struct denode {
159	struct genfs_node de_gnode;
160	struct mkfsvnode *de_mkfsvnode;	/* addr of mkfsvnode we are part of */
161	struct mkfsvnode *de_devvp;	/* mkfsvnode of blk dev we live on */
162	u_long de_flag;		/* flag bits */
163	dev_t de_dev;		/* device where direntry lives */
164	struct denode_key de_key;
165#define de_dirclust de_key.dk_dirclust
166#define de_diroffset de_key.dk_diroffset
167#define de_dirgen de_key.dk_dirgen
168	u_long de_fndoffset;	/* offset of found dir entry */
169	int de_fndcnt;		/* number of slots before de_fndoffset */
170	long de_refcnt;		/* reference count */
171	struct msdosfsmount *de_pmp;	/* addr of our mount struct */
172	struct lockf *de_lockf;	/* byte level lock list */
173	u_char de_Name[12];	/* name, from DOS directory entry */
174	u_char de_Attributes;	/* attributes, from directory entry */
175	u_char de_CHun;		/* Hundredth of second of CTime*/
176	u_short de_CTime;	/* creation time */
177	u_short de_CDate;	/* creation date */
178	u_short de_ADate;	/* access date */
179	u_short de_MTime;	/* modification time */
180	u_short de_MDate;	/* modification date */
181	u_long de_StartCluster; /* starting cluster of file */
182	u_long de_FileSize;	/* size of file in bytes */
183	struct fatcache de_fc[FC_SIZE];	/* FAT cache */
184};
185
186/*
187 * Values for the de_flag field of the denode.
188 */
189#define	DE_UPDATE	0x0001	/* Modification time update request. */
190#define	DE_CREATE	0x0002	/* Creation time update */
191#define	DE_ACCESS	0x0004	/* Access time update */
192#define	DE_MODIFIED	0x0008	/* Denode has been modified. */
193#define	DE_RENAME	0x0010	/* Denode is in the process of being renamed */
194
195/*
196 * Maximum filename length in Win95
197 * Note: Must be < sizeof(dirent.d_name)
198 */
199#define	WIN_MAXLEN	255
200
201/* Maximum size of a file on a FAT filesystem */
202#define MSDOSFS_FILESIZE_MAX	0xFFFFFFFFLL
203
204/*
205 * Transfer directory entries between internal and external form.
206 * dep is a struct denode * (internal form),
207 * dp is a struct direntry * (external form).
208 */
209#define DE_INTERNALIZE32(dep, dp)			\
210	 ((dep)->de_StartCluster |= getushort((dp)->deHighClust) << 16)
211#define DE_INTERNALIZE(dep, dp)				\
212	(memcpy((dep)->de_Name, (dp)->deName, 11),	\
213	 (dep)->de_Attributes = (dp)->deAttributes,	\
214	 (dep)->de_CHun = (dp)->deCTimeHundredth,	\
215	 (dep)->de_CTime = getushort((dp)->deCTime),	\
216	 (dep)->de_CDate = getushort((dp)->deCDate),	\
217	 (dep)->de_ADate = getushort((dp)->deADate),	\
218	 (dep)->de_MTime = getushort((dp)->deMTime),	\
219	 (dep)->de_MDate = getushort((dp)->deMDate),	\
220	 (dep)->de_StartCluster = getushort((dp)->deStartCluster), \
221	 (dep)->de_FileSize = getulong((dp)->deFileSize), \
222	 (FAT32((dep)->de_pmp) ? DE_INTERNALIZE32((dep), (dp)) : 0))
223
224#define DE_EXTERNALIZE32(dp, dep)			\
225	 putushort((dp)->deHighClust, (dep)->de_StartCluster >> 16)
226#define DE_EXTERNALIZE16(dp, dep)			\
227	 putushort((dp)->deHighClust, 0)
228#define DE_EXTERNALIZE(dp, dep)				\
229	(memcpy((dp)->deName, (dep)->de_Name, 11),	\
230	 (dp)->deAttributes = (dep)->de_Attributes,	\
231	 (dp)->deCTimeHundredth = (dep)->de_CHun,	\
232	 putushort((dp)->deCTime, (dep)->de_CTime),	\
233	 putushort((dp)->deCDate, (dep)->de_CDate),	\
234	 putushort((dp)->deADate, (dep)->de_ADate),	\
235	 putushort((dp)->deMTime, (dep)->de_MTime),	\
236	 putushort((dp)->deMDate, (dep)->de_MDate),	\
237	 putushort((dp)->deStartCluster, (dep)->de_StartCluster), \
238	 putulong((dp)->deFileSize,			\
239	     ((dep)->de_Attributes & ATTR_DIRECTORY) ? 0 : (dep)->de_FileSize), \
240	 (FAT32((dep)->de_pmp) ? DE_EXTERNALIZE32((dp), (dep)) : DE_EXTERNALIZE16((dp), (dep))))
241
242#define	de_forw		de_chain[0]
243#define	de_back		de_chain[1]
244
245
246#define	VTODE(vp)	((struct denode *)(vp)->v_data)
247#define	DETOV(de)	((de)->de_mkfsvnode)
248
249#define	DETIMES(dep, acc, mod, cre, gmtoff) \
250	while ((dep)->de_flag & (DE_UPDATE | DE_CREATE | DE_ACCESS)) \
251		msdosfs_detimes(dep, acc, mod, cre, gmtoff)
252
253/*
254 * This overlays the fid structure (see fstypes.h)
255 */
256struct defid {
257	u_int16_t defid_len;	/* length of structure */
258	u_int16_t defid_pad;	/* force 4-byte alignment */
259
260	u_int32_t defid_dirclust; /* cluster this dir entry came from */
261	u_int32_t defid_dirofs;	/* offset of entry within the cluster */
262	u_int32_t defid_gen;	/* generation number */
263};
264
265/*
266 * Prototypes for MSDOSFS mkfsvnode operations
267 */
268int	msdosfs_lookup		(void *);
269int	msdosfs_create		(void *);
270int	msdosfs_close		(void *);
271int	msdosfs_access		(void *);
272int	msdosfs_getattr		(void *);
273int	msdosfs_setattr		(void *);
274int	msdosfs_read		(void *);
275int	msdosfs_write		(void *);
276#define	msdosfs_lease_check	genfs_lease_check
277#define	msdosfs_ioctl		genfs_enoioctl
278#define	msdosfs_poll		genfs_poll
279#define	msdosfs_revoke		genfs_revoke
280#define	msdosfs_mmap		genfs_mmap
281int	msdosfs_fsync		(void *);
282#define	msdosfs_seek		genfs_seek
283int	msdosfs_remove		(void *);
284int	msdosfs_rename		(void *);
285int	msdosfs_mkdir		(void *);
286int	msdosfs_rmdir		(void *);
287int	msdosfs_readdir		(void *);
288#define	msdosfs_abortop		genfs_abortop
289int	msdosfs_inactive	(void *);
290int	msdosfs_reclaim		(void *);
291int	msdosfs_bmap		(void *);
292int	msdosfs_strategy	(void *);
293int	msdosfs_print		(void *);
294int	msdosfs_advlock		(void *);
295int	msdosfs_pathconf	(void *);
296
297/*
298 * Internal service routine prototypes.
299 */
300struct componentname;
301struct direntry;
302int msdosfs_update(struct mkfsvnode *, const struct timespec *,
303	    const struct timespec *, int);
304int createde(struct denode *, struct denode *,
305		struct denode **, struct componentname *);
306int deextend(struct denode *, u_long);
307int deget(struct msdosfsmount *, u_long, u_long, struct denode **);
308int detrunc(struct denode *, u_long, int);
309int deupdat(struct denode *, int);
310int readde(struct denode *, struct mkfsbuf **, struct direntry **);
311int readep(struct msdosfsmount *, u_long, u_long,
312		struct mkfsbuf **, struct direntry **);
313int uniqdosname(struct denode *, struct componentname *, u_char *);
314int msdosfs_gop_alloc(struct mkfsvnode *, off_t, off_t, int);
315void msdosfs_gop_markupdate(struct mkfsvnode *, int);
316void msdosfs_detimes(struct denode *, const struct timespec *,
317    const struct timespec *, const struct timespec *, int);
318int msdosfs_fh_enter(struct msdosfsmount *, uint32_t, uint32_t, uint32_t *);
319int msdosfs_fh_remove(struct msdosfsmount *, uint32_t, uint32_t);
320int msdosfs_fh_lookup(struct msdosfsmount *, uint32_t, uint32_t, uint32_t *);
321void msdosfs_fh_destroy(struct msdosfsmount *);
322#endif /* _MSDOSFS_DENODE_H_ */
323