osdmap.c 26 KB

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