osdmap.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/ceph/ceph_debug.h>
  3. #include <linux/module.h>
  4. #include <linux/slab.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, u32 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 struct crush_choose_arg_map *alloc_choose_arg_map(void)
  127. {
  128. struct crush_choose_arg_map *arg_map;
  129. arg_map = kzalloc(sizeof(*arg_map), GFP_NOIO);
  130. if (!arg_map)
  131. return NULL;
  132. RB_CLEAR_NODE(&arg_map->node);
  133. return arg_map;
  134. }
  135. static void free_choose_arg_map(struct crush_choose_arg_map *arg_map)
  136. {
  137. if (arg_map) {
  138. int i, j;
  139. WARN_ON(!RB_EMPTY_NODE(&arg_map->node));
  140. for (i = 0; i < arg_map->size; i++) {
  141. struct crush_choose_arg *arg = &arg_map->args[i];
  142. for (j = 0; j < arg->weight_set_size; j++)
  143. kfree(arg->weight_set[j].weights);
  144. kfree(arg->weight_set);
  145. kfree(arg->ids);
  146. }
  147. kfree(arg_map->args);
  148. kfree(arg_map);
  149. }
  150. }
  151. DEFINE_RB_FUNCS(choose_arg_map, struct crush_choose_arg_map, choose_args_index,
  152. node);
  153. void clear_choose_args(struct crush_map *c)
  154. {
  155. while (!RB_EMPTY_ROOT(&c->choose_args)) {
  156. struct crush_choose_arg_map *arg_map =
  157. rb_entry(rb_first(&c->choose_args),
  158. struct crush_choose_arg_map, node);
  159. erase_choose_arg_map(&c->choose_args, arg_map);
  160. free_choose_arg_map(arg_map);
  161. }
  162. }
  163. static u32 *decode_array_32_alloc(void **p, void *end, u32 *plen)
  164. {
  165. u32 *a = NULL;
  166. u32 len;
  167. int ret;
  168. ceph_decode_32_safe(p, end, len, e_inval);
  169. if (len) {
  170. u32 i;
  171. a = kmalloc_array(len, sizeof(u32), GFP_NOIO);
  172. if (!a) {
  173. ret = -ENOMEM;
  174. goto fail;
  175. }
  176. ceph_decode_need(p, end, len * sizeof(u32), e_inval);
  177. for (i = 0; i < len; i++)
  178. a[i] = ceph_decode_32(p);
  179. }
  180. *plen = len;
  181. return a;
  182. e_inval:
  183. ret = -EINVAL;
  184. fail:
  185. kfree(a);
  186. return ERR_PTR(ret);
  187. }
  188. /*
  189. * Assumes @arg is zero-initialized.
  190. */
  191. static int decode_choose_arg(void **p, void *end, struct crush_choose_arg *arg)
  192. {
  193. int ret;
  194. ceph_decode_32_safe(p, end, arg->weight_set_size, e_inval);
  195. if (arg->weight_set_size) {
  196. u32 i;
  197. arg->weight_set = kmalloc_array(arg->weight_set_size,
  198. sizeof(*arg->weight_set),
  199. GFP_NOIO);
  200. if (!arg->weight_set)
  201. return -ENOMEM;
  202. for (i = 0; i < arg->weight_set_size; i++) {
  203. struct crush_weight_set *w = &arg->weight_set[i];
  204. w->weights = decode_array_32_alloc(p, end, &w->size);
  205. if (IS_ERR(w->weights)) {
  206. ret = PTR_ERR(w->weights);
  207. w->weights = NULL;
  208. return ret;
  209. }
  210. }
  211. }
  212. arg->ids = decode_array_32_alloc(p, end, &arg->ids_size);
  213. if (IS_ERR(arg->ids)) {
  214. ret = PTR_ERR(arg->ids);
  215. arg->ids = NULL;
  216. return ret;
  217. }
  218. return 0;
  219. e_inval:
  220. return -EINVAL;
  221. }
  222. static int decode_choose_args(void **p, void *end, struct crush_map *c)
  223. {
  224. struct crush_choose_arg_map *arg_map = NULL;
  225. u32 num_choose_arg_maps, num_buckets;
  226. int ret;
  227. ceph_decode_32_safe(p, end, num_choose_arg_maps, e_inval);
  228. while (num_choose_arg_maps--) {
  229. arg_map = alloc_choose_arg_map();
  230. if (!arg_map) {
  231. ret = -ENOMEM;
  232. goto fail;
  233. }
  234. ceph_decode_64_safe(p, end, arg_map->choose_args_index,
  235. e_inval);
  236. arg_map->size = c->max_buckets;
  237. arg_map->args = kcalloc(arg_map->size, sizeof(*arg_map->args),
  238. GFP_NOIO);
  239. if (!arg_map->args) {
  240. ret = -ENOMEM;
  241. goto fail;
  242. }
  243. ceph_decode_32_safe(p, end, num_buckets, e_inval);
  244. while (num_buckets--) {
  245. struct crush_choose_arg *arg;
  246. u32 bucket_index;
  247. ceph_decode_32_safe(p, end, bucket_index, e_inval);
  248. if (bucket_index >= arg_map->size)
  249. goto e_inval;
  250. arg = &arg_map->args[bucket_index];
  251. ret = decode_choose_arg(p, end, arg);
  252. if (ret)
  253. goto fail;
  254. if (arg->ids_size &&
  255. arg->ids_size != c->buckets[bucket_index]->size)
  256. goto e_inval;
  257. }
  258. insert_choose_arg_map(&c->choose_args, arg_map);
  259. }
  260. return 0;
  261. e_inval:
  262. ret = -EINVAL;
  263. fail:
  264. free_choose_arg_map(arg_map);
  265. return ret;
  266. }
  267. static void crush_finalize(struct crush_map *c)
  268. {
  269. __s32 b;
  270. /* Space for the array of pointers to per-bucket workspace */
  271. c->working_size = sizeof(struct crush_work) +
  272. c->max_buckets * sizeof(struct crush_work_bucket *);
  273. for (b = 0; b < c->max_buckets; b++) {
  274. if (!c->buckets[b])
  275. continue;
  276. switch (c->buckets[b]->alg) {
  277. default:
  278. /*
  279. * The base case, permutation variables and
  280. * the pointer to the permutation array.
  281. */
  282. c->working_size += sizeof(struct crush_work_bucket);
  283. break;
  284. }
  285. /* Every bucket has a permutation array. */
  286. c->working_size += c->buckets[b]->size * sizeof(__u32);
  287. }
  288. }
  289. static struct crush_map *crush_decode(void *pbyval, void *end)
  290. {
  291. struct crush_map *c;
  292. int err;
  293. int i, j;
  294. void **p = &pbyval;
  295. void *start = pbyval;
  296. u32 magic;
  297. dout("crush_decode %p to %p len %d\n", *p, end, (int)(end - *p));
  298. c = kzalloc(sizeof(*c), GFP_NOFS);
  299. if (c == NULL)
  300. return ERR_PTR(-ENOMEM);
  301. c->choose_args = RB_ROOT;
  302. /* set tunables to default values */
  303. c->choose_local_tries = 2;
  304. c->choose_local_fallback_tries = 5;
  305. c->choose_total_tries = 19;
  306. c->chooseleaf_descend_once = 0;
  307. ceph_decode_need(p, end, 4*sizeof(u32), bad);
  308. magic = ceph_decode_32(p);
  309. if (magic != CRUSH_MAGIC) {
  310. pr_err("crush_decode magic %x != current %x\n",
  311. (unsigned int)magic, (unsigned int)CRUSH_MAGIC);
  312. goto bad;
  313. }
  314. c->max_buckets = ceph_decode_32(p);
  315. c->max_rules = ceph_decode_32(p);
  316. c->max_devices = ceph_decode_32(p);
  317. c->buckets = kcalloc(c->max_buckets, sizeof(*c->buckets), GFP_NOFS);
  318. if (c->buckets == NULL)
  319. goto badmem;
  320. c->rules = kcalloc(c->max_rules, sizeof(*c->rules), GFP_NOFS);
  321. if (c->rules == NULL)
  322. goto badmem;
  323. /* buckets */
  324. for (i = 0; i < c->max_buckets; i++) {
  325. int size = 0;
  326. u32 alg;
  327. struct crush_bucket *b;
  328. ceph_decode_32_safe(p, end, alg, bad);
  329. if (alg == 0) {
  330. c->buckets[i] = NULL;
  331. continue;
  332. }
  333. dout("crush_decode bucket %d off %x %p to %p\n",
  334. i, (int)(*p-start), *p, end);
  335. switch (alg) {
  336. case CRUSH_BUCKET_UNIFORM:
  337. size = sizeof(struct crush_bucket_uniform);
  338. break;
  339. case CRUSH_BUCKET_LIST:
  340. size = sizeof(struct crush_bucket_list);
  341. break;
  342. case CRUSH_BUCKET_TREE:
  343. size = sizeof(struct crush_bucket_tree);
  344. break;
  345. case CRUSH_BUCKET_STRAW:
  346. size = sizeof(struct crush_bucket_straw);
  347. break;
  348. case CRUSH_BUCKET_STRAW2:
  349. size = sizeof(struct crush_bucket_straw2);
  350. break;
  351. default:
  352. goto bad;
  353. }
  354. BUG_ON(size == 0);
  355. b = c->buckets[i] = kzalloc(size, GFP_NOFS);
  356. if (b == NULL)
  357. goto badmem;
  358. ceph_decode_need(p, end, 4*sizeof(u32), bad);
  359. b->id = ceph_decode_32(p);
  360. b->type = ceph_decode_16(p);
  361. b->alg = ceph_decode_8(p);
  362. b->hash = ceph_decode_8(p);
  363. b->weight = ceph_decode_32(p);
  364. b->size = ceph_decode_32(p);
  365. dout("crush_decode bucket size %d off %x %p to %p\n",
  366. b->size, (int)(*p-start), *p, end);
  367. b->items = kcalloc(b->size, sizeof(__s32), GFP_NOFS);
  368. if (b->items == NULL)
  369. goto badmem;
  370. ceph_decode_need(p, end, b->size*sizeof(u32), bad);
  371. for (j = 0; j < b->size; j++)
  372. b->items[j] = ceph_decode_32(p);
  373. switch (b->alg) {
  374. case CRUSH_BUCKET_UNIFORM:
  375. err = crush_decode_uniform_bucket(p, end,
  376. (struct crush_bucket_uniform *)b);
  377. if (err < 0)
  378. goto fail;
  379. break;
  380. case CRUSH_BUCKET_LIST:
  381. err = crush_decode_list_bucket(p, end,
  382. (struct crush_bucket_list *)b);
  383. if (err < 0)
  384. goto fail;
  385. break;
  386. case CRUSH_BUCKET_TREE:
  387. err = crush_decode_tree_bucket(p, end,
  388. (struct crush_bucket_tree *)b);
  389. if (err < 0)
  390. goto fail;
  391. break;
  392. case CRUSH_BUCKET_STRAW:
  393. err = crush_decode_straw_bucket(p, end,
  394. (struct crush_bucket_straw *)b);
  395. if (err < 0)
  396. goto fail;
  397. break;
  398. case CRUSH_BUCKET_STRAW2:
  399. err = crush_decode_straw2_bucket(p, end,
  400. (struct crush_bucket_straw2 *)b);
  401. if (err < 0)
  402. goto fail;
  403. break;
  404. }
  405. }
  406. /* rules */
  407. dout("rule vec is %p\n", c->rules);
  408. for (i = 0; i < c->max_rules; i++) {
  409. u32 yes;
  410. struct crush_rule *r;
  411. ceph_decode_32_safe(p, end, yes, bad);
  412. if (!yes) {
  413. dout("crush_decode NO rule %d off %x %p to %p\n",
  414. i, (int)(*p-start), *p, end);
  415. c->rules[i] = NULL;
  416. continue;
  417. }
  418. dout("crush_decode rule %d off %x %p to %p\n",
  419. i, (int)(*p-start), *p, end);
  420. /* len */
  421. ceph_decode_32_safe(p, end, yes, bad);
  422. #if BITS_PER_LONG == 32
  423. if (yes > (ULONG_MAX - sizeof(*r))
  424. / sizeof(struct crush_rule_step))
  425. goto bad;
  426. #endif
  427. r = c->rules[i] = kmalloc(sizeof(*r) +
  428. yes*sizeof(struct crush_rule_step),
  429. GFP_NOFS);
  430. if (r == NULL)
  431. goto badmem;
  432. dout(" rule %d is at %p\n", i, r);
  433. r->len = yes;
  434. ceph_decode_copy_safe(p, end, &r->mask, 4, bad); /* 4 u8's */
  435. ceph_decode_need(p, end, r->len*3*sizeof(u32), bad);
  436. for (j = 0; j < r->len; j++) {
  437. r->steps[j].op = ceph_decode_32(p);
  438. r->steps[j].arg1 = ceph_decode_32(p);
  439. r->steps[j].arg2 = ceph_decode_32(p);
  440. }
  441. }
  442. ceph_decode_skip_map(p, end, 32, string, bad); /* type_map */
  443. ceph_decode_skip_map(p, end, 32, string, bad); /* name_map */
  444. ceph_decode_skip_map(p, end, 32, string, bad); /* rule_name_map */
  445. /* tunables */
  446. ceph_decode_need(p, end, 3*sizeof(u32), done);
  447. c->choose_local_tries = ceph_decode_32(p);
  448. c->choose_local_fallback_tries = ceph_decode_32(p);
  449. c->choose_total_tries = ceph_decode_32(p);
  450. dout("crush decode tunable choose_local_tries = %d\n",
  451. c->choose_local_tries);
  452. dout("crush decode tunable choose_local_fallback_tries = %d\n",
  453. c->choose_local_fallback_tries);
  454. dout("crush decode tunable choose_total_tries = %d\n",
  455. c->choose_total_tries);
  456. ceph_decode_need(p, end, sizeof(u32), done);
  457. c->chooseleaf_descend_once = ceph_decode_32(p);
  458. dout("crush decode tunable chooseleaf_descend_once = %d\n",
  459. c->chooseleaf_descend_once);
  460. ceph_decode_need(p, end, sizeof(u8), done);
  461. c->chooseleaf_vary_r = ceph_decode_8(p);
  462. dout("crush decode tunable chooseleaf_vary_r = %d\n",
  463. c->chooseleaf_vary_r);
  464. /* skip straw_calc_version, allowed_bucket_algs */
  465. ceph_decode_need(p, end, sizeof(u8) + sizeof(u32), done);
  466. *p += sizeof(u8) + sizeof(u32);
  467. ceph_decode_need(p, end, sizeof(u8), done);
  468. c->chooseleaf_stable = ceph_decode_8(p);
  469. dout("crush decode tunable chooseleaf_stable = %d\n",
  470. c->chooseleaf_stable);
  471. if (*p != end) {
  472. /* class_map */
  473. ceph_decode_skip_map(p, end, 32, 32, bad);
  474. /* class_name */
  475. ceph_decode_skip_map(p, end, 32, string, bad);
  476. /* class_bucket */
  477. ceph_decode_skip_map_of_map(p, end, 32, 32, 32, bad);
  478. }
  479. if (*p != end) {
  480. err = decode_choose_args(p, end, c);
  481. if (err)
  482. goto fail;
  483. }
  484. done:
  485. crush_finalize(c);
  486. dout("crush_decode success\n");
  487. return c;
  488. badmem:
  489. err = -ENOMEM;
  490. fail:
  491. dout("crush_decode fail %d\n", err);
  492. crush_destroy(c);
  493. return ERR_PTR(err);
  494. bad:
  495. err = -EINVAL;
  496. goto fail;
  497. }
  498. int ceph_pg_compare(const struct ceph_pg *lhs, const struct ceph_pg *rhs)
  499. {
  500. if (lhs->pool < rhs->pool)
  501. return -1;
  502. if (lhs->pool > rhs->pool)
  503. return 1;
  504. if (lhs->seed < rhs->seed)
  505. return -1;
  506. if (lhs->seed > rhs->seed)
  507. return 1;
  508. return 0;
  509. }
  510. int ceph_spg_compare(const struct ceph_spg *lhs, const struct ceph_spg *rhs)
  511. {
  512. int ret;
  513. ret = ceph_pg_compare(&lhs->pgid, &rhs->pgid);
  514. if (ret)
  515. return ret;
  516. if (lhs->shard < rhs->shard)
  517. return -1;
  518. if (lhs->shard > rhs->shard)
  519. return 1;
  520. return 0;
  521. }
  522. static struct ceph_pg_mapping *alloc_pg_mapping(size_t payload_len)
  523. {
  524. struct ceph_pg_mapping *pg;
  525. pg = kmalloc(sizeof(*pg) + payload_len, GFP_NOIO);
  526. if (!pg)
  527. return NULL;
  528. RB_CLEAR_NODE(&pg->node);
  529. return pg;
  530. }
  531. static void free_pg_mapping(struct ceph_pg_mapping *pg)
  532. {
  533. WARN_ON(!RB_EMPTY_NODE(&pg->node));
  534. kfree(pg);
  535. }
  536. /*
  537. * rbtree of pg_mapping for handling pg_temp (explicit mapping of pgid
  538. * to a set of osds) and primary_temp (explicit primary setting)
  539. */
  540. DEFINE_RB_FUNCS2(pg_mapping, struct ceph_pg_mapping, pgid, ceph_pg_compare,
  541. RB_BYPTR, const struct ceph_pg *, node)
  542. /*
  543. * rbtree of pg pool info
  544. */
  545. static int __insert_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *new)
  546. {
  547. struct rb_node **p = &root->rb_node;
  548. struct rb_node *parent = NULL;
  549. struct ceph_pg_pool_info *pi = NULL;
  550. while (*p) {
  551. parent = *p;
  552. pi = rb_entry(parent, struct ceph_pg_pool_info, node);
  553. if (new->id < pi->id)
  554. p = &(*p)->rb_left;
  555. else if (new->id > pi->id)
  556. p = &(*p)->rb_right;
  557. else
  558. return -EEXIST;
  559. }
  560. rb_link_node(&new->node, parent, p);
  561. rb_insert_color(&new->node, root);
  562. return 0;
  563. }
  564. static struct ceph_pg_pool_info *__lookup_pg_pool(struct rb_root *root, u64 id)
  565. {
  566. struct ceph_pg_pool_info *pi;
  567. struct rb_node *n = root->rb_node;
  568. while (n) {
  569. pi = rb_entry(n, struct ceph_pg_pool_info, node);
  570. if (id < pi->id)
  571. n = n->rb_left;
  572. else if (id > pi->id)
  573. n = n->rb_right;
  574. else
  575. return pi;
  576. }
  577. return NULL;
  578. }
  579. struct ceph_pg_pool_info *ceph_pg_pool_by_id(struct ceph_osdmap *map, u64 id)
  580. {
  581. return __lookup_pg_pool(&map->pg_pools, id);
  582. }
  583. const char *ceph_pg_pool_name_by_id(struct ceph_osdmap *map, u64 id)
  584. {
  585. struct ceph_pg_pool_info *pi;
  586. if (id == CEPH_NOPOOL)
  587. return NULL;
  588. if (WARN_ON_ONCE(id > (u64) INT_MAX))
  589. return NULL;
  590. pi = __lookup_pg_pool(&map->pg_pools, (int) id);
  591. return pi ? pi->name : NULL;
  592. }
  593. EXPORT_SYMBOL(ceph_pg_pool_name_by_id);
  594. int ceph_pg_poolid_by_name(struct ceph_osdmap *map, const char *name)
  595. {
  596. struct rb_node *rbp;
  597. for (rbp = rb_first(&map->pg_pools); rbp; rbp = rb_next(rbp)) {
  598. struct ceph_pg_pool_info *pi =
  599. rb_entry(rbp, struct ceph_pg_pool_info, node);
  600. if (pi->name && strcmp(pi->name, name) == 0)
  601. return pi->id;
  602. }
  603. return -ENOENT;
  604. }
  605. EXPORT_SYMBOL(ceph_pg_poolid_by_name);
  606. u64 ceph_pg_pool_flags(struct ceph_osdmap *map, u64 id)
  607. {
  608. struct ceph_pg_pool_info *pi;
  609. pi = __lookup_pg_pool(&map->pg_pools, id);
  610. return pi ? pi->flags : 0;
  611. }
  612. EXPORT_SYMBOL(ceph_pg_pool_flags);
  613. static void __remove_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *pi)
  614. {
  615. rb_erase(&pi->node, root);
  616. kfree(pi->name);
  617. kfree(pi);
  618. }
  619. static int decode_pool(void **p, void *end, struct ceph_pg_pool_info *pi)
  620. {
  621. u8 ev, cv;
  622. unsigned len, num;
  623. void *pool_end;
  624. ceph_decode_need(p, end, 2 + 4, bad);
  625. ev = ceph_decode_8(p); /* encoding version */
  626. cv = ceph_decode_8(p); /* compat version */
  627. if (ev < 5) {
  628. pr_warn("got v %d < 5 cv %d of ceph_pg_pool\n", ev, cv);
  629. return -EINVAL;
  630. }
  631. if (cv > 9) {
  632. pr_warn("got v %d cv %d > 9 of ceph_pg_pool\n", ev, cv);
  633. return -EINVAL;
  634. }
  635. len = ceph_decode_32(p);
  636. ceph_decode_need(p, end, len, bad);
  637. pool_end = *p + len;
  638. pi->type = ceph_decode_8(p);
  639. pi->size = ceph_decode_8(p);
  640. pi->crush_ruleset = ceph_decode_8(p);
  641. pi->object_hash = ceph_decode_8(p);
  642. pi->pg_num = ceph_decode_32(p);
  643. pi->pgp_num = ceph_decode_32(p);
  644. *p += 4 + 4; /* skip lpg* */
  645. *p += 4; /* skip last_change */
  646. *p += 8 + 4; /* skip snap_seq, snap_epoch */
  647. /* skip snaps */
  648. num = ceph_decode_32(p);
  649. while (num--) {
  650. *p += 8; /* snapid key */
  651. *p += 1 + 1; /* versions */
  652. len = ceph_decode_32(p);
  653. *p += len;
  654. }
  655. /* skip removed_snaps */
  656. num = ceph_decode_32(p);
  657. *p += num * (8 + 8);
  658. *p += 8; /* skip auid */
  659. pi->flags = ceph_decode_64(p);
  660. *p += 4; /* skip crash_replay_interval */
  661. if (ev >= 7)
  662. pi->min_size = ceph_decode_8(p);
  663. else
  664. pi->min_size = pi->size - pi->size / 2;
  665. if (ev >= 8)
  666. *p += 8 + 8; /* skip quota_max_* */
  667. if (ev >= 9) {
  668. /* skip tiers */
  669. num = ceph_decode_32(p);
  670. *p += num * 8;
  671. *p += 8; /* skip tier_of */
  672. *p += 1; /* skip cache_mode */
  673. pi->read_tier = ceph_decode_64(p);
  674. pi->write_tier = ceph_decode_64(p);
  675. } else {
  676. pi->read_tier = -1;
  677. pi->write_tier = -1;
  678. }
  679. if (ev >= 10) {
  680. /* skip properties */
  681. num = ceph_decode_32(p);
  682. while (num--) {
  683. len = ceph_decode_32(p);
  684. *p += len; /* key */
  685. len = ceph_decode_32(p);
  686. *p += len; /* val */
  687. }
  688. }
  689. if (ev >= 11) {
  690. /* skip hit_set_params */
  691. *p += 1 + 1; /* versions */
  692. len = ceph_decode_32(p);
  693. *p += len;
  694. *p += 4; /* skip hit_set_period */
  695. *p += 4; /* skip hit_set_count */
  696. }
  697. if (ev >= 12)
  698. *p += 4; /* skip stripe_width */
  699. if (ev >= 13) {
  700. *p += 8; /* skip target_max_bytes */
  701. *p += 8; /* skip target_max_objects */
  702. *p += 4; /* skip cache_target_dirty_ratio_micro */
  703. *p += 4; /* skip cache_target_full_ratio_micro */
  704. *p += 4; /* skip cache_min_flush_age */
  705. *p += 4; /* skip cache_min_evict_age */
  706. }
  707. if (ev >= 14) {
  708. /* skip erasure_code_profile */
  709. len = ceph_decode_32(p);
  710. *p += len;
  711. }
  712. /*
  713. * last_force_op_resend_preluminous, will be overridden if the
  714. * map was encoded with RESEND_ON_SPLIT
  715. */
  716. if (ev >= 15)
  717. pi->last_force_request_resend = ceph_decode_32(p);
  718. else
  719. pi->last_force_request_resend = 0;
  720. if (ev >= 16)
  721. *p += 4; /* skip min_read_recency_for_promote */
  722. if (ev >= 17)
  723. *p += 8; /* skip expected_num_objects */
  724. if (ev >= 19)
  725. *p += 4; /* skip cache_target_dirty_high_ratio_micro */
  726. if (ev >= 20)
  727. *p += 4; /* skip min_write_recency_for_promote */
  728. if (ev >= 21)
  729. *p += 1; /* skip use_gmt_hitset */
  730. if (ev >= 22)
  731. *p += 1; /* skip fast_read */
  732. if (ev >= 23) {
  733. *p += 4; /* skip hit_set_grade_decay_rate */
  734. *p += 4; /* skip hit_set_search_last_n */
  735. }
  736. if (ev >= 24) {
  737. /* skip opts */
  738. *p += 1 + 1; /* versions */
  739. len = ceph_decode_32(p);
  740. *p += len;
  741. }
  742. if (ev >= 25)
  743. pi->last_force_request_resend = ceph_decode_32(p);
  744. /* ignore the rest */
  745. *p = pool_end;
  746. calc_pg_masks(pi);
  747. return 0;
  748. bad:
  749. return -EINVAL;
  750. }
  751. static int decode_pool_names(void **p, void *end, struct ceph_osdmap *map)
  752. {
  753. struct ceph_pg_pool_info *pi;
  754. u32 num, len;
  755. u64 pool;
  756. ceph_decode_32_safe(p, end, num, bad);
  757. dout(" %d pool names\n", num);
  758. while (num--) {
  759. ceph_decode_64_safe(p, end, pool, bad);
  760. ceph_decode_32_safe(p, end, len, bad);
  761. dout(" pool %llu len %d\n", pool, len);
  762. ceph_decode_need(p, end, len, bad);
  763. pi = __lookup_pg_pool(&map->pg_pools, pool);
  764. if (pi) {
  765. char *name = kstrndup(*p, len, GFP_NOFS);
  766. if (!name)
  767. return -ENOMEM;
  768. kfree(pi->name);
  769. pi->name = name;
  770. dout(" name is %s\n", pi->name);
  771. }
  772. *p += len;
  773. }
  774. return 0;
  775. bad:
  776. return -EINVAL;
  777. }
  778. /*
  779. * osd map
  780. */
  781. struct ceph_osdmap *ceph_osdmap_alloc(void)
  782. {
  783. struct ceph_osdmap *map;
  784. map = kzalloc(sizeof(*map), GFP_NOIO);
  785. if (!map)
  786. return NULL;
  787. map->pg_pools = RB_ROOT;
  788. map->pool_max = -1;
  789. map->pg_temp = RB_ROOT;
  790. map->primary_temp = RB_ROOT;
  791. map->pg_upmap = RB_ROOT;
  792. map->pg_upmap_items = RB_ROOT;
  793. mutex_init(&map->crush_workspace_mutex);
  794. return map;
  795. }
  796. void ceph_osdmap_destroy(struct ceph_osdmap *map)
  797. {
  798. dout("osdmap_destroy %p\n", map);
  799. if (map->crush)
  800. crush_destroy(map->crush);
  801. while (!RB_EMPTY_ROOT(&map->pg_temp)) {
  802. struct ceph_pg_mapping *pg =
  803. rb_entry(rb_first(&map->pg_temp),
  804. struct ceph_pg_mapping, node);
  805. erase_pg_mapping(&map->pg_temp, pg);
  806. free_pg_mapping(pg);
  807. }
  808. while (!RB_EMPTY_ROOT(&map->primary_temp)) {
  809. struct ceph_pg_mapping *pg =
  810. rb_entry(rb_first(&map->primary_temp),
  811. struct ceph_pg_mapping, node);
  812. erase_pg_mapping(&map->primary_temp, pg);
  813. free_pg_mapping(pg);
  814. }
  815. while (!RB_EMPTY_ROOT(&map->pg_upmap)) {
  816. struct ceph_pg_mapping *pg =
  817. rb_entry(rb_first(&map->pg_upmap),
  818. struct ceph_pg_mapping, node);
  819. rb_erase(&pg->node, &map->pg_upmap);
  820. kfree(pg);
  821. }
  822. while (!RB_EMPTY_ROOT(&map->pg_upmap_items)) {
  823. struct ceph_pg_mapping *pg =
  824. rb_entry(rb_first(&map->pg_upmap_items),
  825. struct ceph_pg_mapping, node);
  826. rb_erase(&pg->node, &map->pg_upmap_items);
  827. kfree(pg);
  828. }
  829. while (!RB_EMPTY_ROOT(&map->pg_pools)) {
  830. struct ceph_pg_pool_info *pi =
  831. rb_entry(rb_first(&map->pg_pools),
  832. struct ceph_pg_pool_info, node);
  833. __remove_pg_pool(&map->pg_pools, pi);
  834. }
  835. kfree(map->osd_state);
  836. kfree(map->osd_weight);
  837. kfree(map->osd_addr);
  838. kfree(map->osd_primary_affinity);
  839. kfree(map->crush_workspace);
  840. kfree(map);
  841. }
  842. /*
  843. * Adjust max_osd value, (re)allocate arrays.
  844. *
  845. * The new elements are properly initialized.
  846. */
  847. static int osdmap_set_max_osd(struct ceph_osdmap *map, int max)
  848. {
  849. u32 *state;
  850. u32 *weight;
  851. struct ceph_entity_addr *addr;
  852. int i;
  853. state = krealloc(map->osd_state, max*sizeof(*state), GFP_NOFS);
  854. if (!state)
  855. return -ENOMEM;
  856. map->osd_state = state;
  857. weight = krealloc(map->osd_weight, max*sizeof(*weight), GFP_NOFS);
  858. if (!weight)
  859. return -ENOMEM;
  860. map->osd_weight = weight;
  861. addr = krealloc(map->osd_addr, max*sizeof(*addr), GFP_NOFS);
  862. if (!addr)
  863. return -ENOMEM;
  864. map->osd_addr = addr;
  865. for (i = map->max_osd; i < max; i++) {
  866. map->osd_state[i] = 0;
  867. map->osd_weight[i] = CEPH_OSD_OUT;
  868. memset(map->osd_addr + i, 0, sizeof(*map->osd_addr));
  869. }
  870. if (map->osd_primary_affinity) {
  871. u32 *affinity;
  872. affinity = krealloc(map->osd_primary_affinity,
  873. max*sizeof(*affinity), GFP_NOFS);
  874. if (!affinity)
  875. return -ENOMEM;
  876. map->osd_primary_affinity = affinity;
  877. for (i = map->max_osd; i < max; i++)
  878. map->osd_primary_affinity[i] =
  879. CEPH_OSD_DEFAULT_PRIMARY_AFFINITY;
  880. }
  881. map->max_osd = max;
  882. return 0;
  883. }
  884. static int osdmap_set_crush(struct ceph_osdmap *map, struct crush_map *crush)
  885. {
  886. void *workspace;
  887. size_t work_size;
  888. if (IS_ERR(crush))
  889. return PTR_ERR(crush);
  890. work_size = crush_work_size(crush, CEPH_PG_MAX_SIZE);
  891. dout("%s work_size %zu bytes\n", __func__, work_size);
  892. workspace = kmalloc(work_size, GFP_NOIO);
  893. if (!workspace) {
  894. crush_destroy(crush);
  895. return -ENOMEM;
  896. }
  897. crush_init_workspace(crush, workspace);
  898. if (map->crush)
  899. crush_destroy(map->crush);
  900. kfree(map->crush_workspace);
  901. map->crush = crush;
  902. map->crush_workspace = workspace;
  903. return 0;
  904. }
  905. #define OSDMAP_WRAPPER_COMPAT_VER 7
  906. #define OSDMAP_CLIENT_DATA_COMPAT_VER 1
  907. /*
  908. * Return 0 or error. On success, *v is set to 0 for old (v6) osdmaps,
  909. * to struct_v of the client_data section for new (v7 and above)
  910. * osdmaps.
  911. */
  912. static int get_osdmap_client_data_v(void **p, void *end,
  913. const char *prefix, u8 *v)
  914. {
  915. u8 struct_v;
  916. ceph_decode_8_safe(p, end, struct_v, e_inval);
  917. if (struct_v >= 7) {
  918. u8 struct_compat;
  919. ceph_decode_8_safe(p, end, struct_compat, e_inval);
  920. if (struct_compat > OSDMAP_WRAPPER_COMPAT_VER) {
  921. pr_warn("got v %d cv %d > %d of %s ceph_osdmap\n",
  922. struct_v, struct_compat,
  923. OSDMAP_WRAPPER_COMPAT_VER, prefix);
  924. return -EINVAL;
  925. }
  926. *p += 4; /* ignore wrapper struct_len */
  927. ceph_decode_8_safe(p, end, struct_v, e_inval);
  928. ceph_decode_8_safe(p, end, struct_compat, e_inval);
  929. if (struct_compat > OSDMAP_CLIENT_DATA_COMPAT_VER) {
  930. pr_warn("got v %d cv %d > %d of %s ceph_osdmap client data\n",
  931. struct_v, struct_compat,
  932. OSDMAP_CLIENT_DATA_COMPAT_VER, prefix);
  933. return -EINVAL;
  934. }
  935. *p += 4; /* ignore client data struct_len */
  936. } else {
  937. u16 version;
  938. *p -= 1;
  939. ceph_decode_16_safe(p, end, version, e_inval);
  940. if (version < 6) {
  941. pr_warn("got v %d < 6 of %s ceph_osdmap\n",
  942. version, prefix);
  943. return -EINVAL;
  944. }
  945. /* old osdmap enconding */
  946. struct_v = 0;
  947. }
  948. *v = struct_v;
  949. return 0;
  950. e_inval:
  951. return -EINVAL;
  952. }
  953. static int __decode_pools(void **p, void *end, struct ceph_osdmap *map,
  954. bool incremental)
  955. {
  956. u32 n;
  957. ceph_decode_32_safe(p, end, n, e_inval);
  958. while (n--) {
  959. struct ceph_pg_pool_info *pi;
  960. u64 pool;
  961. int ret;
  962. ceph_decode_64_safe(p, end, pool, e_inval);
  963. pi = __lookup_pg_pool(&map->pg_pools, pool);
  964. if (!incremental || !pi) {
  965. pi = kzalloc(sizeof(*pi), GFP_NOFS);
  966. if (!pi)
  967. return -ENOMEM;
  968. pi->id = pool;
  969. ret = __insert_pg_pool(&map->pg_pools, pi);
  970. if (ret) {
  971. kfree(pi);
  972. return ret;
  973. }
  974. }
  975. ret = decode_pool(p, end, pi);
  976. if (ret)
  977. return ret;
  978. }
  979. return 0;
  980. e_inval:
  981. return -EINVAL;
  982. }
  983. static int decode_pools(void **p, void *end, struct ceph_osdmap *map)
  984. {
  985. return __decode_pools(p, end, map, false);
  986. }
  987. static int decode_new_pools(void **p, void *end, struct ceph_osdmap *map)
  988. {
  989. return __decode_pools(p, end, map, true);
  990. }
  991. typedef struct ceph_pg_mapping *(*decode_mapping_fn_t)(void **, void *, bool);
  992. static int decode_pg_mapping(void **p, void *end, struct rb_root *mapping_root,
  993. decode_mapping_fn_t fn, bool incremental)
  994. {
  995. u32 n;
  996. WARN_ON(!incremental && !fn);
  997. ceph_decode_32_safe(p, end, n, e_inval);
  998. while (n--) {
  999. struct ceph_pg_mapping *pg;
  1000. struct ceph_pg pgid;
  1001. int ret;
  1002. ret = ceph_decode_pgid(p, end, &pgid);
  1003. if (ret)
  1004. return ret;
  1005. pg = lookup_pg_mapping(mapping_root, &pgid);
  1006. if (pg) {
  1007. WARN_ON(!incremental);
  1008. erase_pg_mapping(mapping_root, pg);
  1009. free_pg_mapping(pg);
  1010. }
  1011. if (fn) {
  1012. pg = fn(p, end, incremental);
  1013. if (IS_ERR(pg))
  1014. return PTR_ERR(pg);
  1015. if (pg) {
  1016. pg->pgid = pgid; /* struct */
  1017. insert_pg_mapping(mapping_root, pg);
  1018. }
  1019. }
  1020. }
  1021. return 0;
  1022. e_inval:
  1023. return -EINVAL;
  1024. }
  1025. static struct ceph_pg_mapping *__decode_pg_temp(void **p, void *end,
  1026. bool incremental)
  1027. {
  1028. struct ceph_pg_mapping *pg;
  1029. u32 len, i;
  1030. ceph_decode_32_safe(p, end, len, e_inval);
  1031. if (len == 0 && incremental)
  1032. return NULL; /* new_pg_temp: [] to remove */
  1033. if (len > (SIZE_MAX - sizeof(*pg)) / sizeof(u32))
  1034. return ERR_PTR(-EINVAL);
  1035. ceph_decode_need(p, end, len * sizeof(u32), e_inval);
  1036. pg = alloc_pg_mapping(len * sizeof(u32));
  1037. if (!pg)
  1038. return ERR_PTR(-ENOMEM);
  1039. pg->pg_temp.len = len;
  1040. for (i = 0; i < len; i++)
  1041. pg->pg_temp.osds[i] = ceph_decode_32(p);
  1042. return pg;
  1043. e_inval:
  1044. return ERR_PTR(-EINVAL);
  1045. }
  1046. static int decode_pg_temp(void **p, void *end, struct ceph_osdmap *map)
  1047. {
  1048. return decode_pg_mapping(p, end, &map->pg_temp, __decode_pg_temp,
  1049. false);
  1050. }
  1051. static int decode_new_pg_temp(void **p, void *end, struct ceph_osdmap *map)
  1052. {
  1053. return decode_pg_mapping(p, end, &map->pg_temp, __decode_pg_temp,
  1054. true);
  1055. }
  1056. static struct ceph_pg_mapping *__decode_primary_temp(void **p, void *end,
  1057. bool incremental)
  1058. {
  1059. struct ceph_pg_mapping *pg;
  1060. u32 osd;
  1061. ceph_decode_32_safe(p, end, osd, e_inval);
  1062. if (osd == (u32)-1 && incremental)
  1063. return NULL; /* new_primary_temp: -1 to remove */
  1064. pg = alloc_pg_mapping(0);
  1065. if (!pg)
  1066. return ERR_PTR(-ENOMEM);
  1067. pg->primary_temp.osd = osd;
  1068. return pg;
  1069. e_inval:
  1070. return ERR_PTR(-EINVAL);
  1071. }
  1072. static int decode_primary_temp(void **p, void *end, struct ceph_osdmap *map)
  1073. {
  1074. return decode_pg_mapping(p, end, &map->primary_temp,
  1075. __decode_primary_temp, false);
  1076. }
  1077. static int decode_new_primary_temp(void **p, void *end,
  1078. struct ceph_osdmap *map)
  1079. {
  1080. return decode_pg_mapping(p, end, &map->primary_temp,
  1081. __decode_primary_temp, true);
  1082. }
  1083. u32 ceph_get_primary_affinity(struct ceph_osdmap *map, int osd)
  1084. {
  1085. BUG_ON(osd >= map->max_osd);
  1086. if (!map->osd_primary_affinity)
  1087. return CEPH_OSD_DEFAULT_PRIMARY_AFFINITY;
  1088. return map->osd_primary_affinity[osd];
  1089. }
  1090. static int set_primary_affinity(struct ceph_osdmap *map, int osd, u32 aff)
  1091. {
  1092. BUG_ON(osd >= map->max_osd);
  1093. if (!map->osd_primary_affinity) {
  1094. int i;
  1095. map->osd_primary_affinity = kmalloc_array(map->max_osd,
  1096. sizeof(u32),
  1097. GFP_NOFS);
  1098. if (!map->osd_primary_affinity)
  1099. return -ENOMEM;
  1100. for (i = 0; i < map->max_osd; i++)
  1101. map->osd_primary_affinity[i] =
  1102. CEPH_OSD_DEFAULT_PRIMARY_AFFINITY;
  1103. }
  1104. map->osd_primary_affinity[osd] = aff;
  1105. return 0;
  1106. }
  1107. static int decode_primary_affinity(void **p, void *end,
  1108. struct ceph_osdmap *map)
  1109. {
  1110. u32 len, i;
  1111. ceph_decode_32_safe(p, end, len, e_inval);
  1112. if (len == 0) {
  1113. kfree(map->osd_primary_affinity);
  1114. map->osd_primary_affinity = NULL;
  1115. return 0;
  1116. }
  1117. if (len != map->max_osd)
  1118. goto e_inval;
  1119. ceph_decode_need(p, end, map->max_osd*sizeof(u32), e_inval);
  1120. for (i = 0; i < map->max_osd; i++) {
  1121. int ret;
  1122. ret = set_primary_affinity(map, i, ceph_decode_32(p));
  1123. if (ret)
  1124. return ret;
  1125. }
  1126. return 0;
  1127. e_inval:
  1128. return -EINVAL;
  1129. }
  1130. static int decode_new_primary_affinity(void **p, void *end,
  1131. struct ceph_osdmap *map)
  1132. {
  1133. u32 n;
  1134. ceph_decode_32_safe(p, end, n, e_inval);
  1135. while (n--) {
  1136. u32 osd, aff;
  1137. int ret;
  1138. ceph_decode_32_safe(p, end, osd, e_inval);
  1139. ceph_decode_32_safe(p, end, aff, e_inval);
  1140. ret = set_primary_affinity(map, osd, aff);
  1141. if (ret)
  1142. return ret;
  1143. pr_info("osd%d primary-affinity 0x%x\n", osd, aff);
  1144. }
  1145. return 0;
  1146. e_inval:
  1147. return -EINVAL;
  1148. }
  1149. static struct ceph_pg_mapping *__decode_pg_upmap(void **p, void *end,
  1150. bool __unused)
  1151. {
  1152. return __decode_pg_temp(p, end, false);
  1153. }
  1154. static int decode_pg_upmap(void **p, void *end, struct ceph_osdmap *map)
  1155. {
  1156. return decode_pg_mapping(p, end, &map->pg_upmap, __decode_pg_upmap,
  1157. false);
  1158. }
  1159. static int decode_new_pg_upmap(void **p, void *end, struct ceph_osdmap *map)
  1160. {
  1161. return decode_pg_mapping(p, end, &map->pg_upmap, __decode_pg_upmap,
  1162. true);
  1163. }
  1164. static int decode_old_pg_upmap(void **p, void *end, struct ceph_osdmap *map)
  1165. {
  1166. return decode_pg_mapping(p, end, &map->pg_upmap, NULL, true);
  1167. }
  1168. static struct ceph_pg_mapping *__decode_pg_upmap_items(void **p, void *end,
  1169. bool __unused)
  1170. {
  1171. struct ceph_pg_mapping *pg;
  1172. u32 len, i;
  1173. ceph_decode_32_safe(p, end, len, e_inval);
  1174. if (len > (SIZE_MAX - sizeof(*pg)) / (2 * sizeof(u32)))
  1175. return ERR_PTR(-EINVAL);
  1176. ceph_decode_need(p, end, 2 * len * sizeof(u32), e_inval);
  1177. pg = alloc_pg_mapping(2 * len * sizeof(u32));
  1178. if (!pg)
  1179. return ERR_PTR(-ENOMEM);
  1180. pg->pg_upmap_items.len = len;
  1181. for (i = 0; i < len; i++) {
  1182. pg->pg_upmap_items.from_to[i][0] = ceph_decode_32(p);
  1183. pg->pg_upmap_items.from_to[i][1] = ceph_decode_32(p);
  1184. }
  1185. return pg;
  1186. e_inval:
  1187. return ERR_PTR(-EINVAL);
  1188. }
  1189. static int decode_pg_upmap_items(void **p, void *end, struct ceph_osdmap *map)
  1190. {
  1191. return decode_pg_mapping(p, end, &map->pg_upmap_items,
  1192. __decode_pg_upmap_items, false);
  1193. }
  1194. static int decode_new_pg_upmap_items(void **p, void *end,
  1195. struct ceph_osdmap *map)
  1196. {
  1197. return decode_pg_mapping(p, end, &map->pg_upmap_items,
  1198. __decode_pg_upmap_items, true);
  1199. }
  1200. static int decode_old_pg_upmap_items(void **p, void *end,
  1201. struct ceph_osdmap *map)
  1202. {
  1203. return decode_pg_mapping(p, end, &map->pg_upmap_items, NULL, true);
  1204. }
  1205. /*
  1206. * decode a full map.
  1207. */
  1208. static int osdmap_decode(void **p, void *end, struct ceph_osdmap *map)
  1209. {
  1210. u8 struct_v;
  1211. u32 epoch = 0;
  1212. void *start = *p;
  1213. u32 max;
  1214. u32 len, i;
  1215. int err;
  1216. dout("%s %p to %p len %d\n", __func__, *p, end, (int)(end - *p));
  1217. err = get_osdmap_client_data_v(p, end, "full", &struct_v);
  1218. if (err)
  1219. goto bad;
  1220. /* fsid, epoch, created, modified */
  1221. ceph_decode_need(p, end, sizeof(map->fsid) + sizeof(u32) +
  1222. sizeof(map->created) + sizeof(map->modified), e_inval);
  1223. ceph_decode_copy(p, &map->fsid, sizeof(map->fsid));
  1224. epoch = map->epoch = ceph_decode_32(p);
  1225. ceph_decode_copy(p, &map->created, sizeof(map->created));
  1226. ceph_decode_copy(p, &map->modified, sizeof(map->modified));
  1227. /* pools */
  1228. err = decode_pools(p, end, map);
  1229. if (err)
  1230. goto bad;
  1231. /* pool_name */
  1232. err = decode_pool_names(p, end, map);
  1233. if (err)
  1234. goto bad;
  1235. ceph_decode_32_safe(p, end, map->pool_max, e_inval);
  1236. ceph_decode_32_safe(p, end, map->flags, e_inval);
  1237. /* max_osd */
  1238. ceph_decode_32_safe(p, end, max, e_inval);
  1239. /* (re)alloc osd arrays */
  1240. err = osdmap_set_max_osd(map, max);
  1241. if (err)
  1242. goto bad;
  1243. /* osd_state, osd_weight, osd_addrs->client_addr */
  1244. ceph_decode_need(p, end, 3*sizeof(u32) +
  1245. map->max_osd*((struct_v >= 5 ? sizeof(u32) :
  1246. sizeof(u8)) +
  1247. sizeof(*map->osd_weight) +
  1248. sizeof(*map->osd_addr)), e_inval);
  1249. if (ceph_decode_32(p) != map->max_osd)
  1250. goto e_inval;
  1251. if (struct_v >= 5) {
  1252. for (i = 0; i < map->max_osd; i++)
  1253. map->osd_state[i] = ceph_decode_32(p);
  1254. } else {
  1255. for (i = 0; i < map->max_osd; i++)
  1256. map->osd_state[i] = ceph_decode_8(p);
  1257. }
  1258. if (ceph_decode_32(p) != map->max_osd)
  1259. goto e_inval;
  1260. for (i = 0; i < map->max_osd; i++)
  1261. map->osd_weight[i] = ceph_decode_32(p);
  1262. if (ceph_decode_32(p) != map->max_osd)
  1263. goto e_inval;
  1264. ceph_decode_copy(p, map->osd_addr, map->max_osd*sizeof(*map->osd_addr));
  1265. for (i = 0; i < map->max_osd; i++)
  1266. ceph_decode_addr(&map->osd_addr[i]);
  1267. /* pg_temp */
  1268. err = decode_pg_temp(p, end, map);
  1269. if (err)
  1270. goto bad;
  1271. /* primary_temp */
  1272. if (struct_v >= 1) {
  1273. err = decode_primary_temp(p, end, map);
  1274. if (err)
  1275. goto bad;
  1276. }
  1277. /* primary_affinity */
  1278. if (struct_v >= 2) {
  1279. err = decode_primary_affinity(p, end, map);
  1280. if (err)
  1281. goto bad;
  1282. } else {
  1283. WARN_ON(map->osd_primary_affinity);
  1284. }
  1285. /* crush */
  1286. ceph_decode_32_safe(p, end, len, e_inval);
  1287. err = osdmap_set_crush(map, crush_decode(*p, min(*p + len, end)));
  1288. if (err)
  1289. goto bad;
  1290. *p += len;
  1291. if (struct_v >= 3) {
  1292. /* erasure_code_profiles */
  1293. ceph_decode_skip_map_of_map(p, end, string, string, string,
  1294. e_inval);
  1295. }
  1296. if (struct_v >= 4) {
  1297. err = decode_pg_upmap(p, end, map);
  1298. if (err)
  1299. goto bad;
  1300. err = decode_pg_upmap_items(p, end, map);
  1301. if (err)
  1302. goto bad;
  1303. } else {
  1304. WARN_ON(!RB_EMPTY_ROOT(&map->pg_upmap));
  1305. WARN_ON(!RB_EMPTY_ROOT(&map->pg_upmap_items));
  1306. }
  1307. /* ignore the rest */
  1308. *p = end;
  1309. dout("full osdmap epoch %d max_osd %d\n", map->epoch, map->max_osd);
  1310. return 0;
  1311. e_inval:
  1312. err = -EINVAL;
  1313. bad:
  1314. pr_err("corrupt full osdmap (%d) epoch %d off %d (%p of %p-%p)\n",
  1315. err, epoch, (int)(*p - start), *p, start, end);
  1316. print_hex_dump(KERN_DEBUG, "osdmap: ",
  1317. DUMP_PREFIX_OFFSET, 16, 1,
  1318. start, end - start, true);
  1319. return err;
  1320. }
  1321. /*
  1322. * Allocate and decode a full map.
  1323. */
  1324. struct ceph_osdmap *ceph_osdmap_decode(void **p, void *end)
  1325. {
  1326. struct ceph_osdmap *map;
  1327. int ret;
  1328. map = ceph_osdmap_alloc();
  1329. if (!map)
  1330. return ERR_PTR(-ENOMEM);
  1331. ret = osdmap_decode(p, end, map);
  1332. if (ret) {
  1333. ceph_osdmap_destroy(map);
  1334. return ERR_PTR(ret);
  1335. }
  1336. return map;
  1337. }
  1338. /*
  1339. * Encoding order is (new_up_client, new_state, new_weight). Need to
  1340. * apply in the (new_weight, new_state, new_up_client) order, because
  1341. * an incremental map may look like e.g.
  1342. *
  1343. * new_up_client: { osd=6, addr=... } # set osd_state and addr
  1344. * new_state: { osd=6, xorstate=EXISTS } # clear osd_state
  1345. */
  1346. static int decode_new_up_state_weight(void **p, void *end, u8 struct_v,
  1347. struct ceph_osdmap *map)
  1348. {
  1349. void *new_up_client;
  1350. void *new_state;
  1351. void *new_weight_end;
  1352. u32 len;
  1353. new_up_client = *p;
  1354. ceph_decode_32_safe(p, end, len, e_inval);
  1355. len *= sizeof(u32) + sizeof(struct ceph_entity_addr);
  1356. ceph_decode_need(p, end, len, e_inval);
  1357. *p += len;
  1358. new_state = *p;
  1359. ceph_decode_32_safe(p, end, len, e_inval);
  1360. len *= sizeof(u32) + (struct_v >= 5 ? sizeof(u32) : sizeof(u8));
  1361. ceph_decode_need(p, end, len, e_inval);
  1362. *p += len;
  1363. /* new_weight */
  1364. ceph_decode_32_safe(p, end, len, e_inval);
  1365. while (len--) {
  1366. s32 osd;
  1367. u32 w;
  1368. ceph_decode_need(p, end, 2*sizeof(u32), e_inval);
  1369. osd = ceph_decode_32(p);
  1370. w = ceph_decode_32(p);
  1371. BUG_ON(osd >= map->max_osd);
  1372. pr_info("osd%d weight 0x%x %s\n", osd, w,
  1373. w == CEPH_OSD_IN ? "(in)" :
  1374. (w == CEPH_OSD_OUT ? "(out)" : ""));
  1375. map->osd_weight[osd] = w;
  1376. /*
  1377. * If we are marking in, set the EXISTS, and clear the
  1378. * AUTOOUT and NEW bits.
  1379. */
  1380. if (w) {
  1381. map->osd_state[osd] |= CEPH_OSD_EXISTS;
  1382. map->osd_state[osd] &= ~(CEPH_OSD_AUTOOUT |
  1383. CEPH_OSD_NEW);
  1384. }
  1385. }
  1386. new_weight_end = *p;
  1387. /* new_state (up/down) */
  1388. *p = new_state;
  1389. len = ceph_decode_32(p);
  1390. while (len--) {
  1391. s32 osd;
  1392. u32 xorstate;
  1393. int ret;
  1394. osd = ceph_decode_32(p);
  1395. if (struct_v >= 5)
  1396. xorstate = ceph_decode_32(p);
  1397. else
  1398. xorstate = ceph_decode_8(p);
  1399. if (xorstate == 0)
  1400. xorstate = CEPH_OSD_UP;
  1401. BUG_ON(osd >= map->max_osd);
  1402. if ((map->osd_state[osd] & CEPH_OSD_UP) &&
  1403. (xorstate & CEPH_OSD_UP))
  1404. pr_info("osd%d down\n", osd);
  1405. if ((map->osd_state[osd] & CEPH_OSD_EXISTS) &&
  1406. (xorstate & CEPH_OSD_EXISTS)) {
  1407. pr_info("osd%d does not exist\n", osd);
  1408. ret = set_primary_affinity(map, osd,
  1409. CEPH_OSD_DEFAULT_PRIMARY_AFFINITY);
  1410. if (ret)
  1411. return ret;
  1412. memset(map->osd_addr + osd, 0, sizeof(*map->osd_addr));
  1413. map->osd_state[osd] = 0;
  1414. } else {
  1415. map->osd_state[osd] ^= xorstate;
  1416. }
  1417. }
  1418. /* new_up_client */
  1419. *p = new_up_client;
  1420. len = ceph_decode_32(p);
  1421. while (len--) {
  1422. s32 osd;
  1423. struct ceph_entity_addr addr;
  1424. osd = ceph_decode_32(p);
  1425. ceph_decode_copy(p, &addr, sizeof(addr));
  1426. ceph_decode_addr(&addr);
  1427. BUG_ON(osd >= map->max_osd);
  1428. pr_info("osd%d up\n", osd);
  1429. map->osd_state[osd] |= CEPH_OSD_EXISTS | CEPH_OSD_UP;
  1430. map->osd_addr[osd] = addr;
  1431. }
  1432. *p = new_weight_end;
  1433. return 0;
  1434. e_inval:
  1435. return -EINVAL;
  1436. }
  1437. /*
  1438. * decode and apply an incremental map update.
  1439. */
  1440. struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end,
  1441. struct ceph_osdmap *map)
  1442. {
  1443. struct ceph_fsid fsid;
  1444. u32 epoch = 0;
  1445. struct ceph_timespec modified;
  1446. s32 len;
  1447. u64 pool;
  1448. __s64 new_pool_max;
  1449. __s32 new_flags, max;
  1450. void *start = *p;
  1451. int err;
  1452. u8 struct_v;
  1453. dout("%s %p to %p len %d\n", __func__, *p, end, (int)(end - *p));
  1454. err = get_osdmap_client_data_v(p, end, "inc", &struct_v);
  1455. if (err)
  1456. goto bad;
  1457. /* fsid, epoch, modified, new_pool_max, new_flags */
  1458. ceph_decode_need(p, end, sizeof(fsid) + sizeof(u32) + sizeof(modified) +
  1459. sizeof(u64) + sizeof(u32), e_inval);
  1460. ceph_decode_copy(p, &fsid, sizeof(fsid));
  1461. epoch = ceph_decode_32(p);
  1462. BUG_ON(epoch != map->epoch+1);
  1463. ceph_decode_copy(p, &modified, sizeof(modified));
  1464. new_pool_max = ceph_decode_64(p);
  1465. new_flags = ceph_decode_32(p);
  1466. /* full map? */
  1467. ceph_decode_32_safe(p, end, len, e_inval);
  1468. if (len > 0) {
  1469. dout("apply_incremental full map len %d, %p to %p\n",
  1470. len, *p, end);
  1471. return ceph_osdmap_decode(p, min(*p+len, end));
  1472. }
  1473. /* new crush? */
  1474. ceph_decode_32_safe(p, end, len, e_inval);
  1475. if (len > 0) {
  1476. err = osdmap_set_crush(map,
  1477. crush_decode(*p, min(*p + len, end)));
  1478. if (err)
  1479. goto bad;
  1480. *p += len;
  1481. }
  1482. /* new flags? */
  1483. if (new_flags >= 0)
  1484. map->flags = new_flags;
  1485. if (new_pool_max >= 0)
  1486. map->pool_max = new_pool_max;
  1487. /* new max? */
  1488. ceph_decode_32_safe(p, end, max, e_inval);
  1489. if (max >= 0) {
  1490. err = osdmap_set_max_osd(map, max);
  1491. if (err)
  1492. goto bad;
  1493. }
  1494. map->epoch++;
  1495. map->modified = modified;
  1496. /* new_pools */
  1497. err = decode_new_pools(p, end, map);
  1498. if (err)
  1499. goto bad;
  1500. /* new_pool_names */
  1501. err = decode_pool_names(p, end, map);
  1502. if (err)
  1503. goto bad;
  1504. /* old_pool */
  1505. ceph_decode_32_safe(p, end, len, e_inval);
  1506. while (len--) {
  1507. struct ceph_pg_pool_info *pi;
  1508. ceph_decode_64_safe(p, end, pool, e_inval);
  1509. pi = __lookup_pg_pool(&map->pg_pools, pool);
  1510. if (pi)
  1511. __remove_pg_pool(&map->pg_pools, pi);
  1512. }
  1513. /* new_up_client, new_state, new_weight */
  1514. err = decode_new_up_state_weight(p, end, struct_v, map);
  1515. if (err)
  1516. goto bad;
  1517. /* new_pg_temp */
  1518. err = decode_new_pg_temp(p, end, map);
  1519. if (err)
  1520. goto bad;
  1521. /* new_primary_temp */
  1522. if (struct_v >= 1) {
  1523. err = decode_new_primary_temp(p, end, map);
  1524. if (err)
  1525. goto bad;
  1526. }
  1527. /* new_primary_affinity */
  1528. if (struct_v >= 2) {
  1529. err = decode_new_primary_affinity(p, end, map);
  1530. if (err)
  1531. goto bad;
  1532. }
  1533. if (struct_v >= 3) {
  1534. /* new_erasure_code_profiles */
  1535. ceph_decode_skip_map_of_map(p, end, string, string, string,
  1536. e_inval);
  1537. /* old_erasure_code_profiles */
  1538. ceph_decode_skip_set(p, end, string, e_inval);
  1539. }
  1540. if (struct_v >= 4) {
  1541. err = decode_new_pg_upmap(p, end, map);
  1542. if (err)
  1543. goto bad;
  1544. err = decode_old_pg_upmap(p, end, map);
  1545. if (err)
  1546. goto bad;
  1547. err = decode_new_pg_upmap_items(p, end, map);
  1548. if (err)
  1549. goto bad;
  1550. err = decode_old_pg_upmap_items(p, end, map);
  1551. if (err)
  1552. goto bad;
  1553. }
  1554. /* ignore the rest */
  1555. *p = end;
  1556. dout("inc osdmap epoch %d max_osd %d\n", map->epoch, map->max_osd);
  1557. return map;
  1558. e_inval:
  1559. err = -EINVAL;
  1560. bad:
  1561. pr_err("corrupt inc osdmap (%d) epoch %d off %d (%p of %p-%p)\n",
  1562. err, epoch, (int)(*p - start), *p, start, end);
  1563. print_hex_dump(KERN_DEBUG, "osdmap: ",
  1564. DUMP_PREFIX_OFFSET, 16, 1,
  1565. start, end - start, true);
  1566. return ERR_PTR(err);
  1567. }
  1568. void ceph_oloc_copy(struct ceph_object_locator *dest,
  1569. const struct ceph_object_locator *src)
  1570. {
  1571. ceph_oloc_destroy(dest);
  1572. dest->pool = src->pool;
  1573. if (src->pool_ns)
  1574. dest->pool_ns = ceph_get_string(src->pool_ns);
  1575. else
  1576. dest->pool_ns = NULL;
  1577. }
  1578. EXPORT_SYMBOL(ceph_oloc_copy);
  1579. void ceph_oloc_destroy(struct ceph_object_locator *oloc)
  1580. {
  1581. ceph_put_string(oloc->pool_ns);
  1582. }
  1583. EXPORT_SYMBOL(ceph_oloc_destroy);
  1584. void ceph_oid_copy(struct ceph_object_id *dest,
  1585. const struct ceph_object_id *src)
  1586. {
  1587. ceph_oid_destroy(dest);
  1588. if (src->name != src->inline_name) {
  1589. /* very rare, see ceph_object_id definition */
  1590. dest->name = kmalloc(src->name_len + 1,
  1591. GFP_NOIO | __GFP_NOFAIL);
  1592. } else {
  1593. dest->name = dest->inline_name;
  1594. }
  1595. memcpy(dest->name, src->name, src->name_len + 1);
  1596. dest->name_len = src->name_len;
  1597. }
  1598. EXPORT_SYMBOL(ceph_oid_copy);
  1599. static __printf(2, 0)
  1600. int oid_printf_vargs(struct ceph_object_id *oid, const char *fmt, va_list ap)
  1601. {
  1602. int len;
  1603. WARN_ON(!ceph_oid_empty(oid));
  1604. len = vsnprintf(oid->inline_name, sizeof(oid->inline_name), fmt, ap);
  1605. if (len >= sizeof(oid->inline_name))
  1606. return len;
  1607. oid->name_len = len;
  1608. return 0;
  1609. }
  1610. /*
  1611. * If oid doesn't fit into inline buffer, BUG.
  1612. */
  1613. void ceph_oid_printf(struct ceph_object_id *oid, const char *fmt, ...)
  1614. {
  1615. va_list ap;
  1616. va_start(ap, fmt);
  1617. BUG_ON(oid_printf_vargs(oid, fmt, ap));
  1618. va_end(ap);
  1619. }
  1620. EXPORT_SYMBOL(ceph_oid_printf);
  1621. static __printf(3, 0)
  1622. int oid_aprintf_vargs(struct ceph_object_id *oid, gfp_t gfp,
  1623. const char *fmt, va_list ap)
  1624. {
  1625. va_list aq;
  1626. int len;
  1627. va_copy(aq, ap);
  1628. len = oid_printf_vargs(oid, fmt, aq);
  1629. va_end(aq);
  1630. if (len) {
  1631. char *external_name;
  1632. external_name = kmalloc(len + 1, gfp);
  1633. if (!external_name)
  1634. return -ENOMEM;
  1635. oid->name = external_name;
  1636. WARN_ON(vsnprintf(oid->name, len + 1, fmt, ap) != len);
  1637. oid->name_len = len;
  1638. }
  1639. return 0;
  1640. }
  1641. /*
  1642. * If oid doesn't fit into inline buffer, allocate.
  1643. */
  1644. int ceph_oid_aprintf(struct ceph_object_id *oid, gfp_t gfp,
  1645. const char *fmt, ...)
  1646. {
  1647. va_list ap;
  1648. int ret;
  1649. va_start(ap, fmt);
  1650. ret = oid_aprintf_vargs(oid, gfp, fmt, ap);
  1651. va_end(ap);
  1652. return ret;
  1653. }
  1654. EXPORT_SYMBOL(ceph_oid_aprintf);
  1655. void ceph_oid_destroy(struct ceph_object_id *oid)
  1656. {
  1657. if (oid->name != oid->inline_name)
  1658. kfree(oid->name);
  1659. }
  1660. EXPORT_SYMBOL(ceph_oid_destroy);
  1661. /*
  1662. * osds only
  1663. */
  1664. static bool __osds_equal(const struct ceph_osds *lhs,
  1665. const struct ceph_osds *rhs)
  1666. {
  1667. if (lhs->size == rhs->size &&
  1668. !memcmp(lhs->osds, rhs->osds, rhs->size * sizeof(rhs->osds[0])))
  1669. return true;
  1670. return false;
  1671. }
  1672. /*
  1673. * osds + primary
  1674. */
  1675. static bool osds_equal(const struct ceph_osds *lhs,
  1676. const struct ceph_osds *rhs)
  1677. {
  1678. if (__osds_equal(lhs, rhs) &&
  1679. lhs->primary == rhs->primary)
  1680. return true;
  1681. return false;
  1682. }
  1683. static bool osds_valid(const struct ceph_osds *set)
  1684. {
  1685. /* non-empty set */
  1686. if (set->size > 0 && set->primary >= 0)
  1687. return true;
  1688. /* empty can_shift_osds set */
  1689. if (!set->size && set->primary == -1)
  1690. return true;
  1691. /* empty !can_shift_osds set - all NONE */
  1692. if (set->size > 0 && set->primary == -1) {
  1693. int i;
  1694. for (i = 0; i < set->size; i++) {
  1695. if (set->osds[i] != CRUSH_ITEM_NONE)
  1696. break;
  1697. }
  1698. if (i == set->size)
  1699. return true;
  1700. }
  1701. return false;
  1702. }
  1703. void ceph_osds_copy(struct ceph_osds *dest, const struct ceph_osds *src)
  1704. {
  1705. memcpy(dest->osds, src->osds, src->size * sizeof(src->osds[0]));
  1706. dest->size = src->size;
  1707. dest->primary = src->primary;
  1708. }
  1709. bool ceph_pg_is_split(const struct ceph_pg *pgid, u32 old_pg_num,
  1710. u32 new_pg_num)
  1711. {
  1712. int old_bits = calc_bits_of(old_pg_num);
  1713. int old_mask = (1 << old_bits) - 1;
  1714. int n;
  1715. WARN_ON(pgid->seed >= old_pg_num);
  1716. if (new_pg_num <= old_pg_num)
  1717. return false;
  1718. for (n = 1; ; n++) {
  1719. int next_bit = n << (old_bits - 1);
  1720. u32 s = next_bit | pgid->seed;
  1721. if (s < old_pg_num || s == pgid->seed)
  1722. continue;
  1723. if (s >= new_pg_num)
  1724. break;
  1725. s = ceph_stable_mod(s, old_pg_num, old_mask);
  1726. if (s == pgid->seed)
  1727. return true;
  1728. }
  1729. return false;
  1730. }
  1731. bool ceph_is_new_interval(const struct ceph_osds *old_acting,
  1732. const struct ceph_osds *new_acting,
  1733. const struct ceph_osds *old_up,
  1734. const struct ceph_osds *new_up,
  1735. int old_size,
  1736. int new_size,
  1737. int old_min_size,
  1738. int new_min_size,
  1739. u32 old_pg_num,
  1740. u32 new_pg_num,
  1741. bool old_sort_bitwise,
  1742. bool new_sort_bitwise,
  1743. bool old_recovery_deletes,
  1744. bool new_recovery_deletes,
  1745. const struct ceph_pg *pgid)
  1746. {
  1747. return !osds_equal(old_acting, new_acting) ||
  1748. !osds_equal(old_up, new_up) ||
  1749. old_size != new_size ||
  1750. old_min_size != new_min_size ||
  1751. ceph_pg_is_split(pgid, old_pg_num, new_pg_num) ||
  1752. old_sort_bitwise != new_sort_bitwise ||
  1753. old_recovery_deletes != new_recovery_deletes;
  1754. }
  1755. static int calc_pg_rank(int osd, const struct ceph_osds *acting)
  1756. {
  1757. int i;
  1758. for (i = 0; i < acting->size; i++) {
  1759. if (acting->osds[i] == osd)
  1760. return i;
  1761. }
  1762. return -1;
  1763. }
  1764. static bool primary_changed(const struct ceph_osds *old_acting,
  1765. const struct ceph_osds *new_acting)
  1766. {
  1767. if (!old_acting->size && !new_acting->size)
  1768. return false; /* both still empty */
  1769. if (!old_acting->size ^ !new_acting->size)
  1770. return true; /* was empty, now not, or vice versa */
  1771. if (old_acting->primary != new_acting->primary)
  1772. return true; /* primary changed */
  1773. if (calc_pg_rank(old_acting->primary, old_acting) !=
  1774. calc_pg_rank(new_acting->primary, new_acting))
  1775. return true;
  1776. return false; /* same primary (tho replicas may have changed) */
  1777. }
  1778. bool ceph_osds_changed(const struct ceph_osds *old_acting,
  1779. const struct ceph_osds *new_acting,
  1780. bool any_change)
  1781. {
  1782. if (primary_changed(old_acting, new_acting))
  1783. return true;
  1784. if (any_change && !__osds_equal(old_acting, new_acting))
  1785. return true;
  1786. return false;
  1787. }
  1788. /*
  1789. * Map an object into a PG.
  1790. *
  1791. * Should only be called with target_oid and target_oloc (as opposed to
  1792. * base_oid and base_oloc), since tiering isn't taken into account.
  1793. */
  1794. void __ceph_object_locator_to_pg(struct ceph_pg_pool_info *pi,
  1795. const struct ceph_object_id *oid,
  1796. const struct ceph_object_locator *oloc,
  1797. struct ceph_pg *raw_pgid)
  1798. {
  1799. WARN_ON(pi->id != oloc->pool);
  1800. if (!oloc->pool_ns) {
  1801. raw_pgid->pool = oloc->pool;
  1802. raw_pgid->seed = ceph_str_hash(pi->object_hash, oid->name,
  1803. oid->name_len);
  1804. dout("%s %s -> raw_pgid %llu.%x\n", __func__, oid->name,
  1805. raw_pgid->pool, raw_pgid->seed);
  1806. } else {
  1807. char stack_buf[256];
  1808. char *buf = stack_buf;
  1809. int nsl = oloc->pool_ns->len;
  1810. size_t total = nsl + 1 + oid->name_len;
  1811. if (total > sizeof(stack_buf))
  1812. buf = kmalloc(total, GFP_NOIO | __GFP_NOFAIL);
  1813. memcpy(buf, oloc->pool_ns->str, nsl);
  1814. buf[nsl] = '\037';
  1815. memcpy(buf + nsl + 1, oid->name, oid->name_len);
  1816. raw_pgid->pool = oloc->pool;
  1817. raw_pgid->seed = ceph_str_hash(pi->object_hash, buf, total);
  1818. if (buf != stack_buf)
  1819. kfree(buf);
  1820. dout("%s %s ns %.*s -> raw_pgid %llu.%x\n", __func__,
  1821. oid->name, nsl, oloc->pool_ns->str,
  1822. raw_pgid->pool, raw_pgid->seed);
  1823. }
  1824. }
  1825. int ceph_object_locator_to_pg(struct ceph_osdmap *osdmap,
  1826. const struct ceph_object_id *oid,
  1827. const struct ceph_object_locator *oloc,
  1828. struct ceph_pg *raw_pgid)
  1829. {
  1830. struct ceph_pg_pool_info *pi;
  1831. pi = ceph_pg_pool_by_id(osdmap, oloc->pool);
  1832. if (!pi)
  1833. return -ENOENT;
  1834. __ceph_object_locator_to_pg(pi, oid, oloc, raw_pgid);
  1835. return 0;
  1836. }
  1837. EXPORT_SYMBOL(ceph_object_locator_to_pg);
  1838. /*
  1839. * Map a raw PG (full precision ps) into an actual PG.
  1840. */
  1841. static void raw_pg_to_pg(struct ceph_pg_pool_info *pi,
  1842. const struct ceph_pg *raw_pgid,
  1843. struct ceph_pg *pgid)
  1844. {
  1845. pgid->pool = raw_pgid->pool;
  1846. pgid->seed = ceph_stable_mod(raw_pgid->seed, pi->pg_num,
  1847. pi->pg_num_mask);
  1848. }
  1849. /*
  1850. * Map a raw PG (full precision ps) into a placement ps (placement
  1851. * seed). Include pool id in that value so that different pools don't
  1852. * use the same seeds.
  1853. */
  1854. static u32 raw_pg_to_pps(struct ceph_pg_pool_info *pi,
  1855. const struct ceph_pg *raw_pgid)
  1856. {
  1857. if (pi->flags & CEPH_POOL_FLAG_HASHPSPOOL) {
  1858. /* hash pool id and seed so that pool PGs do not overlap */
  1859. return crush_hash32_2(CRUSH_HASH_RJENKINS1,
  1860. ceph_stable_mod(raw_pgid->seed,
  1861. pi->pgp_num,
  1862. pi->pgp_num_mask),
  1863. raw_pgid->pool);
  1864. } else {
  1865. /*
  1866. * legacy behavior: add ps and pool together. this is
  1867. * not a great approach because the PGs from each pool
  1868. * will overlap on top of each other: 0.5 == 1.4 ==
  1869. * 2.3 == ...
  1870. */
  1871. return ceph_stable_mod(raw_pgid->seed, pi->pgp_num,
  1872. pi->pgp_num_mask) +
  1873. (unsigned)raw_pgid->pool;
  1874. }
  1875. }
  1876. /*
  1877. * Magic value used for a "default" fallback choose_args, used if the
  1878. * crush_choose_arg_map passed to do_crush() does not exist. If this
  1879. * also doesn't exist, fall back to canonical weights.
  1880. */
  1881. #define CEPH_DEFAULT_CHOOSE_ARGS -1
  1882. static int do_crush(struct ceph_osdmap *map, int ruleno, int x,
  1883. int *result, int result_max,
  1884. const __u32 *weight, int weight_max,
  1885. s64 choose_args_index)
  1886. {
  1887. struct crush_choose_arg_map *arg_map;
  1888. int r;
  1889. BUG_ON(result_max > CEPH_PG_MAX_SIZE);
  1890. arg_map = lookup_choose_arg_map(&map->crush->choose_args,
  1891. choose_args_index);
  1892. if (!arg_map)
  1893. arg_map = lookup_choose_arg_map(&map->crush->choose_args,
  1894. CEPH_DEFAULT_CHOOSE_ARGS);
  1895. mutex_lock(&map->crush_workspace_mutex);
  1896. r = crush_do_rule(map->crush, ruleno, x, result, result_max,
  1897. weight, weight_max, map->crush_workspace,
  1898. arg_map ? arg_map->args : NULL);
  1899. mutex_unlock(&map->crush_workspace_mutex);
  1900. return r;
  1901. }
  1902. static void remove_nonexistent_osds(struct ceph_osdmap *osdmap,
  1903. struct ceph_pg_pool_info *pi,
  1904. struct ceph_osds *set)
  1905. {
  1906. int i;
  1907. if (ceph_can_shift_osds(pi)) {
  1908. int removed = 0;
  1909. /* shift left */
  1910. for (i = 0; i < set->size; i++) {
  1911. if (!ceph_osd_exists(osdmap, set->osds[i])) {
  1912. removed++;
  1913. continue;
  1914. }
  1915. if (removed)
  1916. set->osds[i - removed] = set->osds[i];
  1917. }
  1918. set->size -= removed;
  1919. } else {
  1920. /* set dne devices to NONE */
  1921. for (i = 0; i < set->size; i++) {
  1922. if (!ceph_osd_exists(osdmap, set->osds[i]))
  1923. set->osds[i] = CRUSH_ITEM_NONE;
  1924. }
  1925. }
  1926. }
  1927. /*
  1928. * Calculate raw set (CRUSH output) for given PG and filter out
  1929. * nonexistent OSDs. ->primary is undefined for a raw set.
  1930. *
  1931. * Placement seed (CRUSH input) is returned through @ppps.
  1932. */
  1933. static void pg_to_raw_osds(struct ceph_osdmap *osdmap,
  1934. struct ceph_pg_pool_info *pi,
  1935. const struct ceph_pg *raw_pgid,
  1936. struct ceph_osds *raw,
  1937. u32 *ppps)
  1938. {
  1939. u32 pps = raw_pg_to_pps(pi, raw_pgid);
  1940. int ruleno;
  1941. int len;
  1942. ceph_osds_init(raw);
  1943. if (ppps)
  1944. *ppps = pps;
  1945. ruleno = crush_find_rule(osdmap->crush, pi->crush_ruleset, pi->type,
  1946. pi->size);
  1947. if (ruleno < 0) {
  1948. pr_err("no crush rule: pool %lld ruleset %d type %d size %d\n",
  1949. pi->id, pi->crush_ruleset, pi->type, pi->size);
  1950. return;
  1951. }
  1952. if (pi->size > ARRAY_SIZE(raw->osds)) {
  1953. pr_err_ratelimited("pool %lld ruleset %d type %d too wide: size %d > %zu\n",
  1954. pi->id, pi->crush_ruleset, pi->type, pi->size,
  1955. ARRAY_SIZE(raw->osds));
  1956. return;
  1957. }
  1958. len = do_crush(osdmap, ruleno, pps, raw->osds, pi->size,
  1959. osdmap->osd_weight, osdmap->max_osd, pi->id);
  1960. if (len < 0) {
  1961. pr_err("error %d from crush rule %d: pool %lld ruleset %d type %d size %d\n",
  1962. len, ruleno, pi->id, pi->crush_ruleset, pi->type,
  1963. pi->size);
  1964. return;
  1965. }
  1966. raw->size = len;
  1967. remove_nonexistent_osds(osdmap, pi, raw);
  1968. }
  1969. /* apply pg_upmap[_items] mappings */
  1970. static void apply_upmap(struct ceph_osdmap *osdmap,
  1971. const struct ceph_pg *pgid,
  1972. struct ceph_osds *raw)
  1973. {
  1974. struct ceph_pg_mapping *pg;
  1975. int i, j;
  1976. pg = lookup_pg_mapping(&osdmap->pg_upmap, pgid);
  1977. if (pg) {
  1978. /* make sure targets aren't marked out */
  1979. for (i = 0; i < pg->pg_upmap.len; i++) {
  1980. int osd = pg->pg_upmap.osds[i];
  1981. if (osd != CRUSH_ITEM_NONE &&
  1982. osd < osdmap->max_osd &&
  1983. osdmap->osd_weight[osd] == 0) {
  1984. /* reject/ignore explicit mapping */
  1985. return;
  1986. }
  1987. }
  1988. for (i = 0; i < pg->pg_upmap.len; i++)
  1989. raw->osds[i] = pg->pg_upmap.osds[i];
  1990. raw->size = pg->pg_upmap.len;
  1991. /* check and apply pg_upmap_items, if any */
  1992. }
  1993. pg = lookup_pg_mapping(&osdmap->pg_upmap_items, pgid);
  1994. if (pg) {
  1995. /*
  1996. * Note: this approach does not allow a bidirectional swap,
  1997. * e.g., [[1,2],[2,1]] applied to [0,1,2] -> [0,2,1].
  1998. */
  1999. for (i = 0; i < pg->pg_upmap_items.len; i++) {
  2000. int from = pg->pg_upmap_items.from_to[i][0];
  2001. int to = pg->pg_upmap_items.from_to[i][1];
  2002. int pos = -1;
  2003. bool exists = false;
  2004. /* make sure replacement doesn't already appear */
  2005. for (j = 0; j < raw->size; j++) {
  2006. int osd = raw->osds[j];
  2007. if (osd == to) {
  2008. exists = true;
  2009. break;
  2010. }
  2011. /* ignore mapping if target is marked out */
  2012. if (osd == from && pos < 0 &&
  2013. !(to != CRUSH_ITEM_NONE &&
  2014. to < osdmap->max_osd &&
  2015. osdmap->osd_weight[to] == 0)) {
  2016. pos = j;
  2017. }
  2018. }
  2019. if (!exists && pos >= 0)
  2020. raw->osds[pos] = to;
  2021. }
  2022. }
  2023. }
  2024. /*
  2025. * Given raw set, calculate up set and up primary. By definition of an
  2026. * up set, the result won't contain nonexistent or down OSDs.
  2027. *
  2028. * This is done in-place - on return @set is the up set. If it's
  2029. * empty, ->primary will remain undefined.
  2030. */
  2031. static void raw_to_up_osds(struct ceph_osdmap *osdmap,
  2032. struct ceph_pg_pool_info *pi,
  2033. struct ceph_osds *set)
  2034. {
  2035. int i;
  2036. /* ->primary is undefined for a raw set */
  2037. BUG_ON(set->primary != -1);
  2038. if (ceph_can_shift_osds(pi)) {
  2039. int removed = 0;
  2040. /* shift left */
  2041. for (i = 0; i < set->size; i++) {
  2042. if (ceph_osd_is_down(osdmap, set->osds[i])) {
  2043. removed++;
  2044. continue;
  2045. }
  2046. if (removed)
  2047. set->osds[i - removed] = set->osds[i];
  2048. }
  2049. set->size -= removed;
  2050. if (set->size > 0)
  2051. set->primary = set->osds[0];
  2052. } else {
  2053. /* set down/dne devices to NONE */
  2054. for (i = set->size - 1; i >= 0; i--) {
  2055. if (ceph_osd_is_down(osdmap, set->osds[i]))
  2056. set->osds[i] = CRUSH_ITEM_NONE;
  2057. else
  2058. set->primary = set->osds[i];
  2059. }
  2060. }
  2061. }
  2062. static void apply_primary_affinity(struct ceph_osdmap *osdmap,
  2063. struct ceph_pg_pool_info *pi,
  2064. u32 pps,
  2065. struct ceph_osds *up)
  2066. {
  2067. int i;
  2068. int pos = -1;
  2069. /*
  2070. * Do we have any non-default primary_affinity values for these
  2071. * osds?
  2072. */
  2073. if (!osdmap->osd_primary_affinity)
  2074. return;
  2075. for (i = 0; i < up->size; i++) {
  2076. int osd = up->osds[i];
  2077. if (osd != CRUSH_ITEM_NONE &&
  2078. osdmap->osd_primary_affinity[osd] !=
  2079. CEPH_OSD_DEFAULT_PRIMARY_AFFINITY) {
  2080. break;
  2081. }
  2082. }
  2083. if (i == up->size)
  2084. return;
  2085. /*
  2086. * Pick the primary. Feed both the seed (for the pg) and the
  2087. * osd into the hash/rng so that a proportional fraction of an
  2088. * osd's pgs get rejected as primary.
  2089. */
  2090. for (i = 0; i < up->size; i++) {
  2091. int osd = up->osds[i];
  2092. u32 aff;
  2093. if (osd == CRUSH_ITEM_NONE)
  2094. continue;
  2095. aff = osdmap->osd_primary_affinity[osd];
  2096. if (aff < CEPH_OSD_MAX_PRIMARY_AFFINITY &&
  2097. (crush_hash32_2(CRUSH_HASH_RJENKINS1,
  2098. pps, osd) >> 16) >= aff) {
  2099. /*
  2100. * We chose not to use this primary. Note it
  2101. * anyway as a fallback in case we don't pick
  2102. * anyone else, but keep looking.
  2103. */
  2104. if (pos < 0)
  2105. pos = i;
  2106. } else {
  2107. pos = i;
  2108. break;
  2109. }
  2110. }
  2111. if (pos < 0)
  2112. return;
  2113. up->primary = up->osds[pos];
  2114. if (ceph_can_shift_osds(pi) && pos > 0) {
  2115. /* move the new primary to the front */
  2116. for (i = pos; i > 0; i--)
  2117. up->osds[i] = up->osds[i - 1];
  2118. up->osds[0] = up->primary;
  2119. }
  2120. }
  2121. /*
  2122. * Get pg_temp and primary_temp mappings for given PG.
  2123. *
  2124. * Note that a PG may have none, only pg_temp, only primary_temp or
  2125. * both pg_temp and primary_temp mappings. This means @temp isn't
  2126. * always a valid OSD set on return: in the "only primary_temp" case,
  2127. * @temp will have its ->primary >= 0 but ->size == 0.
  2128. */
  2129. static void get_temp_osds(struct ceph_osdmap *osdmap,
  2130. struct ceph_pg_pool_info *pi,
  2131. const struct ceph_pg *pgid,
  2132. struct ceph_osds *temp)
  2133. {
  2134. struct ceph_pg_mapping *pg;
  2135. int i;
  2136. ceph_osds_init(temp);
  2137. /* pg_temp? */
  2138. pg = lookup_pg_mapping(&osdmap->pg_temp, pgid);
  2139. if (pg) {
  2140. for (i = 0; i < pg->pg_temp.len; i++) {
  2141. if (ceph_osd_is_down(osdmap, pg->pg_temp.osds[i])) {
  2142. if (ceph_can_shift_osds(pi))
  2143. continue;
  2144. temp->osds[temp->size++] = CRUSH_ITEM_NONE;
  2145. } else {
  2146. temp->osds[temp->size++] = pg->pg_temp.osds[i];
  2147. }
  2148. }
  2149. /* apply pg_temp's primary */
  2150. for (i = 0; i < temp->size; i++) {
  2151. if (temp->osds[i] != CRUSH_ITEM_NONE) {
  2152. temp->primary = temp->osds[i];
  2153. break;
  2154. }
  2155. }
  2156. }
  2157. /* primary_temp? */
  2158. pg = lookup_pg_mapping(&osdmap->primary_temp, pgid);
  2159. if (pg)
  2160. temp->primary = pg->primary_temp.osd;
  2161. }
  2162. /*
  2163. * Map a PG to its acting set as well as its up set.
  2164. *
  2165. * Acting set is used for data mapping purposes, while up set can be
  2166. * recorded for detecting interval changes and deciding whether to
  2167. * resend a request.
  2168. */
  2169. void ceph_pg_to_up_acting_osds(struct ceph_osdmap *osdmap,
  2170. struct ceph_pg_pool_info *pi,
  2171. const struct ceph_pg *raw_pgid,
  2172. struct ceph_osds *up,
  2173. struct ceph_osds *acting)
  2174. {
  2175. struct ceph_pg pgid;
  2176. u32 pps;
  2177. WARN_ON(pi->id != raw_pgid->pool);
  2178. raw_pg_to_pg(pi, raw_pgid, &pgid);
  2179. pg_to_raw_osds(osdmap, pi, raw_pgid, up, &pps);
  2180. apply_upmap(osdmap, &pgid, up);
  2181. raw_to_up_osds(osdmap, pi, up);
  2182. apply_primary_affinity(osdmap, pi, pps, up);
  2183. get_temp_osds(osdmap, pi, &pgid, acting);
  2184. if (!acting->size) {
  2185. memcpy(acting->osds, up->osds, up->size * sizeof(up->osds[0]));
  2186. acting->size = up->size;
  2187. if (acting->primary == -1)
  2188. acting->primary = up->primary;
  2189. }
  2190. WARN_ON(!osds_valid(up) || !osds_valid(acting));
  2191. }
  2192. bool ceph_pg_to_primary_shard(struct ceph_osdmap *osdmap,
  2193. struct ceph_pg_pool_info *pi,
  2194. const struct ceph_pg *raw_pgid,
  2195. struct ceph_spg *spgid)
  2196. {
  2197. struct ceph_pg pgid;
  2198. struct ceph_osds up, acting;
  2199. int i;
  2200. WARN_ON(pi->id != raw_pgid->pool);
  2201. raw_pg_to_pg(pi, raw_pgid, &pgid);
  2202. if (ceph_can_shift_osds(pi)) {
  2203. spgid->pgid = pgid; /* struct */
  2204. spgid->shard = CEPH_SPG_NOSHARD;
  2205. return true;
  2206. }
  2207. ceph_pg_to_up_acting_osds(osdmap, pi, &pgid, &up, &acting);
  2208. for (i = 0; i < acting.size; i++) {
  2209. if (acting.osds[i] == acting.primary) {
  2210. spgid->pgid = pgid; /* struct */
  2211. spgid->shard = i;
  2212. return true;
  2213. }
  2214. }
  2215. return false;
  2216. }
  2217. /*
  2218. * Return acting primary for given PG, or -1 if none.
  2219. */
  2220. int ceph_pg_to_acting_primary(struct ceph_osdmap *osdmap,
  2221. const struct ceph_pg *raw_pgid)
  2222. {
  2223. struct ceph_pg_pool_info *pi;
  2224. struct ceph_osds up, acting;
  2225. pi = ceph_pg_pool_by_id(osdmap, raw_pgid->pool);
  2226. if (!pi)
  2227. return -1;
  2228. ceph_pg_to_up_acting_osds(osdmap, pi, raw_pgid, &up, &acting);
  2229. return acting.primary;
  2230. }
  2231. EXPORT_SYMBOL(ceph_pg_to_acting_primary);