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