osdmap.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  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(*r))
  256. / sizeof(struct crush_rule_step))
  257. goto bad;
  258. #endif
  259. r = c->rules[i] = kmalloc(sizeof(*r) +
  260. yes*sizeof(struct crush_rule_step),
  261. GFP_NOFS);
  262. if (r == NULL)
  263. goto badmem;
  264. dout(" rule %d is at %p\n", i, r);
  265. r->len = yes;
  266. ceph_decode_copy_safe(p, end, &r->mask, 4, bad); /* 4 u8's */
  267. ceph_decode_need(p, end, r->len*3*sizeof(u32), bad);
  268. for (j = 0; j < r->len; j++) {
  269. r->steps[j].op = ceph_decode_32(p);
  270. r->steps[j].arg1 = ceph_decode_32(p);
  271. r->steps[j].arg2 = ceph_decode_32(p);
  272. }
  273. }
  274. /* ignore trailing name maps. */
  275. dout("crush_decode success\n");
  276. return c;
  277. badmem:
  278. err = -ENOMEM;
  279. bad:
  280. dout("crush_decode fail %d\n", err);
  281. crush_destroy(c);
  282. return ERR_PTR(err);
  283. }
  284. /*
  285. * rbtree of pg_mapping for handling pg_temp (explicit mapping of pgid
  286. * to a set of osds)
  287. */
  288. static int pgid_cmp(struct ceph_pg l, struct ceph_pg r)
  289. {
  290. u64 a = *(u64 *)&l;
  291. u64 b = *(u64 *)&r;
  292. if (a < b)
  293. return -1;
  294. if (a > b)
  295. return 1;
  296. return 0;
  297. }
  298. static int __insert_pg_mapping(struct ceph_pg_mapping *new,
  299. struct rb_root *root)
  300. {
  301. struct rb_node **p = &root->rb_node;
  302. struct rb_node *parent = NULL;
  303. struct ceph_pg_mapping *pg = NULL;
  304. int c;
  305. dout("__insert_pg_mapping %llx %p\n", *(u64 *)&new->pgid, new);
  306. while (*p) {
  307. parent = *p;
  308. pg = rb_entry(parent, struct ceph_pg_mapping, node);
  309. c = pgid_cmp(new->pgid, pg->pgid);
  310. if (c < 0)
  311. p = &(*p)->rb_left;
  312. else if (c > 0)
  313. p = &(*p)->rb_right;
  314. else
  315. return -EEXIST;
  316. }
  317. rb_link_node(&new->node, parent, p);
  318. rb_insert_color(&new->node, root);
  319. return 0;
  320. }
  321. static struct ceph_pg_mapping *__lookup_pg_mapping(struct rb_root *root,
  322. struct ceph_pg pgid)
  323. {
  324. struct rb_node *n = root->rb_node;
  325. struct ceph_pg_mapping *pg;
  326. int c;
  327. while (n) {
  328. pg = rb_entry(n, struct ceph_pg_mapping, node);
  329. c = pgid_cmp(pgid, pg->pgid);
  330. if (c < 0) {
  331. n = n->rb_left;
  332. } else if (c > 0) {
  333. n = n->rb_right;
  334. } else {
  335. dout("__lookup_pg_mapping %llx got %p\n",
  336. *(u64 *)&pgid, pg);
  337. return pg;
  338. }
  339. }
  340. return NULL;
  341. }
  342. static int __remove_pg_mapping(struct rb_root *root, struct ceph_pg pgid)
  343. {
  344. struct ceph_pg_mapping *pg = __lookup_pg_mapping(root, pgid);
  345. if (pg) {
  346. dout("__remove_pg_mapping %llx %p\n", *(u64 *)&pgid, pg);
  347. rb_erase(&pg->node, root);
  348. kfree(pg);
  349. return 0;
  350. }
  351. dout("__remove_pg_mapping %llx dne\n", *(u64 *)&pgid);
  352. return -ENOENT;
  353. }
  354. /*
  355. * rbtree of pg pool info
  356. */
  357. static int __insert_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *new)
  358. {
  359. struct rb_node **p = &root->rb_node;
  360. struct rb_node *parent = NULL;
  361. struct ceph_pg_pool_info *pi = NULL;
  362. while (*p) {
  363. parent = *p;
  364. pi = rb_entry(parent, struct ceph_pg_pool_info, node);
  365. if (new->id < pi->id)
  366. p = &(*p)->rb_left;
  367. else if (new->id > pi->id)
  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_pool_info *__lookup_pg_pool(struct rb_root *root, int id)
  377. {
  378. struct ceph_pg_pool_info *pi;
  379. struct rb_node *n = root->rb_node;
  380. while (n) {
  381. pi = rb_entry(n, struct ceph_pg_pool_info, node);
  382. if (id < pi->id)
  383. n = n->rb_left;
  384. else if (id > pi->id)
  385. n = n->rb_right;
  386. else
  387. return pi;
  388. }
  389. return NULL;
  390. }
  391. int ceph_pg_poolid_by_name(struct ceph_osdmap *map, const char *name)
  392. {
  393. struct rb_node *rbp;
  394. for (rbp = rb_first(&map->pg_pools); rbp; rbp = rb_next(rbp)) {
  395. struct ceph_pg_pool_info *pi =
  396. rb_entry(rbp, struct ceph_pg_pool_info, node);
  397. if (pi->name && strcmp(pi->name, name) == 0)
  398. return pi->id;
  399. }
  400. return -ENOENT;
  401. }
  402. EXPORT_SYMBOL(ceph_pg_poolid_by_name);
  403. static void __remove_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *pi)
  404. {
  405. rb_erase(&pi->node, root);
  406. kfree(pi->name);
  407. kfree(pi);
  408. }
  409. static int __decode_pool(void **p, void *end, struct ceph_pg_pool_info *pi)
  410. {
  411. unsigned n, m;
  412. ceph_decode_copy(p, &pi->v, sizeof(pi->v));
  413. calc_pg_masks(pi);
  414. /* num_snaps * snap_info_t */
  415. n = le32_to_cpu(pi->v.num_snaps);
  416. while (n--) {
  417. ceph_decode_need(p, end, sizeof(u64) + 1 + sizeof(u64) +
  418. sizeof(struct ceph_timespec), bad);
  419. *p += sizeof(u64) + /* key */
  420. 1 + sizeof(u64) + /* u8, snapid */
  421. sizeof(struct ceph_timespec);
  422. m = ceph_decode_32(p); /* snap name */
  423. *p += m;
  424. }
  425. *p += le32_to_cpu(pi->v.num_removed_snap_intervals) * sizeof(u64) * 2;
  426. return 0;
  427. bad:
  428. return -EINVAL;
  429. }
  430. static int __decode_pool_names(void **p, void *end, struct ceph_osdmap *map)
  431. {
  432. struct ceph_pg_pool_info *pi;
  433. u32 num, len, pool;
  434. ceph_decode_32_safe(p, end, num, bad);
  435. dout(" %d pool names\n", num);
  436. while (num--) {
  437. ceph_decode_32_safe(p, end, pool, bad);
  438. ceph_decode_32_safe(p, end, len, bad);
  439. dout(" pool %d len %d\n", pool, len);
  440. pi = __lookup_pg_pool(&map->pg_pools, pool);
  441. if (pi) {
  442. kfree(pi->name);
  443. pi->name = kmalloc(len + 1, GFP_NOFS);
  444. if (pi->name) {
  445. memcpy(pi->name, *p, len);
  446. pi->name[len] = '\0';
  447. dout(" name is %s\n", pi->name);
  448. }
  449. }
  450. *p += len;
  451. }
  452. return 0;
  453. bad:
  454. return -EINVAL;
  455. }
  456. /*
  457. * osd map
  458. */
  459. void ceph_osdmap_destroy(struct ceph_osdmap *map)
  460. {
  461. dout("osdmap_destroy %p\n", map);
  462. if (map->crush)
  463. crush_destroy(map->crush);
  464. while (!RB_EMPTY_ROOT(&map->pg_temp)) {
  465. struct ceph_pg_mapping *pg =
  466. rb_entry(rb_first(&map->pg_temp),
  467. struct ceph_pg_mapping, node);
  468. rb_erase(&pg->node, &map->pg_temp);
  469. kfree(pg);
  470. }
  471. while (!RB_EMPTY_ROOT(&map->pg_pools)) {
  472. struct ceph_pg_pool_info *pi =
  473. rb_entry(rb_first(&map->pg_pools),
  474. struct ceph_pg_pool_info, node);
  475. __remove_pg_pool(&map->pg_pools, pi);
  476. }
  477. kfree(map->osd_state);
  478. kfree(map->osd_weight);
  479. kfree(map->osd_addr);
  480. kfree(map);
  481. }
  482. /*
  483. * adjust max osd value. reallocate arrays.
  484. */
  485. static int osdmap_set_max_osd(struct ceph_osdmap *map, int max)
  486. {
  487. u8 *state;
  488. struct ceph_entity_addr *addr;
  489. u32 *weight;
  490. state = kcalloc(max, sizeof(*state), GFP_NOFS);
  491. addr = kcalloc(max, sizeof(*addr), GFP_NOFS);
  492. weight = kcalloc(max, sizeof(*weight), GFP_NOFS);
  493. if (state == NULL || addr == NULL || weight == NULL) {
  494. kfree(state);
  495. kfree(addr);
  496. kfree(weight);
  497. return -ENOMEM;
  498. }
  499. /* copy old? */
  500. if (map->osd_state) {
  501. memcpy(state, map->osd_state, map->max_osd*sizeof(*state));
  502. memcpy(addr, map->osd_addr, map->max_osd*sizeof(*addr));
  503. memcpy(weight, map->osd_weight, map->max_osd*sizeof(*weight));
  504. kfree(map->osd_state);
  505. kfree(map->osd_addr);
  506. kfree(map->osd_weight);
  507. }
  508. map->osd_state = state;
  509. map->osd_weight = weight;
  510. map->osd_addr = addr;
  511. map->max_osd = max;
  512. return 0;
  513. }
  514. /*
  515. * decode a full map.
  516. */
  517. struct ceph_osdmap *osdmap_decode(void **p, void *end)
  518. {
  519. struct ceph_osdmap *map;
  520. u16 version;
  521. u32 len, max, i;
  522. u8 ev;
  523. int err = -EINVAL;
  524. void *start = *p;
  525. struct ceph_pg_pool_info *pi;
  526. dout("osdmap_decode %p to %p len %d\n", *p, end, (int)(end - *p));
  527. map = kzalloc(sizeof(*map), GFP_NOFS);
  528. if (map == NULL)
  529. return ERR_PTR(-ENOMEM);
  530. map->pg_temp = RB_ROOT;
  531. ceph_decode_16_safe(p, end, version, bad);
  532. if (version > CEPH_OSDMAP_VERSION) {
  533. pr_warning("got unknown v %d > %d of osdmap\n", version,
  534. CEPH_OSDMAP_VERSION);
  535. goto bad;
  536. }
  537. ceph_decode_need(p, end, 2*sizeof(u64)+6*sizeof(u32), bad);
  538. ceph_decode_copy(p, &map->fsid, sizeof(map->fsid));
  539. map->epoch = ceph_decode_32(p);
  540. ceph_decode_copy(p, &map->created, sizeof(map->created));
  541. ceph_decode_copy(p, &map->modified, sizeof(map->modified));
  542. ceph_decode_32_safe(p, end, max, bad);
  543. while (max--) {
  544. ceph_decode_need(p, end, 4 + 1 + sizeof(pi->v), bad);
  545. pi = kzalloc(sizeof(*pi), GFP_NOFS);
  546. if (!pi)
  547. goto bad;
  548. pi->id = ceph_decode_32(p);
  549. ev = ceph_decode_8(p); /* encoding version */
  550. if (ev > CEPH_PG_POOL_VERSION) {
  551. pr_warning("got unknown v %d > %d of ceph_pg_pool\n",
  552. ev, CEPH_PG_POOL_VERSION);
  553. kfree(pi);
  554. goto bad;
  555. }
  556. err = __decode_pool(p, end, pi);
  557. if (err < 0) {
  558. kfree(pi);
  559. goto bad;
  560. }
  561. __insert_pg_pool(&map->pg_pools, pi);
  562. }
  563. if (version >= 5 && __decode_pool_names(p, end, map) < 0)
  564. goto bad;
  565. ceph_decode_32_safe(p, end, map->pool_max, bad);
  566. ceph_decode_32_safe(p, end, map->flags, bad);
  567. max = ceph_decode_32(p);
  568. /* (re)alloc osd arrays */
  569. err = osdmap_set_max_osd(map, max);
  570. if (err < 0)
  571. goto bad;
  572. dout("osdmap_decode max_osd = %d\n", map->max_osd);
  573. /* osds */
  574. err = -EINVAL;
  575. ceph_decode_need(p, end, 3*sizeof(u32) +
  576. map->max_osd*(1 + sizeof(*map->osd_weight) +
  577. sizeof(*map->osd_addr)), bad);
  578. *p += 4; /* skip length field (should match max) */
  579. ceph_decode_copy(p, map->osd_state, map->max_osd);
  580. *p += 4; /* skip length field (should match max) */
  581. for (i = 0; i < map->max_osd; i++)
  582. map->osd_weight[i] = ceph_decode_32(p);
  583. *p += 4; /* skip length field (should match max) */
  584. ceph_decode_copy(p, map->osd_addr, map->max_osd*sizeof(*map->osd_addr));
  585. for (i = 0; i < map->max_osd; i++)
  586. ceph_decode_addr(&map->osd_addr[i]);
  587. /* pg_temp */
  588. ceph_decode_32_safe(p, end, len, bad);
  589. for (i = 0; i < len; i++) {
  590. int n, j;
  591. struct ceph_pg pgid;
  592. struct ceph_pg_mapping *pg;
  593. ceph_decode_need(p, end, sizeof(u32) + sizeof(u64), bad);
  594. ceph_decode_copy(p, &pgid, sizeof(pgid));
  595. n = ceph_decode_32(p);
  596. ceph_decode_need(p, end, n * sizeof(u32), bad);
  597. err = -ENOMEM;
  598. pg = kmalloc(sizeof(*pg) + n*sizeof(u32), GFP_NOFS);
  599. if (!pg)
  600. goto bad;
  601. pg->pgid = pgid;
  602. pg->len = n;
  603. for (j = 0; j < n; j++)
  604. pg->osds[j] = ceph_decode_32(p);
  605. err = __insert_pg_mapping(pg, &map->pg_temp);
  606. if (err)
  607. goto bad;
  608. dout(" added pg_temp %llx len %d\n", *(u64 *)&pgid, len);
  609. }
  610. /* crush */
  611. ceph_decode_32_safe(p, end, len, bad);
  612. dout("osdmap_decode crush len %d from off 0x%x\n", len,
  613. (int)(*p - start));
  614. ceph_decode_need(p, end, len, bad);
  615. map->crush = crush_decode(*p, end);
  616. *p += len;
  617. if (IS_ERR(map->crush)) {
  618. err = PTR_ERR(map->crush);
  619. map->crush = NULL;
  620. goto bad;
  621. }
  622. /* ignore the rest of the map */
  623. *p = end;
  624. dout("osdmap_decode done %p %p\n", *p, end);
  625. return map;
  626. bad:
  627. dout("osdmap_decode fail\n");
  628. ceph_osdmap_destroy(map);
  629. return ERR_PTR(err);
  630. }
  631. /*
  632. * decode and apply an incremental map update.
  633. */
  634. struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end,
  635. struct ceph_osdmap *map,
  636. struct ceph_messenger *msgr)
  637. {
  638. struct crush_map *newcrush = NULL;
  639. struct ceph_fsid fsid;
  640. u32 epoch = 0;
  641. struct ceph_timespec modified;
  642. u32 len, pool;
  643. __s32 new_pool_max, new_flags, max;
  644. void *start = *p;
  645. int err = -EINVAL;
  646. u16 version;
  647. ceph_decode_16_safe(p, end, version, bad);
  648. if (version > CEPH_OSDMAP_INC_VERSION) {
  649. pr_warning("got unknown v %d > %d of inc osdmap\n", version,
  650. CEPH_OSDMAP_INC_VERSION);
  651. goto bad;
  652. }
  653. ceph_decode_need(p, end, sizeof(fsid)+sizeof(modified)+2*sizeof(u32),
  654. bad);
  655. ceph_decode_copy(p, &fsid, sizeof(fsid));
  656. epoch = ceph_decode_32(p);
  657. BUG_ON(epoch != map->epoch+1);
  658. ceph_decode_copy(p, &modified, sizeof(modified));
  659. new_pool_max = ceph_decode_32(p);
  660. new_flags = ceph_decode_32(p);
  661. /* full map? */
  662. ceph_decode_32_safe(p, end, len, bad);
  663. if (len > 0) {
  664. dout("apply_incremental full map len %d, %p to %p\n",
  665. len, *p, end);
  666. return osdmap_decode(p, min(*p+len, end));
  667. }
  668. /* new crush? */
  669. ceph_decode_32_safe(p, end, len, bad);
  670. if (len > 0) {
  671. dout("apply_incremental new crush map len %d, %p to %p\n",
  672. len, *p, end);
  673. newcrush = crush_decode(*p, min(*p+len, end));
  674. if (IS_ERR(newcrush))
  675. return ERR_CAST(newcrush);
  676. *p += len;
  677. }
  678. /* new flags? */
  679. if (new_flags >= 0)
  680. map->flags = new_flags;
  681. if (new_pool_max >= 0)
  682. map->pool_max = new_pool_max;
  683. ceph_decode_need(p, end, 5*sizeof(u32), bad);
  684. /* new max? */
  685. max = ceph_decode_32(p);
  686. if (max >= 0) {
  687. err = osdmap_set_max_osd(map, max);
  688. if (err < 0)
  689. goto bad;
  690. }
  691. map->epoch++;
  692. map->modified = modified;
  693. if (newcrush) {
  694. if (map->crush)
  695. crush_destroy(map->crush);
  696. map->crush = newcrush;
  697. newcrush = NULL;
  698. }
  699. /* new_pool */
  700. ceph_decode_32_safe(p, end, len, bad);
  701. while (len--) {
  702. __u8 ev;
  703. struct ceph_pg_pool_info *pi;
  704. ceph_decode_32_safe(p, end, pool, bad);
  705. ceph_decode_need(p, end, 1 + sizeof(pi->v), bad);
  706. ev = ceph_decode_8(p); /* encoding version */
  707. if (ev > CEPH_PG_POOL_VERSION) {
  708. pr_warning("got unknown v %d > %d of ceph_pg_pool\n",
  709. ev, CEPH_PG_POOL_VERSION);
  710. goto bad;
  711. }
  712. pi = __lookup_pg_pool(&map->pg_pools, pool);
  713. if (!pi) {
  714. pi = kzalloc(sizeof(*pi), GFP_NOFS);
  715. if (!pi) {
  716. err = -ENOMEM;
  717. goto bad;
  718. }
  719. pi->id = pool;
  720. __insert_pg_pool(&map->pg_pools, pi);
  721. }
  722. err = __decode_pool(p, end, pi);
  723. if (err < 0)
  724. goto bad;
  725. }
  726. if (version >= 5 && __decode_pool_names(p, end, map) < 0)
  727. goto bad;
  728. /* old_pool */
  729. ceph_decode_32_safe(p, end, len, bad);
  730. while (len--) {
  731. struct ceph_pg_pool_info *pi;
  732. ceph_decode_32_safe(p, end, pool, bad);
  733. pi = __lookup_pg_pool(&map->pg_pools, pool);
  734. if (pi)
  735. __remove_pg_pool(&map->pg_pools, pi);
  736. }
  737. /* new_up */
  738. err = -EINVAL;
  739. ceph_decode_32_safe(p, end, len, bad);
  740. while (len--) {
  741. u32 osd;
  742. struct ceph_entity_addr addr;
  743. ceph_decode_32_safe(p, end, osd, bad);
  744. ceph_decode_copy_safe(p, end, &addr, sizeof(addr), bad);
  745. ceph_decode_addr(&addr);
  746. pr_info("osd%d up\n", osd);
  747. BUG_ON(osd >= map->max_osd);
  748. map->osd_state[osd] |= CEPH_OSD_UP;
  749. map->osd_addr[osd] = addr;
  750. }
  751. /* new_state */
  752. ceph_decode_32_safe(p, end, len, bad);
  753. while (len--) {
  754. u32 osd;
  755. u8 xorstate;
  756. ceph_decode_32_safe(p, end, osd, bad);
  757. xorstate = **(u8 **)p;
  758. (*p)++; /* clean flag */
  759. if (xorstate == 0)
  760. xorstate = CEPH_OSD_UP;
  761. if (xorstate & CEPH_OSD_UP)
  762. pr_info("osd%d down\n", osd);
  763. if (osd < map->max_osd)
  764. map->osd_state[osd] ^= xorstate;
  765. }
  766. /* new_weight */
  767. ceph_decode_32_safe(p, end, len, bad);
  768. while (len--) {
  769. u32 osd, off;
  770. ceph_decode_need(p, end, sizeof(u32)*2, bad);
  771. osd = ceph_decode_32(p);
  772. off = ceph_decode_32(p);
  773. pr_info("osd%d weight 0x%x %s\n", osd, off,
  774. off == CEPH_OSD_IN ? "(in)" :
  775. (off == CEPH_OSD_OUT ? "(out)" : ""));
  776. if (osd < map->max_osd)
  777. map->osd_weight[osd] = off;
  778. }
  779. /* new_pg_temp */
  780. ceph_decode_32_safe(p, end, len, bad);
  781. while (len--) {
  782. struct ceph_pg_mapping *pg;
  783. int j;
  784. struct ceph_pg pgid;
  785. u32 pglen;
  786. ceph_decode_need(p, end, sizeof(u64) + sizeof(u32), bad);
  787. ceph_decode_copy(p, &pgid, sizeof(pgid));
  788. pglen = ceph_decode_32(p);
  789. if (pglen) {
  790. /* insert */
  791. ceph_decode_need(p, end, pglen*sizeof(u32), bad);
  792. pg = kmalloc(sizeof(*pg) + sizeof(u32)*pglen, GFP_NOFS);
  793. if (!pg) {
  794. err = -ENOMEM;
  795. goto bad;
  796. }
  797. pg->pgid = pgid;
  798. pg->len = pglen;
  799. for (j = 0; j < pglen; j++)
  800. pg->osds[j] = ceph_decode_32(p);
  801. err = __insert_pg_mapping(pg, &map->pg_temp);
  802. if (err) {
  803. kfree(pg);
  804. goto bad;
  805. }
  806. dout(" added pg_temp %llx len %d\n", *(u64 *)&pgid,
  807. pglen);
  808. } else {
  809. /* remove */
  810. __remove_pg_mapping(&map->pg_temp, pgid);
  811. }
  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, t;
  927. int preferred;
  928. poolid = le32_to_cpu(pgid.pool);
  929. ps = le16_to_cpu(pgid.ps);
  930. preferred = (s16)le16_to_cpu(pgid.preferred);
  931. pool = __lookup_pg_pool(&osdmap->pg_pools, poolid);
  932. if (!pool)
  933. return NULL;
  934. /* pg_temp? */
  935. if (preferred >= 0)
  936. t = ceph_stable_mod(ps, le32_to_cpu(pool->v.lpg_num),
  937. pool->lpgp_num_mask);
  938. else
  939. t = ceph_stable_mod(ps, le32_to_cpu(pool->v.pg_num),
  940. pool->pgp_num_mask);
  941. pgid.ps = cpu_to_le16(t);
  942. pg = __lookup_pg_mapping(&osdmap->pg_temp, pgid);
  943. if (pg) {
  944. *num = pg->len;
  945. return pg->osds;
  946. }
  947. /* crush */
  948. ruleno = crush_find_rule(osdmap->crush, pool->v.crush_ruleset,
  949. pool->v.type, pool->v.size);
  950. if (ruleno < 0) {
  951. pr_err("no crush rule pool %d ruleset %d type %d size %d\n",
  952. poolid, pool->v.crush_ruleset, pool->v.type,
  953. pool->v.size);
  954. return NULL;
  955. }
  956. /* don't forcefeed bad device ids to crush */
  957. if (preferred >= osdmap->max_osd ||
  958. preferred >= osdmap->crush->max_devices)
  959. preferred = -1;
  960. if (preferred >= 0)
  961. pps = ceph_stable_mod(ps,
  962. le32_to_cpu(pool->v.lpgp_num),
  963. pool->lpgp_num_mask);
  964. else
  965. pps = ceph_stable_mod(ps,
  966. le32_to_cpu(pool->v.pgp_num),
  967. pool->pgp_num_mask);
  968. pps += poolid;
  969. *num = crush_do_rule(osdmap->crush, ruleno, pps, osds,
  970. min_t(int, pool->v.size, *num),
  971. preferred, osdmap->osd_weight);
  972. return osds;
  973. }
  974. /*
  975. * Return acting set for given pgid.
  976. */
  977. int ceph_calc_pg_acting(struct ceph_osdmap *osdmap, struct ceph_pg pgid,
  978. int *acting)
  979. {
  980. int rawosds[CEPH_PG_MAX_SIZE], *osds;
  981. int i, o, num = CEPH_PG_MAX_SIZE;
  982. osds = calc_pg_raw(osdmap, pgid, rawosds, &num);
  983. if (!osds)
  984. return -1;
  985. /* primary is first up osd */
  986. o = 0;
  987. for (i = 0; i < num; i++)
  988. if (ceph_osd_is_up(osdmap, osds[i]))
  989. acting[o++] = osds[i];
  990. return o;
  991. }
  992. /*
  993. * Return primary osd for given pgid, or -1 if none.
  994. */
  995. int ceph_calc_pg_primary(struct ceph_osdmap *osdmap, struct ceph_pg pgid)
  996. {
  997. int rawosds[CEPH_PG_MAX_SIZE], *osds;
  998. int i, num = CEPH_PG_MAX_SIZE;
  999. osds = calc_pg_raw(osdmap, pgid, rawosds, &num);
  1000. if (!osds)
  1001. return -1;
  1002. /* primary is first up osd */
  1003. for (i = 0; i < num; i++)
  1004. if (ceph_osd_is_up(osdmap, osds[i]))
  1005. return osds[i];
  1006. return -1;
  1007. }
  1008. EXPORT_SYMBOL(ceph_calc_pg_primary);