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