xfs_da_format.c 4.1 KB
Newer Older
D
Dave Chinner 已提交
1
// SPDX-License-Identifier: GPL-2.0
2 3 4 5 6 7 8
/*
 * Copyright (c) 2000,2002,2005 Silicon Graphics, Inc.
 * Copyright (c) 2013 Red Hat, Inc.
 * All Rights Reserved.
 */
#include "xfs.h"
#include "xfs_fs.h"
9
#include "xfs_shared.h"
10 11 12 13 14 15
#include "xfs_format.h"
#include "xfs_log_format.h"
#include "xfs_trans_resv.h"
#include "xfs_mount.h"
#include "xfs_inode.h"
#include "xfs_dir2.h"
16
#include "xfs_dir2_priv.h"
17

18 19 20 21
/*
 * Directory data block operations
 */

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/*
 * For special situations, the dirent size ends up fixed because we always know
 * what the size of the entry is. That's true for the "." and "..", and
 * therefore we know that they are a fixed size and hence their offsets are
 * constant, as is the first entry.
 *
 * Hence, this calculation is written as a macro to be able to be calculated at
 * compile time and so certain offsets can be calculated directly in the
 * structure initaliser via the macro. There are two macros - one for dirents
 * with ftype and without so there are no unresolvable conditionals in the
 * calculations. We also use round_up() as XFS_DIR2_DATA_ALIGN is always a power
 * of 2 and the compiler doesn't reject it (unlike roundup()).
 */
#define XFS_DIR2_DATA_ENTSIZE(n)					\
	round_up((offsetof(struct xfs_dir2_data_entry, name[0]) + (n) +	\
		 sizeof(xfs_dir2_data_off_t)), XFS_DIR2_DATA_ALIGN)

#define XFS_DIR3_DATA_ENTSIZE(n)					\
	round_up((offsetof(struct xfs_dir2_data_entry, name[0]) + (n) +	\
41
		 sizeof(xfs_dir2_data_off_t) + sizeof(uint8_t)),	\
42
		XFS_DIR2_DATA_ALIGN)
43

44
int
45
xfs_dir2_data_entsize(
46
	struct xfs_mount	*mp,
47 48
	int			n)
{
49 50 51 52
	if (xfs_sb_version_hasftype(&mp->m_sb))
		return XFS_DIR3_DATA_ENTSIZE(n);
	else
		return XFS_DIR2_DATA_ENTSIZE(n);
53 54
}

55
static uint8_t
56 57 58 59 60 61 62 63 64
xfs_dir2_data_get_ftype(
	struct xfs_dir2_data_entry *dep)
{
	return XFS_DIR3_FT_UNKNOWN;
}

static void
xfs_dir2_data_put_ftype(
	struct xfs_dir2_data_entry *dep,
65
	uint8_t			ftype)
66 67 68 69
{
	ASSERT(ftype < XFS_DIR3_FT_MAX);
}

70
static uint8_t
71 72 73
xfs_dir3_data_get_ftype(
	struct xfs_dir2_data_entry *dep)
{
74
	uint8_t		ftype = dep->name[dep->namelen];
75 76 77 78 79 80 81 82 83

	if (ftype >= XFS_DIR3_FT_MAX)
		return XFS_DIR3_FT_UNKNOWN;
	return ftype;
}

static void
xfs_dir3_data_put_ftype(
	struct xfs_dir2_data_entry *dep,
84
	uint8_t			type)
85 86 87 88 89 90 91
{
	ASSERT(type < XFS_DIR3_FT_MAX);
	ASSERT(dep->namelen != 0);

	dep->name[dep->namelen] = type;
}

92 93 94 95 96 97 98 99 100 101 102 103
static struct xfs_dir2_data_free *
xfs_dir2_data_bestfree_p(struct xfs_dir2_data_hdr *hdr)
{
	return hdr->bestfree;
}

static struct xfs_dir2_data_free *
xfs_dir3_data_bestfree_p(struct xfs_dir2_data_hdr *hdr)
{
	return ((struct xfs_dir3_data_hdr *)hdr)->best_free;
}

104
static const struct xfs_dir_ops xfs_dir2_ops = {
105 106
	.data_get_ftype = xfs_dir2_data_get_ftype,
	.data_put_ftype = xfs_dir2_data_put_ftype,
107
	.data_bestfree_p = xfs_dir2_data_bestfree_p,
108

109 110 111 112
	.data_first_offset =  sizeof(struct xfs_dir2_data_hdr) +
				XFS_DIR2_DATA_ENTSIZE(1) +
				XFS_DIR2_DATA_ENTSIZE(2),
	.data_entry_offset = sizeof(struct xfs_dir2_data_hdr),
113 114
};

115
static const struct xfs_dir_ops xfs_dir2_ftype_ops = {
116 117
	.data_get_ftype = xfs_dir3_data_get_ftype,
	.data_put_ftype = xfs_dir3_data_put_ftype,
118
	.data_bestfree_p = xfs_dir2_data_bestfree_p,
119

120 121 122 123
	.data_first_offset =  sizeof(struct xfs_dir2_data_hdr) +
				XFS_DIR3_DATA_ENTSIZE(1) +
				XFS_DIR3_DATA_ENTSIZE(2),
	.data_entry_offset = sizeof(struct xfs_dir2_data_hdr),
124 125
};

126
static const struct xfs_dir_ops xfs_dir3_ops = {
127 128
	.data_get_ftype = xfs_dir3_data_get_ftype,
	.data_put_ftype = xfs_dir3_data_put_ftype,
129
	.data_bestfree_p = xfs_dir3_data_bestfree_p,
130

131 132 133 134
	.data_first_offset =  sizeof(struct xfs_dir3_data_hdr) +
				XFS_DIR3_DATA_ENTSIZE(1) +
				XFS_DIR3_DATA_ENTSIZE(2),
	.data_entry_offset = sizeof(struct xfs_dir3_data_hdr),
D
Dave Chinner 已提交
135 136
};

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
/*
 * Return the ops structure according to the current config.  If we are passed
 * an inode, then that overrides the default config we use which is based on
 * feature bits.
 */
const struct xfs_dir_ops *
xfs_dir_get_ops(
	struct xfs_mount	*mp,
	struct xfs_inode	*dp)
{
	if (dp)
		return dp->d_ops;
	if (mp->m_dir_inode_ops)
		return mp->m_dir_inode_ops;
	if (xfs_sb_version_hascrc(&mp->m_sb))
		return &xfs_dir3_ops;
	if (xfs_sb_version_hasftype(&mp->m_sb))
		return &xfs_dir2_ftype_ops;
	return &xfs_dir2_ops;
}