150477Speter/* $FreeBSD$ */
233548Sjkh/*	$NetBSD: denode.h,v 1.25 1997/11/17 15:36:28 ws Exp $	*/
32893Sdfr
42893Sdfr/*-
533548Sjkh * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
633548Sjkh * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
72893Sdfr * All rights reserved.
82893Sdfr * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
92893Sdfr *
102893Sdfr * Redistribution and use in source and binary forms, with or without
112893Sdfr * modification, are permitted provided that the following conditions
122893Sdfr * are met:
132893Sdfr * 1. Redistributions of source code must retain the above copyright
142893Sdfr *    notice, this list of conditions and the following disclaimer.
152893Sdfr * 2. Redistributions in binary form must reproduce the above copyright
162893Sdfr *    notice, this list of conditions and the following disclaimer in the
172893Sdfr *    documentation and/or other materials provided with the distribution.
182893Sdfr * 3. All advertising materials mentioning features or use of this software
192893Sdfr *    must display the following acknowledgement:
202893Sdfr *	This product includes software developed by TooLs GmbH.
212893Sdfr * 4. The name of TooLs GmbH may not be used to endorse or promote products
222893Sdfr *    derived from this software without specific prior written permission.
232893Sdfr *
242893Sdfr * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
252893Sdfr * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
262893Sdfr * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
272893Sdfr * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
282893Sdfr * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
292893Sdfr * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
302893Sdfr * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
312893Sdfr * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
322893Sdfr * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
332893Sdfr * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
342893Sdfr */
35139776Simp/*-
362893Sdfr * Written by Paul Popelka (paulp@uts.amdahl.com)
378876Srgrimes *
382893Sdfr * You can do anything you want with this software, just don't say you wrote
392893Sdfr * it, and don't remove this notice.
408876Srgrimes *
412893Sdfr * This software is provided "as is".
428876Srgrimes *
432893Sdfr * The author supplies this software to be publicly redistributed on the
442893Sdfr * understanding that the author is not responsible for the correct
452893Sdfr * functioning of this software in any circumstances and is not liable for
462893Sdfr * any damages caused by this software.
478876Srgrimes *
482893Sdfr * October 1992
492893Sdfr */
502893Sdfr
512893Sdfr/*
522893Sdfr * This is the pc filesystem specific portion of the vnode structure.
532893Sdfr *
542893Sdfr * To describe a file uniquely the de_dirclust, de_diroffset, and
552893Sdfr * de_StartCluster fields are used.
562893Sdfr *
572893Sdfr * de_dirclust contains the cluster number of the directory cluster
582893Sdfr *	containing the entry for a file or directory.
592893Sdfr * de_diroffset is the index into the cluster for the entry describing
602893Sdfr *	a file or directory.
612893Sdfr * de_StartCluster is the number of the first cluster of the file or directory.
622893Sdfr *
632893Sdfr * Now to describe the quirks of the pc filesystem.
642893Sdfr * - Clusters 0 and 1 are reserved.
652893Sdfr * - The first allocatable cluster is 2.
662893Sdfr * - The root directory is of fixed size and all blocks that make it up
672893Sdfr *   are contiguous.
682893Sdfr * - Cluster 0 refers to the root directory when it is found in the
692893Sdfr *   startcluster field of a directory entry that points to another directory.
702893Sdfr * - Cluster 0 implies a 0 length file when found in the start cluster field
712893Sdfr *   of a directory entry that points to a file.
722893Sdfr * - You can't use the cluster number 0 to derive the address of the root
732893Sdfr *   directory.
742893Sdfr * - Multiple directory entries can point to a directory. The entry in the
752893Sdfr *   parent directory points to a child directory.  Any directories in the
762893Sdfr *   child directory contain a ".." entry that points back to the parent.
772893Sdfr *   The child directory itself contains a "." entry that points to itself.
782893Sdfr * - The root directory does not contain a "." or ".." entry.
792893Sdfr * - Directory entries for directories are never changed once they are created
802893Sdfr *   (except when removed).  The size stays 0, and the last modification time
812893Sdfr *   is never changed.  This is because so many directory entries can point to
822893Sdfr *   the physical clusters that make up a directory.  It would lead to an
832893Sdfr *   update nightmare.
842893Sdfr * - The length field in a directory entry pointing to a directory contains 0
852893Sdfr *   (always).  The only way to find the end of a directory is to follow the
862893Sdfr *   cluster chain until the "last cluster" marker is found.
872893Sdfr *
882893Sdfr * My extensions to make this house of cards work.  These apply only to the in
892893Sdfr * memory copy of the directory entry.
902893Sdfr * - A reference count for each denode will be kept since dos doesn't keep such
912893Sdfr *   things.
922893Sdfr */
932893Sdfr
942893Sdfr/*
952893Sdfr * Internal pseudo-offset for (nonexistent) directory entry for the root
962893Sdfr * dir in the root dir
972893Sdfr */
982893Sdfr#define	MSDOSFSROOT_OFS	0x1fffffff
992893Sdfr
1002893Sdfr/*
1012893Sdfr * The fat cache structure. fc_fsrcn is the filesystem relative cluster
1022893Sdfr * number that corresponds to the file relative cluster number in this
1032893Sdfr * structure (fc_frcn).
1042893Sdfr */
1052893Sdfrstruct fatcache {
10633548Sjkh	u_long fc_frcn;		/* file relative cluster number */
10733548Sjkh	u_long fc_fsrcn;	/* filesystem relative cluster number */
1082893Sdfr};
1092893Sdfr
1102893Sdfr/*
1112893Sdfr * The fat entry cache as it stands helps make extending files a "quick"
1122893Sdfr * operation by avoiding having to scan the fat to discover the last
1132893Sdfr * cluster of the file. The cache also helps sequential reads by
1142893Sdfr * remembering the last cluster read from the file.  This also prevents us
1152893Sdfr * from having to rescan the fat to find the next cluster to read.  This
1162893Sdfr * cache is probably pretty worthless if a file is opened by multiple
1172893Sdfr * processes.
1182893Sdfr */
119166062Strhodes#define	FC_SIZE		3	/* number of entries in the cache */
1202893Sdfr#define	FC_LASTMAP	0	/* entry the last call to pcbmap() resolved
1212893Sdfr				 * to */
1222893Sdfr#define	FC_LASTFC	1	/* entry for the last cluster in the file */
123166062Strhodes#define	FC_NEXTTOLASTFC	2	/* entry for a close to the last cluster in
124166062Strhodes				 * the file */
1252893Sdfr
12633548Sjkh#define	FCE_EMPTY	0xffffffff	/* doesn't represent an actual cluster # */
1272893Sdfr
1282893Sdfr/*
1292893Sdfr * Set a slot in the fat cache.
1302893Sdfr */
1312893Sdfr#define	fc_setcache(dep, slot, frcn, fsrcn) \
132106696Salfred	(dep)->de_fc[(slot)].fc_frcn = (frcn); \
133106696Salfred	(dep)->de_fc[(slot)].fc_fsrcn = (fsrcn);
1342893Sdfr
1352893Sdfr/*
1362893Sdfr * This is the in memory variant of a dos directory entry.  It is usually
1372893Sdfr * contained within a vnode.
1382893Sdfr */
1392893Sdfrstruct denode {
1402893Sdfr	struct vnode *de_vnode;	/* addr of vnode we are part of */
1412893Sdfr	u_long de_flag;		/* flag bits */
1422893Sdfr	u_long de_dirclust;	/* cluster of the directory file containing this entry */
14333548Sjkh	u_long de_diroffset;	/* offset of this entry in the directory cluster */
1442893Sdfr	u_long de_fndoffset;	/* offset of found dir entry */
14533548Sjkh	int de_fndcnt;		/* number of slots before de_fndoffset */
1462893Sdfr	long de_refcnt;		/* reference count */
1472893Sdfr	struct msdosfsmount *de_pmp;	/* addr of our mount struct */
14833548Sjkh	u_char de_Name[12];	/* name, from DOS directory entry */
1492893Sdfr	u_char de_Attributes;	/* attributes, from directory entry */
15041275Sdt	u_char de_LowerCase;	/* NT VFAT lower case flags */
15133548Sjkh	u_char de_CHun;		/* Hundredth of second of CTime*/
15233548Sjkh	u_short de_CTime;	/* creation time */
15333548Sjkh	u_short de_CDate;	/* creation date */
15433548Sjkh	u_short de_ADate;	/* access date */
15533548Sjkh	u_short de_MTime;	/* modification time */
15633548Sjkh	u_short de_MDate;	/* modification date */
15733548Sjkh	u_long de_StartCluster; /* starting cluster of file */
1582893Sdfr	u_long de_FileSize;	/* size of file in bytes */
1592893Sdfr	struct fatcache de_fc[FC_SIZE];	/* fat cache */
1609862Sdfr	u_quad_t de_modrev;	/* Revision level for lease. */
161144740Sphk	u_int64_t de_inode;	/* Inode number (really byte offset of direntry) */
1622893Sdfr};
1632893Sdfr
1642893Sdfr/*
1652893Sdfr * Values for the de_flag field of the denode.
1662893Sdfr */
16733548Sjkh#define	DE_UPDATE	0x0004	/* Modification time update request */
16833548Sjkh#define	DE_CREATE	0x0008	/* Creation time update */
16933548Sjkh#define	DE_ACCESS	0x0010	/* Access time update */
17033548Sjkh#define	DE_MODIFIED	0x0020	/* Denode has been modified */
17133548Sjkh#define	DE_RENAME	0x0040	/* Denode is in the process of being renamed */
1722893Sdfr
17333548Sjkh
1742893Sdfr/*
1752893Sdfr * Transfer directory entries between internal and external form.
1762893Sdfr * dep is a struct denode * (internal form),
1772893Sdfr * dp is a struct direntry * (external form).
1782893Sdfr */
17933548Sjkh#define DE_INTERNALIZE32(dep, dp)			\
18033548Sjkh	 ((dep)->de_StartCluster |= getushort((dp)->deHighClust) << 16)
18133548Sjkh#define DE_INTERNALIZE(dep, dp)				\
1822893Sdfr	(bcopy((dp)->deName, (dep)->de_Name, 11),	\
1832893Sdfr	 (dep)->de_Attributes = (dp)->deAttributes,	\
18441275Sdt	 (dep)->de_LowerCase = (dp)->deLowerCase,	\
18533548Sjkh	 (dep)->de_CHun = (dp)->deCHundredth,		\
18633548Sjkh	 (dep)->de_CTime = getushort((dp)->deCTime),	\
18733548Sjkh	 (dep)->de_CDate = getushort((dp)->deCDate),	\
18833548Sjkh	 (dep)->de_ADate = getushort((dp)->deADate),	\
18933548Sjkh	 (dep)->de_MTime = getushort((dp)->deMTime),	\
19033548Sjkh	 (dep)->de_MDate = getushort((dp)->deMDate),	\
1912893Sdfr	 (dep)->de_StartCluster = getushort((dp)->deStartCluster), \
19233548Sjkh	 (dep)->de_FileSize = getulong((dp)->deFileSize), \
19333548Sjkh	 (FAT32((dep)->de_pmp) ? DE_INTERNALIZE32((dep), (dp)) : 0))
1942893Sdfr
1952893Sdfr#define DE_EXTERNALIZE(dp, dep)				\
1962893Sdfr	(bcopy((dep)->de_Name, (dp)->deName, 11),	\
1972893Sdfr	 (dp)->deAttributes = (dep)->de_Attributes,	\
19841275Sdt	 (dp)->deLowerCase = (dep)->de_LowerCase,	\
19933548Sjkh	 (dp)->deCHundredth = (dep)->de_CHun,		\
20033548Sjkh	 putushort((dp)->deCTime, (dep)->de_CTime),	\
20133548Sjkh	 putushort((dp)->deCDate, (dep)->de_CDate),	\
20233548Sjkh	 putushort((dp)->deADate, (dep)->de_ADate),	\
20333548Sjkh	 putushort((dp)->deMTime, (dep)->de_MTime),	\
20433548Sjkh	 putushort((dp)->deMDate, (dep)->de_MDate),	\
2052893Sdfr	 putushort((dp)->deStartCluster, (dep)->de_StartCluster), \
20633548Sjkh	 putulong((dp)->deFileSize,			\
20733548Sjkh	     ((dep)->de_Attributes & ATTR_DIRECTORY) ? 0 : (dep)->de_FileSize), \
20842249Sdt	 putushort((dp)->deHighClust, (dep)->de_StartCluster >> 16))
2092893Sdfr
21055206Speter#ifdef _KERNEL
2112893Sdfr
2122893Sdfr#define	VTODE(vp)	((struct denode *)(vp)->v_data)
2132893Sdfr#define	DETOV(de)	((de)->de_vnode)
2142893Sdfr
21542249Sdt#define	DETIMES(dep, acc, mod, cre) do {				\
216171756Sbde	if ((dep)->de_flag & DE_UPDATE) {				\
21742249Sdt		(dep)->de_flag |= DE_MODIFIED;				\
218163647Sphk		timespec2fattime((mod), 0, &(dep)->de_MDate,		\
219163647Sphk		    &(dep)->de_MTime, NULL);				\
220171756Sbde		(dep)->de_Attributes |= ATTR_ARCHIVE;			\
22142249Sdt	}								\
22242249Sdt	if ((dep)->de_pmp->pm_flags & MSDOSFSMNT_NOWIN95) {		\
22342249Sdt		(dep)->de_flag &= ~(DE_UPDATE | DE_CREATE | DE_ACCESS);	\
22442249Sdt		break;							\
22542249Sdt	}								\
22642249Sdt	if ((dep)->de_flag & DE_ACCESS) {				\
227171756Sbde		u_int16_t adate;					\
22842249Sdt									\
229163647Sphk		timespec2fattime((acc), 0, &adate, NULL, NULL);		\
23042249Sdt		if (adate != (dep)->de_ADate) {				\
23142249Sdt			(dep)->de_flag |= DE_MODIFIED;			\
23242249Sdt			(dep)->de_ADate = adate;			\
23342249Sdt		}							\
23442249Sdt	}								\
23542249Sdt	if ((dep)->de_flag & DE_CREATE) {				\
236163647Sphk		timespec2fattime((cre), 0, &(dep)->de_CDate,		\
237163647Sphk		    &(dep)->de_CTime, &(dep)->de_CHun);			\
238171756Sbde		(dep)->de_flag |= DE_MODIFIED;				\
23942249Sdt	}								\
24042249Sdt	(dep)->de_flag &= ~(DE_UPDATE | DE_CREATE | DE_ACCESS);		\
241161425Simp} while (0)
2422893Sdfr
2432893Sdfr/*
2445083Sbde * This overlays the fid structure (see mount.h)
2452893Sdfr */
2462893Sdfrstruct defid {
2472893Sdfr	u_short defid_len;	/* length of structure */
2482893Sdfr	u_short defid_pad;	/* force long alignment */
2492893Sdfr
250134945Stjr	u_int32_t defid_dirclust; /* cluster this dir entry came from */
251134945Stjr	u_int32_t defid_dirofs;	/* offset of entry within the cluster */
25233548Sjkh#if 0
253134945Stjr	u_int32_t defid_gen;	/* generation number */
25433548Sjkh#endif
2552893Sdfr};
2562893Sdfr
257138290Sphkextern struct vop_vector msdosfs_vnodeops;
25812158Sbde
25992727Salfredint msdosfs_lookup(struct vop_cachedlookup_args *);
26092727Salfredint msdosfs_inactive(struct vop_inactive_args *);
26192727Salfredint msdosfs_reclaim(struct vop_reclaim_args *);
26212158Sbde
2632893Sdfr/*
2642893Sdfr * Internal service routine prototypes.
2652893Sdfr */
26692727Salfredint deget(struct msdosfsmount *, u_long, u_long, struct denode **);
26792727Salfredint uniqdosname(struct denode *, struct componentname *, u_char *);
26892727Salfredint findwin95(struct denode *);
26933548Sjkh
27092727Salfredint readep(struct msdosfsmount *pmp, u_long dirclu, u_long dirofs,  struct buf **bpp, struct direntry **epp);
27192727Salfredint readde(struct denode *dep, struct buf **bpp, struct direntry **epp);
27292727Salfredint deextend(struct denode *dep, u_long length, struct ucred *cred);
27392727Salfredint fillinusemap(struct msdosfsmount *pmp);
27492727Salfredvoid reinsert(struct denode *dep);
27592727Salfredint dosdirempty(struct denode *dep);
27692727Salfredint createde(struct denode *dep, struct denode *ddep, struct denode **depp, struct componentname *cnp);
27792727Salfredint deupdat(struct denode *dep, int waitfor);
27892727Salfredint removede(struct denode *pdep, struct denode *dep);
27992727Salfredint detrunc(struct denode *dep, u_long length, int flags, struct ucred *cred, struct thread *td);
28092727Salfredint doscheckpath( struct denode *source, struct denode *target);
28155206Speter#endif	/* _KERNEL */
282