Skip to content
osdmap.c 31.1 KiB
Newer Older
	err = get_osdmap_client_data_v(p, end, "inc", &struct_v);
	if (err)
		goto bad;
Sage Weil's avatar
Sage Weil committed

	/* fsid, epoch, modified, new_pool_max, new_flags */
	ceph_decode_need(p, end, sizeof(fsid) + sizeof(u32) + sizeof(modified) +
			 sizeof(u64) + sizeof(u32), e_inval);
Sage Weil's avatar
Sage Weil committed
	ceph_decode_copy(p, &fsid, sizeof(fsid));
	epoch = ceph_decode_32(p);
Sage Weil's avatar
Sage Weil committed
	BUG_ON(epoch != map->epoch+1);
	ceph_decode_copy(p, &modified, sizeof(modified));
	new_pool_max = ceph_decode_64(p);
	new_flags = ceph_decode_32(p);
Sage Weil's avatar
Sage Weil committed

	/* full map? */
	ceph_decode_32_safe(p, end, len, e_inval);
Sage Weil's avatar
Sage Weil committed
	if (len > 0) {
		dout("apply_incremental full map len %d, %p to %p\n",
		     len, *p, end);
		return ceph_osdmap_decode(p, min(*p+len, end));
Sage Weil's avatar
Sage Weil committed
	}

	/* new crush? */
	ceph_decode_32_safe(p, end, len, e_inval);
Sage Weil's avatar
Sage Weil committed
	if (len > 0) {
		newcrush = crush_decode(*p, min(*p+len, end));
		if (IS_ERR(newcrush)) {
			err = PTR_ERR(newcrush);
			newcrush = NULL;
			goto bad;
		}
		*p += len;
Sage Weil's avatar
Sage Weil committed
	}

	/* new flags? */
	if (new_flags >= 0)
		map->flags = new_flags;
	if (new_pool_max >= 0)
		map->pool_max = new_pool_max;
Sage Weil's avatar
Sage Weil committed

	/* new max? */
	ceph_decode_32_safe(p, end, max, e_inval);
Sage Weil's avatar
Sage Weil committed
	if (max >= 0) {
		err = osdmap_set_max_osd(map, max);
Sage Weil's avatar
Sage Weil committed
			goto bad;
	}

	map->epoch++;
	map->modified = modified;
Sage Weil's avatar
Sage Weil committed
	if (newcrush) {
		if (map->crush)
			crush_destroy(map->crush);
		map->crush = newcrush;
		newcrush = NULL;
	}

	/* new_pools */
	err = decode_new_pools(p, end, map);
	if (err)
		goto bad;
	/* new_pool_names */
	err = decode_pool_names(p, end, map);
Sage Weil's avatar
Sage Weil committed

	ceph_decode_32_safe(p, end, len, e_inval);
	while (len--) {
		struct ceph_pg_pool_info *pi;

		ceph_decode_64_safe(p, end, pool, e_inval);
		pi = __lookup_pg_pool(&map->pg_pools, pool);
		if (pi)
			__remove_pg_pool(&map->pg_pools, pi);
Sage Weil's avatar
Sage Weil committed

	/* new_up */
	ceph_decode_32_safe(p, end, len, e_inval);
Sage Weil's avatar
Sage Weil committed
	while (len--) {
		u32 osd;
		struct ceph_entity_addr addr;
		ceph_decode_32_safe(p, end, osd, e_inval);
		ceph_decode_copy_safe(p, end, &addr, sizeof(addr), e_inval);
		ceph_decode_addr(&addr);
Sage Weil's avatar
Sage Weil committed
		pr_info("osd%d up\n", osd);
		BUG_ON(osd >= map->max_osd);
		map->osd_state[osd] |= CEPH_OSD_UP;
		map->osd_addr[osd] = addr;
	}

	ceph_decode_32_safe(p, end, len, e_inval);
Sage Weil's avatar
Sage Weil committed
	while (len--) {
		u32 osd;
		ceph_decode_32_safe(p, end, osd, e_inval);
		xorstate = **(u8 **)p;
Sage Weil's avatar
Sage Weil committed
		(*p)++;  /* clean flag */
		if (xorstate == 0)
			xorstate = CEPH_OSD_UP;
		if (xorstate & CEPH_OSD_UP)
			pr_info("osd%d down\n", osd);
Sage Weil's avatar
Sage Weil committed
		if (osd < map->max_osd)
			map->osd_state[osd] ^= xorstate;
Sage Weil's avatar
Sage Weil committed
	}

	/* new_weight */
	ceph_decode_32_safe(p, end, len, e_inval);
Sage Weil's avatar
Sage Weil committed
	while (len--) {
		u32 osd, off;
		ceph_decode_need(p, end, sizeof(u32)*2, e_inval);
		osd = ceph_decode_32(p);
		off = ceph_decode_32(p);
Sage Weil's avatar
Sage Weil committed
		pr_info("osd%d weight 0x%x %s\n", osd, off,
		     off == CEPH_OSD_IN ? "(in)" :
		     (off == CEPH_OSD_OUT ? "(out)" : ""));
		if (osd < map->max_osd)
			map->osd_weight[osd] = off;
	}

	/* new_pg_temp */
	err = decode_new_pg_temp(p, end, map);
	if (err)
		goto bad;
Sage Weil's avatar
Sage Weil committed

	/* ignore the rest */
	*p = end;

	dout("inc osdmap epoch %d max_osd %d\n", map->epoch, map->max_osd);
Sage Weil's avatar
Sage Weil committed
	return map;

Sage Weil's avatar
Sage Weil committed
bad:
	pr_err("corrupt inc osdmap (%d) epoch %d off %d (%p of %p-%p)\n",
	       err, epoch, (int)(*p - start), *p, start, end);
	print_hex_dump(KERN_DEBUG, "osdmap: ",
		       DUMP_PREFIX_OFFSET, 16, 1,
		       start, end - start, true);
Sage Weil's avatar
Sage Weil committed
	if (newcrush)
		crush_destroy(newcrush);
	return ERR_PTR(err);
}




/*
 * calculate file layout from given offset, length.
 * fill in correct oid, logical length, and object extent
 * offset, length.
 *
 * for now, we write only a single su, until we can
 * pass a stride back to the caller.
 */
int ceph_calc_file_object_mapping(struct ceph_file_layout *layout,
Sage Weil's avatar
Sage Weil committed
				   u64 *oxoff, u64 *oxlen)
{
	u32 osize = le32_to_cpu(layout->fl_object_size);
	u32 su = le32_to_cpu(layout->fl_stripe_unit);
	u32 sc = le32_to_cpu(layout->fl_stripe_count);
	u32 bl, stripeno, stripepos, objsetno;
	u32 su_per_object;
	u64 t, su_offset;
Sage Weil's avatar
Sage Weil committed

	dout("mapping %llu~%llu  osize %u fl_su %u\n", off, len,
Sage Weil's avatar
Sage Weil committed
	     osize, su);
	if (su == 0 || sc == 0)
		goto invalid;
	su_per_object = osize / su;
	if (su_per_object == 0)
		goto invalid;
Sage Weil's avatar
Sage Weil committed
	dout("osize %u / su %u = su_per_object %u\n", osize, su,
	     su_per_object);

	if ((su & ~PAGE_MASK) != 0)
		goto invalid;

Sage Weil's avatar
Sage Weil committed
	/* bl = *off / su; */
	t = off;
	do_div(t, su);
	bl = t;
	dout("off %llu / su %u = bl %u\n", off, su, bl);

	stripeno = bl / sc;
	stripepos = bl % sc;
	objsetno = stripeno / su_per_object;

	*ono = objsetno * sc + stripepos;
	dout("objset %u * sc %u = ono %u\n", objsetno, sc, (unsigned int)*ono);

	/* *oxoff = *off % layout->fl_stripe_unit;  # offset in su */
Sage Weil's avatar
Sage Weil committed
	t = off;
	su_offset = do_div(t, su);
	*oxoff = su_offset + (stripeno % su_per_object) * su;

	/*
	 * Calculate the length of the extent being written to the selected
	 * object. This is the minimum of the full length requested (len) or
	 * the remainder of the current stripe being written to.
	 */
	*oxlen = min_t(u64, len, su - su_offset);
Sage Weil's avatar
Sage Weil committed

	dout(" obj extent %llu~%llu\n", *oxoff, *oxlen);
	return 0;

invalid:
	dout(" invalid layout\n");
	*ono = 0;
	*oxoff = 0;
	*oxlen = 0;
	return -EINVAL;
Sage Weil's avatar
Sage Weil committed
}
EXPORT_SYMBOL(ceph_calc_file_object_mapping);
Sage Weil's avatar
Sage Weil committed

/*
 * Calculate mapping of a (oloc, oid) pair to a PG.  Should only be
 * called with target's (oloc, oid), since tiering isn't taken into
 * account.
Sage Weil's avatar
Sage Weil committed
 */
int ceph_oloc_oid_to_pg(struct ceph_osdmap *osdmap,
			struct ceph_object_locator *oloc,
			struct ceph_object_id *oid,
			struct ceph_pg *pg_out)
Sage Weil's avatar
Sage Weil committed
{
	struct ceph_pg_pool_info *pi;
Sage Weil's avatar
Sage Weil committed

	pi = __lookup_pg_pool(&osdmap->pg_pools, oloc->pool);
	if (!pi)
Sage Weil's avatar
Sage Weil committed

	pg_out->pool = oloc->pool;
	pg_out->seed = ceph_str_hash(pi->object_hash, oid->name,
				     oid->name_len);

	dout("%s '%.*s' pgid %llu.%x\n", __func__, oid->name_len, oid->name,
	     pg_out->pool, pg_out->seed);
Sage Weil's avatar
Sage Weil committed
	return 0;
}
EXPORT_SYMBOL(ceph_oloc_oid_to_pg);
Sage Weil's avatar
Sage Weil committed

static int do_crush(struct ceph_osdmap *map, int ruleno, int x,
		    int *result, int result_max,
		    const __u32 *weight, int weight_max)
	int r;

	BUG_ON(result_max > CEPH_PG_MAX_SIZE);

	mutex_lock(&map->crush_scratch_mutex);
	r = crush_do_rule(map->crush, ruleno, x, result, result_max,
			  weight, weight_max, map->crush_scratch_ary);
	mutex_unlock(&map->crush_scratch_mutex);
Sage Weil's avatar
Sage Weil committed
/*
 * Calculate raw osd vector for the given pgid.  Return pointer to osd
 * array, or NULL on failure.
 */
static int *calc_pg_raw(struct ceph_osdmap *osdmap, struct ceph_pg pgid,
Sage Weil's avatar
Sage Weil committed
			int *osds, int *num)
{
	struct ceph_pg_mapping *pg;
	struct ceph_pg_pool_info *pool;
	int ruleno;
Sage Weil's avatar
Sage Weil committed

	pool = __lookup_pg_pool(&osdmap->pg_pools, pgid.pool);
	if (!pool)
		return NULL;

Sage Weil's avatar
Sage Weil committed
	/* pg_temp? */
	pgid.seed = ceph_stable_mod(pgid.seed, pool->pg_num,
	pg = __lookup_pg_mapping(&osdmap->pg_temp, pgid);
	if (pg) {
		*num = pg->len;
		return pg->osds;
Sage Weil's avatar
Sage Weil committed
	}

	/* crush */
	ruleno = crush_find_rule(osdmap->crush, pool->crush_ruleset,
				 pool->type, pool->size);
Sage Weil's avatar
Sage Weil committed
	if (ruleno < 0) {
		pr_err("no crush rule pool %lld ruleset %d type %d size %d\n",
		       pgid.pool, pool->crush_ruleset, pool->type,
Sage Weil's avatar
Sage Weil committed
		return NULL;
	}

	if (pool->flags & CEPH_POOL_FLAG_HASHPSPOOL) {
		/* hash pool id and seed sothat pool PGs do not overlap */
		pps = crush_hash32_2(CRUSH_HASH_RJENKINS1,
				     ceph_stable_mod(pgid.seed, pool->pgp_num,
						     pool->pgp_num_mask),
				     pgid.pool);
	} else {
		/*
		 * legacy ehavior: add ps and pool together.  this is
		 * not a great approach because the PGs from each pool
		 * will overlap on top of each other: 0.5 == 1.4 ==
		 * 2.3 == ...
		 */
		pps = ceph_stable_mod(pgid.seed, pool->pgp_num,
				      pool->pgp_num_mask) +
			(unsigned)pgid.pool;
	}
	r = do_crush(osdmap, ruleno, pps, osds, min_t(int, pool->size, *num),
		     osdmap->osd_weight, osdmap->max_osd);
	if (r < 0) {
		pr_err("error %d from crush rule: pool %lld ruleset %d type %d"
		       " size %d\n", r, pgid.pool, pool->crush_ruleset,
		return NULL;
	}
	*num = r;
Sage Weil's avatar
Sage Weil committed
	return osds;
}

/*
 * Return acting set for given pgid.
 */
int ceph_calc_pg_acting(struct ceph_osdmap *osdmap, struct ceph_pg pgid,
			int *acting)
{
	int rawosds[CEPH_PG_MAX_SIZE], *osds;
	int i, o, num = CEPH_PG_MAX_SIZE;

	osds = calc_pg_raw(osdmap, pgid, rawosds, &num);
	if (!osds)
		return -1;

	/* primary is first up osd */
	o = 0;
	for (i = 0; i < num; i++)
		if (ceph_osd_is_up(osdmap, osds[i]))
			acting[o++] = osds[i];
	return o;
}

Sage Weil's avatar
Sage Weil committed
/*
 * Return primary osd for given pgid, or -1 if none.
 */
int ceph_calc_pg_primary(struct ceph_osdmap *osdmap, struct ceph_pg pgid)
Sage Weil's avatar
Sage Weil committed
{
	int rawosds[CEPH_PG_MAX_SIZE], *osds;
	int i, num = CEPH_PG_MAX_SIZE;
Sage Weil's avatar
Sage Weil committed

	osds = calc_pg_raw(osdmap, pgid, rawosds, &num);
	if (!osds)
		return -1;

	/* primary is first up osd */
	for (i = 0; i < num; i++)
		if (ceph_osd_is_up(osdmap, osds[i]))
Sage Weil's avatar
Sage Weil committed
			return osds[i];
	return -1;
}
EXPORT_SYMBOL(ceph_calc_pg_primary);