drbd_nl.c 84 KB

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