osdmap.c 27 KB

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