osdmap.c 25 KB

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