osdmap.c 26 KB

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