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