osdmap.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  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. static int __decode_pool(void **p, void *end, struct ceph_pg_pool_info *pi)
  380. {
  381. unsigned n, m;
  382. ceph_decode_copy(p, &pi->v, sizeof(pi->v));
  383. calc_pg_masks(pi);
  384. /* num_snaps * snap_info_t */
  385. n = le32_to_cpu(pi->v.num_snaps);
  386. while (n--) {
  387. ceph_decode_need(p, end, sizeof(u64) + 1 + sizeof(u64) +
  388. sizeof(struct ceph_timespec), bad);
  389. *p += sizeof(u64) + /* key */
  390. 1 + sizeof(u64) + /* u8, snapid */
  391. sizeof(struct ceph_timespec);
  392. m = ceph_decode_32(p); /* snap name */
  393. *p += m;
  394. }
  395. *p += le32_to_cpu(pi->v.num_removed_snap_intervals) * sizeof(u64) * 2;
  396. return 0;
  397. bad:
  398. return -EINVAL;
  399. }
  400. static int __decode_pool_names(void **p, void *end, struct ceph_osdmap *map)
  401. {
  402. struct ceph_pg_pool_info *pi;
  403. u32 num, len, pool;
  404. ceph_decode_32_safe(p, end, num, bad);
  405. dout(" %d pool names\n", num);
  406. while (num--) {
  407. ceph_decode_32_safe(p, end, pool, bad);
  408. ceph_decode_32_safe(p, end, len, bad);
  409. dout(" pool %d len %d\n", pool, len);
  410. pi = __lookup_pg_pool(&map->pg_pools, pool);
  411. if (pi) {
  412. kfree(pi->name);
  413. pi->name = kmalloc(len + 1, GFP_NOFS);
  414. if (pi->name) {
  415. memcpy(pi->name, *p, len);
  416. pi->name[len] = '\0';
  417. dout(" name is %s\n", pi->name);
  418. }
  419. }
  420. *p += len;
  421. }
  422. return 0;
  423. bad:
  424. return -EINVAL;
  425. }
  426. /*
  427. * osd map
  428. */
  429. void ceph_osdmap_destroy(struct ceph_osdmap *map)
  430. {
  431. dout("osdmap_destroy %p\n", map);
  432. if (map->crush)
  433. crush_destroy(map->crush);
  434. while (!RB_EMPTY_ROOT(&map->pg_temp)) {
  435. struct ceph_pg_mapping *pg =
  436. rb_entry(rb_first(&map->pg_temp),
  437. struct ceph_pg_mapping, node);
  438. rb_erase(&pg->node, &map->pg_temp);
  439. kfree(pg);
  440. }
  441. while (!RB_EMPTY_ROOT(&map->pg_pools)) {
  442. struct ceph_pg_pool_info *pi =
  443. rb_entry(rb_first(&map->pg_pools),
  444. struct ceph_pg_pool_info, node);
  445. __remove_pg_pool(&map->pg_pools, pi);
  446. }
  447. kfree(map->osd_state);
  448. kfree(map->osd_weight);
  449. kfree(map->osd_addr);
  450. kfree(map);
  451. }
  452. /*
  453. * adjust max osd value. reallocate arrays.
  454. */
  455. static int osdmap_set_max_osd(struct ceph_osdmap *map, int max)
  456. {
  457. u8 *state;
  458. struct ceph_entity_addr *addr;
  459. u32 *weight;
  460. state = kcalloc(max, sizeof(*state), GFP_NOFS);
  461. addr = kcalloc(max, sizeof(*addr), GFP_NOFS);
  462. weight = kcalloc(max, sizeof(*weight), GFP_NOFS);
  463. if (state == NULL || addr == NULL || weight == NULL) {
  464. kfree(state);
  465. kfree(addr);
  466. kfree(weight);
  467. return -ENOMEM;
  468. }
  469. /* copy old? */
  470. if (map->osd_state) {
  471. memcpy(state, map->osd_state, map->max_osd*sizeof(*state));
  472. memcpy(addr, map->osd_addr, map->max_osd*sizeof(*addr));
  473. memcpy(weight, map->osd_weight, map->max_osd*sizeof(*weight));
  474. kfree(map->osd_state);
  475. kfree(map->osd_addr);
  476. kfree(map->osd_weight);
  477. }
  478. map->osd_state = state;
  479. map->osd_weight = weight;
  480. map->osd_addr = addr;
  481. map->max_osd = max;
  482. return 0;
  483. }
  484. /*
  485. * decode a full map.
  486. */
  487. struct ceph_osdmap *osdmap_decode(void **p, void *end)
  488. {
  489. struct ceph_osdmap *map;
  490. u16 version;
  491. u32 len, max, i;
  492. u8 ev;
  493. int err = -EINVAL;
  494. void *start = *p;
  495. struct ceph_pg_pool_info *pi;
  496. dout("osdmap_decode %p to %p len %d\n", *p, end, (int)(end - *p));
  497. map = kzalloc(sizeof(*map), GFP_NOFS);
  498. if (map == NULL)
  499. return ERR_PTR(-ENOMEM);
  500. map->pg_temp = RB_ROOT;
  501. ceph_decode_16_safe(p, end, version, bad);
  502. if (version > CEPH_OSDMAP_VERSION) {
  503. pr_warning("got unknown v %d > %d of osdmap\n", version,
  504. CEPH_OSDMAP_VERSION);
  505. goto bad;
  506. }
  507. ceph_decode_need(p, end, 2*sizeof(u64)+6*sizeof(u32), bad);
  508. ceph_decode_copy(p, &map->fsid, sizeof(map->fsid));
  509. map->epoch = ceph_decode_32(p);
  510. ceph_decode_copy(p, &map->created, sizeof(map->created));
  511. ceph_decode_copy(p, &map->modified, sizeof(map->modified));
  512. ceph_decode_32_safe(p, end, max, bad);
  513. while (max--) {
  514. ceph_decode_need(p, end, 4 + 1 + sizeof(pi->v), bad);
  515. pi = kzalloc(sizeof(*pi), GFP_NOFS);
  516. if (!pi)
  517. goto bad;
  518. pi->id = ceph_decode_32(p);
  519. ev = ceph_decode_8(p); /* encoding version */
  520. if (ev > CEPH_PG_POOL_VERSION) {
  521. pr_warning("got unknown v %d > %d of ceph_pg_pool\n",
  522. ev, CEPH_PG_POOL_VERSION);
  523. kfree(pi);
  524. goto bad;
  525. }
  526. err = __decode_pool(p, end, pi);
  527. if (err < 0)
  528. goto bad;
  529. __insert_pg_pool(&map->pg_pools, pi);
  530. }
  531. if (version >= 5 && __decode_pool_names(p, end, map) < 0)
  532. goto bad;
  533. ceph_decode_32_safe(p, end, map->pool_max, bad);
  534. ceph_decode_32_safe(p, end, map->flags, bad);
  535. max = ceph_decode_32(p);
  536. /* (re)alloc osd arrays */
  537. err = osdmap_set_max_osd(map, max);
  538. if (err < 0)
  539. goto bad;
  540. dout("osdmap_decode max_osd = %d\n", map->max_osd);
  541. /* osds */
  542. err = -EINVAL;
  543. ceph_decode_need(p, end, 3*sizeof(u32) +
  544. map->max_osd*(1 + sizeof(*map->osd_weight) +
  545. sizeof(*map->osd_addr)), bad);
  546. *p += 4; /* skip length field (should match max) */
  547. ceph_decode_copy(p, map->osd_state, map->max_osd);
  548. *p += 4; /* skip length field (should match max) */
  549. for (i = 0; i < map->max_osd; i++)
  550. map->osd_weight[i] = ceph_decode_32(p);
  551. *p += 4; /* skip length field (should match max) */
  552. ceph_decode_copy(p, map->osd_addr, map->max_osd*sizeof(*map->osd_addr));
  553. for (i = 0; i < map->max_osd; i++)
  554. ceph_decode_addr(&map->osd_addr[i]);
  555. /* pg_temp */
  556. ceph_decode_32_safe(p, end, len, bad);
  557. for (i = 0; i < len; i++) {
  558. int n, j;
  559. struct ceph_pg pgid;
  560. struct ceph_pg_mapping *pg;
  561. ceph_decode_need(p, end, sizeof(u32) + sizeof(u64), bad);
  562. ceph_decode_copy(p, &pgid, sizeof(pgid));
  563. n = ceph_decode_32(p);
  564. ceph_decode_need(p, end, n * sizeof(u32), bad);
  565. err = -ENOMEM;
  566. pg = kmalloc(sizeof(*pg) + n*sizeof(u32), GFP_NOFS);
  567. if (!pg)
  568. goto bad;
  569. pg->pgid = pgid;
  570. pg->len = n;
  571. for (j = 0; j < n; j++)
  572. pg->osds[j] = ceph_decode_32(p);
  573. err = __insert_pg_mapping(pg, &map->pg_temp);
  574. if (err)
  575. goto bad;
  576. dout(" added pg_temp %llx len %d\n", *(u64 *)&pgid, len);
  577. }
  578. /* crush */
  579. ceph_decode_32_safe(p, end, len, bad);
  580. dout("osdmap_decode crush len %d from off 0x%x\n", len,
  581. (int)(*p - start));
  582. ceph_decode_need(p, end, len, bad);
  583. map->crush = crush_decode(*p, end);
  584. *p += len;
  585. if (IS_ERR(map->crush)) {
  586. err = PTR_ERR(map->crush);
  587. map->crush = NULL;
  588. goto bad;
  589. }
  590. /* ignore the rest of the map */
  591. *p = end;
  592. dout("osdmap_decode done %p %p\n", *p, end);
  593. return map;
  594. bad:
  595. dout("osdmap_decode fail\n");
  596. ceph_osdmap_destroy(map);
  597. return ERR_PTR(err);
  598. }
  599. /*
  600. * decode and apply an incremental map update.
  601. */
  602. struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end,
  603. struct ceph_osdmap *map,
  604. struct ceph_messenger *msgr)
  605. {
  606. struct crush_map *newcrush = NULL;
  607. struct ceph_fsid fsid;
  608. u32 epoch = 0;
  609. struct ceph_timespec modified;
  610. u32 len, pool;
  611. __s32 new_pool_max, new_flags, max;
  612. void *start = *p;
  613. int err = -EINVAL;
  614. u16 version;
  615. struct rb_node *rbp;
  616. ceph_decode_16_safe(p, end, version, bad);
  617. if (version > CEPH_OSDMAP_INC_VERSION) {
  618. pr_warning("got unknown v %d > %d of inc osdmap\n", version,
  619. CEPH_OSDMAP_INC_VERSION);
  620. goto bad;
  621. }
  622. ceph_decode_need(p, end, sizeof(fsid)+sizeof(modified)+2*sizeof(u32),
  623. bad);
  624. ceph_decode_copy(p, &fsid, sizeof(fsid));
  625. epoch = ceph_decode_32(p);
  626. BUG_ON(epoch != map->epoch+1);
  627. ceph_decode_copy(p, &modified, sizeof(modified));
  628. new_pool_max = ceph_decode_32(p);
  629. new_flags = ceph_decode_32(p);
  630. /* full map? */
  631. ceph_decode_32_safe(p, end, len, bad);
  632. if (len > 0) {
  633. dout("apply_incremental full map len %d, %p to %p\n",
  634. len, *p, end);
  635. return osdmap_decode(p, min(*p+len, end));
  636. }
  637. /* new crush? */
  638. ceph_decode_32_safe(p, end, len, bad);
  639. if (len > 0) {
  640. dout("apply_incremental new crush map len %d, %p to %p\n",
  641. len, *p, end);
  642. newcrush = crush_decode(*p, min(*p+len, end));
  643. if (IS_ERR(newcrush))
  644. return ERR_CAST(newcrush);
  645. *p += len;
  646. }
  647. /* new flags? */
  648. if (new_flags >= 0)
  649. map->flags = new_flags;
  650. if (new_pool_max >= 0)
  651. map->pool_max = new_pool_max;
  652. ceph_decode_need(p, end, 5*sizeof(u32), bad);
  653. /* new max? */
  654. max = ceph_decode_32(p);
  655. if (max >= 0) {
  656. err = osdmap_set_max_osd(map, max);
  657. if (err < 0)
  658. goto bad;
  659. }
  660. map->epoch++;
  661. map->modified = map->modified;
  662. if (newcrush) {
  663. if (map->crush)
  664. crush_destroy(map->crush);
  665. map->crush = newcrush;
  666. newcrush = NULL;
  667. }
  668. /* new_pool */
  669. ceph_decode_32_safe(p, end, len, bad);
  670. while (len--) {
  671. __u8 ev;
  672. struct ceph_pg_pool_info *pi;
  673. ceph_decode_32_safe(p, end, pool, bad);
  674. ceph_decode_need(p, end, 1 + sizeof(pi->v), bad);
  675. ev = ceph_decode_8(p); /* encoding version */
  676. if (ev > CEPH_PG_POOL_VERSION) {
  677. pr_warning("got unknown v %d > %d of ceph_pg_pool\n",
  678. ev, CEPH_PG_POOL_VERSION);
  679. goto bad;
  680. }
  681. pi = __lookup_pg_pool(&map->pg_pools, pool);
  682. if (!pi) {
  683. pi = kzalloc(sizeof(*pi), GFP_NOFS);
  684. if (!pi) {
  685. err = -ENOMEM;
  686. goto bad;
  687. }
  688. pi->id = pool;
  689. __insert_pg_pool(&map->pg_pools, pi);
  690. }
  691. err = __decode_pool(p, end, pi);
  692. if (err < 0)
  693. goto bad;
  694. }
  695. if (version >= 5 && __decode_pool_names(p, end, map) < 0)
  696. goto bad;
  697. /* old_pool */
  698. ceph_decode_32_safe(p, end, len, bad);
  699. while (len--) {
  700. struct ceph_pg_pool_info *pi;
  701. ceph_decode_32_safe(p, end, pool, bad);
  702. pi = __lookup_pg_pool(&map->pg_pools, pool);
  703. if (pi)
  704. __remove_pg_pool(&map->pg_pools, pi);
  705. }
  706. /* new_up */
  707. err = -EINVAL;
  708. ceph_decode_32_safe(p, end, len, bad);
  709. while (len--) {
  710. u32 osd;
  711. struct ceph_entity_addr addr;
  712. ceph_decode_32_safe(p, end, osd, bad);
  713. ceph_decode_copy_safe(p, end, &addr, sizeof(addr), bad);
  714. ceph_decode_addr(&addr);
  715. pr_info("osd%d up\n", osd);
  716. BUG_ON(osd >= map->max_osd);
  717. map->osd_state[osd] |= CEPH_OSD_UP;
  718. map->osd_addr[osd] = addr;
  719. }
  720. /* new_down */
  721. ceph_decode_32_safe(p, end, len, bad);
  722. while (len--) {
  723. u32 osd;
  724. ceph_decode_32_safe(p, end, osd, bad);
  725. (*p)++; /* clean flag */
  726. pr_info("osd%d down\n", osd);
  727. if (osd < map->max_osd)
  728. map->osd_state[osd] &= ~CEPH_OSD_UP;
  729. }
  730. /* new_weight */
  731. ceph_decode_32_safe(p, end, len, bad);
  732. while (len--) {
  733. u32 osd, off;
  734. ceph_decode_need(p, end, sizeof(u32)*2, bad);
  735. osd = ceph_decode_32(p);
  736. off = ceph_decode_32(p);
  737. pr_info("osd%d weight 0x%x %s\n", osd, off,
  738. off == CEPH_OSD_IN ? "(in)" :
  739. (off == CEPH_OSD_OUT ? "(out)" : ""));
  740. if (osd < map->max_osd)
  741. map->osd_weight[osd] = off;
  742. }
  743. /* new_pg_temp */
  744. rbp = rb_first(&map->pg_temp);
  745. ceph_decode_32_safe(p, end, len, bad);
  746. while (len--) {
  747. struct ceph_pg_mapping *pg;
  748. int j;
  749. struct ceph_pg pgid;
  750. u32 pglen;
  751. ceph_decode_need(p, end, sizeof(u64) + sizeof(u32), bad);
  752. ceph_decode_copy(p, &pgid, sizeof(pgid));
  753. pglen = ceph_decode_32(p);
  754. /* remove any? */
  755. while (rbp && pgid_cmp(rb_entry(rbp, struct ceph_pg_mapping,
  756. node)->pgid, pgid) <= 0) {
  757. struct ceph_pg_mapping *cur =
  758. rb_entry(rbp, struct ceph_pg_mapping, node);
  759. rbp = rb_next(rbp);
  760. dout(" removed pg_temp %llx\n", *(u64 *)&cur->pgid);
  761. rb_erase(&cur->node, &map->pg_temp);
  762. kfree(cur);
  763. }
  764. if (pglen) {
  765. /* insert */
  766. ceph_decode_need(p, end, pglen*sizeof(u32), bad);
  767. pg = kmalloc(sizeof(*pg) + sizeof(u32)*pglen, GFP_NOFS);
  768. if (!pg) {
  769. err = -ENOMEM;
  770. goto bad;
  771. }
  772. pg->pgid = pgid;
  773. pg->len = pglen;
  774. for (j = 0; j < pglen; j++)
  775. pg->osds[j] = ceph_decode_32(p);
  776. err = __insert_pg_mapping(pg, &map->pg_temp);
  777. if (err) {
  778. kfree(pg);
  779. goto bad;
  780. }
  781. dout(" added pg_temp %llx len %d\n", *(u64 *)&pgid,
  782. pglen);
  783. }
  784. }
  785. while (rbp) {
  786. struct ceph_pg_mapping *cur =
  787. rb_entry(rbp, struct ceph_pg_mapping, node);
  788. rbp = rb_next(rbp);
  789. dout(" removed pg_temp %llx\n", *(u64 *)&cur->pgid);
  790. rb_erase(&cur->node, &map->pg_temp);
  791. kfree(cur);
  792. }
  793. /* ignore the rest */
  794. *p = end;
  795. return map;
  796. bad:
  797. pr_err("corrupt inc osdmap epoch %d off %d (%p of %p-%p)\n",
  798. epoch, (int)(*p - start), *p, start, end);
  799. print_hex_dump(KERN_DEBUG, "osdmap: ",
  800. DUMP_PREFIX_OFFSET, 16, 1,
  801. start, end - start, true);
  802. if (newcrush)
  803. crush_destroy(newcrush);
  804. return ERR_PTR(err);
  805. }
  806. /*
  807. * calculate file layout from given offset, length.
  808. * fill in correct oid, logical length, and object extent
  809. * offset, length.
  810. *
  811. * for now, we write only a single su, until we can
  812. * pass a stride back to the caller.
  813. */
  814. void ceph_calc_file_object_mapping(struct ceph_file_layout *layout,
  815. u64 off, u64 *plen,
  816. u64 *ono,
  817. u64 *oxoff, u64 *oxlen)
  818. {
  819. u32 osize = le32_to_cpu(layout->fl_object_size);
  820. u32 su = le32_to_cpu(layout->fl_stripe_unit);
  821. u32 sc = le32_to_cpu(layout->fl_stripe_count);
  822. u32 bl, stripeno, stripepos, objsetno;
  823. u32 su_per_object;
  824. u64 t, su_offset;
  825. dout("mapping %llu~%llu osize %u fl_su %u\n", off, *plen,
  826. osize, su);
  827. su_per_object = osize / su;
  828. dout("osize %u / su %u = su_per_object %u\n", osize, su,
  829. su_per_object);
  830. BUG_ON((su & ~PAGE_MASK) != 0);
  831. /* bl = *off / su; */
  832. t = off;
  833. do_div(t, su);
  834. bl = t;
  835. dout("off %llu / su %u = bl %u\n", off, su, bl);
  836. stripeno = bl / sc;
  837. stripepos = bl % sc;
  838. objsetno = stripeno / su_per_object;
  839. *ono = objsetno * sc + stripepos;
  840. dout("objset %u * sc %u = ono %u\n", objsetno, sc, (unsigned)*ono);
  841. /* *oxoff = *off % layout->fl_stripe_unit; # offset in su */
  842. t = off;
  843. su_offset = do_div(t, su);
  844. *oxoff = su_offset + (stripeno % su_per_object) * su;
  845. /*
  846. * Calculate the length of the extent being written to the selected
  847. * object. This is the minimum of the full length requested (plen) or
  848. * the remainder of the current stripe being written to.
  849. */
  850. *oxlen = min_t(u64, *plen, su - su_offset);
  851. *plen = *oxlen;
  852. dout(" obj extent %llu~%llu\n", *oxoff, *oxlen);
  853. }
  854. /*
  855. * calculate an object layout (i.e. pgid) from an oid,
  856. * file_layout, and osdmap
  857. */
  858. int ceph_calc_object_layout(struct ceph_object_layout *ol,
  859. const char *oid,
  860. struct ceph_file_layout *fl,
  861. struct ceph_osdmap *osdmap)
  862. {
  863. unsigned num, num_mask;
  864. struct ceph_pg pgid;
  865. s32 preferred = (s32)le32_to_cpu(fl->fl_pg_preferred);
  866. int poolid = le32_to_cpu(fl->fl_pg_pool);
  867. struct ceph_pg_pool_info *pool;
  868. unsigned ps;
  869. BUG_ON(!osdmap);
  870. pool = __lookup_pg_pool(&osdmap->pg_pools, poolid);
  871. if (!pool)
  872. return -EIO;
  873. ps = ceph_str_hash(pool->v.object_hash, oid, strlen(oid));
  874. if (preferred >= 0) {
  875. ps += preferred;
  876. num = le32_to_cpu(pool->v.lpg_num);
  877. num_mask = pool->lpg_num_mask;
  878. } else {
  879. num = le32_to_cpu(pool->v.pg_num);
  880. num_mask = pool->pg_num_mask;
  881. }
  882. pgid.ps = cpu_to_le16(ps);
  883. pgid.preferred = cpu_to_le16(preferred);
  884. pgid.pool = fl->fl_pg_pool;
  885. if (preferred >= 0)
  886. dout("calc_object_layout '%s' pgid %d.%xp%d\n", oid, poolid, ps,
  887. (int)preferred);
  888. else
  889. dout("calc_object_layout '%s' pgid %d.%x\n", oid, poolid, ps);
  890. ol->ol_pgid = pgid;
  891. ol->ol_stripe_unit = fl->fl_object_stripe_unit;
  892. return 0;
  893. }
  894. /*
  895. * Calculate raw osd vector for the given pgid. Return pointer to osd
  896. * array, or NULL on failure.
  897. */
  898. static int *calc_pg_raw(struct ceph_osdmap *osdmap, struct ceph_pg pgid,
  899. int *osds, int *num)
  900. {
  901. struct ceph_pg_mapping *pg;
  902. struct ceph_pg_pool_info *pool;
  903. int ruleno;
  904. unsigned poolid, ps, pps;
  905. int preferred;
  906. /* pg_temp? */
  907. pg = __lookup_pg_mapping(&osdmap->pg_temp, pgid);
  908. if (pg) {
  909. *num = pg->len;
  910. return pg->osds;
  911. }
  912. /* crush */
  913. poolid = le32_to_cpu(pgid.pool);
  914. ps = le16_to_cpu(pgid.ps);
  915. preferred = (s16)le16_to_cpu(pgid.preferred);
  916. /* don't forcefeed bad device ids to crush */
  917. if (preferred >= osdmap->max_osd ||
  918. preferred >= osdmap->crush->max_devices)
  919. preferred = -1;
  920. pool = __lookup_pg_pool(&osdmap->pg_pools, poolid);
  921. if (!pool)
  922. return NULL;
  923. ruleno = crush_find_rule(osdmap->crush, pool->v.crush_ruleset,
  924. pool->v.type, pool->v.size);
  925. if (ruleno < 0) {
  926. pr_err("no crush rule pool %d ruleset %d type %d size %d\n",
  927. poolid, pool->v.crush_ruleset, pool->v.type,
  928. pool->v.size);
  929. return NULL;
  930. }
  931. if (preferred >= 0)
  932. pps = ceph_stable_mod(ps,
  933. le32_to_cpu(pool->v.lpgp_num),
  934. pool->lpgp_num_mask);
  935. else
  936. pps = ceph_stable_mod(ps,
  937. le32_to_cpu(pool->v.pgp_num),
  938. pool->pgp_num_mask);
  939. pps += poolid;
  940. *num = crush_do_rule(osdmap->crush, ruleno, pps, osds,
  941. min_t(int, pool->v.size, *num),
  942. preferred, osdmap->osd_weight);
  943. return osds;
  944. }
  945. /*
  946. * Return acting set for given pgid.
  947. */
  948. int ceph_calc_pg_acting(struct ceph_osdmap *osdmap, struct ceph_pg pgid,
  949. int *acting)
  950. {
  951. int rawosds[CEPH_PG_MAX_SIZE], *osds;
  952. int i, o, num = CEPH_PG_MAX_SIZE;
  953. osds = calc_pg_raw(osdmap, pgid, rawosds, &num);
  954. if (!osds)
  955. return -1;
  956. /* primary is first up osd */
  957. o = 0;
  958. for (i = 0; i < num; i++)
  959. if (ceph_osd_is_up(osdmap, osds[i]))
  960. acting[o++] = osds[i];
  961. return o;
  962. }
  963. /*
  964. * Return primary osd for given pgid, or -1 if none.
  965. */
  966. int ceph_calc_pg_primary(struct ceph_osdmap *osdmap, struct ceph_pg pgid)
  967. {
  968. int rawosds[CEPH_PG_MAX_SIZE], *osds;
  969. int i, num = CEPH_PG_MAX_SIZE;
  970. osds = calc_pg_raw(osdmap, pgid, rawosds, &num);
  971. if (!osds)
  972. return -1;
  973. /* primary is first up osd */
  974. for (i = 0; i < num; i++)
  975. if (ceph_osd_is_up(osdmap, osds[i]))
  976. return osds[i];
  977. return -1;
  978. }