drbd_nl.c 107 KB

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