PageRenderTime 149ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 1ms

/drivers/block/drbd/drbd_main.c

https://bitbucket.org/cresqo/cm7-p500-kernel
C | 3829 lines | 2652 code | 617 blank | 560 comment | 742 complexity | 8755710aea72157ff27546b0bc5edfdb MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-1.0, GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. drbd.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/module.h>
  22. #include <linux/drbd.h>
  23. #include <asm/uaccess.h>
  24. #include <asm/types.h>
  25. #include <net/sock.h>
  26. #include <linux/ctype.h>
  27. #include <linux/smp_lock.h>
  28. #include <linux/fs.h>
  29. #include <linux/file.h>
  30. #include <linux/proc_fs.h>
  31. #include <linux/init.h>
  32. #include <linux/mm.h>
  33. #include <linux/memcontrol.h>
  34. #include <linux/mm_inline.h>
  35. #include <linux/slab.h>
  36. #include <linux/random.h>
  37. #include <linux/reboot.h>
  38. #include <linux/notifier.h>
  39. #include <linux/kthread.h>
  40. #define __KERNEL_SYSCALLS__
  41. #include <linux/unistd.h>
  42. #include <linux/vmalloc.h>
  43. #include <linux/drbd_limits.h>
  44. #include "drbd_int.h"
  45. #include "drbd_req.h" /* only for _req_mod in tl_release and tl_clear */
  46. #include "drbd_vli.h"
  47. struct after_state_chg_work {
  48. struct drbd_work w;
  49. union drbd_state os;
  50. union drbd_state ns;
  51. enum chg_state_flags flags;
  52. struct completion *done;
  53. };
  54. int drbdd_init(struct drbd_thread *);
  55. int drbd_worker(struct drbd_thread *);
  56. int drbd_asender(struct drbd_thread *);
  57. int drbd_init(void);
  58. static int drbd_open(struct block_device *bdev, fmode_t mode);
  59. static int drbd_release(struct gendisk *gd, fmode_t mode);
  60. static int w_after_state_ch(struct drbd_conf *mdev, struct drbd_work *w, int unused);
  61. static void after_state_ch(struct drbd_conf *mdev, union drbd_state os,
  62. union drbd_state ns, enum chg_state_flags flags);
  63. static int w_md_sync(struct drbd_conf *mdev, struct drbd_work *w, int unused);
  64. static void md_sync_timer_fn(unsigned long data);
  65. static int w_bitmap_io(struct drbd_conf *mdev, struct drbd_work *w, int unused);
  66. MODULE_AUTHOR("Philipp Reisner <phil@linbit.com>, "
  67. "Lars Ellenberg <lars@linbit.com>");
  68. MODULE_DESCRIPTION("drbd - Distributed Replicated Block Device v" REL_VERSION);
  69. MODULE_VERSION(REL_VERSION);
  70. MODULE_LICENSE("GPL");
  71. MODULE_PARM_DESC(minor_count, "Maximum number of drbd devices (1-255)");
  72. MODULE_ALIAS_BLOCKDEV_MAJOR(DRBD_MAJOR);
  73. #include <linux/moduleparam.h>
  74. /* allow_open_on_secondary */
  75. MODULE_PARM_DESC(allow_oos, "DONT USE!");
  76. /* thanks to these macros, if compiled into the kernel (not-module),
  77. * this becomes the boot parameter drbd.minor_count */
  78. module_param(minor_count, uint, 0444);
  79. module_param(disable_sendpage, bool, 0644);
  80. module_param(allow_oos, bool, 0);
  81. module_param(cn_idx, uint, 0444);
  82. module_param(proc_details, int, 0644);
  83. #ifdef CONFIG_DRBD_FAULT_INJECTION
  84. int enable_faults;
  85. int fault_rate;
  86. static int fault_count;
  87. int fault_devs;
  88. /* bitmap of enabled faults */
  89. module_param(enable_faults, int, 0664);
  90. /* fault rate % value - applies to all enabled faults */
  91. module_param(fault_rate, int, 0664);
  92. /* count of faults inserted */
  93. module_param(fault_count, int, 0664);
  94. /* bitmap of devices to insert faults on */
  95. module_param(fault_devs, int, 0644);
  96. #endif
  97. /* module parameter, defined */
  98. unsigned int minor_count = 32;
  99. int disable_sendpage;
  100. int allow_oos;
  101. unsigned int cn_idx = CN_IDX_DRBD;
  102. int proc_details; /* Detail level in proc drbd*/
  103. /* Module parameter for setting the user mode helper program
  104. * to run. Default is /sbin/drbdadm */
  105. char usermode_helper[80] = "/sbin/drbdadm";
  106. module_param_string(usermode_helper, usermode_helper, sizeof(usermode_helper), 0644);
  107. /* in 2.6.x, our device mapping and config info contains our virtual gendisks
  108. * as member "struct gendisk *vdisk;"
  109. */
  110. struct drbd_conf **minor_table;
  111. struct kmem_cache *drbd_request_cache;
  112. struct kmem_cache *drbd_ee_cache; /* epoch entries */
  113. struct kmem_cache *drbd_bm_ext_cache; /* bitmap extents */
  114. struct kmem_cache *drbd_al_ext_cache; /* activity log extents */
  115. mempool_t *drbd_request_mempool;
  116. mempool_t *drbd_ee_mempool;
  117. /* I do not use a standard mempool, because:
  118. 1) I want to hand out the pre-allocated objects first.
  119. 2) I want to be able to interrupt sleeping allocation with a signal.
  120. Note: This is a single linked list, the next pointer is the private
  121. member of struct page.
  122. */
  123. struct page *drbd_pp_pool;
  124. spinlock_t drbd_pp_lock;
  125. int drbd_pp_vacant;
  126. wait_queue_head_t drbd_pp_wait;
  127. DEFINE_RATELIMIT_STATE(drbd_ratelimit_state, 5 * HZ, 5);
  128. static const struct block_device_operations drbd_ops = {
  129. .owner = THIS_MODULE,
  130. .open = drbd_open,
  131. .release = drbd_release,
  132. };
  133. #define ARRY_SIZE(A) (sizeof(A)/sizeof(A[0]))
  134. #ifdef __CHECKER__
  135. /* When checking with sparse, and this is an inline function, sparse will
  136. give tons of false positives. When this is a real functions sparse works.
  137. */
  138. int _get_ldev_if_state(struct drbd_conf *mdev, enum drbd_disk_state mins)
  139. {
  140. int io_allowed;
  141. atomic_inc(&mdev->local_cnt);
  142. io_allowed = (mdev->state.disk >= mins);
  143. if (!io_allowed) {
  144. if (atomic_dec_and_test(&mdev->local_cnt))
  145. wake_up(&mdev->misc_wait);
  146. }
  147. return io_allowed;
  148. }
  149. #endif
  150. /**
  151. * DOC: The transfer log
  152. *
  153. * The transfer log is a single linked list of &struct drbd_tl_epoch objects.
  154. * mdev->newest_tle points to the head, mdev->oldest_tle points to the tail
  155. * of the list. There is always at least one &struct drbd_tl_epoch object.
  156. *
  157. * Each &struct drbd_tl_epoch has a circular double linked list of requests
  158. * attached.
  159. */
  160. static int tl_init(struct drbd_conf *mdev)
  161. {
  162. struct drbd_tl_epoch *b;
  163. /* during device minor initialization, we may well use GFP_KERNEL */
  164. b = kmalloc(sizeof(struct drbd_tl_epoch), GFP_KERNEL);
  165. if (!b)
  166. return 0;
  167. INIT_LIST_HEAD(&b->requests);
  168. INIT_LIST_HEAD(&b->w.list);
  169. b->next = NULL;
  170. b->br_number = 4711;
  171. b->n_req = 0;
  172. b->w.cb = NULL; /* if this is != NULL, we need to dec_ap_pending in tl_clear */
  173. mdev->oldest_tle = b;
  174. mdev->newest_tle = b;
  175. INIT_LIST_HEAD(&mdev->out_of_sequence_requests);
  176. mdev->tl_hash = NULL;
  177. mdev->tl_hash_s = 0;
  178. return 1;
  179. }
  180. static void tl_cleanup(struct drbd_conf *mdev)
  181. {
  182. D_ASSERT(mdev->oldest_tle == mdev->newest_tle);
  183. D_ASSERT(list_empty(&mdev->out_of_sequence_requests));
  184. kfree(mdev->oldest_tle);
  185. mdev->oldest_tle = NULL;
  186. kfree(mdev->unused_spare_tle);
  187. mdev->unused_spare_tle = NULL;
  188. kfree(mdev->tl_hash);
  189. mdev->tl_hash = NULL;
  190. mdev->tl_hash_s = 0;
  191. }
  192. /**
  193. * _tl_add_barrier() - Adds a barrier to the transfer log
  194. * @mdev: DRBD device.
  195. * @new: Barrier to be added before the current head of the TL.
  196. *
  197. * The caller must hold the req_lock.
  198. */
  199. void _tl_add_barrier(struct drbd_conf *mdev, struct drbd_tl_epoch *new)
  200. {
  201. struct drbd_tl_epoch *newest_before;
  202. INIT_LIST_HEAD(&new->requests);
  203. INIT_LIST_HEAD(&new->w.list);
  204. new->w.cb = NULL; /* if this is != NULL, we need to dec_ap_pending in tl_clear */
  205. new->next = NULL;
  206. new->n_req = 0;
  207. newest_before = mdev->newest_tle;
  208. /* never send a barrier number == 0, because that is special-cased
  209. * when using TCQ for our write ordering code */
  210. new->br_number = (newest_before->br_number+1) ?: 1;
  211. if (mdev->newest_tle != new) {
  212. mdev->newest_tle->next = new;
  213. mdev->newest_tle = new;
  214. }
  215. }
  216. /**
  217. * tl_release() - Free or recycle the oldest &struct drbd_tl_epoch object of the TL
  218. * @mdev: DRBD device.
  219. * @barrier_nr: Expected identifier of the DRBD write barrier packet.
  220. * @set_size: Expected number of requests before that barrier.
  221. *
  222. * In case the passed barrier_nr or set_size does not match the oldest
  223. * &struct drbd_tl_epoch objects this function will cause a termination
  224. * of the connection.
  225. */
  226. void tl_release(struct drbd_conf *mdev, unsigned int barrier_nr,
  227. unsigned int set_size)
  228. {
  229. struct drbd_tl_epoch *b, *nob; /* next old barrier */
  230. struct list_head *le, *tle;
  231. struct drbd_request *r;
  232. spin_lock_irq(&mdev->req_lock);
  233. b = mdev->oldest_tle;
  234. /* first some paranoia code */
  235. if (b == NULL) {
  236. dev_err(DEV, "BAD! BarrierAck #%u received, but no epoch in tl!?\n",
  237. barrier_nr);
  238. goto bail;
  239. }
  240. if (b->br_number != barrier_nr) {
  241. dev_err(DEV, "BAD! BarrierAck #%u received, expected #%u!\n",
  242. barrier_nr, b->br_number);
  243. goto bail;
  244. }
  245. if (b->n_req != set_size) {
  246. dev_err(DEV, "BAD! BarrierAck #%u received with n_req=%u, expected n_req=%u!\n",
  247. barrier_nr, set_size, b->n_req);
  248. goto bail;
  249. }
  250. /* Clean up list of requests processed during current epoch */
  251. list_for_each_safe(le, tle, &b->requests) {
  252. r = list_entry(le, struct drbd_request, tl_requests);
  253. _req_mod(r, barrier_acked);
  254. }
  255. /* There could be requests on the list waiting for completion
  256. of the write to the local disk. To avoid corruptions of
  257. slab's data structures we have to remove the lists head.
  258. Also there could have been a barrier ack out of sequence, overtaking
  259. the write acks - which would be a bug and violating write ordering.
  260. To not deadlock in case we lose connection while such requests are
  261. still pending, we need some way to find them for the
  262. _req_mode(connection_lost_while_pending).
  263. These have been list_move'd to the out_of_sequence_requests list in
  264. _req_mod(, barrier_acked) above.
  265. */
  266. list_del_init(&b->requests);
  267. nob = b->next;
  268. if (test_and_clear_bit(CREATE_BARRIER, &mdev->flags)) {
  269. _tl_add_barrier(mdev, b);
  270. if (nob)
  271. mdev->oldest_tle = nob;
  272. /* if nob == NULL b was the only barrier, and becomes the new
  273. barrier. Therefore mdev->oldest_tle points already to b */
  274. } else {
  275. D_ASSERT(nob != NULL);
  276. mdev->oldest_tle = nob;
  277. kfree(b);
  278. }
  279. spin_unlock_irq(&mdev->req_lock);
  280. dec_ap_pending(mdev);
  281. return;
  282. bail:
  283. spin_unlock_irq(&mdev->req_lock);
  284. drbd_force_state(mdev, NS(conn, C_PROTOCOL_ERROR));
  285. }
  286. /**
  287. * tl_clear() - Clears all requests and &struct drbd_tl_epoch objects out of the TL
  288. * @mdev: DRBD device.
  289. *
  290. * This is called after the connection to the peer was lost. The storage covered
  291. * by the requests on the transfer gets marked as our of sync. Called from the
  292. * receiver thread and the worker thread.
  293. */
  294. void tl_clear(struct drbd_conf *mdev)
  295. {
  296. struct drbd_tl_epoch *b, *tmp;
  297. struct list_head *le, *tle;
  298. struct drbd_request *r;
  299. int new_initial_bnr = net_random();
  300. spin_lock_irq(&mdev->req_lock);
  301. b = mdev->oldest_tle;
  302. while (b) {
  303. list_for_each_safe(le, tle, &b->requests) {
  304. r = list_entry(le, struct drbd_request, tl_requests);
  305. /* It would be nice to complete outside of spinlock.
  306. * But this is easier for now. */
  307. _req_mod(r, connection_lost_while_pending);
  308. }
  309. tmp = b->next;
  310. /* there could still be requests on that ring list,
  311. * in case local io is still pending */
  312. list_del(&b->requests);
  313. /* dec_ap_pending corresponding to queue_barrier.
  314. * the newest barrier may not have been queued yet,
  315. * in which case w.cb is still NULL. */
  316. if (b->w.cb != NULL)
  317. dec_ap_pending(mdev);
  318. if (b == mdev->newest_tle) {
  319. /* recycle, but reinit! */
  320. D_ASSERT(tmp == NULL);
  321. INIT_LIST_HEAD(&b->requests);
  322. INIT_LIST_HEAD(&b->w.list);
  323. b->w.cb = NULL;
  324. b->br_number = new_initial_bnr;
  325. b->n_req = 0;
  326. mdev->oldest_tle = b;
  327. break;
  328. }
  329. kfree(b);
  330. b = tmp;
  331. }
  332. /* we expect this list to be empty. */
  333. D_ASSERT(list_empty(&mdev->out_of_sequence_requests));
  334. /* but just in case, clean it up anyways! */
  335. list_for_each_safe(le, tle, &mdev->out_of_sequence_requests) {
  336. r = list_entry(le, struct drbd_request, tl_requests);
  337. /* It would be nice to complete outside of spinlock.
  338. * But this is easier for now. */
  339. _req_mod(r, connection_lost_while_pending);
  340. }
  341. /* ensure bit indicating barrier is required is clear */
  342. clear_bit(CREATE_BARRIER, &mdev->flags);
  343. spin_unlock_irq(&mdev->req_lock);
  344. }
  345. /**
  346. * cl_wide_st_chg() - TRUE if the state change is a cluster wide one
  347. * @mdev: DRBD device.
  348. * @os: old (current) state.
  349. * @ns: new (wanted) state.
  350. */
  351. static int cl_wide_st_chg(struct drbd_conf *mdev,
  352. union drbd_state os, union drbd_state ns)
  353. {
  354. return (os.conn >= C_CONNECTED && ns.conn >= C_CONNECTED &&
  355. ((os.role != R_PRIMARY && ns.role == R_PRIMARY) ||
  356. (os.conn != C_STARTING_SYNC_T && ns.conn == C_STARTING_SYNC_T) ||
  357. (os.conn != C_STARTING_SYNC_S && ns.conn == C_STARTING_SYNC_S) ||
  358. (os.disk != D_DISKLESS && ns.disk == D_DISKLESS))) ||
  359. (os.conn >= C_CONNECTED && ns.conn == C_DISCONNECTING) ||
  360. (os.conn == C_CONNECTED && ns.conn == C_VERIFY_S);
  361. }
  362. int drbd_change_state(struct drbd_conf *mdev, enum chg_state_flags f,
  363. union drbd_state mask, union drbd_state val)
  364. {
  365. unsigned long flags;
  366. union drbd_state os, ns;
  367. int rv;
  368. spin_lock_irqsave(&mdev->req_lock, flags);
  369. os = mdev->state;
  370. ns.i = (os.i & ~mask.i) | val.i;
  371. rv = _drbd_set_state(mdev, ns, f, NULL);
  372. ns = mdev->state;
  373. spin_unlock_irqrestore(&mdev->req_lock, flags);
  374. return rv;
  375. }
  376. /**
  377. * drbd_force_state() - Impose a change which happens outside our control on our state
  378. * @mdev: DRBD device.
  379. * @mask: mask of state bits to change.
  380. * @val: value of new state bits.
  381. */
  382. void drbd_force_state(struct drbd_conf *mdev,
  383. union drbd_state mask, union drbd_state val)
  384. {
  385. drbd_change_state(mdev, CS_HARD, mask, val);
  386. }
  387. static int is_valid_state(struct drbd_conf *mdev, union drbd_state ns);
  388. static int is_valid_state_transition(struct drbd_conf *,
  389. union drbd_state, union drbd_state);
  390. static union drbd_state sanitize_state(struct drbd_conf *mdev, union drbd_state os,
  391. union drbd_state ns, int *warn_sync_abort);
  392. int drbd_send_state_req(struct drbd_conf *,
  393. union drbd_state, union drbd_state);
  394. static enum drbd_state_ret_codes _req_st_cond(struct drbd_conf *mdev,
  395. union drbd_state mask, union drbd_state val)
  396. {
  397. union drbd_state os, ns;
  398. unsigned long flags;
  399. int rv;
  400. if (test_and_clear_bit(CL_ST_CHG_SUCCESS, &mdev->flags))
  401. return SS_CW_SUCCESS;
  402. if (test_and_clear_bit(CL_ST_CHG_FAIL, &mdev->flags))
  403. return SS_CW_FAILED_BY_PEER;
  404. rv = 0;
  405. spin_lock_irqsave(&mdev->req_lock, flags);
  406. os = mdev->state;
  407. ns.i = (os.i & ~mask.i) | val.i;
  408. ns = sanitize_state(mdev, os, ns, NULL);
  409. if (!cl_wide_st_chg(mdev, os, ns))
  410. rv = SS_CW_NO_NEED;
  411. if (!rv) {
  412. rv = is_valid_state(mdev, ns);
  413. if (rv == SS_SUCCESS) {
  414. rv = is_valid_state_transition(mdev, ns, os);
  415. if (rv == SS_SUCCESS)
  416. rv = 0; /* cont waiting, otherwise fail. */
  417. }
  418. }
  419. spin_unlock_irqrestore(&mdev->req_lock, flags);
  420. return rv;
  421. }
  422. /**
  423. * drbd_req_state() - Perform an eventually cluster wide state change
  424. * @mdev: DRBD device.
  425. * @mask: mask of state bits to change.
  426. * @val: value of new state bits.
  427. * @f: flags
  428. *
  429. * Should not be called directly, use drbd_request_state() or
  430. * _drbd_request_state().
  431. */
  432. static int drbd_req_state(struct drbd_conf *mdev,
  433. union drbd_state mask, union drbd_state val,
  434. enum chg_state_flags f)
  435. {
  436. struct completion done;
  437. unsigned long flags;
  438. union drbd_state os, ns;
  439. int rv;
  440. init_completion(&done);
  441. if (f & CS_SERIALIZE)
  442. mutex_lock(&mdev->state_mutex);
  443. spin_lock_irqsave(&mdev->req_lock, flags);
  444. os = mdev->state;
  445. ns.i = (os.i & ~mask.i) | val.i;
  446. ns = sanitize_state(mdev, os, ns, NULL);
  447. if (cl_wide_st_chg(mdev, os, ns)) {
  448. rv = is_valid_state(mdev, ns);
  449. if (rv == SS_SUCCESS)
  450. rv = is_valid_state_transition(mdev, ns, os);
  451. spin_unlock_irqrestore(&mdev->req_lock, flags);
  452. if (rv < SS_SUCCESS) {
  453. if (f & CS_VERBOSE)
  454. print_st_err(mdev, os, ns, rv);
  455. goto abort;
  456. }
  457. drbd_state_lock(mdev);
  458. if (!drbd_send_state_req(mdev, mask, val)) {
  459. drbd_state_unlock(mdev);
  460. rv = SS_CW_FAILED_BY_PEER;
  461. if (f & CS_VERBOSE)
  462. print_st_err(mdev, os, ns, rv);
  463. goto abort;
  464. }
  465. wait_event(mdev->state_wait,
  466. (rv = _req_st_cond(mdev, mask, val)));
  467. if (rv < SS_SUCCESS) {
  468. drbd_state_unlock(mdev);
  469. if (f & CS_VERBOSE)
  470. print_st_err(mdev, os, ns, rv);
  471. goto abort;
  472. }
  473. spin_lock_irqsave(&mdev->req_lock, flags);
  474. os = mdev->state;
  475. ns.i = (os.i & ~mask.i) | val.i;
  476. rv = _drbd_set_state(mdev, ns, f, &done);
  477. drbd_state_unlock(mdev);
  478. } else {
  479. rv = _drbd_set_state(mdev, ns, f, &done);
  480. }
  481. spin_unlock_irqrestore(&mdev->req_lock, flags);
  482. if (f & CS_WAIT_COMPLETE && rv == SS_SUCCESS) {
  483. D_ASSERT(current != mdev->worker.task);
  484. wait_for_completion(&done);
  485. }
  486. abort:
  487. if (f & CS_SERIALIZE)
  488. mutex_unlock(&mdev->state_mutex);
  489. return rv;
  490. }
  491. /**
  492. * _drbd_request_state() - Request a state change (with flags)
  493. * @mdev: DRBD device.
  494. * @mask: mask of state bits to change.
  495. * @val: value of new state bits.
  496. * @f: flags
  497. *
  498. * Cousin of drbd_request_state(), useful with the CS_WAIT_COMPLETE
  499. * flag, or when logging of failed state change requests is not desired.
  500. */
  501. int _drbd_request_state(struct drbd_conf *mdev, union drbd_state mask,
  502. union drbd_state val, enum chg_state_flags f)
  503. {
  504. int rv;
  505. wait_event(mdev->state_wait,
  506. (rv = drbd_req_state(mdev, mask, val, f)) != SS_IN_TRANSIENT_STATE);
  507. return rv;
  508. }
  509. static void print_st(struct drbd_conf *mdev, char *name, union drbd_state ns)
  510. {
  511. dev_err(DEV, " %s = { cs:%s ro:%s/%s ds:%s/%s %c%c%c%c }\n",
  512. name,
  513. drbd_conn_str(ns.conn),
  514. drbd_role_str(ns.role),
  515. drbd_role_str(ns.peer),
  516. drbd_disk_str(ns.disk),
  517. drbd_disk_str(ns.pdsk),
  518. ns.susp ? 's' : 'r',
  519. ns.aftr_isp ? 'a' : '-',
  520. ns.peer_isp ? 'p' : '-',
  521. ns.user_isp ? 'u' : '-'
  522. );
  523. }
  524. void print_st_err(struct drbd_conf *mdev,
  525. union drbd_state os, union drbd_state ns, int err)
  526. {
  527. if (err == SS_IN_TRANSIENT_STATE)
  528. return;
  529. dev_err(DEV, "State change failed: %s\n", drbd_set_st_err_str(err));
  530. print_st(mdev, " state", os);
  531. print_st(mdev, "wanted", ns);
  532. }
  533. #define drbd_peer_str drbd_role_str
  534. #define drbd_pdsk_str drbd_disk_str
  535. #define drbd_susp_str(A) ((A) ? "1" : "0")
  536. #define drbd_aftr_isp_str(A) ((A) ? "1" : "0")
  537. #define drbd_peer_isp_str(A) ((A) ? "1" : "0")
  538. #define drbd_user_isp_str(A) ((A) ? "1" : "0")
  539. #define PSC(A) \
  540. ({ if (ns.A != os.A) { \
  541. pbp += sprintf(pbp, #A "( %s -> %s ) ", \
  542. drbd_##A##_str(os.A), \
  543. drbd_##A##_str(ns.A)); \
  544. } })
  545. /**
  546. * is_valid_state() - Returns an SS_ error code if ns is not valid
  547. * @mdev: DRBD device.
  548. * @ns: State to consider.
  549. */
  550. static int is_valid_state(struct drbd_conf *mdev, union drbd_state ns)
  551. {
  552. /* See drbd_state_sw_errors in drbd_strings.c */
  553. enum drbd_fencing_p fp;
  554. int rv = SS_SUCCESS;
  555. fp = FP_DONT_CARE;
  556. if (get_ldev(mdev)) {
  557. fp = mdev->ldev->dc.fencing;
  558. put_ldev(mdev);
  559. }
  560. if (get_net_conf(mdev)) {
  561. if (!mdev->net_conf->two_primaries &&
  562. ns.role == R_PRIMARY && ns.peer == R_PRIMARY)
  563. rv = SS_TWO_PRIMARIES;
  564. put_net_conf(mdev);
  565. }
  566. if (rv <= 0)
  567. /* already found a reason to abort */;
  568. else if (ns.role == R_SECONDARY && mdev->open_cnt)
  569. rv = SS_DEVICE_IN_USE;
  570. else if (ns.role == R_PRIMARY && ns.conn < C_CONNECTED && ns.disk < D_UP_TO_DATE)
  571. rv = SS_NO_UP_TO_DATE_DISK;
  572. else if (fp >= FP_RESOURCE &&
  573. ns.role == R_PRIMARY && ns.conn < C_CONNECTED && ns.pdsk >= D_UNKNOWN)
  574. rv = SS_PRIMARY_NOP;
  575. else if (ns.role == R_PRIMARY && ns.disk <= D_INCONSISTENT && ns.pdsk <= D_INCONSISTENT)
  576. rv = SS_NO_UP_TO_DATE_DISK;
  577. else if (ns.conn > C_CONNECTED && ns.disk < D_INCONSISTENT)
  578. rv = SS_NO_LOCAL_DISK;
  579. else if (ns.conn > C_CONNECTED && ns.pdsk < D_INCONSISTENT)
  580. rv = SS_NO_REMOTE_DISK;
  581. else if (ns.conn > C_CONNECTED && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE)
  582. rv = SS_NO_UP_TO_DATE_DISK;
  583. else if ((ns.conn == C_CONNECTED ||
  584. ns.conn == C_WF_BITMAP_S ||
  585. ns.conn == C_SYNC_SOURCE ||
  586. ns.conn == C_PAUSED_SYNC_S) &&
  587. ns.disk == D_OUTDATED)
  588. rv = SS_CONNECTED_OUTDATES;
  589. else if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) &&
  590. (mdev->sync_conf.verify_alg[0] == 0))
  591. rv = SS_NO_VERIFY_ALG;
  592. else if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) &&
  593. mdev->agreed_pro_version < 88)
  594. rv = SS_NOT_SUPPORTED;
  595. return rv;
  596. }
  597. /**
  598. * is_valid_state_transition() - Returns an SS_ error code if the state transition is not possible
  599. * @mdev: DRBD device.
  600. * @ns: new state.
  601. * @os: old state.
  602. */
  603. static int is_valid_state_transition(struct drbd_conf *mdev,
  604. union drbd_state ns, union drbd_state os)
  605. {
  606. int rv = SS_SUCCESS;
  607. if ((ns.conn == C_STARTING_SYNC_T || ns.conn == C_STARTING_SYNC_S) &&
  608. os.conn > C_CONNECTED)
  609. rv = SS_RESYNC_RUNNING;
  610. if (ns.conn == C_DISCONNECTING && os.conn == C_STANDALONE)
  611. rv = SS_ALREADY_STANDALONE;
  612. if (ns.disk > D_ATTACHING && os.disk == D_DISKLESS)
  613. rv = SS_IS_DISKLESS;
  614. if (ns.conn == C_WF_CONNECTION && os.conn < C_UNCONNECTED)
  615. rv = SS_NO_NET_CONFIG;
  616. if (ns.disk == D_OUTDATED && os.disk < D_OUTDATED && os.disk != D_ATTACHING)
  617. rv = SS_LOWER_THAN_OUTDATED;
  618. if (ns.conn == C_DISCONNECTING && os.conn == C_UNCONNECTED)
  619. rv = SS_IN_TRANSIENT_STATE;
  620. if (ns.conn == os.conn && ns.conn == C_WF_REPORT_PARAMS)
  621. rv = SS_IN_TRANSIENT_STATE;
  622. if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) && os.conn < C_CONNECTED)
  623. rv = SS_NEED_CONNECTION;
  624. if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) &&
  625. ns.conn != os.conn && os.conn > C_CONNECTED)
  626. rv = SS_RESYNC_RUNNING;
  627. if ((ns.conn == C_STARTING_SYNC_S || ns.conn == C_STARTING_SYNC_T) &&
  628. os.conn < C_CONNECTED)
  629. rv = SS_NEED_CONNECTION;
  630. return rv;
  631. }
  632. /**
  633. * sanitize_state() - Resolves implicitly necessary additional changes to a state transition
  634. * @mdev: DRBD device.
  635. * @os: old state.
  636. * @ns: new state.
  637. * @warn_sync_abort:
  638. *
  639. * When we loose connection, we have to set the state of the peers disk (pdsk)
  640. * to D_UNKNOWN. This rule and many more along those lines are in this function.
  641. */
  642. static union drbd_state sanitize_state(struct drbd_conf *mdev, union drbd_state os,
  643. union drbd_state ns, int *warn_sync_abort)
  644. {
  645. enum drbd_fencing_p fp;
  646. fp = FP_DONT_CARE;
  647. if (get_ldev(mdev)) {
  648. fp = mdev->ldev->dc.fencing;
  649. put_ldev(mdev);
  650. }
  651. /* Disallow Network errors to configure a device's network part */
  652. if ((ns.conn >= C_TIMEOUT && ns.conn <= C_TEAR_DOWN) &&
  653. os.conn <= C_DISCONNECTING)
  654. ns.conn = os.conn;
  655. /* After a network error (+C_TEAR_DOWN) only C_UNCONNECTED or C_DISCONNECTING can follow */
  656. if (os.conn >= C_TIMEOUT && os.conn <= C_TEAR_DOWN &&
  657. ns.conn != C_UNCONNECTED && ns.conn != C_DISCONNECTING)
  658. ns.conn = os.conn;
  659. /* After C_DISCONNECTING only C_STANDALONE may follow */
  660. if (os.conn == C_DISCONNECTING && ns.conn != C_STANDALONE)
  661. ns.conn = os.conn;
  662. if (ns.conn < C_CONNECTED) {
  663. ns.peer_isp = 0;
  664. ns.peer = R_UNKNOWN;
  665. if (ns.pdsk > D_UNKNOWN || ns.pdsk < D_INCONSISTENT)
  666. ns.pdsk = D_UNKNOWN;
  667. }
  668. /* Clear the aftr_isp when becoming unconfigured */
  669. if (ns.conn == C_STANDALONE && ns.disk == D_DISKLESS && ns.role == R_SECONDARY)
  670. ns.aftr_isp = 0;
  671. if (ns.conn <= C_DISCONNECTING && ns.disk == D_DISKLESS)
  672. ns.pdsk = D_UNKNOWN;
  673. /* Abort resync if a disk fails/detaches */
  674. if (os.conn > C_CONNECTED && ns.conn > C_CONNECTED &&
  675. (ns.disk <= D_FAILED || ns.pdsk <= D_FAILED)) {
  676. if (warn_sync_abort)
  677. *warn_sync_abort = 1;
  678. ns.conn = C_CONNECTED;
  679. }
  680. if (ns.conn >= C_CONNECTED &&
  681. ((ns.disk == D_CONSISTENT || ns.disk == D_OUTDATED) ||
  682. (ns.disk == D_NEGOTIATING && ns.conn == C_WF_BITMAP_T))) {
  683. switch (ns.conn) {
  684. case C_WF_BITMAP_T:
  685. case C_PAUSED_SYNC_T:
  686. ns.disk = D_OUTDATED;
  687. break;
  688. case C_CONNECTED:
  689. case C_WF_BITMAP_S:
  690. case C_SYNC_SOURCE:
  691. case C_PAUSED_SYNC_S:
  692. ns.disk = D_UP_TO_DATE;
  693. break;
  694. case C_SYNC_TARGET:
  695. ns.disk = D_INCONSISTENT;
  696. dev_warn(DEV, "Implicitly set disk state Inconsistent!\n");
  697. break;
  698. }
  699. if (os.disk == D_OUTDATED && ns.disk == D_UP_TO_DATE)
  700. dev_warn(DEV, "Implicitly set disk from Outdated to UpToDate\n");
  701. }
  702. if (ns.conn >= C_CONNECTED &&
  703. (ns.pdsk == D_CONSISTENT || ns.pdsk == D_OUTDATED)) {
  704. switch (ns.conn) {
  705. case C_CONNECTED:
  706. case C_WF_BITMAP_T:
  707. case C_PAUSED_SYNC_T:
  708. case C_SYNC_TARGET:
  709. ns.pdsk = D_UP_TO_DATE;
  710. break;
  711. case C_WF_BITMAP_S:
  712. case C_PAUSED_SYNC_S:
  713. /* remap any consistent state to D_OUTDATED,
  714. * but disallow "upgrade" of not even consistent states.
  715. */
  716. ns.pdsk =
  717. (D_DISKLESS < os.pdsk && os.pdsk < D_OUTDATED)
  718. ? os.pdsk : D_OUTDATED;
  719. break;
  720. case C_SYNC_SOURCE:
  721. ns.pdsk = D_INCONSISTENT;
  722. dev_warn(DEV, "Implicitly set pdsk Inconsistent!\n");
  723. break;
  724. }
  725. if (os.pdsk == D_OUTDATED && ns.pdsk == D_UP_TO_DATE)
  726. dev_warn(DEV, "Implicitly set pdsk from Outdated to UpToDate\n");
  727. }
  728. /* Connection breaks down before we finished "Negotiating" */
  729. if (ns.conn < C_CONNECTED && ns.disk == D_NEGOTIATING &&
  730. get_ldev_if_state(mdev, D_NEGOTIATING)) {
  731. if (mdev->ed_uuid == mdev->ldev->md.uuid[UI_CURRENT]) {
  732. ns.disk = mdev->new_state_tmp.disk;
  733. ns.pdsk = mdev->new_state_tmp.pdsk;
  734. } else {
  735. dev_alert(DEV, "Connection lost while negotiating, no data!\n");
  736. ns.disk = D_DISKLESS;
  737. ns.pdsk = D_UNKNOWN;
  738. }
  739. put_ldev(mdev);
  740. }
  741. if (fp == FP_STONITH &&
  742. (ns.role == R_PRIMARY && ns.conn < C_CONNECTED && ns.pdsk > D_OUTDATED) &&
  743. !(os.role == R_PRIMARY && os.conn < C_CONNECTED && os.pdsk > D_OUTDATED))
  744. ns.susp = 1;
  745. if (ns.aftr_isp || ns.peer_isp || ns.user_isp) {
  746. if (ns.conn == C_SYNC_SOURCE)
  747. ns.conn = C_PAUSED_SYNC_S;
  748. if (ns.conn == C_SYNC_TARGET)
  749. ns.conn = C_PAUSED_SYNC_T;
  750. } else {
  751. if (ns.conn == C_PAUSED_SYNC_S)
  752. ns.conn = C_SYNC_SOURCE;
  753. if (ns.conn == C_PAUSED_SYNC_T)
  754. ns.conn = C_SYNC_TARGET;
  755. }
  756. return ns;
  757. }
  758. /* helper for __drbd_set_state */
  759. static void set_ov_position(struct drbd_conf *mdev, enum drbd_conns cs)
  760. {
  761. if (cs == C_VERIFY_T) {
  762. /* starting online verify from an arbitrary position
  763. * does not fit well into the existing protocol.
  764. * on C_VERIFY_T, we initialize ov_left and friends
  765. * implicitly in receive_DataRequest once the
  766. * first P_OV_REQUEST is received */
  767. mdev->ov_start_sector = ~(sector_t)0;
  768. } else {
  769. unsigned long bit = BM_SECT_TO_BIT(mdev->ov_start_sector);
  770. if (bit >= mdev->rs_total)
  771. mdev->ov_start_sector =
  772. BM_BIT_TO_SECT(mdev->rs_total - 1);
  773. mdev->ov_position = mdev->ov_start_sector;
  774. }
  775. }
  776. /**
  777. * __drbd_set_state() - Set a new DRBD state
  778. * @mdev: DRBD device.
  779. * @ns: new state.
  780. * @flags: Flags
  781. * @done: Optional completion, that will get completed after the after_state_ch() finished
  782. *
  783. * Caller needs to hold req_lock, and global_state_lock. Do not call directly.
  784. */
  785. int __drbd_set_state(struct drbd_conf *mdev,
  786. union drbd_state ns, enum chg_state_flags flags,
  787. struct completion *done)
  788. {
  789. union drbd_state os;
  790. int rv = SS_SUCCESS;
  791. int warn_sync_abort = 0;
  792. struct after_state_chg_work *ascw;
  793. os = mdev->state;
  794. ns = sanitize_state(mdev, os, ns, &warn_sync_abort);
  795. if (ns.i == os.i)
  796. return SS_NOTHING_TO_DO;
  797. if (!(flags & CS_HARD)) {
  798. /* pre-state-change checks ; only look at ns */
  799. /* See drbd_state_sw_errors in drbd_strings.c */
  800. rv = is_valid_state(mdev, ns);
  801. if (rv < SS_SUCCESS) {
  802. /* If the old state was illegal as well, then let
  803. this happen...*/
  804. if (is_valid_state(mdev, os) == rv) {
  805. dev_err(DEV, "Considering state change from bad state. "
  806. "Error would be: '%s'\n",
  807. drbd_set_st_err_str(rv));
  808. print_st(mdev, "old", os);
  809. print_st(mdev, "new", ns);
  810. rv = is_valid_state_transition(mdev, ns, os);
  811. }
  812. } else
  813. rv = is_valid_state_transition(mdev, ns, os);
  814. }
  815. if (rv < SS_SUCCESS) {
  816. if (flags & CS_VERBOSE)
  817. print_st_err(mdev, os, ns, rv);
  818. return rv;
  819. }
  820. if (warn_sync_abort)
  821. dev_warn(DEV, "Resync aborted.\n");
  822. {
  823. char *pbp, pb[300];
  824. pbp = pb;
  825. *pbp = 0;
  826. PSC(role);
  827. PSC(peer);
  828. PSC(conn);
  829. PSC(disk);
  830. PSC(pdsk);
  831. PSC(susp);
  832. PSC(aftr_isp);
  833. PSC(peer_isp);
  834. PSC(user_isp);
  835. dev_info(DEV, "%s\n", pb);
  836. }
  837. /* solve the race between becoming unconfigured,
  838. * worker doing the cleanup, and
  839. * admin reconfiguring us:
  840. * on (re)configure, first set CONFIG_PENDING,
  841. * then wait for a potentially exiting worker,
  842. * start the worker, and schedule one no_op.
  843. * then proceed with configuration.
  844. */
  845. if (ns.disk == D_DISKLESS &&
  846. ns.conn == C_STANDALONE &&
  847. ns.role == R_SECONDARY &&
  848. !test_and_set_bit(CONFIG_PENDING, &mdev->flags))
  849. set_bit(DEVICE_DYING, &mdev->flags);
  850. mdev->state.i = ns.i;
  851. wake_up(&mdev->misc_wait);
  852. wake_up(&mdev->state_wait);
  853. /* post-state-change actions */
  854. if (os.conn >= C_SYNC_SOURCE && ns.conn <= C_CONNECTED) {
  855. set_bit(STOP_SYNC_TIMER, &mdev->flags);
  856. mod_timer(&mdev->resync_timer, jiffies);
  857. }
  858. /* aborted verify run. log the last position */
  859. if ((os.conn == C_VERIFY_S || os.conn == C_VERIFY_T) &&
  860. ns.conn < C_CONNECTED) {
  861. mdev->ov_start_sector =
  862. BM_BIT_TO_SECT(mdev->rs_total - mdev->ov_left);
  863. dev_info(DEV, "Online Verify reached sector %llu\n",
  864. (unsigned long long)mdev->ov_start_sector);
  865. }
  866. if ((os.conn == C_PAUSED_SYNC_T || os.conn == C_PAUSED_SYNC_S) &&
  867. (ns.conn == C_SYNC_TARGET || ns.conn == C_SYNC_SOURCE)) {
  868. dev_info(DEV, "Syncer continues.\n");
  869. mdev->rs_paused += (long)jiffies-(long)mdev->rs_mark_time;
  870. if (ns.conn == C_SYNC_TARGET) {
  871. if (!test_and_clear_bit(STOP_SYNC_TIMER, &mdev->flags))
  872. mod_timer(&mdev->resync_timer, jiffies);
  873. /* This if (!test_bit) is only needed for the case
  874. that a device that has ceased to used its timer,
  875. i.e. it is already in drbd_resync_finished() gets
  876. paused and resumed. */
  877. }
  878. }
  879. if ((os.conn == C_SYNC_TARGET || os.conn == C_SYNC_SOURCE) &&
  880. (ns.conn == C_PAUSED_SYNC_T || ns.conn == C_PAUSED_SYNC_S)) {
  881. dev_info(DEV, "Resync suspended\n");
  882. mdev->rs_mark_time = jiffies;
  883. if (ns.conn == C_PAUSED_SYNC_T)
  884. set_bit(STOP_SYNC_TIMER, &mdev->flags);
  885. }
  886. if (os.conn == C_CONNECTED &&
  887. (ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T)) {
  888. mdev->ov_position = 0;
  889. mdev->rs_total =
  890. mdev->rs_mark_left = drbd_bm_bits(mdev);
  891. if (mdev->agreed_pro_version >= 90)
  892. set_ov_position(mdev, ns.conn);
  893. else
  894. mdev->ov_start_sector = 0;
  895. mdev->ov_left = mdev->rs_total
  896. - BM_SECT_TO_BIT(mdev->ov_position);
  897. mdev->rs_start =
  898. mdev->rs_mark_time = jiffies;
  899. mdev->ov_last_oos_size = 0;
  900. mdev->ov_last_oos_start = 0;
  901. if (ns.conn == C_VERIFY_S) {
  902. dev_info(DEV, "Starting Online Verify from sector %llu\n",
  903. (unsigned long long)mdev->ov_position);
  904. mod_timer(&mdev->resync_timer, jiffies);
  905. }
  906. }
  907. if (get_ldev(mdev)) {
  908. u32 mdf = mdev->ldev->md.flags & ~(MDF_CONSISTENT|MDF_PRIMARY_IND|
  909. MDF_CONNECTED_IND|MDF_WAS_UP_TO_DATE|
  910. MDF_PEER_OUT_DATED|MDF_CRASHED_PRIMARY);
  911. if (test_bit(CRASHED_PRIMARY, &mdev->flags))
  912. mdf |= MDF_CRASHED_PRIMARY;
  913. if (mdev->state.role == R_PRIMARY ||
  914. (mdev->state.pdsk < D_INCONSISTENT && mdev->state.peer == R_PRIMARY))
  915. mdf |= MDF_PRIMARY_IND;
  916. if (mdev->state.conn > C_WF_REPORT_PARAMS)
  917. mdf |= MDF_CONNECTED_IND;
  918. if (mdev->state.disk > D_INCONSISTENT)
  919. mdf |= MDF_CONSISTENT;
  920. if (mdev->state.disk > D_OUTDATED)
  921. mdf |= MDF_WAS_UP_TO_DATE;
  922. if (mdev->state.pdsk <= D_OUTDATED && mdev->state.pdsk >= D_INCONSISTENT)
  923. mdf |= MDF_PEER_OUT_DATED;
  924. if (mdf != mdev->ldev->md.flags) {
  925. mdev->ldev->md.flags = mdf;
  926. drbd_md_mark_dirty(mdev);
  927. }
  928. if (os.disk < D_CONSISTENT && ns.disk >= D_CONSISTENT)
  929. drbd_set_ed_uuid(mdev, mdev->ldev->md.uuid[UI_CURRENT]);
  930. put_ldev(mdev);
  931. }
  932. /* Peer was forced D_UP_TO_DATE & R_PRIMARY, consider to resync */
  933. if (os.disk == D_INCONSISTENT && os.pdsk == D_INCONSISTENT &&
  934. os.peer == R_SECONDARY && ns.peer == R_PRIMARY)
  935. set_bit(CONSIDER_RESYNC, &mdev->flags);
  936. /* Receiver should clean up itself */
  937. if (os.conn != C_DISCONNECTING && ns.conn == C_DISCONNECTING)
  938. drbd_thread_stop_nowait(&mdev->receiver);
  939. /* Now the receiver finished cleaning up itself, it should die */
  940. if (os.conn != C_STANDALONE && ns.conn == C_STANDALONE)
  941. drbd_thread_stop_nowait(&mdev->receiver);
  942. /* Upon network failure, we need to restart the receiver. */
  943. if (os.conn > C_TEAR_DOWN &&
  944. ns.conn <= C_TEAR_DOWN && ns.conn >= C_TIMEOUT)
  945. drbd_thread_restart_nowait(&mdev->receiver);
  946. ascw = kmalloc(sizeof(*ascw), GFP_ATOMIC);
  947. if (ascw) {
  948. ascw->os = os;
  949. ascw->ns = ns;
  950. ascw->flags = flags;
  951. ascw->w.cb = w_after_state_ch;
  952. ascw->done = done;
  953. drbd_queue_work(&mdev->data.work, &ascw->w);
  954. } else {
  955. dev_warn(DEV, "Could not kmalloc an ascw\n");
  956. }
  957. return rv;
  958. }
  959. static int w_after_state_ch(struct drbd_conf *mdev, struct drbd_work *w, int unused)
  960. {
  961. struct after_state_chg_work *ascw =
  962. container_of(w, struct after_state_chg_work, w);
  963. after_state_ch(mdev, ascw->os, ascw->ns, ascw->flags);
  964. if (ascw->flags & CS_WAIT_COMPLETE) {
  965. D_ASSERT(ascw->done != NULL);
  966. complete(ascw->done);
  967. }
  968. kfree(ascw);
  969. return 1;
  970. }
  971. static void abw_start_sync(struct drbd_conf *mdev, int rv)
  972. {
  973. if (rv) {
  974. dev_err(DEV, "Writing the bitmap failed not starting resync.\n");
  975. _drbd_request_state(mdev, NS(conn, C_CONNECTED), CS_VERBOSE);
  976. return;
  977. }
  978. switch (mdev->state.conn) {
  979. case C_STARTING_SYNC_T:
  980. _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE);
  981. break;
  982. case C_STARTING_SYNC_S:
  983. drbd_start_resync(mdev, C_SYNC_SOURCE);
  984. break;
  985. }
  986. }
  987. /**
  988. * after_state_ch() - Perform after state change actions that may sleep
  989. * @mdev: DRBD device.
  990. * @os: old state.
  991. * @ns: new state.
  992. * @flags: Flags
  993. */
  994. static void after_state_ch(struct drbd_conf *mdev, union drbd_state os,
  995. union drbd_state ns, enum chg_state_flags flags)
  996. {
  997. enum drbd_fencing_p fp;
  998. if (os.conn != C_CONNECTED && ns.conn == C_CONNECTED) {
  999. clear_bit(CRASHED_PRIMARY, &mdev->flags);
  1000. if (mdev->p_uuid)
  1001. mdev->p_uuid[UI_FLAGS] &= ~((u64)2);
  1002. }
  1003. fp = FP_DONT_CARE;
  1004. if (get_ldev(mdev)) {
  1005. fp = mdev->ldev->dc.fencing;
  1006. put_ldev(mdev);
  1007. }
  1008. /* Inform userspace about the change... */
  1009. drbd_bcast_state(mdev, ns);
  1010. if (!(os.role == R_PRIMARY && os.disk < D_UP_TO_DATE && os.pdsk < D_UP_TO_DATE) &&
  1011. (ns.role == R_PRIMARY && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE))
  1012. drbd_khelper(mdev, "pri-on-incon-degr");
  1013. /* Here we have the actions that are performed after a
  1014. state change. This function might sleep */
  1015. if (fp == FP_STONITH && ns.susp) {
  1016. /* case1: The outdate peer handler is successful:
  1017. * case2: The connection was established again: */
  1018. if ((os.pdsk > D_OUTDATED && ns.pdsk <= D_OUTDATED) ||
  1019. (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED)) {
  1020. tl_clear(mdev);
  1021. spin_lock_irq(&mdev->req_lock);
  1022. _drbd_set_state(_NS(mdev, susp, 0), CS_VERBOSE, NULL);
  1023. spin_unlock_irq(&mdev->req_lock);
  1024. }
  1025. }
  1026. /* Do not change the order of the if above and the two below... */
  1027. if (os.pdsk == D_DISKLESS && ns.pdsk > D_DISKLESS) { /* attach on the peer */
  1028. drbd_send_uuids(mdev);
  1029. drbd_send_state(mdev);
  1030. }
  1031. if (os.conn != C_WF_BITMAP_S && ns.conn == C_WF_BITMAP_S)
  1032. drbd_queue_bitmap_io(mdev, &drbd_send_bitmap, NULL, "send_bitmap (WFBitMapS)");
  1033. /* Lost contact to peer's copy of the data */
  1034. if ((os.pdsk >= D_INCONSISTENT &&
  1035. os.pdsk != D_UNKNOWN &&
  1036. os.pdsk != D_OUTDATED)
  1037. && (ns.pdsk < D_INCONSISTENT ||
  1038. ns.pdsk == D_UNKNOWN ||
  1039. ns.pdsk == D_OUTDATED)) {
  1040. if (get_ldev(mdev)) {
  1041. if ((ns.role == R_PRIMARY || ns.peer == R_PRIMARY) &&
  1042. mdev->ldev->md.uuid[UI_BITMAP] == 0 && ns.disk >= D_UP_TO_DATE) {
  1043. drbd_uuid_new_current(mdev);
  1044. drbd_send_uuids(mdev);
  1045. }
  1046. put_ldev(mdev);
  1047. }
  1048. }
  1049. if (ns.pdsk < D_INCONSISTENT && get_ldev(mdev)) {
  1050. if (ns.peer == R_PRIMARY && mdev->ldev->md.uuid[UI_BITMAP] == 0)
  1051. drbd_uuid_new_current(mdev);
  1052. /* D_DISKLESS Peer becomes secondary */
  1053. if (os.peer == R_PRIMARY && ns.peer == R_SECONDARY)
  1054. drbd_al_to_on_disk_bm(mdev);
  1055. put_ldev(mdev);
  1056. }
  1057. /* Last part of the attaching process ... */
  1058. if (ns.conn >= C_CONNECTED &&
  1059. os.disk == D_ATTACHING && ns.disk == D_NEGOTIATING) {
  1060. drbd_send_sizes(mdev, 0, 0); /* to start sync... */
  1061. drbd_send_uuids(mdev);
  1062. drbd_send_state(mdev);
  1063. }
  1064. /* We want to pause/continue resync, tell peer. */
  1065. if (ns.conn >= C_CONNECTED &&
  1066. ((os.aftr_isp != ns.aftr_isp) ||
  1067. (os.user_isp != ns.user_isp)))
  1068. drbd_send_state(mdev);
  1069. /* In case one of the isp bits got set, suspend other devices. */
  1070. if ((!os.aftr_isp && !os.peer_isp && !os.user_isp) &&
  1071. (ns.aftr_isp || ns.peer_isp || ns.user_isp))
  1072. suspend_other_sg(mdev);
  1073. /* Make sure the peer gets informed about eventual state
  1074. changes (ISP bits) while we were in WFReportParams. */
  1075. if (os.conn == C_WF_REPORT_PARAMS && ns.conn >= C_CONNECTED)
  1076. drbd_send_state(mdev);
  1077. /* We are in the progress to start a full sync... */
  1078. if ((os.conn != C_STARTING_SYNC_T && ns.conn == C_STARTING_SYNC_T) ||
  1079. (os.conn != C_STARTING_SYNC_S && ns.conn == C_STARTING_SYNC_S))
  1080. drbd_queue_bitmap_io(mdev, &drbd_bmio_set_n_write, &abw_start_sync, "set_n_write from StartingSync");
  1081. /* We are invalidating our self... */
  1082. if (os.conn < C_CONNECTED && ns.conn < C_CONNECTED &&
  1083. os.disk > D_INCONSISTENT && ns.disk == D_INCONSISTENT)
  1084. drbd_queue_bitmap_io(mdev, &drbd_bmio_set_n_write, NULL, "set_n_write from invalidate");
  1085. if (os.disk > D_FAILED && ns.disk == D_FAILED) {
  1086. enum drbd_io_error_p eh;
  1087. eh = EP_PASS_ON;
  1088. if (get_ldev_if_state(mdev, D_FAILED)) {
  1089. eh = mdev->ldev->dc.on_io_error;
  1090. put_ldev(mdev);
  1091. }
  1092. drbd_rs_cancel_all(mdev);
  1093. /* since get_ldev() only works as long as disk>=D_INCONSISTENT,
  1094. and it is D_DISKLESS here, local_cnt can only go down, it can
  1095. not increase... It will reach zero */
  1096. wait_event(mdev->misc_wait, !atomic_read(&mdev->local_cnt));
  1097. mdev->rs_total = 0;
  1098. mdev->rs_failed = 0;
  1099. atomic_set(&mdev->rs_pending_cnt, 0);
  1100. spin_lock_irq(&mdev->req_lock);
  1101. _drbd_set_state(_NS(mdev, disk, D_DISKLESS), CS_HARD, NULL);
  1102. spin_unlock_irq(&mdev->req_lock);
  1103. if (eh == EP_CALL_HELPER)
  1104. drbd_khelper(mdev, "local-io-error");
  1105. }
  1106. if (os.disk > D_DISKLESS && ns.disk == D_DISKLESS) {
  1107. if (os.disk == D_FAILED) /* && ns.disk == D_DISKLESS*/ {
  1108. if (drbd_send_state(mdev))
  1109. dev_warn(DEV, "Notified peer that my disk is broken.\n");
  1110. else
  1111. dev_err(DEV, "Sending state in drbd_io_error() failed\n");
  1112. }
  1113. wait_event(mdev->misc_wait, !atomic_read(&mdev->local_cnt));
  1114. lc_destroy(mdev->resync);
  1115. mdev->resync = NULL;
  1116. lc_destroy(mdev->act_log);
  1117. mdev->act_log = NULL;
  1118. __no_warn(local,
  1119. drbd_free_bc(mdev->ldev);
  1120. mdev->ldev = NULL;);
  1121. if (mdev->md_io_tmpp)
  1122. __free_page(mdev->md_io_tmpp);
  1123. }
  1124. /* Disks got bigger while they were detached */
  1125. if (ns.disk > D_NEGOTIATING && ns.pdsk > D_NEGOTIATING &&
  1126. test_and_clear_bit(RESYNC_AFTER_NEG, &mdev->flags)) {
  1127. if (ns.conn == C_CONNECTED)
  1128. resync_after_online_grow(mdev);
  1129. }
  1130. /* A resync finished or aborted, wake paused devices... */
  1131. if ((os.conn > C_CONNECTED && ns.conn <= C_CONNECTED) ||
  1132. (os.peer_isp && !ns.peer_isp) ||
  1133. (os.user_isp && !ns.user_isp))
  1134. resume_next_sg(mdev);
  1135. /* Upon network connection, we need to start the receiver */
  1136. if (os.conn == C_STANDALONE && ns.conn == C_UNCONNECTED)
  1137. drbd_thread_start(&mdev->receiver);
  1138. /* Terminate worker thread if we are unconfigured - it will be
  1139. restarted as needed... */
  1140. if (ns.disk == D_DISKLESS &&
  1141. ns.conn == C_STANDALONE &&
  1142. ns.role == R_SECONDARY) {
  1143. if (os.aftr_isp != ns.aftr_isp)
  1144. resume_next_sg(mdev);
  1145. /* set in __drbd_set_state, unless CONFIG_PENDING was set */
  1146. if (test_bit(DEVICE_DYING, &mdev->flags))
  1147. drbd_thread_stop_nowait(&mdev->worker);
  1148. }
  1149. drbd_md_sync(mdev);
  1150. }
  1151. static int drbd_thread_setup(void *arg)
  1152. {
  1153. struct drbd_thread *thi = (struct drbd_thread *) arg;
  1154. struct drbd_conf *mdev = thi->mdev;
  1155. unsigned long flags;
  1156. int retval;
  1157. restart:
  1158. retval = thi->function(thi);
  1159. spin_lock_irqsave(&thi->t_lock, flags);
  1160. /* if the receiver has been "Exiting", the last thing it did
  1161. * was set the conn state to "StandAlone",
  1162. * if now a re-connect request comes in, conn state goes C_UNCONNECTED,
  1163. * and receiver thread will be "started".
  1164. * drbd_thread_start needs to set "Restarting" in that case.
  1165. * t_state check and assignment needs to be within the same spinlock,
  1166. * so either thread_start sees Exiting, and can remap to Restarting,
  1167. * or thread_start see None, and can proceed as normal.
  1168. */
  1169. if (thi->t_state == Restarting) {
  1170. dev_info(DEV, "Restarting %s\n", current->comm);
  1171. thi->t_state = Running;
  1172. spin_unlock_irqrestore(&thi->t_lock, flags);
  1173. goto restart;
  1174. }
  1175. thi->task = NULL;
  1176. thi->t_state = None;
  1177. smp_mb();
  1178. complete(&thi->stop);
  1179. spin_unlock_irqrestore(&thi->t_lock, flags);
  1180. dev_info(DEV, "Terminating %s\n", current->comm);
  1181. /* Release mod reference taken when thread was started */
  1182. module_put(THIS_MODULE);
  1183. return retval;
  1184. }
  1185. static void drbd_thread_init(struct drbd_conf *mdev, struct drbd_thread *thi,
  1186. int (*func) (struct drbd_thread *))
  1187. {
  1188. spin_lock_init(&thi->t_lock);
  1189. thi->task = NULL;
  1190. thi->t_state = None;
  1191. thi->function = func;
  1192. thi->mdev = mdev;
  1193. }
  1194. int drbd_thread_start(struct drbd_thread *thi)
  1195. {
  1196. struct drbd_conf *mdev = thi->mdev;
  1197. struct task_struct *nt;
  1198. unsigned long flags;
  1199. const char *me =
  1200. thi == &mdev->receiver ? "receiver" :
  1201. thi == &mdev->asender ? "asender" :
  1202. thi == &mdev->worker ? "worker" : "NONSENSE";
  1203. /* is used from state engine doing drbd_thread_stop_nowait,
  1204. * while holding the req lock irqsave */
  1205. spin_lock_irqsave(&thi->t_lock, flags);
  1206. switch (thi->t_state) {
  1207. case None:
  1208. dev_info(DEV, "Starting %s thread (from %s [%d])\n",
  1209. me, current->comm, current->pid);
  1210. /* Get ref on module for thread - this is released when thread exits */
  1211. if (!try_module_get(THIS_MODULE)) {
  1212. dev_err(DEV, "Failed to get module reference in drbd_thread_start\n");
  1213. spin_unlock_irqrestore(&thi->t_lock, flags);
  1214. return FALSE;
  1215. }
  1216. init_completion(&thi->stop);
  1217. D_ASSERT(thi->task == NULL);
  1218. thi->reset_cpu_mask = 1;
  1219. thi->t_state = Running;
  1220. spin_unlock_irqrestore(&thi->t_lock, flags);
  1221. flush_signals(current); /* otherw. may get -ERESTARTNOINTR */
  1222. nt = kthread_create(drbd_thread_setup, (void *) thi,
  1223. "drbd%d_%s", mdev_to_minor(mdev), me);
  1224. if (IS_ERR(nt)) {
  1225. dev_err(DEV, "Couldn't start thread\n");
  1226. module_put(THIS_MODULE);
  1227. return FALSE;
  1228. }
  1229. spin_lock_irqsave(&thi->t_lock, flags);
  1230. thi->task = nt;
  1231. thi->t_state = Running;
  1232. spin_unlock_irqrestore(&thi->t_lock, flags);
  1233. wake_up_process(nt);
  1234. break;
  1235. case Exiting:
  1236. thi->t_state = Restarting;
  1237. dev_info(DEV, "Restarting %s thread (from %s [%d])\n",
  1238. me, current->comm, current->pid);
  1239. /* fall through */
  1240. case Running:
  1241. case Restarting:
  1242. default:
  1243. spin_unlock_irqrestore(&thi->t_lock, flags);
  1244. break;
  1245. }
  1246. return TRUE;
  1247. }
  1248. void _drbd_thread_stop(struct drbd_thread *thi, int restart, int wait)
  1249. {
  1250. unsigned long flags;
  1251. enum drbd_thread_state ns = restart ? Restarting : Exiting;
  1252. /* may be called from state engine, holding the req lock irqsave */
  1253. spin_lock_irqsave(&thi->t_lock, flags);
  1254. if (thi->t_state == None) {
  1255. spin_unlock_irqrestore(&thi->t_lock, flags);
  1256. if (restart)
  1257. drbd_thread_start(thi);
  1258. return;
  1259. }
  1260. if (thi->t_state != ns) {
  1261. if (thi->task == NULL) {
  1262. spin_unlock_irqrestore(&thi->t_lock, flags);
  1263. return;
  1264. }
  1265. thi->t_state = ns;
  1266. smp_mb();
  1267. init_completion(&thi->stop);
  1268. if (thi->task != current)
  1269. force_sig(DRBD_SIGKILL, thi->task);
  1270. }
  1271. spin_unlock_irqrestore(&thi->t_lock, flags);
  1272. if (wait)
  1273. wait_for_completion(&thi->stop);
  1274. }
  1275. #ifdef CONFIG_SMP
  1276. /**
  1277. * drbd_calc_cpu_mask() - Generate CPU masks, spread over all CPUs
  1278. * @mdev: DRBD device.
  1279. *
  1280. * Forces all threads of a device onto the same CPU. This is beneficial for
  1281. * DRBD's performance. May be overwritten by user's configuration.
  1282. */
  1283. void drbd_calc_cpu_mask(struct drbd_conf *mdev)
  1284. {
  1285. int ord, cpu;
  1286. /* user override. */
  1287. if (cpumask_weight(mdev->cpu_mask))
  1288. return;
  1289. ord = mdev_to_minor(mdev) % cpumask_weight(cpu_online_mask);
  1290. for_each_online_cpu(cpu) {
  1291. if (ord-- == 0) {
  1292. cpumask_set_cpu(cpu, mdev->cpu_mask);
  1293. return;
  1294. }
  1295. }
  1296. /* should not be reached */
  1297. cpumask_setall(mdev->cpu_mask);
  1298. }
  1299. /**
  1300. * drbd_thread_current_set_cpu() - modifies the cpu mask of the _current_ thread
  1301. * @mdev: DRBD device.
  1302. *
  1303. * call in the "main loop" of _all_ threads, no need for any mutex, current won't die
  1304. * prematurely.
  1305. */
  1306. void drbd_thread_current_set_cpu(struct drbd_conf *mdev)
  1307. {
  1308. struct task_struct *p = current;
  1309. struct drbd_thread *thi =
  1310. p == mdev->asender.task ? &mdev->asender :
  1311. p == mdev->receiver.task ? &mdev->receiver :
  1312. p == mdev->worker.task ? &mdev->worker :
  1313. NULL;
  1314. ERR_IF(thi == NULL)
  1315. return;
  1316. if (!thi->reset_cpu_mask)
  1317. return;
  1318. thi->reset_cpu_mask = 0;
  1319. set_cpus_allowed_ptr(p, mdev->cpu_mask);
  1320. }
  1321. #endif
  1322. /* the appropriate socket mutex must be held already */
  1323. int _drbd_send_cmd(struct drbd_conf *mdev, struct socket *sock,
  1324. enum drbd_packets cmd, struct p_header *h,
  1325. size_t size, unsigned msg_flags)
  1326. {
  1327. int sent, ok;
  1328. ERR_IF(!h) return FALSE;
  1329. ERR_IF(!size) return FALSE;
  1330. h->magic = BE_DRBD_MAGIC;
  1331. h->command = cpu_to_be16(cmd);
  1332. h->length = cpu_to_be16(size-sizeof(struct p_header));
  1333. sent = drbd_send(mdev, sock, h, size, msg_flags);
  1334. ok = (sent == size);
  1335. if (!ok)
  1336. dev_err(DEV, "short sent %s size=%d sent=%d\n",
  1337. cmdname(cmd), (int)size, sent);
  1338. return ok;
  1339. }
  1340. /* don't pass the socket. we may only look at it
  1341. * when we hold the appropriate socket mutex.
  1342. */
  1343. int drbd_send_cmd(struct drbd_conf *mdev, int use_data_socket,
  1344. enum drbd_packets cmd, struct p_header *h, size_t size)
  1345. {
  1346. int ok = 0;
  1347. struct socket *sock;
  1348. if (use_data_socket) {
  1349. mutex_lock(&mdev->data.mutex);
  1350. sock = mdev->data.socket;
  1351. } else {
  1352. mutex_lock(&mdev->meta.mutex);
  1353. sock = mdev->meta.socket;
  1354. }
  1355. /* drbd_disconnect() could have called drbd_free_sock()
  1356. * while we were waiting in down()... */
  1357. if (likely(sock != NULL))
  1358. ok = _drbd_send_cmd(mdev, sock, cmd, h, size, 0);
  1359. if (use_data_socket)
  1360. mutex_unlock(&mdev->data.mutex);
  1361. else
  1362. mutex_unlock(&mdev->meta.mutex);
  1363. return ok;
  1364. }
  1365. int drbd_send_cmd2(struct drbd_conf *mdev, enum drbd_packets cmd, char *data,
  1366. size_t size)
  1367. {
  1368. struct p_header h;
  1369. int ok;
  1370. h.magic = BE_DRBD_MAGIC;
  1371. h.command = cpu_to_be16(cmd);
  1372. h.length = cpu_to_be16(size);
  1373. if (!drbd_get_data_sock(mdev))
  1374. return 0;
  1375. ok = (sizeof(h) ==
  1376. drbd_send(mdev, mdev->data.socket, &h, sizeof(h), 0));
  1377. ok = ok && (size ==
  1378. drbd_send(mdev, mdev->data.socket, data, size, 0));
  1379. drbd_put_data_sock(mdev);
  1380. return ok;
  1381. }
  1382. int drbd_send_sync_param(struct drbd_conf *mdev, struct syncer_conf *sc)
  1383. {
  1384. struct p_rs_param_89 *p;
  1385. struct socket *sock;
  1386. int size, rv;
  1387. const int apv = mdev->agreed_pro_version;
  1388. size = apv <= 87 ? sizeof(struct p_rs_param)
  1389. : apv == 88 ? sizeof(struct p_rs_param)
  1390. + strlen(mdev->sync_conf.verify_alg) + 1
  1391. : /* 89 */ sizeof(struct p_rs_param_89);
  1392. /* used from admin command context and receiver/worker context.
  1393. * to avoid kmalloc, grab the socket right here,
  1394. * then use the pre-allocated sbuf there */
  1395. mutex_lock(&mdev->data.mutex);
  1396. sock = mdev->data.socket;
  1397. if (likely(sock != NULL)) {
  1398. enum drbd_packets cmd = apv >= 89 ? P_SYNC_PARAM89 : P_SYNC_PARAM;
  1399. p = &mdev->data.sbuf.rs_param_89;
  1400. /* initialize verify_alg and csums_alg */
  1401. memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX);
  1402. p->rate = cpu_to_be32(sc->rate);
  1403. if (apv >= 88)
  1404. strcpy(p->verify_alg, mdev->sync_conf.verify_alg);
  1405. if (apv >= 89)
  1406. strcpy(p->csums_alg, mdev->sync_conf.csums_alg);
  1407. rv = _drbd_send_cmd(mdev, sock, cmd, &p->head, size, 0);
  1408. } else
  1409. rv = 0; /* not ok */
  1410. mutex_unlock(&mdev->data.mutex);
  1411. return rv;
  1412. }
  1413. int drbd_send_protocol(struct drbd_conf *mdev)
  1414. {
  1415. struct p_protocol *p;
  1416. int size, cf, rv;
  1417. size = sizeof(struct p_protocol);
  1418. if (mdev->agreed_pro_version >= 87)
  1419. size += strlen(mdev->net_conf->integrity_alg) + 1;
  1420. /* we must not recurse into our own queue,
  1421. * as that is blocked during handshake */
  1422. p = kmalloc(size, GFP_NOIO);
  1423. if (p == NULL)
  1424. return 0;
  1425. p->protocol = cpu_to_be32(mdev->net_conf->wire_protocol);
  1426. p->after_sb_0p = cpu_to_be32(mdev->net_conf->after_sb_0p);
  1427. p->after_sb_1p = cpu_to_be32(mdev->net_conf->after_sb_1p);
  1428. p->after_sb_2p = cpu_to_be32(mdev->net_conf->after_sb_2p);
  1429. p->two_primaries = cpu_to_be32(mdev->net_conf->two_primaries);
  1430. cf = 0;
  1431. if (mdev->net_conf->want_lose)
  1432. cf |= CF_WANT_LOSE;
  1433. if (mdev->net_conf->dry_run) {
  1434. if (mdev->agreed_pro_version >= 92)
  1435. cf |= CF_DRY_RUN;
  1436. else {
  1437. dev_err(DEV, "--dry-run is not supported by peer");
  1438. kfree(p);
  1439. return 0;
  1440. }
  1441. }
  1442. p->conn_flags = cpu_to_be32(cf);
  1443. if (mdev->agreed_pro_version >= 87)
  1444. strcpy(p->integrity_alg, mdev->net_conf->integrity_alg);
  1445. rv = drbd_send_cmd(mdev, USE_DATA_SOCKET, P_PROTOCOL,
  1446. (struct p_header *)p, size);
  1447. kfree(p);
  1448. return rv;
  1449. }
  1450. int _drbd_send_uuids(struct drbd_conf *mdev, u64 uuid_flags)
  1451. {
  1452. struct p_uuids p;
  1453. int i;
  1454. if (!get_ldev_if_state(mdev, D_NEGOTIATING))
  1455. return 1;
  1456. for (i = UI_CURRENT; i < UI_SIZE; i++)
  1457. p.uuid[i] = mdev->ldev ? cpu_to_be64(mdev->ldev->md.uuid[i]) : 0;
  1458. mdev->comm_bm_set = drbd_bm_total_weight(mdev);
  1459. p.uuid[UI_SIZE] = cpu_to_be64(mdev->comm_bm_set);
  1460. uuid_flags |= mdev->net_conf->want_lose ? 1 : 0;
  1461. uuid_flags |= test_bit(CRASHED_PRIMARY, &mdev->flags) ? 2 : 0;
  1462. uuid_flags |= mdev->new_state_tmp.disk == D_INCONSISTENT ? 4 : 0;
  1463. p.uuid[UI_FLAGS] = cpu_to_be64(uuid_flags);
  1464. put_ldev(mdev);
  1465. return drbd_send_cmd(mdev, USE_DATA_SOCKET, P_UUIDS,
  1466. (struct p_header *)&p, sizeof(p));
  1467. }
  1468. int drbd_send_uuids(struct drbd_conf *mdev)
  1469. {
  1470. return _drbd_send_uuids(mdev, 0);
  1471. }
  1472. int drbd_send_uuids_skip_initial_sync(struct drbd_conf *mdev)
  1473. {
  1474. return _drbd_send_uuids(mdev, 8);
  1475. }

Large files files are truncated, but you can click here to view the full file