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