[BACK]Return to xfs_dir.h CVS log [TXT][DIR] Up to [Development] / xfs-linux

File: [Development] / xfs-linux / Attic / xfs_dir.h (download)

Revision 1.3, Sat Feb 26 00:09:04 1994 UTC (23 years, 7 months ago) by wei_hu
Branch: MAIN
Changes since 1.2: +4 -1 lines

Pass bmapi-related context to createname.

#ifndef _FS_XFS_DIR_H
#define	_FS_XFS_DIR_H

#ident	"$Revision: 1.2 $"

/*
 * xfs_dir.h
 *
 * Directory layout, internal structure, access macros, etc.
 *
 * Large directories are structured around Btrees where all the data
 * elements are in the leaf nodes.  Filenames are hashed into an int,
 * then that int is used as the index into the Btree.  Since the hashval
 * of a filename may not be unique, we may have duplicate keys.  The
 * internal links in the Btree are logical block offsets into the file.
 *
 * Small directories use a different format and are packed as tightly
 * as possible so as to fit into the literal area of the inode.
 */

/*========================================================================
 * Directory Structure when smaller than XFS_LITINO(sbp) bytes.
 *========================================================================*/

/*
 * The parent directory has a dedicated field, and the self-pointer must
 * be calculated on the fly.
 *
 * Entries are packed toward the top as tight as possible.  The header
 * and the elements much be bcopy()'d out into a work area to get correct
 * alignment for the inode number fields.
 */
struct xfs_dir_shortform {
	struct xfs_dir_sf_hdr {		/* constant-structure header block */
		__uint8_t parent[8];	/* parent dir inode number */
		__uint8_t count;	/* count of active entries */
	} hdr;
	struct xfs_dir_sf_entry {
		__uint8_t inumber[8];	/* referenced inode number */
		__uint8_t namelen;	/* actual length of name (no NULL) */
		__uint8_t name[1];	/* name */
	} list[1];			/* variable sized array */
};

#define XFS_DIR_SF_ENTSIZE_BYNAME(LEN)		/* space a name uses */ \
	(sizeof(struct xfs_dir_sf_entry)-1 + (LEN))
#define XFS_DIR_SF_ENTSIZE_BYENTRY(SFEP)	/* space an entry uses */ \
	(sizeof(struct xfs_dir_sf_entry)-1 + (SFEP)->namelen)
#define XFS_DIR_SF_NEXTENTRY(SFEP)		/* next entry in struct */ \
	((struct xfs_dir_sf_entry *) \
		((char *)(SFEP) + XFS_DIR_SF_ENTSIZE_BYENTRY(SFEP)))
#define XFS_DIR_SF_ALLFIT(COUNT, TOTALLEN)	/* will all entries fit? */ \
	(sizeof(struct xfs_dir_sf_hdr) + \
	       (sizeof(struct xfs_dir_sf_entry)-1)*(COUNT) + (TOTALLEN))

/*========================================================================
 * Directory Structure when equal to XFS_LBSIZE(sbp) bytes.
 *========================================================================*/

/*
 * This structure is common to both leaf nodes and non-leaf nodes in the Btree.
 *
 * Is is used to manage a doubly linked list of all blocks at the same
 * level in the Btree, and to identify which type of block this is.
 */
#define XFS_DIR_LEAF_MAGIC	0xfeeb	/* magic number for leaf blocks */
#define XFS_DIR_NODE_MAGIC	0xfebe	/* magic number for non-leaf blocks */
#define XFS_DIR_FREE_MAGIC	0xbefe	/* magic number for free blocks */

struct xfs_dir_blkinfo {
	__uint32_t forw;			/* previous block in list */
	__uint32_t back;			/* following block in list */
	__uint16_t magic;			/* validity check on block */
};

/*
 * This is the structure of the leaf nodes in the Btree.
 *
 * Struct leaf_entry's are packed from the top.  Names grow from the bottom
 * but are not packed.  The freemap contains run-length-encoded entries
 * for the free bytes after the leaf_entry's, but only the N largest such,
 * smaller runs are dropped.  When the freemap doesn't show enough space
 * for an allocation, we compact the namelist area and try again.  If we
 * still don't have enough space, then we have to split the block.
 *
 * Since we have duplicate hash keys, for each key that matches, compare
 * the actual string.  The root and intermediate node search always takes
 * the first-in-the-block key match found, so we should only have to work
 * "forw"ard.  If none matches, continue with the "forw"ard leaf nodes
 * until the hash key changes or the filename is found.
 *
 * The parent directory and the self-pointer are explicitly represented
 * (ie: there are entries for "." and "..").
 *
 * Note that the count being a __uint16_t limits us to something like a 
 * blocksize of 1.3MB in the face of worst case (short) filenames.
 */
#define XFS_DIR_LEAF_MAPSIZE	3	/* how many freespace slots */

struct xfs_dir_leafblock {
	struct xfs_dir_leaf_hdr {	/* constant-structure header block */
		struct xfs_dir_blkinfo info;	/* block type, links, etc. */
		__uint16_t count;	/* count of active leaf_entry's */
		__uint16_t namebytes;	/* num bytes of name strings stored */
		__uint16_t firstused;	/* first used byte in name area */
		__uint8_t  holes;	/* != 0 if blk needs compaction */
		__uint8_t  pad1;
		struct xfs_dir_leaf_map {/* RLE map of free bytes */
			__uint16_t base; /* base of free region */
			__uint16_t size; /* run length of free region */
		} freemap[XFS_DIR_LEAF_MAPSIZE]; /* N largest free regions */
	} hdr;
	struct xfs_dir_leaf_entry {	/* sorted on key, not name */
		__uint32_t hashval;	/* hash value of name */
		__uint16_t nameidx;	/* index into buffer of name */
		__uint16_t pad2;
	} leaves[1];			/* var sized array */
	struct xfs_dir_leaf_name {
		__uint8_t inumber[8];	/* inode number for this key */
		__uint8_t namelen;	/* length of name string */
		__uint8_t name[1];	/* name string itself */
	} namelist[1];			/* grows from bottom of buf */
};

#define XFS_DIR_LEAF_ENTSIZE_BYNAME(LEN)	/* space a name will use */ \
	(sizeof(struct xfs_dir_leaf_name)-1 + LEN)
#define XFS_DIR_LEAF_ENTSIZE_BYENTRY(NAMEENT)	/* space an entry will use */ \
	(sizeof(struct xfs_dir_leaf_name)-1 + (NAMEENT)->namelen)
#define XFS_DIR_LEAF_NAMESTRUCT(LEAFP, OFFSET)	/* point to name struct */ \
	((struct xfs_dir_leaf_name *)&((char *)(LEAFP))[OFFSET])

/*========================================================================
 * Directory Structure when greater than XFS_LBSIZE(sbp) bytes.
 *========================================================================*/

/*
 * This is the structure of the root and intermediate nodes in the Btree.
 * The leaf nodes are defined above.
 *
 * Entries are not packed.
 *
 * Since we have duplicate keys, use a binary search but always follow
 * all match in the block, not just the first match found.
 *
 * The root node only has the hdr.freechain and hdr.freeblks fields set.
 * When directory blocks in the middle of the address space are freed,
 * they are attached to the head of this list and the hdr.freeblks count
 * is incremented.  When the count gets up to XFS_DIR_FREE_CLEANUP, we
 * will garbage collect.
 */
#define	XFS_DIR_NODE_MAXDEPTH	5	/* max depth of Btree */

struct xfs_dir_intnode {
	struct xfs_dir_node_hdr {	/* constant-structure header block */
		struct xfs_dir_blkinfo info;	/* block type, links, etc. */
		__uint16_t count;	/* count of active entries */
		__uint8_t  level;	/* level above leaves (leaf == 0) */
#ifdef GROT
		__uint16_t freecount;	/* num blks in freechain (root only) */
		__uint32_t freechain;	/* free block chain (root node only) */
		__uint8_t  level;	/* level above leaves (leaf == 0) */
		__uint8_t  pad1[5];
#endif
	} hdr;
	struct xfs_dir_node_entry {
		__uint32_t hashval;	/* hash value for this descendant */
		__uint32_t before;	/* Btree block before this key */
	} btree[1];			/* variable sized array of keys */
};

#define XFS_DIR_NODE_ENTSIZE_BYNAME()	/* space a name uses */ \
	(sizeof(struct xfs_dir_node_entry))
#define XFS_DIR_NODE_ENTRIES(sbp)	/* how many entries in this block? */ \
	((XFS_LBSIZE(sbp) - sizeof(struct xfs_dir_node_hdr)) \
		   / sizeof(struct xfs_dir_node_entry))
#define XFS_DIR_MAXBLK		0x10000000	/* max hash value */

/*
 * The contents of a free block in the directory.
 *
 * Those directory blocks that are no longer in use, but are in the
 * middle of the address space, have this format.  When the number of
 * such blocks gets up to XFS_DIR_FREE_CLEANUP, we will shuffle the
 * active blocks around in the address space so as to compress out
 * the unused blocks.  Once they are at the end of the address space,
 * the address space is truncated.
 */
#define XFS_DIR_FREE_CLEANUP	8	/* max free blks before compress */

struct xfs_dir_freeblock {
	struct xfs_dir_free_hdr {	/* constant-structure header block */
		struct xfs_dir_blkinfo info;	/* block type, links, etc. */
		__uint16_t pad;
	} hdr;
	__uint32_t free[1];		/* partial list of free blocks */
};

#define XFS_DIR_FREE_ENTRIES(sbp)	/* how many entries in this block? */ \
	((XFS_LBSIZE(sbp) - sizeof(struct xfs_dir_free_hdr)) \
		   / sizeof(__uint32_t))

/*
 * Macros used by directory code to interface to the filesystem.
 */
#define	XFS_LBSIZE(sbp)	((sbp)->sb_blocksize)
#define	XFS_LBLOG(sbp)	((sbp)->sb_blocklog)
#define	XFS_LITINO(sbp)	((sbp)->sb_inodesize - sizeof(xfs_dinode_core_t))


/*========================================================================
 * Function prototypes for the kernel.
 *========================================================================*/

struct xfs_dir_name;

/*
 * Internal routines when dirsize < XFS_LITINO(sbp).
 */
int	xfs_dir_shortform_create(xfs_trans_t *trans, xfs_inode_t *dp,
					xfs_ino_t parent_inumber);
int	xfs_dir_shortform_addname(xfs_trans_t *trans, struct xfs_dir_name *add);
int	xfs_dir_shortform_removename(xfs_trans_t *trans,
					struct xfs_dir_name *remove);
int	xfs_dir_shortform_lookup(xfs_trans_t *trans, struct xfs_dir_name *args);
int	xfs_dir_shortform_to_leaf(xfs_trans_t *trans, xfs_inode_t *dp);

/*
 * Internal routines when dirsize == XFS_LBSIZE(sbp).
 */
buf_t	*xfs_dir_leaf_create(xfs_trans_t *trans, xfs_inode_t *dp,
				xfs_fsblock_t which_block);
int	xfs_dir_leaf_addname(xfs_trans_t *trans, struct xfs_dir_name *args);
int	xfs_dir_leaf_removename(xfs_trans_t *trans, struct xfs_dir_name *args,
				int *number_entries, int *total_namebytes);
int	xfs_dir_leaf_lookup(xfs_trans_t *trans, struct xfs_dir_name *args);
int	xfs_dir_leaf_to_shortform(xfs_trans_t *trans, xfs_inode_t *dp);
int	xfs_dir_leaf_to_node(xfs_trans_t *trans, xfs_inode_t *dp);

/*
 * Internal routines when dirsize > XFS_LBSIZE(sbp).
 */
buf_t	*xfs_dir_node_create(xfs_trans_t *trans, xfs_inode_t *dp,
				xfs_fsblock_t which_block, int leaf_block_next);
int	xfs_dir_node_addname(xfs_trans_t *trans, struct xfs_dir_name *args);
int	xfs_dir_node_removename(xfs_trans_t *trans, struct xfs_dir_name *args);
int	xfs_dir_node_lookup(xfs_trans_t *trans, struct xfs_dir_name *args);
int	xfs_dir_node_to_leaf(xfs_trans_t *trans, xfs_inode_t *dp);

/*
 * Utility routines.
 */
uint	xfs_dir_hashname(char *name_string, int name_length);
int	xfs_dir_grow_inode(xfs_trans_t *trans, xfs_inode_t *dp,
				int delta_in_bytes,
				xfs_fsblock_t *last_logblk_in_inode);
int	xfs_dir_shrink_inode(xfs_trans_t *trans, xfs_inode_t *dp,
				int delta_in_bytes,
				xfs_fsblock_t *last_logblk_in_inode);
buf_t	*xfs_dir_get_buf(xfs_trans_t *trans, xfs_inode_t *dp,
				xfs_fsblock_t bno);
buf_t	*xfs_dir_read_buf(xfs_trans_t *trans, xfs_inode_t *dp,
				xfs_fsblock_t bno);

/*
 * Overall external interface routines.
 */
int	xfs_dir_isempty(xfs_inode_t *dp);

int	xfs_dir_init(xfs_trans_t *trans,
		     xfs_inode_t *dir,
		     xfs_inode_t *parent_dir);

int	xfs_dir_createname(xfs_trans_t *trans,
			   xfs_fsblock_t *first_block,
			   xfs_extlen_t total,
			   xfs_bmap_free_t *free_list,
			   xfs_inode_t *dp,
			   char *name_string,
			   xfs_ino_t inode_number);

int	xfs_dir_removename(xfs_trans_t *trans,
			   xfs_inode_t *dp,
			   char *name_string);

int	xfs_dir_lookup(xfs_trans_t *tp,
		       xfs_inode_t *dp,
		       char *name_string,
		       int name_length,
		       xfs_ino_t *inode_number);

int	xfs_dir_getdents(void);

#endif	/* !_FS_XFS_DIR_H */