drbd_nl.c 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652
  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/connector.h>
  26. #include <linux/blkpg.h>
  27. #include <linux/cpumask.h>
  28. #include "drbd_int.h"
  29. #include "drbd_req.h"
  30. #include "drbd_wrappers.h"
  31. #include <asm/unaligned.h>
  32. #include <linux/drbd_tag_magic.h>
  33. #include <linux/drbd_limits.h>
  34. #include <linux/compiler.h>
  35. #include <linux/kthread.h>
  36. static unsigned short *tl_add_blob(unsigned short *, enum drbd_tags, const void *, int);
  37. static unsigned short *tl_add_str(unsigned short *, enum drbd_tags, const char *);
  38. static unsigned short *tl_add_int(unsigned short *, enum drbd_tags, const void *);
  39. /* see get_sb_bdev and bd_claim */
  40. static char *drbd_m_holder = "Hands off! this is DRBD's meta data device.";
  41. /* Generate the tag_list to struct functions */
  42. #define NL_PACKET(name, number, fields) \
  43. static int name ## _from_tags(struct drbd_conf *mdev, \
  44. unsigned short *tags, struct name *arg) __attribute__ ((unused)); \
  45. static int name ## _from_tags(struct drbd_conf *mdev, \
  46. unsigned short *tags, struct name *arg) \
  47. { \
  48. int tag; \
  49. int dlen; \
  50. \
  51. while ((tag = get_unaligned(tags++)) != TT_END) { \
  52. dlen = get_unaligned(tags++); \
  53. switch (tag_number(tag)) { \
  54. fields \
  55. default: \
  56. if (tag & T_MANDATORY) { \
  57. dev_err(DEV, "Unknown tag: %d\n", tag_number(tag)); \
  58. return 0; \
  59. } \
  60. } \
  61. tags = (unsigned short *)((char *)tags + dlen); \
  62. } \
  63. return 1; \
  64. }
  65. #define NL_INTEGER(pn, pr, member) \
  66. case pn: /* D_ASSERT( tag_type(tag) == TT_INTEGER ); */ \
  67. arg->member = get_unaligned((int *)(tags)); \
  68. break;
  69. #define NL_INT64(pn, pr, member) \
  70. case pn: /* D_ASSERT( tag_type(tag) == TT_INT64 ); */ \
  71. arg->member = get_unaligned((u64 *)(tags)); \
  72. break;
  73. #define NL_BIT(pn, pr, member) \
  74. case pn: /* D_ASSERT( tag_type(tag) == TT_BIT ); */ \
  75. arg->member = *(char *)(tags) ? 1 : 0; \
  76. break;
  77. #define NL_STRING(pn, pr, member, len) \
  78. case pn: /* D_ASSERT( tag_type(tag) == TT_STRING ); */ \
  79. if (dlen > len) { \
  80. dev_err(DEV, "arg too long: %s (%u wanted, max len: %u bytes)\n", \
  81. #member, dlen, (unsigned int)len); \
  82. return 0; \
  83. } \
  84. arg->member ## _len = dlen; \
  85. memcpy(arg->member, tags, min_t(size_t, dlen, len)); \
  86. break;
  87. #include "linux/drbd_nl.h"
  88. /* Generate the struct to tag_list functions */
  89. #define NL_PACKET(name, number, fields) \
  90. static unsigned short* \
  91. name ## _to_tags(struct drbd_conf *mdev, \
  92. struct name *arg, unsigned short *tags) __attribute__ ((unused)); \
  93. static unsigned short* \
  94. name ## _to_tags(struct drbd_conf *mdev, \
  95. struct name *arg, unsigned short *tags) \
  96. { \
  97. fields \
  98. return tags; \
  99. }
  100. #define NL_INTEGER(pn, pr, member) \
  101. put_unaligned(pn | pr | TT_INTEGER, tags++); \
  102. put_unaligned(sizeof(int), tags++); \
  103. put_unaligned(arg->member, (int *)tags); \
  104. tags = (unsigned short *)((char *)tags+sizeof(int));
  105. #define NL_INT64(pn, pr, member) \
  106. put_unaligned(pn | pr | TT_INT64, tags++); \
  107. put_unaligned(sizeof(u64), tags++); \
  108. put_unaligned(arg->member, (u64 *)tags); \
  109. tags = (unsigned short *)((char *)tags+sizeof(u64));
  110. #define NL_BIT(pn, pr, member) \
  111. put_unaligned(pn | pr | TT_BIT, tags++); \
  112. put_unaligned(sizeof(char), tags++); \
  113. *(char *)tags = arg->member; \
  114. tags = (unsigned short *)((char *)tags+sizeof(char));
  115. #define NL_STRING(pn, pr, member, len) \
  116. put_unaligned(pn | pr | TT_STRING, tags++); \
  117. put_unaligned(arg->member ## _len, tags++); \
  118. memcpy(tags, arg->member, arg->member ## _len); \
  119. tags = (unsigned short *)((char *)tags + arg->member ## _len);
  120. #include "linux/drbd_nl.h"
  121. void drbd_bcast_ev_helper(struct drbd_conf *mdev, char *helper_name);
  122. void drbd_nl_send_reply(struct cn_msg *, int);
  123. int drbd_khelper(struct drbd_conf *mdev, char *cmd)
  124. {
  125. char *envp[] = { "HOME=/",
  126. "TERM=linux",
  127. "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
  128. NULL, /* Will be set to address family */
  129. NULL, /* Will be set to address */
  130. NULL };
  131. char mb[12], af[20], ad[60], *afs;
  132. char *argv[] = {usermode_helper, cmd, mb, NULL };
  133. int ret;
  134. snprintf(mb, 12, "minor-%d", mdev_to_minor(mdev));
  135. if (get_net_conf(mdev)) {
  136. switch (((struct sockaddr *)mdev->net_conf->peer_addr)->sa_family) {
  137. case AF_INET6:
  138. afs = "ipv6";
  139. snprintf(ad, 60, "DRBD_PEER_ADDRESS=%pI6",
  140. &((struct sockaddr_in6 *)mdev->net_conf->peer_addr)->sin6_addr);
  141. break;
  142. case AF_INET:
  143. afs = "ipv4";
  144. snprintf(ad, 60, "DRBD_PEER_ADDRESS=%pI4",
  145. &((struct sockaddr_in *)mdev->net_conf->peer_addr)->sin_addr);
  146. break;
  147. default:
  148. afs = "ssocks";
  149. snprintf(ad, 60, "DRBD_PEER_ADDRESS=%pI4",
  150. &((struct sockaddr_in *)mdev->net_conf->peer_addr)->sin_addr);
  151. }
  152. snprintf(af, 20, "DRBD_PEER_AF=%s", afs);
  153. envp[3]=af;
  154. envp[4]=ad;
  155. put_net_conf(mdev);
  156. }
  157. /* The helper may take some time.
  158. * write out any unsynced meta data changes now */
  159. drbd_md_sync(mdev);
  160. dev_info(DEV, "helper command: %s %s %s\n", usermode_helper, cmd, mb);
  161. drbd_bcast_ev_helper(mdev, cmd);
  162. ret = call_usermodehelper(usermode_helper, argv, envp, 1);
  163. if (ret)
  164. dev_warn(DEV, "helper command: %s %s %s exit code %u (0x%x)\n",
  165. usermode_helper, cmd, mb,
  166. (ret >> 8) & 0xff, ret);
  167. else
  168. dev_info(DEV, "helper command: %s %s %s exit code %u (0x%x)\n",
  169. usermode_helper, cmd, mb,
  170. (ret >> 8) & 0xff, ret);
  171. if (ret < 0) /* Ignore any ERRNOs we got. */
  172. ret = 0;
  173. return ret;
  174. }
  175. enum drbd_disk_state drbd_try_outdate_peer(struct drbd_conf *mdev)
  176. {
  177. char *ex_to_string;
  178. int r;
  179. enum drbd_disk_state nps;
  180. enum drbd_fencing_p fp;
  181. D_ASSERT(mdev->state.pdsk == D_UNKNOWN);
  182. if (get_ldev_if_state(mdev, D_CONSISTENT)) {
  183. fp = mdev->ldev->dc.fencing;
  184. put_ldev(mdev);
  185. } else {
  186. dev_warn(DEV, "Not fencing peer, I'm not even Consistent myself.\n");
  187. nps = mdev->state.pdsk;
  188. goto out;
  189. }
  190. r = drbd_khelper(mdev, "fence-peer");
  191. switch ((r>>8) & 0xff) {
  192. case 3: /* peer is inconsistent */
  193. ex_to_string = "peer is inconsistent or worse";
  194. nps = D_INCONSISTENT;
  195. break;
  196. case 4: /* peer got outdated, or was already outdated */
  197. ex_to_string = "peer was fenced";
  198. nps = D_OUTDATED;
  199. break;
  200. case 5: /* peer was down */
  201. if (mdev->state.disk == D_UP_TO_DATE) {
  202. /* we will(have) create(d) a new UUID anyways... */
  203. ex_to_string = "peer is unreachable, assumed to be dead";
  204. nps = D_OUTDATED;
  205. } else {
  206. ex_to_string = "peer unreachable, doing nothing since disk != UpToDate";
  207. nps = mdev->state.pdsk;
  208. }
  209. break;
  210. case 6: /* Peer is primary, voluntarily outdate myself.
  211. * This is useful when an unconnected R_SECONDARY is asked to
  212. * become R_PRIMARY, but finds the other peer being active. */
  213. ex_to_string = "peer is active";
  214. dev_warn(DEV, "Peer is primary, outdating myself.\n");
  215. nps = D_UNKNOWN;
  216. _drbd_request_state(mdev, NS(disk, D_OUTDATED), CS_WAIT_COMPLETE);
  217. break;
  218. case 7:
  219. if (fp != FP_STONITH)
  220. dev_err(DEV, "fence-peer() = 7 && fencing != Stonith !!!\n");
  221. ex_to_string = "peer was stonithed";
  222. nps = D_OUTDATED;
  223. break;
  224. default:
  225. /* The script is broken ... */
  226. nps = D_UNKNOWN;
  227. dev_err(DEV, "fence-peer helper broken, returned %d\n", (r>>8)&0xff);
  228. return nps;
  229. }
  230. dev_info(DEV, "fence-peer helper returned %d (%s)\n",
  231. (r>>8) & 0xff, ex_to_string);
  232. out:
  233. if (mdev->state.susp_fen && nps >= D_UNKNOWN) {
  234. /* The handler was not successful... unfreeze here, the
  235. state engine can not unfreeze... */
  236. _drbd_request_state(mdev, NS(susp_fen, 0), CS_VERBOSE);
  237. }
  238. return nps;
  239. }
  240. static int _try_outdate_peer_async(void *data)
  241. {
  242. struct drbd_conf *mdev = (struct drbd_conf *)data;
  243. enum drbd_disk_state nps;
  244. union drbd_state ns;
  245. nps = drbd_try_outdate_peer(mdev);
  246. /* Not using
  247. drbd_request_state(mdev, NS(pdsk, nps));
  248. here, because we might were able to re-establish the connection
  249. in the meantime. This can only partially be solved in the state's
  250. engine is_valid_state() and is_valid_state_transition()
  251. functions.
  252. nps can be D_INCONSISTENT, D_OUTDATED or D_UNKNOWN.
  253. pdsk == D_INCONSISTENT while conn >= C_CONNECTED is valid,
  254. therefore we have to have the pre state change check here.
  255. */
  256. spin_lock_irq(&mdev->req_lock);
  257. ns = mdev->state;
  258. if (ns.conn < C_WF_REPORT_PARAMS) {
  259. ns.pdsk = nps;
  260. _drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
  261. }
  262. spin_unlock_irq(&mdev->req_lock);
  263. return 0;
  264. }
  265. void drbd_try_outdate_peer_async(struct drbd_conf *mdev)
  266. {
  267. struct task_struct *opa;
  268. opa = kthread_run(_try_outdate_peer_async, mdev, "drbd%d_a_helper", mdev_to_minor(mdev));
  269. if (IS_ERR(opa))
  270. dev_err(DEV, "out of mem, failed to invoke fence-peer helper\n");
  271. }
  272. enum drbd_state_rv
  273. drbd_set_role(struct drbd_conf *mdev, enum drbd_role new_role, int force)
  274. {
  275. const int max_tries = 4;
  276. enum drbd_state_rv rv = SS_UNKNOWN_ERROR;
  277. int try = 0;
  278. int forced = 0;
  279. union drbd_state mask, val;
  280. enum drbd_disk_state nps;
  281. if (new_role == R_PRIMARY)
  282. request_ping(mdev); /* Detect a dead peer ASAP */
  283. mutex_lock(&mdev->state_mutex);
  284. mask.i = 0; mask.role = R_MASK;
  285. val.i = 0; val.role = new_role;
  286. while (try++ < max_tries) {
  287. rv = _drbd_request_state(mdev, mask, val, CS_WAIT_COMPLETE);
  288. /* in case we first succeeded to outdate,
  289. * but now suddenly could establish a connection */
  290. if (rv == SS_CW_FAILED_BY_PEER && mask.pdsk != 0) {
  291. val.pdsk = 0;
  292. mask.pdsk = 0;
  293. continue;
  294. }
  295. if (rv == SS_NO_UP_TO_DATE_DISK && force &&
  296. (mdev->state.disk < D_UP_TO_DATE &&
  297. mdev->state.disk >= D_INCONSISTENT)) {
  298. mask.disk = D_MASK;
  299. val.disk = D_UP_TO_DATE;
  300. forced = 1;
  301. continue;
  302. }
  303. if (rv == SS_NO_UP_TO_DATE_DISK &&
  304. mdev->state.disk == D_CONSISTENT && mask.pdsk == 0) {
  305. D_ASSERT(mdev->state.pdsk == D_UNKNOWN);
  306. nps = drbd_try_outdate_peer(mdev);
  307. if (nps == D_OUTDATED || nps == D_INCONSISTENT) {
  308. val.disk = D_UP_TO_DATE;
  309. mask.disk = D_MASK;
  310. }
  311. val.pdsk = nps;
  312. mask.pdsk = D_MASK;
  313. continue;
  314. }
  315. if (rv == SS_NOTHING_TO_DO)
  316. goto fail;
  317. if (rv == SS_PRIMARY_NOP && mask.pdsk == 0) {
  318. nps = drbd_try_outdate_peer(mdev);
  319. if (force && nps > D_OUTDATED) {
  320. dev_warn(DEV, "Forced into split brain situation!\n");
  321. nps = D_OUTDATED;
  322. }
  323. mask.pdsk = D_MASK;
  324. val.pdsk = nps;
  325. continue;
  326. }
  327. if (rv == SS_TWO_PRIMARIES) {
  328. /* Maybe the peer is detected as dead very soon...
  329. retry at most once more in this case. */
  330. schedule_timeout_interruptible((mdev->net_conf->ping_timeo+1)*HZ/10);
  331. if (try < max_tries)
  332. try = max_tries - 1;
  333. continue;
  334. }
  335. if (rv < SS_SUCCESS) {
  336. rv = _drbd_request_state(mdev, mask, val,
  337. CS_VERBOSE + CS_WAIT_COMPLETE);
  338. if (rv < SS_SUCCESS)
  339. goto fail;
  340. }
  341. break;
  342. }
  343. if (rv < SS_SUCCESS)
  344. goto fail;
  345. if (forced)
  346. dev_warn(DEV, "Forced to consider local data as UpToDate!\n");
  347. /* Wait until nothing is on the fly :) */
  348. wait_event(mdev->misc_wait, atomic_read(&mdev->ap_pending_cnt) == 0);
  349. if (new_role == R_SECONDARY) {
  350. set_disk_ro(mdev->vdisk, true);
  351. if (get_ldev(mdev)) {
  352. mdev->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
  353. put_ldev(mdev);
  354. }
  355. } else {
  356. if (get_net_conf(mdev)) {
  357. mdev->net_conf->want_lose = 0;
  358. put_net_conf(mdev);
  359. }
  360. set_disk_ro(mdev->vdisk, false);
  361. if (get_ldev(mdev)) {
  362. if (((mdev->state.conn < C_CONNECTED ||
  363. mdev->state.pdsk <= D_FAILED)
  364. && mdev->ldev->md.uuid[UI_BITMAP] == 0) || forced)
  365. drbd_uuid_new_current(mdev);
  366. mdev->ldev->md.uuid[UI_CURRENT] |= (u64)1;
  367. put_ldev(mdev);
  368. }
  369. }
  370. /* writeout of activity log covered areas of the bitmap
  371. * to stable storage done in after state change already */
  372. if (mdev->state.conn >= C_WF_REPORT_PARAMS) {
  373. /* if this was forced, we should consider sync */
  374. if (forced)
  375. drbd_send_uuids(mdev);
  376. drbd_send_state(mdev);
  377. }
  378. drbd_md_sync(mdev);
  379. kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
  380. fail:
  381. mutex_unlock(&mdev->state_mutex);
  382. return rv;
  383. }
  384. static struct drbd_conf *ensure_mdev(int minor, int create)
  385. {
  386. struct drbd_conf *mdev;
  387. if (minor >= minor_count)
  388. return NULL;
  389. mdev = minor_to_mdev(minor);
  390. if (!mdev && create) {
  391. struct gendisk *disk = NULL;
  392. mdev = drbd_new_device(minor);
  393. spin_lock_irq(&drbd_pp_lock);
  394. if (minor_table[minor] == NULL) {
  395. minor_table[minor] = mdev;
  396. disk = mdev->vdisk;
  397. mdev = NULL;
  398. } /* else: we lost the race */
  399. spin_unlock_irq(&drbd_pp_lock);
  400. if (disk) /* we won the race above */
  401. /* in case we ever add a drbd_delete_device(),
  402. * don't forget the del_gendisk! */
  403. add_disk(disk);
  404. else /* we lost the race above */
  405. drbd_free_mdev(mdev);
  406. mdev = minor_to_mdev(minor);
  407. }
  408. return mdev;
  409. }
  410. static int drbd_nl_primary(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
  411. struct drbd_nl_cfg_reply *reply)
  412. {
  413. struct primary primary_args;
  414. memset(&primary_args, 0, sizeof(struct primary));
  415. if (!primary_from_tags(mdev, nlp->tag_list, &primary_args)) {
  416. reply->ret_code = ERR_MANDATORY_TAG;
  417. return 0;
  418. }
  419. reply->ret_code =
  420. drbd_set_role(mdev, R_PRIMARY, primary_args.primary_force);
  421. return 0;
  422. }
  423. static int drbd_nl_secondary(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
  424. struct drbd_nl_cfg_reply *reply)
  425. {
  426. reply->ret_code = drbd_set_role(mdev, R_SECONDARY, 0);
  427. return 0;
  428. }
  429. /* initializes the md.*_offset members, so we are able to find
  430. * the on disk meta data */
  431. static void drbd_md_set_sector_offsets(struct drbd_conf *mdev,
  432. struct drbd_backing_dev *bdev)
  433. {
  434. sector_t md_size_sect = 0;
  435. switch (bdev->dc.meta_dev_idx) {
  436. default:
  437. /* v07 style fixed size indexed meta data */
  438. bdev->md.md_size_sect = MD_RESERVED_SECT;
  439. bdev->md.md_offset = drbd_md_ss__(mdev, bdev);
  440. bdev->md.al_offset = MD_AL_OFFSET;
  441. bdev->md.bm_offset = MD_BM_OFFSET;
  442. break;
  443. case DRBD_MD_INDEX_FLEX_EXT:
  444. /* just occupy the full device; unit: sectors */
  445. bdev->md.md_size_sect = drbd_get_capacity(bdev->md_bdev);
  446. bdev->md.md_offset = 0;
  447. bdev->md.al_offset = MD_AL_OFFSET;
  448. bdev->md.bm_offset = MD_BM_OFFSET;
  449. break;
  450. case DRBD_MD_INDEX_INTERNAL:
  451. case DRBD_MD_INDEX_FLEX_INT:
  452. bdev->md.md_offset = drbd_md_ss__(mdev, bdev);
  453. /* al size is still fixed */
  454. bdev->md.al_offset = -MD_AL_MAX_SIZE;
  455. /* we need (slightly less than) ~ this much bitmap sectors: */
  456. md_size_sect = drbd_get_capacity(bdev->backing_bdev);
  457. md_size_sect = ALIGN(md_size_sect, BM_SECT_PER_EXT);
  458. md_size_sect = BM_SECT_TO_EXT(md_size_sect);
  459. md_size_sect = ALIGN(md_size_sect, 8);
  460. /* plus the "drbd meta data super block",
  461. * and the activity log; */
  462. md_size_sect += MD_BM_OFFSET;
  463. bdev->md.md_size_sect = md_size_sect;
  464. /* bitmap offset is adjusted by 'super' block size */
  465. bdev->md.bm_offset = -md_size_sect + MD_AL_OFFSET;
  466. break;
  467. }
  468. }
  469. /* input size is expected to be in KB */
  470. char *ppsize(char *buf, unsigned long long size)
  471. {
  472. /* Needs 9 bytes at max including trailing NUL:
  473. * -1ULL ==> "16384 EB" */
  474. static char units[] = { 'K', 'M', 'G', 'T', 'P', 'E' };
  475. int base = 0;
  476. while (size >= 10000 && base < sizeof(units)-1) {
  477. /* shift + round */
  478. size = (size >> 10) + !!(size & (1<<9));
  479. base++;
  480. }
  481. sprintf(buf, "%u %cB", (unsigned)size, units[base]);
  482. return buf;
  483. }
  484. /* there is still a theoretical deadlock when called from receiver
  485. * on an D_INCONSISTENT R_PRIMARY:
  486. * remote READ does inc_ap_bio, receiver would need to receive answer
  487. * packet from remote to dec_ap_bio again.
  488. * receiver receive_sizes(), comes here,
  489. * waits for ap_bio_cnt == 0. -> deadlock.
  490. * but this cannot happen, actually, because:
  491. * R_PRIMARY D_INCONSISTENT, and peer's disk is unreachable
  492. * (not connected, or bad/no disk on peer):
  493. * see drbd_fail_request_early, ap_bio_cnt is zero.
  494. * R_PRIMARY D_INCONSISTENT, and C_SYNC_TARGET:
  495. * peer may not initiate a resize.
  496. */
  497. void drbd_suspend_io(struct drbd_conf *mdev)
  498. {
  499. set_bit(SUSPEND_IO, &mdev->flags);
  500. if (is_susp(mdev->state))
  501. return;
  502. wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_bio_cnt));
  503. }
  504. void drbd_resume_io(struct drbd_conf *mdev)
  505. {
  506. clear_bit(SUSPEND_IO, &mdev->flags);
  507. wake_up(&mdev->misc_wait);
  508. }
  509. /**
  510. * drbd_determine_dev_size() - Sets the right device size obeying all constraints
  511. * @mdev: DRBD device.
  512. *
  513. * Returns 0 on success, negative return values indicate errors.
  514. * You should call drbd_md_sync() after calling this function.
  515. */
  516. enum determine_dev_size drbd_determine_dev_size(struct drbd_conf *mdev, enum dds_flags flags) __must_hold(local)
  517. {
  518. sector_t prev_first_sect, prev_size; /* previous meta location */
  519. sector_t la_size;
  520. sector_t size;
  521. char ppb[10];
  522. int md_moved, la_size_changed;
  523. enum determine_dev_size rv = unchanged;
  524. /* race:
  525. * application request passes inc_ap_bio,
  526. * but then cannot get an AL-reference.
  527. * this function later may wait on ap_bio_cnt == 0. -> deadlock.
  528. *
  529. * to avoid that:
  530. * Suspend IO right here.
  531. * still lock the act_log to not trigger ASSERTs there.
  532. */
  533. drbd_suspend_io(mdev);
  534. /* no wait necessary anymore, actually we could assert that */
  535. wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
  536. prev_first_sect = drbd_md_first_sector(mdev->ldev);
  537. prev_size = mdev->ldev->md.md_size_sect;
  538. la_size = mdev->ldev->md.la_size_sect;
  539. /* TODO: should only be some assert here, not (re)init... */
  540. drbd_md_set_sector_offsets(mdev, mdev->ldev);
  541. size = drbd_new_dev_size(mdev, mdev->ldev, flags & DDSF_FORCED);
  542. if (drbd_get_capacity(mdev->this_bdev) != size ||
  543. drbd_bm_capacity(mdev) != size) {
  544. int err;
  545. err = drbd_bm_resize(mdev, size, !(flags & DDSF_NO_RESYNC));
  546. if (unlikely(err)) {
  547. /* currently there is only one error: ENOMEM! */
  548. size = drbd_bm_capacity(mdev)>>1;
  549. if (size == 0) {
  550. dev_err(DEV, "OUT OF MEMORY! "
  551. "Could not allocate bitmap!\n");
  552. } else {
  553. dev_err(DEV, "BM resizing failed. "
  554. "Leaving size unchanged at size = %lu KB\n",
  555. (unsigned long)size);
  556. }
  557. rv = dev_size_error;
  558. }
  559. /* racy, see comments above. */
  560. drbd_set_my_capacity(mdev, size);
  561. mdev->ldev->md.la_size_sect = size;
  562. dev_info(DEV, "size = %s (%llu KB)\n", ppsize(ppb, size>>1),
  563. (unsigned long long)size>>1);
  564. }
  565. if (rv == dev_size_error)
  566. goto out;
  567. la_size_changed = (la_size != mdev->ldev->md.la_size_sect);
  568. md_moved = prev_first_sect != drbd_md_first_sector(mdev->ldev)
  569. || prev_size != mdev->ldev->md.md_size_sect;
  570. if (la_size_changed || md_moved) {
  571. int err;
  572. drbd_al_shrink(mdev); /* All extents inactive. */
  573. dev_info(DEV, "Writing the whole bitmap, %s\n",
  574. la_size_changed && md_moved ? "size changed and md moved" :
  575. la_size_changed ? "size changed" : "md moved");
  576. /* next line implicitly does drbd_suspend_io()+drbd_resume_io() */
  577. err = drbd_bitmap_io(mdev, &drbd_bm_write,
  578. "size changed", BM_LOCKED_MASK);
  579. if (err) {
  580. rv = dev_size_error;
  581. goto out;
  582. }
  583. drbd_md_mark_dirty(mdev);
  584. }
  585. if (size > la_size)
  586. rv = grew;
  587. if (size < la_size)
  588. rv = shrunk;
  589. out:
  590. lc_unlock(mdev->act_log);
  591. wake_up(&mdev->al_wait);
  592. drbd_resume_io(mdev);
  593. return rv;
  594. }
  595. sector_t
  596. drbd_new_dev_size(struct drbd_conf *mdev, struct drbd_backing_dev *bdev, int assume_peer_has_space)
  597. {
  598. sector_t p_size = mdev->p_size; /* partner's disk size. */
  599. sector_t la_size = bdev->md.la_size_sect; /* last agreed size. */
  600. sector_t m_size; /* my size */
  601. sector_t u_size = bdev->dc.disk_size; /* size requested by user. */
  602. sector_t size = 0;
  603. m_size = drbd_get_max_capacity(bdev);
  604. if (mdev->state.conn < C_CONNECTED && assume_peer_has_space) {
  605. dev_warn(DEV, "Resize while not connected was forced by the user!\n");
  606. p_size = m_size;
  607. }
  608. if (p_size && m_size) {
  609. size = min_t(sector_t, p_size, m_size);
  610. } else {
  611. if (la_size) {
  612. size = la_size;
  613. if (m_size && m_size < size)
  614. size = m_size;
  615. if (p_size && p_size < size)
  616. size = p_size;
  617. } else {
  618. if (m_size)
  619. size = m_size;
  620. if (p_size)
  621. size = p_size;
  622. }
  623. }
  624. if (size == 0)
  625. dev_err(DEV, "Both nodes diskless!\n");
  626. if (u_size) {
  627. if (u_size > size)
  628. dev_err(DEV, "Requested disk size is too big (%lu > %lu)\n",
  629. (unsigned long)u_size>>1, (unsigned long)size>>1);
  630. else
  631. size = u_size;
  632. }
  633. return size;
  634. }
  635. /**
  636. * drbd_check_al_size() - Ensures that the AL is of the right size
  637. * @mdev: DRBD device.
  638. *
  639. * Returns -EBUSY if current al lru is still used, -ENOMEM when allocation
  640. * failed, and 0 on success. You should call drbd_md_sync() after you called
  641. * this function.
  642. */
  643. static int drbd_check_al_size(struct drbd_conf *mdev)
  644. {
  645. struct lru_cache *n, *t;
  646. struct lc_element *e;
  647. unsigned int in_use;
  648. int i;
  649. ERR_IF(mdev->sync_conf.al_extents < 7)
  650. mdev->sync_conf.al_extents = 127;
  651. if (mdev->act_log &&
  652. mdev->act_log->nr_elements == mdev->sync_conf.al_extents)
  653. return 0;
  654. in_use = 0;
  655. t = mdev->act_log;
  656. n = lc_create("act_log", drbd_al_ext_cache,
  657. mdev->sync_conf.al_extents, sizeof(struct lc_element), 0);
  658. if (n == NULL) {
  659. dev_err(DEV, "Cannot allocate act_log lru!\n");
  660. return -ENOMEM;
  661. }
  662. spin_lock_irq(&mdev->al_lock);
  663. if (t) {
  664. for (i = 0; i < t->nr_elements; i++) {
  665. e = lc_element_by_index(t, i);
  666. if (e->refcnt)
  667. dev_err(DEV, "refcnt(%d)==%d\n",
  668. e->lc_number, e->refcnt);
  669. in_use += e->refcnt;
  670. }
  671. }
  672. if (!in_use)
  673. mdev->act_log = n;
  674. spin_unlock_irq(&mdev->al_lock);
  675. if (in_use) {
  676. dev_err(DEV, "Activity log still in use!\n");
  677. lc_destroy(n);
  678. return -EBUSY;
  679. } else {
  680. if (t)
  681. lc_destroy(t);
  682. }
  683. drbd_md_mark_dirty(mdev); /* we changed mdev->act_log->nr_elemens */
  684. return 0;
  685. }
  686. static void drbd_setup_queue_param(struct drbd_conf *mdev, unsigned int max_bio_size)
  687. {
  688. struct request_queue * const q = mdev->rq_queue;
  689. int max_hw_sectors = max_bio_size >> 9;
  690. int max_segments = 0;
  691. if (get_ldev_if_state(mdev, D_ATTACHING)) {
  692. struct request_queue * const b = mdev->ldev->backing_bdev->bd_disk->queue;
  693. max_hw_sectors = min(queue_max_hw_sectors(b), max_bio_size >> 9);
  694. max_segments = mdev->ldev->dc.max_bio_bvecs;
  695. put_ldev(mdev);
  696. }
  697. blk_queue_logical_block_size(q, 512);
  698. blk_queue_max_hw_sectors(q, max_hw_sectors);
  699. /* This is the workaround for "bio would need to, but cannot, be split" */
  700. blk_queue_max_segments(q, max_segments ? max_segments : BLK_MAX_SEGMENTS);
  701. blk_queue_segment_boundary(q, PAGE_CACHE_SIZE-1);
  702. if (get_ldev_if_state(mdev, D_ATTACHING)) {
  703. struct request_queue * const b = mdev->ldev->backing_bdev->bd_disk->queue;
  704. blk_queue_stack_limits(q, b);
  705. if (q->backing_dev_info.ra_pages != b->backing_dev_info.ra_pages) {
  706. dev_info(DEV, "Adjusting my ra_pages to backing device's (%lu -> %lu)\n",
  707. q->backing_dev_info.ra_pages,
  708. b->backing_dev_info.ra_pages);
  709. q->backing_dev_info.ra_pages = b->backing_dev_info.ra_pages;
  710. }
  711. put_ldev(mdev);
  712. }
  713. }
  714. void drbd_reconsider_max_bio_size(struct drbd_conf *mdev)
  715. {
  716. int now, new, local, peer;
  717. now = queue_max_hw_sectors(mdev->rq_queue) << 9;
  718. local = mdev->local_max_bio_size; /* Eventually last known value, from volatile memory */
  719. peer = mdev->peer_max_bio_size; /* Eventually last known value, from meta data */
  720. if (get_ldev_if_state(mdev, D_ATTACHING)) {
  721. local = queue_max_hw_sectors(mdev->ldev->backing_bdev->bd_disk->queue) << 9;
  722. mdev->local_max_bio_size = local;
  723. put_ldev(mdev);
  724. }
  725. /* We may ignore peer limits if the peer is modern enough.
  726. Because new from 8.3.8 onwards the peer can use multiple
  727. BIOs for a single peer_request */
  728. if (mdev->state.conn >= C_CONNECTED) {
  729. if (mdev->agreed_pro_version < 94)
  730. peer = mdev->peer_max_bio_size;
  731. else if (mdev->agreed_pro_version == 94)
  732. peer = DRBD_MAX_SIZE_H80_PACKET;
  733. else /* drbd 8.3.8 onwards */
  734. peer = DRBD_MAX_BIO_SIZE;
  735. }
  736. new = min_t(int, local, peer);
  737. if (mdev->state.role == R_PRIMARY && new < now)
  738. dev_err(DEV, "ASSERT FAILED new < now; (%d < %d)\n", new, now);
  739. if (new != now)
  740. dev_info(DEV, "max BIO size = %u\n", new);
  741. drbd_setup_queue_param(mdev, new);
  742. }
  743. /* serialize deconfig (worker exiting, doing cleanup)
  744. * and reconfig (drbdsetup disk, drbdsetup net)
  745. *
  746. * Wait for a potentially exiting worker, then restart it,
  747. * or start a new one. Flush any pending work, there may still be an
  748. * after_state_change queued.
  749. */
  750. static void drbd_reconfig_start(struct drbd_conf *mdev)
  751. {
  752. wait_event(mdev->state_wait, !test_and_set_bit(CONFIG_PENDING, &mdev->flags));
  753. wait_event(mdev->state_wait, !test_bit(DEVICE_DYING, &mdev->flags));
  754. drbd_thread_start(&mdev->worker);
  755. drbd_flush_workqueue(mdev);
  756. }
  757. /* if still unconfigured, stops worker again.
  758. * if configured now, clears CONFIG_PENDING.
  759. * wakes potential waiters */
  760. static void drbd_reconfig_done(struct drbd_conf *mdev)
  761. {
  762. spin_lock_irq(&mdev->req_lock);
  763. if (mdev->state.disk == D_DISKLESS &&
  764. mdev->state.conn == C_STANDALONE &&
  765. mdev->state.role == R_SECONDARY) {
  766. set_bit(DEVICE_DYING, &mdev->flags);
  767. drbd_thread_stop_nowait(&mdev->worker);
  768. } else
  769. clear_bit(CONFIG_PENDING, &mdev->flags);
  770. spin_unlock_irq(&mdev->req_lock);
  771. wake_up(&mdev->state_wait);
  772. }
  773. /* Make sure IO is suspended before calling this function(). */
  774. static void drbd_suspend_al(struct drbd_conf *mdev)
  775. {
  776. int s = 0;
  777. if (lc_try_lock(mdev->act_log)) {
  778. drbd_al_shrink(mdev);
  779. lc_unlock(mdev->act_log);
  780. } else {
  781. dev_warn(DEV, "Failed to lock al in drbd_suspend_al()\n");
  782. return;
  783. }
  784. spin_lock_irq(&mdev->req_lock);
  785. if (mdev->state.conn < C_CONNECTED)
  786. s = !test_and_set_bit(AL_SUSPENDED, &mdev->flags);
  787. spin_unlock_irq(&mdev->req_lock);
  788. if (s)
  789. dev_info(DEV, "Suspended AL updates\n");
  790. }
  791. /* does always return 0;
  792. * interesting return code is in reply->ret_code */
  793. static int drbd_nl_disk_conf(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
  794. struct drbd_nl_cfg_reply *reply)
  795. {
  796. enum drbd_ret_code retcode;
  797. enum determine_dev_size dd;
  798. sector_t max_possible_sectors;
  799. sector_t min_md_device_sectors;
  800. struct drbd_backing_dev *nbc = NULL; /* new_backing_conf */
  801. struct block_device *bdev;
  802. struct lru_cache *resync_lru = NULL;
  803. union drbd_state ns, os;
  804. enum drbd_state_rv rv;
  805. int cp_discovered = 0;
  806. int logical_block_size;
  807. drbd_reconfig_start(mdev);
  808. /* if you want to reconfigure, please tear down first */
  809. if (mdev->state.disk > D_DISKLESS) {
  810. retcode = ERR_DISK_CONFIGURED;
  811. goto fail;
  812. }
  813. /* It may just now have detached because of IO error. Make sure
  814. * drbd_ldev_destroy is done already, we may end up here very fast,
  815. * e.g. if someone calls attach from the on-io-error handler,
  816. * to realize a "hot spare" feature (not that I'd recommend that) */
  817. wait_event(mdev->misc_wait, !atomic_read(&mdev->local_cnt));
  818. /* allocation not in the IO path, cqueue thread context */
  819. nbc = kzalloc(sizeof(struct drbd_backing_dev), GFP_KERNEL);
  820. if (!nbc) {
  821. retcode = ERR_NOMEM;
  822. goto fail;
  823. }
  824. nbc->dc.disk_size = DRBD_DISK_SIZE_SECT_DEF;
  825. nbc->dc.on_io_error = DRBD_ON_IO_ERROR_DEF;
  826. nbc->dc.fencing = DRBD_FENCING_DEF;
  827. nbc->dc.max_bio_bvecs = DRBD_MAX_BIO_BVECS_DEF;
  828. if (!disk_conf_from_tags(mdev, nlp->tag_list, &nbc->dc)) {
  829. retcode = ERR_MANDATORY_TAG;
  830. goto fail;
  831. }
  832. if (nbc->dc.meta_dev_idx < DRBD_MD_INDEX_FLEX_INT) {
  833. retcode = ERR_MD_IDX_INVALID;
  834. goto fail;
  835. }
  836. if (get_net_conf(mdev)) {
  837. int prot = mdev->net_conf->wire_protocol;
  838. put_net_conf(mdev);
  839. if (nbc->dc.fencing == FP_STONITH && prot == DRBD_PROT_A) {
  840. retcode = ERR_STONITH_AND_PROT_A;
  841. goto fail;
  842. }
  843. }
  844. bdev = blkdev_get_by_path(nbc->dc.backing_dev,
  845. FMODE_READ | FMODE_WRITE | FMODE_EXCL, mdev);
  846. if (IS_ERR(bdev)) {
  847. dev_err(DEV, "open(\"%s\") failed with %ld\n", nbc->dc.backing_dev,
  848. PTR_ERR(bdev));
  849. retcode = ERR_OPEN_DISK;
  850. goto fail;
  851. }
  852. nbc->backing_bdev = bdev;
  853. /*
  854. * meta_dev_idx >= 0: external fixed size, possibly multiple
  855. * drbd sharing one meta device. TODO in that case, paranoia
  856. * check that [md_bdev, meta_dev_idx] is not yet used by some
  857. * other drbd minor! (if you use drbd.conf + drbdadm, that
  858. * should check it for you already; but if you don't, or
  859. * someone fooled it, we need to double check here)
  860. */
  861. bdev = blkdev_get_by_path(nbc->dc.meta_dev,
  862. FMODE_READ | FMODE_WRITE | FMODE_EXCL,
  863. (nbc->dc.meta_dev_idx < 0) ?
  864. (void *)mdev : (void *)drbd_m_holder);
  865. if (IS_ERR(bdev)) {
  866. dev_err(DEV, "open(\"%s\") failed with %ld\n", nbc->dc.meta_dev,
  867. PTR_ERR(bdev));
  868. retcode = ERR_OPEN_MD_DISK;
  869. goto fail;
  870. }
  871. nbc->md_bdev = bdev;
  872. if ((nbc->backing_bdev == nbc->md_bdev) !=
  873. (nbc->dc.meta_dev_idx == DRBD_MD_INDEX_INTERNAL ||
  874. nbc->dc.meta_dev_idx == DRBD_MD_INDEX_FLEX_INT)) {
  875. retcode = ERR_MD_IDX_INVALID;
  876. goto fail;
  877. }
  878. resync_lru = lc_create("resync", drbd_bm_ext_cache,
  879. 61, sizeof(struct bm_extent),
  880. offsetof(struct bm_extent, lce));
  881. if (!resync_lru) {
  882. retcode = ERR_NOMEM;
  883. goto fail;
  884. }
  885. /* RT - for drbd_get_max_capacity() DRBD_MD_INDEX_FLEX_INT */
  886. drbd_md_set_sector_offsets(mdev, nbc);
  887. if (drbd_get_max_capacity(nbc) < nbc->dc.disk_size) {
  888. dev_err(DEV, "max capacity %llu smaller than disk size %llu\n",
  889. (unsigned long long) drbd_get_max_capacity(nbc),
  890. (unsigned long long) nbc->dc.disk_size);
  891. retcode = ERR_DISK_TO_SMALL;
  892. goto fail;
  893. }
  894. if (nbc->dc.meta_dev_idx < 0) {
  895. max_possible_sectors = DRBD_MAX_SECTORS_FLEX;
  896. /* at least one MB, otherwise it does not make sense */
  897. min_md_device_sectors = (2<<10);
  898. } else {
  899. max_possible_sectors = DRBD_MAX_SECTORS;
  900. min_md_device_sectors = MD_RESERVED_SECT * (nbc->dc.meta_dev_idx + 1);
  901. }
  902. if (drbd_get_capacity(nbc->md_bdev) < min_md_device_sectors) {
  903. retcode = ERR_MD_DISK_TO_SMALL;
  904. dev_warn(DEV, "refusing attach: md-device too small, "
  905. "at least %llu sectors needed for this meta-disk type\n",
  906. (unsigned long long) min_md_device_sectors);
  907. goto fail;
  908. }
  909. /* Make sure the new disk is big enough
  910. * (we may currently be R_PRIMARY with no local disk...) */
  911. if (drbd_get_max_capacity(nbc) <
  912. drbd_get_capacity(mdev->this_bdev)) {
  913. retcode = ERR_DISK_TO_SMALL;
  914. goto fail;
  915. }
  916. nbc->known_size = drbd_get_capacity(nbc->backing_bdev);
  917. if (nbc->known_size > max_possible_sectors) {
  918. dev_warn(DEV, "==> truncating very big lower level device "
  919. "to currently maximum possible %llu sectors <==\n",
  920. (unsigned long long) max_possible_sectors);
  921. if (nbc->dc.meta_dev_idx >= 0)
  922. dev_warn(DEV, "==>> using internal or flexible "
  923. "meta data may help <<==\n");
  924. }
  925. drbd_suspend_io(mdev);
  926. /* also wait for the last barrier ack. */
  927. wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_pending_cnt) || is_susp(mdev->state));
  928. /* and for any other previously queued work */
  929. drbd_flush_workqueue(mdev);
  930. rv = _drbd_request_state(mdev, NS(disk, D_ATTACHING), CS_VERBOSE);
  931. retcode = rv; /* FIXME: Type mismatch. */
  932. drbd_resume_io(mdev);
  933. if (rv < SS_SUCCESS)
  934. goto fail;
  935. if (!get_ldev_if_state(mdev, D_ATTACHING))
  936. goto force_diskless;
  937. drbd_md_set_sector_offsets(mdev, nbc);
  938. /* allocate a second IO page if logical_block_size != 512 */
  939. logical_block_size = bdev_logical_block_size(nbc->md_bdev);
  940. if (logical_block_size == 0)
  941. logical_block_size = MD_SECTOR_SIZE;
  942. if (logical_block_size != MD_SECTOR_SIZE) {
  943. if (!mdev->md_io_tmpp) {
  944. struct page *page = alloc_page(GFP_NOIO);
  945. if (!page)
  946. goto force_diskless_dec;
  947. dev_warn(DEV, "Meta data's bdev logical_block_size = %d != %d\n",
  948. logical_block_size, MD_SECTOR_SIZE);
  949. dev_warn(DEV, "Workaround engaged (has performance impact).\n");
  950. mdev->md_io_tmpp = page;
  951. }
  952. }
  953. if (!mdev->bitmap) {
  954. if (drbd_bm_init(mdev)) {
  955. retcode = ERR_NOMEM;
  956. goto force_diskless_dec;
  957. }
  958. }
  959. retcode = drbd_md_read(mdev, nbc);
  960. if (retcode != NO_ERROR)
  961. goto force_diskless_dec;
  962. if (mdev->state.conn < C_CONNECTED &&
  963. mdev->state.role == R_PRIMARY &&
  964. (mdev->ed_uuid & ~((u64)1)) != (nbc->md.uuid[UI_CURRENT] & ~((u64)1))) {
  965. dev_err(DEV, "Can only attach to data with current UUID=%016llX\n",
  966. (unsigned long long)mdev->ed_uuid);
  967. retcode = ERR_DATA_NOT_CURRENT;
  968. goto force_diskless_dec;
  969. }
  970. /* Since we are diskless, fix the activity log first... */
  971. if (drbd_check_al_size(mdev)) {
  972. retcode = ERR_NOMEM;
  973. goto force_diskless_dec;
  974. }
  975. /* Prevent shrinking of consistent devices ! */
  976. if (drbd_md_test_flag(nbc, MDF_CONSISTENT) &&
  977. drbd_new_dev_size(mdev, nbc, 0) < nbc->md.la_size_sect) {
  978. dev_warn(DEV, "refusing to truncate a consistent device\n");
  979. retcode = ERR_DISK_TO_SMALL;
  980. goto force_diskless_dec;
  981. }
  982. if (!drbd_al_read_log(mdev, nbc)) {
  983. retcode = ERR_IO_MD_DISK;
  984. goto force_diskless_dec;
  985. }
  986. /* Reset the "barriers don't work" bits here, then force meta data to
  987. * be written, to ensure we determine if barriers are supported. */
  988. if (nbc->dc.no_md_flush)
  989. set_bit(MD_NO_FUA, &mdev->flags);
  990. else
  991. clear_bit(MD_NO_FUA, &mdev->flags);
  992. /* Point of no return reached.
  993. * Devices and memory are no longer released by error cleanup below.
  994. * now mdev takes over responsibility, and the state engine should
  995. * clean it up somewhere. */
  996. D_ASSERT(mdev->ldev == NULL);
  997. mdev->ldev = nbc;
  998. mdev->resync = resync_lru;
  999. nbc = NULL;
  1000. resync_lru = NULL;
  1001. mdev->write_ordering = WO_bdev_flush;
  1002. drbd_bump_write_ordering(mdev, WO_bdev_flush);
  1003. if (drbd_md_test_flag(mdev->ldev, MDF_CRASHED_PRIMARY))
  1004. set_bit(CRASHED_PRIMARY, &mdev->flags);
  1005. else
  1006. clear_bit(CRASHED_PRIMARY, &mdev->flags);
  1007. if (drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) &&
  1008. !(mdev->state.role == R_PRIMARY && mdev->state.susp_nod)) {
  1009. set_bit(CRASHED_PRIMARY, &mdev->flags);
  1010. cp_discovered = 1;
  1011. }
  1012. mdev->send_cnt = 0;
  1013. mdev->recv_cnt = 0;
  1014. mdev->read_cnt = 0;
  1015. mdev->writ_cnt = 0;
  1016. drbd_reconsider_max_bio_size(mdev);
  1017. /* If I am currently not R_PRIMARY,
  1018. * but meta data primary indicator is set,
  1019. * I just now recover from a hard crash,
  1020. * and have been R_PRIMARY before that crash.
  1021. *
  1022. * Now, if I had no connection before that crash
  1023. * (have been degraded R_PRIMARY), chances are that
  1024. * I won't find my peer now either.
  1025. *
  1026. * In that case, and _only_ in that case,
  1027. * we use the degr-wfc-timeout instead of the default,
  1028. * so we can automatically recover from a crash of a
  1029. * degraded but active "cluster" after a certain timeout.
  1030. */
  1031. clear_bit(USE_DEGR_WFC_T, &mdev->flags);
  1032. if (mdev->state.role != R_PRIMARY &&
  1033. drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) &&
  1034. !drbd_md_test_flag(mdev->ldev, MDF_CONNECTED_IND))
  1035. set_bit(USE_DEGR_WFC_T, &mdev->flags);
  1036. dd = drbd_determine_dev_size(mdev, 0);
  1037. if (dd == dev_size_error) {
  1038. retcode = ERR_NOMEM_BITMAP;
  1039. goto force_diskless_dec;
  1040. } else if (dd == grew)
  1041. set_bit(RESYNC_AFTER_NEG, &mdev->flags);
  1042. if (drbd_md_test_flag(mdev->ldev, MDF_FULL_SYNC)) {
  1043. dev_info(DEV, "Assuming that all blocks are out of sync "
  1044. "(aka FullSync)\n");
  1045. if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write,
  1046. "set_n_write from attaching", BM_LOCKED_MASK)) {
  1047. retcode = ERR_IO_MD_DISK;
  1048. goto force_diskless_dec;
  1049. }
  1050. } else {
  1051. if (drbd_bitmap_io(mdev, &drbd_bm_read,
  1052. "read from attaching", BM_LOCKED_MASK) < 0) {
  1053. retcode = ERR_IO_MD_DISK;
  1054. goto force_diskless_dec;
  1055. }
  1056. }
  1057. if (cp_discovered) {
  1058. drbd_al_apply_to_bm(mdev);
  1059. if (drbd_bitmap_io(mdev, &drbd_bm_write,
  1060. "crashed primary apply AL", BM_LOCKED_MASK)) {
  1061. retcode = ERR_IO_MD_DISK;
  1062. goto force_diskless_dec;
  1063. }
  1064. }
  1065. if (_drbd_bm_total_weight(mdev) == drbd_bm_bits(mdev))
  1066. drbd_suspend_al(mdev); /* IO is still suspended here... */
  1067. spin_lock_irq(&mdev->req_lock);
  1068. os = mdev->state;
  1069. ns.i = os.i;
  1070. /* If MDF_CONSISTENT is not set go into inconsistent state,
  1071. otherwise investigate MDF_WasUpToDate...
  1072. If MDF_WAS_UP_TO_DATE is not set go into D_OUTDATED disk state,
  1073. otherwise into D_CONSISTENT state.
  1074. */
  1075. if (drbd_md_test_flag(mdev->ldev, MDF_CONSISTENT)) {
  1076. if (drbd_md_test_flag(mdev->ldev, MDF_WAS_UP_TO_DATE))
  1077. ns.disk = D_CONSISTENT;
  1078. else
  1079. ns.disk = D_OUTDATED;
  1080. } else {
  1081. ns.disk = D_INCONSISTENT;
  1082. }
  1083. if (drbd_md_test_flag(mdev->ldev, MDF_PEER_OUT_DATED))
  1084. ns.pdsk = D_OUTDATED;
  1085. if ( ns.disk == D_CONSISTENT &&
  1086. (ns.pdsk == D_OUTDATED || mdev->ldev->dc.fencing == FP_DONT_CARE))
  1087. ns.disk = D_UP_TO_DATE;
  1088. /* All tests on MDF_PRIMARY_IND, MDF_CONNECTED_IND,
  1089. MDF_CONSISTENT and MDF_WAS_UP_TO_DATE must happen before
  1090. this point, because drbd_request_state() modifies these
  1091. flags. */
  1092. /* In case we are C_CONNECTED postpone any decision on the new disk
  1093. state after the negotiation phase. */
  1094. if (mdev->state.conn == C_CONNECTED) {
  1095. mdev->new_state_tmp.i = ns.i;
  1096. ns.i = os.i;
  1097. ns.disk = D_NEGOTIATING;
  1098. /* We expect to receive up-to-date UUIDs soon.
  1099. To avoid a race in receive_state, free p_uuid while
  1100. holding req_lock. I.e. atomic with the state change */
  1101. kfree(mdev->p_uuid);
  1102. mdev->p_uuid = NULL;
  1103. }
  1104. rv = _drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
  1105. ns = mdev->state;
  1106. spin_unlock_irq(&mdev->req_lock);
  1107. if (rv < SS_SUCCESS)
  1108. goto force_diskless_dec;
  1109. if (mdev->state.role == R_PRIMARY)
  1110. mdev->ldev->md.uuid[UI_CURRENT] |= (u64)1;
  1111. else
  1112. mdev->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
  1113. drbd_md_mark_dirty(mdev);
  1114. drbd_md_sync(mdev);
  1115. kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
  1116. put_ldev(mdev);
  1117. reply->ret_code = retcode;
  1118. drbd_reconfig_done(mdev);
  1119. return 0;
  1120. force_diskless_dec:
  1121. put_ldev(mdev);
  1122. force_diskless:
  1123. drbd_force_state(mdev, NS(disk, D_FAILED));
  1124. drbd_md_sync(mdev);
  1125. fail:
  1126. if (nbc) {
  1127. if (nbc->backing_bdev)
  1128. blkdev_put(nbc->backing_bdev,
  1129. FMODE_READ | FMODE_WRITE | FMODE_EXCL);
  1130. if (nbc->md_bdev)
  1131. blkdev_put(nbc->md_bdev,
  1132. FMODE_READ | FMODE_WRITE | FMODE_EXCL);
  1133. kfree(nbc);
  1134. }
  1135. lc_destroy(resync_lru);
  1136. reply->ret_code = retcode;
  1137. drbd_reconfig_done(mdev);
  1138. return 0;
  1139. }
  1140. /* Detaching the disk is a process in multiple stages. First we need to lock
  1141. * out application IO, in-flight IO, IO stuck in drbd_al_begin_io.
  1142. * Then we transition to D_DISKLESS, and wait for put_ldev() to return all
  1143. * internal references as well.
  1144. * Only then we have finally detached. */
  1145. static int drbd_nl_detach(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
  1146. struct drbd_nl_cfg_reply *reply)
  1147. {
  1148. enum drbd_ret_code retcode;
  1149. int ret;
  1150. drbd_suspend_io(mdev); /* so no-one is stuck in drbd_al_begin_io */
  1151. retcode = drbd_request_state(mdev, NS(disk, D_FAILED));
  1152. /* D_FAILED will transition to DISKLESS. */
  1153. ret = wait_event_interruptible(mdev->misc_wait,
  1154. mdev->state.disk != D_FAILED);
  1155. drbd_resume_io(mdev);
  1156. if ((int)retcode == (int)SS_IS_DISKLESS)
  1157. retcode = SS_NOTHING_TO_DO;
  1158. if (ret)
  1159. retcode = ERR_INTR;
  1160. reply->ret_code = retcode;
  1161. return 0;
  1162. }
  1163. static int drbd_nl_net_conf(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
  1164. struct drbd_nl_cfg_reply *reply)
  1165. {
  1166. int i, ns;
  1167. enum drbd_ret_code retcode;
  1168. struct net_conf *new_conf = NULL;
  1169. struct crypto_hash *tfm = NULL;
  1170. struct crypto_hash *integrity_w_tfm = NULL;
  1171. struct crypto_hash *integrity_r_tfm = NULL;
  1172. struct hlist_head *new_tl_hash = NULL;
  1173. struct hlist_head *new_ee_hash = NULL;
  1174. struct drbd_conf *odev;
  1175. char hmac_name[CRYPTO_MAX_ALG_NAME];
  1176. void *int_dig_out = NULL;
  1177. void *int_dig_in = NULL;
  1178. void *int_dig_vv = NULL;
  1179. struct sockaddr *new_my_addr, *new_peer_addr, *taken_addr;
  1180. drbd_reconfig_start(mdev);
  1181. if (mdev->state.conn > C_STANDALONE) {
  1182. retcode = ERR_NET_CONFIGURED;
  1183. goto fail;
  1184. }
  1185. /* allocation not in the IO path, cqueue thread context */
  1186. new_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
  1187. if (!new_conf) {
  1188. retcode = ERR_NOMEM;
  1189. goto fail;
  1190. }
  1191. new_conf->timeout = DRBD_TIMEOUT_DEF;
  1192. new_conf->try_connect_int = DRBD_CONNECT_INT_DEF;
  1193. new_conf->ping_int = DRBD_PING_INT_DEF;
  1194. new_conf->max_epoch_size = DRBD_MAX_EPOCH_SIZE_DEF;
  1195. new_conf->max_buffers = DRBD_MAX_BUFFERS_DEF;
  1196. new_conf->unplug_watermark = DRBD_UNPLUG_WATERMARK_DEF;
  1197. new_conf->sndbuf_size = DRBD_SNDBUF_SIZE_DEF;
  1198. new_conf->rcvbuf_size = DRBD_RCVBUF_SIZE_DEF;
  1199. new_conf->ko_count = DRBD_KO_COUNT_DEF;
  1200. new_conf->after_sb_0p = DRBD_AFTER_SB_0P_DEF;
  1201. new_conf->after_sb_1p = DRBD_AFTER_SB_1P_DEF;
  1202. new_conf->after_sb_2p = DRBD_AFTER_SB_2P_DEF;
  1203. new_conf->want_lose = 0;
  1204. new_conf->two_primaries = 0;
  1205. new_conf->wire_protocol = DRBD_PROT_C;
  1206. new_conf->ping_timeo = DRBD_PING_TIMEO_DEF;
  1207. new_conf->rr_conflict = DRBD_RR_CONFLICT_DEF;
  1208. new_conf->on_congestion = DRBD_ON_CONGESTION_DEF;
  1209. new_conf->cong_extents = DRBD_CONG_EXTENTS_DEF;
  1210. if (!net_conf_from_tags(mdev, nlp->tag_list, new_conf)) {
  1211. retcode = ERR_MANDATORY_TAG;
  1212. goto fail;
  1213. }
  1214. if (new_conf->two_primaries
  1215. && (new_conf->wire_protocol != DRBD_PROT_C)) {
  1216. retcode = ERR_NOT_PROTO_C;
  1217. goto fail;
  1218. }
  1219. if (get_ldev(mdev)) {
  1220. enum drbd_fencing_p fp = mdev->ldev->dc.fencing;
  1221. put_ldev(mdev);
  1222. if (new_conf->wire_protocol == DRBD_PROT_A && fp == FP_STONITH) {
  1223. retcode = ERR_STONITH_AND_PROT_A;
  1224. goto fail;
  1225. }
  1226. }
  1227. if (new_conf->on_congestion != OC_BLOCK && new_conf->wire_protocol != DRBD_PROT_A) {
  1228. retcode = ERR_CONG_NOT_PROTO_A;
  1229. goto fail;
  1230. }
  1231. if (mdev->state.role == R_PRIMARY && new_conf->want_lose) {
  1232. retcode = ERR_DISCARD;
  1233. goto fail;
  1234. }
  1235. retcode = NO_ERROR;
  1236. new_my_addr = (struct sockaddr *)&new_conf->my_addr;
  1237. new_peer_addr = (struct sockaddr *)&new_conf->peer_addr;
  1238. for (i = 0; i < minor_count; i++) {
  1239. odev = minor_to_mdev(i);
  1240. if (!odev || odev == mdev)
  1241. continue;
  1242. if (get_net_conf(odev)) {
  1243. taken_addr = (struct sockaddr *)&odev->net_conf->my_addr;
  1244. if (new_conf->my_addr_len == odev->net_conf->my_addr_len &&
  1245. !memcmp(new_my_addr, taken_addr, new_conf->my_addr_len))
  1246. retcode = ERR_LOCAL_ADDR;
  1247. taken_addr = (struct sockaddr *)&odev->net_conf->peer_addr;
  1248. if (new_conf->peer_addr_len == odev->net_conf->peer_addr_len &&
  1249. !memcmp(new_peer_addr, taken_addr, new_conf->peer_addr_len))
  1250. retcode = ERR_PEER_ADDR;
  1251. put_net_conf(odev);
  1252. if (retcode != NO_ERROR)
  1253. goto fail;
  1254. }
  1255. }
  1256. if (new_conf->cram_hmac_alg[0] != 0) {
  1257. snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)",
  1258. new_conf->cram_hmac_alg);
  1259. tfm = crypto_alloc_hash(hmac_name, 0, CRYPTO_ALG_ASYNC);
  1260. if (IS_ERR(tfm)) {
  1261. tfm = NULL;
  1262. retcode = ERR_AUTH_ALG;
  1263. goto fail;
  1264. }
  1265. if (!drbd_crypto_is_hash(crypto_hash_tfm(tfm))) {
  1266. retcode = ERR_AUTH_ALG_ND;
  1267. goto fail;
  1268. }
  1269. }
  1270. if (new_conf->integrity_alg[0]) {
  1271. integrity_w_tfm = crypto_alloc_hash(new_conf->integrity_alg, 0, CRYPTO_ALG_ASYNC);
  1272. if (IS_ERR(integrity_w_tfm)) {
  1273. integrity_w_tfm = NULL;
  1274. retcode=ERR_INTEGRITY_ALG;
  1275. goto fail;
  1276. }
  1277. if (!drbd_crypto_is_hash(crypto_hash_tfm(integrity_w_tfm))) {
  1278. retcode=ERR_INTEGRITY_ALG_ND;
  1279. goto fail;
  1280. }
  1281. integrity_r_tfm = crypto_alloc_hash(new_conf->integrity_alg, 0, CRYPTO_ALG_ASYNC);
  1282. if (IS_ERR(integrity_r_tfm)) {
  1283. integrity_r_tfm = NULL;
  1284. retcode=ERR_INTEGRITY_ALG;
  1285. goto fail;
  1286. }
  1287. }
  1288. ns = new_conf->max_epoch_size/8;
  1289. if (mdev->tl_hash_s != ns) {
  1290. new_tl_hash = kzalloc(ns*sizeof(void *), GFP_KERNEL);
  1291. if (!new_tl_hash) {
  1292. retcode = ERR_NOMEM;
  1293. goto fail;
  1294. }
  1295. }
  1296. ns = new_conf->max_buffers/8;
  1297. if (new_conf->two_primaries && (mdev->ee_hash_s != ns)) {
  1298. new_ee_hash = kzalloc(ns*sizeof(void *), GFP_KERNEL);
  1299. if (!new_ee_hash) {
  1300. retcode = ERR_NOMEM;
  1301. goto fail;
  1302. }
  1303. }
  1304. ((char *)new_conf->shared_secret)[SHARED_SECRET_MAX-1] = 0;
  1305. if (integrity_w_tfm) {
  1306. i = crypto_hash_digestsize(integrity_w_tfm);
  1307. int_dig_out = kmalloc(i, GFP_KERNEL);
  1308. if (!int_dig_out) {
  1309. retcode = ERR_NOMEM;
  1310. goto fail;
  1311. }
  1312. int_dig_in = kmalloc(i, GFP_KERNEL);
  1313. if (!int_dig_in) {
  1314. retcode = ERR_NOMEM;
  1315. goto fail;
  1316. }
  1317. int_dig_vv = kmalloc(i, GFP_KERNEL);
  1318. if (!int_dig_vv) {
  1319. retcode = ERR_NOMEM;
  1320. goto fail;
  1321. }
  1322. }
  1323. if (!mdev->bitmap) {
  1324. if(drbd_bm_init(mdev)) {
  1325. retcode = ERR_NOMEM;
  1326. goto fail;
  1327. }
  1328. }
  1329. drbd_flush_workqueue(mdev);
  1330. spin_lock_irq(&mdev->req_lock);
  1331. if (mdev->net_conf != NULL) {
  1332. retcode = ERR_NET_CONFIGURED;
  1333. spin_unlock_irq(&mdev->req_lock);
  1334. goto fail;
  1335. }
  1336. mdev->net_conf = new_conf;
  1337. mdev->send_cnt = 0;
  1338. mdev->recv_cnt = 0;
  1339. if (new_tl_hash) {
  1340. kfree(mdev->tl_hash);
  1341. mdev->tl_hash_s = mdev->net_conf->max_epoch_size/8;
  1342. mdev->tl_hash = new_tl_hash;
  1343. }
  1344. if (new_ee_hash) {
  1345. kfree(mdev->ee_hash);
  1346. mdev->ee_hash_s = mdev->net_conf->max_buffers/8;
  1347. mdev->ee_hash = new_ee_hash;
  1348. }
  1349. crypto_free_hash(mdev->cram_hmac_tfm);
  1350. mdev->cram_hmac_tfm = tfm;
  1351. crypto_free_hash(mdev->integrity_w_tfm);
  1352. mdev->integrity_w_tfm = integrity_w_tfm;
  1353. crypto_free_hash(mdev->integrity_r_tfm);
  1354. mdev->integrity_r_tfm = integrity_r_tfm;
  1355. kfree(mdev->int_dig_out);
  1356. kfree(mdev->int_dig_in);
  1357. kfree(mdev->int_dig_vv);
  1358. mdev->int_dig_out=int_dig_out;
  1359. mdev->int_dig_in=int_dig_in;
  1360. mdev->int_dig_vv=int_dig_vv;
  1361. retcode = _drbd_set_state(_NS(mdev, conn, C_UNCONNECTED), CS_VERBOSE, NULL);
  1362. spin_unlock_irq(&mdev->req_lock);
  1363. kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
  1364. reply->ret_code = retcode;
  1365. drbd_reconfig_done(mdev);
  1366. return 0;
  1367. fail:
  1368. kfree(int_dig_out);
  1369. kfree(int_dig_in);
  1370. kfree(int_dig_vv);
  1371. crypto_free_hash(tfm);
  1372. crypto_free_hash(integrity_w_tfm);
  1373. crypto_free_hash(integrity_r_tfm);
  1374. kfree(new_tl_hash);
  1375. kfree(new_ee_hash);
  1376. kfree(new_conf);
  1377. reply->ret_code = retcode;
  1378. drbd_reconfig_done(mdev);
  1379. return 0;
  1380. }
  1381. static int drbd_nl_disconnect(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
  1382. struct drbd_nl_cfg_reply *reply)
  1383. {
  1384. int retcode;
  1385. struct disconnect dc;
  1386. memset(&dc, 0, sizeof(struct disconnect));
  1387. if (!disconnect_from_tags(mdev, nlp->tag_list, &dc)) {
  1388. retcode = ERR_MANDATORY_TAG;
  1389. goto fail;
  1390. }
  1391. if (dc.force) {
  1392. spin_lock_irq(&mdev->req_lock);
  1393. if (mdev->state.conn >= C_WF_CONNECTION)
  1394. _drbd_set_state(_NS(mdev, conn, C_DISCONNECTING), CS_HARD, NULL);
  1395. spin_unlock_irq(&mdev->req_lock);
  1396. goto done;
  1397. }
  1398. retcode = _drbd_request_state(mdev, NS(conn, C_DISCONNECTING), CS_ORDERED);
  1399. if (retcode == SS_NOTHING_TO_DO)
  1400. goto done;
  1401. else if (retcode == SS_ALREADY_STANDALONE)
  1402. goto done;
  1403. else if (retcode == SS_PRIMARY_NOP) {
  1404. /* Our statche checking code wants to see the peer outdated. */
  1405. retcode = drbd_request_state(mdev, NS2(conn, C_DISCONNECTING,
  1406. pdsk, D_OUTDATED));
  1407. } else if (retcode == SS_CW_FAILED_BY_PEER) {
  1408. /* The peer probably wants to see us outdated. */
  1409. retcode = _drbd_request_state(mdev, NS2(conn, C_DISCONNECTING,
  1410. disk, D_OUTDATED),
  1411. CS_ORDERED);
  1412. if (retcode == SS_IS_DISKLESS || retcode == SS_LOWER_THAN_OUTDATED) {
  1413. drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
  1414. retcode = SS_SUCCESS;
  1415. }
  1416. }
  1417. if (retcode < SS_SUCCESS)
  1418. goto fail;
  1419. if (wait_event_interruptible(mdev->state_wait,
  1420. mdev->state.conn != C_DISCONNECTING)) {
  1421. /* Do not test for mdev->state.conn == C_STANDALONE, since
  1422. someone else might connect us in the mean time! */
  1423. retcode = ERR_INTR;
  1424. goto fail;
  1425. }
  1426. done:
  1427. retcode = NO_ERROR;
  1428. fail:
  1429. drbd_md_sync(mdev);
  1430. reply->ret_code = retcode;
  1431. return 0;
  1432. }
  1433. void resync_after_online_grow(struct drbd_conf *mdev)
  1434. {
  1435. int iass; /* I am sync source */
  1436. dev_info(DEV, "Resync of new storage after online grow\n");
  1437. if (mdev->state.role != mdev->state.peer)
  1438. iass = (mdev->state.role == R_PRIMARY);
  1439. else
  1440. iass = test_bit(DISCARD_CONCURRENT, &mdev->flags);
  1441. if (iass)
  1442. drbd_start_resync(mdev, C_SYNC_SOURCE);
  1443. else
  1444. _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE + CS_SERIALIZE);
  1445. }
  1446. static int drbd_nl_resize(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
  1447. struct drbd_nl_cfg_reply *reply)
  1448. {
  1449. struct resize rs;
  1450. int retcode = NO_ERROR;
  1451. enum determine_dev_size dd;
  1452. enum dds_flags ddsf;
  1453. memset(&rs, 0, sizeof(struct resize));
  1454. if (!resize_from_tags(mdev, nlp->tag_list, &rs)) {
  1455. retcode = ERR_MANDATORY_TAG;
  1456. goto fail;
  1457. }
  1458. if (mdev->state.conn > C_CONNECTED) {
  1459. retcode = ERR_RESIZE_RESYNC;
  1460. goto fail;
  1461. }
  1462. if (mdev->state.role == R_SECONDARY &&
  1463. mdev->state.peer == R_SECONDARY) {
  1464. retcode = ERR_NO_PRIMARY;
  1465. goto fail;
  1466. }
  1467. if (!get_ldev(mdev)) {
  1468. retcode = ERR_NO_DISK;
  1469. goto fail;
  1470. }
  1471. if (rs.no_resync && mdev->agreed_pro_version < 93) {
  1472. retcode = ERR_NEED_APV_93;
  1473. goto fail;
  1474. }
  1475. if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev))
  1476. mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev);
  1477. mdev->ldev->dc.disk_size = (sector_t)rs.resize_size;
  1478. ddsf = (rs.resize_force ? DDSF_FORCED : 0) | (rs.no_resync ? DDSF_NO_RESYNC : 0);
  1479. dd = drbd_determine_dev_size(mdev, ddsf);
  1480. drbd_md_sync(mdev);
  1481. put_ldev(mdev);
  1482. if (dd == dev_size_error) {
  1483. retcode = ERR_NOMEM_BITMAP;
  1484. goto fail;
  1485. }
  1486. if (mdev->state.conn == C_CONNECTED) {
  1487. if (dd == grew)
  1488. set_bit(RESIZE_PENDING, &mdev->flags);
  1489. drbd_send_uuids(mdev);
  1490. drbd_send_sizes(mdev, 1, ddsf);
  1491. }
  1492. fail:
  1493. reply->ret_code = retcode;
  1494. return 0;
  1495. }
  1496. static int drbd_nl_syncer_conf(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
  1497. struct drbd_nl_cfg_reply *reply)
  1498. {
  1499. int retcode = NO_ERROR;
  1500. int err;
  1501. int ovr; /* online verify running */
  1502. int rsr; /* re-sync running */
  1503. struct crypto_hash *verify_tfm = NULL;
  1504. struct crypto_hash *csums_tfm = NULL;
  1505. struct syncer_conf sc;
  1506. cpumask_var_t new_cpu_mask;
  1507. int *rs_plan_s = NULL;
  1508. int fifo_size;
  1509. if (!zalloc_cpumask_var(&new_cpu_mask, GFP_KERNEL)) {
  1510. retcode = ERR_NOMEM;
  1511. goto fail;
  1512. }
  1513. if (nlp->flags & DRBD_NL_SET_DEFAULTS) {
  1514. memset(&sc, 0, sizeof(struct syncer_conf));
  1515. sc.rate = DRBD_RATE_DEF;
  1516. sc.after = DRBD_AFTER_DEF;
  1517. sc.al_extents = DRBD_AL_EXTENTS_DEF;
  1518. sc.on_no_data = DRBD_ON_NO_DATA_DEF;
  1519. sc.c_plan_ahead = DRBD_C_PLAN_AHEAD_DEF;
  1520. sc.c_delay_target = DRBD_C_DELAY_TARGET_DEF;
  1521. sc.c_fill_target = DRBD_C_FILL_TARGET_DEF;
  1522. sc.c_max_rate = DRBD_C_MAX_RATE_DEF;
  1523. sc.c_min_rate = DRBD_C_MIN_RATE_DEF;
  1524. } else
  1525. memcpy(&sc, &mdev->sync_conf, sizeof(struct syncer_conf));
  1526. if (!syncer_conf_from_tags(mdev, nlp->tag_list, &sc)) {
  1527. retcode = ERR_MANDATORY_TAG;
  1528. goto fail;
  1529. }
  1530. /* re-sync running */
  1531. rsr = ( mdev->state.conn == C_SYNC_SOURCE ||
  1532. mdev->state.conn == C_SYNC_TARGET ||
  1533. mdev->state.conn == C_PAUSED_SYNC_S ||
  1534. mdev->state.conn == C_PAUSED_SYNC_T );
  1535. if (rsr && strcmp(sc.csums_alg, mdev->sync_conf.csums_alg)) {
  1536. retcode = ERR_CSUMS_RESYNC_RUNNING;
  1537. goto fail;
  1538. }
  1539. if (!rsr && sc.csums_alg[0]) {
  1540. csums_tfm = crypto_alloc_hash(sc.csums_alg, 0, CRYPTO_ALG_ASYNC);
  1541. if (IS_ERR(csums_tfm)) {
  1542. csums_tfm = NULL;
  1543. retcode = ERR_CSUMS_ALG;
  1544. goto fail;
  1545. }
  1546. if (!drbd_crypto_is_hash(crypto_hash_tfm(csums_tfm))) {
  1547. retcode = ERR_CSUMS_ALG_ND;
  1548. goto fail;
  1549. }
  1550. }
  1551. /* online verify running */
  1552. ovr = (mdev->state.conn == C_VERIFY_S || mdev->state.conn == C_VERIFY_T);
  1553. if (ovr) {
  1554. if (strcmp(sc.verify_alg, mdev->sync_conf.verify_alg)) {
  1555. retcode = ERR_VERIFY_RUNNING;
  1556. goto fail;
  1557. }
  1558. }
  1559. if (!ovr && sc.verify_alg[0]) {
  1560. verify_tfm = crypto_alloc_hash(sc.verify_alg, 0, CRYPTO_ALG_ASYNC);
  1561. if (IS_ERR(verify_tfm)) {
  1562. verify_tfm = NULL;
  1563. retcode = ERR_VERIFY_ALG;
  1564. goto fail;
  1565. }
  1566. if (!drbd_crypto_is_hash(crypto_hash_tfm(verify_tfm))) {
  1567. retcode = ERR_VERIFY_ALG_ND;
  1568. goto fail;
  1569. }
  1570. }
  1571. /* silently ignore cpu mask on UP kernel */
  1572. if (nr_cpu_ids > 1 && sc.cpu_mask[0] != 0) {
  1573. err = __bitmap_parse(sc.cpu_mask, 32, 0,
  1574. cpumask_bits(new_cpu_mask), nr_cpu_ids);
  1575. if (err) {
  1576. dev_warn(DEV, "__bitmap_parse() failed with %d\n", err);
  1577. retcode = ERR_CPU_MASK_PARSE;
  1578. goto fail;
  1579. }
  1580. }
  1581. ERR_IF (sc.rate < 1) sc.rate = 1;
  1582. ERR_IF (sc.al_extents < 7) sc.al_extents = 127; /* arbitrary minimum */
  1583. #define AL_MAX ((MD_AL_MAX_SIZE-1) * AL_EXTENTS_PT)
  1584. if (sc.al_extents > AL_MAX) {
  1585. dev_err(DEV, "sc.al_extents > %d\n", AL_MAX);
  1586. sc.al_extents = AL_MAX;
  1587. }
  1588. #undef AL_MAX
  1589. /* to avoid spurious errors when configuring minors before configuring
  1590. * the minors they depend on: if necessary, first create the minor we
  1591. * depend on */
  1592. if (sc.after >= 0)
  1593. ensure_mdev(sc.after, 1);
  1594. /* most sanity checks done, try to assign the new sync-after
  1595. * dependency. need to hold the global lock in there,
  1596. * to avoid a race in the dependency loop check. */
  1597. retcode = drbd_alter_sa(mdev, sc.after);
  1598. if (retcode != NO_ERROR)
  1599. goto fail;
  1600. fifo_size = (sc.c_plan_ahead * 10 * SLEEP_TIME) / HZ;
  1601. if (fifo_size != mdev->rs_plan_s.size && fifo_size > 0) {
  1602. rs_plan_s = kzalloc(sizeof(int) * fifo_size, GFP_KERNEL);
  1603. if (!rs_plan_s) {
  1604. dev_err(DEV, "kmalloc of fifo_buffer failed");
  1605. retcode = ERR_NOMEM;
  1606. goto fail;
  1607. }
  1608. }
  1609. /* ok, assign the rest of it as well.
  1610. * lock against receive_SyncParam() */
  1611. spin_lock(&mdev->peer_seq_lock);
  1612. mdev->sync_conf = sc;
  1613. if (!rsr) {
  1614. crypto_free_hash(mdev->csums_tfm);
  1615. mdev->csums_tfm = csums_tfm;
  1616. csums_tfm = NULL;
  1617. }
  1618. if (!ovr) {
  1619. crypto_free_hash(mdev->verify_tfm);
  1620. mdev->verify_tfm = verify_tfm;
  1621. verify_tfm = NULL;
  1622. }
  1623. if (fifo_size != mdev->rs_plan_s.size) {
  1624. kfree(mdev->rs_plan_s.values);
  1625. mdev->rs_plan_s.values = rs_plan_s;
  1626. mdev->rs_plan_s.size = fifo_size;
  1627. mdev->rs_planed = 0;
  1628. rs_plan_s = NULL;
  1629. }
  1630. spin_unlock(&mdev->peer_seq_lock);
  1631. if (get_ldev(mdev)) {
  1632. wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
  1633. drbd_al_shrink(mdev);
  1634. err = drbd_check_al_size(mdev);
  1635. lc_unlock(mdev->act_log);
  1636. wake_up(&mdev->al_wait);
  1637. put_ldev(mdev);
  1638. drbd_md_sync(mdev);
  1639. if (err) {
  1640. retcode = ERR_NOMEM;
  1641. goto fail;
  1642. }
  1643. }
  1644. if (mdev->state.conn >= C_CONNECTED)
  1645. drbd_send_sync_param(mdev, &sc);
  1646. if (!cpumask_equal(mdev->cpu_mask, new_cpu_mask)) {
  1647. cpumask_copy(mdev->cpu_mask, new_cpu_mask);
  1648. drbd_calc_cpu_mask(mdev);
  1649. mdev->receiver.reset_cpu_mask = 1;
  1650. mdev->asender.reset_cpu_mask = 1;
  1651. mdev->worker.reset_cpu_mask = 1;
  1652. }
  1653. kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
  1654. fail:
  1655. kfree(rs_plan_s);
  1656. free_cpumask_var(new_cpu_mask);
  1657. crypto_free_hash(csums_tfm);
  1658. crypto_free_hash(verify_tfm);
  1659. reply->ret_code = retcode;
  1660. return 0;
  1661. }
  1662. static int drbd_nl_invalidate(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
  1663. struct drbd_nl_cfg_reply *reply)
  1664. {
  1665. int retcode;
  1666. /* If there is still bitmap IO pending, probably because of a previous
  1667. * resync just being finished, wait for it before requesting a new resync. */
  1668. wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
  1669. retcode = _drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T), CS_ORDERED);
  1670. if (retcode < SS_SUCCESS && retcode != SS_NEED_CONNECTION)
  1671. retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T));
  1672. while (retcode == SS_NEED_CONNECTION) {
  1673. spin_lock_irq(&mdev->req_lock);
  1674. if (mdev->state.conn < C_CONNECTED)
  1675. retcode = _drbd_set_state(_NS(mdev, disk, D_INCONSISTENT), CS_VERBOSE, NULL);
  1676. spin_unlock_irq(&mdev->req_lock);
  1677. if (retcode != SS_NEED_CONNECTION)
  1678. break;
  1679. retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T));
  1680. }
  1681. reply->ret_code = retcode;
  1682. return 0;
  1683. }
  1684. static int drbd_bmio_set_susp_al(struct drbd_conf *mdev)
  1685. {
  1686. int rv;
  1687. rv = drbd_bmio_set_n_write(mdev);
  1688. drbd_suspend_al(mdev);
  1689. return rv;
  1690. }
  1691. static int drbd_nl_invalidate_peer(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
  1692. struct drbd_nl_cfg_reply *reply)
  1693. {
  1694. int retcode;
  1695. /* If there is still bitmap IO pending, probably because of a previous
  1696. * resync just being finished, wait for it before requesting a new resync. */
  1697. wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
  1698. retcode = _drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_S), CS_ORDERED);
  1699. if (retcode < SS_SUCCESS) {
  1700. if (retcode == SS_NEED_CONNECTION && mdev->state.role == R_PRIMARY) {
  1701. /* The peer will get a resync upon connect anyways. Just make that
  1702. into a full resync. */
  1703. retcode = drbd_request_state(mdev, NS(pdsk, D_INCONSISTENT));
  1704. if (retcode >= SS_SUCCESS) {
  1705. if (drbd_bitmap_io(mdev, &drbd_bmio_set_susp_al,
  1706. "set_n_write from invalidate_peer",
  1707. BM_LOCKED_SET_ALLOWED))
  1708. retcode = ERR_IO_MD_DISK;
  1709. }
  1710. } else
  1711. retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_S));
  1712. }
  1713. reply->ret_code = retcode;
  1714. return 0;
  1715. }
  1716. static int drbd_nl_pause_sync(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
  1717. struct drbd_nl_cfg_reply *reply)
  1718. {
  1719. int retcode = NO_ERROR;
  1720. if (drbd_request_state(mdev, NS(user_isp, 1)) == SS_NOTHING_TO_DO)
  1721. retcode = ERR_PAUSE_IS_SET;
  1722. reply->ret_code = retcode;
  1723. return 0;
  1724. }
  1725. static int drbd_nl_resume_sync(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
  1726. struct drbd_nl_cfg_reply *reply)
  1727. {
  1728. int retcode = NO_ERROR;
  1729. union drbd_state s;
  1730. if (drbd_request_state(mdev, NS(user_isp, 0)) == SS_NOTHING_TO_DO) {
  1731. s = mdev->state;
  1732. if (s.conn == C_PAUSED_SYNC_S || s.conn == C_PAUSED_SYNC_T) {
  1733. retcode = s.aftr_isp ? ERR_PIC_AFTER_DEP :
  1734. s.peer_isp ? ERR_PIC_PEER_DEP : ERR_PAUSE_IS_CLEAR;
  1735. } else {
  1736. retcode = ERR_PAUSE_IS_CLEAR;
  1737. }
  1738. }
  1739. reply->ret_code = retcode;
  1740. return 0;
  1741. }
  1742. static int drbd_nl_suspend_io(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
  1743. struct drbd_nl_cfg_reply *reply)
  1744. {
  1745. reply->ret_code = drbd_request_state(mdev, NS(susp, 1));
  1746. return 0;
  1747. }
  1748. static int drbd_nl_resume_io(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
  1749. struct drbd_nl_cfg_reply *reply)
  1750. {
  1751. if (test_bit(NEW_CUR_UUID, &mdev->flags)) {
  1752. drbd_uuid_new_current(mdev);
  1753. clear_bit(NEW_CUR_UUID, &mdev->flags);
  1754. }
  1755. drbd_suspend_io(mdev);
  1756. reply->ret_code = drbd_request_state(mdev, NS3(susp, 0, susp_nod, 0, susp_fen, 0));
  1757. if (reply->ret_code == SS_SUCCESS) {
  1758. if (mdev->state.conn < C_CONNECTED)
  1759. tl_clear(mdev);
  1760. if (mdev->state.disk == D_DISKLESS || mdev->state.disk == D_FAILED)
  1761. tl_restart(mdev, fail_frozen_disk_io);
  1762. }
  1763. drbd_resume_io(mdev);
  1764. return 0;
  1765. }
  1766. static int drbd_nl_outdate(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
  1767. struct drbd_nl_cfg_reply *reply)
  1768. {
  1769. reply->ret_code = drbd_request_state(mdev, NS(disk, D_OUTDATED));
  1770. return 0;
  1771. }
  1772. static int drbd_nl_get_config(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
  1773. struct drbd_nl_cfg_reply *reply)
  1774. {
  1775. unsigned short *tl;
  1776. tl = reply->tag_list;
  1777. if (get_ldev(mdev)) {
  1778. tl = disk_conf_to_tags(mdev, &mdev->ldev->dc, tl);
  1779. put_ldev(mdev);
  1780. }
  1781. if (get_net_conf(mdev)) {
  1782. tl = net_conf_to_tags(mdev, mdev->net_conf, tl);
  1783. put_net_conf(mdev);
  1784. }
  1785. tl = syncer_conf_to_tags(mdev, &mdev->sync_conf, tl);
  1786. put_unaligned(TT_END, tl++); /* Close the tag list */
  1787. return (int)((char *)tl - (char *)reply->tag_list);
  1788. }
  1789. static int drbd_nl_get_state(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
  1790. struct drbd_nl_cfg_reply *reply)
  1791. {
  1792. unsigned short *tl = reply->tag_list;
  1793. union drbd_state s = mdev->state;
  1794. unsigned long rs_left;
  1795. unsigned int res;
  1796. tl = get_state_to_tags(mdev, (struct get_state *)&s, tl);
  1797. /* no local ref, no bitmap, no syncer progress. */
  1798. if (s.conn >= C_SYNC_SOURCE && s.conn <= C_PAUSED_SYNC_T) {
  1799. if (get_ldev(mdev)) {
  1800. drbd_get_syncer_progress(mdev, &rs_left, &res);
  1801. tl = tl_add_int(tl, T_sync_progress, &res);
  1802. put_ldev(mdev);
  1803. }
  1804. }
  1805. put_unaligned(TT_END, tl++); /* Close the tag list */
  1806. return (int)((char *)tl - (char *)reply->tag_list);
  1807. }
  1808. static int drbd_nl_get_uuids(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
  1809. struct drbd_nl_cfg_reply *reply)
  1810. {
  1811. unsigned short *tl;
  1812. tl = reply->tag_list;
  1813. if (get_ldev(mdev)) {
  1814. tl = tl_add_blob(tl, T_uuids, mdev->ldev->md.uuid, UI_SIZE*sizeof(u64));
  1815. tl = tl_add_int(tl, T_uuids_flags, &mdev->ldev->md.flags);
  1816. put_ldev(mdev);
  1817. }
  1818. put_unaligned(TT_END, tl++); /* Close the tag list */
  1819. return (int)((char *)tl - (char *)reply->tag_list);
  1820. }
  1821. /**
  1822. * drbd_nl_get_timeout_flag() - Used by drbdsetup to find out which timeout value to use
  1823. * @mdev: DRBD device.
  1824. * @nlp: Netlink/connector packet from drbdsetup
  1825. * @reply: Reply packet for drbdsetup
  1826. */
  1827. static int drbd_nl_get_timeout_flag(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
  1828. struct drbd_nl_cfg_reply *reply)
  1829. {
  1830. unsigned short *tl;
  1831. char rv;
  1832. tl = reply->tag_list;
  1833. rv = mdev->state.pdsk == D_OUTDATED ? UT_PEER_OUTDATED :
  1834. test_bit(USE_DEGR_WFC_T, &mdev->flags) ? UT_DEGRADED : UT_DEFAULT;
  1835. tl = tl_add_blob(tl, T_use_degraded, &rv, sizeof(rv));
  1836. put_unaligned(TT_END, tl++); /* Close the tag list */
  1837. return (int)((char *)tl - (char *)reply->tag_list);
  1838. }
  1839. static int drbd_nl_start_ov(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
  1840. struct drbd_nl_cfg_reply *reply)
  1841. {
  1842. /* default to resume from last known position, if possible */
  1843. struct start_ov args =
  1844. { .start_sector = mdev->ov_start_sector };
  1845. if (!start_ov_from_tags(mdev, nlp->tag_list, &args)) {
  1846. reply->ret_code = ERR_MANDATORY_TAG;
  1847. return 0;
  1848. }
  1849. /* If there is still bitmap IO pending, e.g. previous resync or verify
  1850. * just being finished, wait for it before requesting a new resync. */
  1851. wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
  1852. /* w_make_ov_request expects position to be aligned */
  1853. mdev->ov_start_sector = args.start_sector & ~BM_SECT_PER_BIT;
  1854. reply->ret_code = drbd_request_state(mdev,NS(conn,C_VERIFY_S));
  1855. return 0;
  1856. }
  1857. static int drbd_nl_new_c_uuid(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
  1858. struct drbd_nl_cfg_reply *reply)
  1859. {
  1860. int retcode = NO_ERROR;
  1861. int skip_initial_sync = 0;
  1862. int err;
  1863. struct new_c_uuid args;
  1864. memset(&args, 0, sizeof(struct new_c_uuid));
  1865. if (!new_c_uuid_from_tags(mdev, nlp->tag_list, &args)) {
  1866. reply->ret_code = ERR_MANDATORY_TAG;
  1867. return 0;
  1868. }
  1869. mutex_lock(&mdev->state_mutex); /* Protects us against serialized state changes. */
  1870. if (!get_ldev(mdev)) {
  1871. retcode = ERR_NO_DISK;
  1872. goto out;
  1873. }
  1874. /* this is "skip initial sync", assume to be clean */
  1875. if (mdev->state.conn == C_CONNECTED && mdev->agreed_pro_version >= 90 &&
  1876. mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED && args.clear_bm) {
  1877. dev_info(DEV, "Preparing to skip initial sync\n");
  1878. skip_initial_sync = 1;
  1879. } else if (mdev->state.conn != C_STANDALONE) {
  1880. retcode = ERR_CONNECTED;
  1881. goto out_dec;
  1882. }
  1883. drbd_uuid_set(mdev, UI_BITMAP, 0); /* Rotate UI_BITMAP to History 1, etc... */
  1884. drbd_uuid_new_current(mdev); /* New current, previous to UI_BITMAP */
  1885. if (args.clear_bm) {
  1886. err = drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write,
  1887. "clear_n_write from new_c_uuid", BM_LOCKED_MASK);
  1888. if (err) {
  1889. dev_err(DEV, "Writing bitmap failed with %d\n",err);
  1890. retcode = ERR_IO_MD_DISK;
  1891. }
  1892. if (skip_initial_sync) {
  1893. drbd_send_uuids_skip_initial_sync(mdev);
  1894. _drbd_uuid_set(mdev, UI_BITMAP, 0);
  1895. drbd_print_uuids(mdev, "cleared bitmap UUID");
  1896. spin_lock_irq(&mdev->req_lock);
  1897. _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
  1898. CS_VERBOSE, NULL);
  1899. spin_unlock_irq(&mdev->req_lock);
  1900. }
  1901. }
  1902. drbd_md_sync(mdev);
  1903. out_dec:
  1904. put_ldev(mdev);
  1905. out:
  1906. mutex_unlock(&mdev->state_mutex);
  1907. reply->ret_code = retcode;
  1908. return 0;
  1909. }
  1910. struct cn_handler_struct {
  1911. int (*function)(struct drbd_conf *,
  1912. struct drbd_nl_cfg_req *,
  1913. struct drbd_nl_cfg_reply *);
  1914. int reply_body_size;
  1915. };
  1916. static struct cn_handler_struct cnd_table[] = {
  1917. [ P_primary ] = { &drbd_nl_primary, 0 },
  1918. [ P_secondary ] = { &drbd_nl_secondary, 0 },
  1919. [ P_disk_conf ] = { &drbd_nl_disk_conf, 0 },
  1920. [ P_detach ] = { &drbd_nl_detach, 0 },
  1921. [ P_net_conf ] = { &drbd_nl_net_conf, 0 },
  1922. [ P_disconnect ] = { &drbd_nl_disconnect, 0 },
  1923. [ P_resize ] = { &drbd_nl_resize, 0 },
  1924. [ P_syncer_conf ] = { &drbd_nl_syncer_conf, 0 },
  1925. [ P_invalidate ] = { &drbd_nl_invalidate, 0 },
  1926. [ P_invalidate_peer ] = { &drbd_nl_invalidate_peer, 0 },
  1927. [ P_pause_sync ] = { &drbd_nl_pause_sync, 0 },
  1928. [ P_resume_sync ] = { &drbd_nl_resume_sync, 0 },
  1929. [ P_suspend_io ] = { &drbd_nl_suspend_io, 0 },
  1930. [ P_resume_io ] = { &drbd_nl_resume_io, 0 },
  1931. [ P_outdate ] = { &drbd_nl_outdate, 0 },
  1932. [ P_get_config ] = { &drbd_nl_get_config,
  1933. sizeof(struct syncer_conf_tag_len_struct) +
  1934. sizeof(struct disk_conf_tag_len_struct) +
  1935. sizeof(struct net_conf_tag_len_struct) },
  1936. [ P_get_state ] = { &drbd_nl_get_state,
  1937. sizeof(struct get_state_tag_len_struct) +
  1938. sizeof(struct sync_progress_tag_len_struct) },
  1939. [ P_get_uuids ] = { &drbd_nl_get_uuids,
  1940. sizeof(struct get_uuids_tag_len_struct) },
  1941. [ P_get_timeout_flag ] = { &drbd_nl_get_timeout_flag,
  1942. sizeof(struct get_timeout_flag_tag_len_struct)},
  1943. [ P_start_ov ] = { &drbd_nl_start_ov, 0 },
  1944. [ P_new_c_uuid ] = { &drbd_nl_new_c_uuid, 0 },
  1945. };
  1946. static void drbd_connector_callback(struct cn_msg *req, struct netlink_skb_parms *nsp)
  1947. {
  1948. struct drbd_nl_cfg_req *nlp = (struct drbd_nl_cfg_req *)req->data;
  1949. struct cn_handler_struct *cm;
  1950. struct cn_msg *cn_reply;
  1951. struct drbd_nl_cfg_reply *reply;
  1952. struct drbd_conf *mdev;
  1953. int retcode, rr;
  1954. int reply_size = sizeof(struct cn_msg)
  1955. + sizeof(struct drbd_nl_cfg_reply)
  1956. + sizeof(short int);
  1957. if (!try_module_get(THIS_MODULE)) {
  1958. printk(KERN_ERR "drbd: try_module_get() failed!\n");
  1959. return;
  1960. }
  1961. if (!cap_raised(current_cap(), CAP_SYS_ADMIN)) {
  1962. retcode = ERR_PERM;
  1963. goto fail;
  1964. }
  1965. mdev = ensure_mdev(nlp->drbd_minor,
  1966. (nlp->flags & DRBD_NL_CREATE_DEVICE));
  1967. if (!mdev) {
  1968. retcode = ERR_MINOR_INVALID;
  1969. goto fail;
  1970. }
  1971. if (nlp->packet_type >= P_nl_after_last_packet ||
  1972. nlp->packet_type == P_return_code_only) {
  1973. retcode = ERR_PACKET_NR;
  1974. goto fail;
  1975. }
  1976. cm = cnd_table + nlp->packet_type;
  1977. /* This may happen if packet number is 0: */
  1978. if (cm->function == NULL) {
  1979. retcode = ERR_PACKET_NR;
  1980. goto fail;
  1981. }
  1982. reply_size += cm->reply_body_size;
  1983. /* allocation not in the IO path, cqueue thread context */
  1984. cn_reply = kzalloc(reply_size, GFP_KERNEL);
  1985. if (!cn_reply) {
  1986. retcode = ERR_NOMEM;
  1987. goto fail;
  1988. }
  1989. reply = (struct drbd_nl_cfg_reply *) cn_reply->data;
  1990. reply->packet_type =
  1991. cm->reply_body_size ? nlp->packet_type : P_return_code_only;
  1992. reply->minor = nlp->drbd_minor;
  1993. reply->ret_code = NO_ERROR; /* Might by modified by cm->function. */
  1994. /* reply->tag_list; might be modified by cm->function. */
  1995. rr = cm->function(mdev, nlp, reply);
  1996. cn_reply->id = req->id;
  1997. cn_reply->seq = req->seq;
  1998. cn_reply->ack = req->ack + 1;
  1999. cn_reply->len = sizeof(struct drbd_nl_cfg_reply) + rr;
  2000. cn_reply->flags = 0;
  2001. rr = cn_netlink_send(cn_reply, CN_IDX_DRBD, GFP_KERNEL);
  2002. if (rr && rr != -ESRCH)
  2003. printk(KERN_INFO "drbd: cn_netlink_send()=%d\n", rr);
  2004. kfree(cn_reply);
  2005. module_put(THIS_MODULE);
  2006. return;
  2007. fail:
  2008. drbd_nl_send_reply(req, retcode);
  2009. module_put(THIS_MODULE);
  2010. }
  2011. static atomic_t drbd_nl_seq = ATOMIC_INIT(2); /* two. */
  2012. static unsigned short *
  2013. __tl_add_blob(unsigned short *tl, enum drbd_tags tag, const void *data,
  2014. unsigned short len, int nul_terminated)
  2015. {
  2016. unsigned short l = tag_descriptions[tag_number(tag)].max_len;
  2017. len = (len < l) ? len : l;
  2018. put_unaligned(tag, tl++);
  2019. put_unaligned(len, tl++);
  2020. memcpy(tl, data, len);
  2021. tl = (unsigned short*)((char*)tl + len);
  2022. if (nul_terminated)
  2023. *((char*)tl - 1) = 0;
  2024. return tl;
  2025. }
  2026. static unsigned short *
  2027. tl_add_blob(unsigned short *tl, enum drbd_tags tag, const void *data, int len)
  2028. {
  2029. return __tl_add_blob(tl, tag, data, len, 0);
  2030. }
  2031. static unsigned short *
  2032. tl_add_str(unsigned short *tl, enum drbd_tags tag, const char *str)
  2033. {
  2034. return __tl_add_blob(tl, tag, str, strlen(str)+1, 0);
  2035. }
  2036. static unsigned short *
  2037. tl_add_int(unsigned short *tl, enum drbd_tags tag, const void *val)
  2038. {
  2039. put_unaligned(tag, tl++);
  2040. switch(tag_type(tag)) {
  2041. case TT_INTEGER:
  2042. put_unaligned(sizeof(int), tl++);
  2043. put_unaligned(*(int *)val, (int *)tl);
  2044. tl = (unsigned short*)((char*)tl+sizeof(int));
  2045. break;
  2046. case TT_INT64:
  2047. put_unaligned(sizeof(u64), tl++);
  2048. put_unaligned(*(u64 *)val, (u64 *)tl);
  2049. tl = (unsigned short*)((char*)tl+sizeof(u64));
  2050. break;
  2051. default:
  2052. /* someone did something stupid. */
  2053. ;
  2054. }
  2055. return tl;
  2056. }
  2057. void drbd_bcast_state(struct drbd_conf *mdev, union drbd_state state)
  2058. {
  2059. char buffer[sizeof(struct cn_msg)+
  2060. sizeof(struct drbd_nl_cfg_reply)+
  2061. sizeof(struct get_state_tag_len_struct)+
  2062. sizeof(short int)];
  2063. struct cn_msg *cn_reply = (struct cn_msg *) buffer;
  2064. struct drbd_nl_cfg_reply *reply =
  2065. (struct drbd_nl_cfg_reply *)cn_reply->data;
  2066. unsigned short *tl = reply->tag_list;
  2067. /* dev_warn(DEV, "drbd_bcast_state() got called\n"); */
  2068. tl = get_state_to_tags(mdev, (struct get_state *)&state, tl);
  2069. put_unaligned(TT_END, tl++); /* Close the tag list */
  2070. cn_reply->id.idx = CN_IDX_DRBD;
  2071. cn_reply->id.val = CN_VAL_DRBD;
  2072. cn_reply->seq = atomic_add_return(1, &drbd_nl_seq);
  2073. cn_reply->ack = 0; /* not used here. */
  2074. cn_reply->len = sizeof(struct drbd_nl_cfg_reply) +
  2075. (int)((char *)tl - (char *)reply->tag_list);
  2076. cn_reply->flags = 0;
  2077. reply->packet_type = P_get_state;
  2078. reply->minor = mdev_to_minor(mdev);
  2079. reply->ret_code = NO_ERROR;
  2080. cn_netlink_send(cn_reply, CN_IDX_DRBD, GFP_NOIO);
  2081. }
  2082. void drbd_bcast_ev_helper(struct drbd_conf *mdev, char *helper_name)
  2083. {
  2084. char buffer[sizeof(struct cn_msg)+
  2085. sizeof(struct drbd_nl_cfg_reply)+
  2086. sizeof(struct call_helper_tag_len_struct)+
  2087. sizeof(short int)];
  2088. struct cn_msg *cn_reply = (struct cn_msg *) buffer;
  2089. struct drbd_nl_cfg_reply *reply =
  2090. (struct drbd_nl_cfg_reply *)cn_reply->data;
  2091. unsigned short *tl = reply->tag_list;
  2092. /* dev_warn(DEV, "drbd_bcast_state() got called\n"); */
  2093. tl = tl_add_str(tl, T_helper, helper_name);
  2094. put_unaligned(TT_END, tl++); /* Close the tag list */
  2095. cn_reply->id.idx = CN_IDX_DRBD;
  2096. cn_reply->id.val = CN_VAL_DRBD;
  2097. cn_reply->seq = atomic_add_return(1, &drbd_nl_seq);
  2098. cn_reply->ack = 0; /* not used here. */
  2099. cn_reply->len = sizeof(struct drbd_nl_cfg_reply) +
  2100. (int)((char *)tl - (char *)reply->tag_list);
  2101. cn_reply->flags = 0;
  2102. reply->packet_type = P_call_helper;
  2103. reply->minor = mdev_to_minor(mdev);
  2104. reply->ret_code = NO_ERROR;
  2105. cn_netlink_send(cn_reply, CN_IDX_DRBD, GFP_NOIO);
  2106. }
  2107. void drbd_bcast_ee(struct drbd_conf *mdev,
  2108. const char *reason, const int dgs,
  2109. const char* seen_hash, const char* calc_hash,
  2110. const struct drbd_epoch_entry* e)
  2111. {
  2112. struct cn_msg *cn_reply;
  2113. struct drbd_nl_cfg_reply *reply;
  2114. unsigned short *tl;
  2115. struct page *page;
  2116. unsigned len;
  2117. if (!e)
  2118. return;
  2119. if (!reason || !reason[0])
  2120. return;
  2121. /* apparently we have to memcpy twice, first to prepare the data for the
  2122. * struct cn_msg, then within cn_netlink_send from the cn_msg to the
  2123. * netlink skb. */
  2124. /* receiver thread context, which is not in the writeout path (of this node),
  2125. * but may be in the writeout path of the _other_ node.
  2126. * GFP_NOIO to avoid potential "distributed deadlock". */
  2127. cn_reply = kzalloc(
  2128. sizeof(struct cn_msg)+
  2129. sizeof(struct drbd_nl_cfg_reply)+
  2130. sizeof(struct dump_ee_tag_len_struct)+
  2131. sizeof(short int),
  2132. GFP_NOIO);
  2133. if (!cn_reply) {
  2134. dev_err(DEV, "could not kmalloc buffer for drbd_bcast_ee, sector %llu, size %u\n",
  2135. (unsigned long long)e->sector, e->size);
  2136. return;
  2137. }
  2138. reply = (struct drbd_nl_cfg_reply*)cn_reply->data;
  2139. tl = reply->tag_list;
  2140. tl = tl_add_str(tl, T_dump_ee_reason, reason);
  2141. tl = tl_add_blob(tl, T_seen_digest, seen_hash, dgs);
  2142. tl = tl_add_blob(tl, T_calc_digest, calc_hash, dgs);
  2143. tl = tl_add_int(tl, T_ee_sector, &e->sector);
  2144. tl = tl_add_int(tl, T_ee_block_id, &e->block_id);
  2145. /* dump the first 32k */
  2146. len = min_t(unsigned, e->size, 32 << 10);
  2147. put_unaligned(T_ee_data, tl++);
  2148. put_unaligned(len, tl++);
  2149. page = e->pages;
  2150. page_chain_for_each(page) {
  2151. void *d = kmap_atomic(page, KM_USER0);
  2152. unsigned l = min_t(unsigned, len, PAGE_SIZE);
  2153. memcpy(tl, d, l);
  2154. kunmap_atomic(d, KM_USER0);
  2155. tl = (unsigned short*)((char*)tl + l);
  2156. len -= l;
  2157. if (len == 0)
  2158. break;
  2159. }
  2160. put_unaligned(TT_END, tl++); /* Close the tag list */
  2161. cn_reply->id.idx = CN_IDX_DRBD;
  2162. cn_reply->id.val = CN_VAL_DRBD;
  2163. cn_reply->seq = atomic_add_return(1,&drbd_nl_seq);
  2164. cn_reply->ack = 0; // not used here.
  2165. cn_reply->len = sizeof(struct drbd_nl_cfg_reply) +
  2166. (int)((char*)tl - (char*)reply->tag_list);
  2167. cn_reply->flags = 0;
  2168. reply->packet_type = P_dump_ee;
  2169. reply->minor = mdev_to_minor(mdev);
  2170. reply->ret_code = NO_ERROR;
  2171. cn_netlink_send(cn_reply, CN_IDX_DRBD, GFP_NOIO);
  2172. kfree(cn_reply);
  2173. }
  2174. void drbd_bcast_sync_progress(struct drbd_conf *mdev)
  2175. {
  2176. char buffer[sizeof(struct cn_msg)+
  2177. sizeof(struct drbd_nl_cfg_reply)+
  2178. sizeof(struct sync_progress_tag_len_struct)+
  2179. sizeof(short int)];
  2180. struct cn_msg *cn_reply = (struct cn_msg *) buffer;
  2181. struct drbd_nl_cfg_reply *reply =
  2182. (struct drbd_nl_cfg_reply *)cn_reply->data;
  2183. unsigned short *tl = reply->tag_list;
  2184. unsigned long rs_left;
  2185. unsigned int res;
  2186. /* no local ref, no bitmap, no syncer progress, no broadcast. */
  2187. if (!get_ldev(mdev))
  2188. return;
  2189. drbd_get_syncer_progress(mdev, &rs_left, &res);
  2190. put_ldev(mdev);
  2191. tl = tl_add_int(tl, T_sync_progress, &res);
  2192. put_unaligned(TT_END, tl++); /* Close the tag list */
  2193. cn_reply->id.idx = CN_IDX_DRBD;
  2194. cn_reply->id.val = CN_VAL_DRBD;
  2195. cn_reply->seq = atomic_add_return(1, &drbd_nl_seq);
  2196. cn_reply->ack = 0; /* not used here. */
  2197. cn_reply->len = sizeof(struct drbd_nl_cfg_reply) +
  2198. (int)((char *)tl - (char *)reply->tag_list);
  2199. cn_reply->flags = 0;
  2200. reply->packet_type = P_sync_progress;
  2201. reply->minor = mdev_to_minor(mdev);
  2202. reply->ret_code = NO_ERROR;
  2203. cn_netlink_send(cn_reply, CN_IDX_DRBD, GFP_NOIO);
  2204. }
  2205. int __init drbd_nl_init(void)
  2206. {
  2207. static struct cb_id cn_id_drbd;
  2208. int err, try=10;
  2209. cn_id_drbd.val = CN_VAL_DRBD;
  2210. do {
  2211. cn_id_drbd.idx = cn_idx;
  2212. err = cn_add_callback(&cn_id_drbd, "cn_drbd", &drbd_connector_callback);
  2213. if (!err)
  2214. break;
  2215. cn_idx = (cn_idx + CN_IDX_STEP);
  2216. } while (try--);
  2217. if (err) {
  2218. printk(KERN_ERR "drbd: cn_drbd failed to register\n");
  2219. return err;
  2220. }
  2221. return 0;
  2222. }
  2223. void drbd_nl_cleanup(void)
  2224. {
  2225. static struct cb_id cn_id_drbd;
  2226. cn_id_drbd.idx = cn_idx;
  2227. cn_id_drbd.val = CN_VAL_DRBD;
  2228. cn_del_callback(&cn_id_drbd);
  2229. }
  2230. void drbd_nl_send_reply(struct cn_msg *req, int ret_code)
  2231. {
  2232. char buffer[sizeof(struct cn_msg)+sizeof(struct drbd_nl_cfg_reply)];
  2233. struct cn_msg *cn_reply = (struct cn_msg *) buffer;
  2234. struct drbd_nl_cfg_reply *reply =
  2235. (struct drbd_nl_cfg_reply *)cn_reply->data;
  2236. int rr;
  2237. memset(buffer, 0, sizeof(buffer));
  2238. cn_reply->id = req->id;
  2239. cn_reply->seq = req->seq;
  2240. cn_reply->ack = req->ack + 1;
  2241. cn_reply->len = sizeof(struct drbd_nl_cfg_reply);
  2242. cn_reply->flags = 0;
  2243. reply->packet_type = P_return_code_only;
  2244. reply->minor = ((struct drbd_nl_cfg_req *)req->data)->drbd_minor;
  2245. reply->ret_code = ret_code;
  2246. rr = cn_netlink_send(cn_reply, CN_IDX_DRBD, GFP_NOIO);
  2247. if (rr && rr != -ESRCH)
  2248. printk(KERN_INFO "drbd: cn_netlink_send()=%d\n", rr);
  2249. }