osdmap.h 8.1 KB

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