osdmap.c 29 KB

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