drbd_state.c 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357
  1. /*
  2. drbd_state.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. Thanks to Carter Burden, Bart Grantham and Gennadiy Nerubayev
  8. from Logicworks, Inc. for making SDP replication support possible.
  9. drbd is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2, or (at your option)
  12. any later version.
  13. drbd is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with drbd; see the file COPYING. If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <linux/drbd_limits.h>
  22. #include "drbd_int.h"
  23. #include "drbd_protocol.h"
  24. #include "drbd_req.h"
  25. #include "drbd_state_change.h"
  26. struct after_state_chg_work {
  27. struct drbd_work w;
  28. struct drbd_device *device;
  29. union drbd_state os;
  30. union drbd_state ns;
  31. enum chg_state_flags flags;
  32. struct completion *done;
  33. struct drbd_state_change *state_change;
  34. };
  35. enum sanitize_state_warnings {
  36. NO_WARNING,
  37. ABORTED_ONLINE_VERIFY,
  38. ABORTED_RESYNC,
  39. CONNECTION_LOST_NEGOTIATING,
  40. IMPLICITLY_UPGRADED_DISK,
  41. IMPLICITLY_UPGRADED_PDSK,
  42. };
  43. static void count_objects(struct drbd_resource *resource,
  44. unsigned int *n_devices,
  45. unsigned int *n_connections)
  46. {
  47. struct drbd_device *device;
  48. struct drbd_connection *connection;
  49. int vnr;
  50. *n_devices = 0;
  51. *n_connections = 0;
  52. idr_for_each_entry(&resource->devices, device, vnr)
  53. (*n_devices)++;
  54. for_each_connection(connection, resource)
  55. (*n_connections)++;
  56. }
  57. static struct drbd_state_change *alloc_state_change(unsigned int n_devices, unsigned int n_connections, gfp_t gfp)
  58. {
  59. struct drbd_state_change *state_change;
  60. unsigned int size, n;
  61. size = sizeof(struct drbd_state_change) +
  62. n_devices * sizeof(struct drbd_device_state_change) +
  63. n_connections * sizeof(struct drbd_connection_state_change) +
  64. n_devices * n_connections * sizeof(struct drbd_peer_device_state_change);
  65. state_change = kmalloc(size, gfp);
  66. if (!state_change)
  67. return NULL;
  68. state_change->n_devices = n_devices;
  69. state_change->n_connections = n_connections;
  70. state_change->devices = (void *)(state_change + 1);
  71. state_change->connections = (void *)&state_change->devices[n_devices];
  72. state_change->peer_devices = (void *)&state_change->connections[n_connections];
  73. state_change->resource->resource = NULL;
  74. for (n = 0; n < n_devices; n++)
  75. state_change->devices[n].device = NULL;
  76. for (n = 0; n < n_connections; n++)
  77. state_change->connections[n].connection = NULL;
  78. return state_change;
  79. }
  80. struct drbd_state_change *remember_old_state(struct drbd_resource *resource, gfp_t gfp)
  81. {
  82. struct drbd_state_change *state_change;
  83. struct drbd_device *device;
  84. unsigned int n_devices;
  85. struct drbd_connection *connection;
  86. unsigned int n_connections;
  87. int vnr;
  88. struct drbd_device_state_change *device_state_change;
  89. struct drbd_peer_device_state_change *peer_device_state_change;
  90. struct drbd_connection_state_change *connection_state_change;
  91. /* Caller holds req_lock spinlock.
  92. * No state, no device IDR, no connections lists can change. */
  93. count_objects(resource, &n_devices, &n_connections);
  94. state_change = alloc_state_change(n_devices, n_connections, gfp);
  95. if (!state_change)
  96. return NULL;
  97. kref_get(&resource->kref);
  98. state_change->resource->resource = resource;
  99. state_change->resource->role[OLD] =
  100. conn_highest_role(first_connection(resource));
  101. state_change->resource->susp[OLD] = resource->susp;
  102. state_change->resource->susp_nod[OLD] = resource->susp_nod;
  103. state_change->resource->susp_fen[OLD] = resource->susp_fen;
  104. connection_state_change = state_change->connections;
  105. for_each_connection(connection, resource) {
  106. kref_get(&connection->kref);
  107. connection_state_change->connection = connection;
  108. connection_state_change->cstate[OLD] =
  109. connection->cstate;
  110. connection_state_change->peer_role[OLD] =
  111. conn_highest_peer(connection);
  112. connection_state_change++;
  113. }
  114. device_state_change = state_change->devices;
  115. peer_device_state_change = state_change->peer_devices;
  116. idr_for_each_entry(&resource->devices, device, vnr) {
  117. kref_get(&device->kref);
  118. device_state_change->device = device;
  119. device_state_change->disk_state[OLD] = device->state.disk;
  120. /* The peer_devices for each device have to be enumerated in
  121. the order of the connections. We may not use for_each_peer_device() here. */
  122. for_each_connection(connection, resource) {
  123. struct drbd_peer_device *peer_device;
  124. peer_device = conn_peer_device(connection, device->vnr);
  125. peer_device_state_change->peer_device = peer_device;
  126. peer_device_state_change->disk_state[OLD] =
  127. device->state.pdsk;
  128. peer_device_state_change->repl_state[OLD] =
  129. max_t(enum drbd_conns,
  130. C_WF_REPORT_PARAMS, device->state.conn);
  131. peer_device_state_change->resync_susp_user[OLD] =
  132. device->state.user_isp;
  133. peer_device_state_change->resync_susp_peer[OLD] =
  134. device->state.peer_isp;
  135. peer_device_state_change->resync_susp_dependency[OLD] =
  136. device->state.aftr_isp;
  137. peer_device_state_change++;
  138. }
  139. device_state_change++;
  140. }
  141. return state_change;
  142. }
  143. static void remember_new_state(struct drbd_state_change *state_change)
  144. {
  145. struct drbd_resource_state_change *resource_state_change;
  146. struct drbd_resource *resource;
  147. unsigned int n;
  148. if (!state_change)
  149. return;
  150. resource_state_change = &state_change->resource[0];
  151. resource = resource_state_change->resource;
  152. resource_state_change->role[NEW] =
  153. conn_highest_role(first_connection(resource));
  154. resource_state_change->susp[NEW] = resource->susp;
  155. resource_state_change->susp_nod[NEW] = resource->susp_nod;
  156. resource_state_change->susp_fen[NEW] = resource->susp_fen;
  157. for (n = 0; n < state_change->n_devices; n++) {
  158. struct drbd_device_state_change *device_state_change =
  159. &state_change->devices[n];
  160. struct drbd_device *device = device_state_change->device;
  161. device_state_change->disk_state[NEW] = device->state.disk;
  162. }
  163. for (n = 0; n < state_change->n_connections; n++) {
  164. struct drbd_connection_state_change *connection_state_change =
  165. &state_change->connections[n];
  166. struct drbd_connection *connection =
  167. connection_state_change->connection;
  168. connection_state_change->cstate[NEW] = connection->cstate;
  169. connection_state_change->peer_role[NEW] =
  170. conn_highest_peer(connection);
  171. }
  172. for (n = 0; n < state_change->n_devices * state_change->n_connections; n++) {
  173. struct drbd_peer_device_state_change *peer_device_state_change =
  174. &state_change->peer_devices[n];
  175. struct drbd_device *device =
  176. peer_device_state_change->peer_device->device;
  177. union drbd_dev_state state = device->state;
  178. peer_device_state_change->disk_state[NEW] = state.pdsk;
  179. peer_device_state_change->repl_state[NEW] =
  180. max_t(enum drbd_conns, C_WF_REPORT_PARAMS, state.conn);
  181. peer_device_state_change->resync_susp_user[NEW] =
  182. state.user_isp;
  183. peer_device_state_change->resync_susp_peer[NEW] =
  184. state.peer_isp;
  185. peer_device_state_change->resync_susp_dependency[NEW] =
  186. state.aftr_isp;
  187. }
  188. }
  189. void copy_old_to_new_state_change(struct drbd_state_change *state_change)
  190. {
  191. struct drbd_resource_state_change *resource_state_change = &state_change->resource[0];
  192. unsigned int n_device, n_connection, n_peer_device, n_peer_devices;
  193. #define OLD_TO_NEW(x) \
  194. (x[NEW] = x[OLD])
  195. OLD_TO_NEW(resource_state_change->role);
  196. OLD_TO_NEW(resource_state_change->susp);
  197. OLD_TO_NEW(resource_state_change->susp_nod);
  198. OLD_TO_NEW(resource_state_change->susp_fen);
  199. for (n_connection = 0; n_connection < state_change->n_connections; n_connection++) {
  200. struct drbd_connection_state_change *connection_state_change =
  201. &state_change->connections[n_connection];
  202. OLD_TO_NEW(connection_state_change->peer_role);
  203. OLD_TO_NEW(connection_state_change->cstate);
  204. }
  205. for (n_device = 0; n_device < state_change->n_devices; n_device++) {
  206. struct drbd_device_state_change *device_state_change =
  207. &state_change->devices[n_device];
  208. OLD_TO_NEW(device_state_change->disk_state);
  209. }
  210. n_peer_devices = state_change->n_devices * state_change->n_connections;
  211. for (n_peer_device = 0; n_peer_device < n_peer_devices; n_peer_device++) {
  212. struct drbd_peer_device_state_change *p =
  213. &state_change->peer_devices[n_peer_device];
  214. OLD_TO_NEW(p->disk_state);
  215. OLD_TO_NEW(p->repl_state);
  216. OLD_TO_NEW(p->resync_susp_user);
  217. OLD_TO_NEW(p->resync_susp_peer);
  218. OLD_TO_NEW(p->resync_susp_dependency);
  219. }
  220. #undef OLD_TO_NEW
  221. }
  222. void forget_state_change(struct drbd_state_change *state_change)
  223. {
  224. unsigned int n;
  225. if (!state_change)
  226. return;
  227. if (state_change->resource->resource)
  228. kref_put(&state_change->resource->resource->kref, drbd_destroy_resource);
  229. for (n = 0; n < state_change->n_devices; n++) {
  230. struct drbd_device *device = state_change->devices[n].device;
  231. if (device)
  232. kref_put(&device->kref, drbd_destroy_device);
  233. }
  234. for (n = 0; n < state_change->n_connections; n++) {
  235. struct drbd_connection *connection =
  236. state_change->connections[n].connection;
  237. if (connection)
  238. kref_put(&connection->kref, drbd_destroy_connection);
  239. }
  240. kfree(state_change);
  241. }
  242. static int w_after_state_ch(struct drbd_work *w, int unused);
  243. static void after_state_ch(struct drbd_device *device, union drbd_state os,
  244. union drbd_state ns, enum chg_state_flags flags,
  245. struct drbd_state_change *);
  246. static enum drbd_state_rv is_valid_state(struct drbd_device *, union drbd_state);
  247. static enum drbd_state_rv is_valid_soft_transition(union drbd_state, union drbd_state, struct drbd_connection *);
  248. static enum drbd_state_rv is_valid_transition(union drbd_state os, union drbd_state ns);
  249. static union drbd_state sanitize_state(struct drbd_device *device, union drbd_state os,
  250. union drbd_state ns, enum sanitize_state_warnings *warn);
  251. static inline bool is_susp(union drbd_state s)
  252. {
  253. return s.susp || s.susp_nod || s.susp_fen;
  254. }
  255. bool conn_all_vols_unconf(struct drbd_connection *connection)
  256. {
  257. struct drbd_peer_device *peer_device;
  258. bool rv = true;
  259. int vnr;
  260. rcu_read_lock();
  261. idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
  262. struct drbd_device *device = peer_device->device;
  263. if (device->state.disk != D_DISKLESS ||
  264. device->state.conn != C_STANDALONE ||
  265. device->state.role != R_SECONDARY) {
  266. rv = false;
  267. break;
  268. }
  269. }
  270. rcu_read_unlock();
  271. return rv;
  272. }
  273. /* Unfortunately the states where not correctly ordered, when
  274. they where defined. therefore can not use max_t() here. */
  275. static enum drbd_role max_role(enum drbd_role role1, enum drbd_role role2)
  276. {
  277. if (role1 == R_PRIMARY || role2 == R_PRIMARY)
  278. return R_PRIMARY;
  279. if (role1 == R_SECONDARY || role2 == R_SECONDARY)
  280. return R_SECONDARY;
  281. return R_UNKNOWN;
  282. }
  283. static enum drbd_role min_role(enum drbd_role role1, enum drbd_role role2)
  284. {
  285. if (role1 == R_UNKNOWN || role2 == R_UNKNOWN)
  286. return R_UNKNOWN;
  287. if (role1 == R_SECONDARY || role2 == R_SECONDARY)
  288. return R_SECONDARY;
  289. return R_PRIMARY;
  290. }
  291. enum drbd_role conn_highest_role(struct drbd_connection *connection)
  292. {
  293. enum drbd_role role = R_UNKNOWN;
  294. struct drbd_peer_device *peer_device;
  295. int vnr;
  296. rcu_read_lock();
  297. idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
  298. struct drbd_device *device = peer_device->device;
  299. role = max_role(role, device->state.role);
  300. }
  301. rcu_read_unlock();
  302. return role;
  303. }
  304. enum drbd_role conn_highest_peer(struct drbd_connection *connection)
  305. {
  306. enum drbd_role peer = R_UNKNOWN;
  307. struct drbd_peer_device *peer_device;
  308. int vnr;
  309. rcu_read_lock();
  310. idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
  311. struct drbd_device *device = peer_device->device;
  312. peer = max_role(peer, device->state.peer);
  313. }
  314. rcu_read_unlock();
  315. return peer;
  316. }
  317. enum drbd_disk_state conn_highest_disk(struct drbd_connection *connection)
  318. {
  319. enum drbd_disk_state disk_state = D_DISKLESS;
  320. struct drbd_peer_device *peer_device;
  321. int vnr;
  322. rcu_read_lock();
  323. idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
  324. struct drbd_device *device = peer_device->device;
  325. disk_state = max_t(enum drbd_disk_state, disk_state, device->state.disk);
  326. }
  327. rcu_read_unlock();
  328. return disk_state;
  329. }
  330. enum drbd_disk_state conn_lowest_disk(struct drbd_connection *connection)
  331. {
  332. enum drbd_disk_state disk_state = D_MASK;
  333. struct drbd_peer_device *peer_device;
  334. int vnr;
  335. rcu_read_lock();
  336. idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
  337. struct drbd_device *device = peer_device->device;
  338. disk_state = min_t(enum drbd_disk_state, disk_state, device->state.disk);
  339. }
  340. rcu_read_unlock();
  341. return disk_state;
  342. }
  343. enum drbd_disk_state conn_highest_pdsk(struct drbd_connection *connection)
  344. {
  345. enum drbd_disk_state disk_state = D_DISKLESS;
  346. struct drbd_peer_device *peer_device;
  347. int vnr;
  348. rcu_read_lock();
  349. idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
  350. struct drbd_device *device = peer_device->device;
  351. disk_state = max_t(enum drbd_disk_state, disk_state, device->state.pdsk);
  352. }
  353. rcu_read_unlock();
  354. return disk_state;
  355. }
  356. enum drbd_conns conn_lowest_conn(struct drbd_connection *connection)
  357. {
  358. enum drbd_conns conn = C_MASK;
  359. struct drbd_peer_device *peer_device;
  360. int vnr;
  361. rcu_read_lock();
  362. idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
  363. struct drbd_device *device = peer_device->device;
  364. conn = min_t(enum drbd_conns, conn, device->state.conn);
  365. }
  366. rcu_read_unlock();
  367. return conn;
  368. }
  369. static bool no_peer_wf_report_params(struct drbd_connection *connection)
  370. {
  371. struct drbd_peer_device *peer_device;
  372. int vnr;
  373. bool rv = true;
  374. rcu_read_lock();
  375. idr_for_each_entry(&connection->peer_devices, peer_device, vnr)
  376. if (peer_device->device->state.conn == C_WF_REPORT_PARAMS) {
  377. rv = false;
  378. break;
  379. }
  380. rcu_read_unlock();
  381. return rv;
  382. }
  383. static void wake_up_all_devices(struct drbd_connection *connection)
  384. {
  385. struct drbd_peer_device *peer_device;
  386. int vnr;
  387. rcu_read_lock();
  388. idr_for_each_entry(&connection->peer_devices, peer_device, vnr)
  389. wake_up(&peer_device->device->state_wait);
  390. rcu_read_unlock();
  391. }
  392. /**
  393. * cl_wide_st_chg() - true if the state change is a cluster wide one
  394. * @device: DRBD device.
  395. * @os: old (current) state.
  396. * @ns: new (wanted) state.
  397. */
  398. static int cl_wide_st_chg(struct drbd_device *device,
  399. union drbd_state os, union drbd_state ns)
  400. {
  401. return (os.conn >= C_CONNECTED && ns.conn >= C_CONNECTED &&
  402. ((os.role != R_PRIMARY && ns.role == R_PRIMARY) ||
  403. (os.conn != C_STARTING_SYNC_T && ns.conn == C_STARTING_SYNC_T) ||
  404. (os.conn != C_STARTING_SYNC_S && ns.conn == C_STARTING_SYNC_S) ||
  405. (os.disk != D_FAILED && ns.disk == D_FAILED))) ||
  406. (os.conn >= C_CONNECTED && ns.conn == C_DISCONNECTING) ||
  407. (os.conn == C_CONNECTED && ns.conn == C_VERIFY_S) ||
  408. (os.conn == C_CONNECTED && ns.conn == C_WF_REPORT_PARAMS);
  409. }
  410. static union drbd_state
  411. apply_mask_val(union drbd_state os, union drbd_state mask, union drbd_state val)
  412. {
  413. union drbd_state ns;
  414. ns.i = (os.i & ~mask.i) | val.i;
  415. return ns;
  416. }
  417. enum drbd_state_rv
  418. drbd_change_state(struct drbd_device *device, enum chg_state_flags f,
  419. union drbd_state mask, union drbd_state val)
  420. {
  421. unsigned long flags;
  422. union drbd_state ns;
  423. enum drbd_state_rv rv;
  424. spin_lock_irqsave(&device->resource->req_lock, flags);
  425. ns = apply_mask_val(drbd_read_state(device), mask, val);
  426. rv = _drbd_set_state(device, ns, f, NULL);
  427. spin_unlock_irqrestore(&device->resource->req_lock, flags);
  428. return rv;
  429. }
  430. /**
  431. * drbd_force_state() - Impose a change which happens outside our control on our state
  432. * @device: DRBD device.
  433. * @mask: mask of state bits to change.
  434. * @val: value of new state bits.
  435. */
  436. void drbd_force_state(struct drbd_device *device,
  437. union drbd_state mask, union drbd_state val)
  438. {
  439. drbd_change_state(device, CS_HARD, mask, val);
  440. }
  441. static enum drbd_state_rv
  442. _req_st_cond(struct drbd_device *device, union drbd_state mask,
  443. union drbd_state val)
  444. {
  445. union drbd_state os, ns;
  446. unsigned long flags;
  447. enum drbd_state_rv rv;
  448. if (test_and_clear_bit(CL_ST_CHG_SUCCESS, &device->flags))
  449. return SS_CW_SUCCESS;
  450. if (test_and_clear_bit(CL_ST_CHG_FAIL, &device->flags))
  451. return SS_CW_FAILED_BY_PEER;
  452. spin_lock_irqsave(&device->resource->req_lock, flags);
  453. os = drbd_read_state(device);
  454. ns = sanitize_state(device, os, apply_mask_val(os, mask, val), NULL);
  455. rv = is_valid_transition(os, ns);
  456. if (rv >= SS_SUCCESS)
  457. rv = SS_UNKNOWN_ERROR; /* cont waiting, otherwise fail. */
  458. if (!cl_wide_st_chg(device, os, ns))
  459. rv = SS_CW_NO_NEED;
  460. if (rv == SS_UNKNOWN_ERROR) {
  461. rv = is_valid_state(device, ns);
  462. if (rv >= SS_SUCCESS) {
  463. rv = is_valid_soft_transition(os, ns, first_peer_device(device)->connection);
  464. if (rv >= SS_SUCCESS)
  465. rv = SS_UNKNOWN_ERROR; /* cont waiting, otherwise fail. */
  466. }
  467. }
  468. spin_unlock_irqrestore(&device->resource->req_lock, flags);
  469. return rv;
  470. }
  471. /**
  472. * drbd_req_state() - Perform an eventually cluster wide state change
  473. * @device: DRBD device.
  474. * @mask: mask of state bits to change.
  475. * @val: value of new state bits.
  476. * @f: flags
  477. *
  478. * Should not be called directly, use drbd_request_state() or
  479. * _drbd_request_state().
  480. */
  481. static enum drbd_state_rv
  482. drbd_req_state(struct drbd_device *device, union drbd_state mask,
  483. union drbd_state val, enum chg_state_flags f)
  484. {
  485. struct completion done;
  486. unsigned long flags;
  487. union drbd_state os, ns;
  488. enum drbd_state_rv rv;
  489. init_completion(&done);
  490. if (f & CS_SERIALIZE)
  491. mutex_lock(device->state_mutex);
  492. spin_lock_irqsave(&device->resource->req_lock, flags);
  493. os = drbd_read_state(device);
  494. ns = sanitize_state(device, os, apply_mask_val(os, mask, val), NULL);
  495. rv = is_valid_transition(os, ns);
  496. if (rv < SS_SUCCESS) {
  497. spin_unlock_irqrestore(&device->resource->req_lock, flags);
  498. goto abort;
  499. }
  500. if (cl_wide_st_chg(device, os, ns)) {
  501. rv = is_valid_state(device, ns);
  502. if (rv == SS_SUCCESS)
  503. rv = is_valid_soft_transition(os, ns, first_peer_device(device)->connection);
  504. spin_unlock_irqrestore(&device->resource->req_lock, flags);
  505. if (rv < SS_SUCCESS) {
  506. if (f & CS_VERBOSE)
  507. print_st_err(device, os, ns, rv);
  508. goto abort;
  509. }
  510. if (drbd_send_state_req(first_peer_device(device), mask, val)) {
  511. rv = SS_CW_FAILED_BY_PEER;
  512. if (f & CS_VERBOSE)
  513. print_st_err(device, os, ns, rv);
  514. goto abort;
  515. }
  516. wait_event(device->state_wait,
  517. (rv = _req_st_cond(device, mask, val)));
  518. if (rv < SS_SUCCESS) {
  519. if (f & CS_VERBOSE)
  520. print_st_err(device, os, ns, rv);
  521. goto abort;
  522. }
  523. spin_lock_irqsave(&device->resource->req_lock, flags);
  524. ns = apply_mask_val(drbd_read_state(device), mask, val);
  525. rv = _drbd_set_state(device, ns, f, &done);
  526. } else {
  527. rv = _drbd_set_state(device, ns, f, &done);
  528. }
  529. spin_unlock_irqrestore(&device->resource->req_lock, flags);
  530. if (f & CS_WAIT_COMPLETE && rv == SS_SUCCESS) {
  531. D_ASSERT(device, current != first_peer_device(device)->connection->worker.task);
  532. wait_for_completion(&done);
  533. }
  534. abort:
  535. if (f & CS_SERIALIZE)
  536. mutex_unlock(device->state_mutex);
  537. return rv;
  538. }
  539. /**
  540. * _drbd_request_state() - Request a state change (with flags)
  541. * @device: DRBD device.
  542. * @mask: mask of state bits to change.
  543. * @val: value of new state bits.
  544. * @f: flags
  545. *
  546. * Cousin of drbd_request_state(), useful with the CS_WAIT_COMPLETE
  547. * flag, or when logging of failed state change requests is not desired.
  548. */
  549. enum drbd_state_rv
  550. _drbd_request_state(struct drbd_device *device, union drbd_state mask,
  551. union drbd_state val, enum chg_state_flags f)
  552. {
  553. enum drbd_state_rv rv;
  554. wait_event(device->state_wait,
  555. (rv = drbd_req_state(device, mask, val, f)) != SS_IN_TRANSIENT_STATE);
  556. return rv;
  557. }
  558. enum drbd_state_rv
  559. _drbd_request_state_holding_state_mutex(struct drbd_device *device, union drbd_state mask,
  560. union drbd_state val, enum chg_state_flags f)
  561. {
  562. enum drbd_state_rv rv;
  563. BUG_ON(f & CS_SERIALIZE);
  564. wait_event_cmd(device->state_wait,
  565. (rv = drbd_req_state(device, mask, val, f)) != SS_IN_TRANSIENT_STATE,
  566. mutex_unlock(device->state_mutex),
  567. mutex_lock(device->state_mutex));
  568. return rv;
  569. }
  570. static void print_st(struct drbd_device *device, const char *name, union drbd_state ns)
  571. {
  572. drbd_err(device, " %s = { cs:%s ro:%s/%s ds:%s/%s %c%c%c%c%c%c }\n",
  573. name,
  574. drbd_conn_str(ns.conn),
  575. drbd_role_str(ns.role),
  576. drbd_role_str(ns.peer),
  577. drbd_disk_str(ns.disk),
  578. drbd_disk_str(ns.pdsk),
  579. is_susp(ns) ? 's' : 'r',
  580. ns.aftr_isp ? 'a' : '-',
  581. ns.peer_isp ? 'p' : '-',
  582. ns.user_isp ? 'u' : '-',
  583. ns.susp_fen ? 'F' : '-',
  584. ns.susp_nod ? 'N' : '-'
  585. );
  586. }
  587. void print_st_err(struct drbd_device *device, union drbd_state os,
  588. union drbd_state ns, enum drbd_state_rv err)
  589. {
  590. if (err == SS_IN_TRANSIENT_STATE)
  591. return;
  592. drbd_err(device, "State change failed: %s\n", drbd_set_st_err_str(err));
  593. print_st(device, " state", os);
  594. print_st(device, "wanted", ns);
  595. }
  596. static long print_state_change(char *pb, union drbd_state os, union drbd_state ns,
  597. enum chg_state_flags flags)
  598. {
  599. char *pbp;
  600. pbp = pb;
  601. *pbp = 0;
  602. if (ns.role != os.role && flags & CS_DC_ROLE)
  603. pbp += sprintf(pbp, "role( %s -> %s ) ",
  604. drbd_role_str(os.role),
  605. drbd_role_str(ns.role));
  606. if (ns.peer != os.peer && flags & CS_DC_PEER)
  607. pbp += sprintf(pbp, "peer( %s -> %s ) ",
  608. drbd_role_str(os.peer),
  609. drbd_role_str(ns.peer));
  610. if (ns.conn != os.conn && flags & CS_DC_CONN)
  611. pbp += sprintf(pbp, "conn( %s -> %s ) ",
  612. drbd_conn_str(os.conn),
  613. drbd_conn_str(ns.conn));
  614. if (ns.disk != os.disk && flags & CS_DC_DISK)
  615. pbp += sprintf(pbp, "disk( %s -> %s ) ",
  616. drbd_disk_str(os.disk),
  617. drbd_disk_str(ns.disk));
  618. if (ns.pdsk != os.pdsk && flags & CS_DC_PDSK)
  619. pbp += sprintf(pbp, "pdsk( %s -> %s ) ",
  620. drbd_disk_str(os.pdsk),
  621. drbd_disk_str(ns.pdsk));
  622. return pbp - pb;
  623. }
  624. static void drbd_pr_state_change(struct drbd_device *device, union drbd_state os, union drbd_state ns,
  625. enum chg_state_flags flags)
  626. {
  627. char pb[300];
  628. char *pbp = pb;
  629. pbp += print_state_change(pbp, os, ns, flags ^ CS_DC_MASK);
  630. if (ns.aftr_isp != os.aftr_isp)
  631. pbp += sprintf(pbp, "aftr_isp( %d -> %d ) ",
  632. os.aftr_isp,
  633. ns.aftr_isp);
  634. if (ns.peer_isp != os.peer_isp)
  635. pbp += sprintf(pbp, "peer_isp( %d -> %d ) ",
  636. os.peer_isp,
  637. ns.peer_isp);
  638. if (ns.user_isp != os.user_isp)
  639. pbp += sprintf(pbp, "user_isp( %d -> %d ) ",
  640. os.user_isp,
  641. ns.user_isp);
  642. if (pbp != pb)
  643. drbd_info(device, "%s\n", pb);
  644. }
  645. static void conn_pr_state_change(struct drbd_connection *connection, union drbd_state os, union drbd_state ns,
  646. enum chg_state_flags flags)
  647. {
  648. char pb[300];
  649. char *pbp = pb;
  650. pbp += print_state_change(pbp, os, ns, flags);
  651. if (is_susp(ns) != is_susp(os) && flags & CS_DC_SUSP)
  652. pbp += sprintf(pbp, "susp( %d -> %d ) ",
  653. is_susp(os),
  654. is_susp(ns));
  655. if (pbp != pb)
  656. drbd_info(connection, "%s\n", pb);
  657. }
  658. /**
  659. * is_valid_state() - Returns an SS_ error code if ns is not valid
  660. * @device: DRBD device.
  661. * @ns: State to consider.
  662. */
  663. static enum drbd_state_rv
  664. is_valid_state(struct drbd_device *device, union drbd_state ns)
  665. {
  666. /* See drbd_state_sw_errors in drbd_strings.c */
  667. enum drbd_fencing_p fp;
  668. enum drbd_state_rv rv = SS_SUCCESS;
  669. struct net_conf *nc;
  670. rcu_read_lock();
  671. fp = FP_DONT_CARE;
  672. if (get_ldev(device)) {
  673. fp = rcu_dereference(device->ldev->disk_conf)->fencing;
  674. put_ldev(device);
  675. }
  676. nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
  677. if (nc) {
  678. if (!nc->two_primaries && ns.role == R_PRIMARY) {
  679. if (ns.peer == R_PRIMARY)
  680. rv = SS_TWO_PRIMARIES;
  681. else if (conn_highest_peer(first_peer_device(device)->connection) == R_PRIMARY)
  682. rv = SS_O_VOL_PEER_PRI;
  683. }
  684. }
  685. if (rv <= 0)
  686. goto out; /* already found a reason to abort */
  687. else if (ns.role == R_SECONDARY && device->open_cnt)
  688. rv = SS_DEVICE_IN_USE;
  689. else if (ns.role == R_PRIMARY && ns.conn < C_CONNECTED && ns.disk < D_UP_TO_DATE)
  690. rv = SS_NO_UP_TO_DATE_DISK;
  691. else if (fp >= FP_RESOURCE &&
  692. ns.role == R_PRIMARY && ns.conn < C_CONNECTED && ns.pdsk >= D_UNKNOWN)
  693. rv = SS_PRIMARY_NOP;
  694. else if (ns.role == R_PRIMARY && ns.disk <= D_INCONSISTENT && ns.pdsk <= D_INCONSISTENT)
  695. rv = SS_NO_UP_TO_DATE_DISK;
  696. else if (ns.conn > C_CONNECTED && ns.disk < D_INCONSISTENT)
  697. rv = SS_NO_LOCAL_DISK;
  698. else if (ns.conn > C_CONNECTED && ns.pdsk < D_INCONSISTENT)
  699. rv = SS_NO_REMOTE_DISK;
  700. else if (ns.conn > C_CONNECTED && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE)
  701. rv = SS_NO_UP_TO_DATE_DISK;
  702. else if ((ns.conn == C_CONNECTED ||
  703. ns.conn == C_WF_BITMAP_S ||
  704. ns.conn == C_SYNC_SOURCE ||
  705. ns.conn == C_PAUSED_SYNC_S) &&
  706. ns.disk == D_OUTDATED)
  707. rv = SS_CONNECTED_OUTDATES;
  708. else if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) &&
  709. (nc->verify_alg[0] == 0))
  710. rv = SS_NO_VERIFY_ALG;
  711. else if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) &&
  712. first_peer_device(device)->connection->agreed_pro_version < 88)
  713. rv = SS_NOT_SUPPORTED;
  714. else if (ns.role == R_PRIMARY && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE)
  715. rv = SS_NO_UP_TO_DATE_DISK;
  716. else if ((ns.conn == C_STARTING_SYNC_S || ns.conn == C_STARTING_SYNC_T) &&
  717. ns.pdsk == D_UNKNOWN)
  718. rv = SS_NEED_CONNECTION;
  719. else if (ns.conn >= C_CONNECTED && ns.pdsk == D_UNKNOWN)
  720. rv = SS_CONNECTED_OUTDATES;
  721. out:
  722. rcu_read_unlock();
  723. return rv;
  724. }
  725. /**
  726. * is_valid_soft_transition() - Returns an SS_ error code if the state transition is not possible
  727. * This function limits state transitions that may be declined by DRBD. I.e.
  728. * user requests (aka soft transitions).
  729. * @device: DRBD device.
  730. * @ns: new state.
  731. * @os: old state.
  732. */
  733. static enum drbd_state_rv
  734. is_valid_soft_transition(union drbd_state os, union drbd_state ns, struct drbd_connection *connection)
  735. {
  736. enum drbd_state_rv rv = SS_SUCCESS;
  737. if ((ns.conn == C_STARTING_SYNC_T || ns.conn == C_STARTING_SYNC_S) &&
  738. os.conn > C_CONNECTED)
  739. rv = SS_RESYNC_RUNNING;
  740. if (ns.conn == C_DISCONNECTING && os.conn == C_STANDALONE)
  741. rv = SS_ALREADY_STANDALONE;
  742. if (ns.disk > D_ATTACHING && os.disk == D_DISKLESS)
  743. rv = SS_IS_DISKLESS;
  744. if (ns.conn == C_WF_CONNECTION && os.conn < C_UNCONNECTED)
  745. rv = SS_NO_NET_CONFIG;
  746. if (ns.disk == D_OUTDATED && os.disk < D_OUTDATED && os.disk != D_ATTACHING)
  747. rv = SS_LOWER_THAN_OUTDATED;
  748. if (ns.conn == C_DISCONNECTING && os.conn == C_UNCONNECTED)
  749. rv = SS_IN_TRANSIENT_STATE;
  750. /* While establishing a connection only allow cstate to change.
  751. Delay/refuse role changes, detach attach etc... (they do not touch cstate) */
  752. if (test_bit(STATE_SENT, &connection->flags) &&
  753. !((ns.conn == C_WF_REPORT_PARAMS && os.conn == C_WF_CONNECTION) ||
  754. (ns.conn >= C_CONNECTED && os.conn == C_WF_REPORT_PARAMS)))
  755. rv = SS_IN_TRANSIENT_STATE;
  756. /* Do not promote during resync handshake triggered by "force primary".
  757. * This is a hack. It should really be rejected by the peer during the
  758. * cluster wide state change request. */
  759. if (os.role != R_PRIMARY && ns.role == R_PRIMARY
  760. && ns.pdsk == D_UP_TO_DATE
  761. && ns.disk != D_UP_TO_DATE && ns.disk != D_DISKLESS
  762. && (ns.conn <= C_WF_SYNC_UUID || ns.conn != os.conn))
  763. rv = SS_IN_TRANSIENT_STATE;
  764. if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) && os.conn < C_CONNECTED)
  765. rv = SS_NEED_CONNECTION;
  766. if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) &&
  767. ns.conn != os.conn && os.conn > C_CONNECTED)
  768. rv = SS_RESYNC_RUNNING;
  769. if ((ns.conn == C_STARTING_SYNC_S || ns.conn == C_STARTING_SYNC_T) &&
  770. os.conn < C_CONNECTED)
  771. rv = SS_NEED_CONNECTION;
  772. if ((ns.conn == C_SYNC_TARGET || ns.conn == C_SYNC_SOURCE)
  773. && os.conn < C_WF_REPORT_PARAMS)
  774. rv = SS_NEED_CONNECTION; /* No NetworkFailure -> SyncTarget etc... */
  775. if (ns.conn == C_DISCONNECTING && ns.pdsk == D_OUTDATED &&
  776. os.conn < C_CONNECTED && os.pdsk > D_OUTDATED)
  777. rv = SS_OUTDATE_WO_CONN;
  778. return rv;
  779. }
  780. static enum drbd_state_rv
  781. is_valid_conn_transition(enum drbd_conns oc, enum drbd_conns nc)
  782. {
  783. /* no change -> nothing to do, at least for the connection part */
  784. if (oc == nc)
  785. return SS_NOTHING_TO_DO;
  786. /* disconnect of an unconfigured connection does not make sense */
  787. if (oc == C_STANDALONE && nc == C_DISCONNECTING)
  788. return SS_ALREADY_STANDALONE;
  789. /* from C_STANDALONE, we start with C_UNCONNECTED */
  790. if (oc == C_STANDALONE && nc != C_UNCONNECTED)
  791. return SS_NEED_CONNECTION;
  792. /* When establishing a connection we need to go through WF_REPORT_PARAMS!
  793. Necessary to do the right thing upon invalidate-remote on a disconnected resource */
  794. if (oc < C_WF_REPORT_PARAMS && nc >= C_CONNECTED)
  795. return SS_NEED_CONNECTION;
  796. /* After a network error only C_UNCONNECTED or C_DISCONNECTING may follow. */
  797. if (oc >= C_TIMEOUT && oc <= C_TEAR_DOWN && nc != C_UNCONNECTED && nc != C_DISCONNECTING)
  798. return SS_IN_TRANSIENT_STATE;
  799. /* After C_DISCONNECTING only C_STANDALONE may follow */
  800. if (oc == C_DISCONNECTING && nc != C_STANDALONE)
  801. return SS_IN_TRANSIENT_STATE;
  802. return SS_SUCCESS;
  803. }
  804. /**
  805. * is_valid_transition() - Returns an SS_ error code if the state transition is not possible
  806. * This limits hard state transitions. Hard state transitions are facts there are
  807. * imposed on DRBD by the environment. E.g. disk broke or network broke down.
  808. * But those hard state transitions are still not allowed to do everything.
  809. * @ns: new state.
  810. * @os: old state.
  811. */
  812. static enum drbd_state_rv
  813. is_valid_transition(union drbd_state os, union drbd_state ns)
  814. {
  815. enum drbd_state_rv rv;
  816. rv = is_valid_conn_transition(os.conn, ns.conn);
  817. /* we cannot fail (again) if we already detached */
  818. if (ns.disk == D_FAILED && os.disk == D_DISKLESS)
  819. rv = SS_IS_DISKLESS;
  820. return rv;
  821. }
  822. static void print_sanitize_warnings(struct drbd_device *device, enum sanitize_state_warnings warn)
  823. {
  824. static const char *msg_table[] = {
  825. [NO_WARNING] = "",
  826. [ABORTED_ONLINE_VERIFY] = "Online-verify aborted.",
  827. [ABORTED_RESYNC] = "Resync aborted.",
  828. [CONNECTION_LOST_NEGOTIATING] = "Connection lost while negotiating, no data!",
  829. [IMPLICITLY_UPGRADED_DISK] = "Implicitly upgraded disk",
  830. [IMPLICITLY_UPGRADED_PDSK] = "Implicitly upgraded pdsk",
  831. };
  832. if (warn != NO_WARNING)
  833. drbd_warn(device, "%s\n", msg_table[warn]);
  834. }
  835. /**
  836. * sanitize_state() - Resolves implicitly necessary additional changes to a state transition
  837. * @device: DRBD device.
  838. * @os: old state.
  839. * @ns: new state.
  840. * @warn_sync_abort:
  841. *
  842. * When we loose connection, we have to set the state of the peers disk (pdsk)
  843. * to D_UNKNOWN. This rule and many more along those lines are in this function.
  844. */
  845. static union drbd_state sanitize_state(struct drbd_device *device, union drbd_state os,
  846. union drbd_state ns, enum sanitize_state_warnings *warn)
  847. {
  848. enum drbd_fencing_p fp;
  849. enum drbd_disk_state disk_min, disk_max, pdsk_min, pdsk_max;
  850. if (warn)
  851. *warn = NO_WARNING;
  852. fp = FP_DONT_CARE;
  853. if (get_ldev(device)) {
  854. rcu_read_lock();
  855. fp = rcu_dereference(device->ldev->disk_conf)->fencing;
  856. rcu_read_unlock();
  857. put_ldev(device);
  858. }
  859. /* Implications from connection to peer and peer_isp */
  860. if (ns.conn < C_CONNECTED) {
  861. ns.peer_isp = 0;
  862. ns.peer = R_UNKNOWN;
  863. if (ns.pdsk > D_UNKNOWN || ns.pdsk < D_INCONSISTENT)
  864. ns.pdsk = D_UNKNOWN;
  865. }
  866. /* Clear the aftr_isp when becoming unconfigured */
  867. if (ns.conn == C_STANDALONE && ns.disk == D_DISKLESS && ns.role == R_SECONDARY)
  868. ns.aftr_isp = 0;
  869. /* An implication of the disk states onto the connection state */
  870. /* Abort resync if a disk fails/detaches */
  871. if (ns.conn > C_CONNECTED && (ns.disk <= D_FAILED || ns.pdsk <= D_FAILED)) {
  872. if (warn)
  873. *warn = ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T ?
  874. ABORTED_ONLINE_VERIFY : ABORTED_RESYNC;
  875. ns.conn = C_CONNECTED;
  876. }
  877. /* Connection breaks down before we finished "Negotiating" */
  878. if (ns.conn < C_CONNECTED && ns.disk == D_NEGOTIATING &&
  879. get_ldev_if_state(device, D_NEGOTIATING)) {
  880. if (device->ed_uuid == device->ldev->md.uuid[UI_CURRENT]) {
  881. ns.disk = device->new_state_tmp.disk;
  882. ns.pdsk = device->new_state_tmp.pdsk;
  883. } else {
  884. if (warn)
  885. *warn = CONNECTION_LOST_NEGOTIATING;
  886. ns.disk = D_DISKLESS;
  887. ns.pdsk = D_UNKNOWN;
  888. }
  889. put_ldev(device);
  890. }
  891. /* D_CONSISTENT and D_OUTDATED vanish when we get connected */
  892. if (ns.conn >= C_CONNECTED && ns.conn < C_AHEAD) {
  893. if (ns.disk == D_CONSISTENT || ns.disk == D_OUTDATED)
  894. ns.disk = D_UP_TO_DATE;
  895. if (ns.pdsk == D_CONSISTENT || ns.pdsk == D_OUTDATED)
  896. ns.pdsk = D_UP_TO_DATE;
  897. }
  898. /* Implications of the connection stat on the disk states */
  899. disk_min = D_DISKLESS;
  900. disk_max = D_UP_TO_DATE;
  901. pdsk_min = D_INCONSISTENT;
  902. pdsk_max = D_UNKNOWN;
  903. switch ((enum drbd_conns)ns.conn) {
  904. case C_WF_BITMAP_T:
  905. case C_PAUSED_SYNC_T:
  906. case C_STARTING_SYNC_T:
  907. case C_WF_SYNC_UUID:
  908. case C_BEHIND:
  909. disk_min = D_INCONSISTENT;
  910. disk_max = D_OUTDATED;
  911. pdsk_min = D_UP_TO_DATE;
  912. pdsk_max = D_UP_TO_DATE;
  913. break;
  914. case C_VERIFY_S:
  915. case C_VERIFY_T:
  916. disk_min = D_UP_TO_DATE;
  917. disk_max = D_UP_TO_DATE;
  918. pdsk_min = D_UP_TO_DATE;
  919. pdsk_max = D_UP_TO_DATE;
  920. break;
  921. case C_CONNECTED:
  922. disk_min = D_DISKLESS;
  923. disk_max = D_UP_TO_DATE;
  924. pdsk_min = D_DISKLESS;
  925. pdsk_max = D_UP_TO_DATE;
  926. break;
  927. case C_WF_BITMAP_S:
  928. case C_PAUSED_SYNC_S:
  929. case C_STARTING_SYNC_S:
  930. case C_AHEAD:
  931. disk_min = D_UP_TO_DATE;
  932. disk_max = D_UP_TO_DATE;
  933. pdsk_min = D_INCONSISTENT;
  934. pdsk_max = D_CONSISTENT; /* D_OUTDATED would be nice. But explicit outdate necessary*/
  935. break;
  936. case C_SYNC_TARGET:
  937. disk_min = D_INCONSISTENT;
  938. disk_max = D_INCONSISTENT;
  939. pdsk_min = D_UP_TO_DATE;
  940. pdsk_max = D_UP_TO_DATE;
  941. break;
  942. case C_SYNC_SOURCE:
  943. disk_min = D_UP_TO_DATE;
  944. disk_max = D_UP_TO_DATE;
  945. pdsk_min = D_INCONSISTENT;
  946. pdsk_max = D_INCONSISTENT;
  947. break;
  948. case C_STANDALONE:
  949. case C_DISCONNECTING:
  950. case C_UNCONNECTED:
  951. case C_TIMEOUT:
  952. case C_BROKEN_PIPE:
  953. case C_NETWORK_FAILURE:
  954. case C_PROTOCOL_ERROR:
  955. case C_TEAR_DOWN:
  956. case C_WF_CONNECTION:
  957. case C_WF_REPORT_PARAMS:
  958. case C_MASK:
  959. break;
  960. }
  961. if (ns.disk > disk_max)
  962. ns.disk = disk_max;
  963. if (ns.disk < disk_min) {
  964. if (warn)
  965. *warn = IMPLICITLY_UPGRADED_DISK;
  966. ns.disk = disk_min;
  967. }
  968. if (ns.pdsk > pdsk_max)
  969. ns.pdsk = pdsk_max;
  970. if (ns.pdsk < pdsk_min) {
  971. if (warn)
  972. *warn = IMPLICITLY_UPGRADED_PDSK;
  973. ns.pdsk = pdsk_min;
  974. }
  975. if (fp == FP_STONITH &&
  976. (ns.role == R_PRIMARY && ns.conn < C_CONNECTED && ns.pdsk > D_OUTDATED) &&
  977. !(os.role == R_PRIMARY && os.conn < C_CONNECTED && os.pdsk > D_OUTDATED))
  978. ns.susp_fen = 1; /* Suspend IO while fence-peer handler runs (peer lost) */
  979. if (device->resource->res_opts.on_no_data == OND_SUSPEND_IO &&
  980. (ns.role == R_PRIMARY && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE) &&
  981. !(os.role == R_PRIMARY && os.disk < D_UP_TO_DATE && os.pdsk < D_UP_TO_DATE))
  982. ns.susp_nod = 1; /* Suspend IO while no data available (no accessible data available) */
  983. if (ns.aftr_isp || ns.peer_isp || ns.user_isp) {
  984. if (ns.conn == C_SYNC_SOURCE)
  985. ns.conn = C_PAUSED_SYNC_S;
  986. if (ns.conn == C_SYNC_TARGET)
  987. ns.conn = C_PAUSED_SYNC_T;
  988. } else {
  989. if (ns.conn == C_PAUSED_SYNC_S)
  990. ns.conn = C_SYNC_SOURCE;
  991. if (ns.conn == C_PAUSED_SYNC_T)
  992. ns.conn = C_SYNC_TARGET;
  993. }
  994. return ns;
  995. }
  996. void drbd_resume_al(struct drbd_device *device)
  997. {
  998. if (test_and_clear_bit(AL_SUSPENDED, &device->flags))
  999. drbd_info(device, "Resumed AL updates\n");
  1000. }
  1001. /* helper for _drbd_set_state */
  1002. static void set_ov_position(struct drbd_device *device, enum drbd_conns cs)
  1003. {
  1004. if (first_peer_device(device)->connection->agreed_pro_version < 90)
  1005. device->ov_start_sector = 0;
  1006. device->rs_total = drbd_bm_bits(device);
  1007. device->ov_position = 0;
  1008. if (cs == C_VERIFY_T) {
  1009. /* starting online verify from an arbitrary position
  1010. * does not fit well into the existing protocol.
  1011. * on C_VERIFY_T, we initialize ov_left and friends
  1012. * implicitly in receive_DataRequest once the
  1013. * first P_OV_REQUEST is received */
  1014. device->ov_start_sector = ~(sector_t)0;
  1015. } else {
  1016. unsigned long bit = BM_SECT_TO_BIT(device->ov_start_sector);
  1017. if (bit >= device->rs_total) {
  1018. device->ov_start_sector =
  1019. BM_BIT_TO_SECT(device->rs_total - 1);
  1020. device->rs_total = 1;
  1021. } else
  1022. device->rs_total -= bit;
  1023. device->ov_position = device->ov_start_sector;
  1024. }
  1025. device->ov_left = device->rs_total;
  1026. }
  1027. /**
  1028. * _drbd_set_state() - Set a new DRBD state
  1029. * @device: DRBD device.
  1030. * @ns: new state.
  1031. * @flags: Flags
  1032. * @done: Optional completion, that will get completed after the after_state_ch() finished
  1033. *
  1034. * Caller needs to hold req_lock. Do not call directly.
  1035. */
  1036. enum drbd_state_rv
  1037. _drbd_set_state(struct drbd_device *device, union drbd_state ns,
  1038. enum chg_state_flags flags, struct completion *done)
  1039. {
  1040. struct drbd_peer_device *peer_device = first_peer_device(device);
  1041. struct drbd_connection *connection = peer_device ? peer_device->connection : NULL;
  1042. union drbd_state os;
  1043. enum drbd_state_rv rv = SS_SUCCESS;
  1044. enum sanitize_state_warnings ssw;
  1045. struct after_state_chg_work *ascw;
  1046. struct drbd_state_change *state_change;
  1047. os = drbd_read_state(device);
  1048. ns = sanitize_state(device, os, ns, &ssw);
  1049. if (ns.i == os.i)
  1050. return SS_NOTHING_TO_DO;
  1051. rv = is_valid_transition(os, ns);
  1052. if (rv < SS_SUCCESS)
  1053. return rv;
  1054. if (!(flags & CS_HARD)) {
  1055. /* pre-state-change checks ; only look at ns */
  1056. /* See drbd_state_sw_errors in drbd_strings.c */
  1057. rv = is_valid_state(device, ns);
  1058. if (rv < SS_SUCCESS) {
  1059. /* If the old state was illegal as well, then let
  1060. this happen...*/
  1061. if (is_valid_state(device, os) == rv)
  1062. rv = is_valid_soft_transition(os, ns, connection);
  1063. } else
  1064. rv = is_valid_soft_transition(os, ns, connection);
  1065. }
  1066. if (rv < SS_SUCCESS) {
  1067. if (flags & CS_VERBOSE)
  1068. print_st_err(device, os, ns, rv);
  1069. return rv;
  1070. }
  1071. print_sanitize_warnings(device, ssw);
  1072. drbd_pr_state_change(device, os, ns, flags);
  1073. /* Display changes to the susp* flags that where caused by the call to
  1074. sanitize_state(). Only display it here if we where not called from
  1075. _conn_request_state() */
  1076. if (!(flags & CS_DC_SUSP))
  1077. conn_pr_state_change(connection, os, ns,
  1078. (flags & ~CS_DC_MASK) | CS_DC_SUSP);
  1079. /* if we are going -> D_FAILED or D_DISKLESS, grab one extra reference
  1080. * on the ldev here, to be sure the transition -> D_DISKLESS resp.
  1081. * drbd_ldev_destroy() won't happen before our corresponding
  1082. * after_state_ch works run, where we put_ldev again. */
  1083. if ((os.disk != D_FAILED && ns.disk == D_FAILED) ||
  1084. (os.disk != D_DISKLESS && ns.disk == D_DISKLESS))
  1085. atomic_inc(&device->local_cnt);
  1086. if (!is_sync_state(os.conn) && is_sync_state(ns.conn))
  1087. clear_bit(RS_DONE, &device->flags);
  1088. /* FIXME: Have any flags been set earlier in this function already? */
  1089. state_change = remember_old_state(device->resource, GFP_ATOMIC);
  1090. /* changes to local_cnt and device flags should be visible before
  1091. * changes to state, which again should be visible before anything else
  1092. * depending on that change happens. */
  1093. smp_wmb();
  1094. device->state.i = ns.i;
  1095. device->resource->susp = ns.susp;
  1096. device->resource->susp_nod = ns.susp_nod;
  1097. device->resource->susp_fen = ns.susp_fen;
  1098. smp_wmb();
  1099. remember_new_state(state_change);
  1100. /* put replicated vs not-replicated requests in seperate epochs */
  1101. if (drbd_should_do_remote((union drbd_dev_state)os.i) !=
  1102. drbd_should_do_remote((union drbd_dev_state)ns.i))
  1103. start_new_tl_epoch(connection);
  1104. if (os.disk == D_ATTACHING && ns.disk >= D_NEGOTIATING)
  1105. drbd_print_uuids(device, "attached to UUIDs");
  1106. /* Wake up role changes, that were delayed because of connection establishing */
  1107. if (os.conn == C_WF_REPORT_PARAMS && ns.conn != C_WF_REPORT_PARAMS &&
  1108. no_peer_wf_report_params(connection)) {
  1109. clear_bit(STATE_SENT, &connection->flags);
  1110. wake_up_all_devices(connection);
  1111. }
  1112. wake_up(&device->misc_wait);
  1113. wake_up(&device->state_wait);
  1114. wake_up(&connection->ping_wait);
  1115. /* Aborted verify run, or we reached the stop sector.
  1116. * Log the last position, unless end-of-device. */
  1117. if ((os.conn == C_VERIFY_S || os.conn == C_VERIFY_T) &&
  1118. ns.conn <= C_CONNECTED) {
  1119. device->ov_start_sector =
  1120. BM_BIT_TO_SECT(drbd_bm_bits(device) - device->ov_left);
  1121. if (device->ov_left)
  1122. drbd_info(device, "Online Verify reached sector %llu\n",
  1123. (unsigned long long)device->ov_start_sector);
  1124. }
  1125. if ((os.conn == C_PAUSED_SYNC_T || os.conn == C_PAUSED_SYNC_S) &&
  1126. (ns.conn == C_SYNC_TARGET || ns.conn == C_SYNC_SOURCE)) {
  1127. drbd_info(device, "Syncer continues.\n");
  1128. device->rs_paused += (long)jiffies
  1129. -(long)device->rs_mark_time[device->rs_last_mark];
  1130. if (ns.conn == C_SYNC_TARGET)
  1131. mod_timer(&device->resync_timer, jiffies);
  1132. }
  1133. if ((os.conn == C_SYNC_TARGET || os.conn == C_SYNC_SOURCE) &&
  1134. (ns.conn == C_PAUSED_SYNC_T || ns.conn == C_PAUSED_SYNC_S)) {
  1135. drbd_info(device, "Resync suspended\n");
  1136. device->rs_mark_time[device->rs_last_mark] = jiffies;
  1137. }
  1138. if (os.conn == C_CONNECTED &&
  1139. (ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T)) {
  1140. unsigned long now = jiffies;
  1141. int i;
  1142. set_ov_position(device, ns.conn);
  1143. device->rs_start = now;
  1144. device->rs_last_sect_ev = 0;
  1145. device->ov_last_oos_size = 0;
  1146. device->ov_last_oos_start = 0;
  1147. for (i = 0; i < DRBD_SYNC_MARKS; i++) {
  1148. device->rs_mark_left[i] = device->ov_left;
  1149. device->rs_mark_time[i] = now;
  1150. }
  1151. drbd_rs_controller_reset(device);
  1152. if (ns.conn == C_VERIFY_S) {
  1153. drbd_info(device, "Starting Online Verify from sector %llu\n",
  1154. (unsigned long long)device->ov_position);
  1155. mod_timer(&device->resync_timer, jiffies);
  1156. }
  1157. }
  1158. if (get_ldev(device)) {
  1159. u32 mdf = device->ldev->md.flags & ~(MDF_CONSISTENT|MDF_PRIMARY_IND|
  1160. MDF_CONNECTED_IND|MDF_WAS_UP_TO_DATE|
  1161. MDF_PEER_OUT_DATED|MDF_CRASHED_PRIMARY);
  1162. mdf &= ~MDF_AL_CLEAN;
  1163. if (test_bit(CRASHED_PRIMARY, &device->flags))
  1164. mdf |= MDF_CRASHED_PRIMARY;
  1165. if (device->state.role == R_PRIMARY ||
  1166. (device->state.pdsk < D_INCONSISTENT && device->state.peer == R_PRIMARY))
  1167. mdf |= MDF_PRIMARY_IND;
  1168. if (device->state.conn > C_WF_REPORT_PARAMS)
  1169. mdf |= MDF_CONNECTED_IND;
  1170. if (device->state.disk > D_INCONSISTENT)
  1171. mdf |= MDF_CONSISTENT;
  1172. if (device->state.disk > D_OUTDATED)
  1173. mdf |= MDF_WAS_UP_TO_DATE;
  1174. if (device->state.pdsk <= D_OUTDATED && device->state.pdsk >= D_INCONSISTENT)
  1175. mdf |= MDF_PEER_OUT_DATED;
  1176. if (mdf != device->ldev->md.flags) {
  1177. device->ldev->md.flags = mdf;
  1178. drbd_md_mark_dirty(device);
  1179. }
  1180. if (os.disk < D_CONSISTENT && ns.disk >= D_CONSISTENT)
  1181. drbd_set_ed_uuid(device, device->ldev->md.uuid[UI_CURRENT]);
  1182. put_ldev(device);
  1183. }
  1184. /* Peer was forced D_UP_TO_DATE & R_PRIMARY, consider to resync */
  1185. if (os.disk == D_INCONSISTENT && os.pdsk == D_INCONSISTENT &&
  1186. os.peer == R_SECONDARY && ns.peer == R_PRIMARY)
  1187. set_bit(CONSIDER_RESYNC, &device->flags);
  1188. /* Receiver should clean up itself */
  1189. if (os.conn != C_DISCONNECTING && ns.conn == C_DISCONNECTING)
  1190. drbd_thread_stop_nowait(&connection->receiver);
  1191. /* Now the receiver finished cleaning up itself, it should die */
  1192. if (os.conn != C_STANDALONE && ns.conn == C_STANDALONE)
  1193. drbd_thread_stop_nowait(&connection->receiver);
  1194. /* Upon network failure, we need to restart the receiver. */
  1195. if (os.conn > C_WF_CONNECTION &&
  1196. ns.conn <= C_TEAR_DOWN && ns.conn >= C_TIMEOUT)
  1197. drbd_thread_restart_nowait(&connection->receiver);
  1198. /* Resume AL writing if we get a connection */
  1199. if (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED) {
  1200. drbd_resume_al(device);
  1201. connection->connect_cnt++;
  1202. }
  1203. /* remember last attach time so request_timer_fn() won't
  1204. * kill newly established sessions while we are still trying to thaw
  1205. * previously frozen IO */
  1206. if ((os.disk == D_ATTACHING || os.disk == D_NEGOTIATING) &&
  1207. ns.disk > D_NEGOTIATING)
  1208. device->last_reattach_jif = jiffies;
  1209. ascw = kmalloc(sizeof(*ascw), GFP_ATOMIC);
  1210. if (ascw) {
  1211. ascw->os = os;
  1212. ascw->ns = ns;
  1213. ascw->flags = flags;
  1214. ascw->w.cb = w_after_state_ch;
  1215. ascw->device = device;
  1216. ascw->done = done;
  1217. ascw->state_change = state_change;
  1218. drbd_queue_work(&connection->sender_work,
  1219. &ascw->w);
  1220. } else {
  1221. drbd_err(device, "Could not kmalloc an ascw\n");
  1222. }
  1223. return rv;
  1224. }
  1225. static int w_after_state_ch(struct drbd_work *w, int unused)
  1226. {
  1227. struct after_state_chg_work *ascw =
  1228. container_of(w, struct after_state_chg_work, w);
  1229. struct drbd_device *device = ascw->device;
  1230. after_state_ch(device, ascw->os, ascw->ns, ascw->flags, ascw->state_change);
  1231. forget_state_change(ascw->state_change);
  1232. if (ascw->flags & CS_WAIT_COMPLETE)
  1233. complete(ascw->done);
  1234. kfree(ascw);
  1235. return 0;
  1236. }
  1237. static void abw_start_sync(struct drbd_device *device, int rv)
  1238. {
  1239. if (rv) {
  1240. drbd_err(device, "Writing the bitmap failed not starting resync.\n");
  1241. _drbd_request_state(device, NS(conn, C_CONNECTED), CS_VERBOSE);
  1242. return;
  1243. }
  1244. switch (device->state.conn) {
  1245. case C_STARTING_SYNC_T:
  1246. _drbd_request_state(device, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE);
  1247. break;
  1248. case C_STARTING_SYNC_S:
  1249. drbd_start_resync(device, C_SYNC_SOURCE);
  1250. break;
  1251. }
  1252. }
  1253. int drbd_bitmap_io_from_worker(struct drbd_device *device,
  1254. int (*io_fn)(struct drbd_device *),
  1255. char *why, enum bm_flag flags)
  1256. {
  1257. int rv;
  1258. D_ASSERT(device, current == first_peer_device(device)->connection->worker.task);
  1259. /* open coded non-blocking drbd_suspend_io(device); */
  1260. atomic_inc(&device->suspend_cnt);
  1261. drbd_bm_lock(device, why, flags);
  1262. rv = io_fn(device);
  1263. drbd_bm_unlock(device);
  1264. drbd_resume_io(device);
  1265. return rv;
  1266. }
  1267. void notify_resource_state_change(struct sk_buff *skb,
  1268. unsigned int seq,
  1269. struct drbd_resource_state_change *resource_state_change,
  1270. enum drbd_notification_type type)
  1271. {
  1272. struct drbd_resource *resource = resource_state_change->resource;
  1273. struct resource_info resource_info = {
  1274. .res_role = resource_state_change->role[NEW],
  1275. .res_susp = resource_state_change->susp[NEW],
  1276. .res_susp_nod = resource_state_change->susp_nod[NEW],
  1277. .res_susp_fen = resource_state_change->susp_fen[NEW],
  1278. };
  1279. notify_resource_state(skb, seq, resource, &resource_info, type);
  1280. }
  1281. void notify_connection_state_change(struct sk_buff *skb,
  1282. unsigned int seq,
  1283. struct drbd_connection_state_change *connection_state_change,
  1284. enum drbd_notification_type type)
  1285. {
  1286. struct drbd_connection *connection = connection_state_change->connection;
  1287. struct connection_info connection_info = {
  1288. .conn_connection_state = connection_state_change->cstate[NEW],
  1289. .conn_role = connection_state_change->peer_role[NEW],
  1290. };
  1291. notify_connection_state(skb, seq, connection, &connection_info, type);
  1292. }
  1293. void notify_device_state_change(struct sk_buff *skb,
  1294. unsigned int seq,
  1295. struct drbd_device_state_change *device_state_change,
  1296. enum drbd_notification_type type)
  1297. {
  1298. struct drbd_device *device = device_state_change->device;
  1299. struct device_info device_info = {
  1300. .dev_disk_state = device_state_change->disk_state[NEW],
  1301. };
  1302. notify_device_state(skb, seq, device, &device_info, type);
  1303. }
  1304. void notify_peer_device_state_change(struct sk_buff *skb,
  1305. unsigned int seq,
  1306. struct drbd_peer_device_state_change *p,
  1307. enum drbd_notification_type type)
  1308. {
  1309. struct drbd_peer_device *peer_device = p->peer_device;
  1310. struct peer_device_info peer_device_info = {
  1311. .peer_repl_state = p->repl_state[NEW],
  1312. .peer_disk_state = p->disk_state[NEW],
  1313. .peer_resync_susp_user = p->resync_susp_user[NEW],
  1314. .peer_resync_susp_peer = p->resync_susp_peer[NEW],
  1315. .peer_resync_susp_dependency = p->resync_susp_dependency[NEW],
  1316. };
  1317. notify_peer_device_state(skb, seq, peer_device, &peer_device_info, type);
  1318. }
  1319. static void broadcast_state_change(struct drbd_state_change *state_change)
  1320. {
  1321. struct drbd_resource_state_change *resource_state_change = &state_change->resource[0];
  1322. bool resource_state_has_changed;
  1323. unsigned int n_device, n_connection, n_peer_device, n_peer_devices;
  1324. void (*last_func)(struct sk_buff *, unsigned int, void *,
  1325. enum drbd_notification_type) = NULL;
  1326. void *uninitialized_var(last_arg);
  1327. #define HAS_CHANGED(state) ((state)[OLD] != (state)[NEW])
  1328. #define FINAL_STATE_CHANGE(type) \
  1329. ({ if (last_func) \
  1330. last_func(NULL, 0, last_arg, type); \
  1331. })
  1332. #define REMEMBER_STATE_CHANGE(func, arg, type) \
  1333. ({ FINAL_STATE_CHANGE(type | NOTIFY_CONTINUES); \
  1334. last_func = (typeof(last_func))func; \
  1335. last_arg = arg; \
  1336. })
  1337. mutex_lock(&notification_mutex);
  1338. resource_state_has_changed =
  1339. HAS_CHANGED(resource_state_change->role) ||
  1340. HAS_CHANGED(resource_state_change->susp) ||
  1341. HAS_CHANGED(resource_state_change->susp_nod) ||
  1342. HAS_CHANGED(resource_state_change->susp_fen);
  1343. if (resource_state_has_changed)
  1344. REMEMBER_STATE_CHANGE(notify_resource_state_change,
  1345. resource_state_change, NOTIFY_CHANGE);
  1346. for (n_connection = 0; n_connection < state_change->n_connections; n_connection++) {
  1347. struct drbd_connection_state_change *connection_state_change =
  1348. &state_change->connections[n_connection];
  1349. if (HAS_CHANGED(connection_state_change->peer_role) ||
  1350. HAS_CHANGED(connection_state_change->cstate))
  1351. REMEMBER_STATE_CHANGE(notify_connection_state_change,
  1352. connection_state_change, NOTIFY_CHANGE);
  1353. }
  1354. for (n_device = 0; n_device < state_change->n_devices; n_device++) {
  1355. struct drbd_device_state_change *device_state_change =
  1356. &state_change->devices[n_device];
  1357. if (HAS_CHANGED(device_state_change->disk_state))
  1358. REMEMBER_STATE_CHANGE(notify_device_state_change,
  1359. device_state_change, NOTIFY_CHANGE);
  1360. }
  1361. n_peer_devices = state_change->n_devices * state_change->n_connections;
  1362. for (n_peer_device = 0; n_peer_device < n_peer_devices; n_peer_device++) {
  1363. struct drbd_peer_device_state_change *p =
  1364. &state_change->peer_devices[n_peer_device];
  1365. if (HAS_CHANGED(p->disk_state) ||
  1366. HAS_CHANGED(p->repl_state) ||
  1367. HAS_CHANGED(p->resync_susp_user) ||
  1368. HAS_CHANGED(p->resync_susp_peer) ||
  1369. HAS_CHANGED(p->resync_susp_dependency))
  1370. REMEMBER_STATE_CHANGE(notify_peer_device_state_change,
  1371. p, NOTIFY_CHANGE);
  1372. }
  1373. FINAL_STATE_CHANGE(NOTIFY_CHANGE);
  1374. mutex_unlock(&notification_mutex);
  1375. #undef HAS_CHANGED
  1376. #undef FINAL_STATE_CHANGE
  1377. #undef REMEMBER_STATE_CHANGE
  1378. }
  1379. /* takes old and new peer disk state */
  1380. static bool lost_contact_to_peer_data(enum drbd_disk_state os, enum drbd_disk_state ns)
  1381. {
  1382. if ((os >= D_INCONSISTENT && os != D_UNKNOWN && os != D_OUTDATED)
  1383. && (ns < D_INCONSISTENT || ns == D_UNKNOWN || ns == D_OUTDATED))
  1384. return true;
  1385. /* Scenario, starting with normal operation
  1386. * Connected Primary/Secondary UpToDate/UpToDate
  1387. * NetworkFailure Primary/Unknown UpToDate/DUnknown (frozen)
  1388. * ...
  1389. * Connected Primary/Secondary UpToDate/Diskless (resumed; needs to bump uuid!)
  1390. */
  1391. if (os == D_UNKNOWN
  1392. && (ns == D_DISKLESS || ns == D_FAILED || ns == D_OUTDATED))
  1393. return true;
  1394. return false;
  1395. }
  1396. /**
  1397. * after_state_ch() - Perform after state change actions that may sleep
  1398. * @device: DRBD device.
  1399. * @os: old state.
  1400. * @ns: new state.
  1401. * @flags: Flags
  1402. */
  1403. static void after_state_ch(struct drbd_device *device, union drbd_state os,
  1404. union drbd_state ns, enum chg_state_flags flags,
  1405. struct drbd_state_change *state_change)
  1406. {
  1407. struct drbd_resource *resource = device->resource;
  1408. struct drbd_peer_device *peer_device = first_peer_device(device);
  1409. struct drbd_connection *connection = peer_device ? peer_device->connection : NULL;
  1410. struct sib_info sib;
  1411. broadcast_state_change(state_change);
  1412. sib.sib_reason = SIB_STATE_CHANGE;
  1413. sib.os = os;
  1414. sib.ns = ns;
  1415. if ((os.disk != D_UP_TO_DATE || os.pdsk != D_UP_TO_DATE)
  1416. && (ns.disk == D_UP_TO_DATE && ns.pdsk == D_UP_TO_DATE)) {
  1417. clear_bit(CRASHED_PRIMARY, &device->flags);
  1418. if (device->p_uuid)
  1419. device->p_uuid[UI_FLAGS] &= ~((u64)2);
  1420. }
  1421. /* Inform userspace about the change... */
  1422. drbd_bcast_event(device, &sib);
  1423. if (!(os.role == R_PRIMARY && os.disk < D_UP_TO_DATE && os.pdsk < D_UP_TO_DATE) &&
  1424. (ns.role == R_PRIMARY && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE))
  1425. drbd_khelper(device, "pri-on-incon-degr");
  1426. /* Here we have the actions that are performed after a
  1427. state change. This function might sleep */
  1428. if (ns.susp_nod) {
  1429. enum drbd_req_event what = NOTHING;
  1430. spin_lock_irq(&device->resource->req_lock);
  1431. if (os.conn < C_CONNECTED && conn_lowest_conn(connection) >= C_CONNECTED)
  1432. what = RESEND;
  1433. if ((os.disk == D_ATTACHING || os.disk == D_NEGOTIATING) &&
  1434. conn_lowest_disk(connection) == D_UP_TO_DATE)
  1435. what = RESTART_FROZEN_DISK_IO;
  1436. if (resource->susp_nod && what != NOTHING) {
  1437. _tl_restart(connection, what);
  1438. _conn_request_state(connection,
  1439. (union drbd_state) { { .susp_nod = 1 } },
  1440. (union drbd_state) { { .susp_nod = 0 } },
  1441. CS_VERBOSE);
  1442. }
  1443. spin_unlock_irq(&device->resource->req_lock);
  1444. }
  1445. if (ns.susp_fen) {
  1446. spin_lock_irq(&device->resource->req_lock);
  1447. if (resource->susp_fen && conn_lowest_conn(connection) >= C_CONNECTED) {
  1448. /* case2: The connection was established again: */
  1449. struct drbd_peer_device *peer_device;
  1450. int vnr;
  1451. rcu_read_lock();
  1452. idr_for_each_entry(&connection->peer_devices, peer_device, vnr)
  1453. clear_bit(NEW_CUR_UUID, &peer_device->device->flags);
  1454. rcu_read_unlock();
  1455. /* We should actively create a new uuid, _before_
  1456. * we resume/resent, if the peer is diskless
  1457. * (recovery from a multiple error scenario).
  1458. * Currently, this happens with a slight delay
  1459. * below when checking lost_contact_to_peer_data() ...
  1460. */
  1461. _tl_restart(connection, RESEND);
  1462. _conn_request_state(connection,
  1463. (union drbd_state) { { .susp_fen = 1 } },
  1464. (union drbd_state) { { .susp_fen = 0 } },
  1465. CS_VERBOSE);
  1466. }
  1467. spin_unlock_irq(&device->resource->req_lock);
  1468. }
  1469. /* Became sync source. With protocol >= 96, we still need to send out
  1470. * the sync uuid now. Need to do that before any drbd_send_state, or
  1471. * the other side may go "paused sync" before receiving the sync uuids,
  1472. * which is unexpected. */
  1473. if ((os.conn != C_SYNC_SOURCE && os.conn != C_PAUSED_SYNC_S) &&
  1474. (ns.conn == C_SYNC_SOURCE || ns.conn == C_PAUSED_SYNC_S) &&
  1475. connection->agreed_pro_version >= 96 && get_ldev(device)) {
  1476. drbd_gen_and_send_sync_uuid(peer_device);
  1477. put_ldev(device);
  1478. }
  1479. /* Do not change the order of the if above and the two below... */
  1480. if (os.pdsk == D_DISKLESS &&
  1481. ns.pdsk > D_DISKLESS && ns.pdsk != D_UNKNOWN) { /* attach on the peer */
  1482. /* we probably will start a resync soon.
  1483. * make sure those things are properly reset. */
  1484. device->rs_total = 0;
  1485. device->rs_failed = 0;
  1486. atomic_set(&device->rs_pending_cnt, 0);
  1487. drbd_rs_cancel_all(device);
  1488. drbd_send_uuids(peer_device);
  1489. drbd_send_state(peer_device, ns);
  1490. }
  1491. /* No point in queuing send_bitmap if we don't have a connection
  1492. * anymore, so check also the _current_ state, not only the new state
  1493. * at the time this work was queued. */
  1494. if (os.conn != C_WF_BITMAP_S && ns.conn == C_WF_BITMAP_S &&
  1495. device->state.conn == C_WF_BITMAP_S)
  1496. drbd_queue_bitmap_io(device, &drbd_send_bitmap, NULL,
  1497. "send_bitmap (WFBitMapS)",
  1498. BM_LOCKED_TEST_ALLOWED);
  1499. /* Lost contact to peer's copy of the data */
  1500. if (lost_contact_to_peer_data(os.pdsk, ns.pdsk)) {
  1501. if (get_ldev(device)) {
  1502. if ((ns.role == R_PRIMARY || ns.peer == R_PRIMARY) &&
  1503. device->ldev->md.uuid[UI_BITMAP] == 0 && ns.disk >= D_UP_TO_DATE) {
  1504. if (drbd_suspended(device)) {
  1505. set_bit(NEW_CUR_UUID, &device->flags);
  1506. } else {
  1507. drbd_uuid_new_current(device);
  1508. drbd_send_uuids(peer_device);
  1509. }
  1510. }
  1511. put_ldev(device);
  1512. }
  1513. }
  1514. if (ns.pdsk < D_INCONSISTENT && get_ldev(device)) {
  1515. if (os.peer != R_PRIMARY && ns.peer == R_PRIMARY &&
  1516. device->ldev->md.uuid[UI_BITMAP] == 0 && ns.disk >= D_UP_TO_DATE) {
  1517. drbd_uuid_new_current(device);
  1518. drbd_send_uuids(peer_device);
  1519. }
  1520. /* D_DISKLESS Peer becomes secondary */
  1521. if (os.peer == R_PRIMARY && ns.peer == R_SECONDARY)
  1522. /* We may still be Primary ourselves.
  1523. * No harm done if the bitmap still changes,
  1524. * redirtied pages will follow later. */
  1525. drbd_bitmap_io_from_worker(device, &drbd_bm_write,
  1526. "demote diskless peer", BM_LOCKED_SET_ALLOWED);
  1527. put_ldev(device);
  1528. }
  1529. /* Write out all changed bits on demote.
  1530. * Though, no need to da that just yet
  1531. * if there is a resync going on still */
  1532. if (os.role == R_PRIMARY && ns.role == R_SECONDARY &&
  1533. device->state.conn <= C_CONNECTED && get_ldev(device)) {
  1534. /* No changes to the bitmap expected this time, so assert that,
  1535. * even though no harm was done if it did change. */
  1536. drbd_bitmap_io_from_worker(device, &drbd_bm_write,
  1537. "demote", BM_LOCKED_TEST_ALLOWED);
  1538. put_ldev(device);
  1539. }
  1540. /* Last part of the attaching process ... */
  1541. if (ns.conn >= C_CONNECTED &&
  1542. os.disk == D_ATTACHING && ns.disk == D_NEGOTIATING) {
  1543. drbd_send_sizes(peer_device, 0, 0); /* to start sync... */
  1544. drbd_send_uuids(peer_device);
  1545. drbd_send_state(peer_device, ns);
  1546. }
  1547. /* We want to pause/continue resync, tell peer. */
  1548. if (ns.conn >= C_CONNECTED &&
  1549. ((os.aftr_isp != ns.aftr_isp) ||
  1550. (os.user_isp != ns.user_isp)))
  1551. drbd_send_state(peer_device, ns);
  1552. /* In case one of the isp bits got set, suspend other devices. */
  1553. if ((!os.aftr_isp && !os.peer_isp && !os.user_isp) &&
  1554. (ns.aftr_isp || ns.peer_isp || ns.user_isp))
  1555. suspend_other_sg(device);
  1556. /* Make sure the peer gets informed about eventual state
  1557. changes (ISP bits) while we were in WFReportParams. */
  1558. if (os.conn == C_WF_REPORT_PARAMS && ns.conn >= C_CONNECTED)
  1559. drbd_send_state(peer_device, ns);
  1560. if (os.conn != C_AHEAD && ns.conn == C_AHEAD)
  1561. drbd_send_state(peer_device, ns);
  1562. /* We are in the progress to start a full sync... */
  1563. if ((os.conn != C_STARTING_SYNC_T && ns.conn == C_STARTING_SYNC_T) ||
  1564. (os.conn != C_STARTING_SYNC_S && ns.conn == C_STARTING_SYNC_S))
  1565. /* no other bitmap changes expected during this phase */
  1566. drbd_queue_bitmap_io(device,
  1567. &drbd_bmio_set_n_write, &abw_start_sync,
  1568. "set_n_write from StartingSync", BM_LOCKED_TEST_ALLOWED);
  1569. /* first half of local IO error, failure to attach,
  1570. * or administrative detach */
  1571. if (os.disk != D_FAILED && ns.disk == D_FAILED) {
  1572. enum drbd_io_error_p eh = EP_PASS_ON;
  1573. int was_io_error = 0;
  1574. /* corresponding get_ldev was in _drbd_set_state, to serialize
  1575. * our cleanup here with the transition to D_DISKLESS.
  1576. * But is is still not save to dreference ldev here, since
  1577. * we might come from an failed Attach before ldev was set. */
  1578. if (device->ldev) {
  1579. rcu_read_lock();
  1580. eh = rcu_dereference(device->ldev->disk_conf)->on_io_error;
  1581. rcu_read_unlock();
  1582. was_io_error = test_and_clear_bit(WAS_IO_ERROR, &device->flags);
  1583. /* Intentionally call this handler first, before drbd_send_state().
  1584. * See: 2932204 drbd: call local-io-error handler early
  1585. * People may chose to hard-reset the box from this handler.
  1586. * It is useful if this looks like a "regular node crash". */
  1587. if (was_io_error && eh == EP_CALL_HELPER)
  1588. drbd_khelper(device, "local-io-error");
  1589. /* Immediately allow completion of all application IO,
  1590. * that waits for completion from the local disk,
  1591. * if this was a force-detach due to disk_timeout
  1592. * or administrator request (drbdsetup detach --force).
  1593. * Do NOT abort otherwise.
  1594. * Aborting local requests may cause serious problems,
  1595. * if requests are completed to upper layers already,
  1596. * and then later the already submitted local bio completes.
  1597. * This can cause DMA into former bio pages that meanwhile
  1598. * have been re-used for other things.
  1599. * So aborting local requests may cause crashes,
  1600. * or even worse, silent data corruption.
  1601. */
  1602. if (test_and_clear_bit(FORCE_DETACH, &device->flags))
  1603. tl_abort_disk_io(device);
  1604. /* current state still has to be D_FAILED,
  1605. * there is only one way out: to D_DISKLESS,
  1606. * and that may only happen after our put_ldev below. */
  1607. if (device->state.disk != D_FAILED)
  1608. drbd_err(device,
  1609. "ASSERT FAILED: disk is %s during detach\n",
  1610. drbd_disk_str(device->state.disk));
  1611. if (ns.conn >= C_CONNECTED)
  1612. drbd_send_state(peer_device, ns);
  1613. drbd_rs_cancel_all(device);
  1614. /* In case we want to get something to stable storage still,
  1615. * this may be the last chance.
  1616. * Following put_ldev may transition to D_DISKLESS. */
  1617. drbd_md_sync(device);
  1618. }
  1619. put_ldev(device);
  1620. }
  1621. /* second half of local IO error, failure to attach,
  1622. * or administrative detach,
  1623. * after local_cnt references have reached zero again */
  1624. if (os.disk != D_DISKLESS && ns.disk == D_DISKLESS) {
  1625. /* We must still be diskless,
  1626. * re-attach has to be serialized with this! */
  1627. if (device->state.disk != D_DISKLESS)
  1628. drbd_err(device,
  1629. "ASSERT FAILED: disk is %s while going diskless\n",
  1630. drbd_disk_str(device->state.disk));
  1631. if (ns.conn >= C_CONNECTED)
  1632. drbd_send_state(peer_device, ns);
  1633. /* corresponding get_ldev in __drbd_set_state
  1634. * this may finally trigger drbd_ldev_destroy. */
  1635. put_ldev(device);
  1636. }
  1637. /* Notify peer that I had a local IO error, and did not detached.. */
  1638. if (os.disk == D_UP_TO_DATE && ns.disk == D_INCONSISTENT && ns.conn >= C_CONNECTED)
  1639. drbd_send_state(peer_device, ns);
  1640. /* Disks got bigger while they were detached */
  1641. if (ns.disk > D_NEGOTIATING && ns.pdsk > D_NEGOTIATING &&
  1642. test_and_clear_bit(RESYNC_AFTER_NEG, &device->flags)) {
  1643. if (ns.conn == C_CONNECTED)
  1644. resync_after_online_grow(device);
  1645. }
  1646. /* A resync finished or aborted, wake paused devices... */
  1647. if ((os.conn > C_CONNECTED && ns.conn <= C_CONNECTED) ||
  1648. (os.peer_isp && !ns.peer_isp) ||
  1649. (os.user_isp && !ns.user_isp))
  1650. resume_next_sg(device);
  1651. /* sync target done with resync. Explicitly notify peer, even though
  1652. * it should (at least for non-empty resyncs) already know itself. */
  1653. if (os.disk < D_UP_TO_DATE && os.conn >= C_SYNC_SOURCE && ns.conn == C_CONNECTED)
  1654. drbd_send_state(peer_device, ns);
  1655. /* Verify finished, or reached stop sector. Peer did not know about
  1656. * the stop sector, and we may even have changed the stop sector during
  1657. * verify to interrupt/stop early. Send the new state. */
  1658. if (os.conn == C_VERIFY_S && ns.conn == C_CONNECTED
  1659. && verify_can_do_stop_sector(device))
  1660. drbd_send_state(peer_device, ns);
  1661. /* This triggers bitmap writeout of potentially still unwritten pages
  1662. * if the resync finished cleanly, or aborted because of peer disk
  1663. * failure, or on transition from resync back to AHEAD/BEHIND.
  1664. *
  1665. * Connection loss is handled in drbd_disconnected() by the receiver.
  1666. *
  1667. * For resync aborted because of local disk failure, we cannot do
  1668. * any bitmap writeout anymore.
  1669. *
  1670. * No harm done if some bits change during this phase.
  1671. */
  1672. if ((os.conn > C_CONNECTED && os.conn < C_AHEAD) &&
  1673. (ns.conn == C_CONNECTED || ns.conn >= C_AHEAD) && get_ldev(device)) {
  1674. drbd_queue_bitmap_io(device, &drbd_bm_write_copy_pages, NULL,
  1675. "write from resync_finished", BM_LOCKED_CHANGE_ALLOWED);
  1676. put_ldev(device);
  1677. }
  1678. if (ns.disk == D_DISKLESS &&
  1679. ns.conn == C_STANDALONE &&
  1680. ns.role == R_SECONDARY) {
  1681. if (os.aftr_isp != ns.aftr_isp)
  1682. resume_next_sg(device);
  1683. }
  1684. drbd_md_sync(device);
  1685. }
  1686. struct after_conn_state_chg_work {
  1687. struct drbd_work w;
  1688. enum drbd_conns oc;
  1689. union drbd_state ns_min;
  1690. union drbd_state ns_max; /* new, max state, over all devices */
  1691. enum chg_state_flags flags;
  1692. struct drbd_connection *connection;
  1693. struct drbd_state_change *state_change;
  1694. };
  1695. static int w_after_conn_state_ch(struct drbd_work *w, int unused)
  1696. {
  1697. struct after_conn_state_chg_work *acscw =
  1698. container_of(w, struct after_conn_state_chg_work, w);
  1699. struct drbd_connection *connection = acscw->connection;
  1700. enum drbd_conns oc = acscw->oc;
  1701. union drbd_state ns_max = acscw->ns_max;
  1702. struct drbd_peer_device *peer_device;
  1703. int vnr;
  1704. broadcast_state_change(acscw->state_change);
  1705. forget_state_change(acscw->state_change);
  1706. kfree(acscw);
  1707. /* Upon network configuration, we need to start the receiver */
  1708. if (oc == C_STANDALONE && ns_max.conn == C_UNCONNECTED)
  1709. drbd_thread_start(&connection->receiver);
  1710. if (oc == C_DISCONNECTING && ns_max.conn == C_STANDALONE) {
  1711. struct net_conf *old_conf;
  1712. mutex_lock(&notification_mutex);
  1713. idr_for_each_entry(&connection->peer_devices, peer_device, vnr)
  1714. notify_peer_device_state(NULL, 0, peer_device, NULL,
  1715. NOTIFY_DESTROY | NOTIFY_CONTINUES);
  1716. notify_connection_state(NULL, 0, connection, NULL, NOTIFY_DESTROY);
  1717. mutex_unlock(&notification_mutex);
  1718. mutex_lock(&connection->resource->conf_update);
  1719. old_conf = connection->net_conf;
  1720. connection->my_addr_len = 0;
  1721. connection->peer_addr_len = 0;
  1722. RCU_INIT_POINTER(connection->net_conf, NULL);
  1723. conn_free_crypto(connection);
  1724. mutex_unlock(&connection->resource->conf_update);
  1725. synchronize_rcu();
  1726. kfree(old_conf);
  1727. }
  1728. if (ns_max.susp_fen) {
  1729. /* case1: The outdate peer handler is successful: */
  1730. if (ns_max.pdsk <= D_OUTDATED) {
  1731. rcu_read_lock();
  1732. idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
  1733. struct drbd_device *device = peer_device->device;
  1734. if (test_bit(NEW_CUR_UUID, &device->flags)) {
  1735. drbd_uuid_new_current(device);
  1736. clear_bit(NEW_CUR_UUID, &device->flags);
  1737. }
  1738. }
  1739. rcu_read_unlock();
  1740. spin_lock_irq(&connection->resource->req_lock);
  1741. _tl_restart(connection, CONNECTION_LOST_WHILE_PENDING);
  1742. _conn_request_state(connection,
  1743. (union drbd_state) { { .susp_fen = 1 } },
  1744. (union drbd_state) { { .susp_fen = 0 } },
  1745. CS_VERBOSE);
  1746. spin_unlock_irq(&connection->resource->req_lock);
  1747. }
  1748. }
  1749. kref_put(&connection->kref, drbd_destroy_connection);
  1750. conn_md_sync(connection);
  1751. return 0;
  1752. }
  1753. static void conn_old_common_state(struct drbd_connection *connection, union drbd_state *pcs, enum chg_state_flags *pf)
  1754. {
  1755. enum chg_state_flags flags = ~0;
  1756. struct drbd_peer_device *peer_device;
  1757. int vnr, first_vol = 1;
  1758. union drbd_dev_state os, cs = {
  1759. { .role = R_SECONDARY,
  1760. .peer = R_UNKNOWN,
  1761. .conn = connection->cstate,
  1762. .disk = D_DISKLESS,
  1763. .pdsk = D_UNKNOWN,
  1764. } };
  1765. rcu_read_lock();
  1766. idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
  1767. struct drbd_device *device = peer_device->device;
  1768. os = device->state;
  1769. if (first_vol) {
  1770. cs = os;
  1771. first_vol = 0;
  1772. continue;
  1773. }
  1774. if (cs.role != os.role)
  1775. flags &= ~CS_DC_ROLE;
  1776. if (cs.peer != os.peer)
  1777. flags &= ~CS_DC_PEER;
  1778. if (cs.conn != os.conn)
  1779. flags &= ~CS_DC_CONN;
  1780. if (cs.disk != os.disk)
  1781. flags &= ~CS_DC_DISK;
  1782. if (cs.pdsk != os.pdsk)
  1783. flags &= ~CS_DC_PDSK;
  1784. }
  1785. rcu_read_unlock();
  1786. *pf |= CS_DC_MASK;
  1787. *pf &= flags;
  1788. (*pcs).i = cs.i;
  1789. }
  1790. static enum drbd_state_rv
  1791. conn_is_valid_transition(struct drbd_connection *connection, union drbd_state mask, union drbd_state val,
  1792. enum chg_state_flags flags)
  1793. {
  1794. enum drbd_state_rv rv = SS_SUCCESS;
  1795. union drbd_state ns, os;
  1796. struct drbd_peer_device *peer_device;
  1797. int vnr;
  1798. rcu_read_lock();
  1799. idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
  1800. struct drbd_device *device = peer_device->device;
  1801. os = drbd_read_state(device);
  1802. ns = sanitize_state(device, os, apply_mask_val(os, mask, val), NULL);
  1803. if (flags & CS_IGN_OUTD_FAIL && ns.disk == D_OUTDATED && os.disk < D_OUTDATED)
  1804. ns.disk = os.disk;
  1805. if (ns.i == os.i)
  1806. continue;
  1807. rv = is_valid_transition(os, ns);
  1808. if (rv >= SS_SUCCESS && !(flags & CS_HARD)) {
  1809. rv = is_valid_state(device, ns);
  1810. if (rv < SS_SUCCESS) {
  1811. if (is_valid_state(device, os) == rv)
  1812. rv = is_valid_soft_transition(os, ns, connection);
  1813. } else
  1814. rv = is_valid_soft_transition(os, ns, connection);
  1815. }
  1816. if (rv < SS_SUCCESS) {
  1817. if (flags & CS_VERBOSE)
  1818. print_st_err(device, os, ns, rv);
  1819. break;
  1820. }
  1821. }
  1822. rcu_read_unlock();
  1823. return rv;
  1824. }
  1825. static void
  1826. conn_set_state(struct drbd_connection *connection, union drbd_state mask, union drbd_state val,
  1827. union drbd_state *pns_min, union drbd_state *pns_max, enum chg_state_flags flags)
  1828. {
  1829. union drbd_state ns, os, ns_max = { };
  1830. union drbd_state ns_min = {
  1831. { .role = R_MASK,
  1832. .peer = R_MASK,
  1833. .conn = val.conn,
  1834. .disk = D_MASK,
  1835. .pdsk = D_MASK
  1836. } };
  1837. struct drbd_peer_device *peer_device;
  1838. enum drbd_state_rv rv;
  1839. int vnr, number_of_volumes = 0;
  1840. if (mask.conn == C_MASK) {
  1841. /* remember last connect time so request_timer_fn() won't
  1842. * kill newly established sessions while we are still trying to thaw
  1843. * previously frozen IO */
  1844. if (connection->cstate != C_WF_REPORT_PARAMS && val.conn == C_WF_REPORT_PARAMS)
  1845. connection->last_reconnect_jif = jiffies;
  1846. connection->cstate = val.conn;
  1847. }
  1848. rcu_read_lock();
  1849. idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
  1850. struct drbd_device *device = peer_device->device;
  1851. number_of_volumes++;
  1852. os = drbd_read_state(device);
  1853. ns = apply_mask_val(os, mask, val);
  1854. ns = sanitize_state(device, os, ns, NULL);
  1855. if (flags & CS_IGN_OUTD_FAIL && ns.disk == D_OUTDATED && os.disk < D_OUTDATED)
  1856. ns.disk = os.disk;
  1857. rv = _drbd_set_state(device, ns, flags, NULL);
  1858. BUG_ON(rv < SS_SUCCESS);
  1859. ns.i = device->state.i;
  1860. ns_max.role = max_role(ns.role, ns_max.role);
  1861. ns_max.peer = max_role(ns.peer, ns_max.peer);
  1862. ns_max.conn = max_t(enum drbd_conns, ns.conn, ns_max.conn);
  1863. ns_max.disk = max_t(enum drbd_disk_state, ns.disk, ns_max.disk);
  1864. ns_max.pdsk = max_t(enum drbd_disk_state, ns.pdsk, ns_max.pdsk);
  1865. ns_min.role = min_role(ns.role, ns_min.role);
  1866. ns_min.peer = min_role(ns.peer, ns_min.peer);
  1867. ns_min.conn = min_t(enum drbd_conns, ns.conn, ns_min.conn);
  1868. ns_min.disk = min_t(enum drbd_disk_state, ns.disk, ns_min.disk);
  1869. ns_min.pdsk = min_t(enum drbd_disk_state, ns.pdsk, ns_min.pdsk);
  1870. }
  1871. rcu_read_unlock();
  1872. if (number_of_volumes == 0) {
  1873. ns_min = ns_max = (union drbd_state) { {
  1874. .role = R_SECONDARY,
  1875. .peer = R_UNKNOWN,
  1876. .conn = val.conn,
  1877. .disk = D_DISKLESS,
  1878. .pdsk = D_UNKNOWN
  1879. } };
  1880. }
  1881. ns_min.susp = ns_max.susp = connection->resource->susp;
  1882. ns_min.susp_nod = ns_max.susp_nod = connection->resource->susp_nod;
  1883. ns_min.susp_fen = ns_max.susp_fen = connection->resource->susp_fen;
  1884. *pns_min = ns_min;
  1885. *pns_max = ns_max;
  1886. }
  1887. static enum drbd_state_rv
  1888. _conn_rq_cond(struct drbd_connection *connection, union drbd_state mask, union drbd_state val)
  1889. {
  1890. enum drbd_state_rv err, rv = SS_UNKNOWN_ERROR; /* continue waiting */;
  1891. if (test_and_clear_bit(CONN_WD_ST_CHG_OKAY, &connection->flags))
  1892. rv = SS_CW_SUCCESS;
  1893. if (test_and_clear_bit(CONN_WD_ST_CHG_FAIL, &connection->flags))
  1894. rv = SS_CW_FAILED_BY_PEER;
  1895. err = conn_is_valid_transition(connection, mask, val, 0);
  1896. if (err == SS_SUCCESS && connection->cstate == C_WF_REPORT_PARAMS)
  1897. return rv;
  1898. return err;
  1899. }
  1900. enum drbd_state_rv
  1901. _conn_request_state(struct drbd_connection *connection, union drbd_state mask, union drbd_state val,
  1902. enum chg_state_flags flags)
  1903. {
  1904. enum drbd_state_rv rv = SS_SUCCESS;
  1905. struct after_conn_state_chg_work *acscw;
  1906. enum drbd_conns oc = connection->cstate;
  1907. union drbd_state ns_max, ns_min, os;
  1908. bool have_mutex = false;
  1909. struct drbd_state_change *state_change;
  1910. if (mask.conn) {
  1911. rv = is_valid_conn_transition(oc, val.conn);
  1912. if (rv < SS_SUCCESS)
  1913. goto abort;
  1914. }
  1915. rv = conn_is_valid_transition(connection, mask, val, flags);
  1916. if (rv < SS_SUCCESS)
  1917. goto abort;
  1918. if (oc == C_WF_REPORT_PARAMS && val.conn == C_DISCONNECTING &&
  1919. !(flags & (CS_LOCAL_ONLY | CS_HARD))) {
  1920. /* This will be a cluster-wide state change.
  1921. * Need to give up the spinlock, grab the mutex,
  1922. * then send the state change request, ... */
  1923. spin_unlock_irq(&connection->resource->req_lock);
  1924. mutex_lock(&connection->cstate_mutex);
  1925. have_mutex = true;
  1926. set_bit(CONN_WD_ST_CHG_REQ, &connection->flags);
  1927. if (conn_send_state_req(connection, mask, val)) {
  1928. /* sending failed. */
  1929. clear_bit(CONN_WD_ST_CHG_REQ, &connection->flags);
  1930. rv = SS_CW_FAILED_BY_PEER;
  1931. /* need to re-aquire the spin lock, though */
  1932. goto abort_unlocked;
  1933. }
  1934. if (val.conn == C_DISCONNECTING)
  1935. set_bit(DISCONNECT_SENT, &connection->flags);
  1936. /* ... and re-aquire the spinlock.
  1937. * If _conn_rq_cond() returned >= SS_SUCCESS, we must call
  1938. * conn_set_state() within the same spinlock. */
  1939. spin_lock_irq(&connection->resource->req_lock);
  1940. wait_event_lock_irq(connection->ping_wait,
  1941. (rv = _conn_rq_cond(connection, mask, val)),
  1942. connection->resource->req_lock);
  1943. clear_bit(CONN_WD_ST_CHG_REQ, &connection->flags);
  1944. if (rv < SS_SUCCESS)
  1945. goto abort;
  1946. }
  1947. state_change = remember_old_state(connection->resource, GFP_ATOMIC);
  1948. conn_old_common_state(connection, &os, &flags);
  1949. flags |= CS_DC_SUSP;
  1950. conn_set_state(connection, mask, val, &ns_min, &ns_max, flags);
  1951. conn_pr_state_change(connection, os, ns_max, flags);
  1952. remember_new_state(state_change);
  1953. acscw = kmalloc(sizeof(*acscw), GFP_ATOMIC);
  1954. if (acscw) {
  1955. acscw->oc = os.conn;
  1956. acscw->ns_min = ns_min;
  1957. acscw->ns_max = ns_max;
  1958. acscw->flags = flags;
  1959. acscw->w.cb = w_after_conn_state_ch;
  1960. kref_get(&connection->kref);
  1961. acscw->connection = connection;
  1962. acscw->state_change = state_change;
  1963. drbd_queue_work(&connection->sender_work, &acscw->w);
  1964. } else {
  1965. drbd_err(connection, "Could not kmalloc an acscw\n");
  1966. }
  1967. abort:
  1968. if (have_mutex) {
  1969. /* mutex_unlock() "... must not be used in interrupt context.",
  1970. * so give up the spinlock, then re-aquire it */
  1971. spin_unlock_irq(&connection->resource->req_lock);
  1972. abort_unlocked:
  1973. mutex_unlock(&connection->cstate_mutex);
  1974. spin_lock_irq(&connection->resource->req_lock);
  1975. }
  1976. if (rv < SS_SUCCESS && flags & CS_VERBOSE) {
  1977. drbd_err(connection, "State change failed: %s\n", drbd_set_st_err_str(rv));
  1978. drbd_err(connection, " mask = 0x%x val = 0x%x\n", mask.i, val.i);
  1979. drbd_err(connection, " old_conn:%s wanted_conn:%s\n", drbd_conn_str(oc), drbd_conn_str(val.conn));
  1980. }
  1981. return rv;
  1982. }
  1983. enum drbd_state_rv
  1984. conn_request_state(struct drbd_connection *connection, union drbd_state mask, union drbd_state val,
  1985. enum chg_state_flags flags)
  1986. {
  1987. enum drbd_state_rv rv;
  1988. spin_lock_irq(&connection->resource->req_lock);
  1989. rv = _conn_request_state(connection, mask, val, flags);
  1990. spin_unlock_irq(&connection->resource->req_lock);
  1991. return rv;
  1992. }