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 };
326 struct drbd_tconn *tconn = mdev->tconn;
330 if (current == tconn->worker.task)
331 set_bit(CALLBACK_PENDING, &tconn->flags);
333 snprintf(mb, 12, "minor-%d", mdev_to_minor(mdev));
334 setup_khelper_env(tconn, envp);
336 /* The helper may take some time.
337 * write out any unsynced meta data changes now */
340 dev_info(DEV, "helper command: %s %s %s\n", usermode_helper, cmd, mb);
341 sib.sib_reason = SIB_HELPER_PRE;
342 sib.helper_name = cmd;
343 drbd_bcast_event(mdev, &sib);
344 ret = call_usermodehelper(usermode_helper, argv, envp, 1);
346 dev_warn(DEV, "helper command: %s %s %s exit code %u (0x%x)\n",
347 usermode_helper, cmd, mb,
348 (ret >> 8) & 0xff, ret);
350 dev_info(DEV, "helper command: %s %s %s exit code %u (0x%x)\n",
351 usermode_helper, cmd, mb,
352 (ret >> 8) & 0xff, ret);
353 sib.sib_reason = SIB_HELPER_POST;
354 sib.helper_exit_code = ret;
355 drbd_bcast_event(mdev, &sib);
357 if (current == tconn->worker.task)
358 clear_bit(CALLBACK_PENDING, &tconn->flags);
360 if (ret < 0) /* Ignore any ERRNOs we got. */
366 static void conn_md_sync(struct drbd_tconn *tconn)
368 struct drbd_conf *mdev;
372 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
373 kref_get(&mdev->kref);
376 kref_put(&mdev->kref, &drbd_minor_destroy);
382 int conn_khelper(struct drbd_tconn *tconn, char *cmd)
384 char *envp[] = { "HOME=/",
386 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
387 (char[20]) { }, /* address family */
388 (char[60]) { }, /* address */
390 char *argv[] = {usermode_helper, cmd, tconn->name, NULL };
393 setup_khelper_env(tconn, envp);
396 conn_info(tconn, "helper command: %s %s %s\n", usermode_helper, cmd, tconn->name);
397 /* TODO: conn_bcast_event() ?? */
399 ret = call_usermodehelper(usermode_helper, argv, envp, 1);
401 conn_warn(tconn, "helper command: %s %s %s exit code %u (0x%x)\n",
402 usermode_helper, cmd, tconn->name,
403 (ret >> 8) & 0xff, ret);
405 conn_info(tconn, "helper command: %s %s %s exit code %u (0x%x)\n",
406 usermode_helper, cmd, tconn->name,
407 (ret >> 8) & 0xff, ret);
408 /* TODO: conn_bcast_event() ?? */
410 if (ret < 0) /* Ignore any ERRNOs we got. */
416 static enum drbd_fencing_p highest_fencing_policy(struct drbd_tconn *tconn)
418 enum drbd_fencing_p fp = FP_NOT_AVAIL;
419 struct drbd_conf *mdev;
423 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
424 if (get_ldev_if_state(mdev, D_CONSISTENT)) {
425 fp = max_t(enum drbd_fencing_p, fp,
426 rcu_dereference(mdev->ldev->disk_conf)->fencing);
435 bool conn_try_outdate_peer(struct drbd_tconn *tconn)
437 union drbd_state mask = { };
438 union drbd_state val = { };
439 enum drbd_fencing_p fp;
443 if (tconn->cstate >= C_WF_REPORT_PARAMS) {
444 conn_err(tconn, "Expected cstate < C_WF_REPORT_PARAMS\n");
448 fp = highest_fencing_policy(tconn);
451 conn_warn(tconn, "Not fencing peer, I'm not even Consistent myself.\n");
458 r = conn_khelper(tconn, "fence-peer");
460 switch ((r>>8) & 0xff) {
461 case 3: /* peer is inconsistent */
462 ex_to_string = "peer is inconsistent or worse";
464 val.pdsk = D_INCONSISTENT;
466 case 4: /* peer got outdated, or was already outdated */
467 ex_to_string = "peer was fenced";
469 val.pdsk = D_OUTDATED;
471 case 5: /* peer was down */
472 if (conn_highest_disk(tconn) == D_UP_TO_DATE) {
473 /* we will(have) create(d) a new UUID anyways... */
474 ex_to_string = "peer is unreachable, assumed to be dead";
476 val.pdsk = D_OUTDATED;
478 ex_to_string = "peer unreachable, doing nothing since disk != UpToDate";
481 case 6: /* Peer is primary, voluntarily outdate myself.
482 * This is useful when an unconnected R_SECONDARY is asked to
483 * become R_PRIMARY, but finds the other peer being active. */
484 ex_to_string = "peer is active";
485 conn_warn(tconn, "Peer is primary, outdating myself.\n");
487 val.disk = D_OUTDATED;
490 if (fp != FP_STONITH)
491 conn_err(tconn, "fence-peer() = 7 && fencing != Stonith !!!\n");
492 ex_to_string = "peer was stonithed";
494 val.pdsk = D_OUTDATED;
497 /* The script is broken ... */
498 conn_err(tconn, "fence-peer helper broken, returned %d\n", (r>>8)&0xff);
499 return false; /* Eventually leave IO frozen */
502 conn_info(tconn, "fence-peer helper returned %d (%s)\n",
503 (r>>8) & 0xff, ex_to_string);
508 conn_request_state(tconn, mask, val, CS_VERBOSE);
509 here, because we might were able to re-establish the connection in the
511 spin_lock_irq(&tconn->req_lock);
512 if (tconn->cstate < C_WF_REPORT_PARAMS && !test_bit(STATE_SENT, &tconn->flags))
513 _conn_request_state(tconn, mask, val, CS_VERBOSE);
514 spin_unlock_irq(&tconn->req_lock);
516 return conn_highest_pdsk(tconn) <= D_OUTDATED;
519 static int _try_outdate_peer_async(void *data)
521 struct drbd_tconn *tconn = (struct drbd_tconn *)data;
523 conn_try_outdate_peer(tconn);
525 kref_put(&tconn->kref, &conn_destroy);
529 void conn_try_outdate_peer_async(struct drbd_tconn *tconn)
531 struct task_struct *opa;
533 kref_get(&tconn->kref);
534 opa = kthread_run(_try_outdate_peer_async, tconn, "drbd_async_h");
536 conn_err(tconn, "out of mem, failed to invoke fence-peer helper\n");
537 kref_put(&tconn->kref, &conn_destroy);
542 drbd_set_role(struct drbd_conf *mdev, enum drbd_role new_role, int force)
544 const int max_tries = 4;
545 enum drbd_state_rv rv = SS_UNKNOWN_ERROR;
549 union drbd_state mask, val;
551 if (new_role == R_PRIMARY)
552 request_ping(mdev->tconn); /* Detect a dead peer ASAP */
554 mutex_lock(mdev->state_mutex);
556 mask.i = 0; mask.role = R_MASK;
557 val.i = 0; val.role = new_role;
559 while (try++ < max_tries) {
560 rv = _drbd_request_state(mdev, mask, val, CS_WAIT_COMPLETE);
562 /* in case we first succeeded to outdate,
563 * but now suddenly could establish a connection */
564 if (rv == SS_CW_FAILED_BY_PEER && mask.pdsk != 0) {
570 if (rv == SS_NO_UP_TO_DATE_DISK && force &&
571 (mdev->state.disk < D_UP_TO_DATE &&
572 mdev->state.disk >= D_INCONSISTENT)) {
574 val.disk = D_UP_TO_DATE;
579 if (rv == SS_NO_UP_TO_DATE_DISK &&
580 mdev->state.disk == D_CONSISTENT && mask.pdsk == 0) {
581 D_ASSERT(mdev->state.pdsk == D_UNKNOWN);
583 if (conn_try_outdate_peer(mdev->tconn)) {
584 val.disk = D_UP_TO_DATE;
590 if (rv == SS_NOTHING_TO_DO)
592 if (rv == SS_PRIMARY_NOP && mask.pdsk == 0) {
593 if (!conn_try_outdate_peer(mdev->tconn) && force) {
594 dev_warn(DEV, "Forced into split brain situation!\n");
596 val.pdsk = D_OUTDATED;
601 if (rv == SS_TWO_PRIMARIES) {
602 /* Maybe the peer is detected as dead very soon...
603 retry at most once more in this case. */
606 nc = rcu_dereference(mdev->tconn->net_conf);
607 timeo = nc ? (nc->ping_timeo + 1) * HZ / 10 : 1;
609 schedule_timeout_interruptible(timeo);
614 if (rv < SS_SUCCESS) {
615 rv = _drbd_request_state(mdev, mask, val,
616 CS_VERBOSE + CS_WAIT_COMPLETE);
627 dev_warn(DEV, "Forced to consider local data as UpToDate!\n");
629 /* Wait until nothing is on the fly :) */
630 wait_event(mdev->misc_wait, atomic_read(&mdev->ap_pending_cnt) == 0);
632 /* FIXME also wait for all pending P_BARRIER_ACK? */
634 if (new_role == R_SECONDARY) {
635 set_disk_ro(mdev->vdisk, true);
636 if (get_ldev(mdev)) {
637 mdev->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
641 mutex_lock(&mdev->tconn->conf_update);
642 nc = mdev->tconn->net_conf;
644 nc->discard_my_data = 0; /* without copy; single bit op is atomic */
645 mutex_unlock(&mdev->tconn->conf_update);
647 set_disk_ro(mdev->vdisk, false);
648 if (get_ldev(mdev)) {
649 if (((mdev->state.conn < C_CONNECTED ||
650 mdev->state.pdsk <= D_FAILED)
651 && mdev->ldev->md.uuid[UI_BITMAP] == 0) || forced)
652 drbd_uuid_new_current(mdev);
654 mdev->ldev->md.uuid[UI_CURRENT] |= (u64)1;
659 /* writeout of activity log covered areas of the bitmap
660 * to stable storage done in after state change already */
662 if (mdev->state.conn >= C_WF_REPORT_PARAMS) {
663 /* if this was forced, we should consider sync */
665 drbd_send_uuids(mdev);
666 drbd_send_current_state(mdev);
671 kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
673 mutex_unlock(mdev->state_mutex);
677 static const char *from_attrs_err_to_txt(int err)
679 return err == -ENOMSG ? "required attribute missing" :
680 err == -EOPNOTSUPP ? "unknown mandatory attribute" :
681 err == -EEXIST ? "can not change invariant setting" :
682 "invalid attribute value";
685 int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info)
687 struct set_role_parms parms;
689 enum drbd_ret_code retcode;
691 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
692 if (!adm_ctx.reply_skb)
694 if (retcode != NO_ERROR)
697 memset(&parms, 0, sizeof(parms));
698 if (info->attrs[DRBD_NLA_SET_ROLE_PARMS]) {
699 err = set_role_parms_from_attrs(&parms, info);
701 retcode = ERR_MANDATORY_TAG;
702 drbd_msg_put_info(from_attrs_err_to_txt(err));
707 if (info->genlhdr->cmd == DRBD_ADM_PRIMARY)
708 retcode = drbd_set_role(adm_ctx.mdev, R_PRIMARY, parms.assume_uptodate);
710 retcode = drbd_set_role(adm_ctx.mdev, R_SECONDARY, 0);
712 drbd_adm_finish(info, retcode);
716 /* initializes the md.*_offset members, so we are able to find
717 * the on disk meta data */
718 static void drbd_md_set_sector_offsets(struct drbd_conf *mdev,
719 struct drbd_backing_dev *bdev)
721 sector_t md_size_sect = 0;
725 meta_dev_idx = rcu_dereference(bdev->disk_conf)->meta_dev_idx;
727 switch (meta_dev_idx) {
729 /* v07 style fixed size indexed meta data */
730 bdev->md.md_size_sect = MD_RESERVED_SECT;
731 bdev->md.md_offset = drbd_md_ss__(mdev, bdev);
732 bdev->md.al_offset = MD_AL_OFFSET;
733 bdev->md.bm_offset = MD_BM_OFFSET;
735 case DRBD_MD_INDEX_FLEX_EXT:
736 /* just occupy the full device; unit: sectors */
737 bdev->md.md_size_sect = drbd_get_capacity(bdev->md_bdev);
738 bdev->md.md_offset = 0;
739 bdev->md.al_offset = MD_AL_OFFSET;
740 bdev->md.bm_offset = MD_BM_OFFSET;
742 case DRBD_MD_INDEX_INTERNAL:
743 case DRBD_MD_INDEX_FLEX_INT:
744 bdev->md.md_offset = drbd_md_ss__(mdev, bdev);
745 /* al size is still fixed */
746 bdev->md.al_offset = -MD_AL_SECTORS;
747 /* we need (slightly less than) ~ this much bitmap sectors: */
748 md_size_sect = drbd_get_capacity(bdev->backing_bdev);
749 md_size_sect = ALIGN(md_size_sect, BM_SECT_PER_EXT);
750 md_size_sect = BM_SECT_TO_EXT(md_size_sect);
751 md_size_sect = ALIGN(md_size_sect, 8);
753 /* plus the "drbd meta data super block",
754 * and the activity log; */
755 md_size_sect += MD_BM_OFFSET;
757 bdev->md.md_size_sect = md_size_sect;
758 /* bitmap offset is adjusted by 'super' block size */
759 bdev->md.bm_offset = -md_size_sect + MD_AL_OFFSET;
765 /* input size is expected to be in KB */
766 char *ppsize(char *buf, unsigned long long size)
768 /* Needs 9 bytes at max including trailing NUL:
769 * -1ULL ==> "16384 EB" */
770 static char units[] = { 'K', 'M', 'G', 'T', 'P', 'E' };
772 while (size >= 10000 && base < sizeof(units)-1) {
774 size = (size >> 10) + !!(size & (1<<9));
777 sprintf(buf, "%u %cB", (unsigned)size, units[base]);
782 /* there is still a theoretical deadlock when called from receiver
783 * on an D_INCONSISTENT R_PRIMARY:
784 * remote READ does inc_ap_bio, receiver would need to receive answer
785 * packet from remote to dec_ap_bio again.
786 * receiver receive_sizes(), comes here,
787 * waits for ap_bio_cnt == 0. -> deadlock.
788 * but this cannot happen, actually, because:
789 * R_PRIMARY D_INCONSISTENT, and peer's disk is unreachable
790 * (not connected, or bad/no disk on peer):
791 * see drbd_fail_request_early, ap_bio_cnt is zero.
792 * R_PRIMARY D_INCONSISTENT, and C_SYNC_TARGET:
793 * peer may not initiate a resize.
795 /* Note these are not to be confused with
796 * drbd_adm_suspend_io/drbd_adm_resume_io,
797 * which are (sub) state changes triggered by admin (drbdsetup),
798 * and can be long lived.
799 * This changes an mdev->flag, is triggered by drbd internals,
800 * and should be short-lived. */
801 void drbd_suspend_io(struct drbd_conf *mdev)
803 set_bit(SUSPEND_IO, &mdev->flags);
804 if (drbd_suspended(mdev))
806 wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_bio_cnt));
809 void drbd_resume_io(struct drbd_conf *mdev)
811 clear_bit(SUSPEND_IO, &mdev->flags);
812 wake_up(&mdev->misc_wait);
816 * drbd_determine_dev_size() - Sets the right device size obeying all constraints
817 * @mdev: DRBD device.
819 * Returns 0 on success, negative return values indicate errors.
820 * You should call drbd_md_sync() after calling this function.
822 enum determine_dev_size drbd_determine_dev_size(struct drbd_conf *mdev, enum dds_flags flags) __must_hold(local)
824 sector_t prev_first_sect, prev_size; /* previous meta location */
825 sector_t la_size, u_size;
829 int md_moved, la_size_changed;
830 enum determine_dev_size rv = unchanged;
833 * application request passes inc_ap_bio,
834 * but then cannot get an AL-reference.
835 * this function later may wait on ap_bio_cnt == 0. -> deadlock.
838 * Suspend IO right here.
839 * still lock the act_log to not trigger ASSERTs there.
841 drbd_suspend_io(mdev);
843 /* no wait necessary anymore, actually we could assert that */
844 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
846 prev_first_sect = drbd_md_first_sector(mdev->ldev);
847 prev_size = mdev->ldev->md.md_size_sect;
848 la_size = mdev->ldev->md.la_size_sect;
850 /* TODO: should only be some assert here, not (re)init... */
851 drbd_md_set_sector_offsets(mdev, mdev->ldev);
854 u_size = rcu_dereference(mdev->ldev->disk_conf)->disk_size;
856 size = drbd_new_dev_size(mdev, mdev->ldev, u_size, flags & DDSF_FORCED);
858 if (drbd_get_capacity(mdev->this_bdev) != size ||
859 drbd_bm_capacity(mdev) != size) {
861 err = drbd_bm_resize(mdev, size, !(flags & DDSF_NO_RESYNC));
863 /* currently there is only one error: ENOMEM! */
864 size = drbd_bm_capacity(mdev)>>1;
866 dev_err(DEV, "OUT OF MEMORY! "
867 "Could not allocate bitmap!\n");
869 dev_err(DEV, "BM resizing failed. "
870 "Leaving size unchanged at size = %lu KB\n",
871 (unsigned long)size);
875 /* racy, see comments above. */
876 drbd_set_my_capacity(mdev, size);
877 mdev->ldev->md.la_size_sect = size;
878 dev_info(DEV, "size = %s (%llu KB)\n", ppsize(ppb, size>>1),
879 (unsigned long long)size>>1);
881 if (rv == dev_size_error)
884 la_size_changed = (la_size != mdev->ldev->md.la_size_sect);
886 md_moved = prev_first_sect != drbd_md_first_sector(mdev->ldev)
887 || prev_size != mdev->ldev->md.md_size_sect;
889 if (la_size_changed || md_moved) {
892 drbd_al_shrink(mdev); /* All extents inactive. */
893 dev_info(DEV, "Writing the whole bitmap, %s\n",
894 la_size_changed && md_moved ? "size changed and md moved" :
895 la_size_changed ? "size changed" : "md moved");
896 /* next line implicitly does drbd_suspend_io()+drbd_resume_io() */
897 err = drbd_bitmap_io(mdev, &drbd_bm_write,
898 "size changed", BM_LOCKED_MASK);
903 drbd_md_mark_dirty(mdev);
911 lc_unlock(mdev->act_log);
912 wake_up(&mdev->al_wait);
913 drbd_resume_io(mdev);
919 drbd_new_dev_size(struct drbd_conf *mdev, struct drbd_backing_dev *bdev,
920 sector_t u_size, int assume_peer_has_space)
922 sector_t p_size = mdev->p_size; /* partner's disk size. */
923 sector_t la_size = bdev->md.la_size_sect; /* last agreed size. */
924 sector_t m_size; /* my size */
927 m_size = drbd_get_max_capacity(bdev);
929 if (mdev->state.conn < C_CONNECTED && assume_peer_has_space) {
930 dev_warn(DEV, "Resize while not connected was forced by the user!\n");
934 if (p_size && m_size) {
935 size = min_t(sector_t, p_size, m_size);
939 if (m_size && m_size < size)
941 if (p_size && p_size < size)
952 dev_err(DEV, "Both nodes diskless!\n");
956 dev_err(DEV, "Requested disk size is too big (%lu > %lu)\n",
957 (unsigned long)u_size>>1, (unsigned long)size>>1);
966 * drbd_check_al_size() - Ensures that the AL is of the right size
967 * @mdev: DRBD device.
969 * Returns -EBUSY if current al lru is still used, -ENOMEM when allocation
970 * failed, and 0 on success. You should call drbd_md_sync() after you called
973 static int drbd_check_al_size(struct drbd_conf *mdev, struct disk_conf *dc)
975 struct lru_cache *n, *t;
976 struct lc_element *e;
981 mdev->act_log->nr_elements == dc->al_extents)
986 n = lc_create("act_log", drbd_al_ext_cache, AL_UPDATES_PER_TRANSACTION,
987 dc->al_extents, sizeof(struct lc_element), 0);
990 dev_err(DEV, "Cannot allocate act_log lru!\n");
993 spin_lock_irq(&mdev->al_lock);
995 for (i = 0; i < t->nr_elements; i++) {
996 e = lc_element_by_index(t, i);
998 dev_err(DEV, "refcnt(%d)==%d\n",
999 e->lc_number, e->refcnt);
1000 in_use += e->refcnt;
1005 spin_unlock_irq(&mdev->al_lock);
1007 dev_err(DEV, "Activity log still in use!\n");
1014 drbd_md_mark_dirty(mdev); /* we changed mdev->act_log->nr_elemens */
1018 static void drbd_setup_queue_param(struct drbd_conf *mdev, unsigned int max_bio_size)
1020 struct request_queue * const q = mdev->rq_queue;
1021 int max_hw_sectors = max_bio_size >> 9;
1022 int max_segments = 0;
1024 if (get_ldev_if_state(mdev, D_ATTACHING)) {
1025 struct request_queue * const b = mdev->ldev->backing_bdev->bd_disk->queue;
1027 max_hw_sectors = min(queue_max_hw_sectors(b), max_bio_size >> 9);
1029 max_segments = rcu_dereference(mdev->ldev->disk_conf)->max_bio_bvecs;
1034 blk_queue_logical_block_size(q, 512);
1035 blk_queue_max_hw_sectors(q, max_hw_sectors);
1036 /* This is the workaround for "bio would need to, but cannot, be split" */
1037 blk_queue_max_segments(q, max_segments ? max_segments : BLK_MAX_SEGMENTS);
1038 blk_queue_segment_boundary(q, PAGE_CACHE_SIZE-1);
1040 if (get_ldev_if_state(mdev, D_ATTACHING)) {
1041 struct request_queue * const b = mdev->ldev->backing_bdev->bd_disk->queue;
1043 blk_queue_stack_limits(q, b);
1045 if (q->backing_dev_info.ra_pages != b->backing_dev_info.ra_pages) {
1046 dev_info(DEV, "Adjusting my ra_pages to backing device's (%lu -> %lu)\n",
1047 q->backing_dev_info.ra_pages,
1048 b->backing_dev_info.ra_pages);
1049 q->backing_dev_info.ra_pages = b->backing_dev_info.ra_pages;
1055 void drbd_reconsider_max_bio_size(struct drbd_conf *mdev)
1057 int now, new, local, peer;
1059 now = queue_max_hw_sectors(mdev->rq_queue) << 9;
1060 local = mdev->local_max_bio_size; /* Eventually last known value, from volatile memory */
1061 peer = mdev->peer_max_bio_size; /* Eventually last known value, from meta data */
1063 if (get_ldev_if_state(mdev, D_ATTACHING)) {
1064 local = queue_max_hw_sectors(mdev->ldev->backing_bdev->bd_disk->queue) << 9;
1065 mdev->local_max_bio_size = local;
1069 /* We may ignore peer limits if the peer is modern enough.
1070 Because new from 8.3.8 onwards the peer can use multiple
1071 BIOs for a single peer_request */
1072 if (mdev->state.conn >= C_CONNECTED) {
1073 if (mdev->tconn->agreed_pro_version < 94)
1074 peer = min_t(int, mdev->peer_max_bio_size, DRBD_MAX_SIZE_H80_PACKET);
1075 /* Correct old drbd (up to 8.3.7) if it believes it can do more than 32KiB */
1076 else if (mdev->tconn->agreed_pro_version == 94)
1077 peer = DRBD_MAX_SIZE_H80_PACKET;
1078 else if (mdev->tconn->agreed_pro_version < 100)
1079 peer = DRBD_MAX_BIO_SIZE_P95; /* drbd 8.3.8 onwards, before 8.4.0 */
1081 peer = DRBD_MAX_BIO_SIZE;
1084 new = min_t(int, local, peer);
1086 if (mdev->state.role == R_PRIMARY && new < now)
1087 dev_err(DEV, "ASSERT FAILED new < now; (%d < %d)\n", new, now);
1090 dev_info(DEV, "max BIO size = %u\n", new);
1092 drbd_setup_queue_param(mdev, new);
1095 /* Starts the worker thread */
1096 static void conn_reconfig_start(struct drbd_tconn *tconn)
1098 drbd_thread_start(&tconn->worker);
1099 conn_flush_workqueue(tconn);
1102 /* if still unconfigured, stops worker again. */
1103 static void conn_reconfig_done(struct drbd_tconn *tconn)
1106 spin_lock_irq(&tconn->req_lock);
1107 stop_threads = conn_all_vols_unconf(tconn) &&
1108 tconn->cstate == C_STANDALONE;
1109 spin_unlock_irq(&tconn->req_lock);
1111 /* asender is implicitly stopped by receiver
1112 * in conn_disconnect() */
1113 drbd_thread_stop(&tconn->receiver);
1114 drbd_thread_stop(&tconn->worker);
1118 /* Make sure IO is suspended before calling this function(). */
1119 static void drbd_suspend_al(struct drbd_conf *mdev)
1123 if (!lc_try_lock(mdev->act_log)) {
1124 dev_warn(DEV, "Failed to lock al in drbd_suspend_al()\n");
1128 drbd_al_shrink(mdev);
1129 spin_lock_irq(&mdev->tconn->req_lock);
1130 if (mdev->state.conn < C_CONNECTED)
1131 s = !test_and_set_bit(AL_SUSPENDED, &mdev->flags);
1132 spin_unlock_irq(&mdev->tconn->req_lock);
1133 lc_unlock(mdev->act_log);
1136 dev_info(DEV, "Suspended AL updates\n");
1140 static bool should_set_defaults(struct genl_info *info)
1142 unsigned flags = ((struct drbd_genlmsghdr*)info->userhdr)->flags;
1143 return 0 != (flags & DRBD_GENL_F_SET_DEFAULTS);
1146 static void enforce_disk_conf_limits(struct disk_conf *dc)
1148 if (dc->al_extents < DRBD_AL_EXTENTS_MIN)
1149 dc->al_extents = DRBD_AL_EXTENTS_MIN;
1150 if (dc->al_extents > DRBD_AL_EXTENTS_MAX)
1151 dc->al_extents = DRBD_AL_EXTENTS_MAX;
1153 if (dc->c_plan_ahead > DRBD_C_PLAN_AHEAD_MAX)
1154 dc->c_plan_ahead = DRBD_C_PLAN_AHEAD_MAX;
1157 int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info)
1159 enum drbd_ret_code retcode;
1160 struct drbd_conf *mdev;
1161 struct disk_conf *new_disk_conf, *old_disk_conf;
1162 struct fifo_buffer *old_plan = NULL, *new_plan = NULL;
1165 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1166 if (!adm_ctx.reply_skb)
1168 if (retcode != NO_ERROR)
1171 mdev = adm_ctx.mdev;
1173 /* we also need a disk
1174 * to change the options on */
1175 if (!get_ldev(mdev)) {
1176 retcode = ERR_NO_DISK;
1180 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
1181 if (!new_disk_conf) {
1182 retcode = ERR_NOMEM;
1186 mutex_lock(&mdev->tconn->conf_update);
1187 old_disk_conf = mdev->ldev->disk_conf;
1188 *new_disk_conf = *old_disk_conf;
1189 if (should_set_defaults(info))
1190 set_disk_conf_defaults(new_disk_conf);
1192 err = disk_conf_from_attrs_for_change(new_disk_conf, info);
1193 if (err && err != -ENOMSG) {
1194 retcode = ERR_MANDATORY_TAG;
1195 drbd_msg_put_info(from_attrs_err_to_txt(err));
1198 if (!expect(new_disk_conf->resync_rate >= 1))
1199 new_disk_conf->resync_rate = 1;
1201 enforce_disk_conf_limits(new_disk_conf);
1203 fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
1204 if (fifo_size != mdev->rs_plan_s->size) {
1205 new_plan = fifo_alloc(fifo_size);
1207 dev_err(DEV, "kmalloc of fifo_buffer failed");
1208 retcode = ERR_NOMEM;
1213 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
1214 drbd_al_shrink(mdev);
1215 err = drbd_check_al_size(mdev, new_disk_conf);
1216 lc_unlock(mdev->act_log);
1217 wake_up(&mdev->al_wait);
1220 retcode = ERR_NOMEM;
1224 write_lock_irq(&global_state_lock);
1225 retcode = drbd_resync_after_valid(mdev, new_disk_conf->resync_after);
1226 if (retcode == NO_ERROR) {
1227 rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
1228 drbd_resync_after_changed(mdev);
1230 write_unlock_irq(&global_state_lock);
1232 if (retcode != NO_ERROR)
1236 old_plan = mdev->rs_plan_s;
1237 rcu_assign_pointer(mdev->rs_plan_s, new_plan);
1240 mutex_unlock(&mdev->tconn->conf_update);
1242 if (new_disk_conf->al_updates)
1243 mdev->ldev->md.flags &= MDF_AL_DISABLED;
1245 mdev->ldev->md.flags |= MDF_AL_DISABLED;
1247 drbd_bump_write_ordering(mdev->tconn, WO_bdev_flush);
1251 if (mdev->state.conn >= C_CONNECTED)
1252 drbd_send_sync_param(mdev);
1255 kfree(old_disk_conf);
1257 mod_timer(&mdev->request_timer, jiffies + HZ);
1261 mutex_unlock(&mdev->tconn->conf_update);
1263 kfree(new_disk_conf);
1268 drbd_adm_finish(info, retcode);
1272 int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info)
1274 struct drbd_conf *mdev;
1276 enum drbd_ret_code retcode;
1277 enum determine_dev_size dd;
1278 sector_t max_possible_sectors;
1279 sector_t min_md_device_sectors;
1280 struct drbd_backing_dev *nbc = NULL; /* new_backing_conf */
1281 struct disk_conf *new_disk_conf = NULL;
1282 struct block_device *bdev;
1283 struct lru_cache *resync_lru = NULL;
1284 struct fifo_buffer *new_plan = NULL;
1285 union drbd_state ns, os;
1286 enum drbd_state_rv rv;
1287 struct net_conf *nc;
1289 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1290 if (!adm_ctx.reply_skb)
1292 if (retcode != NO_ERROR)
1295 mdev = adm_ctx.mdev;
1296 conn_reconfig_start(mdev->tconn);
1298 /* if you want to reconfigure, please tear down first */
1299 if (mdev->state.disk > D_DISKLESS) {
1300 retcode = ERR_DISK_CONFIGURED;
1303 /* It may just now have detached because of IO error. Make sure
1304 * drbd_ldev_destroy is done already, we may end up here very fast,
1305 * e.g. if someone calls attach from the on-io-error handler,
1306 * to realize a "hot spare" feature (not that I'd recommend that) */
1307 wait_event(mdev->misc_wait, !atomic_read(&mdev->local_cnt));
1309 /* make sure there is no leftover from previous force-detach attempts */
1310 clear_bit(FORCE_DETACH, &mdev->flags);
1312 /* and no leftover from previously aborted resync or verify, either */
1314 mdev->rs_failed = 0;
1315 atomic_set(&mdev->rs_pending_cnt, 0);
1317 /* allocation not in the IO path, drbdsetup context */
1318 nbc = kzalloc(sizeof(struct drbd_backing_dev), GFP_KERNEL);
1320 retcode = ERR_NOMEM;
1323 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
1324 if (!new_disk_conf) {
1325 retcode = ERR_NOMEM;
1328 nbc->disk_conf = new_disk_conf;
1330 set_disk_conf_defaults(new_disk_conf);
1331 err = disk_conf_from_attrs(new_disk_conf, info);
1333 retcode = ERR_MANDATORY_TAG;
1334 drbd_msg_put_info(from_attrs_err_to_txt(err));
1338 enforce_disk_conf_limits(new_disk_conf);
1340 new_plan = fifo_alloc((new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ);
1342 retcode = ERR_NOMEM;
1346 if (new_disk_conf->meta_dev_idx < DRBD_MD_INDEX_FLEX_INT) {
1347 retcode = ERR_MD_IDX_INVALID;
1352 nc = rcu_dereference(mdev->tconn->net_conf);
1354 if (new_disk_conf->fencing == FP_STONITH && nc->wire_protocol == DRBD_PROT_A) {
1356 retcode = ERR_STONITH_AND_PROT_A;
1362 bdev = blkdev_get_by_path(new_disk_conf->backing_dev,
1363 FMODE_READ | FMODE_WRITE | FMODE_EXCL, mdev);
1365 dev_err(DEV, "open(\"%s\") failed with %ld\n", new_disk_conf->backing_dev,
1367 retcode = ERR_OPEN_DISK;
1370 nbc->backing_bdev = bdev;
1373 * meta_dev_idx >= 0: external fixed size, possibly multiple
1374 * drbd sharing one meta device. TODO in that case, paranoia
1375 * check that [md_bdev, meta_dev_idx] is not yet used by some
1376 * other drbd minor! (if you use drbd.conf + drbdadm, that
1377 * should check it for you already; but if you don't, or
1378 * someone fooled it, we need to double check here)
1380 bdev = blkdev_get_by_path(new_disk_conf->meta_dev,
1381 FMODE_READ | FMODE_WRITE | FMODE_EXCL,
1382 (new_disk_conf->meta_dev_idx < 0) ?
1383 (void *)mdev : (void *)drbd_m_holder);
1385 dev_err(DEV, "open(\"%s\") failed with %ld\n", new_disk_conf->meta_dev,
1387 retcode = ERR_OPEN_MD_DISK;
1390 nbc->md_bdev = bdev;
1392 if ((nbc->backing_bdev == nbc->md_bdev) !=
1393 (new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_INTERNAL ||
1394 new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_FLEX_INT)) {
1395 retcode = ERR_MD_IDX_INVALID;
1399 resync_lru = lc_create("resync", drbd_bm_ext_cache,
1400 1, 61, sizeof(struct bm_extent),
1401 offsetof(struct bm_extent, lce));
1403 retcode = ERR_NOMEM;
1407 /* RT - for drbd_get_max_capacity() DRBD_MD_INDEX_FLEX_INT */
1408 drbd_md_set_sector_offsets(mdev, nbc);
1410 if (drbd_get_max_capacity(nbc) < new_disk_conf->disk_size) {
1411 dev_err(DEV, "max capacity %llu smaller than disk size %llu\n",
1412 (unsigned long long) drbd_get_max_capacity(nbc),
1413 (unsigned long long) new_disk_conf->disk_size);
1414 retcode = ERR_DISK_TOO_SMALL;
1418 if (new_disk_conf->meta_dev_idx < 0) {
1419 max_possible_sectors = DRBD_MAX_SECTORS_FLEX;
1420 /* at least one MB, otherwise it does not make sense */
1421 min_md_device_sectors = (2<<10);
1423 max_possible_sectors = DRBD_MAX_SECTORS;
1424 min_md_device_sectors = MD_RESERVED_SECT * (new_disk_conf->meta_dev_idx + 1);
1427 if (drbd_get_capacity(nbc->md_bdev) < min_md_device_sectors) {
1428 retcode = ERR_MD_DISK_TOO_SMALL;
1429 dev_warn(DEV, "refusing attach: md-device too small, "
1430 "at least %llu sectors needed for this meta-disk type\n",
1431 (unsigned long long) min_md_device_sectors);
1435 /* Make sure the new disk is big enough
1436 * (we may currently be R_PRIMARY with no local disk...) */
1437 if (drbd_get_max_capacity(nbc) <
1438 drbd_get_capacity(mdev->this_bdev)) {
1439 retcode = ERR_DISK_TOO_SMALL;
1443 nbc->known_size = drbd_get_capacity(nbc->backing_bdev);
1445 if (nbc->known_size > max_possible_sectors) {
1446 dev_warn(DEV, "==> truncating very big lower level device "
1447 "to currently maximum possible %llu sectors <==\n",
1448 (unsigned long long) max_possible_sectors);
1449 if (new_disk_conf->meta_dev_idx >= 0)
1450 dev_warn(DEV, "==>> using internal or flexible "
1451 "meta data may help <<==\n");
1454 drbd_suspend_io(mdev);
1455 /* also wait for the last barrier ack. */
1456 /* FIXME see also https://daiquiri.linbit/cgi-bin/bugzilla/show_bug.cgi?id=171
1457 * We need a way to either ignore barrier acks for barriers sent before a device
1458 * was attached, or a way to wait for all pending barrier acks to come in.
1459 * As barriers are counted per resource,
1460 * we'd need to suspend io on all devices of a resource.
1462 wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_pending_cnt) || drbd_suspended(mdev));
1463 /* and for any other previously queued work */
1464 drbd_flush_workqueue(mdev);
1466 rv = _drbd_request_state(mdev, NS(disk, D_ATTACHING), CS_VERBOSE);
1467 retcode = rv; /* FIXME: Type mismatch. */
1468 drbd_resume_io(mdev);
1469 if (rv < SS_SUCCESS)
1472 if (!get_ldev_if_state(mdev, D_ATTACHING))
1473 goto force_diskless;
1475 drbd_md_set_sector_offsets(mdev, nbc);
1477 if (!mdev->bitmap) {
1478 if (drbd_bm_init(mdev)) {
1479 retcode = ERR_NOMEM;
1480 goto force_diskless_dec;
1484 retcode = drbd_md_read(mdev, nbc);
1485 if (retcode != NO_ERROR)
1486 goto force_diskless_dec;
1488 if (mdev->state.conn < C_CONNECTED &&
1489 mdev->state.role == R_PRIMARY &&
1490 (mdev->ed_uuid & ~((u64)1)) != (nbc->md.uuid[UI_CURRENT] & ~((u64)1))) {
1491 dev_err(DEV, "Can only attach to data with current UUID=%016llX\n",
1492 (unsigned long long)mdev->ed_uuid);
1493 retcode = ERR_DATA_NOT_CURRENT;
1494 goto force_diskless_dec;
1497 /* Since we are diskless, fix the activity log first... */
1498 if (drbd_check_al_size(mdev, new_disk_conf)) {
1499 retcode = ERR_NOMEM;
1500 goto force_diskless_dec;
1503 /* Prevent shrinking of consistent devices ! */
1504 if (drbd_md_test_flag(nbc, MDF_CONSISTENT) &&
1505 drbd_new_dev_size(mdev, nbc, nbc->disk_conf->disk_size, 0) < nbc->md.la_size_sect) {
1506 dev_warn(DEV, "refusing to truncate a consistent device\n");
1507 retcode = ERR_DISK_TOO_SMALL;
1508 goto force_diskless_dec;
1511 /* Reset the "barriers don't work" bits here, then force meta data to
1512 * be written, to ensure we determine if barriers are supported. */
1513 if (new_disk_conf->md_flushes)
1514 clear_bit(MD_NO_FUA, &mdev->flags);
1516 set_bit(MD_NO_FUA, &mdev->flags);
1518 /* Point of no return reached.
1519 * Devices and memory are no longer released by error cleanup below.
1520 * now mdev takes over responsibility, and the state engine should
1521 * clean it up somewhere. */
1522 D_ASSERT(mdev->ldev == NULL);
1524 mdev->resync = resync_lru;
1525 mdev->rs_plan_s = new_plan;
1528 new_disk_conf = NULL;
1531 drbd_bump_write_ordering(mdev->tconn, WO_bdev_flush);
1533 if (drbd_md_test_flag(mdev->ldev, MDF_CRASHED_PRIMARY))
1534 set_bit(CRASHED_PRIMARY, &mdev->flags);
1536 clear_bit(CRASHED_PRIMARY, &mdev->flags);
1538 if (drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) &&
1539 !(mdev->state.role == R_PRIMARY && mdev->tconn->susp_nod))
1540 set_bit(CRASHED_PRIMARY, &mdev->flags);
1547 drbd_reconsider_max_bio_size(mdev);
1549 /* If I am currently not R_PRIMARY,
1550 * but meta data primary indicator is set,
1551 * I just now recover from a hard crash,
1552 * and have been R_PRIMARY before that crash.
1554 * Now, if I had no connection before that crash
1555 * (have been degraded R_PRIMARY), chances are that
1556 * I won't find my peer now either.
1558 * In that case, and _only_ in that case,
1559 * we use the degr-wfc-timeout instead of the default,
1560 * so we can automatically recover from a crash of a
1561 * degraded but active "cluster" after a certain timeout.
1563 clear_bit(USE_DEGR_WFC_T, &mdev->flags);
1564 if (mdev->state.role != R_PRIMARY &&
1565 drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) &&
1566 !drbd_md_test_flag(mdev->ldev, MDF_CONNECTED_IND))
1567 set_bit(USE_DEGR_WFC_T, &mdev->flags);
1569 dd = drbd_determine_dev_size(mdev, 0);
1570 if (dd == dev_size_error) {
1571 retcode = ERR_NOMEM_BITMAP;
1572 goto force_diskless_dec;
1573 } else if (dd == grew)
1574 set_bit(RESYNC_AFTER_NEG, &mdev->flags);
1576 if (drbd_md_test_flag(mdev->ldev, MDF_FULL_SYNC) ||
1577 (test_bit(CRASHED_PRIMARY, &mdev->flags) &&
1578 drbd_md_test_flag(mdev->ldev, MDF_AL_DISABLED))) {
1579 dev_info(DEV, "Assuming that all blocks are out of sync "
1580 "(aka FullSync)\n");
1581 if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write,
1582 "set_n_write from attaching", BM_LOCKED_MASK)) {
1583 retcode = ERR_IO_MD_DISK;
1584 goto force_diskless_dec;
1587 if (drbd_bitmap_io(mdev, &drbd_bm_read,
1588 "read from attaching", BM_LOCKED_MASK)) {
1589 retcode = ERR_IO_MD_DISK;
1590 goto force_diskless_dec;
1594 if (_drbd_bm_total_weight(mdev) == drbd_bm_bits(mdev))
1595 drbd_suspend_al(mdev); /* IO is still suspended here... */
1597 spin_lock_irq(&mdev->tconn->req_lock);
1598 os = drbd_read_state(mdev);
1600 /* If MDF_CONSISTENT is not set go into inconsistent state,
1601 otherwise investigate MDF_WasUpToDate...
1602 If MDF_WAS_UP_TO_DATE is not set go into D_OUTDATED disk state,
1603 otherwise into D_CONSISTENT state.
1605 if (drbd_md_test_flag(mdev->ldev, MDF_CONSISTENT)) {
1606 if (drbd_md_test_flag(mdev->ldev, MDF_WAS_UP_TO_DATE))
1607 ns.disk = D_CONSISTENT;
1609 ns.disk = D_OUTDATED;
1611 ns.disk = D_INCONSISTENT;
1614 if (drbd_md_test_flag(mdev->ldev, MDF_PEER_OUT_DATED))
1615 ns.pdsk = D_OUTDATED;
1618 if (ns.disk == D_CONSISTENT &&
1619 (ns.pdsk == D_OUTDATED || rcu_dereference(mdev->ldev->disk_conf)->fencing == FP_DONT_CARE))
1620 ns.disk = D_UP_TO_DATE;
1622 /* All tests on MDF_PRIMARY_IND, MDF_CONNECTED_IND,
1623 MDF_CONSISTENT and MDF_WAS_UP_TO_DATE must happen before
1624 this point, because drbd_request_state() modifies these
1627 if (rcu_dereference(mdev->ldev->disk_conf)->al_updates)
1628 mdev->ldev->md.flags &= MDF_AL_DISABLED;
1630 mdev->ldev->md.flags |= MDF_AL_DISABLED;
1634 /* In case we are C_CONNECTED postpone any decision on the new disk
1635 state after the negotiation phase. */
1636 if (mdev->state.conn == C_CONNECTED) {
1637 mdev->new_state_tmp.i = ns.i;
1639 ns.disk = D_NEGOTIATING;
1641 /* We expect to receive up-to-date UUIDs soon.
1642 To avoid a race in receive_state, free p_uuid while
1643 holding req_lock. I.e. atomic with the state change */
1644 kfree(mdev->p_uuid);
1645 mdev->p_uuid = NULL;
1648 rv = _drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
1649 spin_unlock_irq(&mdev->tconn->req_lock);
1651 if (rv < SS_SUCCESS)
1652 goto force_diskless_dec;
1654 mod_timer(&mdev->request_timer, jiffies + HZ);
1656 if (mdev->state.role == R_PRIMARY)
1657 mdev->ldev->md.uuid[UI_CURRENT] |= (u64)1;
1659 mdev->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
1661 drbd_md_mark_dirty(mdev);
1664 kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
1666 conn_reconfig_done(mdev->tconn);
1667 drbd_adm_finish(info, retcode);
1673 drbd_force_state(mdev, NS(disk, D_DISKLESS));
1676 conn_reconfig_done(mdev->tconn);
1678 if (nbc->backing_bdev)
1679 blkdev_put(nbc->backing_bdev,
1680 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1682 blkdev_put(nbc->md_bdev,
1683 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1686 kfree(new_disk_conf);
1687 lc_destroy(resync_lru);
1691 drbd_adm_finish(info, retcode);
1695 static int adm_detach(struct drbd_conf *mdev, int force)
1697 enum drbd_state_rv retcode;
1701 set_bit(FORCE_DETACH, &mdev->flags);
1702 drbd_force_state(mdev, NS(disk, D_FAILED));
1703 retcode = SS_SUCCESS;
1707 drbd_suspend_io(mdev); /* so no-one is stuck in drbd_al_begin_io */
1708 drbd_md_get_buffer(mdev); /* make sure there is no in-flight meta-data IO */
1709 retcode = drbd_request_state(mdev, NS(disk, D_FAILED));
1710 drbd_md_put_buffer(mdev);
1711 /* D_FAILED will transition to DISKLESS. */
1712 ret = wait_event_interruptible(mdev->misc_wait,
1713 mdev->state.disk != D_FAILED);
1714 drbd_resume_io(mdev);
1715 if ((int)retcode == (int)SS_IS_DISKLESS)
1716 retcode = SS_NOTHING_TO_DO;
1723 /* Detaching the disk is a process in multiple stages. First we need to lock
1724 * out application IO, in-flight IO, IO stuck in drbd_al_begin_io.
1725 * Then we transition to D_DISKLESS, and wait for put_ldev() to return all
1726 * internal references as well.
1727 * Only then we have finally detached. */
1728 int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info)
1730 enum drbd_ret_code retcode;
1731 struct detach_parms parms = { };
1734 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1735 if (!adm_ctx.reply_skb)
1737 if (retcode != NO_ERROR)
1740 if (info->attrs[DRBD_NLA_DETACH_PARMS]) {
1741 err = detach_parms_from_attrs(&parms, info);
1743 retcode = ERR_MANDATORY_TAG;
1744 drbd_msg_put_info(from_attrs_err_to_txt(err));
1749 retcode = adm_detach(adm_ctx.mdev, parms.force_detach);
1751 drbd_adm_finish(info, retcode);
1755 static bool conn_resync_running(struct drbd_tconn *tconn)
1757 struct drbd_conf *mdev;
1762 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1763 if (mdev->state.conn == C_SYNC_SOURCE ||
1764 mdev->state.conn == C_SYNC_TARGET ||
1765 mdev->state.conn == C_PAUSED_SYNC_S ||
1766 mdev->state.conn == C_PAUSED_SYNC_T) {
1776 static bool conn_ov_running(struct drbd_tconn *tconn)
1778 struct drbd_conf *mdev;
1783 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1784 if (mdev->state.conn == C_VERIFY_S ||
1785 mdev->state.conn == C_VERIFY_T) {
1795 static enum drbd_ret_code
1796 _check_net_options(struct drbd_tconn *tconn, struct net_conf *old_conf, struct net_conf *new_conf)
1798 struct drbd_conf *mdev;
1801 if (old_conf && tconn->cstate == C_WF_REPORT_PARAMS && tconn->agreed_pro_version < 100) {
1802 if (new_conf->wire_protocol != old_conf->wire_protocol)
1803 return ERR_NEED_APV_100;
1805 if (new_conf->two_primaries != old_conf->two_primaries)
1806 return ERR_NEED_APV_100;
1808 if (!new_conf->integrity_alg != !old_conf->integrity_alg)
1809 return ERR_NEED_APV_100;
1811 if (strcmp(new_conf->integrity_alg, old_conf->integrity_alg))
1812 return ERR_NEED_APV_100;
1815 if (!new_conf->two_primaries &&
1816 conn_highest_role(tconn) == R_PRIMARY &&
1817 conn_highest_peer(tconn) == R_PRIMARY)
1818 return ERR_NEED_ALLOW_TWO_PRI;
1820 if (new_conf->two_primaries &&
1821 (new_conf->wire_protocol != DRBD_PROT_C))
1822 return ERR_NOT_PROTO_C;
1824 idr_for_each_entry(&tconn->volumes, mdev, i) {
1825 if (get_ldev(mdev)) {
1826 enum drbd_fencing_p fp = rcu_dereference(mdev->ldev->disk_conf)->fencing;
1828 if (new_conf->wire_protocol == DRBD_PROT_A && fp == FP_STONITH)
1829 return ERR_STONITH_AND_PROT_A;
1831 if (mdev->state.role == R_PRIMARY && new_conf->discard_my_data)
1835 if (new_conf->on_congestion != OC_BLOCK && new_conf->wire_protocol != DRBD_PROT_A)
1836 return ERR_CONG_NOT_PROTO_A;
1841 static enum drbd_ret_code
1842 check_net_options(struct drbd_tconn *tconn, struct net_conf *new_conf)
1844 static enum drbd_ret_code rv;
1845 struct drbd_conf *mdev;
1849 rv = _check_net_options(tconn, rcu_dereference(tconn->net_conf), new_conf);
1852 /* tconn->volumes protected by genl_lock() here */
1853 idr_for_each_entry(&tconn->volumes, mdev, i) {
1854 if (!mdev->bitmap) {
1855 if(drbd_bm_init(mdev))
1864 struct crypto_hash *verify_tfm;
1865 struct crypto_hash *csums_tfm;
1866 struct crypto_hash *cram_hmac_tfm;
1867 struct crypto_hash *integrity_tfm;
1871 alloc_hash(struct crypto_hash **tfm, char *tfm_name, int err_alg)
1876 *tfm = crypto_alloc_hash(tfm_name, 0, CRYPTO_ALG_ASYNC);
1885 static enum drbd_ret_code
1886 alloc_crypto(struct crypto *crypto, struct net_conf *new_conf)
1888 char hmac_name[CRYPTO_MAX_ALG_NAME];
1889 enum drbd_ret_code rv;
1891 rv = alloc_hash(&crypto->csums_tfm, new_conf->csums_alg,
1895 rv = alloc_hash(&crypto->verify_tfm, new_conf->verify_alg,
1899 rv = alloc_hash(&crypto->integrity_tfm, new_conf->integrity_alg,
1903 if (new_conf->cram_hmac_alg[0] != 0) {
1904 snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)",
1905 new_conf->cram_hmac_alg);
1907 rv = alloc_hash(&crypto->cram_hmac_tfm, hmac_name,
1914 static void free_crypto(struct crypto *crypto)
1916 crypto_free_hash(crypto->cram_hmac_tfm);
1917 crypto_free_hash(crypto->integrity_tfm);
1918 crypto_free_hash(crypto->csums_tfm);
1919 crypto_free_hash(crypto->verify_tfm);
1922 int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info)
1924 enum drbd_ret_code retcode;
1925 struct drbd_tconn *tconn;
1926 struct net_conf *old_conf, *new_conf = NULL;
1928 int ovr; /* online verify running */
1929 int rsr; /* re-sync running */
1930 struct crypto crypto = { };
1932 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONNECTION);
1933 if (!adm_ctx.reply_skb)
1935 if (retcode != NO_ERROR)
1938 tconn = adm_ctx.tconn;
1940 new_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
1942 retcode = ERR_NOMEM;
1946 conn_reconfig_start(tconn);
1948 mutex_lock(&tconn->data.mutex);
1949 mutex_lock(&tconn->conf_update);
1950 old_conf = tconn->net_conf;
1953 drbd_msg_put_info("net conf missing, try connect");
1954 retcode = ERR_INVALID_REQUEST;
1958 *new_conf = *old_conf;
1959 if (should_set_defaults(info))
1960 set_net_conf_defaults(new_conf);
1962 err = net_conf_from_attrs_for_change(new_conf, info);
1963 if (err && err != -ENOMSG) {
1964 retcode = ERR_MANDATORY_TAG;
1965 drbd_msg_put_info(from_attrs_err_to_txt(err));
1969 retcode = check_net_options(tconn, new_conf);
1970 if (retcode != NO_ERROR)
1973 /* re-sync running */
1974 rsr = conn_resync_running(tconn);
1975 if (rsr && strcmp(new_conf->csums_alg, old_conf->csums_alg)) {
1976 retcode = ERR_CSUMS_RESYNC_RUNNING;
1980 /* online verify running */
1981 ovr = conn_ov_running(tconn);
1982 if (ovr && strcmp(new_conf->verify_alg, old_conf->verify_alg)) {
1983 retcode = ERR_VERIFY_RUNNING;
1987 retcode = alloc_crypto(&crypto, new_conf);
1988 if (retcode != NO_ERROR)
1991 rcu_assign_pointer(tconn->net_conf, new_conf);
1994 crypto_free_hash(tconn->csums_tfm);
1995 tconn->csums_tfm = crypto.csums_tfm;
1996 crypto.csums_tfm = NULL;
1999 crypto_free_hash(tconn->verify_tfm);
2000 tconn->verify_tfm = crypto.verify_tfm;
2001 crypto.verify_tfm = NULL;
2004 crypto_free_hash(tconn->integrity_tfm);
2005 tconn->integrity_tfm = crypto.integrity_tfm;
2006 if (tconn->cstate >= C_WF_REPORT_PARAMS && tconn->agreed_pro_version >= 100)
2007 /* Do this without trying to take tconn->data.mutex again. */
2008 __drbd_send_protocol(tconn, P_PROTOCOL_UPDATE);
2010 crypto_free_hash(tconn->cram_hmac_tfm);
2011 tconn->cram_hmac_tfm = crypto.cram_hmac_tfm;
2013 mutex_unlock(&tconn->conf_update);
2014 mutex_unlock(&tconn->data.mutex);
2018 if (tconn->cstate >= C_WF_REPORT_PARAMS)
2019 drbd_send_sync_param(minor_to_mdev(conn_lowest_minor(tconn)));
2024 mutex_unlock(&tconn->conf_update);
2025 mutex_unlock(&tconn->data.mutex);
2026 free_crypto(&crypto);
2029 conn_reconfig_done(tconn);
2031 drbd_adm_finish(info, retcode);
2035 int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info)
2037 struct drbd_conf *mdev;
2038 struct net_conf *old_conf, *new_conf = NULL;
2039 struct crypto crypto = { };
2040 struct drbd_tconn *tconn;
2041 enum drbd_ret_code retcode;
2045 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
2047 if (!adm_ctx.reply_skb)
2049 if (retcode != NO_ERROR)
2051 if (!(adm_ctx.my_addr && adm_ctx.peer_addr)) {
2052 drbd_msg_put_info("connection endpoint(s) missing");
2053 retcode = ERR_INVALID_REQUEST;
2057 /* No need for _rcu here. All reconfiguration is
2058 * strictly serialized on genl_lock(). We are protected against
2059 * concurrent reconfiguration/addition/deletion */
2060 list_for_each_entry(tconn, &drbd_tconns, all_tconn) {
2061 if (nla_len(adm_ctx.my_addr) == tconn->my_addr_len &&
2062 !memcmp(nla_data(adm_ctx.my_addr), &tconn->my_addr, tconn->my_addr_len)) {
2063 retcode = ERR_LOCAL_ADDR;
2067 if (nla_len(adm_ctx.peer_addr) == tconn->peer_addr_len &&
2068 !memcmp(nla_data(adm_ctx.peer_addr), &tconn->peer_addr, tconn->peer_addr_len)) {
2069 retcode = ERR_PEER_ADDR;
2074 tconn = adm_ctx.tconn;
2075 conn_reconfig_start(tconn);
2077 if (tconn->cstate > C_STANDALONE) {
2078 retcode = ERR_NET_CONFIGURED;
2082 /* allocation not in the IO path, drbdsetup / netlink process context */
2083 new_conf = kzalloc(sizeof(*new_conf), GFP_KERNEL);
2085 retcode = ERR_NOMEM;
2089 set_net_conf_defaults(new_conf);
2091 err = net_conf_from_attrs(new_conf, info);
2092 if (err && err != -ENOMSG) {
2093 retcode = ERR_MANDATORY_TAG;
2094 drbd_msg_put_info(from_attrs_err_to_txt(err));
2098 retcode = check_net_options(tconn, new_conf);
2099 if (retcode != NO_ERROR)
2102 retcode = alloc_crypto(&crypto, new_conf);
2103 if (retcode != NO_ERROR)
2106 ((char *)new_conf->shared_secret)[SHARED_SECRET_MAX-1] = 0;
2108 conn_flush_workqueue(tconn);
2110 mutex_lock(&tconn->conf_update);
2111 old_conf = tconn->net_conf;
2113 retcode = ERR_NET_CONFIGURED;
2114 mutex_unlock(&tconn->conf_update);
2117 rcu_assign_pointer(tconn->net_conf, new_conf);
2119 conn_free_crypto(tconn);
2120 tconn->cram_hmac_tfm = crypto.cram_hmac_tfm;
2121 tconn->integrity_tfm = crypto.integrity_tfm;
2122 tconn->csums_tfm = crypto.csums_tfm;
2123 tconn->verify_tfm = crypto.verify_tfm;
2125 tconn->my_addr_len = nla_len(adm_ctx.my_addr);
2126 memcpy(&tconn->my_addr, nla_data(adm_ctx.my_addr), tconn->my_addr_len);
2127 tconn->peer_addr_len = nla_len(adm_ctx.peer_addr);
2128 memcpy(&tconn->peer_addr, nla_data(adm_ctx.peer_addr), tconn->peer_addr_len);
2130 mutex_unlock(&tconn->conf_update);
2133 idr_for_each_entry(&tconn->volumes, mdev, i) {
2139 retcode = conn_request_state(tconn, NS(conn, C_UNCONNECTED), CS_VERBOSE);
2141 conn_reconfig_done(tconn);
2142 drbd_adm_finish(info, retcode);
2146 free_crypto(&crypto);
2149 conn_reconfig_done(tconn);
2151 drbd_adm_finish(info, retcode);
2155 static enum drbd_state_rv conn_try_disconnect(struct drbd_tconn *tconn, bool force)
2157 enum drbd_state_rv rv;
2159 rv = conn_request_state(tconn, NS(conn, C_DISCONNECTING),
2160 force ? CS_HARD : 0);
2163 case SS_NOTHING_TO_DO:
2165 case SS_ALREADY_STANDALONE:
2167 case SS_PRIMARY_NOP:
2168 /* Our state checking code wants to see the peer outdated. */
2169 rv = conn_request_state(tconn, NS2(conn, C_DISCONNECTING,
2170 pdsk, D_OUTDATED), CS_VERBOSE);
2172 case SS_CW_FAILED_BY_PEER:
2173 /* The peer probably wants to see us outdated. */
2174 rv = conn_request_state(tconn, NS2(conn, C_DISCONNECTING,
2175 disk, D_OUTDATED), 0);
2176 if (rv == SS_IS_DISKLESS || rv == SS_LOWER_THAN_OUTDATED) {
2177 rv = conn_request_state(tconn, NS(conn, C_DISCONNECTING),
2182 /* no special handling necessary */
2185 if (rv >= SS_SUCCESS) {
2186 enum drbd_state_rv rv2;
2187 /* No one else can reconfigure the network while I am here.
2188 * The state handling only uses drbd_thread_stop_nowait(),
2189 * we want to really wait here until the receiver is no more.
2191 drbd_thread_stop(&adm_ctx.tconn->receiver);
2193 /* Race breaker. This additional state change request may be
2194 * necessary, if this was a forced disconnect during a receiver
2195 * restart. We may have "killed" the receiver thread just
2196 * after drbdd_init() returned. Typically, we should be
2197 * C_STANDALONE already, now, and this becomes a no-op.
2199 rv2 = conn_request_state(tconn, NS(conn, C_STANDALONE),
2200 CS_VERBOSE | CS_HARD);
2201 if (rv2 < SS_SUCCESS)
2203 "unexpected rv2=%d in conn_try_disconnect()\n",
2209 int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info)
2211 struct disconnect_parms parms;
2212 struct drbd_tconn *tconn;
2213 enum drbd_state_rv rv;
2214 enum drbd_ret_code retcode;
2217 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONNECTION);
2218 if (!adm_ctx.reply_skb)
2220 if (retcode != NO_ERROR)
2223 tconn = adm_ctx.tconn;
2224 memset(&parms, 0, sizeof(parms));
2225 if (info->attrs[DRBD_NLA_DISCONNECT_PARMS]) {
2226 err = disconnect_parms_from_attrs(&parms, info);
2228 retcode = ERR_MANDATORY_TAG;
2229 drbd_msg_put_info(from_attrs_err_to_txt(err));
2234 rv = conn_try_disconnect(tconn, parms.force_disconnect);
2235 if (rv < SS_SUCCESS)
2236 retcode = rv; /* FIXME: Type mismatch. */
2240 drbd_adm_finish(info, retcode);
2244 void resync_after_online_grow(struct drbd_conf *mdev)
2246 int iass; /* I am sync source */
2248 dev_info(DEV, "Resync of new storage after online grow\n");
2249 if (mdev->state.role != mdev->state.peer)
2250 iass = (mdev->state.role == R_PRIMARY);
2252 iass = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags);
2255 drbd_start_resync(mdev, C_SYNC_SOURCE);
2257 _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE + CS_SERIALIZE);
2260 int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info)
2262 struct disk_conf *old_disk_conf, *new_disk_conf = NULL;
2263 struct resize_parms rs;
2264 struct drbd_conf *mdev;
2265 enum drbd_ret_code retcode;
2266 enum determine_dev_size dd;
2267 enum dds_flags ddsf;
2271 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2272 if (!adm_ctx.reply_skb)
2274 if (retcode != NO_ERROR)
2277 memset(&rs, 0, sizeof(struct resize_parms));
2278 if (info->attrs[DRBD_NLA_RESIZE_PARMS]) {
2279 err = resize_parms_from_attrs(&rs, info);
2281 retcode = ERR_MANDATORY_TAG;
2282 drbd_msg_put_info(from_attrs_err_to_txt(err));
2287 mdev = adm_ctx.mdev;
2288 if (mdev->state.conn > C_CONNECTED) {
2289 retcode = ERR_RESIZE_RESYNC;
2293 if (mdev->state.role == R_SECONDARY &&
2294 mdev->state.peer == R_SECONDARY) {
2295 retcode = ERR_NO_PRIMARY;
2299 if (!get_ldev(mdev)) {
2300 retcode = ERR_NO_DISK;
2304 if (rs.no_resync && mdev->tconn->agreed_pro_version < 93) {
2305 retcode = ERR_NEED_APV_93;
2310 u_size = rcu_dereference(mdev->ldev->disk_conf)->disk_size;
2312 if (u_size != (sector_t)rs.resize_size) {
2313 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
2314 if (!new_disk_conf) {
2315 retcode = ERR_NOMEM;
2320 if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev))
2321 mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev);
2323 if (new_disk_conf) {
2324 mutex_lock(&mdev->tconn->conf_update);
2325 old_disk_conf = mdev->ldev->disk_conf;
2326 *new_disk_conf = *old_disk_conf;
2327 new_disk_conf->disk_size = (sector_t)rs.resize_size;
2328 rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
2329 mutex_unlock(&mdev->tconn->conf_update);
2331 kfree(old_disk_conf);
2334 ddsf = (rs.resize_force ? DDSF_FORCED : 0) | (rs.no_resync ? DDSF_NO_RESYNC : 0);
2335 dd = drbd_determine_dev_size(mdev, ddsf);
2338 if (dd == dev_size_error) {
2339 retcode = ERR_NOMEM_BITMAP;
2343 if (mdev->state.conn == C_CONNECTED) {
2345 set_bit(RESIZE_PENDING, &mdev->flags);
2347 drbd_send_uuids(mdev);
2348 drbd_send_sizes(mdev, 1, ddsf);
2352 drbd_adm_finish(info, retcode);
2360 int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info)
2362 enum drbd_ret_code retcode;
2363 struct drbd_tconn *tconn;
2364 struct res_opts res_opts;
2367 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
2368 if (!adm_ctx.reply_skb)
2370 if (retcode != NO_ERROR)
2372 tconn = adm_ctx.tconn;
2374 res_opts = tconn->res_opts;
2375 if (should_set_defaults(info))
2376 set_res_opts_defaults(&res_opts);
2378 err = res_opts_from_attrs(&res_opts, info);
2379 if (err && err != -ENOMSG) {
2380 retcode = ERR_MANDATORY_TAG;
2381 drbd_msg_put_info(from_attrs_err_to_txt(err));
2385 err = set_resource_options(tconn, &res_opts);
2387 retcode = ERR_INVALID_REQUEST;
2389 retcode = ERR_NOMEM;
2393 drbd_adm_finish(info, retcode);
2397 int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info)
2399 struct drbd_conf *mdev;
2400 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2402 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2403 if (!adm_ctx.reply_skb)
2405 if (retcode != NO_ERROR)
2408 mdev = adm_ctx.mdev;
2410 /* If there is still bitmap IO pending, probably because of a previous
2411 * resync just being finished, wait for it before requesting a new resync. */
2412 drbd_suspend_io(mdev);
2413 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
2415 retcode = _drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T), CS_ORDERED);
2417 if (retcode < SS_SUCCESS && retcode != SS_NEED_CONNECTION)
2418 retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T));
2420 while (retcode == SS_NEED_CONNECTION) {
2421 spin_lock_irq(&mdev->tconn->req_lock);
2422 if (mdev->state.conn < C_CONNECTED)
2423 retcode = _drbd_set_state(_NS(mdev, disk, D_INCONSISTENT), CS_VERBOSE, NULL);
2424 spin_unlock_irq(&mdev->tconn->req_lock);
2426 if (retcode != SS_NEED_CONNECTION)
2429 retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T));
2431 drbd_resume_io(mdev);
2434 drbd_adm_finish(info, retcode);
2438 static int drbd_adm_simple_request_state(struct sk_buff *skb, struct genl_info *info,
2439 union drbd_state mask, union drbd_state val)
2441 enum drbd_ret_code retcode;
2443 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2444 if (!adm_ctx.reply_skb)
2446 if (retcode != NO_ERROR)
2449 retcode = drbd_request_state(adm_ctx.mdev, mask, val);
2451 drbd_adm_finish(info, retcode);
2455 static int drbd_bmio_set_susp_al(struct drbd_conf *mdev)
2459 rv = drbd_bmio_set_n_write(mdev);
2460 drbd_suspend_al(mdev);
2464 int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info)
2466 int retcode; /* drbd_ret_code, drbd_state_rv */
2467 struct drbd_conf *mdev;
2469 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2470 if (!adm_ctx.reply_skb)
2472 if (retcode != NO_ERROR)
2475 mdev = adm_ctx.mdev;
2477 /* If there is still bitmap IO pending, probably because of a previous
2478 * resync just being finished, wait for it before requesting a new resync. */
2479 drbd_suspend_io(mdev);
2480 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
2482 retcode = _drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_S), CS_ORDERED);
2483 if (retcode < SS_SUCCESS) {
2484 if (retcode == SS_NEED_CONNECTION && mdev->state.role == R_PRIMARY) {
2485 /* The peer will get a resync upon connect anyways.
2486 * Just make that into a full resync. */
2487 retcode = drbd_request_state(mdev, NS(pdsk, D_INCONSISTENT));
2488 if (retcode >= SS_SUCCESS) {
2489 if (drbd_bitmap_io(mdev, &drbd_bmio_set_susp_al,
2490 "set_n_write from invalidate_peer",
2491 BM_LOCKED_SET_ALLOWED))
2492 retcode = ERR_IO_MD_DISK;
2495 retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_S));
2497 drbd_resume_io(mdev);
2500 drbd_adm_finish(info, retcode);
2504 int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info)
2506 enum drbd_ret_code retcode;
2508 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2509 if (!adm_ctx.reply_skb)
2511 if (retcode != NO_ERROR)
2514 if (drbd_request_state(adm_ctx.mdev, NS(user_isp, 1)) == SS_NOTHING_TO_DO)
2515 retcode = ERR_PAUSE_IS_SET;
2517 drbd_adm_finish(info, retcode);
2521 int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info)
2523 union drbd_dev_state s;
2524 enum drbd_ret_code retcode;
2526 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2527 if (!adm_ctx.reply_skb)
2529 if (retcode != NO_ERROR)
2532 if (drbd_request_state(adm_ctx.mdev, NS(user_isp, 0)) == SS_NOTHING_TO_DO) {
2533 s = adm_ctx.mdev->state;
2534 if (s.conn == C_PAUSED_SYNC_S || s.conn == C_PAUSED_SYNC_T) {
2535 retcode = s.aftr_isp ? ERR_PIC_AFTER_DEP :
2536 s.peer_isp ? ERR_PIC_PEER_DEP : ERR_PAUSE_IS_CLEAR;
2538 retcode = ERR_PAUSE_IS_CLEAR;
2543 drbd_adm_finish(info, retcode);
2547 int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info)
2549 return drbd_adm_simple_request_state(skb, info, NS(susp, 1));
2552 int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info)
2554 struct drbd_conf *mdev;
2555 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2557 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2558 if (!adm_ctx.reply_skb)
2560 if (retcode != NO_ERROR)
2563 mdev = adm_ctx.mdev;
2564 if (test_bit(NEW_CUR_UUID, &mdev->flags)) {
2565 drbd_uuid_new_current(mdev);
2566 clear_bit(NEW_CUR_UUID, &mdev->flags);
2568 drbd_suspend_io(mdev);
2569 retcode = drbd_request_state(mdev, NS3(susp, 0, susp_nod, 0, susp_fen, 0));
2570 if (retcode == SS_SUCCESS) {
2571 if (mdev->state.conn < C_CONNECTED)
2572 tl_clear(mdev->tconn);
2573 if (mdev->state.disk == D_DISKLESS || mdev->state.disk == D_FAILED)
2574 tl_restart(mdev->tconn, FAIL_FROZEN_DISK_IO);
2576 drbd_resume_io(mdev);
2579 drbd_adm_finish(info, retcode);
2583 int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info)
2585 return drbd_adm_simple_request_state(skb, info, NS(disk, D_OUTDATED));
2588 int nla_put_drbd_cfg_context(struct sk_buff *skb, struct drbd_tconn *tconn, unsigned vnr)
2591 nla = nla_nest_start(skb, DRBD_NLA_CFG_CONTEXT);
2593 goto nla_put_failure;
2594 if (vnr != VOLUME_UNSPECIFIED &&
2595 nla_put_u32(skb, T_ctx_volume, vnr))
2596 goto nla_put_failure;
2597 if (nla_put_string(skb, T_ctx_resource_name, tconn->name))
2598 goto nla_put_failure;
2599 if (tconn->my_addr_len &&
2600 nla_put(skb, T_ctx_my_addr, tconn->my_addr_len, &tconn->my_addr))
2601 goto nla_put_failure;
2602 if (tconn->peer_addr_len &&
2603 nla_put(skb, T_ctx_peer_addr, tconn->peer_addr_len, &tconn->peer_addr))
2604 goto nla_put_failure;
2605 nla_nest_end(skb, nla);
2610 nla_nest_cancel(skb, nla);
2614 int nla_put_status_info(struct sk_buff *skb, struct drbd_conf *mdev,
2615 const struct sib_info *sib)
2617 struct state_info *si = NULL; /* for sizeof(si->member); */
2618 struct net_conf *nc;
2622 int exclude_sensitive;
2624 /* If sib != NULL, this is drbd_bcast_event, which anyone can listen
2625 * to. So we better exclude_sensitive information.
2627 * If sib == NULL, this is drbd_adm_get_status, executed synchronously
2628 * in the context of the requesting user process. Exclude sensitive
2629 * information, unless current has superuser.
2631 * NOTE: for drbd_adm_get_status_all(), this is a netlink dump, and
2632 * relies on the current implementation of netlink_dump(), which
2633 * executes the dump callback successively from netlink_recvmsg(),
2634 * always in the context of the receiving process */
2635 exclude_sensitive = sib || !capable(CAP_SYS_ADMIN);
2637 got_ldev = get_ldev(mdev);
2639 /* We need to add connection name and volume number information still.
2640 * Minor number is in drbd_genlmsghdr. */
2641 if (nla_put_drbd_cfg_context(skb, mdev->tconn, mdev->vnr))
2642 goto nla_put_failure;
2644 if (res_opts_to_skb(skb, &mdev->tconn->res_opts, exclude_sensitive))
2645 goto nla_put_failure;
2649 if (disk_conf_to_skb(skb, rcu_dereference(mdev->ldev->disk_conf), exclude_sensitive))
2650 goto nla_put_failure;
2652 nc = rcu_dereference(mdev->tconn->net_conf);
2654 err = net_conf_to_skb(skb, nc, exclude_sensitive);
2657 goto nla_put_failure;
2659 nla = nla_nest_start(skb, DRBD_NLA_STATE_INFO);
2661 goto nla_put_failure;
2662 if (nla_put_u32(skb, T_sib_reason, sib ? sib->sib_reason : SIB_GET_STATUS_REPLY) ||
2663 nla_put_u32(skb, T_current_state, mdev->state.i) ||
2664 nla_put_u64(skb, T_ed_uuid, mdev->ed_uuid) ||
2665 nla_put_u64(skb, T_capacity, drbd_get_capacity(mdev->this_bdev)))
2666 goto nla_put_failure;
2669 if (nla_put_u32(skb, T_disk_flags, mdev->ldev->md.flags) ||
2670 nla_put(skb, T_uuids, sizeof(si->uuids), mdev->ldev->md.uuid) ||
2671 nla_put_u64(skb, T_bits_total, drbd_bm_bits(mdev)) ||
2672 nla_put_u64(skb, T_bits_oos, drbd_bm_total_weight(mdev)))
2673 goto nla_put_failure;
2674 if (C_SYNC_SOURCE <= mdev->state.conn &&
2675 C_PAUSED_SYNC_T >= mdev->state.conn) {
2676 if (nla_put_u64(skb, T_bits_rs_total, mdev->rs_total) ||
2677 nla_put_u64(skb, T_bits_rs_failed, mdev->rs_failed))
2678 goto nla_put_failure;
2683 switch(sib->sib_reason) {
2684 case SIB_SYNC_PROGRESS:
2685 case SIB_GET_STATUS_REPLY:
2687 case SIB_STATE_CHANGE:
2688 if (nla_put_u32(skb, T_prev_state, sib->os.i) ||
2689 nla_put_u32(skb, T_new_state, sib->ns.i))
2690 goto nla_put_failure;
2692 case SIB_HELPER_POST:
2693 if (nla_put_u32(skb, T_helper_exit_code,
2694 sib->helper_exit_code))
2695 goto nla_put_failure;
2697 case SIB_HELPER_PRE:
2698 if (nla_put_string(skb, T_helper, sib->helper_name))
2699 goto nla_put_failure;
2703 nla_nest_end(skb, nla);
2713 int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info)
2715 enum drbd_ret_code retcode;
2718 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2719 if (!adm_ctx.reply_skb)
2721 if (retcode != NO_ERROR)
2724 err = nla_put_status_info(adm_ctx.reply_skb, adm_ctx.mdev, NULL);
2726 nlmsg_free(adm_ctx.reply_skb);
2730 drbd_adm_finish(info, retcode);
2734 int get_one_status(struct sk_buff *skb, struct netlink_callback *cb)
2736 struct drbd_conf *mdev;
2737 struct drbd_genlmsghdr *dh;
2738 struct drbd_tconn *pos = (struct drbd_tconn*)cb->args[0];
2739 struct drbd_tconn *tconn = NULL;
2740 struct drbd_tconn *tmp;
2741 unsigned volume = cb->args[1];
2743 /* Open coded, deferred, iteration:
2744 * list_for_each_entry_safe(tconn, tmp, &drbd_tconns, all_tconn) {
2745 * idr_for_each_entry(&tconn->volumes, mdev, i) {
2749 * where tconn is cb->args[0];
2750 * and i is cb->args[1];
2752 * cb->args[2] indicates if we shall loop over all resources,
2753 * or just dump all volumes of a single resource.
2755 * This may miss entries inserted after this dump started,
2756 * or entries deleted before they are reached.
2758 * We need to make sure the mdev won't disappear while
2759 * we are looking at it, and revalidate our iterators
2760 * on each iteration.
2763 /* synchronize with conn_create()/conn_destroy() */
2765 /* revalidate iterator position */
2766 list_for_each_entry_rcu(tmp, &drbd_tconns, all_tconn) {
2768 /* first iteration */
2780 mdev = idr_get_next(&tconn->volumes, &volume);
2782 /* No more volumes to dump on this tconn.
2783 * Advance tconn iterator. */
2784 pos = list_entry_rcu(tconn->all_tconn.next,
2785 struct drbd_tconn, all_tconn);
2786 /* Did we dump any volume on this tconn yet? */
2788 /* If we reached the end of the list,
2789 * or only a single resource dump was requested,
2791 if (&pos->all_tconn == &drbd_tconns || cb->args[2])
2799 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).pid,
2800 cb->nlh->nlmsg_seq, &drbd_genl_family,
2801 NLM_F_MULTI, DRBD_ADM_GET_STATUS);
2806 /* This is a tconn without a single volume.
2807 * Suprisingly enough, it may have a network
2809 struct net_conf *nc;
2811 dh->ret_code = NO_ERROR;
2812 if (nla_put_drbd_cfg_context(skb, tconn, VOLUME_UNSPECIFIED))
2814 nc = rcu_dereference(tconn->net_conf);
2815 if (nc && net_conf_to_skb(skb, nc, 1) != 0)
2820 D_ASSERT(mdev->vnr == volume);
2821 D_ASSERT(mdev->tconn == tconn);
2823 dh->minor = mdev_to_minor(mdev);
2824 dh->ret_code = NO_ERROR;
2826 if (nla_put_status_info(skb, mdev, NULL)) {
2828 genlmsg_cancel(skb, dh);
2832 genlmsg_end(skb, dh);
2837 /* where to start the next iteration */
2838 cb->args[0] = (long)pos;
2839 cb->args[1] = (pos == tconn) ? volume + 1 : 0;
2841 /* No more tconns/volumes/minors found results in an empty skb.
2842 * Which will terminate the dump. */
2847 * Request status of all resources, or of all volumes within a single resource.
2849 * This is a dump, as the answer may not fit in a single reply skb otherwise.
2850 * Which means we cannot use the family->attrbuf or other such members, because
2851 * dump is NOT protected by the genl_lock(). During dump, we only have access
2852 * to the incoming skb, and need to opencode "parsing" of the nlattr payload.
2854 * Once things are setup properly, we call into get_one_status().
2856 int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb)
2858 const unsigned hdrlen = GENL_HDRLEN + GENL_MAGIC_FAMILY_HDRSZ;
2860 const char *resource_name;
2861 struct drbd_tconn *tconn;
2864 /* Is this a followup call? */
2866 /* ... of a single resource dump,
2867 * and the resource iterator has been advanced already? */
2868 if (cb->args[2] && cb->args[2] != cb->args[0])
2869 return 0; /* DONE. */
2873 /* First call (from netlink_dump_start). We need to figure out
2874 * which resource(s) the user wants us to dump. */
2875 nla = nla_find(nlmsg_attrdata(cb->nlh, hdrlen),
2876 nlmsg_attrlen(cb->nlh, hdrlen),
2877 DRBD_NLA_CFG_CONTEXT);
2879 /* No explicit context given. Dump all. */
2882 maxtype = ARRAY_SIZE(drbd_cfg_context_nl_policy) - 1;
2883 nla = drbd_nla_find_nested(maxtype, nla, __nla_type(T_ctx_resource_name));
2885 return PTR_ERR(nla);
2886 /* context given, but no name present? */
2889 resource_name = nla_data(nla);
2890 tconn = conn_get_by_name(resource_name);
2895 kref_put(&tconn->kref, &conn_destroy); /* get_one_status() (re)validates tconn by itself */
2897 /* prime iterators, and set "filter" mode mark:
2898 * only dump this tconn. */
2899 cb->args[0] = (long)tconn;
2900 /* cb->args[1] = 0; passed in this way. */
2901 cb->args[2] = (long)tconn;
2904 return get_one_status(skb, cb);
2907 int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info)
2909 enum drbd_ret_code retcode;
2910 struct timeout_parms tp;
2913 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2914 if (!adm_ctx.reply_skb)
2916 if (retcode != NO_ERROR)
2920 adm_ctx.mdev->state.pdsk == D_OUTDATED ? UT_PEER_OUTDATED :
2921 test_bit(USE_DEGR_WFC_T, &adm_ctx.mdev->flags) ? UT_DEGRADED :
2924 err = timeout_parms_to_priv_skb(adm_ctx.reply_skb, &tp);
2926 nlmsg_free(adm_ctx.reply_skb);
2930 drbd_adm_finish(info, retcode);
2934 int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info)
2936 struct drbd_conf *mdev;
2937 enum drbd_ret_code retcode;
2939 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2940 if (!adm_ctx.reply_skb)
2942 if (retcode != NO_ERROR)
2945 mdev = adm_ctx.mdev;
2946 if (info->attrs[DRBD_NLA_START_OV_PARMS]) {
2947 /* resume from last known position, if possible */
2948 struct start_ov_parms parms =
2949 { .ov_start_sector = mdev->ov_start_sector };
2950 int err = start_ov_parms_from_attrs(&parms, info);
2952 retcode = ERR_MANDATORY_TAG;
2953 drbd_msg_put_info(from_attrs_err_to_txt(err));
2956 /* w_make_ov_request expects position to be aligned */
2957 mdev->ov_start_sector = parms.ov_start_sector & ~BM_SECT_PER_BIT;
2959 /* If there is still bitmap IO pending, e.g. previous resync or verify
2960 * just being finished, wait for it before requesting a new resync. */
2961 drbd_suspend_io(mdev);
2962 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
2963 retcode = drbd_request_state(mdev,NS(conn,C_VERIFY_S));
2964 drbd_resume_io(mdev);
2966 drbd_adm_finish(info, retcode);
2971 int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info)
2973 struct drbd_conf *mdev;
2974 enum drbd_ret_code retcode;
2975 int skip_initial_sync = 0;
2977 struct new_c_uuid_parms args;
2979 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2980 if (!adm_ctx.reply_skb)
2982 if (retcode != NO_ERROR)
2985 mdev = adm_ctx.mdev;
2986 memset(&args, 0, sizeof(args));
2987 if (info->attrs[DRBD_NLA_NEW_C_UUID_PARMS]) {
2988 err = new_c_uuid_parms_from_attrs(&args, info);
2990 retcode = ERR_MANDATORY_TAG;
2991 drbd_msg_put_info(from_attrs_err_to_txt(err));
2996 mutex_lock(mdev->state_mutex); /* Protects us against serialized state changes. */
2998 if (!get_ldev(mdev)) {
2999 retcode = ERR_NO_DISK;
3003 /* this is "skip initial sync", assume to be clean */
3004 if (mdev->state.conn == C_CONNECTED && mdev->tconn->agreed_pro_version >= 90 &&
3005 mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED && args.clear_bm) {
3006 dev_info(DEV, "Preparing to skip initial sync\n");
3007 skip_initial_sync = 1;
3008 } else if (mdev->state.conn != C_STANDALONE) {
3009 retcode = ERR_CONNECTED;
3013 drbd_uuid_set(mdev, UI_BITMAP, 0); /* Rotate UI_BITMAP to History 1, etc... */
3014 drbd_uuid_new_current(mdev); /* New current, previous to UI_BITMAP */
3016 if (args.clear_bm) {
3017 err = drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write,
3018 "clear_n_write from new_c_uuid", BM_LOCKED_MASK);
3020 dev_err(DEV, "Writing bitmap failed with %d\n",err);
3021 retcode = ERR_IO_MD_DISK;
3023 if (skip_initial_sync) {
3024 drbd_send_uuids_skip_initial_sync(mdev);
3025 _drbd_uuid_set(mdev, UI_BITMAP, 0);
3026 drbd_print_uuids(mdev, "cleared bitmap UUID");
3027 spin_lock_irq(&mdev->tconn->req_lock);
3028 _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
3030 spin_unlock_irq(&mdev->tconn->req_lock);
3038 mutex_unlock(mdev->state_mutex);
3040 drbd_adm_finish(info, retcode);
3044 static enum drbd_ret_code
3045 drbd_check_resource_name(const char *name)
3047 if (!name || !name[0]) {
3048 drbd_msg_put_info("resource name missing");
3049 return ERR_MANDATORY_TAG;
3051 /* if we want to use these in sysfs/configfs/debugfs some day,
3052 * we must not allow slashes */
3053 if (strchr(name, '/')) {
3054 drbd_msg_put_info("invalid resource name");
3055 return ERR_INVALID_REQUEST;
3060 int drbd_adm_new_resource(struct sk_buff *skb, struct genl_info *info)
3062 enum drbd_ret_code retcode;
3063 struct res_opts res_opts;
3066 retcode = drbd_adm_prepare(skb, info, 0);
3067 if (!adm_ctx.reply_skb)
3069 if (retcode != NO_ERROR)
3072 set_res_opts_defaults(&res_opts);
3073 err = res_opts_from_attrs(&res_opts, info);
3074 if (err && err != -ENOMSG) {
3075 retcode = ERR_MANDATORY_TAG;
3076 drbd_msg_put_info(from_attrs_err_to_txt(err));
3080 retcode = drbd_check_resource_name(adm_ctx.resource_name);
3081 if (retcode != NO_ERROR)
3084 if (adm_ctx.tconn) {
3085 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL) {
3086 retcode = ERR_INVALID_REQUEST;
3087 drbd_msg_put_info("resource exists");
3089 /* else: still NO_ERROR */
3093 if (!conn_create(adm_ctx.resource_name, &res_opts))
3094 retcode = ERR_NOMEM;
3096 drbd_adm_finish(info, retcode);
3100 int drbd_adm_add_minor(struct sk_buff *skb, struct genl_info *info)
3102 struct drbd_genlmsghdr *dh = info->userhdr;
3103 enum drbd_ret_code retcode;
3105 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
3106 if (!adm_ctx.reply_skb)
3108 if (retcode != NO_ERROR)
3111 if (dh->minor > MINORMASK) {
3112 drbd_msg_put_info("requested minor out of range");
3113 retcode = ERR_INVALID_REQUEST;
3116 if (adm_ctx.volume > DRBD_VOLUME_MAX) {
3117 drbd_msg_put_info("requested volume id out of range");
3118 retcode = ERR_INVALID_REQUEST;
3122 /* drbd_adm_prepare made sure already
3123 * that mdev->tconn and mdev->vnr match the request. */
3125 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
3126 retcode = ERR_MINOR_EXISTS;
3127 /* else: still NO_ERROR */
3131 retcode = conn_new_minor(adm_ctx.tconn, dh->minor, adm_ctx.volume);
3133 drbd_adm_finish(info, retcode);
3137 static enum drbd_ret_code adm_delete_minor(struct drbd_conf *mdev)
3139 if (mdev->state.disk == D_DISKLESS &&
3140 /* no need to be mdev->state.conn == C_STANDALONE &&
3141 * we may want to delete a minor from a live replication group.
3143 mdev->state.role == R_SECONDARY) {
3144 _drbd_request_state(mdev, NS(conn, C_WF_REPORT_PARAMS),
3145 CS_VERBOSE + CS_WAIT_COMPLETE);
3146 idr_remove(&mdev->tconn->volumes, mdev->vnr);
3147 idr_remove(&minors, mdev_to_minor(mdev));
3148 del_gendisk(mdev->vdisk);
3150 kref_put(&mdev->kref, &drbd_minor_destroy);
3153 return ERR_MINOR_CONFIGURED;
3156 int drbd_adm_delete_minor(struct sk_buff *skb, struct genl_info *info)
3158 enum drbd_ret_code retcode;
3160 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
3161 if (!adm_ctx.reply_skb)
3163 if (retcode != NO_ERROR)
3166 retcode = adm_delete_minor(adm_ctx.mdev);
3168 drbd_adm_finish(info, retcode);
3172 int drbd_adm_down(struct sk_buff *skb, struct genl_info *info)
3174 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
3175 struct drbd_conf *mdev;
3178 retcode = drbd_adm_prepare(skb, info, 0);
3179 if (!adm_ctx.reply_skb)
3181 if (retcode != NO_ERROR)
3184 if (!adm_ctx.tconn) {
3185 retcode = ERR_RES_NOT_KNOWN;
3190 idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
3191 retcode = drbd_set_role(mdev, R_SECONDARY, 0);
3192 if (retcode < SS_SUCCESS) {
3193 drbd_msg_put_info("failed to demote");
3198 retcode = conn_try_disconnect(adm_ctx.tconn, 0);
3199 if (retcode < SS_SUCCESS) {
3200 drbd_msg_put_info("failed to disconnect");
3205 idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
3206 retcode = adm_detach(mdev, 0);
3207 if (retcode < SS_SUCCESS || retcode > NO_ERROR) {
3208 drbd_msg_put_info("failed to detach");
3213 /* If we reach this, all volumes (of this tconn) are Secondary,
3214 * Disconnected, Diskless, aka Unconfigured. Make sure all threads have
3215 * actually stopped, state handling only does drbd_thread_stop_nowait(). */
3216 drbd_thread_stop(&adm_ctx.tconn->worker);
3218 /* Now, nothing can fail anymore */
3220 /* delete volumes */
3221 idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
3222 retcode = adm_delete_minor(mdev);
3223 if (retcode != NO_ERROR) {
3224 /* "can not happen" */
3225 drbd_msg_put_info("failed to delete volume");
3230 /* delete connection */
3231 if (conn_lowest_minor(adm_ctx.tconn) < 0) {
3232 list_del_rcu(&adm_ctx.tconn->all_tconn);
3234 kref_put(&adm_ctx.tconn->kref, &conn_destroy);
3238 /* "can not happen" */
3239 retcode = ERR_RES_IN_USE;
3240 drbd_msg_put_info("failed to delete connection");
3244 drbd_adm_finish(info, retcode);
3248 int drbd_adm_del_resource(struct sk_buff *skb, struct genl_info *info)
3250 enum drbd_ret_code retcode;
3252 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
3253 if (!adm_ctx.reply_skb)
3255 if (retcode != NO_ERROR)
3258 if (conn_lowest_minor(adm_ctx.tconn) < 0) {
3259 list_del_rcu(&adm_ctx.tconn->all_tconn);
3261 kref_put(&adm_ctx.tconn->kref, &conn_destroy);
3265 retcode = ERR_RES_IN_USE;
3268 if (retcode == NO_ERROR)
3269 drbd_thread_stop(&adm_ctx.tconn->worker);
3271 drbd_adm_finish(info, retcode);
3275 void drbd_bcast_event(struct drbd_conf *mdev, const struct sib_info *sib)
3277 static atomic_t drbd_genl_seq = ATOMIC_INIT(2); /* two. */
3278 struct sk_buff *msg;
3279 struct drbd_genlmsghdr *d_out;
3283 seq = atomic_inc_return(&drbd_genl_seq);
3284 msg = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
3289 d_out = genlmsg_put(msg, 0, seq, &drbd_genl_family, 0, DRBD_EVENT);
3290 if (!d_out) /* cannot happen, but anyways. */
3291 goto nla_put_failure;
3292 d_out->minor = mdev_to_minor(mdev);
3293 d_out->ret_code = NO_ERROR;
3295 if (nla_put_status_info(msg, mdev, sib))
3296 goto nla_put_failure;
3297 genlmsg_end(msg, d_out);
3298 err = drbd_genl_multicast_events(msg, 0);
3299 /* msg has been consumed or freed in netlink_broadcast() */
3300 if (err && err != -ESRCH)
3308 dev_err(DEV, "Error %d while broadcasting event. "
3309 "Event seq:%u sib_reason:%u\n",
3310 err, seq, sib->sib_reason);