29 #include <linux/compiler.h>
30 #include <linux/types.h>
31 #include <linux/list.h>
32 #include <linux/sched.h>
33 #include <linux/bitops.h>
34 #include <linux/slab.h>
37 #include <linux/tcp.h>
44 #include <linux/prefetch.h>
47 # define __protected_by(x) __attribute__((require_context(x,1,999,"rdwr")))
48 # define __protected_read_by(x) __attribute__((require_context(x,1,999,"read")))
49 # define __protected_write_by(x) __attribute__((require_context(x,1,999,"write")))
50 # define __must_hold(x) __attribute__((context(x,1,1), require_context(x,1,999,"call")))
52 # define __protected_by(x)
53 # define __protected_read_by(x)
54 # define __protected_write_by(x)
55 # define __must_hold(x)
58 #define __no_warn(lock, stmt) do { __acquire(lock); stmt; __release(lock); } while (0)
64 extern unsigned int cn_idx;
66 #ifdef CONFIG_DRBD_FAULT_INJECTION
67 extern int enable_faults;
68 extern int fault_rate;
69 extern int fault_devs;
80 #define DRBD_SIG SIGXCPU
87 #define DRBD_SIGKILL SIGHUP
94 #define ID_IN_SYNC (4711ULL)
95 #define ID_OUT_OF_SYNC (4712ULL)
97 #define ID_SYNCER (-1ULL)
99 #define is_syncer_block_id(id) ((id) == ID_SYNCER)
100 #define UUID_NEW_BM_OFFSET ((u64)0x0001000000000000ULL)
106 #define DEV (disk_to_dev(mdev->vdisk))
108 #define D_ASSERT(exp) if (!(exp)) \
109 dev_err(DEV, "ASSERT( " #exp " ) in %s:%d\n", __FILE__, __LINE__)
111 #define ERR_IF(exp) if (({ \
112 int _b = (exp) != 0; \
113 if (_b) dev_err(DEV, "ASSERT FAILED: %s: (%s) in %s:%d\n", \
114 __func__, #exp, __FILE__, __LINE__); \
138 drbd_insert_fault(
struct drbd_conf *mdev,
unsigned int type) {
139 #ifdef CONFIG_DRBD_FAULT_INJECTION
141 (enable_faults & (1<<
type)) &&
149 #define div_ceil(A, B) ((A)/(B) + ((A)%(B) ? 1 : 0))
151 #define div_floor(A, B) ((A)/(B))
155 #define DRBD_MD_MAGIC (DRBD_MAGIC+4)
279 return cmdnames[
cmd];
303 static inline void bm_xfer_ctx_bit_to_word_offset(
struct bm_xfer_ctx *
c)
312 #if BITS_PER_LONG == 64
314 #elif BITS_PER_LONG == 32
318 # error "unsupported BITS_PER_LONG"
323 #define __packed __attribute__((packed))
373 #define DP_HARDBARRIER 1
375 #define DP_MAY_SET_IN_SYNC 4
379 #define DP_DISCARD 64
627 #define BM_PACKET_PAYLOAD_BYTES (4096 - sizeof(struct p_header80))
628 #define BM_PACKET_WORDS (BM_PACKET_PAYLOAD_BYTES/sizeof(long))
629 #define BM_PACKET_VLI_BYTES_MAX (4096 - sizeof(struct p_compressed_bm))
630 #if (PAGE_SIZE < 4096)
632 #error "PAGE_SIZE too small"
804 #define EE_CALL_AL_COMPLETE_IO (1<<__EE_CALL_AL_COMPLETE_IO)
805 #define EE_MAY_SET_IN_SYNC (1<<__EE_MAY_SET_IN_SYNC)
806 #define EE_RESUBMITTED (1<<__EE_RESUBMITTED)
807 #define EE_WAS_ERROR (1<<__EE_WAS_ERROR)
808 #define EE_HAS_DIGEST (1<<__EE_HAS_DIGEST)
930 #define NL_PACKET(name, number, fields) struct name { fields };
931 #define NL_INTEGER(pn,pr,member) int member;
932 #define NL_INT64(pn,pr,member) __u64 member;
933 #define NL_BIT(pn,pr,member) unsigned member:1;
934 #define NL_STRING(pn,pr,member,len) unsigned char member[len]; int member ## _len;
998 #ifdef DRBD_DEBUG_MD_SYNC
1002 } last_md_mark_dirty;
1043 #define DRBD_SYNC_MARKS 8
1044 #define DRBD_SYNC_MARK_STEP (3*HZ)
1143 static inline struct drbd_conf *minor_to_mdev(
unsigned int minor)
1152 static inline unsigned int mdev_to_minor(
struct drbd_conf *mdev)
1165 static inline int drbd_get_data_sock(
struct drbd_conf *mdev)
1177 static inline void drbd_put_data_sock(
struct drbd_conf *mdev)
1223 #define drbd_thread_current_set_cpu(A) ({})
1224 #define drbd_calc_cpu_mask(A) ({})
1228 unsigned int set_size);
1233 void *
buf,
size_t size,
unsigned msg_flags);
1243 size_t size,
unsigned msg_flags);
1244 #define USE_DATA_SOCKET 1
1245 #define USE_META_SOCKET 0
1270 void *
digest,
int digest_size,
1291 #ifndef DRBD_DEBUG_MD_SYNC
1294 #define drbd_md_mark_dirty(m) drbd_md_mark_dirty_(m, __LINE__ , __func__ )
1295 extern void drbd_md_mark_dirty_(
struct drbd_conf *mdev,
1296 unsigned int line,
const char *
func);
1316 #define MD_RESERVED_SECT (128LU << 11)
1318 #define MD_AL_OFFSET 8
1319 #define MD_AL_MAX_SIZE 64
1321 #define MD_BM_OFFSET (MD_AL_OFFSET + MD_AL_MAX_SIZE)
1324 #define MD_SECTOR_SHIFT 9
1325 #define MD_SECTOR_SIZE (1<<MD_SECTOR_SHIFT)
1328 #define AL_EXTENTS_PT ((MD_SECTOR_SIZE-12)/8-1)
1329 #define AL_EXTENT_SHIFT 22
1330 #define AL_EXTENT_SIZE (1<<AL_EXTENT_SHIFT)
1332 #if BITS_PER_LONG == 32
1334 #define cpu_to_lel(A) cpu_to_le32(A)
1335 #define lel_to_cpu(A) le32_to_cpu(A)
1336 #elif BITS_PER_LONG == 64
1338 #define cpu_to_lel(A) cpu_to_le64(A)
1339 #define lel_to_cpu(A) le64_to_cpu(A)
1341 #error "LN2 of BITS_PER_LONG unknown!"
1353 #define BME_NO_WRITES 0
1354 #define BME_LOCKED 1
1355 #define BME_PRIORITY 2
1365 #define SLEEP_TIME (HZ/10)
1367 #define BM_BLOCK_SHIFT 12
1368 #define BM_BLOCK_SIZE (1<<BM_BLOCK_SHIFT)
1371 #define BM_EXT_SHIFT (BM_BLOCK_SHIFT + MD_SECTOR_SHIFT + 3)
1372 #define BM_EXT_SIZE (1<<BM_EXT_SHIFT)
1374 #if (BM_EXT_SHIFT != 24) || (BM_BLOCK_SHIFT != 12)
1375 #error "HAVE YOU FIXED drbdmeta AS WELL??"
1379 #define BM_SECT_TO_BIT(x) ((x)>>(BM_BLOCK_SHIFT-9))
1380 #define BM_BIT_TO_SECT(x) ((sector_t)(x)<<(BM_BLOCK_SHIFT-9))
1381 #define BM_SECT_PER_BIT BM_BIT_TO_SECT(1)
1384 #define Bit2KB(bits) ((bits)<<(BM_BLOCK_SHIFT-10))
1388 #define BM_SECT_TO_EXT(x) ((x)>>(BM_EXT_SHIFT-9))
1391 #define BM_EXT_TO_SECT(x) ((sector_t)(x) << (BM_EXT_SHIFT-9))
1392 #define BM_SECT_PER_EXT BM_EXT_TO_SECT(1)
1395 #define AL_EXT_PER_BM_SECT (1 << (BM_EXT_SHIFT - AL_EXTENT_SHIFT))
1396 #define BM_WORDS_PER_AL_EXT (1 << (AL_EXTENT_SHIFT-BM_BLOCK_SHIFT-LN2_BPL))
1398 #define BM_BLOCKS_PER_BM_EXT_B (BM_EXT_SHIFT - BM_BLOCK_SHIFT)
1399 #define BM_BLOCKS_PER_BM_EXT_MASK ((1<<BM_BLOCKS_PER_BM_EXT_B) - 1)
1415 #define DRBD_MAX_SECTORS_32 (0xffffffffLU)
1416 #define DRBD_MAX_SECTORS_BM \
1417 ((MD_RESERVED_SECT - MD_BM_OFFSET) * (1LL<<(BM_EXT_SHIFT-9)))
1418 #if DRBD_MAX_SECTORS_BM < DRBD_MAX_SECTORS_32
1419 #define DRBD_MAX_SECTORS DRBD_MAX_SECTORS_BM
1420 #define DRBD_MAX_SECTORS_FLEX DRBD_MAX_SECTORS_BM
1421 #elif !defined(CONFIG_LBDAF) && BITS_PER_LONG == 32
1422 #define DRBD_MAX_SECTORS DRBD_MAX_SECTORS_32
1423 #define DRBD_MAX_SECTORS_FLEX DRBD_MAX_SECTORS_32
1425 #define DRBD_MAX_SECTORS DRBD_MAX_SECTORS_BM
1427 #if BITS_PER_LONG == 32
1431 #define DRBD_MAX_SECTORS_FLEX BM_BIT_TO_SECT(0xffff7fff)
1434 #define DRBD_MAX_SECTORS_FLEX (1UL << 51)
1443 #define DRBD_MAX_BIO_SIZE (1U<<(9+HT_SHIFT))
1444 #define DRBD_MAX_BIO_SIZE_SAFE (1U << 12)
1446 #define DRBD_MAX_SIZE_H80_PACKET (1U << 15)
1449 #define APP_R_HSIZE 15
1458 struct drbd_conf *mdev,
unsigned long s,
unsigned long e);
1460 struct drbd_conf *mdev,
unsigned long s,
unsigned long e);
1462 struct drbd_conf *mdev,
const unsigned long s,
const unsigned long e);
1466 const unsigned long s,
const unsigned long e);
1475 unsigned long al_enr);
1480 #define DRBD_END_OF_BITMAP (~(unsigned long)0)
1490 size_t number,
unsigned long *
buffer);
1493 size_t number,
unsigned long *
buffer);
1529 #define DRBD_MIN_POOL_PAGES 128
1581 unsigned int *
done);
1585 static
inline void ov_oos_print(
struct drbd_conf *mdev)
1587 if (mdev->ov_last_oos_size) {
1588 dev_err(
DEV,
"Out of sync: start=%llu, size=%lu (sectors)\n",
1589 (
unsigned long long)mdev->ov_last_oos_start,
1590 (
unsigned long)mdev->ov_last_oos_size);
1592 mdev->ov_last_oos_size=0;
1625 const unsigned rw,
const int fault_type);
1634 #define drbd_free_ee(m,e) drbd_free_some_ee(m, e, 0)
1635 #define drbd_free_net_ee(m,e) drbd_free_some_ee(m, e, 1)
1647 static inline int drbd_setsockopt(
struct socket *
sock,
int level,
int optname,
1648 char __user *optval,
int optlen)
1654 err = sock->
ops->setsockopt(sock, level, optname, optval,
1659 static inline void drbd_tcp_cork(
struct socket *sock)
1663 (
char __user *)&
val,
sizeof(
val));
1666 static inline void drbd_tcp_uncork(
struct socket *sock)
1670 (
char __user *)&
val,
sizeof(
val));
1673 static inline void drbd_tcp_nodelay(
struct socket *sock)
1677 (
char __user *)&
val,
sizeof(
val));
1680 static inline void drbd_tcp_quickack(
struct socket *sock)
1684 (
char __user *)&
val,
sizeof(
val));
1704 sector_t sector,
int size);
1709 #define drbd_set_in_sync(mdev, sector, size) \
1710 __drbd_set_in_sync(mdev, sector, size, __FILE__, __LINE__)
1713 #define drbd_set_out_of_sync(mdev, sector, size) \
1714 __drbd_set_out_of_sync(mdev, sector, size, __FILE__, __LINE__)
1726 const char *
reason,
const int dgs,
1727 const char* seen_hash,
const char* calc_hash,
1749 #define role_MASK R_MASK
1750 #define peer_MASK R_MASK
1751 #define disk_MASK D_MASK
1752 #define pdsk_MASK D_MASK
1753 #define conn_MASK C_MASK
1755 #define user_isp_MASK 1
1756 #define aftr_isp_MASK 1
1757 #define susp_nod_MASK 1
1758 #define susp_fen_MASK 1
1761 ({ union drbd_state mask; mask.i = 0; mask.T = T##_MASK; mask; }), \
1762 ({ union drbd_state val; val.i = 0; val.T = (S); val; })
1763 #define NS2(T1, S1, T2, S2) \
1764 ({ union drbd_state mask; mask.i = 0; mask.T1 = T1##_MASK; \
1765 mask.T2 = T2##_MASK; mask; }), \
1766 ({ union drbd_state val; val.i = 0; val.T1 = (S1); \
1767 val.T2 = (S2); val; })
1768 #define NS3(T1, S1, T2, S2, T3, S3) \
1769 ({ union drbd_state mask; mask.i = 0; mask.T1 = T1##_MASK; \
1770 mask.T2 = T2##_MASK; mask.T3 = T3##_MASK; mask; }), \
1771 ({ union drbd_state val; val.i = 0; val.T1 = (S1); \
1772 val.T2 = (S2); val.T3 = (S3); val; })
1774 #define _NS(D, T, S) \
1775 D, ({ union drbd_state __ns; __ns.i = D->state.i; __ns.T = (S); __ns; })
1776 #define _NS2(D, T1, S1, T2, S2) \
1777 D, ({ union drbd_state __ns; __ns.i = D->state.i; __ns.T1 = (S1); \
1778 __ns.T2 = (S2); __ns; })
1779 #define _NS3(D, T1, S1, T2, S2, T3, S3) \
1780 D, ({ union drbd_state __ns; __ns.i = D->state.i; __ns.T1 = (S1); \
1781 __ns.T2 = (S2); __ns.T3 = (S3); __ns; })
1788 static inline struct page *page_chain_next(
struct page *
page)
1790 return (
struct page *)page_private(page);
1792 #define page_chain_for_each(page) \
1793 for (; page && ({ prefetch(page_chain_next(page)); 1; }); \
1794 page = page_chain_next(page))
1795 #define page_chain_for_each_safe(page, n) \
1796 for (; page && ({ n = page_chain_next(page); 1; }); page = n)
1802 if (page_count(page) > 1)
1808 static inline void drbd_state_lock(
struct drbd_conf *mdev)
1814 static inline void drbd_state_unlock(
struct drbd_conf *mdev)
1821 _drbd_set_state(
struct drbd_conf *mdev,
union drbd_state ns,
1843 static inline int drbd_request_state(
struct drbd_conf *mdev,
1856 #define __drbd_chk_io_error(m,f) __drbd_chk_io_error_(m,f, __func__)
1857 static inline void __drbd_chk_io_error_(
struct drbd_conf *mdev,
1861 switch (mdev->ldev->dc.on_io_error) {
1865 dev_err(
DEV,
"Local IO failed in %s.\n", where);
1879 "Local IO failed in %s. Detaching...\n", where);
1893 #define drbd_chk_io_error(m,e,f) drbd_chk_io_error_(m,e,f, __func__)
1894 static inline void drbd_chk_io_error_(
struct drbd_conf *mdev,
1898 unsigned long flags;
1900 __drbd_chk_io_error_(mdev, forcedetach, where);
1901 spin_unlock_irqrestore(&mdev->
req_lock, flags);
1915 switch (bdev->
dc.meta_dev_idx) {
1918 return bdev->
md.md_offset + bdev->
md.bm_offset;
1921 return bdev->
md.md_offset;
1931 switch (bdev->
dc.meta_dev_idx) {
1937 return bdev->
md.md_offset + bdev->
md.md_size_sect;
1942 static inline sector_t drbd_get_capacity(
struct block_device *bdev)
1945 return bdev ? i_size_read(bdev->
bd_inode) >> 9 : 0;
1956 static inline sector_t drbd_get_max_capacity(
struct drbd_backing_dev *bdev)
1959 switch (bdev->
dc.meta_dev_idx) {
1964 drbd_md_first_sector(bdev))
1971 s =
min_t(sector_t, s,
1973 - bdev->
md.bm_offset));
1987 static inline sector_t drbd_md_ss__(
struct drbd_conf *mdev,
1990 switch (bdev->
dc.meta_dev_idx) {
2015 unsigned long flags;
2017 list_add(&w->
list, &q->
q);
2020 spin_unlock_irqrestore(&q->
q_lock, flags);
2026 unsigned long flags;
2031 spin_unlock_irqrestore(&q->
q_lock, flags);
2034 static inline void wake_asender(
struct drbd_conf *mdev)
2040 static inline void request_ping(
struct drbd_conf *mdev)
2046 static inline int drbd_send_short_cmd(
struct drbd_conf *mdev,
2053 static inline int drbd_send_ping(
struct drbd_conf *mdev)
2059 static inline int drbd_send_ping_ack(
struct drbd_conf *mdev)
2065 static inline void drbd_thread_stop(
struct drbd_thread *thi)
2070 static inline void drbd_thread_stop_nowait(
struct drbd_thread *thi)
2075 static inline void drbd_thread_restart_nowait(
struct drbd_thread *thi)
2102 static inline void inc_ap_pending(
struct drbd_conf *mdev)
2107 #define ERR_IF_CNT_IS_NEGATIVE(which) \
2108 if (atomic_read(&mdev->which) < 0) \
2109 dev_err(DEV, "in %s:%d: " #which " = %d < 0 !\n", \
2110 __func__ , __LINE__ , \
2111 atomic_read(&mdev->which))
2113 #define dec_ap_pending(mdev) do { \
2114 typecheck(struct drbd_conf *, mdev); \
2115 if (atomic_dec_and_test(&mdev->ap_pending_cnt)) \
2116 wake_up(&mdev->misc_wait); \
2117 ERR_IF_CNT_IS_NEGATIVE(ap_pending_cnt); } while (0)
2125 static inline void inc_rs_pending(
struct drbd_conf *mdev)
2130 #define dec_rs_pending(mdev) do { \
2131 typecheck(struct drbd_conf *, mdev); \
2132 atomic_dec(&mdev->rs_pending_cnt); \
2133 ERR_IF_CNT_IS_NEGATIVE(rs_pending_cnt); } while (0)
2144 static inline void inc_unacked(
struct drbd_conf *mdev)
2149 #define dec_unacked(mdev) do { \
2150 typecheck(struct drbd_conf *, mdev); \
2151 atomic_dec(&mdev->unacked_cnt); \
2152 ERR_IF_CNT_IS_NEGATIVE(unacked_cnt); } while (0)
2154 #define sub_unacked(mdev, n) do { \
2155 typecheck(struct drbd_conf *, mdev); \
2156 atomic_sub(n, &mdev->unacked_cnt); \
2157 ERR_IF_CNT_IS_NEGATIVE(unacked_cnt); } while (0)
2160 static inline void put_net_conf(
struct drbd_conf *mdev)
2172 static inline int get_net_conf(
struct drbd_conf *mdev)
2180 return have_net_conf;
2189 #define get_ldev(M) __cond_lock(local, _get_ldev_if_state(M,D_INCONSISTENT))
2190 #define get_ldev_if_state(M,MINS) __cond_lock(local, _get_ldev_if_state(M,MINS))
2192 static inline void put_ldev(
struct drbd_conf *mdev)
2213 static inline int _get_ldev_if_state(
struct drbd_conf *mdev,
enum drbd_disk_state mins)
2222 io_allowed = (mdev->
state.
disk >= mins);
2228 extern int _get_ldev_if_state(
struct drbd_conf *mdev,
enum drbd_disk_state mins);
2232 static inline void drbd_get_syncer_progress(
struct drbd_conf *mdev,
2233 unsigned long *bits_left,
unsigned int *per_mil_done)
2256 dev_warn(
DEV,
"cs:%s rs_left=%lu > rs_total=%lu (rs_failed %lu)\n",
2271 unsigned long left = *bits_left >> shift;
2272 unsigned long total = 1
UL + (mdev->
rs_total >> shift);
2273 unsigned long tmp = 1000
UL - left * 1000
UL/total;
2274 *per_mil_done =
tmp;
2282 static inline int drbd_get_max_buffers(
struct drbd_conf *mdev)
2285 if (get_net_conf(mdev)) {
2292 static inline int drbd_state_is_stable(
struct drbd_conf *mdev)
2362 static inline int is_susp(
union drbd_state s)
2367 static inline bool may_inc_ap_bio(
struct drbd_conf *mdev)
2369 int mxb = drbd_get_max_buffers(mdev);
2371 if (is_susp(mdev->
state))
2381 if (!drbd_state_is_stable(mdev))
2393 static inline bool inc_ap_bio_cond(
struct drbd_conf *mdev,
int count)
2398 rv = may_inc_ap_bio(mdev);
2406 static inline void inc_ap_bio(
struct drbd_conf *mdev,
int count)
2419 static inline void dec_ap_bio(
struct drbd_conf *mdev)
2421 int mxb = drbd_get_max_buffers(mdev);
2438 static inline int drbd_set_ed_uuid(
struct drbd_conf *mdev,
u64 val)
2445 static inline int seq_cmp(
u32 a,
u32 b)
2454 #define seq_lt(a, b) (seq_cmp((a), (b)) < 0)
2455 #define seq_gt(a, b) (seq_cmp((a), (b)) > 0)
2456 #define seq_ge(a, b) (seq_cmp((a), (b)) >= 0)
2457 #define seq_le(a, b) (seq_cmp((a), (b)) <= 0)
2459 #define seq_max(a, b) ((u32)(seq_gt((a), (b)) ? (a) : (b)))
2461 static inline void update_peer_seq(
struct drbd_conf *mdev,
unsigned int new_seq)
2472 static inline void drbd_update_congested(
struct drbd_conf *mdev)
2474 struct sock *
sk = mdev->
data.socket->sk;
2479 static inline int drbd_queue_order_type(
struct drbd_conf *mdev)
2483 #ifndef QUEUE_ORDERED_NONE
2484 #define QUEUE_ORDERED_NONE 0
2489 static inline void drbd_md_flush(
struct drbd_conf *mdev)
2499 dev_err(
DEV,
"meta data flush failed with status %d, disabling md-flushes\n", r);