License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[linux-block.git] / include / linux / ceph / osdmap.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
f24e9980
SW
2#ifndef _FS_CEPH_OSDMAP_H
3#define _FS_CEPH_OSDMAP_H
4
5#include <linux/rbtree.h>
a1ce3928 6#include <linux/ceph/types.h>
ef4859d6 7#include <linux/ceph/decode.h>
a1ce3928 8#include <linux/ceph/ceph_fs.h>
3d14c5d2 9#include <linux/crush/crush.h>
f24e9980
SW
10
11/*
12 * The osd map describes the current membership of the osd cluster and
13 * specifies the mapping of objects to placement groups and placement
14 * groups to (sets of) osds. That is, it completely specifies the
15 * (desired) distribution of all data objects in the system at some
16 * point in time.
17 *
18 * Each map version is identified by an epoch, which increases monotonically.
19 *
20 * The map can be updated either via an incremental map (diff) describing
21 * the change between two successive epochs, or as a fully encoded map.
22 */
5b191d99
SW
23struct ceph_pg {
24 uint64_t pool;
25 uint32_t seed;
26};
27
dc98ff72
ID
28#define CEPH_SPG_NOSHARD -1
29
30struct ceph_spg {
31 struct ceph_pg pgid;
32 s8 shard;
33};
34
f984cb76 35int ceph_pg_compare(const struct ceph_pg *lhs, const struct ceph_pg *rhs);
a02a946d 36int ceph_spg_compare(const struct ceph_spg *lhs, const struct ceph_spg *rhs);
f984cb76 37
04812acf
ID
38#define CEPH_POOL_FLAG_HASHPSPOOL (1ULL << 0) /* hash pg seed and pool id
39 together */
63244fa1 40#define CEPH_POOL_FLAG_FULL (1ULL << 1) /* pool is full */
83ca14fd 41
f24e9980 42struct ceph_pg_pool_info {
4fc51be8 43 struct rb_node node;
4f6a7e5e 44 s64 id;
04812acf 45 u8 type; /* CEPH_POOL_TYPE_* */
4f6a7e5e 46 u8 size;
04812acf 47 u8 min_size;
4f6a7e5e
SW
48 u8 crush_ruleset;
49 u8 object_hash;
04812acf 50 u32 last_force_request_resend;
4f6a7e5e
SW
51 u32 pg_num, pgp_num;
52 int pg_num_mask, pgp_num_mask;
17a13e40
ID
53 s64 read_tier;
54 s64 write_tier; /* wins for read+write ops */
04812acf 55 u64 flags; /* CEPH_POOL_FLAG_* */
2844a76a 56 char *name;
42c1b124
ID
57
58 bool was_full; /* for handle_one_map() */
f24e9980
SW
59};
60
2abebdbc
ID
61static inline bool ceph_can_shift_osds(struct ceph_pg_pool_info *pool)
62{
63 switch (pool->type) {
64 case CEPH_POOL_TYPE_REP:
65 return true;
66 case CEPH_POOL_TYPE_EC:
67 return false;
68 default:
d24cdcd3 69 BUG();
2abebdbc
ID
70 }
71}
72
4f6a7e5e 73struct ceph_object_locator {
22116525 74 s64 pool;
30c156d9 75 struct ceph_string *pool_ns;
4f6a7e5e
SW
76};
77
63244fa1
ID
78static inline void ceph_oloc_init(struct ceph_object_locator *oloc)
79{
80 oloc->pool = -1;
30c156d9 81 oloc->pool_ns = NULL;
63244fa1
ID
82}
83
84static inline bool ceph_oloc_empty(const struct ceph_object_locator *oloc)
85{
86 return oloc->pool == -1;
87}
88
30c156d9
YZ
89void ceph_oloc_copy(struct ceph_object_locator *dest,
90 const struct ceph_object_locator *src);
91void ceph_oloc_destroy(struct ceph_object_locator *oloc);
63244fa1 92
d30291b9
ID
93/*
94 * 51-char inline_name is long enough for all cephfs and all but one
95 * rbd requests: <imgname> in "<imgname>.rbd"/"rbd_id.<imgname>" can be
96 * arbitrarily long (~PAGE_SIZE). It's done once during rbd map; all
97 * other rbd requests fit into inline_name.
98 *
99 * Makes ceph_object_id 64 bytes on 64-bit.
100 */
101#define CEPH_OID_INLINE_LEN 52
102
103/*
104 * Both inline and external buffers have space for a NUL-terminator,
105 * which is carried around. It's not required though - RADOS object
106 * names don't have to be NUL-terminated and may contain NULs.
107 */
4295f221 108struct ceph_object_id {
d30291b9
ID
109 char *name;
110 char inline_name[CEPH_OID_INLINE_LEN];
4295f221
ID
111 int name_len;
112};
113
d30291b9
ID
114static inline void ceph_oid_init(struct ceph_object_id *oid)
115{
116 oid->name = oid->inline_name;
117 oid->name_len = 0;
118}
119
281dbe5d
ID
120#define CEPH_OID_INIT_ONSTACK(oid) \
121 ({ ceph_oid_init(&oid); oid; })
122#define CEPH_DEFINE_OID_ONSTACK(oid) \
123 struct ceph_object_id oid = CEPH_OID_INIT_ONSTACK(oid)
124
d30291b9
ID
125static inline bool ceph_oid_empty(const struct ceph_object_id *oid)
126{
127 return oid->name == oid->inline_name && !oid->name_len;
128}
129
130void ceph_oid_copy(struct ceph_object_id *dest,
131 const struct ceph_object_id *src);
132__printf(2, 3)
133void ceph_oid_printf(struct ceph_object_id *oid, const char *fmt, ...);
134__printf(3, 4)
135int ceph_oid_aprintf(struct ceph_object_id *oid, gfp_t gfp,
136 const char *fmt, ...);
137void ceph_oid_destroy(struct ceph_object_id *oid);
138
f24e9980
SW
139struct ceph_pg_mapping {
140 struct rb_node node;
5b191d99 141 struct ceph_pg pgid;
35a935d7
ID
142
143 union {
144 struct {
145 int len;
146 int osds[];
6f428df4 147 } pg_temp, pg_upmap;
9686f94c
ID
148 struct {
149 int osd;
150 } primary_temp;
6f428df4
ID
151 struct {
152 int len;
153 int from_to[][2];
154 } pg_upmap_items;
35a935d7 155 };
f24e9980
SW
156};
157
158struct ceph_osdmap {
159 struct ceph_fsid fsid;
160 u32 epoch;
f24e9980
SW
161 struct ceph_timespec created, modified;
162
163 u32 flags; /* CEPH_OSDMAP_* */
164
165 u32 max_osd; /* size of osd_state, _offload, _addr arrays */
0bb05da2 166 u32 *osd_state; /* CEPH_OSD_* */
f24e9980
SW
167 u32 *osd_weight; /* 0 = failed, 0x10000 = 100% normal */
168 struct ceph_entity_addr *osd_addr;
169
170 struct rb_root pg_temp;
9686f94c
ID
171 struct rb_root primary_temp;
172
6f428df4
ID
173 /* remap (post-CRUSH, pre-up) */
174 struct rb_root pg_upmap; /* PG := raw set */
175 struct rb_root pg_upmap_items; /* from -> to within raw set */
176
2cfa34f2
ID
177 u32 *osd_primary_affinity;
178
4fc51be8
SW
179 struct rb_root pg_pools;
180 u32 pool_max;
f24e9980
SW
181
182 /* the CRUSH map specifies the mapping of placement groups to
183 * the list of osds that store+replicate them. */
184 struct crush_map *crush;
9d521470 185
743efcff 186 struct mutex crush_workspace_mutex;
66a0e2d5 187 void *crush_workspace;
f24e9980
SW
188};
189
3b33f692 190static inline bool ceph_osd_exists(struct ceph_osdmap *map, int osd)
246138fa
ID
191{
192 return osd >= 0 && osd < map->max_osd &&
193 (map->osd_state[osd] & CEPH_OSD_EXISTS);
194}
195
3b33f692 196static inline bool ceph_osd_is_up(struct ceph_osdmap *map, int osd)
f24e9980 197{
246138fa
ID
198 return ceph_osd_exists(map, osd) &&
199 (map->osd_state[osd] & CEPH_OSD_UP);
200}
201
3b33f692 202static inline bool ceph_osd_is_down(struct ceph_osdmap *map, int osd)
246138fa
ID
203{
204 return !ceph_osd_is_up(map, osd);
f24e9980
SW
205}
206
0bb05da2 207char *ceph_osdmap_state_str(char *str, int len, u32 state);
2cfa34f2 208extern u32 ceph_get_primary_affinity(struct ceph_osdmap *map, int osd);
f24e9980
SW
209
210static inline struct ceph_entity_addr *ceph_osd_addr(struct ceph_osdmap *map,
211 int osd)
212{
213 if (osd >= map->max_osd)
214 return NULL;
215 return &map->osd_addr[osd];
216}
217
8cb441c0
ID
218#define CEPH_PGID_ENCODING_LEN (1 + 8 + 4 + 4)
219
ef4859d6
AE
220static inline int ceph_decode_pgid(void **p, void *end, struct ceph_pg *pgid)
221{
222 __u8 version;
223
8cb441c0 224 if (!ceph_has_room(p, end, CEPH_PGID_ENCODING_LEN)) {
3ef650d3 225 pr_warn("incomplete pg encoding\n");
ef4859d6
AE
226 return -EINVAL;
227 }
228 version = ceph_decode_8(p);
229 if (version > 1) {
3ef650d3 230 pr_warn("do not understand pg encoding %d > 1\n",
ef4859d6
AE
231 (int)version);
232 return -EINVAL;
233 }
234
235 pgid->pool = ceph_decode_64(p);
236 pgid->seed = ceph_decode_32(p);
237 *p += 4; /* skip deprecated preferred value */
238
239 return 0;
240}
241
e5253a7b 242struct ceph_osdmap *ceph_osdmap_alloc(void);
a2505d63 243extern struct ceph_osdmap *ceph_osdmap_decode(void **p, void *end);
0c0a8de1
ID
244struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end,
245 struct ceph_osdmap *map);
f24e9980
SW
246extern void ceph_osdmap_destroy(struct ceph_osdmap *map);
247
6f3bfd45
ID
248struct ceph_osds {
249 int osds[CEPH_PG_MAX_SIZE];
250 int size;
251 int primary; /* id, NOT index */
252};
253
254static inline void ceph_osds_init(struct ceph_osds *set)
255{
256 set->size = 0;
257 set->primary = -1;
258}
259
260void ceph_osds_copy(struct ceph_osds *dest, const struct ceph_osds *src);
261
7de030d6
ID
262bool ceph_pg_is_split(const struct ceph_pg *pgid, u32 old_pg_num,
263 u32 new_pg_num);
63244fa1
ID
264bool ceph_is_new_interval(const struct ceph_osds *old_acting,
265 const struct ceph_osds *new_acting,
266 const struct ceph_osds *old_up,
267 const struct ceph_osds *new_up,
268 int old_size,
269 int new_size,
270 int old_min_size,
271 int new_min_size,
272 u32 old_pg_num,
273 u32 new_pg_num,
274 bool old_sort_bitwise,
275 bool new_sort_bitwise,
ae78dd81
ID
276 bool old_recovery_deletes,
277 bool new_recovery_deletes,
63244fa1
ID
278 const struct ceph_pg *pgid);
279bool ceph_osds_changed(const struct ceph_osds *old_acting,
280 const struct ceph_osds *new_acting,
281 bool any_change);
282
f24e9980 283/* calculate mapping of a file extent to an object */
d63b77f4 284extern int ceph_calc_file_object_mapping(struct ceph_file_layout *layout,
e8afad65 285 u64 off, u64 len,
d63b77f4 286 u64 *bno, u64 *oxoff, u64 *oxlen);
f24e9980 287
df28152d
ID
288int __ceph_object_locator_to_pg(struct ceph_pg_pool_info *pi,
289 const struct ceph_object_id *oid,
290 const struct ceph_object_locator *oloc,
291 struct ceph_pg *raw_pgid);
d9591f5e 292int ceph_object_locator_to_pg(struct ceph_osdmap *osdmap,
df28152d
ID
293 const struct ceph_object_id *oid,
294 const struct ceph_object_locator *oloc,
d9591f5e 295 struct ceph_pg *raw_pgid);
7c13cb64 296
6f3bfd45 297void ceph_pg_to_up_acting_osds(struct ceph_osdmap *osdmap,
df28152d 298 struct ceph_pg_pool_info *pi,
6f3bfd45
ID
299 const struct ceph_pg *raw_pgid,
300 struct ceph_osds *up,
301 struct ceph_osds *acting);
dc98ff72 302bool ceph_pg_to_primary_shard(struct ceph_osdmap *osdmap,
df28152d 303 struct ceph_pg_pool_info *pi,
dc98ff72
ID
304 const struct ceph_pg *raw_pgid,
305 struct ceph_spg *spgid);
f81f1633
ID
306int ceph_pg_to_acting_primary(struct ceph_osdmap *osdmap,
307 const struct ceph_pg *raw_pgid);
f24e9980 308
ce7f6a27
ID
309extern struct ceph_pg_pool_info *ceph_pg_pool_by_id(struct ceph_osdmap *map,
310 u64 id);
311
72afc71f 312extern const char *ceph_pg_pool_name_by_id(struct ceph_osdmap *map, u64 id);
7669a2c9
YS
313extern int ceph_pg_poolid_by_name(struct ceph_osdmap *map, const char *name);
314
f24e9980 315#endif