osdmap.c 27 KB

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