osdmap.h 8.3 KB
Newer Older
1
/* SPDX-License-Identifier: GPL-2.0 */
S
Sage Weil 已提交
2 3 4 5
#ifndef _FS_CEPH_OSDMAP_H
#define _FS_CEPH_OSDMAP_H

#include <linux/rbtree.h>
6
#include <linux/ceph/types.h>
7
#include <linux/ceph/decode.h>
8
#include <linux/ceph/ceph_fs.h>
9
#include <linux/crush/crush.h>
S
Sage Weil 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22

/*
 * The osd map describes the current membership of the osd cluster and
 * specifies the mapping of objects to placement groups and placement
 * groups to (sets of) osds.  That is, it completely specifies the
 * (desired) distribution of all data objects in the system at some
 * point in time.
 *
 * Each map version is identified by an epoch, which increases monotonically.
 *
 * The map can be updated either via an incremental map (diff) describing
 * the change between two successive epochs, or as a fully encoded map.
 */
23 24 25 26 27
struct ceph_pg {
	uint64_t pool;
	uint32_t seed;
};

28 29 30 31 32 33 34
#define CEPH_SPG_NOSHARD	-1

struct ceph_spg {
	struct ceph_pg pgid;
	s8 shard;
};

I
Ilya Dryomov 已提交
35
int ceph_pg_compare(const struct ceph_pg *lhs, const struct ceph_pg *rhs);
36
int ceph_spg_compare(const struct ceph_spg *lhs, const struct ceph_spg *rhs);
I
Ilya Dryomov 已提交
37

38 39
#define CEPH_POOL_FLAG_HASHPSPOOL	(1ULL << 0) /* hash pg seed and pool id
						       together */
40
#define CEPH_POOL_FLAG_FULL		(1ULL << 1) /* pool is full */
41

S
Sage Weil 已提交
42
struct ceph_pg_pool_info {
43
	struct rb_node node;
44
	s64 id;
45
	u8 type; /* CEPH_POOL_TYPE_* */
46
	u8 size;
47
	u8 min_size;
48 49
	u8 crush_ruleset;
	u8 object_hash;
50
	u32 last_force_request_resend;
51 52
	u32 pg_num, pgp_num;
	int pg_num_mask, pgp_num_mask;
53 54
	s64 read_tier;
	s64 write_tier; /* wins for read+write ops */
55
	u64 flags; /* CEPH_POOL_FLAG_* */
56
	char *name;
I
Ilya Dryomov 已提交
57 58

	bool was_full;  /* for handle_one_map() */
S
Sage Weil 已提交
59 60
};

61 62 63 64 65 66 67 68
static inline bool ceph_can_shift_osds(struct ceph_pg_pool_info *pool)
{
	switch (pool->type) {
	case CEPH_POOL_TYPE_REP:
		return true;
	case CEPH_POOL_TYPE_EC:
		return false;
	default:
69
		BUG();
70 71 72
	}
}

73
struct ceph_object_locator {
74
	s64 pool;
75
	struct ceph_string *pool_ns;
76 77
};

78 79 80
static inline void ceph_oloc_init(struct ceph_object_locator *oloc)
{
	oloc->pool = -1;
81
	oloc->pool_ns = NULL;
82 83 84 85 86 87 88
}

static inline bool ceph_oloc_empty(const struct ceph_object_locator *oloc)
{
	return oloc->pool == -1;
}

89 90 91
void ceph_oloc_copy(struct ceph_object_locator *dest,
		    const struct ceph_object_locator *src);
void ceph_oloc_destroy(struct ceph_object_locator *oloc);
92

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
/*
 * 51-char inline_name is long enough for all cephfs and all but one
 * rbd requests: <imgname> in "<imgname>.rbd"/"rbd_id.<imgname>" can be
 * arbitrarily long (~PAGE_SIZE).  It's done once during rbd map; all
 * other rbd requests fit into inline_name.
 *
 * Makes ceph_object_id 64 bytes on 64-bit.
 */
#define CEPH_OID_INLINE_LEN 52

/*
 * Both inline and external buffers have space for a NUL-terminator,
 * which is carried around.  It's not required though - RADOS object
 * names don't have to be NUL-terminated and may contain NULs.
 */
108
struct ceph_object_id {
109 110
	char *name;
	char inline_name[CEPH_OID_INLINE_LEN];
111 112 113
	int name_len;
};

114 115 116 117 118 119
static inline void ceph_oid_init(struct ceph_object_id *oid)
{
	oid->name = oid->inline_name;
	oid->name_len = 0;
}

120 121 122 123 124
#define CEPH_OID_INIT_ONSTACK(oid)					\
    ({ ceph_oid_init(&oid); oid; })
#define CEPH_DEFINE_OID_ONSTACK(oid)					\
	struct ceph_object_id oid = CEPH_OID_INIT_ONSTACK(oid)

125 126 127 128 129 130 131 132 133 134 135 136 137 138
static inline bool ceph_oid_empty(const struct ceph_object_id *oid)
{
	return oid->name == oid->inline_name && !oid->name_len;
}

void ceph_oid_copy(struct ceph_object_id *dest,
		   const struct ceph_object_id *src);
__printf(2, 3)
void ceph_oid_printf(struct ceph_object_id *oid, const char *fmt, ...);
__printf(3, 4)
int ceph_oid_aprintf(struct ceph_object_id *oid, gfp_t gfp,
		     const char *fmt, ...);
void ceph_oid_destroy(struct ceph_object_id *oid);

S
Sage Weil 已提交
139 140
struct ceph_pg_mapping {
	struct rb_node node;
141
	struct ceph_pg pgid;
142 143 144 145 146

	union {
		struct {
			int len;
			int osds[];
147
		} pg_temp, pg_upmap;
148 149 150
		struct {
			int osd;
		} primary_temp;
151 152 153 154
		struct {
			int len;
			int from_to[][2];
		} pg_upmap_items;
155
	};
S
Sage Weil 已提交
156 157 158 159 160 161 162 163 164 165
};

struct ceph_osdmap {
	struct ceph_fsid fsid;
	u32 epoch;
	struct ceph_timespec created, modified;

	u32 flags;         /* CEPH_OSDMAP_* */

	u32 max_osd;       /* size of osd_state, _offload, _addr arrays */
166
	u32 *osd_state;    /* CEPH_OSD_* */
S
Sage Weil 已提交
167 168 169 170
	u32 *osd_weight;   /* 0 = failed, 0x10000 = 100% normal */
	struct ceph_entity_addr *osd_addr;

	struct rb_root pg_temp;
171 172
	struct rb_root primary_temp;

173 174 175 176
	/* remap (post-CRUSH, pre-up) */
	struct rb_root pg_upmap;	/* PG := raw set */
	struct rb_root pg_upmap_items;	/* from -> to within raw set */

177 178
	u32 *osd_primary_affinity;

179 180
	struct rb_root pg_pools;
	u32 pool_max;
S
Sage Weil 已提交
181 182 183 184

	/* the CRUSH map specifies the mapping of placement groups to
	 * the list of osds that store+replicate them. */
	struct crush_map *crush;
185

186
	struct mutex crush_workspace_mutex;
187
	void *crush_workspace;
S
Sage Weil 已提交
188 189
};

190
static inline bool ceph_osd_exists(struct ceph_osdmap *map, int osd)
191 192 193 194 195
{
	return osd >= 0 && osd < map->max_osd &&
	       (map->osd_state[osd] & CEPH_OSD_EXISTS);
}

196
static inline bool ceph_osd_is_up(struct ceph_osdmap *map, int osd)
S
Sage Weil 已提交
197
{
198 199 200 201
	return ceph_osd_exists(map, osd) &&
	       (map->osd_state[osd] & CEPH_OSD_UP);
}

202
static inline bool ceph_osd_is_down(struct ceph_osdmap *map, int osd)
203 204
{
	return !ceph_osd_is_up(map, osd);
S
Sage Weil 已提交
205 206
}

207
char *ceph_osdmap_state_str(char *str, int len, u32 state);
208
extern u32 ceph_get_primary_affinity(struct ceph_osdmap *map, int osd);
S
Sage Weil 已提交
209 210 211 212 213 214 215 216 217

static inline struct ceph_entity_addr *ceph_osd_addr(struct ceph_osdmap *map,
						     int osd)
{
	if (osd >= map->max_osd)
		return NULL;
	return &map->osd_addr[osd];
}

218 219
#define CEPH_PGID_ENCODING_LEN		(1 + 8 + 4 + 4)

220 221 222 223
static inline int ceph_decode_pgid(void **p, void *end, struct ceph_pg *pgid)
{
	__u8 version;

224
	if (!ceph_has_room(p, end, CEPH_PGID_ENCODING_LEN)) {
225
		pr_warn("incomplete pg encoding\n");
226 227 228 229
		return -EINVAL;
	}
	version = ceph_decode_8(p);
	if (version > 1) {
230
		pr_warn("do not understand pg encoding %d > 1\n",
231 232 233 234 235 236 237 238 239 240 241
			(int)version);
		return -EINVAL;
	}

	pgid->pool = ceph_decode_64(p);
	pgid->seed = ceph_decode_32(p);
	*p += 4;	/* skip deprecated preferred value */

	return 0;
}

242
struct ceph_osdmap *ceph_osdmap_alloc(void);
243
extern struct ceph_osdmap *ceph_osdmap_decode(void **p, void *end);
244 245
struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end,
					     struct ceph_osdmap *map);
S
Sage Weil 已提交
246 247
extern void ceph_osdmap_destroy(struct ceph_osdmap *map);

248 249 250 251 252 253 254 255 256 257 258 259 260 261
struct ceph_osds {
	int osds[CEPH_PG_MAX_SIZE];
	int size;
	int primary; /* id, NOT index */
};

static inline void ceph_osds_init(struct ceph_osds *set)
{
	set->size = 0;
	set->primary = -1;
}

void ceph_osds_copy(struct ceph_osds *dest, const struct ceph_osds *src);

262 263
bool ceph_pg_is_split(const struct ceph_pg *pgid, u32 old_pg_num,
		      u32 new_pg_num);
264 265 266 267 268 269 270 271 272 273 274 275
bool ceph_is_new_interval(const struct ceph_osds *old_acting,
			  const struct ceph_osds *new_acting,
			  const struct ceph_osds *old_up,
			  const struct ceph_osds *new_up,
			  int old_size,
			  int new_size,
			  int old_min_size,
			  int new_min_size,
			  u32 old_pg_num,
			  u32 new_pg_num,
			  bool old_sort_bitwise,
			  bool new_sort_bitwise,
276 277
			  bool old_recovery_deletes,
			  bool new_recovery_deletes,
278 279 280 281 282
			  const struct ceph_pg *pgid);
bool ceph_osds_changed(const struct ceph_osds *old_acting,
		       const struct ceph_osds *new_acting,
		       bool any_change);

283 284 285
void ceph_calc_file_object_mapping(struct ceph_file_layout *l,
				   u64 off, u64 len,
				   u64 *objno, u64 *objoff, u32 *xlen);
S
Sage Weil 已提交
286

287 288 289 290
int __ceph_object_locator_to_pg(struct ceph_pg_pool_info *pi,
				const struct ceph_object_id *oid,
				const struct ceph_object_locator *oloc,
				struct ceph_pg *raw_pgid);
291
int ceph_object_locator_to_pg(struct ceph_osdmap *osdmap,
292 293
			      const struct ceph_object_id *oid,
			      const struct ceph_object_locator *oloc,
294
			      struct ceph_pg *raw_pgid);
295

296
void ceph_pg_to_up_acting_osds(struct ceph_osdmap *osdmap,
297
			       struct ceph_pg_pool_info *pi,
298 299 300
			       const struct ceph_pg *raw_pgid,
			       struct ceph_osds *up,
			       struct ceph_osds *acting);
301
bool ceph_pg_to_primary_shard(struct ceph_osdmap *osdmap,
302
			      struct ceph_pg_pool_info *pi,
303 304
			      const struct ceph_pg *raw_pgid,
			      struct ceph_spg *spgid);
305 306
int ceph_pg_to_acting_primary(struct ceph_osdmap *osdmap,
			      const struct ceph_pg *raw_pgid);
S
Sage Weil 已提交
307

I
Ilya Dryomov 已提交
308 309 310
extern struct ceph_pg_pool_info *ceph_pg_pool_by_id(struct ceph_osdmap *map,
						    u64 id);

311
extern const char *ceph_pg_pool_name_by_id(struct ceph_osdmap *map, u64 id);
Y
Yehuda Sadeh 已提交
312 313
extern int ceph_pg_poolid_by_name(struct ceph_osdmap *map, const char *name);

S
Sage Weil 已提交
314
#endif