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 kref_put(&adm_ctx.tconn->kref, &conn_destroy);
278 adm_ctx.tconn = NULL;
281 if (!adm_ctx.reply_skb)
284 adm_ctx.reply_dh->ret_code = retcode;
285 drbd_adm_send_reply(adm_ctx.reply_skb, info);
289 static void setup_khelper_env(struct drbd_tconn *tconn, char **envp)
293 /* FIXME: A future version will not allow this case. */
294 if (tconn->my_addr_len == 0 || tconn->peer_addr_len == 0)
297 switch (((struct sockaddr *)&tconn->peer_addr)->sa_family) {
300 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI6",
301 &((struct sockaddr_in6 *)&tconn->peer_addr)->sin6_addr);
305 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
306 &((struct sockaddr_in *)&tconn->peer_addr)->sin_addr);
310 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
311 &((struct sockaddr_in *)&tconn->peer_addr)->sin_addr);
313 snprintf(envp[3], 20, "DRBD_PEER_AF=%s", afs);
316 int drbd_khelper(struct drbd_conf *mdev, char *cmd)
318 char *envp[] = { "HOME=/",
320 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
321 (char[20]) { }, /* address family */
322 (char[60]) { }, /* address */
325 char *argv[] = {usermode_helper, cmd, mb, NULL };
329 snprintf(mb, 12, "minor-%d", mdev_to_minor(mdev));
330 setup_khelper_env(mdev->tconn, envp);
332 /* The helper may take some time.
333 * write out any unsynced meta data changes now */
336 dev_info(DEV, "helper command: %s %s %s\n", usermode_helper, cmd, mb);
337 sib.sib_reason = SIB_HELPER_PRE;
338 sib.helper_name = cmd;
339 drbd_bcast_event(mdev, &sib);
340 ret = call_usermodehelper(usermode_helper, argv, envp, 1);
342 dev_warn(DEV, "helper command: %s %s %s exit code %u (0x%x)\n",
343 usermode_helper, cmd, mb,
344 (ret >> 8) & 0xff, ret);
346 dev_info(DEV, "helper command: %s %s %s exit code %u (0x%x)\n",
347 usermode_helper, cmd, mb,
348 (ret >> 8) & 0xff, ret);
349 sib.sib_reason = SIB_HELPER_POST;
350 sib.helper_exit_code = ret;
351 drbd_bcast_event(mdev, &sib);
353 if (ret < 0) /* Ignore any ERRNOs we got. */
359 static void conn_md_sync(struct drbd_tconn *tconn)
361 struct drbd_conf *mdev;
365 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
366 kref_get(&mdev->kref);
369 kref_put(&mdev->kref, &drbd_minor_destroy);
375 int conn_khelper(struct drbd_tconn *tconn, char *cmd)
377 char *envp[] = { "HOME=/",
379 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
380 (char[20]) { }, /* address family */
381 (char[60]) { }, /* address */
383 char *argv[] = {usermode_helper, cmd, tconn->name, NULL };
386 setup_khelper_env(tconn, envp);
389 conn_info(tconn, "helper command: %s %s %s\n", usermode_helper, cmd, tconn->name);
390 /* TODO: conn_bcast_event() ?? */
392 ret = call_usermodehelper(usermode_helper, argv, envp, 1);
394 conn_warn(tconn, "helper command: %s %s %s exit code %u (0x%x)\n",
395 usermode_helper, cmd, tconn->name,
396 (ret >> 8) & 0xff, ret);
398 conn_info(tconn, "helper command: %s %s %s exit code %u (0x%x)\n",
399 usermode_helper, cmd, tconn->name,
400 (ret >> 8) & 0xff, ret);
401 /* TODO: conn_bcast_event() ?? */
403 if (ret < 0) /* Ignore any ERRNOs we got. */
409 static enum drbd_fencing_p highest_fencing_policy(struct drbd_tconn *tconn)
411 enum drbd_fencing_p fp = FP_NOT_AVAIL;
412 struct drbd_conf *mdev;
416 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
417 if (get_ldev_if_state(mdev, D_CONSISTENT)) {
418 fp = max_t(enum drbd_fencing_p, fp,
419 rcu_dereference(mdev->ldev->disk_conf)->fencing);
428 bool conn_try_outdate_peer(struct drbd_tconn *tconn)
430 union drbd_state mask = { };
431 union drbd_state val = { };
432 enum drbd_fencing_p fp;
436 if (tconn->cstate >= C_WF_REPORT_PARAMS) {
437 conn_err(tconn, "Expected cstate < C_WF_REPORT_PARAMS\n");
441 fp = highest_fencing_policy(tconn);
444 conn_warn(tconn, "Not fencing peer, I'm not even Consistent myself.\n");
451 r = conn_khelper(tconn, "fence-peer");
453 switch ((r>>8) & 0xff) {
454 case 3: /* peer is inconsistent */
455 ex_to_string = "peer is inconsistent or worse";
457 val.pdsk = D_INCONSISTENT;
459 case 4: /* peer got outdated, or was already outdated */
460 ex_to_string = "peer was fenced";
462 val.pdsk = D_OUTDATED;
464 case 5: /* peer was down */
465 if (conn_highest_disk(tconn) == D_UP_TO_DATE) {
466 /* we will(have) create(d) a new UUID anyways... */
467 ex_to_string = "peer is unreachable, assumed to be dead";
469 val.pdsk = D_OUTDATED;
471 ex_to_string = "peer unreachable, doing nothing since disk != UpToDate";
474 case 6: /* Peer is primary, voluntarily outdate myself.
475 * This is useful when an unconnected R_SECONDARY is asked to
476 * become R_PRIMARY, but finds the other peer being active. */
477 ex_to_string = "peer is active";
478 conn_warn(tconn, "Peer is primary, outdating myself.\n");
480 val.disk = D_OUTDATED;
483 if (fp != FP_STONITH)
484 conn_err(tconn, "fence-peer() = 7 && fencing != Stonith !!!\n");
485 ex_to_string = "peer was stonithed";
487 val.pdsk = D_OUTDATED;
490 /* The script is broken ... */
491 conn_err(tconn, "fence-peer helper broken, returned %d\n", (r>>8)&0xff);
492 return false; /* Eventually leave IO frozen */
495 conn_info(tconn, "fence-peer helper returned %d (%s)\n",
496 (r>>8) & 0xff, ex_to_string);
501 conn_request_state(tconn, mask, val, CS_VERBOSE);
502 here, because we might were able to re-establish the connection in the
504 spin_lock_irq(&tconn->req_lock);
505 if (tconn->cstate < C_WF_REPORT_PARAMS)
506 _conn_request_state(tconn, mask, val, CS_VERBOSE);
507 spin_unlock_irq(&tconn->req_lock);
509 return conn_highest_pdsk(tconn) <= D_OUTDATED;
512 static int _try_outdate_peer_async(void *data)
514 struct drbd_tconn *tconn = (struct drbd_tconn *)data;
516 conn_try_outdate_peer(tconn);
518 kref_put(&tconn->kref, &conn_destroy);
522 void conn_try_outdate_peer_async(struct drbd_tconn *tconn)
524 struct task_struct *opa;
526 kref_get(&tconn->kref);
527 opa = kthread_run(_try_outdate_peer_async, tconn, "drbd_async_h");
529 conn_err(tconn, "out of mem, failed to invoke fence-peer helper\n");
530 kref_put(&tconn->kref, &conn_destroy);
535 drbd_set_role(struct drbd_conf *mdev, enum drbd_role new_role, int force)
537 const int max_tries = 4;
538 enum drbd_state_rv rv = SS_UNKNOWN_ERROR;
542 union drbd_state mask, val;
544 if (new_role == R_PRIMARY)
545 request_ping(mdev->tconn); /* Detect a dead peer ASAP */
547 mutex_lock(mdev->state_mutex);
549 mask.i = 0; mask.role = R_MASK;
550 val.i = 0; val.role = new_role;
552 while (try++ < max_tries) {
553 rv = _drbd_request_state(mdev, mask, val, CS_WAIT_COMPLETE);
555 /* in case we first succeeded to outdate,
556 * but now suddenly could establish a connection */
557 if (rv == SS_CW_FAILED_BY_PEER && mask.pdsk != 0) {
563 if (rv == SS_NO_UP_TO_DATE_DISK && force &&
564 (mdev->state.disk < D_UP_TO_DATE &&
565 mdev->state.disk >= D_INCONSISTENT)) {
567 val.disk = D_UP_TO_DATE;
572 if (rv == SS_NO_UP_TO_DATE_DISK &&
573 mdev->state.disk == D_CONSISTENT && mask.pdsk == 0) {
574 D_ASSERT(mdev->state.pdsk == D_UNKNOWN);
576 if (conn_try_outdate_peer(mdev->tconn)) {
577 val.disk = D_UP_TO_DATE;
583 if (rv == SS_NOTHING_TO_DO)
585 if (rv == SS_PRIMARY_NOP && mask.pdsk == 0) {
586 if (!conn_try_outdate_peer(mdev->tconn) && force) {
587 dev_warn(DEV, "Forced into split brain situation!\n");
589 val.pdsk = D_OUTDATED;
594 if (rv == SS_TWO_PRIMARIES) {
595 /* Maybe the peer is detected as dead very soon...
596 retry at most once more in this case. */
599 nc = rcu_dereference(mdev->tconn->net_conf);
600 timeo = nc ? (nc->ping_timeo + 1) * HZ / 10 : 1;
602 schedule_timeout_interruptible(timeo);
607 if (rv < SS_SUCCESS) {
608 rv = _drbd_request_state(mdev, mask, val,
609 CS_VERBOSE + CS_WAIT_COMPLETE);
620 dev_warn(DEV, "Forced to consider local data as UpToDate!\n");
622 /* Wait until nothing is on the fly :) */
623 wait_event(mdev->misc_wait, atomic_read(&mdev->ap_pending_cnt) == 0);
625 if (new_role == R_SECONDARY) {
626 set_disk_ro(mdev->vdisk, true);
627 if (get_ldev(mdev)) {
628 mdev->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
632 mutex_lock(&mdev->tconn->conf_update);
633 nc = mdev->tconn->net_conf;
635 nc->discard_my_data = 0; /* without copy; single bit op is atomic */
636 mutex_unlock(&mdev->tconn->conf_update);
638 set_disk_ro(mdev->vdisk, false);
639 if (get_ldev(mdev)) {
640 if (((mdev->state.conn < C_CONNECTED ||
641 mdev->state.pdsk <= D_FAILED)
642 && mdev->ldev->md.uuid[UI_BITMAP] == 0) || forced)
643 drbd_uuid_new_current(mdev);
645 mdev->ldev->md.uuid[UI_CURRENT] |= (u64)1;
650 /* writeout of activity log covered areas of the bitmap
651 * to stable storage done in after state change already */
653 if (mdev->state.conn >= C_WF_REPORT_PARAMS) {
654 /* if this was forced, we should consider sync */
656 drbd_send_uuids(mdev);
657 drbd_send_current_state(mdev);
662 kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
664 mutex_unlock(mdev->state_mutex);
668 static const char *from_attrs_err_to_txt(int err)
670 return err == -ENOMSG ? "required attribute missing" :
671 err == -EOPNOTSUPP ? "unknown mandatory attribute" :
672 err == -EEXIST ? "can not change invariant setting" :
673 "invalid attribute value";
676 int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info)
678 struct set_role_parms parms;
680 enum drbd_ret_code retcode;
682 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
683 if (!adm_ctx.reply_skb)
685 if (retcode != NO_ERROR)
688 memset(&parms, 0, sizeof(parms));
689 if (info->attrs[DRBD_NLA_SET_ROLE_PARMS]) {
690 err = set_role_parms_from_attrs(&parms, info);
692 retcode = ERR_MANDATORY_TAG;
693 drbd_msg_put_info(from_attrs_err_to_txt(err));
698 if (info->genlhdr->cmd == DRBD_ADM_PRIMARY)
699 retcode = drbd_set_role(adm_ctx.mdev, R_PRIMARY, parms.assume_uptodate);
701 retcode = drbd_set_role(adm_ctx.mdev, R_SECONDARY, 0);
703 drbd_adm_finish(info, retcode);
707 /* initializes the md.*_offset members, so we are able to find
708 * the on disk meta data */
709 static void drbd_md_set_sector_offsets(struct drbd_conf *mdev,
710 struct drbd_backing_dev *bdev)
712 sector_t md_size_sect = 0;
716 meta_dev_idx = rcu_dereference(bdev->disk_conf)->meta_dev_idx;
718 switch (meta_dev_idx) {
720 /* v07 style fixed size indexed meta data */
721 bdev->md.md_size_sect = MD_RESERVED_SECT;
722 bdev->md.md_offset = drbd_md_ss__(mdev, bdev);
723 bdev->md.al_offset = MD_AL_OFFSET;
724 bdev->md.bm_offset = MD_BM_OFFSET;
726 case DRBD_MD_INDEX_FLEX_EXT:
727 /* just occupy the full device; unit: sectors */
728 bdev->md.md_size_sect = drbd_get_capacity(bdev->md_bdev);
729 bdev->md.md_offset = 0;
730 bdev->md.al_offset = MD_AL_OFFSET;
731 bdev->md.bm_offset = MD_BM_OFFSET;
733 case DRBD_MD_INDEX_INTERNAL:
734 case DRBD_MD_INDEX_FLEX_INT:
735 bdev->md.md_offset = drbd_md_ss__(mdev, bdev);
736 /* al size is still fixed */
737 bdev->md.al_offset = -MD_AL_SECTORS;
738 /* we need (slightly less than) ~ this much bitmap sectors: */
739 md_size_sect = drbd_get_capacity(bdev->backing_bdev);
740 md_size_sect = ALIGN(md_size_sect, BM_SECT_PER_EXT);
741 md_size_sect = BM_SECT_TO_EXT(md_size_sect);
742 md_size_sect = ALIGN(md_size_sect, 8);
744 /* plus the "drbd meta data super block",
745 * and the activity log; */
746 md_size_sect += MD_BM_OFFSET;
748 bdev->md.md_size_sect = md_size_sect;
749 /* bitmap offset is adjusted by 'super' block size */
750 bdev->md.bm_offset = -md_size_sect + MD_AL_OFFSET;
756 /* input size is expected to be in KB */
757 char *ppsize(char *buf, unsigned long long size)
759 /* Needs 9 bytes at max including trailing NUL:
760 * -1ULL ==> "16384 EB" */
761 static char units[] = { 'K', 'M', 'G', 'T', 'P', 'E' };
763 while (size >= 10000 && base < sizeof(units)-1) {
765 size = (size >> 10) + !!(size & (1<<9));
768 sprintf(buf, "%u %cB", (unsigned)size, units[base]);
773 /* there is still a theoretical deadlock when called from receiver
774 * on an D_INCONSISTENT R_PRIMARY:
775 * remote READ does inc_ap_bio, receiver would need to receive answer
776 * packet from remote to dec_ap_bio again.
777 * receiver receive_sizes(), comes here,
778 * waits for ap_bio_cnt == 0. -> deadlock.
779 * but this cannot happen, actually, because:
780 * R_PRIMARY D_INCONSISTENT, and peer's disk is unreachable
781 * (not connected, or bad/no disk on peer):
782 * see drbd_fail_request_early, ap_bio_cnt is zero.
783 * R_PRIMARY D_INCONSISTENT, and C_SYNC_TARGET:
784 * peer may not initiate a resize.
786 /* Note these are not to be confused with
787 * drbd_adm_suspend_io/drbd_adm_resume_io,
788 * which are (sub) state changes triggered by admin (drbdsetup),
789 * and can be long lived.
790 * This changes an mdev->flag, is triggered by drbd internals,
791 * and should be short-lived. */
792 void drbd_suspend_io(struct drbd_conf *mdev)
794 set_bit(SUSPEND_IO, &mdev->flags);
795 if (drbd_suspended(mdev))
797 wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_bio_cnt));
800 void drbd_resume_io(struct drbd_conf *mdev)
802 clear_bit(SUSPEND_IO, &mdev->flags);
803 wake_up(&mdev->misc_wait);
807 * drbd_determine_dev_size() - Sets the right device size obeying all constraints
808 * @mdev: DRBD device.
810 * Returns 0 on success, negative return values indicate errors.
811 * You should call drbd_md_sync() after calling this function.
813 enum determine_dev_size drbd_determine_dev_size(struct drbd_conf *mdev, enum dds_flags flags) __must_hold(local)
815 sector_t prev_first_sect, prev_size; /* previous meta location */
816 sector_t la_size, u_size;
820 int md_moved, la_size_changed;
821 enum determine_dev_size rv = unchanged;
824 * application request passes inc_ap_bio,
825 * but then cannot get an AL-reference.
826 * this function later may wait on ap_bio_cnt == 0. -> deadlock.
829 * Suspend IO right here.
830 * still lock the act_log to not trigger ASSERTs there.
832 drbd_suspend_io(mdev);
834 /* no wait necessary anymore, actually we could assert that */
835 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
837 prev_first_sect = drbd_md_first_sector(mdev->ldev);
838 prev_size = mdev->ldev->md.md_size_sect;
839 la_size = mdev->ldev->md.la_size_sect;
841 /* TODO: should only be some assert here, not (re)init... */
842 drbd_md_set_sector_offsets(mdev, mdev->ldev);
845 u_size = rcu_dereference(mdev->ldev->disk_conf)->disk_size;
847 size = drbd_new_dev_size(mdev, mdev->ldev, u_size, flags & DDSF_FORCED);
849 if (drbd_get_capacity(mdev->this_bdev) != size ||
850 drbd_bm_capacity(mdev) != size) {
852 err = drbd_bm_resize(mdev, size, !(flags & DDSF_NO_RESYNC));
854 /* currently there is only one error: ENOMEM! */
855 size = drbd_bm_capacity(mdev)>>1;
857 dev_err(DEV, "OUT OF MEMORY! "
858 "Could not allocate bitmap!\n");
860 dev_err(DEV, "BM resizing failed. "
861 "Leaving size unchanged at size = %lu KB\n",
862 (unsigned long)size);
866 /* racy, see comments above. */
867 drbd_set_my_capacity(mdev, size);
868 mdev->ldev->md.la_size_sect = size;
869 dev_info(DEV, "size = %s (%llu KB)\n", ppsize(ppb, size>>1),
870 (unsigned long long)size>>1);
872 if (rv == dev_size_error)
875 la_size_changed = (la_size != mdev->ldev->md.la_size_sect);
877 md_moved = prev_first_sect != drbd_md_first_sector(mdev->ldev)
878 || prev_size != mdev->ldev->md.md_size_sect;
880 if (la_size_changed || md_moved) {
883 drbd_al_shrink(mdev); /* All extents inactive. */
884 dev_info(DEV, "Writing the whole bitmap, %s\n",
885 la_size_changed && md_moved ? "size changed and md moved" :
886 la_size_changed ? "size changed" : "md moved");
887 /* next line implicitly does drbd_suspend_io()+drbd_resume_io() */
888 err = drbd_bitmap_io(mdev, &drbd_bm_write,
889 "size changed", BM_LOCKED_MASK);
894 drbd_md_mark_dirty(mdev);
902 lc_unlock(mdev->act_log);
903 wake_up(&mdev->al_wait);
904 drbd_resume_io(mdev);
910 drbd_new_dev_size(struct drbd_conf *mdev, struct drbd_backing_dev *bdev,
911 sector_t u_size, int assume_peer_has_space)
913 sector_t p_size = mdev->p_size; /* partner's disk size. */
914 sector_t la_size = bdev->md.la_size_sect; /* last agreed size. */
915 sector_t m_size; /* my size */
918 m_size = drbd_get_max_capacity(bdev);
920 if (mdev->state.conn < C_CONNECTED && assume_peer_has_space) {
921 dev_warn(DEV, "Resize while not connected was forced by the user!\n");
925 if (p_size && m_size) {
926 size = min_t(sector_t, p_size, m_size);
930 if (m_size && m_size < size)
932 if (p_size && p_size < size)
943 dev_err(DEV, "Both nodes diskless!\n");
947 dev_err(DEV, "Requested disk size is too big (%lu > %lu)\n",
948 (unsigned long)u_size>>1, (unsigned long)size>>1);
957 * drbd_check_al_size() - Ensures that the AL is of the right size
958 * @mdev: DRBD device.
960 * Returns -EBUSY if current al lru is still used, -ENOMEM when allocation
961 * failed, and 0 on success. You should call drbd_md_sync() after you called
964 static int drbd_check_al_size(struct drbd_conf *mdev, struct disk_conf *dc)
966 struct lru_cache *n, *t;
967 struct lc_element *e;
972 mdev->act_log->nr_elements == dc->al_extents)
977 n = lc_create("act_log", drbd_al_ext_cache, AL_UPDATES_PER_TRANSACTION,
978 dc->al_extents, sizeof(struct lc_element), 0);
981 dev_err(DEV, "Cannot allocate act_log lru!\n");
984 spin_lock_irq(&mdev->al_lock);
986 for (i = 0; i < t->nr_elements; i++) {
987 e = lc_element_by_index(t, i);
989 dev_err(DEV, "refcnt(%d)==%d\n",
990 e->lc_number, e->refcnt);
996 spin_unlock_irq(&mdev->al_lock);
998 dev_err(DEV, "Activity log still in use!\n");
1005 drbd_md_mark_dirty(mdev); /* we changed mdev->act_log->nr_elemens */
1009 static void drbd_setup_queue_param(struct drbd_conf *mdev, unsigned int max_bio_size)
1011 struct request_queue * const q = mdev->rq_queue;
1012 int max_hw_sectors = max_bio_size >> 9;
1013 int max_segments = 0;
1015 if (get_ldev_if_state(mdev, D_ATTACHING)) {
1016 struct request_queue * const b = mdev->ldev->backing_bdev->bd_disk->queue;
1018 max_hw_sectors = min(queue_max_hw_sectors(b), max_bio_size >> 9);
1020 max_segments = rcu_dereference(mdev->ldev->disk_conf)->max_bio_bvecs;
1025 blk_queue_logical_block_size(q, 512);
1026 blk_queue_max_hw_sectors(q, max_hw_sectors);
1027 /* This is the workaround for "bio would need to, but cannot, be split" */
1028 blk_queue_max_segments(q, max_segments ? max_segments : BLK_MAX_SEGMENTS);
1029 blk_queue_segment_boundary(q, PAGE_CACHE_SIZE-1);
1031 if (get_ldev_if_state(mdev, D_ATTACHING)) {
1032 struct request_queue * const b = mdev->ldev->backing_bdev->bd_disk->queue;
1034 blk_queue_stack_limits(q, b);
1036 if (q->backing_dev_info.ra_pages != b->backing_dev_info.ra_pages) {
1037 dev_info(DEV, "Adjusting my ra_pages to backing device's (%lu -> %lu)\n",
1038 q->backing_dev_info.ra_pages,
1039 b->backing_dev_info.ra_pages);
1040 q->backing_dev_info.ra_pages = b->backing_dev_info.ra_pages;
1046 void drbd_reconsider_max_bio_size(struct drbd_conf *mdev)
1048 int now, new, local, peer;
1050 now = queue_max_hw_sectors(mdev->rq_queue) << 9;
1051 local = mdev->local_max_bio_size; /* Eventually last known value, from volatile memory */
1052 peer = mdev->peer_max_bio_size; /* Eventually last known value, from meta data */
1054 if (get_ldev_if_state(mdev, D_ATTACHING)) {
1055 local = queue_max_hw_sectors(mdev->ldev->backing_bdev->bd_disk->queue) << 9;
1056 mdev->local_max_bio_size = local;
1060 /* We may ignore peer limits if the peer is modern enough.
1061 Because new from 8.3.8 onwards the peer can use multiple
1062 BIOs for a single peer_request */
1063 if (mdev->state.conn >= C_CONNECTED) {
1064 if (mdev->tconn->agreed_pro_version < 94)
1065 peer = min_t(int, mdev->peer_max_bio_size, DRBD_MAX_SIZE_H80_PACKET);
1066 /* Correct old drbd (up to 8.3.7) if it believes it can do more than 32KiB */
1067 else if (mdev->tconn->agreed_pro_version == 94)
1068 peer = DRBD_MAX_SIZE_H80_PACKET;
1069 else if (mdev->tconn->agreed_pro_version < 100)
1070 peer = DRBD_MAX_BIO_SIZE_P95; /* drbd 8.3.8 onwards, before 8.4.0 */
1072 peer = DRBD_MAX_BIO_SIZE;
1075 new = min_t(int, local, peer);
1077 if (mdev->state.role == R_PRIMARY && new < now)
1078 dev_err(DEV, "ASSERT FAILED new < now; (%d < %d)\n", new, now);
1081 dev_info(DEV, "max BIO size = %u\n", new);
1083 drbd_setup_queue_param(mdev, new);
1086 /* Starts the worker thread */
1087 static void conn_reconfig_start(struct drbd_tconn *tconn)
1089 drbd_thread_start(&tconn->worker);
1090 conn_flush_workqueue(tconn);
1093 /* if still unconfigured, stops worker again. */
1094 static void conn_reconfig_done(struct drbd_tconn *tconn)
1097 spin_lock_irq(&tconn->req_lock);
1098 stop_threads = conn_all_vols_unconf(tconn) &&
1099 tconn->cstate == C_STANDALONE;
1100 spin_unlock_irq(&tconn->req_lock);
1102 /* asender is implicitly stopped by receiver
1103 * in conn_disconnect() */
1104 drbd_thread_stop(&tconn->receiver);
1105 drbd_thread_stop(&tconn->worker);
1109 /* Make sure IO is suspended before calling this function(). */
1110 static void drbd_suspend_al(struct drbd_conf *mdev)
1114 if (!lc_try_lock(mdev->act_log)) {
1115 dev_warn(DEV, "Failed to lock al in drbd_suspend_al()\n");
1119 drbd_al_shrink(mdev);
1120 spin_lock_irq(&mdev->tconn->req_lock);
1121 if (mdev->state.conn < C_CONNECTED)
1122 s = !test_and_set_bit(AL_SUSPENDED, &mdev->flags);
1123 spin_unlock_irq(&mdev->tconn->req_lock);
1124 lc_unlock(mdev->act_log);
1127 dev_info(DEV, "Suspended AL updates\n");
1131 static bool should_set_defaults(struct genl_info *info)
1133 unsigned flags = ((struct drbd_genlmsghdr*)info->userhdr)->flags;
1134 return 0 != (flags & DRBD_GENL_F_SET_DEFAULTS);
1137 static void enforce_disk_conf_limits(struct disk_conf *dc)
1139 if (dc->al_extents < DRBD_AL_EXTENTS_MIN)
1140 dc->al_extents = DRBD_AL_EXTENTS_MIN;
1141 if (dc->al_extents > DRBD_AL_EXTENTS_MAX)
1142 dc->al_extents = DRBD_AL_EXTENTS_MAX;
1144 if (dc->c_plan_ahead > DRBD_C_PLAN_AHEAD_MAX)
1145 dc->c_plan_ahead = DRBD_C_PLAN_AHEAD_MAX;
1148 int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info)
1150 enum drbd_ret_code retcode;
1151 struct drbd_conf *mdev;
1152 struct disk_conf *new_disk_conf, *old_disk_conf;
1153 struct fifo_buffer *old_plan = NULL, *new_plan = NULL;
1156 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1157 if (!adm_ctx.reply_skb)
1159 if (retcode != NO_ERROR)
1162 mdev = adm_ctx.mdev;
1164 /* we also need a disk
1165 * to change the options on */
1166 if (!get_ldev(mdev)) {
1167 retcode = ERR_NO_DISK;
1171 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
1172 if (!new_disk_conf) {
1173 retcode = ERR_NOMEM;
1177 mutex_lock(&mdev->tconn->conf_update);
1178 old_disk_conf = mdev->ldev->disk_conf;
1179 *new_disk_conf = *old_disk_conf;
1180 if (should_set_defaults(info))
1181 set_disk_conf_defaults(new_disk_conf);
1183 err = disk_conf_from_attrs_for_change(new_disk_conf, info);
1184 if (err && err != -ENOMSG) {
1185 retcode = ERR_MANDATORY_TAG;
1186 drbd_msg_put_info(from_attrs_err_to_txt(err));
1189 if (!expect(new_disk_conf->resync_rate >= 1))
1190 new_disk_conf->resync_rate = 1;
1192 enforce_disk_conf_limits(new_disk_conf);
1194 fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
1195 if (fifo_size != mdev->rs_plan_s->size) {
1196 new_plan = fifo_alloc(fifo_size);
1198 dev_err(DEV, "kmalloc of fifo_buffer failed");
1199 retcode = ERR_NOMEM;
1204 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
1205 drbd_al_shrink(mdev);
1206 err = drbd_check_al_size(mdev, new_disk_conf);
1207 lc_unlock(mdev->act_log);
1208 wake_up(&mdev->al_wait);
1211 retcode = ERR_NOMEM;
1215 write_lock_irq(&global_state_lock);
1216 retcode = drbd_resync_after_valid(mdev, new_disk_conf->resync_after);
1217 if (retcode == NO_ERROR) {
1218 rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
1219 drbd_resync_after_changed(mdev);
1221 write_unlock_irq(&global_state_lock);
1223 if (retcode != NO_ERROR)
1227 old_plan = mdev->rs_plan_s;
1228 rcu_assign_pointer(mdev->rs_plan_s, new_plan);
1231 mutex_unlock(&mdev->tconn->conf_update);
1233 drbd_bump_write_ordering(mdev->tconn, WO_bdev_flush);
1237 if (mdev->state.conn >= C_CONNECTED)
1238 drbd_send_sync_param(mdev);
1241 kfree(old_disk_conf);
1243 mod_timer(&mdev->request_timer, jiffies + HZ);
1247 mutex_unlock(&mdev->tconn->conf_update);
1249 kfree(new_disk_conf);
1254 drbd_adm_finish(info, retcode);
1258 int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info)
1260 struct drbd_conf *mdev;
1262 enum drbd_ret_code retcode;
1263 enum determine_dev_size dd;
1264 sector_t max_possible_sectors;
1265 sector_t min_md_device_sectors;
1266 struct drbd_backing_dev *nbc = NULL; /* new_backing_conf */
1267 struct disk_conf *new_disk_conf = NULL;
1268 struct block_device *bdev;
1269 struct lru_cache *resync_lru = NULL;
1270 struct fifo_buffer *new_plan = NULL;
1271 union drbd_state ns, os;
1272 enum drbd_state_rv rv;
1273 struct net_conf *nc;
1275 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1276 if (!adm_ctx.reply_skb)
1278 if (retcode != NO_ERROR)
1281 mdev = adm_ctx.mdev;
1282 conn_reconfig_start(mdev->tconn);
1284 /* if you want to reconfigure, please tear down first */
1285 if (mdev->state.disk > D_DISKLESS) {
1286 retcode = ERR_DISK_CONFIGURED;
1289 /* It may just now have detached because of IO error. Make sure
1290 * drbd_ldev_destroy is done already, we may end up here very fast,
1291 * e.g. if someone calls attach from the on-io-error handler,
1292 * to realize a "hot spare" feature (not that I'd recommend that) */
1293 wait_event(mdev->misc_wait, !atomic_read(&mdev->local_cnt));
1295 /* allocation not in the IO path, drbdsetup context */
1296 nbc = kzalloc(sizeof(struct drbd_backing_dev), GFP_KERNEL);
1298 retcode = ERR_NOMEM;
1301 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
1302 if (!new_disk_conf) {
1303 retcode = ERR_NOMEM;
1306 nbc->disk_conf = new_disk_conf;
1308 set_disk_conf_defaults(new_disk_conf);
1309 err = disk_conf_from_attrs(new_disk_conf, info);
1311 retcode = ERR_MANDATORY_TAG;
1312 drbd_msg_put_info(from_attrs_err_to_txt(err));
1316 enforce_disk_conf_limits(new_disk_conf);
1318 new_plan = fifo_alloc((new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ);
1320 retcode = ERR_NOMEM;
1324 if (new_disk_conf->meta_dev_idx < DRBD_MD_INDEX_FLEX_INT) {
1325 retcode = ERR_MD_IDX_INVALID;
1330 nc = rcu_dereference(mdev->tconn->net_conf);
1332 if (new_disk_conf->fencing == FP_STONITH && nc->wire_protocol == DRBD_PROT_A) {
1334 retcode = ERR_STONITH_AND_PROT_A;
1340 bdev = blkdev_get_by_path(new_disk_conf->backing_dev,
1341 FMODE_READ | FMODE_WRITE | FMODE_EXCL, mdev);
1343 dev_err(DEV, "open(\"%s\") failed with %ld\n", new_disk_conf->backing_dev,
1345 retcode = ERR_OPEN_DISK;
1348 nbc->backing_bdev = bdev;
1351 * meta_dev_idx >= 0: external fixed size, possibly multiple
1352 * drbd sharing one meta device. TODO in that case, paranoia
1353 * check that [md_bdev, meta_dev_idx] is not yet used by some
1354 * other drbd minor! (if you use drbd.conf + drbdadm, that
1355 * should check it for you already; but if you don't, or
1356 * someone fooled it, we need to double check here)
1358 bdev = blkdev_get_by_path(new_disk_conf->meta_dev,
1359 FMODE_READ | FMODE_WRITE | FMODE_EXCL,
1360 (new_disk_conf->meta_dev_idx < 0) ?
1361 (void *)mdev : (void *)drbd_m_holder);
1363 dev_err(DEV, "open(\"%s\") failed with %ld\n", new_disk_conf->meta_dev,
1365 retcode = ERR_OPEN_MD_DISK;
1368 nbc->md_bdev = bdev;
1370 if ((nbc->backing_bdev == nbc->md_bdev) !=
1371 (new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_INTERNAL ||
1372 new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_FLEX_INT)) {
1373 retcode = ERR_MD_IDX_INVALID;
1377 resync_lru = lc_create("resync", drbd_bm_ext_cache,
1378 1, 61, sizeof(struct bm_extent),
1379 offsetof(struct bm_extent, lce));
1381 retcode = ERR_NOMEM;
1385 /* RT - for drbd_get_max_capacity() DRBD_MD_INDEX_FLEX_INT */
1386 drbd_md_set_sector_offsets(mdev, nbc);
1388 if (drbd_get_max_capacity(nbc) < new_disk_conf->disk_size) {
1389 dev_err(DEV, "max capacity %llu smaller than disk size %llu\n",
1390 (unsigned long long) drbd_get_max_capacity(nbc),
1391 (unsigned long long) new_disk_conf->disk_size);
1392 retcode = ERR_DISK_TOO_SMALL;
1396 if (new_disk_conf->meta_dev_idx < 0) {
1397 max_possible_sectors = DRBD_MAX_SECTORS_FLEX;
1398 /* at least one MB, otherwise it does not make sense */
1399 min_md_device_sectors = (2<<10);
1401 max_possible_sectors = DRBD_MAX_SECTORS;
1402 min_md_device_sectors = MD_RESERVED_SECT * (new_disk_conf->meta_dev_idx + 1);
1405 if (drbd_get_capacity(nbc->md_bdev) < min_md_device_sectors) {
1406 retcode = ERR_MD_DISK_TOO_SMALL;
1407 dev_warn(DEV, "refusing attach: md-device too small, "
1408 "at least %llu sectors needed for this meta-disk type\n",
1409 (unsigned long long) min_md_device_sectors);
1413 /* Make sure the new disk is big enough
1414 * (we may currently be R_PRIMARY with no local disk...) */
1415 if (drbd_get_max_capacity(nbc) <
1416 drbd_get_capacity(mdev->this_bdev)) {
1417 retcode = ERR_DISK_TOO_SMALL;
1421 nbc->known_size = drbd_get_capacity(nbc->backing_bdev);
1423 if (nbc->known_size > max_possible_sectors) {
1424 dev_warn(DEV, "==> truncating very big lower level device "
1425 "to currently maximum possible %llu sectors <==\n",
1426 (unsigned long long) max_possible_sectors);
1427 if (new_disk_conf->meta_dev_idx >= 0)
1428 dev_warn(DEV, "==>> using internal or flexible "
1429 "meta data may help <<==\n");
1432 drbd_suspend_io(mdev);
1433 /* also wait for the last barrier ack. */
1434 wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_pending_cnt) || drbd_suspended(mdev));
1435 /* and for any other previously queued work */
1436 drbd_flush_workqueue(mdev);
1438 rv = _drbd_request_state(mdev, NS(disk, D_ATTACHING), CS_VERBOSE);
1439 retcode = rv; /* FIXME: Type mismatch. */
1440 drbd_resume_io(mdev);
1441 if (rv < SS_SUCCESS)
1444 if (!get_ldev_if_state(mdev, D_ATTACHING))
1445 goto force_diskless;
1447 drbd_md_set_sector_offsets(mdev, nbc);
1449 if (!mdev->bitmap) {
1450 if (drbd_bm_init(mdev)) {
1451 retcode = ERR_NOMEM;
1452 goto force_diskless_dec;
1456 retcode = drbd_md_read(mdev, nbc);
1457 if (retcode != NO_ERROR)
1458 goto force_diskless_dec;
1460 if (mdev->state.conn < C_CONNECTED &&
1461 mdev->state.role == R_PRIMARY &&
1462 (mdev->ed_uuid & ~((u64)1)) != (nbc->md.uuid[UI_CURRENT] & ~((u64)1))) {
1463 dev_err(DEV, "Can only attach to data with current UUID=%016llX\n",
1464 (unsigned long long)mdev->ed_uuid);
1465 retcode = ERR_DATA_NOT_CURRENT;
1466 goto force_diskless_dec;
1469 /* Since we are diskless, fix the activity log first... */
1470 if (drbd_check_al_size(mdev, new_disk_conf)) {
1471 retcode = ERR_NOMEM;
1472 goto force_diskless_dec;
1475 /* Prevent shrinking of consistent devices ! */
1476 if (drbd_md_test_flag(nbc, MDF_CONSISTENT) &&
1477 drbd_new_dev_size(mdev, nbc, nbc->disk_conf->disk_size, 0) < nbc->md.la_size_sect) {
1478 dev_warn(DEV, "refusing to truncate a consistent device\n");
1479 retcode = ERR_DISK_TOO_SMALL;
1480 goto force_diskless_dec;
1483 /* Reset the "barriers don't work" bits here, then force meta data to
1484 * be written, to ensure we determine if barriers are supported. */
1485 if (new_disk_conf->md_flushes)
1486 clear_bit(MD_NO_FUA, &mdev->flags);
1488 set_bit(MD_NO_FUA, &mdev->flags);
1490 /* Point of no return reached.
1491 * Devices and memory are no longer released by error cleanup below.
1492 * now mdev takes over responsibility, and the state engine should
1493 * clean it up somewhere. */
1494 D_ASSERT(mdev->ldev == NULL);
1496 mdev->resync = resync_lru;
1497 mdev->rs_plan_s = new_plan;
1500 new_disk_conf = NULL;
1503 drbd_bump_write_ordering(mdev->tconn, WO_bdev_flush);
1505 if (drbd_md_test_flag(mdev->ldev, MDF_CRASHED_PRIMARY))
1506 set_bit(CRASHED_PRIMARY, &mdev->flags);
1508 clear_bit(CRASHED_PRIMARY, &mdev->flags);
1510 if (drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) &&
1511 !(mdev->state.role == R_PRIMARY && mdev->tconn->susp_nod))
1512 set_bit(CRASHED_PRIMARY, &mdev->flags);
1519 drbd_reconsider_max_bio_size(mdev);
1521 /* If I am currently not R_PRIMARY,
1522 * but meta data primary indicator is set,
1523 * I just now recover from a hard crash,
1524 * and have been R_PRIMARY before that crash.
1526 * Now, if I had no connection before that crash
1527 * (have been degraded R_PRIMARY), chances are that
1528 * I won't find my peer now either.
1530 * In that case, and _only_ in that case,
1531 * we use the degr-wfc-timeout instead of the default,
1532 * so we can automatically recover from a crash of a
1533 * degraded but active "cluster" after a certain timeout.
1535 clear_bit(USE_DEGR_WFC_T, &mdev->flags);
1536 if (mdev->state.role != R_PRIMARY &&
1537 drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) &&
1538 !drbd_md_test_flag(mdev->ldev, MDF_CONNECTED_IND))
1539 set_bit(USE_DEGR_WFC_T, &mdev->flags);
1541 dd = drbd_determine_dev_size(mdev, 0);
1542 if (dd == dev_size_error) {
1543 retcode = ERR_NOMEM_BITMAP;
1544 goto force_diskless_dec;
1545 } else if (dd == grew)
1546 set_bit(RESYNC_AFTER_NEG, &mdev->flags);
1548 if (drbd_md_test_flag(mdev->ldev, MDF_FULL_SYNC)) {
1549 dev_info(DEV, "Assuming that all blocks are out of sync "
1550 "(aka FullSync)\n");
1551 if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write,
1552 "set_n_write from attaching", BM_LOCKED_MASK)) {
1553 retcode = ERR_IO_MD_DISK;
1554 goto force_diskless_dec;
1557 if (drbd_bitmap_io(mdev, &drbd_bm_read,
1558 "read from attaching", BM_LOCKED_MASK)) {
1559 retcode = ERR_IO_MD_DISK;
1560 goto force_diskless_dec;
1564 if (_drbd_bm_total_weight(mdev) == drbd_bm_bits(mdev))
1565 drbd_suspend_al(mdev); /* IO is still suspended here... */
1567 spin_lock_irq(&mdev->tconn->req_lock);
1568 os = drbd_read_state(mdev);
1570 /* If MDF_CONSISTENT is not set go into inconsistent state,
1571 otherwise investigate MDF_WasUpToDate...
1572 If MDF_WAS_UP_TO_DATE is not set go into D_OUTDATED disk state,
1573 otherwise into D_CONSISTENT state.
1575 if (drbd_md_test_flag(mdev->ldev, MDF_CONSISTENT)) {
1576 if (drbd_md_test_flag(mdev->ldev, MDF_WAS_UP_TO_DATE))
1577 ns.disk = D_CONSISTENT;
1579 ns.disk = D_OUTDATED;
1581 ns.disk = D_INCONSISTENT;
1584 if (drbd_md_test_flag(mdev->ldev, MDF_PEER_OUT_DATED))
1585 ns.pdsk = D_OUTDATED;
1588 if (ns.disk == D_CONSISTENT &&
1589 (ns.pdsk == D_OUTDATED || rcu_dereference(mdev->ldev->disk_conf)->fencing == FP_DONT_CARE))
1590 ns.disk = D_UP_TO_DATE;
1593 /* All tests on MDF_PRIMARY_IND, MDF_CONNECTED_IND,
1594 MDF_CONSISTENT and MDF_WAS_UP_TO_DATE must happen before
1595 this point, because drbd_request_state() modifies these
1598 /* In case we are C_CONNECTED postpone any decision on the new disk
1599 state after the negotiation phase. */
1600 if (mdev->state.conn == C_CONNECTED) {
1601 mdev->new_state_tmp.i = ns.i;
1603 ns.disk = D_NEGOTIATING;
1605 /* We expect to receive up-to-date UUIDs soon.
1606 To avoid a race in receive_state, free p_uuid while
1607 holding req_lock. I.e. atomic with the state change */
1608 kfree(mdev->p_uuid);
1609 mdev->p_uuid = NULL;
1612 rv = _drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
1613 spin_unlock_irq(&mdev->tconn->req_lock);
1615 if (rv < SS_SUCCESS)
1616 goto force_diskless_dec;
1618 mod_timer(&mdev->request_timer, jiffies + HZ);
1620 if (mdev->state.role == R_PRIMARY)
1621 mdev->ldev->md.uuid[UI_CURRENT] |= (u64)1;
1623 mdev->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
1625 drbd_md_mark_dirty(mdev);
1628 kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
1630 conn_reconfig_done(mdev->tconn);
1631 drbd_adm_finish(info, retcode);
1637 drbd_force_state(mdev, NS(disk, D_DISKLESS));
1640 conn_reconfig_done(mdev->tconn);
1642 if (nbc->backing_bdev)
1643 blkdev_put(nbc->backing_bdev,
1644 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1646 blkdev_put(nbc->md_bdev,
1647 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1650 kfree(new_disk_conf);
1651 lc_destroy(resync_lru);
1655 drbd_adm_finish(info, retcode);
1659 static int adm_detach(struct drbd_conf *mdev, int force)
1661 enum drbd_state_rv retcode;
1665 drbd_force_state(mdev, NS(disk, D_FAILED));
1666 retcode = SS_SUCCESS;
1670 drbd_suspend_io(mdev); /* so no-one is stuck in drbd_al_begin_io */
1671 drbd_md_get_buffer(mdev); /* make sure there is no in-flight meta-data IO */
1672 retcode = drbd_request_state(mdev, NS(disk, D_FAILED));
1673 drbd_md_put_buffer(mdev);
1674 /* D_FAILED will transition to DISKLESS. */
1675 ret = wait_event_interruptible(mdev->misc_wait,
1676 mdev->state.disk != D_FAILED);
1677 drbd_resume_io(mdev);
1678 if ((int)retcode == (int)SS_IS_DISKLESS)
1679 retcode = SS_NOTHING_TO_DO;
1686 /* Detaching the disk is a process in multiple stages. First we need to lock
1687 * out application IO, in-flight IO, IO stuck in drbd_al_begin_io.
1688 * Then we transition to D_DISKLESS, and wait for put_ldev() to return all
1689 * internal references as well.
1690 * Only then we have finally detached. */
1691 int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info)
1693 enum drbd_ret_code retcode;
1694 struct detach_parms parms = { };
1697 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1698 if (!adm_ctx.reply_skb)
1700 if (retcode != NO_ERROR)
1703 if (info->attrs[DRBD_NLA_DETACH_PARMS]) {
1704 err = detach_parms_from_attrs(&parms, info);
1706 retcode = ERR_MANDATORY_TAG;
1707 drbd_msg_put_info(from_attrs_err_to_txt(err));
1712 retcode = adm_detach(adm_ctx.mdev, parms.force_detach);
1714 drbd_adm_finish(info, retcode);
1718 static bool conn_resync_running(struct drbd_tconn *tconn)
1720 struct drbd_conf *mdev;
1725 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1726 if (mdev->state.conn == C_SYNC_SOURCE ||
1727 mdev->state.conn == C_SYNC_TARGET ||
1728 mdev->state.conn == C_PAUSED_SYNC_S ||
1729 mdev->state.conn == C_PAUSED_SYNC_T) {
1739 static bool conn_ov_running(struct drbd_tconn *tconn)
1741 struct drbd_conf *mdev;
1746 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1747 if (mdev->state.conn == C_VERIFY_S ||
1748 mdev->state.conn == C_VERIFY_T) {
1758 static enum drbd_ret_code
1759 _check_net_options(struct drbd_tconn *tconn, struct net_conf *old_conf, struct net_conf *new_conf)
1761 struct drbd_conf *mdev;
1764 if (old_conf && tconn->cstate == C_WF_REPORT_PARAMS && tconn->agreed_pro_version < 100) {
1765 if (new_conf->wire_protocol != old_conf->wire_protocol)
1766 return ERR_NEED_APV_100;
1768 if (new_conf->two_primaries != old_conf->two_primaries)
1769 return ERR_NEED_APV_100;
1771 if (!new_conf->integrity_alg != !old_conf->integrity_alg)
1772 return ERR_NEED_APV_100;
1774 if (strcmp(new_conf->integrity_alg, old_conf->integrity_alg))
1775 return ERR_NEED_APV_100;
1778 if (!new_conf->two_primaries &&
1779 conn_highest_role(tconn) == R_PRIMARY &&
1780 conn_highest_peer(tconn) == R_PRIMARY)
1781 return ERR_NEED_ALLOW_TWO_PRI;
1783 if (new_conf->two_primaries &&
1784 (new_conf->wire_protocol != DRBD_PROT_C))
1785 return ERR_NOT_PROTO_C;
1787 idr_for_each_entry(&tconn->volumes, mdev, i) {
1788 if (get_ldev(mdev)) {
1789 enum drbd_fencing_p fp = rcu_dereference(mdev->ldev->disk_conf)->fencing;
1791 if (new_conf->wire_protocol == DRBD_PROT_A && fp == FP_STONITH)
1792 return ERR_STONITH_AND_PROT_A;
1794 if (mdev->state.role == R_PRIMARY && new_conf->discard_my_data)
1798 if (new_conf->on_congestion != OC_BLOCK && new_conf->wire_protocol != DRBD_PROT_A)
1799 return ERR_CONG_NOT_PROTO_A;
1804 static enum drbd_ret_code
1805 check_net_options(struct drbd_tconn *tconn, struct net_conf *new_conf)
1807 static enum drbd_ret_code rv;
1808 struct drbd_conf *mdev;
1812 rv = _check_net_options(tconn, rcu_dereference(tconn->net_conf), new_conf);
1815 /* tconn->volumes protected by genl_lock() here */
1816 idr_for_each_entry(&tconn->volumes, mdev, i) {
1817 if (!mdev->bitmap) {
1818 if(drbd_bm_init(mdev))
1827 struct crypto_hash *verify_tfm;
1828 struct crypto_hash *csums_tfm;
1829 struct crypto_hash *cram_hmac_tfm;
1830 struct crypto_hash *integrity_tfm;
1834 alloc_hash(struct crypto_hash **tfm, char *tfm_name, int err_alg)
1839 *tfm = crypto_alloc_hash(tfm_name, 0, CRYPTO_ALG_ASYNC);
1848 static enum drbd_ret_code
1849 alloc_crypto(struct crypto *crypto, struct net_conf *new_conf)
1851 char hmac_name[CRYPTO_MAX_ALG_NAME];
1852 enum drbd_ret_code rv;
1854 rv = alloc_hash(&crypto->csums_tfm, new_conf->csums_alg,
1858 rv = alloc_hash(&crypto->verify_tfm, new_conf->verify_alg,
1862 rv = alloc_hash(&crypto->integrity_tfm, new_conf->integrity_alg,
1866 if (new_conf->cram_hmac_alg[0] != 0) {
1867 snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)",
1868 new_conf->cram_hmac_alg);
1870 rv = alloc_hash(&crypto->cram_hmac_tfm, hmac_name,
1877 static void free_crypto(struct crypto *crypto)
1879 crypto_free_hash(crypto->cram_hmac_tfm);
1880 crypto_free_hash(crypto->integrity_tfm);
1881 crypto_free_hash(crypto->csums_tfm);
1882 crypto_free_hash(crypto->verify_tfm);
1885 int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info)
1887 enum drbd_ret_code retcode;
1888 struct drbd_tconn *tconn;
1889 struct net_conf *old_conf, *new_conf = NULL;
1891 int ovr; /* online verify running */
1892 int rsr; /* re-sync running */
1893 struct crypto crypto = { };
1895 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONNECTION);
1896 if (!adm_ctx.reply_skb)
1898 if (retcode != NO_ERROR)
1901 tconn = adm_ctx.tconn;
1903 new_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
1905 retcode = ERR_NOMEM;
1909 conn_reconfig_start(tconn);
1911 mutex_lock(&tconn->data.mutex);
1912 mutex_lock(&tconn->conf_update);
1913 old_conf = tconn->net_conf;
1916 drbd_msg_put_info("net conf missing, try connect");
1917 retcode = ERR_INVALID_REQUEST;
1921 *new_conf = *old_conf;
1922 if (should_set_defaults(info))
1923 set_net_conf_defaults(new_conf);
1925 err = net_conf_from_attrs_for_change(new_conf, info);
1926 if (err && err != -ENOMSG) {
1927 retcode = ERR_MANDATORY_TAG;
1928 drbd_msg_put_info(from_attrs_err_to_txt(err));
1932 retcode = check_net_options(tconn, new_conf);
1933 if (retcode != NO_ERROR)
1936 /* re-sync running */
1937 rsr = conn_resync_running(tconn);
1938 if (rsr && strcmp(new_conf->csums_alg, old_conf->csums_alg)) {
1939 retcode = ERR_CSUMS_RESYNC_RUNNING;
1943 /* online verify running */
1944 ovr = conn_ov_running(tconn);
1945 if (ovr && strcmp(new_conf->verify_alg, old_conf->verify_alg)) {
1946 retcode = ERR_VERIFY_RUNNING;
1950 retcode = alloc_crypto(&crypto, new_conf);
1951 if (retcode != NO_ERROR)
1954 rcu_assign_pointer(tconn->net_conf, new_conf);
1957 crypto_free_hash(tconn->csums_tfm);
1958 tconn->csums_tfm = crypto.csums_tfm;
1959 crypto.csums_tfm = NULL;
1962 crypto_free_hash(tconn->verify_tfm);
1963 tconn->verify_tfm = crypto.verify_tfm;
1964 crypto.verify_tfm = NULL;
1967 crypto_free_hash(tconn->integrity_tfm);
1968 tconn->integrity_tfm = crypto.integrity_tfm;
1969 if (tconn->cstate >= C_WF_REPORT_PARAMS && tconn->agreed_pro_version >= 100)
1970 /* Do this without trying to take tconn->data.mutex again. */
1971 __drbd_send_protocol(tconn, P_PROTOCOL_UPDATE);
1973 crypto_free_hash(tconn->cram_hmac_tfm);
1974 tconn->cram_hmac_tfm = crypto.cram_hmac_tfm;
1976 mutex_unlock(&tconn->conf_update);
1977 mutex_unlock(&tconn->data.mutex);
1981 if (tconn->cstate >= C_WF_REPORT_PARAMS)
1982 drbd_send_sync_param(minor_to_mdev(conn_lowest_minor(tconn)));
1987 mutex_unlock(&tconn->conf_update);
1988 mutex_unlock(&tconn->data.mutex);
1989 free_crypto(&crypto);
1992 conn_reconfig_done(tconn);
1994 drbd_adm_finish(info, retcode);
1998 int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info)
2000 struct drbd_conf *mdev;
2001 struct net_conf *old_conf, *new_conf = NULL;
2002 struct crypto crypto = { };
2003 struct drbd_tconn *tconn;
2004 enum drbd_ret_code retcode;
2008 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
2010 if (!adm_ctx.reply_skb)
2012 if (retcode != NO_ERROR)
2014 if (!(adm_ctx.my_addr && adm_ctx.peer_addr)) {
2015 drbd_msg_put_info("connection endpoint(s) missing");
2016 retcode = ERR_INVALID_REQUEST;
2020 /* No need for _rcu here. All reconfiguration is
2021 * strictly serialized on genl_lock(). We are protected against
2022 * concurrent reconfiguration/addition/deletion */
2023 list_for_each_entry(tconn, &drbd_tconns, all_tconn) {
2024 if (nla_len(adm_ctx.my_addr) == tconn->my_addr_len &&
2025 !memcmp(nla_data(adm_ctx.my_addr), &tconn->my_addr, tconn->my_addr_len)) {
2026 retcode = ERR_LOCAL_ADDR;
2030 if (nla_len(adm_ctx.peer_addr) == tconn->peer_addr_len &&
2031 !memcmp(nla_data(adm_ctx.peer_addr), &tconn->peer_addr, tconn->peer_addr_len)) {
2032 retcode = ERR_PEER_ADDR;
2037 tconn = adm_ctx.tconn;
2038 conn_reconfig_start(tconn);
2040 if (tconn->cstate > C_STANDALONE) {
2041 retcode = ERR_NET_CONFIGURED;
2045 /* allocation not in the IO path, drbdsetup / netlink process context */
2046 new_conf = kzalloc(sizeof(*new_conf), GFP_KERNEL);
2048 retcode = ERR_NOMEM;
2052 set_net_conf_defaults(new_conf);
2054 err = net_conf_from_attrs(new_conf, info);
2055 if (err && err != -ENOMSG) {
2056 retcode = ERR_MANDATORY_TAG;
2057 drbd_msg_put_info(from_attrs_err_to_txt(err));
2061 retcode = check_net_options(tconn, new_conf);
2062 if (retcode != NO_ERROR)
2065 retcode = alloc_crypto(&crypto, new_conf);
2066 if (retcode != NO_ERROR)
2069 ((char *)new_conf->shared_secret)[SHARED_SECRET_MAX-1] = 0;
2071 conn_flush_workqueue(tconn);
2073 mutex_lock(&tconn->conf_update);
2074 old_conf = tconn->net_conf;
2076 retcode = ERR_NET_CONFIGURED;
2077 mutex_unlock(&tconn->conf_update);
2080 rcu_assign_pointer(tconn->net_conf, new_conf);
2082 conn_free_crypto(tconn);
2083 tconn->cram_hmac_tfm = crypto.cram_hmac_tfm;
2084 tconn->integrity_tfm = crypto.integrity_tfm;
2085 tconn->csums_tfm = crypto.csums_tfm;
2086 tconn->verify_tfm = crypto.verify_tfm;
2088 tconn->my_addr_len = nla_len(adm_ctx.my_addr);
2089 memcpy(&tconn->my_addr, nla_data(adm_ctx.my_addr), tconn->my_addr_len);
2090 tconn->peer_addr_len = nla_len(adm_ctx.peer_addr);
2091 memcpy(&tconn->peer_addr, nla_data(adm_ctx.peer_addr), tconn->peer_addr_len);
2093 mutex_unlock(&tconn->conf_update);
2096 idr_for_each_entry(&tconn->volumes, mdev, i) {
2102 retcode = conn_request_state(tconn, NS(conn, C_UNCONNECTED), CS_VERBOSE);
2104 conn_reconfig_done(tconn);
2105 drbd_adm_finish(info, retcode);
2109 free_crypto(&crypto);
2112 conn_reconfig_done(tconn);
2114 drbd_adm_finish(info, retcode);
2118 static enum drbd_state_rv conn_try_disconnect(struct drbd_tconn *tconn, bool force)
2120 enum drbd_state_rv rv;
2122 rv = conn_request_state(tconn, NS(conn, C_DISCONNECTING),
2123 force ? CS_HARD : 0);
2126 case SS_NOTHING_TO_DO:
2128 case SS_ALREADY_STANDALONE:
2130 case SS_PRIMARY_NOP:
2131 /* Our state checking code wants to see the peer outdated. */
2132 rv = conn_request_state(tconn, NS2(conn, C_DISCONNECTING,
2133 pdsk, D_OUTDATED), CS_VERBOSE);
2135 case SS_CW_FAILED_BY_PEER:
2136 /* The peer probably wants to see us outdated. */
2137 rv = conn_request_state(tconn, NS2(conn, C_DISCONNECTING,
2138 disk, D_OUTDATED), 0);
2139 if (rv == SS_IS_DISKLESS || rv == SS_LOWER_THAN_OUTDATED) {
2140 rv = conn_request_state(tconn, NS(conn, C_DISCONNECTING),
2145 /* no special handling necessary */
2148 if (rv >= SS_SUCCESS) {
2149 enum drbd_state_rv rv2;
2150 /* No one else can reconfigure the network while I am here.
2151 * The state handling only uses drbd_thread_stop_nowait(),
2152 * we want to really wait here until the receiver is no more.
2154 drbd_thread_stop(&adm_ctx.tconn->receiver);
2156 /* Race breaker. This additional state change request may be
2157 * necessary, if this was a forced disconnect during a receiver
2158 * restart. We may have "killed" the receiver thread just
2159 * after drbdd_init() returned. Typically, we should be
2160 * C_STANDALONE already, now, and this becomes a no-op.
2162 rv2 = conn_request_state(tconn, NS(conn, C_STANDALONE),
2163 CS_VERBOSE | CS_HARD);
2164 if (rv2 < SS_SUCCESS)
2166 "unexpected rv2=%d in conn_try_disconnect()\n",
2172 int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info)
2174 struct disconnect_parms parms;
2175 struct drbd_tconn *tconn;
2176 enum drbd_state_rv rv;
2177 enum drbd_ret_code retcode;
2180 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONNECTION);
2181 if (!adm_ctx.reply_skb)
2183 if (retcode != NO_ERROR)
2186 tconn = adm_ctx.tconn;
2187 memset(&parms, 0, sizeof(parms));
2188 if (info->attrs[DRBD_NLA_DISCONNECT_PARMS]) {
2189 err = disconnect_parms_from_attrs(&parms, info);
2191 retcode = ERR_MANDATORY_TAG;
2192 drbd_msg_put_info(from_attrs_err_to_txt(err));
2197 rv = conn_try_disconnect(tconn, parms.force_disconnect);
2198 if (rv < SS_SUCCESS)
2199 retcode = rv; /* FIXME: Type mismatch. */
2203 drbd_adm_finish(info, retcode);
2207 void resync_after_online_grow(struct drbd_conf *mdev)
2209 int iass; /* I am sync source */
2211 dev_info(DEV, "Resync of new storage after online grow\n");
2212 if (mdev->state.role != mdev->state.peer)
2213 iass = (mdev->state.role == R_PRIMARY);
2215 iass = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags);
2218 drbd_start_resync(mdev, C_SYNC_SOURCE);
2220 _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE + CS_SERIALIZE);
2223 int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info)
2225 struct disk_conf *old_disk_conf, *new_disk_conf = NULL;
2226 struct resize_parms rs;
2227 struct drbd_conf *mdev;
2228 enum drbd_ret_code retcode;
2229 enum determine_dev_size dd;
2230 enum dds_flags ddsf;
2234 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2235 if (!adm_ctx.reply_skb)
2237 if (retcode != NO_ERROR)
2240 memset(&rs, 0, sizeof(struct resize_parms));
2241 if (info->attrs[DRBD_NLA_RESIZE_PARMS]) {
2242 err = resize_parms_from_attrs(&rs, info);
2244 retcode = ERR_MANDATORY_TAG;
2245 drbd_msg_put_info(from_attrs_err_to_txt(err));
2250 mdev = adm_ctx.mdev;
2251 if (mdev->state.conn > C_CONNECTED) {
2252 retcode = ERR_RESIZE_RESYNC;
2256 if (mdev->state.role == R_SECONDARY &&
2257 mdev->state.peer == R_SECONDARY) {
2258 retcode = ERR_NO_PRIMARY;
2262 if (!get_ldev(mdev)) {
2263 retcode = ERR_NO_DISK;
2267 if (rs.no_resync && mdev->tconn->agreed_pro_version < 93) {
2268 retcode = ERR_NEED_APV_93;
2273 u_size = rcu_dereference(mdev->ldev->disk_conf)->disk_size;
2275 if (u_size != (sector_t)rs.resize_size) {
2276 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
2277 if (!new_disk_conf) {
2278 retcode = ERR_NOMEM;
2283 if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev))
2284 mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev);
2286 if (new_disk_conf) {
2287 mutex_lock(&mdev->tconn->conf_update);
2288 old_disk_conf = mdev->ldev->disk_conf;
2289 *new_disk_conf = *old_disk_conf;
2290 new_disk_conf->disk_size = (sector_t)rs.resize_size;
2291 rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
2292 mutex_unlock(&mdev->tconn->conf_update);
2294 kfree(old_disk_conf);
2297 ddsf = (rs.resize_force ? DDSF_FORCED : 0) | (rs.no_resync ? DDSF_NO_RESYNC : 0);
2298 dd = drbd_determine_dev_size(mdev, ddsf);
2301 if (dd == dev_size_error) {
2302 retcode = ERR_NOMEM_BITMAP;
2306 if (mdev->state.conn == C_CONNECTED) {
2308 set_bit(RESIZE_PENDING, &mdev->flags);
2310 drbd_send_uuids(mdev);
2311 drbd_send_sizes(mdev, 1, ddsf);
2315 drbd_adm_finish(info, retcode);
2323 int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info)
2325 enum drbd_ret_code retcode;
2326 struct drbd_tconn *tconn;
2327 struct res_opts res_opts;
2330 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
2331 if (!adm_ctx.reply_skb)
2333 if (retcode != NO_ERROR)
2335 tconn = adm_ctx.tconn;
2337 res_opts = tconn->res_opts;
2338 if (should_set_defaults(info))
2339 set_res_opts_defaults(&res_opts);
2341 err = res_opts_from_attrs(&res_opts, info);
2342 if (err && err != -ENOMSG) {
2343 retcode = ERR_MANDATORY_TAG;
2344 drbd_msg_put_info(from_attrs_err_to_txt(err));
2348 err = set_resource_options(tconn, &res_opts);
2350 retcode = ERR_INVALID_REQUEST;
2352 retcode = ERR_NOMEM;
2356 drbd_adm_finish(info, retcode);
2360 int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info)
2362 struct drbd_conf *mdev;
2363 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2365 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2366 if (!adm_ctx.reply_skb)
2368 if (retcode != NO_ERROR)
2371 mdev = adm_ctx.mdev;
2373 /* If there is still bitmap IO pending, probably because of a previous
2374 * resync just being finished, wait for it before requesting a new resync. */
2375 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
2377 retcode = _drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T), CS_ORDERED);
2379 if (retcode < SS_SUCCESS && retcode != SS_NEED_CONNECTION)
2380 retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T));
2382 while (retcode == SS_NEED_CONNECTION) {
2383 spin_lock_irq(&mdev->tconn->req_lock);
2384 if (mdev->state.conn < C_CONNECTED)
2385 retcode = _drbd_set_state(_NS(mdev, disk, D_INCONSISTENT), CS_VERBOSE, NULL);
2386 spin_unlock_irq(&mdev->tconn->req_lock);
2388 if (retcode != SS_NEED_CONNECTION)
2391 retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T));
2395 drbd_adm_finish(info, retcode);
2399 static int drbd_adm_simple_request_state(struct sk_buff *skb, struct genl_info *info,
2400 union drbd_state mask, union drbd_state val)
2402 enum drbd_ret_code retcode;
2404 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2405 if (!adm_ctx.reply_skb)
2407 if (retcode != NO_ERROR)
2410 retcode = drbd_request_state(adm_ctx.mdev, mask, val);
2412 drbd_adm_finish(info, retcode);
2416 static int drbd_bmio_set_susp_al(struct drbd_conf *mdev)
2420 rv = drbd_bmio_set_n_write(mdev);
2421 drbd_suspend_al(mdev);
2425 int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info)
2427 int retcode; /* drbd_ret_code, drbd_state_rv */
2428 struct drbd_conf *mdev;
2430 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2431 if (!adm_ctx.reply_skb)
2433 if (retcode != NO_ERROR)
2436 mdev = adm_ctx.mdev;
2438 retcode = _drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_S), CS_ORDERED);
2439 if (retcode < SS_SUCCESS) {
2440 if (retcode == SS_NEED_CONNECTION && mdev->state.role == R_PRIMARY) {
2441 /* The peer will get a resync upon connect anyways.
2442 * Just make that into a full resync. */
2443 retcode = drbd_request_state(mdev, NS(pdsk, D_INCONSISTENT));
2444 if (retcode >= SS_SUCCESS) {
2445 if (drbd_bitmap_io(mdev, &drbd_bmio_set_susp_al,
2446 "set_n_write from invalidate_peer",
2447 BM_LOCKED_SET_ALLOWED))
2448 retcode = ERR_IO_MD_DISK;
2451 retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_S));
2455 drbd_adm_finish(info, retcode);
2459 int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info)
2461 enum drbd_ret_code retcode;
2463 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2464 if (!adm_ctx.reply_skb)
2466 if (retcode != NO_ERROR)
2469 if (drbd_request_state(adm_ctx.mdev, NS(user_isp, 1)) == SS_NOTHING_TO_DO)
2470 retcode = ERR_PAUSE_IS_SET;
2472 drbd_adm_finish(info, retcode);
2476 int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info)
2478 union drbd_dev_state s;
2479 enum drbd_ret_code retcode;
2481 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2482 if (!adm_ctx.reply_skb)
2484 if (retcode != NO_ERROR)
2487 if (drbd_request_state(adm_ctx.mdev, NS(user_isp, 0)) == SS_NOTHING_TO_DO) {
2488 s = adm_ctx.mdev->state;
2489 if (s.conn == C_PAUSED_SYNC_S || s.conn == C_PAUSED_SYNC_T) {
2490 retcode = s.aftr_isp ? ERR_PIC_AFTER_DEP :
2491 s.peer_isp ? ERR_PIC_PEER_DEP : ERR_PAUSE_IS_CLEAR;
2493 retcode = ERR_PAUSE_IS_CLEAR;
2498 drbd_adm_finish(info, retcode);
2502 int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info)
2504 return drbd_adm_simple_request_state(skb, info, NS(susp, 1));
2507 int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info)
2509 struct drbd_conf *mdev;
2510 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2512 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2513 if (!adm_ctx.reply_skb)
2515 if (retcode != NO_ERROR)
2518 mdev = adm_ctx.mdev;
2519 if (test_bit(NEW_CUR_UUID, &mdev->flags)) {
2520 drbd_uuid_new_current(mdev);
2521 clear_bit(NEW_CUR_UUID, &mdev->flags);
2523 drbd_suspend_io(mdev);
2524 retcode = drbd_request_state(mdev, NS3(susp, 0, susp_nod, 0, susp_fen, 0));
2525 if (retcode == SS_SUCCESS) {
2526 if (mdev->state.conn < C_CONNECTED)
2527 tl_clear(mdev->tconn);
2528 if (mdev->state.disk == D_DISKLESS || mdev->state.disk == D_FAILED)
2529 tl_restart(mdev->tconn, FAIL_FROZEN_DISK_IO);
2531 drbd_resume_io(mdev);
2534 drbd_adm_finish(info, retcode);
2538 int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info)
2540 return drbd_adm_simple_request_state(skb, info, NS(disk, D_OUTDATED));
2543 int nla_put_drbd_cfg_context(struct sk_buff *skb, struct drbd_tconn *tconn, unsigned vnr)
2546 nla = nla_nest_start(skb, DRBD_NLA_CFG_CONTEXT);
2548 goto nla_put_failure;
2549 if (vnr != VOLUME_UNSPECIFIED)
2550 NLA_PUT_U32(skb, T_ctx_volume, vnr);
2551 NLA_PUT_STRING(skb, T_ctx_resource_name, tconn->name);
2552 if (tconn->my_addr_len)
2553 NLA_PUT(skb, T_ctx_my_addr, tconn->my_addr_len, &tconn->my_addr);
2554 if (tconn->peer_addr_len)
2555 NLA_PUT(skb, T_ctx_peer_addr, tconn->peer_addr_len, &tconn->peer_addr);
2556 nla_nest_end(skb, nla);
2561 nla_nest_cancel(skb, nla);
2565 int nla_put_status_info(struct sk_buff *skb, struct drbd_conf *mdev,
2566 const struct sib_info *sib)
2568 struct state_info *si = NULL; /* for sizeof(si->member); */
2569 struct net_conf *nc;
2573 int exclude_sensitive;
2575 /* If sib != NULL, this is drbd_bcast_event, which anyone can listen
2576 * to. So we better exclude_sensitive information.
2578 * If sib == NULL, this is drbd_adm_get_status, executed synchronously
2579 * in the context of the requesting user process. Exclude sensitive
2580 * information, unless current has superuser.
2582 * NOTE: for drbd_adm_get_status_all(), this is a netlink dump, and
2583 * relies on the current implementation of netlink_dump(), which
2584 * executes the dump callback successively from netlink_recvmsg(),
2585 * always in the context of the receiving process */
2586 exclude_sensitive = sib || !capable(CAP_SYS_ADMIN);
2588 got_ldev = get_ldev(mdev);
2590 /* We need to add connection name and volume number information still.
2591 * Minor number is in drbd_genlmsghdr. */
2592 if (nla_put_drbd_cfg_context(skb, mdev->tconn, mdev->vnr))
2593 goto nla_put_failure;
2595 if (res_opts_to_skb(skb, &mdev->tconn->res_opts, exclude_sensitive))
2596 goto nla_put_failure;
2600 if (disk_conf_to_skb(skb, rcu_dereference(mdev->ldev->disk_conf), exclude_sensitive))
2601 goto nla_put_failure;
2603 nc = rcu_dereference(mdev->tconn->net_conf);
2605 err = net_conf_to_skb(skb, nc, exclude_sensitive);
2608 goto nla_put_failure;
2610 nla = nla_nest_start(skb, DRBD_NLA_STATE_INFO);
2612 goto nla_put_failure;
2613 NLA_PUT_U32(skb, T_sib_reason, sib ? sib->sib_reason : SIB_GET_STATUS_REPLY);
2614 NLA_PUT_U32(skb, T_current_state, mdev->state.i);
2615 NLA_PUT_U64(skb, T_ed_uuid, mdev->ed_uuid);
2616 NLA_PUT_U64(skb, T_capacity, drbd_get_capacity(mdev->this_bdev));
2619 NLA_PUT_U32(skb, T_disk_flags, mdev->ldev->md.flags);
2620 NLA_PUT(skb, T_uuids, sizeof(si->uuids), mdev->ldev->md.uuid);
2621 NLA_PUT_U64(skb, T_bits_total, drbd_bm_bits(mdev));
2622 NLA_PUT_U64(skb, T_bits_oos, drbd_bm_total_weight(mdev));
2623 if (C_SYNC_SOURCE <= mdev->state.conn &&
2624 C_PAUSED_SYNC_T >= mdev->state.conn) {
2625 NLA_PUT_U64(skb, T_bits_rs_total, mdev->rs_total);
2626 NLA_PUT_U64(skb, T_bits_rs_failed, mdev->rs_failed);
2631 switch(sib->sib_reason) {
2632 case SIB_SYNC_PROGRESS:
2633 case SIB_GET_STATUS_REPLY:
2635 case SIB_STATE_CHANGE:
2636 NLA_PUT_U32(skb, T_prev_state, sib->os.i);
2637 NLA_PUT_U32(skb, T_new_state, sib->ns.i);
2639 case SIB_HELPER_POST:
2641 T_helper_exit_code, sib->helper_exit_code);
2643 case SIB_HELPER_PRE:
2644 NLA_PUT_STRING(skb, T_helper, sib->helper_name);
2648 nla_nest_end(skb, nla);
2658 int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info)
2660 enum drbd_ret_code retcode;
2663 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2664 if (!adm_ctx.reply_skb)
2666 if (retcode != NO_ERROR)
2669 err = nla_put_status_info(adm_ctx.reply_skb, adm_ctx.mdev, NULL);
2671 nlmsg_free(adm_ctx.reply_skb);
2675 drbd_adm_finish(info, retcode);
2679 int get_one_status(struct sk_buff *skb, struct netlink_callback *cb)
2681 struct drbd_conf *mdev;
2682 struct drbd_genlmsghdr *dh;
2683 struct drbd_tconn *pos = (struct drbd_tconn*)cb->args[0];
2684 struct drbd_tconn *tconn = NULL;
2685 struct drbd_tconn *tmp;
2686 unsigned volume = cb->args[1];
2688 /* Open coded, deferred, iteration:
2689 * list_for_each_entry_safe(tconn, tmp, &drbd_tconns, all_tconn) {
2690 * idr_for_each_entry(&tconn->volumes, mdev, i) {
2694 * where tconn is cb->args[0];
2695 * and i is cb->args[1];
2697 * cb->args[2] indicates if we shall loop over all resources,
2698 * or just dump all volumes of a single resource.
2700 * This may miss entries inserted after this dump started,
2701 * or entries deleted before they are reached.
2703 * We need to make sure the mdev won't disappear while
2704 * we are looking at it, and revalidate our iterators
2705 * on each iteration.
2708 /* synchronize with conn_create()/conn_destroy() */
2710 /* revalidate iterator position */
2711 list_for_each_entry_rcu(tmp, &drbd_tconns, all_tconn) {
2713 /* first iteration */
2725 mdev = idr_get_next(&tconn->volumes, &volume);
2727 /* No more volumes to dump on this tconn.
2728 * Advance tconn iterator. */
2729 pos = list_entry_rcu(tconn->all_tconn.next,
2730 struct drbd_tconn, all_tconn);
2731 /* Did we dump any volume on this tconn yet? */
2733 /* If we reached the end of the list,
2734 * or only a single resource dump was requested,
2736 if (&pos->all_tconn == &drbd_tconns || cb->args[2])
2744 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).pid,
2745 cb->nlh->nlmsg_seq, &drbd_genl_family,
2746 NLM_F_MULTI, DRBD_ADM_GET_STATUS);
2751 /* This is a tconn without a single volume.
2752 * Suprisingly enough, it may have a network
2754 struct net_conf *nc;
2756 dh->ret_code = NO_ERROR;
2757 if (nla_put_drbd_cfg_context(skb, tconn, VOLUME_UNSPECIFIED))
2759 nc = rcu_dereference(tconn->net_conf);
2760 if (nc && net_conf_to_skb(skb, nc, 1) != 0)
2765 D_ASSERT(mdev->vnr == volume);
2766 D_ASSERT(mdev->tconn == tconn);
2768 dh->minor = mdev_to_minor(mdev);
2769 dh->ret_code = NO_ERROR;
2771 if (nla_put_status_info(skb, mdev, NULL)) {
2773 genlmsg_cancel(skb, dh);
2777 genlmsg_end(skb, dh);
2782 /* where to start the next iteration */
2783 cb->args[0] = (long)pos;
2784 cb->args[1] = (pos == tconn) ? volume + 1 : 0;
2786 /* No more tconns/volumes/minors found results in an empty skb.
2787 * Which will terminate the dump. */
2792 * Request status of all resources, or of all volumes within a single resource.
2794 * This is a dump, as the answer may not fit in a single reply skb otherwise.
2795 * Which means we cannot use the family->attrbuf or other such members, because
2796 * dump is NOT protected by the genl_lock(). During dump, we only have access
2797 * to the incoming skb, and need to opencode "parsing" of the nlattr payload.
2799 * Once things are setup properly, we call into get_one_status().
2801 int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb)
2803 const unsigned hdrlen = GENL_HDRLEN + GENL_MAGIC_FAMILY_HDRSZ;
2805 const char *resource_name;
2806 struct drbd_tconn *tconn;
2809 /* Is this a followup call? */
2811 /* ... of a single resource dump,
2812 * and the resource iterator has been advanced already? */
2813 if (cb->args[2] && cb->args[2] != cb->args[0])
2814 return 0; /* DONE. */
2818 /* First call (from netlink_dump_start). We need to figure out
2819 * which resource(s) the user wants us to dump. */
2820 nla = nla_find(nlmsg_attrdata(cb->nlh, hdrlen),
2821 nlmsg_attrlen(cb->nlh, hdrlen),
2822 DRBD_NLA_CFG_CONTEXT);
2824 /* No explicit context given. Dump all. */
2827 maxtype = ARRAY_SIZE(drbd_cfg_context_nl_policy) - 1;
2828 nla = drbd_nla_find_nested(maxtype, nla, __nla_type(T_ctx_resource_name));
2830 return PTR_ERR(nla);
2831 /* context given, but no name present? */
2834 resource_name = nla_data(nla);
2835 tconn = conn_get_by_name(resource_name);
2840 kref_put(&tconn->kref, &conn_destroy); /* get_one_status() (re)validates tconn by itself */
2842 /* prime iterators, and set "filter" mode mark:
2843 * only dump this tconn. */
2844 cb->args[0] = (long)tconn;
2845 /* cb->args[1] = 0; passed in this way. */
2846 cb->args[2] = (long)tconn;
2849 return get_one_status(skb, cb);
2852 int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info)
2854 enum drbd_ret_code retcode;
2855 struct timeout_parms tp;
2858 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2859 if (!adm_ctx.reply_skb)
2861 if (retcode != NO_ERROR)
2865 adm_ctx.mdev->state.pdsk == D_OUTDATED ? UT_PEER_OUTDATED :
2866 test_bit(USE_DEGR_WFC_T, &adm_ctx.mdev->flags) ? UT_DEGRADED :
2869 err = timeout_parms_to_priv_skb(adm_ctx.reply_skb, &tp);
2871 nlmsg_free(adm_ctx.reply_skb);
2875 drbd_adm_finish(info, retcode);
2879 int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info)
2881 struct drbd_conf *mdev;
2882 enum drbd_ret_code retcode;
2884 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2885 if (!adm_ctx.reply_skb)
2887 if (retcode != NO_ERROR)
2890 mdev = adm_ctx.mdev;
2891 if (info->attrs[DRBD_NLA_START_OV_PARMS]) {
2892 /* resume from last known position, if possible */
2893 struct start_ov_parms parms =
2894 { .ov_start_sector = mdev->ov_start_sector };
2895 int err = start_ov_parms_from_attrs(&parms, info);
2897 retcode = ERR_MANDATORY_TAG;
2898 drbd_msg_put_info(from_attrs_err_to_txt(err));
2901 /* w_make_ov_request expects position to be aligned */
2902 mdev->ov_start_sector = parms.ov_start_sector & ~BM_SECT_PER_BIT;
2904 /* If there is still bitmap IO pending, e.g. previous resync or verify
2905 * just being finished, wait for it before requesting a new resync. */
2906 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
2907 retcode = drbd_request_state(mdev,NS(conn,C_VERIFY_S));
2909 drbd_adm_finish(info, retcode);
2914 int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info)
2916 struct drbd_conf *mdev;
2917 enum drbd_ret_code retcode;
2918 int skip_initial_sync = 0;
2920 struct new_c_uuid_parms args;
2922 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2923 if (!adm_ctx.reply_skb)
2925 if (retcode != NO_ERROR)
2928 mdev = adm_ctx.mdev;
2929 memset(&args, 0, sizeof(args));
2930 if (info->attrs[DRBD_NLA_NEW_C_UUID_PARMS]) {
2931 err = new_c_uuid_parms_from_attrs(&args, info);
2933 retcode = ERR_MANDATORY_TAG;
2934 drbd_msg_put_info(from_attrs_err_to_txt(err));
2939 mutex_lock(mdev->state_mutex); /* Protects us against serialized state changes. */
2941 if (!get_ldev(mdev)) {
2942 retcode = ERR_NO_DISK;
2946 /* this is "skip initial sync", assume to be clean */
2947 if (mdev->state.conn == C_CONNECTED && mdev->tconn->agreed_pro_version >= 90 &&
2948 mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED && args.clear_bm) {
2949 dev_info(DEV, "Preparing to skip initial sync\n");
2950 skip_initial_sync = 1;
2951 } else if (mdev->state.conn != C_STANDALONE) {
2952 retcode = ERR_CONNECTED;
2956 drbd_uuid_set(mdev, UI_BITMAP, 0); /* Rotate UI_BITMAP to History 1, etc... */
2957 drbd_uuid_new_current(mdev); /* New current, previous to UI_BITMAP */
2959 if (args.clear_bm) {
2960 err = drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write,
2961 "clear_n_write from new_c_uuid", BM_LOCKED_MASK);
2963 dev_err(DEV, "Writing bitmap failed with %d\n",err);
2964 retcode = ERR_IO_MD_DISK;
2966 if (skip_initial_sync) {
2967 drbd_send_uuids_skip_initial_sync(mdev);
2968 _drbd_uuid_set(mdev, UI_BITMAP, 0);
2969 drbd_print_uuids(mdev, "cleared bitmap UUID");
2970 spin_lock_irq(&mdev->tconn->req_lock);
2971 _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
2973 spin_unlock_irq(&mdev->tconn->req_lock);
2981 mutex_unlock(mdev->state_mutex);
2983 drbd_adm_finish(info, retcode);
2987 static enum drbd_ret_code
2988 drbd_check_resource_name(const char *name)
2990 if (!name || !name[0]) {
2991 drbd_msg_put_info("resource name missing");
2992 return ERR_MANDATORY_TAG;
2994 /* if we want to use these in sysfs/configfs/debugfs some day,
2995 * we must not allow slashes */
2996 if (strchr(name, '/')) {
2997 drbd_msg_put_info("invalid resource name");
2998 return ERR_INVALID_REQUEST;
3003 int drbd_adm_new_resource(struct sk_buff *skb, struct genl_info *info)
3005 enum drbd_ret_code retcode;
3006 struct res_opts res_opts;
3009 retcode = drbd_adm_prepare(skb, info, 0);
3010 if (!adm_ctx.reply_skb)
3012 if (retcode != NO_ERROR)
3015 set_res_opts_defaults(&res_opts);
3016 err = res_opts_from_attrs(&res_opts, info);
3017 if (err && err != -ENOMSG) {
3018 retcode = ERR_MANDATORY_TAG;
3019 drbd_msg_put_info(from_attrs_err_to_txt(err));
3023 retcode = drbd_check_resource_name(adm_ctx.resource_name);
3024 if (retcode != NO_ERROR)
3027 if (adm_ctx.tconn) {
3028 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL) {
3029 retcode = ERR_INVALID_REQUEST;
3030 drbd_msg_put_info("resource exists");
3032 /* else: still NO_ERROR */
3036 if (!conn_create(adm_ctx.resource_name, &res_opts))
3037 retcode = ERR_NOMEM;
3039 drbd_adm_finish(info, retcode);
3043 int drbd_adm_add_minor(struct sk_buff *skb, struct genl_info *info)
3045 struct drbd_genlmsghdr *dh = info->userhdr;
3046 enum drbd_ret_code retcode;
3048 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
3049 if (!adm_ctx.reply_skb)
3051 if (retcode != NO_ERROR)
3054 if (dh->minor > MINORMASK) {
3055 drbd_msg_put_info("requested minor out of range");
3056 retcode = ERR_INVALID_REQUEST;
3059 if (adm_ctx.volume > DRBD_VOLUME_MAX) {
3060 drbd_msg_put_info("requested volume id out of range");
3061 retcode = ERR_INVALID_REQUEST;
3065 /* drbd_adm_prepare made sure already
3066 * that mdev->tconn and mdev->vnr match the request. */
3068 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
3069 retcode = ERR_MINOR_EXISTS;
3070 /* else: still NO_ERROR */
3074 retcode = conn_new_minor(adm_ctx.tconn, dh->minor, adm_ctx.volume);
3076 drbd_adm_finish(info, retcode);
3080 static enum drbd_ret_code adm_delete_minor(struct drbd_conf *mdev)
3082 if (mdev->state.disk == D_DISKLESS &&
3083 /* no need to be mdev->state.conn == C_STANDALONE &&
3084 * we may want to delete a minor from a live replication group.
3086 mdev->state.role == R_SECONDARY) {
3087 _drbd_request_state(mdev, NS(conn, C_WF_REPORT_PARAMS),
3088 CS_VERBOSE + CS_WAIT_COMPLETE);
3089 idr_remove(&mdev->tconn->volumes, mdev->vnr);
3090 idr_remove(&minors, mdev_to_minor(mdev));
3091 del_gendisk(mdev->vdisk);
3093 kref_put(&mdev->kref, &drbd_minor_destroy);
3096 return ERR_MINOR_CONFIGURED;
3099 int drbd_adm_delete_minor(struct sk_buff *skb, struct genl_info *info)
3101 enum drbd_ret_code retcode;
3103 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
3104 if (!adm_ctx.reply_skb)
3106 if (retcode != NO_ERROR)
3109 retcode = adm_delete_minor(adm_ctx.mdev);
3111 drbd_adm_finish(info, retcode);
3115 int drbd_adm_down(struct sk_buff *skb, struct genl_info *info)
3117 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
3118 struct drbd_conf *mdev;
3121 retcode = drbd_adm_prepare(skb, info, 0);
3122 if (!adm_ctx.reply_skb)
3124 if (retcode != NO_ERROR)
3127 if (!adm_ctx.tconn) {
3128 retcode = ERR_RES_NOT_KNOWN;
3133 idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
3134 retcode = drbd_set_role(mdev, R_SECONDARY, 0);
3135 if (retcode < SS_SUCCESS) {
3136 drbd_msg_put_info("failed to demote");
3141 retcode = conn_try_disconnect(adm_ctx.tconn, 0);
3142 if (retcode < SS_SUCCESS) {
3143 drbd_msg_put_info("failed to disconnect");
3148 idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
3149 retcode = adm_detach(mdev, 0);
3150 if (retcode < SS_SUCCESS) {
3151 drbd_msg_put_info("failed to detach");
3156 /* If we reach this, all volumes (of this tconn) are Secondary,
3157 * Disconnected, Diskless, aka Unconfigured. Make sure all threads have
3158 * actually stopped, state handling only does drbd_thread_stop_nowait(). */
3159 drbd_thread_stop(&adm_ctx.tconn->worker);
3161 /* Now, nothing can fail anymore */
3163 /* delete volumes */
3164 idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
3165 retcode = adm_delete_minor(mdev);
3166 if (retcode != NO_ERROR) {
3167 /* "can not happen" */
3168 drbd_msg_put_info("failed to delete volume");
3173 /* delete connection */
3174 if (conn_lowest_minor(adm_ctx.tconn) < 0) {
3175 list_del_rcu(&adm_ctx.tconn->all_tconn);
3177 kref_put(&adm_ctx.tconn->kref, &conn_destroy);
3181 /* "can not happen" */
3182 retcode = ERR_RES_IN_USE;
3183 drbd_msg_put_info("failed to delete connection");
3187 drbd_adm_finish(info, retcode);
3191 int drbd_adm_del_resource(struct sk_buff *skb, struct genl_info *info)
3193 enum drbd_ret_code retcode;
3195 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
3196 if (!adm_ctx.reply_skb)
3198 if (retcode != NO_ERROR)
3201 if (conn_lowest_minor(adm_ctx.tconn) < 0) {
3202 list_del_rcu(&adm_ctx.tconn->all_tconn);
3204 kref_put(&adm_ctx.tconn->kref, &conn_destroy);
3208 retcode = ERR_RES_IN_USE;
3211 if (retcode == NO_ERROR)
3212 drbd_thread_stop(&adm_ctx.tconn->worker);
3214 drbd_adm_finish(info, retcode);
3218 void drbd_bcast_event(struct drbd_conf *mdev, const struct sib_info *sib)
3220 static atomic_t drbd_genl_seq = ATOMIC_INIT(2); /* two. */
3221 struct sk_buff *msg;
3222 struct drbd_genlmsghdr *d_out;
3226 seq = atomic_inc_return(&drbd_genl_seq);
3227 msg = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
3232 d_out = genlmsg_put(msg, 0, seq, &drbd_genl_family, 0, DRBD_EVENT);
3233 if (!d_out) /* cannot happen, but anyways. */
3234 goto nla_put_failure;
3235 d_out->minor = mdev_to_minor(mdev);
3236 d_out->ret_code = NO_ERROR;
3238 if (nla_put_status_info(msg, mdev, sib))
3239 goto nla_put_failure;
3240 genlmsg_end(msg, d_out);
3241 err = drbd_genl_multicast_events(msg, 0);
3242 /* msg has been consumed or freed in netlink_broadcast() */
3243 if (err && err != -ESRCH)
3251 dev_err(DEV, "Error %d while broadcasting event. "
3252 "Event seq:%u sib_reason:%u\n",
3253 err, seq, sib->sib_reason);