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