osdmap.c 24 KB

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