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