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