osdmap.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236
  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. static int __decode_pgid(void **p, void *end, struct ceph_pg *pg)
  579. {
  580. u8 v;
  581. ceph_decode_need(p, end, 1+8+4+4, bad);
  582. v = ceph_decode_8(p);
  583. if (v != 1)
  584. goto bad;
  585. pg->pool = ceph_decode_64(p);
  586. pg->seed = ceph_decode_32(p);
  587. *p += 4; /* skip preferred */
  588. return 0;
  589. bad:
  590. dout("error decoding pgid\n");
  591. return -EINVAL;
  592. }
  593. /*
  594. * decode a full map.
  595. */
  596. struct ceph_osdmap *osdmap_decode(void **p, void *end)
  597. {
  598. struct ceph_osdmap *map;
  599. u16 version;
  600. u32 len, max, i;
  601. int err = -EINVAL;
  602. void *start = *p;
  603. struct ceph_pg_pool_info *pi;
  604. dout("osdmap_decode %p to %p len %d\n", *p, end, (int)(end - *p));
  605. map = kzalloc(sizeof(*map), GFP_NOFS);
  606. if (map == NULL)
  607. return ERR_PTR(-ENOMEM);
  608. map->pg_temp = RB_ROOT;
  609. ceph_decode_16_safe(p, end, version, bad);
  610. if (version > 6) {
  611. pr_warning("got unknown v %d > 6 of osdmap\n", version);
  612. goto bad;
  613. }
  614. if (version < 6) {
  615. pr_warning("got old v %d < 6 of osdmap\n", version);
  616. goto bad;
  617. }
  618. ceph_decode_need(p, end, 2*sizeof(u64)+6*sizeof(u32), bad);
  619. ceph_decode_copy(p, &map->fsid, sizeof(map->fsid));
  620. map->epoch = ceph_decode_32(p);
  621. ceph_decode_copy(p, &map->created, sizeof(map->created));
  622. ceph_decode_copy(p, &map->modified, sizeof(map->modified));
  623. ceph_decode_32_safe(p, end, max, bad);
  624. while (max--) {
  625. ceph_decode_need(p, end, 8 + 2, bad);
  626. err = -ENOMEM;
  627. pi = kzalloc(sizeof(*pi), GFP_NOFS);
  628. if (!pi)
  629. goto bad;
  630. pi->id = ceph_decode_64(p);
  631. err = __decode_pool(p, end, pi);
  632. if (err < 0) {
  633. kfree(pi);
  634. goto bad;
  635. }
  636. __insert_pg_pool(&map->pg_pools, pi);
  637. }
  638. err = __decode_pool_names(p, end, map);
  639. if (err < 0) {
  640. dout("fail to decode pool names");
  641. goto bad;
  642. }
  643. ceph_decode_32_safe(p, end, map->pool_max, bad);
  644. ceph_decode_32_safe(p, end, map->flags, bad);
  645. max = ceph_decode_32(p);
  646. /* (re)alloc osd arrays */
  647. err = osdmap_set_max_osd(map, max);
  648. if (err < 0)
  649. goto bad;
  650. dout("osdmap_decode max_osd = %d\n", map->max_osd);
  651. /* osds */
  652. err = -EINVAL;
  653. ceph_decode_need(p, end, 3*sizeof(u32) +
  654. map->max_osd*(1 + sizeof(*map->osd_weight) +
  655. sizeof(*map->osd_addr)), bad);
  656. *p += 4; /* skip length field (should match max) */
  657. ceph_decode_copy(p, map->osd_state, map->max_osd);
  658. *p += 4; /* skip length field (should match max) */
  659. for (i = 0; i < map->max_osd; i++)
  660. map->osd_weight[i] = ceph_decode_32(p);
  661. *p += 4; /* skip length field (should match max) */
  662. ceph_decode_copy(p, map->osd_addr, map->max_osd*sizeof(*map->osd_addr));
  663. for (i = 0; i < map->max_osd; i++)
  664. ceph_decode_addr(&map->osd_addr[i]);
  665. /* pg_temp */
  666. ceph_decode_32_safe(p, end, len, bad);
  667. for (i = 0; i < len; i++) {
  668. int n, j;
  669. struct ceph_pg pgid;
  670. struct ceph_pg_mapping *pg;
  671. err = __decode_pgid(p, end, &pgid);
  672. if (err)
  673. goto bad;
  674. ceph_decode_need(p, end, sizeof(u32), bad);
  675. n = ceph_decode_32(p);
  676. err = -EINVAL;
  677. if (n > (UINT_MAX - sizeof(*pg)) / sizeof(u32))
  678. goto bad;
  679. ceph_decode_need(p, end, n * sizeof(u32), bad);
  680. err = -ENOMEM;
  681. pg = kmalloc(sizeof(*pg) + n*sizeof(u32), GFP_NOFS);
  682. if (!pg)
  683. goto bad;
  684. pg->pgid = pgid;
  685. pg->len = n;
  686. for (j = 0; j < n; j++)
  687. pg->osds[j] = ceph_decode_32(p);
  688. err = __insert_pg_mapping(pg, &map->pg_temp);
  689. if (err)
  690. goto bad;
  691. dout(" added pg_temp %lld.%x len %d\n", pgid.pool, pgid.seed,
  692. len);
  693. }
  694. /* crush */
  695. ceph_decode_32_safe(p, end, len, bad);
  696. dout("osdmap_decode crush len %d from off 0x%x\n", len,
  697. (int)(*p - start));
  698. ceph_decode_need(p, end, len, bad);
  699. map->crush = crush_decode(*p, end);
  700. *p += len;
  701. if (IS_ERR(map->crush)) {
  702. err = PTR_ERR(map->crush);
  703. map->crush = NULL;
  704. goto bad;
  705. }
  706. /* ignore the rest of the map */
  707. *p = end;
  708. dout("osdmap_decode done %p %p\n", *p, end);
  709. return map;
  710. bad:
  711. dout("osdmap_decode fail err %d\n", err);
  712. ceph_osdmap_destroy(map);
  713. return ERR_PTR(err);
  714. }
  715. /*
  716. * decode and apply an incremental map update.
  717. */
  718. struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end,
  719. struct ceph_osdmap *map,
  720. struct ceph_messenger *msgr)
  721. {
  722. struct crush_map *newcrush = NULL;
  723. struct ceph_fsid fsid;
  724. u32 epoch = 0;
  725. struct ceph_timespec modified;
  726. s32 len;
  727. u64 pool;
  728. __s64 new_pool_max;
  729. __s32 new_flags, max;
  730. void *start = *p;
  731. int err = -EINVAL;
  732. u16 version;
  733. ceph_decode_16_safe(p, end, version, bad);
  734. if (version != 6) {
  735. pr_warning("got unknown v %d != 6 of inc osdmap\n", version);
  736. goto bad;
  737. }
  738. ceph_decode_need(p, end, sizeof(fsid)+sizeof(modified)+2*sizeof(u32),
  739. bad);
  740. ceph_decode_copy(p, &fsid, sizeof(fsid));
  741. epoch = ceph_decode_32(p);
  742. BUG_ON(epoch != map->epoch+1);
  743. ceph_decode_copy(p, &modified, sizeof(modified));
  744. new_pool_max = ceph_decode_64(p);
  745. new_flags = ceph_decode_32(p);
  746. /* full map? */
  747. ceph_decode_32_safe(p, end, len, bad);
  748. if (len > 0) {
  749. dout("apply_incremental full map len %d, %p to %p\n",
  750. len, *p, end);
  751. return osdmap_decode(p, min(*p+len, end));
  752. }
  753. /* new crush? */
  754. ceph_decode_32_safe(p, end, len, bad);
  755. if (len > 0) {
  756. dout("apply_incremental new crush map len %d, %p to %p\n",
  757. len, *p, end);
  758. newcrush = crush_decode(*p, min(*p+len, end));
  759. if (IS_ERR(newcrush))
  760. return ERR_CAST(newcrush);
  761. *p += len;
  762. }
  763. /* new flags? */
  764. if (new_flags >= 0)
  765. map->flags = new_flags;
  766. if (new_pool_max >= 0)
  767. map->pool_max = new_pool_max;
  768. ceph_decode_need(p, end, 5*sizeof(u32), bad);
  769. /* new max? */
  770. max = ceph_decode_32(p);
  771. if (max >= 0) {
  772. err = osdmap_set_max_osd(map, max);
  773. if (err < 0)
  774. goto bad;
  775. }
  776. map->epoch++;
  777. map->modified = modified;
  778. if (newcrush) {
  779. if (map->crush)
  780. crush_destroy(map->crush);
  781. map->crush = newcrush;
  782. newcrush = NULL;
  783. }
  784. /* new_pool */
  785. ceph_decode_32_safe(p, end, len, bad);
  786. while (len--) {
  787. struct ceph_pg_pool_info *pi;
  788. ceph_decode_64_safe(p, end, pool, bad);
  789. pi = __lookup_pg_pool(&map->pg_pools, pool);
  790. if (!pi) {
  791. pi = kzalloc(sizeof(*pi), GFP_NOFS);
  792. if (!pi) {
  793. err = -ENOMEM;
  794. goto bad;
  795. }
  796. pi->id = pool;
  797. __insert_pg_pool(&map->pg_pools, pi);
  798. }
  799. err = __decode_pool(p, end, pi);
  800. if (err < 0)
  801. goto bad;
  802. }
  803. if (version >= 5) {
  804. err = __decode_pool_names(p, end, map);
  805. if (err < 0)
  806. goto bad;
  807. }
  808. /* old_pool */
  809. ceph_decode_32_safe(p, end, len, bad);
  810. while (len--) {
  811. struct ceph_pg_pool_info *pi;
  812. ceph_decode_64_safe(p, end, pool, bad);
  813. pi = __lookup_pg_pool(&map->pg_pools, pool);
  814. if (pi)
  815. __remove_pg_pool(&map->pg_pools, pi);
  816. }
  817. /* new_up */
  818. err = -EINVAL;
  819. ceph_decode_32_safe(p, end, len, bad);
  820. while (len--) {
  821. u32 osd;
  822. struct ceph_entity_addr addr;
  823. ceph_decode_32_safe(p, end, osd, bad);
  824. ceph_decode_copy_safe(p, end, &addr, sizeof(addr), bad);
  825. ceph_decode_addr(&addr);
  826. pr_info("osd%d up\n", osd);
  827. BUG_ON(osd >= map->max_osd);
  828. map->osd_state[osd] |= CEPH_OSD_UP;
  829. map->osd_addr[osd] = addr;
  830. }
  831. /* new_state */
  832. ceph_decode_32_safe(p, end, len, bad);
  833. while (len--) {
  834. u32 osd;
  835. u8 xorstate;
  836. ceph_decode_32_safe(p, end, osd, bad);
  837. xorstate = **(u8 **)p;
  838. (*p)++; /* clean flag */
  839. if (xorstate == 0)
  840. xorstate = CEPH_OSD_UP;
  841. if (xorstate & CEPH_OSD_UP)
  842. pr_info("osd%d down\n", osd);
  843. if (osd < map->max_osd)
  844. map->osd_state[osd] ^= xorstate;
  845. }
  846. /* new_weight */
  847. ceph_decode_32_safe(p, end, len, bad);
  848. while (len--) {
  849. u32 osd, off;
  850. ceph_decode_need(p, end, sizeof(u32)*2, bad);
  851. osd = ceph_decode_32(p);
  852. off = ceph_decode_32(p);
  853. pr_info("osd%d weight 0x%x %s\n", osd, off,
  854. off == CEPH_OSD_IN ? "(in)" :
  855. (off == CEPH_OSD_OUT ? "(out)" : ""));
  856. if (osd < map->max_osd)
  857. map->osd_weight[osd] = off;
  858. }
  859. /* new_pg_temp */
  860. ceph_decode_32_safe(p, end, len, bad);
  861. while (len--) {
  862. struct ceph_pg_mapping *pg;
  863. int j;
  864. struct ceph_pg pgid;
  865. u32 pglen;
  866. err = __decode_pgid(p, end, &pgid);
  867. if (err)
  868. goto bad;
  869. ceph_decode_need(p, end, sizeof(u32), bad);
  870. pglen = ceph_decode_32(p);
  871. if (pglen) {
  872. ceph_decode_need(p, end, pglen*sizeof(u32), bad);
  873. /* removing existing (if any) */
  874. (void) __remove_pg_mapping(&map->pg_temp, pgid);
  875. /* insert */
  876. err = -EINVAL;
  877. if (pglen > (UINT_MAX - sizeof(*pg)) / sizeof(u32))
  878. goto bad;
  879. err = -ENOMEM;
  880. pg = kmalloc(sizeof(*pg) + sizeof(u32)*pglen, GFP_NOFS);
  881. if (!pg)
  882. goto bad;
  883. pg->pgid = pgid;
  884. pg->len = pglen;
  885. for (j = 0; j < pglen; j++)
  886. pg->osds[j] = ceph_decode_32(p);
  887. err = __insert_pg_mapping(pg, &map->pg_temp);
  888. if (err) {
  889. kfree(pg);
  890. goto bad;
  891. }
  892. dout(" added pg_temp %lld.%x len %d\n", pgid.pool,
  893. pgid.seed, pglen);
  894. } else {
  895. /* remove */
  896. __remove_pg_mapping(&map->pg_temp, pgid);
  897. }
  898. }
  899. /* ignore the rest */
  900. *p = end;
  901. return map;
  902. bad:
  903. pr_err("corrupt inc osdmap epoch %d off %d (%p of %p-%p)\n",
  904. epoch, (int)(*p - start), *p, start, end);
  905. print_hex_dump(KERN_DEBUG, "osdmap: ",
  906. DUMP_PREFIX_OFFSET, 16, 1,
  907. start, end - start, true);
  908. if (newcrush)
  909. crush_destroy(newcrush);
  910. return ERR_PTR(err);
  911. }
  912. /*
  913. * calculate file layout from given offset, length.
  914. * fill in correct oid, logical length, and object extent
  915. * offset, length.
  916. *
  917. * for now, we write only a single su, until we can
  918. * pass a stride back to the caller.
  919. */
  920. int ceph_calc_file_object_mapping(struct ceph_file_layout *layout,
  921. u64 off, u64 len,
  922. u64 *ono,
  923. u64 *oxoff, u64 *oxlen)
  924. {
  925. u32 osize = le32_to_cpu(layout->fl_object_size);
  926. u32 su = le32_to_cpu(layout->fl_stripe_unit);
  927. u32 sc = le32_to_cpu(layout->fl_stripe_count);
  928. u32 bl, stripeno, stripepos, objsetno;
  929. u32 su_per_object;
  930. u64 t, su_offset;
  931. dout("mapping %llu~%llu osize %u fl_su %u\n", off, len,
  932. osize, su);
  933. if (su == 0 || sc == 0)
  934. goto invalid;
  935. su_per_object = osize / su;
  936. if (su_per_object == 0)
  937. goto invalid;
  938. dout("osize %u / su %u = su_per_object %u\n", osize, su,
  939. su_per_object);
  940. if ((su & ~PAGE_MASK) != 0)
  941. goto invalid;
  942. /* bl = *off / su; */
  943. t = off;
  944. do_div(t, su);
  945. bl = t;
  946. dout("off %llu / su %u = bl %u\n", off, su, bl);
  947. stripeno = bl / sc;
  948. stripepos = bl % sc;
  949. objsetno = stripeno / su_per_object;
  950. *ono = objsetno * sc + stripepos;
  951. dout("objset %u * sc %u = ono %u\n", objsetno, sc, (unsigned int)*ono);
  952. /* *oxoff = *off % layout->fl_stripe_unit; # offset in su */
  953. t = off;
  954. su_offset = do_div(t, su);
  955. *oxoff = su_offset + (stripeno % su_per_object) * su;
  956. /*
  957. * Calculate the length of the extent being written to the selected
  958. * object. This is the minimum of the full length requested (len) or
  959. * the remainder of the current stripe being written to.
  960. */
  961. *oxlen = min_t(u64, len, su - su_offset);
  962. dout(" obj extent %llu~%llu\n", *oxoff, *oxlen);
  963. return 0;
  964. invalid:
  965. dout(" invalid layout\n");
  966. *ono = 0;
  967. *oxoff = 0;
  968. *oxlen = 0;
  969. return -EINVAL;
  970. }
  971. EXPORT_SYMBOL(ceph_calc_file_object_mapping);
  972. /*
  973. * calculate an object layout (i.e. pgid) from an oid,
  974. * file_layout, and osdmap
  975. */
  976. int ceph_calc_ceph_pg(struct ceph_pg *pg, const char *oid,
  977. struct ceph_osdmap *osdmap, uint64_t pool)
  978. {
  979. struct ceph_pg_pool_info *pool_info;
  980. BUG_ON(!osdmap);
  981. pool_info = __lookup_pg_pool(&osdmap->pg_pools, pool);
  982. if (!pool_info)
  983. return -EIO;
  984. pg->pool = pool;
  985. pg->seed = ceph_str_hash(pool_info->object_hash, oid, strlen(oid));
  986. dout("%s '%s' pgid %lld.%x\n", __func__, oid, pg->pool, pg->seed);
  987. return 0;
  988. }
  989. EXPORT_SYMBOL(ceph_calc_ceph_pg);
  990. /*
  991. * Calculate raw osd vector for the given pgid. Return pointer to osd
  992. * array, or NULL on failure.
  993. */
  994. static int *calc_pg_raw(struct ceph_osdmap *osdmap, struct ceph_pg pgid,
  995. int *osds, int *num)
  996. {
  997. struct ceph_pg_mapping *pg;
  998. struct ceph_pg_pool_info *pool;
  999. int ruleno;
  1000. int r;
  1001. u32 pps;
  1002. pool = __lookup_pg_pool(&osdmap->pg_pools, pgid.pool);
  1003. if (!pool)
  1004. return NULL;
  1005. /* pg_temp? */
  1006. pgid.seed = ceph_stable_mod(pgid.seed, pool->pg_num,
  1007. pool->pgp_num_mask);
  1008. pg = __lookup_pg_mapping(&osdmap->pg_temp, pgid);
  1009. if (pg) {
  1010. *num = pg->len;
  1011. return pg->osds;
  1012. }
  1013. /* crush */
  1014. ruleno = crush_find_rule(osdmap->crush, pool->crush_ruleset,
  1015. pool->type, pool->size);
  1016. if (ruleno < 0) {
  1017. pr_err("no crush rule pool %lld ruleset %d type %d size %d\n",
  1018. pgid.pool, pool->crush_ruleset, pool->type,
  1019. pool->size);
  1020. return NULL;
  1021. }
  1022. if (pool->flags & CEPH_POOL_FLAG_HASHPSPOOL) {
  1023. /* hash pool id and seed sothat pool PGs do not overlap */
  1024. pps = crush_hash32_2(CRUSH_HASH_RJENKINS1,
  1025. ceph_stable_mod(pgid.seed, pool->pgp_num,
  1026. pool->pgp_num_mask),
  1027. pgid.pool);
  1028. } else {
  1029. /*
  1030. * legacy ehavior: add ps and pool together. this is
  1031. * not a great approach because the PGs from each pool
  1032. * will overlap on top of each other: 0.5 == 1.4 ==
  1033. * 2.3 == ...
  1034. */
  1035. pps = ceph_stable_mod(pgid.seed, pool->pgp_num,
  1036. pool->pgp_num_mask) +
  1037. (unsigned)pgid.pool;
  1038. }
  1039. r = crush_do_rule(osdmap->crush, ruleno, pps, osds,
  1040. min_t(int, pool->size, *num),
  1041. osdmap->osd_weight);
  1042. if (r < 0) {
  1043. pr_err("error %d from crush rule: pool %lld ruleset %d type %d"
  1044. " size %d\n", r, pgid.pool, pool->crush_ruleset,
  1045. pool->type, pool->size);
  1046. return NULL;
  1047. }
  1048. *num = r;
  1049. return osds;
  1050. }
  1051. /*
  1052. * Return acting set for given pgid.
  1053. */
  1054. int ceph_calc_pg_acting(struct ceph_osdmap *osdmap, struct ceph_pg pgid,
  1055. int *acting)
  1056. {
  1057. int rawosds[CEPH_PG_MAX_SIZE], *osds;
  1058. int i, o, num = CEPH_PG_MAX_SIZE;
  1059. osds = calc_pg_raw(osdmap, pgid, rawosds, &num);
  1060. if (!osds)
  1061. return -1;
  1062. /* primary is first up osd */
  1063. o = 0;
  1064. for (i = 0; i < num; i++)
  1065. if (ceph_osd_is_up(osdmap, osds[i]))
  1066. acting[o++] = osds[i];
  1067. return o;
  1068. }
  1069. /*
  1070. * Return primary osd for given pgid, or -1 if none.
  1071. */
  1072. int ceph_calc_pg_primary(struct ceph_osdmap *osdmap, struct ceph_pg pgid)
  1073. {
  1074. int rawosds[CEPH_PG_MAX_SIZE], *osds;
  1075. int i, num = CEPH_PG_MAX_SIZE;
  1076. osds = calc_pg_raw(osdmap, pgid, rawosds, &num);
  1077. if (!osds)
  1078. return -1;
  1079. /* primary is first up osd */
  1080. for (i = 0; i < num; i++)
  1081. if (ceph_osd_is_up(osdmap, osds[i]))
  1082. return osds[i];
  1083. return -1;
  1084. }
  1085. EXPORT_SYMBOL(ceph_calc_pg_primary);