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 && !capable(CAP_NET_ADMIN))
161 adm_ctx.reply_skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
162 if (!adm_ctx.reply_skb) {
167 adm_ctx.reply_dh = genlmsg_put_reply(adm_ctx.reply_skb,
168 info, &drbd_genl_family, 0, cmd);
169 /* put of a few bytes into a fresh skb of >= 4k will always succeed.
171 if (!adm_ctx.reply_dh) {
176 adm_ctx.reply_dh->minor = d_in->minor;
177 adm_ctx.reply_dh->ret_code = NO_ERROR;
179 adm_ctx.volume = VOLUME_UNSPECIFIED;
180 if (info->attrs[DRBD_NLA_CFG_CONTEXT]) {
182 /* parse and validate only */
183 err = drbd_cfg_context_from_attrs(NULL, info);
187 /* It was present, and valid,
188 * copy it over to the reply skb. */
189 err = nla_put_nohdr(adm_ctx.reply_skb,
190 info->attrs[DRBD_NLA_CFG_CONTEXT]->nla_len,
191 info->attrs[DRBD_NLA_CFG_CONTEXT]);
195 /* and assign stuff to the global adm_ctx */
196 nla = nested_attr_tb[__nla_type(T_ctx_volume)];
198 adm_ctx.volume = nla_get_u32(nla);
199 nla = nested_attr_tb[__nla_type(T_ctx_resource_name)];
201 adm_ctx.resource_name = nla_data(nla);
202 adm_ctx.my_addr = nested_attr_tb[__nla_type(T_ctx_my_addr)];
203 adm_ctx.peer_addr = nested_attr_tb[__nla_type(T_ctx_peer_addr)];
204 if ((adm_ctx.my_addr &&
205 nla_len(adm_ctx.my_addr) > sizeof(adm_ctx.tconn->my_addr)) ||
206 (adm_ctx.peer_addr &&
207 nla_len(adm_ctx.peer_addr) > sizeof(adm_ctx.tconn->peer_addr))) {
213 adm_ctx.minor = d_in->minor;
214 adm_ctx.mdev = minor_to_mdev(d_in->minor);
215 adm_ctx.tconn = conn_get_by_name(adm_ctx.resource_name);
217 if (!adm_ctx.mdev && (flags & DRBD_ADM_NEED_MINOR)) {
218 drbd_msg_put_info("unknown minor");
219 return ERR_MINOR_INVALID;
221 if (!adm_ctx.tconn && (flags & DRBD_ADM_NEED_RESOURCE)) {
222 drbd_msg_put_info("unknown resource");
223 return ERR_INVALID_REQUEST;
226 if (flags & DRBD_ADM_NEED_CONNECTION) {
227 if (adm_ctx.tconn && !(flags & DRBD_ADM_NEED_RESOURCE)) {
228 drbd_msg_put_info("no resource name expected");
229 return ERR_INVALID_REQUEST;
232 drbd_msg_put_info("no minor number expected");
233 return ERR_INVALID_REQUEST;
235 if (adm_ctx.my_addr && adm_ctx.peer_addr)
236 adm_ctx.tconn = conn_get_by_addrs(nla_data(adm_ctx.my_addr),
237 nla_len(adm_ctx.my_addr),
238 nla_data(adm_ctx.peer_addr),
239 nla_len(adm_ctx.peer_addr));
240 if (!adm_ctx.tconn) {
241 drbd_msg_put_info("unknown connection");
242 return ERR_INVALID_REQUEST;
246 /* some more paranoia, if the request was over-determined */
247 if (adm_ctx.mdev && adm_ctx.tconn &&
248 adm_ctx.mdev->tconn != adm_ctx.tconn) {
249 pr_warning("request: minor=%u, resource=%s; but that minor belongs to connection %s\n",
250 adm_ctx.minor, adm_ctx.resource_name,
251 adm_ctx.mdev->tconn->name);
252 drbd_msg_put_info("minor exists in different resource");
253 return ERR_INVALID_REQUEST;
256 adm_ctx.volume != VOLUME_UNSPECIFIED &&
257 adm_ctx.volume != adm_ctx.mdev->vnr) {
258 pr_warning("request: minor=%u, volume=%u; but that minor is volume %u in %s\n",
259 adm_ctx.minor, adm_ctx.volume,
260 adm_ctx.mdev->vnr, adm_ctx.mdev->tconn->name);
261 drbd_msg_put_info("minor exists as different volume");
262 return ERR_INVALID_REQUEST;
268 nlmsg_free(adm_ctx.reply_skb);
269 adm_ctx.reply_skb = NULL;
273 static int drbd_adm_finish(struct genl_info *info, int retcode)
276 kref_put(&adm_ctx.tconn->kref, &conn_destroy);
277 adm_ctx.tconn = NULL;
280 if (!adm_ctx.reply_skb)
283 adm_ctx.reply_dh->ret_code = retcode;
284 drbd_adm_send_reply(adm_ctx.reply_skb, info);
288 static void setup_khelper_env(struct drbd_tconn *tconn, char **envp)
292 /* FIXME: A future version will not allow this case. */
293 if (tconn->my_addr_len == 0 || tconn->peer_addr_len == 0)
296 switch (((struct sockaddr *)&tconn->peer_addr)->sa_family) {
299 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI6",
300 &((struct sockaddr_in6 *)&tconn->peer_addr)->sin6_addr);
304 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
305 &((struct sockaddr_in *)&tconn->peer_addr)->sin_addr);
309 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
310 &((struct sockaddr_in *)&tconn->peer_addr)->sin_addr);
312 snprintf(envp[3], 20, "DRBD_PEER_AF=%s", afs);
315 int drbd_khelper(struct drbd_conf *mdev, char *cmd)
317 char *envp[] = { "HOME=/",
319 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
320 (char[20]) { }, /* address family */
321 (char[60]) { }, /* address */
324 char *argv[] = {usermode_helper, cmd, mb, NULL };
325 struct drbd_tconn *tconn = mdev->tconn;
329 if (current == tconn->worker.task)
330 set_bit(CALLBACK_PENDING, &tconn->flags);
332 snprintf(mb, 12, "minor-%d", mdev_to_minor(mdev));
333 setup_khelper_env(tconn, envp);
335 /* The helper may take some time.
336 * write out any unsynced meta data changes now */
339 dev_info(DEV, "helper command: %s %s %s\n", usermode_helper, cmd, mb);
340 sib.sib_reason = SIB_HELPER_PRE;
341 sib.helper_name = cmd;
342 drbd_bcast_event(mdev, &sib);
343 ret = call_usermodehelper(usermode_helper, argv, envp, UMH_WAIT_PROC);
345 dev_warn(DEV, "helper command: %s %s %s exit code %u (0x%x)\n",
346 usermode_helper, cmd, mb,
347 (ret >> 8) & 0xff, ret);
349 dev_info(DEV, "helper command: %s %s %s exit code %u (0x%x)\n",
350 usermode_helper, cmd, mb,
351 (ret >> 8) & 0xff, ret);
352 sib.sib_reason = SIB_HELPER_POST;
353 sib.helper_exit_code = ret;
354 drbd_bcast_event(mdev, &sib);
356 if (current == tconn->worker.task)
357 clear_bit(CALLBACK_PENDING, &tconn->flags);
359 if (ret < 0) /* Ignore any ERRNOs we got. */
365 int conn_khelper(struct drbd_tconn *tconn, char *cmd)
367 char *envp[] = { "HOME=/",
369 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
370 (char[20]) { }, /* address family */
371 (char[60]) { }, /* address */
373 char *argv[] = {usermode_helper, cmd, tconn->name, NULL };
376 setup_khelper_env(tconn, envp);
379 conn_info(tconn, "helper command: %s %s %s\n", usermode_helper, cmd, tconn->name);
380 /* TODO: conn_bcast_event() ?? */
382 ret = call_usermodehelper(usermode_helper, argv, envp, UMH_WAIT_PROC);
384 conn_warn(tconn, "helper command: %s %s %s exit code %u (0x%x)\n",
385 usermode_helper, cmd, tconn->name,
386 (ret >> 8) & 0xff, ret);
388 conn_info(tconn, "helper command: %s %s %s exit code %u (0x%x)\n",
389 usermode_helper, cmd, tconn->name,
390 (ret >> 8) & 0xff, ret);
391 /* TODO: conn_bcast_event() ?? */
393 if (ret < 0) /* Ignore any ERRNOs we got. */
399 static enum drbd_fencing_p highest_fencing_policy(struct drbd_tconn *tconn)
401 enum drbd_fencing_p fp = FP_NOT_AVAIL;
402 struct drbd_conf *mdev;
406 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
407 if (get_ldev_if_state(mdev, D_CONSISTENT)) {
408 fp = max_t(enum drbd_fencing_p, fp,
409 rcu_dereference(mdev->ldev->disk_conf)->fencing);
418 bool conn_try_outdate_peer(struct drbd_tconn *tconn)
420 union drbd_state mask = { };
421 union drbd_state val = { };
422 enum drbd_fencing_p fp;
426 if (tconn->cstate >= C_WF_REPORT_PARAMS) {
427 conn_err(tconn, "Expected cstate < C_WF_REPORT_PARAMS\n");
431 fp = highest_fencing_policy(tconn);
434 conn_warn(tconn, "Not fencing peer, I'm not even Consistent myself.\n");
441 r = conn_khelper(tconn, "fence-peer");
443 switch ((r>>8) & 0xff) {
444 case 3: /* peer is inconsistent */
445 ex_to_string = "peer is inconsistent or worse";
447 val.pdsk = D_INCONSISTENT;
449 case 4: /* peer got outdated, or was already outdated */
450 ex_to_string = "peer was fenced";
452 val.pdsk = D_OUTDATED;
454 case 5: /* peer was down */
455 if (conn_highest_disk(tconn) == D_UP_TO_DATE) {
456 /* we will(have) create(d) a new UUID anyways... */
457 ex_to_string = "peer is unreachable, assumed to be dead";
459 val.pdsk = D_OUTDATED;
461 ex_to_string = "peer unreachable, doing nothing since disk != UpToDate";
464 case 6: /* Peer is primary, voluntarily outdate myself.
465 * This is useful when an unconnected R_SECONDARY is asked to
466 * become R_PRIMARY, but finds the other peer being active. */
467 ex_to_string = "peer is active";
468 conn_warn(tconn, "Peer is primary, outdating myself.\n");
470 val.disk = D_OUTDATED;
473 if (fp != FP_STONITH)
474 conn_err(tconn, "fence-peer() = 7 && fencing != Stonith !!!\n");
475 ex_to_string = "peer was stonithed";
477 val.pdsk = D_OUTDATED;
480 /* The script is broken ... */
481 conn_err(tconn, "fence-peer helper broken, returned %d\n", (r>>8)&0xff);
482 return false; /* Eventually leave IO frozen */
485 conn_info(tconn, "fence-peer helper returned %d (%s)\n",
486 (r>>8) & 0xff, ex_to_string);
491 conn_request_state(tconn, mask, val, CS_VERBOSE);
492 here, because we might were able to re-establish the connection in the
494 spin_lock_irq(&tconn->req_lock);
495 if (tconn->cstate < C_WF_REPORT_PARAMS && !test_bit(STATE_SENT, &tconn->flags))
496 _conn_request_state(tconn, mask, val, CS_VERBOSE);
497 spin_unlock_irq(&tconn->req_lock);
499 return conn_highest_pdsk(tconn) <= D_OUTDATED;
502 static int _try_outdate_peer_async(void *data)
504 struct drbd_tconn *tconn = (struct drbd_tconn *)data;
506 conn_try_outdate_peer(tconn);
508 kref_put(&tconn->kref, &conn_destroy);
512 void conn_try_outdate_peer_async(struct drbd_tconn *tconn)
514 struct task_struct *opa;
516 kref_get(&tconn->kref);
517 opa = kthread_run(_try_outdate_peer_async, tconn, "drbd_async_h");
519 conn_err(tconn, "out of mem, failed to invoke fence-peer helper\n");
520 kref_put(&tconn->kref, &conn_destroy);
525 drbd_set_role(struct drbd_conf *mdev, enum drbd_role new_role, int force)
527 const int max_tries = 4;
528 enum drbd_state_rv rv = SS_UNKNOWN_ERROR;
532 union drbd_state mask, val;
534 if (new_role == R_PRIMARY)
535 request_ping(mdev->tconn); /* Detect a dead peer ASAP */
537 mutex_lock(mdev->state_mutex);
539 mask.i = 0; mask.role = R_MASK;
540 val.i = 0; val.role = new_role;
542 while (try++ < max_tries) {
543 rv = _drbd_request_state(mdev, mask, val, CS_WAIT_COMPLETE);
545 /* in case we first succeeded to outdate,
546 * but now suddenly could establish a connection */
547 if (rv == SS_CW_FAILED_BY_PEER && mask.pdsk != 0) {
553 if (rv == SS_NO_UP_TO_DATE_DISK && force &&
554 (mdev->state.disk < D_UP_TO_DATE &&
555 mdev->state.disk >= D_INCONSISTENT)) {
557 val.disk = D_UP_TO_DATE;
562 if (rv == SS_NO_UP_TO_DATE_DISK &&
563 mdev->state.disk == D_CONSISTENT && mask.pdsk == 0) {
564 D_ASSERT(mdev->state.pdsk == D_UNKNOWN);
566 if (conn_try_outdate_peer(mdev->tconn)) {
567 val.disk = D_UP_TO_DATE;
573 if (rv == SS_NOTHING_TO_DO)
575 if (rv == SS_PRIMARY_NOP && mask.pdsk == 0) {
576 if (!conn_try_outdate_peer(mdev->tconn) && force) {
577 dev_warn(DEV, "Forced into split brain situation!\n");
579 val.pdsk = D_OUTDATED;
584 if (rv == SS_TWO_PRIMARIES) {
585 /* Maybe the peer is detected as dead very soon...
586 retry at most once more in this case. */
589 nc = rcu_dereference(mdev->tconn->net_conf);
590 timeo = nc ? (nc->ping_timeo + 1) * HZ / 10 : 1;
592 schedule_timeout_interruptible(timeo);
597 if (rv < SS_SUCCESS) {
598 rv = _drbd_request_state(mdev, mask, val,
599 CS_VERBOSE + CS_WAIT_COMPLETE);
610 dev_warn(DEV, "Forced to consider local data as UpToDate!\n");
612 /* Wait until nothing is on the fly :) */
613 wait_event(mdev->misc_wait, atomic_read(&mdev->ap_pending_cnt) == 0);
615 /* FIXME also wait for all pending P_BARRIER_ACK? */
617 if (new_role == R_SECONDARY) {
618 set_disk_ro(mdev->vdisk, true);
619 if (get_ldev(mdev)) {
620 mdev->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
624 mutex_lock(&mdev->tconn->conf_update);
625 nc = mdev->tconn->net_conf;
627 nc->discard_my_data = 0; /* without copy; single bit op is atomic */
628 mutex_unlock(&mdev->tconn->conf_update);
630 set_disk_ro(mdev->vdisk, false);
631 if (get_ldev(mdev)) {
632 if (((mdev->state.conn < C_CONNECTED ||
633 mdev->state.pdsk <= D_FAILED)
634 && mdev->ldev->md.uuid[UI_BITMAP] == 0) || forced)
635 drbd_uuid_new_current(mdev);
637 mdev->ldev->md.uuid[UI_CURRENT] |= (u64)1;
642 /* writeout of activity log covered areas of the bitmap
643 * to stable storage done in after state change already */
645 if (mdev->state.conn >= C_WF_REPORT_PARAMS) {
646 /* if this was forced, we should consider sync */
648 drbd_send_uuids(mdev);
649 drbd_send_current_state(mdev);
654 kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
656 mutex_unlock(mdev->state_mutex);
660 static const char *from_attrs_err_to_txt(int err)
662 return err == -ENOMSG ? "required attribute missing" :
663 err == -EOPNOTSUPP ? "unknown mandatory attribute" :
664 err == -EEXIST ? "can not change invariant setting" :
665 "invalid attribute value";
668 int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info)
670 struct set_role_parms parms;
672 enum drbd_ret_code retcode;
674 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
675 if (!adm_ctx.reply_skb)
677 if (retcode != NO_ERROR)
680 memset(&parms, 0, sizeof(parms));
681 if (info->attrs[DRBD_NLA_SET_ROLE_PARMS]) {
682 err = set_role_parms_from_attrs(&parms, info);
684 retcode = ERR_MANDATORY_TAG;
685 drbd_msg_put_info(from_attrs_err_to_txt(err));
690 if (info->genlhdr->cmd == DRBD_ADM_PRIMARY)
691 retcode = drbd_set_role(adm_ctx.mdev, R_PRIMARY, parms.assume_uptodate);
693 retcode = drbd_set_role(adm_ctx.mdev, R_SECONDARY, 0);
695 drbd_adm_finish(info, retcode);
699 /* initializes the md.*_offset members, so we are able to find
700 * the on disk meta data */
701 static void drbd_md_set_sector_offsets(struct drbd_conf *mdev,
702 struct drbd_backing_dev *bdev)
704 sector_t md_size_sect = 0;
708 meta_dev_idx = rcu_dereference(bdev->disk_conf)->meta_dev_idx;
710 switch (meta_dev_idx) {
712 /* v07 style fixed size indexed meta data */
713 bdev->md.md_size_sect = MD_RESERVED_SECT;
714 bdev->md.md_offset = drbd_md_ss__(mdev, bdev);
715 bdev->md.al_offset = MD_AL_OFFSET;
716 bdev->md.bm_offset = MD_BM_OFFSET;
718 case DRBD_MD_INDEX_FLEX_EXT:
719 /* just occupy the full device; unit: sectors */
720 bdev->md.md_size_sect = drbd_get_capacity(bdev->md_bdev);
721 bdev->md.md_offset = 0;
722 bdev->md.al_offset = MD_AL_OFFSET;
723 bdev->md.bm_offset = MD_BM_OFFSET;
725 case DRBD_MD_INDEX_INTERNAL:
726 case DRBD_MD_INDEX_FLEX_INT:
727 bdev->md.md_offset = drbd_md_ss__(mdev, bdev);
728 /* al size is still fixed */
729 bdev->md.al_offset = -MD_AL_SECTORS;
730 /* we need (slightly less than) ~ this much bitmap sectors: */
731 md_size_sect = drbd_get_capacity(bdev->backing_bdev);
732 md_size_sect = ALIGN(md_size_sect, BM_SECT_PER_EXT);
733 md_size_sect = BM_SECT_TO_EXT(md_size_sect);
734 md_size_sect = ALIGN(md_size_sect, 8);
736 /* plus the "drbd meta data super block",
737 * and the activity log; */
738 md_size_sect += MD_BM_OFFSET;
740 bdev->md.md_size_sect = md_size_sect;
741 /* bitmap offset is adjusted by 'super' block size */
742 bdev->md.bm_offset = -md_size_sect + MD_AL_OFFSET;
748 /* input size is expected to be in KB */
749 char *ppsize(char *buf, unsigned long long size)
751 /* Needs 9 bytes at max including trailing NUL:
752 * -1ULL ==> "16384 EB" */
753 static char units[] = { 'K', 'M', 'G', 'T', 'P', 'E' };
755 while (size >= 10000 && base < sizeof(units)-1) {
757 size = (size >> 10) + !!(size & (1<<9));
760 sprintf(buf, "%u %cB", (unsigned)size, units[base]);
765 /* there is still a theoretical deadlock when called from receiver
766 * on an D_INCONSISTENT R_PRIMARY:
767 * remote READ does inc_ap_bio, receiver would need to receive answer
768 * packet from remote to dec_ap_bio again.
769 * receiver receive_sizes(), comes here,
770 * waits for ap_bio_cnt == 0. -> deadlock.
771 * but this cannot happen, actually, because:
772 * R_PRIMARY D_INCONSISTENT, and peer's disk is unreachable
773 * (not connected, or bad/no disk on peer):
774 * see drbd_fail_request_early, ap_bio_cnt is zero.
775 * R_PRIMARY D_INCONSISTENT, and C_SYNC_TARGET:
776 * peer may not initiate a resize.
778 /* Note these are not to be confused with
779 * drbd_adm_suspend_io/drbd_adm_resume_io,
780 * which are (sub) state changes triggered by admin (drbdsetup),
781 * and can be long lived.
782 * This changes an mdev->flag, is triggered by drbd internals,
783 * and should be short-lived. */
784 void drbd_suspend_io(struct drbd_conf *mdev)
786 set_bit(SUSPEND_IO, &mdev->flags);
787 if (drbd_suspended(mdev))
789 wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_bio_cnt));
792 void drbd_resume_io(struct drbd_conf *mdev)
794 clear_bit(SUSPEND_IO, &mdev->flags);
795 wake_up(&mdev->misc_wait);
799 * drbd_determine_dev_size() - Sets the right device size obeying all constraints
800 * @mdev: DRBD device.
802 * Returns 0 on success, negative return values indicate errors.
803 * You should call drbd_md_sync() after calling this function.
805 enum determine_dev_size drbd_determine_dev_size(struct drbd_conf *mdev, enum dds_flags flags) __must_hold(local)
807 sector_t prev_first_sect, prev_size; /* previous meta location */
808 sector_t la_size, u_size;
812 int md_moved, la_size_changed;
813 enum determine_dev_size rv = unchanged;
816 * application request passes inc_ap_bio,
817 * but then cannot get an AL-reference.
818 * this function later may wait on ap_bio_cnt == 0. -> deadlock.
821 * Suspend IO right here.
822 * still lock the act_log to not trigger ASSERTs there.
824 drbd_suspend_io(mdev);
826 /* no wait necessary anymore, actually we could assert that */
827 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
829 prev_first_sect = drbd_md_first_sector(mdev->ldev);
830 prev_size = mdev->ldev->md.md_size_sect;
831 la_size = mdev->ldev->md.la_size_sect;
833 /* TODO: should only be some assert here, not (re)init... */
834 drbd_md_set_sector_offsets(mdev, mdev->ldev);
837 u_size = rcu_dereference(mdev->ldev->disk_conf)->disk_size;
839 size = drbd_new_dev_size(mdev, mdev->ldev, u_size, flags & DDSF_FORCED);
841 if (drbd_get_capacity(mdev->this_bdev) != size ||
842 drbd_bm_capacity(mdev) != size) {
844 err = drbd_bm_resize(mdev, size, !(flags & DDSF_NO_RESYNC));
846 /* currently there is only one error: ENOMEM! */
847 size = drbd_bm_capacity(mdev)>>1;
849 dev_err(DEV, "OUT OF MEMORY! "
850 "Could not allocate bitmap!\n");
852 dev_err(DEV, "BM resizing failed. "
853 "Leaving size unchanged at size = %lu KB\n",
854 (unsigned long)size);
858 /* racy, see comments above. */
859 drbd_set_my_capacity(mdev, size);
860 mdev->ldev->md.la_size_sect = size;
861 dev_info(DEV, "size = %s (%llu KB)\n", ppsize(ppb, size>>1),
862 (unsigned long long)size>>1);
864 if (rv == dev_size_error)
867 la_size_changed = (la_size != mdev->ldev->md.la_size_sect);
869 md_moved = prev_first_sect != drbd_md_first_sector(mdev->ldev)
870 || prev_size != mdev->ldev->md.md_size_sect;
872 if (la_size_changed || md_moved) {
875 drbd_al_shrink(mdev); /* All extents inactive. */
876 dev_info(DEV, "Writing the whole bitmap, %s\n",
877 la_size_changed && md_moved ? "size changed and md moved" :
878 la_size_changed ? "size changed" : "md moved");
879 /* next line implicitly does drbd_suspend_io()+drbd_resume_io() */
880 err = drbd_bitmap_io(mdev, md_moved ? &drbd_bm_write_all : &drbd_bm_write,
881 "size changed", BM_LOCKED_MASK);
886 drbd_md_mark_dirty(mdev);
894 lc_unlock(mdev->act_log);
895 wake_up(&mdev->al_wait);
896 drbd_resume_io(mdev);
902 drbd_new_dev_size(struct drbd_conf *mdev, struct drbd_backing_dev *bdev,
903 sector_t u_size, int assume_peer_has_space)
905 sector_t p_size = mdev->p_size; /* partner's disk size. */
906 sector_t la_size = bdev->md.la_size_sect; /* last agreed size. */
907 sector_t m_size; /* my size */
910 m_size = drbd_get_max_capacity(bdev);
912 if (mdev->state.conn < C_CONNECTED && assume_peer_has_space) {
913 dev_warn(DEV, "Resize while not connected was forced by the user!\n");
917 if (p_size && m_size) {
918 size = min_t(sector_t, p_size, m_size);
922 if (m_size && m_size < size)
924 if (p_size && p_size < size)
935 dev_err(DEV, "Both nodes diskless!\n");
939 dev_err(DEV, "Requested disk size is too big (%lu > %lu)\n",
940 (unsigned long)u_size>>1, (unsigned long)size>>1);
949 * drbd_check_al_size() - Ensures that the AL is of the right size
950 * @mdev: DRBD device.
952 * Returns -EBUSY if current al lru is still used, -ENOMEM when allocation
953 * failed, and 0 on success. You should call drbd_md_sync() after you called
956 static int drbd_check_al_size(struct drbd_conf *mdev, struct disk_conf *dc)
958 struct lru_cache *n, *t;
959 struct lc_element *e;
964 mdev->act_log->nr_elements == dc->al_extents)
969 n = lc_create("act_log", drbd_al_ext_cache, AL_UPDATES_PER_TRANSACTION,
970 dc->al_extents, sizeof(struct lc_element), 0);
973 dev_err(DEV, "Cannot allocate act_log lru!\n");
976 spin_lock_irq(&mdev->al_lock);
978 for (i = 0; i < t->nr_elements; i++) {
979 e = lc_element_by_index(t, i);
981 dev_err(DEV, "refcnt(%d)==%d\n",
982 e->lc_number, e->refcnt);
988 spin_unlock_irq(&mdev->al_lock);
990 dev_err(DEV, "Activity log still in use!\n");
997 drbd_md_mark_dirty(mdev); /* we changed mdev->act_log->nr_elemens */
1001 static void drbd_setup_queue_param(struct drbd_conf *mdev, unsigned int max_bio_size)
1003 struct request_queue * const q = mdev->rq_queue;
1004 unsigned int max_hw_sectors = max_bio_size >> 9;
1005 unsigned int max_segments = 0;
1007 if (get_ldev_if_state(mdev, D_ATTACHING)) {
1008 struct request_queue * const b = mdev->ldev->backing_bdev->bd_disk->queue;
1010 max_hw_sectors = min(queue_max_hw_sectors(b), max_bio_size >> 9);
1012 max_segments = rcu_dereference(mdev->ldev->disk_conf)->max_bio_bvecs;
1017 blk_queue_logical_block_size(q, 512);
1018 blk_queue_max_hw_sectors(q, max_hw_sectors);
1019 /* This is the workaround for "bio would need to, but cannot, be split" */
1020 blk_queue_max_segments(q, max_segments ? max_segments : BLK_MAX_SEGMENTS);
1021 blk_queue_segment_boundary(q, PAGE_CACHE_SIZE-1);
1023 if (get_ldev_if_state(mdev, D_ATTACHING)) {
1024 struct request_queue * const b = mdev->ldev->backing_bdev->bd_disk->queue;
1026 blk_queue_stack_limits(q, b);
1028 if (q->backing_dev_info.ra_pages != b->backing_dev_info.ra_pages) {
1029 dev_info(DEV, "Adjusting my ra_pages to backing device's (%lu -> %lu)\n",
1030 q->backing_dev_info.ra_pages,
1031 b->backing_dev_info.ra_pages);
1032 q->backing_dev_info.ra_pages = b->backing_dev_info.ra_pages;
1038 void drbd_reconsider_max_bio_size(struct drbd_conf *mdev)
1040 unsigned int now, new, local, peer;
1042 now = queue_max_hw_sectors(mdev->rq_queue) << 9;
1043 local = mdev->local_max_bio_size; /* Eventually last known value, from volatile memory */
1044 peer = mdev->peer_max_bio_size; /* Eventually last known value, from meta data */
1046 if (get_ldev_if_state(mdev, D_ATTACHING)) {
1047 local = queue_max_hw_sectors(mdev->ldev->backing_bdev->bd_disk->queue) << 9;
1048 mdev->local_max_bio_size = local;
1051 local = min(local, DRBD_MAX_BIO_SIZE);
1053 /* We may ignore peer limits if the peer is modern enough.
1054 Because new from 8.3.8 onwards the peer can use multiple
1055 BIOs for a single peer_request */
1056 if (mdev->state.conn >= C_CONNECTED) {
1057 if (mdev->tconn->agreed_pro_version < 94)
1058 peer = min( mdev->peer_max_bio_size, DRBD_MAX_SIZE_H80_PACKET);
1059 /* Correct old drbd (up to 8.3.7) if it believes it can do more than 32KiB */
1060 else if (mdev->tconn->agreed_pro_version == 94)
1061 peer = DRBD_MAX_SIZE_H80_PACKET;
1062 else if (mdev->tconn->agreed_pro_version < 100)
1063 peer = DRBD_MAX_BIO_SIZE_P95; /* drbd 8.3.8 onwards, before 8.4.0 */
1065 peer = DRBD_MAX_BIO_SIZE;
1068 new = min(local, peer);
1070 if (mdev->state.role == R_PRIMARY && new < now)
1071 dev_err(DEV, "ASSERT FAILED new < now; (%u < %u)\n", new, now);
1074 dev_info(DEV, "max BIO size = %u\n", new);
1076 drbd_setup_queue_param(mdev, new);
1079 /* Starts the worker thread */
1080 static void conn_reconfig_start(struct drbd_tconn *tconn)
1082 drbd_thread_start(&tconn->worker);
1083 conn_flush_workqueue(tconn);
1086 /* if still unconfigured, stops worker again. */
1087 static void conn_reconfig_done(struct drbd_tconn *tconn)
1090 spin_lock_irq(&tconn->req_lock);
1091 stop_threads = conn_all_vols_unconf(tconn) &&
1092 tconn->cstate == C_STANDALONE;
1093 spin_unlock_irq(&tconn->req_lock);
1095 /* asender is implicitly stopped by receiver
1096 * in conn_disconnect() */
1097 drbd_thread_stop(&tconn->receiver);
1098 drbd_thread_stop(&tconn->worker);
1102 /* Make sure IO is suspended before calling this function(). */
1103 static void drbd_suspend_al(struct drbd_conf *mdev)
1107 if (!lc_try_lock(mdev->act_log)) {
1108 dev_warn(DEV, "Failed to lock al in drbd_suspend_al()\n");
1112 drbd_al_shrink(mdev);
1113 spin_lock_irq(&mdev->tconn->req_lock);
1114 if (mdev->state.conn < C_CONNECTED)
1115 s = !test_and_set_bit(AL_SUSPENDED, &mdev->flags);
1116 spin_unlock_irq(&mdev->tconn->req_lock);
1117 lc_unlock(mdev->act_log);
1120 dev_info(DEV, "Suspended AL updates\n");
1124 static bool should_set_defaults(struct genl_info *info)
1126 unsigned flags = ((struct drbd_genlmsghdr*)info->userhdr)->flags;
1127 return 0 != (flags & DRBD_GENL_F_SET_DEFAULTS);
1130 static void enforce_disk_conf_limits(struct disk_conf *dc)
1132 if (dc->al_extents < DRBD_AL_EXTENTS_MIN)
1133 dc->al_extents = DRBD_AL_EXTENTS_MIN;
1134 if (dc->al_extents > DRBD_AL_EXTENTS_MAX)
1135 dc->al_extents = DRBD_AL_EXTENTS_MAX;
1137 if (dc->c_plan_ahead > DRBD_C_PLAN_AHEAD_MAX)
1138 dc->c_plan_ahead = DRBD_C_PLAN_AHEAD_MAX;
1141 int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info)
1143 enum drbd_ret_code retcode;
1144 struct drbd_conf *mdev;
1145 struct disk_conf *new_disk_conf, *old_disk_conf;
1146 struct fifo_buffer *old_plan = NULL, *new_plan = NULL;
1149 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1150 if (!adm_ctx.reply_skb)
1152 if (retcode != NO_ERROR)
1155 mdev = adm_ctx.mdev;
1157 /* we also need a disk
1158 * to change the options on */
1159 if (!get_ldev(mdev)) {
1160 retcode = ERR_NO_DISK;
1164 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
1165 if (!new_disk_conf) {
1166 retcode = ERR_NOMEM;
1170 mutex_lock(&mdev->tconn->conf_update);
1171 old_disk_conf = mdev->ldev->disk_conf;
1172 *new_disk_conf = *old_disk_conf;
1173 if (should_set_defaults(info))
1174 set_disk_conf_defaults(new_disk_conf);
1176 err = disk_conf_from_attrs_for_change(new_disk_conf, info);
1177 if (err && err != -ENOMSG) {
1178 retcode = ERR_MANDATORY_TAG;
1179 drbd_msg_put_info(from_attrs_err_to_txt(err));
1182 if (!expect(new_disk_conf->resync_rate >= 1))
1183 new_disk_conf->resync_rate = 1;
1185 enforce_disk_conf_limits(new_disk_conf);
1187 fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
1188 if (fifo_size != mdev->rs_plan_s->size) {
1189 new_plan = fifo_alloc(fifo_size);
1191 dev_err(DEV, "kmalloc of fifo_buffer failed");
1192 retcode = ERR_NOMEM;
1197 drbd_suspend_io(mdev);
1198 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
1199 drbd_al_shrink(mdev);
1200 err = drbd_check_al_size(mdev, new_disk_conf);
1201 lc_unlock(mdev->act_log);
1202 wake_up(&mdev->al_wait);
1203 drbd_resume_io(mdev);
1206 retcode = ERR_NOMEM;
1210 write_lock_irq(&global_state_lock);
1211 retcode = drbd_resync_after_valid(mdev, new_disk_conf->resync_after);
1212 if (retcode == NO_ERROR) {
1213 rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
1214 drbd_resync_after_changed(mdev);
1216 write_unlock_irq(&global_state_lock);
1218 if (retcode != NO_ERROR)
1222 old_plan = mdev->rs_plan_s;
1223 rcu_assign_pointer(mdev->rs_plan_s, new_plan);
1226 mutex_unlock(&mdev->tconn->conf_update);
1228 if (new_disk_conf->al_updates)
1229 mdev->ldev->md.flags &= ~MDF_AL_DISABLED;
1231 mdev->ldev->md.flags |= MDF_AL_DISABLED;
1233 if (new_disk_conf->md_flushes)
1234 clear_bit(MD_NO_FUA, &mdev->flags);
1236 set_bit(MD_NO_FUA, &mdev->flags);
1238 drbd_bump_write_ordering(mdev->tconn, WO_bdev_flush);
1242 if (mdev->state.conn >= C_CONNECTED)
1243 drbd_send_sync_param(mdev);
1246 kfree(old_disk_conf);
1248 mod_timer(&mdev->request_timer, jiffies + HZ);
1252 mutex_unlock(&mdev->tconn->conf_update);
1254 kfree(new_disk_conf);
1259 drbd_adm_finish(info, retcode);
1263 int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info)
1265 struct drbd_conf *mdev;
1267 enum drbd_ret_code retcode;
1268 enum determine_dev_size dd;
1269 sector_t max_possible_sectors;
1270 sector_t min_md_device_sectors;
1271 struct drbd_backing_dev *nbc = NULL; /* new_backing_conf */
1272 struct disk_conf *new_disk_conf = NULL;
1273 struct block_device *bdev;
1274 struct lru_cache *resync_lru = NULL;
1275 struct fifo_buffer *new_plan = NULL;
1276 union drbd_state ns, os;
1277 enum drbd_state_rv rv;
1278 struct net_conf *nc;
1280 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1281 if (!adm_ctx.reply_skb)
1283 if (retcode != NO_ERROR)
1286 mdev = adm_ctx.mdev;
1287 conn_reconfig_start(mdev->tconn);
1289 /* if you want to reconfigure, please tear down first */
1290 if (mdev->state.disk > D_DISKLESS) {
1291 retcode = ERR_DISK_CONFIGURED;
1294 /* It may just now have detached because of IO error. Make sure
1295 * drbd_ldev_destroy is done already, we may end up here very fast,
1296 * e.g. if someone calls attach from the on-io-error handler,
1297 * to realize a "hot spare" feature (not that I'd recommend that) */
1298 wait_event(mdev->misc_wait, !atomic_read(&mdev->local_cnt));
1300 /* make sure there is no leftover from previous force-detach attempts */
1301 clear_bit(FORCE_DETACH, &mdev->flags);
1302 clear_bit(WAS_IO_ERROR, &mdev->flags);
1303 clear_bit(WAS_READ_ERROR, &mdev->flags);
1305 /* and no leftover from previously aborted resync or verify, either */
1307 mdev->rs_failed = 0;
1308 atomic_set(&mdev->rs_pending_cnt, 0);
1310 /* allocation not in the IO path, drbdsetup context */
1311 nbc = kzalloc(sizeof(struct drbd_backing_dev), GFP_KERNEL);
1313 retcode = ERR_NOMEM;
1316 spin_lock_init(&nbc->md.uuid_lock);
1318 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
1319 if (!new_disk_conf) {
1320 retcode = ERR_NOMEM;
1323 nbc->disk_conf = new_disk_conf;
1325 set_disk_conf_defaults(new_disk_conf);
1326 err = disk_conf_from_attrs(new_disk_conf, info);
1328 retcode = ERR_MANDATORY_TAG;
1329 drbd_msg_put_info(from_attrs_err_to_txt(err));
1333 enforce_disk_conf_limits(new_disk_conf);
1335 new_plan = fifo_alloc((new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ);
1337 retcode = ERR_NOMEM;
1341 if (new_disk_conf->meta_dev_idx < DRBD_MD_INDEX_FLEX_INT) {
1342 retcode = ERR_MD_IDX_INVALID;
1347 nc = rcu_dereference(mdev->tconn->net_conf);
1349 if (new_disk_conf->fencing == FP_STONITH && nc->wire_protocol == DRBD_PROT_A) {
1351 retcode = ERR_STONITH_AND_PROT_A;
1357 bdev = blkdev_get_by_path(new_disk_conf->backing_dev,
1358 FMODE_READ | FMODE_WRITE | FMODE_EXCL, mdev);
1360 dev_err(DEV, "open(\"%s\") failed with %ld\n", new_disk_conf->backing_dev,
1362 retcode = ERR_OPEN_DISK;
1365 nbc->backing_bdev = bdev;
1368 * meta_dev_idx >= 0: external fixed size, possibly multiple
1369 * drbd sharing one meta device. TODO in that case, paranoia
1370 * check that [md_bdev, meta_dev_idx] is not yet used by some
1371 * other drbd minor! (if you use drbd.conf + drbdadm, that
1372 * should check it for you already; but if you don't, or
1373 * someone fooled it, we need to double check here)
1375 bdev = blkdev_get_by_path(new_disk_conf->meta_dev,
1376 FMODE_READ | FMODE_WRITE | FMODE_EXCL,
1377 (new_disk_conf->meta_dev_idx < 0) ?
1378 (void *)mdev : (void *)drbd_m_holder);
1380 dev_err(DEV, "open(\"%s\") failed with %ld\n", new_disk_conf->meta_dev,
1382 retcode = ERR_OPEN_MD_DISK;
1385 nbc->md_bdev = bdev;
1387 if ((nbc->backing_bdev == nbc->md_bdev) !=
1388 (new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_INTERNAL ||
1389 new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_FLEX_INT)) {
1390 retcode = ERR_MD_IDX_INVALID;
1394 resync_lru = lc_create("resync", drbd_bm_ext_cache,
1395 1, 61, sizeof(struct bm_extent),
1396 offsetof(struct bm_extent, lce));
1398 retcode = ERR_NOMEM;
1402 /* RT - for drbd_get_max_capacity() DRBD_MD_INDEX_FLEX_INT */
1403 drbd_md_set_sector_offsets(mdev, nbc);
1405 if (drbd_get_max_capacity(nbc) < new_disk_conf->disk_size) {
1406 dev_err(DEV, "max capacity %llu smaller than disk size %llu\n",
1407 (unsigned long long) drbd_get_max_capacity(nbc),
1408 (unsigned long long) new_disk_conf->disk_size);
1409 retcode = ERR_DISK_TOO_SMALL;
1413 if (new_disk_conf->meta_dev_idx < 0) {
1414 max_possible_sectors = DRBD_MAX_SECTORS_FLEX;
1415 /* at least one MB, otherwise it does not make sense */
1416 min_md_device_sectors = (2<<10);
1418 max_possible_sectors = DRBD_MAX_SECTORS;
1419 min_md_device_sectors = MD_RESERVED_SECT * (new_disk_conf->meta_dev_idx + 1);
1422 if (drbd_get_capacity(nbc->md_bdev) < min_md_device_sectors) {
1423 retcode = ERR_MD_DISK_TOO_SMALL;
1424 dev_warn(DEV, "refusing attach: md-device too small, "
1425 "at least %llu sectors needed for this meta-disk type\n",
1426 (unsigned long long) min_md_device_sectors);
1430 /* Make sure the new disk is big enough
1431 * (we may currently be R_PRIMARY with no local disk...) */
1432 if (drbd_get_max_capacity(nbc) <
1433 drbd_get_capacity(mdev->this_bdev)) {
1434 retcode = ERR_DISK_TOO_SMALL;
1438 nbc->known_size = drbd_get_capacity(nbc->backing_bdev);
1440 if (nbc->known_size > max_possible_sectors) {
1441 dev_warn(DEV, "==> truncating very big lower level device "
1442 "to currently maximum possible %llu sectors <==\n",
1443 (unsigned long long) max_possible_sectors);
1444 if (new_disk_conf->meta_dev_idx >= 0)
1445 dev_warn(DEV, "==>> using internal or flexible "
1446 "meta data may help <<==\n");
1449 drbd_suspend_io(mdev);
1450 /* also wait for the last barrier ack. */
1451 /* FIXME see also https://daiquiri.linbit/cgi-bin/bugzilla/show_bug.cgi?id=171
1452 * We need a way to either ignore barrier acks for barriers sent before a device
1453 * was attached, or a way to wait for all pending barrier acks to come in.
1454 * As barriers are counted per resource,
1455 * we'd need to suspend io on all devices of a resource.
1457 wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_pending_cnt) || drbd_suspended(mdev));
1458 /* and for any other previously queued work */
1459 drbd_flush_workqueue(mdev);
1461 rv = _drbd_request_state(mdev, NS(disk, D_ATTACHING), CS_VERBOSE);
1462 retcode = rv; /* FIXME: Type mismatch. */
1463 drbd_resume_io(mdev);
1464 if (rv < SS_SUCCESS)
1467 if (!get_ldev_if_state(mdev, D_ATTACHING))
1468 goto force_diskless;
1470 drbd_md_set_sector_offsets(mdev, nbc);
1472 if (!mdev->bitmap) {
1473 if (drbd_bm_init(mdev)) {
1474 retcode = ERR_NOMEM;
1475 goto force_diskless_dec;
1479 retcode = drbd_md_read(mdev, nbc);
1480 if (retcode != NO_ERROR)
1481 goto force_diskless_dec;
1483 if (mdev->state.conn < C_CONNECTED &&
1484 mdev->state.role == R_PRIMARY &&
1485 (mdev->ed_uuid & ~((u64)1)) != (nbc->md.uuid[UI_CURRENT] & ~((u64)1))) {
1486 dev_err(DEV, "Can only attach to data with current UUID=%016llX\n",
1487 (unsigned long long)mdev->ed_uuid);
1488 retcode = ERR_DATA_NOT_CURRENT;
1489 goto force_diskless_dec;
1492 /* Since we are diskless, fix the activity log first... */
1493 if (drbd_check_al_size(mdev, new_disk_conf)) {
1494 retcode = ERR_NOMEM;
1495 goto force_diskless_dec;
1498 /* Prevent shrinking of consistent devices ! */
1499 if (drbd_md_test_flag(nbc, MDF_CONSISTENT) &&
1500 drbd_new_dev_size(mdev, nbc, nbc->disk_conf->disk_size, 0) < nbc->md.la_size_sect) {
1501 dev_warn(DEV, "refusing to truncate a consistent device\n");
1502 retcode = ERR_DISK_TOO_SMALL;
1503 goto force_diskless_dec;
1506 /* Reset the "barriers don't work" bits here, then force meta data to
1507 * be written, to ensure we determine if barriers are supported. */
1508 if (new_disk_conf->md_flushes)
1509 clear_bit(MD_NO_FUA, &mdev->flags);
1511 set_bit(MD_NO_FUA, &mdev->flags);
1513 /* Point of no return reached.
1514 * Devices and memory are no longer released by error cleanup below.
1515 * now mdev takes over responsibility, and the state engine should
1516 * clean it up somewhere. */
1517 D_ASSERT(mdev->ldev == NULL);
1519 mdev->resync = resync_lru;
1520 mdev->rs_plan_s = new_plan;
1523 new_disk_conf = NULL;
1526 drbd_bump_write_ordering(mdev->tconn, WO_bdev_flush);
1528 if (drbd_md_test_flag(mdev->ldev, MDF_CRASHED_PRIMARY))
1529 set_bit(CRASHED_PRIMARY, &mdev->flags);
1531 clear_bit(CRASHED_PRIMARY, &mdev->flags);
1533 if (drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) &&
1534 !(mdev->state.role == R_PRIMARY && mdev->tconn->susp_nod))
1535 set_bit(CRASHED_PRIMARY, &mdev->flags);
1542 drbd_reconsider_max_bio_size(mdev);
1544 /* If I am currently not R_PRIMARY,
1545 * but meta data primary indicator is set,
1546 * I just now recover from a hard crash,
1547 * and have been R_PRIMARY before that crash.
1549 * Now, if I had no connection before that crash
1550 * (have been degraded R_PRIMARY), chances are that
1551 * I won't find my peer now either.
1553 * In that case, and _only_ in that case,
1554 * we use the degr-wfc-timeout instead of the default,
1555 * so we can automatically recover from a crash of a
1556 * degraded but active "cluster" after a certain timeout.
1558 clear_bit(USE_DEGR_WFC_T, &mdev->flags);
1559 if (mdev->state.role != R_PRIMARY &&
1560 drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) &&
1561 !drbd_md_test_flag(mdev->ldev, MDF_CONNECTED_IND))
1562 set_bit(USE_DEGR_WFC_T, &mdev->flags);
1564 dd = drbd_determine_dev_size(mdev, 0);
1565 if (dd == dev_size_error) {
1566 retcode = ERR_NOMEM_BITMAP;
1567 goto force_diskless_dec;
1568 } else if (dd == grew)
1569 set_bit(RESYNC_AFTER_NEG, &mdev->flags);
1571 if (drbd_md_test_flag(mdev->ldev, MDF_FULL_SYNC) ||
1572 (test_bit(CRASHED_PRIMARY, &mdev->flags) &&
1573 drbd_md_test_flag(mdev->ldev, MDF_AL_DISABLED))) {
1574 dev_info(DEV, "Assuming that all blocks are out of sync "
1575 "(aka FullSync)\n");
1576 if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write,
1577 "set_n_write from attaching", BM_LOCKED_MASK)) {
1578 retcode = ERR_IO_MD_DISK;
1579 goto force_diskless_dec;
1582 if (drbd_bitmap_io(mdev, &drbd_bm_read,
1583 "read from attaching", BM_LOCKED_MASK)) {
1584 retcode = ERR_IO_MD_DISK;
1585 goto force_diskless_dec;
1589 if (_drbd_bm_total_weight(mdev) == drbd_bm_bits(mdev))
1590 drbd_suspend_al(mdev); /* IO is still suspended here... */
1592 spin_lock_irq(&mdev->tconn->req_lock);
1593 os = drbd_read_state(mdev);
1595 /* If MDF_CONSISTENT is not set go into inconsistent state,
1596 otherwise investigate MDF_WasUpToDate...
1597 If MDF_WAS_UP_TO_DATE is not set go into D_OUTDATED disk state,
1598 otherwise into D_CONSISTENT state.
1600 if (drbd_md_test_flag(mdev->ldev, MDF_CONSISTENT)) {
1601 if (drbd_md_test_flag(mdev->ldev, MDF_WAS_UP_TO_DATE))
1602 ns.disk = D_CONSISTENT;
1604 ns.disk = D_OUTDATED;
1606 ns.disk = D_INCONSISTENT;
1609 if (drbd_md_test_flag(mdev->ldev, MDF_PEER_OUT_DATED))
1610 ns.pdsk = D_OUTDATED;
1613 if (ns.disk == D_CONSISTENT &&
1614 (ns.pdsk == D_OUTDATED || rcu_dereference(mdev->ldev->disk_conf)->fencing == FP_DONT_CARE))
1615 ns.disk = D_UP_TO_DATE;
1617 /* All tests on MDF_PRIMARY_IND, MDF_CONNECTED_IND,
1618 MDF_CONSISTENT and MDF_WAS_UP_TO_DATE must happen before
1619 this point, because drbd_request_state() modifies these
1622 if (rcu_dereference(mdev->ldev->disk_conf)->al_updates)
1623 mdev->ldev->md.flags &= ~MDF_AL_DISABLED;
1625 mdev->ldev->md.flags |= MDF_AL_DISABLED;
1629 /* In case we are C_CONNECTED postpone any decision on the new disk
1630 state after the negotiation phase. */
1631 if (mdev->state.conn == C_CONNECTED) {
1632 mdev->new_state_tmp.i = ns.i;
1634 ns.disk = D_NEGOTIATING;
1636 /* We expect to receive up-to-date UUIDs soon.
1637 To avoid a race in receive_state, free p_uuid while
1638 holding req_lock. I.e. atomic with the state change */
1639 kfree(mdev->p_uuid);
1640 mdev->p_uuid = NULL;
1643 rv = _drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
1644 spin_unlock_irq(&mdev->tconn->req_lock);
1646 if (rv < SS_SUCCESS)
1647 goto force_diskless_dec;
1649 mod_timer(&mdev->request_timer, jiffies + HZ);
1651 if (mdev->state.role == R_PRIMARY)
1652 mdev->ldev->md.uuid[UI_CURRENT] |= (u64)1;
1654 mdev->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
1656 drbd_md_mark_dirty(mdev);
1659 kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
1661 conn_reconfig_done(mdev->tconn);
1662 drbd_adm_finish(info, retcode);
1668 drbd_force_state(mdev, NS(disk, D_DISKLESS));
1671 conn_reconfig_done(mdev->tconn);
1673 if (nbc->backing_bdev)
1674 blkdev_put(nbc->backing_bdev,
1675 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1677 blkdev_put(nbc->md_bdev,
1678 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1681 kfree(new_disk_conf);
1682 lc_destroy(resync_lru);
1686 drbd_adm_finish(info, retcode);
1690 static int adm_detach(struct drbd_conf *mdev, int force)
1692 enum drbd_state_rv retcode;
1696 set_bit(FORCE_DETACH, &mdev->flags);
1697 drbd_force_state(mdev, NS(disk, D_FAILED));
1698 retcode = SS_SUCCESS;
1702 drbd_suspend_io(mdev); /* so no-one is stuck in drbd_al_begin_io */
1703 drbd_md_get_buffer(mdev); /* make sure there is no in-flight meta-data IO */
1704 retcode = drbd_request_state(mdev, NS(disk, D_FAILED));
1705 drbd_md_put_buffer(mdev);
1706 /* D_FAILED will transition to DISKLESS. */
1707 ret = wait_event_interruptible(mdev->misc_wait,
1708 mdev->state.disk != D_FAILED);
1709 drbd_resume_io(mdev);
1710 if ((int)retcode == (int)SS_IS_DISKLESS)
1711 retcode = SS_NOTHING_TO_DO;
1718 /* Detaching the disk is a process in multiple stages. First we need to lock
1719 * out application IO, in-flight IO, IO stuck in drbd_al_begin_io.
1720 * Then we transition to D_DISKLESS, and wait for put_ldev() to return all
1721 * internal references as well.
1722 * Only then we have finally detached. */
1723 int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info)
1725 enum drbd_ret_code retcode;
1726 struct detach_parms parms = { };
1729 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
1730 if (!adm_ctx.reply_skb)
1732 if (retcode != NO_ERROR)
1735 if (info->attrs[DRBD_NLA_DETACH_PARMS]) {
1736 err = detach_parms_from_attrs(&parms, info);
1738 retcode = ERR_MANDATORY_TAG;
1739 drbd_msg_put_info(from_attrs_err_to_txt(err));
1744 retcode = adm_detach(adm_ctx.mdev, parms.force_detach);
1746 drbd_adm_finish(info, retcode);
1750 static bool conn_resync_running(struct drbd_tconn *tconn)
1752 struct drbd_conf *mdev;
1757 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1758 if (mdev->state.conn == C_SYNC_SOURCE ||
1759 mdev->state.conn == C_SYNC_TARGET ||
1760 mdev->state.conn == C_PAUSED_SYNC_S ||
1761 mdev->state.conn == C_PAUSED_SYNC_T) {
1771 static bool conn_ov_running(struct drbd_tconn *tconn)
1773 struct drbd_conf *mdev;
1778 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1779 if (mdev->state.conn == C_VERIFY_S ||
1780 mdev->state.conn == C_VERIFY_T) {
1790 static enum drbd_ret_code
1791 _check_net_options(struct drbd_tconn *tconn, struct net_conf *old_conf, struct net_conf *new_conf)
1793 struct drbd_conf *mdev;
1796 if (old_conf && tconn->cstate == C_WF_REPORT_PARAMS && tconn->agreed_pro_version < 100) {
1797 if (new_conf->wire_protocol != old_conf->wire_protocol)
1798 return ERR_NEED_APV_100;
1800 if (new_conf->two_primaries != old_conf->two_primaries)
1801 return ERR_NEED_APV_100;
1803 if (strcmp(new_conf->integrity_alg, old_conf->integrity_alg))
1804 return ERR_NEED_APV_100;
1807 if (!new_conf->two_primaries &&
1808 conn_highest_role(tconn) == R_PRIMARY &&
1809 conn_highest_peer(tconn) == R_PRIMARY)
1810 return ERR_NEED_ALLOW_TWO_PRI;
1812 if (new_conf->two_primaries &&
1813 (new_conf->wire_protocol != DRBD_PROT_C))
1814 return ERR_NOT_PROTO_C;
1816 idr_for_each_entry(&tconn->volumes, mdev, i) {
1817 if (get_ldev(mdev)) {
1818 enum drbd_fencing_p fp = rcu_dereference(mdev->ldev->disk_conf)->fencing;
1820 if (new_conf->wire_protocol == DRBD_PROT_A && fp == FP_STONITH)
1821 return ERR_STONITH_AND_PROT_A;
1823 if (mdev->state.role == R_PRIMARY && new_conf->discard_my_data)
1824 return ERR_DISCARD_IMPOSSIBLE;
1827 if (new_conf->on_congestion != OC_BLOCK && new_conf->wire_protocol != DRBD_PROT_A)
1828 return ERR_CONG_NOT_PROTO_A;
1833 static enum drbd_ret_code
1834 check_net_options(struct drbd_tconn *tconn, struct net_conf *new_conf)
1836 static enum drbd_ret_code rv;
1837 struct drbd_conf *mdev;
1841 rv = _check_net_options(tconn, rcu_dereference(tconn->net_conf), new_conf);
1844 /* tconn->volumes protected by genl_lock() here */
1845 idr_for_each_entry(&tconn->volumes, mdev, i) {
1846 if (!mdev->bitmap) {
1847 if(drbd_bm_init(mdev))
1856 struct crypto_hash *verify_tfm;
1857 struct crypto_hash *csums_tfm;
1858 struct crypto_hash *cram_hmac_tfm;
1859 struct crypto_hash *integrity_tfm;
1863 alloc_hash(struct crypto_hash **tfm, char *tfm_name, int err_alg)
1868 *tfm = crypto_alloc_hash(tfm_name, 0, CRYPTO_ALG_ASYNC);
1877 static enum drbd_ret_code
1878 alloc_crypto(struct crypto *crypto, struct net_conf *new_conf)
1880 char hmac_name[CRYPTO_MAX_ALG_NAME];
1881 enum drbd_ret_code rv;
1883 rv = alloc_hash(&crypto->csums_tfm, new_conf->csums_alg,
1887 rv = alloc_hash(&crypto->verify_tfm, new_conf->verify_alg,
1891 rv = alloc_hash(&crypto->integrity_tfm, new_conf->integrity_alg,
1895 if (new_conf->cram_hmac_alg[0] != 0) {
1896 snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)",
1897 new_conf->cram_hmac_alg);
1899 rv = alloc_hash(&crypto->cram_hmac_tfm, hmac_name,
1906 static void free_crypto(struct crypto *crypto)
1908 crypto_free_hash(crypto->cram_hmac_tfm);
1909 crypto_free_hash(crypto->integrity_tfm);
1910 crypto_free_hash(crypto->csums_tfm);
1911 crypto_free_hash(crypto->verify_tfm);
1914 int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info)
1916 enum drbd_ret_code retcode;
1917 struct drbd_tconn *tconn;
1918 struct net_conf *old_conf, *new_conf = NULL;
1920 int ovr; /* online verify running */
1921 int rsr; /* re-sync running */
1922 struct crypto crypto = { };
1924 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONNECTION);
1925 if (!adm_ctx.reply_skb)
1927 if (retcode != NO_ERROR)
1930 tconn = adm_ctx.tconn;
1932 new_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
1934 retcode = ERR_NOMEM;
1938 conn_reconfig_start(tconn);
1940 mutex_lock(&tconn->data.mutex);
1941 mutex_lock(&tconn->conf_update);
1942 old_conf = tconn->net_conf;
1945 drbd_msg_put_info("net conf missing, try connect");
1946 retcode = ERR_INVALID_REQUEST;
1950 *new_conf = *old_conf;
1951 if (should_set_defaults(info))
1952 set_net_conf_defaults(new_conf);
1954 err = net_conf_from_attrs_for_change(new_conf, info);
1955 if (err && err != -ENOMSG) {
1956 retcode = ERR_MANDATORY_TAG;
1957 drbd_msg_put_info(from_attrs_err_to_txt(err));
1961 retcode = check_net_options(tconn, new_conf);
1962 if (retcode != NO_ERROR)
1965 /* re-sync running */
1966 rsr = conn_resync_running(tconn);
1967 if (rsr && strcmp(new_conf->csums_alg, old_conf->csums_alg)) {
1968 retcode = ERR_CSUMS_RESYNC_RUNNING;
1972 /* online verify running */
1973 ovr = conn_ov_running(tconn);
1974 if (ovr && strcmp(new_conf->verify_alg, old_conf->verify_alg)) {
1975 retcode = ERR_VERIFY_RUNNING;
1979 retcode = alloc_crypto(&crypto, new_conf);
1980 if (retcode != NO_ERROR)
1983 rcu_assign_pointer(tconn->net_conf, new_conf);
1986 crypto_free_hash(tconn->csums_tfm);
1987 tconn->csums_tfm = crypto.csums_tfm;
1988 crypto.csums_tfm = NULL;
1991 crypto_free_hash(tconn->verify_tfm);
1992 tconn->verify_tfm = crypto.verify_tfm;
1993 crypto.verify_tfm = NULL;
1996 crypto_free_hash(tconn->integrity_tfm);
1997 tconn->integrity_tfm = crypto.integrity_tfm;
1998 if (tconn->cstate >= C_WF_REPORT_PARAMS && tconn->agreed_pro_version >= 100)
1999 /* Do this without trying to take tconn->data.mutex again. */
2000 __drbd_send_protocol(tconn, P_PROTOCOL_UPDATE);
2002 crypto_free_hash(tconn->cram_hmac_tfm);
2003 tconn->cram_hmac_tfm = crypto.cram_hmac_tfm;
2005 mutex_unlock(&tconn->conf_update);
2006 mutex_unlock(&tconn->data.mutex);
2010 if (tconn->cstate >= C_WF_REPORT_PARAMS)
2011 drbd_send_sync_param(minor_to_mdev(conn_lowest_minor(tconn)));
2016 mutex_unlock(&tconn->conf_update);
2017 mutex_unlock(&tconn->data.mutex);
2018 free_crypto(&crypto);
2021 conn_reconfig_done(tconn);
2023 drbd_adm_finish(info, retcode);
2027 int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info)
2029 struct drbd_conf *mdev;
2030 struct net_conf *old_conf, *new_conf = NULL;
2031 struct crypto crypto = { };
2032 struct drbd_tconn *tconn;
2033 enum drbd_ret_code retcode;
2037 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
2039 if (!adm_ctx.reply_skb)
2041 if (retcode != NO_ERROR)
2043 if (!(adm_ctx.my_addr && adm_ctx.peer_addr)) {
2044 drbd_msg_put_info("connection endpoint(s) missing");
2045 retcode = ERR_INVALID_REQUEST;
2049 /* No need for _rcu here. All reconfiguration is
2050 * strictly serialized on genl_lock(). We are protected against
2051 * concurrent reconfiguration/addition/deletion */
2052 list_for_each_entry(tconn, &drbd_tconns, all_tconn) {
2053 if (nla_len(adm_ctx.my_addr) == tconn->my_addr_len &&
2054 !memcmp(nla_data(adm_ctx.my_addr), &tconn->my_addr, tconn->my_addr_len)) {
2055 retcode = ERR_LOCAL_ADDR;
2059 if (nla_len(adm_ctx.peer_addr) == tconn->peer_addr_len &&
2060 !memcmp(nla_data(adm_ctx.peer_addr), &tconn->peer_addr, tconn->peer_addr_len)) {
2061 retcode = ERR_PEER_ADDR;
2066 tconn = adm_ctx.tconn;
2067 conn_reconfig_start(tconn);
2069 if (tconn->cstate > C_STANDALONE) {
2070 retcode = ERR_NET_CONFIGURED;
2074 /* allocation not in the IO path, drbdsetup / netlink process context */
2075 new_conf = kzalloc(sizeof(*new_conf), GFP_KERNEL);
2077 retcode = ERR_NOMEM;
2081 set_net_conf_defaults(new_conf);
2083 err = net_conf_from_attrs(new_conf, info);
2084 if (err && err != -ENOMSG) {
2085 retcode = ERR_MANDATORY_TAG;
2086 drbd_msg_put_info(from_attrs_err_to_txt(err));
2090 retcode = check_net_options(tconn, new_conf);
2091 if (retcode != NO_ERROR)
2094 retcode = alloc_crypto(&crypto, new_conf);
2095 if (retcode != NO_ERROR)
2098 ((char *)new_conf->shared_secret)[SHARED_SECRET_MAX-1] = 0;
2100 conn_flush_workqueue(tconn);
2102 mutex_lock(&tconn->conf_update);
2103 old_conf = tconn->net_conf;
2105 retcode = ERR_NET_CONFIGURED;
2106 mutex_unlock(&tconn->conf_update);
2109 rcu_assign_pointer(tconn->net_conf, new_conf);
2111 conn_free_crypto(tconn);
2112 tconn->cram_hmac_tfm = crypto.cram_hmac_tfm;
2113 tconn->integrity_tfm = crypto.integrity_tfm;
2114 tconn->csums_tfm = crypto.csums_tfm;
2115 tconn->verify_tfm = crypto.verify_tfm;
2117 tconn->my_addr_len = nla_len(adm_ctx.my_addr);
2118 memcpy(&tconn->my_addr, nla_data(adm_ctx.my_addr), tconn->my_addr_len);
2119 tconn->peer_addr_len = nla_len(adm_ctx.peer_addr);
2120 memcpy(&tconn->peer_addr, nla_data(adm_ctx.peer_addr), tconn->peer_addr_len);
2122 mutex_unlock(&tconn->conf_update);
2125 idr_for_each_entry(&tconn->volumes, mdev, i) {
2131 retcode = conn_request_state(tconn, NS(conn, C_UNCONNECTED), CS_VERBOSE);
2133 conn_reconfig_done(tconn);
2134 drbd_adm_finish(info, retcode);
2138 free_crypto(&crypto);
2141 conn_reconfig_done(tconn);
2143 drbd_adm_finish(info, retcode);
2147 static enum drbd_state_rv conn_try_disconnect(struct drbd_tconn *tconn, bool force)
2149 enum drbd_state_rv rv;
2151 rv = conn_request_state(tconn, NS(conn, C_DISCONNECTING),
2152 force ? CS_HARD : 0);
2155 case SS_NOTHING_TO_DO:
2157 case SS_ALREADY_STANDALONE:
2159 case SS_PRIMARY_NOP:
2160 /* Our state checking code wants to see the peer outdated. */
2161 rv = conn_request_state(tconn, NS2(conn, C_DISCONNECTING,
2162 pdsk, D_OUTDATED), CS_VERBOSE);
2164 case SS_CW_FAILED_BY_PEER:
2165 /* The peer probably wants to see us outdated. */
2166 rv = conn_request_state(tconn, NS2(conn, C_DISCONNECTING,
2167 disk, D_OUTDATED), 0);
2168 if (rv == SS_IS_DISKLESS || rv == SS_LOWER_THAN_OUTDATED) {
2169 rv = conn_request_state(tconn, NS(conn, C_DISCONNECTING),
2174 /* no special handling necessary */
2177 if (rv >= SS_SUCCESS) {
2178 enum drbd_state_rv rv2;
2179 /* No one else can reconfigure the network while I am here.
2180 * The state handling only uses drbd_thread_stop_nowait(),
2181 * we want to really wait here until the receiver is no more.
2183 drbd_thread_stop(&adm_ctx.tconn->receiver);
2185 /* Race breaker. This additional state change request may be
2186 * necessary, if this was a forced disconnect during a receiver
2187 * restart. We may have "killed" the receiver thread just
2188 * after drbdd_init() returned. Typically, we should be
2189 * C_STANDALONE already, now, and this becomes a no-op.
2191 rv2 = conn_request_state(tconn, NS(conn, C_STANDALONE),
2192 CS_VERBOSE | CS_HARD);
2193 if (rv2 < SS_SUCCESS)
2195 "unexpected rv2=%d in conn_try_disconnect()\n",
2201 int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info)
2203 struct disconnect_parms parms;
2204 struct drbd_tconn *tconn;
2205 enum drbd_state_rv rv;
2206 enum drbd_ret_code retcode;
2209 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONNECTION);
2210 if (!adm_ctx.reply_skb)
2212 if (retcode != NO_ERROR)
2215 tconn = adm_ctx.tconn;
2216 memset(&parms, 0, sizeof(parms));
2217 if (info->attrs[DRBD_NLA_DISCONNECT_PARMS]) {
2218 err = disconnect_parms_from_attrs(&parms, info);
2220 retcode = ERR_MANDATORY_TAG;
2221 drbd_msg_put_info(from_attrs_err_to_txt(err));
2226 rv = conn_try_disconnect(tconn, parms.force_disconnect);
2227 if (rv < SS_SUCCESS)
2228 retcode = rv; /* FIXME: Type mismatch. */
2232 drbd_adm_finish(info, retcode);
2236 void resync_after_online_grow(struct drbd_conf *mdev)
2238 int iass; /* I am sync source */
2240 dev_info(DEV, "Resync of new storage after online grow\n");
2241 if (mdev->state.role != mdev->state.peer)
2242 iass = (mdev->state.role == R_PRIMARY);
2244 iass = test_bit(RESOLVE_CONFLICTS, &mdev->tconn->flags);
2247 drbd_start_resync(mdev, C_SYNC_SOURCE);
2249 _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE + CS_SERIALIZE);
2252 int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info)
2254 struct disk_conf *old_disk_conf, *new_disk_conf = NULL;
2255 struct resize_parms rs;
2256 struct drbd_conf *mdev;
2257 enum drbd_ret_code retcode;
2258 enum determine_dev_size dd;
2259 enum dds_flags ddsf;
2263 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2264 if (!adm_ctx.reply_skb)
2266 if (retcode != NO_ERROR)
2269 memset(&rs, 0, sizeof(struct resize_parms));
2270 if (info->attrs[DRBD_NLA_RESIZE_PARMS]) {
2271 err = resize_parms_from_attrs(&rs, info);
2273 retcode = ERR_MANDATORY_TAG;
2274 drbd_msg_put_info(from_attrs_err_to_txt(err));
2279 mdev = adm_ctx.mdev;
2280 if (mdev->state.conn > C_CONNECTED) {
2281 retcode = ERR_RESIZE_RESYNC;
2285 if (mdev->state.role == R_SECONDARY &&
2286 mdev->state.peer == R_SECONDARY) {
2287 retcode = ERR_NO_PRIMARY;
2291 if (!get_ldev(mdev)) {
2292 retcode = ERR_NO_DISK;
2296 if (rs.no_resync && mdev->tconn->agreed_pro_version < 93) {
2297 retcode = ERR_NEED_APV_93;
2302 u_size = rcu_dereference(mdev->ldev->disk_conf)->disk_size;
2304 if (u_size != (sector_t)rs.resize_size) {
2305 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
2306 if (!new_disk_conf) {
2307 retcode = ERR_NOMEM;
2312 if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev))
2313 mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev);
2315 if (new_disk_conf) {
2316 mutex_lock(&mdev->tconn->conf_update);
2317 old_disk_conf = mdev->ldev->disk_conf;
2318 *new_disk_conf = *old_disk_conf;
2319 new_disk_conf->disk_size = (sector_t)rs.resize_size;
2320 rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
2321 mutex_unlock(&mdev->tconn->conf_update);
2323 kfree(old_disk_conf);
2326 ddsf = (rs.resize_force ? DDSF_FORCED : 0) | (rs.no_resync ? DDSF_NO_RESYNC : 0);
2327 dd = drbd_determine_dev_size(mdev, ddsf);
2330 if (dd == dev_size_error) {
2331 retcode = ERR_NOMEM_BITMAP;
2335 if (mdev->state.conn == C_CONNECTED) {
2337 set_bit(RESIZE_PENDING, &mdev->flags);
2339 drbd_send_uuids(mdev);
2340 drbd_send_sizes(mdev, 1, ddsf);
2344 drbd_adm_finish(info, retcode);
2352 int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info)
2354 enum drbd_ret_code retcode;
2355 struct drbd_tconn *tconn;
2356 struct res_opts res_opts;
2359 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
2360 if (!adm_ctx.reply_skb)
2362 if (retcode != NO_ERROR)
2364 tconn = adm_ctx.tconn;
2366 res_opts = tconn->res_opts;
2367 if (should_set_defaults(info))
2368 set_res_opts_defaults(&res_opts);
2370 err = res_opts_from_attrs(&res_opts, info);
2371 if (err && err != -ENOMSG) {
2372 retcode = ERR_MANDATORY_TAG;
2373 drbd_msg_put_info(from_attrs_err_to_txt(err));
2377 err = set_resource_options(tconn, &res_opts);
2379 retcode = ERR_INVALID_REQUEST;
2381 retcode = ERR_NOMEM;
2385 drbd_adm_finish(info, retcode);
2389 int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info)
2391 struct drbd_conf *mdev;
2392 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2394 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2395 if (!adm_ctx.reply_skb)
2397 if (retcode != NO_ERROR)
2400 mdev = adm_ctx.mdev;
2402 /* If there is still bitmap IO pending, probably because of a previous
2403 * resync just being finished, wait for it before requesting a new resync.
2404 * Also wait for it's after_state_ch(). */
2405 drbd_suspend_io(mdev);
2406 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
2407 drbd_flush_workqueue(mdev);
2409 retcode = _drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T), CS_ORDERED);
2411 if (retcode < SS_SUCCESS && retcode != SS_NEED_CONNECTION)
2412 retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T));
2414 while (retcode == SS_NEED_CONNECTION) {
2415 spin_lock_irq(&mdev->tconn->req_lock);
2416 if (mdev->state.conn < C_CONNECTED)
2417 retcode = _drbd_set_state(_NS(mdev, disk, D_INCONSISTENT), CS_VERBOSE, NULL);
2418 spin_unlock_irq(&mdev->tconn->req_lock);
2420 if (retcode != SS_NEED_CONNECTION)
2423 retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T));
2425 drbd_resume_io(mdev);
2428 drbd_adm_finish(info, retcode);
2432 static int drbd_adm_simple_request_state(struct sk_buff *skb, struct genl_info *info,
2433 union drbd_state mask, union drbd_state val)
2435 enum drbd_ret_code retcode;
2437 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2438 if (!adm_ctx.reply_skb)
2440 if (retcode != NO_ERROR)
2443 retcode = drbd_request_state(adm_ctx.mdev, mask, val);
2445 drbd_adm_finish(info, retcode);
2449 static int drbd_bmio_set_susp_al(struct drbd_conf *mdev)
2453 rv = drbd_bmio_set_n_write(mdev);
2454 drbd_suspend_al(mdev);
2458 int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info)
2460 int retcode; /* drbd_ret_code, drbd_state_rv */
2461 struct drbd_conf *mdev;
2463 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2464 if (!adm_ctx.reply_skb)
2466 if (retcode != NO_ERROR)
2469 mdev = adm_ctx.mdev;
2471 /* If there is still bitmap IO pending, probably because of a previous
2472 * resync just being finished, wait for it before requesting a new resync.
2473 * Also wait for it's after_state_ch(). */
2474 drbd_suspend_io(mdev);
2475 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
2476 drbd_flush_workqueue(mdev);
2478 retcode = _drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_S), CS_ORDERED);
2479 if (retcode < SS_SUCCESS) {
2480 if (retcode == SS_NEED_CONNECTION && mdev->state.role == R_PRIMARY) {
2481 /* The peer will get a resync upon connect anyways.
2482 * Just make that into a full resync. */
2483 retcode = drbd_request_state(mdev, NS(pdsk, D_INCONSISTENT));
2484 if (retcode >= SS_SUCCESS) {
2485 if (drbd_bitmap_io(mdev, &drbd_bmio_set_susp_al,
2486 "set_n_write from invalidate_peer",
2487 BM_LOCKED_SET_ALLOWED))
2488 retcode = ERR_IO_MD_DISK;
2491 retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_S));
2493 drbd_resume_io(mdev);
2496 drbd_adm_finish(info, retcode);
2500 int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info)
2502 enum drbd_ret_code retcode;
2504 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2505 if (!adm_ctx.reply_skb)
2507 if (retcode != NO_ERROR)
2510 if (drbd_request_state(adm_ctx.mdev, NS(user_isp, 1)) == SS_NOTHING_TO_DO)
2511 retcode = ERR_PAUSE_IS_SET;
2513 drbd_adm_finish(info, retcode);
2517 int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info)
2519 union drbd_dev_state s;
2520 enum drbd_ret_code retcode;
2522 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2523 if (!adm_ctx.reply_skb)
2525 if (retcode != NO_ERROR)
2528 if (drbd_request_state(adm_ctx.mdev, NS(user_isp, 0)) == SS_NOTHING_TO_DO) {
2529 s = adm_ctx.mdev->state;
2530 if (s.conn == C_PAUSED_SYNC_S || s.conn == C_PAUSED_SYNC_T) {
2531 retcode = s.aftr_isp ? ERR_PIC_AFTER_DEP :
2532 s.peer_isp ? ERR_PIC_PEER_DEP : ERR_PAUSE_IS_CLEAR;
2534 retcode = ERR_PAUSE_IS_CLEAR;
2539 drbd_adm_finish(info, retcode);
2543 int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info)
2545 return drbd_adm_simple_request_state(skb, info, NS(susp, 1));
2548 int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info)
2550 struct drbd_conf *mdev;
2551 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2553 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2554 if (!adm_ctx.reply_skb)
2556 if (retcode != NO_ERROR)
2559 mdev = adm_ctx.mdev;
2560 if (test_bit(NEW_CUR_UUID, &mdev->flags)) {
2561 drbd_uuid_new_current(mdev);
2562 clear_bit(NEW_CUR_UUID, &mdev->flags);
2564 drbd_suspend_io(mdev);
2565 retcode = drbd_request_state(mdev, NS3(susp, 0, susp_nod, 0, susp_fen, 0));
2566 if (retcode == SS_SUCCESS) {
2567 if (mdev->state.conn < C_CONNECTED)
2568 tl_clear(mdev->tconn);
2569 if (mdev->state.disk == D_DISKLESS || mdev->state.disk == D_FAILED)
2570 tl_restart(mdev->tconn, FAIL_FROZEN_DISK_IO);
2572 drbd_resume_io(mdev);
2575 drbd_adm_finish(info, retcode);
2579 int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info)
2581 return drbd_adm_simple_request_state(skb, info, NS(disk, D_OUTDATED));
2584 int nla_put_drbd_cfg_context(struct sk_buff *skb, struct drbd_tconn *tconn, unsigned vnr)
2587 nla = nla_nest_start(skb, DRBD_NLA_CFG_CONTEXT);
2589 goto nla_put_failure;
2590 if (vnr != VOLUME_UNSPECIFIED &&
2591 nla_put_u32(skb, T_ctx_volume, vnr))
2592 goto nla_put_failure;
2593 if (nla_put_string(skb, T_ctx_resource_name, tconn->name))
2594 goto nla_put_failure;
2595 if (tconn->my_addr_len &&
2596 nla_put(skb, T_ctx_my_addr, tconn->my_addr_len, &tconn->my_addr))
2597 goto nla_put_failure;
2598 if (tconn->peer_addr_len &&
2599 nla_put(skb, T_ctx_peer_addr, tconn->peer_addr_len, &tconn->peer_addr))
2600 goto nla_put_failure;
2601 nla_nest_end(skb, nla);
2606 nla_nest_cancel(skb, nla);
2610 int nla_put_status_info(struct sk_buff *skb, struct drbd_conf *mdev,
2611 const struct sib_info *sib)
2613 struct state_info *si = NULL; /* for sizeof(si->member); */
2614 struct net_conf *nc;
2618 int exclude_sensitive;
2620 /* If sib != NULL, this is drbd_bcast_event, which anyone can listen
2621 * to. So we better exclude_sensitive information.
2623 * If sib == NULL, this is drbd_adm_get_status, executed synchronously
2624 * in the context of the requesting user process. Exclude sensitive
2625 * information, unless current has superuser.
2627 * NOTE: for drbd_adm_get_status_all(), this is a netlink dump, and
2628 * relies on the current implementation of netlink_dump(), which
2629 * executes the dump callback successively from netlink_recvmsg(),
2630 * always in the context of the receiving process */
2631 exclude_sensitive = sib || !capable(CAP_SYS_ADMIN);
2633 got_ldev = get_ldev(mdev);
2635 /* We need to add connection name and volume number information still.
2636 * Minor number is in drbd_genlmsghdr. */
2637 if (nla_put_drbd_cfg_context(skb, mdev->tconn, mdev->vnr))
2638 goto nla_put_failure;
2640 if (res_opts_to_skb(skb, &mdev->tconn->res_opts, exclude_sensitive))
2641 goto nla_put_failure;
2645 if (disk_conf_to_skb(skb, rcu_dereference(mdev->ldev->disk_conf), exclude_sensitive))
2646 goto nla_put_failure;
2648 nc = rcu_dereference(mdev->tconn->net_conf);
2650 err = net_conf_to_skb(skb, nc, exclude_sensitive);
2653 goto nla_put_failure;
2655 nla = nla_nest_start(skb, DRBD_NLA_STATE_INFO);
2657 goto nla_put_failure;
2658 if (nla_put_u32(skb, T_sib_reason, sib ? sib->sib_reason : SIB_GET_STATUS_REPLY) ||
2659 nla_put_u32(skb, T_current_state, mdev->state.i) ||
2660 nla_put_u64(skb, T_ed_uuid, mdev->ed_uuid) ||
2661 nla_put_u64(skb, T_capacity, drbd_get_capacity(mdev->this_bdev)) ||
2662 nla_put_u64(skb, T_send_cnt, mdev->send_cnt) ||
2663 nla_put_u64(skb, T_recv_cnt, mdev->recv_cnt) ||
2664 nla_put_u64(skb, T_read_cnt, mdev->read_cnt) ||
2665 nla_put_u64(skb, T_writ_cnt, mdev->writ_cnt) ||
2666 nla_put_u64(skb, T_al_writ_cnt, mdev->al_writ_cnt) ||
2667 nla_put_u64(skb, T_bm_writ_cnt, mdev->bm_writ_cnt) ||
2668 nla_put_u32(skb, T_ap_bio_cnt, atomic_read(&mdev->ap_bio_cnt)) ||
2669 nla_put_u32(skb, T_ap_pending_cnt, atomic_read(&mdev->ap_pending_cnt)) ||
2670 nla_put_u32(skb, T_rs_pending_cnt, atomic_read(&mdev->rs_pending_cnt)))
2671 goto nla_put_failure;
2676 spin_lock_irq(&mdev->ldev->md.uuid_lock);
2677 err = nla_put(skb, T_uuids, sizeof(si->uuids), mdev->ldev->md.uuid);
2678 spin_unlock_irq(&mdev->ldev->md.uuid_lock);
2681 goto nla_put_failure;
2683 if (nla_put_u32(skb, T_disk_flags, mdev->ldev->md.flags) ||
2684 nla_put_u64(skb, T_bits_total, drbd_bm_bits(mdev)) ||
2685 nla_put_u64(skb, T_bits_oos, drbd_bm_total_weight(mdev)))
2686 goto nla_put_failure;
2687 if (C_SYNC_SOURCE <= mdev->state.conn &&
2688 C_PAUSED_SYNC_T >= mdev->state.conn) {
2689 if (nla_put_u64(skb, T_bits_rs_total, mdev->rs_total) ||
2690 nla_put_u64(skb, T_bits_rs_failed, mdev->rs_failed))
2691 goto nla_put_failure;
2696 switch(sib->sib_reason) {
2697 case SIB_SYNC_PROGRESS:
2698 case SIB_GET_STATUS_REPLY:
2700 case SIB_STATE_CHANGE:
2701 if (nla_put_u32(skb, T_prev_state, sib->os.i) ||
2702 nla_put_u32(skb, T_new_state, sib->ns.i))
2703 goto nla_put_failure;
2705 case SIB_HELPER_POST:
2706 if (nla_put_u32(skb, T_helper_exit_code,
2707 sib->helper_exit_code))
2708 goto nla_put_failure;
2710 case SIB_HELPER_PRE:
2711 if (nla_put_string(skb, T_helper, sib->helper_name))
2712 goto nla_put_failure;
2716 nla_nest_end(skb, nla);
2726 int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info)
2728 enum drbd_ret_code retcode;
2731 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2732 if (!adm_ctx.reply_skb)
2734 if (retcode != NO_ERROR)
2737 err = nla_put_status_info(adm_ctx.reply_skb, adm_ctx.mdev, NULL);
2739 nlmsg_free(adm_ctx.reply_skb);
2743 drbd_adm_finish(info, retcode);
2747 int get_one_status(struct sk_buff *skb, struct netlink_callback *cb)
2749 struct drbd_conf *mdev;
2750 struct drbd_genlmsghdr *dh;
2751 struct drbd_tconn *pos = (struct drbd_tconn*)cb->args[0];
2752 struct drbd_tconn *tconn = NULL;
2753 struct drbd_tconn *tmp;
2754 unsigned volume = cb->args[1];
2756 /* Open coded, deferred, iteration:
2757 * list_for_each_entry_safe(tconn, tmp, &drbd_tconns, all_tconn) {
2758 * idr_for_each_entry(&tconn->volumes, mdev, i) {
2762 * where tconn is cb->args[0];
2763 * and i is cb->args[1];
2765 * cb->args[2] indicates if we shall loop over all resources,
2766 * or just dump all volumes of a single resource.
2768 * This may miss entries inserted after this dump started,
2769 * or entries deleted before they are reached.
2771 * We need to make sure the mdev won't disappear while
2772 * we are looking at it, and revalidate our iterators
2773 * on each iteration.
2776 /* synchronize with conn_create()/conn_destroy() */
2778 /* revalidate iterator position */
2779 list_for_each_entry_rcu(tmp, &drbd_tconns, all_tconn) {
2781 /* first iteration */
2793 mdev = idr_get_next(&tconn->volumes, &volume);
2795 /* No more volumes to dump on this tconn.
2796 * Advance tconn iterator. */
2797 pos = list_entry_rcu(tconn->all_tconn.next,
2798 struct drbd_tconn, all_tconn);
2799 /* Did we dump any volume on this tconn yet? */
2801 /* If we reached the end of the list,
2802 * or only a single resource dump was requested,
2804 if (&pos->all_tconn == &drbd_tconns || cb->args[2])
2812 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
2813 cb->nlh->nlmsg_seq, &drbd_genl_family,
2814 NLM_F_MULTI, DRBD_ADM_GET_STATUS);
2819 /* This is a tconn without a single volume.
2820 * Suprisingly enough, it may have a network
2822 struct net_conf *nc;
2824 dh->ret_code = NO_ERROR;
2825 if (nla_put_drbd_cfg_context(skb, tconn, VOLUME_UNSPECIFIED))
2827 nc = rcu_dereference(tconn->net_conf);
2828 if (nc && net_conf_to_skb(skb, nc, 1) != 0)
2833 D_ASSERT(mdev->vnr == volume);
2834 D_ASSERT(mdev->tconn == tconn);
2836 dh->minor = mdev_to_minor(mdev);
2837 dh->ret_code = NO_ERROR;
2839 if (nla_put_status_info(skb, mdev, NULL)) {
2841 genlmsg_cancel(skb, dh);
2845 genlmsg_end(skb, dh);
2850 /* where to start the next iteration */
2851 cb->args[0] = (long)pos;
2852 cb->args[1] = (pos == tconn) ? volume + 1 : 0;
2854 /* No more tconns/volumes/minors found results in an empty skb.
2855 * Which will terminate the dump. */
2860 * Request status of all resources, or of all volumes within a single resource.
2862 * This is a dump, as the answer may not fit in a single reply skb otherwise.
2863 * Which means we cannot use the family->attrbuf or other such members, because
2864 * dump is NOT protected by the genl_lock(). During dump, we only have access
2865 * to the incoming skb, and need to opencode "parsing" of the nlattr payload.
2867 * Once things are setup properly, we call into get_one_status().
2869 int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb)
2871 const unsigned hdrlen = GENL_HDRLEN + GENL_MAGIC_FAMILY_HDRSZ;
2873 const char *resource_name;
2874 struct drbd_tconn *tconn;
2877 /* Is this a followup call? */
2879 /* ... of a single resource dump,
2880 * and the resource iterator has been advanced already? */
2881 if (cb->args[2] && cb->args[2] != cb->args[0])
2882 return 0; /* DONE. */
2886 /* First call (from netlink_dump_start). We need to figure out
2887 * which resource(s) the user wants us to dump. */
2888 nla = nla_find(nlmsg_attrdata(cb->nlh, hdrlen),
2889 nlmsg_attrlen(cb->nlh, hdrlen),
2890 DRBD_NLA_CFG_CONTEXT);
2892 /* No explicit context given. Dump all. */
2895 maxtype = ARRAY_SIZE(drbd_cfg_context_nl_policy) - 1;
2896 nla = drbd_nla_find_nested(maxtype, nla, __nla_type(T_ctx_resource_name));
2898 return PTR_ERR(nla);
2899 /* context given, but no name present? */
2902 resource_name = nla_data(nla);
2903 tconn = conn_get_by_name(resource_name);
2908 kref_put(&tconn->kref, &conn_destroy); /* get_one_status() (re)validates tconn by itself */
2910 /* prime iterators, and set "filter" mode mark:
2911 * only dump this tconn. */
2912 cb->args[0] = (long)tconn;
2913 /* cb->args[1] = 0; passed in this way. */
2914 cb->args[2] = (long)tconn;
2917 return get_one_status(skb, cb);
2920 int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info)
2922 enum drbd_ret_code retcode;
2923 struct timeout_parms tp;
2926 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2927 if (!adm_ctx.reply_skb)
2929 if (retcode != NO_ERROR)
2933 adm_ctx.mdev->state.pdsk == D_OUTDATED ? UT_PEER_OUTDATED :
2934 test_bit(USE_DEGR_WFC_T, &adm_ctx.mdev->flags) ? UT_DEGRADED :
2937 err = timeout_parms_to_priv_skb(adm_ctx.reply_skb, &tp);
2939 nlmsg_free(adm_ctx.reply_skb);
2943 drbd_adm_finish(info, retcode);
2947 int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info)
2949 struct drbd_conf *mdev;
2950 enum drbd_ret_code retcode;
2951 struct start_ov_parms parms;
2953 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2954 if (!adm_ctx.reply_skb)
2956 if (retcode != NO_ERROR)
2959 mdev = adm_ctx.mdev;
2961 /* resume from last known position, if possible */
2962 parms.ov_start_sector = mdev->ov_start_sector;
2963 parms.ov_stop_sector = ULLONG_MAX;
2964 if (info->attrs[DRBD_NLA_START_OV_PARMS]) {
2965 int err = start_ov_parms_from_attrs(&parms, info);
2967 retcode = ERR_MANDATORY_TAG;
2968 drbd_msg_put_info(from_attrs_err_to_txt(err));
2972 /* w_make_ov_request expects position to be aligned */
2973 mdev->ov_start_sector = parms.ov_start_sector & ~(BM_SECT_PER_BIT-1);
2974 mdev->ov_stop_sector = parms.ov_stop_sector;
2976 /* If there is still bitmap IO pending, e.g. previous resync or verify
2977 * just being finished, wait for it before requesting a new resync. */
2978 drbd_suspend_io(mdev);
2979 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
2980 retcode = drbd_request_state(mdev,NS(conn,C_VERIFY_S));
2981 drbd_resume_io(mdev);
2983 drbd_adm_finish(info, retcode);
2988 int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info)
2990 struct drbd_conf *mdev;
2991 enum drbd_ret_code retcode;
2992 int skip_initial_sync = 0;
2994 struct new_c_uuid_parms args;
2996 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
2997 if (!adm_ctx.reply_skb)
2999 if (retcode != NO_ERROR)
3002 mdev = adm_ctx.mdev;
3003 memset(&args, 0, sizeof(args));
3004 if (info->attrs[DRBD_NLA_NEW_C_UUID_PARMS]) {
3005 err = new_c_uuid_parms_from_attrs(&args, info);
3007 retcode = ERR_MANDATORY_TAG;
3008 drbd_msg_put_info(from_attrs_err_to_txt(err));
3013 mutex_lock(mdev->state_mutex); /* Protects us against serialized state changes. */
3015 if (!get_ldev(mdev)) {
3016 retcode = ERR_NO_DISK;
3020 /* this is "skip initial sync", assume to be clean */
3021 if (mdev->state.conn == C_CONNECTED && mdev->tconn->agreed_pro_version >= 90 &&
3022 mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED && args.clear_bm) {
3023 dev_info(DEV, "Preparing to skip initial sync\n");
3024 skip_initial_sync = 1;
3025 } else if (mdev->state.conn != C_STANDALONE) {
3026 retcode = ERR_CONNECTED;
3030 drbd_uuid_set(mdev, UI_BITMAP, 0); /* Rotate UI_BITMAP to History 1, etc... */
3031 drbd_uuid_new_current(mdev); /* New current, previous to UI_BITMAP */
3033 if (args.clear_bm) {
3034 err = drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write,
3035 "clear_n_write from new_c_uuid", BM_LOCKED_MASK);
3037 dev_err(DEV, "Writing bitmap failed with %d\n",err);
3038 retcode = ERR_IO_MD_DISK;
3040 if (skip_initial_sync) {
3041 drbd_send_uuids_skip_initial_sync(mdev);
3042 _drbd_uuid_set(mdev, UI_BITMAP, 0);
3043 drbd_print_uuids(mdev, "cleared bitmap UUID");
3044 spin_lock_irq(&mdev->tconn->req_lock);
3045 _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
3047 spin_unlock_irq(&mdev->tconn->req_lock);
3055 mutex_unlock(mdev->state_mutex);
3057 drbd_adm_finish(info, retcode);
3061 static enum drbd_ret_code
3062 drbd_check_resource_name(const char *name)
3064 if (!name || !name[0]) {
3065 drbd_msg_put_info("resource name missing");
3066 return ERR_MANDATORY_TAG;
3068 /* if we want to use these in sysfs/configfs/debugfs some day,
3069 * we must not allow slashes */
3070 if (strchr(name, '/')) {
3071 drbd_msg_put_info("invalid resource name");
3072 return ERR_INVALID_REQUEST;
3077 int drbd_adm_new_resource(struct sk_buff *skb, struct genl_info *info)
3079 enum drbd_ret_code retcode;
3080 struct res_opts res_opts;
3083 retcode = drbd_adm_prepare(skb, info, 0);
3084 if (!adm_ctx.reply_skb)
3086 if (retcode != NO_ERROR)
3089 set_res_opts_defaults(&res_opts);
3090 err = res_opts_from_attrs(&res_opts, info);
3091 if (err && err != -ENOMSG) {
3092 retcode = ERR_MANDATORY_TAG;
3093 drbd_msg_put_info(from_attrs_err_to_txt(err));
3097 retcode = drbd_check_resource_name(adm_ctx.resource_name);
3098 if (retcode != NO_ERROR)
3101 if (adm_ctx.tconn) {
3102 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL) {
3103 retcode = ERR_INVALID_REQUEST;
3104 drbd_msg_put_info("resource exists");
3106 /* else: still NO_ERROR */
3110 if (!conn_create(adm_ctx.resource_name, &res_opts))
3111 retcode = ERR_NOMEM;
3113 drbd_adm_finish(info, retcode);
3117 int drbd_adm_add_minor(struct sk_buff *skb, struct genl_info *info)
3119 struct drbd_genlmsghdr *dh = info->userhdr;
3120 enum drbd_ret_code retcode;
3122 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
3123 if (!adm_ctx.reply_skb)
3125 if (retcode != NO_ERROR)
3128 if (dh->minor > MINORMASK) {
3129 drbd_msg_put_info("requested minor out of range");
3130 retcode = ERR_INVALID_REQUEST;
3133 if (adm_ctx.volume > DRBD_VOLUME_MAX) {
3134 drbd_msg_put_info("requested volume id out of range");
3135 retcode = ERR_INVALID_REQUEST;
3139 /* drbd_adm_prepare made sure already
3140 * that mdev->tconn and mdev->vnr match the request. */
3142 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
3143 retcode = ERR_MINOR_EXISTS;
3144 /* else: still NO_ERROR */
3148 retcode = conn_new_minor(adm_ctx.tconn, dh->minor, adm_ctx.volume);
3150 drbd_adm_finish(info, retcode);
3154 static enum drbd_ret_code adm_delete_minor(struct drbd_conf *mdev)
3156 if (mdev->state.disk == D_DISKLESS &&
3157 /* no need to be mdev->state.conn == C_STANDALONE &&
3158 * we may want to delete a minor from a live replication group.
3160 mdev->state.role == R_SECONDARY) {
3161 _drbd_request_state(mdev, NS(conn, C_WF_REPORT_PARAMS),
3162 CS_VERBOSE + CS_WAIT_COMPLETE);
3163 idr_remove(&mdev->tconn->volumes, mdev->vnr);
3164 idr_remove(&minors, mdev_to_minor(mdev));
3165 del_gendisk(mdev->vdisk);
3167 kref_put(&mdev->kref, &drbd_minor_destroy);
3170 return ERR_MINOR_CONFIGURED;
3173 int drbd_adm_delete_minor(struct sk_buff *skb, struct genl_info *info)
3175 enum drbd_ret_code retcode;
3177 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
3178 if (!adm_ctx.reply_skb)
3180 if (retcode != NO_ERROR)
3183 retcode = adm_delete_minor(adm_ctx.mdev);
3185 drbd_adm_finish(info, retcode);
3189 int drbd_adm_down(struct sk_buff *skb, struct genl_info *info)
3191 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
3192 struct drbd_conf *mdev;
3195 retcode = drbd_adm_prepare(skb, info, 0);
3196 if (!adm_ctx.reply_skb)
3198 if (retcode != NO_ERROR)
3201 if (!adm_ctx.tconn) {
3202 retcode = ERR_RES_NOT_KNOWN;
3207 idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
3208 retcode = drbd_set_role(mdev, R_SECONDARY, 0);
3209 if (retcode < SS_SUCCESS) {
3210 drbd_msg_put_info("failed to demote");
3215 retcode = conn_try_disconnect(adm_ctx.tconn, 0);
3216 if (retcode < SS_SUCCESS) {
3217 drbd_msg_put_info("failed to disconnect");
3222 idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
3223 retcode = adm_detach(mdev, 0);
3224 if (retcode < SS_SUCCESS || retcode > NO_ERROR) {
3225 drbd_msg_put_info("failed to detach");
3230 /* If we reach this, all volumes (of this tconn) are Secondary,
3231 * Disconnected, Diskless, aka Unconfigured. Make sure all threads have
3232 * actually stopped, state handling only does drbd_thread_stop_nowait(). */
3233 drbd_thread_stop(&adm_ctx.tconn->worker);
3235 /* Now, nothing can fail anymore */
3237 /* delete volumes */
3238 idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
3239 retcode = adm_delete_minor(mdev);
3240 if (retcode != NO_ERROR) {
3241 /* "can not happen" */
3242 drbd_msg_put_info("failed to delete volume");
3247 /* delete connection */
3248 if (conn_lowest_minor(adm_ctx.tconn) < 0) {
3249 list_del_rcu(&adm_ctx.tconn->all_tconn);
3251 kref_put(&adm_ctx.tconn->kref, &conn_destroy);
3255 /* "can not happen" */
3256 retcode = ERR_RES_IN_USE;
3257 drbd_msg_put_info("failed to delete connection");
3261 drbd_adm_finish(info, retcode);
3265 int drbd_adm_del_resource(struct sk_buff *skb, struct genl_info *info)
3267 enum drbd_ret_code retcode;
3269 retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
3270 if (!adm_ctx.reply_skb)
3272 if (retcode != NO_ERROR)
3275 if (conn_lowest_minor(adm_ctx.tconn) < 0) {
3276 list_del_rcu(&adm_ctx.tconn->all_tconn);
3278 kref_put(&adm_ctx.tconn->kref, &conn_destroy);
3282 retcode = ERR_RES_IN_USE;
3285 if (retcode == NO_ERROR)
3286 drbd_thread_stop(&adm_ctx.tconn->worker);
3288 drbd_adm_finish(info, retcode);
3292 void drbd_bcast_event(struct drbd_conf *mdev, const struct sib_info *sib)
3294 static atomic_t drbd_genl_seq = ATOMIC_INIT(2); /* two. */
3295 struct sk_buff *msg;
3296 struct drbd_genlmsghdr *d_out;
3300 if (sib->sib_reason == SIB_SYNC_PROGRESS) {
3301 if (time_after(jiffies, mdev->rs_last_bcast + HZ))
3302 mdev->rs_last_bcast = jiffies;
3307 seq = atomic_inc_return(&drbd_genl_seq);
3308 msg = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
3313 d_out = genlmsg_put(msg, 0, seq, &drbd_genl_family, 0, DRBD_EVENT);
3314 if (!d_out) /* cannot happen, but anyways. */
3315 goto nla_put_failure;
3316 d_out->minor = mdev_to_minor(mdev);
3317 d_out->ret_code = NO_ERROR;
3319 if (nla_put_status_info(msg, mdev, sib))
3320 goto nla_put_failure;
3321 genlmsg_end(msg, d_out);
3322 err = drbd_genl_multicast_events(msg, 0);
3323 /* msg has been consumed or freed in netlink_broadcast() */
3324 if (err && err != -ESRCH)
3332 dev_err(DEV, "Error %d while broadcasting event. "
3333 "Event seq:%u sib_reason:%u\n",
3334 err, seq, sib->sib_reason);