drbd_nl.c 84 KB

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