osdmap.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201
  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. err = -EINVAL;
  268. ceph_decode_32_safe(p, end, yes, bad);
  269. if (!yes) {
  270. dout("crush_decode NO rule %d off %x %p to %p\n",
  271. i, (int)(*p-start), *p, end);
  272. c->rules[i] = NULL;
  273. continue;
  274. }
  275. dout("crush_decode rule %d off %x %p to %p\n",
  276. i, (int)(*p-start), *p, end);
  277. /* len */
  278. ceph_decode_32_safe(p, end, yes, bad);
  279. #if BITS_PER_LONG == 32
  280. err = -EINVAL;
  281. if (yes > (ULONG_MAX - sizeof(*r))
  282. / sizeof(struct crush_rule_step))
  283. goto bad;
  284. #endif
  285. r = c->rules[i] = kmalloc(sizeof(*r) +
  286. yes*sizeof(struct crush_rule_step),
  287. GFP_NOFS);
  288. if (r == NULL)
  289. goto badmem;
  290. dout(" rule %d is at %p\n", i, r);
  291. r->len = yes;
  292. ceph_decode_copy_safe(p, end, &r->mask, 4, bad); /* 4 u8's */
  293. ceph_decode_need(p, end, r->len*3*sizeof(u32), bad);
  294. for (j = 0; j < r->len; j++) {
  295. r->steps[j].op = ceph_decode_32(p);
  296. r->steps[j].arg1 = ceph_decode_32(p);
  297. r->steps[j].arg2 = ceph_decode_32(p);
  298. }
  299. }
  300. /* ignore trailing name maps. */
  301. for (num_name_maps = 0; num_name_maps < 3; num_name_maps++) {
  302. err = skip_name_map(p, end);
  303. if (err < 0)
  304. goto done;
  305. }
  306. /* tunables */
  307. ceph_decode_need(p, end, 3*sizeof(u32), done);
  308. c->choose_local_tries = ceph_decode_32(p);
  309. c->choose_local_fallback_tries = ceph_decode_32(p);
  310. c->choose_total_tries = ceph_decode_32(p);
  311. dout("crush decode tunable choose_local_tries = %d\n",
  312. c->choose_local_tries);
  313. dout("crush decode tunable choose_local_fallback_tries = %d\n",
  314. c->choose_local_fallback_tries);
  315. dout("crush decode tunable choose_total_tries = %d\n",
  316. c->choose_total_tries);
  317. ceph_decode_need(p, end, sizeof(u32), done);
  318. c->chooseleaf_descend_once = ceph_decode_32(p);
  319. dout("crush decode tunable chooseleaf_descend_once = %d\n",
  320. c->chooseleaf_descend_once);
  321. ceph_decode_need(p, end, sizeof(u8), done);
  322. c->chooseleaf_vary_r = ceph_decode_8(p);
  323. dout("crush decode tunable chooseleaf_vary_r = %d\n",
  324. c->chooseleaf_vary_r);
  325. /* skip straw_calc_version, allowed_bucket_algs */
  326. ceph_decode_need(p, end, sizeof(u8) + sizeof(u32), done);
  327. *p += sizeof(u8) + sizeof(u32);
  328. ceph_decode_need(p, end, sizeof(u8), done);
  329. c->chooseleaf_stable = ceph_decode_8(p);
  330. dout("crush decode tunable chooseleaf_stable = %d\n",
  331. c->chooseleaf_stable);
  332. done:
  333. dout("crush_decode success\n");
  334. return c;
  335. badmem:
  336. err = -ENOMEM;
  337. bad:
  338. dout("crush_decode fail %d\n", err);
  339. crush_destroy(c);
  340. return ERR_PTR(err);
  341. }
  342. int ceph_pg_compare(const struct ceph_pg *lhs, const struct ceph_pg *rhs)
  343. {
  344. if (lhs->pool < rhs->pool)
  345. return -1;
  346. if (lhs->pool > rhs->pool)
  347. return 1;
  348. if (lhs->seed < rhs->seed)
  349. return -1;
  350. if (lhs->seed > rhs->seed)
  351. return 1;
  352. return 0;
  353. }
  354. /*
  355. * rbtree of pg_mapping for handling pg_temp (explicit mapping of pgid
  356. * to a set of osds) and primary_temp (explicit primary setting)
  357. */
  358. static int __insert_pg_mapping(struct ceph_pg_mapping *new,
  359. struct rb_root *root)
  360. {
  361. struct rb_node **p = &root->rb_node;
  362. struct rb_node *parent = NULL;
  363. struct ceph_pg_mapping *pg = NULL;
  364. int c;
  365. dout("__insert_pg_mapping %llx %p\n", *(u64 *)&new->pgid, new);
  366. while (*p) {
  367. parent = *p;
  368. pg = rb_entry(parent, struct ceph_pg_mapping, node);
  369. c = ceph_pg_compare(&new->pgid, &pg->pgid);
  370. if (c < 0)
  371. p = &(*p)->rb_left;
  372. else if (c > 0)
  373. p = &(*p)->rb_right;
  374. else
  375. return -EEXIST;
  376. }
  377. rb_link_node(&new->node, parent, p);
  378. rb_insert_color(&new->node, root);
  379. return 0;
  380. }
  381. static struct ceph_pg_mapping *__lookup_pg_mapping(struct rb_root *root,
  382. struct ceph_pg pgid)
  383. {
  384. struct rb_node *n = root->rb_node;
  385. struct ceph_pg_mapping *pg;
  386. int c;
  387. while (n) {
  388. pg = rb_entry(n, struct ceph_pg_mapping, node);
  389. c = ceph_pg_compare(&pgid, &pg->pgid);
  390. if (c < 0) {
  391. n = n->rb_left;
  392. } else if (c > 0) {
  393. n = n->rb_right;
  394. } else {
  395. dout("__lookup_pg_mapping %lld.%x got %p\n",
  396. pgid.pool, pgid.seed, pg);
  397. return pg;
  398. }
  399. }
  400. return NULL;
  401. }
  402. static int __remove_pg_mapping(struct rb_root *root, struct ceph_pg pgid)
  403. {
  404. struct ceph_pg_mapping *pg = __lookup_pg_mapping(root, pgid);
  405. if (pg) {
  406. dout("__remove_pg_mapping %lld.%x %p\n", pgid.pool, pgid.seed,
  407. pg);
  408. rb_erase(&pg->node, root);
  409. kfree(pg);
  410. return 0;
  411. }
  412. dout("__remove_pg_mapping %lld.%x dne\n", pgid.pool, pgid.seed);
  413. return -ENOENT;
  414. }
  415. /*
  416. * rbtree of pg pool info
  417. */
  418. static int __insert_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *new)
  419. {
  420. struct rb_node **p = &root->rb_node;
  421. struct rb_node *parent = NULL;
  422. struct ceph_pg_pool_info *pi = NULL;
  423. while (*p) {
  424. parent = *p;
  425. pi = rb_entry(parent, struct ceph_pg_pool_info, node);
  426. if (new->id < pi->id)
  427. p = &(*p)->rb_left;
  428. else if (new->id > pi->id)
  429. p = &(*p)->rb_right;
  430. else
  431. return -EEXIST;
  432. }
  433. rb_link_node(&new->node, parent, p);
  434. rb_insert_color(&new->node, root);
  435. return 0;
  436. }
  437. static struct ceph_pg_pool_info *__lookup_pg_pool(struct rb_root *root, u64 id)
  438. {
  439. struct ceph_pg_pool_info *pi;
  440. struct rb_node *n = root->rb_node;
  441. while (n) {
  442. pi = rb_entry(n, struct ceph_pg_pool_info, node);
  443. if (id < pi->id)
  444. n = n->rb_left;
  445. else if (id > pi->id)
  446. n = n->rb_right;
  447. else
  448. return pi;
  449. }
  450. return NULL;
  451. }
  452. struct ceph_pg_pool_info *ceph_pg_pool_by_id(struct ceph_osdmap *map, u64 id)
  453. {
  454. return __lookup_pg_pool(&map->pg_pools, id);
  455. }
  456. const char *ceph_pg_pool_name_by_id(struct ceph_osdmap *map, u64 id)
  457. {
  458. struct ceph_pg_pool_info *pi;
  459. if (id == CEPH_NOPOOL)
  460. return NULL;
  461. if (WARN_ON_ONCE(id > (u64) INT_MAX))
  462. return NULL;
  463. pi = __lookup_pg_pool(&map->pg_pools, (int) id);
  464. return pi ? pi->name : NULL;
  465. }
  466. EXPORT_SYMBOL(ceph_pg_pool_name_by_id);
  467. int ceph_pg_poolid_by_name(struct ceph_osdmap *map, const char *name)
  468. {
  469. struct rb_node *rbp;
  470. for (rbp = rb_first(&map->pg_pools); rbp; rbp = rb_next(rbp)) {
  471. struct ceph_pg_pool_info *pi =
  472. rb_entry(rbp, struct ceph_pg_pool_info, node);
  473. if (pi->name && strcmp(pi->name, name) == 0)
  474. return pi->id;
  475. }
  476. return -ENOENT;
  477. }
  478. EXPORT_SYMBOL(ceph_pg_poolid_by_name);
  479. static void __remove_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *pi)
  480. {
  481. rb_erase(&pi->node, root);
  482. kfree(pi->name);
  483. kfree(pi);
  484. }
  485. static int decode_pool(void **p, void *end, struct ceph_pg_pool_info *pi)
  486. {
  487. u8 ev, cv;
  488. unsigned len, num;
  489. void *pool_end;
  490. ceph_decode_need(p, end, 2 + 4, bad);
  491. ev = ceph_decode_8(p); /* encoding version */
  492. cv = ceph_decode_8(p); /* compat version */
  493. if (ev < 5) {
  494. pr_warn("got v %d < 5 cv %d of ceph_pg_pool\n", ev, cv);
  495. return -EINVAL;
  496. }
  497. if (cv > 9) {
  498. pr_warn("got v %d cv %d > 9 of ceph_pg_pool\n", ev, cv);
  499. return -EINVAL;
  500. }
  501. len = ceph_decode_32(p);
  502. ceph_decode_need(p, end, len, bad);
  503. pool_end = *p + len;
  504. pi->type = ceph_decode_8(p);
  505. pi->size = ceph_decode_8(p);
  506. pi->crush_ruleset = ceph_decode_8(p);
  507. pi->object_hash = ceph_decode_8(p);
  508. pi->pg_num = ceph_decode_32(p);
  509. pi->pgp_num = ceph_decode_32(p);
  510. *p += 4 + 4; /* skip lpg* */
  511. *p += 4; /* skip last_change */
  512. *p += 8 + 4; /* skip snap_seq, snap_epoch */
  513. /* skip snaps */
  514. num = ceph_decode_32(p);
  515. while (num--) {
  516. *p += 8; /* snapid key */
  517. *p += 1 + 1; /* versions */
  518. len = ceph_decode_32(p);
  519. *p += len;
  520. }
  521. /* skip removed_snaps */
  522. num = ceph_decode_32(p);
  523. *p += num * (8 + 8);
  524. *p += 8; /* skip auid */
  525. pi->flags = ceph_decode_64(p);
  526. *p += 4; /* skip crash_replay_interval */
  527. if (ev >= 7)
  528. pi->min_size = ceph_decode_8(p);
  529. else
  530. pi->min_size = pi->size - pi->size / 2;
  531. if (ev >= 8)
  532. *p += 8 + 8; /* skip quota_max_* */
  533. if (ev >= 9) {
  534. /* skip tiers */
  535. num = ceph_decode_32(p);
  536. *p += num * 8;
  537. *p += 8; /* skip tier_of */
  538. *p += 1; /* skip cache_mode */
  539. pi->read_tier = ceph_decode_64(p);
  540. pi->write_tier = ceph_decode_64(p);
  541. } else {
  542. pi->read_tier = -1;
  543. pi->write_tier = -1;
  544. }
  545. if (ev >= 10) {
  546. /* skip properties */
  547. num = ceph_decode_32(p);
  548. while (num--) {
  549. len = ceph_decode_32(p);
  550. *p += len; /* key */
  551. len = ceph_decode_32(p);
  552. *p += len; /* val */
  553. }
  554. }
  555. if (ev >= 11) {
  556. /* skip hit_set_params */
  557. *p += 1 + 1; /* versions */
  558. len = ceph_decode_32(p);
  559. *p += len;
  560. *p += 4; /* skip hit_set_period */
  561. *p += 4; /* skip hit_set_count */
  562. }
  563. if (ev >= 12)
  564. *p += 4; /* skip stripe_width */
  565. if (ev >= 13) {
  566. *p += 8; /* skip target_max_bytes */
  567. *p += 8; /* skip target_max_objects */
  568. *p += 4; /* skip cache_target_dirty_ratio_micro */
  569. *p += 4; /* skip cache_target_full_ratio_micro */
  570. *p += 4; /* skip cache_min_flush_age */
  571. *p += 4; /* skip cache_min_evict_age */
  572. }
  573. if (ev >= 14) {
  574. /* skip erasure_code_profile */
  575. len = ceph_decode_32(p);
  576. *p += len;
  577. }
  578. if (ev >= 15)
  579. pi->last_force_request_resend = ceph_decode_32(p);
  580. else
  581. pi->last_force_request_resend = 0;
  582. /* ignore the rest */
  583. *p = pool_end;
  584. calc_pg_masks(pi);
  585. return 0;
  586. bad:
  587. return -EINVAL;
  588. }
  589. static int decode_pool_names(void **p, void *end, struct ceph_osdmap *map)
  590. {
  591. struct ceph_pg_pool_info *pi;
  592. u32 num, len;
  593. u64 pool;
  594. ceph_decode_32_safe(p, end, num, bad);
  595. dout(" %d pool names\n", num);
  596. while (num--) {
  597. ceph_decode_64_safe(p, end, pool, bad);
  598. ceph_decode_32_safe(p, end, len, bad);
  599. dout(" pool %llu len %d\n", pool, len);
  600. ceph_decode_need(p, end, len, bad);
  601. pi = __lookup_pg_pool(&map->pg_pools, pool);
  602. if (pi) {
  603. char *name = kstrndup(*p, len, GFP_NOFS);
  604. if (!name)
  605. return -ENOMEM;
  606. kfree(pi->name);
  607. pi->name = name;
  608. dout(" name is %s\n", pi->name);
  609. }
  610. *p += len;
  611. }
  612. return 0;
  613. bad:
  614. return -EINVAL;
  615. }
  616. /*
  617. * osd map
  618. */
  619. struct ceph_osdmap *ceph_osdmap_alloc(void)
  620. {
  621. struct ceph_osdmap *map;
  622. map = kzalloc(sizeof(*map), GFP_NOIO);
  623. if (!map)
  624. return NULL;
  625. map->pg_pools = RB_ROOT;
  626. map->pool_max = -1;
  627. map->pg_temp = RB_ROOT;
  628. map->primary_temp = RB_ROOT;
  629. mutex_init(&map->crush_scratch_mutex);
  630. return map;
  631. }
  632. void ceph_osdmap_destroy(struct ceph_osdmap *map)
  633. {
  634. dout("osdmap_destroy %p\n", map);
  635. if (map->crush)
  636. crush_destroy(map->crush);
  637. while (!RB_EMPTY_ROOT(&map->pg_temp)) {
  638. struct ceph_pg_mapping *pg =
  639. rb_entry(rb_first(&map->pg_temp),
  640. struct ceph_pg_mapping, node);
  641. rb_erase(&pg->node, &map->pg_temp);
  642. kfree(pg);
  643. }
  644. while (!RB_EMPTY_ROOT(&map->primary_temp)) {
  645. struct ceph_pg_mapping *pg =
  646. rb_entry(rb_first(&map->primary_temp),
  647. struct ceph_pg_mapping, node);
  648. rb_erase(&pg->node, &map->primary_temp);
  649. kfree(pg);
  650. }
  651. while (!RB_EMPTY_ROOT(&map->pg_pools)) {
  652. struct ceph_pg_pool_info *pi =
  653. rb_entry(rb_first(&map->pg_pools),
  654. struct ceph_pg_pool_info, node);
  655. __remove_pg_pool(&map->pg_pools, pi);
  656. }
  657. kfree(map->osd_state);
  658. kfree(map->osd_weight);
  659. kfree(map->osd_addr);
  660. kfree(map->osd_primary_affinity);
  661. kfree(map);
  662. }
  663. /*
  664. * Adjust max_osd value, (re)allocate arrays.
  665. *
  666. * The new elements are properly initialized.
  667. */
  668. static int osdmap_set_max_osd(struct ceph_osdmap *map, int max)
  669. {
  670. u8 *state;
  671. u32 *weight;
  672. struct ceph_entity_addr *addr;
  673. int i;
  674. state = krealloc(map->osd_state, max*sizeof(*state), GFP_NOFS);
  675. if (!state)
  676. return -ENOMEM;
  677. map->osd_state = state;
  678. weight = krealloc(map->osd_weight, max*sizeof(*weight), GFP_NOFS);
  679. if (!weight)
  680. return -ENOMEM;
  681. map->osd_weight = weight;
  682. addr = krealloc(map->osd_addr, max*sizeof(*addr), GFP_NOFS);
  683. if (!addr)
  684. return -ENOMEM;
  685. map->osd_addr = addr;
  686. for (i = map->max_osd; i < max; i++) {
  687. map->osd_state[i] = 0;
  688. map->osd_weight[i] = CEPH_OSD_OUT;
  689. memset(map->osd_addr + i, 0, sizeof(*map->osd_addr));
  690. }
  691. if (map->osd_primary_affinity) {
  692. u32 *affinity;
  693. affinity = krealloc(map->osd_primary_affinity,
  694. max*sizeof(*affinity), GFP_NOFS);
  695. if (!affinity)
  696. return -ENOMEM;
  697. map->osd_primary_affinity = affinity;
  698. for (i = map->max_osd; i < max; i++)
  699. map->osd_primary_affinity[i] =
  700. CEPH_OSD_DEFAULT_PRIMARY_AFFINITY;
  701. }
  702. map->max_osd = max;
  703. return 0;
  704. }
  705. #define OSDMAP_WRAPPER_COMPAT_VER 7
  706. #define OSDMAP_CLIENT_DATA_COMPAT_VER 1
  707. /*
  708. * Return 0 or error. On success, *v is set to 0 for old (v6) osdmaps,
  709. * to struct_v of the client_data section for new (v7 and above)
  710. * osdmaps.
  711. */
  712. static int get_osdmap_client_data_v(void **p, void *end,
  713. const char *prefix, u8 *v)
  714. {
  715. u8 struct_v;
  716. ceph_decode_8_safe(p, end, struct_v, e_inval);
  717. if (struct_v >= 7) {
  718. u8 struct_compat;
  719. ceph_decode_8_safe(p, end, struct_compat, e_inval);
  720. if (struct_compat > OSDMAP_WRAPPER_COMPAT_VER) {
  721. pr_warn("got v %d cv %d > %d of %s ceph_osdmap\n",
  722. struct_v, struct_compat,
  723. OSDMAP_WRAPPER_COMPAT_VER, prefix);
  724. return -EINVAL;
  725. }
  726. *p += 4; /* ignore wrapper struct_len */
  727. ceph_decode_8_safe(p, end, struct_v, e_inval);
  728. ceph_decode_8_safe(p, end, struct_compat, e_inval);
  729. if (struct_compat > OSDMAP_CLIENT_DATA_COMPAT_VER) {
  730. pr_warn("got v %d cv %d > %d of %s ceph_osdmap client data\n",
  731. struct_v, struct_compat,
  732. OSDMAP_CLIENT_DATA_COMPAT_VER, prefix);
  733. return -EINVAL;
  734. }
  735. *p += 4; /* ignore client data struct_len */
  736. } else {
  737. u16 version;
  738. *p -= 1;
  739. ceph_decode_16_safe(p, end, version, e_inval);
  740. if (version < 6) {
  741. pr_warn("got v %d < 6 of %s ceph_osdmap\n",
  742. version, prefix);
  743. return -EINVAL;
  744. }
  745. /* old osdmap enconding */
  746. struct_v = 0;
  747. }
  748. *v = struct_v;
  749. return 0;
  750. e_inval:
  751. return -EINVAL;
  752. }
  753. static int __decode_pools(void **p, void *end, struct ceph_osdmap *map,
  754. bool incremental)
  755. {
  756. u32 n;
  757. ceph_decode_32_safe(p, end, n, e_inval);
  758. while (n--) {
  759. struct ceph_pg_pool_info *pi;
  760. u64 pool;
  761. int ret;
  762. ceph_decode_64_safe(p, end, pool, e_inval);
  763. pi = __lookup_pg_pool(&map->pg_pools, pool);
  764. if (!incremental || !pi) {
  765. pi = kzalloc(sizeof(*pi), GFP_NOFS);
  766. if (!pi)
  767. return -ENOMEM;
  768. pi->id = pool;
  769. ret = __insert_pg_pool(&map->pg_pools, pi);
  770. if (ret) {
  771. kfree(pi);
  772. return ret;
  773. }
  774. }
  775. ret = decode_pool(p, end, pi);
  776. if (ret)
  777. return ret;
  778. }
  779. return 0;
  780. e_inval:
  781. return -EINVAL;
  782. }
  783. static int decode_pools(void **p, void *end, struct ceph_osdmap *map)
  784. {
  785. return __decode_pools(p, end, map, false);
  786. }
  787. static int decode_new_pools(void **p, void *end, struct ceph_osdmap *map)
  788. {
  789. return __decode_pools(p, end, map, true);
  790. }
  791. static int __decode_pg_temp(void **p, void *end, struct ceph_osdmap *map,
  792. bool incremental)
  793. {
  794. u32 n;
  795. ceph_decode_32_safe(p, end, n, e_inval);
  796. while (n--) {
  797. struct ceph_pg pgid;
  798. u32 len, i;
  799. int ret;
  800. ret = ceph_decode_pgid(p, end, &pgid);
  801. if (ret)
  802. return ret;
  803. ceph_decode_32_safe(p, end, len, e_inval);
  804. ret = __remove_pg_mapping(&map->pg_temp, pgid);
  805. BUG_ON(!incremental && ret != -ENOENT);
  806. if (!incremental || len > 0) {
  807. struct ceph_pg_mapping *pg;
  808. ceph_decode_need(p, end, len*sizeof(u32), e_inval);
  809. if (len > (UINT_MAX - sizeof(*pg)) / sizeof(u32))
  810. return -EINVAL;
  811. pg = kzalloc(sizeof(*pg) + len*sizeof(u32), GFP_NOFS);
  812. if (!pg)
  813. return -ENOMEM;
  814. pg->pgid = pgid;
  815. pg->pg_temp.len = len;
  816. for (i = 0; i < len; i++)
  817. pg->pg_temp.osds[i] = ceph_decode_32(p);
  818. ret = __insert_pg_mapping(pg, &map->pg_temp);
  819. if (ret) {
  820. kfree(pg);
  821. return ret;
  822. }
  823. }
  824. }
  825. return 0;
  826. e_inval:
  827. return -EINVAL;
  828. }
  829. static int decode_pg_temp(void **p, void *end, struct ceph_osdmap *map)
  830. {
  831. return __decode_pg_temp(p, end, map, false);
  832. }
  833. static int decode_new_pg_temp(void **p, void *end, struct ceph_osdmap *map)
  834. {
  835. return __decode_pg_temp(p, end, map, true);
  836. }
  837. static int __decode_primary_temp(void **p, void *end, struct ceph_osdmap *map,
  838. bool incremental)
  839. {
  840. u32 n;
  841. ceph_decode_32_safe(p, end, n, e_inval);
  842. while (n--) {
  843. struct ceph_pg pgid;
  844. u32 osd;
  845. int ret;
  846. ret = ceph_decode_pgid(p, end, &pgid);
  847. if (ret)
  848. return ret;
  849. ceph_decode_32_safe(p, end, osd, e_inval);
  850. ret = __remove_pg_mapping(&map->primary_temp, pgid);
  851. BUG_ON(!incremental && ret != -ENOENT);
  852. if (!incremental || osd != (u32)-1) {
  853. struct ceph_pg_mapping *pg;
  854. pg = kzalloc(sizeof(*pg), GFP_NOFS);
  855. if (!pg)
  856. return -ENOMEM;
  857. pg->pgid = pgid;
  858. pg->primary_temp.osd = osd;
  859. ret = __insert_pg_mapping(pg, &map->primary_temp);
  860. if (ret) {
  861. kfree(pg);
  862. return ret;
  863. }
  864. }
  865. }
  866. return 0;
  867. e_inval:
  868. return -EINVAL;
  869. }
  870. static int decode_primary_temp(void **p, void *end, struct ceph_osdmap *map)
  871. {
  872. return __decode_primary_temp(p, end, map, false);
  873. }
  874. static int decode_new_primary_temp(void **p, void *end,
  875. struct ceph_osdmap *map)
  876. {
  877. return __decode_primary_temp(p, end, map, true);
  878. }
  879. u32 ceph_get_primary_affinity(struct ceph_osdmap *map, int osd)
  880. {
  881. BUG_ON(osd >= map->max_osd);
  882. if (!map->osd_primary_affinity)
  883. return CEPH_OSD_DEFAULT_PRIMARY_AFFINITY;
  884. return map->osd_primary_affinity[osd];
  885. }
  886. static int set_primary_affinity(struct ceph_osdmap *map, int osd, u32 aff)
  887. {
  888. BUG_ON(osd >= map->max_osd);
  889. if (!map->osd_primary_affinity) {
  890. int i;
  891. map->osd_primary_affinity = kmalloc(map->max_osd*sizeof(u32),
  892. GFP_NOFS);
  893. if (!map->osd_primary_affinity)
  894. return -ENOMEM;
  895. for (i = 0; i < map->max_osd; i++)
  896. map->osd_primary_affinity[i] =
  897. CEPH_OSD_DEFAULT_PRIMARY_AFFINITY;
  898. }
  899. map->osd_primary_affinity[osd] = aff;
  900. return 0;
  901. }
  902. static int decode_primary_affinity(void **p, void *end,
  903. struct ceph_osdmap *map)
  904. {
  905. u32 len, i;
  906. ceph_decode_32_safe(p, end, len, e_inval);
  907. if (len == 0) {
  908. kfree(map->osd_primary_affinity);
  909. map->osd_primary_affinity = NULL;
  910. return 0;
  911. }
  912. if (len != map->max_osd)
  913. goto e_inval;
  914. ceph_decode_need(p, end, map->max_osd*sizeof(u32), e_inval);
  915. for (i = 0; i < map->max_osd; i++) {
  916. int ret;
  917. ret = set_primary_affinity(map, i, ceph_decode_32(p));
  918. if (ret)
  919. return ret;
  920. }
  921. return 0;
  922. e_inval:
  923. return -EINVAL;
  924. }
  925. static int decode_new_primary_affinity(void **p, void *end,
  926. struct ceph_osdmap *map)
  927. {
  928. u32 n;
  929. ceph_decode_32_safe(p, end, n, e_inval);
  930. while (n--) {
  931. u32 osd, aff;
  932. int ret;
  933. ceph_decode_32_safe(p, end, osd, e_inval);
  934. ceph_decode_32_safe(p, end, aff, e_inval);
  935. ret = set_primary_affinity(map, osd, aff);
  936. if (ret)
  937. return ret;
  938. pr_info("osd%d primary-affinity 0x%x\n", osd, aff);
  939. }
  940. return 0;
  941. e_inval:
  942. return -EINVAL;
  943. }
  944. /*
  945. * decode a full map.
  946. */
  947. static int osdmap_decode(void **p, void *end, struct ceph_osdmap *map)
  948. {
  949. u8 struct_v;
  950. u32 epoch = 0;
  951. void *start = *p;
  952. u32 max;
  953. u32 len, i;
  954. int err;
  955. dout("%s %p to %p len %d\n", __func__, *p, end, (int)(end - *p));
  956. err = get_osdmap_client_data_v(p, end, "full", &struct_v);
  957. if (err)
  958. goto bad;
  959. /* fsid, epoch, created, modified */
  960. ceph_decode_need(p, end, sizeof(map->fsid) + sizeof(u32) +
  961. sizeof(map->created) + sizeof(map->modified), e_inval);
  962. ceph_decode_copy(p, &map->fsid, sizeof(map->fsid));
  963. epoch = map->epoch = ceph_decode_32(p);
  964. ceph_decode_copy(p, &map->created, sizeof(map->created));
  965. ceph_decode_copy(p, &map->modified, sizeof(map->modified));
  966. /* pools */
  967. err = decode_pools(p, end, map);
  968. if (err)
  969. goto bad;
  970. /* pool_name */
  971. err = decode_pool_names(p, end, map);
  972. if (err)
  973. goto bad;
  974. ceph_decode_32_safe(p, end, map->pool_max, e_inval);
  975. ceph_decode_32_safe(p, end, map->flags, e_inval);
  976. /* max_osd */
  977. ceph_decode_32_safe(p, end, max, e_inval);
  978. /* (re)alloc osd arrays */
  979. err = osdmap_set_max_osd(map, max);
  980. if (err)
  981. goto bad;
  982. /* osd_state, osd_weight, osd_addrs->client_addr */
  983. ceph_decode_need(p, end, 3*sizeof(u32) +
  984. map->max_osd*(1 + sizeof(*map->osd_weight) +
  985. sizeof(*map->osd_addr)), e_inval);
  986. if (ceph_decode_32(p) != map->max_osd)
  987. goto e_inval;
  988. ceph_decode_copy(p, map->osd_state, map->max_osd);
  989. if (ceph_decode_32(p) != map->max_osd)
  990. goto e_inval;
  991. for (i = 0; i < map->max_osd; i++)
  992. map->osd_weight[i] = ceph_decode_32(p);
  993. if (ceph_decode_32(p) != map->max_osd)
  994. goto e_inval;
  995. ceph_decode_copy(p, map->osd_addr, map->max_osd*sizeof(*map->osd_addr));
  996. for (i = 0; i < map->max_osd; i++)
  997. ceph_decode_addr(&map->osd_addr[i]);
  998. /* pg_temp */
  999. err = decode_pg_temp(p, end, map);
  1000. if (err)
  1001. goto bad;
  1002. /* primary_temp */
  1003. if (struct_v >= 1) {
  1004. err = decode_primary_temp(p, end, map);
  1005. if (err)
  1006. goto bad;
  1007. }
  1008. /* primary_affinity */
  1009. if (struct_v >= 2) {
  1010. err = decode_primary_affinity(p, end, map);
  1011. if (err)
  1012. goto bad;
  1013. } else {
  1014. /* XXX can this happen? */
  1015. kfree(map->osd_primary_affinity);
  1016. map->osd_primary_affinity = NULL;
  1017. }
  1018. /* crush */
  1019. ceph_decode_32_safe(p, end, len, e_inval);
  1020. map->crush = crush_decode(*p, min(*p + len, end));
  1021. if (IS_ERR(map->crush)) {
  1022. err = PTR_ERR(map->crush);
  1023. map->crush = NULL;
  1024. goto bad;
  1025. }
  1026. *p += len;
  1027. /* ignore the rest */
  1028. *p = end;
  1029. dout("full osdmap epoch %d max_osd %d\n", map->epoch, map->max_osd);
  1030. return 0;
  1031. e_inval:
  1032. err = -EINVAL;
  1033. bad:
  1034. pr_err("corrupt full osdmap (%d) epoch %d off %d (%p of %p-%p)\n",
  1035. err, epoch, (int)(*p - start), *p, start, end);
  1036. print_hex_dump(KERN_DEBUG, "osdmap: ",
  1037. DUMP_PREFIX_OFFSET, 16, 1,
  1038. start, end - start, true);
  1039. return err;
  1040. }
  1041. /*
  1042. * Allocate and decode a full map.
  1043. */
  1044. struct ceph_osdmap *ceph_osdmap_decode(void **p, void *end)
  1045. {
  1046. struct ceph_osdmap *map;
  1047. int ret;
  1048. map = ceph_osdmap_alloc();
  1049. if (!map)
  1050. return ERR_PTR(-ENOMEM);
  1051. ret = osdmap_decode(p, end, map);
  1052. if (ret) {
  1053. ceph_osdmap_destroy(map);
  1054. return ERR_PTR(ret);
  1055. }
  1056. return map;
  1057. }
  1058. /*
  1059. * Encoding order is (new_up_client, new_state, new_weight). Need to
  1060. * apply in the (new_weight, new_state, new_up_client) order, because
  1061. * an incremental map may look like e.g.
  1062. *
  1063. * new_up_client: { osd=6, addr=... } # set osd_state and addr
  1064. * new_state: { osd=6, xorstate=EXISTS } # clear osd_state
  1065. */
  1066. static int decode_new_up_state_weight(void **p, void *end,
  1067. struct ceph_osdmap *map)
  1068. {
  1069. void *new_up_client;
  1070. void *new_state;
  1071. void *new_weight_end;
  1072. u32 len;
  1073. new_up_client = *p;
  1074. ceph_decode_32_safe(p, end, len, e_inval);
  1075. len *= sizeof(u32) + sizeof(struct ceph_entity_addr);
  1076. ceph_decode_need(p, end, len, e_inval);
  1077. *p += len;
  1078. new_state = *p;
  1079. ceph_decode_32_safe(p, end, len, e_inval);
  1080. len *= sizeof(u32) + sizeof(u8);
  1081. ceph_decode_need(p, end, len, e_inval);
  1082. *p += len;
  1083. /* new_weight */
  1084. ceph_decode_32_safe(p, end, len, e_inval);
  1085. while (len--) {
  1086. s32 osd;
  1087. u32 w;
  1088. ceph_decode_need(p, end, 2*sizeof(u32), e_inval);
  1089. osd = ceph_decode_32(p);
  1090. w = ceph_decode_32(p);
  1091. BUG_ON(osd >= map->max_osd);
  1092. pr_info("osd%d weight 0x%x %s\n", osd, w,
  1093. w == CEPH_OSD_IN ? "(in)" :
  1094. (w == CEPH_OSD_OUT ? "(out)" : ""));
  1095. map->osd_weight[osd] = w;
  1096. /*
  1097. * If we are marking in, set the EXISTS, and clear the
  1098. * AUTOOUT and NEW bits.
  1099. */
  1100. if (w) {
  1101. map->osd_state[osd] |= CEPH_OSD_EXISTS;
  1102. map->osd_state[osd] &= ~(CEPH_OSD_AUTOOUT |
  1103. CEPH_OSD_NEW);
  1104. }
  1105. }
  1106. new_weight_end = *p;
  1107. /* new_state (up/down) */
  1108. *p = new_state;
  1109. len = ceph_decode_32(p);
  1110. while (len--) {
  1111. s32 osd;
  1112. u8 xorstate;
  1113. int ret;
  1114. osd = ceph_decode_32(p);
  1115. xorstate = ceph_decode_8(p);
  1116. if (xorstate == 0)
  1117. xorstate = CEPH_OSD_UP;
  1118. BUG_ON(osd >= map->max_osd);
  1119. if ((map->osd_state[osd] & CEPH_OSD_UP) &&
  1120. (xorstate & CEPH_OSD_UP))
  1121. pr_info("osd%d down\n", osd);
  1122. if ((map->osd_state[osd] & CEPH_OSD_EXISTS) &&
  1123. (xorstate & CEPH_OSD_EXISTS)) {
  1124. pr_info("osd%d does not exist\n", osd);
  1125. ret = set_primary_affinity(map, osd,
  1126. CEPH_OSD_DEFAULT_PRIMARY_AFFINITY);
  1127. if (ret)
  1128. return ret;
  1129. memset(map->osd_addr + osd, 0, sizeof(*map->osd_addr));
  1130. map->osd_state[osd] = 0;
  1131. } else {
  1132. map->osd_state[osd] ^= xorstate;
  1133. }
  1134. }
  1135. /* new_up_client */
  1136. *p = new_up_client;
  1137. len = ceph_decode_32(p);
  1138. while (len--) {
  1139. s32 osd;
  1140. struct ceph_entity_addr addr;
  1141. osd = ceph_decode_32(p);
  1142. ceph_decode_copy(p, &addr, sizeof(addr));
  1143. ceph_decode_addr(&addr);
  1144. BUG_ON(osd >= map->max_osd);
  1145. pr_info("osd%d up\n", osd);
  1146. map->osd_state[osd] |= CEPH_OSD_EXISTS | CEPH_OSD_UP;
  1147. map->osd_addr[osd] = addr;
  1148. }
  1149. *p = new_weight_end;
  1150. return 0;
  1151. e_inval:
  1152. return -EINVAL;
  1153. }
  1154. /*
  1155. * decode and apply an incremental map update.
  1156. */
  1157. struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end,
  1158. struct ceph_osdmap *map)
  1159. {
  1160. struct crush_map *newcrush = NULL;
  1161. struct ceph_fsid fsid;
  1162. u32 epoch = 0;
  1163. struct ceph_timespec modified;
  1164. s32 len;
  1165. u64 pool;
  1166. __s64 new_pool_max;
  1167. __s32 new_flags, max;
  1168. void *start = *p;
  1169. int err;
  1170. u8 struct_v;
  1171. dout("%s %p to %p len %d\n", __func__, *p, end, (int)(end - *p));
  1172. err = get_osdmap_client_data_v(p, end, "inc", &struct_v);
  1173. if (err)
  1174. goto bad;
  1175. /* fsid, epoch, modified, new_pool_max, new_flags */
  1176. ceph_decode_need(p, end, sizeof(fsid) + sizeof(u32) + sizeof(modified) +
  1177. sizeof(u64) + sizeof(u32), e_inval);
  1178. ceph_decode_copy(p, &fsid, sizeof(fsid));
  1179. epoch = ceph_decode_32(p);
  1180. BUG_ON(epoch != map->epoch+1);
  1181. ceph_decode_copy(p, &modified, sizeof(modified));
  1182. new_pool_max = ceph_decode_64(p);
  1183. new_flags = ceph_decode_32(p);
  1184. /* full map? */
  1185. ceph_decode_32_safe(p, end, len, e_inval);
  1186. if (len > 0) {
  1187. dout("apply_incremental full map len %d, %p to %p\n",
  1188. len, *p, end);
  1189. return ceph_osdmap_decode(p, min(*p+len, end));
  1190. }
  1191. /* new crush? */
  1192. ceph_decode_32_safe(p, end, len, e_inval);
  1193. if (len > 0) {
  1194. newcrush = crush_decode(*p, min(*p+len, end));
  1195. if (IS_ERR(newcrush)) {
  1196. err = PTR_ERR(newcrush);
  1197. newcrush = NULL;
  1198. goto bad;
  1199. }
  1200. *p += len;
  1201. }
  1202. /* new flags? */
  1203. if (new_flags >= 0)
  1204. map->flags = new_flags;
  1205. if (new_pool_max >= 0)
  1206. map->pool_max = new_pool_max;
  1207. /* new max? */
  1208. ceph_decode_32_safe(p, end, max, e_inval);
  1209. if (max >= 0) {
  1210. err = osdmap_set_max_osd(map, max);
  1211. if (err)
  1212. goto bad;
  1213. }
  1214. map->epoch++;
  1215. map->modified = modified;
  1216. if (newcrush) {
  1217. if (map->crush)
  1218. crush_destroy(map->crush);
  1219. map->crush = newcrush;
  1220. newcrush = NULL;
  1221. }
  1222. /* new_pools */
  1223. err = decode_new_pools(p, end, map);
  1224. if (err)
  1225. goto bad;
  1226. /* new_pool_names */
  1227. err = decode_pool_names(p, end, map);
  1228. if (err)
  1229. goto bad;
  1230. /* old_pool */
  1231. ceph_decode_32_safe(p, end, len, e_inval);
  1232. while (len--) {
  1233. struct ceph_pg_pool_info *pi;
  1234. ceph_decode_64_safe(p, end, pool, e_inval);
  1235. pi = __lookup_pg_pool(&map->pg_pools, pool);
  1236. if (pi)
  1237. __remove_pg_pool(&map->pg_pools, pi);
  1238. }
  1239. /* new_up_client, new_state, new_weight */
  1240. err = decode_new_up_state_weight(p, end, map);
  1241. if (err)
  1242. goto bad;
  1243. /* new_pg_temp */
  1244. err = decode_new_pg_temp(p, end, map);
  1245. if (err)
  1246. goto bad;
  1247. /* new_primary_temp */
  1248. if (struct_v >= 1) {
  1249. err = decode_new_primary_temp(p, end, map);
  1250. if (err)
  1251. goto bad;
  1252. }
  1253. /* new_primary_affinity */
  1254. if (struct_v >= 2) {
  1255. err = decode_new_primary_affinity(p, end, map);
  1256. if (err)
  1257. goto bad;
  1258. }
  1259. /* ignore the rest */
  1260. *p = end;
  1261. dout("inc osdmap epoch %d max_osd %d\n", map->epoch, map->max_osd);
  1262. return map;
  1263. e_inval:
  1264. err = -EINVAL;
  1265. bad:
  1266. pr_err("corrupt inc osdmap (%d) epoch %d off %d (%p of %p-%p)\n",
  1267. err, epoch, (int)(*p - start), *p, start, end);
  1268. print_hex_dump(KERN_DEBUG, "osdmap: ",
  1269. DUMP_PREFIX_OFFSET, 16, 1,
  1270. start, end - start, true);
  1271. if (newcrush)
  1272. crush_destroy(newcrush);
  1273. return ERR_PTR(err);
  1274. }
  1275. void ceph_oloc_copy(struct ceph_object_locator *dest,
  1276. const struct ceph_object_locator *src)
  1277. {
  1278. WARN_ON(!ceph_oloc_empty(dest));
  1279. WARN_ON(dest->pool_ns); /* empty() only covers ->pool */
  1280. dest->pool = src->pool;
  1281. if (src->pool_ns)
  1282. dest->pool_ns = ceph_get_string(src->pool_ns);
  1283. }
  1284. EXPORT_SYMBOL(ceph_oloc_copy);
  1285. void ceph_oloc_destroy(struct ceph_object_locator *oloc)
  1286. {
  1287. ceph_put_string(oloc->pool_ns);
  1288. }
  1289. EXPORT_SYMBOL(ceph_oloc_destroy);
  1290. void ceph_oid_copy(struct ceph_object_id *dest,
  1291. const struct ceph_object_id *src)
  1292. {
  1293. WARN_ON(!ceph_oid_empty(dest));
  1294. if (src->name != src->inline_name) {
  1295. /* very rare, see ceph_object_id definition */
  1296. dest->name = kmalloc(src->name_len + 1,
  1297. GFP_NOIO | __GFP_NOFAIL);
  1298. }
  1299. memcpy(dest->name, src->name, src->name_len + 1);
  1300. dest->name_len = src->name_len;
  1301. }
  1302. EXPORT_SYMBOL(ceph_oid_copy);
  1303. static __printf(2, 0)
  1304. int oid_printf_vargs(struct ceph_object_id *oid, const char *fmt, va_list ap)
  1305. {
  1306. int len;
  1307. WARN_ON(!ceph_oid_empty(oid));
  1308. len = vsnprintf(oid->inline_name, sizeof(oid->inline_name), fmt, ap);
  1309. if (len >= sizeof(oid->inline_name))
  1310. return len;
  1311. oid->name_len = len;
  1312. return 0;
  1313. }
  1314. /*
  1315. * If oid doesn't fit into inline buffer, BUG.
  1316. */
  1317. void ceph_oid_printf(struct ceph_object_id *oid, const char *fmt, ...)
  1318. {
  1319. va_list ap;
  1320. va_start(ap, fmt);
  1321. BUG_ON(oid_printf_vargs(oid, fmt, ap));
  1322. va_end(ap);
  1323. }
  1324. EXPORT_SYMBOL(ceph_oid_printf);
  1325. static __printf(3, 0)
  1326. int oid_aprintf_vargs(struct ceph_object_id *oid, gfp_t gfp,
  1327. const char *fmt, va_list ap)
  1328. {
  1329. va_list aq;
  1330. int len;
  1331. va_copy(aq, ap);
  1332. len = oid_printf_vargs(oid, fmt, aq);
  1333. va_end(aq);
  1334. if (len) {
  1335. char *external_name;
  1336. external_name = kmalloc(len + 1, gfp);
  1337. if (!external_name)
  1338. return -ENOMEM;
  1339. oid->name = external_name;
  1340. WARN_ON(vsnprintf(oid->name, len + 1, fmt, ap) != len);
  1341. oid->name_len = len;
  1342. }
  1343. return 0;
  1344. }
  1345. /*
  1346. * If oid doesn't fit into inline buffer, allocate.
  1347. */
  1348. int ceph_oid_aprintf(struct ceph_object_id *oid, gfp_t gfp,
  1349. const char *fmt, ...)
  1350. {
  1351. va_list ap;
  1352. int ret;
  1353. va_start(ap, fmt);
  1354. ret = oid_aprintf_vargs(oid, gfp, fmt, ap);
  1355. va_end(ap);
  1356. return ret;
  1357. }
  1358. EXPORT_SYMBOL(ceph_oid_aprintf);
  1359. void ceph_oid_destroy(struct ceph_object_id *oid)
  1360. {
  1361. if (oid->name != oid->inline_name)
  1362. kfree(oid->name);
  1363. }
  1364. EXPORT_SYMBOL(ceph_oid_destroy);
  1365. /*
  1366. * osds only
  1367. */
  1368. static bool __osds_equal(const struct ceph_osds *lhs,
  1369. const struct ceph_osds *rhs)
  1370. {
  1371. if (lhs->size == rhs->size &&
  1372. !memcmp(lhs->osds, rhs->osds, rhs->size * sizeof(rhs->osds[0])))
  1373. return true;
  1374. return false;
  1375. }
  1376. /*
  1377. * osds + primary
  1378. */
  1379. static bool osds_equal(const struct ceph_osds *lhs,
  1380. const struct ceph_osds *rhs)
  1381. {
  1382. if (__osds_equal(lhs, rhs) &&
  1383. lhs->primary == rhs->primary)
  1384. return true;
  1385. return false;
  1386. }
  1387. static bool osds_valid(const struct ceph_osds *set)
  1388. {
  1389. /* non-empty set */
  1390. if (set->size > 0 && set->primary >= 0)
  1391. return true;
  1392. /* empty can_shift_osds set */
  1393. if (!set->size && set->primary == -1)
  1394. return true;
  1395. /* empty !can_shift_osds set - all NONE */
  1396. if (set->size > 0 && set->primary == -1) {
  1397. int i;
  1398. for (i = 0; i < set->size; i++) {
  1399. if (set->osds[i] != CRUSH_ITEM_NONE)
  1400. break;
  1401. }
  1402. if (i == set->size)
  1403. return true;
  1404. }
  1405. return false;
  1406. }
  1407. void ceph_osds_copy(struct ceph_osds *dest, const struct ceph_osds *src)
  1408. {
  1409. memcpy(dest->osds, src->osds, src->size * sizeof(src->osds[0]));
  1410. dest->size = src->size;
  1411. dest->primary = src->primary;
  1412. }
  1413. static bool is_split(const struct ceph_pg *pgid,
  1414. u32 old_pg_num,
  1415. u32 new_pg_num)
  1416. {
  1417. int old_bits = calc_bits_of(old_pg_num);
  1418. int old_mask = (1 << old_bits) - 1;
  1419. int n;
  1420. WARN_ON(pgid->seed >= old_pg_num);
  1421. if (new_pg_num <= old_pg_num)
  1422. return false;
  1423. for (n = 1; ; n++) {
  1424. int next_bit = n << (old_bits - 1);
  1425. u32 s = next_bit | pgid->seed;
  1426. if (s < old_pg_num || s == pgid->seed)
  1427. continue;
  1428. if (s >= new_pg_num)
  1429. break;
  1430. s = ceph_stable_mod(s, old_pg_num, old_mask);
  1431. if (s == pgid->seed)
  1432. return true;
  1433. }
  1434. return false;
  1435. }
  1436. bool ceph_is_new_interval(const struct ceph_osds *old_acting,
  1437. const struct ceph_osds *new_acting,
  1438. const struct ceph_osds *old_up,
  1439. const struct ceph_osds *new_up,
  1440. int old_size,
  1441. int new_size,
  1442. int old_min_size,
  1443. int new_min_size,
  1444. u32 old_pg_num,
  1445. u32 new_pg_num,
  1446. bool old_sort_bitwise,
  1447. bool new_sort_bitwise,
  1448. const struct ceph_pg *pgid)
  1449. {
  1450. return !osds_equal(old_acting, new_acting) ||
  1451. !osds_equal(old_up, new_up) ||
  1452. old_size != new_size ||
  1453. old_min_size != new_min_size ||
  1454. is_split(pgid, old_pg_num, new_pg_num) ||
  1455. old_sort_bitwise != new_sort_bitwise;
  1456. }
  1457. static int calc_pg_rank(int osd, const struct ceph_osds *acting)
  1458. {
  1459. int i;
  1460. for (i = 0; i < acting->size; i++) {
  1461. if (acting->osds[i] == osd)
  1462. return i;
  1463. }
  1464. return -1;
  1465. }
  1466. static bool primary_changed(const struct ceph_osds *old_acting,
  1467. const struct ceph_osds *new_acting)
  1468. {
  1469. if (!old_acting->size && !new_acting->size)
  1470. return false; /* both still empty */
  1471. if (!old_acting->size ^ !new_acting->size)
  1472. return true; /* was empty, now not, or vice versa */
  1473. if (old_acting->primary != new_acting->primary)
  1474. return true; /* primary changed */
  1475. if (calc_pg_rank(old_acting->primary, old_acting) !=
  1476. calc_pg_rank(new_acting->primary, new_acting))
  1477. return true;
  1478. return false; /* same primary (tho replicas may have changed) */
  1479. }
  1480. bool ceph_osds_changed(const struct ceph_osds *old_acting,
  1481. const struct ceph_osds *new_acting,
  1482. bool any_change)
  1483. {
  1484. if (primary_changed(old_acting, new_acting))
  1485. return true;
  1486. if (any_change && !__osds_equal(old_acting, new_acting))
  1487. return true;
  1488. return false;
  1489. }
  1490. /*
  1491. * calculate file layout from given offset, length.
  1492. * fill in correct oid, logical length, and object extent
  1493. * offset, length.
  1494. *
  1495. * for now, we write only a single su, until we can
  1496. * pass a stride back to the caller.
  1497. */
  1498. int ceph_calc_file_object_mapping(struct ceph_file_layout *layout,
  1499. u64 off, u64 len,
  1500. u64 *ono,
  1501. u64 *oxoff, u64 *oxlen)
  1502. {
  1503. u32 osize = layout->object_size;
  1504. u32 su = layout->stripe_unit;
  1505. u32 sc = layout->stripe_count;
  1506. u32 bl, stripeno, stripepos, objsetno;
  1507. u32 su_per_object;
  1508. u64 t, su_offset;
  1509. dout("mapping %llu~%llu osize %u fl_su %u\n", off, len,
  1510. osize, su);
  1511. if (su == 0 || sc == 0)
  1512. goto invalid;
  1513. su_per_object = osize / su;
  1514. if (su_per_object == 0)
  1515. goto invalid;
  1516. dout("osize %u / su %u = su_per_object %u\n", osize, su,
  1517. su_per_object);
  1518. if ((su & ~PAGE_MASK) != 0)
  1519. goto invalid;
  1520. /* bl = *off / su; */
  1521. t = off;
  1522. do_div(t, su);
  1523. bl = t;
  1524. dout("off %llu / su %u = bl %u\n", off, su, bl);
  1525. stripeno = bl / sc;
  1526. stripepos = bl % sc;
  1527. objsetno = stripeno / su_per_object;
  1528. *ono = objsetno * sc + stripepos;
  1529. dout("objset %u * sc %u = ono %u\n", objsetno, sc, (unsigned int)*ono);
  1530. /* *oxoff = *off % layout->fl_stripe_unit; # offset in su */
  1531. t = off;
  1532. su_offset = do_div(t, su);
  1533. *oxoff = su_offset + (stripeno % su_per_object) * su;
  1534. /*
  1535. * Calculate the length of the extent being written to the selected
  1536. * object. This is the minimum of the full length requested (len) or
  1537. * the remainder of the current stripe being written to.
  1538. */
  1539. *oxlen = min_t(u64, len, su - su_offset);
  1540. dout(" obj extent %llu~%llu\n", *oxoff, *oxlen);
  1541. return 0;
  1542. invalid:
  1543. dout(" invalid layout\n");
  1544. *ono = 0;
  1545. *oxoff = 0;
  1546. *oxlen = 0;
  1547. return -EINVAL;
  1548. }
  1549. EXPORT_SYMBOL(ceph_calc_file_object_mapping);
  1550. /*
  1551. * Map an object into a PG.
  1552. *
  1553. * Should only be called with target_oid and target_oloc (as opposed to
  1554. * base_oid and base_oloc), since tiering isn't taken into account.
  1555. */
  1556. int ceph_object_locator_to_pg(struct ceph_osdmap *osdmap,
  1557. struct ceph_object_id *oid,
  1558. struct ceph_object_locator *oloc,
  1559. struct ceph_pg *raw_pgid)
  1560. {
  1561. struct ceph_pg_pool_info *pi;
  1562. pi = ceph_pg_pool_by_id(osdmap, oloc->pool);
  1563. if (!pi)
  1564. return -ENOENT;
  1565. if (!oloc->pool_ns) {
  1566. raw_pgid->pool = oloc->pool;
  1567. raw_pgid->seed = ceph_str_hash(pi->object_hash, oid->name,
  1568. oid->name_len);
  1569. dout("%s %s -> raw_pgid %llu.%x\n", __func__, oid->name,
  1570. raw_pgid->pool, raw_pgid->seed);
  1571. } else {
  1572. char stack_buf[256];
  1573. char *buf = stack_buf;
  1574. int nsl = oloc->pool_ns->len;
  1575. size_t total = nsl + 1 + oid->name_len;
  1576. if (total > sizeof(stack_buf)) {
  1577. buf = kmalloc(total, GFP_NOIO);
  1578. if (!buf)
  1579. return -ENOMEM;
  1580. }
  1581. memcpy(buf, oloc->pool_ns->str, nsl);
  1582. buf[nsl] = '\037';
  1583. memcpy(buf + nsl + 1, oid->name, oid->name_len);
  1584. raw_pgid->pool = oloc->pool;
  1585. raw_pgid->seed = ceph_str_hash(pi->object_hash, buf, total);
  1586. if (buf != stack_buf)
  1587. kfree(buf);
  1588. dout("%s %s ns %.*s -> raw_pgid %llu.%x\n", __func__,
  1589. oid->name, nsl, oloc->pool_ns->str,
  1590. raw_pgid->pool, raw_pgid->seed);
  1591. }
  1592. return 0;
  1593. }
  1594. EXPORT_SYMBOL(ceph_object_locator_to_pg);
  1595. /*
  1596. * Map a raw PG (full precision ps) into an actual PG.
  1597. */
  1598. static void raw_pg_to_pg(struct ceph_pg_pool_info *pi,
  1599. const struct ceph_pg *raw_pgid,
  1600. struct ceph_pg *pgid)
  1601. {
  1602. pgid->pool = raw_pgid->pool;
  1603. pgid->seed = ceph_stable_mod(raw_pgid->seed, pi->pg_num,
  1604. pi->pg_num_mask);
  1605. }
  1606. /*
  1607. * Map a raw PG (full precision ps) into a placement ps (placement
  1608. * seed). Include pool id in that value so that different pools don't
  1609. * use the same seeds.
  1610. */
  1611. static u32 raw_pg_to_pps(struct ceph_pg_pool_info *pi,
  1612. const struct ceph_pg *raw_pgid)
  1613. {
  1614. if (pi->flags & CEPH_POOL_FLAG_HASHPSPOOL) {
  1615. /* hash pool id and seed so that pool PGs do not overlap */
  1616. return crush_hash32_2(CRUSH_HASH_RJENKINS1,
  1617. ceph_stable_mod(raw_pgid->seed,
  1618. pi->pgp_num,
  1619. pi->pgp_num_mask),
  1620. raw_pgid->pool);
  1621. } else {
  1622. /*
  1623. * legacy behavior: add ps and pool together. this is
  1624. * not a great approach because the PGs from each pool
  1625. * will overlap on top of each other: 0.5 == 1.4 ==
  1626. * 2.3 == ...
  1627. */
  1628. return ceph_stable_mod(raw_pgid->seed, pi->pgp_num,
  1629. pi->pgp_num_mask) +
  1630. (unsigned)raw_pgid->pool;
  1631. }
  1632. }
  1633. static int do_crush(struct ceph_osdmap *map, int ruleno, int x,
  1634. int *result, int result_max,
  1635. const __u32 *weight, int weight_max)
  1636. {
  1637. int r;
  1638. BUG_ON(result_max > CEPH_PG_MAX_SIZE);
  1639. mutex_lock(&map->crush_scratch_mutex);
  1640. r = crush_do_rule(map->crush, ruleno, x, result, result_max,
  1641. weight, weight_max, map->crush_scratch_ary);
  1642. mutex_unlock(&map->crush_scratch_mutex);
  1643. return r;
  1644. }
  1645. /*
  1646. * Calculate raw set (CRUSH output) for given PG. The result may
  1647. * contain nonexistent OSDs. ->primary is undefined for a raw set.
  1648. *
  1649. * Placement seed (CRUSH input) is returned through @ppps.
  1650. */
  1651. static void pg_to_raw_osds(struct ceph_osdmap *osdmap,
  1652. struct ceph_pg_pool_info *pi,
  1653. const struct ceph_pg *raw_pgid,
  1654. struct ceph_osds *raw,
  1655. u32 *ppps)
  1656. {
  1657. u32 pps = raw_pg_to_pps(pi, raw_pgid);
  1658. int ruleno;
  1659. int len;
  1660. ceph_osds_init(raw);
  1661. if (ppps)
  1662. *ppps = pps;
  1663. ruleno = crush_find_rule(osdmap->crush, pi->crush_ruleset, pi->type,
  1664. pi->size);
  1665. if (ruleno < 0) {
  1666. pr_err("no crush rule: pool %lld ruleset %d type %d size %d\n",
  1667. pi->id, pi->crush_ruleset, pi->type, pi->size);
  1668. return;
  1669. }
  1670. len = do_crush(osdmap, ruleno, pps, raw->osds,
  1671. min_t(int, pi->size, ARRAY_SIZE(raw->osds)),
  1672. osdmap->osd_weight, osdmap->max_osd);
  1673. if (len < 0) {
  1674. pr_err("error %d from crush rule %d: pool %lld ruleset %d type %d size %d\n",
  1675. len, ruleno, pi->id, pi->crush_ruleset, pi->type,
  1676. pi->size);
  1677. return;
  1678. }
  1679. raw->size = len;
  1680. }
  1681. /*
  1682. * Given raw set, calculate up set and up primary. By definition of an
  1683. * up set, the result won't contain nonexistent or down OSDs.
  1684. *
  1685. * This is done in-place - on return @set is the up set. If it's
  1686. * empty, ->primary will remain undefined.
  1687. */
  1688. static void raw_to_up_osds(struct ceph_osdmap *osdmap,
  1689. struct ceph_pg_pool_info *pi,
  1690. struct ceph_osds *set)
  1691. {
  1692. int i;
  1693. /* ->primary is undefined for a raw set */
  1694. BUG_ON(set->primary != -1);
  1695. if (ceph_can_shift_osds(pi)) {
  1696. int removed = 0;
  1697. /* shift left */
  1698. for (i = 0; i < set->size; i++) {
  1699. if (ceph_osd_is_down(osdmap, set->osds[i])) {
  1700. removed++;
  1701. continue;
  1702. }
  1703. if (removed)
  1704. set->osds[i - removed] = set->osds[i];
  1705. }
  1706. set->size -= removed;
  1707. if (set->size > 0)
  1708. set->primary = set->osds[0];
  1709. } else {
  1710. /* set down/dne devices to NONE */
  1711. for (i = set->size - 1; i >= 0; i--) {
  1712. if (ceph_osd_is_down(osdmap, set->osds[i]))
  1713. set->osds[i] = CRUSH_ITEM_NONE;
  1714. else
  1715. set->primary = set->osds[i];
  1716. }
  1717. }
  1718. }
  1719. static void apply_primary_affinity(struct ceph_osdmap *osdmap,
  1720. struct ceph_pg_pool_info *pi,
  1721. u32 pps,
  1722. struct ceph_osds *up)
  1723. {
  1724. int i;
  1725. int pos = -1;
  1726. /*
  1727. * Do we have any non-default primary_affinity values for these
  1728. * osds?
  1729. */
  1730. if (!osdmap->osd_primary_affinity)
  1731. return;
  1732. for (i = 0; i < up->size; i++) {
  1733. int osd = up->osds[i];
  1734. if (osd != CRUSH_ITEM_NONE &&
  1735. osdmap->osd_primary_affinity[osd] !=
  1736. CEPH_OSD_DEFAULT_PRIMARY_AFFINITY) {
  1737. break;
  1738. }
  1739. }
  1740. if (i == up->size)
  1741. return;
  1742. /*
  1743. * Pick the primary. Feed both the seed (for the pg) and the
  1744. * osd into the hash/rng so that a proportional fraction of an
  1745. * osd's pgs get rejected as primary.
  1746. */
  1747. for (i = 0; i < up->size; i++) {
  1748. int osd = up->osds[i];
  1749. u32 aff;
  1750. if (osd == CRUSH_ITEM_NONE)
  1751. continue;
  1752. aff = osdmap->osd_primary_affinity[osd];
  1753. if (aff < CEPH_OSD_MAX_PRIMARY_AFFINITY &&
  1754. (crush_hash32_2(CRUSH_HASH_RJENKINS1,
  1755. pps, osd) >> 16) >= aff) {
  1756. /*
  1757. * We chose not to use this primary. Note it
  1758. * anyway as a fallback in case we don't pick
  1759. * anyone else, but keep looking.
  1760. */
  1761. if (pos < 0)
  1762. pos = i;
  1763. } else {
  1764. pos = i;
  1765. break;
  1766. }
  1767. }
  1768. if (pos < 0)
  1769. return;
  1770. up->primary = up->osds[pos];
  1771. if (ceph_can_shift_osds(pi) && pos > 0) {
  1772. /* move the new primary to the front */
  1773. for (i = pos; i > 0; i--)
  1774. up->osds[i] = up->osds[i - 1];
  1775. up->osds[0] = up->primary;
  1776. }
  1777. }
  1778. /*
  1779. * Get pg_temp and primary_temp mappings for given PG.
  1780. *
  1781. * Note that a PG may have none, only pg_temp, only primary_temp or
  1782. * both pg_temp and primary_temp mappings. This means @temp isn't
  1783. * always a valid OSD set on return: in the "only primary_temp" case,
  1784. * @temp will have its ->primary >= 0 but ->size == 0.
  1785. */
  1786. static void get_temp_osds(struct ceph_osdmap *osdmap,
  1787. struct ceph_pg_pool_info *pi,
  1788. const struct ceph_pg *raw_pgid,
  1789. struct ceph_osds *temp)
  1790. {
  1791. struct ceph_pg pgid;
  1792. struct ceph_pg_mapping *pg;
  1793. int i;
  1794. raw_pg_to_pg(pi, raw_pgid, &pgid);
  1795. ceph_osds_init(temp);
  1796. /* pg_temp? */
  1797. pg = __lookup_pg_mapping(&osdmap->pg_temp, pgid);
  1798. if (pg) {
  1799. for (i = 0; i < pg->pg_temp.len; i++) {
  1800. if (ceph_osd_is_down(osdmap, pg->pg_temp.osds[i])) {
  1801. if (ceph_can_shift_osds(pi))
  1802. continue;
  1803. temp->osds[temp->size++] = CRUSH_ITEM_NONE;
  1804. } else {
  1805. temp->osds[temp->size++] = pg->pg_temp.osds[i];
  1806. }
  1807. }
  1808. /* apply pg_temp's primary */
  1809. for (i = 0; i < temp->size; i++) {
  1810. if (temp->osds[i] != CRUSH_ITEM_NONE) {
  1811. temp->primary = temp->osds[i];
  1812. break;
  1813. }
  1814. }
  1815. }
  1816. /* primary_temp? */
  1817. pg = __lookup_pg_mapping(&osdmap->primary_temp, pgid);
  1818. if (pg)
  1819. temp->primary = pg->primary_temp.osd;
  1820. }
  1821. /*
  1822. * Map a PG to its acting set as well as its up set.
  1823. *
  1824. * Acting set is used for data mapping purposes, while up set can be
  1825. * recorded for detecting interval changes and deciding whether to
  1826. * resend a request.
  1827. */
  1828. void ceph_pg_to_up_acting_osds(struct ceph_osdmap *osdmap,
  1829. const struct ceph_pg *raw_pgid,
  1830. struct ceph_osds *up,
  1831. struct ceph_osds *acting)
  1832. {
  1833. struct ceph_pg_pool_info *pi;
  1834. u32 pps;
  1835. pi = ceph_pg_pool_by_id(osdmap, raw_pgid->pool);
  1836. if (!pi) {
  1837. ceph_osds_init(up);
  1838. ceph_osds_init(acting);
  1839. goto out;
  1840. }
  1841. pg_to_raw_osds(osdmap, pi, raw_pgid, up, &pps);
  1842. raw_to_up_osds(osdmap, pi, up);
  1843. apply_primary_affinity(osdmap, pi, pps, up);
  1844. get_temp_osds(osdmap, pi, raw_pgid, acting);
  1845. if (!acting->size) {
  1846. memcpy(acting->osds, up->osds, up->size * sizeof(up->osds[0]));
  1847. acting->size = up->size;
  1848. if (acting->primary == -1)
  1849. acting->primary = up->primary;
  1850. }
  1851. out:
  1852. WARN_ON(!osds_valid(up) || !osds_valid(acting));
  1853. }
  1854. /*
  1855. * Return acting primary for given PG, or -1 if none.
  1856. */
  1857. int ceph_pg_to_acting_primary(struct ceph_osdmap *osdmap,
  1858. const struct ceph_pg *raw_pgid)
  1859. {
  1860. struct ceph_osds up, acting;
  1861. ceph_pg_to_up_acting_osds(osdmap, raw_pgid, &up, &acting);
  1862. return acting.primary;
  1863. }
  1864. EXPORT_SYMBOL(ceph_pg_to_acting_primary);