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