Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | drbd_nl.c |
| 3 | |
| 4 | This file is part of DRBD by Philipp Reisner and Lars Ellenberg. |
| 5 | |
| 6 | Copyright (C) 2001-2008, LINBIT Information Technologies GmbH. |
| 7 | Copyright (C) 1999-2008, Philipp Reisner <philipp.reisner@linbit.com>. |
| 8 | Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@linbit.com>. |
| 9 | |
| 10 | drbd is free software; you can redistribute it and/or modify |
| 11 | it under the terms of the GNU General Public License as published by |
| 12 | the Free Software Foundation; either version 2, or (at your option) |
| 13 | any later version. |
| 14 | |
| 15 | drbd is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with drbd; see the file COPYING. If not, write to |
| 22 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
| 23 | |
| 24 | */ |
| 25 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 26 | #include <linux/module.h> |
| 27 | #include <linux/drbd.h> |
| 28 | #include <linux/in.h> |
| 29 | #include <linux/fs.h> |
| 30 | #include <linux/file.h> |
| 31 | #include <linux/slab.h> |
| 32 | #include <linux/connector.h> |
| 33 | #include <linux/blkpg.h> |
| 34 | #include <linux/cpumask.h> |
| 35 | #include "drbd_int.h" |
Philipp Reisner | 265be2d | 2010-05-31 10:14:17 +0200 | [diff] [blame] | 36 | #include "drbd_req.h" |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 37 | #include "drbd_wrappers.h" |
| 38 | #include <asm/unaligned.h> |
| 39 | #include <linux/drbd_tag_magic.h> |
| 40 | #include <linux/drbd_limits.h> |
Philipp Reisner | 87f7be4 | 2010-06-11 13:56:33 +0200 | [diff] [blame] | 41 | #include <linux/compiler.h> |
| 42 | #include <linux/kthread.h> |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 43 | |
| 44 | static unsigned short *tl_add_blob(unsigned short *, enum drbd_tags, const void *, int); |
| 45 | static unsigned short *tl_add_str(unsigned short *, enum drbd_tags, const char *); |
| 46 | static unsigned short *tl_add_int(unsigned short *, enum drbd_tags, const void *); |
| 47 | |
| 48 | /* see get_sb_bdev and bd_claim */ |
| 49 | static char *drbd_m_holder = "Hands off! this is DRBD's meta data device."; |
| 50 | |
| 51 | /* Generate the tag_list to struct functions */ |
| 52 | #define NL_PACKET(name, number, fields) \ |
| 53 | static int name ## _from_tags(struct drbd_conf *mdev, \ |
| 54 | unsigned short *tags, struct name *arg) __attribute__ ((unused)); \ |
| 55 | static int name ## _from_tags(struct drbd_conf *mdev, \ |
| 56 | unsigned short *tags, struct name *arg) \ |
| 57 | { \ |
| 58 | int tag; \ |
| 59 | int dlen; \ |
| 60 | \ |
| 61 | while ((tag = get_unaligned(tags++)) != TT_END) { \ |
| 62 | dlen = get_unaligned(tags++); \ |
| 63 | switch (tag_number(tag)) { \ |
| 64 | fields \ |
| 65 | default: \ |
| 66 | if (tag & T_MANDATORY) { \ |
| 67 | dev_err(DEV, "Unknown tag: %d\n", tag_number(tag)); \ |
| 68 | return 0; \ |
| 69 | } \ |
| 70 | } \ |
| 71 | tags = (unsigned short *)((char *)tags + dlen); \ |
| 72 | } \ |
| 73 | return 1; \ |
| 74 | } |
| 75 | #define NL_INTEGER(pn, pr, member) \ |
| 76 | case pn: /* D_ASSERT( tag_type(tag) == TT_INTEGER ); */ \ |
| 77 | arg->member = get_unaligned((int *)(tags)); \ |
| 78 | break; |
| 79 | #define NL_INT64(pn, pr, member) \ |
| 80 | case pn: /* D_ASSERT( tag_type(tag) == TT_INT64 ); */ \ |
| 81 | arg->member = get_unaligned((u64 *)(tags)); \ |
| 82 | break; |
| 83 | #define NL_BIT(pn, pr, member) \ |
| 84 | case pn: /* D_ASSERT( tag_type(tag) == TT_BIT ); */ \ |
| 85 | arg->member = *(char *)(tags) ? 1 : 0; \ |
| 86 | break; |
| 87 | #define NL_STRING(pn, pr, member, len) \ |
| 88 | case pn: /* D_ASSERT( tag_type(tag) == TT_STRING ); */ \ |
| 89 | if (dlen > len) { \ |
| 90 | dev_err(DEV, "arg too long: %s (%u wanted, max len: %u bytes)\n", \ |
| 91 | #member, dlen, (unsigned int)len); \ |
| 92 | return 0; \ |
| 93 | } \ |
| 94 | arg->member ## _len = dlen; \ |
| 95 | memcpy(arg->member, tags, min_t(size_t, dlen, len)); \ |
| 96 | break; |
| 97 | #include "linux/drbd_nl.h" |
| 98 | |
| 99 | /* Generate the struct to tag_list functions */ |
| 100 | #define NL_PACKET(name, number, fields) \ |
| 101 | static unsigned short* \ |
| 102 | name ## _to_tags(struct drbd_conf *mdev, \ |
| 103 | struct name *arg, unsigned short *tags) __attribute__ ((unused)); \ |
| 104 | static unsigned short* \ |
| 105 | name ## _to_tags(struct drbd_conf *mdev, \ |
| 106 | struct name *arg, unsigned short *tags) \ |
| 107 | { \ |
| 108 | fields \ |
| 109 | return tags; \ |
| 110 | } |
| 111 | |
| 112 | #define NL_INTEGER(pn, pr, member) \ |
| 113 | put_unaligned(pn | pr | TT_INTEGER, tags++); \ |
| 114 | put_unaligned(sizeof(int), tags++); \ |
| 115 | put_unaligned(arg->member, (int *)tags); \ |
| 116 | tags = (unsigned short *)((char *)tags+sizeof(int)); |
| 117 | #define NL_INT64(pn, pr, member) \ |
| 118 | put_unaligned(pn | pr | TT_INT64, tags++); \ |
| 119 | put_unaligned(sizeof(u64), tags++); \ |
| 120 | put_unaligned(arg->member, (u64 *)tags); \ |
| 121 | tags = (unsigned short *)((char *)tags+sizeof(u64)); |
| 122 | #define NL_BIT(pn, pr, member) \ |
| 123 | put_unaligned(pn | pr | TT_BIT, tags++); \ |
| 124 | put_unaligned(sizeof(char), tags++); \ |
| 125 | *(char *)tags = arg->member; \ |
| 126 | tags = (unsigned short *)((char *)tags+sizeof(char)); |
| 127 | #define NL_STRING(pn, pr, member, len) \ |
| 128 | put_unaligned(pn | pr | TT_STRING, tags++); \ |
| 129 | put_unaligned(arg->member ## _len, tags++); \ |
| 130 | memcpy(tags, arg->member, arg->member ## _len); \ |
| 131 | tags = (unsigned short *)((char *)tags + arg->member ## _len); |
| 132 | #include "linux/drbd_nl.h" |
| 133 | |
| 134 | void drbd_bcast_ev_helper(struct drbd_conf *mdev, char *helper_name); |
| 135 | void drbd_nl_send_reply(struct cn_msg *, int); |
| 136 | |
| 137 | int drbd_khelper(struct drbd_conf *mdev, char *cmd) |
| 138 | { |
| 139 | char *envp[] = { "HOME=/", |
| 140 | "TERM=linux", |
| 141 | "PATH=/sbin:/usr/sbin:/bin:/usr/bin", |
| 142 | NULL, /* Will be set to address family */ |
| 143 | NULL, /* Will be set to address */ |
| 144 | NULL }; |
| 145 | |
| 146 | char mb[12], af[20], ad[60], *afs; |
| 147 | char *argv[] = {usermode_helper, cmd, mb, NULL }; |
| 148 | int ret; |
| 149 | |
| 150 | snprintf(mb, 12, "minor-%d", mdev_to_minor(mdev)); |
| 151 | |
| 152 | if (get_net_conf(mdev)) { |
| 153 | switch (((struct sockaddr *)mdev->net_conf->peer_addr)->sa_family) { |
| 154 | case AF_INET6: |
| 155 | afs = "ipv6"; |
| 156 | snprintf(ad, 60, "DRBD_PEER_ADDRESS=%pI6", |
| 157 | &((struct sockaddr_in6 *)mdev->net_conf->peer_addr)->sin6_addr); |
| 158 | break; |
| 159 | case AF_INET: |
| 160 | afs = "ipv4"; |
| 161 | snprintf(ad, 60, "DRBD_PEER_ADDRESS=%pI4", |
| 162 | &((struct sockaddr_in *)mdev->net_conf->peer_addr)->sin_addr); |
| 163 | break; |
| 164 | default: |
| 165 | afs = "ssocks"; |
| 166 | snprintf(ad, 60, "DRBD_PEER_ADDRESS=%pI4", |
| 167 | &((struct sockaddr_in *)mdev->net_conf->peer_addr)->sin_addr); |
| 168 | } |
| 169 | snprintf(af, 20, "DRBD_PEER_AF=%s", afs); |
| 170 | envp[3]=af; |
| 171 | envp[4]=ad; |
| 172 | put_net_conf(mdev); |
| 173 | } |
| 174 | |
Lars Ellenberg | 1090c05 | 2010-07-19 17:41:04 +0200 | [diff] [blame] | 175 | /* The helper may take some time. |
| 176 | * write out any unsynced meta data changes now */ |
| 177 | drbd_md_sync(mdev); |
| 178 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 179 | dev_info(DEV, "helper command: %s %s %s\n", usermode_helper, cmd, mb); |
| 180 | |
| 181 | drbd_bcast_ev_helper(mdev, cmd); |
| 182 | ret = call_usermodehelper(usermode_helper, argv, envp, 1); |
| 183 | if (ret) |
| 184 | dev_warn(DEV, "helper command: %s %s %s exit code %u (0x%x)\n", |
| 185 | usermode_helper, cmd, mb, |
| 186 | (ret >> 8) & 0xff, ret); |
| 187 | else |
| 188 | dev_info(DEV, "helper command: %s %s %s exit code %u (0x%x)\n", |
| 189 | usermode_helper, cmd, mb, |
| 190 | (ret >> 8) & 0xff, ret); |
| 191 | |
| 192 | if (ret < 0) /* Ignore any ERRNOs we got. */ |
| 193 | ret = 0; |
| 194 | |
| 195 | return ret; |
| 196 | } |
| 197 | |
| 198 | enum drbd_disk_state drbd_try_outdate_peer(struct drbd_conf *mdev) |
| 199 | { |
| 200 | char *ex_to_string; |
| 201 | int r; |
| 202 | enum drbd_disk_state nps; |
| 203 | enum drbd_fencing_p fp; |
| 204 | |
| 205 | D_ASSERT(mdev->state.pdsk == D_UNKNOWN); |
| 206 | |
| 207 | if (get_ldev_if_state(mdev, D_CONSISTENT)) { |
| 208 | fp = mdev->ldev->dc.fencing; |
| 209 | put_ldev(mdev); |
| 210 | } else { |
| 211 | dev_warn(DEV, "Not fencing peer, I'm not even Consistent myself.\n"); |
Philipp Reisner | fb22c40 | 2010-09-08 23:20:21 +0200 | [diff] [blame] | 212 | nps = mdev->state.pdsk; |
| 213 | goto out; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 216 | r = drbd_khelper(mdev, "fence-peer"); |
| 217 | |
| 218 | switch ((r>>8) & 0xff) { |
| 219 | case 3: /* peer is inconsistent */ |
| 220 | ex_to_string = "peer is inconsistent or worse"; |
| 221 | nps = D_INCONSISTENT; |
| 222 | break; |
| 223 | case 4: /* peer got outdated, or was already outdated */ |
| 224 | ex_to_string = "peer was fenced"; |
| 225 | nps = D_OUTDATED; |
| 226 | break; |
| 227 | case 5: /* peer was down */ |
| 228 | if (mdev->state.disk == D_UP_TO_DATE) { |
| 229 | /* we will(have) create(d) a new UUID anyways... */ |
| 230 | ex_to_string = "peer is unreachable, assumed to be dead"; |
| 231 | nps = D_OUTDATED; |
| 232 | } else { |
| 233 | ex_to_string = "peer unreachable, doing nothing since disk != UpToDate"; |
| 234 | nps = mdev->state.pdsk; |
| 235 | } |
| 236 | break; |
| 237 | case 6: /* Peer is primary, voluntarily outdate myself. |
| 238 | * This is useful when an unconnected R_SECONDARY is asked to |
| 239 | * become R_PRIMARY, but finds the other peer being active. */ |
| 240 | ex_to_string = "peer is active"; |
| 241 | dev_warn(DEV, "Peer is primary, outdating myself.\n"); |
| 242 | nps = D_UNKNOWN; |
| 243 | _drbd_request_state(mdev, NS(disk, D_OUTDATED), CS_WAIT_COMPLETE); |
| 244 | break; |
| 245 | case 7: |
| 246 | if (fp != FP_STONITH) |
| 247 | dev_err(DEV, "fence-peer() = 7 && fencing != Stonith !!!\n"); |
| 248 | ex_to_string = "peer was stonithed"; |
| 249 | nps = D_OUTDATED; |
| 250 | break; |
| 251 | default: |
| 252 | /* The script is broken ... */ |
| 253 | nps = D_UNKNOWN; |
| 254 | dev_err(DEV, "fence-peer helper broken, returned %d\n", (r>>8)&0xff); |
| 255 | return nps; |
| 256 | } |
| 257 | |
| 258 | dev_info(DEV, "fence-peer helper returned %d (%s)\n", |
| 259 | (r>>8) & 0xff, ex_to_string); |
Philipp Reisner | fb22c40 | 2010-09-08 23:20:21 +0200 | [diff] [blame] | 260 | |
| 261 | out: |
| 262 | if (mdev->state.susp_fen && nps >= D_UNKNOWN) { |
| 263 | /* The handler was not successful... unfreeze here, the |
| 264 | state engine can not unfreeze... */ |
| 265 | _drbd_request_state(mdev, NS(susp_fen, 0), CS_VERBOSE); |
| 266 | } |
| 267 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 268 | return nps; |
| 269 | } |
| 270 | |
Philipp Reisner | 87f7be4 | 2010-06-11 13:56:33 +0200 | [diff] [blame] | 271 | static int _try_outdate_peer_async(void *data) |
| 272 | { |
| 273 | struct drbd_conf *mdev = (struct drbd_conf *)data; |
| 274 | enum drbd_disk_state nps; |
| 275 | |
| 276 | nps = drbd_try_outdate_peer(mdev); |
| 277 | drbd_request_state(mdev, NS(pdsk, nps)); |
| 278 | |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | void drbd_try_outdate_peer_async(struct drbd_conf *mdev) |
| 283 | { |
| 284 | struct task_struct *opa; |
| 285 | |
| 286 | opa = kthread_run(_try_outdate_peer_async, mdev, "drbd%d_a_helper", mdev_to_minor(mdev)); |
| 287 | if (IS_ERR(opa)) |
| 288 | dev_err(DEV, "out of mem, failed to invoke fence-peer helper\n"); |
| 289 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 290 | |
Andreas Gruenbacher | bf885f8 | 2010-12-08 00:39:32 +0100 | [diff] [blame] | 291 | enum drbd_state_rv |
| 292 | drbd_set_role(struct drbd_conf *mdev, enum drbd_role new_role, int force) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 293 | { |
| 294 | const int max_tries = 4; |
Andreas Gruenbacher | bf885f8 | 2010-12-08 00:39:32 +0100 | [diff] [blame] | 295 | enum drbd_state_rv rv = SS_UNKNOWN_ERROR; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 296 | int try = 0; |
| 297 | int forced = 0; |
| 298 | union drbd_state mask, val; |
| 299 | enum drbd_disk_state nps; |
| 300 | |
| 301 | if (new_role == R_PRIMARY) |
| 302 | request_ping(mdev); /* Detect a dead peer ASAP */ |
| 303 | |
| 304 | mutex_lock(&mdev->state_mutex); |
| 305 | |
| 306 | mask.i = 0; mask.role = R_MASK; |
| 307 | val.i = 0; val.role = new_role; |
| 308 | |
| 309 | while (try++ < max_tries) { |
Andreas Gruenbacher | bf885f8 | 2010-12-08 00:39:32 +0100 | [diff] [blame] | 310 | rv = _drbd_request_state(mdev, mask, val, CS_WAIT_COMPLETE); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 311 | |
| 312 | /* in case we first succeeded to outdate, |
| 313 | * but now suddenly could establish a connection */ |
Andreas Gruenbacher | bf885f8 | 2010-12-08 00:39:32 +0100 | [diff] [blame] | 314 | if (rv == SS_CW_FAILED_BY_PEER && mask.pdsk != 0) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 315 | val.pdsk = 0; |
| 316 | mask.pdsk = 0; |
| 317 | continue; |
| 318 | } |
| 319 | |
Andreas Gruenbacher | bf885f8 | 2010-12-08 00:39:32 +0100 | [diff] [blame] | 320 | if (rv == SS_NO_UP_TO_DATE_DISK && force && |
Philipp Reisner | d10a33c | 2010-03-04 15:11:39 +0100 | [diff] [blame] | 321 | (mdev->state.disk < D_UP_TO_DATE && |
| 322 | mdev->state.disk >= D_INCONSISTENT)) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 323 | mask.disk = D_MASK; |
| 324 | val.disk = D_UP_TO_DATE; |
| 325 | forced = 1; |
| 326 | continue; |
| 327 | } |
| 328 | |
Andreas Gruenbacher | bf885f8 | 2010-12-08 00:39:32 +0100 | [diff] [blame] | 329 | if (rv == SS_NO_UP_TO_DATE_DISK && |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 330 | mdev->state.disk == D_CONSISTENT && mask.pdsk == 0) { |
| 331 | D_ASSERT(mdev->state.pdsk == D_UNKNOWN); |
| 332 | nps = drbd_try_outdate_peer(mdev); |
| 333 | |
| 334 | if (nps == D_OUTDATED || nps == D_INCONSISTENT) { |
| 335 | val.disk = D_UP_TO_DATE; |
| 336 | mask.disk = D_MASK; |
| 337 | } |
| 338 | |
| 339 | val.pdsk = nps; |
| 340 | mask.pdsk = D_MASK; |
| 341 | |
| 342 | continue; |
| 343 | } |
| 344 | |
Andreas Gruenbacher | bf885f8 | 2010-12-08 00:39:32 +0100 | [diff] [blame] | 345 | if (rv == SS_NOTHING_TO_DO) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 346 | goto fail; |
Andreas Gruenbacher | bf885f8 | 2010-12-08 00:39:32 +0100 | [diff] [blame] | 347 | if (rv == SS_PRIMARY_NOP && mask.pdsk == 0) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 348 | nps = drbd_try_outdate_peer(mdev); |
| 349 | |
| 350 | if (force && nps > D_OUTDATED) { |
| 351 | dev_warn(DEV, "Forced into split brain situation!\n"); |
| 352 | nps = D_OUTDATED; |
| 353 | } |
| 354 | |
| 355 | mask.pdsk = D_MASK; |
| 356 | val.pdsk = nps; |
| 357 | |
| 358 | continue; |
| 359 | } |
Andreas Gruenbacher | bf885f8 | 2010-12-08 00:39:32 +0100 | [diff] [blame] | 360 | if (rv == SS_TWO_PRIMARIES) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 361 | /* Maybe the peer is detected as dead very soon... |
| 362 | retry at most once more in this case. */ |
| 363 | __set_current_state(TASK_INTERRUPTIBLE); |
| 364 | schedule_timeout((mdev->net_conf->ping_timeo+1)*HZ/10); |
| 365 | if (try < max_tries) |
| 366 | try = max_tries - 1; |
| 367 | continue; |
| 368 | } |
Andreas Gruenbacher | bf885f8 | 2010-12-08 00:39:32 +0100 | [diff] [blame] | 369 | if (rv < SS_SUCCESS) { |
| 370 | rv = _drbd_request_state(mdev, mask, val, |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 371 | CS_VERBOSE + CS_WAIT_COMPLETE); |
Andreas Gruenbacher | bf885f8 | 2010-12-08 00:39:32 +0100 | [diff] [blame] | 372 | if (rv < SS_SUCCESS) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 373 | goto fail; |
| 374 | } |
| 375 | break; |
| 376 | } |
| 377 | |
Andreas Gruenbacher | bf885f8 | 2010-12-08 00:39:32 +0100 | [diff] [blame] | 378 | if (rv < SS_SUCCESS) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 379 | goto fail; |
| 380 | |
| 381 | if (forced) |
| 382 | dev_warn(DEV, "Forced to consider local data as UpToDate!\n"); |
| 383 | |
| 384 | /* Wait until nothing is on the fly :) */ |
| 385 | wait_event(mdev->misc_wait, atomic_read(&mdev->ap_pending_cnt) == 0); |
| 386 | |
| 387 | if (new_role == R_SECONDARY) { |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 388 | set_disk_ro(mdev->vdisk, true); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 389 | if (get_ldev(mdev)) { |
| 390 | mdev->ldev->md.uuid[UI_CURRENT] &= ~(u64)1; |
| 391 | put_ldev(mdev); |
| 392 | } |
| 393 | } else { |
| 394 | if (get_net_conf(mdev)) { |
| 395 | mdev->net_conf->want_lose = 0; |
| 396 | put_net_conf(mdev); |
| 397 | } |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 398 | set_disk_ro(mdev->vdisk, false); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 399 | if (get_ldev(mdev)) { |
| 400 | if (((mdev->state.conn < C_CONNECTED || |
| 401 | mdev->state.pdsk <= D_FAILED) |
| 402 | && mdev->ldev->md.uuid[UI_BITMAP] == 0) || forced) |
| 403 | drbd_uuid_new_current(mdev); |
| 404 | |
| 405 | mdev->ldev->md.uuid[UI_CURRENT] |= (u64)1; |
| 406 | put_ldev(mdev); |
| 407 | } |
| 408 | } |
| 409 | |
Lars Ellenberg | 19f843a | 2010-12-15 08:59:11 +0100 | [diff] [blame] | 410 | /* writeout of activity log covered areas of the bitmap |
| 411 | * to stable storage done in after state change already */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 412 | |
| 413 | if (mdev->state.conn >= C_WF_REPORT_PARAMS) { |
| 414 | /* if this was forced, we should consider sync */ |
| 415 | if (forced) |
| 416 | drbd_send_uuids(mdev); |
| 417 | drbd_send_state(mdev); |
| 418 | } |
| 419 | |
| 420 | drbd_md_sync(mdev); |
| 421 | |
| 422 | kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE); |
| 423 | fail: |
| 424 | mutex_unlock(&mdev->state_mutex); |
Andreas Gruenbacher | bf885f8 | 2010-12-08 00:39:32 +0100 | [diff] [blame] | 425 | return rv; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Lars Ellenberg | ef50a3e | 2010-09-01 14:39:30 +0200 | [diff] [blame] | 428 | static struct drbd_conf *ensure_mdev(int minor, int create) |
| 429 | { |
| 430 | struct drbd_conf *mdev; |
| 431 | |
| 432 | if (minor >= minor_count) |
| 433 | return NULL; |
| 434 | |
| 435 | mdev = minor_to_mdev(minor); |
| 436 | |
| 437 | if (!mdev && create) { |
| 438 | struct gendisk *disk = NULL; |
| 439 | mdev = drbd_new_device(minor); |
| 440 | |
| 441 | spin_lock_irq(&drbd_pp_lock); |
| 442 | if (minor_table[minor] == NULL) { |
| 443 | minor_table[minor] = mdev; |
| 444 | disk = mdev->vdisk; |
| 445 | mdev = NULL; |
| 446 | } /* else: we lost the race */ |
| 447 | spin_unlock_irq(&drbd_pp_lock); |
| 448 | |
| 449 | if (disk) /* we won the race above */ |
| 450 | /* in case we ever add a drbd_delete_device(), |
| 451 | * don't forget the del_gendisk! */ |
| 452 | add_disk(disk); |
| 453 | else /* we lost the race above */ |
| 454 | drbd_free_mdev(mdev); |
| 455 | |
| 456 | mdev = minor_to_mdev(minor); |
| 457 | } |
| 458 | |
| 459 | return mdev; |
| 460 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 461 | |
| 462 | static int drbd_nl_primary(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp, |
| 463 | struct drbd_nl_cfg_reply *reply) |
| 464 | { |
| 465 | struct primary primary_args; |
| 466 | |
| 467 | memset(&primary_args, 0, sizeof(struct primary)); |
| 468 | if (!primary_from_tags(mdev, nlp->tag_list, &primary_args)) { |
| 469 | reply->ret_code = ERR_MANDATORY_TAG; |
| 470 | return 0; |
| 471 | } |
| 472 | |
| 473 | reply->ret_code = |
Philipp Reisner | 1f55243 | 2010-03-04 15:51:01 +0100 | [diff] [blame] | 474 | drbd_set_role(mdev, R_PRIMARY, primary_args.primary_force); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 475 | |
| 476 | return 0; |
| 477 | } |
| 478 | |
| 479 | static int drbd_nl_secondary(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp, |
| 480 | struct drbd_nl_cfg_reply *reply) |
| 481 | { |
| 482 | reply->ret_code = drbd_set_role(mdev, R_SECONDARY, 0); |
| 483 | |
| 484 | return 0; |
| 485 | } |
| 486 | |
| 487 | /* initializes the md.*_offset members, so we are able to find |
| 488 | * the on disk meta data */ |
| 489 | static void drbd_md_set_sector_offsets(struct drbd_conf *mdev, |
| 490 | struct drbd_backing_dev *bdev) |
| 491 | { |
| 492 | sector_t md_size_sect = 0; |
| 493 | switch (bdev->dc.meta_dev_idx) { |
| 494 | default: |
| 495 | /* v07 style fixed size indexed meta data */ |
| 496 | bdev->md.md_size_sect = MD_RESERVED_SECT; |
| 497 | bdev->md.md_offset = drbd_md_ss__(mdev, bdev); |
| 498 | bdev->md.al_offset = MD_AL_OFFSET; |
| 499 | bdev->md.bm_offset = MD_BM_OFFSET; |
| 500 | break; |
| 501 | case DRBD_MD_INDEX_FLEX_EXT: |
| 502 | /* just occupy the full device; unit: sectors */ |
| 503 | bdev->md.md_size_sect = drbd_get_capacity(bdev->md_bdev); |
| 504 | bdev->md.md_offset = 0; |
| 505 | bdev->md.al_offset = MD_AL_OFFSET; |
| 506 | bdev->md.bm_offset = MD_BM_OFFSET; |
| 507 | break; |
| 508 | case DRBD_MD_INDEX_INTERNAL: |
| 509 | case DRBD_MD_INDEX_FLEX_INT: |
| 510 | bdev->md.md_offset = drbd_md_ss__(mdev, bdev); |
| 511 | /* al size is still fixed */ |
| 512 | bdev->md.al_offset = -MD_AL_MAX_SIZE; |
| 513 | /* we need (slightly less than) ~ this much bitmap sectors: */ |
| 514 | md_size_sect = drbd_get_capacity(bdev->backing_bdev); |
| 515 | md_size_sect = ALIGN(md_size_sect, BM_SECT_PER_EXT); |
| 516 | md_size_sect = BM_SECT_TO_EXT(md_size_sect); |
| 517 | md_size_sect = ALIGN(md_size_sect, 8); |
| 518 | |
| 519 | /* plus the "drbd meta data super block", |
| 520 | * and the activity log; */ |
| 521 | md_size_sect += MD_BM_OFFSET; |
| 522 | |
| 523 | bdev->md.md_size_sect = md_size_sect; |
| 524 | /* bitmap offset is adjusted by 'super' block size */ |
| 525 | bdev->md.bm_offset = -md_size_sect + MD_AL_OFFSET; |
| 526 | break; |
| 527 | } |
| 528 | } |
| 529 | |
Lars Ellenberg | 4b0715f | 2010-12-14 15:13:04 +0100 | [diff] [blame^] | 530 | /* input size is expected to be in KB */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 531 | char *ppsize(char *buf, unsigned long long size) |
| 532 | { |
Lars Ellenberg | 4b0715f | 2010-12-14 15:13:04 +0100 | [diff] [blame^] | 533 | /* Needs 9 bytes at max including trailing NUL: |
| 534 | * -1ULL ==> "16384 EB" */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 535 | static char units[] = { 'K', 'M', 'G', 'T', 'P', 'E' }; |
| 536 | int base = 0; |
Lars Ellenberg | 4b0715f | 2010-12-14 15:13:04 +0100 | [diff] [blame^] | 537 | while (size >= 10000 && base < sizeof(units)-1) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 538 | /* shift + round */ |
| 539 | size = (size >> 10) + !!(size & (1<<9)); |
| 540 | base++; |
| 541 | } |
Lars Ellenberg | 4b0715f | 2010-12-14 15:13:04 +0100 | [diff] [blame^] | 542 | sprintf(buf, "%u %cB", (unsigned)size, units[base]); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 543 | |
| 544 | return buf; |
| 545 | } |
| 546 | |
| 547 | /* there is still a theoretical deadlock when called from receiver |
| 548 | * on an D_INCONSISTENT R_PRIMARY: |
| 549 | * remote READ does inc_ap_bio, receiver would need to receive answer |
| 550 | * packet from remote to dec_ap_bio again. |
| 551 | * receiver receive_sizes(), comes here, |
| 552 | * waits for ap_bio_cnt == 0. -> deadlock. |
| 553 | * but this cannot happen, actually, because: |
| 554 | * R_PRIMARY D_INCONSISTENT, and peer's disk is unreachable |
| 555 | * (not connected, or bad/no disk on peer): |
| 556 | * see drbd_fail_request_early, ap_bio_cnt is zero. |
| 557 | * R_PRIMARY D_INCONSISTENT, and C_SYNC_TARGET: |
| 558 | * peer may not initiate a resize. |
| 559 | */ |
| 560 | void drbd_suspend_io(struct drbd_conf *mdev) |
| 561 | { |
| 562 | set_bit(SUSPEND_IO, &mdev->flags); |
Philipp Reisner | fb22c40 | 2010-09-08 23:20:21 +0200 | [diff] [blame] | 563 | if (is_susp(mdev->state)) |
Philipp Reisner | 265be2d | 2010-05-31 10:14:17 +0200 | [diff] [blame] | 564 | return; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 565 | wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_bio_cnt)); |
| 566 | } |
| 567 | |
| 568 | void drbd_resume_io(struct drbd_conf *mdev) |
| 569 | { |
| 570 | clear_bit(SUSPEND_IO, &mdev->flags); |
| 571 | wake_up(&mdev->misc_wait); |
| 572 | } |
| 573 | |
| 574 | /** |
| 575 | * drbd_determine_dev_size() - Sets the right device size obeying all constraints |
| 576 | * @mdev: DRBD device. |
| 577 | * |
| 578 | * Returns 0 on success, negative return values indicate errors. |
| 579 | * You should call drbd_md_sync() after calling this function. |
| 580 | */ |
Philipp Reisner | d845030 | 2010-03-24 15:51:26 +0100 | [diff] [blame] | 581 | enum determine_dev_size drbd_determin_dev_size(struct drbd_conf *mdev, enum dds_flags flags) __must_hold(local) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 582 | { |
| 583 | sector_t prev_first_sect, prev_size; /* previous meta location */ |
| 584 | sector_t la_size; |
| 585 | sector_t size; |
| 586 | char ppb[10]; |
| 587 | |
| 588 | int md_moved, la_size_changed; |
| 589 | enum determine_dev_size rv = unchanged; |
| 590 | |
| 591 | /* race: |
| 592 | * application request passes inc_ap_bio, |
| 593 | * but then cannot get an AL-reference. |
| 594 | * this function later may wait on ap_bio_cnt == 0. -> deadlock. |
| 595 | * |
| 596 | * to avoid that: |
| 597 | * Suspend IO right here. |
| 598 | * still lock the act_log to not trigger ASSERTs there. |
| 599 | */ |
| 600 | drbd_suspend_io(mdev); |
| 601 | |
| 602 | /* no wait necessary anymore, actually we could assert that */ |
| 603 | wait_event(mdev->al_wait, lc_try_lock(mdev->act_log)); |
| 604 | |
| 605 | prev_first_sect = drbd_md_first_sector(mdev->ldev); |
| 606 | prev_size = mdev->ldev->md.md_size_sect; |
| 607 | la_size = mdev->ldev->md.la_size_sect; |
| 608 | |
| 609 | /* TODO: should only be some assert here, not (re)init... */ |
| 610 | drbd_md_set_sector_offsets(mdev, mdev->ldev); |
| 611 | |
Philipp Reisner | d845030 | 2010-03-24 15:51:26 +0100 | [diff] [blame] | 612 | size = drbd_new_dev_size(mdev, mdev->ldev, flags & DDSF_FORCED); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 613 | |
| 614 | if (drbd_get_capacity(mdev->this_bdev) != size || |
| 615 | drbd_bm_capacity(mdev) != size) { |
| 616 | int err; |
Philipp Reisner | 02d9a94 | 2010-03-24 16:23:03 +0100 | [diff] [blame] | 617 | err = drbd_bm_resize(mdev, size, !(flags & DDSF_NO_RESYNC)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 618 | if (unlikely(err)) { |
| 619 | /* currently there is only one error: ENOMEM! */ |
| 620 | size = drbd_bm_capacity(mdev)>>1; |
| 621 | if (size == 0) { |
| 622 | dev_err(DEV, "OUT OF MEMORY! " |
| 623 | "Could not allocate bitmap!\n"); |
| 624 | } else { |
| 625 | dev_err(DEV, "BM resizing failed. " |
| 626 | "Leaving size unchanged at size = %lu KB\n", |
| 627 | (unsigned long)size); |
| 628 | } |
| 629 | rv = dev_size_error; |
| 630 | } |
| 631 | /* racy, see comments above. */ |
| 632 | drbd_set_my_capacity(mdev, size); |
| 633 | mdev->ldev->md.la_size_sect = size; |
| 634 | dev_info(DEV, "size = %s (%llu KB)\n", ppsize(ppb, size>>1), |
| 635 | (unsigned long long)size>>1); |
| 636 | } |
| 637 | if (rv == dev_size_error) |
| 638 | goto out; |
| 639 | |
| 640 | la_size_changed = (la_size != mdev->ldev->md.la_size_sect); |
| 641 | |
| 642 | md_moved = prev_first_sect != drbd_md_first_sector(mdev->ldev) |
| 643 | || prev_size != mdev->ldev->md.md_size_sect; |
| 644 | |
| 645 | if (la_size_changed || md_moved) { |
Andreas Gruenbacher | 24dccab | 2010-12-12 17:45:41 +0100 | [diff] [blame] | 646 | int err; |
| 647 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 648 | drbd_al_shrink(mdev); /* All extents inactive. */ |
| 649 | dev_info(DEV, "Writing the whole bitmap, %s\n", |
| 650 | la_size_changed && md_moved ? "size changed and md moved" : |
| 651 | la_size_changed ? "size changed" : "md moved"); |
Andreas Gruenbacher | 24dccab | 2010-12-12 17:45:41 +0100 | [diff] [blame] | 652 | err = drbd_bitmap_io(mdev, &drbd_bm_write, "size changed"); /* does drbd_resume_io() ! */ |
| 653 | if (err) { |
| 654 | rv = dev_size_error; |
| 655 | goto out; |
| 656 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 657 | drbd_md_mark_dirty(mdev); |
| 658 | } |
| 659 | |
| 660 | if (size > la_size) |
| 661 | rv = grew; |
| 662 | if (size < la_size) |
| 663 | rv = shrunk; |
| 664 | out: |
| 665 | lc_unlock(mdev->act_log); |
| 666 | wake_up(&mdev->al_wait); |
| 667 | drbd_resume_io(mdev); |
| 668 | |
| 669 | return rv; |
| 670 | } |
| 671 | |
| 672 | sector_t |
Philipp Reisner | a393db6 | 2009-12-22 13:35:52 +0100 | [diff] [blame] | 673 | drbd_new_dev_size(struct drbd_conf *mdev, struct drbd_backing_dev *bdev, int assume_peer_has_space) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 674 | { |
| 675 | sector_t p_size = mdev->p_size; /* partner's disk size. */ |
| 676 | sector_t la_size = bdev->md.la_size_sect; /* last agreed size. */ |
| 677 | sector_t m_size; /* my size */ |
| 678 | sector_t u_size = bdev->dc.disk_size; /* size requested by user. */ |
| 679 | sector_t size = 0; |
| 680 | |
| 681 | m_size = drbd_get_max_capacity(bdev); |
| 682 | |
Philipp Reisner | a393db6 | 2009-12-22 13:35:52 +0100 | [diff] [blame] | 683 | if (mdev->state.conn < C_CONNECTED && assume_peer_has_space) { |
| 684 | dev_warn(DEV, "Resize while not connected was forced by the user!\n"); |
| 685 | p_size = m_size; |
| 686 | } |
| 687 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 688 | if (p_size && m_size) { |
| 689 | size = min_t(sector_t, p_size, m_size); |
| 690 | } else { |
| 691 | if (la_size) { |
| 692 | size = la_size; |
| 693 | if (m_size && m_size < size) |
| 694 | size = m_size; |
| 695 | if (p_size && p_size < size) |
| 696 | size = p_size; |
| 697 | } else { |
| 698 | if (m_size) |
| 699 | size = m_size; |
| 700 | if (p_size) |
| 701 | size = p_size; |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | if (size == 0) |
| 706 | dev_err(DEV, "Both nodes diskless!\n"); |
| 707 | |
| 708 | if (u_size) { |
| 709 | if (u_size > size) |
| 710 | dev_err(DEV, "Requested disk size is too big (%lu > %lu)\n", |
| 711 | (unsigned long)u_size>>1, (unsigned long)size>>1); |
| 712 | else |
| 713 | size = u_size; |
| 714 | } |
| 715 | |
| 716 | return size; |
| 717 | } |
| 718 | |
| 719 | /** |
| 720 | * drbd_check_al_size() - Ensures that the AL is of the right size |
| 721 | * @mdev: DRBD device. |
| 722 | * |
| 723 | * Returns -EBUSY if current al lru is still used, -ENOMEM when allocation |
| 724 | * failed, and 0 on success. You should call drbd_md_sync() after you called |
| 725 | * this function. |
| 726 | */ |
| 727 | static int drbd_check_al_size(struct drbd_conf *mdev) |
| 728 | { |
| 729 | struct lru_cache *n, *t; |
| 730 | struct lc_element *e; |
| 731 | unsigned int in_use; |
| 732 | int i; |
| 733 | |
| 734 | ERR_IF(mdev->sync_conf.al_extents < 7) |
| 735 | mdev->sync_conf.al_extents = 127; |
| 736 | |
| 737 | if (mdev->act_log && |
| 738 | mdev->act_log->nr_elements == mdev->sync_conf.al_extents) |
| 739 | return 0; |
| 740 | |
| 741 | in_use = 0; |
| 742 | t = mdev->act_log; |
| 743 | n = lc_create("act_log", drbd_al_ext_cache, |
| 744 | mdev->sync_conf.al_extents, sizeof(struct lc_element), 0); |
| 745 | |
| 746 | if (n == NULL) { |
| 747 | dev_err(DEV, "Cannot allocate act_log lru!\n"); |
| 748 | return -ENOMEM; |
| 749 | } |
| 750 | spin_lock_irq(&mdev->al_lock); |
| 751 | if (t) { |
| 752 | for (i = 0; i < t->nr_elements; i++) { |
| 753 | e = lc_element_by_index(t, i); |
| 754 | if (e->refcnt) |
| 755 | dev_err(DEV, "refcnt(%d)==%d\n", |
| 756 | e->lc_number, e->refcnt); |
| 757 | in_use += e->refcnt; |
| 758 | } |
| 759 | } |
| 760 | if (!in_use) |
| 761 | mdev->act_log = n; |
| 762 | spin_unlock_irq(&mdev->al_lock); |
| 763 | if (in_use) { |
| 764 | dev_err(DEV, "Activity log still in use!\n"); |
| 765 | lc_destroy(n); |
| 766 | return -EBUSY; |
| 767 | } else { |
| 768 | if (t) |
| 769 | lc_destroy(t); |
| 770 | } |
| 771 | drbd_md_mark_dirty(mdev); /* we changed mdev->act_log->nr_elemens */ |
| 772 | return 0; |
| 773 | } |
| 774 | |
Lars Ellenberg | 1816a2b | 2010-11-11 15:19:07 +0100 | [diff] [blame] | 775 | void drbd_setup_queue_param(struct drbd_conf *mdev, unsigned int max_bio_size) __must_hold(local) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 776 | { |
| 777 | struct request_queue * const q = mdev->rq_queue; |
| 778 | struct request_queue * const b = mdev->ldev->backing_bdev->bd_disk->queue; |
| 779 | int max_segments = mdev->ldev->dc.max_bio_bvecs; |
Lars Ellenberg | 1816a2b | 2010-11-11 15:19:07 +0100 | [diff] [blame] | 780 | int max_hw_sectors = min(queue_max_hw_sectors(b), max_bio_size >> 9); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 781 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 782 | blk_queue_logical_block_size(q, 512); |
Lars Ellenberg | 1816a2b | 2010-11-11 15:19:07 +0100 | [diff] [blame] | 783 | blk_queue_max_hw_sectors(q, max_hw_sectors); |
| 784 | /* This is the workaround for "bio would need to, but cannot, be split" */ |
| 785 | blk_queue_max_segments(q, max_segments ? max_segments : BLK_MAX_SEGMENTS); |
| 786 | blk_queue_segment_boundary(q, PAGE_CACHE_SIZE-1); |
| 787 | blk_queue_stack_limits(q, b); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 788 | |
Lars Ellenberg | 1816a2b | 2010-11-11 15:19:07 +0100 | [diff] [blame] | 789 | dev_info(DEV, "max BIO size = %u\n", queue_max_hw_sectors(q) << 9); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 790 | |
| 791 | if (q->backing_dev_info.ra_pages != b->backing_dev_info.ra_pages) { |
| 792 | dev_info(DEV, "Adjusting my ra_pages to backing device's (%lu -> %lu)\n", |
| 793 | q->backing_dev_info.ra_pages, |
| 794 | b->backing_dev_info.ra_pages); |
| 795 | q->backing_dev_info.ra_pages = b->backing_dev_info.ra_pages; |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | /* serialize deconfig (worker exiting, doing cleanup) |
| 800 | * and reconfig (drbdsetup disk, drbdsetup net) |
| 801 | * |
Lars Ellenberg | c518d04 | 2010-09-01 09:50:23 +0200 | [diff] [blame] | 802 | * Wait for a potentially exiting worker, then restart it, |
| 803 | * or start a new one. Flush any pending work, there may still be an |
| 804 | * after_state_change queued. |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 805 | */ |
| 806 | static void drbd_reconfig_start(struct drbd_conf *mdev) |
| 807 | { |
Lars Ellenberg | 6c6c795 | 2009-11-16 15:48:54 +0100 | [diff] [blame] | 808 | wait_event(mdev->state_wait, !test_and_set_bit(CONFIG_PENDING, &mdev->flags)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 809 | wait_event(mdev->state_wait, !test_bit(DEVICE_DYING, &mdev->flags)); |
| 810 | drbd_thread_start(&mdev->worker); |
Lars Ellenberg | c518d04 | 2010-09-01 09:50:23 +0200 | [diff] [blame] | 811 | drbd_flush_workqueue(mdev); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | /* if still unconfigured, stops worker again. |
| 815 | * if configured now, clears CONFIG_PENDING. |
| 816 | * wakes potential waiters */ |
| 817 | static void drbd_reconfig_done(struct drbd_conf *mdev) |
| 818 | { |
| 819 | spin_lock_irq(&mdev->req_lock); |
| 820 | if (mdev->state.disk == D_DISKLESS && |
| 821 | mdev->state.conn == C_STANDALONE && |
| 822 | mdev->state.role == R_SECONDARY) { |
| 823 | set_bit(DEVICE_DYING, &mdev->flags); |
| 824 | drbd_thread_stop_nowait(&mdev->worker); |
| 825 | } else |
| 826 | clear_bit(CONFIG_PENDING, &mdev->flags); |
| 827 | spin_unlock_irq(&mdev->req_lock); |
| 828 | wake_up(&mdev->state_wait); |
| 829 | } |
| 830 | |
Philipp Reisner | 0778286 | 2010-08-31 12:00:50 +0200 | [diff] [blame] | 831 | /* Make sure IO is suspended before calling this function(). */ |
| 832 | static void drbd_suspend_al(struct drbd_conf *mdev) |
| 833 | { |
| 834 | int s = 0; |
| 835 | |
| 836 | if (lc_try_lock(mdev->act_log)) { |
| 837 | drbd_al_shrink(mdev); |
| 838 | lc_unlock(mdev->act_log); |
| 839 | } else { |
| 840 | dev_warn(DEV, "Failed to lock al in drbd_suspend_al()\n"); |
| 841 | return; |
| 842 | } |
| 843 | |
| 844 | spin_lock_irq(&mdev->req_lock); |
| 845 | if (mdev->state.conn < C_CONNECTED) |
| 846 | s = !test_and_set_bit(AL_SUSPENDED, &mdev->flags); |
| 847 | |
| 848 | spin_unlock_irq(&mdev->req_lock); |
| 849 | |
| 850 | if (s) |
| 851 | dev_info(DEV, "Suspended AL updates\n"); |
| 852 | } |
| 853 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 854 | /* does always return 0; |
| 855 | * interesting return code is in reply->ret_code */ |
| 856 | static int drbd_nl_disk_conf(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp, |
| 857 | struct drbd_nl_cfg_reply *reply) |
| 858 | { |
Andreas Gruenbacher | 116676c | 2010-12-08 13:33:11 +0100 | [diff] [blame] | 859 | enum drbd_ret_code retcode; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 860 | enum determine_dev_size dd; |
| 861 | sector_t max_possible_sectors; |
| 862 | sector_t min_md_device_sectors; |
| 863 | struct drbd_backing_dev *nbc = NULL; /* new_backing_conf */ |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 864 | struct block_device *bdev; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 865 | struct lru_cache *resync_lru = NULL; |
| 866 | union drbd_state ns, os; |
Lars Ellenberg | 1816a2b | 2010-11-11 15:19:07 +0100 | [diff] [blame] | 867 | unsigned int max_bio_size; |
Andreas Gruenbacher | f2024e7 | 2010-12-10 13:44:05 +0100 | [diff] [blame] | 868 | enum drbd_state_rv rv; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 869 | int cp_discovered = 0; |
| 870 | int logical_block_size; |
| 871 | |
| 872 | drbd_reconfig_start(mdev); |
| 873 | |
| 874 | /* if you want to reconfigure, please tear down first */ |
| 875 | if (mdev->state.disk > D_DISKLESS) { |
| 876 | retcode = ERR_DISK_CONFIGURED; |
| 877 | goto fail; |
| 878 | } |
Lars Ellenberg | 82f59cc | 2010-10-16 12:13:47 +0200 | [diff] [blame] | 879 | /* It may just now have detached because of IO error. Make sure |
| 880 | * drbd_ldev_destroy is done already, we may end up here very fast, |
| 881 | * e.g. if someone calls attach from the on-io-error handler, |
| 882 | * to realize a "hot spare" feature (not that I'd recommend that) */ |
| 883 | wait_event(mdev->misc_wait, !atomic_read(&mdev->local_cnt)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 884 | |
| 885 | /* allocation not in the IO path, cqueue thread context */ |
| 886 | nbc = kzalloc(sizeof(struct drbd_backing_dev), GFP_KERNEL); |
| 887 | if (!nbc) { |
| 888 | retcode = ERR_NOMEM; |
| 889 | goto fail; |
| 890 | } |
| 891 | |
| 892 | nbc->dc.disk_size = DRBD_DISK_SIZE_SECT_DEF; |
| 893 | nbc->dc.on_io_error = DRBD_ON_IO_ERROR_DEF; |
| 894 | nbc->dc.fencing = DRBD_FENCING_DEF; |
| 895 | nbc->dc.max_bio_bvecs = DRBD_MAX_BIO_BVECS_DEF; |
| 896 | |
| 897 | if (!disk_conf_from_tags(mdev, nlp->tag_list, &nbc->dc)) { |
| 898 | retcode = ERR_MANDATORY_TAG; |
| 899 | goto fail; |
| 900 | } |
| 901 | |
| 902 | if (nbc->dc.meta_dev_idx < DRBD_MD_INDEX_FLEX_INT) { |
| 903 | retcode = ERR_MD_IDX_INVALID; |
| 904 | goto fail; |
| 905 | } |
| 906 | |
Philipp Reisner | 47ff2d0 | 2010-06-18 13:56:57 +0200 | [diff] [blame] | 907 | if (get_net_conf(mdev)) { |
| 908 | int prot = mdev->net_conf->wire_protocol; |
| 909 | put_net_conf(mdev); |
| 910 | if (nbc->dc.fencing == FP_STONITH && prot == DRBD_PROT_A) { |
| 911 | retcode = ERR_STONITH_AND_PROT_A; |
| 912 | goto fail; |
| 913 | } |
| 914 | } |
| 915 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 916 | bdev = blkdev_get_by_path(nbc->dc.backing_dev, |
| 917 | FMODE_READ | FMODE_WRITE | FMODE_EXCL, mdev); |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 918 | if (IS_ERR(bdev)) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 919 | dev_err(DEV, "open(\"%s\") failed with %ld\n", nbc->dc.backing_dev, |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 920 | PTR_ERR(bdev)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 921 | retcode = ERR_OPEN_DISK; |
| 922 | goto fail; |
| 923 | } |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 924 | nbc->backing_bdev = bdev; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 925 | |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 926 | /* |
| 927 | * meta_dev_idx >= 0: external fixed size, possibly multiple |
| 928 | * drbd sharing one meta device. TODO in that case, paranoia |
| 929 | * check that [md_bdev, meta_dev_idx] is not yet used by some |
| 930 | * other drbd minor! (if you use drbd.conf + drbdadm, that |
| 931 | * should check it for you already; but if you don't, or |
| 932 | * someone fooled it, we need to double check here) |
| 933 | */ |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 934 | bdev = blkdev_get_by_path(nbc->dc.meta_dev, |
| 935 | FMODE_READ | FMODE_WRITE | FMODE_EXCL, |
| 936 | (nbc->dc.meta_dev_idx < 0) ? |
| 937 | (void *)mdev : (void *)drbd_m_holder); |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 938 | if (IS_ERR(bdev)) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 939 | dev_err(DEV, "open(\"%s\") failed with %ld\n", nbc->dc.meta_dev, |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 940 | PTR_ERR(bdev)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 941 | retcode = ERR_OPEN_MD_DISK; |
| 942 | goto fail; |
| 943 | } |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 944 | nbc->md_bdev = bdev; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 945 | |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 946 | if ((nbc->backing_bdev == nbc->md_bdev) != |
| 947 | (nbc->dc.meta_dev_idx == DRBD_MD_INDEX_INTERNAL || |
| 948 | nbc->dc.meta_dev_idx == DRBD_MD_INDEX_FLEX_INT)) { |
| 949 | retcode = ERR_MD_IDX_INVALID; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 950 | goto fail; |
| 951 | } |
| 952 | |
| 953 | resync_lru = lc_create("resync", drbd_bm_ext_cache, |
| 954 | 61, sizeof(struct bm_extent), |
| 955 | offsetof(struct bm_extent, lce)); |
| 956 | if (!resync_lru) { |
| 957 | retcode = ERR_NOMEM; |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 958 | goto fail; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | /* RT - for drbd_get_max_capacity() DRBD_MD_INDEX_FLEX_INT */ |
| 962 | drbd_md_set_sector_offsets(mdev, nbc); |
| 963 | |
| 964 | if (drbd_get_max_capacity(nbc) < nbc->dc.disk_size) { |
| 965 | dev_err(DEV, "max capacity %llu smaller than disk size %llu\n", |
| 966 | (unsigned long long) drbd_get_max_capacity(nbc), |
| 967 | (unsigned long long) nbc->dc.disk_size); |
| 968 | retcode = ERR_DISK_TO_SMALL; |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 969 | goto fail; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | if (nbc->dc.meta_dev_idx < 0) { |
| 973 | max_possible_sectors = DRBD_MAX_SECTORS_FLEX; |
| 974 | /* at least one MB, otherwise it does not make sense */ |
| 975 | min_md_device_sectors = (2<<10); |
| 976 | } else { |
| 977 | max_possible_sectors = DRBD_MAX_SECTORS; |
| 978 | min_md_device_sectors = MD_RESERVED_SECT * (nbc->dc.meta_dev_idx + 1); |
| 979 | } |
| 980 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 981 | if (drbd_get_capacity(nbc->md_bdev) < min_md_device_sectors) { |
| 982 | retcode = ERR_MD_DISK_TO_SMALL; |
| 983 | dev_warn(DEV, "refusing attach: md-device too small, " |
| 984 | "at least %llu sectors needed for this meta-disk type\n", |
| 985 | (unsigned long long) min_md_device_sectors); |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 986 | goto fail; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | /* Make sure the new disk is big enough |
| 990 | * (we may currently be R_PRIMARY with no local disk...) */ |
| 991 | if (drbd_get_max_capacity(nbc) < |
| 992 | drbd_get_capacity(mdev->this_bdev)) { |
| 993 | retcode = ERR_DISK_TO_SMALL; |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 994 | goto fail; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | nbc->known_size = drbd_get_capacity(nbc->backing_bdev); |
| 998 | |
Lars Ellenberg | 1352994 | 2009-10-12 19:07:49 +0200 | [diff] [blame] | 999 | if (nbc->known_size > max_possible_sectors) { |
| 1000 | dev_warn(DEV, "==> truncating very big lower level device " |
| 1001 | "to currently maximum possible %llu sectors <==\n", |
| 1002 | (unsigned long long) max_possible_sectors); |
| 1003 | if (nbc->dc.meta_dev_idx >= 0) |
| 1004 | dev_warn(DEV, "==>> using internal or flexible " |
| 1005 | "meta data may help <<==\n"); |
| 1006 | } |
| 1007 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1008 | drbd_suspend_io(mdev); |
| 1009 | /* also wait for the last barrier ack. */ |
Philipp Reisner | fb22c40 | 2010-09-08 23:20:21 +0200 | [diff] [blame] | 1010 | wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_pending_cnt) || is_susp(mdev->state)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1011 | /* and for any other previously queued work */ |
| 1012 | drbd_flush_workqueue(mdev); |
| 1013 | |
Andreas Gruenbacher | f2024e7 | 2010-12-10 13:44:05 +0100 | [diff] [blame] | 1014 | rv = _drbd_request_state(mdev, NS(disk, D_ATTACHING), CS_VERBOSE); |
| 1015 | retcode = rv; /* FIXME: Type mismatch. */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1016 | drbd_resume_io(mdev); |
Andreas Gruenbacher | f2024e7 | 2010-12-10 13:44:05 +0100 | [diff] [blame] | 1017 | if (rv < SS_SUCCESS) |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1018 | goto fail; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1019 | |
| 1020 | if (!get_ldev_if_state(mdev, D_ATTACHING)) |
| 1021 | goto force_diskless; |
| 1022 | |
| 1023 | drbd_md_set_sector_offsets(mdev, nbc); |
| 1024 | |
Lars Ellenberg | 4aa83b7 | 2010-02-26 16:53:24 +0100 | [diff] [blame] | 1025 | /* allocate a second IO page if logical_block_size != 512 */ |
| 1026 | logical_block_size = bdev_logical_block_size(nbc->md_bdev); |
| 1027 | if (logical_block_size == 0) |
| 1028 | logical_block_size = MD_SECTOR_SIZE; |
| 1029 | |
| 1030 | if (logical_block_size != MD_SECTOR_SIZE) { |
| 1031 | if (!mdev->md_io_tmpp) { |
| 1032 | struct page *page = alloc_page(GFP_NOIO); |
| 1033 | if (!page) |
| 1034 | goto force_diskless_dec; |
| 1035 | |
| 1036 | dev_warn(DEV, "Meta data's bdev logical_block_size = %d != %d\n", |
| 1037 | logical_block_size, MD_SECTOR_SIZE); |
| 1038 | dev_warn(DEV, "Workaround engaged (has performance impact).\n"); |
| 1039 | |
| 1040 | mdev->md_io_tmpp = page; |
| 1041 | } |
| 1042 | } |
| 1043 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1044 | if (!mdev->bitmap) { |
| 1045 | if (drbd_bm_init(mdev)) { |
| 1046 | retcode = ERR_NOMEM; |
| 1047 | goto force_diskless_dec; |
| 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | retcode = drbd_md_read(mdev, nbc); |
| 1052 | if (retcode != NO_ERROR) |
| 1053 | goto force_diskless_dec; |
| 1054 | |
| 1055 | if (mdev->state.conn < C_CONNECTED && |
| 1056 | mdev->state.role == R_PRIMARY && |
| 1057 | (mdev->ed_uuid & ~((u64)1)) != (nbc->md.uuid[UI_CURRENT] & ~((u64)1))) { |
| 1058 | dev_err(DEV, "Can only attach to data with current UUID=%016llX\n", |
| 1059 | (unsigned long long)mdev->ed_uuid); |
| 1060 | retcode = ERR_DATA_NOT_CURRENT; |
| 1061 | goto force_diskless_dec; |
| 1062 | } |
| 1063 | |
| 1064 | /* Since we are diskless, fix the activity log first... */ |
| 1065 | if (drbd_check_al_size(mdev)) { |
| 1066 | retcode = ERR_NOMEM; |
| 1067 | goto force_diskless_dec; |
| 1068 | } |
| 1069 | |
| 1070 | /* Prevent shrinking of consistent devices ! */ |
| 1071 | if (drbd_md_test_flag(nbc, MDF_CONSISTENT) && |
Philipp Reisner | a393db6 | 2009-12-22 13:35:52 +0100 | [diff] [blame] | 1072 | drbd_new_dev_size(mdev, nbc, 0) < nbc->md.la_size_sect) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1073 | dev_warn(DEV, "refusing to truncate a consistent device\n"); |
| 1074 | retcode = ERR_DISK_TO_SMALL; |
| 1075 | goto force_diskless_dec; |
| 1076 | } |
| 1077 | |
| 1078 | if (!drbd_al_read_log(mdev, nbc)) { |
| 1079 | retcode = ERR_IO_MD_DISK; |
| 1080 | goto force_diskless_dec; |
| 1081 | } |
| 1082 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1083 | /* Reset the "barriers don't work" bits here, then force meta data to |
| 1084 | * be written, to ensure we determine if barriers are supported. */ |
| 1085 | if (nbc->dc.no_md_flush) |
Philipp Reisner | a8a4e51 | 2010-08-25 10:21:04 +0200 | [diff] [blame] | 1086 | set_bit(MD_NO_FUA, &mdev->flags); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1087 | else |
Philipp Reisner | a8a4e51 | 2010-08-25 10:21:04 +0200 | [diff] [blame] | 1088 | clear_bit(MD_NO_FUA, &mdev->flags); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1089 | |
| 1090 | /* Point of no return reached. |
| 1091 | * Devices and memory are no longer released by error cleanup below. |
| 1092 | * now mdev takes over responsibility, and the state engine should |
| 1093 | * clean it up somewhere. */ |
| 1094 | D_ASSERT(mdev->ldev == NULL); |
| 1095 | mdev->ldev = nbc; |
| 1096 | mdev->resync = resync_lru; |
| 1097 | nbc = NULL; |
| 1098 | resync_lru = NULL; |
| 1099 | |
Philipp Reisner | 2451fc3 | 2010-08-24 13:43:11 +0200 | [diff] [blame] | 1100 | mdev->write_ordering = WO_bdev_flush; |
| 1101 | drbd_bump_write_ordering(mdev, WO_bdev_flush); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1102 | |
| 1103 | if (drbd_md_test_flag(mdev->ldev, MDF_CRASHED_PRIMARY)) |
| 1104 | set_bit(CRASHED_PRIMARY, &mdev->flags); |
| 1105 | else |
| 1106 | clear_bit(CRASHED_PRIMARY, &mdev->flags); |
| 1107 | |
Philipp Reisner | 894c6a9 | 2010-06-18 16:03:20 +0200 | [diff] [blame] | 1108 | if (drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) && |
Philipp Reisner | fb22c40 | 2010-09-08 23:20:21 +0200 | [diff] [blame] | 1109 | !(mdev->state.role == R_PRIMARY && mdev->state.susp_nod)) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1110 | set_bit(CRASHED_PRIMARY, &mdev->flags); |
| 1111 | cp_discovered = 1; |
| 1112 | } |
| 1113 | |
| 1114 | mdev->send_cnt = 0; |
| 1115 | mdev->recv_cnt = 0; |
| 1116 | mdev->read_cnt = 0; |
| 1117 | mdev->writ_cnt = 0; |
| 1118 | |
Lars Ellenberg | 1816a2b | 2010-11-11 15:19:07 +0100 | [diff] [blame] | 1119 | max_bio_size = DRBD_MAX_BIO_SIZE; |
Lars Ellenberg | 8979d9c | 2010-09-14 15:56:29 +0200 | [diff] [blame] | 1120 | if (mdev->state.conn == C_CONNECTED) { |
| 1121 | /* We are Primary, Connected, and now attach a new local |
| 1122 | * backing store. We must not increase the user visible maximum |
| 1123 | * bio size on this device to something the peer may not be |
| 1124 | * able to handle. */ |
| 1125 | if (mdev->agreed_pro_version < 94) |
Lars Ellenberg | 1816a2b | 2010-11-11 15:19:07 +0100 | [diff] [blame] | 1126 | max_bio_size = queue_max_hw_sectors(mdev->rq_queue) << 9; |
Lars Ellenberg | 8979d9c | 2010-09-14 15:56:29 +0200 | [diff] [blame] | 1127 | else if (mdev->agreed_pro_version == 94) |
Lars Ellenberg | 1816a2b | 2010-11-11 15:19:07 +0100 | [diff] [blame] | 1128 | max_bio_size = DRBD_MAX_SIZE_H80_PACKET; |
Lars Ellenberg | 8979d9c | 2010-09-14 15:56:29 +0200 | [diff] [blame] | 1129 | /* else: drbd 8.3.9 and later, stay with default */ |
| 1130 | } |
| 1131 | |
Lars Ellenberg | 1816a2b | 2010-11-11 15:19:07 +0100 | [diff] [blame] | 1132 | drbd_setup_queue_param(mdev, max_bio_size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1133 | |
| 1134 | /* If I am currently not R_PRIMARY, |
| 1135 | * but meta data primary indicator is set, |
| 1136 | * I just now recover from a hard crash, |
| 1137 | * and have been R_PRIMARY before that crash. |
| 1138 | * |
| 1139 | * Now, if I had no connection before that crash |
| 1140 | * (have been degraded R_PRIMARY), chances are that |
| 1141 | * I won't find my peer now either. |
| 1142 | * |
| 1143 | * In that case, and _only_ in that case, |
| 1144 | * we use the degr-wfc-timeout instead of the default, |
| 1145 | * so we can automatically recover from a crash of a |
| 1146 | * degraded but active "cluster" after a certain timeout. |
| 1147 | */ |
| 1148 | clear_bit(USE_DEGR_WFC_T, &mdev->flags); |
| 1149 | if (mdev->state.role != R_PRIMARY && |
| 1150 | drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) && |
| 1151 | !drbd_md_test_flag(mdev->ldev, MDF_CONNECTED_IND)) |
| 1152 | set_bit(USE_DEGR_WFC_T, &mdev->flags); |
| 1153 | |
Philipp Reisner | a393db6 | 2009-12-22 13:35:52 +0100 | [diff] [blame] | 1154 | dd = drbd_determin_dev_size(mdev, 0); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1155 | if (dd == dev_size_error) { |
| 1156 | retcode = ERR_NOMEM_BITMAP; |
| 1157 | goto force_diskless_dec; |
| 1158 | } else if (dd == grew) |
| 1159 | set_bit(RESYNC_AFTER_NEG, &mdev->flags); |
| 1160 | |
| 1161 | if (drbd_md_test_flag(mdev->ldev, MDF_FULL_SYNC)) { |
| 1162 | dev_info(DEV, "Assuming that all blocks are out of sync " |
| 1163 | "(aka FullSync)\n"); |
| 1164 | if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write, "set_n_write from attaching")) { |
| 1165 | retcode = ERR_IO_MD_DISK; |
| 1166 | goto force_diskless_dec; |
| 1167 | } |
| 1168 | } else { |
| 1169 | if (drbd_bitmap_io(mdev, &drbd_bm_read, "read from attaching") < 0) { |
| 1170 | retcode = ERR_IO_MD_DISK; |
| 1171 | goto force_diskless_dec; |
| 1172 | } |
| 1173 | } |
| 1174 | |
| 1175 | if (cp_discovered) { |
| 1176 | drbd_al_apply_to_bm(mdev); |
Lars Ellenberg | 19f843a | 2010-12-15 08:59:11 +0100 | [diff] [blame] | 1177 | if (drbd_bitmap_io(mdev, &drbd_bm_write, "crashed primary apply AL")) { |
| 1178 | retcode = ERR_IO_MD_DISK; |
| 1179 | goto force_diskless_dec; |
| 1180 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1181 | } |
| 1182 | |
Philipp Reisner | 0778286 | 2010-08-31 12:00:50 +0200 | [diff] [blame] | 1183 | if (_drbd_bm_total_weight(mdev) == drbd_bm_bits(mdev)) |
| 1184 | drbd_suspend_al(mdev); /* IO is still suspended here... */ |
| 1185 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1186 | spin_lock_irq(&mdev->req_lock); |
| 1187 | os = mdev->state; |
| 1188 | ns.i = os.i; |
| 1189 | /* If MDF_CONSISTENT is not set go into inconsistent state, |
| 1190 | otherwise investigate MDF_WasUpToDate... |
| 1191 | If MDF_WAS_UP_TO_DATE is not set go into D_OUTDATED disk state, |
| 1192 | otherwise into D_CONSISTENT state. |
| 1193 | */ |
| 1194 | if (drbd_md_test_flag(mdev->ldev, MDF_CONSISTENT)) { |
| 1195 | if (drbd_md_test_flag(mdev->ldev, MDF_WAS_UP_TO_DATE)) |
| 1196 | ns.disk = D_CONSISTENT; |
| 1197 | else |
| 1198 | ns.disk = D_OUTDATED; |
| 1199 | } else { |
| 1200 | ns.disk = D_INCONSISTENT; |
| 1201 | } |
| 1202 | |
| 1203 | if (drbd_md_test_flag(mdev->ldev, MDF_PEER_OUT_DATED)) |
| 1204 | ns.pdsk = D_OUTDATED; |
| 1205 | |
| 1206 | if ( ns.disk == D_CONSISTENT && |
| 1207 | (ns.pdsk == D_OUTDATED || mdev->ldev->dc.fencing == FP_DONT_CARE)) |
| 1208 | ns.disk = D_UP_TO_DATE; |
| 1209 | |
| 1210 | /* All tests on MDF_PRIMARY_IND, MDF_CONNECTED_IND, |
| 1211 | MDF_CONSISTENT and MDF_WAS_UP_TO_DATE must happen before |
| 1212 | this point, because drbd_request_state() modifies these |
| 1213 | flags. */ |
| 1214 | |
| 1215 | /* In case we are C_CONNECTED postpone any decision on the new disk |
| 1216 | state after the negotiation phase. */ |
| 1217 | if (mdev->state.conn == C_CONNECTED) { |
| 1218 | mdev->new_state_tmp.i = ns.i; |
| 1219 | ns.i = os.i; |
| 1220 | ns.disk = D_NEGOTIATING; |
Philipp Reisner | dc66c74 | 2010-06-02 14:31:29 +0200 | [diff] [blame] | 1221 | |
| 1222 | /* We expect to receive up-to-date UUIDs soon. |
| 1223 | To avoid a race in receive_state, free p_uuid while |
| 1224 | holding req_lock. I.e. atomic with the state change */ |
| 1225 | kfree(mdev->p_uuid); |
| 1226 | mdev->p_uuid = NULL; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | rv = _drbd_set_state(mdev, ns, CS_VERBOSE, NULL); |
| 1230 | ns = mdev->state; |
| 1231 | spin_unlock_irq(&mdev->req_lock); |
| 1232 | |
| 1233 | if (rv < SS_SUCCESS) |
| 1234 | goto force_diskless_dec; |
| 1235 | |
| 1236 | if (mdev->state.role == R_PRIMARY) |
| 1237 | mdev->ldev->md.uuid[UI_CURRENT] |= (u64)1; |
| 1238 | else |
| 1239 | mdev->ldev->md.uuid[UI_CURRENT] &= ~(u64)1; |
| 1240 | |
| 1241 | drbd_md_mark_dirty(mdev); |
| 1242 | drbd_md_sync(mdev); |
| 1243 | |
| 1244 | kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE); |
| 1245 | put_ldev(mdev); |
| 1246 | reply->ret_code = retcode; |
| 1247 | drbd_reconfig_done(mdev); |
| 1248 | return 0; |
| 1249 | |
| 1250 | force_diskless_dec: |
| 1251 | put_ldev(mdev); |
| 1252 | force_diskless: |
Lars Ellenberg | 82f59cc | 2010-10-16 12:13:47 +0200 | [diff] [blame] | 1253 | drbd_force_state(mdev, NS(disk, D_FAILED)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1254 | drbd_md_sync(mdev); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1255 | fail: |
| 1256 | if (nbc) { |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1257 | if (nbc->backing_bdev) |
| 1258 | blkdev_put(nbc->backing_bdev, |
| 1259 | FMODE_READ | FMODE_WRITE | FMODE_EXCL); |
| 1260 | if (nbc->md_bdev) |
| 1261 | blkdev_put(nbc->md_bdev, |
| 1262 | FMODE_READ | FMODE_WRITE | FMODE_EXCL); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1263 | kfree(nbc); |
| 1264 | } |
| 1265 | lc_destroy(resync_lru); |
| 1266 | |
| 1267 | reply->ret_code = retcode; |
| 1268 | drbd_reconfig_done(mdev); |
| 1269 | return 0; |
| 1270 | } |
| 1271 | |
Lars Ellenberg | 82f59cc | 2010-10-16 12:13:47 +0200 | [diff] [blame] | 1272 | /* Detaching the disk is a process in multiple stages. First we need to lock |
| 1273 | * out application IO, in-flight IO, IO stuck in drbd_al_begin_io. |
| 1274 | * Then we transition to D_DISKLESS, and wait for put_ldev() to return all |
| 1275 | * internal references as well. |
| 1276 | * Only then we have finally detached. */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1277 | static int drbd_nl_detach(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp, |
| 1278 | struct drbd_nl_cfg_reply *reply) |
| 1279 | { |
Lars Ellenberg | 82f59cc | 2010-10-16 12:13:47 +0200 | [diff] [blame] | 1280 | drbd_suspend_io(mdev); /* so no-one is stuck in drbd_al_begin_io */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1281 | reply->ret_code = drbd_request_state(mdev, NS(disk, D_DISKLESS)); |
Lars Ellenberg | 82f59cc | 2010-10-16 12:13:47 +0200 | [diff] [blame] | 1282 | if (mdev->state.disk == D_DISKLESS) |
| 1283 | wait_event(mdev->misc_wait, !atomic_read(&mdev->local_cnt)); |
| 1284 | drbd_resume_io(mdev); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1285 | return 0; |
| 1286 | } |
| 1287 | |
| 1288 | static int drbd_nl_net_conf(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp, |
| 1289 | struct drbd_nl_cfg_reply *reply) |
| 1290 | { |
| 1291 | int i, ns; |
Andreas Gruenbacher | 116676c | 2010-12-08 13:33:11 +0100 | [diff] [blame] | 1292 | enum drbd_ret_code retcode; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1293 | struct net_conf *new_conf = NULL; |
| 1294 | struct crypto_hash *tfm = NULL; |
| 1295 | struct crypto_hash *integrity_w_tfm = NULL; |
| 1296 | struct crypto_hash *integrity_r_tfm = NULL; |
| 1297 | struct hlist_head *new_tl_hash = NULL; |
| 1298 | struct hlist_head *new_ee_hash = NULL; |
| 1299 | struct drbd_conf *odev; |
| 1300 | char hmac_name[CRYPTO_MAX_ALG_NAME]; |
| 1301 | void *int_dig_out = NULL; |
| 1302 | void *int_dig_in = NULL; |
| 1303 | void *int_dig_vv = NULL; |
| 1304 | struct sockaddr *new_my_addr, *new_peer_addr, *taken_addr; |
| 1305 | |
| 1306 | drbd_reconfig_start(mdev); |
| 1307 | |
| 1308 | if (mdev->state.conn > C_STANDALONE) { |
| 1309 | retcode = ERR_NET_CONFIGURED; |
| 1310 | goto fail; |
| 1311 | } |
| 1312 | |
| 1313 | /* allocation not in the IO path, cqueue thread context */ |
Julia Lawall | 2db4e42 | 2010-05-13 22:02:21 +0200 | [diff] [blame] | 1314 | new_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1315 | if (!new_conf) { |
| 1316 | retcode = ERR_NOMEM; |
| 1317 | goto fail; |
| 1318 | } |
| 1319 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1320 | new_conf->timeout = DRBD_TIMEOUT_DEF; |
| 1321 | new_conf->try_connect_int = DRBD_CONNECT_INT_DEF; |
| 1322 | new_conf->ping_int = DRBD_PING_INT_DEF; |
| 1323 | new_conf->max_epoch_size = DRBD_MAX_EPOCH_SIZE_DEF; |
| 1324 | new_conf->max_buffers = DRBD_MAX_BUFFERS_DEF; |
| 1325 | new_conf->unplug_watermark = DRBD_UNPLUG_WATERMARK_DEF; |
| 1326 | new_conf->sndbuf_size = DRBD_SNDBUF_SIZE_DEF; |
| 1327 | new_conf->rcvbuf_size = DRBD_RCVBUF_SIZE_DEF; |
| 1328 | new_conf->ko_count = DRBD_KO_COUNT_DEF; |
| 1329 | new_conf->after_sb_0p = DRBD_AFTER_SB_0P_DEF; |
| 1330 | new_conf->after_sb_1p = DRBD_AFTER_SB_1P_DEF; |
| 1331 | new_conf->after_sb_2p = DRBD_AFTER_SB_2P_DEF; |
| 1332 | new_conf->want_lose = 0; |
| 1333 | new_conf->two_primaries = 0; |
| 1334 | new_conf->wire_protocol = DRBD_PROT_C; |
| 1335 | new_conf->ping_timeo = DRBD_PING_TIMEO_DEF; |
| 1336 | new_conf->rr_conflict = DRBD_RR_CONFLICT_DEF; |
Philipp Reisner | 422028b | 2010-10-27 11:12:07 +0200 | [diff] [blame] | 1337 | new_conf->on_congestion = DRBD_ON_CONGESTION_DEF; |
| 1338 | new_conf->cong_extents = DRBD_CONG_EXTENTS_DEF; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1339 | |
| 1340 | if (!net_conf_from_tags(mdev, nlp->tag_list, new_conf)) { |
| 1341 | retcode = ERR_MANDATORY_TAG; |
| 1342 | goto fail; |
| 1343 | } |
| 1344 | |
| 1345 | if (new_conf->two_primaries |
| 1346 | && (new_conf->wire_protocol != DRBD_PROT_C)) { |
| 1347 | retcode = ERR_NOT_PROTO_C; |
| 1348 | goto fail; |
Philipp Reisner | 47ff2d0 | 2010-06-18 13:56:57 +0200 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | if (get_ldev(mdev)) { |
| 1352 | enum drbd_fencing_p fp = mdev->ldev->dc.fencing; |
| 1353 | put_ldev(mdev); |
| 1354 | if (new_conf->wire_protocol == DRBD_PROT_A && fp == FP_STONITH) { |
| 1355 | retcode = ERR_STONITH_AND_PROT_A; |
| 1356 | goto fail; |
| 1357 | } |
| 1358 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1359 | |
Philipp Reisner | 422028b | 2010-10-27 11:12:07 +0200 | [diff] [blame] | 1360 | if (new_conf->on_congestion != OC_BLOCK && new_conf->wire_protocol != DRBD_PROT_A) { |
| 1361 | retcode = ERR_CONG_NOT_PROTO_A; |
| 1362 | goto fail; |
| 1363 | } |
| 1364 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1365 | if (mdev->state.role == R_PRIMARY && new_conf->want_lose) { |
| 1366 | retcode = ERR_DISCARD; |
| 1367 | goto fail; |
| 1368 | } |
| 1369 | |
| 1370 | retcode = NO_ERROR; |
| 1371 | |
| 1372 | new_my_addr = (struct sockaddr *)&new_conf->my_addr; |
| 1373 | new_peer_addr = (struct sockaddr *)&new_conf->peer_addr; |
| 1374 | for (i = 0; i < minor_count; i++) { |
| 1375 | odev = minor_to_mdev(i); |
| 1376 | if (!odev || odev == mdev) |
| 1377 | continue; |
| 1378 | if (get_net_conf(odev)) { |
| 1379 | taken_addr = (struct sockaddr *)&odev->net_conf->my_addr; |
| 1380 | if (new_conf->my_addr_len == odev->net_conf->my_addr_len && |
| 1381 | !memcmp(new_my_addr, taken_addr, new_conf->my_addr_len)) |
| 1382 | retcode = ERR_LOCAL_ADDR; |
| 1383 | |
| 1384 | taken_addr = (struct sockaddr *)&odev->net_conf->peer_addr; |
| 1385 | if (new_conf->peer_addr_len == odev->net_conf->peer_addr_len && |
| 1386 | !memcmp(new_peer_addr, taken_addr, new_conf->peer_addr_len)) |
| 1387 | retcode = ERR_PEER_ADDR; |
| 1388 | |
| 1389 | put_net_conf(odev); |
| 1390 | if (retcode != NO_ERROR) |
| 1391 | goto fail; |
| 1392 | } |
| 1393 | } |
| 1394 | |
| 1395 | if (new_conf->cram_hmac_alg[0] != 0) { |
| 1396 | snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)", |
| 1397 | new_conf->cram_hmac_alg); |
| 1398 | tfm = crypto_alloc_hash(hmac_name, 0, CRYPTO_ALG_ASYNC); |
| 1399 | if (IS_ERR(tfm)) { |
| 1400 | tfm = NULL; |
| 1401 | retcode = ERR_AUTH_ALG; |
| 1402 | goto fail; |
| 1403 | } |
| 1404 | |
Philipp Reisner | 0798219 | 2009-12-28 16:58:38 +0100 | [diff] [blame] | 1405 | if (!drbd_crypto_is_hash(crypto_hash_tfm(tfm))) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1406 | retcode = ERR_AUTH_ALG_ND; |
| 1407 | goto fail; |
| 1408 | } |
| 1409 | } |
| 1410 | |
| 1411 | if (new_conf->integrity_alg[0]) { |
| 1412 | integrity_w_tfm = crypto_alloc_hash(new_conf->integrity_alg, 0, CRYPTO_ALG_ASYNC); |
| 1413 | if (IS_ERR(integrity_w_tfm)) { |
| 1414 | integrity_w_tfm = NULL; |
| 1415 | retcode=ERR_INTEGRITY_ALG; |
| 1416 | goto fail; |
| 1417 | } |
| 1418 | |
| 1419 | if (!drbd_crypto_is_hash(crypto_hash_tfm(integrity_w_tfm))) { |
| 1420 | retcode=ERR_INTEGRITY_ALG_ND; |
| 1421 | goto fail; |
| 1422 | } |
| 1423 | |
| 1424 | integrity_r_tfm = crypto_alloc_hash(new_conf->integrity_alg, 0, CRYPTO_ALG_ASYNC); |
| 1425 | if (IS_ERR(integrity_r_tfm)) { |
| 1426 | integrity_r_tfm = NULL; |
| 1427 | retcode=ERR_INTEGRITY_ALG; |
| 1428 | goto fail; |
| 1429 | } |
| 1430 | } |
| 1431 | |
| 1432 | ns = new_conf->max_epoch_size/8; |
| 1433 | if (mdev->tl_hash_s != ns) { |
| 1434 | new_tl_hash = kzalloc(ns*sizeof(void *), GFP_KERNEL); |
| 1435 | if (!new_tl_hash) { |
| 1436 | retcode = ERR_NOMEM; |
| 1437 | goto fail; |
| 1438 | } |
| 1439 | } |
| 1440 | |
| 1441 | ns = new_conf->max_buffers/8; |
| 1442 | if (new_conf->two_primaries && (mdev->ee_hash_s != ns)) { |
| 1443 | new_ee_hash = kzalloc(ns*sizeof(void *), GFP_KERNEL); |
| 1444 | if (!new_ee_hash) { |
| 1445 | retcode = ERR_NOMEM; |
| 1446 | goto fail; |
| 1447 | } |
| 1448 | } |
| 1449 | |
| 1450 | ((char *)new_conf->shared_secret)[SHARED_SECRET_MAX-1] = 0; |
| 1451 | |
| 1452 | if (integrity_w_tfm) { |
| 1453 | i = crypto_hash_digestsize(integrity_w_tfm); |
| 1454 | int_dig_out = kmalloc(i, GFP_KERNEL); |
| 1455 | if (!int_dig_out) { |
| 1456 | retcode = ERR_NOMEM; |
| 1457 | goto fail; |
| 1458 | } |
| 1459 | int_dig_in = kmalloc(i, GFP_KERNEL); |
| 1460 | if (!int_dig_in) { |
| 1461 | retcode = ERR_NOMEM; |
| 1462 | goto fail; |
| 1463 | } |
| 1464 | int_dig_vv = kmalloc(i, GFP_KERNEL); |
| 1465 | if (!int_dig_vv) { |
| 1466 | retcode = ERR_NOMEM; |
| 1467 | goto fail; |
| 1468 | } |
| 1469 | } |
| 1470 | |
| 1471 | if (!mdev->bitmap) { |
| 1472 | if(drbd_bm_init(mdev)) { |
| 1473 | retcode = ERR_NOMEM; |
| 1474 | goto fail; |
| 1475 | } |
| 1476 | } |
| 1477 | |
Philipp Reisner | f70b3511 | 2010-06-24 14:34:40 +0200 | [diff] [blame] | 1478 | drbd_flush_workqueue(mdev); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1479 | spin_lock_irq(&mdev->req_lock); |
| 1480 | if (mdev->net_conf != NULL) { |
| 1481 | retcode = ERR_NET_CONFIGURED; |
| 1482 | spin_unlock_irq(&mdev->req_lock); |
| 1483 | goto fail; |
| 1484 | } |
| 1485 | mdev->net_conf = new_conf; |
| 1486 | |
| 1487 | mdev->send_cnt = 0; |
| 1488 | mdev->recv_cnt = 0; |
| 1489 | |
| 1490 | if (new_tl_hash) { |
| 1491 | kfree(mdev->tl_hash); |
| 1492 | mdev->tl_hash_s = mdev->net_conf->max_epoch_size/8; |
| 1493 | mdev->tl_hash = new_tl_hash; |
| 1494 | } |
| 1495 | |
| 1496 | if (new_ee_hash) { |
| 1497 | kfree(mdev->ee_hash); |
| 1498 | mdev->ee_hash_s = mdev->net_conf->max_buffers/8; |
| 1499 | mdev->ee_hash = new_ee_hash; |
| 1500 | } |
| 1501 | |
| 1502 | crypto_free_hash(mdev->cram_hmac_tfm); |
| 1503 | mdev->cram_hmac_tfm = tfm; |
| 1504 | |
| 1505 | crypto_free_hash(mdev->integrity_w_tfm); |
| 1506 | mdev->integrity_w_tfm = integrity_w_tfm; |
| 1507 | |
| 1508 | crypto_free_hash(mdev->integrity_r_tfm); |
| 1509 | mdev->integrity_r_tfm = integrity_r_tfm; |
| 1510 | |
| 1511 | kfree(mdev->int_dig_out); |
| 1512 | kfree(mdev->int_dig_in); |
| 1513 | kfree(mdev->int_dig_vv); |
| 1514 | mdev->int_dig_out=int_dig_out; |
| 1515 | mdev->int_dig_in=int_dig_in; |
| 1516 | mdev->int_dig_vv=int_dig_vv; |
Philipp Reisner | f70b3511 | 2010-06-24 14:34:40 +0200 | [diff] [blame] | 1517 | retcode = _drbd_set_state(_NS(mdev, conn, C_UNCONNECTED), CS_VERBOSE, NULL); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1518 | spin_unlock_irq(&mdev->req_lock); |
| 1519 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1520 | kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE); |
| 1521 | reply->ret_code = retcode; |
| 1522 | drbd_reconfig_done(mdev); |
| 1523 | return 0; |
| 1524 | |
| 1525 | fail: |
| 1526 | kfree(int_dig_out); |
| 1527 | kfree(int_dig_in); |
| 1528 | kfree(int_dig_vv); |
| 1529 | crypto_free_hash(tfm); |
| 1530 | crypto_free_hash(integrity_w_tfm); |
| 1531 | crypto_free_hash(integrity_r_tfm); |
| 1532 | kfree(new_tl_hash); |
| 1533 | kfree(new_ee_hash); |
| 1534 | kfree(new_conf); |
| 1535 | |
| 1536 | reply->ret_code = retcode; |
| 1537 | drbd_reconfig_done(mdev); |
| 1538 | return 0; |
| 1539 | } |
| 1540 | |
| 1541 | static int drbd_nl_disconnect(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp, |
| 1542 | struct drbd_nl_cfg_reply *reply) |
| 1543 | { |
| 1544 | int retcode; |
Philipp Reisner | 2561b9c | 2010-12-03 15:22:48 +0100 | [diff] [blame] | 1545 | struct disconnect dc; |
| 1546 | |
| 1547 | memset(&dc, 0, sizeof(struct disconnect)); |
| 1548 | if (!disconnect_from_tags(mdev, nlp->tag_list, &dc)) { |
| 1549 | retcode = ERR_MANDATORY_TAG; |
| 1550 | goto fail; |
| 1551 | } |
| 1552 | |
| 1553 | if (dc.force) { |
| 1554 | spin_lock_irq(&mdev->req_lock); |
| 1555 | if (mdev->state.conn >= C_WF_CONNECTION) |
| 1556 | _drbd_set_state(_NS(mdev, conn, C_DISCONNECTING), CS_HARD, NULL); |
| 1557 | spin_unlock_irq(&mdev->req_lock); |
| 1558 | goto done; |
| 1559 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1560 | |
| 1561 | retcode = _drbd_request_state(mdev, NS(conn, C_DISCONNECTING), CS_ORDERED); |
| 1562 | |
| 1563 | if (retcode == SS_NOTHING_TO_DO) |
| 1564 | goto done; |
| 1565 | else if (retcode == SS_ALREADY_STANDALONE) |
| 1566 | goto done; |
| 1567 | else if (retcode == SS_PRIMARY_NOP) { |
| 1568 | /* Our statche checking code wants to see the peer outdated. */ |
| 1569 | retcode = drbd_request_state(mdev, NS2(conn, C_DISCONNECTING, |
| 1570 | pdsk, D_OUTDATED)); |
| 1571 | } else if (retcode == SS_CW_FAILED_BY_PEER) { |
| 1572 | /* The peer probably wants to see us outdated. */ |
| 1573 | retcode = _drbd_request_state(mdev, NS2(conn, C_DISCONNECTING, |
| 1574 | disk, D_OUTDATED), |
| 1575 | CS_ORDERED); |
| 1576 | if (retcode == SS_IS_DISKLESS || retcode == SS_LOWER_THAN_OUTDATED) { |
| 1577 | drbd_force_state(mdev, NS(conn, C_DISCONNECTING)); |
| 1578 | retcode = SS_SUCCESS; |
| 1579 | } |
| 1580 | } |
| 1581 | |
| 1582 | if (retcode < SS_SUCCESS) |
| 1583 | goto fail; |
| 1584 | |
| 1585 | if (wait_event_interruptible(mdev->state_wait, |
| 1586 | mdev->state.conn != C_DISCONNECTING)) { |
| 1587 | /* Do not test for mdev->state.conn == C_STANDALONE, since |
| 1588 | someone else might connect us in the mean time! */ |
| 1589 | retcode = ERR_INTR; |
| 1590 | goto fail; |
| 1591 | } |
| 1592 | |
| 1593 | done: |
| 1594 | retcode = NO_ERROR; |
| 1595 | fail: |
| 1596 | drbd_md_sync(mdev); |
| 1597 | reply->ret_code = retcode; |
| 1598 | return 0; |
| 1599 | } |
| 1600 | |
| 1601 | void resync_after_online_grow(struct drbd_conf *mdev) |
| 1602 | { |
| 1603 | int iass; /* I am sync source */ |
| 1604 | |
| 1605 | dev_info(DEV, "Resync of new storage after online grow\n"); |
| 1606 | if (mdev->state.role != mdev->state.peer) |
| 1607 | iass = (mdev->state.role == R_PRIMARY); |
| 1608 | else |
| 1609 | iass = test_bit(DISCARD_CONCURRENT, &mdev->flags); |
| 1610 | |
| 1611 | if (iass) |
| 1612 | drbd_start_resync(mdev, C_SYNC_SOURCE); |
| 1613 | else |
| 1614 | _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE + CS_SERIALIZE); |
| 1615 | } |
| 1616 | |
| 1617 | static int drbd_nl_resize(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp, |
| 1618 | struct drbd_nl_cfg_reply *reply) |
| 1619 | { |
| 1620 | struct resize rs; |
| 1621 | int retcode = NO_ERROR; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1622 | enum determine_dev_size dd; |
Philipp Reisner | 6495d2c | 2010-03-24 16:07:04 +0100 | [diff] [blame] | 1623 | enum dds_flags ddsf; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1624 | |
| 1625 | memset(&rs, 0, sizeof(struct resize)); |
| 1626 | if (!resize_from_tags(mdev, nlp->tag_list, &rs)) { |
| 1627 | retcode = ERR_MANDATORY_TAG; |
| 1628 | goto fail; |
| 1629 | } |
| 1630 | |
| 1631 | if (mdev->state.conn > C_CONNECTED) { |
| 1632 | retcode = ERR_RESIZE_RESYNC; |
| 1633 | goto fail; |
| 1634 | } |
| 1635 | |
| 1636 | if (mdev->state.role == R_SECONDARY && |
| 1637 | mdev->state.peer == R_SECONDARY) { |
| 1638 | retcode = ERR_NO_PRIMARY; |
| 1639 | goto fail; |
| 1640 | } |
| 1641 | |
| 1642 | if (!get_ldev(mdev)) { |
| 1643 | retcode = ERR_NO_DISK; |
| 1644 | goto fail; |
| 1645 | } |
| 1646 | |
Philipp Reisner | 6495d2c | 2010-03-24 16:07:04 +0100 | [diff] [blame] | 1647 | if (rs.no_resync && mdev->agreed_pro_version < 93) { |
| 1648 | retcode = ERR_NEED_APV_93; |
| 1649 | goto fail; |
| 1650 | } |
| 1651 | |
Philipp Reisner | 087c249 | 2010-03-26 13:49:56 +0100 | [diff] [blame] | 1652 | if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev)) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1653 | mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1654 | |
| 1655 | mdev->ldev->dc.disk_size = (sector_t)rs.resize_size; |
Philipp Reisner | 6495d2c | 2010-03-24 16:07:04 +0100 | [diff] [blame] | 1656 | ddsf = (rs.resize_force ? DDSF_FORCED : 0) | (rs.no_resync ? DDSF_NO_RESYNC : 0); |
| 1657 | dd = drbd_determin_dev_size(mdev, ddsf); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1658 | drbd_md_sync(mdev); |
| 1659 | put_ldev(mdev); |
| 1660 | if (dd == dev_size_error) { |
| 1661 | retcode = ERR_NOMEM_BITMAP; |
| 1662 | goto fail; |
| 1663 | } |
| 1664 | |
Philipp Reisner | 087c249 | 2010-03-26 13:49:56 +0100 | [diff] [blame] | 1665 | if (mdev->state.conn == C_CONNECTED) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1666 | if (dd == grew) |
| 1667 | set_bit(RESIZE_PENDING, &mdev->flags); |
| 1668 | |
| 1669 | drbd_send_uuids(mdev); |
Philipp Reisner | 6495d2c | 2010-03-24 16:07:04 +0100 | [diff] [blame] | 1670 | drbd_send_sizes(mdev, 1, ddsf); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1671 | } |
| 1672 | |
| 1673 | fail: |
| 1674 | reply->ret_code = retcode; |
| 1675 | return 0; |
| 1676 | } |
| 1677 | |
| 1678 | static int drbd_nl_syncer_conf(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp, |
| 1679 | struct drbd_nl_cfg_reply *reply) |
| 1680 | { |
| 1681 | int retcode = NO_ERROR; |
| 1682 | int err; |
| 1683 | int ovr; /* online verify running */ |
| 1684 | int rsr; /* re-sync running */ |
| 1685 | struct crypto_hash *verify_tfm = NULL; |
| 1686 | struct crypto_hash *csums_tfm = NULL; |
| 1687 | struct syncer_conf sc; |
| 1688 | cpumask_var_t new_cpu_mask; |
Philipp Reisner | 778f271 | 2010-07-06 11:14:00 +0200 | [diff] [blame] | 1689 | int *rs_plan_s = NULL; |
| 1690 | int fifo_size; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1691 | |
| 1692 | if (!zalloc_cpumask_var(&new_cpu_mask, GFP_KERNEL)) { |
| 1693 | retcode = ERR_NOMEM; |
| 1694 | goto fail; |
| 1695 | } |
| 1696 | |
| 1697 | if (nlp->flags & DRBD_NL_SET_DEFAULTS) { |
| 1698 | memset(&sc, 0, sizeof(struct syncer_conf)); |
| 1699 | sc.rate = DRBD_RATE_DEF; |
| 1700 | sc.after = DRBD_AFTER_DEF; |
| 1701 | sc.al_extents = DRBD_AL_EXTENTS_DEF; |
Philipp Reisner | 265be2d | 2010-05-31 10:14:17 +0200 | [diff] [blame] | 1702 | sc.on_no_data = DRBD_ON_NO_DATA_DEF; |
Philipp Reisner | 9a31d71 | 2010-07-05 13:42:03 +0200 | [diff] [blame] | 1703 | sc.c_plan_ahead = DRBD_C_PLAN_AHEAD_DEF; |
| 1704 | sc.c_delay_target = DRBD_C_DELAY_TARGET_DEF; |
| 1705 | sc.c_fill_target = DRBD_C_FILL_TARGET_DEF; |
Lars Ellenberg | 0f0601f | 2010-08-11 23:40:24 +0200 | [diff] [blame] | 1706 | sc.c_max_rate = DRBD_C_MAX_RATE_DEF; |
| 1707 | sc.c_min_rate = DRBD_C_MIN_RATE_DEF; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1708 | } else |
| 1709 | memcpy(&sc, &mdev->sync_conf, sizeof(struct syncer_conf)); |
| 1710 | |
| 1711 | if (!syncer_conf_from_tags(mdev, nlp->tag_list, &sc)) { |
| 1712 | retcode = ERR_MANDATORY_TAG; |
| 1713 | goto fail; |
| 1714 | } |
| 1715 | |
| 1716 | /* re-sync running */ |
| 1717 | rsr = ( mdev->state.conn == C_SYNC_SOURCE || |
| 1718 | mdev->state.conn == C_SYNC_TARGET || |
| 1719 | mdev->state.conn == C_PAUSED_SYNC_S || |
| 1720 | mdev->state.conn == C_PAUSED_SYNC_T ); |
| 1721 | |
| 1722 | if (rsr && strcmp(sc.csums_alg, mdev->sync_conf.csums_alg)) { |
| 1723 | retcode = ERR_CSUMS_RESYNC_RUNNING; |
| 1724 | goto fail; |
| 1725 | } |
| 1726 | |
| 1727 | if (!rsr && sc.csums_alg[0]) { |
| 1728 | csums_tfm = crypto_alloc_hash(sc.csums_alg, 0, CRYPTO_ALG_ASYNC); |
| 1729 | if (IS_ERR(csums_tfm)) { |
| 1730 | csums_tfm = NULL; |
| 1731 | retcode = ERR_CSUMS_ALG; |
| 1732 | goto fail; |
| 1733 | } |
| 1734 | |
| 1735 | if (!drbd_crypto_is_hash(crypto_hash_tfm(csums_tfm))) { |
| 1736 | retcode = ERR_CSUMS_ALG_ND; |
| 1737 | goto fail; |
| 1738 | } |
| 1739 | } |
| 1740 | |
| 1741 | /* online verify running */ |
| 1742 | ovr = (mdev->state.conn == C_VERIFY_S || mdev->state.conn == C_VERIFY_T); |
| 1743 | |
| 1744 | if (ovr) { |
| 1745 | if (strcmp(sc.verify_alg, mdev->sync_conf.verify_alg)) { |
| 1746 | retcode = ERR_VERIFY_RUNNING; |
| 1747 | goto fail; |
| 1748 | } |
| 1749 | } |
| 1750 | |
| 1751 | if (!ovr && sc.verify_alg[0]) { |
| 1752 | verify_tfm = crypto_alloc_hash(sc.verify_alg, 0, CRYPTO_ALG_ASYNC); |
| 1753 | if (IS_ERR(verify_tfm)) { |
| 1754 | verify_tfm = NULL; |
| 1755 | retcode = ERR_VERIFY_ALG; |
| 1756 | goto fail; |
| 1757 | } |
| 1758 | |
| 1759 | if (!drbd_crypto_is_hash(crypto_hash_tfm(verify_tfm))) { |
| 1760 | retcode = ERR_VERIFY_ALG_ND; |
| 1761 | goto fail; |
| 1762 | } |
| 1763 | } |
| 1764 | |
| 1765 | /* silently ignore cpu mask on UP kernel */ |
| 1766 | if (nr_cpu_ids > 1 && sc.cpu_mask[0] != 0) { |
| 1767 | err = __bitmap_parse(sc.cpu_mask, 32, 0, |
| 1768 | cpumask_bits(new_cpu_mask), nr_cpu_ids); |
| 1769 | if (err) { |
| 1770 | dev_warn(DEV, "__bitmap_parse() failed with %d\n", err); |
| 1771 | retcode = ERR_CPU_MASK_PARSE; |
| 1772 | goto fail; |
| 1773 | } |
| 1774 | } |
| 1775 | |
| 1776 | ERR_IF (sc.rate < 1) sc.rate = 1; |
| 1777 | ERR_IF (sc.al_extents < 7) sc.al_extents = 127; /* arbitrary minimum */ |
| 1778 | #define AL_MAX ((MD_AL_MAX_SIZE-1) * AL_EXTENTS_PT) |
| 1779 | if (sc.al_extents > AL_MAX) { |
| 1780 | dev_err(DEV, "sc.al_extents > %d\n", AL_MAX); |
| 1781 | sc.al_extents = AL_MAX; |
| 1782 | } |
| 1783 | #undef AL_MAX |
| 1784 | |
Lars Ellenberg | ef50a3e | 2010-09-01 14:39:30 +0200 | [diff] [blame] | 1785 | /* to avoid spurious errors when configuring minors before configuring |
| 1786 | * the minors they depend on: if necessary, first create the minor we |
| 1787 | * depend on */ |
| 1788 | if (sc.after >= 0) |
| 1789 | ensure_mdev(sc.after, 1); |
| 1790 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1791 | /* most sanity checks done, try to assign the new sync-after |
| 1792 | * dependency. need to hold the global lock in there, |
| 1793 | * to avoid a race in the dependency loop check. */ |
| 1794 | retcode = drbd_alter_sa(mdev, sc.after); |
| 1795 | if (retcode != NO_ERROR) |
| 1796 | goto fail; |
| 1797 | |
Philipp Reisner | 778f271 | 2010-07-06 11:14:00 +0200 | [diff] [blame] | 1798 | fifo_size = (sc.c_plan_ahead * 10 * SLEEP_TIME) / HZ; |
| 1799 | if (fifo_size != mdev->rs_plan_s.size && fifo_size > 0) { |
| 1800 | rs_plan_s = kzalloc(sizeof(int) * fifo_size, GFP_KERNEL); |
| 1801 | if (!rs_plan_s) { |
| 1802 | dev_err(DEV, "kmalloc of fifo_buffer failed"); |
| 1803 | retcode = ERR_NOMEM; |
| 1804 | goto fail; |
| 1805 | } |
| 1806 | } |
| 1807 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1808 | /* ok, assign the rest of it as well. |
| 1809 | * lock against receive_SyncParam() */ |
| 1810 | spin_lock(&mdev->peer_seq_lock); |
| 1811 | mdev->sync_conf = sc; |
| 1812 | |
| 1813 | if (!rsr) { |
| 1814 | crypto_free_hash(mdev->csums_tfm); |
| 1815 | mdev->csums_tfm = csums_tfm; |
| 1816 | csums_tfm = NULL; |
| 1817 | } |
| 1818 | |
| 1819 | if (!ovr) { |
| 1820 | crypto_free_hash(mdev->verify_tfm); |
| 1821 | mdev->verify_tfm = verify_tfm; |
| 1822 | verify_tfm = NULL; |
| 1823 | } |
Philipp Reisner | 778f271 | 2010-07-06 11:14:00 +0200 | [diff] [blame] | 1824 | |
| 1825 | if (fifo_size != mdev->rs_plan_s.size) { |
| 1826 | kfree(mdev->rs_plan_s.values); |
| 1827 | mdev->rs_plan_s.values = rs_plan_s; |
| 1828 | mdev->rs_plan_s.size = fifo_size; |
| 1829 | mdev->rs_planed = 0; |
| 1830 | rs_plan_s = NULL; |
| 1831 | } |
| 1832 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1833 | spin_unlock(&mdev->peer_seq_lock); |
| 1834 | |
| 1835 | if (get_ldev(mdev)) { |
| 1836 | wait_event(mdev->al_wait, lc_try_lock(mdev->act_log)); |
| 1837 | drbd_al_shrink(mdev); |
| 1838 | err = drbd_check_al_size(mdev); |
| 1839 | lc_unlock(mdev->act_log); |
| 1840 | wake_up(&mdev->al_wait); |
| 1841 | |
| 1842 | put_ldev(mdev); |
| 1843 | drbd_md_sync(mdev); |
| 1844 | |
| 1845 | if (err) { |
| 1846 | retcode = ERR_NOMEM; |
| 1847 | goto fail; |
| 1848 | } |
| 1849 | } |
| 1850 | |
| 1851 | if (mdev->state.conn >= C_CONNECTED) |
| 1852 | drbd_send_sync_param(mdev, &sc); |
| 1853 | |
| 1854 | if (!cpumask_equal(mdev->cpu_mask, new_cpu_mask)) { |
| 1855 | cpumask_copy(mdev->cpu_mask, new_cpu_mask); |
| 1856 | drbd_calc_cpu_mask(mdev); |
| 1857 | mdev->receiver.reset_cpu_mask = 1; |
| 1858 | mdev->asender.reset_cpu_mask = 1; |
| 1859 | mdev->worker.reset_cpu_mask = 1; |
| 1860 | } |
| 1861 | |
| 1862 | kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE); |
| 1863 | fail: |
Philipp Reisner | 778f271 | 2010-07-06 11:14:00 +0200 | [diff] [blame] | 1864 | kfree(rs_plan_s); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1865 | free_cpumask_var(new_cpu_mask); |
| 1866 | crypto_free_hash(csums_tfm); |
| 1867 | crypto_free_hash(verify_tfm); |
| 1868 | reply->ret_code = retcode; |
| 1869 | return 0; |
| 1870 | } |
| 1871 | |
| 1872 | static int drbd_nl_invalidate(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp, |
| 1873 | struct drbd_nl_cfg_reply *reply) |
| 1874 | { |
| 1875 | int retcode; |
| 1876 | |
| 1877 | retcode = _drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T), CS_ORDERED); |
| 1878 | |
| 1879 | if (retcode < SS_SUCCESS && retcode != SS_NEED_CONNECTION) |
| 1880 | retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T)); |
| 1881 | |
| 1882 | while (retcode == SS_NEED_CONNECTION) { |
| 1883 | spin_lock_irq(&mdev->req_lock); |
| 1884 | if (mdev->state.conn < C_CONNECTED) |
| 1885 | retcode = _drbd_set_state(_NS(mdev, disk, D_INCONSISTENT), CS_VERBOSE, NULL); |
| 1886 | spin_unlock_irq(&mdev->req_lock); |
| 1887 | |
| 1888 | if (retcode != SS_NEED_CONNECTION) |
| 1889 | break; |
| 1890 | |
| 1891 | retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T)); |
| 1892 | } |
| 1893 | |
| 1894 | reply->ret_code = retcode; |
| 1895 | return 0; |
| 1896 | } |
| 1897 | |
Philipp Reisner | 0778286 | 2010-08-31 12:00:50 +0200 | [diff] [blame] | 1898 | static int drbd_bmio_set_susp_al(struct drbd_conf *mdev) |
| 1899 | { |
| 1900 | int rv; |
| 1901 | |
| 1902 | rv = drbd_bmio_set_n_write(mdev); |
| 1903 | drbd_suspend_al(mdev); |
| 1904 | return rv; |
| 1905 | } |
| 1906 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1907 | static int drbd_nl_invalidate_peer(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp, |
| 1908 | struct drbd_nl_cfg_reply *reply) |
| 1909 | { |
Philipp Reisner | 0778286 | 2010-08-31 12:00:50 +0200 | [diff] [blame] | 1910 | int retcode; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1911 | |
Philipp Reisner | 0778286 | 2010-08-31 12:00:50 +0200 | [diff] [blame] | 1912 | retcode = _drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_S), CS_ORDERED); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1913 | |
Philipp Reisner | 0778286 | 2010-08-31 12:00:50 +0200 | [diff] [blame] | 1914 | if (retcode < SS_SUCCESS) { |
| 1915 | if (retcode == SS_NEED_CONNECTION && mdev->state.role == R_PRIMARY) { |
| 1916 | /* The peer will get a resync upon connect anyways. Just make that |
| 1917 | into a full resync. */ |
| 1918 | retcode = drbd_request_state(mdev, NS(pdsk, D_INCONSISTENT)); |
| 1919 | if (retcode >= SS_SUCCESS) { |
| 1920 | /* open coded drbd_bitmap_io() */ |
| 1921 | if (drbd_bitmap_io(mdev, &drbd_bmio_set_susp_al, |
| 1922 | "set_n_write from invalidate_peer")) |
| 1923 | retcode = ERR_IO_MD_DISK; |
| 1924 | } |
| 1925 | } else |
| 1926 | retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_S)); |
| 1927 | } |
| 1928 | |
| 1929 | reply->ret_code = retcode; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1930 | return 0; |
| 1931 | } |
| 1932 | |
| 1933 | static int drbd_nl_pause_sync(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp, |
| 1934 | struct drbd_nl_cfg_reply *reply) |
| 1935 | { |
| 1936 | int retcode = NO_ERROR; |
| 1937 | |
| 1938 | if (drbd_request_state(mdev, NS(user_isp, 1)) == SS_NOTHING_TO_DO) |
| 1939 | retcode = ERR_PAUSE_IS_SET; |
| 1940 | |
| 1941 | reply->ret_code = retcode; |
| 1942 | return 0; |
| 1943 | } |
| 1944 | |
| 1945 | static int drbd_nl_resume_sync(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp, |
| 1946 | struct drbd_nl_cfg_reply *reply) |
| 1947 | { |
| 1948 | int retcode = NO_ERROR; |
| 1949 | |
| 1950 | if (drbd_request_state(mdev, NS(user_isp, 0)) == SS_NOTHING_TO_DO) |
| 1951 | retcode = ERR_PAUSE_IS_CLEAR; |
| 1952 | |
| 1953 | reply->ret_code = retcode; |
| 1954 | return 0; |
| 1955 | } |
| 1956 | |
| 1957 | static int drbd_nl_suspend_io(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp, |
| 1958 | struct drbd_nl_cfg_reply *reply) |
| 1959 | { |
| 1960 | reply->ret_code = drbd_request_state(mdev, NS(susp, 1)); |
| 1961 | |
| 1962 | return 0; |
| 1963 | } |
| 1964 | |
| 1965 | static int drbd_nl_resume_io(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp, |
| 1966 | struct drbd_nl_cfg_reply *reply) |
| 1967 | { |
Philipp Reisner | 43a5182 | 2010-06-11 11:26:34 +0200 | [diff] [blame] | 1968 | if (test_bit(NEW_CUR_UUID, &mdev->flags)) { |
| 1969 | drbd_uuid_new_current(mdev); |
| 1970 | clear_bit(NEW_CUR_UUID, &mdev->flags); |
Philipp Reisner | 43a5182 | 2010-06-11 11:26:34 +0200 | [diff] [blame] | 1971 | } |
Philipp Reisner | 265be2d | 2010-05-31 10:14:17 +0200 | [diff] [blame] | 1972 | drbd_suspend_io(mdev); |
Philipp Reisner | fb22c40 | 2010-09-08 23:20:21 +0200 | [diff] [blame] | 1973 | reply->ret_code = drbd_request_state(mdev, NS3(susp, 0, susp_nod, 0, susp_fen, 0)); |
Philipp Reisner | 265be2d | 2010-05-31 10:14:17 +0200 | [diff] [blame] | 1974 | if (reply->ret_code == SS_SUCCESS) { |
| 1975 | if (mdev->state.conn < C_CONNECTED) |
| 1976 | tl_clear(mdev); |
| 1977 | if (mdev->state.disk == D_DISKLESS || mdev->state.disk == D_FAILED) |
| 1978 | tl_restart(mdev, fail_frozen_disk_io); |
| 1979 | } |
| 1980 | drbd_resume_io(mdev); |
| 1981 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1982 | return 0; |
| 1983 | } |
| 1984 | |
| 1985 | static int drbd_nl_outdate(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp, |
| 1986 | struct drbd_nl_cfg_reply *reply) |
| 1987 | { |
| 1988 | reply->ret_code = drbd_request_state(mdev, NS(disk, D_OUTDATED)); |
| 1989 | return 0; |
| 1990 | } |
| 1991 | |
| 1992 | static int drbd_nl_get_config(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp, |
| 1993 | struct drbd_nl_cfg_reply *reply) |
| 1994 | { |
| 1995 | unsigned short *tl; |
| 1996 | |
| 1997 | tl = reply->tag_list; |
| 1998 | |
| 1999 | if (get_ldev(mdev)) { |
| 2000 | tl = disk_conf_to_tags(mdev, &mdev->ldev->dc, tl); |
| 2001 | put_ldev(mdev); |
| 2002 | } |
| 2003 | |
| 2004 | if (get_net_conf(mdev)) { |
| 2005 | tl = net_conf_to_tags(mdev, mdev->net_conf, tl); |
| 2006 | put_net_conf(mdev); |
| 2007 | } |
| 2008 | tl = syncer_conf_to_tags(mdev, &mdev->sync_conf, tl); |
| 2009 | |
| 2010 | put_unaligned(TT_END, tl++); /* Close the tag list */ |
| 2011 | |
| 2012 | return (int)((char *)tl - (char *)reply->tag_list); |
| 2013 | } |
| 2014 | |
| 2015 | static int drbd_nl_get_state(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp, |
| 2016 | struct drbd_nl_cfg_reply *reply) |
| 2017 | { |
| 2018 | unsigned short *tl = reply->tag_list; |
| 2019 | union drbd_state s = mdev->state; |
| 2020 | unsigned long rs_left; |
| 2021 | unsigned int res; |
| 2022 | |
| 2023 | tl = get_state_to_tags(mdev, (struct get_state *)&s, tl); |
| 2024 | |
| 2025 | /* no local ref, no bitmap, no syncer progress. */ |
| 2026 | if (s.conn >= C_SYNC_SOURCE && s.conn <= C_PAUSED_SYNC_T) { |
| 2027 | if (get_ldev(mdev)) { |
| 2028 | drbd_get_syncer_progress(mdev, &rs_left, &res); |
| 2029 | tl = tl_add_int(tl, T_sync_progress, &res); |
| 2030 | put_ldev(mdev); |
| 2031 | } |
| 2032 | } |
| 2033 | put_unaligned(TT_END, tl++); /* Close the tag list */ |
| 2034 | |
| 2035 | return (int)((char *)tl - (char *)reply->tag_list); |
| 2036 | } |
| 2037 | |
| 2038 | static int drbd_nl_get_uuids(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp, |
| 2039 | struct drbd_nl_cfg_reply *reply) |
| 2040 | { |
| 2041 | unsigned short *tl; |
| 2042 | |
| 2043 | tl = reply->tag_list; |
| 2044 | |
| 2045 | if (get_ldev(mdev)) { |
| 2046 | tl = tl_add_blob(tl, T_uuids, mdev->ldev->md.uuid, UI_SIZE*sizeof(u64)); |
| 2047 | tl = tl_add_int(tl, T_uuids_flags, &mdev->ldev->md.flags); |
| 2048 | put_ldev(mdev); |
| 2049 | } |
| 2050 | put_unaligned(TT_END, tl++); /* Close the tag list */ |
| 2051 | |
| 2052 | return (int)((char *)tl - (char *)reply->tag_list); |
| 2053 | } |
| 2054 | |
| 2055 | /** |
| 2056 | * drbd_nl_get_timeout_flag() - Used by drbdsetup to find out which timeout value to use |
| 2057 | * @mdev: DRBD device. |
| 2058 | * @nlp: Netlink/connector packet from drbdsetup |
| 2059 | * @reply: Reply packet for drbdsetup |
| 2060 | */ |
| 2061 | static int drbd_nl_get_timeout_flag(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp, |
| 2062 | struct drbd_nl_cfg_reply *reply) |
| 2063 | { |
| 2064 | unsigned short *tl; |
| 2065 | char rv; |
| 2066 | |
| 2067 | tl = reply->tag_list; |
| 2068 | |
| 2069 | rv = mdev->state.pdsk == D_OUTDATED ? UT_PEER_OUTDATED : |
| 2070 | test_bit(USE_DEGR_WFC_T, &mdev->flags) ? UT_DEGRADED : UT_DEFAULT; |
| 2071 | |
| 2072 | tl = tl_add_blob(tl, T_use_degraded, &rv, sizeof(rv)); |
| 2073 | put_unaligned(TT_END, tl++); /* Close the tag list */ |
| 2074 | |
| 2075 | return (int)((char *)tl - (char *)reply->tag_list); |
| 2076 | } |
| 2077 | |
| 2078 | static int drbd_nl_start_ov(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp, |
| 2079 | struct drbd_nl_cfg_reply *reply) |
| 2080 | { |
| 2081 | /* default to resume from last known position, if possible */ |
| 2082 | struct start_ov args = |
| 2083 | { .start_sector = mdev->ov_start_sector }; |
| 2084 | |
| 2085 | if (!start_ov_from_tags(mdev, nlp->tag_list, &args)) { |
| 2086 | reply->ret_code = ERR_MANDATORY_TAG; |
| 2087 | return 0; |
| 2088 | } |
| 2089 | /* w_make_ov_request expects position to be aligned */ |
| 2090 | mdev->ov_start_sector = args.start_sector & ~BM_SECT_PER_BIT; |
| 2091 | reply->ret_code = drbd_request_state(mdev,NS(conn,C_VERIFY_S)); |
| 2092 | return 0; |
| 2093 | } |
| 2094 | |
| 2095 | |
| 2096 | static int drbd_nl_new_c_uuid(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp, |
| 2097 | struct drbd_nl_cfg_reply *reply) |
| 2098 | { |
| 2099 | int retcode = NO_ERROR; |
| 2100 | int skip_initial_sync = 0; |
| 2101 | int err; |
| 2102 | |
| 2103 | struct new_c_uuid args; |
| 2104 | |
| 2105 | memset(&args, 0, sizeof(struct new_c_uuid)); |
| 2106 | if (!new_c_uuid_from_tags(mdev, nlp->tag_list, &args)) { |
| 2107 | reply->ret_code = ERR_MANDATORY_TAG; |
| 2108 | return 0; |
| 2109 | } |
| 2110 | |
| 2111 | mutex_lock(&mdev->state_mutex); /* Protects us against serialized state changes. */ |
| 2112 | |
| 2113 | if (!get_ldev(mdev)) { |
| 2114 | retcode = ERR_NO_DISK; |
| 2115 | goto out; |
| 2116 | } |
| 2117 | |
| 2118 | /* this is "skip initial sync", assume to be clean */ |
| 2119 | if (mdev->state.conn == C_CONNECTED && mdev->agreed_pro_version >= 90 && |
| 2120 | mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED && args.clear_bm) { |
| 2121 | dev_info(DEV, "Preparing to skip initial sync\n"); |
| 2122 | skip_initial_sync = 1; |
| 2123 | } else if (mdev->state.conn != C_STANDALONE) { |
| 2124 | retcode = ERR_CONNECTED; |
| 2125 | goto out_dec; |
| 2126 | } |
| 2127 | |
| 2128 | drbd_uuid_set(mdev, UI_BITMAP, 0); /* Rotate UI_BITMAP to History 1, etc... */ |
| 2129 | drbd_uuid_new_current(mdev); /* New current, previous to UI_BITMAP */ |
| 2130 | |
| 2131 | if (args.clear_bm) { |
| 2132 | err = drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write, "clear_n_write from new_c_uuid"); |
| 2133 | if (err) { |
| 2134 | dev_err(DEV, "Writing bitmap failed with %d\n",err); |
| 2135 | retcode = ERR_IO_MD_DISK; |
| 2136 | } |
| 2137 | if (skip_initial_sync) { |
| 2138 | drbd_send_uuids_skip_initial_sync(mdev); |
| 2139 | _drbd_uuid_set(mdev, UI_BITMAP, 0); |
| 2140 | spin_lock_irq(&mdev->req_lock); |
| 2141 | _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE), |
| 2142 | CS_VERBOSE, NULL); |
| 2143 | spin_unlock_irq(&mdev->req_lock); |
| 2144 | } |
| 2145 | } |
| 2146 | |
| 2147 | drbd_md_sync(mdev); |
| 2148 | out_dec: |
| 2149 | put_ldev(mdev); |
| 2150 | out: |
| 2151 | mutex_unlock(&mdev->state_mutex); |
| 2152 | |
| 2153 | reply->ret_code = retcode; |
| 2154 | return 0; |
| 2155 | } |
| 2156 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2157 | struct cn_handler_struct { |
| 2158 | int (*function)(struct drbd_conf *, |
| 2159 | struct drbd_nl_cfg_req *, |
| 2160 | struct drbd_nl_cfg_reply *); |
| 2161 | int reply_body_size; |
| 2162 | }; |
| 2163 | |
| 2164 | static struct cn_handler_struct cnd_table[] = { |
| 2165 | [ P_primary ] = { &drbd_nl_primary, 0 }, |
| 2166 | [ P_secondary ] = { &drbd_nl_secondary, 0 }, |
| 2167 | [ P_disk_conf ] = { &drbd_nl_disk_conf, 0 }, |
| 2168 | [ P_detach ] = { &drbd_nl_detach, 0 }, |
| 2169 | [ P_net_conf ] = { &drbd_nl_net_conf, 0 }, |
| 2170 | [ P_disconnect ] = { &drbd_nl_disconnect, 0 }, |
| 2171 | [ P_resize ] = { &drbd_nl_resize, 0 }, |
| 2172 | [ P_syncer_conf ] = { &drbd_nl_syncer_conf, 0 }, |
| 2173 | [ P_invalidate ] = { &drbd_nl_invalidate, 0 }, |
| 2174 | [ P_invalidate_peer ] = { &drbd_nl_invalidate_peer, 0 }, |
| 2175 | [ P_pause_sync ] = { &drbd_nl_pause_sync, 0 }, |
| 2176 | [ P_resume_sync ] = { &drbd_nl_resume_sync, 0 }, |
| 2177 | [ P_suspend_io ] = { &drbd_nl_suspend_io, 0 }, |
| 2178 | [ P_resume_io ] = { &drbd_nl_resume_io, 0 }, |
| 2179 | [ P_outdate ] = { &drbd_nl_outdate, 0 }, |
| 2180 | [ P_get_config ] = { &drbd_nl_get_config, |
| 2181 | sizeof(struct syncer_conf_tag_len_struct) + |
| 2182 | sizeof(struct disk_conf_tag_len_struct) + |
| 2183 | sizeof(struct net_conf_tag_len_struct) }, |
| 2184 | [ P_get_state ] = { &drbd_nl_get_state, |
| 2185 | sizeof(struct get_state_tag_len_struct) + |
| 2186 | sizeof(struct sync_progress_tag_len_struct) }, |
| 2187 | [ P_get_uuids ] = { &drbd_nl_get_uuids, |
| 2188 | sizeof(struct get_uuids_tag_len_struct) }, |
| 2189 | [ P_get_timeout_flag ] = { &drbd_nl_get_timeout_flag, |
| 2190 | sizeof(struct get_timeout_flag_tag_len_struct)}, |
| 2191 | [ P_start_ov ] = { &drbd_nl_start_ov, 0 }, |
| 2192 | [ P_new_c_uuid ] = { &drbd_nl_new_c_uuid, 0 }, |
| 2193 | }; |
| 2194 | |
Philipp Reisner | 9f5180e | 2009-10-06 09:30:14 +0200 | [diff] [blame] | 2195 | static void drbd_connector_callback(struct cn_msg *req, struct netlink_skb_parms *nsp) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2196 | { |
| 2197 | struct drbd_nl_cfg_req *nlp = (struct drbd_nl_cfg_req *)req->data; |
| 2198 | struct cn_handler_struct *cm; |
| 2199 | struct cn_msg *cn_reply; |
| 2200 | struct drbd_nl_cfg_reply *reply; |
| 2201 | struct drbd_conf *mdev; |
| 2202 | int retcode, rr; |
| 2203 | int reply_size = sizeof(struct cn_msg) |
| 2204 | + sizeof(struct drbd_nl_cfg_reply) |
| 2205 | + sizeof(short int); |
| 2206 | |
| 2207 | if (!try_module_get(THIS_MODULE)) { |
| 2208 | printk(KERN_ERR "drbd: try_module_get() failed!\n"); |
| 2209 | return; |
| 2210 | } |
| 2211 | |
Philipp Reisner | 9f5180e | 2009-10-06 09:30:14 +0200 | [diff] [blame] | 2212 | if (!cap_raised(nsp->eff_cap, CAP_SYS_ADMIN)) { |
| 2213 | retcode = ERR_PERM; |
| 2214 | goto fail; |
| 2215 | } |
| 2216 | |
Lars Ellenberg | ef50a3e | 2010-09-01 14:39:30 +0200 | [diff] [blame] | 2217 | mdev = ensure_mdev(nlp->drbd_minor, |
| 2218 | (nlp->flags & DRBD_NL_CREATE_DEVICE)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2219 | if (!mdev) { |
| 2220 | retcode = ERR_MINOR_INVALID; |
| 2221 | goto fail; |
| 2222 | } |
| 2223 | |
Lars Ellenberg | 42ff269 | 2010-11-24 10:11:14 +0100 | [diff] [blame] | 2224 | if (nlp->packet_type >= P_nl_after_last_packet || |
| 2225 | nlp->packet_type == P_return_code_only) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2226 | retcode = ERR_PACKET_NR; |
| 2227 | goto fail; |
| 2228 | } |
| 2229 | |
| 2230 | cm = cnd_table + nlp->packet_type; |
| 2231 | |
| 2232 | /* This may happen if packet number is 0: */ |
| 2233 | if (cm->function == NULL) { |
| 2234 | retcode = ERR_PACKET_NR; |
| 2235 | goto fail; |
| 2236 | } |
| 2237 | |
| 2238 | reply_size += cm->reply_body_size; |
| 2239 | |
| 2240 | /* allocation not in the IO path, cqueue thread context */ |
Lars Ellenberg | 3e3a776 | 2010-11-24 10:41:45 +0100 | [diff] [blame] | 2241 | cn_reply = kzalloc(reply_size, GFP_KERNEL); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2242 | if (!cn_reply) { |
| 2243 | retcode = ERR_NOMEM; |
| 2244 | goto fail; |
| 2245 | } |
| 2246 | reply = (struct drbd_nl_cfg_reply *) cn_reply->data; |
| 2247 | |
| 2248 | reply->packet_type = |
Lars Ellenberg | 42ff269 | 2010-11-24 10:11:14 +0100 | [diff] [blame] | 2249 | cm->reply_body_size ? nlp->packet_type : P_return_code_only; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2250 | reply->minor = nlp->drbd_minor; |
| 2251 | reply->ret_code = NO_ERROR; /* Might by modified by cm->function. */ |
| 2252 | /* reply->tag_list; might be modified by cm->function. */ |
| 2253 | |
| 2254 | rr = cm->function(mdev, nlp, reply); |
| 2255 | |
| 2256 | cn_reply->id = req->id; |
| 2257 | cn_reply->seq = req->seq; |
| 2258 | cn_reply->ack = req->ack + 1; |
| 2259 | cn_reply->len = sizeof(struct drbd_nl_cfg_reply) + rr; |
| 2260 | cn_reply->flags = 0; |
| 2261 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2262 | rr = cn_netlink_send(cn_reply, CN_IDX_DRBD, GFP_KERNEL); |
| 2263 | if (rr && rr != -ESRCH) |
| 2264 | printk(KERN_INFO "drbd: cn_netlink_send()=%d\n", rr); |
| 2265 | |
| 2266 | kfree(cn_reply); |
| 2267 | module_put(THIS_MODULE); |
| 2268 | return; |
| 2269 | fail: |
| 2270 | drbd_nl_send_reply(req, retcode); |
| 2271 | module_put(THIS_MODULE); |
| 2272 | } |
| 2273 | |
| 2274 | static atomic_t drbd_nl_seq = ATOMIC_INIT(2); /* two. */ |
| 2275 | |
| 2276 | static unsigned short * |
| 2277 | __tl_add_blob(unsigned short *tl, enum drbd_tags tag, const void *data, |
| 2278 | unsigned short len, int nul_terminated) |
| 2279 | { |
| 2280 | unsigned short l = tag_descriptions[tag_number(tag)].max_len; |
| 2281 | len = (len < l) ? len : l; |
| 2282 | put_unaligned(tag, tl++); |
| 2283 | put_unaligned(len, tl++); |
| 2284 | memcpy(tl, data, len); |
| 2285 | tl = (unsigned short*)((char*)tl + len); |
| 2286 | if (nul_terminated) |
| 2287 | *((char*)tl - 1) = 0; |
| 2288 | return tl; |
| 2289 | } |
| 2290 | |
| 2291 | static unsigned short * |
| 2292 | tl_add_blob(unsigned short *tl, enum drbd_tags tag, const void *data, int len) |
| 2293 | { |
| 2294 | return __tl_add_blob(tl, tag, data, len, 0); |
| 2295 | } |
| 2296 | |
| 2297 | static unsigned short * |
| 2298 | tl_add_str(unsigned short *tl, enum drbd_tags tag, const char *str) |
| 2299 | { |
| 2300 | return __tl_add_blob(tl, tag, str, strlen(str)+1, 0); |
| 2301 | } |
| 2302 | |
| 2303 | static unsigned short * |
| 2304 | tl_add_int(unsigned short *tl, enum drbd_tags tag, const void *val) |
| 2305 | { |
| 2306 | put_unaligned(tag, tl++); |
| 2307 | switch(tag_type(tag)) { |
| 2308 | case TT_INTEGER: |
| 2309 | put_unaligned(sizeof(int), tl++); |
| 2310 | put_unaligned(*(int *)val, (int *)tl); |
| 2311 | tl = (unsigned short*)((char*)tl+sizeof(int)); |
| 2312 | break; |
| 2313 | case TT_INT64: |
| 2314 | put_unaligned(sizeof(u64), tl++); |
| 2315 | put_unaligned(*(u64 *)val, (u64 *)tl); |
| 2316 | tl = (unsigned short*)((char*)tl+sizeof(u64)); |
| 2317 | break; |
| 2318 | default: |
| 2319 | /* someone did something stupid. */ |
| 2320 | ; |
| 2321 | } |
| 2322 | return tl; |
| 2323 | } |
| 2324 | |
| 2325 | void drbd_bcast_state(struct drbd_conf *mdev, union drbd_state state) |
| 2326 | { |
| 2327 | char buffer[sizeof(struct cn_msg)+ |
| 2328 | sizeof(struct drbd_nl_cfg_reply)+ |
| 2329 | sizeof(struct get_state_tag_len_struct)+ |
| 2330 | sizeof(short int)]; |
| 2331 | struct cn_msg *cn_reply = (struct cn_msg *) buffer; |
| 2332 | struct drbd_nl_cfg_reply *reply = |
| 2333 | (struct drbd_nl_cfg_reply *)cn_reply->data; |
| 2334 | unsigned short *tl = reply->tag_list; |
| 2335 | |
| 2336 | /* dev_warn(DEV, "drbd_bcast_state() got called\n"); */ |
| 2337 | |
| 2338 | tl = get_state_to_tags(mdev, (struct get_state *)&state, tl); |
| 2339 | |
| 2340 | put_unaligned(TT_END, tl++); /* Close the tag list */ |
| 2341 | |
| 2342 | cn_reply->id.idx = CN_IDX_DRBD; |
| 2343 | cn_reply->id.val = CN_VAL_DRBD; |
| 2344 | |
| 2345 | cn_reply->seq = atomic_add_return(1, &drbd_nl_seq); |
| 2346 | cn_reply->ack = 0; /* not used here. */ |
| 2347 | cn_reply->len = sizeof(struct drbd_nl_cfg_reply) + |
| 2348 | (int)((char *)tl - (char *)reply->tag_list); |
| 2349 | cn_reply->flags = 0; |
| 2350 | |
| 2351 | reply->packet_type = P_get_state; |
| 2352 | reply->minor = mdev_to_minor(mdev); |
| 2353 | reply->ret_code = NO_ERROR; |
| 2354 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2355 | cn_netlink_send(cn_reply, CN_IDX_DRBD, GFP_NOIO); |
| 2356 | } |
| 2357 | |
| 2358 | void drbd_bcast_ev_helper(struct drbd_conf *mdev, char *helper_name) |
| 2359 | { |
| 2360 | char buffer[sizeof(struct cn_msg)+ |
| 2361 | sizeof(struct drbd_nl_cfg_reply)+ |
| 2362 | sizeof(struct call_helper_tag_len_struct)+ |
| 2363 | sizeof(short int)]; |
| 2364 | struct cn_msg *cn_reply = (struct cn_msg *) buffer; |
| 2365 | struct drbd_nl_cfg_reply *reply = |
| 2366 | (struct drbd_nl_cfg_reply *)cn_reply->data; |
| 2367 | unsigned short *tl = reply->tag_list; |
| 2368 | |
| 2369 | /* dev_warn(DEV, "drbd_bcast_state() got called\n"); */ |
| 2370 | |
| 2371 | tl = tl_add_str(tl, T_helper, helper_name); |
| 2372 | put_unaligned(TT_END, tl++); /* Close the tag list */ |
| 2373 | |
| 2374 | cn_reply->id.idx = CN_IDX_DRBD; |
| 2375 | cn_reply->id.val = CN_VAL_DRBD; |
| 2376 | |
| 2377 | cn_reply->seq = atomic_add_return(1, &drbd_nl_seq); |
| 2378 | cn_reply->ack = 0; /* not used here. */ |
| 2379 | cn_reply->len = sizeof(struct drbd_nl_cfg_reply) + |
| 2380 | (int)((char *)tl - (char *)reply->tag_list); |
| 2381 | cn_reply->flags = 0; |
| 2382 | |
| 2383 | reply->packet_type = P_call_helper; |
| 2384 | reply->minor = mdev_to_minor(mdev); |
| 2385 | reply->ret_code = NO_ERROR; |
| 2386 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2387 | cn_netlink_send(cn_reply, CN_IDX_DRBD, GFP_NOIO); |
| 2388 | } |
| 2389 | |
| 2390 | void drbd_bcast_ee(struct drbd_conf *mdev, |
| 2391 | const char *reason, const int dgs, |
| 2392 | const char* seen_hash, const char* calc_hash, |
| 2393 | const struct drbd_epoch_entry* e) |
| 2394 | { |
| 2395 | struct cn_msg *cn_reply; |
| 2396 | struct drbd_nl_cfg_reply *reply; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2397 | unsigned short *tl; |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 2398 | struct page *page; |
| 2399 | unsigned len; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2400 | |
| 2401 | if (!e) |
| 2402 | return; |
| 2403 | if (!reason || !reason[0]) |
| 2404 | return; |
| 2405 | |
| 2406 | /* apparently we have to memcpy twice, first to prepare the data for the |
| 2407 | * struct cn_msg, then within cn_netlink_send from the cn_msg to the |
| 2408 | * netlink skb. */ |
| 2409 | /* receiver thread context, which is not in the writeout path (of this node), |
| 2410 | * but may be in the writeout path of the _other_ node. |
| 2411 | * GFP_NOIO to avoid potential "distributed deadlock". */ |
Lars Ellenberg | 3e3a776 | 2010-11-24 10:41:45 +0100 | [diff] [blame] | 2412 | cn_reply = kzalloc( |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2413 | sizeof(struct cn_msg)+ |
| 2414 | sizeof(struct drbd_nl_cfg_reply)+ |
| 2415 | sizeof(struct dump_ee_tag_len_struct)+ |
| 2416 | sizeof(short int), |
| 2417 | GFP_NOIO); |
| 2418 | |
| 2419 | if (!cn_reply) { |
| 2420 | dev_err(DEV, "could not kmalloc buffer for drbd_bcast_ee, sector %llu, size %u\n", |
| 2421 | (unsigned long long)e->sector, e->size); |
| 2422 | return; |
| 2423 | } |
| 2424 | |
| 2425 | reply = (struct drbd_nl_cfg_reply*)cn_reply->data; |
| 2426 | tl = reply->tag_list; |
| 2427 | |
| 2428 | tl = tl_add_str(tl, T_dump_ee_reason, reason); |
| 2429 | tl = tl_add_blob(tl, T_seen_digest, seen_hash, dgs); |
| 2430 | tl = tl_add_blob(tl, T_calc_digest, calc_hash, dgs); |
| 2431 | tl = tl_add_int(tl, T_ee_sector, &e->sector); |
| 2432 | tl = tl_add_int(tl, T_ee_block_id, &e->block_id); |
| 2433 | |
Lars Ellenberg | 3129b1b | 2010-11-11 10:47:05 +0100 | [diff] [blame] | 2434 | /* dump the first 32k */ |
| 2435 | len = min_t(unsigned, e->size, 32 << 10); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2436 | put_unaligned(T_ee_data, tl++); |
Lars Ellenberg | 3129b1b | 2010-11-11 10:47:05 +0100 | [diff] [blame] | 2437 | put_unaligned(len, tl++); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2438 | |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 2439 | page = e->pages; |
| 2440 | page_chain_for_each(page) { |
| 2441 | void *d = kmap_atomic(page, KM_USER0); |
| 2442 | unsigned l = min_t(unsigned, len, PAGE_SIZE); |
| 2443 | memcpy(tl, d, l); |
| 2444 | kunmap_atomic(d, KM_USER0); |
| 2445 | tl = (unsigned short*)((char*)tl + l); |
| 2446 | len -= l; |
Lars Ellenberg | 3129b1b | 2010-11-11 10:47:05 +0100 | [diff] [blame] | 2447 | if (len == 0) |
| 2448 | break; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2449 | } |
| 2450 | put_unaligned(TT_END, tl++); /* Close the tag list */ |
| 2451 | |
| 2452 | cn_reply->id.idx = CN_IDX_DRBD; |
| 2453 | cn_reply->id.val = CN_VAL_DRBD; |
| 2454 | |
| 2455 | cn_reply->seq = atomic_add_return(1,&drbd_nl_seq); |
| 2456 | cn_reply->ack = 0; // not used here. |
| 2457 | cn_reply->len = sizeof(struct drbd_nl_cfg_reply) + |
| 2458 | (int)((char*)tl - (char*)reply->tag_list); |
| 2459 | cn_reply->flags = 0; |
| 2460 | |
| 2461 | reply->packet_type = P_dump_ee; |
| 2462 | reply->minor = mdev_to_minor(mdev); |
| 2463 | reply->ret_code = NO_ERROR; |
| 2464 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2465 | cn_netlink_send(cn_reply, CN_IDX_DRBD, GFP_NOIO); |
| 2466 | kfree(cn_reply); |
| 2467 | } |
| 2468 | |
| 2469 | void drbd_bcast_sync_progress(struct drbd_conf *mdev) |
| 2470 | { |
| 2471 | char buffer[sizeof(struct cn_msg)+ |
| 2472 | sizeof(struct drbd_nl_cfg_reply)+ |
| 2473 | sizeof(struct sync_progress_tag_len_struct)+ |
| 2474 | sizeof(short int)]; |
| 2475 | struct cn_msg *cn_reply = (struct cn_msg *) buffer; |
| 2476 | struct drbd_nl_cfg_reply *reply = |
| 2477 | (struct drbd_nl_cfg_reply *)cn_reply->data; |
| 2478 | unsigned short *tl = reply->tag_list; |
| 2479 | unsigned long rs_left; |
| 2480 | unsigned int res; |
| 2481 | |
| 2482 | /* no local ref, no bitmap, no syncer progress, no broadcast. */ |
| 2483 | if (!get_ldev(mdev)) |
| 2484 | return; |
| 2485 | drbd_get_syncer_progress(mdev, &rs_left, &res); |
| 2486 | put_ldev(mdev); |
| 2487 | |
| 2488 | tl = tl_add_int(tl, T_sync_progress, &res); |
| 2489 | put_unaligned(TT_END, tl++); /* Close the tag list */ |
| 2490 | |
| 2491 | cn_reply->id.idx = CN_IDX_DRBD; |
| 2492 | cn_reply->id.val = CN_VAL_DRBD; |
| 2493 | |
| 2494 | cn_reply->seq = atomic_add_return(1, &drbd_nl_seq); |
| 2495 | cn_reply->ack = 0; /* not used here. */ |
| 2496 | cn_reply->len = sizeof(struct drbd_nl_cfg_reply) + |
| 2497 | (int)((char *)tl - (char *)reply->tag_list); |
| 2498 | cn_reply->flags = 0; |
| 2499 | |
| 2500 | reply->packet_type = P_sync_progress; |
| 2501 | reply->minor = mdev_to_minor(mdev); |
| 2502 | reply->ret_code = NO_ERROR; |
| 2503 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2504 | cn_netlink_send(cn_reply, CN_IDX_DRBD, GFP_NOIO); |
| 2505 | } |
| 2506 | |
| 2507 | int __init drbd_nl_init(void) |
| 2508 | { |
| 2509 | static struct cb_id cn_id_drbd; |
| 2510 | int err, try=10; |
| 2511 | |
| 2512 | cn_id_drbd.val = CN_VAL_DRBD; |
| 2513 | do { |
| 2514 | cn_id_drbd.idx = cn_idx; |
| 2515 | err = cn_add_callback(&cn_id_drbd, "cn_drbd", &drbd_connector_callback); |
| 2516 | if (!err) |
| 2517 | break; |
| 2518 | cn_idx = (cn_idx + CN_IDX_STEP); |
| 2519 | } while (try--); |
| 2520 | |
| 2521 | if (err) { |
| 2522 | printk(KERN_ERR "drbd: cn_drbd failed to register\n"); |
| 2523 | return err; |
| 2524 | } |
| 2525 | |
| 2526 | return 0; |
| 2527 | } |
| 2528 | |
| 2529 | void drbd_nl_cleanup(void) |
| 2530 | { |
| 2531 | static struct cb_id cn_id_drbd; |
| 2532 | |
| 2533 | cn_id_drbd.idx = cn_idx; |
| 2534 | cn_id_drbd.val = CN_VAL_DRBD; |
| 2535 | |
| 2536 | cn_del_callback(&cn_id_drbd); |
| 2537 | } |
| 2538 | |
| 2539 | void drbd_nl_send_reply(struct cn_msg *req, int ret_code) |
| 2540 | { |
| 2541 | char buffer[sizeof(struct cn_msg)+sizeof(struct drbd_nl_cfg_reply)]; |
| 2542 | struct cn_msg *cn_reply = (struct cn_msg *) buffer; |
| 2543 | struct drbd_nl_cfg_reply *reply = |
| 2544 | (struct drbd_nl_cfg_reply *)cn_reply->data; |
| 2545 | int rr; |
| 2546 | |
Lars Ellenberg | 3e3a776 | 2010-11-24 10:41:45 +0100 | [diff] [blame] | 2547 | memset(buffer, 0, sizeof(buffer)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2548 | cn_reply->id = req->id; |
| 2549 | |
| 2550 | cn_reply->seq = req->seq; |
| 2551 | cn_reply->ack = req->ack + 1; |
| 2552 | cn_reply->len = sizeof(struct drbd_nl_cfg_reply); |
| 2553 | cn_reply->flags = 0; |
| 2554 | |
Lars Ellenberg | 42ff269 | 2010-11-24 10:11:14 +0100 | [diff] [blame] | 2555 | reply->packet_type = P_return_code_only; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2556 | reply->minor = ((struct drbd_nl_cfg_req *)req->data)->drbd_minor; |
| 2557 | reply->ret_code = ret_code; |
| 2558 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2559 | rr = cn_netlink_send(cn_reply, CN_IDX_DRBD, GFP_NOIO); |
| 2560 | if (rr && rr != -ESRCH) |
| 2561 | printk(KERN_INFO "drbd: cn_netlink_send()=%d\n", rr); |
| 2562 | } |
| 2563 | |