drbd_nl.c 86 KB

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