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