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