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