osdmap.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  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 pgid_cmp(struct ceph_pg l, struct ceph_pg r)
  330. {
  331. u64 a = *(u64 *)&l;
  332. u64 b = *(u64 *)&r;
  333. if (a < b)
  334. return -1;
  335. if (a > b)
  336. return 1;
  337. return 0;
  338. }
  339. static int __insert_pg_mapping(struct ceph_pg_mapping *new,
  340. struct rb_root *root)
  341. {
  342. struct rb_node **p = &root->rb_node;
  343. struct rb_node *parent = NULL;
  344. struct ceph_pg_mapping *pg = NULL;
  345. int c;
  346. while (*p) {
  347. parent = *p;
  348. pg = rb_entry(parent, struct ceph_pg_mapping, node);
  349. c = pgid_cmp(new->pgid, pg->pgid);
  350. if (c < 0)
  351. p = &(*p)->rb_left;
  352. else if (c > 0)
  353. p = &(*p)->rb_right;
  354. else
  355. return -EEXIST;
  356. }
  357. rb_link_node(&new->node, parent, p);
  358. rb_insert_color(&new->node, root);
  359. return 0;
  360. }
  361. /*
  362. * decode a full map.
  363. */
  364. struct ceph_osdmap *osdmap_decode(void **p, void *end)
  365. {
  366. struct ceph_osdmap *map;
  367. u16 version;
  368. u32 len, max, i;
  369. int err = -EINVAL;
  370. void *start = *p;
  371. dout("osdmap_decode %p to %p len %d\n", *p, end, (int)(end - *p));
  372. map = kzalloc(sizeof(*map), GFP_NOFS);
  373. if (map == NULL)
  374. return ERR_PTR(-ENOMEM);
  375. map->pg_temp = RB_ROOT;
  376. ceph_decode_16_safe(p, end, version, bad);
  377. ceph_decode_need(p, end, 2*sizeof(u64)+6*sizeof(u32), bad);
  378. ceph_decode_copy(p, &map->fsid, sizeof(map->fsid));
  379. map->epoch = ceph_decode_32(p);
  380. ceph_decode_copy(p, &map->created, sizeof(map->created));
  381. ceph_decode_copy(p, &map->modified, sizeof(map->modified));
  382. map->num_pools = ceph_decode_32(p);
  383. map->pg_pool = kcalloc(map->num_pools, sizeof(*map->pg_pool),
  384. GFP_NOFS);
  385. if (!map->pg_pool) {
  386. err = -ENOMEM;
  387. goto bad;
  388. }
  389. ceph_decode_32_safe(p, end, max, bad);
  390. while (max--) {
  391. ceph_decode_need(p, end, 4+sizeof(map->pg_pool->v), bad);
  392. i = ceph_decode_32(p);
  393. if (i >= map->num_pools)
  394. goto bad;
  395. ceph_decode_copy(p, &map->pg_pool[i].v,
  396. sizeof(map->pg_pool->v));
  397. calc_pg_masks(&map->pg_pool[i]);
  398. p += le32_to_cpu(map->pg_pool[i].v.num_snaps) * sizeof(u64);
  399. p += le32_to_cpu(map->pg_pool[i].v.num_removed_snap_intervals)
  400. * sizeof(u64) * 2;
  401. }
  402. ceph_decode_32_safe(p, end, map->flags, bad);
  403. max = ceph_decode_32(p);
  404. /* (re)alloc osd arrays */
  405. err = osdmap_set_max_osd(map, max);
  406. if (err < 0)
  407. goto bad;
  408. dout("osdmap_decode max_osd = %d\n", map->max_osd);
  409. /* osds */
  410. err = -EINVAL;
  411. ceph_decode_need(p, end, 3*sizeof(u32) +
  412. map->max_osd*(1 + sizeof(*map->osd_weight) +
  413. sizeof(*map->osd_addr)), bad);
  414. *p += 4; /* skip length field (should match max) */
  415. ceph_decode_copy(p, map->osd_state, map->max_osd);
  416. *p += 4; /* skip length field (should match max) */
  417. for (i = 0; i < map->max_osd; i++)
  418. map->osd_weight[i] = ceph_decode_32(p);
  419. *p += 4; /* skip length field (should match max) */
  420. ceph_decode_copy(p, map->osd_addr, map->max_osd*sizeof(*map->osd_addr));
  421. for (i = 0; i < map->max_osd; i++)
  422. ceph_decode_addr(&map->osd_addr[i]);
  423. /* pg_temp */
  424. ceph_decode_32_safe(p, end, len, bad);
  425. for (i = 0; i < len; i++) {
  426. int n, j;
  427. struct ceph_pg pgid;
  428. struct ceph_pg_mapping *pg;
  429. ceph_decode_need(p, end, sizeof(u32) + sizeof(u64), bad);
  430. ceph_decode_copy(p, &pgid, sizeof(pgid));
  431. n = ceph_decode_32(p);
  432. ceph_decode_need(p, end, n * sizeof(u32), bad);
  433. pg = kmalloc(sizeof(*pg) + n*sizeof(u32), GFP_NOFS);
  434. if (!pg) {
  435. err = -ENOMEM;
  436. goto bad;
  437. }
  438. pg->pgid = pgid;
  439. pg->len = n;
  440. for (j = 0; j < n; j++)
  441. pg->osds[j] = ceph_decode_32(p);
  442. err = __insert_pg_mapping(pg, &map->pg_temp);
  443. if (err)
  444. goto bad;
  445. dout(" added pg_temp %llx len %d\n", *(u64 *)&pgid, len);
  446. }
  447. /* crush */
  448. ceph_decode_32_safe(p, end, len, bad);
  449. dout("osdmap_decode crush len %d from off 0x%x\n", len,
  450. (int)(*p - start));
  451. ceph_decode_need(p, end, len, bad);
  452. map->crush = crush_decode(*p, end);
  453. *p += len;
  454. if (IS_ERR(map->crush)) {
  455. err = PTR_ERR(map->crush);
  456. map->crush = NULL;
  457. goto bad;
  458. }
  459. /* ignore the rest of the map */
  460. *p = end;
  461. dout("osdmap_decode done %p %p\n", *p, end);
  462. return map;
  463. bad:
  464. dout("osdmap_decode fail\n");
  465. ceph_osdmap_destroy(map);
  466. return ERR_PTR(err);
  467. }
  468. /*
  469. * decode and apply an incremental map update.
  470. */
  471. struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end,
  472. struct ceph_osdmap *map,
  473. struct ceph_messenger *msgr)
  474. {
  475. struct ceph_osdmap *newmap = map;
  476. struct crush_map *newcrush = NULL;
  477. struct ceph_fsid fsid;
  478. u32 epoch = 0;
  479. struct ceph_timespec modified;
  480. u32 len, pool;
  481. __s32 new_flags, max;
  482. void *start = *p;
  483. int err = -EINVAL;
  484. u16 version;
  485. struct rb_node *rbp;
  486. ceph_decode_16_safe(p, end, version, bad);
  487. ceph_decode_need(p, end, sizeof(fsid)+sizeof(modified)+2*sizeof(u32),
  488. bad);
  489. ceph_decode_copy(p, &fsid, sizeof(fsid));
  490. epoch = ceph_decode_32(p);
  491. BUG_ON(epoch != map->epoch+1);
  492. ceph_decode_copy(p, &modified, sizeof(modified));
  493. new_flags = ceph_decode_32(p);
  494. /* full map? */
  495. ceph_decode_32_safe(p, end, len, bad);
  496. if (len > 0) {
  497. dout("apply_incremental full map len %d, %p to %p\n",
  498. len, *p, end);
  499. newmap = osdmap_decode(p, min(*p+len, end));
  500. return newmap; /* error or not */
  501. }
  502. /* new crush? */
  503. ceph_decode_32_safe(p, end, len, bad);
  504. if (len > 0) {
  505. dout("apply_incremental new crush map len %d, %p to %p\n",
  506. len, *p, end);
  507. newcrush = crush_decode(*p, min(*p+len, end));
  508. if (IS_ERR(newcrush))
  509. return ERR_PTR(PTR_ERR(newcrush));
  510. }
  511. /* new flags? */
  512. if (new_flags >= 0)
  513. map->flags = new_flags;
  514. ceph_decode_need(p, end, 5*sizeof(u32), bad);
  515. /* new max? */
  516. max = ceph_decode_32(p);
  517. if (max >= 0) {
  518. err = osdmap_set_max_osd(map, max);
  519. if (err < 0)
  520. goto bad;
  521. }
  522. map->epoch++;
  523. map->modified = map->modified;
  524. if (newcrush) {
  525. if (map->crush)
  526. crush_destroy(map->crush);
  527. map->crush = newcrush;
  528. newcrush = NULL;
  529. }
  530. /* new_pool */
  531. ceph_decode_32_safe(p, end, len, bad);
  532. while (len--) {
  533. ceph_decode_32_safe(p, end, pool, bad);
  534. if (pool >= map->num_pools) {
  535. void *pg_pool = kcalloc(pool + 1,
  536. sizeof(*map->pg_pool),
  537. GFP_NOFS);
  538. if (!pg_pool) {
  539. err = -ENOMEM;
  540. goto bad;
  541. }
  542. memcpy(pg_pool, map->pg_pool,
  543. map->num_pools * sizeof(*map->pg_pool));
  544. kfree(map->pg_pool);
  545. map->pg_pool = pg_pool;
  546. map->num_pools = pool+1;
  547. }
  548. ceph_decode_copy(p, &map->pg_pool[pool].v,
  549. sizeof(map->pg_pool->v));
  550. calc_pg_masks(&map->pg_pool[pool]);
  551. }
  552. /* old_pool (ignore) */
  553. ceph_decode_32_safe(p, end, len, bad);
  554. *p += len * sizeof(u32);
  555. /* new_up */
  556. err = -EINVAL;
  557. ceph_decode_32_safe(p, end, len, bad);
  558. while (len--) {
  559. u32 osd;
  560. struct ceph_entity_addr addr;
  561. ceph_decode_32_safe(p, end, osd, bad);
  562. ceph_decode_copy_safe(p, end, &addr, sizeof(addr), bad);
  563. ceph_decode_addr(&addr);
  564. pr_info("osd%d up\n", osd);
  565. BUG_ON(osd >= map->max_osd);
  566. map->osd_state[osd] |= CEPH_OSD_UP;
  567. map->osd_addr[osd] = addr;
  568. }
  569. /* new_down */
  570. ceph_decode_32_safe(p, end, len, bad);
  571. while (len--) {
  572. u32 osd;
  573. ceph_decode_32_safe(p, end, osd, bad);
  574. (*p)++; /* clean flag */
  575. pr_info("osd%d down\n", osd);
  576. if (osd < map->max_osd)
  577. map->osd_state[osd] &= ~CEPH_OSD_UP;
  578. }
  579. /* new_weight */
  580. ceph_decode_32_safe(p, end, len, bad);
  581. while (len--) {
  582. u32 osd, off;
  583. ceph_decode_need(p, end, sizeof(u32)*2, bad);
  584. osd = ceph_decode_32(p);
  585. off = ceph_decode_32(p);
  586. pr_info("osd%d weight 0x%x %s\n", osd, off,
  587. off == CEPH_OSD_IN ? "(in)" :
  588. (off == CEPH_OSD_OUT ? "(out)" : ""));
  589. if (osd < map->max_osd)
  590. map->osd_weight[osd] = off;
  591. }
  592. /* new_pg_temp */
  593. rbp = rb_first(&map->pg_temp);
  594. ceph_decode_32_safe(p, end, len, bad);
  595. while (len--) {
  596. struct ceph_pg_mapping *pg;
  597. int j;
  598. struct ceph_pg pgid;
  599. u32 pglen;
  600. ceph_decode_need(p, end, sizeof(u64) + sizeof(u32), bad);
  601. ceph_decode_copy(p, &pgid, sizeof(pgid));
  602. pglen = ceph_decode_32(p);
  603. /* remove any? */
  604. while (rbp && pgid_cmp(rb_entry(rbp, struct ceph_pg_mapping,
  605. node)->pgid, pgid) <= 0) {
  606. struct rb_node *cur = rbp;
  607. rbp = rb_next(rbp);
  608. dout(" removed pg_temp %llx\n",
  609. *(u64 *)&rb_entry(cur, struct ceph_pg_mapping,
  610. node)->pgid);
  611. rb_erase(cur, &map->pg_temp);
  612. }
  613. if (pglen) {
  614. /* insert */
  615. ceph_decode_need(p, end, pglen*sizeof(u32), bad);
  616. pg = kmalloc(sizeof(*pg) + sizeof(u32)*pglen, GFP_NOFS);
  617. if (!pg) {
  618. err = -ENOMEM;
  619. goto bad;
  620. }
  621. pg->pgid = pgid;
  622. pg->len = pglen;
  623. for (j = 0; j < len; j++)
  624. pg->osds[j] = ceph_decode_32(p);
  625. err = __insert_pg_mapping(pg, &map->pg_temp);
  626. if (err)
  627. goto bad;
  628. dout(" added pg_temp %llx len %d\n", *(u64 *)&pgid,
  629. pglen);
  630. }
  631. }
  632. while (rbp) {
  633. struct rb_node *cur = rbp;
  634. rbp = rb_next(rbp);
  635. dout(" removed pg_temp %llx\n",
  636. *(u64 *)&rb_entry(cur, struct ceph_pg_mapping,
  637. node)->pgid);
  638. rb_erase(cur, &map->pg_temp);
  639. }
  640. /* ignore the rest */
  641. *p = end;
  642. return map;
  643. bad:
  644. pr_err("corrupt inc osdmap epoch %d off %d (%p of %p-%p)\n",
  645. epoch, (int)(*p - start), *p, start, end);
  646. if (newcrush)
  647. crush_destroy(newcrush);
  648. return ERR_PTR(err);
  649. }
  650. /*
  651. * calculate file layout from given offset, length.
  652. * fill in correct oid, logical length, and object extent
  653. * offset, length.
  654. *
  655. * for now, we write only a single su, until we can
  656. * pass a stride back to the caller.
  657. */
  658. void ceph_calc_file_object_mapping(struct ceph_file_layout *layout,
  659. u64 off, u64 *plen,
  660. u64 *ono,
  661. u64 *oxoff, u64 *oxlen)
  662. {
  663. u32 osize = le32_to_cpu(layout->fl_object_size);
  664. u32 su = le32_to_cpu(layout->fl_stripe_unit);
  665. u32 sc = le32_to_cpu(layout->fl_stripe_count);
  666. u32 bl, stripeno, stripepos, objsetno;
  667. u32 su_per_object;
  668. u64 t, su_offset;
  669. dout("mapping %llu~%llu osize %u fl_su %u\n", off, *plen,
  670. osize, su);
  671. su_per_object = osize / su;
  672. dout("osize %u / su %u = su_per_object %u\n", osize, su,
  673. su_per_object);
  674. BUG_ON((su & ~PAGE_MASK) != 0);
  675. /* bl = *off / su; */
  676. t = off;
  677. do_div(t, su);
  678. bl = t;
  679. dout("off %llu / su %u = bl %u\n", off, su, bl);
  680. stripeno = bl / sc;
  681. stripepos = bl % sc;
  682. objsetno = stripeno / su_per_object;
  683. *ono = objsetno * sc + stripepos;
  684. dout("objset %u * sc %u = ono %u\n", objsetno, sc, (unsigned)*ono);
  685. /* *oxoff = *off % layout->fl_stripe_unit; # offset in su */
  686. t = off;
  687. su_offset = do_div(t, su);
  688. *oxoff = su_offset + (stripeno % su_per_object) * su;
  689. /*
  690. * Calculate the length of the extent being written to the selected
  691. * object. This is the minimum of the full length requested (plen) or
  692. * the remainder of the current stripe being written to.
  693. */
  694. *oxlen = min_t(u64, *plen, su - su_offset);
  695. *plen = *oxlen;
  696. dout(" obj extent %llu~%llu\n", *oxoff, *oxlen);
  697. }
  698. /*
  699. * calculate an object layout (i.e. pgid) from an oid,
  700. * file_layout, and osdmap
  701. */
  702. int ceph_calc_object_layout(struct ceph_object_layout *ol,
  703. const char *oid,
  704. struct ceph_file_layout *fl,
  705. struct ceph_osdmap *osdmap)
  706. {
  707. unsigned num, num_mask;
  708. struct ceph_pg pgid;
  709. s32 preferred = (s32)le32_to_cpu(fl->fl_pg_preferred);
  710. int poolid = le32_to_cpu(fl->fl_pg_pool);
  711. struct ceph_pg_pool_info *pool;
  712. unsigned ps;
  713. if (poolid >= osdmap->num_pools)
  714. return -EIO;
  715. pool = &osdmap->pg_pool[poolid];
  716. ps = ceph_full_name_hash(oid, strlen(oid));
  717. if (preferred >= 0) {
  718. ps += preferred;
  719. num = le32_to_cpu(pool->v.lpg_num);
  720. num_mask = pool->lpg_num_mask;
  721. } else {
  722. num = le32_to_cpu(pool->v.pg_num);
  723. num_mask = pool->pg_num_mask;
  724. }
  725. pgid.ps = cpu_to_le16(ps);
  726. pgid.preferred = cpu_to_le16(preferred);
  727. pgid.pool = fl->fl_pg_pool;
  728. if (preferred >= 0)
  729. dout("calc_object_layout '%s' pgid %d.%xp%d\n", oid, poolid, ps,
  730. (int)preferred);
  731. else
  732. dout("calc_object_layout '%s' pgid %d.%x\n", oid, poolid, ps);
  733. ol->ol_pgid = pgid;
  734. ol->ol_stripe_unit = fl->fl_object_stripe_unit;
  735. return 0;
  736. }
  737. /*
  738. * Calculate raw osd vector for the given pgid. Return pointer to osd
  739. * array, or NULL on failure.
  740. */
  741. static int *calc_pg_raw(struct ceph_osdmap *osdmap, struct ceph_pg pgid,
  742. int *osds, int *num)
  743. {
  744. struct rb_node *n = osdmap->pg_temp.rb_node;
  745. struct ceph_pg_mapping *pg;
  746. struct ceph_pg_pool_info *pool;
  747. int ruleno;
  748. unsigned poolid, ps, pps;
  749. int preferred;
  750. int c;
  751. /* pg_temp? */
  752. while (n) {
  753. pg = rb_entry(n, struct ceph_pg_mapping, node);
  754. c = pgid_cmp(pgid, pg->pgid);
  755. if (c < 0)
  756. n = n->rb_left;
  757. else if (c > 0)
  758. n = n->rb_right;
  759. else {
  760. *num = pg->len;
  761. return pg->osds;
  762. }
  763. }
  764. /* crush */
  765. poolid = le32_to_cpu(pgid.pool);
  766. ps = le16_to_cpu(pgid.ps);
  767. preferred = (s16)le16_to_cpu(pgid.preferred);
  768. if (poolid >= osdmap->num_pools)
  769. return NULL;
  770. pool = &osdmap->pg_pool[poolid];
  771. ruleno = crush_find_rule(osdmap->crush, pool->v.crush_ruleset,
  772. pool->v.type, pool->v.size);
  773. if (ruleno < 0) {
  774. pr_err("no crush rule pool %d type %d size %d\n",
  775. poolid, pool->v.type, pool->v.size);
  776. return NULL;
  777. }
  778. if (preferred >= 0)
  779. pps = ceph_stable_mod(ps,
  780. le32_to_cpu(pool->v.lpgp_num),
  781. pool->lpgp_num_mask);
  782. else
  783. pps = ceph_stable_mod(ps,
  784. le32_to_cpu(pool->v.pgp_num),
  785. pool->pgp_num_mask);
  786. pps += poolid;
  787. *num = crush_do_rule(osdmap->crush, ruleno, pps, osds,
  788. min_t(int, pool->v.size, *num),
  789. preferred, osdmap->osd_weight);
  790. return osds;
  791. }
  792. /*
  793. * Return primary osd for given pgid, or -1 if none.
  794. */
  795. int ceph_calc_pg_primary(struct ceph_osdmap *osdmap, struct ceph_pg pgid)
  796. {
  797. int rawosds[10], *osds;
  798. int i, num = ARRAY_SIZE(rawosds);
  799. osds = calc_pg_raw(osdmap, pgid, rawosds, &num);
  800. if (!osds)
  801. return -1;
  802. /* primary is first up osd */
  803. for (i = 0; i < num; i++)
  804. if (ceph_osd_is_up(osdmap, osds[i])) {
  805. return osds[i];
  806. break;
  807. }
  808. return -1;
  809. }