xref: /openbsd-src/sys/msdosfs/denode.h (revision b1b3e58b9987368ec1770ea58497311aa0951089)
1*b1b3e58bSjsg /*	$OpenBSD: denode.h,v 1.36 2022/08/15 01:47:09 jsg Exp $	*/
2b099d67bSprovos /*	$NetBSD: denode.h,v 1.24 1997/10/17 11:23:39 ws Exp $	*/
3df930be7Sderaadt 
4df930be7Sderaadt /*-
5b099d67bSprovos  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
6b099d67bSprovos  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
7df930be7Sderaadt  * All rights reserved.
8df930be7Sderaadt  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
9df930be7Sderaadt  *
10df930be7Sderaadt  * Redistribution and use in source and binary forms, with or without
11df930be7Sderaadt  * modification, are permitted provided that the following conditions
12df930be7Sderaadt  * are met:
13df930be7Sderaadt  * 1. Redistributions of source code must retain the above copyright
14df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer.
15df930be7Sderaadt  * 2. Redistributions in binary form must reproduce the above copyright
16df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer in the
17df930be7Sderaadt  *    documentation and/or other materials provided with the distribution.
18df930be7Sderaadt  * 3. All advertising materials mentioning features or use of this software
19df930be7Sderaadt  *    must display the following acknowledgement:
20df930be7Sderaadt  *	This product includes software developed by TooLs GmbH.
21df930be7Sderaadt  * 4. The name of TooLs GmbH may not be used to endorse or promote products
22df930be7Sderaadt  *    derived from this software without specific prior written permission.
23df930be7Sderaadt  *
24df930be7Sderaadt  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
25df930be7Sderaadt  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26df930be7Sderaadt  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27df930be7Sderaadt  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28df930be7Sderaadt  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29df930be7Sderaadt  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
30df930be7Sderaadt  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31df930be7Sderaadt  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32df930be7Sderaadt  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33df930be7Sderaadt  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34df930be7Sderaadt  */
35df930be7Sderaadt /*
36df930be7Sderaadt  * Written by Paul Popelka (paulp@uts.amdahl.com)
37df930be7Sderaadt  *
38df930be7Sderaadt  * You can do anything you want with this software, just don't say you wrote
39df930be7Sderaadt  * it, and don't remove this notice.
40df930be7Sderaadt  *
41df930be7Sderaadt  * This software is provided "as is".
42df930be7Sderaadt  *
43df930be7Sderaadt  * The author supplies this software to be publicly redistributed on the
44df930be7Sderaadt  * understanding that the author is not responsible for the correct
45df930be7Sderaadt  * functioning of this software in any circumstances and is not liable for
46df930be7Sderaadt  * any damages caused by this software.
47df930be7Sderaadt  *
48df930be7Sderaadt  * October 1992
49df930be7Sderaadt  */
50df930be7Sderaadt 
51df930be7Sderaadt /*
52df930be7Sderaadt  * This is the pc filesystem specific portion of the vnode structure.
53df930be7Sderaadt  *
54df930be7Sderaadt  * To describe a file uniquely the de_dirclust, de_diroffset, and
55df930be7Sderaadt  * de_StartCluster fields are used.
56df930be7Sderaadt  *
57df930be7Sderaadt  * de_dirclust contains the cluster number of the directory cluster
58df930be7Sderaadt  *	containing the entry for a file or directory.
59df930be7Sderaadt  * de_diroffset is the index into the cluster for the entry describing
60df930be7Sderaadt  *	a file or directory.
61df930be7Sderaadt  * de_StartCluster is the number of the first cluster of the file or directory.
62df930be7Sderaadt  *
63df930be7Sderaadt  * Now to describe the quirks of the pc filesystem.
64df930be7Sderaadt  * - Clusters 0 and 1 are reserved.
65df930be7Sderaadt  * - The first allocatable cluster is 2.
66df930be7Sderaadt  * - The root directory is of fixed size and all blocks that make it up
67df930be7Sderaadt  *   are contiguous.
68df930be7Sderaadt  * - Cluster 0 refers to the root directory when it is found in the
69df930be7Sderaadt  *   startcluster field of a directory entry that points to another directory.
70df930be7Sderaadt  * - Cluster 0 implies a 0 length file when found in the start cluster field
71df930be7Sderaadt  *   of a directory entry that points to a file.
72df930be7Sderaadt  * - You can't use the cluster number 0 to derive the address of the root
73df930be7Sderaadt  *   directory.
74df930be7Sderaadt  * - Multiple directory entries can point to a directory. The entry in the
75df930be7Sderaadt  *   parent directory points to a child directory.  Any directories in the
76df930be7Sderaadt  *   child directory contain a ".." entry that points back to the parent.
77df930be7Sderaadt  *   The child directory itself contains a "." entry that points to itself.
78df930be7Sderaadt  * - The root directory does not contain a "." or ".." entry.
79df930be7Sderaadt  * - Directory entries for directories are never changed once they are created
80df930be7Sderaadt  *   (except when removed).  The size stays 0, and the last modification time
81df930be7Sderaadt  *   is never changed.  This is because so many directory entries can point to
82df930be7Sderaadt  *   the physical clusters that make up a directory.  It would lead to an
83df930be7Sderaadt  *   update nightmare.
84df930be7Sderaadt  * - The length field in a directory entry pointing to a directory contains 0
85df930be7Sderaadt  *   (always).  The only way to find the end of a directory is to follow the
86df930be7Sderaadt  *   cluster chain until the "last cluster" marker is found.
87df930be7Sderaadt  *
88df930be7Sderaadt  * My extensions to make this house of cards work.  These apply only to the in
89df930be7Sderaadt  * memory copy of the directory entry.
90df930be7Sderaadt  * - A reference count for each denode will be kept since dos doesn't keep such
91df930be7Sderaadt  *   things.
92df930be7Sderaadt  */
93df930be7Sderaadt 
94df930be7Sderaadt /*
95df930be7Sderaadt  * Internal pseudo-offset for (nonexistent) directory entry for the root
96df930be7Sderaadt  * dir in the root dir
97df930be7Sderaadt  */
98df930be7Sderaadt #define	MSDOSFSROOT_OFS	0x1fffffff
99df930be7Sderaadt 
100df930be7Sderaadt /*
101df930be7Sderaadt  * The fat cache structure. fc_fsrcn is the filesystem relative cluster
102df930be7Sderaadt  * number that corresponds to the file relative cluster number in this
103df930be7Sderaadt  * structure (fc_frcn).
104df930be7Sderaadt  */
105df930be7Sderaadt struct fatcache {
10682fa9538Stedu 	uint32_t fc_frcn;		/* file relative cluster number */
10782fa9538Stedu 	uint32_t fc_fsrcn;	/* filesystem relative cluster number */
108df930be7Sderaadt };
109df930be7Sderaadt 
110df930be7Sderaadt /*
111df930be7Sderaadt  * The fat entry cache as it stands helps make extending files a "quick"
112df930be7Sderaadt  * operation by avoiding having to scan the fat to discover the last
113df930be7Sderaadt  * cluster of the file. The cache also helps sequential reads by
114df930be7Sderaadt  * remembering the last cluster read from the file.  This also prevents us
115df930be7Sderaadt  * from having to rescan the fat to find the next cluster to read.  This
116df930be7Sderaadt  * cache is probably pretty worthless if a file is opened by multiple
117df930be7Sderaadt  * processes.
118df930be7Sderaadt  */
11951bdbb7dSmikeb #define	FC_SIZE		3	/* number of entries in the cache */
120df930be7Sderaadt #define	FC_LASTMAP	0	/* entry the last call to pcbmap() resolved
121df930be7Sderaadt 				 * to */
122df930be7Sderaadt #define	FC_LASTFC	1	/* entry for the last cluster in the file */
12351bdbb7dSmikeb #define	FC_OLASTFC	2	/* entry for the previous last cluster */
124df930be7Sderaadt 
125b099d67bSprovos #define	FCE_EMPTY	0xffffffff	/* doesn't represent an actual cluster # */
126df930be7Sderaadt 
127df930be7Sderaadt /*
128df930be7Sderaadt  * Set a slot in the fat cache.
129df930be7Sderaadt  */
130df930be7Sderaadt #define	fc_setcache(dep, slot, frcn, fsrcn) \
131df930be7Sderaadt 	(dep)->de_fc[slot].fc_frcn = frcn; \
132df930be7Sderaadt 	(dep)->de_fc[slot].fc_fsrcn = fsrcn;
133df930be7Sderaadt 
134df930be7Sderaadt /*
135df930be7Sderaadt  * This is the in memory variant of a dos directory entry.  It is usually
136df930be7Sderaadt  * contained within a vnode.
137df930be7Sderaadt  */
138df930be7Sderaadt struct denode {
139df930be7Sderaadt 	struct denode *de_next;	/* Hash chain forward */
140df930be7Sderaadt 	struct denode **de_prev; /* Hash chain back */
141df930be7Sderaadt 	struct vnode *de_vnode;	/* addr of vnode we are part of */
142df930be7Sderaadt 	struct vnode *de_devvp;	/* vnode of blk dev we live on */
14382fa9538Stedu 	uint32_t de_flag;		/* flag bits */
144df930be7Sderaadt 	dev_t de_dev;		/* device where direntry lives */
14582fa9538Stedu 	uint32_t de_dirclust;	/* cluster of the directory file containing this entry */
14682fa9538Stedu 	uint32_t de_diroffset;	/* offset of this entry in the directory cluster */
14782fa9538Stedu 	uint32_t de_fndoffset;	/* offset of found dir entry */
14816bf7bd1Sderaadt 	int de_fndcnt;		/* number of slots before de_fndoffset */
149df930be7Sderaadt 	long de_refcnt;		/* reference count */
150df930be7Sderaadt 	struct msdosfsmount *de_pmp;	/* addr of our mount struct */
1516bd4f7caSanton 	struct lockf_state *de_lockf;	/* byte level lock list */
15226b8ec94Snatano 	struct rrwlock de_lock;	/* denode lock */
153e6f855f7Skrw 	u_char de_Name[11];	/* name, from DOS directory entry */
154df930be7Sderaadt 	u_char de_Attributes;	/* attributes, from directory entry */
155bae0976aSderaadt 	u_char de_CTimeHundredth; /* creation time, 1/100th of a sec */
15616bf7bd1Sderaadt 	u_short de_CTime;	/* creation time */
15716bf7bd1Sderaadt 	u_short de_CDate;	/* creation date */
15816bf7bd1Sderaadt 	u_short de_ADate;	/* access date */
15916bf7bd1Sderaadt 	u_short de_MTime;	/* modification time */
16016bf7bd1Sderaadt 	u_short de_MDate;	/* modification date */
16182fa9538Stedu 	uint32_t de_StartCluster; /* starting cluster of file */
16282fa9538Stedu 	uint32_t de_FileSize;	/* size of file in bytes */
163df930be7Sderaadt 	struct fatcache de_fc[FC_SIZE];	/* fat cache */
164df930be7Sderaadt };
165df930be7Sderaadt 
166df930be7Sderaadt /*
167df930be7Sderaadt  * Values for the de_flag field of the denode.
168df930be7Sderaadt  */
169df930be7Sderaadt #define	DE_UPDATE	0x0004	/* Modification time update request. */
17016bf7bd1Sderaadt #define	DE_CREATE	0x0008	/* Creation time update */
17116bf7bd1Sderaadt #define	DE_ACCESS	0x0010	/* Access time update */
17216bf7bd1Sderaadt #define	DE_MODIFIED	0x0020	/* Denode has been modified. */
17316bf7bd1Sderaadt #define	DE_RENAME	0x0040	/* Denode is in the process of being renamed */
17416bf7bd1Sderaadt 
17516bf7bd1Sderaadt /*
17616bf7bd1Sderaadt  * Maximum filename length in Win95
17716bf7bd1Sderaadt  * Note: Must be < sizeof(dirent.d_name)
17816bf7bd1Sderaadt  */
17916bf7bd1Sderaadt #define	WIN_MAXLEN	255
180df930be7Sderaadt 
1818b243da0Stom /* Maximum size of a file on a FAT filesystem */
1828b243da0Stom #define MSDOSFS_FILESIZE_MAX	0xFFFFFFFFLL
1838b243da0Stom 
184df930be7Sderaadt /*
185df930be7Sderaadt  * Transfer directory entries between internal and external form.
186df930be7Sderaadt  * dep is a struct denode * (internal form),
187df930be7Sderaadt  * dp is a struct direntry * (external form).
188df930be7Sderaadt  */
189b099d67bSprovos #define DE_INTERNALIZE32(dep, dp)                      \
190b099d67bSprovos         ((dep)->de_StartCluster |= getushort((dp)->deHighClust) << 16)
191df930be7Sderaadt #define DE_INTERNALIZE(dep, dp)			\
1927aea9c4dSnicm 	(bcopy((dp)->deName, (dep)->de_Name, 8),	\
1937aea9c4dSnicm 	 bcopy((dp)->deExtension, (dep)->de_Name + 8, 3), \
194df930be7Sderaadt 	 (dep)->de_Attributes = (dp)->deAttributes,	\
195bae0976aSderaadt 	 (dep)->de_CTimeHundredth = (dp)->deCTimeHundredth, \
19616bf7bd1Sderaadt 	 (dep)->de_CTime = getushort((dp)->deCTime),	\
19716bf7bd1Sderaadt 	 (dep)->de_CDate = getushort((dp)->deCDate),	\
19816bf7bd1Sderaadt 	 (dep)->de_ADate = getushort((dp)->deADate),	\
19916bf7bd1Sderaadt 	 (dep)->de_MTime = getushort((dp)->deMTime),	\
20016bf7bd1Sderaadt 	 (dep)->de_MDate = getushort((dp)->deMDate),	\
201df930be7Sderaadt 	 (dep)->de_StartCluster = getushort((dp)->deStartCluster), \
202b099d67bSprovos 	 (dep)->de_FileSize = getulong((dp)->deFileSize), \
203b099d67bSprovos 	 (FAT32((dep)->de_pmp) ? DE_INTERNALIZE32((dep), (dp)) : 0))
204df930be7Sderaadt 
205df930be7Sderaadt #define DE_EXTERNALIZE(dp, dep)				\
2067aea9c4dSnicm 	(bcopy((dep)->de_Name, (dp)->deName, 8),	\
2077aea9c4dSnicm 	 bcopy((dep)->de_Name + 8, (dp)->deExtension, 3), \
208df930be7Sderaadt 	 (dp)->deAttributes = (dep)->de_Attributes,	\
209bae0976aSderaadt 	 (dp)->deLowerCase = CASE_LOWER_BASE | CASE_LOWER_EXT,	\
210bae0976aSderaadt 	 (dp)->deCTimeHundredth = (dep)->de_CTimeHundredth, \
21116bf7bd1Sderaadt 	 putushort((dp)->deCTime, (dep)->de_CTime),	\
21216bf7bd1Sderaadt 	 putushort((dp)->deCDate, (dep)->de_CDate),	\
21316bf7bd1Sderaadt 	 putushort((dp)->deADate, (dep)->de_ADate),	\
21416bf7bd1Sderaadt 	 putushort((dp)->deMTime, (dep)->de_MTime),	\
21516bf7bd1Sderaadt 	 putushort((dp)->deMDate, (dep)->de_MDate),	\
216df930be7Sderaadt 	 putushort((dp)->deStartCluster, (dep)->de_StartCluster), \
217df930be7Sderaadt 	 putulong((dp)->deFileSize, \
218b099d67bSprovos 	     ((dep)->de_Attributes & ATTR_DIRECTORY) ? 0 : (dep)->de_FileSize),\
219aed070fdStedu 	 putushort((dp)->deHighClust, \
220aed070fdStedu 	     FAT32((dep)->de_pmp) ? (dep)->de_StartCluster >> 16 : 0))
221df930be7Sderaadt 
222df930be7Sderaadt #define	de_forw		de_chain[0]
223df930be7Sderaadt #define	de_back		de_chain[1]
224df930be7Sderaadt 
225df930be7Sderaadt #define	VTODE(vp)	((struct denode *)(vp)->v_data)
226df930be7Sderaadt #define	DETOV(de)	((de)->de_vnode)
227df930be7Sderaadt 
22878b4a1c4Smillert #define	DETIMES(dep, acc, mod, cre) \
22916bf7bd1Sderaadt 	if ((dep)->de_flag & (DE_UPDATE | DE_CREATE | DE_ACCESS)) { \
23078b4a1c4Smillert 		(dep)->de_flag |= DE_MODIFIED; \
23178b4a1c4Smillert 		if ((dep)->de_flag & DE_UPDATE) { \
232b099d67bSprovos 			unix2dostime((mod), &(dep)->de_MDate, &(dep)->de_MTime, NULL); \
233df930be7Sderaadt 			(dep)->de_Attributes |= ATTR_ARCHIVE; \
23416bf7bd1Sderaadt 		} \
23516bf7bd1Sderaadt 		if (!((dep)->de_pmp->pm_flags & MSDOSFSMNT_NOWIN95)) { \
23616bf7bd1Sderaadt 			if ((dep)->de_flag & DE_ACCESS) \
237b099d67bSprovos 				unix2dostime((acc), &(dep)->de_ADate, NULL, NULL); \
238b099d67bSprovos 			if ((dep)->de_flag & DE_CREATE) \
239b099d67bSprovos 				unix2dostime((cre), &(dep)->de_CDate, &(dep)->de_CTime, &(dep)->de_CTimeHundredth); \
240df930be7Sderaadt 		} \
24116bf7bd1Sderaadt 		(dep)->de_flag &= ~(DE_UPDATE | DE_CREATE | DE_ACCESS); \
242df930be7Sderaadt 	}
243df930be7Sderaadt 
244df930be7Sderaadt /*
245df930be7Sderaadt  * This overlays the fid structure (see mount.h)
246df930be7Sderaadt  */
247df930be7Sderaadt struct defid {
248df930be7Sderaadt 	u_short defid_len;	/* length of structure */
249df930be7Sderaadt 	u_short defid_pad;	/* force long alignment */
250df930be7Sderaadt 
25182fa9538Stedu 	uint32_t defid_dirclust;	/* cluster this dir entry came from */
25282fa9538Stedu 	uint32_t defid_dirofs;	/* offset of entry within the cluster */
253879b3eabSniklas #if 0
25482fa9538Stedu 	uint32_t	defid_gen;	/* generation number */
255879b3eabSniklas #endif
256df930be7Sderaadt };
257df930be7Sderaadt 
258b86e97fbSguenther 
259b86e97fbSguenther #ifdef _KERNEL
260df930be7Sderaadt /*
261df930be7Sderaadt  * Prototypes for MSDOSFS vnode operations
262df930be7Sderaadt  */
263c4071fd1Smillert int	msdosfs_lookup(void *);
264c4071fd1Smillert int	msdosfs_create(void *);
265c4071fd1Smillert int	msdosfs_mknod(void *);
266c4071fd1Smillert int	msdosfs_open(void *);
267c4071fd1Smillert int	msdosfs_close(void *);
268c4071fd1Smillert int	msdosfs_access(void *);
269c4071fd1Smillert int	msdosfs_getattr(void *);
270c4071fd1Smillert int	msdosfs_setattr(void *);
271c4071fd1Smillert int	msdosfs_read(void *);
272c4071fd1Smillert int	msdosfs_write(void *);
273c4071fd1Smillert int	msdosfs_ioctl(void *);
274c4071fd1Smillert int	msdosfs_fsync(void *);
275c4071fd1Smillert int	msdosfs_remove(void *);
276c4071fd1Smillert int	msdosfs_link(void *);
277c4071fd1Smillert int	msdosfs_rename(void *);
278c4071fd1Smillert int	msdosfs_mkdir(void *);
279c4071fd1Smillert int	msdosfs_rmdir(void *);
280c4071fd1Smillert int	msdosfs_symlink(void *);
281c4071fd1Smillert int	msdosfs_readdir(void *);
282c4071fd1Smillert int	msdosfs_readlink(void *);
283c4071fd1Smillert int	msdosfs_inactive(void *);
284c4071fd1Smillert int	msdosfs_reclaim(void *);
285c4071fd1Smillert int	msdosfs_lock(void *);
286c4071fd1Smillert int	msdosfs_unlock(void *);
287c4071fd1Smillert int	msdosfs_bmap(void *);
288c4071fd1Smillert int	msdosfs_strategy(void *);
289c4071fd1Smillert int	msdosfs_print(void *);
290c4071fd1Smillert int	msdosfs_islocked(void *);
291c4071fd1Smillert int	msdosfs_advlock(void *);
292c4071fd1Smillert int	msdosfs_pathconf(void *);
293df930be7Sderaadt 
294df930be7Sderaadt /*
295df930be7Sderaadt  * Internal service routine prototypes.
296df930be7Sderaadt  */
297c4071fd1Smillert int createde(struct denode *, struct denode *, struct denode **, struct componentname *);
29882fa9538Stedu int deextend(struct denode *, uint32_t, struct ucred *);
29982fa9538Stedu int deget(struct msdosfsmount *, uint32_t, uint32_t, struct denode **);
30082fa9538Stedu int detrunc(struct denode *, uint32_t, int, struct ucred *, struct proc *);
301c4071fd1Smillert int deupdat(struct denode *, int);
302c4071fd1Smillert int doscheckpath(struct denode *, struct denode *);
303c4071fd1Smillert int dosdirempty(struct denode *);
304c4071fd1Smillert int readde(struct denode *, struct buf **, struct direntry **);
30582fa9538Stedu int readep(struct msdosfsmount *, uint32_t, uint32_t, struct buf **, struct direntry **);
306c4071fd1Smillert void reinsert(struct denode *);
307c4071fd1Smillert int removede(struct denode *, struct denode *);
308c4071fd1Smillert int uniqdosname(struct denode *, struct componentname *, u_char *);
309df930be7Sderaadt #endif	/* _KERNEL */
310