1139825Simp/*-
21541Srgrimes * Copyright (c) 1982, 1986, 1989, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes * (c) UNIX System Laboratories, Inc.
51541Srgrimes * All or some portions of this file are derived from material licensed
61541Srgrimes * to the University of California by American Telephone and Telegraph
71541Srgrimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with
81541Srgrimes * the permission of UNIX System Laboratories, Inc.
91541Srgrimes *
101541Srgrimes * Redistribution and use in source and binary forms, with or without
111541Srgrimes * modification, are permitted provided that the following conditions
121541Srgrimes * are met:
131541Srgrimes * 1. Redistributions of source code must retain the above copyright
141541Srgrimes *    notice, this list of conditions and the following disclaimer.
151541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
161541Srgrimes *    notice, this list of conditions and the following disclaimer in the
171541Srgrimes *    documentation and/or other materials provided with the distribution.
181541Srgrimes * 4. Neither the name of the University nor the names of its contributors
191541Srgrimes *    may be used to endorse or promote products derived from this software
201541Srgrimes *    without specific prior written permission.
211541Srgrimes *
221541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
231541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
241541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
251541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
261541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
271541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
281541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
291541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
301541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
311541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
321541Srgrimes * SUCH DAMAGE.
331541Srgrimes *
341541Srgrimes *	@(#)dir.h	8.2 (Berkeley) 1/21/94
3550477Speter * $FreeBSD$
361541Srgrimes */
371541Srgrimes
382177Spaul#ifndef _UFS_UFS_DIR_H_
392177Spaul#define	_UFS_UFS_DIR_H_
401541Srgrimes
411541Srgrimes/*
4222521Sdyson * Theoretically, directories can be more than 2Gb in length, however, in
4322521Sdyson * practice this seems unlikely. So, we define the type doff_t as a 32-bit
4422521Sdyson * quantity to keep down the cost of doing lookup on a 32-bit machine.
4522521Sdyson */
4622521Sdyson#define	doff_t		int32_t
47262780Spfg#define	MAXDIRSIZE	(0x7fffffff)
4822521Sdyson
4922521Sdyson/*
501541Srgrimes * A directory consists of some number of blocks of DIRBLKSIZ
511541Srgrimes * bytes, where DIRBLKSIZ is chosen such that it can be transferred
521541Srgrimes * to disk in a single atomic operation (e.g. 512 bytes on most machines).
531541Srgrimes *
541541Srgrimes * Each DIRBLKSIZ byte block contains some number of directory entry
551541Srgrimes * structures, which are of variable length.  Each directory entry has
561541Srgrimes * a struct direct at the front of it, containing its inode number,
571541Srgrimes * the length of the entry, and the length of the name contained in
581541Srgrimes * the entry.  These are followed by the name padded to a 4 byte boundary
591541Srgrimes * with null bytes.  All names are guaranteed null terminated.
601541Srgrimes * The maximum length of a name in a directory is MAXNAMLEN.
611541Srgrimes *
621541Srgrimes * The macro DIRSIZ(fmt, dp) gives the amount of space required to represent
631541Srgrimes * a directory entry.  Free space in a directory is represented by
641541Srgrimes * entries which have dp->d_reclen > DIRSIZ(fmt, dp).  All DIRBLKSIZ bytes
651541Srgrimes * in a directory block are claimed by the directory entries.  This
661541Srgrimes * usually results in the last entry in a directory having a large
671541Srgrimes * dp->d_reclen.  When entries are deleted from a directory, the
681541Srgrimes * space is returned to the previous entry in the same directory
691541Srgrimes * block by increasing its dp->d_reclen.  If the first entry of
701541Srgrimes * a directory block is free, then its dp->d_ino is set to 0.
711541Srgrimes * Entries other than the first in a directory do not normally have
721541Srgrimes * dp->d_ino set to 0.
731541Srgrimes */
74262780Spfg#define	DIRBLKSIZ	DEV_BSIZE
751541Srgrimes#define	MAXNAMLEN	255
761541Srgrimes
771541Srgrimesstruct	direct {
7822521Sdyson	u_int32_t d_ino;		/* inode number of entry */
7922521Sdyson	u_int16_t d_reclen;		/* length of this record */
8022521Sdyson	u_int8_t  d_type; 		/* file type, see below */
8122521Sdyson	u_int8_t  d_namlen;		/* length of string in d_name */
8222521Sdyson	char	  d_name[MAXNAMLEN + 1];/* name with length <= MAXNAMLEN */
831541Srgrimes};
841541Srgrimes
851541Srgrimes/*
861541Srgrimes * File types
871541Srgrimes */
881541Srgrimes#define	DT_UNKNOWN	 0
891541Srgrimes#define	DT_FIFO		 1
901541Srgrimes#define	DT_CHR		 2
911541Srgrimes#define	DT_DIR		 4
921541Srgrimes#define	DT_BLK		 6
931541Srgrimes#define	DT_REG		 8
941541Srgrimes#define	DT_LNK		10
951541Srgrimes#define	DT_SOCK		12
9622521Sdyson#define	DT_WHT		14
971541Srgrimes
981541Srgrimes/*
991541Srgrimes * Convert between stat structure types and directory types.
1001541Srgrimes */
1011541Srgrimes#define	IFTODT(mode)	(((mode) & 0170000) >> 12)
1021541Srgrimes#define	DTTOIF(dirtype)	((dirtype) << 12)
1031541Srgrimes
1041541Srgrimes/*
1051541Srgrimes * The DIRSIZ macro gives the minimum record length which will hold
1061541Srgrimes * the directory entry.  This requires the amount of space in struct direct
1071541Srgrimes * without the d_name field, plus enough space for the name with a terminating
1081541Srgrimes * null byte (dp->d_namlen+1), rounded up to a 4 byte boundary.
10910358Sjulian *
11010358Sjulian *
1111541Srgrimes */
11210358Sjulian#define	DIRECTSIZ(namlen)						\
113171147Speter	(((uintptr_t)&((struct direct *)0)->d_name +			\
11410358Sjulian	  ((namlen)+1)*sizeof(((struct direct *)0)->d_name[0]) + 3) & ~3)
1151541Srgrimes#if (BYTE_ORDER == LITTLE_ENDIAN)
116262780Spfg#define	DIRSIZ(oldfmt, dp) \
11710358Sjulian    ((oldfmt) ? DIRECTSIZ((dp)->d_type) : DIRECTSIZ((dp)->d_namlen))
1181541Srgrimes#else
119262780Spfg#define	DIRSIZ(oldfmt, dp) \
12010358Sjulian    DIRECTSIZ((dp)->d_namlen)
1211541Srgrimes#endif
122262780Spfg#define	OLDDIRFMT	1
123262780Spfg#define	NEWDIRFMT	0
1241541Srgrimes
1251541Srgrimes/*
12622521Sdyson * Template for manipulating directories.  Should use struct direct's,
12722521Sdyson * but the name field is MAXNAMLEN - 1, and this just won't do.
1281541Srgrimes */
1291541Srgrimesstruct dirtemplate {
13022521Sdyson	u_int32_t	dot_ino;
13122521Sdyson	int16_t		dot_reclen;
13222521Sdyson	u_int8_t	dot_type;
13322521Sdyson	u_int8_t	dot_namlen;
13422521Sdyson	char		dot_name[4];	/* must be multiple of 4 */
13522521Sdyson	u_int32_t	dotdot_ino;
13622521Sdyson	int16_t		dotdot_reclen;
13722521Sdyson	u_int8_t	dotdot_type;
13822521Sdyson	u_int8_t	dotdot_namlen;
13922521Sdyson	char		dotdot_name[4];	/* ditto */
1401541Srgrimes};
1411541Srgrimes
1421541Srgrimes/*
1431541Srgrimes * This is the old format of directories, sanz type element.
1441541Srgrimes */
1451541Srgrimesstruct odirtemplate {
14622521Sdyson	u_int32_t	dot_ino;
14722521Sdyson	int16_t		dot_reclen;
14822521Sdyson	u_int16_t	dot_namlen;
14922521Sdyson	char		dot_name[4];	/* must be multiple of 4 */
15022521Sdyson	u_int32_t	dotdot_ino;
15122521Sdyson	int16_t		dotdot_reclen;
15222521Sdyson	u_int16_t	dotdot_namlen;
15322521Sdyson	char		dotdot_name[4];	/* ditto */
1541541Srgrimes};
1551541Srgrimes#endif /* !_DIR_H_ */
156