drbd_state.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef DRBD_STATE_H
  3. #define DRBD_STATE_H
  4. struct drbd_device;
  5. struct drbd_connection;
  6. /**
  7. * DOC: DRBD State macros
  8. *
  9. * These macros are used to express state changes in easily readable form.
  10. *
  11. * The NS macros expand to a mask and a value, that can be bit ored onto the
  12. * current state as soon as the spinlock (req_lock) was taken.
  13. *
  14. * The _NS macros are used for state functions that get called with the
  15. * spinlock. These macros expand directly to the new state value.
  16. *
  17. * Besides the basic forms NS() and _NS() additional _?NS[23] are defined
  18. * to express state changes that affect more than one aspect of the state.
  19. *
  20. * E.g. NS2(conn, C_CONNECTED, peer, R_SECONDARY)
  21. * Means that the network connection was established and that the peer
  22. * is in secondary role.
  23. */
  24. #define role_MASK R_MASK
  25. #define peer_MASK R_MASK
  26. #define disk_MASK D_MASK
  27. #define pdsk_MASK D_MASK
  28. #define conn_MASK C_MASK
  29. #define susp_MASK 1
  30. #define user_isp_MASK 1
  31. #define aftr_isp_MASK 1
  32. #define susp_nod_MASK 1
  33. #define susp_fen_MASK 1
  34. #define NS(T, S) \
  35. ({ union drbd_state mask; mask.i = 0; mask.T = T##_MASK; mask; }), \
  36. ({ union drbd_state val; val.i = 0; val.T = (S); val; })
  37. #define NS2(T1, S1, T2, S2) \
  38. ({ union drbd_state mask; mask.i = 0; mask.T1 = T1##_MASK; \
  39. mask.T2 = T2##_MASK; mask; }), \
  40. ({ union drbd_state val; val.i = 0; val.T1 = (S1); \
  41. val.T2 = (S2); val; })
  42. #define NS3(T1, S1, T2, S2, T3, S3) \
  43. ({ union drbd_state mask; mask.i = 0; mask.T1 = T1##_MASK; \
  44. mask.T2 = T2##_MASK; mask.T3 = T3##_MASK; mask; }), \
  45. ({ union drbd_state val; val.i = 0; val.T1 = (S1); \
  46. val.T2 = (S2); val.T3 = (S3); val; })
  47. #define _NS(D, T, S) \
  48. D, ({ union drbd_state __ns; __ns = drbd_read_state(D); __ns.T = (S); __ns; })
  49. #define _NS2(D, T1, S1, T2, S2) \
  50. D, ({ union drbd_state __ns; __ns = drbd_read_state(D); __ns.T1 = (S1); \
  51. __ns.T2 = (S2); __ns; })
  52. #define _NS3(D, T1, S1, T2, S2, T3, S3) \
  53. D, ({ union drbd_state __ns; __ns = drbd_read_state(D); __ns.T1 = (S1); \
  54. __ns.T2 = (S2); __ns.T3 = (S3); __ns; })
  55. enum chg_state_flags {
  56. CS_HARD = 1 << 0,
  57. CS_VERBOSE = 1 << 1,
  58. CS_WAIT_COMPLETE = 1 << 2,
  59. CS_SERIALIZE = 1 << 3,
  60. CS_ORDERED = CS_WAIT_COMPLETE + CS_SERIALIZE,
  61. CS_LOCAL_ONLY = 1 << 4, /* Do not consider a device pair wide state change */
  62. CS_DC_ROLE = 1 << 5, /* DC = display as connection state change */
  63. CS_DC_PEER = 1 << 6,
  64. CS_DC_CONN = 1 << 7,
  65. CS_DC_DISK = 1 << 8,
  66. CS_DC_PDSK = 1 << 9,
  67. CS_DC_SUSP = 1 << 10,
  68. CS_DC_MASK = CS_DC_ROLE + CS_DC_PEER + CS_DC_CONN + CS_DC_DISK + CS_DC_PDSK,
  69. CS_IGN_OUTD_FAIL = 1 << 11,
  70. /* Make sure no meta data IO is in flight, by calling
  71. * drbd_md_get_buffer(). Used for graceful detach. */
  72. CS_INHIBIT_MD_IO = 1 << 12,
  73. };
  74. /* drbd_dev_state and drbd_state are different types. This is to stress the
  75. small difference. There is no suspended flag (.susp), and no suspended
  76. while fence handler runs flas (susp_fen). */
  77. union drbd_dev_state {
  78. struct {
  79. #if defined(__LITTLE_ENDIAN_BITFIELD)
  80. unsigned role:2 ; /* 3/4 primary/secondary/unknown */
  81. unsigned peer:2 ; /* 3/4 primary/secondary/unknown */
  82. unsigned conn:5 ; /* 17/32 cstates */
  83. unsigned disk:4 ; /* 8/16 from D_DISKLESS to D_UP_TO_DATE */
  84. unsigned pdsk:4 ; /* 8/16 from D_DISKLESS to D_UP_TO_DATE */
  85. unsigned _unused:1 ;
  86. unsigned aftr_isp:1 ; /* isp .. imposed sync pause */
  87. unsigned peer_isp:1 ;
  88. unsigned user_isp:1 ;
  89. unsigned _pad:11; /* 0 unused */
  90. #elif defined(__BIG_ENDIAN_BITFIELD)
  91. unsigned _pad:11;
  92. unsigned user_isp:1 ;
  93. unsigned peer_isp:1 ;
  94. unsigned aftr_isp:1 ; /* isp .. imposed sync pause */
  95. unsigned _unused:1 ;
  96. unsigned pdsk:4 ; /* 8/16 from D_DISKLESS to D_UP_TO_DATE */
  97. unsigned disk:4 ; /* 8/16 from D_DISKLESS to D_UP_TO_DATE */
  98. unsigned conn:5 ; /* 17/32 cstates */
  99. unsigned peer:2 ; /* 3/4 primary/secondary/unknown */
  100. unsigned role:2 ; /* 3/4 primary/secondary/unknown */
  101. #else
  102. # error "this endianess is not supported"
  103. #endif
  104. };
  105. unsigned int i;
  106. };
  107. extern enum drbd_state_rv drbd_change_state(struct drbd_device *device,
  108. enum chg_state_flags f,
  109. union drbd_state mask,
  110. union drbd_state val);
  111. extern void drbd_force_state(struct drbd_device *, union drbd_state,
  112. union drbd_state);
  113. extern enum drbd_state_rv _drbd_request_state(struct drbd_device *,
  114. union drbd_state,
  115. union drbd_state,
  116. enum chg_state_flags);
  117. extern enum drbd_state_rv
  118. _drbd_request_state_holding_state_mutex(struct drbd_device *, union drbd_state,
  119. union drbd_state, enum chg_state_flags);
  120. extern enum drbd_state_rv _drbd_set_state(struct drbd_device *, union drbd_state,
  121. enum chg_state_flags,
  122. struct completion *done);
  123. extern void print_st_err(struct drbd_device *, union drbd_state,
  124. union drbd_state, enum drbd_state_rv);
  125. enum drbd_state_rv
  126. _conn_request_state(struct drbd_connection *connection, union drbd_state mask, union drbd_state val,
  127. enum chg_state_flags flags);
  128. enum drbd_state_rv
  129. conn_request_state(struct drbd_connection *connection, union drbd_state mask, union drbd_state val,
  130. enum chg_state_flags flags);
  131. extern void drbd_resume_al(struct drbd_device *device);
  132. extern bool conn_all_vols_unconf(struct drbd_connection *connection);
  133. /**
  134. * drbd_request_state() - Request a state change
  135. * @device: DRBD device.
  136. * @mask: mask of state bits to change.
  137. * @val: value of new state bits.
  138. *
  139. * This is the most graceful way of requesting a state change. It is verbose
  140. * quite verbose in case the state change is not possible, and all those
  141. * state changes are globally serialized.
  142. */
  143. static inline int drbd_request_state(struct drbd_device *device,
  144. union drbd_state mask,
  145. union drbd_state val)
  146. {
  147. return _drbd_request_state(device, mask, val, CS_VERBOSE + CS_ORDERED);
  148. }
  149. /* for use in adm_detach() (drbd_adm_detach(), drbd_adm_down()) */
  150. int drbd_request_detach_interruptible(struct drbd_device *device);
  151. enum drbd_role conn_highest_role(struct drbd_connection *connection);
  152. enum drbd_role conn_highest_peer(struct drbd_connection *connection);
  153. enum drbd_disk_state conn_highest_disk(struct drbd_connection *connection);
  154. enum drbd_disk_state conn_lowest_disk(struct drbd_connection *connection);
  155. enum drbd_disk_state conn_highest_pdsk(struct drbd_connection *connection);
  156. enum drbd_conns conn_lowest_conn(struct drbd_connection *connection);
  157. #endif