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