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_create_connection(struct sk_buff *skb, struct genl_info *info);
51 int drbd_adm_delete_connection(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>
78 #include <linux/genl_magic_func.h>
80 /* used blkdev_get_by_path, to claim our meta data device(s) */
81 static char *drbd_m_holder = "Hands off! this is DRBD's meta data device.";
83 /* Configuration is strictly serialized, because generic netlink message
84 * processing is strictly serialized by the genl_lock().
85 * Which means we can use one static global drbd_config_context struct.
87 static struct drbd_config_context {
88 /* assigned from drbd_genlmsghdr */
90 /* assigned from request attributes, if present */
92 #define VOLUME_UNSPECIFIED (-1U)
93 /* pointer into the request skb,
94 * limited lifetime! */
98 struct sk_buff *reply_skb;
99 /* pointer into reply buffer */
100 struct drbd_genlmsghdr *reply_dh;
101 /* resolved from attributes, if possible */
102 struct drbd_conf *mdev;
103 struct drbd_tconn *tconn;
106 static void drbd_adm_send_reply(struct sk_buff *skb, struct genl_info *info)
108 genlmsg_end(skb, genlmsg_data(nlmsg_data(nlmsg_hdr(skb))));
109 if (genlmsg_reply(skb, info))
110 printk(KERN_ERR "drbd: error sending genl reply\n");
113 /* Used on a fresh "drbd_adm_prepare"d reply_skb, this cannot fail: The only
114 * reason it could fail was no space in skb, and there are 4k available. */
115 int drbd_msg_put_info(const char *info)
117 struct sk_buff *skb = adm_ctx.reply_skb;
121 if (!info || !info[0])
124 nla = nla_nest_start(skb, DRBD_NLA_CFG_REPLY);
128 err = nla_put_string(skb, T_info_text, info);
130 nla_nest_cancel(skb, nla);
133 nla_nest_end(skb, nla);
137 /* This would be a good candidate for a "pre_doit" hook,
138 * and per-family private info->pointers.
139 * But we need to stay compatible with older kernels.
140 * If it returns successfully, adm_ctx members are valid.
142 #define DRBD_ADM_NEED_MINOR 1
143 #define DRBD_ADM_NEED_CONN 2
144 static int drbd_adm_prepare(struct sk_buff *skb, struct genl_info *info,
147 struct drbd_genlmsghdr *d_in = info->userhdr;
148 const u8 cmd = info->genlhdr->cmd;
151 memset(&adm_ctx, 0, sizeof(adm_ctx));
153 /* genl_rcv_msg only checks for CAP_NET_ADMIN on "GENL_ADMIN_PERM" :( */
154 if (cmd != DRBD_ADM_GET_STATUS
155 && security_netlink_recv(skb, CAP_SYS_ADMIN))
158 adm_ctx.reply_skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
159 if (!adm_ctx.reply_skb)
162 adm_ctx.reply_dh = genlmsg_put_reply(adm_ctx.reply_skb,
163 info, &drbd_genl_family, 0, cmd);
164 /* put of a few bytes into a fresh skb of >= 4k will always succeed.
166 if (!adm_ctx.reply_dh)
169 adm_ctx.reply_dh->minor = d_in->minor;
170 adm_ctx.reply_dh->ret_code = NO_ERROR;
172 if (info->attrs[DRBD_NLA_CFG_CONTEXT]) {
174 /* parse and validate only */
175 err = drbd_cfg_context_from_attrs(NULL, info);
179 /* It was present, and valid,
180 * copy it over to the reply skb. */
181 err = nla_put_nohdr(adm_ctx.reply_skb,
182 info->attrs[DRBD_NLA_CFG_CONTEXT]->nla_len,
183 info->attrs[DRBD_NLA_CFG_CONTEXT]);
187 /* and assign stuff to the global adm_ctx */
188 nla = nested_attr_tb[__nla_type(T_ctx_volume)];
189 adm_ctx.volume = nla ? nla_get_u32(nla) : VOLUME_UNSPECIFIED;
190 nla = nested_attr_tb[__nla_type(T_ctx_conn_name)];
192 adm_ctx.conn_name = nla_data(nla);
194 adm_ctx.volume = VOLUME_UNSPECIFIED;
196 adm_ctx.minor = d_in->minor;
197 adm_ctx.mdev = minor_to_mdev(d_in->minor);
198 adm_ctx.tconn = conn_get_by_name(adm_ctx.conn_name);
200 if (!adm_ctx.mdev && (flags & DRBD_ADM_NEED_MINOR)) {
201 drbd_msg_put_info("unknown minor");
202 return ERR_MINOR_INVALID;
204 if (!adm_ctx.tconn && (flags & DRBD_ADM_NEED_CONN)) {
205 drbd_msg_put_info("unknown connection");
206 return ERR_INVALID_REQUEST;
209 /* some more paranoia, if the request was over-determined */
210 if (adm_ctx.mdev && adm_ctx.tconn &&
211 adm_ctx.mdev->tconn != adm_ctx.tconn) {
212 pr_warning("request: minor=%u, conn=%s; but that minor belongs to connection %s\n",
213 adm_ctx.minor, adm_ctx.conn_name, adm_ctx.mdev->tconn->name);
214 drbd_msg_put_info("minor exists in different connection");
215 return ERR_INVALID_REQUEST;
218 adm_ctx.volume != VOLUME_UNSPECIFIED &&
219 adm_ctx.volume != adm_ctx.mdev->vnr) {
220 pr_warning("request: minor=%u, volume=%u; but that minor is volume %u in %s\n",
221 adm_ctx.minor, adm_ctx.volume,
222 adm_ctx.mdev->vnr, adm_ctx.mdev->tconn->name);
223 drbd_msg_put_info("minor exists as different volume");
224 return ERR_INVALID_REQUEST;
230 nlmsg_free(adm_ctx.reply_skb);
231 adm_ctx.reply_skb = NULL;
235 static int drbd_adm_finish(struct genl_info *info, int retcode)
238 const char *conn_name = NULL;
241 kref_put(&adm_ctx.tconn->kref, &conn_destroy);
242 adm_ctx.tconn = NULL;
245 if (!adm_ctx.reply_skb)
248 adm_ctx.reply_dh->ret_code = retcode;
250 nla = info->attrs[DRBD_NLA_CFG_CONTEXT];
252 nla = nla_find_nested(nla, __nla_type(T_ctx_conn_name));
254 conn_name = nla_data(nla);
257 drbd_adm_send_reply(adm_ctx.reply_skb, info);
261 static void setup_khelper_env(struct drbd_tconn *tconn, char **envp)
267 nc = rcu_dereference(tconn->net_conf);
269 switch (((struct sockaddr *)nc->peer_addr)->sa_family) {
272 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI6",
273 &((struct sockaddr_in6 *)nc->peer_addr)->sin6_addr);
277 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
278 &((struct sockaddr_in *)nc->peer_addr)->sin_addr);
282 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
283 &((struct sockaddr_in *)nc->peer_addr)->sin_addr);
285 snprintf(envp[3], 20, "DRBD_PEER_AF=%s", afs);
290 int drbd_khelper(struct drbd_conf *mdev, char *cmd)
292 char *envp[] = { "HOME=/",
294 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
295 (char[20]) { }, /* address family */
296 (char[60]) { }, /* address */
299 char *argv[] = {usermode_helper, cmd, mb, NULL };
303 snprintf(mb, 12, "minor-%d", mdev_to_minor(mdev));
304 setup_khelper_env(mdev->tconn, envp);
306 /* The helper may take some time.
307 * write out any unsynced meta data changes now */
310 dev_info(DEV, "helper command: %s %s %s\n", usermode_helper, cmd, mb);
311 sib.sib_reason = SIB_HELPER_PRE;
312 sib.helper_name = cmd;
313 drbd_bcast_event(mdev, &sib);
314 ret = call_usermodehelper(usermode_helper, argv, envp, 1);
316 dev_warn(DEV, "helper command: %s %s %s exit code %u (0x%x)\n",
317 usermode_helper, cmd, mb,
318 (ret >> 8) & 0xff, ret);
320 dev_info(DEV, "helper command: %s %s %s exit code %u (0x%x)\n",
321 usermode_helper, cmd, mb,
322 (ret >> 8) & 0xff, ret);
323 sib.sib_reason = SIB_HELPER_POST;
324 sib.helper_exit_code = ret;
325 drbd_bcast_event(mdev, &sib);
327 if (ret < 0) /* Ignore any ERRNOs we got. */
333 static void conn_md_sync(struct drbd_tconn *tconn)
335 struct drbd_conf *mdev;
338 down_read(&drbd_cfg_rwsem);
339 idr_for_each_entry(&tconn->volumes, mdev, vnr)
341 up_read(&drbd_cfg_rwsem);
344 int conn_khelper(struct drbd_tconn *tconn, char *cmd)
346 char *envp[] = { "HOME=/",
348 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
349 (char[20]) { }, /* address family */
350 (char[60]) { }, /* address */
352 char *argv[] = {usermode_helper, cmd, tconn->name, NULL };
355 setup_khelper_env(tconn, envp);
358 conn_info(tconn, "helper command: %s %s %s\n", usermode_helper, cmd, tconn->name);
359 /* TODO: conn_bcast_event() ?? */
361 ret = call_usermodehelper(usermode_helper, argv, envp, 1);
363 conn_warn(tconn, "helper command: %s %s %s exit code %u (0x%x)\n",
364 usermode_helper, cmd, tconn->name,
365 (ret >> 8) & 0xff, ret);
367 conn_info(tconn, "helper command: %s %s %s exit code %u (0x%x)\n",
368 usermode_helper, cmd, tconn->name,
369 (ret >> 8) & 0xff, ret);
370 /* TODO: conn_bcast_event() ?? */
372 if (ret < 0) /* Ignore any ERRNOs we got. */
378 static enum drbd_fencing_p highest_fencing_policy(struct drbd_tconn *tconn)
380 enum drbd_fencing_p fp = FP_NOT_AVAIL;
381 struct drbd_conf *mdev;
385 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
386 if (get_ldev_if_state(mdev, D_CONSISTENT)) {
387 fp = max_t(enum drbd_fencing_p, fp, mdev->ldev->dc.fencing);
396 bool conn_try_outdate_peer(struct drbd_tconn *tconn)
398 union drbd_state mask = { };
399 union drbd_state val = { };
400 enum drbd_fencing_p fp;
404 if (tconn->cstate >= C_WF_REPORT_PARAMS) {
405 conn_err(tconn, "Expected cstate < C_WF_REPORT_PARAMS\n");
409 fp = highest_fencing_policy(tconn);
412 conn_warn(tconn, "Not fencing peer, I'm not even Consistent myself.\n");
419 r = conn_khelper(tconn, "fence-peer");
421 switch ((r>>8) & 0xff) {
422 case 3: /* peer is inconsistent */
423 ex_to_string = "peer is inconsistent or worse";
425 val.pdsk = D_INCONSISTENT;
427 case 4: /* peer got outdated, or was already outdated */
428 ex_to_string = "peer was fenced";
430 val.pdsk = D_OUTDATED;
432 case 5: /* peer was down */
433 if (conn_highest_disk(tconn) == D_UP_TO_DATE) {
434 /* we will(have) create(d) a new UUID anyways... */
435 ex_to_string = "peer is unreachable, assumed to be dead";
437 val.pdsk = D_OUTDATED;
439 ex_to_string = "peer unreachable, doing nothing since disk != UpToDate";
442 case 6: /* Peer is primary, voluntarily outdate myself.
443 * This is useful when an unconnected R_SECONDARY is asked to
444 * become R_PRIMARY, but finds the other peer being active. */
445 ex_to_string = "peer is active";
446 conn_warn(tconn, "Peer is primary, outdating myself.\n");
448 val.disk = D_OUTDATED;
451 if (fp != FP_STONITH)
452 conn_err(tconn, "fence-peer() = 7 && fencing != Stonith !!!\n");
453 ex_to_string = "peer was stonithed";
455 val.pdsk = D_OUTDATED;
458 /* The script is broken ... */
459 conn_err(tconn, "fence-peer helper broken, returned %d\n", (r>>8)&0xff);
460 return false; /* Eventually leave IO frozen */
463 conn_info(tconn, "fence-peer helper returned %d (%s)\n",
464 (r>>8) & 0xff, ex_to_string);
469 conn_request_state(tconn, mask, val, CS_VERBOSE);
470 here, because we might were able to re-establish the connection in the
472 spin_lock_irq(&tconn->req_lock);
473 if (tconn->cstate < C_WF_REPORT_PARAMS)
474 _conn_request_state(tconn, mask, val, CS_VERBOSE);
475 spin_unlock_irq(&tconn->req_lock);
477 return conn_highest_pdsk(tconn) <= D_OUTDATED;
480 static int _try_outdate_peer_async(void *data)
482 struct drbd_tconn *tconn = (struct drbd_tconn *)data;
484 conn_try_outdate_peer(tconn);
486 kref_put(&tconn->kref, &conn_destroy);
490 void conn_try_outdate_peer_async(struct drbd_tconn *tconn)
492 struct task_struct *opa;
494 kref_get(&tconn->kref);
495 opa = kthread_run(_try_outdate_peer_async, tconn, "drbd_async_h");
497 conn_err(tconn, "out of mem, failed to invoke fence-peer helper\n");
498 kref_put(&tconn->kref, &conn_destroy);
503 drbd_set_role(struct drbd_conf *mdev, enum drbd_role new_role, int force)
505 const int max_tries = 4;
506 enum drbd_state_rv rv = SS_UNKNOWN_ERROR;
510 union drbd_state mask, val;
512 if (new_role == R_PRIMARY)
513 request_ping(mdev->tconn); /* Detect a dead peer ASAP */
515 mutex_lock(mdev->state_mutex);
517 mask.i = 0; mask.role = R_MASK;
518 val.i = 0; val.role = new_role;
520 while (try++ < max_tries) {
521 rv = _drbd_request_state(mdev, mask, val, CS_WAIT_COMPLETE);
523 /* in case we first succeeded to outdate,
524 * but now suddenly could establish a connection */
525 if (rv == SS_CW_FAILED_BY_PEER && mask.pdsk != 0) {
531 if (rv == SS_NO_UP_TO_DATE_DISK && force &&
532 (mdev->state.disk < D_UP_TO_DATE &&
533 mdev->state.disk >= D_INCONSISTENT)) {
535 val.disk = D_UP_TO_DATE;
540 if (rv == SS_NO_UP_TO_DATE_DISK &&
541 mdev->state.disk == D_CONSISTENT && mask.pdsk == 0) {
542 D_ASSERT(mdev->state.pdsk == D_UNKNOWN);
544 if (conn_try_outdate_peer(mdev->tconn)) {
545 val.disk = D_UP_TO_DATE;
551 if (rv == SS_NOTHING_TO_DO)
553 if (rv == SS_PRIMARY_NOP && mask.pdsk == 0) {
554 if (!conn_try_outdate_peer(mdev->tconn) && force) {
555 dev_warn(DEV, "Forced into split brain situation!\n");
557 val.pdsk = D_OUTDATED;
562 if (rv == SS_TWO_PRIMARIES) {
563 /* Maybe the peer is detected as dead very soon...
564 retry at most once more in this case. */
567 nc = rcu_dereference(mdev->tconn->net_conf);
568 timeo = nc ? (nc->ping_timeo + 1) * HZ / 10 : 1;
570 schedule_timeout_interruptible(timeo);
575 if (rv < SS_SUCCESS) {
576 rv = _drbd_request_state(mdev, mask, val,
577 CS_VERBOSE + CS_WAIT_COMPLETE);
588 dev_warn(DEV, "Forced to consider local data as UpToDate!\n");
590 /* Wait until nothing is on the fly :) */
591 wait_event(mdev->misc_wait, atomic_read(&mdev->ap_pending_cnt) == 0);
593 if (new_role == R_SECONDARY) {
594 set_disk_ro(mdev->vdisk, true);
595 if (get_ldev(mdev)) {
596 mdev->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
600 mutex_lock(&mdev->tconn->conf_update);
601 nc = mdev->tconn->net_conf;
603 nc->want_lose = 0; /* without copy; single bit op is atomic */
604 mutex_unlock(&mdev->tconn->conf_update);
606 set_disk_ro(mdev->vdisk, false);
607 if (get_ldev(mdev)) {
608 if (((mdev->state.conn < C_CONNECTED ||
609 mdev->state.pdsk <= D_FAILED)
610 && mdev->ldev->md.uuid[UI_BITMAP] == 0) || forced)
611 drbd_uuid_new_current(mdev);
613 mdev->ldev->md.uuid[UI_CURRENT] |= (u64)1;
618 /* writeout of activity log covered areas of the bitmap
619 * to stable storage done in after state change already */
621 if (mdev->state.conn >= C_WF_REPORT_PARAMS) {
622 /* if this was forced, we should consider sync */
624 drbd_send_uuids(mdev);
625 drbd_send_state(mdev);
630 kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
632 mutex_unlock(mdev->state_mutex);
636 static const char *from_attrs_err_to_txt(int err)
638 return err == -ENOMSG ? "required attribute missing" :
639 err == -EOPNOTSUPP ? "unknown mandatory attribute" :
640 err == -EEXIST ? "can not change invariant setting" :
641 "invalid attribute value";
644 int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info)
646 struct set_role_parms parms;
648 enum drbd_ret_code retcode;
650 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
651 if (!adm_ctx.reply_skb)
653 if (retcode != NO_ERROR)
656 memset(&parms, 0, sizeof(parms));
657 if (info->attrs[DRBD_NLA_SET_ROLE_PARMS]) {
658 err = set_role_parms_from_attrs(&parms, info);
660 retcode = ERR_MANDATORY_TAG;
661 drbd_msg_put_info(from_attrs_err_to_txt(err));
666 if (info->genlhdr->cmd == DRBD_ADM_PRIMARY)
667 retcode = drbd_set_role(adm_ctx.mdev, R_PRIMARY, parms.assume_uptodate);
669 retcode = drbd_set_role(adm_ctx.mdev, R_SECONDARY, 0);
671 drbd_adm_finish(info, retcode);
675 /* initializes the md.*_offset members, so we are able to find
676 * the on disk meta data */
677 static void drbd_md_set_sector_offsets(struct drbd_conf *mdev,
678 struct drbd_backing_dev *bdev)
680 sector_t md_size_sect = 0;
681 switch (bdev->dc.meta_dev_idx) {
683 /* v07 style fixed size indexed meta data */
684 bdev->md.md_size_sect = MD_RESERVED_SECT;
685 bdev->md.md_offset = drbd_md_ss__(mdev, bdev);
686 bdev->md.al_offset = MD_AL_OFFSET;
687 bdev->md.bm_offset = MD_BM_OFFSET;
689 case DRBD_MD_INDEX_FLEX_EXT:
690 /* just occupy the full device; unit: sectors */
691 bdev->md.md_size_sect = drbd_get_capacity(bdev->md_bdev);
692 bdev->md.md_offset = 0;
693 bdev->md.al_offset = MD_AL_OFFSET;
694 bdev->md.bm_offset = MD_BM_OFFSET;
696 case DRBD_MD_INDEX_INTERNAL:
697 case DRBD_MD_INDEX_FLEX_INT:
698 bdev->md.md_offset = drbd_md_ss__(mdev, bdev);
699 /* al size is still fixed */
700 bdev->md.al_offset = -MD_AL_SECTORS;
701 /* we need (slightly less than) ~ this much bitmap sectors: */
702 md_size_sect = drbd_get_capacity(bdev->backing_bdev);
703 md_size_sect = ALIGN(md_size_sect, BM_SECT_PER_EXT);
704 md_size_sect = BM_SECT_TO_EXT(md_size_sect);
705 md_size_sect = ALIGN(md_size_sect, 8);
707 /* plus the "drbd meta data super block",
708 * and the activity log; */
709 md_size_sect += MD_BM_OFFSET;
711 bdev->md.md_size_sect = md_size_sect;
712 /* bitmap offset is adjusted by 'super' block size */
713 bdev->md.bm_offset = -md_size_sect + MD_AL_OFFSET;
718 /* input size is expected to be in KB */
719 char *ppsize(char *buf, unsigned long long size)
721 /* Needs 9 bytes at max including trailing NUL:
722 * -1ULL ==> "16384 EB" */
723 static char units[] = { 'K', 'M', 'G', 'T', 'P', 'E' };
725 while (size >= 10000 && base < sizeof(units)-1) {
727 size = (size >> 10) + !!(size & (1<<9));
730 sprintf(buf, "%u %cB", (unsigned)size, units[base]);
735 /* there is still a theoretical deadlock when called from receiver
736 * on an D_INCONSISTENT R_PRIMARY:
737 * remote READ does inc_ap_bio, receiver would need to receive answer
738 * packet from remote to dec_ap_bio again.
739 * receiver receive_sizes(), comes here,
740 * waits for ap_bio_cnt == 0. -> deadlock.
741 * but this cannot happen, actually, because:
742 * R_PRIMARY D_INCONSISTENT, and peer's disk is unreachable
743 * (not connected, or bad/no disk on peer):
744 * see drbd_fail_request_early, ap_bio_cnt is zero.
745 * R_PRIMARY D_INCONSISTENT, and C_SYNC_TARGET:
746 * peer may not initiate a resize.
748 /* Note these are not to be confused with
749 * drbd_adm_suspend_io/drbd_adm_resume_io,
750 * which are (sub) state changes triggered by admin (drbdsetup),
751 * and can be long lived.
752 * This changes an mdev->flag, is triggered by drbd internals,
753 * and should be short-lived. */
754 void drbd_suspend_io(struct drbd_conf *mdev)
756 set_bit(SUSPEND_IO, &mdev->flags);
757 if (drbd_suspended(mdev))
759 wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_bio_cnt));
762 void drbd_resume_io(struct drbd_conf *mdev)
764 clear_bit(SUSPEND_IO, &mdev->flags);
765 wake_up(&mdev->misc_wait);
769 * drbd_determine_dev_size() - Sets the right device size obeying all constraints
770 * @mdev: DRBD device.
772 * Returns 0 on success, negative return values indicate errors.
773 * You should call drbd_md_sync() after calling this function.
775 enum determine_dev_size drbd_determine_dev_size(struct drbd_conf *mdev, enum dds_flags flags) __must_hold(local)
777 sector_t prev_first_sect, prev_size; /* previous meta location */
778 sector_t la_size, u_size;
782 int md_moved, la_size_changed;
783 enum determine_dev_size rv = unchanged;
786 * application request passes inc_ap_bio,
787 * but then cannot get an AL-reference.
788 * this function later may wait on ap_bio_cnt == 0. -> deadlock.
791 * Suspend IO right here.
792 * still lock the act_log to not trigger ASSERTs there.
794 drbd_suspend_io(mdev);
796 /* no wait necessary anymore, actually we could assert that */
797 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
799 prev_first_sect = drbd_md_first_sector(mdev->ldev);
800 prev_size = mdev->ldev->md.md_size_sect;
801 la_size = mdev->ldev->md.la_size_sect;
803 /* TODO: should only be some assert here, not (re)init... */
804 drbd_md_set_sector_offsets(mdev, mdev->ldev);
806 u_size = mdev->ldev->dc.disk_size;
807 size = drbd_new_dev_size(mdev, mdev->ldev, u_size, flags & DDSF_FORCED);
809 if (drbd_get_capacity(mdev->this_bdev) != size ||
810 drbd_bm_capacity(mdev) != size) {
812 err = drbd_bm_resize(mdev, size, !(flags & DDSF_NO_RESYNC));
814 /* currently there is only one error: ENOMEM! */
815 size = drbd_bm_capacity(mdev)>>1;
817 dev_err(DEV, "OUT OF MEMORY! "
818 "Could not allocate bitmap!\n");
820 dev_err(DEV, "BM resizing failed. "
821 "Leaving size unchanged at size = %lu KB\n",
822 (unsigned long)size);
826 /* racy, see comments above. */
827 drbd_set_my_capacity(mdev, size);
828 mdev->ldev->md.la_size_sect = size;
829 dev_info(DEV, "size = %s (%llu KB)\n", ppsize(ppb, size>>1),
830 (unsigned long long)size>>1);
832 if (rv == dev_size_error)
835 la_size_changed = (la_size != mdev->ldev->md.la_size_sect);
837 md_moved = prev_first_sect != drbd_md_first_sector(mdev->ldev)
838 || prev_size != mdev->ldev->md.md_size_sect;
840 if (la_size_changed || md_moved) {
843 drbd_al_shrink(mdev); /* All extents inactive. */
844 dev_info(DEV, "Writing the whole bitmap, %s\n",
845 la_size_changed && md_moved ? "size changed and md moved" :
846 la_size_changed ? "size changed" : "md moved");
847 /* next line implicitly does drbd_suspend_io()+drbd_resume_io() */
848 err = drbd_bitmap_io(mdev, &drbd_bm_write,
849 "size changed", BM_LOCKED_MASK);
854 drbd_md_mark_dirty(mdev);
862 lc_unlock(mdev->act_log);
863 wake_up(&mdev->al_wait);
864 drbd_resume_io(mdev);
870 drbd_new_dev_size(struct drbd_conf *mdev, struct drbd_backing_dev *bdev,
871 sector_t u_size, int assume_peer_has_space)
873 sector_t p_size = mdev->p_size; /* partner's disk size. */
874 sector_t la_size = bdev->md.la_size_sect; /* last agreed size. */
875 sector_t m_size; /* my size */
878 m_size = drbd_get_max_capacity(bdev);
880 if (mdev->state.conn < C_CONNECTED && assume_peer_has_space) {
881 dev_warn(DEV, "Resize while not connected was forced by the user!\n");
885 if (p_size && m_size) {
886 size = min_t(sector_t, p_size, m_size);
890 if (m_size && m_size < size)
892 if (p_size && p_size < size)
903 dev_err(DEV, "Both nodes diskless!\n");
907 dev_err(DEV, "Requested disk size is too big (%lu > %lu)\n",
908 (unsigned long)u_size>>1, (unsigned long)size>>1);
917 * drbd_check_al_size() - Ensures that the AL is of the right size
918 * @mdev: DRBD device.
920 * Returns -EBUSY if current al lru is still used, -ENOMEM when allocation
921 * failed, and 0 on success. You should call drbd_md_sync() after you called
924 static int drbd_check_al_size(struct drbd_conf *mdev, struct disk_conf *dc)
926 struct lru_cache *n, *t;
927 struct lc_element *e;
931 if (!expect(dc->al_extents >= DRBD_AL_EXTENTS_MIN))
932 dc->al_extents = DRBD_AL_EXTENTS_MIN;
935 mdev->act_log->nr_elements == dc->al_extents)
940 n = lc_create("act_log", drbd_al_ext_cache, AL_UPDATES_PER_TRANSACTION,
941 dc->al_extents, sizeof(struct lc_element), 0);
944 dev_err(DEV, "Cannot allocate act_log lru!\n");
947 spin_lock_irq(&mdev->al_lock);
949 for (i = 0; i < t->nr_elements; i++) {
950 e = lc_element_by_index(t, i);
952 dev_err(DEV, "refcnt(%d)==%d\n",
953 e->lc_number, e->refcnt);
959 spin_unlock_irq(&mdev->al_lock);
961 dev_err(DEV, "Activity log still in use!\n");
968 drbd_md_mark_dirty(mdev); /* we changed mdev->act_log->nr_elemens */
972 static void drbd_setup_queue_param(struct drbd_conf *mdev, unsigned int max_bio_size)
974 struct request_queue * const q = mdev->rq_queue;
975 int max_hw_sectors = max_bio_size >> 9;
976 int max_segments = 0;
978 if (get_ldev_if_state(mdev, D_ATTACHING)) {
979 struct request_queue * const b = mdev->ldev->backing_bdev->bd_disk->queue;
981 max_hw_sectors = min(queue_max_hw_sectors(b), max_bio_size >> 9);
982 max_segments = mdev->ldev->dc.max_bio_bvecs;
986 blk_queue_logical_block_size(q, 512);
987 blk_queue_max_hw_sectors(q, max_hw_sectors);
988 /* This is the workaround for "bio would need to, but cannot, be split" */
989 blk_queue_max_segments(q, max_segments ? max_segments : BLK_MAX_SEGMENTS);
990 blk_queue_segment_boundary(q, PAGE_CACHE_SIZE-1);
992 if (get_ldev_if_state(mdev, D_ATTACHING)) {
993 struct request_queue * const b = mdev->ldev->backing_bdev->bd_disk->queue;
995 blk_queue_stack_limits(q, b);
997 if (q->backing_dev_info.ra_pages != b->backing_dev_info.ra_pages) {
998 dev_info(DEV, "Adjusting my ra_pages to backing device's (%lu -> %lu)\n",
999 q->backing_dev_info.ra_pages,
1000 b->backing_dev_info.ra_pages);
1001 q->backing_dev_info.ra_pages = b->backing_dev_info.ra_pages;
1007 void drbd_reconsider_max_bio_size(struct drbd_conf *mdev)
1009 int now, new, local, peer;
1011 now = queue_max_hw_sectors(mdev->rq_queue) << 9;
1012 local = mdev->local_max_bio_size; /* Eventually last known value, from volatile memory */
1013 peer = mdev->peer_max_bio_size; /* Eventually last known value, from meta data */
1015 if (get_ldev_if_state(mdev, D_ATTACHING)) {
1016 local = queue_max_hw_sectors(mdev->ldev->backing_bdev->bd_disk->queue) << 9;
1017 mdev->local_max_bio_size = local;
1021 /* We may ignore peer limits if the peer is modern enough.
1022 Because new from 8.3.8 onwards the peer can use multiple
1023 BIOs for a single peer_request */
1024 if (mdev->state.conn >= C_CONNECTED) {
1025 if (mdev->tconn->agreed_pro_version < 94)
1026 peer = mdev->peer_max_bio_size;
1027 else if (mdev->tconn->agreed_pro_version == 94)
1028 peer = DRBD_MAX_SIZE_H80_PACKET;
1029 else /* drbd 8.3.8 onwards */
1030 peer = DRBD_MAX_BIO_SIZE;
1033 new = min_t(int, local, peer);
1035 if (mdev->state.role == R_PRIMARY && new < now)
1036 dev_err(DEV, "ASSERT FAILED new < now; (%d < %d)\n", new, now);
1039 dev_info(DEV, "max BIO size = %u\n", new);
1041 drbd_setup_queue_param(mdev, new);
1044 /* Starts the worker thread */
1045 static void conn_reconfig_start(struct drbd_tconn *tconn)
1047 drbd_thread_start(&tconn->worker);
1048 conn_flush_workqueue(tconn);
1051 /* if still unconfigured, stops worker again. */
1052 static void conn_reconfig_done(struct drbd_tconn *tconn)
1055 spin_lock_irq(&tconn->req_lock);
1056 stop_threads = conn_all_vols_unconf(tconn);
1057 spin_unlock_irq(&tconn->req_lock);
1059 /* asender is implicitly stopped by receiver
1060 * in drbd_disconnect() */
1061 drbd_thread_stop(&tconn->receiver);
1062 drbd_thread_stop(&tconn->worker);
1066 /* Make sure IO is suspended before calling this function(). */
1067 static void drbd_suspend_al(struct drbd_conf *mdev)
1071 if (!lc_try_lock(mdev->act_log)) {
1072 dev_warn(DEV, "Failed to lock al in drbd_suspend_al()\n");
1076 drbd_al_shrink(mdev);
1077 spin_lock_irq(&mdev->tconn->req_lock);
1078 if (mdev->state.conn < C_CONNECTED)
1079 s = !test_and_set_bit(AL_SUSPENDED, &mdev->flags);
1080 spin_unlock_irq(&mdev->tconn->req_lock);
1081 lc_unlock(mdev->act_log);
1084 dev_info(DEV, "Suspended AL updates\n");
1088 static bool should_set_defaults(struct genl_info *info)
1090 unsigned flags = ((struct drbd_genlmsghdr*)info->userhdr)->flags;
1091 return 0 != (flags & DRBD_GENL_F_SET_DEFAULTS);
1094 int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info)
1096 enum drbd_ret_code retcode;
1097 struct drbd_conf *mdev;
1098 struct disk_conf *new_disk_conf;
1100 int *rs_plan_s = NULL;
1102 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1103 if (!adm_ctx.reply_skb)
1105 if (retcode != NO_ERROR)
1108 mdev = adm_ctx.mdev;
1110 /* we also need a disk
1111 * to change the options on */
1112 if (!get_ldev(mdev)) {
1113 retcode = ERR_NO_DISK;
1117 /* FIXME freeze IO, cluster wide.
1119 * We should make sure no-one uses
1120 * some half-updated struct when we
1121 * assign it later. */
1123 new_disk_conf = kmalloc(sizeof(*new_disk_conf), GFP_KERNEL);
1124 if (!new_disk_conf) {
1125 retcode = ERR_NOMEM;
1129 memcpy(new_disk_conf, &mdev->ldev->dc, sizeof(*new_disk_conf));
1130 if (should_set_defaults(info))
1131 set_disk_conf_defaults(new_disk_conf);
1133 err = disk_conf_from_attrs_for_change(new_disk_conf, info);
1135 retcode = ERR_MANDATORY_TAG;
1136 drbd_msg_put_info(from_attrs_err_to_txt(err));
1139 if (!expect(new_disk_conf->resync_rate >= 1))
1140 new_disk_conf->resync_rate = 1;
1142 /* clip to allowed range */
1143 if (!expect(new_disk_conf->al_extents >= DRBD_AL_EXTENTS_MIN))
1144 new_disk_conf->al_extents = DRBD_AL_EXTENTS_MIN;
1145 if (!expect(new_disk_conf->al_extents <= DRBD_AL_EXTENTS_MAX))
1146 new_disk_conf->al_extents = DRBD_AL_EXTENTS_MAX;
1148 fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
1149 if (fifo_size != mdev->rs_plan_s.size && fifo_size > 0) {
1150 rs_plan_s = kzalloc(sizeof(int) * fifo_size, GFP_KERNEL);
1152 dev_err(DEV, "kmalloc of fifo_buffer failed");
1153 retcode = ERR_NOMEM;
1158 if (fifo_size != mdev->rs_plan_s.size) {
1159 kfree(mdev->rs_plan_s.values);
1160 mdev->rs_plan_s.values = rs_plan_s;
1161 mdev->rs_plan_s.size = fifo_size;
1162 mdev->rs_planed = 0;
1166 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
1167 drbd_al_shrink(mdev);
1168 err = drbd_check_al_size(mdev, new_disk_conf);
1169 lc_unlock(mdev->act_log);
1170 wake_up(&mdev->al_wait);
1173 retcode = ERR_NOMEM;
1178 * To avoid someone looking at a half-updated struct, we probably
1179 * should have a rw-semaphor on net_conf and disk_conf.
1181 write_lock_irq(&global_state_lock);
1182 retcode = drbd_sync_after_valid(mdev, new_disk_conf->resync_after);
1183 if (retcode == NO_ERROR) {
1184 mdev->ldev->dc = *new_disk_conf;
1185 drbd_sync_after_changed(mdev);
1187 write_unlock_irq(&global_state_lock);
1192 if (mdev->state.conn >= C_CONNECTED)
1193 drbd_send_sync_param(mdev);
1197 kfree(new_disk_conf);
1200 drbd_adm_finish(info, retcode);
1204 int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info)
1206 struct drbd_conf *mdev;
1208 enum drbd_ret_code retcode;
1209 enum determine_dev_size dd;
1210 sector_t max_possible_sectors;
1211 sector_t min_md_device_sectors;
1212 struct drbd_backing_dev *nbc = NULL; /* new_backing_conf */
1213 struct block_device *bdev;
1214 struct lru_cache *resync_lru = NULL;
1215 union drbd_state ns, os;
1216 enum drbd_state_rv rv;
1217 struct net_conf *nc;
1218 int cp_discovered = 0;
1220 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1221 if (!adm_ctx.reply_skb)
1223 if (retcode != NO_ERROR)
1226 mdev = adm_ctx.mdev;
1227 conn_reconfig_start(mdev->tconn);
1229 /* if you want to reconfigure, please tear down first */
1230 if (mdev->state.disk > D_DISKLESS) {
1231 retcode = ERR_DISK_CONFIGURED;
1234 /* It may just now have detached because of IO error. Make sure
1235 * drbd_ldev_destroy is done already, we may end up here very fast,
1236 * e.g. if someone calls attach from the on-io-error handler,
1237 * to realize a "hot spare" feature (not that I'd recommend that) */
1238 wait_event(mdev->misc_wait, !atomic_read(&mdev->local_cnt));
1240 /* allocation not in the IO path, drbdsetup context */
1241 nbc = kzalloc(sizeof(struct drbd_backing_dev), GFP_KERNEL);
1243 retcode = ERR_NOMEM;
1247 set_disk_conf_defaults(&nbc->dc);
1249 err = disk_conf_from_attrs(&nbc->dc, info);
1251 retcode = ERR_MANDATORY_TAG;
1252 drbd_msg_put_info(from_attrs_err_to_txt(err));
1256 if ((int)nbc->dc.meta_dev_idx < DRBD_MD_INDEX_FLEX_INT) {
1257 retcode = ERR_MD_IDX_INVALID;
1262 nc = rcu_dereference(mdev->tconn->net_conf);
1264 if (nbc->dc.fencing == FP_STONITH && nc->wire_protocol == DRBD_PROT_A) {
1266 retcode = ERR_STONITH_AND_PROT_A;
1272 bdev = blkdev_get_by_path(nbc->dc.backing_dev,
1273 FMODE_READ | FMODE_WRITE | FMODE_EXCL, mdev);
1275 dev_err(DEV, "open(\"%s\") failed with %ld\n", nbc->dc.backing_dev,
1277 retcode = ERR_OPEN_DISK;
1280 nbc->backing_bdev = bdev;
1283 * meta_dev_idx >= 0: external fixed size, possibly multiple
1284 * drbd sharing one meta device. TODO in that case, paranoia
1285 * check that [md_bdev, meta_dev_idx] is not yet used by some
1286 * other drbd minor! (if you use drbd.conf + drbdadm, that
1287 * should check it for you already; but if you don't, or
1288 * someone fooled it, we need to double check here)
1290 bdev = blkdev_get_by_path(nbc->dc.meta_dev,
1291 FMODE_READ | FMODE_WRITE | FMODE_EXCL,
1292 ((int)nbc->dc.meta_dev_idx < 0) ?
1293 (void *)mdev : (void *)drbd_m_holder);
1295 dev_err(DEV, "open(\"%s\") failed with %ld\n", nbc->dc.meta_dev,
1297 retcode = ERR_OPEN_MD_DISK;
1300 nbc->md_bdev = bdev;
1302 if ((nbc->backing_bdev == nbc->md_bdev) !=
1303 (nbc->dc.meta_dev_idx == DRBD_MD_INDEX_INTERNAL ||
1304 nbc->dc.meta_dev_idx == DRBD_MD_INDEX_FLEX_INT)) {
1305 retcode = ERR_MD_IDX_INVALID;
1309 resync_lru = lc_create("resync", drbd_bm_ext_cache,
1310 1, 61, sizeof(struct bm_extent),
1311 offsetof(struct bm_extent, lce));
1313 retcode = ERR_NOMEM;
1317 /* RT - for drbd_get_max_capacity() DRBD_MD_INDEX_FLEX_INT */
1318 drbd_md_set_sector_offsets(mdev, nbc);
1320 if (drbd_get_max_capacity(nbc) < nbc->dc.disk_size) {
1321 dev_err(DEV, "max capacity %llu smaller than disk size %llu\n",
1322 (unsigned long long) drbd_get_max_capacity(nbc),
1323 (unsigned long long) nbc->dc.disk_size);
1324 retcode = ERR_DISK_TO_SMALL;
1328 if ((int)nbc->dc.meta_dev_idx < 0) {
1329 max_possible_sectors = DRBD_MAX_SECTORS_FLEX;
1330 /* at least one MB, otherwise it does not make sense */
1331 min_md_device_sectors = (2<<10);
1333 max_possible_sectors = DRBD_MAX_SECTORS;
1334 min_md_device_sectors = MD_RESERVED_SECT * (nbc->dc.meta_dev_idx + 1);
1337 if (drbd_get_capacity(nbc->md_bdev) < min_md_device_sectors) {
1338 retcode = ERR_MD_DISK_TO_SMALL;
1339 dev_warn(DEV, "refusing attach: md-device too small, "
1340 "at least %llu sectors needed for this meta-disk type\n",
1341 (unsigned long long) min_md_device_sectors);
1345 /* Make sure the new disk is big enough
1346 * (we may currently be R_PRIMARY with no local disk...) */
1347 if (drbd_get_max_capacity(nbc) <
1348 drbd_get_capacity(mdev->this_bdev)) {
1349 retcode = ERR_DISK_TO_SMALL;
1353 nbc->known_size = drbd_get_capacity(nbc->backing_bdev);
1355 if (nbc->known_size > max_possible_sectors) {
1356 dev_warn(DEV, "==> truncating very big lower level device "
1357 "to currently maximum possible %llu sectors <==\n",
1358 (unsigned long long) max_possible_sectors);
1359 if ((int)nbc->dc.meta_dev_idx >= 0)
1360 dev_warn(DEV, "==>> using internal or flexible "
1361 "meta data may help <<==\n");
1364 drbd_suspend_io(mdev);
1365 /* also wait for the last barrier ack. */
1366 wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_pending_cnt) || drbd_suspended(mdev));
1367 /* and for any other previously queued work */
1368 drbd_flush_workqueue(mdev);
1370 rv = _drbd_request_state(mdev, NS(disk, D_ATTACHING), CS_VERBOSE);
1371 retcode = rv; /* FIXME: Type mismatch. */
1372 drbd_resume_io(mdev);
1373 if (rv < SS_SUCCESS)
1376 if (!get_ldev_if_state(mdev, D_ATTACHING))
1377 goto force_diskless;
1379 drbd_md_set_sector_offsets(mdev, nbc);
1381 if (!mdev->bitmap) {
1382 if (drbd_bm_init(mdev)) {
1383 retcode = ERR_NOMEM;
1384 goto force_diskless_dec;
1388 retcode = drbd_md_read(mdev, nbc);
1389 if (retcode != NO_ERROR)
1390 goto force_diskless_dec;
1392 if (mdev->state.conn < C_CONNECTED &&
1393 mdev->state.role == R_PRIMARY &&
1394 (mdev->ed_uuid & ~((u64)1)) != (nbc->md.uuid[UI_CURRENT] & ~((u64)1))) {
1395 dev_err(DEV, "Can only attach to data with current UUID=%016llX\n",
1396 (unsigned long long)mdev->ed_uuid);
1397 retcode = ERR_DATA_NOT_CURRENT;
1398 goto force_diskless_dec;
1401 /* Since we are diskless, fix the activity log first... */
1402 if (drbd_check_al_size(mdev, &nbc->dc)) {
1403 retcode = ERR_NOMEM;
1404 goto force_diskless_dec;
1407 /* Prevent shrinking of consistent devices ! */
1408 if (drbd_md_test_flag(nbc, MDF_CONSISTENT) &&
1409 drbd_new_dev_size(mdev, nbc, nbc->dc.disk_size, 0) < nbc->md.la_size_sect) {
1410 dev_warn(DEV, "refusing to truncate a consistent device\n");
1411 retcode = ERR_DISK_TO_SMALL;
1412 goto force_diskless_dec;
1415 if (!drbd_al_read_log(mdev, nbc)) {
1416 retcode = ERR_IO_MD_DISK;
1417 goto force_diskless_dec;
1420 /* Reset the "barriers don't work" bits here, then force meta data to
1421 * be written, to ensure we determine if barriers are supported. */
1422 if (nbc->dc.no_md_flush)
1423 set_bit(MD_NO_FUA, &mdev->flags);
1425 clear_bit(MD_NO_FUA, &mdev->flags);
1427 /* Point of no return reached.
1428 * Devices and memory are no longer released by error cleanup below.
1429 * now mdev takes over responsibility, and the state engine should
1430 * clean it up somewhere. */
1431 D_ASSERT(mdev->ldev == NULL);
1433 mdev->resync = resync_lru;
1437 mdev->write_ordering = WO_bdev_flush;
1438 drbd_bump_write_ordering(mdev, WO_bdev_flush);
1440 if (drbd_md_test_flag(mdev->ldev, MDF_CRASHED_PRIMARY))
1441 set_bit(CRASHED_PRIMARY, &mdev->flags);
1443 clear_bit(CRASHED_PRIMARY, &mdev->flags);
1445 if (drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) &&
1446 !(mdev->state.role == R_PRIMARY && mdev->tconn->susp_nod)) {
1447 set_bit(CRASHED_PRIMARY, &mdev->flags);
1456 drbd_reconsider_max_bio_size(mdev);
1458 /* If I am currently not R_PRIMARY,
1459 * but meta data primary indicator is set,
1460 * I just now recover from a hard crash,
1461 * and have been R_PRIMARY before that crash.
1463 * Now, if I had no connection before that crash
1464 * (have been degraded R_PRIMARY), chances are that
1465 * I won't find my peer now either.
1467 * In that case, and _only_ in that case,
1468 * we use the degr-wfc-timeout instead of the default,
1469 * so we can automatically recover from a crash of a
1470 * degraded but active "cluster" after a certain timeout.
1472 clear_bit(USE_DEGR_WFC_T, &mdev->flags);
1473 if (mdev->state.role != R_PRIMARY &&
1474 drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) &&
1475 !drbd_md_test_flag(mdev->ldev, MDF_CONNECTED_IND))
1476 set_bit(USE_DEGR_WFC_T, &mdev->flags);
1478 dd = drbd_determine_dev_size(mdev, 0);
1479 if (dd == dev_size_error) {
1480 retcode = ERR_NOMEM_BITMAP;
1481 goto force_diskless_dec;
1482 } else if (dd == grew)
1483 set_bit(RESYNC_AFTER_NEG, &mdev->flags);
1485 if (drbd_md_test_flag(mdev->ldev, MDF_FULL_SYNC)) {
1486 dev_info(DEV, "Assuming that all blocks are out of sync "
1487 "(aka FullSync)\n");
1488 if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write,
1489 "set_n_write from attaching", BM_LOCKED_MASK)) {
1490 retcode = ERR_IO_MD_DISK;
1491 goto force_diskless_dec;
1494 if (drbd_bitmap_io(mdev, &drbd_bm_read,
1495 "read from attaching", BM_LOCKED_MASK)) {
1496 retcode = ERR_IO_MD_DISK;
1497 goto force_diskless_dec;
1501 if (cp_discovered) {
1502 drbd_al_apply_to_bm(mdev);
1503 if (drbd_bitmap_io(mdev, &drbd_bm_write,
1504 "crashed primary apply AL", BM_LOCKED_MASK)) {
1505 retcode = ERR_IO_MD_DISK;
1506 goto force_diskless_dec;
1510 if (_drbd_bm_total_weight(mdev) == drbd_bm_bits(mdev))
1511 drbd_suspend_al(mdev); /* IO is still suspended here... */
1513 spin_lock_irq(&mdev->tconn->req_lock);
1514 os = drbd_read_state(mdev);
1516 /* If MDF_CONSISTENT is not set go into inconsistent state,
1517 otherwise investigate MDF_WasUpToDate...
1518 If MDF_WAS_UP_TO_DATE is not set go into D_OUTDATED disk state,
1519 otherwise into D_CONSISTENT state.
1521 if (drbd_md_test_flag(mdev->ldev, MDF_CONSISTENT)) {
1522 if (drbd_md_test_flag(mdev->ldev, MDF_WAS_UP_TO_DATE))
1523 ns.disk = D_CONSISTENT;
1525 ns.disk = D_OUTDATED;
1527 ns.disk = D_INCONSISTENT;
1530 if (drbd_md_test_flag(mdev->ldev, MDF_PEER_OUT_DATED))
1531 ns.pdsk = D_OUTDATED;
1533 if ( ns.disk == D_CONSISTENT &&
1534 (ns.pdsk == D_OUTDATED || mdev->ldev->dc.fencing == FP_DONT_CARE))
1535 ns.disk = D_UP_TO_DATE;
1537 /* All tests on MDF_PRIMARY_IND, MDF_CONNECTED_IND,
1538 MDF_CONSISTENT and MDF_WAS_UP_TO_DATE must happen before
1539 this point, because drbd_request_state() modifies these
1542 /* In case we are C_CONNECTED postpone any decision on the new disk
1543 state after the negotiation phase. */
1544 if (mdev->state.conn == C_CONNECTED) {
1545 mdev->new_state_tmp.i = ns.i;
1547 ns.disk = D_NEGOTIATING;
1549 /* We expect to receive up-to-date UUIDs soon.
1550 To avoid a race in receive_state, free p_uuid while
1551 holding req_lock. I.e. atomic with the state change */
1552 kfree(mdev->p_uuid);
1553 mdev->p_uuid = NULL;
1556 rv = _drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
1557 spin_unlock_irq(&mdev->tconn->req_lock);
1559 if (rv < SS_SUCCESS)
1560 goto force_diskless_dec;
1562 if (mdev->state.role == R_PRIMARY)
1563 mdev->ldev->md.uuid[UI_CURRENT] |= (u64)1;
1565 mdev->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
1567 drbd_md_mark_dirty(mdev);
1570 kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
1572 conn_reconfig_done(mdev->tconn);
1573 drbd_adm_finish(info, retcode);
1579 drbd_force_state(mdev, NS(disk, D_FAILED));
1582 conn_reconfig_done(mdev->tconn);
1584 if (nbc->backing_bdev)
1585 blkdev_put(nbc->backing_bdev,
1586 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1588 blkdev_put(nbc->md_bdev,
1589 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1592 lc_destroy(resync_lru);
1595 drbd_adm_finish(info, retcode);
1599 static int adm_detach(struct drbd_conf *mdev)
1601 enum drbd_state_rv retcode;
1603 drbd_suspend_io(mdev); /* so no-one is stuck in drbd_al_begin_io */
1604 retcode = drbd_request_state(mdev, NS(disk, D_FAILED));
1605 /* D_FAILED will transition to DISKLESS. */
1606 ret = wait_event_interruptible(mdev->misc_wait,
1607 mdev->state.disk != D_FAILED);
1608 drbd_resume_io(mdev);
1609 if ((int)retcode == (int)SS_IS_DISKLESS)
1610 retcode = SS_NOTHING_TO_DO;
1616 /* Detaching the disk is a process in multiple stages. First we need to lock
1617 * out application IO, in-flight IO, IO stuck in drbd_al_begin_io.
1618 * Then we transition to D_DISKLESS, and wait for put_ldev() to return all
1619 * internal references as well.
1620 * Only then we have finally detached. */
1621 int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info)
1623 enum drbd_ret_code retcode;
1625 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1626 if (!adm_ctx.reply_skb)
1628 if (retcode != NO_ERROR)
1631 retcode = adm_detach(adm_ctx.mdev);
1633 drbd_adm_finish(info, retcode);
1637 static bool conn_resync_running(struct drbd_tconn *tconn)
1639 struct drbd_conf *mdev;
1644 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1645 if (mdev->state.conn == C_SYNC_SOURCE ||
1646 mdev->state.conn == C_SYNC_TARGET ||
1647 mdev->state.conn == C_PAUSED_SYNC_S ||
1648 mdev->state.conn == C_PAUSED_SYNC_T) {
1658 static bool conn_ov_running(struct drbd_tconn *tconn)
1660 struct drbd_conf *mdev;
1665 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1666 if (mdev->state.conn == C_VERIFY_S ||
1667 mdev->state.conn == C_VERIFY_T) {
1677 static enum drbd_ret_code
1678 _check_net_options(struct drbd_tconn *tconn, struct net_conf *old_conf, struct net_conf *new_conf)
1680 struct drbd_conf *mdev;
1683 if (old_conf && tconn->agreed_pro_version < 100 &&
1684 tconn->cstate == C_WF_REPORT_PARAMS &&
1685 new_conf->wire_protocol != old_conf->wire_protocol)
1686 return ERR_NEED_APV_100;
1688 if (new_conf->two_primaries &&
1689 (new_conf->wire_protocol != DRBD_PROT_C))
1690 return ERR_NOT_PROTO_C;
1692 idr_for_each_entry(&tconn->volumes, mdev, i) {
1693 if (get_ldev(mdev)) {
1694 enum drbd_fencing_p fp = mdev->ldev->dc.fencing;
1696 if (new_conf->wire_protocol == DRBD_PROT_A && fp == FP_STONITH)
1697 return ERR_STONITH_AND_PROT_A;
1699 if (mdev->state.role == R_PRIMARY && new_conf->want_lose)
1703 if (new_conf->on_congestion != OC_BLOCK && new_conf->wire_protocol != DRBD_PROT_A)
1704 return ERR_CONG_NOT_PROTO_A;
1709 static enum drbd_ret_code
1710 check_net_options(struct drbd_tconn *tconn, struct net_conf *new_conf)
1712 static enum drbd_ret_code rv;
1713 struct drbd_conf *mdev;
1717 rv = _check_net_options(tconn, rcu_dereference(tconn->net_conf), new_conf);
1720 /* tconn->volumes protected by genl_lock() here */
1721 idr_for_each_entry(&tconn->volumes, mdev, i) {
1722 if (!mdev->bitmap) {
1723 if(drbd_bm_init(mdev))
1732 struct crypto_hash *verify_tfm;
1733 struct crypto_hash *csums_tfm;
1734 struct crypto_hash *cram_hmac_tfm;
1735 struct crypto_hash *integrity_tfm;
1741 alloc_hash(struct crypto_hash **tfm, char *tfm_name, int err_alg)
1746 *tfm = crypto_alloc_hash(tfm_name, 0, CRYPTO_ALG_ASYNC);
1755 static enum drbd_ret_code
1756 alloc_crypto(struct crypto *crypto, struct net_conf *new_conf)
1758 char hmac_name[CRYPTO_MAX_ALG_NAME];
1759 enum drbd_ret_code rv;
1762 rv = alloc_hash(&crypto->csums_tfm, new_conf->csums_alg,
1766 rv = alloc_hash(&crypto->verify_tfm, new_conf->verify_alg,
1770 rv = alloc_hash(&crypto->integrity_tfm, new_conf->integrity_alg,
1774 if (new_conf->cram_hmac_alg[0] != 0) {
1775 snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)",
1776 new_conf->cram_hmac_alg);
1778 rv = alloc_hash(&crypto->cram_hmac_tfm, hmac_name,
1781 if (crypto->integrity_tfm) {
1782 hash_size = crypto_hash_digestsize(crypto->integrity_tfm);
1783 crypto->int_dig_in = kmalloc(hash_size, GFP_KERNEL);
1784 if (!crypto->int_dig_in)
1786 crypto->int_dig_vv = kmalloc(hash_size, GFP_KERNEL);
1787 if (!crypto->int_dig_vv)
1794 static void free_crypto(struct crypto *crypto)
1796 kfree(crypto->int_dig_in);
1797 kfree(crypto->int_dig_vv);
1798 crypto_free_hash(crypto->cram_hmac_tfm);
1799 crypto_free_hash(crypto->integrity_tfm);
1800 crypto_free_hash(crypto->csums_tfm);
1801 crypto_free_hash(crypto->verify_tfm);
1804 int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info)
1806 enum drbd_ret_code retcode;
1807 struct drbd_tconn *tconn;
1808 struct net_conf *old_conf, *new_conf = NULL;
1810 int ovr; /* online verify running */
1811 int rsr; /* re-sync running */
1812 struct crypto crypto = { };
1813 bool change_integrity_alg;
1815 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONN);
1816 if (!adm_ctx.reply_skb)
1818 if (retcode != NO_ERROR)
1821 tconn = adm_ctx.tconn;
1823 new_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
1825 retcode = ERR_NOMEM;
1829 conn_reconfig_start(tconn);
1831 mutex_lock(&tconn->data.mutex);
1832 mutex_lock(&tconn->conf_update);
1833 old_conf = tconn->net_conf;
1836 drbd_msg_put_info("net conf missing, try connect");
1837 retcode = ERR_INVALID_REQUEST;
1841 *new_conf = *old_conf;
1842 if (should_set_defaults(info))
1843 set_net_conf_defaults(new_conf);
1845 err = net_conf_from_attrs_for_change(new_conf, info);
1847 retcode = ERR_MANDATORY_TAG;
1848 drbd_msg_put_info(from_attrs_err_to_txt(err));
1852 retcode = check_net_options(tconn, new_conf);
1853 if (retcode != NO_ERROR)
1856 /* re-sync running */
1857 rsr = conn_resync_running(tconn);
1858 if (rsr && strcmp(new_conf->csums_alg, old_conf->csums_alg)) {
1859 retcode = ERR_CSUMS_RESYNC_RUNNING;
1863 /* online verify running */
1864 ovr = conn_ov_running(tconn);
1865 if (ovr && strcmp(new_conf->verify_alg, old_conf->verify_alg)) {
1866 retcode = ERR_VERIFY_RUNNING;
1870 change_integrity_alg = strcmp(old_conf->integrity_alg,
1871 new_conf->integrity_alg);
1873 retcode = alloc_crypto(&crypto, new_conf);
1874 if (retcode != NO_ERROR)
1877 rcu_assign_pointer(tconn->net_conf, new_conf);
1880 crypto_free_hash(tconn->csums_tfm);
1881 tconn->csums_tfm = crypto.csums_tfm;
1882 crypto.csums_tfm = NULL;
1885 crypto_free_hash(tconn->verify_tfm);
1886 tconn->verify_tfm = crypto.verify_tfm;
1887 crypto.verify_tfm = NULL;
1890 kfree(tconn->int_dig_in);
1891 tconn->int_dig_in = crypto.int_dig_in;
1892 kfree(tconn->int_dig_vv);
1893 tconn->int_dig_vv = crypto.int_dig_vv;
1894 crypto_free_hash(tconn->integrity_tfm);
1895 tconn->integrity_tfm = crypto.integrity_tfm;
1896 if (change_integrity_alg) {
1897 /* Do this without trying to take tconn->data.mutex again. */
1898 if (__drbd_send_protocol(tconn))
1902 /* FIXME Changing cram_hmac while the connection is established is useless */
1903 crypto_free_hash(tconn->cram_hmac_tfm);
1904 tconn->cram_hmac_tfm = crypto.cram_hmac_tfm;
1906 mutex_unlock(&tconn->conf_update);
1907 mutex_unlock(&tconn->data.mutex);
1911 if (tconn->cstate >= C_WF_REPORT_PARAMS)
1912 drbd_send_sync_param(minor_to_mdev(conn_lowest_minor(tconn)));
1917 mutex_unlock(&tconn->conf_update);
1918 mutex_unlock(&tconn->data.mutex);
1919 free_crypto(&crypto);
1922 conn_reconfig_done(tconn);
1924 drbd_adm_finish(info, retcode);
1928 int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info)
1930 struct drbd_conf *mdev;
1931 struct net_conf *old_conf, *new_conf = NULL;
1932 struct crypto crypto = { };
1933 struct drbd_tconn *oconn;
1934 struct drbd_tconn *tconn;
1935 struct sockaddr *new_my_addr, *new_peer_addr, *taken_addr;
1936 enum drbd_ret_code retcode;
1940 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONN);
1941 if (!adm_ctx.reply_skb)
1943 if (retcode != NO_ERROR)
1946 tconn = adm_ctx.tconn;
1947 conn_reconfig_start(tconn);
1949 if (tconn->cstate > C_STANDALONE) {
1950 retcode = ERR_NET_CONFIGURED;
1954 /* allocation not in the IO path, cqueue thread context */
1955 new_conf = kzalloc(sizeof(*new_conf), GFP_KERNEL);
1957 retcode = ERR_NOMEM;
1961 set_net_conf_defaults(new_conf);
1963 err = net_conf_from_attrs(new_conf, info);
1965 retcode = ERR_MANDATORY_TAG;
1966 drbd_msg_put_info(from_attrs_err_to_txt(err));
1970 retcode = check_net_options(tconn, new_conf);
1971 if (retcode != NO_ERROR)
1976 new_my_addr = (struct sockaddr *)&new_conf->my_addr;
1977 new_peer_addr = (struct sockaddr *)&new_conf->peer_addr;
1979 /* No need to take drbd_cfg_rwsem here. All reconfiguration is
1980 * strictly serialized on genl_lock(). We are protected against
1981 * concurrent reconfiguration/addition/deletion */
1982 list_for_each_entry(oconn, &drbd_tconns, all_tconn) {
1983 struct net_conf *nc;
1988 nc = rcu_dereference(oconn->net_conf);
1990 taken_addr = (struct sockaddr *)&nc->my_addr;
1991 if (new_conf->my_addr_len == nc->my_addr_len &&
1992 !memcmp(new_my_addr, taken_addr, new_conf->my_addr_len))
1993 retcode = ERR_LOCAL_ADDR;
1995 taken_addr = (struct sockaddr *)&nc->peer_addr;
1996 if (new_conf->peer_addr_len == nc->peer_addr_len &&
1997 !memcmp(new_peer_addr, taken_addr, new_conf->peer_addr_len))
1998 retcode = ERR_PEER_ADDR;
2001 if (retcode != NO_ERROR)
2005 retcode = alloc_crypto(&crypto, new_conf);
2006 if (retcode != NO_ERROR)
2009 ((char *)new_conf->shared_secret)[SHARED_SECRET_MAX-1] = 0;
2011 conn_flush_workqueue(tconn);
2013 mutex_lock(&tconn->conf_update);
2014 old_conf = tconn->net_conf;
2016 retcode = ERR_NET_CONFIGURED;
2017 mutex_unlock(&tconn->conf_update);
2020 rcu_assign_pointer(tconn->net_conf, new_conf);
2022 conn_free_crypto(tconn);
2023 tconn->int_dig_in = crypto.int_dig_in;
2024 tconn->int_dig_vv = crypto.int_dig_vv;
2025 tconn->cram_hmac_tfm = crypto.cram_hmac_tfm;
2026 tconn->integrity_tfm = crypto.integrity_tfm;
2027 tconn->csums_tfm = crypto.csums_tfm;
2028 tconn->verify_tfm = crypto.verify_tfm;
2030 mutex_unlock(&tconn->conf_update);
2033 idr_for_each_entry(&tconn->volumes, mdev, i) {
2039 retcode = conn_request_state(tconn, NS(conn, C_UNCONNECTED), CS_VERBOSE);
2041 conn_reconfig_done(tconn);
2042 drbd_adm_finish(info, retcode);
2046 free_crypto(&crypto);
2049 conn_reconfig_done(tconn);
2051 drbd_adm_finish(info, retcode);
2055 static enum drbd_state_rv conn_try_disconnect(struct drbd_tconn *tconn, bool force)
2057 enum drbd_state_rv rv;
2059 rv = conn_request_state(tconn, NS(conn, C_DISCONNECTING),
2060 force ? CS_HARD : 0);
2063 case SS_NOTHING_TO_DO:
2065 case SS_ALREADY_STANDALONE:
2067 case SS_PRIMARY_NOP:
2068 /* Our state checking code wants to see the peer outdated. */
2069 rv = conn_request_state(tconn, NS2(conn, C_DISCONNECTING,
2070 pdsk, D_OUTDATED), CS_VERBOSE);
2072 case SS_CW_FAILED_BY_PEER:
2073 /* The peer probably wants to see us outdated. */
2074 rv = conn_request_state(tconn, NS2(conn, C_DISCONNECTING,
2075 disk, D_OUTDATED), 0);
2076 if (rv == SS_IS_DISKLESS || rv == SS_LOWER_THAN_OUTDATED) {
2077 rv = conn_request_state(tconn, NS(conn, C_DISCONNECTING),
2082 /* no special handling necessary */
2085 if (rv >= SS_SUCCESS) {
2086 enum drbd_state_rv rv2;
2087 /* No one else can reconfigure the network while I am here.
2088 * The state handling only uses drbd_thread_stop_nowait(),
2089 * we want to really wait here until the receiver is no more.
2091 drbd_thread_stop(&adm_ctx.tconn->receiver);
2093 /* Race breaker. This additional state change request may be
2094 * necessary, if this was a forced disconnect during a receiver
2095 * restart. We may have "killed" the receiver thread just
2096 * after drbdd_init() returned. Typically, we should be
2097 * C_STANDALONE already, now, and this becomes a no-op.
2099 rv2 = conn_request_state(tconn, NS(conn, C_STANDALONE),
2100 CS_VERBOSE | CS_HARD);
2101 if (rv2 < SS_SUCCESS)
2103 "unexpected rv2=%d in conn_try_disconnect()\n",
2109 int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info)
2111 struct disconnect_parms parms;
2112 struct drbd_tconn *tconn;
2113 enum drbd_state_rv rv;
2114 enum drbd_ret_code retcode;
2117 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONN);
2118 if (!adm_ctx.reply_skb)
2120 if (retcode != NO_ERROR)
2123 tconn = adm_ctx.tconn;
2124 memset(&parms, 0, sizeof(parms));
2125 if (info->attrs[DRBD_NLA_DISCONNECT_PARMS]) {
2126 err = disconnect_parms_from_attrs(&parms, info);
2128 retcode = ERR_MANDATORY_TAG;
2129 drbd_msg_put_info(from_attrs_err_to_txt(err));
2134 rv = conn_try_disconnect(tconn, parms.force_disconnect);
2135 if (rv < SS_SUCCESS)
2136 retcode = rv; /* FIXME: Type mismatch. */
2140 drbd_adm_finish(info, retcode);
2144 void resync_after_online_grow(struct drbd_conf *mdev)
2146 int iass; /* I am sync source */
2148 dev_info(DEV, "Resync of new storage after online grow\n");
2149 if (mdev->state.role != mdev->state.peer)
2150 iass = (mdev->state.role == R_PRIMARY);
2152 iass = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags);
2155 drbd_start_resync(mdev, C_SYNC_SOURCE);
2157 _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE + CS_SERIALIZE);
2160 int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info)
2162 struct resize_parms rs;
2163 struct drbd_conf *mdev;
2164 enum drbd_ret_code retcode;
2165 enum determine_dev_size dd;
2166 enum dds_flags ddsf;
2169 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2170 if (!adm_ctx.reply_skb)
2172 if (retcode != NO_ERROR)
2175 memset(&rs, 0, sizeof(struct resize_parms));
2176 if (info->attrs[DRBD_NLA_RESIZE_PARMS]) {
2177 err = resize_parms_from_attrs(&rs, info);
2179 retcode = ERR_MANDATORY_TAG;
2180 drbd_msg_put_info(from_attrs_err_to_txt(err));
2185 mdev = adm_ctx.mdev;
2186 if (mdev->state.conn > C_CONNECTED) {
2187 retcode = ERR_RESIZE_RESYNC;
2191 if (mdev->state.role == R_SECONDARY &&
2192 mdev->state.peer == R_SECONDARY) {
2193 retcode = ERR_NO_PRIMARY;
2197 if (!get_ldev(mdev)) {
2198 retcode = ERR_NO_DISK;
2202 if (rs.no_resync && mdev->tconn->agreed_pro_version < 93) {
2203 retcode = ERR_NEED_APV_93;
2207 if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev))
2208 mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev);
2210 mdev->ldev->dc.disk_size = (sector_t)rs.resize_size;
2211 ddsf = (rs.resize_force ? DDSF_FORCED : 0) | (rs.no_resync ? DDSF_NO_RESYNC : 0);
2212 dd = drbd_determine_dev_size(mdev, ddsf);
2215 if (dd == dev_size_error) {
2216 retcode = ERR_NOMEM_BITMAP;
2220 if (mdev->state.conn == C_CONNECTED) {
2222 set_bit(RESIZE_PENDING, &mdev->flags);
2224 drbd_send_uuids(mdev);
2225 drbd_send_sizes(mdev, 1, ddsf);
2229 drbd_adm_finish(info, retcode);
2233 void drbd_set_res_opts_defaults(struct res_opts *r)
2235 return set_res_opts_defaults(r);
2238 int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info)
2240 enum drbd_ret_code retcode;
2241 cpumask_var_t new_cpu_mask;
2242 struct drbd_tconn *tconn;
2243 struct res_opts res_opts;
2246 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONN);
2247 if (!adm_ctx.reply_skb)
2249 if (retcode != NO_ERROR)
2251 tconn = adm_ctx.tconn;
2253 if (!zalloc_cpumask_var(&new_cpu_mask, GFP_KERNEL)) {
2254 retcode = ERR_NOMEM;
2255 drbd_msg_put_info("unable to allocate cpumask");
2259 res_opts = tconn->res_opts;
2260 if (should_set_defaults(info))
2261 set_res_opts_defaults(&res_opts);
2263 err = res_opts_from_attrs(&res_opts, info);
2265 retcode = ERR_MANDATORY_TAG;
2266 drbd_msg_put_info(from_attrs_err_to_txt(err));
2270 /* silently ignore cpu mask on UP kernel */
2271 if (nr_cpu_ids > 1 && res_opts.cpu_mask[0] != 0) {
2272 err = __bitmap_parse(res_opts.cpu_mask, 32, 0,
2273 cpumask_bits(new_cpu_mask), nr_cpu_ids);
2275 conn_warn(tconn, "__bitmap_parse() failed with %d\n", err);
2276 retcode = ERR_CPU_MASK_PARSE;
2282 tconn->res_opts = res_opts;
2284 if (!cpumask_equal(tconn->cpu_mask, new_cpu_mask)) {
2285 cpumask_copy(tconn->cpu_mask, new_cpu_mask);
2286 drbd_calc_cpu_mask(tconn);
2287 tconn->receiver.reset_cpu_mask = 1;
2288 tconn->asender.reset_cpu_mask = 1;
2289 tconn->worker.reset_cpu_mask = 1;
2293 free_cpumask_var(new_cpu_mask);
2295 drbd_adm_finish(info, retcode);
2299 int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info)
2301 struct drbd_conf *mdev;
2302 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2304 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2305 if (!adm_ctx.reply_skb)
2307 if (retcode != NO_ERROR)
2310 mdev = adm_ctx.mdev;
2312 /* If there is still bitmap IO pending, probably because of a previous
2313 * resync just being finished, wait for it before requesting a new resync. */
2314 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
2316 retcode = _drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T), CS_ORDERED);
2318 if (retcode < SS_SUCCESS && retcode != SS_NEED_CONNECTION)
2319 retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T));
2321 while (retcode == SS_NEED_CONNECTION) {
2322 spin_lock_irq(&mdev->tconn->req_lock);
2323 if (mdev->state.conn < C_CONNECTED)
2324 retcode = _drbd_set_state(_NS(mdev, disk, D_INCONSISTENT), CS_VERBOSE, NULL);
2325 spin_unlock_irq(&mdev->tconn->req_lock);
2327 if (retcode != SS_NEED_CONNECTION)
2330 retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T));
2334 drbd_adm_finish(info, retcode);
2338 static int drbd_bmio_set_susp_al(struct drbd_conf *mdev)
2342 rv = drbd_bmio_set_n_write(mdev);
2343 drbd_suspend_al(mdev);
2347 static int drbd_adm_simple_request_state(struct sk_buff *skb, struct genl_info *info,
2348 union drbd_state mask, union drbd_state val)
2350 enum drbd_ret_code retcode;
2352 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2353 if (!adm_ctx.reply_skb)
2355 if (retcode != NO_ERROR)
2358 retcode = drbd_request_state(adm_ctx.mdev, mask, val);
2360 drbd_adm_finish(info, retcode);
2364 int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info)
2366 return drbd_adm_simple_request_state(skb, info, NS(conn, C_STARTING_SYNC_S));
2369 int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info)
2371 enum drbd_ret_code retcode;
2373 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2374 if (!adm_ctx.reply_skb)
2376 if (retcode != NO_ERROR)
2379 if (drbd_request_state(adm_ctx.mdev, NS(user_isp, 1)) == SS_NOTHING_TO_DO)
2380 retcode = ERR_PAUSE_IS_SET;
2382 drbd_adm_finish(info, retcode);
2386 int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info)
2388 union drbd_dev_state s;
2389 enum drbd_ret_code retcode;
2391 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2392 if (!adm_ctx.reply_skb)
2394 if (retcode != NO_ERROR)
2397 if (drbd_request_state(adm_ctx.mdev, NS(user_isp, 0)) == SS_NOTHING_TO_DO) {
2398 s = adm_ctx.mdev->state;
2399 if (s.conn == C_PAUSED_SYNC_S || s.conn == C_PAUSED_SYNC_T) {
2400 retcode = s.aftr_isp ? ERR_PIC_AFTER_DEP :
2401 s.peer_isp ? ERR_PIC_PEER_DEP : ERR_PAUSE_IS_CLEAR;
2403 retcode = ERR_PAUSE_IS_CLEAR;
2408 drbd_adm_finish(info, retcode);
2412 int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info)
2414 return drbd_adm_simple_request_state(skb, info, NS(susp, 1));
2417 int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info)
2419 struct drbd_conf *mdev;
2420 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2422 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2423 if (!adm_ctx.reply_skb)
2425 if (retcode != NO_ERROR)
2428 mdev = adm_ctx.mdev;
2429 if (test_bit(NEW_CUR_UUID, &mdev->flags)) {
2430 drbd_uuid_new_current(mdev);
2431 clear_bit(NEW_CUR_UUID, &mdev->flags);
2433 drbd_suspend_io(mdev);
2434 retcode = drbd_request_state(mdev, NS3(susp, 0, susp_nod, 0, susp_fen, 0));
2435 if (retcode == SS_SUCCESS) {
2436 if (mdev->state.conn < C_CONNECTED)
2437 tl_clear(mdev->tconn);
2438 if (mdev->state.disk == D_DISKLESS || mdev->state.disk == D_FAILED)
2439 tl_restart(mdev->tconn, FAIL_FROZEN_DISK_IO);
2441 drbd_resume_io(mdev);
2444 drbd_adm_finish(info, retcode);
2448 int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info)
2450 return drbd_adm_simple_request_state(skb, info, NS(disk, D_OUTDATED));
2453 int nla_put_drbd_cfg_context(struct sk_buff *skb, const char *conn_name, unsigned vnr)
2456 nla = nla_nest_start(skb, DRBD_NLA_CFG_CONTEXT);
2458 goto nla_put_failure;
2459 if (vnr != VOLUME_UNSPECIFIED)
2460 NLA_PUT_U32(skb, T_ctx_volume, vnr);
2461 NLA_PUT_STRING(skb, T_ctx_conn_name, conn_name);
2462 nla_nest_end(skb, nla);
2467 nla_nest_cancel(skb, nla);
2471 int nla_put_status_info(struct sk_buff *skb, struct drbd_conf *mdev,
2472 const struct sib_info *sib)
2474 struct state_info *si = NULL; /* for sizeof(si->member); */
2475 struct net_conf *nc;
2479 int exclude_sensitive;
2481 /* If sib != NULL, this is drbd_bcast_event, which anyone can listen
2482 * to. So we better exclude_sensitive information.
2484 * If sib == NULL, this is drbd_adm_get_status, executed synchronously
2485 * in the context of the requesting user process. Exclude sensitive
2486 * information, unless current has superuser.
2488 * NOTE: for drbd_adm_get_status_all(), this is a netlink dump, and
2489 * relies on the current implementation of netlink_dump(), which
2490 * executes the dump callback successively from netlink_recvmsg(),
2491 * always in the context of the receiving process */
2492 exclude_sensitive = sib || !capable(CAP_SYS_ADMIN);
2494 got_ldev = get_ldev(mdev);
2496 /* We need to add connection name and volume number information still.
2497 * Minor number is in drbd_genlmsghdr. */
2498 if (nla_put_drbd_cfg_context(skb, mdev->tconn->name, mdev->vnr))
2499 goto nla_put_failure;
2501 if (res_opts_to_skb(skb, &mdev->tconn->res_opts, exclude_sensitive))
2502 goto nla_put_failure;
2505 if (disk_conf_to_skb(skb, &mdev->ldev->dc, exclude_sensitive))
2506 goto nla_put_failure;
2509 nc = rcu_dereference(mdev->tconn->net_conf);
2511 err = net_conf_to_skb(skb, nc, exclude_sensitive);
2514 goto nla_put_failure;
2516 nla = nla_nest_start(skb, DRBD_NLA_STATE_INFO);
2518 goto nla_put_failure;
2519 NLA_PUT_U32(skb, T_sib_reason, sib ? sib->sib_reason : SIB_GET_STATUS_REPLY);
2520 NLA_PUT_U32(skb, T_current_state, mdev->state.i);
2521 NLA_PUT_U64(skb, T_ed_uuid, mdev->ed_uuid);
2522 NLA_PUT_U64(skb, T_capacity, drbd_get_capacity(mdev->this_bdev));
2525 NLA_PUT_U32(skb, T_disk_flags, mdev->ldev->md.flags);
2526 NLA_PUT(skb, T_uuids, sizeof(si->uuids), mdev->ldev->md.uuid);
2527 NLA_PUT_U64(skb, T_bits_total, drbd_bm_bits(mdev));
2528 NLA_PUT_U64(skb, T_bits_oos, drbd_bm_total_weight(mdev));
2529 if (C_SYNC_SOURCE <= mdev->state.conn &&
2530 C_PAUSED_SYNC_T >= mdev->state.conn) {
2531 NLA_PUT_U64(skb, T_bits_rs_total, mdev->rs_total);
2532 NLA_PUT_U64(skb, T_bits_rs_failed, mdev->rs_failed);
2537 switch(sib->sib_reason) {
2538 case SIB_SYNC_PROGRESS:
2539 case SIB_GET_STATUS_REPLY:
2541 case SIB_STATE_CHANGE:
2542 NLA_PUT_U32(skb, T_prev_state, sib->os.i);
2543 NLA_PUT_U32(skb, T_new_state, sib->ns.i);
2545 case SIB_HELPER_POST:
2547 T_helper_exit_code, sib->helper_exit_code);
2549 case SIB_HELPER_PRE:
2550 NLA_PUT_STRING(skb, T_helper, sib->helper_name);
2554 nla_nest_end(skb, nla);
2564 int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info)
2566 enum drbd_ret_code retcode;
2569 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2570 if (!adm_ctx.reply_skb)
2572 if (retcode != NO_ERROR)
2575 err = nla_put_status_info(adm_ctx.reply_skb, adm_ctx.mdev, NULL);
2577 nlmsg_free(adm_ctx.reply_skb);
2581 drbd_adm_finish(info, retcode);
2585 int get_one_status(struct sk_buff *skb, struct netlink_callback *cb)
2587 struct drbd_conf *mdev;
2588 struct drbd_genlmsghdr *dh;
2589 struct drbd_tconn *pos = (struct drbd_tconn*)cb->args[0];
2590 struct drbd_tconn *tconn = NULL;
2591 struct drbd_tconn *tmp;
2592 unsigned volume = cb->args[1];
2594 /* Open coded, deferred, iteration:
2595 * list_for_each_entry_safe(tconn, tmp, &drbd_tconns, all_tconn) {
2596 * idr_for_each_entry(&tconn->volumes, mdev, i) {
2600 * where tconn is cb->args[0];
2601 * and i is cb->args[1];
2603 * cb->args[2] indicates if we shall loop over all resources,
2604 * or just dump all volumes of a single resource.
2606 * This may miss entries inserted after this dump started,
2607 * or entries deleted before they are reached.
2609 * We need to make sure the mdev won't disappear while
2610 * we are looking at it, and revalidate our iterators
2611 * on each iteration.
2614 /* synchronize with conn_create()/conn_destroy() */
2615 down_read(&drbd_cfg_rwsem);
2616 /* revalidate iterator position */
2617 list_for_each_entry(tmp, &drbd_tconns, all_tconn) {
2619 /* first iteration */
2631 mdev = idr_get_next(&tconn->volumes, &volume);
2633 /* No more volumes to dump on this tconn.
2634 * Advance tconn iterator. */
2635 pos = list_entry(tconn->all_tconn.next,
2636 struct drbd_tconn, all_tconn);
2637 /* Did we dump any volume on this tconn yet? */
2639 /* If we reached the end of the list,
2640 * or only a single resource dump was requested,
2642 if (&pos->all_tconn == &drbd_tconns || cb->args[2])
2650 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).pid,
2651 cb->nlh->nlmsg_seq, &drbd_genl_family,
2652 NLM_F_MULTI, DRBD_ADM_GET_STATUS);
2657 /* this is a tconn without a single volume */
2659 dh->ret_code = NO_ERROR;
2660 if (nla_put_drbd_cfg_context(skb, tconn->name, VOLUME_UNSPECIFIED))
2661 genlmsg_cancel(skb, dh);
2663 genlmsg_end(skb, dh);
2667 D_ASSERT(mdev->vnr == volume);
2668 D_ASSERT(mdev->tconn == tconn);
2670 dh->minor = mdev_to_minor(mdev);
2671 dh->ret_code = NO_ERROR;
2673 if (nla_put_status_info(skb, mdev, NULL)) {
2674 genlmsg_cancel(skb, dh);
2677 genlmsg_end(skb, dh);
2681 up_read(&drbd_cfg_rwsem);
2682 /* where to start the next iteration */
2683 cb->args[0] = (long)pos;
2684 cb->args[1] = (pos == tconn) ? volume + 1 : 0;
2686 /* No more tconns/volumes/minors found results in an empty skb.
2687 * Which will terminate the dump. */
2692 * Request status of all resources, or of all volumes within a single resource.
2694 * This is a dump, as the answer may not fit in a single reply skb otherwise.
2695 * Which means we cannot use the family->attrbuf or other such members, because
2696 * dump is NOT protected by the genl_lock(). During dump, we only have access
2697 * to the incoming skb, and need to opencode "parsing" of the nlattr payload.
2699 * Once things are setup properly, we call into get_one_status().
2701 int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb)
2703 const unsigned hdrlen = GENL_HDRLEN + GENL_MAGIC_FAMILY_HDRSZ;
2705 const char *conn_name;
2706 struct drbd_tconn *tconn;
2708 /* Is this a followup call? */
2710 /* ... of a single resource dump,
2711 * and the resource iterator has been advanced already? */
2712 if (cb->args[2] && cb->args[2] != cb->args[0])
2713 return 0; /* DONE. */
2717 /* First call (from netlink_dump_start). We need to figure out
2718 * which resource(s) the user wants us to dump. */
2719 nla = nla_find(nlmsg_attrdata(cb->nlh, hdrlen),
2720 nlmsg_attrlen(cb->nlh, hdrlen),
2721 DRBD_NLA_CFG_CONTEXT);
2723 /* No explicit context given. Dump all. */
2726 nla = nla_find_nested(nla, __nla_type(T_ctx_conn_name));
2727 /* context given, but no name present? */
2730 conn_name = nla_data(nla);
2731 tconn = conn_get_by_name(conn_name);
2736 kref_put(&tconn->kref, &conn_destroy); /* get_one_status() (re)validates tconn by itself */
2738 /* prime iterators, and set "filter" mode mark:
2739 * only dump this tconn. */
2740 cb->args[0] = (long)tconn;
2741 /* cb->args[1] = 0; passed in this way. */
2742 cb->args[2] = (long)tconn;
2745 return get_one_status(skb, cb);
2748 int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info)
2750 enum drbd_ret_code retcode;
2751 struct timeout_parms tp;
2754 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2755 if (!adm_ctx.reply_skb)
2757 if (retcode != NO_ERROR)
2761 adm_ctx.mdev->state.pdsk == D_OUTDATED ? UT_PEER_OUTDATED :
2762 test_bit(USE_DEGR_WFC_T, &adm_ctx.mdev->flags) ? UT_DEGRADED :
2765 err = timeout_parms_to_priv_skb(adm_ctx.reply_skb, &tp);
2767 nlmsg_free(adm_ctx.reply_skb);
2771 drbd_adm_finish(info, retcode);
2775 int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info)
2777 struct drbd_conf *mdev;
2778 enum drbd_ret_code retcode;
2780 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2781 if (!adm_ctx.reply_skb)
2783 if (retcode != NO_ERROR)
2786 mdev = adm_ctx.mdev;
2787 if (info->attrs[DRBD_NLA_START_OV_PARMS]) {
2788 /* resume from last known position, if possible */
2789 struct start_ov_parms parms =
2790 { .ov_start_sector = mdev->ov_start_sector };
2791 int err = start_ov_parms_from_attrs(&parms, info);
2793 retcode = ERR_MANDATORY_TAG;
2794 drbd_msg_put_info(from_attrs_err_to_txt(err));
2797 /* w_make_ov_request expects position to be aligned */
2798 mdev->ov_start_sector = parms.ov_start_sector & ~BM_SECT_PER_BIT;
2800 /* If there is still bitmap IO pending, e.g. previous resync or verify
2801 * just being finished, wait for it before requesting a new resync. */
2802 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
2803 retcode = drbd_request_state(mdev,NS(conn,C_VERIFY_S));
2805 drbd_adm_finish(info, retcode);
2810 int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info)
2812 struct drbd_conf *mdev;
2813 enum drbd_ret_code retcode;
2814 int skip_initial_sync = 0;
2816 struct new_c_uuid_parms args;
2818 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2819 if (!adm_ctx.reply_skb)
2821 if (retcode != NO_ERROR)
2824 mdev = adm_ctx.mdev;
2825 memset(&args, 0, sizeof(args));
2826 if (info->attrs[DRBD_NLA_NEW_C_UUID_PARMS]) {
2827 err = new_c_uuid_parms_from_attrs(&args, info);
2829 retcode = ERR_MANDATORY_TAG;
2830 drbd_msg_put_info(from_attrs_err_to_txt(err));
2835 mutex_lock(mdev->state_mutex); /* Protects us against serialized state changes. */
2837 if (!get_ldev(mdev)) {
2838 retcode = ERR_NO_DISK;
2842 /* this is "skip initial sync", assume to be clean */
2843 if (mdev->state.conn == C_CONNECTED && mdev->tconn->agreed_pro_version >= 90 &&
2844 mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED && args.clear_bm) {
2845 dev_info(DEV, "Preparing to skip initial sync\n");
2846 skip_initial_sync = 1;
2847 } else if (mdev->state.conn != C_STANDALONE) {
2848 retcode = ERR_CONNECTED;
2852 drbd_uuid_set(mdev, UI_BITMAP, 0); /* Rotate UI_BITMAP to History 1, etc... */
2853 drbd_uuid_new_current(mdev); /* New current, previous to UI_BITMAP */
2855 if (args.clear_bm) {
2856 err = drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write,
2857 "clear_n_write from new_c_uuid", BM_LOCKED_MASK);
2859 dev_err(DEV, "Writing bitmap failed with %d\n",err);
2860 retcode = ERR_IO_MD_DISK;
2862 if (skip_initial_sync) {
2863 drbd_send_uuids_skip_initial_sync(mdev);
2864 _drbd_uuid_set(mdev, UI_BITMAP, 0);
2865 drbd_print_uuids(mdev, "cleared bitmap UUID");
2866 spin_lock_irq(&mdev->tconn->req_lock);
2867 _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
2869 spin_unlock_irq(&mdev->tconn->req_lock);
2877 mutex_unlock(mdev->state_mutex);
2879 drbd_adm_finish(info, retcode);
2883 static enum drbd_ret_code
2884 drbd_check_conn_name(const char *name)
2886 if (!name || !name[0]) {
2887 drbd_msg_put_info("connection name missing");
2888 return ERR_MANDATORY_TAG;
2890 /* if we want to use these in sysfs/configfs/debugfs some day,
2891 * we must not allow slashes */
2892 if (strchr(name, '/')) {
2893 drbd_msg_put_info("invalid connection name");
2894 return ERR_INVALID_REQUEST;
2899 int drbd_adm_create_connection(struct sk_buff *skb, struct genl_info *info)
2901 enum drbd_ret_code retcode;
2903 retcode = drbd_adm_prepare(skb, info, 0);
2904 if (!adm_ctx.reply_skb)
2906 if (retcode != NO_ERROR)
2909 retcode = drbd_check_conn_name(adm_ctx.conn_name);
2910 if (retcode != NO_ERROR)
2913 if (adm_ctx.tconn) {
2914 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL) {
2915 retcode = ERR_INVALID_REQUEST;
2916 drbd_msg_put_info("connection exists");
2918 /* else: still NO_ERROR */
2922 if (!conn_create(adm_ctx.conn_name))
2923 retcode = ERR_NOMEM;
2925 drbd_adm_finish(info, retcode);
2929 int drbd_adm_add_minor(struct sk_buff *skb, struct genl_info *info)
2931 struct drbd_genlmsghdr *dh = info->userhdr;
2932 enum drbd_ret_code retcode;
2934 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONN);
2935 if (!adm_ctx.reply_skb)
2937 if (retcode != NO_ERROR)
2940 /* FIXME drop minor_count parameter, limit to MINORMASK */
2941 if (dh->minor >= minor_count) {
2942 drbd_msg_put_info("requested minor out of range");
2943 retcode = ERR_INVALID_REQUEST;
2946 if (adm_ctx.volume > DRBD_VOLUME_MAX) {
2947 drbd_msg_put_info("requested volume id out of range");
2948 retcode = ERR_INVALID_REQUEST;
2952 /* drbd_adm_prepare made sure already
2953 * that mdev->tconn and mdev->vnr match the request. */
2955 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
2956 retcode = ERR_MINOR_EXISTS;
2957 /* else: still NO_ERROR */
2961 down_write(&drbd_cfg_rwsem);
2962 retcode = conn_new_minor(adm_ctx.tconn, dh->minor, adm_ctx.volume);
2963 up_write(&drbd_cfg_rwsem);
2965 drbd_adm_finish(info, retcode);
2969 static enum drbd_ret_code adm_delete_minor(struct drbd_conf *mdev)
2971 if (mdev->state.disk == D_DISKLESS &&
2972 /* no need to be mdev->state.conn == C_STANDALONE &&
2973 * we may want to delete a minor from a live replication group.
2975 mdev->state.role == R_SECONDARY) {
2976 drbd_delete_device(mdev);
2979 return ERR_MINOR_CONFIGURED;
2982 int drbd_adm_delete_minor(struct sk_buff *skb, struct genl_info *info)
2984 enum drbd_ret_code retcode;
2986 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2987 if (!adm_ctx.reply_skb)
2989 if (retcode != NO_ERROR)
2992 down_write(&drbd_cfg_rwsem);
2993 retcode = adm_delete_minor(adm_ctx.mdev);
2994 up_write(&drbd_cfg_rwsem);
2996 drbd_adm_finish(info, retcode);
3000 int drbd_adm_down(struct sk_buff *skb, struct genl_info *info)
3002 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
3003 struct drbd_conf *mdev;
3006 retcode = drbd_adm_prepare(skb, info, 0);
3007 if (!adm_ctx.reply_skb)
3009 if (retcode != NO_ERROR)
3012 if (!adm_ctx.tconn) {
3013 retcode = ERR_CONN_NOT_KNOWN;
3017 down_read(&drbd_cfg_rwsem);
3019 idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
3020 retcode = drbd_set_role(mdev, R_SECONDARY, 0);
3021 if (retcode < SS_SUCCESS) {
3022 drbd_msg_put_info("failed to demote");
3026 up_read(&drbd_cfg_rwsem);
3028 /* disconnect; may stop the receiver;
3029 * must not hold the drbd_cfg_rwsem */
3030 retcode = conn_try_disconnect(adm_ctx.tconn, 0);
3031 if (retcode < SS_SUCCESS) {
3032 drbd_msg_put_info("failed to disconnect");
3036 down_read(&drbd_cfg_rwsem);
3038 idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
3039 retcode = adm_detach(mdev);
3040 if (retcode < SS_SUCCESS) {
3041 drbd_msg_put_info("failed to detach");
3045 up_read(&drbd_cfg_rwsem);
3047 /* If we reach this, all volumes (of this tconn) are Secondary,
3048 * Disconnected, Diskless, aka Unconfigured. Make sure all threads have
3049 * actually stopped, state handling only does drbd_thread_stop_nowait().
3050 * This needs to be done without holding drbd_cfg_rwsem. */
3051 drbd_thread_stop(&adm_ctx.tconn->worker);
3053 /* Now, nothing can fail anymore */
3055 /* delete volumes */
3056 down_write(&drbd_cfg_rwsem);
3057 idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
3058 retcode = adm_delete_minor(mdev);
3059 if (retcode != NO_ERROR) {
3060 /* "can not happen" */
3061 drbd_msg_put_info("failed to delete volume");
3062 up_write(&drbd_cfg_rwsem);
3067 /* delete connection */
3068 if (conn_lowest_minor(adm_ctx.tconn) < 0) {
3069 list_del(&adm_ctx.tconn->all_tconn);
3070 kref_put(&adm_ctx.tconn->kref, &conn_destroy);
3074 /* "can not happen" */
3075 retcode = ERR_CONN_IN_USE;
3076 drbd_msg_put_info("failed to delete connection");
3078 up_write(&drbd_cfg_rwsem);
3081 up_read(&drbd_cfg_rwsem);
3083 drbd_adm_finish(info, retcode);
3087 int drbd_adm_delete_connection(struct sk_buff *skb, struct genl_info *info)
3089 enum drbd_ret_code retcode;
3091 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONN);
3092 if (!adm_ctx.reply_skb)
3094 if (retcode != NO_ERROR)
3097 down_write(&drbd_cfg_rwsem);
3098 if (conn_lowest_minor(adm_ctx.tconn) < 0) {
3099 list_del(&adm_ctx.tconn->all_tconn);
3100 kref_put(&adm_ctx.tconn->kref, &conn_destroy);
3104 retcode = ERR_CONN_IN_USE;
3106 up_write(&drbd_cfg_rwsem);
3108 if (retcode == NO_ERROR)
3109 drbd_thread_stop(&adm_ctx.tconn->worker);
3111 drbd_adm_finish(info, retcode);
3115 void drbd_bcast_event(struct drbd_conf *mdev, const struct sib_info *sib)
3117 static atomic_t drbd_genl_seq = ATOMIC_INIT(2); /* two. */
3118 struct sk_buff *msg;
3119 struct drbd_genlmsghdr *d_out;
3123 seq = atomic_inc_return(&drbd_genl_seq);
3124 msg = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
3129 d_out = genlmsg_put(msg, 0, seq, &drbd_genl_family, 0, DRBD_EVENT);
3130 if (!d_out) /* cannot happen, but anyways. */
3131 goto nla_put_failure;
3132 d_out->minor = mdev_to_minor(mdev);
3133 d_out->ret_code = 0;
3135 if (nla_put_status_info(msg, mdev, sib))
3136 goto nla_put_failure;
3137 genlmsg_end(msg, d_out);
3138 err = drbd_genl_multicast_events(msg, 0);
3139 /* msg has been consumed or freed in netlink_broadcast() */
3140 if (err && err != -ESRCH)
3148 dev_err(DEV, "Error %d while broadcasting event. "
3149 "Event seq:%u sib_reason:%u\n",
3150 err, seq, sib->sib_reason);