osdmap.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  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_8(p);
  192. b->hash = ceph_decode_8(p);
  193. b->weight = ceph_decode_32(p);
  194. b->size = ceph_decode_32(p);
  195. dout("crush_decode bucket size %d off %x %p to %p\n",
  196. b->size, (int)(*p-start), *p, end);
  197. b->items = kcalloc(b->size, sizeof(__s32), GFP_NOFS);
  198. if (b->items == NULL)
  199. goto badmem;
  200. b->perm = kcalloc(b->size, sizeof(u32), GFP_NOFS);
  201. if (b->perm == NULL)
  202. goto badmem;
  203. b->perm_n = 0;
  204. ceph_decode_need(p, end, b->size*sizeof(u32), bad);
  205. for (j = 0; j < b->size; j++)
  206. b->items[j] = ceph_decode_32(p);
  207. switch (b->alg) {
  208. case CRUSH_BUCKET_UNIFORM:
  209. err = crush_decode_uniform_bucket(p, end,
  210. (struct crush_bucket_uniform *)b);
  211. if (err < 0)
  212. goto bad;
  213. break;
  214. case CRUSH_BUCKET_LIST:
  215. err = crush_decode_list_bucket(p, end,
  216. (struct crush_bucket_list *)b);
  217. if (err < 0)
  218. goto bad;
  219. break;
  220. case CRUSH_BUCKET_TREE:
  221. err = crush_decode_tree_bucket(p, end,
  222. (struct crush_bucket_tree *)b);
  223. if (err < 0)
  224. goto bad;
  225. break;
  226. case CRUSH_BUCKET_STRAW:
  227. err = crush_decode_straw_bucket(p, end,
  228. (struct crush_bucket_straw *)b);
  229. if (err < 0)
  230. goto bad;
  231. break;
  232. }
  233. }
  234. /* rules */
  235. dout("rule vec is %p\n", c->rules);
  236. for (i = 0; i < c->max_rules; i++) {
  237. u32 yes;
  238. struct crush_rule *r;
  239. ceph_decode_32_safe(p, end, yes, bad);
  240. if (!yes) {
  241. dout("crush_decode NO rule %d off %x %p to %p\n",
  242. i, (int)(*p-start), *p, end);
  243. c->rules[i] = NULL;
  244. continue;
  245. }
  246. dout("crush_decode rule %d off %x %p to %p\n",
  247. i, (int)(*p-start), *p, end);
  248. /* len */
  249. ceph_decode_32_safe(p, end, yes, bad);
  250. #if BITS_PER_LONG == 32
  251. if (yes > ULONG_MAX / sizeof(struct crush_rule_step))
  252. goto bad;
  253. #endif
  254. r = c->rules[i] = kmalloc(sizeof(*r) +
  255. yes*sizeof(struct crush_rule_step),
  256. GFP_NOFS);
  257. if (r == NULL)
  258. goto badmem;
  259. dout(" rule %d is at %p\n", i, r);
  260. r->len = yes;
  261. ceph_decode_copy_safe(p, end, &r->mask, 4, bad); /* 4 u8's */
  262. ceph_decode_need(p, end, r->len*3*sizeof(u32), bad);
  263. for (j = 0; j < r->len; j++) {
  264. r->steps[j].op = ceph_decode_32(p);
  265. r->steps[j].arg1 = ceph_decode_32(p);
  266. r->steps[j].arg2 = ceph_decode_32(p);
  267. }
  268. }
  269. /* ignore trailing name maps. */
  270. dout("crush_decode success\n");
  271. return c;
  272. badmem:
  273. err = -ENOMEM;
  274. bad:
  275. dout("crush_decode fail %d\n", err);
  276. crush_destroy(c);
  277. return ERR_PTR(err);
  278. }
  279. /*
  280. * osd map
  281. */
  282. void ceph_osdmap_destroy(struct ceph_osdmap *map)
  283. {
  284. dout("osdmap_destroy %p\n", map);
  285. if (map->crush)
  286. crush_destroy(map->crush);
  287. while (!RB_EMPTY_ROOT(&map->pg_temp))
  288. rb_erase(rb_first(&map->pg_temp), &map->pg_temp);
  289. kfree(map->osd_state);
  290. kfree(map->osd_weight);
  291. kfree(map->pg_pool);
  292. kfree(map->osd_addr);
  293. kfree(map);
  294. }
  295. /*
  296. * adjust max osd value. reallocate arrays.
  297. */
  298. static int osdmap_set_max_osd(struct ceph_osdmap *map, int max)
  299. {
  300. u8 *state;
  301. struct ceph_entity_addr *addr;
  302. u32 *weight;
  303. state = kcalloc(max, sizeof(*state), GFP_NOFS);
  304. addr = kcalloc(max, sizeof(*addr), GFP_NOFS);
  305. weight = kcalloc(max, sizeof(*weight), GFP_NOFS);
  306. if (state == NULL || addr == NULL || weight == NULL) {
  307. kfree(state);
  308. kfree(addr);
  309. kfree(weight);
  310. return -ENOMEM;
  311. }
  312. /* copy old? */
  313. if (map->osd_state) {
  314. memcpy(state, map->osd_state, map->max_osd*sizeof(*state));
  315. memcpy(addr, map->osd_addr, map->max_osd*sizeof(*addr));
  316. memcpy(weight, map->osd_weight, map->max_osd*sizeof(*weight));
  317. kfree(map->osd_state);
  318. kfree(map->osd_addr);
  319. kfree(map->osd_weight);
  320. }
  321. map->osd_state = state;
  322. map->osd_weight = weight;
  323. map->osd_addr = addr;
  324. map->max_osd = max;
  325. return 0;
  326. }
  327. /*
  328. * Insert a new pg_temp mapping
  329. */
  330. static int pgid_cmp(struct ceph_pg l, struct ceph_pg r)
  331. {
  332. u64 a = *(u64 *)&l;
  333. u64 b = *(u64 *)&r;
  334. if (a < b)
  335. return -1;
  336. if (a > b)
  337. return 1;
  338. return 0;
  339. }
  340. static int __insert_pg_mapping(struct ceph_pg_mapping *new,
  341. struct rb_root *root)
  342. {
  343. struct rb_node **p = &root->rb_node;
  344. struct rb_node *parent = NULL;
  345. struct ceph_pg_mapping *pg = NULL;
  346. int c;
  347. while (*p) {
  348. parent = *p;
  349. pg = rb_entry(parent, struct ceph_pg_mapping, node);
  350. c = pgid_cmp(new->pgid, pg->pgid);
  351. if (c < 0)
  352. p = &(*p)->rb_left;
  353. else if (c > 0)
  354. p = &(*p)->rb_right;
  355. else
  356. return -EEXIST;
  357. }
  358. rb_link_node(&new->node, parent, p);
  359. rb_insert_color(&new->node, root);
  360. return 0;
  361. }
  362. /*
  363. * decode a full map.
  364. */
  365. struct ceph_osdmap *osdmap_decode(void **p, void *end)
  366. {
  367. struct ceph_osdmap *map;
  368. u16 version;
  369. u32 len, max, i;
  370. int err = -EINVAL;
  371. void *start = *p;
  372. dout("osdmap_decode %p to %p len %d\n", *p, end, (int)(end - *p));
  373. map = kzalloc(sizeof(*map), GFP_NOFS);
  374. if (map == NULL)
  375. return ERR_PTR(-ENOMEM);
  376. map->pg_temp = RB_ROOT;
  377. ceph_decode_16_safe(p, end, version, bad);
  378. ceph_decode_need(p, end, 2*sizeof(u64)+6*sizeof(u32), bad);
  379. ceph_decode_copy(p, &map->fsid, sizeof(map->fsid));
  380. map->epoch = ceph_decode_32(p);
  381. ceph_decode_copy(p, &map->created, sizeof(map->created));
  382. ceph_decode_copy(p, &map->modified, sizeof(map->modified));
  383. map->num_pools = ceph_decode_32(p);
  384. map->pg_pool = kcalloc(map->num_pools, sizeof(*map->pg_pool),
  385. GFP_NOFS);
  386. if (!map->pg_pool) {
  387. err = -ENOMEM;
  388. goto bad;
  389. }
  390. ceph_decode_32_safe(p, end, max, bad);
  391. while (max--) {
  392. ceph_decode_need(p, end, 4+sizeof(map->pg_pool->v), bad);
  393. i = ceph_decode_32(p);
  394. if (i >= map->num_pools)
  395. goto bad;
  396. ceph_decode_copy(p, &map->pg_pool[i].v,
  397. sizeof(map->pg_pool->v));
  398. calc_pg_masks(&map->pg_pool[i]);
  399. p += le32_to_cpu(map->pg_pool[i].v.num_snaps) * sizeof(u64);
  400. p += le32_to_cpu(map->pg_pool[i].v.num_removed_snap_intervals)
  401. * sizeof(u64) * 2;
  402. }
  403. ceph_decode_32_safe(p, end, map->flags, bad);
  404. max = ceph_decode_32(p);
  405. /* (re)alloc osd arrays */
  406. err = osdmap_set_max_osd(map, max);
  407. if (err < 0)
  408. goto bad;
  409. dout("osdmap_decode max_osd = %d\n", map->max_osd);
  410. /* osds */
  411. err = -EINVAL;
  412. ceph_decode_need(p, end, 3*sizeof(u32) +
  413. map->max_osd*(1 + sizeof(*map->osd_weight) +
  414. sizeof(*map->osd_addr)), bad);
  415. *p += 4; /* skip length field (should match max) */
  416. ceph_decode_copy(p, map->osd_state, map->max_osd);
  417. *p += 4; /* skip length field (should match max) */
  418. for (i = 0; i < map->max_osd; i++)
  419. map->osd_weight[i] = ceph_decode_32(p);
  420. *p += 4; /* skip length field (should match max) */
  421. ceph_decode_copy(p, map->osd_addr, map->max_osd*sizeof(*map->osd_addr));
  422. for (i = 0; i < map->max_osd; i++)
  423. ceph_decode_addr(&map->osd_addr[i]);
  424. /* pg_temp */
  425. ceph_decode_32_safe(p, end, len, bad);
  426. for (i = 0; i < len; i++) {
  427. int n, j;
  428. struct ceph_pg pgid;
  429. struct ceph_pg_mapping *pg;
  430. ceph_decode_need(p, end, sizeof(u32) + sizeof(u64), bad);
  431. ceph_decode_copy(p, &pgid, sizeof(pgid));
  432. n = ceph_decode_32(p);
  433. ceph_decode_need(p, end, n * sizeof(u32), bad);
  434. pg = kmalloc(sizeof(*pg) + n*sizeof(u32), GFP_NOFS);
  435. if (!pg) {
  436. err = -ENOMEM;
  437. goto bad;
  438. }
  439. pg->pgid = pgid;
  440. pg->len = n;
  441. for (j = 0; j < n; j++)
  442. pg->osds[j] = ceph_decode_32(p);
  443. err = __insert_pg_mapping(pg, &map->pg_temp);
  444. if (err)
  445. goto bad;
  446. dout(" added pg_temp %llx len %d\n", *(u64 *)&pgid, len);
  447. }
  448. /* crush */
  449. ceph_decode_32_safe(p, end, len, bad);
  450. dout("osdmap_decode crush len %d from off 0x%x\n", len,
  451. (int)(*p - start));
  452. ceph_decode_need(p, end, len, bad);
  453. map->crush = crush_decode(*p, end);
  454. *p += len;
  455. if (IS_ERR(map->crush)) {
  456. err = PTR_ERR(map->crush);
  457. map->crush = NULL;
  458. goto bad;
  459. }
  460. /* ignore the rest of the map */
  461. *p = end;
  462. dout("osdmap_decode done %p %p\n", *p, end);
  463. return map;
  464. bad:
  465. dout("osdmap_decode fail\n");
  466. ceph_osdmap_destroy(map);
  467. return ERR_PTR(err);
  468. }
  469. /*
  470. * decode and apply an incremental map update.
  471. */
  472. struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end,
  473. struct ceph_osdmap *map,
  474. struct ceph_messenger *msgr)
  475. {
  476. struct ceph_osdmap *newmap = map;
  477. struct crush_map *newcrush = NULL;
  478. struct ceph_fsid fsid;
  479. u32 epoch = 0;
  480. struct ceph_timespec modified;
  481. u32 len, pool;
  482. __s32 new_flags, max;
  483. void *start = *p;
  484. int err = -EINVAL;
  485. u16 version;
  486. struct rb_node *rbp;
  487. ceph_decode_16_safe(p, end, version, bad);
  488. ceph_decode_need(p, end, sizeof(fsid)+sizeof(modified)+2*sizeof(u32),
  489. bad);
  490. ceph_decode_copy(p, &fsid, sizeof(fsid));
  491. epoch = ceph_decode_32(p);
  492. BUG_ON(epoch != map->epoch+1);
  493. ceph_decode_copy(p, &modified, sizeof(modified));
  494. new_flags = ceph_decode_32(p);
  495. /* full map? */
  496. ceph_decode_32_safe(p, end, len, bad);
  497. if (len > 0) {
  498. dout("apply_incremental full map len %d, %p to %p\n",
  499. len, *p, end);
  500. newmap = osdmap_decode(p, min(*p+len, end));
  501. return newmap; /* error or not */
  502. }
  503. /* new crush? */
  504. ceph_decode_32_safe(p, end, len, bad);
  505. if (len > 0) {
  506. dout("apply_incremental new crush map len %d, %p to %p\n",
  507. len, *p, end);
  508. newcrush = crush_decode(*p, min(*p+len, end));
  509. if (IS_ERR(newcrush))
  510. return ERR_PTR(PTR_ERR(newcrush));
  511. }
  512. /* new flags? */
  513. if (new_flags >= 0)
  514. map->flags = new_flags;
  515. ceph_decode_need(p, end, 5*sizeof(u32), bad);
  516. /* new max? */
  517. max = ceph_decode_32(p);
  518. if (max >= 0) {
  519. err = osdmap_set_max_osd(map, max);
  520. if (err < 0)
  521. goto bad;
  522. }
  523. map->epoch++;
  524. map->modified = map->modified;
  525. if (newcrush) {
  526. if (map->crush)
  527. crush_destroy(map->crush);
  528. map->crush = newcrush;
  529. newcrush = NULL;
  530. }
  531. /* new_pool */
  532. ceph_decode_32_safe(p, end, len, bad);
  533. while (len--) {
  534. ceph_decode_32_safe(p, end, pool, bad);
  535. if (pool >= map->num_pools) {
  536. void *pg_pool = kcalloc(pool + 1,
  537. sizeof(*map->pg_pool),
  538. GFP_NOFS);
  539. if (!pg_pool) {
  540. err = -ENOMEM;
  541. goto bad;
  542. }
  543. memcpy(pg_pool, map->pg_pool,
  544. map->num_pools * sizeof(*map->pg_pool));
  545. kfree(map->pg_pool);
  546. map->pg_pool = pg_pool;
  547. map->num_pools = pool+1;
  548. }
  549. ceph_decode_copy(p, &map->pg_pool[pool].v,
  550. sizeof(map->pg_pool->v));
  551. calc_pg_masks(&map->pg_pool[pool]);
  552. }
  553. /* old_pool (ignore) */
  554. ceph_decode_32_safe(p, end, len, bad);
  555. *p += len * sizeof(u32);
  556. /* new_up */
  557. err = -EINVAL;
  558. ceph_decode_32_safe(p, end, len, bad);
  559. while (len--) {
  560. u32 osd;
  561. struct ceph_entity_addr addr;
  562. ceph_decode_32_safe(p, end, osd, bad);
  563. ceph_decode_copy_safe(p, end, &addr, sizeof(addr), bad);
  564. ceph_decode_addr(&addr);
  565. pr_info("osd%d up\n", osd);
  566. BUG_ON(osd >= map->max_osd);
  567. map->osd_state[osd] |= CEPH_OSD_UP;
  568. map->osd_addr[osd] = addr;
  569. }
  570. /* new_down */
  571. ceph_decode_32_safe(p, end, len, bad);
  572. while (len--) {
  573. u32 osd;
  574. ceph_decode_32_safe(p, end, osd, bad);
  575. (*p)++; /* clean flag */
  576. pr_info("osd%d down\n", osd);
  577. if (osd < map->max_osd)
  578. map->osd_state[osd] &= ~CEPH_OSD_UP;
  579. }
  580. /* new_weight */
  581. ceph_decode_32_safe(p, end, len, bad);
  582. while (len--) {
  583. u32 osd, off;
  584. ceph_decode_need(p, end, sizeof(u32)*2, bad);
  585. osd = ceph_decode_32(p);
  586. off = ceph_decode_32(p);
  587. pr_info("osd%d weight 0x%x %s\n", osd, off,
  588. off == CEPH_OSD_IN ? "(in)" :
  589. (off == CEPH_OSD_OUT ? "(out)" : ""));
  590. if (osd < map->max_osd)
  591. map->osd_weight[osd] = off;
  592. }
  593. /* new_pg_temp */
  594. rbp = rb_first(&map->pg_temp);
  595. ceph_decode_32_safe(p, end, len, bad);
  596. while (len--) {
  597. struct ceph_pg_mapping *pg;
  598. int j;
  599. struct ceph_pg pgid;
  600. u32 pglen;
  601. ceph_decode_need(p, end, sizeof(u64) + sizeof(u32), bad);
  602. ceph_decode_copy(p, &pgid, sizeof(pgid));
  603. pglen = ceph_decode_32(p);
  604. /* remove any? */
  605. while (rbp && pgid_cmp(rb_entry(rbp, struct ceph_pg_mapping,
  606. node)->pgid, pgid) <= 0) {
  607. struct rb_node *cur = rbp;
  608. rbp = rb_next(rbp);
  609. dout(" removed pg_temp %llx\n",
  610. *(u64 *)&rb_entry(cur, struct ceph_pg_mapping,
  611. node)->pgid);
  612. rb_erase(cur, &map->pg_temp);
  613. }
  614. if (pglen) {
  615. /* insert */
  616. ceph_decode_need(p, end, pglen*sizeof(u32), bad);
  617. pg = kmalloc(sizeof(*pg) + sizeof(u32)*pglen, GFP_NOFS);
  618. if (!pg) {
  619. err = -ENOMEM;
  620. goto bad;
  621. }
  622. pg->pgid = pgid;
  623. pg->len = pglen;
  624. for (j = 0; j < len; j++)
  625. pg->osds[j] = ceph_decode_32(p);
  626. err = __insert_pg_mapping(pg, &map->pg_temp);
  627. if (err)
  628. goto bad;
  629. dout(" added pg_temp %llx len %d\n", *(u64 *)&pgid,
  630. pglen);
  631. }
  632. }
  633. while (rbp) {
  634. struct rb_node *cur = rbp;
  635. rbp = rb_next(rbp);
  636. dout(" removed pg_temp %llx\n",
  637. *(u64 *)&rb_entry(cur, struct ceph_pg_mapping,
  638. node)->pgid);
  639. rb_erase(cur, &map->pg_temp);
  640. }
  641. /* ignore the rest */
  642. *p = end;
  643. return map;
  644. bad:
  645. pr_err("corrupt inc osdmap epoch %d off %d (%p of %p-%p)\n",
  646. epoch, (int)(*p - start), *p, start, end);
  647. if (newcrush)
  648. crush_destroy(newcrush);
  649. return ERR_PTR(err);
  650. }
  651. /*
  652. * calculate file layout from given offset, length.
  653. * fill in correct oid, logical length, and object extent
  654. * offset, length.
  655. *
  656. * for now, we write only a single su, until we can
  657. * pass a stride back to the caller.
  658. */
  659. void ceph_calc_file_object_mapping(struct ceph_file_layout *layout,
  660. u64 off, u64 *plen,
  661. u64 *ono,
  662. u64 *oxoff, u64 *oxlen)
  663. {
  664. u32 osize = le32_to_cpu(layout->fl_object_size);
  665. u32 su = le32_to_cpu(layout->fl_stripe_unit);
  666. u32 sc = le32_to_cpu(layout->fl_stripe_count);
  667. u32 bl, stripeno, stripepos, objsetno;
  668. u32 su_per_object;
  669. u64 t, su_offset;
  670. dout("mapping %llu~%llu osize %u fl_su %u\n", off, *plen,
  671. osize, su);
  672. su_per_object = osize / su;
  673. dout("osize %u / su %u = su_per_object %u\n", osize, su,
  674. su_per_object);
  675. BUG_ON((su & ~PAGE_MASK) != 0);
  676. /* bl = *off / su; */
  677. t = off;
  678. do_div(t, su);
  679. bl = t;
  680. dout("off %llu / su %u = bl %u\n", off, su, bl);
  681. stripeno = bl / sc;
  682. stripepos = bl % sc;
  683. objsetno = stripeno / su_per_object;
  684. *ono = objsetno * sc + stripepos;
  685. dout("objset %u * sc %u = ono %u\n", objsetno, sc, (unsigned)*ono);
  686. /* *oxoff = *off % layout->fl_stripe_unit; # offset in su */
  687. t = off;
  688. su_offset = do_div(t, su);
  689. *oxoff = su_offset + (stripeno % su_per_object) * su;
  690. /*
  691. * Calculate the length of the extent being written to the selected
  692. * object. This is the minimum of the full length requested (plen) or
  693. * the remainder of the current stripe being written to.
  694. */
  695. *oxlen = min_t(u64, *plen, su - su_offset);
  696. *plen = *oxlen;
  697. dout(" obj extent %llu~%llu\n", *oxoff, *oxlen);
  698. }
  699. /*
  700. * calculate an object layout (i.e. pgid) from an oid,
  701. * file_layout, and osdmap
  702. */
  703. int ceph_calc_object_layout(struct ceph_object_layout *ol,
  704. const char *oid,
  705. struct ceph_file_layout *fl,
  706. struct ceph_osdmap *osdmap)
  707. {
  708. unsigned num, num_mask;
  709. struct ceph_pg pgid;
  710. s32 preferred = (s32)le32_to_cpu(fl->fl_pg_preferred);
  711. int poolid = le32_to_cpu(fl->fl_pg_pool);
  712. struct ceph_pg_pool_info *pool;
  713. unsigned ps;
  714. if (poolid >= osdmap->num_pools)
  715. return -EIO;
  716. pool = &osdmap->pg_pool[poolid];
  717. ps = ceph_str_hash(pool->v.object_hash, oid, strlen(oid));
  718. if (preferred >= 0) {
  719. ps += preferred;
  720. num = le32_to_cpu(pool->v.lpg_num);
  721. num_mask = pool->lpg_num_mask;
  722. } else {
  723. num = le32_to_cpu(pool->v.pg_num);
  724. num_mask = pool->pg_num_mask;
  725. }
  726. pgid.ps = cpu_to_le16(ps);
  727. pgid.preferred = cpu_to_le16(preferred);
  728. pgid.pool = fl->fl_pg_pool;
  729. if (preferred >= 0)
  730. dout("calc_object_layout '%s' pgid %d.%xp%d\n", oid, poolid, ps,
  731. (int)preferred);
  732. else
  733. dout("calc_object_layout '%s' pgid %d.%x\n", oid, poolid, ps);
  734. ol->ol_pgid = pgid;
  735. ol->ol_stripe_unit = fl->fl_object_stripe_unit;
  736. return 0;
  737. }
  738. /*
  739. * Calculate raw osd vector for the given pgid. Return pointer to osd
  740. * array, or NULL on failure.
  741. */
  742. static int *calc_pg_raw(struct ceph_osdmap *osdmap, struct ceph_pg pgid,
  743. int *osds, int *num)
  744. {
  745. struct rb_node *n = osdmap->pg_temp.rb_node;
  746. struct ceph_pg_mapping *pg;
  747. struct ceph_pg_pool_info *pool;
  748. int ruleno;
  749. unsigned poolid, ps, pps;
  750. int preferred;
  751. int c;
  752. /* pg_temp? */
  753. while (n) {
  754. pg = rb_entry(n, struct ceph_pg_mapping, node);
  755. c = pgid_cmp(pgid, pg->pgid);
  756. if (c < 0)
  757. n = n->rb_left;
  758. else if (c > 0)
  759. n = n->rb_right;
  760. else {
  761. *num = pg->len;
  762. return pg->osds;
  763. }
  764. }
  765. /* crush */
  766. poolid = le32_to_cpu(pgid.pool);
  767. ps = le16_to_cpu(pgid.ps);
  768. preferred = (s16)le16_to_cpu(pgid.preferred);
  769. /* don't forcefeed bad device ids to crush */
  770. if (preferred >= osdmap->max_osd ||
  771. preferred >= osdmap->crush->max_devices)
  772. preferred = -1;
  773. if (poolid >= osdmap->num_pools)
  774. return NULL;
  775. pool = &osdmap->pg_pool[poolid];
  776. ruleno = crush_find_rule(osdmap->crush, pool->v.crush_ruleset,
  777. pool->v.type, pool->v.size);
  778. if (ruleno < 0) {
  779. pr_err("no crush rule pool %d type %d size %d\n",
  780. poolid, pool->v.type, pool->v.size);
  781. return NULL;
  782. }
  783. if (preferred >= 0)
  784. pps = ceph_stable_mod(ps,
  785. le32_to_cpu(pool->v.lpgp_num),
  786. pool->lpgp_num_mask);
  787. else
  788. pps = ceph_stable_mod(ps,
  789. le32_to_cpu(pool->v.pgp_num),
  790. pool->pgp_num_mask);
  791. pps += poolid;
  792. *num = crush_do_rule(osdmap->crush, ruleno, pps, osds,
  793. min_t(int, pool->v.size, *num),
  794. preferred, osdmap->osd_weight);
  795. return osds;
  796. }
  797. /*
  798. * Return primary osd for given pgid, or -1 if none.
  799. */
  800. int ceph_calc_pg_primary(struct ceph_osdmap *osdmap, struct ceph_pg pgid)
  801. {
  802. int rawosds[10], *osds;
  803. int i, num = ARRAY_SIZE(rawosds);
  804. osds = calc_pg_raw(osdmap, pgid, rawosds, &num);
  805. if (!osds)
  806. return -1;
  807. /* primary is first up osd */
  808. for (i = 0; i < num; i++)
  809. if (ceph_osd_is_up(osdmap, osds[i])) {
  810. return osds[i];
  811. break;
  812. }
  813. return -1;
  814. }