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