osdmap.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  1. #include <asm/div64.h>
  2. #include "super.h"
  3. #include "osdmap.h"
  4. #include "crush/hash.h"
  5. #include "crush/mapper.h"
  6. #include "decode.h"
  7. #include "ceph_debug.h"
  8. char *ceph_osdmap_state_str(char *str, int len, int state)
  9. {
  10. int flag = 0;
  11. if (!len)
  12. goto done;
  13. *str = '\0';
  14. if (state) {
  15. if (state & CEPH_OSD_EXISTS) {
  16. snprintf(str, len, "exists");
  17. flag = 1;
  18. }
  19. if (state & CEPH_OSD_UP) {
  20. snprintf(str, len, "%s%s%s", str, (flag ? ", " : ""),
  21. "up");
  22. flag = 1;
  23. }
  24. } else {
  25. snprintf(str, len, "doesn't exist");
  26. }
  27. done:
  28. return str;
  29. }
  30. /* maps */
  31. static int calc_bits_of(unsigned t)
  32. {
  33. int b = 0;
  34. while (t) {
  35. t = t >> 1;
  36. b++;
  37. }
  38. return b;
  39. }
  40. /*
  41. * the foo_mask is the smallest value 2^n-1 that is >= foo.
  42. */
  43. static void calc_pg_masks(struct ceph_pg_pool_info *pi)
  44. {
  45. pi->pg_num_mask = (1 << calc_bits_of(le32_to_cpu(pi->v.pg_num)-1)) - 1;
  46. pi->pgp_num_mask =
  47. (1 << calc_bits_of(le32_to_cpu(pi->v.pgp_num)-1)) - 1;
  48. pi->lpg_num_mask =
  49. (1 << calc_bits_of(le32_to_cpu(pi->v.lpg_num)-1)) - 1;
  50. pi->lpgp_num_mask =
  51. (1 << calc_bits_of(le32_to_cpu(pi->v.lpgp_num)-1)) - 1;
  52. }
  53. /*
  54. * decode crush map
  55. */
  56. static int crush_decode_uniform_bucket(void **p, void *end,
  57. struct crush_bucket_uniform *b)
  58. {
  59. dout("crush_decode_uniform_bucket %p to %p\n", *p, end);
  60. ceph_decode_need(p, end, (1+b->h.size) * sizeof(u32), bad);
  61. b->item_weight = ceph_decode_32(p);
  62. return 0;
  63. bad:
  64. return -EINVAL;
  65. }
  66. static int crush_decode_list_bucket(void **p, void *end,
  67. struct crush_bucket_list *b)
  68. {
  69. int j;
  70. dout("crush_decode_list_bucket %p to %p\n", *p, end);
  71. b->item_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
  72. if (b->item_weights == NULL)
  73. return -ENOMEM;
  74. b->sum_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
  75. if (b->sum_weights == NULL)
  76. return -ENOMEM;
  77. ceph_decode_need(p, end, 2 * b->h.size * sizeof(u32), bad);
  78. for (j = 0; j < b->h.size; j++) {
  79. b->item_weights[j] = ceph_decode_32(p);
  80. b->sum_weights[j] = ceph_decode_32(p);
  81. }
  82. return 0;
  83. bad:
  84. return -EINVAL;
  85. }
  86. static int crush_decode_tree_bucket(void **p, void *end,
  87. struct crush_bucket_tree *b)
  88. {
  89. int j;
  90. dout("crush_decode_tree_bucket %p to %p\n", *p, end);
  91. ceph_decode_32_safe(p, end, b->num_nodes, bad);
  92. b->node_weights = kcalloc(b->num_nodes, sizeof(u32), GFP_NOFS);
  93. if (b->node_weights == NULL)
  94. return -ENOMEM;
  95. ceph_decode_need(p, end, b->num_nodes * sizeof(u32), bad);
  96. for (j = 0; j < b->num_nodes; j++)
  97. b->node_weights[j] = ceph_decode_32(p);
  98. return 0;
  99. bad:
  100. return -EINVAL;
  101. }
  102. static int crush_decode_straw_bucket(void **p, void *end,
  103. struct crush_bucket_straw *b)
  104. {
  105. int j;
  106. dout("crush_decode_straw_bucket %p to %p\n", *p, end);
  107. b->item_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
  108. if (b->item_weights == NULL)
  109. return -ENOMEM;
  110. b->straws = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
  111. if (b->straws == NULL)
  112. return -ENOMEM;
  113. ceph_decode_need(p, end, 2 * b->h.size * sizeof(u32), bad);
  114. for (j = 0; j < b->h.size; j++) {
  115. b->item_weights[j] = ceph_decode_32(p);
  116. b->straws[j] = ceph_decode_32(p);
  117. }
  118. return 0;
  119. bad:
  120. return -EINVAL;
  121. }
  122. static struct crush_map *crush_decode(void *pbyval, void *end)
  123. {
  124. struct crush_map *c;
  125. int err = -EINVAL;
  126. int i, j;
  127. void **p = &pbyval;
  128. void *start = pbyval;
  129. u32 magic;
  130. dout("crush_decode %p to %p len %d\n", *p, end, (int)(end - *p));
  131. c = kzalloc(sizeof(*c), GFP_NOFS);
  132. if (c == NULL)
  133. return ERR_PTR(-ENOMEM);
  134. ceph_decode_need(p, end, 4*sizeof(u32), bad);
  135. magic = ceph_decode_32(p);
  136. if (magic != CRUSH_MAGIC) {
  137. pr_err("crush_decode magic %x != current %x\n",
  138. (unsigned)magic, (unsigned)CRUSH_MAGIC);
  139. goto bad;
  140. }
  141. c->max_buckets = ceph_decode_32(p);
  142. c->max_rules = ceph_decode_32(p);
  143. c->max_devices = ceph_decode_32(p);
  144. c->device_parents = kcalloc(c->max_devices, sizeof(u32), GFP_NOFS);
  145. if (c->device_parents == NULL)
  146. goto badmem;
  147. c->bucket_parents = kcalloc(c->max_buckets, sizeof(u32), GFP_NOFS);
  148. if (c->bucket_parents == NULL)
  149. goto badmem;
  150. c->buckets = kcalloc(c->max_buckets, sizeof(*c->buckets), GFP_NOFS);
  151. if (c->buckets == NULL)
  152. goto badmem;
  153. c->rules = kcalloc(c->max_rules, sizeof(*c->rules), GFP_NOFS);
  154. if (c->rules == NULL)
  155. goto badmem;
  156. /* buckets */
  157. for (i = 0; i < c->max_buckets; i++) {
  158. int size = 0;
  159. u32 alg;
  160. struct crush_bucket *b;
  161. ceph_decode_32_safe(p, end, alg, bad);
  162. if (alg == 0) {
  163. c->buckets[i] = NULL;
  164. continue;
  165. }
  166. dout("crush_decode bucket %d off %x %p to %p\n",
  167. i, (int)(*p-start), *p, end);
  168. switch (alg) {
  169. case CRUSH_BUCKET_UNIFORM:
  170. size = sizeof(struct crush_bucket_uniform);
  171. break;
  172. case CRUSH_BUCKET_LIST:
  173. size = sizeof(struct crush_bucket_list);
  174. break;
  175. case CRUSH_BUCKET_TREE:
  176. size = sizeof(struct crush_bucket_tree);
  177. break;
  178. case CRUSH_BUCKET_STRAW:
  179. size = sizeof(struct crush_bucket_straw);
  180. break;
  181. default:
  182. goto bad;
  183. }
  184. BUG_ON(size == 0);
  185. b = c->buckets[i] = kzalloc(size, GFP_NOFS);
  186. if (b == NULL)
  187. goto badmem;
  188. ceph_decode_need(p, end, 4*sizeof(u32), bad);
  189. b->id = ceph_decode_32(p);
  190. b->type = ceph_decode_16(p);
  191. b->alg = ceph_decode_16(p);
  192. b->weight = ceph_decode_32(p);
  193. b->size = ceph_decode_32(p);
  194. dout("crush_decode bucket size %d off %x %p to %p\n",
  195. b->size, (int)(*p-start), *p, end);
  196. b->items = kcalloc(b->size, sizeof(__s32), GFP_NOFS);
  197. if (b->items == NULL)
  198. goto badmem;
  199. b->perm = kcalloc(b->size, sizeof(u32), GFP_NOFS);
  200. if (b->perm == NULL)
  201. goto badmem;
  202. b->perm_n = 0;
  203. ceph_decode_need(p, end, b->size*sizeof(u32), bad);
  204. for (j = 0; j < b->size; j++)
  205. b->items[j] = ceph_decode_32(p);
  206. switch (b->alg) {
  207. case CRUSH_BUCKET_UNIFORM:
  208. err = crush_decode_uniform_bucket(p, end,
  209. (struct crush_bucket_uniform *)b);
  210. if (err < 0)
  211. goto bad;
  212. break;
  213. case CRUSH_BUCKET_LIST:
  214. err = crush_decode_list_bucket(p, end,
  215. (struct crush_bucket_list *)b);
  216. if (err < 0)
  217. goto bad;
  218. break;
  219. case CRUSH_BUCKET_TREE:
  220. err = crush_decode_tree_bucket(p, end,
  221. (struct crush_bucket_tree *)b);
  222. if (err < 0)
  223. goto bad;
  224. break;
  225. case CRUSH_BUCKET_STRAW:
  226. err = crush_decode_straw_bucket(p, end,
  227. (struct crush_bucket_straw *)b);
  228. if (err < 0)
  229. goto bad;
  230. break;
  231. }
  232. }
  233. /* rules */
  234. dout("rule vec is %p\n", c->rules);
  235. for (i = 0; i < c->max_rules; i++) {
  236. u32 yes;
  237. struct crush_rule *r;
  238. ceph_decode_32_safe(p, end, yes, bad);
  239. if (!yes) {
  240. dout("crush_decode NO rule %d off %x %p to %p\n",
  241. i, (int)(*p-start), *p, end);
  242. c->rules[i] = NULL;
  243. continue;
  244. }
  245. dout("crush_decode rule %d off %x %p to %p\n",
  246. i, (int)(*p-start), *p, end);
  247. /* len */
  248. ceph_decode_32_safe(p, end, yes, bad);
  249. #if BITS_PER_LONG == 32
  250. if (yes > ULONG_MAX / sizeof(struct crush_rule_step))
  251. goto bad;
  252. #endif
  253. r = c->rules[i] = kmalloc(sizeof(*r) +
  254. yes*sizeof(struct crush_rule_step),
  255. GFP_NOFS);
  256. if (r == NULL)
  257. goto badmem;
  258. dout(" rule %d is at %p\n", i, r);
  259. r->len = yes;
  260. ceph_decode_copy_safe(p, end, &r->mask, 4, bad); /* 4 u8's */
  261. ceph_decode_need(p, end, r->len*3*sizeof(u32), bad);
  262. for (j = 0; j < r->len; j++) {
  263. r->steps[j].op = ceph_decode_32(p);
  264. r->steps[j].arg1 = ceph_decode_32(p);
  265. r->steps[j].arg2 = ceph_decode_32(p);
  266. }
  267. }
  268. /* ignore trailing name maps. */
  269. dout("crush_decode success\n");
  270. return c;
  271. badmem:
  272. err = -ENOMEM;
  273. bad:
  274. dout("crush_decode fail %d\n", err);
  275. crush_destroy(c);
  276. return ERR_PTR(err);
  277. }
  278. /*
  279. * osd map
  280. */
  281. void ceph_osdmap_destroy(struct ceph_osdmap *map)
  282. {
  283. dout("osdmap_destroy %p\n", map);
  284. if (map->crush)
  285. crush_destroy(map->crush);
  286. while (!RB_EMPTY_ROOT(&map->pg_temp))
  287. rb_erase(rb_first(&map->pg_temp), &map->pg_temp);
  288. kfree(map->osd_state);
  289. kfree(map->osd_weight);
  290. kfree(map->pg_pool);
  291. kfree(map->osd_addr);
  292. kfree(map);
  293. }
  294. /*
  295. * adjust max osd value. reallocate arrays.
  296. */
  297. static int osdmap_set_max_osd(struct ceph_osdmap *map, int max)
  298. {
  299. u8 *state;
  300. struct ceph_entity_addr *addr;
  301. u32 *weight;
  302. state = kcalloc(max, sizeof(*state), GFP_NOFS);
  303. addr = kcalloc(max, sizeof(*addr), GFP_NOFS);
  304. weight = kcalloc(max, sizeof(*weight), GFP_NOFS);
  305. if (state == NULL || addr == NULL || weight == NULL) {
  306. kfree(state);
  307. kfree(addr);
  308. kfree(weight);
  309. return -ENOMEM;
  310. }
  311. /* copy old? */
  312. if (map->osd_state) {
  313. memcpy(state, map->osd_state, map->max_osd*sizeof(*state));
  314. memcpy(addr, map->osd_addr, map->max_osd*sizeof(*addr));
  315. memcpy(weight, map->osd_weight, map->max_osd*sizeof(*weight));
  316. kfree(map->osd_state);
  317. kfree(map->osd_addr);
  318. kfree(map->osd_weight);
  319. }
  320. map->osd_state = state;
  321. map->osd_weight = weight;
  322. map->osd_addr = addr;
  323. map->max_osd = max;
  324. return 0;
  325. }
  326. /*
  327. * Insert a new pg_temp mapping
  328. */
  329. static int __insert_pg_mapping(struct ceph_pg_mapping *new,
  330. struct rb_root *root)
  331. {
  332. struct rb_node **p = &root->rb_node;
  333. struct rb_node *parent = NULL;
  334. struct ceph_pg_mapping *pg = NULL;
  335. while (*p) {
  336. parent = *p;
  337. pg = rb_entry(parent, struct ceph_pg_mapping, node);
  338. if (new->pgid < pg->pgid)
  339. p = &(*p)->rb_left;
  340. else if (new->pgid > pg->pgid)
  341. p = &(*p)->rb_right;
  342. else
  343. return -EEXIST;
  344. }
  345. rb_link_node(&new->node, parent, p);
  346. rb_insert_color(&new->node, root);
  347. return 0;
  348. }
  349. /*
  350. * decode a full map.
  351. */
  352. struct ceph_osdmap *osdmap_decode(void **p, void *end)
  353. {
  354. struct ceph_osdmap *map;
  355. u16 version;
  356. u32 len, max, i;
  357. int err = -EINVAL;
  358. void *start = *p;
  359. dout("osdmap_decode %p to %p len %d\n", *p, end, (int)(end - *p));
  360. map = kzalloc(sizeof(*map), GFP_NOFS);
  361. if (map == NULL)
  362. return ERR_PTR(-ENOMEM);
  363. map->pg_temp = RB_ROOT;
  364. ceph_decode_16_safe(p, end, version, bad);
  365. ceph_decode_need(p, end, 2*sizeof(u64)+6*sizeof(u32), bad);
  366. ceph_decode_copy(p, &map->fsid, sizeof(map->fsid));
  367. map->epoch = ceph_decode_32(p);
  368. ceph_decode_copy(p, &map->created, sizeof(map->created));
  369. ceph_decode_copy(p, &map->modified, sizeof(map->modified));
  370. map->num_pools = ceph_decode_32(p);
  371. map->pg_pool = kcalloc(map->num_pools, sizeof(*map->pg_pool),
  372. GFP_NOFS);
  373. if (!map->pg_pool) {
  374. err = -ENOMEM;
  375. goto bad;
  376. }
  377. ceph_decode_32_safe(p, end, max, bad);
  378. while (max--) {
  379. ceph_decode_need(p, end, 4+sizeof(map->pg_pool->v), bad);
  380. i = ceph_decode_32(p);
  381. if (i >= map->num_pools)
  382. goto bad;
  383. ceph_decode_copy(p, &map->pg_pool[i].v,
  384. sizeof(map->pg_pool->v));
  385. calc_pg_masks(&map->pg_pool[i]);
  386. p += le32_to_cpu(map->pg_pool[i].v.num_snaps) * sizeof(u64);
  387. p += le32_to_cpu(map->pg_pool[i].v.num_removed_snap_intervals)
  388. * sizeof(u64) * 2;
  389. }
  390. ceph_decode_32_safe(p, end, map->flags, bad);
  391. max = ceph_decode_32(p);
  392. /* (re)alloc osd arrays */
  393. err = osdmap_set_max_osd(map, max);
  394. if (err < 0)
  395. goto bad;
  396. dout("osdmap_decode max_osd = %d\n", map->max_osd);
  397. /* osds */
  398. err = -EINVAL;
  399. ceph_decode_need(p, end, 3*sizeof(u32) +
  400. map->max_osd*(1 + sizeof(*map->osd_weight) +
  401. sizeof(*map->osd_addr)), bad);
  402. *p += 4; /* skip length field (should match max) */
  403. ceph_decode_copy(p, map->osd_state, map->max_osd);
  404. *p += 4; /* skip length field (should match max) */
  405. for (i = 0; i < map->max_osd; i++)
  406. map->osd_weight[i] = ceph_decode_32(p);
  407. *p += 4; /* skip length field (should match max) */
  408. ceph_decode_copy(p, map->osd_addr, map->max_osd*sizeof(*map->osd_addr));
  409. /* pg_temp */
  410. ceph_decode_32_safe(p, end, len, bad);
  411. for (i = 0; i < len; i++) {
  412. int n, j;
  413. u64 pgid;
  414. struct ceph_pg_mapping *pg;
  415. ceph_decode_need(p, end, sizeof(u32) + sizeof(u64), bad);
  416. pgid = ceph_decode_64(p);
  417. n = ceph_decode_32(p);
  418. ceph_decode_need(p, end, n * sizeof(u32), bad);
  419. pg = kmalloc(sizeof(*pg) + n*sizeof(u32), GFP_NOFS);
  420. if (!pg) {
  421. err = -ENOMEM;
  422. goto bad;
  423. }
  424. pg->pgid = pgid;
  425. pg->len = n;
  426. for (j = 0; j < n; j++)
  427. pg->osds[j] = ceph_decode_32(p);
  428. err = __insert_pg_mapping(pg, &map->pg_temp);
  429. if (err)
  430. goto bad;
  431. dout(" added pg_temp %llx len %d\n", pgid, len);
  432. }
  433. /* crush */
  434. ceph_decode_32_safe(p, end, len, bad);
  435. dout("osdmap_decode crush len %d from off 0x%x\n", len,
  436. (int)(*p - start));
  437. ceph_decode_need(p, end, len, bad);
  438. map->crush = crush_decode(*p, end);
  439. *p += len;
  440. if (IS_ERR(map->crush)) {
  441. err = PTR_ERR(map->crush);
  442. map->crush = NULL;
  443. goto bad;
  444. }
  445. /* ignore the rest of the map */
  446. *p = end;
  447. dout("osdmap_decode done %p %p\n", *p, end);
  448. return map;
  449. bad:
  450. dout("osdmap_decode fail\n");
  451. ceph_osdmap_destroy(map);
  452. return ERR_PTR(err);
  453. }
  454. /*
  455. * decode and apply an incremental map update.
  456. */
  457. struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end,
  458. struct ceph_osdmap *map,
  459. struct ceph_messenger *msgr)
  460. {
  461. struct ceph_osdmap *newmap = map;
  462. struct crush_map *newcrush = NULL;
  463. struct ceph_fsid fsid;
  464. u32 epoch = 0;
  465. struct ceph_timespec modified;
  466. u32 len, pool;
  467. __s32 new_flags, max;
  468. void *start = *p;
  469. int err = -EINVAL;
  470. u16 version;
  471. struct rb_node *rbp;
  472. ceph_decode_16_safe(p, end, version, bad);
  473. ceph_decode_need(p, end, sizeof(fsid)+sizeof(modified)+2*sizeof(u32),
  474. bad);
  475. ceph_decode_copy(p, &fsid, sizeof(fsid));
  476. epoch = ceph_decode_32(p);
  477. BUG_ON(epoch != map->epoch+1);
  478. ceph_decode_copy(p, &modified, sizeof(modified));
  479. new_flags = ceph_decode_32(p);
  480. /* full map? */
  481. ceph_decode_32_safe(p, end, len, bad);
  482. if (len > 0) {
  483. dout("apply_incremental full map len %d, %p to %p\n",
  484. len, *p, end);
  485. newmap = osdmap_decode(p, min(*p+len, end));
  486. return newmap; /* error or not */
  487. }
  488. /* new crush? */
  489. ceph_decode_32_safe(p, end, len, bad);
  490. if (len > 0) {
  491. dout("apply_incremental new crush map len %d, %p to %p\n",
  492. len, *p, end);
  493. newcrush = crush_decode(*p, min(*p+len, end));
  494. if (IS_ERR(newcrush))
  495. return ERR_PTR(PTR_ERR(newcrush));
  496. }
  497. /* new flags? */
  498. if (new_flags >= 0)
  499. map->flags = new_flags;
  500. ceph_decode_need(p, end, 5*sizeof(u32), bad);
  501. /* new max? */
  502. max = ceph_decode_32(p);
  503. if (max >= 0) {
  504. err = osdmap_set_max_osd(map, max);
  505. if (err < 0)
  506. goto bad;
  507. }
  508. map->epoch++;
  509. map->modified = map->modified;
  510. if (newcrush) {
  511. if (map->crush)
  512. crush_destroy(map->crush);
  513. map->crush = newcrush;
  514. newcrush = NULL;
  515. }
  516. /* new_pool */
  517. ceph_decode_32_safe(p, end, len, bad);
  518. while (len--) {
  519. ceph_decode_32_safe(p, end, pool, bad);
  520. if (pool >= map->num_pools) {
  521. void *pg_pool = kcalloc(pool + 1,
  522. sizeof(*map->pg_pool),
  523. GFP_NOFS);
  524. if (!pg_pool) {
  525. err = -ENOMEM;
  526. goto bad;
  527. }
  528. memcpy(pg_pool, map->pg_pool,
  529. map->num_pools * sizeof(*map->pg_pool));
  530. kfree(map->pg_pool);
  531. map->pg_pool = pg_pool;
  532. map->num_pools = pool+1;
  533. }
  534. ceph_decode_copy(p, &map->pg_pool[pool].v,
  535. sizeof(map->pg_pool->v));
  536. calc_pg_masks(&map->pg_pool[pool]);
  537. }
  538. /* old_pool (ignore) */
  539. ceph_decode_32_safe(p, end, len, bad);
  540. *p += len * sizeof(u32);
  541. /* new_up */
  542. err = -EINVAL;
  543. ceph_decode_32_safe(p, end, len, bad);
  544. while (len--) {
  545. u32 osd;
  546. struct ceph_entity_addr addr;
  547. ceph_decode_32_safe(p, end, osd, bad);
  548. ceph_decode_copy_safe(p, end, &addr, sizeof(addr), bad);
  549. pr_info("osd%d up\n", osd);
  550. BUG_ON(osd >= map->max_osd);
  551. map->osd_state[osd] |= CEPH_OSD_UP;
  552. map->osd_addr[osd] = addr;
  553. }
  554. /* new_down */
  555. ceph_decode_32_safe(p, end, len, bad);
  556. while (len--) {
  557. u32 osd;
  558. ceph_decode_32_safe(p, end, osd, bad);
  559. (*p)++; /* clean flag */
  560. pr_info("ceph osd%d down\n", osd);
  561. if (osd < map->max_osd)
  562. map->osd_state[osd] &= ~CEPH_OSD_UP;
  563. }
  564. /* new_weight */
  565. ceph_decode_32_safe(p, end, len, bad);
  566. while (len--) {
  567. u32 osd, off;
  568. ceph_decode_need(p, end, sizeof(u32)*2, bad);
  569. osd = ceph_decode_32(p);
  570. off = ceph_decode_32(p);
  571. pr_info("osd%d weight 0x%x %s\n", osd, off,
  572. off == CEPH_OSD_IN ? "(in)" :
  573. (off == CEPH_OSD_OUT ? "(out)" : ""));
  574. if (osd < map->max_osd)
  575. map->osd_weight[osd] = off;
  576. }
  577. /* new_pg_temp */
  578. rbp = rb_first(&map->pg_temp);
  579. ceph_decode_32_safe(p, end, len, bad);
  580. while (len--) {
  581. struct ceph_pg_mapping *pg;
  582. int j;
  583. u64 pgid;
  584. u32 pglen;
  585. ceph_decode_need(p, end, sizeof(u64) + sizeof(u32), bad);
  586. pgid = ceph_decode_64(p);
  587. pglen = ceph_decode_32(p);
  588. /* remove any? */
  589. while (rbp && rb_entry(rbp, struct ceph_pg_mapping,
  590. node)->pgid <= pgid) {
  591. struct rb_node *cur = rbp;
  592. rbp = rb_next(rbp);
  593. dout(" removed pg_temp %llx\n",
  594. rb_entry(cur, struct ceph_pg_mapping, node)->pgid);
  595. rb_erase(cur, &map->pg_temp);
  596. }
  597. if (pglen) {
  598. /* insert */
  599. ceph_decode_need(p, end, pglen*sizeof(u32), bad);
  600. pg = kmalloc(sizeof(*pg) + sizeof(u32)*pglen, GFP_NOFS);
  601. if (!pg) {
  602. err = -ENOMEM;
  603. goto bad;
  604. }
  605. pg->pgid = pgid;
  606. pg->len = pglen;
  607. for (j = 0; j < len; j++)
  608. pg->osds[j] = ceph_decode_32(p);
  609. err = __insert_pg_mapping(pg, &map->pg_temp);
  610. if (err)
  611. goto bad;
  612. dout(" added pg_temp %llx len %d\n", pgid, pglen);
  613. }
  614. }
  615. while (rbp) {
  616. struct rb_node *cur = rbp;
  617. rbp = rb_next(rbp);
  618. dout(" removed pg_temp %llx\n",
  619. rb_entry(cur, struct ceph_pg_mapping, node)->pgid);
  620. rb_erase(cur, &map->pg_temp);
  621. }
  622. /* ignore the rest */
  623. *p = end;
  624. return map;
  625. bad:
  626. pr_err("corrupt inc osdmap epoch %d off %d (%p of %p-%p)\n",
  627. epoch, (int)(*p - start), *p, start, end);
  628. if (newcrush)
  629. crush_destroy(newcrush);
  630. return ERR_PTR(err);
  631. }
  632. /*
  633. * calculate file layout from given offset, length.
  634. * fill in correct oid, logical length, and object extent
  635. * offset, length.
  636. *
  637. * for now, we write only a single su, until we can
  638. * pass a stride back to the caller.
  639. */
  640. void ceph_calc_file_object_mapping(struct ceph_file_layout *layout,
  641. u64 off, u64 *plen,
  642. u64 *ono,
  643. u64 *oxoff, u64 *oxlen)
  644. {
  645. u32 osize = le32_to_cpu(layout->fl_object_size);
  646. u32 su = le32_to_cpu(layout->fl_stripe_unit);
  647. u32 sc = le32_to_cpu(layout->fl_stripe_count);
  648. u32 bl, stripeno, stripepos, objsetno;
  649. u32 su_per_object;
  650. u64 t;
  651. dout("mapping %llu~%llu osize %u fl_su %u\n", off, *plen,
  652. osize, su);
  653. su_per_object = osize / su;
  654. dout("osize %u / su %u = su_per_object %u\n", osize, su,
  655. su_per_object);
  656. BUG_ON((su & ~PAGE_MASK) != 0);
  657. /* bl = *off / su; */
  658. t = off;
  659. do_div(t, su);
  660. bl = t;
  661. dout("off %llu / su %u = bl %u\n", off, su, bl);
  662. stripeno = bl / sc;
  663. stripepos = bl % sc;
  664. objsetno = stripeno / su_per_object;
  665. *ono = objsetno * sc + stripepos;
  666. dout("objset %u * sc %u = ono %u\n", objsetno, sc, (unsigned)*ono);
  667. /* *oxoff = *off % layout->fl_stripe_unit; # offset in su */
  668. t = off;
  669. *oxoff = do_div(t, su);
  670. *oxoff += (stripeno % su_per_object) * su;
  671. *oxlen = min_t(u64, *plen, su - *oxoff);
  672. *plen = *oxlen;
  673. dout(" obj extent %llu~%llu\n", *oxoff, *oxlen);
  674. }
  675. /*
  676. * calculate an object layout (i.e. pgid) from an oid,
  677. * file_layout, and osdmap
  678. */
  679. int ceph_calc_object_layout(struct ceph_object_layout *ol,
  680. const char *oid,
  681. struct ceph_file_layout *fl,
  682. struct ceph_osdmap *osdmap)
  683. {
  684. unsigned num, num_mask;
  685. union ceph_pg pgid;
  686. s32 preferred = (s32)le32_to_cpu(fl->fl_pg_preferred);
  687. int poolid = le32_to_cpu(fl->fl_pg_pool);
  688. struct ceph_pg_pool_info *pool;
  689. if (poolid >= osdmap->num_pools)
  690. return -EIO;
  691. pool = &osdmap->pg_pool[poolid];
  692. if (preferred >= 0) {
  693. num = le32_to_cpu(pool->v.lpg_num);
  694. num_mask = pool->lpg_num_mask;
  695. } else {
  696. num = le32_to_cpu(pool->v.pg_num);
  697. num_mask = pool->pg_num_mask;
  698. }
  699. pgid.pg64 = 0; /* start with it zeroed out */
  700. pgid.pg.ps = ceph_full_name_hash(oid, strlen(oid));
  701. pgid.pg.preferred = preferred;
  702. if (preferred >= 0)
  703. pgid.pg.ps += preferred;
  704. pgid.pg.pool = le32_to_cpu(fl->fl_pg_pool);
  705. if (preferred >= 0)
  706. dout("calc_object_layout '%s' pgid %d.%xp%d (%llx)\n", oid,
  707. pgid.pg.pool, pgid.pg.ps, (int)preferred, pgid.pg64);
  708. else
  709. dout("calc_object_layout '%s' pgid %d.%x (%llx)\n", oid,
  710. pgid.pg.pool, pgid.pg.ps, pgid.pg64);
  711. ol->ol_pgid = cpu_to_le64(pgid.pg64);
  712. ol->ol_stripe_unit = fl->fl_object_stripe_unit;
  713. return 0;
  714. }
  715. /*
  716. * Calculate raw osd vector for the given pgid. Return pointer to osd
  717. * array, or NULL on failure.
  718. */
  719. static int *calc_pg_raw(struct ceph_osdmap *osdmap, union ceph_pg pgid,
  720. int *osds, int *num)
  721. {
  722. struct rb_node *n = osdmap->pg_temp.rb_node;
  723. struct ceph_pg_mapping *pg;
  724. struct ceph_pg_pool_info *pool;
  725. int ruleno;
  726. unsigned pps; /* placement ps */
  727. /* pg_temp? */
  728. while (n) {
  729. pg = rb_entry(n, struct ceph_pg_mapping, node);
  730. if (pgid.pg64 < pg->pgid)
  731. n = n->rb_left;
  732. else if (pgid.pg64 > pg->pgid)
  733. n = n->rb_right;
  734. else {
  735. *num = pg->len;
  736. return pg->osds;
  737. }
  738. }
  739. /* crush */
  740. if (pgid.pg.pool >= osdmap->num_pools)
  741. return NULL;
  742. pool = &osdmap->pg_pool[pgid.pg.pool];
  743. ruleno = crush_find_rule(osdmap->crush, pool->v.crush_ruleset,
  744. pool->v.type, pool->v.size);
  745. if (ruleno < 0) {
  746. pr_err("no crush rule pool %d type %d size %d\n",
  747. pgid.pg.pool, pool->v.type, pool->v.size);
  748. return NULL;
  749. }
  750. if (pgid.pg.preferred >= 0)
  751. pps = ceph_stable_mod(pgid.pg.ps,
  752. le32_to_cpu(pool->v.lpgp_num),
  753. pool->lpgp_num_mask);
  754. else
  755. pps = ceph_stable_mod(pgid.pg.ps,
  756. le32_to_cpu(pool->v.pgp_num),
  757. pool->pgp_num_mask);
  758. pps += pgid.pg.pool;
  759. *num = crush_do_rule(osdmap->crush, ruleno, pps, osds,
  760. min_t(int, pool->v.size, *num),
  761. pgid.pg.preferred, osdmap->osd_weight);
  762. return osds;
  763. }
  764. /*
  765. * Return primary osd for given pgid, or -1 if none.
  766. */
  767. int ceph_calc_pg_primary(struct ceph_osdmap *osdmap, union ceph_pg pgid)
  768. {
  769. int rawosds[10], *osds;
  770. int i, num = ARRAY_SIZE(rawosds);
  771. osds = calc_pg_raw(osdmap, pgid, rawosds, &num);
  772. if (!osds)
  773. return -1;
  774. /* primary is first up osd */
  775. for (i = 0; i < num; i++)
  776. if (ceph_osd_is_up(osdmap, osds[i])) {
  777. return osds[i];
  778. break;
  779. }
  780. return -1;
  781. }