drbd_nl.c 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380
  1. /*
  2. drbd_nl.c
  3. This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
  4. Copyright (C) 2001-2008, LINBIT Information Technologies GmbH.
  5. Copyright (C) 1999-2008, Philipp Reisner <philipp.reisner@linbit.com>.
  6. Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
  7. drbd is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11. drbd is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with drbd; see the file COPYING. If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/drbd.h>
  21. #include <linux/in.h>
  22. #include <linux/fs.h>
  23. #include <linux/file.h>
  24. #include <linux/slab.h>
  25. #include <linux/blkpg.h>
  26. #include <linux/cpumask.h>
  27. #include "drbd_int.h"
  28. #include "drbd_req.h"
  29. #include "drbd_wrappers.h"
  30. #include <asm/unaligned.h>
  31. #include <linux/drbd_limits.h>
  32. #include <linux/kthread.h>
  33. #include <net/genetlink.h>
  34. /* .doit */
  35. // int drbd_adm_create_resource(struct sk_buff *skb, struct genl_info *info);
  36. // int drbd_adm_delete_resource(struct sk_buff *skb, struct genl_info *info);
  37. int drbd_adm_add_minor(struct sk_buff *skb, struct genl_info *info);
  38. int drbd_adm_delete_minor(struct sk_buff *skb, struct genl_info *info);
  39. int drbd_adm_new_resource(struct sk_buff *skb, struct genl_info *info);
  40. int drbd_adm_del_resource(struct sk_buff *skb, struct genl_info *info);
  41. int drbd_adm_down(struct sk_buff *skb, struct genl_info *info);
  42. int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info);
  43. int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info);
  44. int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info);
  45. int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info);
  46. int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info);
  47. int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info);
  48. int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info);
  49. int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info);
  50. int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info);
  51. int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info);
  52. int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info);
  53. int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info);
  54. int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info);
  55. int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info);
  56. int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info);
  57. int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info);
  58. int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info);
  59. int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info);
  60. int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info);
  61. int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info);
  62. /* .dumpit */
  63. int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb);
  64. #include <linux/drbd_genl_api.h>
  65. #include "drbd_nla.h"
  66. #include <linux/genl_magic_func.h>
  67. /* used blkdev_get_by_path, to claim our meta data device(s) */
  68. static char *drbd_m_holder = "Hands off! this is DRBD's meta data device.";
  69. /* Configuration is strictly serialized, because generic netlink message
  70. * processing is strictly serialized by the genl_lock().
  71. * Which means we can use one static global drbd_config_context struct.
  72. */
  73. static struct drbd_config_context {
  74. /* assigned from drbd_genlmsghdr */
  75. unsigned int minor;
  76. /* assigned from request attributes, if present */
  77. unsigned int volume;
  78. #define VOLUME_UNSPECIFIED (-1U)
  79. /* pointer into the request skb,
  80. * limited lifetime! */
  81. char *resource_name;
  82. struct nlattr *my_addr;
  83. struct nlattr *peer_addr;
  84. /* reply buffer */
  85. struct sk_buff *reply_skb;
  86. /* pointer into reply buffer */
  87. struct drbd_genlmsghdr *reply_dh;
  88. /* resolved from attributes, if possible */
  89. struct drbd_conf *mdev;
  90. struct drbd_tconn *tconn;
  91. } adm_ctx;
  92. static void drbd_adm_send_reply(struct sk_buff *skb, struct genl_info *info)
  93. {
  94. genlmsg_end(skb, genlmsg_data(nlmsg_data(nlmsg_hdr(skb))));
  95. if (genlmsg_reply(skb, info))
  96. printk(KERN_ERR "drbd: error sending genl reply\n");
  97. }
  98. /* Used on a fresh "drbd_adm_prepare"d reply_skb, this cannot fail: The only
  99. * reason it could fail was no space in skb, and there are 4k available. */
  100. int drbd_msg_put_info(const char *info)
  101. {
  102. struct sk_buff *skb = adm_ctx.reply_skb;
  103. struct nlattr *nla;
  104. int err = -EMSGSIZE;
  105. if (!info || !info[0])
  106. return 0;
  107. nla = nla_nest_start(skb, DRBD_NLA_CFG_REPLY);
  108. if (!nla)
  109. return err;
  110. err = nla_put_string(skb, T_info_text, info);
  111. if (err) {
  112. nla_nest_cancel(skb, nla);
  113. return err;
  114. } else
  115. nla_nest_end(skb, nla);
  116. return 0;
  117. }
  118. /* This would be a good candidate for a "pre_doit" hook,
  119. * and per-family private info->pointers.
  120. * But we need to stay compatible with older kernels.
  121. * If it returns successfully, adm_ctx members are valid.
  122. */
  123. #define DRBD_ADM_NEED_MINOR 1
  124. #define DRBD_ADM_NEED_RESOURCE 2
  125. #define DRBD_ADM_NEED_CONNECTION 4
  126. static int drbd_adm_prepare(struct sk_buff *skb, struct genl_info *info,
  127. unsigned flags)
  128. {
  129. struct drbd_genlmsghdr *d_in = info->userhdr;
  130. const u8 cmd = info->genlhdr->cmd;
  131. int err;
  132. memset(&adm_ctx, 0, sizeof(adm_ctx));
  133. /* genl_rcv_msg only checks for CAP_NET_ADMIN on "GENL_ADMIN_PERM" :( */
  134. if (cmd != DRBD_ADM_GET_STATUS && !capable(CAP_NET_ADMIN))
  135. return -EPERM;
  136. adm_ctx.reply_skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  137. if (!adm_ctx.reply_skb) {
  138. err = -ENOMEM;
  139. goto fail;
  140. }
  141. adm_ctx.reply_dh = genlmsg_put_reply(adm_ctx.reply_skb,
  142. info, &drbd_genl_family, 0, cmd);
  143. /* put of a few bytes into a fresh skb of >= 4k will always succeed.
  144. * but anyways */
  145. if (!adm_ctx.reply_dh) {
  146. err = -ENOMEM;
  147. goto fail;
  148. }
  149. adm_ctx.reply_dh->minor = d_in->minor;
  150. adm_ctx.reply_dh->ret_code = NO_ERROR;
  151. adm_ctx.volume = VOLUME_UNSPECIFIED;
  152. if (info->attrs[DRBD_NLA_CFG_CONTEXT]) {
  153. struct nlattr *nla;
  154. /* parse and validate only */
  155. err = drbd_cfg_context_from_attrs(NULL, info);
  156. if (err)
  157. goto fail;
  158. /* It was present, and valid,
  159. * copy it over to the reply skb. */
  160. err = nla_put_nohdr(adm_ctx.reply_skb,
  161. info->attrs[DRBD_NLA_CFG_CONTEXT]->nla_len,
  162. info->attrs[DRBD_NLA_CFG_CONTEXT]);
  163. if (err)
  164. goto fail;
  165. /* and assign stuff to the global adm_ctx */
  166. nla = nested_attr_tb[__nla_type(T_ctx_volume)];
  167. if (nla)
  168. adm_ctx.volume = nla_get_u32(nla);
  169. nla = nested_attr_tb[__nla_type(T_ctx_resource_name)];
  170. if (nla)
  171. adm_ctx.resource_name = nla_data(nla);
  172. adm_ctx.my_addr = nested_attr_tb[__nla_type(T_ctx_my_addr)];
  173. adm_ctx.peer_addr = nested_attr_tb[__nla_type(T_ctx_peer_addr)];
  174. if ((adm_ctx.my_addr &&
  175. nla_len(adm_ctx.my_addr) > sizeof(adm_ctx.tconn->my_addr)) ||
  176. (adm_ctx.peer_addr &&
  177. nla_len(adm_ctx.peer_addr) > sizeof(adm_ctx.tconn->peer_addr))) {
  178. err = -EINVAL;
  179. goto fail;
  180. }
  181. }
  182. adm_ctx.minor = d_in->minor;
  183. adm_ctx.mdev = minor_to_mdev(d_in->minor);
  184. adm_ctx.tconn = conn_get_by_name(adm_ctx.resource_name);
  185. if (!adm_ctx.mdev && (flags & DRBD_ADM_NEED_MINOR)) {
  186. drbd_msg_put_info("unknown minor");
  187. return ERR_MINOR_INVALID;
  188. }
  189. if (!adm_ctx.tconn && (flags & DRBD_ADM_NEED_RESOURCE)) {
  190. drbd_msg_put_info("unknown resource");
  191. return ERR_INVALID_REQUEST;
  192. }
  193. if (flags & DRBD_ADM_NEED_CONNECTION) {
  194. if (adm_ctx.tconn && !(flags & DRBD_ADM_NEED_RESOURCE)) {
  195. drbd_msg_put_info("no resource name expected");
  196. return ERR_INVALID_REQUEST;
  197. }
  198. if (adm_ctx.mdev) {
  199. drbd_msg_put_info("no minor number expected");
  200. return ERR_INVALID_REQUEST;
  201. }
  202. if (adm_ctx.my_addr && adm_ctx.peer_addr)
  203. adm_ctx.tconn = conn_get_by_addrs(nla_data(adm_ctx.my_addr),
  204. nla_len(adm_ctx.my_addr),
  205. nla_data(adm_ctx.peer_addr),
  206. nla_len(adm_ctx.peer_addr));
  207. if (!adm_ctx.tconn) {
  208. drbd_msg_put_info("unknown connection");
  209. return ERR_INVALID_REQUEST;
  210. }
  211. }
  212. /* some more paranoia, if the request was over-determined */
  213. if (adm_ctx.mdev && adm_ctx.tconn &&
  214. adm_ctx.mdev->tconn != adm_ctx.tconn) {
  215. pr_warning("request: minor=%u, resource=%s; but that minor belongs to connection %s\n",
  216. adm_ctx.minor, adm_ctx.resource_name,
  217. adm_ctx.mdev->tconn->name);
  218. drbd_msg_put_info("minor exists in different resource");
  219. return ERR_INVALID_REQUEST;
  220. }
  221. if (adm_ctx.mdev &&
  222. adm_ctx.volume != VOLUME_UNSPECIFIED &&
  223. adm_ctx.volume != adm_ctx.mdev->vnr) {
  224. pr_warning("request: minor=%u, volume=%u; but that minor is volume %u in %s\n",
  225. adm_ctx.minor, adm_ctx.volume,
  226. adm_ctx.mdev->vnr, adm_ctx.mdev->tconn->name);
  227. drbd_msg_put_info("minor exists as different volume");
  228. return ERR_INVALID_REQUEST;
  229. }
  230. return NO_ERROR;
  231. fail:
  232. nlmsg_free(adm_ctx.reply_skb);
  233. adm_ctx.reply_skb = NULL;
  234. return err;
  235. }
  236. static int drbd_adm_finish(struct genl_info *info, int retcode)
  237. {
  238. if (adm_ctx.tconn) {
  239. kref_put(&adm_ctx.tconn->kref, &conn_destroy);
  240. adm_ctx.tconn = NULL;
  241. }
  242. if (!adm_ctx.reply_skb)
  243. return -ENOMEM;
  244. adm_ctx.reply_dh->ret_code = retcode;
  245. drbd_adm_send_reply(adm_ctx.reply_skb, info);
  246. return 0;
  247. }
  248. static void setup_khelper_env(struct drbd_tconn *tconn, char **envp)
  249. {
  250. char *afs;
  251. /* FIXME: A future version will not allow this case. */
  252. if (tconn->my_addr_len == 0 || tconn->peer_addr_len == 0)
  253. return;
  254. switch (((struct sockaddr *)&tconn->peer_addr)->sa_family) {
  255. case AF_INET6:
  256. afs = "ipv6";
  257. snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI6",
  258. &((struct sockaddr_in6 *)&tconn->peer_addr)->sin6_addr);
  259. break;
  260. case AF_INET:
  261. afs = "ipv4";
  262. snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
  263. &((struct sockaddr_in *)&tconn->peer_addr)->sin_addr);
  264. break;
  265. default:
  266. afs = "ssocks";
  267. snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
  268. &((struct sockaddr_in *)&tconn->peer_addr)->sin_addr);
  269. }
  270. snprintf(envp[3], 20, "DRBD_PEER_AF=%s", afs);
  271. }
  272. int drbd_khelper(struct drbd_conf *mdev, char *cmd)
  273. {
  274. char *envp[] = { "HOME=/",
  275. "TERM=linux",
  276. "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
  277. (char[20]) { }, /* address family */
  278. (char[60]) { }, /* address */
  279. NULL };
  280. char mb[12];
  281. char *argv[] = {usermode_helper, cmd, mb, NULL };
  282. struct drbd_tconn *tconn = mdev->tconn;
  283. struct sib_info sib;
  284. int ret;
  285. if (current == tconn->worker.task)
  286. set_bit(CALLBACK_PENDING, &tconn->flags);
  287. snprintf(mb, 12, "minor-%d", mdev_to_minor(mdev));
  288. setup_khelper_env(tconn, envp);
  289. /* The helper may take some time.
  290. * write out any unsynced meta data changes now */
  291. drbd_md_sync(mdev);
  292. dev_info(DEV, "helper command: %s %s %s\n", usermode_helper, cmd, mb);
  293. sib.sib_reason = SIB_HELPER_PRE;
  294. sib.helper_name = cmd;
  295. drbd_bcast_event(mdev, &sib);
  296. ret = call_usermodehelper(usermode_helper, argv, envp, UMH_WAIT_PROC);
  297. if (ret)
  298. dev_warn(DEV, "helper command: %s %s %s exit code %u (0x%x)\n",
  299. usermode_helper, cmd, mb,
  300. (ret >> 8) & 0xff, ret);
  301. else
  302. dev_info(DEV, "helper command: %s %s %s exit code %u (0x%x)\n",
  303. usermode_helper, cmd, mb,
  304. (ret >> 8) & 0xff, ret);
  305. sib.sib_reason = SIB_HELPER_POST;
  306. sib.helper_exit_code = ret;
  307. drbd_bcast_event(mdev, &sib);
  308. if (current == tconn->worker.task)
  309. clear_bit(CALLBACK_PENDING, &tconn->flags);
  310. if (ret < 0) /* Ignore any ERRNOs we got. */
  311. ret = 0;
  312. return ret;
  313. }
  314. int conn_khelper(struct drbd_tconn *tconn, char *cmd)
  315. {
  316. char *envp[] = { "HOME=/",
  317. "TERM=linux",
  318. "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
  319. (char[20]) { }, /* address family */
  320. (char[60]) { }, /* address */
  321. NULL };
  322. char *argv[] = {usermode_helper, cmd, tconn->name, NULL };
  323. int ret;
  324. setup_khelper_env(tconn, envp);
  325. conn_md_sync(tconn);
  326. conn_info(tconn, "helper command: %s %s %s\n", usermode_helper, cmd, tconn->name);
  327. /* TODO: conn_bcast_event() ?? */
  328. ret = call_usermodehelper(usermode_helper, argv, envp, UMH_WAIT_PROC);
  329. if (ret)
  330. conn_warn(tconn, "helper command: %s %s %s exit code %u (0x%x)\n",
  331. usermode_helper, cmd, tconn->name,
  332. (ret >> 8) & 0xff, ret);
  333. else
  334. conn_info(tconn, "helper command: %s %s %s exit code %u (0x%x)\n",
  335. usermode_helper, cmd, tconn->name,
  336. (ret >> 8) & 0xff, ret);
  337. /* TODO: conn_bcast_event() ?? */
  338. if (ret < 0) /* Ignore any ERRNOs we got. */
  339. ret = 0;
  340. return ret;
  341. }
  342. static enum drbd_fencing_p highest_fencing_policy(struct drbd_tconn *tconn)
  343. {
  344. enum drbd_fencing_p fp = FP_NOT_AVAIL;
  345. struct drbd_conf *mdev;
  346. int vnr;
  347. rcu_read_lock();
  348. idr_for_each_entry(&tconn->volumes, mdev, vnr) {
  349. if (get_ldev_if_state(mdev, D_CONSISTENT)) {
  350. fp = max_t(enum drbd_fencing_p, fp,
  351. rcu_dereference(mdev->ldev->disk_conf)->fencing);
  352. put_ldev(mdev);
  353. }
  354. }
  355. rcu_read_unlock();
  356. return fp;
  357. }
  358. bool conn_try_outdate_peer(struct drbd_tconn *tconn)
  359. {
  360. union drbd_state mask = { };
  361. union drbd_state val = { };
  362. enum drbd_fencing_p fp;
  363. char *ex_to_string;
  364. int r;
  365. if (tconn->cstate >= C_WF_REPORT_PARAMS) {
  366. conn_err(tconn, "Expected cstate < C_WF_REPORT_PARAMS\n");
  367. return false;
  368. }
  369. fp = highest_fencing_policy(tconn);
  370. switch (fp) {
  371. case FP_NOT_AVAIL:
  372. conn_warn(tconn, "Not fencing peer, I'm not even Consistent myself.\n");
  373. goto out;
  374. case FP_DONT_CARE:
  375. return true;
  376. default: ;
  377. }
  378. r = conn_khelper(tconn, "fence-peer");
  379. switch ((r>>8) & 0xff) {
  380. case 3: /* peer is inconsistent */
  381. ex_to_string = "peer is inconsistent or worse";
  382. mask.pdsk = D_MASK;
  383. val.pdsk = D_INCONSISTENT;
  384. break;
  385. case 4: /* peer got outdated, or was already outdated */
  386. ex_to_string = "peer was fenced";
  387. mask.pdsk = D_MASK;
  388. val.pdsk = D_OUTDATED;
  389. break;
  390. case 5: /* peer was down */
  391. if (conn_highest_disk(tconn) == D_UP_TO_DATE) {
  392. /* we will(have) create(d) a new UUID anyways... */
  393. ex_to_string = "peer is unreachable, assumed to be dead";
  394. mask.pdsk = D_MASK;
  395. val.pdsk = D_OUTDATED;
  396. } else {
  397. ex_to_string = "peer unreachable, doing nothing since disk != UpToDate";
  398. }
  399. break;
  400. case 6: /* Peer is primary, voluntarily outdate myself.
  401. * This is useful when an unconnected R_SECONDARY is asked to
  402. * become R_PRIMARY, but finds the other peer being active. */
  403. ex_to_string = "peer is active";
  404. conn_warn(tconn, "Peer is primary, outdating myself.\n");
  405. mask.disk = D_MASK;
  406. val.disk = D_OUTDATED;
  407. break;
  408. case 7:
  409. if (fp != FP_STONITH)
  410. conn_err(tconn, "fence-peer() = 7 && fencing != Stonith !!!\n");
  411. ex_to_string = "peer was stonithed";
  412. mask.pdsk = D_MASK;
  413. val.pdsk = D_OUTDATED;
  414. break;
  415. default:
  416. /* The script is broken ... */
  417. conn_err(tconn, "fence-peer helper broken, returned %d\n", (r>>8)&0xff);
  418. return false; /* Eventually leave IO frozen */
  419. }
  420. conn_info(tconn, "fence-peer helper returned %d (%s)\n",
  421. (r>>8) & 0xff, ex_to_string);
  422. out:
  423. /* Not using
  424. conn_request_state(tconn, mask, val, CS_VERBOSE);
  425. here, because we might were able to re-establish the connection in the
  426. meantime. */
  427. spin_lock_irq(&tconn->req_lock);
  428. if (tconn->cstate < C_WF_REPORT_PARAMS && !test_bit(STATE_SENT, &tconn->flags))
  429. _conn_request_state(tconn, mask, val, CS_VERBOSE);
  430. spin_unlock_irq(&tconn->req_lock);
  431. return conn_highest_pdsk(tconn) <= D_OUTDATED;
  432. }
  433. static int _try_outdate_peer_async(void *data)
  434. {
  435. struct drbd_tconn *tconn = (struct drbd_tconn *)data;
  436. conn_try_outdate_peer(tconn);
  437. kref_put(&tconn->kref, &conn_destroy);
  438. return 0;
  439. }
  440. void conn_try_outdate_peer_async(struct drbd_tconn *tconn)
  441. {
  442. struct task_struct *opa;
  443. kref_get(&tconn->kref);
  444. opa = kthread_run(_try_outdate_peer_async, tconn, "drbd_async_h");
  445. if (IS_ERR(opa)) {
  446. conn_err(tconn, "out of mem, failed to invoke fence-peer helper\n");
  447. kref_put(&tconn->kref, &conn_destroy);
  448. }
  449. }
  450. enum drbd_state_rv
  451. drbd_set_role(struct drbd_conf *mdev, enum drbd_role new_role, int force)
  452. {
  453. const int max_tries = 4;
  454. enum drbd_state_rv rv = SS_UNKNOWN_ERROR;
  455. struct net_conf *nc;
  456. int try = 0;
  457. int forced = 0;
  458. union drbd_state mask, val;
  459. if (new_role == R_PRIMARY)
  460. request_ping(mdev->tconn); /* Detect a dead peer ASAP */
  461. mutex_lock(mdev->state_mutex);
  462. mask.i = 0; mask.role = R_MASK;
  463. val.i = 0; val.role = new_role;
  464. while (try++ < max_tries) {
  465. rv = _drbd_request_state(mdev, mask, val, CS_WAIT_COMPLETE);
  466. /* in case we first succeeded to outdate,
  467. * but now suddenly could establish a connection */
  468. if (rv == SS_CW_FAILED_BY_PEER && mask.pdsk != 0) {
  469. val.pdsk = 0;
  470. mask.pdsk = 0;
  471. continue;
  472. }
  473. if (rv == SS_NO_UP_TO_DATE_DISK && force &&
  474. (mdev->state.disk < D_UP_TO_DATE &&
  475. mdev->state.disk >= D_INCONSISTENT)) {
  476. mask.disk = D_MASK;
  477. val.disk = D_UP_TO_DATE;
  478. forced = 1;
  479. continue;
  480. }
  481. if (rv == SS_NO_UP_TO_DATE_DISK &&
  482. mdev->state.disk == D_CONSISTENT && mask.pdsk == 0) {
  483. D_ASSERT(mdev->state.pdsk == D_UNKNOWN);
  484. if (conn_try_outdate_peer(mdev->tconn)) {
  485. val.disk = D_UP_TO_DATE;
  486. mask.disk = D_MASK;
  487. }
  488. continue;
  489. }
  490. if (rv == SS_NOTHING_TO_DO)
  491. goto out;
  492. if (rv == SS_PRIMARY_NOP && mask.pdsk == 0) {
  493. if (!conn_try_outdate_peer(mdev->tconn) && force) {
  494. dev_warn(DEV, "Forced into split brain situation!\n");
  495. mask.pdsk = D_MASK;
  496. val.pdsk = D_OUTDATED;
  497. }
  498. continue;
  499. }
  500. if (rv == SS_TWO_PRIMARIES) {
  501. /* Maybe the peer is detected as dead very soon...
  502. retry at most once more in this case. */
  503. int timeo;
  504. rcu_read_lock();
  505. nc = rcu_dereference(mdev->tconn->net_conf);
  506. timeo = nc ? (nc->ping_timeo + 1) * HZ / 10 : 1;
  507. rcu_read_unlock();
  508. schedule_timeout_interruptible(timeo);
  509. if (try < max_tries)
  510. try = max_tries - 1;
  511. continue;
  512. }
  513. if (rv < SS_SUCCESS) {
  514. rv = _drbd_request_state(mdev, mask, val,
  515. CS_VERBOSE + CS_WAIT_COMPLETE);
  516. if (rv < SS_SUCCESS)
  517. goto out;
  518. }
  519. break;
  520. }
  521. if (rv < SS_SUCCESS)
  522. goto out;
  523. if (forced)
  524. dev_warn(DEV, "Forced to consider local data as UpToDate!\n");
  525. /* Wait until nothing is on the fly :) */
  526. wait_event(mdev->misc_wait, atomic_read(&mdev->ap_pending_cnt) == 0);
  527. /* FIXME also wait for all pending P_BARRIER_ACK? */
  528. if (new_role == R_SECONDARY) {
  529. set_disk_ro(mdev->vdisk, true);
  530. if (get_ldev(mdev)) {
  531. mdev->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
  532. put_ldev(mdev);
  533. }
  534. } else {
  535. mutex_lock(&mdev->tconn->conf_update);
  536. nc = mdev->tconn->net_conf;
  537. if (nc)
  538. nc->discard_my_data = 0; /* without copy; single bit op is atomic */
  539. mutex_unlock(&mdev->tconn->conf_update);
  540. set_disk_ro(mdev->vdisk, false);
  541. if (get_ldev(mdev)) {
  542. if (((mdev->state.conn < C_CONNECTED ||
  543. mdev->state.pdsk <= D_FAILED)
  544. && mdev->ldev->md.uuid[UI_BITMAP] == 0) || forced)
  545. drbd_uuid_new_current(mdev);
  546. mdev->ldev->md.uuid[UI_CURRENT] |= (u64)1;
  547. put_ldev(mdev);
  548. }
  549. }
  550. /* writeout of activity log covered areas of the bitmap
  551. * to stable storage done in after state change already */
  552. if (mdev->state.conn >= C_WF_REPORT_PARAMS) {
  553. /* if this was forced, we should consider sync */
  554. if (forced)
  555. drbd_send_uuids(mdev);
  556. drbd_send_current_state(mdev);
  557. }
  558. drbd_md_sync(mdev);
  559. kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
  560. out:
  561. mutex_unlock(mdev->state_mutex);
  562. return rv;
  563. }
  564. static const char *from_attrs_err_to_txt(int err)
  565. {
  566. return err == -ENOMSG ? "required attribute missing" :
  567. err == -EOPNOTSUPP ? "unknown mandatory attribute" :
  568. err == -EEXIST ? "can not change invariant setting" :
  569. "invalid attribute value";
  570. }
  571. int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info)
  572. {
  573. struct set_role_parms parms;
  574. int err;
  575. enum drbd_ret_code retcode;
  576. retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
  577. if (!adm_ctx.reply_skb)
  578. return retcode;
  579. if (retcode != NO_ERROR)
  580. goto out;
  581. memset(&parms, 0, sizeof(parms));
  582. if (info->attrs[DRBD_NLA_SET_ROLE_PARMS]) {
  583. err = set_role_parms_from_attrs(&parms, info);
  584. if (err) {
  585. retcode = ERR_MANDATORY_TAG;
  586. drbd_msg_put_info(from_attrs_err_to_txt(err));
  587. goto out;
  588. }
  589. }
  590. if (info->genlhdr->cmd == DRBD_ADM_PRIMARY)
  591. retcode = drbd_set_role(adm_ctx.mdev, R_PRIMARY, parms.assume_uptodate);
  592. else
  593. retcode = drbd_set_role(adm_ctx.mdev, R_SECONDARY, 0);
  594. out:
  595. drbd_adm_finish(info, retcode);
  596. return 0;
  597. }
  598. /* Initializes the md.*_offset members, so we are able to find
  599. * the on disk meta data.
  600. *
  601. * We currently have two possible layouts:
  602. * external:
  603. * |----------- md_size_sect ------------------|
  604. * [ 4k superblock ][ activity log ][ Bitmap ]
  605. * | al_offset == 8 |
  606. * | bm_offset = al_offset + X |
  607. * ==> bitmap sectors = md_size_sect - bm_offset
  608. *
  609. * internal:
  610. * |----------- md_size_sect ------------------|
  611. * [data.....][ Bitmap ][ activity log ][ 4k superblock ]
  612. * | al_offset < 0 |
  613. * | bm_offset = al_offset - Y |
  614. * ==> bitmap sectors = Y = al_offset - bm_offset
  615. *
  616. * Activity log size used to be fixed 32kB,
  617. * but is about to become configurable.
  618. */
  619. static void drbd_md_set_sector_offsets(struct drbd_conf *mdev,
  620. struct drbd_backing_dev *bdev)
  621. {
  622. sector_t md_size_sect = 0;
  623. unsigned int al_size_sect = bdev->md.al_size_4k * 8;
  624. bdev->md.md_offset = drbd_md_ss(bdev);
  625. switch (bdev->md.meta_dev_idx) {
  626. default:
  627. /* v07 style fixed size indexed meta data */
  628. bdev->md.md_size_sect = MD_128MB_SECT;
  629. bdev->md.al_offset = MD_4kB_SECT;
  630. bdev->md.bm_offset = MD_4kB_SECT + al_size_sect;
  631. break;
  632. case DRBD_MD_INDEX_FLEX_EXT:
  633. /* just occupy the full device; unit: sectors */
  634. bdev->md.md_size_sect = drbd_get_capacity(bdev->md_bdev);
  635. bdev->md.al_offset = MD_4kB_SECT;
  636. bdev->md.bm_offset = MD_4kB_SECT + al_size_sect;
  637. break;
  638. case DRBD_MD_INDEX_INTERNAL:
  639. case DRBD_MD_INDEX_FLEX_INT:
  640. /* al size is still fixed */
  641. bdev->md.al_offset = -al_size_sect;
  642. /* we need (slightly less than) ~ this much bitmap sectors: */
  643. md_size_sect = drbd_get_capacity(bdev->backing_bdev);
  644. md_size_sect = ALIGN(md_size_sect, BM_SECT_PER_EXT);
  645. md_size_sect = BM_SECT_TO_EXT(md_size_sect);
  646. md_size_sect = ALIGN(md_size_sect, 8);
  647. /* plus the "drbd meta data super block",
  648. * and the activity log; */
  649. md_size_sect += MD_4kB_SECT + al_size_sect;
  650. bdev->md.md_size_sect = md_size_sect;
  651. /* bitmap offset is adjusted by 'super' block size */
  652. bdev->md.bm_offset = -md_size_sect + MD_4kB_SECT;
  653. break;
  654. }
  655. }
  656. /* input size is expected to be in KB */
  657. char *ppsize(char *buf, unsigned long long size)
  658. {
  659. /* Needs 9 bytes at max including trailing NUL:
  660. * -1ULL ==> "16384 EB" */
  661. static char units[] = { 'K', 'M', 'G', 'T', 'P', 'E' };
  662. int base = 0;
  663. while (size >= 10000 && base < sizeof(units)-1) {
  664. /* shift + round */
  665. size = (size >> 10) + !!(size & (1<<9));
  666. base++;
  667. }
  668. sprintf(buf, "%u %cB", (unsigned)size, units[base]);
  669. return buf;
  670. }
  671. /* there is still a theoretical deadlock when called from receiver
  672. * on an D_INCONSISTENT R_PRIMARY:
  673. * remote READ does inc_ap_bio, receiver would need to receive answer
  674. * packet from remote to dec_ap_bio again.
  675. * receiver receive_sizes(), comes here,
  676. * waits for ap_bio_cnt == 0. -> deadlock.
  677. * but this cannot happen, actually, because:
  678. * R_PRIMARY D_INCONSISTENT, and peer's disk is unreachable
  679. * (not connected, or bad/no disk on peer):
  680. * see drbd_fail_request_early, ap_bio_cnt is zero.
  681. * R_PRIMARY D_INCONSISTENT, and C_SYNC_TARGET:
  682. * peer may not initiate a resize.
  683. */
  684. /* Note these are not to be confused with
  685. * drbd_adm_suspend_io/drbd_adm_resume_io,
  686. * which are (sub) state changes triggered by admin (drbdsetup),
  687. * and can be long lived.
  688. * This changes an mdev->flag, is triggered by drbd internals,
  689. * and should be short-lived. */
  690. void drbd_suspend_io(struct drbd_conf *mdev)
  691. {
  692. set_bit(SUSPEND_IO, &mdev->flags);
  693. if (drbd_suspended(mdev))
  694. return;
  695. wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_bio_cnt));
  696. }
  697. void drbd_resume_io(struct drbd_conf *mdev)
  698. {
  699. clear_bit(SUSPEND_IO, &mdev->flags);
  700. wake_up(&mdev->misc_wait);
  701. }
  702. /**
  703. * drbd_determine_dev_size() - Sets the right device size obeying all constraints
  704. * @mdev: DRBD device.
  705. *
  706. * Returns 0 on success, negative return values indicate errors.
  707. * You should call drbd_md_sync() after calling this function.
  708. */
  709. enum determine_dev_size drbd_determine_dev_size(struct drbd_conf *mdev, enum dds_flags flags) __must_hold(local)
  710. {
  711. sector_t prev_first_sect, prev_size; /* previous meta location */
  712. sector_t la_size_sect, u_size;
  713. sector_t size;
  714. char ppb[10];
  715. int md_moved, la_size_changed;
  716. enum determine_dev_size rv = unchanged;
  717. /* race:
  718. * application request passes inc_ap_bio,
  719. * but then cannot get an AL-reference.
  720. * this function later may wait on ap_bio_cnt == 0. -> deadlock.
  721. *
  722. * to avoid that:
  723. * Suspend IO right here.
  724. * still lock the act_log to not trigger ASSERTs there.
  725. */
  726. drbd_suspend_io(mdev);
  727. /* no wait necessary anymore, actually we could assert that */
  728. wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
  729. prev_first_sect = drbd_md_first_sector(mdev->ldev);
  730. prev_size = mdev->ldev->md.md_size_sect;
  731. la_size_sect = mdev->ldev->md.la_size_sect;
  732. /* TODO: should only be some assert here, not (re)init... */
  733. drbd_md_set_sector_offsets(mdev, mdev->ldev);
  734. rcu_read_lock();
  735. u_size = rcu_dereference(mdev->ldev->disk_conf)->disk_size;
  736. rcu_read_unlock();
  737. size = drbd_new_dev_size(mdev, mdev->ldev, u_size, flags & DDSF_FORCED);
  738. if (drbd_get_capacity(mdev->this_bdev) != size ||
  739. drbd_bm_capacity(mdev) != size) {
  740. int err;
  741. err = drbd_bm_resize(mdev, size, !(flags & DDSF_NO_RESYNC));
  742. if (unlikely(err)) {
  743. /* currently there is only one error: ENOMEM! */
  744. size = drbd_bm_capacity(mdev)>>1;
  745. if (size == 0) {
  746. dev_err(DEV, "OUT OF MEMORY! "
  747. "Could not allocate bitmap!\n");
  748. } else {
  749. dev_err(DEV, "BM resizing failed. "
  750. "Leaving size unchanged at size = %lu KB\n",
  751. (unsigned long)size);
  752. }
  753. rv = dev_size_error;
  754. }
  755. /* racy, see comments above. */
  756. drbd_set_my_capacity(mdev, size);
  757. mdev->ldev->md.la_size_sect = size;
  758. dev_info(DEV, "size = %s (%llu KB)\n", ppsize(ppb, size>>1),
  759. (unsigned long long)size>>1);
  760. }
  761. if (rv == dev_size_error)
  762. goto out;
  763. la_size_changed = (la_size_sect != mdev->ldev->md.la_size_sect);
  764. md_moved = prev_first_sect != drbd_md_first_sector(mdev->ldev)
  765. || prev_size != mdev->ldev->md.md_size_sect;
  766. if (la_size_changed || md_moved) {
  767. int err;
  768. drbd_al_shrink(mdev); /* All extents inactive. */
  769. dev_info(DEV, "Writing the whole bitmap, %s\n",
  770. la_size_changed && md_moved ? "size changed and md moved" :
  771. la_size_changed ? "size changed" : "md moved");
  772. /* next line implicitly does drbd_suspend_io()+drbd_resume_io() */
  773. err = drbd_bitmap_io(mdev, md_moved ? &drbd_bm_write_all : &drbd_bm_write,
  774. "size changed", BM_LOCKED_MASK);
  775. if (err) {
  776. rv = dev_size_error;
  777. goto out;
  778. }
  779. drbd_md_mark_dirty(mdev);
  780. }
  781. if (size > la_size_sect)
  782. rv = grew;
  783. if (size < la_size_sect)
  784. rv = shrunk;
  785. out:
  786. lc_unlock(mdev->act_log);
  787. wake_up(&mdev->al_wait);
  788. drbd_resume_io(mdev);
  789. return rv;
  790. }
  791. sector_t
  792. drbd_new_dev_size(struct drbd_conf *mdev, struct drbd_backing_dev *bdev,
  793. sector_t u_size, int assume_peer_has_space)
  794. {
  795. sector_t p_size = mdev->p_size; /* partner's disk size. */
  796. sector_t la_size_sect = bdev->md.la_size_sect; /* last agreed size. */
  797. sector_t m_size; /* my size */
  798. sector_t size = 0;
  799. m_size = drbd_get_max_capacity(bdev);
  800. if (mdev->state.conn < C_CONNECTED && assume_peer_has_space) {
  801. dev_warn(DEV, "Resize while not connected was forced by the user!\n");
  802. p_size = m_size;
  803. }
  804. if (p_size && m_size) {
  805. size = min_t(sector_t, p_size, m_size);
  806. } else {
  807. if (la_size_sect) {
  808. size = la_size_sect;
  809. if (m_size && m_size < size)
  810. size = m_size;
  811. if (p_size && p_size < size)
  812. size = p_size;
  813. } else {
  814. if (m_size)
  815. size = m_size;
  816. if (p_size)
  817. size = p_size;
  818. }
  819. }
  820. if (size == 0)
  821. dev_err(DEV, "Both nodes diskless!\n");
  822. if (u_size) {
  823. if (u_size > size)
  824. dev_err(DEV, "Requested disk size is too big (%lu > %lu)\n",
  825. (unsigned long)u_size>>1, (unsigned long)size>>1);
  826. else
  827. size = u_size;
  828. }
  829. return size;
  830. }
  831. /**
  832. * drbd_check_al_size() - Ensures that the AL is of the right size
  833. * @mdev: DRBD device.
  834. *
  835. * Returns -EBUSY if current al lru is still used, -ENOMEM when allocation
  836. * failed, and 0 on success. You should call drbd_md_sync() after you called
  837. * this function.
  838. */
  839. static int drbd_check_al_size(struct drbd_conf *mdev, struct disk_conf *dc)
  840. {
  841. struct lru_cache *n, *t;
  842. struct lc_element *e;
  843. unsigned int in_use;
  844. int i;
  845. if (mdev->act_log &&
  846. mdev->act_log->nr_elements == dc->al_extents)
  847. return 0;
  848. in_use = 0;
  849. t = mdev->act_log;
  850. n = lc_create("act_log", drbd_al_ext_cache, AL_UPDATES_PER_TRANSACTION,
  851. dc->al_extents, sizeof(struct lc_element), 0);
  852. if (n == NULL) {
  853. dev_err(DEV, "Cannot allocate act_log lru!\n");
  854. return -ENOMEM;
  855. }
  856. spin_lock_irq(&mdev->al_lock);
  857. if (t) {
  858. for (i = 0; i < t->nr_elements; i++) {
  859. e = lc_element_by_index(t, i);
  860. if (e->refcnt)
  861. dev_err(DEV, "refcnt(%d)==%d\n",
  862. e->lc_number, e->refcnt);
  863. in_use += e->refcnt;
  864. }
  865. }
  866. if (!in_use)
  867. mdev->act_log = n;
  868. spin_unlock_irq(&mdev->al_lock);
  869. if (in_use) {
  870. dev_err(DEV, "Activity log still in use!\n");
  871. lc_destroy(n);
  872. return -EBUSY;
  873. } else {
  874. if (t)
  875. lc_destroy(t);
  876. }
  877. drbd_md_mark_dirty(mdev); /* we changed mdev->act_log->nr_elemens */
  878. return 0;
  879. }
  880. static void drbd_setup_queue_param(struct drbd_conf *mdev, unsigned int max_bio_size)
  881. {
  882. struct request_queue * const q = mdev->rq_queue;
  883. unsigned int max_hw_sectors = max_bio_size >> 9;
  884. unsigned int max_segments = 0;
  885. if (get_ldev_if_state(mdev, D_ATTACHING)) {
  886. struct request_queue * const b = mdev->ldev->backing_bdev->bd_disk->queue;
  887. max_hw_sectors = min(queue_max_hw_sectors(b), max_bio_size >> 9);
  888. rcu_read_lock();
  889. max_segments = rcu_dereference(mdev->ldev->disk_conf)->max_bio_bvecs;
  890. rcu_read_unlock();
  891. put_ldev(mdev);
  892. }
  893. blk_queue_logical_block_size(q, 512);
  894. blk_queue_max_hw_sectors(q, max_hw_sectors);
  895. /* This is the workaround for "bio would need to, but cannot, be split" */
  896. blk_queue_max_segments(q, max_segments ? max_segments : BLK_MAX_SEGMENTS);
  897. blk_queue_segment_boundary(q, PAGE_CACHE_SIZE-1);
  898. if (get_ldev_if_state(mdev, D_ATTACHING)) {
  899. struct request_queue * const b = mdev->ldev->backing_bdev->bd_disk->queue;
  900. blk_queue_stack_limits(q, b);
  901. if (q->backing_dev_info.ra_pages != b->backing_dev_info.ra_pages) {
  902. dev_info(DEV, "Adjusting my ra_pages to backing device's (%lu -> %lu)\n",
  903. q->backing_dev_info.ra_pages,
  904. b->backing_dev_info.ra_pages);
  905. q->backing_dev_info.ra_pages = b->backing_dev_info.ra_pages;
  906. }
  907. put_ldev(mdev);
  908. }
  909. }
  910. void drbd_reconsider_max_bio_size(struct drbd_conf *mdev)
  911. {
  912. unsigned int now, new, local, peer;
  913. now = queue_max_hw_sectors(mdev->rq_queue) << 9;
  914. local = mdev->local_max_bio_size; /* Eventually last known value, from volatile memory */
  915. peer = mdev->peer_max_bio_size; /* Eventually last known value, from meta data */
  916. if (get_ldev_if_state(mdev, D_ATTACHING)) {
  917. local = queue_max_hw_sectors(mdev->ldev->backing_bdev->bd_disk->queue) << 9;
  918. mdev->local_max_bio_size = local;
  919. put_ldev(mdev);
  920. }
  921. local = min(local, DRBD_MAX_BIO_SIZE);
  922. /* We may ignore peer limits if the peer is modern enough.
  923. Because new from 8.3.8 onwards the peer can use multiple
  924. BIOs for a single peer_request */
  925. if (mdev->state.conn >= C_CONNECTED) {
  926. if (mdev->tconn->agreed_pro_version < 94)
  927. peer = min( mdev->peer_max_bio_size, DRBD_MAX_SIZE_H80_PACKET);
  928. /* Correct old drbd (up to 8.3.7) if it believes it can do more than 32KiB */
  929. else if (mdev->tconn->agreed_pro_version == 94)
  930. peer = DRBD_MAX_SIZE_H80_PACKET;
  931. else if (mdev->tconn->agreed_pro_version < 100)
  932. peer = DRBD_MAX_BIO_SIZE_P95; /* drbd 8.3.8 onwards, before 8.4.0 */
  933. else
  934. peer = DRBD_MAX_BIO_SIZE;
  935. }
  936. new = min(local, peer);
  937. if (mdev->state.role == R_PRIMARY && new < now)
  938. dev_err(DEV, "ASSERT FAILED new < now; (%u < %u)\n", new, now);
  939. if (new != now)
  940. dev_info(DEV, "max BIO size = %u\n", new);
  941. drbd_setup_queue_param(mdev, new);
  942. }
  943. /* Starts the worker thread */
  944. static void conn_reconfig_start(struct drbd_tconn *tconn)
  945. {
  946. drbd_thread_start(&tconn->worker);
  947. conn_flush_workqueue(tconn);
  948. }
  949. /* if still unconfigured, stops worker again. */
  950. static void conn_reconfig_done(struct drbd_tconn *tconn)
  951. {
  952. bool stop_threads;
  953. spin_lock_irq(&tconn->req_lock);
  954. stop_threads = conn_all_vols_unconf(tconn) &&
  955. tconn->cstate == C_STANDALONE;
  956. spin_unlock_irq(&tconn->req_lock);
  957. if (stop_threads) {
  958. /* asender is implicitly stopped by receiver
  959. * in conn_disconnect() */
  960. drbd_thread_stop(&tconn->receiver);
  961. drbd_thread_stop(&tconn->worker);
  962. }
  963. }
  964. /* Make sure IO is suspended before calling this function(). */
  965. static void drbd_suspend_al(struct drbd_conf *mdev)
  966. {
  967. int s = 0;
  968. if (!lc_try_lock(mdev->act_log)) {
  969. dev_warn(DEV, "Failed to lock al in drbd_suspend_al()\n");
  970. return;
  971. }
  972. drbd_al_shrink(mdev);
  973. spin_lock_irq(&mdev->tconn->req_lock);
  974. if (mdev->state.conn < C_CONNECTED)
  975. s = !test_and_set_bit(AL_SUSPENDED, &mdev->flags);
  976. spin_unlock_irq(&mdev->tconn->req_lock);
  977. lc_unlock(mdev->act_log);
  978. if (s)
  979. dev_info(DEV, "Suspended AL updates\n");
  980. }
  981. static bool should_set_defaults(struct genl_info *info)
  982. {
  983. unsigned flags = ((struct drbd_genlmsghdr*)info->userhdr)->flags;
  984. return 0 != (flags & DRBD_GENL_F_SET_DEFAULTS);
  985. }
  986. static unsigned int drbd_al_extents_max(struct drbd_backing_dev *bdev)
  987. {
  988. /* This is limited by 16 bit "slot" numbers,
  989. * and by available on-disk context storage.
  990. *
  991. * Also (u16)~0 is special (denotes a "free" extent).
  992. *
  993. * One transaction occupies one 4kB on-disk block,
  994. * we have n such blocks in the on disk ring buffer,
  995. * the "current" transaction may fail (n-1),
  996. * and there is 919 slot numbers context information per transaction.
  997. *
  998. * 72 transaction blocks amounts to more than 2**16 context slots,
  999. * so cap there first.
  1000. */
  1001. const unsigned int max_al_nr = DRBD_AL_EXTENTS_MAX;
  1002. const unsigned int sufficient_on_disk =
  1003. (max_al_nr + AL_CONTEXT_PER_TRANSACTION -1)
  1004. /AL_CONTEXT_PER_TRANSACTION;
  1005. unsigned int al_size_4k = bdev->md.al_size_4k;
  1006. if (al_size_4k > sufficient_on_disk)
  1007. return max_al_nr;
  1008. return (al_size_4k - 1) * AL_CONTEXT_PER_TRANSACTION;
  1009. }
  1010. int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info)
  1011. {
  1012. enum drbd_ret_code retcode;
  1013. struct drbd_conf *mdev;
  1014. struct disk_conf *new_disk_conf, *old_disk_conf;
  1015. struct fifo_buffer *old_plan = NULL, *new_plan = NULL;
  1016. int err, fifo_size;
  1017. retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
  1018. if (!adm_ctx.reply_skb)
  1019. return retcode;
  1020. if (retcode != NO_ERROR)
  1021. goto out;
  1022. mdev = adm_ctx.mdev;
  1023. /* we also need a disk
  1024. * to change the options on */
  1025. if (!get_ldev(mdev)) {
  1026. retcode = ERR_NO_DISK;
  1027. goto out;
  1028. }
  1029. new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
  1030. if (!new_disk_conf) {
  1031. retcode = ERR_NOMEM;
  1032. goto fail;
  1033. }
  1034. mutex_lock(&mdev->tconn->conf_update);
  1035. old_disk_conf = mdev->ldev->disk_conf;
  1036. *new_disk_conf = *old_disk_conf;
  1037. if (should_set_defaults(info))
  1038. set_disk_conf_defaults(new_disk_conf);
  1039. err = disk_conf_from_attrs_for_change(new_disk_conf, info);
  1040. if (err && err != -ENOMSG) {
  1041. retcode = ERR_MANDATORY_TAG;
  1042. drbd_msg_put_info(from_attrs_err_to_txt(err));
  1043. }
  1044. if (!expect(new_disk_conf->resync_rate >= 1))
  1045. new_disk_conf->resync_rate = 1;
  1046. if (new_disk_conf->al_extents < DRBD_AL_EXTENTS_MIN)
  1047. new_disk_conf->al_extents = DRBD_AL_EXTENTS_MIN;
  1048. if (new_disk_conf->al_extents > drbd_al_extents_max(mdev->ldev))
  1049. new_disk_conf->al_extents = drbd_al_extents_max(mdev->ldev);
  1050. if (new_disk_conf->c_plan_ahead > DRBD_C_PLAN_AHEAD_MAX)
  1051. new_disk_conf->c_plan_ahead = DRBD_C_PLAN_AHEAD_MAX;
  1052. fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
  1053. if (fifo_size != mdev->rs_plan_s->size) {
  1054. new_plan = fifo_alloc(fifo_size);
  1055. if (!new_plan) {
  1056. dev_err(DEV, "kmalloc of fifo_buffer failed");
  1057. retcode = ERR_NOMEM;
  1058. goto fail_unlock;
  1059. }
  1060. }
  1061. drbd_suspend_io(mdev);
  1062. wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
  1063. drbd_al_shrink(mdev);
  1064. err = drbd_check_al_size(mdev, new_disk_conf);
  1065. lc_unlock(mdev->act_log);
  1066. wake_up(&mdev->al_wait);
  1067. drbd_resume_io(mdev);
  1068. if (err) {
  1069. retcode = ERR_NOMEM;
  1070. goto fail_unlock;
  1071. }
  1072. write_lock_irq(&global_state_lock);
  1073. retcode = drbd_resync_after_valid(mdev, new_disk_conf->resync_after);
  1074. if (retcode == NO_ERROR) {
  1075. rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
  1076. drbd_resync_after_changed(mdev);
  1077. }
  1078. write_unlock_irq(&global_state_lock);
  1079. if (retcode != NO_ERROR)
  1080. goto fail_unlock;
  1081. if (new_plan) {
  1082. old_plan = mdev->rs_plan_s;
  1083. rcu_assign_pointer(mdev->rs_plan_s, new_plan);
  1084. }
  1085. mutex_unlock(&mdev->tconn->conf_update);
  1086. if (new_disk_conf->al_updates)
  1087. mdev->ldev->md.flags &= ~MDF_AL_DISABLED;
  1088. else
  1089. mdev->ldev->md.flags |= MDF_AL_DISABLED;
  1090. if (new_disk_conf->md_flushes)
  1091. clear_bit(MD_NO_FUA, &mdev->flags);
  1092. else
  1093. set_bit(MD_NO_FUA, &mdev->flags);
  1094. drbd_bump_write_ordering(mdev->tconn, WO_bdev_flush);
  1095. drbd_md_sync(mdev);
  1096. if (mdev->state.conn >= C_CONNECTED)
  1097. drbd_send_sync_param(mdev);
  1098. synchronize_rcu();
  1099. kfree(old_disk_conf);
  1100. kfree(old_plan);
  1101. mod_timer(&mdev->request_timer, jiffies + HZ);
  1102. goto success;
  1103. fail_unlock:
  1104. mutex_unlock(&mdev->tconn->conf_update);
  1105. fail:
  1106. kfree(new_disk_conf);
  1107. kfree(new_plan);
  1108. success:
  1109. put_ldev(mdev);
  1110. out:
  1111. drbd_adm_finish(info, retcode);
  1112. return 0;
  1113. }
  1114. int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info)
  1115. {
  1116. struct drbd_conf *mdev;
  1117. int err;
  1118. enum drbd_ret_code retcode;
  1119. enum determine_dev_size dd;
  1120. sector_t max_possible_sectors;
  1121. sector_t min_md_device_sectors;
  1122. struct drbd_backing_dev *nbc = NULL; /* new_backing_conf */
  1123. struct disk_conf *new_disk_conf = NULL;
  1124. struct block_device *bdev;
  1125. struct lru_cache *resync_lru = NULL;
  1126. struct fifo_buffer *new_plan = NULL;
  1127. union drbd_state ns, os;
  1128. enum drbd_state_rv rv;
  1129. struct net_conf *nc;
  1130. retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
  1131. if (!adm_ctx.reply_skb)
  1132. return retcode;
  1133. if (retcode != NO_ERROR)
  1134. goto finish;
  1135. mdev = adm_ctx.mdev;
  1136. conn_reconfig_start(mdev->tconn);
  1137. /* if you want to reconfigure, please tear down first */
  1138. if (mdev->state.disk > D_DISKLESS) {
  1139. retcode = ERR_DISK_CONFIGURED;
  1140. goto fail;
  1141. }
  1142. /* It may just now have detached because of IO error. Make sure
  1143. * drbd_ldev_destroy is done already, we may end up here very fast,
  1144. * e.g. if someone calls attach from the on-io-error handler,
  1145. * to realize a "hot spare" feature (not that I'd recommend that) */
  1146. wait_event(mdev->misc_wait, !atomic_read(&mdev->local_cnt));
  1147. /* make sure there is no leftover from previous force-detach attempts */
  1148. clear_bit(FORCE_DETACH, &mdev->flags);
  1149. clear_bit(WAS_IO_ERROR, &mdev->flags);
  1150. clear_bit(WAS_READ_ERROR, &mdev->flags);
  1151. /* and no leftover from previously aborted resync or verify, either */
  1152. mdev->rs_total = 0;
  1153. mdev->rs_failed = 0;
  1154. atomic_set(&mdev->rs_pending_cnt, 0);
  1155. /* allocation not in the IO path, drbdsetup context */
  1156. nbc = kzalloc(sizeof(struct drbd_backing_dev), GFP_KERNEL);
  1157. if (!nbc) {
  1158. retcode = ERR_NOMEM;
  1159. goto fail;
  1160. }
  1161. spin_lock_init(&nbc->md.uuid_lock);
  1162. new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
  1163. if (!new_disk_conf) {
  1164. retcode = ERR_NOMEM;
  1165. goto fail;
  1166. }
  1167. nbc->disk_conf = new_disk_conf;
  1168. set_disk_conf_defaults(new_disk_conf);
  1169. err = disk_conf_from_attrs(new_disk_conf, info);
  1170. if (err) {
  1171. retcode = ERR_MANDATORY_TAG;
  1172. drbd_msg_put_info(from_attrs_err_to_txt(err));
  1173. goto fail;
  1174. }
  1175. if (new_disk_conf->c_plan_ahead > DRBD_C_PLAN_AHEAD_MAX)
  1176. new_disk_conf->c_plan_ahead = DRBD_C_PLAN_AHEAD_MAX;
  1177. new_plan = fifo_alloc((new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ);
  1178. if (!new_plan) {
  1179. retcode = ERR_NOMEM;
  1180. goto fail;
  1181. }
  1182. if (new_disk_conf->meta_dev_idx < DRBD_MD_INDEX_FLEX_INT) {
  1183. retcode = ERR_MD_IDX_INVALID;
  1184. goto fail;
  1185. }
  1186. rcu_read_lock();
  1187. nc = rcu_dereference(mdev->tconn->net_conf);
  1188. if (nc) {
  1189. if (new_disk_conf->fencing == FP_STONITH && nc->wire_protocol == DRBD_PROT_A) {
  1190. rcu_read_unlock();
  1191. retcode = ERR_STONITH_AND_PROT_A;
  1192. goto fail;
  1193. }
  1194. }
  1195. rcu_read_unlock();
  1196. bdev = blkdev_get_by_path(new_disk_conf->backing_dev,
  1197. FMODE_READ | FMODE_WRITE | FMODE_EXCL, mdev);
  1198. if (IS_ERR(bdev)) {
  1199. dev_err(DEV, "open(\"%s\") failed with %ld\n", new_disk_conf->backing_dev,
  1200. PTR_ERR(bdev));
  1201. retcode = ERR_OPEN_DISK;
  1202. goto fail;
  1203. }
  1204. nbc->backing_bdev = bdev;
  1205. /*
  1206. * meta_dev_idx >= 0: external fixed size, possibly multiple
  1207. * drbd sharing one meta device. TODO in that case, paranoia
  1208. * check that [md_bdev, meta_dev_idx] is not yet used by some
  1209. * other drbd minor! (if you use drbd.conf + drbdadm, that
  1210. * should check it for you already; but if you don't, or
  1211. * someone fooled it, we need to double check here)
  1212. */
  1213. bdev = blkdev_get_by_path(new_disk_conf->meta_dev,
  1214. FMODE_READ | FMODE_WRITE | FMODE_EXCL,
  1215. (new_disk_conf->meta_dev_idx < 0) ?
  1216. (void *)mdev : (void *)drbd_m_holder);
  1217. if (IS_ERR(bdev)) {
  1218. dev_err(DEV, "open(\"%s\") failed with %ld\n", new_disk_conf->meta_dev,
  1219. PTR_ERR(bdev));
  1220. retcode = ERR_OPEN_MD_DISK;
  1221. goto fail;
  1222. }
  1223. nbc->md_bdev = bdev;
  1224. if ((nbc->backing_bdev == nbc->md_bdev) !=
  1225. (new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_INTERNAL ||
  1226. new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_FLEX_INT)) {
  1227. retcode = ERR_MD_IDX_INVALID;
  1228. goto fail;
  1229. }
  1230. resync_lru = lc_create("resync", drbd_bm_ext_cache,
  1231. 1, 61, sizeof(struct bm_extent),
  1232. offsetof(struct bm_extent, lce));
  1233. if (!resync_lru) {
  1234. retcode = ERR_NOMEM;
  1235. goto fail;
  1236. }
  1237. /* Read our meta data super block early.
  1238. * This also sets other on-disk offsets. */
  1239. retcode = drbd_md_read(mdev, nbc);
  1240. if (retcode != NO_ERROR)
  1241. goto fail;
  1242. if (new_disk_conf->al_extents < DRBD_AL_EXTENTS_MIN)
  1243. new_disk_conf->al_extents = DRBD_AL_EXTENTS_MIN;
  1244. if (new_disk_conf->al_extents > drbd_al_extents_max(nbc))
  1245. new_disk_conf->al_extents = drbd_al_extents_max(nbc);
  1246. if (drbd_get_max_capacity(nbc) < new_disk_conf->disk_size) {
  1247. dev_err(DEV, "max capacity %llu smaller than disk size %llu\n",
  1248. (unsigned long long) drbd_get_max_capacity(nbc),
  1249. (unsigned long long) new_disk_conf->disk_size);
  1250. retcode = ERR_DISK_TOO_SMALL;
  1251. goto fail;
  1252. }
  1253. if (new_disk_conf->meta_dev_idx < 0) {
  1254. max_possible_sectors = DRBD_MAX_SECTORS_FLEX;
  1255. /* at least one MB, otherwise it does not make sense */
  1256. min_md_device_sectors = (2<<10);
  1257. } else {
  1258. max_possible_sectors = DRBD_MAX_SECTORS;
  1259. min_md_device_sectors = MD_128MB_SECT * (new_disk_conf->meta_dev_idx + 1);
  1260. }
  1261. if (drbd_get_capacity(nbc->md_bdev) < min_md_device_sectors) {
  1262. retcode = ERR_MD_DISK_TOO_SMALL;
  1263. dev_warn(DEV, "refusing attach: md-device too small, "
  1264. "at least %llu sectors needed for this meta-disk type\n",
  1265. (unsigned long long) min_md_device_sectors);
  1266. goto fail;
  1267. }
  1268. /* Make sure the new disk is big enough
  1269. * (we may currently be R_PRIMARY with no local disk...) */
  1270. if (drbd_get_max_capacity(nbc) <
  1271. drbd_get_capacity(mdev->this_bdev)) {
  1272. retcode = ERR_DISK_TOO_SMALL;
  1273. goto fail;
  1274. }
  1275. nbc->known_size = drbd_get_capacity(nbc->backing_bdev);
  1276. if (nbc->known_size > max_possible_sectors) {
  1277. dev_warn(DEV, "==> truncating very big lower level device "
  1278. "to currently maximum possible %llu sectors <==\n",
  1279. (unsigned long long) max_possible_sectors);
  1280. if (new_disk_conf->meta_dev_idx >= 0)
  1281. dev_warn(DEV, "==>> using internal or flexible "
  1282. "meta data may help <<==\n");
  1283. }
  1284. drbd_suspend_io(mdev);
  1285. /* also wait for the last barrier ack. */
  1286. /* FIXME see also https://daiquiri.linbit/cgi-bin/bugzilla/show_bug.cgi?id=171
  1287. * We need a way to either ignore barrier acks for barriers sent before a device
  1288. * was attached, or a way to wait for all pending barrier acks to come in.
  1289. * As barriers are counted per resource,
  1290. * we'd need to suspend io on all devices of a resource.
  1291. */
  1292. wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_pending_cnt) || drbd_suspended(mdev));
  1293. /* and for any other previously queued work */
  1294. drbd_flush_workqueue(mdev);
  1295. rv = _drbd_request_state(mdev, NS(disk, D_ATTACHING), CS_VERBOSE);
  1296. retcode = rv; /* FIXME: Type mismatch. */
  1297. drbd_resume_io(mdev);
  1298. if (rv < SS_SUCCESS)
  1299. goto fail;
  1300. if (!get_ldev_if_state(mdev, D_ATTACHING))
  1301. goto force_diskless;
  1302. if (!mdev->bitmap) {
  1303. if (drbd_bm_init(mdev)) {
  1304. retcode = ERR_NOMEM;
  1305. goto force_diskless_dec;
  1306. }
  1307. }
  1308. if (mdev->state.conn < C_CONNECTED &&
  1309. mdev->state.role == R_PRIMARY &&
  1310. (mdev->ed_uuid & ~((u64)1)) != (nbc->md.uuid[UI_CURRENT] & ~((u64)1))) {
  1311. dev_err(DEV, "Can only attach to data with current UUID=%016llX\n",
  1312. (unsigned long long)mdev->ed_uuid);
  1313. retcode = ERR_DATA_NOT_CURRENT;
  1314. goto force_diskless_dec;
  1315. }
  1316. /* Since we are diskless, fix the activity log first... */
  1317. if (drbd_check_al_size(mdev, new_disk_conf)) {
  1318. retcode = ERR_NOMEM;
  1319. goto force_diskless_dec;
  1320. }
  1321. /* Prevent shrinking of consistent devices ! */
  1322. if (drbd_md_test_flag(nbc, MDF_CONSISTENT) &&
  1323. drbd_new_dev_size(mdev, nbc, nbc->disk_conf->disk_size, 0) < nbc->md.la_size_sect) {
  1324. dev_warn(DEV, "refusing to truncate a consistent device\n");
  1325. retcode = ERR_DISK_TOO_SMALL;
  1326. goto force_diskless_dec;
  1327. }
  1328. /* Reset the "barriers don't work" bits here, then force meta data to
  1329. * be written, to ensure we determine if barriers are supported. */
  1330. if (new_disk_conf->md_flushes)
  1331. clear_bit(MD_NO_FUA, &mdev->flags);
  1332. else
  1333. set_bit(MD_NO_FUA, &mdev->flags);
  1334. /* Point of no return reached.
  1335. * Devices and memory are no longer released by error cleanup below.
  1336. * now mdev takes over responsibility, and the state engine should
  1337. * clean it up somewhere. */
  1338. D_ASSERT(mdev->ldev == NULL);
  1339. mdev->ldev = nbc;
  1340. mdev->resync = resync_lru;
  1341. mdev->rs_plan_s = new_plan;
  1342. nbc = NULL;
  1343. resync_lru = NULL;
  1344. new_disk_conf = NULL;
  1345. new_plan = NULL;
  1346. drbd_bump_write_ordering(mdev->tconn, WO_bdev_flush);
  1347. if (drbd_md_test_flag(mdev->ldev, MDF_CRASHED_PRIMARY))
  1348. set_bit(CRASHED_PRIMARY, &mdev->flags);
  1349. else
  1350. clear_bit(CRASHED_PRIMARY, &mdev->flags);
  1351. if (drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) &&
  1352. !(mdev->state.role == R_PRIMARY && mdev->tconn->susp_nod))
  1353. set_bit(CRASHED_PRIMARY, &mdev->flags);
  1354. mdev->send_cnt = 0;
  1355. mdev->recv_cnt = 0;
  1356. mdev->read_cnt = 0;
  1357. mdev->writ_cnt = 0;
  1358. drbd_reconsider_max_bio_size(mdev);
  1359. /* If I am currently not R_PRIMARY,
  1360. * but meta data primary indicator is set,
  1361. * I just now recover from a hard crash,
  1362. * and have been R_PRIMARY before that crash.
  1363. *
  1364. * Now, if I had no connection before that crash
  1365. * (have been degraded R_PRIMARY), chances are that
  1366. * I won't find my peer now either.
  1367. *
  1368. * In that case, and _only_ in that case,
  1369. * we use the degr-wfc-timeout instead of the default,
  1370. * so we can automatically recover from a crash of a
  1371. * degraded but active "cluster" after a certain timeout.
  1372. */
  1373. clear_bit(USE_DEGR_WFC_T, &mdev->flags);
  1374. if (mdev->state.role != R_PRIMARY &&
  1375. drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) &&
  1376. !drbd_md_test_flag(mdev->ldev, MDF_CONNECTED_IND))
  1377. set_bit(USE_DEGR_WFC_T, &mdev->flags);
  1378. dd = drbd_determine_dev_size(mdev, 0);
  1379. if (dd == dev_size_error) {
  1380. retcode = ERR_NOMEM_BITMAP;
  1381. goto force_diskless_dec;
  1382. } else if (dd == grew)
  1383. set_bit(RESYNC_AFTER_NEG, &mdev->flags);
  1384. if (drbd_md_test_flag(mdev->ldev, MDF_FULL_SYNC) ||
  1385. (test_bit(CRASHED_PRIMARY, &mdev->flags) &&
  1386. drbd_md_test_flag(mdev->ldev, MDF_AL_DISABLED))) {
  1387. dev_info(DEV, "Assuming that all blocks are out of sync "
  1388. "(aka FullSync)\n");
  1389. if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write,
  1390. "set_n_write from attaching", BM_LOCKED_MASK)) {
  1391. retcode = ERR_IO_MD_DISK;
  1392. goto force_diskless_dec;
  1393. }
  1394. } else {
  1395. if (drbd_bitmap_io(mdev, &drbd_bm_read,
  1396. "read from attaching", BM_LOCKED_MASK)) {
  1397. retcode = ERR_IO_MD_DISK;
  1398. goto force_diskless_dec;
  1399. }
  1400. }
  1401. if (_drbd_bm_total_weight(mdev) == drbd_bm_bits(mdev))
  1402. drbd_suspend_al(mdev); /* IO is still suspended here... */
  1403. spin_lock_irq(&mdev->tconn->req_lock);
  1404. os = drbd_read_state(mdev);
  1405. ns = os;
  1406. /* If MDF_CONSISTENT is not set go into inconsistent state,
  1407. otherwise investigate MDF_WasUpToDate...
  1408. If MDF_WAS_UP_TO_DATE is not set go into D_OUTDATED disk state,
  1409. otherwise into D_CONSISTENT state.
  1410. */
  1411. if (drbd_md_test_flag(mdev->ldev, MDF_CONSISTENT)) {
  1412. if (drbd_md_test_flag(mdev->ldev, MDF_WAS_UP_TO_DATE))
  1413. ns.disk = D_CONSISTENT;
  1414. else
  1415. ns.disk = D_OUTDATED;
  1416. } else {
  1417. ns.disk = D_INCONSISTENT;
  1418. }
  1419. if (drbd_md_test_flag(mdev->ldev, MDF_PEER_OUT_DATED))
  1420. ns.pdsk = D_OUTDATED;
  1421. rcu_read_lock();
  1422. if (ns.disk == D_CONSISTENT &&
  1423. (ns.pdsk == D_OUTDATED || rcu_dereference(mdev->ldev->disk_conf)->fencing == FP_DONT_CARE))
  1424. ns.disk = D_UP_TO_DATE;
  1425. /* All tests on MDF_PRIMARY_IND, MDF_CONNECTED_IND,
  1426. MDF_CONSISTENT and MDF_WAS_UP_TO_DATE must happen before
  1427. this point, because drbd_request_state() modifies these
  1428. flags. */
  1429. if (rcu_dereference(mdev->ldev->disk_conf)->al_updates)
  1430. mdev->ldev->md.flags &= ~MDF_AL_DISABLED;
  1431. else
  1432. mdev->ldev->md.flags |= MDF_AL_DISABLED;
  1433. rcu_read_unlock();
  1434. /* In case we are C_CONNECTED postpone any decision on the new disk
  1435. state after the negotiation phase. */
  1436. if (mdev->state.conn == C_CONNECTED) {
  1437. mdev->new_state_tmp.i = ns.i;
  1438. ns.i = os.i;
  1439. ns.disk = D_NEGOTIATING;
  1440. /* We expect to receive up-to-date UUIDs soon.
  1441. To avoid a race in receive_state, free p_uuid while
  1442. holding req_lock. I.e. atomic with the state change */
  1443. kfree(mdev->p_uuid);
  1444. mdev->p_uuid = NULL;
  1445. }
  1446. rv = _drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
  1447. spin_unlock_irq(&mdev->tconn->req_lock);
  1448. if (rv < SS_SUCCESS)
  1449. goto force_diskless_dec;
  1450. mod_timer(&mdev->request_timer, jiffies + HZ);
  1451. if (mdev->state.role == R_PRIMARY)
  1452. mdev->ldev->md.uuid[UI_CURRENT] |= (u64)1;
  1453. else
  1454. mdev->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
  1455. drbd_md_mark_dirty(mdev);
  1456. drbd_md_sync(mdev);
  1457. kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
  1458. put_ldev(mdev);
  1459. conn_reconfig_done(mdev->tconn);
  1460. drbd_adm_finish(info, retcode);
  1461. return 0;
  1462. force_diskless_dec:
  1463. put_ldev(mdev);
  1464. force_diskless:
  1465. drbd_force_state(mdev, NS(disk, D_DISKLESS));
  1466. drbd_md_sync(mdev);
  1467. fail:
  1468. conn_reconfig_done(mdev->tconn);
  1469. if (nbc) {
  1470. if (nbc->backing_bdev)
  1471. blkdev_put(nbc->backing_bdev,
  1472. FMODE_READ | FMODE_WRITE | FMODE_EXCL);
  1473. if (nbc->md_bdev)
  1474. blkdev_put(nbc->md_bdev,
  1475. FMODE_READ | FMODE_WRITE | FMODE_EXCL);
  1476. kfree(nbc);
  1477. }
  1478. kfree(new_disk_conf);
  1479. lc_destroy(resync_lru);
  1480. kfree(new_plan);
  1481. finish:
  1482. drbd_adm_finish(info, retcode);
  1483. return 0;
  1484. }
  1485. static int adm_detach(struct drbd_conf *mdev, int force)
  1486. {
  1487. enum drbd_state_rv retcode;
  1488. int ret;
  1489. if (force) {
  1490. set_bit(FORCE_DETACH, &mdev->flags);
  1491. drbd_force_state(mdev, NS(disk, D_FAILED));
  1492. retcode = SS_SUCCESS;
  1493. goto out;
  1494. }
  1495. drbd_suspend_io(mdev); /* so no-one is stuck in drbd_al_begin_io */
  1496. drbd_md_get_buffer(mdev); /* make sure there is no in-flight meta-data IO */
  1497. retcode = drbd_request_state(mdev, NS(disk, D_FAILED));
  1498. drbd_md_put_buffer(mdev);
  1499. /* D_FAILED will transition to DISKLESS. */
  1500. ret = wait_event_interruptible(mdev->misc_wait,
  1501. mdev->state.disk != D_FAILED);
  1502. drbd_resume_io(mdev);
  1503. if ((int)retcode == (int)SS_IS_DISKLESS)
  1504. retcode = SS_NOTHING_TO_DO;
  1505. if (ret)
  1506. retcode = ERR_INTR;
  1507. out:
  1508. return retcode;
  1509. }
  1510. /* Detaching the disk is a process in multiple stages. First we need to lock
  1511. * out application IO, in-flight IO, IO stuck in drbd_al_begin_io.
  1512. * Then we transition to D_DISKLESS, and wait for put_ldev() to return all
  1513. * internal references as well.
  1514. * Only then we have finally detached. */
  1515. int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info)
  1516. {
  1517. enum drbd_ret_code retcode;
  1518. struct detach_parms parms = { };
  1519. int err;
  1520. retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
  1521. if (!adm_ctx.reply_skb)
  1522. return retcode;
  1523. if (retcode != NO_ERROR)
  1524. goto out;
  1525. if (info->attrs[DRBD_NLA_DETACH_PARMS]) {
  1526. err = detach_parms_from_attrs(&parms, info);
  1527. if (err) {
  1528. retcode = ERR_MANDATORY_TAG;
  1529. drbd_msg_put_info(from_attrs_err_to_txt(err));
  1530. goto out;
  1531. }
  1532. }
  1533. retcode = adm_detach(adm_ctx.mdev, parms.force_detach);
  1534. out:
  1535. drbd_adm_finish(info, retcode);
  1536. return 0;
  1537. }
  1538. static bool conn_resync_running(struct drbd_tconn *tconn)
  1539. {
  1540. struct drbd_conf *mdev;
  1541. bool rv = false;
  1542. int vnr;
  1543. rcu_read_lock();
  1544. idr_for_each_entry(&tconn->volumes, mdev, vnr) {
  1545. if (mdev->state.conn == C_SYNC_SOURCE ||
  1546. mdev->state.conn == C_SYNC_TARGET ||
  1547. mdev->state.conn == C_PAUSED_SYNC_S ||
  1548. mdev->state.conn == C_PAUSED_SYNC_T) {
  1549. rv = true;
  1550. break;
  1551. }
  1552. }
  1553. rcu_read_unlock();
  1554. return rv;
  1555. }
  1556. static bool conn_ov_running(struct drbd_tconn *tconn)
  1557. {
  1558. struct drbd_conf *mdev;
  1559. bool rv = false;
  1560. int vnr;
  1561. rcu_read_lock();
  1562. idr_for_each_entry(&tconn->volumes, mdev, vnr) {
  1563. if (mdev->state.conn == C_VERIFY_S ||
  1564. mdev->state.conn == C_VERIFY_T) {
  1565. rv = true;
  1566. break;
  1567. }
  1568. }
  1569. rcu_read_unlock();
  1570. return rv;
  1571. }
  1572. static enum drbd_ret_code
  1573. _check_net_options(struct drbd_tconn *tconn, struct net_conf *old_conf, struct net_conf *new_conf)
  1574. {
  1575. struct drbd_conf *mdev;
  1576. int i;
  1577. if (old_conf && tconn->cstate == C_WF_REPORT_PARAMS && tconn->agreed_pro_version < 100) {
  1578. if (new_conf->wire_protocol != old_conf->wire_protocol)
  1579. return ERR_NEED_APV_100;
  1580. if (new_conf->two_primaries != old_conf->two_primaries)
  1581. return ERR_NEED_APV_100;
  1582. if (strcmp(new_conf->integrity_alg, old_conf->integrity_alg))
  1583. return ERR_NEED_APV_100;
  1584. }
  1585. if (!new_conf->two_primaries &&
  1586. conn_highest_role(tconn) == R_PRIMARY &&
  1587. conn_highest_peer(tconn) == R_PRIMARY)
  1588. return ERR_NEED_ALLOW_TWO_PRI;
  1589. if (new_conf->two_primaries &&
  1590. (new_conf->wire_protocol != DRBD_PROT_C))
  1591. return ERR_NOT_PROTO_C;
  1592. idr_for_each_entry(&tconn->volumes, mdev, i) {
  1593. if (get_ldev(mdev)) {
  1594. enum drbd_fencing_p fp = rcu_dereference(mdev->ldev->disk_conf)->fencing;
  1595. put_ldev(mdev);
  1596. if (new_conf->wire_protocol == DRBD_PROT_A && fp == FP_STONITH)
  1597. return ERR_STONITH_AND_PROT_A;
  1598. }
  1599. if (mdev->state.role == R_PRIMARY && new_conf->discard_my_data)
  1600. return ERR_DISCARD_IMPOSSIBLE;
  1601. }
  1602. if (new_conf->on_congestion != OC_BLOCK && new_conf->wire_protocol != DRBD_PROT_A)
  1603. return ERR_CONG_NOT_PROTO_A;
  1604. return NO_ERROR;
  1605. }
  1606. static enum drbd_ret_code
  1607. check_net_options(struct drbd_tconn *tconn, struct net_conf *new_conf)
  1608. {
  1609. static enum drbd_ret_code rv;
  1610. struct drbd_conf *mdev;
  1611. int i;
  1612. rcu_read_lock();
  1613. rv = _check_net_options(tconn, rcu_dereference(tconn->net_conf), new_conf);
  1614. rcu_read_unlock();
  1615. /* tconn->volumes protected by genl_lock() here */
  1616. idr_for_each_entry(&tconn->volumes, mdev, i) {
  1617. if (!mdev->bitmap) {
  1618. if(drbd_bm_init(mdev))
  1619. return ERR_NOMEM;
  1620. }
  1621. }
  1622. return rv;
  1623. }
  1624. struct crypto {
  1625. struct crypto_hash *verify_tfm;
  1626. struct crypto_hash *csums_tfm;
  1627. struct crypto_hash *cram_hmac_tfm;
  1628. struct crypto_hash *integrity_tfm;
  1629. };
  1630. static int
  1631. alloc_hash(struct crypto_hash **tfm, char *tfm_name, int err_alg)
  1632. {
  1633. if (!tfm_name[0])
  1634. return NO_ERROR;
  1635. *tfm = crypto_alloc_hash(tfm_name, 0, CRYPTO_ALG_ASYNC);
  1636. if (IS_ERR(*tfm)) {
  1637. *tfm = NULL;
  1638. return err_alg;
  1639. }
  1640. return NO_ERROR;
  1641. }
  1642. static enum drbd_ret_code
  1643. alloc_crypto(struct crypto *crypto, struct net_conf *new_conf)
  1644. {
  1645. char hmac_name[CRYPTO_MAX_ALG_NAME];
  1646. enum drbd_ret_code rv;
  1647. rv = alloc_hash(&crypto->csums_tfm, new_conf->csums_alg,
  1648. ERR_CSUMS_ALG);
  1649. if (rv != NO_ERROR)
  1650. return rv;
  1651. rv = alloc_hash(&crypto->verify_tfm, new_conf->verify_alg,
  1652. ERR_VERIFY_ALG);
  1653. if (rv != NO_ERROR)
  1654. return rv;
  1655. rv = alloc_hash(&crypto->integrity_tfm, new_conf->integrity_alg,
  1656. ERR_INTEGRITY_ALG);
  1657. if (rv != NO_ERROR)
  1658. return rv;
  1659. if (new_conf->cram_hmac_alg[0] != 0) {
  1660. snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)",
  1661. new_conf->cram_hmac_alg);
  1662. rv = alloc_hash(&crypto->cram_hmac_tfm, hmac_name,
  1663. ERR_AUTH_ALG);
  1664. }
  1665. return rv;
  1666. }
  1667. static void free_crypto(struct crypto *crypto)
  1668. {
  1669. crypto_free_hash(crypto->cram_hmac_tfm);
  1670. crypto_free_hash(crypto->integrity_tfm);
  1671. crypto_free_hash(crypto->csums_tfm);
  1672. crypto_free_hash(crypto->verify_tfm);
  1673. }
  1674. int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info)
  1675. {
  1676. enum drbd_ret_code retcode;
  1677. struct drbd_tconn *tconn;
  1678. struct net_conf *old_conf, *new_conf = NULL;
  1679. int err;
  1680. int ovr; /* online verify running */
  1681. int rsr; /* re-sync running */
  1682. struct crypto crypto = { };
  1683. retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONNECTION);
  1684. if (!adm_ctx.reply_skb)
  1685. return retcode;
  1686. if (retcode != NO_ERROR)
  1687. goto out;
  1688. tconn = adm_ctx.tconn;
  1689. new_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
  1690. if (!new_conf) {
  1691. retcode = ERR_NOMEM;
  1692. goto out;
  1693. }
  1694. conn_reconfig_start(tconn);
  1695. mutex_lock(&tconn->data.mutex);
  1696. mutex_lock(&tconn->conf_update);
  1697. old_conf = tconn->net_conf;
  1698. if (!old_conf) {
  1699. drbd_msg_put_info("net conf missing, try connect");
  1700. retcode = ERR_INVALID_REQUEST;
  1701. goto fail;
  1702. }
  1703. *new_conf = *old_conf;
  1704. if (should_set_defaults(info))
  1705. set_net_conf_defaults(new_conf);
  1706. err = net_conf_from_attrs_for_change(new_conf, info);
  1707. if (err && err != -ENOMSG) {
  1708. retcode = ERR_MANDATORY_TAG;
  1709. drbd_msg_put_info(from_attrs_err_to_txt(err));
  1710. goto fail;
  1711. }
  1712. retcode = check_net_options(tconn, new_conf);
  1713. if (retcode != NO_ERROR)
  1714. goto fail;
  1715. /* re-sync running */
  1716. rsr = conn_resync_running(tconn);
  1717. if (rsr && strcmp(new_conf->csums_alg, old_conf->csums_alg)) {
  1718. retcode = ERR_CSUMS_RESYNC_RUNNING;
  1719. goto fail;
  1720. }
  1721. /* online verify running */
  1722. ovr = conn_ov_running(tconn);
  1723. if (ovr && strcmp(new_conf->verify_alg, old_conf->verify_alg)) {
  1724. retcode = ERR_VERIFY_RUNNING;
  1725. goto fail;
  1726. }
  1727. retcode = alloc_crypto(&crypto, new_conf);
  1728. if (retcode != NO_ERROR)
  1729. goto fail;
  1730. rcu_assign_pointer(tconn->net_conf, new_conf);
  1731. if (!rsr) {
  1732. crypto_free_hash(tconn->csums_tfm);
  1733. tconn->csums_tfm = crypto.csums_tfm;
  1734. crypto.csums_tfm = NULL;
  1735. }
  1736. if (!ovr) {
  1737. crypto_free_hash(tconn->verify_tfm);
  1738. tconn->verify_tfm = crypto.verify_tfm;
  1739. crypto.verify_tfm = NULL;
  1740. }
  1741. crypto_free_hash(tconn->integrity_tfm);
  1742. tconn->integrity_tfm = crypto.integrity_tfm;
  1743. if (tconn->cstate >= C_WF_REPORT_PARAMS && tconn->agreed_pro_version >= 100)
  1744. /* Do this without trying to take tconn->data.mutex again. */
  1745. __drbd_send_protocol(tconn, P_PROTOCOL_UPDATE);
  1746. crypto_free_hash(tconn->cram_hmac_tfm);
  1747. tconn->cram_hmac_tfm = crypto.cram_hmac_tfm;
  1748. mutex_unlock(&tconn->conf_update);
  1749. mutex_unlock(&tconn->data.mutex);
  1750. synchronize_rcu();
  1751. kfree(old_conf);
  1752. if (tconn->cstate >= C_WF_REPORT_PARAMS)
  1753. drbd_send_sync_param(minor_to_mdev(conn_lowest_minor(tconn)));
  1754. goto done;
  1755. fail:
  1756. mutex_unlock(&tconn->conf_update);
  1757. mutex_unlock(&tconn->data.mutex);
  1758. free_crypto(&crypto);
  1759. kfree(new_conf);
  1760. done:
  1761. conn_reconfig_done(tconn);
  1762. out:
  1763. drbd_adm_finish(info, retcode);
  1764. return 0;
  1765. }
  1766. int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info)
  1767. {
  1768. struct drbd_conf *mdev;
  1769. struct net_conf *old_conf, *new_conf = NULL;
  1770. struct crypto crypto = { };
  1771. struct drbd_tconn *tconn;
  1772. enum drbd_ret_code retcode;
  1773. int i;
  1774. int err;
  1775. retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
  1776. if (!adm_ctx.reply_skb)
  1777. return retcode;
  1778. if (retcode != NO_ERROR)
  1779. goto out;
  1780. if (!(adm_ctx.my_addr && adm_ctx.peer_addr)) {
  1781. drbd_msg_put_info("connection endpoint(s) missing");
  1782. retcode = ERR_INVALID_REQUEST;
  1783. goto out;
  1784. }
  1785. /* No need for _rcu here. All reconfiguration is
  1786. * strictly serialized on genl_lock(). We are protected against
  1787. * concurrent reconfiguration/addition/deletion */
  1788. list_for_each_entry(tconn, &drbd_tconns, all_tconn) {
  1789. if (nla_len(adm_ctx.my_addr) == tconn->my_addr_len &&
  1790. !memcmp(nla_data(adm_ctx.my_addr), &tconn->my_addr, tconn->my_addr_len)) {
  1791. retcode = ERR_LOCAL_ADDR;
  1792. goto out;
  1793. }
  1794. if (nla_len(adm_ctx.peer_addr) == tconn->peer_addr_len &&
  1795. !memcmp(nla_data(adm_ctx.peer_addr), &tconn->peer_addr, tconn->peer_addr_len)) {
  1796. retcode = ERR_PEER_ADDR;
  1797. goto out;
  1798. }
  1799. }
  1800. tconn = adm_ctx.tconn;
  1801. conn_reconfig_start(tconn);
  1802. if (tconn->cstate > C_STANDALONE) {
  1803. retcode = ERR_NET_CONFIGURED;
  1804. goto fail;
  1805. }
  1806. /* allocation not in the IO path, drbdsetup / netlink process context */
  1807. new_conf = kzalloc(sizeof(*new_conf), GFP_KERNEL);
  1808. if (!new_conf) {
  1809. retcode = ERR_NOMEM;
  1810. goto fail;
  1811. }
  1812. set_net_conf_defaults(new_conf);
  1813. err = net_conf_from_attrs(new_conf, info);
  1814. if (err && err != -ENOMSG) {
  1815. retcode = ERR_MANDATORY_TAG;
  1816. drbd_msg_put_info(from_attrs_err_to_txt(err));
  1817. goto fail;
  1818. }
  1819. retcode = check_net_options(tconn, new_conf);
  1820. if (retcode != NO_ERROR)
  1821. goto fail;
  1822. retcode = alloc_crypto(&crypto, new_conf);
  1823. if (retcode != NO_ERROR)
  1824. goto fail;
  1825. ((char *)new_conf->shared_secret)[SHARED_SECRET_MAX-1] = 0;
  1826. conn_flush_workqueue(tconn);
  1827. mutex_lock(&tconn->conf_update);
  1828. old_conf = tconn->net_conf;
  1829. if (old_conf) {
  1830. retcode = ERR_NET_CONFIGURED;
  1831. mutex_unlock(&tconn->conf_update);
  1832. goto fail;
  1833. }
  1834. rcu_assign_pointer(tconn->net_conf, new_conf);
  1835. conn_free_crypto(tconn);
  1836. tconn->cram_hmac_tfm = crypto.cram_hmac_tfm;
  1837. tconn->integrity_tfm = crypto.integrity_tfm;
  1838. tconn->csums_tfm = crypto.csums_tfm;
  1839. tconn->verify_tfm = crypto.verify_tfm;
  1840. tconn->my_addr_len = nla_len(adm_ctx.my_addr);
  1841. memcpy(&tconn->my_addr, nla_data(adm_ctx.my_addr), tconn->my_addr_len);
  1842. tconn->peer_addr_len = nla_len(adm_ctx.peer_addr);
  1843. memcpy(&tconn->peer_addr, nla_data(adm_ctx.peer_addr), tconn->peer_addr_len);
  1844. mutex_unlock(&tconn->conf_update);
  1845. rcu_read_lock();
  1846. idr_for_each_entry(&tconn->volumes, mdev, i) {
  1847. mdev->send_cnt = 0;
  1848. mdev->recv_cnt = 0;
  1849. }
  1850. rcu_read_unlock();
  1851. retcode = conn_request_state(tconn, NS(conn, C_UNCONNECTED), CS_VERBOSE);
  1852. conn_reconfig_done(tconn);
  1853. drbd_adm_finish(info, retcode);
  1854. return 0;
  1855. fail:
  1856. free_crypto(&crypto);
  1857. kfree(new_conf);
  1858. conn_reconfig_done(tconn);
  1859. out:
  1860. drbd_adm_finish(info, retcode);
  1861. return 0;
  1862. }
  1863. static enum drbd_state_rv conn_try_disconnect(struct drbd_tconn *tconn, bool force)
  1864. {
  1865. enum drbd_state_rv rv;
  1866. rv = conn_request_state(tconn, NS(conn, C_DISCONNECTING),
  1867. force ? CS_HARD : 0);
  1868. switch (rv) {
  1869. case SS_NOTHING_TO_DO:
  1870. break;
  1871. case SS_ALREADY_STANDALONE:
  1872. return SS_SUCCESS;
  1873. case SS_PRIMARY_NOP:
  1874. /* Our state checking code wants to see the peer outdated. */
  1875. rv = conn_request_state(tconn, NS2(conn, C_DISCONNECTING,
  1876. pdsk, D_OUTDATED), CS_VERBOSE);
  1877. break;
  1878. case SS_CW_FAILED_BY_PEER:
  1879. /* The peer probably wants to see us outdated. */
  1880. rv = conn_request_state(tconn, NS2(conn, C_DISCONNECTING,
  1881. disk, D_OUTDATED), 0);
  1882. if (rv == SS_IS_DISKLESS || rv == SS_LOWER_THAN_OUTDATED) {
  1883. rv = conn_request_state(tconn, NS(conn, C_DISCONNECTING),
  1884. CS_HARD);
  1885. }
  1886. break;
  1887. default:;
  1888. /* no special handling necessary */
  1889. }
  1890. if (rv >= SS_SUCCESS) {
  1891. enum drbd_state_rv rv2;
  1892. /* No one else can reconfigure the network while I am here.
  1893. * The state handling only uses drbd_thread_stop_nowait(),
  1894. * we want to really wait here until the receiver is no more.
  1895. */
  1896. drbd_thread_stop(&adm_ctx.tconn->receiver);
  1897. /* Race breaker. This additional state change request may be
  1898. * necessary, if this was a forced disconnect during a receiver
  1899. * restart. We may have "killed" the receiver thread just
  1900. * after drbdd_init() returned. Typically, we should be
  1901. * C_STANDALONE already, now, and this becomes a no-op.
  1902. */
  1903. rv2 = conn_request_state(tconn, NS(conn, C_STANDALONE),
  1904. CS_VERBOSE | CS_HARD);
  1905. if (rv2 < SS_SUCCESS)
  1906. conn_err(tconn,
  1907. "unexpected rv2=%d in conn_try_disconnect()\n",
  1908. rv2);
  1909. }
  1910. return rv;
  1911. }
  1912. int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info)
  1913. {
  1914. struct disconnect_parms parms;
  1915. struct drbd_tconn *tconn;
  1916. enum drbd_state_rv rv;
  1917. enum drbd_ret_code retcode;
  1918. int err;
  1919. retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONNECTION);
  1920. if (!adm_ctx.reply_skb)
  1921. return retcode;
  1922. if (retcode != NO_ERROR)
  1923. goto fail;
  1924. tconn = adm_ctx.tconn;
  1925. memset(&parms, 0, sizeof(parms));
  1926. if (info->attrs[DRBD_NLA_DISCONNECT_PARMS]) {
  1927. err = disconnect_parms_from_attrs(&parms, info);
  1928. if (err) {
  1929. retcode = ERR_MANDATORY_TAG;
  1930. drbd_msg_put_info(from_attrs_err_to_txt(err));
  1931. goto fail;
  1932. }
  1933. }
  1934. rv = conn_try_disconnect(tconn, parms.force_disconnect);
  1935. if (rv < SS_SUCCESS)
  1936. retcode = rv; /* FIXME: Type mismatch. */
  1937. else
  1938. retcode = NO_ERROR;
  1939. fail:
  1940. drbd_adm_finish(info, retcode);
  1941. return 0;
  1942. }
  1943. void resync_after_online_grow(struct drbd_conf *mdev)
  1944. {
  1945. int iass; /* I am sync source */
  1946. dev_info(DEV, "Resync of new storage after online grow\n");
  1947. if (mdev->state.role != mdev->state.peer)
  1948. iass = (mdev->state.role == R_PRIMARY);
  1949. else
  1950. iass = test_bit(RESOLVE_CONFLICTS, &mdev->tconn->flags);
  1951. if (iass)
  1952. drbd_start_resync(mdev, C_SYNC_SOURCE);
  1953. else
  1954. _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE + CS_SERIALIZE);
  1955. }
  1956. int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info)
  1957. {
  1958. struct disk_conf *old_disk_conf, *new_disk_conf = NULL;
  1959. struct resize_parms rs;
  1960. struct drbd_conf *mdev;
  1961. enum drbd_ret_code retcode;
  1962. enum determine_dev_size dd;
  1963. enum dds_flags ddsf;
  1964. sector_t u_size;
  1965. int err;
  1966. retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
  1967. if (!adm_ctx.reply_skb)
  1968. return retcode;
  1969. if (retcode != NO_ERROR)
  1970. goto fail;
  1971. memset(&rs, 0, sizeof(struct resize_parms));
  1972. if (info->attrs[DRBD_NLA_RESIZE_PARMS]) {
  1973. err = resize_parms_from_attrs(&rs, info);
  1974. if (err) {
  1975. retcode = ERR_MANDATORY_TAG;
  1976. drbd_msg_put_info(from_attrs_err_to_txt(err));
  1977. goto fail;
  1978. }
  1979. }
  1980. mdev = adm_ctx.mdev;
  1981. if (mdev->state.conn > C_CONNECTED) {
  1982. retcode = ERR_RESIZE_RESYNC;
  1983. goto fail;
  1984. }
  1985. if (mdev->state.role == R_SECONDARY &&
  1986. mdev->state.peer == R_SECONDARY) {
  1987. retcode = ERR_NO_PRIMARY;
  1988. goto fail;
  1989. }
  1990. if (!get_ldev(mdev)) {
  1991. retcode = ERR_NO_DISK;
  1992. goto fail;
  1993. }
  1994. if (rs.no_resync && mdev->tconn->agreed_pro_version < 93) {
  1995. retcode = ERR_NEED_APV_93;
  1996. goto fail_ldev;
  1997. }
  1998. rcu_read_lock();
  1999. u_size = rcu_dereference(mdev->ldev->disk_conf)->disk_size;
  2000. rcu_read_unlock();
  2001. if (u_size != (sector_t)rs.resize_size) {
  2002. new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
  2003. if (!new_disk_conf) {
  2004. retcode = ERR_NOMEM;
  2005. goto fail_ldev;
  2006. }
  2007. }
  2008. if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev))
  2009. mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev);
  2010. if (new_disk_conf) {
  2011. mutex_lock(&mdev->tconn->conf_update);
  2012. old_disk_conf = mdev->ldev->disk_conf;
  2013. *new_disk_conf = *old_disk_conf;
  2014. new_disk_conf->disk_size = (sector_t)rs.resize_size;
  2015. rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
  2016. mutex_unlock(&mdev->tconn->conf_update);
  2017. synchronize_rcu();
  2018. kfree(old_disk_conf);
  2019. }
  2020. ddsf = (rs.resize_force ? DDSF_FORCED : 0) | (rs.no_resync ? DDSF_NO_RESYNC : 0);
  2021. dd = drbd_determine_dev_size(mdev, ddsf);
  2022. drbd_md_sync(mdev);
  2023. put_ldev(mdev);
  2024. if (dd == dev_size_error) {
  2025. retcode = ERR_NOMEM_BITMAP;
  2026. goto fail;
  2027. }
  2028. if (mdev->state.conn == C_CONNECTED) {
  2029. if (dd == grew)
  2030. set_bit(RESIZE_PENDING, &mdev->flags);
  2031. drbd_send_uuids(mdev);
  2032. drbd_send_sizes(mdev, 1, ddsf);
  2033. }
  2034. fail:
  2035. drbd_adm_finish(info, retcode);
  2036. return 0;
  2037. fail_ldev:
  2038. put_ldev(mdev);
  2039. goto fail;
  2040. }
  2041. int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info)
  2042. {
  2043. enum drbd_ret_code retcode;
  2044. struct drbd_tconn *tconn;
  2045. struct res_opts res_opts;
  2046. int err;
  2047. retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
  2048. if (!adm_ctx.reply_skb)
  2049. return retcode;
  2050. if (retcode != NO_ERROR)
  2051. goto fail;
  2052. tconn = adm_ctx.tconn;
  2053. res_opts = tconn->res_opts;
  2054. if (should_set_defaults(info))
  2055. set_res_opts_defaults(&res_opts);
  2056. err = res_opts_from_attrs(&res_opts, info);
  2057. if (err && err != -ENOMSG) {
  2058. retcode = ERR_MANDATORY_TAG;
  2059. drbd_msg_put_info(from_attrs_err_to_txt(err));
  2060. goto fail;
  2061. }
  2062. err = set_resource_options(tconn, &res_opts);
  2063. if (err) {
  2064. retcode = ERR_INVALID_REQUEST;
  2065. if (err == -ENOMEM)
  2066. retcode = ERR_NOMEM;
  2067. }
  2068. fail:
  2069. drbd_adm_finish(info, retcode);
  2070. return 0;
  2071. }
  2072. int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info)
  2073. {
  2074. struct drbd_conf *mdev;
  2075. int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
  2076. retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
  2077. if (!adm_ctx.reply_skb)
  2078. return retcode;
  2079. if (retcode != NO_ERROR)
  2080. goto out;
  2081. mdev = adm_ctx.mdev;
  2082. /* If there is still bitmap IO pending, probably because of a previous
  2083. * resync just being finished, wait for it before requesting a new resync.
  2084. * Also wait for it's after_state_ch(). */
  2085. drbd_suspend_io(mdev);
  2086. wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
  2087. drbd_flush_workqueue(mdev);
  2088. retcode = _drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T), CS_ORDERED);
  2089. /* If that did not work, try again,
  2090. * but log failures this time (implicit CS_VERBOSE).
  2091. *
  2092. * If we happen to be C_STANDALONE R_SECONDARY,
  2093. * just change to D_INCONSISTENT, and set all bits in the bitmap.
  2094. * Otherwise, we just fail, to avoid races with the resync handshake.
  2095. */
  2096. if (retcode < SS_SUCCESS) {
  2097. if (mdev->state.conn == C_STANDALONE && mdev->state.role == R_SECONDARY) {
  2098. retcode = drbd_request_state(mdev, NS(disk, D_INCONSISTENT));
  2099. if (retcode >= SS_SUCCESS) {
  2100. if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write,
  2101. "set_n_write from invalidate", BM_LOCKED_MASK))
  2102. retcode = ERR_IO_MD_DISK;
  2103. }
  2104. } else
  2105. retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T));
  2106. }
  2107. drbd_resume_io(mdev);
  2108. out:
  2109. drbd_adm_finish(info, retcode);
  2110. return 0;
  2111. }
  2112. static int drbd_adm_simple_request_state(struct sk_buff *skb, struct genl_info *info,
  2113. union drbd_state mask, union drbd_state val)
  2114. {
  2115. enum drbd_ret_code retcode;
  2116. retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
  2117. if (!adm_ctx.reply_skb)
  2118. return retcode;
  2119. if (retcode != NO_ERROR)
  2120. goto out;
  2121. retcode = drbd_request_state(adm_ctx.mdev, mask, val);
  2122. out:
  2123. drbd_adm_finish(info, retcode);
  2124. return 0;
  2125. }
  2126. static int drbd_bmio_set_susp_al(struct drbd_conf *mdev)
  2127. {
  2128. int rv;
  2129. rv = drbd_bmio_set_n_write(mdev);
  2130. drbd_suspend_al(mdev);
  2131. return rv;
  2132. }
  2133. int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info)
  2134. {
  2135. int retcode; /* drbd_ret_code, drbd_state_rv */
  2136. struct drbd_conf *mdev;
  2137. retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
  2138. if (!adm_ctx.reply_skb)
  2139. return retcode;
  2140. if (retcode != NO_ERROR)
  2141. goto out;
  2142. mdev = adm_ctx.mdev;
  2143. /* If there is still bitmap IO pending, probably because of a previous
  2144. * resync just being finished, wait for it before requesting a new resync.
  2145. * Also wait for it's after_state_ch(). */
  2146. drbd_suspend_io(mdev);
  2147. wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
  2148. drbd_flush_workqueue(mdev);
  2149. retcode = _drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_S), CS_ORDERED);
  2150. if (retcode < SS_SUCCESS) {
  2151. if (mdev->state.conn == C_STANDALONE && mdev->state.role == R_PRIMARY) {
  2152. /* The peer will get a resync upon connect anyways. Just make that
  2153. into a full resync. */
  2154. retcode = drbd_request_state(mdev, NS(pdsk, D_INCONSISTENT));
  2155. if (retcode >= SS_SUCCESS) {
  2156. if (drbd_bitmap_io(mdev, &drbd_bmio_set_susp_al,
  2157. "set_n_write from invalidate_peer",
  2158. BM_LOCKED_SET_ALLOWED))
  2159. retcode = ERR_IO_MD_DISK;
  2160. }
  2161. } else
  2162. retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_S));
  2163. }
  2164. drbd_resume_io(mdev);
  2165. out:
  2166. drbd_adm_finish(info, retcode);
  2167. return 0;
  2168. }
  2169. int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info)
  2170. {
  2171. enum drbd_ret_code retcode;
  2172. retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
  2173. if (!adm_ctx.reply_skb)
  2174. return retcode;
  2175. if (retcode != NO_ERROR)
  2176. goto out;
  2177. if (drbd_request_state(adm_ctx.mdev, NS(user_isp, 1)) == SS_NOTHING_TO_DO)
  2178. retcode = ERR_PAUSE_IS_SET;
  2179. out:
  2180. drbd_adm_finish(info, retcode);
  2181. return 0;
  2182. }
  2183. int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info)
  2184. {
  2185. union drbd_dev_state s;
  2186. enum drbd_ret_code retcode;
  2187. retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
  2188. if (!adm_ctx.reply_skb)
  2189. return retcode;
  2190. if (retcode != NO_ERROR)
  2191. goto out;
  2192. if (drbd_request_state(adm_ctx.mdev, NS(user_isp, 0)) == SS_NOTHING_TO_DO) {
  2193. s = adm_ctx.mdev->state;
  2194. if (s.conn == C_PAUSED_SYNC_S || s.conn == C_PAUSED_SYNC_T) {
  2195. retcode = s.aftr_isp ? ERR_PIC_AFTER_DEP :
  2196. s.peer_isp ? ERR_PIC_PEER_DEP : ERR_PAUSE_IS_CLEAR;
  2197. } else {
  2198. retcode = ERR_PAUSE_IS_CLEAR;
  2199. }
  2200. }
  2201. out:
  2202. drbd_adm_finish(info, retcode);
  2203. return 0;
  2204. }
  2205. int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info)
  2206. {
  2207. return drbd_adm_simple_request_state(skb, info, NS(susp, 1));
  2208. }
  2209. int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info)
  2210. {
  2211. struct drbd_conf *mdev;
  2212. int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
  2213. retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
  2214. if (!adm_ctx.reply_skb)
  2215. return retcode;
  2216. if (retcode != NO_ERROR)
  2217. goto out;
  2218. mdev = adm_ctx.mdev;
  2219. if (test_bit(NEW_CUR_UUID, &mdev->flags)) {
  2220. drbd_uuid_new_current(mdev);
  2221. clear_bit(NEW_CUR_UUID, &mdev->flags);
  2222. }
  2223. drbd_suspend_io(mdev);
  2224. retcode = drbd_request_state(mdev, NS3(susp, 0, susp_nod, 0, susp_fen, 0));
  2225. if (retcode == SS_SUCCESS) {
  2226. if (mdev->state.conn < C_CONNECTED)
  2227. tl_clear(mdev->tconn);
  2228. if (mdev->state.disk == D_DISKLESS || mdev->state.disk == D_FAILED)
  2229. tl_restart(mdev->tconn, FAIL_FROZEN_DISK_IO);
  2230. }
  2231. drbd_resume_io(mdev);
  2232. out:
  2233. drbd_adm_finish(info, retcode);
  2234. return 0;
  2235. }
  2236. int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info)
  2237. {
  2238. return drbd_adm_simple_request_state(skb, info, NS(disk, D_OUTDATED));
  2239. }
  2240. int nla_put_drbd_cfg_context(struct sk_buff *skb, struct drbd_tconn *tconn, unsigned vnr)
  2241. {
  2242. struct nlattr *nla;
  2243. nla = nla_nest_start(skb, DRBD_NLA_CFG_CONTEXT);
  2244. if (!nla)
  2245. goto nla_put_failure;
  2246. if (vnr != VOLUME_UNSPECIFIED &&
  2247. nla_put_u32(skb, T_ctx_volume, vnr))
  2248. goto nla_put_failure;
  2249. if (nla_put_string(skb, T_ctx_resource_name, tconn->name))
  2250. goto nla_put_failure;
  2251. if (tconn->my_addr_len &&
  2252. nla_put(skb, T_ctx_my_addr, tconn->my_addr_len, &tconn->my_addr))
  2253. goto nla_put_failure;
  2254. if (tconn->peer_addr_len &&
  2255. nla_put(skb, T_ctx_peer_addr, tconn->peer_addr_len, &tconn->peer_addr))
  2256. goto nla_put_failure;
  2257. nla_nest_end(skb, nla);
  2258. return 0;
  2259. nla_put_failure:
  2260. if (nla)
  2261. nla_nest_cancel(skb, nla);
  2262. return -EMSGSIZE;
  2263. }
  2264. int nla_put_status_info(struct sk_buff *skb, struct drbd_conf *mdev,
  2265. const struct sib_info *sib)
  2266. {
  2267. struct state_info *si = NULL; /* for sizeof(si->member); */
  2268. struct net_conf *nc;
  2269. struct nlattr *nla;
  2270. int got_ldev;
  2271. int err = 0;
  2272. int exclude_sensitive;
  2273. /* If sib != NULL, this is drbd_bcast_event, which anyone can listen
  2274. * to. So we better exclude_sensitive information.
  2275. *
  2276. * If sib == NULL, this is drbd_adm_get_status, executed synchronously
  2277. * in the context of the requesting user process. Exclude sensitive
  2278. * information, unless current has superuser.
  2279. *
  2280. * NOTE: for drbd_adm_get_status_all(), this is a netlink dump, and
  2281. * relies on the current implementation of netlink_dump(), which
  2282. * executes the dump callback successively from netlink_recvmsg(),
  2283. * always in the context of the receiving process */
  2284. exclude_sensitive = sib || !capable(CAP_SYS_ADMIN);
  2285. got_ldev = get_ldev(mdev);
  2286. /* We need to add connection name and volume number information still.
  2287. * Minor number is in drbd_genlmsghdr. */
  2288. if (nla_put_drbd_cfg_context(skb, mdev->tconn, mdev->vnr))
  2289. goto nla_put_failure;
  2290. if (res_opts_to_skb(skb, &mdev->tconn->res_opts, exclude_sensitive))
  2291. goto nla_put_failure;
  2292. rcu_read_lock();
  2293. if (got_ldev)
  2294. if (disk_conf_to_skb(skb, rcu_dereference(mdev->ldev->disk_conf), exclude_sensitive))
  2295. goto nla_put_failure;
  2296. nc = rcu_dereference(mdev->tconn->net_conf);
  2297. if (nc)
  2298. err = net_conf_to_skb(skb, nc, exclude_sensitive);
  2299. rcu_read_unlock();
  2300. if (err)
  2301. goto nla_put_failure;
  2302. nla = nla_nest_start(skb, DRBD_NLA_STATE_INFO);
  2303. if (!nla)
  2304. goto nla_put_failure;
  2305. if (nla_put_u32(skb, T_sib_reason, sib ? sib->sib_reason : SIB_GET_STATUS_REPLY) ||
  2306. nla_put_u32(skb, T_current_state, mdev->state.i) ||
  2307. nla_put_u64(skb, T_ed_uuid, mdev->ed_uuid) ||
  2308. nla_put_u64(skb, T_capacity, drbd_get_capacity(mdev->this_bdev)) ||
  2309. nla_put_u64(skb, T_send_cnt, mdev->send_cnt) ||
  2310. nla_put_u64(skb, T_recv_cnt, mdev->recv_cnt) ||
  2311. nla_put_u64(skb, T_read_cnt, mdev->read_cnt) ||
  2312. nla_put_u64(skb, T_writ_cnt, mdev->writ_cnt) ||
  2313. nla_put_u64(skb, T_al_writ_cnt, mdev->al_writ_cnt) ||
  2314. nla_put_u64(skb, T_bm_writ_cnt, mdev->bm_writ_cnt) ||
  2315. nla_put_u32(skb, T_ap_bio_cnt, atomic_read(&mdev->ap_bio_cnt)) ||
  2316. nla_put_u32(skb, T_ap_pending_cnt, atomic_read(&mdev->ap_pending_cnt)) ||
  2317. nla_put_u32(skb, T_rs_pending_cnt, atomic_read(&mdev->rs_pending_cnt)))
  2318. goto nla_put_failure;
  2319. if (got_ldev) {
  2320. int err;
  2321. spin_lock_irq(&mdev->ldev->md.uuid_lock);
  2322. err = nla_put(skb, T_uuids, sizeof(si->uuids), mdev->ldev->md.uuid);
  2323. spin_unlock_irq(&mdev->ldev->md.uuid_lock);
  2324. if (err)
  2325. goto nla_put_failure;
  2326. if (nla_put_u32(skb, T_disk_flags, mdev->ldev->md.flags) ||
  2327. nla_put_u64(skb, T_bits_total, drbd_bm_bits(mdev)) ||
  2328. nla_put_u64(skb, T_bits_oos, drbd_bm_total_weight(mdev)))
  2329. goto nla_put_failure;
  2330. if (C_SYNC_SOURCE <= mdev->state.conn &&
  2331. C_PAUSED_SYNC_T >= mdev->state.conn) {
  2332. if (nla_put_u64(skb, T_bits_rs_total, mdev->rs_total) ||
  2333. nla_put_u64(skb, T_bits_rs_failed, mdev->rs_failed))
  2334. goto nla_put_failure;
  2335. }
  2336. }
  2337. if (sib) {
  2338. switch(sib->sib_reason) {
  2339. case SIB_SYNC_PROGRESS:
  2340. case SIB_GET_STATUS_REPLY:
  2341. break;
  2342. case SIB_STATE_CHANGE:
  2343. if (nla_put_u32(skb, T_prev_state, sib->os.i) ||
  2344. nla_put_u32(skb, T_new_state, sib->ns.i))
  2345. goto nla_put_failure;
  2346. break;
  2347. case SIB_HELPER_POST:
  2348. if (nla_put_u32(skb, T_helper_exit_code,
  2349. sib->helper_exit_code))
  2350. goto nla_put_failure;
  2351. /* fall through */
  2352. case SIB_HELPER_PRE:
  2353. if (nla_put_string(skb, T_helper, sib->helper_name))
  2354. goto nla_put_failure;
  2355. break;
  2356. }
  2357. }
  2358. nla_nest_end(skb, nla);
  2359. if (0)
  2360. nla_put_failure:
  2361. err = -EMSGSIZE;
  2362. if (got_ldev)
  2363. put_ldev(mdev);
  2364. return err;
  2365. }
  2366. int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info)
  2367. {
  2368. enum drbd_ret_code retcode;
  2369. int err;
  2370. retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
  2371. if (!adm_ctx.reply_skb)
  2372. return retcode;
  2373. if (retcode != NO_ERROR)
  2374. goto out;
  2375. err = nla_put_status_info(adm_ctx.reply_skb, adm_ctx.mdev, NULL);
  2376. if (err) {
  2377. nlmsg_free(adm_ctx.reply_skb);
  2378. return err;
  2379. }
  2380. out:
  2381. drbd_adm_finish(info, retcode);
  2382. return 0;
  2383. }
  2384. int get_one_status(struct sk_buff *skb, struct netlink_callback *cb)
  2385. {
  2386. struct drbd_conf *mdev;
  2387. struct drbd_genlmsghdr *dh;
  2388. struct drbd_tconn *pos = (struct drbd_tconn*)cb->args[0];
  2389. struct drbd_tconn *tconn = NULL;
  2390. struct drbd_tconn *tmp;
  2391. unsigned volume = cb->args[1];
  2392. /* Open coded, deferred, iteration:
  2393. * list_for_each_entry_safe(tconn, tmp, &drbd_tconns, all_tconn) {
  2394. * idr_for_each_entry(&tconn->volumes, mdev, i) {
  2395. * ...
  2396. * }
  2397. * }
  2398. * where tconn is cb->args[0];
  2399. * and i is cb->args[1];
  2400. *
  2401. * cb->args[2] indicates if we shall loop over all resources,
  2402. * or just dump all volumes of a single resource.
  2403. *
  2404. * This may miss entries inserted after this dump started,
  2405. * or entries deleted before they are reached.
  2406. *
  2407. * We need to make sure the mdev won't disappear while
  2408. * we are looking at it, and revalidate our iterators
  2409. * on each iteration.
  2410. */
  2411. /* synchronize with conn_create()/conn_destroy() */
  2412. rcu_read_lock();
  2413. /* revalidate iterator position */
  2414. list_for_each_entry_rcu(tmp, &drbd_tconns, all_tconn) {
  2415. if (pos == NULL) {
  2416. /* first iteration */
  2417. pos = tmp;
  2418. tconn = pos;
  2419. break;
  2420. }
  2421. if (tmp == pos) {
  2422. tconn = pos;
  2423. break;
  2424. }
  2425. }
  2426. if (tconn) {
  2427. next_tconn:
  2428. mdev = idr_get_next(&tconn->volumes, &volume);
  2429. if (!mdev) {
  2430. /* No more volumes to dump on this tconn.
  2431. * Advance tconn iterator. */
  2432. pos = list_entry_rcu(tconn->all_tconn.next,
  2433. struct drbd_tconn, all_tconn);
  2434. /* Did we dump any volume on this tconn yet? */
  2435. if (volume != 0) {
  2436. /* If we reached the end of the list,
  2437. * or only a single resource dump was requested,
  2438. * we are done. */
  2439. if (&pos->all_tconn == &drbd_tconns || cb->args[2])
  2440. goto out;
  2441. volume = 0;
  2442. tconn = pos;
  2443. goto next_tconn;
  2444. }
  2445. }
  2446. dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
  2447. cb->nlh->nlmsg_seq, &drbd_genl_family,
  2448. NLM_F_MULTI, DRBD_ADM_GET_STATUS);
  2449. if (!dh)
  2450. goto out;
  2451. if (!mdev) {
  2452. /* This is a tconn without a single volume.
  2453. * Suprisingly enough, it may have a network
  2454. * configuration. */
  2455. struct net_conf *nc;
  2456. dh->minor = -1U;
  2457. dh->ret_code = NO_ERROR;
  2458. if (nla_put_drbd_cfg_context(skb, tconn, VOLUME_UNSPECIFIED))
  2459. goto cancel;
  2460. nc = rcu_dereference(tconn->net_conf);
  2461. if (nc && net_conf_to_skb(skb, nc, 1) != 0)
  2462. goto cancel;
  2463. goto done;
  2464. }
  2465. D_ASSERT(mdev->vnr == volume);
  2466. D_ASSERT(mdev->tconn == tconn);
  2467. dh->minor = mdev_to_minor(mdev);
  2468. dh->ret_code = NO_ERROR;
  2469. if (nla_put_status_info(skb, mdev, NULL)) {
  2470. cancel:
  2471. genlmsg_cancel(skb, dh);
  2472. goto out;
  2473. }
  2474. done:
  2475. genlmsg_end(skb, dh);
  2476. }
  2477. out:
  2478. rcu_read_unlock();
  2479. /* where to start the next iteration */
  2480. cb->args[0] = (long)pos;
  2481. cb->args[1] = (pos == tconn) ? volume + 1 : 0;
  2482. /* No more tconns/volumes/minors found results in an empty skb.
  2483. * Which will terminate the dump. */
  2484. return skb->len;
  2485. }
  2486. /*
  2487. * Request status of all resources, or of all volumes within a single resource.
  2488. *
  2489. * This is a dump, as the answer may not fit in a single reply skb otherwise.
  2490. * Which means we cannot use the family->attrbuf or other such members, because
  2491. * dump is NOT protected by the genl_lock(). During dump, we only have access
  2492. * to the incoming skb, and need to opencode "parsing" of the nlattr payload.
  2493. *
  2494. * Once things are setup properly, we call into get_one_status().
  2495. */
  2496. int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb)
  2497. {
  2498. const unsigned hdrlen = GENL_HDRLEN + GENL_MAGIC_FAMILY_HDRSZ;
  2499. struct nlattr *nla;
  2500. const char *resource_name;
  2501. struct drbd_tconn *tconn;
  2502. int maxtype;
  2503. /* Is this a followup call? */
  2504. if (cb->args[0]) {
  2505. /* ... of a single resource dump,
  2506. * and the resource iterator has been advanced already? */
  2507. if (cb->args[2] && cb->args[2] != cb->args[0])
  2508. return 0; /* DONE. */
  2509. goto dump;
  2510. }
  2511. /* First call (from netlink_dump_start). We need to figure out
  2512. * which resource(s) the user wants us to dump. */
  2513. nla = nla_find(nlmsg_attrdata(cb->nlh, hdrlen),
  2514. nlmsg_attrlen(cb->nlh, hdrlen),
  2515. DRBD_NLA_CFG_CONTEXT);
  2516. /* No explicit context given. Dump all. */
  2517. if (!nla)
  2518. goto dump;
  2519. maxtype = ARRAY_SIZE(drbd_cfg_context_nl_policy) - 1;
  2520. nla = drbd_nla_find_nested(maxtype, nla, __nla_type(T_ctx_resource_name));
  2521. if (IS_ERR(nla))
  2522. return PTR_ERR(nla);
  2523. /* context given, but no name present? */
  2524. if (!nla)
  2525. return -EINVAL;
  2526. resource_name = nla_data(nla);
  2527. tconn = conn_get_by_name(resource_name);
  2528. if (!tconn)
  2529. return -ENODEV;
  2530. kref_put(&tconn->kref, &conn_destroy); /* get_one_status() (re)validates tconn by itself */
  2531. /* prime iterators, and set "filter" mode mark:
  2532. * only dump this tconn. */
  2533. cb->args[0] = (long)tconn;
  2534. /* cb->args[1] = 0; passed in this way. */
  2535. cb->args[2] = (long)tconn;
  2536. dump:
  2537. return get_one_status(skb, cb);
  2538. }
  2539. int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info)
  2540. {
  2541. enum drbd_ret_code retcode;
  2542. struct timeout_parms tp;
  2543. int err;
  2544. retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
  2545. if (!adm_ctx.reply_skb)
  2546. return retcode;
  2547. if (retcode != NO_ERROR)
  2548. goto out;
  2549. tp.timeout_type =
  2550. adm_ctx.mdev->state.pdsk == D_OUTDATED ? UT_PEER_OUTDATED :
  2551. test_bit(USE_DEGR_WFC_T, &adm_ctx.mdev->flags) ? UT_DEGRADED :
  2552. UT_DEFAULT;
  2553. err = timeout_parms_to_priv_skb(adm_ctx.reply_skb, &tp);
  2554. if (err) {
  2555. nlmsg_free(adm_ctx.reply_skb);
  2556. return err;
  2557. }
  2558. out:
  2559. drbd_adm_finish(info, retcode);
  2560. return 0;
  2561. }
  2562. int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info)
  2563. {
  2564. struct drbd_conf *mdev;
  2565. enum drbd_ret_code retcode;
  2566. struct start_ov_parms parms;
  2567. retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
  2568. if (!adm_ctx.reply_skb)
  2569. return retcode;
  2570. if (retcode != NO_ERROR)
  2571. goto out;
  2572. mdev = adm_ctx.mdev;
  2573. /* resume from last known position, if possible */
  2574. parms.ov_start_sector = mdev->ov_start_sector;
  2575. parms.ov_stop_sector = ULLONG_MAX;
  2576. if (info->attrs[DRBD_NLA_START_OV_PARMS]) {
  2577. int err = start_ov_parms_from_attrs(&parms, info);
  2578. if (err) {
  2579. retcode = ERR_MANDATORY_TAG;
  2580. drbd_msg_put_info(from_attrs_err_to_txt(err));
  2581. goto out;
  2582. }
  2583. }
  2584. /* w_make_ov_request expects position to be aligned */
  2585. mdev->ov_start_sector = parms.ov_start_sector & ~(BM_SECT_PER_BIT-1);
  2586. mdev->ov_stop_sector = parms.ov_stop_sector;
  2587. /* If there is still bitmap IO pending, e.g. previous resync or verify
  2588. * just being finished, wait for it before requesting a new resync. */
  2589. drbd_suspend_io(mdev);
  2590. wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
  2591. retcode = drbd_request_state(mdev,NS(conn,C_VERIFY_S));
  2592. drbd_resume_io(mdev);
  2593. out:
  2594. drbd_adm_finish(info, retcode);
  2595. return 0;
  2596. }
  2597. int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info)
  2598. {
  2599. struct drbd_conf *mdev;
  2600. enum drbd_ret_code retcode;
  2601. int skip_initial_sync = 0;
  2602. int err;
  2603. struct new_c_uuid_parms args;
  2604. retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
  2605. if (!adm_ctx.reply_skb)
  2606. return retcode;
  2607. if (retcode != NO_ERROR)
  2608. goto out_nolock;
  2609. mdev = adm_ctx.mdev;
  2610. memset(&args, 0, sizeof(args));
  2611. if (info->attrs[DRBD_NLA_NEW_C_UUID_PARMS]) {
  2612. err = new_c_uuid_parms_from_attrs(&args, info);
  2613. if (err) {
  2614. retcode = ERR_MANDATORY_TAG;
  2615. drbd_msg_put_info(from_attrs_err_to_txt(err));
  2616. goto out_nolock;
  2617. }
  2618. }
  2619. mutex_lock(mdev->state_mutex); /* Protects us against serialized state changes. */
  2620. if (!get_ldev(mdev)) {
  2621. retcode = ERR_NO_DISK;
  2622. goto out;
  2623. }
  2624. /* this is "skip initial sync", assume to be clean */
  2625. if (mdev->state.conn == C_CONNECTED && mdev->tconn->agreed_pro_version >= 90 &&
  2626. mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED && args.clear_bm) {
  2627. dev_info(DEV, "Preparing to skip initial sync\n");
  2628. skip_initial_sync = 1;
  2629. } else if (mdev->state.conn != C_STANDALONE) {
  2630. retcode = ERR_CONNECTED;
  2631. goto out_dec;
  2632. }
  2633. drbd_uuid_set(mdev, UI_BITMAP, 0); /* Rotate UI_BITMAP to History 1, etc... */
  2634. drbd_uuid_new_current(mdev); /* New current, previous to UI_BITMAP */
  2635. if (args.clear_bm) {
  2636. err = drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write,
  2637. "clear_n_write from new_c_uuid", BM_LOCKED_MASK);
  2638. if (err) {
  2639. dev_err(DEV, "Writing bitmap failed with %d\n",err);
  2640. retcode = ERR_IO_MD_DISK;
  2641. }
  2642. if (skip_initial_sync) {
  2643. drbd_send_uuids_skip_initial_sync(mdev);
  2644. _drbd_uuid_set(mdev, UI_BITMAP, 0);
  2645. drbd_print_uuids(mdev, "cleared bitmap UUID");
  2646. spin_lock_irq(&mdev->tconn->req_lock);
  2647. _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
  2648. CS_VERBOSE, NULL);
  2649. spin_unlock_irq(&mdev->tconn->req_lock);
  2650. }
  2651. }
  2652. drbd_md_sync(mdev);
  2653. out_dec:
  2654. put_ldev(mdev);
  2655. out:
  2656. mutex_unlock(mdev->state_mutex);
  2657. out_nolock:
  2658. drbd_adm_finish(info, retcode);
  2659. return 0;
  2660. }
  2661. static enum drbd_ret_code
  2662. drbd_check_resource_name(const char *name)
  2663. {
  2664. if (!name || !name[0]) {
  2665. drbd_msg_put_info("resource name missing");
  2666. return ERR_MANDATORY_TAG;
  2667. }
  2668. /* if we want to use these in sysfs/configfs/debugfs some day,
  2669. * we must not allow slashes */
  2670. if (strchr(name, '/')) {
  2671. drbd_msg_put_info("invalid resource name");
  2672. return ERR_INVALID_REQUEST;
  2673. }
  2674. return NO_ERROR;
  2675. }
  2676. int drbd_adm_new_resource(struct sk_buff *skb, struct genl_info *info)
  2677. {
  2678. enum drbd_ret_code retcode;
  2679. struct res_opts res_opts;
  2680. int err;
  2681. retcode = drbd_adm_prepare(skb, info, 0);
  2682. if (!adm_ctx.reply_skb)
  2683. return retcode;
  2684. if (retcode != NO_ERROR)
  2685. goto out;
  2686. set_res_opts_defaults(&res_opts);
  2687. err = res_opts_from_attrs(&res_opts, info);
  2688. if (err && err != -ENOMSG) {
  2689. retcode = ERR_MANDATORY_TAG;
  2690. drbd_msg_put_info(from_attrs_err_to_txt(err));
  2691. goto out;
  2692. }
  2693. retcode = drbd_check_resource_name(adm_ctx.resource_name);
  2694. if (retcode != NO_ERROR)
  2695. goto out;
  2696. if (adm_ctx.tconn) {
  2697. if (info->nlhdr->nlmsg_flags & NLM_F_EXCL) {
  2698. retcode = ERR_INVALID_REQUEST;
  2699. drbd_msg_put_info("resource exists");
  2700. }
  2701. /* else: still NO_ERROR */
  2702. goto out;
  2703. }
  2704. if (!conn_create(adm_ctx.resource_name, &res_opts))
  2705. retcode = ERR_NOMEM;
  2706. out:
  2707. drbd_adm_finish(info, retcode);
  2708. return 0;
  2709. }
  2710. int drbd_adm_add_minor(struct sk_buff *skb, struct genl_info *info)
  2711. {
  2712. struct drbd_genlmsghdr *dh = info->userhdr;
  2713. enum drbd_ret_code retcode;
  2714. retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
  2715. if (!adm_ctx.reply_skb)
  2716. return retcode;
  2717. if (retcode != NO_ERROR)
  2718. goto out;
  2719. if (dh->minor > MINORMASK) {
  2720. drbd_msg_put_info("requested minor out of range");
  2721. retcode = ERR_INVALID_REQUEST;
  2722. goto out;
  2723. }
  2724. if (adm_ctx.volume > DRBD_VOLUME_MAX) {
  2725. drbd_msg_put_info("requested volume id out of range");
  2726. retcode = ERR_INVALID_REQUEST;
  2727. goto out;
  2728. }
  2729. /* drbd_adm_prepare made sure already
  2730. * that mdev->tconn and mdev->vnr match the request. */
  2731. if (adm_ctx.mdev) {
  2732. if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
  2733. retcode = ERR_MINOR_EXISTS;
  2734. /* else: still NO_ERROR */
  2735. goto out;
  2736. }
  2737. retcode = conn_new_minor(adm_ctx.tconn, dh->minor, adm_ctx.volume);
  2738. out:
  2739. drbd_adm_finish(info, retcode);
  2740. return 0;
  2741. }
  2742. static enum drbd_ret_code adm_delete_minor(struct drbd_conf *mdev)
  2743. {
  2744. if (mdev->state.disk == D_DISKLESS &&
  2745. /* no need to be mdev->state.conn == C_STANDALONE &&
  2746. * we may want to delete a minor from a live replication group.
  2747. */
  2748. mdev->state.role == R_SECONDARY) {
  2749. _drbd_request_state(mdev, NS(conn, C_WF_REPORT_PARAMS),
  2750. CS_VERBOSE + CS_WAIT_COMPLETE);
  2751. idr_remove(&mdev->tconn->volumes, mdev->vnr);
  2752. idr_remove(&minors, mdev_to_minor(mdev));
  2753. destroy_workqueue(mdev->submit.wq);
  2754. del_gendisk(mdev->vdisk);
  2755. synchronize_rcu();
  2756. kref_put(&mdev->kref, &drbd_minor_destroy);
  2757. return NO_ERROR;
  2758. } else
  2759. return ERR_MINOR_CONFIGURED;
  2760. }
  2761. int drbd_adm_delete_minor(struct sk_buff *skb, struct genl_info *info)
  2762. {
  2763. enum drbd_ret_code retcode;
  2764. retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
  2765. if (!adm_ctx.reply_skb)
  2766. return retcode;
  2767. if (retcode != NO_ERROR)
  2768. goto out;
  2769. retcode = adm_delete_minor(adm_ctx.mdev);
  2770. out:
  2771. drbd_adm_finish(info, retcode);
  2772. return 0;
  2773. }
  2774. int drbd_adm_down(struct sk_buff *skb, struct genl_info *info)
  2775. {
  2776. int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
  2777. struct drbd_conf *mdev;
  2778. unsigned i;
  2779. retcode = drbd_adm_prepare(skb, info, 0);
  2780. if (!adm_ctx.reply_skb)
  2781. return retcode;
  2782. if (retcode != NO_ERROR)
  2783. goto out;
  2784. if (!adm_ctx.tconn) {
  2785. retcode = ERR_RES_NOT_KNOWN;
  2786. goto out;
  2787. }
  2788. /* demote */
  2789. idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
  2790. retcode = drbd_set_role(mdev, R_SECONDARY, 0);
  2791. if (retcode < SS_SUCCESS) {
  2792. drbd_msg_put_info("failed to demote");
  2793. goto out;
  2794. }
  2795. }
  2796. retcode = conn_try_disconnect(adm_ctx.tconn, 0);
  2797. if (retcode < SS_SUCCESS) {
  2798. drbd_msg_put_info("failed to disconnect");
  2799. goto out;
  2800. }
  2801. /* detach */
  2802. idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
  2803. retcode = adm_detach(mdev, 0);
  2804. if (retcode < SS_SUCCESS || retcode > NO_ERROR) {
  2805. drbd_msg_put_info("failed to detach");
  2806. goto out;
  2807. }
  2808. }
  2809. /* If we reach this, all volumes (of this tconn) are Secondary,
  2810. * Disconnected, Diskless, aka Unconfigured. Make sure all threads have
  2811. * actually stopped, state handling only does drbd_thread_stop_nowait(). */
  2812. drbd_thread_stop(&adm_ctx.tconn->worker);
  2813. /* Now, nothing can fail anymore */
  2814. /* delete volumes */
  2815. idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
  2816. retcode = adm_delete_minor(mdev);
  2817. if (retcode != NO_ERROR) {
  2818. /* "can not happen" */
  2819. drbd_msg_put_info("failed to delete volume");
  2820. goto out;
  2821. }
  2822. }
  2823. /* delete connection */
  2824. if (conn_lowest_minor(adm_ctx.tconn) < 0) {
  2825. list_del_rcu(&adm_ctx.tconn->all_tconn);
  2826. synchronize_rcu();
  2827. kref_put(&adm_ctx.tconn->kref, &conn_destroy);
  2828. retcode = NO_ERROR;
  2829. } else {
  2830. /* "can not happen" */
  2831. retcode = ERR_RES_IN_USE;
  2832. drbd_msg_put_info("failed to delete connection");
  2833. }
  2834. goto out;
  2835. out:
  2836. drbd_adm_finish(info, retcode);
  2837. return 0;
  2838. }
  2839. int drbd_adm_del_resource(struct sk_buff *skb, struct genl_info *info)
  2840. {
  2841. enum drbd_ret_code retcode;
  2842. retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
  2843. if (!adm_ctx.reply_skb)
  2844. return retcode;
  2845. if (retcode != NO_ERROR)
  2846. goto out;
  2847. if (conn_lowest_minor(adm_ctx.tconn) < 0) {
  2848. list_del_rcu(&adm_ctx.tconn->all_tconn);
  2849. synchronize_rcu();
  2850. kref_put(&adm_ctx.tconn->kref, &conn_destroy);
  2851. retcode = NO_ERROR;
  2852. } else {
  2853. retcode = ERR_RES_IN_USE;
  2854. }
  2855. if (retcode == NO_ERROR)
  2856. drbd_thread_stop(&adm_ctx.tconn->worker);
  2857. out:
  2858. drbd_adm_finish(info, retcode);
  2859. return 0;
  2860. }
  2861. void drbd_bcast_event(struct drbd_conf *mdev, const struct sib_info *sib)
  2862. {
  2863. static atomic_t drbd_genl_seq = ATOMIC_INIT(2); /* two. */
  2864. struct sk_buff *msg;
  2865. struct drbd_genlmsghdr *d_out;
  2866. unsigned seq;
  2867. int err = -ENOMEM;
  2868. if (sib->sib_reason == SIB_SYNC_PROGRESS) {
  2869. if (time_after(jiffies, mdev->rs_last_bcast + HZ))
  2870. mdev->rs_last_bcast = jiffies;
  2871. else
  2872. return;
  2873. }
  2874. seq = atomic_inc_return(&drbd_genl_seq);
  2875. msg = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
  2876. if (!msg)
  2877. goto failed;
  2878. err = -EMSGSIZE;
  2879. d_out = genlmsg_put(msg, 0, seq, &drbd_genl_family, 0, DRBD_EVENT);
  2880. if (!d_out) /* cannot happen, but anyways. */
  2881. goto nla_put_failure;
  2882. d_out->minor = mdev_to_minor(mdev);
  2883. d_out->ret_code = NO_ERROR;
  2884. if (nla_put_status_info(msg, mdev, sib))
  2885. goto nla_put_failure;
  2886. genlmsg_end(msg, d_out);
  2887. err = drbd_genl_multicast_events(msg, 0);
  2888. /* msg has been consumed or freed in netlink_broadcast() */
  2889. if (err && err != -ESRCH)
  2890. goto failed;
  2891. return;
  2892. nla_put_failure:
  2893. nlmsg_free(msg);
  2894. failed:
  2895. dev_err(DEV, "Error %d while broadcasting event. "
  2896. "Event seq:%u sib_reason:%u\n",
  2897. err, seq, sib->sib_reason);
  2898. }