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