osdmap.c 28 KB

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