1952a6212SJordan K. Hubbard /* $NetBSD: denode.h,v 1.25 1997/11/17 15:36:28 ws Exp $ */ 227a0bc89SDoug Rabson 327a0bc89SDoug Rabson /*- 4d63027b6SPedro F. Giffuni * SPDX-License-Identifier: BSD-4-Clause 5d63027b6SPedro F. Giffuni * 6952a6212SJordan K. Hubbard * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank. 7952a6212SJordan K. Hubbard * Copyright (C) 1994, 1995, 1997 TooLs GmbH. 827a0bc89SDoug Rabson * All rights reserved. 927a0bc89SDoug Rabson * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below). 1027a0bc89SDoug Rabson * 1127a0bc89SDoug Rabson * Redistribution and use in source and binary forms, with or without 1227a0bc89SDoug Rabson * modification, are permitted provided that the following conditions 1327a0bc89SDoug Rabson * are met: 1427a0bc89SDoug Rabson * 1. Redistributions of source code must retain the above copyright 1527a0bc89SDoug Rabson * notice, this list of conditions and the following disclaimer. 1627a0bc89SDoug Rabson * 2. Redistributions in binary form must reproduce the above copyright 1727a0bc89SDoug Rabson * notice, this list of conditions and the following disclaimer in the 1827a0bc89SDoug Rabson * documentation and/or other materials provided with the distribution. 1927a0bc89SDoug Rabson * 3. All advertising materials mentioning features or use of this software 2027a0bc89SDoug Rabson * must display the following acknowledgement: 2127a0bc89SDoug Rabson * This product includes software developed by TooLs GmbH. 2227a0bc89SDoug Rabson * 4. The name of TooLs GmbH may not be used to endorse or promote products 2327a0bc89SDoug Rabson * derived from this software without specific prior written permission. 2427a0bc89SDoug Rabson * 2527a0bc89SDoug Rabson * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 2627a0bc89SDoug Rabson * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 2727a0bc89SDoug Rabson * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2827a0bc89SDoug Rabson * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2927a0bc89SDoug Rabson * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 3027a0bc89SDoug Rabson * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 3127a0bc89SDoug Rabson * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 3227a0bc89SDoug Rabson * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 3327a0bc89SDoug Rabson * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 3427a0bc89SDoug Rabson * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3527a0bc89SDoug Rabson */ 36d167cf6fSWarner Losh /*- 3727a0bc89SDoug Rabson * Written by Paul Popelka (paulp@uts.amdahl.com) 3827a0bc89SDoug Rabson * 3927a0bc89SDoug Rabson * You can do anything you want with this software, just don't say you wrote 4027a0bc89SDoug Rabson * it, and don't remove this notice. 4127a0bc89SDoug Rabson * 4227a0bc89SDoug Rabson * This software is provided "as is". 4327a0bc89SDoug Rabson * 4427a0bc89SDoug Rabson * The author supplies this software to be publicly redistributed on the 4527a0bc89SDoug Rabson * understanding that the author is not responsible for the correct 4627a0bc89SDoug Rabson * functioning of this software in any circumstances and is not liable for 4727a0bc89SDoug Rabson * any damages caused by this software. 4827a0bc89SDoug Rabson * 4927a0bc89SDoug Rabson * October 1992 5027a0bc89SDoug Rabson */ 51db20c27dSEd Maste #ifndef _FS_MSDOSFS_DENODE_H_ 52db20c27dSEd Maste #define _FS_MSDOSFS_DENODE_H_ 5327a0bc89SDoug Rabson 5427a0bc89SDoug Rabson /* 5527a0bc89SDoug Rabson * This is the pc filesystem specific portion of the vnode structure. 5627a0bc89SDoug Rabson * 5727a0bc89SDoug Rabson * To describe a file uniquely the de_dirclust, de_diroffset, and 5827a0bc89SDoug Rabson * de_StartCluster fields are used. 5927a0bc89SDoug Rabson * 6027a0bc89SDoug Rabson * de_dirclust contains the cluster number of the directory cluster 6127a0bc89SDoug Rabson * containing the entry for a file or directory. 6227a0bc89SDoug Rabson * de_diroffset is the index into the cluster for the entry describing 6327a0bc89SDoug Rabson * a file or directory. 6427a0bc89SDoug Rabson * de_StartCluster is the number of the first cluster of the file or directory. 6527a0bc89SDoug Rabson * 6627a0bc89SDoug Rabson * Now to describe the quirks of the pc filesystem. 6727a0bc89SDoug Rabson * - Clusters 0 and 1 are reserved. 6827a0bc89SDoug Rabson * - The first allocatable cluster is 2. 6927a0bc89SDoug Rabson * - The root directory is of fixed size and all blocks that make it up 7027a0bc89SDoug Rabson * are contiguous. 7127a0bc89SDoug Rabson * - Cluster 0 refers to the root directory when it is found in the 7227a0bc89SDoug Rabson * startcluster field of a directory entry that points to another directory. 7327a0bc89SDoug Rabson * - Cluster 0 implies a 0 length file when found in the start cluster field 7427a0bc89SDoug Rabson * of a directory entry that points to a file. 7527a0bc89SDoug Rabson * - You can't use the cluster number 0 to derive the address of the root 7627a0bc89SDoug Rabson * directory. 7727a0bc89SDoug Rabson * - Multiple directory entries can point to a directory. The entry in the 7827a0bc89SDoug Rabson * parent directory points to a child directory. Any directories in the 7927a0bc89SDoug Rabson * child directory contain a ".." entry that points back to the parent. 8027a0bc89SDoug Rabson * The child directory itself contains a "." entry that points to itself. 8127a0bc89SDoug Rabson * - The root directory does not contain a "." or ".." entry. 8227a0bc89SDoug Rabson * - Directory entries for directories are never changed once they are created 8327a0bc89SDoug Rabson * (except when removed). The size stays 0, and the last modification time 8427a0bc89SDoug Rabson * is never changed. This is because so many directory entries can point to 8527a0bc89SDoug Rabson * the physical clusters that make up a directory. It would lead to an 8627a0bc89SDoug Rabson * update nightmare. 8727a0bc89SDoug Rabson * - The length field in a directory entry pointing to a directory contains 0 8827a0bc89SDoug Rabson * (always). The only way to find the end of a directory is to follow the 8927a0bc89SDoug Rabson * cluster chain until the "last cluster" marker is found. 9027a0bc89SDoug Rabson * 9127a0bc89SDoug Rabson * My extensions to make this house of cards work. These apply only to the in 9227a0bc89SDoug Rabson * memory copy of the directory entry. 9327a0bc89SDoug Rabson * - A reference count for each denode will be kept since dos doesn't keep such 9427a0bc89SDoug Rabson * things. 9527a0bc89SDoug Rabson */ 9627a0bc89SDoug Rabson 9727a0bc89SDoug Rabson /* 9827a0bc89SDoug Rabson * Internal pseudo-offset for (nonexistent) directory entry for the root 9927a0bc89SDoug Rabson * dir in the root dir 10027a0bc89SDoug Rabson */ 10127a0bc89SDoug Rabson #define MSDOSFSROOT_OFS 0x1fffffff 10227a0bc89SDoug Rabson 10327a0bc89SDoug Rabson /* 1049287dbaaSEd Maste * The FAT cache structure. fc_fsrcn is the filesystem relative cluster 10527a0bc89SDoug Rabson * number that corresponds to the file relative cluster number in this 10627a0bc89SDoug Rabson * structure (fc_frcn). 10727a0bc89SDoug Rabson */ 10827a0bc89SDoug Rabson struct fatcache { 109952a6212SJordan K. Hubbard u_long fc_frcn; /* file relative cluster number */ 110952a6212SJordan K. Hubbard u_long fc_fsrcn; /* filesystem relative cluster number */ 11127a0bc89SDoug Rabson }; 11227a0bc89SDoug Rabson 11327a0bc89SDoug Rabson /* 1149287dbaaSEd Maste * The FAT entry cache as it stands helps make extending files a "quick" 1159287dbaaSEd Maste * operation by avoiding having to scan the FAT to discover the last 11627a0bc89SDoug Rabson * cluster of the file. The cache also helps sequential reads by 11727a0bc89SDoug Rabson * remembering the last cluster read from the file. This also prevents us 1189287dbaaSEd Maste * from having to rescan the FAT to find the next cluster to read. This 11927a0bc89SDoug Rabson * cache is probably pretty worthless if a file is opened by multiple 12027a0bc89SDoug Rabson * processes. 12127a0bc89SDoug Rabson */ 122752945d6STom Rhodes #define FC_SIZE 3 /* number of entries in the cache */ 12327a0bc89SDoug Rabson #define FC_LASTMAP 0 /* entry the last call to pcbmap() resolved 12427a0bc89SDoug Rabson * to */ 12527a0bc89SDoug Rabson #define FC_LASTFC 1 /* entry for the last cluster in the file */ 126752945d6STom Rhodes #define FC_NEXTTOLASTFC 2 /* entry for a close to the last cluster in 127752945d6STom Rhodes * the file */ 12827a0bc89SDoug Rabson 129952a6212SJordan K. Hubbard #define FCE_EMPTY 0xffffffff /* doesn't represent an actual cluster # */ 13027a0bc89SDoug Rabson 13127a0bc89SDoug Rabson /* 1329287dbaaSEd Maste * Set a slot in the FAT cache. 13327a0bc89SDoug Rabson */ 13427a0bc89SDoug Rabson #define fc_setcache(dep, slot, frcn, fsrcn) \ 13529f19445SAlfred Perlstein (dep)->de_fc[(slot)].fc_frcn = (frcn); \ 13629f19445SAlfred Perlstein (dep)->de_fc[(slot)].fc_fsrcn = (fsrcn); 13727a0bc89SDoug Rabson 13827a0bc89SDoug Rabson /* 13927a0bc89SDoug Rabson * This is the in memory variant of a dos directory entry. It is usually 14027a0bc89SDoug Rabson * contained within a vnode. 14127a0bc89SDoug Rabson */ 14227a0bc89SDoug Rabson struct denode { 14327a0bc89SDoug Rabson struct vnode *de_vnode; /* addr of vnode we are part of */ 1442bfd8992SKonstantin Belousov struct vn_clusterw de_clusterw; /* buffer clustering information */ 14527a0bc89SDoug Rabson u_long de_flag; /* flag bits */ 14627a0bc89SDoug Rabson u_long de_dirclust; /* cluster of the directory file containing this entry */ 147952a6212SJordan K. Hubbard u_long de_diroffset; /* offset of this entry in the directory cluster */ 14827a0bc89SDoug Rabson u_long de_fndoffset; /* offset of found dir entry */ 149952a6212SJordan K. Hubbard int de_fndcnt; /* number of slots before de_fndoffset */ 15027a0bc89SDoug Rabson long de_refcnt; /* reference count */ 15127a0bc89SDoug Rabson struct msdosfsmount *de_pmp; /* addr of our mount struct */ 152952a6212SJordan K. Hubbard u_char de_Name[12]; /* name, from DOS directory entry */ 15327a0bc89SDoug Rabson u_char de_Attributes; /* attributes, from directory entry */ 154bad3d41dSDmitrij Tejblum u_char de_LowerCase; /* NT VFAT lower case flags */ 155952a6212SJordan K. Hubbard u_char de_CHun; /* Hundredth of second of CTime*/ 156952a6212SJordan K. Hubbard u_short de_CTime; /* creation time */ 157952a6212SJordan K. Hubbard u_short de_CDate; /* creation date */ 158952a6212SJordan K. Hubbard u_short de_ADate; /* access date */ 159952a6212SJordan K. Hubbard u_short de_MTime; /* modification time */ 160952a6212SJordan K. Hubbard u_short de_MDate; /* modification date */ 161952a6212SJordan K. Hubbard u_long de_StartCluster; /* starting cluster of file */ 16227a0bc89SDoug Rabson u_long de_FileSize; /* size of file in bytes */ 1639287dbaaSEd Maste struct fatcache de_fc[FC_SIZE]; /* FAT cache */ 16494a8606fSDoug Rabson u_quad_t de_modrev; /* Revision level for lease. */ 165*445d3d22SStefan Eßer uint64_t de_inode; /* Inode number (really index of DOS style direntry) */ 16627a0bc89SDoug Rabson }; 16727a0bc89SDoug Rabson 16827a0bc89SDoug Rabson /* 16927a0bc89SDoug Rabson * Values for the de_flag field of the denode. 17027a0bc89SDoug Rabson */ 171952a6212SJordan K. Hubbard #define DE_UPDATE 0x0004 /* Modification time update request */ 172952a6212SJordan K. Hubbard #define DE_CREATE 0x0008 /* Creation time update */ 173952a6212SJordan K. Hubbard #define DE_ACCESS 0x0010 /* Access time update */ 174952a6212SJordan K. Hubbard #define DE_MODIFIED 0x0020 /* Denode has been modified */ 175952a6212SJordan K. Hubbard 17622e56aeaSEd Maste /* Maximum size of a file on a FAT filesystem */ 17722e56aeaSEd Maste #define MSDOSFS_FILESIZE_MAX 0xFFFFFFFFLL 17827a0bc89SDoug Rabson 17927a0bc89SDoug Rabson /* 18027a0bc89SDoug Rabson * Transfer directory entries between internal and external form. 18127a0bc89SDoug Rabson * dep is a struct denode * (internal form), 18227a0bc89SDoug Rabson * dp is a struct direntry * (external form). 18327a0bc89SDoug Rabson */ 184952a6212SJordan K. Hubbard #define DE_INTERNALIZE32(dep, dp) \ 185952a6212SJordan K. Hubbard ((dep)->de_StartCluster |= getushort((dp)->deHighClust) << 16) 18627a0bc89SDoug Rabson #define DE_INTERNALIZE(dep, dp) \ 1876a1c2e1fSEd Maste (memcpy((dep)->de_Name, (dp)->deName, 11), \ 18827a0bc89SDoug Rabson (dep)->de_Attributes = (dp)->deAttributes, \ 189bad3d41dSDmitrij Tejblum (dep)->de_LowerCase = (dp)->deLowerCase, \ 190952a6212SJordan K. Hubbard (dep)->de_CHun = (dp)->deCHundredth, \ 191952a6212SJordan K. Hubbard (dep)->de_CTime = getushort((dp)->deCTime), \ 192952a6212SJordan K. Hubbard (dep)->de_CDate = getushort((dp)->deCDate), \ 193952a6212SJordan K. Hubbard (dep)->de_ADate = getushort((dp)->deADate), \ 194952a6212SJordan K. Hubbard (dep)->de_MTime = getushort((dp)->deMTime), \ 195952a6212SJordan K. Hubbard (dep)->de_MDate = getushort((dp)->deMDate), \ 19627a0bc89SDoug Rabson (dep)->de_StartCluster = getushort((dp)->deStartCluster), \ 197952a6212SJordan K. Hubbard (dep)->de_FileSize = getulong((dp)->deFileSize), \ 198952a6212SJordan K. Hubbard (FAT32((dep)->de_pmp) ? DE_INTERNALIZE32((dep), (dp)) : 0)) 19927a0bc89SDoug Rabson 20027a0bc89SDoug Rabson #define DE_EXTERNALIZE(dp, dep) \ 2016a1c2e1fSEd Maste (memcpy((dp)->deName, (dep)->de_Name, 11), \ 20227a0bc89SDoug Rabson (dp)->deAttributes = (dep)->de_Attributes, \ 203bad3d41dSDmitrij Tejblum (dp)->deLowerCase = (dep)->de_LowerCase, \ 204952a6212SJordan K. Hubbard (dp)->deCHundredth = (dep)->de_CHun, \ 205952a6212SJordan K. Hubbard putushort((dp)->deCTime, (dep)->de_CTime), \ 206952a6212SJordan K. Hubbard putushort((dp)->deCDate, (dep)->de_CDate), \ 207952a6212SJordan K. Hubbard putushort((dp)->deADate, (dep)->de_ADate), \ 208952a6212SJordan K. Hubbard putushort((dp)->deMTime, (dep)->de_MTime), \ 209952a6212SJordan K. Hubbard putushort((dp)->deMDate, (dep)->de_MDate), \ 21027a0bc89SDoug Rabson putushort((dp)->deStartCluster, (dep)->de_StartCluster), \ 211952a6212SJordan K. Hubbard putulong((dp)->deFileSize, \ 212952a6212SJordan K. Hubbard ((dep)->de_Attributes & ATTR_DIRECTORY) ? 0 : (dep)->de_FileSize), \ 2139d9fdb45SDmitrij Tejblum putushort((dp)->deHighClust, (dep)->de_StartCluster >> 16)) 21427a0bc89SDoug Rabson 215476b0ab7SEd Maste #if defined(_KERNEL) || defined(MAKEFS) 21627a0bc89SDoug Rabson 21727a0bc89SDoug Rabson #define VTODE(vp) ((struct denode *)(vp)->v_data) 21827a0bc89SDoug Rabson #define DETOV(de) ((de)->de_vnode) 21927a0bc89SDoug Rabson 220*445d3d22SStefan Eßer #define DETOI(pmp, cn, off) \ 221*445d3d22SStefan Eßer ((cn) == MSDOSFSROOT \ 222*445d3d22SStefan Eßer ? (((uint64_t)(off) >> 5)) \ 223*445d3d22SStefan Eßer : (((((uint64_t)pmp->pm_bpcluster * ((cn) - 2) + (off))) >> 5) \ 224*445d3d22SStefan Eßer + pmp->pm_RootDirEnts)) 225*445d3d22SStefan Eßer 2269d9fdb45SDmitrij Tejblum #define DETIMES(dep, acc, mod, cre) do { \ 227952a6212SJordan K. Hubbard if ((dep)->de_flag & DE_UPDATE) { \ 2289d9fdb45SDmitrij Tejblum (dep)->de_flag |= DE_MODIFIED; \ 2293c960d93SPoul-Henning Kamp timespec2fattime((mod), 0, &(dep)->de_MDate, \ 2303c960d93SPoul-Henning Kamp &(dep)->de_MTime, NULL); \ 231a176f73dSBruce Evans (dep)->de_Attributes |= ATTR_ARCHIVE; \ 232a176f73dSBruce Evans } \ 2339d9fdb45SDmitrij Tejblum if ((dep)->de_pmp->pm_flags & MSDOSFSMNT_NOWIN95) { \ 2349d9fdb45SDmitrij Tejblum (dep)->de_flag &= ~(DE_UPDATE | DE_CREATE | DE_ACCESS); \ 2359d9fdb45SDmitrij Tejblum break; \ 2369d9fdb45SDmitrij Tejblum } \ 2379d9fdb45SDmitrij Tejblum if ((dep)->de_flag & DE_ACCESS) { \ 23823c53312SEd Maste uint16_t adate; \ 2399d9fdb45SDmitrij Tejblum \ 2403c960d93SPoul-Henning Kamp timespec2fattime((acc), 0, &adate, NULL, NULL); \ 2419d9fdb45SDmitrij Tejblum if (adate != (dep)->de_ADate) { \ 2429d9fdb45SDmitrij Tejblum (dep)->de_flag |= DE_MODIFIED; \ 2439d9fdb45SDmitrij Tejblum (dep)->de_ADate = adate; \ 2449d9fdb45SDmitrij Tejblum } \ 2459d9fdb45SDmitrij Tejblum } \ 2469d9fdb45SDmitrij Tejblum if ((dep)->de_flag & DE_CREATE) { \ 2473c960d93SPoul-Henning Kamp timespec2fattime((cre), 0, &(dep)->de_CDate, \ 2483c960d93SPoul-Henning Kamp &(dep)->de_CTime, &(dep)->de_CHun); \ 2499d9fdb45SDmitrij Tejblum (dep)->de_flag |= DE_MODIFIED; \ 250952a6212SJordan K. Hubbard } \ 251952a6212SJordan K. Hubbard (dep)->de_flag &= ~(DE_UPDATE | DE_CREATE | DE_ACCESS); \ 2521a3c917fSWarner Losh } while (0) 25327a0bc89SDoug Rabson 25427a0bc89SDoug Rabson /* 25563e4f22aSBruce Evans * This overlays the fid structure (see mount.h) 25627a0bc89SDoug Rabson */ 25727a0bc89SDoug Rabson struct defid { 25827a0bc89SDoug Rabson u_short defid_len; /* length of structure */ 25927a0bc89SDoug Rabson u_short defid_pad; /* force long alignment */ 26027a0bc89SDoug Rabson 26123c53312SEd Maste uint32_t defid_dirclust; /* cluster this dir entry came from */ 26223c53312SEd Maste uint32_t defid_dirofs; /* offset of entry within the cluster */ 263952a6212SJordan K. Hubbard #if 0 26423c53312SEd Maste uint32_t defid_gen; /* generation number */ 265952a6212SJordan K. Hubbard #endif 26627a0bc89SDoug Rabson }; 26727a0bc89SDoug Rabson 268aec0fb7bSPoul-Henning Kamp extern struct vop_vector msdosfs_vnodeops; 269f57e6547SBruce Evans 270476b0ab7SEd Maste #ifdef _KERNEL 27111caded3SAlfred Perlstein int msdosfs_lookup(struct vop_cachedlookup_args *); 27211caded3SAlfred Perlstein int msdosfs_inactive(struct vop_inactive_args *); 27311caded3SAlfred Perlstein int msdosfs_reclaim(struct vop_reclaim_args *); 27492d4e088SKonstantin Belousov int msdosfs_lookup_ino(struct vnode *vdp, struct vnode **vpp, 27595d42526SKonstantin Belousov struct componentname *cnp, daddr_t *scnp, u_long *blkoffp); 276476b0ab7SEd Maste #endif 277f57e6547SBruce Evans 27827a0bc89SDoug Rabson /* 27927a0bc89SDoug Rabson * Internal service routine prototypes. 28027a0bc89SDoug Rabson */ 281d485c77fSKonstantin Belousov struct componentname; 282ae7e8a02SKonstantin Belousov int deget(struct msdosfsmount *, u_long, u_long, int, struct denode **); 28311caded3SAlfred Perlstein int uniqdosname(struct denode *, struct componentname *, u_char *); 284952a6212SJordan K. Hubbard 28511caded3SAlfred Perlstein int readep(struct msdosfsmount *pmp, u_long dirclu, u_long dirofs, struct buf **bpp, struct direntry **epp); 28611caded3SAlfred Perlstein int readde(struct denode *dep, struct buf **bpp, struct direntry **epp); 28711caded3SAlfred Perlstein int deextend(struct denode *dep, u_long length, struct ucred *cred); 28811caded3SAlfred Perlstein int fillinusemap(struct msdosfsmount *pmp); 28911caded3SAlfred Perlstein void reinsert(struct denode *dep); 29011caded3SAlfred Perlstein int dosdirempty(struct denode *dep); 29111caded3SAlfred Perlstein int createde(struct denode *dep, struct denode *ddep, struct denode **depp, struct componentname *cnp); 29211caded3SAlfred Perlstein int deupdat(struct denode *dep, int waitfor); 29311caded3SAlfred Perlstein int removede(struct denode *pdep, struct denode *dep); 294c52fd858SEdward Tomasz Napierala int detrunc(struct denode *dep, u_long length, int flags, struct ucred *cred); 29595d42526SKonstantin Belousov int doscheckpath( struct denode *source, struct denode *target, 29695d42526SKonstantin Belousov daddr_t *wait_scn); 297476b0ab7SEd Maste #endif /* _KERNEL || MAKEFS */ 298db20c27dSEd Maste #endif /* !_FS_MSDOSFS_DENODE_H_ */ 299