drbd_nl.c 90 KB

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