1*8086f46eSthorpej /* $NetBSD: denode.h,v 1.29 2021/10/23 16:58:17 thorpej Exp $ */ 298d58548Sjdolecek 398d58548Sjdolecek /*- 498d58548Sjdolecek * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank. 598d58548Sjdolecek * Copyright (C) 1994, 1995, 1997 TooLs GmbH. 698d58548Sjdolecek * All rights reserved. 798d58548Sjdolecek * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below). 898d58548Sjdolecek * 998d58548Sjdolecek * Redistribution and use in source and binary forms, with or without 1098d58548Sjdolecek * modification, are permitted provided that the following conditions 1198d58548Sjdolecek * are met: 1298d58548Sjdolecek * 1. Redistributions of source code must retain the above copyright 1398d58548Sjdolecek * notice, this list of conditions and the following disclaimer. 1498d58548Sjdolecek * 2. Redistributions in binary form must reproduce the above copyright 1598d58548Sjdolecek * notice, this list of conditions and the following disclaimer in the 1698d58548Sjdolecek * documentation and/or other materials provided with the distribution. 1798d58548Sjdolecek * 3. All advertising materials mentioning features or use of this software 1898d58548Sjdolecek * must display the following acknowledgement: 1998d58548Sjdolecek * This product includes software developed by TooLs GmbH. 2098d58548Sjdolecek * 4. The name of TooLs GmbH may not be used to endorse or promote products 2198d58548Sjdolecek * derived from this software without specific prior written permission. 2298d58548Sjdolecek * 2398d58548Sjdolecek * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 2498d58548Sjdolecek * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 2598d58548Sjdolecek * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2698d58548Sjdolecek * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2798d58548Sjdolecek * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 2898d58548Sjdolecek * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 2998d58548Sjdolecek * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 3098d58548Sjdolecek * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 3198d58548Sjdolecek * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 3298d58548Sjdolecek * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3398d58548Sjdolecek */ 3498d58548Sjdolecek /* 3598d58548Sjdolecek * Written by Paul Popelka (paulp@uts.amdahl.com) 3698d58548Sjdolecek * 3798d58548Sjdolecek * You can do anything you want with this software, just don't say you wrote 3898d58548Sjdolecek * it, and don't remove this notice. 3998d58548Sjdolecek * 4098d58548Sjdolecek * This software is provided "as is". 4198d58548Sjdolecek * 4298d58548Sjdolecek * The author supplies this software to be publicly redistributed on the 4398d58548Sjdolecek * understanding that the author is not responsible for the correct 4498d58548Sjdolecek * functioning of this software in any circumstances and is not liable for 4598d58548Sjdolecek * any damages caused by this software. 4698d58548Sjdolecek * 4798d58548Sjdolecek * October 1992 4898d58548Sjdolecek */ 494bffed72Schristos #ifndef _MSDOSFS_DENODE_H_ 504bffed72Schristos #define _MSDOSFS_DENODE_H_ 5198d58548Sjdolecek 521ed2e30dSchristos #ifndef MAKEFS 5398d58548Sjdolecek #include <miscfs/genfs/genfs_node.h> 54691bcd5dSchristos #else 55691bcd5dSchristos struct genfs_node { 56691bcd5dSchristos }; 57691bcd5dSchristos struct vnode; 58691bcd5dSchristos struct msdosfsmount; 59691bcd5dSchristos struct buf; 60691bcd5dSchristos #endif 6198d58548Sjdolecek 6298d58548Sjdolecek /* 6398d58548Sjdolecek * This is the pc filesystem specific portion of the vnode structure. 6498d58548Sjdolecek * 6598d58548Sjdolecek * To describe a file uniquely the de_dirclust, de_diroffset, and 6698d58548Sjdolecek * de_StartCluster fields are used. 6798d58548Sjdolecek * 6898d58548Sjdolecek * de_dirclust contains the cluster number of the directory cluster 6998d58548Sjdolecek * containing the entry for a file or directory. 7098d58548Sjdolecek * de_diroffset is the index into the cluster for the entry describing 7198d58548Sjdolecek * a file or directory. 7298d58548Sjdolecek * de_StartCluster is the number of the first cluster of the file or directory. 7398d58548Sjdolecek * 7498d58548Sjdolecek * Now to describe the quirks of the pc filesystem. 7598d58548Sjdolecek * - Clusters 0 and 1 are reserved. 7698d58548Sjdolecek * - The first allocatable cluster is 2. 7798d58548Sjdolecek * - The root directory is of fixed size and all blocks that make it up 7898d58548Sjdolecek * are contiguous. 7998d58548Sjdolecek * - Cluster 0 refers to the root directory when it is found in the 8098d58548Sjdolecek * startcluster field of a directory entry that points to another directory. 8198d58548Sjdolecek * - Cluster 0 implies a 0 length file when found in the start cluster field 8298d58548Sjdolecek * of a directory entry that points to a file. 8398d58548Sjdolecek * - You can't use the cluster number 0 to derive the address of the root 8498d58548Sjdolecek * directory. 8598d58548Sjdolecek * - Multiple directory entries can point to a directory. The entry in the 8698d58548Sjdolecek * parent directory points to a child directory. Any directories in the 8798d58548Sjdolecek * child directory contain a ".." entry that points back to the parent. 8898d58548Sjdolecek * The child directory itself contains a "." entry that points to itself. 8998d58548Sjdolecek * - The root directory does not contain a "." or ".." entry. 9098d58548Sjdolecek * - Directory entries for directories are never changed once they are created 9198d58548Sjdolecek * (except when removed). The size stays 0, and the last modification time 9298d58548Sjdolecek * is never changed. This is because so many directory entries can point to 9398d58548Sjdolecek * the physical clusters that make up a directory. It would lead to an 9498d58548Sjdolecek * update nightmare. 9598d58548Sjdolecek * - The length field in a directory entry pointing to a directory contains 0 9698d58548Sjdolecek * (always). The only way to find the end of a directory is to follow the 9798d58548Sjdolecek * cluster chain until the "last cluster" marker is found. 9898d58548Sjdolecek * 9998d58548Sjdolecek * My extensions to make this house of cards work. These apply only to the in 10098d58548Sjdolecek * memory copy of the directory entry. 10198d58548Sjdolecek * - A reference count for each denode will be kept since dos doesn't keep such 10298d58548Sjdolecek * things. 10398d58548Sjdolecek */ 10498d58548Sjdolecek 10598d58548Sjdolecek /* 10698d58548Sjdolecek * Internal pseudo-offset for (nonexistent) directory entry for the root 10798d58548Sjdolecek * dir in the root dir 10898d58548Sjdolecek */ 10998d58548Sjdolecek #define MSDOSFSROOT_OFS 0x1fffffff 11098d58548Sjdolecek 11198d58548Sjdolecek /* 11248958e52Sjakllsch * The FAT cache structure. fc_fsrcn is the filesystem relative cluster 11398d58548Sjdolecek * number that corresponds to the file relative cluster number in this 11498d58548Sjdolecek * structure (fc_frcn). 11598d58548Sjdolecek */ 11698d58548Sjdolecek struct fatcache { 11798d58548Sjdolecek u_long fc_frcn; /* file relative cluster number */ 11898d58548Sjdolecek u_long fc_fsrcn; /* filesystem relative cluster number */ 11998d58548Sjdolecek }; 12098d58548Sjdolecek 12198d58548Sjdolecek /* 12248958e52Sjakllsch * The FAT entry cache as it stands helps make extending files a "quick" 12348958e52Sjakllsch * operation by avoiding having to scan the FAT to discover the last 12498d58548Sjdolecek * cluster of the file. The cache also helps sequential reads by 12598d58548Sjdolecek * remembering the last cluster read from the file. This also prevents us 12648958e52Sjakllsch * from having to rescan the FAT to find the next cluster to read. This 12798d58548Sjdolecek * cache is probably pretty worthless if a file is opened by multiple 12898d58548Sjdolecek * processes. 12998d58548Sjdolecek */ 130729d0a01Sxtraeme #define FC_SIZE 3 /* number of entries in the cache */ 13198d58548Sjdolecek #define FC_LASTMAP 0 /* entry the last call to pcbmap() resolved 13298d58548Sjdolecek * to */ 13398d58548Sjdolecek #define FC_LASTFC 1 /* entry for the last cluster in the file */ 134729d0a01Sxtraeme #define FC_NEXTTOLASTFC 2 /* entry for a close to the last cluster in the file */ 13598d58548Sjdolecek 13698d58548Sjdolecek #define FCE_EMPTY 0xffffffff /* doesn't represent an actual cluster # */ 13798d58548Sjdolecek 13898d58548Sjdolecek /* 13948958e52Sjakllsch * Set a slot in the FAT cache. 14098d58548Sjdolecek */ 14198d58548Sjdolecek #define fc_setcache(dep, slot, frcn, fsrcn) \ 14298d58548Sjdolecek (dep)->de_fc[slot].fc_frcn = frcn; \ 14398d58548Sjdolecek (dep)->de_fc[slot].fc_fsrcn = fsrcn; 14498d58548Sjdolecek 145729d0a01Sxtraeme #define fc_last_to_nexttolast(dep) \ 146729d0a01Sxtraeme do { \ 147729d0a01Sxtraeme (dep)->de_fc[FC_NEXTTOLASTFC].fc_frcn = (dep)->de_fc[FC_LASTFC].fc_frcn; \ 148729d0a01Sxtraeme (dep)->de_fc[FC_NEXTTOLASTFC].fc_fsrcn = (dep)->de_fc[FC_LASTFC].fc_fsrcn; \ 149729d0a01Sxtraeme } while (0) 150729d0a01Sxtraeme 15125fae635Shannken /* 15225fae635Shannken * Auxiliary results from an msdosfs_lookup operation 15325fae635Shannken */ 15425fae635Shannken struct msdosfs_lookup_results { 15525fae635Shannken u_long mlr_fndoffset; /* offset of found dir entry */ 15625fae635Shannken int mlr_fndcnt; /* number of slots before de_fndoffset */ 15725fae635Shannken }; 158729d0a01Sxtraeme 15998d58548Sjdolecek /* 16098d58548Sjdolecek * This is the in memory variant of a dos directory entry. It is usually 16198d58548Sjdolecek * contained within a vnode. 16298d58548Sjdolecek */ 163798256c9Shannken struct denode_key { 164798256c9Shannken u_long dk_dirclust; /* cluster of the directory file containing this entry */ 165798256c9Shannken u_long dk_diroffset; /* offset of this entry in the directory cluster */ 166798256c9Shannken void *dk_dirgen; /* non zero and unique for unlinked nodes */ 167798256c9Shannken }; 16898d58548Sjdolecek struct denode { 16998d58548Sjdolecek struct genfs_node de_gnode; 17098d58548Sjdolecek struct vnode *de_vnode; /* addr of vnode we are part of */ 17198d58548Sjdolecek struct vnode *de_devvp; /* vnode of blk dev we live on */ 17298d58548Sjdolecek u_long de_flag; /* flag bits */ 17398d58548Sjdolecek dev_t de_dev; /* device where direntry lives */ 174798256c9Shannken struct denode_key de_key; 175798256c9Shannken #define de_dirclust de_key.dk_dirclust 176798256c9Shannken #define de_diroffset de_key.dk_diroffset 177798256c9Shannken #define de_dirgen de_key.dk_dirgen 17825fae635Shannken struct msdosfs_lookup_results de_crap; /* results from lookup */ 17998d58548Sjdolecek long de_refcnt; /* reference count */ 18098d58548Sjdolecek struct msdosfsmount *de_pmp; /* addr of our mount struct */ 18198d58548Sjdolecek struct lockf *de_lockf; /* byte level lock list */ 18298d58548Sjdolecek u_char de_Name[12]; /* name, from DOS directory entry */ 18398d58548Sjdolecek u_char de_Attributes; /* attributes, from directory entry */ 18498d58548Sjdolecek u_char de_CHun; /* Hundredth of second of CTime*/ 18598d58548Sjdolecek u_short de_CTime; /* creation time */ 18698d58548Sjdolecek u_short de_CDate; /* creation date */ 18798d58548Sjdolecek u_short de_ADate; /* access date */ 18898d58548Sjdolecek u_short de_MTime; /* modification time */ 18998d58548Sjdolecek u_short de_MDate; /* modification date */ 19098d58548Sjdolecek u_long de_StartCluster; /* starting cluster of file */ 19198d58548Sjdolecek u_long de_FileSize; /* size of file in bytes */ 19248958e52Sjakllsch struct fatcache de_fc[FC_SIZE]; /* FAT cache */ 19398d58548Sjdolecek }; 19498d58548Sjdolecek 19598d58548Sjdolecek /* 19698d58548Sjdolecek * Values for the de_flag field of the denode. 19798d58548Sjdolecek */ 19898d58548Sjdolecek #define DE_UPDATE 0x0001 /* Modification time update request. */ 19998d58548Sjdolecek #define DE_CREATE 0x0002 /* Creation time update */ 20098d58548Sjdolecek #define DE_ACCESS 0x0004 /* Access time update */ 20198d58548Sjdolecek #define DE_MODIFIED 0x0008 /* Denode has been modified. */ 20298d58548Sjdolecek #define DE_RENAME 0x0010 /* Denode is in the process of being renamed */ 20398d58548Sjdolecek 20498d58548Sjdolecek /* 20598d58548Sjdolecek * Maximum filename length in Win95 20698d58548Sjdolecek * Note: Must be < sizeof(dirent.d_name) 20798d58548Sjdolecek */ 20898d58548Sjdolecek #define WIN_MAXLEN 255 20998d58548Sjdolecek 210529eaccbSxtraeme /* Maximum size of a file on a FAT filesystem */ 211529eaccbSxtraeme #define MSDOSFS_FILESIZE_MAX 0xFFFFFFFFLL 212529eaccbSxtraeme 21398d58548Sjdolecek /* 21498d58548Sjdolecek * Transfer directory entries between internal and external form. 21598d58548Sjdolecek * dep is a struct denode * (internal form), 21698d58548Sjdolecek * dp is a struct direntry * (external form). 21798d58548Sjdolecek */ 21898d58548Sjdolecek #define DE_INTERNALIZE32(dep, dp) \ 21998d58548Sjdolecek ((dep)->de_StartCluster |= getushort((dp)->deHighClust) << 16) 22098d58548Sjdolecek #define DE_INTERNALIZE(dep, dp) \ 221be48413aSmaya (memcpy((dep)->de_Name, (dp)->deName, 8), \ 222be48413aSmaya memcpy((dep)->de_Name+8, (dp)->deExtension, 3),\ 22398d58548Sjdolecek (dep)->de_Attributes = (dp)->deAttributes, \ 22498d58548Sjdolecek (dep)->de_CHun = (dp)->deCHundredth, \ 22598d58548Sjdolecek (dep)->de_CTime = getushort((dp)->deCTime), \ 22698d58548Sjdolecek (dep)->de_CDate = getushort((dp)->deCDate), \ 22798d58548Sjdolecek (dep)->de_ADate = getushort((dp)->deADate), \ 22898d58548Sjdolecek (dep)->de_MTime = getushort((dp)->deMTime), \ 22998d58548Sjdolecek (dep)->de_MDate = getushort((dp)->deMDate), \ 23098d58548Sjdolecek (dep)->de_StartCluster = getushort((dp)->deStartCluster), \ 23198d58548Sjdolecek (dep)->de_FileSize = getulong((dp)->deFileSize), \ 23298d58548Sjdolecek (FAT32((dep)->de_pmp) ? DE_INTERNALIZE32((dep), (dp)) : 0)) 23398d58548Sjdolecek 23498d58548Sjdolecek #define DE_EXTERNALIZE32(dp, dep) \ 23598d58548Sjdolecek putushort((dp)->deHighClust, (dep)->de_StartCluster >> 16) 236fe1de4d6Schristos #define DE_EXTERNALIZE16(dp, dep) \ 237fe1de4d6Schristos putushort((dp)->deHighClust, 0) 23898d58548Sjdolecek #define DE_EXTERNALIZE(dp, dep) \ 239be48413aSmaya (memcpy((dp)->deName, (dep)->de_Name, 8), \ 240be48413aSmaya memcpy((dp)->deExtension, (dep)->de_Name+8, 3),\ 24198d58548Sjdolecek (dp)->deAttributes = (dep)->de_Attributes, \ 24298d58548Sjdolecek (dp)->deCHundredth = (dep)->de_CHun, \ 24398d58548Sjdolecek putushort((dp)->deCTime, (dep)->de_CTime), \ 24498d58548Sjdolecek putushort((dp)->deCDate, (dep)->de_CDate), \ 24598d58548Sjdolecek putushort((dp)->deADate, (dep)->de_ADate), \ 24698d58548Sjdolecek putushort((dp)->deMTime, (dep)->de_MTime), \ 24798d58548Sjdolecek putushort((dp)->deMDate, (dep)->de_MDate), \ 24898d58548Sjdolecek putushort((dp)->deStartCluster, (dep)->de_StartCluster), \ 24998d58548Sjdolecek putulong((dp)->deFileSize, \ 25098d58548Sjdolecek ((dep)->de_Attributes & ATTR_DIRECTORY) ? 0 : (dep)->de_FileSize), \ 251fe1de4d6Schristos (FAT32((dep)->de_pmp) ? DE_EXTERNALIZE32((dp), (dep)) : DE_EXTERNALIZE16((dp), (dep)))) 25298d58548Sjdolecek 25398d58548Sjdolecek #define de_forw de_chain[0] 25498d58548Sjdolecek #define de_back de_chain[1] 25598d58548Sjdolecek 256929f8943Schristos #if defined(_KERNEL) || defined(MAKEFS) 25798d58548Sjdolecek 25898d58548Sjdolecek #define VTODE(vp) ((struct denode *)(vp)->v_data) 25998d58548Sjdolecek #define DETOV(de) ((de)->de_vnode) 26098d58548Sjdolecek 261ce112dfcSitojun #define DETIMES(dep, acc, mod, cre, gmtoff) \ 262a12024daSchristos while ((dep)->de_flag & (DE_UPDATE | DE_CREATE | DE_ACCESS)) \ 263a12024daSchristos msdosfs_detimes(dep, acc, mod, cre, gmtoff) 26498d58548Sjdolecek 26598d58548Sjdolecek /* 266bc8224a1Spooka * This overlays the fid structure (see fstypes.h) 26798d58548Sjdolecek */ 26898d58548Sjdolecek struct defid { 26998d58548Sjdolecek u_int16_t defid_len; /* length of structure */ 27098d58548Sjdolecek u_int16_t defid_pad; /* force 4-byte alignment */ 27198d58548Sjdolecek 27298d58548Sjdolecek u_int32_t defid_dirclust; /* cluster this dir entry came from */ 27398d58548Sjdolecek u_int32_t defid_dirofs; /* offset of entry within the cluster */ 27498d58548Sjdolecek u_int32_t defid_gen; /* generation number */ 27598d58548Sjdolecek }; 27698d58548Sjdolecek 27798d58548Sjdolecek /* 27898d58548Sjdolecek * Prototypes for MSDOSFS vnode operations 27998d58548Sjdolecek */ 28047216f84Sxtraeme int msdosfs_lookup (void *); 28147216f84Sxtraeme int msdosfs_create (void *); 28247216f84Sxtraeme int msdosfs_close (void *); 28347216f84Sxtraeme int msdosfs_access (void *); 28447216f84Sxtraeme int msdosfs_getattr (void *); 28547216f84Sxtraeme int msdosfs_setattr (void *); 28647216f84Sxtraeme int msdosfs_read (void *); 28747216f84Sxtraeme int msdosfs_write (void *); 288a748ea88Syamt int msdosfs_fsync (void *); 28947216f84Sxtraeme int msdosfs_remove (void *); 29047216f84Sxtraeme int msdosfs_rename (void *); 29147216f84Sxtraeme int msdosfs_mkdir (void *); 29247216f84Sxtraeme int msdosfs_rmdir (void *); 29347216f84Sxtraeme int msdosfs_readdir (void *); 29447216f84Sxtraeme int msdosfs_inactive (void *); 29547216f84Sxtraeme int msdosfs_reclaim (void *); 29647216f84Sxtraeme int msdosfs_bmap (void *); 29747216f84Sxtraeme int msdosfs_strategy (void *); 29847216f84Sxtraeme int msdosfs_print (void *); 29947216f84Sxtraeme int msdosfs_advlock (void *); 30047216f84Sxtraeme int msdosfs_pathconf (void *); 30198d58548Sjdolecek 30298d58548Sjdolecek /* 30398d58548Sjdolecek * Internal service routine prototypes. 30498d58548Sjdolecek */ 305929f8943Schristos struct componentname; 306929f8943Schristos struct direntry; 307691bcd5dSchristos struct kauth_cred; 308a748ea88Syamt int msdosfs_update(struct vnode *, const struct timespec *, 309a748ea88Syamt const struct timespec *, int); 310*8086f46eSthorpej int msdosfs_createde(struct denode *, struct denode *, 31125fae635Shannken const struct msdosfs_lookup_results *, 31247216f84Sxtraeme struct denode **, struct componentname *); 313*8086f46eSthorpej int msdosfs_deextend(struct denode *, u_long, struct kauth_cred *); 314798256c9Shannken #ifdef MAKEFS 315*8086f46eSthorpej int msdosfs_deget(struct msdosfsmount *, u_long, u_long, struct denode **); 316798256c9Shannken #else 317*8086f46eSthorpej int msdosfs_deget(struct msdosfsmount *, u_long, u_long, struct vnode **); 318798256c9Shannken #endif 319*8086f46eSthorpej int msdosfs_detrunc(struct denode *, u_long, int, struct kauth_cred *); 320*8086f46eSthorpej int msdosfs_deupdat(struct denode *, int); 321*8086f46eSthorpej int msdosfs_dosdirempty(struct denode *); 322*8086f46eSthorpej int msdosfs_readde(struct denode *, struct buf **, struct direntry **); 323*8086f46eSthorpej int msdosfs_readep(struct msdosfsmount *, u_long, u_long, 32447216f84Sxtraeme struct buf **, struct direntry **); 325*8086f46eSthorpej int msdosfs_removede(struct denode *, struct denode *, 32625fae635Shannken const struct msdosfs_lookup_results *); 327*8086f46eSthorpej int msdosfs_uniqdosname(struct denode *, struct componentname *, u_char *); 328*8086f46eSthorpej int msdosfs_findwin95(struct denode *); 329*8086f46eSthorpej int msdosfs_gop_alloc(struct vnode *, off_t, off_t, int, 330*8086f46eSthorpej struct kauth_cred *); 33147216f84Sxtraeme void msdosfs_gop_markupdate(struct vnode *, int); 332a12024daSchristos void msdosfs_detimes(struct denode *, const struct timespec *, 333a12024daSchristos const struct timespec *, const struct timespec *, int); 334fff1c84cShannken int msdosfs_fh_enter(struct msdosfsmount *, uint32_t, uint32_t, uint32_t *); 335fff1c84cShannken int msdosfs_fh_remove(struct msdosfsmount *, uint32_t, uint32_t); 336*8086f46eSthorpej int msdosfs_fh_lookup(struct msdosfsmount *, uint32_t, uint32_t, 337*8086f46eSthorpej uint32_t *); 338fff1c84cShannken void msdosfs_fh_destroy(struct msdosfsmount *); 339929f8943Schristos #endif /* _KERNEL || MAKEFS */ 3404bffed72Schristos #endif /* _MSDOSFS_DENODE_H_ */ 341