/* $NetBSD: denode.h,v 1.25 1997/11/17 15:36:28 ws Exp $ */12/*-3* SPDX-License-Identifier: BSD-4-Clause4*5* Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.6* Copyright (C) 1994, 1995, 1997 TooLs GmbH.7* All rights reserved.8* Original code by Paul Popelka ([email protected]) (see below).9*10* Redistribution and use in source and binary forms, with or without11* modification, are permitted provided that the following conditions12* are met:13* 1. Redistributions of source code must retain the above copyright14* notice, this list of conditions and the following disclaimer.15* 2. Redistributions in binary form must reproduce the above copyright16* notice, this list of conditions and the following disclaimer in the17* documentation and/or other materials provided with the distribution.18* 3. All advertising materials mentioning features or use of this software19* must display the following acknowledgement:20* This product includes software developed by TooLs GmbH.21* 4. The name of TooLs GmbH may not be used to endorse or promote products22* derived from this software without specific prior written permission.23*24* THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR25* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES26* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.27* IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,28* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,29* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;30* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,31* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR32* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF33* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.34*/35/*-36* Written by Paul Popelka ([email protected])37*38* You can do anything you want with this software, just don't say you wrote39* it, and don't remove this notice.40*41* This software is provided "as is".42*43* The author supplies this software to be publicly redistributed on the44* understanding that the author is not responsible for the correct45* functioning of this software in any circumstances and is not liable for46* any damages caused by this software.47*48* October 199249*/50#ifndef _FS_MSDOSFS_DENODE_H_51#define _FS_MSDOSFS_DENODE_H_5253/*54* This is the pc filesystem specific portion of the vnode structure.55*56* To describe a file uniquely the de_dirclust, de_diroffset, and57* de_StartCluster fields are used.58*59* de_dirclust contains the cluster number of the directory cluster60* containing the entry for a file or directory.61* de_diroffset is the index into the cluster for the entry describing62* a file or directory.63* de_StartCluster is the number of the first cluster of the file or directory.64*65* Now to describe the quirks of the pc filesystem.66* - Clusters 0 and 1 are reserved.67* - The first allocatable cluster is 2.68* - The root directory is of fixed size and all blocks that make it up69* are contiguous.70* - Cluster 0 refers to the root directory when it is found in the71* startcluster field of a directory entry that points to another directory.72* - Cluster 0 implies a 0 length file when found in the start cluster field73* of a directory entry that points to a file.74* - You can't use the cluster number 0 to derive the address of the root75* directory.76* - Multiple directory entries can point to a directory. The entry in the77* parent directory points to a child directory. Any directories in the78* child directory contain a ".." entry that points back to the parent.79* The child directory itself contains a "." entry that points to itself.80* - The root directory does not contain a "." or ".." entry.81* - Directory entries for directories are never changed once they are created82* (except when removed). The size stays 0, and the last modification time83* is never changed. This is because so many directory entries can point to84* the physical clusters that make up a directory. It would lead to an85* update nightmare.86* - The length field in a directory entry pointing to a directory contains 087* (always). The only way to find the end of a directory is to follow the88* cluster chain until the "last cluster" marker is found.89*90* My extensions to make this house of cards work. These apply only to the in91* memory copy of the directory entry.92* - A reference count for each denode will be kept since dos doesn't keep such93* things.94*/9596/*97* Internal pseudo-offset for (nonexistent) directory entry for the root98* dir in the root dir99*/100#define MSDOSFSROOT_OFS 0x1fffffff101102/*103* The FAT cache structure. fc_fsrcn is the filesystem relative cluster104* number that corresponds to the file relative cluster number in this105* structure (fc_frcn).106*/107struct fatcache {108u_long fc_frcn; /* file relative cluster number */109u_long fc_fsrcn; /* filesystem relative cluster number */110};111112/*113* The FAT entry cache as it stands helps make extending files a "quick"114* operation by avoiding having to scan the FAT to discover the last115* cluster of the file. The cache also helps sequential reads by116* remembering the last cluster read from the file. This also prevents us117* from having to rescan the FAT to find the next cluster to read. This118* cache is probably pretty worthless if a file is opened by multiple119* processes.120*/121#define FC_SIZE 3 /* number of entries in the cache */122#define FC_LASTMAP 0 /* entry the last call to pcbmap() resolved123* to */124#define FC_LASTFC 1 /* entry for the last cluster in the file */125#define FC_NEXTTOLASTFC 2 /* entry for a close to the last cluster in126* the file */127128#define FCE_EMPTY 0xffffffff /* doesn't represent an actual cluster # */129130/*131* Set a slot in the FAT cache.132*/133#define fc_setcache(dep, slot, frcn, fsrcn) \134(dep)->de_fc[(slot)].fc_frcn = (frcn); \135(dep)->de_fc[(slot)].fc_fsrcn = (fsrcn);136137/*138* This is the in memory variant of a dos directory entry. It is usually139* contained within a vnode.140*/141struct denode {142struct vnode *de_vnode; /* addr of vnode we are part of */143struct vn_clusterw de_clusterw; /* buffer clustering information */144u_long de_flag; /* flag bits */145u_long de_dirclust; /* cluster of the directory file containing this entry */146u_long de_diroffset; /* offset of this entry in the directory cluster */147u_long de_fndoffset; /* offset of found dir entry */148int de_fndcnt; /* number of slots before de_fndoffset */149long de_refcnt; /* reference count */150struct msdosfsmount *de_pmp; /* addr of our mount struct */151u_char de_Name[12]; /* name, from DOS directory entry */152u_char de_Attributes; /* attributes, from directory entry */153u_char de_LowerCase; /* NT VFAT lower case flags */154u_char de_CHun; /* Hundredth of second of CTime*/155u_short de_CTime; /* creation time */156u_short de_CDate; /* creation date */157u_short de_ADate; /* access date */158u_short de_MTime; /* modification time */159u_short de_MDate; /* modification date */160u_long de_StartCluster; /* starting cluster of file */161u_long de_FileSize; /* size of file in bytes */162struct fatcache de_fc[FC_SIZE]; /* FAT cache */163u_quad_t de_modrev; /* Revision level for lease. */164uint64_t de_inode; /* Inode number (really index of DOS style direntry) */165};166167/*168* Values for the de_flag field of the denode.169*/170#define DE_UPDATE 0x0004 /* Modification time update request */171#define DE_CREATE 0x0008 /* Creation time update */172#define DE_ACCESS 0x0010 /* Access time update */173#define DE_MODIFIED 0x0020 /* Denode has been modified */174175/* Maximum size of a file on a FAT filesystem */176#define MSDOSFS_FILESIZE_MAX 0xFFFFFFFFLL177178/*179* Transfer directory entries between internal and external form.180* dep is a struct denode * (internal form),181* dp is a struct direntry * (external form).182*/183#define DE_INTERNALIZE32(dep, dp) \184((dep)->de_StartCluster |= getushort((dp)->deHighClust) << 16)185#define DE_INTERNALIZE(dep, dp) \186(memcpy((dep)->de_Name, (dp)->deName, 11), \187(dep)->de_Attributes = (dp)->deAttributes, \188(dep)->de_LowerCase = (dp)->deLowerCase, \189(dep)->de_CHun = (dp)->deCHundredth, \190(dep)->de_CTime = getushort((dp)->deCTime), \191(dep)->de_CDate = getushort((dp)->deCDate), \192(dep)->de_ADate = getushort((dp)->deADate), \193(dep)->de_MTime = getushort((dp)->deMTime), \194(dep)->de_MDate = getushort((dp)->deMDate), \195(dep)->de_StartCluster = getushort((dp)->deStartCluster), \196(dep)->de_FileSize = getulong((dp)->deFileSize), \197(FAT32((dep)->de_pmp) ? DE_INTERNALIZE32((dep), (dp)) : 0))198199#define DE_EXTERNALIZE(dp, dep) \200(memcpy((dp)->deName, (dep)->de_Name, 11), \201(dp)->deAttributes = (dep)->de_Attributes, \202(dp)->deLowerCase = (dep)->de_LowerCase, \203(dp)->deCHundredth = (dep)->de_CHun, \204putushort((dp)->deCTime, (dep)->de_CTime), \205putushort((dp)->deCDate, (dep)->de_CDate), \206putushort((dp)->deADate, (dep)->de_ADate), \207putushort((dp)->deMTime, (dep)->de_MTime), \208putushort((dp)->deMDate, (dep)->de_MDate), \209putushort((dp)->deStartCluster, (dep)->de_StartCluster), \210putulong((dp)->deFileSize, \211((dep)->de_Attributes & ATTR_DIRECTORY) ? 0 : (dep)->de_FileSize), \212putushort((dp)->deHighClust, (dep)->de_StartCluster >> 16))213214#if defined(_KERNEL) || defined(_WANT_MSDOSFS_INTERNALS)215216#define VTODE(vp) ((struct denode *)(vp)->v_data)217#define DETOV(de) ((de)->de_vnode)218219#define DETOI(pmp, cn, off) \220((cn) == MSDOSFSROOT \221? (((uint64_t)(off) >> 5)) \222: (((((uint64_t)pmp->pm_bpcluster * ((cn) - 2) + (off))) >> 5) \223+ pmp->pm_RootDirEnts))224225#define DETIMES(dep, acc, mod, cre) do { \226if ((dep)->de_flag & DE_UPDATE) { \227(dep)->de_flag |= DE_MODIFIED; \228timespec2fattime((mod), 0, &(dep)->de_MDate, \229&(dep)->de_MTime, NULL); \230(dep)->de_Attributes |= ATTR_ARCHIVE; \231} \232if ((dep)->de_pmp->pm_flags & MSDOSFSMNT_NOWIN95) { \233(dep)->de_flag &= ~(DE_UPDATE | DE_CREATE | DE_ACCESS); \234break; \235} \236if ((dep)->de_flag & DE_ACCESS) { \237uint16_t adate; \238\239timespec2fattime((acc), 0, &adate, NULL, NULL); \240if (adate != (dep)->de_ADate) { \241(dep)->de_flag |= DE_MODIFIED; \242(dep)->de_ADate = adate; \243} \244} \245if ((dep)->de_flag & DE_CREATE) { \246timespec2fattime((cre), 0, &(dep)->de_CDate, \247&(dep)->de_CTime, &(dep)->de_CHun); \248(dep)->de_flag |= DE_MODIFIED; \249} \250(dep)->de_flag &= ~(DE_UPDATE | DE_CREATE | DE_ACCESS); \251} while (0)252253/*254* This overlays the fid structure (see mount.h)255*/256struct defid {257u_short defid_len; /* length of structure */258u_short defid_pad; /* force long alignment */259260uint32_t defid_dirclust; /* cluster this dir entry came from */261uint32_t defid_dirofs; /* offset of entry within the cluster */262#if 0263uint32_t defid_gen; /* generation number */264#endif265};266267extern struct vop_vector msdosfs_vnodeops;268269#ifdef _KERNEL270int msdosfs_lookup(struct vop_cachedlookup_args *);271int msdosfs_inactive(struct vop_inactive_args *);272int msdosfs_reclaim(struct vop_reclaim_args *);273int msdosfs_lookup_ino(struct vnode *vdp, struct vnode **vpp,274struct componentname *cnp, daddr_t *scnp, u_long *blkoffp);275#endif276277/*278* Internal service routine prototypes.279*/280struct componentname;281int deget(struct msdosfsmount *, u_long, u_long, int, struct denode **);282int uniqdosname(struct denode *, struct componentname *, u_char *);283284int readep(struct msdosfsmount *pmp, u_long dirclu, u_long dirofs, struct buf **bpp, struct direntry **epp);285int readde(struct denode *dep, struct buf **bpp, struct direntry **epp);286int deextend(struct denode *dep, u_long length, struct ucred *cred);287int fillinusemap(struct msdosfsmount *pmp);288void reinsert(struct denode *dep);289int dosdirempty(struct denode *dep);290int createde(struct denode *dep, struct denode *ddep, struct denode **depp, struct componentname *cnp);291int deupdat(struct denode *dep, int waitfor);292int removede(struct denode *pdep, struct denode *dep);293int detrunc(struct denode *dep, u_long length, int flags, struct ucred *cred);294int doscheckpath( struct denode *source, struct denode *target,295daddr_t *wait_scn);296#endif /* _KERNEL || _WANT_MSDOSFS_INTERNALS */297#endif /* !_FS_MSDOSFS_DENODE_H_ */298299300