drbd_state.c 58 KB

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