drbd_nl.c 90 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335
  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. static void drbd_md_set_sector_offsets(struct drbd_conf *mdev,
  601. struct drbd_backing_dev *bdev)
  602. {
  603. sector_t md_size_sect = 0;
  604. int meta_dev_idx;
  605. rcu_read_lock();
  606. meta_dev_idx = rcu_dereference(bdev->disk_conf)->meta_dev_idx;
  607. switch (meta_dev_idx) {
  608. default:
  609. /* v07 style fixed size indexed meta data */
  610. bdev->md.md_size_sect = MD_RESERVED_SECT;
  611. bdev->md.md_offset = drbd_md_ss__(mdev, bdev);
  612. bdev->md.al_offset = MD_AL_OFFSET;
  613. bdev->md.bm_offset = MD_BM_OFFSET;
  614. break;
  615. case DRBD_MD_INDEX_FLEX_EXT:
  616. /* just occupy the full device; unit: sectors */
  617. bdev->md.md_size_sect = drbd_get_capacity(bdev->md_bdev);
  618. bdev->md.md_offset = 0;
  619. bdev->md.al_offset = MD_AL_OFFSET;
  620. bdev->md.bm_offset = MD_BM_OFFSET;
  621. break;
  622. case DRBD_MD_INDEX_INTERNAL:
  623. case DRBD_MD_INDEX_FLEX_INT:
  624. bdev->md.md_offset = drbd_md_ss__(mdev, bdev);
  625. /* al size is still fixed */
  626. bdev->md.al_offset = -MD_AL_SECTORS;
  627. /* we need (slightly less than) ~ this much bitmap sectors: */
  628. md_size_sect = drbd_get_capacity(bdev->backing_bdev);
  629. md_size_sect = ALIGN(md_size_sect, BM_SECT_PER_EXT);
  630. md_size_sect = BM_SECT_TO_EXT(md_size_sect);
  631. md_size_sect = ALIGN(md_size_sect, 8);
  632. /* plus the "drbd meta data super block",
  633. * and the activity log; */
  634. md_size_sect += MD_BM_OFFSET;
  635. bdev->md.md_size_sect = md_size_sect;
  636. /* bitmap offset is adjusted by 'super' block size */
  637. bdev->md.bm_offset = -md_size_sect + MD_AL_OFFSET;
  638. break;
  639. }
  640. rcu_read_unlock();
  641. }
  642. /* input size is expected to be in KB */
  643. char *ppsize(char *buf, unsigned long long size)
  644. {
  645. /* Needs 9 bytes at max including trailing NUL:
  646. * -1ULL ==> "16384 EB" */
  647. static char units[] = { 'K', 'M', 'G', 'T', 'P', 'E' };
  648. int base = 0;
  649. while (size >= 10000 && base < sizeof(units)-1) {
  650. /* shift + round */
  651. size = (size >> 10) + !!(size & (1<<9));
  652. base++;
  653. }
  654. sprintf(buf, "%u %cB", (unsigned)size, units[base]);
  655. return buf;
  656. }
  657. /* there is still a theoretical deadlock when called from receiver
  658. * on an D_INCONSISTENT R_PRIMARY:
  659. * remote READ does inc_ap_bio, receiver would need to receive answer
  660. * packet from remote to dec_ap_bio again.
  661. * receiver receive_sizes(), comes here,
  662. * waits for ap_bio_cnt == 0. -> deadlock.
  663. * but this cannot happen, actually, because:
  664. * R_PRIMARY D_INCONSISTENT, and peer's disk is unreachable
  665. * (not connected, or bad/no disk on peer):
  666. * see drbd_fail_request_early, ap_bio_cnt is zero.
  667. * R_PRIMARY D_INCONSISTENT, and C_SYNC_TARGET:
  668. * peer may not initiate a resize.
  669. */
  670. /* Note these are not to be confused with
  671. * drbd_adm_suspend_io/drbd_adm_resume_io,
  672. * which are (sub) state changes triggered by admin (drbdsetup),
  673. * and can be long lived.
  674. * This changes an mdev->flag, is triggered by drbd internals,
  675. * and should be short-lived. */
  676. void drbd_suspend_io(struct drbd_conf *mdev)
  677. {
  678. set_bit(SUSPEND_IO, &mdev->flags);
  679. if (drbd_suspended(mdev))
  680. return;
  681. wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_bio_cnt));
  682. }
  683. void drbd_resume_io(struct drbd_conf *mdev)
  684. {
  685. clear_bit(SUSPEND_IO, &mdev->flags);
  686. wake_up(&mdev->misc_wait);
  687. }
  688. /**
  689. * drbd_determine_dev_size() - Sets the right device size obeying all constraints
  690. * @mdev: DRBD device.
  691. *
  692. * Returns 0 on success, negative return values indicate errors.
  693. * You should call drbd_md_sync() after calling this function.
  694. */
  695. enum determine_dev_size drbd_determine_dev_size(struct drbd_conf *mdev, enum dds_flags flags) __must_hold(local)
  696. {
  697. sector_t prev_first_sect, prev_size; /* previous meta location */
  698. sector_t la_size, u_size;
  699. sector_t size;
  700. char ppb[10];
  701. int md_moved, la_size_changed;
  702. enum determine_dev_size rv = unchanged;
  703. /* race:
  704. * application request passes inc_ap_bio,
  705. * but then cannot get an AL-reference.
  706. * this function later may wait on ap_bio_cnt == 0. -> deadlock.
  707. *
  708. * to avoid that:
  709. * Suspend IO right here.
  710. * still lock the act_log to not trigger ASSERTs there.
  711. */
  712. drbd_suspend_io(mdev);
  713. /* no wait necessary anymore, actually we could assert that */
  714. wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
  715. prev_first_sect = drbd_md_first_sector(mdev->ldev);
  716. prev_size = mdev->ldev->md.md_size_sect;
  717. la_size = mdev->ldev->md.la_size_sect;
  718. /* TODO: should only be some assert here, not (re)init... */
  719. drbd_md_set_sector_offsets(mdev, mdev->ldev);
  720. rcu_read_lock();
  721. u_size = rcu_dereference(mdev->ldev->disk_conf)->disk_size;
  722. rcu_read_unlock();
  723. size = drbd_new_dev_size(mdev, mdev->ldev, u_size, flags & DDSF_FORCED);
  724. if (drbd_get_capacity(mdev->this_bdev) != size ||
  725. drbd_bm_capacity(mdev) != size) {
  726. int err;
  727. err = drbd_bm_resize(mdev, size, !(flags & DDSF_NO_RESYNC));
  728. if (unlikely(err)) {
  729. /* currently there is only one error: ENOMEM! */
  730. size = drbd_bm_capacity(mdev)>>1;
  731. if (size == 0) {
  732. dev_err(DEV, "OUT OF MEMORY! "
  733. "Could not allocate bitmap!\n");
  734. } else {
  735. dev_err(DEV, "BM resizing failed. "
  736. "Leaving size unchanged at size = %lu KB\n",
  737. (unsigned long)size);
  738. }
  739. rv = dev_size_error;
  740. }
  741. /* racy, see comments above. */
  742. drbd_set_my_capacity(mdev, size);
  743. mdev->ldev->md.la_size_sect = size;
  744. dev_info(DEV, "size = %s (%llu KB)\n", ppsize(ppb, size>>1),
  745. (unsigned long long)size>>1);
  746. }
  747. if (rv == dev_size_error)
  748. goto out;
  749. la_size_changed = (la_size != mdev->ldev->md.la_size_sect);
  750. md_moved = prev_first_sect != drbd_md_first_sector(mdev->ldev)
  751. || prev_size != mdev->ldev->md.md_size_sect;
  752. if (la_size_changed || md_moved) {
  753. int err;
  754. drbd_al_shrink(mdev); /* All extents inactive. */
  755. dev_info(DEV, "Writing the whole bitmap, %s\n",
  756. la_size_changed && md_moved ? "size changed and md moved" :
  757. la_size_changed ? "size changed" : "md moved");
  758. /* next line implicitly does drbd_suspend_io()+drbd_resume_io() */
  759. err = drbd_bitmap_io(mdev, md_moved ? &drbd_bm_write_all : &drbd_bm_write,
  760. "size changed", BM_LOCKED_MASK);
  761. if (err) {
  762. rv = dev_size_error;
  763. goto out;
  764. }
  765. drbd_md_mark_dirty(mdev);
  766. }
  767. if (size > la_size)
  768. rv = grew;
  769. if (size < la_size)
  770. rv = shrunk;
  771. out:
  772. lc_unlock(mdev->act_log);
  773. wake_up(&mdev->al_wait);
  774. drbd_resume_io(mdev);
  775. return rv;
  776. }
  777. sector_t
  778. drbd_new_dev_size(struct drbd_conf *mdev, struct drbd_backing_dev *bdev,
  779. sector_t u_size, int assume_peer_has_space)
  780. {
  781. sector_t p_size = mdev->p_size; /* partner's disk size. */
  782. sector_t la_size = bdev->md.la_size_sect; /* last agreed size. */
  783. sector_t m_size; /* my size */
  784. sector_t size = 0;
  785. m_size = drbd_get_max_capacity(bdev);
  786. if (mdev->state.conn < C_CONNECTED && assume_peer_has_space) {
  787. dev_warn(DEV, "Resize while not connected was forced by the user!\n");
  788. p_size = m_size;
  789. }
  790. if (p_size && m_size) {
  791. size = min_t(sector_t, p_size, m_size);
  792. } else {
  793. if (la_size) {
  794. size = la_size;
  795. if (m_size && m_size < size)
  796. size = m_size;
  797. if (p_size && p_size < size)
  798. size = p_size;
  799. } else {
  800. if (m_size)
  801. size = m_size;
  802. if (p_size)
  803. size = p_size;
  804. }
  805. }
  806. if (size == 0)
  807. dev_err(DEV, "Both nodes diskless!\n");
  808. if (u_size) {
  809. if (u_size > size)
  810. dev_err(DEV, "Requested disk size is too big (%lu > %lu)\n",
  811. (unsigned long)u_size>>1, (unsigned long)size>>1);
  812. else
  813. size = u_size;
  814. }
  815. return size;
  816. }
  817. /**
  818. * drbd_check_al_size() - Ensures that the AL is of the right size
  819. * @mdev: DRBD device.
  820. *
  821. * Returns -EBUSY if current al lru is still used, -ENOMEM when allocation
  822. * failed, and 0 on success. You should call drbd_md_sync() after you called
  823. * this function.
  824. */
  825. static int drbd_check_al_size(struct drbd_conf *mdev, struct disk_conf *dc)
  826. {
  827. struct lru_cache *n, *t;
  828. struct lc_element *e;
  829. unsigned int in_use;
  830. int i;
  831. if (mdev->act_log &&
  832. mdev->act_log->nr_elements == dc->al_extents)
  833. return 0;
  834. in_use = 0;
  835. t = mdev->act_log;
  836. n = lc_create("act_log", drbd_al_ext_cache, AL_UPDATES_PER_TRANSACTION,
  837. dc->al_extents, sizeof(struct lc_element), 0);
  838. if (n == NULL) {
  839. dev_err(DEV, "Cannot allocate act_log lru!\n");
  840. return -ENOMEM;
  841. }
  842. spin_lock_irq(&mdev->al_lock);
  843. if (t) {
  844. for (i = 0; i < t->nr_elements; i++) {
  845. e = lc_element_by_index(t, i);
  846. if (e->refcnt)
  847. dev_err(DEV, "refcnt(%d)==%d\n",
  848. e->lc_number, e->refcnt);
  849. in_use += e->refcnt;
  850. }
  851. }
  852. if (!in_use)
  853. mdev->act_log = n;
  854. spin_unlock_irq(&mdev->al_lock);
  855. if (in_use) {
  856. dev_err(DEV, "Activity log still in use!\n");
  857. lc_destroy(n);
  858. return -EBUSY;
  859. } else {
  860. if (t)
  861. lc_destroy(t);
  862. }
  863. drbd_md_mark_dirty(mdev); /* we changed mdev->act_log->nr_elemens */
  864. return 0;
  865. }
  866. static void drbd_setup_queue_param(struct drbd_conf *mdev, unsigned int max_bio_size)
  867. {
  868. struct request_queue * const q = mdev->rq_queue;
  869. unsigned int max_hw_sectors = max_bio_size >> 9;
  870. unsigned int max_segments = 0;
  871. if (get_ldev_if_state(mdev, D_ATTACHING)) {
  872. struct request_queue * const b = mdev->ldev->backing_bdev->bd_disk->queue;
  873. max_hw_sectors = min(queue_max_hw_sectors(b), max_bio_size >> 9);
  874. rcu_read_lock();
  875. max_segments = rcu_dereference(mdev->ldev->disk_conf)->max_bio_bvecs;
  876. rcu_read_unlock();
  877. put_ldev(mdev);
  878. }
  879. blk_queue_logical_block_size(q, 512);
  880. blk_queue_max_hw_sectors(q, max_hw_sectors);
  881. /* This is the workaround for "bio would need to, but cannot, be split" */
  882. blk_queue_max_segments(q, max_segments ? max_segments : BLK_MAX_SEGMENTS);
  883. blk_queue_segment_boundary(q, PAGE_CACHE_SIZE-1);
  884. if (get_ldev_if_state(mdev, D_ATTACHING)) {
  885. struct request_queue * const b = mdev->ldev->backing_bdev->bd_disk->queue;
  886. blk_queue_stack_limits(q, b);
  887. if (q->backing_dev_info.ra_pages != b->backing_dev_info.ra_pages) {
  888. dev_info(DEV, "Adjusting my ra_pages to backing device's (%lu -> %lu)\n",
  889. q->backing_dev_info.ra_pages,
  890. b->backing_dev_info.ra_pages);
  891. q->backing_dev_info.ra_pages = b->backing_dev_info.ra_pages;
  892. }
  893. put_ldev(mdev);
  894. }
  895. }
  896. void drbd_reconsider_max_bio_size(struct drbd_conf *mdev)
  897. {
  898. unsigned int now, new, local, peer;
  899. now = queue_max_hw_sectors(mdev->rq_queue) << 9;
  900. local = mdev->local_max_bio_size; /* Eventually last known value, from volatile memory */
  901. peer = mdev->peer_max_bio_size; /* Eventually last known value, from meta data */
  902. if (get_ldev_if_state(mdev, D_ATTACHING)) {
  903. local = queue_max_hw_sectors(mdev->ldev->backing_bdev->bd_disk->queue) << 9;
  904. mdev->local_max_bio_size = local;
  905. put_ldev(mdev);
  906. }
  907. local = min(local, DRBD_MAX_BIO_SIZE);
  908. /* We may ignore peer limits if the peer is modern enough.
  909. Because new from 8.3.8 onwards the peer can use multiple
  910. BIOs for a single peer_request */
  911. if (mdev->state.conn >= C_CONNECTED) {
  912. if (mdev->tconn->agreed_pro_version < 94)
  913. peer = min( mdev->peer_max_bio_size, DRBD_MAX_SIZE_H80_PACKET);
  914. /* Correct old drbd (up to 8.3.7) if it believes it can do more than 32KiB */
  915. else if (mdev->tconn->agreed_pro_version == 94)
  916. peer = DRBD_MAX_SIZE_H80_PACKET;
  917. else if (mdev->tconn->agreed_pro_version < 100)
  918. peer = DRBD_MAX_BIO_SIZE_P95; /* drbd 8.3.8 onwards, before 8.4.0 */
  919. else
  920. peer = DRBD_MAX_BIO_SIZE;
  921. }
  922. new = min(local, peer);
  923. if (mdev->state.role == R_PRIMARY && new < now)
  924. dev_err(DEV, "ASSERT FAILED new < now; (%u < %u)\n", new, now);
  925. if (new != now)
  926. dev_info(DEV, "max BIO size = %u\n", new);
  927. drbd_setup_queue_param(mdev, new);
  928. }
  929. /* Starts the worker thread */
  930. static void conn_reconfig_start(struct drbd_tconn *tconn)
  931. {
  932. drbd_thread_start(&tconn->worker);
  933. conn_flush_workqueue(tconn);
  934. }
  935. /* if still unconfigured, stops worker again. */
  936. static void conn_reconfig_done(struct drbd_tconn *tconn)
  937. {
  938. bool stop_threads;
  939. spin_lock_irq(&tconn->req_lock);
  940. stop_threads = conn_all_vols_unconf(tconn) &&
  941. tconn->cstate == C_STANDALONE;
  942. spin_unlock_irq(&tconn->req_lock);
  943. if (stop_threads) {
  944. /* asender is implicitly stopped by receiver
  945. * in conn_disconnect() */
  946. drbd_thread_stop(&tconn->receiver);
  947. drbd_thread_stop(&tconn->worker);
  948. }
  949. }
  950. /* Make sure IO is suspended before calling this function(). */
  951. static void drbd_suspend_al(struct drbd_conf *mdev)
  952. {
  953. int s = 0;
  954. if (!lc_try_lock(mdev->act_log)) {
  955. dev_warn(DEV, "Failed to lock al in drbd_suspend_al()\n");
  956. return;
  957. }
  958. drbd_al_shrink(mdev);
  959. spin_lock_irq(&mdev->tconn->req_lock);
  960. if (mdev->state.conn < C_CONNECTED)
  961. s = !test_and_set_bit(AL_SUSPENDED, &mdev->flags);
  962. spin_unlock_irq(&mdev->tconn->req_lock);
  963. lc_unlock(mdev->act_log);
  964. if (s)
  965. dev_info(DEV, "Suspended AL updates\n");
  966. }
  967. static bool should_set_defaults(struct genl_info *info)
  968. {
  969. unsigned flags = ((struct drbd_genlmsghdr*)info->userhdr)->flags;
  970. return 0 != (flags & DRBD_GENL_F_SET_DEFAULTS);
  971. }
  972. static void enforce_disk_conf_limits(struct disk_conf *dc)
  973. {
  974. if (dc->al_extents < DRBD_AL_EXTENTS_MIN)
  975. dc->al_extents = DRBD_AL_EXTENTS_MIN;
  976. if (dc->al_extents > DRBD_AL_EXTENTS_MAX)
  977. dc->al_extents = DRBD_AL_EXTENTS_MAX;
  978. if (dc->c_plan_ahead > DRBD_C_PLAN_AHEAD_MAX)
  979. dc->c_plan_ahead = DRBD_C_PLAN_AHEAD_MAX;
  980. }
  981. int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info)
  982. {
  983. enum drbd_ret_code retcode;
  984. struct drbd_conf *mdev;
  985. struct disk_conf *new_disk_conf, *old_disk_conf;
  986. struct fifo_buffer *old_plan = NULL, *new_plan = NULL;
  987. int err, fifo_size;
  988. retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
  989. if (!adm_ctx.reply_skb)
  990. return retcode;
  991. if (retcode != NO_ERROR)
  992. goto out;
  993. mdev = adm_ctx.mdev;
  994. /* we also need a disk
  995. * to change the options on */
  996. if (!get_ldev(mdev)) {
  997. retcode = ERR_NO_DISK;
  998. goto out;
  999. }
  1000. new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
  1001. if (!new_disk_conf) {
  1002. retcode = ERR_NOMEM;
  1003. goto fail;
  1004. }
  1005. mutex_lock(&mdev->tconn->conf_update);
  1006. old_disk_conf = mdev->ldev->disk_conf;
  1007. *new_disk_conf = *old_disk_conf;
  1008. if (should_set_defaults(info))
  1009. set_disk_conf_defaults(new_disk_conf);
  1010. err = disk_conf_from_attrs_for_change(new_disk_conf, info);
  1011. if (err && err != -ENOMSG) {
  1012. retcode = ERR_MANDATORY_TAG;
  1013. drbd_msg_put_info(from_attrs_err_to_txt(err));
  1014. }
  1015. if (!expect(new_disk_conf->resync_rate >= 1))
  1016. new_disk_conf->resync_rate = 1;
  1017. enforce_disk_conf_limits(new_disk_conf);
  1018. fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
  1019. if (fifo_size != mdev->rs_plan_s->size) {
  1020. new_plan = fifo_alloc(fifo_size);
  1021. if (!new_plan) {
  1022. dev_err(DEV, "kmalloc of fifo_buffer failed");
  1023. retcode = ERR_NOMEM;
  1024. goto fail_unlock;
  1025. }
  1026. }
  1027. drbd_suspend_io(mdev);
  1028. wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
  1029. drbd_al_shrink(mdev);
  1030. err = drbd_check_al_size(mdev, new_disk_conf);
  1031. lc_unlock(mdev->act_log);
  1032. wake_up(&mdev->al_wait);
  1033. drbd_resume_io(mdev);
  1034. if (err) {
  1035. retcode = ERR_NOMEM;
  1036. goto fail_unlock;
  1037. }
  1038. write_lock_irq(&global_state_lock);
  1039. retcode = drbd_resync_after_valid(mdev, new_disk_conf->resync_after);
  1040. if (retcode == NO_ERROR) {
  1041. rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
  1042. drbd_resync_after_changed(mdev);
  1043. }
  1044. write_unlock_irq(&global_state_lock);
  1045. if (retcode != NO_ERROR)
  1046. goto fail_unlock;
  1047. if (new_plan) {
  1048. old_plan = mdev->rs_plan_s;
  1049. rcu_assign_pointer(mdev->rs_plan_s, new_plan);
  1050. }
  1051. mutex_unlock(&mdev->tconn->conf_update);
  1052. if (new_disk_conf->al_updates)
  1053. mdev->ldev->md.flags &= ~MDF_AL_DISABLED;
  1054. else
  1055. mdev->ldev->md.flags |= MDF_AL_DISABLED;
  1056. if (new_disk_conf->md_flushes)
  1057. clear_bit(MD_NO_FUA, &mdev->flags);
  1058. else
  1059. set_bit(MD_NO_FUA, &mdev->flags);
  1060. drbd_bump_write_ordering(mdev->tconn, WO_bdev_flush);
  1061. drbd_md_sync(mdev);
  1062. if (mdev->state.conn >= C_CONNECTED)
  1063. drbd_send_sync_param(mdev);
  1064. synchronize_rcu();
  1065. kfree(old_disk_conf);
  1066. kfree(old_plan);
  1067. mod_timer(&mdev->request_timer, jiffies + HZ);
  1068. goto success;
  1069. fail_unlock:
  1070. mutex_unlock(&mdev->tconn->conf_update);
  1071. fail:
  1072. kfree(new_disk_conf);
  1073. kfree(new_plan);
  1074. success:
  1075. put_ldev(mdev);
  1076. out:
  1077. drbd_adm_finish(info, retcode);
  1078. return 0;
  1079. }
  1080. int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info)
  1081. {
  1082. struct drbd_conf *mdev;
  1083. int err;
  1084. enum drbd_ret_code retcode;
  1085. enum determine_dev_size dd;
  1086. sector_t max_possible_sectors;
  1087. sector_t min_md_device_sectors;
  1088. struct drbd_backing_dev *nbc = NULL; /* new_backing_conf */
  1089. struct disk_conf *new_disk_conf = NULL;
  1090. struct block_device *bdev;
  1091. struct lru_cache *resync_lru = NULL;
  1092. struct fifo_buffer *new_plan = NULL;
  1093. union drbd_state ns, os;
  1094. enum drbd_state_rv rv;
  1095. struct net_conf *nc;
  1096. retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
  1097. if (!adm_ctx.reply_skb)
  1098. return retcode;
  1099. if (retcode != NO_ERROR)
  1100. goto finish;
  1101. mdev = adm_ctx.mdev;
  1102. conn_reconfig_start(mdev->tconn);
  1103. /* if you want to reconfigure, please tear down first */
  1104. if (mdev->state.disk > D_DISKLESS) {
  1105. retcode = ERR_DISK_CONFIGURED;
  1106. goto fail;
  1107. }
  1108. /* It may just now have detached because of IO error. Make sure
  1109. * drbd_ldev_destroy is done already, we may end up here very fast,
  1110. * e.g. if someone calls attach from the on-io-error handler,
  1111. * to realize a "hot spare" feature (not that I'd recommend that) */
  1112. wait_event(mdev->misc_wait, !atomic_read(&mdev->local_cnt));
  1113. /* make sure there is no leftover from previous force-detach attempts */
  1114. clear_bit(FORCE_DETACH, &mdev->flags);
  1115. clear_bit(WAS_IO_ERROR, &mdev->flags);
  1116. clear_bit(WAS_READ_ERROR, &mdev->flags);
  1117. /* and no leftover from previously aborted resync or verify, either */
  1118. mdev->rs_total = 0;
  1119. mdev->rs_failed = 0;
  1120. atomic_set(&mdev->rs_pending_cnt, 0);
  1121. /* allocation not in the IO path, drbdsetup context */
  1122. nbc = kzalloc(sizeof(struct drbd_backing_dev), GFP_KERNEL);
  1123. if (!nbc) {
  1124. retcode = ERR_NOMEM;
  1125. goto fail;
  1126. }
  1127. spin_lock_init(&nbc->md.uuid_lock);
  1128. new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
  1129. if (!new_disk_conf) {
  1130. retcode = ERR_NOMEM;
  1131. goto fail;
  1132. }
  1133. nbc->disk_conf = new_disk_conf;
  1134. set_disk_conf_defaults(new_disk_conf);
  1135. err = disk_conf_from_attrs(new_disk_conf, info);
  1136. if (err) {
  1137. retcode = ERR_MANDATORY_TAG;
  1138. drbd_msg_put_info(from_attrs_err_to_txt(err));
  1139. goto fail;
  1140. }
  1141. enforce_disk_conf_limits(new_disk_conf);
  1142. new_plan = fifo_alloc((new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ);
  1143. if (!new_plan) {
  1144. retcode = ERR_NOMEM;
  1145. goto fail;
  1146. }
  1147. if (new_disk_conf->meta_dev_idx < DRBD_MD_INDEX_FLEX_INT) {
  1148. retcode = ERR_MD_IDX_INVALID;
  1149. goto fail;
  1150. }
  1151. rcu_read_lock();
  1152. nc = rcu_dereference(mdev->tconn->net_conf);
  1153. if (nc) {
  1154. if (new_disk_conf->fencing == FP_STONITH && nc->wire_protocol == DRBD_PROT_A) {
  1155. rcu_read_unlock();
  1156. retcode = ERR_STONITH_AND_PROT_A;
  1157. goto fail;
  1158. }
  1159. }
  1160. rcu_read_unlock();
  1161. bdev = blkdev_get_by_path(new_disk_conf->backing_dev,
  1162. FMODE_READ | FMODE_WRITE | FMODE_EXCL, mdev);
  1163. if (IS_ERR(bdev)) {
  1164. dev_err(DEV, "open(\"%s\") failed with %ld\n", new_disk_conf->backing_dev,
  1165. PTR_ERR(bdev));
  1166. retcode = ERR_OPEN_DISK;
  1167. goto fail;
  1168. }
  1169. nbc->backing_bdev = bdev;
  1170. /*
  1171. * meta_dev_idx >= 0: external fixed size, possibly multiple
  1172. * drbd sharing one meta device. TODO in that case, paranoia
  1173. * check that [md_bdev, meta_dev_idx] is not yet used by some
  1174. * other drbd minor! (if you use drbd.conf + drbdadm, that
  1175. * should check it for you already; but if you don't, or
  1176. * someone fooled it, we need to double check here)
  1177. */
  1178. bdev = blkdev_get_by_path(new_disk_conf->meta_dev,
  1179. FMODE_READ | FMODE_WRITE | FMODE_EXCL,
  1180. (new_disk_conf->meta_dev_idx < 0) ?
  1181. (void *)mdev : (void *)drbd_m_holder);
  1182. if (IS_ERR(bdev)) {
  1183. dev_err(DEV, "open(\"%s\") failed with %ld\n", new_disk_conf->meta_dev,
  1184. PTR_ERR(bdev));
  1185. retcode = ERR_OPEN_MD_DISK;
  1186. goto fail;
  1187. }
  1188. nbc->md_bdev = bdev;
  1189. if ((nbc->backing_bdev == nbc->md_bdev) !=
  1190. (new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_INTERNAL ||
  1191. new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_FLEX_INT)) {
  1192. retcode = ERR_MD_IDX_INVALID;
  1193. goto fail;
  1194. }
  1195. resync_lru = lc_create("resync", drbd_bm_ext_cache,
  1196. 1, 61, sizeof(struct bm_extent),
  1197. offsetof(struct bm_extent, lce));
  1198. if (!resync_lru) {
  1199. retcode = ERR_NOMEM;
  1200. goto fail;
  1201. }
  1202. /* RT - for drbd_get_max_capacity() DRBD_MD_INDEX_FLEX_INT */
  1203. drbd_md_set_sector_offsets(mdev, nbc);
  1204. if (drbd_get_max_capacity(nbc) < new_disk_conf->disk_size) {
  1205. dev_err(DEV, "max capacity %llu smaller than disk size %llu\n",
  1206. (unsigned long long) drbd_get_max_capacity(nbc),
  1207. (unsigned long long) new_disk_conf->disk_size);
  1208. retcode = ERR_DISK_TOO_SMALL;
  1209. goto fail;
  1210. }
  1211. if (new_disk_conf->meta_dev_idx < 0) {
  1212. max_possible_sectors = DRBD_MAX_SECTORS_FLEX;
  1213. /* at least one MB, otherwise it does not make sense */
  1214. min_md_device_sectors = (2<<10);
  1215. } else {
  1216. max_possible_sectors = DRBD_MAX_SECTORS;
  1217. min_md_device_sectors = MD_RESERVED_SECT * (new_disk_conf->meta_dev_idx + 1);
  1218. }
  1219. if (drbd_get_capacity(nbc->md_bdev) < min_md_device_sectors) {
  1220. retcode = ERR_MD_DISK_TOO_SMALL;
  1221. dev_warn(DEV, "refusing attach: md-device too small, "
  1222. "at least %llu sectors needed for this meta-disk type\n",
  1223. (unsigned long long) min_md_device_sectors);
  1224. goto fail;
  1225. }
  1226. /* Make sure the new disk is big enough
  1227. * (we may currently be R_PRIMARY with no local disk...) */
  1228. if (drbd_get_max_capacity(nbc) <
  1229. drbd_get_capacity(mdev->this_bdev)) {
  1230. retcode = ERR_DISK_TOO_SMALL;
  1231. goto fail;
  1232. }
  1233. nbc->known_size = drbd_get_capacity(nbc->backing_bdev);
  1234. if (nbc->known_size > max_possible_sectors) {
  1235. dev_warn(DEV, "==> truncating very big lower level device "
  1236. "to currently maximum possible %llu sectors <==\n",
  1237. (unsigned long long) max_possible_sectors);
  1238. if (new_disk_conf->meta_dev_idx >= 0)
  1239. dev_warn(DEV, "==>> using internal or flexible "
  1240. "meta data may help <<==\n");
  1241. }
  1242. drbd_suspend_io(mdev);
  1243. /* also wait for the last barrier ack. */
  1244. /* FIXME see also https://daiquiri.linbit/cgi-bin/bugzilla/show_bug.cgi?id=171
  1245. * We need a way to either ignore barrier acks for barriers sent before a device
  1246. * was attached, or a way to wait for all pending barrier acks to come in.
  1247. * As barriers are counted per resource,
  1248. * we'd need to suspend io on all devices of a resource.
  1249. */
  1250. wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_pending_cnt) || drbd_suspended(mdev));
  1251. /* and for any other previously queued work */
  1252. drbd_flush_workqueue(mdev);
  1253. rv = _drbd_request_state(mdev, NS(disk, D_ATTACHING), CS_VERBOSE);
  1254. retcode = rv; /* FIXME: Type mismatch. */
  1255. drbd_resume_io(mdev);
  1256. if (rv < SS_SUCCESS)
  1257. goto fail;
  1258. if (!get_ldev_if_state(mdev, D_ATTACHING))
  1259. goto force_diskless;
  1260. drbd_md_set_sector_offsets(mdev, nbc);
  1261. if (!mdev->bitmap) {
  1262. if (drbd_bm_init(mdev)) {
  1263. retcode = ERR_NOMEM;
  1264. goto force_diskless_dec;
  1265. }
  1266. }
  1267. retcode = drbd_md_read(mdev, nbc);
  1268. if (retcode != NO_ERROR)
  1269. goto force_diskless_dec;
  1270. if (mdev->state.conn < C_CONNECTED &&
  1271. mdev->state.role == R_PRIMARY &&
  1272. (mdev->ed_uuid & ~((u64)1)) != (nbc->md.uuid[UI_CURRENT] & ~((u64)1))) {
  1273. dev_err(DEV, "Can only attach to data with current UUID=%016llX\n",
  1274. (unsigned long long)mdev->ed_uuid);
  1275. retcode = ERR_DATA_NOT_CURRENT;
  1276. goto force_diskless_dec;
  1277. }
  1278. /* Since we are diskless, fix the activity log first... */
  1279. if (drbd_check_al_size(mdev, new_disk_conf)) {
  1280. retcode = ERR_NOMEM;
  1281. goto force_diskless_dec;
  1282. }
  1283. /* Prevent shrinking of consistent devices ! */
  1284. if (drbd_md_test_flag(nbc, MDF_CONSISTENT) &&
  1285. drbd_new_dev_size(mdev, nbc, nbc->disk_conf->disk_size, 0) < nbc->md.la_size_sect) {
  1286. dev_warn(DEV, "refusing to truncate a consistent device\n");
  1287. retcode = ERR_DISK_TOO_SMALL;
  1288. goto force_diskless_dec;
  1289. }
  1290. /* Reset the "barriers don't work" bits here, then force meta data to
  1291. * be written, to ensure we determine if barriers are supported. */
  1292. if (new_disk_conf->md_flushes)
  1293. clear_bit(MD_NO_FUA, &mdev->flags);
  1294. else
  1295. set_bit(MD_NO_FUA, &mdev->flags);
  1296. /* Point of no return reached.
  1297. * Devices and memory are no longer released by error cleanup below.
  1298. * now mdev takes over responsibility, and the state engine should
  1299. * clean it up somewhere. */
  1300. D_ASSERT(mdev->ldev == NULL);
  1301. mdev->ldev = nbc;
  1302. mdev->resync = resync_lru;
  1303. mdev->rs_plan_s = new_plan;
  1304. nbc = NULL;
  1305. resync_lru = NULL;
  1306. new_disk_conf = NULL;
  1307. new_plan = NULL;
  1308. drbd_bump_write_ordering(mdev->tconn, WO_bdev_flush);
  1309. if (drbd_md_test_flag(mdev->ldev, MDF_CRASHED_PRIMARY))
  1310. set_bit(CRASHED_PRIMARY, &mdev->flags);
  1311. else
  1312. clear_bit(CRASHED_PRIMARY, &mdev->flags);
  1313. if (drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) &&
  1314. !(mdev->state.role == R_PRIMARY && mdev->tconn->susp_nod))
  1315. set_bit(CRASHED_PRIMARY, &mdev->flags);
  1316. mdev->send_cnt = 0;
  1317. mdev->recv_cnt = 0;
  1318. mdev->read_cnt = 0;
  1319. mdev->writ_cnt = 0;
  1320. drbd_reconsider_max_bio_size(mdev);
  1321. /* If I am currently not R_PRIMARY,
  1322. * but meta data primary indicator is set,
  1323. * I just now recover from a hard crash,
  1324. * and have been R_PRIMARY before that crash.
  1325. *
  1326. * Now, if I had no connection before that crash
  1327. * (have been degraded R_PRIMARY), chances are that
  1328. * I won't find my peer now either.
  1329. *
  1330. * In that case, and _only_ in that case,
  1331. * we use the degr-wfc-timeout instead of the default,
  1332. * so we can automatically recover from a crash of a
  1333. * degraded but active "cluster" after a certain timeout.
  1334. */
  1335. clear_bit(USE_DEGR_WFC_T, &mdev->flags);
  1336. if (mdev->state.role != R_PRIMARY &&
  1337. drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) &&
  1338. !drbd_md_test_flag(mdev->ldev, MDF_CONNECTED_IND))
  1339. set_bit(USE_DEGR_WFC_T, &mdev->flags);
  1340. dd = drbd_determine_dev_size(mdev, 0);
  1341. if (dd == dev_size_error) {
  1342. retcode = ERR_NOMEM_BITMAP;
  1343. goto force_diskless_dec;
  1344. } else if (dd == grew)
  1345. set_bit(RESYNC_AFTER_NEG, &mdev->flags);
  1346. if (drbd_md_test_flag(mdev->ldev, MDF_FULL_SYNC) ||
  1347. (test_bit(CRASHED_PRIMARY, &mdev->flags) &&
  1348. drbd_md_test_flag(mdev->ldev, MDF_AL_DISABLED))) {
  1349. dev_info(DEV, "Assuming that all blocks are out of sync "
  1350. "(aka FullSync)\n");
  1351. if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write,
  1352. "set_n_write from attaching", BM_LOCKED_MASK)) {
  1353. retcode = ERR_IO_MD_DISK;
  1354. goto force_diskless_dec;
  1355. }
  1356. } else {
  1357. if (drbd_bitmap_io(mdev, &drbd_bm_read,
  1358. "read from attaching", BM_LOCKED_MASK)) {
  1359. retcode = ERR_IO_MD_DISK;
  1360. goto force_diskless_dec;
  1361. }
  1362. }
  1363. if (_drbd_bm_total_weight(mdev) == drbd_bm_bits(mdev))
  1364. drbd_suspend_al(mdev); /* IO is still suspended here... */
  1365. spin_lock_irq(&mdev->tconn->req_lock);
  1366. os = drbd_read_state(mdev);
  1367. ns = os;
  1368. /* If MDF_CONSISTENT is not set go into inconsistent state,
  1369. otherwise investigate MDF_WasUpToDate...
  1370. If MDF_WAS_UP_TO_DATE is not set go into D_OUTDATED disk state,
  1371. otherwise into D_CONSISTENT state.
  1372. */
  1373. if (drbd_md_test_flag(mdev->ldev, MDF_CONSISTENT)) {
  1374. if (drbd_md_test_flag(mdev->ldev, MDF_WAS_UP_TO_DATE))
  1375. ns.disk = D_CONSISTENT;
  1376. else
  1377. ns.disk = D_OUTDATED;
  1378. } else {
  1379. ns.disk = D_INCONSISTENT;
  1380. }
  1381. if (drbd_md_test_flag(mdev->ldev, MDF_PEER_OUT_DATED))
  1382. ns.pdsk = D_OUTDATED;
  1383. rcu_read_lock();
  1384. if (ns.disk == D_CONSISTENT &&
  1385. (ns.pdsk == D_OUTDATED || rcu_dereference(mdev->ldev->disk_conf)->fencing == FP_DONT_CARE))
  1386. ns.disk = D_UP_TO_DATE;
  1387. /* All tests on MDF_PRIMARY_IND, MDF_CONNECTED_IND,
  1388. MDF_CONSISTENT and MDF_WAS_UP_TO_DATE must happen before
  1389. this point, because drbd_request_state() modifies these
  1390. flags. */
  1391. if (rcu_dereference(mdev->ldev->disk_conf)->al_updates)
  1392. mdev->ldev->md.flags &= ~MDF_AL_DISABLED;
  1393. else
  1394. mdev->ldev->md.flags |= MDF_AL_DISABLED;
  1395. rcu_read_unlock();
  1396. /* In case we are C_CONNECTED postpone any decision on the new disk
  1397. state after the negotiation phase. */
  1398. if (mdev->state.conn == C_CONNECTED) {
  1399. mdev->new_state_tmp.i = ns.i;
  1400. ns.i = os.i;
  1401. ns.disk = D_NEGOTIATING;
  1402. /* We expect to receive up-to-date UUIDs soon.
  1403. To avoid a race in receive_state, free p_uuid while
  1404. holding req_lock. I.e. atomic with the state change */
  1405. kfree(mdev->p_uuid);
  1406. mdev->p_uuid = NULL;
  1407. }
  1408. rv = _drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
  1409. spin_unlock_irq(&mdev->tconn->req_lock);
  1410. if (rv < SS_SUCCESS)
  1411. goto force_diskless_dec;
  1412. mod_timer(&mdev->request_timer, jiffies + HZ);
  1413. if (mdev->state.role == R_PRIMARY)
  1414. mdev->ldev->md.uuid[UI_CURRENT] |= (u64)1;
  1415. else
  1416. mdev->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
  1417. drbd_md_mark_dirty(mdev);
  1418. drbd_md_sync(mdev);
  1419. kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
  1420. put_ldev(mdev);
  1421. conn_reconfig_done(mdev->tconn);
  1422. drbd_adm_finish(info, retcode);
  1423. return 0;
  1424. force_diskless_dec:
  1425. put_ldev(mdev);
  1426. force_diskless:
  1427. drbd_force_state(mdev, NS(disk, D_DISKLESS));
  1428. drbd_md_sync(mdev);
  1429. fail:
  1430. conn_reconfig_done(mdev->tconn);
  1431. if (nbc) {
  1432. if (nbc->backing_bdev)
  1433. blkdev_put(nbc->backing_bdev,
  1434. FMODE_READ | FMODE_WRITE | FMODE_EXCL);
  1435. if (nbc->md_bdev)
  1436. blkdev_put(nbc->md_bdev,
  1437. FMODE_READ | FMODE_WRITE | FMODE_EXCL);
  1438. kfree(nbc);
  1439. }
  1440. kfree(new_disk_conf);
  1441. lc_destroy(resync_lru);
  1442. kfree(new_plan);
  1443. finish:
  1444. drbd_adm_finish(info, retcode);
  1445. return 0;
  1446. }
  1447. static int adm_detach(struct drbd_conf *mdev, int force)
  1448. {
  1449. enum drbd_state_rv retcode;
  1450. int ret;
  1451. if (force) {
  1452. set_bit(FORCE_DETACH, &mdev->flags);
  1453. drbd_force_state(mdev, NS(disk, D_FAILED));
  1454. retcode = SS_SUCCESS;
  1455. goto out;
  1456. }
  1457. drbd_suspend_io(mdev); /* so no-one is stuck in drbd_al_begin_io */
  1458. drbd_md_get_buffer(mdev); /* make sure there is no in-flight meta-data IO */
  1459. retcode = drbd_request_state(mdev, NS(disk, D_FAILED));
  1460. drbd_md_put_buffer(mdev);
  1461. /* D_FAILED will transition to DISKLESS. */
  1462. ret = wait_event_interruptible(mdev->misc_wait,
  1463. mdev->state.disk != D_FAILED);
  1464. drbd_resume_io(mdev);
  1465. if ((int)retcode == (int)SS_IS_DISKLESS)
  1466. retcode = SS_NOTHING_TO_DO;
  1467. if (ret)
  1468. retcode = ERR_INTR;
  1469. out:
  1470. return retcode;
  1471. }
  1472. /* Detaching the disk is a process in multiple stages. First we need to lock
  1473. * out application IO, in-flight IO, IO stuck in drbd_al_begin_io.
  1474. * Then we transition to D_DISKLESS, and wait for put_ldev() to return all
  1475. * internal references as well.
  1476. * Only then we have finally detached. */
  1477. int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info)
  1478. {
  1479. enum drbd_ret_code retcode;
  1480. struct detach_parms parms = { };
  1481. int err;
  1482. retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
  1483. if (!adm_ctx.reply_skb)
  1484. return retcode;
  1485. if (retcode != NO_ERROR)
  1486. goto out;
  1487. if (info->attrs[DRBD_NLA_DETACH_PARMS]) {
  1488. err = detach_parms_from_attrs(&parms, info);
  1489. if (err) {
  1490. retcode = ERR_MANDATORY_TAG;
  1491. drbd_msg_put_info(from_attrs_err_to_txt(err));
  1492. goto out;
  1493. }
  1494. }
  1495. retcode = adm_detach(adm_ctx.mdev, parms.force_detach);
  1496. out:
  1497. drbd_adm_finish(info, retcode);
  1498. return 0;
  1499. }
  1500. static bool conn_resync_running(struct drbd_tconn *tconn)
  1501. {
  1502. struct drbd_conf *mdev;
  1503. bool rv = false;
  1504. int vnr;
  1505. rcu_read_lock();
  1506. idr_for_each_entry(&tconn->volumes, mdev, vnr) {
  1507. if (mdev->state.conn == C_SYNC_SOURCE ||
  1508. mdev->state.conn == C_SYNC_TARGET ||
  1509. mdev->state.conn == C_PAUSED_SYNC_S ||
  1510. mdev->state.conn == C_PAUSED_SYNC_T) {
  1511. rv = true;
  1512. break;
  1513. }
  1514. }
  1515. rcu_read_unlock();
  1516. return rv;
  1517. }
  1518. static bool conn_ov_running(struct drbd_tconn *tconn)
  1519. {
  1520. struct drbd_conf *mdev;
  1521. bool rv = false;
  1522. int vnr;
  1523. rcu_read_lock();
  1524. idr_for_each_entry(&tconn->volumes, mdev, vnr) {
  1525. if (mdev->state.conn == C_VERIFY_S ||
  1526. mdev->state.conn == C_VERIFY_T) {
  1527. rv = true;
  1528. break;
  1529. }
  1530. }
  1531. rcu_read_unlock();
  1532. return rv;
  1533. }
  1534. static enum drbd_ret_code
  1535. _check_net_options(struct drbd_tconn *tconn, struct net_conf *old_conf, struct net_conf *new_conf)
  1536. {
  1537. struct drbd_conf *mdev;
  1538. int i;
  1539. if (old_conf && tconn->cstate == C_WF_REPORT_PARAMS && tconn->agreed_pro_version < 100) {
  1540. if (new_conf->wire_protocol != old_conf->wire_protocol)
  1541. return ERR_NEED_APV_100;
  1542. if (new_conf->two_primaries != old_conf->two_primaries)
  1543. return ERR_NEED_APV_100;
  1544. if (strcmp(new_conf->integrity_alg, old_conf->integrity_alg))
  1545. return ERR_NEED_APV_100;
  1546. }
  1547. if (!new_conf->two_primaries &&
  1548. conn_highest_role(tconn) == R_PRIMARY &&
  1549. conn_highest_peer(tconn) == R_PRIMARY)
  1550. return ERR_NEED_ALLOW_TWO_PRI;
  1551. if (new_conf->two_primaries &&
  1552. (new_conf->wire_protocol != DRBD_PROT_C))
  1553. return ERR_NOT_PROTO_C;
  1554. idr_for_each_entry(&tconn->volumes, mdev, i) {
  1555. if (get_ldev(mdev)) {
  1556. enum drbd_fencing_p fp = rcu_dereference(mdev->ldev->disk_conf)->fencing;
  1557. put_ldev(mdev);
  1558. if (new_conf->wire_protocol == DRBD_PROT_A && fp == FP_STONITH)
  1559. return ERR_STONITH_AND_PROT_A;
  1560. }
  1561. if (mdev->state.role == R_PRIMARY && new_conf->discard_my_data)
  1562. return ERR_DISCARD_IMPOSSIBLE;
  1563. }
  1564. if (new_conf->on_congestion != OC_BLOCK && new_conf->wire_protocol != DRBD_PROT_A)
  1565. return ERR_CONG_NOT_PROTO_A;
  1566. return NO_ERROR;
  1567. }
  1568. static enum drbd_ret_code
  1569. check_net_options(struct drbd_tconn *tconn, struct net_conf *new_conf)
  1570. {
  1571. static enum drbd_ret_code rv;
  1572. struct drbd_conf *mdev;
  1573. int i;
  1574. rcu_read_lock();
  1575. rv = _check_net_options(tconn, rcu_dereference(tconn->net_conf), new_conf);
  1576. rcu_read_unlock();
  1577. /* tconn->volumes protected by genl_lock() here */
  1578. idr_for_each_entry(&tconn->volumes, mdev, i) {
  1579. if (!mdev->bitmap) {
  1580. if(drbd_bm_init(mdev))
  1581. return ERR_NOMEM;
  1582. }
  1583. }
  1584. return rv;
  1585. }
  1586. struct crypto {
  1587. struct crypto_hash *verify_tfm;
  1588. struct crypto_hash *csums_tfm;
  1589. struct crypto_hash *cram_hmac_tfm;
  1590. struct crypto_hash *integrity_tfm;
  1591. };
  1592. static int
  1593. alloc_hash(struct crypto_hash **tfm, char *tfm_name, int err_alg)
  1594. {
  1595. if (!tfm_name[0])
  1596. return NO_ERROR;
  1597. *tfm = crypto_alloc_hash(tfm_name, 0, CRYPTO_ALG_ASYNC);
  1598. if (IS_ERR(*tfm)) {
  1599. *tfm = NULL;
  1600. return err_alg;
  1601. }
  1602. return NO_ERROR;
  1603. }
  1604. static enum drbd_ret_code
  1605. alloc_crypto(struct crypto *crypto, struct net_conf *new_conf)
  1606. {
  1607. char hmac_name[CRYPTO_MAX_ALG_NAME];
  1608. enum drbd_ret_code rv;
  1609. rv = alloc_hash(&crypto->csums_tfm, new_conf->csums_alg,
  1610. ERR_CSUMS_ALG);
  1611. if (rv != NO_ERROR)
  1612. return rv;
  1613. rv = alloc_hash(&crypto->verify_tfm, new_conf->verify_alg,
  1614. ERR_VERIFY_ALG);
  1615. if (rv != NO_ERROR)
  1616. return rv;
  1617. rv = alloc_hash(&crypto->integrity_tfm, new_conf->integrity_alg,
  1618. ERR_INTEGRITY_ALG);
  1619. if (rv != NO_ERROR)
  1620. return rv;
  1621. if (new_conf->cram_hmac_alg[0] != 0) {
  1622. snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)",
  1623. new_conf->cram_hmac_alg);
  1624. rv = alloc_hash(&crypto->cram_hmac_tfm, hmac_name,
  1625. ERR_AUTH_ALG);
  1626. }
  1627. return rv;
  1628. }
  1629. static void free_crypto(struct crypto *crypto)
  1630. {
  1631. crypto_free_hash(crypto->cram_hmac_tfm);
  1632. crypto_free_hash(crypto->integrity_tfm);
  1633. crypto_free_hash(crypto->csums_tfm);
  1634. crypto_free_hash(crypto->verify_tfm);
  1635. }
  1636. int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info)
  1637. {
  1638. enum drbd_ret_code retcode;
  1639. struct drbd_tconn *tconn;
  1640. struct net_conf *old_conf, *new_conf = NULL;
  1641. int err;
  1642. int ovr; /* online verify running */
  1643. int rsr; /* re-sync running */
  1644. struct crypto crypto = { };
  1645. retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONNECTION);
  1646. if (!adm_ctx.reply_skb)
  1647. return retcode;
  1648. if (retcode != NO_ERROR)
  1649. goto out;
  1650. tconn = adm_ctx.tconn;
  1651. new_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
  1652. if (!new_conf) {
  1653. retcode = ERR_NOMEM;
  1654. goto out;
  1655. }
  1656. conn_reconfig_start(tconn);
  1657. mutex_lock(&tconn->data.mutex);
  1658. mutex_lock(&tconn->conf_update);
  1659. old_conf = tconn->net_conf;
  1660. if (!old_conf) {
  1661. drbd_msg_put_info("net conf missing, try connect");
  1662. retcode = ERR_INVALID_REQUEST;
  1663. goto fail;
  1664. }
  1665. *new_conf = *old_conf;
  1666. if (should_set_defaults(info))
  1667. set_net_conf_defaults(new_conf);
  1668. err = net_conf_from_attrs_for_change(new_conf, info);
  1669. if (err && err != -ENOMSG) {
  1670. retcode = ERR_MANDATORY_TAG;
  1671. drbd_msg_put_info(from_attrs_err_to_txt(err));
  1672. goto fail;
  1673. }
  1674. retcode = check_net_options(tconn, new_conf);
  1675. if (retcode != NO_ERROR)
  1676. goto fail;
  1677. /* re-sync running */
  1678. rsr = conn_resync_running(tconn);
  1679. if (rsr && strcmp(new_conf->csums_alg, old_conf->csums_alg)) {
  1680. retcode = ERR_CSUMS_RESYNC_RUNNING;
  1681. goto fail;
  1682. }
  1683. /* online verify running */
  1684. ovr = conn_ov_running(tconn);
  1685. if (ovr && strcmp(new_conf->verify_alg, old_conf->verify_alg)) {
  1686. retcode = ERR_VERIFY_RUNNING;
  1687. goto fail;
  1688. }
  1689. retcode = alloc_crypto(&crypto, new_conf);
  1690. if (retcode != NO_ERROR)
  1691. goto fail;
  1692. rcu_assign_pointer(tconn->net_conf, new_conf);
  1693. if (!rsr) {
  1694. crypto_free_hash(tconn->csums_tfm);
  1695. tconn->csums_tfm = crypto.csums_tfm;
  1696. crypto.csums_tfm = NULL;
  1697. }
  1698. if (!ovr) {
  1699. crypto_free_hash(tconn->verify_tfm);
  1700. tconn->verify_tfm = crypto.verify_tfm;
  1701. crypto.verify_tfm = NULL;
  1702. }
  1703. crypto_free_hash(tconn->integrity_tfm);
  1704. tconn->integrity_tfm = crypto.integrity_tfm;
  1705. if (tconn->cstate >= C_WF_REPORT_PARAMS && tconn->agreed_pro_version >= 100)
  1706. /* Do this without trying to take tconn->data.mutex again. */
  1707. __drbd_send_protocol(tconn, P_PROTOCOL_UPDATE);
  1708. crypto_free_hash(tconn->cram_hmac_tfm);
  1709. tconn->cram_hmac_tfm = crypto.cram_hmac_tfm;
  1710. mutex_unlock(&tconn->conf_update);
  1711. mutex_unlock(&tconn->data.mutex);
  1712. synchronize_rcu();
  1713. kfree(old_conf);
  1714. if (tconn->cstate >= C_WF_REPORT_PARAMS)
  1715. drbd_send_sync_param(minor_to_mdev(conn_lowest_minor(tconn)));
  1716. goto done;
  1717. fail:
  1718. mutex_unlock(&tconn->conf_update);
  1719. mutex_unlock(&tconn->data.mutex);
  1720. free_crypto(&crypto);
  1721. kfree(new_conf);
  1722. done:
  1723. conn_reconfig_done(tconn);
  1724. out:
  1725. drbd_adm_finish(info, retcode);
  1726. return 0;
  1727. }
  1728. int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info)
  1729. {
  1730. struct drbd_conf *mdev;
  1731. struct net_conf *old_conf, *new_conf = NULL;
  1732. struct crypto crypto = { };
  1733. struct drbd_tconn *tconn;
  1734. enum drbd_ret_code retcode;
  1735. int i;
  1736. int err;
  1737. retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
  1738. if (!adm_ctx.reply_skb)
  1739. return retcode;
  1740. if (retcode != NO_ERROR)
  1741. goto out;
  1742. if (!(adm_ctx.my_addr && adm_ctx.peer_addr)) {
  1743. drbd_msg_put_info("connection endpoint(s) missing");
  1744. retcode = ERR_INVALID_REQUEST;
  1745. goto out;
  1746. }
  1747. /* No need for _rcu here. All reconfiguration is
  1748. * strictly serialized on genl_lock(). We are protected against
  1749. * concurrent reconfiguration/addition/deletion */
  1750. list_for_each_entry(tconn, &drbd_tconns, all_tconn) {
  1751. if (nla_len(adm_ctx.my_addr) == tconn->my_addr_len &&
  1752. !memcmp(nla_data(adm_ctx.my_addr), &tconn->my_addr, tconn->my_addr_len)) {
  1753. retcode = ERR_LOCAL_ADDR;
  1754. goto out;
  1755. }
  1756. if (nla_len(adm_ctx.peer_addr) == tconn->peer_addr_len &&
  1757. !memcmp(nla_data(adm_ctx.peer_addr), &tconn->peer_addr, tconn->peer_addr_len)) {
  1758. retcode = ERR_PEER_ADDR;
  1759. goto out;
  1760. }
  1761. }
  1762. tconn = adm_ctx.tconn;
  1763. conn_reconfig_start(tconn);
  1764. if (tconn->cstate > C_STANDALONE) {
  1765. retcode = ERR_NET_CONFIGURED;
  1766. goto fail;
  1767. }
  1768. /* allocation not in the IO path, drbdsetup / netlink process context */
  1769. new_conf = kzalloc(sizeof(*new_conf), GFP_KERNEL);
  1770. if (!new_conf) {
  1771. retcode = ERR_NOMEM;
  1772. goto fail;
  1773. }
  1774. set_net_conf_defaults(new_conf);
  1775. err = net_conf_from_attrs(new_conf, info);
  1776. if (err && err != -ENOMSG) {
  1777. retcode = ERR_MANDATORY_TAG;
  1778. drbd_msg_put_info(from_attrs_err_to_txt(err));
  1779. goto fail;
  1780. }
  1781. retcode = check_net_options(tconn, new_conf);
  1782. if (retcode != NO_ERROR)
  1783. goto fail;
  1784. retcode = alloc_crypto(&crypto, new_conf);
  1785. if (retcode != NO_ERROR)
  1786. goto fail;
  1787. ((char *)new_conf->shared_secret)[SHARED_SECRET_MAX-1] = 0;
  1788. conn_flush_workqueue(tconn);
  1789. mutex_lock(&tconn->conf_update);
  1790. old_conf = tconn->net_conf;
  1791. if (old_conf) {
  1792. retcode = ERR_NET_CONFIGURED;
  1793. mutex_unlock(&tconn->conf_update);
  1794. goto fail;
  1795. }
  1796. rcu_assign_pointer(tconn->net_conf, new_conf);
  1797. conn_free_crypto(tconn);
  1798. tconn->cram_hmac_tfm = crypto.cram_hmac_tfm;
  1799. tconn->integrity_tfm = crypto.integrity_tfm;
  1800. tconn->csums_tfm = crypto.csums_tfm;
  1801. tconn->verify_tfm = crypto.verify_tfm;
  1802. tconn->my_addr_len = nla_len(adm_ctx.my_addr);
  1803. memcpy(&tconn->my_addr, nla_data(adm_ctx.my_addr), tconn->my_addr_len);
  1804. tconn->peer_addr_len = nla_len(adm_ctx.peer_addr);
  1805. memcpy(&tconn->peer_addr, nla_data(adm_ctx.peer_addr), tconn->peer_addr_len);
  1806. mutex_unlock(&tconn->conf_update);
  1807. rcu_read_lock();
  1808. idr_for_each_entry(&tconn->volumes, mdev, i) {
  1809. mdev->send_cnt = 0;
  1810. mdev->recv_cnt = 0;
  1811. }
  1812. rcu_read_unlock();
  1813. retcode = conn_request_state(tconn, NS(conn, C_UNCONNECTED), CS_VERBOSE);
  1814. conn_reconfig_done(tconn);
  1815. drbd_adm_finish(info, retcode);
  1816. return 0;
  1817. fail:
  1818. free_crypto(&crypto);
  1819. kfree(new_conf);
  1820. conn_reconfig_done(tconn);
  1821. out:
  1822. drbd_adm_finish(info, retcode);
  1823. return 0;
  1824. }
  1825. static enum drbd_state_rv conn_try_disconnect(struct drbd_tconn *tconn, bool force)
  1826. {
  1827. enum drbd_state_rv rv;
  1828. rv = conn_request_state(tconn, NS(conn, C_DISCONNECTING),
  1829. force ? CS_HARD : 0);
  1830. switch (rv) {
  1831. case SS_NOTHING_TO_DO:
  1832. break;
  1833. case SS_ALREADY_STANDALONE:
  1834. return SS_SUCCESS;
  1835. case SS_PRIMARY_NOP:
  1836. /* Our state checking code wants to see the peer outdated. */
  1837. rv = conn_request_state(tconn, NS2(conn, C_DISCONNECTING,
  1838. pdsk, D_OUTDATED), CS_VERBOSE);
  1839. break;
  1840. case SS_CW_FAILED_BY_PEER:
  1841. /* The peer probably wants to see us outdated. */
  1842. rv = conn_request_state(tconn, NS2(conn, C_DISCONNECTING,
  1843. disk, D_OUTDATED), 0);
  1844. if (rv == SS_IS_DISKLESS || rv == SS_LOWER_THAN_OUTDATED) {
  1845. rv = conn_request_state(tconn, NS(conn, C_DISCONNECTING),
  1846. CS_HARD);
  1847. }
  1848. break;
  1849. default:;
  1850. /* no special handling necessary */
  1851. }
  1852. if (rv >= SS_SUCCESS) {
  1853. enum drbd_state_rv rv2;
  1854. /* No one else can reconfigure the network while I am here.
  1855. * The state handling only uses drbd_thread_stop_nowait(),
  1856. * we want to really wait here until the receiver is no more.
  1857. */
  1858. drbd_thread_stop(&adm_ctx.tconn->receiver);
  1859. /* Race breaker. This additional state change request may be
  1860. * necessary, if this was a forced disconnect during a receiver
  1861. * restart. We may have "killed" the receiver thread just
  1862. * after drbdd_init() returned. Typically, we should be
  1863. * C_STANDALONE already, now, and this becomes a no-op.
  1864. */
  1865. rv2 = conn_request_state(tconn, NS(conn, C_STANDALONE),
  1866. CS_VERBOSE | CS_HARD);
  1867. if (rv2 < SS_SUCCESS)
  1868. conn_err(tconn,
  1869. "unexpected rv2=%d in conn_try_disconnect()\n",
  1870. rv2);
  1871. }
  1872. return rv;
  1873. }
  1874. int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info)
  1875. {
  1876. struct disconnect_parms parms;
  1877. struct drbd_tconn *tconn;
  1878. enum drbd_state_rv rv;
  1879. enum drbd_ret_code retcode;
  1880. int err;
  1881. retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONNECTION);
  1882. if (!adm_ctx.reply_skb)
  1883. return retcode;
  1884. if (retcode != NO_ERROR)
  1885. goto fail;
  1886. tconn = adm_ctx.tconn;
  1887. memset(&parms, 0, sizeof(parms));
  1888. if (info->attrs[DRBD_NLA_DISCONNECT_PARMS]) {
  1889. err = disconnect_parms_from_attrs(&parms, info);
  1890. if (err) {
  1891. retcode = ERR_MANDATORY_TAG;
  1892. drbd_msg_put_info(from_attrs_err_to_txt(err));
  1893. goto fail;
  1894. }
  1895. }
  1896. rv = conn_try_disconnect(tconn, parms.force_disconnect);
  1897. if (rv < SS_SUCCESS)
  1898. retcode = rv; /* FIXME: Type mismatch. */
  1899. else
  1900. retcode = NO_ERROR;
  1901. fail:
  1902. drbd_adm_finish(info, retcode);
  1903. return 0;
  1904. }
  1905. void resync_after_online_grow(struct drbd_conf *mdev)
  1906. {
  1907. int iass; /* I am sync source */
  1908. dev_info(DEV, "Resync of new storage after online grow\n");
  1909. if (mdev->state.role != mdev->state.peer)
  1910. iass = (mdev->state.role == R_PRIMARY);
  1911. else
  1912. iass = test_bit(RESOLVE_CONFLICTS, &mdev->tconn->flags);
  1913. if (iass)
  1914. drbd_start_resync(mdev, C_SYNC_SOURCE);
  1915. else
  1916. _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE + CS_SERIALIZE);
  1917. }
  1918. int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info)
  1919. {
  1920. struct disk_conf *old_disk_conf, *new_disk_conf = NULL;
  1921. struct resize_parms rs;
  1922. struct drbd_conf *mdev;
  1923. enum drbd_ret_code retcode;
  1924. enum determine_dev_size dd;
  1925. enum dds_flags ddsf;
  1926. sector_t u_size;
  1927. int err;
  1928. retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
  1929. if (!adm_ctx.reply_skb)
  1930. return retcode;
  1931. if (retcode != NO_ERROR)
  1932. goto fail;
  1933. memset(&rs, 0, sizeof(struct resize_parms));
  1934. if (info->attrs[DRBD_NLA_RESIZE_PARMS]) {
  1935. err = resize_parms_from_attrs(&rs, info);
  1936. if (err) {
  1937. retcode = ERR_MANDATORY_TAG;
  1938. drbd_msg_put_info(from_attrs_err_to_txt(err));
  1939. goto fail;
  1940. }
  1941. }
  1942. mdev = adm_ctx.mdev;
  1943. if (mdev->state.conn > C_CONNECTED) {
  1944. retcode = ERR_RESIZE_RESYNC;
  1945. goto fail;
  1946. }
  1947. if (mdev->state.role == R_SECONDARY &&
  1948. mdev->state.peer == R_SECONDARY) {
  1949. retcode = ERR_NO_PRIMARY;
  1950. goto fail;
  1951. }
  1952. if (!get_ldev(mdev)) {
  1953. retcode = ERR_NO_DISK;
  1954. goto fail;
  1955. }
  1956. if (rs.no_resync && mdev->tconn->agreed_pro_version < 93) {
  1957. retcode = ERR_NEED_APV_93;
  1958. goto fail_ldev;
  1959. }
  1960. rcu_read_lock();
  1961. u_size = rcu_dereference(mdev->ldev->disk_conf)->disk_size;
  1962. rcu_read_unlock();
  1963. if (u_size != (sector_t)rs.resize_size) {
  1964. new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
  1965. if (!new_disk_conf) {
  1966. retcode = ERR_NOMEM;
  1967. goto fail_ldev;
  1968. }
  1969. }
  1970. if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev))
  1971. mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev);
  1972. if (new_disk_conf) {
  1973. mutex_lock(&mdev->tconn->conf_update);
  1974. old_disk_conf = mdev->ldev->disk_conf;
  1975. *new_disk_conf = *old_disk_conf;
  1976. new_disk_conf->disk_size = (sector_t)rs.resize_size;
  1977. rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
  1978. mutex_unlock(&mdev->tconn->conf_update);
  1979. synchronize_rcu();
  1980. kfree(old_disk_conf);
  1981. }
  1982. ddsf = (rs.resize_force ? DDSF_FORCED : 0) | (rs.no_resync ? DDSF_NO_RESYNC : 0);
  1983. dd = drbd_determine_dev_size(mdev, ddsf);
  1984. drbd_md_sync(mdev);
  1985. put_ldev(mdev);
  1986. if (dd == dev_size_error) {
  1987. retcode = ERR_NOMEM_BITMAP;
  1988. goto fail;
  1989. }
  1990. if (mdev->state.conn == C_CONNECTED) {
  1991. if (dd == grew)
  1992. set_bit(RESIZE_PENDING, &mdev->flags);
  1993. drbd_send_uuids(mdev);
  1994. drbd_send_sizes(mdev, 1, ddsf);
  1995. }
  1996. fail:
  1997. drbd_adm_finish(info, retcode);
  1998. return 0;
  1999. fail_ldev:
  2000. put_ldev(mdev);
  2001. goto fail;
  2002. }
  2003. int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info)
  2004. {
  2005. enum drbd_ret_code retcode;
  2006. struct drbd_tconn *tconn;
  2007. struct res_opts res_opts;
  2008. int err;
  2009. retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
  2010. if (!adm_ctx.reply_skb)
  2011. return retcode;
  2012. if (retcode != NO_ERROR)
  2013. goto fail;
  2014. tconn = adm_ctx.tconn;
  2015. res_opts = tconn->res_opts;
  2016. if (should_set_defaults(info))
  2017. set_res_opts_defaults(&res_opts);
  2018. err = res_opts_from_attrs(&res_opts, info);
  2019. if (err && err != -ENOMSG) {
  2020. retcode = ERR_MANDATORY_TAG;
  2021. drbd_msg_put_info(from_attrs_err_to_txt(err));
  2022. goto fail;
  2023. }
  2024. err = set_resource_options(tconn, &res_opts);
  2025. if (err) {
  2026. retcode = ERR_INVALID_REQUEST;
  2027. if (err == -ENOMEM)
  2028. retcode = ERR_NOMEM;
  2029. }
  2030. fail:
  2031. drbd_adm_finish(info, retcode);
  2032. return 0;
  2033. }
  2034. int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info)
  2035. {
  2036. struct drbd_conf *mdev;
  2037. int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
  2038. retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
  2039. if (!adm_ctx.reply_skb)
  2040. return retcode;
  2041. if (retcode != NO_ERROR)
  2042. goto out;
  2043. mdev = adm_ctx.mdev;
  2044. /* If there is still bitmap IO pending, probably because of a previous
  2045. * resync just being finished, wait for it before requesting a new resync.
  2046. * Also wait for it's after_state_ch(). */
  2047. drbd_suspend_io(mdev);
  2048. wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
  2049. drbd_flush_workqueue(mdev);
  2050. retcode = _drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T), CS_ORDERED);
  2051. if (retcode < SS_SUCCESS && retcode != SS_NEED_CONNECTION)
  2052. retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T));
  2053. while (retcode == SS_NEED_CONNECTION) {
  2054. spin_lock_irq(&mdev->tconn->req_lock);
  2055. if (mdev->state.conn < C_CONNECTED)
  2056. retcode = _drbd_set_state(_NS(mdev, disk, D_INCONSISTENT), CS_VERBOSE, NULL);
  2057. spin_unlock_irq(&mdev->tconn->req_lock);
  2058. if (retcode != SS_NEED_CONNECTION)
  2059. break;
  2060. retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T));
  2061. }
  2062. drbd_resume_io(mdev);
  2063. out:
  2064. drbd_adm_finish(info, retcode);
  2065. return 0;
  2066. }
  2067. static int drbd_adm_simple_request_state(struct sk_buff *skb, struct genl_info *info,
  2068. union drbd_state mask, union drbd_state val)
  2069. {
  2070. enum drbd_ret_code retcode;
  2071. retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
  2072. if (!adm_ctx.reply_skb)
  2073. return retcode;
  2074. if (retcode != NO_ERROR)
  2075. goto out;
  2076. retcode = drbd_request_state(adm_ctx.mdev, mask, val);
  2077. out:
  2078. drbd_adm_finish(info, retcode);
  2079. return 0;
  2080. }
  2081. static int drbd_bmio_set_susp_al(struct drbd_conf *mdev)
  2082. {
  2083. int rv;
  2084. rv = drbd_bmio_set_n_write(mdev);
  2085. drbd_suspend_al(mdev);
  2086. return rv;
  2087. }
  2088. int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info)
  2089. {
  2090. int retcode; /* drbd_ret_code, drbd_state_rv */
  2091. struct drbd_conf *mdev;
  2092. retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
  2093. if (!adm_ctx.reply_skb)
  2094. return retcode;
  2095. if (retcode != NO_ERROR)
  2096. goto out;
  2097. mdev = adm_ctx.mdev;
  2098. /* If there is still bitmap IO pending, probably because of a previous
  2099. * resync just being finished, wait for it before requesting a new resync.
  2100. * Also wait for it's after_state_ch(). */
  2101. drbd_suspend_io(mdev);
  2102. wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
  2103. drbd_flush_workqueue(mdev);
  2104. retcode = _drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_S), CS_ORDERED);
  2105. if (retcode < SS_SUCCESS) {
  2106. if (retcode == SS_NEED_CONNECTION && mdev->state.role == R_PRIMARY) {
  2107. /* The peer will get a resync upon connect anyways.
  2108. * Just make that into a full resync. */
  2109. retcode = drbd_request_state(mdev, NS(pdsk, D_INCONSISTENT));
  2110. if (retcode >= SS_SUCCESS) {
  2111. if (drbd_bitmap_io(mdev, &drbd_bmio_set_susp_al,
  2112. "set_n_write from invalidate_peer",
  2113. BM_LOCKED_SET_ALLOWED))
  2114. retcode = ERR_IO_MD_DISK;
  2115. }
  2116. } else
  2117. retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_S));
  2118. }
  2119. drbd_resume_io(mdev);
  2120. out:
  2121. drbd_adm_finish(info, retcode);
  2122. return 0;
  2123. }
  2124. int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info)
  2125. {
  2126. enum drbd_ret_code retcode;
  2127. retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
  2128. if (!adm_ctx.reply_skb)
  2129. return retcode;
  2130. if (retcode != NO_ERROR)
  2131. goto out;
  2132. if (drbd_request_state(adm_ctx.mdev, NS(user_isp, 1)) == SS_NOTHING_TO_DO)
  2133. retcode = ERR_PAUSE_IS_SET;
  2134. out:
  2135. drbd_adm_finish(info, retcode);
  2136. return 0;
  2137. }
  2138. int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info)
  2139. {
  2140. union drbd_dev_state s;
  2141. enum drbd_ret_code retcode;
  2142. retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
  2143. if (!adm_ctx.reply_skb)
  2144. return retcode;
  2145. if (retcode != NO_ERROR)
  2146. goto out;
  2147. if (drbd_request_state(adm_ctx.mdev, NS(user_isp, 0)) == SS_NOTHING_TO_DO) {
  2148. s = adm_ctx.mdev->state;
  2149. if (s.conn == C_PAUSED_SYNC_S || s.conn == C_PAUSED_SYNC_T) {
  2150. retcode = s.aftr_isp ? ERR_PIC_AFTER_DEP :
  2151. s.peer_isp ? ERR_PIC_PEER_DEP : ERR_PAUSE_IS_CLEAR;
  2152. } else {
  2153. retcode = ERR_PAUSE_IS_CLEAR;
  2154. }
  2155. }
  2156. out:
  2157. drbd_adm_finish(info, retcode);
  2158. return 0;
  2159. }
  2160. int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info)
  2161. {
  2162. return drbd_adm_simple_request_state(skb, info, NS(susp, 1));
  2163. }
  2164. int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info)
  2165. {
  2166. struct drbd_conf *mdev;
  2167. int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
  2168. retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
  2169. if (!adm_ctx.reply_skb)
  2170. return retcode;
  2171. if (retcode != NO_ERROR)
  2172. goto out;
  2173. mdev = adm_ctx.mdev;
  2174. if (test_bit(NEW_CUR_UUID, &mdev->flags)) {
  2175. drbd_uuid_new_current(mdev);
  2176. clear_bit(NEW_CUR_UUID, &mdev->flags);
  2177. }
  2178. drbd_suspend_io(mdev);
  2179. retcode = drbd_request_state(mdev, NS3(susp, 0, susp_nod, 0, susp_fen, 0));
  2180. if (retcode == SS_SUCCESS) {
  2181. if (mdev->state.conn < C_CONNECTED)
  2182. tl_clear(mdev->tconn);
  2183. if (mdev->state.disk == D_DISKLESS || mdev->state.disk == D_FAILED)
  2184. tl_restart(mdev->tconn, FAIL_FROZEN_DISK_IO);
  2185. }
  2186. drbd_resume_io(mdev);
  2187. out:
  2188. drbd_adm_finish(info, retcode);
  2189. return 0;
  2190. }
  2191. int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info)
  2192. {
  2193. return drbd_adm_simple_request_state(skb, info, NS(disk, D_OUTDATED));
  2194. }
  2195. int nla_put_drbd_cfg_context(struct sk_buff *skb, struct drbd_tconn *tconn, unsigned vnr)
  2196. {
  2197. struct nlattr *nla;
  2198. nla = nla_nest_start(skb, DRBD_NLA_CFG_CONTEXT);
  2199. if (!nla)
  2200. goto nla_put_failure;
  2201. if (vnr != VOLUME_UNSPECIFIED &&
  2202. nla_put_u32(skb, T_ctx_volume, vnr))
  2203. goto nla_put_failure;
  2204. if (nla_put_string(skb, T_ctx_resource_name, tconn->name))
  2205. goto nla_put_failure;
  2206. if (tconn->my_addr_len &&
  2207. nla_put(skb, T_ctx_my_addr, tconn->my_addr_len, &tconn->my_addr))
  2208. goto nla_put_failure;
  2209. if (tconn->peer_addr_len &&
  2210. nla_put(skb, T_ctx_peer_addr, tconn->peer_addr_len, &tconn->peer_addr))
  2211. goto nla_put_failure;
  2212. nla_nest_end(skb, nla);
  2213. return 0;
  2214. nla_put_failure:
  2215. if (nla)
  2216. nla_nest_cancel(skb, nla);
  2217. return -EMSGSIZE;
  2218. }
  2219. int nla_put_status_info(struct sk_buff *skb, struct drbd_conf *mdev,
  2220. const struct sib_info *sib)
  2221. {
  2222. struct state_info *si = NULL; /* for sizeof(si->member); */
  2223. struct net_conf *nc;
  2224. struct nlattr *nla;
  2225. int got_ldev;
  2226. int err = 0;
  2227. int exclude_sensitive;
  2228. /* If sib != NULL, this is drbd_bcast_event, which anyone can listen
  2229. * to. So we better exclude_sensitive information.
  2230. *
  2231. * If sib == NULL, this is drbd_adm_get_status, executed synchronously
  2232. * in the context of the requesting user process. Exclude sensitive
  2233. * information, unless current has superuser.
  2234. *
  2235. * NOTE: for drbd_adm_get_status_all(), this is a netlink dump, and
  2236. * relies on the current implementation of netlink_dump(), which
  2237. * executes the dump callback successively from netlink_recvmsg(),
  2238. * always in the context of the receiving process */
  2239. exclude_sensitive = sib || !capable(CAP_SYS_ADMIN);
  2240. got_ldev = get_ldev(mdev);
  2241. /* We need to add connection name and volume number information still.
  2242. * Minor number is in drbd_genlmsghdr. */
  2243. if (nla_put_drbd_cfg_context(skb, mdev->tconn, mdev->vnr))
  2244. goto nla_put_failure;
  2245. if (res_opts_to_skb(skb, &mdev->tconn->res_opts, exclude_sensitive))
  2246. goto nla_put_failure;
  2247. rcu_read_lock();
  2248. if (got_ldev)
  2249. if (disk_conf_to_skb(skb, rcu_dereference(mdev->ldev->disk_conf), exclude_sensitive))
  2250. goto nla_put_failure;
  2251. nc = rcu_dereference(mdev->tconn->net_conf);
  2252. if (nc)
  2253. err = net_conf_to_skb(skb, nc, exclude_sensitive);
  2254. rcu_read_unlock();
  2255. if (err)
  2256. goto nla_put_failure;
  2257. nla = nla_nest_start(skb, DRBD_NLA_STATE_INFO);
  2258. if (!nla)
  2259. goto nla_put_failure;
  2260. if (nla_put_u32(skb, T_sib_reason, sib ? sib->sib_reason : SIB_GET_STATUS_REPLY) ||
  2261. nla_put_u32(skb, T_current_state, mdev->state.i) ||
  2262. nla_put_u64(skb, T_ed_uuid, mdev->ed_uuid) ||
  2263. nla_put_u64(skb, T_capacity, drbd_get_capacity(mdev->this_bdev)) ||
  2264. nla_put_u64(skb, T_send_cnt, mdev->send_cnt) ||
  2265. nla_put_u64(skb, T_recv_cnt, mdev->recv_cnt) ||
  2266. nla_put_u64(skb, T_read_cnt, mdev->read_cnt) ||
  2267. nla_put_u64(skb, T_writ_cnt, mdev->writ_cnt) ||
  2268. nla_put_u64(skb, T_al_writ_cnt, mdev->al_writ_cnt) ||
  2269. nla_put_u64(skb, T_bm_writ_cnt, mdev->bm_writ_cnt) ||
  2270. nla_put_u32(skb, T_ap_bio_cnt, atomic_read(&mdev->ap_bio_cnt)) ||
  2271. nla_put_u32(skb, T_ap_pending_cnt, atomic_read(&mdev->ap_pending_cnt)) ||
  2272. nla_put_u32(skb, T_rs_pending_cnt, atomic_read(&mdev->rs_pending_cnt)))
  2273. goto nla_put_failure;
  2274. if (got_ldev) {
  2275. int err;
  2276. spin_lock_irq(&mdev->ldev->md.uuid_lock);
  2277. err = nla_put(skb, T_uuids, sizeof(si->uuids), mdev->ldev->md.uuid);
  2278. spin_unlock_irq(&mdev->ldev->md.uuid_lock);
  2279. if (err)
  2280. goto nla_put_failure;
  2281. if (nla_put_u32(skb, T_disk_flags, mdev->ldev->md.flags) ||
  2282. nla_put_u64(skb, T_bits_total, drbd_bm_bits(mdev)) ||
  2283. nla_put_u64(skb, T_bits_oos, drbd_bm_total_weight(mdev)))
  2284. goto nla_put_failure;
  2285. if (C_SYNC_SOURCE <= mdev->state.conn &&
  2286. C_PAUSED_SYNC_T >= mdev->state.conn) {
  2287. if (nla_put_u64(skb, T_bits_rs_total, mdev->rs_total) ||
  2288. nla_put_u64(skb, T_bits_rs_failed, mdev->rs_failed))
  2289. goto nla_put_failure;
  2290. }
  2291. }
  2292. if (sib) {
  2293. switch(sib->sib_reason) {
  2294. case SIB_SYNC_PROGRESS:
  2295. case SIB_GET_STATUS_REPLY:
  2296. break;
  2297. case SIB_STATE_CHANGE:
  2298. if (nla_put_u32(skb, T_prev_state, sib->os.i) ||
  2299. nla_put_u32(skb, T_new_state, sib->ns.i))
  2300. goto nla_put_failure;
  2301. break;
  2302. case SIB_HELPER_POST:
  2303. if (nla_put_u32(skb, T_helper_exit_code,
  2304. sib->helper_exit_code))
  2305. goto nla_put_failure;
  2306. /* fall through */
  2307. case SIB_HELPER_PRE:
  2308. if (nla_put_string(skb, T_helper, sib->helper_name))
  2309. goto nla_put_failure;
  2310. break;
  2311. }
  2312. }
  2313. nla_nest_end(skb, nla);
  2314. if (0)
  2315. nla_put_failure:
  2316. err = -EMSGSIZE;
  2317. if (got_ldev)
  2318. put_ldev(mdev);
  2319. return err;
  2320. }
  2321. int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info)
  2322. {
  2323. enum drbd_ret_code retcode;
  2324. int err;
  2325. retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
  2326. if (!adm_ctx.reply_skb)
  2327. return retcode;
  2328. if (retcode != NO_ERROR)
  2329. goto out;
  2330. err = nla_put_status_info(adm_ctx.reply_skb, adm_ctx.mdev, NULL);
  2331. if (err) {
  2332. nlmsg_free(adm_ctx.reply_skb);
  2333. return err;
  2334. }
  2335. out:
  2336. drbd_adm_finish(info, retcode);
  2337. return 0;
  2338. }
  2339. int get_one_status(struct sk_buff *skb, struct netlink_callback *cb)
  2340. {
  2341. struct drbd_conf *mdev;
  2342. struct drbd_genlmsghdr *dh;
  2343. struct drbd_tconn *pos = (struct drbd_tconn*)cb->args[0];
  2344. struct drbd_tconn *tconn = NULL;
  2345. struct drbd_tconn *tmp;
  2346. unsigned volume = cb->args[1];
  2347. /* Open coded, deferred, iteration:
  2348. * list_for_each_entry_safe(tconn, tmp, &drbd_tconns, all_tconn) {
  2349. * idr_for_each_entry(&tconn->volumes, mdev, i) {
  2350. * ...
  2351. * }
  2352. * }
  2353. * where tconn is cb->args[0];
  2354. * and i is cb->args[1];
  2355. *
  2356. * cb->args[2] indicates if we shall loop over all resources,
  2357. * or just dump all volumes of a single resource.
  2358. *
  2359. * This may miss entries inserted after this dump started,
  2360. * or entries deleted before they are reached.
  2361. *
  2362. * We need to make sure the mdev won't disappear while
  2363. * we are looking at it, and revalidate our iterators
  2364. * on each iteration.
  2365. */
  2366. /* synchronize with conn_create()/conn_destroy() */
  2367. rcu_read_lock();
  2368. /* revalidate iterator position */
  2369. list_for_each_entry_rcu(tmp, &drbd_tconns, all_tconn) {
  2370. if (pos == NULL) {
  2371. /* first iteration */
  2372. pos = tmp;
  2373. tconn = pos;
  2374. break;
  2375. }
  2376. if (tmp == pos) {
  2377. tconn = pos;
  2378. break;
  2379. }
  2380. }
  2381. if (tconn) {
  2382. next_tconn:
  2383. mdev = idr_get_next(&tconn->volumes, &volume);
  2384. if (!mdev) {
  2385. /* No more volumes to dump on this tconn.
  2386. * Advance tconn iterator. */
  2387. pos = list_entry_rcu(tconn->all_tconn.next,
  2388. struct drbd_tconn, all_tconn);
  2389. /* Did we dump any volume on this tconn yet? */
  2390. if (volume != 0) {
  2391. /* If we reached the end of the list,
  2392. * or only a single resource dump was requested,
  2393. * we are done. */
  2394. if (&pos->all_tconn == &drbd_tconns || cb->args[2])
  2395. goto out;
  2396. volume = 0;
  2397. tconn = pos;
  2398. goto next_tconn;
  2399. }
  2400. }
  2401. dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
  2402. cb->nlh->nlmsg_seq, &drbd_genl_family,
  2403. NLM_F_MULTI, DRBD_ADM_GET_STATUS);
  2404. if (!dh)
  2405. goto out;
  2406. if (!mdev) {
  2407. /* This is a tconn without a single volume.
  2408. * Suprisingly enough, it may have a network
  2409. * configuration. */
  2410. struct net_conf *nc;
  2411. dh->minor = -1U;
  2412. dh->ret_code = NO_ERROR;
  2413. if (nla_put_drbd_cfg_context(skb, tconn, VOLUME_UNSPECIFIED))
  2414. goto cancel;
  2415. nc = rcu_dereference(tconn->net_conf);
  2416. if (nc && net_conf_to_skb(skb, nc, 1) != 0)
  2417. goto cancel;
  2418. goto done;
  2419. }
  2420. D_ASSERT(mdev->vnr == volume);
  2421. D_ASSERT(mdev->tconn == tconn);
  2422. dh->minor = mdev_to_minor(mdev);
  2423. dh->ret_code = NO_ERROR;
  2424. if (nla_put_status_info(skb, mdev, NULL)) {
  2425. cancel:
  2426. genlmsg_cancel(skb, dh);
  2427. goto out;
  2428. }
  2429. done:
  2430. genlmsg_end(skb, dh);
  2431. }
  2432. out:
  2433. rcu_read_unlock();
  2434. /* where to start the next iteration */
  2435. cb->args[0] = (long)pos;
  2436. cb->args[1] = (pos == tconn) ? volume + 1 : 0;
  2437. /* No more tconns/volumes/minors found results in an empty skb.
  2438. * Which will terminate the dump. */
  2439. return skb->len;
  2440. }
  2441. /*
  2442. * Request status of all resources, or of all volumes within a single resource.
  2443. *
  2444. * This is a dump, as the answer may not fit in a single reply skb otherwise.
  2445. * Which means we cannot use the family->attrbuf or other such members, because
  2446. * dump is NOT protected by the genl_lock(). During dump, we only have access
  2447. * to the incoming skb, and need to opencode "parsing" of the nlattr payload.
  2448. *
  2449. * Once things are setup properly, we call into get_one_status().
  2450. */
  2451. int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb)
  2452. {
  2453. const unsigned hdrlen = GENL_HDRLEN + GENL_MAGIC_FAMILY_HDRSZ;
  2454. struct nlattr *nla;
  2455. const char *resource_name;
  2456. struct drbd_tconn *tconn;
  2457. int maxtype;
  2458. /* Is this a followup call? */
  2459. if (cb->args[0]) {
  2460. /* ... of a single resource dump,
  2461. * and the resource iterator has been advanced already? */
  2462. if (cb->args[2] && cb->args[2] != cb->args[0])
  2463. return 0; /* DONE. */
  2464. goto dump;
  2465. }
  2466. /* First call (from netlink_dump_start). We need to figure out
  2467. * which resource(s) the user wants us to dump. */
  2468. nla = nla_find(nlmsg_attrdata(cb->nlh, hdrlen),
  2469. nlmsg_attrlen(cb->nlh, hdrlen),
  2470. DRBD_NLA_CFG_CONTEXT);
  2471. /* No explicit context given. Dump all. */
  2472. if (!nla)
  2473. goto dump;
  2474. maxtype = ARRAY_SIZE(drbd_cfg_context_nl_policy) - 1;
  2475. nla = drbd_nla_find_nested(maxtype, nla, __nla_type(T_ctx_resource_name));
  2476. if (IS_ERR(nla))
  2477. return PTR_ERR(nla);
  2478. /* context given, but no name present? */
  2479. if (!nla)
  2480. return -EINVAL;
  2481. resource_name = nla_data(nla);
  2482. tconn = conn_get_by_name(resource_name);
  2483. if (!tconn)
  2484. return -ENODEV;
  2485. kref_put(&tconn->kref, &conn_destroy); /* get_one_status() (re)validates tconn by itself */
  2486. /* prime iterators, and set "filter" mode mark:
  2487. * only dump this tconn. */
  2488. cb->args[0] = (long)tconn;
  2489. /* cb->args[1] = 0; passed in this way. */
  2490. cb->args[2] = (long)tconn;
  2491. dump:
  2492. return get_one_status(skb, cb);
  2493. }
  2494. int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info)
  2495. {
  2496. enum drbd_ret_code retcode;
  2497. struct timeout_parms tp;
  2498. int err;
  2499. retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
  2500. if (!adm_ctx.reply_skb)
  2501. return retcode;
  2502. if (retcode != NO_ERROR)
  2503. goto out;
  2504. tp.timeout_type =
  2505. adm_ctx.mdev->state.pdsk == D_OUTDATED ? UT_PEER_OUTDATED :
  2506. test_bit(USE_DEGR_WFC_T, &adm_ctx.mdev->flags) ? UT_DEGRADED :
  2507. UT_DEFAULT;
  2508. err = timeout_parms_to_priv_skb(adm_ctx.reply_skb, &tp);
  2509. if (err) {
  2510. nlmsg_free(adm_ctx.reply_skb);
  2511. return err;
  2512. }
  2513. out:
  2514. drbd_adm_finish(info, retcode);
  2515. return 0;
  2516. }
  2517. int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info)
  2518. {
  2519. struct drbd_conf *mdev;
  2520. enum drbd_ret_code retcode;
  2521. struct start_ov_parms parms;
  2522. retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
  2523. if (!adm_ctx.reply_skb)
  2524. return retcode;
  2525. if (retcode != NO_ERROR)
  2526. goto out;
  2527. mdev = adm_ctx.mdev;
  2528. /* resume from last known position, if possible */
  2529. parms.ov_start_sector = mdev->ov_start_sector;
  2530. parms.ov_stop_sector = ULLONG_MAX;
  2531. if (info->attrs[DRBD_NLA_START_OV_PARMS]) {
  2532. int err = start_ov_parms_from_attrs(&parms, info);
  2533. if (err) {
  2534. retcode = ERR_MANDATORY_TAG;
  2535. drbd_msg_put_info(from_attrs_err_to_txt(err));
  2536. goto out;
  2537. }
  2538. }
  2539. /* w_make_ov_request expects position to be aligned */
  2540. mdev->ov_start_sector = parms.ov_start_sector & ~(BM_SECT_PER_BIT-1);
  2541. mdev->ov_stop_sector = parms.ov_stop_sector;
  2542. /* If there is still bitmap IO pending, e.g. previous resync or verify
  2543. * just being finished, wait for it before requesting a new resync. */
  2544. drbd_suspend_io(mdev);
  2545. wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
  2546. retcode = drbd_request_state(mdev,NS(conn,C_VERIFY_S));
  2547. drbd_resume_io(mdev);
  2548. out:
  2549. drbd_adm_finish(info, retcode);
  2550. return 0;
  2551. }
  2552. int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info)
  2553. {
  2554. struct drbd_conf *mdev;
  2555. enum drbd_ret_code retcode;
  2556. int skip_initial_sync = 0;
  2557. int err;
  2558. struct new_c_uuid_parms args;
  2559. retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
  2560. if (!adm_ctx.reply_skb)
  2561. return retcode;
  2562. if (retcode != NO_ERROR)
  2563. goto out_nolock;
  2564. mdev = adm_ctx.mdev;
  2565. memset(&args, 0, sizeof(args));
  2566. if (info->attrs[DRBD_NLA_NEW_C_UUID_PARMS]) {
  2567. err = new_c_uuid_parms_from_attrs(&args, info);
  2568. if (err) {
  2569. retcode = ERR_MANDATORY_TAG;
  2570. drbd_msg_put_info(from_attrs_err_to_txt(err));
  2571. goto out_nolock;
  2572. }
  2573. }
  2574. mutex_lock(mdev->state_mutex); /* Protects us against serialized state changes. */
  2575. if (!get_ldev(mdev)) {
  2576. retcode = ERR_NO_DISK;
  2577. goto out;
  2578. }
  2579. /* this is "skip initial sync", assume to be clean */
  2580. if (mdev->state.conn == C_CONNECTED && mdev->tconn->agreed_pro_version >= 90 &&
  2581. mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED && args.clear_bm) {
  2582. dev_info(DEV, "Preparing to skip initial sync\n");
  2583. skip_initial_sync = 1;
  2584. } else if (mdev->state.conn != C_STANDALONE) {
  2585. retcode = ERR_CONNECTED;
  2586. goto out_dec;
  2587. }
  2588. drbd_uuid_set(mdev, UI_BITMAP, 0); /* Rotate UI_BITMAP to History 1, etc... */
  2589. drbd_uuid_new_current(mdev); /* New current, previous to UI_BITMAP */
  2590. if (args.clear_bm) {
  2591. err = drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write,
  2592. "clear_n_write from new_c_uuid", BM_LOCKED_MASK);
  2593. if (err) {
  2594. dev_err(DEV, "Writing bitmap failed with %d\n",err);
  2595. retcode = ERR_IO_MD_DISK;
  2596. }
  2597. if (skip_initial_sync) {
  2598. drbd_send_uuids_skip_initial_sync(mdev);
  2599. _drbd_uuid_set(mdev, UI_BITMAP, 0);
  2600. drbd_print_uuids(mdev, "cleared bitmap UUID");
  2601. spin_lock_irq(&mdev->tconn->req_lock);
  2602. _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
  2603. CS_VERBOSE, NULL);
  2604. spin_unlock_irq(&mdev->tconn->req_lock);
  2605. }
  2606. }
  2607. drbd_md_sync(mdev);
  2608. out_dec:
  2609. put_ldev(mdev);
  2610. out:
  2611. mutex_unlock(mdev->state_mutex);
  2612. out_nolock:
  2613. drbd_adm_finish(info, retcode);
  2614. return 0;
  2615. }
  2616. static enum drbd_ret_code
  2617. drbd_check_resource_name(const char *name)
  2618. {
  2619. if (!name || !name[0]) {
  2620. drbd_msg_put_info("resource name missing");
  2621. return ERR_MANDATORY_TAG;
  2622. }
  2623. /* if we want to use these in sysfs/configfs/debugfs some day,
  2624. * we must not allow slashes */
  2625. if (strchr(name, '/')) {
  2626. drbd_msg_put_info("invalid resource name");
  2627. return ERR_INVALID_REQUEST;
  2628. }
  2629. return NO_ERROR;
  2630. }
  2631. int drbd_adm_new_resource(struct sk_buff *skb, struct genl_info *info)
  2632. {
  2633. enum drbd_ret_code retcode;
  2634. struct res_opts res_opts;
  2635. int err;
  2636. retcode = drbd_adm_prepare(skb, info, 0);
  2637. if (!adm_ctx.reply_skb)
  2638. return retcode;
  2639. if (retcode != NO_ERROR)
  2640. goto out;
  2641. set_res_opts_defaults(&res_opts);
  2642. err = res_opts_from_attrs(&res_opts, info);
  2643. if (err && err != -ENOMSG) {
  2644. retcode = ERR_MANDATORY_TAG;
  2645. drbd_msg_put_info(from_attrs_err_to_txt(err));
  2646. goto out;
  2647. }
  2648. retcode = drbd_check_resource_name(adm_ctx.resource_name);
  2649. if (retcode != NO_ERROR)
  2650. goto out;
  2651. if (adm_ctx.tconn) {
  2652. if (info->nlhdr->nlmsg_flags & NLM_F_EXCL) {
  2653. retcode = ERR_INVALID_REQUEST;
  2654. drbd_msg_put_info("resource exists");
  2655. }
  2656. /* else: still NO_ERROR */
  2657. goto out;
  2658. }
  2659. if (!conn_create(adm_ctx.resource_name, &res_opts))
  2660. retcode = ERR_NOMEM;
  2661. out:
  2662. drbd_adm_finish(info, retcode);
  2663. return 0;
  2664. }
  2665. int drbd_adm_add_minor(struct sk_buff *skb, struct genl_info *info)
  2666. {
  2667. struct drbd_genlmsghdr *dh = info->userhdr;
  2668. enum drbd_ret_code retcode;
  2669. retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
  2670. if (!adm_ctx.reply_skb)
  2671. return retcode;
  2672. if (retcode != NO_ERROR)
  2673. goto out;
  2674. if (dh->minor > MINORMASK) {
  2675. drbd_msg_put_info("requested minor out of range");
  2676. retcode = ERR_INVALID_REQUEST;
  2677. goto out;
  2678. }
  2679. if (adm_ctx.volume > DRBD_VOLUME_MAX) {
  2680. drbd_msg_put_info("requested volume id out of range");
  2681. retcode = ERR_INVALID_REQUEST;
  2682. goto out;
  2683. }
  2684. /* drbd_adm_prepare made sure already
  2685. * that mdev->tconn and mdev->vnr match the request. */
  2686. if (adm_ctx.mdev) {
  2687. if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
  2688. retcode = ERR_MINOR_EXISTS;
  2689. /* else: still NO_ERROR */
  2690. goto out;
  2691. }
  2692. retcode = conn_new_minor(adm_ctx.tconn, dh->minor, adm_ctx.volume);
  2693. out:
  2694. drbd_adm_finish(info, retcode);
  2695. return 0;
  2696. }
  2697. static enum drbd_ret_code adm_delete_minor(struct drbd_conf *mdev)
  2698. {
  2699. if (mdev->state.disk == D_DISKLESS &&
  2700. /* no need to be mdev->state.conn == C_STANDALONE &&
  2701. * we may want to delete a minor from a live replication group.
  2702. */
  2703. mdev->state.role == R_SECONDARY) {
  2704. _drbd_request_state(mdev, NS(conn, C_WF_REPORT_PARAMS),
  2705. CS_VERBOSE + CS_WAIT_COMPLETE);
  2706. idr_remove(&mdev->tconn->volumes, mdev->vnr);
  2707. idr_remove(&minors, mdev_to_minor(mdev));
  2708. del_gendisk(mdev->vdisk);
  2709. synchronize_rcu();
  2710. kref_put(&mdev->kref, &drbd_minor_destroy);
  2711. return NO_ERROR;
  2712. } else
  2713. return ERR_MINOR_CONFIGURED;
  2714. }
  2715. int drbd_adm_delete_minor(struct sk_buff *skb, struct genl_info *info)
  2716. {
  2717. enum drbd_ret_code retcode;
  2718. retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
  2719. if (!adm_ctx.reply_skb)
  2720. return retcode;
  2721. if (retcode != NO_ERROR)
  2722. goto out;
  2723. retcode = adm_delete_minor(adm_ctx.mdev);
  2724. out:
  2725. drbd_adm_finish(info, retcode);
  2726. return 0;
  2727. }
  2728. int drbd_adm_down(struct sk_buff *skb, struct genl_info *info)
  2729. {
  2730. int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
  2731. struct drbd_conf *mdev;
  2732. unsigned i;
  2733. retcode = drbd_adm_prepare(skb, info, 0);
  2734. if (!adm_ctx.reply_skb)
  2735. return retcode;
  2736. if (retcode != NO_ERROR)
  2737. goto out;
  2738. if (!adm_ctx.tconn) {
  2739. retcode = ERR_RES_NOT_KNOWN;
  2740. goto out;
  2741. }
  2742. /* demote */
  2743. idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
  2744. retcode = drbd_set_role(mdev, R_SECONDARY, 0);
  2745. if (retcode < SS_SUCCESS) {
  2746. drbd_msg_put_info("failed to demote");
  2747. goto out;
  2748. }
  2749. }
  2750. retcode = conn_try_disconnect(adm_ctx.tconn, 0);
  2751. if (retcode < SS_SUCCESS) {
  2752. drbd_msg_put_info("failed to disconnect");
  2753. goto out;
  2754. }
  2755. /* detach */
  2756. idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
  2757. retcode = adm_detach(mdev, 0);
  2758. if (retcode < SS_SUCCESS || retcode > NO_ERROR) {
  2759. drbd_msg_put_info("failed to detach");
  2760. goto out;
  2761. }
  2762. }
  2763. /* If we reach this, all volumes (of this tconn) are Secondary,
  2764. * Disconnected, Diskless, aka Unconfigured. Make sure all threads have
  2765. * actually stopped, state handling only does drbd_thread_stop_nowait(). */
  2766. drbd_thread_stop(&adm_ctx.tconn->worker);
  2767. /* Now, nothing can fail anymore */
  2768. /* delete volumes */
  2769. idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
  2770. retcode = adm_delete_minor(mdev);
  2771. if (retcode != NO_ERROR) {
  2772. /* "can not happen" */
  2773. drbd_msg_put_info("failed to delete volume");
  2774. goto out;
  2775. }
  2776. }
  2777. /* delete connection */
  2778. if (conn_lowest_minor(adm_ctx.tconn) < 0) {
  2779. list_del_rcu(&adm_ctx.tconn->all_tconn);
  2780. synchronize_rcu();
  2781. kref_put(&adm_ctx.tconn->kref, &conn_destroy);
  2782. retcode = NO_ERROR;
  2783. } else {
  2784. /* "can not happen" */
  2785. retcode = ERR_RES_IN_USE;
  2786. drbd_msg_put_info("failed to delete connection");
  2787. }
  2788. goto out;
  2789. out:
  2790. drbd_adm_finish(info, retcode);
  2791. return 0;
  2792. }
  2793. int drbd_adm_del_resource(struct sk_buff *skb, struct genl_info *info)
  2794. {
  2795. enum drbd_ret_code retcode;
  2796. retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
  2797. if (!adm_ctx.reply_skb)
  2798. return retcode;
  2799. if (retcode != NO_ERROR)
  2800. goto out;
  2801. if (conn_lowest_minor(adm_ctx.tconn) < 0) {
  2802. list_del_rcu(&adm_ctx.tconn->all_tconn);
  2803. synchronize_rcu();
  2804. kref_put(&adm_ctx.tconn->kref, &conn_destroy);
  2805. retcode = NO_ERROR;
  2806. } else {
  2807. retcode = ERR_RES_IN_USE;
  2808. }
  2809. if (retcode == NO_ERROR)
  2810. drbd_thread_stop(&adm_ctx.tconn->worker);
  2811. out:
  2812. drbd_adm_finish(info, retcode);
  2813. return 0;
  2814. }
  2815. void drbd_bcast_event(struct drbd_conf *mdev, const struct sib_info *sib)
  2816. {
  2817. static atomic_t drbd_genl_seq = ATOMIC_INIT(2); /* two. */
  2818. struct sk_buff *msg;
  2819. struct drbd_genlmsghdr *d_out;
  2820. unsigned seq;
  2821. int err = -ENOMEM;
  2822. if (sib->sib_reason == SIB_SYNC_PROGRESS) {
  2823. if (time_after(jiffies, mdev->rs_last_bcast + HZ))
  2824. mdev->rs_last_bcast = jiffies;
  2825. else
  2826. return;
  2827. }
  2828. seq = atomic_inc_return(&drbd_genl_seq);
  2829. msg = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
  2830. if (!msg)
  2831. goto failed;
  2832. err = -EMSGSIZE;
  2833. d_out = genlmsg_put(msg, 0, seq, &drbd_genl_family, 0, DRBD_EVENT);
  2834. if (!d_out) /* cannot happen, but anyways. */
  2835. goto nla_put_failure;
  2836. d_out->minor = mdev_to_minor(mdev);
  2837. d_out->ret_code = NO_ERROR;
  2838. if (nla_put_status_info(msg, mdev, sib))
  2839. goto nla_put_failure;
  2840. genlmsg_end(msg, d_out);
  2841. err = drbd_genl_multicast_events(msg, 0);
  2842. /* msg has been consumed or freed in netlink_broadcast() */
  2843. if (err && err != -ESRCH)
  2844. goto failed;
  2845. return;
  2846. nla_put_failure:
  2847. nlmsg_free(msg);
  2848. failed:
  2849. dev_err(DEV, "Error %d while broadcasting event. "
  2850. "Event seq:%u sib_reason:%u\n",
  2851. err, seq, sib->sib_reason);
  2852. }