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> |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 32 | #include <linux/blkpg.h> |
| 33 | #include <linux/cpumask.h> |
| 34 | #include "drbd_int.h" |
Philipp Reisner | 265be2d | 2010-05-31 10:14:17 +0200 | [diff] [blame] | 35 | #include "drbd_req.h" |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 36 | #include "drbd_wrappers.h" |
| 37 | #include <asm/unaligned.h> |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 38 | #include <linux/drbd_limits.h> |
Philipp Reisner | 87f7be4 | 2010-06-11 13:56:33 +0200 | [diff] [blame] | 39 | #include <linux/kthread.h> |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 40 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 41 | #include <net/genetlink.h> |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 42 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 43 | /* .doit */ |
| 44 | // int drbd_adm_create_resource(struct sk_buff *skb, struct genl_info *info); |
| 45 | // int drbd_adm_delete_resource(struct sk_buff *skb, struct genl_info *info); |
| 46 | |
| 47 | int drbd_adm_add_minor(struct sk_buff *skb, struct genl_info *info); |
| 48 | int drbd_adm_delete_minor(struct sk_buff *skb, struct genl_info *info); |
| 49 | |
| 50 | int drbd_adm_create_connection(struct sk_buff *skb, struct genl_info *info); |
| 51 | int drbd_adm_delete_connection(struct sk_buff *skb, struct genl_info *info); |
Lars Ellenberg | 85f75dd7 | 2011-03-15 16:26:37 +0100 | [diff] [blame] | 52 | int drbd_adm_down(struct sk_buff *skb, struct genl_info *info); |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 53 | |
| 54 | int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info); |
| 55 | int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info); |
| 56 | int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info); |
| 57 | int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info); |
| 58 | int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info); |
| 59 | int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info); |
| 60 | int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info); |
| 61 | int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info); |
| 62 | int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info); |
| 63 | int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info); |
| 64 | int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info); |
| 65 | int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info); |
| 66 | int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info); |
| 67 | int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info); |
| 68 | int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info); |
| 69 | int drbd_adm_syncer(struct sk_buff *skb, struct genl_info *info); |
| 70 | int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info); |
| 71 | int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info); |
| 72 | /* .dumpit */ |
| 73 | int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb); |
| 74 | |
| 75 | #include <linux/drbd_genl_api.h> |
| 76 | #include <linux/genl_magic_func.h> |
| 77 | |
| 78 | /* used blkdev_get_by_path, to claim our meta data device(s) */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 79 | static char *drbd_m_holder = "Hands off! this is DRBD's meta data device."; |
| 80 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 81 | /* Configuration is strictly serialized, because generic netlink message |
| 82 | * processing is strictly serialized by the genl_lock(). |
| 83 | * Which means we can use one static global drbd_config_context struct. |
| 84 | */ |
| 85 | static struct drbd_config_context { |
| 86 | /* assigned from drbd_genlmsghdr */ |
| 87 | unsigned int minor; |
| 88 | /* assigned from request attributes, if present */ |
| 89 | unsigned int volume; |
| 90 | #define VOLUME_UNSPECIFIED (-1U) |
| 91 | /* pointer into the request skb, |
| 92 | * limited lifetime! */ |
| 93 | char *conn_name; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 94 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 95 | /* reply buffer */ |
| 96 | struct sk_buff *reply_skb; |
| 97 | /* pointer into reply buffer */ |
| 98 | struct drbd_genlmsghdr *reply_dh; |
| 99 | /* resolved from attributes, if possible */ |
| 100 | struct drbd_conf *mdev; |
| 101 | struct drbd_tconn *tconn; |
| 102 | } adm_ctx; |
| 103 | |
| 104 | static void drbd_adm_send_reply(struct sk_buff *skb, struct genl_info *info) |
| 105 | { |
| 106 | genlmsg_end(skb, genlmsg_data(nlmsg_data(nlmsg_hdr(skb)))); |
| 107 | if (genlmsg_reply(skb, info)) |
| 108 | printk(KERN_ERR "drbd: error sending genl reply\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 111 | /* Used on a fresh "drbd_adm_prepare"d reply_skb, this cannot fail: The only |
| 112 | * reason it could fail was no space in skb, and there are 4k available. */ |
Lars Ellenberg | 8432b31 | 2011-03-08 16:11:16 +0100 | [diff] [blame] | 113 | int drbd_msg_put_info(const char *info) |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 114 | { |
| 115 | struct sk_buff *skb = adm_ctx.reply_skb; |
| 116 | struct nlattr *nla; |
| 117 | int err = -EMSGSIZE; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 118 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 119 | if (!info || !info[0]) |
| 120 | return 0; |
| 121 | |
| 122 | nla = nla_nest_start(skb, DRBD_NLA_CFG_REPLY); |
| 123 | if (!nla) |
| 124 | return err; |
| 125 | |
| 126 | err = nla_put_string(skb, T_info_text, info); |
| 127 | if (err) { |
| 128 | nla_nest_cancel(skb, nla); |
| 129 | return err; |
| 130 | } else |
| 131 | nla_nest_end(skb, nla); |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | /* This would be a good candidate for a "pre_doit" hook, |
| 136 | * and per-family private info->pointers. |
| 137 | * But we need to stay compatible with older kernels. |
| 138 | * If it returns successfully, adm_ctx members are valid. |
| 139 | */ |
| 140 | #define DRBD_ADM_NEED_MINOR 1 |
| 141 | #define DRBD_ADM_NEED_CONN 2 |
| 142 | static int drbd_adm_prepare(struct sk_buff *skb, struct genl_info *info, |
| 143 | unsigned flags) |
| 144 | { |
| 145 | struct drbd_genlmsghdr *d_in = info->userhdr; |
| 146 | const u8 cmd = info->genlhdr->cmd; |
| 147 | int err; |
| 148 | |
| 149 | memset(&adm_ctx, 0, sizeof(adm_ctx)); |
| 150 | |
| 151 | /* genl_rcv_msg only checks for CAP_NET_ADMIN on "GENL_ADMIN_PERM" :( */ |
| 152 | if (cmd != DRBD_ADM_GET_STATUS |
| 153 | && security_netlink_recv(skb, CAP_SYS_ADMIN)) |
| 154 | return -EPERM; |
| 155 | |
| 156 | adm_ctx.reply_skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
| 157 | if (!adm_ctx.reply_skb) |
| 158 | goto fail; |
| 159 | |
| 160 | adm_ctx.reply_dh = genlmsg_put_reply(adm_ctx.reply_skb, |
| 161 | info, &drbd_genl_family, 0, cmd); |
| 162 | /* put of a few bytes into a fresh skb of >= 4k will always succeed. |
| 163 | * but anyways */ |
| 164 | if (!adm_ctx.reply_dh) |
| 165 | goto fail; |
| 166 | |
| 167 | adm_ctx.reply_dh->minor = d_in->minor; |
| 168 | adm_ctx.reply_dh->ret_code = NO_ERROR; |
| 169 | |
| 170 | if (info->attrs[DRBD_NLA_CFG_CONTEXT]) { |
| 171 | struct nlattr *nla; |
| 172 | /* parse and validate only */ |
| 173 | err = drbd_cfg_context_from_attrs(NULL, info->attrs); |
| 174 | if (err) |
| 175 | goto fail; |
| 176 | |
| 177 | /* It was present, and valid, |
| 178 | * copy it over to the reply skb. */ |
| 179 | err = nla_put_nohdr(adm_ctx.reply_skb, |
| 180 | info->attrs[DRBD_NLA_CFG_CONTEXT]->nla_len, |
| 181 | info->attrs[DRBD_NLA_CFG_CONTEXT]); |
| 182 | if (err) |
| 183 | goto fail; |
| 184 | |
| 185 | /* and assign stuff to the global adm_ctx */ |
| 186 | nla = nested_attr_tb[__nla_type(T_ctx_volume)]; |
| 187 | adm_ctx.volume = nla ? nla_get_u32(nla) : VOLUME_UNSPECIFIED; |
| 188 | nla = nested_attr_tb[__nla_type(T_ctx_conn_name)]; |
| 189 | if (nla) |
| 190 | adm_ctx.conn_name = nla_data(nla); |
| 191 | } else |
| 192 | adm_ctx.volume = VOLUME_UNSPECIFIED; |
| 193 | |
| 194 | adm_ctx.minor = d_in->minor; |
| 195 | adm_ctx.mdev = minor_to_mdev(d_in->minor); |
| 196 | adm_ctx.tconn = conn_by_name(adm_ctx.conn_name); |
| 197 | |
| 198 | if (!adm_ctx.mdev && (flags & DRBD_ADM_NEED_MINOR)) { |
| 199 | drbd_msg_put_info("unknown minor"); |
| 200 | return ERR_MINOR_INVALID; |
| 201 | } |
| 202 | if (!adm_ctx.tconn && (flags & DRBD_ADM_NEED_CONN)) { |
| 203 | drbd_msg_put_info("unknown connection"); |
| 204 | return ERR_INVALID_REQUEST; |
| 205 | } |
| 206 | |
| 207 | /* some more paranoia, if the request was over-determined */ |
Lars Ellenberg | 527f4b2 | 2011-03-14 13:58:03 +0100 | [diff] [blame] | 208 | if (adm_ctx.mdev && adm_ctx.tconn && |
| 209 | adm_ctx.mdev->tconn != adm_ctx.tconn) { |
| 210 | pr_warning("request: minor=%u, conn=%s; but that minor belongs to connection %s\n", |
| 211 | adm_ctx.minor, adm_ctx.conn_name, adm_ctx.mdev->tconn->name); |
| 212 | drbd_msg_put_info("minor exists in different connection"); |
| 213 | return ERR_INVALID_REQUEST; |
| 214 | } |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 215 | if (adm_ctx.mdev && |
| 216 | adm_ctx.volume != VOLUME_UNSPECIFIED && |
| 217 | adm_ctx.volume != adm_ctx.mdev->vnr) { |
| 218 | pr_warning("request: minor=%u, volume=%u; but that minor is volume %u in %s\n", |
| 219 | adm_ctx.minor, adm_ctx.volume, |
| 220 | adm_ctx.mdev->vnr, adm_ctx.mdev->tconn->name); |
Lars Ellenberg | 527f4b2 | 2011-03-14 13:58:03 +0100 | [diff] [blame] | 221 | drbd_msg_put_info("minor exists as different volume"); |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 222 | return ERR_INVALID_REQUEST; |
| 223 | } |
Lars Ellenberg | cffec5b | 2011-03-10 23:33:15 +0100 | [diff] [blame] | 224 | if (adm_ctx.mdev && !adm_ctx.tconn) |
| 225 | adm_ctx.tconn = adm_ctx.mdev->tconn; |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 226 | return NO_ERROR; |
| 227 | |
| 228 | fail: |
| 229 | nlmsg_free(adm_ctx.reply_skb); |
| 230 | adm_ctx.reply_skb = NULL; |
| 231 | return -ENOMEM; |
| 232 | } |
| 233 | |
| 234 | static int drbd_adm_finish(struct genl_info *info, int retcode) |
| 235 | { |
| 236 | struct nlattr *nla; |
| 237 | const char *conn_name = NULL; |
| 238 | |
| 239 | if (!adm_ctx.reply_skb) |
| 240 | return -ENOMEM; |
| 241 | |
| 242 | adm_ctx.reply_dh->ret_code = retcode; |
| 243 | |
| 244 | nla = info->attrs[DRBD_NLA_CFG_CONTEXT]; |
| 245 | if (nla) { |
| 246 | nla = nla_find_nested(nla, __nla_type(T_ctx_conn_name)); |
| 247 | if (nla) |
| 248 | conn_name = nla_data(nla); |
| 249 | } |
| 250 | |
| 251 | drbd_adm_send_reply(adm_ctx.reply_skb, info); |
| 252 | return 0; |
| 253 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 254 | |
| 255 | int drbd_khelper(struct drbd_conf *mdev, char *cmd) |
| 256 | { |
| 257 | char *envp[] = { "HOME=/", |
| 258 | "TERM=linux", |
| 259 | "PATH=/sbin:/usr/sbin:/bin:/usr/bin", |
| 260 | NULL, /* Will be set to address family */ |
| 261 | NULL, /* Will be set to address */ |
| 262 | NULL }; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 263 | char mb[12], af[20], ad[60], *afs; |
| 264 | char *argv[] = {usermode_helper, cmd, mb, NULL }; |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 265 | struct sib_info sib; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 266 | int ret; |
| 267 | |
| 268 | snprintf(mb, 12, "minor-%d", mdev_to_minor(mdev)); |
| 269 | |
Philipp Reisner | b2fb6dbe | 2011-01-19 13:48:44 +0100 | [diff] [blame] | 270 | if (get_net_conf(mdev->tconn)) { |
Philipp Reisner | 89e58e7 | 2011-01-19 13:12:45 +0100 | [diff] [blame] | 271 | switch (((struct sockaddr *)mdev->tconn->net_conf->peer_addr)->sa_family) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 272 | case AF_INET6: |
| 273 | afs = "ipv6"; |
| 274 | snprintf(ad, 60, "DRBD_PEER_ADDRESS=%pI6", |
Philipp Reisner | 89e58e7 | 2011-01-19 13:12:45 +0100 | [diff] [blame] | 275 | &((struct sockaddr_in6 *)mdev->tconn->net_conf->peer_addr)->sin6_addr); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 276 | break; |
| 277 | case AF_INET: |
| 278 | afs = "ipv4"; |
| 279 | snprintf(ad, 60, "DRBD_PEER_ADDRESS=%pI4", |
Philipp Reisner | 89e58e7 | 2011-01-19 13:12:45 +0100 | [diff] [blame] | 280 | &((struct sockaddr_in *)mdev->tconn->net_conf->peer_addr)->sin_addr); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 281 | break; |
| 282 | default: |
| 283 | afs = "ssocks"; |
| 284 | snprintf(ad, 60, "DRBD_PEER_ADDRESS=%pI4", |
Philipp Reisner | 89e58e7 | 2011-01-19 13:12:45 +0100 | [diff] [blame] | 285 | &((struct sockaddr_in *)mdev->tconn->net_conf->peer_addr)->sin_addr); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 286 | } |
| 287 | snprintf(af, 20, "DRBD_PEER_AF=%s", afs); |
| 288 | envp[3]=af; |
| 289 | envp[4]=ad; |
Philipp Reisner | b2fb6dbe | 2011-01-19 13:48:44 +0100 | [diff] [blame] | 290 | put_net_conf(mdev->tconn); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Lars Ellenberg | 1090c05 | 2010-07-19 17:41:04 +0200 | [diff] [blame] | 293 | /* The helper may take some time. |
| 294 | * write out any unsynced meta data changes now */ |
| 295 | drbd_md_sync(mdev); |
| 296 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 297 | dev_info(DEV, "helper command: %s %s %s\n", usermode_helper, cmd, mb); |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 298 | sib.sib_reason = SIB_HELPER_PRE; |
| 299 | sib.helper_name = cmd; |
| 300 | drbd_bcast_event(mdev, &sib); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 301 | ret = call_usermodehelper(usermode_helper, argv, envp, 1); |
| 302 | if (ret) |
| 303 | dev_warn(DEV, "helper command: %s %s %s exit code %u (0x%x)\n", |
| 304 | usermode_helper, cmd, mb, |
| 305 | (ret >> 8) & 0xff, ret); |
| 306 | else |
| 307 | dev_info(DEV, "helper command: %s %s %s exit code %u (0x%x)\n", |
| 308 | usermode_helper, cmd, mb, |
| 309 | (ret >> 8) & 0xff, ret); |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 310 | sib.sib_reason = SIB_HELPER_POST; |
| 311 | sib.helper_exit_code = ret; |
| 312 | drbd_bcast_event(mdev, &sib); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 313 | |
| 314 | if (ret < 0) /* Ignore any ERRNOs we got. */ |
| 315 | ret = 0; |
| 316 | |
| 317 | return ret; |
| 318 | } |
| 319 | |
| 320 | enum drbd_disk_state drbd_try_outdate_peer(struct drbd_conf *mdev) |
| 321 | { |
| 322 | char *ex_to_string; |
| 323 | int r; |
| 324 | enum drbd_disk_state nps; |
| 325 | enum drbd_fencing_p fp; |
| 326 | |
| 327 | D_ASSERT(mdev->state.pdsk == D_UNKNOWN); |
| 328 | |
| 329 | if (get_ldev_if_state(mdev, D_CONSISTENT)) { |
| 330 | fp = mdev->ldev->dc.fencing; |
| 331 | put_ldev(mdev); |
| 332 | } else { |
| 333 | 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] | 334 | nps = mdev->state.pdsk; |
| 335 | goto out; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 338 | r = drbd_khelper(mdev, "fence-peer"); |
| 339 | |
| 340 | switch ((r>>8) & 0xff) { |
| 341 | case 3: /* peer is inconsistent */ |
| 342 | ex_to_string = "peer is inconsistent or worse"; |
| 343 | nps = D_INCONSISTENT; |
| 344 | break; |
| 345 | case 4: /* peer got outdated, or was already outdated */ |
| 346 | ex_to_string = "peer was fenced"; |
| 347 | nps = D_OUTDATED; |
| 348 | break; |
| 349 | case 5: /* peer was down */ |
| 350 | if (mdev->state.disk == D_UP_TO_DATE) { |
| 351 | /* we will(have) create(d) a new UUID anyways... */ |
| 352 | ex_to_string = "peer is unreachable, assumed to be dead"; |
| 353 | nps = D_OUTDATED; |
| 354 | } else { |
| 355 | ex_to_string = "peer unreachable, doing nothing since disk != UpToDate"; |
| 356 | nps = mdev->state.pdsk; |
| 357 | } |
| 358 | break; |
| 359 | case 6: /* Peer is primary, voluntarily outdate myself. |
| 360 | * This is useful when an unconnected R_SECONDARY is asked to |
| 361 | * become R_PRIMARY, but finds the other peer being active. */ |
| 362 | ex_to_string = "peer is active"; |
| 363 | dev_warn(DEV, "Peer is primary, outdating myself.\n"); |
| 364 | nps = D_UNKNOWN; |
| 365 | _drbd_request_state(mdev, NS(disk, D_OUTDATED), CS_WAIT_COMPLETE); |
| 366 | break; |
| 367 | case 7: |
| 368 | if (fp != FP_STONITH) |
| 369 | dev_err(DEV, "fence-peer() = 7 && fencing != Stonith !!!\n"); |
| 370 | ex_to_string = "peer was stonithed"; |
| 371 | nps = D_OUTDATED; |
| 372 | break; |
| 373 | default: |
| 374 | /* The script is broken ... */ |
| 375 | nps = D_UNKNOWN; |
| 376 | dev_err(DEV, "fence-peer helper broken, returned %d\n", (r>>8)&0xff); |
| 377 | return nps; |
| 378 | } |
| 379 | |
| 380 | dev_info(DEV, "fence-peer helper returned %d (%s)\n", |
| 381 | (r>>8) & 0xff, ex_to_string); |
Philipp Reisner | fb22c40 | 2010-09-08 23:20:21 +0200 | [diff] [blame] | 382 | |
| 383 | out: |
| 384 | if (mdev->state.susp_fen && nps >= D_UNKNOWN) { |
| 385 | /* The handler was not successful... unfreeze here, the |
| 386 | state engine can not unfreeze... */ |
| 387 | _drbd_request_state(mdev, NS(susp_fen, 0), CS_VERBOSE); |
| 388 | } |
| 389 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 390 | return nps; |
| 391 | } |
| 392 | |
Philipp Reisner | 87f7be4 | 2010-06-11 13:56:33 +0200 | [diff] [blame] | 393 | static int _try_outdate_peer_async(void *data) |
| 394 | { |
| 395 | struct drbd_conf *mdev = (struct drbd_conf *)data; |
| 396 | enum drbd_disk_state nps; |
Philipp Reisner | 21423fa | 2011-05-17 14:19:41 +0200 | [diff] [blame] | 397 | union drbd_state ns; |
Philipp Reisner | 87f7be4 | 2010-06-11 13:56:33 +0200 | [diff] [blame] | 398 | |
| 399 | nps = drbd_try_outdate_peer(mdev); |
Philipp Reisner | 21423fa | 2011-05-17 14:19:41 +0200 | [diff] [blame] | 400 | |
| 401 | /* Not using |
| 402 | drbd_request_state(mdev, NS(pdsk, nps)); |
Philipp Reisner | 99432fc | 2011-05-20 16:39:13 +0200 | [diff] [blame] | 403 | here, because we might were able to re-establish the connection |
| 404 | in the meantime. This can only partially be solved in the state's |
| 405 | engine is_valid_state() and is_valid_state_transition() |
| 406 | functions. |
| 407 | |
| 408 | nps can be D_INCONSISTENT, D_OUTDATED or D_UNKNOWN. |
| 409 | pdsk == D_INCONSISTENT while conn >= C_CONNECTED is valid, |
| 410 | therefore we have to have the pre state change check here. |
Philipp Reisner | 21423fa | 2011-05-17 14:19:41 +0200 | [diff] [blame] | 411 | */ |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 412 | spin_lock_irq(&mdev->tconn->req_lock); |
Philipp Reisner | 21423fa | 2011-05-17 14:19:41 +0200 | [diff] [blame] | 413 | ns = mdev->state; |
| 414 | if (ns.conn < C_WF_REPORT_PARAMS) { |
| 415 | ns.pdsk = nps; |
| 416 | _drbd_set_state(mdev, ns, CS_VERBOSE, NULL); |
| 417 | } |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 418 | spin_unlock_irq(&mdev->tconn->req_lock); |
Philipp Reisner | 87f7be4 | 2010-06-11 13:56:33 +0200 | [diff] [blame] | 419 | |
| 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | void drbd_try_outdate_peer_async(struct drbd_conf *mdev) |
| 424 | { |
| 425 | struct task_struct *opa; |
| 426 | |
| 427 | opa = kthread_run(_try_outdate_peer_async, mdev, "drbd%d_a_helper", mdev_to_minor(mdev)); |
| 428 | if (IS_ERR(opa)) |
| 429 | dev_err(DEV, "out of mem, failed to invoke fence-peer helper\n"); |
| 430 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 431 | |
Andreas Gruenbacher | bf885f8 | 2010-12-08 00:39:32 +0100 | [diff] [blame] | 432 | enum drbd_state_rv |
| 433 | 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] | 434 | { |
| 435 | const int max_tries = 4; |
Andreas Gruenbacher | bf885f8 | 2010-12-08 00:39:32 +0100 | [diff] [blame] | 436 | enum drbd_state_rv rv = SS_UNKNOWN_ERROR; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 437 | int try = 0; |
| 438 | int forced = 0; |
| 439 | union drbd_state mask, val; |
| 440 | enum drbd_disk_state nps; |
| 441 | |
| 442 | if (new_role == R_PRIMARY) |
Philipp Reisner | 0625ac1 | 2011-02-07 14:49:19 +0100 | [diff] [blame] | 443 | request_ping(mdev->tconn); /* Detect a dead peer ASAP */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 444 | |
Philipp Reisner | 8410da8 | 2011-02-11 20:11:10 +0100 | [diff] [blame] | 445 | mutex_lock(mdev->state_mutex); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 446 | |
| 447 | mask.i = 0; mask.role = R_MASK; |
| 448 | val.i = 0; val.role = new_role; |
| 449 | |
| 450 | while (try++ < max_tries) { |
Andreas Gruenbacher | bf885f8 | 2010-12-08 00:39:32 +0100 | [diff] [blame] | 451 | rv = _drbd_request_state(mdev, mask, val, CS_WAIT_COMPLETE); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 452 | |
| 453 | /* in case we first succeeded to outdate, |
| 454 | * but now suddenly could establish a connection */ |
Andreas Gruenbacher | bf885f8 | 2010-12-08 00:39:32 +0100 | [diff] [blame] | 455 | if (rv == SS_CW_FAILED_BY_PEER && mask.pdsk != 0) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 456 | val.pdsk = 0; |
| 457 | mask.pdsk = 0; |
| 458 | continue; |
| 459 | } |
| 460 | |
Andreas Gruenbacher | bf885f8 | 2010-12-08 00:39:32 +0100 | [diff] [blame] | 461 | if (rv == SS_NO_UP_TO_DATE_DISK && force && |
Philipp Reisner | d10a33c | 2010-03-04 15:11:39 +0100 | [diff] [blame] | 462 | (mdev->state.disk < D_UP_TO_DATE && |
| 463 | mdev->state.disk >= D_INCONSISTENT)) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 464 | mask.disk = D_MASK; |
| 465 | val.disk = D_UP_TO_DATE; |
| 466 | forced = 1; |
| 467 | continue; |
| 468 | } |
| 469 | |
Andreas Gruenbacher | bf885f8 | 2010-12-08 00:39:32 +0100 | [diff] [blame] | 470 | if (rv == SS_NO_UP_TO_DATE_DISK && |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 471 | mdev->state.disk == D_CONSISTENT && mask.pdsk == 0) { |
| 472 | D_ASSERT(mdev->state.pdsk == D_UNKNOWN); |
| 473 | nps = drbd_try_outdate_peer(mdev); |
| 474 | |
| 475 | if (nps == D_OUTDATED || nps == D_INCONSISTENT) { |
| 476 | val.disk = D_UP_TO_DATE; |
| 477 | mask.disk = D_MASK; |
| 478 | } |
| 479 | |
| 480 | val.pdsk = nps; |
| 481 | mask.pdsk = D_MASK; |
| 482 | |
| 483 | continue; |
| 484 | } |
| 485 | |
Andreas Gruenbacher | bf885f8 | 2010-12-08 00:39:32 +0100 | [diff] [blame] | 486 | if (rv == SS_NOTHING_TO_DO) |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 487 | goto out; |
Andreas Gruenbacher | bf885f8 | 2010-12-08 00:39:32 +0100 | [diff] [blame] | 488 | if (rv == SS_PRIMARY_NOP && mask.pdsk == 0) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 489 | nps = drbd_try_outdate_peer(mdev); |
| 490 | |
| 491 | if (force && nps > D_OUTDATED) { |
| 492 | dev_warn(DEV, "Forced into split brain situation!\n"); |
| 493 | nps = D_OUTDATED; |
| 494 | } |
| 495 | |
| 496 | mask.pdsk = D_MASK; |
| 497 | val.pdsk = nps; |
| 498 | |
| 499 | continue; |
| 500 | } |
Andreas Gruenbacher | bf885f8 | 2010-12-08 00:39:32 +0100 | [diff] [blame] | 501 | if (rv == SS_TWO_PRIMARIES) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 502 | /* Maybe the peer is detected as dead very soon... |
| 503 | retry at most once more in this case. */ |
Philipp Reisner | 89e58e7 | 2011-01-19 13:12:45 +0100 | [diff] [blame] | 504 | schedule_timeout_interruptible((mdev->tconn->net_conf->ping_timeo+1)*HZ/10); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 505 | if (try < max_tries) |
| 506 | try = max_tries - 1; |
| 507 | continue; |
| 508 | } |
Andreas Gruenbacher | bf885f8 | 2010-12-08 00:39:32 +0100 | [diff] [blame] | 509 | if (rv < SS_SUCCESS) { |
| 510 | rv = _drbd_request_state(mdev, mask, val, |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 511 | CS_VERBOSE + CS_WAIT_COMPLETE); |
Andreas Gruenbacher | bf885f8 | 2010-12-08 00:39:32 +0100 | [diff] [blame] | 512 | if (rv < SS_SUCCESS) |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 513 | goto out; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 514 | } |
| 515 | break; |
| 516 | } |
| 517 | |
Andreas Gruenbacher | bf885f8 | 2010-12-08 00:39:32 +0100 | [diff] [blame] | 518 | if (rv < SS_SUCCESS) |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 519 | goto out; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 520 | |
| 521 | if (forced) |
| 522 | dev_warn(DEV, "Forced to consider local data as UpToDate!\n"); |
| 523 | |
| 524 | /* Wait until nothing is on the fly :) */ |
| 525 | wait_event(mdev->misc_wait, atomic_read(&mdev->ap_pending_cnt) == 0); |
| 526 | |
| 527 | if (new_role == R_SECONDARY) { |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 528 | set_disk_ro(mdev->vdisk, true); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 529 | if (get_ldev(mdev)) { |
| 530 | mdev->ldev->md.uuid[UI_CURRENT] &= ~(u64)1; |
| 531 | put_ldev(mdev); |
| 532 | } |
| 533 | } else { |
Philipp Reisner | b2fb6dbe | 2011-01-19 13:48:44 +0100 | [diff] [blame] | 534 | if (get_net_conf(mdev->tconn)) { |
Philipp Reisner | 89e58e7 | 2011-01-19 13:12:45 +0100 | [diff] [blame] | 535 | mdev->tconn->net_conf->want_lose = 0; |
Philipp Reisner | b2fb6dbe | 2011-01-19 13:48:44 +0100 | [diff] [blame] | 536 | put_net_conf(mdev->tconn); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 537 | } |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 538 | set_disk_ro(mdev->vdisk, false); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 539 | if (get_ldev(mdev)) { |
| 540 | if (((mdev->state.conn < C_CONNECTED || |
| 541 | mdev->state.pdsk <= D_FAILED) |
| 542 | && mdev->ldev->md.uuid[UI_BITMAP] == 0) || forced) |
| 543 | drbd_uuid_new_current(mdev); |
| 544 | |
| 545 | mdev->ldev->md.uuid[UI_CURRENT] |= (u64)1; |
| 546 | put_ldev(mdev); |
| 547 | } |
| 548 | } |
| 549 | |
Lars Ellenberg | 19f843a | 2010-12-15 08:59:11 +0100 | [diff] [blame] | 550 | /* writeout of activity log covered areas of the bitmap |
| 551 | * to stable storage done in after state change already */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 552 | |
| 553 | if (mdev->state.conn >= C_WF_REPORT_PARAMS) { |
| 554 | /* if this was forced, we should consider sync */ |
| 555 | if (forced) |
| 556 | drbd_send_uuids(mdev); |
| 557 | drbd_send_state(mdev); |
| 558 | } |
| 559 | |
| 560 | drbd_md_sync(mdev); |
| 561 | |
| 562 | kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE); |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 563 | out: |
Philipp Reisner | 8410da8 | 2011-02-11 20:11:10 +0100 | [diff] [blame] | 564 | mutex_unlock(mdev->state_mutex); |
Andreas Gruenbacher | bf885f8 | 2010-12-08 00:39:32 +0100 | [diff] [blame] | 565 | return rv; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 566 | } |
| 567 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 568 | static const char *from_attrs_err_to_txt(int err) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 569 | { |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 570 | return err == -ENOMSG ? "required attribute missing" : |
| 571 | err == -EOPNOTSUPP ? "unknown mandatory attribute" : |
| 572 | "invalid attribute value"; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 573 | } |
| 574 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 575 | int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 576 | { |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 577 | struct set_role_parms parms; |
| 578 | int err; |
| 579 | enum drbd_ret_code retcode; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 580 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 581 | retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR); |
| 582 | if (!adm_ctx.reply_skb) |
| 583 | return retcode; |
| 584 | if (retcode != NO_ERROR) |
| 585 | goto out; |
| 586 | |
| 587 | memset(&parms, 0, sizeof(parms)); |
| 588 | if (info->attrs[DRBD_NLA_SET_ROLE_PARMS]) { |
| 589 | err = set_role_parms_from_attrs(&parms, info->attrs); |
| 590 | if (err) { |
| 591 | retcode = ERR_MANDATORY_TAG; |
| 592 | drbd_msg_put_info(from_attrs_err_to_txt(err)); |
| 593 | goto out; |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | if (info->genlhdr->cmd == DRBD_ADM_PRIMARY) |
| 598 | retcode = drbd_set_role(adm_ctx.mdev, R_PRIMARY, parms.assume_uptodate); |
| 599 | else |
| 600 | retcode = drbd_set_role(adm_ctx.mdev, R_SECONDARY, 0); |
| 601 | out: |
| 602 | drbd_adm_finish(info, retcode); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 603 | return 0; |
| 604 | } |
| 605 | |
| 606 | /* initializes the md.*_offset members, so we are able to find |
| 607 | * the on disk meta data */ |
| 608 | static void drbd_md_set_sector_offsets(struct drbd_conf *mdev, |
| 609 | struct drbd_backing_dev *bdev) |
| 610 | { |
| 611 | sector_t md_size_sect = 0; |
| 612 | switch (bdev->dc.meta_dev_idx) { |
| 613 | default: |
| 614 | /* v07 style fixed size indexed meta data */ |
| 615 | bdev->md.md_size_sect = MD_RESERVED_SECT; |
| 616 | bdev->md.md_offset = drbd_md_ss__(mdev, bdev); |
| 617 | bdev->md.al_offset = MD_AL_OFFSET; |
| 618 | bdev->md.bm_offset = MD_BM_OFFSET; |
| 619 | break; |
| 620 | case DRBD_MD_INDEX_FLEX_EXT: |
| 621 | /* just occupy the full device; unit: sectors */ |
| 622 | bdev->md.md_size_sect = drbd_get_capacity(bdev->md_bdev); |
| 623 | bdev->md.md_offset = 0; |
| 624 | bdev->md.al_offset = MD_AL_OFFSET; |
| 625 | bdev->md.bm_offset = MD_BM_OFFSET; |
| 626 | break; |
| 627 | case DRBD_MD_INDEX_INTERNAL: |
| 628 | case DRBD_MD_INDEX_FLEX_INT: |
| 629 | bdev->md.md_offset = drbd_md_ss__(mdev, bdev); |
| 630 | /* al size is still fixed */ |
Lars Ellenberg | 7ad651b | 2011-02-21 13:21:03 +0100 | [diff] [blame] | 631 | bdev->md.al_offset = -MD_AL_SECTORS; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 632 | /* we need (slightly less than) ~ this much bitmap sectors: */ |
| 633 | md_size_sect = drbd_get_capacity(bdev->backing_bdev); |
| 634 | md_size_sect = ALIGN(md_size_sect, BM_SECT_PER_EXT); |
| 635 | md_size_sect = BM_SECT_TO_EXT(md_size_sect); |
| 636 | md_size_sect = ALIGN(md_size_sect, 8); |
| 637 | |
| 638 | /* plus the "drbd meta data super block", |
| 639 | * and the activity log; */ |
| 640 | md_size_sect += MD_BM_OFFSET; |
| 641 | |
| 642 | bdev->md.md_size_sect = md_size_sect; |
| 643 | /* bitmap offset is adjusted by 'super' block size */ |
| 644 | bdev->md.bm_offset = -md_size_sect + MD_AL_OFFSET; |
| 645 | break; |
| 646 | } |
| 647 | } |
| 648 | |
Lars Ellenberg | 4b0715f | 2010-12-14 15:13:04 +0100 | [diff] [blame] | 649 | /* input size is expected to be in KB */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 650 | char *ppsize(char *buf, unsigned long long size) |
| 651 | { |
Lars Ellenberg | 4b0715f | 2010-12-14 15:13:04 +0100 | [diff] [blame] | 652 | /* Needs 9 bytes at max including trailing NUL: |
| 653 | * -1ULL ==> "16384 EB" */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 654 | static char units[] = { 'K', 'M', 'G', 'T', 'P', 'E' }; |
| 655 | int base = 0; |
Lars Ellenberg | 4b0715f | 2010-12-14 15:13:04 +0100 | [diff] [blame] | 656 | while (size >= 10000 && base < sizeof(units)-1) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 657 | /* shift + round */ |
| 658 | size = (size >> 10) + !!(size & (1<<9)); |
| 659 | base++; |
| 660 | } |
Lars Ellenberg | 4b0715f | 2010-12-14 15:13:04 +0100 | [diff] [blame] | 661 | sprintf(buf, "%u %cB", (unsigned)size, units[base]); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 662 | |
| 663 | return buf; |
| 664 | } |
| 665 | |
| 666 | /* there is still a theoretical deadlock when called from receiver |
| 667 | * on an D_INCONSISTENT R_PRIMARY: |
| 668 | * remote READ does inc_ap_bio, receiver would need to receive answer |
| 669 | * packet from remote to dec_ap_bio again. |
| 670 | * receiver receive_sizes(), comes here, |
| 671 | * waits for ap_bio_cnt == 0. -> deadlock. |
| 672 | * but this cannot happen, actually, because: |
| 673 | * R_PRIMARY D_INCONSISTENT, and peer's disk is unreachable |
| 674 | * (not connected, or bad/no disk on peer): |
| 675 | * see drbd_fail_request_early, ap_bio_cnt is zero. |
| 676 | * R_PRIMARY D_INCONSISTENT, and C_SYNC_TARGET: |
| 677 | * peer may not initiate a resize. |
| 678 | */ |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 679 | /* Note these are not to be confused with |
| 680 | * drbd_adm_suspend_io/drbd_adm_resume_io, |
| 681 | * which are (sub) state changes triggered by admin (drbdsetup), |
| 682 | * and can be long lived. |
| 683 | * This changes an mdev->flag, is triggered by drbd internals, |
| 684 | * and should be short-lived. */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 685 | void drbd_suspend_io(struct drbd_conf *mdev) |
| 686 | { |
| 687 | set_bit(SUSPEND_IO, &mdev->flags); |
Philipp Reisner | fb22c40 | 2010-09-08 23:20:21 +0200 | [diff] [blame] | 688 | if (is_susp(mdev->state)) |
Philipp Reisner | 265be2d | 2010-05-31 10:14:17 +0200 | [diff] [blame] | 689 | return; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 690 | wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_bio_cnt)); |
| 691 | } |
| 692 | |
| 693 | void drbd_resume_io(struct drbd_conf *mdev) |
| 694 | { |
| 695 | clear_bit(SUSPEND_IO, &mdev->flags); |
| 696 | wake_up(&mdev->misc_wait); |
| 697 | } |
| 698 | |
| 699 | /** |
| 700 | * drbd_determine_dev_size() - Sets the right device size obeying all constraints |
| 701 | * @mdev: DRBD device. |
| 702 | * |
| 703 | * Returns 0 on success, negative return values indicate errors. |
| 704 | * You should call drbd_md_sync() after calling this function. |
| 705 | */ |
Bart Van Assche | 24c4830 | 2011-05-21 18:32:29 +0200 | [diff] [blame] | 706 | 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] | 707 | { |
| 708 | sector_t prev_first_sect, prev_size; /* previous meta location */ |
| 709 | sector_t la_size; |
| 710 | sector_t size; |
| 711 | char ppb[10]; |
| 712 | |
| 713 | int md_moved, la_size_changed; |
| 714 | enum determine_dev_size rv = unchanged; |
| 715 | |
| 716 | /* race: |
| 717 | * application request passes inc_ap_bio, |
| 718 | * but then cannot get an AL-reference. |
| 719 | * this function later may wait on ap_bio_cnt == 0. -> deadlock. |
| 720 | * |
| 721 | * to avoid that: |
| 722 | * Suspend IO right here. |
| 723 | * still lock the act_log to not trigger ASSERTs there. |
| 724 | */ |
| 725 | drbd_suspend_io(mdev); |
| 726 | |
| 727 | /* no wait necessary anymore, actually we could assert that */ |
| 728 | wait_event(mdev->al_wait, lc_try_lock(mdev->act_log)); |
| 729 | |
| 730 | prev_first_sect = drbd_md_first_sector(mdev->ldev); |
| 731 | prev_size = mdev->ldev->md.md_size_sect; |
| 732 | la_size = mdev->ldev->md.la_size_sect; |
| 733 | |
| 734 | /* TODO: should only be some assert here, not (re)init... */ |
| 735 | drbd_md_set_sector_offsets(mdev, mdev->ldev); |
| 736 | |
Philipp Reisner | d845030 | 2010-03-24 15:51:26 +0100 | [diff] [blame] | 737 | size = drbd_new_dev_size(mdev, mdev->ldev, flags & DDSF_FORCED); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 738 | |
| 739 | if (drbd_get_capacity(mdev->this_bdev) != size || |
| 740 | drbd_bm_capacity(mdev) != size) { |
| 741 | int err; |
Philipp Reisner | 02d9a94 | 2010-03-24 16:23:03 +0100 | [diff] [blame] | 742 | err = drbd_bm_resize(mdev, size, !(flags & DDSF_NO_RESYNC)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 743 | if (unlikely(err)) { |
| 744 | /* currently there is only one error: ENOMEM! */ |
| 745 | size = drbd_bm_capacity(mdev)>>1; |
| 746 | if (size == 0) { |
| 747 | dev_err(DEV, "OUT OF MEMORY! " |
| 748 | "Could not allocate bitmap!\n"); |
| 749 | } else { |
| 750 | dev_err(DEV, "BM resizing failed. " |
| 751 | "Leaving size unchanged at size = %lu KB\n", |
| 752 | (unsigned long)size); |
| 753 | } |
| 754 | rv = dev_size_error; |
| 755 | } |
| 756 | /* racy, see comments above. */ |
| 757 | drbd_set_my_capacity(mdev, size); |
| 758 | mdev->ldev->md.la_size_sect = size; |
| 759 | dev_info(DEV, "size = %s (%llu KB)\n", ppsize(ppb, size>>1), |
| 760 | (unsigned long long)size>>1); |
| 761 | } |
| 762 | if (rv == dev_size_error) |
| 763 | goto out; |
| 764 | |
| 765 | la_size_changed = (la_size != mdev->ldev->md.la_size_sect); |
| 766 | |
| 767 | md_moved = prev_first_sect != drbd_md_first_sector(mdev->ldev) |
| 768 | || prev_size != mdev->ldev->md.md_size_sect; |
| 769 | |
| 770 | if (la_size_changed || md_moved) { |
Andreas Gruenbacher | 24dccab | 2010-12-12 17:45:41 +0100 | [diff] [blame] | 771 | int err; |
| 772 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 773 | drbd_al_shrink(mdev); /* All extents inactive. */ |
| 774 | dev_info(DEV, "Writing the whole bitmap, %s\n", |
| 775 | la_size_changed && md_moved ? "size changed and md moved" : |
| 776 | la_size_changed ? "size changed" : "md moved"); |
Lars Ellenberg | 20ceb2b | 2011-01-21 10:56:44 +0100 | [diff] [blame] | 777 | /* next line implicitly does drbd_suspend_io()+drbd_resume_io() */ |
| 778 | err = drbd_bitmap_io(mdev, &drbd_bm_write, |
| 779 | "size changed", BM_LOCKED_MASK); |
Andreas Gruenbacher | 24dccab | 2010-12-12 17:45:41 +0100 | [diff] [blame] | 780 | if (err) { |
| 781 | rv = dev_size_error; |
| 782 | goto out; |
| 783 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 784 | drbd_md_mark_dirty(mdev); |
| 785 | } |
| 786 | |
| 787 | if (size > la_size) |
| 788 | rv = grew; |
| 789 | if (size < la_size) |
| 790 | rv = shrunk; |
| 791 | out: |
| 792 | lc_unlock(mdev->act_log); |
| 793 | wake_up(&mdev->al_wait); |
| 794 | drbd_resume_io(mdev); |
| 795 | |
| 796 | return rv; |
| 797 | } |
| 798 | |
| 799 | sector_t |
Philipp Reisner | a393db6 | 2009-12-22 13:35:52 +0100 | [diff] [blame] | 800 | 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] | 801 | { |
| 802 | sector_t p_size = mdev->p_size; /* partner's disk size. */ |
| 803 | sector_t la_size = bdev->md.la_size_sect; /* last agreed size. */ |
| 804 | sector_t m_size; /* my size */ |
| 805 | sector_t u_size = bdev->dc.disk_size; /* size requested by user. */ |
| 806 | sector_t size = 0; |
| 807 | |
| 808 | m_size = drbd_get_max_capacity(bdev); |
| 809 | |
Philipp Reisner | a393db6 | 2009-12-22 13:35:52 +0100 | [diff] [blame] | 810 | if (mdev->state.conn < C_CONNECTED && assume_peer_has_space) { |
| 811 | dev_warn(DEV, "Resize while not connected was forced by the user!\n"); |
| 812 | p_size = m_size; |
| 813 | } |
| 814 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 815 | if (p_size && m_size) { |
| 816 | size = min_t(sector_t, p_size, m_size); |
| 817 | } else { |
| 818 | if (la_size) { |
| 819 | size = la_size; |
| 820 | if (m_size && m_size < size) |
| 821 | size = m_size; |
| 822 | if (p_size && p_size < size) |
| 823 | size = p_size; |
| 824 | } else { |
| 825 | if (m_size) |
| 826 | size = m_size; |
| 827 | if (p_size) |
| 828 | size = p_size; |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | if (size == 0) |
| 833 | dev_err(DEV, "Both nodes diskless!\n"); |
| 834 | |
| 835 | if (u_size) { |
| 836 | if (u_size > size) |
| 837 | dev_err(DEV, "Requested disk size is too big (%lu > %lu)\n", |
| 838 | (unsigned long)u_size>>1, (unsigned long)size>>1); |
| 839 | else |
| 840 | size = u_size; |
| 841 | } |
| 842 | |
| 843 | return size; |
| 844 | } |
| 845 | |
| 846 | /** |
| 847 | * drbd_check_al_size() - Ensures that the AL is of the right size |
| 848 | * @mdev: DRBD device. |
| 849 | * |
| 850 | * Returns -EBUSY if current al lru is still used, -ENOMEM when allocation |
| 851 | * failed, and 0 on success. You should call drbd_md_sync() after you called |
| 852 | * this function. |
| 853 | */ |
| 854 | static int drbd_check_al_size(struct drbd_conf *mdev) |
| 855 | { |
| 856 | struct lru_cache *n, *t; |
| 857 | struct lc_element *e; |
| 858 | unsigned int in_use; |
| 859 | int i; |
| 860 | |
Lars Ellenberg | 7ad651b | 2011-02-21 13:21:03 +0100 | [diff] [blame] | 861 | if (!expect(mdev->sync_conf.al_extents >= DRBD_AL_EXTENTS_MIN)) |
| 862 | mdev->sync_conf.al_extents = DRBD_AL_EXTENTS_MIN; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 863 | |
| 864 | if (mdev->act_log && |
| 865 | mdev->act_log->nr_elements == mdev->sync_conf.al_extents) |
| 866 | return 0; |
| 867 | |
| 868 | in_use = 0; |
| 869 | t = mdev->act_log; |
Lars Ellenberg | 7ad651b | 2011-02-21 13:21:03 +0100 | [diff] [blame] | 870 | 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] | 871 | mdev->sync_conf.al_extents, sizeof(struct lc_element), 0); |
| 872 | |
| 873 | if (n == NULL) { |
| 874 | dev_err(DEV, "Cannot allocate act_log lru!\n"); |
| 875 | return -ENOMEM; |
| 876 | } |
| 877 | spin_lock_irq(&mdev->al_lock); |
| 878 | if (t) { |
| 879 | for (i = 0; i < t->nr_elements; i++) { |
| 880 | e = lc_element_by_index(t, i); |
| 881 | if (e->refcnt) |
| 882 | dev_err(DEV, "refcnt(%d)==%d\n", |
| 883 | e->lc_number, e->refcnt); |
| 884 | in_use += e->refcnt; |
| 885 | } |
| 886 | } |
| 887 | if (!in_use) |
| 888 | mdev->act_log = n; |
| 889 | spin_unlock_irq(&mdev->al_lock); |
| 890 | if (in_use) { |
| 891 | dev_err(DEV, "Activity log still in use!\n"); |
| 892 | lc_destroy(n); |
| 893 | return -EBUSY; |
| 894 | } else { |
| 895 | if (t) |
| 896 | lc_destroy(t); |
| 897 | } |
| 898 | drbd_md_mark_dirty(mdev); /* we changed mdev->act_log->nr_elemens */ |
| 899 | return 0; |
| 900 | } |
| 901 | |
Philipp Reisner | 99432fc | 2011-05-20 16:39:13 +0200 | [diff] [blame] | 902 | 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] | 903 | { |
| 904 | struct request_queue * const q = mdev->rq_queue; |
Philipp Reisner | 99432fc | 2011-05-20 16:39:13 +0200 | [diff] [blame] | 905 | int max_hw_sectors = max_bio_size >> 9; |
| 906 | int max_segments = 0; |
| 907 | |
| 908 | if (get_ldev_if_state(mdev, D_ATTACHING)) { |
| 909 | struct request_queue * const b = mdev->ldev->backing_bdev->bd_disk->queue; |
| 910 | |
| 911 | max_hw_sectors = min(queue_max_hw_sectors(b), max_bio_size >> 9); |
| 912 | max_segments = mdev->ldev->dc.max_bio_bvecs; |
| 913 | put_ldev(mdev); |
| 914 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 915 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 916 | blk_queue_logical_block_size(q, 512); |
Lars Ellenberg | 1816a2b | 2010-11-11 15:19:07 +0100 | [diff] [blame] | 917 | blk_queue_max_hw_sectors(q, max_hw_sectors); |
| 918 | /* This is the workaround for "bio would need to, but cannot, be split" */ |
| 919 | blk_queue_max_segments(q, max_segments ? max_segments : BLK_MAX_SEGMENTS); |
| 920 | blk_queue_segment_boundary(q, PAGE_CACHE_SIZE-1); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 921 | |
Philipp Reisner | 99432fc | 2011-05-20 16:39:13 +0200 | [diff] [blame] | 922 | if (get_ldev_if_state(mdev, D_ATTACHING)) { |
| 923 | struct request_queue * const b = mdev->ldev->backing_bdev->bd_disk->queue; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 924 | |
Philipp Reisner | 99432fc | 2011-05-20 16:39:13 +0200 | [diff] [blame] | 925 | blk_queue_stack_limits(q, b); |
| 926 | |
| 927 | if (q->backing_dev_info.ra_pages != b->backing_dev_info.ra_pages) { |
| 928 | dev_info(DEV, "Adjusting my ra_pages to backing device's (%lu -> %lu)\n", |
| 929 | q->backing_dev_info.ra_pages, |
| 930 | b->backing_dev_info.ra_pages); |
| 931 | q->backing_dev_info.ra_pages = b->backing_dev_info.ra_pages; |
| 932 | } |
| 933 | put_ldev(mdev); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 934 | } |
| 935 | } |
| 936 | |
Philipp Reisner | 99432fc | 2011-05-20 16:39:13 +0200 | [diff] [blame] | 937 | void drbd_reconsider_max_bio_size(struct drbd_conf *mdev) |
| 938 | { |
| 939 | int now, new, local, peer; |
| 940 | |
| 941 | now = queue_max_hw_sectors(mdev->rq_queue) << 9; |
| 942 | local = mdev->local_max_bio_size; /* Eventually last known value, from volatile memory */ |
| 943 | peer = mdev->peer_max_bio_size; /* Eventually last known value, from meta data */ |
| 944 | |
| 945 | if (get_ldev_if_state(mdev, D_ATTACHING)) { |
| 946 | local = queue_max_hw_sectors(mdev->ldev->backing_bdev->bd_disk->queue) << 9; |
| 947 | mdev->local_max_bio_size = local; |
| 948 | put_ldev(mdev); |
| 949 | } |
| 950 | |
| 951 | /* We may ignore peer limits if the peer is modern enough. |
| 952 | Because new from 8.3.8 onwards the peer can use multiple |
| 953 | BIOs for a single peer_request */ |
| 954 | if (mdev->state.conn >= C_CONNECTED) { |
Philipp Reisner | 31890f4 | 2011-01-19 14:12:51 +0100 | [diff] [blame] | 955 | if (mdev->tconn->agreed_pro_version < 94) |
Philipp Reisner | 99432fc | 2011-05-20 16:39:13 +0200 | [diff] [blame] | 956 | peer = mdev->peer_max_bio_size; |
Philipp Reisner | 31890f4 | 2011-01-19 14:12:51 +0100 | [diff] [blame] | 957 | else if (mdev->tconn->agreed_pro_version == 94) |
Philipp Reisner | 99432fc | 2011-05-20 16:39:13 +0200 | [diff] [blame] | 958 | peer = DRBD_MAX_SIZE_H80_PACKET; |
| 959 | else /* drbd 8.3.8 onwards */ |
| 960 | peer = DRBD_MAX_BIO_SIZE; |
| 961 | } |
| 962 | |
| 963 | new = min_t(int, local, peer); |
| 964 | |
| 965 | if (mdev->state.role == R_PRIMARY && new < now) |
| 966 | dev_err(DEV, "ASSERT FAILED new < now; (%d < %d)\n", new, now); |
| 967 | |
| 968 | if (new != now) |
| 969 | dev_info(DEV, "max BIO size = %u\n", new); |
| 970 | |
| 971 | drbd_setup_queue_param(mdev, new); |
| 972 | } |
| 973 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 974 | /* serialize deconfig (worker exiting, doing cleanup) |
| 975 | * and reconfig (drbdsetup disk, drbdsetup net) |
| 976 | * |
Lars Ellenberg | c518d04 | 2010-09-01 09:50:23 +0200 | [diff] [blame] | 977 | * Wait for a potentially exiting worker, then restart it, |
| 978 | * or start a new one. Flush any pending work, there may still be an |
| 979 | * after_state_change queued. |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 980 | */ |
Philipp Reisner | 0e29d16 | 2011-02-18 14:23:11 +0100 | [diff] [blame] | 981 | static void conn_reconfig_start(struct drbd_tconn *tconn) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 982 | { |
Philipp Reisner | 0e29d16 | 2011-02-18 14:23:11 +0100 | [diff] [blame] | 983 | wait_event(tconn->ping_wait, !test_and_set_bit(CONFIG_PENDING, &tconn->flags)); |
| 984 | wait_event(tconn->ping_wait, !test_bit(OBJECT_DYING, &tconn->flags)); |
| 985 | drbd_thread_start(&tconn->worker); |
| 986 | conn_flush_workqueue(tconn); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | /* if still unconfigured, stops worker again. |
| 990 | * if configured now, clears CONFIG_PENDING. |
| 991 | * wakes potential waiters */ |
Philipp Reisner | 0e29d16 | 2011-02-18 14:23:11 +0100 | [diff] [blame] | 992 | static void conn_reconfig_done(struct drbd_tconn *tconn) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 993 | { |
Philipp Reisner | 0e29d16 | 2011-02-18 14:23:11 +0100 | [diff] [blame] | 994 | spin_lock_irq(&tconn->req_lock); |
| 995 | if (conn_all_vols_unconf(tconn)) { |
| 996 | set_bit(OBJECT_DYING, &tconn->flags); |
| 997 | drbd_thread_stop_nowait(&tconn->worker); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 998 | } else |
Philipp Reisner | 0e29d16 | 2011-02-18 14:23:11 +0100 | [diff] [blame] | 999 | clear_bit(CONFIG_PENDING, &tconn->flags); |
| 1000 | spin_unlock_irq(&tconn->req_lock); |
| 1001 | wake_up(&tconn->ping_wait); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1002 | } |
| 1003 | |
Philipp Reisner | 0778286 | 2010-08-31 12:00:50 +0200 | [diff] [blame] | 1004 | /* Make sure IO is suspended before calling this function(). */ |
| 1005 | static void drbd_suspend_al(struct drbd_conf *mdev) |
| 1006 | { |
| 1007 | int s = 0; |
| 1008 | |
Lars Ellenberg | 6161042 | 2011-02-21 13:20:54 +0100 | [diff] [blame] | 1009 | if (!lc_try_lock(mdev->act_log)) { |
Philipp Reisner | 0778286 | 2010-08-31 12:00:50 +0200 | [diff] [blame] | 1010 | dev_warn(DEV, "Failed to lock al in drbd_suspend_al()\n"); |
| 1011 | return; |
| 1012 | } |
| 1013 | |
Lars Ellenberg | 6161042 | 2011-02-21 13:20:54 +0100 | [diff] [blame] | 1014 | drbd_al_shrink(mdev); |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 1015 | spin_lock_irq(&mdev->tconn->req_lock); |
Philipp Reisner | 0778286 | 2010-08-31 12:00:50 +0200 | [diff] [blame] | 1016 | if (mdev->state.conn < C_CONNECTED) |
| 1017 | s = !test_and_set_bit(AL_SUSPENDED, &mdev->flags); |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 1018 | spin_unlock_irq(&mdev->tconn->req_lock); |
Lars Ellenberg | 6161042 | 2011-02-21 13:20:54 +0100 | [diff] [blame] | 1019 | lc_unlock(mdev->act_log); |
Philipp Reisner | 0778286 | 2010-08-31 12:00:50 +0200 | [diff] [blame] | 1020 | |
| 1021 | if (s) |
| 1022 | dev_info(DEV, "Suspended AL updates\n"); |
| 1023 | } |
| 1024 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 1025 | int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1026 | { |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 1027 | struct drbd_conf *mdev; |
| 1028 | int err; |
Andreas Gruenbacher | 116676c | 2010-12-08 13:33:11 +0100 | [diff] [blame] | 1029 | enum drbd_ret_code retcode; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1030 | enum determine_dev_size dd; |
| 1031 | sector_t max_possible_sectors; |
| 1032 | sector_t min_md_device_sectors; |
| 1033 | struct drbd_backing_dev *nbc = NULL; /* new_backing_conf */ |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1034 | struct block_device *bdev; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1035 | struct lru_cache *resync_lru = NULL; |
| 1036 | union drbd_state ns, os; |
Andreas Gruenbacher | f2024e7 | 2010-12-10 13:44:05 +0100 | [diff] [blame] | 1037 | enum drbd_state_rv rv; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1038 | int cp_discovered = 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1039 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 1040 | retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR); |
| 1041 | if (!adm_ctx.reply_skb) |
| 1042 | return retcode; |
| 1043 | if (retcode != NO_ERROR) |
Lars Ellenberg | 40cbf08 | 2011-03-16 16:52:10 +0100 | [diff] [blame^] | 1044 | goto finish; |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 1045 | |
| 1046 | mdev = adm_ctx.mdev; |
Philipp Reisner | 0e29d16 | 2011-02-18 14:23:11 +0100 | [diff] [blame] | 1047 | conn_reconfig_start(mdev->tconn); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1048 | |
| 1049 | /* if you want to reconfigure, please tear down first */ |
| 1050 | if (mdev->state.disk > D_DISKLESS) { |
| 1051 | retcode = ERR_DISK_CONFIGURED; |
| 1052 | goto fail; |
| 1053 | } |
Lars Ellenberg | 82f59cc | 2010-10-16 12:13:47 +0200 | [diff] [blame] | 1054 | /* It may just now have detached because of IO error. Make sure |
| 1055 | * drbd_ldev_destroy is done already, we may end up here very fast, |
| 1056 | * e.g. if someone calls attach from the on-io-error handler, |
| 1057 | * to realize a "hot spare" feature (not that I'd recommend that) */ |
| 1058 | wait_event(mdev->misc_wait, !atomic_read(&mdev->local_cnt)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1059 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 1060 | /* allocation not in the IO path, drbdsetup context */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1061 | nbc = kzalloc(sizeof(struct drbd_backing_dev), GFP_KERNEL); |
| 1062 | if (!nbc) { |
| 1063 | retcode = ERR_NOMEM; |
| 1064 | goto fail; |
| 1065 | } |
| 1066 | |
| 1067 | nbc->dc.disk_size = DRBD_DISK_SIZE_SECT_DEF; |
| 1068 | nbc->dc.on_io_error = DRBD_ON_IO_ERROR_DEF; |
| 1069 | nbc->dc.fencing = DRBD_FENCING_DEF; |
| 1070 | nbc->dc.max_bio_bvecs = DRBD_MAX_BIO_BVECS_DEF; |
| 1071 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 1072 | err = disk_conf_from_attrs(&nbc->dc, info->attrs); |
| 1073 | if (err) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1074 | retcode = ERR_MANDATORY_TAG; |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 1075 | drbd_msg_put_info(from_attrs_err_to_txt(err)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1076 | goto fail; |
| 1077 | } |
| 1078 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 1079 | if ((int)nbc->dc.meta_dev_idx < DRBD_MD_INDEX_FLEX_INT) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1080 | retcode = ERR_MD_IDX_INVALID; |
| 1081 | goto fail; |
| 1082 | } |
| 1083 | |
Philipp Reisner | b2fb6dbe | 2011-01-19 13:48:44 +0100 | [diff] [blame] | 1084 | if (get_net_conf(mdev->tconn)) { |
Philipp Reisner | 89e58e7 | 2011-01-19 13:12:45 +0100 | [diff] [blame] | 1085 | int prot = mdev->tconn->net_conf->wire_protocol; |
Philipp Reisner | b2fb6dbe | 2011-01-19 13:48:44 +0100 | [diff] [blame] | 1086 | put_net_conf(mdev->tconn); |
Philipp Reisner | 47ff2d0 | 2010-06-18 13:56:57 +0200 | [diff] [blame] | 1087 | if (nbc->dc.fencing == FP_STONITH && prot == DRBD_PROT_A) { |
| 1088 | retcode = ERR_STONITH_AND_PROT_A; |
| 1089 | goto fail; |
| 1090 | } |
| 1091 | } |
| 1092 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1093 | bdev = blkdev_get_by_path(nbc->dc.backing_dev, |
| 1094 | FMODE_READ | FMODE_WRITE | FMODE_EXCL, mdev); |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1095 | if (IS_ERR(bdev)) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1096 | 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] | 1097 | PTR_ERR(bdev)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1098 | retcode = ERR_OPEN_DISK; |
| 1099 | goto fail; |
| 1100 | } |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1101 | nbc->backing_bdev = bdev; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1102 | |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1103 | /* |
| 1104 | * meta_dev_idx >= 0: external fixed size, possibly multiple |
| 1105 | * drbd sharing one meta device. TODO in that case, paranoia |
| 1106 | * check that [md_bdev, meta_dev_idx] is not yet used by some |
| 1107 | * other drbd minor! (if you use drbd.conf + drbdadm, that |
| 1108 | * should check it for you already; but if you don't, or |
| 1109 | * someone fooled it, we need to double check here) |
| 1110 | */ |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1111 | bdev = blkdev_get_by_path(nbc->dc.meta_dev, |
| 1112 | FMODE_READ | FMODE_WRITE | FMODE_EXCL, |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 1113 | ((int)nbc->dc.meta_dev_idx < 0) ? |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1114 | (void *)mdev : (void *)drbd_m_holder); |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1115 | if (IS_ERR(bdev)) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1116 | 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] | 1117 | PTR_ERR(bdev)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1118 | retcode = ERR_OPEN_MD_DISK; |
| 1119 | goto fail; |
| 1120 | } |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1121 | nbc->md_bdev = bdev; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1122 | |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1123 | if ((nbc->backing_bdev == nbc->md_bdev) != |
| 1124 | (nbc->dc.meta_dev_idx == DRBD_MD_INDEX_INTERNAL || |
| 1125 | nbc->dc.meta_dev_idx == DRBD_MD_INDEX_FLEX_INT)) { |
| 1126 | retcode = ERR_MD_IDX_INVALID; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1127 | goto fail; |
| 1128 | } |
| 1129 | |
| 1130 | resync_lru = lc_create("resync", drbd_bm_ext_cache, |
Lars Ellenberg | 46a15bc | 2011-02-21 13:21:01 +0100 | [diff] [blame] | 1131 | 1, 61, sizeof(struct bm_extent), |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1132 | offsetof(struct bm_extent, lce)); |
| 1133 | if (!resync_lru) { |
| 1134 | retcode = ERR_NOMEM; |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1135 | goto fail; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | /* RT - for drbd_get_max_capacity() DRBD_MD_INDEX_FLEX_INT */ |
| 1139 | drbd_md_set_sector_offsets(mdev, nbc); |
| 1140 | |
| 1141 | if (drbd_get_max_capacity(nbc) < nbc->dc.disk_size) { |
| 1142 | dev_err(DEV, "max capacity %llu smaller than disk size %llu\n", |
| 1143 | (unsigned long long) drbd_get_max_capacity(nbc), |
| 1144 | (unsigned long long) nbc->dc.disk_size); |
| 1145 | retcode = ERR_DISK_TO_SMALL; |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1146 | goto fail; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1147 | } |
| 1148 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 1149 | if ((int)nbc->dc.meta_dev_idx < 0) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1150 | max_possible_sectors = DRBD_MAX_SECTORS_FLEX; |
| 1151 | /* at least one MB, otherwise it does not make sense */ |
| 1152 | min_md_device_sectors = (2<<10); |
| 1153 | } else { |
| 1154 | max_possible_sectors = DRBD_MAX_SECTORS; |
| 1155 | min_md_device_sectors = MD_RESERVED_SECT * (nbc->dc.meta_dev_idx + 1); |
| 1156 | } |
| 1157 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1158 | if (drbd_get_capacity(nbc->md_bdev) < min_md_device_sectors) { |
| 1159 | retcode = ERR_MD_DISK_TO_SMALL; |
| 1160 | dev_warn(DEV, "refusing attach: md-device too small, " |
| 1161 | "at least %llu sectors needed for this meta-disk type\n", |
| 1162 | (unsigned long long) min_md_device_sectors); |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1163 | goto fail; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | /* Make sure the new disk is big enough |
| 1167 | * (we may currently be R_PRIMARY with no local disk...) */ |
| 1168 | if (drbd_get_max_capacity(nbc) < |
| 1169 | drbd_get_capacity(mdev->this_bdev)) { |
| 1170 | retcode = ERR_DISK_TO_SMALL; |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1171 | goto fail; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1172 | } |
| 1173 | |
| 1174 | nbc->known_size = drbd_get_capacity(nbc->backing_bdev); |
| 1175 | |
Lars Ellenberg | 1352994 | 2009-10-12 19:07:49 +0200 | [diff] [blame] | 1176 | if (nbc->known_size > max_possible_sectors) { |
| 1177 | dev_warn(DEV, "==> truncating very big lower level device " |
| 1178 | "to currently maximum possible %llu sectors <==\n", |
| 1179 | (unsigned long long) max_possible_sectors); |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 1180 | if ((int)nbc->dc.meta_dev_idx >= 0) |
Lars Ellenberg | 1352994 | 2009-10-12 19:07:49 +0200 | [diff] [blame] | 1181 | dev_warn(DEV, "==>> using internal or flexible " |
| 1182 | "meta data may help <<==\n"); |
| 1183 | } |
| 1184 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1185 | drbd_suspend_io(mdev); |
| 1186 | /* also wait for the last barrier ack. */ |
Philipp Reisner | fb22c40 | 2010-09-08 23:20:21 +0200 | [diff] [blame] | 1187 | 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] | 1188 | /* and for any other previously queued work */ |
Philipp Reisner | a21e929 | 2011-02-08 15:08:49 +0100 | [diff] [blame] | 1189 | drbd_flush_workqueue(mdev); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1190 | |
Andreas Gruenbacher | f2024e7 | 2010-12-10 13:44:05 +0100 | [diff] [blame] | 1191 | rv = _drbd_request_state(mdev, NS(disk, D_ATTACHING), CS_VERBOSE); |
| 1192 | retcode = rv; /* FIXME: Type mismatch. */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1193 | drbd_resume_io(mdev); |
Andreas Gruenbacher | f2024e7 | 2010-12-10 13:44:05 +0100 | [diff] [blame] | 1194 | if (rv < SS_SUCCESS) |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1195 | goto fail; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1196 | |
| 1197 | if (!get_ldev_if_state(mdev, D_ATTACHING)) |
| 1198 | goto force_diskless; |
| 1199 | |
| 1200 | drbd_md_set_sector_offsets(mdev, nbc); |
| 1201 | |
| 1202 | if (!mdev->bitmap) { |
| 1203 | if (drbd_bm_init(mdev)) { |
| 1204 | retcode = ERR_NOMEM; |
| 1205 | goto force_diskless_dec; |
| 1206 | } |
| 1207 | } |
| 1208 | |
| 1209 | retcode = drbd_md_read(mdev, nbc); |
| 1210 | if (retcode != NO_ERROR) |
| 1211 | goto force_diskless_dec; |
| 1212 | |
| 1213 | if (mdev->state.conn < C_CONNECTED && |
| 1214 | mdev->state.role == R_PRIMARY && |
| 1215 | (mdev->ed_uuid & ~((u64)1)) != (nbc->md.uuid[UI_CURRENT] & ~((u64)1))) { |
| 1216 | dev_err(DEV, "Can only attach to data with current UUID=%016llX\n", |
| 1217 | (unsigned long long)mdev->ed_uuid); |
| 1218 | retcode = ERR_DATA_NOT_CURRENT; |
| 1219 | goto force_diskless_dec; |
| 1220 | } |
| 1221 | |
| 1222 | /* Since we are diskless, fix the activity log first... */ |
| 1223 | if (drbd_check_al_size(mdev)) { |
| 1224 | retcode = ERR_NOMEM; |
| 1225 | goto force_diskless_dec; |
| 1226 | } |
| 1227 | |
| 1228 | /* Prevent shrinking of consistent devices ! */ |
| 1229 | if (drbd_md_test_flag(nbc, MDF_CONSISTENT) && |
Philipp Reisner | a393db6 | 2009-12-22 13:35:52 +0100 | [diff] [blame] | 1230 | drbd_new_dev_size(mdev, nbc, 0) < nbc->md.la_size_sect) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1231 | dev_warn(DEV, "refusing to truncate a consistent device\n"); |
| 1232 | retcode = ERR_DISK_TO_SMALL; |
| 1233 | goto force_diskless_dec; |
| 1234 | } |
| 1235 | |
| 1236 | if (!drbd_al_read_log(mdev, nbc)) { |
| 1237 | retcode = ERR_IO_MD_DISK; |
| 1238 | goto force_diskless_dec; |
| 1239 | } |
| 1240 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1241 | /* Reset the "barriers don't work" bits here, then force meta data to |
| 1242 | * be written, to ensure we determine if barriers are supported. */ |
| 1243 | if (nbc->dc.no_md_flush) |
Philipp Reisner | a8a4e51 | 2010-08-25 10:21:04 +0200 | [diff] [blame] | 1244 | set_bit(MD_NO_FUA, &mdev->flags); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1245 | else |
Philipp Reisner | a8a4e51 | 2010-08-25 10:21:04 +0200 | [diff] [blame] | 1246 | clear_bit(MD_NO_FUA, &mdev->flags); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1247 | |
| 1248 | /* Point of no return reached. |
| 1249 | * Devices and memory are no longer released by error cleanup below. |
| 1250 | * now mdev takes over responsibility, and the state engine should |
| 1251 | * clean it up somewhere. */ |
| 1252 | D_ASSERT(mdev->ldev == NULL); |
| 1253 | mdev->ldev = nbc; |
| 1254 | mdev->resync = resync_lru; |
| 1255 | nbc = NULL; |
| 1256 | resync_lru = NULL; |
| 1257 | |
Philipp Reisner | 2451fc3 | 2010-08-24 13:43:11 +0200 | [diff] [blame] | 1258 | mdev->write_ordering = WO_bdev_flush; |
| 1259 | drbd_bump_write_ordering(mdev, WO_bdev_flush); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1260 | |
| 1261 | if (drbd_md_test_flag(mdev->ldev, MDF_CRASHED_PRIMARY)) |
| 1262 | set_bit(CRASHED_PRIMARY, &mdev->flags); |
| 1263 | else |
| 1264 | clear_bit(CRASHED_PRIMARY, &mdev->flags); |
| 1265 | |
Philipp Reisner | 894c6a9 | 2010-06-18 16:03:20 +0200 | [diff] [blame] | 1266 | if (drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) && |
Philipp Reisner | fb22c40 | 2010-09-08 23:20:21 +0200 | [diff] [blame] | 1267 | !(mdev->state.role == R_PRIMARY && mdev->state.susp_nod)) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1268 | set_bit(CRASHED_PRIMARY, &mdev->flags); |
| 1269 | cp_discovered = 1; |
| 1270 | } |
| 1271 | |
| 1272 | mdev->send_cnt = 0; |
| 1273 | mdev->recv_cnt = 0; |
| 1274 | mdev->read_cnt = 0; |
| 1275 | mdev->writ_cnt = 0; |
| 1276 | |
Philipp Reisner | 99432fc | 2011-05-20 16:39:13 +0200 | [diff] [blame] | 1277 | drbd_reconsider_max_bio_size(mdev); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1278 | |
| 1279 | /* If I am currently not R_PRIMARY, |
| 1280 | * but meta data primary indicator is set, |
| 1281 | * I just now recover from a hard crash, |
| 1282 | * and have been R_PRIMARY before that crash. |
| 1283 | * |
| 1284 | * Now, if I had no connection before that crash |
| 1285 | * (have been degraded R_PRIMARY), chances are that |
| 1286 | * I won't find my peer now either. |
| 1287 | * |
| 1288 | * In that case, and _only_ in that case, |
| 1289 | * we use the degr-wfc-timeout instead of the default, |
| 1290 | * so we can automatically recover from a crash of a |
| 1291 | * degraded but active "cluster" after a certain timeout. |
| 1292 | */ |
| 1293 | clear_bit(USE_DEGR_WFC_T, &mdev->flags); |
| 1294 | if (mdev->state.role != R_PRIMARY && |
| 1295 | drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) && |
| 1296 | !drbd_md_test_flag(mdev->ldev, MDF_CONNECTED_IND)) |
| 1297 | set_bit(USE_DEGR_WFC_T, &mdev->flags); |
| 1298 | |
Bart Van Assche | 24c4830 | 2011-05-21 18:32:29 +0200 | [diff] [blame] | 1299 | dd = drbd_determine_dev_size(mdev, 0); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1300 | if (dd == dev_size_error) { |
| 1301 | retcode = ERR_NOMEM_BITMAP; |
| 1302 | goto force_diskless_dec; |
| 1303 | } else if (dd == grew) |
| 1304 | set_bit(RESYNC_AFTER_NEG, &mdev->flags); |
| 1305 | |
| 1306 | if (drbd_md_test_flag(mdev->ldev, MDF_FULL_SYNC)) { |
| 1307 | dev_info(DEV, "Assuming that all blocks are out of sync " |
| 1308 | "(aka FullSync)\n"); |
Lars Ellenberg | 20ceb2b | 2011-01-21 10:56:44 +0100 | [diff] [blame] | 1309 | if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write, |
| 1310 | "set_n_write from attaching", BM_LOCKED_MASK)) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1311 | retcode = ERR_IO_MD_DISK; |
| 1312 | goto force_diskless_dec; |
| 1313 | } |
| 1314 | } else { |
Lars Ellenberg | 20ceb2b | 2011-01-21 10:56:44 +0100 | [diff] [blame] | 1315 | if (drbd_bitmap_io(mdev, &drbd_bm_read, |
| 1316 | "read from attaching", BM_LOCKED_MASK) < 0) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1317 | retcode = ERR_IO_MD_DISK; |
| 1318 | goto force_diskless_dec; |
| 1319 | } |
| 1320 | } |
| 1321 | |
| 1322 | if (cp_discovered) { |
| 1323 | drbd_al_apply_to_bm(mdev); |
Lars Ellenberg | 20ceb2b | 2011-01-21 10:56:44 +0100 | [diff] [blame] | 1324 | if (drbd_bitmap_io(mdev, &drbd_bm_write, |
| 1325 | "crashed primary apply AL", BM_LOCKED_MASK)) { |
Lars Ellenberg | 19f843a | 2010-12-15 08:59:11 +0100 | [diff] [blame] | 1326 | retcode = ERR_IO_MD_DISK; |
| 1327 | goto force_diskless_dec; |
| 1328 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1329 | } |
| 1330 | |
Philipp Reisner | 0778286 | 2010-08-31 12:00:50 +0200 | [diff] [blame] | 1331 | if (_drbd_bm_total_weight(mdev) == drbd_bm_bits(mdev)) |
| 1332 | drbd_suspend_al(mdev); /* IO is still suspended here... */ |
| 1333 | |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 1334 | spin_lock_irq(&mdev->tconn->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1335 | os = mdev->state; |
| 1336 | ns.i = os.i; |
| 1337 | /* If MDF_CONSISTENT is not set go into inconsistent state, |
| 1338 | otherwise investigate MDF_WasUpToDate... |
| 1339 | If MDF_WAS_UP_TO_DATE is not set go into D_OUTDATED disk state, |
| 1340 | otherwise into D_CONSISTENT state. |
| 1341 | */ |
| 1342 | if (drbd_md_test_flag(mdev->ldev, MDF_CONSISTENT)) { |
| 1343 | if (drbd_md_test_flag(mdev->ldev, MDF_WAS_UP_TO_DATE)) |
| 1344 | ns.disk = D_CONSISTENT; |
| 1345 | else |
| 1346 | ns.disk = D_OUTDATED; |
| 1347 | } else { |
| 1348 | ns.disk = D_INCONSISTENT; |
| 1349 | } |
| 1350 | |
| 1351 | if (drbd_md_test_flag(mdev->ldev, MDF_PEER_OUT_DATED)) |
| 1352 | ns.pdsk = D_OUTDATED; |
| 1353 | |
| 1354 | if ( ns.disk == D_CONSISTENT && |
| 1355 | (ns.pdsk == D_OUTDATED || mdev->ldev->dc.fencing == FP_DONT_CARE)) |
| 1356 | ns.disk = D_UP_TO_DATE; |
| 1357 | |
| 1358 | /* All tests on MDF_PRIMARY_IND, MDF_CONNECTED_IND, |
| 1359 | MDF_CONSISTENT and MDF_WAS_UP_TO_DATE must happen before |
| 1360 | this point, because drbd_request_state() modifies these |
| 1361 | flags. */ |
| 1362 | |
| 1363 | /* In case we are C_CONNECTED postpone any decision on the new disk |
| 1364 | state after the negotiation phase. */ |
| 1365 | if (mdev->state.conn == C_CONNECTED) { |
| 1366 | mdev->new_state_tmp.i = ns.i; |
| 1367 | ns.i = os.i; |
| 1368 | ns.disk = D_NEGOTIATING; |
Philipp Reisner | dc66c74 | 2010-06-02 14:31:29 +0200 | [diff] [blame] | 1369 | |
| 1370 | /* We expect to receive up-to-date UUIDs soon. |
| 1371 | To avoid a race in receive_state, free p_uuid while |
| 1372 | holding req_lock. I.e. atomic with the state change */ |
| 1373 | kfree(mdev->p_uuid); |
| 1374 | mdev->p_uuid = NULL; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1375 | } |
| 1376 | |
| 1377 | rv = _drbd_set_state(mdev, ns, CS_VERBOSE, NULL); |
| 1378 | ns = mdev->state; |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 1379 | spin_unlock_irq(&mdev->tconn->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1380 | |
| 1381 | if (rv < SS_SUCCESS) |
| 1382 | goto force_diskless_dec; |
| 1383 | |
| 1384 | if (mdev->state.role == R_PRIMARY) |
| 1385 | mdev->ldev->md.uuid[UI_CURRENT] |= (u64)1; |
| 1386 | else |
| 1387 | mdev->ldev->md.uuid[UI_CURRENT] &= ~(u64)1; |
| 1388 | |
| 1389 | drbd_md_mark_dirty(mdev); |
| 1390 | drbd_md_sync(mdev); |
| 1391 | |
| 1392 | kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE); |
| 1393 | put_ldev(mdev); |
Philipp Reisner | 0e29d16 | 2011-02-18 14:23:11 +0100 | [diff] [blame] | 1394 | conn_reconfig_done(mdev->tconn); |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 1395 | drbd_adm_finish(info, retcode); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1396 | return 0; |
| 1397 | |
| 1398 | force_diskless_dec: |
| 1399 | put_ldev(mdev); |
| 1400 | force_diskless: |
Lars Ellenberg | 82f59cc | 2010-10-16 12:13:47 +0200 | [diff] [blame] | 1401 | drbd_force_state(mdev, NS(disk, D_FAILED)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1402 | drbd_md_sync(mdev); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1403 | fail: |
Lars Ellenberg | 40cbf08 | 2011-03-16 16:52:10 +0100 | [diff] [blame^] | 1404 | conn_reconfig_done(mdev->tconn); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1405 | if (nbc) { |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1406 | if (nbc->backing_bdev) |
| 1407 | blkdev_put(nbc->backing_bdev, |
| 1408 | FMODE_READ | FMODE_WRITE | FMODE_EXCL); |
| 1409 | if (nbc->md_bdev) |
| 1410 | blkdev_put(nbc->md_bdev, |
| 1411 | FMODE_READ | FMODE_WRITE | FMODE_EXCL); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1412 | kfree(nbc); |
| 1413 | } |
| 1414 | lc_destroy(resync_lru); |
| 1415 | |
Lars Ellenberg | 40cbf08 | 2011-03-16 16:52:10 +0100 | [diff] [blame^] | 1416 | finish: |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 1417 | drbd_adm_finish(info, retcode); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1418 | return 0; |
| 1419 | } |
| 1420 | |
Lars Ellenberg | 85f75dd7 | 2011-03-15 16:26:37 +0100 | [diff] [blame] | 1421 | static int adm_detach(struct drbd_conf *mdev) |
| 1422 | { |
| 1423 | enum drbd_ret_code retcode; |
| 1424 | drbd_suspend_io(mdev); /* so no-one is stuck in drbd_al_begin_io */ |
| 1425 | retcode = drbd_request_state(mdev, NS(disk, D_DISKLESS)); |
| 1426 | wait_event(mdev->misc_wait, |
| 1427 | mdev->state.disk != D_DISKLESS || |
| 1428 | !atomic_read(&mdev->local_cnt)); |
| 1429 | drbd_resume_io(mdev); |
| 1430 | return retcode; |
| 1431 | } |
| 1432 | |
Lars Ellenberg | 82f59cc | 2010-10-16 12:13:47 +0200 | [diff] [blame] | 1433 | /* Detaching the disk is a process in multiple stages. First we need to lock |
| 1434 | * out application IO, in-flight IO, IO stuck in drbd_al_begin_io. |
| 1435 | * Then we transition to D_DISKLESS, and wait for put_ldev() to return all |
| 1436 | * internal references as well. |
| 1437 | * Only then we have finally detached. */ |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 1438 | int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1439 | { |
Lars Ellenberg | 9a0d9d0 | 2011-05-02 11:51:31 +0200 | [diff] [blame] | 1440 | enum drbd_ret_code retcode; |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 1441 | |
| 1442 | retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR); |
| 1443 | if (!adm_ctx.reply_skb) |
| 1444 | return retcode; |
| 1445 | if (retcode != NO_ERROR) |
| 1446 | goto out; |
| 1447 | |
Lars Ellenberg | 85f75dd7 | 2011-03-15 16:26:37 +0100 | [diff] [blame] | 1448 | retcode = adm_detach(adm_ctx.mdev); |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 1449 | out: |
| 1450 | drbd_adm_finish(info, retcode); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1451 | return 0; |
| 1452 | } |
| 1453 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 1454 | int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1455 | { |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 1456 | char hmac_name[CRYPTO_MAX_ALG_NAME]; |
| 1457 | struct drbd_conf *mdev; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1458 | struct net_conf *new_conf = NULL; |
| 1459 | struct crypto_hash *tfm = NULL; |
| 1460 | struct crypto_hash *integrity_w_tfm = NULL; |
| 1461 | struct crypto_hash *integrity_r_tfm = NULL; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1462 | void *int_dig_out = NULL; |
| 1463 | void *int_dig_in = NULL; |
| 1464 | void *int_dig_vv = NULL; |
Philipp Reisner | 8088319 | 2011-02-18 14:56:45 +0100 | [diff] [blame] | 1465 | struct drbd_tconn *oconn; |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 1466 | struct drbd_tconn *tconn; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1467 | struct sockaddr *new_my_addr, *new_peer_addr, *taken_addr; |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 1468 | enum drbd_ret_code retcode; |
| 1469 | int i; |
| 1470 | int err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1471 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 1472 | retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONN); |
| 1473 | if (!adm_ctx.reply_skb) |
| 1474 | return retcode; |
| 1475 | if (retcode != NO_ERROR) |
| 1476 | goto out; |
| 1477 | |
| 1478 | tconn = adm_ctx.tconn; |
Philipp Reisner | 8088319 | 2011-02-18 14:56:45 +0100 | [diff] [blame] | 1479 | conn_reconfig_start(tconn); |
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 | if (tconn->cstate > C_STANDALONE) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1482 | retcode = ERR_NET_CONFIGURED; |
| 1483 | goto fail; |
| 1484 | } |
| 1485 | |
| 1486 | /* allocation not in the IO path, cqueue thread context */ |
Julia Lawall | 2db4e42 | 2010-05-13 22:02:21 +0200 | [diff] [blame] | 1487 | new_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1488 | if (!new_conf) { |
| 1489 | retcode = ERR_NOMEM; |
| 1490 | goto fail; |
| 1491 | } |
| 1492 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1493 | new_conf->timeout = DRBD_TIMEOUT_DEF; |
| 1494 | new_conf->try_connect_int = DRBD_CONNECT_INT_DEF; |
| 1495 | new_conf->ping_int = DRBD_PING_INT_DEF; |
| 1496 | new_conf->max_epoch_size = DRBD_MAX_EPOCH_SIZE_DEF; |
| 1497 | new_conf->max_buffers = DRBD_MAX_BUFFERS_DEF; |
| 1498 | new_conf->unplug_watermark = DRBD_UNPLUG_WATERMARK_DEF; |
| 1499 | new_conf->sndbuf_size = DRBD_SNDBUF_SIZE_DEF; |
| 1500 | new_conf->rcvbuf_size = DRBD_RCVBUF_SIZE_DEF; |
| 1501 | new_conf->ko_count = DRBD_KO_COUNT_DEF; |
| 1502 | new_conf->after_sb_0p = DRBD_AFTER_SB_0P_DEF; |
| 1503 | new_conf->after_sb_1p = DRBD_AFTER_SB_1P_DEF; |
| 1504 | new_conf->after_sb_2p = DRBD_AFTER_SB_2P_DEF; |
| 1505 | new_conf->want_lose = 0; |
| 1506 | new_conf->two_primaries = 0; |
| 1507 | new_conf->wire_protocol = DRBD_PROT_C; |
| 1508 | new_conf->ping_timeo = DRBD_PING_TIMEO_DEF; |
| 1509 | new_conf->rr_conflict = DRBD_RR_CONFLICT_DEF; |
Philipp Reisner | 422028b | 2010-10-27 11:12:07 +0200 | [diff] [blame] | 1510 | new_conf->on_congestion = DRBD_ON_CONGESTION_DEF; |
| 1511 | new_conf->cong_extents = DRBD_CONG_EXTENTS_DEF; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1512 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 1513 | err = net_conf_from_attrs(new_conf, info->attrs); |
| 1514 | if (err) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1515 | retcode = ERR_MANDATORY_TAG; |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 1516 | drbd_msg_put_info(from_attrs_err_to_txt(err)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1517 | goto fail; |
| 1518 | } |
| 1519 | |
| 1520 | if (new_conf->two_primaries |
| 1521 | && (new_conf->wire_protocol != DRBD_PROT_C)) { |
| 1522 | retcode = ERR_NOT_PROTO_C; |
| 1523 | goto fail; |
Philipp Reisner | 47ff2d0 | 2010-06-18 13:56:57 +0200 | [diff] [blame] | 1524 | } |
| 1525 | |
Philipp Reisner | 8088319 | 2011-02-18 14:56:45 +0100 | [diff] [blame] | 1526 | idr_for_each_entry(&tconn->volumes, mdev, i) { |
| 1527 | if (get_ldev(mdev)) { |
| 1528 | enum drbd_fencing_p fp = mdev->ldev->dc.fencing; |
| 1529 | put_ldev(mdev); |
| 1530 | if (new_conf->wire_protocol == DRBD_PROT_A && fp == FP_STONITH) { |
| 1531 | retcode = ERR_STONITH_AND_PROT_A; |
| 1532 | goto fail; |
| 1533 | } |
| 1534 | } |
| 1535 | if (mdev->state.role == R_PRIMARY && new_conf->want_lose) { |
| 1536 | retcode = ERR_DISCARD; |
Philipp Reisner | 47ff2d0 | 2010-06-18 13:56:57 +0200 | [diff] [blame] | 1537 | goto fail; |
| 1538 | } |
Philipp Reisner | 8088319 | 2011-02-18 14:56:45 +0100 | [diff] [blame] | 1539 | if (!mdev->bitmap) { |
| 1540 | if(drbd_bm_init(mdev)) { |
| 1541 | retcode = ERR_NOMEM; |
| 1542 | goto fail; |
| 1543 | } |
| 1544 | } |
Philipp Reisner | 47ff2d0 | 2010-06-18 13:56:57 +0200 | [diff] [blame] | 1545 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1546 | |
Philipp Reisner | 422028b | 2010-10-27 11:12:07 +0200 | [diff] [blame] | 1547 | if (new_conf->on_congestion != OC_BLOCK && new_conf->wire_protocol != DRBD_PROT_A) { |
| 1548 | retcode = ERR_CONG_NOT_PROTO_A; |
| 1549 | goto fail; |
| 1550 | } |
| 1551 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1552 | retcode = NO_ERROR; |
| 1553 | |
| 1554 | new_my_addr = (struct sockaddr *)&new_conf->my_addr; |
| 1555 | new_peer_addr = (struct sockaddr *)&new_conf->peer_addr; |
Lars Ellenberg | 543cc10 | 2011-03-10 22:18:18 +0100 | [diff] [blame] | 1556 | |
| 1557 | /* No need to take drbd_cfg_mutex here. All reconfiguration is |
| 1558 | * strictly serialized on genl_lock(). We are protected against |
| 1559 | * concurrent reconfiguration/addition/deletion */ |
Philipp Reisner | 8088319 | 2011-02-18 14:56:45 +0100 | [diff] [blame] | 1560 | list_for_each_entry(oconn, &drbd_tconns, all_tconn) { |
| 1561 | if (oconn == tconn) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1562 | continue; |
Philipp Reisner | 8088319 | 2011-02-18 14:56:45 +0100 | [diff] [blame] | 1563 | if (get_net_conf(oconn)) { |
| 1564 | taken_addr = (struct sockaddr *)&oconn->net_conf->my_addr; |
| 1565 | if (new_conf->my_addr_len == oconn->net_conf->my_addr_len && |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1566 | !memcmp(new_my_addr, taken_addr, new_conf->my_addr_len)) |
| 1567 | retcode = ERR_LOCAL_ADDR; |
| 1568 | |
Philipp Reisner | 8088319 | 2011-02-18 14:56:45 +0100 | [diff] [blame] | 1569 | taken_addr = (struct sockaddr *)&oconn->net_conf->peer_addr; |
| 1570 | if (new_conf->peer_addr_len == oconn->net_conf->peer_addr_len && |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1571 | !memcmp(new_peer_addr, taken_addr, new_conf->peer_addr_len)) |
| 1572 | retcode = ERR_PEER_ADDR; |
| 1573 | |
Philipp Reisner | 8088319 | 2011-02-18 14:56:45 +0100 | [diff] [blame] | 1574 | put_net_conf(oconn); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1575 | if (retcode != NO_ERROR) |
| 1576 | goto fail; |
| 1577 | } |
| 1578 | } |
| 1579 | |
| 1580 | if (new_conf->cram_hmac_alg[0] != 0) { |
| 1581 | snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)", |
| 1582 | new_conf->cram_hmac_alg); |
| 1583 | tfm = crypto_alloc_hash(hmac_name, 0, CRYPTO_ALG_ASYNC); |
| 1584 | if (IS_ERR(tfm)) { |
| 1585 | tfm = NULL; |
| 1586 | retcode = ERR_AUTH_ALG; |
| 1587 | goto fail; |
| 1588 | } |
| 1589 | |
Philipp Reisner | 0798219 | 2009-12-28 16:58:38 +0100 | [diff] [blame] | 1590 | if (!drbd_crypto_is_hash(crypto_hash_tfm(tfm))) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1591 | retcode = ERR_AUTH_ALG_ND; |
| 1592 | goto fail; |
| 1593 | } |
| 1594 | } |
| 1595 | |
| 1596 | if (new_conf->integrity_alg[0]) { |
| 1597 | integrity_w_tfm = crypto_alloc_hash(new_conf->integrity_alg, 0, CRYPTO_ALG_ASYNC); |
| 1598 | if (IS_ERR(integrity_w_tfm)) { |
| 1599 | integrity_w_tfm = NULL; |
| 1600 | retcode=ERR_INTEGRITY_ALG; |
| 1601 | goto fail; |
| 1602 | } |
| 1603 | |
| 1604 | if (!drbd_crypto_is_hash(crypto_hash_tfm(integrity_w_tfm))) { |
| 1605 | retcode=ERR_INTEGRITY_ALG_ND; |
| 1606 | goto fail; |
| 1607 | } |
| 1608 | |
| 1609 | integrity_r_tfm = crypto_alloc_hash(new_conf->integrity_alg, 0, CRYPTO_ALG_ASYNC); |
| 1610 | if (IS_ERR(integrity_r_tfm)) { |
| 1611 | integrity_r_tfm = NULL; |
| 1612 | retcode=ERR_INTEGRITY_ALG; |
| 1613 | goto fail; |
| 1614 | } |
| 1615 | } |
| 1616 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1617 | ((char *)new_conf->shared_secret)[SHARED_SECRET_MAX-1] = 0; |
| 1618 | |
Philipp Reisner | 8088319 | 2011-02-18 14:56:45 +0100 | [diff] [blame] | 1619 | /* allocation not in the IO path, cqueue thread context */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1620 | if (integrity_w_tfm) { |
| 1621 | i = crypto_hash_digestsize(integrity_w_tfm); |
| 1622 | int_dig_out = kmalloc(i, GFP_KERNEL); |
| 1623 | if (!int_dig_out) { |
| 1624 | retcode = ERR_NOMEM; |
| 1625 | goto fail; |
| 1626 | } |
| 1627 | int_dig_in = kmalloc(i, GFP_KERNEL); |
| 1628 | if (!int_dig_in) { |
| 1629 | retcode = ERR_NOMEM; |
| 1630 | goto fail; |
| 1631 | } |
| 1632 | int_dig_vv = kmalloc(i, GFP_KERNEL); |
| 1633 | if (!int_dig_vv) { |
| 1634 | retcode = ERR_NOMEM; |
| 1635 | goto fail; |
| 1636 | } |
| 1637 | } |
| 1638 | |
Philipp Reisner | 8088319 | 2011-02-18 14:56:45 +0100 | [diff] [blame] | 1639 | conn_flush_workqueue(tconn); |
| 1640 | spin_lock_irq(&tconn->req_lock); |
| 1641 | if (tconn->net_conf != NULL) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1642 | retcode = ERR_NET_CONFIGURED; |
Philipp Reisner | 8088319 | 2011-02-18 14:56:45 +0100 | [diff] [blame] | 1643 | spin_unlock_irq(&tconn->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1644 | goto fail; |
| 1645 | } |
Philipp Reisner | 8088319 | 2011-02-18 14:56:45 +0100 | [diff] [blame] | 1646 | tconn->net_conf = new_conf; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1647 | |
Philipp Reisner | 8088319 | 2011-02-18 14:56:45 +0100 | [diff] [blame] | 1648 | crypto_free_hash(tconn->cram_hmac_tfm); |
| 1649 | tconn->cram_hmac_tfm = tfm; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1650 | |
Philipp Reisner | 8088319 | 2011-02-18 14:56:45 +0100 | [diff] [blame] | 1651 | crypto_free_hash(tconn->integrity_w_tfm); |
| 1652 | tconn->integrity_w_tfm = integrity_w_tfm; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1653 | |
Philipp Reisner | 8088319 | 2011-02-18 14:56:45 +0100 | [diff] [blame] | 1654 | crypto_free_hash(tconn->integrity_r_tfm); |
| 1655 | tconn->integrity_r_tfm = integrity_r_tfm; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1656 | |
Philipp Reisner | 8088319 | 2011-02-18 14:56:45 +0100 | [diff] [blame] | 1657 | kfree(tconn->int_dig_out); |
| 1658 | kfree(tconn->int_dig_in); |
| 1659 | kfree(tconn->int_dig_vv); |
| 1660 | tconn->int_dig_out=int_dig_out; |
| 1661 | tconn->int_dig_in=int_dig_in; |
| 1662 | tconn->int_dig_vv=int_dig_vv; |
| 1663 | retcode = _conn_request_state(tconn, NS(conn, C_UNCONNECTED), CS_VERBOSE); |
| 1664 | spin_unlock_irq(&tconn->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1665 | |
Philipp Reisner | 8088319 | 2011-02-18 14:56:45 +0100 | [diff] [blame] | 1666 | idr_for_each_entry(&tconn->volumes, mdev, i) { |
| 1667 | mdev->send_cnt = 0; |
| 1668 | mdev->recv_cnt = 0; |
| 1669 | kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE); |
| 1670 | } |
Philipp Reisner | 8088319 | 2011-02-18 14:56:45 +0100 | [diff] [blame] | 1671 | conn_reconfig_done(tconn); |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 1672 | drbd_adm_finish(info, retcode); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1673 | return 0; |
| 1674 | |
| 1675 | fail: |
| 1676 | kfree(int_dig_out); |
| 1677 | kfree(int_dig_in); |
| 1678 | kfree(int_dig_vv); |
| 1679 | crypto_free_hash(tfm); |
| 1680 | crypto_free_hash(integrity_w_tfm); |
| 1681 | crypto_free_hash(integrity_r_tfm); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1682 | kfree(new_conf); |
| 1683 | |
Philipp Reisner | 8088319 | 2011-02-18 14:56:45 +0100 | [diff] [blame] | 1684 | conn_reconfig_done(tconn); |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 1685 | out: |
| 1686 | drbd_adm_finish(info, retcode); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1687 | return 0; |
| 1688 | } |
| 1689 | |
Lars Ellenberg | 85f75dd7 | 2011-03-15 16:26:37 +0100 | [diff] [blame] | 1690 | static enum drbd_state_rv conn_try_disconnect(struct drbd_tconn *tconn, bool force) |
| 1691 | { |
| 1692 | enum drbd_state_rv rv; |
| 1693 | if (force) { |
| 1694 | spin_lock_irq(&tconn->req_lock); |
| 1695 | if (tconn->cstate >= C_WF_CONNECTION) |
| 1696 | _conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD); |
| 1697 | spin_unlock_irq(&tconn->req_lock); |
| 1698 | return SS_SUCCESS; |
| 1699 | } |
| 1700 | |
| 1701 | rv = conn_request_state(tconn, NS(conn, C_DISCONNECTING), 0); |
| 1702 | |
| 1703 | switch (rv) { |
| 1704 | case SS_NOTHING_TO_DO: |
| 1705 | case SS_ALREADY_STANDALONE: |
| 1706 | return SS_SUCCESS; |
| 1707 | case SS_PRIMARY_NOP: |
| 1708 | /* Our state checking code wants to see the peer outdated. */ |
| 1709 | rv = conn_request_state(tconn, NS2(conn, C_DISCONNECTING, |
| 1710 | pdsk, D_OUTDATED), CS_VERBOSE); |
| 1711 | break; |
| 1712 | case SS_CW_FAILED_BY_PEER: |
| 1713 | /* The peer probably wants to see us outdated. */ |
| 1714 | rv = conn_request_state(tconn, NS2(conn, C_DISCONNECTING, |
| 1715 | disk, D_OUTDATED), 0); |
| 1716 | if (rv == SS_IS_DISKLESS || rv == SS_LOWER_THAN_OUTDATED) { |
| 1717 | conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD); |
| 1718 | rv = SS_SUCCESS; |
| 1719 | } |
| 1720 | break; |
| 1721 | default:; |
| 1722 | /* no special handling necessary */ |
| 1723 | } |
| 1724 | |
| 1725 | return rv; |
| 1726 | } |
| 1727 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 1728 | int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1729 | { |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 1730 | struct disconnect_parms parms; |
| 1731 | struct drbd_tconn *tconn; |
Lars Ellenberg | 85f75dd7 | 2011-03-15 16:26:37 +0100 | [diff] [blame] | 1732 | enum drbd_state_rv rv; |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 1733 | enum drbd_ret_code retcode; |
| 1734 | int err; |
Philipp Reisner | 2561b9c | 2010-12-03 15:22:48 +0100 | [diff] [blame] | 1735 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 1736 | retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONN); |
| 1737 | if (!adm_ctx.reply_skb) |
| 1738 | return retcode; |
| 1739 | if (retcode != NO_ERROR) |
Philipp Reisner | 2561b9c | 2010-12-03 15:22:48 +0100 | [diff] [blame] | 1740 | goto fail; |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 1741 | |
| 1742 | tconn = adm_ctx.tconn; |
| 1743 | memset(&parms, 0, sizeof(parms)); |
| 1744 | if (info->attrs[DRBD_NLA_DISCONNECT_PARMS]) { |
| 1745 | err = disconnect_parms_from_attrs(&parms, info->attrs); |
| 1746 | if (err) { |
| 1747 | retcode = ERR_MANDATORY_TAG; |
| 1748 | drbd_msg_put_info(from_attrs_err_to_txt(err)); |
| 1749 | goto fail; |
| 1750 | } |
Philipp Reisner | 2561b9c | 2010-12-03 15:22:48 +0100 | [diff] [blame] | 1751 | } |
| 1752 | |
Lars Ellenberg | 85f75dd7 | 2011-03-15 16:26:37 +0100 | [diff] [blame] | 1753 | rv = conn_try_disconnect(tconn, parms.force_disconnect); |
| 1754 | if (rv < SS_SUCCESS) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1755 | goto fail; |
| 1756 | |
Philipp Reisner | df24aa4 | 2011-02-15 11:14:44 +0100 | [diff] [blame] | 1757 | if (wait_event_interruptible(tconn->ping_wait, |
| 1758 | tconn->cstate != C_DISCONNECTING)) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1759 | /* Do not test for mdev->state.conn == C_STANDALONE, since |
| 1760 | someone else might connect us in the mean time! */ |
| 1761 | retcode = ERR_INTR; |
| 1762 | goto fail; |
| 1763 | } |
| 1764 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1765 | retcode = NO_ERROR; |
| 1766 | fail: |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 1767 | drbd_adm_finish(info, retcode); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1768 | return 0; |
| 1769 | } |
| 1770 | |
| 1771 | void resync_after_online_grow(struct drbd_conf *mdev) |
| 1772 | { |
| 1773 | int iass; /* I am sync source */ |
| 1774 | |
| 1775 | dev_info(DEV, "Resync of new storage after online grow\n"); |
| 1776 | if (mdev->state.role != mdev->state.peer) |
| 1777 | iass = (mdev->state.role == R_PRIMARY); |
| 1778 | else |
Philipp Reisner | 25703f8 | 2011-02-07 14:35:25 +0100 | [diff] [blame] | 1779 | iass = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1780 | |
| 1781 | if (iass) |
| 1782 | drbd_start_resync(mdev, C_SYNC_SOURCE); |
| 1783 | else |
| 1784 | _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE + CS_SERIALIZE); |
| 1785 | } |
| 1786 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 1787 | int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1788 | { |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 1789 | struct resize_parms rs; |
| 1790 | struct drbd_conf *mdev; |
| 1791 | enum drbd_ret_code retcode; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1792 | enum determine_dev_size dd; |
Philipp Reisner | 6495d2c | 2010-03-24 16:07:04 +0100 | [diff] [blame] | 1793 | enum dds_flags ddsf; |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 1794 | int err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1795 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 1796 | retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR); |
| 1797 | if (!adm_ctx.reply_skb) |
| 1798 | return retcode; |
| 1799 | if (retcode != NO_ERROR) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1800 | goto fail; |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 1801 | |
| 1802 | memset(&rs, 0, sizeof(struct resize_parms)); |
| 1803 | if (info->attrs[DRBD_NLA_RESIZE_PARMS]) { |
| 1804 | err = resize_parms_from_attrs(&rs, info->attrs); |
| 1805 | if (err) { |
| 1806 | retcode = ERR_MANDATORY_TAG; |
| 1807 | drbd_msg_put_info(from_attrs_err_to_txt(err)); |
| 1808 | goto fail; |
| 1809 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1810 | } |
| 1811 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 1812 | mdev = adm_ctx.mdev; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1813 | if (mdev->state.conn > C_CONNECTED) { |
| 1814 | retcode = ERR_RESIZE_RESYNC; |
| 1815 | goto fail; |
| 1816 | } |
| 1817 | |
| 1818 | if (mdev->state.role == R_SECONDARY && |
| 1819 | mdev->state.peer == R_SECONDARY) { |
| 1820 | retcode = ERR_NO_PRIMARY; |
| 1821 | goto fail; |
| 1822 | } |
| 1823 | |
| 1824 | if (!get_ldev(mdev)) { |
| 1825 | retcode = ERR_NO_DISK; |
| 1826 | goto fail; |
| 1827 | } |
| 1828 | |
Philipp Reisner | 31890f4 | 2011-01-19 14:12:51 +0100 | [diff] [blame] | 1829 | if (rs.no_resync && mdev->tconn->agreed_pro_version < 93) { |
Philipp Reisner | 6495d2c | 2010-03-24 16:07:04 +0100 | [diff] [blame] | 1830 | retcode = ERR_NEED_APV_93; |
| 1831 | goto fail; |
| 1832 | } |
| 1833 | |
Philipp Reisner | 087c249 | 2010-03-26 13:49:56 +0100 | [diff] [blame] | 1834 | if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev)) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1835 | mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1836 | |
| 1837 | mdev->ldev->dc.disk_size = (sector_t)rs.resize_size; |
Philipp Reisner | 6495d2c | 2010-03-24 16:07:04 +0100 | [diff] [blame] | 1838 | 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] | 1839 | dd = drbd_determine_dev_size(mdev, ddsf); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1840 | drbd_md_sync(mdev); |
| 1841 | put_ldev(mdev); |
| 1842 | if (dd == dev_size_error) { |
| 1843 | retcode = ERR_NOMEM_BITMAP; |
| 1844 | goto fail; |
| 1845 | } |
| 1846 | |
Philipp Reisner | 087c249 | 2010-03-26 13:49:56 +0100 | [diff] [blame] | 1847 | if (mdev->state.conn == C_CONNECTED) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1848 | if (dd == grew) |
| 1849 | set_bit(RESIZE_PENDING, &mdev->flags); |
| 1850 | |
| 1851 | drbd_send_uuids(mdev); |
Philipp Reisner | 6495d2c | 2010-03-24 16:07:04 +0100 | [diff] [blame] | 1852 | drbd_send_sizes(mdev, 1, ddsf); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1853 | } |
| 1854 | |
| 1855 | fail: |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 1856 | drbd_adm_finish(info, retcode); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1857 | return 0; |
| 1858 | } |
| 1859 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 1860 | int drbd_adm_syncer(struct sk_buff *skb, struct genl_info *info) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1861 | { |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 1862 | struct drbd_conf *mdev; |
| 1863 | enum drbd_ret_code retcode; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1864 | int err; |
| 1865 | int ovr; /* online verify running */ |
| 1866 | int rsr; /* re-sync running */ |
| 1867 | struct crypto_hash *verify_tfm = NULL; |
| 1868 | struct crypto_hash *csums_tfm = NULL; |
| 1869 | struct syncer_conf sc; |
| 1870 | cpumask_var_t new_cpu_mask; |
Philipp Reisner | 778f271 | 2010-07-06 11:14:00 +0200 | [diff] [blame] | 1871 | int *rs_plan_s = NULL; |
| 1872 | int fifo_size; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1873 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 1874 | retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR); |
| 1875 | if (!adm_ctx.reply_skb) |
| 1876 | return retcode; |
| 1877 | if (retcode != NO_ERROR) |
| 1878 | goto fail; |
| 1879 | mdev = adm_ctx.mdev; |
| 1880 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1881 | if (!zalloc_cpumask_var(&new_cpu_mask, GFP_KERNEL)) { |
| 1882 | retcode = ERR_NOMEM; |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 1883 | drbd_msg_put_info("unable to allocate cpumask"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1884 | goto fail; |
| 1885 | } |
| 1886 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 1887 | if (((struct drbd_genlmsghdr*)info->userhdr)->flags |
| 1888 | & DRBD_GENL_F_SET_DEFAULTS) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1889 | memset(&sc, 0, sizeof(struct syncer_conf)); |
| 1890 | sc.rate = DRBD_RATE_DEF; |
| 1891 | sc.after = DRBD_AFTER_DEF; |
| 1892 | sc.al_extents = DRBD_AL_EXTENTS_DEF; |
Philipp Reisner | 265be2d | 2010-05-31 10:14:17 +0200 | [diff] [blame] | 1893 | sc.on_no_data = DRBD_ON_NO_DATA_DEF; |
Philipp Reisner | 9a31d71 | 2010-07-05 13:42:03 +0200 | [diff] [blame] | 1894 | sc.c_plan_ahead = DRBD_C_PLAN_AHEAD_DEF; |
| 1895 | sc.c_delay_target = DRBD_C_DELAY_TARGET_DEF; |
| 1896 | sc.c_fill_target = DRBD_C_FILL_TARGET_DEF; |
Lars Ellenberg | 0f0601f | 2010-08-11 23:40:24 +0200 | [diff] [blame] | 1897 | sc.c_max_rate = DRBD_C_MAX_RATE_DEF; |
| 1898 | sc.c_min_rate = DRBD_C_MIN_RATE_DEF; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1899 | } else |
| 1900 | memcpy(&sc, &mdev->sync_conf, sizeof(struct syncer_conf)); |
| 1901 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 1902 | err = syncer_conf_from_attrs(&sc, info->attrs); |
| 1903 | if (err) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1904 | retcode = ERR_MANDATORY_TAG; |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 1905 | drbd_msg_put_info(from_attrs_err_to_txt(err)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1906 | goto fail; |
| 1907 | } |
| 1908 | |
| 1909 | /* re-sync running */ |
| 1910 | rsr = ( mdev->state.conn == C_SYNC_SOURCE || |
| 1911 | mdev->state.conn == C_SYNC_TARGET || |
| 1912 | mdev->state.conn == C_PAUSED_SYNC_S || |
| 1913 | mdev->state.conn == C_PAUSED_SYNC_T ); |
| 1914 | |
| 1915 | if (rsr && strcmp(sc.csums_alg, mdev->sync_conf.csums_alg)) { |
| 1916 | retcode = ERR_CSUMS_RESYNC_RUNNING; |
| 1917 | goto fail; |
| 1918 | } |
| 1919 | |
| 1920 | if (!rsr && sc.csums_alg[0]) { |
| 1921 | csums_tfm = crypto_alloc_hash(sc.csums_alg, 0, CRYPTO_ALG_ASYNC); |
| 1922 | if (IS_ERR(csums_tfm)) { |
| 1923 | csums_tfm = NULL; |
| 1924 | retcode = ERR_CSUMS_ALG; |
| 1925 | goto fail; |
| 1926 | } |
| 1927 | |
| 1928 | if (!drbd_crypto_is_hash(crypto_hash_tfm(csums_tfm))) { |
| 1929 | retcode = ERR_CSUMS_ALG_ND; |
| 1930 | goto fail; |
| 1931 | } |
| 1932 | } |
| 1933 | |
| 1934 | /* online verify running */ |
| 1935 | ovr = (mdev->state.conn == C_VERIFY_S || mdev->state.conn == C_VERIFY_T); |
| 1936 | |
| 1937 | if (ovr) { |
| 1938 | if (strcmp(sc.verify_alg, mdev->sync_conf.verify_alg)) { |
| 1939 | retcode = ERR_VERIFY_RUNNING; |
| 1940 | goto fail; |
| 1941 | } |
| 1942 | } |
| 1943 | |
| 1944 | if (!ovr && sc.verify_alg[0]) { |
| 1945 | verify_tfm = crypto_alloc_hash(sc.verify_alg, 0, CRYPTO_ALG_ASYNC); |
| 1946 | if (IS_ERR(verify_tfm)) { |
| 1947 | verify_tfm = NULL; |
| 1948 | retcode = ERR_VERIFY_ALG; |
| 1949 | goto fail; |
| 1950 | } |
| 1951 | |
| 1952 | if (!drbd_crypto_is_hash(crypto_hash_tfm(verify_tfm))) { |
| 1953 | retcode = ERR_VERIFY_ALG_ND; |
| 1954 | goto fail; |
| 1955 | } |
| 1956 | } |
| 1957 | |
| 1958 | /* silently ignore cpu mask on UP kernel */ |
| 1959 | if (nr_cpu_ids > 1 && sc.cpu_mask[0] != 0) { |
| 1960 | err = __bitmap_parse(sc.cpu_mask, 32, 0, |
| 1961 | cpumask_bits(new_cpu_mask), nr_cpu_ids); |
| 1962 | if (err) { |
| 1963 | dev_warn(DEV, "__bitmap_parse() failed with %d\n", err); |
| 1964 | retcode = ERR_CPU_MASK_PARSE; |
| 1965 | goto fail; |
| 1966 | } |
| 1967 | } |
| 1968 | |
Andreas Gruenbacher | 841ce24 | 2010-12-15 19:31:20 +0100 | [diff] [blame] | 1969 | if (!expect(sc.rate >= 1)) |
| 1970 | sc.rate = 1; |
Lars Ellenberg | 7ad651b | 2011-02-21 13:21:03 +0100 | [diff] [blame] | 1971 | |
| 1972 | /* clip to allowed range */ |
| 1973 | if (!expect(sc.al_extents >= DRBD_AL_EXTENTS_MIN)) |
| 1974 | sc.al_extents = DRBD_AL_EXTENTS_MIN; |
| 1975 | if (!expect(sc.al_extents <= DRBD_AL_EXTENTS_MAX)) |
| 1976 | sc.al_extents = DRBD_AL_EXTENTS_MAX; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1977 | |
| 1978 | /* most sanity checks done, try to assign the new sync-after |
| 1979 | * dependency. need to hold the global lock in there, |
| 1980 | * to avoid a race in the dependency loop check. */ |
| 1981 | retcode = drbd_alter_sa(mdev, sc.after); |
| 1982 | if (retcode != NO_ERROR) |
| 1983 | goto fail; |
| 1984 | |
Philipp Reisner | 778f271 | 2010-07-06 11:14:00 +0200 | [diff] [blame] | 1985 | fifo_size = (sc.c_plan_ahead * 10 * SLEEP_TIME) / HZ; |
| 1986 | if (fifo_size != mdev->rs_plan_s.size && fifo_size > 0) { |
| 1987 | rs_plan_s = kzalloc(sizeof(int) * fifo_size, GFP_KERNEL); |
| 1988 | if (!rs_plan_s) { |
| 1989 | dev_err(DEV, "kmalloc of fifo_buffer failed"); |
| 1990 | retcode = ERR_NOMEM; |
| 1991 | goto fail; |
| 1992 | } |
| 1993 | } |
| 1994 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1995 | /* ok, assign the rest of it as well. |
| 1996 | * lock against receive_SyncParam() */ |
| 1997 | spin_lock(&mdev->peer_seq_lock); |
| 1998 | mdev->sync_conf = sc; |
| 1999 | |
| 2000 | if (!rsr) { |
| 2001 | crypto_free_hash(mdev->csums_tfm); |
| 2002 | mdev->csums_tfm = csums_tfm; |
| 2003 | csums_tfm = NULL; |
| 2004 | } |
| 2005 | |
| 2006 | if (!ovr) { |
| 2007 | crypto_free_hash(mdev->verify_tfm); |
| 2008 | mdev->verify_tfm = verify_tfm; |
| 2009 | verify_tfm = NULL; |
| 2010 | } |
Philipp Reisner | 778f271 | 2010-07-06 11:14:00 +0200 | [diff] [blame] | 2011 | |
| 2012 | if (fifo_size != mdev->rs_plan_s.size) { |
| 2013 | kfree(mdev->rs_plan_s.values); |
| 2014 | mdev->rs_plan_s.values = rs_plan_s; |
| 2015 | mdev->rs_plan_s.size = fifo_size; |
| 2016 | mdev->rs_planed = 0; |
| 2017 | rs_plan_s = NULL; |
| 2018 | } |
| 2019 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2020 | spin_unlock(&mdev->peer_seq_lock); |
| 2021 | |
| 2022 | if (get_ldev(mdev)) { |
| 2023 | wait_event(mdev->al_wait, lc_try_lock(mdev->act_log)); |
| 2024 | drbd_al_shrink(mdev); |
| 2025 | err = drbd_check_al_size(mdev); |
| 2026 | lc_unlock(mdev->act_log); |
| 2027 | wake_up(&mdev->al_wait); |
| 2028 | |
| 2029 | put_ldev(mdev); |
| 2030 | drbd_md_sync(mdev); |
| 2031 | |
| 2032 | if (err) { |
| 2033 | retcode = ERR_NOMEM; |
| 2034 | goto fail; |
| 2035 | } |
| 2036 | } |
| 2037 | |
| 2038 | if (mdev->state.conn >= C_CONNECTED) |
| 2039 | drbd_send_sync_param(mdev, &sc); |
| 2040 | |
Philipp Reisner | 8082228 | 2011-02-08 12:46:30 +0100 | [diff] [blame] | 2041 | if (!cpumask_equal(mdev->tconn->cpu_mask, new_cpu_mask)) { |
| 2042 | cpumask_copy(mdev->tconn->cpu_mask, new_cpu_mask); |
| 2043 | drbd_calc_cpu_mask(mdev->tconn); |
Philipp Reisner | e6b3ea8 | 2011-01-19 14:02:01 +0100 | [diff] [blame] | 2044 | mdev->tconn->receiver.reset_cpu_mask = 1; |
| 2045 | mdev->tconn->asender.reset_cpu_mask = 1; |
| 2046 | mdev->tconn->worker.reset_cpu_mask = 1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2047 | } |
| 2048 | |
| 2049 | kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE); |
| 2050 | fail: |
Philipp Reisner | 778f271 | 2010-07-06 11:14:00 +0200 | [diff] [blame] | 2051 | kfree(rs_plan_s); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2052 | free_cpumask_var(new_cpu_mask); |
| 2053 | crypto_free_hash(csums_tfm); |
| 2054 | crypto_free_hash(verify_tfm); |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2055 | |
| 2056 | drbd_adm_finish(info, retcode); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2057 | return 0; |
| 2058 | } |
| 2059 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2060 | int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2061 | { |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2062 | struct drbd_conf *mdev; |
| 2063 | int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */ |
| 2064 | |
| 2065 | retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR); |
| 2066 | if (!adm_ctx.reply_skb) |
| 2067 | return retcode; |
| 2068 | if (retcode != NO_ERROR) |
| 2069 | goto out; |
| 2070 | |
| 2071 | mdev = adm_ctx.mdev; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2072 | |
Lars Ellenberg | 194bfb3 | 2011-01-18 10:38:01 +0100 | [diff] [blame] | 2073 | /* If there is still bitmap IO pending, probably because of a previous |
| 2074 | * resync 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 | retcode = _drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T), CS_ORDERED); |
| 2078 | |
| 2079 | if (retcode < SS_SUCCESS && retcode != SS_NEED_CONNECTION) |
| 2080 | retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T)); |
| 2081 | |
| 2082 | while (retcode == SS_NEED_CONNECTION) { |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 2083 | spin_lock_irq(&mdev->tconn->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2084 | if (mdev->state.conn < C_CONNECTED) |
| 2085 | retcode = _drbd_set_state(_NS(mdev, disk, D_INCONSISTENT), CS_VERBOSE, NULL); |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 2086 | spin_unlock_irq(&mdev->tconn->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2087 | |
| 2088 | if (retcode != SS_NEED_CONNECTION) |
| 2089 | break; |
| 2090 | |
| 2091 | retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T)); |
| 2092 | } |
| 2093 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2094 | out: |
| 2095 | drbd_adm_finish(info, retcode); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2096 | return 0; |
| 2097 | } |
| 2098 | |
Philipp Reisner | 0778286 | 2010-08-31 12:00:50 +0200 | [diff] [blame] | 2099 | static int drbd_bmio_set_susp_al(struct drbd_conf *mdev) |
| 2100 | { |
| 2101 | int rv; |
| 2102 | |
| 2103 | rv = drbd_bmio_set_n_write(mdev); |
| 2104 | drbd_suspend_al(mdev); |
| 2105 | return rv; |
| 2106 | } |
| 2107 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2108 | static int drbd_adm_simple_request_state(struct sk_buff *skb, struct genl_info *info, |
| 2109 | union drbd_state mask, union drbd_state val) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2110 | { |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2111 | enum drbd_ret_code retcode; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2112 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2113 | retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR); |
| 2114 | if (!adm_ctx.reply_skb) |
| 2115 | return retcode; |
| 2116 | if (retcode != NO_ERROR) |
| 2117 | goto out; |
Lars Ellenberg | 194bfb3 | 2011-01-18 10:38:01 +0100 | [diff] [blame] | 2118 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2119 | retcode = drbd_request_state(adm_ctx.mdev, mask, val); |
| 2120 | out: |
| 2121 | drbd_adm_finish(info, retcode); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2122 | return 0; |
| 2123 | } |
| 2124 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2125 | int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2126 | { |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2127 | return drbd_adm_simple_request_state(skb, info, NS(conn, C_STARTING_SYNC_S)); |
| 2128 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2129 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2130 | int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info) |
| 2131 | { |
| 2132 | enum drbd_ret_code retcode; |
| 2133 | |
| 2134 | retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR); |
| 2135 | if (!adm_ctx.reply_skb) |
| 2136 | return retcode; |
| 2137 | if (retcode != NO_ERROR) |
| 2138 | goto out; |
| 2139 | |
| 2140 | if (drbd_request_state(adm_ctx.mdev, NS(user_isp, 1)) == SS_NOTHING_TO_DO) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2141 | retcode = ERR_PAUSE_IS_SET; |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2142 | out: |
| 2143 | drbd_adm_finish(info, retcode); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2144 | return 0; |
| 2145 | } |
| 2146 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2147 | int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2148 | { |
Philipp Reisner | cd88d03 | 2011-01-20 11:46:41 +0100 | [diff] [blame] | 2149 | union drbd_state s; |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2150 | enum drbd_ret_code retcode; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2151 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2152 | retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR); |
| 2153 | if (!adm_ctx.reply_skb) |
| 2154 | return retcode; |
| 2155 | if (retcode != NO_ERROR) |
| 2156 | goto out; |
| 2157 | |
| 2158 | if (drbd_request_state(adm_ctx.mdev, NS(user_isp, 0)) == SS_NOTHING_TO_DO) { |
| 2159 | s = adm_ctx.mdev->state; |
Philipp Reisner | cd88d03 | 2011-01-20 11:46:41 +0100 | [diff] [blame] | 2160 | if (s.conn == C_PAUSED_SYNC_S || s.conn == C_PAUSED_SYNC_T) { |
| 2161 | retcode = s.aftr_isp ? ERR_PIC_AFTER_DEP : |
| 2162 | s.peer_isp ? ERR_PIC_PEER_DEP : ERR_PAUSE_IS_CLEAR; |
| 2163 | } else { |
| 2164 | retcode = ERR_PAUSE_IS_CLEAR; |
| 2165 | } |
| 2166 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2167 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2168 | out: |
| 2169 | drbd_adm_finish(info, retcode); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2170 | return 0; |
| 2171 | } |
| 2172 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2173 | int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2174 | { |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2175 | return drbd_adm_simple_request_state(skb, info, NS(susp, 1)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2176 | } |
| 2177 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2178 | int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2179 | { |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2180 | struct drbd_conf *mdev; |
| 2181 | int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */ |
| 2182 | |
| 2183 | retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR); |
| 2184 | if (!adm_ctx.reply_skb) |
| 2185 | return retcode; |
| 2186 | if (retcode != NO_ERROR) |
| 2187 | goto out; |
| 2188 | |
| 2189 | mdev = adm_ctx.mdev; |
Philipp Reisner | 43a5182 | 2010-06-11 11:26:34 +0200 | [diff] [blame] | 2190 | if (test_bit(NEW_CUR_UUID, &mdev->flags)) { |
| 2191 | drbd_uuid_new_current(mdev); |
| 2192 | clear_bit(NEW_CUR_UUID, &mdev->flags); |
Philipp Reisner | 43a5182 | 2010-06-11 11:26:34 +0200 | [diff] [blame] | 2193 | } |
Philipp Reisner | 265be2d | 2010-05-31 10:14:17 +0200 | [diff] [blame] | 2194 | drbd_suspend_io(mdev); |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2195 | retcode = drbd_request_state(mdev, NS3(susp, 0, susp_nod, 0, susp_fen, 0)); |
| 2196 | if (retcode == SS_SUCCESS) { |
Philipp Reisner | 265be2d | 2010-05-31 10:14:17 +0200 | [diff] [blame] | 2197 | if (mdev->state.conn < C_CONNECTED) |
Philipp Reisner | 2f5cdd0 | 2011-02-21 14:29:27 +0100 | [diff] [blame] | 2198 | tl_clear(mdev->tconn); |
Philipp Reisner | 265be2d | 2010-05-31 10:14:17 +0200 | [diff] [blame] | 2199 | if (mdev->state.disk == D_DISKLESS || mdev->state.disk == D_FAILED) |
Philipp Reisner | 2f5cdd0 | 2011-02-21 14:29:27 +0100 | [diff] [blame] | 2200 | tl_restart(mdev->tconn, FAIL_FROZEN_DISK_IO); |
Philipp Reisner | 265be2d | 2010-05-31 10:14:17 +0200 | [diff] [blame] | 2201 | } |
| 2202 | drbd_resume_io(mdev); |
| 2203 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2204 | out: |
| 2205 | drbd_adm_finish(info, retcode); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2206 | return 0; |
| 2207 | } |
| 2208 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2209 | int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2210 | { |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2211 | return drbd_adm_simple_request_state(skb, info, NS(disk, D_OUTDATED)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2212 | } |
| 2213 | |
Lars Ellenberg | 543cc10 | 2011-03-10 22:18:18 +0100 | [diff] [blame] | 2214 | int nla_put_drbd_cfg_context(struct sk_buff *skb, const char *conn_name, unsigned vnr) |
| 2215 | { |
| 2216 | struct nlattr *nla; |
| 2217 | nla = nla_nest_start(skb, DRBD_NLA_CFG_CONTEXT); |
| 2218 | if (!nla) |
| 2219 | goto nla_put_failure; |
| 2220 | if (vnr != VOLUME_UNSPECIFIED) |
| 2221 | NLA_PUT_U32(skb, T_ctx_volume, vnr); |
| 2222 | NLA_PUT_STRING(skb, T_ctx_conn_name, conn_name); |
| 2223 | nla_nest_end(skb, nla); |
| 2224 | return 0; |
| 2225 | |
| 2226 | nla_put_failure: |
| 2227 | if (nla) |
| 2228 | nla_nest_cancel(skb, nla); |
| 2229 | return -EMSGSIZE; |
| 2230 | } |
| 2231 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2232 | int nla_put_status_info(struct sk_buff *skb, struct drbd_conf *mdev, |
| 2233 | const struct sib_info *sib) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2234 | { |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2235 | struct state_info *si = NULL; /* for sizeof(si->member); */ |
| 2236 | struct nlattr *nla; |
| 2237 | int got_ldev; |
| 2238 | int got_net; |
| 2239 | int err = 0; |
| 2240 | int exclude_sensitive; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2241 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2242 | /* If sib != NULL, this is drbd_bcast_event, which anyone can listen |
| 2243 | * to. So we better exclude_sensitive information. |
| 2244 | * |
| 2245 | * If sib == NULL, this is drbd_adm_get_status, executed synchronously |
| 2246 | * in the context of the requesting user process. Exclude sensitive |
| 2247 | * information, unless current has superuser. |
| 2248 | * |
| 2249 | * NOTE: for drbd_adm_get_status_all(), this is a netlink dump, and |
| 2250 | * relies on the current implementation of netlink_dump(), which |
| 2251 | * executes the dump callback successively from netlink_recvmsg(), |
| 2252 | * always in the context of the receiving process */ |
| 2253 | exclude_sensitive = sib || !capable(CAP_SYS_ADMIN); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2254 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2255 | got_ldev = get_ldev(mdev); |
| 2256 | got_net = get_net_conf(mdev->tconn); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2257 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2258 | /* We need to add connection name and volume number information still. |
| 2259 | * Minor number is in drbd_genlmsghdr. */ |
Lars Ellenberg | 543cc10 | 2011-03-10 22:18:18 +0100 | [diff] [blame] | 2260 | if (nla_put_drbd_cfg_context(skb, mdev->tconn->name, mdev->vnr)) |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2261 | goto nla_put_failure; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2262 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2263 | if (got_ldev) |
| 2264 | if (disk_conf_to_skb(skb, &mdev->ldev->dc, exclude_sensitive)) |
| 2265 | goto nla_put_failure; |
| 2266 | if (got_net) |
| 2267 | if (net_conf_to_skb(skb, mdev->tconn->net_conf, exclude_sensitive)) |
| 2268 | goto nla_put_failure; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2269 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2270 | if (syncer_conf_to_skb(skb, &mdev->sync_conf, exclude_sensitive)) |
| 2271 | goto nla_put_failure; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2272 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2273 | nla = nla_nest_start(skb, DRBD_NLA_STATE_INFO); |
| 2274 | if (!nla) |
| 2275 | goto nla_put_failure; |
| 2276 | NLA_PUT_U32(skb, T_sib_reason, sib ? sib->sib_reason : SIB_GET_STATUS_REPLY); |
| 2277 | NLA_PUT_U32(skb, T_current_state, mdev->state.i); |
| 2278 | NLA_PUT_U64(skb, T_ed_uuid, mdev->ed_uuid); |
| 2279 | NLA_PUT_U64(skb, T_capacity, drbd_get_capacity(mdev->this_bdev)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2280 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2281 | if (got_ldev) { |
| 2282 | NLA_PUT_U32(skb, T_disk_flags, mdev->ldev->md.flags); |
| 2283 | NLA_PUT(skb, T_uuids, sizeof(si->uuids), mdev->ldev->md.uuid); |
| 2284 | NLA_PUT_U64(skb, T_bits_total, drbd_bm_bits(mdev)); |
| 2285 | NLA_PUT_U64(skb, T_bits_oos, drbd_bm_total_weight(mdev)); |
| 2286 | if (C_SYNC_SOURCE <= mdev->state.conn && |
| 2287 | C_PAUSED_SYNC_T >= mdev->state.conn) { |
| 2288 | NLA_PUT_U64(skb, T_bits_rs_total, mdev->rs_total); |
| 2289 | NLA_PUT_U64(skb, T_bits_rs_failed, mdev->rs_failed); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2290 | } |
| 2291 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2292 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2293 | if (sib) { |
| 2294 | switch(sib->sib_reason) { |
| 2295 | case SIB_SYNC_PROGRESS: |
| 2296 | case SIB_GET_STATUS_REPLY: |
| 2297 | break; |
| 2298 | case SIB_STATE_CHANGE: |
| 2299 | NLA_PUT_U32(skb, T_prev_state, sib->os.i); |
| 2300 | NLA_PUT_U32(skb, T_new_state, sib->ns.i); |
| 2301 | break; |
| 2302 | case SIB_HELPER_POST: |
| 2303 | NLA_PUT_U32(skb, |
| 2304 | T_helper_exit_code, sib->helper_exit_code); |
| 2305 | /* fall through */ |
| 2306 | case SIB_HELPER_PRE: |
| 2307 | NLA_PUT_STRING(skb, T_helper, sib->helper_name); |
| 2308 | break; |
| 2309 | } |
| 2310 | } |
| 2311 | nla_nest_end(skb, nla); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2312 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2313 | if (0) |
| 2314 | nla_put_failure: |
| 2315 | err = -EMSGSIZE; |
| 2316 | if (got_ldev) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2317 | put_ldev(mdev); |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2318 | if (got_net) |
| 2319 | put_net_conf(mdev->tconn); |
| 2320 | return err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2321 | } |
| 2322 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2323 | int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2324 | { |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2325 | enum drbd_ret_code retcode; |
| 2326 | int err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2327 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2328 | retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR); |
| 2329 | if (!adm_ctx.reply_skb) |
| 2330 | return retcode; |
| 2331 | if (retcode != NO_ERROR) |
| 2332 | goto out; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2333 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2334 | err = nla_put_status_info(adm_ctx.reply_skb, adm_ctx.mdev, NULL); |
| 2335 | if (err) { |
| 2336 | nlmsg_free(adm_ctx.reply_skb); |
| 2337 | return err; |
| 2338 | } |
| 2339 | out: |
| 2340 | drbd_adm_finish(info, retcode); |
| 2341 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2342 | } |
| 2343 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2344 | int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2345 | { |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2346 | struct drbd_conf *mdev; |
| 2347 | struct drbd_genlmsghdr *dh; |
Lars Ellenberg | 543cc10 | 2011-03-10 22:18:18 +0100 | [diff] [blame] | 2348 | struct drbd_tconn *pos = (struct drbd_tconn*)cb->args[0]; |
| 2349 | struct drbd_tconn *tconn = NULL; |
| 2350 | struct drbd_tconn *tmp; |
| 2351 | unsigned volume = cb->args[1]; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2352 | |
Lars Ellenberg | 543cc10 | 2011-03-10 22:18:18 +0100 | [diff] [blame] | 2353 | /* Open coded, deferred, iteration: |
| 2354 | * list_for_each_entry_safe(tconn, tmp, &drbd_tconns, all_tconn) { |
| 2355 | * idr_for_each_entry(&tconn->volumes, mdev, i) { |
| 2356 | * ... |
| 2357 | * } |
| 2358 | * } |
| 2359 | * where tconn is cb->args[0]; |
| 2360 | * and i is cb->args[1]; |
| 2361 | * |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2362 | * This may miss entries inserted after this dump started, |
| 2363 | * or entries deleted before they are reached. |
Lars Ellenberg | 543cc10 | 2011-03-10 22:18:18 +0100 | [diff] [blame] | 2364 | * |
| 2365 | * We need to make sure the mdev won't disappear while |
| 2366 | * we are looking at it, and revalidate our iterators |
| 2367 | * on each iteration. |
| 2368 | */ |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2369 | |
Lars Ellenberg | 543cc10 | 2011-03-10 22:18:18 +0100 | [diff] [blame] | 2370 | /* synchronize with drbd_new_tconn/drbd_free_tconn */ |
| 2371 | mutex_lock(&drbd_cfg_mutex); |
| 2372 | /* synchronize with drbd_delete_device */ |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2373 | rcu_read_lock(); |
Lars Ellenberg | 543cc10 | 2011-03-10 22:18:18 +0100 | [diff] [blame] | 2374 | next_tconn: |
| 2375 | /* revalidate iterator position */ |
| 2376 | list_for_each_entry(tmp, &drbd_tconns, all_tconn) { |
| 2377 | if (pos == NULL) { |
| 2378 | /* first iteration */ |
| 2379 | pos = tmp; |
| 2380 | tconn = pos; |
| 2381 | break; |
| 2382 | } |
| 2383 | if (tmp == pos) { |
| 2384 | tconn = pos; |
| 2385 | break; |
| 2386 | } |
| 2387 | } |
| 2388 | if (tconn) { |
| 2389 | mdev = idr_get_next(&tconn->volumes, &volume); |
| 2390 | if (!mdev) { |
| 2391 | /* No more volumes to dump on this tconn. |
| 2392 | * Advance tconn iterator. */ |
| 2393 | pos = list_entry(tconn->all_tconn.next, |
| 2394 | struct drbd_tconn, all_tconn); |
| 2395 | /* But, did we dump any volume on this tconn yet? */ |
| 2396 | if (volume != 0) { |
| 2397 | tconn = NULL; |
| 2398 | volume = 0; |
| 2399 | goto next_tconn; |
| 2400 | } |
| 2401 | } |
| 2402 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2403 | dh = genlmsg_put(skb, NETLINK_CB(cb->skb).pid, |
| 2404 | cb->nlh->nlmsg_seq, &drbd_genl_family, |
| 2405 | NLM_F_MULTI, DRBD_ADM_GET_STATUS); |
| 2406 | if (!dh) |
Lars Ellenberg | 543cc10 | 2011-03-10 22:18:18 +0100 | [diff] [blame] | 2407 | goto out; |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2408 | |
Lars Ellenberg | 543cc10 | 2011-03-10 22:18:18 +0100 | [diff] [blame] | 2409 | if (!mdev) { |
| 2410 | /* this is a tconn without a single volume */ |
| 2411 | dh->minor = -1U; |
| 2412 | dh->ret_code = NO_ERROR; |
| 2413 | if (nla_put_drbd_cfg_context(skb, tconn->name, VOLUME_UNSPECIFIED)) |
| 2414 | genlmsg_cancel(skb, dh); |
| 2415 | else |
| 2416 | genlmsg_end(skb, dh); |
| 2417 | goto out; |
| 2418 | } |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2419 | |
Lars Ellenberg | 543cc10 | 2011-03-10 22:18:18 +0100 | [diff] [blame] | 2420 | D_ASSERT(mdev->vnr == volume); |
| 2421 | D_ASSERT(mdev->tconn == tconn); |
| 2422 | |
| 2423 | dh->minor = mdev_to_minor(mdev); |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2424 | dh->ret_code = NO_ERROR; |
| 2425 | |
| 2426 | if (nla_put_status_info(skb, mdev, NULL)) { |
| 2427 | genlmsg_cancel(skb, dh); |
Lars Ellenberg | 543cc10 | 2011-03-10 22:18:18 +0100 | [diff] [blame] | 2428 | goto out; |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2429 | } |
| 2430 | genlmsg_end(skb, dh); |
| 2431 | } |
| 2432 | |
Lars Ellenberg | 543cc10 | 2011-03-10 22:18:18 +0100 | [diff] [blame] | 2433 | out: |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2434 | rcu_read_unlock(); |
Lars Ellenberg | 543cc10 | 2011-03-10 22:18:18 +0100 | [diff] [blame] | 2435 | mutex_unlock(&drbd_cfg_mutex); |
| 2436 | /* where to start the next iteration */ |
| 2437 | cb->args[0] = (long)pos; |
| 2438 | cb->args[1] = (pos == tconn) ? volume + 1 : 0; |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2439 | |
Lars Ellenberg | 543cc10 | 2011-03-10 22:18:18 +0100 | [diff] [blame] | 2440 | /* No more tconns/volumes/minors found results in an empty skb. |
| 2441 | * Which will terminate the dump. */ |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2442 | return skb->len; |
| 2443 | } |
| 2444 | |
| 2445 | int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info) |
| 2446 | { |
| 2447 | enum drbd_ret_code retcode; |
| 2448 | struct timeout_parms tp; |
| 2449 | int err; |
| 2450 | |
| 2451 | retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR); |
| 2452 | if (!adm_ctx.reply_skb) |
| 2453 | return retcode; |
| 2454 | if (retcode != NO_ERROR) |
| 2455 | goto out; |
| 2456 | |
| 2457 | tp.timeout_type = |
| 2458 | adm_ctx.mdev->state.pdsk == D_OUTDATED ? UT_PEER_OUTDATED : |
| 2459 | test_bit(USE_DEGR_WFC_T, &adm_ctx.mdev->flags) ? UT_DEGRADED : |
| 2460 | UT_DEFAULT; |
| 2461 | |
| 2462 | err = timeout_parms_to_priv_skb(adm_ctx.reply_skb, &tp); |
| 2463 | if (err) { |
| 2464 | nlmsg_free(adm_ctx.reply_skb); |
| 2465 | return err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2466 | } |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2467 | out: |
| 2468 | drbd_adm_finish(info, retcode); |
| 2469 | return 0; |
| 2470 | } |
Lars Ellenberg | 873b0d5 | 2011-01-21 22:53:48 +0100 | [diff] [blame] | 2471 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2472 | int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info) |
| 2473 | { |
| 2474 | struct drbd_conf *mdev; |
| 2475 | enum drbd_ret_code retcode; |
| 2476 | |
| 2477 | retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR); |
| 2478 | if (!adm_ctx.reply_skb) |
| 2479 | return retcode; |
| 2480 | if (retcode != NO_ERROR) |
| 2481 | goto out; |
| 2482 | |
| 2483 | mdev = adm_ctx.mdev; |
| 2484 | if (info->attrs[DRBD_NLA_START_OV_PARMS]) { |
| 2485 | /* resume from last known position, if possible */ |
| 2486 | struct start_ov_parms parms = |
| 2487 | { .ov_start_sector = mdev->ov_start_sector }; |
| 2488 | int err = start_ov_parms_from_attrs(&parms, info->attrs); |
| 2489 | if (err) { |
| 2490 | retcode = ERR_MANDATORY_TAG; |
| 2491 | drbd_msg_put_info(from_attrs_err_to_txt(err)); |
| 2492 | goto out; |
| 2493 | } |
| 2494 | /* w_make_ov_request expects position to be aligned */ |
| 2495 | mdev->ov_start_sector = parms.ov_start_sector & ~BM_SECT_PER_BIT; |
| 2496 | } |
Lars Ellenberg | 873b0d5 | 2011-01-21 22:53:48 +0100 | [diff] [blame] | 2497 | /* If there is still bitmap IO pending, e.g. previous resync or verify |
| 2498 | * just being finished, wait for it before requesting a new resync. */ |
| 2499 | wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags)); |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2500 | retcode = drbd_request_state(mdev,NS(conn,C_VERIFY_S)); |
| 2501 | out: |
| 2502 | drbd_adm_finish(info, retcode); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2503 | return 0; |
| 2504 | } |
| 2505 | |
| 2506 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2507 | int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2508 | { |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2509 | struct drbd_conf *mdev; |
| 2510 | enum drbd_ret_code retcode; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2511 | int skip_initial_sync = 0; |
| 2512 | int err; |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2513 | struct new_c_uuid_parms args; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2514 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2515 | retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR); |
| 2516 | if (!adm_ctx.reply_skb) |
| 2517 | return retcode; |
| 2518 | if (retcode != NO_ERROR) |
| 2519 | goto out_nolock; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2520 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2521 | mdev = adm_ctx.mdev; |
| 2522 | memset(&args, 0, sizeof(args)); |
| 2523 | if (info->attrs[DRBD_NLA_NEW_C_UUID_PARMS]) { |
| 2524 | err = new_c_uuid_parms_from_attrs(&args, info->attrs); |
| 2525 | if (err) { |
| 2526 | retcode = ERR_MANDATORY_TAG; |
| 2527 | drbd_msg_put_info(from_attrs_err_to_txt(err)); |
| 2528 | goto out_nolock; |
| 2529 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2530 | } |
| 2531 | |
Philipp Reisner | 8410da8 | 2011-02-11 20:11:10 +0100 | [diff] [blame] | 2532 | mutex_lock(mdev->state_mutex); /* Protects us against serialized state changes. */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2533 | |
| 2534 | if (!get_ldev(mdev)) { |
| 2535 | retcode = ERR_NO_DISK; |
| 2536 | goto out; |
| 2537 | } |
| 2538 | |
| 2539 | /* this is "skip initial sync", assume to be clean */ |
Philipp Reisner | 31890f4 | 2011-01-19 14:12:51 +0100 | [diff] [blame] | 2540 | if (mdev->state.conn == C_CONNECTED && mdev->tconn->agreed_pro_version >= 90 && |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2541 | mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED && args.clear_bm) { |
| 2542 | dev_info(DEV, "Preparing to skip initial sync\n"); |
| 2543 | skip_initial_sync = 1; |
| 2544 | } else if (mdev->state.conn != C_STANDALONE) { |
| 2545 | retcode = ERR_CONNECTED; |
| 2546 | goto out_dec; |
| 2547 | } |
| 2548 | |
| 2549 | drbd_uuid_set(mdev, UI_BITMAP, 0); /* Rotate UI_BITMAP to History 1, etc... */ |
| 2550 | drbd_uuid_new_current(mdev); /* New current, previous to UI_BITMAP */ |
| 2551 | |
| 2552 | if (args.clear_bm) { |
Lars Ellenberg | 20ceb2b | 2011-01-21 10:56:44 +0100 | [diff] [blame] | 2553 | err = drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write, |
| 2554 | "clear_n_write from new_c_uuid", BM_LOCKED_MASK); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2555 | if (err) { |
| 2556 | dev_err(DEV, "Writing bitmap failed with %d\n",err); |
| 2557 | retcode = ERR_IO_MD_DISK; |
| 2558 | } |
| 2559 | if (skip_initial_sync) { |
| 2560 | drbd_send_uuids_skip_initial_sync(mdev); |
| 2561 | _drbd_uuid_set(mdev, UI_BITMAP, 0); |
Lars Ellenberg | 62b0da3 | 2011-01-20 13:25:21 +0100 | [diff] [blame] | 2562 | drbd_print_uuids(mdev, "cleared bitmap UUID"); |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 2563 | spin_lock_irq(&mdev->tconn->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2564 | _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE), |
| 2565 | CS_VERBOSE, NULL); |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 2566 | spin_unlock_irq(&mdev->tconn->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2567 | } |
| 2568 | } |
| 2569 | |
| 2570 | drbd_md_sync(mdev); |
| 2571 | out_dec: |
| 2572 | put_ldev(mdev); |
| 2573 | out: |
Philipp Reisner | 8410da8 | 2011-02-11 20:11:10 +0100 | [diff] [blame] | 2574 | mutex_unlock(mdev->state_mutex); |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2575 | out_nolock: |
| 2576 | drbd_adm_finish(info, retcode); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2577 | return 0; |
| 2578 | } |
| 2579 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2580 | static enum drbd_ret_code |
| 2581 | drbd_check_conn_name(const char *name) |
Philipp Reisner | 774b305 | 2011-02-22 02:07:03 -0500 | [diff] [blame] | 2582 | { |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2583 | if (!name || !name[0]) { |
| 2584 | drbd_msg_put_info("connection name missing"); |
| 2585 | return ERR_MANDATORY_TAG; |
| 2586 | } |
| 2587 | /* if we want to use these in sysfs/configfs/debugfs some day, |
| 2588 | * we must not allow slashes */ |
| 2589 | if (strchr(name, '/')) { |
| 2590 | drbd_msg_put_info("invalid connection name"); |
| 2591 | return ERR_INVALID_REQUEST; |
| 2592 | } |
| 2593 | return NO_ERROR; |
| 2594 | } |
Philipp Reisner | 774b305 | 2011-02-22 02:07:03 -0500 | [diff] [blame] | 2595 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2596 | int drbd_adm_create_connection(struct sk_buff *skb, struct genl_info *info) |
| 2597 | { |
| 2598 | enum drbd_ret_code retcode; |
| 2599 | |
| 2600 | retcode = drbd_adm_prepare(skb, info, 0); |
| 2601 | if (!adm_ctx.reply_skb) |
| 2602 | return retcode; |
| 2603 | if (retcode != NO_ERROR) |
| 2604 | goto out; |
| 2605 | |
| 2606 | retcode = drbd_check_conn_name(adm_ctx.conn_name); |
| 2607 | if (retcode != NO_ERROR) |
| 2608 | goto out; |
| 2609 | |
| 2610 | if (adm_ctx.tconn) { |
Lars Ellenberg | 38f1961 | 2011-03-14 13:22:35 +0100 | [diff] [blame] | 2611 | if (info->nlhdr->nlmsg_flags & NLM_F_EXCL) { |
| 2612 | retcode = ERR_INVALID_REQUEST; |
| 2613 | drbd_msg_put_info("connection exists"); |
| 2614 | } |
| 2615 | /* else: still NO_ERROR */ |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2616 | goto out; |
Philipp Reisner | 774b305 | 2011-02-22 02:07:03 -0500 | [diff] [blame] | 2617 | } |
| 2618 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2619 | if (!drbd_new_tconn(adm_ctx.conn_name)) |
| 2620 | retcode = ERR_NOMEM; |
| 2621 | out: |
| 2622 | drbd_adm_finish(info, retcode); |
Philipp Reisner | 774b305 | 2011-02-22 02:07:03 -0500 | [diff] [blame] | 2623 | return 0; |
| 2624 | } |
| 2625 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2626 | int drbd_adm_add_minor(struct sk_buff *skb, struct genl_info *info) |
Philipp Reisner | 774b305 | 2011-02-22 02:07:03 -0500 | [diff] [blame] | 2627 | { |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2628 | struct drbd_genlmsghdr *dh = info->userhdr; |
| 2629 | enum drbd_ret_code retcode; |
Philipp Reisner | 774b305 | 2011-02-22 02:07:03 -0500 | [diff] [blame] | 2630 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2631 | retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONN); |
| 2632 | if (!adm_ctx.reply_skb) |
| 2633 | return retcode; |
| 2634 | if (retcode != NO_ERROR) |
| 2635 | goto out; |
Philipp Reisner | 774b305 | 2011-02-22 02:07:03 -0500 | [diff] [blame] | 2636 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2637 | /* FIXME drop minor_count parameter, limit to MINORMASK */ |
| 2638 | if (dh->minor >= minor_count) { |
| 2639 | drbd_msg_put_info("requested minor out of range"); |
| 2640 | retcode = ERR_INVALID_REQUEST; |
| 2641 | goto out; |
| 2642 | } |
| 2643 | /* FIXME we need a define here */ |
| 2644 | if (adm_ctx.volume >= 256) { |
| 2645 | drbd_msg_put_info("requested volume id out of range"); |
| 2646 | retcode = ERR_INVALID_REQUEST; |
| 2647 | goto out; |
Philipp Reisner | 774b305 | 2011-02-22 02:07:03 -0500 | [diff] [blame] | 2648 | } |
| 2649 | |
Lars Ellenberg | 38f1961 | 2011-03-14 13:22:35 +0100 | [diff] [blame] | 2650 | /* drbd_adm_prepare made sure already |
| 2651 | * that mdev->tconn and mdev->vnr match the request. */ |
| 2652 | if (adm_ctx.mdev) { |
| 2653 | if (info->nlhdr->nlmsg_flags & NLM_F_EXCL) |
| 2654 | retcode = ERR_MINOR_EXISTS; |
| 2655 | /* else: still NO_ERROR */ |
| 2656 | goto out; |
| 2657 | } |
| 2658 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2659 | retcode = conn_new_minor(adm_ctx.tconn, dh->minor, adm_ctx.volume); |
| 2660 | out: |
| 2661 | drbd_adm_finish(info, retcode); |
Philipp Reisner | 774b305 | 2011-02-22 02:07:03 -0500 | [diff] [blame] | 2662 | return 0; |
| 2663 | } |
| 2664 | |
Lars Ellenberg | 85f75dd7 | 2011-03-15 16:26:37 +0100 | [diff] [blame] | 2665 | static enum drbd_ret_code adm_delete_minor(struct drbd_conf *mdev) |
| 2666 | { |
| 2667 | if (mdev->state.disk == D_DISKLESS && |
| 2668 | /* no need to be mdev->state.conn == C_STANDALONE && |
| 2669 | * we may want to delete a minor from a live replication group. |
| 2670 | */ |
| 2671 | mdev->state.role == R_SECONDARY) { |
| 2672 | drbd_delete_device(mdev_to_minor(mdev)); |
| 2673 | return NO_ERROR; |
| 2674 | } else |
| 2675 | return ERR_MINOR_CONFIGURED; |
| 2676 | } |
| 2677 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2678 | int drbd_adm_delete_minor(struct sk_buff *skb, struct genl_info *info) |
Philipp Reisner | 774b305 | 2011-02-22 02:07:03 -0500 | [diff] [blame] | 2679 | { |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2680 | enum drbd_ret_code retcode; |
| 2681 | |
| 2682 | retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR); |
| 2683 | if (!adm_ctx.reply_skb) |
| 2684 | return retcode; |
| 2685 | if (retcode != NO_ERROR) |
| 2686 | goto out; |
| 2687 | |
Lars Ellenberg | 85f75dd7 | 2011-03-15 16:26:37 +0100 | [diff] [blame] | 2688 | mutex_lock(&drbd_cfg_mutex); |
| 2689 | retcode = adm_delete_minor(adm_ctx.mdev); |
| 2690 | mutex_unlock(&drbd_cfg_mutex); |
| 2691 | /* if this was the last volume of this connection, |
| 2692 | * this will terminate all threads */ |
| 2693 | if (retcode == NO_ERROR) |
Lars Ellenberg | cffec5b | 2011-03-10 23:33:15 +0100 | [diff] [blame] | 2694 | conn_reconfig_done(adm_ctx.tconn); |
Lars Ellenberg | 85f75dd7 | 2011-03-15 16:26:37 +0100 | [diff] [blame] | 2695 | out: |
| 2696 | drbd_adm_finish(info, retcode); |
| 2697 | return 0; |
| 2698 | } |
| 2699 | |
| 2700 | int drbd_adm_down(struct sk_buff *skb, struct genl_info *info) |
| 2701 | { |
| 2702 | enum drbd_ret_code retcode; |
| 2703 | enum drbd_state_rv rv; |
| 2704 | struct drbd_conf *mdev; |
| 2705 | unsigned i; |
| 2706 | |
| 2707 | retcode = drbd_adm_prepare(skb, info, 0); |
| 2708 | if (!adm_ctx.reply_skb) |
| 2709 | return retcode; |
| 2710 | if (retcode != NO_ERROR) |
| 2711 | goto out; |
| 2712 | |
| 2713 | if (!adm_ctx.tconn) { |
| 2714 | retcode = ERR_CONN_NOT_KNOWN; |
| 2715 | goto out; |
| 2716 | } |
| 2717 | |
| 2718 | mutex_lock(&drbd_cfg_mutex); |
| 2719 | /* demote */ |
| 2720 | idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) { |
| 2721 | retcode = drbd_set_role(mdev, R_SECONDARY, 0); |
| 2722 | if (retcode < SS_SUCCESS) { |
| 2723 | drbd_msg_put_info("failed to demote"); |
| 2724 | goto out_unlock; |
| 2725 | } |
| 2726 | } |
| 2727 | |
| 2728 | /* disconnect */ |
| 2729 | rv = conn_try_disconnect(adm_ctx.tconn, 0); |
| 2730 | if (rv < SS_SUCCESS) { |
| 2731 | retcode = rv; /* enum type mismatch! */ |
| 2732 | drbd_msg_put_info("failed to disconnect"); |
| 2733 | goto out_unlock; |
| 2734 | } |
| 2735 | |
| 2736 | /* detach */ |
| 2737 | idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) { |
| 2738 | rv = adm_detach(mdev); |
| 2739 | if (rv < SS_SUCCESS) { |
| 2740 | retcode = rv; /* enum type mismatch! */ |
| 2741 | drbd_msg_put_info("failed to detach"); |
| 2742 | goto out_unlock; |
| 2743 | } |
| 2744 | } |
| 2745 | |
| 2746 | /* delete volumes */ |
| 2747 | idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) { |
| 2748 | retcode = adm_delete_minor(mdev); |
| 2749 | if (retcode != NO_ERROR) { |
| 2750 | /* "can not happen" */ |
| 2751 | drbd_msg_put_info("failed to delete volume"); |
| 2752 | goto out_unlock; |
| 2753 | } |
| 2754 | } |
| 2755 | |
| 2756 | /* stop all threads */ |
| 2757 | conn_reconfig_done(adm_ctx.tconn); |
| 2758 | |
| 2759 | /* delete connection */ |
| 2760 | if (conn_lowest_minor(adm_ctx.tconn) < 0) { |
| 2761 | drbd_free_tconn(adm_ctx.tconn); |
| 2762 | retcode = NO_ERROR; |
| 2763 | } else { |
| 2764 | /* "can not happen" */ |
| 2765 | retcode = ERR_CONN_IN_USE; |
| 2766 | drbd_msg_put_info("failed to delete connection"); |
| 2767 | goto out_unlock; |
| 2768 | } |
| 2769 | out_unlock: |
| 2770 | mutex_unlock(&drbd_cfg_mutex); |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2771 | out: |
| 2772 | drbd_adm_finish(info, retcode); |
Philipp Reisner | 774b305 | 2011-02-22 02:07:03 -0500 | [diff] [blame] | 2773 | return 0; |
| 2774 | } |
| 2775 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2776 | int drbd_adm_delete_connection(struct sk_buff *skb, struct genl_info *info) |
Philipp Reisner | 774b305 | 2011-02-22 02:07:03 -0500 | [diff] [blame] | 2777 | { |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2778 | enum drbd_ret_code retcode; |
| 2779 | |
| 2780 | retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONN); |
| 2781 | if (!adm_ctx.reply_skb) |
| 2782 | return retcode; |
| 2783 | if (retcode != NO_ERROR) |
| 2784 | goto out; |
| 2785 | |
Lars Ellenberg | 85f75dd7 | 2011-03-15 16:26:37 +0100 | [diff] [blame] | 2786 | mutex_lock(&drbd_cfg_mutex); |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2787 | if (conn_lowest_minor(adm_ctx.tconn) < 0) { |
| 2788 | drbd_free_tconn(adm_ctx.tconn); |
| 2789 | retcode = NO_ERROR; |
Philipp Reisner | 774b305 | 2011-02-22 02:07:03 -0500 | [diff] [blame] | 2790 | } else { |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2791 | retcode = ERR_CONN_IN_USE; |
Philipp Reisner | 774b305 | 2011-02-22 02:07:03 -0500 | [diff] [blame] | 2792 | } |
Lars Ellenberg | 85f75dd7 | 2011-03-15 16:26:37 +0100 | [diff] [blame] | 2793 | mutex_unlock(&drbd_cfg_mutex); |
Philipp Reisner | 774b305 | 2011-02-22 02:07:03 -0500 | [diff] [blame] | 2794 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2795 | out: |
| 2796 | drbd_adm_finish(info, retcode); |
Philipp Reisner | 774b305 | 2011-02-22 02:07:03 -0500 | [diff] [blame] | 2797 | return 0; |
| 2798 | } |
| 2799 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2800 | void drbd_bcast_event(struct drbd_conf *mdev, const struct sib_info *sib) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2801 | { |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2802 | static atomic_t drbd_genl_seq = ATOMIC_INIT(2); /* two. */ |
| 2803 | struct sk_buff *msg; |
| 2804 | struct drbd_genlmsghdr *d_out; |
| 2805 | unsigned seq; |
| 2806 | int err = -ENOMEM; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2807 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2808 | seq = atomic_inc_return(&drbd_genl_seq); |
| 2809 | msg = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO); |
| 2810 | if (!msg) |
| 2811 | goto failed; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2812 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2813 | err = -EMSGSIZE; |
| 2814 | d_out = genlmsg_put(msg, 0, seq, &drbd_genl_family, 0, DRBD_EVENT); |
| 2815 | if (!d_out) /* cannot happen, but anyways. */ |
| 2816 | goto nla_put_failure; |
| 2817 | d_out->minor = mdev_to_minor(mdev); |
| 2818 | d_out->ret_code = 0; |
Philipp Reisner | 9f5180e | 2009-10-06 09:30:14 +0200 | [diff] [blame] | 2819 | |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2820 | if (nla_put_status_info(msg, mdev, sib)) |
| 2821 | goto nla_put_failure; |
| 2822 | genlmsg_end(msg, d_out); |
| 2823 | err = drbd_genl_multicast_events(msg, 0); |
| 2824 | /* msg has been consumed or freed in netlink_broadcast() */ |
| 2825 | if (err && err != -ESRCH) |
| 2826 | goto failed; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2827 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2828 | return; |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 2829 | |
| 2830 | nla_put_failure: |
| 2831 | nlmsg_free(msg); |
| 2832 | failed: |
| 2833 | dev_err(DEV, "Error %d while broadcasting event. " |
| 2834 | "Event seq:%u sib_reason:%u\n", |
| 2835 | err, seq, sib->sib_reason); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2836 | } |