xref: /openbsd-src/usr.sbin/makefs/msdos/denode.h (revision 8b94819b2b5c22856d283a83be37d1b43831c868)
1*8b94819bSkrw /*	$OpenBSD: denode.h,v 1.2 2016/12/17 16:43:30 krw Exp $	*/
272290da1Snatano /*	$NetBSD: denode.h,v 1.24 2014/07/08 09:21:52 hannken Exp $	*/
372290da1Snatano 
472290da1Snatano /*-
572290da1Snatano  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
672290da1Snatano  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
772290da1Snatano  * All rights reserved.
872290da1Snatano  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
972290da1Snatano  *
1072290da1Snatano  * Redistribution and use in source and binary forms, with or without
1172290da1Snatano  * modification, are permitted provided that the following conditions
1272290da1Snatano  * are met:
1372290da1Snatano  * 1. Redistributions of source code must retain the above copyright
1472290da1Snatano  *    notice, this list of conditions and the following disclaimer.
1572290da1Snatano  * 2. Redistributions in binary form must reproduce the above copyright
1672290da1Snatano  *    notice, this list of conditions and the following disclaimer in the
1772290da1Snatano  *    documentation and/or other materials provided with the distribution.
1872290da1Snatano  * 3. All advertising materials mentioning features or use of this software
1972290da1Snatano  *    must display the following acknowledgement:
2072290da1Snatano  *	This product includes software developed by TooLs GmbH.
2172290da1Snatano  * 4. The name of TooLs GmbH may not be used to endorse or promote products
2272290da1Snatano  *    derived from this software without specific prior written permission.
2372290da1Snatano  *
2472290da1Snatano  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
2572290da1Snatano  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2672290da1Snatano  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2772290da1Snatano  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2872290da1Snatano  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2972290da1Snatano  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
3072290da1Snatano  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
3172290da1Snatano  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
3272290da1Snatano  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
3372290da1Snatano  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3472290da1Snatano  */
3572290da1Snatano /*
3672290da1Snatano  * Written by Paul Popelka (paulp@uts.amdahl.com)
3772290da1Snatano  *
3872290da1Snatano  * You can do anything you want with this software, just don't say you wrote
3972290da1Snatano  * it, and don't remove this notice.
4072290da1Snatano  *
4172290da1Snatano  * This software is provided "as is".
4272290da1Snatano  *
4372290da1Snatano  * The author supplies this software to be publicly redistributed on the
4472290da1Snatano  * understanding that the author is not responsible for the correct
4572290da1Snatano  * functioning of this software in any circumstances and is not liable for
4672290da1Snatano  * any damages caused by this software.
4772290da1Snatano  *
4872290da1Snatano  * October 1992
4972290da1Snatano  */
5072290da1Snatano #ifndef _MSDOSFS_DENODE_H_
5172290da1Snatano #define _MSDOSFS_DENODE_H_
5272290da1Snatano 
5372290da1Snatano struct genfs_node {
5472290da1Snatano };
5572290da1Snatano struct mkfsvnode;
5672290da1Snatano struct msdosfsmount;
5772290da1Snatano struct mkfsbuf;
5872290da1Snatano 
5972290da1Snatano /*
6072290da1Snatano  * This is the pc filesystem specific portion of the mkfsvnode structure.
6172290da1Snatano  *
6272290da1Snatano  * To describe a file uniquely the de_dirclust, de_diroffset, and
6372290da1Snatano  * de_StartCluster fields are used.
6472290da1Snatano  *
6572290da1Snatano  * de_dirclust contains the cluster number of the directory cluster
6672290da1Snatano  *	containing the entry for a file or directory.
6772290da1Snatano  * de_diroffset is the index into the cluster for the entry describing
6872290da1Snatano  *	a file or directory.
6972290da1Snatano  * de_StartCluster is the number of the first cluster of the file or directory.
7072290da1Snatano  *
7172290da1Snatano  * Now to describe the quirks of the pc filesystem.
7272290da1Snatano  * - Clusters 0 and 1 are reserved.
7372290da1Snatano  * - The first allocatable cluster is 2.
7472290da1Snatano  * - The root directory is of fixed size and all blocks that make it up
7572290da1Snatano  *   are contiguous.
7672290da1Snatano  * - Cluster 0 refers to the root directory when it is found in the
7772290da1Snatano  *   startcluster field of a directory entry that points to another directory.
7872290da1Snatano  * - Cluster 0 implies a 0 length file when found in the start cluster field
7972290da1Snatano  *   of a directory entry that points to a file.
8072290da1Snatano  * - You can't use the cluster number 0 to derive the address of the root
8172290da1Snatano  *   directory.
8272290da1Snatano  * - Multiple directory entries can point to a directory. The entry in the
8372290da1Snatano  *   parent directory points to a child directory.  Any directories in the
8472290da1Snatano  *   child directory contain a ".." entry that points back to the parent.
8572290da1Snatano  *   The child directory itself contains a "." entry that points to itself.
8672290da1Snatano  * - The root directory does not contain a "." or ".." entry.
8772290da1Snatano  * - Directory entries for directories are never changed once they are created
8872290da1Snatano  *   (except when removed).  The size stays 0, and the last modification time
8972290da1Snatano  *   is never changed.  This is because so many directory entries can point to
9072290da1Snatano  *   the physical clusters that make up a directory.  It would lead to an
9172290da1Snatano  *   update nightmare.
9272290da1Snatano  * - The length field in a directory entry pointing to a directory contains 0
9372290da1Snatano  *   (always).  The only way to find the end of a directory is to follow the
9472290da1Snatano  *   cluster chain until the "last cluster" marker is found.
9572290da1Snatano  *
9672290da1Snatano  * My extensions to make this house of cards work.  These apply only to the in
9772290da1Snatano  * memory copy of the directory entry.
9872290da1Snatano  * - A reference count for each denode will be kept since dos doesn't keep such
9972290da1Snatano  *   things.
10072290da1Snatano  */
10172290da1Snatano 
10272290da1Snatano /*
10372290da1Snatano  * Internal pseudo-offset for (nonexistent) directory entry for the root
10472290da1Snatano  * dir in the root dir
10572290da1Snatano  */
10672290da1Snatano #define	MSDOSFSROOT_OFS	0x1fffffff
10772290da1Snatano 
10872290da1Snatano /*
10972290da1Snatano  * The FAT cache structure. fc_fsrcn is the filesystem relative cluster
11072290da1Snatano  * number that corresponds to the file relative cluster number in this
11172290da1Snatano  * structure (fc_frcn).
11272290da1Snatano  */
11372290da1Snatano struct fatcache {
11472290da1Snatano 	u_long fc_frcn;		/* file relative cluster number */
11572290da1Snatano 	u_long fc_fsrcn;	/* filesystem relative cluster number */
11672290da1Snatano };
11772290da1Snatano 
11872290da1Snatano /*
11972290da1Snatano  * The FAT entry cache as it stands helps make extending files a "quick"
12072290da1Snatano  * operation by avoiding having to scan the FAT to discover the last
12172290da1Snatano  * cluster of the file. The cache also helps sequential reads by
12272290da1Snatano  * remembering the last cluster read from the file.  This also prevents us
12372290da1Snatano  * from having to rescan the FAT to find the next cluster to read.  This
12472290da1Snatano  * cache is probably pretty worthless if a file is opened by multiple
12572290da1Snatano  * processes.
12672290da1Snatano  */
12772290da1Snatano #define	FC_SIZE		3	/* number of entries in the cache */
12872290da1Snatano #define	FC_LASTMAP	0	/* entry the last call to pcbmap() resolved
12972290da1Snatano 				 * to */
13072290da1Snatano #define	FC_LASTFC	1	/* entry for the last cluster in the file */
13172290da1Snatano #define	FC_NEXTTOLASTFC	2	/* entry for a close to the last cluster in the file */
13272290da1Snatano 
13372290da1Snatano #define	FCE_EMPTY	0xffffffff	/* doesn't represent an actual cluster # */
13472290da1Snatano 
13572290da1Snatano /*
13672290da1Snatano  * Set a slot in the FAT cache.
13772290da1Snatano  */
13872290da1Snatano #define	fc_setcache(dep, slot, frcn, fsrcn) \
13972290da1Snatano 	(dep)->de_fc[slot].fc_frcn = frcn; \
14072290da1Snatano 	(dep)->de_fc[slot].fc_fsrcn = fsrcn;
14172290da1Snatano 
14272290da1Snatano #define fc_last_to_nexttolast(dep) \
14372290da1Snatano 	do {  \
14472290da1Snatano 		(dep)->de_fc[FC_NEXTTOLASTFC].fc_frcn = (dep)->de_fc[FC_LASTFC].fc_frcn; \
14572290da1Snatano 		(dep)->de_fc[FC_NEXTTOLASTFC].fc_fsrcn = (dep)->de_fc[FC_LASTFC].fc_fsrcn; \
14672290da1Snatano 	} while (0)
14772290da1Snatano 
14872290da1Snatano 
14972290da1Snatano /*
15072290da1Snatano  * This is the in memory variant of a dos directory entry.  It is usually
15172290da1Snatano  * contained within a mkfsvnode.
15272290da1Snatano  */
15372290da1Snatano struct denode_key {
15472290da1Snatano 	u_long dk_dirclust;	/* cluster of the directory file containing this entry */
15572290da1Snatano 	u_long dk_diroffset;	/* offset of this entry in the directory cluster */
15672290da1Snatano 	void *dk_dirgen;	/* non zero and unique for unlinked nodes */
15772290da1Snatano };
15872290da1Snatano struct denode {
15972290da1Snatano 	struct genfs_node de_gnode;
16072290da1Snatano 	struct mkfsvnode *de_mkfsvnode;	/* addr of mkfsvnode we are part of */
16172290da1Snatano 	struct mkfsvnode *de_devvp;	/* mkfsvnode of blk dev we live on */
16272290da1Snatano 	u_long de_flag;		/* flag bits */
16372290da1Snatano 	dev_t de_dev;		/* device where direntry lives */
16472290da1Snatano 	struct denode_key de_key;
16572290da1Snatano #define de_dirclust de_key.dk_dirclust
16672290da1Snatano #define de_diroffset de_key.dk_diroffset
16772290da1Snatano #define de_dirgen de_key.dk_dirgen
16872290da1Snatano 	u_long de_fndoffset;	/* offset of found dir entry */
16972290da1Snatano 	int de_fndcnt;		/* number of slots before de_fndoffset */
17072290da1Snatano 	long de_refcnt;		/* reference count */
17172290da1Snatano 	struct msdosfsmount *de_pmp;	/* addr of our mount struct */
17272290da1Snatano 	struct lockf *de_lockf;	/* byte level lock list */
17372290da1Snatano 	u_char de_Name[12];	/* name, from DOS directory entry */
17472290da1Snatano 	u_char de_Attributes;	/* attributes, from directory entry */
17572290da1Snatano 	u_char de_CHun;		/* Hundredth of second of CTime*/
17672290da1Snatano 	u_short de_CTime;	/* creation time */
17772290da1Snatano 	u_short de_CDate;	/* creation date */
17872290da1Snatano 	u_short de_ADate;	/* access date */
17972290da1Snatano 	u_short de_MTime;	/* modification time */
18072290da1Snatano 	u_short de_MDate;	/* modification date */
18172290da1Snatano 	u_long de_StartCluster; /* starting cluster of file */
18272290da1Snatano 	u_long de_FileSize;	/* size of file in bytes */
18372290da1Snatano 	struct fatcache de_fc[FC_SIZE];	/* FAT cache */
18472290da1Snatano };
18572290da1Snatano 
18672290da1Snatano /*
18772290da1Snatano  * Values for the de_flag field of the denode.
18872290da1Snatano  */
18972290da1Snatano #define	DE_UPDATE	0x0001	/* Modification time update request. */
19072290da1Snatano #define	DE_CREATE	0x0002	/* Creation time update */
19172290da1Snatano #define	DE_ACCESS	0x0004	/* Access time update */
19272290da1Snatano #define	DE_MODIFIED	0x0008	/* Denode has been modified. */
19372290da1Snatano #define	DE_RENAME	0x0010	/* Denode is in the process of being renamed */
19472290da1Snatano 
19572290da1Snatano /*
19672290da1Snatano  * Maximum filename length in Win95
19772290da1Snatano  * Note: Must be < sizeof(dirent.d_name)
19872290da1Snatano  */
19972290da1Snatano #define	WIN_MAXLEN	255
20072290da1Snatano 
20172290da1Snatano /* Maximum size of a file on a FAT filesystem */
20272290da1Snatano #define MSDOSFS_FILESIZE_MAX	0xFFFFFFFFLL
20372290da1Snatano 
20472290da1Snatano /*
20572290da1Snatano  * Transfer directory entries between internal and external form.
20672290da1Snatano  * dep is a struct denode * (internal form),
20772290da1Snatano  * dp is a struct direntry * (external form).
20872290da1Snatano  */
20972290da1Snatano #define DE_INTERNALIZE32(dep, dp)			\
21072290da1Snatano 	 ((dep)->de_StartCluster |= getushort((dp)->deHighClust) << 16)
21172290da1Snatano #define DE_INTERNALIZE(dep, dp)				\
21272290da1Snatano 	(memcpy((dep)->de_Name, (dp)->deName, 11),	\
21372290da1Snatano 	 (dep)->de_Attributes = (dp)->deAttributes,	\
21472290da1Snatano 	 (dep)->de_CHun = (dp)->deCTimeHundredth,	\
21572290da1Snatano 	 (dep)->de_CTime = getushort((dp)->deCTime),	\
21672290da1Snatano 	 (dep)->de_CDate = getushort((dp)->deCDate),	\
21772290da1Snatano 	 (dep)->de_ADate = getushort((dp)->deADate),	\
21872290da1Snatano 	 (dep)->de_MTime = getushort((dp)->deMTime),	\
21972290da1Snatano 	 (dep)->de_MDate = getushort((dp)->deMDate),	\
22072290da1Snatano 	 (dep)->de_StartCluster = getushort((dp)->deStartCluster), \
22172290da1Snatano 	 (dep)->de_FileSize = getulong((dp)->deFileSize), \
22272290da1Snatano 	 (FAT32((dep)->de_pmp) ? DE_INTERNALIZE32((dep), (dp)) : 0))
22372290da1Snatano 
22472290da1Snatano #define DE_EXTERNALIZE32(dp, dep)			\
22572290da1Snatano 	 putushort((dp)->deHighClust, (dep)->de_StartCluster >> 16)
22672290da1Snatano #define DE_EXTERNALIZE16(dp, dep)			\
22772290da1Snatano 	 putushort((dp)->deHighClust, 0)
22872290da1Snatano #define DE_EXTERNALIZE(dp, dep)				\
22972290da1Snatano 	(memcpy((dp)->deName, (dep)->de_Name, 11),	\
23072290da1Snatano 	 (dp)->deAttributes = (dep)->de_Attributes,	\
23172290da1Snatano 	 (dp)->deCTimeHundredth = (dep)->de_CHun,	\
23272290da1Snatano 	 putushort((dp)->deCTime, (dep)->de_CTime),	\
23372290da1Snatano 	 putushort((dp)->deCDate, (dep)->de_CDate),	\
23472290da1Snatano 	 putushort((dp)->deADate, (dep)->de_ADate),	\
23572290da1Snatano 	 putushort((dp)->deMTime, (dep)->de_MTime),	\
23672290da1Snatano 	 putushort((dp)->deMDate, (dep)->de_MDate),	\
23772290da1Snatano 	 putushort((dp)->deStartCluster, (dep)->de_StartCluster), \
23872290da1Snatano 	 putulong((dp)->deFileSize,			\
23972290da1Snatano 	     ((dep)->de_Attributes & ATTR_DIRECTORY) ? 0 : (dep)->de_FileSize), \
24072290da1Snatano 	 (FAT32((dep)->de_pmp) ? DE_EXTERNALIZE32((dp), (dep)) : DE_EXTERNALIZE16((dp), (dep))))
24172290da1Snatano 
24272290da1Snatano #define	de_forw		de_chain[0]
24372290da1Snatano #define	de_back		de_chain[1]
24472290da1Snatano 
24572290da1Snatano 
24672290da1Snatano #define	VTODE(vp)	((struct denode *)(vp)->v_data)
24772290da1Snatano #define	DETOV(de)	((de)->de_mkfsvnode)
24872290da1Snatano 
24972290da1Snatano #define	DETIMES(dep, acc, mod, cre, gmtoff) \
25072290da1Snatano 	while ((dep)->de_flag & (DE_UPDATE | DE_CREATE | DE_ACCESS)) \
25172290da1Snatano 		msdosfs_detimes(dep, acc, mod, cre, gmtoff)
25272290da1Snatano 
25372290da1Snatano /*
25472290da1Snatano  * This overlays the fid structure (see fstypes.h)
25572290da1Snatano  */
25672290da1Snatano struct defid {
25772290da1Snatano 	u_int16_t defid_len;	/* length of structure */
25872290da1Snatano 	u_int16_t defid_pad;	/* force 4-byte alignment */
25972290da1Snatano 
26072290da1Snatano 	u_int32_t defid_dirclust; /* cluster this dir entry came from */
26172290da1Snatano 	u_int32_t defid_dirofs;	/* offset of entry within the cluster */
26272290da1Snatano 	u_int32_t defid_gen;	/* generation number */
26372290da1Snatano };
26472290da1Snatano 
26572290da1Snatano /*
26672290da1Snatano  * Prototypes for MSDOSFS mkfsvnode operations
26772290da1Snatano  */
26872290da1Snatano int	msdosfs_lookup		(void *);
26972290da1Snatano int	msdosfs_create		(void *);
27072290da1Snatano int	msdosfs_close		(void *);
27172290da1Snatano int	msdosfs_access		(void *);
27272290da1Snatano int	msdosfs_getattr		(void *);
27372290da1Snatano int	msdosfs_setattr		(void *);
27472290da1Snatano int	msdosfs_read		(void *);
27572290da1Snatano int	msdosfs_write		(void *);
27672290da1Snatano #define	msdosfs_lease_check	genfs_lease_check
27772290da1Snatano #define	msdosfs_ioctl		genfs_enoioctl
27872290da1Snatano #define	msdosfs_poll		genfs_poll
27972290da1Snatano #define	msdosfs_revoke		genfs_revoke
28072290da1Snatano #define	msdosfs_mmap		genfs_mmap
28172290da1Snatano int	msdosfs_fsync		(void *);
28272290da1Snatano #define	msdosfs_seek		genfs_seek
28372290da1Snatano int	msdosfs_remove		(void *);
28472290da1Snatano int	msdosfs_rename		(void *);
28572290da1Snatano int	msdosfs_mkdir		(void *);
28672290da1Snatano int	msdosfs_rmdir		(void *);
28772290da1Snatano int	msdosfs_readdir		(void *);
28872290da1Snatano #define	msdosfs_abortop		genfs_abortop
28972290da1Snatano int	msdosfs_inactive	(void *);
29072290da1Snatano int	msdosfs_reclaim		(void *);
29172290da1Snatano int	msdosfs_bmap		(void *);
29272290da1Snatano int	msdosfs_strategy	(void *);
29372290da1Snatano int	msdosfs_print		(void *);
29472290da1Snatano int	msdosfs_advlock		(void *);
29572290da1Snatano int	msdosfs_pathconf	(void *);
29672290da1Snatano 
29772290da1Snatano /*
29872290da1Snatano  * Internal service routine prototypes.
29972290da1Snatano  */
30072290da1Snatano struct componentname;
30172290da1Snatano struct direntry;
30272290da1Snatano int msdosfs_update(struct mkfsvnode *, const struct timespec *,
30372290da1Snatano 	    const struct timespec *, int);
30472290da1Snatano int createde(struct denode *, struct denode *,
30572290da1Snatano 		struct denode **, struct componentname *);
30672290da1Snatano int deextend(struct denode *, u_long);
30772290da1Snatano int deget(struct msdosfsmount *, u_long, u_long, struct denode **);
30872290da1Snatano int detrunc(struct denode *, u_long, int);
30972290da1Snatano int deupdat(struct denode *, int);
31072290da1Snatano int readde(struct denode *, struct mkfsbuf **, struct direntry **);
31172290da1Snatano int readep(struct msdosfsmount *, u_long, u_long,
31272290da1Snatano 		struct mkfsbuf **, struct direntry **);
31372290da1Snatano int uniqdosname(struct denode *, struct componentname *, u_char *);
31472290da1Snatano int msdosfs_gop_alloc(struct mkfsvnode *, off_t, off_t, int);
31572290da1Snatano void msdosfs_gop_markupdate(struct mkfsvnode *, int);
31672290da1Snatano void msdosfs_detimes(struct denode *, const struct timespec *,
31772290da1Snatano     const struct timespec *, const struct timespec *, int);
31872290da1Snatano int msdosfs_fh_enter(struct msdosfsmount *, uint32_t, uint32_t, uint32_t *);
31972290da1Snatano int msdosfs_fh_remove(struct msdosfsmount *, uint32_t, uint32_t);
32072290da1Snatano int msdosfs_fh_lookup(struct msdosfsmount *, uint32_t, uint32_t, uint32_t *);
32172290da1Snatano void msdosfs_fh_destroy(struct msdosfsmount *);
32272290da1Snatano #endif /* _MSDOSFS_DENODE_H_ */
323