blob: ac0a175e778c60b9f62a7dd174a8eb487022c6ed [file] [log] [blame]
Philipp Reisnerb411b362009-09-25 16:07:19 -07001/*
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
Philipp Reisnerb411b362009-09-25 16:07:19 -070026#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>
Philipp Reisnerb411b362009-09-25 16:07:19 -070032#include <linux/blkpg.h>
33#include <linux/cpumask.h>
34#include "drbd_int.h"
Philipp Reisner265be2d2010-05-31 10:14:17 +020035#include "drbd_req.h"
Philipp Reisnerb411b362009-09-25 16:07:19 -070036#include "drbd_wrappers.h"
37#include <asm/unaligned.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070038#include <linux/drbd_limits.h>
Philipp Reisner87f7be42010-06-11 13:56:33 +020039#include <linux/kthread.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070040
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010041#include <net/genetlink.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070042
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010043/* .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
47int drbd_adm_add_minor(struct sk_buff *skb, struct genl_info *info);
48int drbd_adm_delete_minor(struct sk_buff *skb, struct genl_info *info);
49
50int drbd_adm_create_connection(struct sk_buff *skb, struct genl_info *info);
51int drbd_adm_delete_connection(struct sk_buff *skb, struct genl_info *info);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +010052int drbd_adm_down(struct sk_buff *skb, struct genl_info *info);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010053
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);
56int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info);
57int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info);
58int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info);
59int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info);
60int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info);
61int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info);
62int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info);
63int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info);
64int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info);
65int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info);
66int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info);
67int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info);
68int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info);
69int drbd_adm_syncer(struct sk_buff *skb, struct genl_info *info);
70int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info);
71int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info);
72/* .dumpit */
73int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb);
74
75#include <linux/drbd_genl_api.h>
76#include <linux/genl_magic_func.h>
77
78/* used blkdev_get_by_path, to claim our meta data device(s) */
Philipp Reisnerb411b362009-09-25 16:07:19 -070079static char *drbd_m_holder = "Hands off! this is DRBD's meta data device.";
80
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010081/* Configuration is strictly serialized, because generic netlink message
82 * processing is strictly serialized by the genl_lock().
83 * Which means we can use one static global drbd_config_context struct.
84 */
85static struct drbd_config_context {
86 /* assigned from drbd_genlmsghdr */
87 unsigned int minor;
88 /* assigned from request attributes, if present */
89 unsigned int volume;
90#define VOLUME_UNSPECIFIED (-1U)
91 /* pointer into the request skb,
92 * limited lifetime! */
93 char *conn_name;
Philipp Reisnerb411b362009-09-25 16:07:19 -070094
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +010095 /* reply buffer */
96 struct sk_buff *reply_skb;
97 /* pointer into reply buffer */
98 struct drbd_genlmsghdr *reply_dh;
99 /* resolved from attributes, if possible */
100 struct drbd_conf *mdev;
101 struct drbd_tconn *tconn;
102} adm_ctx;
103
104static void drbd_adm_send_reply(struct sk_buff *skb, struct genl_info *info)
105{
106 genlmsg_end(skb, genlmsg_data(nlmsg_data(nlmsg_hdr(skb))));
107 if (genlmsg_reply(skb, info))
108 printk(KERN_ERR "drbd: error sending genl reply\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700109}
110
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100111/* Used on a fresh "drbd_adm_prepare"d reply_skb, this cannot fail: The only
112 * reason it could fail was no space in skb, and there are 4k available. */
Lars Ellenberg8432b312011-03-08 16:11:16 +0100113int drbd_msg_put_info(const char *info)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100114{
115 struct sk_buff *skb = adm_ctx.reply_skb;
116 struct nlattr *nla;
117 int err = -EMSGSIZE;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700118
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100119 if (!info || !info[0])
120 return 0;
121
122 nla = nla_nest_start(skb, DRBD_NLA_CFG_REPLY);
123 if (!nla)
124 return err;
125
126 err = nla_put_string(skb, T_info_text, info);
127 if (err) {
128 nla_nest_cancel(skb, nla);
129 return err;
130 } else
131 nla_nest_end(skb, nla);
132 return 0;
133}
134
135/* This would be a good candidate for a "pre_doit" hook,
136 * and per-family private info->pointers.
137 * But we need to stay compatible with older kernels.
138 * If it returns successfully, adm_ctx members are valid.
139 */
140#define DRBD_ADM_NEED_MINOR 1
141#define DRBD_ADM_NEED_CONN 2
142static int drbd_adm_prepare(struct sk_buff *skb, struct genl_info *info,
143 unsigned flags)
144{
145 struct drbd_genlmsghdr *d_in = info->userhdr;
146 const u8 cmd = info->genlhdr->cmd;
147 int err;
148
149 memset(&adm_ctx, 0, sizeof(adm_ctx));
150
151 /* genl_rcv_msg only checks for CAP_NET_ADMIN on "GENL_ADMIN_PERM" :( */
152 if (cmd != DRBD_ADM_GET_STATUS
153 && security_netlink_recv(skb, CAP_SYS_ADMIN))
154 return -EPERM;
155
156 adm_ctx.reply_skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
157 if (!adm_ctx.reply_skb)
158 goto fail;
159
160 adm_ctx.reply_dh = genlmsg_put_reply(adm_ctx.reply_skb,
161 info, &drbd_genl_family, 0, cmd);
162 /* put of a few bytes into a fresh skb of >= 4k will always succeed.
163 * but anyways */
164 if (!adm_ctx.reply_dh)
165 goto fail;
166
167 adm_ctx.reply_dh->minor = d_in->minor;
168 adm_ctx.reply_dh->ret_code = NO_ERROR;
169
170 if (info->attrs[DRBD_NLA_CFG_CONTEXT]) {
171 struct nlattr *nla;
172 /* parse and validate only */
173 err = drbd_cfg_context_from_attrs(NULL, info->attrs);
174 if (err)
175 goto fail;
176
177 /* It was present, and valid,
178 * copy it over to the reply skb. */
179 err = nla_put_nohdr(adm_ctx.reply_skb,
180 info->attrs[DRBD_NLA_CFG_CONTEXT]->nla_len,
181 info->attrs[DRBD_NLA_CFG_CONTEXT]);
182 if (err)
183 goto fail;
184
185 /* and assign stuff to the global adm_ctx */
186 nla = nested_attr_tb[__nla_type(T_ctx_volume)];
187 adm_ctx.volume = nla ? nla_get_u32(nla) : VOLUME_UNSPECIFIED;
188 nla = nested_attr_tb[__nla_type(T_ctx_conn_name)];
189 if (nla)
190 adm_ctx.conn_name = nla_data(nla);
191 } else
192 adm_ctx.volume = VOLUME_UNSPECIFIED;
193
194 adm_ctx.minor = d_in->minor;
195 adm_ctx.mdev = minor_to_mdev(d_in->minor);
196 adm_ctx.tconn = conn_by_name(adm_ctx.conn_name);
197
198 if (!adm_ctx.mdev && (flags & DRBD_ADM_NEED_MINOR)) {
199 drbd_msg_put_info("unknown minor");
200 return ERR_MINOR_INVALID;
201 }
202 if (!adm_ctx.tconn && (flags & DRBD_ADM_NEED_CONN)) {
203 drbd_msg_put_info("unknown connection");
204 return ERR_INVALID_REQUEST;
205 }
206
207 /* some more paranoia, if the request was over-determined */
Lars Ellenberg527f4b22011-03-14 13:58:03 +0100208 if (adm_ctx.mdev && adm_ctx.tconn &&
209 adm_ctx.mdev->tconn != adm_ctx.tconn) {
210 pr_warning("request: minor=%u, conn=%s; but that minor belongs to connection %s\n",
211 adm_ctx.minor, adm_ctx.conn_name, adm_ctx.mdev->tconn->name);
212 drbd_msg_put_info("minor exists in different connection");
213 return ERR_INVALID_REQUEST;
214 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100215 if (adm_ctx.mdev &&
216 adm_ctx.volume != VOLUME_UNSPECIFIED &&
217 adm_ctx.volume != adm_ctx.mdev->vnr) {
218 pr_warning("request: minor=%u, volume=%u; but that minor is volume %u in %s\n",
219 adm_ctx.minor, adm_ctx.volume,
220 adm_ctx.mdev->vnr, adm_ctx.mdev->tconn->name);
Lars Ellenberg527f4b22011-03-14 13:58:03 +0100221 drbd_msg_put_info("minor exists as different volume");
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100222 return ERR_INVALID_REQUEST;
223 }
Lars Ellenbergcffec5b2011-03-10 23:33:15 +0100224 if (adm_ctx.mdev && !adm_ctx.tconn)
225 adm_ctx.tconn = adm_ctx.mdev->tconn;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100226 return NO_ERROR;
227
228fail:
229 nlmsg_free(adm_ctx.reply_skb);
230 adm_ctx.reply_skb = NULL;
231 return -ENOMEM;
232}
233
234static int drbd_adm_finish(struct genl_info *info, int retcode)
235{
236 struct nlattr *nla;
237 const char *conn_name = NULL;
238
239 if (!adm_ctx.reply_skb)
240 return -ENOMEM;
241
242 adm_ctx.reply_dh->ret_code = retcode;
243
244 nla = info->attrs[DRBD_NLA_CFG_CONTEXT];
245 if (nla) {
246 nla = nla_find_nested(nla, __nla_type(T_ctx_conn_name));
247 if (nla)
248 conn_name = nla_data(nla);
249 }
250
251 drbd_adm_send_reply(adm_ctx.reply_skb, info);
252 return 0;
253}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700254
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100255static void setup_khelper_env(struct drbd_tconn *tconn, char **envp)
256{
257 char *afs;
258
259 if (get_net_conf(tconn)) {
260 switch (((struct sockaddr *)tconn->net_conf->peer_addr)->sa_family) {
261 case AF_INET6:
262 afs = "ipv6";
263 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI6",
264 &((struct sockaddr_in6 *)tconn->net_conf->peer_addr)->sin6_addr);
265 break;
266 case AF_INET:
267 afs = "ipv4";
268 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
269 &((struct sockaddr_in *)tconn->net_conf->peer_addr)->sin_addr);
270 break;
271 default:
272 afs = "ssocks";
273 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
274 &((struct sockaddr_in *)tconn->net_conf->peer_addr)->sin_addr);
275 }
276 snprintf(envp[3], 20, "DRBD_PEER_AF=%s", afs);
277 put_net_conf(tconn);
278 }
279}
280
Philipp Reisnerb411b362009-09-25 16:07:19 -0700281int drbd_khelper(struct drbd_conf *mdev, char *cmd)
282{
283 char *envp[] = { "HOME=/",
284 "TERM=linux",
285 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100286 (char[20]) { }, /* address family */
287 (char[60]) { }, /* address */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700288 NULL };
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100289 char mb[12];
Philipp Reisnerb411b362009-09-25 16:07:19 -0700290 char *argv[] = {usermode_helper, cmd, mb, NULL };
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100291 struct sib_info sib;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700292 int ret;
293
294 snprintf(mb, 12, "minor-%d", mdev_to_minor(mdev));
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100295 setup_khelper_env(mdev->tconn, envp);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700296
Lars Ellenberg1090c052010-07-19 17:41:04 +0200297 /* The helper may take some time.
298 * write out any unsynced meta data changes now */
299 drbd_md_sync(mdev);
300
Philipp Reisnerb411b362009-09-25 16:07:19 -0700301 dev_info(DEV, "helper command: %s %s %s\n", usermode_helper, cmd, mb);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100302 sib.sib_reason = SIB_HELPER_PRE;
303 sib.helper_name = cmd;
304 drbd_bcast_event(mdev, &sib);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700305 ret = call_usermodehelper(usermode_helper, argv, envp, 1);
306 if (ret)
307 dev_warn(DEV, "helper command: %s %s %s exit code %u (0x%x)\n",
308 usermode_helper, cmd, mb,
309 (ret >> 8) & 0xff, ret);
310 else
311 dev_info(DEV, "helper command: %s %s %s exit code %u (0x%x)\n",
312 usermode_helper, cmd, mb,
313 (ret >> 8) & 0xff, ret);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100314 sib.sib_reason = SIB_HELPER_POST;
315 sib.helper_exit_code = ret;
316 drbd_bcast_event(mdev, &sib);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700317
318 if (ret < 0) /* Ignore any ERRNOs we got. */
319 ret = 0;
320
321 return ret;
322}
323
Philipp Reisner6b75dce2011-03-16 17:39:12 +0100324static void conn_md_sync(struct drbd_tconn *tconn)
325{
326 struct drbd_conf *mdev;
327 int minor;
328
329 idr_for_each_entry(&tconn->volumes, mdev, minor)
330 drbd_md_sync(mdev);
331}
332
333int conn_khelper(struct drbd_tconn *tconn, char *cmd)
334{
335 char *envp[] = { "HOME=/",
336 "TERM=linux",
337 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
338 (char[20]) { }, /* address family */
339 (char[60]) { }, /* address */
340 NULL };
341 char *argv[] = {usermode_helper, cmd, tconn->name, NULL };
342 int ret;
343
344 setup_khelper_env(tconn, envp);
345 conn_md_sync(tconn);
346
347 conn_info(tconn, "helper command: %s %s %s\n", usermode_helper, cmd, tconn->name);
348 /* TODO: conn_bcast_event() ?? */
349
350 ret = call_usermodehelper(usermode_helper, argv, envp, 1);
351 if (ret)
352 conn_warn(tconn, "helper command: %s %s %s exit code %u (0x%x)\n",
353 usermode_helper, cmd, tconn->name,
354 (ret >> 8) & 0xff, ret);
355 else
356 conn_info(tconn, "helper command: %s %s %s exit code %u (0x%x)\n",
357 usermode_helper, cmd, tconn->name,
358 (ret >> 8) & 0xff, ret);
359 /* TODO: conn_bcast_event() ?? */
360
361 if (ret < 0) /* Ignore any ERRNOs we got. */
362 ret = 0;
363
364 return ret;
365}
366
Philipp Reisnerb411b362009-09-25 16:07:19 -0700367enum drbd_disk_state drbd_try_outdate_peer(struct drbd_conf *mdev)
368{
369 char *ex_to_string;
370 int r;
371 enum drbd_disk_state nps;
372 enum drbd_fencing_p fp;
373
374 D_ASSERT(mdev->state.pdsk == D_UNKNOWN);
375
376 if (get_ldev_if_state(mdev, D_CONSISTENT)) {
377 fp = mdev->ldev->dc.fencing;
378 put_ldev(mdev);
379 } else {
380 dev_warn(DEV, "Not fencing peer, I'm not even Consistent myself.\n");
Philipp Reisnerfb22c402010-09-08 23:20:21 +0200381 nps = mdev->state.pdsk;
382 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700383 }
384
Philipp Reisnerb411b362009-09-25 16:07:19 -0700385 r = drbd_khelper(mdev, "fence-peer");
386
387 switch ((r>>8) & 0xff) {
388 case 3: /* peer is inconsistent */
389 ex_to_string = "peer is inconsistent or worse";
390 nps = D_INCONSISTENT;
391 break;
392 case 4: /* peer got outdated, or was already outdated */
393 ex_to_string = "peer was fenced";
394 nps = D_OUTDATED;
395 break;
396 case 5: /* peer was down */
397 if (mdev->state.disk == D_UP_TO_DATE) {
398 /* we will(have) create(d) a new UUID anyways... */
399 ex_to_string = "peer is unreachable, assumed to be dead";
400 nps = D_OUTDATED;
401 } else {
402 ex_to_string = "peer unreachable, doing nothing since disk != UpToDate";
403 nps = mdev->state.pdsk;
404 }
405 break;
406 case 6: /* Peer is primary, voluntarily outdate myself.
407 * This is useful when an unconnected R_SECONDARY is asked to
408 * become R_PRIMARY, but finds the other peer being active. */
409 ex_to_string = "peer is active";
410 dev_warn(DEV, "Peer is primary, outdating myself.\n");
411 nps = D_UNKNOWN;
412 _drbd_request_state(mdev, NS(disk, D_OUTDATED), CS_WAIT_COMPLETE);
413 break;
414 case 7:
415 if (fp != FP_STONITH)
416 dev_err(DEV, "fence-peer() = 7 && fencing != Stonith !!!\n");
417 ex_to_string = "peer was stonithed";
418 nps = D_OUTDATED;
419 break;
420 default:
421 /* The script is broken ... */
422 nps = D_UNKNOWN;
423 dev_err(DEV, "fence-peer helper broken, returned %d\n", (r>>8)&0xff);
424 return nps;
425 }
426
427 dev_info(DEV, "fence-peer helper returned %d (%s)\n",
428 (r>>8) & 0xff, ex_to_string);
Philipp Reisnerfb22c402010-09-08 23:20:21 +0200429
430out:
431 if (mdev->state.susp_fen && nps >= D_UNKNOWN) {
432 /* The handler was not successful... unfreeze here, the
433 state engine can not unfreeze... */
434 _drbd_request_state(mdev, NS(susp_fen, 0), CS_VERBOSE);
435 }
436
Philipp Reisnerb411b362009-09-25 16:07:19 -0700437 return nps;
438}
439
Philipp Reisner87f7be42010-06-11 13:56:33 +0200440static int _try_outdate_peer_async(void *data)
441{
442 struct drbd_conf *mdev = (struct drbd_conf *)data;
443 enum drbd_disk_state nps;
Philipp Reisner21423fa2011-05-17 14:19:41 +0200444 union drbd_state ns;
Philipp Reisner87f7be42010-06-11 13:56:33 +0200445
446 nps = drbd_try_outdate_peer(mdev);
Philipp Reisner21423fa2011-05-17 14:19:41 +0200447
448 /* Not using
449 drbd_request_state(mdev, NS(pdsk, nps));
Philipp Reisner99432fc2011-05-20 16:39:13 +0200450 here, because we might were able to re-establish the connection
451 in the meantime. This can only partially be solved in the state's
452 engine is_valid_state() and is_valid_state_transition()
453 functions.
454
455 nps can be D_INCONSISTENT, D_OUTDATED or D_UNKNOWN.
456 pdsk == D_INCONSISTENT while conn >= C_CONNECTED is valid,
457 therefore we have to have the pre state change check here.
Philipp Reisner21423fa2011-05-17 14:19:41 +0200458 */
Philipp Reisner87eeee42011-01-19 14:16:30 +0100459 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisner21423fa2011-05-17 14:19:41 +0200460 ns = mdev->state;
461 if (ns.conn < C_WF_REPORT_PARAMS) {
462 ns.pdsk = nps;
463 _drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
464 }
Philipp Reisner87eeee42011-01-19 14:16:30 +0100465 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisner87f7be42010-06-11 13:56:33 +0200466
467 return 0;
468}
469
470void drbd_try_outdate_peer_async(struct drbd_conf *mdev)
471{
472 struct task_struct *opa;
473
474 opa = kthread_run(_try_outdate_peer_async, mdev, "drbd%d_a_helper", mdev_to_minor(mdev));
475 if (IS_ERR(opa))
476 dev_err(DEV, "out of mem, failed to invoke fence-peer helper\n");
477}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700478
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100479enum drbd_state_rv
480drbd_set_role(struct drbd_conf *mdev, enum drbd_role new_role, int force)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700481{
482 const int max_tries = 4;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100483 enum drbd_state_rv rv = SS_UNKNOWN_ERROR;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700484 int try = 0;
485 int forced = 0;
486 union drbd_state mask, val;
487 enum drbd_disk_state nps;
488
489 if (new_role == R_PRIMARY)
Philipp Reisner0625ac12011-02-07 14:49:19 +0100490 request_ping(mdev->tconn); /* Detect a dead peer ASAP */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700491
Philipp Reisner8410da82011-02-11 20:11:10 +0100492 mutex_lock(mdev->state_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700493
494 mask.i = 0; mask.role = R_MASK;
495 val.i = 0; val.role = new_role;
496
497 while (try++ < max_tries) {
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100498 rv = _drbd_request_state(mdev, mask, val, CS_WAIT_COMPLETE);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700499
500 /* in case we first succeeded to outdate,
501 * but now suddenly could establish a connection */
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100502 if (rv == SS_CW_FAILED_BY_PEER && mask.pdsk != 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700503 val.pdsk = 0;
504 mask.pdsk = 0;
505 continue;
506 }
507
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100508 if (rv == SS_NO_UP_TO_DATE_DISK && force &&
Philipp Reisnerd10a33c2010-03-04 15:11:39 +0100509 (mdev->state.disk < D_UP_TO_DATE &&
510 mdev->state.disk >= D_INCONSISTENT)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700511 mask.disk = D_MASK;
512 val.disk = D_UP_TO_DATE;
513 forced = 1;
514 continue;
515 }
516
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100517 if (rv == SS_NO_UP_TO_DATE_DISK &&
Philipp Reisnerb411b362009-09-25 16:07:19 -0700518 mdev->state.disk == D_CONSISTENT && mask.pdsk == 0) {
519 D_ASSERT(mdev->state.pdsk == D_UNKNOWN);
520 nps = drbd_try_outdate_peer(mdev);
521
522 if (nps == D_OUTDATED || nps == D_INCONSISTENT) {
523 val.disk = D_UP_TO_DATE;
524 mask.disk = D_MASK;
525 }
526
527 val.pdsk = nps;
528 mask.pdsk = D_MASK;
529
530 continue;
531 }
532
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100533 if (rv == SS_NOTHING_TO_DO)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100534 goto out;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100535 if (rv == SS_PRIMARY_NOP && mask.pdsk == 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700536 nps = drbd_try_outdate_peer(mdev);
537
538 if (force && nps > D_OUTDATED) {
539 dev_warn(DEV, "Forced into split brain situation!\n");
540 nps = D_OUTDATED;
541 }
542
543 mask.pdsk = D_MASK;
544 val.pdsk = nps;
545
546 continue;
547 }
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100548 if (rv == SS_TWO_PRIMARIES) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700549 /* Maybe the peer is detected as dead very soon...
550 retry at most once more in this case. */
Philipp Reisner89e58e72011-01-19 13:12:45 +0100551 schedule_timeout_interruptible((mdev->tconn->net_conf->ping_timeo+1)*HZ/10);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700552 if (try < max_tries)
553 try = max_tries - 1;
554 continue;
555 }
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100556 if (rv < SS_SUCCESS) {
557 rv = _drbd_request_state(mdev, mask, val,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700558 CS_VERBOSE + CS_WAIT_COMPLETE);
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100559 if (rv < SS_SUCCESS)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100560 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700561 }
562 break;
563 }
564
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100565 if (rv < SS_SUCCESS)
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100566 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700567
568 if (forced)
569 dev_warn(DEV, "Forced to consider local data as UpToDate!\n");
570
571 /* Wait until nothing is on the fly :) */
572 wait_event(mdev->misc_wait, atomic_read(&mdev->ap_pending_cnt) == 0);
573
574 if (new_role == R_SECONDARY) {
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100575 set_disk_ro(mdev->vdisk, true);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700576 if (get_ldev(mdev)) {
577 mdev->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
578 put_ldev(mdev);
579 }
580 } else {
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +0100581 if (get_net_conf(mdev->tconn)) {
Philipp Reisner89e58e72011-01-19 13:12:45 +0100582 mdev->tconn->net_conf->want_lose = 0;
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +0100583 put_net_conf(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700584 }
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100585 set_disk_ro(mdev->vdisk, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700586 if (get_ldev(mdev)) {
587 if (((mdev->state.conn < C_CONNECTED ||
588 mdev->state.pdsk <= D_FAILED)
589 && mdev->ldev->md.uuid[UI_BITMAP] == 0) || forced)
590 drbd_uuid_new_current(mdev);
591
592 mdev->ldev->md.uuid[UI_CURRENT] |= (u64)1;
593 put_ldev(mdev);
594 }
595 }
596
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100597 /* writeout of activity log covered areas of the bitmap
598 * to stable storage done in after state change already */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700599
600 if (mdev->state.conn >= C_WF_REPORT_PARAMS) {
601 /* if this was forced, we should consider sync */
602 if (forced)
603 drbd_send_uuids(mdev);
604 drbd_send_state(mdev);
605 }
606
607 drbd_md_sync(mdev);
608
609 kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100610out:
Philipp Reisner8410da82011-02-11 20:11:10 +0100611 mutex_unlock(mdev->state_mutex);
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +0100612 return rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700613}
614
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100615static const char *from_attrs_err_to_txt(int err)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700616{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100617 return err == -ENOMSG ? "required attribute missing" :
618 err == -EOPNOTSUPP ? "unknown mandatory attribute" :
619 "invalid attribute value";
Philipp Reisnerb411b362009-09-25 16:07:19 -0700620}
621
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100622int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700623{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100624 struct set_role_parms parms;
625 int err;
626 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700627
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100628 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
629 if (!adm_ctx.reply_skb)
630 return retcode;
631 if (retcode != NO_ERROR)
632 goto out;
633
634 memset(&parms, 0, sizeof(parms));
635 if (info->attrs[DRBD_NLA_SET_ROLE_PARMS]) {
636 err = set_role_parms_from_attrs(&parms, info->attrs);
637 if (err) {
638 retcode = ERR_MANDATORY_TAG;
639 drbd_msg_put_info(from_attrs_err_to_txt(err));
640 goto out;
641 }
642 }
643
644 if (info->genlhdr->cmd == DRBD_ADM_PRIMARY)
645 retcode = drbd_set_role(adm_ctx.mdev, R_PRIMARY, parms.assume_uptodate);
646 else
647 retcode = drbd_set_role(adm_ctx.mdev, R_SECONDARY, 0);
648out:
649 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700650 return 0;
651}
652
653/* initializes the md.*_offset members, so we are able to find
654 * the on disk meta data */
655static void drbd_md_set_sector_offsets(struct drbd_conf *mdev,
656 struct drbd_backing_dev *bdev)
657{
658 sector_t md_size_sect = 0;
659 switch (bdev->dc.meta_dev_idx) {
660 default:
661 /* v07 style fixed size indexed meta data */
662 bdev->md.md_size_sect = MD_RESERVED_SECT;
663 bdev->md.md_offset = drbd_md_ss__(mdev, bdev);
664 bdev->md.al_offset = MD_AL_OFFSET;
665 bdev->md.bm_offset = MD_BM_OFFSET;
666 break;
667 case DRBD_MD_INDEX_FLEX_EXT:
668 /* just occupy the full device; unit: sectors */
669 bdev->md.md_size_sect = drbd_get_capacity(bdev->md_bdev);
670 bdev->md.md_offset = 0;
671 bdev->md.al_offset = MD_AL_OFFSET;
672 bdev->md.bm_offset = MD_BM_OFFSET;
673 break;
674 case DRBD_MD_INDEX_INTERNAL:
675 case DRBD_MD_INDEX_FLEX_INT:
676 bdev->md.md_offset = drbd_md_ss__(mdev, bdev);
677 /* al size is still fixed */
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100678 bdev->md.al_offset = -MD_AL_SECTORS;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700679 /* we need (slightly less than) ~ this much bitmap sectors: */
680 md_size_sect = drbd_get_capacity(bdev->backing_bdev);
681 md_size_sect = ALIGN(md_size_sect, BM_SECT_PER_EXT);
682 md_size_sect = BM_SECT_TO_EXT(md_size_sect);
683 md_size_sect = ALIGN(md_size_sect, 8);
684
685 /* plus the "drbd meta data super block",
686 * and the activity log; */
687 md_size_sect += MD_BM_OFFSET;
688
689 bdev->md.md_size_sect = md_size_sect;
690 /* bitmap offset is adjusted by 'super' block size */
691 bdev->md.bm_offset = -md_size_sect + MD_AL_OFFSET;
692 break;
693 }
694}
695
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100696/* input size is expected to be in KB */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700697char *ppsize(char *buf, unsigned long long size)
698{
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100699 /* Needs 9 bytes at max including trailing NUL:
700 * -1ULL ==> "16384 EB" */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700701 static char units[] = { 'K', 'M', 'G', 'T', 'P', 'E' };
702 int base = 0;
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100703 while (size >= 10000 && base < sizeof(units)-1) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700704 /* shift + round */
705 size = (size >> 10) + !!(size & (1<<9));
706 base++;
707 }
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100708 sprintf(buf, "%u %cB", (unsigned)size, units[base]);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700709
710 return buf;
711}
712
713/* there is still a theoretical deadlock when called from receiver
714 * on an D_INCONSISTENT R_PRIMARY:
715 * remote READ does inc_ap_bio, receiver would need to receive answer
716 * packet from remote to dec_ap_bio again.
717 * receiver receive_sizes(), comes here,
718 * waits for ap_bio_cnt == 0. -> deadlock.
719 * but this cannot happen, actually, because:
720 * R_PRIMARY D_INCONSISTENT, and peer's disk is unreachable
721 * (not connected, or bad/no disk on peer):
722 * see drbd_fail_request_early, ap_bio_cnt is zero.
723 * R_PRIMARY D_INCONSISTENT, and C_SYNC_TARGET:
724 * peer may not initiate a resize.
725 */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100726/* Note these are not to be confused with
727 * drbd_adm_suspend_io/drbd_adm_resume_io,
728 * which are (sub) state changes triggered by admin (drbdsetup),
729 * and can be long lived.
730 * This changes an mdev->flag, is triggered by drbd internals,
731 * and should be short-lived. */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700732void drbd_suspend_io(struct drbd_conf *mdev)
733{
734 set_bit(SUSPEND_IO, &mdev->flags);
Philipp Reisnerfb22c402010-09-08 23:20:21 +0200735 if (is_susp(mdev->state))
Philipp Reisner265be2d2010-05-31 10:14:17 +0200736 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700737 wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_bio_cnt));
738}
739
740void drbd_resume_io(struct drbd_conf *mdev)
741{
742 clear_bit(SUSPEND_IO, &mdev->flags);
743 wake_up(&mdev->misc_wait);
744}
745
746/**
747 * drbd_determine_dev_size() - Sets the right device size obeying all constraints
748 * @mdev: DRBD device.
749 *
750 * Returns 0 on success, negative return values indicate errors.
751 * You should call drbd_md_sync() after calling this function.
752 */
Bart Van Assche24c48302011-05-21 18:32:29 +0200753enum determine_dev_size drbd_determine_dev_size(struct drbd_conf *mdev, enum dds_flags flags) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700754{
755 sector_t prev_first_sect, prev_size; /* previous meta location */
756 sector_t la_size;
757 sector_t size;
758 char ppb[10];
759
760 int md_moved, la_size_changed;
761 enum determine_dev_size rv = unchanged;
762
763 /* race:
764 * application request passes inc_ap_bio,
765 * but then cannot get an AL-reference.
766 * this function later may wait on ap_bio_cnt == 0. -> deadlock.
767 *
768 * to avoid that:
769 * Suspend IO right here.
770 * still lock the act_log to not trigger ASSERTs there.
771 */
772 drbd_suspend_io(mdev);
773
774 /* no wait necessary anymore, actually we could assert that */
775 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
776
777 prev_first_sect = drbd_md_first_sector(mdev->ldev);
778 prev_size = mdev->ldev->md.md_size_sect;
779 la_size = mdev->ldev->md.la_size_sect;
780
781 /* TODO: should only be some assert here, not (re)init... */
782 drbd_md_set_sector_offsets(mdev, mdev->ldev);
783
Philipp Reisnerd8450302010-03-24 15:51:26 +0100784 size = drbd_new_dev_size(mdev, mdev->ldev, flags & DDSF_FORCED);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700785
786 if (drbd_get_capacity(mdev->this_bdev) != size ||
787 drbd_bm_capacity(mdev) != size) {
788 int err;
Philipp Reisner02d9a942010-03-24 16:23:03 +0100789 err = drbd_bm_resize(mdev, size, !(flags & DDSF_NO_RESYNC));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700790 if (unlikely(err)) {
791 /* currently there is only one error: ENOMEM! */
792 size = drbd_bm_capacity(mdev)>>1;
793 if (size == 0) {
794 dev_err(DEV, "OUT OF MEMORY! "
795 "Could not allocate bitmap!\n");
796 } else {
797 dev_err(DEV, "BM resizing failed. "
798 "Leaving size unchanged at size = %lu KB\n",
799 (unsigned long)size);
800 }
801 rv = dev_size_error;
802 }
803 /* racy, see comments above. */
804 drbd_set_my_capacity(mdev, size);
805 mdev->ldev->md.la_size_sect = size;
806 dev_info(DEV, "size = %s (%llu KB)\n", ppsize(ppb, size>>1),
807 (unsigned long long)size>>1);
808 }
809 if (rv == dev_size_error)
810 goto out;
811
812 la_size_changed = (la_size != mdev->ldev->md.la_size_sect);
813
814 md_moved = prev_first_sect != drbd_md_first_sector(mdev->ldev)
815 || prev_size != mdev->ldev->md.md_size_sect;
816
817 if (la_size_changed || md_moved) {
Andreas Gruenbacher24dccab2010-12-12 17:45:41 +0100818 int err;
819
Philipp Reisnerb411b362009-09-25 16:07:19 -0700820 drbd_al_shrink(mdev); /* All extents inactive. */
821 dev_info(DEV, "Writing the whole bitmap, %s\n",
822 la_size_changed && md_moved ? "size changed and md moved" :
823 la_size_changed ? "size changed" : "md moved");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100824 /* next line implicitly does drbd_suspend_io()+drbd_resume_io() */
825 err = drbd_bitmap_io(mdev, &drbd_bm_write,
826 "size changed", BM_LOCKED_MASK);
Andreas Gruenbacher24dccab2010-12-12 17:45:41 +0100827 if (err) {
828 rv = dev_size_error;
829 goto out;
830 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700831 drbd_md_mark_dirty(mdev);
832 }
833
834 if (size > la_size)
835 rv = grew;
836 if (size < la_size)
837 rv = shrunk;
838out:
839 lc_unlock(mdev->act_log);
840 wake_up(&mdev->al_wait);
841 drbd_resume_io(mdev);
842
843 return rv;
844}
845
846sector_t
Philipp Reisnera393db62009-12-22 13:35:52 +0100847drbd_new_dev_size(struct drbd_conf *mdev, struct drbd_backing_dev *bdev, int assume_peer_has_space)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700848{
849 sector_t p_size = mdev->p_size; /* partner's disk size. */
850 sector_t la_size = bdev->md.la_size_sect; /* last agreed size. */
851 sector_t m_size; /* my size */
852 sector_t u_size = bdev->dc.disk_size; /* size requested by user. */
853 sector_t size = 0;
854
855 m_size = drbd_get_max_capacity(bdev);
856
Philipp Reisnera393db62009-12-22 13:35:52 +0100857 if (mdev->state.conn < C_CONNECTED && assume_peer_has_space) {
858 dev_warn(DEV, "Resize while not connected was forced by the user!\n");
859 p_size = m_size;
860 }
861
Philipp Reisnerb411b362009-09-25 16:07:19 -0700862 if (p_size && m_size) {
863 size = min_t(sector_t, p_size, m_size);
864 } else {
865 if (la_size) {
866 size = la_size;
867 if (m_size && m_size < size)
868 size = m_size;
869 if (p_size && p_size < size)
870 size = p_size;
871 } else {
872 if (m_size)
873 size = m_size;
874 if (p_size)
875 size = p_size;
876 }
877 }
878
879 if (size == 0)
880 dev_err(DEV, "Both nodes diskless!\n");
881
882 if (u_size) {
883 if (u_size > size)
884 dev_err(DEV, "Requested disk size is too big (%lu > %lu)\n",
885 (unsigned long)u_size>>1, (unsigned long)size>>1);
886 else
887 size = u_size;
888 }
889
890 return size;
891}
892
893/**
894 * drbd_check_al_size() - Ensures that the AL is of the right size
895 * @mdev: DRBD device.
896 *
897 * Returns -EBUSY if current al lru is still used, -ENOMEM when allocation
898 * failed, and 0 on success. You should call drbd_md_sync() after you called
899 * this function.
900 */
901static int drbd_check_al_size(struct drbd_conf *mdev)
902{
903 struct lru_cache *n, *t;
904 struct lc_element *e;
905 unsigned int in_use;
906 int i;
907
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100908 if (!expect(mdev->sync_conf.al_extents >= DRBD_AL_EXTENTS_MIN))
909 mdev->sync_conf.al_extents = DRBD_AL_EXTENTS_MIN;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700910
911 if (mdev->act_log &&
912 mdev->act_log->nr_elements == mdev->sync_conf.al_extents)
913 return 0;
914
915 in_use = 0;
916 t = mdev->act_log;
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100917 n = lc_create("act_log", drbd_al_ext_cache, AL_UPDATES_PER_TRANSACTION,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700918 mdev->sync_conf.al_extents, sizeof(struct lc_element), 0);
919
920 if (n == NULL) {
921 dev_err(DEV, "Cannot allocate act_log lru!\n");
922 return -ENOMEM;
923 }
924 spin_lock_irq(&mdev->al_lock);
925 if (t) {
926 for (i = 0; i < t->nr_elements; i++) {
927 e = lc_element_by_index(t, i);
928 if (e->refcnt)
929 dev_err(DEV, "refcnt(%d)==%d\n",
930 e->lc_number, e->refcnt);
931 in_use += e->refcnt;
932 }
933 }
934 if (!in_use)
935 mdev->act_log = n;
936 spin_unlock_irq(&mdev->al_lock);
937 if (in_use) {
938 dev_err(DEV, "Activity log still in use!\n");
939 lc_destroy(n);
940 return -EBUSY;
941 } else {
942 if (t)
943 lc_destroy(t);
944 }
945 drbd_md_mark_dirty(mdev); /* we changed mdev->act_log->nr_elemens */
946 return 0;
947}
948
Philipp Reisner99432fc2011-05-20 16:39:13 +0200949static void drbd_setup_queue_param(struct drbd_conf *mdev, unsigned int max_bio_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700950{
951 struct request_queue * const q = mdev->rq_queue;
Philipp Reisner99432fc2011-05-20 16:39:13 +0200952 int max_hw_sectors = max_bio_size >> 9;
953 int max_segments = 0;
954
955 if (get_ldev_if_state(mdev, D_ATTACHING)) {
956 struct request_queue * const b = mdev->ldev->backing_bdev->bd_disk->queue;
957
958 max_hw_sectors = min(queue_max_hw_sectors(b), max_bio_size >> 9);
959 max_segments = mdev->ldev->dc.max_bio_bvecs;
960 put_ldev(mdev);
961 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700962
Philipp Reisnerb411b362009-09-25 16:07:19 -0700963 blk_queue_logical_block_size(q, 512);
Lars Ellenberg1816a2b2010-11-11 15:19:07 +0100964 blk_queue_max_hw_sectors(q, max_hw_sectors);
965 /* This is the workaround for "bio would need to, but cannot, be split" */
966 blk_queue_max_segments(q, max_segments ? max_segments : BLK_MAX_SEGMENTS);
967 blk_queue_segment_boundary(q, PAGE_CACHE_SIZE-1);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700968
Philipp Reisner99432fc2011-05-20 16:39:13 +0200969 if (get_ldev_if_state(mdev, D_ATTACHING)) {
970 struct request_queue * const b = mdev->ldev->backing_bdev->bd_disk->queue;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700971
Philipp Reisner99432fc2011-05-20 16:39:13 +0200972 blk_queue_stack_limits(q, b);
973
974 if (q->backing_dev_info.ra_pages != b->backing_dev_info.ra_pages) {
975 dev_info(DEV, "Adjusting my ra_pages to backing device's (%lu -> %lu)\n",
976 q->backing_dev_info.ra_pages,
977 b->backing_dev_info.ra_pages);
978 q->backing_dev_info.ra_pages = b->backing_dev_info.ra_pages;
979 }
980 put_ldev(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700981 }
982}
983
Philipp Reisner99432fc2011-05-20 16:39:13 +0200984void drbd_reconsider_max_bio_size(struct drbd_conf *mdev)
985{
986 int now, new, local, peer;
987
988 now = queue_max_hw_sectors(mdev->rq_queue) << 9;
989 local = mdev->local_max_bio_size; /* Eventually last known value, from volatile memory */
990 peer = mdev->peer_max_bio_size; /* Eventually last known value, from meta data */
991
992 if (get_ldev_if_state(mdev, D_ATTACHING)) {
993 local = queue_max_hw_sectors(mdev->ldev->backing_bdev->bd_disk->queue) << 9;
994 mdev->local_max_bio_size = local;
995 put_ldev(mdev);
996 }
997
998 /* We may ignore peer limits if the peer is modern enough.
999 Because new from 8.3.8 onwards the peer can use multiple
1000 BIOs for a single peer_request */
1001 if (mdev->state.conn >= C_CONNECTED) {
Philipp Reisner31890f42011-01-19 14:12:51 +01001002 if (mdev->tconn->agreed_pro_version < 94)
Philipp Reisner99432fc2011-05-20 16:39:13 +02001003 peer = mdev->peer_max_bio_size;
Philipp Reisner31890f42011-01-19 14:12:51 +01001004 else if (mdev->tconn->agreed_pro_version == 94)
Philipp Reisner99432fc2011-05-20 16:39:13 +02001005 peer = DRBD_MAX_SIZE_H80_PACKET;
1006 else /* drbd 8.3.8 onwards */
1007 peer = DRBD_MAX_BIO_SIZE;
1008 }
1009
1010 new = min_t(int, local, peer);
1011
1012 if (mdev->state.role == R_PRIMARY && new < now)
1013 dev_err(DEV, "ASSERT FAILED new < now; (%d < %d)\n", new, now);
1014
1015 if (new != now)
1016 dev_info(DEV, "max BIO size = %u\n", new);
1017
1018 drbd_setup_queue_param(mdev, new);
1019}
1020
Philipp Reisnerb411b362009-09-25 16:07:19 -07001021/* serialize deconfig (worker exiting, doing cleanup)
1022 * and reconfig (drbdsetup disk, drbdsetup net)
1023 *
Lars Ellenbergc518d042010-09-01 09:50:23 +02001024 * Wait for a potentially exiting worker, then restart it,
1025 * or start a new one. Flush any pending work, there may still be an
1026 * after_state_change queued.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001027 */
Philipp Reisner0e29d162011-02-18 14:23:11 +01001028static void conn_reconfig_start(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001029{
Philipp Reisner0e29d162011-02-18 14:23:11 +01001030 wait_event(tconn->ping_wait, !test_and_set_bit(CONFIG_PENDING, &tconn->flags));
1031 wait_event(tconn->ping_wait, !test_bit(OBJECT_DYING, &tconn->flags));
1032 drbd_thread_start(&tconn->worker);
1033 conn_flush_workqueue(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001034}
1035
1036/* if still unconfigured, stops worker again.
1037 * if configured now, clears CONFIG_PENDING.
1038 * wakes potential waiters */
Philipp Reisner0e29d162011-02-18 14:23:11 +01001039static void conn_reconfig_done(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001040{
Philipp Reisner0e29d162011-02-18 14:23:11 +01001041 spin_lock_irq(&tconn->req_lock);
1042 if (conn_all_vols_unconf(tconn)) {
1043 set_bit(OBJECT_DYING, &tconn->flags);
1044 drbd_thread_stop_nowait(&tconn->worker);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001045 } else
Philipp Reisner0e29d162011-02-18 14:23:11 +01001046 clear_bit(CONFIG_PENDING, &tconn->flags);
1047 spin_unlock_irq(&tconn->req_lock);
1048 wake_up(&tconn->ping_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001049}
1050
Philipp Reisner07782862010-08-31 12:00:50 +02001051/* Make sure IO is suspended before calling this function(). */
1052static void drbd_suspend_al(struct drbd_conf *mdev)
1053{
1054 int s = 0;
1055
Lars Ellenberg61610422011-02-21 13:20:54 +01001056 if (!lc_try_lock(mdev->act_log)) {
Philipp Reisner07782862010-08-31 12:00:50 +02001057 dev_warn(DEV, "Failed to lock al in drbd_suspend_al()\n");
1058 return;
1059 }
1060
Lars Ellenberg61610422011-02-21 13:20:54 +01001061 drbd_al_shrink(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001062 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisner07782862010-08-31 12:00:50 +02001063 if (mdev->state.conn < C_CONNECTED)
1064 s = !test_and_set_bit(AL_SUSPENDED, &mdev->flags);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001065 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg61610422011-02-21 13:20:54 +01001066 lc_unlock(mdev->act_log);
Philipp Reisner07782862010-08-31 12:00:50 +02001067
1068 if (s)
1069 dev_info(DEV, "Suspended AL updates\n");
1070}
1071
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001072int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001073{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001074 struct drbd_conf *mdev;
1075 int err;
Andreas Gruenbacher116676c2010-12-08 13:33:11 +01001076 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001077 enum determine_dev_size dd;
1078 sector_t max_possible_sectors;
1079 sector_t min_md_device_sectors;
1080 struct drbd_backing_dev *nbc = NULL; /* new_backing_conf */
Tejun Heoe525fd82010-11-13 11:55:17 +01001081 struct block_device *bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001082 struct lru_cache *resync_lru = NULL;
1083 union drbd_state ns, os;
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +01001084 enum drbd_state_rv rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001085 int cp_discovered = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001086
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001087 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1088 if (!adm_ctx.reply_skb)
1089 return retcode;
1090 if (retcode != NO_ERROR)
Lars Ellenberg40cbf082011-03-16 16:52:10 +01001091 goto finish;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001092
1093 mdev = adm_ctx.mdev;
Philipp Reisner0e29d162011-02-18 14:23:11 +01001094 conn_reconfig_start(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001095
1096 /* if you want to reconfigure, please tear down first */
1097 if (mdev->state.disk > D_DISKLESS) {
1098 retcode = ERR_DISK_CONFIGURED;
1099 goto fail;
1100 }
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001101 /* It may just now have detached because of IO error. Make sure
1102 * drbd_ldev_destroy is done already, we may end up here very fast,
1103 * e.g. if someone calls attach from the on-io-error handler,
1104 * to realize a "hot spare" feature (not that I'd recommend that) */
1105 wait_event(mdev->misc_wait, !atomic_read(&mdev->local_cnt));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001106
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001107 /* allocation not in the IO path, drbdsetup context */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001108 nbc = kzalloc(sizeof(struct drbd_backing_dev), GFP_KERNEL);
1109 if (!nbc) {
1110 retcode = ERR_NOMEM;
1111 goto fail;
1112 }
1113
1114 nbc->dc.disk_size = DRBD_DISK_SIZE_SECT_DEF;
1115 nbc->dc.on_io_error = DRBD_ON_IO_ERROR_DEF;
1116 nbc->dc.fencing = DRBD_FENCING_DEF;
1117 nbc->dc.max_bio_bvecs = DRBD_MAX_BIO_BVECS_DEF;
1118
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001119 err = disk_conf_from_attrs(&nbc->dc, info->attrs);
1120 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001121 retcode = ERR_MANDATORY_TAG;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001122 drbd_msg_put_info(from_attrs_err_to_txt(err));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001123 goto fail;
1124 }
1125
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001126 if ((int)nbc->dc.meta_dev_idx < DRBD_MD_INDEX_FLEX_INT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001127 retcode = ERR_MD_IDX_INVALID;
1128 goto fail;
1129 }
1130
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +01001131 if (get_net_conf(mdev->tconn)) {
Philipp Reisner89e58e72011-01-19 13:12:45 +01001132 int prot = mdev->tconn->net_conf->wire_protocol;
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +01001133 put_net_conf(mdev->tconn);
Philipp Reisner47ff2d02010-06-18 13:56:57 +02001134 if (nbc->dc.fencing == FP_STONITH && prot == DRBD_PROT_A) {
1135 retcode = ERR_STONITH_AND_PROT_A;
1136 goto fail;
1137 }
1138 }
1139
Tejun Heod4d77622010-11-13 11:55:18 +01001140 bdev = blkdev_get_by_path(nbc->dc.backing_dev,
1141 FMODE_READ | FMODE_WRITE | FMODE_EXCL, mdev);
Tejun Heoe525fd82010-11-13 11:55:17 +01001142 if (IS_ERR(bdev)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001143 dev_err(DEV, "open(\"%s\") failed with %ld\n", nbc->dc.backing_dev,
Tejun Heoe525fd82010-11-13 11:55:17 +01001144 PTR_ERR(bdev));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001145 retcode = ERR_OPEN_DISK;
1146 goto fail;
1147 }
Tejun Heoe525fd82010-11-13 11:55:17 +01001148 nbc->backing_bdev = bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001149
Tejun Heoe525fd82010-11-13 11:55:17 +01001150 /*
1151 * meta_dev_idx >= 0: external fixed size, possibly multiple
1152 * drbd sharing one meta device. TODO in that case, paranoia
1153 * check that [md_bdev, meta_dev_idx] is not yet used by some
1154 * other drbd minor! (if you use drbd.conf + drbdadm, that
1155 * should check it for you already; but if you don't, or
1156 * someone fooled it, we need to double check here)
1157 */
Tejun Heod4d77622010-11-13 11:55:18 +01001158 bdev = blkdev_get_by_path(nbc->dc.meta_dev,
1159 FMODE_READ | FMODE_WRITE | FMODE_EXCL,
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001160 ((int)nbc->dc.meta_dev_idx < 0) ?
Tejun Heod4d77622010-11-13 11:55:18 +01001161 (void *)mdev : (void *)drbd_m_holder);
Tejun Heoe525fd82010-11-13 11:55:17 +01001162 if (IS_ERR(bdev)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001163 dev_err(DEV, "open(\"%s\") failed with %ld\n", nbc->dc.meta_dev,
Tejun Heoe525fd82010-11-13 11:55:17 +01001164 PTR_ERR(bdev));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001165 retcode = ERR_OPEN_MD_DISK;
1166 goto fail;
1167 }
Tejun Heoe525fd82010-11-13 11:55:17 +01001168 nbc->md_bdev = bdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001169
Tejun Heoe525fd82010-11-13 11:55:17 +01001170 if ((nbc->backing_bdev == nbc->md_bdev) !=
1171 (nbc->dc.meta_dev_idx == DRBD_MD_INDEX_INTERNAL ||
1172 nbc->dc.meta_dev_idx == DRBD_MD_INDEX_FLEX_INT)) {
1173 retcode = ERR_MD_IDX_INVALID;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001174 goto fail;
1175 }
1176
1177 resync_lru = lc_create("resync", drbd_bm_ext_cache,
Lars Ellenberg46a15bc2011-02-21 13:21:01 +01001178 1, 61, sizeof(struct bm_extent),
Philipp Reisnerb411b362009-09-25 16:07:19 -07001179 offsetof(struct bm_extent, lce));
1180 if (!resync_lru) {
1181 retcode = ERR_NOMEM;
Tejun Heoe525fd82010-11-13 11:55:17 +01001182 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001183 }
1184
1185 /* RT - for drbd_get_max_capacity() DRBD_MD_INDEX_FLEX_INT */
1186 drbd_md_set_sector_offsets(mdev, nbc);
1187
1188 if (drbd_get_max_capacity(nbc) < nbc->dc.disk_size) {
1189 dev_err(DEV, "max capacity %llu smaller than disk size %llu\n",
1190 (unsigned long long) drbd_get_max_capacity(nbc),
1191 (unsigned long long) nbc->dc.disk_size);
1192 retcode = ERR_DISK_TO_SMALL;
Tejun Heoe525fd82010-11-13 11:55:17 +01001193 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001194 }
1195
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001196 if ((int)nbc->dc.meta_dev_idx < 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001197 max_possible_sectors = DRBD_MAX_SECTORS_FLEX;
1198 /* at least one MB, otherwise it does not make sense */
1199 min_md_device_sectors = (2<<10);
1200 } else {
1201 max_possible_sectors = DRBD_MAX_SECTORS;
1202 min_md_device_sectors = MD_RESERVED_SECT * (nbc->dc.meta_dev_idx + 1);
1203 }
1204
Philipp Reisnerb411b362009-09-25 16:07:19 -07001205 if (drbd_get_capacity(nbc->md_bdev) < min_md_device_sectors) {
1206 retcode = ERR_MD_DISK_TO_SMALL;
1207 dev_warn(DEV, "refusing attach: md-device too small, "
1208 "at least %llu sectors needed for this meta-disk type\n",
1209 (unsigned long long) min_md_device_sectors);
Tejun Heoe525fd82010-11-13 11:55:17 +01001210 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001211 }
1212
1213 /* Make sure the new disk is big enough
1214 * (we may currently be R_PRIMARY with no local disk...) */
1215 if (drbd_get_max_capacity(nbc) <
1216 drbd_get_capacity(mdev->this_bdev)) {
1217 retcode = ERR_DISK_TO_SMALL;
Tejun Heoe525fd82010-11-13 11:55:17 +01001218 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001219 }
1220
1221 nbc->known_size = drbd_get_capacity(nbc->backing_bdev);
1222
Lars Ellenberg13529942009-10-12 19:07:49 +02001223 if (nbc->known_size > max_possible_sectors) {
1224 dev_warn(DEV, "==> truncating very big lower level device "
1225 "to currently maximum possible %llu sectors <==\n",
1226 (unsigned long long) max_possible_sectors);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001227 if ((int)nbc->dc.meta_dev_idx >= 0)
Lars Ellenberg13529942009-10-12 19:07:49 +02001228 dev_warn(DEV, "==>> using internal or flexible "
1229 "meta data may help <<==\n");
1230 }
1231
Philipp Reisnerb411b362009-09-25 16:07:19 -07001232 drbd_suspend_io(mdev);
1233 /* also wait for the last barrier ack. */
Philipp Reisnerfb22c402010-09-08 23:20:21 +02001234 wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_pending_cnt) || is_susp(mdev->state));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001235 /* and for any other previously queued work */
Philipp Reisnera21e9292011-02-08 15:08:49 +01001236 drbd_flush_workqueue(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001237
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +01001238 rv = _drbd_request_state(mdev, NS(disk, D_ATTACHING), CS_VERBOSE);
1239 retcode = rv; /* FIXME: Type mismatch. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001240 drbd_resume_io(mdev);
Andreas Gruenbacherf2024e72010-12-10 13:44:05 +01001241 if (rv < SS_SUCCESS)
Tejun Heoe525fd82010-11-13 11:55:17 +01001242 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001243
1244 if (!get_ldev_if_state(mdev, D_ATTACHING))
1245 goto force_diskless;
1246
1247 drbd_md_set_sector_offsets(mdev, nbc);
1248
1249 if (!mdev->bitmap) {
1250 if (drbd_bm_init(mdev)) {
1251 retcode = ERR_NOMEM;
1252 goto force_diskless_dec;
1253 }
1254 }
1255
1256 retcode = drbd_md_read(mdev, nbc);
1257 if (retcode != NO_ERROR)
1258 goto force_diskless_dec;
1259
1260 if (mdev->state.conn < C_CONNECTED &&
1261 mdev->state.role == R_PRIMARY &&
1262 (mdev->ed_uuid & ~((u64)1)) != (nbc->md.uuid[UI_CURRENT] & ~((u64)1))) {
1263 dev_err(DEV, "Can only attach to data with current UUID=%016llX\n",
1264 (unsigned long long)mdev->ed_uuid);
1265 retcode = ERR_DATA_NOT_CURRENT;
1266 goto force_diskless_dec;
1267 }
1268
1269 /* Since we are diskless, fix the activity log first... */
1270 if (drbd_check_al_size(mdev)) {
1271 retcode = ERR_NOMEM;
1272 goto force_diskless_dec;
1273 }
1274
1275 /* Prevent shrinking of consistent devices ! */
1276 if (drbd_md_test_flag(nbc, MDF_CONSISTENT) &&
Philipp Reisnera393db62009-12-22 13:35:52 +01001277 drbd_new_dev_size(mdev, nbc, 0) < nbc->md.la_size_sect) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001278 dev_warn(DEV, "refusing to truncate a consistent device\n");
1279 retcode = ERR_DISK_TO_SMALL;
1280 goto force_diskless_dec;
1281 }
1282
1283 if (!drbd_al_read_log(mdev, nbc)) {
1284 retcode = ERR_IO_MD_DISK;
1285 goto force_diskless_dec;
1286 }
1287
Philipp Reisnerb411b362009-09-25 16:07:19 -07001288 /* Reset the "barriers don't work" bits here, then force meta data to
1289 * be written, to ensure we determine if barriers are supported. */
1290 if (nbc->dc.no_md_flush)
Philipp Reisnera8a4e512010-08-25 10:21:04 +02001291 set_bit(MD_NO_FUA, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001292 else
Philipp Reisnera8a4e512010-08-25 10:21:04 +02001293 clear_bit(MD_NO_FUA, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001294
1295 /* Point of no return reached.
1296 * Devices and memory are no longer released by error cleanup below.
1297 * now mdev takes over responsibility, and the state engine should
1298 * clean it up somewhere. */
1299 D_ASSERT(mdev->ldev == NULL);
1300 mdev->ldev = nbc;
1301 mdev->resync = resync_lru;
1302 nbc = NULL;
1303 resync_lru = NULL;
1304
Philipp Reisner2451fc32010-08-24 13:43:11 +02001305 mdev->write_ordering = WO_bdev_flush;
1306 drbd_bump_write_ordering(mdev, WO_bdev_flush);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001307
1308 if (drbd_md_test_flag(mdev->ldev, MDF_CRASHED_PRIMARY))
1309 set_bit(CRASHED_PRIMARY, &mdev->flags);
1310 else
1311 clear_bit(CRASHED_PRIMARY, &mdev->flags);
1312
Philipp Reisner894c6a92010-06-18 16:03:20 +02001313 if (drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) &&
Philipp Reisnerfb22c402010-09-08 23:20:21 +02001314 !(mdev->state.role == R_PRIMARY && mdev->state.susp_nod)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001315 set_bit(CRASHED_PRIMARY, &mdev->flags);
1316 cp_discovered = 1;
1317 }
1318
1319 mdev->send_cnt = 0;
1320 mdev->recv_cnt = 0;
1321 mdev->read_cnt = 0;
1322 mdev->writ_cnt = 0;
1323
Philipp Reisner99432fc2011-05-20 16:39:13 +02001324 drbd_reconsider_max_bio_size(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001325
1326 /* If I am currently not R_PRIMARY,
1327 * but meta data primary indicator is set,
1328 * I just now recover from a hard crash,
1329 * and have been R_PRIMARY before that crash.
1330 *
1331 * Now, if I had no connection before that crash
1332 * (have been degraded R_PRIMARY), chances are that
1333 * I won't find my peer now either.
1334 *
1335 * In that case, and _only_ in that case,
1336 * we use the degr-wfc-timeout instead of the default,
1337 * so we can automatically recover from a crash of a
1338 * degraded but active "cluster" after a certain timeout.
1339 */
1340 clear_bit(USE_DEGR_WFC_T, &mdev->flags);
1341 if (mdev->state.role != R_PRIMARY &&
1342 drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) &&
1343 !drbd_md_test_flag(mdev->ldev, MDF_CONNECTED_IND))
1344 set_bit(USE_DEGR_WFC_T, &mdev->flags);
1345
Bart Van Assche24c48302011-05-21 18:32:29 +02001346 dd = drbd_determine_dev_size(mdev, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001347 if (dd == dev_size_error) {
1348 retcode = ERR_NOMEM_BITMAP;
1349 goto force_diskless_dec;
1350 } else if (dd == grew)
1351 set_bit(RESYNC_AFTER_NEG, &mdev->flags);
1352
1353 if (drbd_md_test_flag(mdev->ldev, MDF_FULL_SYNC)) {
1354 dev_info(DEV, "Assuming that all blocks are out of sync "
1355 "(aka FullSync)\n");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001356 if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write,
1357 "set_n_write from attaching", BM_LOCKED_MASK)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001358 retcode = ERR_IO_MD_DISK;
1359 goto force_diskless_dec;
1360 }
1361 } else {
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001362 if (drbd_bitmap_io(mdev, &drbd_bm_read,
1363 "read from attaching", BM_LOCKED_MASK) < 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001364 retcode = ERR_IO_MD_DISK;
1365 goto force_diskless_dec;
1366 }
1367 }
1368
1369 if (cp_discovered) {
1370 drbd_al_apply_to_bm(mdev);
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001371 if (drbd_bitmap_io(mdev, &drbd_bm_write,
1372 "crashed primary apply AL", BM_LOCKED_MASK)) {
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001373 retcode = ERR_IO_MD_DISK;
1374 goto force_diskless_dec;
1375 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001376 }
1377
Philipp Reisner07782862010-08-31 12:00:50 +02001378 if (_drbd_bm_total_weight(mdev) == drbd_bm_bits(mdev))
1379 drbd_suspend_al(mdev); /* IO is still suspended here... */
1380
Philipp Reisner87eeee42011-01-19 14:16:30 +01001381 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001382 os = mdev->state;
1383 ns.i = os.i;
1384 /* If MDF_CONSISTENT is not set go into inconsistent state,
1385 otherwise investigate MDF_WasUpToDate...
1386 If MDF_WAS_UP_TO_DATE is not set go into D_OUTDATED disk state,
1387 otherwise into D_CONSISTENT state.
1388 */
1389 if (drbd_md_test_flag(mdev->ldev, MDF_CONSISTENT)) {
1390 if (drbd_md_test_flag(mdev->ldev, MDF_WAS_UP_TO_DATE))
1391 ns.disk = D_CONSISTENT;
1392 else
1393 ns.disk = D_OUTDATED;
1394 } else {
1395 ns.disk = D_INCONSISTENT;
1396 }
1397
1398 if (drbd_md_test_flag(mdev->ldev, MDF_PEER_OUT_DATED))
1399 ns.pdsk = D_OUTDATED;
1400
1401 if ( ns.disk == D_CONSISTENT &&
1402 (ns.pdsk == D_OUTDATED || mdev->ldev->dc.fencing == FP_DONT_CARE))
1403 ns.disk = D_UP_TO_DATE;
1404
1405 /* All tests on MDF_PRIMARY_IND, MDF_CONNECTED_IND,
1406 MDF_CONSISTENT and MDF_WAS_UP_TO_DATE must happen before
1407 this point, because drbd_request_state() modifies these
1408 flags. */
1409
1410 /* In case we are C_CONNECTED postpone any decision on the new disk
1411 state after the negotiation phase. */
1412 if (mdev->state.conn == C_CONNECTED) {
1413 mdev->new_state_tmp.i = ns.i;
1414 ns.i = os.i;
1415 ns.disk = D_NEGOTIATING;
Philipp Reisnerdc66c742010-06-02 14:31:29 +02001416
1417 /* We expect to receive up-to-date UUIDs soon.
1418 To avoid a race in receive_state, free p_uuid while
1419 holding req_lock. I.e. atomic with the state change */
1420 kfree(mdev->p_uuid);
1421 mdev->p_uuid = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001422 }
1423
1424 rv = _drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
1425 ns = mdev->state;
Philipp Reisner87eeee42011-01-19 14:16:30 +01001426 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001427
1428 if (rv < SS_SUCCESS)
1429 goto force_diskless_dec;
1430
1431 if (mdev->state.role == R_PRIMARY)
1432 mdev->ldev->md.uuid[UI_CURRENT] |= (u64)1;
1433 else
1434 mdev->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
1435
1436 drbd_md_mark_dirty(mdev);
1437 drbd_md_sync(mdev);
1438
1439 kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
1440 put_ldev(mdev);
Philipp Reisner0e29d162011-02-18 14:23:11 +01001441 conn_reconfig_done(mdev->tconn);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001442 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001443 return 0;
1444
1445 force_diskless_dec:
1446 put_ldev(mdev);
1447 force_diskless:
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001448 drbd_force_state(mdev, NS(disk, D_FAILED));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001449 drbd_md_sync(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001450 fail:
Lars Ellenberg40cbf082011-03-16 16:52:10 +01001451 conn_reconfig_done(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001452 if (nbc) {
Tejun Heoe525fd82010-11-13 11:55:17 +01001453 if (nbc->backing_bdev)
1454 blkdev_put(nbc->backing_bdev,
1455 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1456 if (nbc->md_bdev)
1457 blkdev_put(nbc->md_bdev,
1458 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001459 kfree(nbc);
1460 }
1461 lc_destroy(resync_lru);
1462
Lars Ellenberg40cbf082011-03-16 16:52:10 +01001463 finish:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001464 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001465 return 0;
1466}
1467
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01001468static int adm_detach(struct drbd_conf *mdev)
1469{
1470 enum drbd_ret_code retcode;
1471 drbd_suspend_io(mdev); /* so no-one is stuck in drbd_al_begin_io */
1472 retcode = drbd_request_state(mdev, NS(disk, D_DISKLESS));
1473 wait_event(mdev->misc_wait,
1474 mdev->state.disk != D_DISKLESS ||
1475 !atomic_read(&mdev->local_cnt));
1476 drbd_resume_io(mdev);
1477 return retcode;
1478}
1479
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02001480/* Detaching the disk is a process in multiple stages. First we need to lock
1481 * out application IO, in-flight IO, IO stuck in drbd_al_begin_io.
1482 * Then we transition to D_DISKLESS, and wait for put_ldev() to return all
1483 * internal references as well.
1484 * Only then we have finally detached. */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001485int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001486{
Lars Ellenberg9a0d9d02011-05-02 11:51:31 +02001487 enum drbd_ret_code retcode;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001488
1489 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1490 if (!adm_ctx.reply_skb)
1491 return retcode;
1492 if (retcode != NO_ERROR)
1493 goto out;
1494
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01001495 retcode = adm_detach(adm_ctx.mdev);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001496out:
1497 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001498 return 0;
1499}
1500
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001501int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001502{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001503 char hmac_name[CRYPTO_MAX_ALG_NAME];
1504 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001505 struct net_conf *new_conf = NULL;
1506 struct crypto_hash *tfm = NULL;
1507 struct crypto_hash *integrity_w_tfm = NULL;
1508 struct crypto_hash *integrity_r_tfm = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001509 void *int_dig_out = NULL;
1510 void *int_dig_in = NULL;
1511 void *int_dig_vv = NULL;
Philipp Reisner80883192011-02-18 14:56:45 +01001512 struct drbd_tconn *oconn;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001513 struct drbd_tconn *tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001514 struct sockaddr *new_my_addr, *new_peer_addr, *taken_addr;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001515 enum drbd_ret_code retcode;
1516 int i;
1517 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001518
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001519 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONN);
1520 if (!adm_ctx.reply_skb)
1521 return retcode;
1522 if (retcode != NO_ERROR)
1523 goto out;
1524
1525 tconn = adm_ctx.tconn;
Philipp Reisner80883192011-02-18 14:56:45 +01001526 conn_reconfig_start(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001527
Philipp Reisner80883192011-02-18 14:56:45 +01001528 if (tconn->cstate > C_STANDALONE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001529 retcode = ERR_NET_CONFIGURED;
1530 goto fail;
1531 }
1532
1533 /* allocation not in the IO path, cqueue thread context */
Julia Lawall2db4e422010-05-13 22:02:21 +02001534 new_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001535 if (!new_conf) {
1536 retcode = ERR_NOMEM;
1537 goto fail;
1538 }
1539
Philipp Reisnerb411b362009-09-25 16:07:19 -07001540 new_conf->timeout = DRBD_TIMEOUT_DEF;
1541 new_conf->try_connect_int = DRBD_CONNECT_INT_DEF;
1542 new_conf->ping_int = DRBD_PING_INT_DEF;
1543 new_conf->max_epoch_size = DRBD_MAX_EPOCH_SIZE_DEF;
1544 new_conf->max_buffers = DRBD_MAX_BUFFERS_DEF;
1545 new_conf->unplug_watermark = DRBD_UNPLUG_WATERMARK_DEF;
1546 new_conf->sndbuf_size = DRBD_SNDBUF_SIZE_DEF;
1547 new_conf->rcvbuf_size = DRBD_RCVBUF_SIZE_DEF;
1548 new_conf->ko_count = DRBD_KO_COUNT_DEF;
1549 new_conf->after_sb_0p = DRBD_AFTER_SB_0P_DEF;
1550 new_conf->after_sb_1p = DRBD_AFTER_SB_1P_DEF;
1551 new_conf->after_sb_2p = DRBD_AFTER_SB_2P_DEF;
1552 new_conf->want_lose = 0;
1553 new_conf->two_primaries = 0;
1554 new_conf->wire_protocol = DRBD_PROT_C;
1555 new_conf->ping_timeo = DRBD_PING_TIMEO_DEF;
1556 new_conf->rr_conflict = DRBD_RR_CONFLICT_DEF;
Philipp Reisner422028b2010-10-27 11:12:07 +02001557 new_conf->on_congestion = DRBD_ON_CONGESTION_DEF;
1558 new_conf->cong_extents = DRBD_CONG_EXTENTS_DEF;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001559
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001560 err = net_conf_from_attrs(new_conf, info->attrs);
1561 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001562 retcode = ERR_MANDATORY_TAG;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001563 drbd_msg_put_info(from_attrs_err_to_txt(err));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001564 goto fail;
1565 }
1566
1567 if (new_conf->two_primaries
1568 && (new_conf->wire_protocol != DRBD_PROT_C)) {
1569 retcode = ERR_NOT_PROTO_C;
1570 goto fail;
Philipp Reisner47ff2d02010-06-18 13:56:57 +02001571 }
1572
Philipp Reisner80883192011-02-18 14:56:45 +01001573 idr_for_each_entry(&tconn->volumes, mdev, i) {
1574 if (get_ldev(mdev)) {
1575 enum drbd_fencing_p fp = mdev->ldev->dc.fencing;
1576 put_ldev(mdev);
1577 if (new_conf->wire_protocol == DRBD_PROT_A && fp == FP_STONITH) {
1578 retcode = ERR_STONITH_AND_PROT_A;
1579 goto fail;
1580 }
1581 }
1582 if (mdev->state.role == R_PRIMARY && new_conf->want_lose) {
1583 retcode = ERR_DISCARD;
Philipp Reisner47ff2d02010-06-18 13:56:57 +02001584 goto fail;
1585 }
Philipp Reisner80883192011-02-18 14:56:45 +01001586 if (!mdev->bitmap) {
1587 if(drbd_bm_init(mdev)) {
1588 retcode = ERR_NOMEM;
1589 goto fail;
1590 }
1591 }
Philipp Reisner47ff2d02010-06-18 13:56:57 +02001592 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001593
Philipp Reisner422028b2010-10-27 11:12:07 +02001594 if (new_conf->on_congestion != OC_BLOCK && new_conf->wire_protocol != DRBD_PROT_A) {
1595 retcode = ERR_CONG_NOT_PROTO_A;
1596 goto fail;
1597 }
1598
Philipp Reisnerb411b362009-09-25 16:07:19 -07001599 retcode = NO_ERROR;
1600
1601 new_my_addr = (struct sockaddr *)&new_conf->my_addr;
1602 new_peer_addr = (struct sockaddr *)&new_conf->peer_addr;
Lars Ellenberg543cc102011-03-10 22:18:18 +01001603
1604 /* No need to take drbd_cfg_mutex here. All reconfiguration is
1605 * strictly serialized on genl_lock(). We are protected against
1606 * concurrent reconfiguration/addition/deletion */
Philipp Reisner80883192011-02-18 14:56:45 +01001607 list_for_each_entry(oconn, &drbd_tconns, all_tconn) {
1608 if (oconn == tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001609 continue;
Philipp Reisner80883192011-02-18 14:56:45 +01001610 if (get_net_conf(oconn)) {
1611 taken_addr = (struct sockaddr *)&oconn->net_conf->my_addr;
1612 if (new_conf->my_addr_len == oconn->net_conf->my_addr_len &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07001613 !memcmp(new_my_addr, taken_addr, new_conf->my_addr_len))
1614 retcode = ERR_LOCAL_ADDR;
1615
Philipp Reisner80883192011-02-18 14:56:45 +01001616 taken_addr = (struct sockaddr *)&oconn->net_conf->peer_addr;
1617 if (new_conf->peer_addr_len == oconn->net_conf->peer_addr_len &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07001618 !memcmp(new_peer_addr, taken_addr, new_conf->peer_addr_len))
1619 retcode = ERR_PEER_ADDR;
1620
Philipp Reisner80883192011-02-18 14:56:45 +01001621 put_net_conf(oconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001622 if (retcode != NO_ERROR)
1623 goto fail;
1624 }
1625 }
1626
1627 if (new_conf->cram_hmac_alg[0] != 0) {
1628 snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)",
1629 new_conf->cram_hmac_alg);
1630 tfm = crypto_alloc_hash(hmac_name, 0, CRYPTO_ALG_ASYNC);
1631 if (IS_ERR(tfm)) {
1632 tfm = NULL;
1633 retcode = ERR_AUTH_ALG;
1634 goto fail;
1635 }
1636
Philipp Reisner07982192009-12-28 16:58:38 +01001637 if (!drbd_crypto_is_hash(crypto_hash_tfm(tfm))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001638 retcode = ERR_AUTH_ALG_ND;
1639 goto fail;
1640 }
1641 }
1642
1643 if (new_conf->integrity_alg[0]) {
1644 integrity_w_tfm = crypto_alloc_hash(new_conf->integrity_alg, 0, CRYPTO_ALG_ASYNC);
1645 if (IS_ERR(integrity_w_tfm)) {
1646 integrity_w_tfm = NULL;
1647 retcode=ERR_INTEGRITY_ALG;
1648 goto fail;
1649 }
1650
1651 if (!drbd_crypto_is_hash(crypto_hash_tfm(integrity_w_tfm))) {
1652 retcode=ERR_INTEGRITY_ALG_ND;
1653 goto fail;
1654 }
1655
1656 integrity_r_tfm = crypto_alloc_hash(new_conf->integrity_alg, 0, CRYPTO_ALG_ASYNC);
1657 if (IS_ERR(integrity_r_tfm)) {
1658 integrity_r_tfm = NULL;
1659 retcode=ERR_INTEGRITY_ALG;
1660 goto fail;
1661 }
1662 }
1663
Philipp Reisnerb411b362009-09-25 16:07:19 -07001664 ((char *)new_conf->shared_secret)[SHARED_SECRET_MAX-1] = 0;
1665
Philipp Reisner80883192011-02-18 14:56:45 +01001666 /* allocation not in the IO path, cqueue thread context */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001667 if (integrity_w_tfm) {
1668 i = crypto_hash_digestsize(integrity_w_tfm);
1669 int_dig_out = kmalloc(i, GFP_KERNEL);
1670 if (!int_dig_out) {
1671 retcode = ERR_NOMEM;
1672 goto fail;
1673 }
1674 int_dig_in = kmalloc(i, GFP_KERNEL);
1675 if (!int_dig_in) {
1676 retcode = ERR_NOMEM;
1677 goto fail;
1678 }
1679 int_dig_vv = kmalloc(i, GFP_KERNEL);
1680 if (!int_dig_vv) {
1681 retcode = ERR_NOMEM;
1682 goto fail;
1683 }
1684 }
1685
Philipp Reisner80883192011-02-18 14:56:45 +01001686 conn_flush_workqueue(tconn);
1687 spin_lock_irq(&tconn->req_lock);
1688 if (tconn->net_conf != NULL) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001689 retcode = ERR_NET_CONFIGURED;
Philipp Reisner80883192011-02-18 14:56:45 +01001690 spin_unlock_irq(&tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001691 goto fail;
1692 }
Philipp Reisner80883192011-02-18 14:56:45 +01001693 tconn->net_conf = new_conf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001694
Philipp Reisner80883192011-02-18 14:56:45 +01001695 crypto_free_hash(tconn->cram_hmac_tfm);
1696 tconn->cram_hmac_tfm = tfm;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001697
Philipp Reisner80883192011-02-18 14:56:45 +01001698 crypto_free_hash(tconn->integrity_w_tfm);
1699 tconn->integrity_w_tfm = integrity_w_tfm;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001700
Philipp Reisner80883192011-02-18 14:56:45 +01001701 crypto_free_hash(tconn->integrity_r_tfm);
1702 tconn->integrity_r_tfm = integrity_r_tfm;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001703
Philipp Reisner80883192011-02-18 14:56:45 +01001704 kfree(tconn->int_dig_out);
1705 kfree(tconn->int_dig_in);
1706 kfree(tconn->int_dig_vv);
1707 tconn->int_dig_out=int_dig_out;
1708 tconn->int_dig_in=int_dig_in;
1709 tconn->int_dig_vv=int_dig_vv;
1710 retcode = _conn_request_state(tconn, NS(conn, C_UNCONNECTED), CS_VERBOSE);
1711 spin_unlock_irq(&tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001712
Philipp Reisner80883192011-02-18 14:56:45 +01001713 idr_for_each_entry(&tconn->volumes, mdev, i) {
1714 mdev->send_cnt = 0;
1715 mdev->recv_cnt = 0;
1716 kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
1717 }
Philipp Reisner80883192011-02-18 14:56:45 +01001718 conn_reconfig_done(tconn);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001719 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001720 return 0;
1721
1722fail:
1723 kfree(int_dig_out);
1724 kfree(int_dig_in);
1725 kfree(int_dig_vv);
1726 crypto_free_hash(tfm);
1727 crypto_free_hash(integrity_w_tfm);
1728 crypto_free_hash(integrity_r_tfm);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001729 kfree(new_conf);
1730
Philipp Reisner80883192011-02-18 14:56:45 +01001731 conn_reconfig_done(tconn);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001732out:
1733 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001734 return 0;
1735}
1736
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01001737static enum drbd_state_rv conn_try_disconnect(struct drbd_tconn *tconn, bool force)
1738{
1739 enum drbd_state_rv rv;
1740 if (force) {
1741 spin_lock_irq(&tconn->req_lock);
1742 if (tconn->cstate >= C_WF_CONNECTION)
1743 _conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
1744 spin_unlock_irq(&tconn->req_lock);
1745 return SS_SUCCESS;
1746 }
1747
1748 rv = conn_request_state(tconn, NS(conn, C_DISCONNECTING), 0);
1749
1750 switch (rv) {
1751 case SS_NOTHING_TO_DO:
1752 case SS_ALREADY_STANDALONE:
1753 return SS_SUCCESS;
1754 case SS_PRIMARY_NOP:
1755 /* Our state checking code wants to see the peer outdated. */
1756 rv = conn_request_state(tconn, NS2(conn, C_DISCONNECTING,
1757 pdsk, D_OUTDATED), CS_VERBOSE);
1758 break;
1759 case SS_CW_FAILED_BY_PEER:
1760 /* The peer probably wants to see us outdated. */
1761 rv = conn_request_state(tconn, NS2(conn, C_DISCONNECTING,
1762 disk, D_OUTDATED), 0);
1763 if (rv == SS_IS_DISKLESS || rv == SS_LOWER_THAN_OUTDATED) {
1764 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
1765 rv = SS_SUCCESS;
1766 }
1767 break;
1768 default:;
1769 /* no special handling necessary */
1770 }
1771
1772 return rv;
1773}
1774
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001775int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001776{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001777 struct disconnect_parms parms;
1778 struct drbd_tconn *tconn;
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01001779 enum drbd_state_rv rv;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001780 enum drbd_ret_code retcode;
1781 int err;
Philipp Reisner2561b9c2010-12-03 15:22:48 +01001782
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001783 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONN);
1784 if (!adm_ctx.reply_skb)
1785 return retcode;
1786 if (retcode != NO_ERROR)
Philipp Reisner2561b9c2010-12-03 15:22:48 +01001787 goto fail;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001788
1789 tconn = adm_ctx.tconn;
1790 memset(&parms, 0, sizeof(parms));
1791 if (info->attrs[DRBD_NLA_DISCONNECT_PARMS]) {
1792 err = disconnect_parms_from_attrs(&parms, info->attrs);
1793 if (err) {
1794 retcode = ERR_MANDATORY_TAG;
1795 drbd_msg_put_info(from_attrs_err_to_txt(err));
1796 goto fail;
1797 }
Philipp Reisner2561b9c2010-12-03 15:22:48 +01001798 }
1799
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01001800 rv = conn_try_disconnect(tconn, parms.force_disconnect);
1801 if (rv < SS_SUCCESS)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001802 goto fail;
1803
Philipp Reisnerdf24aa42011-02-15 11:14:44 +01001804 if (wait_event_interruptible(tconn->ping_wait,
1805 tconn->cstate != C_DISCONNECTING)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001806 /* Do not test for mdev->state.conn == C_STANDALONE, since
1807 someone else might connect us in the mean time! */
1808 retcode = ERR_INTR;
1809 goto fail;
1810 }
1811
Philipp Reisnerb411b362009-09-25 16:07:19 -07001812 retcode = NO_ERROR;
1813 fail:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001814 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001815 return 0;
1816}
1817
1818void resync_after_online_grow(struct drbd_conf *mdev)
1819{
1820 int iass; /* I am sync source */
1821
1822 dev_info(DEV, "Resync of new storage after online grow\n");
1823 if (mdev->state.role != mdev->state.peer)
1824 iass = (mdev->state.role == R_PRIMARY);
1825 else
Philipp Reisner25703f82011-02-07 14:35:25 +01001826 iass = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001827
1828 if (iass)
1829 drbd_start_resync(mdev, C_SYNC_SOURCE);
1830 else
1831 _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE + CS_SERIALIZE);
1832}
1833
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001834int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001835{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001836 struct resize_parms rs;
1837 struct drbd_conf *mdev;
1838 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001839 enum determine_dev_size dd;
Philipp Reisner6495d2c2010-03-24 16:07:04 +01001840 enum dds_flags ddsf;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001841 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001842
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001843 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1844 if (!adm_ctx.reply_skb)
1845 return retcode;
1846 if (retcode != NO_ERROR)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001847 goto fail;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001848
1849 memset(&rs, 0, sizeof(struct resize_parms));
1850 if (info->attrs[DRBD_NLA_RESIZE_PARMS]) {
1851 err = resize_parms_from_attrs(&rs, info->attrs);
1852 if (err) {
1853 retcode = ERR_MANDATORY_TAG;
1854 drbd_msg_put_info(from_attrs_err_to_txt(err));
1855 goto fail;
1856 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001857 }
1858
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001859 mdev = adm_ctx.mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001860 if (mdev->state.conn > C_CONNECTED) {
1861 retcode = ERR_RESIZE_RESYNC;
1862 goto fail;
1863 }
1864
1865 if (mdev->state.role == R_SECONDARY &&
1866 mdev->state.peer == R_SECONDARY) {
1867 retcode = ERR_NO_PRIMARY;
1868 goto fail;
1869 }
1870
1871 if (!get_ldev(mdev)) {
1872 retcode = ERR_NO_DISK;
1873 goto fail;
1874 }
1875
Philipp Reisner31890f42011-01-19 14:12:51 +01001876 if (rs.no_resync && mdev->tconn->agreed_pro_version < 93) {
Philipp Reisner6495d2c2010-03-24 16:07:04 +01001877 retcode = ERR_NEED_APV_93;
1878 goto fail;
1879 }
1880
Philipp Reisner087c2492010-03-26 13:49:56 +01001881 if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001882 mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001883
1884 mdev->ldev->dc.disk_size = (sector_t)rs.resize_size;
Philipp Reisner6495d2c2010-03-24 16:07:04 +01001885 ddsf = (rs.resize_force ? DDSF_FORCED : 0) | (rs.no_resync ? DDSF_NO_RESYNC : 0);
Bart Van Assche24c48302011-05-21 18:32:29 +02001886 dd = drbd_determine_dev_size(mdev, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001887 drbd_md_sync(mdev);
1888 put_ldev(mdev);
1889 if (dd == dev_size_error) {
1890 retcode = ERR_NOMEM_BITMAP;
1891 goto fail;
1892 }
1893
Philipp Reisner087c2492010-03-26 13:49:56 +01001894 if (mdev->state.conn == C_CONNECTED) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001895 if (dd == grew)
1896 set_bit(RESIZE_PENDING, &mdev->flags);
1897
1898 drbd_send_uuids(mdev);
Philipp Reisner6495d2c2010-03-24 16:07:04 +01001899 drbd_send_sizes(mdev, 1, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001900 }
1901
1902 fail:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001903 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001904 return 0;
1905}
1906
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001907int drbd_adm_syncer(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001908{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001909 struct drbd_conf *mdev;
1910 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001911 int err;
1912 int ovr; /* online verify running */
1913 int rsr; /* re-sync running */
1914 struct crypto_hash *verify_tfm = NULL;
1915 struct crypto_hash *csums_tfm = NULL;
1916 struct syncer_conf sc;
1917 cpumask_var_t new_cpu_mask;
Philipp Reisner778f2712010-07-06 11:14:00 +02001918 int *rs_plan_s = NULL;
1919 int fifo_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001920
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001921 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1922 if (!adm_ctx.reply_skb)
1923 return retcode;
1924 if (retcode != NO_ERROR)
1925 goto fail;
1926 mdev = adm_ctx.mdev;
1927
Philipp Reisnerb411b362009-09-25 16:07:19 -07001928 if (!zalloc_cpumask_var(&new_cpu_mask, GFP_KERNEL)) {
1929 retcode = ERR_NOMEM;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001930 drbd_msg_put_info("unable to allocate cpumask");
Philipp Reisnerb411b362009-09-25 16:07:19 -07001931 goto fail;
1932 }
1933
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001934 if (((struct drbd_genlmsghdr*)info->userhdr)->flags
1935 & DRBD_GENL_F_SET_DEFAULTS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001936 memset(&sc, 0, sizeof(struct syncer_conf));
1937 sc.rate = DRBD_RATE_DEF;
1938 sc.after = DRBD_AFTER_DEF;
1939 sc.al_extents = DRBD_AL_EXTENTS_DEF;
Philipp Reisner265be2d2010-05-31 10:14:17 +02001940 sc.on_no_data = DRBD_ON_NO_DATA_DEF;
Philipp Reisner9a31d712010-07-05 13:42:03 +02001941 sc.c_plan_ahead = DRBD_C_PLAN_AHEAD_DEF;
1942 sc.c_delay_target = DRBD_C_DELAY_TARGET_DEF;
1943 sc.c_fill_target = DRBD_C_FILL_TARGET_DEF;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001944 sc.c_max_rate = DRBD_C_MAX_RATE_DEF;
1945 sc.c_min_rate = DRBD_C_MIN_RATE_DEF;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001946 } else
1947 memcpy(&sc, &mdev->sync_conf, sizeof(struct syncer_conf));
1948
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001949 err = syncer_conf_from_attrs(&sc, info->attrs);
1950 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001951 retcode = ERR_MANDATORY_TAG;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01001952 drbd_msg_put_info(from_attrs_err_to_txt(err));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001953 goto fail;
1954 }
1955
1956 /* re-sync running */
1957 rsr = ( mdev->state.conn == C_SYNC_SOURCE ||
1958 mdev->state.conn == C_SYNC_TARGET ||
1959 mdev->state.conn == C_PAUSED_SYNC_S ||
1960 mdev->state.conn == C_PAUSED_SYNC_T );
1961
1962 if (rsr && strcmp(sc.csums_alg, mdev->sync_conf.csums_alg)) {
1963 retcode = ERR_CSUMS_RESYNC_RUNNING;
1964 goto fail;
1965 }
1966
1967 if (!rsr && sc.csums_alg[0]) {
1968 csums_tfm = crypto_alloc_hash(sc.csums_alg, 0, CRYPTO_ALG_ASYNC);
1969 if (IS_ERR(csums_tfm)) {
1970 csums_tfm = NULL;
1971 retcode = ERR_CSUMS_ALG;
1972 goto fail;
1973 }
1974
1975 if (!drbd_crypto_is_hash(crypto_hash_tfm(csums_tfm))) {
1976 retcode = ERR_CSUMS_ALG_ND;
1977 goto fail;
1978 }
1979 }
1980
1981 /* online verify running */
1982 ovr = (mdev->state.conn == C_VERIFY_S || mdev->state.conn == C_VERIFY_T);
1983
1984 if (ovr) {
1985 if (strcmp(sc.verify_alg, mdev->sync_conf.verify_alg)) {
1986 retcode = ERR_VERIFY_RUNNING;
1987 goto fail;
1988 }
1989 }
1990
1991 if (!ovr && sc.verify_alg[0]) {
1992 verify_tfm = crypto_alloc_hash(sc.verify_alg, 0, CRYPTO_ALG_ASYNC);
1993 if (IS_ERR(verify_tfm)) {
1994 verify_tfm = NULL;
1995 retcode = ERR_VERIFY_ALG;
1996 goto fail;
1997 }
1998
1999 if (!drbd_crypto_is_hash(crypto_hash_tfm(verify_tfm))) {
2000 retcode = ERR_VERIFY_ALG_ND;
2001 goto fail;
2002 }
2003 }
2004
2005 /* silently ignore cpu mask on UP kernel */
2006 if (nr_cpu_ids > 1 && sc.cpu_mask[0] != 0) {
2007 err = __bitmap_parse(sc.cpu_mask, 32, 0,
2008 cpumask_bits(new_cpu_mask), nr_cpu_ids);
2009 if (err) {
2010 dev_warn(DEV, "__bitmap_parse() failed with %d\n", err);
2011 retcode = ERR_CPU_MASK_PARSE;
2012 goto fail;
2013 }
2014 }
2015
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01002016 if (!expect(sc.rate >= 1))
2017 sc.rate = 1;
Lars Ellenberg7ad651b2011-02-21 13:21:03 +01002018
2019 /* clip to allowed range */
2020 if (!expect(sc.al_extents >= DRBD_AL_EXTENTS_MIN))
2021 sc.al_extents = DRBD_AL_EXTENTS_MIN;
2022 if (!expect(sc.al_extents <= DRBD_AL_EXTENTS_MAX))
2023 sc.al_extents = DRBD_AL_EXTENTS_MAX;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002024
2025 /* most sanity checks done, try to assign the new sync-after
2026 * dependency. need to hold the global lock in there,
2027 * to avoid a race in the dependency loop check. */
2028 retcode = drbd_alter_sa(mdev, sc.after);
2029 if (retcode != NO_ERROR)
2030 goto fail;
2031
Philipp Reisner778f2712010-07-06 11:14:00 +02002032 fifo_size = (sc.c_plan_ahead * 10 * SLEEP_TIME) / HZ;
2033 if (fifo_size != mdev->rs_plan_s.size && fifo_size > 0) {
2034 rs_plan_s = kzalloc(sizeof(int) * fifo_size, GFP_KERNEL);
2035 if (!rs_plan_s) {
2036 dev_err(DEV, "kmalloc of fifo_buffer failed");
2037 retcode = ERR_NOMEM;
2038 goto fail;
2039 }
2040 }
2041
Philipp Reisnerb411b362009-09-25 16:07:19 -07002042 /* ok, assign the rest of it as well.
2043 * lock against receive_SyncParam() */
2044 spin_lock(&mdev->peer_seq_lock);
2045 mdev->sync_conf = sc;
2046
2047 if (!rsr) {
2048 crypto_free_hash(mdev->csums_tfm);
2049 mdev->csums_tfm = csums_tfm;
2050 csums_tfm = NULL;
2051 }
2052
2053 if (!ovr) {
2054 crypto_free_hash(mdev->verify_tfm);
2055 mdev->verify_tfm = verify_tfm;
2056 verify_tfm = NULL;
2057 }
Philipp Reisner778f2712010-07-06 11:14:00 +02002058
2059 if (fifo_size != mdev->rs_plan_s.size) {
2060 kfree(mdev->rs_plan_s.values);
2061 mdev->rs_plan_s.values = rs_plan_s;
2062 mdev->rs_plan_s.size = fifo_size;
2063 mdev->rs_planed = 0;
2064 rs_plan_s = NULL;
2065 }
2066
Philipp Reisnerb411b362009-09-25 16:07:19 -07002067 spin_unlock(&mdev->peer_seq_lock);
2068
2069 if (get_ldev(mdev)) {
2070 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
2071 drbd_al_shrink(mdev);
2072 err = drbd_check_al_size(mdev);
2073 lc_unlock(mdev->act_log);
2074 wake_up(&mdev->al_wait);
2075
2076 put_ldev(mdev);
2077 drbd_md_sync(mdev);
2078
2079 if (err) {
2080 retcode = ERR_NOMEM;
2081 goto fail;
2082 }
2083 }
2084
2085 if (mdev->state.conn >= C_CONNECTED)
2086 drbd_send_sync_param(mdev, &sc);
2087
Philipp Reisner80822282011-02-08 12:46:30 +01002088 if (!cpumask_equal(mdev->tconn->cpu_mask, new_cpu_mask)) {
2089 cpumask_copy(mdev->tconn->cpu_mask, new_cpu_mask);
2090 drbd_calc_cpu_mask(mdev->tconn);
Philipp Reisnere6b3ea82011-01-19 14:02:01 +01002091 mdev->tconn->receiver.reset_cpu_mask = 1;
2092 mdev->tconn->asender.reset_cpu_mask = 1;
2093 mdev->tconn->worker.reset_cpu_mask = 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002094 }
2095
2096 kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
2097fail:
Philipp Reisner778f2712010-07-06 11:14:00 +02002098 kfree(rs_plan_s);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002099 free_cpumask_var(new_cpu_mask);
2100 crypto_free_hash(csums_tfm);
2101 crypto_free_hash(verify_tfm);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002102
2103 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002104 return 0;
2105}
2106
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002107int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002108{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002109 struct drbd_conf *mdev;
2110 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2111
2112 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2113 if (!adm_ctx.reply_skb)
2114 return retcode;
2115 if (retcode != NO_ERROR)
2116 goto out;
2117
2118 mdev = adm_ctx.mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002119
Lars Ellenberg194bfb32011-01-18 10:38:01 +01002120 /* If there is still bitmap IO pending, probably because of a previous
2121 * resync just being finished, wait for it before requesting a new resync. */
2122 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
2123
Philipp Reisnerb411b362009-09-25 16:07:19 -07002124 retcode = _drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T), CS_ORDERED);
2125
2126 if (retcode < SS_SUCCESS && retcode != SS_NEED_CONNECTION)
2127 retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T));
2128
2129 while (retcode == SS_NEED_CONNECTION) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01002130 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002131 if (mdev->state.conn < C_CONNECTED)
2132 retcode = _drbd_set_state(_NS(mdev, disk, D_INCONSISTENT), CS_VERBOSE, NULL);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002133 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002134
2135 if (retcode != SS_NEED_CONNECTION)
2136 break;
2137
2138 retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T));
2139 }
2140
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002141out:
2142 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002143 return 0;
2144}
2145
Philipp Reisner07782862010-08-31 12:00:50 +02002146static int drbd_bmio_set_susp_al(struct drbd_conf *mdev)
2147{
2148 int rv;
2149
2150 rv = drbd_bmio_set_n_write(mdev);
2151 drbd_suspend_al(mdev);
2152 return rv;
2153}
2154
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002155static int drbd_adm_simple_request_state(struct sk_buff *skb, struct genl_info *info,
2156 union drbd_state mask, union drbd_state val)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002157{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002158 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002159
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002160 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2161 if (!adm_ctx.reply_skb)
2162 return retcode;
2163 if (retcode != NO_ERROR)
2164 goto out;
Lars Ellenberg194bfb32011-01-18 10:38:01 +01002165
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002166 retcode = drbd_request_state(adm_ctx.mdev, mask, val);
2167out:
2168 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002169 return 0;
2170}
2171
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002172int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002173{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002174 return drbd_adm_simple_request_state(skb, info, NS(conn, C_STARTING_SYNC_S));
2175}
Philipp Reisnerb411b362009-09-25 16:07:19 -07002176
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002177int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info)
2178{
2179 enum drbd_ret_code retcode;
2180
2181 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2182 if (!adm_ctx.reply_skb)
2183 return retcode;
2184 if (retcode != NO_ERROR)
2185 goto out;
2186
2187 if (drbd_request_state(adm_ctx.mdev, NS(user_isp, 1)) == SS_NOTHING_TO_DO)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002188 retcode = ERR_PAUSE_IS_SET;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002189out:
2190 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002191 return 0;
2192}
2193
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002194int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002195{
Philipp Reisnercd88d032011-01-20 11:46:41 +01002196 union drbd_state s;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002197 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002198
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002199 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2200 if (!adm_ctx.reply_skb)
2201 return retcode;
2202 if (retcode != NO_ERROR)
2203 goto out;
2204
2205 if (drbd_request_state(adm_ctx.mdev, NS(user_isp, 0)) == SS_NOTHING_TO_DO) {
2206 s = adm_ctx.mdev->state;
Philipp Reisnercd88d032011-01-20 11:46:41 +01002207 if (s.conn == C_PAUSED_SYNC_S || s.conn == C_PAUSED_SYNC_T) {
2208 retcode = s.aftr_isp ? ERR_PIC_AFTER_DEP :
2209 s.peer_isp ? ERR_PIC_PEER_DEP : ERR_PAUSE_IS_CLEAR;
2210 } else {
2211 retcode = ERR_PAUSE_IS_CLEAR;
2212 }
2213 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002214
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002215out:
2216 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002217 return 0;
2218}
2219
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002220int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002221{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002222 return drbd_adm_simple_request_state(skb, info, NS(susp, 1));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002223}
2224
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002225int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002226{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002227 struct drbd_conf *mdev;
2228 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2229
2230 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2231 if (!adm_ctx.reply_skb)
2232 return retcode;
2233 if (retcode != NO_ERROR)
2234 goto out;
2235
2236 mdev = adm_ctx.mdev;
Philipp Reisner43a51822010-06-11 11:26:34 +02002237 if (test_bit(NEW_CUR_UUID, &mdev->flags)) {
2238 drbd_uuid_new_current(mdev);
2239 clear_bit(NEW_CUR_UUID, &mdev->flags);
Philipp Reisner43a51822010-06-11 11:26:34 +02002240 }
Philipp Reisner265be2d2010-05-31 10:14:17 +02002241 drbd_suspend_io(mdev);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002242 retcode = drbd_request_state(mdev, NS3(susp, 0, susp_nod, 0, susp_fen, 0));
2243 if (retcode == SS_SUCCESS) {
Philipp Reisner265be2d2010-05-31 10:14:17 +02002244 if (mdev->state.conn < C_CONNECTED)
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01002245 tl_clear(mdev->tconn);
Philipp Reisner265be2d2010-05-31 10:14:17 +02002246 if (mdev->state.disk == D_DISKLESS || mdev->state.disk == D_FAILED)
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01002247 tl_restart(mdev->tconn, FAIL_FROZEN_DISK_IO);
Philipp Reisner265be2d2010-05-31 10:14:17 +02002248 }
2249 drbd_resume_io(mdev);
2250
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002251out:
2252 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002253 return 0;
2254}
2255
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002256int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002257{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002258 return drbd_adm_simple_request_state(skb, info, NS(disk, D_OUTDATED));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002259}
2260
Lars Ellenberg543cc102011-03-10 22:18:18 +01002261int nla_put_drbd_cfg_context(struct sk_buff *skb, const char *conn_name, unsigned vnr)
2262{
2263 struct nlattr *nla;
2264 nla = nla_nest_start(skb, DRBD_NLA_CFG_CONTEXT);
2265 if (!nla)
2266 goto nla_put_failure;
2267 if (vnr != VOLUME_UNSPECIFIED)
2268 NLA_PUT_U32(skb, T_ctx_volume, vnr);
2269 NLA_PUT_STRING(skb, T_ctx_conn_name, conn_name);
2270 nla_nest_end(skb, nla);
2271 return 0;
2272
2273nla_put_failure:
2274 if (nla)
2275 nla_nest_cancel(skb, nla);
2276 return -EMSGSIZE;
2277}
2278
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002279int nla_put_status_info(struct sk_buff *skb, struct drbd_conf *mdev,
2280 const struct sib_info *sib)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002281{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002282 struct state_info *si = NULL; /* for sizeof(si->member); */
2283 struct nlattr *nla;
2284 int got_ldev;
2285 int got_net;
2286 int err = 0;
2287 int exclude_sensitive;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002288
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002289 /* If sib != NULL, this is drbd_bcast_event, which anyone can listen
2290 * to. So we better exclude_sensitive information.
2291 *
2292 * If sib == NULL, this is drbd_adm_get_status, executed synchronously
2293 * in the context of the requesting user process. Exclude sensitive
2294 * information, unless current has superuser.
2295 *
2296 * NOTE: for drbd_adm_get_status_all(), this is a netlink dump, and
2297 * relies on the current implementation of netlink_dump(), which
2298 * executes the dump callback successively from netlink_recvmsg(),
2299 * always in the context of the receiving process */
2300 exclude_sensitive = sib || !capable(CAP_SYS_ADMIN);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002301
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002302 got_ldev = get_ldev(mdev);
2303 got_net = get_net_conf(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002304
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002305 /* We need to add connection name and volume number information still.
2306 * Minor number is in drbd_genlmsghdr. */
Lars Ellenberg543cc102011-03-10 22:18:18 +01002307 if (nla_put_drbd_cfg_context(skb, mdev->tconn->name, mdev->vnr))
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002308 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002309
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002310 if (got_ldev)
2311 if (disk_conf_to_skb(skb, &mdev->ldev->dc, exclude_sensitive))
2312 goto nla_put_failure;
2313 if (got_net)
2314 if (net_conf_to_skb(skb, mdev->tconn->net_conf, exclude_sensitive))
2315 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002316
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002317 if (syncer_conf_to_skb(skb, &mdev->sync_conf, exclude_sensitive))
2318 goto nla_put_failure;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002319
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002320 nla = nla_nest_start(skb, DRBD_NLA_STATE_INFO);
2321 if (!nla)
2322 goto nla_put_failure;
2323 NLA_PUT_U32(skb, T_sib_reason, sib ? sib->sib_reason : SIB_GET_STATUS_REPLY);
2324 NLA_PUT_U32(skb, T_current_state, mdev->state.i);
2325 NLA_PUT_U64(skb, T_ed_uuid, mdev->ed_uuid);
2326 NLA_PUT_U64(skb, T_capacity, drbd_get_capacity(mdev->this_bdev));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002327
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002328 if (got_ldev) {
2329 NLA_PUT_U32(skb, T_disk_flags, mdev->ldev->md.flags);
2330 NLA_PUT(skb, T_uuids, sizeof(si->uuids), mdev->ldev->md.uuid);
2331 NLA_PUT_U64(skb, T_bits_total, drbd_bm_bits(mdev));
2332 NLA_PUT_U64(skb, T_bits_oos, drbd_bm_total_weight(mdev));
2333 if (C_SYNC_SOURCE <= mdev->state.conn &&
2334 C_PAUSED_SYNC_T >= mdev->state.conn) {
2335 NLA_PUT_U64(skb, T_bits_rs_total, mdev->rs_total);
2336 NLA_PUT_U64(skb, T_bits_rs_failed, mdev->rs_failed);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002337 }
2338 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002339
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002340 if (sib) {
2341 switch(sib->sib_reason) {
2342 case SIB_SYNC_PROGRESS:
2343 case SIB_GET_STATUS_REPLY:
2344 break;
2345 case SIB_STATE_CHANGE:
2346 NLA_PUT_U32(skb, T_prev_state, sib->os.i);
2347 NLA_PUT_U32(skb, T_new_state, sib->ns.i);
2348 break;
2349 case SIB_HELPER_POST:
2350 NLA_PUT_U32(skb,
2351 T_helper_exit_code, sib->helper_exit_code);
2352 /* fall through */
2353 case SIB_HELPER_PRE:
2354 NLA_PUT_STRING(skb, T_helper, sib->helper_name);
2355 break;
2356 }
2357 }
2358 nla_nest_end(skb, nla);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002359
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002360 if (0)
2361nla_put_failure:
2362 err = -EMSGSIZE;
2363 if (got_ldev)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002364 put_ldev(mdev);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002365 if (got_net)
2366 put_net_conf(mdev->tconn);
2367 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002368}
2369
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002370int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002371{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002372 enum drbd_ret_code retcode;
2373 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002374
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002375 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2376 if (!adm_ctx.reply_skb)
2377 return retcode;
2378 if (retcode != NO_ERROR)
2379 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002380
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002381 err = nla_put_status_info(adm_ctx.reply_skb, adm_ctx.mdev, NULL);
2382 if (err) {
2383 nlmsg_free(adm_ctx.reply_skb);
2384 return err;
2385 }
2386out:
2387 drbd_adm_finish(info, retcode);
2388 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002389}
2390
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002391int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002392{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002393 struct drbd_conf *mdev;
2394 struct drbd_genlmsghdr *dh;
Lars Ellenberg543cc102011-03-10 22:18:18 +01002395 struct drbd_tconn *pos = (struct drbd_tconn*)cb->args[0];
2396 struct drbd_tconn *tconn = NULL;
2397 struct drbd_tconn *tmp;
2398 unsigned volume = cb->args[1];
Philipp Reisnerb411b362009-09-25 16:07:19 -07002399
Lars Ellenberg543cc102011-03-10 22:18:18 +01002400 /* Open coded, deferred, iteration:
2401 * list_for_each_entry_safe(tconn, tmp, &drbd_tconns, all_tconn) {
2402 * idr_for_each_entry(&tconn->volumes, mdev, i) {
2403 * ...
2404 * }
2405 * }
2406 * where tconn is cb->args[0];
2407 * and i is cb->args[1];
2408 *
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002409 * This may miss entries inserted after this dump started,
2410 * or entries deleted before they are reached.
Lars Ellenberg543cc102011-03-10 22:18:18 +01002411 *
2412 * We need to make sure the mdev won't disappear while
2413 * we are looking at it, and revalidate our iterators
2414 * on each iteration.
2415 */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002416
Lars Ellenberg543cc102011-03-10 22:18:18 +01002417 /* synchronize with drbd_new_tconn/drbd_free_tconn */
2418 mutex_lock(&drbd_cfg_mutex);
2419 /* synchronize with drbd_delete_device */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002420 rcu_read_lock();
Lars Ellenberg543cc102011-03-10 22:18:18 +01002421next_tconn:
2422 /* revalidate iterator position */
2423 list_for_each_entry(tmp, &drbd_tconns, all_tconn) {
2424 if (pos == NULL) {
2425 /* first iteration */
2426 pos = tmp;
2427 tconn = pos;
2428 break;
2429 }
2430 if (tmp == pos) {
2431 tconn = pos;
2432 break;
2433 }
2434 }
2435 if (tconn) {
2436 mdev = idr_get_next(&tconn->volumes, &volume);
2437 if (!mdev) {
2438 /* No more volumes to dump on this tconn.
2439 * Advance tconn iterator. */
2440 pos = list_entry(tconn->all_tconn.next,
2441 struct drbd_tconn, all_tconn);
2442 /* But, did we dump any volume on this tconn yet? */
2443 if (volume != 0) {
2444 tconn = NULL;
2445 volume = 0;
2446 goto next_tconn;
2447 }
2448 }
2449
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002450 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).pid,
2451 cb->nlh->nlmsg_seq, &drbd_genl_family,
2452 NLM_F_MULTI, DRBD_ADM_GET_STATUS);
2453 if (!dh)
Lars Ellenberg543cc102011-03-10 22:18:18 +01002454 goto out;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002455
Lars Ellenberg543cc102011-03-10 22:18:18 +01002456 if (!mdev) {
2457 /* this is a tconn without a single volume */
2458 dh->minor = -1U;
2459 dh->ret_code = NO_ERROR;
2460 if (nla_put_drbd_cfg_context(skb, tconn->name, VOLUME_UNSPECIFIED))
2461 genlmsg_cancel(skb, dh);
2462 else
2463 genlmsg_end(skb, dh);
2464 goto out;
2465 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002466
Lars Ellenberg543cc102011-03-10 22:18:18 +01002467 D_ASSERT(mdev->vnr == volume);
2468 D_ASSERT(mdev->tconn == tconn);
2469
2470 dh->minor = mdev_to_minor(mdev);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002471 dh->ret_code = NO_ERROR;
2472
2473 if (nla_put_status_info(skb, mdev, NULL)) {
2474 genlmsg_cancel(skb, dh);
Lars Ellenberg543cc102011-03-10 22:18:18 +01002475 goto out;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002476 }
2477 genlmsg_end(skb, dh);
2478 }
2479
Lars Ellenberg543cc102011-03-10 22:18:18 +01002480out:
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002481 rcu_read_unlock();
Lars Ellenberg543cc102011-03-10 22:18:18 +01002482 mutex_unlock(&drbd_cfg_mutex);
2483 /* where to start the next iteration */
2484 cb->args[0] = (long)pos;
2485 cb->args[1] = (pos == tconn) ? volume + 1 : 0;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002486
Lars Ellenberg543cc102011-03-10 22:18:18 +01002487 /* No more tconns/volumes/minors found results in an empty skb.
2488 * Which will terminate the dump. */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002489 return skb->len;
2490}
2491
2492int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info)
2493{
2494 enum drbd_ret_code retcode;
2495 struct timeout_parms tp;
2496 int err;
2497
2498 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2499 if (!adm_ctx.reply_skb)
2500 return retcode;
2501 if (retcode != NO_ERROR)
2502 goto out;
2503
2504 tp.timeout_type =
2505 adm_ctx.mdev->state.pdsk == D_OUTDATED ? UT_PEER_OUTDATED :
2506 test_bit(USE_DEGR_WFC_T, &adm_ctx.mdev->flags) ? UT_DEGRADED :
2507 UT_DEFAULT;
2508
2509 err = timeout_parms_to_priv_skb(adm_ctx.reply_skb, &tp);
2510 if (err) {
2511 nlmsg_free(adm_ctx.reply_skb);
2512 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002513 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002514out:
2515 drbd_adm_finish(info, retcode);
2516 return 0;
2517}
Lars Ellenberg873b0d52011-01-21 22:53:48 +01002518
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002519int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info)
2520{
2521 struct drbd_conf *mdev;
2522 enum drbd_ret_code retcode;
2523
2524 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2525 if (!adm_ctx.reply_skb)
2526 return retcode;
2527 if (retcode != NO_ERROR)
2528 goto out;
2529
2530 mdev = adm_ctx.mdev;
2531 if (info->attrs[DRBD_NLA_START_OV_PARMS]) {
2532 /* resume from last known position, if possible */
2533 struct start_ov_parms parms =
2534 { .ov_start_sector = mdev->ov_start_sector };
2535 int err = start_ov_parms_from_attrs(&parms, info->attrs);
2536 if (err) {
2537 retcode = ERR_MANDATORY_TAG;
2538 drbd_msg_put_info(from_attrs_err_to_txt(err));
2539 goto out;
2540 }
2541 /* w_make_ov_request expects position to be aligned */
2542 mdev->ov_start_sector = parms.ov_start_sector & ~BM_SECT_PER_BIT;
2543 }
Lars Ellenberg873b0d52011-01-21 22:53:48 +01002544 /* If there is still bitmap IO pending, e.g. previous resync or verify
2545 * just being finished, wait for it before requesting a new resync. */
2546 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002547 retcode = drbd_request_state(mdev,NS(conn,C_VERIFY_S));
2548out:
2549 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002550 return 0;
2551}
2552
2553
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002554int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002555{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002556 struct drbd_conf *mdev;
2557 enum drbd_ret_code retcode;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002558 int skip_initial_sync = 0;
2559 int err;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002560 struct new_c_uuid_parms args;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002561
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002562 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2563 if (!adm_ctx.reply_skb)
2564 return retcode;
2565 if (retcode != NO_ERROR)
2566 goto out_nolock;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002567
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002568 mdev = adm_ctx.mdev;
2569 memset(&args, 0, sizeof(args));
2570 if (info->attrs[DRBD_NLA_NEW_C_UUID_PARMS]) {
2571 err = new_c_uuid_parms_from_attrs(&args, info->attrs);
2572 if (err) {
2573 retcode = ERR_MANDATORY_TAG;
2574 drbd_msg_put_info(from_attrs_err_to_txt(err));
2575 goto out_nolock;
2576 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002577 }
2578
Philipp Reisner8410da82011-02-11 20:11:10 +01002579 mutex_lock(mdev->state_mutex); /* Protects us against serialized state changes. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002580
2581 if (!get_ldev(mdev)) {
2582 retcode = ERR_NO_DISK;
2583 goto out;
2584 }
2585
2586 /* this is "skip initial sync", assume to be clean */
Philipp Reisner31890f42011-01-19 14:12:51 +01002587 if (mdev->state.conn == C_CONNECTED && mdev->tconn->agreed_pro_version >= 90 &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07002588 mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED && args.clear_bm) {
2589 dev_info(DEV, "Preparing to skip initial sync\n");
2590 skip_initial_sync = 1;
2591 } else if (mdev->state.conn != C_STANDALONE) {
2592 retcode = ERR_CONNECTED;
2593 goto out_dec;
2594 }
2595
2596 drbd_uuid_set(mdev, UI_BITMAP, 0); /* Rotate UI_BITMAP to History 1, etc... */
2597 drbd_uuid_new_current(mdev); /* New current, previous to UI_BITMAP */
2598
2599 if (args.clear_bm) {
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002600 err = drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write,
2601 "clear_n_write from new_c_uuid", BM_LOCKED_MASK);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002602 if (err) {
2603 dev_err(DEV, "Writing bitmap failed with %d\n",err);
2604 retcode = ERR_IO_MD_DISK;
2605 }
2606 if (skip_initial_sync) {
2607 drbd_send_uuids_skip_initial_sync(mdev);
2608 _drbd_uuid_set(mdev, UI_BITMAP, 0);
Lars Ellenberg62b0da32011-01-20 13:25:21 +01002609 drbd_print_uuids(mdev, "cleared bitmap UUID");
Philipp Reisner87eeee42011-01-19 14:16:30 +01002610 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002611 _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
2612 CS_VERBOSE, NULL);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002613 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002614 }
2615 }
2616
2617 drbd_md_sync(mdev);
2618out_dec:
2619 put_ldev(mdev);
2620out:
Philipp Reisner8410da82011-02-11 20:11:10 +01002621 mutex_unlock(mdev->state_mutex);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002622out_nolock:
2623 drbd_adm_finish(info, retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002624 return 0;
2625}
2626
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002627static enum drbd_ret_code
2628drbd_check_conn_name(const char *name)
Philipp Reisner774b3052011-02-22 02:07:03 -05002629{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002630 if (!name || !name[0]) {
2631 drbd_msg_put_info("connection name missing");
2632 return ERR_MANDATORY_TAG;
2633 }
2634 /* if we want to use these in sysfs/configfs/debugfs some day,
2635 * we must not allow slashes */
2636 if (strchr(name, '/')) {
2637 drbd_msg_put_info("invalid connection name");
2638 return ERR_INVALID_REQUEST;
2639 }
2640 return NO_ERROR;
2641}
Philipp Reisner774b3052011-02-22 02:07:03 -05002642
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002643int drbd_adm_create_connection(struct sk_buff *skb, struct genl_info *info)
2644{
2645 enum drbd_ret_code retcode;
2646
2647 retcode = drbd_adm_prepare(skb, info, 0);
2648 if (!adm_ctx.reply_skb)
2649 return retcode;
2650 if (retcode != NO_ERROR)
2651 goto out;
2652
2653 retcode = drbd_check_conn_name(adm_ctx.conn_name);
2654 if (retcode != NO_ERROR)
2655 goto out;
2656
2657 if (adm_ctx.tconn) {
Lars Ellenberg38f19612011-03-14 13:22:35 +01002658 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL) {
2659 retcode = ERR_INVALID_REQUEST;
2660 drbd_msg_put_info("connection exists");
2661 }
2662 /* else: still NO_ERROR */
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002663 goto out;
Philipp Reisner774b3052011-02-22 02:07:03 -05002664 }
2665
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002666 if (!drbd_new_tconn(adm_ctx.conn_name))
2667 retcode = ERR_NOMEM;
2668out:
2669 drbd_adm_finish(info, retcode);
Philipp Reisner774b3052011-02-22 02:07:03 -05002670 return 0;
2671}
2672
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002673int drbd_adm_add_minor(struct sk_buff *skb, struct genl_info *info)
Philipp Reisner774b3052011-02-22 02:07:03 -05002674{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002675 struct drbd_genlmsghdr *dh = info->userhdr;
2676 enum drbd_ret_code retcode;
Philipp Reisner774b3052011-02-22 02:07:03 -05002677
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002678 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONN);
2679 if (!adm_ctx.reply_skb)
2680 return retcode;
2681 if (retcode != NO_ERROR)
2682 goto out;
Philipp Reisner774b3052011-02-22 02:07:03 -05002683
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002684 /* FIXME drop minor_count parameter, limit to MINORMASK */
2685 if (dh->minor >= minor_count) {
2686 drbd_msg_put_info("requested minor out of range");
2687 retcode = ERR_INVALID_REQUEST;
2688 goto out;
2689 }
2690 /* FIXME we need a define here */
2691 if (adm_ctx.volume >= 256) {
2692 drbd_msg_put_info("requested volume id out of range");
2693 retcode = ERR_INVALID_REQUEST;
2694 goto out;
Philipp Reisner774b3052011-02-22 02:07:03 -05002695 }
2696
Lars Ellenberg38f19612011-03-14 13:22:35 +01002697 /* drbd_adm_prepare made sure already
2698 * that mdev->tconn and mdev->vnr match the request. */
2699 if (adm_ctx.mdev) {
2700 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
2701 retcode = ERR_MINOR_EXISTS;
2702 /* else: still NO_ERROR */
2703 goto out;
2704 }
2705
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002706 retcode = conn_new_minor(adm_ctx.tconn, dh->minor, adm_ctx.volume);
2707out:
2708 drbd_adm_finish(info, retcode);
Philipp Reisner774b3052011-02-22 02:07:03 -05002709 return 0;
2710}
2711
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002712static enum drbd_ret_code adm_delete_minor(struct drbd_conf *mdev)
2713{
2714 if (mdev->state.disk == D_DISKLESS &&
2715 /* no need to be mdev->state.conn == C_STANDALONE &&
2716 * we may want to delete a minor from a live replication group.
2717 */
2718 mdev->state.role == R_SECONDARY) {
2719 drbd_delete_device(mdev_to_minor(mdev));
2720 return NO_ERROR;
2721 } else
2722 return ERR_MINOR_CONFIGURED;
2723}
2724
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002725int drbd_adm_delete_minor(struct sk_buff *skb, struct genl_info *info)
Philipp Reisner774b3052011-02-22 02:07:03 -05002726{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002727 enum drbd_ret_code retcode;
2728
2729 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2730 if (!adm_ctx.reply_skb)
2731 return retcode;
2732 if (retcode != NO_ERROR)
2733 goto out;
2734
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002735 mutex_lock(&drbd_cfg_mutex);
2736 retcode = adm_delete_minor(adm_ctx.mdev);
2737 mutex_unlock(&drbd_cfg_mutex);
2738 /* if this was the last volume of this connection,
2739 * this will terminate all threads */
2740 if (retcode == NO_ERROR)
Lars Ellenbergcffec5b2011-03-10 23:33:15 +01002741 conn_reconfig_done(adm_ctx.tconn);
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002742out:
2743 drbd_adm_finish(info, retcode);
2744 return 0;
2745}
2746
2747int drbd_adm_down(struct sk_buff *skb, struct genl_info *info)
2748{
2749 enum drbd_ret_code retcode;
2750 enum drbd_state_rv rv;
2751 struct drbd_conf *mdev;
2752 unsigned i;
2753
2754 retcode = drbd_adm_prepare(skb, info, 0);
2755 if (!adm_ctx.reply_skb)
2756 return retcode;
2757 if (retcode != NO_ERROR)
2758 goto out;
2759
2760 if (!adm_ctx.tconn) {
2761 retcode = ERR_CONN_NOT_KNOWN;
2762 goto out;
2763 }
2764
2765 mutex_lock(&drbd_cfg_mutex);
2766 /* demote */
2767 idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
2768 retcode = drbd_set_role(mdev, R_SECONDARY, 0);
2769 if (retcode < SS_SUCCESS) {
2770 drbd_msg_put_info("failed to demote");
2771 goto out_unlock;
2772 }
2773 }
2774
2775 /* disconnect */
2776 rv = conn_try_disconnect(adm_ctx.tconn, 0);
2777 if (rv < SS_SUCCESS) {
2778 retcode = rv; /* enum type mismatch! */
2779 drbd_msg_put_info("failed to disconnect");
2780 goto out_unlock;
2781 }
2782
2783 /* detach */
2784 idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
2785 rv = adm_detach(mdev);
2786 if (rv < SS_SUCCESS) {
2787 retcode = rv; /* enum type mismatch! */
2788 drbd_msg_put_info("failed to detach");
2789 goto out_unlock;
2790 }
2791 }
2792
2793 /* delete volumes */
2794 idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
2795 retcode = adm_delete_minor(mdev);
2796 if (retcode != NO_ERROR) {
2797 /* "can not happen" */
2798 drbd_msg_put_info("failed to delete volume");
2799 goto out_unlock;
2800 }
2801 }
2802
2803 /* stop all threads */
2804 conn_reconfig_done(adm_ctx.tconn);
2805
2806 /* delete connection */
2807 if (conn_lowest_minor(adm_ctx.tconn) < 0) {
2808 drbd_free_tconn(adm_ctx.tconn);
2809 retcode = NO_ERROR;
2810 } else {
2811 /* "can not happen" */
2812 retcode = ERR_CONN_IN_USE;
2813 drbd_msg_put_info("failed to delete connection");
2814 goto out_unlock;
2815 }
2816out_unlock:
2817 mutex_unlock(&drbd_cfg_mutex);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002818out:
2819 drbd_adm_finish(info, retcode);
Philipp Reisner774b3052011-02-22 02:07:03 -05002820 return 0;
2821}
2822
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002823int drbd_adm_delete_connection(struct sk_buff *skb, struct genl_info *info)
Philipp Reisner774b3052011-02-22 02:07:03 -05002824{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002825 enum drbd_ret_code retcode;
2826
2827 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONN);
2828 if (!adm_ctx.reply_skb)
2829 return retcode;
2830 if (retcode != NO_ERROR)
2831 goto out;
2832
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002833 mutex_lock(&drbd_cfg_mutex);
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002834 if (conn_lowest_minor(adm_ctx.tconn) < 0) {
2835 drbd_free_tconn(adm_ctx.tconn);
2836 retcode = NO_ERROR;
Philipp Reisner774b3052011-02-22 02:07:03 -05002837 } else {
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002838 retcode = ERR_CONN_IN_USE;
Philipp Reisner774b3052011-02-22 02:07:03 -05002839 }
Lars Ellenberg85f75dd72011-03-15 16:26:37 +01002840 mutex_unlock(&drbd_cfg_mutex);
Philipp Reisner774b3052011-02-22 02:07:03 -05002841
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002842out:
2843 drbd_adm_finish(info, retcode);
Philipp Reisner774b3052011-02-22 02:07:03 -05002844 return 0;
2845}
2846
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002847void drbd_bcast_event(struct drbd_conf *mdev, const struct sib_info *sib)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002848{
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002849 static atomic_t drbd_genl_seq = ATOMIC_INIT(2); /* two. */
2850 struct sk_buff *msg;
2851 struct drbd_genlmsghdr *d_out;
2852 unsigned seq;
2853 int err = -ENOMEM;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002854
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002855 seq = atomic_inc_return(&drbd_genl_seq);
2856 msg = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
2857 if (!msg)
2858 goto failed;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002859
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002860 err = -EMSGSIZE;
2861 d_out = genlmsg_put(msg, 0, seq, &drbd_genl_family, 0, DRBD_EVENT);
2862 if (!d_out) /* cannot happen, but anyways. */
2863 goto nla_put_failure;
2864 d_out->minor = mdev_to_minor(mdev);
2865 d_out->ret_code = 0;
Philipp Reisner9f5180e2009-10-06 09:30:14 +02002866
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002867 if (nla_put_status_info(msg, mdev, sib))
2868 goto nla_put_failure;
2869 genlmsg_end(msg, d_out);
2870 err = drbd_genl_multicast_events(msg, 0);
2871 /* msg has been consumed or freed in netlink_broadcast() */
2872 if (err && err != -ESRCH)
2873 goto failed;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002874
Philipp Reisnerb411b362009-09-25 16:07:19 -07002875 return;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +01002876
2877nla_put_failure:
2878 nlmsg_free(msg);
2879failed:
2880 dev_err(DEV, "Error %d while broadcasting event. "
2881 "Event seq:%u sib_reason:%u\n",
2882 err, seq, sib->sib_reason);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002883}