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_resource
*resource
;
108 struct drbd_connection
*connection
;
111 static void drbd_adm_send_reply(struct sk_buff
*skb
, struct genl_info
*info
)
113 genlmsg_end(skb
, genlmsg_data(nlmsg_data(nlmsg_hdr(skb
))));
114 if (genlmsg_reply(skb
, info
))
115 printk(KERN_ERR
"drbd: error sending genl reply\n");
118 /* Used on a fresh "drbd_adm_prepare"d reply_skb, this cannot fail: The only
119 * reason it could fail was no space in skb, and there are 4k available. */
120 int drbd_msg_put_info(const char *info
)
122 struct sk_buff
*skb
= adm_ctx
.reply_skb
;
126 if (!info
|| !info
[0])
129 nla
= nla_nest_start(skb
, DRBD_NLA_CFG_REPLY
);
133 err
= nla_put_string(skb
, T_info_text
, info
);
135 nla_nest_cancel(skb
, nla
);
138 nla_nest_end(skb
, nla
);
142 /* This would be a good candidate for a "pre_doit" hook,
143 * and per-family private info->pointers.
144 * But we need to stay compatible with older kernels.
145 * If it returns successfully, adm_ctx members are valid.
147 #define DRBD_ADM_NEED_MINOR 1
148 #define DRBD_ADM_NEED_RESOURCE 2
149 #define DRBD_ADM_NEED_CONNECTION 4
150 static int drbd_adm_prepare(struct sk_buff
*skb
, struct genl_info
*info
,
153 struct drbd_genlmsghdr
*d_in
= info
->userhdr
;
154 const u8 cmd
= info
->genlhdr
->cmd
;
157 memset(&adm_ctx
, 0, sizeof(adm_ctx
));
159 /* genl_rcv_msg only checks for CAP_NET_ADMIN on "GENL_ADMIN_PERM" :( */
160 if (cmd
!= DRBD_ADM_GET_STATUS
&& !capable(CAP_NET_ADMIN
))
163 adm_ctx
.reply_skb
= genlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
164 if (!adm_ctx
.reply_skb
) {
169 adm_ctx
.reply_dh
= genlmsg_put_reply(adm_ctx
.reply_skb
,
170 info
, &drbd_genl_family
, 0, cmd
);
171 /* put of a few bytes into a fresh skb of >= 4k will always succeed.
173 if (!adm_ctx
.reply_dh
) {
178 adm_ctx
.reply_dh
->minor
= d_in
->minor
;
179 adm_ctx
.reply_dh
->ret_code
= NO_ERROR
;
181 adm_ctx
.volume
= VOLUME_UNSPECIFIED
;
182 if (info
->attrs
[DRBD_NLA_CFG_CONTEXT
]) {
184 /* parse and validate only */
185 err
= drbd_cfg_context_from_attrs(NULL
, info
);
189 /* It was present, and valid,
190 * copy it over to the reply skb. */
191 err
= nla_put_nohdr(adm_ctx
.reply_skb
,
192 info
->attrs
[DRBD_NLA_CFG_CONTEXT
]->nla_len
,
193 info
->attrs
[DRBD_NLA_CFG_CONTEXT
]);
197 /* and assign stuff to the global adm_ctx */
198 nla
= nested_attr_tb
[__nla_type(T_ctx_volume
)];
200 adm_ctx
.volume
= nla_get_u32(nla
);
201 nla
= nested_attr_tb
[__nla_type(T_ctx_resource_name
)];
203 adm_ctx
.resource_name
= nla_data(nla
);
204 adm_ctx
.my_addr
= nested_attr_tb
[__nla_type(T_ctx_my_addr
)];
205 adm_ctx
.peer_addr
= nested_attr_tb
[__nla_type(T_ctx_peer_addr
)];
206 if ((adm_ctx
.my_addr
&&
207 nla_len(adm_ctx
.my_addr
) > sizeof(adm_ctx
.connection
->my_addr
)) ||
208 (adm_ctx
.peer_addr
&&
209 nla_len(adm_ctx
.peer_addr
) > sizeof(adm_ctx
.connection
->peer_addr
))) {
215 adm_ctx
.minor
= d_in
->minor
;
216 adm_ctx
.device
= minor_to_device(d_in
->minor
);
217 if (adm_ctx
.resource_name
) {
218 adm_ctx
.resource
= drbd_find_resource(adm_ctx
.resource_name
);
219 if (adm_ctx
.resource
) {
220 adm_ctx
.connection
= first_connection(adm_ctx
.resource
);
221 kref_get(&adm_ctx
.connection
->kref
);
225 if (!adm_ctx
.device
&& (flags
& DRBD_ADM_NEED_MINOR
)) {
226 drbd_msg_put_info("unknown minor");
227 return ERR_MINOR_INVALID
;
229 if (!adm_ctx
.resource
&& (flags
& DRBD_ADM_NEED_RESOURCE
)) {
230 drbd_msg_put_info("unknown resource");
231 if (adm_ctx
.resource_name
)
232 return ERR_RES_NOT_KNOWN
;
233 return ERR_INVALID_REQUEST
;
236 if (flags
& DRBD_ADM_NEED_CONNECTION
) {
237 if (adm_ctx
.connection
&& !(flags
& DRBD_ADM_NEED_RESOURCE
)) {
238 drbd_msg_put_info("no resource name expected");
239 return ERR_INVALID_REQUEST
;
241 if (adm_ctx
.device
) {
242 drbd_msg_put_info("no minor number expected");
243 return ERR_INVALID_REQUEST
;
245 if (adm_ctx
.my_addr
&& adm_ctx
.peer_addr
)
246 adm_ctx
.connection
= conn_get_by_addrs(nla_data(adm_ctx
.my_addr
),
247 nla_len(adm_ctx
.my_addr
),
248 nla_data(adm_ctx
.peer_addr
),
249 nla_len(adm_ctx
.peer_addr
));
250 if (!adm_ctx
.connection
) {
251 drbd_msg_put_info("unknown connection");
252 return ERR_INVALID_REQUEST
;
256 /* some more paranoia, if the request was over-determined */
257 if (adm_ctx
.device
&& adm_ctx
.resource
&&
258 adm_ctx
.device
->resource
!= adm_ctx
.resource
) {
259 pr_warning("request: minor=%u, resource=%s; but that minor belongs to resource %s\n",
260 adm_ctx
.minor
, adm_ctx
.resource
->name
,
261 adm_ctx
.device
->resource
->name
);
262 drbd_msg_put_info("minor exists in different resource");
263 return ERR_INVALID_REQUEST
;
265 if (adm_ctx
.device
&&
266 adm_ctx
.volume
!= VOLUME_UNSPECIFIED
&&
267 adm_ctx
.volume
!= adm_ctx
.device
->vnr
) {
268 pr_warning("request: minor=%u, volume=%u; but that minor is volume %u in %s\n",
269 adm_ctx
.minor
, adm_ctx
.volume
,
271 adm_ctx
.device
->resource
->name
);
272 drbd_msg_put_info("minor exists as different volume");
273 return ERR_INVALID_REQUEST
;
279 nlmsg_free(adm_ctx
.reply_skb
);
280 adm_ctx
.reply_skb
= NULL
;
284 static int drbd_adm_finish(struct genl_info
*info
, int retcode
)
286 if (adm_ctx
.connection
) {
287 kref_put(&adm_ctx
.connection
->kref
, drbd_destroy_connection
);
288 adm_ctx
.connection
= NULL
;
290 if (adm_ctx
.resource
) {
291 kref_put(&adm_ctx
.resource
->kref
, drbd_destroy_resource
);
292 adm_ctx
.resource
= NULL
;
295 if (!adm_ctx
.reply_skb
)
298 adm_ctx
.reply_dh
->ret_code
= retcode
;
299 drbd_adm_send_reply(adm_ctx
.reply_skb
, info
);
303 static void setup_khelper_env(struct drbd_connection
*connection
, char **envp
)
307 /* FIXME: A future version will not allow this case. */
308 if (connection
->my_addr_len
== 0 || connection
->peer_addr_len
== 0)
311 switch (((struct sockaddr
*)&connection
->peer_addr
)->sa_family
) {
314 snprintf(envp
[4], 60, "DRBD_PEER_ADDRESS=%pI6",
315 &((struct sockaddr_in6
*)&connection
->peer_addr
)->sin6_addr
);
319 snprintf(envp
[4], 60, "DRBD_PEER_ADDRESS=%pI4",
320 &((struct sockaddr_in
*)&connection
->peer_addr
)->sin_addr
);
324 snprintf(envp
[4], 60, "DRBD_PEER_ADDRESS=%pI4",
325 &((struct sockaddr_in
*)&connection
->peer_addr
)->sin_addr
);
327 snprintf(envp
[3], 20, "DRBD_PEER_AF=%s", afs
);
330 int drbd_khelper(struct drbd_device
*device
, char *cmd
)
332 char *envp
[] = { "HOME=/",
334 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
335 (char[20]) { }, /* address family */
336 (char[60]) { }, /* address */
339 char *argv
[] = {usermode_helper
, cmd
, mb
, NULL
};
340 struct drbd_connection
*connection
= first_peer_device(device
)->connection
;
344 if (current
== connection
->worker
.task
)
345 set_bit(CALLBACK_PENDING
, &connection
->flags
);
347 snprintf(mb
, 12, "minor-%d", device_to_minor(device
));
348 setup_khelper_env(connection
, envp
);
350 /* The helper may take some time.
351 * write out any unsynced meta data changes now */
352 drbd_md_sync(device
);
354 drbd_info(device
, "helper command: %s %s %s\n", usermode_helper
, cmd
, mb
);
355 sib
.sib_reason
= SIB_HELPER_PRE
;
356 sib
.helper_name
= cmd
;
357 drbd_bcast_event(device
, &sib
);
358 ret
= call_usermodehelper(usermode_helper
, argv
, envp
, UMH_WAIT_PROC
);
360 drbd_warn(device
, "helper command: %s %s %s exit code %u (0x%x)\n",
361 usermode_helper
, cmd
, mb
,
362 (ret
>> 8) & 0xff, ret
);
364 drbd_info(device
, "helper command: %s %s %s exit code %u (0x%x)\n",
365 usermode_helper
, cmd
, mb
,
366 (ret
>> 8) & 0xff, ret
);
367 sib
.sib_reason
= SIB_HELPER_POST
;
368 sib
.helper_exit_code
= ret
;
369 drbd_bcast_event(device
, &sib
);
371 if (current
== connection
->worker
.task
)
372 clear_bit(CALLBACK_PENDING
, &connection
->flags
);
374 if (ret
< 0) /* Ignore any ERRNOs we got. */
380 static int conn_khelper(struct drbd_connection
*connection
, char *cmd
)
382 char *envp
[] = { "HOME=/",
384 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
385 (char[20]) { }, /* address family */
386 (char[60]) { }, /* address */
388 char *resource_name
= connection
->resource
->name
;
389 char *argv
[] = {usermode_helper
, cmd
, resource_name
, NULL
};
392 setup_khelper_env(connection
, envp
);
393 conn_md_sync(connection
);
395 drbd_info(connection
, "helper command: %s %s %s\n", usermode_helper
, cmd
, resource_name
);
396 /* TODO: conn_bcast_event() ?? */
398 ret
= call_usermodehelper(usermode_helper
, argv
, envp
, UMH_WAIT_PROC
);
400 drbd_warn(connection
, "helper command: %s %s %s exit code %u (0x%x)\n",
401 usermode_helper
, cmd
, resource_name
,
402 (ret
>> 8) & 0xff, ret
);
404 drbd_info(connection
, "helper command: %s %s %s exit code %u (0x%x)\n",
405 usermode_helper
, cmd
, resource_name
,
406 (ret
>> 8) & 0xff, ret
);
407 /* TODO: conn_bcast_event() ?? */
409 if (ret
< 0) /* Ignore any ERRNOs we got. */
415 static enum drbd_fencing_p
highest_fencing_policy(struct drbd_connection
*connection
)
417 enum drbd_fencing_p fp
= FP_NOT_AVAIL
;
418 struct drbd_peer_device
*peer_device
;
422 idr_for_each_entry(&connection
->peer_devices
, peer_device
, vnr
) {
423 struct drbd_device
*device
= peer_device
->device
;
424 if (get_ldev_if_state(device
, D_CONSISTENT
)) {
425 struct disk_conf
*disk_conf
=
426 rcu_dereference(peer_device
->device
->ldev
->disk_conf
);
427 fp
= max_t(enum drbd_fencing_p
, fp
, disk_conf
->fencing
);
436 bool conn_try_outdate_peer(struct drbd_connection
*connection
)
438 unsigned int connect_cnt
;
439 union drbd_state mask
= { };
440 union drbd_state val
= { };
441 enum drbd_fencing_p fp
;
445 if (connection
->cstate
>= C_WF_REPORT_PARAMS
) {
446 drbd_err(connection
, "Expected cstate < C_WF_REPORT_PARAMS\n");
450 spin_lock_irq(&connection
->req_lock
);
451 connect_cnt
= connection
->connect_cnt
;
452 spin_unlock_irq(&connection
->req_lock
);
454 fp
= highest_fencing_policy(connection
);
457 drbd_warn(connection
, "Not fencing peer, I'm not even Consistent myself.\n");
464 r
= conn_khelper(connection
, "fence-peer");
466 switch ((r
>>8) & 0xff) {
467 case 3: /* peer is inconsistent */
468 ex_to_string
= "peer is inconsistent or worse";
470 val
.pdsk
= D_INCONSISTENT
;
472 case 4: /* peer got outdated, or was already outdated */
473 ex_to_string
= "peer was fenced";
475 val
.pdsk
= D_OUTDATED
;
477 case 5: /* peer was down */
478 if (conn_highest_disk(connection
) == D_UP_TO_DATE
) {
479 /* we will(have) create(d) a new UUID anyways... */
480 ex_to_string
= "peer is unreachable, assumed to be dead";
482 val
.pdsk
= D_OUTDATED
;
484 ex_to_string
= "peer unreachable, doing nothing since disk != UpToDate";
487 case 6: /* Peer is primary, voluntarily outdate myself.
488 * This is useful when an unconnected R_SECONDARY is asked to
489 * become R_PRIMARY, but finds the other peer being active. */
490 ex_to_string
= "peer is active";
491 drbd_warn(connection
, "Peer is primary, outdating myself.\n");
493 val
.disk
= D_OUTDATED
;
496 if (fp
!= FP_STONITH
)
497 drbd_err(connection
, "fence-peer() = 7 && fencing != Stonith !!!\n");
498 ex_to_string
= "peer was stonithed";
500 val
.pdsk
= D_OUTDATED
;
503 /* The script is broken ... */
504 drbd_err(connection
, "fence-peer helper broken, returned %d\n", (r
>>8)&0xff);
505 return false; /* Eventually leave IO frozen */
508 drbd_info(connection
, "fence-peer helper returned %d (%s)\n",
509 (r
>>8) & 0xff, ex_to_string
);
514 conn_request_state(connection, mask, val, CS_VERBOSE);
515 here, because we might were able to re-establish the connection in the
517 spin_lock_irq(&connection
->req_lock
);
518 if (connection
->cstate
< C_WF_REPORT_PARAMS
&& !test_bit(STATE_SENT
, &connection
->flags
)) {
519 if (connection
->connect_cnt
!= connect_cnt
)
520 /* In case the connection was established and droped
521 while the fence-peer handler was running, ignore it */
522 drbd_info(connection
, "Ignoring fence-peer exit code\n");
524 _conn_request_state(connection
, mask
, val
, CS_VERBOSE
);
526 spin_unlock_irq(&connection
->req_lock
);
528 return conn_highest_pdsk(connection
) <= D_OUTDATED
;
531 static int _try_outdate_peer_async(void *data
)
533 struct drbd_connection
*connection
= (struct drbd_connection
*)data
;
535 conn_try_outdate_peer(connection
);
537 kref_put(&connection
->kref
, drbd_destroy_connection
);
541 void conn_try_outdate_peer_async(struct drbd_connection
*connection
)
543 struct task_struct
*opa
;
545 kref_get(&connection
->kref
);
546 opa
= kthread_run(_try_outdate_peer_async
, connection
, "drbd_async_h");
548 drbd_err(connection
, "out of mem, failed to invoke fence-peer helper\n");
549 kref_put(&connection
->kref
, drbd_destroy_connection
);
554 drbd_set_role(struct drbd_device
*device
, enum drbd_role new_role
, int force
)
556 const int max_tries
= 4;
557 enum drbd_state_rv rv
= SS_UNKNOWN_ERROR
;
561 union drbd_state mask
, val
;
563 if (new_role
== R_PRIMARY
)
564 request_ping(first_peer_device(device
)->connection
); /* Detect a dead peer ASAP */
566 mutex_lock(device
->state_mutex
);
568 mask
.i
= 0; mask
.role
= R_MASK
;
569 val
.i
= 0; val
.role
= new_role
;
571 while (try++ < max_tries
) {
572 rv
= _drbd_request_state(device
, mask
, val
, CS_WAIT_COMPLETE
);
574 /* in case we first succeeded to outdate,
575 * but now suddenly could establish a connection */
576 if (rv
== SS_CW_FAILED_BY_PEER
&& mask
.pdsk
!= 0) {
582 if (rv
== SS_NO_UP_TO_DATE_DISK
&& force
&&
583 (device
->state
.disk
< D_UP_TO_DATE
&&
584 device
->state
.disk
>= D_INCONSISTENT
)) {
586 val
.disk
= D_UP_TO_DATE
;
591 if (rv
== SS_NO_UP_TO_DATE_DISK
&&
592 device
->state
.disk
== D_CONSISTENT
&& mask
.pdsk
== 0) {
593 D_ASSERT(device
, device
->state
.pdsk
== D_UNKNOWN
);
595 if (conn_try_outdate_peer(first_peer_device(device
)->connection
)) {
596 val
.disk
= D_UP_TO_DATE
;
602 if (rv
== SS_NOTHING_TO_DO
)
604 if (rv
== SS_PRIMARY_NOP
&& mask
.pdsk
== 0) {
605 if (!conn_try_outdate_peer(first_peer_device(device
)->connection
) && force
) {
606 drbd_warn(device
, "Forced into split brain situation!\n");
608 val
.pdsk
= D_OUTDATED
;
613 if (rv
== SS_TWO_PRIMARIES
) {
614 /* Maybe the peer is detected as dead very soon...
615 retry at most once more in this case. */
618 nc
= rcu_dereference(first_peer_device(device
)->connection
->net_conf
);
619 timeo
= nc
? (nc
->ping_timeo
+ 1) * HZ
/ 10 : 1;
621 schedule_timeout_interruptible(timeo
);
626 if (rv
< SS_SUCCESS
) {
627 rv
= _drbd_request_state(device
, mask
, val
,
628 CS_VERBOSE
+ CS_WAIT_COMPLETE
);
639 drbd_warn(device
, "Forced to consider local data as UpToDate!\n");
641 /* Wait until nothing is on the fly :) */
642 wait_event(device
->misc_wait
, atomic_read(&device
->ap_pending_cnt
) == 0);
644 /* FIXME also wait for all pending P_BARRIER_ACK? */
646 if (new_role
== R_SECONDARY
) {
647 set_disk_ro(device
->vdisk
, true);
648 if (get_ldev(device
)) {
649 device
->ldev
->md
.uuid
[UI_CURRENT
] &= ~(u64
)1;
653 mutex_lock(&first_peer_device(device
)->connection
->conf_update
);
654 nc
= first_peer_device(device
)->connection
->net_conf
;
656 nc
->discard_my_data
= 0; /* without copy; single bit op is atomic */
657 mutex_unlock(&first_peer_device(device
)->connection
->conf_update
);
659 set_disk_ro(device
->vdisk
, false);
660 if (get_ldev(device
)) {
661 if (((device
->state
.conn
< C_CONNECTED
||
662 device
->state
.pdsk
<= D_FAILED
)
663 && device
->ldev
->md
.uuid
[UI_BITMAP
] == 0) || forced
)
664 drbd_uuid_new_current(device
);
666 device
->ldev
->md
.uuid
[UI_CURRENT
] |= (u64
)1;
671 /* writeout of activity log covered areas of the bitmap
672 * to stable storage done in after state change already */
674 if (device
->state
.conn
>= C_WF_REPORT_PARAMS
) {
675 /* if this was forced, we should consider sync */
677 drbd_send_uuids(device
);
678 drbd_send_current_state(device
);
681 drbd_md_sync(device
);
683 kobject_uevent(&disk_to_dev(device
->vdisk
)->kobj
, KOBJ_CHANGE
);
685 mutex_unlock(device
->state_mutex
);
689 static const char *from_attrs_err_to_txt(int err
)
691 return err
== -ENOMSG
? "required attribute missing" :
692 err
== -EOPNOTSUPP
? "unknown mandatory attribute" :
693 err
== -EEXIST
? "can not change invariant setting" :
694 "invalid attribute value";
697 int drbd_adm_set_role(struct sk_buff
*skb
, struct genl_info
*info
)
699 struct set_role_parms parms
;
701 enum drbd_ret_code retcode
;
703 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_MINOR
);
704 if (!adm_ctx
.reply_skb
)
706 if (retcode
!= NO_ERROR
)
709 memset(&parms
, 0, sizeof(parms
));
710 if (info
->attrs
[DRBD_NLA_SET_ROLE_PARMS
]) {
711 err
= set_role_parms_from_attrs(&parms
, info
);
713 retcode
= ERR_MANDATORY_TAG
;
714 drbd_msg_put_info(from_attrs_err_to_txt(err
));
719 if (info
->genlhdr
->cmd
== DRBD_ADM_PRIMARY
)
720 retcode
= drbd_set_role(adm_ctx
.device
, R_PRIMARY
, parms
.assume_uptodate
);
722 retcode
= drbd_set_role(adm_ctx
.device
, R_SECONDARY
, 0);
724 drbd_adm_finish(info
, retcode
);
728 /* Initializes the md.*_offset members, so we are able to find
729 * the on disk meta data.
731 * We currently have two possible layouts:
733 * |----------- md_size_sect ------------------|
734 * [ 4k superblock ][ activity log ][ Bitmap ]
736 * | bm_offset = al_offset + X |
737 * ==> bitmap sectors = md_size_sect - bm_offset
740 * |----------- md_size_sect ------------------|
741 * [data.....][ Bitmap ][ activity log ][ 4k superblock ]
743 * | bm_offset = al_offset - Y |
744 * ==> bitmap sectors = Y = al_offset - bm_offset
746 * Activity log size used to be fixed 32kB,
747 * but is about to become configurable.
749 static void drbd_md_set_sector_offsets(struct drbd_device
*device
,
750 struct drbd_backing_dev
*bdev
)
752 sector_t md_size_sect
= 0;
753 unsigned int al_size_sect
= bdev
->md
.al_size_4k
* 8;
755 bdev
->md
.md_offset
= drbd_md_ss(bdev
);
757 switch (bdev
->md
.meta_dev_idx
) {
759 /* v07 style fixed size indexed meta data */
760 bdev
->md
.md_size_sect
= MD_128MB_SECT
;
761 bdev
->md
.al_offset
= MD_4kB_SECT
;
762 bdev
->md
.bm_offset
= MD_4kB_SECT
+ al_size_sect
;
764 case DRBD_MD_INDEX_FLEX_EXT
:
765 /* just occupy the full device; unit: sectors */
766 bdev
->md
.md_size_sect
= drbd_get_capacity(bdev
->md_bdev
);
767 bdev
->md
.al_offset
= MD_4kB_SECT
;
768 bdev
->md
.bm_offset
= MD_4kB_SECT
+ al_size_sect
;
770 case DRBD_MD_INDEX_INTERNAL
:
771 case DRBD_MD_INDEX_FLEX_INT
:
772 /* al size is still fixed */
773 bdev
->md
.al_offset
= -al_size_sect
;
774 /* we need (slightly less than) ~ this much bitmap sectors: */
775 md_size_sect
= drbd_get_capacity(bdev
->backing_bdev
);
776 md_size_sect
= ALIGN(md_size_sect
, BM_SECT_PER_EXT
);
777 md_size_sect
= BM_SECT_TO_EXT(md_size_sect
);
778 md_size_sect
= ALIGN(md_size_sect
, 8);
780 /* plus the "drbd meta data super block",
781 * and the activity log; */
782 md_size_sect
+= MD_4kB_SECT
+ al_size_sect
;
784 bdev
->md
.md_size_sect
= md_size_sect
;
785 /* bitmap offset is adjusted by 'super' block size */
786 bdev
->md
.bm_offset
= -md_size_sect
+ MD_4kB_SECT
;
791 /* input size is expected to be in KB */
792 char *ppsize(char *buf
, unsigned long long size
)
794 /* Needs 9 bytes at max including trailing NUL:
795 * -1ULL ==> "16384 EB" */
796 static char units
[] = { 'K', 'M', 'G', 'T', 'P', 'E' };
798 while (size
>= 10000 && base
< sizeof(units
)-1) {
800 size
= (size
>> 10) + !!(size
& (1<<9));
803 sprintf(buf
, "%u %cB", (unsigned)size
, units
[base
]);
808 /* there is still a theoretical deadlock when called from receiver
809 * on an D_INCONSISTENT R_PRIMARY:
810 * remote READ does inc_ap_bio, receiver would need to receive answer
811 * packet from remote to dec_ap_bio again.
812 * receiver receive_sizes(), comes here,
813 * waits for ap_bio_cnt == 0. -> deadlock.
814 * but this cannot happen, actually, because:
815 * R_PRIMARY D_INCONSISTENT, and peer's disk is unreachable
816 * (not connected, or bad/no disk on peer):
817 * see drbd_fail_request_early, ap_bio_cnt is zero.
818 * R_PRIMARY D_INCONSISTENT, and C_SYNC_TARGET:
819 * peer may not initiate a resize.
821 /* Note these are not to be confused with
822 * drbd_adm_suspend_io/drbd_adm_resume_io,
823 * which are (sub) state changes triggered by admin (drbdsetup),
824 * and can be long lived.
825 * This changes an device->flag, is triggered by drbd internals,
826 * and should be short-lived. */
827 void drbd_suspend_io(struct drbd_device
*device
)
829 set_bit(SUSPEND_IO
, &device
->flags
);
830 if (drbd_suspended(device
))
832 wait_event(device
->misc_wait
, !atomic_read(&device
->ap_bio_cnt
));
835 void drbd_resume_io(struct drbd_device
*device
)
837 clear_bit(SUSPEND_IO
, &device
->flags
);
838 wake_up(&device
->misc_wait
);
842 * drbd_determine_dev_size() - Sets the right device size obeying all constraints
843 * @device: DRBD device.
845 * Returns 0 on success, negative return values indicate errors.
846 * You should call drbd_md_sync() after calling this function.
848 enum determine_dev_size
849 drbd_determine_dev_size(struct drbd_device
*device
, enum dds_flags flags
, struct resize_parms
*rs
) __must_hold(local
)
851 sector_t prev_first_sect
, prev_size
; /* previous meta location */
852 sector_t la_size_sect
, u_size
;
853 struct drbd_md
*md
= &device
->ldev
->md
;
854 u32 prev_al_stripe_size_4k
;
860 int md_moved
, la_size_changed
;
861 enum determine_dev_size rv
= DS_UNCHANGED
;
864 * application request passes inc_ap_bio,
865 * but then cannot get an AL-reference.
866 * this function later may wait on ap_bio_cnt == 0. -> deadlock.
869 * Suspend IO right here.
870 * still lock the act_log to not trigger ASSERTs there.
872 drbd_suspend_io(device
);
873 buffer
= drbd_md_get_buffer(device
); /* Lock meta-data IO */
875 drbd_resume_io(device
);
879 /* no wait necessary anymore, actually we could assert that */
880 wait_event(device
->al_wait
, lc_try_lock(device
->act_log
));
882 prev_first_sect
= drbd_md_first_sector(device
->ldev
);
883 prev_size
= device
->ldev
->md
.md_size_sect
;
884 la_size_sect
= device
->ldev
->md
.la_size_sect
;
887 /* rs is non NULL if we should change the AL layout only */
889 prev_al_stripes
= md
->al_stripes
;
890 prev_al_stripe_size_4k
= md
->al_stripe_size_4k
;
892 md
->al_stripes
= rs
->al_stripes
;
893 md
->al_stripe_size_4k
= rs
->al_stripe_size
/ 4;
894 md
->al_size_4k
= (u64
)rs
->al_stripes
* rs
->al_stripe_size
/ 4;
897 drbd_md_set_sector_offsets(device
, device
->ldev
);
900 u_size
= rcu_dereference(device
->ldev
->disk_conf
)->disk_size
;
902 size
= drbd_new_dev_size(device
, device
->ldev
, u_size
, flags
& DDSF_FORCED
);
904 if (size
< la_size_sect
) {
905 if (rs
&& u_size
== 0) {
906 /* Remove "rs &&" later. This check should always be active, but
907 right now the receiver expects the permissive behavior */
908 drbd_warn(device
, "Implicit shrink not allowed. "
909 "Use --size=%llus for explicit shrink.\n",
910 (unsigned long long)size
);
911 rv
= DS_ERROR_SHRINK
;
914 rv
= DS_ERROR_SPACE_MD
;
915 if (rv
!= DS_UNCHANGED
)
919 if (drbd_get_capacity(device
->this_bdev
) != size
||
920 drbd_bm_capacity(device
) != size
) {
922 err
= drbd_bm_resize(device
, size
, !(flags
& DDSF_NO_RESYNC
));
924 /* currently there is only one error: ENOMEM! */
925 size
= drbd_bm_capacity(device
)>>1;
927 drbd_err(device
, "OUT OF MEMORY! "
928 "Could not allocate bitmap!\n");
930 drbd_err(device
, "BM resizing failed. "
931 "Leaving size unchanged at size = %lu KB\n",
932 (unsigned long)size
);
936 /* racy, see comments above. */
937 drbd_set_my_capacity(device
, size
);
938 device
->ldev
->md
.la_size_sect
= size
;
939 drbd_info(device
, "size = %s (%llu KB)\n", ppsize(ppb
, size
>>1),
940 (unsigned long long)size
>>1);
945 la_size_changed
= (la_size_sect
!= device
->ldev
->md
.la_size_sect
);
947 md_moved
= prev_first_sect
!= drbd_md_first_sector(device
->ldev
)
948 || prev_size
!= device
->ldev
->md
.md_size_sect
;
950 if (la_size_changed
|| md_moved
|| rs
) {
953 drbd_al_shrink(device
); /* All extents inactive. */
955 prev_flags
= md
->flags
;
956 md
->flags
&= ~MDF_PRIMARY_IND
;
957 drbd_md_write(device
, buffer
);
959 drbd_info(device
, "Writing the whole bitmap, %s\n",
960 la_size_changed
&& md_moved
? "size changed and md moved" :
961 la_size_changed
? "size changed" : "md moved");
962 /* next line implicitly does drbd_suspend_io()+drbd_resume_io() */
963 drbd_bitmap_io(device
, md_moved
? &drbd_bm_write_all
: &drbd_bm_write
,
964 "size changed", BM_LOCKED_MASK
);
965 drbd_initialize_al(device
, buffer
);
967 md
->flags
= prev_flags
;
968 drbd_md_write(device
, buffer
);
971 drbd_info(device
, "Changed AL layout to al-stripes = %d, al-stripe-size-kB = %d\n",
972 md
->al_stripes
, md
->al_stripe_size_4k
* 4);
975 if (size
> la_size_sect
)
976 rv
= la_size_sect
? DS_GREW
: DS_GREW_FROM_ZERO
;
977 if (size
< la_size_sect
)
983 md
->al_stripes
= prev_al_stripes
;
984 md
->al_stripe_size_4k
= prev_al_stripe_size_4k
;
985 md
->al_size_4k
= (u64
)prev_al_stripes
* prev_al_stripe_size_4k
;
987 drbd_md_set_sector_offsets(device
, device
->ldev
);
990 lc_unlock(device
->act_log
);
991 wake_up(&device
->al_wait
);
992 drbd_md_put_buffer(device
);
993 drbd_resume_io(device
);
999 drbd_new_dev_size(struct drbd_device
*device
, struct drbd_backing_dev
*bdev
,
1000 sector_t u_size
, int assume_peer_has_space
)
1002 sector_t p_size
= device
->p_size
; /* partner's disk size. */
1003 sector_t la_size_sect
= bdev
->md
.la_size_sect
; /* last agreed size. */
1004 sector_t m_size
; /* my size */
1007 m_size
= drbd_get_max_capacity(bdev
);
1009 if (device
->state
.conn
< C_CONNECTED
&& assume_peer_has_space
) {
1010 drbd_warn(device
, "Resize while not connected was forced by the user!\n");
1014 if (p_size
&& m_size
) {
1015 size
= min_t(sector_t
, p_size
, m_size
);
1018 size
= la_size_sect
;
1019 if (m_size
&& m_size
< size
)
1021 if (p_size
&& p_size
< size
)
1032 drbd_err(device
, "Both nodes diskless!\n");
1036 drbd_err(device
, "Requested disk size is too big (%lu > %lu)\n",
1037 (unsigned long)u_size
>>1, (unsigned long)size
>>1);
1046 * drbd_check_al_size() - Ensures that the AL is of the right size
1047 * @device: DRBD device.
1049 * Returns -EBUSY if current al lru is still used, -ENOMEM when allocation
1050 * failed, and 0 on success. You should call drbd_md_sync() after you called
1053 static int drbd_check_al_size(struct drbd_device
*device
, struct disk_conf
*dc
)
1055 struct lru_cache
*n
, *t
;
1056 struct lc_element
*e
;
1057 unsigned int in_use
;
1060 if (device
->act_log
&&
1061 device
->act_log
->nr_elements
== dc
->al_extents
)
1065 t
= device
->act_log
;
1066 n
= lc_create("act_log", drbd_al_ext_cache
, AL_UPDATES_PER_TRANSACTION
,
1067 dc
->al_extents
, sizeof(struct lc_element
), 0);
1070 drbd_err(device
, "Cannot allocate act_log lru!\n");
1073 spin_lock_irq(&device
->al_lock
);
1075 for (i
= 0; i
< t
->nr_elements
; i
++) {
1076 e
= lc_element_by_index(t
, i
);
1078 drbd_err(device
, "refcnt(%d)==%d\n",
1079 e
->lc_number
, e
->refcnt
);
1080 in_use
+= e
->refcnt
;
1084 device
->act_log
= n
;
1085 spin_unlock_irq(&device
->al_lock
);
1087 drbd_err(device
, "Activity log still in use!\n");
1094 drbd_md_mark_dirty(device
); /* we changed device->act_log->nr_elemens */
1098 static void drbd_setup_queue_param(struct drbd_device
*device
, unsigned int max_bio_size
)
1100 struct request_queue
* const q
= device
->rq_queue
;
1101 unsigned int max_hw_sectors
= max_bio_size
>> 9;
1102 unsigned int max_segments
= 0;
1104 if (get_ldev_if_state(device
, D_ATTACHING
)) {
1105 struct request_queue
* const b
= device
->ldev
->backing_bdev
->bd_disk
->queue
;
1107 max_hw_sectors
= min(queue_max_hw_sectors(b
), max_bio_size
>> 9);
1109 max_segments
= rcu_dereference(device
->ldev
->disk_conf
)->max_bio_bvecs
;
1114 blk_queue_logical_block_size(q
, 512);
1115 blk_queue_max_hw_sectors(q
, max_hw_sectors
);
1116 /* This is the workaround for "bio would need to, but cannot, be split" */
1117 blk_queue_max_segments(q
, max_segments
? max_segments
: BLK_MAX_SEGMENTS
);
1118 blk_queue_segment_boundary(q
, PAGE_CACHE_SIZE
-1);
1120 if (get_ldev_if_state(device
, D_ATTACHING
)) {
1121 struct request_queue
* const b
= device
->ldev
->backing_bdev
->bd_disk
->queue
;
1123 blk_queue_stack_limits(q
, b
);
1125 if (q
->backing_dev_info
.ra_pages
!= b
->backing_dev_info
.ra_pages
) {
1126 drbd_info(device
, "Adjusting my ra_pages to backing device's (%lu -> %lu)\n",
1127 q
->backing_dev_info
.ra_pages
,
1128 b
->backing_dev_info
.ra_pages
);
1129 q
->backing_dev_info
.ra_pages
= b
->backing_dev_info
.ra_pages
;
1135 void drbd_reconsider_max_bio_size(struct drbd_device
*device
)
1137 unsigned int now
, new, local
, peer
;
1139 now
= queue_max_hw_sectors(device
->rq_queue
) << 9;
1140 local
= device
->local_max_bio_size
; /* Eventually last known value, from volatile memory */
1141 peer
= device
->peer_max_bio_size
; /* Eventually last known value, from meta data */
1143 if (get_ldev_if_state(device
, D_ATTACHING
)) {
1144 local
= queue_max_hw_sectors(device
->ldev
->backing_bdev
->bd_disk
->queue
) << 9;
1145 device
->local_max_bio_size
= local
;
1148 local
= min(local
, DRBD_MAX_BIO_SIZE
);
1150 /* We may ignore peer limits if the peer is modern enough.
1151 Because new from 8.3.8 onwards the peer can use multiple
1152 BIOs for a single peer_request */
1153 if (device
->state
.conn
>= C_WF_REPORT_PARAMS
) {
1154 if (first_peer_device(device
)->connection
->agreed_pro_version
< 94)
1155 peer
= min(device
->peer_max_bio_size
, DRBD_MAX_SIZE_H80_PACKET
);
1156 /* Correct old drbd (up to 8.3.7) if it believes it can do more than 32KiB */
1157 else if (first_peer_device(device
)->connection
->agreed_pro_version
== 94)
1158 peer
= DRBD_MAX_SIZE_H80_PACKET
;
1159 else if (first_peer_device(device
)->connection
->agreed_pro_version
< 100)
1160 peer
= DRBD_MAX_BIO_SIZE_P95
; /* drbd 8.3.8 onwards, before 8.4.0 */
1162 peer
= DRBD_MAX_BIO_SIZE
;
1165 new = min(local
, peer
);
1167 if (device
->state
.role
== R_PRIMARY
&& new < now
)
1168 drbd_err(device
, "ASSERT FAILED new < now; (%u < %u)\n", new, now
);
1171 drbd_info(device
, "max BIO size = %u\n", new);
1173 drbd_setup_queue_param(device
, new);
1176 /* Starts the worker thread */
1177 static void conn_reconfig_start(struct drbd_connection
*connection
)
1179 drbd_thread_start(&connection
->worker
);
1180 conn_flush_workqueue(connection
);
1183 /* if still unconfigured, stops worker again. */
1184 static void conn_reconfig_done(struct drbd_connection
*connection
)
1187 spin_lock_irq(&connection
->req_lock
);
1188 stop_threads
= conn_all_vols_unconf(connection
) &&
1189 connection
->cstate
== C_STANDALONE
;
1190 spin_unlock_irq(&connection
->req_lock
);
1192 /* asender is implicitly stopped by receiver
1193 * in conn_disconnect() */
1194 drbd_thread_stop(&connection
->receiver
);
1195 drbd_thread_stop(&connection
->worker
);
1199 /* Make sure IO is suspended before calling this function(). */
1200 static void drbd_suspend_al(struct drbd_device
*device
)
1204 if (!lc_try_lock(device
->act_log
)) {
1205 drbd_warn(device
, "Failed to lock al in drbd_suspend_al()\n");
1209 drbd_al_shrink(device
);
1210 spin_lock_irq(&first_peer_device(device
)->connection
->req_lock
);
1211 if (device
->state
.conn
< C_CONNECTED
)
1212 s
= !test_and_set_bit(AL_SUSPENDED
, &device
->flags
);
1213 spin_unlock_irq(&first_peer_device(device
)->connection
->req_lock
);
1214 lc_unlock(device
->act_log
);
1217 drbd_info(device
, "Suspended AL updates\n");
1221 static bool should_set_defaults(struct genl_info
*info
)
1223 unsigned flags
= ((struct drbd_genlmsghdr
*)info
->userhdr
)->flags
;
1224 return 0 != (flags
& DRBD_GENL_F_SET_DEFAULTS
);
1227 static unsigned int drbd_al_extents_max(struct drbd_backing_dev
*bdev
)
1229 /* This is limited by 16 bit "slot" numbers,
1230 * and by available on-disk context storage.
1232 * Also (u16)~0 is special (denotes a "free" extent).
1234 * One transaction occupies one 4kB on-disk block,
1235 * we have n such blocks in the on disk ring buffer,
1236 * the "current" transaction may fail (n-1),
1237 * and there is 919 slot numbers context information per transaction.
1239 * 72 transaction blocks amounts to more than 2**16 context slots,
1240 * so cap there first.
1242 const unsigned int max_al_nr
= DRBD_AL_EXTENTS_MAX
;
1243 const unsigned int sufficient_on_disk
=
1244 (max_al_nr
+ AL_CONTEXT_PER_TRANSACTION
-1)
1245 /AL_CONTEXT_PER_TRANSACTION
;
1247 unsigned int al_size_4k
= bdev
->md
.al_size_4k
;
1249 if (al_size_4k
> sufficient_on_disk
)
1252 return (al_size_4k
- 1) * AL_CONTEXT_PER_TRANSACTION
;
1255 int drbd_adm_disk_opts(struct sk_buff
*skb
, struct genl_info
*info
)
1257 enum drbd_ret_code retcode
;
1258 struct drbd_device
*device
;
1259 struct disk_conf
*new_disk_conf
, *old_disk_conf
;
1260 struct fifo_buffer
*old_plan
= NULL
, *new_plan
= NULL
;
1263 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_MINOR
);
1264 if (!adm_ctx
.reply_skb
)
1266 if (retcode
!= NO_ERROR
)
1269 device
= adm_ctx
.device
;
1271 /* we also need a disk
1272 * to change the options on */
1273 if (!get_ldev(device
)) {
1274 retcode
= ERR_NO_DISK
;
1278 new_disk_conf
= kmalloc(sizeof(struct disk_conf
), GFP_KERNEL
);
1279 if (!new_disk_conf
) {
1280 retcode
= ERR_NOMEM
;
1284 mutex_lock(&first_peer_device(device
)->connection
->conf_update
);
1285 old_disk_conf
= device
->ldev
->disk_conf
;
1286 *new_disk_conf
= *old_disk_conf
;
1287 if (should_set_defaults(info
))
1288 set_disk_conf_defaults(new_disk_conf
);
1290 err
= disk_conf_from_attrs_for_change(new_disk_conf
, info
);
1291 if (err
&& err
!= -ENOMSG
) {
1292 retcode
= ERR_MANDATORY_TAG
;
1293 drbd_msg_put_info(from_attrs_err_to_txt(err
));
1297 if (!expect(new_disk_conf
->resync_rate
>= 1))
1298 new_disk_conf
->resync_rate
= 1;
1300 if (new_disk_conf
->al_extents
< DRBD_AL_EXTENTS_MIN
)
1301 new_disk_conf
->al_extents
= DRBD_AL_EXTENTS_MIN
;
1302 if (new_disk_conf
->al_extents
> drbd_al_extents_max(device
->ldev
))
1303 new_disk_conf
->al_extents
= drbd_al_extents_max(device
->ldev
);
1305 if (new_disk_conf
->c_plan_ahead
> DRBD_C_PLAN_AHEAD_MAX
)
1306 new_disk_conf
->c_plan_ahead
= DRBD_C_PLAN_AHEAD_MAX
;
1308 fifo_size
= (new_disk_conf
->c_plan_ahead
* 10 * SLEEP_TIME
) / HZ
;
1309 if (fifo_size
!= device
->rs_plan_s
->size
) {
1310 new_plan
= fifo_alloc(fifo_size
);
1312 drbd_err(device
, "kmalloc of fifo_buffer failed");
1313 retcode
= ERR_NOMEM
;
1318 drbd_suspend_io(device
);
1319 wait_event(device
->al_wait
, lc_try_lock(device
->act_log
));
1320 drbd_al_shrink(device
);
1321 err
= drbd_check_al_size(device
, new_disk_conf
);
1322 lc_unlock(device
->act_log
);
1323 wake_up(&device
->al_wait
);
1324 drbd_resume_io(device
);
1327 retcode
= ERR_NOMEM
;
1331 write_lock_irq(&global_state_lock
);
1332 retcode
= drbd_resync_after_valid(device
, new_disk_conf
->resync_after
);
1333 if (retcode
== NO_ERROR
) {
1334 rcu_assign_pointer(device
->ldev
->disk_conf
, new_disk_conf
);
1335 drbd_resync_after_changed(device
);
1337 write_unlock_irq(&global_state_lock
);
1339 if (retcode
!= NO_ERROR
)
1343 old_plan
= device
->rs_plan_s
;
1344 rcu_assign_pointer(device
->rs_plan_s
, new_plan
);
1347 mutex_unlock(&first_peer_device(device
)->connection
->conf_update
);
1349 if (new_disk_conf
->al_updates
)
1350 device
->ldev
->md
.flags
&= ~MDF_AL_DISABLED
;
1352 device
->ldev
->md
.flags
|= MDF_AL_DISABLED
;
1354 if (new_disk_conf
->md_flushes
)
1355 clear_bit(MD_NO_FUA
, &device
->flags
);
1357 set_bit(MD_NO_FUA
, &device
->flags
);
1359 drbd_bump_write_ordering(first_peer_device(device
)->connection
, WO_bdev_flush
);
1361 drbd_md_sync(device
);
1363 if (device
->state
.conn
>= C_CONNECTED
)
1364 drbd_send_sync_param(device
);
1367 kfree(old_disk_conf
);
1369 mod_timer(&device
->request_timer
, jiffies
+ HZ
);
1373 mutex_unlock(&first_peer_device(device
)->connection
->conf_update
);
1375 kfree(new_disk_conf
);
1380 drbd_adm_finish(info
, retcode
);
1384 int drbd_adm_attach(struct sk_buff
*skb
, struct genl_info
*info
)
1386 struct drbd_device
*device
;
1388 enum drbd_ret_code retcode
;
1389 enum determine_dev_size dd
;
1390 sector_t max_possible_sectors
;
1391 sector_t min_md_device_sectors
;
1392 struct drbd_backing_dev
*nbc
= NULL
; /* new_backing_conf */
1393 struct disk_conf
*new_disk_conf
= NULL
;
1394 struct block_device
*bdev
;
1395 struct lru_cache
*resync_lru
= NULL
;
1396 struct fifo_buffer
*new_plan
= NULL
;
1397 union drbd_state ns
, os
;
1398 enum drbd_state_rv rv
;
1399 struct net_conf
*nc
;
1401 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_MINOR
);
1402 if (!adm_ctx
.reply_skb
)
1404 if (retcode
!= NO_ERROR
)
1407 device
= adm_ctx
.device
;
1408 conn_reconfig_start(first_peer_device(device
)->connection
);
1410 /* if you want to reconfigure, please tear down first */
1411 if (device
->state
.disk
> D_DISKLESS
) {
1412 retcode
= ERR_DISK_CONFIGURED
;
1415 /* It may just now have detached because of IO error. Make sure
1416 * drbd_ldev_destroy is done already, we may end up here very fast,
1417 * e.g. if someone calls attach from the on-io-error handler,
1418 * to realize a "hot spare" feature (not that I'd recommend that) */
1419 wait_event(device
->misc_wait
, !atomic_read(&device
->local_cnt
));
1421 /* make sure there is no leftover from previous force-detach attempts */
1422 clear_bit(FORCE_DETACH
, &device
->flags
);
1423 clear_bit(WAS_IO_ERROR
, &device
->flags
);
1424 clear_bit(WAS_READ_ERROR
, &device
->flags
);
1426 /* and no leftover from previously aborted resync or verify, either */
1427 device
->rs_total
= 0;
1428 device
->rs_failed
= 0;
1429 atomic_set(&device
->rs_pending_cnt
, 0);
1431 /* allocation not in the IO path, drbdsetup context */
1432 nbc
= kzalloc(sizeof(struct drbd_backing_dev
), GFP_KERNEL
);
1434 retcode
= ERR_NOMEM
;
1437 spin_lock_init(&nbc
->md
.uuid_lock
);
1439 new_disk_conf
= kzalloc(sizeof(struct disk_conf
), GFP_KERNEL
);
1440 if (!new_disk_conf
) {
1441 retcode
= ERR_NOMEM
;
1444 nbc
->disk_conf
= new_disk_conf
;
1446 set_disk_conf_defaults(new_disk_conf
);
1447 err
= disk_conf_from_attrs(new_disk_conf
, info
);
1449 retcode
= ERR_MANDATORY_TAG
;
1450 drbd_msg_put_info(from_attrs_err_to_txt(err
));
1454 if (new_disk_conf
->c_plan_ahead
> DRBD_C_PLAN_AHEAD_MAX
)
1455 new_disk_conf
->c_plan_ahead
= DRBD_C_PLAN_AHEAD_MAX
;
1457 new_plan
= fifo_alloc((new_disk_conf
->c_plan_ahead
* 10 * SLEEP_TIME
) / HZ
);
1459 retcode
= ERR_NOMEM
;
1463 if (new_disk_conf
->meta_dev_idx
< DRBD_MD_INDEX_FLEX_INT
) {
1464 retcode
= ERR_MD_IDX_INVALID
;
1468 write_lock_irq(&global_state_lock
);
1469 retcode
= drbd_resync_after_valid(device
, new_disk_conf
->resync_after
);
1470 write_unlock_irq(&global_state_lock
);
1471 if (retcode
!= NO_ERROR
)
1475 nc
= rcu_dereference(first_peer_device(device
)->connection
->net_conf
);
1477 if (new_disk_conf
->fencing
== FP_STONITH
&& nc
->wire_protocol
== DRBD_PROT_A
) {
1479 retcode
= ERR_STONITH_AND_PROT_A
;
1485 bdev
= blkdev_get_by_path(new_disk_conf
->backing_dev
,
1486 FMODE_READ
| FMODE_WRITE
| FMODE_EXCL
, device
);
1488 drbd_err(device
, "open(\"%s\") failed with %ld\n", new_disk_conf
->backing_dev
,
1490 retcode
= ERR_OPEN_DISK
;
1493 nbc
->backing_bdev
= bdev
;
1496 * meta_dev_idx >= 0: external fixed size, possibly multiple
1497 * drbd sharing one meta device. TODO in that case, paranoia
1498 * check that [md_bdev, meta_dev_idx] is not yet used by some
1499 * other drbd minor! (if you use drbd.conf + drbdadm, that
1500 * should check it for you already; but if you don't, or
1501 * someone fooled it, we need to double check here)
1503 bdev
= blkdev_get_by_path(new_disk_conf
->meta_dev
,
1504 FMODE_READ
| FMODE_WRITE
| FMODE_EXCL
,
1505 (new_disk_conf
->meta_dev_idx
< 0) ?
1506 (void *)device
: (void *)drbd_m_holder
);
1508 drbd_err(device
, "open(\"%s\") failed with %ld\n", new_disk_conf
->meta_dev
,
1510 retcode
= ERR_OPEN_MD_DISK
;
1513 nbc
->md_bdev
= bdev
;
1515 if ((nbc
->backing_bdev
== nbc
->md_bdev
) !=
1516 (new_disk_conf
->meta_dev_idx
== DRBD_MD_INDEX_INTERNAL
||
1517 new_disk_conf
->meta_dev_idx
== DRBD_MD_INDEX_FLEX_INT
)) {
1518 retcode
= ERR_MD_IDX_INVALID
;
1522 resync_lru
= lc_create("resync", drbd_bm_ext_cache
,
1523 1, 61, sizeof(struct bm_extent
),
1524 offsetof(struct bm_extent
, lce
));
1526 retcode
= ERR_NOMEM
;
1530 /* Read our meta data super block early.
1531 * This also sets other on-disk offsets. */
1532 retcode
= drbd_md_read(device
, nbc
);
1533 if (retcode
!= NO_ERROR
)
1536 if (new_disk_conf
->al_extents
< DRBD_AL_EXTENTS_MIN
)
1537 new_disk_conf
->al_extents
= DRBD_AL_EXTENTS_MIN
;
1538 if (new_disk_conf
->al_extents
> drbd_al_extents_max(nbc
))
1539 new_disk_conf
->al_extents
= drbd_al_extents_max(nbc
);
1541 if (drbd_get_max_capacity(nbc
) < new_disk_conf
->disk_size
) {
1542 drbd_err(device
, "max capacity %llu smaller than disk size %llu\n",
1543 (unsigned long long) drbd_get_max_capacity(nbc
),
1544 (unsigned long long) new_disk_conf
->disk_size
);
1545 retcode
= ERR_DISK_TOO_SMALL
;
1549 if (new_disk_conf
->meta_dev_idx
< 0) {
1550 max_possible_sectors
= DRBD_MAX_SECTORS_FLEX
;
1551 /* at least one MB, otherwise it does not make sense */
1552 min_md_device_sectors
= (2<<10);
1554 max_possible_sectors
= DRBD_MAX_SECTORS
;
1555 min_md_device_sectors
= MD_128MB_SECT
* (new_disk_conf
->meta_dev_idx
+ 1);
1558 if (drbd_get_capacity(nbc
->md_bdev
) < min_md_device_sectors
) {
1559 retcode
= ERR_MD_DISK_TOO_SMALL
;
1560 drbd_warn(device
, "refusing attach: md-device too small, "
1561 "at least %llu sectors needed for this meta-disk type\n",
1562 (unsigned long long) min_md_device_sectors
);
1566 /* Make sure the new disk is big enough
1567 * (we may currently be R_PRIMARY with no local disk...) */
1568 if (drbd_get_max_capacity(nbc
) <
1569 drbd_get_capacity(device
->this_bdev
)) {
1570 retcode
= ERR_DISK_TOO_SMALL
;
1574 nbc
->known_size
= drbd_get_capacity(nbc
->backing_bdev
);
1576 if (nbc
->known_size
> max_possible_sectors
) {
1577 drbd_warn(device
, "==> truncating very big lower level device "
1578 "to currently maximum possible %llu sectors <==\n",
1579 (unsigned long long) max_possible_sectors
);
1580 if (new_disk_conf
->meta_dev_idx
>= 0)
1581 drbd_warn(device
, "==>> using internal or flexible "
1582 "meta data may help <<==\n");
1585 drbd_suspend_io(device
);
1586 /* also wait for the last barrier ack. */
1587 /* FIXME see also https://daiquiri.linbit/cgi-bin/bugzilla/show_bug.cgi?id=171
1588 * We need a way to either ignore barrier acks for barriers sent before a device
1589 * was attached, or a way to wait for all pending barrier acks to come in.
1590 * As barriers are counted per resource,
1591 * we'd need to suspend io on all devices of a resource.
1593 wait_event(device
->misc_wait
, !atomic_read(&device
->ap_pending_cnt
) || drbd_suspended(device
));
1594 /* and for any other previously queued work */
1595 drbd_flush_workqueue(device
);
1597 rv
= _drbd_request_state(device
, NS(disk
, D_ATTACHING
), CS_VERBOSE
);
1598 retcode
= rv
; /* FIXME: Type mismatch. */
1599 drbd_resume_io(device
);
1600 if (rv
< SS_SUCCESS
)
1603 if (!get_ldev_if_state(device
, D_ATTACHING
))
1604 goto force_diskless
;
1606 if (!device
->bitmap
) {
1607 if (drbd_bm_init(device
)) {
1608 retcode
= ERR_NOMEM
;
1609 goto force_diskless_dec
;
1613 if (device
->state
.conn
< C_CONNECTED
&&
1614 device
->state
.role
== R_PRIMARY
&&
1615 (device
->ed_uuid
& ~((u64
)1)) != (nbc
->md
.uuid
[UI_CURRENT
] & ~((u64
)1))) {
1616 drbd_err(device
, "Can only attach to data with current UUID=%016llX\n",
1617 (unsigned long long)device
->ed_uuid
);
1618 retcode
= ERR_DATA_NOT_CURRENT
;
1619 goto force_diskless_dec
;
1622 /* Since we are diskless, fix the activity log first... */
1623 if (drbd_check_al_size(device
, new_disk_conf
)) {
1624 retcode
= ERR_NOMEM
;
1625 goto force_diskless_dec
;
1628 /* Prevent shrinking of consistent devices ! */
1629 if (drbd_md_test_flag(nbc
, MDF_CONSISTENT
) &&
1630 drbd_new_dev_size(device
, nbc
, nbc
->disk_conf
->disk_size
, 0) < nbc
->md
.la_size_sect
) {
1631 drbd_warn(device
, "refusing to truncate a consistent device\n");
1632 retcode
= ERR_DISK_TOO_SMALL
;
1633 goto force_diskless_dec
;
1636 /* Reset the "barriers don't work" bits here, then force meta data to
1637 * be written, to ensure we determine if barriers are supported. */
1638 if (new_disk_conf
->md_flushes
)
1639 clear_bit(MD_NO_FUA
, &device
->flags
);
1641 set_bit(MD_NO_FUA
, &device
->flags
);
1643 /* Point of no return reached.
1644 * Devices and memory are no longer released by error cleanup below.
1645 * now device takes over responsibility, and the state engine should
1646 * clean it up somewhere. */
1647 D_ASSERT(device
, device
->ldev
== NULL
);
1649 device
->resync
= resync_lru
;
1650 device
->rs_plan_s
= new_plan
;
1653 new_disk_conf
= NULL
;
1656 drbd_bump_write_ordering(first_peer_device(device
)->connection
, WO_bdev_flush
);
1658 if (drbd_md_test_flag(device
->ldev
, MDF_CRASHED_PRIMARY
))
1659 set_bit(CRASHED_PRIMARY
, &device
->flags
);
1661 clear_bit(CRASHED_PRIMARY
, &device
->flags
);
1663 if (drbd_md_test_flag(device
->ldev
, MDF_PRIMARY_IND
) &&
1664 !(device
->state
.role
== R_PRIMARY
&&
1665 first_peer_device(device
)->connection
->susp_nod
))
1666 set_bit(CRASHED_PRIMARY
, &device
->flags
);
1668 device
->send_cnt
= 0;
1669 device
->recv_cnt
= 0;
1670 device
->read_cnt
= 0;
1671 device
->writ_cnt
= 0;
1673 drbd_reconsider_max_bio_size(device
);
1675 /* If I am currently not R_PRIMARY,
1676 * but meta data primary indicator is set,
1677 * I just now recover from a hard crash,
1678 * and have been R_PRIMARY before that crash.
1680 * Now, if I had no connection before that crash
1681 * (have been degraded R_PRIMARY), chances are that
1682 * I won't find my peer now either.
1684 * In that case, and _only_ in that case,
1685 * we use the degr-wfc-timeout instead of the default,
1686 * so we can automatically recover from a crash of a
1687 * degraded but active "cluster" after a certain timeout.
1689 clear_bit(USE_DEGR_WFC_T
, &device
->flags
);
1690 if (device
->state
.role
!= R_PRIMARY
&&
1691 drbd_md_test_flag(device
->ldev
, MDF_PRIMARY_IND
) &&
1692 !drbd_md_test_flag(device
->ldev
, MDF_CONNECTED_IND
))
1693 set_bit(USE_DEGR_WFC_T
, &device
->flags
);
1695 dd
= drbd_determine_dev_size(device
, 0, NULL
);
1696 if (dd
<= DS_ERROR
) {
1697 retcode
= ERR_NOMEM_BITMAP
;
1698 goto force_diskless_dec
;
1699 } else if (dd
== DS_GREW
)
1700 set_bit(RESYNC_AFTER_NEG
, &device
->flags
);
1702 if (drbd_md_test_flag(device
->ldev
, MDF_FULL_SYNC
) ||
1703 (test_bit(CRASHED_PRIMARY
, &device
->flags
) &&
1704 drbd_md_test_flag(device
->ldev
, MDF_AL_DISABLED
))) {
1705 drbd_info(device
, "Assuming that all blocks are out of sync "
1706 "(aka FullSync)\n");
1707 if (drbd_bitmap_io(device
, &drbd_bmio_set_n_write
,
1708 "set_n_write from attaching", BM_LOCKED_MASK
)) {
1709 retcode
= ERR_IO_MD_DISK
;
1710 goto force_diskless_dec
;
1713 if (drbd_bitmap_io(device
, &drbd_bm_read
,
1714 "read from attaching", BM_LOCKED_MASK
)) {
1715 retcode
= ERR_IO_MD_DISK
;
1716 goto force_diskless_dec
;
1720 if (_drbd_bm_total_weight(device
) == drbd_bm_bits(device
))
1721 drbd_suspend_al(device
); /* IO is still suspended here... */
1723 spin_lock_irq(&first_peer_device(device
)->connection
->req_lock
);
1724 os
= drbd_read_state(device
);
1726 /* If MDF_CONSISTENT is not set go into inconsistent state,
1727 otherwise investigate MDF_WasUpToDate...
1728 If MDF_WAS_UP_TO_DATE is not set go into D_OUTDATED disk state,
1729 otherwise into D_CONSISTENT state.
1731 if (drbd_md_test_flag(device
->ldev
, MDF_CONSISTENT
)) {
1732 if (drbd_md_test_flag(device
->ldev
, MDF_WAS_UP_TO_DATE
))
1733 ns
.disk
= D_CONSISTENT
;
1735 ns
.disk
= D_OUTDATED
;
1737 ns
.disk
= D_INCONSISTENT
;
1740 if (drbd_md_test_flag(device
->ldev
, MDF_PEER_OUT_DATED
))
1741 ns
.pdsk
= D_OUTDATED
;
1744 if (ns
.disk
== D_CONSISTENT
&&
1745 (ns
.pdsk
== D_OUTDATED
|| rcu_dereference(device
->ldev
->disk_conf
)->fencing
== FP_DONT_CARE
))
1746 ns
.disk
= D_UP_TO_DATE
;
1748 /* All tests on MDF_PRIMARY_IND, MDF_CONNECTED_IND,
1749 MDF_CONSISTENT and MDF_WAS_UP_TO_DATE must happen before
1750 this point, because drbd_request_state() modifies these
1753 if (rcu_dereference(device
->ldev
->disk_conf
)->al_updates
)
1754 device
->ldev
->md
.flags
&= ~MDF_AL_DISABLED
;
1756 device
->ldev
->md
.flags
|= MDF_AL_DISABLED
;
1760 /* In case we are C_CONNECTED postpone any decision on the new disk
1761 state after the negotiation phase. */
1762 if (device
->state
.conn
== C_CONNECTED
) {
1763 device
->new_state_tmp
.i
= ns
.i
;
1765 ns
.disk
= D_NEGOTIATING
;
1767 /* We expect to receive up-to-date UUIDs soon.
1768 To avoid a race in receive_state, free p_uuid while
1769 holding req_lock. I.e. atomic with the state change */
1770 kfree(device
->p_uuid
);
1771 device
->p_uuid
= NULL
;
1774 rv
= _drbd_set_state(device
, ns
, CS_VERBOSE
, NULL
);
1775 spin_unlock_irq(&first_peer_device(device
)->connection
->req_lock
);
1777 if (rv
< SS_SUCCESS
)
1778 goto force_diskless_dec
;
1780 mod_timer(&device
->request_timer
, jiffies
+ HZ
);
1782 if (device
->state
.role
== R_PRIMARY
)
1783 device
->ldev
->md
.uuid
[UI_CURRENT
] |= (u64
)1;
1785 device
->ldev
->md
.uuid
[UI_CURRENT
] &= ~(u64
)1;
1787 drbd_md_mark_dirty(device
);
1788 drbd_md_sync(device
);
1790 kobject_uevent(&disk_to_dev(device
->vdisk
)->kobj
, KOBJ_CHANGE
);
1792 conn_reconfig_done(first_peer_device(device
)->connection
);
1793 drbd_adm_finish(info
, retcode
);
1799 drbd_force_state(device
, NS(disk
, D_DISKLESS
));
1800 drbd_md_sync(device
);
1802 conn_reconfig_done(first_peer_device(device
)->connection
);
1804 if (nbc
->backing_bdev
)
1805 blkdev_put(nbc
->backing_bdev
,
1806 FMODE_READ
| FMODE_WRITE
| FMODE_EXCL
);
1808 blkdev_put(nbc
->md_bdev
,
1809 FMODE_READ
| FMODE_WRITE
| FMODE_EXCL
);
1812 kfree(new_disk_conf
);
1813 lc_destroy(resync_lru
);
1817 drbd_adm_finish(info
, retcode
);
1821 static int adm_detach(struct drbd_device
*device
, int force
)
1823 enum drbd_state_rv retcode
;
1827 set_bit(FORCE_DETACH
, &device
->flags
);
1828 drbd_force_state(device
, NS(disk
, D_FAILED
));
1829 retcode
= SS_SUCCESS
;
1833 drbd_suspend_io(device
); /* so no-one is stuck in drbd_al_begin_io */
1834 drbd_md_get_buffer(device
); /* make sure there is no in-flight meta-data IO */
1835 retcode
= drbd_request_state(device
, NS(disk
, D_FAILED
));
1836 drbd_md_put_buffer(device
);
1837 /* D_FAILED will transition to DISKLESS. */
1838 ret
= wait_event_interruptible(device
->misc_wait
,
1839 device
->state
.disk
!= D_FAILED
);
1840 drbd_resume_io(device
);
1841 if ((int)retcode
== (int)SS_IS_DISKLESS
)
1842 retcode
= SS_NOTHING_TO_DO
;
1849 /* Detaching the disk is a process in multiple stages. First we need to lock
1850 * out application IO, in-flight IO, IO stuck in drbd_al_begin_io.
1851 * Then we transition to D_DISKLESS, and wait for put_ldev() to return all
1852 * internal references as well.
1853 * Only then we have finally detached. */
1854 int drbd_adm_detach(struct sk_buff
*skb
, struct genl_info
*info
)
1856 enum drbd_ret_code retcode
;
1857 struct detach_parms parms
= { };
1860 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_MINOR
);
1861 if (!adm_ctx
.reply_skb
)
1863 if (retcode
!= NO_ERROR
)
1866 if (info
->attrs
[DRBD_NLA_DETACH_PARMS
]) {
1867 err
= detach_parms_from_attrs(&parms
, info
);
1869 retcode
= ERR_MANDATORY_TAG
;
1870 drbd_msg_put_info(from_attrs_err_to_txt(err
));
1875 retcode
= adm_detach(adm_ctx
.device
, parms
.force_detach
);
1877 drbd_adm_finish(info
, retcode
);
1881 static bool conn_resync_running(struct drbd_connection
*connection
)
1883 struct drbd_peer_device
*peer_device
;
1888 idr_for_each_entry(&connection
->peer_devices
, peer_device
, vnr
) {
1889 struct drbd_device
*device
= peer_device
->device
;
1890 if (device
->state
.conn
== C_SYNC_SOURCE
||
1891 device
->state
.conn
== C_SYNC_TARGET
||
1892 device
->state
.conn
== C_PAUSED_SYNC_S
||
1893 device
->state
.conn
== C_PAUSED_SYNC_T
) {
1903 static bool conn_ov_running(struct drbd_connection
*connection
)
1905 struct drbd_peer_device
*peer_device
;
1910 idr_for_each_entry(&connection
->peer_devices
, peer_device
, vnr
) {
1911 struct drbd_device
*device
= peer_device
->device
;
1912 if (device
->state
.conn
== C_VERIFY_S
||
1913 device
->state
.conn
== C_VERIFY_T
) {
1923 static enum drbd_ret_code
1924 _check_net_options(struct drbd_connection
*connection
, struct net_conf
*old_net_conf
, struct net_conf
*new_net_conf
)
1926 struct drbd_peer_device
*peer_device
;
1929 if (old_net_conf
&& connection
->cstate
== C_WF_REPORT_PARAMS
&& connection
->agreed_pro_version
< 100) {
1930 if (new_net_conf
->wire_protocol
!= old_net_conf
->wire_protocol
)
1931 return ERR_NEED_APV_100
;
1933 if (new_net_conf
->two_primaries
!= old_net_conf
->two_primaries
)
1934 return ERR_NEED_APV_100
;
1936 if (strcmp(new_net_conf
->integrity_alg
, old_net_conf
->integrity_alg
))
1937 return ERR_NEED_APV_100
;
1940 if (!new_net_conf
->two_primaries
&&
1941 conn_highest_role(connection
) == R_PRIMARY
&&
1942 conn_highest_peer(connection
) == R_PRIMARY
)
1943 return ERR_NEED_ALLOW_TWO_PRI
;
1945 if (new_net_conf
->two_primaries
&&
1946 (new_net_conf
->wire_protocol
!= DRBD_PROT_C
))
1947 return ERR_NOT_PROTO_C
;
1949 idr_for_each_entry(&connection
->peer_devices
, peer_device
, i
) {
1950 struct drbd_device
*device
= peer_device
->device
;
1951 if (get_ldev(device
)) {
1952 enum drbd_fencing_p fp
= rcu_dereference(device
->ldev
->disk_conf
)->fencing
;
1954 if (new_net_conf
->wire_protocol
== DRBD_PROT_A
&& fp
== FP_STONITH
)
1955 return ERR_STONITH_AND_PROT_A
;
1957 if (device
->state
.role
== R_PRIMARY
&& new_net_conf
->discard_my_data
)
1958 return ERR_DISCARD_IMPOSSIBLE
;
1961 if (new_net_conf
->on_congestion
!= OC_BLOCK
&& new_net_conf
->wire_protocol
!= DRBD_PROT_A
)
1962 return ERR_CONG_NOT_PROTO_A
;
1967 static enum drbd_ret_code
1968 check_net_options(struct drbd_connection
*connection
, struct net_conf
*new_net_conf
)
1970 static enum drbd_ret_code rv
;
1971 struct drbd_peer_device
*peer_device
;
1975 rv
= _check_net_options(connection
, rcu_dereference(connection
->net_conf
), new_net_conf
);
1978 /* connection->volumes protected by genl_lock() here */
1979 idr_for_each_entry(&connection
->peer_devices
, peer_device
, i
) {
1980 struct drbd_device
*device
= peer_device
->device
;
1981 if (!device
->bitmap
) {
1982 if (drbd_bm_init(device
))
1991 struct crypto_hash
*verify_tfm
;
1992 struct crypto_hash
*csums_tfm
;
1993 struct crypto_hash
*cram_hmac_tfm
;
1994 struct crypto_hash
*integrity_tfm
;
1998 alloc_hash(struct crypto_hash
**tfm
, char *tfm_name
, int err_alg
)
2003 *tfm
= crypto_alloc_hash(tfm_name
, 0, CRYPTO_ALG_ASYNC
);
2012 static enum drbd_ret_code
2013 alloc_crypto(struct crypto
*crypto
, struct net_conf
*new_net_conf
)
2015 char hmac_name
[CRYPTO_MAX_ALG_NAME
];
2016 enum drbd_ret_code rv
;
2018 rv
= alloc_hash(&crypto
->csums_tfm
, new_net_conf
->csums_alg
,
2022 rv
= alloc_hash(&crypto
->verify_tfm
, new_net_conf
->verify_alg
,
2026 rv
= alloc_hash(&crypto
->integrity_tfm
, new_net_conf
->integrity_alg
,
2030 if (new_net_conf
->cram_hmac_alg
[0] != 0) {
2031 snprintf(hmac_name
, CRYPTO_MAX_ALG_NAME
, "hmac(%s)",
2032 new_net_conf
->cram_hmac_alg
);
2034 rv
= alloc_hash(&crypto
->cram_hmac_tfm
, hmac_name
,
2041 static void free_crypto(struct crypto
*crypto
)
2043 crypto_free_hash(crypto
->cram_hmac_tfm
);
2044 crypto_free_hash(crypto
->integrity_tfm
);
2045 crypto_free_hash(crypto
->csums_tfm
);
2046 crypto_free_hash(crypto
->verify_tfm
);
2049 int drbd_adm_net_opts(struct sk_buff
*skb
, struct genl_info
*info
)
2051 enum drbd_ret_code retcode
;
2052 struct drbd_connection
*connection
;
2053 struct net_conf
*old_net_conf
, *new_net_conf
= NULL
;
2055 int ovr
; /* online verify running */
2056 int rsr
; /* re-sync running */
2057 struct crypto crypto
= { };
2059 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_CONNECTION
);
2060 if (!adm_ctx
.reply_skb
)
2062 if (retcode
!= NO_ERROR
)
2065 connection
= adm_ctx
.connection
;
2067 new_net_conf
= kzalloc(sizeof(struct net_conf
), GFP_KERNEL
);
2068 if (!new_net_conf
) {
2069 retcode
= ERR_NOMEM
;
2073 conn_reconfig_start(connection
);
2075 mutex_lock(&connection
->data
.mutex
);
2076 mutex_lock(&connection
->conf_update
);
2077 old_net_conf
= connection
->net_conf
;
2079 if (!old_net_conf
) {
2080 drbd_msg_put_info("net conf missing, try connect");
2081 retcode
= ERR_INVALID_REQUEST
;
2085 *new_net_conf
= *old_net_conf
;
2086 if (should_set_defaults(info
))
2087 set_net_conf_defaults(new_net_conf
);
2089 err
= net_conf_from_attrs_for_change(new_net_conf
, info
);
2090 if (err
&& err
!= -ENOMSG
) {
2091 retcode
= ERR_MANDATORY_TAG
;
2092 drbd_msg_put_info(from_attrs_err_to_txt(err
));
2096 retcode
= check_net_options(connection
, new_net_conf
);
2097 if (retcode
!= NO_ERROR
)
2100 /* re-sync running */
2101 rsr
= conn_resync_running(connection
);
2102 if (rsr
&& strcmp(new_net_conf
->csums_alg
, old_net_conf
->csums_alg
)) {
2103 retcode
= ERR_CSUMS_RESYNC_RUNNING
;
2107 /* online verify running */
2108 ovr
= conn_ov_running(connection
);
2109 if (ovr
&& strcmp(new_net_conf
->verify_alg
, old_net_conf
->verify_alg
)) {
2110 retcode
= ERR_VERIFY_RUNNING
;
2114 retcode
= alloc_crypto(&crypto
, new_net_conf
);
2115 if (retcode
!= NO_ERROR
)
2118 rcu_assign_pointer(connection
->net_conf
, new_net_conf
);
2121 crypto_free_hash(connection
->csums_tfm
);
2122 connection
->csums_tfm
= crypto
.csums_tfm
;
2123 crypto
.csums_tfm
= NULL
;
2126 crypto_free_hash(connection
->verify_tfm
);
2127 connection
->verify_tfm
= crypto
.verify_tfm
;
2128 crypto
.verify_tfm
= NULL
;
2131 crypto_free_hash(connection
->integrity_tfm
);
2132 connection
->integrity_tfm
= crypto
.integrity_tfm
;
2133 if (connection
->cstate
>= C_WF_REPORT_PARAMS
&& connection
->agreed_pro_version
>= 100)
2134 /* Do this without trying to take connection->data.mutex again. */
2135 __drbd_send_protocol(connection
, P_PROTOCOL_UPDATE
);
2137 crypto_free_hash(connection
->cram_hmac_tfm
);
2138 connection
->cram_hmac_tfm
= crypto
.cram_hmac_tfm
;
2140 mutex_unlock(&connection
->conf_update
);
2141 mutex_unlock(&connection
->data
.mutex
);
2143 kfree(old_net_conf
);
2145 if (connection
->cstate
>= C_WF_REPORT_PARAMS
)
2146 drbd_send_sync_param(minor_to_device(conn_lowest_minor(connection
)));
2151 mutex_unlock(&connection
->conf_update
);
2152 mutex_unlock(&connection
->data
.mutex
);
2153 free_crypto(&crypto
);
2154 kfree(new_net_conf
);
2156 conn_reconfig_done(connection
);
2158 drbd_adm_finish(info
, retcode
);
2162 int drbd_adm_connect(struct sk_buff
*skb
, struct genl_info
*info
)
2164 struct drbd_peer_device
*peer_device
;
2165 struct net_conf
*old_net_conf
, *new_net_conf
= NULL
;
2166 struct crypto crypto
= { };
2167 struct drbd_resource
*resource
;
2168 struct drbd_connection
*connection
;
2169 enum drbd_ret_code retcode
;
2173 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_RESOURCE
);
2175 if (!adm_ctx
.reply_skb
)
2177 if (retcode
!= NO_ERROR
)
2179 if (!(adm_ctx
.my_addr
&& adm_ctx
.peer_addr
)) {
2180 drbd_msg_put_info("connection endpoint(s) missing");
2181 retcode
= ERR_INVALID_REQUEST
;
2185 /* No need for _rcu here. All reconfiguration is
2186 * strictly serialized on genl_lock(). We are protected against
2187 * concurrent reconfiguration/addition/deletion */
2188 for_each_resource(resource
, &drbd_resources
) {
2189 for_each_connection(connection
, resource
) {
2190 if (nla_len(adm_ctx
.my_addr
) == connection
->my_addr_len
&&
2191 !memcmp(nla_data(adm_ctx
.my_addr
), &connection
->my_addr
,
2192 connection
->my_addr_len
)) {
2193 retcode
= ERR_LOCAL_ADDR
;
2197 if (nla_len(adm_ctx
.peer_addr
) == connection
->peer_addr_len
&&
2198 !memcmp(nla_data(adm_ctx
.peer_addr
), &connection
->peer_addr
,
2199 connection
->peer_addr_len
)) {
2200 retcode
= ERR_PEER_ADDR
;
2206 connection
= adm_ctx
.connection
;
2207 conn_reconfig_start(connection
);
2209 if (connection
->cstate
> C_STANDALONE
) {
2210 retcode
= ERR_NET_CONFIGURED
;
2214 /* allocation not in the IO path, drbdsetup / netlink process context */
2215 new_net_conf
= kzalloc(sizeof(*new_net_conf
), GFP_KERNEL
);
2216 if (!new_net_conf
) {
2217 retcode
= ERR_NOMEM
;
2221 set_net_conf_defaults(new_net_conf
);
2223 err
= net_conf_from_attrs(new_net_conf
, info
);
2224 if (err
&& err
!= -ENOMSG
) {
2225 retcode
= ERR_MANDATORY_TAG
;
2226 drbd_msg_put_info(from_attrs_err_to_txt(err
));
2230 retcode
= check_net_options(connection
, new_net_conf
);
2231 if (retcode
!= NO_ERROR
)
2234 retcode
= alloc_crypto(&crypto
, new_net_conf
);
2235 if (retcode
!= NO_ERROR
)
2238 ((char *)new_net_conf
->shared_secret
)[SHARED_SECRET_MAX
-1] = 0;
2240 conn_flush_workqueue(connection
);
2242 mutex_lock(&connection
->conf_update
);
2243 old_net_conf
= connection
->net_conf
;
2245 retcode
= ERR_NET_CONFIGURED
;
2246 mutex_unlock(&connection
->conf_update
);
2249 rcu_assign_pointer(connection
->net_conf
, new_net_conf
);
2251 conn_free_crypto(connection
);
2252 connection
->cram_hmac_tfm
= crypto
.cram_hmac_tfm
;
2253 connection
->integrity_tfm
= crypto
.integrity_tfm
;
2254 connection
->csums_tfm
= crypto
.csums_tfm
;
2255 connection
->verify_tfm
= crypto
.verify_tfm
;
2257 connection
->my_addr_len
= nla_len(adm_ctx
.my_addr
);
2258 memcpy(&connection
->my_addr
, nla_data(adm_ctx
.my_addr
), connection
->my_addr_len
);
2259 connection
->peer_addr_len
= nla_len(adm_ctx
.peer_addr
);
2260 memcpy(&connection
->peer_addr
, nla_data(adm_ctx
.peer_addr
), connection
->peer_addr_len
);
2262 mutex_unlock(&connection
->conf_update
);
2265 idr_for_each_entry(&connection
->peer_devices
, peer_device
, i
) {
2266 struct drbd_device
*device
= peer_device
->device
;
2267 device
->send_cnt
= 0;
2268 device
->recv_cnt
= 0;
2272 retcode
= conn_request_state(connection
, NS(conn
, C_UNCONNECTED
), CS_VERBOSE
);
2274 conn_reconfig_done(connection
);
2275 drbd_adm_finish(info
, retcode
);
2279 free_crypto(&crypto
);
2280 kfree(new_net_conf
);
2282 conn_reconfig_done(connection
);
2284 drbd_adm_finish(info
, retcode
);
2288 static enum drbd_state_rv
conn_try_disconnect(struct drbd_connection
*connection
, bool force
)
2290 enum drbd_state_rv rv
;
2292 rv
= conn_request_state(connection
, NS(conn
, C_DISCONNECTING
),
2293 force
? CS_HARD
: 0);
2296 case SS_NOTHING_TO_DO
:
2298 case SS_ALREADY_STANDALONE
:
2300 case SS_PRIMARY_NOP
:
2301 /* Our state checking code wants to see the peer outdated. */
2302 rv
= conn_request_state(connection
, NS2(conn
, C_DISCONNECTING
, pdsk
, D_OUTDATED
), 0);
2304 if (rv
== SS_OUTDATE_WO_CONN
) /* lost connection before graceful disconnect succeeded */
2305 rv
= conn_request_state(connection
, NS(conn
, C_DISCONNECTING
), CS_VERBOSE
);
2308 case SS_CW_FAILED_BY_PEER
:
2309 /* The peer probably wants to see us outdated. */
2310 rv
= conn_request_state(connection
, NS2(conn
, C_DISCONNECTING
,
2311 disk
, D_OUTDATED
), 0);
2312 if (rv
== SS_IS_DISKLESS
|| rv
== SS_LOWER_THAN_OUTDATED
) {
2313 rv
= conn_request_state(connection
, NS(conn
, C_DISCONNECTING
),
2318 /* no special handling necessary */
2321 if (rv
>= SS_SUCCESS
) {
2322 enum drbd_state_rv rv2
;
2323 /* No one else can reconfigure the network while I am here.
2324 * The state handling only uses drbd_thread_stop_nowait(),
2325 * we want to really wait here until the receiver is no more.
2327 drbd_thread_stop(&connection
->receiver
);
2329 /* Race breaker. This additional state change request may be
2330 * necessary, if this was a forced disconnect during a receiver
2331 * restart. We may have "killed" the receiver thread just
2332 * after drbdd_init() returned. Typically, we should be
2333 * C_STANDALONE already, now, and this becomes a no-op.
2335 rv2
= conn_request_state(connection
, NS(conn
, C_STANDALONE
),
2336 CS_VERBOSE
| CS_HARD
);
2337 if (rv2
< SS_SUCCESS
)
2338 drbd_err(connection
,
2339 "unexpected rv2=%d in conn_try_disconnect()\n",
2345 int drbd_adm_disconnect(struct sk_buff
*skb
, struct genl_info
*info
)
2347 struct disconnect_parms parms
;
2348 struct drbd_connection
*connection
;
2349 enum drbd_state_rv rv
;
2350 enum drbd_ret_code retcode
;
2353 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_CONNECTION
);
2354 if (!adm_ctx
.reply_skb
)
2356 if (retcode
!= NO_ERROR
)
2359 connection
= adm_ctx
.connection
;
2360 memset(&parms
, 0, sizeof(parms
));
2361 if (info
->attrs
[DRBD_NLA_DISCONNECT_PARMS
]) {
2362 err
= disconnect_parms_from_attrs(&parms
, info
);
2364 retcode
= ERR_MANDATORY_TAG
;
2365 drbd_msg_put_info(from_attrs_err_to_txt(err
));
2370 rv
= conn_try_disconnect(connection
, parms
.force_disconnect
);
2371 if (rv
< SS_SUCCESS
)
2372 retcode
= rv
; /* FIXME: Type mismatch. */
2376 drbd_adm_finish(info
, retcode
);
2380 void resync_after_online_grow(struct drbd_device
*device
)
2382 int iass
; /* I am sync source */
2384 drbd_info(device
, "Resync of new storage after online grow\n");
2385 if (device
->state
.role
!= device
->state
.peer
)
2386 iass
= (device
->state
.role
== R_PRIMARY
);
2388 iass
= test_bit(RESOLVE_CONFLICTS
, &first_peer_device(device
)->connection
->flags
);
2391 drbd_start_resync(device
, C_SYNC_SOURCE
);
2393 _drbd_request_state(device
, NS(conn
, C_WF_SYNC_UUID
), CS_VERBOSE
+ CS_SERIALIZE
);
2396 int drbd_adm_resize(struct sk_buff
*skb
, struct genl_info
*info
)
2398 struct disk_conf
*old_disk_conf
, *new_disk_conf
= NULL
;
2399 struct resize_parms rs
;
2400 struct drbd_device
*device
;
2401 enum drbd_ret_code retcode
;
2402 enum determine_dev_size dd
;
2403 bool change_al_layout
= false;
2404 enum dds_flags ddsf
;
2408 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_MINOR
);
2409 if (!adm_ctx
.reply_skb
)
2411 if (retcode
!= NO_ERROR
)
2414 device
= adm_ctx
.device
;
2415 if (!get_ldev(device
)) {
2416 retcode
= ERR_NO_DISK
;
2420 memset(&rs
, 0, sizeof(struct resize_parms
));
2421 rs
.al_stripes
= device
->ldev
->md
.al_stripes
;
2422 rs
.al_stripe_size
= device
->ldev
->md
.al_stripe_size_4k
* 4;
2423 if (info
->attrs
[DRBD_NLA_RESIZE_PARMS
]) {
2424 err
= resize_parms_from_attrs(&rs
, info
);
2426 retcode
= ERR_MANDATORY_TAG
;
2427 drbd_msg_put_info(from_attrs_err_to_txt(err
));
2432 if (device
->state
.conn
> C_CONNECTED
) {
2433 retcode
= ERR_RESIZE_RESYNC
;
2437 if (device
->state
.role
== R_SECONDARY
&&
2438 device
->state
.peer
== R_SECONDARY
) {
2439 retcode
= ERR_NO_PRIMARY
;
2443 if (rs
.no_resync
&& first_peer_device(device
)->connection
->agreed_pro_version
< 93) {
2444 retcode
= ERR_NEED_APV_93
;
2449 u_size
= rcu_dereference(device
->ldev
->disk_conf
)->disk_size
;
2451 if (u_size
!= (sector_t
)rs
.resize_size
) {
2452 new_disk_conf
= kmalloc(sizeof(struct disk_conf
), GFP_KERNEL
);
2453 if (!new_disk_conf
) {
2454 retcode
= ERR_NOMEM
;
2459 if (device
->ldev
->md
.al_stripes
!= rs
.al_stripes
||
2460 device
->ldev
->md
.al_stripe_size_4k
!= rs
.al_stripe_size
/ 4) {
2461 u32 al_size_k
= rs
.al_stripes
* rs
.al_stripe_size
;
2463 if (al_size_k
> (16 * 1024 * 1024)) {
2464 retcode
= ERR_MD_LAYOUT_TOO_BIG
;
2468 if (al_size_k
< MD_32kB_SECT
/2) {
2469 retcode
= ERR_MD_LAYOUT_TOO_SMALL
;
2473 if (device
->state
.conn
!= C_CONNECTED
) {
2474 retcode
= ERR_MD_LAYOUT_CONNECTED
;
2478 change_al_layout
= true;
2481 if (device
->ldev
->known_size
!= drbd_get_capacity(device
->ldev
->backing_bdev
))
2482 device
->ldev
->known_size
= drbd_get_capacity(device
->ldev
->backing_bdev
);
2484 if (new_disk_conf
) {
2485 mutex_lock(&first_peer_device(device
)->connection
->conf_update
);
2486 old_disk_conf
= device
->ldev
->disk_conf
;
2487 *new_disk_conf
= *old_disk_conf
;
2488 new_disk_conf
->disk_size
= (sector_t
)rs
.resize_size
;
2489 rcu_assign_pointer(device
->ldev
->disk_conf
, new_disk_conf
);
2490 mutex_unlock(&first_peer_device(device
)->connection
->conf_update
);
2492 kfree(old_disk_conf
);
2495 ddsf
= (rs
.resize_force
? DDSF_FORCED
: 0) | (rs
.no_resync
? DDSF_NO_RESYNC
: 0);
2496 dd
= drbd_determine_dev_size(device
, ddsf
, change_al_layout
? &rs
: NULL
);
2497 drbd_md_sync(device
);
2499 if (dd
== DS_ERROR
) {
2500 retcode
= ERR_NOMEM_BITMAP
;
2502 } else if (dd
== DS_ERROR_SPACE_MD
) {
2503 retcode
= ERR_MD_LAYOUT_NO_FIT
;
2505 } else if (dd
== DS_ERROR_SHRINK
) {
2506 retcode
= ERR_IMPLICIT_SHRINK
;
2510 if (device
->state
.conn
== C_CONNECTED
) {
2512 set_bit(RESIZE_PENDING
, &device
->flags
);
2514 drbd_send_uuids(device
);
2515 drbd_send_sizes(device
, 1, ddsf
);
2519 drbd_adm_finish(info
, retcode
);
2527 int drbd_adm_resource_opts(struct sk_buff
*skb
, struct genl_info
*info
)
2529 enum drbd_ret_code retcode
;
2530 struct res_opts res_opts
;
2533 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_RESOURCE
);
2534 if (!adm_ctx
.reply_skb
)
2536 if (retcode
!= NO_ERROR
)
2539 res_opts
= adm_ctx
.resource
->res_opts
;
2540 if (should_set_defaults(info
))
2541 set_res_opts_defaults(&res_opts
);
2543 err
= res_opts_from_attrs(&res_opts
, info
);
2544 if (err
&& err
!= -ENOMSG
) {
2545 retcode
= ERR_MANDATORY_TAG
;
2546 drbd_msg_put_info(from_attrs_err_to_txt(err
));
2550 err
= set_resource_options(adm_ctx
.resource
, &res_opts
);
2552 retcode
= ERR_INVALID_REQUEST
;
2554 retcode
= ERR_NOMEM
;
2558 drbd_adm_finish(info
, retcode
);
2562 int drbd_adm_invalidate(struct sk_buff
*skb
, struct genl_info
*info
)
2564 struct drbd_device
*device
;
2565 int retcode
; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2567 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_MINOR
);
2568 if (!adm_ctx
.reply_skb
)
2570 if (retcode
!= NO_ERROR
)
2573 device
= adm_ctx
.device
;
2575 /* If there is still bitmap IO pending, probably because of a previous
2576 * resync just being finished, wait for it before requesting a new resync.
2577 * Also wait for it's after_state_ch(). */
2578 drbd_suspend_io(device
);
2579 wait_event(device
->misc_wait
, !test_bit(BITMAP_IO
, &device
->flags
));
2580 drbd_flush_workqueue(device
);
2582 /* If we happen to be C_STANDALONE R_SECONDARY, just change to
2583 * D_INCONSISTENT, and set all bits in the bitmap. Otherwise,
2584 * try to start a resync handshake as sync target for full sync.
2586 if (device
->state
.conn
== C_STANDALONE
&& device
->state
.role
== R_SECONDARY
) {
2587 retcode
= drbd_request_state(device
, NS(disk
, D_INCONSISTENT
));
2588 if (retcode
>= SS_SUCCESS
) {
2589 if (drbd_bitmap_io(device
, &drbd_bmio_set_n_write
,
2590 "set_n_write from invalidate", BM_LOCKED_MASK
))
2591 retcode
= ERR_IO_MD_DISK
;
2594 retcode
= drbd_request_state(device
, NS(conn
, C_STARTING_SYNC_T
));
2595 drbd_resume_io(device
);
2598 drbd_adm_finish(info
, retcode
);
2602 static int drbd_adm_simple_request_state(struct sk_buff
*skb
, struct genl_info
*info
,
2603 union drbd_state mask
, union drbd_state val
)
2605 enum drbd_ret_code retcode
;
2607 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_MINOR
);
2608 if (!adm_ctx
.reply_skb
)
2610 if (retcode
!= NO_ERROR
)
2613 retcode
= drbd_request_state(adm_ctx
.device
, mask
, val
);
2615 drbd_adm_finish(info
, retcode
);
2619 static int drbd_bmio_set_susp_al(struct drbd_device
*device
)
2623 rv
= drbd_bmio_set_n_write(device
);
2624 drbd_suspend_al(device
);
2628 int drbd_adm_invalidate_peer(struct sk_buff
*skb
, struct genl_info
*info
)
2630 int retcode
; /* drbd_ret_code, drbd_state_rv */
2631 struct drbd_device
*device
;
2633 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_MINOR
);
2634 if (!adm_ctx
.reply_skb
)
2636 if (retcode
!= NO_ERROR
)
2639 device
= adm_ctx
.device
;
2641 /* If there is still bitmap IO pending, probably because of a previous
2642 * resync just being finished, wait for it before requesting a new resync.
2643 * Also wait for it's after_state_ch(). */
2644 drbd_suspend_io(device
);
2645 wait_event(device
->misc_wait
, !test_bit(BITMAP_IO
, &device
->flags
));
2646 drbd_flush_workqueue(device
);
2648 /* If we happen to be C_STANDALONE R_PRIMARY, just set all bits
2649 * in the bitmap. Otherwise, try to start a resync handshake
2650 * as sync source for full sync.
2652 if (device
->state
.conn
== C_STANDALONE
&& device
->state
.role
== R_PRIMARY
) {
2653 /* The peer will get a resync upon connect anyways. Just make that
2654 into a full resync. */
2655 retcode
= drbd_request_state(device
, NS(pdsk
, D_INCONSISTENT
));
2656 if (retcode
>= SS_SUCCESS
) {
2657 if (drbd_bitmap_io(device
, &drbd_bmio_set_susp_al
,
2658 "set_n_write from invalidate_peer",
2659 BM_LOCKED_SET_ALLOWED
))
2660 retcode
= ERR_IO_MD_DISK
;
2663 retcode
= drbd_request_state(device
, NS(conn
, C_STARTING_SYNC_S
));
2664 drbd_resume_io(device
);
2667 drbd_adm_finish(info
, retcode
);
2671 int drbd_adm_pause_sync(struct sk_buff
*skb
, struct genl_info
*info
)
2673 enum drbd_ret_code retcode
;
2675 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_MINOR
);
2676 if (!adm_ctx
.reply_skb
)
2678 if (retcode
!= NO_ERROR
)
2681 if (drbd_request_state(adm_ctx
.device
, NS(user_isp
, 1)) == SS_NOTHING_TO_DO
)
2682 retcode
= ERR_PAUSE_IS_SET
;
2684 drbd_adm_finish(info
, retcode
);
2688 int drbd_adm_resume_sync(struct sk_buff
*skb
, struct genl_info
*info
)
2690 union drbd_dev_state s
;
2691 enum drbd_ret_code retcode
;
2693 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_MINOR
);
2694 if (!adm_ctx
.reply_skb
)
2696 if (retcode
!= NO_ERROR
)
2699 if (drbd_request_state(adm_ctx
.device
, NS(user_isp
, 0)) == SS_NOTHING_TO_DO
) {
2700 s
= adm_ctx
.device
->state
;
2701 if (s
.conn
== C_PAUSED_SYNC_S
|| s
.conn
== C_PAUSED_SYNC_T
) {
2702 retcode
= s
.aftr_isp
? ERR_PIC_AFTER_DEP
:
2703 s
.peer_isp
? ERR_PIC_PEER_DEP
: ERR_PAUSE_IS_CLEAR
;
2705 retcode
= ERR_PAUSE_IS_CLEAR
;
2710 drbd_adm_finish(info
, retcode
);
2714 int drbd_adm_suspend_io(struct sk_buff
*skb
, struct genl_info
*info
)
2716 return drbd_adm_simple_request_state(skb
, info
, NS(susp
, 1));
2719 int drbd_adm_resume_io(struct sk_buff
*skb
, struct genl_info
*info
)
2721 struct drbd_device
*device
;
2722 int retcode
; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2724 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_MINOR
);
2725 if (!adm_ctx
.reply_skb
)
2727 if (retcode
!= NO_ERROR
)
2730 device
= adm_ctx
.device
;
2731 if (test_bit(NEW_CUR_UUID
, &device
->flags
)) {
2732 drbd_uuid_new_current(device
);
2733 clear_bit(NEW_CUR_UUID
, &device
->flags
);
2735 drbd_suspend_io(device
);
2736 retcode
= drbd_request_state(device
, NS3(susp
, 0, susp_nod
, 0, susp_fen
, 0));
2737 if (retcode
== SS_SUCCESS
) {
2738 if (device
->state
.conn
< C_CONNECTED
)
2739 tl_clear(first_peer_device(device
)->connection
);
2740 if (device
->state
.disk
== D_DISKLESS
|| device
->state
.disk
== D_FAILED
)
2741 tl_restart(first_peer_device(device
)->connection
, FAIL_FROZEN_DISK_IO
);
2743 drbd_resume_io(device
);
2746 drbd_adm_finish(info
, retcode
);
2750 int drbd_adm_outdate(struct sk_buff
*skb
, struct genl_info
*info
)
2752 return drbd_adm_simple_request_state(skb
, info
, NS(disk
, D_OUTDATED
));
2755 static int nla_put_drbd_cfg_context(struct sk_buff
*skb
,
2756 struct drbd_resource
*resource
,
2757 struct drbd_connection
*connection
,
2758 struct drbd_device
*device
)
2761 nla
= nla_nest_start(skb
, DRBD_NLA_CFG_CONTEXT
);
2763 goto nla_put_failure
;
2765 nla_put_u32(skb
, T_ctx_volume
, device
->vnr
))
2766 goto nla_put_failure
;
2767 if (nla_put_string(skb
, T_ctx_resource_name
, connection
->resource
->name
))
2768 goto nla_put_failure
;
2770 if (connection
->my_addr_len
&&
2771 nla_put(skb
, T_ctx_my_addr
, connection
->my_addr_len
, &connection
->my_addr
))
2772 goto nla_put_failure
;
2773 if (connection
->peer_addr_len
&&
2774 nla_put(skb
, T_ctx_peer_addr
, connection
->peer_addr_len
, &connection
->peer_addr
))
2775 goto nla_put_failure
;
2777 nla_nest_end(skb
, nla
);
2782 nla_nest_cancel(skb
, nla
);
2787 * Return the connection of @resource if @resource has exactly one connection.
2789 static struct drbd_connection
*the_only_connection(struct drbd_resource
*resource
)
2791 struct list_head
*connections
= &resource
->connections
;
2793 if (list_empty(connections
) || connections
->next
->next
!= connections
)
2795 return list_first_entry(&resource
->connections
, struct drbd_connection
, connections
);
2798 int nla_put_status_info(struct sk_buff
*skb
, struct drbd_device
*device
,
2799 const struct sib_info
*sib
)
2801 struct drbd_resource
*resource
= device
->resource
;
2802 struct state_info
*si
= NULL
; /* for sizeof(si->member); */
2806 int exclude_sensitive
;
2808 /* If sib != NULL, this is drbd_bcast_event, which anyone can listen
2809 * to. So we better exclude_sensitive information.
2811 * If sib == NULL, this is drbd_adm_get_status, executed synchronously
2812 * in the context of the requesting user process. Exclude sensitive
2813 * information, unless current has superuser.
2815 * NOTE: for drbd_adm_get_status_all(), this is a netlink dump, and
2816 * relies on the current implementation of netlink_dump(), which
2817 * executes the dump callback successively from netlink_recvmsg(),
2818 * always in the context of the receiving process */
2819 exclude_sensitive
= sib
|| !capable(CAP_SYS_ADMIN
);
2821 got_ldev
= get_ldev(device
);
2823 /* We need to add connection name and volume number information still.
2824 * Minor number is in drbd_genlmsghdr. */
2825 if (nla_put_drbd_cfg_context(skb
, resource
, the_only_connection(resource
), device
))
2826 goto nla_put_failure
;
2828 if (res_opts_to_skb(skb
, &device
->resource
->res_opts
, exclude_sensitive
))
2829 goto nla_put_failure
;
2833 struct disk_conf
*disk_conf
;
2835 disk_conf
= rcu_dereference(device
->ldev
->disk_conf
);
2836 err
= disk_conf_to_skb(skb
, disk_conf
, exclude_sensitive
);
2839 struct net_conf
*nc
;
2841 nc
= rcu_dereference(first_peer_device(device
)->connection
->net_conf
);
2843 err
= net_conf_to_skb(skb
, nc
, exclude_sensitive
);
2847 goto nla_put_failure
;
2849 nla
= nla_nest_start(skb
, DRBD_NLA_STATE_INFO
);
2851 goto nla_put_failure
;
2852 if (nla_put_u32(skb
, T_sib_reason
, sib
? sib
->sib_reason
: SIB_GET_STATUS_REPLY
) ||
2853 nla_put_u32(skb
, T_current_state
, device
->state
.i
) ||
2854 nla_put_u64(skb
, T_ed_uuid
, device
->ed_uuid
) ||
2855 nla_put_u64(skb
, T_capacity
, drbd_get_capacity(device
->this_bdev
)) ||
2856 nla_put_u64(skb
, T_send_cnt
, device
->send_cnt
) ||
2857 nla_put_u64(skb
, T_recv_cnt
, device
->recv_cnt
) ||
2858 nla_put_u64(skb
, T_read_cnt
, device
->read_cnt
) ||
2859 nla_put_u64(skb
, T_writ_cnt
, device
->writ_cnt
) ||
2860 nla_put_u64(skb
, T_al_writ_cnt
, device
->al_writ_cnt
) ||
2861 nla_put_u64(skb
, T_bm_writ_cnt
, device
->bm_writ_cnt
) ||
2862 nla_put_u32(skb
, T_ap_bio_cnt
, atomic_read(&device
->ap_bio_cnt
)) ||
2863 nla_put_u32(skb
, T_ap_pending_cnt
, atomic_read(&device
->ap_pending_cnt
)) ||
2864 nla_put_u32(skb
, T_rs_pending_cnt
, atomic_read(&device
->rs_pending_cnt
)))
2865 goto nla_put_failure
;
2870 spin_lock_irq(&device
->ldev
->md
.uuid_lock
);
2871 err
= nla_put(skb
, T_uuids
, sizeof(si
->uuids
), device
->ldev
->md
.uuid
);
2872 spin_unlock_irq(&device
->ldev
->md
.uuid_lock
);
2875 goto nla_put_failure
;
2877 if (nla_put_u32(skb
, T_disk_flags
, device
->ldev
->md
.flags
) ||
2878 nla_put_u64(skb
, T_bits_total
, drbd_bm_bits(device
)) ||
2879 nla_put_u64(skb
, T_bits_oos
, drbd_bm_total_weight(device
)))
2880 goto nla_put_failure
;
2881 if (C_SYNC_SOURCE
<= device
->state
.conn
&&
2882 C_PAUSED_SYNC_T
>= device
->state
.conn
) {
2883 if (nla_put_u64(skb
, T_bits_rs_total
, device
->rs_total
) ||
2884 nla_put_u64(skb
, T_bits_rs_failed
, device
->rs_failed
))
2885 goto nla_put_failure
;
2890 switch(sib
->sib_reason
) {
2891 case SIB_SYNC_PROGRESS
:
2892 case SIB_GET_STATUS_REPLY
:
2894 case SIB_STATE_CHANGE
:
2895 if (nla_put_u32(skb
, T_prev_state
, sib
->os
.i
) ||
2896 nla_put_u32(skb
, T_new_state
, sib
->ns
.i
))
2897 goto nla_put_failure
;
2899 case SIB_HELPER_POST
:
2900 if (nla_put_u32(skb
, T_helper_exit_code
,
2901 sib
->helper_exit_code
))
2902 goto nla_put_failure
;
2904 case SIB_HELPER_PRE
:
2905 if (nla_put_string(skb
, T_helper
, sib
->helper_name
))
2906 goto nla_put_failure
;
2910 nla_nest_end(skb
, nla
);
2920 int drbd_adm_get_status(struct sk_buff
*skb
, struct genl_info
*info
)
2922 enum drbd_ret_code retcode
;
2925 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_MINOR
);
2926 if (!adm_ctx
.reply_skb
)
2928 if (retcode
!= NO_ERROR
)
2931 err
= nla_put_status_info(adm_ctx
.reply_skb
, adm_ctx
.device
, NULL
);
2933 nlmsg_free(adm_ctx
.reply_skb
);
2937 drbd_adm_finish(info
, retcode
);
2941 static int get_one_status(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2943 struct drbd_device
*device
;
2944 struct drbd_genlmsghdr
*dh
;
2945 struct drbd_resource
*pos
= (struct drbd_resource
*)cb
->args
[0];
2946 struct drbd_resource
*resource
= NULL
;
2947 struct drbd_resource
*tmp
;
2948 unsigned volume
= cb
->args
[1];
2950 /* Open coded, deferred, iteration:
2951 * for_each_resource_safe(resource, tmp, &drbd_resources) {
2952 * connection = "first connection of resource or undefined";
2953 * idr_for_each_entry(&resource->devices, device, i) {
2957 * where resource is cb->args[0];
2958 * and i is cb->args[1];
2960 * cb->args[2] indicates if we shall loop over all resources,
2961 * or just dump all volumes of a single resource.
2963 * This may miss entries inserted after this dump started,
2964 * or entries deleted before they are reached.
2966 * We need to make sure the device won't disappear while
2967 * we are looking at it, and revalidate our iterators
2968 * on each iteration.
2971 /* synchronize with conn_create()/drbd_destroy_connection() */
2973 /* revalidate iterator position */
2974 for_each_resource_rcu(tmp
, &drbd_resources
) {
2976 /* first iteration */
2988 device
= idr_get_next(&resource
->devices
, &volume
);
2990 /* No more volumes to dump on this resource.
2991 * Advance resource iterator. */
2992 pos
= list_entry_rcu(resource
->resources
.next
,
2993 struct drbd_resource
, resources
);
2994 /* Did we dump any volume of this resource yet? */
2996 /* If we reached the end of the list,
2997 * or only a single resource dump was requested,
2999 if (&pos
->resources
== &drbd_resources
|| cb
->args
[2])
3007 dh
= genlmsg_put(skb
, NETLINK_CB(cb
->skb
).portid
,
3008 cb
->nlh
->nlmsg_seq
, &drbd_genl_family
,
3009 NLM_F_MULTI
, DRBD_ADM_GET_STATUS
);
3014 /* This is a connection without a single volume.
3015 * Suprisingly enough, it may have a network
3017 struct drbd_connection
*connection
;
3020 dh
->ret_code
= NO_ERROR
;
3021 connection
= the_only_connection(resource
);
3022 if (nla_put_drbd_cfg_context(skb
, resource
, connection
, NULL
))
3025 struct net_conf
*nc
;
3027 nc
= rcu_dereference(connection
->net_conf
);
3028 if (nc
&& net_conf_to_skb(skb
, nc
, 1) != 0)
3034 D_ASSERT(device
, device
->vnr
== volume
);
3035 D_ASSERT(device
, device
->resource
== resource
);
3037 dh
->minor
= device_to_minor(device
);
3038 dh
->ret_code
= NO_ERROR
;
3040 if (nla_put_status_info(skb
, device
, NULL
)) {
3042 genlmsg_cancel(skb
, dh
);
3046 genlmsg_end(skb
, dh
);
3051 /* where to start the next iteration */
3052 cb
->args
[0] = (long)pos
;
3053 cb
->args
[1] = (pos
== resource
) ? volume
+ 1 : 0;
3055 /* No more resources/volumes/minors found results in an empty skb.
3056 * Which will terminate the dump. */
3061 * Request status of all resources, or of all volumes within a single resource.
3063 * This is a dump, as the answer may not fit in a single reply skb otherwise.
3064 * Which means we cannot use the family->attrbuf or other such members, because
3065 * dump is NOT protected by the genl_lock(). During dump, we only have access
3066 * to the incoming skb, and need to opencode "parsing" of the nlattr payload.
3068 * Once things are setup properly, we call into get_one_status().
3070 int drbd_adm_get_status_all(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3072 const unsigned hdrlen
= GENL_HDRLEN
+ GENL_MAGIC_FAMILY_HDRSZ
;
3074 const char *resource_name
;
3075 struct drbd_resource
*resource
;
3078 /* Is this a followup call? */
3080 /* ... of a single resource dump,
3081 * and the resource iterator has been advanced already? */
3082 if (cb
->args
[2] && cb
->args
[2] != cb
->args
[0])
3083 return 0; /* DONE. */
3087 /* First call (from netlink_dump_start). We need to figure out
3088 * which resource(s) the user wants us to dump. */
3089 nla
= nla_find(nlmsg_attrdata(cb
->nlh
, hdrlen
),
3090 nlmsg_attrlen(cb
->nlh
, hdrlen
),
3091 DRBD_NLA_CFG_CONTEXT
);
3093 /* No explicit context given. Dump all. */
3096 maxtype
= ARRAY_SIZE(drbd_cfg_context_nl_policy
) - 1;
3097 nla
= drbd_nla_find_nested(maxtype
, nla
, __nla_type(T_ctx_resource_name
));
3099 return PTR_ERR(nla
);
3100 /* context given, but no name present? */
3103 resource_name
= nla_data(nla
);
3104 if (!*resource_name
)
3106 resource
= drbd_find_resource(resource_name
);
3110 kref_put(&resource
->kref
, drbd_destroy_resource
); /* get_one_status() revalidates the resource */
3112 /* prime iterators, and set "filter" mode mark:
3113 * only dump this connection. */
3114 cb
->args
[0] = (long)resource
;
3115 /* cb->args[1] = 0; passed in this way. */
3116 cb
->args
[2] = (long)resource
;
3119 return get_one_status(skb
, cb
);
3122 int drbd_adm_get_timeout_type(struct sk_buff
*skb
, struct genl_info
*info
)
3124 enum drbd_ret_code retcode
;
3125 struct timeout_parms tp
;
3128 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_MINOR
);
3129 if (!adm_ctx
.reply_skb
)
3131 if (retcode
!= NO_ERROR
)
3135 adm_ctx
.device
->state
.pdsk
== D_OUTDATED
? UT_PEER_OUTDATED
:
3136 test_bit(USE_DEGR_WFC_T
, &adm_ctx
.device
->flags
) ? UT_DEGRADED
:
3139 err
= timeout_parms_to_priv_skb(adm_ctx
.reply_skb
, &tp
);
3141 nlmsg_free(adm_ctx
.reply_skb
);
3145 drbd_adm_finish(info
, retcode
);
3149 int drbd_adm_start_ov(struct sk_buff
*skb
, struct genl_info
*info
)
3151 struct drbd_device
*device
;
3152 enum drbd_ret_code retcode
;
3153 struct start_ov_parms parms
;
3155 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_MINOR
);
3156 if (!adm_ctx
.reply_skb
)
3158 if (retcode
!= NO_ERROR
)
3161 device
= adm_ctx
.device
;
3163 /* resume from last known position, if possible */
3164 parms
.ov_start_sector
= device
->ov_start_sector
;
3165 parms
.ov_stop_sector
= ULLONG_MAX
;
3166 if (info
->attrs
[DRBD_NLA_START_OV_PARMS
]) {
3167 int err
= start_ov_parms_from_attrs(&parms
, info
);
3169 retcode
= ERR_MANDATORY_TAG
;
3170 drbd_msg_put_info(from_attrs_err_to_txt(err
));
3174 /* w_make_ov_request expects position to be aligned */
3175 device
->ov_start_sector
= parms
.ov_start_sector
& ~(BM_SECT_PER_BIT
-1);
3176 device
->ov_stop_sector
= parms
.ov_stop_sector
;
3178 /* If there is still bitmap IO pending, e.g. previous resync or verify
3179 * just being finished, wait for it before requesting a new resync. */
3180 drbd_suspend_io(device
);
3181 wait_event(device
->misc_wait
, !test_bit(BITMAP_IO
, &device
->flags
));
3182 retcode
= drbd_request_state(device
, NS(conn
, C_VERIFY_S
));
3183 drbd_resume_io(device
);
3185 drbd_adm_finish(info
, retcode
);
3190 int drbd_adm_new_c_uuid(struct sk_buff
*skb
, struct genl_info
*info
)
3192 struct drbd_device
*device
;
3193 enum drbd_ret_code retcode
;
3194 int skip_initial_sync
= 0;
3196 struct new_c_uuid_parms args
;
3198 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_MINOR
);
3199 if (!adm_ctx
.reply_skb
)
3201 if (retcode
!= NO_ERROR
)
3204 device
= adm_ctx
.device
;
3205 memset(&args
, 0, sizeof(args
));
3206 if (info
->attrs
[DRBD_NLA_NEW_C_UUID_PARMS
]) {
3207 err
= new_c_uuid_parms_from_attrs(&args
, info
);
3209 retcode
= ERR_MANDATORY_TAG
;
3210 drbd_msg_put_info(from_attrs_err_to_txt(err
));
3215 mutex_lock(device
->state_mutex
); /* Protects us against serialized state changes. */
3217 if (!get_ldev(device
)) {
3218 retcode
= ERR_NO_DISK
;
3222 /* this is "skip initial sync", assume to be clean */
3223 if (device
->state
.conn
== C_CONNECTED
&&
3224 first_peer_device(device
)->connection
->agreed_pro_version
>= 90 &&
3225 device
->ldev
->md
.uuid
[UI_CURRENT
] == UUID_JUST_CREATED
&& args
.clear_bm
) {
3226 drbd_info(device
, "Preparing to skip initial sync\n");
3227 skip_initial_sync
= 1;
3228 } else if (device
->state
.conn
!= C_STANDALONE
) {
3229 retcode
= ERR_CONNECTED
;
3233 drbd_uuid_set(device
, UI_BITMAP
, 0); /* Rotate UI_BITMAP to History 1, etc... */
3234 drbd_uuid_new_current(device
); /* New current, previous to UI_BITMAP */
3236 if (args
.clear_bm
) {
3237 err
= drbd_bitmap_io(device
, &drbd_bmio_clear_n_write
,
3238 "clear_n_write from new_c_uuid", BM_LOCKED_MASK
);
3240 drbd_err(device
, "Writing bitmap failed with %d\n", err
);
3241 retcode
= ERR_IO_MD_DISK
;
3243 if (skip_initial_sync
) {
3244 drbd_send_uuids_skip_initial_sync(device
);
3245 _drbd_uuid_set(device
, UI_BITMAP
, 0);
3246 drbd_print_uuids(device
, "cleared bitmap UUID");
3247 spin_lock_irq(&first_peer_device(device
)->connection
->req_lock
);
3248 _drbd_set_state(_NS2(device
, disk
, D_UP_TO_DATE
, pdsk
, D_UP_TO_DATE
),
3250 spin_unlock_irq(&first_peer_device(device
)->connection
->req_lock
);
3254 drbd_md_sync(device
);
3258 mutex_unlock(device
->state_mutex
);
3260 drbd_adm_finish(info
, retcode
);
3264 static enum drbd_ret_code
3265 drbd_check_resource_name(const char *name
)
3267 if (!name
|| !name
[0]) {
3268 drbd_msg_put_info("resource name missing");
3269 return ERR_MANDATORY_TAG
;
3271 /* if we want to use these in sysfs/configfs/debugfs some day,
3272 * we must not allow slashes */
3273 if (strchr(name
, '/')) {
3274 drbd_msg_put_info("invalid resource name");
3275 return ERR_INVALID_REQUEST
;
3280 int drbd_adm_new_resource(struct sk_buff
*skb
, struct genl_info
*info
)
3282 enum drbd_ret_code retcode
;
3283 struct res_opts res_opts
;
3286 retcode
= drbd_adm_prepare(skb
, info
, 0);
3287 if (!adm_ctx
.reply_skb
)
3289 if (retcode
!= NO_ERROR
)
3292 set_res_opts_defaults(&res_opts
);
3293 err
= res_opts_from_attrs(&res_opts
, info
);
3294 if (err
&& err
!= -ENOMSG
) {
3295 retcode
= ERR_MANDATORY_TAG
;
3296 drbd_msg_put_info(from_attrs_err_to_txt(err
));
3300 retcode
= drbd_check_resource_name(adm_ctx
.resource_name
);
3301 if (retcode
!= NO_ERROR
)
3304 if (adm_ctx
.resource
) {
3305 if (info
->nlhdr
->nlmsg_flags
& NLM_F_EXCL
) {
3306 retcode
= ERR_INVALID_REQUEST
;
3307 drbd_msg_put_info("resource exists");
3309 /* else: still NO_ERROR */
3313 if (!conn_create(adm_ctx
.resource_name
, &res_opts
))
3314 retcode
= ERR_NOMEM
;
3316 drbd_adm_finish(info
, retcode
);
3320 int drbd_adm_new_minor(struct sk_buff
*skb
, struct genl_info
*info
)
3322 struct drbd_genlmsghdr
*dh
= info
->userhdr
;
3323 enum drbd_ret_code retcode
;
3325 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_RESOURCE
);
3326 if (!adm_ctx
.reply_skb
)
3328 if (retcode
!= NO_ERROR
)
3331 if (dh
->minor
> MINORMASK
) {
3332 drbd_msg_put_info("requested minor out of range");
3333 retcode
= ERR_INVALID_REQUEST
;
3336 if (adm_ctx
.volume
> DRBD_VOLUME_MAX
) {
3337 drbd_msg_put_info("requested volume id out of range");
3338 retcode
= ERR_INVALID_REQUEST
;
3342 /* drbd_adm_prepare made sure already
3343 * that first_peer_device(device)->connection and device->vnr match the request. */
3344 if (adm_ctx
.device
) {
3345 if (info
->nlhdr
->nlmsg_flags
& NLM_F_EXCL
)
3346 retcode
= ERR_MINOR_EXISTS
;
3347 /* else: still NO_ERROR */
3351 retcode
= drbd_create_device(adm_ctx
.resource
, dh
->minor
, adm_ctx
.volume
);
3353 drbd_adm_finish(info
, retcode
);
3357 static enum drbd_ret_code
adm_del_minor(struct drbd_device
*device
)
3359 if (device
->state
.disk
== D_DISKLESS
&&
3360 /* no need to be device->state.conn == C_STANDALONE &&
3361 * we may want to delete a minor from a live replication group.
3363 device
->state
.role
== R_SECONDARY
) {
3364 _drbd_request_state(device
, NS(conn
, C_WF_REPORT_PARAMS
),
3365 CS_VERBOSE
+ CS_WAIT_COMPLETE
);
3366 drbd_delete_device(device
);
3369 return ERR_MINOR_CONFIGURED
;
3372 int drbd_adm_del_minor(struct sk_buff
*skb
, struct genl_info
*info
)
3374 enum drbd_ret_code retcode
;
3376 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_MINOR
);
3377 if (!adm_ctx
.reply_skb
)
3379 if (retcode
!= NO_ERROR
)
3382 retcode
= adm_del_minor(adm_ctx
.device
);
3384 drbd_adm_finish(info
, retcode
);
3388 int drbd_adm_down(struct sk_buff
*skb
, struct genl_info
*info
)
3390 int retcode
; /* enum drbd_ret_code rsp. enum drbd_state_rv */
3391 struct drbd_peer_device
*peer_device
;
3394 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_RESOURCE
);
3395 if (!adm_ctx
.reply_skb
)
3397 if (retcode
!= NO_ERROR
)
3401 idr_for_each_entry(&adm_ctx
.connection
->peer_devices
, peer_device
, i
) {
3402 retcode
= drbd_set_role(peer_device
->device
, R_SECONDARY
, 0);
3403 if (retcode
< SS_SUCCESS
) {
3404 drbd_msg_put_info("failed to demote");
3409 retcode
= conn_try_disconnect(adm_ctx
.connection
, 0);
3410 if (retcode
< SS_SUCCESS
) {
3411 drbd_msg_put_info("failed to disconnect");
3416 idr_for_each_entry(&adm_ctx
.connection
->peer_devices
, peer_device
, i
) {
3417 retcode
= adm_detach(peer_device
->device
, 0);
3418 if (retcode
< SS_SUCCESS
|| retcode
> NO_ERROR
) {
3419 drbd_msg_put_info("failed to detach");
3424 /* If we reach this, all volumes (of this connection) are Secondary,
3425 * Disconnected, Diskless, aka Unconfigured. Make sure all threads have
3426 * actually stopped, state handling only does drbd_thread_stop_nowait(). */
3427 drbd_thread_stop(&adm_ctx
.connection
->worker
);
3429 /* Now, nothing can fail anymore */
3431 /* delete volumes */
3432 idr_for_each_entry(&adm_ctx
.connection
->peer_devices
, peer_device
, i
) {
3433 retcode
= adm_del_minor(peer_device
->device
);
3434 if (retcode
!= NO_ERROR
) {
3435 /* "can not happen" */
3436 drbd_msg_put_info("failed to delete volume");
3441 /* delete connection */
3442 if (conn_lowest_minor(adm_ctx
.connection
) < 0) {
3443 struct drbd_resource
*resource
= adm_ctx
.connection
->resource
;
3445 list_del_rcu(&resource
->resources
);
3447 drbd_free_resource(resource
);
3451 /* "can not happen" */
3452 retcode
= ERR_RES_IN_USE
;
3453 drbd_msg_put_info("failed to delete connection");
3457 drbd_adm_finish(info
, retcode
);
3461 int drbd_adm_del_resource(struct sk_buff
*skb
, struct genl_info
*info
)
3463 struct drbd_resource
*resource
;
3464 struct drbd_connection
*connection
;
3465 enum drbd_ret_code retcode
;
3467 retcode
= drbd_adm_prepare(skb
, info
, DRBD_ADM_NEED_RESOURCE
);
3468 if (!adm_ctx
.reply_skb
)
3470 if (retcode
!= NO_ERROR
)
3473 resource
= adm_ctx
.resource
;
3474 for_each_connection(connection
, resource
) {
3475 if (connection
->cstate
> C_STANDALONE
) {
3476 retcode
= ERR_NET_CONFIGURED
;
3480 if (!idr_is_empty(&resource
->devices
)) {
3481 retcode
= ERR_RES_IN_USE
;
3485 list_del_rcu(&resource
->resources
);
3486 for_each_connection(connection
, resource
)
3487 drbd_thread_stop(&connection
->worker
);
3489 drbd_free_resource(resource
);
3492 drbd_adm_finish(info
, retcode
);
3496 void drbd_bcast_event(struct drbd_device
*device
, const struct sib_info
*sib
)
3498 static atomic_t drbd_genl_seq
= ATOMIC_INIT(2); /* two. */
3499 struct sk_buff
*msg
;
3500 struct drbd_genlmsghdr
*d_out
;
3504 if (sib
->sib_reason
== SIB_SYNC_PROGRESS
) {
3505 if (time_after(jiffies
, device
->rs_last_bcast
+ HZ
))
3506 device
->rs_last_bcast
= jiffies
;
3511 seq
= atomic_inc_return(&drbd_genl_seq
);
3512 msg
= genlmsg_new(NLMSG_GOODSIZE
, GFP_NOIO
);
3517 d_out
= genlmsg_put(msg
, 0, seq
, &drbd_genl_family
, 0, DRBD_EVENT
);
3518 if (!d_out
) /* cannot happen, but anyways. */
3519 goto nla_put_failure
;
3520 d_out
->minor
= device_to_minor(device
);
3521 d_out
->ret_code
= NO_ERROR
;
3523 if (nla_put_status_info(msg
, device
, sib
))
3524 goto nla_put_failure
;
3525 genlmsg_end(msg
, d_out
);
3526 err
= drbd_genl_multicast_events(msg
, 0);
3527 /* msg has been consumed or freed in netlink_broadcast() */
3528 if (err
&& err
!= -ESRCH
)
3536 drbd_err(device
, "Error %d while broadcasting event. "
3537 "Event seq:%u sib_reason:%u\n",
3538 err
, seq
, sib
->sib_reason
);