osdmap.c 28 KB

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