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