drbd_nl.c 86 KB

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