osdmap.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  1. #include <linux/ceph/ceph_debug.h>
  2. #include <linux/module.h>
  3. #include <linux/slab.h>
  4. #include <asm/div64.h>
  5. #include <linux/ceph/libceph.h>
  6. #include <linux/ceph/osdmap.h>
  7. #include <linux/ceph/decode.h>
  8. #include <linux/crush/hash.h>
  9. #include <linux/crush/mapper.h>
  10. char *ceph_osdmap_state_str(char *str, int len, int state)
  11. {
  12. int flag = 0;
  13. if (!len)
  14. goto done;
  15. *str = '\0';
  16. if (state) {
  17. if (state & CEPH_OSD_EXISTS) {
  18. snprintf(str, len, "exists");
  19. flag = 1;
  20. }
  21. if (state & CEPH_OSD_UP) {
  22. snprintf(str, len, "%s%s%s", str, (flag ? ", " : ""),
  23. "up");
  24. flag = 1;
  25. }
  26. } else {
  27. snprintf(str, len, "doesn't exist");
  28. }
  29. done:
  30. return str;
  31. }
  32. /* maps */
  33. static int calc_bits_of(unsigned int t)
  34. {
  35. int b = 0;
  36. while (t) {
  37. t = t >> 1;
  38. b++;
  39. }
  40. return b;
  41. }
  42. /*
  43. * the foo_mask is the smallest value 2^n-1 that is >= foo.
  44. */
  45. static void calc_pg_masks(struct ceph_pg_pool_info *pi)
  46. {
  47. pi->pg_num_mask = (1 << calc_bits_of(le32_to_cpu(pi->v.pg_num)-1)) - 1;
  48. pi->pgp_num_mask =
  49. (1 << calc_bits_of(le32_to_cpu(pi->v.pgp_num)-1)) - 1;
  50. pi->lpg_num_mask =
  51. (1 << calc_bits_of(le32_to_cpu(pi->v.lpg_num)-1)) - 1;
  52. pi->lpgp_num_mask =
  53. (1 << calc_bits_of(le32_to_cpu(pi->v.lpgp_num)-1)) - 1;
  54. }
  55. /*
  56. * decode crush map
  57. */
  58. static int crush_decode_uniform_bucket(void **p, void *end,
  59. struct crush_bucket_uniform *b)
  60. {
  61. dout("crush_decode_uniform_bucket %p to %p\n", *p, end);
  62. ceph_decode_need(p, end, (1+b->h.size) * sizeof(u32), bad);
  63. b->item_weight = ceph_decode_32(p);
  64. return 0;
  65. bad:
  66. return -EINVAL;
  67. }
  68. static int crush_decode_list_bucket(void **p, void *end,
  69. struct crush_bucket_list *b)
  70. {
  71. int j;
  72. dout("crush_decode_list_bucket %p to %p\n", *p, end);
  73. b->item_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
  74. if (b->item_weights == NULL)
  75. return -ENOMEM;
  76. b->sum_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
  77. if (b->sum_weights == NULL)
  78. return -ENOMEM;
  79. ceph_decode_need(p, end, 2 * b->h.size * sizeof(u32), bad);
  80. for (j = 0; j < b->h.size; j++) {
  81. b->item_weights[j] = ceph_decode_32(p);
  82. b->sum_weights[j] = ceph_decode_32(p);
  83. }
  84. return 0;
  85. bad:
  86. return -EINVAL;
  87. }
  88. static int crush_decode_tree_bucket(void **p, void *end,
  89. struct crush_bucket_tree *b)
  90. {
  91. int j;
  92. dout("crush_decode_tree_bucket %p to %p\n", *p, end);
  93. ceph_decode_32_safe(p, end, b->num_nodes, bad);
  94. b->node_weights = kcalloc(b->num_nodes, sizeof(u32), GFP_NOFS);
  95. if (b->node_weights == NULL)
  96. return -ENOMEM;
  97. ceph_decode_need(p, end, b->num_nodes * sizeof(u32), bad);
  98. for (j = 0; j < b->num_nodes; j++)
  99. b->node_weights[j] = ceph_decode_32(p);
  100. return 0;
  101. bad:
  102. return -EINVAL;
  103. }
  104. static int crush_decode_straw_bucket(void **p, void *end,
  105. struct crush_bucket_straw *b)
  106. {
  107. int j;
  108. dout("crush_decode_straw_bucket %p to %p\n", *p, end);
  109. b->item_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
  110. if (b->item_weights == NULL)
  111. return -ENOMEM;
  112. b->straws = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
  113. if (b->straws == NULL)
  114. return -ENOMEM;
  115. ceph_decode_need(p, end, 2 * b->h.size * sizeof(u32), bad);
  116. for (j = 0; j < b->h.size; j++) {
  117. b->item_weights[j] = ceph_decode_32(p);
  118. b->straws[j] = ceph_decode_32(p);
  119. }
  120. return 0;
  121. bad:
  122. return -EINVAL;
  123. }
  124. static 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 int)magic, (unsigned int)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 int 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. ceph_decode_need(p, end, pglen*sizeof(u32), bad);
  785. /* removing existing (if any) */
  786. (void) __remove_pg_mapping(&map->pg_temp, pgid);
  787. /* insert */
  788. pg = kmalloc(sizeof(*pg) + sizeof(u32)*pglen, GFP_NOFS);
  789. if (!pg) {
  790. err = -ENOMEM;
  791. goto bad;
  792. }
  793. pg->pgid = pgid;
  794. pg->len = pglen;
  795. for (j = 0; j < pglen; j++)
  796. pg->osds[j] = ceph_decode_32(p);
  797. err = __insert_pg_mapping(pg, &map->pg_temp);
  798. if (err) {
  799. kfree(pg);
  800. goto bad;
  801. }
  802. dout(" added pg_temp %llx len %d\n", *(u64 *)&pgid,
  803. pglen);
  804. } else {
  805. /* remove */
  806. __remove_pg_mapping(&map->pg_temp, pgid);
  807. }
  808. }
  809. /* ignore the rest */
  810. *p = end;
  811. return map;
  812. bad:
  813. pr_err("corrupt inc osdmap epoch %d off %d (%p of %p-%p)\n",
  814. epoch, (int)(*p - start), *p, start, end);
  815. print_hex_dump(KERN_DEBUG, "osdmap: ",
  816. DUMP_PREFIX_OFFSET, 16, 1,
  817. start, end - start, true);
  818. if (newcrush)
  819. crush_destroy(newcrush);
  820. return ERR_PTR(err);
  821. }
  822. /*
  823. * calculate file layout from given offset, length.
  824. * fill in correct oid, logical length, and object extent
  825. * offset, length.
  826. *
  827. * for now, we write only a single su, until we can
  828. * pass a stride back to the caller.
  829. */
  830. void ceph_calc_file_object_mapping(struct ceph_file_layout *layout,
  831. u64 off, u64 *plen,
  832. u64 *ono,
  833. u64 *oxoff, u64 *oxlen)
  834. {
  835. u32 osize = le32_to_cpu(layout->fl_object_size);
  836. u32 su = le32_to_cpu(layout->fl_stripe_unit);
  837. u32 sc = le32_to_cpu(layout->fl_stripe_count);
  838. u32 bl, stripeno, stripepos, objsetno;
  839. u32 su_per_object;
  840. u64 t, su_offset;
  841. dout("mapping %llu~%llu osize %u fl_su %u\n", off, *plen,
  842. osize, su);
  843. su_per_object = osize / su;
  844. dout("osize %u / su %u = su_per_object %u\n", osize, su,
  845. su_per_object);
  846. BUG_ON((su & ~PAGE_MASK) != 0);
  847. /* bl = *off / su; */
  848. t = off;
  849. do_div(t, su);
  850. bl = t;
  851. dout("off %llu / su %u = bl %u\n", off, su, bl);
  852. stripeno = bl / sc;
  853. stripepos = bl % sc;
  854. objsetno = stripeno / su_per_object;
  855. *ono = objsetno * sc + stripepos;
  856. dout("objset %u * sc %u = ono %u\n", objsetno, sc, (unsigned int)*ono);
  857. /* *oxoff = *off % layout->fl_stripe_unit; # offset in su */
  858. t = off;
  859. su_offset = do_div(t, su);
  860. *oxoff = su_offset + (stripeno % su_per_object) * su;
  861. /*
  862. * Calculate the length of the extent being written to the selected
  863. * object. This is the minimum of the full length requested (plen) or
  864. * the remainder of the current stripe being written to.
  865. */
  866. *oxlen = min_t(u64, *plen, su - su_offset);
  867. *plen = *oxlen;
  868. dout(" obj extent %llu~%llu\n", *oxoff, *oxlen);
  869. }
  870. EXPORT_SYMBOL(ceph_calc_file_object_mapping);
  871. /*
  872. * calculate an object layout (i.e. pgid) from an oid,
  873. * file_layout, and osdmap
  874. */
  875. int ceph_calc_object_layout(struct ceph_object_layout *ol,
  876. const char *oid,
  877. struct ceph_file_layout *fl,
  878. struct ceph_osdmap *osdmap)
  879. {
  880. unsigned int num, num_mask;
  881. struct ceph_pg pgid;
  882. int poolid = le32_to_cpu(fl->fl_pg_pool);
  883. struct ceph_pg_pool_info *pool;
  884. unsigned int ps;
  885. BUG_ON(!osdmap);
  886. pool = __lookup_pg_pool(&osdmap->pg_pools, poolid);
  887. if (!pool)
  888. return -EIO;
  889. ps = ceph_str_hash(pool->v.object_hash, oid, strlen(oid));
  890. num = le32_to_cpu(pool->v.pg_num);
  891. num_mask = pool->pg_num_mask;
  892. pgid.ps = cpu_to_le16(ps);
  893. pgid.preferred = cpu_to_le16(-1);
  894. pgid.pool = fl->fl_pg_pool;
  895. dout("calc_object_layout '%s' pgid %d.%x\n", oid, poolid, ps);
  896. ol->ol_pgid = pgid;
  897. ol->ol_stripe_unit = fl->fl_object_stripe_unit;
  898. return 0;
  899. }
  900. EXPORT_SYMBOL(ceph_calc_object_layout);
  901. /*
  902. * Calculate raw osd vector for the given pgid. Return pointer to osd
  903. * array, or NULL on failure.
  904. */
  905. static int *calc_pg_raw(struct ceph_osdmap *osdmap, struct ceph_pg pgid,
  906. int *osds, int *num)
  907. {
  908. struct ceph_pg_mapping *pg;
  909. struct ceph_pg_pool_info *pool;
  910. int ruleno;
  911. unsigned int poolid, ps, pps, t, r;
  912. poolid = le32_to_cpu(pgid.pool);
  913. ps = le16_to_cpu(pgid.ps);
  914. pool = __lookup_pg_pool(&osdmap->pg_pools, poolid);
  915. if (!pool)
  916. return NULL;
  917. /* pg_temp? */
  918. t = ceph_stable_mod(ps, le32_to_cpu(pool->v.pg_num),
  919. pool->pgp_num_mask);
  920. pgid.ps = cpu_to_le16(t);
  921. pg = __lookup_pg_mapping(&osdmap->pg_temp, pgid);
  922. if (pg) {
  923. *num = pg->len;
  924. return pg->osds;
  925. }
  926. /* crush */
  927. ruleno = crush_find_rule(osdmap->crush, pool->v.crush_ruleset,
  928. pool->v.type, pool->v.size);
  929. if (ruleno < 0) {
  930. pr_err("no crush rule pool %d ruleset %d type %d size %d\n",
  931. poolid, pool->v.crush_ruleset, pool->v.type,
  932. pool->v.size);
  933. return NULL;
  934. }
  935. pps = ceph_stable_mod(ps,
  936. le32_to_cpu(pool->v.pgp_num),
  937. pool->pgp_num_mask);
  938. pps += poolid;
  939. r = crush_do_rule(osdmap->crush, ruleno, pps, osds,
  940. min_t(int, pool->v.size, *num),
  941. osdmap->osd_weight);
  942. if (r < 0) {
  943. pr_err("error %d from crush rule: pool %d ruleset %d type %d"
  944. " size %d\n", r, poolid, pool->v.crush_ruleset,
  945. pool->v.type, pool->v.size);
  946. return NULL;
  947. }
  948. *num = r;
  949. return osds;
  950. }
  951. /*
  952. * Return acting set for given pgid.
  953. */
  954. int ceph_calc_pg_acting(struct ceph_osdmap *osdmap, struct ceph_pg pgid,
  955. int *acting)
  956. {
  957. int rawosds[CEPH_PG_MAX_SIZE], *osds;
  958. int i, o, num = CEPH_PG_MAX_SIZE;
  959. osds = calc_pg_raw(osdmap, pgid, rawosds, &num);
  960. if (!osds)
  961. return -1;
  962. /* primary is first up osd */
  963. o = 0;
  964. for (i = 0; i < num; i++)
  965. if (ceph_osd_is_up(osdmap, osds[i]))
  966. acting[o++] = osds[i];
  967. return o;
  968. }
  969. /*
  970. * Return primary osd for given pgid, or -1 if none.
  971. */
  972. int ceph_calc_pg_primary(struct ceph_osdmap *osdmap, struct ceph_pg pgid)
  973. {
  974. int rawosds[CEPH_PG_MAX_SIZE], *osds;
  975. int i, num = CEPH_PG_MAX_SIZE;
  976. osds = calc_pg_raw(osdmap, pgid, rawosds, &num);
  977. if (!osds)
  978. return -1;
  979. /* primary is first up osd */
  980. for (i = 0; i < num; i++)
  981. if (ceph_osd_is_up(osdmap, osds[i]))
  982. return osds[i];
  983. return -1;
  984. }
  985. EXPORT_SYMBOL(ceph_calc_pg_primary);