blob: 3f2e1673808053a4de70077735a67723f9955b64 [file] [log] [blame]
Philipp Reisnerb411b362009-09-25 16:07:19 -07001/*
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 Reisnerb411b362009-09-25 16:07:19 -070026#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 Reisnerb411b362009-09-25 16:07:19 -070032#include <linux/blkpg.h>
33#include <linux/cpumask.h>
34#include "drbd_int.h"
Andreas Gruenbachera3603a62011-05-30 11:47:37 +020035#include "drbd_protocol.h"
Philipp Reisner265be2d2010-05-31 10:14:17 +020036#include "drbd_req.h"
Philipp Reisnerb411b362009-09-25 16:07:19 -070037#include <asm/unaligned.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070038#include <linux/drbd_limits.h>
Philipp Reisner87f7be42010-06-11 13:56:33 +020039#include <linux/kthread.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070040
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010041#include <net/genetlink.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070042
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010043/* .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
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +020047int drbd_adm_new_minor(struct sk_buff *skb, struct genl_info *info);
48int drbd_adm_del_minor(struct sk_buff *skb, struct genl_info *info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010049
Andreas Gruenbacher789c1b62011-06-06 16:16:44 +020050int drbd_adm_new_resource(struct sk_buff *skb, struct genl_info *info);
51int drbd_adm_del_resource(struct sk_buff *skb, struct genl_info *info);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +010052int drbd_adm_down(struct sk_buff *skb, struct genl_info *info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010053
54int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info);
55int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info);
Lars Ellenbergf3990022011-03-23 14:31:09 +010056int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010057int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info);
58int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info);
Lars Ellenbergf3990022011-03-23 14:31:09 +010059int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010060int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info);
61int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info);
62int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info);
63int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info);
64int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info);
65int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info);
66int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info);
67int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info);
68int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info);
69int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info);
70int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info);
Lars Ellenbergf3990022011-03-23 14:31:09 +010071int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010072int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info);
73int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info);
74/* .dumpit */
75int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb);
76
77#include <linux/drbd_genl_api.h>
Andreas Gruenbacher01b39b52011-06-10 12:57:26 +020078#include "drbd_nla.h"
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010079#include <linux/genl_magic_func.h>
80
81/* used blkdev_get_by_path, to claim our meta data device(s) */
Philipp Reisnerb411b362009-09-25 16:07:19 -070082static char *drbd_m_holder = "Hands off! this is DRBD's meta data device.";
83
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010084static void drbd_adm_send_reply(struct sk_buff *skb, struct genl_info *info)
85{
86 genlmsg_end(skb, genlmsg_data(nlmsg_data(nlmsg_hdr(skb))));
87 if (genlmsg_reply(skb, info))
88 printk(KERN_ERR "drbd: error sending genl reply\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -070089}
90
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010091/* Used on a fresh "drbd_adm_prepare"d reply_skb, this cannot fail: The only
92 * reason it could fail was no space in skb, and there are 4k available. */
Lars Ellenberga910b122014-04-28 18:43:21 +020093int drbd_msg_put_info(struct sk_buff *skb, const char *info)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010094{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010095 struct nlattr *nla;
96 int err = -EMSGSIZE;
Philipp Reisnerb411b362009-09-25 16:07:19 -070097
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010098 if (!info || !info[0])
99 return 0;
100
101 nla = nla_nest_start(skb, DRBD_NLA_CFG_REPLY);
102 if (!nla)
103 return err;
104
105 err = nla_put_string(skb, T_info_text, info);
106 if (err) {
107 nla_nest_cancel(skb, nla);
108 return err;
109 } else
110 nla_nest_end(skb, nla);
111 return 0;
112}
113
114/* This would be a good candidate for a "pre_doit" hook,
115 * and per-family private info->pointers.
116 * But we need to stay compatible with older kernels.
117 * If it returns successfully, adm_ctx members are valid.
Lars Ellenberg9e276872014-04-28 18:43:22 +0200118 *
119 * At this point, we still rely on the global genl_lock().
120 * If we want to avoid that, and allow "genl_family.parallel_ops", we may need
121 * to add additional synchronization against object destruction/modification.
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100122 */
123#define DRBD_ADM_NEED_MINOR 1
Andreas Gruenbacher44e52cf2011-06-14 16:07:32 +0200124#define DRBD_ADM_NEED_RESOURCE 2
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200125#define DRBD_ADM_NEED_CONNECTION 4
Lars Ellenberga910b122014-04-28 18:43:21 +0200126static int drbd_adm_prepare(struct drbd_config_context *adm_ctx,
127 struct sk_buff *skb, struct genl_info *info, unsigned flags)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100128{
129 struct drbd_genlmsghdr *d_in = info->userhdr;
130 const u8 cmd = info->genlhdr->cmd;
131 int err;
132
Lars Ellenberga910b122014-04-28 18:43:21 +0200133 memset(adm_ctx, 0, sizeof(*adm_ctx));
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100134
135 /* genl_rcv_msg only checks for CAP_NET_ADMIN on "GENL_ADMIN_PERM" :( */
Philipp Reisner98683652012-11-09 14:18:43 +0100136 if (cmd != DRBD_ADM_GET_STATUS && !capable(CAP_NET_ADMIN))
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100137 return -EPERM;
138
Lars Ellenberga910b122014-04-28 18:43:21 +0200139 adm_ctx->reply_skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
140 if (!adm_ctx->reply_skb) {
Andreas Gruenbacher1e2a2552011-05-24 14:17:08 +0200141 err = -ENOMEM;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100142 goto fail;
Andreas Gruenbacher1e2a2552011-05-24 14:17:08 +0200143 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100144
Lars Ellenberga910b122014-04-28 18:43:21 +0200145 adm_ctx->reply_dh = genlmsg_put_reply(adm_ctx->reply_skb,
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100146 info, &drbd_genl_family, 0, cmd);
147 /* put of a few bytes into a fresh skb of >= 4k will always succeed.
148 * but anyways */
Lars Ellenberga910b122014-04-28 18:43:21 +0200149 if (!adm_ctx->reply_dh) {
Andreas Gruenbacher1e2a2552011-05-24 14:17:08 +0200150 err = -ENOMEM;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100151 goto fail;
Andreas Gruenbacher1e2a2552011-05-24 14:17:08 +0200152 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100153
Lars Ellenberga910b122014-04-28 18:43:21 +0200154 adm_ctx->reply_dh->minor = d_in->minor;
155 adm_ctx->reply_dh->ret_code = NO_ERROR;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100156
Lars Ellenberga910b122014-04-28 18:43:21 +0200157 adm_ctx->volume = VOLUME_UNSPECIFIED;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100158 if (info->attrs[DRBD_NLA_CFG_CONTEXT]) {
159 struct nlattr *nla;
160 /* parse and validate only */
Lars Ellenbergf3990022011-03-23 14:31:09 +0100161 err = drbd_cfg_context_from_attrs(NULL, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100162 if (err)
163 goto fail;
164
165 /* It was present, and valid,
166 * copy it over to the reply skb. */
Lars Ellenberga910b122014-04-28 18:43:21 +0200167 err = nla_put_nohdr(adm_ctx->reply_skb,
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100168 info->attrs[DRBD_NLA_CFG_CONTEXT]->nla_len,
169 info->attrs[DRBD_NLA_CFG_CONTEXT]);
170 if (err)
171 goto fail;
172
Lars Ellenberg9e276872014-04-28 18:43:22 +0200173 /* and assign stuff to the adm_ctx */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100174 nla = nested_attr_tb[__nla_type(T_ctx_volume)];
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200175 if (nla)
Lars Ellenberga910b122014-04-28 18:43:21 +0200176 adm_ctx->volume = nla_get_u32(nla);
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +0200177 nla = nested_attr_tb[__nla_type(T_ctx_resource_name)];
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100178 if (nla)
Lars Ellenberga910b122014-04-28 18:43:21 +0200179 adm_ctx->resource_name = nla_data(nla);
180 adm_ctx->my_addr = nested_attr_tb[__nla_type(T_ctx_my_addr)];
181 adm_ctx->peer_addr = nested_attr_tb[__nla_type(T_ctx_peer_addr)];
182 if ((adm_ctx->my_addr &&
183 nla_len(adm_ctx->my_addr) > sizeof(adm_ctx->connection->my_addr)) ||
184 (adm_ctx->peer_addr &&
185 nla_len(adm_ctx->peer_addr) > sizeof(adm_ctx->connection->peer_addr))) {
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200186 err = -EINVAL;
187 goto fail;
188 }
189 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100190
Lars Ellenberga910b122014-04-28 18:43:21 +0200191 adm_ctx->minor = d_in->minor;
192 adm_ctx->device = minor_to_device(d_in->minor);
Lars Ellenberg9e276872014-04-28 18:43:22 +0200193
194 /* We are protected by the global genl_lock().
195 * But we may explicitly drop it/retake it in drbd_adm_set_role(),
196 * so make sure this object stays around. */
197 if (adm_ctx->device)
198 kref_get(&adm_ctx->device->kref);
199
Lars Ellenberga910b122014-04-28 18:43:21 +0200200 if (adm_ctx->resource_name) {
201 adm_ctx->resource = drbd_find_resource(adm_ctx->resource_name);
Andreas Gruenbacher4bc76042011-06-13 14:27:45 +0200202 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100203
Lars Ellenberga910b122014-04-28 18:43:21 +0200204 if (!adm_ctx->device && (flags & DRBD_ADM_NEED_MINOR)) {
205 drbd_msg_put_info(adm_ctx->reply_skb, "unknown minor");
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100206 return ERR_MINOR_INVALID;
207 }
Lars Ellenberga910b122014-04-28 18:43:21 +0200208 if (!adm_ctx->resource && (flags & DRBD_ADM_NEED_RESOURCE)) {
209 drbd_msg_put_info(adm_ctx->reply_skb, "unknown resource");
210 if (adm_ctx->resource_name)
Andreas Gruenbachera10f6b82011-06-09 01:26:16 +0200211 return ERR_RES_NOT_KNOWN;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100212 return ERR_INVALID_REQUEST;
213 }
214
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200215 if (flags & DRBD_ADM_NEED_CONNECTION) {
Lars Ellenberga910b122014-04-28 18:43:21 +0200216 if (adm_ctx->resource) {
217 drbd_msg_put_info(adm_ctx->reply_skb, "no resource name expected");
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200218 return ERR_INVALID_REQUEST;
219 }
Lars Ellenberga910b122014-04-28 18:43:21 +0200220 if (adm_ctx->device) {
221 drbd_msg_put_info(adm_ctx->reply_skb, "no minor number expected");
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200222 return ERR_INVALID_REQUEST;
223 }
Lars Ellenberga910b122014-04-28 18:43:21 +0200224 if (adm_ctx->my_addr && adm_ctx->peer_addr)
225 adm_ctx->connection = conn_get_by_addrs(nla_data(adm_ctx->my_addr),
226 nla_len(adm_ctx->my_addr),
227 nla_data(adm_ctx->peer_addr),
228 nla_len(adm_ctx->peer_addr));
229 if (!adm_ctx->connection) {
230 drbd_msg_put_info(adm_ctx->reply_skb, "unknown connection");
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200231 return ERR_INVALID_REQUEST;
232 }
233 }
234
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100235 /* some more paranoia, if the request was over-determined */
Lars Ellenberga910b122014-04-28 18:43:21 +0200236 if (adm_ctx->device && adm_ctx->resource &&
237 adm_ctx->device->resource != adm_ctx->resource) {
Andreas Gruenbacher4bc76042011-06-13 14:27:45 +0200238 pr_warning("request: minor=%u, resource=%s; but that minor belongs to resource %s\n",
Lars Ellenberga910b122014-04-28 18:43:21 +0200239 adm_ctx->minor, adm_ctx->resource->name,
240 adm_ctx->device->resource->name);
241 drbd_msg_put_info(adm_ctx->reply_skb, "minor exists in different resource");
Lars Ellenberg527f4b22011-03-14 13:58:03 +0100242 return ERR_INVALID_REQUEST;
243 }
Lars Ellenberga910b122014-04-28 18:43:21 +0200244 if (adm_ctx->device &&
245 adm_ctx->volume != VOLUME_UNSPECIFIED &&
246 adm_ctx->volume != adm_ctx->device->vnr) {
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100247 pr_warning("request: minor=%u, volume=%u; but that minor is volume %u in %s\n",
Lars Ellenberga910b122014-04-28 18:43:21 +0200248 adm_ctx->minor, adm_ctx->volume,
249 adm_ctx->device->vnr,
250 adm_ctx->device->resource->name);
251 drbd_msg_put_info(adm_ctx->reply_skb, "minor exists as different volume");
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100252 return ERR_INVALID_REQUEST;
253 }
Philipp Reisner0ace9df2011-04-24 10:53:19 +0200254
Lars Ellenberg9e276872014-04-28 18:43:22 +0200255 /* still, provide adm_ctx->resource always, if possible. */
256 if (!adm_ctx->resource) {
257 adm_ctx->resource = adm_ctx->device ? adm_ctx->device->resource
258 : adm_ctx->connection ? adm_ctx->connection->resource : NULL;
259 if (adm_ctx->resource)
260 kref_get(&adm_ctx->resource->kref);
261 }
262
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100263 return NO_ERROR;
264
265fail:
Lars Ellenberga910b122014-04-28 18:43:21 +0200266 nlmsg_free(adm_ctx->reply_skb);
267 adm_ctx->reply_skb = NULL;
Andreas Gruenbacher1e2a2552011-05-24 14:17:08 +0200268 return err;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100269}
270
Lars Ellenberga910b122014-04-28 18:43:21 +0200271static int drbd_adm_finish(struct drbd_config_context *adm_ctx,
272 struct genl_info *info, int retcode)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100273{
Lars Ellenberg9e276872014-04-28 18:43:22 +0200274 if (adm_ctx->device) {
275 kref_put(&adm_ctx->device->kref, drbd_destroy_device);
276 adm_ctx->device = NULL;
277 }
Lars Ellenberga910b122014-04-28 18:43:21 +0200278 if (adm_ctx->connection) {
279 kref_put(&adm_ctx->connection->kref, &drbd_destroy_connection);
280 adm_ctx->connection = NULL;
Philipp Reisner0ace9df2011-04-24 10:53:19 +0200281 }
Lars Ellenberga910b122014-04-28 18:43:21 +0200282 if (adm_ctx->resource) {
283 kref_put(&adm_ctx->resource->kref, drbd_destroy_resource);
284 adm_ctx->resource = NULL;
Andreas Gruenbacher4bc76042011-06-13 14:27:45 +0200285 }
Philipp Reisner0ace9df2011-04-24 10:53:19 +0200286
Lars Ellenberga910b122014-04-28 18:43:21 +0200287 if (!adm_ctx->reply_skb)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100288 return -ENOMEM;
289
Lars Ellenberga910b122014-04-28 18:43:21 +0200290 adm_ctx->reply_dh->ret_code = retcode;
291 drbd_adm_send_reply(adm_ctx->reply_skb, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100292 return 0;
293}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700294
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200295static void setup_khelper_env(struct drbd_connection *connection, char **envp)
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100296{
297 char *afs;
298
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200299 /* FIXME: A future version will not allow this case. */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200300 if (connection->my_addr_len == 0 || connection->peer_addr_len == 0)
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200301 return;
302
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200303 switch (((struct sockaddr *)&connection->peer_addr)->sa_family) {
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200304 case AF_INET6:
305 afs = "ipv6";
306 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI6",
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200307 &((struct sockaddr_in6 *)&connection->peer_addr)->sin6_addr);
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200308 break;
309 case AF_INET:
310 afs = "ipv4";
311 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200312 &((struct sockaddr_in *)&connection->peer_addr)->sin_addr);
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200313 break;
314 default:
315 afs = "ssocks";
316 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200317 &((struct sockaddr_in *)&connection->peer_addr)->sin_addr);
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100318 }
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200319 snprintf(envp[3], 20, "DRBD_PEER_AF=%s", afs);
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100320}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700321
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200322int drbd_khelper(struct drbd_device *device, char *cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700323{
324 char *envp[] = { "HOME=/",
325 "TERM=linux",
326 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100327 (char[20]) { }, /* address family */
328 (char[60]) { }, /* address */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700329 NULL };
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100330 char mb[12];
Philipp Reisnerb411b362009-09-25 16:07:19 -0700331 char *argv[] = {usermode_helper, cmd, mb, NULL };
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200332 struct drbd_connection *connection = first_peer_device(device)->connection;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100333 struct sib_info sib;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700334 int ret;
335
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200336 if (current == connection->worker.task)
337 set_bit(CALLBACK_PENDING, &connection->flags);
Lars Ellenbergc2ba6862012-06-14 15:14:06 +0200338
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200339 snprintf(mb, 12, "minor-%d", device_to_minor(device));
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200340 setup_khelper_env(connection, envp);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700341
Lars Ellenberg1090c052010-07-19 17:41:04 +0200342 /* The helper may take some time.
343 * write out any unsynced meta data changes now */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200344 drbd_md_sync(device);
Lars Ellenberg1090c052010-07-19 17:41:04 +0200345
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200346 drbd_info(device, "helper command: %s %s %s\n", usermode_helper, cmd, mb);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100347 sib.sib_reason = SIB_HELPER_PRE;
348 sib.helper_name = cmd;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200349 drbd_bcast_event(device, &sib);
Oleg Nesterov70834d32012-03-23 15:02:46 -0700350 ret = call_usermodehelper(usermode_helper, argv, envp, UMH_WAIT_PROC);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700351 if (ret)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200352 drbd_warn(device, "helper command: %s %s %s exit code %u (0x%x)\n",
Philipp Reisnerb411b362009-09-25 16:07:19 -0700353 usermode_helper, cmd, mb,
354 (ret >> 8) & 0xff, ret);
355 else
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200356 drbd_info(device, "helper command: %s %s %s exit code %u (0x%x)\n",
Philipp Reisnerb411b362009-09-25 16:07:19 -0700357 usermode_helper, cmd, mb,
358 (ret >> 8) & 0xff, ret);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100359 sib.sib_reason = SIB_HELPER_POST;
360 sib.helper_exit_code = ret;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200361 drbd_bcast_event(device, &sib);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700362
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200363 if (current == connection->worker.task)
364 clear_bit(CALLBACK_PENDING, &connection->flags);
Lars Ellenbergc2ba6862012-06-14 15:14:06 +0200365
Philipp Reisnerb411b362009-09-25 16:07:19 -0700366 if (ret < 0) /* Ignore any ERRNOs we got. */
367 ret = 0;
368
369 return ret;
370}
371
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200372static int conn_khelper(struct drbd_connection *connection, char *cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700373{
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100374 char *envp[] = { "HOME=/",
375 "TERM=linux",
376 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
377 (char[20]) { }, /* address family */
378 (char[60]) { }, /* address */
379 NULL };
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +0200380 char *resource_name = connection->resource->name;
381 char *argv[] = {usermode_helper, cmd, resource_name, NULL };
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100382 int ret;
383
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200384 setup_khelper_env(connection, envp);
385 conn_md_sync(connection);
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100386
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +0200387 drbd_info(connection, "helper command: %s %s %s\n", usermode_helper, cmd, resource_name);
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100388 /* TODO: conn_bcast_event() ?? */
389
Philipp Reisner98683652012-11-09 14:18:43 +0100390 ret = call_usermodehelper(usermode_helper, argv, envp, UMH_WAIT_PROC);
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100391 if (ret)
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +0200392 drbd_warn(connection, "helper command: %s %s %s exit code %u (0x%x)\n",
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +0200393 usermode_helper, cmd, resource_name,
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100394 (ret >> 8) & 0xff, ret);
395 else
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +0200396 drbd_info(connection, "helper command: %s %s %s exit code %u (0x%x)\n",
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +0200397 usermode_helper, cmd, resource_name,
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100398 (ret >> 8) & 0xff, ret);
399 /* TODO: conn_bcast_event() ?? */
400
401 if (ret < 0) /* Ignore any ERRNOs we got. */
402 ret = 0;
403
404 return ret;
405}
406
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200407static enum drbd_fencing_p highest_fencing_policy(struct drbd_connection *connection)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700408{
Philipp Reisnercb703452011-03-24 11:03:07 +0100409 enum drbd_fencing_p fp = FP_NOT_AVAIL;
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200410 struct drbd_peer_device *peer_device;
Philipp Reisnercb703452011-03-24 11:03:07 +0100411 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700412
Philipp Reisner695d08f2011-04-11 22:53:32 -0700413 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200414 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
415 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200416 if (get_ldev_if_state(device, D_CONSISTENT)) {
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +0200417 struct disk_conf *disk_conf =
418 rcu_dereference(peer_device->device->ldev->disk_conf);
419 fp = max_t(enum drbd_fencing_p, fp, disk_conf->fencing);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200420 put_ldev(device);
Philipp Reisnercb703452011-03-24 11:03:07 +0100421 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700422 }
Philipp Reisner695d08f2011-04-11 22:53:32 -0700423 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700424
Philipp Reisnerd7fe69c2014-04-28 18:43:13 +0200425 if (fp == FP_NOT_AVAIL) {
426 /* IO Suspending works on the whole resource.
427 Do it only for one device. */
428 vnr = 0;
429 peer_device = idr_get_next(&connection->peer_devices, &vnr);
430 drbd_change_state(peer_device->device, CS_VERBOSE | CS_HARD, NS(susp_fen, 0));
431 }
432
Philipp Reisnercb703452011-03-24 11:03:07 +0100433 return fp;
434}
435
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200436bool conn_try_outdate_peer(struct drbd_connection *connection)
Philipp Reisnercb703452011-03-24 11:03:07 +0100437{
Philipp Reisner28e448b2013-06-25 16:50:06 +0200438 unsigned int connect_cnt;
Philipp Reisnercb703452011-03-24 11:03:07 +0100439 union drbd_state mask = { };
440 union drbd_state val = { };
441 enum drbd_fencing_p fp;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700442 char *ex_to_string;
443 int r;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700444
Philipp Reisner02df6fe2014-04-28 18:43:33 +0200445 spin_lock_irq(&connection->resource->req_lock);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200446 if (connection->cstate >= C_WF_REPORT_PARAMS) {
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +0200447 drbd_err(connection, "Expected cstate < C_WF_REPORT_PARAMS\n");
Philipp Reisner02df6fe2014-04-28 18:43:33 +0200448 spin_unlock_irq(&connection->resource->req_lock);
Philipp Reisnercb703452011-03-24 11:03:07 +0100449 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700450 }
451
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200452 connect_cnt = connection->connect_cnt;
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200453 spin_unlock_irq(&connection->resource->req_lock);
Philipp Reisner28e448b2013-06-25 16:50:06 +0200454
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200455 fp = highest_fencing_policy(connection);
Philipp Reisnercb703452011-03-24 11:03:07 +0100456 switch (fp) {
457 case FP_NOT_AVAIL:
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +0200458 drbd_warn(connection, "Not fencing peer, I'm not even Consistent myself.\n");
Philipp Reisnercb703452011-03-24 11:03:07 +0100459 goto out;
460 case FP_DONT_CARE:
461 return true;
462 default: ;
463 }
464
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200465 r = conn_khelper(connection, "fence-peer");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700466
467 switch ((r>>8) & 0xff) {
468 case 3: /* peer is inconsistent */
469 ex_to_string = "peer is inconsistent or worse";
Philipp Reisnercb703452011-03-24 11:03:07 +0100470 mask.pdsk = D_MASK;
471 val.pdsk = D_INCONSISTENT;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700472 break;
473 case 4: /* peer got outdated, or was already outdated */
474 ex_to_string = "peer was fenced";
Philipp Reisnercb703452011-03-24 11:03:07 +0100475 mask.pdsk = D_MASK;
476 val.pdsk = D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700477 break;
478 case 5: /* peer was down */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200479 if (conn_highest_disk(connection) == D_UP_TO_DATE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700480 /* we will(have) create(d) a new UUID anyways... */
481 ex_to_string = "peer is unreachable, assumed to be dead";
Philipp Reisnercb703452011-03-24 11:03:07 +0100482 mask.pdsk = D_MASK;
483 val.pdsk = D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700484 } else {
485 ex_to_string = "peer unreachable, doing nothing since disk != UpToDate";
Philipp Reisnerb411b362009-09-25 16:07:19 -0700486 }
487 break;
488 case 6: /* Peer is primary, voluntarily outdate myself.
489 * This is useful when an unconnected R_SECONDARY is asked to
490 * become R_PRIMARY, but finds the other peer being active. */
491 ex_to_string = "peer is active";
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +0200492 drbd_warn(connection, "Peer is primary, outdating myself.\n");
Philipp Reisnercb703452011-03-24 11:03:07 +0100493 mask.disk = D_MASK;
494 val.disk = D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700495 break;
496 case 7:
497 if (fp != FP_STONITH)
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +0200498 drbd_err(connection, "fence-peer() = 7 && fencing != Stonith !!!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700499 ex_to_string = "peer was stonithed";
Philipp Reisnercb703452011-03-24 11:03:07 +0100500 mask.pdsk = D_MASK;
501 val.pdsk = D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700502 break;
503 default:
504 /* The script is broken ... */
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +0200505 drbd_err(connection, "fence-peer helper broken, returned %d\n", (r>>8)&0xff);
Philipp Reisnercb703452011-03-24 11:03:07 +0100506 return false; /* Eventually leave IO frozen */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700507 }
508
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +0200509 drbd_info(connection, "fence-peer helper returned %d (%s)\n",
Philipp Reisnercb703452011-03-24 11:03:07 +0100510 (r>>8) & 0xff, ex_to_string);
Philipp Reisnerfb22c402010-09-08 23:20:21 +0200511
Philipp Reisnercb703452011-03-24 11:03:07 +0100512 out:
Philipp Reisnerfb22c402010-09-08 23:20:21 +0200513
Philipp Reisnercb703452011-03-24 11:03:07 +0100514 /* Not using
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200515 conn_request_state(connection, mask, val, CS_VERBOSE);
Philipp Reisnercb703452011-03-24 11:03:07 +0100516 here, because we might were able to re-establish the connection in the
517 meantime. */
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200518 spin_lock_irq(&connection->resource->req_lock);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200519 if (connection->cstate < C_WF_REPORT_PARAMS && !test_bit(STATE_SENT, &connection->flags)) {
520 if (connection->connect_cnt != connect_cnt)
Philipp Reisner28e448b2013-06-25 16:50:06 +0200521 /* In case the connection was established and droped
522 while the fence-peer handler was running, ignore it */
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +0200523 drbd_info(connection, "Ignoring fence-peer exit code\n");
Philipp Reisner28e448b2013-06-25 16:50:06 +0200524 else
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200525 _conn_request_state(connection, mask, val, CS_VERBOSE);
Philipp Reisner28e448b2013-06-25 16:50:06 +0200526 }
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200527 spin_unlock_irq(&connection->resource->req_lock);
Philipp Reisnercb703452011-03-24 11:03:07 +0100528
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200529 return conn_highest_pdsk(connection) <= D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700530}
531
Philipp Reisner87f7be42010-06-11 13:56:33 +0200532static int _try_outdate_peer_async(void *data)
533{
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200534 struct drbd_connection *connection = (struct drbd_connection *)data;
Philipp Reisner87f7be42010-06-11 13:56:33 +0200535
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200536 conn_try_outdate_peer(connection);
Philipp Reisner21423fa2011-05-17 14:19:41 +0200537
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +0200538 kref_put(&connection->kref, drbd_destroy_connection);
Philipp Reisner87f7be42010-06-11 13:56:33 +0200539 return 0;
540}
541
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200542void conn_try_outdate_peer_async(struct drbd_connection *connection)
Philipp Reisner87f7be42010-06-11 13:56:33 +0200543{
544 struct task_struct *opa;
545
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200546 kref_get(&connection->kref);
Lars Ellenbergbbc1c5e2014-07-09 21:18:32 +0200547 /* We may just have force_sig()'ed this thread
548 * to get it out of some blocking network function.
549 * Clear signals; otherwise kthread_run(), which internally uses
550 * wait_on_completion_killable(), will mistake our pending signal
551 * for a new fatal signal and fail. */
552 flush_signals(current);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200553 opa = kthread_run(_try_outdate_peer_async, connection, "drbd_async_h");
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +0200554 if (IS_ERR(opa)) {
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +0200555 drbd_err(connection, "out of mem, failed to invoke fence-peer helper\n");
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +0200556 kref_put(&connection->kref, drbd_destroy_connection);
Philipp Reisner9dc9fbb2011-04-22 15:23:32 +0200557 }
Philipp Reisner87f7be42010-06-11 13:56:33 +0200558}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700559
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100560enum drbd_state_rv
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200561drbd_set_role(struct drbd_device *device, enum drbd_role new_role, int force)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700562{
563 const int max_tries = 4;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100564 enum drbd_state_rv rv = SS_UNKNOWN_ERROR;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200565 struct net_conf *nc;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700566 int try = 0;
567 int forced = 0;
568 union drbd_state mask, val;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700569
Andreas Gruenbacherb6f85ef2011-07-06 15:03:31 +0200570 if (new_role == R_PRIMARY) {
571 struct drbd_connection *connection;
572
573 /* Detect dead peers as soon as possible. */
574
575 rcu_read_lock();
576 for_each_connection(connection, device->resource)
577 request_ping(connection);
578 rcu_read_unlock();
579 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700580
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200581 mutex_lock(device->state_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700582
583 mask.i = 0; mask.role = R_MASK;
584 val.i = 0; val.role = new_role;
585
586 while (try++ < max_tries) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200587 rv = _drbd_request_state(device, mask, val, CS_WAIT_COMPLETE);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700588
589 /* in case we first succeeded to outdate,
590 * but now suddenly could establish a connection */
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100591 if (rv == SS_CW_FAILED_BY_PEER && mask.pdsk != 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700592 val.pdsk = 0;
593 mask.pdsk = 0;
594 continue;
595 }
596
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100597 if (rv == SS_NO_UP_TO_DATE_DISK && force &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200598 (device->state.disk < D_UP_TO_DATE &&
599 device->state.disk >= D_INCONSISTENT)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700600 mask.disk = D_MASK;
601 val.disk = D_UP_TO_DATE;
602 forced = 1;
603 continue;
604 }
605
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100606 if (rv == SS_NO_UP_TO_DATE_DISK &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200607 device->state.disk == D_CONSISTENT && mask.pdsk == 0) {
Andreas Gruenbacher0b0ba1e2011-06-27 16:23:33 +0200608 D_ASSERT(device, device->state.pdsk == D_UNKNOWN);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700609
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200610 if (conn_try_outdate_peer(first_peer_device(device)->connection)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700611 val.disk = D_UP_TO_DATE;
612 mask.disk = D_MASK;
613 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700614 continue;
615 }
616
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100617 if (rv == SS_NOTHING_TO_DO)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100618 goto out;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100619 if (rv == SS_PRIMARY_NOP && mask.pdsk == 0) {
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200620 if (!conn_try_outdate_peer(first_peer_device(device)->connection) && force) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200621 drbd_warn(device, "Forced into split brain situation!\n");
Philipp Reisnercb703452011-03-24 11:03:07 +0100622 mask.pdsk = D_MASK;
623 val.pdsk = D_OUTDATED;
624
Philipp Reisnerb411b362009-09-25 16:07:19 -0700625 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700626 continue;
627 }
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100628 if (rv == SS_TWO_PRIMARIES) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700629 /* Maybe the peer is detected as dead very soon...
630 retry at most once more in this case. */
Philipp Reisner44ed1672011-04-19 17:10:19 +0200631 int timeo;
632 rcu_read_lock();
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200633 nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
Philipp Reisner44ed1672011-04-19 17:10:19 +0200634 timeo = nc ? (nc->ping_timeo + 1) * HZ / 10 : 1;
635 rcu_read_unlock();
636 schedule_timeout_interruptible(timeo);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700637 if (try < max_tries)
638 try = max_tries - 1;
639 continue;
640 }
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100641 if (rv < SS_SUCCESS) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200642 rv = _drbd_request_state(device, mask, val,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700643 CS_VERBOSE + CS_WAIT_COMPLETE);
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100644 if (rv < SS_SUCCESS)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100645 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700646 }
647 break;
648 }
649
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100650 if (rv < SS_SUCCESS)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100651 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700652
653 if (forced)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200654 drbd_warn(device, "Forced to consider local data as UpToDate!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700655
656 /* Wait until nothing is on the fly :) */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200657 wait_event(device->misc_wait, atomic_read(&device->ap_pending_cnt) == 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700658
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +0100659 /* FIXME also wait for all pending P_BARRIER_ACK? */
660
Philipp Reisnerb411b362009-09-25 16:07:19 -0700661 if (new_role == R_SECONDARY) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200662 set_disk_ro(device->vdisk, true);
663 if (get_ldev(device)) {
664 device->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
665 put_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700666 }
667 } else {
Lars Ellenberg9e276872014-04-28 18:43:22 +0200668 /* Called from drbd_adm_set_role only.
669 * We are still holding the conf_update mutex. */
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200670 nc = first_peer_device(device)->connection->net_conf;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200671 if (nc)
Andreas Gruenbacher6139f602011-05-06 20:00:02 +0200672 nc->discard_my_data = 0; /* without copy; single bit op is atomic */
Philipp Reisner91fd4da2011-04-20 17:47:29 +0200673
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200674 set_disk_ro(device->vdisk, false);
675 if (get_ldev(device)) {
676 if (((device->state.conn < C_CONNECTED ||
677 device->state.pdsk <= D_FAILED)
678 && device->ldev->md.uuid[UI_BITMAP] == 0) || forced)
679 drbd_uuid_new_current(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700680
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200681 device->ldev->md.uuid[UI_CURRENT] |= (u64)1;
682 put_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700683 }
684 }
685
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100686 /* writeout of activity log covered areas of the bitmap
687 * to stable storage done in after state change already */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700688
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200689 if (device->state.conn >= C_WF_REPORT_PARAMS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700690 /* if this was forced, we should consider sync */
691 if (forced)
Andreas Gruenbacher69a22772011-08-09 00:47:13 +0200692 drbd_send_uuids(first_peer_device(device));
693 drbd_send_current_state(first_peer_device(device));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700694 }
695
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200696 drbd_md_sync(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700697
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200698 kobject_uevent(&disk_to_dev(device->vdisk)->kobj, KOBJ_CHANGE);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100699out:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200700 mutex_unlock(device->state_mutex);
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100701 return rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700702}
703
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100704static const char *from_attrs_err_to_txt(int err)
Lars Ellenbergef50a3e2010-09-01 14:39:30 +0200705{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100706 return err == -ENOMSG ? "required attribute missing" :
707 err == -EOPNOTSUPP ? "unknown mandatory attribute" :
Lars Ellenbergf3990022011-03-23 14:31:09 +0100708 err == -EEXIST ? "can not change invariant setting" :
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100709 "invalid attribute value";
Lars Ellenbergef50a3e2010-09-01 14:39:30 +0200710}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700711
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100712int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700713{
Lars Ellenberga910b122014-04-28 18:43:21 +0200714 struct drbd_config_context adm_ctx;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100715 struct set_role_parms parms;
716 int err;
717 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700718
Lars Ellenberga910b122014-04-28 18:43:21 +0200719 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100720 if (!adm_ctx.reply_skb)
721 return retcode;
722 if (retcode != NO_ERROR)
723 goto out;
724
725 memset(&parms, 0, sizeof(parms));
726 if (info->attrs[DRBD_NLA_SET_ROLE_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +0100727 err = set_role_parms_from_attrs(&parms, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100728 if (err) {
729 retcode = ERR_MANDATORY_TAG;
Lars Ellenberga910b122014-04-28 18:43:21 +0200730 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100731 goto out;
732 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700733 }
Lars Ellenberg9e276872014-04-28 18:43:22 +0200734 genl_unlock();
735 mutex_lock(&adm_ctx.resource->adm_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700736
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100737 if (info->genlhdr->cmd == DRBD_ADM_PRIMARY)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200738 retcode = drbd_set_role(adm_ctx.device, R_PRIMARY, parms.assume_uptodate);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100739 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200740 retcode = drbd_set_role(adm_ctx.device, R_SECONDARY, 0);
Lars Ellenberg9e276872014-04-28 18:43:22 +0200741
742 mutex_unlock(&adm_ctx.resource->adm_mutex);
743 genl_lock();
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100744out:
Lars Ellenberga910b122014-04-28 18:43:21 +0200745 drbd_adm_finish(&adm_ctx, info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700746 return 0;
747}
748
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100749/* Initializes the md.*_offset members, so we are able to find
750 * the on disk meta data.
751 *
752 * We currently have two possible layouts:
753 * external:
754 * |----------- md_size_sect ------------------|
755 * [ 4k superblock ][ activity log ][ Bitmap ]
756 * | al_offset == 8 |
757 * | bm_offset = al_offset + X |
758 * ==> bitmap sectors = md_size_sect - bm_offset
759 *
760 * internal:
761 * |----------- md_size_sect ------------------|
762 * [data.....][ Bitmap ][ activity log ][ 4k superblock ]
763 * | al_offset < 0 |
764 * | bm_offset = al_offset - Y |
765 * ==> bitmap sectors = Y = al_offset - bm_offset
766 *
767 * Activity log size used to be fixed 32kB,
768 * but is about to become configurable.
769 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200770static void drbd_md_set_sector_offsets(struct drbd_device *device,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700771 struct drbd_backing_dev *bdev)
772{
773 sector_t md_size_sect = 0;
Lars Ellenbergc04ccaa2013-03-19 18:16:47 +0100774 unsigned int al_size_sect = bdev->md.al_size_4k * 8;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200775
Lars Ellenberg3a4d4eb2013-03-19 18:16:44 +0100776 bdev->md.md_offset = drbd_md_ss(bdev);
777
Lars Ellenberg68e41a42013-03-19 18:16:45 +0100778 switch (bdev->md.meta_dev_idx) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700779 default:
780 /* v07 style fixed size indexed meta data */
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100781 bdev->md.md_size_sect = MD_128MB_SECT;
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100782 bdev->md.al_offset = MD_4kB_SECT;
783 bdev->md.bm_offset = MD_4kB_SECT + al_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700784 break;
785 case DRBD_MD_INDEX_FLEX_EXT:
786 /* just occupy the full device; unit: sectors */
787 bdev->md.md_size_sect = drbd_get_capacity(bdev->md_bdev);
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100788 bdev->md.al_offset = MD_4kB_SECT;
789 bdev->md.bm_offset = MD_4kB_SECT + al_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700790 break;
791 case DRBD_MD_INDEX_INTERNAL:
792 case DRBD_MD_INDEX_FLEX_INT:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700793 /* al size is still fixed */
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100794 bdev->md.al_offset = -al_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700795 /* we need (slightly less than) ~ this much bitmap sectors: */
796 md_size_sect = drbd_get_capacity(bdev->backing_bdev);
797 md_size_sect = ALIGN(md_size_sect, BM_SECT_PER_EXT);
798 md_size_sect = BM_SECT_TO_EXT(md_size_sect);
799 md_size_sect = ALIGN(md_size_sect, 8);
800
801 /* plus the "drbd meta data super block",
802 * and the activity log; */
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100803 md_size_sect += MD_4kB_SECT + al_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700804
805 bdev->md.md_size_sect = md_size_sect;
806 /* bitmap offset is adjusted by 'super' block size */
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100807 bdev->md.bm_offset = -md_size_sect + MD_4kB_SECT;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700808 break;
809 }
810}
811
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100812/* input size is expected to be in KB */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700813char *ppsize(char *buf, unsigned long long size)
814{
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100815 /* Needs 9 bytes at max including trailing NUL:
816 * -1ULL ==> "16384 EB" */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700817 static char units[] = { 'K', 'M', 'G', 'T', 'P', 'E' };
818 int base = 0;
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100819 while (size >= 10000 && base < sizeof(units)-1) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700820 /* shift + round */
821 size = (size >> 10) + !!(size & (1<<9));
822 base++;
823 }
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100824 sprintf(buf, "%u %cB", (unsigned)size, units[base]);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700825
826 return buf;
827}
828
829/* there is still a theoretical deadlock when called from receiver
830 * on an D_INCONSISTENT R_PRIMARY:
831 * remote READ does inc_ap_bio, receiver would need to receive answer
832 * packet from remote to dec_ap_bio again.
833 * receiver receive_sizes(), comes here,
834 * waits for ap_bio_cnt == 0. -> deadlock.
835 * but this cannot happen, actually, because:
836 * R_PRIMARY D_INCONSISTENT, and peer's disk is unreachable
837 * (not connected, or bad/no disk on peer):
838 * see drbd_fail_request_early, ap_bio_cnt is zero.
839 * R_PRIMARY D_INCONSISTENT, and C_SYNC_TARGET:
840 * peer may not initiate a resize.
841 */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100842/* Note these are not to be confused with
843 * drbd_adm_suspend_io/drbd_adm_resume_io,
844 * which are (sub) state changes triggered by admin (drbdsetup),
845 * and can be long lived.
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200846 * This changes an device->flag, is triggered by drbd internals,
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100847 * and should be short-lived. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200848void drbd_suspend_io(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700849{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200850 set_bit(SUSPEND_IO, &device->flags);
851 if (drbd_suspended(device))
Philipp Reisner265be2d2010-05-31 10:14:17 +0200852 return;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200853 wait_event(device->misc_wait, !atomic_read(&device->ap_bio_cnt));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700854}
855
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200856void drbd_resume_io(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700857{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200858 clear_bit(SUSPEND_IO, &device->flags);
859 wake_up(&device->misc_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700860}
861
862/**
863 * drbd_determine_dev_size() - Sets the right device size obeying all constraints
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200864 * @device: DRBD device.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700865 *
866 * Returns 0 on success, negative return values indicate errors.
867 * You should call drbd_md_sync() after calling this function.
868 */
Philipp Reisnerd752b262013-06-25 16:50:08 +0200869enum determine_dev_size
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200870drbd_determine_dev_size(struct drbd_device *device, enum dds_flags flags, struct resize_parms *rs) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700871{
872 sector_t prev_first_sect, prev_size; /* previous meta location */
Lars Ellenbergcccac982013-03-19 18:16:46 +0100873 sector_t la_size_sect, u_size;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200874 struct drbd_md *md = &device->ldev->md;
Philipp Reisnerd752b262013-06-25 16:50:08 +0200875 u32 prev_al_stripe_size_4k;
876 u32 prev_al_stripes;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700877 sector_t size;
878 char ppb[10];
Philipp Reisnerd752b262013-06-25 16:50:08 +0200879 void *buffer;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700880
881 int md_moved, la_size_changed;
Philipp Reisnere96c9632013-06-25 16:50:07 +0200882 enum determine_dev_size rv = DS_UNCHANGED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700883
884 /* race:
885 * application request passes inc_ap_bio,
886 * but then cannot get an AL-reference.
887 * this function later may wait on ap_bio_cnt == 0. -> deadlock.
888 *
889 * to avoid that:
890 * Suspend IO right here.
891 * still lock the act_log to not trigger ASSERTs there.
892 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200893 drbd_suspend_io(device);
894 buffer = drbd_md_get_buffer(device); /* Lock meta-data IO */
Philipp Reisnerd752b262013-06-25 16:50:08 +0200895 if (!buffer) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200896 drbd_resume_io(device);
Philipp Reisnerd752b262013-06-25 16:50:08 +0200897 return DS_ERROR;
898 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700899
900 /* no wait necessary anymore, actually we could assert that */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200901 wait_event(device->al_wait, lc_try_lock(device->act_log));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700902
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200903 prev_first_sect = drbd_md_first_sector(device->ldev);
904 prev_size = device->ldev->md.md_size_sect;
905 la_size_sect = device->ldev->md.la_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700906
Philipp Reisnerd752b262013-06-25 16:50:08 +0200907 if (rs) {
908 /* rs is non NULL if we should change the AL layout only */
909
910 prev_al_stripes = md->al_stripes;
911 prev_al_stripe_size_4k = md->al_stripe_size_4k;
912
913 md->al_stripes = rs->al_stripes;
914 md->al_stripe_size_4k = rs->al_stripe_size / 4;
915 md->al_size_4k = (u64)rs->al_stripes * rs->al_stripe_size / 4;
916 }
917
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200918 drbd_md_set_sector_offsets(device, device->ldev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700919
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200920 rcu_read_lock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200921 u_size = rcu_dereference(device->ldev->disk_conf)->disk_size;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200922 rcu_read_unlock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200923 size = drbd_new_dev_size(device, device->ldev, u_size, flags & DDSF_FORCED);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700924
Philipp Reisnerd752b262013-06-25 16:50:08 +0200925 if (size < la_size_sect) {
926 if (rs && u_size == 0) {
927 /* Remove "rs &&" later. This check should always be active, but
928 right now the receiver expects the permissive behavior */
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200929 drbd_warn(device, "Implicit shrink not allowed. "
Philipp Reisnerd752b262013-06-25 16:50:08 +0200930 "Use --size=%llus for explicit shrink.\n",
931 (unsigned long long)size);
932 rv = DS_ERROR_SHRINK;
933 }
934 if (u_size > size)
935 rv = DS_ERROR_SPACE_MD;
936 if (rv != DS_UNCHANGED)
937 goto err_out;
938 }
939
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200940 if (drbd_get_capacity(device->this_bdev) != size ||
941 drbd_bm_capacity(device) != size) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700942 int err;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200943 err = drbd_bm_resize(device, size, !(flags & DDSF_NO_RESYNC));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700944 if (unlikely(err)) {
945 /* currently there is only one error: ENOMEM! */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200946 size = drbd_bm_capacity(device)>>1;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700947 if (size == 0) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200948 drbd_err(device, "OUT OF MEMORY! "
Philipp Reisnerb411b362009-09-25 16:07:19 -0700949 "Could not allocate bitmap!\n");
950 } else {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200951 drbd_err(device, "BM resizing failed. "
Philipp Reisnerb411b362009-09-25 16:07:19 -0700952 "Leaving size unchanged at size = %lu KB\n",
953 (unsigned long)size);
954 }
Philipp Reisnere96c9632013-06-25 16:50:07 +0200955 rv = DS_ERROR;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700956 }
957 /* racy, see comments above. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200958 drbd_set_my_capacity(device, size);
959 device->ldev->md.la_size_sect = size;
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200960 drbd_info(device, "size = %s (%llu KB)\n", ppsize(ppb, size>>1),
Philipp Reisnerb411b362009-09-25 16:07:19 -0700961 (unsigned long long)size>>1);
962 }
Philipp Reisnerd752b262013-06-25 16:50:08 +0200963 if (rv <= DS_ERROR)
964 goto err_out;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700965
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200966 la_size_changed = (la_size_sect != device->ldev->md.la_size_sect);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700967
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200968 md_moved = prev_first_sect != drbd_md_first_sector(device->ldev)
969 || prev_size != device->ldev->md.md_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700970
Philipp Reisnerd752b262013-06-25 16:50:08 +0200971 if (la_size_changed || md_moved || rs) {
972 u32 prev_flags;
Andreas Gruenbacher24dccab2010-12-12 17:45:41 +0100973
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200974 drbd_al_shrink(device); /* All extents inactive. */
Philipp Reisnerd752b262013-06-25 16:50:08 +0200975
976 prev_flags = md->flags;
977 md->flags &= ~MDF_PRIMARY_IND;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200978 drbd_md_write(device, buffer);
Philipp Reisnerd752b262013-06-25 16:50:08 +0200979
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200980 drbd_info(device, "Writing the whole bitmap, %s\n",
Philipp Reisnerb411b362009-09-25 16:07:19 -0700981 la_size_changed && md_moved ? "size changed and md moved" :
982 la_size_changed ? "size changed" : "md moved");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100983 /* next line implicitly does drbd_suspend_io()+drbd_resume_io() */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200984 drbd_bitmap_io(device, md_moved ? &drbd_bm_write_all : &drbd_bm_write,
Philipp Reisnerd752b262013-06-25 16:50:08 +0200985 "size changed", BM_LOCKED_MASK);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200986 drbd_initialize_al(device, buffer);
Philipp Reisnerd752b262013-06-25 16:50:08 +0200987
988 md->flags = prev_flags;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200989 drbd_md_write(device, buffer);
Philipp Reisnerd752b262013-06-25 16:50:08 +0200990
991 if (rs)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200992 drbd_info(device, "Changed AL layout to al-stripes = %d, al-stripe-size-kB = %d\n",
993 md->al_stripes, md->al_stripe_size_4k * 4);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700994 }
995
Lars Ellenbergcccac982013-03-19 18:16:46 +0100996 if (size > la_size_sect)
Philipp Reisner57737ad2013-10-23 10:59:17 +0200997 rv = la_size_sect ? DS_GREW : DS_GREW_FROM_ZERO;
Lars Ellenbergcccac982013-03-19 18:16:46 +0100998 if (size < la_size_sect)
Philipp Reisnere96c9632013-06-25 16:50:07 +0200999 rv = DS_SHRUNK;
Philipp Reisnerd752b262013-06-25 16:50:08 +02001000
1001 if (0) {
1002 err_out:
1003 if (rs) {
1004 md->al_stripes = prev_al_stripes;
1005 md->al_stripe_size_4k = prev_al_stripe_size_4k;
1006 md->al_size_4k = (u64)prev_al_stripes * prev_al_stripe_size_4k;
1007
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001008 drbd_md_set_sector_offsets(device, device->ldev);
Philipp Reisnerd752b262013-06-25 16:50:08 +02001009 }
1010 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001011 lc_unlock(device->act_log);
1012 wake_up(&device->al_wait);
1013 drbd_md_put_buffer(device);
1014 drbd_resume_io(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001015
1016 return rv;
1017}
1018
1019sector_t
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001020drbd_new_dev_size(struct drbd_device *device, struct drbd_backing_dev *bdev,
Philipp Reisneref5e44a2011-05-03 13:27:43 +02001021 sector_t u_size, int assume_peer_has_space)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001022{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001023 sector_t p_size = device->p_size; /* partner's disk size. */
Lars Ellenbergcccac982013-03-19 18:16:46 +01001024 sector_t la_size_sect = bdev->md.la_size_sect; /* last agreed size. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001025 sector_t m_size; /* my size */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001026 sector_t size = 0;
1027
1028 m_size = drbd_get_max_capacity(bdev);
1029
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001030 if (device->state.conn < C_CONNECTED && assume_peer_has_space) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001031 drbd_warn(device, "Resize while not connected was forced by the user!\n");
Philipp Reisnera393db62009-12-22 13:35:52 +01001032 p_size = m_size;
1033 }
1034
Philipp Reisnerb411b362009-09-25 16:07:19 -07001035 if (p_size && m_size) {
1036 size = min_t(sector_t, p_size, m_size);
1037 } else {
Lars Ellenbergcccac982013-03-19 18:16:46 +01001038 if (la_size_sect) {
1039 size = la_size_sect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001040 if (m_size && m_size < size)
1041 size = m_size;
1042 if (p_size && p_size < size)
1043 size = p_size;
1044 } else {
1045 if (m_size)
1046 size = m_size;
1047 if (p_size)
1048 size = p_size;
1049 }
1050 }
1051
1052 if (size == 0)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001053 drbd_err(device, "Both nodes diskless!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07001054
1055 if (u_size) {
1056 if (u_size > size)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001057 drbd_err(device, "Requested disk size is too big (%lu > %lu)\n",
Philipp Reisnerb411b362009-09-25 16:07:19 -07001058 (unsigned long)u_size>>1, (unsigned long)size>>1);
1059 else
1060 size = u_size;
1061 }
1062
1063 return size;
1064}
1065
1066/**
1067 * drbd_check_al_size() - Ensures that the AL is of the right size
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001068 * @device: DRBD device.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001069 *
1070 * Returns -EBUSY if current al lru is still used, -ENOMEM when allocation
1071 * failed, and 0 on success. You should call drbd_md_sync() after you called
1072 * this function.
1073 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001074static int drbd_check_al_size(struct drbd_device *device, struct disk_conf *dc)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001075{
1076 struct lru_cache *n, *t;
1077 struct lc_element *e;
1078 unsigned int in_use;
1079 int i;
1080
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001081 if (device->act_log &&
1082 device->act_log->nr_elements == dc->al_extents)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001083 return 0;
1084
1085 in_use = 0;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001086 t = device->act_log;
Lars Ellenberg7ad651b2011-02-21 13:21:03 +01001087 n = lc_create("act_log", drbd_al_ext_cache, AL_UPDATES_PER_TRANSACTION,
Lars Ellenbergf3990022011-03-23 14:31:09 +01001088 dc->al_extents, sizeof(struct lc_element), 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001089
1090 if (n == NULL) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001091 drbd_err(device, "Cannot allocate act_log lru!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07001092 return -ENOMEM;
1093 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001094 spin_lock_irq(&device->al_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001095 if (t) {
1096 for (i = 0; i < t->nr_elements; i++) {
1097 e = lc_element_by_index(t, i);
1098 if (e->refcnt)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001099 drbd_err(device, "refcnt(%d)==%d\n",
Philipp Reisnerb411b362009-09-25 16:07:19 -07001100 e->lc_number, e->refcnt);
1101 in_use += e->refcnt;
1102 }
1103 }
1104 if (!in_use)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001105 device->act_log = n;
1106 spin_unlock_irq(&device->al_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001107 if (in_use) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001108 drbd_err(device, "Activity log still in use!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07001109 lc_destroy(n);
1110 return -EBUSY;
1111 } else {
1112 if (t)
1113 lc_destroy(t);
1114 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001115 drbd_md_mark_dirty(device); /* we changed device->act_log->nr_elemens */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001116 return 0;
1117}
1118
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001119static void drbd_setup_queue_param(struct drbd_device *device, unsigned int max_bio_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001120{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001121 struct request_queue * const q = device->rq_queue;
Lars Ellenbergdb141b22012-06-25 19:15:58 +02001122 unsigned int max_hw_sectors = max_bio_size >> 9;
1123 unsigned int max_segments = 0;
Philipp Reisnerc1b31562014-04-28 18:43:32 +02001124 struct request_queue *b = NULL;
Philipp Reisner99432fc2011-05-20 16:39:13 +02001125
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001126 if (get_ldev_if_state(device, D_ATTACHING)) {
Philipp Reisnerc1b31562014-04-28 18:43:32 +02001127 b = device->ldev->backing_bdev->bd_disk->queue;
Philipp Reisner99432fc2011-05-20 16:39:13 +02001128
1129 max_hw_sectors = min(queue_max_hw_sectors(b), max_bio_size >> 9);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001130 rcu_read_lock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001131 max_segments = rcu_dereference(device->ldev->disk_conf)->max_bio_bvecs;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001132 rcu_read_unlock();
Philipp Reisnerc1b31562014-04-28 18:43:32 +02001133
1134 blk_set_stacking_limits(&q->limits);
1135 blk_queue_max_write_same_sectors(q, 0);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001136 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001137
Philipp Reisnerb411b362009-09-25 16:07:19 -07001138 blk_queue_logical_block_size(q, 512);
Lars Ellenberg1816a2b2010-11-11 15:19:07 +01001139 blk_queue_max_hw_sectors(q, max_hw_sectors);
1140 /* This is the workaround for "bio would need to, but cannot, be split" */
1141 blk_queue_max_segments(q, max_segments ? max_segments : BLK_MAX_SEGMENTS);
1142 blk_queue_segment_boundary(q, PAGE_CACHE_SIZE-1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001143
Philipp Reisnerc1b31562014-04-28 18:43:32 +02001144 if (b) {
Lars Ellenberg20c68fd2014-04-28 18:43:25 +02001145 struct drbd_connection *connection = first_peer_device(device)->connection;
1146
1147 if (blk_queue_discard(b) &&
1148 (connection->cstate < C_CONNECTED || connection->agreed_features & FF_TRIM)) {
Lars Ellenberg20c68fd2014-04-28 18:43:25 +02001149 /* For now, don't allow more than one activity log extent worth of data
1150 * to be discarded in one go. We may need to rework drbd_al_begin_io()
1151 * to allow for even larger discard ranges */
1152 q->limits.max_discard_sectors = DRBD_MAX_DISCARD_SECTORS;
1153
1154 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
1155 /* REALLY? Is stacking secdiscard "legal"? */
1156 if (blk_queue_secdiscard(b))
1157 queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD, q);
1158 } else {
1159 q->limits.max_discard_sectors = 0;
1160 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, q);
1161 queue_flag_clear_unlocked(QUEUE_FLAG_SECDISCARD, q);
1162 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001163
Philipp Reisner99432fc2011-05-20 16:39:13 +02001164 blk_queue_stack_limits(q, b);
1165
1166 if (q->backing_dev_info.ra_pages != b->backing_dev_info.ra_pages) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001167 drbd_info(device, "Adjusting my ra_pages to backing device's (%lu -> %lu)\n",
Philipp Reisner99432fc2011-05-20 16:39:13 +02001168 q->backing_dev_info.ra_pages,
1169 b->backing_dev_info.ra_pages);
1170 q->backing_dev_info.ra_pages = b->backing_dev_info.ra_pages;
1171 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001172 put_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001173 }
1174}
1175
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001176void drbd_reconsider_max_bio_size(struct drbd_device *device)
Philipp Reisner99432fc2011-05-20 16:39:13 +02001177{
Lars Ellenbergdb141b22012-06-25 19:15:58 +02001178 unsigned int now, new, local, peer;
Philipp Reisner99432fc2011-05-20 16:39:13 +02001179
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001180 now = queue_max_hw_sectors(device->rq_queue) << 9;
1181 local = device->local_max_bio_size; /* Eventually last known value, from volatile memory */
1182 peer = device->peer_max_bio_size; /* Eventually last known value, from meta data */
Philipp Reisner99432fc2011-05-20 16:39:13 +02001183
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001184 if (get_ldev_if_state(device, D_ATTACHING)) {
1185 local = queue_max_hw_sectors(device->ldev->backing_bdev->bd_disk->queue) << 9;
1186 device->local_max_bio_size = local;
1187 put_ldev(device);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001188 }
Lars Ellenbergdb141b22012-06-25 19:15:58 +02001189 local = min(local, DRBD_MAX_BIO_SIZE);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001190
1191 /* We may ignore peer limits if the peer is modern enough.
1192 Because new from 8.3.8 onwards the peer can use multiple
1193 BIOs for a single peer_request */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001194 if (device->state.conn >= C_WF_REPORT_PARAMS) {
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001195 if (first_peer_device(device)->connection->agreed_pro_version < 94)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001196 peer = min(device->peer_max_bio_size, DRBD_MAX_SIZE_H80_PACKET);
Philipp Reisner68093842011-06-30 15:43:06 +02001197 /* Correct old drbd (up to 8.3.7) if it believes it can do more than 32KiB */
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001198 else if (first_peer_device(device)->connection->agreed_pro_version == 94)
Philipp Reisner99432fc2011-05-20 16:39:13 +02001199 peer = DRBD_MAX_SIZE_H80_PACKET;
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001200 else if (first_peer_device(device)->connection->agreed_pro_version < 100)
Philipp Reisner2ffca4f2011-06-30 15:43:06 +02001201 peer = DRBD_MAX_BIO_SIZE_P95; /* drbd 8.3.8 onwards, before 8.4.0 */
1202 else
Philipp Reisner99432fc2011-05-20 16:39:13 +02001203 peer = DRBD_MAX_BIO_SIZE;
Philipp Reisner99432fc2011-05-20 16:39:13 +02001204
Lars Ellenbergfa090e72014-04-28 18:43:27 +02001205 /* We may later detach and re-attach on a disconnected Primary.
1206 * Avoid this setting to jump back in that case.
1207 * We want to store what we know the peer DRBD can handle,
1208 * not what the peer IO backend can handle. */
1209 if (peer > device->peer_max_bio_size)
1210 device->peer_max_bio_size = peer;
1211 }
Lars Ellenbergdb141b22012-06-25 19:15:58 +02001212 new = min(local, peer);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001213
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001214 if (device->state.role == R_PRIMARY && new < now)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001215 drbd_err(device, "ASSERT FAILED new < now; (%u < %u)\n", new, now);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001216
1217 if (new != now)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001218 drbd_info(device, "max BIO size = %u\n", new);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001219
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001220 drbd_setup_queue_param(device, new);
Philipp Reisner99432fc2011-05-20 16:39:13 +02001221}
1222
Philipp Reisnera18e9d12011-04-24 11:09:55 +02001223/* Starts the worker thread */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001224static void conn_reconfig_start(struct drbd_connection *connection)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001225{
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001226 drbd_thread_start(&connection->worker);
Andreas Gruenbacherb5043c52011-07-28 15:56:02 +02001227 drbd_flush_workqueue(&connection->sender_work);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001228}
1229
Philipp Reisnera18e9d12011-04-24 11:09:55 +02001230/* if still unconfigured, stops worker again. */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001231static void conn_reconfig_done(struct drbd_connection *connection)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001232{
Lars Ellenberg992d6e92011-05-02 11:47:18 +02001233 bool stop_threads;
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001234 spin_lock_irq(&connection->resource->req_lock);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001235 stop_threads = conn_all_vols_unconf(connection) &&
1236 connection->cstate == C_STANDALONE;
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001237 spin_unlock_irq(&connection->resource->req_lock);
Lars Ellenberg992d6e92011-05-02 11:47:18 +02001238 if (stop_threads) {
1239 /* asender is implicitly stopped by receiver
Philipp Reisner81fa2e62011-05-04 15:10:30 +02001240 * in conn_disconnect() */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001241 drbd_thread_stop(&connection->receiver);
1242 drbd_thread_stop(&connection->worker);
Lars Ellenberg992d6e92011-05-02 11:47:18 +02001243 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001244}
1245
Philipp Reisner07782862010-08-31 12:00:50 +02001246/* Make sure IO is suspended before calling this function(). */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001247static void drbd_suspend_al(struct drbd_device *device)
Philipp Reisner07782862010-08-31 12:00:50 +02001248{
1249 int s = 0;
1250
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001251 if (!lc_try_lock(device->act_log)) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001252 drbd_warn(device, "Failed to lock al in drbd_suspend_al()\n");
Philipp Reisner07782862010-08-31 12:00:50 +02001253 return;
1254 }
1255
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001256 drbd_al_shrink(device);
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001257 spin_lock_irq(&device->resource->req_lock);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001258 if (device->state.conn < C_CONNECTED)
1259 s = !test_and_set_bit(AL_SUSPENDED, &device->flags);
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001260 spin_unlock_irq(&device->resource->req_lock);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001261 lc_unlock(device->act_log);
Philipp Reisner07782862010-08-31 12:00:50 +02001262
1263 if (s)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001264 drbd_info(device, "Suspended AL updates\n");
Philipp Reisner07782862010-08-31 12:00:50 +02001265}
1266
Lars Ellenberg5979e362011-04-27 21:09:55 +02001267
1268static bool should_set_defaults(struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001269{
Lars Ellenberg5979e362011-04-27 21:09:55 +02001270 unsigned flags = ((struct drbd_genlmsghdr*)info->userhdr)->flags;
1271 return 0 != (flags & DRBD_GENL_F_SET_DEFAULTS);
1272}
1273
Lars Ellenberg5bbcf5e2013-03-19 18:16:59 +01001274static unsigned int drbd_al_extents_max(struct drbd_backing_dev *bdev)
Philipp Reisnerd589a212011-05-04 10:06:52 +02001275{
Lars Ellenberg5bbcf5e2013-03-19 18:16:59 +01001276 /* This is limited by 16 bit "slot" numbers,
1277 * and by available on-disk context storage.
1278 *
1279 * Also (u16)~0 is special (denotes a "free" extent).
1280 *
1281 * One transaction occupies one 4kB on-disk block,
1282 * we have n such blocks in the on disk ring buffer,
1283 * the "current" transaction may fail (n-1),
1284 * and there is 919 slot numbers context information per transaction.
1285 *
1286 * 72 transaction blocks amounts to more than 2**16 context slots,
1287 * so cap there first.
1288 */
1289 const unsigned int max_al_nr = DRBD_AL_EXTENTS_MAX;
1290 const unsigned int sufficient_on_disk =
1291 (max_al_nr + AL_CONTEXT_PER_TRANSACTION -1)
1292 /AL_CONTEXT_PER_TRANSACTION;
Philipp Reisnerd589a212011-05-04 10:06:52 +02001293
Lars Ellenberg5bbcf5e2013-03-19 18:16:59 +01001294 unsigned int al_size_4k = bdev->md.al_size_4k;
1295
1296 if (al_size_4k > sufficient_on_disk)
1297 return max_al_nr;
1298
1299 return (al_size_4k - 1) * AL_CONTEXT_PER_TRANSACTION;
Philipp Reisnerd589a212011-05-04 10:06:52 +02001300}
1301
Lars Ellenbergf3990022011-03-23 14:31:09 +01001302int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info)
1303{
Lars Ellenberga910b122014-04-28 18:43:21 +02001304 struct drbd_config_context adm_ctx;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001305 enum drbd_ret_code retcode;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001306 struct drbd_device *device;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001307 struct disk_conf *new_disk_conf, *old_disk_conf;
Philipp Reisner813472c2011-05-03 16:47:02 +02001308 struct fifo_buffer *old_plan = NULL, *new_plan = NULL;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001309 int err, fifo_size;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001310
Lars Ellenberga910b122014-04-28 18:43:21 +02001311 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001312 if (!adm_ctx.reply_skb)
1313 return retcode;
1314 if (retcode != NO_ERROR)
Lars Ellenberg9e276872014-04-28 18:43:22 +02001315 goto finish;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001316
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001317 device = adm_ctx.device;
Lars Ellenberg9e276872014-04-28 18:43:22 +02001318 mutex_lock(&adm_ctx.resource->adm_mutex);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001319
1320 /* we also need a disk
1321 * to change the options on */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001322 if (!get_ldev(device)) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001323 retcode = ERR_NO_DISK;
1324 goto out;
1325 }
1326
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001327 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001328 if (!new_disk_conf) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001329 retcode = ERR_NOMEM;
1330 goto fail;
1331 }
1332
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001333 mutex_lock(&device->resource->conf_update);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001334 old_disk_conf = device->ldev->disk_conf;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001335 *new_disk_conf = *old_disk_conf;
Lars Ellenberg5979e362011-04-27 21:09:55 +02001336 if (should_set_defaults(info))
Andreas Gruenbacherb966b5d2011-05-03 14:56:09 +02001337 set_disk_conf_defaults(new_disk_conf);
Lars Ellenberg5979e362011-04-27 21:09:55 +02001338
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001339 err = disk_conf_from_attrs_for_change(new_disk_conf, info);
Andreas Gruenbacherc75b9b12011-05-24 14:18:31 +02001340 if (err && err != -ENOMSG) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001341 retcode = ERR_MANDATORY_TAG;
Lars Ellenberga910b122014-04-28 18:43:21 +02001342 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
Philipp Reisner8e229432013-08-01 10:21:47 +02001343 goto fail_unlock;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001344 }
1345
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001346 if (!expect(new_disk_conf->resync_rate >= 1))
1347 new_disk_conf->resync_rate = 1;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001348
Lars Ellenberg5bbcf5e2013-03-19 18:16:59 +01001349 if (new_disk_conf->al_extents < DRBD_AL_EXTENTS_MIN)
1350 new_disk_conf->al_extents = DRBD_AL_EXTENTS_MIN;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001351 if (new_disk_conf->al_extents > drbd_al_extents_max(device->ldev))
1352 new_disk_conf->al_extents = drbd_al_extents_max(device->ldev);
Lars Ellenberg5bbcf5e2013-03-19 18:16:59 +01001353
1354 if (new_disk_conf->c_plan_ahead > DRBD_C_PLAN_AHEAD_MAX)
1355 new_disk_conf->c_plan_ahead = DRBD_C_PLAN_AHEAD_MAX;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001356
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001357 fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001358 if (fifo_size != device->rs_plan_s->size) {
Philipp Reisner813472c2011-05-03 16:47:02 +02001359 new_plan = fifo_alloc(fifo_size);
1360 if (!new_plan) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001361 drbd_err(device, "kmalloc of fifo_buffer failed");
Lars Ellenbergf3990022011-03-23 14:31:09 +01001362 retcode = ERR_NOMEM;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001363 goto fail_unlock;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001364 }
1365 }
1366
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001367 drbd_suspend_io(device);
1368 wait_event(device->al_wait, lc_try_lock(device->act_log));
1369 drbd_al_shrink(device);
1370 err = drbd_check_al_size(device, new_disk_conf);
1371 lc_unlock(device->act_log);
1372 wake_up(&device->al_wait);
1373 drbd_resume_io(device);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001374
1375 if (err) {
1376 retcode = ERR_NOMEM;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001377 goto fail_unlock;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001378 }
1379
Philipp Reisnerdc97b702011-05-03 14:27:15 +02001380 write_lock_irq(&global_state_lock);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001381 retcode = drbd_resync_after_valid(device, new_disk_conf->resync_after);
Philipp Reisnerdc97b702011-05-03 14:27:15 +02001382 if (retcode == NO_ERROR) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001383 rcu_assign_pointer(device->ldev->disk_conf, new_disk_conf);
1384 drbd_resync_after_changed(device);
Philipp Reisnerdc97b702011-05-03 14:27:15 +02001385 }
1386 write_unlock_irq(&global_state_lock);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001387
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001388 if (retcode != NO_ERROR)
1389 goto fail_unlock;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001390
Philipp Reisner813472c2011-05-03 16:47:02 +02001391 if (new_plan) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001392 old_plan = device->rs_plan_s;
1393 rcu_assign_pointer(device->rs_plan_s, new_plan);
Philipp Reisner9958c852011-05-03 16:19:31 +02001394 }
Philipp Reisner9958c852011-05-03 16:19:31 +02001395
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001396 mutex_unlock(&device->resource->conf_update);
Philipp Reisner27eb13e2012-03-30 14:12:15 +02001397
Philipp Reisner9a51ab12012-02-20 21:53:28 +01001398 if (new_disk_conf->al_updates)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001399 device->ldev->md.flags &= ~MDF_AL_DISABLED;
Philipp Reisner9a51ab12012-02-20 21:53:28 +01001400 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001401 device->ldev->md.flags |= MDF_AL_DISABLED;
Philipp Reisner9a51ab12012-02-20 21:53:28 +01001402
Lars Ellenberg691631c2012-10-26 00:41:50 +02001403 if (new_disk_conf->md_flushes)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001404 clear_bit(MD_NO_FUA, &device->flags);
Lars Ellenberg691631c2012-10-26 00:41:50 +02001405 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001406 set_bit(MD_NO_FUA, &device->flags);
Lars Ellenberg691631c2012-10-26 00:41:50 +02001407
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001408 drbd_bump_write_ordering(first_peer_device(device)->connection, WO_bdev_flush);
Philipp Reisner27eb13e2012-03-30 14:12:15 +02001409
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001410 drbd_md_sync(device);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001411
Andreas Gruenbacher69a22772011-08-09 00:47:13 +02001412 if (device->state.conn >= C_CONNECTED) {
1413 struct drbd_peer_device *peer_device;
1414
1415 for_each_peer_device(peer_device, device)
1416 drbd_send_sync_param(peer_device);
1417 }
Lars Ellenbergf3990022011-03-23 14:31:09 +01001418
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001419 synchronize_rcu();
1420 kfree(old_disk_conf);
Philipp Reisner813472c2011-05-03 16:47:02 +02001421 kfree(old_plan);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001422 mod_timer(&device->request_timer, jiffies + HZ);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001423 goto success;
1424
1425fail_unlock:
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001426 mutex_unlock(&device->resource->conf_update);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001427 fail:
Lars Ellenberg5ecc72c2011-04-27 21:14:57 +02001428 kfree(new_disk_conf);
Philipp Reisner813472c2011-05-03 16:47:02 +02001429 kfree(new_plan);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001430success:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001431 put_ldev(device);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001432 out:
Lars Ellenberg9e276872014-04-28 18:43:22 +02001433 mutex_unlock(&adm_ctx.resource->adm_mutex);
1434 finish:
Lars Ellenberga910b122014-04-28 18:43:21 +02001435 drbd_adm_finish(&adm_ctx, info, retcode);
Lars Ellenbergf3990022011-03-23 14:31:09 +01001436 return 0;
1437}
1438
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001439int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001440{
Lars Ellenberga910b122014-04-28 18:43:21 +02001441 struct drbd_config_context adm_ctx;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001442 struct drbd_device *device;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001443 int err;
Andreas Gruenbacher116676c2010-12-08 13:33:11 +01001444 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001445 enum determine_dev_size dd;
1446 sector_t max_possible_sectors;
1447 sector_t min_md_device_sectors;
1448 struct drbd_backing_dev *nbc = NULL; /* new_backing_conf */
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001449 struct disk_conf *new_disk_conf = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01001450 struct block_device *bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001451 struct lru_cache *resync_lru = NULL;
Philipp Reisner9958c852011-05-03 16:19:31 +02001452 struct fifo_buffer *new_plan = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001453 union drbd_state ns, os;
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +01001454 enum drbd_state_rv rv;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001455 struct net_conf *nc;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001456
Lars Ellenberga910b122014-04-28 18:43:21 +02001457 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001458 if (!adm_ctx.reply_skb)
1459 return retcode;
1460 if (retcode != NO_ERROR)
Lars Ellenberg40cbf082011-03-16 16:52:10 +01001461 goto finish;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001462
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001463 device = adm_ctx.device;
Lars Ellenberg9e276872014-04-28 18:43:22 +02001464 mutex_lock(&adm_ctx.resource->adm_mutex);
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001465 conn_reconfig_start(first_peer_device(device)->connection);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001466
1467 /* if you want to reconfigure, please tear down first */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001468 if (device->state.disk > D_DISKLESS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001469 retcode = ERR_DISK_CONFIGURED;
1470 goto fail;
1471 }
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001472 /* It may just now have detached because of IO error. Make sure
1473 * drbd_ldev_destroy is done already, we may end up here very fast,
1474 * e.g. if someone calls attach from the on-io-error handler,
1475 * to realize a "hot spare" feature (not that I'd recommend that) */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001476 wait_event(device->misc_wait, !atomic_read(&device->local_cnt));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001477
Lars Ellenberg383606e2012-06-14 14:21:32 +02001478 /* make sure there is no leftover from previous force-detach attempts */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001479 clear_bit(FORCE_DETACH, &device->flags);
1480 clear_bit(WAS_IO_ERROR, &device->flags);
1481 clear_bit(WAS_READ_ERROR, &device->flags);
Lars Ellenberg383606e2012-06-14 14:21:32 +02001482
Lars Ellenberg0029d622012-06-14 18:02:52 +02001483 /* and no leftover from previously aborted resync or verify, either */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001484 device->rs_total = 0;
1485 device->rs_failed = 0;
1486 atomic_set(&device->rs_pending_cnt, 0);
Lars Ellenberg0029d622012-06-14 18:02:52 +02001487
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001488 /* allocation not in the IO path, drbdsetup context */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001489 nbc = kzalloc(sizeof(struct drbd_backing_dev), GFP_KERNEL);
1490 if (!nbc) {
1491 retcode = ERR_NOMEM;
1492 goto fail;
1493 }
Philipp Reisner9f2247b2012-08-16 14:25:58 +02001494 spin_lock_init(&nbc->md.uuid_lock);
1495
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001496 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
1497 if (!new_disk_conf) {
1498 retcode = ERR_NOMEM;
1499 goto fail;
1500 }
1501 nbc->disk_conf = new_disk_conf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001502
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001503 set_disk_conf_defaults(new_disk_conf);
1504 err = disk_conf_from_attrs(new_disk_conf, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001505 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001506 retcode = ERR_MANDATORY_TAG;
Lars Ellenberga910b122014-04-28 18:43:21 +02001507 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001508 goto fail;
1509 }
1510
Lars Ellenberg5bbcf5e2013-03-19 18:16:59 +01001511 if (new_disk_conf->c_plan_ahead > DRBD_C_PLAN_AHEAD_MAX)
1512 new_disk_conf->c_plan_ahead = DRBD_C_PLAN_AHEAD_MAX;
Philipp Reisnerd589a212011-05-04 10:06:52 +02001513
Philipp Reisner9958c852011-05-03 16:19:31 +02001514 new_plan = fifo_alloc((new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ);
1515 if (!new_plan) {
1516 retcode = ERR_NOMEM;
1517 goto fail;
1518 }
1519
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001520 if (new_disk_conf->meta_dev_idx < DRBD_MD_INDEX_FLEX_INT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001521 retcode = ERR_MD_IDX_INVALID;
1522 goto fail;
1523 }
1524
Lars Ellenberga3f8f7d2013-03-27 14:08:43 +01001525 write_lock_irq(&global_state_lock);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001526 retcode = drbd_resync_after_valid(device, new_disk_conf->resync_after);
Lars Ellenberga3f8f7d2013-03-27 14:08:43 +01001527 write_unlock_irq(&global_state_lock);
1528 if (retcode != NO_ERROR)
1529 goto fail;
1530
Philipp Reisner44ed1672011-04-19 17:10:19 +02001531 rcu_read_lock();
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001532 nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
Philipp Reisner44ed1672011-04-19 17:10:19 +02001533 if (nc) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001534 if (new_disk_conf->fencing == FP_STONITH && nc->wire_protocol == DRBD_PROT_A) {
Philipp Reisner44ed1672011-04-19 17:10:19 +02001535 rcu_read_unlock();
Philipp Reisner47ff2d02010-06-18 13:56:57 +02001536 retcode = ERR_STONITH_AND_PROT_A;
1537 goto fail;
1538 }
1539 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02001540 rcu_read_unlock();
Philipp Reisner47ff2d02010-06-18 13:56:57 +02001541
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001542 bdev = blkdev_get_by_path(new_disk_conf->backing_dev,
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001543 FMODE_READ | FMODE_WRITE | FMODE_EXCL, device);
Tejun Heoe525fd82010-11-13 11:55:17 +01001544 if (IS_ERR(bdev)) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001545 drbd_err(device, "open(\"%s\") failed with %ld\n", new_disk_conf->backing_dev,
Tejun Heoe525fd82010-11-13 11:55:17 +01001546 PTR_ERR(bdev));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001547 retcode = ERR_OPEN_DISK;
1548 goto fail;
1549 }
Tejun Heoe525fd82010-11-13 11:55:17 +01001550 nbc->backing_bdev = bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001551
Tejun Heoe525fd82010-11-13 11:55:17 +01001552 /*
1553 * meta_dev_idx >= 0: external fixed size, possibly multiple
1554 * drbd sharing one meta device. TODO in that case, paranoia
1555 * check that [md_bdev, meta_dev_idx] is not yet used by some
1556 * other drbd minor! (if you use drbd.conf + drbdadm, that
1557 * should check it for you already; but if you don't, or
1558 * someone fooled it, we need to double check here)
1559 */
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001560 bdev = blkdev_get_by_path(new_disk_conf->meta_dev,
Tejun Heod4d77622010-11-13 11:55:18 +01001561 FMODE_READ | FMODE_WRITE | FMODE_EXCL,
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001562 (new_disk_conf->meta_dev_idx < 0) ?
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001563 (void *)device : (void *)drbd_m_holder);
Tejun Heoe525fd82010-11-13 11:55:17 +01001564 if (IS_ERR(bdev)) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001565 drbd_err(device, "open(\"%s\") failed with %ld\n", new_disk_conf->meta_dev,
Tejun Heoe525fd82010-11-13 11:55:17 +01001566 PTR_ERR(bdev));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001567 retcode = ERR_OPEN_MD_DISK;
1568 goto fail;
1569 }
Tejun Heoe525fd82010-11-13 11:55:17 +01001570 nbc->md_bdev = bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001571
Tejun Heoe525fd82010-11-13 11:55:17 +01001572 if ((nbc->backing_bdev == nbc->md_bdev) !=
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001573 (new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_INTERNAL ||
1574 new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_FLEX_INT)) {
Tejun Heoe525fd82010-11-13 11:55:17 +01001575 retcode = ERR_MD_IDX_INVALID;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001576 goto fail;
1577 }
1578
1579 resync_lru = lc_create("resync", drbd_bm_ext_cache,
Lars Ellenberg46a15bc2011-02-21 13:21:01 +01001580 1, 61, sizeof(struct bm_extent),
Philipp Reisnerb411b362009-09-25 16:07:19 -07001581 offsetof(struct bm_extent, lce));
1582 if (!resync_lru) {
1583 retcode = ERR_NOMEM;
Tejun Heoe525fd82010-11-13 11:55:17 +01001584 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001585 }
1586
Lars Ellenbergc04ccaa2013-03-19 18:16:47 +01001587 /* Read our meta data super block early.
1588 * This also sets other on-disk offsets. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001589 retcode = drbd_md_read(device, nbc);
Lars Ellenbergc04ccaa2013-03-19 18:16:47 +01001590 if (retcode != NO_ERROR)
1591 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001592
Lars Ellenberg5bbcf5e2013-03-19 18:16:59 +01001593 if (new_disk_conf->al_extents < DRBD_AL_EXTENTS_MIN)
1594 new_disk_conf->al_extents = DRBD_AL_EXTENTS_MIN;
1595 if (new_disk_conf->al_extents > drbd_al_extents_max(nbc))
1596 new_disk_conf->al_extents = drbd_al_extents_max(nbc);
1597
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001598 if (drbd_get_max_capacity(nbc) < new_disk_conf->disk_size) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001599 drbd_err(device, "max capacity %llu smaller than disk size %llu\n",
Philipp Reisnerb411b362009-09-25 16:07:19 -07001600 (unsigned long long) drbd_get_max_capacity(nbc),
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001601 (unsigned long long) new_disk_conf->disk_size);
Lars Ellenberg7948bcd2011-06-06 15:36:04 +02001602 retcode = ERR_DISK_TOO_SMALL;
Tejun Heoe525fd82010-11-13 11:55:17 +01001603 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001604 }
1605
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001606 if (new_disk_conf->meta_dev_idx < 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001607 max_possible_sectors = DRBD_MAX_SECTORS_FLEX;
1608 /* at least one MB, otherwise it does not make sense */
1609 min_md_device_sectors = (2<<10);
1610 } else {
1611 max_possible_sectors = DRBD_MAX_SECTORS;
Lars Ellenbergae8bf312013-03-19 18:16:43 +01001612 min_md_device_sectors = MD_128MB_SECT * (new_disk_conf->meta_dev_idx + 1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001613 }
1614
Philipp Reisnerb411b362009-09-25 16:07:19 -07001615 if (drbd_get_capacity(nbc->md_bdev) < min_md_device_sectors) {
Lars Ellenberg7948bcd2011-06-06 15:36:04 +02001616 retcode = ERR_MD_DISK_TOO_SMALL;
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001617 drbd_warn(device, "refusing attach: md-device too small, "
Philipp Reisnerb411b362009-09-25 16:07:19 -07001618 "at least %llu sectors needed for this meta-disk type\n",
1619 (unsigned long long) min_md_device_sectors);
Tejun Heoe525fd82010-11-13 11:55:17 +01001620 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001621 }
1622
1623 /* Make sure the new disk is big enough
1624 * (we may currently be R_PRIMARY with no local disk...) */
1625 if (drbd_get_max_capacity(nbc) <
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001626 drbd_get_capacity(device->this_bdev)) {
Lars Ellenberg7948bcd2011-06-06 15:36:04 +02001627 retcode = ERR_DISK_TOO_SMALL;
Tejun Heoe525fd82010-11-13 11:55:17 +01001628 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001629 }
1630
1631 nbc->known_size = drbd_get_capacity(nbc->backing_bdev);
1632
Lars Ellenberg13529942009-10-12 19:07:49 +02001633 if (nbc->known_size > max_possible_sectors) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001634 drbd_warn(device, "==> truncating very big lower level device "
Lars Ellenberg13529942009-10-12 19:07:49 +02001635 "to currently maximum possible %llu sectors <==\n",
1636 (unsigned long long) max_possible_sectors);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001637 if (new_disk_conf->meta_dev_idx >= 0)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001638 drbd_warn(device, "==>> using internal or flexible "
Lars Ellenberg13529942009-10-12 19:07:49 +02001639 "meta data may help <<==\n");
1640 }
1641
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001642 drbd_suspend_io(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001643 /* also wait for the last barrier ack. */
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001644 /* FIXME see also https://daiquiri.linbit/cgi-bin/bugzilla/show_bug.cgi?id=171
1645 * We need a way to either ignore barrier acks for barriers sent before a device
1646 * was attached, or a way to wait for all pending barrier acks to come in.
1647 * As barriers are counted per resource,
1648 * we'd need to suspend io on all devices of a resource.
1649 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001650 wait_event(device->misc_wait, !atomic_read(&device->ap_pending_cnt) || drbd_suspended(device));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001651 /* and for any other previously queued work */
Andreas Gruenbacherb5043c52011-07-28 15:56:02 +02001652 drbd_flush_workqueue(&first_peer_device(device)->connection->sender_work);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001653
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001654 rv = _drbd_request_state(device, NS(disk, D_ATTACHING), CS_VERBOSE);
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +01001655 retcode = rv; /* FIXME: Type mismatch. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001656 drbd_resume_io(device);
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +01001657 if (rv < SS_SUCCESS)
Tejun Heoe525fd82010-11-13 11:55:17 +01001658 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001659
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001660 if (!get_ldev_if_state(device, D_ATTACHING))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001661 goto force_diskless;
1662
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001663 if (!device->bitmap) {
1664 if (drbd_bm_init(device)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001665 retcode = ERR_NOMEM;
1666 goto force_diskless_dec;
1667 }
1668 }
1669
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001670 if (device->state.conn < C_CONNECTED &&
Philipp Reisnerbabea492014-04-28 18:43:34 +02001671 device->state.role == R_PRIMARY && device->ed_uuid &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001672 (device->ed_uuid & ~((u64)1)) != (nbc->md.uuid[UI_CURRENT] & ~((u64)1))) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001673 drbd_err(device, "Can only attach to data with current UUID=%016llX\n",
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001674 (unsigned long long)device->ed_uuid);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001675 retcode = ERR_DATA_NOT_CURRENT;
1676 goto force_diskless_dec;
1677 }
1678
1679 /* Since we are diskless, fix the activity log first... */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001680 if (drbd_check_al_size(device, new_disk_conf)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001681 retcode = ERR_NOMEM;
1682 goto force_diskless_dec;
1683 }
1684
1685 /* Prevent shrinking of consistent devices ! */
1686 if (drbd_md_test_flag(nbc, MDF_CONSISTENT) &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001687 drbd_new_dev_size(device, nbc, nbc->disk_conf->disk_size, 0) < nbc->md.la_size_sect) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001688 drbd_warn(device, "refusing to truncate a consistent device\n");
Lars Ellenberg7948bcd2011-06-06 15:36:04 +02001689 retcode = ERR_DISK_TOO_SMALL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001690 goto force_diskless_dec;
1691 }
1692
Philipp Reisnerb411b362009-09-25 16:07:19 -07001693 /* Reset the "barriers don't work" bits here, then force meta data to
1694 * be written, to ensure we determine if barriers are supported. */
Andreas Gruenbachere5440462011-05-04 15:25:35 +02001695 if (new_disk_conf->md_flushes)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001696 clear_bit(MD_NO_FUA, &device->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001697 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001698 set_bit(MD_NO_FUA, &device->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001699
1700 /* Point of no return reached.
1701 * Devices and memory are no longer released by error cleanup below.
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001702 * now device takes over responsibility, and the state engine should
Philipp Reisnerb411b362009-09-25 16:07:19 -07001703 * clean it up somewhere. */
Andreas Gruenbacher0b0ba1e2011-06-27 16:23:33 +02001704 D_ASSERT(device, device->ldev == NULL);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001705 device->ldev = nbc;
1706 device->resync = resync_lru;
1707 device->rs_plan_s = new_plan;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001708 nbc = NULL;
1709 resync_lru = NULL;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001710 new_disk_conf = NULL;
Philipp Reisner9958c852011-05-03 16:19:31 +02001711 new_plan = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001712
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001713 drbd_bump_write_ordering(first_peer_device(device)->connection, WO_bdev_flush);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001714
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001715 if (drbd_md_test_flag(device->ldev, MDF_CRASHED_PRIMARY))
1716 set_bit(CRASHED_PRIMARY, &device->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001717 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001718 clear_bit(CRASHED_PRIMARY, &device->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001719
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001720 if (drbd_md_test_flag(device->ldev, MDF_PRIMARY_IND) &&
Andreas Gruenbacher6bbf53c2011-07-08 01:19:44 +02001721 !(device->state.role == R_PRIMARY && device->resource->susp_nod))
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001722 set_bit(CRASHED_PRIMARY, &device->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001723
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001724 device->send_cnt = 0;
1725 device->recv_cnt = 0;
1726 device->read_cnt = 0;
1727 device->writ_cnt = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001728
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001729 drbd_reconsider_max_bio_size(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001730
1731 /* If I am currently not R_PRIMARY,
1732 * but meta data primary indicator is set,
1733 * I just now recover from a hard crash,
1734 * and have been R_PRIMARY before that crash.
1735 *
1736 * Now, if I had no connection before that crash
1737 * (have been degraded R_PRIMARY), chances are that
1738 * I won't find my peer now either.
1739 *
1740 * In that case, and _only_ in that case,
1741 * we use the degr-wfc-timeout instead of the default,
1742 * so we can automatically recover from a crash of a
1743 * degraded but active "cluster" after a certain timeout.
1744 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001745 clear_bit(USE_DEGR_WFC_T, &device->flags);
1746 if (device->state.role != R_PRIMARY &&
1747 drbd_md_test_flag(device->ldev, MDF_PRIMARY_IND) &&
1748 !drbd_md_test_flag(device->ldev, MDF_CONNECTED_IND))
1749 set_bit(USE_DEGR_WFC_T, &device->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001750
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001751 dd = drbd_determine_dev_size(device, 0, NULL);
Philipp Reisnerd752b262013-06-25 16:50:08 +02001752 if (dd <= DS_ERROR) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001753 retcode = ERR_NOMEM_BITMAP;
1754 goto force_diskless_dec;
Philipp Reisnere96c9632013-06-25 16:50:07 +02001755 } else if (dd == DS_GREW)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001756 set_bit(RESYNC_AFTER_NEG, &device->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001757
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001758 if (drbd_md_test_flag(device->ldev, MDF_FULL_SYNC) ||
1759 (test_bit(CRASHED_PRIMARY, &device->flags) &&
1760 drbd_md_test_flag(device->ldev, MDF_AL_DISABLED))) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001761 drbd_info(device, "Assuming that all blocks are out of sync "
Philipp Reisnerb411b362009-09-25 16:07:19 -07001762 "(aka FullSync)\n");
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001763 if (drbd_bitmap_io(device, &drbd_bmio_set_n_write,
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001764 "set_n_write from attaching", BM_LOCKED_MASK)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001765 retcode = ERR_IO_MD_DISK;
1766 goto force_diskless_dec;
1767 }
1768 } else {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001769 if (drbd_bitmap_io(device, &drbd_bm_read,
Andreas Gruenbacher22ab6a32010-12-13 01:44:11 +01001770 "read from attaching", BM_LOCKED_MASK)) {
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001771 retcode = ERR_IO_MD_DISK;
1772 goto force_diskless_dec;
1773 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001774 }
1775
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001776 if (_drbd_bm_total_weight(device) == drbd_bm_bits(device))
1777 drbd_suspend_al(device); /* IO is still suspended here... */
Philipp Reisner07782862010-08-31 12:00:50 +02001778
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001779 spin_lock_irq(&device->resource->req_lock);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001780 os = drbd_read_state(device);
Philipp Reisner78bae592011-03-28 15:40:12 +02001781 ns = os;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001782 /* If MDF_CONSISTENT is not set go into inconsistent state,
1783 otherwise investigate MDF_WasUpToDate...
1784 If MDF_WAS_UP_TO_DATE is not set go into D_OUTDATED disk state,
1785 otherwise into D_CONSISTENT state.
1786 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001787 if (drbd_md_test_flag(device->ldev, MDF_CONSISTENT)) {
1788 if (drbd_md_test_flag(device->ldev, MDF_WAS_UP_TO_DATE))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001789 ns.disk = D_CONSISTENT;
1790 else
1791 ns.disk = D_OUTDATED;
1792 } else {
1793 ns.disk = D_INCONSISTENT;
1794 }
1795
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001796 if (drbd_md_test_flag(device->ldev, MDF_PEER_OUT_DATED))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001797 ns.pdsk = D_OUTDATED;
1798
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001799 rcu_read_lock();
1800 if (ns.disk == D_CONSISTENT &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001801 (ns.pdsk == D_OUTDATED || rcu_dereference(device->ldev->disk_conf)->fencing == FP_DONT_CARE))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001802 ns.disk = D_UP_TO_DATE;
1803
1804 /* All tests on MDF_PRIMARY_IND, MDF_CONNECTED_IND,
1805 MDF_CONSISTENT and MDF_WAS_UP_TO_DATE must happen before
1806 this point, because drbd_request_state() modifies these
1807 flags. */
1808
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001809 if (rcu_dereference(device->ldev->disk_conf)->al_updates)
1810 device->ldev->md.flags &= ~MDF_AL_DISABLED;
Philipp Reisner9a51ab12012-02-20 21:53:28 +01001811 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001812 device->ldev->md.flags |= MDF_AL_DISABLED;
Philipp Reisner9a51ab12012-02-20 21:53:28 +01001813
1814 rcu_read_unlock();
1815
Philipp Reisnerb411b362009-09-25 16:07:19 -07001816 /* In case we are C_CONNECTED postpone any decision on the new disk
1817 state after the negotiation phase. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001818 if (device->state.conn == C_CONNECTED) {
1819 device->new_state_tmp.i = ns.i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001820 ns.i = os.i;
1821 ns.disk = D_NEGOTIATING;
Philipp Reisnerdc66c742010-06-02 14:31:29 +02001822
1823 /* We expect to receive up-to-date UUIDs soon.
1824 To avoid a race in receive_state, free p_uuid while
1825 holding req_lock. I.e. atomic with the state change */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001826 kfree(device->p_uuid);
1827 device->p_uuid = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001828 }
1829
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001830 rv = _drbd_set_state(device, ns, CS_VERBOSE, NULL);
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001831 spin_unlock_irq(&device->resource->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001832
1833 if (rv < SS_SUCCESS)
1834 goto force_diskless_dec;
1835
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001836 mod_timer(&device->request_timer, jiffies + HZ);
Philipp Reisnercdfda632011-07-05 15:38:59 +02001837
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001838 if (device->state.role == R_PRIMARY)
1839 device->ldev->md.uuid[UI_CURRENT] |= (u64)1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001840 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001841 device->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001842
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001843 drbd_md_mark_dirty(device);
1844 drbd_md_sync(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001845
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001846 kobject_uevent(&disk_to_dev(device->vdisk)->kobj, KOBJ_CHANGE);
1847 put_ldev(device);
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001848 conn_reconfig_done(first_peer_device(device)->connection);
Lars Ellenberg9e276872014-04-28 18:43:22 +02001849 mutex_unlock(&adm_ctx.resource->adm_mutex);
Lars Ellenberga910b122014-04-28 18:43:21 +02001850 drbd_adm_finish(&adm_ctx, info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001851 return 0;
1852
1853 force_diskless_dec:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001854 put_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001855 force_diskless:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001856 drbd_force_state(device, NS(disk, D_DISKLESS));
1857 drbd_md_sync(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001858 fail:
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001859 conn_reconfig_done(first_peer_device(device)->connection);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001860 if (nbc) {
Tejun Heoe525fd82010-11-13 11:55:17 +01001861 if (nbc->backing_bdev)
1862 blkdev_put(nbc->backing_bdev,
1863 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1864 if (nbc->md_bdev)
1865 blkdev_put(nbc->md_bdev,
1866 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001867 kfree(nbc);
1868 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001869 kfree(new_disk_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001870 lc_destroy(resync_lru);
Philipp Reisner9958c852011-05-03 16:19:31 +02001871 kfree(new_plan);
Lars Ellenberg9e276872014-04-28 18:43:22 +02001872 mutex_unlock(&adm_ctx.resource->adm_mutex);
Lars Ellenberg40cbf082011-03-16 16:52:10 +01001873 finish:
Lars Ellenberga910b122014-04-28 18:43:21 +02001874 drbd_adm_finish(&adm_ctx, info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001875 return 0;
1876}
1877
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001878static int adm_detach(struct drbd_device *device, int force)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001879{
Philipp Reisner19f83c72011-03-29 14:21:03 +02001880 enum drbd_state_rv retcode;
Lars Ellenberg9a0d9d02011-05-02 11:51:31 +02001881 int ret;
Philipp Reisner02ee8f92011-03-14 11:54:47 +01001882
Philipp Reisnercdfda632011-07-05 15:38:59 +02001883 if (force) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001884 set_bit(FORCE_DETACH, &device->flags);
1885 drbd_force_state(device, NS(disk, D_FAILED));
Philipp Reisnercdfda632011-07-05 15:38:59 +02001886 retcode = SS_SUCCESS;
Philipp Reisner02ee8f92011-03-14 11:54:47 +01001887 goto out;
1888 }
1889
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001890 drbd_suspend_io(device); /* so no-one is stuck in drbd_al_begin_io */
1891 drbd_md_get_buffer(device); /* make sure there is no in-flight meta-data IO */
1892 retcode = drbd_request_state(device, NS(disk, D_FAILED));
1893 drbd_md_put_buffer(device);
Lars Ellenberg9a0d9d02011-05-02 11:51:31 +02001894 /* D_FAILED will transition to DISKLESS. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001895 ret = wait_event_interruptible(device->misc_wait,
1896 device->state.disk != D_FAILED);
1897 drbd_resume_io(device);
Philipp Reisner9b2f61a2011-05-24 10:27:38 +02001898 if ((int)retcode == (int)SS_IS_DISKLESS)
Lars Ellenberg9a0d9d02011-05-02 11:51:31 +02001899 retcode = SS_NOTHING_TO_DO;
1900 if (ret)
1901 retcode = ERR_INTR;
Philipp Reisner02ee8f92011-03-14 11:54:47 +01001902out:
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01001903 return retcode;
1904}
1905
Philipp Reisnerb411b362009-09-25 16:07:19 -07001906/* Detaching the disk is a process in multiple stages. First we need to lock
1907 * out application IO, in-flight IO, IO stuck in drbd_al_begin_io.
1908 * Then we transition to D_DISKLESS, and wait for put_ldev() to return all
1909 * internal references as well.
1910 * Only then we have finally detached. */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001911int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001912{
Lars Ellenberga910b122014-04-28 18:43:21 +02001913 struct drbd_config_context adm_ctx;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001914 enum drbd_ret_code retcode;
Philipp Reisnercdfda632011-07-05 15:38:59 +02001915 struct detach_parms parms = { };
1916 int err;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001917
Lars Ellenberga910b122014-04-28 18:43:21 +02001918 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001919 if (!adm_ctx.reply_skb)
1920 return retcode;
1921 if (retcode != NO_ERROR)
1922 goto out;
1923
Philipp Reisnercdfda632011-07-05 15:38:59 +02001924 if (info->attrs[DRBD_NLA_DETACH_PARMS]) {
1925 err = detach_parms_from_attrs(&parms, info);
1926 if (err) {
1927 retcode = ERR_MANDATORY_TAG;
Lars Ellenberga910b122014-04-28 18:43:21 +02001928 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
Philipp Reisnercdfda632011-07-05 15:38:59 +02001929 goto out;
1930 }
1931 }
1932
Lars Ellenberg9e276872014-04-28 18:43:22 +02001933 mutex_lock(&adm_ctx.resource->adm_mutex);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001934 retcode = adm_detach(adm_ctx.device, parms.force_detach);
Lars Ellenberg9e276872014-04-28 18:43:22 +02001935 mutex_unlock(&adm_ctx.resource->adm_mutex);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001936out:
Lars Ellenberga910b122014-04-28 18:43:21 +02001937 drbd_adm_finish(&adm_ctx, info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001938 return 0;
1939}
1940
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001941static bool conn_resync_running(struct drbd_connection *connection)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001942{
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001943 struct drbd_peer_device *peer_device;
Philipp Reisner695d08f2011-04-11 22:53:32 -07001944 bool rv = false;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001945 int vnr;
1946
Philipp Reisner695d08f2011-04-11 22:53:32 -07001947 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001948 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
1949 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001950 if (device->state.conn == C_SYNC_SOURCE ||
1951 device->state.conn == C_SYNC_TARGET ||
1952 device->state.conn == C_PAUSED_SYNC_S ||
1953 device->state.conn == C_PAUSED_SYNC_T) {
Philipp Reisner695d08f2011-04-11 22:53:32 -07001954 rv = true;
1955 break;
1956 }
Lars Ellenbergf3990022011-03-23 14:31:09 +01001957 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001958 rcu_read_unlock();
1959
1960 return rv;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001961}
1962
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001963static bool conn_ov_running(struct drbd_connection *connection)
Lars Ellenbergf3990022011-03-23 14:31:09 +01001964{
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001965 struct drbd_peer_device *peer_device;
Philipp Reisner695d08f2011-04-11 22:53:32 -07001966 bool rv = false;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001967 int vnr;
1968
Philipp Reisner695d08f2011-04-11 22:53:32 -07001969 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001970 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
1971 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001972 if (device->state.conn == C_VERIFY_S ||
1973 device->state.conn == C_VERIFY_T) {
Philipp Reisner695d08f2011-04-11 22:53:32 -07001974 rv = true;
1975 break;
1976 }
Lars Ellenbergf3990022011-03-23 14:31:09 +01001977 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001978 rcu_read_unlock();
1979
1980 return rv;
Lars Ellenbergf3990022011-03-23 14:31:09 +01001981}
1982
Philipp Reisnercd643972011-04-13 18:00:59 -07001983static enum drbd_ret_code
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02001984_check_net_options(struct drbd_connection *connection, struct net_conf *old_net_conf, struct net_conf *new_net_conf)
Philipp Reisnercd643972011-04-13 18:00:59 -07001985{
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02001986 struct drbd_peer_device *peer_device;
Philipp Reisnercd643972011-04-13 18:00:59 -07001987 int i;
1988
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02001989 if (old_net_conf && connection->cstate == C_WF_REPORT_PARAMS && connection->agreed_pro_version < 100) {
1990 if (new_net_conf->wire_protocol != old_net_conf->wire_protocol)
Philipp Reisnerdcb20d12011-05-16 14:30:24 +02001991 return ERR_NEED_APV_100;
1992
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02001993 if (new_net_conf->two_primaries != old_net_conf->two_primaries)
Philipp Reisnerdcb20d12011-05-16 14:30:24 +02001994 return ERR_NEED_APV_100;
1995
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02001996 if (strcmp(new_net_conf->integrity_alg, old_net_conf->integrity_alg))
Philipp Reisnerdcb20d12011-05-16 14:30:24 +02001997 return ERR_NEED_APV_100;
1998 }
1999
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002000 if (!new_net_conf->two_primaries &&
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002001 conn_highest_role(connection) == R_PRIMARY &&
2002 conn_highest_peer(connection) == R_PRIMARY)
Philipp Reisnerdcb20d12011-05-16 14:30:24 +02002003 return ERR_NEED_ALLOW_TWO_PRI;
Philipp Reisnerb032b6f2011-04-13 18:16:10 -07002004
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002005 if (new_net_conf->two_primaries &&
2006 (new_net_conf->wire_protocol != DRBD_PROT_C))
Philipp Reisnercd643972011-04-13 18:00:59 -07002007 return ERR_NOT_PROTO_C;
2008
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02002009 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
2010 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002011 if (get_ldev(device)) {
2012 enum drbd_fencing_p fp = rcu_dereference(device->ldev->disk_conf)->fencing;
2013 put_ldev(device);
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002014 if (new_net_conf->wire_protocol == DRBD_PROT_A && fp == FP_STONITH)
Philipp Reisnercd643972011-04-13 18:00:59 -07002015 return ERR_STONITH_AND_PROT_A;
Philipp Reisnercd643972011-04-13 18:00:59 -07002016 }
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002017 if (device->state.role == R_PRIMARY && new_net_conf->discard_my_data)
Lars Ellenbergeb120102012-08-01 12:46:20 +02002018 return ERR_DISCARD_IMPOSSIBLE;
Philipp Reisnercd643972011-04-13 18:00:59 -07002019 }
Philipp Reisnercd643972011-04-13 18:00:59 -07002020
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002021 if (new_net_conf->on_congestion != OC_BLOCK && new_net_conf->wire_protocol != DRBD_PROT_A)
Philipp Reisnercd643972011-04-13 18:00:59 -07002022 return ERR_CONG_NOT_PROTO_A;
2023
2024 return NO_ERROR;
2025}
2026
Philipp Reisner44ed1672011-04-19 17:10:19 +02002027static enum drbd_ret_code
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002028check_net_options(struct drbd_connection *connection, struct net_conf *new_net_conf)
Philipp Reisner44ed1672011-04-19 17:10:19 +02002029{
2030 static enum drbd_ret_code rv;
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02002031 struct drbd_peer_device *peer_device;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002032 int i;
2033
2034 rcu_read_lock();
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002035 rv = _check_net_options(connection, rcu_dereference(connection->net_conf), new_net_conf);
Philipp Reisner44ed1672011-04-19 17:10:19 +02002036 rcu_read_unlock();
2037
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002038 /* connection->volumes protected by genl_lock() here */
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02002039 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
2040 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002041 if (!device->bitmap) {
2042 if (drbd_bm_init(device))
Philipp Reisner44ed1672011-04-19 17:10:19 +02002043 return ERR_NOMEM;
2044 }
2045 }
2046
2047 return rv;
2048}
2049
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002050struct crypto {
2051 struct crypto_hash *verify_tfm;
2052 struct crypto_hash *csums_tfm;
2053 struct crypto_hash *cram_hmac_tfm;
Andreas Gruenbacher8d412fc2011-04-27 20:59:18 +02002054 struct crypto_hash *integrity_tfm;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002055};
2056
2057static int
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02002058alloc_hash(struct crypto_hash **tfm, char *tfm_name, int err_alg)
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002059{
2060 if (!tfm_name[0])
2061 return NO_ERROR;
2062
2063 *tfm = crypto_alloc_hash(tfm_name, 0, CRYPTO_ALG_ASYNC);
2064 if (IS_ERR(*tfm)) {
2065 *tfm = NULL;
2066 return err_alg;
2067 }
2068
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002069 return NO_ERROR;
2070}
2071
2072static enum drbd_ret_code
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002073alloc_crypto(struct crypto *crypto, struct net_conf *new_net_conf)
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002074{
Philipp Reisnerb411b362009-09-25 16:07:19 -07002075 char hmac_name[CRYPTO_MAX_ALG_NAME];
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002076 enum drbd_ret_code rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002077
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002078 rv = alloc_hash(&crypto->csums_tfm, new_net_conf->csums_alg,
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02002079 ERR_CSUMS_ALG);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002080 if (rv != NO_ERROR)
2081 return rv;
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002082 rv = alloc_hash(&crypto->verify_tfm, new_net_conf->verify_alg,
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02002083 ERR_VERIFY_ALG);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002084 if (rv != NO_ERROR)
2085 return rv;
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002086 rv = alloc_hash(&crypto->integrity_tfm, new_net_conf->integrity_alg,
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02002087 ERR_INTEGRITY_ALG);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002088 if (rv != NO_ERROR)
2089 return rv;
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002090 if (new_net_conf->cram_hmac_alg[0] != 0) {
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002091 snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)",
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002092 new_net_conf->cram_hmac_alg);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002093
Andreas Gruenbacher4b6ad6d2011-04-29 10:20:08 +02002094 rv = alloc_hash(&crypto->cram_hmac_tfm, hmac_name,
2095 ERR_AUTH_ALG);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002096 }
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002097
2098 return rv;
2099}
2100
2101static void free_crypto(struct crypto *crypto)
2102{
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002103 crypto_free_hash(crypto->cram_hmac_tfm);
Andreas Gruenbacher8d412fc2011-04-27 20:59:18 +02002104 crypto_free_hash(crypto->integrity_tfm);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002105 crypto_free_hash(crypto->csums_tfm);
2106 crypto_free_hash(crypto->verify_tfm);
2107}
2108
Lars Ellenbergf3990022011-03-23 14:31:09 +01002109int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info)
2110{
Lars Ellenberga910b122014-04-28 18:43:21 +02002111 struct drbd_config_context adm_ctx;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002112 enum drbd_ret_code retcode;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002113 struct drbd_connection *connection;
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002114 struct net_conf *old_net_conf, *new_net_conf = NULL;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002115 int err;
2116 int ovr; /* online verify running */
2117 int rsr; /* re-sync running */
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002118 struct crypto crypto = { };
Lars Ellenbergf3990022011-03-23 14:31:09 +01002119
Lars Ellenberga910b122014-04-28 18:43:21 +02002120 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_CONNECTION);
Lars Ellenbergf3990022011-03-23 14:31:09 +01002121 if (!adm_ctx.reply_skb)
2122 return retcode;
2123 if (retcode != NO_ERROR)
Lars Ellenberg9e276872014-04-28 18:43:22 +02002124 goto finish;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002125
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002126 connection = adm_ctx.connection;
Lars Ellenberg9e276872014-04-28 18:43:22 +02002127 mutex_lock(&adm_ctx.resource->adm_mutex);
Lars Ellenbergf3990022011-03-23 14:31:09 +01002128
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002129 new_net_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
2130 if (!new_net_conf) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002131 retcode = ERR_NOMEM;
2132 goto out;
2133 }
2134
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002135 conn_reconfig_start(connection);
Lars Ellenbergf3990022011-03-23 14:31:09 +01002136
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002137 mutex_lock(&connection->data.mutex);
Andreas Gruenbacher05008132011-07-07 14:19:42 +02002138 mutex_lock(&connection->resource->conf_update);
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002139 old_net_conf = connection->net_conf;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002140
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002141 if (!old_net_conf) {
Lars Ellenberga910b122014-04-28 18:43:21 +02002142 drbd_msg_put_info(adm_ctx.reply_skb, "net conf missing, try connect");
Philipp Reisner44ed1672011-04-19 17:10:19 +02002143 retcode = ERR_INVALID_REQUEST;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002144 goto fail;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002145 }
2146
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002147 *new_net_conf = *old_net_conf;
Lars Ellenberg5979e362011-04-27 21:09:55 +02002148 if (should_set_defaults(info))
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002149 set_net_conf_defaults(new_net_conf);
Philipp Reisner44ed1672011-04-19 17:10:19 +02002150
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002151 err = net_conf_from_attrs_for_change(new_net_conf, info);
Andreas Gruenbacherc75b9b12011-05-24 14:18:31 +02002152 if (err && err != -ENOMSG) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002153 retcode = ERR_MANDATORY_TAG;
Lars Ellenberga910b122014-04-28 18:43:21 +02002154 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
Lars Ellenbergf3990022011-03-23 14:31:09 +01002155 goto fail;
2156 }
2157
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002158 retcode = check_net_options(connection, new_net_conf);
Philipp Reisnercd643972011-04-13 18:00:59 -07002159 if (retcode != NO_ERROR)
2160 goto fail;
2161
Lars Ellenbergf3990022011-03-23 14:31:09 +01002162 /* re-sync running */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002163 rsr = conn_resync_running(connection);
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002164 if (rsr && strcmp(new_net_conf->csums_alg, old_net_conf->csums_alg)) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002165 retcode = ERR_CSUMS_RESYNC_RUNNING;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002166 goto fail;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002167 }
2168
Lars Ellenbergf3990022011-03-23 14:31:09 +01002169 /* online verify running */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002170 ovr = conn_ov_running(connection);
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002171 if (ovr && strcmp(new_net_conf->verify_alg, old_net_conf->verify_alg)) {
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002172 retcode = ERR_VERIFY_RUNNING;
2173 goto fail;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002174 }
2175
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002176 retcode = alloc_crypto(&crypto, new_net_conf);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002177 if (retcode != NO_ERROR)
2178 goto fail;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002179
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002180 rcu_assign_pointer(connection->net_conf, new_net_conf);
Lars Ellenbergf3990022011-03-23 14:31:09 +01002181
2182 if (!rsr) {
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002183 crypto_free_hash(connection->csums_tfm);
2184 connection->csums_tfm = crypto.csums_tfm;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002185 crypto.csums_tfm = NULL;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002186 }
2187 if (!ovr) {
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002188 crypto_free_hash(connection->verify_tfm);
2189 connection->verify_tfm = crypto.verify_tfm;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002190 crypto.verify_tfm = NULL;
Lars Ellenbergf3990022011-03-23 14:31:09 +01002191 }
2192
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002193 crypto_free_hash(connection->integrity_tfm);
2194 connection->integrity_tfm = crypto.integrity_tfm;
2195 if (connection->cstate >= C_WF_REPORT_PARAMS && connection->agreed_pro_version >= 100)
2196 /* Do this without trying to take connection->data.mutex again. */
2197 __drbd_send_protocol(connection, P_PROTOCOL_UPDATE);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002198
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002199 crypto_free_hash(connection->cram_hmac_tfm);
2200 connection->cram_hmac_tfm = crypto.cram_hmac_tfm;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002201
Andreas Gruenbacher05008132011-07-07 14:19:42 +02002202 mutex_unlock(&connection->resource->conf_update);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002203 mutex_unlock(&connection->data.mutex);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002204 synchronize_rcu();
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002205 kfree(old_net_conf);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002206
Andreas Gruenbacher69a22772011-08-09 00:47:13 +02002207 if (connection->cstate >= C_WF_REPORT_PARAMS) {
2208 struct drbd_peer_device *peer_device;
2209 int vnr;
2210
2211 idr_for_each_entry(&connection->peer_devices, peer_device, vnr)
2212 drbd_send_sync_param(peer_device);
2213 }
Lars Ellenbergf3990022011-03-23 14:31:09 +01002214
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002215 goto done;
2216
Lars Ellenbergf3990022011-03-23 14:31:09 +01002217 fail:
Andreas Gruenbacher05008132011-07-07 14:19:42 +02002218 mutex_unlock(&connection->resource->conf_update);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002219 mutex_unlock(&connection->data.mutex);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002220 free_crypto(&crypto);
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002221 kfree(new_net_conf);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002222 done:
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002223 conn_reconfig_done(connection);
Lars Ellenbergf3990022011-03-23 14:31:09 +01002224 out:
Lars Ellenberg9e276872014-04-28 18:43:22 +02002225 mutex_unlock(&adm_ctx.resource->adm_mutex);
2226 finish:
Lars Ellenberga910b122014-04-28 18:43:21 +02002227 drbd_adm_finish(&adm_ctx, info, retcode);
Lars Ellenbergf3990022011-03-23 14:31:09 +01002228 return 0;
2229}
2230
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002231int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002232{
Lars Ellenberga910b122014-04-28 18:43:21 +02002233 struct drbd_config_context adm_ctx;
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02002234 struct drbd_peer_device *peer_device;
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002235 struct net_conf *old_net_conf, *new_net_conf = NULL;
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002236 struct crypto crypto = { };
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002237 struct drbd_resource *resource;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002238 struct drbd_connection *connection;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002239 enum drbd_ret_code retcode;
2240 int i;
2241 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002242
Lars Ellenberga910b122014-04-28 18:43:21 +02002243 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002244
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002245 if (!adm_ctx.reply_skb)
2246 return retcode;
2247 if (retcode != NO_ERROR)
2248 goto out;
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002249 if (!(adm_ctx.my_addr && adm_ctx.peer_addr)) {
Lars Ellenberga910b122014-04-28 18:43:21 +02002250 drbd_msg_put_info(adm_ctx.reply_skb, "connection endpoint(s) missing");
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002251 retcode = ERR_INVALID_REQUEST;
2252 goto out;
2253 }
2254
2255 /* No need for _rcu here. All reconfiguration is
2256 * strictly serialized on genl_lock(). We are protected against
2257 * concurrent reconfiguration/addition/deletion */
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002258 for_each_resource(resource, &drbd_resources) {
2259 for_each_connection(connection, resource) {
2260 if (nla_len(adm_ctx.my_addr) == connection->my_addr_len &&
2261 !memcmp(nla_data(adm_ctx.my_addr), &connection->my_addr,
2262 connection->my_addr_len)) {
2263 retcode = ERR_LOCAL_ADDR;
2264 goto out;
2265 }
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002266
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02002267 if (nla_len(adm_ctx.peer_addr) == connection->peer_addr_len &&
2268 !memcmp(nla_data(adm_ctx.peer_addr), &connection->peer_addr,
2269 connection->peer_addr_len)) {
2270 retcode = ERR_PEER_ADDR;
2271 goto out;
2272 }
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002273 }
2274 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002275
Lars Ellenberg9e276872014-04-28 18:43:22 +02002276 mutex_lock(&adm_ctx.resource->adm_mutex);
Andreas Gruenbacher3ab706f2011-07-06 15:05:58 +02002277 connection = first_connection(adm_ctx.resource);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002278 conn_reconfig_start(connection);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002279
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002280 if (connection->cstate > C_STANDALONE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002281 retcode = ERR_NET_CONFIGURED;
2282 goto fail;
2283 }
2284
Andreas Gruenbachera209b4a2011-08-17 12:43:25 +02002285 /* allocation not in the IO path, drbdsetup / netlink process context */
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002286 new_net_conf = kzalloc(sizeof(*new_net_conf), GFP_KERNEL);
2287 if (!new_net_conf) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002288 retcode = ERR_NOMEM;
2289 goto fail;
2290 }
2291
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002292 set_net_conf_defaults(new_net_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002293
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002294 err = net_conf_from_attrs(new_net_conf, info);
Lars Ellenberg25e40932011-08-19 10:39:00 +02002295 if (err && err != -ENOMSG) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002296 retcode = ERR_MANDATORY_TAG;
Lars Ellenberga910b122014-04-28 18:43:21 +02002297 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002298 goto fail;
2299 }
2300
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002301 retcode = check_net_options(connection, new_net_conf);
Philipp Reisnercd643972011-04-13 18:00:59 -07002302 if (retcode != NO_ERROR)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002303 goto fail;
Philipp Reisner47ff2d02010-06-18 13:56:57 +02002304
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002305 retcode = alloc_crypto(&crypto, new_net_conf);
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002306 if (retcode != NO_ERROR)
Philipp Reisner422028b2010-10-27 11:12:07 +02002307 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002308
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002309 ((char *)new_net_conf->shared_secret)[SHARED_SECRET_MAX-1] = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002310
Andreas Gruenbacherb5043c52011-07-28 15:56:02 +02002311 drbd_flush_workqueue(&connection->sender_work);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002312
Andreas Gruenbacher05008132011-07-07 14:19:42 +02002313 mutex_lock(&adm_ctx.resource->conf_update);
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002314 old_net_conf = connection->net_conf;
2315 if (old_net_conf) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002316 retcode = ERR_NET_CONFIGURED;
Andreas Gruenbacher05008132011-07-07 14:19:42 +02002317 mutex_unlock(&adm_ctx.resource->conf_update);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002318 goto fail;
2319 }
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002320 rcu_assign_pointer(connection->net_conf, new_net_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002321
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002322 conn_free_crypto(connection);
2323 connection->cram_hmac_tfm = crypto.cram_hmac_tfm;
2324 connection->integrity_tfm = crypto.integrity_tfm;
2325 connection->csums_tfm = crypto.csums_tfm;
2326 connection->verify_tfm = crypto.verify_tfm;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002327
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002328 connection->my_addr_len = nla_len(adm_ctx.my_addr);
2329 memcpy(&connection->my_addr, nla_data(adm_ctx.my_addr), connection->my_addr_len);
2330 connection->peer_addr_len = nla_len(adm_ctx.peer_addr);
2331 memcpy(&connection->peer_addr, nla_data(adm_ctx.peer_addr), connection->peer_addr_len);
Andreas Gruenbacher089c0752011-06-14 18:28:09 +02002332
Andreas Gruenbacher05008132011-07-07 14:19:42 +02002333 mutex_unlock(&adm_ctx.resource->conf_update);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02002334
Philipp Reisner695d08f2011-04-11 22:53:32 -07002335 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02002336 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
2337 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002338 device->send_cnt = 0;
2339 device->recv_cnt = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002340 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07002341 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07002342
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002343 retcode = conn_request_state(connection, NS(conn, C_UNCONNECTED), CS_VERBOSE);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002344
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002345 conn_reconfig_done(connection);
Lars Ellenberg9e276872014-04-28 18:43:22 +02002346 mutex_unlock(&adm_ctx.resource->adm_mutex);
Lars Ellenberga910b122014-04-28 18:43:21 +02002347 drbd_adm_finish(&adm_ctx, info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002348 return 0;
2349
2350fail:
Philipp Reisner0fd0ea02011-04-27 11:27:47 +02002351 free_crypto(&crypto);
Andreas Gruenbacher270eb5c2011-07-06 14:42:39 +02002352 kfree(new_net_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002353
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002354 conn_reconfig_done(connection);
Lars Ellenberg9e276872014-04-28 18:43:22 +02002355 mutex_unlock(&adm_ctx.resource->adm_mutex);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002356out:
Lars Ellenberga910b122014-04-28 18:43:21 +02002357 drbd_adm_finish(&adm_ctx, info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002358 return 0;
2359}
2360
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002361static enum drbd_state_rv conn_try_disconnect(struct drbd_connection *connection, bool force)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002362{
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002363 enum drbd_state_rv rv;
Philipp Reisner2561b9c2010-12-03 15:22:48 +01002364
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002365 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING),
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002366 force ? CS_HARD : 0);
Philipp Reisner2561b9c2010-12-03 15:22:48 +01002367
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002368 switch (rv) {
2369 case SS_NOTHING_TO_DO:
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002370 break;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002371 case SS_ALREADY_STANDALONE:
2372 return SS_SUCCESS;
2373 case SS_PRIMARY_NOP:
2374 /* Our state checking code wants to see the peer outdated. */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002375 rv = conn_request_state(connection, NS2(conn, C_DISCONNECTING, pdsk, D_OUTDATED), 0);
Philipp Reisner2bd5ed52013-03-27 14:08:40 +01002376
2377 if (rv == SS_OUTDATE_WO_CONN) /* lost connection before graceful disconnect succeeded */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002378 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING), CS_VERBOSE);
Philipp Reisner2bd5ed52013-03-27 14:08:40 +01002379
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002380 break;
2381 case SS_CW_FAILED_BY_PEER:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002382 /* The peer probably wants to see us outdated. */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002383 rv = conn_request_state(connection, NS2(conn, C_DISCONNECTING,
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002384 disk, D_OUTDATED), 0);
2385 if (rv == SS_IS_DISKLESS || rv == SS_LOWER_THAN_OUTDATED) {
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002386 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING),
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002387 CS_HARD);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002388 }
2389 break;
2390 default:;
2391 /* no special handling necessary */
2392 }
2393
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002394 if (rv >= SS_SUCCESS) {
2395 enum drbd_state_rv rv2;
2396 /* No one else can reconfigure the network while I am here.
2397 * The state handling only uses drbd_thread_stop_nowait(),
2398 * we want to really wait here until the receiver is no more.
2399 */
Andreas Gruenbacher9693da22011-06-21 13:58:18 +02002400 drbd_thread_stop(&connection->receiver);
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002401
2402 /* Race breaker. This additional state change request may be
2403 * necessary, if this was a forced disconnect during a receiver
2404 * restart. We may have "killed" the receiver thread just
Andreas Gruenbacher8fe60552011-07-22 11:04:36 +02002405 * after drbd_receiver() returned. Typically, we should be
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002406 * C_STANDALONE already, now, and this becomes a no-op.
2407 */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002408 rv2 = conn_request_state(connection, NS(conn, C_STANDALONE),
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002409 CS_VERBOSE | CS_HARD);
2410 if (rv2 < SS_SUCCESS)
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +02002411 drbd_err(connection,
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002412 "unexpected rv2=%d in conn_try_disconnect()\n",
2413 rv2);
2414 }
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002415 return rv;
2416}
2417
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002418int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002419{
Lars Ellenberga910b122014-04-28 18:43:21 +02002420 struct drbd_config_context adm_ctx;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002421 struct disconnect_parms parms;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002422 struct drbd_connection *connection;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002423 enum drbd_state_rv rv;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002424 enum drbd_ret_code retcode;
2425 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002426
Lars Ellenberga910b122014-04-28 18:43:21 +02002427 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_CONNECTION);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002428 if (!adm_ctx.reply_skb)
2429 return retcode;
2430 if (retcode != NO_ERROR)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002431 goto fail;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002432
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002433 connection = adm_ctx.connection;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002434 memset(&parms, 0, sizeof(parms));
2435 if (info->attrs[DRBD_NLA_DISCONNECT_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002436 err = disconnect_parms_from_attrs(&parms, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002437 if (err) {
2438 retcode = ERR_MANDATORY_TAG;
Lars Ellenberga910b122014-04-28 18:43:21 +02002439 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002440 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002441 }
2442 }
2443
Lars Ellenberg9e276872014-04-28 18:43:22 +02002444 mutex_lock(&adm_ctx.resource->adm_mutex);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002445 rv = conn_try_disconnect(connection, parms.force_disconnect);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002446 if (rv < SS_SUCCESS)
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02002447 retcode = rv; /* FIXME: Type mismatch. */
2448 else
2449 retcode = NO_ERROR;
Lars Ellenberg9e276872014-04-28 18:43:22 +02002450 mutex_unlock(&adm_ctx.resource->adm_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002451 fail:
Lars Ellenberga910b122014-04-28 18:43:21 +02002452 drbd_adm_finish(&adm_ctx, info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002453 return 0;
2454}
2455
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002456void resync_after_online_grow(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002457{
2458 int iass; /* I am sync source */
2459
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02002460 drbd_info(device, "Resync of new storage after online grow\n");
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002461 if (device->state.role != device->state.peer)
2462 iass = (device->state.role == R_PRIMARY);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002463 else
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02002464 iass = test_bit(RESOLVE_CONFLICTS, &first_peer_device(device)->connection->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002465
2466 if (iass)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002467 drbd_start_resync(device, C_SYNC_SOURCE);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002468 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002469 _drbd_request_state(device, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE + CS_SERIALIZE);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002470}
2471
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002472int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002473{
Lars Ellenberga910b122014-04-28 18:43:21 +02002474 struct drbd_config_context adm_ctx;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002475 struct disk_conf *old_disk_conf, *new_disk_conf = NULL;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002476 struct resize_parms rs;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002477 struct drbd_device *device;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002478 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002479 enum determine_dev_size dd;
Philipp Reisnerd752b262013-06-25 16:50:08 +02002480 bool change_al_layout = false;
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002481 enum dds_flags ddsf;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002482 sector_t u_size;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002483 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002484
Lars Ellenberga910b122014-04-28 18:43:21 +02002485 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002486 if (!adm_ctx.reply_skb)
2487 return retcode;
2488 if (retcode != NO_ERROR)
Lars Ellenberg9e276872014-04-28 18:43:22 +02002489 goto finish;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002490
Lars Ellenberg9e276872014-04-28 18:43:22 +02002491 mutex_lock(&adm_ctx.resource->adm_mutex);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002492 device = adm_ctx.device;
2493 if (!get_ldev(device)) {
Philipp Reisnerd752b262013-06-25 16:50:08 +02002494 retcode = ERR_NO_DISK;
2495 goto fail;
2496 }
2497
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002498 memset(&rs, 0, sizeof(struct resize_parms));
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002499 rs.al_stripes = device->ldev->md.al_stripes;
2500 rs.al_stripe_size = device->ldev->md.al_stripe_size_4k * 4;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002501 if (info->attrs[DRBD_NLA_RESIZE_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01002502 err = resize_parms_from_attrs(&rs, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002503 if (err) {
2504 retcode = ERR_MANDATORY_TAG;
Lars Ellenberga910b122014-04-28 18:43:21 +02002505 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
Philipp Reisnerd752b262013-06-25 16:50:08 +02002506 goto fail_ldev;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002507 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002508 }
2509
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002510 if (device->state.conn > C_CONNECTED) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002511 retcode = ERR_RESIZE_RESYNC;
Philipp Reisnerd752b262013-06-25 16:50:08 +02002512 goto fail_ldev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002513 }
2514
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002515 if (device->state.role == R_SECONDARY &&
2516 device->state.peer == R_SECONDARY) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002517 retcode = ERR_NO_PRIMARY;
Philipp Reisnerd752b262013-06-25 16:50:08 +02002518 goto fail_ldev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002519 }
2520
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02002521 if (rs.no_resync && first_peer_device(device)->connection->agreed_pro_version < 93) {
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002522 retcode = ERR_NEED_APV_93;
Andreas Gruenbacher7b4e4d32011-09-28 22:15:04 +02002523 goto fail_ldev;
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002524 }
2525
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002526 rcu_read_lock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002527 u_size = rcu_dereference(device->ldev->disk_conf)->disk_size;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002528 rcu_read_unlock();
2529 if (u_size != (sector_t)rs.resize_size) {
2530 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
2531 if (!new_disk_conf) {
2532 retcode = ERR_NOMEM;
Philipp Reisner9bcd2522011-09-29 13:00:14 +02002533 goto fail_ldev;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002534 }
2535 }
2536
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002537 if (device->ldev->md.al_stripes != rs.al_stripes ||
2538 device->ldev->md.al_stripe_size_4k != rs.al_stripe_size / 4) {
Philipp Reisnerd752b262013-06-25 16:50:08 +02002539 u32 al_size_k = rs.al_stripes * rs.al_stripe_size;
2540
2541 if (al_size_k > (16 * 1024 * 1024)) {
2542 retcode = ERR_MD_LAYOUT_TOO_BIG;
2543 goto fail_ldev;
2544 }
2545
2546 if (al_size_k < MD_32kB_SECT/2) {
2547 retcode = ERR_MD_LAYOUT_TOO_SMALL;
2548 goto fail_ldev;
2549 }
2550
Philipp Reisnercdc6af82014-04-28 18:43:15 +02002551 if (device->state.conn != C_CONNECTED && !rs.resize_force) {
Philipp Reisnerd752b262013-06-25 16:50:08 +02002552 retcode = ERR_MD_LAYOUT_CONNECTED;
2553 goto fail_ldev;
2554 }
2555
2556 change_al_layout = true;
2557 }
2558
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002559 if (device->ldev->known_size != drbd_get_capacity(device->ldev->backing_bdev))
2560 device->ldev->known_size = drbd_get_capacity(device->ldev->backing_bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002561
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002562 if (new_disk_conf) {
Andreas Gruenbacher05008132011-07-07 14:19:42 +02002563 mutex_lock(&device->resource->conf_update);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002564 old_disk_conf = device->ldev->disk_conf;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002565 *new_disk_conf = *old_disk_conf;
2566 new_disk_conf->disk_size = (sector_t)rs.resize_size;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002567 rcu_assign_pointer(device->ldev->disk_conf, new_disk_conf);
Andreas Gruenbacher05008132011-07-07 14:19:42 +02002568 mutex_unlock(&device->resource->conf_update);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002569 synchronize_rcu();
2570 kfree(old_disk_conf);
2571 }
2572
Philipp Reisner6495d2c2010-03-24 16:07:04 +01002573 ddsf = (rs.resize_force ? DDSF_FORCED : 0) | (rs.no_resync ? DDSF_NO_RESYNC : 0);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002574 dd = drbd_determine_dev_size(device, ddsf, change_al_layout ? &rs : NULL);
2575 drbd_md_sync(device);
2576 put_ldev(device);
Philipp Reisnere96c9632013-06-25 16:50:07 +02002577 if (dd == DS_ERROR) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002578 retcode = ERR_NOMEM_BITMAP;
2579 goto fail;
Philipp Reisnerd752b262013-06-25 16:50:08 +02002580 } else if (dd == DS_ERROR_SPACE_MD) {
2581 retcode = ERR_MD_LAYOUT_NO_FIT;
2582 goto fail;
2583 } else if (dd == DS_ERROR_SHRINK) {
2584 retcode = ERR_IMPLICIT_SHRINK;
2585 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002586 }
2587
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002588 if (device->state.conn == C_CONNECTED) {
Philipp Reisnere96c9632013-06-25 16:50:07 +02002589 if (dd == DS_GREW)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002590 set_bit(RESIZE_PENDING, &device->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002591
Andreas Gruenbacher69a22772011-08-09 00:47:13 +02002592 drbd_send_uuids(first_peer_device(device));
2593 drbd_send_sizes(first_peer_device(device), 1, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002594 }
2595
2596 fail:
Lars Ellenberg9e276872014-04-28 18:43:22 +02002597 mutex_unlock(&adm_ctx.resource->adm_mutex);
2598 finish:
Lars Ellenberga910b122014-04-28 18:43:21 +02002599 drbd_adm_finish(&adm_ctx, info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002600 return 0;
Andreas Gruenbacher7b4e4d32011-09-28 22:15:04 +02002601
2602 fail_ldev:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002603 put_ldev(device);
Andreas Gruenbacher7b4e4d32011-09-28 22:15:04 +02002604 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002605}
2606
Lars Ellenbergf3990022011-03-23 14:31:09 +01002607int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002608{
Lars Ellenberga910b122014-04-28 18:43:21 +02002609 struct drbd_config_context adm_ctx;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002610 enum drbd_ret_code retcode;
Lars Ellenbergb57a1e22011-04-27 21:17:33 +02002611 struct res_opts res_opts;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002612 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002613
Lars Ellenberga910b122014-04-28 18:43:21 +02002614 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002615 if (!adm_ctx.reply_skb)
2616 return retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002617 if (retcode != NO_ERROR)
2618 goto fail;
2619
Andreas Gruenbachereb6bea62011-06-21 16:11:28 +02002620 res_opts = adm_ctx.resource->res_opts;
Lars Ellenberg5979e362011-04-27 21:09:55 +02002621 if (should_set_defaults(info))
Andreas Gruenbacherb966b5d2011-05-03 14:56:09 +02002622 set_res_opts_defaults(&res_opts);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002623
Lars Ellenbergb57a1e22011-04-27 21:17:33 +02002624 err = res_opts_from_attrs(&res_opts, info);
Andreas Gruenbacherc75b9b12011-05-24 14:18:31 +02002625 if (err && err != -ENOMSG) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002626 retcode = ERR_MANDATORY_TAG;
Lars Ellenberga910b122014-04-28 18:43:21 +02002627 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002628 goto fail;
2629 }
2630
Lars Ellenberg9e276872014-04-28 18:43:22 +02002631 mutex_lock(&adm_ctx.resource->adm_mutex);
Andreas Gruenbachereb6bea62011-06-21 16:11:28 +02002632 err = set_resource_options(adm_ctx.resource, &res_opts);
Andreas Gruenbacherafbbfa82011-06-16 17:58:02 +02002633 if (err) {
2634 retcode = ERR_INVALID_REQUEST;
2635 if (err == -ENOMEM)
Philipp Reisner778f2712010-07-06 11:14:00 +02002636 retcode = ERR_NOMEM;
Philipp Reisner778f2712010-07-06 11:14:00 +02002637 }
Lars Ellenberg9e276872014-04-28 18:43:22 +02002638 mutex_unlock(&adm_ctx.resource->adm_mutex);
Philipp Reisner778f2712010-07-06 11:14:00 +02002639
Philipp Reisnerb411b362009-09-25 16:07:19 -07002640fail:
Lars Ellenberga910b122014-04-28 18:43:21 +02002641 drbd_adm_finish(&adm_ctx, info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002642 return 0;
2643}
2644
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002645int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002646{
Lars Ellenberga910b122014-04-28 18:43:21 +02002647 struct drbd_config_context adm_ctx;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002648 struct drbd_device *device;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002649 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2650
Lars Ellenberga910b122014-04-28 18:43:21 +02002651 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002652 if (!adm_ctx.reply_skb)
2653 return retcode;
2654 if (retcode != NO_ERROR)
2655 goto out;
2656
Lars Ellenberg9e276872014-04-28 18:43:22 +02002657 mutex_lock(&adm_ctx.resource->adm_mutex);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002658 device = adm_ctx.device;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002659
Lars Ellenberg194bfb32011-01-18 10:38:01 +01002660 /* If there is still bitmap IO pending, probably because of a previous
Lars Ellenberg7ee1fb92012-06-19 10:27:58 +02002661 * resync just being finished, wait for it before requesting a new resync.
2662 * Also wait for it's after_state_ch(). */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002663 drbd_suspend_io(device);
2664 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
Andreas Gruenbacherb5043c52011-07-28 15:56:02 +02002665 drbd_flush_workqueue(&first_peer_device(device)->connection->sender_work);
Lars Ellenberg194bfb32011-01-18 10:38:01 +01002666
Philipp Reisner0b2dafc2013-03-27 14:08:38 +01002667 /* If we happen to be C_STANDALONE R_SECONDARY, just change to
2668 * D_INCONSISTENT, and set all bits in the bitmap. Otherwise,
2669 * try to start a resync handshake as sync target for full sync.
Philipp Reisner9376d9f2013-03-27 14:08:36 +01002670 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002671 if (device->state.conn == C_STANDALONE && device->state.role == R_SECONDARY) {
2672 retcode = drbd_request_state(device, NS(disk, D_INCONSISTENT));
Philipp Reisner0b2dafc2013-03-27 14:08:38 +01002673 if (retcode >= SS_SUCCESS) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002674 if (drbd_bitmap_io(device, &drbd_bmio_set_n_write,
Philipp Reisner0b2dafc2013-03-27 14:08:38 +01002675 "set_n_write from invalidate", BM_LOCKED_MASK))
2676 retcode = ERR_IO_MD_DISK;
2677 }
2678 } else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002679 retcode = drbd_request_state(device, NS(conn, C_STARTING_SYNC_T));
2680 drbd_resume_io(device);
Lars Ellenberg9e276872014-04-28 18:43:22 +02002681 mutex_unlock(&adm_ctx.resource->adm_mutex);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002682out:
Lars Ellenberga910b122014-04-28 18:43:21 +02002683 drbd_adm_finish(&adm_ctx, info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002684 return 0;
2685}
2686
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002687static int drbd_adm_simple_request_state(struct sk_buff *skb, struct genl_info *info,
2688 union drbd_state mask, union drbd_state val)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002689{
Lars Ellenberga910b122014-04-28 18:43:21 +02002690 struct drbd_config_context adm_ctx;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002691 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002692
Lars Ellenberga910b122014-04-28 18:43:21 +02002693 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002694 if (!adm_ctx.reply_skb)
2695 return retcode;
2696 if (retcode != NO_ERROR)
2697 goto out;
Lars Ellenberg194bfb32011-01-18 10:38:01 +01002698
Lars Ellenberg9e276872014-04-28 18:43:22 +02002699 mutex_lock(&adm_ctx.resource->adm_mutex);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002700 retcode = drbd_request_state(adm_ctx.device, mask, val);
Lars Ellenberg9e276872014-04-28 18:43:22 +02002701 mutex_unlock(&adm_ctx.resource->adm_mutex);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002702out:
Lars Ellenberga910b122014-04-28 18:43:21 +02002703 drbd_adm_finish(&adm_ctx, info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002704 return 0;
2705}
2706
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002707static int drbd_bmio_set_susp_al(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002708{
2709 int rv;
2710
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002711 rv = drbd_bmio_set_n_write(device);
2712 drbd_suspend_al(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002713 return rv;
2714}
2715
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002716int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002717{
Lars Ellenberga910b122014-04-28 18:43:21 +02002718 struct drbd_config_context adm_ctx;
Philipp Reisner25b0d6c2012-02-14 12:12:35 +01002719 int retcode; /* drbd_ret_code, drbd_state_rv */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002720 struct drbd_device *device;
Philipp Reisner25b0d6c2012-02-14 12:12:35 +01002721
Lars Ellenberga910b122014-04-28 18:43:21 +02002722 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
Philipp Reisner25b0d6c2012-02-14 12:12:35 +01002723 if (!adm_ctx.reply_skb)
2724 return retcode;
2725 if (retcode != NO_ERROR)
2726 goto out;
2727
Lars Ellenberg9e276872014-04-28 18:43:22 +02002728 mutex_lock(&adm_ctx.resource->adm_mutex);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002729 device = adm_ctx.device;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002730
2731 /* If there is still bitmap IO pending, probably because of a previous
Lars Ellenberg7ee1fb92012-06-19 10:27:58 +02002732 * resync just being finished, wait for it before requesting a new resync.
2733 * Also wait for it's after_state_ch(). */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002734 drbd_suspend_io(device);
2735 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
Andreas Gruenbacherb5043c52011-07-28 15:56:02 +02002736 drbd_flush_workqueue(&first_peer_device(device)->connection->sender_work);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002737
Philipp Reisner0b2dafc2013-03-27 14:08:38 +01002738 /* If we happen to be C_STANDALONE R_PRIMARY, just set all bits
2739 * in the bitmap. Otherwise, try to start a resync handshake
2740 * as sync source for full sync.
2741 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002742 if (device->state.conn == C_STANDALONE && device->state.role == R_PRIMARY) {
Philipp Reisner0b2dafc2013-03-27 14:08:38 +01002743 /* The peer will get a resync upon connect anyways. Just make that
2744 into a full resync. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002745 retcode = drbd_request_state(device, NS(pdsk, D_INCONSISTENT));
Philipp Reisner0b2dafc2013-03-27 14:08:38 +01002746 if (retcode >= SS_SUCCESS) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002747 if (drbd_bitmap_io(device, &drbd_bmio_set_susp_al,
Philipp Reisner0b2dafc2013-03-27 14:08:38 +01002748 "set_n_write from invalidate_peer",
2749 BM_LOCKED_SET_ALLOWED))
2750 retcode = ERR_IO_MD_DISK;
2751 }
2752 } else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002753 retcode = drbd_request_state(device, NS(conn, C_STARTING_SYNC_S));
2754 drbd_resume_io(device);
Lars Ellenberg9e276872014-04-28 18:43:22 +02002755 mutex_unlock(&adm_ctx.resource->adm_mutex);
Philipp Reisner25b0d6c2012-02-14 12:12:35 +01002756out:
Lars Ellenberga910b122014-04-28 18:43:21 +02002757 drbd_adm_finish(&adm_ctx, info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002758 return 0;
2759}
2760
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002761int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002762{
Lars Ellenberga910b122014-04-28 18:43:21 +02002763 struct drbd_config_context adm_ctx;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002764 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002765
Lars Ellenberga910b122014-04-28 18:43:21 +02002766 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002767 if (!adm_ctx.reply_skb)
2768 return retcode;
2769 if (retcode != NO_ERROR)
2770 goto out;
2771
Lars Ellenberg9e276872014-04-28 18:43:22 +02002772 mutex_lock(&adm_ctx.resource->adm_mutex);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002773 if (drbd_request_state(adm_ctx.device, NS(user_isp, 1)) == SS_NOTHING_TO_DO)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002774 retcode = ERR_PAUSE_IS_SET;
Lars Ellenberg9e276872014-04-28 18:43:22 +02002775 mutex_unlock(&adm_ctx.resource->adm_mutex);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002776out:
Lars Ellenberga910b122014-04-28 18:43:21 +02002777 drbd_adm_finish(&adm_ctx, info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002778 return 0;
2779}
2780
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002781int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002782{
Lars Ellenberga910b122014-04-28 18:43:21 +02002783 struct drbd_config_context adm_ctx;
Philipp Reisnerda9fbc22011-03-29 10:52:01 +02002784 union drbd_dev_state s;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002785 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002786
Lars Ellenberga910b122014-04-28 18:43:21 +02002787 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002788 if (!adm_ctx.reply_skb)
2789 return retcode;
2790 if (retcode != NO_ERROR)
2791 goto out;
2792
Lars Ellenberg9e276872014-04-28 18:43:22 +02002793 mutex_lock(&adm_ctx.resource->adm_mutex);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002794 if (drbd_request_state(adm_ctx.device, NS(user_isp, 0)) == SS_NOTHING_TO_DO) {
2795 s = adm_ctx.device->state;
Philipp Reisnercd88d032011-01-20 11:46:41 +01002796 if (s.conn == C_PAUSED_SYNC_S || s.conn == C_PAUSED_SYNC_T) {
2797 retcode = s.aftr_isp ? ERR_PIC_AFTER_DEP :
2798 s.peer_isp ? ERR_PIC_PEER_DEP : ERR_PAUSE_IS_CLEAR;
2799 } else {
2800 retcode = ERR_PAUSE_IS_CLEAR;
2801 }
2802 }
Lars Ellenberg9e276872014-04-28 18:43:22 +02002803 mutex_unlock(&adm_ctx.resource->adm_mutex);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002804out:
Lars Ellenberga910b122014-04-28 18:43:21 +02002805 drbd_adm_finish(&adm_ctx, info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002806 return 0;
2807}
2808
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002809int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002810{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002811 return drbd_adm_simple_request_state(skb, info, NS(susp, 1));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002812}
2813
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002814int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002815{
Lars Ellenberga910b122014-04-28 18:43:21 +02002816 struct drbd_config_context adm_ctx;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002817 struct drbd_device *device;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002818 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2819
Lars Ellenberga910b122014-04-28 18:43:21 +02002820 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002821 if (!adm_ctx.reply_skb)
2822 return retcode;
2823 if (retcode != NO_ERROR)
2824 goto out;
2825
Lars Ellenberg9e276872014-04-28 18:43:22 +02002826 mutex_lock(&adm_ctx.resource->adm_mutex);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002827 device = adm_ctx.device;
2828 if (test_bit(NEW_CUR_UUID, &device->flags)) {
2829 drbd_uuid_new_current(device);
2830 clear_bit(NEW_CUR_UUID, &device->flags);
Philipp Reisner43a51822010-06-11 11:26:34 +02002831 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002832 drbd_suspend_io(device);
2833 retcode = drbd_request_state(device, NS3(susp, 0, susp_nod, 0, susp_fen, 0));
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002834 if (retcode == SS_SUCCESS) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002835 if (device->state.conn < C_CONNECTED)
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02002836 tl_clear(first_peer_device(device)->connection);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002837 if (device->state.disk == D_DISKLESS || device->state.disk == D_FAILED)
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02002838 tl_restart(first_peer_device(device)->connection, FAIL_FROZEN_DISK_IO);
Philipp Reisner265be2d2010-05-31 10:14:17 +02002839 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002840 drbd_resume_io(device);
Lars Ellenberg9e276872014-04-28 18:43:22 +02002841 mutex_unlock(&adm_ctx.resource->adm_mutex);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002842out:
Lars Ellenberga910b122014-04-28 18:43:21 +02002843 drbd_adm_finish(&adm_ctx, info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002844 return 0;
2845}
2846
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002847int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002848{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002849 return drbd_adm_simple_request_state(skb, info, NS(disk, D_OUTDATED));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002850}
2851
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02002852static int nla_put_drbd_cfg_context(struct sk_buff *skb,
2853 struct drbd_resource *resource,
2854 struct drbd_connection *connection,
2855 struct drbd_device *device)
Lars Ellenberg543cc102011-03-10 22:18:18 +01002856{
2857 struct nlattr *nla;
2858 nla = nla_nest_start(skb, DRBD_NLA_CFG_CONTEXT);
2859 if (!nla)
2860 goto nla_put_failure;
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02002861 if (device &&
2862 nla_put_u32(skb, T_ctx_volume, device->vnr))
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002863 goto nla_put_failure;
Andreas Gruenbacherf597f6b2014-02-19 10:49:07 +01002864 if (nla_put_string(skb, T_ctx_resource_name, resource->name))
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002865 goto nla_put_failure;
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02002866 if (connection) {
2867 if (connection->my_addr_len &&
2868 nla_put(skb, T_ctx_my_addr, connection->my_addr_len, &connection->my_addr))
2869 goto nla_put_failure;
2870 if (connection->peer_addr_len &&
2871 nla_put(skb, T_ctx_peer_addr, connection->peer_addr_len, &connection->peer_addr))
2872 goto nla_put_failure;
2873 }
Lars Ellenberg543cc102011-03-10 22:18:18 +01002874 nla_nest_end(skb, nla);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002875 return 0;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002876
2877nla_put_failure:
2878 if (nla)
2879 nla_nest_cancel(skb, nla);
2880 return -EMSGSIZE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002881}
2882
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02002883/*
2884 * Return the connection of @resource if @resource has exactly one connection.
2885 */
2886static struct drbd_connection *the_only_connection(struct drbd_resource *resource)
2887{
2888 struct list_head *connections = &resource->connections;
2889
2890 if (list_empty(connections) || connections->next->next != connections)
2891 return NULL;
2892 return list_first_entry(&resource->connections, struct drbd_connection, connections);
2893}
2894
2895int nla_put_status_info(struct sk_buff *skb, struct drbd_device *device,
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002896 const struct sib_info *sib)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002897{
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02002898 struct drbd_resource *resource = device->resource;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002899 struct state_info *si = NULL; /* for sizeof(si->member); */
2900 struct nlattr *nla;
2901 int got_ldev;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002902 int err = 0;
2903 int exclude_sensitive;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002904
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002905 /* If sib != NULL, this is drbd_bcast_event, which anyone can listen
2906 * to. So we better exclude_sensitive information.
2907 *
2908 * If sib == NULL, this is drbd_adm_get_status, executed synchronously
2909 * in the context of the requesting user process. Exclude sensitive
2910 * information, unless current has superuser.
2911 *
2912 * NOTE: for drbd_adm_get_status_all(), this is a netlink dump, and
2913 * relies on the current implementation of netlink_dump(), which
2914 * executes the dump callback successively from netlink_recvmsg(),
2915 * always in the context of the receiving process */
2916 exclude_sensitive = sib || !capable(CAP_SYS_ADMIN);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002917
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002918 got_ldev = get_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002919
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002920 /* We need to add connection name and volume number information still.
2921 * Minor number is in drbd_genlmsghdr. */
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02002922 if (nla_put_drbd_cfg_context(skb, resource, the_only_connection(resource), device))
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002923 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002924
Andreas Gruenbachereb6bea62011-06-21 16:11:28 +02002925 if (res_opts_to_skb(skb, &device->resource->res_opts, exclude_sensitive))
Lars Ellenbergf3990022011-03-23 14:31:09 +01002926 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002927
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002928 rcu_read_lock();
Andreas Gruenbacherf9eb7bf2013-06-25 16:50:05 +02002929 if (got_ldev) {
2930 struct disk_conf *disk_conf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002931
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002932 disk_conf = rcu_dereference(device->ldev->disk_conf);
Andreas Gruenbacherf9eb7bf2013-06-25 16:50:05 +02002933 err = disk_conf_to_skb(skb, disk_conf, exclude_sensitive);
2934 }
2935 if (!err) {
2936 struct net_conf *nc;
2937
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02002938 nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
Andreas Gruenbacherf9eb7bf2013-06-25 16:50:05 +02002939 if (nc)
2940 err = net_conf_to_skb(skb, nc, exclude_sensitive);
2941 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02002942 rcu_read_unlock();
2943 if (err)
2944 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002945
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002946 nla = nla_nest_start(skb, DRBD_NLA_STATE_INFO);
2947 if (!nla)
2948 goto nla_put_failure;
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002949 if (nla_put_u32(skb, T_sib_reason, sib ? sib->sib_reason : SIB_GET_STATUS_REPLY) ||
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002950 nla_put_u32(skb, T_current_state, device->state.i) ||
2951 nla_put_u64(skb, T_ed_uuid, device->ed_uuid) ||
2952 nla_put_u64(skb, T_capacity, drbd_get_capacity(device->this_bdev)) ||
2953 nla_put_u64(skb, T_send_cnt, device->send_cnt) ||
2954 nla_put_u64(skb, T_recv_cnt, device->recv_cnt) ||
2955 nla_put_u64(skb, T_read_cnt, device->read_cnt) ||
2956 nla_put_u64(skb, T_writ_cnt, device->writ_cnt) ||
2957 nla_put_u64(skb, T_al_writ_cnt, device->al_writ_cnt) ||
2958 nla_put_u64(skb, T_bm_writ_cnt, device->bm_writ_cnt) ||
2959 nla_put_u32(skb, T_ap_bio_cnt, atomic_read(&device->ap_bio_cnt)) ||
2960 nla_put_u32(skb, T_ap_pending_cnt, atomic_read(&device->ap_pending_cnt)) ||
2961 nla_put_u32(skb, T_rs_pending_cnt, atomic_read(&device->rs_pending_cnt)))
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002962 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002963
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002964 if (got_ldev) {
Philipp Reisner39a1aa72012-08-08 21:19:09 +02002965 int err;
2966
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002967 spin_lock_irq(&device->ldev->md.uuid_lock);
2968 err = nla_put(skb, T_uuids, sizeof(si->uuids), device->ldev->md.uuid);
2969 spin_unlock_irq(&device->ldev->md.uuid_lock);
Philipp Reisner39a1aa72012-08-08 21:19:09 +02002970
2971 if (err)
2972 goto nla_put_failure;
2973
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002974 if (nla_put_u32(skb, T_disk_flags, device->ldev->md.flags) ||
2975 nla_put_u64(skb, T_bits_total, drbd_bm_bits(device)) ||
2976 nla_put_u64(skb, T_bits_oos, drbd_bm_total_weight(device)))
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002977 goto nla_put_failure;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002978 if (C_SYNC_SOURCE <= device->state.conn &&
2979 C_PAUSED_SYNC_T >= device->state.conn) {
2980 if (nla_put_u64(skb, T_bits_rs_total, device->rs_total) ||
2981 nla_put_u64(skb, T_bits_rs_failed, device->rs_failed))
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002982 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002983 }
2984 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002985
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002986 if (sib) {
2987 switch(sib->sib_reason) {
2988 case SIB_SYNC_PROGRESS:
2989 case SIB_GET_STATUS_REPLY:
2990 break;
2991 case SIB_STATE_CHANGE:
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002992 if (nla_put_u32(skb, T_prev_state, sib->os.i) ||
2993 nla_put_u32(skb, T_new_state, sib->ns.i))
2994 goto nla_put_failure;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002995 break;
2996 case SIB_HELPER_POST:
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02002997 if (nla_put_u32(skb, T_helper_exit_code,
2998 sib->helper_exit_code))
2999 goto nla_put_failure;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003000 /* fall through */
3001 case SIB_HELPER_PRE:
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +02003002 if (nla_put_string(skb, T_helper, sib->helper_name))
3003 goto nla_put_failure;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003004 break;
3005 }
3006 }
3007 nla_nest_end(skb, nla);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003008
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003009 if (0)
3010nla_put_failure:
3011 err = -EMSGSIZE;
3012 if (got_ldev)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003013 put_ldev(device);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003014 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003015}
3016
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003017int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003018{
Lars Ellenberga910b122014-04-28 18:43:21 +02003019 struct drbd_config_context adm_ctx;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003020 enum drbd_ret_code retcode;
3021 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003022
Lars Ellenberga910b122014-04-28 18:43:21 +02003023 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003024 if (!adm_ctx.reply_skb)
3025 return retcode;
3026 if (retcode != NO_ERROR)
3027 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003028
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003029 err = nla_put_status_info(adm_ctx.reply_skb, adm_ctx.device, NULL);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003030 if (err) {
3031 nlmsg_free(adm_ctx.reply_skb);
3032 return err;
3033 }
3034out:
Lars Ellenberga910b122014-04-28 18:43:21 +02003035 drbd_adm_finish(&adm_ctx, info, retcode);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003036 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003037}
3038
Rashika Kheria4b7a5302013-12-19 15:17:33 +05303039static int get_one_status(struct sk_buff *skb, struct netlink_callback *cb)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003040{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003041 struct drbd_device *device;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003042 struct drbd_genlmsghdr *dh;
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003043 struct drbd_resource *pos = (struct drbd_resource *)cb->args[0];
3044 struct drbd_resource *resource = NULL;
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003045 struct drbd_resource *tmp;
Lars Ellenberg543cc102011-03-10 22:18:18 +01003046 unsigned volume = cb->args[1];
Philipp Reisnerb411b362009-09-25 16:07:19 -07003047
Lars Ellenberg543cc102011-03-10 22:18:18 +01003048 /* Open coded, deferred, iteration:
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003049 * for_each_resource_safe(resource, tmp, &drbd_resources) {
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02003050 * connection = "first connection of resource or undefined";
3051 * idr_for_each_entry(&resource->devices, device, i) {
Lars Ellenberg543cc102011-03-10 22:18:18 +01003052 * ...
3053 * }
3054 * }
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003055 * where resource is cb->args[0];
Lars Ellenberg543cc102011-03-10 22:18:18 +01003056 * and i is cb->args[1];
3057 *
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003058 * cb->args[2] indicates if we shall loop over all resources,
3059 * or just dump all volumes of a single resource.
3060 *
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003061 * This may miss entries inserted after this dump started,
3062 * or entries deleted before they are reached.
Lars Ellenberg543cc102011-03-10 22:18:18 +01003063 *
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003064 * We need to make sure the device won't disappear while
Lars Ellenberg543cc102011-03-10 22:18:18 +01003065 * we are looking at it, and revalidate our iterators
3066 * on each iteration.
3067 */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003068
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +02003069 /* synchronize with conn_create()/drbd_destroy_connection() */
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02003070 rcu_read_lock();
Lars Ellenberg543cc102011-03-10 22:18:18 +01003071 /* revalidate iterator position */
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003072 for_each_resource_rcu(tmp, &drbd_resources) {
Lars Ellenberg543cc102011-03-10 22:18:18 +01003073 if (pos == NULL) {
3074 /* first iteration */
3075 pos = tmp;
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003076 resource = pos;
Lars Ellenberg543cc102011-03-10 22:18:18 +01003077 break;
3078 }
3079 if (tmp == pos) {
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003080 resource = pos;
Lars Ellenberg543cc102011-03-10 22:18:18 +01003081 break;
3082 }
3083 }
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003084 if (resource) {
3085next_resource:
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02003086 device = idr_get_next(&resource->devices, &volume);
3087 if (!device) {
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003088 /* No more volumes to dump on this resource.
3089 * Advance resource iterator. */
3090 pos = list_entry_rcu(resource->resources.next,
3091 struct drbd_resource, resources);
3092 /* Did we dump any volume of this resource yet? */
Lars Ellenberg543cc102011-03-10 22:18:18 +01003093 if (volume != 0) {
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003094 /* If we reached the end of the list,
3095 * or only a single resource dump was requested,
3096 * we are done. */
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003097 if (&pos->resources == &drbd_resources || cb->args[2])
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003098 goto out;
Lars Ellenberg543cc102011-03-10 22:18:18 +01003099 volume = 0;
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003100 resource = pos;
3101 goto next_resource;
Lars Ellenberg543cc102011-03-10 22:18:18 +01003102 }
3103 }
3104
Philipp Reisner98683652012-11-09 14:18:43 +01003105 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003106 cb->nlh->nlmsg_seq, &drbd_genl_family,
3107 NLM_F_MULTI, DRBD_ADM_GET_STATUS);
3108 if (!dh)
Lars Ellenberg543cc102011-03-10 22:18:18 +01003109 goto out;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003110
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02003111 if (!device) {
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02003112 /* This is a connection without a single volume.
Lars Ellenberg367d675d2011-07-11 23:49:55 +02003113 * Suprisingly enough, it may have a network
3114 * configuration. */
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02003115 struct drbd_connection *connection;
3116
Lars Ellenberg543cc102011-03-10 22:18:18 +01003117 dh->minor = -1U;
3118 dh->ret_code = NO_ERROR;
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02003119 connection = the_only_connection(resource);
3120 if (nla_put_drbd_cfg_context(skb, resource, connection, NULL))
Lars Ellenberg367d675d2011-07-11 23:49:55 +02003121 goto cancel;
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02003122 if (connection) {
3123 struct net_conf *nc;
3124
3125 nc = rcu_dereference(connection->net_conf);
3126 if (nc && net_conf_to_skb(skb, nc, 1) != 0)
3127 goto cancel;
3128 }
Lars Ellenberg367d675d2011-07-11 23:49:55 +02003129 goto done;
Lars Ellenberg543cc102011-03-10 22:18:18 +01003130 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003131
Andreas Gruenbacher0b0ba1e2011-06-27 16:23:33 +02003132 D_ASSERT(device, device->vnr == volume);
Andreas Gruenbacher251b8f82011-07-05 18:23:07 +02003133 D_ASSERT(device, device->resource == resource);
Lars Ellenberg543cc102011-03-10 22:18:18 +01003134
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003135 dh->minor = device_to_minor(device);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003136 dh->ret_code = NO_ERROR;
3137
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003138 if (nla_put_status_info(skb, device, NULL)) {
Lars Ellenberg367d675d2011-07-11 23:49:55 +02003139cancel:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003140 genlmsg_cancel(skb, dh);
Lars Ellenberg543cc102011-03-10 22:18:18 +01003141 goto out;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003142 }
Lars Ellenberg367d675d2011-07-11 23:49:55 +02003143done:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003144 genlmsg_end(skb, dh);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02003145 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003146
Lars Ellenberg543cc102011-03-10 22:18:18 +01003147out:
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02003148 rcu_read_unlock();
Lars Ellenberg543cc102011-03-10 22:18:18 +01003149 /* where to start the next iteration */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02003150 cb->args[0] = (long)pos;
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003151 cb->args[1] = (pos == resource) ? volume + 1 : 0;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003152
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003153 /* No more resources/volumes/minors found results in an empty skb.
Lars Ellenberg543cc102011-03-10 22:18:18 +01003154 * Which will terminate the dump. */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003155 return skb->len;
3156}
3157
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003158/*
3159 * Request status of all resources, or of all volumes within a single resource.
3160 *
3161 * This is a dump, as the answer may not fit in a single reply skb otherwise.
3162 * Which means we cannot use the family->attrbuf or other such members, because
3163 * dump is NOT protected by the genl_lock(). During dump, we only have access
3164 * to the incoming skb, and need to opencode "parsing" of the nlattr payload.
3165 *
3166 * Once things are setup properly, we call into get_one_status().
Philipp Reisnerb411b362009-09-25 16:07:19 -07003167 */
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003168int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003169{
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003170 const unsigned hdrlen = GENL_HDRLEN + GENL_MAGIC_FAMILY_HDRSZ;
3171 struct nlattr *nla;
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003172 const char *resource_name;
Andreas Gruenbacher4bc76042011-06-13 14:27:45 +02003173 struct drbd_resource *resource;
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003174 int maxtype;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003175
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003176 /* Is this a followup call? */
3177 if (cb->args[0]) {
3178 /* ... of a single resource dump,
3179 * and the resource iterator has been advanced already? */
3180 if (cb->args[2] && cb->args[2] != cb->args[0])
3181 return 0; /* DONE. */
3182 goto dump;
3183 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003184
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003185 /* First call (from netlink_dump_start). We need to figure out
3186 * which resource(s) the user wants us to dump. */
3187 nla = nla_find(nlmsg_attrdata(cb->nlh, hdrlen),
3188 nlmsg_attrlen(cb->nlh, hdrlen),
3189 DRBD_NLA_CFG_CONTEXT);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003190
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003191 /* No explicit context given. Dump all. */
3192 if (!nla)
3193 goto dump;
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003194 maxtype = ARRAY_SIZE(drbd_cfg_context_nl_policy) - 1;
3195 nla = drbd_nla_find_nested(maxtype, nla, __nla_type(T_ctx_resource_name));
3196 if (IS_ERR(nla))
3197 return PTR_ERR(nla);
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003198 /* context given, but no name present? */
3199 if (!nla)
3200 return -EINVAL;
Andreas Gruenbacher7c3063c2011-06-09 17:52:12 +02003201 resource_name = nla_data(nla);
Andreas Gruenbacher4bc76042011-06-13 14:27:45 +02003202 if (!*resource_name)
3203 return -ENODEV;
3204 resource = drbd_find_resource(resource_name);
3205 if (!resource)
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003206 return -ENODEV;
3207
Andreas Gruenbacher4bc76042011-06-13 14:27:45 +02003208 kref_put(&resource->kref, drbd_destroy_resource); /* get_one_status() revalidates the resource */
Philipp Reisner0ace9df2011-04-24 10:53:19 +02003209
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003210 /* prime iterators, and set "filter" mode mark:
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02003211 * only dump this connection. */
Andreas Gruenbacher4bc76042011-06-13 14:27:45 +02003212 cb->args[0] = (long)resource;
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003213 /* cb->args[1] = 0; passed in this way. */
Andreas Gruenbacher4bc76042011-06-13 14:27:45 +02003214 cb->args[2] = (long)resource;
Lars Ellenberg71932ef2011-04-18 09:43:25 +02003215
3216dump:
3217 return get_one_status(skb, cb);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003218}
3219
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003220int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003221{
Lars Ellenberga910b122014-04-28 18:43:21 +02003222 struct drbd_config_context adm_ctx;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003223 enum drbd_ret_code retcode;
3224 struct timeout_parms tp;
3225 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003226
Lars Ellenberga910b122014-04-28 18:43:21 +02003227 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003228 if (!adm_ctx.reply_skb)
3229 return retcode;
3230 if (retcode != NO_ERROR)
3231 goto out;
3232
3233 tp.timeout_type =
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003234 adm_ctx.device->state.pdsk == D_OUTDATED ? UT_PEER_OUTDATED :
3235 test_bit(USE_DEGR_WFC_T, &adm_ctx.device->flags) ? UT_DEGRADED :
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003236 UT_DEFAULT;
3237
3238 err = timeout_parms_to_priv_skb(adm_ctx.reply_skb, &tp);
3239 if (err) {
3240 nlmsg_free(adm_ctx.reply_skb);
3241 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003242 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003243out:
Lars Ellenberga910b122014-04-28 18:43:21 +02003244 drbd_adm_finish(&adm_ctx, info, retcode);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003245 return 0;
3246}
Lars Ellenberg873b0d52011-01-21 22:53:48 +01003247
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003248int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info)
3249{
Lars Ellenberga910b122014-04-28 18:43:21 +02003250 struct drbd_config_context adm_ctx;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003251 struct drbd_device *device;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003252 enum drbd_ret_code retcode;
Lars Ellenberg58ffa582012-07-26 14:09:49 +02003253 struct start_ov_parms parms;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003254
Lars Ellenberga910b122014-04-28 18:43:21 +02003255 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003256 if (!adm_ctx.reply_skb)
3257 return retcode;
3258 if (retcode != NO_ERROR)
3259 goto out;
3260
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003261 device = adm_ctx.device;
Lars Ellenberg58ffa582012-07-26 14:09:49 +02003262
3263 /* resume from last known position, if possible */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003264 parms.ov_start_sector = device->ov_start_sector;
Lars Ellenberg58ffa582012-07-26 14:09:49 +02003265 parms.ov_stop_sector = ULLONG_MAX;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003266 if (info->attrs[DRBD_NLA_START_OV_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01003267 int err = start_ov_parms_from_attrs(&parms, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003268 if (err) {
3269 retcode = ERR_MANDATORY_TAG;
Lars Ellenberga910b122014-04-28 18:43:21 +02003270 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003271 goto out;
3272 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003273 }
Lars Ellenberg9e276872014-04-28 18:43:22 +02003274 mutex_lock(&adm_ctx.resource->adm_mutex);
3275
Lars Ellenberg58ffa582012-07-26 14:09:49 +02003276 /* w_make_ov_request expects position to be aligned */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003277 device->ov_start_sector = parms.ov_start_sector & ~(BM_SECT_PER_BIT-1);
3278 device->ov_stop_sector = parms.ov_stop_sector;
Lars Ellenberg873b0d52011-01-21 22:53:48 +01003279
3280 /* If there is still bitmap IO pending, e.g. previous resync or verify
3281 * just being finished, wait for it before requesting a new resync. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003282 drbd_suspend_io(device);
3283 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
3284 retcode = drbd_request_state(device, NS(conn, C_VERIFY_S));
3285 drbd_resume_io(device);
Lars Ellenberg9e276872014-04-28 18:43:22 +02003286
3287 mutex_unlock(&adm_ctx.resource->adm_mutex);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003288out:
Lars Ellenberga910b122014-04-28 18:43:21 +02003289 drbd_adm_finish(&adm_ctx, info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003290 return 0;
3291}
3292
3293
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003294int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003295{
Lars Ellenberga910b122014-04-28 18:43:21 +02003296 struct drbd_config_context adm_ctx;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003297 struct drbd_device *device;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003298 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003299 int skip_initial_sync = 0;
3300 int err;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003301 struct new_c_uuid_parms args;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003302
Lars Ellenberga910b122014-04-28 18:43:21 +02003303 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003304 if (!adm_ctx.reply_skb)
3305 return retcode;
3306 if (retcode != NO_ERROR)
3307 goto out_nolock;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003308
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003309 device = adm_ctx.device;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003310 memset(&args, 0, sizeof(args));
3311 if (info->attrs[DRBD_NLA_NEW_C_UUID_PARMS]) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01003312 err = new_c_uuid_parms_from_attrs(&args, info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003313 if (err) {
3314 retcode = ERR_MANDATORY_TAG;
Lars Ellenberga910b122014-04-28 18:43:21 +02003315 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003316 goto out_nolock;
3317 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003318 }
3319
Lars Ellenberg9e276872014-04-28 18:43:22 +02003320 mutex_lock(&adm_ctx.resource->adm_mutex);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003321 mutex_lock(device->state_mutex); /* Protects us against serialized state changes. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07003322
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003323 if (!get_ldev(device)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003324 retcode = ERR_NO_DISK;
3325 goto out;
3326 }
3327
3328 /* this is "skip initial sync", assume to be clean */
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02003329 if (device->state.conn == C_CONNECTED &&
3330 first_peer_device(device)->connection->agreed_pro_version >= 90 &&
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003331 device->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED && args.clear_bm) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02003332 drbd_info(device, "Preparing to skip initial sync\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07003333 skip_initial_sync = 1;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003334 } else if (device->state.conn != C_STANDALONE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003335 retcode = ERR_CONNECTED;
3336 goto out_dec;
3337 }
3338
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003339 drbd_uuid_set(device, UI_BITMAP, 0); /* Rotate UI_BITMAP to History 1, etc... */
3340 drbd_uuid_new_current(device); /* New current, previous to UI_BITMAP */
Philipp Reisnerb411b362009-09-25 16:07:19 -07003341
3342 if (args.clear_bm) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003343 err = drbd_bitmap_io(device, &drbd_bmio_clear_n_write,
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003344 "clear_n_write from new_c_uuid", BM_LOCKED_MASK);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003345 if (err) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02003346 drbd_err(device, "Writing bitmap failed with %d\n", err);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003347 retcode = ERR_IO_MD_DISK;
3348 }
3349 if (skip_initial_sync) {
Andreas Gruenbacher69a22772011-08-09 00:47:13 +02003350 drbd_send_uuids_skip_initial_sync(first_peer_device(device));
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003351 _drbd_uuid_set(device, UI_BITMAP, 0);
3352 drbd_print_uuids(device, "cleared bitmap UUID");
Andreas Gruenbacher05008132011-07-07 14:19:42 +02003353 spin_lock_irq(&device->resource->req_lock);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003354 _drbd_set_state(_NS2(device, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
Philipp Reisnerb411b362009-09-25 16:07:19 -07003355 CS_VERBOSE, NULL);
Andreas Gruenbacher05008132011-07-07 14:19:42 +02003356 spin_unlock_irq(&device->resource->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003357 }
3358 }
3359
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003360 drbd_md_sync(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003361out_dec:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003362 put_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003363out:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003364 mutex_unlock(device->state_mutex);
Lars Ellenberg9e276872014-04-28 18:43:22 +02003365 mutex_unlock(&adm_ctx.resource->adm_mutex);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003366out_nolock:
Lars Ellenberga910b122014-04-28 18:43:21 +02003367 drbd_adm_finish(&adm_ctx, info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003368 return 0;
3369}
3370
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003371static enum drbd_ret_code
Lars Ellenberga910b122014-04-28 18:43:21 +02003372drbd_check_resource_name(struct drbd_config_context *adm_ctx)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003373{
Lars Ellenberga910b122014-04-28 18:43:21 +02003374 const char *name = adm_ctx->resource_name;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003375 if (!name || !name[0]) {
Lars Ellenberga910b122014-04-28 18:43:21 +02003376 drbd_msg_put_info(adm_ctx->reply_skb, "resource name missing");
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003377 return ERR_MANDATORY_TAG;
3378 }
3379 /* if we want to use these in sysfs/configfs/debugfs some day,
3380 * we must not allow slashes */
3381 if (strchr(name, '/')) {
Lars Ellenberga910b122014-04-28 18:43:21 +02003382 drbd_msg_put_info(adm_ctx->reply_skb, "invalid resource name");
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003383 return ERR_INVALID_REQUEST;
3384 }
3385 return NO_ERROR;
3386}
Philipp Reisnerb411b362009-09-25 16:07:19 -07003387
Andreas Gruenbacher789c1b62011-06-06 16:16:44 +02003388int drbd_adm_new_resource(struct sk_buff *skb, struct genl_info *info)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003389{
Lars Ellenberga910b122014-04-28 18:43:21 +02003390 struct drbd_config_context adm_ctx;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003391 enum drbd_ret_code retcode;
Andreas Gruenbacherafbbfa82011-06-16 17:58:02 +02003392 struct res_opts res_opts;
3393 int err;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003394
Lars Ellenberga910b122014-04-28 18:43:21 +02003395 retcode = drbd_adm_prepare(&adm_ctx, skb, info, 0);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003396 if (!adm_ctx.reply_skb)
3397 return retcode;
3398 if (retcode != NO_ERROR)
3399 goto out;
3400
Andreas Gruenbacherafbbfa82011-06-16 17:58:02 +02003401 set_res_opts_defaults(&res_opts);
3402 err = res_opts_from_attrs(&res_opts, info);
3403 if (err && err != -ENOMSG) {
3404 retcode = ERR_MANDATORY_TAG;
Lars Ellenberga910b122014-04-28 18:43:21 +02003405 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
Andreas Gruenbacherafbbfa82011-06-16 17:58:02 +02003406 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003407 }
3408
Lars Ellenberga910b122014-04-28 18:43:21 +02003409 retcode = drbd_check_resource_name(&adm_ctx);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003410 if (retcode != NO_ERROR)
3411 goto out;
3412
Andreas Gruenbacher5c661042011-07-05 18:26:00 +02003413 if (adm_ctx.resource) {
Lars Ellenberg38f19612011-03-14 13:22:35 +01003414 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL) {
3415 retcode = ERR_INVALID_REQUEST;
Lars Ellenberga910b122014-04-28 18:43:21 +02003416 drbd_msg_put_info(adm_ctx.reply_skb, "resource exists");
Lars Ellenberg38f19612011-03-14 13:22:35 +01003417 }
3418 /* else: still NO_ERROR */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003419 goto out;
Philipp Reisner9f5180e2009-10-06 09:30:14 +02003420 }
3421
Lars Ellenberg9e276872014-04-28 18:43:22 +02003422 /* not yet safe for genl_family.parallel_ops */
Andreas Gruenbacherafbbfa82011-06-16 17:58:02 +02003423 if (!conn_create(adm_ctx.resource_name, &res_opts))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003424 retcode = ERR_NOMEM;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003425out:
Lars Ellenberga910b122014-04-28 18:43:21 +02003426 drbd_adm_finish(&adm_ctx, info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003427 return 0;
3428}
3429
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +02003430int drbd_adm_new_minor(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003431{
Lars Ellenberga910b122014-04-28 18:43:21 +02003432 struct drbd_config_context adm_ctx;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003433 struct drbd_genlmsghdr *dh = info->userhdr;
3434 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003435
Lars Ellenberga910b122014-04-28 18:43:21 +02003436 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003437 if (!adm_ctx.reply_skb)
3438 return retcode;
3439 if (retcode != NO_ERROR)
3440 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003441
Andreas Gruenbacherf2257a52011-07-14 16:00:40 +02003442 if (dh->minor > MINORMASK) {
Lars Ellenberga910b122014-04-28 18:43:21 +02003443 drbd_msg_put_info(adm_ctx.reply_skb, "requested minor out of range");
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003444 retcode = ERR_INVALID_REQUEST;
3445 goto out;
3446 }
Andreas Gruenbacher0c8e36d2011-03-30 16:00:17 +02003447 if (adm_ctx.volume > DRBD_VOLUME_MAX) {
Lars Ellenberga910b122014-04-28 18:43:21 +02003448 drbd_msg_put_info(adm_ctx.reply_skb, "requested volume id out of range");
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003449 retcode = ERR_INVALID_REQUEST;
3450 goto out;
Philipp Reisner774b3052011-02-22 02:07:03 -05003451 }
3452
Lars Ellenberg38f19612011-03-14 13:22:35 +01003453 /* drbd_adm_prepare made sure already
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02003454 * that first_peer_device(device)->connection and device->vnr match the request. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003455 if (adm_ctx.device) {
Lars Ellenberg38f19612011-03-14 13:22:35 +01003456 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
3457 retcode = ERR_MINOR_EXISTS;
3458 /* else: still NO_ERROR */
3459 goto out;
3460 }
3461
Lars Ellenberg9e276872014-04-28 18:43:22 +02003462 mutex_lock(&adm_ctx.resource->adm_mutex);
Lars Ellenberga910b122014-04-28 18:43:21 +02003463 retcode = drbd_create_device(&adm_ctx, dh->minor);
Lars Ellenberg9e276872014-04-28 18:43:22 +02003464 mutex_unlock(&adm_ctx.resource->adm_mutex);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003465out:
Lars Ellenberga910b122014-04-28 18:43:21 +02003466 drbd_adm_finish(&adm_ctx, info, retcode);
Philipp Reisner774b3052011-02-22 02:07:03 -05003467 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003468}
3469
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +02003470static enum drbd_ret_code adm_del_minor(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003471{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003472 if (device->state.disk == D_DISKLESS &&
3473 /* no need to be device->state.conn == C_STANDALONE &&
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003474 * we may want to delete a minor from a live replication group.
3475 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003476 device->state.role == R_SECONDARY) {
3477 _drbd_request_state(device, NS(conn, C_WF_REPORT_PARAMS),
Philipp Reisner369bea62011-07-06 23:04:44 +02003478 CS_VERBOSE + CS_WAIT_COMPLETE);
Andreas Gruenbacherf82795d2011-07-03 23:32:26 +02003479 drbd_delete_device(device);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003480 return NO_ERROR;
3481 } else
3482 return ERR_MINOR_CONFIGURED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003483}
3484
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +02003485int drbd_adm_del_minor(struct sk_buff *skb, struct genl_info *info)
Philipp Reisner774b3052011-02-22 02:07:03 -05003486{
Lars Ellenberga910b122014-04-28 18:43:21 +02003487 struct drbd_config_context adm_ctx;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003488 enum drbd_ret_code retcode;
3489
Lars Ellenberga910b122014-04-28 18:43:21 +02003490 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003491 if (!adm_ctx.reply_skb)
3492 return retcode;
3493 if (retcode != NO_ERROR)
3494 goto out;
3495
Lars Ellenberg9e276872014-04-28 18:43:22 +02003496 mutex_lock(&adm_ctx.resource->adm_mutex);
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +02003497 retcode = adm_del_minor(adm_ctx.device);
Lars Ellenberg9e276872014-04-28 18:43:22 +02003498 mutex_unlock(&adm_ctx.resource->adm_mutex);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003499out:
Lars Ellenberga910b122014-04-28 18:43:21 +02003500 drbd_adm_finish(&adm_ctx, info, retcode);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003501 return 0;
3502}
3503
3504int drbd_adm_down(struct sk_buff *skb, struct genl_info *info)
3505{
Lars Ellenberga910b122014-04-28 18:43:21 +02003506 struct drbd_config_context adm_ctx;
Andreas Gruenbacherb6f85ef2011-07-06 15:03:31 +02003507 struct drbd_resource *resource;
3508 struct drbd_connection *connection;
3509 struct drbd_device *device;
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02003510 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003511 unsigned i;
3512
Lars Ellenberga910b122014-04-28 18:43:21 +02003513 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003514 if (!adm_ctx.reply_skb)
3515 return retcode;
3516 if (retcode != NO_ERROR)
Lars Ellenberg9e276872014-04-28 18:43:22 +02003517 goto finish;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003518
Andreas Gruenbacherb6f85ef2011-07-06 15:03:31 +02003519 resource = adm_ctx.resource;
Lars Ellenberg9e276872014-04-28 18:43:22 +02003520 mutex_lock(&resource->adm_mutex);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003521 /* demote */
Andreas Gruenbacherb6f85ef2011-07-06 15:03:31 +02003522 for_each_connection(connection, resource) {
3523 struct drbd_peer_device *peer_device;
3524
3525 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
3526 retcode = drbd_set_role(peer_device->device, R_SECONDARY, 0);
3527 if (retcode < SS_SUCCESS) {
Lars Ellenberga910b122014-04-28 18:43:21 +02003528 drbd_msg_put_info(adm_ctx.reply_skb, "failed to demote");
Andreas Gruenbacherb6f85ef2011-07-06 15:03:31 +02003529 goto out;
3530 }
3531 }
3532
3533 retcode = conn_try_disconnect(connection, 0);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003534 if (retcode < SS_SUCCESS) {
Lars Ellenberga910b122014-04-28 18:43:21 +02003535 drbd_msg_put_info(adm_ctx.reply_skb, "failed to disconnect");
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02003536 goto out;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003537 }
3538 }
3539
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003540 /* detach */
Andreas Gruenbacherb6f85ef2011-07-06 15:03:31 +02003541 idr_for_each_entry(&resource->devices, device, i) {
3542 retcode = adm_detach(device, 0);
Lars Ellenberg27012382012-07-24 10:13:55 +02003543 if (retcode < SS_SUCCESS || retcode > NO_ERROR) {
Lars Ellenberga910b122014-04-28 18:43:21 +02003544 drbd_msg_put_info(adm_ctx.reply_skb, "failed to detach");
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02003545 goto out;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003546 }
3547 }
3548
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02003549 /* If we reach this, all volumes (of this connection) are Secondary,
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02003550 * Disconnected, Diskless, aka Unconfigured. Make sure all threads have
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02003551 * actually stopped, state handling only does drbd_thread_stop_nowait(). */
Andreas Gruenbacherb6f85ef2011-07-06 15:03:31 +02003552 for_each_connection(connection, resource)
3553 drbd_thread_stop(&connection->worker);
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02003554
3555 /* Now, nothing can fail anymore */
3556
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003557 /* delete volumes */
Andreas Gruenbacherb6f85ef2011-07-06 15:03:31 +02003558 idr_for_each_entry(&resource->devices, device, i) {
3559 retcode = adm_del_minor(device);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003560 if (retcode != NO_ERROR) {
3561 /* "can not happen" */
Lars Ellenberga910b122014-04-28 18:43:21 +02003562 drbd_msg_put_info(adm_ctx.reply_skb, "failed to delete volume");
Philipp Reisneref356262011-04-13 14:21:29 -07003563 goto out;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01003564 }
3565 }
3566
Andreas Gruenbacherb6f85ef2011-07-06 15:03:31 +02003567 list_del_rcu(&resource->resources);
3568 synchronize_rcu();
3569 drbd_free_resource(resource);
3570 retcode = NO_ERROR;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003571out:
Lars Ellenberg9e276872014-04-28 18:43:22 +02003572 mutex_unlock(&resource->adm_mutex);
3573finish:
Lars Ellenberga910b122014-04-28 18:43:21 +02003574 drbd_adm_finish(&adm_ctx, info, retcode);
Philipp Reisner774b3052011-02-22 02:07:03 -05003575 return 0;
3576}
3577
Andreas Gruenbacher789c1b62011-06-06 16:16:44 +02003578int drbd_adm_del_resource(struct sk_buff *skb, struct genl_info *info)
Philipp Reisner774b3052011-02-22 02:07:03 -05003579{
Lars Ellenberga910b122014-04-28 18:43:21 +02003580 struct drbd_config_context adm_ctx;
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003581 struct drbd_resource *resource;
3582 struct drbd_connection *connection;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003583 enum drbd_ret_code retcode;
3584
Lars Ellenberga910b122014-04-28 18:43:21 +02003585 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003586 if (!adm_ctx.reply_skb)
3587 return retcode;
3588 if (retcode != NO_ERROR)
Lars Ellenberg9e276872014-04-28 18:43:22 +02003589 goto finish;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003590
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003591 resource = adm_ctx.resource;
Lars Ellenberg9e276872014-04-28 18:43:22 +02003592 mutex_lock(&resource->adm_mutex);
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003593 for_each_connection(connection, resource) {
3594 if (connection->cstate > C_STANDALONE) {
3595 retcode = ERR_NET_CONFIGURED;
3596 goto out;
3597 }
3598 }
3599 if (!idr_is_empty(&resource->devices)) {
Andreas Gruenbacher789c1b62011-06-06 16:16:44 +02003600 retcode = ERR_RES_IN_USE;
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003601 goto out;
Philipp Reisner774b3052011-02-22 02:07:03 -05003602 }
3603
Andreas Gruenbacher77c556f2011-06-08 22:17:38 +02003604 list_del_rcu(&resource->resources);
3605 for_each_connection(connection, resource)
3606 drbd_thread_stop(&connection->worker);
3607 synchronize_rcu();
3608 drbd_free_resource(resource);
3609 retcode = NO_ERROR;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003610out:
Lars Ellenberg9e276872014-04-28 18:43:22 +02003611 mutex_unlock(&resource->adm_mutex);
3612finish:
Lars Ellenberga910b122014-04-28 18:43:21 +02003613 drbd_adm_finish(&adm_ctx, info, retcode);
Philipp Reisner774b3052011-02-22 02:07:03 -05003614 return 0;
3615}
3616
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003617void drbd_bcast_event(struct drbd_device *device, const struct sib_info *sib)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003618{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003619 static atomic_t drbd_genl_seq = ATOMIC_INIT(2); /* two. */
3620 struct sk_buff *msg;
3621 struct drbd_genlmsghdr *d_out;
3622 unsigned seq;
3623 int err = -ENOMEM;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003624
Philipp Reisneref86b772012-12-06 10:34:34 +01003625 if (sib->sib_reason == SIB_SYNC_PROGRESS) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003626 if (time_after(jiffies, device->rs_last_bcast + HZ))
3627 device->rs_last_bcast = jiffies;
Philipp Reisneref86b772012-12-06 10:34:34 +01003628 else
3629 return;
3630 }
Philipp Reisner328e0f12012-10-19 14:37:47 +02003631
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003632 seq = atomic_inc_return(&drbd_genl_seq);
3633 msg = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
3634 if (!msg)
3635 goto failed;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003636
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003637 err = -EMSGSIZE;
3638 d_out = genlmsg_put(msg, 0, seq, &drbd_genl_family, 0, DRBD_EVENT);
3639 if (!d_out) /* cannot happen, but anyways. */
3640 goto nla_put_failure;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003641 d_out->minor = device_to_minor(device);
Andreas Gruenbacher6f9b5f82011-05-06 01:03:32 +02003642 d_out->ret_code = NO_ERROR;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003643
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02003644 if (nla_put_status_info(msg, device, sib))
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003645 goto nla_put_failure;
3646 genlmsg_end(msg, d_out);
3647 err = drbd_genl_multicast_events(msg, 0);
3648 /* msg has been consumed or freed in netlink_broadcast() */
3649 if (err && err != -ESRCH)
3650 goto failed;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003651
Philipp Reisnerb411b362009-09-25 16:07:19 -07003652 return;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003653
3654nla_put_failure:
3655 nlmsg_free(msg);
3656failed:
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02003657 drbd_err(device, "Error %d while broadcasting event. "
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01003658 "Event seq:%u sib_reason:%u\n",
3659 err, seq, sib->sib_reason);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003660}