Merge branch 'x86-pti-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-block.git] / drivers / block / drbd / drbd_nl.c
CommitLineData
c6ae4c04 1// SPDX-License-Identifier: GPL-2.0-or-later
b411b363
PR
2/*
3 drbd_nl.c
4
5 This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
6
7 Copyright (C) 2001-2008, LINBIT Information Technologies GmbH.
8 Copyright (C) 1999-2008, Philipp Reisner <philipp.reisner@linbit.com>.
9 Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
10
b411b363
PR
11
12 */
13
f88c5d90
LE
14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
b411b363
PR
16#include <linux/module.h>
17#include <linux/drbd.h>
18#include <linux/in.h>
19#include <linux/fs.h>
20#include <linux/file.h>
21#include <linux/slab.h>
b411b363
PR
22#include <linux/blkpg.h>
23#include <linux/cpumask.h>
24#include "drbd_int.h"
a3603a6e 25#include "drbd_protocol.h"
265be2d0 26#include "drbd_req.h"
a2972846 27#include "drbd_state_change.h"
b411b363 28#include <asm/unaligned.h>
b411b363 29#include <linux/drbd_limits.h>
87f7be4c 30#include <linux/kthread.h>
b411b363 31
3b98c0c2
LE
32#include <net/genetlink.h>
33
34/* .doit */
35// int drbd_adm_create_resource(struct sk_buff *skb, struct genl_info *info);
36// int drbd_adm_delete_resource(struct sk_buff *skb, struct genl_info *info);
37
05a10ec7
AG
38int drbd_adm_new_minor(struct sk_buff *skb, struct genl_info *info);
39int drbd_adm_del_minor(struct sk_buff *skb, struct genl_info *info);
3b98c0c2 40
789c1b62
AG
41int drbd_adm_new_resource(struct sk_buff *skb, struct genl_info *info);
42int drbd_adm_del_resource(struct sk_buff *skb, struct genl_info *info);
85f75dd7 43int drbd_adm_down(struct sk_buff *skb, struct genl_info *info);
3b98c0c2
LE
44
45int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info);
46int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info);
f399002e 47int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info);
3b98c0c2
LE
48int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info);
49int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info);
f399002e 50int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info);
3b98c0c2
LE
51int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info);
52int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info);
53int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info);
54int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info);
55int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info);
56int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info);
57int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info);
58int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info);
59int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info);
60int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info);
61int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info);
f399002e 62int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info);
3b98c0c2
LE
63int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info);
64int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info);
65/* .dumpit */
66int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb);
a55bbd37
AG
67int drbd_adm_dump_resources(struct sk_buff *skb, struct netlink_callback *cb);
68int drbd_adm_dump_devices(struct sk_buff *skb, struct netlink_callback *cb);
69int drbd_adm_dump_devices_done(struct netlink_callback *cb);
70int drbd_adm_dump_connections(struct sk_buff *skb, struct netlink_callback *cb);
71int drbd_adm_dump_connections_done(struct netlink_callback *cb);
72int drbd_adm_dump_peer_devices(struct sk_buff *skb, struct netlink_callback *cb);
73int drbd_adm_dump_peer_devices_done(struct netlink_callback *cb);
a2972846 74int drbd_adm_get_initial_state(struct sk_buff *skb, struct netlink_callback *cb);
3b98c0c2
LE
75
76#include <linux/drbd_genl_api.h>
01b39b50 77#include "drbd_nla.h"
3b98c0c2
LE
78#include <linux/genl_magic_func.h>
79
a2972846
AG
80static atomic_t drbd_genl_seq = ATOMIC_INIT(2); /* two. */
81static atomic_t notify_genl_seq = ATOMIC_INIT(2); /* two. */
82
83DEFINE_MUTEX(notification_mutex);
84
3b98c0c2 85/* used blkdev_get_by_path, to claim our meta data device(s) */
b411b363
PR
86static char *drbd_m_holder = "Hands off! this is DRBD's meta data device.";
87
3b98c0c2
LE
88static void drbd_adm_send_reply(struct sk_buff *skb, struct genl_info *info)
89{
90 genlmsg_end(skb, genlmsg_data(nlmsg_data(nlmsg_hdr(skb))));
91 if (genlmsg_reply(skb, info))
f88c5d90 92 pr_err("error sending genl reply\n");
b411b363 93}
3b98c0c2
LE
94
95/* Used on a fresh "drbd_adm_prepare"d reply_skb, this cannot fail: The only
96 * reason it could fail was no space in skb, and there are 4k available. */
f221f4bc 97static int drbd_msg_put_info(struct sk_buff *skb, const char *info)
3b98c0c2 98{
3b98c0c2
LE
99 struct nlattr *nla;
100 int err = -EMSGSIZE;
101
102 if (!info || !info[0])
103 return 0;
104
ae0be8de 105 nla = nla_nest_start_noflag(skb, DRBD_NLA_CFG_REPLY);
3b98c0c2
LE
106 if (!nla)
107 return err;
108
109 err = nla_put_string(skb, T_info_text, info);
110 if (err) {
111 nla_nest_cancel(skb, nla);
112 return err;
113 } else
114 nla_nest_end(skb, nla);
115 return 0;
b411b363
PR
116}
117
4ef2a4f4
LE
118__printf(2, 3)
119static int drbd_msg_sprintf_info(struct sk_buff *skb, const char *fmt, ...)
120{
121 va_list args;
122 struct nlattr *nla, *txt;
123 int err = -EMSGSIZE;
124 int len;
125
ae0be8de 126 nla = nla_nest_start_noflag(skb, DRBD_NLA_CFG_REPLY);
4ef2a4f4
LE
127 if (!nla)
128 return err;
129
130 txt = nla_reserve(skb, T_info_text, 256);
131 if (!txt) {
132 nla_nest_cancel(skb, nla);
133 return err;
134 }
135 va_start(args, fmt);
136 len = vscnprintf(nla_data(txt), 256, fmt, args);
137 va_end(args);
138
139 /* maybe: retry with larger reserve, if truncated */
140 txt->nla_len = nla_attr_size(len+1);
141 nlmsg_trim(skb, (char*)txt + NLA_ALIGN(txt->nla_len));
142 nla_nest_end(skb, nla);
143
144 return 0;
145}
146
3b98c0c2
LE
147/* This would be a good candidate for a "pre_doit" hook,
148 * and per-family private info->pointers.
149 * But we need to stay compatible with older kernels.
150 * If it returns successfully, adm_ctx members are valid.
9e276872
LE
151 *
152 * At this point, we still rely on the global genl_lock().
153 * If we want to avoid that, and allow "genl_family.parallel_ops", we may need
154 * to add additional synchronization against object destruction/modification.
3b98c0c2
LE
155 */
156#define DRBD_ADM_NEED_MINOR 1
44e52cfa 157#define DRBD_ADM_NEED_RESOURCE 2
089c075d 158#define DRBD_ADM_NEED_CONNECTION 4
a910b123
LE
159static int drbd_adm_prepare(struct drbd_config_context *adm_ctx,
160 struct sk_buff *skb, struct genl_info *info, unsigned flags)
3b98c0c2
LE
161{
162 struct drbd_genlmsghdr *d_in = info->userhdr;
163 const u8 cmd = info->genlhdr->cmd;
164 int err;
165
a910b123 166 memset(adm_ctx, 0, sizeof(*adm_ctx));
3b98c0c2
LE
167
168 /* genl_rcv_msg only checks for CAP_NET_ADMIN on "GENL_ADMIN_PERM" :( */
98683650 169 if (cmd != DRBD_ADM_GET_STATUS && !capable(CAP_NET_ADMIN))
3b98c0c2
LE
170 return -EPERM;
171
a910b123
LE
172 adm_ctx->reply_skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
173 if (!adm_ctx->reply_skb) {
1e2a2551 174 err = -ENOMEM;
3b98c0c2 175 goto fail;
1e2a2551 176 }
3b98c0c2 177
a910b123 178 adm_ctx->reply_dh = genlmsg_put_reply(adm_ctx->reply_skb,
3b98c0c2
LE
179 info, &drbd_genl_family, 0, cmd);
180 /* put of a few bytes into a fresh skb of >= 4k will always succeed.
181 * but anyways */
a910b123 182 if (!adm_ctx->reply_dh) {
1e2a2551 183 err = -ENOMEM;
3b98c0c2 184 goto fail;
1e2a2551 185 }
3b98c0c2 186
a910b123
LE
187 adm_ctx->reply_dh->minor = d_in->minor;
188 adm_ctx->reply_dh->ret_code = NO_ERROR;
3b98c0c2 189
a910b123 190 adm_ctx->volume = VOLUME_UNSPECIFIED;
3b98c0c2
LE
191 if (info->attrs[DRBD_NLA_CFG_CONTEXT]) {
192 struct nlattr *nla;
193 /* parse and validate only */
f399002e 194 err = drbd_cfg_context_from_attrs(NULL, info);
3b98c0c2
LE
195 if (err)
196 goto fail;
197
198 /* It was present, and valid,
199 * copy it over to the reply skb. */
a910b123 200 err = nla_put_nohdr(adm_ctx->reply_skb,
3b98c0c2
LE
201 info->attrs[DRBD_NLA_CFG_CONTEXT]->nla_len,
202 info->attrs[DRBD_NLA_CFG_CONTEXT]);
203 if (err)
204 goto fail;
205
9e276872 206 /* and assign stuff to the adm_ctx */
3b98c0c2 207 nla = nested_attr_tb[__nla_type(T_ctx_volume)];
089c075d 208 if (nla)
a910b123 209 adm_ctx->volume = nla_get_u32(nla);
7c3063cc 210 nla = nested_attr_tb[__nla_type(T_ctx_resource_name)];
3b98c0c2 211 if (nla)
a910b123
LE
212 adm_ctx->resource_name = nla_data(nla);
213 adm_ctx->my_addr = nested_attr_tb[__nla_type(T_ctx_my_addr)];
214 adm_ctx->peer_addr = nested_attr_tb[__nla_type(T_ctx_peer_addr)];
215 if ((adm_ctx->my_addr &&
216 nla_len(adm_ctx->my_addr) > sizeof(adm_ctx->connection->my_addr)) ||
217 (adm_ctx->peer_addr &&
218 nla_len(adm_ctx->peer_addr) > sizeof(adm_ctx->connection->peer_addr))) {
089c075d
AG
219 err = -EINVAL;
220 goto fail;
221 }
222 }
3b98c0c2 223
a910b123
LE
224 adm_ctx->minor = d_in->minor;
225 adm_ctx->device = minor_to_device(d_in->minor);
9e276872
LE
226
227 /* We are protected by the global genl_lock().
228 * But we may explicitly drop it/retake it in drbd_adm_set_role(),
229 * so make sure this object stays around. */
230 if (adm_ctx->device)
231 kref_get(&adm_ctx->device->kref);
232
a910b123
LE
233 if (adm_ctx->resource_name) {
234 adm_ctx->resource = drbd_find_resource(adm_ctx->resource_name);
4bc76048 235 }
3b98c0c2 236
a910b123
LE
237 if (!adm_ctx->device && (flags & DRBD_ADM_NEED_MINOR)) {
238 drbd_msg_put_info(adm_ctx->reply_skb, "unknown minor");
3b98c0c2
LE
239 return ERR_MINOR_INVALID;
240 }
a910b123
LE
241 if (!adm_ctx->resource && (flags & DRBD_ADM_NEED_RESOURCE)) {
242 drbd_msg_put_info(adm_ctx->reply_skb, "unknown resource");
243 if (adm_ctx->resource_name)
a10f6b8a 244 return ERR_RES_NOT_KNOWN;
3b98c0c2
LE
245 return ERR_INVALID_REQUEST;
246 }
247
089c075d 248 if (flags & DRBD_ADM_NEED_CONNECTION) {
a910b123
LE
249 if (adm_ctx->resource) {
250 drbd_msg_put_info(adm_ctx->reply_skb, "no resource name expected");
089c075d
AG
251 return ERR_INVALID_REQUEST;
252 }
a910b123
LE
253 if (adm_ctx->device) {
254 drbd_msg_put_info(adm_ctx->reply_skb, "no minor number expected");
089c075d
AG
255 return ERR_INVALID_REQUEST;
256 }
a910b123
LE
257 if (adm_ctx->my_addr && adm_ctx->peer_addr)
258 adm_ctx->connection = conn_get_by_addrs(nla_data(adm_ctx->my_addr),
259 nla_len(adm_ctx->my_addr),
260 nla_data(adm_ctx->peer_addr),
261 nla_len(adm_ctx->peer_addr));
262 if (!adm_ctx->connection) {
263 drbd_msg_put_info(adm_ctx->reply_skb, "unknown connection");
089c075d
AG
264 return ERR_INVALID_REQUEST;
265 }
266 }
267
3b98c0c2 268 /* some more paranoia, if the request was over-determined */
a910b123
LE
269 if (adm_ctx->device && adm_ctx->resource &&
270 adm_ctx->device->resource != adm_ctx->resource) {
afa69539
KW
271 pr_warn("request: minor=%u, resource=%s; but that minor belongs to resource %s\n",
272 adm_ctx->minor, adm_ctx->resource->name,
273 adm_ctx->device->resource->name);
a910b123 274 drbd_msg_put_info(adm_ctx->reply_skb, "minor exists in different resource");
527f4b24
LE
275 return ERR_INVALID_REQUEST;
276 }
a910b123
LE
277 if (adm_ctx->device &&
278 adm_ctx->volume != VOLUME_UNSPECIFIED &&
279 adm_ctx->volume != adm_ctx->device->vnr) {
afa69539
KW
280 pr_warn("request: minor=%u, volume=%u; but that minor is volume %u in %s\n",
281 adm_ctx->minor, adm_ctx->volume,
282 adm_ctx->device->vnr, adm_ctx->device->resource->name);
a910b123 283 drbd_msg_put_info(adm_ctx->reply_skb, "minor exists as different volume");
3b98c0c2
LE
284 return ERR_INVALID_REQUEST;
285 }
0ace9dfa 286
9e276872
LE
287 /* still, provide adm_ctx->resource always, if possible. */
288 if (!adm_ctx->resource) {
289 adm_ctx->resource = adm_ctx->device ? adm_ctx->device->resource
290 : adm_ctx->connection ? adm_ctx->connection->resource : NULL;
291 if (adm_ctx->resource)
292 kref_get(&adm_ctx->resource->kref);
293 }
294
3b98c0c2
LE
295 return NO_ERROR;
296
297fail:
a910b123
LE
298 nlmsg_free(adm_ctx->reply_skb);
299 adm_ctx->reply_skb = NULL;
1e2a2551 300 return err;
3b98c0c2
LE
301}
302
a910b123
LE
303static int drbd_adm_finish(struct drbd_config_context *adm_ctx,
304 struct genl_info *info, int retcode)
3b98c0c2 305{
9e276872
LE
306 if (adm_ctx->device) {
307 kref_put(&adm_ctx->device->kref, drbd_destroy_device);
308 adm_ctx->device = NULL;
309 }
a910b123
LE
310 if (adm_ctx->connection) {
311 kref_put(&adm_ctx->connection->kref, &drbd_destroy_connection);
312 adm_ctx->connection = NULL;
0ace9dfa 313 }
a910b123
LE
314 if (adm_ctx->resource) {
315 kref_put(&adm_ctx->resource->kref, drbd_destroy_resource);
316 adm_ctx->resource = NULL;
4bc76048 317 }
0ace9dfa 318
a910b123 319 if (!adm_ctx->reply_skb)
3b98c0c2
LE
320 return -ENOMEM;
321
a910b123
LE
322 adm_ctx->reply_dh->ret_code = retcode;
323 drbd_adm_send_reply(adm_ctx->reply_skb, info);
3b98c0c2
LE
324 return 0;
325}
b411b363 326
bde89a9e 327static void setup_khelper_env(struct drbd_connection *connection, char **envp)
b411b363 328{
6b75dced 329 char *afs;
b411b363 330
089c075d 331 /* FIXME: A future version will not allow this case. */
bde89a9e 332 if (connection->my_addr_len == 0 || connection->peer_addr_len == 0)
089c075d
AG
333 return;
334
bde89a9e 335 switch (((struct sockaddr *)&connection->peer_addr)->sa_family) {
089c075d
AG
336 case AF_INET6:
337 afs = "ipv6";
338 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI6",
bde89a9e 339 &((struct sockaddr_in6 *)&connection->peer_addr)->sin6_addr);
b411b363 340 break;
089c075d
AG
341 case AF_INET:
342 afs = "ipv4";
343 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
bde89a9e 344 &((struct sockaddr_in *)&connection->peer_addr)->sin_addr);
b411b363 345 break;
089c075d
AG
346 default:
347 afs = "ssocks";
348 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
bde89a9e 349 &((struct sockaddr_in *)&connection->peer_addr)->sin_addr);
b411b363 350 }
089c075d 351 snprintf(envp[3], 20, "DRBD_PEER_AF=%s", afs);
6b75dced 352}
b411b363 353
b30ab791 354int drbd_khelper(struct drbd_device *device, char *cmd)
b411b363
PR
355{
356 char *envp[] = { "HOME=/",
357 "TERM=linux",
358 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
6b75dced
PR
359 (char[20]) { }, /* address family */
360 (char[60]) { }, /* address */
b411b363 361 NULL };
0982368b 362 char mb[14];
8ab761e1 363 char *argv[] = {drbd_usermode_helper, cmd, mb, NULL };
a6b32bc3 364 struct drbd_connection *connection = first_peer_device(device)->connection;
6b75dced 365 struct sib_info sib;
b411b363
PR
366 int ret;
367
bde89a9e
AG
368 if (current == connection->worker.task)
369 set_bit(CALLBACK_PENDING, &connection->flags);
c2ba686f 370
0982368b 371 snprintf(mb, 14, "minor-%d", device_to_minor(device));
bde89a9e 372 setup_khelper_env(connection, envp);
b411b363 373
1090c056
LE
374 /* The helper may take some time.
375 * write out any unsynced meta data changes now */
b30ab791 376 drbd_md_sync(device);
1090c056 377
8ab761e1 378 drbd_info(device, "helper command: %s %s %s\n", drbd_usermode_helper, cmd, mb);
3b98c0c2
LE
379 sib.sib_reason = SIB_HELPER_PRE;
380 sib.helper_name = cmd;
b30ab791 381 drbd_bcast_event(device, &sib);
a2972846 382 notify_helper(NOTIFY_CALL, device, connection, cmd, 0);
8ab761e1 383 ret = call_usermodehelper(drbd_usermode_helper, argv, envp, UMH_WAIT_PROC);
b411b363 384 if (ret)
d0180171 385 drbd_warn(device, "helper command: %s %s %s exit code %u (0x%x)\n",
8ab761e1 386 drbd_usermode_helper, cmd, mb,
b411b363
PR
387 (ret >> 8) & 0xff, ret);
388 else
d0180171 389 drbd_info(device, "helper command: %s %s %s exit code %u (0x%x)\n",
8ab761e1 390 drbd_usermode_helper, cmd, mb,
b411b363 391 (ret >> 8) & 0xff, ret);
3b98c0c2
LE
392 sib.sib_reason = SIB_HELPER_POST;
393 sib.helper_exit_code = ret;
b30ab791 394 drbd_bcast_event(device, &sib);
a2972846 395 notify_helper(NOTIFY_RESPONSE, device, connection, cmd, ret);
b411b363 396
bde89a9e
AG
397 if (current == connection->worker.task)
398 clear_bit(CALLBACK_PENDING, &connection->flags);
b411b363
PR
399
400 if (ret < 0) /* Ignore any ERRNOs we got. */
401 ret = 0;
402
403 return ret;
404}
405
7e5fec31 406enum drbd_peer_state conn_khelper(struct drbd_connection *connection, char *cmd)
6b75dced
PR
407{
408 char *envp[] = { "HOME=/",
409 "TERM=linux",
410 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
411 (char[20]) { }, /* address family */
412 (char[60]) { }, /* address */
413 NULL };
77c556f6 414 char *resource_name = connection->resource->name;
8ab761e1 415 char *argv[] = {drbd_usermode_helper, cmd, resource_name, NULL };
6b75dced
PR
416 int ret;
417
bde89a9e
AG
418 setup_khelper_env(connection, envp);
419 conn_md_sync(connection);
6b75dced 420
8ab761e1 421 drbd_info(connection, "helper command: %s %s %s\n", drbd_usermode_helper, cmd, resource_name);
6b75dced 422 /* TODO: conn_bcast_event() ?? */
a2972846 423 notify_helper(NOTIFY_CALL, NULL, connection, cmd, 0);
6b75dced 424
8ab761e1 425 ret = call_usermodehelper(drbd_usermode_helper, argv, envp, UMH_WAIT_PROC);
6b75dced 426 if (ret)
1ec861eb 427 drbd_warn(connection, "helper command: %s %s %s exit code %u (0x%x)\n",
8ab761e1 428 drbd_usermode_helper, cmd, resource_name,
6b75dced
PR
429 (ret >> 8) & 0xff, ret);
430 else
1ec861eb 431 drbd_info(connection, "helper command: %s %s %s exit code %u (0x%x)\n",
8ab761e1 432 drbd_usermode_helper, cmd, resource_name,
6b75dced
PR
433 (ret >> 8) & 0xff, ret);
434 /* TODO: conn_bcast_event() ?? */
a2972846 435 notify_helper(NOTIFY_RESPONSE, NULL, connection, cmd, ret);
c2ba686f 436
b411b363
PR
437 if (ret < 0) /* Ignore any ERRNOs we got. */
438 ret = 0;
439
440 return ret;
441}
442
bde89a9e 443static enum drbd_fencing_p highest_fencing_policy(struct drbd_connection *connection)
b411b363 444{
cb703454 445 enum drbd_fencing_p fp = FP_NOT_AVAIL;
c06ece6b 446 struct drbd_peer_device *peer_device;
cb703454
PR
447 int vnr;
448
695d08fa 449 rcu_read_lock();
c06ece6b
AG
450 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
451 struct drbd_device *device = peer_device->device;
b30ab791 452 if (get_ldev_if_state(device, D_CONSISTENT)) {
c06ece6b
AG
453 struct disk_conf *disk_conf =
454 rcu_dereference(peer_device->device->ldev->disk_conf);
455 fp = max_t(enum drbd_fencing_p, fp, disk_conf->fencing);
b30ab791 456 put_ldev(device);
cb703454
PR
457 }
458 }
695d08fa 459 rcu_read_unlock();
cb703454
PR
460
461 return fp;
462}
463
7bd000cb
LE
464static bool resource_is_supended(struct drbd_resource *resource)
465{
466 return resource->susp || resource->susp_fen || resource->susp_nod;
467}
468
bde89a9e 469bool conn_try_outdate_peer(struct drbd_connection *connection)
b411b363 470{
7bd000cb 471 struct drbd_resource * const resource = connection->resource;
28e448bb 472 unsigned int connect_cnt;
cb703454
PR
473 union drbd_state mask = { };
474 union drbd_state val = { };
475 enum drbd_fencing_p fp;
b411b363
PR
476 char *ex_to_string;
477 int r;
b411b363 478
7bd000cb 479 spin_lock_irq(&resource->req_lock);
bde89a9e 480 if (connection->cstate >= C_WF_REPORT_PARAMS) {
1ec861eb 481 drbd_err(connection, "Expected cstate < C_WF_REPORT_PARAMS\n");
7bd000cb 482 spin_unlock_irq(&resource->req_lock);
cb703454
PR
483 return false;
484 }
b411b363 485
bde89a9e 486 connect_cnt = connection->connect_cnt;
7bd000cb 487 spin_unlock_irq(&resource->req_lock);
28e448bb 488
bde89a9e 489 fp = highest_fencing_policy(connection);
cb703454
PR
490 switch (fp) {
491 case FP_NOT_AVAIL:
1ec861eb 492 drbd_warn(connection, "Not fencing peer, I'm not even Consistent myself.\n");
7bd000cb
LE
493 spin_lock_irq(&resource->req_lock);
494 if (connection->cstate < C_WF_REPORT_PARAMS) {
495 _conn_request_state(connection,
496 (union drbd_state) { { .susp_fen = 1 } },
497 (union drbd_state) { { .susp_fen = 0 } },
498 CS_VERBOSE | CS_HARD | CS_DC_SUSP);
499 /* We are no longer suspended due to the fencing policy.
500 * We may still be suspended due to the on-no-data-accessible policy.
501 * If that was OND_IO_ERROR, fail pending requests. */
502 if (!resource_is_supended(resource))
503 _tl_restart(connection, CONNECTION_LOST_WHILE_PENDING);
504 }
505 /* Else: in case we raced with a connection handshake,
506 * let the handshake figure out if we maybe can RESEND,
507 * and do not resume/fail pending requests here.
508 * Worst case is we stay suspended for now, which may be
509 * resolved by either re-establishing the replication link, or
510 * the next link failure, or eventually the administrator. */
511 spin_unlock_irq(&resource->req_lock);
512 return false;
513
cb703454
PR
514 case FP_DONT_CARE:
515 return true;
516 default: ;
b411b363
PR
517 }
518
bde89a9e 519 r = conn_khelper(connection, "fence-peer");
b411b363
PR
520
521 switch ((r>>8) & 0xff) {
7e5fec31 522 case P_INCONSISTENT: /* peer is inconsistent */
b411b363 523 ex_to_string = "peer is inconsistent or worse";
cb703454
PR
524 mask.pdsk = D_MASK;
525 val.pdsk = D_INCONSISTENT;
b411b363 526 break;
7e5fec31 527 case P_OUTDATED: /* peer got outdated, or was already outdated */
b411b363 528 ex_to_string = "peer was fenced";
cb703454
PR
529 mask.pdsk = D_MASK;
530 val.pdsk = D_OUTDATED;
b411b363 531 break;
7e5fec31 532 case P_DOWN: /* peer was down */
bde89a9e 533 if (conn_highest_disk(connection) == D_UP_TO_DATE) {
b411b363
PR
534 /* we will(have) create(d) a new UUID anyways... */
535 ex_to_string = "peer is unreachable, assumed to be dead";
cb703454
PR
536 mask.pdsk = D_MASK;
537 val.pdsk = D_OUTDATED;
b411b363
PR
538 } else {
539 ex_to_string = "peer unreachable, doing nothing since disk != UpToDate";
b411b363
PR
540 }
541 break;
7e5fec31 542 case P_PRIMARY: /* Peer is primary, voluntarily outdate myself.
b411b363
PR
543 * This is useful when an unconnected R_SECONDARY is asked to
544 * become R_PRIMARY, but finds the other peer being active. */
545 ex_to_string = "peer is active";
1ec861eb 546 drbd_warn(connection, "Peer is primary, outdating myself.\n");
cb703454
PR
547 mask.disk = D_MASK;
548 val.disk = D_OUTDATED;
b411b363 549 break;
7e5fec31
FF
550 case P_FENCING:
551 /* THINK: do we need to handle this
552 * like case 4, or more like case 5? */
b411b363 553 if (fp != FP_STONITH)
1ec861eb 554 drbd_err(connection, "fence-peer() = 7 && fencing != Stonith !!!\n");
b411b363 555 ex_to_string = "peer was stonithed";
cb703454
PR
556 mask.pdsk = D_MASK;
557 val.pdsk = D_OUTDATED;
b411b363
PR
558 break;
559 default:
560 /* The script is broken ... */
1ec861eb 561 drbd_err(connection, "fence-peer helper broken, returned %d\n", (r>>8)&0xff);
cb703454 562 return false; /* Eventually leave IO frozen */
b411b363
PR
563 }
564
1ec861eb 565 drbd_info(connection, "fence-peer helper returned %d (%s)\n",
cb703454 566 (r>>8) & 0xff, ex_to_string);
fb22c402 567
cb703454 568 /* Not using
bde89a9e 569 conn_request_state(connection, mask, val, CS_VERBOSE);
cb703454
PR
570 here, because we might were able to re-establish the connection in the
571 meantime. */
7bd000cb 572 spin_lock_irq(&resource->req_lock);
bde89a9e
AG
573 if (connection->cstate < C_WF_REPORT_PARAMS && !test_bit(STATE_SENT, &connection->flags)) {
574 if (connection->connect_cnt != connect_cnt)
28e448bb
PR
575 /* In case the connection was established and droped
576 while the fence-peer handler was running, ignore it */
1ec861eb 577 drbd_info(connection, "Ignoring fence-peer exit code\n");
28e448bb 578 else
bde89a9e 579 _conn_request_state(connection, mask, val, CS_VERBOSE);
28e448bb 580 }
7bd000cb 581 spin_unlock_irq(&resource->req_lock);
cb703454 582
bde89a9e 583 return conn_highest_pdsk(connection) <= D_OUTDATED;
b411b363
PR
584}
585
87f7be4c
PR
586static int _try_outdate_peer_async(void *data)
587{
bde89a9e 588 struct drbd_connection *connection = (struct drbd_connection *)data;
87f7be4c 589
bde89a9e 590 conn_try_outdate_peer(connection);
87f7be4c 591
05a10ec7 592 kref_put(&connection->kref, drbd_destroy_connection);
87f7be4c
PR
593 return 0;
594}
595
bde89a9e 596void conn_try_outdate_peer_async(struct drbd_connection *connection)
87f7be4c
PR
597{
598 struct task_struct *opa;
599
bde89a9e 600 kref_get(&connection->kref);
fee10990 601 /* We may have just sent a signal to this thread
bbc1c5e8
LE
602 * to get it out of some blocking network function.
603 * Clear signals; otherwise kthread_run(), which internally uses
604 * wait_on_completion_killable(), will mistake our pending signal
605 * for a new fatal signal and fail. */
606 flush_signals(current);
bde89a9e 607 opa = kthread_run(_try_outdate_peer_async, connection, "drbd_async_h");
9dc9fbb3 608 if (IS_ERR(opa)) {
1ec861eb 609 drbd_err(connection, "out of mem, failed to invoke fence-peer helper\n");
05a10ec7 610 kref_put(&connection->kref, drbd_destroy_connection);
9dc9fbb3 611 }
87f7be4c 612}
b411b363 613
bf885f8a 614enum drbd_state_rv
44a4d551 615drbd_set_role(struct drbd_device *const device, enum drbd_role new_role, int force)
b411b363 616{
44a4d551
LE
617 struct drbd_peer_device *const peer_device = first_peer_device(device);
618 struct drbd_connection *const connection = peer_device ? peer_device->connection : NULL;
b411b363 619 const int max_tries = 4;
bf885f8a 620 enum drbd_state_rv rv = SS_UNKNOWN_ERROR;
44ed167d 621 struct net_conf *nc;
b411b363
PR
622 int try = 0;
623 int forced = 0;
624 union drbd_state mask, val;
b411b363 625
b6f85ef9
AG
626 if (new_role == R_PRIMARY) {
627 struct drbd_connection *connection;
628
629 /* Detect dead peers as soon as possible. */
630
631 rcu_read_lock();
632 for_each_connection(connection, device->resource)
633 request_ping(connection);
634 rcu_read_unlock();
635 }
b411b363 636
b30ab791 637 mutex_lock(device->state_mutex);
b411b363
PR
638
639 mask.i = 0; mask.role = R_MASK;
640 val.i = 0; val.role = new_role;
641
642 while (try++ < max_tries) {
a8821531 643 rv = _drbd_request_state_holding_state_mutex(device, mask, val, CS_WAIT_COMPLETE);
b411b363
PR
644
645 /* in case we first succeeded to outdate,
646 * but now suddenly could establish a connection */
bf885f8a 647 if (rv == SS_CW_FAILED_BY_PEER && mask.pdsk != 0) {
b411b363
PR
648 val.pdsk = 0;
649 mask.pdsk = 0;
650 continue;
651 }
652
bf885f8a 653 if (rv == SS_NO_UP_TO_DATE_DISK && force &&
b30ab791
AG
654 (device->state.disk < D_UP_TO_DATE &&
655 device->state.disk >= D_INCONSISTENT)) {
b411b363
PR
656 mask.disk = D_MASK;
657 val.disk = D_UP_TO_DATE;
658 forced = 1;
659 continue;
660 }
661
bf885f8a 662 if (rv == SS_NO_UP_TO_DATE_DISK &&
b30ab791 663 device->state.disk == D_CONSISTENT && mask.pdsk == 0) {
0b0ba1ef 664 D_ASSERT(device, device->state.pdsk == D_UNKNOWN);
b411b363 665
44a4d551 666 if (conn_try_outdate_peer(connection)) {
b411b363
PR
667 val.disk = D_UP_TO_DATE;
668 mask.disk = D_MASK;
669 }
b411b363
PR
670 continue;
671 }
672
bf885f8a 673 if (rv == SS_NOTHING_TO_DO)
3b98c0c2 674 goto out;
bf885f8a 675 if (rv == SS_PRIMARY_NOP && mask.pdsk == 0) {
44a4d551 676 if (!conn_try_outdate_peer(connection) && force) {
d0180171 677 drbd_warn(device, "Forced into split brain situation!\n");
cb703454
PR
678 mask.pdsk = D_MASK;
679 val.pdsk = D_OUTDATED;
b411b363 680
cb703454 681 }
b411b363
PR
682 continue;
683 }
bf885f8a 684 if (rv == SS_TWO_PRIMARIES) {
b411b363
PR
685 /* Maybe the peer is detected as dead very soon...
686 retry at most once more in this case. */
9848b6dd
LE
687 if (try < max_tries) {
688 int timeo;
b411b363 689 try = max_tries - 1;
9848b6dd
LE
690 rcu_read_lock();
691 nc = rcu_dereference(connection->net_conf);
692 timeo = nc ? (nc->ping_timeo + 1) * HZ / 10 : 1;
693 rcu_read_unlock();
694 schedule_timeout_interruptible(timeo);
695 }
b411b363
PR
696 continue;
697 }
bf885f8a 698 if (rv < SS_SUCCESS) {
b30ab791 699 rv = _drbd_request_state(device, mask, val,
b411b363 700 CS_VERBOSE + CS_WAIT_COMPLETE);
bf885f8a 701 if (rv < SS_SUCCESS)
3b98c0c2 702 goto out;
b411b363
PR
703 }
704 break;
705 }
706
bf885f8a 707 if (rv < SS_SUCCESS)
3b98c0c2 708 goto out;
b411b363
PR
709
710 if (forced)
d0180171 711 drbd_warn(device, "Forced to consider local data as UpToDate!\n");
b411b363
PR
712
713 /* Wait until nothing is on the fly :) */
b30ab791 714 wait_event(device->misc_wait, atomic_read(&device->ap_pending_cnt) == 0);
b411b363 715
b6dd1a89
LE
716 /* FIXME also wait for all pending P_BARRIER_ACK? */
717
b411b363 718 if (new_role == R_SECONDARY) {
b30ab791
AG
719 if (get_ldev(device)) {
720 device->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
721 put_ldev(device);
b411b363
PR
722 }
723 } else {
66ce6dbc 724 mutex_lock(&device->resource->conf_update);
44a4d551 725 nc = connection->net_conf;
44ed167d 726 if (nc)
6139f60d 727 nc->discard_my_data = 0; /* without copy; single bit op is atomic */
66ce6dbc 728 mutex_unlock(&device->resource->conf_update);
91fd4dad 729
b30ab791
AG
730 if (get_ldev(device)) {
731 if (((device->state.conn < C_CONNECTED ||
732 device->state.pdsk <= D_FAILED)
733 && device->ldev->md.uuid[UI_BITMAP] == 0) || forced)
734 drbd_uuid_new_current(device);
b411b363 735
b30ab791
AG
736 device->ldev->md.uuid[UI_CURRENT] |= (u64)1;
737 put_ldev(device);
b411b363
PR
738 }
739 }
740
19f843aa
LE
741 /* writeout of activity log covered areas of the bitmap
742 * to stable storage done in after state change already */
b411b363 743
b30ab791 744 if (device->state.conn >= C_WF_REPORT_PARAMS) {
b411b363
PR
745 /* if this was forced, we should consider sync */
746 if (forced)
44a4d551
LE
747 drbd_send_uuids(peer_device);
748 drbd_send_current_state(peer_device);
b411b363
PR
749 }
750
b30ab791 751 drbd_md_sync(device);
720979fb 752 set_disk_ro(device->vdisk, new_role == R_SECONDARY);
b30ab791 753 kobject_uevent(&disk_to_dev(device->vdisk)->kobj, KOBJ_CHANGE);
3b98c0c2 754out:
b30ab791 755 mutex_unlock(device->state_mutex);
bf885f8a 756 return rv;
b411b363
PR
757}
758
3b98c0c2 759static const char *from_attrs_err_to_txt(int err)
ef50a3e3 760{
3b98c0c2
LE
761 return err == -ENOMSG ? "required attribute missing" :
762 err == -EOPNOTSUPP ? "unknown mandatory attribute" :
f399002e 763 err == -EEXIST ? "can not change invariant setting" :
3b98c0c2 764 "invalid attribute value";
ef50a3e3 765}
b411b363 766
3b98c0c2 767int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info)
b411b363 768{
a910b123 769 struct drbd_config_context adm_ctx;
3b98c0c2
LE
770 struct set_role_parms parms;
771 int err;
772 enum drbd_ret_code retcode;
b411b363 773
a910b123 774 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3b98c0c2
LE
775 if (!adm_ctx.reply_skb)
776 return retcode;
777 if (retcode != NO_ERROR)
778 goto out;
b411b363 779
3b98c0c2
LE
780 memset(&parms, 0, sizeof(parms));
781 if (info->attrs[DRBD_NLA_SET_ROLE_PARMS]) {
f399002e 782 err = set_role_parms_from_attrs(&parms, info);
3b98c0c2
LE
783 if (err) {
784 retcode = ERR_MANDATORY_TAG;
a910b123 785 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
3b98c0c2
LE
786 goto out;
787 }
788 }
9e276872
LE
789 genl_unlock();
790 mutex_lock(&adm_ctx.resource->adm_mutex);
b411b363 791
3b98c0c2 792 if (info->genlhdr->cmd == DRBD_ADM_PRIMARY)
b30ab791 793 retcode = drbd_set_role(adm_ctx.device, R_PRIMARY, parms.assume_uptodate);
3b98c0c2 794 else
b30ab791 795 retcode = drbd_set_role(adm_ctx.device, R_SECONDARY, 0);
9e276872
LE
796
797 mutex_unlock(&adm_ctx.resource->adm_mutex);
798 genl_lock();
3b98c0c2 799out:
a910b123 800 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
801 return 0;
802}
803
ae8bf312
LE
804/* Initializes the md.*_offset members, so we are able to find
805 * the on disk meta data.
806 *
807 * We currently have two possible layouts:
808 * external:
809 * |----------- md_size_sect ------------------|
810 * [ 4k superblock ][ activity log ][ Bitmap ]
811 * | al_offset == 8 |
812 * | bm_offset = al_offset + X |
813 * ==> bitmap sectors = md_size_sect - bm_offset
814 *
815 * internal:
816 * |----------- md_size_sect ------------------|
817 * [data.....][ Bitmap ][ activity log ][ 4k superblock ]
818 * | al_offset < 0 |
819 * | bm_offset = al_offset - Y |
820 * ==> bitmap sectors = Y = al_offset - bm_offset
821 *
822 * Activity log size used to be fixed 32kB,
823 * but is about to become configurable.
824 */
b30ab791 825static void drbd_md_set_sector_offsets(struct drbd_device *device,
b411b363
PR
826 struct drbd_backing_dev *bdev)
827{
828 sector_t md_size_sect = 0;
c04ccaa6 829 unsigned int al_size_sect = bdev->md.al_size_4k * 8;
daeda1cc 830
3a4d4eb3
LE
831 bdev->md.md_offset = drbd_md_ss(bdev);
832
68e41a43 833 switch (bdev->md.meta_dev_idx) {
b411b363
PR
834 default:
835 /* v07 style fixed size indexed meta data */
ae8bf312 836 bdev->md.md_size_sect = MD_128MB_SECT;
ae8bf312
LE
837 bdev->md.al_offset = MD_4kB_SECT;
838 bdev->md.bm_offset = MD_4kB_SECT + al_size_sect;
b411b363
PR
839 break;
840 case DRBD_MD_INDEX_FLEX_EXT:
841 /* just occupy the full device; unit: sectors */
842 bdev->md.md_size_sect = drbd_get_capacity(bdev->md_bdev);
ae8bf312
LE
843 bdev->md.al_offset = MD_4kB_SECT;
844 bdev->md.bm_offset = MD_4kB_SECT + al_size_sect;
b411b363
PR
845 break;
846 case DRBD_MD_INDEX_INTERNAL:
847 case DRBD_MD_INDEX_FLEX_INT:
b411b363 848 /* al size is still fixed */
ae8bf312 849 bdev->md.al_offset = -al_size_sect;
b411b363
PR
850 /* we need (slightly less than) ~ this much bitmap sectors: */
851 md_size_sect = drbd_get_capacity(bdev->backing_bdev);
852 md_size_sect = ALIGN(md_size_sect, BM_SECT_PER_EXT);
853 md_size_sect = BM_SECT_TO_EXT(md_size_sect);
854 md_size_sect = ALIGN(md_size_sect, 8);
855
856 /* plus the "drbd meta data super block",
857 * and the activity log; */
ae8bf312 858 md_size_sect += MD_4kB_SECT + al_size_sect;
b411b363
PR
859
860 bdev->md.md_size_sect = md_size_sect;
861 /* bitmap offset is adjusted by 'super' block size */
ae8bf312 862 bdev->md.bm_offset = -md_size_sect + MD_4kB_SECT;
b411b363
PR
863 break;
864 }
865}
866
4b0715f0 867/* input size is expected to be in KB */
b411b363
PR
868char *ppsize(char *buf, unsigned long long size)
869{
4b0715f0
LE
870 /* Needs 9 bytes at max including trailing NUL:
871 * -1ULL ==> "16384 EB" */
b411b363
PR
872 static char units[] = { 'K', 'M', 'G', 'T', 'P', 'E' };
873 int base = 0;
4b0715f0 874 while (size >= 10000 && base < sizeof(units)-1) {
b411b363
PR
875 /* shift + round */
876 size = (size >> 10) + !!(size & (1<<9));
877 base++;
878 }
4b0715f0 879 sprintf(buf, "%u %cB", (unsigned)size, units[base]);
b411b363
PR
880
881 return buf;
882}
883
884/* there is still a theoretical deadlock when called from receiver
885 * on an D_INCONSISTENT R_PRIMARY:
886 * remote READ does inc_ap_bio, receiver would need to receive answer
887 * packet from remote to dec_ap_bio again.
888 * receiver receive_sizes(), comes here,
889 * waits for ap_bio_cnt == 0. -> deadlock.
890 * but this cannot happen, actually, because:
891 * R_PRIMARY D_INCONSISTENT, and peer's disk is unreachable
892 * (not connected, or bad/no disk on peer):
893 * see drbd_fail_request_early, ap_bio_cnt is zero.
894 * R_PRIMARY D_INCONSISTENT, and C_SYNC_TARGET:
895 * peer may not initiate a resize.
896 */
3b98c0c2
LE
897/* Note these are not to be confused with
898 * drbd_adm_suspend_io/drbd_adm_resume_io,
899 * which are (sub) state changes triggered by admin (drbdsetup),
900 * and can be long lived.
b30ab791 901 * This changes an device->flag, is triggered by drbd internals,
3b98c0c2 902 * and should be short-lived. */
7dbb4386
PR
903/* It needs to be a counter, since multiple threads might
904 independently suspend and resume IO. */
b30ab791 905void drbd_suspend_io(struct drbd_device *device)
b411b363 906{
7dbb4386 907 atomic_inc(&device->suspend_cnt);
b30ab791 908 if (drbd_suspended(device))
265be2d0 909 return;
b30ab791 910 wait_event(device->misc_wait, !atomic_read(&device->ap_bio_cnt));
b411b363
PR
911}
912
b30ab791 913void drbd_resume_io(struct drbd_device *device)
b411b363 914{
7dbb4386
PR
915 if (atomic_dec_and_test(&device->suspend_cnt))
916 wake_up(&device->misc_wait);
b411b363
PR
917}
918
919/**
920 * drbd_determine_dev_size() - Sets the right device size obeying all constraints
b30ab791 921 * @device: DRBD device.
b411b363
PR
922 *
923 * Returns 0 on success, negative return values indicate errors.
924 * You should call drbd_md_sync() after calling this function.
925 */
d752b269 926enum determine_dev_size
b30ab791 927drbd_determine_dev_size(struct drbd_device *device, enum dds_flags flags, struct resize_parms *rs) __must_hold(local)
b411b363 928{
8011e249
LE
929 struct md_offsets_and_sizes {
930 u64 last_agreed_sect;
931 u64 md_offset;
932 s32 al_offset;
933 s32 bm_offset;
934 u32 md_size_sect;
935
936 u32 al_stripes;
937 u32 al_stripe_size_4k;
938 } prev;
939 sector_t u_size, size;
b30ab791 940 struct drbd_md *md = &device->ldev->md;
d752b269 941 void *buffer;
b411b363
PR
942
943 int md_moved, la_size_changed;
e96c9633 944 enum determine_dev_size rv = DS_UNCHANGED;
b411b363 945
5f7c0124
LE
946 /* We may change the on-disk offsets of our meta data below. Lock out
947 * anything that may cause meta data IO, to avoid acting on incomplete
948 * layout changes or scribbling over meta data that is in the process
949 * of being moved.
b411b363 950 *
5f7c0124
LE
951 * Move is not exactly correct, btw, currently we have all our meta
952 * data in core memory, to "move" it we just write it all out, there
953 * are no reads. */
b30ab791 954 drbd_suspend_io(device);
e37d2438 955 buffer = drbd_md_get_buffer(device, __func__); /* Lock meta-data IO */
d752b269 956 if (!buffer) {
b30ab791 957 drbd_resume_io(device);
d752b269
PR
958 return DS_ERROR;
959 }
b411b363 960
8011e249
LE
961 /* remember current offset and sizes */
962 prev.last_agreed_sect = md->la_size_sect;
963 prev.md_offset = md->md_offset;
964 prev.al_offset = md->al_offset;
965 prev.bm_offset = md->bm_offset;
966 prev.md_size_sect = md->md_size_sect;
967 prev.al_stripes = md->al_stripes;
968 prev.al_stripe_size_4k = md->al_stripe_size_4k;
b411b363 969
d752b269
PR
970 if (rs) {
971 /* rs is non NULL if we should change the AL layout only */
d752b269
PR
972 md->al_stripes = rs->al_stripes;
973 md->al_stripe_size_4k = rs->al_stripe_size / 4;
974 md->al_size_4k = (u64)rs->al_stripes * rs->al_stripe_size / 4;
975 }
976
b30ab791 977 drbd_md_set_sector_offsets(device, device->ldev);
b411b363 978
daeda1cc 979 rcu_read_lock();
b30ab791 980 u_size = rcu_dereference(device->ldev->disk_conf)->disk_size;
daeda1cc 981 rcu_read_unlock();
b30ab791 982 size = drbd_new_dev_size(device, device->ldev, u_size, flags & DDSF_FORCED);
b411b363 983
8011e249 984 if (size < prev.last_agreed_sect) {
d752b269
PR
985 if (rs && u_size == 0) {
986 /* Remove "rs &&" later. This check should always be active, but
987 right now the receiver expects the permissive behavior */
d0180171 988 drbd_warn(device, "Implicit shrink not allowed. "
d752b269
PR
989 "Use --size=%llus for explicit shrink.\n",
990 (unsigned long long)size);
991 rv = DS_ERROR_SHRINK;
992 }
993 if (u_size > size)
994 rv = DS_ERROR_SPACE_MD;
995 if (rv != DS_UNCHANGED)
996 goto err_out;
997 }
998
b30ab791
AG
999 if (drbd_get_capacity(device->this_bdev) != size ||
1000 drbd_bm_capacity(device) != size) {
b411b363 1001 int err;
b30ab791 1002 err = drbd_bm_resize(device, size, !(flags & DDSF_NO_RESYNC));
b411b363
PR
1003 if (unlikely(err)) {
1004 /* currently there is only one error: ENOMEM! */
8011e249 1005 size = drbd_bm_capacity(device);
b411b363 1006 if (size == 0) {
d0180171 1007 drbd_err(device, "OUT OF MEMORY! "
b411b363
PR
1008 "Could not allocate bitmap!\n");
1009 } else {
d0180171 1010 drbd_err(device, "BM resizing failed. "
8011e249 1011 "Leaving size unchanged\n");
b411b363 1012 }
e96c9633 1013 rv = DS_ERROR;
b411b363
PR
1014 }
1015 /* racy, see comments above. */
b30ab791 1016 drbd_set_my_capacity(device, size);
8011e249 1017 md->la_size_sect = size;
b411b363 1018 }
d752b269
PR
1019 if (rv <= DS_ERROR)
1020 goto err_out;
b411b363 1021
8011e249 1022 la_size_changed = (prev.last_agreed_sect != md->la_size_sect);
b411b363 1023
8011e249
LE
1024 md_moved = prev.md_offset != md->md_offset
1025 || prev.md_size_sect != md->md_size_sect;
b411b363 1026
d752b269
PR
1027 if (la_size_changed || md_moved || rs) {
1028 u32 prev_flags;
24dccabb 1029
fcb09674
LE
1030 /* We do some synchronous IO below, which may take some time.
1031 * Clear the timer, to avoid scary "timer expired!" messages,
1032 * "Superblock" is written out at least twice below, anyways. */
1033 del_timer(&device->md_sync_timer);
d752b269 1034
5f7c0124
LE
1035 /* We won't change the "al-extents" setting, we just may need
1036 * to move the on-disk location of the activity log ringbuffer.
1037 * Lock for transaction is good enough, it may well be "dirty"
1038 * or even "starving". */
1039 wait_event(device->al_wait, lc_try_lock_for_transaction(device->act_log));
1040
1041 /* mark current on-disk bitmap and activity log as unreliable */
d752b269 1042 prev_flags = md->flags;
5f7c0124 1043 md->flags |= MDF_FULL_SYNC | MDF_AL_DISABLED;
b30ab791 1044 drbd_md_write(device, buffer);
d752b269 1045
5f7c0124
LE
1046 drbd_al_initialize(device, buffer);
1047
d0180171 1048 drbd_info(device, "Writing the whole bitmap, %s\n",
b411b363
PR
1049 la_size_changed && md_moved ? "size changed and md moved" :
1050 la_size_changed ? "size changed" : "md moved");
20ceb2b2 1051 /* next line implicitly does drbd_suspend_io()+drbd_resume_io() */
b30ab791 1052 drbd_bitmap_io(device, md_moved ? &drbd_bm_write_all : &drbd_bm_write,
d752b269 1053 "size changed", BM_LOCKED_MASK);
d752b269 1054
5f7c0124
LE
1055 /* on-disk bitmap and activity log is authoritative again
1056 * (unless there was an IO error meanwhile...) */
d752b269 1057 md->flags = prev_flags;
b30ab791 1058 drbd_md_write(device, buffer);
d752b269
PR
1059
1060 if (rs)
d0180171
AG
1061 drbd_info(device, "Changed AL layout to al-stripes = %d, al-stripe-size-kB = %d\n",
1062 md->al_stripes, md->al_stripe_size_4k * 4);
b411b363
PR
1063 }
1064
8011e249
LE
1065 if (size > prev.last_agreed_sect)
1066 rv = prev.last_agreed_sect ? DS_GREW : DS_GREW_FROM_ZERO;
1067 if (size < prev.last_agreed_sect)
e96c9633 1068 rv = DS_SHRUNK;
d752b269
PR
1069
1070 if (0) {
1071 err_out:
8011e249
LE
1072 /* restore previous offset and sizes */
1073 md->la_size_sect = prev.last_agreed_sect;
1074 md->md_offset = prev.md_offset;
1075 md->al_offset = prev.al_offset;
1076 md->bm_offset = prev.bm_offset;
1077 md->md_size_sect = prev.md_size_sect;
1078 md->al_stripes = prev.al_stripes;
1079 md->al_stripe_size_4k = prev.al_stripe_size_4k;
1080 md->al_size_4k = (u64)prev.al_stripes * prev.al_stripe_size_4k;
d752b269 1081 }
b30ab791
AG
1082 lc_unlock(device->act_log);
1083 wake_up(&device->al_wait);
1084 drbd_md_put_buffer(device);
1085 drbd_resume_io(device);
b411b363
PR
1086
1087 return rv;
1088}
1089
1090sector_t
b30ab791 1091drbd_new_dev_size(struct drbd_device *device, struct drbd_backing_dev *bdev,
ef5e44a6 1092 sector_t u_size, int assume_peer_has_space)
b411b363 1093{
b30ab791 1094 sector_t p_size = device->p_size; /* partner's disk size. */
cccac985 1095 sector_t la_size_sect = bdev->md.la_size_sect; /* last agreed size. */
b411b363 1096 sector_t m_size; /* my size */
b411b363
PR
1097 sector_t size = 0;
1098
1099 m_size = drbd_get_max_capacity(bdev);
1100
b30ab791 1101 if (device->state.conn < C_CONNECTED && assume_peer_has_space) {
d0180171 1102 drbd_warn(device, "Resize while not connected was forced by the user!\n");
a393db6f
PR
1103 p_size = m_size;
1104 }
1105
b411b363
PR
1106 if (p_size && m_size) {
1107 size = min_t(sector_t, p_size, m_size);
1108 } else {
cccac985
LE
1109 if (la_size_sect) {
1110 size = la_size_sect;
b411b363
PR
1111 if (m_size && m_size < size)
1112 size = m_size;
1113 if (p_size && p_size < size)
1114 size = p_size;
1115 } else {
1116 if (m_size)
1117 size = m_size;
1118 if (p_size)
1119 size = p_size;
1120 }
1121 }
1122
1123 if (size == 0)
d0180171 1124 drbd_err(device, "Both nodes diskless!\n");
b411b363
PR
1125
1126 if (u_size) {
1127 if (u_size > size)
d0180171 1128 drbd_err(device, "Requested disk size is too big (%lu > %lu)\n",
b411b363
PR
1129 (unsigned long)u_size>>1, (unsigned long)size>>1);
1130 else
1131 size = u_size;
1132 }
1133
1134 return size;
1135}
1136
1137/**
1138 * drbd_check_al_size() - Ensures that the AL is of the right size
b30ab791 1139 * @device: DRBD device.
b411b363
PR
1140 *
1141 * Returns -EBUSY if current al lru is still used, -ENOMEM when allocation
1142 * failed, and 0 on success. You should call drbd_md_sync() after you called
1143 * this function.
1144 */
b30ab791 1145static int drbd_check_al_size(struct drbd_device *device, struct disk_conf *dc)
b411b363
PR
1146{
1147 struct lru_cache *n, *t;
1148 struct lc_element *e;
1149 unsigned int in_use;
1150 int i;
1151
b30ab791
AG
1152 if (device->act_log &&
1153 device->act_log->nr_elements == dc->al_extents)
b411b363
PR
1154 return 0;
1155
1156 in_use = 0;
b30ab791 1157 t = device->act_log;
7ad651b5 1158 n = lc_create("act_log", drbd_al_ext_cache, AL_UPDATES_PER_TRANSACTION,
f399002e 1159 dc->al_extents, sizeof(struct lc_element), 0);
b411b363
PR
1160
1161 if (n == NULL) {
d0180171 1162 drbd_err(device, "Cannot allocate act_log lru!\n");
b411b363
PR
1163 return -ENOMEM;
1164 }
b30ab791 1165 spin_lock_irq(&device->al_lock);
b411b363
PR
1166 if (t) {
1167 for (i = 0; i < t->nr_elements; i++) {
1168 e = lc_element_by_index(t, i);
1169 if (e->refcnt)
d0180171 1170 drbd_err(device, "refcnt(%d)==%d\n",
b411b363
PR
1171 e->lc_number, e->refcnt);
1172 in_use += e->refcnt;
1173 }
1174 }
1175 if (!in_use)
b30ab791
AG
1176 device->act_log = n;
1177 spin_unlock_irq(&device->al_lock);
b411b363 1178 if (in_use) {
d0180171 1179 drbd_err(device, "Activity log still in use!\n");
b411b363
PR
1180 lc_destroy(n);
1181 return -EBUSY;
1182 } else {
d01efcee 1183 lc_destroy(t);
b411b363 1184 }
b30ab791 1185 drbd_md_mark_dirty(device); /* we changed device->act_log->nr_elemens */
b411b363
PR
1186 return 0;
1187}
1188
69ba1ee9
LE
1189static void blk_queue_discard_granularity(struct request_queue *q, unsigned int granularity)
1190{
1191 q->limits.discard_granularity = granularity;
1192}
9104d31a
LE
1193
1194static unsigned int drbd_max_discard_sectors(struct drbd_connection *connection)
1195{
1196 /* when we introduced REQ_WRITE_SAME support, we also bumped
1197 * our maximum supported batch bio size used for discards. */
1198 if (connection->agreed_features & DRBD_FF_WSAME)
1199 return DRBD_MAX_BBIO_SECTORS;
1200 /* before, with DRBD <= 8.4.6, we only allowed up to one AL_EXTENT_SIZE. */
1201 return AL_EXTENT_SIZE >> 9;
1202}
1203
69ba1ee9
LE
1204static void decide_on_discard_support(struct drbd_device *device,
1205 struct request_queue *q,
1206 struct request_queue *b,
1207 bool discard_zeroes_if_aligned)
1208{
1209 /* q = drbd device queue (device->rq_queue)
1210 * b = backing device queue (device->ldev->backing_bdev->bd_disk->queue),
1211 * or NULL if diskless
1212 */
1213 struct drbd_connection *connection = first_peer_device(device)->connection;
1214 bool can_do = b ? blk_queue_discard(b) : true;
1215
9104d31a 1216 if (can_do && connection->cstate >= C_CONNECTED && !(connection->agreed_features & DRBD_FF_TRIM)) {
69ba1ee9
LE
1217 can_do = false;
1218 drbd_info(connection, "peer DRBD too old, does not support TRIM: disabling discards\n");
1219 }
1220 if (can_do) {
1221 /* We don't care for the granularity, really.
1222 * Stacking limits below should fix it for the local
1223 * device. Whether or not it is a suitable granularity
1224 * on the remote device is not our problem, really. If
1225 * you care, you need to use devices with similar
1226 * topology on all peers. */
1227 blk_queue_discard_granularity(q, 512);
9104d31a 1228 q->limits.max_discard_sectors = drbd_max_discard_sectors(connection);
8b904b5b 1229 blk_queue_flag_set(QUEUE_FLAG_DISCARD, q);
45c21793 1230 q->limits.max_write_zeroes_sectors = drbd_max_discard_sectors(connection);
69ba1ee9 1231 } else {
8b904b5b 1232 blk_queue_flag_clear(QUEUE_FLAG_DISCARD, q);
69ba1ee9
LE
1233 blk_queue_discard_granularity(q, 0);
1234 q->limits.max_discard_sectors = 0;
45c21793 1235 q->limits.max_write_zeroes_sectors = 0;
69ba1ee9
LE
1236 }
1237}
1238
1239static void fixup_discard_if_not_supported(struct request_queue *q)
1240{
1241 /* To avoid confusion, if this queue does not support discard, clear
1242 * max_discard_sectors, which is what lsblk -D reports to the user.
1243 * Older kernels got this wrong in "stack limits".
1244 * */
1245 if (!blk_queue_discard(q)) {
1246 blk_queue_max_discard_sectors(q, 0);
1247 blk_queue_discard_granularity(q, 0);
1248 }
1249}
1250
f31e583a
LE
1251static void fixup_write_zeroes(struct drbd_device *device, struct request_queue *q)
1252{
1253 /* Fixup max_write_zeroes_sectors after blk_queue_stack_limits():
1254 * if we can handle "zeroes" efficiently on the protocol,
1255 * we want to do that, even if our backend does not announce
1256 * max_write_zeroes_sectors itself. */
1257 struct drbd_connection *connection = first_peer_device(device)->connection;
1258 /* If the peer announces WZEROES support, use it. Otherwise, rather
1259 * send explicit zeroes than rely on some discard-zeroes-data magic. */
1260 if (connection->agreed_features & DRBD_FF_WZEROES)
1261 q->limits.max_write_zeroes_sectors = DRBD_MAX_BBIO_SECTORS;
1262 else
1263 q->limits.max_write_zeroes_sectors = 0;
1264}
1265
9104d31a
LE
1266static void decide_on_write_same_support(struct drbd_device *device,
1267 struct request_queue *q,
9de7e14a
LE
1268 struct request_queue *b, struct o_qlim *o,
1269 bool disable_write_same)
9104d31a
LE
1270{
1271 struct drbd_peer_device *peer_device = first_peer_device(device);
1272 struct drbd_connection *connection = peer_device->connection;
1273 bool can_do = b ? b->limits.max_write_same_sectors : true;
1274
9de7e14a
LE
1275 if (can_do && disable_write_same) {
1276 can_do = false;
1277 drbd_info(peer_device, "WRITE_SAME disabled by config\n");
1278 }
1279
9104d31a
LE
1280 if (can_do && connection->cstate >= C_CONNECTED && !(connection->agreed_features & DRBD_FF_WSAME)) {
1281 can_do = false;
1282 drbd_info(peer_device, "peer does not support WRITE_SAME\n");
1283 }
1284
1285 if (o) {
1286 /* logical block size; queue_logical_block_size(NULL) is 512 */
1287 unsigned int peer_lbs = be32_to_cpu(o->logical_block_size);
1288 unsigned int me_lbs_b = queue_logical_block_size(b);
1289 unsigned int me_lbs = queue_logical_block_size(q);
1290
1291 if (me_lbs_b != me_lbs) {
1292 drbd_warn(device,
1293 "logical block size of local backend does not match (drbd:%u, backend:%u); was this a late attach?\n",
1294 me_lbs, me_lbs_b);
1295 /* rather disable write same than trigger some BUG_ON later in the scsi layer. */
1296 can_do = false;
1297 }
1298 if (me_lbs_b != peer_lbs) {
1299 drbd_warn(peer_device, "logical block sizes do not match (me:%u, peer:%u); this may cause problems.\n",
1300 me_lbs, peer_lbs);
1301 if (can_do) {
1302 drbd_dbg(peer_device, "logical block size mismatch: WRITE_SAME disabled.\n");
1303 can_do = false;
1304 }
1305 me_lbs = max(me_lbs, me_lbs_b);
1306 /* We cannot change the logical block size of an in-use queue.
1307 * We can only hope that access happens to be properly aligned.
1308 * If not, the peer will likely produce an IO error, and detach. */
1309 if (peer_lbs > me_lbs) {
1310 if (device->state.role != R_PRIMARY) {
1311 blk_queue_logical_block_size(q, peer_lbs);
1312 drbd_warn(peer_device, "logical block size set to %u\n", peer_lbs);
1313 } else {
1314 drbd_warn(peer_device,
1315 "current Primary must NOT adjust logical block size (%u -> %u); hope for the best.\n",
1316 me_lbs, peer_lbs);
1317 }
1318 }
1319 }
1320 if (can_do && !o->write_same_capable) {
1321 /* If we introduce an open-coded write-same loop on the receiving side,
1322 * the peer would present itself as "capable". */
1323 drbd_dbg(peer_device, "WRITE_SAME disabled (peer device not capable)\n");
1324 can_do = false;
1325 }
1326 }
1327
1328 blk_queue_max_write_same_sectors(q, can_do ? DRBD_MAX_BBIO_SECTORS : 0);
1329}
1330
8fe39aac 1331static void drbd_setup_queue_param(struct drbd_device *device, struct drbd_backing_dev *bdev,
9104d31a 1332 unsigned int max_bio_size, struct o_qlim *o)
b411b363 1333{
b30ab791 1334 struct request_queue * const q = device->rq_queue;
db141b2f
LE
1335 unsigned int max_hw_sectors = max_bio_size >> 9;
1336 unsigned int max_segments = 0;
c1b3156f 1337 struct request_queue *b = NULL;
dd4f699d
LE
1338 struct disk_conf *dc;
1339 bool discard_zeroes_if_aligned = true;
9de7e14a 1340 bool disable_write_same = false;
99432fcc 1341
8fe39aac
PR
1342 if (bdev) {
1343 b = bdev->backing_bdev->bd_disk->queue;
99432fcc
PR
1344
1345 max_hw_sectors = min(queue_max_hw_sectors(b), max_bio_size >> 9);
daeda1cc 1346 rcu_read_lock();
dd4f699d
LE
1347 dc = rcu_dereference(device->ldev->disk_conf);
1348 max_segments = dc->max_bio_bvecs;
1349 discard_zeroes_if_aligned = dc->discard_zeroes_if_aligned;
9de7e14a 1350 disable_write_same = dc->disable_write_same;
daeda1cc 1351 rcu_read_unlock();
c1b3156f
PR
1352
1353 blk_set_stacking_limits(&q->limits);
99432fcc 1354 }
b411b363 1355
1816a2b4
LE
1356 blk_queue_max_hw_sectors(q, max_hw_sectors);
1357 /* This is the workaround for "bio would need to, but cannot, be split" */
1358 blk_queue_max_segments(q, max_segments ? max_segments : BLK_MAX_SEGMENTS);
09cbfeaf 1359 blk_queue_segment_boundary(q, PAGE_SIZE-1);
69ba1ee9 1360 decide_on_discard_support(device, q, b, discard_zeroes_if_aligned);
9de7e14a 1361 decide_on_write_same_support(device, q, b, o, disable_write_same);
9104d31a 1362
c1b3156f 1363 if (b) {
99432fcc
PR
1364 blk_queue_stack_limits(q, b);
1365
dc3b17cc
JK
1366 if (q->backing_dev_info->ra_pages !=
1367 b->backing_dev_info->ra_pages) {
d0180171 1368 drbd_info(device, "Adjusting my ra_pages to backing device's (%lu -> %lu)\n",
dc3b17cc
JK
1369 q->backing_dev_info->ra_pages,
1370 b->backing_dev_info->ra_pages);
1371 q->backing_dev_info->ra_pages =
1372 b->backing_dev_info->ra_pages;
99432fcc 1373 }
99432fcc 1374 }
69ba1ee9 1375 fixup_discard_if_not_supported(q);
f31e583a 1376 fixup_write_zeroes(device, q);
99432fcc
PR
1377}
1378
9104d31a 1379void drbd_reconsider_queue_parameters(struct drbd_device *device, struct drbd_backing_dev *bdev, struct o_qlim *o)
99432fcc 1380{
db141b2f 1381 unsigned int now, new, local, peer;
99432fcc 1382
b30ab791
AG
1383 now = queue_max_hw_sectors(device->rq_queue) << 9;
1384 local = device->local_max_bio_size; /* Eventually last known value, from volatile memory */
1385 peer = device->peer_max_bio_size; /* Eventually last known value, from meta data */
b411b363 1386
8fe39aac
PR
1387 if (bdev) {
1388 local = queue_max_hw_sectors(bdev->backing_bdev->bd_disk->queue) << 9;
b30ab791 1389 device->local_max_bio_size = local;
b411b363 1390 }
db141b2f 1391 local = min(local, DRBD_MAX_BIO_SIZE);
99432fcc
PR
1392
1393 /* We may ignore peer limits if the peer is modern enough.
1394 Because new from 8.3.8 onwards the peer can use multiple
1395 BIOs for a single peer_request */
b30ab791 1396 if (device->state.conn >= C_WF_REPORT_PARAMS) {
a6b32bc3 1397 if (first_peer_device(device)->connection->agreed_pro_version < 94)
b30ab791 1398 peer = min(device->peer_max_bio_size, DRBD_MAX_SIZE_H80_PACKET);
6809384c 1399 /* Correct old drbd (up to 8.3.7) if it believes it can do more than 32KiB */
a6b32bc3 1400 else if (first_peer_device(device)->connection->agreed_pro_version == 94)
99432fcc 1401 peer = DRBD_MAX_SIZE_H80_PACKET;
a6b32bc3 1402 else if (first_peer_device(device)->connection->agreed_pro_version < 100)
2ffca4f3
PR
1403 peer = DRBD_MAX_BIO_SIZE_P95; /* drbd 8.3.8 onwards, before 8.4.0 */
1404 else
99432fcc 1405 peer = DRBD_MAX_BIO_SIZE;
99432fcc 1406
fa090e70
LE
1407 /* We may later detach and re-attach on a disconnected Primary.
1408 * Avoid this setting to jump back in that case.
1409 * We want to store what we know the peer DRBD can handle,
1410 * not what the peer IO backend can handle. */
1411 if (peer > device->peer_max_bio_size)
1412 device->peer_max_bio_size = peer;
1413 }
db141b2f 1414 new = min(local, peer);
99432fcc 1415
b30ab791 1416 if (device->state.role == R_PRIMARY && new < now)
d0180171 1417 drbd_err(device, "ASSERT FAILED new < now; (%u < %u)\n", new, now);
99432fcc
PR
1418
1419 if (new != now)
d0180171 1420 drbd_info(device, "max BIO size = %u\n", new);
99432fcc 1421
9104d31a 1422 drbd_setup_queue_param(device, bdev, new, o);
b411b363
PR
1423}
1424
a18e9d1e 1425/* Starts the worker thread */
bde89a9e 1426static void conn_reconfig_start(struct drbd_connection *connection)
b411b363 1427{
bde89a9e 1428 drbd_thread_start(&connection->worker);
b5043c5e 1429 drbd_flush_workqueue(&connection->sender_work);
b411b363
PR
1430}
1431
a18e9d1e 1432/* if still unconfigured, stops worker again. */
bde89a9e 1433static void conn_reconfig_done(struct drbd_connection *connection)
b411b363 1434{
992d6e91 1435 bool stop_threads;
0500813f 1436 spin_lock_irq(&connection->resource->req_lock);
bde89a9e
AG
1437 stop_threads = conn_all_vols_unconf(connection) &&
1438 connection->cstate == C_STANDALONE;
0500813f 1439 spin_unlock_irq(&connection->resource->req_lock);
992d6e91 1440 if (stop_threads) {
668700b4
PR
1441 /* ack_receiver thread and ack_sender workqueue are implicitly
1442 * stopped by receiver in conn_disconnect() */
bde89a9e
AG
1443 drbd_thread_stop(&connection->receiver);
1444 drbd_thread_stop(&connection->worker);
992d6e91 1445 }
b411b363
PR
1446}
1447
0778286a 1448/* Make sure IO is suspended before calling this function(). */
b30ab791 1449static void drbd_suspend_al(struct drbd_device *device)
0778286a
PR
1450{
1451 int s = 0;
1452
b30ab791 1453 if (!lc_try_lock(device->act_log)) {
d0180171 1454 drbd_warn(device, "Failed to lock al in drbd_suspend_al()\n");
0778286a
PR
1455 return;
1456 }
1457
b30ab791 1458 drbd_al_shrink(device);
0500813f 1459 spin_lock_irq(&device->resource->req_lock);
b30ab791
AG
1460 if (device->state.conn < C_CONNECTED)
1461 s = !test_and_set_bit(AL_SUSPENDED, &device->flags);
0500813f 1462 spin_unlock_irq(&device->resource->req_lock);
b30ab791 1463 lc_unlock(device->act_log);
0778286a
PR
1464
1465 if (s)
d0180171 1466 drbd_info(device, "Suspended AL updates\n");
0778286a
PR
1467}
1468
5979e361
LE
1469
1470static bool should_set_defaults(struct genl_info *info)
1471{
1472 unsigned flags = ((struct drbd_genlmsghdr*)info->userhdr)->flags;
1473 return 0 != (flags & DRBD_GENL_F_SET_DEFAULTS);
1474}
1475
5bbcf5e6 1476static unsigned int drbd_al_extents_max(struct drbd_backing_dev *bdev)
d589a21e 1477{
5bbcf5e6
LE
1478 /* This is limited by 16 bit "slot" numbers,
1479 * and by available on-disk context storage.
1480 *
1481 * Also (u16)~0 is special (denotes a "free" extent).
1482 *
1483 * One transaction occupies one 4kB on-disk block,
1484 * we have n such blocks in the on disk ring buffer,
1485 * the "current" transaction may fail (n-1),
1486 * and there is 919 slot numbers context information per transaction.
1487 *
1488 * 72 transaction blocks amounts to more than 2**16 context slots,
1489 * so cap there first.
1490 */
1491 const unsigned int max_al_nr = DRBD_AL_EXTENTS_MAX;
1492 const unsigned int sufficient_on_disk =
1493 (max_al_nr + AL_CONTEXT_PER_TRANSACTION -1)
1494 /AL_CONTEXT_PER_TRANSACTION;
d589a21e 1495
5bbcf5e6
LE
1496 unsigned int al_size_4k = bdev->md.al_size_4k;
1497
1498 if (al_size_4k > sufficient_on_disk)
1499 return max_al_nr;
1500
1501 return (al_size_4k - 1) * AL_CONTEXT_PER_TRANSACTION;
d589a21e
PR
1502}
1503
70df7092
LE
1504static bool write_ordering_changed(struct disk_conf *a, struct disk_conf *b)
1505{
1506 return a->disk_barrier != b->disk_barrier ||
1507 a->disk_flushes != b->disk_flushes ||
1508 a->disk_drain != b->disk_drain;
1509}
1510
a5ca66c4
PR
1511static void sanitize_disk_conf(struct drbd_device *device, struct disk_conf *disk_conf,
1512 struct drbd_backing_dev *nbc)
c5c23854 1513{
a5ca66c4
PR
1514 struct request_queue * const q = nbc->backing_bdev->bd_disk->queue;
1515
c5c23854
PR
1516 if (disk_conf->al_extents < DRBD_AL_EXTENTS_MIN)
1517 disk_conf->al_extents = DRBD_AL_EXTENTS_MIN;
1518 if (disk_conf->al_extents > drbd_al_extents_max(nbc))
1519 disk_conf->al_extents = drbd_al_extents_max(nbc);
a5ca66c4 1520
48920ff2 1521 if (!blk_queue_discard(q)) {
65f5be35
LE
1522 if (disk_conf->rs_discard_granularity) {
1523 disk_conf->rs_discard_granularity = 0; /* disable feature */
1524 drbd_info(device, "rs_discard_granularity feature disabled\n");
1525 }
a5ca66c4
PR
1526 }
1527
1528 if (disk_conf->rs_discard_granularity) {
1529 int orig_value = disk_conf->rs_discard_granularity;
1530 int remainder;
1531
1532 if (q->limits.discard_granularity > disk_conf->rs_discard_granularity)
1533 disk_conf->rs_discard_granularity = q->limits.discard_granularity;
1534
1535 remainder = disk_conf->rs_discard_granularity % q->limits.discard_granularity;
1536 disk_conf->rs_discard_granularity += remainder;
1537
1538 if (disk_conf->rs_discard_granularity > q->limits.max_discard_sectors << 9)
1539 disk_conf->rs_discard_granularity = q->limits.max_discard_sectors << 9;
1540
1541 if (disk_conf->rs_discard_granularity != orig_value)
1542 drbd_info(device, "rs_discard_granularity changed to %d\n",
1543 disk_conf->rs_discard_granularity);
1544 }
c5c23854
PR
1545}
1546
f708bd08
LE
1547static int disk_opts_check_al_size(struct drbd_device *device, struct disk_conf *dc)
1548{
1549 int err = -EBUSY;
1550
1551 if (device->act_log &&
1552 device->act_log->nr_elements == dc->al_extents)
1553 return 0;
1554
1555 drbd_suspend_io(device);
1556 /* If IO completion is currently blocked, we would likely wait
1557 * "forever" for the activity log to become unused. So we don't. */
1558 if (atomic_read(&device->ap_bio_cnt))
1559 goto out;
1560
1561 wait_event(device->al_wait, lc_try_lock(device->act_log));
1562 drbd_al_shrink(device);
1563 err = drbd_check_al_size(device, dc);
1564 lc_unlock(device->act_log);
1565 wake_up(&device->al_wait);
1566out:
1567 drbd_resume_io(device);
1568 return err;
1569}
1570
f399002e
LE
1571int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info)
1572{
a910b123 1573 struct drbd_config_context adm_ctx;
f399002e 1574 enum drbd_ret_code retcode;
b30ab791 1575 struct drbd_device *device;
daeda1cc 1576 struct disk_conf *new_disk_conf, *old_disk_conf;
813472ce 1577 struct fifo_buffer *old_plan = NULL, *new_plan = NULL;
f399002e 1578 int err, fifo_size;
f399002e 1579
a910b123 1580 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
f399002e
LE
1581 if (!adm_ctx.reply_skb)
1582 return retcode;
1583 if (retcode != NO_ERROR)
9e276872 1584 goto finish;
f399002e 1585
b30ab791 1586 device = adm_ctx.device;
9e276872 1587 mutex_lock(&adm_ctx.resource->adm_mutex);
f399002e
LE
1588
1589 /* we also need a disk
1590 * to change the options on */
b30ab791 1591 if (!get_ldev(device)) {
f399002e
LE
1592 retcode = ERR_NO_DISK;
1593 goto out;
1594 }
1595
daeda1cc 1596 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
5ecc72c3 1597 if (!new_disk_conf) {
f399002e
LE
1598 retcode = ERR_NOMEM;
1599 goto fail;
1600 }
1601
0500813f 1602 mutex_lock(&device->resource->conf_update);
b30ab791 1603 old_disk_conf = device->ldev->disk_conf;
daeda1cc 1604 *new_disk_conf = *old_disk_conf;
5979e361 1605 if (should_set_defaults(info))
b966b5dd 1606 set_disk_conf_defaults(new_disk_conf);
5979e361 1607
5ecc72c3 1608 err = disk_conf_from_attrs_for_change(new_disk_conf, info);
c75b9b10 1609 if (err && err != -ENOMSG) {
f399002e 1610 retcode = ERR_MANDATORY_TAG;
a910b123 1611 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
8e229434 1612 goto fail_unlock;
f399002e
LE
1613 }
1614
5ecc72c3
LE
1615 if (!expect(new_disk_conf->resync_rate >= 1))
1616 new_disk_conf->resync_rate = 1;
f399002e 1617
a5ca66c4 1618 sanitize_disk_conf(device, new_disk_conf, device->ldev);
5bbcf5e6
LE
1619
1620 if (new_disk_conf->c_plan_ahead > DRBD_C_PLAN_AHEAD_MAX)
1621 new_disk_conf->c_plan_ahead = DRBD_C_PLAN_AHEAD_MAX;
f399002e 1622
5ecc72c3 1623 fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
b30ab791 1624 if (fifo_size != device->rs_plan_s->size) {
813472ce
PR
1625 new_plan = fifo_alloc(fifo_size);
1626 if (!new_plan) {
d0180171 1627 drbd_err(device, "kmalloc of fifo_buffer failed");
f399002e 1628 retcode = ERR_NOMEM;
daeda1cc 1629 goto fail_unlock;
f399002e
LE
1630 }
1631 }
1632
f708bd08 1633 err = disk_opts_check_al_size(device, new_disk_conf);
f399002e 1634 if (err) {
f708bd08
LE
1635 /* Could be just "busy". Ignore?
1636 * Introduce dedicated error code? */
1637 drbd_msg_put_info(adm_ctx.reply_skb,
1638 "Try again without changing current al-extents setting");
f399002e 1639 retcode = ERR_NOMEM;
daeda1cc 1640 goto fail_unlock;
f399002e
LE
1641 }
1642
28bc3b8c 1643 lock_all_resources();
b30ab791 1644 retcode = drbd_resync_after_valid(device, new_disk_conf->resync_after);
dc97b708 1645 if (retcode == NO_ERROR) {
b30ab791
AG
1646 rcu_assign_pointer(device->ldev->disk_conf, new_disk_conf);
1647 drbd_resync_after_changed(device);
dc97b708 1648 }
28bc3b8c 1649 unlock_all_resources();
f399002e 1650
daeda1cc
PR
1651 if (retcode != NO_ERROR)
1652 goto fail_unlock;
f399002e 1653
813472ce 1654 if (new_plan) {
b30ab791
AG
1655 old_plan = device->rs_plan_s;
1656 rcu_assign_pointer(device->rs_plan_s, new_plan);
9958c857 1657 }
9958c857 1658
0500813f 1659 mutex_unlock(&device->resource->conf_update);
27eb13e9 1660
9a51ab1c 1661 if (new_disk_conf->al_updates)
b30ab791 1662 device->ldev->md.flags &= ~MDF_AL_DISABLED;
9a51ab1c 1663 else
b30ab791 1664 device->ldev->md.flags |= MDF_AL_DISABLED;
9a51ab1c 1665
691631c0 1666 if (new_disk_conf->md_flushes)
b30ab791 1667 clear_bit(MD_NO_FUA, &device->flags);
691631c0 1668 else
b30ab791 1669 set_bit(MD_NO_FUA, &device->flags);
691631c0 1670
70df7092 1671 if (write_ordering_changed(old_disk_conf, new_disk_conf))
f6ba8636 1672 drbd_bump_write_ordering(device->resource, NULL, WO_BDEV_FLUSH);
27eb13e9 1673
9de7e14a
LE
1674 if (old_disk_conf->discard_zeroes_if_aligned != new_disk_conf->discard_zeroes_if_aligned
1675 || old_disk_conf->disable_write_same != new_disk_conf->disable_write_same)
9104d31a 1676 drbd_reconsider_queue_parameters(device, device->ldev, NULL);
dd4f699d 1677
b30ab791 1678 drbd_md_sync(device);
f399002e 1679
69a22773
AG
1680 if (device->state.conn >= C_CONNECTED) {
1681 struct drbd_peer_device *peer_device;
1682
1683 for_each_peer_device(peer_device, device)
1684 drbd_send_sync_param(peer_device);
1685 }
f399002e 1686
daeda1cc
PR
1687 synchronize_rcu();
1688 kfree(old_disk_conf);
813472ce 1689 kfree(old_plan);
b30ab791 1690 mod_timer(&device->request_timer, jiffies + HZ);
daeda1cc
PR
1691 goto success;
1692
1693fail_unlock:
0500813f 1694 mutex_unlock(&device->resource->conf_update);
f399002e 1695 fail:
5ecc72c3 1696 kfree(new_disk_conf);
813472ce 1697 kfree(new_plan);
daeda1cc 1698success:
b30ab791 1699 put_ldev(device);
f399002e 1700 out:
9e276872
LE
1701 mutex_unlock(&adm_ctx.resource->adm_mutex);
1702 finish:
a910b123 1703 drbd_adm_finish(&adm_ctx, info, retcode);
f399002e
LE
1704 return 0;
1705}
1706
63a7c8ad
LE
1707static struct block_device *open_backing_dev(struct drbd_device *device,
1708 const char *bdev_path, void *claim_ptr, bool do_bd_link)
1709{
1710 struct block_device *bdev;
1711 int err = 0;
1712
1713 bdev = blkdev_get_by_path(bdev_path,
1714 FMODE_READ | FMODE_WRITE | FMODE_EXCL, claim_ptr);
1715 if (IS_ERR(bdev)) {
1716 drbd_err(device, "open(\"%s\") failed with %ld\n",
1717 bdev_path, PTR_ERR(bdev));
1718 return bdev;
1719 }
1720
1721 if (!do_bd_link)
1722 return bdev;
1723
1724 err = bd_link_disk_holder(bdev, device->vdisk);
1725 if (err) {
1726 blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1727 drbd_err(device, "bd_link_disk_holder(\"%s\", ...) failed with %d\n",
1728 bdev_path, err);
1729 bdev = ERR_PTR(err);
1730 }
1731 return bdev;
1732}
1733
1734static int open_backing_devices(struct drbd_device *device,
1735 struct disk_conf *new_disk_conf,
1736 struct drbd_backing_dev *nbc)
1737{
1738 struct block_device *bdev;
1739
1740 bdev = open_backing_dev(device, new_disk_conf->backing_dev, device, true);
1741 if (IS_ERR(bdev))
1742 return ERR_OPEN_DISK;
1743 nbc->backing_bdev = bdev;
1744
1745 /*
1746 * meta_dev_idx >= 0: external fixed size, possibly multiple
1747 * drbd sharing one meta device. TODO in that case, paranoia
1748 * check that [md_bdev, meta_dev_idx] is not yet used by some
1749 * other drbd minor! (if you use drbd.conf + drbdadm, that
1750 * should check it for you already; but if you don't, or
1751 * someone fooled it, we need to double check here)
1752 */
1753 bdev = open_backing_dev(device, new_disk_conf->meta_dev,
1754 /* claim ptr: device, if claimed exclusively; shared drbd_m_holder,
1755 * if potentially shared with other drbd minors */
1756 (new_disk_conf->meta_dev_idx < 0) ? (void*)device : (void*)drbd_m_holder,
1757 /* avoid double bd_claim_by_disk() for the same (source,target) tuple,
1758 * as would happen with internal metadata. */
1759 (new_disk_conf->meta_dev_idx != DRBD_MD_INDEX_FLEX_INT &&
1760 new_disk_conf->meta_dev_idx != DRBD_MD_INDEX_INTERNAL));
1761 if (IS_ERR(bdev))
1762 return ERR_OPEN_MD_DISK;
1763 nbc->md_bdev = bdev;
1764 return NO_ERROR;
1765}
1766
1767static void close_backing_dev(struct drbd_device *device, struct block_device *bdev,
1768 bool do_bd_unlink)
1769{
1770 if (!bdev)
1771 return;
1772 if (do_bd_unlink)
1773 bd_unlink_disk_holder(bdev, device->vdisk);
1774 blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1775}
1776
1777void drbd_backing_dev_free(struct drbd_device *device, struct drbd_backing_dev *ldev)
1778{
1779 if (ldev == NULL)
1780 return;
1781
1782 close_backing_dev(device, ldev->md_bdev, ldev->md_bdev != ldev->backing_bdev);
1783 close_backing_dev(device, ldev->backing_bdev, true);
1784
1785 kfree(ldev->disk_conf);
1786 kfree(ldev);
1787}
1788
3b98c0c2 1789int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info)
b411b363 1790{
a910b123 1791 struct drbd_config_context adm_ctx;
b30ab791 1792 struct drbd_device *device;
44a4d551
LE
1793 struct drbd_peer_device *peer_device;
1794 struct drbd_connection *connection;
3b98c0c2 1795 int err;
116676ca 1796 enum drbd_ret_code retcode;
b411b363
PR
1797 enum determine_dev_size dd;
1798 sector_t max_possible_sectors;
1799 sector_t min_md_device_sectors;
1800 struct drbd_backing_dev *nbc = NULL; /* new_backing_conf */
daeda1cc 1801 struct disk_conf *new_disk_conf = NULL;
b411b363 1802 struct lru_cache *resync_lru = NULL;
9958c857 1803 struct fifo_buffer *new_plan = NULL;
b411b363 1804 union drbd_state ns, os;
f2024e7c 1805 enum drbd_state_rv rv;
44ed167d 1806 struct net_conf *nc;
b411b363 1807
a910b123 1808 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3b98c0c2
LE
1809 if (!adm_ctx.reply_skb)
1810 return retcode;
1811 if (retcode != NO_ERROR)
40cbf085 1812 goto finish;
b411b363 1813
b30ab791 1814 device = adm_ctx.device;
9e276872 1815 mutex_lock(&adm_ctx.resource->adm_mutex);
44a4d551 1816 peer_device = first_peer_device(device);
3b8a44f8 1817 connection = peer_device->connection;
44a4d551 1818 conn_reconfig_start(connection);
b411b363
PR
1819
1820 /* if you want to reconfigure, please tear down first */
b30ab791 1821 if (device->state.disk > D_DISKLESS) {
b411b363
PR
1822 retcode = ERR_DISK_CONFIGURED;
1823 goto fail;
1824 }
82f59cc6
LE
1825 /* It may just now have detached because of IO error. Make sure
1826 * drbd_ldev_destroy is done already, we may end up here very fast,
1827 * e.g. if someone calls attach from the on-io-error handler,
1828 * to realize a "hot spare" feature (not that I'd recommend that) */
e334f550 1829 wait_event(device->misc_wait, !test_bit(GOING_DISKLESS, &device->flags));
b411b363 1830
383606e0 1831 /* make sure there is no leftover from previous force-detach attempts */
b30ab791
AG
1832 clear_bit(FORCE_DETACH, &device->flags);
1833 clear_bit(WAS_IO_ERROR, &device->flags);
1834 clear_bit(WAS_READ_ERROR, &device->flags);
383606e0 1835
0029d624 1836 /* and no leftover from previously aborted resync or verify, either */
b30ab791
AG
1837 device->rs_total = 0;
1838 device->rs_failed = 0;
1839 atomic_set(&device->rs_pending_cnt, 0);
0029d624 1840
3b98c0c2 1841 /* allocation not in the IO path, drbdsetup context */
b411b363
PR
1842 nbc = kzalloc(sizeof(struct drbd_backing_dev), GFP_KERNEL);
1843 if (!nbc) {
1844 retcode = ERR_NOMEM;
1845 goto fail;
1846 }
9f2247bb
PR
1847 spin_lock_init(&nbc->md.uuid_lock);
1848
daeda1cc
PR
1849 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
1850 if (!new_disk_conf) {
1851 retcode = ERR_NOMEM;
b411b363
PR
1852 goto fail;
1853 }
daeda1cc 1854 nbc->disk_conf = new_disk_conf;
b411b363 1855
daeda1cc
PR
1856 set_disk_conf_defaults(new_disk_conf);
1857 err = disk_conf_from_attrs(new_disk_conf, info);
3b98c0c2 1858 if (err) {
b411b363 1859 retcode = ERR_MANDATORY_TAG;
a910b123 1860 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
b411b363
PR
1861 goto fail;
1862 }
1863
5bbcf5e6
LE
1864 if (new_disk_conf->c_plan_ahead > DRBD_C_PLAN_AHEAD_MAX)
1865 new_disk_conf->c_plan_ahead = DRBD_C_PLAN_AHEAD_MAX;
d589a21e 1866
9958c857
PR
1867 new_plan = fifo_alloc((new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ);
1868 if (!new_plan) {
1869 retcode = ERR_NOMEM;
1870 goto fail;
1871 }
1872
daeda1cc 1873 if (new_disk_conf->meta_dev_idx < DRBD_MD_INDEX_FLEX_INT) {
b411b363
PR
1874 retcode = ERR_MD_IDX_INVALID;
1875 goto fail;
1876 }
1877
44ed167d 1878 rcu_read_lock();
44a4d551 1879 nc = rcu_dereference(connection->net_conf);
44ed167d 1880 if (nc) {
daeda1cc 1881 if (new_disk_conf->fencing == FP_STONITH && nc->wire_protocol == DRBD_PROT_A) {
44ed167d 1882 rcu_read_unlock();
47ff2d0a 1883 retcode = ERR_STONITH_AND_PROT_A;
28bc3b8c 1884 goto fail;
47ff2d0a
PR
1885 }
1886 }
44ed167d 1887 rcu_read_unlock();
47ff2d0a 1888
63a7c8ad
LE
1889 retcode = open_backing_devices(device, new_disk_conf, nbc);
1890 if (retcode != NO_ERROR)
28bc3b8c 1891 goto fail;
b411b363 1892
e525fd89 1893 if ((nbc->backing_bdev == nbc->md_bdev) !=
daeda1cc
PR
1894 (new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_INTERNAL ||
1895 new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_FLEX_INT)) {
e525fd89 1896 retcode = ERR_MD_IDX_INVALID;
28bc3b8c 1897 goto fail;
b411b363
PR
1898 }
1899
1900 resync_lru = lc_create("resync", drbd_bm_ext_cache,
46a15bc3 1901 1, 61, sizeof(struct bm_extent),
b411b363
PR
1902 offsetof(struct bm_extent, lce));
1903 if (!resync_lru) {
1904 retcode = ERR_NOMEM;
28bc3b8c 1905 goto fail;
b411b363
PR
1906 }
1907
c04ccaa6
LE
1908 /* Read our meta data super block early.
1909 * This also sets other on-disk offsets. */
b30ab791 1910 retcode = drbd_md_read(device, nbc);
c04ccaa6 1911 if (retcode != NO_ERROR)
28bc3b8c 1912 goto fail;
b411b363 1913
a5ca66c4 1914 sanitize_disk_conf(device, new_disk_conf, nbc);
5bbcf5e6 1915
daeda1cc 1916 if (drbd_get_max_capacity(nbc) < new_disk_conf->disk_size) {
d0180171 1917 drbd_err(device, "max capacity %llu smaller than disk size %llu\n",
b411b363 1918 (unsigned long long) drbd_get_max_capacity(nbc),
daeda1cc 1919 (unsigned long long) new_disk_conf->disk_size);
7948bcdc 1920 retcode = ERR_DISK_TOO_SMALL;
28bc3b8c 1921 goto fail;
b411b363
PR
1922 }
1923
daeda1cc 1924 if (new_disk_conf->meta_dev_idx < 0) {
b411b363
PR
1925 max_possible_sectors = DRBD_MAX_SECTORS_FLEX;
1926 /* at least one MB, otherwise it does not make sense */
1927 min_md_device_sectors = (2<<10);
1928 } else {
1929 max_possible_sectors = DRBD_MAX_SECTORS;
ae8bf312 1930 min_md_device_sectors = MD_128MB_SECT * (new_disk_conf->meta_dev_idx + 1);
b411b363
PR
1931 }
1932
b411b363 1933 if (drbd_get_capacity(nbc->md_bdev) < min_md_device_sectors) {
7948bcdc 1934 retcode = ERR_MD_DISK_TOO_SMALL;
d0180171 1935 drbd_warn(device, "refusing attach: md-device too small, "
b411b363
PR
1936 "at least %llu sectors needed for this meta-disk type\n",
1937 (unsigned long long) min_md_device_sectors);
28bc3b8c 1938 goto fail;
b411b363
PR
1939 }
1940
1941 /* Make sure the new disk is big enough
1942 * (we may currently be R_PRIMARY with no local disk...) */
1943 if (drbd_get_max_capacity(nbc) <
b30ab791 1944 drbd_get_capacity(device->this_bdev)) {
7948bcdc 1945 retcode = ERR_DISK_TOO_SMALL;
28bc3b8c 1946 goto fail;
b411b363
PR
1947 }
1948
1949 nbc->known_size = drbd_get_capacity(nbc->backing_bdev);
1950
1352994b 1951 if (nbc->known_size > max_possible_sectors) {
d0180171 1952 drbd_warn(device, "==> truncating very big lower level device "
1352994b
LE
1953 "to currently maximum possible %llu sectors <==\n",
1954 (unsigned long long) max_possible_sectors);
daeda1cc 1955 if (new_disk_conf->meta_dev_idx >= 0)
d0180171 1956 drbd_warn(device, "==>> using internal or flexible "
1352994b
LE
1957 "meta data may help <<==\n");
1958 }
1959
b30ab791 1960 drbd_suspend_io(device);
b411b363 1961 /* also wait for the last barrier ack. */
b6dd1a89
LE
1962 /* FIXME see also https://daiquiri.linbit/cgi-bin/bugzilla/show_bug.cgi?id=171
1963 * We need a way to either ignore barrier acks for barriers sent before a device
1964 * was attached, or a way to wait for all pending barrier acks to come in.
1965 * As barriers are counted per resource,
1966 * we'd need to suspend io on all devices of a resource.
1967 */
b30ab791 1968 wait_event(device->misc_wait, !atomic_read(&device->ap_pending_cnt) || drbd_suspended(device));
b411b363 1969 /* and for any other previously queued work */
44a4d551 1970 drbd_flush_workqueue(&connection->sender_work);
b411b363 1971
b30ab791 1972 rv = _drbd_request_state(device, NS(disk, D_ATTACHING), CS_VERBOSE);
f2024e7c 1973 retcode = rv; /* FIXME: Type mismatch. */
b30ab791 1974 drbd_resume_io(device);
f2024e7c 1975 if (rv < SS_SUCCESS)
28bc3b8c 1976 goto fail;
b411b363 1977
b30ab791 1978 if (!get_ldev_if_state(device, D_ATTACHING))
b411b363
PR
1979 goto force_diskless;
1980
b30ab791
AG
1981 if (!device->bitmap) {
1982 if (drbd_bm_init(device)) {
b411b363
PR
1983 retcode = ERR_NOMEM;
1984 goto force_diskless_dec;
1985 }
1986 }
1987
fe43ed97
LE
1988 if (device->state.pdsk != D_UP_TO_DATE && device->ed_uuid &&
1989 (device->state.role == R_PRIMARY || device->state.peer == R_PRIMARY) &&
1990 (device->ed_uuid & ~((u64)1)) != (nbc->md.uuid[UI_CURRENT] & ~((u64)1))) {
d0180171 1991 drbd_err(device, "Can only attach to data with current UUID=%016llX\n",
b30ab791 1992 (unsigned long long)device->ed_uuid);
b411b363
PR
1993 retcode = ERR_DATA_NOT_CURRENT;
1994 goto force_diskless_dec;
1995 }
1996
1997 /* Since we are diskless, fix the activity log first... */
b30ab791 1998 if (drbd_check_al_size(device, new_disk_conf)) {
b411b363
PR
1999 retcode = ERR_NOMEM;
2000 goto force_diskless_dec;
2001 }
2002
2003 /* Prevent shrinking of consistent devices ! */
4ef2a4f4
LE
2004 {
2005 unsigned long long nsz = drbd_new_dev_size(device, nbc, nbc->disk_conf->disk_size, 0);
2006 unsigned long long eff = nbc->md.la_size_sect;
2007 if (drbd_md_test_flag(nbc, MDF_CONSISTENT) && nsz < eff) {
2008 if (nsz == nbc->disk_conf->disk_size) {
2009 drbd_warn(device, "truncating a consistent device during attach (%llu < %llu)\n", nsz, eff);
2010 } else {
2011 drbd_warn(device, "refusing to truncate a consistent device (%llu < %llu)\n", nsz, eff);
2012 drbd_msg_sprintf_info(adm_ctx.reply_skb,
2013 "To-be-attached device has last effective > current size, and is consistent\n"
2014 "(%llu > %llu sectors). Refusing to attach.", eff, nsz);
2015 retcode = ERR_IMPLICIT_SHRINK;
2016 goto force_diskless_dec;
2017 }
2018 }
b411b363
PR
2019 }
2020
28bc3b8c
AG
2021 lock_all_resources();
2022 retcode = drbd_resync_after_valid(device, new_disk_conf->resync_after);
2023 if (retcode != NO_ERROR) {
2024 unlock_all_resources();
2025 goto force_diskless_dec;
2026 }
2027
b411b363
PR
2028 /* Reset the "barriers don't work" bits here, then force meta data to
2029 * be written, to ensure we determine if barriers are supported. */
e544046a 2030 if (new_disk_conf->md_flushes)
b30ab791 2031 clear_bit(MD_NO_FUA, &device->flags);
b411b363 2032 else
b30ab791 2033 set_bit(MD_NO_FUA, &device->flags);
b411b363
PR
2034
2035 /* Point of no return reached.
2036 * Devices and memory are no longer released by error cleanup below.
b30ab791 2037 * now device takes over responsibility, and the state engine should
b411b363 2038 * clean it up somewhere. */
0b0ba1ef 2039 D_ASSERT(device, device->ldev == NULL);
b30ab791
AG
2040 device->ldev = nbc;
2041 device->resync = resync_lru;
2042 device->rs_plan_s = new_plan;
b411b363
PR
2043 nbc = NULL;
2044 resync_lru = NULL;
daeda1cc 2045 new_disk_conf = NULL;
9958c857 2046 new_plan = NULL;
b411b363 2047
1ec317d3 2048 drbd_resync_after_changed(device);
f6ba8636 2049 drbd_bump_write_ordering(device->resource, device->ldev, WO_BDEV_FLUSH);
28bc3b8c 2050 unlock_all_resources();
b411b363 2051
b30ab791
AG
2052 if (drbd_md_test_flag(device->ldev, MDF_CRASHED_PRIMARY))
2053 set_bit(CRASHED_PRIMARY, &device->flags);
b411b363 2054 else
b30ab791 2055 clear_bit(CRASHED_PRIMARY, &device->flags);
b411b363 2056
b30ab791 2057 if (drbd_md_test_flag(device->ldev, MDF_PRIMARY_IND) &&
6bbf53ca 2058 !(device->state.role == R_PRIMARY && device->resource->susp_nod))
b30ab791 2059 set_bit(CRASHED_PRIMARY, &device->flags);
b411b363 2060
b30ab791
AG
2061 device->send_cnt = 0;
2062 device->recv_cnt = 0;
2063 device->read_cnt = 0;
2064 device->writ_cnt = 0;
b411b363 2065
9104d31a 2066 drbd_reconsider_queue_parameters(device, device->ldev, NULL);
b411b363
PR
2067
2068 /* If I am currently not R_PRIMARY,
2069 * but meta data primary indicator is set,
2070 * I just now recover from a hard crash,
2071 * and have been R_PRIMARY before that crash.
2072 *
2073 * Now, if I had no connection before that crash
2074 * (have been degraded R_PRIMARY), chances are that
2075 * I won't find my peer now either.
2076 *
2077 * In that case, and _only_ in that case,
2078 * we use the degr-wfc-timeout instead of the default,
2079 * so we can automatically recover from a crash of a
2080 * degraded but active "cluster" after a certain timeout.
2081 */
b30ab791
AG
2082 clear_bit(USE_DEGR_WFC_T, &device->flags);
2083 if (device->state.role != R_PRIMARY &&
2084 drbd_md_test_flag(device->ldev, MDF_PRIMARY_IND) &&
2085 !drbd_md_test_flag(device->ldev, MDF_CONNECTED_IND))
2086 set_bit(USE_DEGR_WFC_T, &device->flags);
b411b363 2087
b30ab791 2088 dd = drbd_determine_dev_size(device, 0, NULL);
d752b269 2089 if (dd <= DS_ERROR) {
b411b363
PR
2090 retcode = ERR_NOMEM_BITMAP;
2091 goto force_diskless_dec;
e96c9633 2092 } else if (dd == DS_GREW)
b30ab791 2093 set_bit(RESYNC_AFTER_NEG, &device->flags);
b411b363 2094
b30ab791
AG
2095 if (drbd_md_test_flag(device->ldev, MDF_FULL_SYNC) ||
2096 (test_bit(CRASHED_PRIMARY, &device->flags) &&
2097 drbd_md_test_flag(device->ldev, MDF_AL_DISABLED))) {
d0180171 2098 drbd_info(device, "Assuming that all blocks are out of sync "
b411b363 2099 "(aka FullSync)\n");
b30ab791 2100 if (drbd_bitmap_io(device, &drbd_bmio_set_n_write,
20ceb2b2 2101 "set_n_write from attaching", BM_LOCKED_MASK)) {
b411b363
PR
2102 retcode = ERR_IO_MD_DISK;
2103 goto force_diskless_dec;
2104 }
2105 } else {
b30ab791 2106 if (drbd_bitmap_io(device, &drbd_bm_read,
22ab6a30 2107 "read from attaching", BM_LOCKED_MASK)) {
19f843aa
LE
2108 retcode = ERR_IO_MD_DISK;
2109 goto force_diskless_dec;
2110 }
b411b363
PR
2111 }
2112
b30ab791
AG
2113 if (_drbd_bm_total_weight(device) == drbd_bm_bits(device))
2114 drbd_suspend_al(device); /* IO is still suspended here... */
0778286a 2115
0500813f 2116 spin_lock_irq(&device->resource->req_lock);
b30ab791 2117 os = drbd_read_state(device);
78bae59b 2118 ns = os;
b411b363
PR
2119 /* If MDF_CONSISTENT is not set go into inconsistent state,
2120 otherwise investigate MDF_WasUpToDate...
2121 If MDF_WAS_UP_TO_DATE is not set go into D_OUTDATED disk state,
2122 otherwise into D_CONSISTENT state.
2123 */
b30ab791
AG
2124 if (drbd_md_test_flag(device->ldev, MDF_CONSISTENT)) {
2125 if (drbd_md_test_flag(device->ldev, MDF_WAS_UP_TO_DATE))
b411b363
PR
2126 ns.disk = D_CONSISTENT;
2127 else
2128 ns.disk = D_OUTDATED;
2129 } else {
2130 ns.disk = D_INCONSISTENT;
2131 }
2132
b30ab791 2133 if (drbd_md_test_flag(device->ldev, MDF_PEER_OUT_DATED))
b411b363
PR
2134 ns.pdsk = D_OUTDATED;
2135
daeda1cc
PR
2136 rcu_read_lock();
2137 if (ns.disk == D_CONSISTENT &&
b30ab791 2138 (ns.pdsk == D_OUTDATED || rcu_dereference(device->ldev->disk_conf)->fencing == FP_DONT_CARE))
b411b363
PR
2139 ns.disk = D_UP_TO_DATE;
2140
2141 /* All tests on MDF_PRIMARY_IND, MDF_CONNECTED_IND,
2142 MDF_CONSISTENT and MDF_WAS_UP_TO_DATE must happen before
2143 this point, because drbd_request_state() modifies these
2144 flags. */
2145
b30ab791
AG
2146 if (rcu_dereference(device->ldev->disk_conf)->al_updates)
2147 device->ldev->md.flags &= ~MDF_AL_DISABLED;
9a51ab1c 2148 else
b30ab791 2149 device->ldev->md.flags |= MDF_AL_DISABLED;
9a51ab1c
PR
2150
2151 rcu_read_unlock();
2152
b411b363
PR
2153 /* In case we are C_CONNECTED postpone any decision on the new disk
2154 state after the negotiation phase. */
b30ab791
AG
2155 if (device->state.conn == C_CONNECTED) {
2156 device->new_state_tmp.i = ns.i;
b411b363
PR
2157 ns.i = os.i;
2158 ns.disk = D_NEGOTIATING;
dc66c74d
PR
2159
2160 /* We expect to receive up-to-date UUIDs soon.
2161 To avoid a race in receive_state, free p_uuid while
2162 holding req_lock. I.e. atomic with the state change */
b30ab791
AG
2163 kfree(device->p_uuid);
2164 device->p_uuid = NULL;
b411b363
PR
2165 }
2166
b30ab791 2167 rv = _drbd_set_state(device, ns, CS_VERBOSE, NULL);
0500813f 2168 spin_unlock_irq(&device->resource->req_lock);
b411b363
PR
2169
2170 if (rv < SS_SUCCESS)
2171 goto force_diskless_dec;
2172
b30ab791 2173 mod_timer(&device->request_timer, jiffies + HZ);
cdfda633 2174
b30ab791
AG
2175 if (device->state.role == R_PRIMARY)
2176 device->ldev->md.uuid[UI_CURRENT] |= (u64)1;
b411b363 2177 else
b30ab791 2178 device->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
b411b363 2179
b30ab791
AG
2180 drbd_md_mark_dirty(device);
2181 drbd_md_sync(device);
b411b363 2182
b30ab791
AG
2183 kobject_uevent(&disk_to_dev(device->vdisk)->kobj, KOBJ_CHANGE);
2184 put_ldev(device);
44a4d551 2185 conn_reconfig_done(connection);
9e276872 2186 mutex_unlock(&adm_ctx.resource->adm_mutex);
a910b123 2187 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
2188 return 0;
2189
2190 force_diskless_dec:
b30ab791 2191 put_ldev(device);
b411b363 2192 force_diskless:
b30ab791
AG
2193 drbd_force_state(device, NS(disk, D_DISKLESS));
2194 drbd_md_sync(device);
b411b363 2195 fail:
44a4d551 2196 conn_reconfig_done(connection);
b411b363 2197 if (nbc) {
63a7c8ad
LE
2198 close_backing_dev(device, nbc->md_bdev, nbc->md_bdev != nbc->backing_bdev);
2199 close_backing_dev(device, nbc->backing_bdev, true);
b411b363
PR
2200 kfree(nbc);
2201 }
daeda1cc 2202 kfree(new_disk_conf);
b411b363 2203 lc_destroy(resync_lru);
9958c857 2204 kfree(new_plan);
9e276872 2205 mutex_unlock(&adm_ctx.resource->adm_mutex);
40cbf085 2206 finish:
a910b123 2207 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
2208 return 0;
2209}
2210
b30ab791 2211static int adm_detach(struct drbd_device *device, int force)
b411b363 2212{
cdfda633 2213 if (force) {
b30ab791
AG
2214 set_bit(FORCE_DETACH, &device->flags);
2215 drbd_force_state(device, NS(disk, D_FAILED));
33d32fa7 2216 return SS_SUCCESS;
02ee8f95
PR
2217 }
2218
33d32fa7 2219 return drbd_request_detach_interruptible(device);
b411b363
PR
2220}
2221
82f59cc6
LE
2222/* Detaching the disk is a process in multiple stages. First we need to lock
2223 * out application IO, in-flight IO, IO stuck in drbd_al_begin_io.
2224 * Then we transition to D_DISKLESS, and wait for put_ldev() to return all
2225 * internal references as well.
2226 * Only then we have finally detached. */
3b98c0c2 2227int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info)
b411b363 2228{
a910b123 2229 struct drbd_config_context adm_ctx;
116676ca 2230 enum drbd_ret_code retcode;
cdfda633
PR
2231 struct detach_parms parms = { };
2232 int err;
b411b363 2233
a910b123 2234 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3b98c0c2
LE
2235 if (!adm_ctx.reply_skb)
2236 return retcode;
2237 if (retcode != NO_ERROR)
2238 goto out;
b411b363 2239
cdfda633
PR
2240 if (info->attrs[DRBD_NLA_DETACH_PARMS]) {
2241 err = detach_parms_from_attrs(&parms, info);
2242 if (err) {
2243 retcode = ERR_MANDATORY_TAG;
a910b123 2244 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
cdfda633
PR
2245 goto out;
2246 }
b411b363
PR
2247 }
2248
9e276872 2249 mutex_lock(&adm_ctx.resource->adm_mutex);
b30ab791 2250 retcode = adm_detach(adm_ctx.device, parms.force_detach);
9e276872 2251 mutex_unlock(&adm_ctx.resource->adm_mutex);
3b98c0c2 2252out:
a910b123 2253 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
2254 return 0;
2255}
b411b363 2256
bde89a9e 2257static bool conn_resync_running(struct drbd_connection *connection)
f399002e 2258{
c06ece6b 2259 struct drbd_peer_device *peer_device;
695d08fa 2260 bool rv = false;
f399002e
LE
2261 int vnr;
2262
695d08fa 2263 rcu_read_lock();
c06ece6b
AG
2264 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
2265 struct drbd_device *device = peer_device->device;
b30ab791
AG
2266 if (device->state.conn == C_SYNC_SOURCE ||
2267 device->state.conn == C_SYNC_TARGET ||
2268 device->state.conn == C_PAUSED_SYNC_S ||
2269 device->state.conn == C_PAUSED_SYNC_T) {
695d08fa
PR
2270 rv = true;
2271 break;
2272 }
b411b363 2273 }
695d08fa 2274 rcu_read_unlock();
b411b363 2275
695d08fa 2276 return rv;
f399002e 2277}
47ff2d0a 2278
bde89a9e 2279static bool conn_ov_running(struct drbd_connection *connection)
f399002e 2280{
c06ece6b 2281 struct drbd_peer_device *peer_device;
695d08fa 2282 bool rv = false;
f399002e
LE
2283 int vnr;
2284
695d08fa 2285 rcu_read_lock();
c06ece6b
AG
2286 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
2287 struct drbd_device *device = peer_device->device;
b30ab791
AG
2288 if (device->state.conn == C_VERIFY_S ||
2289 device->state.conn == C_VERIFY_T) {
695d08fa
PR
2290 rv = true;
2291 break;
47ff2d0a
PR
2292 }
2293 }
695d08fa 2294 rcu_read_unlock();
b411b363 2295
695d08fa 2296 return rv;
f399002e 2297}
422028b1 2298
cd64397c 2299static enum drbd_ret_code
270eb5c9 2300_check_net_options(struct drbd_connection *connection, struct net_conf *old_net_conf, struct net_conf *new_net_conf)
cd64397c 2301{
c06ece6b 2302 struct drbd_peer_device *peer_device;
cd64397c 2303 int i;
b411b363 2304
270eb5c9
AG
2305 if (old_net_conf && connection->cstate == C_WF_REPORT_PARAMS && connection->agreed_pro_version < 100) {
2306 if (new_net_conf->wire_protocol != old_net_conf->wire_protocol)
dcb20d1a 2307 return ERR_NEED_APV_100;
b411b363 2308
270eb5c9 2309 if (new_net_conf->two_primaries != old_net_conf->two_primaries)
dcb20d1a
PR
2310 return ERR_NEED_APV_100;
2311
270eb5c9 2312 if (strcmp(new_net_conf->integrity_alg, old_net_conf->integrity_alg))
dcb20d1a 2313 return ERR_NEED_APV_100;
b411b363
PR
2314 }
2315
270eb5c9 2316 if (!new_net_conf->two_primaries &&
bde89a9e
AG
2317 conn_highest_role(connection) == R_PRIMARY &&
2318 conn_highest_peer(connection) == R_PRIMARY)
dcb20d1a 2319 return ERR_NEED_ALLOW_TWO_PRI;
b411b363 2320
270eb5c9
AG
2321 if (new_net_conf->two_primaries &&
2322 (new_net_conf->wire_protocol != DRBD_PROT_C))
cd64397c
PR
2323 return ERR_NOT_PROTO_C;
2324
c06ece6b
AG
2325 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
2326 struct drbd_device *device = peer_device->device;
b30ab791
AG
2327 if (get_ldev(device)) {
2328 enum drbd_fencing_p fp = rcu_dereference(device->ldev->disk_conf)->fencing;
2329 put_ldev(device);
270eb5c9 2330 if (new_net_conf->wire_protocol == DRBD_PROT_A && fp == FP_STONITH)
cd64397c 2331 return ERR_STONITH_AND_PROT_A;
b411b363 2332 }
270eb5c9 2333 if (device->state.role == R_PRIMARY && new_net_conf->discard_my_data)
eb12010e 2334 return ERR_DISCARD_IMPOSSIBLE;
b411b363
PR
2335 }
2336
270eb5c9 2337 if (new_net_conf->on_congestion != OC_BLOCK && new_net_conf->wire_protocol != DRBD_PROT_A)
cd64397c 2338 return ERR_CONG_NOT_PROTO_A;
b411b363 2339
cd64397c
PR
2340 return NO_ERROR;
2341}
b411b363 2342
44ed167d 2343static enum drbd_ret_code
270eb5c9 2344check_net_options(struct drbd_connection *connection, struct net_conf *new_net_conf)
44ed167d 2345{
e9d5d4a0 2346 enum drbd_ret_code rv;
c06ece6b 2347 struct drbd_peer_device *peer_device;
44ed167d 2348 int i;
b411b363 2349
44ed167d 2350 rcu_read_lock();
270eb5c9 2351 rv = _check_net_options(connection, rcu_dereference(connection->net_conf), new_net_conf);
44ed167d 2352 rcu_read_unlock();
b411b363 2353
179e20b8 2354 /* connection->peer_devices protected by genl_lock() here */
c06ece6b
AG
2355 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
2356 struct drbd_device *device = peer_device->device;
b30ab791
AG
2357 if (!device->bitmap) {
2358 if (drbd_bm_init(device))
44ed167d 2359 return ERR_NOMEM;
b411b363
PR
2360 }
2361 }
2362
44ed167d
PR
2363 return rv;
2364}
b411b363 2365
0fd0ea06 2366struct crypto {
3d0e6375
KC
2367 struct crypto_shash *verify_tfm;
2368 struct crypto_shash *csums_tfm;
9534d671 2369 struct crypto_shash *cram_hmac_tfm;
3d0e6375 2370 struct crypto_shash *integrity_tfm;
0fd0ea06 2371};
b411b363 2372
0fd0ea06 2373static int
9534d671 2374alloc_shash(struct crypto_shash **tfm, char *tfm_name, int err_alg)
0fd0ea06
PR
2375{
2376 if (!tfm_name[0])
2377 return NO_ERROR;
b411b363 2378
9534d671
HX
2379 *tfm = crypto_alloc_shash(tfm_name, 0, 0);
2380 if (IS_ERR(*tfm)) {
2381 *tfm = NULL;
2382 return err_alg;
2383 }
2384
2385 return NO_ERROR;
2386}
2387
0fd0ea06 2388static enum drbd_ret_code
270eb5c9 2389alloc_crypto(struct crypto *crypto, struct net_conf *new_net_conf)
0fd0ea06
PR
2390{
2391 char hmac_name[CRYPTO_MAX_ALG_NAME];
2392 enum drbd_ret_code rv;
0fd0ea06 2393
3d0e6375 2394 rv = alloc_shash(&crypto->csums_tfm, new_net_conf->csums_alg,
9534d671 2395 ERR_CSUMS_ALG);
0fd0ea06
PR
2396 if (rv != NO_ERROR)
2397 return rv;
3d0e6375 2398 rv = alloc_shash(&crypto->verify_tfm, new_net_conf->verify_alg,
9534d671 2399 ERR_VERIFY_ALG);
0fd0ea06
PR
2400 if (rv != NO_ERROR)
2401 return rv;
3d0e6375 2402 rv = alloc_shash(&crypto->integrity_tfm, new_net_conf->integrity_alg,
9534d671 2403 ERR_INTEGRITY_ALG);
0fd0ea06
PR
2404 if (rv != NO_ERROR)
2405 return rv;
270eb5c9 2406 if (new_net_conf->cram_hmac_alg[0] != 0) {
0fd0ea06 2407 snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)",
270eb5c9 2408 new_net_conf->cram_hmac_alg);
b411b363 2409
9534d671
HX
2410 rv = alloc_shash(&crypto->cram_hmac_tfm, hmac_name,
2411 ERR_AUTH_ALG);
b411b363
PR
2412 }
2413
0fd0ea06
PR
2414 return rv;
2415}
b411b363 2416
0fd0ea06
PR
2417static void free_crypto(struct crypto *crypto)
2418{
9534d671 2419 crypto_free_shash(crypto->cram_hmac_tfm);
3d0e6375
KC
2420 crypto_free_shash(crypto->integrity_tfm);
2421 crypto_free_shash(crypto->csums_tfm);
2422 crypto_free_shash(crypto->verify_tfm);
0fd0ea06 2423}
b411b363 2424
f399002e
LE
2425int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info)
2426{
a910b123 2427 struct drbd_config_context adm_ctx;
f399002e 2428 enum drbd_ret_code retcode;
bde89a9e 2429 struct drbd_connection *connection;
270eb5c9 2430 struct net_conf *old_net_conf, *new_net_conf = NULL;
f399002e
LE
2431 int err;
2432 int ovr; /* online verify running */
2433 int rsr; /* re-sync running */
0fd0ea06 2434 struct crypto crypto = { };
b411b363 2435
a910b123 2436 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_CONNECTION);
f399002e
LE
2437 if (!adm_ctx.reply_skb)
2438 return retcode;
2439 if (retcode != NO_ERROR)
9e276872 2440 goto finish;
b411b363 2441
bde89a9e 2442 connection = adm_ctx.connection;
9e276872 2443 mutex_lock(&adm_ctx.resource->adm_mutex);
b411b363 2444
270eb5c9
AG
2445 new_net_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
2446 if (!new_net_conf) {
f399002e
LE
2447 retcode = ERR_NOMEM;
2448 goto out;
2449 }
b411b363 2450
bde89a9e 2451 conn_reconfig_start(connection);
b411b363 2452
bde89a9e 2453 mutex_lock(&connection->data.mutex);
0500813f 2454 mutex_lock(&connection->resource->conf_update);
270eb5c9 2455 old_net_conf = connection->net_conf;
2561b9c1 2456
270eb5c9 2457 if (!old_net_conf) {
a910b123 2458 drbd_msg_put_info(adm_ctx.reply_skb, "net conf missing, try connect");
f399002e 2459 retcode = ERR_INVALID_REQUEST;
2561b9c1
PR
2460 goto fail;
2461 }
2462
270eb5c9 2463 *new_net_conf = *old_net_conf;
5979e361 2464 if (should_set_defaults(info))
270eb5c9 2465 set_net_conf_defaults(new_net_conf);
f399002e 2466
270eb5c9 2467 err = net_conf_from_attrs_for_change(new_net_conf, info);
c75b9b10 2468 if (err && err != -ENOMSG) {
f399002e 2469 retcode = ERR_MANDATORY_TAG;
a910b123 2470 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
f399002e 2471 goto fail;
2561b9c1 2472 }
b411b363 2473
270eb5c9 2474 retcode = check_net_options(connection, new_net_conf);
cd64397c
PR
2475 if (retcode != NO_ERROR)
2476 goto fail;
b411b363 2477
f399002e 2478 /* re-sync running */
bde89a9e 2479 rsr = conn_resync_running(connection);
270eb5c9 2480 if (rsr && strcmp(new_net_conf->csums_alg, old_net_conf->csums_alg)) {
f399002e 2481 retcode = ERR_CSUMS_RESYNC_RUNNING;
91fd4dad 2482 goto fail;
b411b363
PR
2483 }
2484
f399002e 2485 /* online verify running */
bde89a9e 2486 ovr = conn_ov_running(connection);
270eb5c9 2487 if (ovr && strcmp(new_net_conf->verify_alg, old_net_conf->verify_alg)) {
0fd0ea06 2488 retcode = ERR_VERIFY_RUNNING;
b411b363 2489 goto fail;
f399002e 2490 }
b411b363 2491
270eb5c9 2492 retcode = alloc_crypto(&crypto, new_net_conf);
0fd0ea06 2493 if (retcode != NO_ERROR)
b411b363 2494 goto fail;
f399002e 2495
270eb5c9 2496 rcu_assign_pointer(connection->net_conf, new_net_conf);
f399002e
LE
2497
2498 if (!rsr) {
3d0e6375 2499 crypto_free_shash(connection->csums_tfm);
bde89a9e 2500 connection->csums_tfm = crypto.csums_tfm;
0fd0ea06 2501 crypto.csums_tfm = NULL;
f399002e
LE
2502 }
2503 if (!ovr) {
3d0e6375 2504 crypto_free_shash(connection->verify_tfm);
bde89a9e 2505 connection->verify_tfm = crypto.verify_tfm;
0fd0ea06 2506 crypto.verify_tfm = NULL;
b411b363
PR
2507 }
2508
3d0e6375 2509 crypto_free_shash(connection->integrity_tfm);
bde89a9e
AG
2510 connection->integrity_tfm = crypto.integrity_tfm;
2511 if (connection->cstate >= C_WF_REPORT_PARAMS && connection->agreed_pro_version >= 100)
2512 /* Do this without trying to take connection->data.mutex again. */
2513 __drbd_send_protocol(connection, P_PROTOCOL_UPDATE);
0fd0ea06 2514
9534d671 2515 crypto_free_shash(connection->cram_hmac_tfm);
bde89a9e 2516 connection->cram_hmac_tfm = crypto.cram_hmac_tfm;
0fd0ea06 2517
0500813f 2518 mutex_unlock(&connection->resource->conf_update);
bde89a9e 2519 mutex_unlock(&connection->data.mutex);
91fd4dad 2520 synchronize_rcu();
270eb5c9 2521 kfree(old_net_conf);
91fd4dad 2522
69a22773
AG
2523 if (connection->cstate >= C_WF_REPORT_PARAMS) {
2524 struct drbd_peer_device *peer_device;
2525 int vnr;
2526
2527 idr_for_each_entry(&connection->peer_devices, peer_device, vnr)
2528 drbd_send_sync_param(peer_device);
2529 }
f399002e 2530
91fd4dad
PR
2531 goto done;
2532
b411b363 2533 fail:
0500813f 2534 mutex_unlock(&connection->resource->conf_update);
bde89a9e 2535 mutex_unlock(&connection->data.mutex);
0fd0ea06 2536 free_crypto(&crypto);
270eb5c9 2537 kfree(new_net_conf);
91fd4dad 2538 done:
bde89a9e 2539 conn_reconfig_done(connection);
f399002e 2540 out:
9e276872
LE
2541 mutex_unlock(&adm_ctx.resource->adm_mutex);
2542 finish:
a910b123 2543 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
2544 return 0;
2545}
2546
a2972846
AG
2547static void connection_to_info(struct connection_info *info,
2548 struct drbd_connection *connection)
2549{
2550 info->conn_connection_state = connection->cstate;
2551 info->conn_role = conn_highest_peer(connection);
2552}
2553
2554static void peer_device_to_info(struct peer_device_info *info,
2555 struct drbd_peer_device *peer_device)
2556{
2557 struct drbd_device *device = peer_device->device;
2558
2559 info->peer_repl_state =
2560 max_t(enum drbd_conns, C_WF_REPORT_PARAMS, device->state.conn);
2561 info->peer_disk_state = device->state.pdsk;
2562 info->peer_resync_susp_user = device->state.user_isp;
2563 info->peer_resync_susp_peer = device->state.peer_isp;
2564 info->peer_resync_susp_dependency = device->state.aftr_isp;
2565}
2566
3b98c0c2 2567int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info)
b411b363 2568{
a2972846
AG
2569 struct connection_info connection_info;
2570 enum drbd_notification_type flags;
2571 unsigned int peer_devices = 0;
a910b123 2572 struct drbd_config_context adm_ctx;
c06ece6b 2573 struct drbd_peer_device *peer_device;
270eb5c9 2574 struct net_conf *old_net_conf, *new_net_conf = NULL;
0fd0ea06 2575 struct crypto crypto = { };
77c556f6 2576 struct drbd_resource *resource;
bde89a9e 2577 struct drbd_connection *connection;
3b98c0c2
LE
2578 enum drbd_ret_code retcode;
2579 int i;
2580 int err;
b411b363 2581
a910b123 2582 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
b411b363 2583
3b98c0c2
LE
2584 if (!adm_ctx.reply_skb)
2585 return retcode;
2586 if (retcode != NO_ERROR)
2587 goto out;
089c075d 2588 if (!(adm_ctx.my_addr && adm_ctx.peer_addr)) {
a910b123 2589 drbd_msg_put_info(adm_ctx.reply_skb, "connection endpoint(s) missing");
089c075d
AG
2590 retcode = ERR_INVALID_REQUEST;
2591 goto out;
2592 }
b411b363 2593
089c075d
AG
2594 /* No need for _rcu here. All reconfiguration is
2595 * strictly serialized on genl_lock(). We are protected against
2596 * concurrent reconfiguration/addition/deletion */
77c556f6
AG
2597 for_each_resource(resource, &drbd_resources) {
2598 for_each_connection(connection, resource) {
2599 if (nla_len(adm_ctx.my_addr) == connection->my_addr_len &&
2600 !memcmp(nla_data(adm_ctx.my_addr), &connection->my_addr,
2601 connection->my_addr_len)) {
2602 retcode = ERR_LOCAL_ADDR;
2603 goto out;
2604 }
b411b363 2605
77c556f6
AG
2606 if (nla_len(adm_ctx.peer_addr) == connection->peer_addr_len &&
2607 !memcmp(nla_data(adm_ctx.peer_addr), &connection->peer_addr,
2608 connection->peer_addr_len)) {
2609 retcode = ERR_PEER_ADDR;
2610 goto out;
2611 }
089c075d 2612 }
b411b363
PR
2613 }
2614
9e276872 2615 mutex_lock(&adm_ctx.resource->adm_mutex);
3ab706fe 2616 connection = first_connection(adm_ctx.resource);
bde89a9e 2617 conn_reconfig_start(connection);
b411b363 2618
bde89a9e 2619 if (connection->cstate > C_STANDALONE) {
b411b363 2620 retcode = ERR_NET_CONFIGURED;
b411b363
PR
2621 goto fail;
2622 }
2623
a209b4ae 2624 /* allocation not in the IO path, drbdsetup / netlink process context */
270eb5c9
AG
2625 new_net_conf = kzalloc(sizeof(*new_net_conf), GFP_KERNEL);
2626 if (!new_net_conf) {
b411b363 2627 retcode = ERR_NOMEM;
b411b363
PR
2628 goto fail;
2629 }
2630
270eb5c9 2631 set_net_conf_defaults(new_net_conf);
b411b363 2632
270eb5c9 2633 err = net_conf_from_attrs(new_net_conf, info);
25e40932 2634 if (err && err != -ENOMSG) {
b411b363 2635 retcode = ERR_MANDATORY_TAG;
a910b123 2636 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
b411b363
PR
2637 goto fail;
2638 }
2639
270eb5c9 2640 retcode = check_net_options(connection, new_net_conf);
cd64397c 2641 if (retcode != NO_ERROR)
422028b1 2642 goto fail;
b411b363 2643
270eb5c9 2644 retcode = alloc_crypto(&crypto, new_net_conf);
0fd0ea06
PR
2645 if (retcode != NO_ERROR)
2646 goto fail;
b411b363 2647
270eb5c9 2648 ((char *)new_net_conf->shared_secret)[SHARED_SECRET_MAX-1] = 0;
7b4e4d31 2649
b5043c5e 2650 drbd_flush_workqueue(&connection->sender_work);
b411b363 2651
0500813f 2652 mutex_lock(&adm_ctx.resource->conf_update);
270eb5c9
AG
2653 old_net_conf = connection->net_conf;
2654 if (old_net_conf) {
b411b363 2655 retcode = ERR_NET_CONFIGURED;
0500813f 2656 mutex_unlock(&adm_ctx.resource->conf_update);
b411b363
PR
2657 goto fail;
2658 }
270eb5c9 2659 rcu_assign_pointer(connection->net_conf, new_net_conf);
b411b363 2660
bde89a9e
AG
2661 conn_free_crypto(connection);
2662 connection->cram_hmac_tfm = crypto.cram_hmac_tfm;
2663 connection->integrity_tfm = crypto.integrity_tfm;
2664 connection->csums_tfm = crypto.csums_tfm;
2665 connection->verify_tfm = crypto.verify_tfm;
b411b363 2666
bde89a9e
AG
2667 connection->my_addr_len = nla_len(adm_ctx.my_addr);
2668 memcpy(&connection->my_addr, nla_data(adm_ctx.my_addr), connection->my_addr_len);
2669 connection->peer_addr_len = nla_len(adm_ctx.peer_addr);
2670 memcpy(&connection->peer_addr, nla_data(adm_ctx.peer_addr), connection->peer_addr_len);
b411b363 2671
a2972846
AG
2672 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
2673 peer_devices++;
2674 }
2675
2676 connection_to_info(&connection_info, connection);
2677 flags = (peer_devices--) ? NOTIFY_CONTINUES : 0;
2678 mutex_lock(&notification_mutex);
2679 notify_connection_state(NULL, 0, connection, &connection_info, NOTIFY_CREATE | flags);
2680 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
2681 struct peer_device_info peer_device_info;
2682
2683 peer_device_to_info(&peer_device_info, peer_device);
2684 flags = (peer_devices--) ? NOTIFY_CONTINUES : 0;
2685 notify_peer_device_state(NULL, 0, peer_device, &peer_device_info, NOTIFY_CREATE | flags);
2686 }
2687 mutex_unlock(&notification_mutex);
0500813f 2688 mutex_unlock(&adm_ctx.resource->conf_update);
b411b363 2689
695d08fa 2690 rcu_read_lock();
c06ece6b
AG
2691 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
2692 struct drbd_device *device = peer_device->device;
b30ab791
AG
2693 device->send_cnt = 0;
2694 device->recv_cnt = 0;
b411b363 2695 }
695d08fa 2696 rcu_read_unlock();
b411b363 2697
bde89a9e 2698 retcode = conn_request_state(connection, NS(conn, C_UNCONNECTED), CS_VERBOSE);
b411b363 2699
bde89a9e 2700 conn_reconfig_done(connection);
9e276872 2701 mutex_unlock(&adm_ctx.resource->adm_mutex);
a910b123 2702 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363 2703 return 0;
b411b363 2704
b411b363 2705fail:
0fd0ea06 2706 free_crypto(&crypto);
270eb5c9 2707 kfree(new_net_conf);
b411b363 2708
bde89a9e 2709 conn_reconfig_done(connection);
9e276872 2710 mutex_unlock(&adm_ctx.resource->adm_mutex);
3b98c0c2 2711out:
a910b123 2712 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
2713 return 0;
2714}
2715
bde89a9e 2716static enum drbd_state_rv conn_try_disconnect(struct drbd_connection *connection, bool force)
85f75dd7 2717{
be80ff88 2718 enum drbd_conns cstate;
85f75dd7 2719 enum drbd_state_rv rv;
85f75dd7 2720
be80ff88 2721repeat:
bde89a9e 2722 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING),
f3dfa40a 2723 force ? CS_HARD : 0);
85f75dd7
LE
2724
2725 switch (rv) {
2726 case SS_NOTHING_TO_DO:
f3dfa40a 2727 break;
85f75dd7
LE
2728 case SS_ALREADY_STANDALONE:
2729 return SS_SUCCESS;
2730 case SS_PRIMARY_NOP:
2731 /* Our state checking code wants to see the peer outdated. */
bde89a9e 2732 rv = conn_request_state(connection, NS2(conn, C_DISCONNECTING, pdsk, D_OUTDATED), 0);
2bd5ed5d
PR
2733
2734 if (rv == SS_OUTDATE_WO_CONN) /* lost connection before graceful disconnect succeeded */
bde89a9e 2735 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING), CS_VERBOSE);
2bd5ed5d 2736
85f75dd7
LE
2737 break;
2738 case SS_CW_FAILED_BY_PEER:
be80ff88
LE
2739 spin_lock_irq(&connection->resource->req_lock);
2740 cstate = connection->cstate;
2741 spin_unlock_irq(&connection->resource->req_lock);
2742 if (cstate <= C_WF_CONNECTION)
2743 goto repeat;
85f75dd7 2744 /* The peer probably wants to see us outdated. */
bde89a9e 2745 rv = conn_request_state(connection, NS2(conn, C_DISCONNECTING,
85f75dd7
LE
2746 disk, D_OUTDATED), 0);
2747 if (rv == SS_IS_DISKLESS || rv == SS_LOWER_THAN_OUTDATED) {
bde89a9e 2748 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING),
f3dfa40a 2749 CS_HARD);
b411b363 2750 }
85f75dd7
LE
2751 break;
2752 default:;
2753 /* no special handling necessary */
2754 }
2755
f3dfa40a
LE
2756 if (rv >= SS_SUCCESS) {
2757 enum drbd_state_rv rv2;
2758 /* No one else can reconfigure the network while I am here.
2759 * The state handling only uses drbd_thread_stop_nowait(),
2760 * we want to really wait here until the receiver is no more.
2761 */
9693da23 2762 drbd_thread_stop(&connection->receiver);
f3dfa40a
LE
2763
2764 /* Race breaker. This additional state change request may be
2765 * necessary, if this was a forced disconnect during a receiver
2766 * restart. We may have "killed" the receiver thread just
8fe60551 2767 * after drbd_receiver() returned. Typically, we should be
f3dfa40a
LE
2768 * C_STANDALONE already, now, and this becomes a no-op.
2769 */
bde89a9e 2770 rv2 = conn_request_state(connection, NS(conn, C_STANDALONE),
f3dfa40a
LE
2771 CS_VERBOSE | CS_HARD);
2772 if (rv2 < SS_SUCCESS)
1ec861eb 2773 drbd_err(connection,
f3dfa40a
LE
2774 "unexpected rv2=%d in conn_try_disconnect()\n",
2775 rv2);
a2972846
AG
2776 /* Unlike in DRBD 9, the state engine has generated
2777 * NOTIFY_DESTROY events before clearing connection->net_conf. */
b411b363 2778 }
85f75dd7
LE
2779 return rv;
2780}
b411b363 2781
3b98c0c2 2782int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info)
b411b363 2783{
a910b123 2784 struct drbd_config_context adm_ctx;
3b98c0c2 2785 struct disconnect_parms parms;
bde89a9e 2786 struct drbd_connection *connection;
85f75dd7 2787 enum drbd_state_rv rv;
3b98c0c2
LE
2788 enum drbd_ret_code retcode;
2789 int err;
2561b9c1 2790
a910b123 2791 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_CONNECTION);
3b98c0c2
LE
2792 if (!adm_ctx.reply_skb)
2793 return retcode;
2794 if (retcode != NO_ERROR)
2561b9c1 2795 goto fail;
b411b363 2796
bde89a9e 2797 connection = adm_ctx.connection;
3b98c0c2
LE
2798 memset(&parms, 0, sizeof(parms));
2799 if (info->attrs[DRBD_NLA_DISCONNECT_PARMS]) {
f399002e 2800 err = disconnect_parms_from_attrs(&parms, info);
3b98c0c2
LE
2801 if (err) {
2802 retcode = ERR_MANDATORY_TAG;
a910b123 2803 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
b411b363
PR
2804 goto fail;
2805 }
2806 }
2807
9e276872 2808 mutex_lock(&adm_ctx.resource->adm_mutex);
bde89a9e 2809 rv = conn_try_disconnect(connection, parms.force_disconnect);
85f75dd7 2810 if (rv < SS_SUCCESS)
f3dfa40a
LE
2811 retcode = rv; /* FIXME: Type mismatch. */
2812 else
2813 retcode = NO_ERROR;
9e276872 2814 mutex_unlock(&adm_ctx.resource->adm_mutex);
b411b363 2815 fail:
a910b123 2816 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
2817 return 0;
2818}
2819
b30ab791 2820void resync_after_online_grow(struct drbd_device *device)
b411b363
PR
2821{
2822 int iass; /* I am sync source */
2823
d0180171 2824 drbd_info(device, "Resync of new storage after online grow\n");
b30ab791
AG
2825 if (device->state.role != device->state.peer)
2826 iass = (device->state.role == R_PRIMARY);
b411b363 2827 else
a6b32bc3 2828 iass = test_bit(RESOLVE_CONFLICTS, &first_peer_device(device)->connection->flags);
b411b363
PR
2829
2830 if (iass)
b30ab791 2831 drbd_start_resync(device, C_SYNC_SOURCE);
b411b363 2832 else
b30ab791 2833 _drbd_request_state(device, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE + CS_SERIALIZE);
b411b363
PR
2834}
2835
3b98c0c2 2836int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info)
b411b363 2837{
a910b123 2838 struct drbd_config_context adm_ctx;
daeda1cc 2839 struct disk_conf *old_disk_conf, *new_disk_conf = NULL;
3b98c0c2 2840 struct resize_parms rs;
b30ab791 2841 struct drbd_device *device;
3b98c0c2 2842 enum drbd_ret_code retcode;
b411b363 2843 enum determine_dev_size dd;
d752b269 2844 bool change_al_layout = false;
6495d2c6 2845 enum dds_flags ddsf;
daeda1cc 2846 sector_t u_size;
3b98c0c2 2847 int err;
b411b363 2848
a910b123 2849 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3b98c0c2
LE
2850 if (!adm_ctx.reply_skb)
2851 return retcode;
2852 if (retcode != NO_ERROR)
9e276872 2853 goto finish;
3b98c0c2 2854
9e276872 2855 mutex_lock(&adm_ctx.resource->adm_mutex);
b30ab791
AG
2856 device = adm_ctx.device;
2857 if (!get_ldev(device)) {
d752b269
PR
2858 retcode = ERR_NO_DISK;
2859 goto fail;
2860 }
2861
3b98c0c2 2862 memset(&rs, 0, sizeof(struct resize_parms));
b30ab791
AG
2863 rs.al_stripes = device->ldev->md.al_stripes;
2864 rs.al_stripe_size = device->ldev->md.al_stripe_size_4k * 4;
3b98c0c2 2865 if (info->attrs[DRBD_NLA_RESIZE_PARMS]) {
f399002e 2866 err = resize_parms_from_attrs(&rs, info);
b411b363 2867 if (err) {
3b98c0c2 2868 retcode = ERR_MANDATORY_TAG;
a910b123 2869 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
d752b269 2870 goto fail_ldev;
b411b363
PR
2871 }
2872 }
2873
b30ab791 2874 if (device->state.conn > C_CONNECTED) {
b411b363 2875 retcode = ERR_RESIZE_RESYNC;
d752b269 2876 goto fail_ldev;
b411b363 2877 }
b411b363 2878
b30ab791
AG
2879 if (device->state.role == R_SECONDARY &&
2880 device->state.peer == R_SECONDARY) {
b411b363 2881 retcode = ERR_NO_PRIMARY;
d752b269 2882 goto fail_ldev;
b411b363 2883 }
b411b363 2884
a6b32bc3 2885 if (rs.no_resync && first_peer_device(device)->connection->agreed_pro_version < 93) {
6495d2c6 2886 retcode = ERR_NEED_APV_93;
9bcd2521 2887 goto fail_ldev;
6495d2c6
PR
2888 }
2889
daeda1cc 2890 rcu_read_lock();
b30ab791 2891 u_size = rcu_dereference(device->ldev->disk_conf)->disk_size;
daeda1cc
PR
2892 rcu_read_unlock();
2893 if (u_size != (sector_t)rs.resize_size) {
2894 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
2895 if (!new_disk_conf) {
778f271d 2896 retcode = ERR_NOMEM;
9bcd2521 2897 goto fail_ldev;
778f271d
PR
2898 }
2899 }
2900
b30ab791
AG
2901 if (device->ldev->md.al_stripes != rs.al_stripes ||
2902 device->ldev->md.al_stripe_size_4k != rs.al_stripe_size / 4) {
d752b269
PR
2903 u32 al_size_k = rs.al_stripes * rs.al_stripe_size;
2904
2905 if (al_size_k > (16 * 1024 * 1024)) {
2906 retcode = ERR_MD_LAYOUT_TOO_BIG;
2907 goto fail_ldev;
2908 }
2909
2910 if (al_size_k < MD_32kB_SECT/2) {
2911 retcode = ERR_MD_LAYOUT_TOO_SMALL;
2912 goto fail_ldev;
2913 }
2914
cdc6af8d 2915 if (device->state.conn != C_CONNECTED && !rs.resize_force) {
d752b269
PR
2916 retcode = ERR_MD_LAYOUT_CONNECTED;
2917 goto fail_ldev;
2918 }
2919
2920 change_al_layout = true;
2921 }
2922
b30ab791
AG
2923 if (device->ldev->known_size != drbd_get_capacity(device->ldev->backing_bdev))
2924 device->ldev->known_size = drbd_get_capacity(device->ldev->backing_bdev);
b411b363 2925
daeda1cc 2926 if (new_disk_conf) {
0500813f 2927 mutex_lock(&device->resource->conf_update);
b30ab791 2928 old_disk_conf = device->ldev->disk_conf;
daeda1cc
PR
2929 *new_disk_conf = *old_disk_conf;
2930 new_disk_conf->disk_size = (sector_t)rs.resize_size;
b30ab791 2931 rcu_assign_pointer(device->ldev->disk_conf, new_disk_conf);
0500813f 2932 mutex_unlock(&device->resource->conf_update);
daeda1cc
PR
2933 synchronize_rcu();
2934 kfree(old_disk_conf);
70644786 2935 new_disk_conf = NULL;
b411b363
PR
2936 }
2937
6495d2c6 2938 ddsf = (rs.resize_force ? DDSF_FORCED : 0) | (rs.no_resync ? DDSF_NO_RESYNC : 0);
b30ab791
AG
2939 dd = drbd_determine_dev_size(device, ddsf, change_al_layout ? &rs : NULL);
2940 drbd_md_sync(device);
2941 put_ldev(device);
e96c9633 2942 if (dd == DS_ERROR) {
b411b363
PR
2943 retcode = ERR_NOMEM_BITMAP;
2944 goto fail;
d752b269
PR
2945 } else if (dd == DS_ERROR_SPACE_MD) {
2946 retcode = ERR_MD_LAYOUT_NO_FIT;
2947 goto fail;
2948 } else if (dd == DS_ERROR_SHRINK) {
2949 retcode = ERR_IMPLICIT_SHRINK;
2950 goto fail;
b411b363 2951 }
778f271d 2952
b30ab791 2953 if (device->state.conn == C_CONNECTED) {
e96c9633 2954 if (dd == DS_GREW)
b30ab791 2955 set_bit(RESIZE_PENDING, &device->flags);
b411b363 2956
69a22773
AG
2957 drbd_send_uuids(first_peer_device(device));
2958 drbd_send_sizes(first_peer_device(device), 1, ddsf);
778f271d
PR
2959 }
2960
b411b363 2961 fail:
9e276872
LE
2962 mutex_unlock(&adm_ctx.resource->adm_mutex);
2963 finish:
a910b123 2964 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363 2965 return 0;
b411b363 2966
9bcd2521 2967 fail_ldev:
b30ab791 2968 put_ldev(device);
70644786 2969 kfree(new_disk_conf);
9bcd2521 2970 goto fail;
b411b363 2971}
b411b363 2972
f399002e 2973int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info)
b411b363 2974{
a910b123 2975 struct drbd_config_context adm_ctx;
3b98c0c2 2976 enum drbd_ret_code retcode;
b57a1e27 2977 struct res_opts res_opts;
f399002e 2978 int err;
b411b363 2979
a910b123 2980 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
3b98c0c2
LE
2981 if (!adm_ctx.reply_skb)
2982 return retcode;
2983 if (retcode != NO_ERROR)
2984 goto fail;
b411b363 2985
eb6bea67 2986 res_opts = adm_ctx.resource->res_opts;
5979e361 2987 if (should_set_defaults(info))
b966b5dd 2988 set_res_opts_defaults(&res_opts);
b411b363 2989
b57a1e27 2990 err = res_opts_from_attrs(&res_opts, info);
c75b9b10 2991 if (err && err != -ENOMSG) {
b411b363 2992 retcode = ERR_MANDATORY_TAG;
a910b123 2993 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
b411b363
PR
2994 goto fail;
2995 }
2996
9e276872 2997 mutex_lock(&adm_ctx.resource->adm_mutex);
eb6bea67 2998 err = set_resource_options(adm_ctx.resource, &res_opts);
afbbfa88
AG
2999 if (err) {
3000 retcode = ERR_INVALID_REQUEST;
3001 if (err == -ENOMEM)
3002 retcode = ERR_NOMEM;
b411b363 3003 }
9e276872 3004 mutex_unlock(&adm_ctx.resource->adm_mutex);
b411b363 3005
b411b363 3006fail:
a910b123 3007 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
3008 return 0;
3009}
3010
3b98c0c2 3011int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info)
b411b363 3012{
a910b123 3013 struct drbd_config_context adm_ctx;
b30ab791 3014 struct drbd_device *device;
3b98c0c2
LE
3015 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
3016
a910b123 3017 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3b98c0c2
LE
3018 if (!adm_ctx.reply_skb)
3019 return retcode;
3020 if (retcode != NO_ERROR)
3021 goto out;
3022
b30ab791 3023 device = adm_ctx.device;
8fe39aac
PR
3024 if (!get_ldev(device)) {
3025 retcode = ERR_NO_DISK;
3026 goto out;
3027 }
3028
3029 mutex_lock(&adm_ctx.resource->adm_mutex);
b411b363 3030
194bfb32 3031 /* If there is still bitmap IO pending, probably because of a previous
7ee1fb93
LE
3032 * resync just being finished, wait for it before requesting a new resync.
3033 * Also wait for it's after_state_ch(). */
b30ab791
AG
3034 drbd_suspend_io(device);
3035 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
b5043c5e 3036 drbd_flush_workqueue(&first_peer_device(device)->connection->sender_work);
194bfb32 3037
0b2dafcd
PR
3038 /* If we happen to be C_STANDALONE R_SECONDARY, just change to
3039 * D_INCONSISTENT, and set all bits in the bitmap. Otherwise,
3040 * try to start a resync handshake as sync target for full sync.
9376d9f8 3041 */
b30ab791
AG
3042 if (device->state.conn == C_STANDALONE && device->state.role == R_SECONDARY) {
3043 retcode = drbd_request_state(device, NS(disk, D_INCONSISTENT));
0b2dafcd 3044 if (retcode >= SS_SUCCESS) {
b30ab791 3045 if (drbd_bitmap_io(device, &drbd_bmio_set_n_write,
0b2dafcd
PR
3046 "set_n_write from invalidate", BM_LOCKED_MASK))
3047 retcode = ERR_IO_MD_DISK;
3048 }
3049 } else
b30ab791
AG
3050 retcode = drbd_request_state(device, NS(conn, C_STARTING_SYNC_T));
3051 drbd_resume_io(device);
9e276872 3052 mutex_unlock(&adm_ctx.resource->adm_mutex);
8fe39aac 3053 put_ldev(device);
3b98c0c2 3054out:
a910b123 3055 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
3056 return 0;
3057}
3058
3b98c0c2
LE
3059static int drbd_adm_simple_request_state(struct sk_buff *skb, struct genl_info *info,
3060 union drbd_state mask, union drbd_state val)
b411b363 3061{
a910b123 3062 struct drbd_config_context adm_ctx;
3b98c0c2 3063 enum drbd_ret_code retcode;
194bfb32 3064
a910b123 3065 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3b98c0c2
LE
3066 if (!adm_ctx.reply_skb)
3067 return retcode;
3068 if (retcode != NO_ERROR)
3069 goto out;
b411b363 3070
9e276872 3071 mutex_lock(&adm_ctx.resource->adm_mutex);
b30ab791 3072 retcode = drbd_request_state(adm_ctx.device, mask, val);
9e276872 3073 mutex_unlock(&adm_ctx.resource->adm_mutex);
3b98c0c2 3074out:
a910b123 3075 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
3076 return 0;
3077}
3078
8fe39aac 3079static int drbd_bmio_set_susp_al(struct drbd_device *device) __must_hold(local)
0778286a
PR
3080{
3081 int rv;
3082
b30ab791
AG
3083 rv = drbd_bmio_set_n_write(device);
3084 drbd_suspend_al(device);
0778286a
PR
3085 return rv;
3086}
3087
3b98c0c2 3088int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info)
b411b363 3089{
a910b123 3090 struct drbd_config_context adm_ctx;
25b0d6c8 3091 int retcode; /* drbd_ret_code, drbd_state_rv */
b30ab791 3092 struct drbd_device *device;
25b0d6c8 3093
a910b123 3094 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
25b0d6c8
PR
3095 if (!adm_ctx.reply_skb)
3096 return retcode;
3097 if (retcode != NO_ERROR)
3098 goto out;
3099
b30ab791 3100 device = adm_ctx.device;
8fe39aac
PR
3101 if (!get_ldev(device)) {
3102 retcode = ERR_NO_DISK;
3103 goto out;
3104 }
3105
3106 mutex_lock(&adm_ctx.resource->adm_mutex);
b411b363 3107
194bfb32 3108 /* If there is still bitmap IO pending, probably because of a previous
7ee1fb93
LE
3109 * resync just being finished, wait for it before requesting a new resync.
3110 * Also wait for it's after_state_ch(). */
b30ab791
AG
3111 drbd_suspend_io(device);
3112 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
b5043c5e 3113 drbd_flush_workqueue(&first_peer_device(device)->connection->sender_work);
194bfb32 3114
0b2dafcd
PR
3115 /* If we happen to be C_STANDALONE R_PRIMARY, just set all bits
3116 * in the bitmap. Otherwise, try to start a resync handshake
3117 * as sync source for full sync.
3118 */
b30ab791 3119 if (device->state.conn == C_STANDALONE && device->state.role == R_PRIMARY) {
0b2dafcd
PR
3120 /* The peer will get a resync upon connect anyways. Just make that
3121 into a full resync. */
b30ab791 3122 retcode = drbd_request_state(device, NS(pdsk, D_INCONSISTENT));
0b2dafcd 3123 if (retcode >= SS_SUCCESS) {
b30ab791 3124 if (drbd_bitmap_io(device, &drbd_bmio_set_susp_al,
0b2dafcd
PR
3125 "set_n_write from invalidate_peer",
3126 BM_LOCKED_SET_ALLOWED))
3127 retcode = ERR_IO_MD_DISK;
3128 }
3129 } else
b30ab791
AG
3130 retcode = drbd_request_state(device, NS(conn, C_STARTING_SYNC_S));
3131 drbd_resume_io(device);
9e276872 3132 mutex_unlock(&adm_ctx.resource->adm_mutex);
8fe39aac 3133 put_ldev(device);
25b0d6c8 3134out:
a910b123 3135 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
3136 return 0;
3137}
3138
3b98c0c2 3139int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info)
b411b363 3140{
a910b123 3141 struct drbd_config_context adm_ctx;
3b98c0c2 3142 enum drbd_ret_code retcode;
b411b363 3143
a910b123 3144 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3b98c0c2
LE
3145 if (!adm_ctx.reply_skb)
3146 return retcode;
3147 if (retcode != NO_ERROR)
3148 goto out;
b411b363 3149
9e276872 3150 mutex_lock(&adm_ctx.resource->adm_mutex);
b30ab791 3151 if (drbd_request_state(adm_ctx.device, NS(user_isp, 1)) == SS_NOTHING_TO_DO)
3b98c0c2 3152 retcode = ERR_PAUSE_IS_SET;
9e276872 3153 mutex_unlock(&adm_ctx.resource->adm_mutex);
3b98c0c2 3154out:
a910b123 3155 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
3156 return 0;
3157}
3158
3b98c0c2 3159int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info)
b411b363 3160{
a910b123 3161 struct drbd_config_context adm_ctx;
da9fbc27 3162 union drbd_dev_state s;
3b98c0c2
LE
3163 enum drbd_ret_code retcode;
3164
a910b123 3165 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3b98c0c2
LE
3166 if (!adm_ctx.reply_skb)
3167 return retcode;
3168 if (retcode != NO_ERROR)
3169 goto out;
b411b363 3170
9e276872 3171 mutex_lock(&adm_ctx.resource->adm_mutex);
b30ab791
AG
3172 if (drbd_request_state(adm_ctx.device, NS(user_isp, 0)) == SS_NOTHING_TO_DO) {
3173 s = adm_ctx.device->state;
cd88d030
PR
3174 if (s.conn == C_PAUSED_SYNC_S || s.conn == C_PAUSED_SYNC_T) {
3175 retcode = s.aftr_isp ? ERR_PIC_AFTER_DEP :
3176 s.peer_isp ? ERR_PIC_PEER_DEP : ERR_PAUSE_IS_CLEAR;
3177 } else {
3178 retcode = ERR_PAUSE_IS_CLEAR;
3179 }
3180 }
9e276872 3181 mutex_unlock(&adm_ctx.resource->adm_mutex);
3b98c0c2 3182out:
a910b123 3183 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
3184 return 0;
3185}
3186
3b98c0c2 3187int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info)
b411b363 3188{
3b98c0c2 3189 return drbd_adm_simple_request_state(skb, info, NS(susp, 1));
b411b363
PR
3190}
3191
3b98c0c2 3192int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info)
b411b363 3193{
a910b123 3194 struct drbd_config_context adm_ctx;
b30ab791 3195 struct drbd_device *device;
3b98c0c2
LE
3196 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
3197
a910b123 3198 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3b98c0c2
LE
3199 if (!adm_ctx.reply_skb)
3200 return retcode;
3201 if (retcode != NO_ERROR)
3202 goto out;
3203
9e276872 3204 mutex_lock(&adm_ctx.resource->adm_mutex);
b30ab791
AG
3205 device = adm_ctx.device;
3206 if (test_bit(NEW_CUR_UUID, &device->flags)) {
9fa48269
LE
3207 if (get_ldev_if_state(device, D_ATTACHING)) {
3208 drbd_uuid_new_current(device);
3209 put_ldev(device);
3210 } else {
3211 /* This is effectively a multi-stage "forced down".
3212 * The NEW_CUR_UUID bit is supposedly only set, if we
3213 * lost the replication connection, and are configured
3214 * to freeze IO and wait for some fence-peer handler.
3215 * So we still don't have a replication connection.
3216 * And now we don't have a local disk either. After
3217 * resume, we will fail all pending and new IO, because
3218 * we don't have any data anymore. Which means we will
3219 * eventually be able to terminate all users of this
3220 * device, and then take it down. By bumping the
3221 * "effective" data uuid, we make sure that you really
3222 * need to tear down before you reconfigure, we will
3223 * the refuse to re-connect or re-attach (because no
3224 * matching real data uuid exists).
3225 */
3226 u64 val;
3227 get_random_bytes(&val, sizeof(u64));
3228 drbd_set_ed_uuid(device, val);
3229 drbd_warn(device, "Resumed without access to data; please tear down before attempting to re-configure.\n");
3230 }
b30ab791 3231 clear_bit(NEW_CUR_UUID, &device->flags);
43a5182c 3232 }
b30ab791
AG
3233 drbd_suspend_io(device);
3234 retcode = drbd_request_state(device, NS3(susp, 0, susp_nod, 0, susp_fen, 0));
3b98c0c2 3235 if (retcode == SS_SUCCESS) {
b30ab791 3236 if (device->state.conn < C_CONNECTED)
a6b32bc3 3237 tl_clear(first_peer_device(device)->connection);
b30ab791 3238 if (device->state.disk == D_DISKLESS || device->state.disk == D_FAILED)
a6b32bc3 3239 tl_restart(first_peer_device(device)->connection, FAIL_FROZEN_DISK_IO);
265be2d0 3240 }
b30ab791 3241 drbd_resume_io(device);
9e276872 3242 mutex_unlock(&adm_ctx.resource->adm_mutex);
3b98c0c2 3243out:
a910b123 3244 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
3245 return 0;
3246}
3247
3b98c0c2 3248int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info)
b411b363 3249{
3b98c0c2 3250 return drbd_adm_simple_request_state(skb, info, NS(disk, D_OUTDATED));
b411b363
PR
3251}
3252
251b8f8e
AG
3253static int nla_put_drbd_cfg_context(struct sk_buff *skb,
3254 struct drbd_resource *resource,
3255 struct drbd_connection *connection,
3256 struct drbd_device *device)
b411b363 3257{
543cc10b 3258 struct nlattr *nla;
ae0be8de 3259 nla = nla_nest_start_noflag(skb, DRBD_NLA_CFG_CONTEXT);
543cc10b
LE
3260 if (!nla)
3261 goto nla_put_failure;
251b8f8e
AG
3262 if (device &&
3263 nla_put_u32(skb, T_ctx_volume, device->vnr))
26ec9287 3264 goto nla_put_failure;
f597f6b8 3265 if (nla_put_string(skb, T_ctx_resource_name, resource->name))
26ec9287 3266 goto nla_put_failure;
251b8f8e
AG
3267 if (connection) {
3268 if (connection->my_addr_len &&
3269 nla_put(skb, T_ctx_my_addr, connection->my_addr_len, &connection->my_addr))
3270 goto nla_put_failure;
3271 if (connection->peer_addr_len &&
3272 nla_put(skb, T_ctx_peer_addr, connection->peer_addr_len, &connection->peer_addr))
3273 goto nla_put_failure;
3274 }
543cc10b
LE
3275 nla_nest_end(skb, nla);
3276 return 0;
b411b363 3277
543cc10b
LE
3278nla_put_failure:
3279 if (nla)
3280 nla_nest_cancel(skb, nla);
3281 return -EMSGSIZE;
3282}
b411b363 3283
a55bbd37
AG
3284/*
3285 * The generic netlink dump callbacks are called outside the genl_lock(), so
3286 * they cannot use the simple attribute parsing code which uses global
3287 * attribute tables.
3288 */
3289static struct nlattr *find_cfg_context_attr(const struct nlmsghdr *nlh, int attr)
3290{
3291 const unsigned hdrlen = GENL_HDRLEN + GENL_MAGIC_FAMILY_HDRSZ;
3292 const int maxtype = ARRAY_SIZE(drbd_cfg_context_nl_policy) - 1;
3293 struct nlattr *nla;
3294
3295 nla = nla_find(nlmsg_attrdata(nlh, hdrlen), nlmsg_attrlen(nlh, hdrlen),
3296 DRBD_NLA_CFG_CONTEXT);
3297 if (!nla)
3298 return NULL;
3299 return drbd_nla_find_nested(maxtype, nla, __nla_type(attr));
3300}
3301
3302static void resource_to_info(struct resource_info *, struct drbd_resource *);
3303
3304int drbd_adm_dump_resources(struct sk_buff *skb, struct netlink_callback *cb)
3305{
3306 struct drbd_genlmsghdr *dh;
3307 struct drbd_resource *resource;
3308 struct resource_info resource_info;
3309 struct resource_statistics resource_statistics;
3310 int err;
3311
3312 rcu_read_lock();
3313 if (cb->args[0]) {
3314 for_each_resource_rcu(resource, &drbd_resources)
3315 if (resource == (struct drbd_resource *)cb->args[0])
3316 goto found_resource;
3317 err = 0; /* resource was probably deleted */
3318 goto out;
3319 }
3320 resource = list_entry(&drbd_resources,
3321 struct drbd_resource, resources);
3322
3323found_resource:
3324 list_for_each_entry_continue_rcu(resource, &drbd_resources, resources) {
3325 goto put_result;
3326 }
3327 err = 0;
3328 goto out;
3329
3330put_result:
3331 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
3332 cb->nlh->nlmsg_seq, &drbd_genl_family,
3333 NLM_F_MULTI, DRBD_ADM_GET_RESOURCES);
3334 err = -ENOMEM;
3335 if (!dh)
3336 goto out;
3337 dh->minor = -1U;
3338 dh->ret_code = NO_ERROR;
3339 err = nla_put_drbd_cfg_context(skb, resource, NULL, NULL);
3340 if (err)
3341 goto out;
3342 err = res_opts_to_skb(skb, &resource->res_opts, !capable(CAP_SYS_ADMIN));
3343 if (err)
3344 goto out;
3345 resource_to_info(&resource_info, resource);
3346 err = resource_info_to_skb(skb, &resource_info, !capable(CAP_SYS_ADMIN));
3347 if (err)
3348 goto out;
3349 resource_statistics.res_stat_write_ordering = resource->write_ordering;
3350 err = resource_statistics_to_skb(skb, &resource_statistics, !capable(CAP_SYS_ADMIN));
3351 if (err)
3352 goto out;
3353 cb->args[0] = (long)resource;
3354 genlmsg_end(skb, dh);
3355 err = 0;
3356
3357out:
3358 rcu_read_unlock();
3359 if (err)
3360 return err;
3361 return skb->len;
3362}
3363
3364static void device_to_statistics(struct device_statistics *s,
3365 struct drbd_device *device)
3366{
3367 memset(s, 0, sizeof(*s));
3368 s->dev_upper_blocked = !may_inc_ap_bio(device);
3369 if (get_ldev(device)) {
3370 struct drbd_md *md = &device->ldev->md;
3371 u64 *history_uuids = (u64 *)s->history_uuids;
3372 struct request_queue *q;
3373 int n;
3374
3375 spin_lock_irq(&md->uuid_lock);
3376 s->dev_current_uuid = md->uuid[UI_CURRENT];
3377 BUILD_BUG_ON(sizeof(s->history_uuids) < UI_HISTORY_END - UI_HISTORY_START + 1);
3378 for (n = 0; n < UI_HISTORY_END - UI_HISTORY_START + 1; n++)
3379 history_uuids[n] = md->uuid[UI_HISTORY_START + n];
3380 for (; n < HISTORY_UUIDS; n++)
3381 history_uuids[n] = 0;
3382 s->history_uuids_len = HISTORY_UUIDS;
3383 spin_unlock_irq(&md->uuid_lock);
3384
3385 s->dev_disk_flags = md->flags;
3386 q = bdev_get_queue(device->ldev->backing_bdev);
3387 s->dev_lower_blocked =
dc3b17cc 3388 bdi_congested(q->backing_dev_info,
a55bbd37
AG
3389 (1 << WB_async_congested) |
3390 (1 << WB_sync_congested));
3391 put_ldev(device);
3392 }
3393 s->dev_size = drbd_get_capacity(device->this_bdev);
3394 s->dev_read = device->read_cnt;
3395 s->dev_write = device->writ_cnt;
3396 s->dev_al_writes = device->al_writ_cnt;
3397 s->dev_bm_writes = device->bm_writ_cnt;
3398 s->dev_upper_pending = atomic_read(&device->ap_bio_cnt);
3399 s->dev_lower_pending = atomic_read(&device->local_cnt);
3400 s->dev_al_suspended = test_bit(AL_SUSPENDED, &device->flags);
3401 s->dev_exposed_data_uuid = device->ed_uuid;
3402}
3403
3404static int put_resource_in_arg0(struct netlink_callback *cb, int holder_nr)
3405{
3406 if (cb->args[0]) {
3407 struct drbd_resource *resource =
3408 (struct drbd_resource *)cb->args[0];
3409 kref_put(&resource->kref, drbd_destroy_resource);
3410 }
3411
3412 return 0;
3413}
3414
3415int drbd_adm_dump_devices_done(struct netlink_callback *cb) {
3416 return put_resource_in_arg0(cb, 7);
3417}
3418
3419static void device_to_info(struct device_info *, struct drbd_device *);
3420
3421int drbd_adm_dump_devices(struct sk_buff *skb, struct netlink_callback *cb)
3422{
3423 struct nlattr *resource_filter;
3424 struct drbd_resource *resource;
3425 struct drbd_device *uninitialized_var(device);
3426 int minor, err, retcode;
3427 struct drbd_genlmsghdr *dh;
3428 struct device_info device_info;
3429 struct device_statistics device_statistics;
3430 struct idr *idr_to_search;
3431
3432 resource = (struct drbd_resource *)cb->args[0];
3433 if (!cb->args[0] && !cb->args[1]) {
3434 resource_filter = find_cfg_context_attr(cb->nlh, T_ctx_resource_name);
3435 if (resource_filter) {
3436 retcode = ERR_RES_NOT_KNOWN;
3437 resource = drbd_find_resource(nla_data(resource_filter));
3438 if (!resource)
3439 goto put_result;
3440 cb->args[0] = (long)resource;
3441 }
3442 }
3443
3444 rcu_read_lock();
3445 minor = cb->args[1];
3446 idr_to_search = resource ? &resource->devices : &drbd_devices;
3447 device = idr_get_next(idr_to_search, &minor);
3448 if (!device) {
3449 err = 0;
3450 goto out;
3451 }
3452 idr_for_each_entry_continue(idr_to_search, device, minor) {
3453 retcode = NO_ERROR;
3454 goto put_result; /* only one iteration */
3455 }
3456 err = 0;
3457 goto out; /* no more devices */
3458
3459put_result:
3460 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
3461 cb->nlh->nlmsg_seq, &drbd_genl_family,
3462 NLM_F_MULTI, DRBD_ADM_GET_DEVICES);
3463 err = -ENOMEM;
3464 if (!dh)
3465 goto out;
3466 dh->ret_code = retcode;
3467 dh->minor = -1U;
3468 if (retcode == NO_ERROR) {
3469 dh->minor = device->minor;
3470 err = nla_put_drbd_cfg_context(skb, device->resource, NULL, device);
3471 if (err)
3472 goto out;
3473 if (get_ldev(device)) {
3474 struct disk_conf *disk_conf =
3475 rcu_dereference(device->ldev->disk_conf);
3476
3477 err = disk_conf_to_skb(skb, disk_conf, !capable(CAP_SYS_ADMIN));
3478 put_ldev(device);
3479 if (err)
3480 goto out;
3481 }
3482 device_to_info(&device_info, device);
3483 err = device_info_to_skb(skb, &device_info, !capable(CAP_SYS_ADMIN));
3484 if (err)
3485 goto out;
3486
3487 device_to_statistics(&device_statistics, device);
3488 err = device_statistics_to_skb(skb, &device_statistics, !capable(CAP_SYS_ADMIN));
3489 if (err)
3490 goto out;
3491 cb->args[1] = minor + 1;
3492 }
3493 genlmsg_end(skb, dh);
3494 err = 0;
3495
3496out:
3497 rcu_read_unlock();
3498 if (err)
3499 return err;
3500 return skb->len;
3501}
3502
3503int drbd_adm_dump_connections_done(struct netlink_callback *cb)
3504{
3505 return put_resource_in_arg0(cb, 6);
3506}
3507
3508enum { SINGLE_RESOURCE, ITERATE_RESOURCES };
3509
3510int drbd_adm_dump_connections(struct sk_buff *skb, struct netlink_callback *cb)
3511{
3512 struct nlattr *resource_filter;
3513 struct drbd_resource *resource = NULL, *next_resource;
3514 struct drbd_connection *uninitialized_var(connection);
3515 int err = 0, retcode;
3516 struct drbd_genlmsghdr *dh;
3517 struct connection_info connection_info;
3518 struct connection_statistics connection_statistics;
3519
3520 rcu_read_lock();
3521 resource = (struct drbd_resource *)cb->args[0];
3522 if (!cb->args[0]) {
3523 resource_filter = find_cfg_context_attr(cb->nlh, T_ctx_resource_name);
3524 if (resource_filter) {
3525 retcode = ERR_RES_NOT_KNOWN;
3526 resource = drbd_find_resource(nla_data(resource_filter));
3527 if (!resource)
3528 goto put_result;
3529 cb->args[0] = (long)resource;
3530 cb->args[1] = SINGLE_RESOURCE;
3531 }
3532 }
3533 if (!resource) {
3534 if (list_empty(&drbd_resources))
3535 goto out;
3536 resource = list_first_entry(&drbd_resources, struct drbd_resource, resources);
3537 kref_get(&resource->kref);
3538 cb->args[0] = (long)resource;
3539 cb->args[1] = ITERATE_RESOURCES;
3540 }
3541
3542 next_resource:
3543 rcu_read_unlock();
3544 mutex_lock(&resource->conf_update);
3545 rcu_read_lock();
3546 if (cb->args[2]) {
3547 for_each_connection_rcu(connection, resource)
3548 if (connection == (struct drbd_connection *)cb->args[2])
3549 goto found_connection;
3550 /* connection was probably deleted */
3551 goto no_more_connections;
3552 }
3553 connection = list_entry(&resource->connections, struct drbd_connection, connections);
3554
3555found_connection:
3556 list_for_each_entry_continue_rcu(connection, &resource->connections, connections) {
3557 if (!has_net_conf(connection))
3558 continue;
3559 retcode = NO_ERROR;
3560 goto put_result; /* only one iteration */
3561 }
3562
3563no_more_connections:
3564 if (cb->args[1] == ITERATE_RESOURCES) {
3565 for_each_resource_rcu(next_resource, &drbd_resources) {
3566 if (next_resource == resource)
3567 goto found_resource;
3568 }
3569 /* resource was probably deleted */
3570 }
3571 goto out;
3572
3573found_resource:
3574 list_for_each_entry_continue_rcu(next_resource, &drbd_resources, resources) {
3575 mutex_unlock(&resource->conf_update);
3576 kref_put(&resource->kref, drbd_destroy_resource);
3577 resource = next_resource;
3578 kref_get(&resource->kref);
3579 cb->args[0] = (long)resource;
3580 cb->args[2] = 0;
3581 goto next_resource;
3582 }
3583 goto out; /* no more resources */
3584
3585put_result:
3586 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
3587 cb->nlh->nlmsg_seq, &drbd_genl_family,
3588 NLM_F_MULTI, DRBD_ADM_GET_CONNECTIONS);
3589 err = -ENOMEM;
3590 if (!dh)
3591 goto out;
3592 dh->ret_code = retcode;
3593 dh->minor = -1U;
3594 if (retcode == NO_ERROR) {
3595 struct net_conf *net_conf;
3596
3597 err = nla_put_drbd_cfg_context(skb, resource, connection, NULL);
3598 if (err)
3599 goto out;
3600 net_conf = rcu_dereference(connection->net_conf);
3601 if (net_conf) {
3602 err = net_conf_to_skb(skb, net_conf, !capable(CAP_SYS_ADMIN));
3603 if (err)
3604 goto out;
3605 }
3606 connection_to_info(&connection_info, connection);
3607 err = connection_info_to_skb(skb, &connection_info, !capable(CAP_SYS_ADMIN));
3608 if (err)
3609 goto out;
3610 connection_statistics.conn_congested = test_bit(NET_CONGESTED, &connection->flags);
3611 err = connection_statistics_to_skb(skb, &connection_statistics, !capable(CAP_SYS_ADMIN));
3612 if (err)
3613 goto out;
3614 cb->args[2] = (long)connection;
3615 }
3616 genlmsg_end(skb, dh);
3617 err = 0;
3618
3619out:
3620 rcu_read_unlock();
3621 if (resource)
3622 mutex_unlock(&resource->conf_update);
3623 if (err)
3624 return err;
3625 return skb->len;
3626}
3627
3628enum mdf_peer_flag {
3629 MDF_PEER_CONNECTED = 1 << 0,
3630 MDF_PEER_OUTDATED = 1 << 1,
3631 MDF_PEER_FENCING = 1 << 2,
3632 MDF_PEER_FULL_SYNC = 1 << 3,
3633};
3634
3635static void peer_device_to_statistics(struct peer_device_statistics *s,
3636 struct drbd_peer_device *peer_device)
3637{
3638 struct drbd_device *device = peer_device->device;
3639
3640 memset(s, 0, sizeof(*s));
3641 s->peer_dev_received = device->recv_cnt;
3642 s->peer_dev_sent = device->send_cnt;
3643 s->peer_dev_pending = atomic_read(&device->ap_pending_cnt) +
3644 atomic_read(&device->rs_pending_cnt);
3645 s->peer_dev_unacked = atomic_read(&device->unacked_cnt);
3646 s->peer_dev_out_of_sync = drbd_bm_total_weight(device) << (BM_BLOCK_SHIFT - 9);
3647 s->peer_dev_resync_failed = device->rs_failed << (BM_BLOCK_SHIFT - 9);
3648 if (get_ldev(device)) {
3649 struct drbd_md *md = &device->ldev->md;
3650
3651 spin_lock_irq(&md->uuid_lock);
3652 s->peer_dev_bitmap_uuid = md->uuid[UI_BITMAP];
3653 spin_unlock_irq(&md->uuid_lock);
3654 s->peer_dev_flags =
3655 (drbd_md_test_flag(device->ldev, MDF_CONNECTED_IND) ?
3656 MDF_PEER_CONNECTED : 0) +
3657 (drbd_md_test_flag(device->ldev, MDF_CONSISTENT) &&
3658 !drbd_md_test_flag(device->ldev, MDF_WAS_UP_TO_DATE) ?
3659 MDF_PEER_OUTDATED : 0) +
3660 /* FIXME: MDF_PEER_FENCING? */
3661 (drbd_md_test_flag(device->ldev, MDF_FULL_SYNC) ?
3662 MDF_PEER_FULL_SYNC : 0);
3663 put_ldev(device);
3664 }
3665}
3666
3667int drbd_adm_dump_peer_devices_done(struct netlink_callback *cb)
3668{
3669 return put_resource_in_arg0(cb, 9);
3670}
3671
3672int drbd_adm_dump_peer_devices(struct sk_buff *skb, struct netlink_callback *cb)
3673{
3674 struct nlattr *resource_filter;
3675 struct drbd_resource *resource;
3676 struct drbd_device *uninitialized_var(device);
3677 struct drbd_peer_device *peer_device = NULL;
3678 int minor, err, retcode;
3679 struct drbd_genlmsghdr *dh;
3680 struct idr *idr_to_search;
3681
3682 resource = (struct drbd_resource *)cb->args[0];
3683 if (!cb->args[0] && !cb->args[1]) {
3684 resource_filter = find_cfg_context_attr(cb->nlh, T_ctx_resource_name);
3685 if (resource_filter) {
3686 retcode = ERR_RES_NOT_KNOWN;
3687 resource = drbd_find_resource(nla_data(resource_filter));
3688 if (!resource)
3689 goto put_result;
3690 }
3691 cb->args[0] = (long)resource;
3692 }
3693
3694 rcu_read_lock();
3695 minor = cb->args[1];
3696 idr_to_search = resource ? &resource->devices : &drbd_devices;
3697 device = idr_find(idr_to_search, minor);
3698 if (!device) {
3699next_device:
3700 minor++;
3701 cb->args[2] = 0;
3702 device = idr_get_next(idr_to_search, &minor);
3703 if (!device) {
3704 err = 0;
3705 goto out;
3706 }
3707 }
3708 if (cb->args[2]) {
3709 for_each_peer_device(peer_device, device)
3710 if (peer_device == (struct drbd_peer_device *)cb->args[2])
3711 goto found_peer_device;
3712 /* peer device was probably deleted */
3713 goto next_device;
3714 }
3715 /* Make peer_device point to the list head (not the first entry). */
3716 peer_device = list_entry(&device->peer_devices, struct drbd_peer_device, peer_devices);
3717
3718found_peer_device:
3719 list_for_each_entry_continue_rcu(peer_device, &device->peer_devices, peer_devices) {
3720 if (!has_net_conf(peer_device->connection))
3721 continue;
3722 retcode = NO_ERROR;
3723 goto put_result; /* only one iteration */
3724 }
3725 goto next_device;
3726
3727put_result:
3728 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
3729 cb->nlh->nlmsg_seq, &drbd_genl_family,
3730 NLM_F_MULTI, DRBD_ADM_GET_PEER_DEVICES);
3731 err = -ENOMEM;
3732 if (!dh)
3733 goto out;
3734 dh->ret_code = retcode;
3735 dh->minor = -1U;
3736 if (retcode == NO_ERROR) {
3737 struct peer_device_info peer_device_info;
3738 struct peer_device_statistics peer_device_statistics;
3739
3740 dh->minor = minor;
3741 err = nla_put_drbd_cfg_context(skb, device->resource, peer_device->connection, device);
3742 if (err)
3743 goto out;
3744 peer_device_to_info(&peer_device_info, peer_device);
3745 err = peer_device_info_to_skb(skb, &peer_device_info, !capable(CAP_SYS_ADMIN));
3746 if (err)
3747 goto out;
3748 peer_device_to_statistics(&peer_device_statistics, peer_device);
3749 err = peer_device_statistics_to_skb(skb, &peer_device_statistics, !capable(CAP_SYS_ADMIN));
3750 if (err)
3751 goto out;
3752 cb->args[1] = minor;
3753 cb->args[2] = (long)peer_device;
3754 }
3755 genlmsg_end(skb, dh);
3756 err = 0;
3757
3758out:
3759 rcu_read_unlock();
3760 if (err)
3761 return err;
3762 return skb->len;
3763}
251b8f8e
AG
3764/*
3765 * Return the connection of @resource if @resource has exactly one connection.
3766 */
3767static struct drbd_connection *the_only_connection(struct drbd_resource *resource)
3768{
3769 struct list_head *connections = &resource->connections;
3770
3771 if (list_empty(connections) || connections->next->next != connections)
3772 return NULL;
3773 return list_first_entry(&resource->connections, struct drbd_connection, connections);
3774}
3775
8ce953aa 3776static int nla_put_status_info(struct sk_buff *skb, struct drbd_device *device,
3b98c0c2 3777 const struct sib_info *sib)
b411b363 3778{
251b8f8e 3779 struct drbd_resource *resource = device->resource;
3b98c0c2
LE
3780 struct state_info *si = NULL; /* for sizeof(si->member); */
3781 struct nlattr *nla;
3782 int got_ldev;
3b98c0c2
LE
3783 int err = 0;
3784 int exclude_sensitive;
3785
3786 /* If sib != NULL, this is drbd_bcast_event, which anyone can listen
3787 * to. So we better exclude_sensitive information.
3788 *
3789 * If sib == NULL, this is drbd_adm_get_status, executed synchronously
3790 * in the context of the requesting user process. Exclude sensitive
3791 * information, unless current has superuser.
3792 *
3793 * NOTE: for drbd_adm_get_status_all(), this is a netlink dump, and
3794 * relies on the current implementation of netlink_dump(), which
3795 * executes the dump callback successively from netlink_recvmsg(),
3796 * always in the context of the receiving process */
3797 exclude_sensitive = sib || !capable(CAP_SYS_ADMIN);
3798
b30ab791 3799 got_ldev = get_ldev(device);
3b98c0c2
LE
3800
3801 /* We need to add connection name and volume number information still.
3802 * Minor number is in drbd_genlmsghdr. */
251b8f8e 3803 if (nla_put_drbd_cfg_context(skb, resource, the_only_connection(resource), device))
3b98c0c2 3804 goto nla_put_failure;
3b98c0c2 3805
eb6bea67 3806 if (res_opts_to_skb(skb, &device->resource->res_opts, exclude_sensitive))
f399002e
LE
3807 goto nla_put_failure;
3808
daeda1cc 3809 rcu_read_lock();
f9eb7bf4
AG
3810 if (got_ldev) {
3811 struct disk_conf *disk_conf;
44ed167d 3812
b30ab791 3813 disk_conf = rcu_dereference(device->ldev->disk_conf);
f9eb7bf4
AG
3814 err = disk_conf_to_skb(skb, disk_conf, exclude_sensitive);
3815 }
3816 if (!err) {
3817 struct net_conf *nc;
3818
a6b32bc3 3819 nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
f9eb7bf4
AG
3820 if (nc)
3821 err = net_conf_to_skb(skb, nc, exclude_sensitive);
3822 }
44ed167d
PR
3823 rcu_read_unlock();
3824 if (err)
3825 goto nla_put_failure;
3b98c0c2 3826
ae0be8de 3827 nla = nla_nest_start_noflag(skb, DRBD_NLA_STATE_INFO);
3b98c0c2
LE
3828 if (!nla)
3829 goto nla_put_failure;
26ec9287 3830 if (nla_put_u32(skb, T_sib_reason, sib ? sib->sib_reason : SIB_GET_STATUS_REPLY) ||
b30ab791 3831 nla_put_u32(skb, T_current_state, device->state.i) ||
1dee3f59
ND
3832 nla_put_u64_0pad(skb, T_ed_uuid, device->ed_uuid) ||
3833 nla_put_u64_0pad(skb, T_capacity,
3834 drbd_get_capacity(device->this_bdev)) ||
3835 nla_put_u64_0pad(skb, T_send_cnt, device->send_cnt) ||
3836 nla_put_u64_0pad(skb, T_recv_cnt, device->recv_cnt) ||
3837 nla_put_u64_0pad(skb, T_read_cnt, device->read_cnt) ||
3838 nla_put_u64_0pad(skb, T_writ_cnt, device->writ_cnt) ||
3839 nla_put_u64_0pad(skb, T_al_writ_cnt, device->al_writ_cnt) ||
3840 nla_put_u64_0pad(skb, T_bm_writ_cnt, device->bm_writ_cnt) ||
b30ab791
AG
3841 nla_put_u32(skb, T_ap_bio_cnt, atomic_read(&device->ap_bio_cnt)) ||
3842 nla_put_u32(skb, T_ap_pending_cnt, atomic_read(&device->ap_pending_cnt)) ||
3843 nla_put_u32(skb, T_rs_pending_cnt, atomic_read(&device->rs_pending_cnt)))
26ec9287 3844 goto nla_put_failure;
3b98c0c2
LE
3845
3846 if (got_ldev) {
39a1aa7f 3847 int err;
b411b363 3848
b30ab791
AG
3849 spin_lock_irq(&device->ldev->md.uuid_lock);
3850 err = nla_put(skb, T_uuids, sizeof(si->uuids), device->ldev->md.uuid);
3851 spin_unlock_irq(&device->ldev->md.uuid_lock);
39a1aa7f
PR
3852
3853 if (err)
3854 goto nla_put_failure;
3855
b30ab791 3856 if (nla_put_u32(skb, T_disk_flags, device->ldev->md.flags) ||
1dee3f59
ND
3857 nla_put_u64_0pad(skb, T_bits_total, drbd_bm_bits(device)) ||
3858 nla_put_u64_0pad(skb, T_bits_oos,
3859 drbd_bm_total_weight(device)))
26ec9287 3860 goto nla_put_failure;
b30ab791
AG
3861 if (C_SYNC_SOURCE <= device->state.conn &&
3862 C_PAUSED_SYNC_T >= device->state.conn) {
1dee3f59
ND
3863 if (nla_put_u64_0pad(skb, T_bits_rs_total,
3864 device->rs_total) ||
3865 nla_put_u64_0pad(skb, T_bits_rs_failed,
3866 device->rs_failed))
26ec9287 3867 goto nla_put_failure;
3b98c0c2 3868 }
b411b363 3869 }
b411b363 3870
3b98c0c2
LE
3871 if (sib) {
3872 switch(sib->sib_reason) {
3873 case SIB_SYNC_PROGRESS:
3874 case SIB_GET_STATUS_REPLY:
3875 break;
3876 case SIB_STATE_CHANGE:
26ec9287
AG
3877 if (nla_put_u32(skb, T_prev_state, sib->os.i) ||
3878 nla_put_u32(skb, T_new_state, sib->ns.i))
3879 goto nla_put_failure;
3b98c0c2
LE
3880 break;
3881 case SIB_HELPER_POST:
26ec9287
AG
3882 if (nla_put_u32(skb, T_helper_exit_code,
3883 sib->helper_exit_code))
3884 goto nla_put_failure;
3b98c0c2
LE
3885 /* fall through */
3886 case SIB_HELPER_PRE:
26ec9287
AG
3887 if (nla_put_string(skb, T_helper, sib->helper_name))
3888 goto nla_put_failure;
3b98c0c2
LE
3889 break;
3890 }
b411b363 3891 }
3b98c0c2 3892 nla_nest_end(skb, nla);
b411b363 3893
3b98c0c2
LE
3894 if (0)
3895nla_put_failure:
3896 err = -EMSGSIZE;
3897 if (got_ldev)
b30ab791 3898 put_ldev(device);
3b98c0c2 3899 return err;
b411b363
PR
3900}
3901
3b98c0c2 3902int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info)
b411b363 3903{
a910b123 3904 struct drbd_config_context adm_ctx;
3b98c0c2
LE
3905 enum drbd_ret_code retcode;
3906 int err;
b411b363 3907
a910b123 3908 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3b98c0c2
LE
3909 if (!adm_ctx.reply_skb)
3910 return retcode;
3911 if (retcode != NO_ERROR)
3912 goto out;
b411b363 3913
b30ab791 3914 err = nla_put_status_info(adm_ctx.reply_skb, adm_ctx.device, NULL);
3b98c0c2
LE
3915 if (err) {
3916 nlmsg_free(adm_ctx.reply_skb);
3917 return err;
b411b363 3918 }
3b98c0c2 3919out:
a910b123 3920 drbd_adm_finish(&adm_ctx, info, retcode);
3b98c0c2 3921 return 0;
b411b363
PR
3922}
3923
4b7a530f 3924static int get_one_status(struct sk_buff *skb, struct netlink_callback *cb)
b411b363 3925{
b30ab791 3926 struct drbd_device *device;
3b98c0c2 3927 struct drbd_genlmsghdr *dh;
77c556f6
AG
3928 struct drbd_resource *pos = (struct drbd_resource *)cb->args[0];
3929 struct drbd_resource *resource = NULL;
77c556f6 3930 struct drbd_resource *tmp;
543cc10b
LE
3931 unsigned volume = cb->args[1];
3932
3933 /* Open coded, deferred, iteration:
77c556f6 3934 * for_each_resource_safe(resource, tmp, &drbd_resources) {
251b8f8e
AG
3935 * connection = "first connection of resource or undefined";
3936 * idr_for_each_entry(&resource->devices, device, i) {
543cc10b
LE
3937 * ...
3938 * }
3939 * }
77c556f6 3940 * where resource is cb->args[0];
543cc10b
LE
3941 * and i is cb->args[1];
3942 *
71932efc
LE
3943 * cb->args[2] indicates if we shall loop over all resources,
3944 * or just dump all volumes of a single resource.
3945 *
3b98c0c2
LE
3946 * This may miss entries inserted after this dump started,
3947 * or entries deleted before they are reached.
543cc10b 3948 *
b30ab791 3949 * We need to make sure the device won't disappear while
543cc10b
LE
3950 * we are looking at it, and revalidate our iterators
3951 * on each iteration.
3952 */
b411b363 3953
05a10ec7 3954 /* synchronize with conn_create()/drbd_destroy_connection() */
c141ebda 3955 rcu_read_lock();
543cc10b 3956 /* revalidate iterator position */
77c556f6 3957 for_each_resource_rcu(tmp, &drbd_resources) {
543cc10b
LE
3958 if (pos == NULL) {
3959 /* first iteration */
3960 pos = tmp;
77c556f6 3961 resource = pos;
543cc10b
LE
3962 break;
3963 }
3964 if (tmp == pos) {
77c556f6 3965 resource = pos;
543cc10b
LE
3966 break;
3967 }
b411b363 3968 }
77c556f6
AG
3969 if (resource) {
3970next_resource:
251b8f8e
AG
3971 device = idr_get_next(&resource->devices, &volume);
3972 if (!device) {
77c556f6
AG
3973 /* No more volumes to dump on this resource.
3974 * Advance resource iterator. */
3975 pos = list_entry_rcu(resource->resources.next,
3976 struct drbd_resource, resources);
3977 /* Did we dump any volume of this resource yet? */
543cc10b 3978 if (volume != 0) {
71932efc
LE
3979 /* If we reached the end of the list,
3980 * or only a single resource dump was requested,
3981 * we are done. */
77c556f6 3982 if (&pos->resources == &drbd_resources || cb->args[2])
71932efc 3983 goto out;
543cc10b 3984 volume = 0;
77c556f6
AG
3985 resource = pos;
3986 goto next_resource;
543cc10b
LE
3987 }
3988 }
3989
98683650 3990 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
3b98c0c2
LE
3991 cb->nlh->nlmsg_seq, &drbd_genl_family,
3992 NLM_F_MULTI, DRBD_ADM_GET_STATUS);
3993 if (!dh)
543cc10b
LE
3994 goto out;
3995
251b8f8e 3996 if (!device) {
bde89a9e 3997 /* This is a connection without a single volume.
367d675d
LE
3998 * Suprisingly enough, it may have a network
3999 * configuration. */
251b8f8e
AG
4000 struct drbd_connection *connection;
4001
543cc10b
LE
4002 dh->minor = -1U;
4003 dh->ret_code = NO_ERROR;
251b8f8e
AG
4004 connection = the_only_connection(resource);
4005 if (nla_put_drbd_cfg_context(skb, resource, connection, NULL))
367d675d 4006 goto cancel;
251b8f8e
AG
4007 if (connection) {
4008 struct net_conf *nc;
4009
4010 nc = rcu_dereference(connection->net_conf);
4011 if (nc && net_conf_to_skb(skb, nc, 1) != 0)
4012 goto cancel;
4013 }
367d675d 4014 goto done;
543cc10b 4015 }
b411b363 4016
0b0ba1ef 4017 D_ASSERT(device, device->vnr == volume);
251b8f8e 4018 D_ASSERT(device, device->resource == resource);
3b98c0c2 4019
b30ab791 4020 dh->minor = device_to_minor(device);
3b98c0c2
LE
4021 dh->ret_code = NO_ERROR;
4022
b30ab791 4023 if (nla_put_status_info(skb, device, NULL)) {
367d675d 4024cancel:
3b98c0c2 4025 genlmsg_cancel(skb, dh);
543cc10b 4026 goto out;
3b98c0c2 4027 }
367d675d 4028done:
3b98c0c2 4029 genlmsg_end(skb, dh);
bde89a9e 4030 }
b411b363 4031
543cc10b 4032out:
c141ebda 4033 rcu_read_unlock();
543cc10b 4034 /* where to start the next iteration */
bde89a9e 4035 cb->args[0] = (long)pos;
77c556f6 4036 cb->args[1] = (pos == resource) ? volume + 1 : 0;
b411b363 4037
77c556f6 4038 /* No more resources/volumes/minors found results in an empty skb.
543cc10b 4039 * Which will terminate the dump. */
3b98c0c2 4040 return skb->len;
b411b363
PR
4041}
4042
71932efc
LE
4043/*
4044 * Request status of all resources, or of all volumes within a single resource.
4045 *
4046 * This is a dump, as the answer may not fit in a single reply skb otherwise.
4047 * Which means we cannot use the family->attrbuf or other such members, because
4048 * dump is NOT protected by the genl_lock(). During dump, we only have access
4049 * to the incoming skb, and need to opencode "parsing" of the nlattr payload.
4050 *
4051 * Once things are setup properly, we call into get_one_status().
b411b363 4052 */
71932efc 4053int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb)
b411b363 4054{
71932efc
LE
4055 const unsigned hdrlen = GENL_HDRLEN + GENL_MAGIC_FAMILY_HDRSZ;
4056 struct nlattr *nla;
7c3063cc 4057 const char *resource_name;
4bc76048 4058 struct drbd_resource *resource;
7c3063cc 4059 int maxtype;
71932efc
LE
4060
4061 /* Is this a followup call? */
4062 if (cb->args[0]) {
4063 /* ... of a single resource dump,
4064 * and the resource iterator has been advanced already? */
4065 if (cb->args[2] && cb->args[2] != cb->args[0])
4066 return 0; /* DONE. */
4067 goto dump;
4068 }
4069
4070 /* First call (from netlink_dump_start). We need to figure out
4071 * which resource(s) the user wants us to dump. */
4072 nla = nla_find(nlmsg_attrdata(cb->nlh, hdrlen),
4073 nlmsg_attrlen(cb->nlh, hdrlen),
4074 DRBD_NLA_CFG_CONTEXT);
4075
4076 /* No explicit context given. Dump all. */
4077 if (!nla)
4078 goto dump;
7c3063cc
AG
4079 maxtype = ARRAY_SIZE(drbd_cfg_context_nl_policy) - 1;
4080 nla = drbd_nla_find_nested(maxtype, nla, __nla_type(T_ctx_resource_name));
4081 if (IS_ERR(nla))
4082 return PTR_ERR(nla);
71932efc
LE
4083 /* context given, but no name present? */
4084 if (!nla)
4085 return -EINVAL;
7c3063cc 4086 resource_name = nla_data(nla);
4bc76048
AG
4087 if (!*resource_name)
4088 return -ENODEV;
4089 resource = drbd_find_resource(resource_name);
4090 if (!resource)
71932efc
LE
4091 return -ENODEV;
4092
4bc76048 4093 kref_put(&resource->kref, drbd_destroy_resource); /* get_one_status() revalidates the resource */
0ace9dfa 4094
71932efc 4095 /* prime iterators, and set "filter" mode mark:
bde89a9e 4096 * only dump this connection. */
4bc76048 4097 cb->args[0] = (long)resource;
71932efc 4098 /* cb->args[1] = 0; passed in this way. */
4bc76048 4099 cb->args[2] = (long)resource;
71932efc
LE
4100
4101dump:
4102 return get_one_status(skb, cb);
4103}
b411b363 4104
3b98c0c2 4105int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info)
b411b363 4106{
a910b123 4107 struct drbd_config_context adm_ctx;
3b98c0c2
LE
4108 enum drbd_ret_code retcode;
4109 struct timeout_parms tp;
4110 int err;
b411b363 4111
a910b123 4112 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3b98c0c2
LE
4113 if (!adm_ctx.reply_skb)
4114 return retcode;
4115 if (retcode != NO_ERROR)
4116 goto out;
b411b363 4117
3b98c0c2 4118 tp.timeout_type =
b30ab791
AG
4119 adm_ctx.device->state.pdsk == D_OUTDATED ? UT_PEER_OUTDATED :
4120 test_bit(USE_DEGR_WFC_T, &adm_ctx.device->flags) ? UT_DEGRADED :
3b98c0c2 4121 UT_DEFAULT;
b411b363 4122
3b98c0c2
LE
4123 err = timeout_parms_to_priv_skb(adm_ctx.reply_skb, &tp);
4124 if (err) {
4125 nlmsg_free(adm_ctx.reply_skb);
4126 return err;
4127 }
4128out:
a910b123 4129 drbd_adm_finish(&adm_ctx, info, retcode);
3b98c0c2 4130 return 0;
b411b363
PR
4131}
4132
3b98c0c2 4133int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info)
b411b363 4134{
a910b123 4135 struct drbd_config_context adm_ctx;
b30ab791 4136 struct drbd_device *device;
3b98c0c2 4137 enum drbd_ret_code retcode;
58ffa580 4138 struct start_ov_parms parms;
b411b363 4139
a910b123 4140 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3b98c0c2
LE
4141 if (!adm_ctx.reply_skb)
4142 return retcode;
4143 if (retcode != NO_ERROR)
4144 goto out;
873b0d5f 4145
b30ab791 4146 device = adm_ctx.device;
58ffa580
LE
4147
4148 /* resume from last known position, if possible */
b30ab791 4149 parms.ov_start_sector = device->ov_start_sector;
58ffa580 4150 parms.ov_stop_sector = ULLONG_MAX;
3b98c0c2 4151 if (info->attrs[DRBD_NLA_START_OV_PARMS]) {
f399002e 4152 int err = start_ov_parms_from_attrs(&parms, info);
3b98c0c2
LE
4153 if (err) {
4154 retcode = ERR_MANDATORY_TAG;
a910b123 4155 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
3b98c0c2
LE
4156 goto out;
4157 }
b411b363 4158 }
9e276872
LE
4159 mutex_lock(&adm_ctx.resource->adm_mutex);
4160
58ffa580 4161 /* w_make_ov_request expects position to be aligned */
b30ab791
AG
4162 device->ov_start_sector = parms.ov_start_sector & ~(BM_SECT_PER_BIT-1);
4163 device->ov_stop_sector = parms.ov_stop_sector;
873b0d5f
LE
4164
4165 /* If there is still bitmap IO pending, e.g. previous resync or verify
4166 * just being finished, wait for it before requesting a new resync. */
b30ab791
AG
4167 drbd_suspend_io(device);
4168 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
4169 retcode = drbd_request_state(device, NS(conn, C_VERIFY_S));
4170 drbd_resume_io(device);
9e276872
LE
4171
4172 mutex_unlock(&adm_ctx.resource->adm_mutex);
3b98c0c2 4173out:
a910b123 4174 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
4175 return 0;
4176}
4177
4178
3b98c0c2 4179int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info)
b411b363 4180{
a910b123 4181 struct drbd_config_context adm_ctx;
b30ab791 4182 struct drbd_device *device;
3b98c0c2 4183 enum drbd_ret_code retcode;
b411b363
PR
4184 int skip_initial_sync = 0;
4185 int err;
3b98c0c2 4186 struct new_c_uuid_parms args;
b411b363 4187
a910b123 4188 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3b98c0c2
LE
4189 if (!adm_ctx.reply_skb)
4190 return retcode;
4191 if (retcode != NO_ERROR)
4192 goto out_nolock;
b411b363 4193
b30ab791 4194 device = adm_ctx.device;
3b98c0c2
LE
4195 memset(&args, 0, sizeof(args));
4196 if (info->attrs[DRBD_NLA_NEW_C_UUID_PARMS]) {
f399002e 4197 err = new_c_uuid_parms_from_attrs(&args, info);
3b98c0c2
LE
4198 if (err) {
4199 retcode = ERR_MANDATORY_TAG;
a910b123 4200 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
3b98c0c2
LE
4201 goto out_nolock;
4202 }
b411b363
PR
4203 }
4204
9e276872 4205 mutex_lock(&adm_ctx.resource->adm_mutex);
b30ab791 4206 mutex_lock(device->state_mutex); /* Protects us against serialized state changes. */
b411b363 4207
b30ab791 4208 if (!get_ldev(device)) {
b411b363
PR
4209 retcode = ERR_NO_DISK;
4210 goto out;
4211 }
4212
4213 /* this is "skip initial sync", assume to be clean */
a6b32bc3
AG
4214 if (device->state.conn == C_CONNECTED &&
4215 first_peer_device(device)->connection->agreed_pro_version >= 90 &&
b30ab791 4216 device->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED && args.clear_bm) {
d0180171 4217 drbd_info(device, "Preparing to skip initial sync\n");
b411b363 4218 skip_initial_sync = 1;
b30ab791 4219 } else if (device->state.conn != C_STANDALONE) {
b411b363
PR
4220 retcode = ERR_CONNECTED;
4221 goto out_dec;
4222 }
4223
b30ab791
AG
4224 drbd_uuid_set(device, UI_BITMAP, 0); /* Rotate UI_BITMAP to History 1, etc... */
4225 drbd_uuid_new_current(device); /* New current, previous to UI_BITMAP */
b411b363
PR
4226
4227 if (args.clear_bm) {
b30ab791 4228 err = drbd_bitmap_io(device, &drbd_bmio_clear_n_write,
20ceb2b2 4229 "clear_n_write from new_c_uuid", BM_LOCKED_MASK);
b411b363 4230 if (err) {
d0180171 4231 drbd_err(device, "Writing bitmap failed with %d\n", err);
b411b363
PR
4232 retcode = ERR_IO_MD_DISK;
4233 }
4234 if (skip_initial_sync) {
69a22773 4235 drbd_send_uuids_skip_initial_sync(first_peer_device(device));
b30ab791
AG
4236 _drbd_uuid_set(device, UI_BITMAP, 0);
4237 drbd_print_uuids(device, "cleared bitmap UUID");
0500813f 4238 spin_lock_irq(&device->resource->req_lock);
b30ab791 4239 _drbd_set_state(_NS2(device, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
b411b363 4240 CS_VERBOSE, NULL);
0500813f 4241 spin_unlock_irq(&device->resource->req_lock);
b411b363
PR
4242 }
4243 }
4244
b30ab791 4245 drbd_md_sync(device);
b411b363 4246out_dec:
b30ab791 4247 put_ldev(device);
b411b363 4248out:
b30ab791 4249 mutex_unlock(device->state_mutex);
9e276872 4250 mutex_unlock(&adm_ctx.resource->adm_mutex);
3b98c0c2 4251out_nolock:
a910b123 4252 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
4253 return 0;
4254}
4255
3b98c0c2 4256static enum drbd_ret_code
a910b123 4257drbd_check_resource_name(struct drbd_config_context *adm_ctx)
b411b363 4258{
a910b123 4259 const char *name = adm_ctx->resource_name;
3b98c0c2 4260 if (!name || !name[0]) {
a910b123 4261 drbd_msg_put_info(adm_ctx->reply_skb, "resource name missing");
3b98c0c2 4262 return ERR_MANDATORY_TAG;
b411b363 4263 }
3b98c0c2
LE
4264 /* if we want to use these in sysfs/configfs/debugfs some day,
4265 * we must not allow slashes */
4266 if (strchr(name, '/')) {
a910b123 4267 drbd_msg_put_info(adm_ctx->reply_skb, "invalid resource name");
3b98c0c2 4268 return ERR_INVALID_REQUEST;
b411b363 4269 }
3b98c0c2 4270 return NO_ERROR;
774b3055 4271}
b411b363 4272
a2972846
AG
4273static void resource_to_info(struct resource_info *info,
4274 struct drbd_resource *resource)
4275{
4276 info->res_role = conn_highest_role(first_connection(resource));
4277 info->res_susp = resource->susp;
4278 info->res_susp_nod = resource->susp_nod;
4279 info->res_susp_fen = resource->susp_fen;
4280}
4281
789c1b62 4282int drbd_adm_new_resource(struct sk_buff *skb, struct genl_info *info)
b411b363 4283{
a2972846 4284 struct drbd_connection *connection;
a910b123 4285 struct drbd_config_context adm_ctx;
3b98c0c2 4286 enum drbd_ret_code retcode;
afbbfa88
AG
4287 struct res_opts res_opts;
4288 int err;
b411b363 4289
a910b123 4290 retcode = drbd_adm_prepare(&adm_ctx, skb, info, 0);
3b98c0c2
LE
4291 if (!adm_ctx.reply_skb)
4292 return retcode;
4293 if (retcode != NO_ERROR)
4294 goto out;
b411b363 4295
afbbfa88
AG
4296 set_res_opts_defaults(&res_opts);
4297 err = res_opts_from_attrs(&res_opts, info);
4298 if (err && err != -ENOMSG) {
4299 retcode = ERR_MANDATORY_TAG;
a910b123 4300 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
afbbfa88 4301 goto out;
b411b363
PR
4302 }
4303
a910b123 4304 retcode = drbd_check_resource_name(&adm_ctx);
3b98c0c2
LE
4305 if (retcode != NO_ERROR)
4306 goto out;
b411b363 4307
5c661042 4308 if (adm_ctx.resource) {
38f19616
LE
4309 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL) {
4310 retcode = ERR_INVALID_REQUEST;
a910b123 4311 drbd_msg_put_info(adm_ctx.reply_skb, "resource exists");
38f19616
LE
4312 }
4313 /* else: still NO_ERROR */
3b98c0c2 4314 goto out;
b411b363 4315 }
b411b363 4316
9e276872 4317 /* not yet safe for genl_family.parallel_ops */
28bc3b8c 4318 mutex_lock(&resources_mutex);
a2972846 4319 connection = conn_create(adm_ctx.resource_name, &res_opts);
28bc3b8c 4320 mutex_unlock(&resources_mutex);
a2972846
AG
4321
4322 if (connection) {
4323 struct resource_info resource_info;
4324
4325 mutex_lock(&notification_mutex);
4326 resource_to_info(&resource_info, connection->resource);
4327 notify_resource_state(NULL, 0, connection->resource,
4328 &resource_info, NOTIFY_CREATE);
4329 mutex_unlock(&notification_mutex);
4330 } else
4331 retcode = ERR_NOMEM;
4332
3b98c0c2 4333out:
a910b123 4334 drbd_adm_finish(&adm_ctx, info, retcode);
3b98c0c2 4335 return 0;
b411b363
PR
4336}
4337
a2972846
AG
4338static void device_to_info(struct device_info *info,
4339 struct drbd_device *device)
4340{
4341 info->dev_disk_state = device->state.disk;
4342}
4343
4344
05a10ec7 4345int drbd_adm_new_minor(struct sk_buff *skb, struct genl_info *info)
b411b363 4346{
a910b123 4347 struct drbd_config_context adm_ctx;
3b98c0c2
LE
4348 struct drbd_genlmsghdr *dh = info->userhdr;
4349 enum drbd_ret_code retcode;
b411b363 4350
a910b123 4351 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
3b98c0c2
LE
4352 if (!adm_ctx.reply_skb)
4353 return retcode;
4354 if (retcode != NO_ERROR)
4355 goto out;
b411b363 4356
f2257a56 4357 if (dh->minor > MINORMASK) {
a910b123 4358 drbd_msg_put_info(adm_ctx.reply_skb, "requested minor out of range");
3b98c0c2
LE
4359 retcode = ERR_INVALID_REQUEST;
4360 goto out;
b411b363 4361 }
0c8e36d9 4362 if (adm_ctx.volume > DRBD_VOLUME_MAX) {
a910b123 4363 drbd_msg_put_info(adm_ctx.reply_skb, "requested volume id out of range");
3b98c0c2
LE
4364 retcode = ERR_INVALID_REQUEST;
4365 goto out;
b411b363 4366 }
b411b363 4367
38f19616 4368 /* drbd_adm_prepare made sure already
a6b32bc3 4369 * that first_peer_device(device)->connection and device->vnr match the request. */
b30ab791 4370 if (adm_ctx.device) {
38f19616 4371 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
179e20b8 4372 retcode = ERR_MINOR_OR_VOLUME_EXISTS;
38f19616
LE
4373 /* else: still NO_ERROR */
4374 goto out;
b411b363 4375 }
38f19616 4376
9e276872 4377 mutex_lock(&adm_ctx.resource->adm_mutex);
a910b123 4378 retcode = drbd_create_device(&adm_ctx, dh->minor);
a2972846
AG
4379 if (retcode == NO_ERROR) {
4380 struct drbd_device *device;
4381 struct drbd_peer_device *peer_device;
4382 struct device_info info;
4383 unsigned int peer_devices = 0;
4384 enum drbd_notification_type flags;
4385
4386 device = minor_to_device(dh->minor);
4387 for_each_peer_device(peer_device, device) {
4388 if (!has_net_conf(peer_device->connection))
4389 continue;
4390 peer_devices++;
4391 }
4392
4393 device_to_info(&info, device);
4394 mutex_lock(&notification_mutex);
4395 flags = (peer_devices--) ? NOTIFY_CONTINUES : 0;
4396 notify_device_state(NULL, 0, device, &info, NOTIFY_CREATE | flags);
4397 for_each_peer_device(peer_device, device) {
4398 struct peer_device_info peer_device_info;
4399
4400 if (!has_net_conf(peer_device->connection))
4401 continue;
4402 peer_device_to_info(&peer_device_info, peer_device);
4403 flags = (peer_devices--) ? NOTIFY_CONTINUES : 0;
4404 notify_peer_device_state(NULL, 0, peer_device, &peer_device_info,
4405 NOTIFY_CREATE | flags);
4406 }
4407 mutex_unlock(&notification_mutex);
4408 }
9e276872 4409 mutex_unlock(&adm_ctx.resource->adm_mutex);
3b98c0c2 4410out:
a910b123 4411 drbd_adm_finish(&adm_ctx, info, retcode);
3b98c0c2 4412 return 0;
b411b363
PR
4413}
4414
05a10ec7 4415static enum drbd_ret_code adm_del_minor(struct drbd_device *device)
b411b363 4416{
a2972846
AG
4417 struct drbd_peer_device *peer_device;
4418
b30ab791
AG
4419 if (device->state.disk == D_DISKLESS &&
4420 /* no need to be device->state.conn == C_STANDALONE &&
85f75dd7
LE
4421 * we may want to delete a minor from a live replication group.
4422 */
b30ab791 4423 device->state.role == R_SECONDARY) {
a2972846
AG
4424 struct drbd_connection *connection =
4425 first_connection(device->resource);
4426
b30ab791 4427 _drbd_request_state(device, NS(conn, C_WF_REPORT_PARAMS),
369bea63 4428 CS_VERBOSE + CS_WAIT_COMPLETE);
a2972846
AG
4429
4430 /* If the state engine hasn't stopped the sender thread yet, we
4431 * need to flush the sender work queue before generating the
4432 * DESTROY events here. */
4433 if (get_t_state(&connection->worker) == RUNNING)
4434 drbd_flush_workqueue(&connection->sender_work);
4435
4436 mutex_lock(&notification_mutex);
4437 for_each_peer_device(peer_device, device) {
4438 if (!has_net_conf(peer_device->connection))
4439 continue;
4440 notify_peer_device_state(NULL, 0, peer_device, NULL,
4441 NOTIFY_DESTROY | NOTIFY_CONTINUES);
4442 }
4443 notify_device_state(NULL, 0, device, NULL, NOTIFY_DESTROY);
4444 mutex_unlock(&notification_mutex);
4445
f82795d6 4446 drbd_delete_device(device);
85f75dd7
LE
4447 return NO_ERROR;
4448 } else
4449 return ERR_MINOR_CONFIGURED;
b411b363
PR
4450}
4451
05a10ec7 4452int drbd_adm_del_minor(struct sk_buff *skb, struct genl_info *info)
b411b363 4453{
a910b123 4454 struct drbd_config_context adm_ctx;
3b98c0c2 4455 enum drbd_ret_code retcode;
b411b363 4456
a910b123 4457 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3b98c0c2
LE
4458 if (!adm_ctx.reply_skb)
4459 return retcode;
4460 if (retcode != NO_ERROR)
4461 goto out;
b411b363 4462
9e276872 4463 mutex_lock(&adm_ctx.resource->adm_mutex);
05a10ec7 4464 retcode = adm_del_minor(adm_ctx.device);
9e276872 4465 mutex_unlock(&adm_ctx.resource->adm_mutex);
85f75dd7 4466out:
a910b123 4467 drbd_adm_finish(&adm_ctx, info, retcode);
85f75dd7 4468 return 0;
b411b363
PR
4469}
4470
179e20b8
AG
4471static int adm_del_resource(struct drbd_resource *resource)
4472{
4473 struct drbd_connection *connection;
4474
4475 for_each_connection(connection, resource) {
4476 if (connection->cstate > C_STANDALONE)
4477 return ERR_NET_CONFIGURED;
4478 }
4479 if (!idr_is_empty(&resource->devices))
4480 return ERR_RES_IN_USE;
4481
a2972846
AG
4482 /* The state engine has stopped the sender thread, so we don't
4483 * need to flush the sender work queue before generating the
4484 * DESTROY event here. */
4485 mutex_lock(&notification_mutex);
4486 notify_resource_state(NULL, 0, resource, NULL, NOTIFY_DESTROY);
4487 mutex_unlock(&notification_mutex);
4488
28bc3b8c 4489 mutex_lock(&resources_mutex);
179e20b8 4490 list_del_rcu(&resource->resources);
28bc3b8c 4491 mutex_unlock(&resources_mutex);
179e20b8
AG
4492 /* Make sure all threads have actually stopped: state handling only
4493 * does drbd_thread_stop_nowait(). */
4494 list_for_each_entry(connection, &resource->connections, connections)
4495 drbd_thread_stop(&connection->worker);
4496 synchronize_rcu();
4497 drbd_free_resource(resource);
4498 return NO_ERROR;
4499}
4500
85f75dd7 4501int drbd_adm_down(struct sk_buff *skb, struct genl_info *info)
b411b363 4502{
a910b123 4503 struct drbd_config_context adm_ctx;
b6f85ef9
AG
4504 struct drbd_resource *resource;
4505 struct drbd_connection *connection;
4506 struct drbd_device *device;
f3dfa40a 4507 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
85f75dd7 4508 unsigned i;
b411b363 4509
a910b123 4510 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
85f75dd7
LE
4511 if (!adm_ctx.reply_skb)
4512 return retcode;
4513 if (retcode != NO_ERROR)
9e276872 4514 goto finish;
b411b363 4515
b6f85ef9 4516 resource = adm_ctx.resource;
9e276872 4517 mutex_lock(&resource->adm_mutex);
85f75dd7 4518 /* demote */
b6f85ef9
AG
4519 for_each_connection(connection, resource) {
4520 struct drbd_peer_device *peer_device;
4521
4522 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
4523 retcode = drbd_set_role(peer_device->device, R_SECONDARY, 0);
4524 if (retcode < SS_SUCCESS) {
a910b123 4525 drbd_msg_put_info(adm_ctx.reply_skb, "failed to demote");
b6f85ef9
AG
4526 goto out;
4527 }
4528 }
4529
4530 retcode = conn_try_disconnect(connection, 0);
85f75dd7 4531 if (retcode < SS_SUCCESS) {
a910b123 4532 drbd_msg_put_info(adm_ctx.reply_skb, "failed to disconnect");
c141ebda 4533 goto out;
85f75dd7 4534 }
b411b363 4535 }
b411b363 4536
85f75dd7 4537 /* detach */
b6f85ef9
AG
4538 idr_for_each_entry(&resource->devices, device, i) {
4539 retcode = adm_detach(device, 0);
27012382 4540 if (retcode < SS_SUCCESS || retcode > NO_ERROR) {
a910b123 4541 drbd_msg_put_info(adm_ctx.reply_skb, "failed to detach");
c141ebda 4542 goto out;
85f75dd7
LE
4543 }
4544 }
b411b363 4545
85f75dd7 4546 /* delete volumes */
b6f85ef9
AG
4547 idr_for_each_entry(&resource->devices, device, i) {
4548 retcode = adm_del_minor(device);
85f75dd7
LE
4549 if (retcode != NO_ERROR) {
4550 /* "can not happen" */
a910b123 4551 drbd_msg_put_info(adm_ctx.reply_skb, "failed to delete volume");
ef356262 4552 goto out;
85f75dd7
LE
4553 }
4554 }
b411b363 4555
179e20b8 4556 retcode = adm_del_resource(resource);
3b98c0c2 4557out:
9e276872
LE
4558 mutex_unlock(&resource->adm_mutex);
4559finish:
a910b123 4560 drbd_adm_finish(&adm_ctx, info, retcode);
3b98c0c2 4561 return 0;
b411b363
PR
4562}
4563
789c1b62 4564int drbd_adm_del_resource(struct sk_buff *skb, struct genl_info *info)
b411b363 4565{
a910b123 4566 struct drbd_config_context adm_ctx;
77c556f6 4567 struct drbd_resource *resource;
3b98c0c2 4568 enum drbd_ret_code retcode;
b411b363 4569
a910b123 4570 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
3b98c0c2
LE
4571 if (!adm_ctx.reply_skb)
4572 return retcode;
4573 if (retcode != NO_ERROR)
9e276872 4574 goto finish;
77c556f6 4575 resource = adm_ctx.resource;
b411b363 4576
179e20b8
AG
4577 mutex_lock(&resource->adm_mutex);
4578 retcode = adm_del_resource(resource);
9e276872
LE
4579 mutex_unlock(&resource->adm_mutex);
4580finish:
a910b123 4581 drbd_adm_finish(&adm_ctx, info, retcode);
b411b363
PR
4582 return 0;
4583}
4584
b30ab791 4585void drbd_bcast_event(struct drbd_device *device, const struct sib_info *sib)
b411b363 4586{
3b98c0c2
LE
4587 struct sk_buff *msg;
4588 struct drbd_genlmsghdr *d_out;
4589 unsigned seq;
4590 int err = -ENOMEM;
4591
4592 seq = atomic_inc_return(&drbd_genl_seq);
4593 msg = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
4594 if (!msg)
4595 goto failed;
4596
4597 err = -EMSGSIZE;
4598 d_out = genlmsg_put(msg, 0, seq, &drbd_genl_family, 0, DRBD_EVENT);
4599 if (!d_out) /* cannot happen, but anyways. */
4600 goto nla_put_failure;
b30ab791 4601 d_out->minor = device_to_minor(device);
6f9b5f84 4602 d_out->ret_code = NO_ERROR;
3b98c0c2 4603
b30ab791 4604 if (nla_put_status_info(msg, device, sib))
3b98c0c2
LE
4605 goto nla_put_failure;
4606 genlmsg_end(msg, d_out);
d38f8612 4607 err = drbd_genl_multicast_events(msg, GFP_NOWAIT);
3b98c0c2
LE
4608 /* msg has been consumed or freed in netlink_broadcast() */
4609 if (err && err != -ESRCH)
4610 goto failed;
b411b363 4611
3b98c0c2 4612 return;
b411b363 4613
3b98c0c2
LE
4614nla_put_failure:
4615 nlmsg_free(msg);
4616failed:
d0180171 4617 drbd_err(device, "Error %d while broadcasting event. "
3b98c0c2
LE
4618 "Event seq:%u sib_reason:%u\n",
4619 err, seq, sib->sib_reason);
b411b363 4620}
a2972846 4621
a2972846
AG
4622static int nla_put_notification_header(struct sk_buff *msg,
4623 enum drbd_notification_type type)
4624{
4625 struct drbd_notification_header nh = {
4626 .nh_type = type,
4627 };
4628
4629 return drbd_notification_header_to_skb(msg, &nh, true);
4630}
4631
4632void notify_resource_state(struct sk_buff *skb,
4633 unsigned int seq,
4634 struct drbd_resource *resource,
4635 struct resource_info *resource_info,
4636 enum drbd_notification_type type)
4637{
4638 struct resource_statistics resource_statistics;
4639 struct drbd_genlmsghdr *dh;
4640 bool multicast = false;
4641 int err;
4642
4643 if (!skb) {
4644 seq = atomic_inc_return(&notify_genl_seq);
4645 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
4646 err = -ENOMEM;
4647 if (!skb)
4648 goto failed;
4649 multicast = true;
4650 }
4651
4652 err = -EMSGSIZE;
4653 dh = genlmsg_put(skb, 0, seq, &drbd_genl_family, 0, DRBD_RESOURCE_STATE);
4654 if (!dh)
4655 goto nla_put_failure;
4656 dh->minor = -1U;
4657 dh->ret_code = NO_ERROR;
4658 if (nla_put_drbd_cfg_context(skb, resource, NULL, NULL) ||
4659 nla_put_notification_header(skb, type) ||
4660 ((type & ~NOTIFY_FLAGS) != NOTIFY_DESTROY &&
4661 resource_info_to_skb(skb, resource_info, true)))
4662 goto nla_put_failure;
4663 resource_statistics.res_stat_write_ordering = resource->write_ordering;
4664 err = resource_statistics_to_skb(skb, &resource_statistics, !capable(CAP_SYS_ADMIN));
4665 if (err)
4666 goto nla_put_failure;
4667 genlmsg_end(skb, dh);
4668 if (multicast) {
d38f8612 4669 err = drbd_genl_multicast_events(skb, GFP_NOWAIT);
a2972846
AG
4670 /* skb has been consumed or freed in netlink_broadcast() */
4671 if (err && err != -ESRCH)
4672 goto failed;
4673 }
4674 return;
4675
4676nla_put_failure:
4677 nlmsg_free(skb);
4678failed:
4679 drbd_err(resource, "Error %d while broadcasting event. Event seq:%u\n",
4680 err, seq);
4681}
4682
4683void notify_device_state(struct sk_buff *skb,
4684 unsigned int seq,
4685 struct drbd_device *device,
4686 struct device_info *device_info,
4687 enum drbd_notification_type type)
4688{
4689 struct device_statistics device_statistics;
4690 struct drbd_genlmsghdr *dh;
4691 bool multicast = false;
4692 int err;
4693
4694 if (!skb) {
4695 seq = atomic_inc_return(&notify_genl_seq);
4696 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
4697 err = -ENOMEM;
4698 if (!skb)
4699 goto failed;
4700 multicast = true;
4701 }
4702
4703 err = -EMSGSIZE;
4704 dh = genlmsg_put(skb, 0, seq, &drbd_genl_family, 0, DRBD_DEVICE_STATE);
4705 if (!dh)
4706 goto nla_put_failure;
4707 dh->minor = device->minor;
4708 dh->ret_code = NO_ERROR;
4709 if (nla_put_drbd_cfg_context(skb, device->resource, NULL, device) ||
4710 nla_put_notification_header(skb, type) ||
4711 ((type & ~NOTIFY_FLAGS) != NOTIFY_DESTROY &&
4712 device_info_to_skb(skb, device_info, true)))
4713 goto nla_put_failure;
4714 device_to_statistics(&device_statistics, device);
4715 device_statistics_to_skb(skb, &device_statistics, !capable(CAP_SYS_ADMIN));
4716 genlmsg_end(skb, dh);
4717 if (multicast) {
d38f8612 4718 err = drbd_genl_multicast_events(skb, GFP_NOWAIT);
a2972846
AG
4719 /* skb has been consumed or freed in netlink_broadcast() */
4720 if (err && err != -ESRCH)
4721 goto failed;
4722 }
4723 return;
4724
4725nla_put_failure:
4726 nlmsg_free(skb);
4727failed:
4728 drbd_err(device, "Error %d while broadcasting event. Event seq:%u\n",
4729 err, seq);
4730}
4731
4732void notify_connection_state(struct sk_buff *skb,
4733 unsigned int seq,
4734 struct drbd_connection *connection,
4735 struct connection_info *connection_info,
4736 enum drbd_notification_type type)
4737{
4738 struct connection_statistics connection_statistics;
4739 struct drbd_genlmsghdr *dh;
4740 bool multicast = false;
4741 int err;
4742
4743 if (!skb) {
4744 seq = atomic_inc_return(&notify_genl_seq);
4745 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
4746 err = -ENOMEM;
4747 if (!skb)
4748 goto failed;
4749 multicast = true;
4750 }
4751
4752 err = -EMSGSIZE;
4753 dh = genlmsg_put(skb, 0, seq, &drbd_genl_family, 0, DRBD_CONNECTION_STATE);
4754 if (!dh)
4755 goto nla_put_failure;
4756 dh->minor = -1U;
4757 dh->ret_code = NO_ERROR;
4758 if (nla_put_drbd_cfg_context(skb, connection->resource, connection, NULL) ||
4759 nla_put_notification_header(skb, type) ||
4760 ((type & ~NOTIFY_FLAGS) != NOTIFY_DESTROY &&
4761 connection_info_to_skb(skb, connection_info, true)))
4762 goto nla_put_failure;
4763 connection_statistics.conn_congested = test_bit(NET_CONGESTED, &connection->flags);
4764 connection_statistics_to_skb(skb, &connection_statistics, !capable(CAP_SYS_ADMIN));
4765 genlmsg_end(skb, dh);
4766 if (multicast) {
d38f8612 4767 err = drbd_genl_multicast_events(skb, GFP_NOWAIT);
a2972846
AG
4768 /* skb has been consumed or freed in netlink_broadcast() */
4769 if (err && err != -ESRCH)
4770 goto failed;
4771 }
4772 return;
4773
4774nla_put_failure:
4775 nlmsg_free(skb);
4776failed:
4777 drbd_err(connection, "Error %d while broadcasting event. Event seq:%u\n",
4778 err, seq);
4779}
4780
4781void notify_peer_device_state(struct sk_buff *skb,
4782 unsigned int seq,
4783 struct drbd_peer_device *peer_device,
4784 struct peer_device_info *peer_device_info,
4785 enum drbd_notification_type type)
4786{
4787 struct peer_device_statistics peer_device_statistics;
4788 struct drbd_resource *resource = peer_device->device->resource;
4789 struct drbd_genlmsghdr *dh;
4790 bool multicast = false;
4791 int err;
4792
4793 if (!skb) {
4794 seq = atomic_inc_return(&notify_genl_seq);
4795 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
4796 err = -ENOMEM;
4797 if (!skb)
4798 goto failed;
4799 multicast = true;
4800 }
4801
4802 err = -EMSGSIZE;
4803 dh = genlmsg_put(skb, 0, seq, &drbd_genl_family, 0, DRBD_PEER_DEVICE_STATE);
4804 if (!dh)
4805 goto nla_put_failure;
4806 dh->minor = -1U;
4807 dh->ret_code = NO_ERROR;
4808 if (nla_put_drbd_cfg_context(skb, resource, peer_device->connection, peer_device->device) ||
4809 nla_put_notification_header(skb, type) ||
4810 ((type & ~NOTIFY_FLAGS) != NOTIFY_DESTROY &&
4811 peer_device_info_to_skb(skb, peer_device_info, true)))
4812 goto nla_put_failure;
4813 peer_device_to_statistics(&peer_device_statistics, peer_device);
4814 peer_device_statistics_to_skb(skb, &peer_device_statistics, !capable(CAP_SYS_ADMIN));
4815 genlmsg_end(skb, dh);
4816 if (multicast) {
d38f8612 4817 err = drbd_genl_multicast_events(skb, GFP_NOWAIT);
a2972846
AG
4818 /* skb has been consumed or freed in netlink_broadcast() */
4819 if (err && err != -ESRCH)
4820 goto failed;
4821 }
4822 return;
4823
4824nla_put_failure:
4825 nlmsg_free(skb);
4826failed:
4827 drbd_err(peer_device, "Error %d while broadcasting event. Event seq:%u\n",
4828 err, seq);
4829}
4830
4831void notify_helper(enum drbd_notification_type type,
4832 struct drbd_device *device, struct drbd_connection *connection,
4833 const char *name, int status)
4834{
4835 struct drbd_resource *resource = device ? device->resource : connection->resource;
4836 struct drbd_helper_info helper_info;
4837 unsigned int seq = atomic_inc_return(&notify_genl_seq);
4838 struct sk_buff *skb = NULL;
4839 struct drbd_genlmsghdr *dh;
4840 int err;
4841
4842 strlcpy(helper_info.helper_name, name, sizeof(helper_info.helper_name));
4843 helper_info.helper_name_len = min(strlen(name), sizeof(helper_info.helper_name));
4844 helper_info.helper_status = status;
4845
4846 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
4847 err = -ENOMEM;
4848 if (!skb)
4849 goto fail;
4850
4851 err = -EMSGSIZE;
4852 dh = genlmsg_put(skb, 0, seq, &drbd_genl_family, 0, DRBD_HELPER);
4853 if (!dh)
4854 goto fail;
4855 dh->minor = device ? device->minor : -1;
4856 dh->ret_code = NO_ERROR;
4857 mutex_lock(&notification_mutex);
4858 if (nla_put_drbd_cfg_context(skb, resource, connection, device) ||
4859 nla_put_notification_header(skb, type) ||
4860 drbd_helper_info_to_skb(skb, &helper_info, true))
4861 goto unlock_fail;
4862 genlmsg_end(skb, dh);
d38f8612 4863 err = drbd_genl_multicast_events(skb, GFP_NOWAIT);
a2972846
AG
4864 skb = NULL;
4865 /* skb has been consumed or freed in netlink_broadcast() */
4866 if (err && err != -ESRCH)
4867 goto unlock_fail;
4868 mutex_unlock(&notification_mutex);
4869 return;
4870
4871unlock_fail:
4872 mutex_unlock(&notification_mutex);
4873fail:
4874 nlmsg_free(skb);
4875 drbd_err(resource, "Error %d while broadcasting event. Event seq:%u\n",
4876 err, seq);
4877}
4878
4879static void notify_initial_state_done(struct sk_buff *skb, unsigned int seq)
4880{
4881 struct drbd_genlmsghdr *dh;
4882 int err;
4883
4884 err = -EMSGSIZE;
4885 dh = genlmsg_put(skb, 0, seq, &drbd_genl_family, 0, DRBD_INITIAL_STATE_DONE);
4886 if (!dh)
4887 goto nla_put_failure;
4888 dh->minor = -1U;
4889 dh->ret_code = NO_ERROR;
4890 if (nla_put_notification_header(skb, NOTIFY_EXISTS))
4891 goto nla_put_failure;
4892 genlmsg_end(skb, dh);
4893 return;
4894
4895nla_put_failure:
4896 nlmsg_free(skb);
4897 pr_err("Error %d sending event. Event seq:%u\n", err, seq);
4898}
4899
4900static void free_state_changes(struct list_head *list)
4901{
4902 while (!list_empty(list)) {
4903 struct drbd_state_change *state_change =
4904 list_first_entry(list, struct drbd_state_change, list);
4905 list_del(&state_change->list);
4906 forget_state_change(state_change);
4907 }
4908}
4909
4910static unsigned int notifications_for_state_change(struct drbd_state_change *state_change)
4911{
4912 return 1 +
4913 state_change->n_connections +
4914 state_change->n_devices +
4915 state_change->n_devices * state_change->n_connections;
4916}
4917
4918static int get_initial_state(struct sk_buff *skb, struct netlink_callback *cb)
4919{
4920 struct drbd_state_change *state_change = (struct drbd_state_change *)cb->args[0];
4921 unsigned int seq = cb->args[2];
4922 unsigned int n;
4923 enum drbd_notification_type flags = 0;
4924
4925 /* There is no need for taking notification_mutex here: it doesn't
4926 matter if the initial state events mix with later state chage
4927 events; we can always tell the events apart by the NOTIFY_EXISTS
4928 flag. */
4929
4930 cb->args[5]--;
4931 if (cb->args[5] == 1) {
4932 notify_initial_state_done(skb, seq);
4933 goto out;
4934 }
4935 n = cb->args[4]++;
4936 if (cb->args[4] < cb->args[3])
4937 flags |= NOTIFY_CONTINUES;
4938 if (n < 1) {
4939 notify_resource_state_change(skb, seq, state_change->resource,
4940 NOTIFY_EXISTS | flags);
4941 goto next;
4942 }
4943 n--;
4944 if (n < state_change->n_connections) {
4945 notify_connection_state_change(skb, seq, &state_change->connections[n],
4946 NOTIFY_EXISTS | flags);
4947 goto next;
4948 }
4949 n -= state_change->n_connections;
4950 if (n < state_change->n_devices) {
4951 notify_device_state_change(skb, seq, &state_change->devices[n],
4952 NOTIFY_EXISTS | flags);
4953 goto next;
4954 }
4955 n -= state_change->n_devices;
4956 if (n < state_change->n_devices * state_change->n_connections) {
4957 notify_peer_device_state_change(skb, seq, &state_change->peer_devices[n],
4958 NOTIFY_EXISTS | flags);
4959 goto next;
4960 }
4961
4962next:
4963 if (cb->args[4] == cb->args[3]) {
4964 struct drbd_state_change *next_state_change =
4965 list_entry(state_change->list.next,
4966 struct drbd_state_change, list);
4967 cb->args[0] = (long)next_state_change;
4968 cb->args[3] = notifications_for_state_change(next_state_change);
4969 cb->args[4] = 0;
4970 }
4971out:
4972 return skb->len;
4973}
4974
4975int drbd_adm_get_initial_state(struct sk_buff *skb, struct netlink_callback *cb)
4976{
4977 struct drbd_resource *resource;
4978 LIST_HEAD(head);
4979
4980 if (cb->args[5] >= 1) {
4981 if (cb->args[5] > 1)
4982 return get_initial_state(skb, cb);
4983 if (cb->args[0]) {
4984 struct drbd_state_change *state_change =
4985 (struct drbd_state_change *)cb->args[0];
4986
4987 /* connect list to head */
4988 list_add(&head, &state_change->list);
4989 free_state_changes(&head);
4990 }
4991 return 0;
4992 }
4993
4994 cb->args[5] = 2; /* number of iterations */
4995 mutex_lock(&resources_mutex);
4996 for_each_resource(resource, &drbd_resources) {
4997 struct drbd_state_change *state_change;
4998
4999 state_change = remember_old_state(resource, GFP_KERNEL);
5000 if (!state_change) {
5001 if (!list_empty(&head))
5002 free_state_changes(&head);
5003 mutex_unlock(&resources_mutex);
5004 return -ENOMEM;
5005 }
5006 copy_old_to_new_state_change(state_change);
5007 list_add_tail(&state_change->list, &head);
5008 cb->args[5] += notifications_for_state_change(state_change);
5009 }
5010 mutex_unlock(&resources_mutex);
5011
5012 if (!list_empty(&head)) {
5013 struct drbd_state_change *state_change =
5014 list_entry(head.next, struct drbd_state_change, list);
5015 cb->args[0] = (long)state_change;
5016 cb->args[3] = notifications_for_state_change(state_change);
5017 list_del(&head); /* detach list from head */
5018 }
5019
5020 cb->args[2] = cb->nlh->nlmsg_seq;
5021 return get_initial_state(skb, cb);
5022}