xref: /original-bsd/sys/ufs/ufs/dir.h (revision 0842ddeb)
1 /*
2  * Copyright (c) 1982, 1986, 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * %sccs.include.redist.c%
11  *
12  *	@(#)dir.h	8.5 (Berkeley) 04/27/95
13  */
14 
15 #ifndef _DIR_H_
16 #define	_DIR_H_
17 
18 /*
19  * Theoretically, directories can be more than 2Gb in length, however, in
20  * practice this seems unlikely. So, we define the type doff_t as a 32-bit
21  * quantity to keep down the cost of doing lookup on a 32-bit machine.
22  */
23 #define	doff_t		int32_t
24 #define MAXDIRSIZE	(0x7fffffff)
25 
26 /*
27  * A directory consists of some number of blocks of DIRBLKSIZ
28  * bytes, where DIRBLKSIZ is chosen such that it can be transferred
29  * to disk in a single atomic operation (e.g. 512 bytes on most machines).
30  *
31  * Each DIRBLKSIZ byte block contains some number of directory entry
32  * structures, which are of variable length.  Each directory entry has
33  * a struct direct at the front of it, containing its inode number,
34  * the length of the entry, and the length of the name contained in
35  * the entry.  These are followed by the name padded to a 4 byte boundary
36  * with null bytes.  All names are guaranteed null terminated.
37  * The maximum length of a name in a directory is MAXNAMLEN.
38  *
39  * The macro DIRSIZ(fmt, dp) gives the amount of space required to represent
40  * a directory entry.  Free space in a directory is represented by
41  * entries which have dp->d_reclen > DIRSIZ(fmt, dp).  All DIRBLKSIZ bytes
42  * in a directory block are claimed by the directory entries.  This
43  * usually results in the last entry in a directory having a large
44  * dp->d_reclen.  When entries are deleted from a directory, the
45  * space is returned to the previous entry in the same directory
46  * block by increasing its dp->d_reclen.  If the first entry of
47  * a directory block is free, then its dp->d_ino is set to 0.
48  * Entries other than the first in a directory do not normally have
49  * dp->d_ino set to 0.
50  */
51 #define DIRBLKSIZ	DEV_BSIZE
52 #define	MAXNAMLEN	255
53 
54 struct	direct {
55 	u_int32_t d_ino;		/* inode number of entry */
56 	u_int16_t d_reclen;		/* length of this record */
57 	u_int8_t  d_type; 		/* file type, see below */
58 	u_int8_t  d_namlen;		/* length of string in d_name */
59 	char	  d_name[MAXNAMLEN + 1];/* name with length <= MAXNAMLEN */
60 };
61 
62 /*
63  * File types
64  */
65 #define	DT_UNKNOWN	 0
66 #define	DT_FIFO		 1
67 #define	DT_CHR		 2
68 #define	DT_DIR		 4
69 #define	DT_BLK		 6
70 #define	DT_REG		 8
71 #define	DT_LNK		10
72 #define	DT_SOCK		12
73 #define	DT_WHT		14
74 
75 /*
76  * Convert between stat structure types and directory types.
77  */
78 #define	IFTODT(mode)	(((mode) & 0170000) >> 12)
79 #define	DTTOIF(dirtype)	((dirtype) << 12)
80 
81 /*
82  * The DIRSIZ macro gives the minimum record length which will hold
83  * the directory entry.  This requires the amount of space in struct direct
84  * without the d_name field, plus enough space for the name with a terminating
85  * null byte (dp->d_namlen+1), rounded up to a 4 byte boundary.
86  */
87 #if (BYTE_ORDER == LITTLE_ENDIAN)
88 #define DIRSIZ(oldfmt, dp) \
89     ((oldfmt) ? \
90     ((sizeof(struct direct) - (MAXNAMLEN+1)) + (((dp)->d_type+1 + 3) &~ 3)) : \
91     ((sizeof(struct direct) - (MAXNAMLEN+1)) + (((dp)->d_namlen+1 + 3) &~ 3)))
92 #else
93 #define DIRSIZ(oldfmt, dp) \
94     ((sizeof(struct direct) - (MAXNAMLEN+1)) + (((dp)->d_namlen+1 + 3) &~ 3))
95 #endif
96 #define OLDDIRFMT	1
97 #define NEWDIRFMT	0
98 
99 /*
100  * Template for manipulating directories.  Should use struct direct's,
101  * but the name field is MAXNAMLEN - 1, and this just won't do.
102  */
103 struct dirtemplate {
104 	u_int32_t	dot_ino;
105 	int16_t		dot_reclen;
106 	u_int8_t	dot_type;
107 	u_int8_t	dot_namlen;
108 	char		dot_name[4];	/* must be multiple of 4 */
109 	u_int32_t	dotdot_ino;
110 	int16_t		dotdot_reclen;
111 	u_int8_t	dotdot_type;
112 	u_int8_t	dotdot_namlen;
113 	char		dotdot_name[4];	/* ditto */
114 };
115 
116 /*
117  * This is the old format of directories, sanz type element.
118  */
119 struct odirtemplate {
120 	u_int32_t	dot_ino;
121 	int16_t		dot_reclen;
122 	u_int16_t	dot_namlen;
123 	char		dot_name[4];	/* must be multiple of 4 */
124 	u_int32_t	dotdot_ino;
125 	int16_t		dotdot_reclen;
126 	u_int16_t	dotdot_namlen;
127 	char		dotdot_name[4];	/* ditto */
128 };
129 #endif /* !_DIR_H_ */
130