osdmap.c 28 KB

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