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