drbd_nl.c 86 KB

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