osdmap.c 28 KB

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