drbd_state.c 74 KB

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