drbd_state.c 57 KB

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