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>
35 #include "drbd_protocol.h"
37 #include "drbd_wrappers.h"
38 #include <asm/unaligned.h>
39 #include <linux/drbd_limits.h>
40 #include <linux/kthread.h>
42 #include <net/genetlink.h>
45 // int drbd_adm_create_resource(struct sk_buff *skb, struct genl_info *info);
46 // int drbd_adm_delete_resource(struct sk_buff *skb, struct genl_info *info);
48 int drbd_adm_new_minor(struct sk_buff
*skb
, struct genl_info
*info
);
49 int drbd_adm_del_minor(struct sk_buff
*skb
, struct genl_info
*info
);
51 int drbd_adm_new_resource(struct sk_buff
*skb
, struct genl_info
*info
);
52 int drbd_adm_del_resource(struct sk_buff
*skb
, struct genl_info
*info
);
53 int drbd_adm_down(struct sk_buff
*skb
, struct genl_info
*info
);
55 int drbd_adm_set_role(struct sk_buff
*skb
, struct genl_info
*info
);
56 int drbd_adm_attach(struct sk_buff
*skb
, struct genl_info
*info
);
57 int drbd_adm_disk_opts(struct sk_buff
*skb
, struct genl_info
*info
);
58 int drbd_adm_detach(struct sk_buff
*skb
, struct genl_info
*info
);
59 int drbd_adm_connect(struct sk_buff
*skb
, struct genl_info
*info
);
60 int drbd_adm_net_opts(struct sk_buff
*skb
, struct genl_info
*info
);
61 int drbd_adm_resize(struct sk_buff
*skb
, struct genl_info
*info
);
62 int drbd_adm_start_ov(struct sk_buff
*skb
, struct genl_info
*info
);
63 int drbd_adm_new_c_uuid(struct sk_buff
*skb
, struct genl_info
*info
);
64 int drbd_adm_disconnect(struct sk_buff
*skb
, struct genl_info
*info
);
65 int drbd_adm_invalidate(struct sk_buff
*skb
, struct genl_info
*info
);
66 int drbd_adm_invalidate_peer(struct sk_buff
*skb
, struct genl_info
*info
);
67 int drbd_adm_pause_sync(struct sk_buff
*skb
, struct genl_info
*info
);
68 int drbd_adm_resume_sync(struct sk_buff
*skb
, struct genl_info
*info
);
69 int drbd_adm_suspend_io(struct sk_buff
*skb
, struct genl_info
*info
);
70 int drbd_adm_resume_io(struct sk_buff
*skb
, struct genl_info
*info
);
71 int drbd_adm_outdate(struct sk_buff
*skb
, struct genl_info
*info
);
72 int drbd_adm_resource_opts(struct sk_buff
*skb
, struct genl_info
*info
);
73 int drbd_adm_get_status(struct sk_buff
*skb
, struct genl_info
*info
);
74 int drbd_adm_get_timeout_type(struct sk_buff
*skb
, struct genl_info
*info
);
76 int drbd_adm_get_status_all(struct sk_buff
*skb
, struct netlink_callback
*cb
);
78 #include <linux/drbd_genl_api.h>
80 #include <linux/genl_magic_func.h>
82 /* used blkdev_get_by_path, to claim our meta data device(s) */
83 static char *drbd_m_holder
= "Hands off! this is DRBD's meta data device.";
85 /* Configuration is strictly serialized, because generic netlink message
86 * processing is strictly serialized by the genl_lock().
87 * Which means we can use one static global drbd_config_context struct.
89 static struct drbd_config_context
{
90 /* assigned from drbd_genlmsghdr */
92 /* assigned from request attributes, if present */
94 #define VOLUME_UNSPECIFIED (-1U)
95 /* pointer into the request skb,
96 * limited lifetime! */
98 struct nlattr
*my_addr
;
99 struct nlattr
*peer_addr
;
102 struct sk_buff
*reply_skb
;
103 /* pointer into reply buffer */
104 struct drbd_genlmsghdr
*reply_dh
;
105 /* resolved from attributes, if possible */
106 struct drbd_device
*device
;
107 struct drbd_connection
*connection
;
110 static void drbd_adm_send_reply(struct sk_buff
*skb
, struct genl_info
*info
)
112 genlmsg_end(skb
, genlmsg_data(nlmsg_data(nlmsg_hdr(skb
))));
113 if (genlmsg_reply(skb
, info
))
114 printk(KERN_ERR
"drbd: error sending genl reply\n");
117 /* Used on a fresh "drbd_adm_prepare"d reply_skb, this cannot fail: The only
118 * reason it could fail was no space in skb, and there are 4k available. */
119 int drbd_msg_put_info(const char *info
)
121 struct sk_buff
*skb
= adm_ctx
.reply_skb
;
125 if (!info
|| !info
[0])
128 nla
= nla_nest_start(skb
, DRBD_NLA_CFG_REPLY
);
132 err
= nla_put_string(skb
, T_info_text
, info
);
134 nla_nest_cancel(skb
, nla
);
137 nla_nest_end(skb
, nla
);
141 /* This would be a good candidate for a "pre_doit" hook,
142 * and per-family private info->pointers.
143 * But we need to stay compatible with older kernels.
144 * If it returns successfully, adm_ctx members are valid.
146 #define DRBD_ADM_NEED_MINOR 1
147 #define DRBD_ADM_NEED_RESOURCE 2
148 #define DRBD_ADM_NEED_CONNECTION 4
149 static int drbd_adm_prepare(struct sk_buff
*skb
, struct genl_info
*info
,
152 struct drbd_genlmsghdr
*d_in
= info
->userhdr
;
153 const u8 cmd
= info
->genlhdr
->cmd
;
156 memset(&adm_ctx
, 0, sizeof(adm_ctx
));
158 /* genl_rcv_msg only checks for CAP_NET_ADMIN on "GENL_ADMIN_PERM" :( */
159 if (cmd
!= DRBD_ADM_GET_STATUS
&& !capable(CAP_NET_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
.connection
->my_addr
)) ||
207 (adm_ctx
.peer_addr
&&
208 nla_len(adm_ctx
.peer_addr
) > sizeof(adm_ctx
.connection
->peer_addr
))) {
214 adm_ctx
.minor
= d_in
->minor
;
215 adm_ctx
.device
= minor_to_device(d_in
->minor
);
216 adm_ctx
.connection
= conn_get_by_name(adm_ctx
.resource_name
);
218 if (!adm_ctx
.device
&& (flags
& DRBD_ADM_NEED_MINOR
)) {
219 drbd_msg_put_info("unknown minor");
220 return ERR_MINOR_INVALID
;
222 if (!adm_ctx
.connection
&& (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
.connection
&& !(flags
& DRBD_ADM_NEED_RESOURCE
)) {
229 drbd_msg_put_info("no resource name expected");
230 return ERR_INVALID_REQUEST
;
232 if (adm_ctx
.device
) {
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
.connection
= 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
.connection
) {
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
.device
&& adm_ctx
.connection
&&
249 first_peer_device(adm_ctx
.device
)->connection
!= adm_ctx
.connection
) {
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 first_peer_device(adm_ctx
.device
)->connection
->name
);
253 drbd_msg_put_info("minor exists in different resource");
254 return ERR_INVALID_REQUEST
;
256 if (adm_ctx
.device
&&
257 adm_ctx
.volume
!= VOLUME_UNSPECIFIED
&&
258 adm_ctx
.volume
!= adm_ctx
.device
->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
.device
->vnr
, first_peer_device(adm_ctx
.device
)->connection
->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
)
276 if (adm_ctx
.connection
) {
277 kref_put(&adm_ctx
.connection
->kref
, drbd_destroy_connection
);
278 adm_ctx
.connection
= 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_connection
*connection
, char **envp
)
293 /* FIXME: A future version will not allow this case. */
294 if (connection
->my_addr_len
== 0 || connection
->peer_addr_len
== 0)
297 switch (((struct sockaddr
*)&connection
->peer_addr
)->sa_family
) {
300 snprintf(envp
[4], 60, "DRBD_PEER_ADDRESS=%pI6",
301 &((struct sockaddr_in6
*)&connection
->peer_addr
)->sin6_addr
);
305 snprintf(envp
[4], 60, "DRBD_PEER_ADDRESS=%pI4",
306 &((struct sockaddr_in
*)&connection
->peer_addr
)->sin_addr
);
310 snprintf(envp
[4], 60, "DRBD_PEER_ADDRESS=%pI4",
311 &((struct sockaddr_in
*)&connection
->peer_addr
)->sin_addr
);
313 snprintf(envp
[3], 20, "DRBD_PEER_AF=%s", afs
);
316 int drbd_khelper(struct drbd_device
*device
, 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_connection
*connection
= first_peer_device(device
)->connection
;
330 if (current
== connection
->worker
.task
)
331 set_bit(CALLBACK_PENDING
, &connection
->flags
);
333 snprintf(mb
, 12, "minor-%d", device_to_minor(device
));
334 setup_khelper_env(connection
, envp
);
336 /* The helper may take some time.
337 * write out any unsynced meta data changes now */
338 drbd_md_sync(device
);
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(device
, &sib
);
344 ret
= call_usermodehelper(usermode_helper
, argv
, envp
, UMH_WAIT_PROC
);
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(device
, &sib
);
357 if (current
== connection
->worker
.task
)
358 clear_bit(CALLBACK_PENDING
, &connection
->flags
);
360 if (ret
< 0) /* Ignore any ERRNOs we got. */
366 static int conn_khelper(struct drbd_connection
*connection
, char *cmd
)
368 char *envp
[] = { "HOME=/",
370 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
371 (char[20]) { }, /* address family */
372 (char[60]) { }, /* address */
374 char *argv
[] = {usermode_helper
, cmd
, connection
->name
, NULL
};
377 setup_khelper_env(connection
, envp
);
378 conn_md_sync(connection
);
380 conn_info(connection
, "helper command: %s %s %s\n", usermode_helper
, cmd
, connection
->name
);
381 /* TODO: conn_bcast_event() ?? */
383 ret
= call_usermodehelper(usermode_helper
, argv
, envp
, UMH_WAIT_PROC
);
385 conn_warn(connection
, "helper command: %s %s %s exit code %u (0x%x)\n",
386 usermode_helper
, cmd
, connection
->name
,
387 (ret
>> 8) & 0xff, ret
);
389 conn_info(connection
, "helper command: %s %s %s exit code %u (0x%x)\n",
390 usermode_helper
, cmd
, connection
->name
,
391 (ret
>> 8) & 0xff, ret
);
392 /* TODO: conn_bcast_event() ?? */
394 if (ret
< 0) /* Ignore any ERRNOs we got. */
400 static enum drbd_fencing_p
highest_fencing_policy(struct drbd_connection
*connection
)
402 enum drbd_fencing_p fp
= FP_NOT_AVAIL
;
403 struct drbd_device
*device
;
407 idr_for_each_entry(&connection
->volumes
, device
, vnr
) {
408 if (get_ldev_if_state(device
, D_CONSISTENT
)) {
409 fp
= max_t(enum drbd_fencing_p
, fp
,
410 rcu_dereference(device
->ldev
->disk_conf
)->fencing
);
419 bool conn_try_outdate_peer(struct drbd_connection
*connection
)
421 unsigned int connect_cnt
;
422 union drbd_state mask
= { };
423 union drbd_state val
= { };
424 enum drbd_fencing_p fp
;
428 if (connection
->cstate
>= C_WF_REPORT_PARAMS
) {
429 conn_err(connection
, "Expected cstate < C_WF_REPORT_PARAMS\n");
433 spin_lock_irq(&connection
->req_lock
);
434 connect_cnt
= connection
->connect_cnt
;
435 spin_unlock_irq(&connection
->req_lock
);
437 fp
= highest_fencing_policy(connection
);
440 conn_warn(connection
, "Not fencing peer, I'm not even Consistent myself.\n");
447 r
= conn_khelper(connection
, "fence-peer");
449 switch ((r
>>8) & 0xff) {
450 case 3: /* peer is inconsistent */
451 ex_to_string
= "peer is inconsistent or worse";
453 val
.pdsk
= D_INCONSISTENT
;
455 case 4: /* peer got outdated, or was already outdated */
456 ex_to_string
= "peer was fenced";
458 val
.pdsk
= D_OUTDATED
;
460 case 5: /* peer was down */
461 if (conn_highest_disk(connection
) == D_UP_TO_DATE
) {
462 /* we will(have) create(d) a new UUID anyways... */
463 ex_to_string
= "peer is unreachable, assumed to be dead";
465 val
.pdsk
= D_OUTDATED
;
467 ex_to_string
= "peer unreachable, doing nothing since disk != UpToDate";
470 case 6: /* Peer is primary, voluntarily outdate myself.
471 * This is useful when an unconnected R_SECONDARY is asked to
472 * become R_PRIMARY, but finds the other peer being active. */
473 ex_to_string
= "peer is active";
474 conn_warn(connection
, "Peer is primary, outdating myself.\n");
476 val
.disk
= D_OUTDATED
;
479 if (fp
!= FP_STONITH
)
480 conn_err(connection
, "fence-peer() = 7 && fencing != Stonith !!!\n");
481 ex_to_string
= "peer was stonithed";
483 val
.pdsk
= D_OUTDATED
;
486 /* The script is broken ... */
487 conn_err(connection
, "fence-peer helper broken, returned %d\n", (r
>>8)&0xff);
488 return false; /* Eventually leave IO frozen */
491 conn_info(connection
, "fence-peer helper returned %d (%s)\n",
492 (r
>>8) & 0xff, ex_to_string
);
497 conn_request_state(connection, mask, val, CS_VERBOSE);
498 here, because we might were able to re-establish the connection in the
500 spin_lock_irq(&connection
->req_lock
);
501 if (connection
->cstate
< C_WF_REPORT_PARAMS
&& !test_bit(STATE_SENT
, &connection
->flags
)) {
502 if (connection
->connect_cnt
!= connect_cnt
)
503 /* In case the connection was established and droped
504 while the fence-peer handler was running, ignore it */
505 conn_info(connection
, "Ignoring fence-peer exit code\n");
507 _conn_request_state(connection
, mask
, val
, CS_VERBOSE
);
509 spin_unlock_irq(&connection
->req_lock
);
511 return conn_highest_pdsk(connection
) <= D_OUTDATED
;
514 static int _try_outdate_peer_async(void *data
)
516 struct drbd_connection
*connection
= (struct drbd_connection
*)data
;
518 conn_try_outdate_peer(connection
);
520 kref_put(&connection
->kref
, drbd_destroy_connection
);
524 void conn_try_outdate_peer_async(struct drbd_connection
*connection
)
526 struct task_struct
*opa
;
528 kref_get(&connection
->kref
);
529 opa
= kthread_run(_try_outdate_peer_async
, connection
, "drbd_async_h");
531 conn_err(connection
, "out of mem, failed to invoke fence-peer helper\n");
532 kref_put(&connection
->kref
, drbd_destroy_connection
);
537 drbd_set_role(struct drbd_device
*device
, enum drbd_role new_role
, int force
)
539 const int max_tries
= 4;
540 enum drbd_state_rv rv
= SS_UNKNOWN_ERROR
;
544 union drbd_state mask
, val
;
546 if (new_role
== R_PRIMARY
)
547 request_ping(first_peer_device(device
)->connection
); /* Detect a dead peer ASAP */
549 mutex_lock(device
->state_mutex
);
551 mask
.i
= 0; mask
.role
= R_MASK
;
552 val
.i
= 0; val
.role
= new_role
;
554 while (try++ < max_tries
) {
555 rv
= _drbd_request_state(device
, mask
, val
, CS_WAIT_COMPLETE
);
557 /* in case we first succeeded to outdate,
558 * but now suddenly could establish a connection */
559 if (rv
== SS_CW_FAILED_BY_PEER
&& mask
.pdsk
!= 0) {
565 if (rv
== SS_NO_UP_TO_DATE_DISK
&& force
&&
566 (device
->state
.disk
< D_UP_TO_DATE
&&
567 device
->state
.disk
>= D_INCONSISTENT
)) {
569 val
.disk
= D_UP_TO_DATE
;
574 if (rv
== SS_NO_UP_TO_DATE_DISK
&&
575 device
->state
.disk
== D_CONSISTENT
&& mask
.pdsk
== 0) {
576 D_ASSERT(device
->state
.pdsk
== D_UNKNOWN
);
578 if (conn_try_outdate_peer(first_peer_device(device
)->connection
)) {
579 val
.disk
= D_UP_TO_DATE
;
585 if (rv
== SS_NOTHING_TO_DO
)
587 if (rv
== SS_PRIMARY_NOP
&& mask
.pdsk
== 0) {
588 if (!conn_try_outdate_peer(first_peer_device(device
)->connection
) && force
) {
589 dev_warn(DEV
, "Forced into split brain situation!\n");
591 val
.pdsk
= D_OUTDATED
;
596 if (rv
== SS_TWO_PRIMARIES
) {
597 /* Maybe the peer is detected as dead very soon...
598 retry at most once more in this case. */
601 nc
= rcu_dereference(first_peer_device(device
)->connection
->net_conf
);
602 timeo
= nc
? (nc
->ping_timeo
+ 1) * HZ
/ 10 : 1;
604 schedule_timeout_interruptible(timeo
);
609 if (rv
< SS_SUCCESS
) {
610 rv
= _drbd_request_state(device
, mask
, val
,
611 CS_VERBOSE
+ CS_WAIT_COMPLETE
);
622 dev_warn(DEV
, "Forced to consider local data as UpToDate!\n");
624 /* Wait until nothing is on the fly :) */
625 wait_event(device
->misc_wait
, atomic_read(&device
->ap_pending_cnt
) == 0);
627 /* FIXME also wait for all pending P_BARRIER_ACK? */
629 if (new_role
== R_SECONDARY
) {
630 set_disk_ro(device
->vdisk
, true);
631 if (get_ldev(device
)) {
632 device
->ldev
->md
.uuid
[UI_CURRENT
] &= ~(u64
)1;
636 mutex_lock(&first_peer_device(device
)->connection
->conf_update
);
637 nc
= first_peer_device(device
)->connection
->net_conf
;
639 nc
->discard_my_data
= 0; /* without copy; single bit op is atomic */
640 mutex_unlock(&first_peer_device(device
)->connection
->conf_update
);
642 set_disk_ro(device
->vdisk
, false);
643 if (get_ldev(device
)) {
644 if (((device
->state
.conn
< C_CONNECTED
||
645 device
->state
.pdsk
<= D_FAILED
)
646 && device
->ldev
->md
.uuid
[UI_BITMAP
] == 0) || forced
)
647 drbd_uuid_new_current(device
);
649 device
->ldev
->md
.uuid
[UI_CURRENT
] |= (u64
)1;
654 /* writeout of activity log covered areas of the bitmap
655 * to stable storage done in after state change already */
657 if (device
->state
.conn
>= C_WF_REPORT_PARAMS
) {
658 /* if this was forced, we should consider sync */
660 drbd_send_uuids(device
);
661 drbd_send_current_state(device
);
664 drbd_md_sync(device
);
666 kobject_uevent(&disk_to_dev(device
->vdisk
)->kobj
, KOBJ_CHANGE
);
668 mutex_unlock(device
->state_mutex
);
672 static const char *from_attrs_err_to_txt(int err
)
674 return err
== -ENOMSG
? "required attribute missing" :
675 err
== -EOPNOTSUPP
? "unknown mandatory attribute" :
676 err
== -EEXIST
? "can not change invariant setting" :
677 "invalid attribute value";
680 int drbd_adm_set_role(struct sk_buff
*skb
, struct genl_info
*info
)
682 struct set_role_parms parms
;
684 enum drbd_ret_code retcode
;
686 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_MINOR
);
687 if (!adm_ctx
.reply_skb
)
689 if (retcode
!= NO_ERROR
)
692 memset(&parms
, 0, sizeof(parms
));
693 if (info
->attrs
[DRBD_NLA_SET_ROLE_PARMS
]) {
694 err
= set_role_parms_from_attrs(&parms
, info
);
696 retcode
= ERR_MANDATORY_TAG
;
697 drbd_msg_put_info(from_attrs_err_to_txt(err
));
702 if (info
->genlhdr
->cmd
== DRBD_ADM_PRIMARY
)
703 retcode
= drbd_set_role(adm_ctx
.device
, R_PRIMARY
, parms
.assume_uptodate
);
705 retcode
= drbd_set_role(adm_ctx
.device
, R_SECONDARY
, 0);
707 drbd_adm_finish(info
, retcode
);
711 /* Initializes the md.*_offset members, so we are able to find
712 * the on disk meta data.
714 * We currently have two possible layouts:
716 * |----------- md_size_sect ------------------|
717 * [ 4k superblock ][ activity log ][ Bitmap ]
719 * | bm_offset = al_offset + X |
720 * ==> bitmap sectors = md_size_sect - bm_offset
723 * |----------- md_size_sect ------------------|
724 * [data.....][ Bitmap ][ activity log ][ 4k superblock ]
726 * | bm_offset = al_offset - Y |
727 * ==> bitmap sectors = Y = al_offset - bm_offset
729 * Activity log size used to be fixed 32kB,
730 * but is about to become configurable.
732 static void drbd_md_set_sector_offsets(struct drbd_device
*device
,
733 struct drbd_backing_dev
*bdev
)
735 sector_t md_size_sect
= 0;
736 unsigned int al_size_sect
= bdev
->md
.al_size_4k
* 8;
738 bdev
->md
.md_offset
= drbd_md_ss(bdev
);
740 switch (bdev
->md
.meta_dev_idx
) {
742 /* v07 style fixed size indexed meta data */
743 bdev
->md
.md_size_sect
= MD_128MB_SECT
;
744 bdev
->md
.al_offset
= MD_4kB_SECT
;
745 bdev
->md
.bm_offset
= MD_4kB_SECT
+ al_size_sect
;
747 case DRBD_MD_INDEX_FLEX_EXT
:
748 /* just occupy the full device; unit: sectors */
749 bdev
->md
.md_size_sect
= drbd_get_capacity(bdev
->md_bdev
);
750 bdev
->md
.al_offset
= MD_4kB_SECT
;
751 bdev
->md
.bm_offset
= MD_4kB_SECT
+ al_size_sect
;
753 case DRBD_MD_INDEX_INTERNAL
:
754 case DRBD_MD_INDEX_FLEX_INT
:
755 /* al size is still fixed */
756 bdev
->md
.al_offset
= -al_size_sect
;
757 /* we need (slightly less than) ~ this much bitmap sectors: */
758 md_size_sect
= drbd_get_capacity(bdev
->backing_bdev
);
759 md_size_sect
= ALIGN(md_size_sect
, BM_SECT_PER_EXT
);
760 md_size_sect
= BM_SECT_TO_EXT(md_size_sect
);
761 md_size_sect
= ALIGN(md_size_sect
, 8);
763 /* plus the "drbd meta data super block",
764 * and the activity log; */
765 md_size_sect
+= MD_4kB_SECT
+ al_size_sect
;
767 bdev
->md
.md_size_sect
= md_size_sect
;
768 /* bitmap offset is adjusted by 'super' block size */
769 bdev
->md
.bm_offset
= -md_size_sect
+ MD_4kB_SECT
;
774 /* input size is expected to be in KB */
775 char *ppsize(char *buf
, unsigned long long size
)
777 /* Needs 9 bytes at max including trailing NUL:
778 * -1ULL ==> "16384 EB" */
779 static char units
[] = { 'K', 'M', 'G', 'T', 'P', 'E' };
781 while (size
>= 10000 && base
< sizeof(units
)-1) {
783 size
= (size
>> 10) + !!(size
& (1<<9));
786 sprintf(buf
, "%u %cB", (unsigned)size
, units
[base
]);
791 /* there is still a theoretical deadlock when called from receiver
792 * on an D_INCONSISTENT R_PRIMARY:
793 * remote READ does inc_ap_bio, receiver would need to receive answer
794 * packet from remote to dec_ap_bio again.
795 * receiver receive_sizes(), comes here,
796 * waits for ap_bio_cnt == 0. -> deadlock.
797 * but this cannot happen, actually, because:
798 * R_PRIMARY D_INCONSISTENT, and peer's disk is unreachable
799 * (not connected, or bad/no disk on peer):
800 * see drbd_fail_request_early, ap_bio_cnt is zero.
801 * R_PRIMARY D_INCONSISTENT, and C_SYNC_TARGET:
802 * peer may not initiate a resize.
804 /* Note these are not to be confused with
805 * drbd_adm_suspend_io/drbd_adm_resume_io,
806 * which are (sub) state changes triggered by admin (drbdsetup),
807 * and can be long lived.
808 * This changes an device->flag, is triggered by drbd internals,
809 * and should be short-lived. */
810 void drbd_suspend_io(struct drbd_device
*device
)
812 set_bit(SUSPEND_IO
, &device
->flags
);
813 if (drbd_suspended(device
))
815 wait_event(device
->misc_wait
, !atomic_read(&device
->ap_bio_cnt
));
818 void drbd_resume_io(struct drbd_device
*device
)
820 clear_bit(SUSPEND_IO
, &device
->flags
);
821 wake_up(&device
->misc_wait
);
825 * drbd_determine_dev_size() - Sets the right device size obeying all constraints
826 * @device: DRBD device.
828 * Returns 0 on success, negative return values indicate errors.
829 * You should call drbd_md_sync() after calling this function.
831 enum determine_dev_size
832 drbd_determine_dev_size(struct drbd_device
*device
, enum dds_flags flags
, struct resize_parms
*rs
) __must_hold(local
)
834 sector_t prev_first_sect
, prev_size
; /* previous meta location */
835 sector_t la_size_sect
, u_size
;
836 struct drbd_md
*md
= &device
->ldev
->md
;
837 u32 prev_al_stripe_size_4k
;
843 int md_moved
, la_size_changed
;
844 enum determine_dev_size rv
= DS_UNCHANGED
;
847 * application request passes inc_ap_bio,
848 * but then cannot get an AL-reference.
849 * this function later may wait on ap_bio_cnt == 0. -> deadlock.
852 * Suspend IO right here.
853 * still lock the act_log to not trigger ASSERTs there.
855 drbd_suspend_io(device
);
856 buffer
= drbd_md_get_buffer(device
); /* Lock meta-data IO */
858 drbd_resume_io(device
);
862 /* no wait necessary anymore, actually we could assert that */
863 wait_event(device
->al_wait
, lc_try_lock(device
->act_log
));
865 prev_first_sect
= drbd_md_first_sector(device
->ldev
);
866 prev_size
= device
->ldev
->md
.md_size_sect
;
867 la_size_sect
= device
->ldev
->md
.la_size_sect
;
870 /* rs is non NULL if we should change the AL layout only */
872 prev_al_stripes
= md
->al_stripes
;
873 prev_al_stripe_size_4k
= md
->al_stripe_size_4k
;
875 md
->al_stripes
= rs
->al_stripes
;
876 md
->al_stripe_size_4k
= rs
->al_stripe_size
/ 4;
877 md
->al_size_4k
= (u64
)rs
->al_stripes
* rs
->al_stripe_size
/ 4;
880 drbd_md_set_sector_offsets(device
, device
->ldev
);
883 u_size
= rcu_dereference(device
->ldev
->disk_conf
)->disk_size
;
885 size
= drbd_new_dev_size(device
, device
->ldev
, u_size
, flags
& DDSF_FORCED
);
887 if (size
< la_size_sect
) {
888 if (rs
&& u_size
== 0) {
889 /* Remove "rs &&" later. This check should always be active, but
890 right now the receiver expects the permissive behavior */
891 dev_warn(DEV
, "Implicit shrink not allowed. "
892 "Use --size=%llus for explicit shrink.\n",
893 (unsigned long long)size
);
894 rv
= DS_ERROR_SHRINK
;
897 rv
= DS_ERROR_SPACE_MD
;
898 if (rv
!= DS_UNCHANGED
)
902 if (drbd_get_capacity(device
->this_bdev
) != size
||
903 drbd_bm_capacity(device
) != size
) {
905 err
= drbd_bm_resize(device
, size
, !(flags
& DDSF_NO_RESYNC
));
907 /* currently there is only one error: ENOMEM! */
908 size
= drbd_bm_capacity(device
)>>1;
910 dev_err(DEV
, "OUT OF MEMORY! "
911 "Could not allocate bitmap!\n");
913 dev_err(DEV
, "BM resizing failed. "
914 "Leaving size unchanged at size = %lu KB\n",
915 (unsigned long)size
);
919 /* racy, see comments above. */
920 drbd_set_my_capacity(device
, size
);
921 device
->ldev
->md
.la_size_sect
= size
;
922 dev_info(DEV
, "size = %s (%llu KB)\n", ppsize(ppb
, size
>>1),
923 (unsigned long long)size
>>1);
928 la_size_changed
= (la_size_sect
!= device
->ldev
->md
.la_size_sect
);
930 md_moved
= prev_first_sect
!= drbd_md_first_sector(device
->ldev
)
931 || prev_size
!= device
->ldev
->md
.md_size_sect
;
933 if (la_size_changed
|| md_moved
|| rs
) {
936 drbd_al_shrink(device
); /* All extents inactive. */
938 prev_flags
= md
->flags
;
939 md
->flags
&= ~MDF_PRIMARY_IND
;
940 drbd_md_write(device
, buffer
);
942 dev_info(DEV
, "Writing the whole bitmap, %s\n",
943 la_size_changed
&& md_moved
? "size changed and md moved" :
944 la_size_changed
? "size changed" : "md moved");
945 /* next line implicitly does drbd_suspend_io()+drbd_resume_io() */
946 drbd_bitmap_io(device
, md_moved
? &drbd_bm_write_all
: &drbd_bm_write
,
947 "size changed", BM_LOCKED_MASK
);
948 drbd_initialize_al(device
, buffer
);
950 md
->flags
= prev_flags
;
951 drbd_md_write(device
, buffer
);
954 dev_info(DEV
, "Changed AL layout to al-stripes = %d, al-stripe-size-kB = %d\n",
955 md
->al_stripes
, md
->al_stripe_size_4k
* 4);
958 if (size
> la_size_sect
)
959 rv
= la_size_sect
? DS_GREW
: DS_GREW_FROM_ZERO
;
960 if (size
< la_size_sect
)
966 md
->al_stripes
= prev_al_stripes
;
967 md
->al_stripe_size_4k
= prev_al_stripe_size_4k
;
968 md
->al_size_4k
= (u64
)prev_al_stripes
* prev_al_stripe_size_4k
;
970 drbd_md_set_sector_offsets(device
, device
->ldev
);
973 lc_unlock(device
->act_log
);
974 wake_up(&device
->al_wait
);
975 drbd_md_put_buffer(device
);
976 drbd_resume_io(device
);
982 drbd_new_dev_size(struct drbd_device
*device
, struct drbd_backing_dev
*bdev
,
983 sector_t u_size
, int assume_peer_has_space
)
985 sector_t p_size
= device
->p_size
; /* partner's disk size. */
986 sector_t la_size_sect
= bdev
->md
.la_size_sect
; /* last agreed size. */
987 sector_t m_size
; /* my size */
990 m_size
= drbd_get_max_capacity(bdev
);
992 if (device
->state
.conn
< C_CONNECTED
&& assume_peer_has_space
) {
993 dev_warn(DEV
, "Resize while not connected was forced by the user!\n");
997 if (p_size
&& m_size
) {
998 size
= min_t(sector_t
, p_size
, m_size
);
1001 size
= la_size_sect
;
1002 if (m_size
&& m_size
< size
)
1004 if (p_size
&& p_size
< size
)
1015 dev_err(DEV
, "Both nodes diskless!\n");
1019 dev_err(DEV
, "Requested disk size is too big (%lu > %lu)\n",
1020 (unsigned long)u_size
>>1, (unsigned long)size
>>1);
1029 * drbd_check_al_size() - Ensures that the AL is of the right size
1030 * @device: DRBD device.
1032 * Returns -EBUSY if current al lru is still used, -ENOMEM when allocation
1033 * failed, and 0 on success. You should call drbd_md_sync() after you called
1036 static int drbd_check_al_size(struct drbd_device
*device
, struct disk_conf
*dc
)
1038 struct lru_cache
*n
, *t
;
1039 struct lc_element
*e
;
1040 unsigned int in_use
;
1043 if (device
->act_log
&&
1044 device
->act_log
->nr_elements
== dc
->al_extents
)
1048 t
= device
->act_log
;
1049 n
= lc_create("act_log", drbd_al_ext_cache
, AL_UPDATES_PER_TRANSACTION
,
1050 dc
->al_extents
, sizeof(struct lc_element
), 0);
1053 dev_err(DEV
, "Cannot allocate act_log lru!\n");
1056 spin_lock_irq(&device
->al_lock
);
1058 for (i
= 0; i
< t
->nr_elements
; i
++) {
1059 e
= lc_element_by_index(t
, i
);
1061 dev_err(DEV
, "refcnt(%d)==%d\n",
1062 e
->lc_number
, e
->refcnt
);
1063 in_use
+= e
->refcnt
;
1067 device
->act_log
= n
;
1068 spin_unlock_irq(&device
->al_lock
);
1070 dev_err(DEV
, "Activity log still in use!\n");
1077 drbd_md_mark_dirty(device
); /* we changed device->act_log->nr_elemens */
1081 static void drbd_setup_queue_param(struct drbd_device
*device
, unsigned int max_bio_size
)
1083 struct request_queue
* const q
= device
->rq_queue
;
1084 unsigned int max_hw_sectors
= max_bio_size
>> 9;
1085 unsigned int max_segments
= 0;
1087 if (get_ldev_if_state(device
, D_ATTACHING
)) {
1088 struct request_queue
* const b
= device
->ldev
->backing_bdev
->bd_disk
->queue
;
1090 max_hw_sectors
= min(queue_max_hw_sectors(b
), max_bio_size
>> 9);
1092 max_segments
= rcu_dereference(device
->ldev
->disk_conf
)->max_bio_bvecs
;
1097 blk_queue_logical_block_size(q
, 512);
1098 blk_queue_max_hw_sectors(q
, max_hw_sectors
);
1099 /* This is the workaround for "bio would need to, but cannot, be split" */
1100 blk_queue_max_segments(q
, max_segments
? max_segments
: BLK_MAX_SEGMENTS
);
1101 blk_queue_segment_boundary(q
, PAGE_CACHE_SIZE
-1);
1103 if (get_ldev_if_state(device
, D_ATTACHING
)) {
1104 struct request_queue
* const b
= device
->ldev
->backing_bdev
->bd_disk
->queue
;
1106 blk_queue_stack_limits(q
, b
);
1108 if (q
->backing_dev_info
.ra_pages
!= b
->backing_dev_info
.ra_pages
) {
1109 dev_info(DEV
, "Adjusting my ra_pages to backing device's (%lu -> %lu)\n",
1110 q
->backing_dev_info
.ra_pages
,
1111 b
->backing_dev_info
.ra_pages
);
1112 q
->backing_dev_info
.ra_pages
= b
->backing_dev_info
.ra_pages
;
1118 void drbd_reconsider_max_bio_size(struct drbd_device
*device
)
1120 unsigned int now
, new, local
, peer
;
1122 now
= queue_max_hw_sectors(device
->rq_queue
) << 9;
1123 local
= device
->local_max_bio_size
; /* Eventually last known value, from volatile memory */
1124 peer
= device
->peer_max_bio_size
; /* Eventually last known value, from meta data */
1126 if (get_ldev_if_state(device
, D_ATTACHING
)) {
1127 local
= queue_max_hw_sectors(device
->ldev
->backing_bdev
->bd_disk
->queue
) << 9;
1128 device
->local_max_bio_size
= local
;
1131 local
= min(local
, DRBD_MAX_BIO_SIZE
);
1133 /* We may ignore peer limits if the peer is modern enough.
1134 Because new from 8.3.8 onwards the peer can use multiple
1135 BIOs for a single peer_request */
1136 if (device
->state
.conn
>= C_WF_REPORT_PARAMS
) {
1137 if (first_peer_device(device
)->connection
->agreed_pro_version
< 94)
1138 peer
= min(device
->peer_max_bio_size
, DRBD_MAX_SIZE_H80_PACKET
);
1139 /* Correct old drbd (up to 8.3.7) if it believes it can do more than 32KiB */
1140 else if (first_peer_device(device
)->connection
->agreed_pro_version
== 94)
1141 peer
= DRBD_MAX_SIZE_H80_PACKET
;
1142 else if (first_peer_device(device
)->connection
->agreed_pro_version
< 100)
1143 peer
= DRBD_MAX_BIO_SIZE_P95
; /* drbd 8.3.8 onwards, before 8.4.0 */
1145 peer
= DRBD_MAX_BIO_SIZE
;
1148 new = min(local
, peer
);
1150 if (device
->state
.role
== R_PRIMARY
&& new < now
)
1151 dev_err(DEV
, "ASSERT FAILED new < now; (%u < %u)\n", new, now
);
1154 dev_info(DEV
, "max BIO size = %u\n", new);
1156 drbd_setup_queue_param(device
, new);
1159 /* Starts the worker thread */
1160 static void conn_reconfig_start(struct drbd_connection
*connection
)
1162 drbd_thread_start(&connection
->worker
);
1163 conn_flush_workqueue(connection
);
1166 /* if still unconfigured, stops worker again. */
1167 static void conn_reconfig_done(struct drbd_connection
*connection
)
1170 spin_lock_irq(&connection
->req_lock
);
1171 stop_threads
= conn_all_vols_unconf(connection
) &&
1172 connection
->cstate
== C_STANDALONE
;
1173 spin_unlock_irq(&connection
->req_lock
);
1175 /* asender is implicitly stopped by receiver
1176 * in conn_disconnect() */
1177 drbd_thread_stop(&connection
->receiver
);
1178 drbd_thread_stop(&connection
->worker
);
1182 /* Make sure IO is suspended before calling this function(). */
1183 static void drbd_suspend_al(struct drbd_device
*device
)
1187 if (!lc_try_lock(device
->act_log
)) {
1188 dev_warn(DEV
, "Failed to lock al in drbd_suspend_al()\n");
1192 drbd_al_shrink(device
);
1193 spin_lock_irq(&first_peer_device(device
)->connection
->req_lock
);
1194 if (device
->state
.conn
< C_CONNECTED
)
1195 s
= !test_and_set_bit(AL_SUSPENDED
, &device
->flags
);
1196 spin_unlock_irq(&first_peer_device(device
)->connection
->req_lock
);
1197 lc_unlock(device
->act_log
);
1200 dev_info(DEV
, "Suspended AL updates\n");
1204 static bool should_set_defaults(struct genl_info
*info
)
1206 unsigned flags
= ((struct drbd_genlmsghdr
*)info
->userhdr
)->flags
;
1207 return 0 != (flags
& DRBD_GENL_F_SET_DEFAULTS
);
1210 static unsigned int drbd_al_extents_max(struct drbd_backing_dev
*bdev
)
1212 /* This is limited by 16 bit "slot" numbers,
1213 * and by available on-disk context storage.
1215 * Also (u16)~0 is special (denotes a "free" extent).
1217 * One transaction occupies one 4kB on-disk block,
1218 * we have n such blocks in the on disk ring buffer,
1219 * the "current" transaction may fail (n-1),
1220 * and there is 919 slot numbers context information per transaction.
1222 * 72 transaction blocks amounts to more than 2**16 context slots,
1223 * so cap there first.
1225 const unsigned int max_al_nr
= DRBD_AL_EXTENTS_MAX
;
1226 const unsigned int sufficient_on_disk
=
1227 (max_al_nr
+ AL_CONTEXT_PER_TRANSACTION
-1)
1228 /AL_CONTEXT_PER_TRANSACTION
;
1230 unsigned int al_size_4k
= bdev
->md
.al_size_4k
;
1232 if (al_size_4k
> sufficient_on_disk
)
1235 return (al_size_4k
- 1) * AL_CONTEXT_PER_TRANSACTION
;
1238 int drbd_adm_disk_opts(struct sk_buff
*skb
, struct genl_info
*info
)
1240 enum drbd_ret_code retcode
;
1241 struct drbd_device
*device
;
1242 struct disk_conf
*new_disk_conf
, *old_disk_conf
;
1243 struct fifo_buffer
*old_plan
= NULL
, *new_plan
= NULL
;
1246 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_MINOR
);
1247 if (!adm_ctx
.reply_skb
)
1249 if (retcode
!= NO_ERROR
)
1252 device
= adm_ctx
.device
;
1254 /* we also need a disk
1255 * to change the options on */
1256 if (!get_ldev(device
)) {
1257 retcode
= ERR_NO_DISK
;
1261 new_disk_conf
= kmalloc(sizeof(struct disk_conf
), GFP_KERNEL
);
1262 if (!new_disk_conf
) {
1263 retcode
= ERR_NOMEM
;
1267 mutex_lock(&first_peer_device(device
)->connection
->conf_update
);
1268 old_disk_conf
= device
->ldev
->disk_conf
;
1269 *new_disk_conf
= *old_disk_conf
;
1270 if (should_set_defaults(info
))
1271 set_disk_conf_defaults(new_disk_conf
);
1273 err
= disk_conf_from_attrs_for_change(new_disk_conf
, info
);
1274 if (err
&& err
!= -ENOMSG
) {
1275 retcode
= ERR_MANDATORY_TAG
;
1276 drbd_msg_put_info(from_attrs_err_to_txt(err
));
1280 if (!expect(new_disk_conf
->resync_rate
>= 1))
1281 new_disk_conf
->resync_rate
= 1;
1283 if (new_disk_conf
->al_extents
< DRBD_AL_EXTENTS_MIN
)
1284 new_disk_conf
->al_extents
= DRBD_AL_EXTENTS_MIN
;
1285 if (new_disk_conf
->al_extents
> drbd_al_extents_max(device
->ldev
))
1286 new_disk_conf
->al_extents
= drbd_al_extents_max(device
->ldev
);
1288 if (new_disk_conf
->c_plan_ahead
> DRBD_C_PLAN_AHEAD_MAX
)
1289 new_disk_conf
->c_plan_ahead
= DRBD_C_PLAN_AHEAD_MAX
;
1291 fifo_size
= (new_disk_conf
->c_plan_ahead
* 10 * SLEEP_TIME
) / HZ
;
1292 if (fifo_size
!= device
->rs_plan_s
->size
) {
1293 new_plan
= fifo_alloc(fifo_size
);
1295 dev_err(DEV
, "kmalloc of fifo_buffer failed");
1296 retcode
= ERR_NOMEM
;
1301 drbd_suspend_io(device
);
1302 wait_event(device
->al_wait
, lc_try_lock(device
->act_log
));
1303 drbd_al_shrink(device
);
1304 err
= drbd_check_al_size(device
, new_disk_conf
);
1305 lc_unlock(device
->act_log
);
1306 wake_up(&device
->al_wait
);
1307 drbd_resume_io(device
);
1310 retcode
= ERR_NOMEM
;
1314 write_lock_irq(&global_state_lock
);
1315 retcode
= drbd_resync_after_valid(device
, new_disk_conf
->resync_after
);
1316 if (retcode
== NO_ERROR
) {
1317 rcu_assign_pointer(device
->ldev
->disk_conf
, new_disk_conf
);
1318 drbd_resync_after_changed(device
);
1320 write_unlock_irq(&global_state_lock
);
1322 if (retcode
!= NO_ERROR
)
1326 old_plan
= device
->rs_plan_s
;
1327 rcu_assign_pointer(device
->rs_plan_s
, new_plan
);
1330 mutex_unlock(&first_peer_device(device
)->connection
->conf_update
);
1332 if (new_disk_conf
->al_updates
)
1333 device
->ldev
->md
.flags
&= ~MDF_AL_DISABLED
;
1335 device
->ldev
->md
.flags
|= MDF_AL_DISABLED
;
1337 if (new_disk_conf
->md_flushes
)
1338 clear_bit(MD_NO_FUA
, &device
->flags
);
1340 set_bit(MD_NO_FUA
, &device
->flags
);
1342 drbd_bump_write_ordering(first_peer_device(device
)->connection
, WO_bdev_flush
);
1344 drbd_md_sync(device
);
1346 if (device
->state
.conn
>= C_CONNECTED
)
1347 drbd_send_sync_param(device
);
1350 kfree(old_disk_conf
);
1352 mod_timer(&device
->request_timer
, jiffies
+ HZ
);
1356 mutex_unlock(&first_peer_device(device
)->connection
->conf_update
);
1358 kfree(new_disk_conf
);
1363 drbd_adm_finish(info
, retcode
);
1367 int drbd_adm_attach(struct sk_buff
*skb
, struct genl_info
*info
)
1369 struct drbd_device
*device
;
1371 enum drbd_ret_code retcode
;
1372 enum determine_dev_size dd
;
1373 sector_t max_possible_sectors
;
1374 sector_t min_md_device_sectors
;
1375 struct drbd_backing_dev
*nbc
= NULL
; /* new_backing_conf */
1376 struct disk_conf
*new_disk_conf
= NULL
;
1377 struct block_device
*bdev
;
1378 struct lru_cache
*resync_lru
= NULL
;
1379 struct fifo_buffer
*new_plan
= NULL
;
1380 union drbd_state ns
, os
;
1381 enum drbd_state_rv rv
;
1382 struct net_conf
*nc
;
1384 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_MINOR
);
1385 if (!adm_ctx
.reply_skb
)
1387 if (retcode
!= NO_ERROR
)
1390 device
= adm_ctx
.device
;
1391 conn_reconfig_start(first_peer_device(device
)->connection
);
1393 /* if you want to reconfigure, please tear down first */
1394 if (device
->state
.disk
> D_DISKLESS
) {
1395 retcode
= ERR_DISK_CONFIGURED
;
1398 /* It may just now have detached because of IO error. Make sure
1399 * drbd_ldev_destroy is done already, we may end up here very fast,
1400 * e.g. if someone calls attach from the on-io-error handler,
1401 * to realize a "hot spare" feature (not that I'd recommend that) */
1402 wait_event(device
->misc_wait
, !atomic_read(&device
->local_cnt
));
1404 /* make sure there is no leftover from previous force-detach attempts */
1405 clear_bit(FORCE_DETACH
, &device
->flags
);
1406 clear_bit(WAS_IO_ERROR
, &device
->flags
);
1407 clear_bit(WAS_READ_ERROR
, &device
->flags
);
1409 /* and no leftover from previously aborted resync or verify, either */
1410 device
->rs_total
= 0;
1411 device
->rs_failed
= 0;
1412 atomic_set(&device
->rs_pending_cnt
, 0);
1414 /* allocation not in the IO path, drbdsetup context */
1415 nbc
= kzalloc(sizeof(struct drbd_backing_dev
), GFP_KERNEL
);
1417 retcode
= ERR_NOMEM
;
1420 spin_lock_init(&nbc
->md
.uuid_lock
);
1422 new_disk_conf
= kzalloc(sizeof(struct disk_conf
), GFP_KERNEL
);
1423 if (!new_disk_conf
) {
1424 retcode
= ERR_NOMEM
;
1427 nbc
->disk_conf
= new_disk_conf
;
1429 set_disk_conf_defaults(new_disk_conf
);
1430 err
= disk_conf_from_attrs(new_disk_conf
, info
);
1432 retcode
= ERR_MANDATORY_TAG
;
1433 drbd_msg_put_info(from_attrs_err_to_txt(err
));
1437 if (new_disk_conf
->c_plan_ahead
> DRBD_C_PLAN_AHEAD_MAX
)
1438 new_disk_conf
->c_plan_ahead
= DRBD_C_PLAN_AHEAD_MAX
;
1440 new_plan
= fifo_alloc((new_disk_conf
->c_plan_ahead
* 10 * SLEEP_TIME
) / HZ
);
1442 retcode
= ERR_NOMEM
;
1446 if (new_disk_conf
->meta_dev_idx
< DRBD_MD_INDEX_FLEX_INT
) {
1447 retcode
= ERR_MD_IDX_INVALID
;
1451 write_lock_irq(&global_state_lock
);
1452 retcode
= drbd_resync_after_valid(device
, new_disk_conf
->resync_after
);
1453 write_unlock_irq(&global_state_lock
);
1454 if (retcode
!= NO_ERROR
)
1458 nc
= rcu_dereference(first_peer_device(device
)->connection
->net_conf
);
1460 if (new_disk_conf
->fencing
== FP_STONITH
&& nc
->wire_protocol
== DRBD_PROT_A
) {
1462 retcode
= ERR_STONITH_AND_PROT_A
;
1468 bdev
= blkdev_get_by_path(new_disk_conf
->backing_dev
,
1469 FMODE_READ
| FMODE_WRITE
| FMODE_EXCL
, device
);
1471 dev_err(DEV
, "open(\"%s\") failed with %ld\n", new_disk_conf
->backing_dev
,
1473 retcode
= ERR_OPEN_DISK
;
1476 nbc
->backing_bdev
= bdev
;
1479 * meta_dev_idx >= 0: external fixed size, possibly multiple
1480 * drbd sharing one meta device. TODO in that case, paranoia
1481 * check that [md_bdev, meta_dev_idx] is not yet used by some
1482 * other drbd minor! (if you use drbd.conf + drbdadm, that
1483 * should check it for you already; but if you don't, or
1484 * someone fooled it, we need to double check here)
1486 bdev
= blkdev_get_by_path(new_disk_conf
->meta_dev
,
1487 FMODE_READ
| FMODE_WRITE
| FMODE_EXCL
,
1488 (new_disk_conf
->meta_dev_idx
< 0) ?
1489 (void *)device
: (void *)drbd_m_holder
);
1491 dev_err(DEV
, "open(\"%s\") failed with %ld\n", new_disk_conf
->meta_dev
,
1493 retcode
= ERR_OPEN_MD_DISK
;
1496 nbc
->md_bdev
= bdev
;
1498 if ((nbc
->backing_bdev
== nbc
->md_bdev
) !=
1499 (new_disk_conf
->meta_dev_idx
== DRBD_MD_INDEX_INTERNAL
||
1500 new_disk_conf
->meta_dev_idx
== DRBD_MD_INDEX_FLEX_INT
)) {
1501 retcode
= ERR_MD_IDX_INVALID
;
1505 resync_lru
= lc_create("resync", drbd_bm_ext_cache
,
1506 1, 61, sizeof(struct bm_extent
),
1507 offsetof(struct bm_extent
, lce
));
1509 retcode
= ERR_NOMEM
;
1513 /* Read our meta data super block early.
1514 * This also sets other on-disk offsets. */
1515 retcode
= drbd_md_read(device
, nbc
);
1516 if (retcode
!= NO_ERROR
)
1519 if (new_disk_conf
->al_extents
< DRBD_AL_EXTENTS_MIN
)
1520 new_disk_conf
->al_extents
= DRBD_AL_EXTENTS_MIN
;
1521 if (new_disk_conf
->al_extents
> drbd_al_extents_max(nbc
))
1522 new_disk_conf
->al_extents
= drbd_al_extents_max(nbc
);
1524 if (drbd_get_max_capacity(nbc
) < new_disk_conf
->disk_size
) {
1525 dev_err(DEV
, "max capacity %llu smaller than disk size %llu\n",
1526 (unsigned long long) drbd_get_max_capacity(nbc
),
1527 (unsigned long long) new_disk_conf
->disk_size
);
1528 retcode
= ERR_DISK_TOO_SMALL
;
1532 if (new_disk_conf
->meta_dev_idx
< 0) {
1533 max_possible_sectors
= DRBD_MAX_SECTORS_FLEX
;
1534 /* at least one MB, otherwise it does not make sense */
1535 min_md_device_sectors
= (2<<10);
1537 max_possible_sectors
= DRBD_MAX_SECTORS
;
1538 min_md_device_sectors
= MD_128MB_SECT
* (new_disk_conf
->meta_dev_idx
+ 1);
1541 if (drbd_get_capacity(nbc
->md_bdev
) < min_md_device_sectors
) {
1542 retcode
= ERR_MD_DISK_TOO_SMALL
;
1543 dev_warn(DEV
, "refusing attach: md-device too small, "
1544 "at least %llu sectors needed for this meta-disk type\n",
1545 (unsigned long long) min_md_device_sectors
);
1549 /* Make sure the new disk is big enough
1550 * (we may currently be R_PRIMARY with no local disk...) */
1551 if (drbd_get_max_capacity(nbc
) <
1552 drbd_get_capacity(device
->this_bdev
)) {
1553 retcode
= ERR_DISK_TOO_SMALL
;
1557 nbc
->known_size
= drbd_get_capacity(nbc
->backing_bdev
);
1559 if (nbc
->known_size
> max_possible_sectors
) {
1560 dev_warn(DEV
, "==> truncating very big lower level device "
1561 "to currently maximum possible %llu sectors <==\n",
1562 (unsigned long long) max_possible_sectors
);
1563 if (new_disk_conf
->meta_dev_idx
>= 0)
1564 dev_warn(DEV
, "==>> using internal or flexible "
1565 "meta data may help <<==\n");
1568 drbd_suspend_io(device
);
1569 /* also wait for the last barrier ack. */
1570 /* FIXME see also https://daiquiri.linbit/cgi-bin/bugzilla/show_bug.cgi?id=171
1571 * We need a way to either ignore barrier acks for barriers sent before a device
1572 * was attached, or a way to wait for all pending barrier acks to come in.
1573 * As barriers are counted per resource,
1574 * we'd need to suspend io on all devices of a resource.
1576 wait_event(device
->misc_wait
, !atomic_read(&device
->ap_pending_cnt
) || drbd_suspended(device
));
1577 /* and for any other previously queued work */
1578 drbd_flush_workqueue(device
);
1580 rv
= _drbd_request_state(device
, NS(disk
, D_ATTACHING
), CS_VERBOSE
);
1581 retcode
= rv
; /* FIXME: Type mismatch. */
1582 drbd_resume_io(device
);
1583 if (rv
< SS_SUCCESS
)
1586 if (!get_ldev_if_state(device
, D_ATTACHING
))
1587 goto force_diskless
;
1589 if (!device
->bitmap
) {
1590 if (drbd_bm_init(device
)) {
1591 retcode
= ERR_NOMEM
;
1592 goto force_diskless_dec
;
1596 if (device
->state
.conn
< C_CONNECTED
&&
1597 device
->state
.role
== R_PRIMARY
&&
1598 (device
->ed_uuid
& ~((u64
)1)) != (nbc
->md
.uuid
[UI_CURRENT
] & ~((u64
)1))) {
1599 dev_err(DEV
, "Can only attach to data with current UUID=%016llX\n",
1600 (unsigned long long)device
->ed_uuid
);
1601 retcode
= ERR_DATA_NOT_CURRENT
;
1602 goto force_diskless_dec
;
1605 /* Since we are diskless, fix the activity log first... */
1606 if (drbd_check_al_size(device
, new_disk_conf
)) {
1607 retcode
= ERR_NOMEM
;
1608 goto force_diskless_dec
;
1611 /* Prevent shrinking of consistent devices ! */
1612 if (drbd_md_test_flag(nbc
, MDF_CONSISTENT
) &&
1613 drbd_new_dev_size(device
, nbc
, nbc
->disk_conf
->disk_size
, 0) < nbc
->md
.la_size_sect
) {
1614 dev_warn(DEV
, "refusing to truncate a consistent device\n");
1615 retcode
= ERR_DISK_TOO_SMALL
;
1616 goto force_diskless_dec
;
1619 /* Reset the "barriers don't work" bits here, then force meta data to
1620 * be written, to ensure we determine if barriers are supported. */
1621 if (new_disk_conf
->md_flushes
)
1622 clear_bit(MD_NO_FUA
, &device
->flags
);
1624 set_bit(MD_NO_FUA
, &device
->flags
);
1626 /* Point of no return reached.
1627 * Devices and memory are no longer released by error cleanup below.
1628 * now device takes over responsibility, and the state engine should
1629 * clean it up somewhere. */
1630 D_ASSERT(device
->ldev
== NULL
);
1632 device
->resync
= resync_lru
;
1633 device
->rs_plan_s
= new_plan
;
1636 new_disk_conf
= NULL
;
1639 drbd_bump_write_ordering(first_peer_device(device
)->connection
, WO_bdev_flush
);
1641 if (drbd_md_test_flag(device
->ldev
, MDF_CRASHED_PRIMARY
))
1642 set_bit(CRASHED_PRIMARY
, &device
->flags
);
1644 clear_bit(CRASHED_PRIMARY
, &device
->flags
);
1646 if (drbd_md_test_flag(device
->ldev
, MDF_PRIMARY_IND
) &&
1647 !(device
->state
.role
== R_PRIMARY
&&
1648 first_peer_device(device
)->connection
->susp_nod
))
1649 set_bit(CRASHED_PRIMARY
, &device
->flags
);
1651 device
->send_cnt
= 0;
1652 device
->recv_cnt
= 0;
1653 device
->read_cnt
= 0;
1654 device
->writ_cnt
= 0;
1656 drbd_reconsider_max_bio_size(device
);
1658 /* If I am currently not R_PRIMARY,
1659 * but meta data primary indicator is set,
1660 * I just now recover from a hard crash,
1661 * and have been R_PRIMARY before that crash.
1663 * Now, if I had no connection before that crash
1664 * (have been degraded R_PRIMARY), chances are that
1665 * I won't find my peer now either.
1667 * In that case, and _only_ in that case,
1668 * we use the degr-wfc-timeout instead of the default,
1669 * so we can automatically recover from a crash of a
1670 * degraded but active "cluster" after a certain timeout.
1672 clear_bit(USE_DEGR_WFC_T
, &device
->flags
);
1673 if (device
->state
.role
!= R_PRIMARY
&&
1674 drbd_md_test_flag(device
->ldev
, MDF_PRIMARY_IND
) &&
1675 !drbd_md_test_flag(device
->ldev
, MDF_CONNECTED_IND
))
1676 set_bit(USE_DEGR_WFC_T
, &device
->flags
);
1678 dd
= drbd_determine_dev_size(device
, 0, NULL
);
1679 if (dd
<= DS_ERROR
) {
1680 retcode
= ERR_NOMEM_BITMAP
;
1681 goto force_diskless_dec
;
1682 } else if (dd
== DS_GREW
)
1683 set_bit(RESYNC_AFTER_NEG
, &device
->flags
);
1685 if (drbd_md_test_flag(device
->ldev
, MDF_FULL_SYNC
) ||
1686 (test_bit(CRASHED_PRIMARY
, &device
->flags
) &&
1687 drbd_md_test_flag(device
->ldev
, MDF_AL_DISABLED
))) {
1688 dev_info(DEV
, "Assuming that all blocks are out of sync "
1689 "(aka FullSync)\n");
1690 if (drbd_bitmap_io(device
, &drbd_bmio_set_n_write
,
1691 "set_n_write from attaching", BM_LOCKED_MASK
)) {
1692 retcode
= ERR_IO_MD_DISK
;
1693 goto force_diskless_dec
;
1696 if (drbd_bitmap_io(device
, &drbd_bm_read
,
1697 "read from attaching", BM_LOCKED_MASK
)) {
1698 retcode
= ERR_IO_MD_DISK
;
1699 goto force_diskless_dec
;
1703 if (_drbd_bm_total_weight(device
) == drbd_bm_bits(device
))
1704 drbd_suspend_al(device
); /* IO is still suspended here... */
1706 spin_lock_irq(&first_peer_device(device
)->connection
->req_lock
);
1707 os
= drbd_read_state(device
);
1709 /* If MDF_CONSISTENT is not set go into inconsistent state,
1710 otherwise investigate MDF_WasUpToDate...
1711 If MDF_WAS_UP_TO_DATE is not set go into D_OUTDATED disk state,
1712 otherwise into D_CONSISTENT state.
1714 if (drbd_md_test_flag(device
->ldev
, MDF_CONSISTENT
)) {
1715 if (drbd_md_test_flag(device
->ldev
, MDF_WAS_UP_TO_DATE
))
1716 ns
.disk
= D_CONSISTENT
;
1718 ns
.disk
= D_OUTDATED
;
1720 ns
.disk
= D_INCONSISTENT
;
1723 if (drbd_md_test_flag(device
->ldev
, MDF_PEER_OUT_DATED
))
1724 ns
.pdsk
= D_OUTDATED
;
1727 if (ns
.disk
== D_CONSISTENT
&&
1728 (ns
.pdsk
== D_OUTDATED
|| rcu_dereference(device
->ldev
->disk_conf
)->fencing
== FP_DONT_CARE
))
1729 ns
.disk
= D_UP_TO_DATE
;
1731 /* All tests on MDF_PRIMARY_IND, MDF_CONNECTED_IND,
1732 MDF_CONSISTENT and MDF_WAS_UP_TO_DATE must happen before
1733 this point, because drbd_request_state() modifies these
1736 if (rcu_dereference(device
->ldev
->disk_conf
)->al_updates
)
1737 device
->ldev
->md
.flags
&= ~MDF_AL_DISABLED
;
1739 device
->ldev
->md
.flags
|= MDF_AL_DISABLED
;
1743 /* In case we are C_CONNECTED postpone any decision on the new disk
1744 state after the negotiation phase. */
1745 if (device
->state
.conn
== C_CONNECTED
) {
1746 device
->new_state_tmp
.i
= ns
.i
;
1748 ns
.disk
= D_NEGOTIATING
;
1750 /* We expect to receive up-to-date UUIDs soon.
1751 To avoid a race in receive_state, free p_uuid while
1752 holding req_lock. I.e. atomic with the state change */
1753 kfree(device
->p_uuid
);
1754 device
->p_uuid
= NULL
;
1757 rv
= _drbd_set_state(device
, ns
, CS_VERBOSE
, NULL
);
1758 spin_unlock_irq(&first_peer_device(device
)->connection
->req_lock
);
1760 if (rv
< SS_SUCCESS
)
1761 goto force_diskless_dec
;
1763 mod_timer(&device
->request_timer
, jiffies
+ HZ
);
1765 if (device
->state
.role
== R_PRIMARY
)
1766 device
->ldev
->md
.uuid
[UI_CURRENT
] |= (u64
)1;
1768 device
->ldev
->md
.uuid
[UI_CURRENT
] &= ~(u64
)1;
1770 drbd_md_mark_dirty(device
);
1771 drbd_md_sync(device
);
1773 kobject_uevent(&disk_to_dev(device
->vdisk
)->kobj
, KOBJ_CHANGE
);
1775 conn_reconfig_done(first_peer_device(device
)->connection
);
1776 drbd_adm_finish(info
, retcode
);
1782 drbd_force_state(device
, NS(disk
, D_DISKLESS
));
1783 drbd_md_sync(device
);
1785 conn_reconfig_done(first_peer_device(device
)->connection
);
1787 if (nbc
->backing_bdev
)
1788 blkdev_put(nbc
->backing_bdev
,
1789 FMODE_READ
| FMODE_WRITE
| FMODE_EXCL
);
1791 blkdev_put(nbc
->md_bdev
,
1792 FMODE_READ
| FMODE_WRITE
| FMODE_EXCL
);
1795 kfree(new_disk_conf
);
1796 lc_destroy(resync_lru
);
1800 drbd_adm_finish(info
, retcode
);
1804 static int adm_detach(struct drbd_device
*device
, int force
)
1806 enum drbd_state_rv retcode
;
1810 set_bit(FORCE_DETACH
, &device
->flags
);
1811 drbd_force_state(device
, NS(disk
, D_FAILED
));
1812 retcode
= SS_SUCCESS
;
1816 drbd_suspend_io(device
); /* so no-one is stuck in drbd_al_begin_io */
1817 drbd_md_get_buffer(device
); /* make sure there is no in-flight meta-data IO */
1818 retcode
= drbd_request_state(device
, NS(disk
, D_FAILED
));
1819 drbd_md_put_buffer(device
);
1820 /* D_FAILED will transition to DISKLESS. */
1821 ret
= wait_event_interruptible(device
->misc_wait
,
1822 device
->state
.disk
!= D_FAILED
);
1823 drbd_resume_io(device
);
1824 if ((int)retcode
== (int)SS_IS_DISKLESS
)
1825 retcode
= SS_NOTHING_TO_DO
;
1832 /* Detaching the disk is a process in multiple stages. First we need to lock
1833 * out application IO, in-flight IO, IO stuck in drbd_al_begin_io.
1834 * Then we transition to D_DISKLESS, and wait for put_ldev() to return all
1835 * internal references as well.
1836 * Only then we have finally detached. */
1837 int drbd_adm_detach(struct sk_buff
*skb
, struct genl_info
*info
)
1839 enum drbd_ret_code retcode
;
1840 struct detach_parms parms
= { };
1843 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_MINOR
);
1844 if (!adm_ctx
.reply_skb
)
1846 if (retcode
!= NO_ERROR
)
1849 if (info
->attrs
[DRBD_NLA_DETACH_PARMS
]) {
1850 err
= detach_parms_from_attrs(&parms
, info
);
1852 retcode
= ERR_MANDATORY_TAG
;
1853 drbd_msg_put_info(from_attrs_err_to_txt(err
));
1858 retcode
= adm_detach(adm_ctx
.device
, parms
.force_detach
);
1860 drbd_adm_finish(info
, retcode
);
1864 static bool conn_resync_running(struct drbd_connection
*connection
)
1866 struct drbd_device
*device
;
1871 idr_for_each_entry(&connection
->volumes
, device
, vnr
) {
1872 if (device
->state
.conn
== C_SYNC_SOURCE
||
1873 device
->state
.conn
== C_SYNC_TARGET
||
1874 device
->state
.conn
== C_PAUSED_SYNC_S
||
1875 device
->state
.conn
== C_PAUSED_SYNC_T
) {
1885 static bool conn_ov_running(struct drbd_connection
*connection
)
1887 struct drbd_device
*device
;
1892 idr_for_each_entry(&connection
->volumes
, device
, vnr
) {
1893 if (device
->state
.conn
== C_VERIFY_S
||
1894 device
->state
.conn
== C_VERIFY_T
) {
1904 static enum drbd_ret_code
1905 _check_net_options(struct drbd_connection
*connection
, struct net_conf
*old_conf
, struct net_conf
*new_conf
)
1907 struct drbd_device
*device
;
1910 if (old_conf
&& connection
->cstate
== C_WF_REPORT_PARAMS
&& connection
->agreed_pro_version
< 100) {
1911 if (new_conf
->wire_protocol
!= old_conf
->wire_protocol
)
1912 return ERR_NEED_APV_100
;
1914 if (new_conf
->two_primaries
!= old_conf
->two_primaries
)
1915 return ERR_NEED_APV_100
;
1917 if (strcmp(new_conf
->integrity_alg
, old_conf
->integrity_alg
))
1918 return ERR_NEED_APV_100
;
1921 if (!new_conf
->two_primaries
&&
1922 conn_highest_role(connection
) == R_PRIMARY
&&
1923 conn_highest_peer(connection
) == R_PRIMARY
)
1924 return ERR_NEED_ALLOW_TWO_PRI
;
1926 if (new_conf
->two_primaries
&&
1927 (new_conf
->wire_protocol
!= DRBD_PROT_C
))
1928 return ERR_NOT_PROTO_C
;
1930 idr_for_each_entry(&connection
->volumes
, device
, i
) {
1931 if (get_ldev(device
)) {
1932 enum drbd_fencing_p fp
= rcu_dereference(device
->ldev
->disk_conf
)->fencing
;
1934 if (new_conf
->wire_protocol
== DRBD_PROT_A
&& fp
== FP_STONITH
)
1935 return ERR_STONITH_AND_PROT_A
;
1937 if (device
->state
.role
== R_PRIMARY
&& new_conf
->discard_my_data
)
1938 return ERR_DISCARD_IMPOSSIBLE
;
1941 if (new_conf
->on_congestion
!= OC_BLOCK
&& new_conf
->wire_protocol
!= DRBD_PROT_A
)
1942 return ERR_CONG_NOT_PROTO_A
;
1947 static enum drbd_ret_code
1948 check_net_options(struct drbd_connection
*connection
, struct net_conf
*new_conf
)
1950 static enum drbd_ret_code rv
;
1951 struct drbd_device
*device
;
1955 rv
= _check_net_options(connection
, rcu_dereference(connection
->net_conf
), new_conf
);
1958 /* connection->volumes protected by genl_lock() here */
1959 idr_for_each_entry(&connection
->volumes
, device
, i
) {
1960 if (!device
->bitmap
) {
1961 if (drbd_bm_init(device
))
1970 struct crypto_hash
*verify_tfm
;
1971 struct crypto_hash
*csums_tfm
;
1972 struct crypto_hash
*cram_hmac_tfm
;
1973 struct crypto_hash
*integrity_tfm
;
1977 alloc_hash(struct crypto_hash
**tfm
, char *tfm_name
, int err_alg
)
1982 *tfm
= crypto_alloc_hash(tfm_name
, 0, CRYPTO_ALG_ASYNC
);
1991 static enum drbd_ret_code
1992 alloc_crypto(struct crypto
*crypto
, struct net_conf
*new_conf
)
1994 char hmac_name
[CRYPTO_MAX_ALG_NAME
];
1995 enum drbd_ret_code rv
;
1997 rv
= alloc_hash(&crypto
->csums_tfm
, new_conf
->csums_alg
,
2001 rv
= alloc_hash(&crypto
->verify_tfm
, new_conf
->verify_alg
,
2005 rv
= alloc_hash(&crypto
->integrity_tfm
, new_conf
->integrity_alg
,
2009 if (new_conf
->cram_hmac_alg
[0] != 0) {
2010 snprintf(hmac_name
, CRYPTO_MAX_ALG_NAME
, "hmac(%s)",
2011 new_conf
->cram_hmac_alg
);
2013 rv
= alloc_hash(&crypto
->cram_hmac_tfm
, hmac_name
,
2020 static void free_crypto(struct crypto
*crypto
)
2022 crypto_free_hash(crypto
->cram_hmac_tfm
);
2023 crypto_free_hash(crypto
->integrity_tfm
);
2024 crypto_free_hash(crypto
->csums_tfm
);
2025 crypto_free_hash(crypto
->verify_tfm
);
2028 int drbd_adm_net_opts(struct sk_buff
*skb
, struct genl_info
*info
)
2030 enum drbd_ret_code retcode
;
2031 struct drbd_connection
*connection
;
2032 struct net_conf
*old_conf
, *new_conf
= NULL
;
2034 int ovr
; /* online verify running */
2035 int rsr
; /* re-sync running */
2036 struct crypto crypto
= { };
2038 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_CONNECTION
);
2039 if (!adm_ctx
.reply_skb
)
2041 if (retcode
!= NO_ERROR
)
2044 connection
= adm_ctx
.connection
;
2046 new_conf
= kzalloc(sizeof(struct net_conf
), GFP_KERNEL
);
2048 retcode
= ERR_NOMEM
;
2052 conn_reconfig_start(connection
);
2054 mutex_lock(&connection
->data
.mutex
);
2055 mutex_lock(&connection
->conf_update
);
2056 old_conf
= connection
->net_conf
;
2059 drbd_msg_put_info("net conf missing, try connect");
2060 retcode
= ERR_INVALID_REQUEST
;
2064 *new_conf
= *old_conf
;
2065 if (should_set_defaults(info
))
2066 set_net_conf_defaults(new_conf
);
2068 err
= net_conf_from_attrs_for_change(new_conf
, info
);
2069 if (err
&& err
!= -ENOMSG
) {
2070 retcode
= ERR_MANDATORY_TAG
;
2071 drbd_msg_put_info(from_attrs_err_to_txt(err
));
2075 retcode
= check_net_options(connection
, new_conf
);
2076 if (retcode
!= NO_ERROR
)
2079 /* re-sync running */
2080 rsr
= conn_resync_running(connection
);
2081 if (rsr
&& strcmp(new_conf
->csums_alg
, old_conf
->csums_alg
)) {
2082 retcode
= ERR_CSUMS_RESYNC_RUNNING
;
2086 /* online verify running */
2087 ovr
= conn_ov_running(connection
);
2088 if (ovr
&& strcmp(new_conf
->verify_alg
, old_conf
->verify_alg
)) {
2089 retcode
= ERR_VERIFY_RUNNING
;
2093 retcode
= alloc_crypto(&crypto
, new_conf
);
2094 if (retcode
!= NO_ERROR
)
2097 rcu_assign_pointer(connection
->net_conf
, new_conf
);
2100 crypto_free_hash(connection
->csums_tfm
);
2101 connection
->csums_tfm
= crypto
.csums_tfm
;
2102 crypto
.csums_tfm
= NULL
;
2105 crypto_free_hash(connection
->verify_tfm
);
2106 connection
->verify_tfm
= crypto
.verify_tfm
;
2107 crypto
.verify_tfm
= NULL
;
2110 crypto_free_hash(connection
->integrity_tfm
);
2111 connection
->integrity_tfm
= crypto
.integrity_tfm
;
2112 if (connection
->cstate
>= C_WF_REPORT_PARAMS
&& connection
->agreed_pro_version
>= 100)
2113 /* Do this without trying to take connection->data.mutex again. */
2114 __drbd_send_protocol(connection
, P_PROTOCOL_UPDATE
);
2116 crypto_free_hash(connection
->cram_hmac_tfm
);
2117 connection
->cram_hmac_tfm
= crypto
.cram_hmac_tfm
;
2119 mutex_unlock(&connection
->conf_update
);
2120 mutex_unlock(&connection
->data
.mutex
);
2124 if (connection
->cstate
>= C_WF_REPORT_PARAMS
)
2125 drbd_send_sync_param(minor_to_device(conn_lowest_minor(connection
)));
2130 mutex_unlock(&connection
->conf_update
);
2131 mutex_unlock(&connection
->data
.mutex
);
2132 free_crypto(&crypto
);
2135 conn_reconfig_done(connection
);
2137 drbd_adm_finish(info
, retcode
);
2141 int drbd_adm_connect(struct sk_buff
*skb
, struct genl_info
*info
)
2143 struct drbd_device
*device
;
2144 struct net_conf
*old_conf
, *new_conf
= NULL
;
2145 struct crypto crypto
= { };
2146 struct drbd_connection
*connection
;
2147 enum drbd_ret_code retcode
;
2151 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_RESOURCE
);
2153 if (!adm_ctx
.reply_skb
)
2155 if (retcode
!= NO_ERROR
)
2157 if (!(adm_ctx
.my_addr
&& adm_ctx
.peer_addr
)) {
2158 drbd_msg_put_info("connection endpoint(s) missing");
2159 retcode
= ERR_INVALID_REQUEST
;
2163 /* No need for _rcu here. All reconfiguration is
2164 * strictly serialized on genl_lock(). We are protected against
2165 * concurrent reconfiguration/addition/deletion */
2166 list_for_each_entry(connection
, &drbd_connections
, connections
) {
2167 if (nla_len(adm_ctx
.my_addr
) == connection
->my_addr_len
&&
2168 !memcmp(nla_data(adm_ctx
.my_addr
), &connection
->my_addr
, connection
->my_addr_len
)) {
2169 retcode
= ERR_LOCAL_ADDR
;
2173 if (nla_len(adm_ctx
.peer_addr
) == connection
->peer_addr_len
&&
2174 !memcmp(nla_data(adm_ctx
.peer_addr
), &connection
->peer_addr
, connection
->peer_addr_len
)) {
2175 retcode
= ERR_PEER_ADDR
;
2180 connection
= adm_ctx
.connection
;
2181 conn_reconfig_start(connection
);
2183 if (connection
->cstate
> C_STANDALONE
) {
2184 retcode
= ERR_NET_CONFIGURED
;
2188 /* allocation not in the IO path, drbdsetup / netlink process context */
2189 new_conf
= kzalloc(sizeof(*new_conf
), GFP_KERNEL
);
2191 retcode
= ERR_NOMEM
;
2195 set_net_conf_defaults(new_conf
);
2197 err
= net_conf_from_attrs(new_conf
, info
);
2198 if (err
&& err
!= -ENOMSG
) {
2199 retcode
= ERR_MANDATORY_TAG
;
2200 drbd_msg_put_info(from_attrs_err_to_txt(err
));
2204 retcode
= check_net_options(connection
, new_conf
);
2205 if (retcode
!= NO_ERROR
)
2208 retcode
= alloc_crypto(&crypto
, new_conf
);
2209 if (retcode
!= NO_ERROR
)
2212 ((char *)new_conf
->shared_secret
)[SHARED_SECRET_MAX
-1] = 0;
2214 conn_flush_workqueue(connection
);
2216 mutex_lock(&connection
->conf_update
);
2217 old_conf
= connection
->net_conf
;
2219 retcode
= ERR_NET_CONFIGURED
;
2220 mutex_unlock(&connection
->conf_update
);
2223 rcu_assign_pointer(connection
->net_conf
, new_conf
);
2225 conn_free_crypto(connection
);
2226 connection
->cram_hmac_tfm
= crypto
.cram_hmac_tfm
;
2227 connection
->integrity_tfm
= crypto
.integrity_tfm
;
2228 connection
->csums_tfm
= crypto
.csums_tfm
;
2229 connection
->verify_tfm
= crypto
.verify_tfm
;
2231 connection
->my_addr_len
= nla_len(adm_ctx
.my_addr
);
2232 memcpy(&connection
->my_addr
, nla_data(adm_ctx
.my_addr
), connection
->my_addr_len
);
2233 connection
->peer_addr_len
= nla_len(adm_ctx
.peer_addr
);
2234 memcpy(&connection
->peer_addr
, nla_data(adm_ctx
.peer_addr
), connection
->peer_addr_len
);
2236 mutex_unlock(&connection
->conf_update
);
2239 idr_for_each_entry(&connection
->volumes
, device
, i
) {
2240 device
->send_cnt
= 0;
2241 device
->recv_cnt
= 0;
2245 retcode
= conn_request_state(connection
, NS(conn
, C_UNCONNECTED
), CS_VERBOSE
);
2247 conn_reconfig_done(connection
);
2248 drbd_adm_finish(info
, retcode
);
2252 free_crypto(&crypto
);
2255 conn_reconfig_done(connection
);
2257 drbd_adm_finish(info
, retcode
);
2261 static enum drbd_state_rv
conn_try_disconnect(struct drbd_connection
*connection
, bool force
)
2263 enum drbd_state_rv rv
;
2265 rv
= conn_request_state(connection
, NS(conn
, C_DISCONNECTING
),
2266 force
? CS_HARD
: 0);
2269 case SS_NOTHING_TO_DO
:
2271 case SS_ALREADY_STANDALONE
:
2273 case SS_PRIMARY_NOP
:
2274 /* Our state checking code wants to see the peer outdated. */
2275 rv
= conn_request_state(connection
, NS2(conn
, C_DISCONNECTING
, pdsk
, D_OUTDATED
), 0);
2277 if (rv
== SS_OUTDATE_WO_CONN
) /* lost connection before graceful disconnect succeeded */
2278 rv
= conn_request_state(connection
, NS(conn
, C_DISCONNECTING
), CS_VERBOSE
);
2281 case SS_CW_FAILED_BY_PEER
:
2282 /* The peer probably wants to see us outdated. */
2283 rv
= conn_request_state(connection
, NS2(conn
, C_DISCONNECTING
,
2284 disk
, D_OUTDATED
), 0);
2285 if (rv
== SS_IS_DISKLESS
|| rv
== SS_LOWER_THAN_OUTDATED
) {
2286 rv
= conn_request_state(connection
, NS(conn
, C_DISCONNECTING
),
2291 /* no special handling necessary */
2294 if (rv
>= SS_SUCCESS
) {
2295 enum drbd_state_rv rv2
;
2296 /* No one else can reconfigure the network while I am here.
2297 * The state handling only uses drbd_thread_stop_nowait(),
2298 * we want to really wait here until the receiver is no more.
2300 drbd_thread_stop(&adm_ctx
.connection
->receiver
);
2302 /* Race breaker. This additional state change request may be
2303 * necessary, if this was a forced disconnect during a receiver
2304 * restart. We may have "killed" the receiver thread just
2305 * after drbdd_init() returned. Typically, we should be
2306 * C_STANDALONE already, now, and this becomes a no-op.
2308 rv2
= conn_request_state(connection
, NS(conn
, C_STANDALONE
),
2309 CS_VERBOSE
| CS_HARD
);
2310 if (rv2
< SS_SUCCESS
)
2311 conn_err(connection
,
2312 "unexpected rv2=%d in conn_try_disconnect()\n",
2318 int drbd_adm_disconnect(struct sk_buff
*skb
, struct genl_info
*info
)
2320 struct disconnect_parms parms
;
2321 struct drbd_connection
*connection
;
2322 enum drbd_state_rv rv
;
2323 enum drbd_ret_code retcode
;
2326 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_CONNECTION
);
2327 if (!adm_ctx
.reply_skb
)
2329 if (retcode
!= NO_ERROR
)
2332 connection
= adm_ctx
.connection
;
2333 memset(&parms
, 0, sizeof(parms
));
2334 if (info
->attrs
[DRBD_NLA_DISCONNECT_PARMS
]) {
2335 err
= disconnect_parms_from_attrs(&parms
, info
);
2337 retcode
= ERR_MANDATORY_TAG
;
2338 drbd_msg_put_info(from_attrs_err_to_txt(err
));
2343 rv
= conn_try_disconnect(connection
, parms
.force_disconnect
);
2344 if (rv
< SS_SUCCESS
)
2345 retcode
= rv
; /* FIXME: Type mismatch. */
2349 drbd_adm_finish(info
, retcode
);
2353 void resync_after_online_grow(struct drbd_device
*device
)
2355 int iass
; /* I am sync source */
2357 dev_info(DEV
, "Resync of new storage after online grow\n");
2358 if (device
->state
.role
!= device
->state
.peer
)
2359 iass
= (device
->state
.role
== R_PRIMARY
);
2361 iass
= test_bit(RESOLVE_CONFLICTS
, &first_peer_device(device
)->connection
->flags
);
2364 drbd_start_resync(device
, C_SYNC_SOURCE
);
2366 _drbd_request_state(device
, NS(conn
, C_WF_SYNC_UUID
), CS_VERBOSE
+ CS_SERIALIZE
);
2369 int drbd_adm_resize(struct sk_buff
*skb
, struct genl_info
*info
)
2371 struct disk_conf
*old_disk_conf
, *new_disk_conf
= NULL
;
2372 struct resize_parms rs
;
2373 struct drbd_device
*device
;
2374 enum drbd_ret_code retcode
;
2375 enum determine_dev_size dd
;
2376 bool change_al_layout
= false;
2377 enum dds_flags ddsf
;
2381 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_MINOR
);
2382 if (!adm_ctx
.reply_skb
)
2384 if (retcode
!= NO_ERROR
)
2387 device
= adm_ctx
.device
;
2388 if (!get_ldev(device
)) {
2389 retcode
= ERR_NO_DISK
;
2393 memset(&rs
, 0, sizeof(struct resize_parms
));
2394 rs
.al_stripes
= device
->ldev
->md
.al_stripes
;
2395 rs
.al_stripe_size
= device
->ldev
->md
.al_stripe_size_4k
* 4;
2396 if (info
->attrs
[DRBD_NLA_RESIZE_PARMS
]) {
2397 err
= resize_parms_from_attrs(&rs
, info
);
2399 retcode
= ERR_MANDATORY_TAG
;
2400 drbd_msg_put_info(from_attrs_err_to_txt(err
));
2405 if (device
->state
.conn
> C_CONNECTED
) {
2406 retcode
= ERR_RESIZE_RESYNC
;
2410 if (device
->state
.role
== R_SECONDARY
&&
2411 device
->state
.peer
== R_SECONDARY
) {
2412 retcode
= ERR_NO_PRIMARY
;
2416 if (rs
.no_resync
&& first_peer_device(device
)->connection
->agreed_pro_version
< 93) {
2417 retcode
= ERR_NEED_APV_93
;
2422 u_size
= rcu_dereference(device
->ldev
->disk_conf
)->disk_size
;
2424 if (u_size
!= (sector_t
)rs
.resize_size
) {
2425 new_disk_conf
= kmalloc(sizeof(struct disk_conf
), GFP_KERNEL
);
2426 if (!new_disk_conf
) {
2427 retcode
= ERR_NOMEM
;
2432 if (device
->ldev
->md
.al_stripes
!= rs
.al_stripes
||
2433 device
->ldev
->md
.al_stripe_size_4k
!= rs
.al_stripe_size
/ 4) {
2434 u32 al_size_k
= rs
.al_stripes
* rs
.al_stripe_size
;
2436 if (al_size_k
> (16 * 1024 * 1024)) {
2437 retcode
= ERR_MD_LAYOUT_TOO_BIG
;
2441 if (al_size_k
< MD_32kB_SECT
/2) {
2442 retcode
= ERR_MD_LAYOUT_TOO_SMALL
;
2446 if (device
->state
.conn
!= C_CONNECTED
) {
2447 retcode
= ERR_MD_LAYOUT_CONNECTED
;
2451 change_al_layout
= true;
2454 if (device
->ldev
->known_size
!= drbd_get_capacity(device
->ldev
->backing_bdev
))
2455 device
->ldev
->known_size
= drbd_get_capacity(device
->ldev
->backing_bdev
);
2457 if (new_disk_conf
) {
2458 mutex_lock(&first_peer_device(device
)->connection
->conf_update
);
2459 old_disk_conf
= device
->ldev
->disk_conf
;
2460 *new_disk_conf
= *old_disk_conf
;
2461 new_disk_conf
->disk_size
= (sector_t
)rs
.resize_size
;
2462 rcu_assign_pointer(device
->ldev
->disk_conf
, new_disk_conf
);
2463 mutex_unlock(&first_peer_device(device
)->connection
->conf_update
);
2465 kfree(old_disk_conf
);
2468 ddsf
= (rs
.resize_force
? DDSF_FORCED
: 0) | (rs
.no_resync
? DDSF_NO_RESYNC
: 0);
2469 dd
= drbd_determine_dev_size(device
, ddsf
, change_al_layout
? &rs
: NULL
);
2470 drbd_md_sync(device
);
2472 if (dd
== DS_ERROR
) {
2473 retcode
= ERR_NOMEM_BITMAP
;
2475 } else if (dd
== DS_ERROR_SPACE_MD
) {
2476 retcode
= ERR_MD_LAYOUT_NO_FIT
;
2478 } else if (dd
== DS_ERROR_SHRINK
) {
2479 retcode
= ERR_IMPLICIT_SHRINK
;
2483 if (device
->state
.conn
== C_CONNECTED
) {
2485 set_bit(RESIZE_PENDING
, &device
->flags
);
2487 drbd_send_uuids(device
);
2488 drbd_send_sizes(device
, 1, ddsf
);
2492 drbd_adm_finish(info
, retcode
);
2500 int drbd_adm_resource_opts(struct sk_buff
*skb
, struct genl_info
*info
)
2502 enum drbd_ret_code retcode
;
2503 struct drbd_connection
*connection
;
2504 struct res_opts res_opts
;
2507 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_RESOURCE
);
2508 if (!adm_ctx
.reply_skb
)
2510 if (retcode
!= NO_ERROR
)
2512 connection
= adm_ctx
.connection
;
2514 res_opts
= connection
->res_opts
;
2515 if (should_set_defaults(info
))
2516 set_res_opts_defaults(&res_opts
);
2518 err
= res_opts_from_attrs(&res_opts
, info
);
2519 if (err
&& err
!= -ENOMSG
) {
2520 retcode
= ERR_MANDATORY_TAG
;
2521 drbd_msg_put_info(from_attrs_err_to_txt(err
));
2525 err
= set_resource_options(connection
, &res_opts
);
2527 retcode
= ERR_INVALID_REQUEST
;
2529 retcode
= ERR_NOMEM
;
2533 drbd_adm_finish(info
, retcode
);
2537 int drbd_adm_invalidate(struct sk_buff
*skb
, struct genl_info
*info
)
2539 struct drbd_device
*device
;
2540 int retcode
; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2542 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_MINOR
);
2543 if (!adm_ctx
.reply_skb
)
2545 if (retcode
!= NO_ERROR
)
2548 device
= adm_ctx
.device
;
2550 /* If there is still bitmap IO pending, probably because of a previous
2551 * resync just being finished, wait for it before requesting a new resync.
2552 * Also wait for it's after_state_ch(). */
2553 drbd_suspend_io(device
);
2554 wait_event(device
->misc_wait
, !test_bit(BITMAP_IO
, &device
->flags
));
2555 drbd_flush_workqueue(device
);
2557 /* If we happen to be C_STANDALONE R_SECONDARY, just change to
2558 * D_INCONSISTENT, and set all bits in the bitmap. Otherwise,
2559 * try to start a resync handshake as sync target for full sync.
2561 if (device
->state
.conn
== C_STANDALONE
&& device
->state
.role
== R_SECONDARY
) {
2562 retcode
= drbd_request_state(device
, NS(disk
, D_INCONSISTENT
));
2563 if (retcode
>= SS_SUCCESS
) {
2564 if (drbd_bitmap_io(device
, &drbd_bmio_set_n_write
,
2565 "set_n_write from invalidate", BM_LOCKED_MASK
))
2566 retcode
= ERR_IO_MD_DISK
;
2569 retcode
= drbd_request_state(device
, NS(conn
, C_STARTING_SYNC_T
));
2570 drbd_resume_io(device
);
2573 drbd_adm_finish(info
, retcode
);
2577 static int drbd_adm_simple_request_state(struct sk_buff
*skb
, struct genl_info
*info
,
2578 union drbd_state mask
, union drbd_state val
)
2580 enum drbd_ret_code retcode
;
2582 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_MINOR
);
2583 if (!adm_ctx
.reply_skb
)
2585 if (retcode
!= NO_ERROR
)
2588 retcode
= drbd_request_state(adm_ctx
.device
, mask
, val
);
2590 drbd_adm_finish(info
, retcode
);
2594 static int drbd_bmio_set_susp_al(struct drbd_device
*device
)
2598 rv
= drbd_bmio_set_n_write(device
);
2599 drbd_suspend_al(device
);
2603 int drbd_adm_invalidate_peer(struct sk_buff
*skb
, struct genl_info
*info
)
2605 int retcode
; /* drbd_ret_code, drbd_state_rv */
2606 struct drbd_device
*device
;
2608 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_MINOR
);
2609 if (!adm_ctx
.reply_skb
)
2611 if (retcode
!= NO_ERROR
)
2614 device
= adm_ctx
.device
;
2616 /* If there is still bitmap IO pending, probably because of a previous
2617 * resync just being finished, wait for it before requesting a new resync.
2618 * Also wait for it's after_state_ch(). */
2619 drbd_suspend_io(device
);
2620 wait_event(device
->misc_wait
, !test_bit(BITMAP_IO
, &device
->flags
));
2621 drbd_flush_workqueue(device
);
2623 /* If we happen to be C_STANDALONE R_PRIMARY, just set all bits
2624 * in the bitmap. Otherwise, try to start a resync handshake
2625 * as sync source for full sync.
2627 if (device
->state
.conn
== C_STANDALONE
&& device
->state
.role
== R_PRIMARY
) {
2628 /* The peer will get a resync upon connect anyways. Just make that
2629 into a full resync. */
2630 retcode
= drbd_request_state(device
, NS(pdsk
, D_INCONSISTENT
));
2631 if (retcode
>= SS_SUCCESS
) {
2632 if (drbd_bitmap_io(device
, &drbd_bmio_set_susp_al
,
2633 "set_n_write from invalidate_peer",
2634 BM_LOCKED_SET_ALLOWED
))
2635 retcode
= ERR_IO_MD_DISK
;
2638 retcode
= drbd_request_state(device
, NS(conn
, C_STARTING_SYNC_S
));
2639 drbd_resume_io(device
);
2642 drbd_adm_finish(info
, retcode
);
2646 int drbd_adm_pause_sync(struct sk_buff
*skb
, struct genl_info
*info
)
2648 enum drbd_ret_code retcode
;
2650 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_MINOR
);
2651 if (!adm_ctx
.reply_skb
)
2653 if (retcode
!= NO_ERROR
)
2656 if (drbd_request_state(adm_ctx
.device
, NS(user_isp
, 1)) == SS_NOTHING_TO_DO
)
2657 retcode
= ERR_PAUSE_IS_SET
;
2659 drbd_adm_finish(info
, retcode
);
2663 int drbd_adm_resume_sync(struct sk_buff
*skb
, struct genl_info
*info
)
2665 union drbd_dev_state s
;
2666 enum drbd_ret_code retcode
;
2668 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_MINOR
);
2669 if (!adm_ctx
.reply_skb
)
2671 if (retcode
!= NO_ERROR
)
2674 if (drbd_request_state(adm_ctx
.device
, NS(user_isp
, 0)) == SS_NOTHING_TO_DO
) {
2675 s
= adm_ctx
.device
->state
;
2676 if (s
.conn
== C_PAUSED_SYNC_S
|| s
.conn
== C_PAUSED_SYNC_T
) {
2677 retcode
= s
.aftr_isp
? ERR_PIC_AFTER_DEP
:
2678 s
.peer_isp
? ERR_PIC_PEER_DEP
: ERR_PAUSE_IS_CLEAR
;
2680 retcode
= ERR_PAUSE_IS_CLEAR
;
2685 drbd_adm_finish(info
, retcode
);
2689 int drbd_adm_suspend_io(struct sk_buff
*skb
, struct genl_info
*info
)
2691 return drbd_adm_simple_request_state(skb
, info
, NS(susp
, 1));
2694 int drbd_adm_resume_io(struct sk_buff
*skb
, struct genl_info
*info
)
2696 struct drbd_device
*device
;
2697 int retcode
; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2699 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_MINOR
);
2700 if (!adm_ctx
.reply_skb
)
2702 if (retcode
!= NO_ERROR
)
2705 device
= adm_ctx
.device
;
2706 if (test_bit(NEW_CUR_UUID
, &device
->flags
)) {
2707 drbd_uuid_new_current(device
);
2708 clear_bit(NEW_CUR_UUID
, &device
->flags
);
2710 drbd_suspend_io(device
);
2711 retcode
= drbd_request_state(device
, NS3(susp
, 0, susp_nod
, 0, susp_fen
, 0));
2712 if (retcode
== SS_SUCCESS
) {
2713 if (device
->state
.conn
< C_CONNECTED
)
2714 tl_clear(first_peer_device(device
)->connection
);
2715 if (device
->state
.disk
== D_DISKLESS
|| device
->state
.disk
== D_FAILED
)
2716 tl_restart(first_peer_device(device
)->connection
, FAIL_FROZEN_DISK_IO
);
2718 drbd_resume_io(device
);
2721 drbd_adm_finish(info
, retcode
);
2725 int drbd_adm_outdate(struct sk_buff
*skb
, struct genl_info
*info
)
2727 return drbd_adm_simple_request_state(skb
, info
, NS(disk
, D_OUTDATED
));
2730 static int nla_put_drbd_cfg_context(struct sk_buff
*skb
, struct drbd_connection
*connection
, unsigned vnr
)
2733 nla
= nla_nest_start(skb
, DRBD_NLA_CFG_CONTEXT
);
2735 goto nla_put_failure
;
2736 if (vnr
!= VOLUME_UNSPECIFIED
&&
2737 nla_put_u32(skb
, T_ctx_volume
, vnr
))
2738 goto nla_put_failure
;
2739 if (nla_put_string(skb
, T_ctx_resource_name
, connection
->name
))
2740 goto nla_put_failure
;
2741 if (connection
->my_addr_len
&&
2742 nla_put(skb
, T_ctx_my_addr
, connection
->my_addr_len
, &connection
->my_addr
))
2743 goto nla_put_failure
;
2744 if (connection
->peer_addr_len
&&
2745 nla_put(skb
, T_ctx_peer_addr
, connection
->peer_addr_len
, &connection
->peer_addr
))
2746 goto nla_put_failure
;
2747 nla_nest_end(skb
, nla
);
2752 nla_nest_cancel(skb
, nla
);
2756 static int nla_put_status_info(struct sk_buff
*skb
, struct drbd_device
*device
,
2757 const struct sib_info
*sib
)
2759 struct state_info
*si
= NULL
; /* for sizeof(si->member); */
2763 int exclude_sensitive
;
2765 /* If sib != NULL, this is drbd_bcast_event, which anyone can listen
2766 * to. So we better exclude_sensitive information.
2768 * If sib == NULL, this is drbd_adm_get_status, executed synchronously
2769 * in the context of the requesting user process. Exclude sensitive
2770 * information, unless current has superuser.
2772 * NOTE: for drbd_adm_get_status_all(), this is a netlink dump, and
2773 * relies on the current implementation of netlink_dump(), which
2774 * executes the dump callback successively from netlink_recvmsg(),
2775 * always in the context of the receiving process */
2776 exclude_sensitive
= sib
|| !capable(CAP_SYS_ADMIN
);
2778 got_ldev
= get_ldev(device
);
2780 /* We need to add connection name and volume number information still.
2781 * Minor number is in drbd_genlmsghdr. */
2782 if (nla_put_drbd_cfg_context(skb
, first_peer_device(device
)->connection
, device
->vnr
))
2783 goto nla_put_failure
;
2785 if (res_opts_to_skb(skb
, &first_peer_device(device
)->connection
->res_opts
, exclude_sensitive
))
2786 goto nla_put_failure
;
2790 struct disk_conf
*disk_conf
;
2792 disk_conf
= rcu_dereference(device
->ldev
->disk_conf
);
2793 err
= disk_conf_to_skb(skb
, disk_conf
, exclude_sensitive
);
2796 struct net_conf
*nc
;
2798 nc
= rcu_dereference(first_peer_device(device
)->connection
->net_conf
);
2800 err
= net_conf_to_skb(skb
, nc
, exclude_sensitive
);
2804 goto nla_put_failure
;
2806 nla
= nla_nest_start(skb
, DRBD_NLA_STATE_INFO
);
2808 goto nla_put_failure
;
2809 if (nla_put_u32(skb
, T_sib_reason
, sib
? sib
->sib_reason
: SIB_GET_STATUS_REPLY
) ||
2810 nla_put_u32(skb
, T_current_state
, device
->state
.i
) ||
2811 nla_put_u64(skb
, T_ed_uuid
, device
->ed_uuid
) ||
2812 nla_put_u64(skb
, T_capacity
, drbd_get_capacity(device
->this_bdev
)) ||
2813 nla_put_u64(skb
, T_send_cnt
, device
->send_cnt
) ||
2814 nla_put_u64(skb
, T_recv_cnt
, device
->recv_cnt
) ||
2815 nla_put_u64(skb
, T_read_cnt
, device
->read_cnt
) ||
2816 nla_put_u64(skb
, T_writ_cnt
, device
->writ_cnt
) ||
2817 nla_put_u64(skb
, T_al_writ_cnt
, device
->al_writ_cnt
) ||
2818 nla_put_u64(skb
, T_bm_writ_cnt
, device
->bm_writ_cnt
) ||
2819 nla_put_u32(skb
, T_ap_bio_cnt
, atomic_read(&device
->ap_bio_cnt
)) ||
2820 nla_put_u32(skb
, T_ap_pending_cnt
, atomic_read(&device
->ap_pending_cnt
)) ||
2821 nla_put_u32(skb
, T_rs_pending_cnt
, atomic_read(&device
->rs_pending_cnt
)))
2822 goto nla_put_failure
;
2827 spin_lock_irq(&device
->ldev
->md
.uuid_lock
);
2828 err
= nla_put(skb
, T_uuids
, sizeof(si
->uuids
), device
->ldev
->md
.uuid
);
2829 spin_unlock_irq(&device
->ldev
->md
.uuid_lock
);
2832 goto nla_put_failure
;
2834 if (nla_put_u32(skb
, T_disk_flags
, device
->ldev
->md
.flags
) ||
2835 nla_put_u64(skb
, T_bits_total
, drbd_bm_bits(device
)) ||
2836 nla_put_u64(skb
, T_bits_oos
, drbd_bm_total_weight(device
)))
2837 goto nla_put_failure
;
2838 if (C_SYNC_SOURCE
<= device
->state
.conn
&&
2839 C_PAUSED_SYNC_T
>= device
->state
.conn
) {
2840 if (nla_put_u64(skb
, T_bits_rs_total
, device
->rs_total
) ||
2841 nla_put_u64(skb
, T_bits_rs_failed
, device
->rs_failed
))
2842 goto nla_put_failure
;
2847 switch(sib
->sib_reason
) {
2848 case SIB_SYNC_PROGRESS
:
2849 case SIB_GET_STATUS_REPLY
:
2851 case SIB_STATE_CHANGE
:
2852 if (nla_put_u32(skb
, T_prev_state
, sib
->os
.i
) ||
2853 nla_put_u32(skb
, T_new_state
, sib
->ns
.i
))
2854 goto nla_put_failure
;
2856 case SIB_HELPER_POST
:
2857 if (nla_put_u32(skb
, T_helper_exit_code
,
2858 sib
->helper_exit_code
))
2859 goto nla_put_failure
;
2861 case SIB_HELPER_PRE
:
2862 if (nla_put_string(skb
, T_helper
, sib
->helper_name
))
2863 goto nla_put_failure
;
2867 nla_nest_end(skb
, nla
);
2877 int drbd_adm_get_status(struct sk_buff
*skb
, struct genl_info
*info
)
2879 enum drbd_ret_code retcode
;
2882 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_MINOR
);
2883 if (!adm_ctx
.reply_skb
)
2885 if (retcode
!= NO_ERROR
)
2888 err
= nla_put_status_info(adm_ctx
.reply_skb
, adm_ctx
.device
, NULL
);
2890 nlmsg_free(adm_ctx
.reply_skb
);
2894 drbd_adm_finish(info
, retcode
);
2898 static int get_one_status(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2900 struct drbd_device
*device
;
2901 struct drbd_genlmsghdr
*dh
;
2902 struct drbd_connection
*pos
= (struct drbd_connection
*)cb
->args
[0];
2903 struct drbd_connection
*connection
= NULL
;
2904 struct drbd_connection
*tmp
;
2905 unsigned volume
= cb
->args
[1];
2907 /* Open coded, deferred, iteration:
2908 * list_for_each_entry_safe(connection, tmp, &drbd_connections, connections) {
2909 * idr_for_each_entry(&connection->volumes, device, i) {
2913 * where connection is cb->args[0];
2914 * and i is cb->args[1];
2916 * cb->args[2] indicates if we shall loop over all resources,
2917 * or just dump all volumes of a single resource.
2919 * This may miss entries inserted after this dump started,
2920 * or entries deleted before they are reached.
2922 * We need to make sure the device won't disappear while
2923 * we are looking at it, and revalidate our iterators
2924 * on each iteration.
2927 /* synchronize with conn_create()/drbd_destroy_connection() */
2929 /* revalidate iterator position */
2930 list_for_each_entry_rcu(tmp
, &drbd_connections
, connections
) {
2932 /* first iteration */
2944 device
= idr_get_next(&connection
->volumes
, &volume
);
2946 /* No more volumes to dump on this connection.
2947 * Advance connection iterator. */
2948 pos
= list_entry_rcu(connection
->connections
.next
,
2949 struct drbd_connection
, connections
);
2950 /* Did we dump any volume on this connection yet? */
2952 /* If we reached the end of the list,
2953 * or only a single resource dump was requested,
2955 if (&pos
->connections
== &drbd_connections
|| cb
->args
[2])
2959 goto next_connection
;
2963 dh
= genlmsg_put(skb
, NETLINK_CB(cb
->skb
).portid
,
2964 cb
->nlh
->nlmsg_seq
, &drbd_genl_family
,
2965 NLM_F_MULTI
, DRBD_ADM_GET_STATUS
);
2970 /* This is a connection without a single volume.
2971 * Suprisingly enough, it may have a network
2973 struct net_conf
*nc
;
2975 dh
->ret_code
= NO_ERROR
;
2976 if (nla_put_drbd_cfg_context(skb
, connection
, VOLUME_UNSPECIFIED
))
2978 nc
= rcu_dereference(connection
->net_conf
);
2979 if (nc
&& net_conf_to_skb(skb
, nc
, 1) != 0)
2984 D_ASSERT(device
->vnr
== volume
);
2985 D_ASSERT(first_peer_device(device
)->connection
== connection
);
2987 dh
->minor
= device_to_minor(device
);
2988 dh
->ret_code
= NO_ERROR
;
2990 if (nla_put_status_info(skb
, device
, NULL
)) {
2992 genlmsg_cancel(skb
, dh
);
2996 genlmsg_end(skb
, dh
);
3001 /* where to start the next iteration */
3002 cb
->args
[0] = (long)pos
;
3003 cb
->args
[1] = (pos
== connection
) ? volume
+ 1 : 0;
3005 /* No more connections/volumes/minors found results in an empty skb.
3006 * Which will terminate the dump. */
3011 * Request status of all resources, or of all volumes within a single resource.
3013 * This is a dump, as the answer may not fit in a single reply skb otherwise.
3014 * Which means we cannot use the family->attrbuf or other such members, because
3015 * dump is NOT protected by the genl_lock(). During dump, we only have access
3016 * to the incoming skb, and need to opencode "parsing" of the nlattr payload.
3018 * Once things are setup properly, we call into get_one_status().
3020 int drbd_adm_get_status_all(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3022 const unsigned hdrlen
= GENL_HDRLEN
+ GENL_MAGIC_FAMILY_HDRSZ
;
3024 const char *resource_name
;
3025 struct drbd_connection
*connection
;
3028 /* Is this a followup call? */
3030 /* ... of a single resource dump,
3031 * and the resource iterator has been advanced already? */
3032 if (cb
->args
[2] && cb
->args
[2] != cb
->args
[0])
3033 return 0; /* DONE. */
3037 /* First call (from netlink_dump_start). We need to figure out
3038 * which resource(s) the user wants us to dump. */
3039 nla
= nla_find(nlmsg_attrdata(cb
->nlh
, hdrlen
),
3040 nlmsg_attrlen(cb
->nlh
, hdrlen
),
3041 DRBD_NLA_CFG_CONTEXT
);
3043 /* No explicit context given. Dump all. */
3046 maxtype
= ARRAY_SIZE(drbd_cfg_context_nl_policy
) - 1;
3047 nla
= drbd_nla_find_nested(maxtype
, nla
, __nla_type(T_ctx_resource_name
));
3049 return PTR_ERR(nla
);
3050 /* context given, but no name present? */
3053 resource_name
= nla_data(nla
);
3054 connection
= conn_get_by_name(resource_name
);
3059 kref_put(&connection
->kref
, drbd_destroy_connection
); /* get_one_status() (re)validates connection by itself */
3061 /* prime iterators, and set "filter" mode mark:
3062 * only dump this connection. */
3063 cb
->args
[0] = (long)connection
;
3064 /* cb->args[1] = 0; passed in this way. */
3065 cb
->args
[2] = (long)connection
;
3068 return get_one_status(skb
, cb
);
3071 int drbd_adm_get_timeout_type(struct sk_buff
*skb
, struct genl_info
*info
)
3073 enum drbd_ret_code retcode
;
3074 struct timeout_parms tp
;
3077 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_MINOR
);
3078 if (!adm_ctx
.reply_skb
)
3080 if (retcode
!= NO_ERROR
)
3084 adm_ctx
.device
->state
.pdsk
== D_OUTDATED
? UT_PEER_OUTDATED
:
3085 test_bit(USE_DEGR_WFC_T
, &adm_ctx
.device
->flags
) ? UT_DEGRADED
:
3088 err
= timeout_parms_to_priv_skb(adm_ctx
.reply_skb
, &tp
);
3090 nlmsg_free(adm_ctx
.reply_skb
);
3094 drbd_adm_finish(info
, retcode
);
3098 int drbd_adm_start_ov(struct sk_buff
*skb
, struct genl_info
*info
)
3100 struct drbd_device
*device
;
3101 enum drbd_ret_code retcode
;
3102 struct start_ov_parms parms
;
3104 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_MINOR
);
3105 if (!adm_ctx
.reply_skb
)
3107 if (retcode
!= NO_ERROR
)
3110 device
= adm_ctx
.device
;
3112 /* resume from last known position, if possible */
3113 parms
.ov_start_sector
= device
->ov_start_sector
;
3114 parms
.ov_stop_sector
= ULLONG_MAX
;
3115 if (info
->attrs
[DRBD_NLA_START_OV_PARMS
]) {
3116 int err
= start_ov_parms_from_attrs(&parms
, info
);
3118 retcode
= ERR_MANDATORY_TAG
;
3119 drbd_msg_put_info(from_attrs_err_to_txt(err
));
3123 /* w_make_ov_request expects position to be aligned */
3124 device
->ov_start_sector
= parms
.ov_start_sector
& ~(BM_SECT_PER_BIT
-1);
3125 device
->ov_stop_sector
= parms
.ov_stop_sector
;
3127 /* If there is still bitmap IO pending, e.g. previous resync or verify
3128 * just being finished, wait for it before requesting a new resync. */
3129 drbd_suspend_io(device
);
3130 wait_event(device
->misc_wait
, !test_bit(BITMAP_IO
, &device
->flags
));
3131 retcode
= drbd_request_state(device
, NS(conn
, C_VERIFY_S
));
3132 drbd_resume_io(device
);
3134 drbd_adm_finish(info
, retcode
);
3139 int drbd_adm_new_c_uuid(struct sk_buff
*skb
, struct genl_info
*info
)
3141 struct drbd_device
*device
;
3142 enum drbd_ret_code retcode
;
3143 int skip_initial_sync
= 0;
3145 struct new_c_uuid_parms args
;
3147 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_MINOR
);
3148 if (!adm_ctx
.reply_skb
)
3150 if (retcode
!= NO_ERROR
)
3153 device
= adm_ctx
.device
;
3154 memset(&args
, 0, sizeof(args
));
3155 if (info
->attrs
[DRBD_NLA_NEW_C_UUID_PARMS
]) {
3156 err
= new_c_uuid_parms_from_attrs(&args
, info
);
3158 retcode
= ERR_MANDATORY_TAG
;
3159 drbd_msg_put_info(from_attrs_err_to_txt(err
));
3164 mutex_lock(device
->state_mutex
); /* Protects us against serialized state changes. */
3166 if (!get_ldev(device
)) {
3167 retcode
= ERR_NO_DISK
;
3171 /* this is "skip initial sync", assume to be clean */
3172 if (device
->state
.conn
== C_CONNECTED
&&
3173 first_peer_device(device
)->connection
->agreed_pro_version
>= 90 &&
3174 device
->ldev
->md
.uuid
[UI_CURRENT
] == UUID_JUST_CREATED
&& args
.clear_bm
) {
3175 dev_info(DEV
, "Preparing to skip initial sync\n");
3176 skip_initial_sync
= 1;
3177 } else if (device
->state
.conn
!= C_STANDALONE
) {
3178 retcode
= ERR_CONNECTED
;
3182 drbd_uuid_set(device
, UI_BITMAP
, 0); /* Rotate UI_BITMAP to History 1, etc... */
3183 drbd_uuid_new_current(device
); /* New current, previous to UI_BITMAP */
3185 if (args
.clear_bm
) {
3186 err
= drbd_bitmap_io(device
, &drbd_bmio_clear_n_write
,
3187 "clear_n_write from new_c_uuid", BM_LOCKED_MASK
);
3189 dev_err(DEV
, "Writing bitmap failed with %d\n",err
);
3190 retcode
= ERR_IO_MD_DISK
;
3192 if (skip_initial_sync
) {
3193 drbd_send_uuids_skip_initial_sync(device
);
3194 _drbd_uuid_set(device
, UI_BITMAP
, 0);
3195 drbd_print_uuids(device
, "cleared bitmap UUID");
3196 spin_lock_irq(&first_peer_device(device
)->connection
->req_lock
);
3197 _drbd_set_state(_NS2(device
, disk
, D_UP_TO_DATE
, pdsk
, D_UP_TO_DATE
),
3199 spin_unlock_irq(&first_peer_device(device
)->connection
->req_lock
);
3203 drbd_md_sync(device
);
3207 mutex_unlock(device
->state_mutex
);
3209 drbd_adm_finish(info
, retcode
);
3213 static enum drbd_ret_code
3214 drbd_check_resource_name(const char *name
)
3216 if (!name
|| !name
[0]) {
3217 drbd_msg_put_info("resource name missing");
3218 return ERR_MANDATORY_TAG
;
3220 /* if we want to use these in sysfs/configfs/debugfs some day,
3221 * we must not allow slashes */
3222 if (strchr(name
, '/')) {
3223 drbd_msg_put_info("invalid resource name");
3224 return ERR_INVALID_REQUEST
;
3229 int drbd_adm_new_resource(struct sk_buff
*skb
, struct genl_info
*info
)
3231 enum drbd_ret_code retcode
;
3232 struct res_opts res_opts
;
3235 retcode
= drbd_adm_prepare(skb
, info
, 0);
3236 if (!adm_ctx
.reply_skb
)
3238 if (retcode
!= NO_ERROR
)
3241 set_res_opts_defaults(&res_opts
);
3242 err
= res_opts_from_attrs(&res_opts
, info
);
3243 if (err
&& err
!= -ENOMSG
) {
3244 retcode
= ERR_MANDATORY_TAG
;
3245 drbd_msg_put_info(from_attrs_err_to_txt(err
));
3249 retcode
= drbd_check_resource_name(adm_ctx
.resource_name
);
3250 if (retcode
!= NO_ERROR
)
3253 if (adm_ctx
.connection
) {
3254 if (info
->nlhdr
->nlmsg_flags
& NLM_F_EXCL
) {
3255 retcode
= ERR_INVALID_REQUEST
;
3256 drbd_msg_put_info("resource exists");
3258 /* else: still NO_ERROR */
3262 if (!conn_create(adm_ctx
.resource_name
, &res_opts
))
3263 retcode
= ERR_NOMEM
;
3265 drbd_adm_finish(info
, retcode
);
3269 int drbd_adm_new_minor(struct sk_buff
*skb
, struct genl_info
*info
)
3271 struct drbd_genlmsghdr
*dh
= info
->userhdr
;
3272 enum drbd_ret_code retcode
;
3274 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_RESOURCE
);
3275 if (!adm_ctx
.reply_skb
)
3277 if (retcode
!= NO_ERROR
)
3280 if (dh
->minor
> MINORMASK
) {
3281 drbd_msg_put_info("requested minor out of range");
3282 retcode
= ERR_INVALID_REQUEST
;
3285 if (adm_ctx
.volume
> DRBD_VOLUME_MAX
) {
3286 drbd_msg_put_info("requested volume id out of range");
3287 retcode
= ERR_INVALID_REQUEST
;
3291 /* drbd_adm_prepare made sure already
3292 * that first_peer_device(device)->connection and device->vnr match the request. */
3293 if (adm_ctx
.device
) {
3294 if (info
->nlhdr
->nlmsg_flags
& NLM_F_EXCL
)
3295 retcode
= ERR_MINOR_EXISTS
;
3296 /* else: still NO_ERROR */
3300 retcode
= drbd_create_minor(adm_ctx
.connection
, dh
->minor
, adm_ctx
.volume
);
3302 drbd_adm_finish(info
, retcode
);
3306 static enum drbd_ret_code
adm_del_minor(struct drbd_device
*device
)
3308 if (device
->state
.disk
== D_DISKLESS
&&
3309 /* no need to be device->state.conn == C_STANDALONE &&
3310 * we may want to delete a minor from a live replication group.
3312 device
->state
.role
== R_SECONDARY
) {
3313 _drbd_request_state(device
, NS(conn
, C_WF_REPORT_PARAMS
),
3314 CS_VERBOSE
+ CS_WAIT_COMPLETE
);
3315 idr_remove(&first_peer_device(device
)->connection
->volumes
, device
->vnr
);
3316 idr_remove(&drbd_devices
, device_to_minor(device
));
3317 destroy_workqueue(device
->submit
.wq
);
3318 del_gendisk(device
->vdisk
);
3320 kref_put(&device
->kref
, drbd_destroy_device
);
3323 return ERR_MINOR_CONFIGURED
;
3326 int drbd_adm_del_minor(struct sk_buff
*skb
, struct genl_info
*info
)
3328 enum drbd_ret_code retcode
;
3330 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_MINOR
);
3331 if (!adm_ctx
.reply_skb
)
3333 if (retcode
!= NO_ERROR
)
3336 retcode
= adm_del_minor(adm_ctx
.device
);
3338 drbd_adm_finish(info
, retcode
);
3342 int drbd_adm_down(struct sk_buff
*skb
, struct genl_info
*info
)
3344 int retcode
; /* enum drbd_ret_code rsp. enum drbd_state_rv */
3345 struct drbd_device
*device
;
3348 retcode
= drbd_adm_prepare(skb
, info
, 0);
3349 if (!adm_ctx
.reply_skb
)
3351 if (retcode
!= NO_ERROR
)
3354 if (!adm_ctx
.connection
) {
3355 retcode
= ERR_RES_NOT_KNOWN
;
3360 idr_for_each_entry(&adm_ctx
.connection
->volumes
, device
, i
) {
3361 retcode
= drbd_set_role(device
, R_SECONDARY
, 0);
3362 if (retcode
< SS_SUCCESS
) {
3363 drbd_msg_put_info("failed to demote");
3368 retcode
= conn_try_disconnect(adm_ctx
.connection
, 0);
3369 if (retcode
< SS_SUCCESS
) {
3370 drbd_msg_put_info("failed to disconnect");
3375 idr_for_each_entry(&adm_ctx
.connection
->volumes
, device
, i
) {
3376 retcode
= adm_detach(device
, 0);
3377 if (retcode
< SS_SUCCESS
|| retcode
> NO_ERROR
) {
3378 drbd_msg_put_info("failed to detach");
3383 /* If we reach this, all volumes (of this connection) are Secondary,
3384 * Disconnected, Diskless, aka Unconfigured. Make sure all threads have
3385 * actually stopped, state handling only does drbd_thread_stop_nowait(). */
3386 drbd_thread_stop(&adm_ctx
.connection
->worker
);
3388 /* Now, nothing can fail anymore */
3390 /* delete volumes */
3391 idr_for_each_entry(&adm_ctx
.connection
->volumes
, device
, i
) {
3392 retcode
= adm_del_minor(device
);
3393 if (retcode
!= NO_ERROR
) {
3394 /* "can not happen" */
3395 drbd_msg_put_info("failed to delete volume");
3400 /* delete connection */
3401 if (conn_lowest_minor(adm_ctx
.connection
) < 0) {
3402 list_del_rcu(&adm_ctx
.connection
->connections
);
3404 kref_put(&adm_ctx
.connection
->kref
, drbd_destroy_connection
);
3408 /* "can not happen" */
3409 retcode
= ERR_RES_IN_USE
;
3410 drbd_msg_put_info("failed to delete connection");
3414 drbd_adm_finish(info
, retcode
);
3418 int drbd_adm_del_resource(struct sk_buff
*skb
, struct genl_info
*info
)
3420 enum drbd_ret_code retcode
;
3422 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_RESOURCE
);
3423 if (!adm_ctx
.reply_skb
)
3425 if (retcode
!= NO_ERROR
)
3428 if (conn_lowest_minor(adm_ctx
.connection
) < 0) {
3429 list_del_rcu(&adm_ctx
.connection
->connections
);
3431 kref_put(&adm_ctx
.connection
->kref
, drbd_destroy_connection
);
3435 retcode
= ERR_RES_IN_USE
;
3438 if (retcode
== NO_ERROR
)
3439 drbd_thread_stop(&adm_ctx
.connection
->worker
);
3441 drbd_adm_finish(info
, retcode
);
3445 void drbd_bcast_event(struct drbd_device
*device
, const struct sib_info
*sib
)
3447 static atomic_t drbd_genl_seq
= ATOMIC_INIT(2); /* two. */
3448 struct sk_buff
*msg
;
3449 struct drbd_genlmsghdr
*d_out
;
3453 if (sib
->sib_reason
== SIB_SYNC_PROGRESS
) {
3454 if (time_after(jiffies
, device
->rs_last_bcast
+ HZ
))
3455 device
->rs_last_bcast
= jiffies
;
3460 seq
= atomic_inc_return(&drbd_genl_seq
);
3461 msg
= genlmsg_new(NLMSG_GOODSIZE
, GFP_NOIO
);
3466 d_out
= genlmsg_put(msg
, 0, seq
, &drbd_genl_family
, 0, DRBD_EVENT
);
3467 if (!d_out
) /* cannot happen, but anyways. */
3468 goto nla_put_failure
;
3469 d_out
->minor
= device_to_minor(device
);
3470 d_out
->ret_code
= NO_ERROR
;
3472 if (nla_put_status_info(msg
, device
, sib
))
3473 goto nla_put_failure
;
3474 genlmsg_end(msg
, d_out
);
3475 err
= drbd_genl_multicast_events(msg
, 0);
3476 /* msg has been consumed or freed in netlink_broadcast() */
3477 if (err
&& err
!= -ESRCH
)
3485 dev_err(DEV
, "Error %d while broadcasting event. "
3486 "Event seq:%u sib_reason:%u\n",
3487 err
, seq
, sib
->sib_reason
);