Skip to content
osdmap.c 28.8 KiB
Newer Older
Sage Weil's avatar
Sage Weil committed
		}
	}

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

bad:
	pr_err("corrupt inc osdmap epoch %d off %d (%p of %p-%p)\n",
	       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 an object layout (i.e. pgid) from an oid,
 * file_layout, and osdmap
 */
int ceph_calc_ceph_pg(struct ceph_pg *pg, const char *oid,
			struct ceph_osdmap *osdmap, uint64_t pool)
Sage Weil's avatar
Sage Weil committed
{
	struct ceph_pg_pool_info *pool_info;
Sage Weil's avatar
Sage Weil committed

	pool_info = __lookup_pg_pool(&osdmap->pg_pools, pool);
	if (!pool_info)
	pg->pool = pool;
	pg->seed = ceph_str_hash(pool_info->object_hash, oid, strlen(oid));
Sage Weil's avatar
Sage Weil committed

	dout("%s '%s' pgid %lld.%x\n", __func__, oid, pg->pool, pg->seed);
Sage Weil's avatar
Sage Weil committed
	return 0;
}
EXPORT_SYMBOL(ceph_calc_ceph_pg);
Sage Weil's avatar
Sage Weil committed

static int crush_do_rule_ary(const struct crush_map *map, int ruleno, int x,
			     int *result, int result_max,
			     const __u32 *weight, int weight_max)
{
	int scratch[result_max * 3];

	return crush_do_rule(map, ruleno, x, result, result_max,
			     weight, weight_max, scratch);
}

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 = crush_do_rule_ary(osdmap->crush, 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);