Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _FS_CEPH_OSDMAP_H
0003 #define _FS_CEPH_OSDMAP_H
0004 
0005 #include <linux/rbtree.h>
0006 #include <linux/ceph/types.h>
0007 #include <linux/ceph/decode.h>
0008 #include <linux/crush/crush.h>
0009 
0010 /*
0011  * The osd map describes the current membership of the osd cluster and
0012  * specifies the mapping of objects to placement groups and placement
0013  * groups to (sets of) osds.  That is, it completely specifies the
0014  * (desired) distribution of all data objects in the system at some
0015  * point in time.
0016  *
0017  * Each map version is identified by an epoch, which increases monotonically.
0018  *
0019  * The map can be updated either via an incremental map (diff) describing
0020  * the change between two successive epochs, or as a fully encoded map.
0021  */
0022 struct ceph_pg {
0023     uint64_t pool;
0024     uint32_t seed;
0025 };
0026 
0027 #define CEPH_SPG_NOSHARD    -1
0028 
0029 struct ceph_spg {
0030     struct ceph_pg pgid;
0031     s8 shard;
0032 };
0033 
0034 int ceph_pg_compare(const struct ceph_pg *lhs, const struct ceph_pg *rhs);
0035 int ceph_spg_compare(const struct ceph_spg *lhs, const struct ceph_spg *rhs);
0036 
0037 #define CEPH_POOL_FLAG_HASHPSPOOL   (1ULL << 0) /* hash pg seed and pool id
0038                                together */
0039 #define CEPH_POOL_FLAG_FULL     (1ULL << 1) /* pool is full */
0040 #define CEPH_POOL_FLAG_FULL_QUOTA   (1ULL << 10) /* pool ran out of quota,
0041                             will set FULL too */
0042 #define CEPH_POOL_FLAG_NEARFULL     (1ULL << 11) /* pool is nearfull */
0043 
0044 struct ceph_pg_pool_info {
0045     struct rb_node node;
0046     s64 id;
0047     u8 type; /* CEPH_POOL_TYPE_* */
0048     u8 size;
0049     u8 min_size;
0050     u8 crush_ruleset;
0051     u8 object_hash;
0052     u32 last_force_request_resend;
0053     u32 pg_num, pgp_num;
0054     int pg_num_mask, pgp_num_mask;
0055     s64 read_tier;
0056     s64 write_tier; /* wins for read+write ops */
0057     u64 flags; /* CEPH_POOL_FLAG_* */
0058     char *name;
0059 
0060     bool was_full;  /* for handle_one_map() */
0061 };
0062 
0063 static inline bool ceph_can_shift_osds(struct ceph_pg_pool_info *pool)
0064 {
0065     switch (pool->type) {
0066     case CEPH_POOL_TYPE_REP:
0067         return true;
0068     case CEPH_POOL_TYPE_EC:
0069         return false;
0070     default:
0071         BUG();
0072     }
0073 }
0074 
0075 struct ceph_object_locator {
0076     s64 pool;
0077     struct ceph_string *pool_ns;
0078 };
0079 
0080 static inline void ceph_oloc_init(struct ceph_object_locator *oloc)
0081 {
0082     oloc->pool = -1;
0083     oloc->pool_ns = NULL;
0084 }
0085 
0086 static inline bool ceph_oloc_empty(const struct ceph_object_locator *oloc)
0087 {
0088     return oloc->pool == -1;
0089 }
0090 
0091 void ceph_oloc_copy(struct ceph_object_locator *dest,
0092             const struct ceph_object_locator *src);
0093 void ceph_oloc_destroy(struct ceph_object_locator *oloc);
0094 
0095 /*
0096  * 51-char inline_name is long enough for all cephfs and all but one
0097  * rbd requests: <imgname> in "<imgname>.rbd"/"rbd_id.<imgname>" can be
0098  * arbitrarily long (~PAGE_SIZE).  It's done once during rbd map; all
0099  * other rbd requests fit into inline_name.
0100  *
0101  * Makes ceph_object_id 64 bytes on 64-bit.
0102  */
0103 #define CEPH_OID_INLINE_LEN 52
0104 
0105 /*
0106  * Both inline and external buffers have space for a NUL-terminator,
0107  * which is carried around.  It's not required though - RADOS object
0108  * names don't have to be NUL-terminated and may contain NULs.
0109  */
0110 struct ceph_object_id {
0111     char *name;
0112     char inline_name[CEPH_OID_INLINE_LEN];
0113     int name_len;
0114 };
0115 
0116 #define __CEPH_OID_INITIALIZER(oid) { .name = (oid).inline_name }
0117 
0118 #define CEPH_DEFINE_OID_ONSTACK(oid)                \
0119     struct ceph_object_id oid = __CEPH_OID_INITIALIZER(oid)
0120 
0121 static inline void ceph_oid_init(struct ceph_object_id *oid)
0122 {
0123     *oid = (struct ceph_object_id) __CEPH_OID_INITIALIZER(*oid);
0124 }
0125 
0126 static inline bool ceph_oid_empty(const struct ceph_object_id *oid)
0127 {
0128     return oid->name == oid->inline_name && !oid->name_len;
0129 }
0130 
0131 void ceph_oid_copy(struct ceph_object_id *dest,
0132            const struct ceph_object_id *src);
0133 __printf(2, 3)
0134 void ceph_oid_printf(struct ceph_object_id *oid, const char *fmt, ...);
0135 __printf(3, 4)
0136 int ceph_oid_aprintf(struct ceph_object_id *oid, gfp_t gfp,
0137              const char *fmt, ...);
0138 void ceph_oid_destroy(struct ceph_object_id *oid);
0139 
0140 struct workspace_manager {
0141     struct list_head idle_ws;
0142     spinlock_t ws_lock;
0143     /* Number of free workspaces */
0144     int free_ws;
0145     /* Total number of allocated workspaces */
0146     atomic_t total_ws;
0147     /* Waiters for a free workspace */
0148     wait_queue_head_t ws_wait;
0149 };
0150 
0151 struct ceph_pg_mapping {
0152     struct rb_node node;
0153     struct ceph_pg pgid;
0154 
0155     union {
0156         struct {
0157             int len;
0158             int osds[];
0159         } pg_temp, pg_upmap;
0160         struct {
0161             int osd;
0162         } primary_temp;
0163         struct {
0164             int len;
0165             int from_to[][2];
0166         } pg_upmap_items;
0167     };
0168 };
0169 
0170 struct ceph_osdmap {
0171     struct ceph_fsid fsid;
0172     u32 epoch;
0173     struct ceph_timespec created, modified;
0174 
0175     u32 flags;         /* CEPH_OSDMAP_* */
0176 
0177     u32 max_osd;       /* size of osd_state, _offload, _addr arrays */
0178     u32 *osd_state;    /* CEPH_OSD_* */
0179     u32 *osd_weight;   /* 0 = failed, 0x10000 = 100% normal */
0180     struct ceph_entity_addr *osd_addr;
0181 
0182     struct rb_root pg_temp;
0183     struct rb_root primary_temp;
0184 
0185     /* remap (post-CRUSH, pre-up) */
0186     struct rb_root pg_upmap;    /* PG := raw set */
0187     struct rb_root pg_upmap_items;  /* from -> to within raw set */
0188 
0189     u32 *osd_primary_affinity;
0190 
0191     struct rb_root pg_pools;
0192     u32 pool_max;
0193 
0194     /* the CRUSH map specifies the mapping of placement groups to
0195      * the list of osds that store+replicate them. */
0196     struct crush_map *crush;
0197 
0198     struct workspace_manager crush_wsm;
0199 };
0200 
0201 static inline bool ceph_osd_exists(struct ceph_osdmap *map, int osd)
0202 {
0203     return osd >= 0 && osd < map->max_osd &&
0204            (map->osd_state[osd] & CEPH_OSD_EXISTS);
0205 }
0206 
0207 static inline bool ceph_osd_is_up(struct ceph_osdmap *map, int osd)
0208 {
0209     return ceph_osd_exists(map, osd) &&
0210            (map->osd_state[osd] & CEPH_OSD_UP);
0211 }
0212 
0213 static inline bool ceph_osd_is_down(struct ceph_osdmap *map, int osd)
0214 {
0215     return !ceph_osd_is_up(map, osd);
0216 }
0217 
0218 char *ceph_osdmap_state_str(char *str, int len, u32 state);
0219 extern u32 ceph_get_primary_affinity(struct ceph_osdmap *map, int osd);
0220 
0221 static inline struct ceph_entity_addr *ceph_osd_addr(struct ceph_osdmap *map,
0222                              int osd)
0223 {
0224     if (osd >= map->max_osd)
0225         return NULL;
0226     return &map->osd_addr[osd];
0227 }
0228 
0229 #define CEPH_PGID_ENCODING_LEN      (1 + 8 + 4 + 4)
0230 
0231 static inline int ceph_decode_pgid(void **p, void *end, struct ceph_pg *pgid)
0232 {
0233     __u8 version;
0234 
0235     if (!ceph_has_room(p, end, CEPH_PGID_ENCODING_LEN)) {
0236         pr_warn("incomplete pg encoding\n");
0237         return -EINVAL;
0238     }
0239     version = ceph_decode_8(p);
0240     if (version > 1) {
0241         pr_warn("do not understand pg encoding %d > 1\n",
0242             (int)version);
0243         return -EINVAL;
0244     }
0245 
0246     pgid->pool = ceph_decode_64(p);
0247     pgid->seed = ceph_decode_32(p);
0248     *p += 4;    /* skip deprecated preferred value */
0249 
0250     return 0;
0251 }
0252 
0253 struct ceph_osdmap *ceph_osdmap_alloc(void);
0254 struct ceph_osdmap *ceph_osdmap_decode(void **p, void *end, bool msgr2);
0255 struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end, bool msgr2,
0256                          struct ceph_osdmap *map);
0257 extern void ceph_osdmap_destroy(struct ceph_osdmap *map);
0258 
0259 struct ceph_osds {
0260     int osds[CEPH_PG_MAX_SIZE];
0261     int size;
0262     int primary; /* id, NOT index */
0263 };
0264 
0265 static inline void ceph_osds_init(struct ceph_osds *set)
0266 {
0267     set->size = 0;
0268     set->primary = -1;
0269 }
0270 
0271 void ceph_osds_copy(struct ceph_osds *dest, const struct ceph_osds *src);
0272 
0273 bool ceph_pg_is_split(const struct ceph_pg *pgid, u32 old_pg_num,
0274               u32 new_pg_num);
0275 bool ceph_is_new_interval(const struct ceph_osds *old_acting,
0276               const struct ceph_osds *new_acting,
0277               const struct ceph_osds *old_up,
0278               const struct ceph_osds *new_up,
0279               int old_size,
0280               int new_size,
0281               int old_min_size,
0282               int new_min_size,
0283               u32 old_pg_num,
0284               u32 new_pg_num,
0285               bool old_sort_bitwise,
0286               bool new_sort_bitwise,
0287               bool old_recovery_deletes,
0288               bool new_recovery_deletes,
0289               const struct ceph_pg *pgid);
0290 bool ceph_osds_changed(const struct ceph_osds *old_acting,
0291                const struct ceph_osds *new_acting,
0292                bool any_change);
0293 
0294 void __ceph_object_locator_to_pg(struct ceph_pg_pool_info *pi,
0295                  const struct ceph_object_id *oid,
0296                  const struct ceph_object_locator *oloc,
0297                  struct ceph_pg *raw_pgid);
0298 int ceph_object_locator_to_pg(struct ceph_osdmap *osdmap,
0299                   const struct ceph_object_id *oid,
0300                   const struct ceph_object_locator *oloc,
0301                   struct ceph_pg *raw_pgid);
0302 
0303 void ceph_pg_to_up_acting_osds(struct ceph_osdmap *osdmap,
0304                    struct ceph_pg_pool_info *pi,
0305                    const struct ceph_pg *raw_pgid,
0306                    struct ceph_osds *up,
0307                    struct ceph_osds *acting);
0308 bool ceph_pg_to_primary_shard(struct ceph_osdmap *osdmap,
0309                   struct ceph_pg_pool_info *pi,
0310                   const struct ceph_pg *raw_pgid,
0311                   struct ceph_spg *spgid);
0312 int ceph_pg_to_acting_primary(struct ceph_osdmap *osdmap,
0313                   const struct ceph_pg *raw_pgid);
0314 
0315 struct crush_loc {
0316     char *cl_type_name;
0317     char *cl_name;
0318 };
0319 
0320 struct crush_loc_node {
0321     struct rb_node cl_node;
0322     struct crush_loc cl_loc;  /* pointers into cl_data */
0323     char cl_data[];
0324 };
0325 
0326 int ceph_parse_crush_location(char *crush_location, struct rb_root *locs);
0327 int ceph_compare_crush_locs(struct rb_root *locs1, struct rb_root *locs2);
0328 void ceph_clear_crush_locs(struct rb_root *locs);
0329 
0330 int ceph_get_crush_locality(struct ceph_osdmap *osdmap, int id,
0331                 struct rb_root *locs);
0332 
0333 extern struct ceph_pg_pool_info *ceph_pg_pool_by_id(struct ceph_osdmap *map,
0334                             u64 id);
0335 extern const char *ceph_pg_pool_name_by_id(struct ceph_osdmap *map, u64 id);
0336 extern int ceph_pg_poolid_by_name(struct ceph_osdmap *map, const char *name);
0337 u64 ceph_pg_pool_flags(struct ceph_osdmap *map, u64 id);
0338 
0339 #endif