xref: /freebsd/sys/fs/msdosfs/denode.h (revision ba27dd8be821792e15bdabfac69fd6cab0cf9dd3)
1 /* $FreeBSD$ */
2 /*	$NetBSD: denode.h,v 1.25 1997/11/17 15:36:28 ws Exp $	*/
3 
4 /*-
5  * SPDX-License-Identifier: BSD-4-Clause
6  *
7  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
8  * Copyright (C) 1994, 1995, 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 #ifndef _FS_MSDOSFS_DENODE_H_
53 #define	_FS_MSDOSFS_DENODE_H_
54 
55 #include <sys/buf.h>
56 
57 /*
58  * This is the pc filesystem specific portion of the vnode structure.
59  *
60  * To describe a file uniquely the de_dirclust, de_diroffset, and
61  * de_StartCluster fields are used.
62  *
63  * de_dirclust contains the cluster number of the directory cluster
64  *	containing the entry for a file or directory.
65  * de_diroffset is the index into the cluster for the entry describing
66  *	a file or directory.
67  * de_StartCluster is the number of the first cluster of the file or directory.
68  *
69  * Now to describe the quirks of the pc filesystem.
70  * - Clusters 0 and 1 are reserved.
71  * - The first allocatable cluster is 2.
72  * - The root directory is of fixed size and all blocks that make it up
73  *   are contiguous.
74  * - Cluster 0 refers to the root directory when it is found in the
75  *   startcluster field of a directory entry that points to another directory.
76  * - Cluster 0 implies a 0 length file when found in the start cluster field
77  *   of a directory entry that points to a file.
78  * - You can't use the cluster number 0 to derive the address of the root
79  *   directory.
80  * - Multiple directory entries can point to a directory. The entry in the
81  *   parent directory points to a child directory.  Any directories in the
82  *   child directory contain a ".." entry that points back to the parent.
83  *   The child directory itself contains a "." entry that points to itself.
84  * - The root directory does not contain a "." or ".." entry.
85  * - Directory entries for directories are never changed once they are created
86  *   (except when removed).  The size stays 0, and the last modification time
87  *   is never changed.  This is because so many directory entries can point to
88  *   the physical clusters that make up a directory.  It would lead to an
89  *   update nightmare.
90  * - The length field in a directory entry pointing to a directory contains 0
91  *   (always).  The only way to find the end of a directory is to follow the
92  *   cluster chain until the "last cluster" marker is found.
93  *
94  * My extensions to make this house of cards work.  These apply only to the in
95  * memory copy of the directory entry.
96  * - A reference count for each denode will be kept since dos doesn't keep such
97  *   things.
98  */
99 
100 /*
101  * Internal pseudo-offset for (nonexistent) directory entry for the root
102  * dir in the root dir
103  */
104 #define	MSDOSFSROOT_OFS	0x1fffffff
105 
106 /*
107  * The FAT cache structure. fc_fsrcn is the filesystem relative cluster
108  * number that corresponds to the file relative cluster number in this
109  * structure (fc_frcn).
110  */
111 struct fatcache {
112 	u_long fc_frcn;		/* file relative cluster number */
113 	u_long fc_fsrcn;	/* filesystem relative cluster number */
114 };
115 
116 /*
117  * The FAT entry cache as it stands helps make extending files a "quick"
118  * operation by avoiding having to scan the FAT to discover the last
119  * cluster of the file. The cache also helps sequential reads by
120  * remembering the last cluster read from the file.  This also prevents us
121  * from having to rescan the FAT to find the next cluster to read.  This
122  * cache is probably pretty worthless if a file is opened by multiple
123  * processes.
124  */
125 #define	FC_SIZE		3	/* number of entries in the cache */
126 #define	FC_LASTMAP	0	/* entry the last call to pcbmap() resolved
127 				 * to */
128 #define	FC_LASTFC	1	/* entry for the last cluster in the file */
129 #define	FC_NEXTTOLASTFC	2	/* entry for a close to the last cluster in
130 				 * the file */
131 
132 #define	FCE_EMPTY	0xffffffff	/* doesn't represent an actual cluster # */
133 
134 /*
135  * Set a slot in the FAT cache.
136  */
137 #define	fc_setcache(dep, slot, frcn, fsrcn) \
138 	(dep)->de_fc[(slot)].fc_frcn = (frcn); \
139 	(dep)->de_fc[(slot)].fc_fsrcn = (fsrcn);
140 
141 /*
142  * This is the in memory variant of a dos directory entry.  It is usually
143  * contained within a vnode.
144  */
145 struct denode {
146 	struct vnode *de_vnode;	/* addr of vnode we are part of */
147 	struct vn_clusterw de_clusterw;	/* buffer clustering information */
148 	u_long de_flag;		/* flag bits */
149 	u_long de_dirclust;	/* cluster of the directory file containing this entry */
150 	u_long de_diroffset;	/* offset of this entry in the directory cluster */
151 	u_long de_fndoffset;	/* offset of found dir entry */
152 	int de_fndcnt;		/* number of slots before de_fndoffset */
153 	long de_refcnt;		/* reference count */
154 	struct msdosfsmount *de_pmp;	/* addr of our mount struct */
155 	u_char de_Name[12];	/* name, from DOS directory entry */
156 	u_char de_Attributes;	/* attributes, from directory entry */
157 	u_char de_LowerCase;	/* NT VFAT lower case flags */
158 	u_char de_CHun;		/* Hundredth of second of CTime*/
159 	u_short de_CTime;	/* creation time */
160 	u_short de_CDate;	/* creation date */
161 	u_short de_ADate;	/* access date */
162 	u_short de_MTime;	/* modification time */
163 	u_short de_MDate;	/* modification date */
164 	u_long de_StartCluster; /* starting cluster of file */
165 	u_long de_FileSize;	/* size of file in bytes */
166 	struct fatcache de_fc[FC_SIZE];	/* FAT cache */
167 	u_quad_t de_modrev;	/* Revision level for lease. */
168 	uint64_t de_inode;	/* Inode number (really byte offset of direntry) */
169 };
170 
171 /*
172  * Values for the de_flag field of the denode.
173  */
174 #define	DE_UPDATE	0x0004	/* Modification time update request */
175 #define	DE_CREATE	0x0008	/* Creation time update */
176 #define	DE_ACCESS	0x0010	/* Access time update */
177 #define	DE_MODIFIED	0x0020	/* Denode has been modified */
178 #define	DE_RENAME	0x0040	/* Denode is in the process of being renamed */
179 
180 /* Maximum size of a file on a FAT filesystem */
181 #define MSDOSFS_FILESIZE_MAX	0xFFFFFFFFLL
182 
183 /*
184  * Transfer directory entries between internal and external form.
185  * dep is a struct denode * (internal form),
186  * dp is a struct direntry * (external form).
187  */
188 #define DE_INTERNALIZE32(dep, dp)			\
189 	 ((dep)->de_StartCluster |= getushort((dp)->deHighClust) << 16)
190 #define DE_INTERNALIZE(dep, dp)				\
191 	(memcpy((dep)->de_Name, (dp)->deName, 11),	\
192 	 (dep)->de_Attributes = (dp)->deAttributes,	\
193 	 (dep)->de_LowerCase = (dp)->deLowerCase,	\
194 	 (dep)->de_CHun = (dp)->deCHundredth,		\
195 	 (dep)->de_CTime = getushort((dp)->deCTime),	\
196 	 (dep)->de_CDate = getushort((dp)->deCDate),	\
197 	 (dep)->de_ADate = getushort((dp)->deADate),	\
198 	 (dep)->de_MTime = getushort((dp)->deMTime),	\
199 	 (dep)->de_MDate = getushort((dp)->deMDate),	\
200 	 (dep)->de_StartCluster = getushort((dp)->deStartCluster), \
201 	 (dep)->de_FileSize = getulong((dp)->deFileSize), \
202 	 (FAT32((dep)->de_pmp) ? DE_INTERNALIZE32((dep), (dp)) : 0))
203 
204 #define DE_EXTERNALIZE(dp, dep)				\
205 	(memcpy((dp)->deName, (dep)->de_Name, 11),	\
206 	 (dp)->deAttributes = (dep)->de_Attributes,	\
207 	 (dp)->deLowerCase = (dep)->de_LowerCase,	\
208 	 (dp)->deCHundredth = (dep)->de_CHun,		\
209 	 putushort((dp)->deCTime, (dep)->de_CTime),	\
210 	 putushort((dp)->deCDate, (dep)->de_CDate),	\
211 	 putushort((dp)->deADate, (dep)->de_ADate),	\
212 	 putushort((dp)->deMTime, (dep)->de_MTime),	\
213 	 putushort((dp)->deMDate, (dep)->de_MDate),	\
214 	 putushort((dp)->deStartCluster, (dep)->de_StartCluster), \
215 	 putulong((dp)->deFileSize,			\
216 	     ((dep)->de_Attributes & ATTR_DIRECTORY) ? 0 : (dep)->de_FileSize), \
217 	 putushort((dp)->deHighClust, (dep)->de_StartCluster >> 16))
218 
219 #if defined(_KERNEL) || defined(MAKEFS)
220 
221 #define	VTODE(vp)	((struct denode *)(vp)->v_data)
222 #define	DETOV(de)	((de)->de_vnode)
223 
224 #define	DETIMES(dep, acc, mod, cre) do {				\
225 	if ((dep)->de_flag & DE_UPDATE) {				\
226 		(dep)->de_flag |= DE_MODIFIED;				\
227 		timespec2fattime((mod), 0, &(dep)->de_MDate,		\
228 		    &(dep)->de_MTime, NULL);				\
229 		(dep)->de_Attributes |= ATTR_ARCHIVE;			\
230 	}								\
231 	if ((dep)->de_pmp->pm_flags & MSDOSFSMNT_NOWIN95) {		\
232 		(dep)->de_flag &= ~(DE_UPDATE | DE_CREATE | DE_ACCESS);	\
233 		break;							\
234 	}								\
235 	if ((dep)->de_flag & DE_ACCESS) {				\
236 		uint16_t adate;						\
237 									\
238 		timespec2fattime((acc), 0, &adate, NULL, NULL);		\
239 		if (adate != (dep)->de_ADate) {				\
240 			(dep)->de_flag |= DE_MODIFIED;			\
241 			(dep)->de_ADate = adate;			\
242 		}							\
243 	}								\
244 	if ((dep)->de_flag & DE_CREATE) {				\
245 		timespec2fattime((cre), 0, &(dep)->de_CDate,		\
246 		    &(dep)->de_CTime, &(dep)->de_CHun);			\
247 		(dep)->de_flag |= DE_MODIFIED;				\
248 	}								\
249 	(dep)->de_flag &= ~(DE_UPDATE | DE_CREATE | DE_ACCESS);		\
250 } while (0)
251 
252 /*
253  * This overlays the fid structure (see mount.h)
254  */
255 struct defid {
256 	u_short defid_len;	/* length of structure */
257 	u_short defid_pad;	/* force long alignment */
258 
259 	uint32_t defid_dirclust; /* cluster this dir entry came from */
260 	uint32_t defid_dirofs;	/* offset of entry within the cluster */
261 #if 0
262 	uint32_t defid_gen;	/* generation number */
263 #endif
264 };
265 
266 extern struct vop_vector msdosfs_vnodeops;
267 
268 #ifdef _KERNEL
269 int msdosfs_lookup(struct vop_cachedlookup_args *);
270 int msdosfs_inactive(struct vop_inactive_args *);
271 int msdosfs_reclaim(struct vop_reclaim_args *);
272 #endif
273 
274 /*
275  * Internal service routine prototypes.
276  */
277 struct componentname;
278 int deget(struct msdosfsmount *, u_long, u_long, struct denode **);
279 int uniqdosname(struct denode *, struct componentname *, u_char *);
280 
281 int readep(struct msdosfsmount *pmp, u_long dirclu, u_long dirofs,  struct buf **bpp, struct direntry **epp);
282 int readde(struct denode *dep, struct buf **bpp, struct direntry **epp);
283 int deextend(struct denode *dep, u_long length, struct ucred *cred);
284 int fillinusemap(struct msdosfsmount *pmp);
285 void reinsert(struct denode *dep);
286 int dosdirempty(struct denode *dep);
287 int createde(struct denode *dep, struct denode *ddep, struct denode **depp, struct componentname *cnp);
288 int deupdat(struct denode *dep, int waitfor);
289 int removede(struct denode *pdep, struct denode *dep);
290 int detrunc(struct denode *dep, u_long length, int flags, struct ucred *cred);
291 int doscheckpath( struct denode *source, struct denode *target);
292 #endif	/* _KERNEL || MAKEFS */
293 #endif	/* !_FS_MSDOSFS_DENODE_H_ */
294