]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/block/drbd/drbd_nl.c
drbd: Add explicit device parameter to D_ASSERT
[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
b411b363
PR
26#include <linux/module.h>
27#include <linux/drbd.h>
28#include <linux/in.h>
29#include <linux/fs.h>
30#include <linux/file.h>
31#include <linux/slab.h>
b411b363
PR
32#include <linux/blkpg.h>
33#include <linux/cpumask.h>
34#include "drbd_int.h"
a3603a6e 35#include "drbd_protocol.h"
265be2d0 36#include "drbd_req.h"
b411b363
PR
37#include "drbd_wrappers.h"
38#include <asm/unaligned.h>
b411b363 39#include <linux/drbd_limits.h>
87f7be4c 40#include <linux/kthread.h>
b411b363 41
3b98c0c2
LE
42#include <net/genetlink.h>
43
44/* .doit */
45// int drbd_adm_create_resource(struct sk_buff *skb, struct genl_info *info);
46// int drbd_adm_delete_resource(struct sk_buff *skb, struct genl_info *info);
47
05a10ec7
AG
48int drbd_adm_new_minor(struct sk_buff *skb, struct genl_info *info);
49int drbd_adm_del_minor(struct sk_buff *skb, struct genl_info *info);
3b98c0c2 50
789c1b62
AG
51int drbd_adm_new_resource(struct sk_buff *skb, struct genl_info *info);
52int drbd_adm_del_resource(struct sk_buff *skb, struct genl_info *info);
85f75dd7 53int drbd_adm_down(struct sk_buff *skb, struct genl_info *info);
3b98c0c2
LE
54
55int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info);
56int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info);
f399002e 57int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info);
3b98c0c2
LE
58int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info);
59int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info);
f399002e 60int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info);
3b98c0c2
LE
61int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info);
62int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info);
63int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info);
64int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info);
65int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info);
66int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info);
67int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info);
68int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info);
69int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info);
70int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info);
71int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info);
f399002e 72int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info);
3b98c0c2
LE
73int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info);
74int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info);
75/* .dumpit */
76int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb);
77
78#include <linux/drbd_genl_api.h>
01b39b50 79#include "drbd_nla.h"
3b98c0c2
LE
80#include <linux/genl_magic_func.h>
81
82/* used blkdev_get_by_path, to claim our meta data device(s) */
b411b363
PR
83static char *drbd_m_holder = "Hands off! this is DRBD's meta data device.";
84
3b98c0c2
LE
85/* Configuration is strictly serialized, because generic netlink message
86 * processing is strictly serialized by the genl_lock().
87 * Which means we can use one static global drbd_config_context struct.
88 */
89static struct drbd_config_context {
90 /* assigned from drbd_genlmsghdr */
91 unsigned int minor;
92 /* assigned from request attributes, if present */
93 unsigned int volume;
94#define VOLUME_UNSPECIFIED (-1U)
95 /* pointer into the request skb,
96 * limited lifetime! */
7c3063cc 97 char *resource_name;
089c075d
AG
98 struct nlattr *my_addr;
99 struct nlattr *peer_addr;
3b98c0c2
LE
100
101 /* reply buffer */
102 struct sk_buff *reply_skb;
103 /* pointer into reply buffer */
104 struct drbd_genlmsghdr *reply_dh;
105 /* resolved from attributes, if possible */
b30ab791 106 struct drbd_device *device;
4bc76048 107 struct drbd_resource *resource;
bde89a9e 108 struct drbd_connection *connection;
3b98c0c2
LE
109} adm_ctx;
110
111static void drbd_adm_send_reply(struct sk_buff *skb, struct genl_info *info)
112{
113 genlmsg_end(skb, genlmsg_data(nlmsg_data(nlmsg_hdr(skb))));
114 if (genlmsg_reply(skb, info))
115 printk(KERN_ERR "drbd: error sending genl reply\n");
b411b363 116}
3b98c0c2
LE
117
118/* Used on a fresh "drbd_adm_prepare"d reply_skb, this cannot fail: The only
119 * reason it could fail was no space in skb, and there are 4k available. */
8432b314 120int drbd_msg_put_info(const char *info)
3b98c0c2
LE
121{
122 struct sk_buff *skb = adm_ctx.reply_skb;
123 struct nlattr *nla;
124 int err = -EMSGSIZE;
125
126 if (!info || !info[0])
127 return 0;
128
129 nla = nla_nest_start(skb, DRBD_NLA_CFG_REPLY);
130 if (!nla)
131 return err;
132
133 err = nla_put_string(skb, T_info_text, info);
134 if (err) {
135 nla_nest_cancel(skb, nla);
136 return err;
137 } else
138 nla_nest_end(skb, nla);
139 return 0;
b411b363
PR
140}
141
3b98c0c2
LE
142/* This would be a good candidate for a "pre_doit" hook,
143 * and per-family private info->pointers.
144 * But we need to stay compatible with older kernels.
145 * If it returns successfully, adm_ctx members are valid.
146 */
147#define DRBD_ADM_NEED_MINOR 1
44e52cfa 148#define DRBD_ADM_NEED_RESOURCE 2
089c075d 149#define DRBD_ADM_NEED_CONNECTION 4
3b98c0c2
LE
150static int drbd_adm_prepare(struct sk_buff *skb, struct genl_info *info,
151 unsigned flags)
152{
153 struct drbd_genlmsghdr *d_in = info->userhdr;
154 const u8 cmd = info->genlhdr->cmd;
155 int err;
156
157 memset(&adm_ctx, 0, sizeof(adm_ctx));
158
159 /* genl_rcv_msg only checks for CAP_NET_ADMIN on "GENL_ADMIN_PERM" :( */
98683650 160 if (cmd != DRBD_ADM_GET_STATUS && !capable(CAP_NET_ADMIN))
3b98c0c2
LE
161 return -EPERM;
162
163 adm_ctx.reply_skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1e2a2551
AG
164 if (!adm_ctx.reply_skb) {
165 err = -ENOMEM;
3b98c0c2 166 goto fail;
1e2a2551 167 }
3b98c0c2
LE
168
169 adm_ctx.reply_dh = genlmsg_put_reply(adm_ctx.reply_skb,
170 info, &drbd_genl_family, 0, cmd);
171 /* put of a few bytes into a fresh skb of >= 4k will always succeed.
172 * but anyways */
1e2a2551
AG
173 if (!adm_ctx.reply_dh) {
174 err = -ENOMEM;
3b98c0c2 175 goto fail;
1e2a2551 176 }
3b98c0c2
LE
177
178 adm_ctx.reply_dh->minor = d_in->minor;
179 adm_ctx.reply_dh->ret_code = NO_ERROR;
180
089c075d 181 adm_ctx.volume = VOLUME_UNSPECIFIED;
3b98c0c2
LE
182 if (info->attrs[DRBD_NLA_CFG_CONTEXT]) {
183 struct nlattr *nla;
184 /* parse and validate only */
f399002e 185 err = drbd_cfg_context_from_attrs(NULL, info);
3b98c0c2
LE
186 if (err)
187 goto fail;
188
189 /* It was present, and valid,
190 * copy it over to the reply skb. */
191 err = nla_put_nohdr(adm_ctx.reply_skb,
192 info->attrs[DRBD_NLA_CFG_CONTEXT]->nla_len,
193 info->attrs[DRBD_NLA_CFG_CONTEXT]);
194 if (err)
195 goto fail;
196
197 /* and assign stuff to the global adm_ctx */
198 nla = nested_attr_tb[__nla_type(T_ctx_volume)];
089c075d
AG
199 if (nla)
200 adm_ctx.volume = nla_get_u32(nla);
7c3063cc 201 nla = nested_attr_tb[__nla_type(T_ctx_resource_name)];
3b98c0c2 202 if (nla)
7c3063cc 203 adm_ctx.resource_name = nla_data(nla);
089c075d
AG
204 adm_ctx.my_addr = nested_attr_tb[__nla_type(T_ctx_my_addr)];
205 adm_ctx.peer_addr = nested_attr_tb[__nla_type(T_ctx_peer_addr)];
206 if ((adm_ctx.my_addr &&
bde89a9e 207 nla_len(adm_ctx.my_addr) > sizeof(adm_ctx.connection->my_addr)) ||
089c075d 208 (adm_ctx.peer_addr &&
bde89a9e 209 nla_len(adm_ctx.peer_addr) > sizeof(adm_ctx.connection->peer_addr))) {
089c075d
AG
210 err = -EINVAL;
211 goto fail;
212 }
213 }
3b98c0c2
LE
214
215 adm_ctx.minor = d_in->minor;
b30ab791 216 adm_ctx.device = minor_to_device(d_in->minor);
4bc76048
AG
217 if (adm_ctx.resource_name) {
218 adm_ctx.resource = drbd_find_resource(adm_ctx.resource_name);
219 if (adm_ctx.resource) {
220 adm_ctx.connection = first_connection(adm_ctx.resource);
221 kref_get(&adm_ctx.connection->kref);
222 }
223 }
3b98c0c2 224
b30ab791 225 if (!adm_ctx.device && (flags & DRBD_ADM_NEED_MINOR)) {
3b98c0c2
LE
226 drbd_msg_put_info("unknown minor");
227 return ERR_MINOR_INVALID;
228 }
4bc76048 229 if (!adm_ctx.resource && (flags & DRBD_ADM_NEED_RESOURCE)) {
44e52cfa 230 drbd_msg_put_info("unknown resource");
a10f6b8a
AG
231 if (adm_ctx.resource_name)
232 return ERR_RES_NOT_KNOWN;
3b98c0c2
LE
233 return ERR_INVALID_REQUEST;
234 }
235
089c075d 236 if (flags & DRBD_ADM_NEED_CONNECTION) {
bde89a9e 237 if (adm_ctx.connection && !(flags & DRBD_ADM_NEED_RESOURCE)) {
089c075d
AG
238 drbd_msg_put_info("no resource name expected");
239 return ERR_INVALID_REQUEST;
240 }
b30ab791 241 if (adm_ctx.device) {
089c075d
AG
242 drbd_msg_put_info("no minor number expected");
243 return ERR_INVALID_REQUEST;
244 }
245 if (adm_ctx.my_addr && adm_ctx.peer_addr)
bde89a9e 246 adm_ctx.connection = conn_get_by_addrs(nla_data(adm_ctx.my_addr),
089c075d
AG
247 nla_len(adm_ctx.my_addr),
248 nla_data(adm_ctx.peer_addr),
249 nla_len(adm_ctx.peer_addr));
bde89a9e 250 if (!adm_ctx.connection) {
089c075d
AG
251 drbd_msg_put_info("unknown connection");
252 return ERR_INVALID_REQUEST;
253 }
254 }
255
3b98c0c2 256 /* some more paranoia, if the request was over-determined */
4bc76048
AG
257 if (adm_ctx.device && adm_ctx.resource &&
258 adm_ctx.device->resource != adm_ctx.resource) {
259 pr_warning("request: minor=%u, resource=%s; but that minor belongs to resource %s\n",
260 adm_ctx.minor, adm_ctx.resource->name,
d8628a86 261 adm_ctx.device->resource->name);
44e52cfa 262 drbd_msg_put_info("minor exists in different resource");
527f4b24
LE
263 return ERR_INVALID_REQUEST;
264 }
b30ab791 265 if (adm_ctx.device &&
3b98c0c2 266 adm_ctx.volume != VOLUME_UNSPECIFIED &&
b30ab791 267 adm_ctx.volume != adm_ctx.device->vnr) {
3b98c0c2
LE
268 pr_warning("request: minor=%u, volume=%u; but that minor is volume %u in %s\n",
269 adm_ctx.minor, adm_ctx.volume,
77c556f6 270 adm_ctx.device->vnr,
d8628a86 271 adm_ctx.device->resource->name);
527f4b24 272 drbd_msg_put_info("minor exists as different volume");
3b98c0c2
LE
273 return ERR_INVALID_REQUEST;
274 }
0ace9dfa 275
3b98c0c2
LE
276 return NO_ERROR;
277
278fail:
279 nlmsg_free(adm_ctx.reply_skb);
280 adm_ctx.reply_skb = NULL;
1e2a2551 281 return err;
3b98c0c2
LE
282}
283
284static int drbd_adm_finish(struct genl_info *info, int retcode)
285{
bde89a9e 286 if (adm_ctx.connection) {
05a10ec7 287 kref_put(&adm_ctx.connection->kref, drbd_destroy_connection);
bde89a9e 288 adm_ctx.connection = NULL;
0ace9dfa 289 }
4bc76048
AG
290 if (adm_ctx.resource) {
291 kref_put(&adm_ctx.resource->kref, drbd_destroy_resource);
292 adm_ctx.resource = NULL;
293 }
0ace9dfa 294
3b98c0c2
LE
295 if (!adm_ctx.reply_skb)
296 return -ENOMEM;
297
298 adm_ctx.reply_dh->ret_code = retcode;
3b98c0c2
LE
299 drbd_adm_send_reply(adm_ctx.reply_skb, info);
300 return 0;
301}
b411b363 302
bde89a9e 303static void setup_khelper_env(struct drbd_connection *connection, char **envp)
b411b363 304{
6b75dced 305 char *afs;
b411b363 306
089c075d 307 /* FIXME: A future version will not allow this case. */
bde89a9e 308 if (connection->my_addr_len == 0 || connection->peer_addr_len == 0)
089c075d
AG
309 return;
310
bde89a9e 311 switch (((struct sockaddr *)&connection->peer_addr)->sa_family) {
089c075d
AG
312 case AF_INET6:
313 afs = "ipv6";
314 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI6",
bde89a9e 315 &((struct sockaddr_in6 *)&connection->peer_addr)->sin6_addr);
b411b363 316 break;
089c075d
AG
317 case AF_INET:
318 afs = "ipv4";
319 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
bde89a9e 320 &((struct sockaddr_in *)&connection->peer_addr)->sin_addr);
b411b363 321 break;
089c075d
AG
322 default:
323 afs = "ssocks";
324 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
bde89a9e 325 &((struct sockaddr_in *)&connection->peer_addr)->sin_addr);
b411b363 326 }
089c075d 327 snprintf(envp[3], 20, "DRBD_PEER_AF=%s", afs);
6b75dced 328}
b411b363 329
b30ab791 330int drbd_khelper(struct drbd_device *device, char *cmd)
b411b363
PR
331{
332 char *envp[] = { "HOME=/",
333 "TERM=linux",
334 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
6b75dced
PR
335 (char[20]) { }, /* address family */
336 (char[60]) { }, /* address */
b411b363 337 NULL };
6b75dced 338 char mb[12];
b411b363 339 char *argv[] = {usermode_helper, cmd, mb, NULL };
a6b32bc3 340 struct drbd_connection *connection = first_peer_device(device)->connection;
6b75dced 341 struct sib_info sib;
b411b363
PR
342 int ret;
343
bde89a9e
AG
344 if (current == connection->worker.task)
345 set_bit(CALLBACK_PENDING, &connection->flags);
c2ba686f 346
b30ab791 347 snprintf(mb, 12, "minor-%d", device_to_minor(device));
bde89a9e 348 setup_khelper_env(connection, envp);
b411b363 349
1090c056
LE
350 /* The helper may take some time.
351 * write out any unsynced meta data changes now */
b30ab791 352 drbd_md_sync(device);
1090c056 353
d0180171 354 drbd_info(device, "helper command: %s %s %s\n", usermode_helper, cmd, mb);
3b98c0c2
LE
355 sib.sib_reason = SIB_HELPER_PRE;
356 sib.helper_name = cmd;
b30ab791 357 drbd_bcast_event(device, &sib);
70834d30 358 ret = call_usermodehelper(usermode_helper, argv, envp, UMH_WAIT_PROC);
b411b363 359 if (ret)
d0180171 360 drbd_warn(device, "helper command: %s %s %s exit code %u (0x%x)\n",
b411b363
PR
361 usermode_helper, cmd, mb,
362 (ret >> 8) & 0xff, ret);
363 else
d0180171 364 drbd_info(device, "helper command: %s %s %s exit code %u (0x%x)\n",
b411b363
PR
365 usermode_helper, cmd, mb,
366 (ret >> 8) & 0xff, ret);
3b98c0c2
LE
367 sib.sib_reason = SIB_HELPER_POST;
368 sib.helper_exit_code = ret;
b30ab791 369 drbd_bcast_event(device, &sib);
b411b363 370
bde89a9e
AG
371 if (current == connection->worker.task)
372 clear_bit(CALLBACK_PENDING, &connection->flags);
b411b363
PR
373
374 if (ret < 0) /* Ignore any ERRNOs we got. */
375 ret = 0;
376
377 return ret;
378}
379
bde89a9e 380static int conn_khelper(struct drbd_connection *connection, char *cmd)
6b75dced
PR
381{
382 char *envp[] = { "HOME=/",
383 "TERM=linux",
384 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
385 (char[20]) { }, /* address family */
386 (char[60]) { }, /* address */
387 NULL };
77c556f6
AG
388 char *resource_name = connection->resource->name;
389 char *argv[] = {usermode_helper, cmd, resource_name, NULL };
6b75dced
PR
390 int ret;
391
bde89a9e
AG
392 setup_khelper_env(connection, envp);
393 conn_md_sync(connection);
6b75dced 394
1ec861eb 395 drbd_info(connection, "helper command: %s %s %s\n", usermode_helper, cmd, resource_name);
6b75dced
PR
396 /* TODO: conn_bcast_event() ?? */
397
98683650 398 ret = call_usermodehelper(usermode_helper, argv, envp, UMH_WAIT_PROC);
6b75dced 399 if (ret)
1ec861eb 400 drbd_warn(connection, "helper command: %s %s %s exit code %u (0x%x)\n",
77c556f6 401 usermode_helper, cmd, resource_name,
6b75dced
PR
402 (ret >> 8) & 0xff, ret);
403 else
1ec861eb 404 drbd_info(connection, "helper command: %s %s %s exit code %u (0x%x)\n",
77c556f6 405 usermode_helper, cmd, resource_name,
6b75dced
PR
406 (ret >> 8) & 0xff, ret);
407 /* TODO: conn_bcast_event() ?? */
c2ba686f 408
b411b363
PR
409 if (ret < 0) /* Ignore any ERRNOs we got. */
410 ret = 0;
411
412 return ret;
413}
414
bde89a9e 415static enum drbd_fencing_p highest_fencing_policy(struct drbd_connection *connection)
b411b363 416{
cb703454 417 enum drbd_fencing_p fp = FP_NOT_AVAIL;
c06ece6b 418 struct drbd_peer_device *peer_device;
cb703454
PR
419 int vnr;
420
695d08fa 421 rcu_read_lock();
c06ece6b
AG
422 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
423 struct drbd_device *device = peer_device->device;
b30ab791 424 if (get_ldev_if_state(device, D_CONSISTENT)) {
c06ece6b
AG
425 struct disk_conf *disk_conf =
426 rcu_dereference(peer_device->device->ldev->disk_conf);
427 fp = max_t(enum drbd_fencing_p, fp, disk_conf->fencing);
b30ab791 428 put_ldev(device);
cb703454
PR
429 }
430 }
695d08fa 431 rcu_read_unlock();
cb703454
PR
432
433 return fp;
434}
435
bde89a9e 436bool conn_try_outdate_peer(struct drbd_connection *connection)
b411b363 437{
28e448bb 438 unsigned int connect_cnt;
cb703454
PR
439 union drbd_state mask = { };
440 union drbd_state val = { };
441 enum drbd_fencing_p fp;
b411b363
PR
442 char *ex_to_string;
443 int r;
b411b363 444
bde89a9e 445 if (connection->cstate >= C_WF_REPORT_PARAMS) {
1ec861eb 446 drbd_err(connection, "Expected cstate < C_WF_REPORT_PARAMS\n");
cb703454
PR
447 return false;
448 }
b411b363 449
bde89a9e
AG
450 spin_lock_irq(&connection->req_lock);
451 connect_cnt = connection->connect_cnt;
452 spin_unlock_irq(&connection->req_lock);
28e448bb 453
bde89a9e 454 fp = highest_fencing_policy(connection);
cb703454
PR
455 switch (fp) {
456 case FP_NOT_AVAIL:
1ec861eb 457 drbd_warn(connection, "Not fencing peer, I'm not even Consistent myself.\n");
fb22c402 458 goto out;
cb703454
PR
459 case FP_DONT_CARE:
460 return true;
461 default: ;
b411b363
PR
462 }
463
bde89a9e 464 r = conn_khelper(connection, "fence-peer");
b411b363
PR
465
466 switch ((r>>8) & 0xff) {
467 case 3: /* peer is inconsistent */
468 ex_to_string = "peer is inconsistent or worse";
cb703454
PR
469 mask.pdsk = D_MASK;
470 val.pdsk = D_INCONSISTENT;
b411b363
PR
471 break;
472 case 4: /* peer got outdated, or was already outdated */
473 ex_to_string = "peer was fenced";
cb703454
PR
474 mask.pdsk = D_MASK;
475 val.pdsk = D_OUTDATED;
b411b363
PR
476 break;
477 case 5: /* peer was down */
bde89a9e 478 if (conn_highest_disk(connection) == D_UP_TO_DATE) {
b411b363
PR
479 /* we will(have) create(d) a new UUID anyways... */
480 ex_to_string = "peer is unreachable, assumed to be dead";
cb703454
PR
481 mask.pdsk = D_MASK;
482 val.pdsk = D_OUTDATED;
b411b363
PR
483 } else {
484 ex_to_string = "peer unreachable, doing nothing since disk != UpToDate";
b411b363
PR
485 }
486 break;
487 case 6: /* Peer is primary, voluntarily outdate myself.
488 * This is useful when an unconnected R_SECONDARY is asked to
489 * become R_PRIMARY, but finds the other peer being active. */
490 ex_to_string = "peer is active";
1ec861eb 491 drbd_warn(connection, "Peer is primary, outdating myself.\n");
cb703454
PR
492 mask.disk = D_MASK;
493 val.disk = D_OUTDATED;
b411b363
PR
494 break;
495 case 7:
496 if (fp != FP_STONITH)
1ec861eb 497 drbd_err(connection, "fence-peer() = 7 && fencing != Stonith !!!\n");
b411b363 498 ex_to_string = "peer was stonithed";
cb703454
PR
499 mask.pdsk = D_MASK;
500 val.pdsk = D_OUTDATED;
b411b363
PR
501 break;
502 default:
503 /* The script is broken ... */
1ec861eb 504 drbd_err(connection, "fence-peer helper broken, returned %d\n", (r>>8)&0xff);
cb703454 505 return false; /* Eventually leave IO frozen */
b411b363
PR
506 }
507
1ec861eb 508 drbd_info(connection, "fence-peer helper returned %d (%s)\n",
cb703454 509 (r>>8) & 0xff, ex_to_string);
fb22c402 510
cb703454 511 out:
fb22c402 512
cb703454 513 /* Not using
bde89a9e 514 conn_request_state(connection, mask, val, CS_VERBOSE);
cb703454
PR
515 here, because we might were able to re-establish the connection in the
516 meantime. */
bde89a9e
AG
517 spin_lock_irq(&connection->req_lock);
518 if (connection->cstate < C_WF_REPORT_PARAMS && !test_bit(STATE_SENT, &connection->flags)) {
519 if (connection->connect_cnt != connect_cnt)
28e448bb
PR
520 /* In case the connection was established and droped
521 while the fence-peer handler was running, ignore it */
1ec861eb 522 drbd_info(connection, "Ignoring fence-peer exit code\n");
28e448bb 523 else
bde89a9e 524 _conn_request_state(connection, mask, val, CS_VERBOSE);
28e448bb 525 }
bde89a9e 526 spin_unlock_irq(&connection->req_lock);
cb703454 527
bde89a9e 528 return conn_highest_pdsk(connection) <= D_OUTDATED;
b411b363
PR
529}
530
87f7be4c
PR
531static int _try_outdate_peer_async(void *data)
532{
bde89a9e 533 struct drbd_connection *connection = (struct drbd_connection *)data;
87f7be4c 534
bde89a9e 535 conn_try_outdate_peer(connection);
87f7be4c 536
05a10ec7 537 kref_put(&connection->kref, drbd_destroy_connection);
87f7be4c
PR
538 return 0;
539}
540
bde89a9e 541void conn_try_outdate_peer_async(struct drbd_connection *connection)
87f7be4c
PR
542{
543 struct task_struct *opa;
544
bde89a9e
AG
545 kref_get(&connection->kref);
546 opa = kthread_run(_try_outdate_peer_async, connection, "drbd_async_h");
9dc9fbb3 547 if (IS_ERR(opa)) {
1ec861eb 548 drbd_err(connection, "out of mem, failed to invoke fence-peer helper\n");
05a10ec7 549 kref_put(&connection->kref, drbd_destroy_connection);
9dc9fbb3 550 }
87f7be4c 551}
b411b363 552
bf885f8a 553enum drbd_state_rv
b30ab791 554drbd_set_role(struct drbd_device *device, enum drbd_role new_role, int force)
b411b363
PR
555{
556 const int max_tries = 4;
bf885f8a 557 enum drbd_state_rv rv = SS_UNKNOWN_ERROR;
44ed167d 558 struct net_conf *nc;
b411b363
PR
559 int try = 0;
560 int forced = 0;
561 union drbd_state mask, val;
b411b363
PR
562
563 if (new_role == R_PRIMARY)
a6b32bc3 564 request_ping(first_peer_device(device)->connection); /* Detect a dead peer ASAP */
b411b363 565
b30ab791 566 mutex_lock(device->state_mutex);
b411b363
PR
567
568 mask.i = 0; mask.role = R_MASK;
569 val.i = 0; val.role = new_role;
570
571 while (try++ < max_tries) {
b30ab791 572 rv = _drbd_request_state(device, mask, val, CS_WAIT_COMPLETE);
b411b363
PR
573
574 /* in case we first succeeded to outdate,
575 * but now suddenly could establish a connection */
bf885f8a 576 if (rv == SS_CW_FAILED_BY_PEER && mask.pdsk != 0) {
b411b363
PR
577 val.pdsk = 0;
578 mask.pdsk = 0;
579 continue;
580 }
581
bf885f8a 582 if (rv == SS_NO_UP_TO_DATE_DISK && force &&
b30ab791
AG
583 (device->state.disk < D_UP_TO_DATE &&
584 device->state.disk >= D_INCONSISTENT)) {
b411b363
PR
585 mask.disk = D_MASK;
586 val.disk = D_UP_TO_DATE;
587 forced = 1;
588 continue;
589 }
590
bf885f8a 591 if (rv == SS_NO_UP_TO_DATE_DISK &&
b30ab791 592 device->state.disk == D_CONSISTENT && mask.pdsk == 0) {
0b0ba1ef 593 D_ASSERT(device, device->state.pdsk == D_UNKNOWN);
b411b363 594
a6b32bc3 595 if (conn_try_outdate_peer(first_peer_device(device)->connection)) {
b411b363
PR
596 val.disk = D_UP_TO_DATE;
597 mask.disk = D_MASK;
598 }
b411b363
PR
599 continue;
600 }
601
bf885f8a 602 if (rv == SS_NOTHING_TO_DO)
3b98c0c2 603 goto out;
bf885f8a 604 if (rv == SS_PRIMARY_NOP && mask.pdsk == 0) {
a6b32bc3 605 if (!conn_try_outdate_peer(first_peer_device(device)->connection) && force) {
d0180171 606 drbd_warn(device, "Forced into split brain situation!\n");
cb703454
PR
607 mask.pdsk = D_MASK;
608 val.pdsk = D_OUTDATED;
b411b363 609
cb703454 610 }
b411b363
PR
611 continue;
612 }
bf885f8a 613 if (rv == SS_TWO_PRIMARIES) {
b411b363
PR
614 /* Maybe the peer is detected as dead very soon...
615 retry at most once more in this case. */
44ed167d
PR
616 int timeo;
617 rcu_read_lock();
a6b32bc3 618 nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
44ed167d
PR
619 timeo = nc ? (nc->ping_timeo + 1) * HZ / 10 : 1;
620 rcu_read_unlock();
621 schedule_timeout_interruptible(timeo);
b411b363
PR
622 if (try < max_tries)
623 try = max_tries - 1;
624 continue;
625 }
bf885f8a 626 if (rv < SS_SUCCESS) {
b30ab791 627 rv = _drbd_request_state(device, mask, val,
b411b363 628 CS_VERBOSE + CS_WAIT_COMPLETE);
bf885f8a 629 if (rv < SS_SUCCESS)
3b98c0c2 630 goto out;
b411b363
PR
631 }
632 break;
633 }
634
bf885f8a 635 if (rv < SS_SUCCESS)
3b98c0c2 636 goto out;
b411b363
PR
637
638 if (forced)
d0180171 639 drbd_warn(device, "Forced to consider local data as UpToDate!\n");
b411b363
PR
640
641 /* Wait until nothing is on the fly :) */
b30ab791 642 wait_event(device->misc_wait, atomic_read(&device->ap_pending_cnt) == 0);
b411b363 643
b6dd1a89
LE
644 /* FIXME also wait for all pending P_BARRIER_ACK? */
645
b411b363 646 if (new_role == R_SECONDARY) {
b30ab791
AG
647 set_disk_ro(device->vdisk, true);
648 if (get_ldev(device)) {
649 device->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
650 put_ldev(device);
b411b363
PR
651 }
652 } else {
a6b32bc3
AG
653 mutex_lock(&first_peer_device(device)->connection->conf_update);
654 nc = first_peer_device(device)->connection->net_conf;
44ed167d 655 if (nc)
6139f60d 656 nc->discard_my_data = 0; /* without copy; single bit op is atomic */
a6b32bc3 657 mutex_unlock(&first_peer_device(device)->connection->conf_update);
91fd4dad 658
b30ab791
AG
659 set_disk_ro(device->vdisk, false);
660 if (get_ldev(device)) {
661 if (((device->state.conn < C_CONNECTED ||
662 device->state.pdsk <= D_FAILED)
663 && device->ldev->md.uuid[UI_BITMAP] == 0) || forced)
664 drbd_uuid_new_current(device);
b411b363 665
b30ab791
AG
666 device->ldev->md.uuid[UI_CURRENT] |= (u64)1;
667 put_ldev(device);
b411b363
PR
668 }
669 }
670
19f843aa
LE
671 /* writeout of activity log covered areas of the bitmap
672 * to stable storage done in after state change already */
b411b363 673
b30ab791 674 if (device->state.conn >= C_WF_REPORT_PARAMS) {
b411b363
PR
675 /* if this was forced, we should consider sync */
676 if (forced)
b30ab791
AG
677 drbd_send_uuids(device);
678 drbd_send_current_state(device);
b411b363
PR
679 }
680
b30ab791 681 drbd_md_sync(device);
b411b363 682
b30ab791 683 kobject_uevent(&disk_to_dev(device->vdisk)->kobj, KOBJ_CHANGE);
3b98c0c2 684out:
b30ab791 685 mutex_unlock(device->state_mutex);
bf885f8a 686 return rv;
b411b363
PR
687}
688
3b98c0c2 689static const char *from_attrs_err_to_txt(int err)
ef50a3e3 690{
3b98c0c2
LE
691 return err == -ENOMSG ? "required attribute missing" :
692 err == -EOPNOTSUPP ? "unknown mandatory attribute" :
f399002e 693 err == -EEXIST ? "can not change invariant setting" :
3b98c0c2 694 "invalid attribute value";
ef50a3e3 695}
b411b363 696
3b98c0c2 697int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info)
b411b363 698{
3b98c0c2
LE
699 struct set_role_parms parms;
700 int err;
701 enum drbd_ret_code retcode;
b411b363 702
3b98c0c2
LE
703 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
704 if (!adm_ctx.reply_skb)
705 return retcode;
706 if (retcode != NO_ERROR)
707 goto out;
b411b363 708
3b98c0c2
LE
709 memset(&parms, 0, sizeof(parms));
710 if (info->attrs[DRBD_NLA_SET_ROLE_PARMS]) {
f399002e 711 err = set_role_parms_from_attrs(&parms, info);
3b98c0c2
LE
712 if (err) {
713 retcode = ERR_MANDATORY_TAG;
714 drbd_msg_put_info(from_attrs_err_to_txt(err));
715 goto out;
716 }
717 }
b411b363 718
3b98c0c2 719 if (info->genlhdr->cmd == DRBD_ADM_PRIMARY)
b30ab791 720 retcode = drbd_set_role(adm_ctx.device, R_PRIMARY, parms.assume_uptodate);
3b98c0c2 721 else
b30ab791 722 retcode = drbd_set_role(adm_ctx.device, R_SECONDARY, 0);
3b98c0c2
LE
723out:
724 drbd_adm_finish(info, retcode);
b411b363
PR
725 return 0;
726}
727
ae8bf312
LE
728/* Initializes the md.*_offset members, so we are able to find
729 * the on disk meta data.
730 *
731 * We currently have two possible layouts:
732 * external:
733 * |----------- md_size_sect ------------------|
734 * [ 4k superblock ][ activity log ][ Bitmap ]
735 * | al_offset == 8 |
736 * | bm_offset = al_offset + X |
737 * ==> bitmap sectors = md_size_sect - bm_offset
738 *
739 * internal:
740 * |----------- md_size_sect ------------------|
741 * [data.....][ Bitmap ][ activity log ][ 4k superblock ]
742 * | al_offset < 0 |
743 * | bm_offset = al_offset - Y |
744 * ==> bitmap sectors = Y = al_offset - bm_offset
745 *
746 * Activity log size used to be fixed 32kB,
747 * but is about to become configurable.
748 */
b30ab791 749static void drbd_md_set_sector_offsets(struct drbd_device *device,
b411b363
PR
750 struct drbd_backing_dev *bdev)
751{
752 sector_t md_size_sect = 0;
c04ccaa6 753 unsigned int al_size_sect = bdev->md.al_size_4k * 8;
daeda1cc 754
3a4d4eb3
LE
755 bdev->md.md_offset = drbd_md_ss(bdev);
756
68e41a43 757 switch (bdev->md.meta_dev_idx) {
b411b363
PR
758 default:
759 /* v07 style fixed size indexed meta data */
ae8bf312 760 bdev->md.md_size_sect = MD_128MB_SECT;
ae8bf312
LE
761 bdev->md.al_offset = MD_4kB_SECT;
762 bdev->md.bm_offset = MD_4kB_SECT + al_size_sect;
b411b363
PR
763 break;
764 case DRBD_MD_INDEX_FLEX_EXT:
765 /* just occupy the full device; unit: sectors */
766 bdev->md.md_size_sect = drbd_get_capacity(bdev->md_bdev);
ae8bf312
LE
767 bdev->md.al_offset = MD_4kB_SECT;
768 bdev->md.bm_offset = MD_4kB_SECT + al_size_sect;
b411b363
PR
769 break;
770 case DRBD_MD_INDEX_INTERNAL:
771 case DRBD_MD_INDEX_FLEX_INT:
b411b363 772 /* al size is still fixed */
ae8bf312 773 bdev->md.al_offset = -al_size_sect;
b411b363
PR
774 /* we need (slightly less than) ~ this much bitmap sectors: */
775 md_size_sect = drbd_get_capacity(bdev->backing_bdev);
776 md_size_sect = ALIGN(md_size_sect, BM_SECT_PER_EXT);
777 md_size_sect = BM_SECT_TO_EXT(md_size_sect);
778 md_size_sect = ALIGN(md_size_sect, 8);
779
780 /* plus the "drbd meta data super block",
781 * and the activity log; */
ae8bf312 782 md_size_sect += MD_4kB_SECT + al_size_sect;
b411b363
PR
783
784 bdev->md.md_size_sect = md_size_sect;
785 /* bitmap offset is adjusted by 'super' block size */
ae8bf312 786 bdev->md.bm_offset = -md_size_sect + MD_4kB_SECT;
b411b363
PR
787 break;
788 }
789}
790
4b0715f0 791/* input size is expected to be in KB */
b411b363
PR
792char *ppsize(char *buf, unsigned long long size)
793{
4b0715f0
LE
794 /* Needs 9 bytes at max including trailing NUL:
795 * -1ULL ==> "16384 EB" */
b411b363
PR
796 static char units[] = { 'K', 'M', 'G', 'T', 'P', 'E' };
797 int base = 0;
4b0715f0 798 while (size >= 10000 && base < sizeof(units)-1) {
b411b363
PR
799 /* shift + round */
800 size = (size >> 10) + !!(size & (1<<9));
801 base++;
802 }
4b0715f0 803 sprintf(buf, "%u %cB", (unsigned)size, units[base]);
b411b363
PR
804
805 return buf;
806}
807
808/* there is still a theoretical deadlock when called from receiver
809 * on an D_INCONSISTENT R_PRIMARY:
810 * remote READ does inc_ap_bio, receiver would need to receive answer
811 * packet from remote to dec_ap_bio again.
812 * receiver receive_sizes(), comes here,
813 * waits for ap_bio_cnt == 0. -> deadlock.
814 * but this cannot happen, actually, because:
815 * R_PRIMARY D_INCONSISTENT, and peer's disk is unreachable
816 * (not connected, or bad/no disk on peer):
817 * see drbd_fail_request_early, ap_bio_cnt is zero.
818 * R_PRIMARY D_INCONSISTENT, and C_SYNC_TARGET:
819 * peer may not initiate a resize.
820 */
3b98c0c2
LE
821/* Note these are not to be confused with
822 * drbd_adm_suspend_io/drbd_adm_resume_io,
823 * which are (sub) state changes triggered by admin (drbdsetup),
824 * and can be long lived.
b30ab791 825 * This changes an device->flag, is triggered by drbd internals,
3b98c0c2 826 * and should be short-lived. */
b30ab791 827void drbd_suspend_io(struct drbd_device *device)
b411b363 828{
b30ab791
AG
829 set_bit(SUSPEND_IO, &device->flags);
830 if (drbd_suspended(device))
265be2d0 831 return;
b30ab791 832 wait_event(device->misc_wait, !atomic_read(&device->ap_bio_cnt));
b411b363
PR
833}
834
b30ab791 835void drbd_resume_io(struct drbd_device *device)
b411b363 836{
b30ab791
AG
837 clear_bit(SUSPEND_IO, &device->flags);
838 wake_up(&device->misc_wait);
b411b363
PR
839}
840
841/**
842 * drbd_determine_dev_size() - Sets the right device size obeying all constraints
b30ab791 843 * @device: DRBD device.
b411b363
PR
844 *
845 * Returns 0 on success, negative return values indicate errors.
846 * You should call drbd_md_sync() after calling this function.
847 */
d752b269 848enum determine_dev_size
b30ab791 849drbd_determine_dev_size(struct drbd_device *device, enum dds_flags flags, struct resize_parms *rs) __must_hold(local)
b411b363
PR
850{
851 sector_t prev_first_sect, prev_size; /* previous meta location */
cccac985 852 sector_t la_size_sect, u_size;
b30ab791 853 struct drbd_md *md = &device->ldev->md;
d752b269
PR
854 u32 prev_al_stripe_size_4k;
855 u32 prev_al_stripes;
b411b363
PR
856 sector_t size;
857 char ppb[10];
d752b269 858 void *buffer;
b411b363
PR
859
860 int md_moved, la_size_changed;
e96c9633 861 enum determine_dev_size rv = DS_UNCHANGED;
b411b363
PR
862
863 /* race:
864 * application request passes inc_ap_bio,
865 * but then cannot get an AL-reference.
866 * this function later may wait on ap_bio_cnt == 0. -> deadlock.
867 *
868 * to avoid that:
869 * Suspend IO right here.
870 * still lock the act_log to not trigger ASSERTs there.
871 */
b30ab791
AG
872 drbd_suspend_io(device);
873 buffer = drbd_md_get_buffer(device); /* Lock meta-data IO */
d752b269 874 if (!buffer) {
b30ab791 875 drbd_resume_io(device);
d752b269
PR
876 return DS_ERROR;
877 }
b411b363
PR
878
879 /* no wait necessary anymore, actually we could assert that */
b30ab791 880 wait_event(device->al_wait, lc_try_lock(device->act_log));
b411b363 881
b30ab791
AG
882 prev_first_sect = drbd_md_first_sector(device->ldev);
883 prev_size = device->ldev->md.md_size_sect;
884 la_size_sect = device->ldev->md.la_size_sect;
b411b363 885
d752b269
PR
886 if (rs) {
887 /* rs is non NULL if we should change the AL layout only */
888
889 prev_al_stripes = md->al_stripes;
890 prev_al_stripe_size_4k = md->al_stripe_size_4k;
891
892 md->al_stripes = rs->al_stripes;
893 md->al_stripe_size_4k = rs->al_stripe_size / 4;
894 md->al_size_4k = (u64)rs->al_stripes * rs->al_stripe_size / 4;
895 }
896
b30ab791 897 drbd_md_set_sector_offsets(device, device->ldev);
b411b363 898
daeda1cc 899 rcu_read_lock();
b30ab791 900 u_size = rcu_dereference(device->ldev->disk_conf)->disk_size;
daeda1cc 901 rcu_read_unlock();
b30ab791 902 size = drbd_new_dev_size(device, device->ldev, u_size, flags & DDSF_FORCED);
b411b363 903
d752b269
PR
904 if (size < la_size_sect) {
905 if (rs && u_size == 0) {
906 /* Remove "rs &&" later. This check should always be active, but
907 right now the receiver expects the permissive behavior */
d0180171 908 drbd_warn(device, "Implicit shrink not allowed. "
d752b269
PR
909 "Use --size=%llus for explicit shrink.\n",
910 (unsigned long long)size);
911 rv = DS_ERROR_SHRINK;
912 }
913 if (u_size > size)
914 rv = DS_ERROR_SPACE_MD;
915 if (rv != DS_UNCHANGED)
916 goto err_out;
917 }
918
b30ab791
AG
919 if (drbd_get_capacity(device->this_bdev) != size ||
920 drbd_bm_capacity(device) != size) {
b411b363 921 int err;
b30ab791 922 err = drbd_bm_resize(device, size, !(flags & DDSF_NO_RESYNC));
b411b363
PR
923 if (unlikely(err)) {
924 /* currently there is only one error: ENOMEM! */
b30ab791 925 size = drbd_bm_capacity(device)>>1;
b411b363 926 if (size == 0) {
d0180171 927 drbd_err(device, "OUT OF MEMORY! "
b411b363
PR
928 "Could not allocate bitmap!\n");
929 } else {
d0180171 930 drbd_err(device, "BM resizing failed. "
b411b363
PR
931 "Leaving size unchanged at size = %lu KB\n",
932 (unsigned long)size);
933 }
e96c9633 934 rv = DS_ERROR;
b411b363
PR
935 }
936 /* racy, see comments above. */
b30ab791
AG
937 drbd_set_my_capacity(device, size);
938 device->ldev->md.la_size_sect = size;
d0180171 939 drbd_info(device, "size = %s (%llu KB)\n", ppsize(ppb, size>>1),
b411b363
PR
940 (unsigned long long)size>>1);
941 }
d752b269
PR
942 if (rv <= DS_ERROR)
943 goto err_out;
b411b363 944
b30ab791 945 la_size_changed = (la_size_sect != device->ldev->md.la_size_sect);
b411b363 946
b30ab791
AG
947 md_moved = prev_first_sect != drbd_md_first_sector(device->ldev)
948 || prev_size != device->ldev->md.md_size_sect;
b411b363 949
d752b269
PR
950 if (la_size_changed || md_moved || rs) {
951 u32 prev_flags;
24dccabb 952
b30ab791 953 drbd_al_shrink(device); /* All extents inactive. */
d752b269
PR
954
955 prev_flags = md->flags;
956 md->flags &= ~MDF_PRIMARY_IND;
b30ab791 957 drbd_md_write(device, buffer);
d752b269 958
d0180171 959 drbd_info(device, "Writing the whole bitmap, %s\n",
b411b363
PR
960 la_size_changed && md_moved ? "size changed and md moved" :
961 la_size_changed ? "size changed" : "md moved");
20ceb2b2 962 /* next line implicitly does drbd_suspend_io()+drbd_resume_io() */
b30ab791 963 drbd_bitmap_io(device, md_moved ? &drbd_bm_write_all : &drbd_bm_write,
d752b269 964 "size changed", BM_LOCKED_MASK);
b30ab791 965 drbd_initialize_al(device, buffer);
d752b269
PR
966
967 md->flags = prev_flags;
b30ab791 968 drbd_md_write(device, buffer);
d752b269
PR
969
970 if (rs)
d0180171
AG
971 drbd_info(device, "Changed AL layout to al-stripes = %d, al-stripe-size-kB = %d\n",
972 md->al_stripes, md->al_stripe_size_4k * 4);
b411b363
PR
973 }
974
cccac985 975 if (size > la_size_sect)
57737adc 976 rv = la_size_sect ? DS_GREW : DS_GREW_FROM_ZERO;
cccac985 977 if (size < la_size_sect)
e96c9633 978 rv = DS_SHRUNK;
d752b269
PR
979
980 if (0) {
981 err_out:
982 if (rs) {
983 md->al_stripes = prev_al_stripes;
984 md->al_stripe_size_4k = prev_al_stripe_size_4k;
985 md->al_size_4k = (u64)prev_al_stripes * prev_al_stripe_size_4k;
986
b30ab791 987 drbd_md_set_sector_offsets(device, device->ldev);
d752b269
PR
988 }
989 }
b30ab791
AG
990 lc_unlock(device->act_log);
991 wake_up(&device->al_wait);
992 drbd_md_put_buffer(device);
993 drbd_resume_io(device);
b411b363
PR
994
995 return rv;
996}
997
998sector_t
b30ab791 999drbd_new_dev_size(struct drbd_device *device, struct drbd_backing_dev *bdev,
ef5e44a6 1000 sector_t u_size, int assume_peer_has_space)
b411b363 1001{
b30ab791 1002 sector_t p_size = device->p_size; /* partner's disk size. */
cccac985 1003 sector_t la_size_sect = bdev->md.la_size_sect; /* last agreed size. */
b411b363 1004 sector_t m_size; /* my size */
b411b363
PR
1005 sector_t size = 0;
1006
1007 m_size = drbd_get_max_capacity(bdev);
1008
b30ab791 1009 if (device->state.conn < C_CONNECTED && assume_peer_has_space) {
d0180171 1010 drbd_warn(device, "Resize while not connected was forced by the user!\n");
a393db6f
PR
1011 p_size = m_size;
1012 }
1013
b411b363
PR
1014 if (p_size && m_size) {
1015 size = min_t(sector_t, p_size, m_size);
1016 } else {
cccac985
LE
1017 if (la_size_sect) {
1018 size = la_size_sect;
b411b363
PR
1019 if (m_size && m_size < size)
1020 size = m_size;
1021 if (p_size && p_size < size)
1022 size = p_size;
1023 } else {
1024 if (m_size)
1025 size = m_size;
1026 if (p_size)
1027 size = p_size;
1028 }
1029 }
1030
1031 if (size == 0)
d0180171 1032 drbd_err(device, "Both nodes diskless!\n");
b411b363
PR
1033
1034 if (u_size) {
1035 if (u_size > size)
d0180171 1036 drbd_err(device, "Requested disk size is too big (%lu > %lu)\n",
b411b363
PR
1037 (unsigned long)u_size>>1, (unsigned long)size>>1);
1038 else
1039 size = u_size;
1040 }
1041
1042 return size;
1043}
1044
1045/**
1046 * drbd_check_al_size() - Ensures that the AL is of the right size
b30ab791 1047 * @device: DRBD device.
b411b363
PR
1048 *
1049 * Returns -EBUSY if current al lru is still used, -ENOMEM when allocation
1050 * failed, and 0 on success. You should call drbd_md_sync() after you called
1051 * this function.
1052 */
b30ab791 1053static int drbd_check_al_size(struct drbd_device *device, struct disk_conf *dc)
b411b363
PR
1054{
1055 struct lru_cache *n, *t;
1056 struct lc_element *e;
1057 unsigned int in_use;
1058 int i;
1059
b30ab791
AG
1060 if (device->act_log &&
1061 device->act_log->nr_elements == dc->al_extents)
b411b363
PR
1062 return 0;
1063
1064 in_use = 0;
b30ab791 1065 t = device->act_log;
7ad651b5 1066 n = lc_create("act_log", drbd_al_ext_cache, AL_UPDATES_PER_TRANSACTION,
f399002e 1067 dc->al_extents, sizeof(struct lc_element), 0);
b411b363
PR
1068
1069 if (n == NULL) {
d0180171 1070 drbd_err(device, "Cannot allocate act_log lru!\n");
b411b363
PR
1071 return -ENOMEM;
1072 }
b30ab791 1073 spin_lock_irq(&device->al_lock);
b411b363
PR
1074 if (t) {
1075 for (i = 0; i < t->nr_elements; i++) {
1076 e = lc_element_by_index(t, i);
1077 if (e->refcnt)
d0180171 1078 drbd_err(device, "refcnt(%d)==%d\n",
b411b363
PR
1079 e->lc_number, e->refcnt);
1080 in_use += e->refcnt;
1081 }
1082 }
1083 if (!in_use)
b30ab791
AG
1084 device->act_log = n;
1085 spin_unlock_irq(&device->al_lock);
b411b363 1086 if (in_use) {
d0180171 1087 drbd_err(device, "Activity log still in use!\n");
b411b363
PR
1088 lc_destroy(n);
1089 return -EBUSY;
1090 } else {
1091 if (t)
1092 lc_destroy(t);
1093 }
b30ab791 1094 drbd_md_mark_dirty(device); /* we changed device->act_log->nr_elemens */
b411b363
PR
1095 return 0;
1096}
1097
b30ab791 1098static void drbd_setup_queue_param(struct drbd_device *device, unsigned int max_bio_size)
b411b363 1099{
b30ab791 1100 struct request_queue * const q = device->rq_queue;
db141b2f
LE
1101 unsigned int max_hw_sectors = max_bio_size >> 9;
1102 unsigned int max_segments = 0;
99432fcc 1103
b30ab791
AG
1104 if (get_ldev_if_state(device, D_ATTACHING)) {
1105 struct request_queue * const b = device->ldev->backing_bdev->bd_disk->queue;
99432fcc
PR
1106
1107 max_hw_sectors = min(queue_max_hw_sectors(b), max_bio_size >> 9);
daeda1cc 1108 rcu_read_lock();
b30ab791 1109 max_segments = rcu_dereference(device->ldev->disk_conf)->max_bio_bvecs;
daeda1cc 1110 rcu_read_unlock();
b30ab791 1111 put_ldev(device);
99432fcc 1112 }
b411b363 1113
b411b363 1114 blk_queue_logical_block_size(q, 512);
1816a2b4
LE
1115 blk_queue_max_hw_sectors(q, max_hw_sectors);
1116 /* This is the workaround for "bio would need to, but cannot, be split" */
1117 blk_queue_max_segments(q, max_segments ? max_segments : BLK_MAX_SEGMENTS);
1118 blk_queue_segment_boundary(q, PAGE_CACHE_SIZE-1);
b411b363 1119
b30ab791
AG
1120 if (get_ldev_if_state(device, D_ATTACHING)) {
1121 struct request_queue * const b = device->ldev->backing_bdev->bd_disk->queue;
99432fcc
PR
1122
1123 blk_queue_stack_limits(q, b);
1124
1125 if (q->backing_dev_info.ra_pages != b->backing_dev_info.ra_pages) {
d0180171 1126 drbd_info(device, "Adjusting my ra_pages to backing device's (%lu -> %lu)\n",
99432fcc
PR
1127 q->backing_dev_info.ra_pages,
1128 b->backing_dev_info.ra_pages);
1129 q->backing_dev_info.ra_pages = b->backing_dev_info.ra_pages;
1130 }
b30ab791 1131 put_ldev(device);
99432fcc
PR
1132 }
1133}
1134
b30ab791 1135void drbd_reconsider_max_bio_size(struct drbd_device *device)
99432fcc 1136{
db141b2f 1137 unsigned int now, new, local, peer;
99432fcc 1138
b30ab791
AG
1139 now = queue_max_hw_sectors(device->rq_queue) << 9;
1140 local = device->local_max_bio_size; /* Eventually last known value, from volatile memory */
1141 peer = device->peer_max_bio_size; /* Eventually last known value, from meta data */
b411b363 1142
b30ab791
AG
1143 if (get_ldev_if_state(device, D_ATTACHING)) {
1144 local = queue_max_hw_sectors(device->ldev->backing_bdev->bd_disk->queue) << 9;
1145 device->local_max_bio_size = local;
1146 put_ldev(device);
b411b363 1147 }
db141b2f 1148 local = min(local, DRBD_MAX_BIO_SIZE);
99432fcc
PR
1149
1150 /* We may ignore peer limits if the peer is modern enough.
1151 Because new from 8.3.8 onwards the peer can use multiple
1152 BIOs for a single peer_request */
b30ab791 1153 if (device->state.conn >= C_WF_REPORT_PARAMS) {
a6b32bc3 1154 if (first_peer_device(device)->connection->agreed_pro_version < 94)
b30ab791 1155 peer = min(device->peer_max_bio_size, DRBD_MAX_SIZE_H80_PACKET);
6809384c 1156 /* Correct old drbd (up to 8.3.7) if it believes it can do more than 32KiB */
a6b32bc3 1157 else if (first_peer_device(device)->connection->agreed_pro_version == 94)
99432fcc 1158 peer = DRBD_MAX_SIZE_H80_PACKET;
a6b32bc3 1159 else if (first_peer_device(device)->connection->agreed_pro_version < 100)
2ffca4f3
PR
1160 peer = DRBD_MAX_BIO_SIZE_P95; /* drbd 8.3.8 onwards, before 8.4.0 */
1161 else
99432fcc
PR
1162 peer = DRBD_MAX_BIO_SIZE;
1163 }
1164
db141b2f 1165 new = min(local, peer);
99432fcc 1166
b30ab791 1167 if (device->state.role == R_PRIMARY && new < now)
d0180171 1168 drbd_err(device, "ASSERT FAILED new < now; (%u < %u)\n", new, now);
99432fcc
PR
1169
1170 if (new != now)
d0180171 1171 drbd_info(device, "max BIO size = %u\n", new);
99432fcc 1172
b30ab791 1173 drbd_setup_queue_param(device, new);
b411b363
PR
1174}
1175
a18e9d1e 1176/* Starts the worker thread */
bde89a9e 1177static void conn_reconfig_start(struct drbd_connection *connection)
b411b363 1178{
bde89a9e
AG
1179 drbd_thread_start(&connection->worker);
1180 conn_flush_workqueue(connection);
b411b363
PR
1181}
1182
a18e9d1e 1183/* if still unconfigured, stops worker again. */
bde89a9e 1184static void conn_reconfig_done(struct drbd_connection *connection)
b411b363 1185{
992d6e91 1186 bool stop_threads;
bde89a9e
AG
1187 spin_lock_irq(&connection->req_lock);
1188 stop_threads = conn_all_vols_unconf(connection) &&
1189 connection->cstate == C_STANDALONE;
1190 spin_unlock_irq(&connection->req_lock);
992d6e91
LE
1191 if (stop_threads) {
1192 /* asender is implicitly stopped by receiver
81fa2e67 1193 * in conn_disconnect() */
bde89a9e
AG
1194 drbd_thread_stop(&connection->receiver);
1195 drbd_thread_stop(&connection->worker);
992d6e91 1196 }
b411b363
PR
1197}
1198
0778286a 1199/* Make sure IO is suspended before calling this function(). */
b30ab791 1200static void drbd_suspend_al(struct drbd_device *device)
0778286a
PR
1201{
1202 int s = 0;
1203
b30ab791 1204 if (!lc_try_lock(device->act_log)) {
d0180171 1205 drbd_warn(device, "Failed to lock al in drbd_suspend_al()\n");
0778286a
PR
1206 return;
1207 }
1208
b30ab791 1209 drbd_al_shrink(device);
a6b32bc3 1210 spin_lock_irq(&first_peer_device(device)->connection->req_lock);
b30ab791
AG
1211 if (device->state.conn < C_CONNECTED)
1212 s = !test_and_set_bit(AL_SUSPENDED, &device->flags);
a6b32bc3 1213 spin_unlock_irq(&first_peer_device(device)->connection->req_lock);
b30ab791 1214 lc_unlock(device->act_log);
0778286a
PR
1215
1216 if (s)
d0180171 1217 drbd_info(device, "Suspended AL updates\n");
0778286a
PR
1218}
1219
5979e361
LE
1220
1221static bool should_set_defaults(struct genl_info *info)
1222{
1223 unsigned flags = ((struct drbd_genlmsghdr*)info->userhdr)->flags;
1224 return 0 != (flags & DRBD_GENL_F_SET_DEFAULTS);
1225}
1226
5bbcf5e6 1227static unsigned int drbd_al_extents_max(struct drbd_backing_dev *bdev)
d589a21e 1228{
5bbcf5e6
LE
1229 /* This is limited by 16 bit "slot" numbers,
1230 * and by available on-disk context storage.
1231 *
1232 * Also (u16)~0 is special (denotes a "free" extent).
1233 *
1234 * One transaction occupies one 4kB on-disk block,
1235 * we have n such blocks in the on disk ring buffer,
1236 * the "current" transaction may fail (n-1),
1237 * and there is 919 slot numbers context information per transaction.
1238 *
1239 * 72 transaction blocks amounts to more than 2**16 context slots,
1240 * so cap there first.
1241 */
1242 const unsigned int max_al_nr = DRBD_AL_EXTENTS_MAX;
1243 const unsigned int sufficient_on_disk =
1244 (max_al_nr + AL_CONTEXT_PER_TRANSACTION -1)
1245 /AL_CONTEXT_PER_TRANSACTION;
d589a21e 1246
5bbcf5e6
LE
1247 unsigned int al_size_4k = bdev->md.al_size_4k;
1248
1249 if (al_size_4k > sufficient_on_disk)
1250 return max_al_nr;
1251
1252 return (al_size_4k - 1) * AL_CONTEXT_PER_TRANSACTION;
d589a21e
PR
1253}
1254
f399002e
LE
1255int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info)
1256{
1257 enum drbd_ret_code retcode;
b30ab791 1258 struct drbd_device *device;
daeda1cc 1259 struct disk_conf *new_disk_conf, *old_disk_conf;
813472ce 1260 struct fifo_buffer *old_plan = NULL, *new_plan = NULL;
f399002e 1261 int err, fifo_size;
f399002e
LE
1262
1263 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1264 if (!adm_ctx.reply_skb)
1265 return retcode;
1266 if (retcode != NO_ERROR)
1267 goto out;
1268
b30ab791 1269 device = adm_ctx.device;
f399002e
LE
1270
1271 /* we also need a disk
1272 * to change the options on */
b30ab791 1273 if (!get_ldev(device)) {
f399002e
LE
1274 retcode = ERR_NO_DISK;
1275 goto out;
1276 }
1277
daeda1cc 1278 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
5ecc72c3 1279 if (!new_disk_conf) {
f399002e
LE
1280 retcode = ERR_NOMEM;
1281 goto fail;
1282 }
1283
a6b32bc3 1284 mutex_lock(&first_peer_device(device)->connection->conf_update);
b30ab791 1285 old_disk_conf = device->ldev->disk_conf;
daeda1cc 1286 *new_disk_conf = *old_disk_conf;
5979e361 1287 if (should_set_defaults(info))
b966b5dd 1288 set_disk_conf_defaults(new_disk_conf);
5979e361 1289
5ecc72c3 1290 err = disk_conf_from_attrs_for_change(new_disk_conf, info);
c75b9b10 1291 if (err && err != -ENOMSG) {
f399002e
LE
1292 retcode = ERR_MANDATORY_TAG;
1293 drbd_msg_put_info(from_attrs_err_to_txt(err));
8e229434 1294 goto fail_unlock;
f399002e
LE
1295 }
1296
5ecc72c3
LE
1297 if (!expect(new_disk_conf->resync_rate >= 1))
1298 new_disk_conf->resync_rate = 1;
f399002e 1299
5bbcf5e6
LE
1300 if (new_disk_conf->al_extents < DRBD_AL_EXTENTS_MIN)
1301 new_disk_conf->al_extents = DRBD_AL_EXTENTS_MIN;
b30ab791
AG
1302 if (new_disk_conf->al_extents > drbd_al_extents_max(device->ldev))
1303 new_disk_conf->al_extents = drbd_al_extents_max(device->ldev);
5bbcf5e6
LE
1304
1305 if (new_disk_conf->c_plan_ahead > DRBD_C_PLAN_AHEAD_MAX)
1306 new_disk_conf->c_plan_ahead = DRBD_C_PLAN_AHEAD_MAX;
f399002e 1307
5ecc72c3 1308 fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
b30ab791 1309 if (fifo_size != device->rs_plan_s->size) {
813472ce
PR
1310 new_plan = fifo_alloc(fifo_size);
1311 if (!new_plan) {
d0180171 1312 drbd_err(device, "kmalloc of fifo_buffer failed");
f399002e 1313 retcode = ERR_NOMEM;
daeda1cc 1314 goto fail_unlock;
f399002e
LE
1315 }
1316 }
1317
b30ab791
AG
1318 drbd_suspend_io(device);
1319 wait_event(device->al_wait, lc_try_lock(device->act_log));
1320 drbd_al_shrink(device);
1321 err = drbd_check_al_size(device, new_disk_conf);
1322 lc_unlock(device->act_log);
1323 wake_up(&device->al_wait);
1324 drbd_resume_io(device);
f399002e
LE
1325
1326 if (err) {
1327 retcode = ERR_NOMEM;
daeda1cc 1328 goto fail_unlock;
f399002e
LE
1329 }
1330
dc97b708 1331 write_lock_irq(&global_state_lock);
b30ab791 1332 retcode = drbd_resync_after_valid(device, new_disk_conf->resync_after);
dc97b708 1333 if (retcode == NO_ERROR) {
b30ab791
AG
1334 rcu_assign_pointer(device->ldev->disk_conf, new_disk_conf);
1335 drbd_resync_after_changed(device);
dc97b708
PR
1336 }
1337 write_unlock_irq(&global_state_lock);
f399002e 1338
daeda1cc
PR
1339 if (retcode != NO_ERROR)
1340 goto fail_unlock;
f399002e 1341
813472ce 1342 if (new_plan) {
b30ab791
AG
1343 old_plan = device->rs_plan_s;
1344 rcu_assign_pointer(device->rs_plan_s, new_plan);
9958c857 1345 }
9958c857 1346
a6b32bc3 1347 mutex_unlock(&first_peer_device(device)->connection->conf_update);
27eb13e9 1348
9a51ab1c 1349 if (new_disk_conf->al_updates)
b30ab791 1350 device->ldev->md.flags &= ~MDF_AL_DISABLED;
9a51ab1c 1351 else
b30ab791 1352 device->ldev->md.flags |= MDF_AL_DISABLED;
9a51ab1c 1353
691631c0 1354 if (new_disk_conf->md_flushes)
b30ab791 1355 clear_bit(MD_NO_FUA, &device->flags);
691631c0 1356 else
b30ab791 1357 set_bit(MD_NO_FUA, &device->flags);
691631c0 1358
a6b32bc3 1359 drbd_bump_write_ordering(first_peer_device(device)->connection, WO_bdev_flush);
27eb13e9 1360
b30ab791 1361 drbd_md_sync(device);
f399002e 1362
b30ab791
AG
1363 if (device->state.conn >= C_CONNECTED)
1364 drbd_send_sync_param(device);
f399002e 1365
daeda1cc
PR
1366 synchronize_rcu();
1367 kfree(old_disk_conf);
813472ce 1368 kfree(old_plan);
b30ab791 1369 mod_timer(&device->request_timer, jiffies + HZ);
daeda1cc
PR
1370 goto success;
1371
1372fail_unlock:
a6b32bc3 1373 mutex_unlock(&first_peer_device(device)->connection->conf_update);
f399002e 1374 fail:
5ecc72c3 1375 kfree(new_disk_conf);
813472ce 1376 kfree(new_plan);
daeda1cc 1377success:
b30ab791 1378 put_ldev(device);
f399002e
LE
1379 out:
1380 drbd_adm_finish(info, retcode);
1381 return 0;
1382}
1383
3b98c0c2 1384int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info)
b411b363 1385{
b30ab791 1386 struct drbd_device *device;
3b98c0c2 1387 int err;
116676ca 1388 enum drbd_ret_code retcode;
b411b363
PR
1389 enum determine_dev_size dd;
1390 sector_t max_possible_sectors;
1391 sector_t min_md_device_sectors;
1392 struct drbd_backing_dev *nbc = NULL; /* new_backing_conf */
daeda1cc 1393 struct disk_conf *new_disk_conf = NULL;
e525fd89 1394 struct block_device *bdev;
b411b363 1395 struct lru_cache *resync_lru = NULL;
9958c857 1396 struct fifo_buffer *new_plan = NULL;
b411b363 1397 union drbd_state ns, os;
f2024e7c 1398 enum drbd_state_rv rv;
44ed167d 1399 struct net_conf *nc;
b411b363 1400
3b98c0c2
LE
1401 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1402 if (!adm_ctx.reply_skb)
1403 return retcode;
1404 if (retcode != NO_ERROR)
40cbf085 1405 goto finish;
b411b363 1406
b30ab791 1407 device = adm_ctx.device;
a6b32bc3 1408 conn_reconfig_start(first_peer_device(device)->connection);
b411b363
PR
1409
1410 /* if you want to reconfigure, please tear down first */
b30ab791 1411 if (device->state.disk > D_DISKLESS) {
b411b363
PR
1412 retcode = ERR_DISK_CONFIGURED;
1413 goto fail;
1414 }
82f59cc6
LE
1415 /* It may just now have detached because of IO error. Make sure
1416 * drbd_ldev_destroy is done already, we may end up here very fast,
1417 * e.g. if someone calls attach from the on-io-error handler,
1418 * to realize a "hot spare" feature (not that I'd recommend that) */
b30ab791 1419 wait_event(device->misc_wait, !atomic_read(&device->local_cnt));
b411b363 1420
383606e0 1421 /* make sure there is no leftover from previous force-detach attempts */
b30ab791
AG
1422 clear_bit(FORCE_DETACH, &device->flags);
1423 clear_bit(WAS_IO_ERROR, &device->flags);
1424 clear_bit(WAS_READ_ERROR, &device->flags);
383606e0 1425
0029d624 1426 /* and no leftover from previously aborted resync or verify, either */
b30ab791
AG
1427 device->rs_total = 0;
1428 device->rs_failed = 0;
1429 atomic_set(&device->rs_pending_cnt, 0);
0029d624 1430
3b98c0c2 1431 /* allocation not in the IO path, drbdsetup context */
b411b363
PR
1432 nbc = kzalloc(sizeof(struct drbd_backing_dev), GFP_KERNEL);
1433 if (!nbc) {
1434 retcode = ERR_NOMEM;
1435 goto fail;
1436 }
9f2247bb
PR
1437 spin_lock_init(&nbc->md.uuid_lock);
1438
daeda1cc
PR
1439 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
1440 if (!new_disk_conf) {
1441 retcode = ERR_NOMEM;
b411b363
PR
1442 goto fail;
1443 }
daeda1cc 1444 nbc->disk_conf = new_disk_conf;
b411b363 1445
daeda1cc
PR
1446 set_disk_conf_defaults(new_disk_conf);
1447 err = disk_conf_from_attrs(new_disk_conf, info);
3b98c0c2 1448 if (err) {
b411b363 1449 retcode = ERR_MANDATORY_TAG;
3b98c0c2 1450 drbd_msg_put_info(from_attrs_err_to_txt(err));
b411b363
PR
1451 goto fail;
1452 }
1453
5bbcf5e6
LE
1454 if (new_disk_conf->c_plan_ahead > DRBD_C_PLAN_AHEAD_MAX)
1455 new_disk_conf->c_plan_ahead = DRBD_C_PLAN_AHEAD_MAX;
d589a21e 1456
9958c857
PR
1457 new_plan = fifo_alloc((new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ);
1458 if (!new_plan) {
1459 retcode = ERR_NOMEM;
1460 goto fail;
1461 }
1462
daeda1cc 1463 if (new_disk_conf->meta_dev_idx < DRBD_MD_INDEX_FLEX_INT) {
b411b363
PR
1464 retcode = ERR_MD_IDX_INVALID;
1465 goto fail;
1466 }
1467
a3f8f7dc 1468 write_lock_irq(&global_state_lock);
b30ab791 1469 retcode = drbd_resync_after_valid(device, new_disk_conf->resync_after);
a3f8f7dc
LE
1470 write_unlock_irq(&global_state_lock);
1471 if (retcode != NO_ERROR)
1472 goto fail;
1473
44ed167d 1474 rcu_read_lock();
a6b32bc3 1475 nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
44ed167d 1476 if (nc) {
daeda1cc 1477 if (new_disk_conf->fencing == FP_STONITH && nc->wire_protocol == DRBD_PROT_A) {
44ed167d 1478 rcu_read_unlock();
47ff2d0a
PR
1479 retcode = ERR_STONITH_AND_PROT_A;
1480 goto fail;
1481 }
1482 }
44ed167d 1483 rcu_read_unlock();
47ff2d0a 1484
daeda1cc 1485 bdev = blkdev_get_by_path(new_disk_conf->backing_dev,
b30ab791 1486 FMODE_READ | FMODE_WRITE | FMODE_EXCL, device);
e525fd89 1487 if (IS_ERR(bdev)) {
d0180171 1488 drbd_err(device, "open(\"%s\") failed with %ld\n", new_disk_conf->backing_dev,
e525fd89 1489 PTR_ERR(bdev));
b411b363
PR
1490 retcode = ERR_OPEN_DISK;
1491 goto fail;
1492 }
e525fd89
TH
1493 nbc->backing_bdev = bdev;
1494
1495 /*
1496 * meta_dev_idx >= 0: external fixed size, possibly multiple
1497 * drbd sharing one meta device. TODO in that case, paranoia
1498 * check that [md_bdev, meta_dev_idx] is not yet used by some
1499 * other drbd minor! (if you use drbd.conf + drbdadm, that
1500 * should check it for you already; but if you don't, or
1501 * someone fooled it, we need to double check here)
1502 */
daeda1cc 1503 bdev = blkdev_get_by_path(new_disk_conf->meta_dev,
d4d77629 1504 FMODE_READ | FMODE_WRITE | FMODE_EXCL,
daeda1cc 1505 (new_disk_conf->meta_dev_idx < 0) ?
b30ab791 1506 (void *)device : (void *)drbd_m_holder);
e525fd89 1507 if (IS_ERR(bdev)) {
d0180171 1508 drbd_err(device, "open(\"%s\") failed with %ld\n", new_disk_conf->meta_dev,
e525fd89 1509 PTR_ERR(bdev));
b411b363
PR
1510 retcode = ERR_OPEN_MD_DISK;
1511 goto fail;
1512 }
e525fd89 1513 nbc->md_bdev = bdev;
b411b363 1514
e525fd89 1515 if ((nbc->backing_bdev == nbc->md_bdev) !=
daeda1cc
PR
1516 (new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_INTERNAL ||
1517 new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_FLEX_INT)) {
e525fd89 1518 retcode = ERR_MD_IDX_INVALID;
b411b363
PR
1519 goto fail;
1520 }
1521
1522 resync_lru = lc_create("resync", drbd_bm_ext_cache,
46a15bc3 1523 1, 61, sizeof(struct bm_extent),
b411b363
PR
1524 offsetof(struct bm_extent, lce));
1525 if (!resync_lru) {
1526 retcode = ERR_NOMEM;
e525fd89 1527 goto fail;
b411b363
PR
1528 }
1529
c04ccaa6
LE
1530 /* Read our meta data super block early.
1531 * This also sets other on-disk offsets. */
b30ab791 1532 retcode = drbd_md_read(device, nbc);
c04ccaa6
LE
1533 if (retcode != NO_ERROR)
1534 goto fail;
b411b363 1535
5bbcf5e6
LE
1536 if (new_disk_conf->al_extents < DRBD_AL_EXTENTS_MIN)
1537 new_disk_conf->al_extents = DRBD_AL_EXTENTS_MIN;
1538 if (new_disk_conf->al_extents > drbd_al_extents_max(nbc))
1539 new_disk_conf->al_extents = drbd_al_extents_max(nbc);
1540
daeda1cc 1541 if (drbd_get_max_capacity(nbc) < new_disk_conf->disk_size) {
d0180171 1542 drbd_err(device, "max capacity %llu smaller than disk size %llu\n",
b411b363 1543 (unsigned long long) drbd_get_max_capacity(nbc),
daeda1cc 1544 (unsigned long long) new_disk_conf->disk_size);
7948bcdc 1545 retcode = ERR_DISK_TOO_SMALL;
e525fd89 1546 goto fail;
b411b363
PR
1547 }
1548
daeda1cc 1549 if (new_disk_conf->meta_dev_idx < 0) {
b411b363
PR
1550 max_possible_sectors = DRBD_MAX_SECTORS_FLEX;
1551 /* at least one MB, otherwise it does not make sense */
1552 min_md_device_sectors = (2<<10);
1553 } else {
1554 max_possible_sectors = DRBD_MAX_SECTORS;
ae8bf312 1555 min_md_device_sectors = MD_128MB_SECT * (new_disk_conf->meta_dev_idx + 1);
b411b363
PR
1556 }
1557
b411b363 1558 if (drbd_get_capacity(nbc->md_bdev) < min_md_device_sectors) {
7948bcdc 1559 retcode = ERR_MD_DISK_TOO_SMALL;
d0180171 1560 drbd_warn(device, "refusing attach: md-device too small, "
b411b363
PR
1561 "at least %llu sectors needed for this meta-disk type\n",
1562 (unsigned long long) min_md_device_sectors);
e525fd89 1563 goto fail;
b411b363
PR
1564 }
1565
1566 /* Make sure the new disk is big enough
1567 * (we may currently be R_PRIMARY with no local disk...) */
1568 if (drbd_get_max_capacity(nbc) <
b30ab791 1569 drbd_get_capacity(device->this_bdev)) {
7948bcdc 1570 retcode = ERR_DISK_TOO_SMALL;
e525fd89 1571 goto fail;
b411b363
PR
1572 }
1573
1574 nbc->known_size = drbd_get_capacity(nbc->backing_bdev);
1575
1352994b 1576 if (nbc->known_size > max_possible_sectors) {
d0180171 1577 drbd_warn(device, "==> truncating very big lower level device "
1352994b
LE
1578 "to currently maximum possible %llu sectors <==\n",
1579 (unsigned long long) max_possible_sectors);
daeda1cc 1580 if (new_disk_conf->meta_dev_idx >= 0)
d0180171 1581 drbd_warn(device, "==>> using internal or flexible "
1352994b
LE
1582 "meta data may help <<==\n");
1583 }
1584
b30ab791 1585 drbd_suspend_io(device);
b411b363 1586 /* also wait for the last barrier ack. */
b6dd1a89
LE
1587 /* FIXME see also https://daiquiri.linbit/cgi-bin/bugzilla/show_bug.cgi?id=171
1588 * We need a way to either ignore barrier acks for barriers sent before a device
1589 * was attached, or a way to wait for all pending barrier acks to come in.
1590 * As barriers are counted per resource,
1591 * we'd need to suspend io on all devices of a resource.
1592 */
b30ab791 1593 wait_event(device->misc_wait, !atomic_read(&device->ap_pending_cnt) || drbd_suspended(device));
b411b363 1594 /* and for any other previously queued work */
b30ab791 1595 drbd_flush_workqueue(device);
b411b363 1596
b30ab791 1597 rv = _drbd_request_state(device, NS(disk, D_ATTACHING), CS_VERBOSE);
f2024e7c 1598 retcode = rv; /* FIXME: Type mismatch. */
b30ab791 1599 drbd_resume_io(device);
f2024e7c 1600 if (rv < SS_SUCCESS)
e525fd89 1601 goto fail;
b411b363 1602
b30ab791 1603 if (!get_ldev_if_state(device, D_ATTACHING))
b411b363
PR
1604 goto force_diskless;
1605
b30ab791
AG
1606 if (!device->bitmap) {
1607 if (drbd_bm_init(device)) {
b411b363
PR
1608 retcode = ERR_NOMEM;
1609 goto force_diskless_dec;
1610 }
1611 }
1612
b30ab791
AG
1613 if (device->state.conn < C_CONNECTED &&
1614 device->state.role == R_PRIMARY &&
1615 (device->ed_uuid & ~((u64)1)) != (nbc->md.uuid[UI_CURRENT] & ~((u64)1))) {
d0180171 1616 drbd_err(device, "Can only attach to data with current UUID=%016llX\n",
b30ab791 1617 (unsigned long long)device->ed_uuid);
b411b363
PR
1618 retcode = ERR_DATA_NOT_CURRENT;
1619 goto force_diskless_dec;
1620 }
1621
1622 /* Since we are diskless, fix the activity log first... */
b30ab791 1623 if (drbd_check_al_size(device, new_disk_conf)) {
b411b363
PR
1624 retcode = ERR_NOMEM;
1625 goto force_diskless_dec;
1626 }
1627
1628 /* Prevent shrinking of consistent devices ! */
1629 if (drbd_md_test_flag(nbc, MDF_CONSISTENT) &&
b30ab791 1630 drbd_new_dev_size(device, nbc, nbc->disk_conf->disk_size, 0) < nbc->md.la_size_sect) {
d0180171 1631 drbd_warn(device, "refusing to truncate a consistent device\n");
7948bcdc 1632 retcode = ERR_DISK_TOO_SMALL;
b411b363
PR
1633 goto force_diskless_dec;
1634 }
1635
b411b363
PR
1636 /* Reset the "barriers don't work" bits here, then force meta data to
1637 * be written, to ensure we determine if barriers are supported. */
e544046a 1638 if (new_disk_conf->md_flushes)
b30ab791 1639 clear_bit(MD_NO_FUA, &device->flags);
b411b363 1640 else
b30ab791 1641 set_bit(MD_NO_FUA, &device->flags);
b411b363
PR
1642
1643 /* Point of no return reached.
1644 * Devices and memory are no longer released by error cleanup below.
b30ab791 1645 * now device takes over responsibility, and the state engine should
b411b363 1646 * clean it up somewhere. */
0b0ba1ef 1647 D_ASSERT(device, device->ldev == NULL);
b30ab791
AG
1648 device->ldev = nbc;
1649 device->resync = resync_lru;
1650 device->rs_plan_s = new_plan;
b411b363
PR
1651 nbc = NULL;
1652 resync_lru = NULL;
daeda1cc 1653 new_disk_conf = NULL;
9958c857 1654 new_plan = NULL;
b411b363 1655
a6b32bc3 1656 drbd_bump_write_ordering(first_peer_device(device)->connection, WO_bdev_flush);
b411b363 1657
b30ab791
AG
1658 if (drbd_md_test_flag(device->ldev, MDF_CRASHED_PRIMARY))
1659 set_bit(CRASHED_PRIMARY, &device->flags);
b411b363 1660 else
b30ab791 1661 clear_bit(CRASHED_PRIMARY, &device->flags);
b411b363 1662
b30ab791 1663 if (drbd_md_test_flag(device->ldev, MDF_PRIMARY_IND) &&
a6b32bc3
AG
1664 !(device->state.role == R_PRIMARY &&
1665 first_peer_device(device)->connection->susp_nod))
b30ab791 1666 set_bit(CRASHED_PRIMARY, &device->flags);
b411b363 1667
b30ab791
AG
1668 device->send_cnt = 0;
1669 device->recv_cnt = 0;
1670 device->read_cnt = 0;
1671 device->writ_cnt = 0;
b411b363 1672
b30ab791 1673 drbd_reconsider_max_bio_size(device);
b411b363
PR
1674
1675 /* If I am currently not R_PRIMARY,
1676 * but meta data primary indicator is set,
1677 * I just now recover from a hard crash,
1678 * and have been R_PRIMARY before that crash.
1679 *
1680 * Now, if I had no connection before that crash
1681 * (have been degraded R_PRIMARY), chances are that
1682 * I won't find my peer now either.
1683 *
1684 * In that case, and _only_ in that case,
1685 * we use the degr-wfc-timeout instead of the default,
1686 * so we can automatically recover from a crash of a
1687 * degraded but active "cluster" after a certain timeout.
1688 */
b30ab791
AG
1689 clear_bit(USE_DEGR_WFC_T, &device->flags);
1690 if (device->state.role != R_PRIMARY &&
1691 drbd_md_test_flag(device->ldev, MDF_PRIMARY_IND) &&
1692 !drbd_md_test_flag(device->ldev, MDF_CONNECTED_IND))
1693 set_bit(USE_DEGR_WFC_T, &device->flags);
b411b363 1694
b30ab791 1695 dd = drbd_determine_dev_size(device, 0, NULL);
d752b269 1696 if (dd <= DS_ERROR) {
b411b363
PR
1697 retcode = ERR_NOMEM_BITMAP;
1698 goto force_diskless_dec;
e96c9633 1699 } else if (dd == DS_GREW)
b30ab791 1700 set_bit(RESYNC_AFTER_NEG, &device->flags);
b411b363 1701
b30ab791
AG
1702 if (drbd_md_test_flag(device->ldev, MDF_FULL_SYNC) ||
1703 (test_bit(CRASHED_PRIMARY, &device->flags) &&
1704 drbd_md_test_flag(device->ldev, MDF_AL_DISABLED))) {
d0180171 1705 drbd_info(device, "Assuming that all blocks are out of sync "
b411b363 1706 "(aka FullSync)\n");
b30ab791 1707 if (drbd_bitmap_io(device, &drbd_bmio_set_n_write,
20ceb2b2 1708 "set_n_write from attaching", BM_LOCKED_MASK)) {
b411b363
PR
1709 retcode = ERR_IO_MD_DISK;
1710 goto force_diskless_dec;
1711 }
1712 } else {
b30ab791 1713 if (drbd_bitmap_io(device, &drbd_bm_read,
22ab6a30 1714 "read from attaching", BM_LOCKED_MASK)) {
19f843aa
LE
1715 retcode = ERR_IO_MD_DISK;
1716 goto force_diskless_dec;
1717 }
b411b363
PR
1718 }
1719
b30ab791
AG
1720 if (_drbd_bm_total_weight(device) == drbd_bm_bits(device))
1721 drbd_suspend_al(device); /* IO is still suspended here... */
0778286a 1722
a6b32bc3 1723 spin_lock_irq(&first_peer_device(device)->connection->req_lock);
b30ab791 1724 os = drbd_read_state(device);
78bae59b 1725 ns = os;
b411b363
PR
1726 /* If MDF_CONSISTENT is not set go into inconsistent state,
1727 otherwise investigate MDF_WasUpToDate...
1728 If MDF_WAS_UP_TO_DATE is not set go into D_OUTDATED disk state,
1729 otherwise into D_CONSISTENT state.
1730 */
b30ab791
AG
1731 if (drbd_md_test_flag(device->ldev, MDF_CONSISTENT)) {
1732 if (drbd_md_test_flag(device->ldev, MDF_WAS_UP_TO_DATE))
b411b363
PR
1733 ns.disk = D_CONSISTENT;
1734 else
1735 ns.disk = D_OUTDATED;
1736 } else {
1737 ns.disk = D_INCONSISTENT;
1738 }
1739
b30ab791 1740 if (drbd_md_test_flag(device->ldev, MDF_PEER_OUT_DATED))
b411b363
PR
1741 ns.pdsk = D_OUTDATED;
1742
daeda1cc
PR
1743 rcu_read_lock();
1744 if (ns.disk == D_CONSISTENT &&
b30ab791 1745 (ns.pdsk == D_OUTDATED || rcu_dereference(device->ldev->disk_conf)->fencing == FP_DONT_CARE))
b411b363
PR
1746 ns.disk = D_UP_TO_DATE;
1747
1748 /* All tests on MDF_PRIMARY_IND, MDF_CONNECTED_IND,
1749 MDF_CONSISTENT and MDF_WAS_UP_TO_DATE must happen before
1750 this point, because drbd_request_state() modifies these
1751 flags. */
1752
b30ab791
AG
1753 if (rcu_dereference(device->ldev->disk_conf)->al_updates)
1754 device->ldev->md.flags &= ~MDF_AL_DISABLED;
9a51ab1c 1755 else
b30ab791 1756 device->ldev->md.flags |= MDF_AL_DISABLED;
9a51ab1c
PR
1757
1758 rcu_read_unlock();
1759
b411b363
PR
1760 /* In case we are C_CONNECTED postpone any decision on the new disk
1761 state after the negotiation phase. */
b30ab791
AG
1762 if (device->state.conn == C_CONNECTED) {
1763 device->new_state_tmp.i = ns.i;
b411b363
PR
1764 ns.i = os.i;
1765 ns.disk = D_NEGOTIATING;
dc66c74d
PR
1766
1767 /* We expect to receive up-to-date UUIDs soon.
1768 To avoid a race in receive_state, free p_uuid while
1769 holding req_lock. I.e. atomic with the state change */
b30ab791
AG
1770 kfree(device->p_uuid);
1771 device->p_uuid = NULL;
b411b363
PR
1772 }
1773
b30ab791 1774 rv = _drbd_set_state(device, ns, CS_VERBOSE, NULL);
a6b32bc3 1775 spin_unlock_irq(&first_peer_device(device)->connection->req_lock);
b411b363
PR
1776
1777 if (rv < SS_SUCCESS)
1778 goto force_diskless_dec;
1779
b30ab791 1780 mod_timer(&device->request_timer, jiffies + HZ);
cdfda633 1781
b30ab791
AG
1782 if (device->state.role == R_PRIMARY)
1783 device->ldev->md.uuid[UI_CURRENT] |= (u64)1;
b411b363 1784 else
b30ab791 1785 device->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
b411b363 1786
b30ab791
AG
1787 drbd_md_mark_dirty(device);
1788 drbd_md_sync(device);
b411b363 1789
b30ab791
AG
1790 kobject_uevent(&disk_to_dev(device->vdisk)->kobj, KOBJ_CHANGE);
1791 put_ldev(device);
a6b32bc3 1792 conn_reconfig_done(first_peer_device(device)->connection);
3b98c0c2 1793 drbd_adm_finish(info, retcode);
b411b363
PR
1794 return 0;
1795
1796 force_diskless_dec:
b30ab791 1797 put_ldev(device);
b411b363 1798 force_diskless:
b30ab791
AG
1799 drbd_force_state(device, NS(disk, D_DISKLESS));
1800 drbd_md_sync(device);
b411b363 1801 fail:
a6b32bc3 1802 conn_reconfig_done(first_peer_device(device)->connection);
b411b363 1803 if (nbc) {
e525fd89
TH
1804 if (nbc->backing_bdev)
1805 blkdev_put(nbc->backing_bdev,
1806 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1807 if (nbc->md_bdev)
1808 blkdev_put(nbc->md_bdev,
1809 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
b411b363
PR
1810 kfree(nbc);
1811 }
daeda1cc 1812 kfree(new_disk_conf);
b411b363 1813 lc_destroy(resync_lru);
9958c857 1814 kfree(new_plan);
b411b363 1815
40cbf085 1816 finish:
3b98c0c2 1817 drbd_adm_finish(info, retcode);
b411b363
PR
1818 return 0;
1819}
1820
b30ab791 1821static int adm_detach(struct drbd_device *device, int force)
b411b363 1822{
19f83c76 1823 enum drbd_state_rv retcode;
9a0d9d03 1824 int ret;
02ee8f95 1825
cdfda633 1826 if (force) {
b30ab791
AG
1827 set_bit(FORCE_DETACH, &device->flags);
1828 drbd_force_state(device, NS(disk, D_FAILED));
cdfda633 1829 retcode = SS_SUCCESS;
02ee8f95
PR
1830 goto out;
1831 }
1832
b30ab791
AG
1833 drbd_suspend_io(device); /* so no-one is stuck in drbd_al_begin_io */
1834 drbd_md_get_buffer(device); /* make sure there is no in-flight meta-data IO */
1835 retcode = drbd_request_state(device, NS(disk, D_FAILED));
1836 drbd_md_put_buffer(device);
9a0d9d03 1837 /* D_FAILED will transition to DISKLESS. */
b30ab791
AG
1838 ret = wait_event_interruptible(device->misc_wait,
1839 device->state.disk != D_FAILED);
1840 drbd_resume_io(device);
9b2f61ae 1841 if ((int)retcode == (int)SS_IS_DISKLESS)
9a0d9d03
LE
1842 retcode = SS_NOTHING_TO_DO;
1843 if (ret)
1844 retcode = ERR_INTR;
02ee8f95 1845out:
85f75dd7 1846 return retcode;
b411b363
PR
1847}
1848
82f59cc6
LE
1849/* Detaching the disk is a process in multiple stages. First we need to lock
1850 * out application IO, in-flight IO, IO stuck in drbd_al_begin_io.
1851 * Then we transition to D_DISKLESS, and wait for put_ldev() to return all
1852 * internal references as well.
1853 * Only then we have finally detached. */
3b98c0c2 1854int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info)
b411b363 1855{
116676ca 1856 enum drbd_ret_code retcode;
cdfda633
PR
1857 struct detach_parms parms = { };
1858 int err;
b411b363 1859
3b98c0c2
LE
1860 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1861 if (!adm_ctx.reply_skb)
1862 return retcode;
1863 if (retcode != NO_ERROR)
1864 goto out;
b411b363 1865
cdfda633
PR
1866 if (info->attrs[DRBD_NLA_DETACH_PARMS]) {
1867 err = detach_parms_from_attrs(&parms, info);
1868 if (err) {
1869 retcode = ERR_MANDATORY_TAG;
1870 drbd_msg_put_info(from_attrs_err_to_txt(err));
1871 goto out;
1872 }
b411b363
PR
1873 }
1874
b30ab791 1875 retcode = adm_detach(adm_ctx.device, parms.force_detach);
3b98c0c2
LE
1876out:
1877 drbd_adm_finish(info, retcode);
b411b363
PR
1878 return 0;
1879}
b411b363 1880
bde89a9e 1881static bool conn_resync_running(struct drbd_connection *connection)
f399002e 1882{
c06ece6b 1883 struct drbd_peer_device *peer_device;
695d08fa 1884 bool rv = false;
f399002e
LE
1885 int vnr;
1886
695d08fa 1887 rcu_read_lock();
c06ece6b
AG
1888 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
1889 struct drbd_device *device = peer_device->device;
b30ab791
AG
1890 if (device->state.conn == C_SYNC_SOURCE ||
1891 device->state.conn == C_SYNC_TARGET ||
1892 device->state.conn == C_PAUSED_SYNC_S ||
1893 device->state.conn == C_PAUSED_SYNC_T) {
695d08fa
PR
1894 rv = true;
1895 break;
1896 }
b411b363 1897 }
695d08fa 1898 rcu_read_unlock();
b411b363 1899
695d08fa 1900 return rv;
f399002e 1901}
47ff2d0a 1902
bde89a9e 1903static bool conn_ov_running(struct drbd_connection *connection)
f399002e 1904{
c06ece6b 1905 struct drbd_peer_device *peer_device;
695d08fa 1906 bool rv = false;
f399002e
LE
1907 int vnr;
1908
695d08fa 1909 rcu_read_lock();
c06ece6b
AG
1910 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
1911 struct drbd_device *device = peer_device->device;
b30ab791
AG
1912 if (device->state.conn == C_VERIFY_S ||
1913 device->state.conn == C_VERIFY_T) {
695d08fa
PR
1914 rv = true;
1915 break;
47ff2d0a
PR
1916 }
1917 }
695d08fa 1918 rcu_read_unlock();
b411b363 1919
695d08fa 1920 return rv;
f399002e 1921}
422028b1 1922
cd64397c 1923static enum drbd_ret_code
bde89a9e 1924_check_net_options(struct drbd_connection *connection, struct net_conf *old_conf, struct net_conf *new_conf)
cd64397c 1925{
c06ece6b 1926 struct drbd_peer_device *peer_device;
cd64397c 1927 int i;
b411b363 1928
bde89a9e 1929 if (old_conf && connection->cstate == C_WF_REPORT_PARAMS && connection->agreed_pro_version < 100) {
dcb20d1a
PR
1930 if (new_conf->wire_protocol != old_conf->wire_protocol)
1931 return ERR_NEED_APV_100;
b411b363 1932
dcb20d1a
PR
1933 if (new_conf->two_primaries != old_conf->two_primaries)
1934 return ERR_NEED_APV_100;
1935
dcb20d1a
PR
1936 if (strcmp(new_conf->integrity_alg, old_conf->integrity_alg))
1937 return ERR_NEED_APV_100;
b411b363
PR
1938 }
1939
dcb20d1a 1940 if (!new_conf->two_primaries &&
bde89a9e
AG
1941 conn_highest_role(connection) == R_PRIMARY &&
1942 conn_highest_peer(connection) == R_PRIMARY)
dcb20d1a 1943 return ERR_NEED_ALLOW_TWO_PRI;
b411b363 1944
cd64397c
PR
1945 if (new_conf->two_primaries &&
1946 (new_conf->wire_protocol != DRBD_PROT_C))
1947 return ERR_NOT_PROTO_C;
1948
c06ece6b
AG
1949 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
1950 struct drbd_device *device = peer_device->device;
b30ab791
AG
1951 if (get_ldev(device)) {
1952 enum drbd_fencing_p fp = rcu_dereference(device->ldev->disk_conf)->fencing;
1953 put_ldev(device);
44ed167d 1954 if (new_conf->wire_protocol == DRBD_PROT_A && fp == FP_STONITH)
cd64397c 1955 return ERR_STONITH_AND_PROT_A;
b411b363 1956 }
b30ab791 1957 if (device->state.role == R_PRIMARY && new_conf->discard_my_data)
eb12010e 1958 return ERR_DISCARD_IMPOSSIBLE;
b411b363
PR
1959 }
1960
cd64397c
PR
1961 if (new_conf->on_congestion != OC_BLOCK && new_conf->wire_protocol != DRBD_PROT_A)
1962 return ERR_CONG_NOT_PROTO_A;
b411b363 1963
cd64397c
PR
1964 return NO_ERROR;
1965}
b411b363 1966
44ed167d 1967static enum drbd_ret_code
bde89a9e 1968check_net_options(struct drbd_connection *connection, struct net_conf *new_conf)
44ed167d
PR
1969{
1970 static enum drbd_ret_code rv;
c06ece6b 1971 struct drbd_peer_device *peer_device;
44ed167d 1972 int i;
b411b363 1973
44ed167d 1974 rcu_read_lock();
bde89a9e 1975 rv = _check_net_options(connection, rcu_dereference(connection->net_conf), new_conf);
44ed167d 1976 rcu_read_unlock();
b411b363 1977
bde89a9e 1978 /* connection->volumes protected by genl_lock() here */
c06ece6b
AG
1979 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
1980 struct drbd_device *device = peer_device->device;
b30ab791
AG
1981 if (!device->bitmap) {
1982 if (drbd_bm_init(device))
44ed167d 1983 return ERR_NOMEM;
b411b363
PR
1984 }
1985 }
1986
44ed167d
PR
1987 return rv;
1988}
b411b363 1989
0fd0ea06
PR
1990struct crypto {
1991 struct crypto_hash *verify_tfm;
1992 struct crypto_hash *csums_tfm;
1993 struct crypto_hash *cram_hmac_tfm;
8d412fc6 1994 struct crypto_hash *integrity_tfm;
0fd0ea06 1995};
b411b363 1996
0fd0ea06 1997static int
4b6ad6d4 1998alloc_hash(struct crypto_hash **tfm, char *tfm_name, int err_alg)
0fd0ea06
PR
1999{
2000 if (!tfm_name[0])
2001 return NO_ERROR;
b411b363 2002
0fd0ea06
PR
2003 *tfm = crypto_alloc_hash(tfm_name, 0, CRYPTO_ALG_ASYNC);
2004 if (IS_ERR(*tfm)) {
2005 *tfm = NULL;
2006 return err_alg;
b411b363 2007 }
b411b363 2008
0fd0ea06
PR
2009 return NO_ERROR;
2010}
b411b363 2011
0fd0ea06
PR
2012static enum drbd_ret_code
2013alloc_crypto(struct crypto *crypto, struct net_conf *new_conf)
2014{
2015 char hmac_name[CRYPTO_MAX_ALG_NAME];
2016 enum drbd_ret_code rv;
0fd0ea06 2017
4b6ad6d4
AG
2018 rv = alloc_hash(&crypto->csums_tfm, new_conf->csums_alg,
2019 ERR_CSUMS_ALG);
0fd0ea06
PR
2020 if (rv != NO_ERROR)
2021 return rv;
4b6ad6d4
AG
2022 rv = alloc_hash(&crypto->verify_tfm, new_conf->verify_alg,
2023 ERR_VERIFY_ALG);
0fd0ea06
PR
2024 if (rv != NO_ERROR)
2025 return rv;
4b6ad6d4
AG
2026 rv = alloc_hash(&crypto->integrity_tfm, new_conf->integrity_alg,
2027 ERR_INTEGRITY_ALG);
0fd0ea06
PR
2028 if (rv != NO_ERROR)
2029 return rv;
0fd0ea06
PR
2030 if (new_conf->cram_hmac_alg[0] != 0) {
2031 snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)",
2032 new_conf->cram_hmac_alg);
b411b363 2033
4b6ad6d4
AG
2034 rv = alloc_hash(&crypto->cram_hmac_tfm, hmac_name,
2035 ERR_AUTH_ALG);
b411b363
PR
2036 }
2037
0fd0ea06
PR
2038 return rv;
2039}
b411b363 2040
0fd0ea06
PR
2041static void free_crypto(struct crypto *crypto)
2042{
0fd0ea06 2043 crypto_free_hash(crypto->cram_hmac_tfm);
8d412fc6 2044 crypto_free_hash(crypto->integrity_tfm);
0fd0ea06
PR
2045 crypto_free_hash(crypto->csums_tfm);
2046 crypto_free_hash(crypto->verify_tfm);
2047}
b411b363 2048
f399002e
LE
2049int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info)
2050{
2051 enum drbd_ret_code retcode;
bde89a9e 2052 struct drbd_connection *connection;
44ed167d 2053 struct net_conf *old_conf, *new_conf = NULL;
f399002e
LE
2054 int err;
2055 int ovr; /* online verify running */
2056 int rsr; /* re-sync running */
0fd0ea06 2057 struct crypto crypto = { };
b411b363 2058
089c075d 2059 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONNECTION);
f399002e
LE
2060 if (!adm_ctx.reply_skb)
2061 return retcode;
2062 if (retcode != NO_ERROR)
2063 goto out;
b411b363 2064
bde89a9e 2065 connection = adm_ctx.connection;
b411b363 2066
f399002e
LE
2067 new_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
2068 if (!new_conf) {
2069 retcode = ERR_NOMEM;
2070 goto out;
2071 }
b411b363 2072
bde89a9e 2073 conn_reconfig_start(connection);
b411b363 2074
bde89a9e
AG
2075 mutex_lock(&connection->data.mutex);
2076 mutex_lock(&connection->conf_update);
2077 old_conf = connection->net_conf;
2561b9c1 2078
44ed167d 2079 if (!old_conf) {
f399002e
LE
2080 drbd_msg_put_info("net conf missing, try connect");
2081 retcode = ERR_INVALID_REQUEST;
2561b9c1
PR
2082 goto fail;
2083 }
2084
44ed167d 2085 *new_conf = *old_conf;
5979e361 2086 if (should_set_defaults(info))
b966b5dd 2087 set_net_conf_defaults(new_conf);
f399002e 2088
f399002e 2089 err = net_conf_from_attrs_for_change(new_conf, info);
c75b9b10 2090 if (err && err != -ENOMSG) {
f399002e
LE
2091 retcode = ERR_MANDATORY_TAG;
2092 drbd_msg_put_info(from_attrs_err_to_txt(err));
2093 goto fail;
2561b9c1 2094 }
b411b363 2095
bde89a9e 2096 retcode = check_net_options(connection, new_conf);
cd64397c
PR
2097 if (retcode != NO_ERROR)
2098 goto fail;
b411b363 2099
f399002e 2100 /* re-sync running */
bde89a9e 2101 rsr = conn_resync_running(connection);
0fd0ea06 2102 if (rsr && strcmp(new_conf->csums_alg, old_conf->csums_alg)) {
f399002e 2103 retcode = ERR_CSUMS_RESYNC_RUNNING;
91fd4dad 2104 goto fail;
b411b363
PR
2105 }
2106
f399002e 2107 /* online verify running */
bde89a9e 2108 ovr = conn_ov_running(connection);
0fd0ea06
PR
2109 if (ovr && strcmp(new_conf->verify_alg, old_conf->verify_alg)) {
2110 retcode = ERR_VERIFY_RUNNING;
b411b363 2111 goto fail;
f399002e 2112 }
b411b363 2113
0fd0ea06
PR
2114 retcode = alloc_crypto(&crypto, new_conf);
2115 if (retcode != NO_ERROR)
b411b363 2116 goto fail;
f399002e 2117
bde89a9e 2118 rcu_assign_pointer(connection->net_conf, new_conf);
f399002e
LE
2119
2120 if (!rsr) {
bde89a9e
AG
2121 crypto_free_hash(connection->csums_tfm);
2122 connection->csums_tfm = crypto.csums_tfm;
0fd0ea06 2123 crypto.csums_tfm = NULL;
f399002e
LE
2124 }
2125 if (!ovr) {
bde89a9e
AG
2126 crypto_free_hash(connection->verify_tfm);
2127 connection->verify_tfm = crypto.verify_tfm;
0fd0ea06 2128 crypto.verify_tfm = NULL;
b411b363
PR
2129 }
2130
bde89a9e
AG
2131 crypto_free_hash(connection->integrity_tfm);
2132 connection->integrity_tfm = crypto.integrity_tfm;
2133 if (connection->cstate >= C_WF_REPORT_PARAMS && connection->agreed_pro_version >= 100)
2134 /* Do this without trying to take connection->data.mutex again. */
2135 __drbd_send_protocol(connection, P_PROTOCOL_UPDATE);
0fd0ea06 2136
bde89a9e
AG
2137 crypto_free_hash(connection->cram_hmac_tfm);
2138 connection->cram_hmac_tfm = crypto.cram_hmac_tfm;
0fd0ea06 2139
bde89a9e
AG
2140 mutex_unlock(&connection->conf_update);
2141 mutex_unlock(&connection->data.mutex);
91fd4dad
PR
2142 synchronize_rcu();
2143 kfree(old_conf);
2144
bde89a9e
AG
2145 if (connection->cstate >= C_WF_REPORT_PARAMS)
2146 drbd_send_sync_param(minor_to_device(conn_lowest_minor(connection)));
f399002e 2147
91fd4dad
PR
2148 goto done;
2149
b411b363 2150 fail:
bde89a9e
AG
2151 mutex_unlock(&connection->conf_update);
2152 mutex_unlock(&connection->data.mutex);
0fd0ea06 2153 free_crypto(&crypto);
f399002e 2154 kfree(new_conf);
91fd4dad 2155 done:
bde89a9e 2156 conn_reconfig_done(connection);
f399002e
LE
2157 out:
2158 drbd_adm_finish(info, retcode);
b411b363
PR
2159 return 0;
2160}
2161
3b98c0c2 2162int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info)
b411b363 2163{
c06ece6b 2164 struct drbd_peer_device *peer_device;
44ed167d 2165 struct net_conf *old_conf, *new_conf = NULL;
0fd0ea06 2166 struct crypto crypto = { };
77c556f6 2167 struct drbd_resource *resource;
bde89a9e 2168 struct drbd_connection *connection;
3b98c0c2
LE
2169 enum drbd_ret_code retcode;
2170 int i;
2171 int err;
b411b363 2172
44e52cfa 2173 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
b411b363 2174
3b98c0c2
LE
2175 if (!adm_ctx.reply_skb)
2176 return retcode;
2177 if (retcode != NO_ERROR)
2178 goto out;
089c075d
AG
2179 if (!(adm_ctx.my_addr && adm_ctx.peer_addr)) {
2180 drbd_msg_put_info("connection endpoint(s) missing");
2181 retcode = ERR_INVALID_REQUEST;
2182 goto out;
2183 }
b411b363 2184
089c075d
AG
2185 /* No need for _rcu here. All reconfiguration is
2186 * strictly serialized on genl_lock(). We are protected against
2187 * concurrent reconfiguration/addition/deletion */
77c556f6
AG
2188 for_each_resource(resource, &drbd_resources) {
2189 for_each_connection(connection, resource) {
2190 if (nla_len(adm_ctx.my_addr) == connection->my_addr_len &&
2191 !memcmp(nla_data(adm_ctx.my_addr), &connection->my_addr,
2192 connection->my_addr_len)) {
2193 retcode = ERR_LOCAL_ADDR;
2194 goto out;
2195 }
b411b363 2196
77c556f6
AG
2197 if (nla_len(adm_ctx.peer_addr) == connection->peer_addr_len &&
2198 !memcmp(nla_data(adm_ctx.peer_addr), &connection->peer_addr,
2199 connection->peer_addr_len)) {
2200 retcode = ERR_PEER_ADDR;
2201 goto out;
2202 }
089c075d 2203 }
b411b363
PR
2204 }
2205
bde89a9e
AG
2206 connection = adm_ctx.connection;
2207 conn_reconfig_start(connection);
b411b363 2208
bde89a9e 2209 if (connection->cstate > C_STANDALONE) {
b411b363 2210 retcode = ERR_NET_CONFIGURED;
b411b363
PR
2211 goto fail;
2212 }
2213
a209b4ae 2214 /* allocation not in the IO path, drbdsetup / netlink process context */
5979e361 2215 new_conf = kzalloc(sizeof(*new_conf), GFP_KERNEL);
b411b363
PR
2216 if (!new_conf) {
2217 retcode = ERR_NOMEM;
b411b363
PR
2218 goto fail;
2219 }
2220
b966b5dd 2221 set_net_conf_defaults(new_conf);
b411b363 2222
f399002e 2223 err = net_conf_from_attrs(new_conf, info);
25e40932 2224 if (err && err != -ENOMSG) {
b411b363 2225 retcode = ERR_MANDATORY_TAG;
3b98c0c2 2226 drbd_msg_put_info(from_attrs_err_to_txt(err));
b411b363
PR
2227 goto fail;
2228 }
2229
bde89a9e 2230 retcode = check_net_options(connection, new_conf);
cd64397c 2231 if (retcode != NO_ERROR)
422028b1 2232 goto fail;
b411b363 2233
0fd0ea06
PR
2234 retcode = alloc_crypto(&crypto, new_conf);
2235 if (retcode != NO_ERROR)
2236 goto fail;
b411b363 2237
b411b363 2238 ((char *)new_conf->shared_secret)[SHARED_SECRET_MAX-1] = 0;
7b4e4d31 2239
bde89a9e 2240 conn_flush_workqueue(connection);
b411b363 2241
bde89a9e
AG
2242 mutex_lock(&connection->conf_update);
2243 old_conf = connection->net_conf;
91fd4dad 2244 if (old_conf) {
b411b363 2245 retcode = ERR_NET_CONFIGURED;
bde89a9e 2246 mutex_unlock(&connection->conf_update);
b411b363
PR
2247 goto fail;
2248 }
bde89a9e 2249 rcu_assign_pointer(connection->net_conf, new_conf);
b411b363 2250
bde89a9e
AG
2251 conn_free_crypto(connection);
2252 connection->cram_hmac_tfm = crypto.cram_hmac_tfm;
2253 connection->integrity_tfm = crypto.integrity_tfm;
2254 connection->csums_tfm = crypto.csums_tfm;
2255 connection->verify_tfm = crypto.verify_tfm;
b411b363 2256
bde89a9e
AG
2257 connection->my_addr_len = nla_len(adm_ctx.my_addr);
2258 memcpy(&connection->my_addr, nla_data(adm_ctx.my_addr), connection->my_addr_len);
2259 connection->peer_addr_len = nla_len(adm_ctx.peer_addr);
2260 memcpy(&connection->peer_addr, nla_data(adm_ctx.peer_addr), connection->peer_addr_len);
b411b363 2261
bde89a9e 2262 mutex_unlock(&connection->conf_update);
b411b363 2263
695d08fa 2264 rcu_read_lock();
c06ece6b
AG
2265 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
2266 struct drbd_device *device = peer_device->device;
b30ab791
AG
2267 device->send_cnt = 0;
2268 device->recv_cnt = 0;
b411b363 2269 }
695d08fa 2270 rcu_read_unlock();
b411b363 2271
bde89a9e 2272 retcode = conn_request_state(connection, NS(conn, C_UNCONNECTED), CS_VERBOSE);
b411b363 2273
bde89a9e 2274 conn_reconfig_done(connection);
3b98c0c2 2275 drbd_adm_finish(info, retcode);
b411b363 2276 return 0;
b411b363 2277
b411b363 2278fail:
0fd0ea06 2279 free_crypto(&crypto);
b411b363 2280 kfree(new_conf);
b411b363 2281
bde89a9e 2282 conn_reconfig_done(connection);
3b98c0c2
LE
2283out:
2284 drbd_adm_finish(info, retcode);
b411b363
PR
2285 return 0;
2286}
2287
bde89a9e 2288static enum drbd_state_rv conn_try_disconnect(struct drbd_connection *connection, bool force)
85f75dd7
LE
2289{
2290 enum drbd_state_rv rv;
85f75dd7 2291
bde89a9e 2292 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING),
f3dfa40a 2293 force ? CS_HARD : 0);
85f75dd7
LE
2294
2295 switch (rv) {
2296 case SS_NOTHING_TO_DO:
f3dfa40a 2297 break;
85f75dd7
LE
2298 case SS_ALREADY_STANDALONE:
2299 return SS_SUCCESS;
2300 case SS_PRIMARY_NOP:
2301 /* Our state checking code wants to see the peer outdated. */
bde89a9e 2302 rv = conn_request_state(connection, NS2(conn, C_DISCONNECTING, pdsk, D_OUTDATED), 0);
2bd5ed5d
PR
2303
2304 if (rv == SS_OUTDATE_WO_CONN) /* lost connection before graceful disconnect succeeded */
bde89a9e 2305 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING), CS_VERBOSE);
2bd5ed5d 2306
85f75dd7
LE
2307 break;
2308 case SS_CW_FAILED_BY_PEER:
2309 /* The peer probably wants to see us outdated. */
bde89a9e 2310 rv = conn_request_state(connection, NS2(conn, C_DISCONNECTING,
85f75dd7
LE
2311 disk, D_OUTDATED), 0);
2312 if (rv == SS_IS_DISKLESS || rv == SS_LOWER_THAN_OUTDATED) {
bde89a9e 2313 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING),
f3dfa40a 2314 CS_HARD);
b411b363 2315 }
85f75dd7
LE
2316 break;
2317 default:;
2318 /* no special handling necessary */
2319 }
2320
f3dfa40a
LE
2321 if (rv >= SS_SUCCESS) {
2322 enum drbd_state_rv rv2;
2323 /* No one else can reconfigure the network while I am here.
2324 * The state handling only uses drbd_thread_stop_nowait(),
2325 * we want to really wait here until the receiver is no more.
2326 */
9693da23 2327 drbd_thread_stop(&connection->receiver);
f3dfa40a
LE
2328
2329 /* Race breaker. This additional state change request may be
2330 * necessary, if this was a forced disconnect during a receiver
2331 * restart. We may have "killed" the receiver thread just
2332 * after drbdd_init() returned. Typically, we should be
2333 * C_STANDALONE already, now, and this becomes a no-op.
2334 */
bde89a9e 2335 rv2 = conn_request_state(connection, NS(conn, C_STANDALONE),
f3dfa40a
LE
2336 CS_VERBOSE | CS_HARD);
2337 if (rv2 < SS_SUCCESS)
1ec861eb 2338 drbd_err(connection,
f3dfa40a
LE
2339 "unexpected rv2=%d in conn_try_disconnect()\n",
2340 rv2);
b411b363 2341 }
85f75dd7
LE
2342 return rv;
2343}
b411b363 2344
3b98c0c2 2345int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info)
b411b363 2346{
3b98c0c2 2347 struct disconnect_parms parms;
bde89a9e 2348 struct drbd_connection *connection;
85f75dd7 2349 enum drbd_state_rv rv;
3b98c0c2
LE
2350 enum drbd_ret_code retcode;
2351 int err;
2561b9c1 2352
089c075d 2353 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONNECTION);
3b98c0c2
LE
2354 if (!adm_ctx.reply_skb)
2355 return retcode;
2356 if (retcode != NO_ERROR)
2561b9c1 2357 goto fail;
b411b363 2358
bde89a9e 2359 connection = adm_ctx.connection;
3b98c0c2
LE
2360 memset(&parms, 0, sizeof(parms));
2361 if (info->attrs[DRBD_NLA_DISCONNECT_PARMS]) {
f399002e 2362 err = disconnect_parms_from_attrs(&parms, info);
3b98c0c2
LE
2363 if (err) {
2364 retcode = ERR_MANDATORY_TAG;
2365 drbd_msg_put_info(from_attrs_err_to_txt(err));
b411b363
PR
2366 goto fail;
2367 }
2368 }
2369
bde89a9e 2370 rv = conn_try_disconnect(connection, parms.force_disconnect);
85f75dd7 2371 if (rv < SS_SUCCESS)
f3dfa40a
LE
2372 retcode = rv; /* FIXME: Type mismatch. */
2373 else
2374 retcode = NO_ERROR;
b411b363 2375 fail:
3b98c0c2 2376 drbd_adm_finish(info, retcode);
b411b363
PR
2377 return 0;
2378}
2379
b30ab791 2380void resync_after_online_grow(struct drbd_device *device)
b411b363
PR
2381{
2382 int iass; /* I am sync source */
2383
d0180171 2384 drbd_info(device, "Resync of new storage after online grow\n");
b30ab791
AG
2385 if (device->state.role != device->state.peer)
2386 iass = (device->state.role == R_PRIMARY);
b411b363 2387 else
a6b32bc3 2388 iass = test_bit(RESOLVE_CONFLICTS, &first_peer_device(device)->connection->flags);
b411b363
PR
2389
2390 if (iass)
b30ab791 2391 drbd_start_resync(device, C_SYNC_SOURCE);
b411b363 2392 else
b30ab791 2393 _drbd_request_state(device, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE + CS_SERIALIZE);
b411b363
PR
2394}
2395
3b98c0c2 2396int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info)
b411b363 2397{
daeda1cc 2398 struct disk_conf *old_disk_conf, *new_disk_conf = NULL;
3b98c0c2 2399 struct resize_parms rs;
b30ab791 2400 struct drbd_device *device;
3b98c0c2 2401 enum drbd_ret_code retcode;
b411b363 2402 enum determine_dev_size dd;
d752b269 2403 bool change_al_layout = false;
6495d2c6 2404 enum dds_flags ddsf;
daeda1cc 2405 sector_t u_size;
3b98c0c2 2406 int err;
b411b363 2407
3b98c0c2
LE
2408 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2409 if (!adm_ctx.reply_skb)
2410 return retcode;
2411 if (retcode != NO_ERROR)
b411b363 2412 goto fail;
3b98c0c2 2413
b30ab791
AG
2414 device = adm_ctx.device;
2415 if (!get_ldev(device)) {
d752b269
PR
2416 retcode = ERR_NO_DISK;
2417 goto fail;
2418 }
2419
3b98c0c2 2420 memset(&rs, 0, sizeof(struct resize_parms));
b30ab791
AG
2421 rs.al_stripes = device->ldev->md.al_stripes;
2422 rs.al_stripe_size = device->ldev->md.al_stripe_size_4k * 4;
3b98c0c2 2423 if (info->attrs[DRBD_NLA_RESIZE_PARMS]) {
f399002e 2424 err = resize_parms_from_attrs(&rs, info);
b411b363 2425 if (err) {
3b98c0c2
LE
2426 retcode = ERR_MANDATORY_TAG;
2427 drbd_msg_put_info(from_attrs_err_to_txt(err));
d752b269 2428 goto fail_ldev;
b411b363
PR
2429 }
2430 }
2431
b30ab791 2432 if (device->state.conn > C_CONNECTED) {
b411b363 2433 retcode = ERR_RESIZE_RESYNC;
d752b269 2434 goto fail_ldev;
b411b363 2435 }
b411b363 2436
b30ab791
AG
2437 if (device->state.role == R_SECONDARY &&
2438 device->state.peer == R_SECONDARY) {
b411b363 2439 retcode = ERR_NO_PRIMARY;
d752b269 2440 goto fail_ldev;
b411b363 2441 }
b411b363 2442
a6b32bc3 2443 if (rs.no_resync && first_peer_device(device)->connection->agreed_pro_version < 93) {
6495d2c6 2444 retcode = ERR_NEED_APV_93;
9bcd2521 2445 goto fail_ldev;
6495d2c6
PR
2446 }
2447
daeda1cc 2448 rcu_read_lock();
b30ab791 2449 u_size = rcu_dereference(device->ldev->disk_conf)->disk_size;
daeda1cc
PR
2450 rcu_read_unlock();
2451 if (u_size != (sector_t)rs.resize_size) {
2452 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
2453 if (!new_disk_conf) {
778f271d 2454 retcode = ERR_NOMEM;
9bcd2521 2455 goto fail_ldev;
778f271d
PR
2456 }
2457 }
2458
b30ab791
AG
2459 if (device->ldev->md.al_stripes != rs.al_stripes ||
2460 device->ldev->md.al_stripe_size_4k != rs.al_stripe_size / 4) {
d752b269
PR
2461 u32 al_size_k = rs.al_stripes * rs.al_stripe_size;
2462
2463 if (al_size_k > (16 * 1024 * 1024)) {
2464 retcode = ERR_MD_LAYOUT_TOO_BIG;
2465 goto fail_ldev;
2466 }
2467
2468 if (al_size_k < MD_32kB_SECT/2) {
2469 retcode = ERR_MD_LAYOUT_TOO_SMALL;
2470 goto fail_ldev;
2471 }
2472
b30ab791 2473 if (device->state.conn != C_CONNECTED) {
d752b269
PR
2474 retcode = ERR_MD_LAYOUT_CONNECTED;
2475 goto fail_ldev;
2476 }
2477
2478 change_al_layout = true;
2479 }
2480
b30ab791
AG
2481 if (device->ldev->known_size != drbd_get_capacity(device->ldev->backing_bdev))
2482 device->ldev->known_size = drbd_get_capacity(device->ldev->backing_bdev);
b411b363 2483
daeda1cc 2484 if (new_disk_conf) {
a6b32bc3 2485 mutex_lock(&first_peer_device(device)->connection->conf_update);
b30ab791 2486 old_disk_conf = device->ldev->disk_conf;
daeda1cc
PR
2487 *new_disk_conf = *old_disk_conf;
2488 new_disk_conf->disk_size = (sector_t)rs.resize_size;
b30ab791 2489 rcu_assign_pointer(device->ldev->disk_conf, new_disk_conf);
a6b32bc3 2490 mutex_unlock(&first_peer_device(device)->connection->conf_update);
daeda1cc
PR
2491 synchronize_rcu();
2492 kfree(old_disk_conf);
b411b363
PR
2493 }
2494
6495d2c6 2495 ddsf = (rs.resize_force ? DDSF_FORCED : 0) | (rs.no_resync ? DDSF_NO_RESYNC : 0);
b30ab791
AG
2496 dd = drbd_determine_dev_size(device, ddsf, change_al_layout ? &rs : NULL);
2497 drbd_md_sync(device);
2498 put_ldev(device);
e96c9633 2499 if (dd == DS_ERROR) {
b411b363
PR
2500 retcode = ERR_NOMEM_BITMAP;
2501 goto fail;
d752b269
PR
2502 } else if (dd == DS_ERROR_SPACE_MD) {
2503 retcode = ERR_MD_LAYOUT_NO_FIT;
2504 goto fail;
2505 } else if (dd == DS_ERROR_SHRINK) {
2506 retcode = ERR_IMPLICIT_SHRINK;
2507 goto fail;
b411b363 2508 }
778f271d 2509
b30ab791 2510 if (device->state.conn == C_CONNECTED) {
e96c9633 2511 if (dd == DS_GREW)
b30ab791 2512 set_bit(RESIZE_PENDING, &device->flags);
b411b363 2513
b30ab791
AG
2514 drbd_send_uuids(device);
2515 drbd_send_sizes(device, 1, ddsf);
778f271d
PR
2516 }
2517
b411b363 2518 fail:
3b98c0c2 2519 drbd_adm_finish(info, retcode);
b411b363 2520 return 0;
b411b363 2521
9bcd2521 2522 fail_ldev:
b30ab791 2523 put_ldev(device);
9bcd2521 2524 goto fail;
b411b363 2525}
b411b363 2526
f399002e 2527int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info)
b411b363 2528{
3b98c0c2 2529 enum drbd_ret_code retcode;
b57a1e27 2530 struct res_opts res_opts;
f399002e 2531 int err;
b411b363 2532
44e52cfa 2533 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
3b98c0c2
LE
2534 if (!adm_ctx.reply_skb)
2535 return retcode;
2536 if (retcode != NO_ERROR)
2537 goto fail;
b411b363 2538
eb6bea67 2539 res_opts = adm_ctx.resource->res_opts;
5979e361 2540 if (should_set_defaults(info))
b966b5dd 2541 set_res_opts_defaults(&res_opts);
b411b363 2542
b57a1e27 2543 err = res_opts_from_attrs(&res_opts, info);
c75b9b10 2544 if (err && err != -ENOMSG) {
b411b363 2545 retcode = ERR_MANDATORY_TAG;
3b98c0c2 2546 drbd_msg_put_info(from_attrs_err_to_txt(err));
b411b363
PR
2547 goto fail;
2548 }
2549
eb6bea67 2550 err = set_resource_options(adm_ctx.resource, &res_opts);
afbbfa88
AG
2551 if (err) {
2552 retcode = ERR_INVALID_REQUEST;
2553 if (err == -ENOMEM)
2554 retcode = ERR_NOMEM;
b411b363
PR
2555 }
2556
b411b363 2557fail:
3b98c0c2 2558 drbd_adm_finish(info, retcode);
b411b363
PR
2559 return 0;
2560}
2561
3b98c0c2 2562int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info)
b411b363 2563{
b30ab791 2564 struct drbd_device *device;
3b98c0c2
LE
2565 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2566
2567 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2568 if (!adm_ctx.reply_skb)
2569 return retcode;
2570 if (retcode != NO_ERROR)
2571 goto out;
2572
b30ab791 2573 device = adm_ctx.device;
b411b363 2574
194bfb32 2575 /* If there is still bitmap IO pending, probably because of a previous
7ee1fb93
LE
2576 * resync just being finished, wait for it before requesting a new resync.
2577 * Also wait for it's after_state_ch(). */
b30ab791
AG
2578 drbd_suspend_io(device);
2579 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
2580 drbd_flush_workqueue(device);
194bfb32 2581
0b2dafcd
PR
2582 /* If we happen to be C_STANDALONE R_SECONDARY, just change to
2583 * D_INCONSISTENT, and set all bits in the bitmap. Otherwise,
2584 * try to start a resync handshake as sync target for full sync.
9376d9f8 2585 */
b30ab791
AG
2586 if (device->state.conn == C_STANDALONE && device->state.role == R_SECONDARY) {
2587 retcode = drbd_request_state(device, NS(disk, D_INCONSISTENT));
0b2dafcd 2588 if (retcode >= SS_SUCCESS) {
b30ab791 2589 if (drbd_bitmap_io(device, &drbd_bmio_set_n_write,
0b2dafcd
PR
2590 "set_n_write from invalidate", BM_LOCKED_MASK))
2591 retcode = ERR_IO_MD_DISK;
2592 }
2593 } else
b30ab791
AG
2594 retcode = drbd_request_state(device, NS(conn, C_STARTING_SYNC_T));
2595 drbd_resume_io(device);
b411b363 2596
3b98c0c2
LE
2597out:
2598 drbd_adm_finish(info, retcode);
b411b363
PR
2599 return 0;
2600}
2601
3b98c0c2
LE
2602static int drbd_adm_simple_request_state(struct sk_buff *skb, struct genl_info *info,
2603 union drbd_state mask, union drbd_state val)
b411b363 2604{
3b98c0c2 2605 enum drbd_ret_code retcode;
194bfb32 2606
3b98c0c2
LE
2607 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2608 if (!adm_ctx.reply_skb)
2609 return retcode;
2610 if (retcode != NO_ERROR)
2611 goto out;
b411b363 2612
b30ab791 2613 retcode = drbd_request_state(adm_ctx.device, mask, val);
3b98c0c2
LE
2614out:
2615 drbd_adm_finish(info, retcode);
b411b363
PR
2616 return 0;
2617}
2618
b30ab791 2619static int drbd_bmio_set_susp_al(struct drbd_device *device)
0778286a
PR
2620{
2621 int rv;
2622
b30ab791
AG
2623 rv = drbd_bmio_set_n_write(device);
2624 drbd_suspend_al(device);
0778286a
PR
2625 return rv;
2626}
2627
3b98c0c2 2628int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info)
b411b363 2629{
25b0d6c8 2630 int retcode; /* drbd_ret_code, drbd_state_rv */
b30ab791 2631 struct drbd_device *device;
25b0d6c8
PR
2632
2633 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2634 if (!adm_ctx.reply_skb)
2635 return retcode;
2636 if (retcode != NO_ERROR)
2637 goto out;
2638
b30ab791 2639 device = adm_ctx.device;
b411b363 2640
194bfb32 2641 /* If there is still bitmap IO pending, probably because of a previous
7ee1fb93
LE
2642 * resync just being finished, wait for it before requesting a new resync.
2643 * Also wait for it's after_state_ch(). */
b30ab791
AG
2644 drbd_suspend_io(device);
2645 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
2646 drbd_flush_workqueue(device);
194bfb32 2647
0b2dafcd
PR
2648 /* If we happen to be C_STANDALONE R_PRIMARY, just set all bits
2649 * in the bitmap. Otherwise, try to start a resync handshake
2650 * as sync source for full sync.
2651 */
b30ab791 2652 if (device->state.conn == C_STANDALONE && device->state.role == R_PRIMARY) {
0b2dafcd
PR
2653 /* The peer will get a resync upon connect anyways. Just make that
2654 into a full resync. */
b30ab791 2655 retcode = drbd_request_state(device, NS(pdsk, D_INCONSISTENT));
0b2dafcd 2656 if (retcode >= SS_SUCCESS) {
b30ab791 2657 if (drbd_bitmap_io(device, &drbd_bmio_set_susp_al,
0b2dafcd
PR
2658 "set_n_write from invalidate_peer",
2659 BM_LOCKED_SET_ALLOWED))
2660 retcode = ERR_IO_MD_DISK;
2661 }
2662 } else
b30ab791
AG
2663 retcode = drbd_request_state(device, NS(conn, C_STARTING_SYNC_S));
2664 drbd_resume_io(device);
b411b363 2665
25b0d6c8
PR
2666out:
2667 drbd_adm_finish(info, retcode);
b411b363
PR
2668 return 0;
2669}
2670
3b98c0c2 2671int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info)
b411b363 2672{
3b98c0c2 2673 enum drbd_ret_code retcode;
b411b363 2674
3b98c0c2
LE
2675 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2676 if (!adm_ctx.reply_skb)
2677 return retcode;
2678 if (retcode != NO_ERROR)
2679 goto out;
b411b363 2680
b30ab791 2681 if (drbd_request_state(adm_ctx.device, NS(user_isp, 1)) == SS_NOTHING_TO_DO)
3b98c0c2
LE
2682 retcode = ERR_PAUSE_IS_SET;
2683out:
2684 drbd_adm_finish(info, retcode);
b411b363
PR
2685 return 0;
2686}
2687
3b98c0c2 2688int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info)
b411b363 2689{
da9fbc27 2690 union drbd_dev_state s;
3b98c0c2
LE
2691 enum drbd_ret_code retcode;
2692
2693 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2694 if (!adm_ctx.reply_skb)
2695 return retcode;
2696 if (retcode != NO_ERROR)
2697 goto out;
b411b363 2698
b30ab791
AG
2699 if (drbd_request_state(adm_ctx.device, NS(user_isp, 0)) == SS_NOTHING_TO_DO) {
2700 s = adm_ctx.device->state;
cd88d030
PR
2701 if (s.conn == C_PAUSED_SYNC_S || s.conn == C_PAUSED_SYNC_T) {
2702 retcode = s.aftr_isp ? ERR_PIC_AFTER_DEP :
2703 s.peer_isp ? ERR_PIC_PEER_DEP : ERR_PAUSE_IS_CLEAR;
2704 } else {
2705 retcode = ERR_PAUSE_IS_CLEAR;
2706 }
2707 }
b411b363 2708
3b98c0c2
LE
2709out:
2710 drbd_adm_finish(info, retcode);
b411b363
PR
2711 return 0;
2712}
2713
3b98c0c2 2714int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info)
b411b363 2715{
3b98c0c2 2716 return drbd_adm_simple_request_state(skb, info, NS(susp, 1));
b411b363
PR
2717}
2718
3b98c0c2 2719int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info)
b411b363 2720{
b30ab791 2721 struct drbd_device *device;
3b98c0c2
LE
2722 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2723
2724 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2725 if (!adm_ctx.reply_skb)
2726 return retcode;
2727 if (retcode != NO_ERROR)
2728 goto out;
2729
b30ab791
AG
2730 device = adm_ctx.device;
2731 if (test_bit(NEW_CUR_UUID, &device->flags)) {
2732 drbd_uuid_new_current(device);
2733 clear_bit(NEW_CUR_UUID, &device->flags);
43a5182c 2734 }
b30ab791
AG
2735 drbd_suspend_io(device);
2736 retcode = drbd_request_state(device, NS3(susp, 0, susp_nod, 0, susp_fen, 0));
3b98c0c2 2737 if (retcode == SS_SUCCESS) {
b30ab791 2738 if (device->state.conn < C_CONNECTED)
a6b32bc3 2739 tl_clear(first_peer_device(device)->connection);
b30ab791 2740 if (device->state.disk == D_DISKLESS || device->state.disk == D_FAILED)
a6b32bc3 2741 tl_restart(first_peer_device(device)->connection, FAIL_FROZEN_DISK_IO);
265be2d0 2742 }
b30ab791 2743 drbd_resume_io(device);
265be2d0 2744
3b98c0c2
LE
2745out:
2746 drbd_adm_finish(info, retcode);
b411b363
PR
2747 return 0;
2748}
2749
3b98c0c2 2750int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info)
b411b363 2751{
3b98c0c2 2752 return drbd_adm_simple_request_state(skb, info, NS(disk, D_OUTDATED));
b411b363
PR
2753}
2754
bde89a9e 2755static int nla_put_drbd_cfg_context(struct sk_buff *skb, struct drbd_connection *connection, unsigned vnr)
b411b363 2756{
543cc10b
LE
2757 struct nlattr *nla;
2758 nla = nla_nest_start(skb, DRBD_NLA_CFG_CONTEXT);
2759 if (!nla)
2760 goto nla_put_failure;
26ec9287
AG
2761 if (vnr != VOLUME_UNSPECIFIED &&
2762 nla_put_u32(skb, T_ctx_volume, vnr))
2763 goto nla_put_failure;
77c556f6 2764 if (nla_put_string(skb, T_ctx_resource_name, connection->resource->name))
26ec9287 2765 goto nla_put_failure;
bde89a9e
AG
2766 if (connection->my_addr_len &&
2767 nla_put(skb, T_ctx_my_addr, connection->my_addr_len, &connection->my_addr))
26ec9287 2768 goto nla_put_failure;
bde89a9e
AG
2769 if (connection->peer_addr_len &&
2770 nla_put(skb, T_ctx_peer_addr, connection->peer_addr_len, &connection->peer_addr))
26ec9287 2771 goto nla_put_failure;
543cc10b
LE
2772 nla_nest_end(skb, nla);
2773 return 0;
b411b363 2774
543cc10b
LE
2775nla_put_failure:
2776 if (nla)
2777 nla_nest_cancel(skb, nla);
2778 return -EMSGSIZE;
2779}
b411b363 2780
b30ab791 2781static int nla_put_status_info(struct sk_buff *skb, struct drbd_device *device,
3b98c0c2 2782 const struct sib_info *sib)
b411b363 2783{
3b98c0c2
LE
2784 struct state_info *si = NULL; /* for sizeof(si->member); */
2785 struct nlattr *nla;
2786 int got_ldev;
3b98c0c2
LE
2787 int err = 0;
2788 int exclude_sensitive;
2789
2790 /* If sib != NULL, this is drbd_bcast_event, which anyone can listen
2791 * to. So we better exclude_sensitive information.
2792 *
2793 * If sib == NULL, this is drbd_adm_get_status, executed synchronously
2794 * in the context of the requesting user process. Exclude sensitive
2795 * information, unless current has superuser.
2796 *
2797 * NOTE: for drbd_adm_get_status_all(), this is a netlink dump, and
2798 * relies on the current implementation of netlink_dump(), which
2799 * executes the dump callback successively from netlink_recvmsg(),
2800 * always in the context of the receiving process */
2801 exclude_sensitive = sib || !capable(CAP_SYS_ADMIN);
2802
b30ab791 2803 got_ldev = get_ldev(device);
3b98c0c2
LE
2804
2805 /* We need to add connection name and volume number information still.
2806 * Minor number is in drbd_genlmsghdr. */
a6b32bc3 2807 if (nla_put_drbd_cfg_context(skb, first_peer_device(device)->connection, device->vnr))
3b98c0c2 2808 goto nla_put_failure;
3b98c0c2 2809
eb6bea67 2810 if (res_opts_to_skb(skb, &device->resource->res_opts, exclude_sensitive))
f399002e
LE
2811 goto nla_put_failure;
2812
daeda1cc 2813 rcu_read_lock();
f9eb7bf4
AG
2814 if (got_ldev) {
2815 struct disk_conf *disk_conf;
44ed167d 2816
b30ab791 2817 disk_conf = rcu_dereference(device->ldev->disk_conf);
f9eb7bf4
AG
2818 err = disk_conf_to_skb(skb, disk_conf, exclude_sensitive);
2819 }
2820 if (!err) {
2821 struct net_conf *nc;
2822
a6b32bc3 2823 nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
f9eb7bf4
AG
2824 if (nc)
2825 err = net_conf_to_skb(skb, nc, exclude_sensitive);
2826 }
44ed167d
PR
2827 rcu_read_unlock();
2828 if (err)
2829 goto nla_put_failure;
3b98c0c2 2830
3b98c0c2
LE
2831 nla = nla_nest_start(skb, DRBD_NLA_STATE_INFO);
2832 if (!nla)
2833 goto nla_put_failure;
26ec9287 2834 if (nla_put_u32(skb, T_sib_reason, sib ? sib->sib_reason : SIB_GET_STATUS_REPLY) ||
b30ab791
AG
2835 nla_put_u32(skb, T_current_state, device->state.i) ||
2836 nla_put_u64(skb, T_ed_uuid, device->ed_uuid) ||
2837 nla_put_u64(skb, T_capacity, drbd_get_capacity(device->this_bdev)) ||
2838 nla_put_u64(skb, T_send_cnt, device->send_cnt) ||
2839 nla_put_u64(skb, T_recv_cnt, device->recv_cnt) ||
2840 nla_put_u64(skb, T_read_cnt, device->read_cnt) ||
2841 nla_put_u64(skb, T_writ_cnt, device->writ_cnt) ||
2842 nla_put_u64(skb, T_al_writ_cnt, device->al_writ_cnt) ||
2843 nla_put_u64(skb, T_bm_writ_cnt, device->bm_writ_cnt) ||
2844 nla_put_u32(skb, T_ap_bio_cnt, atomic_read(&device->ap_bio_cnt)) ||
2845 nla_put_u32(skb, T_ap_pending_cnt, atomic_read(&device->ap_pending_cnt)) ||
2846 nla_put_u32(skb, T_rs_pending_cnt, atomic_read(&device->rs_pending_cnt)))
26ec9287 2847 goto nla_put_failure;
3b98c0c2
LE
2848
2849 if (got_ldev) {
39a1aa7f 2850 int err;
b411b363 2851
b30ab791
AG
2852 spin_lock_irq(&device->ldev->md.uuid_lock);
2853 err = nla_put(skb, T_uuids, sizeof(si->uuids), device->ldev->md.uuid);
2854 spin_unlock_irq(&device->ldev->md.uuid_lock);
39a1aa7f
PR
2855
2856 if (err)
2857 goto nla_put_failure;
2858
b30ab791
AG
2859 if (nla_put_u32(skb, T_disk_flags, device->ldev->md.flags) ||
2860 nla_put_u64(skb, T_bits_total, drbd_bm_bits(device)) ||
2861 nla_put_u64(skb, T_bits_oos, drbd_bm_total_weight(device)))
26ec9287 2862 goto nla_put_failure;
b30ab791
AG
2863 if (C_SYNC_SOURCE <= device->state.conn &&
2864 C_PAUSED_SYNC_T >= device->state.conn) {
2865 if (nla_put_u64(skb, T_bits_rs_total, device->rs_total) ||
2866 nla_put_u64(skb, T_bits_rs_failed, device->rs_failed))
26ec9287 2867 goto nla_put_failure;
3b98c0c2 2868 }
b411b363 2869 }
b411b363 2870
3b98c0c2
LE
2871 if (sib) {
2872 switch(sib->sib_reason) {
2873 case SIB_SYNC_PROGRESS:
2874 case SIB_GET_STATUS_REPLY:
2875 break;
2876 case SIB_STATE_CHANGE:
26ec9287
AG
2877 if (nla_put_u32(skb, T_prev_state, sib->os.i) ||
2878 nla_put_u32(skb, T_new_state, sib->ns.i))
2879 goto nla_put_failure;
3b98c0c2
LE
2880 break;
2881 case SIB_HELPER_POST:
26ec9287
AG
2882 if (nla_put_u32(skb, T_helper_exit_code,
2883 sib->helper_exit_code))
2884 goto nla_put_failure;
3b98c0c2
LE
2885 /* fall through */
2886 case SIB_HELPER_PRE:
26ec9287
AG
2887 if (nla_put_string(skb, T_helper, sib->helper_name))
2888 goto nla_put_failure;
3b98c0c2
LE
2889 break;
2890 }
b411b363 2891 }
3b98c0c2 2892 nla_nest_end(skb, nla);
b411b363 2893
3b98c0c2
LE
2894 if (0)
2895nla_put_failure:
2896 err = -EMSGSIZE;
2897 if (got_ldev)
b30ab791 2898 put_ldev(device);
3b98c0c2 2899 return err;
b411b363
PR
2900}
2901
3b98c0c2 2902int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info)
b411b363 2903{
3b98c0c2
LE
2904 enum drbd_ret_code retcode;
2905 int err;
b411b363 2906
3b98c0c2
LE
2907 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2908 if (!adm_ctx.reply_skb)
2909 return retcode;
2910 if (retcode != NO_ERROR)
2911 goto out;
b411b363 2912
b30ab791 2913 err = nla_put_status_info(adm_ctx.reply_skb, adm_ctx.device, NULL);
3b98c0c2
LE
2914 if (err) {
2915 nlmsg_free(adm_ctx.reply_skb);
2916 return err;
b411b363 2917 }
3b98c0c2
LE
2918out:
2919 drbd_adm_finish(info, retcode);
2920 return 0;
b411b363
PR
2921}
2922
4b7a530f 2923static int get_one_status(struct sk_buff *skb, struct netlink_callback *cb)
b411b363 2924{
c06ece6b 2925 struct drbd_peer_device *peer_device;
b30ab791 2926 struct drbd_device *device;
3b98c0c2 2927 struct drbd_genlmsghdr *dh;
77c556f6
AG
2928 struct drbd_resource *pos = (struct drbd_resource *)cb->args[0];
2929 struct drbd_resource *resource = NULL;
2930 struct drbd_connection *connection;
2931 struct drbd_resource *tmp;
543cc10b
LE
2932 unsigned volume = cb->args[1];
2933
2934 /* Open coded, deferred, iteration:
77c556f6
AG
2935 * for_each_resource_safe(resource, tmp, &drbd_resources) {
2936 * connection = "first connection of resource";
c06ece6b 2937 * idr_for_each_entry(&connection->peer_devices, peer_device, i) {
543cc10b
LE
2938 * ...
2939 * }
2940 * }
77c556f6 2941 * where resource is cb->args[0];
543cc10b
LE
2942 * and i is cb->args[1];
2943 *
71932efc
LE
2944 * cb->args[2] indicates if we shall loop over all resources,
2945 * or just dump all volumes of a single resource.
2946 *
3b98c0c2
LE
2947 * This may miss entries inserted after this dump started,
2948 * or entries deleted before they are reached.
543cc10b 2949 *
b30ab791 2950 * We need to make sure the device won't disappear while
543cc10b
LE
2951 * we are looking at it, and revalidate our iterators
2952 * on each iteration.
2953 */
b411b363 2954
05a10ec7 2955 /* synchronize with conn_create()/drbd_destroy_connection() */
c141ebda 2956 rcu_read_lock();
543cc10b 2957 /* revalidate iterator position */
77c556f6 2958 for_each_resource_rcu(tmp, &drbd_resources) {
543cc10b
LE
2959 if (pos == NULL) {
2960 /* first iteration */
2961 pos = tmp;
77c556f6 2962 resource = pos;
543cc10b
LE
2963 break;
2964 }
2965 if (tmp == pos) {
77c556f6 2966 resource = pos;
543cc10b
LE
2967 break;
2968 }
b411b363 2969 }
77c556f6
AG
2970 if (resource) {
2971next_resource:
2972 connection = first_connection(resource);
c06ece6b
AG
2973 peer_device = idr_get_next(&connection->peer_devices, &volume);
2974 if (!peer_device) {
77c556f6
AG
2975 /* No more volumes to dump on this resource.
2976 * Advance resource iterator. */
2977 pos = list_entry_rcu(resource->resources.next,
2978 struct drbd_resource, resources);
2979 /* Did we dump any volume of this resource yet? */
543cc10b 2980 if (volume != 0) {
71932efc
LE
2981 /* If we reached the end of the list,
2982 * or only a single resource dump was requested,
2983 * we are done. */
77c556f6 2984 if (&pos->resources == &drbd_resources || cb->args[2])
71932efc 2985 goto out;
543cc10b 2986 volume = 0;
77c556f6
AG
2987 resource = pos;
2988 goto next_resource;
543cc10b
LE
2989 }
2990 }
2991
98683650 2992 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
3b98c0c2
LE
2993 cb->nlh->nlmsg_seq, &drbd_genl_family,
2994 NLM_F_MULTI, DRBD_ADM_GET_STATUS);
2995 if (!dh)
543cc10b
LE
2996 goto out;
2997
c06ece6b 2998 if (!peer_device) {
bde89a9e 2999 /* This is a connection without a single volume.
367d675d
LE
3000 * Suprisingly enough, it may have a network
3001 * configuration. */
3002 struct net_conf *nc;
543cc10b
LE
3003 dh->minor = -1U;
3004 dh->ret_code = NO_ERROR;
bde89a9e 3005 if (nla_put_drbd_cfg_context(skb, connection, VOLUME_UNSPECIFIED))
367d675d 3006 goto cancel;
bde89a9e 3007 nc = rcu_dereference(connection->net_conf);
367d675d
LE
3008 if (nc && net_conf_to_skb(skb, nc, 1) != 0)
3009 goto cancel;
3010 goto done;
543cc10b 3011 }
b411b363 3012
c06ece6b 3013 device = peer_device->device;
0b0ba1ef
AG
3014 D_ASSERT(device, device->vnr == volume);
3015 D_ASSERT(device, first_peer_device(device)->connection == connection);
3b98c0c2 3016
b30ab791 3017 dh->minor = device_to_minor(device);
3b98c0c2
LE
3018 dh->ret_code = NO_ERROR;
3019
b30ab791 3020 if (nla_put_status_info(skb, device, NULL)) {
367d675d 3021cancel:
3b98c0c2 3022 genlmsg_cancel(skb, dh);
543cc10b 3023 goto out;
3b98c0c2 3024 }
367d675d 3025done:
3b98c0c2 3026 genlmsg_end(skb, dh);
bde89a9e 3027 }
b411b363 3028
543cc10b 3029out:
c141ebda 3030 rcu_read_unlock();
543cc10b 3031 /* where to start the next iteration */
bde89a9e 3032 cb->args[0] = (long)pos;
77c556f6 3033 cb->args[1] = (pos == resource) ? volume + 1 : 0;
b411b363 3034
77c556f6 3035 /* No more resources/volumes/minors found results in an empty skb.
543cc10b 3036 * Which will terminate the dump. */
3b98c0c2 3037 return skb->len;
b411b363
PR
3038}
3039
71932efc
LE
3040/*
3041 * Request status of all resources, or of all volumes within a single resource.
3042 *
3043 * This is a dump, as the answer may not fit in a single reply skb otherwise.
3044 * Which means we cannot use the family->attrbuf or other such members, because
3045 * dump is NOT protected by the genl_lock(). During dump, we only have access
3046 * to the incoming skb, and need to opencode "parsing" of the nlattr payload.
3047 *
3048 * Once things are setup properly, we call into get_one_status().
b411b363 3049 */
71932efc 3050int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb)
b411b363 3051{
71932efc
LE
3052 const unsigned hdrlen = GENL_HDRLEN + GENL_MAGIC_FAMILY_HDRSZ;
3053 struct nlattr *nla;
7c3063cc 3054 const char *resource_name;
4bc76048 3055 struct drbd_resource *resource;
7c3063cc 3056 int maxtype;
71932efc
LE
3057
3058 /* Is this a followup call? */
3059 if (cb->args[0]) {
3060 /* ... of a single resource dump,
3061 * and the resource iterator has been advanced already? */
3062 if (cb->args[2] && cb->args[2] != cb->args[0])
3063 return 0; /* DONE. */
3064 goto dump;
3065 }
3066
3067 /* First call (from netlink_dump_start). We need to figure out
3068 * which resource(s) the user wants us to dump. */
3069 nla = nla_find(nlmsg_attrdata(cb->nlh, hdrlen),
3070 nlmsg_attrlen(cb->nlh, hdrlen),
3071 DRBD_NLA_CFG_CONTEXT);
3072
3073 /* No explicit context given. Dump all. */
3074 if (!nla)
3075 goto dump;
7c3063cc
AG
3076 maxtype = ARRAY_SIZE(drbd_cfg_context_nl_policy) - 1;
3077 nla = drbd_nla_find_nested(maxtype, nla, __nla_type(T_ctx_resource_name));
3078 if (IS_ERR(nla))
3079 return PTR_ERR(nla);
71932efc
LE
3080 /* context given, but no name present? */
3081 if (!nla)
3082 return -EINVAL;
7c3063cc 3083 resource_name = nla_data(nla);
4bc76048
AG
3084 if (!*resource_name)
3085 return -ENODEV;
3086 resource = drbd_find_resource(resource_name);
3087 if (!resource)
71932efc
LE
3088 return -ENODEV;
3089
4bc76048 3090 kref_put(&resource->kref, drbd_destroy_resource); /* get_one_status() revalidates the resource */
0ace9dfa 3091
71932efc 3092 /* prime iterators, and set "filter" mode mark:
bde89a9e 3093 * only dump this connection. */
4bc76048 3094 cb->args[0] = (long)resource;
71932efc 3095 /* cb->args[1] = 0; passed in this way. */
4bc76048 3096 cb->args[2] = (long)resource;
71932efc
LE
3097
3098dump:
3099 return get_one_status(skb, cb);
3100}
b411b363 3101
3b98c0c2 3102int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info)
b411b363 3103{
3b98c0c2
LE
3104 enum drbd_ret_code retcode;
3105 struct timeout_parms tp;
3106 int err;
b411b363 3107
3b98c0c2
LE
3108 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
3109 if (!adm_ctx.reply_skb)
3110 return retcode;
3111 if (retcode != NO_ERROR)
3112 goto out;
b411b363 3113
3b98c0c2 3114 tp.timeout_type =
b30ab791
AG
3115 adm_ctx.device->state.pdsk == D_OUTDATED ? UT_PEER_OUTDATED :
3116 test_bit(USE_DEGR_WFC_T, &adm_ctx.device->flags) ? UT_DEGRADED :
3b98c0c2 3117 UT_DEFAULT;
b411b363 3118
3b98c0c2
LE
3119 err = timeout_parms_to_priv_skb(adm_ctx.reply_skb, &tp);
3120 if (err) {
3121 nlmsg_free(adm_ctx.reply_skb);
3122 return err;
3123 }
3124out:
3125 drbd_adm_finish(info, retcode);
3126 return 0;
b411b363
PR
3127}
3128
3b98c0c2 3129int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info)
b411b363 3130{
b30ab791 3131 struct drbd_device *device;
3b98c0c2 3132 enum drbd_ret_code retcode;
58ffa580 3133 struct start_ov_parms parms;
b411b363 3134
3b98c0c2
LE
3135 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
3136 if (!adm_ctx.reply_skb)
3137 return retcode;
3138 if (retcode != NO_ERROR)
3139 goto out;
873b0d5f 3140
b30ab791 3141 device = adm_ctx.device;
58ffa580
LE
3142
3143 /* resume from last known position, if possible */
b30ab791 3144 parms.ov_start_sector = device->ov_start_sector;
58ffa580 3145 parms.ov_stop_sector = ULLONG_MAX;
3b98c0c2 3146 if (info->attrs[DRBD_NLA_START_OV_PARMS]) {
f399002e 3147 int err = start_ov_parms_from_attrs(&parms, info);
3b98c0c2
LE
3148 if (err) {
3149 retcode = ERR_MANDATORY_TAG;
3150 drbd_msg_put_info(from_attrs_err_to_txt(err));
3151 goto out;
3152 }
b411b363 3153 }
58ffa580 3154 /* w_make_ov_request expects position to be aligned */
b30ab791
AG
3155 device->ov_start_sector = parms.ov_start_sector & ~(BM_SECT_PER_BIT-1);
3156 device->ov_stop_sector = parms.ov_stop_sector;
873b0d5f
LE
3157
3158 /* If there is still bitmap IO pending, e.g. previous resync or verify
3159 * just being finished, wait for it before requesting a new resync. */
b30ab791
AG
3160 drbd_suspend_io(device);
3161 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
3162 retcode = drbd_request_state(device, NS(conn, C_VERIFY_S));
3163 drbd_resume_io(device);
3b98c0c2
LE
3164out:
3165 drbd_adm_finish(info, retcode);
b411b363
PR
3166 return 0;
3167}
3168
3169
3b98c0c2 3170int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info)
b411b363 3171{
b30ab791 3172 struct drbd_device *device;
3b98c0c2 3173 enum drbd_ret_code retcode;
b411b363
PR
3174 int skip_initial_sync = 0;
3175 int err;
3b98c0c2 3176 struct new_c_uuid_parms args;
b411b363 3177
3b98c0c2
LE
3178 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
3179 if (!adm_ctx.reply_skb)
3180 return retcode;
3181 if (retcode != NO_ERROR)
3182 goto out_nolock;
b411b363 3183
b30ab791 3184 device = adm_ctx.device;
3b98c0c2
LE
3185 memset(&args, 0, sizeof(args));
3186 if (info->attrs[DRBD_NLA_NEW_C_UUID_PARMS]) {
f399002e 3187 err = new_c_uuid_parms_from_attrs(&args, info);
3b98c0c2
LE
3188 if (err) {
3189 retcode = ERR_MANDATORY_TAG;
3190 drbd_msg_put_info(from_attrs_err_to_txt(err));
3191 goto out_nolock;
3192 }
b411b363
PR
3193 }
3194
b30ab791 3195 mutex_lock(device->state_mutex); /* Protects us against serialized state changes. */
b411b363 3196
b30ab791 3197 if (!get_ldev(device)) {
b411b363
PR
3198 retcode = ERR_NO_DISK;
3199 goto out;
3200 }
3201
3202 /* this is "skip initial sync", assume to be clean */
a6b32bc3
AG
3203 if (device->state.conn == C_CONNECTED &&
3204 first_peer_device(device)->connection->agreed_pro_version >= 90 &&
b30ab791 3205 device->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED && args.clear_bm) {
d0180171 3206 drbd_info(device, "Preparing to skip initial sync\n");
b411b363 3207 skip_initial_sync = 1;
b30ab791 3208 } else if (device->state.conn != C_STANDALONE) {
b411b363
PR
3209 retcode = ERR_CONNECTED;
3210 goto out_dec;
3211 }
3212
b30ab791
AG
3213 drbd_uuid_set(device, UI_BITMAP, 0); /* Rotate UI_BITMAP to History 1, etc... */
3214 drbd_uuid_new_current(device); /* New current, previous to UI_BITMAP */
b411b363
PR
3215
3216 if (args.clear_bm) {
b30ab791 3217 err = drbd_bitmap_io(device, &drbd_bmio_clear_n_write,
20ceb2b2 3218 "clear_n_write from new_c_uuid", BM_LOCKED_MASK);
b411b363 3219 if (err) {
d0180171 3220 drbd_err(device, "Writing bitmap failed with %d\n", err);
b411b363
PR
3221 retcode = ERR_IO_MD_DISK;
3222 }
3223 if (skip_initial_sync) {
b30ab791
AG
3224 drbd_send_uuids_skip_initial_sync(device);
3225 _drbd_uuid_set(device, UI_BITMAP, 0);
3226 drbd_print_uuids(device, "cleared bitmap UUID");
a6b32bc3 3227 spin_lock_irq(&first_peer_device(device)->connection->req_lock);
b30ab791 3228 _drbd_set_state(_NS2(device, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
b411b363 3229 CS_VERBOSE, NULL);
a6b32bc3 3230 spin_unlock_irq(&first_peer_device(device)->connection->req_lock);
b411b363
PR
3231 }
3232 }
3233
b30ab791 3234 drbd_md_sync(device);
b411b363 3235out_dec:
b30ab791 3236 put_ldev(device);
b411b363 3237out:
b30ab791 3238 mutex_unlock(device->state_mutex);
3b98c0c2
LE
3239out_nolock:
3240 drbd_adm_finish(info, retcode);
b411b363
PR
3241 return 0;
3242}
3243
3b98c0c2 3244static enum drbd_ret_code
7c3063cc 3245drbd_check_resource_name(const char *name)
b411b363 3246{
3b98c0c2 3247 if (!name || !name[0]) {
7c3063cc 3248 drbd_msg_put_info("resource name missing");
3b98c0c2 3249 return ERR_MANDATORY_TAG;
b411b363 3250 }
3b98c0c2
LE
3251 /* if we want to use these in sysfs/configfs/debugfs some day,
3252 * we must not allow slashes */
3253 if (strchr(name, '/')) {
7c3063cc 3254 drbd_msg_put_info("invalid resource name");
3b98c0c2 3255 return ERR_INVALID_REQUEST;
b411b363 3256 }
3b98c0c2 3257 return NO_ERROR;
774b3055 3258}
b411b363 3259
789c1b62 3260int drbd_adm_new_resource(struct sk_buff *skb, struct genl_info *info)
b411b363 3261{
3b98c0c2 3262 enum drbd_ret_code retcode;
afbbfa88
AG
3263 struct res_opts res_opts;
3264 int err;
b411b363 3265
3b98c0c2
LE
3266 retcode = drbd_adm_prepare(skb, info, 0);
3267 if (!adm_ctx.reply_skb)
3268 return retcode;
3269 if (retcode != NO_ERROR)
3270 goto out;
b411b363 3271
afbbfa88
AG
3272 set_res_opts_defaults(&res_opts);
3273 err = res_opts_from_attrs(&res_opts, info);
3274 if (err && err != -ENOMSG) {
3275 retcode = ERR_MANDATORY_TAG;
3276 drbd_msg_put_info(from_attrs_err_to_txt(err));
3277 goto out;
b411b363
PR
3278 }
3279
7c3063cc 3280 retcode = drbd_check_resource_name(adm_ctx.resource_name);
3b98c0c2
LE
3281 if (retcode != NO_ERROR)
3282 goto out;
b411b363 3283
bde89a9e 3284 if (adm_ctx.connection) {
38f19616
LE
3285 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL) {
3286 retcode = ERR_INVALID_REQUEST;
789c1b62 3287 drbd_msg_put_info("resource exists");
38f19616
LE
3288 }
3289 /* else: still NO_ERROR */
3b98c0c2 3290 goto out;
b411b363 3291 }
b411b363 3292
afbbfa88 3293 if (!conn_create(adm_ctx.resource_name, &res_opts))
b411b363 3294 retcode = ERR_NOMEM;
3b98c0c2
LE
3295out:
3296 drbd_adm_finish(info, retcode);
3297 return 0;
b411b363
PR
3298}
3299
05a10ec7 3300int drbd_adm_new_minor(struct sk_buff *skb, struct genl_info *info)
b411b363 3301{
3b98c0c2
LE
3302 struct drbd_genlmsghdr *dh = info->userhdr;
3303 enum drbd_ret_code retcode;
b411b363 3304
44e52cfa 3305 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
3b98c0c2
LE
3306 if (!adm_ctx.reply_skb)
3307 return retcode;
3308 if (retcode != NO_ERROR)
3309 goto out;
b411b363 3310
f2257a56 3311 if (dh->minor > MINORMASK) {
3b98c0c2
LE
3312 drbd_msg_put_info("requested minor out of range");
3313 retcode = ERR_INVALID_REQUEST;
3314 goto out;
b411b363 3315 }
0c8e36d9 3316 if (adm_ctx.volume > DRBD_VOLUME_MAX) {
3b98c0c2
LE
3317 drbd_msg_put_info("requested volume id out of range");
3318 retcode = ERR_INVALID_REQUEST;
3319 goto out;
b411b363 3320 }
b411b363 3321
38f19616 3322 /* drbd_adm_prepare made sure already
a6b32bc3 3323 * that first_peer_device(device)->connection and device->vnr match the request. */
b30ab791 3324 if (adm_ctx.device) {
38f19616
LE
3325 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
3326 retcode = ERR_MINOR_EXISTS;
3327 /* else: still NO_ERROR */
3328 goto out;
b411b363 3329 }
38f19616 3330
a6b32bc3 3331 retcode = drbd_create_minor(adm_ctx.connection, dh->minor, adm_ctx.volume);
3b98c0c2
LE
3332out:
3333 drbd_adm_finish(info, retcode);
3334 return 0;
b411b363
PR
3335}
3336
05a10ec7 3337static enum drbd_ret_code adm_del_minor(struct drbd_device *device)
b411b363 3338{
b30ab791
AG
3339 if (device->state.disk == D_DISKLESS &&
3340 /* no need to be device->state.conn == C_STANDALONE &&
85f75dd7
LE
3341 * we may want to delete a minor from a live replication group.
3342 */
b30ab791
AG
3343 device->state.role == R_SECONDARY) {
3344 _drbd_request_state(device, NS(conn, C_WF_REPORT_PARAMS),
369bea63 3345 CS_VERBOSE + CS_WAIT_COMPLETE);
803ea134 3346 drbd_delete_minor(device);
85f75dd7
LE
3347 return NO_ERROR;
3348 } else
3349 return ERR_MINOR_CONFIGURED;
b411b363
PR
3350}
3351
05a10ec7 3352int drbd_adm_del_minor(struct sk_buff *skb, struct genl_info *info)
b411b363 3353{
3b98c0c2 3354 enum drbd_ret_code retcode;
b411b363 3355
3b98c0c2
LE
3356 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
3357 if (!adm_ctx.reply_skb)
3358 return retcode;
3359 if (retcode != NO_ERROR)
3360 goto out;
b411b363 3361
05a10ec7 3362 retcode = adm_del_minor(adm_ctx.device);
85f75dd7
LE
3363out:
3364 drbd_adm_finish(info, retcode);
3365 return 0;
b411b363
PR
3366}
3367
85f75dd7 3368int drbd_adm_down(struct sk_buff *skb, struct genl_info *info)
b411b363 3369{
f3dfa40a 3370 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
c06ece6b 3371 struct drbd_peer_device *peer_device;
85f75dd7 3372 unsigned i;
b411b363 3373
a10f6b8a 3374 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
85f75dd7
LE
3375 if (!adm_ctx.reply_skb)
3376 return retcode;
3377 if (retcode != NO_ERROR)
3378 goto out;
b411b363 3379
85f75dd7 3380 /* demote */
c06ece6b
AG
3381 idr_for_each_entry(&adm_ctx.connection->peer_devices, peer_device, i) {
3382 retcode = drbd_set_role(peer_device->device, R_SECONDARY, 0);
85f75dd7
LE
3383 if (retcode < SS_SUCCESS) {
3384 drbd_msg_put_info("failed to demote");
c141ebda 3385 goto out;
85f75dd7 3386 }
b411b363 3387 }
b411b363 3388
bde89a9e 3389 retcode = conn_try_disconnect(adm_ctx.connection, 0);
f3dfa40a 3390 if (retcode < SS_SUCCESS) {
85f75dd7 3391 drbd_msg_put_info("failed to disconnect");
f3dfa40a 3392 goto out;
85f75dd7 3393 }
b411b363 3394
85f75dd7 3395 /* detach */
c06ece6b
AG
3396 idr_for_each_entry(&adm_ctx.connection->peer_devices, peer_device, i) {
3397 retcode = adm_detach(peer_device->device, 0);
27012382 3398 if (retcode < SS_SUCCESS || retcode > NO_ERROR) {
85f75dd7 3399 drbd_msg_put_info("failed to detach");
c141ebda 3400 goto out;
85f75dd7
LE
3401 }
3402 }
b411b363 3403
bde89a9e 3404 /* If we reach this, all volumes (of this connection) are Secondary,
f3dfa40a 3405 * Disconnected, Diskless, aka Unconfigured. Make sure all threads have
c141ebda 3406 * actually stopped, state handling only does drbd_thread_stop_nowait(). */
bde89a9e 3407 drbd_thread_stop(&adm_ctx.connection->worker);
b411b363 3408
f3dfa40a 3409 /* Now, nothing can fail anymore */
b411b363 3410
85f75dd7 3411 /* delete volumes */
c06ece6b
AG
3412 idr_for_each_entry(&adm_ctx.connection->peer_devices, peer_device, i) {
3413 retcode = adm_del_minor(peer_device->device);
85f75dd7
LE
3414 if (retcode != NO_ERROR) {
3415 /* "can not happen" */
3416 drbd_msg_put_info("failed to delete volume");
ef356262 3417 goto out;
85f75dd7
LE
3418 }
3419 }
b411b363 3420
85f75dd7 3421 /* delete connection */
bde89a9e 3422 if (conn_lowest_minor(adm_ctx.connection) < 0) {
77c556f6
AG
3423 struct drbd_resource *resource = adm_ctx.connection->resource;
3424
3425 list_del_rcu(&resource->resources);
ec0bddbc 3426 synchronize_rcu();
77c556f6 3427 drbd_free_resource(resource);
b411b363 3428
85f75dd7
LE
3429 retcode = NO_ERROR;
3430 } else {
3431 /* "can not happen" */
789c1b62 3432 retcode = ERR_RES_IN_USE;
85f75dd7 3433 drbd_msg_put_info("failed to delete connection");
85f75dd7 3434 }
ef356262 3435 goto out;
3b98c0c2
LE
3436out:
3437 drbd_adm_finish(info, retcode);
3438 return 0;
b411b363
PR
3439}
3440
789c1b62 3441int drbd_adm_del_resource(struct sk_buff *skb, struct genl_info *info)
b411b363 3442{
77c556f6
AG
3443 struct drbd_resource *resource;
3444 struct drbd_connection *connection;
3b98c0c2 3445 enum drbd_ret_code retcode;
b411b363 3446
44e52cfa 3447 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
3b98c0c2
LE
3448 if (!adm_ctx.reply_skb)
3449 return retcode;
3450 if (retcode != NO_ERROR)
3451 goto out;
b411b363 3452
77c556f6
AG
3453 resource = adm_ctx.resource;
3454 for_each_connection(connection, resource) {
3455 if (connection->cstate > C_STANDALONE) {
3456 retcode = ERR_NET_CONFIGURED;
3457 goto out;
3458 }
3459 }
3460 if (!idr_is_empty(&resource->devices)) {
789c1b62 3461 retcode = ERR_RES_IN_USE;
77c556f6 3462 goto out;
b411b363
PR
3463 }
3464
77c556f6
AG
3465 list_del_rcu(&resource->resources);
3466 for_each_connection(connection, resource)
3467 drbd_thread_stop(&connection->worker);
3468 synchronize_rcu();
3469 drbd_free_resource(resource);
3470 retcode = NO_ERROR;
3b98c0c2
LE
3471out:
3472 drbd_adm_finish(info, retcode);
b411b363
PR
3473 return 0;
3474}
3475
b30ab791 3476void drbd_bcast_event(struct drbd_device *device, const struct sib_info *sib)
b411b363 3477{
3b98c0c2
LE
3478 static atomic_t drbd_genl_seq = ATOMIC_INIT(2); /* two. */
3479 struct sk_buff *msg;
3480 struct drbd_genlmsghdr *d_out;
3481 unsigned seq;
3482 int err = -ENOMEM;
3483
ef86b779 3484 if (sib->sib_reason == SIB_SYNC_PROGRESS) {
b30ab791
AG
3485 if (time_after(jiffies, device->rs_last_bcast + HZ))
3486 device->rs_last_bcast = jiffies;
ef86b779
PR
3487 else
3488 return;
3489 }
b411b363 3490
3b98c0c2
LE
3491 seq = atomic_inc_return(&drbd_genl_seq);
3492 msg = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
3493 if (!msg)
3494 goto failed;
3495
3496 err = -EMSGSIZE;
3497 d_out = genlmsg_put(msg, 0, seq, &drbd_genl_family, 0, DRBD_EVENT);
3498 if (!d_out) /* cannot happen, but anyways. */
3499 goto nla_put_failure;
b30ab791 3500 d_out->minor = device_to_minor(device);
6f9b5f84 3501 d_out->ret_code = NO_ERROR;
3b98c0c2 3502
b30ab791 3503 if (nla_put_status_info(msg, device, sib))
3b98c0c2
LE
3504 goto nla_put_failure;
3505 genlmsg_end(msg, d_out);
3506 err = drbd_genl_multicast_events(msg, 0);
3507 /* msg has been consumed or freed in netlink_broadcast() */
3508 if (err && err != -ESRCH)
3509 goto failed;
b411b363 3510
3b98c0c2 3511 return;
b411b363 3512
3b98c0c2
LE
3513nla_put_failure:
3514 nlmsg_free(msg);
3515failed:
d0180171 3516 drbd_err(device, "Error %d while broadcasting event. "
3b98c0c2
LE
3517 "Event seq:%u sib_reason:%u\n",
3518 err, seq, sib->sib_reason);
b411b363 3519}