blob: 133464935ef1911faca5fb961d822c13526f6035 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 2000-2001 Silicon Graphics, Inc. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22 *
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31 */
32#ifndef __XFS_DIR2_SF_H__
33#define __XFS_DIR2_SF_H__
34
35/*
36 * Directory layout when stored internal to an inode.
37 *
38 * Small directories are packed as tightly as possible so as to
39 * fit into the literal area of the inode.
40 */
41
42struct uio;
43struct xfs_dabuf;
44struct xfs_da_args;
45struct xfs_dir2_block;
46struct xfs_inode;
47struct xfs_mount;
48struct xfs_trans;
49
50/*
51 * Maximum size of a shortform directory.
52 */
53#define XFS_DIR2_SF_MAX_SIZE \
54 (XFS_DINODE_MAX_SIZE - (uint)sizeof(xfs_dinode_core_t) - \
55 (uint)sizeof(xfs_agino_t))
56
57/*
58 * Inode number stored as 8 8-bit values.
59 */
60typedef struct { __uint8_t i[8]; } xfs_dir2_ino8_t;
61
62/*
63 * Inode number stored as 4 8-bit values.
64 * Works a lot of the time, when all the inode numbers in a directory
65 * fit in 32 bits.
66 */
67typedef struct { __uint8_t i[4]; } xfs_dir2_ino4_t;
68
69typedef union {
70 xfs_dir2_ino8_t i8;
71 xfs_dir2_ino4_t i4;
72} xfs_dir2_inou_t;
73#define XFS_DIR2_MAX_SHORT_INUM ((xfs_ino_t)0xffffffffULL)
74
75/*
76 * Normalized offset (in a data block) of the entry, really xfs_dir2_data_off_t.
77 * Only need 16 bits, this is the byte offset into the single block form.
78 */
79typedef struct { __uint8_t i[2]; } xfs_dir2_sf_off_t;
80
81/*
82 * The parent directory has a dedicated field, and the self-pointer must
83 * be calculated on the fly.
84 *
85 * Entries are packed toward the top as tightly as possible. The header
86 * and the elements must be memcpy'd out into a work area to get correct
87 * alignment for the inode number fields.
88 */
89typedef struct xfs_dir2_sf_hdr {
90 __uint8_t count; /* count of entries */
91 __uint8_t i8count; /* count of 8-byte inode #s */
92 xfs_dir2_inou_t parent; /* parent dir inode number */
93} xfs_dir2_sf_hdr_t;
94
95typedef struct xfs_dir2_sf_entry {
96 __uint8_t namelen; /* actual name length */
97 xfs_dir2_sf_off_t offset; /* saved offset */
98 __uint8_t name[1]; /* name, variable size */
99 xfs_dir2_inou_t inumber; /* inode number, var. offset */
100} xfs_dir2_sf_entry_t;
101
102typedef struct xfs_dir2_sf {
103 xfs_dir2_sf_hdr_t hdr; /* shortform header */
104 xfs_dir2_sf_entry_t list[1]; /* shortform entries */
105} xfs_dir2_sf_t;
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107#define XFS_DIR2_SF_HDR_SIZE(i8count) xfs_dir2_sf_hdr_size(i8count)
Nathan Scotta844f452005-11-02 14:38:42 +1100108static inline int xfs_dir2_sf_hdr_size(int i8count)
109{
110 return ((uint)sizeof(xfs_dir2_sf_hdr_t) - \
111 ((i8count) == 0) * \
112 ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t)));
113}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115#define XFS_DIR2_SF_INUMBERP(sfep) xfs_dir2_sf_inumberp(sfep)
Nathan Scotta844f452005-11-02 14:38:42 +1100116static inline xfs_dir2_inou_t *xfs_dir2_sf_inumberp(xfs_dir2_sf_entry_t *sfep)
117{
118 return (xfs_dir2_inou_t *)&(sfep)->name[(sfep)->namelen];
119}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Nathan Scotta844f452005-11-02 14:38:42 +1100121#define XFS_DIR2_SF_GET_INUMBER(sfp, from) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 xfs_dir2_sf_get_inumber(sfp, from)
Nathan Scotta844f452005-11-02 14:38:42 +1100123static inline xfs_intino_t
124xfs_dir2_sf_get_inumber(xfs_dir2_sf_t *sfp, xfs_dir2_inou_t *from)
125{
126 return ((sfp)->hdr.i8count == 0 ? \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 (xfs_intino_t)XFS_GET_DIR_INO4((from)->i4) : \
Nathan Scotta844f452005-11-02 14:38:42 +1100128 (xfs_intino_t)XFS_GET_DIR_INO8((from)->i8));
129}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Nathan Scotta844f452005-11-02 14:38:42 +1100131#define XFS_DIR2_SF_PUT_INUMBER(sfp,from,to) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 xfs_dir2_sf_put_inumber(sfp,from,to)
Nathan Scotta844f452005-11-02 14:38:42 +1100133static inline void xfs_dir2_sf_put_inumber(xfs_dir2_sf_t *sfp, xfs_ino_t *from,
134 xfs_dir2_inou_t *to)
135{
136 if ((sfp)->hdr.i8count == 0)
137 XFS_PUT_DIR_INO4(*(from), (to)->i4);
138 else
139 XFS_PUT_DIR_INO8(*(from), (to)->i8);
140}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142#define XFS_DIR2_SF_GET_OFFSET(sfep) \
143 xfs_dir2_sf_get_offset(sfep)
Nathan Scotta844f452005-11-02 14:38:42 +1100144static inline xfs_dir2_data_aoff_t
145xfs_dir2_sf_get_offset(xfs_dir2_sf_entry_t *sfep)
146{
147 return INT_GET_UNALIGNED_16_BE(&(sfep)->offset.i);
148}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150#define XFS_DIR2_SF_PUT_OFFSET(sfep,off) \
151 xfs_dir2_sf_put_offset(sfep,off)
Nathan Scotta844f452005-11-02 14:38:42 +1100152static inline void
153xfs_dir2_sf_put_offset(xfs_dir2_sf_entry_t *sfep, xfs_dir2_data_aoff_t off)
154{
155 INT_SET_UNALIGNED_16_BE(&(sfep)->offset.i, off);
156}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158#define XFS_DIR2_SF_ENTSIZE_BYNAME(sfp,len) \
159 xfs_dir2_sf_entsize_byname(sfp,len)
Nathan Scotta844f452005-11-02 14:38:42 +1100160static inline int xfs_dir2_sf_entsize_byname(xfs_dir2_sf_t *sfp, int len)
161{
162 return ((uint)sizeof(xfs_dir2_sf_entry_t) - 1 + (len) - \
163 ((sfp)->hdr.i8count == 0) * \
164 ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t)));
165}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167#define XFS_DIR2_SF_ENTSIZE_BYENTRY(sfp,sfep) \
168 xfs_dir2_sf_entsize_byentry(sfp,sfep)
Nathan Scotta844f452005-11-02 14:38:42 +1100169static inline int
170xfs_dir2_sf_entsize_byentry(xfs_dir2_sf_t *sfp, xfs_dir2_sf_entry_t *sfep)
171{
172 return ((uint)sizeof(xfs_dir2_sf_entry_t) - 1 + (sfep)->namelen - \
173 ((sfp)->hdr.i8count == 0) * \
174 ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t)));
175}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177#define XFS_DIR2_SF_FIRSTENTRY(sfp) xfs_dir2_sf_firstentry(sfp)
Nathan Scotta844f452005-11-02 14:38:42 +1100178static inline xfs_dir2_sf_entry_t *xfs_dir2_sf_firstentry(xfs_dir2_sf_t *sfp)
179{
180 return ((xfs_dir2_sf_entry_t *) \
181 ((char *)(sfp) + XFS_DIR2_SF_HDR_SIZE(sfp->hdr.i8count)));
182}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Nathan Scotta844f452005-11-02 14:38:42 +1100184#define XFS_DIR2_SF_NEXTENTRY(sfp,sfep) xfs_dir2_sf_nextentry(sfp,sfep)
185static inline xfs_dir2_sf_entry_t *
186xfs_dir2_sf_nextentry(xfs_dir2_sf_t *sfp, xfs_dir2_sf_entry_t *sfep)
187{
188 return ((xfs_dir2_sf_entry_t *) \
189 ((char *)(sfep) + XFS_DIR2_SF_ENTSIZE_BYENTRY(sfp,sfep)));
190}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192/*
193 * Functions.
194 */
Nathan Scotta844f452005-11-02 14:38:42 +1100195extern int xfs_dir2_block_sfsize(struct xfs_inode *dp,
196 struct xfs_dir2_block *block,
197 xfs_dir2_sf_hdr_t *sfhp);
198extern int xfs_dir2_block_to_sf(struct xfs_da_args *args, struct xfs_dabuf *bp,
199 int size, xfs_dir2_sf_hdr_t *sfhp);
200extern int xfs_dir2_sf_addname(struct xfs_da_args *args);
201extern int xfs_dir2_sf_create(struct xfs_da_args *args, xfs_ino_t pino);
202extern int xfs_dir2_sf_getdents(struct xfs_inode *dp, struct uio *uio,
203 int *eofp, struct xfs_dirent *dbp,
204 xfs_dir2_put_t put);
205extern int xfs_dir2_sf_lookup(struct xfs_da_args *args);
206extern int xfs_dir2_sf_removename(struct xfs_da_args *args);
207extern int xfs_dir2_sf_replace(struct xfs_da_args *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209#endif /* __XFS_DIR2_SF_H__ */