blob: c1ab2e7c66aecce6851ada12d27d47d8354a752c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
18#ifndef __XFS_DIR2_SF_H__
19#define __XFS_DIR2_SF_H__
20
21/*
22 * Directory layout when stored internal to an inode.
23 *
Christoph Hellwigac8ba502011-07-08 14:35:13 +020024 * Small directories are packed as tightly as possible so as to fit into the
25 * literal area of the inode. They consist of a single xfs_dir2_sf_hdr header
26 * followed by zero or more xfs_dir2_sf_entry structures. Due the different
27 * inode number storage size and the variable length name field in
28 * the xfs_dir2_sf_entry all these structure are variable length, and the
29 * accessors in this file should be used to iterate over them.
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 */
31
32struct uio;
33struct xfs_dabuf;
34struct xfs_da_args;
35struct xfs_dir2_block;
36struct xfs_inode;
37struct xfs_mount;
38struct xfs_trans;
39
40/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 * Inode number stored as 8 8-bit values.
42 */
43typedef struct { __uint8_t i[8]; } xfs_dir2_ino8_t;
44
45/*
46 * Inode number stored as 4 8-bit values.
47 * Works a lot of the time, when all the inode numbers in a directory
48 * fit in 32 bits.
49 */
50typedef struct { __uint8_t i[4]; } xfs_dir2_ino4_t;
51
52typedef union {
53 xfs_dir2_ino8_t i8;
54 xfs_dir2_ino4_t i4;
55} xfs_dir2_inou_t;
56#define XFS_DIR2_MAX_SHORT_INUM ((xfs_ino_t)0xffffffffULL)
57
58/*
59 * Normalized offset (in a data block) of the entry, really xfs_dir2_data_off_t.
60 * Only need 16 bits, this is the byte offset into the single block form.
61 */
Eric Sandeenae23a5e2008-06-23 13:23:32 +100062typedef struct { __uint8_t i[2]; } __arch_pack xfs_dir2_sf_off_t;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64/*
65 * The parent directory has a dedicated field, and the self-pointer must
66 * be calculated on the fly.
67 *
Christoph Hellwigac8ba502011-07-08 14:35:13 +020068 * Entries are packed toward the top as tightly as possible, and thus may
69 * be misaligned. Care needs to be taken to access them through special
70 * helpers or copy them into aligned variables first.
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 */
72typedef struct xfs_dir2_sf_hdr {
73 __uint8_t count; /* count of entries */
74 __uint8_t i8count; /* count of 8-byte inode #s */
75 xfs_dir2_inou_t parent; /* parent dir inode number */
Eric Sandeenae23a5e2008-06-23 13:23:32 +100076} __arch_pack xfs_dir2_sf_hdr_t;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78typedef struct xfs_dir2_sf_entry {
79 __uint8_t namelen; /* actual name length */
80 xfs_dir2_sf_off_t offset; /* saved offset */
81 __uint8_t name[1]; /* name, variable size */
82 xfs_dir2_inou_t inumber; /* inode number, var. offset */
Eric Sandeenae23a5e2008-06-23 13:23:32 +100083} __arch_pack xfs_dir2_sf_entry_t;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Nathan Scotta844f452005-11-02 14:38:42 +110085static inline int xfs_dir2_sf_hdr_size(int i8count)
86{
87 return ((uint)sizeof(xfs_dir2_sf_hdr_t) - \
88 ((i8count) == 0) * \
89 ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t)));
90}
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Nathan Scotta844f452005-11-02 14:38:42 +110092static inline xfs_dir2_data_aoff_t
93xfs_dir2_sf_get_offset(xfs_dir2_sf_entry_t *sfep)
94{
95 return INT_GET_UNALIGNED_16_BE(&(sfep)->offset.i);
96}
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Nathan Scotta844f452005-11-02 14:38:42 +110098static inline void
99xfs_dir2_sf_put_offset(xfs_dir2_sf_entry_t *sfep, xfs_dir2_data_aoff_t off)
100{
101 INT_SET_UNALIGNED_16_BE(&(sfep)->offset.i, off);
102}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200104static inline int xfs_dir2_sf_entsize_byname(xfs_dir2_sf_hdr_t *sfp, int len)
Nathan Scotta844f452005-11-02 14:38:42 +1100105{
106 return ((uint)sizeof(xfs_dir2_sf_entry_t) - 1 + (len) - \
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200107 ((sfp)->i8count == 0) * \
Nathan Scotta844f452005-11-02 14:38:42 +1100108 ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t)));
109}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Nathan Scotta844f452005-11-02 14:38:42 +1100111static inline int
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200112xfs_dir2_sf_entsize_byentry(xfs_dir2_sf_hdr_t *sfp, xfs_dir2_sf_entry_t *sfep)
Nathan Scotta844f452005-11-02 14:38:42 +1100113{
114 return ((uint)sizeof(xfs_dir2_sf_entry_t) - 1 + (sfep)->namelen - \
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200115 ((sfp)->i8count == 0) * \
Nathan Scotta844f452005-11-02 14:38:42 +1100116 ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t)));
117}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200119static inline struct xfs_dir2_sf_entry *
120xfs_dir2_sf_firstentry(struct xfs_dir2_sf_hdr *hdr)
Nathan Scotta844f452005-11-02 14:38:42 +1100121{
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200122 return (struct xfs_dir2_sf_entry *)
123 ((char *)hdr + xfs_dir2_sf_hdr_size(hdr->i8count));
Nathan Scotta844f452005-11-02 14:38:42 +1100124}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200126static inline struct xfs_dir2_sf_entry *
127xfs_dir2_sf_nextentry(struct xfs_dir2_sf_hdr *hdr,
128 struct xfs_dir2_sf_entry *sfep)
Nathan Scotta844f452005-11-02 14:38:42 +1100129{
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200130 return (struct xfs_dir2_sf_entry *)
131 ((char *)sfep + xfs_dir2_sf_entsize_byentry(hdr, sfep));
Nathan Scotta844f452005-11-02 14:38:42 +1100132}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134/*
135 * Functions.
136 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +0200137extern xfs_ino_t xfs_dir2_sf_get_parent_ino(struct xfs_dir2_sf_hdr *sfp);
138extern xfs_ino_t xfs_dir2_sfe_get_ino(struct xfs_dir2_sf_hdr *sfp,
Christoph Hellwig8bc38782011-07-08 14:35:03 +0200139 struct xfs_dir2_sf_entry *sfep);
Nathan Scotta844f452005-11-02 14:38:42 +1100140extern int xfs_dir2_block_sfsize(struct xfs_inode *dp,
141 struct xfs_dir2_block *block,
142 xfs_dir2_sf_hdr_t *sfhp);
143extern int xfs_dir2_block_to_sf(struct xfs_da_args *args, struct xfs_dabuf *bp,
144 int size, xfs_dir2_sf_hdr_t *sfhp);
145extern int xfs_dir2_sf_addname(struct xfs_da_args *args);
146extern int xfs_dir2_sf_create(struct xfs_da_args *args, xfs_ino_t pino);
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000147extern int xfs_dir2_sf_getdents(struct xfs_inode *dp, void *dirent,
148 xfs_off_t *offset, filldir_t filldir);
Nathan Scotta844f452005-11-02 14:38:42 +1100149extern int xfs_dir2_sf_lookup(struct xfs_da_args *args);
150extern int xfs_dir2_sf_removename(struct xfs_da_args *args);
151extern int xfs_dir2_sf_replace(struct xfs_da_args *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153#endif /* __XFS_DIR2_SF_H__ */