osdmap.c 28 KB

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