drbd_nl.c 95 KB

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