drbd_nl.c 84 KB

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