osdmap.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  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. kfree(pi);
  542. goto bad;
  543. }
  544. __insert_pg_pool(&map->pg_pools, pi);
  545. }
  546. if (version >= 5 && __decode_pool_names(p, end, map) < 0)
  547. goto bad;
  548. ceph_decode_32_safe(p, end, map->pool_max, bad);
  549. ceph_decode_32_safe(p, end, map->flags, bad);
  550. max = ceph_decode_32(p);
  551. /* (re)alloc osd arrays */
  552. err = osdmap_set_max_osd(map, max);
  553. if (err < 0)
  554. goto bad;
  555. dout("osdmap_decode max_osd = %d\n", map->max_osd);
  556. /* osds */
  557. err = -EINVAL;
  558. ceph_decode_need(p, end, 3*sizeof(u32) +
  559. map->max_osd*(1 + sizeof(*map->osd_weight) +
  560. sizeof(*map->osd_addr)), bad);
  561. *p += 4; /* skip length field (should match max) */
  562. ceph_decode_copy(p, map->osd_state, map->max_osd);
  563. *p += 4; /* skip length field (should match max) */
  564. for (i = 0; i < map->max_osd; i++)
  565. map->osd_weight[i] = ceph_decode_32(p);
  566. *p += 4; /* skip length field (should match max) */
  567. ceph_decode_copy(p, map->osd_addr, map->max_osd*sizeof(*map->osd_addr));
  568. for (i = 0; i < map->max_osd; i++)
  569. ceph_decode_addr(&map->osd_addr[i]);
  570. /* pg_temp */
  571. ceph_decode_32_safe(p, end, len, bad);
  572. for (i = 0; i < len; i++) {
  573. int n, j;
  574. struct ceph_pg pgid;
  575. struct ceph_pg_mapping *pg;
  576. ceph_decode_need(p, end, sizeof(u32) + sizeof(u64), bad);
  577. ceph_decode_copy(p, &pgid, sizeof(pgid));
  578. n = ceph_decode_32(p);
  579. ceph_decode_need(p, end, n * sizeof(u32), bad);
  580. err = -ENOMEM;
  581. pg = kmalloc(sizeof(*pg) + n*sizeof(u32), GFP_NOFS);
  582. if (!pg)
  583. goto bad;
  584. pg->pgid = pgid;
  585. pg->len = n;
  586. for (j = 0; j < n; j++)
  587. pg->osds[j] = ceph_decode_32(p);
  588. err = __insert_pg_mapping(pg, &map->pg_temp);
  589. if (err)
  590. goto bad;
  591. dout(" added pg_temp %llx len %d\n", *(u64 *)&pgid, len);
  592. }
  593. /* crush */
  594. ceph_decode_32_safe(p, end, len, bad);
  595. dout("osdmap_decode crush len %d from off 0x%x\n", len,
  596. (int)(*p - start));
  597. ceph_decode_need(p, end, len, bad);
  598. map->crush = crush_decode(*p, end);
  599. *p += len;
  600. if (IS_ERR(map->crush)) {
  601. err = PTR_ERR(map->crush);
  602. map->crush = NULL;
  603. goto bad;
  604. }
  605. /* ignore the rest of the map */
  606. *p = end;
  607. dout("osdmap_decode done %p %p\n", *p, end);
  608. return map;
  609. bad:
  610. dout("osdmap_decode fail\n");
  611. ceph_osdmap_destroy(map);
  612. return ERR_PTR(err);
  613. }
  614. /*
  615. * decode and apply an incremental map update.
  616. */
  617. struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end,
  618. struct ceph_osdmap *map,
  619. struct ceph_messenger *msgr)
  620. {
  621. struct crush_map *newcrush = NULL;
  622. struct ceph_fsid fsid;
  623. u32 epoch = 0;
  624. struct ceph_timespec modified;
  625. u32 len, pool;
  626. __s32 new_pool_max, new_flags, max;
  627. void *start = *p;
  628. int err = -EINVAL;
  629. u16 version;
  630. struct rb_node *rbp;
  631. ceph_decode_16_safe(p, end, version, bad);
  632. if (version > CEPH_OSDMAP_INC_VERSION) {
  633. pr_warning("got unknown v %d > %d of inc osdmap\n", version,
  634. CEPH_OSDMAP_INC_VERSION);
  635. goto bad;
  636. }
  637. ceph_decode_need(p, end, sizeof(fsid)+sizeof(modified)+2*sizeof(u32),
  638. bad);
  639. ceph_decode_copy(p, &fsid, sizeof(fsid));
  640. epoch = ceph_decode_32(p);
  641. BUG_ON(epoch != map->epoch+1);
  642. ceph_decode_copy(p, &modified, sizeof(modified));
  643. new_pool_max = ceph_decode_32(p);
  644. new_flags = ceph_decode_32(p);
  645. /* full map? */
  646. ceph_decode_32_safe(p, end, len, bad);
  647. if (len > 0) {
  648. dout("apply_incremental full map len %d, %p to %p\n",
  649. len, *p, end);
  650. return osdmap_decode(p, min(*p+len, end));
  651. }
  652. /* new crush? */
  653. ceph_decode_32_safe(p, end, len, bad);
  654. if (len > 0) {
  655. dout("apply_incremental new crush map len %d, %p to %p\n",
  656. len, *p, end);
  657. newcrush = crush_decode(*p, min(*p+len, end));
  658. if (IS_ERR(newcrush))
  659. return ERR_CAST(newcrush);
  660. *p += len;
  661. }
  662. /* new flags? */
  663. if (new_flags >= 0)
  664. map->flags = new_flags;
  665. if (new_pool_max >= 0)
  666. map->pool_max = new_pool_max;
  667. ceph_decode_need(p, end, 5*sizeof(u32), bad);
  668. /* new max? */
  669. max = ceph_decode_32(p);
  670. if (max >= 0) {
  671. err = osdmap_set_max_osd(map, max);
  672. if (err < 0)
  673. goto bad;
  674. }
  675. map->epoch++;
  676. map->modified = modified;
  677. if (newcrush) {
  678. if (map->crush)
  679. crush_destroy(map->crush);
  680. map->crush = newcrush;
  681. newcrush = NULL;
  682. }
  683. /* new_pool */
  684. ceph_decode_32_safe(p, end, len, bad);
  685. while (len--) {
  686. __u8 ev;
  687. struct ceph_pg_pool_info *pi;
  688. ceph_decode_32_safe(p, end, pool, bad);
  689. ceph_decode_need(p, end, 1 + sizeof(pi->v), bad);
  690. ev = ceph_decode_8(p); /* encoding version */
  691. if (ev > CEPH_PG_POOL_VERSION) {
  692. pr_warning("got unknown v %d > %d of ceph_pg_pool\n",
  693. ev, CEPH_PG_POOL_VERSION);
  694. goto bad;
  695. }
  696. pi = __lookup_pg_pool(&map->pg_pools, pool);
  697. if (!pi) {
  698. pi = kzalloc(sizeof(*pi), GFP_NOFS);
  699. if (!pi) {
  700. err = -ENOMEM;
  701. goto bad;
  702. }
  703. pi->id = pool;
  704. __insert_pg_pool(&map->pg_pools, pi);
  705. }
  706. err = __decode_pool(p, end, pi);
  707. if (err < 0)
  708. goto bad;
  709. }
  710. if (version >= 5 && __decode_pool_names(p, end, map) < 0)
  711. goto bad;
  712. /* old_pool */
  713. ceph_decode_32_safe(p, end, len, bad);
  714. while (len--) {
  715. struct ceph_pg_pool_info *pi;
  716. ceph_decode_32_safe(p, end, pool, bad);
  717. pi = __lookup_pg_pool(&map->pg_pools, pool);
  718. if (pi)
  719. __remove_pg_pool(&map->pg_pools, pi);
  720. }
  721. /* new_up */
  722. err = -EINVAL;
  723. ceph_decode_32_safe(p, end, len, bad);
  724. while (len--) {
  725. u32 osd;
  726. struct ceph_entity_addr addr;
  727. ceph_decode_32_safe(p, end, osd, bad);
  728. ceph_decode_copy_safe(p, end, &addr, sizeof(addr), bad);
  729. ceph_decode_addr(&addr);
  730. pr_info("osd%d up\n", osd);
  731. BUG_ON(osd >= map->max_osd);
  732. map->osd_state[osd] |= CEPH_OSD_UP;
  733. map->osd_addr[osd] = addr;
  734. }
  735. /* new_state */
  736. ceph_decode_32_safe(p, end, len, bad);
  737. while (len--) {
  738. u32 osd;
  739. u8 xorstate;
  740. ceph_decode_32_safe(p, end, osd, bad);
  741. xorstate = **(u8 **)p;
  742. (*p)++; /* clean flag */
  743. if (xorstate == 0)
  744. xorstate = CEPH_OSD_UP;
  745. if (xorstate & CEPH_OSD_UP)
  746. pr_info("osd%d down\n", osd);
  747. if (osd < map->max_osd)
  748. map->osd_state[osd] ^= xorstate;
  749. }
  750. /* new_weight */
  751. ceph_decode_32_safe(p, end, len, bad);
  752. while (len--) {
  753. u32 osd, off;
  754. ceph_decode_need(p, end, sizeof(u32)*2, bad);
  755. osd = ceph_decode_32(p);
  756. off = ceph_decode_32(p);
  757. pr_info("osd%d weight 0x%x %s\n", osd, off,
  758. off == CEPH_OSD_IN ? "(in)" :
  759. (off == CEPH_OSD_OUT ? "(out)" : ""));
  760. if (osd < map->max_osd)
  761. map->osd_weight[osd] = off;
  762. }
  763. /* new_pg_temp */
  764. rbp = rb_first(&map->pg_temp);
  765. ceph_decode_32_safe(p, end, len, bad);
  766. while (len--) {
  767. struct ceph_pg_mapping *pg;
  768. int j;
  769. struct ceph_pg pgid;
  770. u32 pglen;
  771. ceph_decode_need(p, end, sizeof(u64) + sizeof(u32), bad);
  772. ceph_decode_copy(p, &pgid, sizeof(pgid));
  773. pglen = ceph_decode_32(p);
  774. /* remove any? */
  775. while (rbp && pgid_cmp(rb_entry(rbp, struct ceph_pg_mapping,
  776. node)->pgid, pgid) <= 0) {
  777. struct ceph_pg_mapping *cur =
  778. rb_entry(rbp, struct ceph_pg_mapping, node);
  779. rbp = rb_next(rbp);
  780. dout(" removed pg_temp %llx\n", *(u64 *)&cur->pgid);
  781. rb_erase(&cur->node, &map->pg_temp);
  782. kfree(cur);
  783. }
  784. if (pglen) {
  785. /* insert */
  786. ceph_decode_need(p, end, pglen*sizeof(u32), bad);
  787. pg = kmalloc(sizeof(*pg) + sizeof(u32)*pglen, GFP_NOFS);
  788. if (!pg) {
  789. err = -ENOMEM;
  790. goto bad;
  791. }
  792. pg->pgid = pgid;
  793. pg->len = pglen;
  794. for (j = 0; j < pglen; j++)
  795. pg->osds[j] = ceph_decode_32(p);
  796. err = __insert_pg_mapping(pg, &map->pg_temp);
  797. if (err) {
  798. kfree(pg);
  799. goto bad;
  800. }
  801. dout(" added pg_temp %llx len %d\n", *(u64 *)&pgid,
  802. pglen);
  803. }
  804. }
  805. while (rbp) {
  806. struct ceph_pg_mapping *cur =
  807. rb_entry(rbp, struct ceph_pg_mapping, node);
  808. rbp = rb_next(rbp);
  809. dout(" removed pg_temp %llx\n", *(u64 *)&cur->pgid);
  810. rb_erase(&cur->node, &map->pg_temp);
  811. kfree(cur);
  812. }
  813. /* ignore the rest */
  814. *p = end;
  815. return map;
  816. bad:
  817. pr_err("corrupt inc osdmap epoch %d off %d (%p of %p-%p)\n",
  818. epoch, (int)(*p - start), *p, start, end);
  819. print_hex_dump(KERN_DEBUG, "osdmap: ",
  820. DUMP_PREFIX_OFFSET, 16, 1,
  821. start, end - start, true);
  822. if (newcrush)
  823. crush_destroy(newcrush);
  824. return ERR_PTR(err);
  825. }
  826. /*
  827. * calculate file layout from given offset, length.
  828. * fill in correct oid, logical length, and object extent
  829. * offset, length.
  830. *
  831. * for now, we write only a single su, until we can
  832. * pass a stride back to the caller.
  833. */
  834. void ceph_calc_file_object_mapping(struct ceph_file_layout *layout,
  835. u64 off, u64 *plen,
  836. u64 *ono,
  837. u64 *oxoff, u64 *oxlen)
  838. {
  839. u32 osize = le32_to_cpu(layout->fl_object_size);
  840. u32 su = le32_to_cpu(layout->fl_stripe_unit);
  841. u32 sc = le32_to_cpu(layout->fl_stripe_count);
  842. u32 bl, stripeno, stripepos, objsetno;
  843. u32 su_per_object;
  844. u64 t, su_offset;
  845. dout("mapping %llu~%llu osize %u fl_su %u\n", off, *plen,
  846. osize, su);
  847. su_per_object = osize / su;
  848. dout("osize %u / su %u = su_per_object %u\n", osize, su,
  849. su_per_object);
  850. BUG_ON((su & ~PAGE_MASK) != 0);
  851. /* bl = *off / su; */
  852. t = off;
  853. do_div(t, su);
  854. bl = t;
  855. dout("off %llu / su %u = bl %u\n", off, su, bl);
  856. stripeno = bl / sc;
  857. stripepos = bl % sc;
  858. objsetno = stripeno / su_per_object;
  859. *ono = objsetno * sc + stripepos;
  860. dout("objset %u * sc %u = ono %u\n", objsetno, sc, (unsigned)*ono);
  861. /* *oxoff = *off % layout->fl_stripe_unit; # offset in su */
  862. t = off;
  863. su_offset = do_div(t, su);
  864. *oxoff = su_offset + (stripeno % su_per_object) * su;
  865. /*
  866. * Calculate the length of the extent being written to the selected
  867. * object. This is the minimum of the full length requested (plen) or
  868. * the remainder of the current stripe being written to.
  869. */
  870. *oxlen = min_t(u64, *plen, su - su_offset);
  871. *plen = *oxlen;
  872. dout(" obj extent %llu~%llu\n", *oxoff, *oxlen);
  873. }
  874. EXPORT_SYMBOL(ceph_calc_file_object_mapping);
  875. /*
  876. * calculate an object layout (i.e. pgid) from an oid,
  877. * file_layout, and osdmap
  878. */
  879. int ceph_calc_object_layout(struct ceph_object_layout *ol,
  880. const char *oid,
  881. struct ceph_file_layout *fl,
  882. struct ceph_osdmap *osdmap)
  883. {
  884. unsigned num, num_mask;
  885. struct ceph_pg pgid;
  886. s32 preferred = (s32)le32_to_cpu(fl->fl_pg_preferred);
  887. int poolid = le32_to_cpu(fl->fl_pg_pool);
  888. struct ceph_pg_pool_info *pool;
  889. unsigned ps;
  890. BUG_ON(!osdmap);
  891. pool = __lookup_pg_pool(&osdmap->pg_pools, poolid);
  892. if (!pool)
  893. return -EIO;
  894. ps = ceph_str_hash(pool->v.object_hash, oid, strlen(oid));
  895. if (preferred >= 0) {
  896. ps += preferred;
  897. num = le32_to_cpu(pool->v.lpg_num);
  898. num_mask = pool->lpg_num_mask;
  899. } else {
  900. num = le32_to_cpu(pool->v.pg_num);
  901. num_mask = pool->pg_num_mask;
  902. }
  903. pgid.ps = cpu_to_le16(ps);
  904. pgid.preferred = cpu_to_le16(preferred);
  905. pgid.pool = fl->fl_pg_pool;
  906. if (preferred >= 0)
  907. dout("calc_object_layout '%s' pgid %d.%xp%d\n", oid, poolid, ps,
  908. (int)preferred);
  909. else
  910. dout("calc_object_layout '%s' pgid %d.%x\n", oid, poolid, ps);
  911. ol->ol_pgid = pgid;
  912. ol->ol_stripe_unit = fl->fl_object_stripe_unit;
  913. return 0;
  914. }
  915. EXPORT_SYMBOL(ceph_calc_object_layout);
  916. /*
  917. * Calculate raw osd vector for the given pgid. Return pointer to osd
  918. * array, or NULL on failure.
  919. */
  920. static int *calc_pg_raw(struct ceph_osdmap *osdmap, struct ceph_pg pgid,
  921. int *osds, int *num)
  922. {
  923. struct ceph_pg_mapping *pg;
  924. struct ceph_pg_pool_info *pool;
  925. int ruleno;
  926. unsigned poolid, ps, pps;
  927. int preferred;
  928. /* pg_temp? */
  929. pg = __lookup_pg_mapping(&osdmap->pg_temp, pgid);
  930. if (pg) {
  931. *num = pg->len;
  932. return pg->osds;
  933. }
  934. /* crush */
  935. poolid = le32_to_cpu(pgid.pool);
  936. ps = le16_to_cpu(pgid.ps);
  937. preferred = (s16)le16_to_cpu(pgid.preferred);
  938. /* don't forcefeed bad device ids to crush */
  939. if (preferred >= osdmap->max_osd ||
  940. preferred >= osdmap->crush->max_devices)
  941. preferred = -1;
  942. pool = __lookup_pg_pool(&osdmap->pg_pools, poolid);
  943. if (!pool)
  944. return NULL;
  945. ruleno = crush_find_rule(osdmap->crush, pool->v.crush_ruleset,
  946. pool->v.type, pool->v.size);
  947. if (ruleno < 0) {
  948. pr_err("no crush rule pool %d ruleset %d type %d size %d\n",
  949. poolid, pool->v.crush_ruleset, pool->v.type,
  950. pool->v.size);
  951. return NULL;
  952. }
  953. if (preferred >= 0)
  954. pps = ceph_stable_mod(ps,
  955. le32_to_cpu(pool->v.lpgp_num),
  956. pool->lpgp_num_mask);
  957. else
  958. pps = ceph_stable_mod(ps,
  959. le32_to_cpu(pool->v.pgp_num),
  960. pool->pgp_num_mask);
  961. pps += poolid;
  962. *num = crush_do_rule(osdmap->crush, ruleno, pps, osds,
  963. min_t(int, pool->v.size, *num),
  964. preferred, osdmap->osd_weight);
  965. return osds;
  966. }
  967. /*
  968. * Return acting set for given pgid.
  969. */
  970. int ceph_calc_pg_acting(struct ceph_osdmap *osdmap, struct ceph_pg pgid,
  971. int *acting)
  972. {
  973. int rawosds[CEPH_PG_MAX_SIZE], *osds;
  974. int i, o, num = CEPH_PG_MAX_SIZE;
  975. osds = calc_pg_raw(osdmap, pgid, rawosds, &num);
  976. if (!osds)
  977. return -1;
  978. /* primary is first up osd */
  979. o = 0;
  980. for (i = 0; i < num; i++)
  981. if (ceph_osd_is_up(osdmap, osds[i]))
  982. acting[o++] = osds[i];
  983. return o;
  984. }
  985. /*
  986. * Return primary osd for given pgid, or -1 if none.
  987. */
  988. int ceph_calc_pg_primary(struct ceph_osdmap *osdmap, struct ceph_pg pgid)
  989. {
  990. int rawosds[CEPH_PG_MAX_SIZE], *osds;
  991. int i, num = CEPH_PG_MAX_SIZE;
  992. osds = calc_pg_raw(osdmap, pgid, rawosds, &num);
  993. if (!osds)
  994. return -1;
  995. /* primary is first up osd */
  996. for (i = 0; i < num; i++)
  997. if (ceph_osd_is_up(osdmap, osds[i]))
  998. return osds[i];
  999. return -1;
  1000. }
  1001. EXPORT_SYMBOL(ceph_calc_pg_primary);