osdmap.c 24 KB

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