]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/block/drbd/drbd_nl.c
drbd: prepare sending side for REQ_DISCARD
[mirror_ubuntu-artful-kernel.git] / drivers / block / drbd / drbd_nl.c
1 /*
2 drbd_nl.c
3
4 This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
5
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>.
9
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)
13 any later version.
14
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.
19
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.
23
24 */
25
26 #include <linux/module.h>
27 #include <linux/drbd.h>
28 #include <linux/in.h>
29 #include <linux/fs.h>
30 #include <linux/file.h>
31 #include <linux/slab.h>
32 #include <linux/blkpg.h>
33 #include <linux/cpumask.h>
34 #include "drbd_int.h"
35 #include "drbd_protocol.h"
36 #include "drbd_req.h"
37 #include <asm/unaligned.h>
38 #include <linux/drbd_limits.h>
39 #include <linux/kthread.h>
40
41 #include <net/genetlink.h>
42
43 /* .doit */
44 // int drbd_adm_create_resource(struct sk_buff *skb, struct genl_info *info);
45 // int drbd_adm_delete_resource(struct sk_buff *skb, struct genl_info *info);
46
47 int drbd_adm_new_minor(struct sk_buff *skb, struct genl_info *info);
48 int drbd_adm_del_minor(struct sk_buff *skb, struct genl_info *info);
49
50 int drbd_adm_new_resource(struct sk_buff *skb, struct genl_info *info);
51 int drbd_adm_del_resource(struct sk_buff *skb, struct genl_info *info);
52 int drbd_adm_down(struct sk_buff *skb, struct genl_info *info);
53
54 int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info);
55 int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info);
56 int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info);
57 int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info);
58 int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info);
59 int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info);
60 int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info);
61 int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info);
62 int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info);
63 int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info);
64 int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info);
65 int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info);
66 int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info);
67 int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info);
68 int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info);
69 int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info);
70 int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info);
71 int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info);
72 int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info);
73 int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info);
74 /* .dumpit */
75 int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb);
76
77 #include <linux/drbd_genl_api.h>
78 #include "drbd_nla.h"
79 #include <linux/genl_magic_func.h>
80
81 /* used blkdev_get_by_path, to claim our meta data device(s) */
82 static char *drbd_m_holder = "Hands off! this is DRBD's meta data device.";
83
84 static void drbd_adm_send_reply(struct sk_buff *skb, struct genl_info *info)
85 {
86 genlmsg_end(skb, genlmsg_data(nlmsg_data(nlmsg_hdr(skb))));
87 if (genlmsg_reply(skb, info))
88 printk(KERN_ERR "drbd: error sending genl reply\n");
89 }
90
91 /* Used on a fresh "drbd_adm_prepare"d reply_skb, this cannot fail: The only
92 * reason it could fail was no space in skb, and there are 4k available. */
93 int drbd_msg_put_info(struct sk_buff *skb, const char *info)
94 {
95 struct nlattr *nla;
96 int err = -EMSGSIZE;
97
98 if (!info || !info[0])
99 return 0;
100
101 nla = nla_nest_start(skb, DRBD_NLA_CFG_REPLY);
102 if (!nla)
103 return err;
104
105 err = nla_put_string(skb, T_info_text, info);
106 if (err) {
107 nla_nest_cancel(skb, nla);
108 return err;
109 } else
110 nla_nest_end(skb, nla);
111 return 0;
112 }
113
114 /* This would be a good candidate for a "pre_doit" hook,
115 * and per-family private info->pointers.
116 * But we need to stay compatible with older kernels.
117 * If it returns successfully, adm_ctx members are valid.
118 *
119 * At this point, we still rely on the global genl_lock().
120 * If we want to avoid that, and allow "genl_family.parallel_ops", we may need
121 * to add additional synchronization against object destruction/modification.
122 */
123 #define DRBD_ADM_NEED_MINOR 1
124 #define DRBD_ADM_NEED_RESOURCE 2
125 #define DRBD_ADM_NEED_CONNECTION 4
126 static int drbd_adm_prepare(struct drbd_config_context *adm_ctx,
127 struct sk_buff *skb, struct genl_info *info, unsigned flags)
128 {
129 struct drbd_genlmsghdr *d_in = info->userhdr;
130 const u8 cmd = info->genlhdr->cmd;
131 int err;
132
133 memset(adm_ctx, 0, sizeof(*adm_ctx));
134
135 /* genl_rcv_msg only checks for CAP_NET_ADMIN on "GENL_ADMIN_PERM" :( */
136 if (cmd != DRBD_ADM_GET_STATUS && !capable(CAP_NET_ADMIN))
137 return -EPERM;
138
139 adm_ctx->reply_skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
140 if (!adm_ctx->reply_skb) {
141 err = -ENOMEM;
142 goto fail;
143 }
144
145 adm_ctx->reply_dh = genlmsg_put_reply(adm_ctx->reply_skb,
146 info, &drbd_genl_family, 0, cmd);
147 /* put of a few bytes into a fresh skb of >= 4k will always succeed.
148 * but anyways */
149 if (!adm_ctx->reply_dh) {
150 err = -ENOMEM;
151 goto fail;
152 }
153
154 adm_ctx->reply_dh->minor = d_in->minor;
155 adm_ctx->reply_dh->ret_code = NO_ERROR;
156
157 adm_ctx->volume = VOLUME_UNSPECIFIED;
158 if (info->attrs[DRBD_NLA_CFG_CONTEXT]) {
159 struct nlattr *nla;
160 /* parse and validate only */
161 err = drbd_cfg_context_from_attrs(NULL, info);
162 if (err)
163 goto fail;
164
165 /* It was present, and valid,
166 * copy it over to the reply skb. */
167 err = nla_put_nohdr(adm_ctx->reply_skb,
168 info->attrs[DRBD_NLA_CFG_CONTEXT]->nla_len,
169 info->attrs[DRBD_NLA_CFG_CONTEXT]);
170 if (err)
171 goto fail;
172
173 /* and assign stuff to the adm_ctx */
174 nla = nested_attr_tb[__nla_type(T_ctx_volume)];
175 if (nla)
176 adm_ctx->volume = nla_get_u32(nla);
177 nla = nested_attr_tb[__nla_type(T_ctx_resource_name)];
178 if (nla)
179 adm_ctx->resource_name = nla_data(nla);
180 adm_ctx->my_addr = nested_attr_tb[__nla_type(T_ctx_my_addr)];
181 adm_ctx->peer_addr = nested_attr_tb[__nla_type(T_ctx_peer_addr)];
182 if ((adm_ctx->my_addr &&
183 nla_len(adm_ctx->my_addr) > sizeof(adm_ctx->connection->my_addr)) ||
184 (adm_ctx->peer_addr &&
185 nla_len(adm_ctx->peer_addr) > sizeof(adm_ctx->connection->peer_addr))) {
186 err = -EINVAL;
187 goto fail;
188 }
189 }
190
191 adm_ctx->minor = d_in->minor;
192 adm_ctx->device = minor_to_device(d_in->minor);
193
194 /* We are protected by the global genl_lock().
195 * But we may explicitly drop it/retake it in drbd_adm_set_role(),
196 * so make sure this object stays around. */
197 if (adm_ctx->device)
198 kref_get(&adm_ctx->device->kref);
199
200 if (adm_ctx->resource_name) {
201 adm_ctx->resource = drbd_find_resource(adm_ctx->resource_name);
202 }
203
204 if (!adm_ctx->device && (flags & DRBD_ADM_NEED_MINOR)) {
205 drbd_msg_put_info(adm_ctx->reply_skb, "unknown minor");
206 return ERR_MINOR_INVALID;
207 }
208 if (!adm_ctx->resource && (flags & DRBD_ADM_NEED_RESOURCE)) {
209 drbd_msg_put_info(adm_ctx->reply_skb, "unknown resource");
210 if (adm_ctx->resource_name)
211 return ERR_RES_NOT_KNOWN;
212 return ERR_INVALID_REQUEST;
213 }
214
215 if (flags & DRBD_ADM_NEED_CONNECTION) {
216 if (adm_ctx->resource) {
217 drbd_msg_put_info(adm_ctx->reply_skb, "no resource name expected");
218 return ERR_INVALID_REQUEST;
219 }
220 if (adm_ctx->device) {
221 drbd_msg_put_info(adm_ctx->reply_skb, "no minor number expected");
222 return ERR_INVALID_REQUEST;
223 }
224 if (adm_ctx->my_addr && adm_ctx->peer_addr)
225 adm_ctx->connection = conn_get_by_addrs(nla_data(adm_ctx->my_addr),
226 nla_len(adm_ctx->my_addr),
227 nla_data(adm_ctx->peer_addr),
228 nla_len(adm_ctx->peer_addr));
229 if (!adm_ctx->connection) {
230 drbd_msg_put_info(adm_ctx->reply_skb, "unknown connection");
231 return ERR_INVALID_REQUEST;
232 }
233 }
234
235 /* some more paranoia, if the request was over-determined */
236 if (adm_ctx->device && adm_ctx->resource &&
237 adm_ctx->device->resource != adm_ctx->resource) {
238 pr_warning("request: minor=%u, resource=%s; but that minor belongs to resource %s\n",
239 adm_ctx->minor, adm_ctx->resource->name,
240 adm_ctx->device->resource->name);
241 drbd_msg_put_info(adm_ctx->reply_skb, "minor exists in different resource");
242 return ERR_INVALID_REQUEST;
243 }
244 if (adm_ctx->device &&
245 adm_ctx->volume != VOLUME_UNSPECIFIED &&
246 adm_ctx->volume != adm_ctx->device->vnr) {
247 pr_warning("request: minor=%u, volume=%u; but that minor is volume %u in %s\n",
248 adm_ctx->minor, adm_ctx->volume,
249 adm_ctx->device->vnr,
250 adm_ctx->device->resource->name);
251 drbd_msg_put_info(adm_ctx->reply_skb, "minor exists as different volume");
252 return ERR_INVALID_REQUEST;
253 }
254
255 /* still, provide adm_ctx->resource always, if possible. */
256 if (!adm_ctx->resource) {
257 adm_ctx->resource = adm_ctx->device ? adm_ctx->device->resource
258 : adm_ctx->connection ? adm_ctx->connection->resource : NULL;
259 if (adm_ctx->resource)
260 kref_get(&adm_ctx->resource->kref);
261 }
262
263 return NO_ERROR;
264
265 fail:
266 nlmsg_free(adm_ctx->reply_skb);
267 adm_ctx->reply_skb = NULL;
268 return err;
269 }
270
271 static int drbd_adm_finish(struct drbd_config_context *adm_ctx,
272 struct genl_info *info, int retcode)
273 {
274 if (adm_ctx->device) {
275 kref_put(&adm_ctx->device->kref, drbd_destroy_device);
276 adm_ctx->device = NULL;
277 }
278 if (adm_ctx->connection) {
279 kref_put(&adm_ctx->connection->kref, &drbd_destroy_connection);
280 adm_ctx->connection = NULL;
281 }
282 if (adm_ctx->resource) {
283 kref_put(&adm_ctx->resource->kref, drbd_destroy_resource);
284 adm_ctx->resource = NULL;
285 }
286
287 if (!adm_ctx->reply_skb)
288 return -ENOMEM;
289
290 adm_ctx->reply_dh->ret_code = retcode;
291 drbd_adm_send_reply(adm_ctx->reply_skb, info);
292 return 0;
293 }
294
295 static void setup_khelper_env(struct drbd_connection *connection, char **envp)
296 {
297 char *afs;
298
299 /* FIXME: A future version will not allow this case. */
300 if (connection->my_addr_len == 0 || connection->peer_addr_len == 0)
301 return;
302
303 switch (((struct sockaddr *)&connection->peer_addr)->sa_family) {
304 case AF_INET6:
305 afs = "ipv6";
306 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI6",
307 &((struct sockaddr_in6 *)&connection->peer_addr)->sin6_addr);
308 break;
309 case AF_INET:
310 afs = "ipv4";
311 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
312 &((struct sockaddr_in *)&connection->peer_addr)->sin_addr);
313 break;
314 default:
315 afs = "ssocks";
316 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
317 &((struct sockaddr_in *)&connection->peer_addr)->sin_addr);
318 }
319 snprintf(envp[3], 20, "DRBD_PEER_AF=%s", afs);
320 }
321
322 int drbd_khelper(struct drbd_device *device, char *cmd)
323 {
324 char *envp[] = { "HOME=/",
325 "TERM=linux",
326 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
327 (char[20]) { }, /* address family */
328 (char[60]) { }, /* address */
329 NULL };
330 char mb[12];
331 char *argv[] = {usermode_helper, cmd, mb, NULL };
332 struct drbd_connection *connection = first_peer_device(device)->connection;
333 struct sib_info sib;
334 int ret;
335
336 if (current == connection->worker.task)
337 set_bit(CALLBACK_PENDING, &connection->flags);
338
339 snprintf(mb, 12, "minor-%d", device_to_minor(device));
340 setup_khelper_env(connection, envp);
341
342 /* The helper may take some time.
343 * write out any unsynced meta data changes now */
344 drbd_md_sync(device);
345
346 drbd_info(device, "helper command: %s %s %s\n", usermode_helper, cmd, mb);
347 sib.sib_reason = SIB_HELPER_PRE;
348 sib.helper_name = cmd;
349 drbd_bcast_event(device, &sib);
350 ret = call_usermodehelper(usermode_helper, argv, envp, UMH_WAIT_PROC);
351 if (ret)
352 drbd_warn(device, "helper command: %s %s %s exit code %u (0x%x)\n",
353 usermode_helper, cmd, mb,
354 (ret >> 8) & 0xff, ret);
355 else
356 drbd_info(device, "helper command: %s %s %s exit code %u (0x%x)\n",
357 usermode_helper, cmd, mb,
358 (ret >> 8) & 0xff, ret);
359 sib.sib_reason = SIB_HELPER_POST;
360 sib.helper_exit_code = ret;
361 drbd_bcast_event(device, &sib);
362
363 if (current == connection->worker.task)
364 clear_bit(CALLBACK_PENDING, &connection->flags);
365
366 if (ret < 0) /* Ignore any ERRNOs we got. */
367 ret = 0;
368
369 return ret;
370 }
371
372 static int conn_khelper(struct drbd_connection *connection, char *cmd)
373 {
374 char *envp[] = { "HOME=/",
375 "TERM=linux",
376 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
377 (char[20]) { }, /* address family */
378 (char[60]) { }, /* address */
379 NULL };
380 char *resource_name = connection->resource->name;
381 char *argv[] = {usermode_helper, cmd, resource_name, NULL };
382 int ret;
383
384 setup_khelper_env(connection, envp);
385 conn_md_sync(connection);
386
387 drbd_info(connection, "helper command: %s %s %s\n", usermode_helper, cmd, resource_name);
388 /* TODO: conn_bcast_event() ?? */
389
390 ret = call_usermodehelper(usermode_helper, argv, envp, UMH_WAIT_PROC);
391 if (ret)
392 drbd_warn(connection, "helper command: %s %s %s exit code %u (0x%x)\n",
393 usermode_helper, cmd, resource_name,
394 (ret >> 8) & 0xff, ret);
395 else
396 drbd_info(connection, "helper command: %s %s %s exit code %u (0x%x)\n",
397 usermode_helper, cmd, resource_name,
398 (ret >> 8) & 0xff, ret);
399 /* TODO: conn_bcast_event() ?? */
400
401 if (ret < 0) /* Ignore any ERRNOs we got. */
402 ret = 0;
403
404 return ret;
405 }
406
407 static enum drbd_fencing_p highest_fencing_policy(struct drbd_connection *connection)
408 {
409 enum drbd_fencing_p fp = FP_NOT_AVAIL;
410 struct drbd_peer_device *peer_device;
411 int vnr;
412
413 rcu_read_lock();
414 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
415 struct drbd_device *device = peer_device->device;
416 if (get_ldev_if_state(device, D_CONSISTENT)) {
417 struct disk_conf *disk_conf =
418 rcu_dereference(peer_device->device->ldev->disk_conf);
419 fp = max_t(enum drbd_fencing_p, fp, disk_conf->fencing);
420 put_ldev(device);
421 }
422 }
423 rcu_read_unlock();
424
425 if (fp == FP_NOT_AVAIL) {
426 /* IO Suspending works on the whole resource.
427 Do it only for one device. */
428 vnr = 0;
429 peer_device = idr_get_next(&connection->peer_devices, &vnr);
430 drbd_change_state(peer_device->device, CS_VERBOSE | CS_HARD, NS(susp_fen, 0));
431 }
432
433 return fp;
434 }
435
436 bool conn_try_outdate_peer(struct drbd_connection *connection)
437 {
438 unsigned int connect_cnt;
439 union drbd_state mask = { };
440 union drbd_state val = { };
441 enum drbd_fencing_p fp;
442 char *ex_to_string;
443 int r;
444
445 if (connection->cstate >= C_WF_REPORT_PARAMS) {
446 drbd_err(connection, "Expected cstate < C_WF_REPORT_PARAMS\n");
447 return false;
448 }
449
450 spin_lock_irq(&connection->resource->req_lock);
451 connect_cnt = connection->connect_cnt;
452 spin_unlock_irq(&connection->resource->req_lock);
453
454 fp = highest_fencing_policy(connection);
455 switch (fp) {
456 case FP_NOT_AVAIL:
457 drbd_warn(connection, "Not fencing peer, I'm not even Consistent myself.\n");
458 goto out;
459 case FP_DONT_CARE:
460 return true;
461 default: ;
462 }
463
464 r = conn_khelper(connection, "fence-peer");
465
466 switch ((r>>8) & 0xff) {
467 case 3: /* peer is inconsistent */
468 ex_to_string = "peer is inconsistent or worse";
469 mask.pdsk = D_MASK;
470 val.pdsk = D_INCONSISTENT;
471 break;
472 case 4: /* peer got outdated, or was already outdated */
473 ex_to_string = "peer was fenced";
474 mask.pdsk = D_MASK;
475 val.pdsk = D_OUTDATED;
476 break;
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";
481 mask.pdsk = D_MASK;
482 val.pdsk = D_OUTDATED;
483 } else {
484 ex_to_string = "peer unreachable, doing nothing since disk != UpToDate";
485 }
486 break;
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");
492 mask.disk = D_MASK;
493 val.disk = D_OUTDATED;
494 break;
495 case 7:
496 if (fp != FP_STONITH)
497 drbd_err(connection, "fence-peer() = 7 && fencing != Stonith !!!\n");
498 ex_to_string = "peer was stonithed";
499 mask.pdsk = D_MASK;
500 val.pdsk = D_OUTDATED;
501 break;
502 default:
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 */
506 }
507
508 drbd_info(connection, "fence-peer helper returned %d (%s)\n",
509 (r>>8) & 0xff, ex_to_string);
510
511 out:
512
513 /* Not using
514 conn_request_state(connection, mask, val, CS_VERBOSE);
515 here, because we might were able to re-establish the connection in the
516 meantime. */
517 spin_lock_irq(&connection->resource->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");
523 else
524 _conn_request_state(connection, mask, val, CS_VERBOSE);
525 }
526 spin_unlock_irq(&connection->resource->req_lock);
527
528 return conn_highest_pdsk(connection) <= D_OUTDATED;
529 }
530
531 static int _try_outdate_peer_async(void *data)
532 {
533 struct drbd_connection *connection = (struct drbd_connection *)data;
534
535 conn_try_outdate_peer(connection);
536
537 kref_put(&connection->kref, drbd_destroy_connection);
538 return 0;
539 }
540
541 void conn_try_outdate_peer_async(struct drbd_connection *connection)
542 {
543 struct task_struct *opa;
544
545 kref_get(&connection->kref);
546 opa = kthread_run(_try_outdate_peer_async, connection, "drbd_async_h");
547 if (IS_ERR(opa)) {
548 drbd_err(connection, "out of mem, failed to invoke fence-peer helper\n");
549 kref_put(&connection->kref, drbd_destroy_connection);
550 }
551 }
552
553 enum drbd_state_rv
554 drbd_set_role(struct drbd_device *device, enum drbd_role new_role, int force)
555 {
556 const int max_tries = 4;
557 enum drbd_state_rv rv = SS_UNKNOWN_ERROR;
558 struct net_conf *nc;
559 int try = 0;
560 int forced = 0;
561 union drbd_state mask, val;
562
563 if (new_role == R_PRIMARY) {
564 struct drbd_connection *connection;
565
566 /* Detect dead peers as soon as possible. */
567
568 rcu_read_lock();
569 for_each_connection(connection, device->resource)
570 request_ping(connection);
571 rcu_read_unlock();
572 }
573
574 mutex_lock(device->state_mutex);
575
576 mask.i = 0; mask.role = R_MASK;
577 val.i = 0; val.role = new_role;
578
579 while (try++ < max_tries) {
580 rv = _drbd_request_state(device, mask, val, CS_WAIT_COMPLETE);
581
582 /* in case we first succeeded to outdate,
583 * but now suddenly could establish a connection */
584 if (rv == SS_CW_FAILED_BY_PEER && mask.pdsk != 0) {
585 val.pdsk = 0;
586 mask.pdsk = 0;
587 continue;
588 }
589
590 if (rv == SS_NO_UP_TO_DATE_DISK && force &&
591 (device->state.disk < D_UP_TO_DATE &&
592 device->state.disk >= D_INCONSISTENT)) {
593 mask.disk = D_MASK;
594 val.disk = D_UP_TO_DATE;
595 forced = 1;
596 continue;
597 }
598
599 if (rv == SS_NO_UP_TO_DATE_DISK &&
600 device->state.disk == D_CONSISTENT && mask.pdsk == 0) {
601 D_ASSERT(device, device->state.pdsk == D_UNKNOWN);
602
603 if (conn_try_outdate_peer(first_peer_device(device)->connection)) {
604 val.disk = D_UP_TO_DATE;
605 mask.disk = D_MASK;
606 }
607 continue;
608 }
609
610 if (rv == SS_NOTHING_TO_DO)
611 goto out;
612 if (rv == SS_PRIMARY_NOP && mask.pdsk == 0) {
613 if (!conn_try_outdate_peer(first_peer_device(device)->connection) && force) {
614 drbd_warn(device, "Forced into split brain situation!\n");
615 mask.pdsk = D_MASK;
616 val.pdsk = D_OUTDATED;
617
618 }
619 continue;
620 }
621 if (rv == SS_TWO_PRIMARIES) {
622 /* Maybe the peer is detected as dead very soon...
623 retry at most once more in this case. */
624 int timeo;
625 rcu_read_lock();
626 nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
627 timeo = nc ? (nc->ping_timeo + 1) * HZ / 10 : 1;
628 rcu_read_unlock();
629 schedule_timeout_interruptible(timeo);
630 if (try < max_tries)
631 try = max_tries - 1;
632 continue;
633 }
634 if (rv < SS_SUCCESS) {
635 rv = _drbd_request_state(device, mask, val,
636 CS_VERBOSE + CS_WAIT_COMPLETE);
637 if (rv < SS_SUCCESS)
638 goto out;
639 }
640 break;
641 }
642
643 if (rv < SS_SUCCESS)
644 goto out;
645
646 if (forced)
647 drbd_warn(device, "Forced to consider local data as UpToDate!\n");
648
649 /* Wait until nothing is on the fly :) */
650 wait_event(device->misc_wait, atomic_read(&device->ap_pending_cnt) == 0);
651
652 /* FIXME also wait for all pending P_BARRIER_ACK? */
653
654 if (new_role == R_SECONDARY) {
655 set_disk_ro(device->vdisk, true);
656 if (get_ldev(device)) {
657 device->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
658 put_ldev(device);
659 }
660 } else {
661 /* Called from drbd_adm_set_role only.
662 * We are still holding the conf_update mutex. */
663 nc = first_peer_device(device)->connection->net_conf;
664 if (nc)
665 nc->discard_my_data = 0; /* without copy; single bit op is atomic */
666
667 set_disk_ro(device->vdisk, false);
668 if (get_ldev(device)) {
669 if (((device->state.conn < C_CONNECTED ||
670 device->state.pdsk <= D_FAILED)
671 && device->ldev->md.uuid[UI_BITMAP] == 0) || forced)
672 drbd_uuid_new_current(device);
673
674 device->ldev->md.uuid[UI_CURRENT] |= (u64)1;
675 put_ldev(device);
676 }
677 }
678
679 /* writeout of activity log covered areas of the bitmap
680 * to stable storage done in after state change already */
681
682 if (device->state.conn >= C_WF_REPORT_PARAMS) {
683 /* if this was forced, we should consider sync */
684 if (forced)
685 drbd_send_uuids(first_peer_device(device));
686 drbd_send_current_state(first_peer_device(device));
687 }
688
689 drbd_md_sync(device);
690
691 kobject_uevent(&disk_to_dev(device->vdisk)->kobj, KOBJ_CHANGE);
692 out:
693 mutex_unlock(device->state_mutex);
694 return rv;
695 }
696
697 static const char *from_attrs_err_to_txt(int err)
698 {
699 return err == -ENOMSG ? "required attribute missing" :
700 err == -EOPNOTSUPP ? "unknown mandatory attribute" :
701 err == -EEXIST ? "can not change invariant setting" :
702 "invalid attribute value";
703 }
704
705 int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info)
706 {
707 struct drbd_config_context adm_ctx;
708 struct set_role_parms parms;
709 int err;
710 enum drbd_ret_code retcode;
711
712 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
713 if (!adm_ctx.reply_skb)
714 return retcode;
715 if (retcode != NO_ERROR)
716 goto out;
717
718 memset(&parms, 0, sizeof(parms));
719 if (info->attrs[DRBD_NLA_SET_ROLE_PARMS]) {
720 err = set_role_parms_from_attrs(&parms, info);
721 if (err) {
722 retcode = ERR_MANDATORY_TAG;
723 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
724 goto out;
725 }
726 }
727 genl_unlock();
728 mutex_lock(&adm_ctx.resource->adm_mutex);
729
730 if (info->genlhdr->cmd == DRBD_ADM_PRIMARY)
731 retcode = drbd_set_role(adm_ctx.device, R_PRIMARY, parms.assume_uptodate);
732 else
733 retcode = drbd_set_role(adm_ctx.device, R_SECONDARY, 0);
734
735 mutex_unlock(&adm_ctx.resource->adm_mutex);
736 genl_lock();
737 out:
738 drbd_adm_finish(&adm_ctx, info, retcode);
739 return 0;
740 }
741
742 /* Initializes the md.*_offset members, so we are able to find
743 * the on disk meta data.
744 *
745 * We currently have two possible layouts:
746 * external:
747 * |----------- md_size_sect ------------------|
748 * [ 4k superblock ][ activity log ][ Bitmap ]
749 * | al_offset == 8 |
750 * | bm_offset = al_offset + X |
751 * ==> bitmap sectors = md_size_sect - bm_offset
752 *
753 * internal:
754 * |----------- md_size_sect ------------------|
755 * [data.....][ Bitmap ][ activity log ][ 4k superblock ]
756 * | al_offset < 0 |
757 * | bm_offset = al_offset - Y |
758 * ==> bitmap sectors = Y = al_offset - bm_offset
759 *
760 * Activity log size used to be fixed 32kB,
761 * but is about to become configurable.
762 */
763 static void drbd_md_set_sector_offsets(struct drbd_device *device,
764 struct drbd_backing_dev *bdev)
765 {
766 sector_t md_size_sect = 0;
767 unsigned int al_size_sect = bdev->md.al_size_4k * 8;
768
769 bdev->md.md_offset = drbd_md_ss(bdev);
770
771 switch (bdev->md.meta_dev_idx) {
772 default:
773 /* v07 style fixed size indexed meta data */
774 bdev->md.md_size_sect = MD_128MB_SECT;
775 bdev->md.al_offset = MD_4kB_SECT;
776 bdev->md.bm_offset = MD_4kB_SECT + al_size_sect;
777 break;
778 case DRBD_MD_INDEX_FLEX_EXT:
779 /* just occupy the full device; unit: sectors */
780 bdev->md.md_size_sect = drbd_get_capacity(bdev->md_bdev);
781 bdev->md.al_offset = MD_4kB_SECT;
782 bdev->md.bm_offset = MD_4kB_SECT + al_size_sect;
783 break;
784 case DRBD_MD_INDEX_INTERNAL:
785 case DRBD_MD_INDEX_FLEX_INT:
786 /* al size is still fixed */
787 bdev->md.al_offset = -al_size_sect;
788 /* we need (slightly less than) ~ this much bitmap sectors: */
789 md_size_sect = drbd_get_capacity(bdev->backing_bdev);
790 md_size_sect = ALIGN(md_size_sect, BM_SECT_PER_EXT);
791 md_size_sect = BM_SECT_TO_EXT(md_size_sect);
792 md_size_sect = ALIGN(md_size_sect, 8);
793
794 /* plus the "drbd meta data super block",
795 * and the activity log; */
796 md_size_sect += MD_4kB_SECT + al_size_sect;
797
798 bdev->md.md_size_sect = md_size_sect;
799 /* bitmap offset is adjusted by 'super' block size */
800 bdev->md.bm_offset = -md_size_sect + MD_4kB_SECT;
801 break;
802 }
803 }
804
805 /* input size is expected to be in KB */
806 char *ppsize(char *buf, unsigned long long size)
807 {
808 /* Needs 9 bytes at max including trailing NUL:
809 * -1ULL ==> "16384 EB" */
810 static char units[] = { 'K', 'M', 'G', 'T', 'P', 'E' };
811 int base = 0;
812 while (size >= 10000 && base < sizeof(units)-1) {
813 /* shift + round */
814 size = (size >> 10) + !!(size & (1<<9));
815 base++;
816 }
817 sprintf(buf, "%u %cB", (unsigned)size, units[base]);
818
819 return buf;
820 }
821
822 /* there is still a theoretical deadlock when called from receiver
823 * on an D_INCONSISTENT R_PRIMARY:
824 * remote READ does inc_ap_bio, receiver would need to receive answer
825 * packet from remote to dec_ap_bio again.
826 * receiver receive_sizes(), comes here,
827 * waits for ap_bio_cnt == 0. -> deadlock.
828 * but this cannot happen, actually, because:
829 * R_PRIMARY D_INCONSISTENT, and peer's disk is unreachable
830 * (not connected, or bad/no disk on peer):
831 * see drbd_fail_request_early, ap_bio_cnt is zero.
832 * R_PRIMARY D_INCONSISTENT, and C_SYNC_TARGET:
833 * peer may not initiate a resize.
834 */
835 /* Note these are not to be confused with
836 * drbd_adm_suspend_io/drbd_adm_resume_io,
837 * which are (sub) state changes triggered by admin (drbdsetup),
838 * and can be long lived.
839 * This changes an device->flag, is triggered by drbd internals,
840 * and should be short-lived. */
841 void drbd_suspend_io(struct drbd_device *device)
842 {
843 set_bit(SUSPEND_IO, &device->flags);
844 if (drbd_suspended(device))
845 return;
846 wait_event(device->misc_wait, !atomic_read(&device->ap_bio_cnt));
847 }
848
849 void drbd_resume_io(struct drbd_device *device)
850 {
851 clear_bit(SUSPEND_IO, &device->flags);
852 wake_up(&device->misc_wait);
853 }
854
855 /**
856 * drbd_determine_dev_size() - Sets the right device size obeying all constraints
857 * @device: DRBD device.
858 *
859 * Returns 0 on success, negative return values indicate errors.
860 * You should call drbd_md_sync() after calling this function.
861 */
862 enum determine_dev_size
863 drbd_determine_dev_size(struct drbd_device *device, enum dds_flags flags, struct resize_parms *rs) __must_hold(local)
864 {
865 sector_t prev_first_sect, prev_size; /* previous meta location */
866 sector_t la_size_sect, u_size;
867 struct drbd_md *md = &device->ldev->md;
868 u32 prev_al_stripe_size_4k;
869 u32 prev_al_stripes;
870 sector_t size;
871 char ppb[10];
872 void *buffer;
873
874 int md_moved, la_size_changed;
875 enum determine_dev_size rv = DS_UNCHANGED;
876
877 /* race:
878 * application request passes inc_ap_bio,
879 * but then cannot get an AL-reference.
880 * this function later may wait on ap_bio_cnt == 0. -> deadlock.
881 *
882 * to avoid that:
883 * Suspend IO right here.
884 * still lock the act_log to not trigger ASSERTs there.
885 */
886 drbd_suspend_io(device);
887 buffer = drbd_md_get_buffer(device); /* Lock meta-data IO */
888 if (!buffer) {
889 drbd_resume_io(device);
890 return DS_ERROR;
891 }
892
893 /* no wait necessary anymore, actually we could assert that */
894 wait_event(device->al_wait, lc_try_lock(device->act_log));
895
896 prev_first_sect = drbd_md_first_sector(device->ldev);
897 prev_size = device->ldev->md.md_size_sect;
898 la_size_sect = device->ldev->md.la_size_sect;
899
900 if (rs) {
901 /* rs is non NULL if we should change the AL layout only */
902
903 prev_al_stripes = md->al_stripes;
904 prev_al_stripe_size_4k = md->al_stripe_size_4k;
905
906 md->al_stripes = rs->al_stripes;
907 md->al_stripe_size_4k = rs->al_stripe_size / 4;
908 md->al_size_4k = (u64)rs->al_stripes * rs->al_stripe_size / 4;
909 }
910
911 drbd_md_set_sector_offsets(device, device->ldev);
912
913 rcu_read_lock();
914 u_size = rcu_dereference(device->ldev->disk_conf)->disk_size;
915 rcu_read_unlock();
916 size = drbd_new_dev_size(device, device->ldev, u_size, flags & DDSF_FORCED);
917
918 if (size < la_size_sect) {
919 if (rs && u_size == 0) {
920 /* Remove "rs &&" later. This check should always be active, but
921 right now the receiver expects the permissive behavior */
922 drbd_warn(device, "Implicit shrink not allowed. "
923 "Use --size=%llus for explicit shrink.\n",
924 (unsigned long long)size);
925 rv = DS_ERROR_SHRINK;
926 }
927 if (u_size > size)
928 rv = DS_ERROR_SPACE_MD;
929 if (rv != DS_UNCHANGED)
930 goto err_out;
931 }
932
933 if (drbd_get_capacity(device->this_bdev) != size ||
934 drbd_bm_capacity(device) != size) {
935 int err;
936 err = drbd_bm_resize(device, size, !(flags & DDSF_NO_RESYNC));
937 if (unlikely(err)) {
938 /* currently there is only one error: ENOMEM! */
939 size = drbd_bm_capacity(device)>>1;
940 if (size == 0) {
941 drbd_err(device, "OUT OF MEMORY! "
942 "Could not allocate bitmap!\n");
943 } else {
944 drbd_err(device, "BM resizing failed. "
945 "Leaving size unchanged at size = %lu KB\n",
946 (unsigned long)size);
947 }
948 rv = DS_ERROR;
949 }
950 /* racy, see comments above. */
951 drbd_set_my_capacity(device, size);
952 device->ldev->md.la_size_sect = size;
953 drbd_info(device, "size = %s (%llu KB)\n", ppsize(ppb, size>>1),
954 (unsigned long long)size>>1);
955 }
956 if (rv <= DS_ERROR)
957 goto err_out;
958
959 la_size_changed = (la_size_sect != device->ldev->md.la_size_sect);
960
961 md_moved = prev_first_sect != drbd_md_first_sector(device->ldev)
962 || prev_size != device->ldev->md.md_size_sect;
963
964 if (la_size_changed || md_moved || rs) {
965 u32 prev_flags;
966
967 drbd_al_shrink(device); /* All extents inactive. */
968
969 prev_flags = md->flags;
970 md->flags &= ~MDF_PRIMARY_IND;
971 drbd_md_write(device, buffer);
972
973 drbd_info(device, "Writing the whole bitmap, %s\n",
974 la_size_changed && md_moved ? "size changed and md moved" :
975 la_size_changed ? "size changed" : "md moved");
976 /* next line implicitly does drbd_suspend_io()+drbd_resume_io() */
977 drbd_bitmap_io(device, md_moved ? &drbd_bm_write_all : &drbd_bm_write,
978 "size changed", BM_LOCKED_MASK);
979 drbd_initialize_al(device, buffer);
980
981 md->flags = prev_flags;
982 drbd_md_write(device, buffer);
983
984 if (rs)
985 drbd_info(device, "Changed AL layout to al-stripes = %d, al-stripe-size-kB = %d\n",
986 md->al_stripes, md->al_stripe_size_4k * 4);
987 }
988
989 if (size > la_size_sect)
990 rv = la_size_sect ? DS_GREW : DS_GREW_FROM_ZERO;
991 if (size < la_size_sect)
992 rv = DS_SHRUNK;
993
994 if (0) {
995 err_out:
996 if (rs) {
997 md->al_stripes = prev_al_stripes;
998 md->al_stripe_size_4k = prev_al_stripe_size_4k;
999 md->al_size_4k = (u64)prev_al_stripes * prev_al_stripe_size_4k;
1000
1001 drbd_md_set_sector_offsets(device, device->ldev);
1002 }
1003 }
1004 lc_unlock(device->act_log);
1005 wake_up(&device->al_wait);
1006 drbd_md_put_buffer(device);
1007 drbd_resume_io(device);
1008
1009 return rv;
1010 }
1011
1012 sector_t
1013 drbd_new_dev_size(struct drbd_device *device, struct drbd_backing_dev *bdev,
1014 sector_t u_size, int assume_peer_has_space)
1015 {
1016 sector_t p_size = device->p_size; /* partner's disk size. */
1017 sector_t la_size_sect = bdev->md.la_size_sect; /* last agreed size. */
1018 sector_t m_size; /* my size */
1019 sector_t size = 0;
1020
1021 m_size = drbd_get_max_capacity(bdev);
1022
1023 if (device->state.conn < C_CONNECTED && assume_peer_has_space) {
1024 drbd_warn(device, "Resize while not connected was forced by the user!\n");
1025 p_size = m_size;
1026 }
1027
1028 if (p_size && m_size) {
1029 size = min_t(sector_t, p_size, m_size);
1030 } else {
1031 if (la_size_sect) {
1032 size = la_size_sect;
1033 if (m_size && m_size < size)
1034 size = m_size;
1035 if (p_size && p_size < size)
1036 size = p_size;
1037 } else {
1038 if (m_size)
1039 size = m_size;
1040 if (p_size)
1041 size = p_size;
1042 }
1043 }
1044
1045 if (size == 0)
1046 drbd_err(device, "Both nodes diskless!\n");
1047
1048 if (u_size) {
1049 if (u_size > size)
1050 drbd_err(device, "Requested disk size is too big (%lu > %lu)\n",
1051 (unsigned long)u_size>>1, (unsigned long)size>>1);
1052 else
1053 size = u_size;
1054 }
1055
1056 return size;
1057 }
1058
1059 /**
1060 * drbd_check_al_size() - Ensures that the AL is of the right size
1061 * @device: DRBD device.
1062 *
1063 * Returns -EBUSY if current al lru is still used, -ENOMEM when allocation
1064 * failed, and 0 on success. You should call drbd_md_sync() after you called
1065 * this function.
1066 */
1067 static int drbd_check_al_size(struct drbd_device *device, struct disk_conf *dc)
1068 {
1069 struct lru_cache *n, *t;
1070 struct lc_element *e;
1071 unsigned int in_use;
1072 int i;
1073
1074 if (device->act_log &&
1075 device->act_log->nr_elements == dc->al_extents)
1076 return 0;
1077
1078 in_use = 0;
1079 t = device->act_log;
1080 n = lc_create("act_log", drbd_al_ext_cache, AL_UPDATES_PER_TRANSACTION,
1081 dc->al_extents, sizeof(struct lc_element), 0);
1082
1083 if (n == NULL) {
1084 drbd_err(device, "Cannot allocate act_log lru!\n");
1085 return -ENOMEM;
1086 }
1087 spin_lock_irq(&device->al_lock);
1088 if (t) {
1089 for (i = 0; i < t->nr_elements; i++) {
1090 e = lc_element_by_index(t, i);
1091 if (e->refcnt)
1092 drbd_err(device, "refcnt(%d)==%d\n",
1093 e->lc_number, e->refcnt);
1094 in_use += e->refcnt;
1095 }
1096 }
1097 if (!in_use)
1098 device->act_log = n;
1099 spin_unlock_irq(&device->al_lock);
1100 if (in_use) {
1101 drbd_err(device, "Activity log still in use!\n");
1102 lc_destroy(n);
1103 return -EBUSY;
1104 } else {
1105 if (t)
1106 lc_destroy(t);
1107 }
1108 drbd_md_mark_dirty(device); /* we changed device->act_log->nr_elemens */
1109 return 0;
1110 }
1111
1112 static void drbd_setup_queue_param(struct drbd_device *device, unsigned int max_bio_size)
1113 {
1114 struct request_queue * const q = device->rq_queue;
1115 unsigned int max_hw_sectors = max_bio_size >> 9;
1116 unsigned int max_segments = 0;
1117
1118 if (get_ldev_if_state(device, D_ATTACHING)) {
1119 struct request_queue * const b = device->ldev->backing_bdev->bd_disk->queue;
1120
1121 max_hw_sectors = min(queue_max_hw_sectors(b), max_bio_size >> 9);
1122 rcu_read_lock();
1123 max_segments = rcu_dereference(device->ldev->disk_conf)->max_bio_bvecs;
1124 rcu_read_unlock();
1125 put_ldev(device);
1126 }
1127
1128 blk_queue_logical_block_size(q, 512);
1129 blk_queue_max_hw_sectors(q, max_hw_sectors);
1130 /* This is the workaround for "bio would need to, but cannot, be split" */
1131 blk_queue_max_segments(q, max_segments ? max_segments : BLK_MAX_SEGMENTS);
1132 blk_queue_segment_boundary(q, PAGE_CACHE_SIZE-1);
1133
1134 if (get_ldev_if_state(device, D_ATTACHING)) {
1135 struct request_queue * const b = device->ldev->backing_bdev->bd_disk->queue;
1136
1137 blk_queue_stack_limits(q, b);
1138
1139 if (q->backing_dev_info.ra_pages != b->backing_dev_info.ra_pages) {
1140 drbd_info(device, "Adjusting my ra_pages to backing device's (%lu -> %lu)\n",
1141 q->backing_dev_info.ra_pages,
1142 b->backing_dev_info.ra_pages);
1143 q->backing_dev_info.ra_pages = b->backing_dev_info.ra_pages;
1144 }
1145 put_ldev(device);
1146 }
1147 }
1148
1149 void drbd_reconsider_max_bio_size(struct drbd_device *device)
1150 {
1151 unsigned int now, new, local, peer;
1152
1153 now = queue_max_hw_sectors(device->rq_queue) << 9;
1154 local = device->local_max_bio_size; /* Eventually last known value, from volatile memory */
1155 peer = device->peer_max_bio_size; /* Eventually last known value, from meta data */
1156
1157 if (get_ldev_if_state(device, D_ATTACHING)) {
1158 local = queue_max_hw_sectors(device->ldev->backing_bdev->bd_disk->queue) << 9;
1159 device->local_max_bio_size = local;
1160 put_ldev(device);
1161 }
1162 local = min(local, DRBD_MAX_BIO_SIZE);
1163
1164 /* We may ignore peer limits if the peer is modern enough.
1165 Because new from 8.3.8 onwards the peer can use multiple
1166 BIOs for a single peer_request */
1167 if (device->state.conn >= C_WF_REPORT_PARAMS) {
1168 if (first_peer_device(device)->connection->agreed_pro_version < 94)
1169 peer = min(device->peer_max_bio_size, DRBD_MAX_SIZE_H80_PACKET);
1170 /* Correct old drbd (up to 8.3.7) if it believes it can do more than 32KiB */
1171 else if (first_peer_device(device)->connection->agreed_pro_version == 94)
1172 peer = DRBD_MAX_SIZE_H80_PACKET;
1173 else if (first_peer_device(device)->connection->agreed_pro_version < 100)
1174 peer = DRBD_MAX_BIO_SIZE_P95; /* drbd 8.3.8 onwards, before 8.4.0 */
1175 else
1176 peer = DRBD_MAX_BIO_SIZE;
1177 }
1178
1179 new = min(local, peer);
1180
1181 if (device->state.role == R_PRIMARY && new < now)
1182 drbd_err(device, "ASSERT FAILED new < now; (%u < %u)\n", new, now);
1183
1184 if (new != now)
1185 drbd_info(device, "max BIO size = %u\n", new);
1186
1187 drbd_setup_queue_param(device, new);
1188 }
1189
1190 /* Starts the worker thread */
1191 static void conn_reconfig_start(struct drbd_connection *connection)
1192 {
1193 drbd_thread_start(&connection->worker);
1194 drbd_flush_workqueue(&connection->sender_work);
1195 }
1196
1197 /* if still unconfigured, stops worker again. */
1198 static void conn_reconfig_done(struct drbd_connection *connection)
1199 {
1200 bool stop_threads;
1201 spin_lock_irq(&connection->resource->req_lock);
1202 stop_threads = conn_all_vols_unconf(connection) &&
1203 connection->cstate == C_STANDALONE;
1204 spin_unlock_irq(&connection->resource->req_lock);
1205 if (stop_threads) {
1206 /* asender is implicitly stopped by receiver
1207 * in conn_disconnect() */
1208 drbd_thread_stop(&connection->receiver);
1209 drbd_thread_stop(&connection->worker);
1210 }
1211 }
1212
1213 /* Make sure IO is suspended before calling this function(). */
1214 static void drbd_suspend_al(struct drbd_device *device)
1215 {
1216 int s = 0;
1217
1218 if (!lc_try_lock(device->act_log)) {
1219 drbd_warn(device, "Failed to lock al in drbd_suspend_al()\n");
1220 return;
1221 }
1222
1223 drbd_al_shrink(device);
1224 spin_lock_irq(&device->resource->req_lock);
1225 if (device->state.conn < C_CONNECTED)
1226 s = !test_and_set_bit(AL_SUSPENDED, &device->flags);
1227 spin_unlock_irq(&device->resource->req_lock);
1228 lc_unlock(device->act_log);
1229
1230 if (s)
1231 drbd_info(device, "Suspended AL updates\n");
1232 }
1233
1234
1235 static bool should_set_defaults(struct genl_info *info)
1236 {
1237 unsigned flags = ((struct drbd_genlmsghdr*)info->userhdr)->flags;
1238 return 0 != (flags & DRBD_GENL_F_SET_DEFAULTS);
1239 }
1240
1241 static unsigned int drbd_al_extents_max(struct drbd_backing_dev *bdev)
1242 {
1243 /* This is limited by 16 bit "slot" numbers,
1244 * and by available on-disk context storage.
1245 *
1246 * Also (u16)~0 is special (denotes a "free" extent).
1247 *
1248 * One transaction occupies one 4kB on-disk block,
1249 * we have n such blocks in the on disk ring buffer,
1250 * the "current" transaction may fail (n-1),
1251 * and there is 919 slot numbers context information per transaction.
1252 *
1253 * 72 transaction blocks amounts to more than 2**16 context slots,
1254 * so cap there first.
1255 */
1256 const unsigned int max_al_nr = DRBD_AL_EXTENTS_MAX;
1257 const unsigned int sufficient_on_disk =
1258 (max_al_nr + AL_CONTEXT_PER_TRANSACTION -1)
1259 /AL_CONTEXT_PER_TRANSACTION;
1260
1261 unsigned int al_size_4k = bdev->md.al_size_4k;
1262
1263 if (al_size_4k > sufficient_on_disk)
1264 return max_al_nr;
1265
1266 return (al_size_4k - 1) * AL_CONTEXT_PER_TRANSACTION;
1267 }
1268
1269 int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info)
1270 {
1271 struct drbd_config_context adm_ctx;
1272 enum drbd_ret_code retcode;
1273 struct drbd_device *device;
1274 struct disk_conf *new_disk_conf, *old_disk_conf;
1275 struct fifo_buffer *old_plan = NULL, *new_plan = NULL;
1276 int err, fifo_size;
1277
1278 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
1279 if (!adm_ctx.reply_skb)
1280 return retcode;
1281 if (retcode != NO_ERROR)
1282 goto finish;
1283
1284 device = adm_ctx.device;
1285 mutex_lock(&adm_ctx.resource->adm_mutex);
1286
1287 /* we also need a disk
1288 * to change the options on */
1289 if (!get_ldev(device)) {
1290 retcode = ERR_NO_DISK;
1291 goto out;
1292 }
1293
1294 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
1295 if (!new_disk_conf) {
1296 retcode = ERR_NOMEM;
1297 goto fail;
1298 }
1299
1300 mutex_lock(&device->resource->conf_update);
1301 old_disk_conf = device->ldev->disk_conf;
1302 *new_disk_conf = *old_disk_conf;
1303 if (should_set_defaults(info))
1304 set_disk_conf_defaults(new_disk_conf);
1305
1306 err = disk_conf_from_attrs_for_change(new_disk_conf, info);
1307 if (err && err != -ENOMSG) {
1308 retcode = ERR_MANDATORY_TAG;
1309 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
1310 goto fail_unlock;
1311 }
1312
1313 if (!expect(new_disk_conf->resync_rate >= 1))
1314 new_disk_conf->resync_rate = 1;
1315
1316 if (new_disk_conf->al_extents < DRBD_AL_EXTENTS_MIN)
1317 new_disk_conf->al_extents = DRBD_AL_EXTENTS_MIN;
1318 if (new_disk_conf->al_extents > drbd_al_extents_max(device->ldev))
1319 new_disk_conf->al_extents = drbd_al_extents_max(device->ldev);
1320
1321 if (new_disk_conf->c_plan_ahead > DRBD_C_PLAN_AHEAD_MAX)
1322 new_disk_conf->c_plan_ahead = DRBD_C_PLAN_AHEAD_MAX;
1323
1324 fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
1325 if (fifo_size != device->rs_plan_s->size) {
1326 new_plan = fifo_alloc(fifo_size);
1327 if (!new_plan) {
1328 drbd_err(device, "kmalloc of fifo_buffer failed");
1329 retcode = ERR_NOMEM;
1330 goto fail_unlock;
1331 }
1332 }
1333
1334 drbd_suspend_io(device);
1335 wait_event(device->al_wait, lc_try_lock(device->act_log));
1336 drbd_al_shrink(device);
1337 err = drbd_check_al_size(device, new_disk_conf);
1338 lc_unlock(device->act_log);
1339 wake_up(&device->al_wait);
1340 drbd_resume_io(device);
1341
1342 if (err) {
1343 retcode = ERR_NOMEM;
1344 goto fail_unlock;
1345 }
1346
1347 write_lock_irq(&global_state_lock);
1348 retcode = drbd_resync_after_valid(device, new_disk_conf->resync_after);
1349 if (retcode == NO_ERROR) {
1350 rcu_assign_pointer(device->ldev->disk_conf, new_disk_conf);
1351 drbd_resync_after_changed(device);
1352 }
1353 write_unlock_irq(&global_state_lock);
1354
1355 if (retcode != NO_ERROR)
1356 goto fail_unlock;
1357
1358 if (new_plan) {
1359 old_plan = device->rs_plan_s;
1360 rcu_assign_pointer(device->rs_plan_s, new_plan);
1361 }
1362
1363 mutex_unlock(&device->resource->conf_update);
1364
1365 if (new_disk_conf->al_updates)
1366 device->ldev->md.flags &= ~MDF_AL_DISABLED;
1367 else
1368 device->ldev->md.flags |= MDF_AL_DISABLED;
1369
1370 if (new_disk_conf->md_flushes)
1371 clear_bit(MD_NO_FUA, &device->flags);
1372 else
1373 set_bit(MD_NO_FUA, &device->flags);
1374
1375 drbd_bump_write_ordering(first_peer_device(device)->connection, WO_bdev_flush);
1376
1377 drbd_md_sync(device);
1378
1379 if (device->state.conn >= C_CONNECTED) {
1380 struct drbd_peer_device *peer_device;
1381
1382 for_each_peer_device(peer_device, device)
1383 drbd_send_sync_param(peer_device);
1384 }
1385
1386 synchronize_rcu();
1387 kfree(old_disk_conf);
1388 kfree(old_plan);
1389 mod_timer(&device->request_timer, jiffies + HZ);
1390 goto success;
1391
1392 fail_unlock:
1393 mutex_unlock(&device->resource->conf_update);
1394 fail:
1395 kfree(new_disk_conf);
1396 kfree(new_plan);
1397 success:
1398 put_ldev(device);
1399 out:
1400 mutex_unlock(&adm_ctx.resource->adm_mutex);
1401 finish:
1402 drbd_adm_finish(&adm_ctx, info, retcode);
1403 return 0;
1404 }
1405
1406 int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info)
1407 {
1408 struct drbd_config_context adm_ctx;
1409 struct drbd_device *device;
1410 int err;
1411 enum drbd_ret_code retcode;
1412 enum determine_dev_size dd;
1413 sector_t max_possible_sectors;
1414 sector_t min_md_device_sectors;
1415 struct drbd_backing_dev *nbc = NULL; /* new_backing_conf */
1416 struct disk_conf *new_disk_conf = NULL;
1417 struct block_device *bdev;
1418 struct lru_cache *resync_lru = NULL;
1419 struct fifo_buffer *new_plan = NULL;
1420 union drbd_state ns, os;
1421 enum drbd_state_rv rv;
1422 struct net_conf *nc;
1423
1424 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
1425 if (!adm_ctx.reply_skb)
1426 return retcode;
1427 if (retcode != NO_ERROR)
1428 goto finish;
1429
1430 device = adm_ctx.device;
1431 mutex_lock(&adm_ctx.resource->adm_mutex);
1432 conn_reconfig_start(first_peer_device(device)->connection);
1433
1434 /* if you want to reconfigure, please tear down first */
1435 if (device->state.disk > D_DISKLESS) {
1436 retcode = ERR_DISK_CONFIGURED;
1437 goto fail;
1438 }
1439 /* It may just now have detached because of IO error. Make sure
1440 * drbd_ldev_destroy is done already, we may end up here very fast,
1441 * e.g. if someone calls attach from the on-io-error handler,
1442 * to realize a "hot spare" feature (not that I'd recommend that) */
1443 wait_event(device->misc_wait, !atomic_read(&device->local_cnt));
1444
1445 /* make sure there is no leftover from previous force-detach attempts */
1446 clear_bit(FORCE_DETACH, &device->flags);
1447 clear_bit(WAS_IO_ERROR, &device->flags);
1448 clear_bit(WAS_READ_ERROR, &device->flags);
1449
1450 /* and no leftover from previously aborted resync or verify, either */
1451 device->rs_total = 0;
1452 device->rs_failed = 0;
1453 atomic_set(&device->rs_pending_cnt, 0);
1454
1455 /* allocation not in the IO path, drbdsetup context */
1456 nbc = kzalloc(sizeof(struct drbd_backing_dev), GFP_KERNEL);
1457 if (!nbc) {
1458 retcode = ERR_NOMEM;
1459 goto fail;
1460 }
1461 spin_lock_init(&nbc->md.uuid_lock);
1462
1463 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
1464 if (!new_disk_conf) {
1465 retcode = ERR_NOMEM;
1466 goto fail;
1467 }
1468 nbc->disk_conf = new_disk_conf;
1469
1470 set_disk_conf_defaults(new_disk_conf);
1471 err = disk_conf_from_attrs(new_disk_conf, info);
1472 if (err) {
1473 retcode = ERR_MANDATORY_TAG;
1474 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
1475 goto fail;
1476 }
1477
1478 if (new_disk_conf->c_plan_ahead > DRBD_C_PLAN_AHEAD_MAX)
1479 new_disk_conf->c_plan_ahead = DRBD_C_PLAN_AHEAD_MAX;
1480
1481 new_plan = fifo_alloc((new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ);
1482 if (!new_plan) {
1483 retcode = ERR_NOMEM;
1484 goto fail;
1485 }
1486
1487 if (new_disk_conf->meta_dev_idx < DRBD_MD_INDEX_FLEX_INT) {
1488 retcode = ERR_MD_IDX_INVALID;
1489 goto fail;
1490 }
1491
1492 write_lock_irq(&global_state_lock);
1493 retcode = drbd_resync_after_valid(device, new_disk_conf->resync_after);
1494 write_unlock_irq(&global_state_lock);
1495 if (retcode != NO_ERROR)
1496 goto fail;
1497
1498 rcu_read_lock();
1499 nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
1500 if (nc) {
1501 if (new_disk_conf->fencing == FP_STONITH && nc->wire_protocol == DRBD_PROT_A) {
1502 rcu_read_unlock();
1503 retcode = ERR_STONITH_AND_PROT_A;
1504 goto fail;
1505 }
1506 }
1507 rcu_read_unlock();
1508
1509 bdev = blkdev_get_by_path(new_disk_conf->backing_dev,
1510 FMODE_READ | FMODE_WRITE | FMODE_EXCL, device);
1511 if (IS_ERR(bdev)) {
1512 drbd_err(device, "open(\"%s\") failed with %ld\n", new_disk_conf->backing_dev,
1513 PTR_ERR(bdev));
1514 retcode = ERR_OPEN_DISK;
1515 goto fail;
1516 }
1517 nbc->backing_bdev = bdev;
1518
1519 /*
1520 * meta_dev_idx >= 0: external fixed size, possibly multiple
1521 * drbd sharing one meta device. TODO in that case, paranoia
1522 * check that [md_bdev, meta_dev_idx] is not yet used by some
1523 * other drbd minor! (if you use drbd.conf + drbdadm, that
1524 * should check it for you already; but if you don't, or
1525 * someone fooled it, we need to double check here)
1526 */
1527 bdev = blkdev_get_by_path(new_disk_conf->meta_dev,
1528 FMODE_READ | FMODE_WRITE | FMODE_EXCL,
1529 (new_disk_conf->meta_dev_idx < 0) ?
1530 (void *)device : (void *)drbd_m_holder);
1531 if (IS_ERR(bdev)) {
1532 drbd_err(device, "open(\"%s\") failed with %ld\n", new_disk_conf->meta_dev,
1533 PTR_ERR(bdev));
1534 retcode = ERR_OPEN_MD_DISK;
1535 goto fail;
1536 }
1537 nbc->md_bdev = bdev;
1538
1539 if ((nbc->backing_bdev == nbc->md_bdev) !=
1540 (new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_INTERNAL ||
1541 new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_FLEX_INT)) {
1542 retcode = ERR_MD_IDX_INVALID;
1543 goto fail;
1544 }
1545
1546 resync_lru = lc_create("resync", drbd_bm_ext_cache,
1547 1, 61, sizeof(struct bm_extent),
1548 offsetof(struct bm_extent, lce));
1549 if (!resync_lru) {
1550 retcode = ERR_NOMEM;
1551 goto fail;
1552 }
1553
1554 /* Read our meta data super block early.
1555 * This also sets other on-disk offsets. */
1556 retcode = drbd_md_read(device, nbc);
1557 if (retcode != NO_ERROR)
1558 goto fail;
1559
1560 if (new_disk_conf->al_extents < DRBD_AL_EXTENTS_MIN)
1561 new_disk_conf->al_extents = DRBD_AL_EXTENTS_MIN;
1562 if (new_disk_conf->al_extents > drbd_al_extents_max(nbc))
1563 new_disk_conf->al_extents = drbd_al_extents_max(nbc);
1564
1565 if (drbd_get_max_capacity(nbc) < new_disk_conf->disk_size) {
1566 drbd_err(device, "max capacity %llu smaller than disk size %llu\n",
1567 (unsigned long long) drbd_get_max_capacity(nbc),
1568 (unsigned long long) new_disk_conf->disk_size);
1569 retcode = ERR_DISK_TOO_SMALL;
1570 goto fail;
1571 }
1572
1573 if (new_disk_conf->meta_dev_idx < 0) {
1574 max_possible_sectors = DRBD_MAX_SECTORS_FLEX;
1575 /* at least one MB, otherwise it does not make sense */
1576 min_md_device_sectors = (2<<10);
1577 } else {
1578 max_possible_sectors = DRBD_MAX_SECTORS;
1579 min_md_device_sectors = MD_128MB_SECT * (new_disk_conf->meta_dev_idx + 1);
1580 }
1581
1582 if (drbd_get_capacity(nbc->md_bdev) < min_md_device_sectors) {
1583 retcode = ERR_MD_DISK_TOO_SMALL;
1584 drbd_warn(device, "refusing attach: md-device too small, "
1585 "at least %llu sectors needed for this meta-disk type\n",
1586 (unsigned long long) min_md_device_sectors);
1587 goto fail;
1588 }
1589
1590 /* Make sure the new disk is big enough
1591 * (we may currently be R_PRIMARY with no local disk...) */
1592 if (drbd_get_max_capacity(nbc) <
1593 drbd_get_capacity(device->this_bdev)) {
1594 retcode = ERR_DISK_TOO_SMALL;
1595 goto fail;
1596 }
1597
1598 nbc->known_size = drbd_get_capacity(nbc->backing_bdev);
1599
1600 if (nbc->known_size > max_possible_sectors) {
1601 drbd_warn(device, "==> truncating very big lower level device "
1602 "to currently maximum possible %llu sectors <==\n",
1603 (unsigned long long) max_possible_sectors);
1604 if (new_disk_conf->meta_dev_idx >= 0)
1605 drbd_warn(device, "==>> using internal or flexible "
1606 "meta data may help <<==\n");
1607 }
1608
1609 drbd_suspend_io(device);
1610 /* also wait for the last barrier ack. */
1611 /* FIXME see also https://daiquiri.linbit/cgi-bin/bugzilla/show_bug.cgi?id=171
1612 * We need a way to either ignore barrier acks for barriers sent before a device
1613 * was attached, or a way to wait for all pending barrier acks to come in.
1614 * As barriers are counted per resource,
1615 * we'd need to suspend io on all devices of a resource.
1616 */
1617 wait_event(device->misc_wait, !atomic_read(&device->ap_pending_cnt) || drbd_suspended(device));
1618 /* and for any other previously queued work */
1619 drbd_flush_workqueue(&first_peer_device(device)->connection->sender_work);
1620
1621 rv = _drbd_request_state(device, NS(disk, D_ATTACHING), CS_VERBOSE);
1622 retcode = rv; /* FIXME: Type mismatch. */
1623 drbd_resume_io(device);
1624 if (rv < SS_SUCCESS)
1625 goto fail;
1626
1627 if (!get_ldev_if_state(device, D_ATTACHING))
1628 goto force_diskless;
1629
1630 if (!device->bitmap) {
1631 if (drbd_bm_init(device)) {
1632 retcode = ERR_NOMEM;
1633 goto force_diskless_dec;
1634 }
1635 }
1636
1637 if (device->state.conn < C_CONNECTED &&
1638 device->state.role == R_PRIMARY &&
1639 (device->ed_uuid & ~((u64)1)) != (nbc->md.uuid[UI_CURRENT] & ~((u64)1))) {
1640 drbd_err(device, "Can only attach to data with current UUID=%016llX\n",
1641 (unsigned long long)device->ed_uuid);
1642 retcode = ERR_DATA_NOT_CURRENT;
1643 goto force_diskless_dec;
1644 }
1645
1646 /* Since we are diskless, fix the activity log first... */
1647 if (drbd_check_al_size(device, new_disk_conf)) {
1648 retcode = ERR_NOMEM;
1649 goto force_diskless_dec;
1650 }
1651
1652 /* Prevent shrinking of consistent devices ! */
1653 if (drbd_md_test_flag(nbc, MDF_CONSISTENT) &&
1654 drbd_new_dev_size(device, nbc, nbc->disk_conf->disk_size, 0) < nbc->md.la_size_sect) {
1655 drbd_warn(device, "refusing to truncate a consistent device\n");
1656 retcode = ERR_DISK_TOO_SMALL;
1657 goto force_diskless_dec;
1658 }
1659
1660 /* Reset the "barriers don't work" bits here, then force meta data to
1661 * be written, to ensure we determine if barriers are supported. */
1662 if (new_disk_conf->md_flushes)
1663 clear_bit(MD_NO_FUA, &device->flags);
1664 else
1665 set_bit(MD_NO_FUA, &device->flags);
1666
1667 /* Point of no return reached.
1668 * Devices and memory are no longer released by error cleanup below.
1669 * now device takes over responsibility, and the state engine should
1670 * clean it up somewhere. */
1671 D_ASSERT(device, device->ldev == NULL);
1672 device->ldev = nbc;
1673 device->resync = resync_lru;
1674 device->rs_plan_s = new_plan;
1675 nbc = NULL;
1676 resync_lru = NULL;
1677 new_disk_conf = NULL;
1678 new_plan = NULL;
1679
1680 drbd_bump_write_ordering(first_peer_device(device)->connection, WO_bdev_flush);
1681
1682 if (drbd_md_test_flag(device->ldev, MDF_CRASHED_PRIMARY))
1683 set_bit(CRASHED_PRIMARY, &device->flags);
1684 else
1685 clear_bit(CRASHED_PRIMARY, &device->flags);
1686
1687 if (drbd_md_test_flag(device->ldev, MDF_PRIMARY_IND) &&
1688 !(device->state.role == R_PRIMARY && device->resource->susp_nod))
1689 set_bit(CRASHED_PRIMARY, &device->flags);
1690
1691 device->send_cnt = 0;
1692 device->recv_cnt = 0;
1693 device->read_cnt = 0;
1694 device->writ_cnt = 0;
1695
1696 drbd_reconsider_max_bio_size(device);
1697
1698 /* If I am currently not R_PRIMARY,
1699 * but meta data primary indicator is set,
1700 * I just now recover from a hard crash,
1701 * and have been R_PRIMARY before that crash.
1702 *
1703 * Now, if I had no connection before that crash
1704 * (have been degraded R_PRIMARY), chances are that
1705 * I won't find my peer now either.
1706 *
1707 * In that case, and _only_ in that case,
1708 * we use the degr-wfc-timeout instead of the default,
1709 * so we can automatically recover from a crash of a
1710 * degraded but active "cluster" after a certain timeout.
1711 */
1712 clear_bit(USE_DEGR_WFC_T, &device->flags);
1713 if (device->state.role != R_PRIMARY &&
1714 drbd_md_test_flag(device->ldev, MDF_PRIMARY_IND) &&
1715 !drbd_md_test_flag(device->ldev, MDF_CONNECTED_IND))
1716 set_bit(USE_DEGR_WFC_T, &device->flags);
1717
1718 dd = drbd_determine_dev_size(device, 0, NULL);
1719 if (dd <= DS_ERROR) {
1720 retcode = ERR_NOMEM_BITMAP;
1721 goto force_diskless_dec;
1722 } else if (dd == DS_GREW)
1723 set_bit(RESYNC_AFTER_NEG, &device->flags);
1724
1725 if (drbd_md_test_flag(device->ldev, MDF_FULL_SYNC) ||
1726 (test_bit(CRASHED_PRIMARY, &device->flags) &&
1727 drbd_md_test_flag(device->ldev, MDF_AL_DISABLED))) {
1728 drbd_info(device, "Assuming that all blocks are out of sync "
1729 "(aka FullSync)\n");
1730 if (drbd_bitmap_io(device, &drbd_bmio_set_n_write,
1731 "set_n_write from attaching", BM_LOCKED_MASK)) {
1732 retcode = ERR_IO_MD_DISK;
1733 goto force_diskless_dec;
1734 }
1735 } else {
1736 if (drbd_bitmap_io(device, &drbd_bm_read,
1737 "read from attaching", BM_LOCKED_MASK)) {
1738 retcode = ERR_IO_MD_DISK;
1739 goto force_diskless_dec;
1740 }
1741 }
1742
1743 if (_drbd_bm_total_weight(device) == drbd_bm_bits(device))
1744 drbd_suspend_al(device); /* IO is still suspended here... */
1745
1746 spin_lock_irq(&device->resource->req_lock);
1747 os = drbd_read_state(device);
1748 ns = os;
1749 /* If MDF_CONSISTENT is not set go into inconsistent state,
1750 otherwise investigate MDF_WasUpToDate...
1751 If MDF_WAS_UP_TO_DATE is not set go into D_OUTDATED disk state,
1752 otherwise into D_CONSISTENT state.
1753 */
1754 if (drbd_md_test_flag(device->ldev, MDF_CONSISTENT)) {
1755 if (drbd_md_test_flag(device->ldev, MDF_WAS_UP_TO_DATE))
1756 ns.disk = D_CONSISTENT;
1757 else
1758 ns.disk = D_OUTDATED;
1759 } else {
1760 ns.disk = D_INCONSISTENT;
1761 }
1762
1763 if (drbd_md_test_flag(device->ldev, MDF_PEER_OUT_DATED))
1764 ns.pdsk = D_OUTDATED;
1765
1766 rcu_read_lock();
1767 if (ns.disk == D_CONSISTENT &&
1768 (ns.pdsk == D_OUTDATED || rcu_dereference(device->ldev->disk_conf)->fencing == FP_DONT_CARE))
1769 ns.disk = D_UP_TO_DATE;
1770
1771 /* All tests on MDF_PRIMARY_IND, MDF_CONNECTED_IND,
1772 MDF_CONSISTENT and MDF_WAS_UP_TO_DATE must happen before
1773 this point, because drbd_request_state() modifies these
1774 flags. */
1775
1776 if (rcu_dereference(device->ldev->disk_conf)->al_updates)
1777 device->ldev->md.flags &= ~MDF_AL_DISABLED;
1778 else
1779 device->ldev->md.flags |= MDF_AL_DISABLED;
1780
1781 rcu_read_unlock();
1782
1783 /* In case we are C_CONNECTED postpone any decision on the new disk
1784 state after the negotiation phase. */
1785 if (device->state.conn == C_CONNECTED) {
1786 device->new_state_tmp.i = ns.i;
1787 ns.i = os.i;
1788 ns.disk = D_NEGOTIATING;
1789
1790 /* We expect to receive up-to-date UUIDs soon.
1791 To avoid a race in receive_state, free p_uuid while
1792 holding req_lock. I.e. atomic with the state change */
1793 kfree(device->p_uuid);
1794 device->p_uuid = NULL;
1795 }
1796
1797 rv = _drbd_set_state(device, ns, CS_VERBOSE, NULL);
1798 spin_unlock_irq(&device->resource->req_lock);
1799
1800 if (rv < SS_SUCCESS)
1801 goto force_diskless_dec;
1802
1803 mod_timer(&device->request_timer, jiffies + HZ);
1804
1805 if (device->state.role == R_PRIMARY)
1806 device->ldev->md.uuid[UI_CURRENT] |= (u64)1;
1807 else
1808 device->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
1809
1810 drbd_md_mark_dirty(device);
1811 drbd_md_sync(device);
1812
1813 kobject_uevent(&disk_to_dev(device->vdisk)->kobj, KOBJ_CHANGE);
1814 put_ldev(device);
1815 conn_reconfig_done(first_peer_device(device)->connection);
1816 mutex_unlock(&adm_ctx.resource->adm_mutex);
1817 drbd_adm_finish(&adm_ctx, info, retcode);
1818 return 0;
1819
1820 force_diskless_dec:
1821 put_ldev(device);
1822 force_diskless:
1823 drbd_force_state(device, NS(disk, D_DISKLESS));
1824 drbd_md_sync(device);
1825 fail:
1826 conn_reconfig_done(first_peer_device(device)->connection);
1827 if (nbc) {
1828 if (nbc->backing_bdev)
1829 blkdev_put(nbc->backing_bdev,
1830 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1831 if (nbc->md_bdev)
1832 blkdev_put(nbc->md_bdev,
1833 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1834 kfree(nbc);
1835 }
1836 kfree(new_disk_conf);
1837 lc_destroy(resync_lru);
1838 kfree(new_plan);
1839 mutex_unlock(&adm_ctx.resource->adm_mutex);
1840 finish:
1841 drbd_adm_finish(&adm_ctx, info, retcode);
1842 return 0;
1843 }
1844
1845 static int adm_detach(struct drbd_device *device, int force)
1846 {
1847 enum drbd_state_rv retcode;
1848 int ret;
1849
1850 if (force) {
1851 set_bit(FORCE_DETACH, &device->flags);
1852 drbd_force_state(device, NS(disk, D_FAILED));
1853 retcode = SS_SUCCESS;
1854 goto out;
1855 }
1856
1857 drbd_suspend_io(device); /* so no-one is stuck in drbd_al_begin_io */
1858 drbd_md_get_buffer(device); /* make sure there is no in-flight meta-data IO */
1859 retcode = drbd_request_state(device, NS(disk, D_FAILED));
1860 drbd_md_put_buffer(device);
1861 /* D_FAILED will transition to DISKLESS. */
1862 ret = wait_event_interruptible(device->misc_wait,
1863 device->state.disk != D_FAILED);
1864 drbd_resume_io(device);
1865 if ((int)retcode == (int)SS_IS_DISKLESS)
1866 retcode = SS_NOTHING_TO_DO;
1867 if (ret)
1868 retcode = ERR_INTR;
1869 out:
1870 return retcode;
1871 }
1872
1873 /* Detaching the disk is a process in multiple stages. First we need to lock
1874 * out application IO, in-flight IO, IO stuck in drbd_al_begin_io.
1875 * Then we transition to D_DISKLESS, and wait for put_ldev() to return all
1876 * internal references as well.
1877 * Only then we have finally detached. */
1878 int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info)
1879 {
1880 struct drbd_config_context adm_ctx;
1881 enum drbd_ret_code retcode;
1882 struct detach_parms parms = { };
1883 int err;
1884
1885 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
1886 if (!adm_ctx.reply_skb)
1887 return retcode;
1888 if (retcode != NO_ERROR)
1889 goto out;
1890
1891 if (info->attrs[DRBD_NLA_DETACH_PARMS]) {
1892 err = detach_parms_from_attrs(&parms, info);
1893 if (err) {
1894 retcode = ERR_MANDATORY_TAG;
1895 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
1896 goto out;
1897 }
1898 }
1899
1900 mutex_lock(&adm_ctx.resource->adm_mutex);
1901 retcode = adm_detach(adm_ctx.device, parms.force_detach);
1902 mutex_unlock(&adm_ctx.resource->adm_mutex);
1903 out:
1904 drbd_adm_finish(&adm_ctx, info, retcode);
1905 return 0;
1906 }
1907
1908 static bool conn_resync_running(struct drbd_connection *connection)
1909 {
1910 struct drbd_peer_device *peer_device;
1911 bool rv = false;
1912 int vnr;
1913
1914 rcu_read_lock();
1915 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
1916 struct drbd_device *device = peer_device->device;
1917 if (device->state.conn == C_SYNC_SOURCE ||
1918 device->state.conn == C_SYNC_TARGET ||
1919 device->state.conn == C_PAUSED_SYNC_S ||
1920 device->state.conn == C_PAUSED_SYNC_T) {
1921 rv = true;
1922 break;
1923 }
1924 }
1925 rcu_read_unlock();
1926
1927 return rv;
1928 }
1929
1930 static bool conn_ov_running(struct drbd_connection *connection)
1931 {
1932 struct drbd_peer_device *peer_device;
1933 bool rv = false;
1934 int vnr;
1935
1936 rcu_read_lock();
1937 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
1938 struct drbd_device *device = peer_device->device;
1939 if (device->state.conn == C_VERIFY_S ||
1940 device->state.conn == C_VERIFY_T) {
1941 rv = true;
1942 break;
1943 }
1944 }
1945 rcu_read_unlock();
1946
1947 return rv;
1948 }
1949
1950 static enum drbd_ret_code
1951 _check_net_options(struct drbd_connection *connection, struct net_conf *old_net_conf, struct net_conf *new_net_conf)
1952 {
1953 struct drbd_peer_device *peer_device;
1954 int i;
1955
1956 if (old_net_conf && connection->cstate == C_WF_REPORT_PARAMS && connection->agreed_pro_version < 100) {
1957 if (new_net_conf->wire_protocol != old_net_conf->wire_protocol)
1958 return ERR_NEED_APV_100;
1959
1960 if (new_net_conf->two_primaries != old_net_conf->two_primaries)
1961 return ERR_NEED_APV_100;
1962
1963 if (strcmp(new_net_conf->integrity_alg, old_net_conf->integrity_alg))
1964 return ERR_NEED_APV_100;
1965 }
1966
1967 if (!new_net_conf->two_primaries &&
1968 conn_highest_role(connection) == R_PRIMARY &&
1969 conn_highest_peer(connection) == R_PRIMARY)
1970 return ERR_NEED_ALLOW_TWO_PRI;
1971
1972 if (new_net_conf->two_primaries &&
1973 (new_net_conf->wire_protocol != DRBD_PROT_C))
1974 return ERR_NOT_PROTO_C;
1975
1976 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
1977 struct drbd_device *device = peer_device->device;
1978 if (get_ldev(device)) {
1979 enum drbd_fencing_p fp = rcu_dereference(device->ldev->disk_conf)->fencing;
1980 put_ldev(device);
1981 if (new_net_conf->wire_protocol == DRBD_PROT_A && fp == FP_STONITH)
1982 return ERR_STONITH_AND_PROT_A;
1983 }
1984 if (device->state.role == R_PRIMARY && new_net_conf->discard_my_data)
1985 return ERR_DISCARD_IMPOSSIBLE;
1986 }
1987
1988 if (new_net_conf->on_congestion != OC_BLOCK && new_net_conf->wire_protocol != DRBD_PROT_A)
1989 return ERR_CONG_NOT_PROTO_A;
1990
1991 return NO_ERROR;
1992 }
1993
1994 static enum drbd_ret_code
1995 check_net_options(struct drbd_connection *connection, struct net_conf *new_net_conf)
1996 {
1997 static enum drbd_ret_code rv;
1998 struct drbd_peer_device *peer_device;
1999 int i;
2000
2001 rcu_read_lock();
2002 rv = _check_net_options(connection, rcu_dereference(connection->net_conf), new_net_conf);
2003 rcu_read_unlock();
2004
2005 /* connection->volumes protected by genl_lock() here */
2006 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
2007 struct drbd_device *device = peer_device->device;
2008 if (!device->bitmap) {
2009 if (drbd_bm_init(device))
2010 return ERR_NOMEM;
2011 }
2012 }
2013
2014 return rv;
2015 }
2016
2017 struct crypto {
2018 struct crypto_hash *verify_tfm;
2019 struct crypto_hash *csums_tfm;
2020 struct crypto_hash *cram_hmac_tfm;
2021 struct crypto_hash *integrity_tfm;
2022 };
2023
2024 static int
2025 alloc_hash(struct crypto_hash **tfm, char *tfm_name, int err_alg)
2026 {
2027 if (!tfm_name[0])
2028 return NO_ERROR;
2029
2030 *tfm = crypto_alloc_hash(tfm_name, 0, CRYPTO_ALG_ASYNC);
2031 if (IS_ERR(*tfm)) {
2032 *tfm = NULL;
2033 return err_alg;
2034 }
2035
2036 return NO_ERROR;
2037 }
2038
2039 static enum drbd_ret_code
2040 alloc_crypto(struct crypto *crypto, struct net_conf *new_net_conf)
2041 {
2042 char hmac_name[CRYPTO_MAX_ALG_NAME];
2043 enum drbd_ret_code rv;
2044
2045 rv = alloc_hash(&crypto->csums_tfm, new_net_conf->csums_alg,
2046 ERR_CSUMS_ALG);
2047 if (rv != NO_ERROR)
2048 return rv;
2049 rv = alloc_hash(&crypto->verify_tfm, new_net_conf->verify_alg,
2050 ERR_VERIFY_ALG);
2051 if (rv != NO_ERROR)
2052 return rv;
2053 rv = alloc_hash(&crypto->integrity_tfm, new_net_conf->integrity_alg,
2054 ERR_INTEGRITY_ALG);
2055 if (rv != NO_ERROR)
2056 return rv;
2057 if (new_net_conf->cram_hmac_alg[0] != 0) {
2058 snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)",
2059 new_net_conf->cram_hmac_alg);
2060
2061 rv = alloc_hash(&crypto->cram_hmac_tfm, hmac_name,
2062 ERR_AUTH_ALG);
2063 }
2064
2065 return rv;
2066 }
2067
2068 static void free_crypto(struct crypto *crypto)
2069 {
2070 crypto_free_hash(crypto->cram_hmac_tfm);
2071 crypto_free_hash(crypto->integrity_tfm);
2072 crypto_free_hash(crypto->csums_tfm);
2073 crypto_free_hash(crypto->verify_tfm);
2074 }
2075
2076 int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info)
2077 {
2078 struct drbd_config_context adm_ctx;
2079 enum drbd_ret_code retcode;
2080 struct drbd_connection *connection;
2081 struct net_conf *old_net_conf, *new_net_conf = NULL;
2082 int err;
2083 int ovr; /* online verify running */
2084 int rsr; /* re-sync running */
2085 struct crypto crypto = { };
2086
2087 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_CONNECTION);
2088 if (!adm_ctx.reply_skb)
2089 return retcode;
2090 if (retcode != NO_ERROR)
2091 goto finish;
2092
2093 connection = adm_ctx.connection;
2094 mutex_lock(&adm_ctx.resource->adm_mutex);
2095
2096 new_net_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
2097 if (!new_net_conf) {
2098 retcode = ERR_NOMEM;
2099 goto out;
2100 }
2101
2102 conn_reconfig_start(connection);
2103
2104 mutex_lock(&connection->data.mutex);
2105 mutex_lock(&connection->resource->conf_update);
2106 old_net_conf = connection->net_conf;
2107
2108 if (!old_net_conf) {
2109 drbd_msg_put_info(adm_ctx.reply_skb, "net conf missing, try connect");
2110 retcode = ERR_INVALID_REQUEST;
2111 goto fail;
2112 }
2113
2114 *new_net_conf = *old_net_conf;
2115 if (should_set_defaults(info))
2116 set_net_conf_defaults(new_net_conf);
2117
2118 err = net_conf_from_attrs_for_change(new_net_conf, info);
2119 if (err && err != -ENOMSG) {
2120 retcode = ERR_MANDATORY_TAG;
2121 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
2122 goto fail;
2123 }
2124
2125 retcode = check_net_options(connection, new_net_conf);
2126 if (retcode != NO_ERROR)
2127 goto fail;
2128
2129 /* re-sync running */
2130 rsr = conn_resync_running(connection);
2131 if (rsr && strcmp(new_net_conf->csums_alg, old_net_conf->csums_alg)) {
2132 retcode = ERR_CSUMS_RESYNC_RUNNING;
2133 goto fail;
2134 }
2135
2136 /* online verify running */
2137 ovr = conn_ov_running(connection);
2138 if (ovr && strcmp(new_net_conf->verify_alg, old_net_conf->verify_alg)) {
2139 retcode = ERR_VERIFY_RUNNING;
2140 goto fail;
2141 }
2142
2143 retcode = alloc_crypto(&crypto, new_net_conf);
2144 if (retcode != NO_ERROR)
2145 goto fail;
2146
2147 rcu_assign_pointer(connection->net_conf, new_net_conf);
2148
2149 if (!rsr) {
2150 crypto_free_hash(connection->csums_tfm);
2151 connection->csums_tfm = crypto.csums_tfm;
2152 crypto.csums_tfm = NULL;
2153 }
2154 if (!ovr) {
2155 crypto_free_hash(connection->verify_tfm);
2156 connection->verify_tfm = crypto.verify_tfm;
2157 crypto.verify_tfm = NULL;
2158 }
2159
2160 crypto_free_hash(connection->integrity_tfm);
2161 connection->integrity_tfm = crypto.integrity_tfm;
2162 if (connection->cstate >= C_WF_REPORT_PARAMS && connection->agreed_pro_version >= 100)
2163 /* Do this without trying to take connection->data.mutex again. */
2164 __drbd_send_protocol(connection, P_PROTOCOL_UPDATE);
2165
2166 crypto_free_hash(connection->cram_hmac_tfm);
2167 connection->cram_hmac_tfm = crypto.cram_hmac_tfm;
2168
2169 mutex_unlock(&connection->resource->conf_update);
2170 mutex_unlock(&connection->data.mutex);
2171 synchronize_rcu();
2172 kfree(old_net_conf);
2173
2174 if (connection->cstate >= C_WF_REPORT_PARAMS) {
2175 struct drbd_peer_device *peer_device;
2176 int vnr;
2177
2178 idr_for_each_entry(&connection->peer_devices, peer_device, vnr)
2179 drbd_send_sync_param(peer_device);
2180 }
2181
2182 goto done;
2183
2184 fail:
2185 mutex_unlock(&connection->resource->conf_update);
2186 mutex_unlock(&connection->data.mutex);
2187 free_crypto(&crypto);
2188 kfree(new_net_conf);
2189 done:
2190 conn_reconfig_done(connection);
2191 out:
2192 mutex_unlock(&adm_ctx.resource->adm_mutex);
2193 finish:
2194 drbd_adm_finish(&adm_ctx, info, retcode);
2195 return 0;
2196 }
2197
2198 int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info)
2199 {
2200 struct drbd_config_context adm_ctx;
2201 struct drbd_peer_device *peer_device;
2202 struct net_conf *old_net_conf, *new_net_conf = NULL;
2203 struct crypto crypto = { };
2204 struct drbd_resource *resource;
2205 struct drbd_connection *connection;
2206 enum drbd_ret_code retcode;
2207 int i;
2208 int err;
2209
2210 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
2211
2212 if (!adm_ctx.reply_skb)
2213 return retcode;
2214 if (retcode != NO_ERROR)
2215 goto out;
2216 if (!(adm_ctx.my_addr && adm_ctx.peer_addr)) {
2217 drbd_msg_put_info(adm_ctx.reply_skb, "connection endpoint(s) missing");
2218 retcode = ERR_INVALID_REQUEST;
2219 goto out;
2220 }
2221
2222 /* No need for _rcu here. All reconfiguration is
2223 * strictly serialized on genl_lock(). We are protected against
2224 * concurrent reconfiguration/addition/deletion */
2225 for_each_resource(resource, &drbd_resources) {
2226 for_each_connection(connection, resource) {
2227 if (nla_len(adm_ctx.my_addr) == connection->my_addr_len &&
2228 !memcmp(nla_data(adm_ctx.my_addr), &connection->my_addr,
2229 connection->my_addr_len)) {
2230 retcode = ERR_LOCAL_ADDR;
2231 goto out;
2232 }
2233
2234 if (nla_len(adm_ctx.peer_addr) == connection->peer_addr_len &&
2235 !memcmp(nla_data(adm_ctx.peer_addr), &connection->peer_addr,
2236 connection->peer_addr_len)) {
2237 retcode = ERR_PEER_ADDR;
2238 goto out;
2239 }
2240 }
2241 }
2242
2243 mutex_lock(&adm_ctx.resource->adm_mutex);
2244 connection = first_connection(adm_ctx.resource);
2245 conn_reconfig_start(connection);
2246
2247 if (connection->cstate > C_STANDALONE) {
2248 retcode = ERR_NET_CONFIGURED;
2249 goto fail;
2250 }
2251
2252 /* allocation not in the IO path, drbdsetup / netlink process context */
2253 new_net_conf = kzalloc(sizeof(*new_net_conf), GFP_KERNEL);
2254 if (!new_net_conf) {
2255 retcode = ERR_NOMEM;
2256 goto fail;
2257 }
2258
2259 set_net_conf_defaults(new_net_conf);
2260
2261 err = net_conf_from_attrs(new_net_conf, info);
2262 if (err && err != -ENOMSG) {
2263 retcode = ERR_MANDATORY_TAG;
2264 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
2265 goto fail;
2266 }
2267
2268 retcode = check_net_options(connection, new_net_conf);
2269 if (retcode != NO_ERROR)
2270 goto fail;
2271
2272 retcode = alloc_crypto(&crypto, new_net_conf);
2273 if (retcode != NO_ERROR)
2274 goto fail;
2275
2276 ((char *)new_net_conf->shared_secret)[SHARED_SECRET_MAX-1] = 0;
2277
2278 drbd_flush_workqueue(&connection->sender_work);
2279
2280 mutex_lock(&adm_ctx.resource->conf_update);
2281 old_net_conf = connection->net_conf;
2282 if (old_net_conf) {
2283 retcode = ERR_NET_CONFIGURED;
2284 mutex_unlock(&adm_ctx.resource->conf_update);
2285 goto fail;
2286 }
2287 rcu_assign_pointer(connection->net_conf, new_net_conf);
2288
2289 conn_free_crypto(connection);
2290 connection->cram_hmac_tfm = crypto.cram_hmac_tfm;
2291 connection->integrity_tfm = crypto.integrity_tfm;
2292 connection->csums_tfm = crypto.csums_tfm;
2293 connection->verify_tfm = crypto.verify_tfm;
2294
2295 connection->my_addr_len = nla_len(adm_ctx.my_addr);
2296 memcpy(&connection->my_addr, nla_data(adm_ctx.my_addr), connection->my_addr_len);
2297 connection->peer_addr_len = nla_len(adm_ctx.peer_addr);
2298 memcpy(&connection->peer_addr, nla_data(adm_ctx.peer_addr), connection->peer_addr_len);
2299
2300 mutex_unlock(&adm_ctx.resource->conf_update);
2301
2302 rcu_read_lock();
2303 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
2304 struct drbd_device *device = peer_device->device;
2305 device->send_cnt = 0;
2306 device->recv_cnt = 0;
2307 }
2308 rcu_read_unlock();
2309
2310 retcode = conn_request_state(connection, NS(conn, C_UNCONNECTED), CS_VERBOSE);
2311
2312 conn_reconfig_done(connection);
2313 mutex_unlock(&adm_ctx.resource->adm_mutex);
2314 drbd_adm_finish(&adm_ctx, info, retcode);
2315 return 0;
2316
2317 fail:
2318 free_crypto(&crypto);
2319 kfree(new_net_conf);
2320
2321 conn_reconfig_done(connection);
2322 mutex_unlock(&adm_ctx.resource->adm_mutex);
2323 out:
2324 drbd_adm_finish(&adm_ctx, info, retcode);
2325 return 0;
2326 }
2327
2328 static enum drbd_state_rv conn_try_disconnect(struct drbd_connection *connection, bool force)
2329 {
2330 enum drbd_state_rv rv;
2331
2332 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING),
2333 force ? CS_HARD : 0);
2334
2335 switch (rv) {
2336 case SS_NOTHING_TO_DO:
2337 break;
2338 case SS_ALREADY_STANDALONE:
2339 return SS_SUCCESS;
2340 case SS_PRIMARY_NOP:
2341 /* Our state checking code wants to see the peer outdated. */
2342 rv = conn_request_state(connection, NS2(conn, C_DISCONNECTING, pdsk, D_OUTDATED), 0);
2343
2344 if (rv == SS_OUTDATE_WO_CONN) /* lost connection before graceful disconnect succeeded */
2345 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING), CS_VERBOSE);
2346
2347 break;
2348 case SS_CW_FAILED_BY_PEER:
2349 /* The peer probably wants to see us outdated. */
2350 rv = conn_request_state(connection, NS2(conn, C_DISCONNECTING,
2351 disk, D_OUTDATED), 0);
2352 if (rv == SS_IS_DISKLESS || rv == SS_LOWER_THAN_OUTDATED) {
2353 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING),
2354 CS_HARD);
2355 }
2356 break;
2357 default:;
2358 /* no special handling necessary */
2359 }
2360
2361 if (rv >= SS_SUCCESS) {
2362 enum drbd_state_rv rv2;
2363 /* No one else can reconfigure the network while I am here.
2364 * The state handling only uses drbd_thread_stop_nowait(),
2365 * we want to really wait here until the receiver is no more.
2366 */
2367 drbd_thread_stop(&connection->receiver);
2368
2369 /* Race breaker. This additional state change request may be
2370 * necessary, if this was a forced disconnect during a receiver
2371 * restart. We may have "killed" the receiver thread just
2372 * after drbd_receiver() returned. Typically, we should be
2373 * C_STANDALONE already, now, and this becomes a no-op.
2374 */
2375 rv2 = conn_request_state(connection, NS(conn, C_STANDALONE),
2376 CS_VERBOSE | CS_HARD);
2377 if (rv2 < SS_SUCCESS)
2378 drbd_err(connection,
2379 "unexpected rv2=%d in conn_try_disconnect()\n",
2380 rv2);
2381 }
2382 return rv;
2383 }
2384
2385 int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info)
2386 {
2387 struct drbd_config_context adm_ctx;
2388 struct disconnect_parms parms;
2389 struct drbd_connection *connection;
2390 enum drbd_state_rv rv;
2391 enum drbd_ret_code retcode;
2392 int err;
2393
2394 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_CONNECTION);
2395 if (!adm_ctx.reply_skb)
2396 return retcode;
2397 if (retcode != NO_ERROR)
2398 goto fail;
2399
2400 connection = adm_ctx.connection;
2401 memset(&parms, 0, sizeof(parms));
2402 if (info->attrs[DRBD_NLA_DISCONNECT_PARMS]) {
2403 err = disconnect_parms_from_attrs(&parms, info);
2404 if (err) {
2405 retcode = ERR_MANDATORY_TAG;
2406 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
2407 goto fail;
2408 }
2409 }
2410
2411 mutex_lock(&adm_ctx.resource->adm_mutex);
2412 rv = conn_try_disconnect(connection, parms.force_disconnect);
2413 if (rv < SS_SUCCESS)
2414 retcode = rv; /* FIXME: Type mismatch. */
2415 else
2416 retcode = NO_ERROR;
2417 mutex_unlock(&adm_ctx.resource->adm_mutex);
2418 fail:
2419 drbd_adm_finish(&adm_ctx, info, retcode);
2420 return 0;
2421 }
2422
2423 void resync_after_online_grow(struct drbd_device *device)
2424 {
2425 int iass; /* I am sync source */
2426
2427 drbd_info(device, "Resync of new storage after online grow\n");
2428 if (device->state.role != device->state.peer)
2429 iass = (device->state.role == R_PRIMARY);
2430 else
2431 iass = test_bit(RESOLVE_CONFLICTS, &first_peer_device(device)->connection->flags);
2432
2433 if (iass)
2434 drbd_start_resync(device, C_SYNC_SOURCE);
2435 else
2436 _drbd_request_state(device, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE + CS_SERIALIZE);
2437 }
2438
2439 int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info)
2440 {
2441 struct drbd_config_context adm_ctx;
2442 struct disk_conf *old_disk_conf, *new_disk_conf = NULL;
2443 struct resize_parms rs;
2444 struct drbd_device *device;
2445 enum drbd_ret_code retcode;
2446 enum determine_dev_size dd;
2447 bool change_al_layout = false;
2448 enum dds_flags ddsf;
2449 sector_t u_size;
2450 int err;
2451
2452 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
2453 if (!adm_ctx.reply_skb)
2454 return retcode;
2455 if (retcode != NO_ERROR)
2456 goto finish;
2457
2458 mutex_lock(&adm_ctx.resource->adm_mutex);
2459 device = adm_ctx.device;
2460 if (!get_ldev(device)) {
2461 retcode = ERR_NO_DISK;
2462 goto fail;
2463 }
2464
2465 memset(&rs, 0, sizeof(struct resize_parms));
2466 rs.al_stripes = device->ldev->md.al_stripes;
2467 rs.al_stripe_size = device->ldev->md.al_stripe_size_4k * 4;
2468 if (info->attrs[DRBD_NLA_RESIZE_PARMS]) {
2469 err = resize_parms_from_attrs(&rs, info);
2470 if (err) {
2471 retcode = ERR_MANDATORY_TAG;
2472 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
2473 goto fail_ldev;
2474 }
2475 }
2476
2477 if (device->state.conn > C_CONNECTED) {
2478 retcode = ERR_RESIZE_RESYNC;
2479 goto fail_ldev;
2480 }
2481
2482 if (device->state.role == R_SECONDARY &&
2483 device->state.peer == R_SECONDARY) {
2484 retcode = ERR_NO_PRIMARY;
2485 goto fail_ldev;
2486 }
2487
2488 if (rs.no_resync && first_peer_device(device)->connection->agreed_pro_version < 93) {
2489 retcode = ERR_NEED_APV_93;
2490 goto fail_ldev;
2491 }
2492
2493 rcu_read_lock();
2494 u_size = rcu_dereference(device->ldev->disk_conf)->disk_size;
2495 rcu_read_unlock();
2496 if (u_size != (sector_t)rs.resize_size) {
2497 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
2498 if (!new_disk_conf) {
2499 retcode = ERR_NOMEM;
2500 goto fail_ldev;
2501 }
2502 }
2503
2504 if (device->ldev->md.al_stripes != rs.al_stripes ||
2505 device->ldev->md.al_stripe_size_4k != rs.al_stripe_size / 4) {
2506 u32 al_size_k = rs.al_stripes * rs.al_stripe_size;
2507
2508 if (al_size_k > (16 * 1024 * 1024)) {
2509 retcode = ERR_MD_LAYOUT_TOO_BIG;
2510 goto fail_ldev;
2511 }
2512
2513 if (al_size_k < MD_32kB_SECT/2) {
2514 retcode = ERR_MD_LAYOUT_TOO_SMALL;
2515 goto fail_ldev;
2516 }
2517
2518 if (device->state.conn != C_CONNECTED && !rs.resize_force) {
2519 retcode = ERR_MD_LAYOUT_CONNECTED;
2520 goto fail_ldev;
2521 }
2522
2523 change_al_layout = true;
2524 }
2525
2526 if (device->ldev->known_size != drbd_get_capacity(device->ldev->backing_bdev))
2527 device->ldev->known_size = drbd_get_capacity(device->ldev->backing_bdev);
2528
2529 if (new_disk_conf) {
2530 mutex_lock(&device->resource->conf_update);
2531 old_disk_conf = device->ldev->disk_conf;
2532 *new_disk_conf = *old_disk_conf;
2533 new_disk_conf->disk_size = (sector_t)rs.resize_size;
2534 rcu_assign_pointer(device->ldev->disk_conf, new_disk_conf);
2535 mutex_unlock(&device->resource->conf_update);
2536 synchronize_rcu();
2537 kfree(old_disk_conf);
2538 }
2539
2540 ddsf = (rs.resize_force ? DDSF_FORCED : 0) | (rs.no_resync ? DDSF_NO_RESYNC : 0);
2541 dd = drbd_determine_dev_size(device, ddsf, change_al_layout ? &rs : NULL);
2542 drbd_md_sync(device);
2543 put_ldev(device);
2544 if (dd == DS_ERROR) {
2545 retcode = ERR_NOMEM_BITMAP;
2546 goto fail;
2547 } else if (dd == DS_ERROR_SPACE_MD) {
2548 retcode = ERR_MD_LAYOUT_NO_FIT;
2549 goto fail;
2550 } else if (dd == DS_ERROR_SHRINK) {
2551 retcode = ERR_IMPLICIT_SHRINK;
2552 goto fail;
2553 }
2554
2555 if (device->state.conn == C_CONNECTED) {
2556 if (dd == DS_GREW)
2557 set_bit(RESIZE_PENDING, &device->flags);
2558
2559 drbd_send_uuids(first_peer_device(device));
2560 drbd_send_sizes(first_peer_device(device), 1, ddsf);
2561 }
2562
2563 fail:
2564 mutex_unlock(&adm_ctx.resource->adm_mutex);
2565 finish:
2566 drbd_adm_finish(&adm_ctx, info, retcode);
2567 return 0;
2568
2569 fail_ldev:
2570 put_ldev(device);
2571 goto fail;
2572 }
2573
2574 int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info)
2575 {
2576 struct drbd_config_context adm_ctx;
2577 enum drbd_ret_code retcode;
2578 struct res_opts res_opts;
2579 int err;
2580
2581 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
2582 if (!adm_ctx.reply_skb)
2583 return retcode;
2584 if (retcode != NO_ERROR)
2585 goto fail;
2586
2587 res_opts = adm_ctx.resource->res_opts;
2588 if (should_set_defaults(info))
2589 set_res_opts_defaults(&res_opts);
2590
2591 err = res_opts_from_attrs(&res_opts, info);
2592 if (err && err != -ENOMSG) {
2593 retcode = ERR_MANDATORY_TAG;
2594 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
2595 goto fail;
2596 }
2597
2598 mutex_lock(&adm_ctx.resource->adm_mutex);
2599 err = set_resource_options(adm_ctx.resource, &res_opts);
2600 if (err) {
2601 retcode = ERR_INVALID_REQUEST;
2602 if (err == -ENOMEM)
2603 retcode = ERR_NOMEM;
2604 }
2605 mutex_unlock(&adm_ctx.resource->adm_mutex);
2606
2607 fail:
2608 drbd_adm_finish(&adm_ctx, info, retcode);
2609 return 0;
2610 }
2611
2612 int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info)
2613 {
2614 struct drbd_config_context adm_ctx;
2615 struct drbd_device *device;
2616 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2617
2618 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
2619 if (!adm_ctx.reply_skb)
2620 return retcode;
2621 if (retcode != NO_ERROR)
2622 goto out;
2623
2624 mutex_lock(&adm_ctx.resource->adm_mutex);
2625 device = adm_ctx.device;
2626
2627 /* If there is still bitmap IO pending, probably because of a previous
2628 * resync just being finished, wait for it before requesting a new resync.
2629 * Also wait for it's after_state_ch(). */
2630 drbd_suspend_io(device);
2631 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
2632 drbd_flush_workqueue(&first_peer_device(device)->connection->sender_work);
2633
2634 /* If we happen to be C_STANDALONE R_SECONDARY, just change to
2635 * D_INCONSISTENT, and set all bits in the bitmap. Otherwise,
2636 * try to start a resync handshake as sync target for full sync.
2637 */
2638 if (device->state.conn == C_STANDALONE && device->state.role == R_SECONDARY) {
2639 retcode = drbd_request_state(device, NS(disk, D_INCONSISTENT));
2640 if (retcode >= SS_SUCCESS) {
2641 if (drbd_bitmap_io(device, &drbd_bmio_set_n_write,
2642 "set_n_write from invalidate", BM_LOCKED_MASK))
2643 retcode = ERR_IO_MD_DISK;
2644 }
2645 } else
2646 retcode = drbd_request_state(device, NS(conn, C_STARTING_SYNC_T));
2647 drbd_resume_io(device);
2648 mutex_unlock(&adm_ctx.resource->adm_mutex);
2649 out:
2650 drbd_adm_finish(&adm_ctx, info, retcode);
2651 return 0;
2652 }
2653
2654 static int drbd_adm_simple_request_state(struct sk_buff *skb, struct genl_info *info,
2655 union drbd_state mask, union drbd_state val)
2656 {
2657 struct drbd_config_context adm_ctx;
2658 enum drbd_ret_code retcode;
2659
2660 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
2661 if (!adm_ctx.reply_skb)
2662 return retcode;
2663 if (retcode != NO_ERROR)
2664 goto out;
2665
2666 mutex_lock(&adm_ctx.resource->adm_mutex);
2667 retcode = drbd_request_state(adm_ctx.device, mask, val);
2668 mutex_unlock(&adm_ctx.resource->adm_mutex);
2669 out:
2670 drbd_adm_finish(&adm_ctx, info, retcode);
2671 return 0;
2672 }
2673
2674 static int drbd_bmio_set_susp_al(struct drbd_device *device)
2675 {
2676 int rv;
2677
2678 rv = drbd_bmio_set_n_write(device);
2679 drbd_suspend_al(device);
2680 return rv;
2681 }
2682
2683 int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info)
2684 {
2685 struct drbd_config_context adm_ctx;
2686 int retcode; /* drbd_ret_code, drbd_state_rv */
2687 struct drbd_device *device;
2688
2689 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
2690 if (!adm_ctx.reply_skb)
2691 return retcode;
2692 if (retcode != NO_ERROR)
2693 goto out;
2694
2695 mutex_lock(&adm_ctx.resource->adm_mutex);
2696 device = adm_ctx.device;
2697
2698 /* If there is still bitmap IO pending, probably because of a previous
2699 * resync just being finished, wait for it before requesting a new resync.
2700 * Also wait for it's after_state_ch(). */
2701 drbd_suspend_io(device);
2702 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
2703 drbd_flush_workqueue(&first_peer_device(device)->connection->sender_work);
2704
2705 /* If we happen to be C_STANDALONE R_PRIMARY, just set all bits
2706 * in the bitmap. Otherwise, try to start a resync handshake
2707 * as sync source for full sync.
2708 */
2709 if (device->state.conn == C_STANDALONE && device->state.role == R_PRIMARY) {
2710 /* The peer will get a resync upon connect anyways. Just make that
2711 into a full resync. */
2712 retcode = drbd_request_state(device, NS(pdsk, D_INCONSISTENT));
2713 if (retcode >= SS_SUCCESS) {
2714 if (drbd_bitmap_io(device, &drbd_bmio_set_susp_al,
2715 "set_n_write from invalidate_peer",
2716 BM_LOCKED_SET_ALLOWED))
2717 retcode = ERR_IO_MD_DISK;
2718 }
2719 } else
2720 retcode = drbd_request_state(device, NS(conn, C_STARTING_SYNC_S));
2721 drbd_resume_io(device);
2722 mutex_unlock(&adm_ctx.resource->adm_mutex);
2723 out:
2724 drbd_adm_finish(&adm_ctx, info, retcode);
2725 return 0;
2726 }
2727
2728 int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info)
2729 {
2730 struct drbd_config_context adm_ctx;
2731 enum drbd_ret_code retcode;
2732
2733 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
2734 if (!adm_ctx.reply_skb)
2735 return retcode;
2736 if (retcode != NO_ERROR)
2737 goto out;
2738
2739 mutex_lock(&adm_ctx.resource->adm_mutex);
2740 if (drbd_request_state(adm_ctx.device, NS(user_isp, 1)) == SS_NOTHING_TO_DO)
2741 retcode = ERR_PAUSE_IS_SET;
2742 mutex_unlock(&adm_ctx.resource->adm_mutex);
2743 out:
2744 drbd_adm_finish(&adm_ctx, info, retcode);
2745 return 0;
2746 }
2747
2748 int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info)
2749 {
2750 struct drbd_config_context adm_ctx;
2751 union drbd_dev_state s;
2752 enum drbd_ret_code retcode;
2753
2754 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
2755 if (!adm_ctx.reply_skb)
2756 return retcode;
2757 if (retcode != NO_ERROR)
2758 goto out;
2759
2760 mutex_lock(&adm_ctx.resource->adm_mutex);
2761 if (drbd_request_state(adm_ctx.device, NS(user_isp, 0)) == SS_NOTHING_TO_DO) {
2762 s = adm_ctx.device->state;
2763 if (s.conn == C_PAUSED_SYNC_S || s.conn == C_PAUSED_SYNC_T) {
2764 retcode = s.aftr_isp ? ERR_PIC_AFTER_DEP :
2765 s.peer_isp ? ERR_PIC_PEER_DEP : ERR_PAUSE_IS_CLEAR;
2766 } else {
2767 retcode = ERR_PAUSE_IS_CLEAR;
2768 }
2769 }
2770 mutex_unlock(&adm_ctx.resource->adm_mutex);
2771 out:
2772 drbd_adm_finish(&adm_ctx, info, retcode);
2773 return 0;
2774 }
2775
2776 int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info)
2777 {
2778 return drbd_adm_simple_request_state(skb, info, NS(susp, 1));
2779 }
2780
2781 int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info)
2782 {
2783 struct drbd_config_context adm_ctx;
2784 struct drbd_device *device;
2785 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2786
2787 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
2788 if (!adm_ctx.reply_skb)
2789 return retcode;
2790 if (retcode != NO_ERROR)
2791 goto out;
2792
2793 mutex_lock(&adm_ctx.resource->adm_mutex);
2794 device = adm_ctx.device;
2795 if (test_bit(NEW_CUR_UUID, &device->flags)) {
2796 drbd_uuid_new_current(device);
2797 clear_bit(NEW_CUR_UUID, &device->flags);
2798 }
2799 drbd_suspend_io(device);
2800 retcode = drbd_request_state(device, NS3(susp, 0, susp_nod, 0, susp_fen, 0));
2801 if (retcode == SS_SUCCESS) {
2802 if (device->state.conn < C_CONNECTED)
2803 tl_clear(first_peer_device(device)->connection);
2804 if (device->state.disk == D_DISKLESS || device->state.disk == D_FAILED)
2805 tl_restart(first_peer_device(device)->connection, FAIL_FROZEN_DISK_IO);
2806 }
2807 drbd_resume_io(device);
2808 mutex_unlock(&adm_ctx.resource->adm_mutex);
2809 out:
2810 drbd_adm_finish(&adm_ctx, info, retcode);
2811 return 0;
2812 }
2813
2814 int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info)
2815 {
2816 return drbd_adm_simple_request_state(skb, info, NS(disk, D_OUTDATED));
2817 }
2818
2819 static int nla_put_drbd_cfg_context(struct sk_buff *skb,
2820 struct drbd_resource *resource,
2821 struct drbd_connection *connection,
2822 struct drbd_device *device)
2823 {
2824 struct nlattr *nla;
2825 nla = nla_nest_start(skb, DRBD_NLA_CFG_CONTEXT);
2826 if (!nla)
2827 goto nla_put_failure;
2828 if (device &&
2829 nla_put_u32(skb, T_ctx_volume, device->vnr))
2830 goto nla_put_failure;
2831 if (nla_put_string(skb, T_ctx_resource_name, resource->name))
2832 goto nla_put_failure;
2833 if (connection) {
2834 if (connection->my_addr_len &&
2835 nla_put(skb, T_ctx_my_addr, connection->my_addr_len, &connection->my_addr))
2836 goto nla_put_failure;
2837 if (connection->peer_addr_len &&
2838 nla_put(skb, T_ctx_peer_addr, connection->peer_addr_len, &connection->peer_addr))
2839 goto nla_put_failure;
2840 }
2841 nla_nest_end(skb, nla);
2842 return 0;
2843
2844 nla_put_failure:
2845 if (nla)
2846 nla_nest_cancel(skb, nla);
2847 return -EMSGSIZE;
2848 }
2849
2850 /*
2851 * Return the connection of @resource if @resource has exactly one connection.
2852 */
2853 static struct drbd_connection *the_only_connection(struct drbd_resource *resource)
2854 {
2855 struct list_head *connections = &resource->connections;
2856
2857 if (list_empty(connections) || connections->next->next != connections)
2858 return NULL;
2859 return list_first_entry(&resource->connections, struct drbd_connection, connections);
2860 }
2861
2862 int nla_put_status_info(struct sk_buff *skb, struct drbd_device *device,
2863 const struct sib_info *sib)
2864 {
2865 struct drbd_resource *resource = device->resource;
2866 struct state_info *si = NULL; /* for sizeof(si->member); */
2867 struct nlattr *nla;
2868 int got_ldev;
2869 int err = 0;
2870 int exclude_sensitive;
2871
2872 /* If sib != NULL, this is drbd_bcast_event, which anyone can listen
2873 * to. So we better exclude_sensitive information.
2874 *
2875 * If sib == NULL, this is drbd_adm_get_status, executed synchronously
2876 * in the context of the requesting user process. Exclude sensitive
2877 * information, unless current has superuser.
2878 *
2879 * NOTE: for drbd_adm_get_status_all(), this is a netlink dump, and
2880 * relies on the current implementation of netlink_dump(), which
2881 * executes the dump callback successively from netlink_recvmsg(),
2882 * always in the context of the receiving process */
2883 exclude_sensitive = sib || !capable(CAP_SYS_ADMIN);
2884
2885 got_ldev = get_ldev(device);
2886
2887 /* We need to add connection name and volume number information still.
2888 * Minor number is in drbd_genlmsghdr. */
2889 if (nla_put_drbd_cfg_context(skb, resource, the_only_connection(resource), device))
2890 goto nla_put_failure;
2891
2892 if (res_opts_to_skb(skb, &device->resource->res_opts, exclude_sensitive))
2893 goto nla_put_failure;
2894
2895 rcu_read_lock();
2896 if (got_ldev) {
2897 struct disk_conf *disk_conf;
2898
2899 disk_conf = rcu_dereference(device->ldev->disk_conf);
2900 err = disk_conf_to_skb(skb, disk_conf, exclude_sensitive);
2901 }
2902 if (!err) {
2903 struct net_conf *nc;
2904
2905 nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
2906 if (nc)
2907 err = net_conf_to_skb(skb, nc, exclude_sensitive);
2908 }
2909 rcu_read_unlock();
2910 if (err)
2911 goto nla_put_failure;
2912
2913 nla = nla_nest_start(skb, DRBD_NLA_STATE_INFO);
2914 if (!nla)
2915 goto nla_put_failure;
2916 if (nla_put_u32(skb, T_sib_reason, sib ? sib->sib_reason : SIB_GET_STATUS_REPLY) ||
2917 nla_put_u32(skb, T_current_state, device->state.i) ||
2918 nla_put_u64(skb, T_ed_uuid, device->ed_uuid) ||
2919 nla_put_u64(skb, T_capacity, drbd_get_capacity(device->this_bdev)) ||
2920 nla_put_u64(skb, T_send_cnt, device->send_cnt) ||
2921 nla_put_u64(skb, T_recv_cnt, device->recv_cnt) ||
2922 nla_put_u64(skb, T_read_cnt, device->read_cnt) ||
2923 nla_put_u64(skb, T_writ_cnt, device->writ_cnt) ||
2924 nla_put_u64(skb, T_al_writ_cnt, device->al_writ_cnt) ||
2925 nla_put_u64(skb, T_bm_writ_cnt, device->bm_writ_cnt) ||
2926 nla_put_u32(skb, T_ap_bio_cnt, atomic_read(&device->ap_bio_cnt)) ||
2927 nla_put_u32(skb, T_ap_pending_cnt, atomic_read(&device->ap_pending_cnt)) ||
2928 nla_put_u32(skb, T_rs_pending_cnt, atomic_read(&device->rs_pending_cnt)))
2929 goto nla_put_failure;
2930
2931 if (got_ldev) {
2932 int err;
2933
2934 spin_lock_irq(&device->ldev->md.uuid_lock);
2935 err = nla_put(skb, T_uuids, sizeof(si->uuids), device->ldev->md.uuid);
2936 spin_unlock_irq(&device->ldev->md.uuid_lock);
2937
2938 if (err)
2939 goto nla_put_failure;
2940
2941 if (nla_put_u32(skb, T_disk_flags, device->ldev->md.flags) ||
2942 nla_put_u64(skb, T_bits_total, drbd_bm_bits(device)) ||
2943 nla_put_u64(skb, T_bits_oos, drbd_bm_total_weight(device)))
2944 goto nla_put_failure;
2945 if (C_SYNC_SOURCE <= device->state.conn &&
2946 C_PAUSED_SYNC_T >= device->state.conn) {
2947 if (nla_put_u64(skb, T_bits_rs_total, device->rs_total) ||
2948 nla_put_u64(skb, T_bits_rs_failed, device->rs_failed))
2949 goto nla_put_failure;
2950 }
2951 }
2952
2953 if (sib) {
2954 switch(sib->sib_reason) {
2955 case SIB_SYNC_PROGRESS:
2956 case SIB_GET_STATUS_REPLY:
2957 break;
2958 case SIB_STATE_CHANGE:
2959 if (nla_put_u32(skb, T_prev_state, sib->os.i) ||
2960 nla_put_u32(skb, T_new_state, sib->ns.i))
2961 goto nla_put_failure;
2962 break;
2963 case SIB_HELPER_POST:
2964 if (nla_put_u32(skb, T_helper_exit_code,
2965 sib->helper_exit_code))
2966 goto nla_put_failure;
2967 /* fall through */
2968 case SIB_HELPER_PRE:
2969 if (nla_put_string(skb, T_helper, sib->helper_name))
2970 goto nla_put_failure;
2971 break;
2972 }
2973 }
2974 nla_nest_end(skb, nla);
2975
2976 if (0)
2977 nla_put_failure:
2978 err = -EMSGSIZE;
2979 if (got_ldev)
2980 put_ldev(device);
2981 return err;
2982 }
2983
2984 int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info)
2985 {
2986 struct drbd_config_context adm_ctx;
2987 enum drbd_ret_code retcode;
2988 int err;
2989
2990 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
2991 if (!adm_ctx.reply_skb)
2992 return retcode;
2993 if (retcode != NO_ERROR)
2994 goto out;
2995
2996 err = nla_put_status_info(adm_ctx.reply_skb, adm_ctx.device, NULL);
2997 if (err) {
2998 nlmsg_free(adm_ctx.reply_skb);
2999 return err;
3000 }
3001 out:
3002 drbd_adm_finish(&adm_ctx, info, retcode);
3003 return 0;
3004 }
3005
3006 static int get_one_status(struct sk_buff *skb, struct netlink_callback *cb)
3007 {
3008 struct drbd_device *device;
3009 struct drbd_genlmsghdr *dh;
3010 struct drbd_resource *pos = (struct drbd_resource *)cb->args[0];
3011 struct drbd_resource *resource = NULL;
3012 struct drbd_resource *tmp;
3013 unsigned volume = cb->args[1];
3014
3015 /* Open coded, deferred, iteration:
3016 * for_each_resource_safe(resource, tmp, &drbd_resources) {
3017 * connection = "first connection of resource or undefined";
3018 * idr_for_each_entry(&resource->devices, device, i) {
3019 * ...
3020 * }
3021 * }
3022 * where resource is cb->args[0];
3023 * and i is cb->args[1];
3024 *
3025 * cb->args[2] indicates if we shall loop over all resources,
3026 * or just dump all volumes of a single resource.
3027 *
3028 * This may miss entries inserted after this dump started,
3029 * or entries deleted before they are reached.
3030 *
3031 * We need to make sure the device won't disappear while
3032 * we are looking at it, and revalidate our iterators
3033 * on each iteration.
3034 */
3035
3036 /* synchronize with conn_create()/drbd_destroy_connection() */
3037 rcu_read_lock();
3038 /* revalidate iterator position */
3039 for_each_resource_rcu(tmp, &drbd_resources) {
3040 if (pos == NULL) {
3041 /* first iteration */
3042 pos = tmp;
3043 resource = pos;
3044 break;
3045 }
3046 if (tmp == pos) {
3047 resource = pos;
3048 break;
3049 }
3050 }
3051 if (resource) {
3052 next_resource:
3053 device = idr_get_next(&resource->devices, &volume);
3054 if (!device) {
3055 /* No more volumes to dump on this resource.
3056 * Advance resource iterator. */
3057 pos = list_entry_rcu(resource->resources.next,
3058 struct drbd_resource, resources);
3059 /* Did we dump any volume of this resource yet? */
3060 if (volume != 0) {
3061 /* If we reached the end of the list,
3062 * or only a single resource dump was requested,
3063 * we are done. */
3064 if (&pos->resources == &drbd_resources || cb->args[2])
3065 goto out;
3066 volume = 0;
3067 resource = pos;
3068 goto next_resource;
3069 }
3070 }
3071
3072 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
3073 cb->nlh->nlmsg_seq, &drbd_genl_family,
3074 NLM_F_MULTI, DRBD_ADM_GET_STATUS);
3075 if (!dh)
3076 goto out;
3077
3078 if (!device) {
3079 /* This is a connection without a single volume.
3080 * Suprisingly enough, it may have a network
3081 * configuration. */
3082 struct drbd_connection *connection;
3083
3084 dh->minor = -1U;
3085 dh->ret_code = NO_ERROR;
3086 connection = the_only_connection(resource);
3087 if (nla_put_drbd_cfg_context(skb, resource, connection, NULL))
3088 goto cancel;
3089 if (connection) {
3090 struct net_conf *nc;
3091
3092 nc = rcu_dereference(connection->net_conf);
3093 if (nc && net_conf_to_skb(skb, nc, 1) != 0)
3094 goto cancel;
3095 }
3096 goto done;
3097 }
3098
3099 D_ASSERT(device, device->vnr == volume);
3100 D_ASSERT(device, device->resource == resource);
3101
3102 dh->minor = device_to_minor(device);
3103 dh->ret_code = NO_ERROR;
3104
3105 if (nla_put_status_info(skb, device, NULL)) {
3106 cancel:
3107 genlmsg_cancel(skb, dh);
3108 goto out;
3109 }
3110 done:
3111 genlmsg_end(skb, dh);
3112 }
3113
3114 out:
3115 rcu_read_unlock();
3116 /* where to start the next iteration */
3117 cb->args[0] = (long)pos;
3118 cb->args[1] = (pos == resource) ? volume + 1 : 0;
3119
3120 /* No more resources/volumes/minors found results in an empty skb.
3121 * Which will terminate the dump. */
3122 return skb->len;
3123 }
3124
3125 /*
3126 * Request status of all resources, or of all volumes within a single resource.
3127 *
3128 * This is a dump, as the answer may not fit in a single reply skb otherwise.
3129 * Which means we cannot use the family->attrbuf or other such members, because
3130 * dump is NOT protected by the genl_lock(). During dump, we only have access
3131 * to the incoming skb, and need to opencode "parsing" of the nlattr payload.
3132 *
3133 * Once things are setup properly, we call into get_one_status().
3134 */
3135 int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb)
3136 {
3137 const unsigned hdrlen = GENL_HDRLEN + GENL_MAGIC_FAMILY_HDRSZ;
3138 struct nlattr *nla;
3139 const char *resource_name;
3140 struct drbd_resource *resource;
3141 int maxtype;
3142
3143 /* Is this a followup call? */
3144 if (cb->args[0]) {
3145 /* ... of a single resource dump,
3146 * and the resource iterator has been advanced already? */
3147 if (cb->args[2] && cb->args[2] != cb->args[0])
3148 return 0; /* DONE. */
3149 goto dump;
3150 }
3151
3152 /* First call (from netlink_dump_start). We need to figure out
3153 * which resource(s) the user wants us to dump. */
3154 nla = nla_find(nlmsg_attrdata(cb->nlh, hdrlen),
3155 nlmsg_attrlen(cb->nlh, hdrlen),
3156 DRBD_NLA_CFG_CONTEXT);
3157
3158 /* No explicit context given. Dump all. */
3159 if (!nla)
3160 goto dump;
3161 maxtype = ARRAY_SIZE(drbd_cfg_context_nl_policy) - 1;
3162 nla = drbd_nla_find_nested(maxtype, nla, __nla_type(T_ctx_resource_name));
3163 if (IS_ERR(nla))
3164 return PTR_ERR(nla);
3165 /* context given, but no name present? */
3166 if (!nla)
3167 return -EINVAL;
3168 resource_name = nla_data(nla);
3169 if (!*resource_name)
3170 return -ENODEV;
3171 resource = drbd_find_resource(resource_name);
3172 if (!resource)
3173 return -ENODEV;
3174
3175 kref_put(&resource->kref, drbd_destroy_resource); /* get_one_status() revalidates the resource */
3176
3177 /* prime iterators, and set "filter" mode mark:
3178 * only dump this connection. */
3179 cb->args[0] = (long)resource;
3180 /* cb->args[1] = 0; passed in this way. */
3181 cb->args[2] = (long)resource;
3182
3183 dump:
3184 return get_one_status(skb, cb);
3185 }
3186
3187 int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info)
3188 {
3189 struct drbd_config_context adm_ctx;
3190 enum drbd_ret_code retcode;
3191 struct timeout_parms tp;
3192 int err;
3193
3194 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3195 if (!adm_ctx.reply_skb)
3196 return retcode;
3197 if (retcode != NO_ERROR)
3198 goto out;
3199
3200 tp.timeout_type =
3201 adm_ctx.device->state.pdsk == D_OUTDATED ? UT_PEER_OUTDATED :
3202 test_bit(USE_DEGR_WFC_T, &adm_ctx.device->flags) ? UT_DEGRADED :
3203 UT_DEFAULT;
3204
3205 err = timeout_parms_to_priv_skb(adm_ctx.reply_skb, &tp);
3206 if (err) {
3207 nlmsg_free(adm_ctx.reply_skb);
3208 return err;
3209 }
3210 out:
3211 drbd_adm_finish(&adm_ctx, info, retcode);
3212 return 0;
3213 }
3214
3215 int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info)
3216 {
3217 struct drbd_config_context adm_ctx;
3218 struct drbd_device *device;
3219 enum drbd_ret_code retcode;
3220 struct start_ov_parms parms;
3221
3222 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3223 if (!adm_ctx.reply_skb)
3224 return retcode;
3225 if (retcode != NO_ERROR)
3226 goto out;
3227
3228 device = adm_ctx.device;
3229
3230 /* resume from last known position, if possible */
3231 parms.ov_start_sector = device->ov_start_sector;
3232 parms.ov_stop_sector = ULLONG_MAX;
3233 if (info->attrs[DRBD_NLA_START_OV_PARMS]) {
3234 int err = start_ov_parms_from_attrs(&parms, info);
3235 if (err) {
3236 retcode = ERR_MANDATORY_TAG;
3237 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
3238 goto out;
3239 }
3240 }
3241 mutex_lock(&adm_ctx.resource->adm_mutex);
3242
3243 /* w_make_ov_request expects position to be aligned */
3244 device->ov_start_sector = parms.ov_start_sector & ~(BM_SECT_PER_BIT-1);
3245 device->ov_stop_sector = parms.ov_stop_sector;
3246
3247 /* If there is still bitmap IO pending, e.g. previous resync or verify
3248 * just being finished, wait for it before requesting a new resync. */
3249 drbd_suspend_io(device);
3250 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
3251 retcode = drbd_request_state(device, NS(conn, C_VERIFY_S));
3252 drbd_resume_io(device);
3253
3254 mutex_unlock(&adm_ctx.resource->adm_mutex);
3255 out:
3256 drbd_adm_finish(&adm_ctx, info, retcode);
3257 return 0;
3258 }
3259
3260
3261 int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info)
3262 {
3263 struct drbd_config_context adm_ctx;
3264 struct drbd_device *device;
3265 enum drbd_ret_code retcode;
3266 int skip_initial_sync = 0;
3267 int err;
3268 struct new_c_uuid_parms args;
3269
3270 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3271 if (!adm_ctx.reply_skb)
3272 return retcode;
3273 if (retcode != NO_ERROR)
3274 goto out_nolock;
3275
3276 device = adm_ctx.device;
3277 memset(&args, 0, sizeof(args));
3278 if (info->attrs[DRBD_NLA_NEW_C_UUID_PARMS]) {
3279 err = new_c_uuid_parms_from_attrs(&args, info);
3280 if (err) {
3281 retcode = ERR_MANDATORY_TAG;
3282 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
3283 goto out_nolock;
3284 }
3285 }
3286
3287 mutex_lock(&adm_ctx.resource->adm_mutex);
3288 mutex_lock(device->state_mutex); /* Protects us against serialized state changes. */
3289
3290 if (!get_ldev(device)) {
3291 retcode = ERR_NO_DISK;
3292 goto out;
3293 }
3294
3295 /* this is "skip initial sync", assume to be clean */
3296 if (device->state.conn == C_CONNECTED &&
3297 first_peer_device(device)->connection->agreed_pro_version >= 90 &&
3298 device->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED && args.clear_bm) {
3299 drbd_info(device, "Preparing to skip initial sync\n");
3300 skip_initial_sync = 1;
3301 } else if (device->state.conn != C_STANDALONE) {
3302 retcode = ERR_CONNECTED;
3303 goto out_dec;
3304 }
3305
3306 drbd_uuid_set(device, UI_BITMAP, 0); /* Rotate UI_BITMAP to History 1, etc... */
3307 drbd_uuid_new_current(device); /* New current, previous to UI_BITMAP */
3308
3309 if (args.clear_bm) {
3310 err = drbd_bitmap_io(device, &drbd_bmio_clear_n_write,
3311 "clear_n_write from new_c_uuid", BM_LOCKED_MASK);
3312 if (err) {
3313 drbd_err(device, "Writing bitmap failed with %d\n", err);
3314 retcode = ERR_IO_MD_DISK;
3315 }
3316 if (skip_initial_sync) {
3317 drbd_send_uuids_skip_initial_sync(first_peer_device(device));
3318 _drbd_uuid_set(device, UI_BITMAP, 0);
3319 drbd_print_uuids(device, "cleared bitmap UUID");
3320 spin_lock_irq(&device->resource->req_lock);
3321 _drbd_set_state(_NS2(device, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
3322 CS_VERBOSE, NULL);
3323 spin_unlock_irq(&device->resource->req_lock);
3324 }
3325 }
3326
3327 drbd_md_sync(device);
3328 out_dec:
3329 put_ldev(device);
3330 out:
3331 mutex_unlock(device->state_mutex);
3332 mutex_unlock(&adm_ctx.resource->adm_mutex);
3333 out_nolock:
3334 drbd_adm_finish(&adm_ctx, info, retcode);
3335 return 0;
3336 }
3337
3338 static enum drbd_ret_code
3339 drbd_check_resource_name(struct drbd_config_context *adm_ctx)
3340 {
3341 const char *name = adm_ctx->resource_name;
3342 if (!name || !name[0]) {
3343 drbd_msg_put_info(adm_ctx->reply_skb, "resource name missing");
3344 return ERR_MANDATORY_TAG;
3345 }
3346 /* if we want to use these in sysfs/configfs/debugfs some day,
3347 * we must not allow slashes */
3348 if (strchr(name, '/')) {
3349 drbd_msg_put_info(adm_ctx->reply_skb, "invalid resource name");
3350 return ERR_INVALID_REQUEST;
3351 }
3352 return NO_ERROR;
3353 }
3354
3355 int drbd_adm_new_resource(struct sk_buff *skb, struct genl_info *info)
3356 {
3357 struct drbd_config_context adm_ctx;
3358 enum drbd_ret_code retcode;
3359 struct res_opts res_opts;
3360 int err;
3361
3362 retcode = drbd_adm_prepare(&adm_ctx, skb, info, 0);
3363 if (!adm_ctx.reply_skb)
3364 return retcode;
3365 if (retcode != NO_ERROR)
3366 goto out;
3367
3368 set_res_opts_defaults(&res_opts);
3369 err = res_opts_from_attrs(&res_opts, info);
3370 if (err && err != -ENOMSG) {
3371 retcode = ERR_MANDATORY_TAG;
3372 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
3373 goto out;
3374 }
3375
3376 retcode = drbd_check_resource_name(&adm_ctx);
3377 if (retcode != NO_ERROR)
3378 goto out;
3379
3380 if (adm_ctx.resource) {
3381 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL) {
3382 retcode = ERR_INVALID_REQUEST;
3383 drbd_msg_put_info(adm_ctx.reply_skb, "resource exists");
3384 }
3385 /* else: still NO_ERROR */
3386 goto out;
3387 }
3388
3389 /* not yet safe for genl_family.parallel_ops */
3390 if (!conn_create(adm_ctx.resource_name, &res_opts))
3391 retcode = ERR_NOMEM;
3392 out:
3393 drbd_adm_finish(&adm_ctx, info, retcode);
3394 return 0;
3395 }
3396
3397 int drbd_adm_new_minor(struct sk_buff *skb, struct genl_info *info)
3398 {
3399 struct drbd_config_context adm_ctx;
3400 struct drbd_genlmsghdr *dh = info->userhdr;
3401 enum drbd_ret_code retcode;
3402
3403 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
3404 if (!adm_ctx.reply_skb)
3405 return retcode;
3406 if (retcode != NO_ERROR)
3407 goto out;
3408
3409 if (dh->minor > MINORMASK) {
3410 drbd_msg_put_info(adm_ctx.reply_skb, "requested minor out of range");
3411 retcode = ERR_INVALID_REQUEST;
3412 goto out;
3413 }
3414 if (adm_ctx.volume > DRBD_VOLUME_MAX) {
3415 drbd_msg_put_info(adm_ctx.reply_skb, "requested volume id out of range");
3416 retcode = ERR_INVALID_REQUEST;
3417 goto out;
3418 }
3419
3420 /* drbd_adm_prepare made sure already
3421 * that first_peer_device(device)->connection and device->vnr match the request. */
3422 if (adm_ctx.device) {
3423 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
3424 retcode = ERR_MINOR_EXISTS;
3425 /* else: still NO_ERROR */
3426 goto out;
3427 }
3428
3429 mutex_lock(&adm_ctx.resource->adm_mutex);
3430 retcode = drbd_create_device(&adm_ctx, dh->minor);
3431 mutex_unlock(&adm_ctx.resource->adm_mutex);
3432 out:
3433 drbd_adm_finish(&adm_ctx, info, retcode);
3434 return 0;
3435 }
3436
3437 static enum drbd_ret_code adm_del_minor(struct drbd_device *device)
3438 {
3439 if (device->state.disk == D_DISKLESS &&
3440 /* no need to be device->state.conn == C_STANDALONE &&
3441 * we may want to delete a minor from a live replication group.
3442 */
3443 device->state.role == R_SECONDARY) {
3444 _drbd_request_state(device, NS(conn, C_WF_REPORT_PARAMS),
3445 CS_VERBOSE + CS_WAIT_COMPLETE);
3446 drbd_delete_device(device);
3447 return NO_ERROR;
3448 } else
3449 return ERR_MINOR_CONFIGURED;
3450 }
3451
3452 int drbd_adm_del_minor(struct sk_buff *skb, struct genl_info *info)
3453 {
3454 struct drbd_config_context adm_ctx;
3455 enum drbd_ret_code retcode;
3456
3457 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3458 if (!adm_ctx.reply_skb)
3459 return retcode;
3460 if (retcode != NO_ERROR)
3461 goto out;
3462
3463 mutex_lock(&adm_ctx.resource->adm_mutex);
3464 retcode = adm_del_minor(adm_ctx.device);
3465 mutex_unlock(&adm_ctx.resource->adm_mutex);
3466 out:
3467 drbd_adm_finish(&adm_ctx, info, retcode);
3468 return 0;
3469 }
3470
3471 int drbd_adm_down(struct sk_buff *skb, struct genl_info *info)
3472 {
3473 struct drbd_config_context adm_ctx;
3474 struct drbd_resource *resource;
3475 struct drbd_connection *connection;
3476 struct drbd_device *device;
3477 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
3478 unsigned i;
3479
3480 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
3481 if (!adm_ctx.reply_skb)
3482 return retcode;
3483 if (retcode != NO_ERROR)
3484 goto finish;
3485
3486 resource = adm_ctx.resource;
3487 mutex_lock(&resource->adm_mutex);
3488 /* demote */
3489 for_each_connection(connection, resource) {
3490 struct drbd_peer_device *peer_device;
3491
3492 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
3493 retcode = drbd_set_role(peer_device->device, R_SECONDARY, 0);
3494 if (retcode < SS_SUCCESS) {
3495 drbd_msg_put_info(adm_ctx.reply_skb, "failed to demote");
3496 goto out;
3497 }
3498 }
3499
3500 retcode = conn_try_disconnect(connection, 0);
3501 if (retcode < SS_SUCCESS) {
3502 drbd_msg_put_info(adm_ctx.reply_skb, "failed to disconnect");
3503 goto out;
3504 }
3505 }
3506
3507 /* detach */
3508 idr_for_each_entry(&resource->devices, device, i) {
3509 retcode = adm_detach(device, 0);
3510 if (retcode < SS_SUCCESS || retcode > NO_ERROR) {
3511 drbd_msg_put_info(adm_ctx.reply_skb, "failed to detach");
3512 goto out;
3513 }
3514 }
3515
3516 /* If we reach this, all volumes (of this connection) are Secondary,
3517 * Disconnected, Diskless, aka Unconfigured. Make sure all threads have
3518 * actually stopped, state handling only does drbd_thread_stop_nowait(). */
3519 for_each_connection(connection, resource)
3520 drbd_thread_stop(&connection->worker);
3521
3522 /* Now, nothing can fail anymore */
3523
3524 /* delete volumes */
3525 idr_for_each_entry(&resource->devices, device, i) {
3526 retcode = adm_del_minor(device);
3527 if (retcode != NO_ERROR) {
3528 /* "can not happen" */
3529 drbd_msg_put_info(adm_ctx.reply_skb, "failed to delete volume");
3530 goto out;
3531 }
3532 }
3533
3534 list_del_rcu(&resource->resources);
3535 synchronize_rcu();
3536 drbd_free_resource(resource);
3537 retcode = NO_ERROR;
3538 out:
3539 mutex_unlock(&resource->adm_mutex);
3540 finish:
3541 drbd_adm_finish(&adm_ctx, info, retcode);
3542 return 0;
3543 }
3544
3545 int drbd_adm_del_resource(struct sk_buff *skb, struct genl_info *info)
3546 {
3547 struct drbd_config_context adm_ctx;
3548 struct drbd_resource *resource;
3549 struct drbd_connection *connection;
3550 enum drbd_ret_code retcode;
3551
3552 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
3553 if (!adm_ctx.reply_skb)
3554 return retcode;
3555 if (retcode != NO_ERROR)
3556 goto finish;
3557
3558 resource = adm_ctx.resource;
3559 mutex_lock(&resource->adm_mutex);
3560 for_each_connection(connection, resource) {
3561 if (connection->cstate > C_STANDALONE) {
3562 retcode = ERR_NET_CONFIGURED;
3563 goto out;
3564 }
3565 }
3566 if (!idr_is_empty(&resource->devices)) {
3567 retcode = ERR_RES_IN_USE;
3568 goto out;
3569 }
3570
3571 list_del_rcu(&resource->resources);
3572 for_each_connection(connection, resource)
3573 drbd_thread_stop(&connection->worker);
3574 synchronize_rcu();
3575 drbd_free_resource(resource);
3576 retcode = NO_ERROR;
3577 out:
3578 mutex_unlock(&resource->adm_mutex);
3579 finish:
3580 drbd_adm_finish(&adm_ctx, info, retcode);
3581 return 0;
3582 }
3583
3584 void drbd_bcast_event(struct drbd_device *device, const struct sib_info *sib)
3585 {
3586 static atomic_t drbd_genl_seq = ATOMIC_INIT(2); /* two. */
3587 struct sk_buff *msg;
3588 struct drbd_genlmsghdr *d_out;
3589 unsigned seq;
3590 int err = -ENOMEM;
3591
3592 if (sib->sib_reason == SIB_SYNC_PROGRESS) {
3593 if (time_after(jiffies, device->rs_last_bcast + HZ))
3594 device->rs_last_bcast = jiffies;
3595 else
3596 return;
3597 }
3598
3599 seq = atomic_inc_return(&drbd_genl_seq);
3600 msg = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
3601 if (!msg)
3602 goto failed;
3603
3604 err = -EMSGSIZE;
3605 d_out = genlmsg_put(msg, 0, seq, &drbd_genl_family, 0, DRBD_EVENT);
3606 if (!d_out) /* cannot happen, but anyways. */
3607 goto nla_put_failure;
3608 d_out->minor = device_to_minor(device);
3609 d_out->ret_code = NO_ERROR;
3610
3611 if (nla_put_status_info(msg, device, sib))
3612 goto nla_put_failure;
3613 genlmsg_end(msg, d_out);
3614 err = drbd_genl_multicast_events(msg, 0);
3615 /* msg has been consumed or freed in netlink_broadcast() */
3616 if (err && err != -ESRCH)
3617 goto failed;
3618
3619 return;
3620
3621 nla_put_failure:
3622 nlmsg_free(msg);
3623 failed:
3624 drbd_err(device, "Error %d while broadcasting event. "
3625 "Event seq:%u sib_reason:%u\n",
3626 err, seq, sib->sib_reason);
3627 }