PageRenderTime 86ms CodeModel.GetById 25ms 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
  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. }
  1476. int drbd_send_sync_uuid(struct drbd_conf *mdev, u64 val)
  1477. {
  1478. struct p_rs_uuid p;
  1479. p.uuid = cpu_to_be64(val);
  1480. return drbd_send_cmd(mdev, USE_DATA_SOCKET, P_SYNC_UUID,
  1481. (struct p_header *)&p, sizeof(p));
  1482. }
  1483. int drbd_send_sizes(struct drbd_conf *mdev, int trigger_reply, enum dds_flags flags)
  1484. {
  1485. struct p_sizes p;
  1486. sector_t d_size, u_size;
  1487. int q_order_type;
  1488. int ok;
  1489. if (get_ldev_if_state(mdev, D_NEGOTIATING)) {
  1490. D_ASSERT(mdev->ldev->backing_bdev);
  1491. d_size = drbd_get_max_capacity(mdev->ldev);
  1492. u_size = mdev->ldev->dc.disk_size;
  1493. q_order_type = drbd_queue_order_type(mdev);
  1494. put_ldev(mdev);
  1495. } else {
  1496. d_size = 0;
  1497. u_size = 0;
  1498. q_order_type = QUEUE_ORDERED_NONE;
  1499. }
  1500. p.d_size = cpu_to_be64(d_size);
  1501. p.u_size = cpu_to_be64(u_size);
  1502. p.c_size = cpu_to_be64(trigger_reply ? 0 : drbd_get_capacity(mdev->this_bdev));
  1503. p.max_segment_size = cpu_to_be32(queue_max_segment_size(mdev->rq_queue));
  1504. p.queue_order_type = cpu_to_be16(q_order_type);
  1505. p.dds_flags = cpu_to_be16(flags);
  1506. ok = drbd_send_cmd(mdev, USE_DATA_SOCKET, P_SIZES,
  1507. (struct p_header *)&p, sizeof(p));
  1508. return ok;
  1509. }
  1510. /**
  1511. * drbd_send_state() - Sends the drbd state to the peer
  1512. * @mdev: DRBD device.
  1513. */
  1514. int drbd_send_state(struct drbd_conf *mdev)
  1515. {
  1516. struct socket *sock;
  1517. struct p_state p;
  1518. int ok = 0;
  1519. /* Grab state lock so we wont send state if we're in the middle
  1520. * of a cluster wide state change on another thread */
  1521. drbd_state_lock(mdev);
  1522. mutex_lock(&mdev->data.mutex);
  1523. p.state = cpu_to_be32(mdev->state.i); /* Within the send mutex */
  1524. sock = mdev->data.socket;
  1525. if (likely(sock != NULL)) {
  1526. ok = _drbd_send_cmd(mdev, sock, P_STATE,
  1527. (struct p_header *)&p, sizeof(p), 0);
  1528. }
  1529. mutex_unlock(&mdev->data.mutex);
  1530. drbd_state_unlock(mdev);
  1531. return ok;
  1532. }
  1533. int drbd_send_state_req(struct drbd_conf *mdev,
  1534. union drbd_state mask, union drbd_state val)
  1535. {
  1536. struct p_req_state p;
  1537. p.mask = cpu_to_be32(mask.i);
  1538. p.val = cpu_to_be32(val.i);
  1539. return drbd_send_cmd(mdev, USE_DATA_SOCKET, P_STATE_CHG_REQ,
  1540. (struct p_header *)&p, sizeof(p));
  1541. }
  1542. int drbd_send_sr_reply(struct drbd_conf *mdev, int retcode)
  1543. {
  1544. struct p_req_state_reply p;
  1545. p.retcode = cpu_to_be32(retcode);
  1546. return drbd_send_cmd(mdev, USE_META_SOCKET, P_STATE_CHG_REPLY,
  1547. (struct p_header *)&p, sizeof(p));
  1548. }
  1549. int fill_bitmap_rle_bits(struct drbd_conf *mdev,
  1550. struct p_compressed_bm *p,
  1551. struct bm_xfer_ctx *c)
  1552. {
  1553. struct bitstream bs;
  1554. unsigned long plain_bits;
  1555. unsigned long tmp;
  1556. unsigned long rl;
  1557. unsigned len;
  1558. unsigned toggle;
  1559. int bits;
  1560. /* may we use this feature? */
  1561. if ((mdev->sync_conf.use_rle == 0) ||
  1562. (mdev->agreed_pro_version < 90))
  1563. return 0;
  1564. if (c->bit_offset >= c->bm_bits)
  1565. return 0; /* nothing to do. */
  1566. /* use at most thus many bytes */
  1567. bitstream_init(&bs, p->code, BM_PACKET_VLI_BYTES_MAX, 0);
  1568. memset(p->code, 0, BM_PACKET_VLI_BYTES_MAX);
  1569. /* plain bits covered in this code string */
  1570. plain_bits = 0;
  1571. /* p->encoding & 0x80 stores whether the first run length is set.
  1572. * bit offset is implicit.
  1573. * start with toggle == 2 to be able to tell the first iteration */
  1574. toggle = 2;
  1575. /* see how much plain bits we can stuff into one packet
  1576. * using RLE and VLI. */
  1577. do {
  1578. tmp = (toggle == 0) ? _drbd_bm_find_next_zero(mdev, c->bit_offset)
  1579. : _drbd_bm_find_next(mdev, c->bit_offset);
  1580. if (tmp == -1UL)
  1581. tmp = c->bm_bits;
  1582. rl = tmp - c->bit_offset;
  1583. if (toggle == 2) { /* first iteration */
  1584. if (rl == 0) {
  1585. /* the first checked bit was set,
  1586. * store start value, */
  1587. DCBP_set_start(p, 1);
  1588. /* but skip encoding of zero run length */
  1589. toggle = !toggle;
  1590. continue;
  1591. }
  1592. DCBP_set_start(p, 0);
  1593. }
  1594. /* paranoia: catch zero runlength.
  1595. * can only happen if bitmap is modified while we scan it. */
  1596. if (rl == 0) {
  1597. dev_err(DEV, "unexpected zero runlength while encoding bitmap "
  1598. "t:%u bo:%lu\n", toggle, c->bit_offset);
  1599. return -1;
  1600. }
  1601. bits = vli_encode_bits(&bs, rl);
  1602. if (bits == -ENOBUFS) /* buffer full */
  1603. break;
  1604. if (bits <= 0) {
  1605. dev_err(DEV, "error while encoding bitmap: %d\n", bits);
  1606. return 0;
  1607. }
  1608. toggle = !toggle;
  1609. plain_bits += rl;
  1610. c->bit_offset = tmp;
  1611. } while (c->bit_offset < c->bm_bits);
  1612. len = bs.cur.b - p->code + !!bs.cur.bit;
  1613. if (plain_bits < (len << 3)) {
  1614. /* incompressible with this method.
  1615. * we need to rewind both word and bit position. */
  1616. c->bit_offset -= plain_bits;
  1617. bm_xfer_ctx_bit_to_word_offset(c);
  1618. c->bit_offset = c->word_offset * BITS_PER_LONG;
  1619. return 0;
  1620. }
  1621. /* RLE + VLI was able to compress it just fine.
  1622. * update c->word_offset. */
  1623. bm_xfer_ctx_bit_to_word_offset(c);
  1624. /* store pad_bits */
  1625. DCBP_set_pad_bits(p, (8 - bs.cur.bit) & 0x7);
  1626. return len;
  1627. }
  1628. enum { OK, FAILED, DONE }
  1629. send_bitmap_rle_or_plain(struct drbd_conf *mdev,
  1630. struct p_header *h, struct bm_xfer_ctx *c)
  1631. {
  1632. struct p_compressed_bm *p = (void*)h;
  1633. unsigned long num_words;
  1634. int len;
  1635. int ok;
  1636. len = fill_bitmap_rle_bits(mdev, p, c);
  1637. if (len < 0)
  1638. return FAILED;
  1639. if (len) {
  1640. DCBP_set_code(p, RLE_VLI_Bits);
  1641. ok = _drbd_send_cmd(mdev, mdev->data.socket, P_COMPRESSED_BITMAP, h,
  1642. sizeof(*p) + len, 0);
  1643. c->packets[0]++;
  1644. c->bytes[0] += sizeof(*p) + len;
  1645. if (c->bit_offset >= c->bm_bits)
  1646. len = 0; /* DONE */
  1647. } else {
  1648. /* was not compressible.
  1649. * send a buffer full of plain text bits instead. */
  1650. num_words = min_t(size_t, BM_PACKET_WORDS, c->bm_words - c->word_offset);
  1651. len = num_words * sizeof(long);
  1652. if (len)
  1653. drbd_bm_get_lel(mdev, c->word_offset, num_words, (unsigned long*)h->payload);
  1654. ok = _drbd_send_cmd(mdev, mdev->data.socket, P_BITMAP,
  1655. h, sizeof(struct p_header) + len, 0);
  1656. c->word_offset += num_words;
  1657. c->bit_offset = c->word_offset * BITS_PER_LONG;
  1658. c->packets[1]++;
  1659. c->bytes[1] += sizeof(struct p_header) + len;
  1660. if (c->bit_offset > c->bm_bits)
  1661. c->bit_offset = c->bm_bits;
  1662. }
  1663. ok = ok ? ((len == 0) ? DONE : OK) : FAILED;
  1664. if (ok == DONE)
  1665. INFO_bm_xfer_stats(mdev, "send", c);
  1666. return ok;
  1667. }
  1668. /* See the comment at receive_bitmap() */
  1669. int _drbd_send_bitmap(struct drbd_conf *mdev)
  1670. {
  1671. struct bm_xfer_ctx c;
  1672. struct p_header *p;
  1673. int ret;
  1674. ERR_IF(!mdev->bitmap) return FALSE;
  1675. /* maybe we should use some per thread scratch page,
  1676. * and allocate that during initial device creation? */
  1677. p = (struct p_header *) __get_free_page(GFP_NOIO);
  1678. if (!p) {
  1679. dev_err(DEV, "failed to allocate one page buffer in %s\n", __func__);
  1680. return FALSE;
  1681. }
  1682. if (get_ldev(mdev)) {
  1683. if (drbd_md_test_flag(mdev->ldev, MDF_FULL_SYNC)) {
  1684. dev_info(DEV, "Writing the whole bitmap, MDF_FullSync was set.\n");
  1685. drbd_bm_set_all(mdev);
  1686. if (drbd_bm_write(mdev)) {
  1687. /* write_bm did fail! Leave full sync flag set in Meta P_DATA
  1688. * but otherwise process as per normal - need to tell other
  1689. * side that a full resync is required! */
  1690. dev_err(DEV, "Failed to write bitmap to disk!\n");
  1691. } else {
  1692. drbd_md_clear_flag(mdev, MDF_FULL_SYNC);
  1693. drbd_md_sync(mdev);
  1694. }
  1695. }
  1696. put_ldev(mdev);
  1697. }
  1698. c = (struct bm_xfer_ctx) {
  1699. .bm_bits = drbd_bm_bits(mdev),
  1700. .bm_words = drbd_bm_words(mdev),
  1701. };
  1702. do {
  1703. ret = send_bitmap_rle_or_plain(mdev, p, &c);
  1704. } while (ret == OK);
  1705. free_page((unsigned long) p);
  1706. return (ret == DONE);
  1707. }
  1708. int drbd_send_bitmap(struct drbd_conf *mdev)
  1709. {
  1710. int err;
  1711. if (!drbd_get_data_sock(mdev))
  1712. return -1;
  1713. err = !_drbd_send_bitmap(mdev);
  1714. drbd_put_data_sock(mdev);
  1715. return err;
  1716. }
  1717. int drbd_send_b_ack(struct drbd_conf *mdev, u32 barrier_nr, u32 set_size)
  1718. {
  1719. int ok;
  1720. struct p_barrier_ack p;
  1721. p.barrier = barrier_nr;
  1722. p.set_size = cpu_to_be32(set_size);
  1723. if (mdev->state.conn < C_CONNECTED)
  1724. return FALSE;
  1725. ok = drbd_send_cmd(mdev, USE_META_SOCKET, P_BARRIER_ACK,
  1726. (struct p_header *)&p, sizeof(p));
  1727. return ok;
  1728. }
  1729. /**
  1730. * _drbd_send_ack() - Sends an ack packet
  1731. * @mdev: DRBD device.
  1732. * @cmd: Packet command code.
  1733. * @sector: sector, needs to be in big endian byte order
  1734. * @blksize: size in byte, needs to be in big endian byte order
  1735. * @block_id: Id, big endian byte order
  1736. */
  1737. static int _drbd_send_ack(struct drbd_conf *mdev, enum drbd_packets cmd,
  1738. u64 sector,
  1739. u32 blksize,
  1740. u64 block_id)
  1741. {
  1742. int ok;
  1743. struct p_block_ack p;
  1744. p.sector = sector;
  1745. p.block_id = block_id;
  1746. p.blksize = blksize;
  1747. p.seq_num = cpu_to_be32(atomic_add_return(1, &mdev->packet_seq));
  1748. if (!mdev->meta.socket || mdev->state.conn < C_CONNECTED)
  1749. return FALSE;
  1750. ok = drbd_send_cmd(mdev, USE_META_SOCKET, cmd,
  1751. (struct p_header *)&p, sizeof(p));
  1752. return ok;
  1753. }
  1754. int drbd_send_ack_dp(struct drbd_conf *mdev, enum drbd_packets cmd,
  1755. struct p_data *dp)
  1756. {
  1757. const int header_size = sizeof(struct p_data)
  1758. - sizeof(struct p_header);
  1759. int data_size = ((struct p_header *)dp)->length - header_size;
  1760. return _drbd_send_ack(mdev, cmd, dp->sector, cpu_to_be32(data_size),
  1761. dp->block_id);
  1762. }
  1763. int drbd_send_ack_rp(struct drbd_conf *mdev, enum drbd_packets cmd,
  1764. struct p_block_req *rp)
  1765. {
  1766. return _drbd_send_ack(mdev, cmd, rp->sector, rp->blksize, rp->block_id);
  1767. }
  1768. /**
  1769. * drbd_send_ack() - Sends an ack packet
  1770. * @mdev: DRBD device.
  1771. * @cmd: Packet command code.
  1772. * @e: Epoch entry.
  1773. */
  1774. int drbd_send_ack(struct drbd_conf *mdev,
  1775. enum drbd_packets cmd, struct drbd_epoch_entry *e)
  1776. {
  1777. return _drbd_send_ack(mdev, cmd,
  1778. cpu_to_be64(e->sector),
  1779. cpu_to_be32(e->size),
  1780. e->block_id);
  1781. }
  1782. /* This function misuses the block_id field to signal if the blocks
  1783. * are is sync or not. */
  1784. int drbd_send_ack_ex(struct drbd_conf *mdev, enum drbd_packets cmd,
  1785. sector_t sector, int blksize, u64 block_id)
  1786. {
  1787. return _drbd_send_ack(mdev, cmd,
  1788. cpu_to_be64(sector),
  1789. cpu_to_be32(blksize),
  1790. cpu_to_be64(block_id));
  1791. }
  1792. int drbd_send_drequest(struct drbd_conf *mdev, int cmd,
  1793. sector_t sector, int size, u64 block_id)
  1794. {
  1795. int ok;
  1796. struct p_block_req p;
  1797. p.sector = cpu_to_be64(sector);
  1798. p.block_id = block_id;
  1799. p.blksize = cpu_to_be32(size);
  1800. ok = drbd_send_cmd(mdev, USE_DATA_SOCKET, cmd,
  1801. (struct p_header *)&p, sizeof(p));
  1802. return ok;
  1803. }
  1804. int drbd_send_drequest_csum(struct drbd_conf *mdev,
  1805. sector_t sector, int size,
  1806. void *digest, int digest_size,
  1807. enum drbd_packets cmd)
  1808. {
  1809. int ok;
  1810. struct p_block_req p;
  1811. p.sector = cpu_to_be64(sector);
  1812. p.block_id = BE_DRBD_MAGIC + 0xbeef;
  1813. p.blksize = cpu_to_be32(size);
  1814. p.head.magic = BE_DRBD_MAGIC;
  1815. p.head.command = cpu_to_be16(cmd);
  1816. p.head.length = cpu_to_be16(sizeof(p) - sizeof(struct p_header) + digest_size);
  1817. mutex_lock(&mdev->data.mutex);
  1818. ok = (sizeof(p) == drbd_send(mdev, mdev->data.socket, &p, sizeof(p), 0));
  1819. ok = ok && (digest_size == drbd_send(mdev, mdev->data.socket, digest, digest_size, 0));
  1820. mutex_unlock(&mdev->data.mutex);
  1821. return ok;
  1822. }
  1823. int drbd_send_ov_request(struct drbd_conf *mdev, sector_t sector, int size)
  1824. {
  1825. int ok;
  1826. struct p_block_req p;
  1827. p.sector = cpu_to_be64(sector);
  1828. p.block_id = BE_DRBD_MAGIC + 0xbabe;
  1829. p.blksize = cpu_to_be32(size);
  1830. ok = drbd_send_cmd(mdev, USE_DATA_SOCKET, P_OV_REQUEST,
  1831. (struct p_header *)&p, sizeof(p));
  1832. return ok;
  1833. }
  1834. static int drbd_send_delay_probe(struct drbd_conf *mdev, struct drbd_socket *ds)
  1835. {
  1836. struct p_delay_probe dp;
  1837. int offset, ok = 0;
  1838. struct timeval now;
  1839. mutex_lock(&ds->mutex);
  1840. if (likely(ds->socket)) {
  1841. do_gettimeofday(&now);
  1842. offset = now.tv_usec - mdev->dps_time.tv_usec +
  1843. (now.tv_sec - mdev->dps_time.tv_sec) * 1000000;
  1844. dp.seq_num = cpu_to_be32(mdev->delay_seq);
  1845. dp.offset = cpu_to_be32(offset);
  1846. ok = _drbd_send_cmd(mdev, ds->socket, P_DELAY_PROBE,
  1847. (struct p_header *)&dp, sizeof(dp), 0);
  1848. }
  1849. mutex_unlock(&ds->mutex);
  1850. return ok;
  1851. }
  1852. static int drbd_send_delay_probes(struct drbd_conf *mdev)
  1853. {
  1854. int ok;
  1855. mdev->delay_seq++;
  1856. do_gettimeofday(&mdev->dps_time);
  1857. ok = drbd_send_delay_probe(mdev, &mdev->meta);
  1858. ok = ok && drbd_send_delay_probe(mdev, &mdev->data);
  1859. mdev->dp_volume_last = mdev->send_cnt;
  1860. mod_timer(&mdev->delay_probe_timer, jiffies + mdev->sync_conf.dp_interval * HZ / 10);
  1861. return ok;
  1862. }
  1863. /* called on sndtimeo
  1864. * returns FALSE if we should retry,
  1865. * TRUE if we think connection is dead
  1866. */
  1867. static int we_should_drop_the_connection(struct drbd_conf *mdev, struct socket *sock)
  1868. {
  1869. int drop_it;
  1870. /* long elapsed = (long)(jiffies - mdev->last_received); */
  1871. drop_it = mdev->meta.socket == sock
  1872. || !mdev->asender.task
  1873. || get_t_state(&mdev->asender) != Running
  1874. || mdev->state.conn < C_CONNECTED;
  1875. if (drop_it)
  1876. return TRUE;
  1877. drop_it = !--mdev->ko_count;
  1878. if (!drop_it) {
  1879. dev_err(DEV, "[%s/%d] sock_sendmsg time expired, ko = %u\n",
  1880. current->comm, current->pid, mdev->ko_count);
  1881. request_ping(mdev);
  1882. }
  1883. return drop_it; /* && (mdev->state == R_PRIMARY) */;
  1884. }
  1885. /* The idea of sendpage seems to be to put some kind of reference
  1886. * to the page into the skb, and to hand it over to the NIC. In
  1887. * this process get_page() gets called.
  1888. *
  1889. * As soon as the page was really sent over the network put_page()
  1890. * gets called by some part of the network layer. [ NIC driver? ]
  1891. *
  1892. * [ get_page() / put_page() increment/decrement the count. If count
  1893. * reaches 0 the page will be freed. ]
  1894. *
  1895. * This works nicely with pages from FSs.
  1896. * But this means that in protocol A we might signal IO completion too early!
  1897. *
  1898. * In order not to corrupt data during a resync we must make sure
  1899. * that we do not reuse our own buffer pages (EEs) to early, therefore
  1900. * we have the net_ee list.
  1901. *
  1902. * XFS seems to have problems, still, it submits pages with page_count == 0!
  1903. * As a workaround, we disable sendpage on pages
  1904. * with page_count == 0 or PageSlab.
  1905. */
  1906. static int _drbd_no_send_page(struct drbd_conf *mdev, struct page *page,
  1907. int offset, size_t size, unsigned msg_flags)
  1908. {
  1909. int sent = drbd_send(mdev, mdev->data.socket, kmap(page) + offset, size, msg_flags);
  1910. kunmap(page);
  1911. if (sent == size)
  1912. mdev->send_cnt += size>>9;
  1913. return sent == size;
  1914. }
  1915. static int _drbd_send_page(struct drbd_conf *mdev, struct page *page,
  1916. int offset, size_t size, unsigned msg_flags)
  1917. {
  1918. mm_segment_t oldfs = get_fs();
  1919. int sent, ok;
  1920. int len = size;
  1921. /* e.g. XFS meta- & log-data is in slab pages, which have a
  1922. * page_count of 0 and/or have PageSlab() set.
  1923. * we cannot use send_page for those, as that does get_page();
  1924. * put_page(); and would cause either a VM_BUG directly, or
  1925. * __page_cache_release a page that would actually still be referenced
  1926. * by someone, leading to some obscure delayed Oops somewhere else. */
  1927. if (disable_sendpage || (page_count(page) < 1) || PageSlab(page))
  1928. return _drbd_no_send_page(mdev, page, offset, size, msg_flags);
  1929. msg_flags |= MSG_NOSIGNAL;
  1930. drbd_update_congested(mdev);
  1931. set_fs(KERNEL_DS);
  1932. do {
  1933. sent = mdev->data.socket->ops->sendpage(mdev->data.socket, page,
  1934. offset, len,
  1935. msg_flags);
  1936. if (sent == -EAGAIN) {
  1937. if (we_should_drop_the_connection(mdev,
  1938. mdev->data.socket))
  1939. break;
  1940. else
  1941. continue;
  1942. }
  1943. if (sent <= 0) {
  1944. dev_warn(DEV, "%s: size=%d len=%d sent=%d\n",
  1945. __func__, (int)size, len, sent);
  1946. break;
  1947. }
  1948. len -= sent;
  1949. offset += sent;
  1950. } while (len > 0 /* THINK && mdev->cstate >= C_CONNECTED*/);
  1951. set_fs(oldfs);
  1952. clear_bit(NET_CONGESTED, &mdev->flags);
  1953. ok = (len == 0);
  1954. if (likely(ok))
  1955. mdev->send_cnt += size>>9;
  1956. return ok;
  1957. }
  1958. static int _drbd_send_bio(struct drbd_conf *mdev, struct bio *bio)
  1959. {
  1960. struct bio_vec *bvec;
  1961. int i;
  1962. /* hint all but last page with MSG_MORE */
  1963. __bio_for_each_segment(bvec, bio, i, 0) {
  1964. if (!_drbd_no_send_page(mdev, bvec->bv_page,
  1965. bvec->bv_offset, bvec->bv_len,
  1966. i == bio->bi_vcnt -1 ? 0 : MSG_MORE))
  1967. return 0;
  1968. }
  1969. return 1;
  1970. }
  1971. static int _drbd_send_zc_bio(struct drbd_conf *mdev, struct bio *bio)
  1972. {
  1973. struct bio_vec *bvec;
  1974. int i;
  1975. /* hint all but last page with MSG_MORE */
  1976. __bio_for_each_segment(bvec, bio, i, 0) {
  1977. if (!_drbd_send_page(mdev, bvec->bv_page,
  1978. bvec->bv_offset, bvec->bv_len,
  1979. i == bio->bi_vcnt -1 ? 0 : MSG_MORE))
  1980. return 0;
  1981. }
  1982. return 1;
  1983. }
  1984. static int _drbd_send_zc_ee(struct drbd_conf *mdev, struct drbd_epoch_entry *e)
  1985. {
  1986. struct page *page = e->pages;
  1987. unsigned len = e->size;
  1988. /* hint all but last page with MSG_MORE */
  1989. page_chain_for_each(page) {
  1990. unsigned l = min_t(unsigned, len, PAGE_SIZE);
  1991. if (!_drbd_send_page(mdev, page, 0, l,
  1992. page_chain_next(page) ? MSG_MORE : 0))
  1993. return 0;
  1994. len -= l;
  1995. }
  1996. return 1;
  1997. }
  1998. static void consider_delay_probes(struct drbd_conf *mdev)
  1999. {
  2000. return;
  2001. }
  2002. static int w_delay_probes(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
  2003. {
  2004. if (!cancel && mdev->state.conn == C_SYNC_SOURCE)
  2005. drbd_send_delay_probes(mdev);
  2006. return 1;
  2007. }
  2008. static void delay_probe_timer_fn(unsigned long data)
  2009. {
  2010. struct drbd_conf *mdev = (struct drbd_conf *) data;
  2011. if (list_empty(&mdev->delay_probe_work.list))
  2012. drbd_queue_work(&mdev->data.work, &mdev->delay_probe_work);
  2013. }
  2014. /* Used to send write requests
  2015. * R_PRIMARY -> Peer (P_DATA)
  2016. */
  2017. int drbd_send_dblock(struct drbd_conf *mdev, struct drbd_request *req)
  2018. {
  2019. int ok = 1;
  2020. struct p_data p;
  2021. unsigned int dp_flags = 0;
  2022. void *dgb;
  2023. int dgs;
  2024. if (!drbd_get_data_sock(mdev))
  2025. return 0;
  2026. dgs = (mdev->agreed_pro_version >= 87 && mdev->integrity_w_tfm) ?
  2027. crypto_hash_digestsize(mdev->integrity_w_tfm) : 0;
  2028. p.head.magic = BE_DRBD_MAGIC;
  2029. p.head.command = cpu_to_be16(P_DATA);
  2030. p.head.length =
  2031. cpu_to_be16(sizeof(p) - sizeof(struct p_header) + dgs + req->size);
  2032. p.sector = cpu_to_be64(req->sector);
  2033. p.block_id = (unsigned long)req;
  2034. p.seq_num = cpu_to_be32(req->seq_num =
  2035. atomic_add_return(1, &mdev->packet_seq));
  2036. dp_flags = 0;
  2037. /* NOTE: no need to check if barriers supported here as we would
  2038. * not pass the test in make_request_common in that case
  2039. */
  2040. if (bio_rw_flagged(req->master_bio, BIO_RW_BARRIER)) {
  2041. dev_err(DEV, "ASSERT FAILED would have set DP_HARDBARRIER\n");
  2042. /* dp_flags |= DP_HARDBARRIER; */
  2043. }
  2044. if (bio_rw_flagged(req->master_bio, BIO_RW_SYNCIO))
  2045. dp_flags |= DP_RW_SYNC;
  2046. /* for now handle SYNCIO and UNPLUG
  2047. * as if they still were one and the same flag */
  2048. if (bio_rw_flagged(req->master_bio, BIO_RW_UNPLUG))
  2049. dp_flags |= DP_RW_SYNC;
  2050. if (mdev->state.conn >= C_SYNC_SOURCE &&
  2051. mdev->state.conn <= C_PAUSED_SYNC_T)
  2052. dp_flags |= DP_MAY_SET_IN_SYNC;
  2053. p.dp_flags = cpu_to_be32(dp_flags);
  2054. set_bit(UNPLUG_REMOTE, &mdev->flags);
  2055. ok = (sizeof(p) ==
  2056. drbd_send(mdev, mdev->data.socket, &p, sizeof(p), dgs ? MSG_MORE : 0));
  2057. if (ok && dgs) {
  2058. dgb = mdev->int_dig_out;
  2059. drbd_csum_bio(mdev, mdev->integrity_w_tfm, req->master_bio, dgb);
  2060. ok = drbd_send(mdev, mdev->data.socket, dgb, dgs, 0);
  2061. }
  2062. if (ok) {
  2063. if (mdev->net_conf->wire_protocol == DRBD_PROT_A)
  2064. ok = _drbd_send_bio(mdev, req->master_bio);
  2065. else
  2066. ok = _drbd_send_zc_bio(mdev, req->master_bio);
  2067. }
  2068. drbd_put_data_sock(mdev);
  2069. if (ok)
  2070. consider_delay_probes(mdev);
  2071. return ok;
  2072. }
  2073. /* answer packet, used to send data back for read requests:
  2074. * Peer -> (diskless) R_PRIMARY (P_DATA_REPLY)
  2075. * C_SYNC_SOURCE -> C_SYNC_TARGET (P_RS_DATA_REPLY)
  2076. */
  2077. int drbd_send_block(struct drbd_conf *mdev, enum drbd_packets cmd,
  2078. struct drbd_epoch_entry *e)
  2079. {
  2080. int ok;
  2081. struct p_data p;
  2082. void *dgb;
  2083. int dgs;
  2084. dgs = (mdev->agreed_pro_version >= 87 && mdev->integrity_w_tfm) ?
  2085. crypto_hash_digestsize(mdev->integrity_w_tfm) : 0;
  2086. p.head.magic = BE_DRBD_MAGIC;
  2087. p.head.command = cpu_to_be16(cmd);
  2088. p.head.length =
  2089. cpu_to_be16(sizeof(p) - sizeof(struct p_header) + dgs + e->size);
  2090. p.sector = cpu_to_be64(e->sector);
  2091. p.block_id = e->block_id;
  2092. /* p.seq_num = 0; No sequence numbers here.. */
  2093. /* Only called by our kernel thread.
  2094. * This one may be interrupted by DRBD_SIG and/or DRBD_SIGKILL
  2095. * in response to admin command or module unload.
  2096. */
  2097. if (!drbd_get_data_sock(mdev))
  2098. return 0;
  2099. ok = sizeof(p) == drbd_send(mdev, mdev->data.socket, &p,
  2100. sizeof(p), dgs ? MSG_MORE : 0);
  2101. if (ok && dgs) {
  2102. dgb = mdev->int_dig_out;
  2103. drbd_csum_ee(mdev, mdev->integrity_w_tfm, e, dgb);
  2104. ok = drbd_send(mdev, mdev->data.socket, dgb, dgs, 0);
  2105. }
  2106. if (ok)
  2107. ok = _drbd_send_zc_ee(mdev, e);
  2108. drbd_put_data_sock(mdev);
  2109. if (ok)
  2110. consider_delay_probes(mdev);
  2111. return ok;
  2112. }
  2113. /*
  2114. drbd_send distinguishes two cases:
  2115. Packets sent via the data socket "sock"
  2116. and packets sent via the meta data socket "msock"
  2117. sock msock
  2118. -----------------+-------------------------+------------------------------
  2119. timeout conf.timeout / 2 conf.timeout / 2
  2120. timeout action send a ping via msock Abort communication
  2121. and close all sockets
  2122. */
  2123. /*
  2124. * you must have down()ed the appropriate [m]sock_mutex elsewhere!
  2125. */
  2126. int drbd_send(struct drbd_conf *mdev, struct socket *sock,
  2127. void *buf, size_t size, unsigned msg_flags)
  2128. {
  2129. struct kvec iov;
  2130. struct msghdr msg;
  2131. int rv, sent = 0;
  2132. if (!sock)
  2133. return -1000;
  2134. /* THINK if (signal_pending) return ... ? */
  2135. iov.iov_base = buf;
  2136. iov.iov_len = size;
  2137. msg.msg_name = NULL;
  2138. msg.msg_namelen = 0;
  2139. msg.msg_control = NULL;
  2140. msg.msg_controllen = 0;
  2141. msg.msg_flags = msg_flags | MSG_NOSIGNAL;
  2142. if (sock == mdev->data.socket) {
  2143. mdev->ko_count = mdev->net_conf->ko_count;
  2144. drbd_update_congested(mdev);
  2145. }
  2146. do {
  2147. /* STRANGE
  2148. * tcp_sendmsg does _not_ use its size parameter at all ?
  2149. *
  2150. * -EAGAIN on timeout, -EINTR on signal.
  2151. */
  2152. /* THINK
  2153. * do we need to block DRBD_SIG if sock == &meta.socket ??
  2154. * otherwise wake_asender() might interrupt some send_*Ack !
  2155. */
  2156. rv = kernel_sendmsg(sock, &msg, &iov, 1, size);
  2157. if (rv == -EAGAIN) {
  2158. if (we_should_drop_the_connection(mdev, sock))
  2159. break;
  2160. else
  2161. continue;
  2162. }
  2163. D_ASSERT(rv != 0);
  2164. if (rv == -EINTR) {
  2165. flush_signals(current);
  2166. rv = 0;
  2167. }
  2168. if (rv < 0)
  2169. break;
  2170. sent += rv;
  2171. iov.iov_base += rv;
  2172. iov.iov_len -= rv;
  2173. } while (sent < size);
  2174. if (sock == mdev->data.socket)
  2175. clear_bit(NET_CONGESTED, &mdev->flags);
  2176. if (rv <= 0) {
  2177. if (rv != -EAGAIN) {
  2178. dev_err(DEV, "%s_sendmsg returned %d\n",
  2179. sock == mdev->meta.socket ? "msock" : "sock",
  2180. rv);
  2181. drbd_force_state(mdev, NS(conn, C_BROKEN_PIPE));
  2182. } else
  2183. drbd_force_state(mdev, NS(conn, C_TIMEOUT));
  2184. }
  2185. return sent;
  2186. }
  2187. static int drbd_open(struct block_device *bdev, fmode_t mode)
  2188. {
  2189. struct drbd_conf *mdev = bdev->bd_disk->private_data;
  2190. unsigned long flags;
  2191. int rv = 0;
  2192. spin_lock_irqsave(&mdev->req_lock, flags);
  2193. /* to have a stable mdev->state.role
  2194. * and no race with updating open_cnt */
  2195. if (mdev->state.role != R_PRIMARY) {
  2196. if (mode & FMODE_WRITE)
  2197. rv = -EROFS;
  2198. else if (!allow_oos)
  2199. rv = -EMEDIUMTYPE;
  2200. }
  2201. if (!rv)
  2202. mdev->open_cnt++;
  2203. spin_unlock_irqrestore(&mdev->req_lock, flags);
  2204. return rv;
  2205. }
  2206. static int drbd_release(struct gendisk *gd, fmode_t mode)
  2207. {
  2208. struct drbd_conf *mdev = gd->private_data;
  2209. mdev->open_cnt--;
  2210. return 0;
  2211. }
  2212. static void drbd_unplug_fn(struct request_queue *q)
  2213. {
  2214. struct drbd_conf *mdev = q->queuedata;
  2215. /* unplug FIRST */
  2216. spin_lock_irq(q->queue_lock);
  2217. blk_remove_plug(q);
  2218. spin_unlock_irq(q->queue_lock);
  2219. /* only if connected */
  2220. spin_lock_irq(&mdev->req_lock);
  2221. if (mdev->state.pdsk >= D_INCONSISTENT && mdev->state.conn >= C_CONNECTED) {
  2222. D_ASSERT(mdev->state.role == R_PRIMARY);
  2223. if (test_and_clear_bit(UNPLUG_REMOTE, &mdev->flags)) {
  2224. /* add to the data.work queue,
  2225. * unless already queued.
  2226. * XXX this might be a good addition to drbd_queue_work
  2227. * anyways, to detect "double queuing" ... */
  2228. if (list_empty(&mdev->unplug_work.list))
  2229. drbd_queue_work(&mdev->data.work,
  2230. &mdev->unplug_work);
  2231. }
  2232. }
  2233. spin_unlock_irq(&mdev->req_lock);
  2234. if (mdev->state.disk >= D_INCONSISTENT)
  2235. drbd_kick_lo(mdev);
  2236. }
  2237. static void drbd_set_defaults(struct drbd_conf *mdev)
  2238. {
  2239. /* This way we get a compile error when sync_conf grows,
  2240. and we forgot to initialize it here */
  2241. mdev->sync_conf = (struct syncer_conf) {
  2242. /* .rate = */ DRBD_RATE_DEF,
  2243. /* .after = */ DRBD_AFTER_DEF,
  2244. /* .al_extents = */ DRBD_AL_EXTENTS_DEF,
  2245. /* .dp_volume = */ DRBD_DP_VOLUME_DEF,
  2246. /* .dp_interval = */ DRBD_DP_INTERVAL_DEF,
  2247. /* .throttle_th = */ DRBD_RS_THROTTLE_TH_DEF,
  2248. /* .hold_off_th = */ DRBD_RS_HOLD_OFF_TH_DEF,
  2249. /* .verify_alg = */ {}, 0,
  2250. /* .cpu_mask = */ {}, 0,
  2251. /* .csums_alg = */ {}, 0,
  2252. /* .use_rle = */ 0
  2253. };
  2254. /* Have to use that way, because the layout differs between
  2255. big endian and little endian */
  2256. mdev->state = (union drbd_state) {
  2257. { .role = R_SECONDARY,
  2258. .peer = R_UNKNOWN,
  2259. .conn = C_STANDALONE,
  2260. .disk = D_DISKLESS,
  2261. .pdsk = D_UNKNOWN,
  2262. .susp = 0
  2263. } };
  2264. }
  2265. void drbd_init_set_defaults(struct drbd_conf *mdev)
  2266. {
  2267. /* the memset(,0,) did most of this.
  2268. * note: only assignments, no allocation in here */
  2269. drbd_set_defaults(mdev);
  2270. /* for now, we do NOT yet support it,
  2271. * even though we start some framework
  2272. * to eventually support barriers */
  2273. set_bit(NO_BARRIER_SUPP, &mdev->flags);
  2274. atomic_set(&mdev->ap_bio_cnt, 0);
  2275. atomic_set(&mdev->ap_pending_cnt, 0);
  2276. atomic_set(&mdev->rs_pending_cnt, 0);
  2277. atomic_set(&mdev->unacked_cnt, 0);
  2278. atomic_set(&mdev->local_cnt, 0);
  2279. atomic_set(&mdev->net_cnt, 0);
  2280. atomic_set(&mdev->packet_seq, 0);
  2281. atomic_set(&mdev->pp_in_use, 0);
  2282. mutex_init(&mdev->md_io_mutex);
  2283. mutex_init(&mdev->data.mutex);
  2284. mutex_init(&mdev->meta.mutex);
  2285. sema_init(&mdev->data.work.s, 0);
  2286. sema_init(&mdev->meta.work.s, 0);
  2287. mutex_init(&mdev->state_mutex);
  2288. spin_lock_init(&mdev->data.work.q_lock);
  2289. spin_lock_init(&mdev->meta.work.q_lock);
  2290. spin_lock_init(&mdev->al_lock);
  2291. spin_lock_init(&mdev->req_lock);
  2292. spin_lock_init(&mdev->peer_seq_lock);
  2293. spin_lock_init(&mdev->epoch_lock);
  2294. INIT_LIST_HEAD(&mdev->active_ee);
  2295. INIT_LIST_HEAD(&mdev->sync_ee);
  2296. INIT_LIST_HEAD(&mdev->done_ee);
  2297. INIT_LIST_HEAD(&mdev->read_ee);
  2298. INIT_LIST_HEAD(&mdev->net_ee);
  2299. INIT_LIST_HEAD(&mdev->resync_reads);
  2300. INIT_LIST_HEAD(&mdev->data.work.q);
  2301. INIT_LIST_HEAD(&mdev->meta.work.q);
  2302. INIT_LIST_HEAD(&mdev->resync_work.list);
  2303. INIT_LIST_HEAD(&mdev->unplug_work.list);
  2304. INIT_LIST_HEAD(&mdev->md_sync_work.list);
  2305. INIT_LIST_HEAD(&mdev->bm_io_work.w.list);
  2306. INIT_LIST_HEAD(&mdev->delay_probes);
  2307. INIT_LIST_HEAD(&mdev->delay_probe_work.list);
  2308. mdev->resync_work.cb = w_resync_inactive;
  2309. mdev->unplug_work.cb = w_send_write_hint;
  2310. mdev->md_sync_work.cb = w_md_sync;
  2311. mdev->bm_io_work.w.cb = w_bitmap_io;
  2312. mdev->delay_probe_work.cb = w_delay_probes;
  2313. init_timer(&mdev->resync_timer);
  2314. init_timer(&mdev->md_sync_timer);
  2315. init_timer(&mdev->delay_probe_timer);
  2316. mdev->resync_timer.function = resync_timer_fn;
  2317. mdev->resync_timer.data = (unsigned long) mdev;
  2318. mdev->md_sync_timer.function = md_sync_timer_fn;
  2319. mdev->md_sync_timer.data = (unsigned long) mdev;
  2320. mdev->delay_probe_timer.function = delay_probe_timer_fn;
  2321. mdev->delay_probe_timer.data = (unsigned long) mdev;
  2322. init_waitqueue_head(&mdev->misc_wait);
  2323. init_waitqueue_head(&mdev->state_wait);
  2324. init_waitqueue_head(&mdev->ee_wait);
  2325. init_waitqueue_head(&mdev->al_wait);
  2326. init_waitqueue_head(&mdev->seq_wait);
  2327. drbd_thread_init(mdev, &mdev->receiver, drbdd_init);
  2328. drbd_thread_init(mdev, &mdev->worker, drbd_worker);
  2329. drbd_thread_init(mdev, &mdev->asender, drbd_asender);
  2330. mdev->agreed_pro_version = PRO_VERSION_MAX;
  2331. mdev->write_ordering = WO_bio_barrier;
  2332. mdev->resync_wenr = LC_FREE;
  2333. }
  2334. void drbd_mdev_cleanup(struct drbd_conf *mdev)
  2335. {
  2336. if (mdev->receiver.t_state != None)
  2337. dev_err(DEV, "ASSERT FAILED: receiver t_state == %d expected 0.\n",
  2338. mdev->receiver.t_state);
  2339. /* no need to lock it, I'm the only thread alive */
  2340. if (atomic_read(&mdev->current_epoch->epoch_size) != 0)
  2341. dev_err(DEV, "epoch_size:%d\n", atomic_read(&mdev->current_epoch->epoch_size));
  2342. mdev->al_writ_cnt =
  2343. mdev->bm_writ_cnt =
  2344. mdev->read_cnt =
  2345. mdev->recv_cnt =
  2346. mdev->send_cnt =
  2347. mdev->writ_cnt =
  2348. mdev->p_size =
  2349. mdev->rs_start =
  2350. mdev->rs_total =
  2351. mdev->rs_failed =
  2352. mdev->rs_mark_left =
  2353. mdev->rs_mark_time = 0;
  2354. D_ASSERT(mdev->net_conf == NULL);
  2355. drbd_set_my_capacity(mdev, 0);
  2356. if (mdev->bitmap) {
  2357. /* maybe never allocated. */
  2358. drbd_bm_resize(mdev, 0, 1);
  2359. drbd_bm_cleanup(mdev);
  2360. }
  2361. drbd_free_resources(mdev);
  2362. /*
  2363. * currently we drbd_init_ee only on module load, so
  2364. * we may do drbd_release_ee only on module unload!
  2365. */
  2366. D_ASSERT(list_empty(&mdev->active_ee));
  2367. D_ASSERT(list_empty(&mdev->sync_ee));
  2368. D_ASSERT(list_empty(&mdev->done_ee));
  2369. D_ASSERT(list_empty(&mdev->read_ee));
  2370. D_ASSERT(list_empty(&mdev->net_ee));
  2371. D_ASSERT(list_empty(&mdev->resync_reads));
  2372. D_ASSERT(list_empty(&mdev->data.work.q));
  2373. D_ASSERT(list_empty(&mdev->meta.work.q));
  2374. D_ASSERT(list_empty(&mdev->resync_work.list));
  2375. D_ASSERT(list_empty(&mdev->unplug_work.list));
  2376. }
  2377. static void drbd_destroy_mempools(void)
  2378. {
  2379. struct page *page;
  2380. while (drbd_pp_pool) {
  2381. page = drbd_pp_pool;
  2382. drbd_pp_pool = (struct page *)page_private(page);
  2383. __free_page(page);
  2384. drbd_pp_vacant--;
  2385. }
  2386. /* D_ASSERT(atomic_read(&drbd_pp_vacant)==0); */
  2387. if (drbd_ee_mempool)
  2388. mempool_destroy(drbd_ee_mempool);
  2389. if (drbd_request_mempool)
  2390. mempool_destroy(drbd_request_mempool);
  2391. if (drbd_ee_cache)
  2392. kmem_cache_destroy(drbd_ee_cache);
  2393. if (drbd_request_cache)
  2394. kmem_cache_destroy(drbd_request_cache);
  2395. if (drbd_bm_ext_cache)
  2396. kmem_cache_destroy(drbd_bm_ext_cache);
  2397. if (drbd_al_ext_cache)
  2398. kmem_cache_destroy(drbd_al_ext_cache);
  2399. drbd_ee_mempool = NULL;
  2400. drbd_request_mempool = NULL;
  2401. drbd_ee_cache = NULL;
  2402. drbd_request_cache = NULL;
  2403. drbd_bm_ext_cache = NULL;
  2404. drbd_al_ext_cache = NULL;
  2405. return;
  2406. }
  2407. static int drbd_create_mempools(void)
  2408. {
  2409. struct page *page;
  2410. const int number = (DRBD_MAX_SEGMENT_SIZE/PAGE_SIZE) * minor_count;
  2411. int i;
  2412. /* prepare our caches and mempools */
  2413. drbd_request_mempool = NULL;
  2414. drbd_ee_cache = NULL;
  2415. drbd_request_cache = NULL;
  2416. drbd_bm_ext_cache = NULL;
  2417. drbd_al_ext_cache = NULL;
  2418. drbd_pp_pool = NULL;
  2419. /* caches */
  2420. drbd_request_cache = kmem_cache_create(
  2421. "drbd_req", sizeof(struct drbd_request), 0, 0, NULL);
  2422. if (drbd_request_cache == NULL)
  2423. goto Enomem;
  2424. drbd_ee_cache = kmem_cache_create(
  2425. "drbd_ee", sizeof(struct drbd_epoch_entry), 0, 0, NULL);
  2426. if (drbd_ee_cache == NULL)
  2427. goto Enomem;
  2428. drbd_bm_ext_cache = kmem_cache_create(
  2429. "drbd_bm", sizeof(struct bm_extent), 0, 0, NULL);
  2430. if (drbd_bm_ext_cache == NULL)
  2431. goto Enomem;
  2432. drbd_al_ext_cache = kmem_cache_create(
  2433. "drbd_al", sizeof(struct lc_element), 0, 0, NULL);
  2434. if (drbd_al_ext_cache == NULL)
  2435. goto Enomem;
  2436. /* mempools */
  2437. drbd_request_mempool = mempool_create(number,
  2438. mempool_alloc_slab, mempool_free_slab, drbd_request_cache);
  2439. if (drbd_request_mempool == NULL)
  2440. goto Enomem;
  2441. drbd_ee_mempool = mempool_create(number,
  2442. mempool_alloc_slab, mempool_free_slab, drbd_ee_cache);
  2443. if (drbd_request_mempool == NULL)
  2444. goto Enomem;
  2445. /* drbd's page pool */
  2446. spin_lock_init(&drbd_pp_lock);
  2447. for (i = 0; i < number; i++) {
  2448. page = alloc_page(GFP_HIGHUSER);
  2449. if (!page)
  2450. goto Enomem;
  2451. set_page_private(page, (unsigned long)drbd_pp_pool);
  2452. drbd_pp_pool = page;
  2453. }
  2454. drbd_pp_vacant = number;
  2455. return 0;
  2456. Enomem:
  2457. drbd_destroy_mempools(); /* in case we allocated some */
  2458. return -ENOMEM;
  2459. }
  2460. static int drbd_notify_sys(struct notifier_block *this, unsigned long code,
  2461. void *unused)
  2462. {
  2463. /* just so we have it. you never know what interesting things we
  2464. * might want to do here some day...
  2465. */
  2466. return NOTIFY_DONE;
  2467. }
  2468. static struct notifier_block drbd_notifier = {
  2469. .notifier_call = drbd_notify_sys,
  2470. };
  2471. static void drbd_release_ee_lists(struct drbd_conf *mdev)
  2472. {
  2473. int rr;
  2474. rr = drbd_release_ee(mdev, &mdev->active_ee);
  2475. if (rr)
  2476. dev_err(DEV, "%d EEs in active list found!\n", rr);
  2477. rr = drbd_release_ee(mdev, &mdev->sync_ee);
  2478. if (rr)
  2479. dev_err(DEV, "%d EEs in sync list found!\n", rr);
  2480. rr = drbd_release_ee(mdev, &mdev->read_ee);
  2481. if (rr)
  2482. dev_err(DEV, "%d EEs in read list found!\n", rr);
  2483. rr = drbd_release_ee(mdev, &mdev->done_ee);
  2484. if (rr)
  2485. dev_err(DEV, "%d EEs in done list found!\n", rr);
  2486. rr = drbd_release_ee(mdev, &mdev->net_ee);
  2487. if (rr)
  2488. dev_err(DEV, "%d EEs in net list found!\n", rr);
  2489. }
  2490. /* caution. no locking.
  2491. * currently only used from module cleanup code. */
  2492. static void drbd_delete_device(unsigned int minor)
  2493. {
  2494. struct drbd_conf *mdev = minor_to_mdev(minor);
  2495. if (!mdev)
  2496. return;
  2497. /* paranoia asserts */
  2498. if (mdev->open_cnt != 0)
  2499. dev_err(DEV, "open_cnt = %d in %s:%u", mdev->open_cnt,
  2500. __FILE__ , __LINE__);
  2501. ERR_IF (!list_empty(&mdev->data.work.q)) {
  2502. struct list_head *lp;
  2503. list_for_each(lp, &mdev->data.work.q) {
  2504. dev_err(DEV, "lp = %p\n", lp);
  2505. }
  2506. };
  2507. /* end paranoia asserts */
  2508. del_gendisk(mdev->vdisk);
  2509. /* cleanup stuff that may have been allocated during
  2510. * device (re-)configuration or state changes */
  2511. if (mdev->this_bdev)
  2512. bdput(mdev->this_bdev);
  2513. drbd_free_resources(mdev);
  2514. drbd_release_ee_lists(mdev);
  2515. /* should be free'd on disconnect? */
  2516. kfree(mdev->ee_hash);
  2517. /*
  2518. mdev->ee_hash_s = 0;
  2519. mdev->ee_hash = NULL;
  2520. */
  2521. lc_destroy(mdev->act_log);
  2522. lc_destroy(mdev->resync);
  2523. kfree(mdev->p_uuid);
  2524. /* mdev->p_uuid = NULL; */
  2525. kfree(mdev->int_dig_out);
  2526. kfree(mdev->int_dig_in);
  2527. kfree(mdev->int_dig_vv);
  2528. /* cleanup the rest that has been
  2529. * allocated from drbd_new_device
  2530. * and actually free the mdev itself */
  2531. drbd_free_mdev(mdev);
  2532. }
  2533. static void drbd_cleanup(void)
  2534. {
  2535. unsigned int i;
  2536. unregister_reboot_notifier(&drbd_notifier);
  2537. drbd_nl_cleanup();
  2538. if (minor_table) {
  2539. if (drbd_proc)
  2540. remove_proc_entry("drbd", NULL);
  2541. i = minor_count;
  2542. while (i--)
  2543. drbd_delete_device(i);
  2544. drbd_destroy_mempools();
  2545. }
  2546. kfree(minor_table);
  2547. unregister_blkdev(DRBD_MAJOR, "drbd");
  2548. printk(KERN_INFO "drbd: module cleanup done.\n");
  2549. }
  2550. /**
  2551. * drbd_congested() - Callback for pdflush
  2552. * @congested_data: User data
  2553. * @bdi_bits: Bits pdflush is currently interested in
  2554. *
  2555. * Returns 1<<BDI_async_congested and/or 1<<BDI_sync_congested if we are congested.
  2556. */
  2557. static int drbd_congested(void *congested_data, int bdi_bits)
  2558. {
  2559. struct drbd_conf *mdev = congested_data;
  2560. struct request_queue *q;
  2561. char reason = '-';
  2562. int r = 0;
  2563. if (!__inc_ap_bio_cond(mdev)) {
  2564. /* DRBD has frozen IO */
  2565. r = bdi_bits;
  2566. reason = 'd';
  2567. goto out;
  2568. }
  2569. if (get_ldev(mdev)) {
  2570. q = bdev_get_queue(mdev->ldev->backing_bdev);
  2571. r = bdi_congested(&q->backing_dev_info, bdi_bits);
  2572. put_ldev(mdev);
  2573. if (r)
  2574. reason = 'b';
  2575. }
  2576. if (bdi_bits & (1 << BDI_async_congested) && test_bit(NET_CONGESTED, &mdev->flags)) {
  2577. r |= (1 << BDI_async_congested);
  2578. reason = reason == 'b' ? 'a' : 'n';
  2579. }
  2580. out:
  2581. mdev->congestion_reason = reason;
  2582. return r;
  2583. }
  2584. struct drbd_conf *drbd_new_device(unsigned int minor)
  2585. {
  2586. struct drbd_conf *mdev;
  2587. struct gendisk *disk;
  2588. struct request_queue *q;
  2589. /* GFP_KERNEL, we are outside of all write-out paths */
  2590. mdev = kzalloc(sizeof(struct drbd_conf), GFP_KERNEL);
  2591. if (!mdev)
  2592. return NULL;
  2593. if (!zalloc_cpumask_var(&mdev->cpu_mask, GFP_KERNEL))
  2594. goto out_no_cpumask;
  2595. mdev->minor = minor;
  2596. drbd_init_set_defaults(mdev);
  2597. q = blk_alloc_queue(GFP_KERNEL);
  2598. if (!q)
  2599. goto out_no_q;
  2600. mdev->rq_queue = q;
  2601. q->queuedata = mdev;
  2602. disk = alloc_disk(1);
  2603. if (!disk)
  2604. goto out_no_disk;
  2605. mdev->vdisk = disk;
  2606. set_disk_ro(disk, TRUE);
  2607. disk->queue = q;
  2608. disk->major = DRBD_MAJOR;
  2609. disk->first_minor = minor;
  2610. disk->fops = &drbd_ops;
  2611. sprintf(disk->disk_name, "drbd%d", minor);
  2612. disk->private_data = mdev;
  2613. mdev->this_bdev = bdget(MKDEV(DRBD_MAJOR, minor));
  2614. /* we have no partitions. we contain only ourselves. */
  2615. mdev->this_bdev->bd_contains = mdev->this_bdev;
  2616. q->backing_dev_info.congested_fn = drbd_congested;
  2617. q->backing_dev_info.congested_data = mdev;
  2618. blk_queue_make_request(q, drbd_make_request_26);
  2619. blk_queue_max_segment_size(q, DRBD_MAX_SEGMENT_SIZE);
  2620. blk_queue_bounce_limit(q, BLK_BOUNCE_ANY);
  2621. blk_queue_merge_bvec(q, drbd_merge_bvec);
  2622. q->queue_lock = &mdev->req_lock; /* needed since we use */
  2623. /* plugging on a queue, that actually has no requests! */
  2624. q->unplug_fn = drbd_unplug_fn;
  2625. mdev->md_io_page = alloc_page(GFP_KERNEL);
  2626. if (!mdev->md_io_page)
  2627. goto out_no_io_page;
  2628. if (drbd_bm_init(mdev))
  2629. goto out_no_bitmap;
  2630. /* no need to lock access, we are still initializing this minor device. */
  2631. if (!tl_init(mdev))
  2632. goto out_no_tl;
  2633. mdev->app_reads_hash = kzalloc(APP_R_HSIZE*sizeof(void *), GFP_KERNEL);
  2634. if (!mdev->app_reads_hash)
  2635. goto out_no_app_reads;
  2636. mdev->current_epoch = kzalloc(sizeof(struct drbd_epoch), GFP_KERNEL);
  2637. if (!mdev->current_epoch)
  2638. goto out_no_epoch;
  2639. INIT_LIST_HEAD(&mdev->current_epoch->list);
  2640. mdev->epochs = 1;
  2641. return mdev;
  2642. /* out_whatever_else:
  2643. kfree(mdev->current_epoch); */
  2644. out_no_epoch:
  2645. kfree(mdev->app_reads_hash);
  2646. out_no_app_reads:
  2647. tl_cleanup(mdev);
  2648. out_no_tl:
  2649. drbd_bm_cleanup(mdev);
  2650. out_no_bitmap:
  2651. __free_page(mdev->md_io_page);
  2652. out_no_io_page:
  2653. put_disk(disk);
  2654. out_no_disk:
  2655. blk_cleanup_queue(q);
  2656. out_no_q:
  2657. free_cpumask_var(mdev->cpu_mask);
  2658. out_no_cpumask:
  2659. kfree(mdev);
  2660. return NULL;
  2661. }
  2662. /* counterpart of drbd_new_device.
  2663. * last part of drbd_delete_device. */
  2664. void drbd_free_mdev(struct drbd_conf *mdev)
  2665. {
  2666. kfree(mdev->current_epoch);
  2667. kfree(mdev->app_reads_hash);
  2668. tl_cleanup(mdev);
  2669. if (mdev->bitmap) /* should no longer be there. */
  2670. drbd_bm_cleanup(mdev);
  2671. __free_page(mdev->md_io_page);
  2672. put_disk(mdev->vdisk);
  2673. blk_cleanup_queue(mdev->rq_queue);
  2674. free_cpumask_var(mdev->cpu_mask);
  2675. kfree(mdev);
  2676. }
  2677. int __init drbd_init(void)
  2678. {
  2679. int err;
  2680. if (sizeof(struct p_handshake) != 80) {
  2681. printk(KERN_ERR
  2682. "drbd: never change the size or layout "
  2683. "of the HandShake packet.\n");
  2684. return -EINVAL;
  2685. }
  2686. if (1 > minor_count || minor_count > 255) {
  2687. printk(KERN_ERR
  2688. "drbd: invalid minor_count (%d)\n", minor_count);
  2689. #ifdef MODULE
  2690. return -EINVAL;
  2691. #else
  2692. minor_count = 8;
  2693. #endif
  2694. }
  2695. err = drbd_nl_init();
  2696. if (err)
  2697. return err;
  2698. err = register_blkdev(DRBD_MAJOR, "drbd");
  2699. if (err) {
  2700. printk(KERN_ERR
  2701. "drbd: unable to register block device major %d\n",
  2702. DRBD_MAJOR);
  2703. return err;
  2704. }
  2705. register_reboot_notifier(&drbd_notifier);
  2706. /*
  2707. * allocate all necessary structs
  2708. */
  2709. err = -ENOMEM;
  2710. init_waitqueue_head(&drbd_pp_wait);
  2711. drbd_proc = NULL; /* play safe for drbd_cleanup */
  2712. minor_table = kzalloc(sizeof(struct drbd_conf *)*minor_count,
  2713. GFP_KERNEL);
  2714. if (!minor_table)
  2715. goto Enomem;
  2716. err = drbd_create_mempools();
  2717. if (err)
  2718. goto Enomem;
  2719. drbd_proc = proc_create_data("drbd", S_IFREG | S_IRUGO , NULL, &drbd_proc_fops, NULL);
  2720. if (!drbd_proc) {
  2721. printk(KERN_ERR "drbd: unable to register proc file\n");
  2722. goto Enomem;
  2723. }
  2724. rwlock_init(&global_state_lock);
  2725. printk(KERN_INFO "drbd: initialized. "
  2726. "Version: " REL_VERSION " (api:%d/proto:%d-%d)\n",
  2727. API_VERSION, PRO_VERSION_MIN, PRO_VERSION_MAX);
  2728. printk(KERN_INFO "drbd: %s\n", drbd_buildtag());
  2729. printk(KERN_INFO "drbd: registered as block device major %d\n",
  2730. DRBD_MAJOR);
  2731. printk(KERN_INFO "drbd: minor_table @ 0x%p\n", minor_table);
  2732. return 0; /* Success! */
  2733. Enomem:
  2734. drbd_cleanup();
  2735. if (err == -ENOMEM)
  2736. /* currently always the case */
  2737. printk(KERN_ERR "drbd: ran out of memory\n");
  2738. else
  2739. printk(KERN_ERR "drbd: initialization failure\n");
  2740. return err;
  2741. }
  2742. void drbd_free_bc(struct drbd_backing_dev *ldev)
  2743. {
  2744. if (ldev == NULL)
  2745. return;
  2746. bd_release(ldev->backing_bdev);
  2747. bd_release(ldev->md_bdev);
  2748. fput(ldev->lo_file);
  2749. fput(ldev->md_file);
  2750. kfree(ldev);
  2751. }
  2752. void drbd_free_sock(struct drbd_conf *mdev)
  2753. {
  2754. if (mdev->data.socket) {
  2755. mutex_lock(&mdev->data.mutex);
  2756. kernel_sock_shutdown(mdev->data.socket, SHUT_RDWR);
  2757. sock_release(mdev->data.socket);
  2758. mdev->data.socket = NULL;
  2759. mutex_unlock(&mdev->data.mutex);
  2760. }
  2761. if (mdev->meta.socket) {
  2762. mutex_lock(&mdev->meta.mutex);
  2763. kernel_sock_shutdown(mdev->meta.socket, SHUT_RDWR);
  2764. sock_release(mdev->meta.socket);
  2765. mdev->meta.socket = NULL;
  2766. mutex_unlock(&mdev->meta.mutex);
  2767. }
  2768. }
  2769. void drbd_free_resources(struct drbd_conf *mdev)
  2770. {
  2771. crypto_free_hash(mdev->csums_tfm);
  2772. mdev->csums_tfm = NULL;
  2773. crypto_free_hash(mdev->verify_tfm);
  2774. mdev->verify_tfm = NULL;
  2775. crypto_free_hash(mdev->cram_hmac_tfm);
  2776. mdev->cram_hmac_tfm = NULL;
  2777. crypto_free_hash(mdev->integrity_w_tfm);
  2778. mdev->integrity_w_tfm = NULL;
  2779. crypto_free_hash(mdev->integrity_r_tfm);
  2780. mdev->integrity_r_tfm = NULL;
  2781. drbd_free_sock(mdev);
  2782. __no_warn(local,
  2783. drbd_free_bc(mdev->ldev);
  2784. mdev->ldev = NULL;);
  2785. }
  2786. /* meta data management */
  2787. struct meta_data_on_disk {
  2788. u64 la_size; /* last agreed size. */
  2789. u64 uuid[UI_SIZE]; /* UUIDs. */
  2790. u64 device_uuid;
  2791. u64 reserved_u64_1;
  2792. u32 flags; /* MDF */
  2793. u32 magic;
  2794. u32 md_size_sect;
  2795. u32 al_offset; /* offset to this block */
  2796. u32 al_nr_extents; /* important for restoring the AL */
  2797. /* `-- act_log->nr_elements <-- sync_conf.al_extents */
  2798. u32 bm_offset; /* offset to the bitmap, from here */
  2799. u32 bm_bytes_per_bit; /* BM_BLOCK_SIZE */
  2800. u32 reserved_u32[4];
  2801. } __packed;
  2802. /**
  2803. * drbd_md_sync() - Writes the meta data super block if the MD_DIRTY flag bit is set
  2804. * @mdev: DRBD device.
  2805. */
  2806. void drbd_md_sync(struct drbd_conf *mdev)
  2807. {
  2808. struct meta_data_on_disk *buffer;
  2809. sector_t sector;
  2810. int i;
  2811. if (!test_and_clear_bit(MD_DIRTY, &mdev->flags))
  2812. return;
  2813. del_timer(&mdev->md_sync_timer);
  2814. /* We use here D_FAILED and not D_ATTACHING because we try to write
  2815. * metadata even if we detach due to a disk failure! */
  2816. if (!get_ldev_if_state(mdev, D_FAILED))
  2817. return;
  2818. mutex_lock(&mdev->md_io_mutex);
  2819. buffer = (struct meta_data_on_disk *)page_address(mdev->md_io_page);
  2820. memset(buffer, 0, 512);
  2821. buffer->la_size = cpu_to_be64(drbd_get_capacity(mdev->this_bdev));
  2822. for (i = UI_CURRENT; i < UI_SIZE; i++)
  2823. buffer->uuid[i] = cpu_to_be64(mdev->ldev->md.uuid[i]);
  2824. buffer->flags = cpu_to_be32(mdev->ldev->md.flags);
  2825. buffer->magic = cpu_to_be32(DRBD_MD_MAGIC);
  2826. buffer->md_size_sect = cpu_to_be32(mdev->ldev->md.md_size_sect);
  2827. buffer->al_offset = cpu_to_be32(mdev->ldev->md.al_offset);
  2828. buffer->al_nr_extents = cpu_to_be32(mdev->act_log->nr_elements);
  2829. buffer->bm_bytes_per_bit = cpu_to_be32(BM_BLOCK_SIZE);
  2830. buffer->device_uuid = cpu_to_be64(mdev->ldev->md.device_uuid);
  2831. buffer->bm_offset = cpu_to_be32(mdev->ldev->md.bm_offset);
  2832. D_ASSERT(drbd_md_ss__(mdev, mdev->ldev) == mdev->ldev->md.md_offset);
  2833. sector = mdev->ldev->md.md_offset;
  2834. if (drbd_md_sync_page_io(mdev, mdev->ldev, sector, WRITE)) {
  2835. clear_bit(MD_DIRTY, &mdev->flags);
  2836. } else {
  2837. /* this was a try anyways ... */
  2838. dev_err(DEV, "meta data update failed!\n");
  2839. drbd_chk_io_error(mdev, 1, TRUE);
  2840. }
  2841. /* Update mdev->ldev->md.la_size_sect,
  2842. * since we updated it on metadata. */
  2843. mdev->ldev->md.la_size_sect = drbd_get_capacity(mdev->this_bdev);
  2844. mutex_unlock(&mdev->md_io_mutex);
  2845. put_ldev(mdev);
  2846. }
  2847. /**
  2848. * drbd_md_read() - Reads in the meta data super block
  2849. * @mdev: DRBD device.
  2850. * @bdev: Device from which the meta data should be read in.
  2851. *
  2852. * Return 0 (NO_ERROR) on success, and an enum drbd_ret_codes in case
  2853. * something goes wrong. Currently only: ERR_IO_MD_DISK, ERR_MD_INVALID.
  2854. */
  2855. int drbd_md_read(struct drbd_conf *mdev, struct drbd_backing_dev *bdev)
  2856. {
  2857. struct meta_data_on_disk *buffer;
  2858. int i, rv = NO_ERROR;
  2859. if (!get_ldev_if_state(mdev, D_ATTACHING))
  2860. return ERR_IO_MD_DISK;
  2861. mutex_lock(&mdev->md_io_mutex);
  2862. buffer = (struct meta_data_on_disk *)page_address(mdev->md_io_page);
  2863. if (!drbd_md_sync_page_io(mdev, bdev, bdev->md.md_offset, READ)) {
  2864. /* NOTE: cant do normal error processing here as this is
  2865. called BEFORE disk is attached */
  2866. dev_err(DEV, "Error while reading metadata.\n");
  2867. rv = ERR_IO_MD_DISK;
  2868. goto err;
  2869. }
  2870. if (be32_to_cpu(buffer->magic) != DRBD_MD_MAGIC) {
  2871. dev_err(DEV, "Error while reading metadata, magic not found.\n");
  2872. rv = ERR_MD_INVALID;
  2873. goto err;
  2874. }
  2875. if (be32_to_cpu(buffer->al_offset) != bdev->md.al_offset) {
  2876. dev_err(DEV, "unexpected al_offset: %d (expected %d)\n",
  2877. be32_to_cpu(buffer->al_offset), bdev->md.al_offset);
  2878. rv = ERR_MD_INVALID;
  2879. goto err;
  2880. }
  2881. if (be32_to_cpu(buffer->bm_offset) != bdev->md.bm_offset) {
  2882. dev_err(DEV, "unexpected bm_offset: %d (expected %d)\n",
  2883. be32_to_cpu(buffer->bm_offset), bdev->md.bm_offset);
  2884. rv = ERR_MD_INVALID;
  2885. goto err;
  2886. }
  2887. if (be32_to_cpu(buffer->md_size_sect) != bdev->md.md_size_sect) {
  2888. dev_err(DEV, "unexpected md_size: %u (expected %u)\n",
  2889. be32_to_cpu(buffer->md_size_sect), bdev->md.md_size_sect);
  2890. rv = ERR_MD_INVALID;
  2891. goto err;
  2892. }
  2893. if (be32_to_cpu(buffer->bm_bytes_per_bit) != BM_BLOCK_SIZE) {
  2894. dev_err(DEV, "unexpected bm_bytes_per_bit: %u (expected %u)\n",
  2895. be32_to_cpu(buffer->bm_bytes_per_bit), BM_BLOCK_SIZE);
  2896. rv = ERR_MD_INVALID;
  2897. goto err;
  2898. }
  2899. bdev->md.la_size_sect = be64_to_cpu(buffer->la_size);
  2900. for (i = UI_CURRENT; i < UI_SIZE; i++)
  2901. bdev->md.uuid[i] = be64_to_cpu(buffer->uuid[i]);
  2902. bdev->md.flags = be32_to_cpu(buffer->flags);
  2903. mdev->sync_conf.al_extents = be32_to_cpu(buffer->al_nr_extents);
  2904. bdev->md.device_uuid = be64_to_cpu(buffer->device_uuid);
  2905. if (mdev->sync_conf.al_extents < 7)
  2906. mdev->sync_conf.al_extents = 127;
  2907. err:
  2908. mutex_unlock(&mdev->md_io_mutex);
  2909. put_ldev(mdev);
  2910. return rv;
  2911. }
  2912. /**
  2913. * drbd_md_mark_dirty() - Mark meta data super block as dirty
  2914. * @mdev: DRBD device.
  2915. *
  2916. * Call this function if you change anything that should be written to
  2917. * the meta-data super block. This function sets MD_DIRTY, and starts a
  2918. * timer that ensures that within five seconds you have to call drbd_md_sync().
  2919. */
  2920. void drbd_md_mark_dirty(struct drbd_conf *mdev)
  2921. {
  2922. set_bit(MD_DIRTY, &mdev->flags);
  2923. mod_timer(&mdev->md_sync_timer, jiffies + 5*HZ);
  2924. }
  2925. static void drbd_uuid_move_history(struct drbd_conf *mdev) __must_hold(local)
  2926. {
  2927. int i;
  2928. for (i = UI_HISTORY_START; i < UI_HISTORY_END; i++)
  2929. mdev->ldev->md.uuid[i+1] = mdev->ldev->md.uuid[i];
  2930. }
  2931. void _drbd_uuid_set(struct drbd_conf *mdev, int idx, u64 val) __must_hold(local)
  2932. {
  2933. if (idx == UI_CURRENT) {
  2934. if (mdev->state.role == R_PRIMARY)
  2935. val |= 1;
  2936. else
  2937. val &= ~((u64)1);
  2938. drbd_set_ed_uuid(mdev, val);
  2939. }
  2940. mdev->ldev->md.uuid[idx] = val;
  2941. drbd_md_mark_dirty(mdev);
  2942. }
  2943. void drbd_uuid_set(struct drbd_conf *mdev, int idx, u64 val) __must_hold(local)
  2944. {
  2945. if (mdev->ldev->md.uuid[idx]) {
  2946. drbd_uuid_move_history(mdev);
  2947. mdev->ldev->md.uuid[UI_HISTORY_START] = mdev->ldev->md.uuid[idx];
  2948. }
  2949. _drbd_uuid_set(mdev, idx, val);
  2950. }
  2951. /**
  2952. * drbd_uuid_new_current() - Creates a new current UUID
  2953. * @mdev: DRBD device.
  2954. *
  2955. * Creates a new current UUID, and rotates the old current UUID into
  2956. * the bitmap slot. Causes an incremental resync upon next connect.
  2957. */
  2958. void drbd_uuid_new_current(struct drbd_conf *mdev) __must_hold(local)
  2959. {
  2960. u64 val;
  2961. dev_info(DEV, "Creating new current UUID\n");
  2962. D_ASSERT(mdev->ldev->md.uuid[UI_BITMAP] == 0);
  2963. mdev->ldev->md.uuid[UI_BITMAP] = mdev->ldev->md.uuid[UI_CURRENT];
  2964. get_random_bytes(&val, sizeof(u64));
  2965. _drbd_uuid_set(mdev, UI_CURRENT, val);
  2966. }
  2967. void drbd_uuid_set_bm(struct drbd_conf *mdev, u64 val) __must_hold(local)
  2968. {
  2969. if (mdev->ldev->md.uuid[UI_BITMAP] == 0 && val == 0)
  2970. return;
  2971. if (val == 0) {
  2972. drbd_uuid_move_history(mdev);
  2973. mdev->ldev->md.uuid[UI_HISTORY_START] = mdev->ldev->md.uuid[UI_BITMAP];
  2974. mdev->ldev->md.uuid[UI_BITMAP] = 0;
  2975. } else {
  2976. if (mdev->ldev->md.uuid[UI_BITMAP])
  2977. dev_warn(DEV, "bm UUID already set");
  2978. mdev->ldev->md.uuid[UI_BITMAP] = val;
  2979. mdev->ldev->md.uuid[UI_BITMAP] &= ~((u64)1);
  2980. }
  2981. drbd_md_mark_dirty(mdev);
  2982. }
  2983. /**
  2984. * drbd_bmio_set_n_write() - io_fn for drbd_queue_bitmap_io() or drbd_bitmap_io()
  2985. * @mdev: DRBD device.
  2986. *
  2987. * Sets all bits in the bitmap and writes the whole bitmap to stable storage.
  2988. */
  2989. int drbd_bmio_set_n_write(struct drbd_conf *mdev)
  2990. {
  2991. int rv = -EIO;
  2992. if (get_ldev_if_state(mdev, D_ATTACHING)) {
  2993. drbd_md_set_flag(mdev, MDF_FULL_SYNC);
  2994. drbd_md_sync(mdev);
  2995. drbd_bm_set_all(mdev);
  2996. rv = drbd_bm_write(mdev);
  2997. if (!rv) {
  2998. drbd_md_clear_flag(mdev, MDF_FULL_SYNC);
  2999. drbd_md_sync(mdev);
  3000. }
  3001. put_ldev(mdev);
  3002. }
  3003. return rv;
  3004. }
  3005. /**
  3006. * drbd_bmio_clear_n_write() - io_fn for drbd_queue_bitmap_io() or drbd_bitmap_io()
  3007. * @mdev: DRBD device.
  3008. *
  3009. * Clears all bits in the bitmap and writes the whole bitmap to stable storage.
  3010. */
  3011. int drbd_bmio_clear_n_write(struct drbd_conf *mdev)
  3012. {
  3013. int rv = -EIO;
  3014. if (get_ldev_if_state(mdev, D_ATTACHING)) {
  3015. drbd_bm_clear_all(mdev);
  3016. rv = drbd_bm_write(mdev);
  3017. put_ldev(mdev);
  3018. }
  3019. return rv;
  3020. }
  3021. static int w_bitmap_io(struct drbd_conf *mdev, struct drbd_work *w, int unused)
  3022. {
  3023. struct bm_io_work *work = container_of(w, struct bm_io_work, w);
  3024. int rv;
  3025. D_ASSERT(atomic_read(&mdev->ap_bio_cnt) == 0);
  3026. drbd_bm_lock(mdev, work->why);
  3027. rv = work->io_fn(mdev);
  3028. drbd_bm_unlock(mdev);
  3029. clear_bit(BITMAP_IO, &mdev->flags);
  3030. wake_up(&mdev->misc_wait);
  3031. if (work->done)
  3032. work->done(mdev, rv);
  3033. clear_bit(BITMAP_IO_QUEUED, &mdev->flags);
  3034. work->why = NULL;
  3035. return 1;
  3036. }
  3037. /**
  3038. * drbd_queue_bitmap_io() - Queues an IO operation on the whole bitmap
  3039. * @mdev: DRBD device.
  3040. * @io_fn: IO callback to be called when bitmap IO is possible
  3041. * @done: callback to be called after the bitmap IO was performed
  3042. * @why: Descriptive text of the reason for doing the IO
  3043. *
  3044. * While IO on the bitmap happens we freeze application IO thus we ensure
  3045. * that drbd_set_out_of_sync() can not be called. This function MAY ONLY be
  3046. * called from worker context. It MUST NOT be used while a previous such
  3047. * work is still pending!
  3048. */
  3049. void drbd_queue_bitmap_io(struct drbd_conf *mdev,
  3050. int (*io_fn)(struct drbd_conf *),
  3051. void (*done)(struct drbd_conf *, int),
  3052. char *why)
  3053. {
  3054. D_ASSERT(current == mdev->worker.task);
  3055. D_ASSERT(!test_bit(BITMAP_IO_QUEUED, &mdev->flags));
  3056. D_ASSERT(!test_bit(BITMAP_IO, &mdev->flags));
  3057. D_ASSERT(list_empty(&mdev->bm_io_work.w.list));
  3058. if (mdev->bm_io_work.why)
  3059. dev_err(DEV, "FIXME going to queue '%s' but '%s' still pending?\n",
  3060. why, mdev->bm_io_work.why);
  3061. mdev->bm_io_work.io_fn = io_fn;
  3062. mdev->bm_io_work.done = done;
  3063. mdev->bm_io_work.why = why;
  3064. set_bit(BITMAP_IO, &mdev->flags);
  3065. if (atomic_read(&mdev->ap_bio_cnt) == 0) {
  3066. if (list_empty(&mdev->bm_io_work.w.list)) {
  3067. set_bit(BITMAP_IO_QUEUED, &mdev->flags);
  3068. drbd_queue_work(&mdev->data.work, &mdev->bm_io_work.w);
  3069. } else
  3070. dev_err(DEV, "FIXME avoided double queuing bm_io_work\n");
  3071. }
  3072. }
  3073. /**
  3074. * drbd_bitmap_io() - Does an IO operation on the whole bitmap
  3075. * @mdev: DRBD device.
  3076. * @io_fn: IO callback to be called when bitmap IO is possible
  3077. * @why: Descriptive text of the reason for doing the IO
  3078. *
  3079. * freezes application IO while that the actual IO operations runs. This
  3080. * functions MAY NOT be called from worker context.
  3081. */
  3082. int drbd_bitmap_io(struct drbd_conf *mdev, int (*io_fn)(struct drbd_conf *), char *why)
  3083. {
  3084. int rv;
  3085. D_ASSERT(current != mdev->worker.task);
  3086. drbd_suspend_io(mdev);
  3087. drbd_bm_lock(mdev, why);
  3088. rv = io_fn(mdev);
  3089. drbd_bm_unlock(mdev);
  3090. drbd_resume_io(mdev);
  3091. return rv;
  3092. }
  3093. void drbd_md_set_flag(struct drbd_conf *mdev, int flag) __must_hold(local)
  3094. {
  3095. if ((mdev->ldev->md.flags & flag) != flag) {
  3096. drbd_md_mark_dirty(mdev);
  3097. mdev->ldev->md.flags |= flag;
  3098. }
  3099. }
  3100. void drbd_md_clear_flag(struct drbd_conf *mdev, int flag) __must_hold(local)
  3101. {
  3102. if ((mdev->ldev->md.flags & flag) != 0) {
  3103. drbd_md_mark_dirty(mdev);
  3104. mdev->ldev->md.flags &= ~flag;
  3105. }
  3106. }
  3107. int drbd_md_test_flag(struct drbd_backing_dev *bdev, int flag)
  3108. {
  3109. return (bdev->md.flags & flag) != 0;
  3110. }
  3111. static void md_sync_timer_fn(unsigned long data)
  3112. {
  3113. struct drbd_conf *mdev = (struct drbd_conf *) data;
  3114. drbd_queue_work_front(&mdev->data.work, &mdev->md_sync_work);
  3115. }
  3116. static int w_md_sync(struct drbd_conf *mdev, struct drbd_work *w, int unused)
  3117. {
  3118. dev_warn(DEV, "md_sync_timer expired! Worker calls drbd_md_sync().\n");
  3119. drbd_md_sync(mdev);
  3120. return 1;
  3121. }
  3122. #ifdef CONFIG_DRBD_FAULT_INJECTION
  3123. /* Fault insertion support including random number generator shamelessly
  3124. * stolen from kernel/rcutorture.c */
  3125. struct fault_random_state {
  3126. unsigned long state;
  3127. unsigned long count;
  3128. };
  3129. #define FAULT_RANDOM_MULT 39916801 /* prime */
  3130. #define FAULT_RANDOM_ADD 479001701 /* prime */
  3131. #define FAULT_RANDOM_REFRESH 10000
  3132. /*
  3133. * Crude but fast random-number generator. Uses a linear congruential
  3134. * generator, with occasional help from get_random_bytes().
  3135. */
  3136. static unsigned long
  3137. _drbd_fault_random(struct fault_random_state *rsp)
  3138. {
  3139. long refresh;
  3140. if (!rsp->count--) {
  3141. get_random_bytes(&refresh, sizeof(refresh));
  3142. rsp->state += refresh;
  3143. rsp->count = FAULT_RANDOM_REFRESH;
  3144. }
  3145. rsp->state = rsp->state * FAULT_RANDOM_MULT + FAULT_RANDOM_ADD;
  3146. return swahw32(rsp->state);
  3147. }
  3148. static char *
  3149. _drbd_fault_str(unsigned int type) {
  3150. static char *_faults[] = {
  3151. [DRBD_FAULT_MD_WR] = "Meta-data write",
  3152. [DRBD_FAULT_MD_RD] = "Meta-data read",
  3153. [DRBD_FAULT_RS_WR] = "Resync write",
  3154. [DRBD_FAULT_RS_RD] = "Resync read",
  3155. [DRBD_FAULT_DT_WR] = "Data write",
  3156. [DRBD_FAULT_DT_RD] = "Data read",
  3157. [DRBD_FAULT_DT_RA] = "Data read ahead",
  3158. [DRBD_FAULT_BM_ALLOC] = "BM allocation",
  3159. [DRBD_FAULT_AL_EE] = "EE allocation",
  3160. [DRBD_FAULT_RECEIVE] = "receive data corruption",
  3161. };
  3162. return (type < DRBD_FAULT_MAX) ? _faults[type] : "**Unknown**";
  3163. }
  3164. unsigned int
  3165. _drbd_insert_fault(struct drbd_conf *mdev, unsigned int type)
  3166. {
  3167. static struct fault_random_state rrs = {0, 0};
  3168. unsigned int ret = (
  3169. (fault_devs == 0 ||
  3170. ((1 << mdev_to_minor(mdev)) & fault_devs) != 0) &&
  3171. (((_drbd_fault_random(&rrs) % 100) + 1) <= fault_rate));
  3172. if (ret) {
  3173. fault_count++;
  3174. if (__ratelimit(&drbd_ratelimit_state))
  3175. dev_warn(DEV, "***Simulating %s failure\n",
  3176. _drbd_fault_str(type));
  3177. }
  3178. return ret;
  3179. }
  3180. #endif
  3181. const char *drbd_buildtag(void)
  3182. {
  3183. /* DRBD built from external sources has here a reference to the
  3184. git hash of the source code. */
  3185. static char buildtag[38] = "\0uilt-in";
  3186. if (buildtag[0] == 0) {
  3187. #ifdef CONFIG_MODULES
  3188. if (THIS_MODULE != NULL)
  3189. sprintf(buildtag, "srcversion: %-24s", THIS_MODULE->srcversion);
  3190. else
  3191. #endif
  3192. buildtag[0] = 'b';
  3193. }
  3194. return buildtag;
  3195. }
  3196. module_init(drbd_init)
  3197. module_exit(drbd_cleanup)
  3198. EXPORT_SYMBOL(drbd_conn_str);
  3199. EXPORT_SYMBOL(drbd_role_str);
  3200. EXPORT_SYMBOL(drbd_disk_str);
  3201. EXPORT_SYMBOL(drbd_set_st_err_str);