4 This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
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>.
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)
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.
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.
26 #include <linux/module.h>
27 #include <linux/drbd.h>
30 #include <linux/file.h>
31 #include <linux/slab.h>
32 #include <linux/blkpg.h>
33 #include <linux/cpumask.h>
36 #include "drbd_wrappers.h"
37 #include <asm/unaligned.h>
38 #include <linux/drbd_limits.h>
39 #include <linux/kthread.h>
41 #include <net/genetlink.h>
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);
47 int drbd_adm_add_minor(struct sk_buff *skb, struct genl_info *info);
48 int drbd_adm_delete_minor(struct sk_buff *skb, struct genl_info *info);
50 int drbd_adm_new_resource(struct sk_buff *skb, struct genl_info *info);
51 int drbd_adm_del_resource(struct sk_buff *skb, struct genl_info *info);
52 int drbd_adm_down(struct sk_buff *skb, struct genl_info *info);
54 int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info);
55 int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info);
56 int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info);
57 int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info);
58 int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info);
59 int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info);
60 int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info);
61 int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info);
62 int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info);
63 int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info);
64 int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info);
65 int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info);
66 int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info);
67 int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info);
68 int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info);
69 int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info);
70 int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info);
71 int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info);
72 int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info);
73 int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info);
75 int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb);
77 #include <linux/drbd_genl_api.h>
79 #include <linux/genl_magic_func.h>
81 /* used blkdev_get_by_path, to claim our meta data device(s) */
82 static char *drbd_m_holder = "Hands off! this is DRBD's meta data device.";
84 /* Configuration is strictly serialized, because generic netlink message
85 * processing is strictly serialized by the genl_lock().
86 * Which means we can use one static global drbd_config_context struct.
88 static struct drbd_config_context {
89 /* assigned from drbd_genlmsghdr */
91 /* assigned from request attributes, if present */
93 #define VOLUME_UNSPECIFIED (-1U)
94 /* pointer into the request skb,
95 * limited lifetime! */
97 struct nlattr *my_addr;
98 struct nlattr *peer_addr;
101 struct sk_buff *reply_skb;
102 /* pointer into reply buffer */
103 struct drbd_genlmsghdr *reply_dh;
104 /* resolved from attributes, if possible */
105 struct drbd_conf *mdev;
106 struct drbd_tconn *tconn;
109 static void drbd_adm_send_reply(struct sk_buff *skb, struct genl_info *info)
111 genlmsg_end(skb, genlmsg_data(nlmsg_data(nlmsg_hdr(skb))));
112 if (genlmsg_reply(skb, info))
113 printk(KERN_ERR "drbd: error sending genl reply\n");
116 /* Used on a fresh "drbd_adm_prepare"d reply_skb, this cannot fail: The only
117 * reason it could fail was no space in skb, and there are 4k available. */
118 int drbd_msg_put_info(const char *info)
120 struct sk_buff *skb = adm_ctx.reply_skb;
124 if (!info || !info[0])
127 nla = nla_nest_start(skb, DRBD_NLA_CFG_REPLY);
131 err = nla_put_string(skb, T_info_text, info);
133 nla_nest_cancel(skb, nla);
136 nla_nest_end(skb, nla);
140 /* This would be a good candidate for a "pre_doit" hook,
141 * and per-family private info->pointers.
142 * But we need to stay compatible with older kernels.
143 * If it returns successfully, adm_ctx members are valid.
145 #define DRBD_ADM_NEED_MINOR 1
146 #define DRBD_ADM_NEED_RESOURCE 2
147 #define DRBD_ADM_NEED_CONNECTION 4
148 static int drbd_adm_prepare(struct sk_buff *skb, struct genl_info *info,
151 struct drbd_genlmsghdr *d_in = info->userhdr;
152 const u8 cmd = info->genlhdr->cmd;
155 memset(&adm_ctx, 0, sizeof(adm_ctx));
157 /* genl_rcv_msg only checks for CAP_NET_ADMIN on "GENL_ADMIN_PERM" :( */
158 if (cmd != DRBD_ADM_GET_STATUS
159 && security_netlink_recv(skb, CAP_SYS_ADMIN))
162 adm_ctx.reply_skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
163 if (!adm_ctx.reply_skb) {
168 adm_ctx.reply_dh = genlmsg_put_reply(adm_ctx.reply_skb,
169 info, &drbd_genl_family, 0, cmd);
170 /* put of a few bytes into a fresh skb of >= 4k will always succeed.
172 if (!adm_ctx.reply_dh) {
177 adm_ctx.reply_dh->minor = d_in->minor;
178 adm_ctx.reply_dh->ret_code = NO_ERROR;
180 adm_ctx.volume = VOLUME_UNSPECIFIED;
181 if (info->attrs[DRBD_NLA_CFG_CONTEXT]) {
183 /* parse and validate only */
184 err = drbd_cfg_context_from_attrs(NULL, info);
188 /* It was present, and valid,
189 * copy it over to the reply skb. */
190 err = nla_put_nohdr(adm_ctx.reply_skb,
191 info->attrs[DRBD_NLA_CFG_CONTEXT]->nla_len,
192 info->attrs[DRBD_NLA_CFG_CONTEXT]);
196 /* and assign stuff to the global adm_ctx */
197 nla = nested_attr_tb[__nla_type(T_ctx_volume)];
199 adm_ctx.volume = nla_get_u32(nla);
200 nla = nested_attr_tb[__nla_type(T_ctx_resource_name)];
202 adm_ctx.resource_name = nla_data(nla);
203 adm_ctx.my_addr = nested_attr_tb[__nla_type(T_ctx_my_addr)];
204 adm_ctx.peer_addr = nested_attr_tb[__nla_type(T_ctx_peer_addr)];
205 if ((adm_ctx.my_addr &&
206 nla_len(adm_ctx.my_addr) > sizeof(adm_ctx.tconn->my_addr)) ||
207 (adm_ctx.peer_addr &&
208 nla_len(adm_ctx.peer_addr) > sizeof(adm_ctx.tconn->peer_addr))) {
214 adm_ctx.minor = d_in->minor;
215 adm_ctx.mdev = minor_to_mdev(d_in->minor);
216 adm_ctx.tconn = conn_get_by_name(adm_ctx.resource_name);
218 if (!adm_ctx.mdev && (flags & DRBD_ADM_NEED_MINOR)) {
219 drbd_msg_put_info("unknown minor");
220 return ERR_MINOR_INVALID;
222 if (!adm_ctx.tconn && (flags & DRBD_ADM_NEED_RESOURCE)) {
223 drbd_msg_put_info("unknown resource");
224 return ERR_INVALID_REQUEST;
227 if (flags & DRBD_ADM_NEED_CONNECTION) {
228 if (adm_ctx.tconn && !(flags & DRBD_ADM_NEED_RESOURCE)) {
229 drbd_msg_put_info("no resource name expected");
230 return ERR_INVALID_REQUEST;
233 drbd_msg_put_info("no minor number expected");
234 return ERR_INVALID_REQUEST;
236 if (adm_ctx.my_addr && adm_ctx.peer_addr)
237 adm_ctx.tconn = conn_get_by_addrs(nla_data(adm_ctx.my_addr),
238 nla_len(adm_ctx.my_addr),
239 nla_data(adm_ctx.peer_addr),
240 nla_len(adm_ctx.peer_addr));
241 if (!adm_ctx.tconn) {
242 drbd_msg_put_info("unknown connection");
243 return ERR_INVALID_REQUEST;
247 /* some more paranoia, if the request was over-determined */
248 if (adm_ctx.mdev && adm_ctx.tconn &&
249 adm_ctx.mdev->tconn != adm_ctx.tconn) {
250 pr_warning("request: minor=%u, resource=%s; but that minor belongs to connection %s\n",
251 adm_ctx.minor, adm_ctx.resource_name,
252 adm_ctx.mdev->tconn->name);
253 drbd_msg_put_info("minor exists in different resource");
254 return ERR_INVALID_REQUEST;
257 adm_ctx.volume != VOLUME_UNSPECIFIED &&
258 adm_ctx.volume != adm_ctx.mdev->vnr) {
259 pr_warning("request: minor=%u, volume=%u; but that minor is volume %u in %s\n",
260 adm_ctx.minor, adm_ctx.volume,
261 adm_ctx.mdev->vnr, adm_ctx.mdev->tconn->name);
262 drbd_msg_put_info("minor exists as different volume");
263 return ERR_INVALID_REQUEST;
269 nlmsg_free(adm_ctx.reply_skb);
270 adm_ctx.reply_skb = NULL;
274 static int drbd_adm_finish(struct genl_info *info, int retcode)
277 const char *resource_name = NULL;
280 kref_put(&adm_ctx.tconn->kref, &conn_destroy);
281 adm_ctx.tconn = NULL;
284 if (!adm_ctx.reply_skb)
287 adm_ctx.reply_dh->ret_code = retcode;
289 nla = info->attrs[DRBD_NLA_CFG_CONTEXT];
291 int maxtype = ARRAY_SIZE(drbd_cfg_context_nl_policy) - 1;
292 nla = drbd_nla_find_nested(maxtype, nla, __nla_type(T_ctx_resource_name));
293 if (nla && !IS_ERR(nla))
294 resource_name = nla_data(nla);
297 drbd_adm_send_reply(adm_ctx.reply_skb, info);
301 static void setup_khelper_env(struct drbd_tconn *tconn, char **envp)
305 /* FIXME: A future version will not allow this case. */
306 if (tconn->my_addr_len == 0 || tconn->peer_addr_len == 0)
309 switch (((struct sockaddr *)&tconn->peer_addr)->sa_family) {
312 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI6",
313 &((struct sockaddr_in6 *)&tconn->peer_addr)->sin6_addr);
317 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
318 &((struct sockaddr_in *)&tconn->peer_addr)->sin_addr);
322 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
323 &((struct sockaddr_in *)&tconn->peer_addr)->sin_addr);
325 snprintf(envp[3], 20, "DRBD_PEER_AF=%s", afs);
328 int drbd_khelper(struct drbd_conf *mdev, char *cmd)
330 char *envp[] = { "HOME=/",
332 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
333 (char[20]) { }, /* address family */
334 (char[60]) { }, /* address */
337 char *argv[] = {usermode_helper, cmd, mb, NULL };
341 snprintf(mb, 12, "minor-%d", mdev_to_minor(mdev));
342 setup_khelper_env(mdev->tconn, envp);
344 /* The helper may take some time.
345 * write out any unsynced meta data changes now */
348 dev_info(DEV, "helper command: %s %s %s\n", usermode_helper, cmd, mb);
349 sib.sib_reason = SIB_HELPER_PRE;
350 sib.helper_name = cmd;
351 drbd_bcast_event(mdev, &sib);
352 ret = call_usermodehelper(usermode_helper, argv, envp, 1);
354 dev_warn(DEV, "helper command: %s %s %s exit code %u (0x%x)\n",
355 usermode_helper, cmd, mb,
356 (ret >> 8) & 0xff, ret);
358 dev_info(DEV, "helper command: %s %s %s exit code %u (0x%x)\n",
359 usermode_helper, cmd, mb,
360 (ret >> 8) & 0xff, ret);
361 sib.sib_reason = SIB_HELPER_POST;
362 sib.helper_exit_code = ret;
363 drbd_bcast_event(mdev, &sib);
365 if (ret < 0) /* Ignore any ERRNOs we got. */
371 static void conn_md_sync(struct drbd_tconn *tconn)
373 struct drbd_conf *mdev;
377 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
378 kref_get(&mdev->kref);
381 kref_put(&mdev->kref, &drbd_minor_destroy);
387 int conn_khelper(struct drbd_tconn *tconn, char *cmd)
389 char *envp[] = { "HOME=/",
391 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
392 (char[20]) { }, /* address family */
393 (char[60]) { }, /* address */
395 char *argv[] = {usermode_helper, cmd, tconn->name, NULL };
398 setup_khelper_env(tconn, envp);
401 conn_info(tconn, "helper command: %s %s %s\n", usermode_helper, cmd, tconn->name);
402 /* TODO: conn_bcast_event() ?? */
404 ret = call_usermodehelper(usermode_helper, argv, envp, 1);
406 conn_warn(tconn, "helper command: %s %s %s exit code %u (0x%x)\n",
407 usermode_helper, cmd, tconn->name,
408 (ret >> 8) & 0xff, ret);
410 conn_info(tconn, "helper command: %s %s %s exit code %u (0x%x)\n",
411 usermode_helper, cmd, tconn->name,
412 (ret >> 8) & 0xff, ret);
413 /* TODO: conn_bcast_event() ?? */
415 if (ret < 0) /* Ignore any ERRNOs we got. */
421 static enum drbd_fencing_p highest_fencing_policy(struct drbd_tconn *tconn)
423 enum drbd_fencing_p fp = FP_NOT_AVAIL;
424 struct drbd_conf *mdev;
428 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
429 if (get_ldev_if_state(mdev, D_CONSISTENT)) {
430 fp = max_t(enum drbd_fencing_p, fp,
431 rcu_dereference(mdev->ldev->disk_conf)->fencing);
440 bool conn_try_outdate_peer(struct drbd_tconn *tconn)
442 union drbd_state mask = { };
443 union drbd_state val = { };
444 enum drbd_fencing_p fp;
448 if (tconn->cstate >= C_WF_REPORT_PARAMS) {
449 conn_err(tconn, "Expected cstate < C_WF_REPORT_PARAMS\n");
453 fp = highest_fencing_policy(tconn);
456 conn_warn(tconn, "Not fencing peer, I'm not even Consistent myself.\n");
463 r = conn_khelper(tconn, "fence-peer");
465 switch ((r>>8) & 0xff) {
466 case 3: /* peer is inconsistent */
467 ex_to_string = "peer is inconsistent or worse";
469 val.pdsk = D_INCONSISTENT;
471 case 4: /* peer got outdated, or was already outdated */
472 ex_to_string = "peer was fenced";
474 val.pdsk = D_OUTDATED;
476 case 5: /* peer was down */
477 if (conn_highest_disk(tconn) == D_UP_TO_DATE) {
478 /* we will(have) create(d) a new UUID anyways... */
479 ex_to_string = "peer is unreachable, assumed to be dead";
481 val.pdsk = D_OUTDATED;
483 ex_to_string = "peer unreachable, doing nothing since disk != UpToDate";
486 case 6: /* Peer is primary, voluntarily outdate myself.
487 * This is useful when an unconnected R_SECONDARY is asked to
488 * become R_PRIMARY, but finds the other peer being active. */
489 ex_to_string = "peer is active";
490 conn_warn(tconn, "Peer is primary, outdating myself.\n");
492 val.disk = D_OUTDATED;
495 if (fp != FP_STONITH)
496 conn_err(tconn, "fence-peer() = 7 && fencing != Stonith !!!\n");
497 ex_to_string = "peer was stonithed";
499 val.pdsk = D_OUTDATED;
502 /* The script is broken ... */
503 conn_err(tconn, "fence-peer helper broken, returned %d\n", (r>>8)&0xff);
504 return false; /* Eventually leave IO frozen */
507 conn_info(tconn, "fence-peer helper returned %d (%s)\n",
508 (r>>8) & 0xff, ex_to_string);
513 conn_request_state(tconn, mask, val, CS_VERBOSE);
514 here, because we might were able to re-establish the connection in the
516 spin_lock_irq(&tconn->req_lock);
517 if (tconn->cstate < C_WF_REPORT_PARAMS)
518 _conn_request_state(tconn, mask, val, CS_VERBOSE);
519 spin_unlock_irq(&tconn->req_lock);
521 return conn_highest_pdsk(tconn) <= D_OUTDATED;
524 static int _try_outdate_peer_async(void *data)
526 struct drbd_tconn *tconn = (struct drbd_tconn *)data;
528 conn_try_outdate_peer(tconn);
530 kref_put(&tconn->kref, &conn_destroy);
534 void conn_try_outdate_peer_async(struct drbd_tconn *tconn)
536 struct task_struct *opa;
538 kref_get(&tconn->kref);
539 opa = kthread_run(_try_outdate_peer_async, tconn, "drbd_async_h");
541 conn_err(tconn, "out of mem, failed to invoke fence-peer helper\n");
542 kref_put(&tconn->kref, &conn_destroy);
547 drbd_set_role(struct drbd_conf *mdev, enum drbd_role new_role, int force)
549 const int max_tries = 4;
550 enum drbd_state_rv rv = SS_UNKNOWN_ERROR;
554 union drbd_state mask, val;
556 if (new_role == R_PRIMARY)
557 request_ping(mdev->tconn); /* Detect a dead peer ASAP */
559 mutex_lock(mdev->state_mutex);
561 mask.i = 0; mask.role = R_MASK;
562 val.i = 0; val.role = new_role;
564 while (try++ < max_tries) {
565 rv = _drbd_request_state(mdev, mask, val, CS_WAIT_COMPLETE);
567 /* in case we first succeeded to outdate,
568 * but now suddenly could establish a connection */
569 if (rv == SS_CW_FAILED_BY_PEER && mask.pdsk != 0) {
575 if (rv == SS_NO_UP_TO_DATE_DISK && force &&
576 (mdev->state.disk < D_UP_TO_DATE &&
577 mdev->state.disk >= D_INCONSISTENT)) {
579 val.disk = D_UP_TO_DATE;
584 if (rv == SS_NO_UP_TO_DATE_DISK &&
585 mdev->state.disk == D_CONSISTENT && mask.pdsk == 0) {
586 D_ASSERT(mdev->state.pdsk == D_UNKNOWN);
588 if (conn_try_outdate_peer(mdev->tconn)) {
589 val.disk = D_UP_TO_DATE;
595 if (rv == SS_NOTHING_TO_DO)
597 if (rv == SS_PRIMARY_NOP && mask.pdsk == 0) {
598 if (!conn_try_outdate_peer(mdev->tconn) && force) {
599 dev_warn(DEV, "Forced into split brain situation!\n");
601 val.pdsk = D_OUTDATED;
606 if (rv == SS_TWO_PRIMARIES) {
607 /* Maybe the peer is detected as dead very soon...
608 retry at most once more in this case. */
611 nc = rcu_dereference(mdev->tconn->net_conf);
612 timeo = nc ? (nc->ping_timeo + 1) * HZ / 10 : 1;
614 schedule_timeout_interruptible(timeo);
619 if (rv < SS_SUCCESS) {
620 rv = _drbd_request_state(mdev, mask, val,
621 CS_VERBOSE + CS_WAIT_COMPLETE);
632 dev_warn(DEV, "Forced to consider local data as UpToDate!\n");
634 /* Wait until nothing is on the fly :) */
635 wait_event(mdev->misc_wait, atomic_read(&mdev->ap_pending_cnt) == 0);
637 if (new_role == R_SECONDARY) {
638 set_disk_ro(mdev->vdisk, true);
639 if (get_ldev(mdev)) {
640 mdev->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
644 mutex_lock(&mdev->tconn->conf_update);
645 nc = mdev->tconn->net_conf;
647 nc->discard_my_data = 0; /* without copy; single bit op is atomic */
648 mutex_unlock(&mdev->tconn->conf_update);
650 set_disk_ro(mdev->vdisk, false);
651 if (get_ldev(mdev)) {
652 if (((mdev->state.conn < C_CONNECTED ||
653 mdev->state.pdsk <= D_FAILED)
654 && mdev->ldev->md.uuid[UI_BITMAP] == 0) || forced)
655 drbd_uuid_new_current(mdev);
657 mdev->ldev->md.uuid[UI_CURRENT] |= (u64)1;
662 /* writeout of activity log covered areas of the bitmap
663 * to stable storage done in after state change already */
665 if (mdev->state.conn >= C_WF_REPORT_PARAMS) {
666 /* if this was forced, we should consider sync */
668 drbd_send_uuids(mdev);
669 drbd_send_state(mdev);
674 kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
676 mutex_unlock(mdev->state_mutex);
680 static const char *from_attrs_err_to_txt(int err)
682 return err == -ENOMSG ? "required attribute missing" :
683 err == -EOPNOTSUPP ? "unknown mandatory attribute" :
684 err == -EEXIST ? "can not change invariant setting" :
685 "invalid attribute value";
688 int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info)
690 struct set_role_parms parms;
692 enum drbd_ret_code retcode;
694 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
695 if (!adm_ctx.reply_skb)
697 if (retcode != NO_ERROR)
700 memset(&parms, 0, sizeof(parms));
701 if (info->attrs[DRBD_NLA_SET_ROLE_PARMS]) {
702 err = set_role_parms_from_attrs(&parms, info);
704 retcode = ERR_MANDATORY_TAG;
705 drbd_msg_put_info(from_attrs_err_to_txt(err));
710 if (info->genlhdr->cmd == DRBD_ADM_PRIMARY)
711 retcode = drbd_set_role(adm_ctx.mdev, R_PRIMARY, parms.assume_uptodate);
713 retcode = drbd_set_role(adm_ctx.mdev, R_SECONDARY, 0);
715 drbd_adm_finish(info, retcode);
719 /* initializes the md.*_offset members, so we are able to find
720 * the on disk meta data */
721 static void drbd_md_set_sector_offsets(struct drbd_conf *mdev,
722 struct drbd_backing_dev *bdev)
724 sector_t md_size_sect = 0;
728 meta_dev_idx = rcu_dereference(bdev->disk_conf)->meta_dev_idx;
730 switch (meta_dev_idx) {
732 /* v07 style fixed size indexed meta data */
733 bdev->md.md_size_sect = MD_RESERVED_SECT;
734 bdev->md.md_offset = drbd_md_ss__(mdev, bdev);
735 bdev->md.al_offset = MD_AL_OFFSET;
736 bdev->md.bm_offset = MD_BM_OFFSET;
738 case DRBD_MD_INDEX_FLEX_EXT:
739 /* just occupy the full device; unit: sectors */
740 bdev->md.md_size_sect = drbd_get_capacity(bdev->md_bdev);
741 bdev->md.md_offset = 0;
742 bdev->md.al_offset = MD_AL_OFFSET;
743 bdev->md.bm_offset = MD_BM_OFFSET;
745 case DRBD_MD_INDEX_INTERNAL:
746 case DRBD_MD_INDEX_FLEX_INT:
747 bdev->md.md_offset = drbd_md_ss__(mdev, bdev);
748 /* al size is still fixed */
749 bdev->md.al_offset = -MD_AL_SECTORS;
750 /* we need (slightly less than) ~ this much bitmap sectors: */
751 md_size_sect = drbd_get_capacity(bdev->backing_bdev);
752 md_size_sect = ALIGN(md_size_sect, BM_SECT_PER_EXT);
753 md_size_sect = BM_SECT_TO_EXT(md_size_sect);
754 md_size_sect = ALIGN(md_size_sect, 8);
756 /* plus the "drbd meta data super block",
757 * and the activity log; */
758 md_size_sect += MD_BM_OFFSET;
760 bdev->md.md_size_sect = md_size_sect;
761 /* bitmap offset is adjusted by 'super' block size */
762 bdev->md.bm_offset = -md_size_sect + MD_AL_OFFSET;
768 /* input size is expected to be in KB */
769 char *ppsize(char *buf, unsigned long long size)
771 /* Needs 9 bytes at max including trailing NUL:
772 * -1ULL ==> "16384 EB" */
773 static char units[] = { 'K', 'M', 'G', 'T', 'P', 'E' };
775 while (size >= 10000 && base < sizeof(units)-1) {
777 size = (size >> 10) + !!(size & (1<<9));
780 sprintf(buf, "%u %cB", (unsigned)size, units[base]);
785 /* there is still a theoretical deadlock when called from receiver
786 * on an D_INCONSISTENT R_PRIMARY:
787 * remote READ does inc_ap_bio, receiver would need to receive answer
788 * packet from remote to dec_ap_bio again.
789 * receiver receive_sizes(), comes here,
790 * waits for ap_bio_cnt == 0. -> deadlock.
791 * but this cannot happen, actually, because:
792 * R_PRIMARY D_INCONSISTENT, and peer's disk is unreachable
793 * (not connected, or bad/no disk on peer):
794 * see drbd_fail_request_early, ap_bio_cnt is zero.
795 * R_PRIMARY D_INCONSISTENT, and C_SYNC_TARGET:
796 * peer may not initiate a resize.
798 /* Note these are not to be confused with
799 * drbd_adm_suspend_io/drbd_adm_resume_io,
800 * which are (sub) state changes triggered by admin (drbdsetup),
801 * and can be long lived.
802 * This changes an mdev->flag, is triggered by drbd internals,
803 * and should be short-lived. */
804 void drbd_suspend_io(struct drbd_conf *mdev)
806 set_bit(SUSPEND_IO, &mdev->flags);
807 if (drbd_suspended(mdev))
809 wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_bio_cnt));
812 void drbd_resume_io(struct drbd_conf *mdev)
814 clear_bit(SUSPEND_IO, &mdev->flags);
815 wake_up(&mdev->misc_wait);
819 * drbd_determine_dev_size() - Sets the right device size obeying all constraints
820 * @mdev: DRBD device.
822 * Returns 0 on success, negative return values indicate errors.
823 * You should call drbd_md_sync() after calling this function.
825 enum determine_dev_size drbd_determine_dev_size(struct drbd_conf *mdev, enum dds_flags flags) __must_hold(local)
827 sector_t prev_first_sect, prev_size; /* previous meta location */
828 sector_t la_size, u_size;
832 int md_moved, la_size_changed;
833 enum determine_dev_size rv = unchanged;
836 * application request passes inc_ap_bio,
837 * but then cannot get an AL-reference.
838 * this function later may wait on ap_bio_cnt == 0. -> deadlock.
841 * Suspend IO right here.
842 * still lock the act_log to not trigger ASSERTs there.
844 drbd_suspend_io(mdev);
846 /* no wait necessary anymore, actually we could assert that */
847 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
849 prev_first_sect = drbd_md_first_sector(mdev->ldev);
850 prev_size = mdev->ldev->md.md_size_sect;
851 la_size = mdev->ldev->md.la_size_sect;
853 /* TODO: should only be some assert here, not (re)init... */
854 drbd_md_set_sector_offsets(mdev, mdev->ldev);
857 u_size = rcu_dereference(mdev->ldev->disk_conf)->disk_size;
859 size = drbd_new_dev_size(mdev, mdev->ldev, u_size, flags & DDSF_FORCED);
861 if (drbd_get_capacity(mdev->this_bdev) != size ||
862 drbd_bm_capacity(mdev) != size) {
864 err = drbd_bm_resize(mdev, size, !(flags & DDSF_NO_RESYNC));
866 /* currently there is only one error: ENOMEM! */
867 size = drbd_bm_capacity(mdev)>>1;
869 dev_err(DEV, "OUT OF MEMORY! "
870 "Could not allocate bitmap!\n");
872 dev_err(DEV, "BM resizing failed. "
873 "Leaving size unchanged at size = %lu KB\n",
874 (unsigned long)size);
878 /* racy, see comments above. */
879 drbd_set_my_capacity(mdev, size);
880 mdev->ldev->md.la_size_sect = size;
881 dev_info(DEV, "size = %s (%llu KB)\n", ppsize(ppb, size>>1),
882 (unsigned long long)size>>1);
884 if (rv == dev_size_error)
887 la_size_changed = (la_size != mdev->ldev->md.la_size_sect);
889 md_moved = prev_first_sect != drbd_md_first_sector(mdev->ldev)
890 || prev_size != mdev->ldev->md.md_size_sect;
892 if (la_size_changed || md_moved) {
895 drbd_al_shrink(mdev); /* All extents inactive. */
896 dev_info(DEV, "Writing the whole bitmap, %s\n",
897 la_size_changed && md_moved ? "size changed and md moved" :
898 la_size_changed ? "size changed" : "md moved");
899 /* next line implicitly does drbd_suspend_io()+drbd_resume_io() */
900 err = drbd_bitmap_io(mdev, &drbd_bm_write,
901 "size changed", BM_LOCKED_MASK);
906 drbd_md_mark_dirty(mdev);
914 lc_unlock(mdev->act_log);
915 wake_up(&mdev->al_wait);
916 drbd_resume_io(mdev);
922 drbd_new_dev_size(struct drbd_conf *mdev, struct drbd_backing_dev *bdev,
923 sector_t u_size, int assume_peer_has_space)
925 sector_t p_size = mdev->p_size; /* partner's disk size. */
926 sector_t la_size = bdev->md.la_size_sect; /* last agreed size. */
927 sector_t m_size; /* my size */
930 m_size = drbd_get_max_capacity(bdev);
932 if (mdev->state.conn < C_CONNECTED && assume_peer_has_space) {
933 dev_warn(DEV, "Resize while not connected was forced by the user!\n");
937 if (p_size && m_size) {
938 size = min_t(sector_t, p_size, m_size);
942 if (m_size && m_size < size)
944 if (p_size && p_size < size)
955 dev_err(DEV, "Both nodes diskless!\n");
959 dev_err(DEV, "Requested disk size is too big (%lu > %lu)\n",
960 (unsigned long)u_size>>1, (unsigned long)size>>1);
969 * drbd_check_al_size() - Ensures that the AL is of the right size
970 * @mdev: DRBD device.
972 * Returns -EBUSY if current al lru is still used, -ENOMEM when allocation
973 * failed, and 0 on success. You should call drbd_md_sync() after you called
976 static int drbd_check_al_size(struct drbd_conf *mdev, struct disk_conf *dc)
978 struct lru_cache *n, *t;
979 struct lc_element *e;
984 mdev->act_log->nr_elements == dc->al_extents)
989 n = lc_create("act_log", drbd_al_ext_cache, AL_UPDATES_PER_TRANSACTION,
990 dc->al_extents, sizeof(struct lc_element), 0);
993 dev_err(DEV, "Cannot allocate act_log lru!\n");
996 spin_lock_irq(&mdev->al_lock);
998 for (i = 0; i < t->nr_elements; i++) {
999 e = lc_element_by_index(t, i);
1001 dev_err(DEV, "refcnt(%d)==%d\n",
1002 e->lc_number, e->refcnt);
1003 in_use += e->refcnt;
1008 spin_unlock_irq(&mdev->al_lock);
1010 dev_err(DEV, "Activity log still in use!\n");
1017 drbd_md_mark_dirty(mdev); /* we changed mdev->act_log->nr_elemens */
1021 static void drbd_setup_queue_param(struct drbd_conf *mdev, unsigned int max_bio_size)
1023 struct request_queue * const q = mdev->rq_queue;
1024 int max_hw_sectors = max_bio_size >> 9;
1025 int max_segments = 0;
1027 if (get_ldev_if_state(mdev, D_ATTACHING)) {
1028 struct request_queue * const b = mdev->ldev->backing_bdev->bd_disk->queue;
1030 max_hw_sectors = min(queue_max_hw_sectors(b), max_bio_size >> 9);
1032 max_segments = rcu_dereference(mdev->ldev->disk_conf)->max_bio_bvecs;
1037 blk_queue_logical_block_size(q, 512);
1038 blk_queue_max_hw_sectors(q, max_hw_sectors);
1039 /* This is the workaround for "bio would need to, but cannot, be split" */
1040 blk_queue_max_segments(q, max_segments ? max_segments : BLK_MAX_SEGMENTS);
1041 blk_queue_segment_boundary(q, PAGE_CACHE_SIZE-1);
1043 if (get_ldev_if_state(mdev, D_ATTACHING)) {
1044 struct request_queue * const b = mdev->ldev->backing_bdev->bd_disk->queue;
1046 blk_queue_stack_limits(q, b);
1048 if (q->backing_dev_info.ra_pages != b->backing_dev_info.ra_pages) {
1049 dev_info(DEV, "Adjusting my ra_pages to backing device's (%lu -> %lu)\n",
1050 q->backing_dev_info.ra_pages,
1051 b->backing_dev_info.ra_pages);
1052 q->backing_dev_info.ra_pages = b->backing_dev_info.ra_pages;
1058 void drbd_reconsider_max_bio_size(struct drbd_conf *mdev)
1060 int now, new, local, peer;
1062 now = queue_max_hw_sectors(mdev->rq_queue) << 9;
1063 local = mdev->local_max_bio_size; /* Eventually last known value, from volatile memory */
1064 peer = mdev->peer_max_bio_size; /* Eventually last known value, from meta data */
1066 if (get_ldev_if_state(mdev, D_ATTACHING)) {
1067 local = queue_max_hw_sectors(mdev->ldev->backing_bdev->bd_disk->queue) << 9;
1068 mdev->local_max_bio_size = local;
1072 /* We may ignore peer limits if the peer is modern enough.
1073 Because new from 8.3.8 onwards the peer can use multiple
1074 BIOs for a single peer_request */
1075 if (mdev->state.conn >= C_CONNECTED) {
1076 if (mdev->tconn->agreed_pro_version < 94)
1077 peer = mdev->peer_max_bio_size;
1078 else if (mdev->tconn->agreed_pro_version == 94)
1079 peer = DRBD_MAX_SIZE_H80_PACKET;
1080 else /* drbd 8.3.8 onwards */
1081 peer = DRBD_MAX_BIO_SIZE;
1084 new = min_t(int, local, peer);
1086 if (mdev->state.role == R_PRIMARY && new < now)
1087 dev_err(DEV, "ASSERT FAILED new < now; (%d < %d)\n", new, now);
1090 dev_info(DEV, "max BIO size = %u\n", new);
1092 drbd_setup_queue_param(mdev, new);
1095 /* Starts the worker thread */
1096 static void conn_reconfig_start(struct drbd_tconn *tconn)
1098 drbd_thread_start(&tconn->worker);
1099 conn_flush_workqueue(tconn);
1102 /* if still unconfigured, stops worker again. */
1103 static void conn_reconfig_done(struct drbd_tconn *tconn)
1106 spin_lock_irq(&tconn->req_lock);
1107 stop_threads = conn_all_vols_unconf(tconn);
1108 spin_unlock_irq(&tconn->req_lock);
1110 /* asender is implicitly stopped by receiver
1111 * in conn_disconnect() */
1112 drbd_thread_stop(&tconn->receiver);
1113 drbd_thread_stop(&tconn->worker);
1117 /* Make sure IO is suspended before calling this function(). */
1118 static void drbd_suspend_al(struct drbd_conf *mdev)
1122 if (!lc_try_lock(mdev->act_log)) {
1123 dev_warn(DEV, "Failed to lock al in drbd_suspend_al()\n");
1127 drbd_al_shrink(mdev);
1128 spin_lock_irq(&mdev->tconn->req_lock);
1129 if (mdev->state.conn < C_CONNECTED)
1130 s = !test_and_set_bit(AL_SUSPENDED, &mdev->flags);
1131 spin_unlock_irq(&mdev->tconn->req_lock);
1132 lc_unlock(mdev->act_log);
1135 dev_info(DEV, "Suspended AL updates\n");
1139 static bool should_set_defaults(struct genl_info *info)
1141 unsigned flags = ((struct drbd_genlmsghdr*)info->userhdr)->flags;
1142 return 0 != (flags & DRBD_GENL_F_SET_DEFAULTS);
1145 static void enforce_disk_conf_limits(struct disk_conf *dc)
1147 if (dc->al_extents < DRBD_AL_EXTENTS_MIN)
1148 dc->al_extents = DRBD_AL_EXTENTS_MIN;
1149 if (dc->al_extents > DRBD_AL_EXTENTS_MAX)
1150 dc->al_extents = DRBD_AL_EXTENTS_MAX;
1152 if (dc->c_plan_ahead > DRBD_C_PLAN_AHEAD_MAX)
1153 dc->c_plan_ahead = DRBD_C_PLAN_AHEAD_MAX;
1156 int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info)
1158 enum drbd_ret_code retcode;
1159 struct drbd_conf *mdev;
1160 struct disk_conf *new_disk_conf, *old_disk_conf;
1161 struct fifo_buffer *old_plan = NULL, *new_plan = NULL;
1164 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1165 if (!adm_ctx.reply_skb)
1167 if (retcode != NO_ERROR)
1170 mdev = adm_ctx.mdev;
1172 /* we also need a disk
1173 * to change the options on */
1174 if (!get_ldev(mdev)) {
1175 retcode = ERR_NO_DISK;
1179 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
1180 if (!new_disk_conf) {
1181 retcode = ERR_NOMEM;
1185 mutex_lock(&mdev->tconn->conf_update);
1186 old_disk_conf = mdev->ldev->disk_conf;
1187 *new_disk_conf = *old_disk_conf;
1188 if (should_set_defaults(info))
1189 set_disk_conf_defaults(new_disk_conf);
1191 err = disk_conf_from_attrs_for_change(new_disk_conf, info);
1192 if (err && err != -ENOMSG) {
1193 retcode = ERR_MANDATORY_TAG;
1194 drbd_msg_put_info(from_attrs_err_to_txt(err));
1197 if (!expect(new_disk_conf->resync_rate >= 1))
1198 new_disk_conf->resync_rate = 1;
1200 enforce_disk_conf_limits(new_disk_conf);
1202 fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
1203 if (fifo_size != mdev->rs_plan_s->size) {
1204 new_plan = fifo_alloc(fifo_size);
1206 dev_err(DEV, "kmalloc of fifo_buffer failed");
1207 retcode = ERR_NOMEM;
1212 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
1213 drbd_al_shrink(mdev);
1214 err = drbd_check_al_size(mdev, new_disk_conf);
1215 lc_unlock(mdev->act_log);
1216 wake_up(&mdev->al_wait);
1219 retcode = ERR_NOMEM;
1223 write_lock_irq(&global_state_lock);
1224 retcode = drbd_resync_after_valid(mdev, new_disk_conf->resync_after);
1225 if (retcode == NO_ERROR) {
1226 rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
1227 drbd_resync_after_changed(mdev);
1229 write_unlock_irq(&global_state_lock);
1231 if (retcode != NO_ERROR)
1235 old_plan = mdev->rs_plan_s;
1236 rcu_assign_pointer(mdev->rs_plan_s, new_plan);
1239 mutex_unlock(&mdev->tconn->conf_update);
1242 if (mdev->state.conn >= C_CONNECTED)
1243 drbd_send_sync_param(mdev);
1246 kfree(old_disk_conf);
1251 mutex_unlock(&mdev->tconn->conf_update);
1253 kfree(new_disk_conf);
1258 drbd_adm_finish(info, retcode);
1262 int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info)
1264 struct drbd_conf *mdev;
1266 enum drbd_ret_code retcode;
1267 enum determine_dev_size dd;
1268 sector_t max_possible_sectors;
1269 sector_t min_md_device_sectors;
1270 struct drbd_backing_dev *nbc = NULL; /* new_backing_conf */
1271 struct disk_conf *new_disk_conf = NULL;
1272 struct block_device *bdev;
1273 struct lru_cache *resync_lru = NULL;
1274 struct fifo_buffer *new_plan = NULL;
1275 union drbd_state ns, os;
1276 enum drbd_state_rv rv;
1277 struct net_conf *nc;
1278 int cp_discovered = 0;
1280 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1281 if (!adm_ctx.reply_skb)
1283 if (retcode != NO_ERROR)
1286 mdev = adm_ctx.mdev;
1287 conn_reconfig_start(mdev->tconn);
1289 /* if you want to reconfigure, please tear down first */
1290 if (mdev->state.disk > D_DISKLESS) {
1291 retcode = ERR_DISK_CONFIGURED;
1294 /* It may just now have detached because of IO error. Make sure
1295 * drbd_ldev_destroy is done already, we may end up here very fast,
1296 * e.g. if someone calls attach from the on-io-error handler,
1297 * to realize a "hot spare" feature (not that I'd recommend that) */
1298 wait_event(mdev->misc_wait, !atomic_read(&mdev->local_cnt));
1300 /* allocation not in the IO path, drbdsetup context */
1301 nbc = kzalloc(sizeof(struct drbd_backing_dev), GFP_KERNEL);
1303 retcode = ERR_NOMEM;
1306 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
1307 if (!new_disk_conf) {
1308 retcode = ERR_NOMEM;
1311 nbc->disk_conf = new_disk_conf;
1313 set_disk_conf_defaults(new_disk_conf);
1314 err = disk_conf_from_attrs(new_disk_conf, info);
1316 retcode = ERR_MANDATORY_TAG;
1317 drbd_msg_put_info(from_attrs_err_to_txt(err));
1321 enforce_disk_conf_limits(new_disk_conf);
1323 new_plan = fifo_alloc((new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ);
1325 retcode = ERR_NOMEM;
1329 if (new_disk_conf->meta_dev_idx < DRBD_MD_INDEX_FLEX_INT) {
1330 retcode = ERR_MD_IDX_INVALID;
1335 nc = rcu_dereference(mdev->tconn->net_conf);
1337 if (new_disk_conf->fencing == FP_STONITH && nc->wire_protocol == DRBD_PROT_A) {
1339 retcode = ERR_STONITH_AND_PROT_A;
1345 bdev = blkdev_get_by_path(new_disk_conf->backing_dev,
1346 FMODE_READ | FMODE_WRITE | FMODE_EXCL, mdev);
1348 dev_err(DEV, "open(\"%s\") failed with %ld\n", new_disk_conf->backing_dev,
1350 retcode = ERR_OPEN_DISK;
1353 nbc->backing_bdev = bdev;
1356 * meta_dev_idx >= 0: external fixed size, possibly multiple
1357 * drbd sharing one meta device. TODO in that case, paranoia
1358 * check that [md_bdev, meta_dev_idx] is not yet used by some
1359 * other drbd minor! (if you use drbd.conf + drbdadm, that
1360 * should check it for you already; but if you don't, or
1361 * someone fooled it, we need to double check here)
1363 bdev = blkdev_get_by_path(new_disk_conf->meta_dev,
1364 FMODE_READ | FMODE_WRITE | FMODE_EXCL,
1365 (new_disk_conf->meta_dev_idx < 0) ?
1366 (void *)mdev : (void *)drbd_m_holder);
1368 dev_err(DEV, "open(\"%s\") failed with %ld\n", new_disk_conf->meta_dev,
1370 retcode = ERR_OPEN_MD_DISK;
1373 nbc->md_bdev = bdev;
1375 if ((nbc->backing_bdev == nbc->md_bdev) !=
1376 (new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_INTERNAL ||
1377 new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_FLEX_INT)) {
1378 retcode = ERR_MD_IDX_INVALID;
1382 resync_lru = lc_create("resync", drbd_bm_ext_cache,
1383 1, 61, sizeof(struct bm_extent),
1384 offsetof(struct bm_extent, lce));
1386 retcode = ERR_NOMEM;
1390 /* RT - for drbd_get_max_capacity() DRBD_MD_INDEX_FLEX_INT */
1391 drbd_md_set_sector_offsets(mdev, nbc);
1393 if (drbd_get_max_capacity(nbc) < new_disk_conf->disk_size) {
1394 dev_err(DEV, "max capacity %llu smaller than disk size %llu\n",
1395 (unsigned long long) drbd_get_max_capacity(nbc),
1396 (unsigned long long) new_disk_conf->disk_size);
1397 retcode = ERR_DISK_TOO_SMALL;
1401 if (new_disk_conf->meta_dev_idx < 0) {
1402 max_possible_sectors = DRBD_MAX_SECTORS_FLEX;
1403 /* at least one MB, otherwise it does not make sense */
1404 min_md_device_sectors = (2<<10);
1406 max_possible_sectors = DRBD_MAX_SECTORS;
1407 min_md_device_sectors = MD_RESERVED_SECT * (new_disk_conf->meta_dev_idx + 1);
1410 if (drbd_get_capacity(nbc->md_bdev) < min_md_device_sectors) {
1411 retcode = ERR_MD_DISK_TOO_SMALL;
1412 dev_warn(DEV, "refusing attach: md-device too small, "
1413 "at least %llu sectors needed for this meta-disk type\n",
1414 (unsigned long long) min_md_device_sectors);
1418 /* Make sure the new disk is big enough
1419 * (we may currently be R_PRIMARY with no local disk...) */
1420 if (drbd_get_max_capacity(nbc) <
1421 drbd_get_capacity(mdev->this_bdev)) {
1422 retcode = ERR_DISK_TOO_SMALL;
1426 nbc->known_size = drbd_get_capacity(nbc->backing_bdev);
1428 if (nbc->known_size > max_possible_sectors) {
1429 dev_warn(DEV, "==> truncating very big lower level device "
1430 "to currently maximum possible %llu sectors <==\n",
1431 (unsigned long long) max_possible_sectors);
1432 if (new_disk_conf->meta_dev_idx >= 0)
1433 dev_warn(DEV, "==>> using internal or flexible "
1434 "meta data may help <<==\n");
1437 drbd_suspend_io(mdev);
1438 /* also wait for the last barrier ack. */
1439 wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_pending_cnt) || drbd_suspended(mdev));
1440 /* and for any other previously queued work */
1441 drbd_flush_workqueue(mdev);
1443 rv = _drbd_request_state(mdev, NS(disk, D_ATTACHING), CS_VERBOSE);
1444 retcode = rv; /* FIXME: Type mismatch. */
1445 drbd_resume_io(mdev);
1446 if (rv < SS_SUCCESS)
1449 if (!get_ldev_if_state(mdev, D_ATTACHING))
1450 goto force_diskless;
1452 drbd_md_set_sector_offsets(mdev, nbc);
1454 if (!mdev->bitmap) {
1455 if (drbd_bm_init(mdev)) {
1456 retcode = ERR_NOMEM;
1457 goto force_diskless_dec;
1461 retcode = drbd_md_read(mdev, nbc);
1462 if (retcode != NO_ERROR)
1463 goto force_diskless_dec;
1465 if (mdev->state.conn < C_CONNECTED &&
1466 mdev->state.role == R_PRIMARY &&
1467 (mdev->ed_uuid & ~((u64)1)) != (nbc->md.uuid[UI_CURRENT] & ~((u64)1))) {
1468 dev_err(DEV, "Can only attach to data with current UUID=%016llX\n",
1469 (unsigned long long)mdev->ed_uuid);
1470 retcode = ERR_DATA_NOT_CURRENT;
1471 goto force_diskless_dec;
1474 /* Since we are diskless, fix the activity log first... */
1475 if (drbd_check_al_size(mdev, new_disk_conf)) {
1476 retcode = ERR_NOMEM;
1477 goto force_diskless_dec;
1480 /* Prevent shrinking of consistent devices ! */
1481 if (drbd_md_test_flag(nbc, MDF_CONSISTENT) &&
1482 drbd_new_dev_size(mdev, nbc, nbc->disk_conf->disk_size, 0) < nbc->md.la_size_sect) {
1483 dev_warn(DEV, "refusing to truncate a consistent device\n");
1484 retcode = ERR_DISK_TOO_SMALL;
1485 goto force_diskless_dec;
1488 if (!drbd_al_read_log(mdev, nbc)) {
1489 retcode = ERR_IO_MD_DISK;
1490 goto force_diskless_dec;
1493 /* Reset the "barriers don't work" bits here, then force meta data to
1494 * be written, to ensure we determine if barriers are supported. */
1495 if (new_disk_conf->md_flushes)
1496 clear_bit(MD_NO_FUA, &mdev->flags);
1498 set_bit(MD_NO_FUA, &mdev->flags);
1500 /* Point of no return reached.
1501 * Devices and memory are no longer released by error cleanup below.
1502 * now mdev takes over responsibility, and the state engine should
1503 * clean it up somewhere. */
1504 D_ASSERT(mdev->ldev == NULL);
1506 mdev->resync = resync_lru;
1507 mdev->rs_plan_s = new_plan;
1510 new_disk_conf = NULL;
1513 mdev->write_ordering = WO_bdev_flush;
1514 drbd_bump_write_ordering(mdev, WO_bdev_flush);
1516 if (drbd_md_test_flag(mdev->ldev, MDF_CRASHED_PRIMARY))
1517 set_bit(CRASHED_PRIMARY, &mdev->flags);
1519 clear_bit(CRASHED_PRIMARY, &mdev->flags);
1521 if (drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) &&
1522 !(mdev->state.role == R_PRIMARY && mdev->tconn->susp_nod)) {
1523 set_bit(CRASHED_PRIMARY, &mdev->flags);
1532 drbd_reconsider_max_bio_size(mdev);
1534 /* If I am currently not R_PRIMARY,
1535 * but meta data primary indicator is set,
1536 * I just now recover from a hard crash,
1537 * and have been R_PRIMARY before that crash.
1539 * Now, if I had no connection before that crash
1540 * (have been degraded R_PRIMARY), chances are that
1541 * I won't find my peer now either.
1543 * In that case, and _only_ in that case,
1544 * we use the degr-wfc-timeout instead of the default,
1545 * so we can automatically recover from a crash of a
1546 * degraded but active "cluster" after a certain timeout.
1548 clear_bit(USE_DEGR_WFC_T, &mdev->flags);
1549 if (mdev->state.role != R_PRIMARY &&
1550 drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) &&
1551 !drbd_md_test_flag(mdev->ldev, MDF_CONNECTED_IND))
1552 set_bit(USE_DEGR_WFC_T, &mdev->flags);
1554 dd = drbd_determine_dev_size(mdev, 0);
1555 if (dd == dev_size_error) {
1556 retcode = ERR_NOMEM_BITMAP;
1557 goto force_diskless_dec;
1558 } else if (dd == grew)
1559 set_bit(RESYNC_AFTER_NEG, &mdev->flags);
1561 if (drbd_md_test_flag(mdev->ldev, MDF_FULL_SYNC)) {
1562 dev_info(DEV, "Assuming that all blocks are out of sync "
1563 "(aka FullSync)\n");
1564 if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write,
1565 "set_n_write from attaching", BM_LOCKED_MASK)) {
1566 retcode = ERR_IO_MD_DISK;
1567 goto force_diskless_dec;
1570 if (drbd_bitmap_io(mdev, &drbd_bm_read,
1571 "read from attaching", BM_LOCKED_MASK)) {
1572 retcode = ERR_IO_MD_DISK;
1573 goto force_diskless_dec;
1577 if (cp_discovered) {
1578 drbd_al_apply_to_bm(mdev);
1579 if (drbd_bitmap_io(mdev, &drbd_bm_write,
1580 "crashed primary apply AL", BM_LOCKED_MASK)) {
1581 retcode = ERR_IO_MD_DISK;
1582 goto force_diskless_dec;
1586 if (_drbd_bm_total_weight(mdev) == drbd_bm_bits(mdev))
1587 drbd_suspend_al(mdev); /* IO is still suspended here... */
1589 spin_lock_irq(&mdev->tconn->req_lock);
1590 os = drbd_read_state(mdev);
1592 /* If MDF_CONSISTENT is not set go into inconsistent state,
1593 otherwise investigate MDF_WasUpToDate...
1594 If MDF_WAS_UP_TO_DATE is not set go into D_OUTDATED disk state,
1595 otherwise into D_CONSISTENT state.
1597 if (drbd_md_test_flag(mdev->ldev, MDF_CONSISTENT)) {
1598 if (drbd_md_test_flag(mdev->ldev, MDF_WAS_UP_TO_DATE))
1599 ns.disk = D_CONSISTENT;
1601 ns.disk = D_OUTDATED;
1603 ns.disk = D_INCONSISTENT;
1606 if (drbd_md_test_flag(mdev->ldev, MDF_PEER_OUT_DATED))
1607 ns.pdsk = D_OUTDATED;
1610 if (ns.disk == D_CONSISTENT &&
1611 (ns.pdsk == D_OUTDATED || rcu_dereference(mdev->ldev->disk_conf)->fencing == FP_DONT_CARE))
1612 ns.disk = D_UP_TO_DATE;
1615 /* All tests on MDF_PRIMARY_IND, MDF_CONNECTED_IND,
1616 MDF_CONSISTENT and MDF_WAS_UP_TO_DATE must happen before
1617 this point, because drbd_request_state() modifies these
1620 /* In case we are C_CONNECTED postpone any decision on the new disk
1621 state after the negotiation phase. */
1622 if (mdev->state.conn == C_CONNECTED) {
1623 mdev->new_state_tmp.i = ns.i;
1625 ns.disk = D_NEGOTIATING;
1627 /* We expect to receive up-to-date UUIDs soon.
1628 To avoid a race in receive_state, free p_uuid while
1629 holding req_lock. I.e. atomic with the state change */
1630 kfree(mdev->p_uuid);
1631 mdev->p_uuid = NULL;
1634 rv = _drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
1635 spin_unlock_irq(&mdev->tconn->req_lock);
1637 if (rv < SS_SUCCESS)
1638 goto force_diskless_dec;
1640 if (mdev->state.role == R_PRIMARY)
1641 mdev->ldev->md.uuid[UI_CURRENT] |= (u64)1;
1643 mdev->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
1645 drbd_md_mark_dirty(mdev);
1648 kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
1650 conn_reconfig_done(mdev->tconn);
1651 drbd_adm_finish(info, retcode);
1657 drbd_force_state(mdev, NS(disk, D_FAILED));
1660 conn_reconfig_done(mdev->tconn);
1662 if (nbc->backing_bdev)
1663 blkdev_put(nbc->backing_bdev,
1664 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1666 blkdev_put(nbc->md_bdev,
1667 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1670 kfree(new_disk_conf);
1671 lc_destroy(resync_lru);
1675 drbd_adm_finish(info, retcode);
1679 static int adm_detach(struct drbd_conf *mdev)
1681 enum drbd_state_rv retcode;
1683 drbd_suspend_io(mdev); /* so no-one is stuck in drbd_al_begin_io */
1684 retcode = drbd_request_state(mdev, NS(disk, D_FAILED));
1685 /* D_FAILED will transition to DISKLESS. */
1686 ret = wait_event_interruptible(mdev->misc_wait,
1687 mdev->state.disk != D_FAILED);
1688 drbd_resume_io(mdev);
1689 if ((int)retcode == (int)SS_IS_DISKLESS)
1690 retcode = SS_NOTHING_TO_DO;
1696 /* Detaching the disk is a process in multiple stages. First we need to lock
1697 * out application IO, in-flight IO, IO stuck in drbd_al_begin_io.
1698 * Then we transition to D_DISKLESS, and wait for put_ldev() to return all
1699 * internal references as well.
1700 * Only then we have finally detached. */
1701 int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info)
1703 enum drbd_ret_code retcode;
1705 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1706 if (!adm_ctx.reply_skb)
1708 if (retcode != NO_ERROR)
1711 retcode = adm_detach(adm_ctx.mdev);
1713 drbd_adm_finish(info, retcode);
1717 static bool conn_resync_running(struct drbd_tconn *tconn)
1719 struct drbd_conf *mdev;
1724 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1725 if (mdev->state.conn == C_SYNC_SOURCE ||
1726 mdev->state.conn == C_SYNC_TARGET ||
1727 mdev->state.conn == C_PAUSED_SYNC_S ||
1728 mdev->state.conn == C_PAUSED_SYNC_T) {
1738 static bool conn_ov_running(struct drbd_tconn *tconn)
1740 struct drbd_conf *mdev;
1745 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1746 if (mdev->state.conn == C_VERIFY_S ||
1747 mdev->state.conn == C_VERIFY_T) {
1757 static enum drbd_ret_code
1758 _check_net_options(struct drbd_tconn *tconn, struct net_conf *old_conf, struct net_conf *new_conf)
1760 struct drbd_conf *mdev;
1763 if (old_conf && tconn->cstate == C_WF_REPORT_PARAMS && tconn->agreed_pro_version < 100) {
1764 if (new_conf->wire_protocol != old_conf->wire_protocol)
1765 return ERR_NEED_APV_100;
1767 if (new_conf->two_primaries != old_conf->two_primaries)
1768 return ERR_NEED_APV_100;
1770 if (!new_conf->integrity_alg != !old_conf->integrity_alg)
1771 return ERR_NEED_APV_100;
1773 if (strcmp(new_conf->integrity_alg, old_conf->integrity_alg))
1774 return ERR_NEED_APV_100;
1777 if (!new_conf->two_primaries &&
1778 conn_highest_role(tconn) == R_PRIMARY &&
1779 conn_highest_peer(tconn) == R_PRIMARY)
1780 return ERR_NEED_ALLOW_TWO_PRI;
1782 if (new_conf->two_primaries &&
1783 (new_conf->wire_protocol != DRBD_PROT_C))
1784 return ERR_NOT_PROTO_C;
1786 idr_for_each_entry(&tconn->volumes, mdev, i) {
1787 if (get_ldev(mdev)) {
1788 enum drbd_fencing_p fp = rcu_dereference(mdev->ldev->disk_conf)->fencing;
1790 if (new_conf->wire_protocol == DRBD_PROT_A && fp == FP_STONITH)
1791 return ERR_STONITH_AND_PROT_A;
1793 if (mdev->state.role == R_PRIMARY && new_conf->discard_my_data)
1797 if (new_conf->on_congestion != OC_BLOCK && new_conf->wire_protocol != DRBD_PROT_A)
1798 return ERR_CONG_NOT_PROTO_A;
1803 static enum drbd_ret_code
1804 check_net_options(struct drbd_tconn *tconn, struct net_conf *new_conf)
1806 static enum drbd_ret_code rv;
1807 struct drbd_conf *mdev;
1811 rv = _check_net_options(tconn, rcu_dereference(tconn->net_conf), new_conf);
1814 /* tconn->volumes protected by genl_lock() here */
1815 idr_for_each_entry(&tconn->volumes, mdev, i) {
1816 if (!mdev->bitmap) {
1817 if(drbd_bm_init(mdev))
1826 struct crypto_hash *verify_tfm;
1827 struct crypto_hash *csums_tfm;
1828 struct crypto_hash *cram_hmac_tfm;
1829 struct crypto_hash *integrity_tfm;
1835 alloc_hash(struct crypto_hash **tfm, char *tfm_name, int err_alg)
1840 *tfm = crypto_alloc_hash(tfm_name, 0, CRYPTO_ALG_ASYNC);
1849 static enum drbd_ret_code
1850 alloc_crypto(struct crypto *crypto, struct net_conf *new_conf)
1852 char hmac_name[CRYPTO_MAX_ALG_NAME];
1853 enum drbd_ret_code rv;
1856 rv = alloc_hash(&crypto->csums_tfm, new_conf->csums_alg,
1860 rv = alloc_hash(&crypto->verify_tfm, new_conf->verify_alg,
1864 rv = alloc_hash(&crypto->integrity_tfm, new_conf->integrity_alg,
1868 if (new_conf->cram_hmac_alg[0] != 0) {
1869 snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)",
1870 new_conf->cram_hmac_alg);
1872 rv = alloc_hash(&crypto->cram_hmac_tfm, hmac_name,
1875 if (crypto->integrity_tfm) {
1876 hash_size = crypto_hash_digestsize(crypto->integrity_tfm);
1877 crypto->int_dig_in = kmalloc(hash_size, GFP_KERNEL);
1878 if (!crypto->int_dig_in)
1880 crypto->int_dig_vv = kmalloc(hash_size, GFP_KERNEL);
1881 if (!crypto->int_dig_vv)
1888 static void free_crypto(struct crypto *crypto)
1890 kfree(crypto->int_dig_in);
1891 kfree(crypto->int_dig_vv);
1892 crypto_free_hash(crypto->cram_hmac_tfm);
1893 crypto_free_hash(crypto->integrity_tfm);
1894 crypto_free_hash(crypto->csums_tfm);
1895 crypto_free_hash(crypto->verify_tfm);
1898 int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info)
1900 enum drbd_ret_code retcode;
1901 struct drbd_tconn *tconn;
1902 struct net_conf *old_conf, *new_conf = NULL;
1904 int ovr; /* online verify running */
1905 int rsr; /* re-sync running */
1906 struct crypto crypto = { };
1908 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONNECTION);
1909 if (!adm_ctx.reply_skb)
1911 if (retcode != NO_ERROR)
1914 tconn = adm_ctx.tconn;
1916 new_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
1918 retcode = ERR_NOMEM;
1922 conn_reconfig_start(tconn);
1924 mutex_lock(&tconn->data.mutex);
1925 mutex_lock(&tconn->conf_update);
1926 old_conf = tconn->net_conf;
1929 drbd_msg_put_info("net conf missing, try connect");
1930 retcode = ERR_INVALID_REQUEST;
1934 *new_conf = *old_conf;
1935 if (should_set_defaults(info))
1936 set_net_conf_defaults(new_conf);
1938 err = net_conf_from_attrs_for_change(new_conf, info);
1939 if (err && err != -ENOMSG) {
1940 retcode = ERR_MANDATORY_TAG;
1941 drbd_msg_put_info(from_attrs_err_to_txt(err));
1945 retcode = check_net_options(tconn, new_conf);
1946 if (retcode != NO_ERROR)
1949 /* re-sync running */
1950 rsr = conn_resync_running(tconn);
1951 if (rsr && strcmp(new_conf->csums_alg, old_conf->csums_alg)) {
1952 retcode = ERR_CSUMS_RESYNC_RUNNING;
1956 /* online verify running */
1957 ovr = conn_ov_running(tconn);
1958 if (ovr && strcmp(new_conf->verify_alg, old_conf->verify_alg)) {
1959 retcode = ERR_VERIFY_RUNNING;
1963 retcode = alloc_crypto(&crypto, new_conf);
1964 if (retcode != NO_ERROR)
1967 rcu_assign_pointer(tconn->net_conf, new_conf);
1970 crypto_free_hash(tconn->csums_tfm);
1971 tconn->csums_tfm = crypto.csums_tfm;
1972 crypto.csums_tfm = NULL;
1975 crypto_free_hash(tconn->verify_tfm);
1976 tconn->verify_tfm = crypto.verify_tfm;
1977 crypto.verify_tfm = NULL;
1980 kfree(tconn->int_dig_in);
1981 tconn->int_dig_in = crypto.int_dig_in;
1982 kfree(tconn->int_dig_vv);
1983 tconn->int_dig_vv = crypto.int_dig_vv;
1984 crypto_free_hash(tconn->integrity_tfm);
1985 tconn->integrity_tfm = crypto.integrity_tfm;
1986 if (tconn->cstate >= C_WF_REPORT_PARAMS && tconn->agreed_pro_version >= 100)
1987 /* Do this without trying to take tconn->data.mutex again. */
1988 __drbd_send_protocol(tconn, P_PROTOCOL_UPDATE);
1990 crypto_free_hash(tconn->cram_hmac_tfm);
1991 tconn->cram_hmac_tfm = crypto.cram_hmac_tfm;
1993 mutex_unlock(&tconn->conf_update);
1994 mutex_unlock(&tconn->data.mutex);
1998 if (tconn->cstate >= C_WF_REPORT_PARAMS)
1999 drbd_send_sync_param(minor_to_mdev(conn_lowest_minor(tconn)));
2004 mutex_unlock(&tconn->conf_update);
2005 mutex_unlock(&tconn->data.mutex);
2006 free_crypto(&crypto);
2009 conn_reconfig_done(tconn);
2011 drbd_adm_finish(info, retcode);
2015 int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info)
2017 struct drbd_conf *mdev;
2018 struct net_conf *old_conf, *new_conf = NULL;
2019 struct crypto crypto = { };
2020 struct drbd_tconn *tconn;
2021 enum drbd_ret_code retcode;
2025 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
2027 if (!adm_ctx.reply_skb)
2029 if (retcode != NO_ERROR)
2031 if (!(adm_ctx.my_addr && adm_ctx.peer_addr)) {
2032 drbd_msg_put_info("connection endpoint(s) missing");
2033 retcode = ERR_INVALID_REQUEST;
2037 /* No need for _rcu here. All reconfiguration is
2038 * strictly serialized on genl_lock(). We are protected against
2039 * concurrent reconfiguration/addition/deletion */
2040 list_for_each_entry(tconn, &drbd_tconns, all_tconn) {
2041 if (nla_len(adm_ctx.my_addr) == tconn->my_addr_len &&
2042 !memcmp(nla_data(adm_ctx.my_addr), &tconn->my_addr, tconn->my_addr_len)) {
2043 retcode = ERR_LOCAL_ADDR;
2047 if (nla_len(adm_ctx.peer_addr) == tconn->peer_addr_len &&
2048 !memcmp(nla_data(adm_ctx.peer_addr), &tconn->peer_addr, tconn->peer_addr_len)) {
2049 retcode = ERR_PEER_ADDR;
2054 tconn = adm_ctx.tconn;
2055 conn_reconfig_start(tconn);
2057 if (tconn->cstate > C_STANDALONE) {
2058 retcode = ERR_NET_CONFIGURED;
2062 /* allocation not in the IO path, cqueue thread context */
2063 new_conf = kzalloc(sizeof(*new_conf), GFP_KERNEL);
2065 retcode = ERR_NOMEM;
2069 set_net_conf_defaults(new_conf);
2071 err = net_conf_from_attrs(new_conf, info);
2073 retcode = ERR_MANDATORY_TAG;
2074 drbd_msg_put_info(from_attrs_err_to_txt(err));
2078 retcode = check_net_options(tconn, new_conf);
2079 if (retcode != NO_ERROR)
2082 retcode = alloc_crypto(&crypto, new_conf);
2083 if (retcode != NO_ERROR)
2086 ((char *)new_conf->shared_secret)[SHARED_SECRET_MAX-1] = 0;
2088 conn_flush_workqueue(tconn);
2090 mutex_lock(&tconn->conf_update);
2091 old_conf = tconn->net_conf;
2093 retcode = ERR_NET_CONFIGURED;
2094 mutex_unlock(&tconn->conf_update);
2097 rcu_assign_pointer(tconn->net_conf, new_conf);
2099 conn_free_crypto(tconn);
2100 tconn->int_dig_in = crypto.int_dig_in;
2101 tconn->int_dig_vv = crypto.int_dig_vv;
2102 tconn->cram_hmac_tfm = crypto.cram_hmac_tfm;
2103 tconn->integrity_tfm = crypto.integrity_tfm;
2104 tconn->csums_tfm = crypto.csums_tfm;
2105 tconn->verify_tfm = crypto.verify_tfm;
2107 tconn->my_addr_len = nla_len(adm_ctx.my_addr);
2108 memcpy(&tconn->my_addr, nla_data(adm_ctx.my_addr), tconn->my_addr_len);
2109 tconn->peer_addr_len = nla_len(adm_ctx.peer_addr);
2110 memcpy(&tconn->peer_addr, nla_data(adm_ctx.peer_addr), tconn->peer_addr_len);
2112 mutex_unlock(&tconn->conf_update);
2115 idr_for_each_entry(&tconn->volumes, mdev, i) {
2121 retcode = conn_request_state(tconn, NS(conn, C_UNCONNECTED), CS_VERBOSE);
2123 conn_reconfig_done(tconn);
2124 drbd_adm_finish(info, retcode);
2128 free_crypto(&crypto);
2131 conn_reconfig_done(tconn);
2133 drbd_adm_finish(info, retcode);
2137 static enum drbd_state_rv conn_try_disconnect(struct drbd_tconn *tconn, bool force)
2139 enum drbd_state_rv rv;
2141 rv = conn_request_state(tconn, NS(conn, C_DISCONNECTING),
2142 force ? CS_HARD : 0);
2145 case SS_NOTHING_TO_DO:
2147 case SS_ALREADY_STANDALONE:
2149 case SS_PRIMARY_NOP:
2150 /* Our state checking code wants to see the peer outdated. */
2151 rv = conn_request_state(tconn, NS2(conn, C_DISCONNECTING,
2152 pdsk, D_OUTDATED), CS_VERBOSE);
2154 case SS_CW_FAILED_BY_PEER:
2155 /* The peer probably wants to see us outdated. */
2156 rv = conn_request_state(tconn, NS2(conn, C_DISCONNECTING,
2157 disk, D_OUTDATED), 0);
2158 if (rv == SS_IS_DISKLESS || rv == SS_LOWER_THAN_OUTDATED) {
2159 rv = conn_request_state(tconn, NS(conn, C_DISCONNECTING),
2164 /* no special handling necessary */
2167 if (rv >= SS_SUCCESS) {
2168 enum drbd_state_rv rv2;
2169 /* No one else can reconfigure the network while I am here.
2170 * The state handling only uses drbd_thread_stop_nowait(),
2171 * we want to really wait here until the receiver is no more.
2173 drbd_thread_stop(&adm_ctx.tconn->receiver);
2175 /* Race breaker. This additional state change request may be
2176 * necessary, if this was a forced disconnect during a receiver
2177 * restart. We may have "killed" the receiver thread just
2178 * after drbdd_init() returned. Typically, we should be
2179 * C_STANDALONE already, now, and this becomes a no-op.
2181 rv2 = conn_request_state(tconn, NS(conn, C_STANDALONE),
2182 CS_VERBOSE | CS_HARD);
2183 if (rv2 < SS_SUCCESS)
2185 "unexpected rv2=%d in conn_try_disconnect()\n",
2191 int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info)
2193 struct disconnect_parms parms;
2194 struct drbd_tconn *tconn;
2195 enum drbd_state_rv rv;
2196 enum drbd_ret_code retcode;
2199 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONNECTION);
2200 if (!adm_ctx.reply_skb)
2202 if (retcode != NO_ERROR)
2205 tconn = adm_ctx.tconn;
2206 memset(&parms, 0, sizeof(parms));
2207 if (info->attrs[DRBD_NLA_DISCONNECT_PARMS]) {
2208 err = disconnect_parms_from_attrs(&parms, info);
2210 retcode = ERR_MANDATORY_TAG;
2211 drbd_msg_put_info(from_attrs_err_to_txt(err));
2216 rv = conn_try_disconnect(tconn, parms.force_disconnect);
2217 if (rv < SS_SUCCESS)
2218 retcode = rv; /* FIXME: Type mismatch. */
2222 drbd_adm_finish(info, retcode);
2226 void resync_after_online_grow(struct drbd_conf *mdev)
2228 int iass; /* I am sync source */
2230 dev_info(DEV, "Resync of new storage after online grow\n");
2231 if (mdev->state.role != mdev->state.peer)
2232 iass = (mdev->state.role == R_PRIMARY);
2234 iass = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags);
2237 drbd_start_resync(mdev, C_SYNC_SOURCE);
2239 _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE + CS_SERIALIZE);
2242 int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info)
2244 struct disk_conf *old_disk_conf, *new_disk_conf = NULL;
2245 struct resize_parms rs;
2246 struct drbd_conf *mdev;
2247 enum drbd_ret_code retcode;
2248 enum determine_dev_size dd;
2249 enum dds_flags ddsf;
2253 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2254 if (!adm_ctx.reply_skb)
2256 if (retcode != NO_ERROR)
2259 memset(&rs, 0, sizeof(struct resize_parms));
2260 if (info->attrs[DRBD_NLA_RESIZE_PARMS]) {
2261 err = resize_parms_from_attrs(&rs, info);
2263 retcode = ERR_MANDATORY_TAG;
2264 drbd_msg_put_info(from_attrs_err_to_txt(err));
2269 mdev = adm_ctx.mdev;
2270 if (mdev->state.conn > C_CONNECTED) {
2271 retcode = ERR_RESIZE_RESYNC;
2275 if (mdev->state.role == R_SECONDARY &&
2276 mdev->state.peer == R_SECONDARY) {
2277 retcode = ERR_NO_PRIMARY;
2281 if (!get_ldev(mdev)) {
2282 retcode = ERR_NO_DISK;
2286 if (rs.no_resync && mdev->tconn->agreed_pro_version < 93) {
2287 retcode = ERR_NEED_APV_93;
2292 u_size = rcu_dereference(mdev->ldev->disk_conf)->disk_size;
2294 if (u_size != (sector_t)rs.resize_size) {
2295 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
2296 if (!new_disk_conf) {
2297 retcode = ERR_NOMEM;
2302 if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev))
2303 mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev);
2305 if (new_disk_conf) {
2306 mutex_lock(&mdev->tconn->conf_update);
2307 old_disk_conf = mdev->ldev->disk_conf;
2308 *new_disk_conf = *old_disk_conf;
2309 new_disk_conf->disk_size = (sector_t)rs.resize_size;
2310 rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
2311 mutex_unlock(&mdev->tconn->conf_update);
2313 kfree(old_disk_conf);
2316 ddsf = (rs.resize_force ? DDSF_FORCED : 0) | (rs.no_resync ? DDSF_NO_RESYNC : 0);
2317 dd = drbd_determine_dev_size(mdev, ddsf);
2320 if (dd == dev_size_error) {
2321 retcode = ERR_NOMEM_BITMAP;
2325 if (mdev->state.conn == C_CONNECTED) {
2327 set_bit(RESIZE_PENDING, &mdev->flags);
2329 drbd_send_uuids(mdev);
2330 drbd_send_sizes(mdev, 1, ddsf);
2334 drbd_adm_finish(info, retcode);
2338 int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info)
2340 enum drbd_ret_code retcode;
2341 struct drbd_tconn *tconn;
2342 struct res_opts res_opts;
2345 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
2346 if (!adm_ctx.reply_skb)
2348 if (retcode != NO_ERROR)
2350 tconn = adm_ctx.tconn;
2352 res_opts = tconn->res_opts;
2353 if (should_set_defaults(info))
2354 set_res_opts_defaults(&res_opts);
2356 err = res_opts_from_attrs(&res_opts, info);
2357 if (err && err != -ENOMSG) {
2358 retcode = ERR_MANDATORY_TAG;
2359 drbd_msg_put_info(from_attrs_err_to_txt(err));
2363 err = set_resource_options(tconn, &res_opts);
2365 retcode = ERR_INVALID_REQUEST;
2367 retcode = ERR_NOMEM;
2371 drbd_adm_finish(info, retcode);
2375 int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info)
2377 struct drbd_conf *mdev;
2378 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2380 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2381 if (!adm_ctx.reply_skb)
2383 if (retcode != NO_ERROR)
2386 mdev = adm_ctx.mdev;
2388 /* If there is still bitmap IO pending, probably because of a previous
2389 * resync just being finished, wait for it before requesting a new resync. */
2390 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
2392 retcode = _drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T), CS_ORDERED);
2394 if (retcode < SS_SUCCESS && retcode != SS_NEED_CONNECTION)
2395 retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T));
2397 while (retcode == SS_NEED_CONNECTION) {
2398 spin_lock_irq(&mdev->tconn->req_lock);
2399 if (mdev->state.conn < C_CONNECTED)
2400 retcode = _drbd_set_state(_NS(mdev, disk, D_INCONSISTENT), CS_VERBOSE, NULL);
2401 spin_unlock_irq(&mdev->tconn->req_lock);
2403 if (retcode != SS_NEED_CONNECTION)
2406 retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T));
2410 drbd_adm_finish(info, retcode);
2414 static int drbd_bmio_set_susp_al(struct drbd_conf *mdev)
2418 rv = drbd_bmio_set_n_write(mdev);
2419 drbd_suspend_al(mdev);
2423 static int drbd_adm_simple_request_state(struct sk_buff *skb, struct genl_info *info,
2424 union drbd_state mask, union drbd_state val)
2426 enum drbd_ret_code retcode;
2428 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2429 if (!adm_ctx.reply_skb)
2431 if (retcode != NO_ERROR)
2434 retcode = drbd_request_state(adm_ctx.mdev, mask, val);
2436 drbd_adm_finish(info, retcode);
2440 int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info)
2442 return drbd_adm_simple_request_state(skb, info, NS(conn, C_STARTING_SYNC_S));
2445 int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info)
2447 enum drbd_ret_code retcode;
2449 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2450 if (!adm_ctx.reply_skb)
2452 if (retcode != NO_ERROR)
2455 if (drbd_request_state(adm_ctx.mdev, NS(user_isp, 1)) == SS_NOTHING_TO_DO)
2456 retcode = ERR_PAUSE_IS_SET;
2458 drbd_adm_finish(info, retcode);
2462 int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info)
2464 union drbd_dev_state s;
2465 enum drbd_ret_code retcode;
2467 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2468 if (!adm_ctx.reply_skb)
2470 if (retcode != NO_ERROR)
2473 if (drbd_request_state(adm_ctx.mdev, NS(user_isp, 0)) == SS_NOTHING_TO_DO) {
2474 s = adm_ctx.mdev->state;
2475 if (s.conn == C_PAUSED_SYNC_S || s.conn == C_PAUSED_SYNC_T) {
2476 retcode = s.aftr_isp ? ERR_PIC_AFTER_DEP :
2477 s.peer_isp ? ERR_PIC_PEER_DEP : ERR_PAUSE_IS_CLEAR;
2479 retcode = ERR_PAUSE_IS_CLEAR;
2484 drbd_adm_finish(info, retcode);
2488 int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info)
2490 return drbd_adm_simple_request_state(skb, info, NS(susp, 1));
2493 int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info)
2495 struct drbd_conf *mdev;
2496 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2498 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2499 if (!adm_ctx.reply_skb)
2501 if (retcode != NO_ERROR)
2504 mdev = adm_ctx.mdev;
2505 if (test_bit(NEW_CUR_UUID, &mdev->flags)) {
2506 drbd_uuid_new_current(mdev);
2507 clear_bit(NEW_CUR_UUID, &mdev->flags);
2509 drbd_suspend_io(mdev);
2510 retcode = drbd_request_state(mdev, NS3(susp, 0, susp_nod, 0, susp_fen, 0));
2511 if (retcode == SS_SUCCESS) {
2512 if (mdev->state.conn < C_CONNECTED)
2513 tl_clear(mdev->tconn);
2514 if (mdev->state.disk == D_DISKLESS || mdev->state.disk == D_FAILED)
2515 tl_restart(mdev->tconn, FAIL_FROZEN_DISK_IO);
2517 drbd_resume_io(mdev);
2520 drbd_adm_finish(info, retcode);
2524 int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info)
2526 return drbd_adm_simple_request_state(skb, info, NS(disk, D_OUTDATED));
2529 int nla_put_drbd_cfg_context(struct sk_buff *skb, struct drbd_tconn *tconn, unsigned vnr)
2532 nla = nla_nest_start(skb, DRBD_NLA_CFG_CONTEXT);
2534 goto nla_put_failure;
2535 if (vnr != VOLUME_UNSPECIFIED)
2536 NLA_PUT_U32(skb, T_ctx_volume, vnr);
2537 NLA_PUT_STRING(skb, T_ctx_resource_name, tconn->name);
2538 if (tconn->my_addr_len)
2539 NLA_PUT(skb, T_ctx_my_addr, tconn->my_addr_len, &tconn->my_addr);
2540 if (tconn->peer_addr_len)
2541 NLA_PUT(skb, T_ctx_peer_addr, tconn->peer_addr_len, &tconn->peer_addr);
2542 nla_nest_end(skb, nla);
2547 nla_nest_cancel(skb, nla);
2551 int nla_put_status_info(struct sk_buff *skb, struct drbd_conf *mdev,
2552 const struct sib_info *sib)
2554 struct state_info *si = NULL; /* for sizeof(si->member); */
2555 struct net_conf *nc;
2559 int exclude_sensitive;
2561 /* If sib != NULL, this is drbd_bcast_event, which anyone can listen
2562 * to. So we better exclude_sensitive information.
2564 * If sib == NULL, this is drbd_adm_get_status, executed synchronously
2565 * in the context of the requesting user process. Exclude sensitive
2566 * information, unless current has superuser.
2568 * NOTE: for drbd_adm_get_status_all(), this is a netlink dump, and
2569 * relies on the current implementation of netlink_dump(), which
2570 * executes the dump callback successively from netlink_recvmsg(),
2571 * always in the context of the receiving process */
2572 exclude_sensitive = sib || !capable(CAP_SYS_ADMIN);
2574 got_ldev = get_ldev(mdev);
2576 /* We need to add connection name and volume number information still.
2577 * Minor number is in drbd_genlmsghdr. */
2578 if (nla_put_drbd_cfg_context(skb, mdev->tconn, mdev->vnr))
2579 goto nla_put_failure;
2581 if (res_opts_to_skb(skb, &mdev->tconn->res_opts, exclude_sensitive))
2582 goto nla_put_failure;
2586 if (disk_conf_to_skb(skb, rcu_dereference(mdev->ldev->disk_conf), exclude_sensitive))
2587 goto nla_put_failure;
2589 nc = rcu_dereference(mdev->tconn->net_conf);
2591 err = net_conf_to_skb(skb, nc, exclude_sensitive);
2594 goto nla_put_failure;
2596 nla = nla_nest_start(skb, DRBD_NLA_STATE_INFO);
2598 goto nla_put_failure;
2599 NLA_PUT_U32(skb, T_sib_reason, sib ? sib->sib_reason : SIB_GET_STATUS_REPLY);
2600 NLA_PUT_U32(skb, T_current_state, mdev->state.i);
2601 NLA_PUT_U64(skb, T_ed_uuid, mdev->ed_uuid);
2602 NLA_PUT_U64(skb, T_capacity, drbd_get_capacity(mdev->this_bdev));
2605 NLA_PUT_U32(skb, T_disk_flags, mdev->ldev->md.flags);
2606 NLA_PUT(skb, T_uuids, sizeof(si->uuids), mdev->ldev->md.uuid);
2607 NLA_PUT_U64(skb, T_bits_total, drbd_bm_bits(mdev));
2608 NLA_PUT_U64(skb, T_bits_oos, drbd_bm_total_weight(mdev));
2609 if (C_SYNC_SOURCE <= mdev->state.conn &&
2610 C_PAUSED_SYNC_T >= mdev->state.conn) {
2611 NLA_PUT_U64(skb, T_bits_rs_total, mdev->rs_total);
2612 NLA_PUT_U64(skb, T_bits_rs_failed, mdev->rs_failed);
2617 switch(sib->sib_reason) {
2618 case SIB_SYNC_PROGRESS:
2619 case SIB_GET_STATUS_REPLY:
2621 case SIB_STATE_CHANGE:
2622 NLA_PUT_U32(skb, T_prev_state, sib->os.i);
2623 NLA_PUT_U32(skb, T_new_state, sib->ns.i);
2625 case SIB_HELPER_POST:
2627 T_helper_exit_code, sib->helper_exit_code);
2629 case SIB_HELPER_PRE:
2630 NLA_PUT_STRING(skb, T_helper, sib->helper_name);
2634 nla_nest_end(skb, nla);
2644 int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info)
2646 enum drbd_ret_code retcode;
2649 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2650 if (!adm_ctx.reply_skb)
2652 if (retcode != NO_ERROR)
2655 err = nla_put_status_info(adm_ctx.reply_skb, adm_ctx.mdev, NULL);
2657 nlmsg_free(adm_ctx.reply_skb);
2661 drbd_adm_finish(info, retcode);
2665 int get_one_status(struct sk_buff *skb, struct netlink_callback *cb)
2667 struct drbd_conf *mdev;
2668 struct drbd_genlmsghdr *dh;
2669 struct drbd_tconn *pos = (struct drbd_tconn*)cb->args[0];
2670 struct drbd_tconn *tconn = NULL;
2671 struct drbd_tconn *tmp;
2672 unsigned volume = cb->args[1];
2674 /* Open coded, deferred, iteration:
2675 * list_for_each_entry_safe(tconn, tmp, &drbd_tconns, all_tconn) {
2676 * idr_for_each_entry(&tconn->volumes, mdev, i) {
2680 * where tconn is cb->args[0];
2681 * and i is cb->args[1];
2683 * cb->args[2] indicates if we shall loop over all resources,
2684 * or just dump all volumes of a single resource.
2686 * This may miss entries inserted after this dump started,
2687 * or entries deleted before they are reached.
2689 * We need to make sure the mdev won't disappear while
2690 * we are looking at it, and revalidate our iterators
2691 * on each iteration.
2694 /* synchronize with conn_create()/conn_destroy() */
2696 /* revalidate iterator position */
2697 list_for_each_entry_rcu(tmp, &drbd_tconns, all_tconn) {
2699 /* first iteration */
2711 mdev = idr_get_next(&tconn->volumes, &volume);
2713 /* No more volumes to dump on this tconn.
2714 * Advance tconn iterator. */
2715 pos = list_entry_rcu(tconn->all_tconn.next,
2716 struct drbd_tconn, all_tconn);
2717 /* Did we dump any volume on this tconn yet? */
2719 /* If we reached the end of the list,
2720 * or only a single resource dump was requested,
2722 if (&pos->all_tconn == &drbd_tconns || cb->args[2])
2730 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).pid,
2731 cb->nlh->nlmsg_seq, &drbd_genl_family,
2732 NLM_F_MULTI, DRBD_ADM_GET_STATUS);
2737 /* this is a tconn without a single volume */
2739 dh->ret_code = NO_ERROR;
2740 if (nla_put_drbd_cfg_context(skb, tconn, VOLUME_UNSPECIFIED))
2741 genlmsg_cancel(skb, dh);
2743 genlmsg_end(skb, dh);
2747 D_ASSERT(mdev->vnr == volume);
2748 D_ASSERT(mdev->tconn == tconn);
2750 dh->minor = mdev_to_minor(mdev);
2751 dh->ret_code = NO_ERROR;
2753 if (nla_put_status_info(skb, mdev, NULL)) {
2754 genlmsg_cancel(skb, dh);
2757 genlmsg_end(skb, dh);
2762 /* where to start the next iteration */
2763 cb->args[0] = (long)pos;
2764 cb->args[1] = (pos == tconn) ? volume + 1 : 0;
2766 /* No more tconns/volumes/minors found results in an empty skb.
2767 * Which will terminate the dump. */
2772 * Request status of all resources, or of all volumes within a single resource.
2774 * This is a dump, as the answer may not fit in a single reply skb otherwise.
2775 * Which means we cannot use the family->attrbuf or other such members, because
2776 * dump is NOT protected by the genl_lock(). During dump, we only have access
2777 * to the incoming skb, and need to opencode "parsing" of the nlattr payload.
2779 * Once things are setup properly, we call into get_one_status().
2781 int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb)
2783 const unsigned hdrlen = GENL_HDRLEN + GENL_MAGIC_FAMILY_HDRSZ;
2785 const char *resource_name;
2786 struct drbd_tconn *tconn;
2789 /* Is this a followup call? */
2791 /* ... of a single resource dump,
2792 * and the resource iterator has been advanced already? */
2793 if (cb->args[2] && cb->args[2] != cb->args[0])
2794 return 0; /* DONE. */
2798 /* First call (from netlink_dump_start). We need to figure out
2799 * which resource(s) the user wants us to dump. */
2800 nla = nla_find(nlmsg_attrdata(cb->nlh, hdrlen),
2801 nlmsg_attrlen(cb->nlh, hdrlen),
2802 DRBD_NLA_CFG_CONTEXT);
2804 /* No explicit context given. Dump all. */
2807 maxtype = ARRAY_SIZE(drbd_cfg_context_nl_policy) - 1;
2808 nla = drbd_nla_find_nested(maxtype, nla, __nla_type(T_ctx_resource_name));
2810 return PTR_ERR(nla);
2811 /* context given, but no name present? */
2814 resource_name = nla_data(nla);
2815 tconn = conn_get_by_name(resource_name);
2820 kref_put(&tconn->kref, &conn_destroy); /* get_one_status() (re)validates tconn by itself */
2822 /* prime iterators, and set "filter" mode mark:
2823 * only dump this tconn. */
2824 cb->args[0] = (long)tconn;
2825 /* cb->args[1] = 0; passed in this way. */
2826 cb->args[2] = (long)tconn;
2829 return get_one_status(skb, cb);
2832 int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info)
2834 enum drbd_ret_code retcode;
2835 struct timeout_parms tp;
2838 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2839 if (!adm_ctx.reply_skb)
2841 if (retcode != NO_ERROR)
2845 adm_ctx.mdev->state.pdsk == D_OUTDATED ? UT_PEER_OUTDATED :
2846 test_bit(USE_DEGR_WFC_T, &adm_ctx.mdev->flags) ? UT_DEGRADED :
2849 err = timeout_parms_to_priv_skb(adm_ctx.reply_skb, &tp);
2851 nlmsg_free(adm_ctx.reply_skb);
2855 drbd_adm_finish(info, retcode);
2859 int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info)
2861 struct drbd_conf *mdev;
2862 enum drbd_ret_code retcode;
2864 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2865 if (!adm_ctx.reply_skb)
2867 if (retcode != NO_ERROR)
2870 mdev = adm_ctx.mdev;
2871 if (info->attrs[DRBD_NLA_START_OV_PARMS]) {
2872 /* resume from last known position, if possible */
2873 struct start_ov_parms parms =
2874 { .ov_start_sector = mdev->ov_start_sector };
2875 int err = start_ov_parms_from_attrs(&parms, info);
2877 retcode = ERR_MANDATORY_TAG;
2878 drbd_msg_put_info(from_attrs_err_to_txt(err));
2881 /* w_make_ov_request expects position to be aligned */
2882 mdev->ov_start_sector = parms.ov_start_sector & ~BM_SECT_PER_BIT;
2884 /* If there is still bitmap IO pending, e.g. previous resync or verify
2885 * just being finished, wait for it before requesting a new resync. */
2886 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
2887 retcode = drbd_request_state(mdev,NS(conn,C_VERIFY_S));
2889 drbd_adm_finish(info, retcode);
2894 int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info)
2896 struct drbd_conf *mdev;
2897 enum drbd_ret_code retcode;
2898 int skip_initial_sync = 0;
2900 struct new_c_uuid_parms args;
2902 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2903 if (!adm_ctx.reply_skb)
2905 if (retcode != NO_ERROR)
2908 mdev = adm_ctx.mdev;
2909 memset(&args, 0, sizeof(args));
2910 if (info->attrs[DRBD_NLA_NEW_C_UUID_PARMS]) {
2911 err = new_c_uuid_parms_from_attrs(&args, info);
2913 retcode = ERR_MANDATORY_TAG;
2914 drbd_msg_put_info(from_attrs_err_to_txt(err));
2919 mutex_lock(mdev->state_mutex); /* Protects us against serialized state changes. */
2921 if (!get_ldev(mdev)) {
2922 retcode = ERR_NO_DISK;
2926 /* this is "skip initial sync", assume to be clean */
2927 if (mdev->state.conn == C_CONNECTED && mdev->tconn->agreed_pro_version >= 90 &&
2928 mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED && args.clear_bm) {
2929 dev_info(DEV, "Preparing to skip initial sync\n");
2930 skip_initial_sync = 1;
2931 } else if (mdev->state.conn != C_STANDALONE) {
2932 retcode = ERR_CONNECTED;
2936 drbd_uuid_set(mdev, UI_BITMAP, 0); /* Rotate UI_BITMAP to History 1, etc... */
2937 drbd_uuid_new_current(mdev); /* New current, previous to UI_BITMAP */
2939 if (args.clear_bm) {
2940 err = drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write,
2941 "clear_n_write from new_c_uuid", BM_LOCKED_MASK);
2943 dev_err(DEV, "Writing bitmap failed with %d\n",err);
2944 retcode = ERR_IO_MD_DISK;
2946 if (skip_initial_sync) {
2947 drbd_send_uuids_skip_initial_sync(mdev);
2948 _drbd_uuid_set(mdev, UI_BITMAP, 0);
2949 drbd_print_uuids(mdev, "cleared bitmap UUID");
2950 spin_lock_irq(&mdev->tconn->req_lock);
2951 _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
2953 spin_unlock_irq(&mdev->tconn->req_lock);
2961 mutex_unlock(mdev->state_mutex);
2963 drbd_adm_finish(info, retcode);
2967 static enum drbd_ret_code
2968 drbd_check_resource_name(const char *name)
2970 if (!name || !name[0]) {
2971 drbd_msg_put_info("resource name missing");
2972 return ERR_MANDATORY_TAG;
2974 /* if we want to use these in sysfs/configfs/debugfs some day,
2975 * we must not allow slashes */
2976 if (strchr(name, '/')) {
2977 drbd_msg_put_info("invalid resource name");
2978 return ERR_INVALID_REQUEST;
2983 int drbd_adm_new_resource(struct sk_buff *skb, struct genl_info *info)
2985 enum drbd_ret_code retcode;
2986 struct res_opts res_opts;
2989 retcode = drbd_adm_prepare(skb, info, 0);
2990 if (!adm_ctx.reply_skb)
2992 if (retcode != NO_ERROR)
2995 set_res_opts_defaults(&res_opts);
2996 err = res_opts_from_attrs(&res_opts, info);
2997 if (err && err != -ENOMSG) {
2998 retcode = ERR_MANDATORY_TAG;
2999 drbd_msg_put_info(from_attrs_err_to_txt(err));
3003 retcode = drbd_check_resource_name(adm_ctx.resource_name);
3004 if (retcode != NO_ERROR)
3007 if (adm_ctx.tconn) {
3008 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL) {
3009 retcode = ERR_INVALID_REQUEST;
3010 drbd_msg_put_info("resource exists");
3012 /* else: still NO_ERROR */
3016 if (!conn_create(adm_ctx.resource_name, &res_opts))
3017 retcode = ERR_NOMEM;
3019 drbd_adm_finish(info, retcode);
3023 int drbd_adm_add_minor(struct sk_buff *skb, struct genl_info *info)
3025 struct drbd_genlmsghdr *dh = info->userhdr;
3026 enum drbd_ret_code retcode;
3028 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
3029 if (!adm_ctx.reply_skb)
3031 if (retcode != NO_ERROR)
3034 /* FIXME drop minor_count parameter, limit to MINORMASK */
3035 if (dh->minor >= minor_count) {
3036 drbd_msg_put_info("requested minor out of range");
3037 retcode = ERR_INVALID_REQUEST;
3040 if (adm_ctx.volume > DRBD_VOLUME_MAX) {
3041 drbd_msg_put_info("requested volume id out of range");
3042 retcode = ERR_INVALID_REQUEST;
3046 /* drbd_adm_prepare made sure already
3047 * that mdev->tconn and mdev->vnr match the request. */
3049 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
3050 retcode = ERR_MINOR_EXISTS;
3051 /* else: still NO_ERROR */
3055 retcode = conn_new_minor(adm_ctx.tconn, dh->minor, adm_ctx.volume);
3057 drbd_adm_finish(info, retcode);
3061 static enum drbd_ret_code adm_delete_minor(struct drbd_conf *mdev)
3063 if (mdev->state.disk == D_DISKLESS &&
3064 /* no need to be mdev->state.conn == C_STANDALONE &&
3065 * we may want to delete a minor from a live replication group.
3067 mdev->state.role == R_SECONDARY) {
3068 idr_remove(&mdev->tconn->volumes, mdev->vnr);
3069 idr_remove(&minors, mdev_to_minor(mdev));
3070 del_gendisk(mdev->vdisk);
3072 kref_put(&mdev->kref, &drbd_minor_destroy);
3075 return ERR_MINOR_CONFIGURED;
3078 int drbd_adm_delete_minor(struct sk_buff *skb, struct genl_info *info)
3080 enum drbd_ret_code retcode;
3082 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
3083 if (!adm_ctx.reply_skb)
3085 if (retcode != NO_ERROR)
3088 retcode = adm_delete_minor(adm_ctx.mdev);
3090 drbd_adm_finish(info, retcode);
3094 int drbd_adm_down(struct sk_buff *skb, struct genl_info *info)
3096 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
3097 struct drbd_conf *mdev;
3100 retcode = drbd_adm_prepare(skb, info, 0);
3101 if (!adm_ctx.reply_skb)
3103 if (retcode != NO_ERROR)
3106 if (!adm_ctx.tconn) {
3107 retcode = ERR_RES_NOT_KNOWN;
3112 idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
3113 retcode = drbd_set_role(mdev, R_SECONDARY, 0);
3114 if (retcode < SS_SUCCESS) {
3115 drbd_msg_put_info("failed to demote");
3120 retcode = conn_try_disconnect(adm_ctx.tconn, 0);
3121 if (retcode < SS_SUCCESS) {
3122 drbd_msg_put_info("failed to disconnect");
3127 idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
3128 retcode = adm_detach(mdev);
3129 if (retcode < SS_SUCCESS) {
3130 drbd_msg_put_info("failed to detach");
3135 /* If we reach this, all volumes (of this tconn) are Secondary,
3136 * Disconnected, Diskless, aka Unconfigured. Make sure all threads have
3137 * actually stopped, state handling only does drbd_thread_stop_nowait(). */
3138 drbd_thread_stop(&adm_ctx.tconn->worker);
3140 /* Now, nothing can fail anymore */
3142 /* delete volumes */
3143 idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
3144 retcode = adm_delete_minor(mdev);
3145 if (retcode != NO_ERROR) {
3146 /* "can not happen" */
3147 drbd_msg_put_info("failed to delete volume");
3152 /* delete connection */
3153 if (conn_lowest_minor(adm_ctx.tconn) < 0) {
3154 list_del_rcu(&adm_ctx.tconn->all_tconn);
3156 kref_put(&adm_ctx.tconn->kref, &conn_destroy);
3160 /* "can not happen" */
3161 retcode = ERR_RES_IN_USE;
3162 drbd_msg_put_info("failed to delete connection");
3166 drbd_adm_finish(info, retcode);
3170 int drbd_adm_del_resource(struct sk_buff *skb, struct genl_info *info)
3172 enum drbd_ret_code retcode;
3174 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
3175 if (!adm_ctx.reply_skb)
3177 if (retcode != NO_ERROR)
3180 if (conn_lowest_minor(adm_ctx.tconn) < 0) {
3181 list_del_rcu(&adm_ctx.tconn->all_tconn);
3183 kref_put(&adm_ctx.tconn->kref, &conn_destroy);
3187 retcode = ERR_RES_IN_USE;
3190 if (retcode == NO_ERROR)
3191 drbd_thread_stop(&adm_ctx.tconn->worker);
3193 drbd_adm_finish(info, retcode);
3197 void drbd_bcast_event(struct drbd_conf *mdev, const struct sib_info *sib)
3199 static atomic_t drbd_genl_seq = ATOMIC_INIT(2); /* two. */
3200 struct sk_buff *msg;
3201 struct drbd_genlmsghdr *d_out;
3205 seq = atomic_inc_return(&drbd_genl_seq);
3206 msg = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
3211 d_out = genlmsg_put(msg, 0, seq, &drbd_genl_family, 0, DRBD_EVENT);
3212 if (!d_out) /* cannot happen, but anyways. */
3213 goto nla_put_failure;
3214 d_out->minor = mdev_to_minor(mdev);
3215 d_out->ret_code = NO_ERROR;
3217 if (nla_put_status_info(msg, mdev, sib))
3218 goto nla_put_failure;
3219 genlmsg_end(msg, d_out);
3220 err = drbd_genl_multicast_events(msg, 0);
3221 /* msg has been consumed or freed in netlink_broadcast() */
3222 if (err && err != -ESRCH)
3230 dev_err(DEV, "Error %d while broadcasting event. "
3231 "Event seq:%u sib_reason:%u\n",
3232 err, seq, sib->sib_reason);