drbd_nl.c 87 KB

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