denode.h revision 2893
12893Sdfr/*	$Id$ */
22893Sdfr/*	$NetBSD: denode.h,v 1.8 1994/08/21 18:43:49 ws Exp $	*/
32893Sdfr
42893Sdfr/*-
52893Sdfr * Copyright (C) 1994 Wolfgang Solfrank.
62893Sdfr * Copyright (C) 1994 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 */
352893Sdfr/*
362893Sdfr * Written by Paul Popelka (paulp@uts.amdahl.com)
372893Sdfr *
382893Sdfr * You can do anything you want with this software, just don't say you wrote
392893Sdfr * it, and don't remove this notice.
402893Sdfr *
412893Sdfr * This software is provided "as is".
422893Sdfr *
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.
472893Sdfr *
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 {
1062893Sdfr	u_short fc_frcn;	/* file relative cluster number */
1072893Sdfr	u_short 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 */
1192893Sdfr#define	FC_SIZE		2	/* 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 */
1232893Sdfr
1242893Sdfr#define	FCE_EMPTY	0xffff	/* doesn't represent an actual cluster # */
1252893Sdfr
1262893Sdfr/*
1272893Sdfr * Set a slot in the fat cache.
1282893Sdfr */
1292893Sdfr#define	fc_setcache(dep, slot, frcn, fsrcn) \
1302893Sdfr	(dep)->de_fc[slot].fc_frcn = frcn; \
1312893Sdfr	(dep)->de_fc[slot].fc_fsrcn = fsrcn;
1322893Sdfr
1332893Sdfr/*
1342893Sdfr * This is the in memory variant of a dos directory entry.  It is usually
1352893Sdfr * contained within a vnode.
1362893Sdfr */
1372893Sdfrstruct denode {
1382893Sdfr	struct denode *de_next;	/* Hash chain forward */
1392893Sdfr	struct denode **de_prev; /* Hash chain back */
1402893Sdfr	struct vnode *de_vnode;	/* addr of vnode we are part of */
1412893Sdfr	struct vnode *de_devvp;	/* vnode of blk dev we live on */
1422893Sdfr	u_long de_flag;		/* flag bits */
1432893Sdfr	dev_t de_dev;		/* device where direntry lives */
1442893Sdfr	u_long de_dirclust;	/* cluster of the directory file containing this entry */
1452893Sdfr	u_long de_diroffset;	/* ordinal of this entry in the directory */
1462893Sdfr	u_long de_fndclust;	/* cluster of found dir entry */
1472893Sdfr	u_long de_fndoffset;	/* offset of found dir entry */
1482893Sdfr	long de_refcnt;		/* reference count */
1492893Sdfr	struct msdosfsmount *de_pmp;	/* addr of our mount struct */
1502893Sdfr	struct lockf *de_lockf;	/* byte level lock list */
1512893Sdfr	pid_t de_lockholder;	/* current lock holder */
1522893Sdfr	pid_t de_lockwaiter;	/* lock wanter */
1532893Sdfr	/* the next two fields must be contiguous in memory... */
1542893Sdfr	u_char de_Name[8];	/* name, from directory entry */
1552893Sdfr	u_char de_Extension[3];	/* extension, from directory entry */
1562893Sdfr	u_char de_Attributes;	/* attributes, from directory entry */
1572893Sdfr	u_short de_Time;	/* creation time */
1582893Sdfr	u_short de_Date;	/* creation date */
1592893Sdfr	u_short de_StartCluster; /* starting cluster of file */
1602893Sdfr	u_long de_FileSize;	/* size of file in bytes */
1612893Sdfr	struct fatcache de_fc[FC_SIZE];	/* fat cache */
1622893Sdfr};
1632893Sdfr
1642893Sdfr/*
1652893Sdfr * Values for the de_flag field of the denode.
1662893Sdfr */
1672893Sdfr#define	DE_LOCKED	0x0001	/* directory entry is locked */
1682893Sdfr#define	DE_WANTED	0x0002	/* someone wants this de */
1692893Sdfr#define	DE_UPDATE	0x0004	/* file has been modified */
1702893Sdfr#define	DE_MODIFIED	0x0080	/* denode wants to be written back to disk */
1712893Sdfr
1722893Sdfr/*
1732893Sdfr * Transfer directory entries between internal and external form.
1742893Sdfr * dep is a struct denode * (internal form),
1752893Sdfr * dp is a struct direntry * (external form).
1762893Sdfr */
1772893Sdfr#define DE_INTERNALIZE(dep, dp)			\
1782893Sdfr	(bcopy((dp)->deName, (dep)->de_Name, 11),	\
1792893Sdfr	 (dep)->de_Attributes = (dp)->deAttributes,	\
1802893Sdfr	 (dep)->de_Time = getushort((dp)->deTime),	\
1812893Sdfr	 (dep)->de_Date = getushort((dp)->deDate),	\
1822893Sdfr	 (dep)->de_StartCluster = getushort((dp)->deStartCluster), \
1832893Sdfr	 (dep)->de_FileSize = getulong((dp)->deFileSize))
1842893Sdfr
1852893Sdfr#define DE_EXTERNALIZE(dp, dep)				\
1862893Sdfr	(bcopy((dep)->de_Name, (dp)->deName, 11),	\
1872893Sdfr	 (dp)->deAttributes = (dep)->de_Attributes,	\
1882893Sdfr	 putushort((dp)->deTime, (dep)->de_Time),	\
1892893Sdfr	 putushort((dp)->deDate, (dep)->de_Date),	\
1902893Sdfr	 putushort((dp)->deStartCluster, (dep)->de_StartCluster), \
1912893Sdfr	 putulong((dp)->deFileSize, (dep)->de_FileSize))
1922893Sdfr
1932893Sdfr#define	de_forw		de_chain[0]
1942893Sdfr#define	de_back		de_chain[1]
1952893Sdfr
1962893Sdfr#ifdef KERNEL
1972893Sdfr
1982893Sdfr#define	VTODE(vp)	((struct denode *)(vp)->v_data)
1992893Sdfr#define	DETOV(de)	((de)->de_vnode)
2002893Sdfr
2012893Sdfr#define	DE_UPDAT(dep, t, waitfor) \
2022893Sdfr	if (dep->de_flag & DE_UPDATE) \
2032893Sdfr		(void) deupdat(dep, t, waitfor);
2042893Sdfr
2052893Sdfr#define	DE_TIMES(dep, t) \
2062893Sdfr	if (dep->de_flag & DE_UPDATE) { \
2072893Sdfr		(dep)->de_flag |= DE_MODIFIED; \
2082893Sdfr		unix2dostime(t, &dep->de_Date, &dep->de_Time); \
2092893Sdfr		(dep)->de_flag &= ~DE_UPDATE; \
2102893Sdfr	}
2112893Sdfr
2122893Sdfr/*
2132893Sdfr * This overlays the fid sturcture (see mount.h)
2142893Sdfr */
2152893Sdfrstruct defid {
2162893Sdfr	u_short defid_len;	/* length of structure */
2172893Sdfr	u_short defid_pad;	/* force long alignment */
2182893Sdfr
2192893Sdfr	u_long defid_dirclust;	/* cluster this dir entry came from */
2202893Sdfr	u_long defid_dirofs;	/* index of entry within the cluster */
2212893Sdfr
2222893Sdfr	/* u_long	defid_gen;	 generation number */
2232893Sdfr};
2242893Sdfr
2252893Sdfr/*
2262893Sdfr * Prototypes for MSDOSFS vnode operations
2272893Sdfr */
2282893Sdfrint msdosfs_lookup __P((struct vop_lookup_args *));
2292893Sdfrint msdosfs_create __P((struct vop_create_args *));
2302893Sdfrint msdosfs_mknod __P((struct vop_mknod_args *));
2312893Sdfrint msdosfs_open __P((struct vop_open_args *));
2322893Sdfrint msdosfs_close __P((struct vop_close_args *));
2332893Sdfrint msdosfs_access __P((struct vop_access_args *));
2342893Sdfrint msdosfs_getattr __P((struct vop_getattr_args *));
2352893Sdfrint msdosfs_setattr __P((struct vop_setattr_args *));
2362893Sdfrint msdosfs_read __P((struct vop_read_args *));
2372893Sdfrint msdosfs_write __P((struct vop_write_args *));
2382893Sdfrint msdosfs_ioctl __P((struct vop_ioctl_args *));
2392893Sdfrint msdosfs_select __P((struct vop_select_args *));
2402893Sdfrint msdosfs_mmap __P((struct vop_mmap_args *));
2412893Sdfrint msdosfs_fsync __P((struct vop_fsync_args *));
2422893Sdfrint msdosfs_seek __P((struct vop_seek_args *));
2432893Sdfrint msdosfs_remove __P((struct vop_remove_args *));
2442893Sdfrint msdosfs_link __P((struct vop_link_args *));
2452893Sdfrint msdosfs_rename __P((struct vop_rename_args *));
2462893Sdfrint msdosfs_mkdir __P((struct vop_mkdir_args *));
2472893Sdfrint msdosfs_rmdir __P((struct vop_rmdir_args *));
2482893Sdfrint msdosfs_symlink __P((struct vop_symlink_args *));
2492893Sdfrint msdosfs_readdir __P((struct vop_readdir_args *));
2502893Sdfrint msdosfs_readlink __P((struct vop_readlink_args *));
2512893Sdfrint msdosfs_abortop __P((struct vop_abortop_args *));
2522893Sdfrint msdosfs_inactive __P((struct vop_inactive_args *));
2532893Sdfrint msdosfs_reclaim __P((struct vop_reclaim_args *));
2542893Sdfrint msdosfs_lock __P((struct vop_lock_args *));
2552893Sdfrint msdosfs_unlock __P((struct vop_unlock_args *));
2562893Sdfrint msdosfs_bmap __P((struct vop_bmap_args *));
2572893Sdfrint msdosfs_strategy __P((struct vop_strategy_args *));
2582893Sdfrint msdosfs_print __P((struct vop_print_args *));
2592893Sdfrint msdosfs_islocked __P((struct vop_islocked_args *));
2602893Sdfrint msdosfs_advlock __P((struct vop_advlock_args *));
2612893Sdfrint msdosfs_reallocblks __P((struct vop_reallocblks_args *));
2622893Sdfr
2632893Sdfr/*
2642893Sdfr * Internal service routine prototypes.
2652893Sdfr */
2662893Sdfrint deget __P((struct msdosfsmount * pmp, u_long dirclust, u_long diroffset, struct direntry * direntptr, struct denode ** depp));
2672893Sdfr#endif	/* KERNEL */
268