Merge branch 'vendor/GCC50'
[dragonfly.git] / sys / gnu / vfs / ext2fs / dir.h
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  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by the University of
21  *      California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *      @(#)dir.h       8.2 (Berkeley) 1/21/94
39  * $FreeBSD: src/sys/ufs/ufs/dir.h,v 1.9 1999/08/28 00:52:27 peter Exp $
40  */
41
42 #ifndef _VFS_GNU_EXT2FS_DIR_H_
43 #define _VFS_GNU_EXT2FS_DIR_H_
44
45 #include "ext2_fs.h"
46
47 /*
48  * Theoretically, directories can be more than 2Gb in length, however, in
49  * practice this seems unlikely. So, we define the type doff_t as a 32-bit
50  * quantity to keep down the cost of doing lookup on a 32-bit machine.
51  */
52 #define doff_t          int32_t
53 #define MAXDIRSIZE      (0x7fffffff)
54
55 /*
56  * A directory consists of some number of blocks of DIRBLKSIZ
57  * bytes, where DIRBLKSIZ is chosen such that it can be transferred
58  * to disk in a single atomic operation (e.g. 512 bytes on most machines).
59  *
60  * Each DIRBLKSIZ byte block contains some number of directory entry
61  * structures, which are of variable length.  Each directory entry has
62  * a struct direct at the front of it, containing its inode number,
63  * the length of the entry, and the length of the name contained in
64  * the entry.  These are followed by the name padded to a 4 byte boundary
65  * with null bytes.  All names are guaranteed null terminated.
66  * The maximum length of a name in a directory is MAXNAMLEN.
67  *
68  * The macro DIRSIZ(fmt, dp) gives the amount of space required to represent
69  * a directory entry.  Free space in a directory is represented by
70  * entries which have dp->d_reclen > DIRSIZ(fmt, dp).  All DIRBLKSIZ bytes
71  * in a directory block are claimed by the directory entries.  This
72  * usually results in the last entry in a directory having a large
73  * dp->d_reclen.  When entries are deleted from a directory, the
74  * space is returned to the previous entry in the same directory
75  * block by increasing its dp->d_reclen.  If the first entry of
76  * a directory block is free, then its dp->d_ino is set to 0.
77  * Entries other than the first in a directory do not normally have
78  * dp->d_ino set to 0.
79  */
80 #define DIRBLKSIZ       DEV_BSIZE
81 #define MAXNAMLEN       EXT2_NAME_LEN
82
83 struct  direct {
84         uint32_t d_ino;         /* inode number of entry */
85         uint16_t d_reclen;              /* length of this record */
86         uint8_t  d_type;                /* file type, see below */
87         uint8_t  d_namlen;              /* length of string in d_name */
88         char      d_name[MAXNAMLEN + 1];/* name with length <= MAXNAMLEN */
89 };
90
91 /*
92  * File types
93  */
94 #define DT_UNKNOWN       0
95 #define DT_FIFO          1
96 #define DT_CHR           2
97 #define DT_DIR           4
98 #define DT_BLK           6
99 #define DT_REG           8
100 #define DT_LNK          10
101 #define DT_SOCK         12
102 #define DT_WHT          14
103
104 /*
105  * Convert between stat structure types and directory types.
106  */
107 #define IFTODT(mode)    (((mode) & 0170000) >> 12)
108 #define DTTOIF(dirtype) ((dirtype) << 12)
109
110 /*
111  * The DIRSIZ macro gives the minimum record length which will hold
112  * the directory entry.  This requires the amount of space in struct direct
113  * without the d_name field, plus enough space for the name with a terminating
114  * null byte (dp->d_namlen+1), rounded up to a 4 byte boundary.
115  *
116  *
117  */
118 #define DIRECTSIZ(namlen)                                               \
119         ((__offsetof(struct direct, d_name) +                           \
120           ((namlen)+1)*sizeof(((struct direct *)0)->d_name[0]) + 3) & ~3)
121 #if (BYTE_ORDER == LITTLE_ENDIAN)
122 #define DIRSIZ(oldfmt, dp) \
123     ((oldfmt) ? DIRECTSIZ((dp)->d_type) : DIRECTSIZ((dp)->d_namlen))
124 #else
125 #define DIRSIZ(oldfmt, dp) \
126     DIRECTSIZ((dp)->d_namlen)
127 #endif
128 #define OLDDIRFMT       1
129 #define NEWDIRFMT       0
130
131 /*
132  * Template for manipulating directories.  Should use struct direct's,
133  * but the name field is MAXNAMLEN - 1, and this just won't do.
134  */
135 struct dirtemplate {
136         uint32_t        dot_ino;
137         int16_t         dot_reclen;
138         uint8_t dot_type;
139         uint8_t dot_namlen;
140         char            dot_name[4];    /* must be multiple of 4 */
141         uint32_t        dotdot_ino;
142         int16_t         dotdot_reclen;
143         uint8_t dotdot_type;
144         uint8_t dotdot_namlen;
145         char            dotdot_name[4]; /* ditto */
146 };
147
148 /*
149  * This is the old format of directories, sanz type element.
150  */
151 struct odirtemplate {
152         uint32_t        dot_ino;
153         int16_t         dot_reclen;
154         uint16_t        dot_namlen;
155         char            dot_name[4];    /* must be multiple of 4 */
156         uint32_t        dotdot_ino;
157         int16_t         dotdot_reclen;
158         uint16_t        dotdot_namlen;
159         char            dotdot_name[4]; /* ditto */
160 };
161 #endif /* _VFS_GNU_EXT2FS_DIR_H_ */