osdmap.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755
  1. #include <linux/ceph/ceph_debug.h>
  2. #include <linux/module.h>
  3. #include <linux/slab.h>
  4. #include <asm/div64.h>
  5. #include <linux/ceph/libceph.h>
  6. #include <linux/ceph/osdmap.h>
  7. #include <linux/ceph/decode.h>
  8. #include <linux/crush/hash.h>
  9. #include <linux/crush/mapper.h>
  10. char *ceph_osdmap_state_str(char *str, int len, int state)
  11. {
  12. if (!len)
  13. return str;
  14. if ((state & CEPH_OSD_EXISTS) && (state & CEPH_OSD_UP))
  15. snprintf(str, len, "exists, up");
  16. else if (state & CEPH_OSD_EXISTS)
  17. snprintf(str, len, "exists");
  18. else if (state & CEPH_OSD_UP)
  19. snprintf(str, len, "up");
  20. else
  21. snprintf(str, len, "doesn't exist");
  22. return str;
  23. }
  24. /* maps */
  25. static int calc_bits_of(unsigned int t)
  26. {
  27. int b = 0;
  28. while (t) {
  29. t = t >> 1;
  30. b++;
  31. }
  32. return b;
  33. }
  34. /*
  35. * the foo_mask is the smallest value 2^n-1 that is >= foo.
  36. */
  37. static void calc_pg_masks(struct ceph_pg_pool_info *pi)
  38. {
  39. pi->pg_num_mask = (1 << calc_bits_of(pi->pg_num-1)) - 1;
  40. pi->pgp_num_mask = (1 << calc_bits_of(pi->pgp_num-1)) - 1;
  41. }
  42. /*
  43. * decode crush map
  44. */
  45. static int crush_decode_uniform_bucket(void **p, void *end,
  46. struct crush_bucket_uniform *b)
  47. {
  48. dout("crush_decode_uniform_bucket %p to %p\n", *p, end);
  49. ceph_decode_need(p, end, (1+b->h.size) * sizeof(u32), bad);
  50. b->item_weight = ceph_decode_32(p);
  51. return 0;
  52. bad:
  53. return -EINVAL;
  54. }
  55. static int crush_decode_list_bucket(void **p, void *end,
  56. struct crush_bucket_list *b)
  57. {
  58. int j;
  59. dout("crush_decode_list_bucket %p to %p\n", *p, end);
  60. b->item_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
  61. if (b->item_weights == NULL)
  62. return -ENOMEM;
  63. b->sum_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
  64. if (b->sum_weights == NULL)
  65. return -ENOMEM;
  66. ceph_decode_need(p, end, 2 * b->h.size * sizeof(u32), bad);
  67. for (j = 0; j < b->h.size; j++) {
  68. b->item_weights[j] = ceph_decode_32(p);
  69. b->sum_weights[j] = ceph_decode_32(p);
  70. }
  71. return 0;
  72. bad:
  73. return -EINVAL;
  74. }
  75. static int crush_decode_tree_bucket(void **p, void *end,
  76. struct crush_bucket_tree *b)
  77. {
  78. int j;
  79. dout("crush_decode_tree_bucket %p to %p\n", *p, end);
  80. ceph_decode_8_safe(p, end, b->num_nodes, bad);
  81. b->node_weights = kcalloc(b->num_nodes, sizeof(u32), GFP_NOFS);
  82. if (b->node_weights == NULL)
  83. return -ENOMEM;
  84. ceph_decode_need(p, end, b->num_nodes * sizeof(u32), bad);
  85. for (j = 0; j < b->num_nodes; j++)
  86. b->node_weights[j] = ceph_decode_32(p);
  87. return 0;
  88. bad:
  89. return -EINVAL;
  90. }
  91. static int crush_decode_straw_bucket(void **p, void *end,
  92. struct crush_bucket_straw *b)
  93. {
  94. int j;
  95. dout("crush_decode_straw_bucket %p to %p\n", *p, end);
  96. b->item_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
  97. if (b->item_weights == NULL)
  98. return -ENOMEM;
  99. b->straws = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
  100. if (b->straws == NULL)
  101. return -ENOMEM;
  102. ceph_decode_need(p, end, 2 * b->h.size * sizeof(u32), bad);
  103. for (j = 0; j < b->h.size; j++) {
  104. b->item_weights[j] = ceph_decode_32(p);
  105. b->straws[j] = ceph_decode_32(p);
  106. }
  107. return 0;
  108. bad:
  109. return -EINVAL;
  110. }
  111. static int crush_decode_straw2_bucket(void **p, void *end,
  112. struct crush_bucket_straw2 *b)
  113. {
  114. int j;
  115. dout("crush_decode_straw2_bucket %p to %p\n", *p, end);
  116. b->item_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
  117. if (b->item_weights == NULL)
  118. return -ENOMEM;
  119. ceph_decode_need(p, end, b->h.size * sizeof(u32), bad);
  120. for (j = 0; j < b->h.size; j++)
  121. b->item_weights[j] = ceph_decode_32(p);
  122. return 0;
  123. bad:
  124. return -EINVAL;
  125. }
  126. static int skip_name_map(void **p, void *end)
  127. {
  128. int len;
  129. ceph_decode_32_safe(p, end, len ,bad);
  130. while (len--) {
  131. int strlen;
  132. *p += sizeof(u32);
  133. ceph_decode_32_safe(p, end, strlen, bad);
  134. *p += strlen;
  135. }
  136. return 0;
  137. bad:
  138. return -EINVAL;
  139. }
  140. static struct crush_map *crush_decode(void *pbyval, void *end)
  141. {
  142. struct crush_map *c;
  143. int err = -EINVAL;
  144. int i, j;
  145. void **p = &pbyval;
  146. void *start = pbyval;
  147. u32 magic;
  148. u32 num_name_maps;
  149. dout("crush_decode %p to %p len %d\n", *p, end, (int)(end - *p));
  150. c = kzalloc(sizeof(*c), GFP_NOFS);
  151. if (c == NULL)
  152. return ERR_PTR(-ENOMEM);
  153. /* set tunables to default values */
  154. c->choose_local_tries = 2;
  155. c->choose_local_fallback_tries = 5;
  156. c->choose_total_tries = 19;
  157. c->chooseleaf_descend_once = 0;
  158. ceph_decode_need(p, end, 4*sizeof(u32), bad);
  159. magic = ceph_decode_32(p);
  160. if (magic != CRUSH_MAGIC) {
  161. pr_err("crush_decode magic %x != current %x\n",
  162. (unsigned int)magic, (unsigned int)CRUSH_MAGIC);
  163. goto bad;
  164. }
  165. c->max_buckets = ceph_decode_32(p);
  166. c->max_rules = ceph_decode_32(p);
  167. c->max_devices = ceph_decode_32(p);
  168. c->buckets = kcalloc(c->max_buckets, sizeof(*c->buckets), GFP_NOFS);
  169. if (c->buckets == NULL)
  170. goto badmem;
  171. c->rules = kcalloc(c->max_rules, sizeof(*c->rules), GFP_NOFS);
  172. if (c->rules == NULL)
  173. goto badmem;
  174. /* buckets */
  175. for (i = 0; i < c->max_buckets; i++) {
  176. int size = 0;
  177. u32 alg;
  178. struct crush_bucket *b;
  179. ceph_decode_32_safe(p, end, alg, bad);
  180. if (alg == 0) {
  181. c->buckets[i] = NULL;
  182. continue;
  183. }
  184. dout("crush_decode bucket %d off %x %p to %p\n",
  185. i, (int)(*p-start), *p, end);
  186. switch (alg) {
  187. case CRUSH_BUCKET_UNIFORM:
  188. size = sizeof(struct crush_bucket_uniform);
  189. break;
  190. case CRUSH_BUCKET_LIST:
  191. size = sizeof(struct crush_bucket_list);
  192. break;
  193. case CRUSH_BUCKET_TREE:
  194. size = sizeof(struct crush_bucket_tree);
  195. break;
  196. case CRUSH_BUCKET_STRAW:
  197. size = sizeof(struct crush_bucket_straw);
  198. break;
  199. case CRUSH_BUCKET_STRAW2:
  200. size = sizeof(struct crush_bucket_straw2);
  201. break;
  202. default:
  203. err = -EINVAL;
  204. goto bad;
  205. }
  206. BUG_ON(size == 0);
  207. b = c->buckets[i] = kzalloc(size, GFP_NOFS);
  208. if (b == NULL)
  209. goto badmem;
  210. ceph_decode_need(p, end, 4*sizeof(u32), bad);
  211. b->id = ceph_decode_32(p);
  212. b->type = ceph_decode_16(p);
  213. b->alg = ceph_decode_8(p);
  214. b->hash = ceph_decode_8(p);
  215. b->weight = ceph_decode_32(p);
  216. b->size = ceph_decode_32(p);
  217. dout("crush_decode bucket size %d off %x %p to %p\n",
  218. b->size, (int)(*p-start), *p, end);
  219. b->items = kcalloc(b->size, sizeof(__s32), GFP_NOFS);
  220. if (b->items == NULL)
  221. goto badmem;
  222. b->perm = kcalloc(b->size, sizeof(u32), GFP_NOFS);
  223. if (b->perm == NULL)
  224. goto badmem;
  225. b->perm_n = 0;
  226. ceph_decode_need(p, end, b->size*sizeof(u32), bad);
  227. for (j = 0; j < b->size; j++)
  228. b->items[j] = ceph_decode_32(p);
  229. switch (b->alg) {
  230. case CRUSH_BUCKET_UNIFORM:
  231. err = crush_decode_uniform_bucket(p, end,
  232. (struct crush_bucket_uniform *)b);
  233. if (err < 0)
  234. goto bad;
  235. break;
  236. case CRUSH_BUCKET_LIST:
  237. err = crush_decode_list_bucket(p, end,
  238. (struct crush_bucket_list *)b);
  239. if (err < 0)
  240. goto bad;
  241. break;
  242. case CRUSH_BUCKET_TREE:
  243. err = crush_decode_tree_bucket(p, end,
  244. (struct crush_bucket_tree *)b);
  245. if (err < 0)
  246. goto bad;
  247. break;
  248. case CRUSH_BUCKET_STRAW:
  249. err = crush_decode_straw_bucket(p, end,
  250. (struct crush_bucket_straw *)b);
  251. if (err < 0)
  252. goto bad;
  253. break;
  254. case CRUSH_BUCKET_STRAW2:
  255. err = crush_decode_straw2_bucket(p, end,
  256. (struct crush_bucket_straw2 *)b);
  257. if (err < 0)
  258. goto bad;
  259. break;
  260. }
  261. }
  262. /* rules */
  263. dout("rule vec is %p\n", c->rules);
  264. for (i = 0; i < c->max_rules; i++) {
  265. u32 yes;
  266. struct crush_rule *r;
  267. ceph_decode_32_safe(p, end, yes, bad);
  268. if (!yes) {
  269. dout("crush_decode NO rule %d off %x %p to %p\n",
  270. i, (int)(*p-start), *p, end);
  271. c->rules[i] = NULL;
  272. continue;
  273. }
  274. dout("crush_decode rule %d off %x %p to %p\n",
  275. i, (int)(*p-start), *p, end);
  276. /* len */
  277. ceph_decode_32_safe(p, end, yes, bad);
  278. #if BITS_PER_LONG == 32
  279. err = -EINVAL;
  280. if (yes > (ULONG_MAX - sizeof(*r))
  281. / sizeof(struct crush_rule_step))
  282. goto bad;
  283. #endif
  284. r = c->rules[i] = kmalloc(sizeof(*r) +
  285. yes*sizeof(struct crush_rule_step),
  286. GFP_NOFS);
  287. if (r == NULL)
  288. goto badmem;
  289. dout(" rule %d is at %p\n", i, r);
  290. r->len = yes;
  291. ceph_decode_copy_safe(p, end, &r->mask, 4, bad); /* 4 u8's */
  292. ceph_decode_need(p, end, r->len*3*sizeof(u32), bad);
  293. for (j = 0; j < r->len; j++) {
  294. r->steps[j].op = ceph_decode_32(p);
  295. r->steps[j].arg1 = ceph_decode_32(p);
  296. r->steps[j].arg2 = ceph_decode_32(p);
  297. }
  298. }
  299. /* ignore trailing name maps. */
  300. for (num_name_maps = 0; num_name_maps < 3; num_name_maps++) {
  301. err = skip_name_map(p, end);
  302. if (err < 0)
  303. goto done;
  304. }
  305. /* tunables */
  306. ceph_decode_need(p, end, 3*sizeof(u32), done);
  307. c->choose_local_tries = ceph_decode_32(p);
  308. c->choose_local_fallback_tries = ceph_decode_32(p);
  309. c->choose_total_tries = ceph_decode_32(p);
  310. dout("crush decode tunable choose_local_tries = %d",
  311. c->choose_local_tries);
  312. dout("crush decode tunable choose_local_fallback_tries = %d",
  313. c->choose_local_fallback_tries);
  314. dout("crush decode tunable choose_total_tries = %d",
  315. c->choose_total_tries);
  316. ceph_decode_need(p, end, sizeof(u32), done);
  317. c->chooseleaf_descend_once = ceph_decode_32(p);
  318. dout("crush decode tunable chooseleaf_descend_once = %d",
  319. c->chooseleaf_descend_once);
  320. ceph_decode_need(p, end, sizeof(u8), done);
  321. c->chooseleaf_vary_r = ceph_decode_8(p);
  322. dout("crush decode tunable chooseleaf_vary_r = %d",
  323. c->chooseleaf_vary_r);
  324. done:
  325. dout("crush_decode success\n");
  326. return c;
  327. badmem:
  328. err = -ENOMEM;
  329. bad:
  330. dout("crush_decode fail %d\n", err);
  331. crush_destroy(c);
  332. return ERR_PTR(err);
  333. }
  334. /*
  335. * rbtree of pg_mapping for handling pg_temp (explicit mapping of pgid
  336. * to a set of osds) and primary_temp (explicit primary setting)
  337. */
  338. static int pgid_cmp(struct ceph_pg l, struct ceph_pg r)
  339. {
  340. if (l.pool < r.pool)
  341. return -1;
  342. if (l.pool > r.pool)
  343. return 1;
  344. if (l.seed < r.seed)
  345. return -1;
  346. if (l.seed > r.seed)
  347. return 1;
  348. return 0;
  349. }
  350. static int __insert_pg_mapping(struct ceph_pg_mapping *new,
  351. struct rb_root *root)
  352. {
  353. struct rb_node **p = &root->rb_node;
  354. struct rb_node *parent = NULL;
  355. struct ceph_pg_mapping *pg = NULL;
  356. int c;
  357. dout("__insert_pg_mapping %llx %p\n", *(u64 *)&new->pgid, new);
  358. while (*p) {
  359. parent = *p;
  360. pg = rb_entry(parent, struct ceph_pg_mapping, node);
  361. c = pgid_cmp(new->pgid, pg->pgid);
  362. if (c < 0)
  363. p = &(*p)->rb_left;
  364. else if (c > 0)
  365. p = &(*p)->rb_right;
  366. else
  367. return -EEXIST;
  368. }
  369. rb_link_node(&new->node, parent, p);
  370. rb_insert_color(&new->node, root);
  371. return 0;
  372. }
  373. static struct ceph_pg_mapping *__lookup_pg_mapping(struct rb_root *root,
  374. struct ceph_pg pgid)
  375. {
  376. struct rb_node *n = root->rb_node;
  377. struct ceph_pg_mapping *pg;
  378. int c;
  379. while (n) {
  380. pg = rb_entry(n, struct ceph_pg_mapping, node);
  381. c = pgid_cmp(pgid, pg->pgid);
  382. if (c < 0) {
  383. n = n->rb_left;
  384. } else if (c > 0) {
  385. n = n->rb_right;
  386. } else {
  387. dout("__lookup_pg_mapping %lld.%x got %p\n",
  388. pgid.pool, pgid.seed, pg);
  389. return pg;
  390. }
  391. }
  392. return NULL;
  393. }
  394. static int __remove_pg_mapping(struct rb_root *root, struct ceph_pg pgid)
  395. {
  396. struct ceph_pg_mapping *pg = __lookup_pg_mapping(root, pgid);
  397. if (pg) {
  398. dout("__remove_pg_mapping %lld.%x %p\n", pgid.pool, pgid.seed,
  399. pg);
  400. rb_erase(&pg->node, root);
  401. kfree(pg);
  402. return 0;
  403. }
  404. dout("__remove_pg_mapping %lld.%x dne\n", pgid.pool, pgid.seed);
  405. return -ENOENT;
  406. }
  407. /*
  408. * rbtree of pg pool info
  409. */
  410. static int __insert_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *new)
  411. {
  412. struct rb_node **p = &root->rb_node;
  413. struct rb_node *parent = NULL;
  414. struct ceph_pg_pool_info *pi = NULL;
  415. while (*p) {
  416. parent = *p;
  417. pi = rb_entry(parent, struct ceph_pg_pool_info, node);
  418. if (new->id < pi->id)
  419. p = &(*p)->rb_left;
  420. else if (new->id > pi->id)
  421. p = &(*p)->rb_right;
  422. else
  423. return -EEXIST;
  424. }
  425. rb_link_node(&new->node, parent, p);
  426. rb_insert_color(&new->node, root);
  427. return 0;
  428. }
  429. static struct ceph_pg_pool_info *__lookup_pg_pool(struct rb_root *root, u64 id)
  430. {
  431. struct ceph_pg_pool_info *pi;
  432. struct rb_node *n = root->rb_node;
  433. while (n) {
  434. pi = rb_entry(n, struct ceph_pg_pool_info, node);
  435. if (id < pi->id)
  436. n = n->rb_left;
  437. else if (id > pi->id)
  438. n = n->rb_right;
  439. else
  440. return pi;
  441. }
  442. return NULL;
  443. }
  444. struct ceph_pg_pool_info *ceph_pg_pool_by_id(struct ceph_osdmap *map, u64 id)
  445. {
  446. return __lookup_pg_pool(&map->pg_pools, id);
  447. }
  448. const char *ceph_pg_pool_name_by_id(struct ceph_osdmap *map, u64 id)
  449. {
  450. struct ceph_pg_pool_info *pi;
  451. if (id == CEPH_NOPOOL)
  452. return NULL;
  453. if (WARN_ON_ONCE(id > (u64) INT_MAX))
  454. return NULL;
  455. pi = __lookup_pg_pool(&map->pg_pools, (int) id);
  456. return pi ? pi->name : NULL;
  457. }
  458. EXPORT_SYMBOL(ceph_pg_pool_name_by_id);
  459. int ceph_pg_poolid_by_name(struct ceph_osdmap *map, const char *name)
  460. {
  461. struct rb_node *rbp;
  462. for (rbp = rb_first(&map->pg_pools); rbp; rbp = rb_next(rbp)) {
  463. struct ceph_pg_pool_info *pi =
  464. rb_entry(rbp, struct ceph_pg_pool_info, node);
  465. if (pi->name && strcmp(pi->name, name) == 0)
  466. return pi->id;
  467. }
  468. return -ENOENT;
  469. }
  470. EXPORT_SYMBOL(ceph_pg_poolid_by_name);
  471. static void __remove_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *pi)
  472. {
  473. rb_erase(&pi->node, root);
  474. kfree(pi->name);
  475. kfree(pi);
  476. }
  477. static int decode_pool(void **p, void *end, struct ceph_pg_pool_info *pi)
  478. {
  479. u8 ev, cv;
  480. unsigned len, num;
  481. void *pool_end;
  482. ceph_decode_need(p, end, 2 + 4, bad);
  483. ev = ceph_decode_8(p); /* encoding version */
  484. cv = ceph_decode_8(p); /* compat version */
  485. if (ev < 5) {
  486. pr_warn("got v %d < 5 cv %d of ceph_pg_pool\n", ev, cv);
  487. return -EINVAL;
  488. }
  489. if (cv > 9) {
  490. pr_warn("got v %d cv %d > 9 of ceph_pg_pool\n", ev, cv);
  491. return -EINVAL;
  492. }
  493. len = ceph_decode_32(p);
  494. ceph_decode_need(p, end, len, bad);
  495. pool_end = *p + len;
  496. pi->type = ceph_decode_8(p);
  497. pi->size = ceph_decode_8(p);
  498. pi->crush_ruleset = ceph_decode_8(p);
  499. pi->object_hash = ceph_decode_8(p);
  500. pi->pg_num = ceph_decode_32(p);
  501. pi->pgp_num = ceph_decode_32(p);
  502. *p += 4 + 4; /* skip lpg* */
  503. *p += 4; /* skip last_change */
  504. *p += 8 + 4; /* skip snap_seq, snap_epoch */
  505. /* skip snaps */
  506. num = ceph_decode_32(p);
  507. while (num--) {
  508. *p += 8; /* snapid key */
  509. *p += 1 + 1; /* versions */
  510. len = ceph_decode_32(p);
  511. *p += len;
  512. }
  513. /* skip removed_snaps */
  514. num = ceph_decode_32(p);
  515. *p += num * (8 + 8);
  516. *p += 8; /* skip auid */
  517. pi->flags = ceph_decode_64(p);
  518. *p += 4; /* skip crash_replay_interval */
  519. if (ev >= 7)
  520. *p += 1; /* skip min_size */
  521. if (ev >= 8)
  522. *p += 8 + 8; /* skip quota_max_* */
  523. if (ev >= 9) {
  524. /* skip tiers */
  525. num = ceph_decode_32(p);
  526. *p += num * 8;
  527. *p += 8; /* skip tier_of */
  528. *p += 1; /* skip cache_mode */
  529. pi->read_tier = ceph_decode_64(p);
  530. pi->write_tier = ceph_decode_64(p);
  531. } else {
  532. pi->read_tier = -1;
  533. pi->write_tier = -1;
  534. }
  535. /* ignore the rest */
  536. *p = pool_end;
  537. calc_pg_masks(pi);
  538. return 0;
  539. bad:
  540. return -EINVAL;
  541. }
  542. static int decode_pool_names(void **p, void *end, struct ceph_osdmap *map)
  543. {
  544. struct ceph_pg_pool_info *pi;
  545. u32 num, len;
  546. u64 pool;
  547. ceph_decode_32_safe(p, end, num, bad);
  548. dout(" %d pool names\n", num);
  549. while (num--) {
  550. ceph_decode_64_safe(p, end, pool, bad);
  551. ceph_decode_32_safe(p, end, len, bad);
  552. dout(" pool %llu len %d\n", pool, len);
  553. ceph_decode_need(p, end, len, bad);
  554. pi = __lookup_pg_pool(&map->pg_pools, pool);
  555. if (pi) {
  556. char *name = kstrndup(*p, len, GFP_NOFS);
  557. if (!name)
  558. return -ENOMEM;
  559. kfree(pi->name);
  560. pi->name = name;
  561. dout(" name is %s\n", pi->name);
  562. }
  563. *p += len;
  564. }
  565. return 0;
  566. bad:
  567. return -EINVAL;
  568. }
  569. /*
  570. * osd map
  571. */
  572. void ceph_osdmap_destroy(struct ceph_osdmap *map)
  573. {
  574. dout("osdmap_destroy %p\n", map);
  575. if (map->crush)
  576. crush_destroy(map->crush);
  577. while (!RB_EMPTY_ROOT(&map->pg_temp)) {
  578. struct ceph_pg_mapping *pg =
  579. rb_entry(rb_first(&map->pg_temp),
  580. struct ceph_pg_mapping, node);
  581. rb_erase(&pg->node, &map->pg_temp);
  582. kfree(pg);
  583. }
  584. while (!RB_EMPTY_ROOT(&map->primary_temp)) {
  585. struct ceph_pg_mapping *pg =
  586. rb_entry(rb_first(&map->primary_temp),
  587. struct ceph_pg_mapping, node);
  588. rb_erase(&pg->node, &map->primary_temp);
  589. kfree(pg);
  590. }
  591. while (!RB_EMPTY_ROOT(&map->pg_pools)) {
  592. struct ceph_pg_pool_info *pi =
  593. rb_entry(rb_first(&map->pg_pools),
  594. struct ceph_pg_pool_info, node);
  595. __remove_pg_pool(&map->pg_pools, pi);
  596. }
  597. kfree(map->osd_state);
  598. kfree(map->osd_weight);
  599. kfree(map->osd_addr);
  600. kfree(map->osd_primary_affinity);
  601. kfree(map);
  602. }
  603. /*
  604. * Adjust max_osd value, (re)allocate arrays.
  605. *
  606. * The new elements are properly initialized.
  607. */
  608. static int osdmap_set_max_osd(struct ceph_osdmap *map, int max)
  609. {
  610. u8 *state;
  611. u32 *weight;
  612. struct ceph_entity_addr *addr;
  613. int i;
  614. state = krealloc(map->osd_state, max*sizeof(*state), GFP_NOFS);
  615. if (!state)
  616. return -ENOMEM;
  617. map->osd_state = state;
  618. weight = krealloc(map->osd_weight, max*sizeof(*weight), GFP_NOFS);
  619. if (!weight)
  620. return -ENOMEM;
  621. map->osd_weight = weight;
  622. addr = krealloc(map->osd_addr, max*sizeof(*addr), GFP_NOFS);
  623. if (!addr)
  624. return -ENOMEM;
  625. map->osd_addr = addr;
  626. for (i = map->max_osd; i < max; i++) {
  627. map->osd_state[i] = 0;
  628. map->osd_weight[i] = CEPH_OSD_OUT;
  629. memset(map->osd_addr + i, 0, sizeof(*map->osd_addr));
  630. }
  631. if (map->osd_primary_affinity) {
  632. u32 *affinity;
  633. affinity = krealloc(map->osd_primary_affinity,
  634. max*sizeof(*affinity), GFP_NOFS);
  635. if (!affinity)
  636. return -ENOMEM;
  637. map->osd_primary_affinity = affinity;
  638. for (i = map->max_osd; i < max; i++)
  639. map->osd_primary_affinity[i] =
  640. CEPH_OSD_DEFAULT_PRIMARY_AFFINITY;
  641. }
  642. map->max_osd = max;
  643. return 0;
  644. }
  645. #define OSDMAP_WRAPPER_COMPAT_VER 7
  646. #define OSDMAP_CLIENT_DATA_COMPAT_VER 1
  647. /*
  648. * Return 0 or error. On success, *v is set to 0 for old (v6) osdmaps,
  649. * to struct_v of the client_data section for new (v7 and above)
  650. * osdmaps.
  651. */
  652. static int get_osdmap_client_data_v(void **p, void *end,
  653. const char *prefix, u8 *v)
  654. {
  655. u8 struct_v;
  656. ceph_decode_8_safe(p, end, struct_v, e_inval);
  657. if (struct_v >= 7) {
  658. u8 struct_compat;
  659. ceph_decode_8_safe(p, end, struct_compat, e_inval);
  660. if (struct_compat > OSDMAP_WRAPPER_COMPAT_VER) {
  661. pr_warn("got v %d cv %d > %d of %s ceph_osdmap\n",
  662. struct_v, struct_compat,
  663. OSDMAP_WRAPPER_COMPAT_VER, prefix);
  664. return -EINVAL;
  665. }
  666. *p += 4; /* ignore wrapper struct_len */
  667. ceph_decode_8_safe(p, end, struct_v, e_inval);
  668. ceph_decode_8_safe(p, end, struct_compat, e_inval);
  669. if (struct_compat > OSDMAP_CLIENT_DATA_COMPAT_VER) {
  670. pr_warn("got v %d cv %d > %d of %s ceph_osdmap client data\n",
  671. struct_v, struct_compat,
  672. OSDMAP_CLIENT_DATA_COMPAT_VER, prefix);
  673. return -EINVAL;
  674. }
  675. *p += 4; /* ignore client data struct_len */
  676. } else {
  677. u16 version;
  678. *p -= 1;
  679. ceph_decode_16_safe(p, end, version, e_inval);
  680. if (version < 6) {
  681. pr_warn("got v %d < 6 of %s ceph_osdmap\n",
  682. version, prefix);
  683. return -EINVAL;
  684. }
  685. /* old osdmap enconding */
  686. struct_v = 0;
  687. }
  688. *v = struct_v;
  689. return 0;
  690. e_inval:
  691. return -EINVAL;
  692. }
  693. static int __decode_pools(void **p, void *end, struct ceph_osdmap *map,
  694. bool incremental)
  695. {
  696. u32 n;
  697. ceph_decode_32_safe(p, end, n, e_inval);
  698. while (n--) {
  699. struct ceph_pg_pool_info *pi;
  700. u64 pool;
  701. int ret;
  702. ceph_decode_64_safe(p, end, pool, e_inval);
  703. pi = __lookup_pg_pool(&map->pg_pools, pool);
  704. if (!incremental || !pi) {
  705. pi = kzalloc(sizeof(*pi), GFP_NOFS);
  706. if (!pi)
  707. return -ENOMEM;
  708. pi->id = pool;
  709. ret = __insert_pg_pool(&map->pg_pools, pi);
  710. if (ret) {
  711. kfree(pi);
  712. return ret;
  713. }
  714. }
  715. ret = decode_pool(p, end, pi);
  716. if (ret)
  717. return ret;
  718. }
  719. return 0;
  720. e_inval:
  721. return -EINVAL;
  722. }
  723. static int decode_pools(void **p, void *end, struct ceph_osdmap *map)
  724. {
  725. return __decode_pools(p, end, map, false);
  726. }
  727. static int decode_new_pools(void **p, void *end, struct ceph_osdmap *map)
  728. {
  729. return __decode_pools(p, end, map, true);
  730. }
  731. static int __decode_pg_temp(void **p, void *end, struct ceph_osdmap *map,
  732. bool incremental)
  733. {
  734. u32 n;
  735. ceph_decode_32_safe(p, end, n, e_inval);
  736. while (n--) {
  737. struct ceph_pg pgid;
  738. u32 len, i;
  739. int ret;
  740. ret = ceph_decode_pgid(p, end, &pgid);
  741. if (ret)
  742. return ret;
  743. ceph_decode_32_safe(p, end, len, e_inval);
  744. ret = __remove_pg_mapping(&map->pg_temp, pgid);
  745. BUG_ON(!incremental && ret != -ENOENT);
  746. if (!incremental || len > 0) {
  747. struct ceph_pg_mapping *pg;
  748. ceph_decode_need(p, end, len*sizeof(u32), e_inval);
  749. if (len > (UINT_MAX - sizeof(*pg)) / sizeof(u32))
  750. return -EINVAL;
  751. pg = kzalloc(sizeof(*pg) + len*sizeof(u32), GFP_NOFS);
  752. if (!pg)
  753. return -ENOMEM;
  754. pg->pgid = pgid;
  755. pg->pg_temp.len = len;
  756. for (i = 0; i < len; i++)
  757. pg->pg_temp.osds[i] = ceph_decode_32(p);
  758. ret = __insert_pg_mapping(pg, &map->pg_temp);
  759. if (ret) {
  760. kfree(pg);
  761. return ret;
  762. }
  763. }
  764. }
  765. return 0;
  766. e_inval:
  767. return -EINVAL;
  768. }
  769. static int decode_pg_temp(void **p, void *end, struct ceph_osdmap *map)
  770. {
  771. return __decode_pg_temp(p, end, map, false);
  772. }
  773. static int decode_new_pg_temp(void **p, void *end, struct ceph_osdmap *map)
  774. {
  775. return __decode_pg_temp(p, end, map, true);
  776. }
  777. static int __decode_primary_temp(void **p, void *end, struct ceph_osdmap *map,
  778. bool incremental)
  779. {
  780. u32 n;
  781. ceph_decode_32_safe(p, end, n, e_inval);
  782. while (n--) {
  783. struct ceph_pg pgid;
  784. u32 osd;
  785. int ret;
  786. ret = ceph_decode_pgid(p, end, &pgid);
  787. if (ret)
  788. return ret;
  789. ceph_decode_32_safe(p, end, osd, e_inval);
  790. ret = __remove_pg_mapping(&map->primary_temp, pgid);
  791. BUG_ON(!incremental && ret != -ENOENT);
  792. if (!incremental || osd != (u32)-1) {
  793. struct ceph_pg_mapping *pg;
  794. pg = kzalloc(sizeof(*pg), GFP_NOFS);
  795. if (!pg)
  796. return -ENOMEM;
  797. pg->pgid = pgid;
  798. pg->primary_temp.osd = osd;
  799. ret = __insert_pg_mapping(pg, &map->primary_temp);
  800. if (ret) {
  801. kfree(pg);
  802. return ret;
  803. }
  804. }
  805. }
  806. return 0;
  807. e_inval:
  808. return -EINVAL;
  809. }
  810. static int decode_primary_temp(void **p, void *end, struct ceph_osdmap *map)
  811. {
  812. return __decode_primary_temp(p, end, map, false);
  813. }
  814. static int decode_new_primary_temp(void **p, void *end,
  815. struct ceph_osdmap *map)
  816. {
  817. return __decode_primary_temp(p, end, map, true);
  818. }
  819. u32 ceph_get_primary_affinity(struct ceph_osdmap *map, int osd)
  820. {
  821. BUG_ON(osd >= map->max_osd);
  822. if (!map->osd_primary_affinity)
  823. return CEPH_OSD_DEFAULT_PRIMARY_AFFINITY;
  824. return map->osd_primary_affinity[osd];
  825. }
  826. static int set_primary_affinity(struct ceph_osdmap *map, int osd, u32 aff)
  827. {
  828. BUG_ON(osd >= map->max_osd);
  829. if (!map->osd_primary_affinity) {
  830. int i;
  831. map->osd_primary_affinity = kmalloc(map->max_osd*sizeof(u32),
  832. GFP_NOFS);
  833. if (!map->osd_primary_affinity)
  834. return -ENOMEM;
  835. for (i = 0; i < map->max_osd; i++)
  836. map->osd_primary_affinity[i] =
  837. CEPH_OSD_DEFAULT_PRIMARY_AFFINITY;
  838. }
  839. map->osd_primary_affinity[osd] = aff;
  840. return 0;
  841. }
  842. static int decode_primary_affinity(void **p, void *end,
  843. struct ceph_osdmap *map)
  844. {
  845. u32 len, i;
  846. ceph_decode_32_safe(p, end, len, e_inval);
  847. if (len == 0) {
  848. kfree(map->osd_primary_affinity);
  849. map->osd_primary_affinity = NULL;
  850. return 0;
  851. }
  852. if (len != map->max_osd)
  853. goto e_inval;
  854. ceph_decode_need(p, end, map->max_osd*sizeof(u32), e_inval);
  855. for (i = 0; i < map->max_osd; i++) {
  856. int ret;
  857. ret = set_primary_affinity(map, i, ceph_decode_32(p));
  858. if (ret)
  859. return ret;
  860. }
  861. return 0;
  862. e_inval:
  863. return -EINVAL;
  864. }
  865. static int decode_new_primary_affinity(void **p, void *end,
  866. struct ceph_osdmap *map)
  867. {
  868. u32 n;
  869. ceph_decode_32_safe(p, end, n, e_inval);
  870. while (n--) {
  871. u32 osd, aff;
  872. int ret;
  873. ceph_decode_32_safe(p, end, osd, e_inval);
  874. ceph_decode_32_safe(p, end, aff, e_inval);
  875. ret = set_primary_affinity(map, osd, aff);
  876. if (ret)
  877. return ret;
  878. pr_info("osd%d primary-affinity 0x%x\n", osd, aff);
  879. }
  880. return 0;
  881. e_inval:
  882. return -EINVAL;
  883. }
  884. /*
  885. * decode a full map.
  886. */
  887. static int osdmap_decode(void **p, void *end, struct ceph_osdmap *map)
  888. {
  889. u8 struct_v;
  890. u32 epoch = 0;
  891. void *start = *p;
  892. u32 max;
  893. u32 len, i;
  894. int err;
  895. dout("%s %p to %p len %d\n", __func__, *p, end, (int)(end - *p));
  896. err = get_osdmap_client_data_v(p, end, "full", &struct_v);
  897. if (err)
  898. goto bad;
  899. /* fsid, epoch, created, modified */
  900. ceph_decode_need(p, end, sizeof(map->fsid) + sizeof(u32) +
  901. sizeof(map->created) + sizeof(map->modified), e_inval);
  902. ceph_decode_copy(p, &map->fsid, sizeof(map->fsid));
  903. epoch = map->epoch = ceph_decode_32(p);
  904. ceph_decode_copy(p, &map->created, sizeof(map->created));
  905. ceph_decode_copy(p, &map->modified, sizeof(map->modified));
  906. /* pools */
  907. err = decode_pools(p, end, map);
  908. if (err)
  909. goto bad;
  910. /* pool_name */
  911. err = decode_pool_names(p, end, map);
  912. if (err)
  913. goto bad;
  914. ceph_decode_32_safe(p, end, map->pool_max, e_inval);
  915. ceph_decode_32_safe(p, end, map->flags, e_inval);
  916. /* max_osd */
  917. ceph_decode_32_safe(p, end, max, e_inval);
  918. /* (re)alloc osd arrays */
  919. err = osdmap_set_max_osd(map, max);
  920. if (err)
  921. goto bad;
  922. /* osd_state, osd_weight, osd_addrs->client_addr */
  923. ceph_decode_need(p, end, 3*sizeof(u32) +
  924. map->max_osd*(1 + sizeof(*map->osd_weight) +
  925. sizeof(*map->osd_addr)), e_inval);
  926. if (ceph_decode_32(p) != map->max_osd)
  927. goto e_inval;
  928. ceph_decode_copy(p, map->osd_state, map->max_osd);
  929. if (ceph_decode_32(p) != map->max_osd)
  930. goto e_inval;
  931. for (i = 0; i < map->max_osd; i++)
  932. map->osd_weight[i] = ceph_decode_32(p);
  933. if (ceph_decode_32(p) != map->max_osd)
  934. goto e_inval;
  935. ceph_decode_copy(p, map->osd_addr, map->max_osd*sizeof(*map->osd_addr));
  936. for (i = 0; i < map->max_osd; i++)
  937. ceph_decode_addr(&map->osd_addr[i]);
  938. /* pg_temp */
  939. err = decode_pg_temp(p, end, map);
  940. if (err)
  941. goto bad;
  942. /* primary_temp */
  943. if (struct_v >= 1) {
  944. err = decode_primary_temp(p, end, map);
  945. if (err)
  946. goto bad;
  947. }
  948. /* primary_affinity */
  949. if (struct_v >= 2) {
  950. err = decode_primary_affinity(p, end, map);
  951. if (err)
  952. goto bad;
  953. } else {
  954. /* XXX can this happen? */
  955. kfree(map->osd_primary_affinity);
  956. map->osd_primary_affinity = NULL;
  957. }
  958. /* crush */
  959. ceph_decode_32_safe(p, end, len, e_inval);
  960. map->crush = crush_decode(*p, min(*p + len, end));
  961. if (IS_ERR(map->crush)) {
  962. err = PTR_ERR(map->crush);
  963. map->crush = NULL;
  964. goto bad;
  965. }
  966. *p += len;
  967. /* ignore the rest */
  968. *p = end;
  969. dout("full osdmap epoch %d max_osd %d\n", map->epoch, map->max_osd);
  970. return 0;
  971. e_inval:
  972. err = -EINVAL;
  973. bad:
  974. pr_err("corrupt full osdmap (%d) epoch %d off %d (%p of %p-%p)\n",
  975. err, epoch, (int)(*p - start), *p, start, end);
  976. print_hex_dump(KERN_DEBUG, "osdmap: ",
  977. DUMP_PREFIX_OFFSET, 16, 1,
  978. start, end - start, true);
  979. return err;
  980. }
  981. /*
  982. * Allocate and decode a full map.
  983. */
  984. struct ceph_osdmap *ceph_osdmap_decode(void **p, void *end)
  985. {
  986. struct ceph_osdmap *map;
  987. int ret;
  988. map = kzalloc(sizeof(*map), GFP_NOFS);
  989. if (!map)
  990. return ERR_PTR(-ENOMEM);
  991. map->pg_temp = RB_ROOT;
  992. map->primary_temp = RB_ROOT;
  993. mutex_init(&map->crush_scratch_mutex);
  994. ret = osdmap_decode(p, end, map);
  995. if (ret) {
  996. ceph_osdmap_destroy(map);
  997. return ERR_PTR(ret);
  998. }
  999. return map;
  1000. }
  1001. /*
  1002. * decode and apply an incremental map update.
  1003. */
  1004. struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end,
  1005. struct ceph_osdmap *map,
  1006. struct ceph_messenger *msgr)
  1007. {
  1008. struct crush_map *newcrush = NULL;
  1009. struct ceph_fsid fsid;
  1010. u32 epoch = 0;
  1011. struct ceph_timespec modified;
  1012. s32 len;
  1013. u64 pool;
  1014. __s64 new_pool_max;
  1015. __s32 new_flags, max;
  1016. void *start = *p;
  1017. int err;
  1018. u8 struct_v;
  1019. dout("%s %p to %p len %d\n", __func__, *p, end, (int)(end - *p));
  1020. err = get_osdmap_client_data_v(p, end, "inc", &struct_v);
  1021. if (err)
  1022. goto bad;
  1023. /* fsid, epoch, modified, new_pool_max, new_flags */
  1024. ceph_decode_need(p, end, sizeof(fsid) + sizeof(u32) + sizeof(modified) +
  1025. sizeof(u64) + sizeof(u32), e_inval);
  1026. ceph_decode_copy(p, &fsid, sizeof(fsid));
  1027. epoch = ceph_decode_32(p);
  1028. BUG_ON(epoch != map->epoch+1);
  1029. ceph_decode_copy(p, &modified, sizeof(modified));
  1030. new_pool_max = ceph_decode_64(p);
  1031. new_flags = ceph_decode_32(p);
  1032. /* full map? */
  1033. ceph_decode_32_safe(p, end, len, e_inval);
  1034. if (len > 0) {
  1035. dout("apply_incremental full map len %d, %p to %p\n",
  1036. len, *p, end);
  1037. return ceph_osdmap_decode(p, min(*p+len, end));
  1038. }
  1039. /* new crush? */
  1040. ceph_decode_32_safe(p, end, len, e_inval);
  1041. if (len > 0) {
  1042. newcrush = crush_decode(*p, min(*p+len, end));
  1043. if (IS_ERR(newcrush)) {
  1044. err = PTR_ERR(newcrush);
  1045. newcrush = NULL;
  1046. goto bad;
  1047. }
  1048. *p += len;
  1049. }
  1050. /* new flags? */
  1051. if (new_flags >= 0)
  1052. map->flags = new_flags;
  1053. if (new_pool_max >= 0)
  1054. map->pool_max = new_pool_max;
  1055. /* new max? */
  1056. ceph_decode_32_safe(p, end, max, e_inval);
  1057. if (max >= 0) {
  1058. err = osdmap_set_max_osd(map, max);
  1059. if (err)
  1060. goto bad;
  1061. }
  1062. map->epoch++;
  1063. map->modified = modified;
  1064. if (newcrush) {
  1065. if (map->crush)
  1066. crush_destroy(map->crush);
  1067. map->crush = newcrush;
  1068. newcrush = NULL;
  1069. }
  1070. /* new_pools */
  1071. err = decode_new_pools(p, end, map);
  1072. if (err)
  1073. goto bad;
  1074. /* new_pool_names */
  1075. err = decode_pool_names(p, end, map);
  1076. if (err)
  1077. goto bad;
  1078. /* old_pool */
  1079. ceph_decode_32_safe(p, end, len, e_inval);
  1080. while (len--) {
  1081. struct ceph_pg_pool_info *pi;
  1082. ceph_decode_64_safe(p, end, pool, e_inval);
  1083. pi = __lookup_pg_pool(&map->pg_pools, pool);
  1084. if (pi)
  1085. __remove_pg_pool(&map->pg_pools, pi);
  1086. }
  1087. /* new_up */
  1088. ceph_decode_32_safe(p, end, len, e_inval);
  1089. while (len--) {
  1090. u32 osd;
  1091. struct ceph_entity_addr addr;
  1092. ceph_decode_32_safe(p, end, osd, e_inval);
  1093. ceph_decode_copy_safe(p, end, &addr, sizeof(addr), e_inval);
  1094. ceph_decode_addr(&addr);
  1095. pr_info("osd%d up\n", osd);
  1096. BUG_ON(osd >= map->max_osd);
  1097. map->osd_state[osd] |= CEPH_OSD_UP;
  1098. map->osd_addr[osd] = addr;
  1099. }
  1100. /* new_state */
  1101. ceph_decode_32_safe(p, end, len, e_inval);
  1102. while (len--) {
  1103. u32 osd;
  1104. u8 xorstate;
  1105. ceph_decode_32_safe(p, end, osd, e_inval);
  1106. xorstate = **(u8 **)p;
  1107. (*p)++; /* clean flag */
  1108. if (xorstate == 0)
  1109. xorstate = CEPH_OSD_UP;
  1110. if (xorstate & CEPH_OSD_UP)
  1111. pr_info("osd%d down\n", osd);
  1112. if (osd < map->max_osd)
  1113. map->osd_state[osd] ^= xorstate;
  1114. }
  1115. /* new_weight */
  1116. ceph_decode_32_safe(p, end, len, e_inval);
  1117. while (len--) {
  1118. u32 osd, off;
  1119. ceph_decode_need(p, end, sizeof(u32)*2, e_inval);
  1120. osd = ceph_decode_32(p);
  1121. off = ceph_decode_32(p);
  1122. pr_info("osd%d weight 0x%x %s\n", osd, off,
  1123. off == CEPH_OSD_IN ? "(in)" :
  1124. (off == CEPH_OSD_OUT ? "(out)" : ""));
  1125. if (osd < map->max_osd)
  1126. map->osd_weight[osd] = off;
  1127. }
  1128. /* new_pg_temp */
  1129. err = decode_new_pg_temp(p, end, map);
  1130. if (err)
  1131. goto bad;
  1132. /* new_primary_temp */
  1133. if (struct_v >= 1) {
  1134. err = decode_new_primary_temp(p, end, map);
  1135. if (err)
  1136. goto bad;
  1137. }
  1138. /* new_primary_affinity */
  1139. if (struct_v >= 2) {
  1140. err = decode_new_primary_affinity(p, end, map);
  1141. if (err)
  1142. goto bad;
  1143. }
  1144. /* ignore the rest */
  1145. *p = end;
  1146. dout("inc osdmap epoch %d max_osd %d\n", map->epoch, map->max_osd);
  1147. return map;
  1148. e_inval:
  1149. err = -EINVAL;
  1150. bad:
  1151. pr_err("corrupt inc osdmap (%d) epoch %d off %d (%p of %p-%p)\n",
  1152. err, epoch, (int)(*p - start), *p, start, end);
  1153. print_hex_dump(KERN_DEBUG, "osdmap: ",
  1154. DUMP_PREFIX_OFFSET, 16, 1,
  1155. start, end - start, true);
  1156. if (newcrush)
  1157. crush_destroy(newcrush);
  1158. return ERR_PTR(err);
  1159. }
  1160. /*
  1161. * calculate file layout from given offset, length.
  1162. * fill in correct oid, logical length, and object extent
  1163. * offset, length.
  1164. *
  1165. * for now, we write only a single su, until we can
  1166. * pass a stride back to the caller.
  1167. */
  1168. int ceph_calc_file_object_mapping(struct ceph_file_layout *layout,
  1169. u64 off, u64 len,
  1170. u64 *ono,
  1171. u64 *oxoff, u64 *oxlen)
  1172. {
  1173. u32 osize = le32_to_cpu(layout->fl_object_size);
  1174. u32 su = le32_to_cpu(layout->fl_stripe_unit);
  1175. u32 sc = le32_to_cpu(layout->fl_stripe_count);
  1176. u32 bl, stripeno, stripepos, objsetno;
  1177. u32 su_per_object;
  1178. u64 t, su_offset;
  1179. dout("mapping %llu~%llu osize %u fl_su %u\n", off, len,
  1180. osize, su);
  1181. if (su == 0 || sc == 0)
  1182. goto invalid;
  1183. su_per_object = osize / su;
  1184. if (su_per_object == 0)
  1185. goto invalid;
  1186. dout("osize %u / su %u = su_per_object %u\n", osize, su,
  1187. su_per_object);
  1188. if ((su & ~PAGE_MASK) != 0)
  1189. goto invalid;
  1190. /* bl = *off / su; */
  1191. t = off;
  1192. do_div(t, su);
  1193. bl = t;
  1194. dout("off %llu / su %u = bl %u\n", off, su, bl);
  1195. stripeno = bl / sc;
  1196. stripepos = bl % sc;
  1197. objsetno = stripeno / su_per_object;
  1198. *ono = objsetno * sc + stripepos;
  1199. dout("objset %u * sc %u = ono %u\n", objsetno, sc, (unsigned int)*ono);
  1200. /* *oxoff = *off % layout->fl_stripe_unit; # offset in su */
  1201. t = off;
  1202. su_offset = do_div(t, su);
  1203. *oxoff = su_offset + (stripeno % su_per_object) * su;
  1204. /*
  1205. * Calculate the length of the extent being written to the selected
  1206. * object. This is the minimum of the full length requested (len) or
  1207. * the remainder of the current stripe being written to.
  1208. */
  1209. *oxlen = min_t(u64, len, su - su_offset);
  1210. dout(" obj extent %llu~%llu\n", *oxoff, *oxlen);
  1211. return 0;
  1212. invalid:
  1213. dout(" invalid layout\n");
  1214. *ono = 0;
  1215. *oxoff = 0;
  1216. *oxlen = 0;
  1217. return -EINVAL;
  1218. }
  1219. EXPORT_SYMBOL(ceph_calc_file_object_mapping);
  1220. /*
  1221. * Calculate mapping of a (oloc, oid) pair to a PG. Should only be
  1222. * called with target's (oloc, oid), since tiering isn't taken into
  1223. * account.
  1224. */
  1225. int ceph_oloc_oid_to_pg(struct ceph_osdmap *osdmap,
  1226. struct ceph_object_locator *oloc,
  1227. struct ceph_object_id *oid,
  1228. struct ceph_pg *pg_out)
  1229. {
  1230. struct ceph_pg_pool_info *pi;
  1231. pi = __lookup_pg_pool(&osdmap->pg_pools, oloc->pool);
  1232. if (!pi)
  1233. return -EIO;
  1234. pg_out->pool = oloc->pool;
  1235. pg_out->seed = ceph_str_hash(pi->object_hash, oid->name,
  1236. oid->name_len);
  1237. dout("%s '%.*s' pgid %llu.%x\n", __func__, oid->name_len, oid->name,
  1238. pg_out->pool, pg_out->seed);
  1239. return 0;
  1240. }
  1241. EXPORT_SYMBOL(ceph_oloc_oid_to_pg);
  1242. static int do_crush(struct ceph_osdmap *map, int ruleno, int x,
  1243. int *result, int result_max,
  1244. const __u32 *weight, int weight_max)
  1245. {
  1246. int r;
  1247. BUG_ON(result_max > CEPH_PG_MAX_SIZE);
  1248. mutex_lock(&map->crush_scratch_mutex);
  1249. r = crush_do_rule(map->crush, ruleno, x, result, result_max,
  1250. weight, weight_max, map->crush_scratch_ary);
  1251. mutex_unlock(&map->crush_scratch_mutex);
  1252. return r;
  1253. }
  1254. /*
  1255. * Calculate raw (crush) set for given pgid.
  1256. *
  1257. * Return raw set length, or error.
  1258. */
  1259. static int pg_to_raw_osds(struct ceph_osdmap *osdmap,
  1260. struct ceph_pg_pool_info *pool,
  1261. struct ceph_pg pgid, u32 pps, int *osds)
  1262. {
  1263. int ruleno;
  1264. int len;
  1265. /* crush */
  1266. ruleno = crush_find_rule(osdmap->crush, pool->crush_ruleset,
  1267. pool->type, pool->size);
  1268. if (ruleno < 0) {
  1269. pr_err("no crush rule: pool %lld ruleset %d type %d size %d\n",
  1270. pgid.pool, pool->crush_ruleset, pool->type,
  1271. pool->size);
  1272. return -ENOENT;
  1273. }
  1274. len = do_crush(osdmap, ruleno, pps, osds,
  1275. min_t(int, pool->size, CEPH_PG_MAX_SIZE),
  1276. osdmap->osd_weight, osdmap->max_osd);
  1277. if (len < 0) {
  1278. pr_err("error %d from crush rule %d: pool %lld ruleset %d type %d size %d\n",
  1279. len, ruleno, pgid.pool, pool->crush_ruleset,
  1280. pool->type, pool->size);
  1281. return len;
  1282. }
  1283. return len;
  1284. }
  1285. /*
  1286. * Given raw set, calculate up set and up primary.
  1287. *
  1288. * Return up set length. *primary is set to up primary osd id, or -1
  1289. * if up set is empty.
  1290. */
  1291. static int raw_to_up_osds(struct ceph_osdmap *osdmap,
  1292. struct ceph_pg_pool_info *pool,
  1293. int *osds, int len, int *primary)
  1294. {
  1295. int up_primary = -1;
  1296. int i;
  1297. if (ceph_can_shift_osds(pool)) {
  1298. int removed = 0;
  1299. for (i = 0; i < len; i++) {
  1300. if (ceph_osd_is_down(osdmap, osds[i])) {
  1301. removed++;
  1302. continue;
  1303. }
  1304. if (removed)
  1305. osds[i - removed] = osds[i];
  1306. }
  1307. len -= removed;
  1308. if (len > 0)
  1309. up_primary = osds[0];
  1310. } else {
  1311. for (i = len - 1; i >= 0; i--) {
  1312. if (ceph_osd_is_down(osdmap, osds[i]))
  1313. osds[i] = CRUSH_ITEM_NONE;
  1314. else
  1315. up_primary = osds[i];
  1316. }
  1317. }
  1318. *primary = up_primary;
  1319. return len;
  1320. }
  1321. static void apply_primary_affinity(struct ceph_osdmap *osdmap, u32 pps,
  1322. struct ceph_pg_pool_info *pool,
  1323. int *osds, int len, int *primary)
  1324. {
  1325. int i;
  1326. int pos = -1;
  1327. /*
  1328. * Do we have any non-default primary_affinity values for these
  1329. * osds?
  1330. */
  1331. if (!osdmap->osd_primary_affinity)
  1332. return;
  1333. for (i = 0; i < len; i++) {
  1334. int osd = osds[i];
  1335. if (osd != CRUSH_ITEM_NONE &&
  1336. osdmap->osd_primary_affinity[osd] !=
  1337. CEPH_OSD_DEFAULT_PRIMARY_AFFINITY) {
  1338. break;
  1339. }
  1340. }
  1341. if (i == len)
  1342. return;
  1343. /*
  1344. * Pick the primary. Feed both the seed (for the pg) and the
  1345. * osd into the hash/rng so that a proportional fraction of an
  1346. * osd's pgs get rejected as primary.
  1347. */
  1348. for (i = 0; i < len; i++) {
  1349. int osd = osds[i];
  1350. u32 aff;
  1351. if (osd == CRUSH_ITEM_NONE)
  1352. continue;
  1353. aff = osdmap->osd_primary_affinity[osd];
  1354. if (aff < CEPH_OSD_MAX_PRIMARY_AFFINITY &&
  1355. (crush_hash32_2(CRUSH_HASH_RJENKINS1,
  1356. pps, osd) >> 16) >= aff) {
  1357. /*
  1358. * We chose not to use this primary. Note it
  1359. * anyway as a fallback in case we don't pick
  1360. * anyone else, but keep looking.
  1361. */
  1362. if (pos < 0)
  1363. pos = i;
  1364. } else {
  1365. pos = i;
  1366. break;
  1367. }
  1368. }
  1369. if (pos < 0)
  1370. return;
  1371. *primary = osds[pos];
  1372. if (ceph_can_shift_osds(pool) && pos > 0) {
  1373. /* move the new primary to the front */
  1374. for (i = pos; i > 0; i--)
  1375. osds[i] = osds[i - 1];
  1376. osds[0] = *primary;
  1377. }
  1378. }
  1379. /*
  1380. * Given up set, apply pg_temp and primary_temp mappings.
  1381. *
  1382. * Return acting set length. *primary is set to acting primary osd id,
  1383. * or -1 if acting set is empty.
  1384. */
  1385. static int apply_temps(struct ceph_osdmap *osdmap,
  1386. struct ceph_pg_pool_info *pool, struct ceph_pg pgid,
  1387. int *osds, int len, int *primary)
  1388. {
  1389. struct ceph_pg_mapping *pg;
  1390. int temp_len;
  1391. int temp_primary;
  1392. int i;
  1393. /* raw_pg -> pg */
  1394. pgid.seed = ceph_stable_mod(pgid.seed, pool->pg_num,
  1395. pool->pg_num_mask);
  1396. /* pg_temp? */
  1397. pg = __lookup_pg_mapping(&osdmap->pg_temp, pgid);
  1398. if (pg) {
  1399. temp_len = 0;
  1400. temp_primary = -1;
  1401. for (i = 0; i < pg->pg_temp.len; i++) {
  1402. if (ceph_osd_is_down(osdmap, pg->pg_temp.osds[i])) {
  1403. if (ceph_can_shift_osds(pool))
  1404. continue;
  1405. else
  1406. osds[temp_len++] = CRUSH_ITEM_NONE;
  1407. } else {
  1408. osds[temp_len++] = pg->pg_temp.osds[i];
  1409. }
  1410. }
  1411. /* apply pg_temp's primary */
  1412. for (i = 0; i < temp_len; i++) {
  1413. if (osds[i] != CRUSH_ITEM_NONE) {
  1414. temp_primary = osds[i];
  1415. break;
  1416. }
  1417. }
  1418. } else {
  1419. temp_len = len;
  1420. temp_primary = *primary;
  1421. }
  1422. /* primary_temp? */
  1423. pg = __lookup_pg_mapping(&osdmap->primary_temp, pgid);
  1424. if (pg)
  1425. temp_primary = pg->primary_temp.osd;
  1426. *primary = temp_primary;
  1427. return temp_len;
  1428. }
  1429. /*
  1430. * Calculate acting set for given pgid.
  1431. *
  1432. * Return acting set length, or error. *primary is set to acting
  1433. * primary osd id, or -1 if acting set is empty or on error.
  1434. */
  1435. int ceph_calc_pg_acting(struct ceph_osdmap *osdmap, struct ceph_pg pgid,
  1436. int *osds, int *primary)
  1437. {
  1438. struct ceph_pg_pool_info *pool;
  1439. u32 pps;
  1440. int len;
  1441. pool = __lookup_pg_pool(&osdmap->pg_pools, pgid.pool);
  1442. if (!pool) {
  1443. *primary = -1;
  1444. return -ENOENT;
  1445. }
  1446. if (pool->flags & CEPH_POOL_FLAG_HASHPSPOOL) {
  1447. /* hash pool id and seed so that pool PGs do not overlap */
  1448. pps = crush_hash32_2(CRUSH_HASH_RJENKINS1,
  1449. ceph_stable_mod(pgid.seed, pool->pgp_num,
  1450. pool->pgp_num_mask),
  1451. pgid.pool);
  1452. } else {
  1453. /*
  1454. * legacy behavior: add ps and pool together. this is
  1455. * not a great approach because the PGs from each pool
  1456. * will overlap on top of each other: 0.5 == 1.4 ==
  1457. * 2.3 == ...
  1458. */
  1459. pps = ceph_stable_mod(pgid.seed, pool->pgp_num,
  1460. pool->pgp_num_mask) +
  1461. (unsigned)pgid.pool;
  1462. }
  1463. len = pg_to_raw_osds(osdmap, pool, pgid, pps, osds);
  1464. if (len < 0) {
  1465. *primary = -1;
  1466. return len;
  1467. }
  1468. len = raw_to_up_osds(osdmap, pool, osds, len, primary);
  1469. apply_primary_affinity(osdmap, pps, pool, osds, len, primary);
  1470. len = apply_temps(osdmap, pool, pgid, osds, len, primary);
  1471. return len;
  1472. }
  1473. /*
  1474. * Return primary osd for given pgid, or -1 if none.
  1475. */
  1476. int ceph_calc_pg_primary(struct ceph_osdmap *osdmap, struct ceph_pg pgid)
  1477. {
  1478. int osds[CEPH_PG_MAX_SIZE];
  1479. int primary;
  1480. ceph_calc_pg_acting(osdmap, pgid, osds, &primary);
  1481. return primary;
  1482. }
  1483. EXPORT_SYMBOL(ceph_calc_pg_primary);