drbd: possibly disable discard support, if backend has discard_zeroes_data=0
[deliverable/linux.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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27
28 #include <linux/module.h>
29 #include <linux/drbd.h>
30 #include <linux/in.h>
31 #include <linux/fs.h>
32 #include <linux/file.h>
33 #include <linux/slab.h>
34 #include <linux/blkpg.h>
35 #include <linux/cpumask.h>
36 #include "drbd_int.h"
37 #include "drbd_protocol.h"
38 #include "drbd_req.h"
39 #include "drbd_state_change.h"
40 #include <asm/unaligned.h>
41 #include <linux/drbd_limits.h>
42 #include <linux/kthread.h>
43
44 #include <net/genetlink.h>
45
46 /* .doit */
47 // int drbd_adm_create_resource(struct sk_buff *skb, struct genl_info *info);
48 // int drbd_adm_delete_resource(struct sk_buff *skb, struct genl_info *info);
49
50 int drbd_adm_new_minor(struct sk_buff *skb, struct genl_info *info);
51 int drbd_adm_del_minor(struct sk_buff *skb, struct genl_info *info);
52
53 int drbd_adm_new_resource(struct sk_buff *skb, struct genl_info *info);
54 int drbd_adm_del_resource(struct sk_buff *skb, struct genl_info *info);
55 int drbd_adm_down(struct sk_buff *skb, struct genl_info *info);
56
57 int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info);
58 int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info);
59 int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info);
60 int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info);
61 int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info);
62 int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info);
63 int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info);
64 int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info);
65 int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info);
66 int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info);
67 int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info);
68 int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info);
69 int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info);
70 int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info);
71 int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info);
72 int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info);
73 int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info);
74 int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info);
75 int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info);
76 int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info);
77 /* .dumpit */
78 int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb);
79 int drbd_adm_dump_resources(struct sk_buff *skb, struct netlink_callback *cb);
80 int drbd_adm_dump_devices(struct sk_buff *skb, struct netlink_callback *cb);
81 int drbd_adm_dump_devices_done(struct netlink_callback *cb);
82 int drbd_adm_dump_connections(struct sk_buff *skb, struct netlink_callback *cb);
83 int drbd_adm_dump_connections_done(struct netlink_callback *cb);
84 int drbd_adm_dump_peer_devices(struct sk_buff *skb, struct netlink_callback *cb);
85 int drbd_adm_dump_peer_devices_done(struct netlink_callback *cb);
86 int drbd_adm_get_initial_state(struct sk_buff *skb, struct netlink_callback *cb);
87
88 #include <linux/drbd_genl_api.h>
89 #include "drbd_nla.h"
90 #include <linux/genl_magic_func.h>
91
92 static atomic_t drbd_genl_seq = ATOMIC_INIT(2); /* two. */
93 static atomic_t notify_genl_seq = ATOMIC_INIT(2); /* two. */
94
95 DEFINE_MUTEX(notification_mutex);
96
97 /* used blkdev_get_by_path, to claim our meta data device(s) */
98 static char *drbd_m_holder = "Hands off! this is DRBD's meta data device.";
99
100 static void drbd_adm_send_reply(struct sk_buff *skb, struct genl_info *info)
101 {
102 genlmsg_end(skb, genlmsg_data(nlmsg_data(nlmsg_hdr(skb))));
103 if (genlmsg_reply(skb, info))
104 pr_err("error sending genl reply\n");
105 }
106
107 /* Used on a fresh "drbd_adm_prepare"d reply_skb, this cannot fail: The only
108 * reason it could fail was no space in skb, and there are 4k available. */
109 static int drbd_msg_put_info(struct sk_buff *skb, const char *info)
110 {
111 struct nlattr *nla;
112 int err = -EMSGSIZE;
113
114 if (!info || !info[0])
115 return 0;
116
117 nla = nla_nest_start(skb, DRBD_NLA_CFG_REPLY);
118 if (!nla)
119 return err;
120
121 err = nla_put_string(skb, T_info_text, info);
122 if (err) {
123 nla_nest_cancel(skb, nla);
124 return err;
125 } else
126 nla_nest_end(skb, nla);
127 return 0;
128 }
129
130 /* This would be a good candidate for a "pre_doit" hook,
131 * and per-family private info->pointers.
132 * But we need to stay compatible with older kernels.
133 * If it returns successfully, adm_ctx members are valid.
134 *
135 * At this point, we still rely on the global genl_lock().
136 * If we want to avoid that, and allow "genl_family.parallel_ops", we may need
137 * to add additional synchronization against object destruction/modification.
138 */
139 #define DRBD_ADM_NEED_MINOR 1
140 #define DRBD_ADM_NEED_RESOURCE 2
141 #define DRBD_ADM_NEED_CONNECTION 4
142 static int drbd_adm_prepare(struct drbd_config_context *adm_ctx,
143 struct sk_buff *skb, struct genl_info *info, unsigned flags)
144 {
145 struct drbd_genlmsghdr *d_in = info->userhdr;
146 const u8 cmd = info->genlhdr->cmd;
147 int err;
148
149 memset(adm_ctx, 0, sizeof(*adm_ctx));
150
151 /* genl_rcv_msg only checks for CAP_NET_ADMIN on "GENL_ADMIN_PERM" :( */
152 if (cmd != DRBD_ADM_GET_STATUS && !capable(CAP_NET_ADMIN))
153 return -EPERM;
154
155 adm_ctx->reply_skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
156 if (!adm_ctx->reply_skb) {
157 err = -ENOMEM;
158 goto fail;
159 }
160
161 adm_ctx->reply_dh = genlmsg_put_reply(adm_ctx->reply_skb,
162 info, &drbd_genl_family, 0, cmd);
163 /* put of a few bytes into a fresh skb of >= 4k will always succeed.
164 * but anyways */
165 if (!adm_ctx->reply_dh) {
166 err = -ENOMEM;
167 goto fail;
168 }
169
170 adm_ctx->reply_dh->minor = d_in->minor;
171 adm_ctx->reply_dh->ret_code = NO_ERROR;
172
173 adm_ctx->volume = VOLUME_UNSPECIFIED;
174 if (info->attrs[DRBD_NLA_CFG_CONTEXT]) {
175 struct nlattr *nla;
176 /* parse and validate only */
177 err = drbd_cfg_context_from_attrs(NULL, info);
178 if (err)
179 goto fail;
180
181 /* It was present, and valid,
182 * copy it over to the reply skb. */
183 err = nla_put_nohdr(adm_ctx->reply_skb,
184 info->attrs[DRBD_NLA_CFG_CONTEXT]->nla_len,
185 info->attrs[DRBD_NLA_CFG_CONTEXT]);
186 if (err)
187 goto fail;
188
189 /* and assign stuff to the adm_ctx */
190 nla = nested_attr_tb[__nla_type(T_ctx_volume)];
191 if (nla)
192 adm_ctx->volume = nla_get_u32(nla);
193 nla = nested_attr_tb[__nla_type(T_ctx_resource_name)];
194 if (nla)
195 adm_ctx->resource_name = nla_data(nla);
196 adm_ctx->my_addr = nested_attr_tb[__nla_type(T_ctx_my_addr)];
197 adm_ctx->peer_addr = nested_attr_tb[__nla_type(T_ctx_peer_addr)];
198 if ((adm_ctx->my_addr &&
199 nla_len(adm_ctx->my_addr) > sizeof(adm_ctx->connection->my_addr)) ||
200 (adm_ctx->peer_addr &&
201 nla_len(adm_ctx->peer_addr) > sizeof(adm_ctx->connection->peer_addr))) {
202 err = -EINVAL;
203 goto fail;
204 }
205 }
206
207 adm_ctx->minor = d_in->minor;
208 adm_ctx->device = minor_to_device(d_in->minor);
209
210 /* We are protected by the global genl_lock().
211 * But we may explicitly drop it/retake it in drbd_adm_set_role(),
212 * so make sure this object stays around. */
213 if (adm_ctx->device)
214 kref_get(&adm_ctx->device->kref);
215
216 if (adm_ctx->resource_name) {
217 adm_ctx->resource = drbd_find_resource(adm_ctx->resource_name);
218 }
219
220 if (!adm_ctx->device && (flags & DRBD_ADM_NEED_MINOR)) {
221 drbd_msg_put_info(adm_ctx->reply_skb, "unknown minor");
222 return ERR_MINOR_INVALID;
223 }
224 if (!adm_ctx->resource && (flags & DRBD_ADM_NEED_RESOURCE)) {
225 drbd_msg_put_info(adm_ctx->reply_skb, "unknown resource");
226 if (adm_ctx->resource_name)
227 return ERR_RES_NOT_KNOWN;
228 return ERR_INVALID_REQUEST;
229 }
230
231 if (flags & DRBD_ADM_NEED_CONNECTION) {
232 if (adm_ctx->resource) {
233 drbd_msg_put_info(adm_ctx->reply_skb, "no resource name expected");
234 return ERR_INVALID_REQUEST;
235 }
236 if (adm_ctx->device) {
237 drbd_msg_put_info(adm_ctx->reply_skb, "no minor number expected");
238 return ERR_INVALID_REQUEST;
239 }
240 if (adm_ctx->my_addr && adm_ctx->peer_addr)
241 adm_ctx->connection = conn_get_by_addrs(nla_data(adm_ctx->my_addr),
242 nla_len(adm_ctx->my_addr),
243 nla_data(adm_ctx->peer_addr),
244 nla_len(adm_ctx->peer_addr));
245 if (!adm_ctx->connection) {
246 drbd_msg_put_info(adm_ctx->reply_skb, "unknown connection");
247 return ERR_INVALID_REQUEST;
248 }
249 }
250
251 /* some more paranoia, if the request was over-determined */
252 if (adm_ctx->device && adm_ctx->resource &&
253 adm_ctx->device->resource != adm_ctx->resource) {
254 pr_warning("request: minor=%u, resource=%s; but that minor belongs to resource %s\n",
255 adm_ctx->minor, adm_ctx->resource->name,
256 adm_ctx->device->resource->name);
257 drbd_msg_put_info(adm_ctx->reply_skb, "minor exists in different resource");
258 return ERR_INVALID_REQUEST;
259 }
260 if (adm_ctx->device &&
261 adm_ctx->volume != VOLUME_UNSPECIFIED &&
262 adm_ctx->volume != adm_ctx->device->vnr) {
263 pr_warning("request: minor=%u, volume=%u; but that minor is volume %u in %s\n",
264 adm_ctx->minor, adm_ctx->volume,
265 adm_ctx->device->vnr,
266 adm_ctx->device->resource->name);
267 drbd_msg_put_info(adm_ctx->reply_skb, "minor exists as different volume");
268 return ERR_INVALID_REQUEST;
269 }
270
271 /* still, provide adm_ctx->resource always, if possible. */
272 if (!adm_ctx->resource) {
273 adm_ctx->resource = adm_ctx->device ? adm_ctx->device->resource
274 : adm_ctx->connection ? adm_ctx->connection->resource : NULL;
275 if (adm_ctx->resource)
276 kref_get(&adm_ctx->resource->kref);
277 }
278
279 return NO_ERROR;
280
281 fail:
282 nlmsg_free(adm_ctx->reply_skb);
283 adm_ctx->reply_skb = NULL;
284 return err;
285 }
286
287 static int drbd_adm_finish(struct drbd_config_context *adm_ctx,
288 struct genl_info *info, int retcode)
289 {
290 if (adm_ctx->device) {
291 kref_put(&adm_ctx->device->kref, drbd_destroy_device);
292 adm_ctx->device = NULL;
293 }
294 if (adm_ctx->connection) {
295 kref_put(&adm_ctx->connection->kref, &drbd_destroy_connection);
296 adm_ctx->connection = NULL;
297 }
298 if (adm_ctx->resource) {
299 kref_put(&adm_ctx->resource->kref, drbd_destroy_resource);
300 adm_ctx->resource = NULL;
301 }
302
303 if (!adm_ctx->reply_skb)
304 return -ENOMEM;
305
306 adm_ctx->reply_dh->ret_code = retcode;
307 drbd_adm_send_reply(adm_ctx->reply_skb, info);
308 return 0;
309 }
310
311 static void setup_khelper_env(struct drbd_connection *connection, char **envp)
312 {
313 char *afs;
314
315 /* FIXME: A future version will not allow this case. */
316 if (connection->my_addr_len == 0 || connection->peer_addr_len == 0)
317 return;
318
319 switch (((struct sockaddr *)&connection->peer_addr)->sa_family) {
320 case AF_INET6:
321 afs = "ipv6";
322 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI6",
323 &((struct sockaddr_in6 *)&connection->peer_addr)->sin6_addr);
324 break;
325 case AF_INET:
326 afs = "ipv4";
327 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
328 &((struct sockaddr_in *)&connection->peer_addr)->sin_addr);
329 break;
330 default:
331 afs = "ssocks";
332 snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
333 &((struct sockaddr_in *)&connection->peer_addr)->sin_addr);
334 }
335 snprintf(envp[3], 20, "DRBD_PEER_AF=%s", afs);
336 }
337
338 int drbd_khelper(struct drbd_device *device, char *cmd)
339 {
340 char *envp[] = { "HOME=/",
341 "TERM=linux",
342 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
343 (char[20]) { }, /* address family */
344 (char[60]) { }, /* address */
345 NULL };
346 char mb[14];
347 char *argv[] = {usermode_helper, cmd, mb, NULL };
348 struct drbd_connection *connection = first_peer_device(device)->connection;
349 struct sib_info sib;
350 int ret;
351
352 if (current == connection->worker.task)
353 set_bit(CALLBACK_PENDING, &connection->flags);
354
355 snprintf(mb, 14, "minor-%d", device_to_minor(device));
356 setup_khelper_env(connection, envp);
357
358 /* The helper may take some time.
359 * write out any unsynced meta data changes now */
360 drbd_md_sync(device);
361
362 drbd_info(device, "helper command: %s %s %s\n", usermode_helper, cmd, mb);
363 sib.sib_reason = SIB_HELPER_PRE;
364 sib.helper_name = cmd;
365 drbd_bcast_event(device, &sib);
366 notify_helper(NOTIFY_CALL, device, connection, cmd, 0);
367 ret = call_usermodehelper(usermode_helper, argv, envp, UMH_WAIT_PROC);
368 if (ret)
369 drbd_warn(device, "helper command: %s %s %s exit code %u (0x%x)\n",
370 usermode_helper, cmd, mb,
371 (ret >> 8) & 0xff, ret);
372 else
373 drbd_info(device, "helper command: %s %s %s exit code %u (0x%x)\n",
374 usermode_helper, cmd, mb,
375 (ret >> 8) & 0xff, ret);
376 sib.sib_reason = SIB_HELPER_POST;
377 sib.helper_exit_code = ret;
378 drbd_bcast_event(device, &sib);
379 notify_helper(NOTIFY_RESPONSE, device, connection, cmd, ret);
380
381 if (current == connection->worker.task)
382 clear_bit(CALLBACK_PENDING, &connection->flags);
383
384 if (ret < 0) /* Ignore any ERRNOs we got. */
385 ret = 0;
386
387 return ret;
388 }
389
390 static int conn_khelper(struct drbd_connection *connection, char *cmd)
391 {
392 char *envp[] = { "HOME=/",
393 "TERM=linux",
394 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
395 (char[20]) { }, /* address family */
396 (char[60]) { }, /* address */
397 NULL };
398 char *resource_name = connection->resource->name;
399 char *argv[] = {usermode_helper, cmd, resource_name, NULL };
400 int ret;
401
402 setup_khelper_env(connection, envp);
403 conn_md_sync(connection);
404
405 drbd_info(connection, "helper command: %s %s %s\n", usermode_helper, cmd, resource_name);
406 /* TODO: conn_bcast_event() ?? */
407 notify_helper(NOTIFY_CALL, NULL, connection, cmd, 0);
408
409 ret = call_usermodehelper(usermode_helper, argv, envp, UMH_WAIT_PROC);
410 if (ret)
411 drbd_warn(connection, "helper command: %s %s %s exit code %u (0x%x)\n",
412 usermode_helper, cmd, resource_name,
413 (ret >> 8) & 0xff, ret);
414 else
415 drbd_info(connection, "helper command: %s %s %s exit code %u (0x%x)\n",
416 usermode_helper, cmd, resource_name,
417 (ret >> 8) & 0xff, ret);
418 /* TODO: conn_bcast_event() ?? */
419 notify_helper(NOTIFY_RESPONSE, NULL, connection, cmd, ret);
420
421 if (ret < 0) /* Ignore any ERRNOs we got. */
422 ret = 0;
423
424 return ret;
425 }
426
427 static enum drbd_fencing_p highest_fencing_policy(struct drbd_connection *connection)
428 {
429 enum drbd_fencing_p fp = FP_NOT_AVAIL;
430 struct drbd_peer_device *peer_device;
431 int vnr;
432
433 rcu_read_lock();
434 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
435 struct drbd_device *device = peer_device->device;
436 if (get_ldev_if_state(device, D_CONSISTENT)) {
437 struct disk_conf *disk_conf =
438 rcu_dereference(peer_device->device->ldev->disk_conf);
439 fp = max_t(enum drbd_fencing_p, fp, disk_conf->fencing);
440 put_ldev(device);
441 }
442 }
443 rcu_read_unlock();
444
445 if (fp == FP_NOT_AVAIL) {
446 /* IO Suspending works on the whole resource.
447 Do it only for one device. */
448 vnr = 0;
449 peer_device = idr_get_next(&connection->peer_devices, &vnr);
450 drbd_change_state(peer_device->device, CS_VERBOSE | CS_HARD, NS(susp_fen, 0));
451 }
452
453 return fp;
454 }
455
456 bool conn_try_outdate_peer(struct drbd_connection *connection)
457 {
458 unsigned int connect_cnt;
459 union drbd_state mask = { };
460 union drbd_state val = { };
461 enum drbd_fencing_p fp;
462 char *ex_to_string;
463 int r;
464
465 spin_lock_irq(&connection->resource->req_lock);
466 if (connection->cstate >= C_WF_REPORT_PARAMS) {
467 drbd_err(connection, "Expected cstate < C_WF_REPORT_PARAMS\n");
468 spin_unlock_irq(&connection->resource->req_lock);
469 return false;
470 }
471
472 connect_cnt = connection->connect_cnt;
473 spin_unlock_irq(&connection->resource->req_lock);
474
475 fp = highest_fencing_policy(connection);
476 switch (fp) {
477 case FP_NOT_AVAIL:
478 drbd_warn(connection, "Not fencing peer, I'm not even Consistent myself.\n");
479 goto out;
480 case FP_DONT_CARE:
481 return true;
482 default: ;
483 }
484
485 r = conn_khelper(connection, "fence-peer");
486
487 switch ((r>>8) & 0xff) {
488 case 3: /* peer is inconsistent */
489 ex_to_string = "peer is inconsistent or worse";
490 mask.pdsk = D_MASK;
491 val.pdsk = D_INCONSISTENT;
492 break;
493 case 4: /* peer got outdated, or was already outdated */
494 ex_to_string = "peer was fenced";
495 mask.pdsk = D_MASK;
496 val.pdsk = D_OUTDATED;
497 break;
498 case 5: /* peer was down */
499 if (conn_highest_disk(connection) == D_UP_TO_DATE) {
500 /* we will(have) create(d) a new UUID anyways... */
501 ex_to_string = "peer is unreachable, assumed to be dead";
502 mask.pdsk = D_MASK;
503 val.pdsk = D_OUTDATED;
504 } else {
505 ex_to_string = "peer unreachable, doing nothing since disk != UpToDate";
506 }
507 break;
508 case 6: /* Peer is primary, voluntarily outdate myself.
509 * This is useful when an unconnected R_SECONDARY is asked to
510 * become R_PRIMARY, but finds the other peer being active. */
511 ex_to_string = "peer is active";
512 drbd_warn(connection, "Peer is primary, outdating myself.\n");
513 mask.disk = D_MASK;
514 val.disk = D_OUTDATED;
515 break;
516 case 7:
517 if (fp != FP_STONITH)
518 drbd_err(connection, "fence-peer() = 7 && fencing != Stonith !!!\n");
519 ex_to_string = "peer was stonithed";
520 mask.pdsk = D_MASK;
521 val.pdsk = D_OUTDATED;
522 break;
523 default:
524 /* The script is broken ... */
525 drbd_err(connection, "fence-peer helper broken, returned %d\n", (r>>8)&0xff);
526 return false; /* Eventually leave IO frozen */
527 }
528
529 drbd_info(connection, "fence-peer helper returned %d (%s)\n",
530 (r>>8) & 0xff, ex_to_string);
531
532 out:
533
534 /* Not using
535 conn_request_state(connection, mask, val, CS_VERBOSE);
536 here, because we might were able to re-establish the connection in the
537 meantime. */
538 spin_lock_irq(&connection->resource->req_lock);
539 if (connection->cstate < C_WF_REPORT_PARAMS && !test_bit(STATE_SENT, &connection->flags)) {
540 if (connection->connect_cnt != connect_cnt)
541 /* In case the connection was established and droped
542 while the fence-peer handler was running, ignore it */
543 drbd_info(connection, "Ignoring fence-peer exit code\n");
544 else
545 _conn_request_state(connection, mask, val, CS_VERBOSE);
546 }
547 spin_unlock_irq(&connection->resource->req_lock);
548
549 return conn_highest_pdsk(connection) <= D_OUTDATED;
550 }
551
552 static int _try_outdate_peer_async(void *data)
553 {
554 struct drbd_connection *connection = (struct drbd_connection *)data;
555
556 conn_try_outdate_peer(connection);
557
558 kref_put(&connection->kref, drbd_destroy_connection);
559 return 0;
560 }
561
562 void conn_try_outdate_peer_async(struct drbd_connection *connection)
563 {
564 struct task_struct *opa;
565
566 kref_get(&connection->kref);
567 /* We may just have force_sig()'ed this thread
568 * to get it out of some blocking network function.
569 * Clear signals; otherwise kthread_run(), which internally uses
570 * wait_on_completion_killable(), will mistake our pending signal
571 * for a new fatal signal and fail. */
572 flush_signals(current);
573 opa = kthread_run(_try_outdate_peer_async, connection, "drbd_async_h");
574 if (IS_ERR(opa)) {
575 drbd_err(connection, "out of mem, failed to invoke fence-peer helper\n");
576 kref_put(&connection->kref, drbd_destroy_connection);
577 }
578 }
579
580 enum drbd_state_rv
581 drbd_set_role(struct drbd_device *const device, enum drbd_role new_role, int force)
582 {
583 struct drbd_peer_device *const peer_device = first_peer_device(device);
584 struct drbd_connection *const connection = peer_device ? peer_device->connection : NULL;
585 const int max_tries = 4;
586 enum drbd_state_rv rv = SS_UNKNOWN_ERROR;
587 struct net_conf *nc;
588 int try = 0;
589 int forced = 0;
590 union drbd_state mask, val;
591
592 if (new_role == R_PRIMARY) {
593 struct drbd_connection *connection;
594
595 /* Detect dead peers as soon as possible. */
596
597 rcu_read_lock();
598 for_each_connection(connection, device->resource)
599 request_ping(connection);
600 rcu_read_unlock();
601 }
602
603 mutex_lock(device->state_mutex);
604
605 mask.i = 0; mask.role = R_MASK;
606 val.i = 0; val.role = new_role;
607
608 while (try++ < max_tries) {
609 rv = _drbd_request_state_holding_state_mutex(device, mask, val, CS_WAIT_COMPLETE);
610
611 /* in case we first succeeded to outdate,
612 * but now suddenly could establish a connection */
613 if (rv == SS_CW_FAILED_BY_PEER && mask.pdsk != 0) {
614 val.pdsk = 0;
615 mask.pdsk = 0;
616 continue;
617 }
618
619 if (rv == SS_NO_UP_TO_DATE_DISK && force &&
620 (device->state.disk < D_UP_TO_DATE &&
621 device->state.disk >= D_INCONSISTENT)) {
622 mask.disk = D_MASK;
623 val.disk = D_UP_TO_DATE;
624 forced = 1;
625 continue;
626 }
627
628 if (rv == SS_NO_UP_TO_DATE_DISK &&
629 device->state.disk == D_CONSISTENT && mask.pdsk == 0) {
630 D_ASSERT(device, device->state.pdsk == D_UNKNOWN);
631
632 if (conn_try_outdate_peer(connection)) {
633 val.disk = D_UP_TO_DATE;
634 mask.disk = D_MASK;
635 }
636 continue;
637 }
638
639 if (rv == SS_NOTHING_TO_DO)
640 goto out;
641 if (rv == SS_PRIMARY_NOP && mask.pdsk == 0) {
642 if (!conn_try_outdate_peer(connection) && force) {
643 drbd_warn(device, "Forced into split brain situation!\n");
644 mask.pdsk = D_MASK;
645 val.pdsk = D_OUTDATED;
646
647 }
648 continue;
649 }
650 if (rv == SS_TWO_PRIMARIES) {
651 /* Maybe the peer is detected as dead very soon...
652 retry at most once more in this case. */
653 int timeo;
654 rcu_read_lock();
655 nc = rcu_dereference(connection->net_conf);
656 timeo = nc ? (nc->ping_timeo + 1) * HZ / 10 : 1;
657 rcu_read_unlock();
658 schedule_timeout_interruptible(timeo);
659 if (try < max_tries)
660 try = max_tries - 1;
661 continue;
662 }
663 if (rv < SS_SUCCESS) {
664 rv = _drbd_request_state(device, mask, val,
665 CS_VERBOSE + CS_WAIT_COMPLETE);
666 if (rv < SS_SUCCESS)
667 goto out;
668 }
669 break;
670 }
671
672 if (rv < SS_SUCCESS)
673 goto out;
674
675 if (forced)
676 drbd_warn(device, "Forced to consider local data as UpToDate!\n");
677
678 /* Wait until nothing is on the fly :) */
679 wait_event(device->misc_wait, atomic_read(&device->ap_pending_cnt) == 0);
680
681 /* FIXME also wait for all pending P_BARRIER_ACK? */
682
683 if (new_role == R_SECONDARY) {
684 if (get_ldev(device)) {
685 device->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
686 put_ldev(device);
687 }
688 } else {
689 mutex_lock(&device->resource->conf_update);
690 nc = connection->net_conf;
691 if (nc)
692 nc->discard_my_data = 0; /* without copy; single bit op is atomic */
693 mutex_unlock(&device->resource->conf_update);
694
695 if (get_ldev(device)) {
696 if (((device->state.conn < C_CONNECTED ||
697 device->state.pdsk <= D_FAILED)
698 && device->ldev->md.uuid[UI_BITMAP] == 0) || forced)
699 drbd_uuid_new_current(device);
700
701 device->ldev->md.uuid[UI_CURRENT] |= (u64)1;
702 put_ldev(device);
703 }
704 }
705
706 /* writeout of activity log covered areas of the bitmap
707 * to stable storage done in after state change already */
708
709 if (device->state.conn >= C_WF_REPORT_PARAMS) {
710 /* if this was forced, we should consider sync */
711 if (forced)
712 drbd_send_uuids(peer_device);
713 drbd_send_current_state(peer_device);
714 }
715
716 drbd_md_sync(device);
717 set_disk_ro(device->vdisk, new_role == R_SECONDARY);
718 kobject_uevent(&disk_to_dev(device->vdisk)->kobj, KOBJ_CHANGE);
719 out:
720 mutex_unlock(device->state_mutex);
721 return rv;
722 }
723
724 static const char *from_attrs_err_to_txt(int err)
725 {
726 return err == -ENOMSG ? "required attribute missing" :
727 err == -EOPNOTSUPP ? "unknown mandatory attribute" :
728 err == -EEXIST ? "can not change invariant setting" :
729 "invalid attribute value";
730 }
731
732 int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info)
733 {
734 struct drbd_config_context adm_ctx;
735 struct set_role_parms parms;
736 int err;
737 enum drbd_ret_code retcode;
738
739 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
740 if (!adm_ctx.reply_skb)
741 return retcode;
742 if (retcode != NO_ERROR)
743 goto out;
744
745 memset(&parms, 0, sizeof(parms));
746 if (info->attrs[DRBD_NLA_SET_ROLE_PARMS]) {
747 err = set_role_parms_from_attrs(&parms, info);
748 if (err) {
749 retcode = ERR_MANDATORY_TAG;
750 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
751 goto out;
752 }
753 }
754 genl_unlock();
755 mutex_lock(&adm_ctx.resource->adm_mutex);
756
757 if (info->genlhdr->cmd == DRBD_ADM_PRIMARY)
758 retcode = drbd_set_role(adm_ctx.device, R_PRIMARY, parms.assume_uptodate);
759 else
760 retcode = drbd_set_role(adm_ctx.device, R_SECONDARY, 0);
761
762 mutex_unlock(&adm_ctx.resource->adm_mutex);
763 genl_lock();
764 out:
765 drbd_adm_finish(&adm_ctx, info, retcode);
766 return 0;
767 }
768
769 /* Initializes the md.*_offset members, so we are able to find
770 * the on disk meta data.
771 *
772 * We currently have two possible layouts:
773 * external:
774 * |----------- md_size_sect ------------------|
775 * [ 4k superblock ][ activity log ][ Bitmap ]
776 * | al_offset == 8 |
777 * | bm_offset = al_offset + X |
778 * ==> bitmap sectors = md_size_sect - bm_offset
779 *
780 * internal:
781 * |----------- md_size_sect ------------------|
782 * [data.....][ Bitmap ][ activity log ][ 4k superblock ]
783 * | al_offset < 0 |
784 * | bm_offset = al_offset - Y |
785 * ==> bitmap sectors = Y = al_offset - bm_offset
786 *
787 * Activity log size used to be fixed 32kB,
788 * but is about to become configurable.
789 */
790 static void drbd_md_set_sector_offsets(struct drbd_device *device,
791 struct drbd_backing_dev *bdev)
792 {
793 sector_t md_size_sect = 0;
794 unsigned int al_size_sect = bdev->md.al_size_4k * 8;
795
796 bdev->md.md_offset = drbd_md_ss(bdev);
797
798 switch (bdev->md.meta_dev_idx) {
799 default:
800 /* v07 style fixed size indexed meta data */
801 bdev->md.md_size_sect = MD_128MB_SECT;
802 bdev->md.al_offset = MD_4kB_SECT;
803 bdev->md.bm_offset = MD_4kB_SECT + al_size_sect;
804 break;
805 case DRBD_MD_INDEX_FLEX_EXT:
806 /* just occupy the full device; unit: sectors */
807 bdev->md.md_size_sect = drbd_get_capacity(bdev->md_bdev);
808 bdev->md.al_offset = MD_4kB_SECT;
809 bdev->md.bm_offset = MD_4kB_SECT + al_size_sect;
810 break;
811 case DRBD_MD_INDEX_INTERNAL:
812 case DRBD_MD_INDEX_FLEX_INT:
813 /* al size is still fixed */
814 bdev->md.al_offset = -al_size_sect;
815 /* we need (slightly less than) ~ this much bitmap sectors: */
816 md_size_sect = drbd_get_capacity(bdev->backing_bdev);
817 md_size_sect = ALIGN(md_size_sect, BM_SECT_PER_EXT);
818 md_size_sect = BM_SECT_TO_EXT(md_size_sect);
819 md_size_sect = ALIGN(md_size_sect, 8);
820
821 /* plus the "drbd meta data super block",
822 * and the activity log; */
823 md_size_sect += MD_4kB_SECT + al_size_sect;
824
825 bdev->md.md_size_sect = md_size_sect;
826 /* bitmap offset is adjusted by 'super' block size */
827 bdev->md.bm_offset = -md_size_sect + MD_4kB_SECT;
828 break;
829 }
830 }
831
832 /* input size is expected to be in KB */
833 char *ppsize(char *buf, unsigned long long size)
834 {
835 /* Needs 9 bytes at max including trailing NUL:
836 * -1ULL ==> "16384 EB" */
837 static char units[] = { 'K', 'M', 'G', 'T', 'P', 'E' };
838 int base = 0;
839 while (size >= 10000 && base < sizeof(units)-1) {
840 /* shift + round */
841 size = (size >> 10) + !!(size & (1<<9));
842 base++;
843 }
844 sprintf(buf, "%u %cB", (unsigned)size, units[base]);
845
846 return buf;
847 }
848
849 /* there is still a theoretical deadlock when called from receiver
850 * on an D_INCONSISTENT R_PRIMARY:
851 * remote READ does inc_ap_bio, receiver would need to receive answer
852 * packet from remote to dec_ap_bio again.
853 * receiver receive_sizes(), comes here,
854 * waits for ap_bio_cnt == 0. -> deadlock.
855 * but this cannot happen, actually, because:
856 * R_PRIMARY D_INCONSISTENT, and peer's disk is unreachable
857 * (not connected, or bad/no disk on peer):
858 * see drbd_fail_request_early, ap_bio_cnt is zero.
859 * R_PRIMARY D_INCONSISTENT, and C_SYNC_TARGET:
860 * peer may not initiate a resize.
861 */
862 /* Note these are not to be confused with
863 * drbd_adm_suspend_io/drbd_adm_resume_io,
864 * which are (sub) state changes triggered by admin (drbdsetup),
865 * and can be long lived.
866 * This changes an device->flag, is triggered by drbd internals,
867 * and should be short-lived. */
868 /* It needs to be a counter, since multiple threads might
869 independently suspend and resume IO. */
870 void drbd_suspend_io(struct drbd_device *device)
871 {
872 atomic_inc(&device->suspend_cnt);
873 if (drbd_suspended(device))
874 return;
875 wait_event(device->misc_wait, !atomic_read(&device->ap_bio_cnt));
876 }
877
878 void drbd_resume_io(struct drbd_device *device)
879 {
880 if (atomic_dec_and_test(&device->suspend_cnt))
881 wake_up(&device->misc_wait);
882 }
883
884 /**
885 * drbd_determine_dev_size() - Sets the right device size obeying all constraints
886 * @device: DRBD device.
887 *
888 * Returns 0 on success, negative return values indicate errors.
889 * You should call drbd_md_sync() after calling this function.
890 */
891 enum determine_dev_size
892 drbd_determine_dev_size(struct drbd_device *device, enum dds_flags flags, struct resize_parms *rs) __must_hold(local)
893 {
894 struct md_offsets_and_sizes {
895 u64 last_agreed_sect;
896 u64 md_offset;
897 s32 al_offset;
898 s32 bm_offset;
899 u32 md_size_sect;
900
901 u32 al_stripes;
902 u32 al_stripe_size_4k;
903 } prev;
904 sector_t u_size, size;
905 struct drbd_md *md = &device->ldev->md;
906 char ppb[10];
907 void *buffer;
908
909 int md_moved, la_size_changed;
910 enum determine_dev_size rv = DS_UNCHANGED;
911
912 /* We may change the on-disk offsets of our meta data below. Lock out
913 * anything that may cause meta data IO, to avoid acting on incomplete
914 * layout changes or scribbling over meta data that is in the process
915 * of being moved.
916 *
917 * Move is not exactly correct, btw, currently we have all our meta
918 * data in core memory, to "move" it we just write it all out, there
919 * are no reads. */
920 drbd_suspend_io(device);
921 buffer = drbd_md_get_buffer(device, __func__); /* Lock meta-data IO */
922 if (!buffer) {
923 drbd_resume_io(device);
924 return DS_ERROR;
925 }
926
927 /* remember current offset and sizes */
928 prev.last_agreed_sect = md->la_size_sect;
929 prev.md_offset = md->md_offset;
930 prev.al_offset = md->al_offset;
931 prev.bm_offset = md->bm_offset;
932 prev.md_size_sect = md->md_size_sect;
933 prev.al_stripes = md->al_stripes;
934 prev.al_stripe_size_4k = md->al_stripe_size_4k;
935
936 if (rs) {
937 /* rs is non NULL if we should change the AL layout only */
938 md->al_stripes = rs->al_stripes;
939 md->al_stripe_size_4k = rs->al_stripe_size / 4;
940 md->al_size_4k = (u64)rs->al_stripes * rs->al_stripe_size / 4;
941 }
942
943 drbd_md_set_sector_offsets(device, device->ldev);
944
945 rcu_read_lock();
946 u_size = rcu_dereference(device->ldev->disk_conf)->disk_size;
947 rcu_read_unlock();
948 size = drbd_new_dev_size(device, device->ldev, u_size, flags & DDSF_FORCED);
949
950 if (size < prev.last_agreed_sect) {
951 if (rs && u_size == 0) {
952 /* Remove "rs &&" later. This check should always be active, but
953 right now the receiver expects the permissive behavior */
954 drbd_warn(device, "Implicit shrink not allowed. "
955 "Use --size=%llus for explicit shrink.\n",
956 (unsigned long long)size);
957 rv = DS_ERROR_SHRINK;
958 }
959 if (u_size > size)
960 rv = DS_ERROR_SPACE_MD;
961 if (rv != DS_UNCHANGED)
962 goto err_out;
963 }
964
965 if (drbd_get_capacity(device->this_bdev) != size ||
966 drbd_bm_capacity(device) != size) {
967 int err;
968 err = drbd_bm_resize(device, size, !(flags & DDSF_NO_RESYNC));
969 if (unlikely(err)) {
970 /* currently there is only one error: ENOMEM! */
971 size = drbd_bm_capacity(device);
972 if (size == 0) {
973 drbd_err(device, "OUT OF MEMORY! "
974 "Could not allocate bitmap!\n");
975 } else {
976 drbd_err(device, "BM resizing failed. "
977 "Leaving size unchanged\n");
978 }
979 rv = DS_ERROR;
980 }
981 /* racy, see comments above. */
982 drbd_set_my_capacity(device, size);
983 md->la_size_sect = size;
984 drbd_info(device, "size = %s (%llu KB)\n", ppsize(ppb, size>>1),
985 (unsigned long long)size>>1);
986 }
987 if (rv <= DS_ERROR)
988 goto err_out;
989
990 la_size_changed = (prev.last_agreed_sect != md->la_size_sect);
991
992 md_moved = prev.md_offset != md->md_offset
993 || prev.md_size_sect != md->md_size_sect;
994
995 if (la_size_changed || md_moved || rs) {
996 u32 prev_flags;
997
998 /* We do some synchronous IO below, which may take some time.
999 * Clear the timer, to avoid scary "timer expired!" messages,
1000 * "Superblock" is written out at least twice below, anyways. */
1001 del_timer(&device->md_sync_timer);
1002
1003 /* We won't change the "al-extents" setting, we just may need
1004 * to move the on-disk location of the activity log ringbuffer.
1005 * Lock for transaction is good enough, it may well be "dirty"
1006 * or even "starving". */
1007 wait_event(device->al_wait, lc_try_lock_for_transaction(device->act_log));
1008
1009 /* mark current on-disk bitmap and activity log as unreliable */
1010 prev_flags = md->flags;
1011 md->flags |= MDF_FULL_SYNC | MDF_AL_DISABLED;
1012 drbd_md_write(device, buffer);
1013
1014 drbd_al_initialize(device, buffer);
1015
1016 drbd_info(device, "Writing the whole bitmap, %s\n",
1017 la_size_changed && md_moved ? "size changed and md moved" :
1018 la_size_changed ? "size changed" : "md moved");
1019 /* next line implicitly does drbd_suspend_io()+drbd_resume_io() */
1020 drbd_bitmap_io(device, md_moved ? &drbd_bm_write_all : &drbd_bm_write,
1021 "size changed", BM_LOCKED_MASK);
1022
1023 /* on-disk bitmap and activity log is authoritative again
1024 * (unless there was an IO error meanwhile...) */
1025 md->flags = prev_flags;
1026 drbd_md_write(device, buffer);
1027
1028 if (rs)
1029 drbd_info(device, "Changed AL layout to al-stripes = %d, al-stripe-size-kB = %d\n",
1030 md->al_stripes, md->al_stripe_size_4k * 4);
1031 }
1032
1033 if (size > prev.last_agreed_sect)
1034 rv = prev.last_agreed_sect ? DS_GREW : DS_GREW_FROM_ZERO;
1035 if (size < prev.last_agreed_sect)
1036 rv = DS_SHRUNK;
1037
1038 if (0) {
1039 err_out:
1040 /* restore previous offset and sizes */
1041 md->la_size_sect = prev.last_agreed_sect;
1042 md->md_offset = prev.md_offset;
1043 md->al_offset = prev.al_offset;
1044 md->bm_offset = prev.bm_offset;
1045 md->md_size_sect = prev.md_size_sect;
1046 md->al_stripes = prev.al_stripes;
1047 md->al_stripe_size_4k = prev.al_stripe_size_4k;
1048 md->al_size_4k = (u64)prev.al_stripes * prev.al_stripe_size_4k;
1049 }
1050 lc_unlock(device->act_log);
1051 wake_up(&device->al_wait);
1052 drbd_md_put_buffer(device);
1053 drbd_resume_io(device);
1054
1055 return rv;
1056 }
1057
1058 sector_t
1059 drbd_new_dev_size(struct drbd_device *device, struct drbd_backing_dev *bdev,
1060 sector_t u_size, int assume_peer_has_space)
1061 {
1062 sector_t p_size = device->p_size; /* partner's disk size. */
1063 sector_t la_size_sect = bdev->md.la_size_sect; /* last agreed size. */
1064 sector_t m_size; /* my size */
1065 sector_t size = 0;
1066
1067 m_size = drbd_get_max_capacity(bdev);
1068
1069 if (device->state.conn < C_CONNECTED && assume_peer_has_space) {
1070 drbd_warn(device, "Resize while not connected was forced by the user!\n");
1071 p_size = m_size;
1072 }
1073
1074 if (p_size && m_size) {
1075 size = min_t(sector_t, p_size, m_size);
1076 } else {
1077 if (la_size_sect) {
1078 size = la_size_sect;
1079 if (m_size && m_size < size)
1080 size = m_size;
1081 if (p_size && p_size < size)
1082 size = p_size;
1083 } else {
1084 if (m_size)
1085 size = m_size;
1086 if (p_size)
1087 size = p_size;
1088 }
1089 }
1090
1091 if (size == 0)
1092 drbd_err(device, "Both nodes diskless!\n");
1093
1094 if (u_size) {
1095 if (u_size > size)
1096 drbd_err(device, "Requested disk size is too big (%lu > %lu)\n",
1097 (unsigned long)u_size>>1, (unsigned long)size>>1);
1098 else
1099 size = u_size;
1100 }
1101
1102 return size;
1103 }
1104
1105 /**
1106 * drbd_check_al_size() - Ensures that the AL is of the right size
1107 * @device: DRBD device.
1108 *
1109 * Returns -EBUSY if current al lru is still used, -ENOMEM when allocation
1110 * failed, and 0 on success. You should call drbd_md_sync() after you called
1111 * this function.
1112 */
1113 static int drbd_check_al_size(struct drbd_device *device, struct disk_conf *dc)
1114 {
1115 struct lru_cache *n, *t;
1116 struct lc_element *e;
1117 unsigned int in_use;
1118 int i;
1119
1120 if (device->act_log &&
1121 device->act_log->nr_elements == dc->al_extents)
1122 return 0;
1123
1124 in_use = 0;
1125 t = device->act_log;
1126 n = lc_create("act_log", drbd_al_ext_cache, AL_UPDATES_PER_TRANSACTION,
1127 dc->al_extents, sizeof(struct lc_element), 0);
1128
1129 if (n == NULL) {
1130 drbd_err(device, "Cannot allocate act_log lru!\n");
1131 return -ENOMEM;
1132 }
1133 spin_lock_irq(&device->al_lock);
1134 if (t) {
1135 for (i = 0; i < t->nr_elements; i++) {
1136 e = lc_element_by_index(t, i);
1137 if (e->refcnt)
1138 drbd_err(device, "refcnt(%d)==%d\n",
1139 e->lc_number, e->refcnt);
1140 in_use += e->refcnt;
1141 }
1142 }
1143 if (!in_use)
1144 device->act_log = n;
1145 spin_unlock_irq(&device->al_lock);
1146 if (in_use) {
1147 drbd_err(device, "Activity log still in use!\n");
1148 lc_destroy(n);
1149 return -EBUSY;
1150 } else {
1151 lc_destroy(t);
1152 }
1153 drbd_md_mark_dirty(device); /* we changed device->act_log->nr_elemens */
1154 return 0;
1155 }
1156
1157 static void blk_queue_discard_granularity(struct request_queue *q, unsigned int granularity)
1158 {
1159 q->limits.discard_granularity = granularity;
1160 }
1161 static void decide_on_discard_support(struct drbd_device *device,
1162 struct request_queue *q,
1163 struct request_queue *b,
1164 bool discard_zeroes_if_aligned)
1165 {
1166 /* q = drbd device queue (device->rq_queue)
1167 * b = backing device queue (device->ldev->backing_bdev->bd_disk->queue),
1168 * or NULL if diskless
1169 */
1170 struct drbd_connection *connection = first_peer_device(device)->connection;
1171 bool can_do = b ? blk_queue_discard(b) : true;
1172
1173 if (can_do && b && !b->limits.discard_zeroes_data && !discard_zeroes_if_aligned) {
1174 can_do = false;
1175 drbd_info(device, "discard_zeroes_data=0 and discard_zeroes_if_aligned=no: disabling discards\n");
1176 }
1177 if (can_do && connection->cstate >= C_CONNECTED && !(connection->agreed_features & FF_TRIM)) {
1178 can_do = false;
1179 drbd_info(connection, "peer DRBD too old, does not support TRIM: disabling discards\n");
1180 }
1181 if (can_do) {
1182 /* We don't care for the granularity, really.
1183 * Stacking limits below should fix it for the local
1184 * device. Whether or not it is a suitable granularity
1185 * on the remote device is not our problem, really. If
1186 * you care, you need to use devices with similar
1187 * topology on all peers. */
1188 blk_queue_discard_granularity(q, 512);
1189 q->limits.max_discard_sectors = DRBD_MAX_DISCARD_SECTORS;
1190 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
1191 } else {
1192 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, q);
1193 blk_queue_discard_granularity(q, 0);
1194 q->limits.max_discard_sectors = 0;
1195 }
1196 }
1197
1198 static void fixup_discard_if_not_supported(struct request_queue *q)
1199 {
1200 /* To avoid confusion, if this queue does not support discard, clear
1201 * max_discard_sectors, which is what lsblk -D reports to the user.
1202 * Older kernels got this wrong in "stack limits".
1203 * */
1204 if (!blk_queue_discard(q)) {
1205 blk_queue_max_discard_sectors(q, 0);
1206 blk_queue_discard_granularity(q, 0);
1207 }
1208 }
1209
1210 static void drbd_setup_queue_param(struct drbd_device *device, struct drbd_backing_dev *bdev,
1211 unsigned int max_bio_size)
1212 {
1213 struct request_queue * const q = device->rq_queue;
1214 unsigned int max_hw_sectors = max_bio_size >> 9;
1215 unsigned int max_segments = 0;
1216 struct request_queue *b = NULL;
1217 struct disk_conf *dc;
1218 bool discard_zeroes_if_aligned = true;
1219
1220 if (bdev) {
1221 b = bdev->backing_bdev->bd_disk->queue;
1222
1223 max_hw_sectors = min(queue_max_hw_sectors(b), max_bio_size >> 9);
1224 rcu_read_lock();
1225 dc = rcu_dereference(device->ldev->disk_conf);
1226 max_segments = dc->max_bio_bvecs;
1227 discard_zeroes_if_aligned = dc->discard_zeroes_if_aligned;
1228 rcu_read_unlock();
1229
1230 blk_set_stacking_limits(&q->limits);
1231 blk_queue_max_write_same_sectors(q, 0);
1232 }
1233
1234 blk_queue_logical_block_size(q, 512);
1235 blk_queue_max_hw_sectors(q, max_hw_sectors);
1236 /* This is the workaround for "bio would need to, but cannot, be split" */
1237 blk_queue_max_segments(q, max_segments ? max_segments : BLK_MAX_SEGMENTS);
1238 blk_queue_segment_boundary(q, PAGE_SIZE-1);
1239 decide_on_discard_support(device, q, b, discard_zeroes_if_aligned);
1240 if (b) {
1241 blk_queue_stack_limits(q, b);
1242
1243 if (q->backing_dev_info.ra_pages != b->backing_dev_info.ra_pages) {
1244 drbd_info(device, "Adjusting my ra_pages to backing device's (%lu -> %lu)\n",
1245 q->backing_dev_info.ra_pages,
1246 b->backing_dev_info.ra_pages);
1247 q->backing_dev_info.ra_pages = b->backing_dev_info.ra_pages;
1248 }
1249 }
1250 fixup_discard_if_not_supported(q);
1251 }
1252
1253 void drbd_reconsider_queue_parameters(struct drbd_device *device, struct drbd_backing_dev *bdev)
1254 {
1255 unsigned int now, new, local, peer;
1256
1257 now = queue_max_hw_sectors(device->rq_queue) << 9;
1258 local = device->local_max_bio_size; /* Eventually last known value, from volatile memory */
1259 peer = device->peer_max_bio_size; /* Eventually last known value, from meta data */
1260
1261 if (bdev) {
1262 local = queue_max_hw_sectors(bdev->backing_bdev->bd_disk->queue) << 9;
1263 device->local_max_bio_size = local;
1264 }
1265 local = min(local, DRBD_MAX_BIO_SIZE);
1266
1267 /* We may ignore peer limits if the peer is modern enough.
1268 Because new from 8.3.8 onwards the peer can use multiple
1269 BIOs for a single peer_request */
1270 if (device->state.conn >= C_WF_REPORT_PARAMS) {
1271 if (first_peer_device(device)->connection->agreed_pro_version < 94)
1272 peer = min(device->peer_max_bio_size, DRBD_MAX_SIZE_H80_PACKET);
1273 /* Correct old drbd (up to 8.3.7) if it believes it can do more than 32KiB */
1274 else if (first_peer_device(device)->connection->agreed_pro_version == 94)
1275 peer = DRBD_MAX_SIZE_H80_PACKET;
1276 else if (first_peer_device(device)->connection->agreed_pro_version < 100)
1277 peer = DRBD_MAX_BIO_SIZE_P95; /* drbd 8.3.8 onwards, before 8.4.0 */
1278 else
1279 peer = DRBD_MAX_BIO_SIZE;
1280
1281 /* We may later detach and re-attach on a disconnected Primary.
1282 * Avoid this setting to jump back in that case.
1283 * We want to store what we know the peer DRBD can handle,
1284 * not what the peer IO backend can handle. */
1285 if (peer > device->peer_max_bio_size)
1286 device->peer_max_bio_size = peer;
1287 }
1288 new = min(local, peer);
1289
1290 if (device->state.role == R_PRIMARY && new < now)
1291 drbd_err(device, "ASSERT FAILED new < now; (%u < %u)\n", new, now);
1292
1293 if (new != now)
1294 drbd_info(device, "max BIO size = %u\n", new);
1295
1296 drbd_setup_queue_param(device, bdev, new);
1297 }
1298
1299 /* Starts the worker thread */
1300 static void conn_reconfig_start(struct drbd_connection *connection)
1301 {
1302 drbd_thread_start(&connection->worker);
1303 drbd_flush_workqueue(&connection->sender_work);
1304 }
1305
1306 /* if still unconfigured, stops worker again. */
1307 static void conn_reconfig_done(struct drbd_connection *connection)
1308 {
1309 bool stop_threads;
1310 spin_lock_irq(&connection->resource->req_lock);
1311 stop_threads = conn_all_vols_unconf(connection) &&
1312 connection->cstate == C_STANDALONE;
1313 spin_unlock_irq(&connection->resource->req_lock);
1314 if (stop_threads) {
1315 /* ack_receiver thread and ack_sender workqueue are implicitly
1316 * stopped by receiver in conn_disconnect() */
1317 drbd_thread_stop(&connection->receiver);
1318 drbd_thread_stop(&connection->worker);
1319 }
1320 }
1321
1322 /* Make sure IO is suspended before calling this function(). */
1323 static void drbd_suspend_al(struct drbd_device *device)
1324 {
1325 int s = 0;
1326
1327 if (!lc_try_lock(device->act_log)) {
1328 drbd_warn(device, "Failed to lock al in drbd_suspend_al()\n");
1329 return;
1330 }
1331
1332 drbd_al_shrink(device);
1333 spin_lock_irq(&device->resource->req_lock);
1334 if (device->state.conn < C_CONNECTED)
1335 s = !test_and_set_bit(AL_SUSPENDED, &device->flags);
1336 spin_unlock_irq(&device->resource->req_lock);
1337 lc_unlock(device->act_log);
1338
1339 if (s)
1340 drbd_info(device, "Suspended AL updates\n");
1341 }
1342
1343
1344 static bool should_set_defaults(struct genl_info *info)
1345 {
1346 unsigned flags = ((struct drbd_genlmsghdr*)info->userhdr)->flags;
1347 return 0 != (flags & DRBD_GENL_F_SET_DEFAULTS);
1348 }
1349
1350 static unsigned int drbd_al_extents_max(struct drbd_backing_dev *bdev)
1351 {
1352 /* This is limited by 16 bit "slot" numbers,
1353 * and by available on-disk context storage.
1354 *
1355 * Also (u16)~0 is special (denotes a "free" extent).
1356 *
1357 * One transaction occupies one 4kB on-disk block,
1358 * we have n such blocks in the on disk ring buffer,
1359 * the "current" transaction may fail (n-1),
1360 * and there is 919 slot numbers context information per transaction.
1361 *
1362 * 72 transaction blocks amounts to more than 2**16 context slots,
1363 * so cap there first.
1364 */
1365 const unsigned int max_al_nr = DRBD_AL_EXTENTS_MAX;
1366 const unsigned int sufficient_on_disk =
1367 (max_al_nr + AL_CONTEXT_PER_TRANSACTION -1)
1368 /AL_CONTEXT_PER_TRANSACTION;
1369
1370 unsigned int al_size_4k = bdev->md.al_size_4k;
1371
1372 if (al_size_4k > sufficient_on_disk)
1373 return max_al_nr;
1374
1375 return (al_size_4k - 1) * AL_CONTEXT_PER_TRANSACTION;
1376 }
1377
1378 static bool write_ordering_changed(struct disk_conf *a, struct disk_conf *b)
1379 {
1380 return a->disk_barrier != b->disk_barrier ||
1381 a->disk_flushes != b->disk_flushes ||
1382 a->disk_drain != b->disk_drain;
1383 }
1384
1385 static void sanitize_disk_conf(struct drbd_device *device, struct disk_conf *disk_conf,
1386 struct drbd_backing_dev *nbc)
1387 {
1388 struct request_queue * const q = nbc->backing_bdev->bd_disk->queue;
1389
1390 if (disk_conf->al_extents < DRBD_AL_EXTENTS_MIN)
1391 disk_conf->al_extents = DRBD_AL_EXTENTS_MIN;
1392 if (disk_conf->al_extents > drbd_al_extents_max(nbc))
1393 disk_conf->al_extents = drbd_al_extents_max(nbc);
1394
1395 if (!blk_queue_discard(q) || !q->limits.discard_zeroes_data) {
1396 disk_conf->rs_discard_granularity = 0; /* disable feature */
1397 drbd_info(device, "rs_discard_granularity feature disabled\n");
1398 }
1399
1400 if (disk_conf->rs_discard_granularity) {
1401 int orig_value = disk_conf->rs_discard_granularity;
1402 int remainder;
1403
1404 if (q->limits.discard_granularity > disk_conf->rs_discard_granularity)
1405 disk_conf->rs_discard_granularity = q->limits.discard_granularity;
1406
1407 remainder = disk_conf->rs_discard_granularity % q->limits.discard_granularity;
1408 disk_conf->rs_discard_granularity += remainder;
1409
1410 if (disk_conf->rs_discard_granularity > q->limits.max_discard_sectors << 9)
1411 disk_conf->rs_discard_granularity = q->limits.max_discard_sectors << 9;
1412
1413 if (disk_conf->rs_discard_granularity != orig_value)
1414 drbd_info(device, "rs_discard_granularity changed to %d\n",
1415 disk_conf->rs_discard_granularity);
1416 }
1417 }
1418
1419 int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info)
1420 {
1421 struct drbd_config_context adm_ctx;
1422 enum drbd_ret_code retcode;
1423 struct drbd_device *device;
1424 struct disk_conf *new_disk_conf, *old_disk_conf;
1425 struct fifo_buffer *old_plan = NULL, *new_plan = NULL;
1426 int err, fifo_size;
1427
1428 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
1429 if (!adm_ctx.reply_skb)
1430 return retcode;
1431 if (retcode != NO_ERROR)
1432 goto finish;
1433
1434 device = adm_ctx.device;
1435 mutex_lock(&adm_ctx.resource->adm_mutex);
1436
1437 /* we also need a disk
1438 * to change the options on */
1439 if (!get_ldev(device)) {
1440 retcode = ERR_NO_DISK;
1441 goto out;
1442 }
1443
1444 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
1445 if (!new_disk_conf) {
1446 retcode = ERR_NOMEM;
1447 goto fail;
1448 }
1449
1450 mutex_lock(&device->resource->conf_update);
1451 old_disk_conf = device->ldev->disk_conf;
1452 *new_disk_conf = *old_disk_conf;
1453 if (should_set_defaults(info))
1454 set_disk_conf_defaults(new_disk_conf);
1455
1456 err = disk_conf_from_attrs_for_change(new_disk_conf, info);
1457 if (err && err != -ENOMSG) {
1458 retcode = ERR_MANDATORY_TAG;
1459 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
1460 goto fail_unlock;
1461 }
1462
1463 if (!expect(new_disk_conf->resync_rate >= 1))
1464 new_disk_conf->resync_rate = 1;
1465
1466 sanitize_disk_conf(device, new_disk_conf, device->ldev);
1467
1468 if (new_disk_conf->c_plan_ahead > DRBD_C_PLAN_AHEAD_MAX)
1469 new_disk_conf->c_plan_ahead = DRBD_C_PLAN_AHEAD_MAX;
1470
1471 fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
1472 if (fifo_size != device->rs_plan_s->size) {
1473 new_plan = fifo_alloc(fifo_size);
1474 if (!new_plan) {
1475 drbd_err(device, "kmalloc of fifo_buffer failed");
1476 retcode = ERR_NOMEM;
1477 goto fail_unlock;
1478 }
1479 }
1480
1481 drbd_suspend_io(device);
1482 wait_event(device->al_wait, lc_try_lock(device->act_log));
1483 drbd_al_shrink(device);
1484 err = drbd_check_al_size(device, new_disk_conf);
1485 lc_unlock(device->act_log);
1486 wake_up(&device->al_wait);
1487 drbd_resume_io(device);
1488
1489 if (err) {
1490 retcode = ERR_NOMEM;
1491 goto fail_unlock;
1492 }
1493
1494 lock_all_resources();
1495 retcode = drbd_resync_after_valid(device, new_disk_conf->resync_after);
1496 if (retcode == NO_ERROR) {
1497 rcu_assign_pointer(device->ldev->disk_conf, new_disk_conf);
1498 drbd_resync_after_changed(device);
1499 }
1500 unlock_all_resources();
1501
1502 if (retcode != NO_ERROR)
1503 goto fail_unlock;
1504
1505 if (new_plan) {
1506 old_plan = device->rs_plan_s;
1507 rcu_assign_pointer(device->rs_plan_s, new_plan);
1508 }
1509
1510 mutex_unlock(&device->resource->conf_update);
1511
1512 if (new_disk_conf->al_updates)
1513 device->ldev->md.flags &= ~MDF_AL_DISABLED;
1514 else
1515 device->ldev->md.flags |= MDF_AL_DISABLED;
1516
1517 if (new_disk_conf->md_flushes)
1518 clear_bit(MD_NO_FUA, &device->flags);
1519 else
1520 set_bit(MD_NO_FUA, &device->flags);
1521
1522 if (write_ordering_changed(old_disk_conf, new_disk_conf))
1523 drbd_bump_write_ordering(device->resource, NULL, WO_BDEV_FLUSH);
1524
1525 if (old_disk_conf->discard_zeroes_if_aligned != new_disk_conf->discard_zeroes_if_aligned)
1526 drbd_reconsider_queue_parameters(device, device->ldev);
1527
1528 drbd_md_sync(device);
1529
1530 if (device->state.conn >= C_CONNECTED) {
1531 struct drbd_peer_device *peer_device;
1532
1533 for_each_peer_device(peer_device, device)
1534 drbd_send_sync_param(peer_device);
1535 }
1536
1537 synchronize_rcu();
1538 kfree(old_disk_conf);
1539 kfree(old_plan);
1540 mod_timer(&device->request_timer, jiffies + HZ);
1541 goto success;
1542
1543 fail_unlock:
1544 mutex_unlock(&device->resource->conf_update);
1545 fail:
1546 kfree(new_disk_conf);
1547 kfree(new_plan);
1548 success:
1549 put_ldev(device);
1550 out:
1551 mutex_unlock(&adm_ctx.resource->adm_mutex);
1552 finish:
1553 drbd_adm_finish(&adm_ctx, info, retcode);
1554 return 0;
1555 }
1556
1557 static struct block_device *open_backing_dev(struct drbd_device *device,
1558 const char *bdev_path, void *claim_ptr, bool do_bd_link)
1559 {
1560 struct block_device *bdev;
1561 int err = 0;
1562
1563 bdev = blkdev_get_by_path(bdev_path,
1564 FMODE_READ | FMODE_WRITE | FMODE_EXCL, claim_ptr);
1565 if (IS_ERR(bdev)) {
1566 drbd_err(device, "open(\"%s\") failed with %ld\n",
1567 bdev_path, PTR_ERR(bdev));
1568 return bdev;
1569 }
1570
1571 if (!do_bd_link)
1572 return bdev;
1573
1574 err = bd_link_disk_holder(bdev, device->vdisk);
1575 if (err) {
1576 blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1577 drbd_err(device, "bd_link_disk_holder(\"%s\", ...) failed with %d\n",
1578 bdev_path, err);
1579 bdev = ERR_PTR(err);
1580 }
1581 return bdev;
1582 }
1583
1584 static int open_backing_devices(struct drbd_device *device,
1585 struct disk_conf *new_disk_conf,
1586 struct drbd_backing_dev *nbc)
1587 {
1588 struct block_device *bdev;
1589
1590 bdev = open_backing_dev(device, new_disk_conf->backing_dev, device, true);
1591 if (IS_ERR(bdev))
1592 return ERR_OPEN_DISK;
1593 nbc->backing_bdev = bdev;
1594
1595 /*
1596 * meta_dev_idx >= 0: external fixed size, possibly multiple
1597 * drbd sharing one meta device. TODO in that case, paranoia
1598 * check that [md_bdev, meta_dev_idx] is not yet used by some
1599 * other drbd minor! (if you use drbd.conf + drbdadm, that
1600 * should check it for you already; but if you don't, or
1601 * someone fooled it, we need to double check here)
1602 */
1603 bdev = open_backing_dev(device, new_disk_conf->meta_dev,
1604 /* claim ptr: device, if claimed exclusively; shared drbd_m_holder,
1605 * if potentially shared with other drbd minors */
1606 (new_disk_conf->meta_dev_idx < 0) ? (void*)device : (void*)drbd_m_holder,
1607 /* avoid double bd_claim_by_disk() for the same (source,target) tuple,
1608 * as would happen with internal metadata. */
1609 (new_disk_conf->meta_dev_idx != DRBD_MD_INDEX_FLEX_INT &&
1610 new_disk_conf->meta_dev_idx != DRBD_MD_INDEX_INTERNAL));
1611 if (IS_ERR(bdev))
1612 return ERR_OPEN_MD_DISK;
1613 nbc->md_bdev = bdev;
1614 return NO_ERROR;
1615 }
1616
1617 static void close_backing_dev(struct drbd_device *device, struct block_device *bdev,
1618 bool do_bd_unlink)
1619 {
1620 if (!bdev)
1621 return;
1622 if (do_bd_unlink)
1623 bd_unlink_disk_holder(bdev, device->vdisk);
1624 blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1625 }
1626
1627 void drbd_backing_dev_free(struct drbd_device *device, struct drbd_backing_dev *ldev)
1628 {
1629 if (ldev == NULL)
1630 return;
1631
1632 close_backing_dev(device, ldev->md_bdev, ldev->md_bdev != ldev->backing_bdev);
1633 close_backing_dev(device, ldev->backing_bdev, true);
1634
1635 kfree(ldev->disk_conf);
1636 kfree(ldev);
1637 }
1638
1639 int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info)
1640 {
1641 struct drbd_config_context adm_ctx;
1642 struct drbd_device *device;
1643 struct drbd_peer_device *peer_device;
1644 struct drbd_connection *connection;
1645 int err;
1646 enum drbd_ret_code retcode;
1647 enum determine_dev_size dd;
1648 sector_t max_possible_sectors;
1649 sector_t min_md_device_sectors;
1650 struct drbd_backing_dev *nbc = NULL; /* new_backing_conf */
1651 struct disk_conf *new_disk_conf = NULL;
1652 struct lru_cache *resync_lru = NULL;
1653 struct fifo_buffer *new_plan = NULL;
1654 union drbd_state ns, os;
1655 enum drbd_state_rv rv;
1656 struct net_conf *nc;
1657
1658 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
1659 if (!adm_ctx.reply_skb)
1660 return retcode;
1661 if (retcode != NO_ERROR)
1662 goto finish;
1663
1664 device = adm_ctx.device;
1665 mutex_lock(&adm_ctx.resource->adm_mutex);
1666 peer_device = first_peer_device(device);
1667 connection = peer_device->connection;
1668 conn_reconfig_start(connection);
1669
1670 /* if you want to reconfigure, please tear down first */
1671 if (device->state.disk > D_DISKLESS) {
1672 retcode = ERR_DISK_CONFIGURED;
1673 goto fail;
1674 }
1675 /* It may just now have detached because of IO error. Make sure
1676 * drbd_ldev_destroy is done already, we may end up here very fast,
1677 * e.g. if someone calls attach from the on-io-error handler,
1678 * to realize a "hot spare" feature (not that I'd recommend that) */
1679 wait_event(device->misc_wait, !test_bit(GOING_DISKLESS, &device->flags));
1680
1681 /* make sure there is no leftover from previous force-detach attempts */
1682 clear_bit(FORCE_DETACH, &device->flags);
1683 clear_bit(WAS_IO_ERROR, &device->flags);
1684 clear_bit(WAS_READ_ERROR, &device->flags);
1685
1686 /* and no leftover from previously aborted resync or verify, either */
1687 device->rs_total = 0;
1688 device->rs_failed = 0;
1689 atomic_set(&device->rs_pending_cnt, 0);
1690
1691 /* allocation not in the IO path, drbdsetup context */
1692 nbc = kzalloc(sizeof(struct drbd_backing_dev), GFP_KERNEL);
1693 if (!nbc) {
1694 retcode = ERR_NOMEM;
1695 goto fail;
1696 }
1697 spin_lock_init(&nbc->md.uuid_lock);
1698
1699 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
1700 if (!new_disk_conf) {
1701 retcode = ERR_NOMEM;
1702 goto fail;
1703 }
1704 nbc->disk_conf = new_disk_conf;
1705
1706 set_disk_conf_defaults(new_disk_conf);
1707 err = disk_conf_from_attrs(new_disk_conf, info);
1708 if (err) {
1709 retcode = ERR_MANDATORY_TAG;
1710 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
1711 goto fail;
1712 }
1713
1714 if (new_disk_conf->c_plan_ahead > DRBD_C_PLAN_AHEAD_MAX)
1715 new_disk_conf->c_plan_ahead = DRBD_C_PLAN_AHEAD_MAX;
1716
1717 new_plan = fifo_alloc((new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ);
1718 if (!new_plan) {
1719 retcode = ERR_NOMEM;
1720 goto fail;
1721 }
1722
1723 if (new_disk_conf->meta_dev_idx < DRBD_MD_INDEX_FLEX_INT) {
1724 retcode = ERR_MD_IDX_INVALID;
1725 goto fail;
1726 }
1727
1728 rcu_read_lock();
1729 nc = rcu_dereference(connection->net_conf);
1730 if (nc) {
1731 if (new_disk_conf->fencing == FP_STONITH && nc->wire_protocol == DRBD_PROT_A) {
1732 rcu_read_unlock();
1733 retcode = ERR_STONITH_AND_PROT_A;
1734 goto fail;
1735 }
1736 }
1737 rcu_read_unlock();
1738
1739 retcode = open_backing_devices(device, new_disk_conf, nbc);
1740 if (retcode != NO_ERROR)
1741 goto fail;
1742
1743 if ((nbc->backing_bdev == nbc->md_bdev) !=
1744 (new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_INTERNAL ||
1745 new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_FLEX_INT)) {
1746 retcode = ERR_MD_IDX_INVALID;
1747 goto fail;
1748 }
1749
1750 resync_lru = lc_create("resync", drbd_bm_ext_cache,
1751 1, 61, sizeof(struct bm_extent),
1752 offsetof(struct bm_extent, lce));
1753 if (!resync_lru) {
1754 retcode = ERR_NOMEM;
1755 goto fail;
1756 }
1757
1758 /* Read our meta data super block early.
1759 * This also sets other on-disk offsets. */
1760 retcode = drbd_md_read(device, nbc);
1761 if (retcode != NO_ERROR)
1762 goto fail;
1763
1764 sanitize_disk_conf(device, new_disk_conf, nbc);
1765
1766 if (drbd_get_max_capacity(nbc) < new_disk_conf->disk_size) {
1767 drbd_err(device, "max capacity %llu smaller than disk size %llu\n",
1768 (unsigned long long) drbd_get_max_capacity(nbc),
1769 (unsigned long long) new_disk_conf->disk_size);
1770 retcode = ERR_DISK_TOO_SMALL;
1771 goto fail;
1772 }
1773
1774 if (new_disk_conf->meta_dev_idx < 0) {
1775 max_possible_sectors = DRBD_MAX_SECTORS_FLEX;
1776 /* at least one MB, otherwise it does not make sense */
1777 min_md_device_sectors = (2<<10);
1778 } else {
1779 max_possible_sectors = DRBD_MAX_SECTORS;
1780 min_md_device_sectors = MD_128MB_SECT * (new_disk_conf->meta_dev_idx + 1);
1781 }
1782
1783 if (drbd_get_capacity(nbc->md_bdev) < min_md_device_sectors) {
1784 retcode = ERR_MD_DISK_TOO_SMALL;
1785 drbd_warn(device, "refusing attach: md-device too small, "
1786 "at least %llu sectors needed for this meta-disk type\n",
1787 (unsigned long long) min_md_device_sectors);
1788 goto fail;
1789 }
1790
1791 /* Make sure the new disk is big enough
1792 * (we may currently be R_PRIMARY with no local disk...) */
1793 if (drbd_get_max_capacity(nbc) <
1794 drbd_get_capacity(device->this_bdev)) {
1795 retcode = ERR_DISK_TOO_SMALL;
1796 goto fail;
1797 }
1798
1799 nbc->known_size = drbd_get_capacity(nbc->backing_bdev);
1800
1801 if (nbc->known_size > max_possible_sectors) {
1802 drbd_warn(device, "==> truncating very big lower level device "
1803 "to currently maximum possible %llu sectors <==\n",
1804 (unsigned long long) max_possible_sectors);
1805 if (new_disk_conf->meta_dev_idx >= 0)
1806 drbd_warn(device, "==>> using internal or flexible "
1807 "meta data may help <<==\n");
1808 }
1809
1810 drbd_suspend_io(device);
1811 /* also wait for the last barrier ack. */
1812 /* FIXME see also https://daiquiri.linbit/cgi-bin/bugzilla/show_bug.cgi?id=171
1813 * We need a way to either ignore barrier acks for barriers sent before a device
1814 * was attached, or a way to wait for all pending barrier acks to come in.
1815 * As barriers are counted per resource,
1816 * we'd need to suspend io on all devices of a resource.
1817 */
1818 wait_event(device->misc_wait, !atomic_read(&device->ap_pending_cnt) || drbd_suspended(device));
1819 /* and for any other previously queued work */
1820 drbd_flush_workqueue(&connection->sender_work);
1821
1822 rv = _drbd_request_state(device, NS(disk, D_ATTACHING), CS_VERBOSE);
1823 retcode = rv; /* FIXME: Type mismatch. */
1824 drbd_resume_io(device);
1825 if (rv < SS_SUCCESS)
1826 goto fail;
1827
1828 if (!get_ldev_if_state(device, D_ATTACHING))
1829 goto force_diskless;
1830
1831 if (!device->bitmap) {
1832 if (drbd_bm_init(device)) {
1833 retcode = ERR_NOMEM;
1834 goto force_diskless_dec;
1835 }
1836 }
1837
1838 if (device->state.conn < C_CONNECTED &&
1839 device->state.role == R_PRIMARY && device->ed_uuid &&
1840 (device->ed_uuid & ~((u64)1)) != (nbc->md.uuid[UI_CURRENT] & ~((u64)1))) {
1841 drbd_err(device, "Can only attach to data with current UUID=%016llX\n",
1842 (unsigned long long)device->ed_uuid);
1843 retcode = ERR_DATA_NOT_CURRENT;
1844 goto force_diskless_dec;
1845 }
1846
1847 /* Since we are diskless, fix the activity log first... */
1848 if (drbd_check_al_size(device, new_disk_conf)) {
1849 retcode = ERR_NOMEM;
1850 goto force_diskless_dec;
1851 }
1852
1853 /* Prevent shrinking of consistent devices ! */
1854 if (drbd_md_test_flag(nbc, MDF_CONSISTENT) &&
1855 drbd_new_dev_size(device, nbc, nbc->disk_conf->disk_size, 0) < nbc->md.la_size_sect) {
1856 drbd_warn(device, "refusing to truncate a consistent device\n");
1857 retcode = ERR_DISK_TOO_SMALL;
1858 goto force_diskless_dec;
1859 }
1860
1861 lock_all_resources();
1862 retcode = drbd_resync_after_valid(device, new_disk_conf->resync_after);
1863 if (retcode != NO_ERROR) {
1864 unlock_all_resources();
1865 goto force_diskless_dec;
1866 }
1867
1868 /* Reset the "barriers don't work" bits here, then force meta data to
1869 * be written, to ensure we determine if barriers are supported. */
1870 if (new_disk_conf->md_flushes)
1871 clear_bit(MD_NO_FUA, &device->flags);
1872 else
1873 set_bit(MD_NO_FUA, &device->flags);
1874
1875 /* Point of no return reached.
1876 * Devices and memory are no longer released by error cleanup below.
1877 * now device takes over responsibility, and the state engine should
1878 * clean it up somewhere. */
1879 D_ASSERT(device, device->ldev == NULL);
1880 device->ldev = nbc;
1881 device->resync = resync_lru;
1882 device->rs_plan_s = new_plan;
1883 nbc = NULL;
1884 resync_lru = NULL;
1885 new_disk_conf = NULL;
1886 new_plan = NULL;
1887
1888 drbd_resync_after_changed(device);
1889 drbd_bump_write_ordering(device->resource, device->ldev, WO_BDEV_FLUSH);
1890 unlock_all_resources();
1891
1892 if (drbd_md_test_flag(device->ldev, MDF_CRASHED_PRIMARY))
1893 set_bit(CRASHED_PRIMARY, &device->flags);
1894 else
1895 clear_bit(CRASHED_PRIMARY, &device->flags);
1896
1897 if (drbd_md_test_flag(device->ldev, MDF_PRIMARY_IND) &&
1898 !(device->state.role == R_PRIMARY && device->resource->susp_nod))
1899 set_bit(CRASHED_PRIMARY, &device->flags);
1900
1901 device->send_cnt = 0;
1902 device->recv_cnt = 0;
1903 device->read_cnt = 0;
1904 device->writ_cnt = 0;
1905
1906 drbd_reconsider_queue_parameters(device, device->ldev);
1907
1908 /* If I am currently not R_PRIMARY,
1909 * but meta data primary indicator is set,
1910 * I just now recover from a hard crash,
1911 * and have been R_PRIMARY before that crash.
1912 *
1913 * Now, if I had no connection before that crash
1914 * (have been degraded R_PRIMARY), chances are that
1915 * I won't find my peer now either.
1916 *
1917 * In that case, and _only_ in that case,
1918 * we use the degr-wfc-timeout instead of the default,
1919 * so we can automatically recover from a crash of a
1920 * degraded but active "cluster" after a certain timeout.
1921 */
1922 clear_bit(USE_DEGR_WFC_T, &device->flags);
1923 if (device->state.role != R_PRIMARY &&
1924 drbd_md_test_flag(device->ldev, MDF_PRIMARY_IND) &&
1925 !drbd_md_test_flag(device->ldev, MDF_CONNECTED_IND))
1926 set_bit(USE_DEGR_WFC_T, &device->flags);
1927
1928 dd = drbd_determine_dev_size(device, 0, NULL);
1929 if (dd <= DS_ERROR) {
1930 retcode = ERR_NOMEM_BITMAP;
1931 goto force_diskless_dec;
1932 } else if (dd == DS_GREW)
1933 set_bit(RESYNC_AFTER_NEG, &device->flags);
1934
1935 if (drbd_md_test_flag(device->ldev, MDF_FULL_SYNC) ||
1936 (test_bit(CRASHED_PRIMARY, &device->flags) &&
1937 drbd_md_test_flag(device->ldev, MDF_AL_DISABLED))) {
1938 drbd_info(device, "Assuming that all blocks are out of sync "
1939 "(aka FullSync)\n");
1940 if (drbd_bitmap_io(device, &drbd_bmio_set_n_write,
1941 "set_n_write from attaching", BM_LOCKED_MASK)) {
1942 retcode = ERR_IO_MD_DISK;
1943 goto force_diskless_dec;
1944 }
1945 } else {
1946 if (drbd_bitmap_io(device, &drbd_bm_read,
1947 "read from attaching", BM_LOCKED_MASK)) {
1948 retcode = ERR_IO_MD_DISK;
1949 goto force_diskless_dec;
1950 }
1951 }
1952
1953 if (_drbd_bm_total_weight(device) == drbd_bm_bits(device))
1954 drbd_suspend_al(device); /* IO is still suspended here... */
1955
1956 spin_lock_irq(&device->resource->req_lock);
1957 os = drbd_read_state(device);
1958 ns = os;
1959 /* If MDF_CONSISTENT is not set go into inconsistent state,
1960 otherwise investigate MDF_WasUpToDate...
1961 If MDF_WAS_UP_TO_DATE is not set go into D_OUTDATED disk state,
1962 otherwise into D_CONSISTENT state.
1963 */
1964 if (drbd_md_test_flag(device->ldev, MDF_CONSISTENT)) {
1965 if (drbd_md_test_flag(device->ldev, MDF_WAS_UP_TO_DATE))
1966 ns.disk = D_CONSISTENT;
1967 else
1968 ns.disk = D_OUTDATED;
1969 } else {
1970 ns.disk = D_INCONSISTENT;
1971 }
1972
1973 if (drbd_md_test_flag(device->ldev, MDF_PEER_OUT_DATED))
1974 ns.pdsk = D_OUTDATED;
1975
1976 rcu_read_lock();
1977 if (ns.disk == D_CONSISTENT &&
1978 (ns.pdsk == D_OUTDATED || rcu_dereference(device->ldev->disk_conf)->fencing == FP_DONT_CARE))
1979 ns.disk = D_UP_TO_DATE;
1980
1981 /* All tests on MDF_PRIMARY_IND, MDF_CONNECTED_IND,
1982 MDF_CONSISTENT and MDF_WAS_UP_TO_DATE must happen before
1983 this point, because drbd_request_state() modifies these
1984 flags. */
1985
1986 if (rcu_dereference(device->ldev->disk_conf)->al_updates)
1987 device->ldev->md.flags &= ~MDF_AL_DISABLED;
1988 else
1989 device->ldev->md.flags |= MDF_AL_DISABLED;
1990
1991 rcu_read_unlock();
1992
1993 /* In case we are C_CONNECTED postpone any decision on the new disk
1994 state after the negotiation phase. */
1995 if (device->state.conn == C_CONNECTED) {
1996 device->new_state_tmp.i = ns.i;
1997 ns.i = os.i;
1998 ns.disk = D_NEGOTIATING;
1999
2000 /* We expect to receive up-to-date UUIDs soon.
2001 To avoid a race in receive_state, free p_uuid while
2002 holding req_lock. I.e. atomic with the state change */
2003 kfree(device->p_uuid);
2004 device->p_uuid = NULL;
2005 }
2006
2007 rv = _drbd_set_state(device, ns, CS_VERBOSE, NULL);
2008 spin_unlock_irq(&device->resource->req_lock);
2009
2010 if (rv < SS_SUCCESS)
2011 goto force_diskless_dec;
2012
2013 mod_timer(&device->request_timer, jiffies + HZ);
2014
2015 if (device->state.role == R_PRIMARY)
2016 device->ldev->md.uuid[UI_CURRENT] |= (u64)1;
2017 else
2018 device->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
2019
2020 drbd_md_mark_dirty(device);
2021 drbd_md_sync(device);
2022
2023 kobject_uevent(&disk_to_dev(device->vdisk)->kobj, KOBJ_CHANGE);
2024 put_ldev(device);
2025 conn_reconfig_done(connection);
2026 mutex_unlock(&adm_ctx.resource->adm_mutex);
2027 drbd_adm_finish(&adm_ctx, info, retcode);
2028 return 0;
2029
2030 force_diskless_dec:
2031 put_ldev(device);
2032 force_diskless:
2033 drbd_force_state(device, NS(disk, D_DISKLESS));
2034 drbd_md_sync(device);
2035 fail:
2036 conn_reconfig_done(connection);
2037 if (nbc) {
2038 close_backing_dev(device, nbc->md_bdev, nbc->md_bdev != nbc->backing_bdev);
2039 close_backing_dev(device, nbc->backing_bdev, true);
2040 kfree(nbc);
2041 }
2042 kfree(new_disk_conf);
2043 lc_destroy(resync_lru);
2044 kfree(new_plan);
2045 mutex_unlock(&adm_ctx.resource->adm_mutex);
2046 finish:
2047 drbd_adm_finish(&adm_ctx, info, retcode);
2048 return 0;
2049 }
2050
2051 static int adm_detach(struct drbd_device *device, int force)
2052 {
2053 enum drbd_state_rv retcode;
2054 void *buffer;
2055 int ret;
2056
2057 if (force) {
2058 set_bit(FORCE_DETACH, &device->flags);
2059 drbd_force_state(device, NS(disk, D_FAILED));
2060 retcode = SS_SUCCESS;
2061 goto out;
2062 }
2063
2064 drbd_suspend_io(device); /* so no-one is stuck in drbd_al_begin_io */
2065 buffer = drbd_md_get_buffer(device, __func__); /* make sure there is no in-flight meta-data IO */
2066 if (buffer) {
2067 retcode = drbd_request_state(device, NS(disk, D_FAILED));
2068 drbd_md_put_buffer(device);
2069 } else /* already <= D_FAILED */
2070 retcode = SS_NOTHING_TO_DO;
2071 /* D_FAILED will transition to DISKLESS. */
2072 drbd_resume_io(device);
2073 ret = wait_event_interruptible(device->misc_wait,
2074 device->state.disk != D_FAILED);
2075 if ((int)retcode == (int)SS_IS_DISKLESS)
2076 retcode = SS_NOTHING_TO_DO;
2077 if (ret)
2078 retcode = ERR_INTR;
2079 out:
2080 return retcode;
2081 }
2082
2083 /* Detaching the disk is a process in multiple stages. First we need to lock
2084 * out application IO, in-flight IO, IO stuck in drbd_al_begin_io.
2085 * Then we transition to D_DISKLESS, and wait for put_ldev() to return all
2086 * internal references as well.
2087 * Only then we have finally detached. */
2088 int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info)
2089 {
2090 struct drbd_config_context adm_ctx;
2091 enum drbd_ret_code retcode;
2092 struct detach_parms parms = { };
2093 int err;
2094
2095 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
2096 if (!adm_ctx.reply_skb)
2097 return retcode;
2098 if (retcode != NO_ERROR)
2099 goto out;
2100
2101 if (info->attrs[DRBD_NLA_DETACH_PARMS]) {
2102 err = detach_parms_from_attrs(&parms, info);
2103 if (err) {
2104 retcode = ERR_MANDATORY_TAG;
2105 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
2106 goto out;
2107 }
2108 }
2109
2110 mutex_lock(&adm_ctx.resource->adm_mutex);
2111 retcode = adm_detach(adm_ctx.device, parms.force_detach);
2112 mutex_unlock(&adm_ctx.resource->adm_mutex);
2113 out:
2114 drbd_adm_finish(&adm_ctx, info, retcode);
2115 return 0;
2116 }
2117
2118 static bool conn_resync_running(struct drbd_connection *connection)
2119 {
2120 struct drbd_peer_device *peer_device;
2121 bool rv = false;
2122 int vnr;
2123
2124 rcu_read_lock();
2125 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
2126 struct drbd_device *device = peer_device->device;
2127 if (device->state.conn == C_SYNC_SOURCE ||
2128 device->state.conn == C_SYNC_TARGET ||
2129 device->state.conn == C_PAUSED_SYNC_S ||
2130 device->state.conn == C_PAUSED_SYNC_T) {
2131 rv = true;
2132 break;
2133 }
2134 }
2135 rcu_read_unlock();
2136
2137 return rv;
2138 }
2139
2140 static bool conn_ov_running(struct drbd_connection *connection)
2141 {
2142 struct drbd_peer_device *peer_device;
2143 bool rv = false;
2144 int vnr;
2145
2146 rcu_read_lock();
2147 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
2148 struct drbd_device *device = peer_device->device;
2149 if (device->state.conn == C_VERIFY_S ||
2150 device->state.conn == C_VERIFY_T) {
2151 rv = true;
2152 break;
2153 }
2154 }
2155 rcu_read_unlock();
2156
2157 return rv;
2158 }
2159
2160 static enum drbd_ret_code
2161 _check_net_options(struct drbd_connection *connection, struct net_conf *old_net_conf, struct net_conf *new_net_conf)
2162 {
2163 struct drbd_peer_device *peer_device;
2164 int i;
2165
2166 if (old_net_conf && connection->cstate == C_WF_REPORT_PARAMS && connection->agreed_pro_version < 100) {
2167 if (new_net_conf->wire_protocol != old_net_conf->wire_protocol)
2168 return ERR_NEED_APV_100;
2169
2170 if (new_net_conf->two_primaries != old_net_conf->two_primaries)
2171 return ERR_NEED_APV_100;
2172
2173 if (strcmp(new_net_conf->integrity_alg, old_net_conf->integrity_alg))
2174 return ERR_NEED_APV_100;
2175 }
2176
2177 if (!new_net_conf->two_primaries &&
2178 conn_highest_role(connection) == R_PRIMARY &&
2179 conn_highest_peer(connection) == R_PRIMARY)
2180 return ERR_NEED_ALLOW_TWO_PRI;
2181
2182 if (new_net_conf->two_primaries &&
2183 (new_net_conf->wire_protocol != DRBD_PROT_C))
2184 return ERR_NOT_PROTO_C;
2185
2186 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
2187 struct drbd_device *device = peer_device->device;
2188 if (get_ldev(device)) {
2189 enum drbd_fencing_p fp = rcu_dereference(device->ldev->disk_conf)->fencing;
2190 put_ldev(device);
2191 if (new_net_conf->wire_protocol == DRBD_PROT_A && fp == FP_STONITH)
2192 return ERR_STONITH_AND_PROT_A;
2193 }
2194 if (device->state.role == R_PRIMARY && new_net_conf->discard_my_data)
2195 return ERR_DISCARD_IMPOSSIBLE;
2196 }
2197
2198 if (new_net_conf->on_congestion != OC_BLOCK && new_net_conf->wire_protocol != DRBD_PROT_A)
2199 return ERR_CONG_NOT_PROTO_A;
2200
2201 return NO_ERROR;
2202 }
2203
2204 static enum drbd_ret_code
2205 check_net_options(struct drbd_connection *connection, struct net_conf *new_net_conf)
2206 {
2207 static enum drbd_ret_code rv;
2208 struct drbd_peer_device *peer_device;
2209 int i;
2210
2211 rcu_read_lock();
2212 rv = _check_net_options(connection, rcu_dereference(connection->net_conf), new_net_conf);
2213 rcu_read_unlock();
2214
2215 /* connection->peer_devices protected by genl_lock() here */
2216 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
2217 struct drbd_device *device = peer_device->device;
2218 if (!device->bitmap) {
2219 if (drbd_bm_init(device))
2220 return ERR_NOMEM;
2221 }
2222 }
2223
2224 return rv;
2225 }
2226
2227 struct crypto {
2228 struct crypto_ahash *verify_tfm;
2229 struct crypto_ahash *csums_tfm;
2230 struct crypto_shash *cram_hmac_tfm;
2231 struct crypto_ahash *integrity_tfm;
2232 };
2233
2234 static int
2235 alloc_shash(struct crypto_shash **tfm, char *tfm_name, int err_alg)
2236 {
2237 if (!tfm_name[0])
2238 return NO_ERROR;
2239
2240 *tfm = crypto_alloc_shash(tfm_name, 0, 0);
2241 if (IS_ERR(*tfm)) {
2242 *tfm = NULL;
2243 return err_alg;
2244 }
2245
2246 return NO_ERROR;
2247 }
2248
2249 static int
2250 alloc_ahash(struct crypto_ahash **tfm, char *tfm_name, int err_alg)
2251 {
2252 if (!tfm_name[0])
2253 return NO_ERROR;
2254
2255 *tfm = crypto_alloc_ahash(tfm_name, 0, CRYPTO_ALG_ASYNC);
2256 if (IS_ERR(*tfm)) {
2257 *tfm = NULL;
2258 return err_alg;
2259 }
2260
2261 return NO_ERROR;
2262 }
2263
2264 static enum drbd_ret_code
2265 alloc_crypto(struct crypto *crypto, struct net_conf *new_net_conf)
2266 {
2267 char hmac_name[CRYPTO_MAX_ALG_NAME];
2268 enum drbd_ret_code rv;
2269
2270 rv = alloc_ahash(&crypto->csums_tfm, new_net_conf->csums_alg,
2271 ERR_CSUMS_ALG);
2272 if (rv != NO_ERROR)
2273 return rv;
2274 rv = alloc_ahash(&crypto->verify_tfm, new_net_conf->verify_alg,
2275 ERR_VERIFY_ALG);
2276 if (rv != NO_ERROR)
2277 return rv;
2278 rv = alloc_ahash(&crypto->integrity_tfm, new_net_conf->integrity_alg,
2279 ERR_INTEGRITY_ALG);
2280 if (rv != NO_ERROR)
2281 return rv;
2282 if (new_net_conf->cram_hmac_alg[0] != 0) {
2283 snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)",
2284 new_net_conf->cram_hmac_alg);
2285
2286 rv = alloc_shash(&crypto->cram_hmac_tfm, hmac_name,
2287 ERR_AUTH_ALG);
2288 }
2289
2290 return rv;
2291 }
2292
2293 static void free_crypto(struct crypto *crypto)
2294 {
2295 crypto_free_shash(crypto->cram_hmac_tfm);
2296 crypto_free_ahash(crypto->integrity_tfm);
2297 crypto_free_ahash(crypto->csums_tfm);
2298 crypto_free_ahash(crypto->verify_tfm);
2299 }
2300
2301 int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info)
2302 {
2303 struct drbd_config_context adm_ctx;
2304 enum drbd_ret_code retcode;
2305 struct drbd_connection *connection;
2306 struct net_conf *old_net_conf, *new_net_conf = NULL;
2307 int err;
2308 int ovr; /* online verify running */
2309 int rsr; /* re-sync running */
2310 struct crypto crypto = { };
2311
2312 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_CONNECTION);
2313 if (!adm_ctx.reply_skb)
2314 return retcode;
2315 if (retcode != NO_ERROR)
2316 goto finish;
2317
2318 connection = adm_ctx.connection;
2319 mutex_lock(&adm_ctx.resource->adm_mutex);
2320
2321 new_net_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
2322 if (!new_net_conf) {
2323 retcode = ERR_NOMEM;
2324 goto out;
2325 }
2326
2327 conn_reconfig_start(connection);
2328
2329 mutex_lock(&connection->data.mutex);
2330 mutex_lock(&connection->resource->conf_update);
2331 old_net_conf = connection->net_conf;
2332
2333 if (!old_net_conf) {
2334 drbd_msg_put_info(adm_ctx.reply_skb, "net conf missing, try connect");
2335 retcode = ERR_INVALID_REQUEST;
2336 goto fail;
2337 }
2338
2339 *new_net_conf = *old_net_conf;
2340 if (should_set_defaults(info))
2341 set_net_conf_defaults(new_net_conf);
2342
2343 err = net_conf_from_attrs_for_change(new_net_conf, info);
2344 if (err && err != -ENOMSG) {
2345 retcode = ERR_MANDATORY_TAG;
2346 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
2347 goto fail;
2348 }
2349
2350 retcode = check_net_options(connection, new_net_conf);
2351 if (retcode != NO_ERROR)
2352 goto fail;
2353
2354 /* re-sync running */
2355 rsr = conn_resync_running(connection);
2356 if (rsr && strcmp(new_net_conf->csums_alg, old_net_conf->csums_alg)) {
2357 retcode = ERR_CSUMS_RESYNC_RUNNING;
2358 goto fail;
2359 }
2360
2361 /* online verify running */
2362 ovr = conn_ov_running(connection);
2363 if (ovr && strcmp(new_net_conf->verify_alg, old_net_conf->verify_alg)) {
2364 retcode = ERR_VERIFY_RUNNING;
2365 goto fail;
2366 }
2367
2368 retcode = alloc_crypto(&crypto, new_net_conf);
2369 if (retcode != NO_ERROR)
2370 goto fail;
2371
2372 rcu_assign_pointer(connection->net_conf, new_net_conf);
2373
2374 if (!rsr) {
2375 crypto_free_ahash(connection->csums_tfm);
2376 connection->csums_tfm = crypto.csums_tfm;
2377 crypto.csums_tfm = NULL;
2378 }
2379 if (!ovr) {
2380 crypto_free_ahash(connection->verify_tfm);
2381 connection->verify_tfm = crypto.verify_tfm;
2382 crypto.verify_tfm = NULL;
2383 }
2384
2385 crypto_free_ahash(connection->integrity_tfm);
2386 connection->integrity_tfm = crypto.integrity_tfm;
2387 if (connection->cstate >= C_WF_REPORT_PARAMS && connection->agreed_pro_version >= 100)
2388 /* Do this without trying to take connection->data.mutex again. */
2389 __drbd_send_protocol(connection, P_PROTOCOL_UPDATE);
2390
2391 crypto_free_shash(connection->cram_hmac_tfm);
2392 connection->cram_hmac_tfm = crypto.cram_hmac_tfm;
2393
2394 mutex_unlock(&connection->resource->conf_update);
2395 mutex_unlock(&connection->data.mutex);
2396 synchronize_rcu();
2397 kfree(old_net_conf);
2398
2399 if (connection->cstate >= C_WF_REPORT_PARAMS) {
2400 struct drbd_peer_device *peer_device;
2401 int vnr;
2402
2403 idr_for_each_entry(&connection->peer_devices, peer_device, vnr)
2404 drbd_send_sync_param(peer_device);
2405 }
2406
2407 goto done;
2408
2409 fail:
2410 mutex_unlock(&connection->resource->conf_update);
2411 mutex_unlock(&connection->data.mutex);
2412 free_crypto(&crypto);
2413 kfree(new_net_conf);
2414 done:
2415 conn_reconfig_done(connection);
2416 out:
2417 mutex_unlock(&adm_ctx.resource->adm_mutex);
2418 finish:
2419 drbd_adm_finish(&adm_ctx, info, retcode);
2420 return 0;
2421 }
2422
2423 static void connection_to_info(struct connection_info *info,
2424 struct drbd_connection *connection)
2425 {
2426 info->conn_connection_state = connection->cstate;
2427 info->conn_role = conn_highest_peer(connection);
2428 }
2429
2430 static void peer_device_to_info(struct peer_device_info *info,
2431 struct drbd_peer_device *peer_device)
2432 {
2433 struct drbd_device *device = peer_device->device;
2434
2435 info->peer_repl_state =
2436 max_t(enum drbd_conns, C_WF_REPORT_PARAMS, device->state.conn);
2437 info->peer_disk_state = device->state.pdsk;
2438 info->peer_resync_susp_user = device->state.user_isp;
2439 info->peer_resync_susp_peer = device->state.peer_isp;
2440 info->peer_resync_susp_dependency = device->state.aftr_isp;
2441 }
2442
2443 int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info)
2444 {
2445 struct connection_info connection_info;
2446 enum drbd_notification_type flags;
2447 unsigned int peer_devices = 0;
2448 struct drbd_config_context adm_ctx;
2449 struct drbd_peer_device *peer_device;
2450 struct net_conf *old_net_conf, *new_net_conf = NULL;
2451 struct crypto crypto = { };
2452 struct drbd_resource *resource;
2453 struct drbd_connection *connection;
2454 enum drbd_ret_code retcode;
2455 int i;
2456 int err;
2457
2458 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
2459
2460 if (!adm_ctx.reply_skb)
2461 return retcode;
2462 if (retcode != NO_ERROR)
2463 goto out;
2464 if (!(adm_ctx.my_addr && adm_ctx.peer_addr)) {
2465 drbd_msg_put_info(adm_ctx.reply_skb, "connection endpoint(s) missing");
2466 retcode = ERR_INVALID_REQUEST;
2467 goto out;
2468 }
2469
2470 /* No need for _rcu here. All reconfiguration is
2471 * strictly serialized on genl_lock(). We are protected against
2472 * concurrent reconfiguration/addition/deletion */
2473 for_each_resource(resource, &drbd_resources) {
2474 for_each_connection(connection, resource) {
2475 if (nla_len(adm_ctx.my_addr) == connection->my_addr_len &&
2476 !memcmp(nla_data(adm_ctx.my_addr), &connection->my_addr,
2477 connection->my_addr_len)) {
2478 retcode = ERR_LOCAL_ADDR;
2479 goto out;
2480 }
2481
2482 if (nla_len(adm_ctx.peer_addr) == connection->peer_addr_len &&
2483 !memcmp(nla_data(adm_ctx.peer_addr), &connection->peer_addr,
2484 connection->peer_addr_len)) {
2485 retcode = ERR_PEER_ADDR;
2486 goto out;
2487 }
2488 }
2489 }
2490
2491 mutex_lock(&adm_ctx.resource->adm_mutex);
2492 connection = first_connection(adm_ctx.resource);
2493 conn_reconfig_start(connection);
2494
2495 if (connection->cstate > C_STANDALONE) {
2496 retcode = ERR_NET_CONFIGURED;
2497 goto fail;
2498 }
2499
2500 /* allocation not in the IO path, drbdsetup / netlink process context */
2501 new_net_conf = kzalloc(sizeof(*new_net_conf), GFP_KERNEL);
2502 if (!new_net_conf) {
2503 retcode = ERR_NOMEM;
2504 goto fail;
2505 }
2506
2507 set_net_conf_defaults(new_net_conf);
2508
2509 err = net_conf_from_attrs(new_net_conf, info);
2510 if (err && err != -ENOMSG) {
2511 retcode = ERR_MANDATORY_TAG;
2512 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
2513 goto fail;
2514 }
2515
2516 retcode = check_net_options(connection, new_net_conf);
2517 if (retcode != NO_ERROR)
2518 goto fail;
2519
2520 retcode = alloc_crypto(&crypto, new_net_conf);
2521 if (retcode != NO_ERROR)
2522 goto fail;
2523
2524 ((char *)new_net_conf->shared_secret)[SHARED_SECRET_MAX-1] = 0;
2525
2526 drbd_flush_workqueue(&connection->sender_work);
2527
2528 mutex_lock(&adm_ctx.resource->conf_update);
2529 old_net_conf = connection->net_conf;
2530 if (old_net_conf) {
2531 retcode = ERR_NET_CONFIGURED;
2532 mutex_unlock(&adm_ctx.resource->conf_update);
2533 goto fail;
2534 }
2535 rcu_assign_pointer(connection->net_conf, new_net_conf);
2536
2537 conn_free_crypto(connection);
2538 connection->cram_hmac_tfm = crypto.cram_hmac_tfm;
2539 connection->integrity_tfm = crypto.integrity_tfm;
2540 connection->csums_tfm = crypto.csums_tfm;
2541 connection->verify_tfm = crypto.verify_tfm;
2542
2543 connection->my_addr_len = nla_len(adm_ctx.my_addr);
2544 memcpy(&connection->my_addr, nla_data(adm_ctx.my_addr), connection->my_addr_len);
2545 connection->peer_addr_len = nla_len(adm_ctx.peer_addr);
2546 memcpy(&connection->peer_addr, nla_data(adm_ctx.peer_addr), connection->peer_addr_len);
2547
2548 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
2549 peer_devices++;
2550 }
2551
2552 connection_to_info(&connection_info, connection);
2553 flags = (peer_devices--) ? NOTIFY_CONTINUES : 0;
2554 mutex_lock(&notification_mutex);
2555 notify_connection_state(NULL, 0, connection, &connection_info, NOTIFY_CREATE | flags);
2556 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
2557 struct peer_device_info peer_device_info;
2558
2559 peer_device_to_info(&peer_device_info, peer_device);
2560 flags = (peer_devices--) ? NOTIFY_CONTINUES : 0;
2561 notify_peer_device_state(NULL, 0, peer_device, &peer_device_info, NOTIFY_CREATE | flags);
2562 }
2563 mutex_unlock(&notification_mutex);
2564 mutex_unlock(&adm_ctx.resource->conf_update);
2565
2566 rcu_read_lock();
2567 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
2568 struct drbd_device *device = peer_device->device;
2569 device->send_cnt = 0;
2570 device->recv_cnt = 0;
2571 }
2572 rcu_read_unlock();
2573
2574 retcode = conn_request_state(connection, NS(conn, C_UNCONNECTED), CS_VERBOSE);
2575
2576 conn_reconfig_done(connection);
2577 mutex_unlock(&adm_ctx.resource->adm_mutex);
2578 drbd_adm_finish(&adm_ctx, info, retcode);
2579 return 0;
2580
2581 fail:
2582 free_crypto(&crypto);
2583 kfree(new_net_conf);
2584
2585 conn_reconfig_done(connection);
2586 mutex_unlock(&adm_ctx.resource->adm_mutex);
2587 out:
2588 drbd_adm_finish(&adm_ctx, info, retcode);
2589 return 0;
2590 }
2591
2592 static enum drbd_state_rv conn_try_disconnect(struct drbd_connection *connection, bool force)
2593 {
2594 enum drbd_state_rv rv;
2595
2596 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING),
2597 force ? CS_HARD : 0);
2598
2599 switch (rv) {
2600 case SS_NOTHING_TO_DO:
2601 break;
2602 case SS_ALREADY_STANDALONE:
2603 return SS_SUCCESS;
2604 case SS_PRIMARY_NOP:
2605 /* Our state checking code wants to see the peer outdated. */
2606 rv = conn_request_state(connection, NS2(conn, C_DISCONNECTING, pdsk, D_OUTDATED), 0);
2607
2608 if (rv == SS_OUTDATE_WO_CONN) /* lost connection before graceful disconnect succeeded */
2609 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING), CS_VERBOSE);
2610
2611 break;
2612 case SS_CW_FAILED_BY_PEER:
2613 /* The peer probably wants to see us outdated. */
2614 rv = conn_request_state(connection, NS2(conn, C_DISCONNECTING,
2615 disk, D_OUTDATED), 0);
2616 if (rv == SS_IS_DISKLESS || rv == SS_LOWER_THAN_OUTDATED) {
2617 rv = conn_request_state(connection, NS(conn, C_DISCONNECTING),
2618 CS_HARD);
2619 }
2620 break;
2621 default:;
2622 /* no special handling necessary */
2623 }
2624
2625 if (rv >= SS_SUCCESS) {
2626 enum drbd_state_rv rv2;
2627 /* No one else can reconfigure the network while I am here.
2628 * The state handling only uses drbd_thread_stop_nowait(),
2629 * we want to really wait here until the receiver is no more.
2630 */
2631 drbd_thread_stop(&connection->receiver);
2632
2633 /* Race breaker. This additional state change request may be
2634 * necessary, if this was a forced disconnect during a receiver
2635 * restart. We may have "killed" the receiver thread just
2636 * after drbd_receiver() returned. Typically, we should be
2637 * C_STANDALONE already, now, and this becomes a no-op.
2638 */
2639 rv2 = conn_request_state(connection, NS(conn, C_STANDALONE),
2640 CS_VERBOSE | CS_HARD);
2641 if (rv2 < SS_SUCCESS)
2642 drbd_err(connection,
2643 "unexpected rv2=%d in conn_try_disconnect()\n",
2644 rv2);
2645 /* Unlike in DRBD 9, the state engine has generated
2646 * NOTIFY_DESTROY events before clearing connection->net_conf. */
2647 }
2648 return rv;
2649 }
2650
2651 int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info)
2652 {
2653 struct drbd_config_context adm_ctx;
2654 struct disconnect_parms parms;
2655 struct drbd_connection *connection;
2656 enum drbd_state_rv rv;
2657 enum drbd_ret_code retcode;
2658 int err;
2659
2660 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_CONNECTION);
2661 if (!adm_ctx.reply_skb)
2662 return retcode;
2663 if (retcode != NO_ERROR)
2664 goto fail;
2665
2666 connection = adm_ctx.connection;
2667 memset(&parms, 0, sizeof(parms));
2668 if (info->attrs[DRBD_NLA_DISCONNECT_PARMS]) {
2669 err = disconnect_parms_from_attrs(&parms, info);
2670 if (err) {
2671 retcode = ERR_MANDATORY_TAG;
2672 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
2673 goto fail;
2674 }
2675 }
2676
2677 mutex_lock(&adm_ctx.resource->adm_mutex);
2678 rv = conn_try_disconnect(connection, parms.force_disconnect);
2679 if (rv < SS_SUCCESS)
2680 retcode = rv; /* FIXME: Type mismatch. */
2681 else
2682 retcode = NO_ERROR;
2683 mutex_unlock(&adm_ctx.resource->adm_mutex);
2684 fail:
2685 drbd_adm_finish(&adm_ctx, info, retcode);
2686 return 0;
2687 }
2688
2689 void resync_after_online_grow(struct drbd_device *device)
2690 {
2691 int iass; /* I am sync source */
2692
2693 drbd_info(device, "Resync of new storage after online grow\n");
2694 if (device->state.role != device->state.peer)
2695 iass = (device->state.role == R_PRIMARY);
2696 else
2697 iass = test_bit(RESOLVE_CONFLICTS, &first_peer_device(device)->connection->flags);
2698
2699 if (iass)
2700 drbd_start_resync(device, C_SYNC_SOURCE);
2701 else
2702 _drbd_request_state(device, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE + CS_SERIALIZE);
2703 }
2704
2705 int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info)
2706 {
2707 struct drbd_config_context adm_ctx;
2708 struct disk_conf *old_disk_conf, *new_disk_conf = NULL;
2709 struct resize_parms rs;
2710 struct drbd_device *device;
2711 enum drbd_ret_code retcode;
2712 enum determine_dev_size dd;
2713 bool change_al_layout = false;
2714 enum dds_flags ddsf;
2715 sector_t u_size;
2716 int err;
2717
2718 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
2719 if (!adm_ctx.reply_skb)
2720 return retcode;
2721 if (retcode != NO_ERROR)
2722 goto finish;
2723
2724 mutex_lock(&adm_ctx.resource->adm_mutex);
2725 device = adm_ctx.device;
2726 if (!get_ldev(device)) {
2727 retcode = ERR_NO_DISK;
2728 goto fail;
2729 }
2730
2731 memset(&rs, 0, sizeof(struct resize_parms));
2732 rs.al_stripes = device->ldev->md.al_stripes;
2733 rs.al_stripe_size = device->ldev->md.al_stripe_size_4k * 4;
2734 if (info->attrs[DRBD_NLA_RESIZE_PARMS]) {
2735 err = resize_parms_from_attrs(&rs, info);
2736 if (err) {
2737 retcode = ERR_MANDATORY_TAG;
2738 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
2739 goto fail_ldev;
2740 }
2741 }
2742
2743 if (device->state.conn > C_CONNECTED) {
2744 retcode = ERR_RESIZE_RESYNC;
2745 goto fail_ldev;
2746 }
2747
2748 if (device->state.role == R_SECONDARY &&
2749 device->state.peer == R_SECONDARY) {
2750 retcode = ERR_NO_PRIMARY;
2751 goto fail_ldev;
2752 }
2753
2754 if (rs.no_resync && first_peer_device(device)->connection->agreed_pro_version < 93) {
2755 retcode = ERR_NEED_APV_93;
2756 goto fail_ldev;
2757 }
2758
2759 rcu_read_lock();
2760 u_size = rcu_dereference(device->ldev->disk_conf)->disk_size;
2761 rcu_read_unlock();
2762 if (u_size != (sector_t)rs.resize_size) {
2763 new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
2764 if (!new_disk_conf) {
2765 retcode = ERR_NOMEM;
2766 goto fail_ldev;
2767 }
2768 }
2769
2770 if (device->ldev->md.al_stripes != rs.al_stripes ||
2771 device->ldev->md.al_stripe_size_4k != rs.al_stripe_size / 4) {
2772 u32 al_size_k = rs.al_stripes * rs.al_stripe_size;
2773
2774 if (al_size_k > (16 * 1024 * 1024)) {
2775 retcode = ERR_MD_LAYOUT_TOO_BIG;
2776 goto fail_ldev;
2777 }
2778
2779 if (al_size_k < MD_32kB_SECT/2) {
2780 retcode = ERR_MD_LAYOUT_TOO_SMALL;
2781 goto fail_ldev;
2782 }
2783
2784 if (device->state.conn != C_CONNECTED && !rs.resize_force) {
2785 retcode = ERR_MD_LAYOUT_CONNECTED;
2786 goto fail_ldev;
2787 }
2788
2789 change_al_layout = true;
2790 }
2791
2792 if (device->ldev->known_size != drbd_get_capacity(device->ldev->backing_bdev))
2793 device->ldev->known_size = drbd_get_capacity(device->ldev->backing_bdev);
2794
2795 if (new_disk_conf) {
2796 mutex_lock(&device->resource->conf_update);
2797 old_disk_conf = device->ldev->disk_conf;
2798 *new_disk_conf = *old_disk_conf;
2799 new_disk_conf->disk_size = (sector_t)rs.resize_size;
2800 rcu_assign_pointer(device->ldev->disk_conf, new_disk_conf);
2801 mutex_unlock(&device->resource->conf_update);
2802 synchronize_rcu();
2803 kfree(old_disk_conf);
2804 new_disk_conf = NULL;
2805 }
2806
2807 ddsf = (rs.resize_force ? DDSF_FORCED : 0) | (rs.no_resync ? DDSF_NO_RESYNC : 0);
2808 dd = drbd_determine_dev_size(device, ddsf, change_al_layout ? &rs : NULL);
2809 drbd_md_sync(device);
2810 put_ldev(device);
2811 if (dd == DS_ERROR) {
2812 retcode = ERR_NOMEM_BITMAP;
2813 goto fail;
2814 } else if (dd == DS_ERROR_SPACE_MD) {
2815 retcode = ERR_MD_LAYOUT_NO_FIT;
2816 goto fail;
2817 } else if (dd == DS_ERROR_SHRINK) {
2818 retcode = ERR_IMPLICIT_SHRINK;
2819 goto fail;
2820 }
2821
2822 if (device->state.conn == C_CONNECTED) {
2823 if (dd == DS_GREW)
2824 set_bit(RESIZE_PENDING, &device->flags);
2825
2826 drbd_send_uuids(first_peer_device(device));
2827 drbd_send_sizes(first_peer_device(device), 1, ddsf);
2828 }
2829
2830 fail:
2831 mutex_unlock(&adm_ctx.resource->adm_mutex);
2832 finish:
2833 drbd_adm_finish(&adm_ctx, info, retcode);
2834 return 0;
2835
2836 fail_ldev:
2837 put_ldev(device);
2838 kfree(new_disk_conf);
2839 goto fail;
2840 }
2841
2842 int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info)
2843 {
2844 struct drbd_config_context adm_ctx;
2845 enum drbd_ret_code retcode;
2846 struct res_opts res_opts;
2847 int err;
2848
2849 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
2850 if (!adm_ctx.reply_skb)
2851 return retcode;
2852 if (retcode != NO_ERROR)
2853 goto fail;
2854
2855 res_opts = adm_ctx.resource->res_opts;
2856 if (should_set_defaults(info))
2857 set_res_opts_defaults(&res_opts);
2858
2859 err = res_opts_from_attrs(&res_opts, info);
2860 if (err && err != -ENOMSG) {
2861 retcode = ERR_MANDATORY_TAG;
2862 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
2863 goto fail;
2864 }
2865
2866 mutex_lock(&adm_ctx.resource->adm_mutex);
2867 err = set_resource_options(adm_ctx.resource, &res_opts);
2868 if (err) {
2869 retcode = ERR_INVALID_REQUEST;
2870 if (err == -ENOMEM)
2871 retcode = ERR_NOMEM;
2872 }
2873 mutex_unlock(&adm_ctx.resource->adm_mutex);
2874
2875 fail:
2876 drbd_adm_finish(&adm_ctx, info, retcode);
2877 return 0;
2878 }
2879
2880 int drbd_adm_invalidate(struct sk_buff *skb, struct genl_info *info)
2881 {
2882 struct drbd_config_context adm_ctx;
2883 struct drbd_device *device;
2884 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
2885
2886 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
2887 if (!adm_ctx.reply_skb)
2888 return retcode;
2889 if (retcode != NO_ERROR)
2890 goto out;
2891
2892 device = adm_ctx.device;
2893 if (!get_ldev(device)) {
2894 retcode = ERR_NO_DISK;
2895 goto out;
2896 }
2897
2898 mutex_lock(&adm_ctx.resource->adm_mutex);
2899
2900 /* If there is still bitmap IO pending, probably because of a previous
2901 * resync just being finished, wait for it before requesting a new resync.
2902 * Also wait for it's after_state_ch(). */
2903 drbd_suspend_io(device);
2904 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
2905 drbd_flush_workqueue(&first_peer_device(device)->connection->sender_work);
2906
2907 /* If we happen to be C_STANDALONE R_SECONDARY, just change to
2908 * D_INCONSISTENT, and set all bits in the bitmap. Otherwise,
2909 * try to start a resync handshake as sync target for full sync.
2910 */
2911 if (device->state.conn == C_STANDALONE && device->state.role == R_SECONDARY) {
2912 retcode = drbd_request_state(device, NS(disk, D_INCONSISTENT));
2913 if (retcode >= SS_SUCCESS) {
2914 if (drbd_bitmap_io(device, &drbd_bmio_set_n_write,
2915 "set_n_write from invalidate", BM_LOCKED_MASK))
2916 retcode = ERR_IO_MD_DISK;
2917 }
2918 } else
2919 retcode = drbd_request_state(device, NS(conn, C_STARTING_SYNC_T));
2920 drbd_resume_io(device);
2921 mutex_unlock(&adm_ctx.resource->adm_mutex);
2922 put_ldev(device);
2923 out:
2924 drbd_adm_finish(&adm_ctx, info, retcode);
2925 return 0;
2926 }
2927
2928 static int drbd_adm_simple_request_state(struct sk_buff *skb, struct genl_info *info,
2929 union drbd_state mask, union drbd_state val)
2930 {
2931 struct drbd_config_context adm_ctx;
2932 enum drbd_ret_code retcode;
2933
2934 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
2935 if (!adm_ctx.reply_skb)
2936 return retcode;
2937 if (retcode != NO_ERROR)
2938 goto out;
2939
2940 mutex_lock(&adm_ctx.resource->adm_mutex);
2941 retcode = drbd_request_state(adm_ctx.device, mask, val);
2942 mutex_unlock(&adm_ctx.resource->adm_mutex);
2943 out:
2944 drbd_adm_finish(&adm_ctx, info, retcode);
2945 return 0;
2946 }
2947
2948 static int drbd_bmio_set_susp_al(struct drbd_device *device) __must_hold(local)
2949 {
2950 int rv;
2951
2952 rv = drbd_bmio_set_n_write(device);
2953 drbd_suspend_al(device);
2954 return rv;
2955 }
2956
2957 int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info)
2958 {
2959 struct drbd_config_context adm_ctx;
2960 int retcode; /* drbd_ret_code, drbd_state_rv */
2961 struct drbd_device *device;
2962
2963 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
2964 if (!adm_ctx.reply_skb)
2965 return retcode;
2966 if (retcode != NO_ERROR)
2967 goto out;
2968
2969 device = adm_ctx.device;
2970 if (!get_ldev(device)) {
2971 retcode = ERR_NO_DISK;
2972 goto out;
2973 }
2974
2975 mutex_lock(&adm_ctx.resource->adm_mutex);
2976
2977 /* If there is still bitmap IO pending, probably because of a previous
2978 * resync just being finished, wait for it before requesting a new resync.
2979 * Also wait for it's after_state_ch(). */
2980 drbd_suspend_io(device);
2981 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
2982 drbd_flush_workqueue(&first_peer_device(device)->connection->sender_work);
2983
2984 /* If we happen to be C_STANDALONE R_PRIMARY, just set all bits
2985 * in the bitmap. Otherwise, try to start a resync handshake
2986 * as sync source for full sync.
2987 */
2988 if (device->state.conn == C_STANDALONE && device->state.role == R_PRIMARY) {
2989 /* The peer will get a resync upon connect anyways. Just make that
2990 into a full resync. */
2991 retcode = drbd_request_state(device, NS(pdsk, D_INCONSISTENT));
2992 if (retcode >= SS_SUCCESS) {
2993 if (drbd_bitmap_io(device, &drbd_bmio_set_susp_al,
2994 "set_n_write from invalidate_peer",
2995 BM_LOCKED_SET_ALLOWED))
2996 retcode = ERR_IO_MD_DISK;
2997 }
2998 } else
2999 retcode = drbd_request_state(device, NS(conn, C_STARTING_SYNC_S));
3000 drbd_resume_io(device);
3001 mutex_unlock(&adm_ctx.resource->adm_mutex);
3002 put_ldev(device);
3003 out:
3004 drbd_adm_finish(&adm_ctx, info, retcode);
3005 return 0;
3006 }
3007
3008 int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info)
3009 {
3010 struct drbd_config_context adm_ctx;
3011 enum drbd_ret_code retcode;
3012
3013 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3014 if (!adm_ctx.reply_skb)
3015 return retcode;
3016 if (retcode != NO_ERROR)
3017 goto out;
3018
3019 mutex_lock(&adm_ctx.resource->adm_mutex);
3020 if (drbd_request_state(adm_ctx.device, NS(user_isp, 1)) == SS_NOTHING_TO_DO)
3021 retcode = ERR_PAUSE_IS_SET;
3022 mutex_unlock(&adm_ctx.resource->adm_mutex);
3023 out:
3024 drbd_adm_finish(&adm_ctx, info, retcode);
3025 return 0;
3026 }
3027
3028 int drbd_adm_resume_sync(struct sk_buff *skb, struct genl_info *info)
3029 {
3030 struct drbd_config_context adm_ctx;
3031 union drbd_dev_state s;
3032 enum drbd_ret_code retcode;
3033
3034 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3035 if (!adm_ctx.reply_skb)
3036 return retcode;
3037 if (retcode != NO_ERROR)
3038 goto out;
3039
3040 mutex_lock(&adm_ctx.resource->adm_mutex);
3041 if (drbd_request_state(adm_ctx.device, NS(user_isp, 0)) == SS_NOTHING_TO_DO) {
3042 s = adm_ctx.device->state;
3043 if (s.conn == C_PAUSED_SYNC_S || s.conn == C_PAUSED_SYNC_T) {
3044 retcode = s.aftr_isp ? ERR_PIC_AFTER_DEP :
3045 s.peer_isp ? ERR_PIC_PEER_DEP : ERR_PAUSE_IS_CLEAR;
3046 } else {
3047 retcode = ERR_PAUSE_IS_CLEAR;
3048 }
3049 }
3050 mutex_unlock(&adm_ctx.resource->adm_mutex);
3051 out:
3052 drbd_adm_finish(&adm_ctx, info, retcode);
3053 return 0;
3054 }
3055
3056 int drbd_adm_suspend_io(struct sk_buff *skb, struct genl_info *info)
3057 {
3058 return drbd_adm_simple_request_state(skb, info, NS(susp, 1));
3059 }
3060
3061 int drbd_adm_resume_io(struct sk_buff *skb, struct genl_info *info)
3062 {
3063 struct drbd_config_context adm_ctx;
3064 struct drbd_device *device;
3065 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
3066
3067 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3068 if (!adm_ctx.reply_skb)
3069 return retcode;
3070 if (retcode != NO_ERROR)
3071 goto out;
3072
3073 mutex_lock(&adm_ctx.resource->adm_mutex);
3074 device = adm_ctx.device;
3075 if (test_bit(NEW_CUR_UUID, &device->flags)) {
3076 if (get_ldev_if_state(device, D_ATTACHING)) {
3077 drbd_uuid_new_current(device);
3078 put_ldev(device);
3079 } else {
3080 /* This is effectively a multi-stage "forced down".
3081 * The NEW_CUR_UUID bit is supposedly only set, if we
3082 * lost the replication connection, and are configured
3083 * to freeze IO and wait for some fence-peer handler.
3084 * So we still don't have a replication connection.
3085 * And now we don't have a local disk either. After
3086 * resume, we will fail all pending and new IO, because
3087 * we don't have any data anymore. Which means we will
3088 * eventually be able to terminate all users of this
3089 * device, and then take it down. By bumping the
3090 * "effective" data uuid, we make sure that you really
3091 * need to tear down before you reconfigure, we will
3092 * the refuse to re-connect or re-attach (because no
3093 * matching real data uuid exists).
3094 */
3095 u64 val;
3096 get_random_bytes(&val, sizeof(u64));
3097 drbd_set_ed_uuid(device, val);
3098 drbd_warn(device, "Resumed without access to data; please tear down before attempting to re-configure.\n");
3099 }
3100 clear_bit(NEW_CUR_UUID, &device->flags);
3101 }
3102 drbd_suspend_io(device);
3103 retcode = drbd_request_state(device, NS3(susp, 0, susp_nod, 0, susp_fen, 0));
3104 if (retcode == SS_SUCCESS) {
3105 if (device->state.conn < C_CONNECTED)
3106 tl_clear(first_peer_device(device)->connection);
3107 if (device->state.disk == D_DISKLESS || device->state.disk == D_FAILED)
3108 tl_restart(first_peer_device(device)->connection, FAIL_FROZEN_DISK_IO);
3109 }
3110 drbd_resume_io(device);
3111 mutex_unlock(&adm_ctx.resource->adm_mutex);
3112 out:
3113 drbd_adm_finish(&adm_ctx, info, retcode);
3114 return 0;
3115 }
3116
3117 int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info)
3118 {
3119 return drbd_adm_simple_request_state(skb, info, NS(disk, D_OUTDATED));
3120 }
3121
3122 static int nla_put_drbd_cfg_context(struct sk_buff *skb,
3123 struct drbd_resource *resource,
3124 struct drbd_connection *connection,
3125 struct drbd_device *device)
3126 {
3127 struct nlattr *nla;
3128 nla = nla_nest_start(skb, DRBD_NLA_CFG_CONTEXT);
3129 if (!nla)
3130 goto nla_put_failure;
3131 if (device &&
3132 nla_put_u32(skb, T_ctx_volume, device->vnr))
3133 goto nla_put_failure;
3134 if (nla_put_string(skb, T_ctx_resource_name, resource->name))
3135 goto nla_put_failure;
3136 if (connection) {
3137 if (connection->my_addr_len &&
3138 nla_put(skb, T_ctx_my_addr, connection->my_addr_len, &connection->my_addr))
3139 goto nla_put_failure;
3140 if (connection->peer_addr_len &&
3141 nla_put(skb, T_ctx_peer_addr, connection->peer_addr_len, &connection->peer_addr))
3142 goto nla_put_failure;
3143 }
3144 nla_nest_end(skb, nla);
3145 return 0;
3146
3147 nla_put_failure:
3148 if (nla)
3149 nla_nest_cancel(skb, nla);
3150 return -EMSGSIZE;
3151 }
3152
3153 /*
3154 * The generic netlink dump callbacks are called outside the genl_lock(), so
3155 * they cannot use the simple attribute parsing code which uses global
3156 * attribute tables.
3157 */
3158 static struct nlattr *find_cfg_context_attr(const struct nlmsghdr *nlh, int attr)
3159 {
3160 const unsigned hdrlen = GENL_HDRLEN + GENL_MAGIC_FAMILY_HDRSZ;
3161 const int maxtype = ARRAY_SIZE(drbd_cfg_context_nl_policy) - 1;
3162 struct nlattr *nla;
3163
3164 nla = nla_find(nlmsg_attrdata(nlh, hdrlen), nlmsg_attrlen(nlh, hdrlen),
3165 DRBD_NLA_CFG_CONTEXT);
3166 if (!nla)
3167 return NULL;
3168 return drbd_nla_find_nested(maxtype, nla, __nla_type(attr));
3169 }
3170
3171 static void resource_to_info(struct resource_info *, struct drbd_resource *);
3172
3173 int drbd_adm_dump_resources(struct sk_buff *skb, struct netlink_callback *cb)
3174 {
3175 struct drbd_genlmsghdr *dh;
3176 struct drbd_resource *resource;
3177 struct resource_info resource_info;
3178 struct resource_statistics resource_statistics;
3179 int err;
3180
3181 rcu_read_lock();
3182 if (cb->args[0]) {
3183 for_each_resource_rcu(resource, &drbd_resources)
3184 if (resource == (struct drbd_resource *)cb->args[0])
3185 goto found_resource;
3186 err = 0; /* resource was probably deleted */
3187 goto out;
3188 }
3189 resource = list_entry(&drbd_resources,
3190 struct drbd_resource, resources);
3191
3192 found_resource:
3193 list_for_each_entry_continue_rcu(resource, &drbd_resources, resources) {
3194 goto put_result;
3195 }
3196 err = 0;
3197 goto out;
3198
3199 put_result:
3200 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
3201 cb->nlh->nlmsg_seq, &drbd_genl_family,
3202 NLM_F_MULTI, DRBD_ADM_GET_RESOURCES);
3203 err = -ENOMEM;
3204 if (!dh)
3205 goto out;
3206 dh->minor = -1U;
3207 dh->ret_code = NO_ERROR;
3208 err = nla_put_drbd_cfg_context(skb, resource, NULL, NULL);
3209 if (err)
3210 goto out;
3211 err = res_opts_to_skb(skb, &resource->res_opts, !capable(CAP_SYS_ADMIN));
3212 if (err)
3213 goto out;
3214 resource_to_info(&resource_info, resource);
3215 err = resource_info_to_skb(skb, &resource_info, !capable(CAP_SYS_ADMIN));
3216 if (err)
3217 goto out;
3218 resource_statistics.res_stat_write_ordering = resource->write_ordering;
3219 err = resource_statistics_to_skb(skb, &resource_statistics, !capable(CAP_SYS_ADMIN));
3220 if (err)
3221 goto out;
3222 cb->args[0] = (long)resource;
3223 genlmsg_end(skb, dh);
3224 err = 0;
3225
3226 out:
3227 rcu_read_unlock();
3228 if (err)
3229 return err;
3230 return skb->len;
3231 }
3232
3233 static void device_to_statistics(struct device_statistics *s,
3234 struct drbd_device *device)
3235 {
3236 memset(s, 0, sizeof(*s));
3237 s->dev_upper_blocked = !may_inc_ap_bio(device);
3238 if (get_ldev(device)) {
3239 struct drbd_md *md = &device->ldev->md;
3240 u64 *history_uuids = (u64 *)s->history_uuids;
3241 struct request_queue *q;
3242 int n;
3243
3244 spin_lock_irq(&md->uuid_lock);
3245 s->dev_current_uuid = md->uuid[UI_CURRENT];
3246 BUILD_BUG_ON(sizeof(s->history_uuids) < UI_HISTORY_END - UI_HISTORY_START + 1);
3247 for (n = 0; n < UI_HISTORY_END - UI_HISTORY_START + 1; n++)
3248 history_uuids[n] = md->uuid[UI_HISTORY_START + n];
3249 for (; n < HISTORY_UUIDS; n++)
3250 history_uuids[n] = 0;
3251 s->history_uuids_len = HISTORY_UUIDS;
3252 spin_unlock_irq(&md->uuid_lock);
3253
3254 s->dev_disk_flags = md->flags;
3255 q = bdev_get_queue(device->ldev->backing_bdev);
3256 s->dev_lower_blocked =
3257 bdi_congested(&q->backing_dev_info,
3258 (1 << WB_async_congested) |
3259 (1 << WB_sync_congested));
3260 put_ldev(device);
3261 }
3262 s->dev_size = drbd_get_capacity(device->this_bdev);
3263 s->dev_read = device->read_cnt;
3264 s->dev_write = device->writ_cnt;
3265 s->dev_al_writes = device->al_writ_cnt;
3266 s->dev_bm_writes = device->bm_writ_cnt;
3267 s->dev_upper_pending = atomic_read(&device->ap_bio_cnt);
3268 s->dev_lower_pending = atomic_read(&device->local_cnt);
3269 s->dev_al_suspended = test_bit(AL_SUSPENDED, &device->flags);
3270 s->dev_exposed_data_uuid = device->ed_uuid;
3271 }
3272
3273 static int put_resource_in_arg0(struct netlink_callback *cb, int holder_nr)
3274 {
3275 if (cb->args[0]) {
3276 struct drbd_resource *resource =
3277 (struct drbd_resource *)cb->args[0];
3278 kref_put(&resource->kref, drbd_destroy_resource);
3279 }
3280
3281 return 0;
3282 }
3283
3284 int drbd_adm_dump_devices_done(struct netlink_callback *cb) {
3285 return put_resource_in_arg0(cb, 7);
3286 }
3287
3288 static void device_to_info(struct device_info *, struct drbd_device *);
3289
3290 int drbd_adm_dump_devices(struct sk_buff *skb, struct netlink_callback *cb)
3291 {
3292 struct nlattr *resource_filter;
3293 struct drbd_resource *resource;
3294 struct drbd_device *uninitialized_var(device);
3295 int minor, err, retcode;
3296 struct drbd_genlmsghdr *dh;
3297 struct device_info device_info;
3298 struct device_statistics device_statistics;
3299 struct idr *idr_to_search;
3300
3301 resource = (struct drbd_resource *)cb->args[0];
3302 if (!cb->args[0] && !cb->args[1]) {
3303 resource_filter = find_cfg_context_attr(cb->nlh, T_ctx_resource_name);
3304 if (resource_filter) {
3305 retcode = ERR_RES_NOT_KNOWN;
3306 resource = drbd_find_resource(nla_data(resource_filter));
3307 if (!resource)
3308 goto put_result;
3309 cb->args[0] = (long)resource;
3310 }
3311 }
3312
3313 rcu_read_lock();
3314 minor = cb->args[1];
3315 idr_to_search = resource ? &resource->devices : &drbd_devices;
3316 device = idr_get_next(idr_to_search, &minor);
3317 if (!device) {
3318 err = 0;
3319 goto out;
3320 }
3321 idr_for_each_entry_continue(idr_to_search, device, minor) {
3322 retcode = NO_ERROR;
3323 goto put_result; /* only one iteration */
3324 }
3325 err = 0;
3326 goto out; /* no more devices */
3327
3328 put_result:
3329 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
3330 cb->nlh->nlmsg_seq, &drbd_genl_family,
3331 NLM_F_MULTI, DRBD_ADM_GET_DEVICES);
3332 err = -ENOMEM;
3333 if (!dh)
3334 goto out;
3335 dh->ret_code = retcode;
3336 dh->minor = -1U;
3337 if (retcode == NO_ERROR) {
3338 dh->minor = device->minor;
3339 err = nla_put_drbd_cfg_context(skb, device->resource, NULL, device);
3340 if (err)
3341 goto out;
3342 if (get_ldev(device)) {
3343 struct disk_conf *disk_conf =
3344 rcu_dereference(device->ldev->disk_conf);
3345
3346 err = disk_conf_to_skb(skb, disk_conf, !capable(CAP_SYS_ADMIN));
3347 put_ldev(device);
3348 if (err)
3349 goto out;
3350 }
3351 device_to_info(&device_info, device);
3352 err = device_info_to_skb(skb, &device_info, !capable(CAP_SYS_ADMIN));
3353 if (err)
3354 goto out;
3355
3356 device_to_statistics(&device_statistics, device);
3357 err = device_statistics_to_skb(skb, &device_statistics, !capable(CAP_SYS_ADMIN));
3358 if (err)
3359 goto out;
3360 cb->args[1] = minor + 1;
3361 }
3362 genlmsg_end(skb, dh);
3363 err = 0;
3364
3365 out:
3366 rcu_read_unlock();
3367 if (err)
3368 return err;
3369 return skb->len;
3370 }
3371
3372 int drbd_adm_dump_connections_done(struct netlink_callback *cb)
3373 {
3374 return put_resource_in_arg0(cb, 6);
3375 }
3376
3377 enum { SINGLE_RESOURCE, ITERATE_RESOURCES };
3378
3379 int drbd_adm_dump_connections(struct sk_buff *skb, struct netlink_callback *cb)
3380 {
3381 struct nlattr *resource_filter;
3382 struct drbd_resource *resource = NULL, *next_resource;
3383 struct drbd_connection *uninitialized_var(connection);
3384 int err = 0, retcode;
3385 struct drbd_genlmsghdr *dh;
3386 struct connection_info connection_info;
3387 struct connection_statistics connection_statistics;
3388
3389 rcu_read_lock();
3390 resource = (struct drbd_resource *)cb->args[0];
3391 if (!cb->args[0]) {
3392 resource_filter = find_cfg_context_attr(cb->nlh, T_ctx_resource_name);
3393 if (resource_filter) {
3394 retcode = ERR_RES_NOT_KNOWN;
3395 resource = drbd_find_resource(nla_data(resource_filter));
3396 if (!resource)
3397 goto put_result;
3398 cb->args[0] = (long)resource;
3399 cb->args[1] = SINGLE_RESOURCE;
3400 }
3401 }
3402 if (!resource) {
3403 if (list_empty(&drbd_resources))
3404 goto out;
3405 resource = list_first_entry(&drbd_resources, struct drbd_resource, resources);
3406 kref_get(&resource->kref);
3407 cb->args[0] = (long)resource;
3408 cb->args[1] = ITERATE_RESOURCES;
3409 }
3410
3411 next_resource:
3412 rcu_read_unlock();
3413 mutex_lock(&resource->conf_update);
3414 rcu_read_lock();
3415 if (cb->args[2]) {
3416 for_each_connection_rcu(connection, resource)
3417 if (connection == (struct drbd_connection *)cb->args[2])
3418 goto found_connection;
3419 /* connection was probably deleted */
3420 goto no_more_connections;
3421 }
3422 connection = list_entry(&resource->connections, struct drbd_connection, connections);
3423
3424 found_connection:
3425 list_for_each_entry_continue_rcu(connection, &resource->connections, connections) {
3426 if (!has_net_conf(connection))
3427 continue;
3428 retcode = NO_ERROR;
3429 goto put_result; /* only one iteration */
3430 }
3431
3432 no_more_connections:
3433 if (cb->args[1] == ITERATE_RESOURCES) {
3434 for_each_resource_rcu(next_resource, &drbd_resources) {
3435 if (next_resource == resource)
3436 goto found_resource;
3437 }
3438 /* resource was probably deleted */
3439 }
3440 goto out;
3441
3442 found_resource:
3443 list_for_each_entry_continue_rcu(next_resource, &drbd_resources, resources) {
3444 mutex_unlock(&resource->conf_update);
3445 kref_put(&resource->kref, drbd_destroy_resource);
3446 resource = next_resource;
3447 kref_get(&resource->kref);
3448 cb->args[0] = (long)resource;
3449 cb->args[2] = 0;
3450 goto next_resource;
3451 }
3452 goto out; /* no more resources */
3453
3454 put_result:
3455 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
3456 cb->nlh->nlmsg_seq, &drbd_genl_family,
3457 NLM_F_MULTI, DRBD_ADM_GET_CONNECTIONS);
3458 err = -ENOMEM;
3459 if (!dh)
3460 goto out;
3461 dh->ret_code = retcode;
3462 dh->minor = -1U;
3463 if (retcode == NO_ERROR) {
3464 struct net_conf *net_conf;
3465
3466 err = nla_put_drbd_cfg_context(skb, resource, connection, NULL);
3467 if (err)
3468 goto out;
3469 net_conf = rcu_dereference(connection->net_conf);
3470 if (net_conf) {
3471 err = net_conf_to_skb(skb, net_conf, !capable(CAP_SYS_ADMIN));
3472 if (err)
3473 goto out;
3474 }
3475 connection_to_info(&connection_info, connection);
3476 err = connection_info_to_skb(skb, &connection_info, !capable(CAP_SYS_ADMIN));
3477 if (err)
3478 goto out;
3479 connection_statistics.conn_congested = test_bit(NET_CONGESTED, &connection->flags);
3480 err = connection_statistics_to_skb(skb, &connection_statistics, !capable(CAP_SYS_ADMIN));
3481 if (err)
3482 goto out;
3483 cb->args[2] = (long)connection;
3484 }
3485 genlmsg_end(skb, dh);
3486 err = 0;
3487
3488 out:
3489 rcu_read_unlock();
3490 if (resource)
3491 mutex_unlock(&resource->conf_update);
3492 if (err)
3493 return err;
3494 return skb->len;
3495 }
3496
3497 enum mdf_peer_flag {
3498 MDF_PEER_CONNECTED = 1 << 0,
3499 MDF_PEER_OUTDATED = 1 << 1,
3500 MDF_PEER_FENCING = 1 << 2,
3501 MDF_PEER_FULL_SYNC = 1 << 3,
3502 };
3503
3504 static void peer_device_to_statistics(struct peer_device_statistics *s,
3505 struct drbd_peer_device *peer_device)
3506 {
3507 struct drbd_device *device = peer_device->device;
3508
3509 memset(s, 0, sizeof(*s));
3510 s->peer_dev_received = device->recv_cnt;
3511 s->peer_dev_sent = device->send_cnt;
3512 s->peer_dev_pending = atomic_read(&device->ap_pending_cnt) +
3513 atomic_read(&device->rs_pending_cnt);
3514 s->peer_dev_unacked = atomic_read(&device->unacked_cnt);
3515 s->peer_dev_out_of_sync = drbd_bm_total_weight(device) << (BM_BLOCK_SHIFT - 9);
3516 s->peer_dev_resync_failed = device->rs_failed << (BM_BLOCK_SHIFT - 9);
3517 if (get_ldev(device)) {
3518 struct drbd_md *md = &device->ldev->md;
3519
3520 spin_lock_irq(&md->uuid_lock);
3521 s->peer_dev_bitmap_uuid = md->uuid[UI_BITMAP];
3522 spin_unlock_irq(&md->uuid_lock);
3523 s->peer_dev_flags =
3524 (drbd_md_test_flag(device->ldev, MDF_CONNECTED_IND) ?
3525 MDF_PEER_CONNECTED : 0) +
3526 (drbd_md_test_flag(device->ldev, MDF_CONSISTENT) &&
3527 !drbd_md_test_flag(device->ldev, MDF_WAS_UP_TO_DATE) ?
3528 MDF_PEER_OUTDATED : 0) +
3529 /* FIXME: MDF_PEER_FENCING? */
3530 (drbd_md_test_flag(device->ldev, MDF_FULL_SYNC) ?
3531 MDF_PEER_FULL_SYNC : 0);
3532 put_ldev(device);
3533 }
3534 }
3535
3536 int drbd_adm_dump_peer_devices_done(struct netlink_callback *cb)
3537 {
3538 return put_resource_in_arg0(cb, 9);
3539 }
3540
3541 int drbd_adm_dump_peer_devices(struct sk_buff *skb, struct netlink_callback *cb)
3542 {
3543 struct nlattr *resource_filter;
3544 struct drbd_resource *resource;
3545 struct drbd_device *uninitialized_var(device);
3546 struct drbd_peer_device *peer_device = NULL;
3547 int minor, err, retcode;
3548 struct drbd_genlmsghdr *dh;
3549 struct idr *idr_to_search;
3550
3551 resource = (struct drbd_resource *)cb->args[0];
3552 if (!cb->args[0] && !cb->args[1]) {
3553 resource_filter = find_cfg_context_attr(cb->nlh, T_ctx_resource_name);
3554 if (resource_filter) {
3555 retcode = ERR_RES_NOT_KNOWN;
3556 resource = drbd_find_resource(nla_data(resource_filter));
3557 if (!resource)
3558 goto put_result;
3559 }
3560 cb->args[0] = (long)resource;
3561 }
3562
3563 rcu_read_lock();
3564 minor = cb->args[1];
3565 idr_to_search = resource ? &resource->devices : &drbd_devices;
3566 device = idr_find(idr_to_search, minor);
3567 if (!device) {
3568 next_device:
3569 minor++;
3570 cb->args[2] = 0;
3571 device = idr_get_next(idr_to_search, &minor);
3572 if (!device) {
3573 err = 0;
3574 goto out;
3575 }
3576 }
3577 if (cb->args[2]) {
3578 for_each_peer_device(peer_device, device)
3579 if (peer_device == (struct drbd_peer_device *)cb->args[2])
3580 goto found_peer_device;
3581 /* peer device was probably deleted */
3582 goto next_device;
3583 }
3584 /* Make peer_device point to the list head (not the first entry). */
3585 peer_device = list_entry(&device->peer_devices, struct drbd_peer_device, peer_devices);
3586
3587 found_peer_device:
3588 list_for_each_entry_continue_rcu(peer_device, &device->peer_devices, peer_devices) {
3589 if (!has_net_conf(peer_device->connection))
3590 continue;
3591 retcode = NO_ERROR;
3592 goto put_result; /* only one iteration */
3593 }
3594 goto next_device;
3595
3596 put_result:
3597 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
3598 cb->nlh->nlmsg_seq, &drbd_genl_family,
3599 NLM_F_MULTI, DRBD_ADM_GET_PEER_DEVICES);
3600 err = -ENOMEM;
3601 if (!dh)
3602 goto out;
3603 dh->ret_code = retcode;
3604 dh->minor = -1U;
3605 if (retcode == NO_ERROR) {
3606 struct peer_device_info peer_device_info;
3607 struct peer_device_statistics peer_device_statistics;
3608
3609 dh->minor = minor;
3610 err = nla_put_drbd_cfg_context(skb, device->resource, peer_device->connection, device);
3611 if (err)
3612 goto out;
3613 peer_device_to_info(&peer_device_info, peer_device);
3614 err = peer_device_info_to_skb(skb, &peer_device_info, !capable(CAP_SYS_ADMIN));
3615 if (err)
3616 goto out;
3617 peer_device_to_statistics(&peer_device_statistics, peer_device);
3618 err = peer_device_statistics_to_skb(skb, &peer_device_statistics, !capable(CAP_SYS_ADMIN));
3619 if (err)
3620 goto out;
3621 cb->args[1] = minor;
3622 cb->args[2] = (long)peer_device;
3623 }
3624 genlmsg_end(skb, dh);
3625 err = 0;
3626
3627 out:
3628 rcu_read_unlock();
3629 if (err)
3630 return err;
3631 return skb->len;
3632 }
3633 /*
3634 * Return the connection of @resource if @resource has exactly one connection.
3635 */
3636 static struct drbd_connection *the_only_connection(struct drbd_resource *resource)
3637 {
3638 struct list_head *connections = &resource->connections;
3639
3640 if (list_empty(connections) || connections->next->next != connections)
3641 return NULL;
3642 return list_first_entry(&resource->connections, struct drbd_connection, connections);
3643 }
3644
3645 static int nla_put_status_info(struct sk_buff *skb, struct drbd_device *device,
3646 const struct sib_info *sib)
3647 {
3648 struct drbd_resource *resource = device->resource;
3649 struct state_info *si = NULL; /* for sizeof(si->member); */
3650 struct nlattr *nla;
3651 int got_ldev;
3652 int err = 0;
3653 int exclude_sensitive;
3654
3655 /* If sib != NULL, this is drbd_bcast_event, which anyone can listen
3656 * to. So we better exclude_sensitive information.
3657 *
3658 * If sib == NULL, this is drbd_adm_get_status, executed synchronously
3659 * in the context of the requesting user process. Exclude sensitive
3660 * information, unless current has superuser.
3661 *
3662 * NOTE: for drbd_adm_get_status_all(), this is a netlink dump, and
3663 * relies on the current implementation of netlink_dump(), which
3664 * executes the dump callback successively from netlink_recvmsg(),
3665 * always in the context of the receiving process */
3666 exclude_sensitive = sib || !capable(CAP_SYS_ADMIN);
3667
3668 got_ldev = get_ldev(device);
3669
3670 /* We need to add connection name and volume number information still.
3671 * Minor number is in drbd_genlmsghdr. */
3672 if (nla_put_drbd_cfg_context(skb, resource, the_only_connection(resource), device))
3673 goto nla_put_failure;
3674
3675 if (res_opts_to_skb(skb, &device->resource->res_opts, exclude_sensitive))
3676 goto nla_put_failure;
3677
3678 rcu_read_lock();
3679 if (got_ldev) {
3680 struct disk_conf *disk_conf;
3681
3682 disk_conf = rcu_dereference(device->ldev->disk_conf);
3683 err = disk_conf_to_skb(skb, disk_conf, exclude_sensitive);
3684 }
3685 if (!err) {
3686 struct net_conf *nc;
3687
3688 nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
3689 if (nc)
3690 err = net_conf_to_skb(skb, nc, exclude_sensitive);
3691 }
3692 rcu_read_unlock();
3693 if (err)
3694 goto nla_put_failure;
3695
3696 nla = nla_nest_start(skb, DRBD_NLA_STATE_INFO);
3697 if (!nla)
3698 goto nla_put_failure;
3699 if (nla_put_u32(skb, T_sib_reason, sib ? sib->sib_reason : SIB_GET_STATUS_REPLY) ||
3700 nla_put_u32(skb, T_current_state, device->state.i) ||
3701 nla_put_u64_0pad(skb, T_ed_uuid, device->ed_uuid) ||
3702 nla_put_u64_0pad(skb, T_capacity,
3703 drbd_get_capacity(device->this_bdev)) ||
3704 nla_put_u64_0pad(skb, T_send_cnt, device->send_cnt) ||
3705 nla_put_u64_0pad(skb, T_recv_cnt, device->recv_cnt) ||
3706 nla_put_u64_0pad(skb, T_read_cnt, device->read_cnt) ||
3707 nla_put_u64_0pad(skb, T_writ_cnt, device->writ_cnt) ||
3708 nla_put_u64_0pad(skb, T_al_writ_cnt, device->al_writ_cnt) ||
3709 nla_put_u64_0pad(skb, T_bm_writ_cnt, device->bm_writ_cnt) ||
3710 nla_put_u32(skb, T_ap_bio_cnt, atomic_read(&device->ap_bio_cnt)) ||
3711 nla_put_u32(skb, T_ap_pending_cnt, atomic_read(&device->ap_pending_cnt)) ||
3712 nla_put_u32(skb, T_rs_pending_cnt, atomic_read(&device->rs_pending_cnt)))
3713 goto nla_put_failure;
3714
3715 if (got_ldev) {
3716 int err;
3717
3718 spin_lock_irq(&device->ldev->md.uuid_lock);
3719 err = nla_put(skb, T_uuids, sizeof(si->uuids), device->ldev->md.uuid);
3720 spin_unlock_irq(&device->ldev->md.uuid_lock);
3721
3722 if (err)
3723 goto nla_put_failure;
3724
3725 if (nla_put_u32(skb, T_disk_flags, device->ldev->md.flags) ||
3726 nla_put_u64_0pad(skb, T_bits_total, drbd_bm_bits(device)) ||
3727 nla_put_u64_0pad(skb, T_bits_oos,
3728 drbd_bm_total_weight(device)))
3729 goto nla_put_failure;
3730 if (C_SYNC_SOURCE <= device->state.conn &&
3731 C_PAUSED_SYNC_T >= device->state.conn) {
3732 if (nla_put_u64_0pad(skb, T_bits_rs_total,
3733 device->rs_total) ||
3734 nla_put_u64_0pad(skb, T_bits_rs_failed,
3735 device->rs_failed))
3736 goto nla_put_failure;
3737 }
3738 }
3739
3740 if (sib) {
3741 switch(sib->sib_reason) {
3742 case SIB_SYNC_PROGRESS:
3743 case SIB_GET_STATUS_REPLY:
3744 break;
3745 case SIB_STATE_CHANGE:
3746 if (nla_put_u32(skb, T_prev_state, sib->os.i) ||
3747 nla_put_u32(skb, T_new_state, sib->ns.i))
3748 goto nla_put_failure;
3749 break;
3750 case SIB_HELPER_POST:
3751 if (nla_put_u32(skb, T_helper_exit_code,
3752 sib->helper_exit_code))
3753 goto nla_put_failure;
3754 /* fall through */
3755 case SIB_HELPER_PRE:
3756 if (nla_put_string(skb, T_helper, sib->helper_name))
3757 goto nla_put_failure;
3758 break;
3759 }
3760 }
3761 nla_nest_end(skb, nla);
3762
3763 if (0)
3764 nla_put_failure:
3765 err = -EMSGSIZE;
3766 if (got_ldev)
3767 put_ldev(device);
3768 return err;
3769 }
3770
3771 int drbd_adm_get_status(struct sk_buff *skb, struct genl_info *info)
3772 {
3773 struct drbd_config_context adm_ctx;
3774 enum drbd_ret_code retcode;
3775 int err;
3776
3777 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3778 if (!adm_ctx.reply_skb)
3779 return retcode;
3780 if (retcode != NO_ERROR)
3781 goto out;
3782
3783 err = nla_put_status_info(adm_ctx.reply_skb, adm_ctx.device, NULL);
3784 if (err) {
3785 nlmsg_free(adm_ctx.reply_skb);
3786 return err;
3787 }
3788 out:
3789 drbd_adm_finish(&adm_ctx, info, retcode);
3790 return 0;
3791 }
3792
3793 static int get_one_status(struct sk_buff *skb, struct netlink_callback *cb)
3794 {
3795 struct drbd_device *device;
3796 struct drbd_genlmsghdr *dh;
3797 struct drbd_resource *pos = (struct drbd_resource *)cb->args[0];
3798 struct drbd_resource *resource = NULL;
3799 struct drbd_resource *tmp;
3800 unsigned volume = cb->args[1];
3801
3802 /* Open coded, deferred, iteration:
3803 * for_each_resource_safe(resource, tmp, &drbd_resources) {
3804 * connection = "first connection of resource or undefined";
3805 * idr_for_each_entry(&resource->devices, device, i) {
3806 * ...
3807 * }
3808 * }
3809 * where resource is cb->args[0];
3810 * and i is cb->args[1];
3811 *
3812 * cb->args[2] indicates if we shall loop over all resources,
3813 * or just dump all volumes of a single resource.
3814 *
3815 * This may miss entries inserted after this dump started,
3816 * or entries deleted before they are reached.
3817 *
3818 * We need to make sure the device won't disappear while
3819 * we are looking at it, and revalidate our iterators
3820 * on each iteration.
3821 */
3822
3823 /* synchronize with conn_create()/drbd_destroy_connection() */
3824 rcu_read_lock();
3825 /* revalidate iterator position */
3826 for_each_resource_rcu(tmp, &drbd_resources) {
3827 if (pos == NULL) {
3828 /* first iteration */
3829 pos = tmp;
3830 resource = pos;
3831 break;
3832 }
3833 if (tmp == pos) {
3834 resource = pos;
3835 break;
3836 }
3837 }
3838 if (resource) {
3839 next_resource:
3840 device = idr_get_next(&resource->devices, &volume);
3841 if (!device) {
3842 /* No more volumes to dump on this resource.
3843 * Advance resource iterator. */
3844 pos = list_entry_rcu(resource->resources.next,
3845 struct drbd_resource, resources);
3846 /* Did we dump any volume of this resource yet? */
3847 if (volume != 0) {
3848 /* If we reached the end of the list,
3849 * or only a single resource dump was requested,
3850 * we are done. */
3851 if (&pos->resources == &drbd_resources || cb->args[2])
3852 goto out;
3853 volume = 0;
3854 resource = pos;
3855 goto next_resource;
3856 }
3857 }
3858
3859 dh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
3860 cb->nlh->nlmsg_seq, &drbd_genl_family,
3861 NLM_F_MULTI, DRBD_ADM_GET_STATUS);
3862 if (!dh)
3863 goto out;
3864
3865 if (!device) {
3866 /* This is a connection without a single volume.
3867 * Suprisingly enough, it may have a network
3868 * configuration. */
3869 struct drbd_connection *connection;
3870
3871 dh->minor = -1U;
3872 dh->ret_code = NO_ERROR;
3873 connection = the_only_connection(resource);
3874 if (nla_put_drbd_cfg_context(skb, resource, connection, NULL))
3875 goto cancel;
3876 if (connection) {
3877 struct net_conf *nc;
3878
3879 nc = rcu_dereference(connection->net_conf);
3880 if (nc && net_conf_to_skb(skb, nc, 1) != 0)
3881 goto cancel;
3882 }
3883 goto done;
3884 }
3885
3886 D_ASSERT(device, device->vnr == volume);
3887 D_ASSERT(device, device->resource == resource);
3888
3889 dh->minor = device_to_minor(device);
3890 dh->ret_code = NO_ERROR;
3891
3892 if (nla_put_status_info(skb, device, NULL)) {
3893 cancel:
3894 genlmsg_cancel(skb, dh);
3895 goto out;
3896 }
3897 done:
3898 genlmsg_end(skb, dh);
3899 }
3900
3901 out:
3902 rcu_read_unlock();
3903 /* where to start the next iteration */
3904 cb->args[0] = (long)pos;
3905 cb->args[1] = (pos == resource) ? volume + 1 : 0;
3906
3907 /* No more resources/volumes/minors found results in an empty skb.
3908 * Which will terminate the dump. */
3909 return skb->len;
3910 }
3911
3912 /*
3913 * Request status of all resources, or of all volumes within a single resource.
3914 *
3915 * This is a dump, as the answer may not fit in a single reply skb otherwise.
3916 * Which means we cannot use the family->attrbuf or other such members, because
3917 * dump is NOT protected by the genl_lock(). During dump, we only have access
3918 * to the incoming skb, and need to opencode "parsing" of the nlattr payload.
3919 *
3920 * Once things are setup properly, we call into get_one_status().
3921 */
3922 int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb)
3923 {
3924 const unsigned hdrlen = GENL_HDRLEN + GENL_MAGIC_FAMILY_HDRSZ;
3925 struct nlattr *nla;
3926 const char *resource_name;
3927 struct drbd_resource *resource;
3928 int maxtype;
3929
3930 /* Is this a followup call? */
3931 if (cb->args[0]) {
3932 /* ... of a single resource dump,
3933 * and the resource iterator has been advanced already? */
3934 if (cb->args[2] && cb->args[2] != cb->args[0])
3935 return 0; /* DONE. */
3936 goto dump;
3937 }
3938
3939 /* First call (from netlink_dump_start). We need to figure out
3940 * which resource(s) the user wants us to dump. */
3941 nla = nla_find(nlmsg_attrdata(cb->nlh, hdrlen),
3942 nlmsg_attrlen(cb->nlh, hdrlen),
3943 DRBD_NLA_CFG_CONTEXT);
3944
3945 /* No explicit context given. Dump all. */
3946 if (!nla)
3947 goto dump;
3948 maxtype = ARRAY_SIZE(drbd_cfg_context_nl_policy) - 1;
3949 nla = drbd_nla_find_nested(maxtype, nla, __nla_type(T_ctx_resource_name));
3950 if (IS_ERR(nla))
3951 return PTR_ERR(nla);
3952 /* context given, but no name present? */
3953 if (!nla)
3954 return -EINVAL;
3955 resource_name = nla_data(nla);
3956 if (!*resource_name)
3957 return -ENODEV;
3958 resource = drbd_find_resource(resource_name);
3959 if (!resource)
3960 return -ENODEV;
3961
3962 kref_put(&resource->kref, drbd_destroy_resource); /* get_one_status() revalidates the resource */
3963
3964 /* prime iterators, and set "filter" mode mark:
3965 * only dump this connection. */
3966 cb->args[0] = (long)resource;
3967 /* cb->args[1] = 0; passed in this way. */
3968 cb->args[2] = (long)resource;
3969
3970 dump:
3971 return get_one_status(skb, cb);
3972 }
3973
3974 int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info)
3975 {
3976 struct drbd_config_context adm_ctx;
3977 enum drbd_ret_code retcode;
3978 struct timeout_parms tp;
3979 int err;
3980
3981 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
3982 if (!adm_ctx.reply_skb)
3983 return retcode;
3984 if (retcode != NO_ERROR)
3985 goto out;
3986
3987 tp.timeout_type =
3988 adm_ctx.device->state.pdsk == D_OUTDATED ? UT_PEER_OUTDATED :
3989 test_bit(USE_DEGR_WFC_T, &adm_ctx.device->flags) ? UT_DEGRADED :
3990 UT_DEFAULT;
3991
3992 err = timeout_parms_to_priv_skb(adm_ctx.reply_skb, &tp);
3993 if (err) {
3994 nlmsg_free(adm_ctx.reply_skb);
3995 return err;
3996 }
3997 out:
3998 drbd_adm_finish(&adm_ctx, info, retcode);
3999 return 0;
4000 }
4001
4002 int drbd_adm_start_ov(struct sk_buff *skb, struct genl_info *info)
4003 {
4004 struct drbd_config_context adm_ctx;
4005 struct drbd_device *device;
4006 enum drbd_ret_code retcode;
4007 struct start_ov_parms parms;
4008
4009 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
4010 if (!adm_ctx.reply_skb)
4011 return retcode;
4012 if (retcode != NO_ERROR)
4013 goto out;
4014
4015 device = adm_ctx.device;
4016
4017 /* resume from last known position, if possible */
4018 parms.ov_start_sector = device->ov_start_sector;
4019 parms.ov_stop_sector = ULLONG_MAX;
4020 if (info->attrs[DRBD_NLA_START_OV_PARMS]) {
4021 int err = start_ov_parms_from_attrs(&parms, info);
4022 if (err) {
4023 retcode = ERR_MANDATORY_TAG;
4024 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
4025 goto out;
4026 }
4027 }
4028 mutex_lock(&adm_ctx.resource->adm_mutex);
4029
4030 /* w_make_ov_request expects position to be aligned */
4031 device->ov_start_sector = parms.ov_start_sector & ~(BM_SECT_PER_BIT-1);
4032 device->ov_stop_sector = parms.ov_stop_sector;
4033
4034 /* If there is still bitmap IO pending, e.g. previous resync or verify
4035 * just being finished, wait for it before requesting a new resync. */
4036 drbd_suspend_io(device);
4037 wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags));
4038 retcode = drbd_request_state(device, NS(conn, C_VERIFY_S));
4039 drbd_resume_io(device);
4040
4041 mutex_unlock(&adm_ctx.resource->adm_mutex);
4042 out:
4043 drbd_adm_finish(&adm_ctx, info, retcode);
4044 return 0;
4045 }
4046
4047
4048 int drbd_adm_new_c_uuid(struct sk_buff *skb, struct genl_info *info)
4049 {
4050 struct drbd_config_context adm_ctx;
4051 struct drbd_device *device;
4052 enum drbd_ret_code retcode;
4053 int skip_initial_sync = 0;
4054 int err;
4055 struct new_c_uuid_parms args;
4056
4057 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
4058 if (!adm_ctx.reply_skb)
4059 return retcode;
4060 if (retcode != NO_ERROR)
4061 goto out_nolock;
4062
4063 device = adm_ctx.device;
4064 memset(&args, 0, sizeof(args));
4065 if (info->attrs[DRBD_NLA_NEW_C_UUID_PARMS]) {
4066 err = new_c_uuid_parms_from_attrs(&args, info);
4067 if (err) {
4068 retcode = ERR_MANDATORY_TAG;
4069 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
4070 goto out_nolock;
4071 }
4072 }
4073
4074 mutex_lock(&adm_ctx.resource->adm_mutex);
4075 mutex_lock(device->state_mutex); /* Protects us against serialized state changes. */
4076
4077 if (!get_ldev(device)) {
4078 retcode = ERR_NO_DISK;
4079 goto out;
4080 }
4081
4082 /* this is "skip initial sync", assume to be clean */
4083 if (device->state.conn == C_CONNECTED &&
4084 first_peer_device(device)->connection->agreed_pro_version >= 90 &&
4085 device->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED && args.clear_bm) {
4086 drbd_info(device, "Preparing to skip initial sync\n");
4087 skip_initial_sync = 1;
4088 } else if (device->state.conn != C_STANDALONE) {
4089 retcode = ERR_CONNECTED;
4090 goto out_dec;
4091 }
4092
4093 drbd_uuid_set(device, UI_BITMAP, 0); /* Rotate UI_BITMAP to History 1, etc... */
4094 drbd_uuid_new_current(device); /* New current, previous to UI_BITMAP */
4095
4096 if (args.clear_bm) {
4097 err = drbd_bitmap_io(device, &drbd_bmio_clear_n_write,
4098 "clear_n_write from new_c_uuid", BM_LOCKED_MASK);
4099 if (err) {
4100 drbd_err(device, "Writing bitmap failed with %d\n", err);
4101 retcode = ERR_IO_MD_DISK;
4102 }
4103 if (skip_initial_sync) {
4104 drbd_send_uuids_skip_initial_sync(first_peer_device(device));
4105 _drbd_uuid_set(device, UI_BITMAP, 0);
4106 drbd_print_uuids(device, "cleared bitmap UUID");
4107 spin_lock_irq(&device->resource->req_lock);
4108 _drbd_set_state(_NS2(device, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
4109 CS_VERBOSE, NULL);
4110 spin_unlock_irq(&device->resource->req_lock);
4111 }
4112 }
4113
4114 drbd_md_sync(device);
4115 out_dec:
4116 put_ldev(device);
4117 out:
4118 mutex_unlock(device->state_mutex);
4119 mutex_unlock(&adm_ctx.resource->adm_mutex);
4120 out_nolock:
4121 drbd_adm_finish(&adm_ctx, info, retcode);
4122 return 0;
4123 }
4124
4125 static enum drbd_ret_code
4126 drbd_check_resource_name(struct drbd_config_context *adm_ctx)
4127 {
4128 const char *name = adm_ctx->resource_name;
4129 if (!name || !name[0]) {
4130 drbd_msg_put_info(adm_ctx->reply_skb, "resource name missing");
4131 return ERR_MANDATORY_TAG;
4132 }
4133 /* if we want to use these in sysfs/configfs/debugfs some day,
4134 * we must not allow slashes */
4135 if (strchr(name, '/')) {
4136 drbd_msg_put_info(adm_ctx->reply_skb, "invalid resource name");
4137 return ERR_INVALID_REQUEST;
4138 }
4139 return NO_ERROR;
4140 }
4141
4142 static void resource_to_info(struct resource_info *info,
4143 struct drbd_resource *resource)
4144 {
4145 info->res_role = conn_highest_role(first_connection(resource));
4146 info->res_susp = resource->susp;
4147 info->res_susp_nod = resource->susp_nod;
4148 info->res_susp_fen = resource->susp_fen;
4149 }
4150
4151 int drbd_adm_new_resource(struct sk_buff *skb, struct genl_info *info)
4152 {
4153 struct drbd_connection *connection;
4154 struct drbd_config_context adm_ctx;
4155 enum drbd_ret_code retcode;
4156 struct res_opts res_opts;
4157 int err;
4158
4159 retcode = drbd_adm_prepare(&adm_ctx, skb, info, 0);
4160 if (!adm_ctx.reply_skb)
4161 return retcode;
4162 if (retcode != NO_ERROR)
4163 goto out;
4164
4165 set_res_opts_defaults(&res_opts);
4166 err = res_opts_from_attrs(&res_opts, info);
4167 if (err && err != -ENOMSG) {
4168 retcode = ERR_MANDATORY_TAG;
4169 drbd_msg_put_info(adm_ctx.reply_skb, from_attrs_err_to_txt(err));
4170 goto out;
4171 }
4172
4173 retcode = drbd_check_resource_name(&adm_ctx);
4174 if (retcode != NO_ERROR)
4175 goto out;
4176
4177 if (adm_ctx.resource) {
4178 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL) {
4179 retcode = ERR_INVALID_REQUEST;
4180 drbd_msg_put_info(adm_ctx.reply_skb, "resource exists");
4181 }
4182 /* else: still NO_ERROR */
4183 goto out;
4184 }
4185
4186 /* not yet safe for genl_family.parallel_ops */
4187 mutex_lock(&resources_mutex);
4188 connection = conn_create(adm_ctx.resource_name, &res_opts);
4189 mutex_unlock(&resources_mutex);
4190
4191 if (connection) {
4192 struct resource_info resource_info;
4193
4194 mutex_lock(&notification_mutex);
4195 resource_to_info(&resource_info, connection->resource);
4196 notify_resource_state(NULL, 0, connection->resource,
4197 &resource_info, NOTIFY_CREATE);
4198 mutex_unlock(&notification_mutex);
4199 } else
4200 retcode = ERR_NOMEM;
4201
4202 out:
4203 drbd_adm_finish(&adm_ctx, info, retcode);
4204 return 0;
4205 }
4206
4207 static void device_to_info(struct device_info *info,
4208 struct drbd_device *device)
4209 {
4210 info->dev_disk_state = device->state.disk;
4211 }
4212
4213
4214 int drbd_adm_new_minor(struct sk_buff *skb, struct genl_info *info)
4215 {
4216 struct drbd_config_context adm_ctx;
4217 struct drbd_genlmsghdr *dh = info->userhdr;
4218 enum drbd_ret_code retcode;
4219
4220 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
4221 if (!adm_ctx.reply_skb)
4222 return retcode;
4223 if (retcode != NO_ERROR)
4224 goto out;
4225
4226 if (dh->minor > MINORMASK) {
4227 drbd_msg_put_info(adm_ctx.reply_skb, "requested minor out of range");
4228 retcode = ERR_INVALID_REQUEST;
4229 goto out;
4230 }
4231 if (adm_ctx.volume > DRBD_VOLUME_MAX) {
4232 drbd_msg_put_info(adm_ctx.reply_skb, "requested volume id out of range");
4233 retcode = ERR_INVALID_REQUEST;
4234 goto out;
4235 }
4236
4237 /* drbd_adm_prepare made sure already
4238 * that first_peer_device(device)->connection and device->vnr match the request. */
4239 if (adm_ctx.device) {
4240 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
4241 retcode = ERR_MINOR_OR_VOLUME_EXISTS;
4242 /* else: still NO_ERROR */
4243 goto out;
4244 }
4245
4246 mutex_lock(&adm_ctx.resource->adm_mutex);
4247 retcode = drbd_create_device(&adm_ctx, dh->minor);
4248 if (retcode == NO_ERROR) {
4249 struct drbd_device *device;
4250 struct drbd_peer_device *peer_device;
4251 struct device_info info;
4252 unsigned int peer_devices = 0;
4253 enum drbd_notification_type flags;
4254
4255 device = minor_to_device(dh->minor);
4256 for_each_peer_device(peer_device, device) {
4257 if (!has_net_conf(peer_device->connection))
4258 continue;
4259 peer_devices++;
4260 }
4261
4262 device_to_info(&info, device);
4263 mutex_lock(&notification_mutex);
4264 flags = (peer_devices--) ? NOTIFY_CONTINUES : 0;
4265 notify_device_state(NULL, 0, device, &info, NOTIFY_CREATE | flags);
4266 for_each_peer_device(peer_device, device) {
4267 struct peer_device_info peer_device_info;
4268
4269 if (!has_net_conf(peer_device->connection))
4270 continue;
4271 peer_device_to_info(&peer_device_info, peer_device);
4272 flags = (peer_devices--) ? NOTIFY_CONTINUES : 0;
4273 notify_peer_device_state(NULL, 0, peer_device, &peer_device_info,
4274 NOTIFY_CREATE | flags);
4275 }
4276 mutex_unlock(&notification_mutex);
4277 }
4278 mutex_unlock(&adm_ctx.resource->adm_mutex);
4279 out:
4280 drbd_adm_finish(&adm_ctx, info, retcode);
4281 return 0;
4282 }
4283
4284 static enum drbd_ret_code adm_del_minor(struct drbd_device *device)
4285 {
4286 struct drbd_peer_device *peer_device;
4287
4288 if (device->state.disk == D_DISKLESS &&
4289 /* no need to be device->state.conn == C_STANDALONE &&
4290 * we may want to delete a minor from a live replication group.
4291 */
4292 device->state.role == R_SECONDARY) {
4293 struct drbd_connection *connection =
4294 first_connection(device->resource);
4295
4296 _drbd_request_state(device, NS(conn, C_WF_REPORT_PARAMS),
4297 CS_VERBOSE + CS_WAIT_COMPLETE);
4298
4299 /* If the state engine hasn't stopped the sender thread yet, we
4300 * need to flush the sender work queue before generating the
4301 * DESTROY events here. */
4302 if (get_t_state(&connection->worker) == RUNNING)
4303 drbd_flush_workqueue(&connection->sender_work);
4304
4305 mutex_lock(&notification_mutex);
4306 for_each_peer_device(peer_device, device) {
4307 if (!has_net_conf(peer_device->connection))
4308 continue;
4309 notify_peer_device_state(NULL, 0, peer_device, NULL,
4310 NOTIFY_DESTROY | NOTIFY_CONTINUES);
4311 }
4312 notify_device_state(NULL, 0, device, NULL, NOTIFY_DESTROY);
4313 mutex_unlock(&notification_mutex);
4314
4315 drbd_delete_device(device);
4316 return NO_ERROR;
4317 } else
4318 return ERR_MINOR_CONFIGURED;
4319 }
4320
4321 int drbd_adm_del_minor(struct sk_buff *skb, struct genl_info *info)
4322 {
4323 struct drbd_config_context adm_ctx;
4324 enum drbd_ret_code retcode;
4325
4326 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_MINOR);
4327 if (!adm_ctx.reply_skb)
4328 return retcode;
4329 if (retcode != NO_ERROR)
4330 goto out;
4331
4332 mutex_lock(&adm_ctx.resource->adm_mutex);
4333 retcode = adm_del_minor(adm_ctx.device);
4334 mutex_unlock(&adm_ctx.resource->adm_mutex);
4335 out:
4336 drbd_adm_finish(&adm_ctx, info, retcode);
4337 return 0;
4338 }
4339
4340 static int adm_del_resource(struct drbd_resource *resource)
4341 {
4342 struct drbd_connection *connection;
4343
4344 for_each_connection(connection, resource) {
4345 if (connection->cstate > C_STANDALONE)
4346 return ERR_NET_CONFIGURED;
4347 }
4348 if (!idr_is_empty(&resource->devices))
4349 return ERR_RES_IN_USE;
4350
4351 /* The state engine has stopped the sender thread, so we don't
4352 * need to flush the sender work queue before generating the
4353 * DESTROY event here. */
4354 mutex_lock(&notification_mutex);
4355 notify_resource_state(NULL, 0, resource, NULL, NOTIFY_DESTROY);
4356 mutex_unlock(&notification_mutex);
4357
4358 mutex_lock(&resources_mutex);
4359 list_del_rcu(&resource->resources);
4360 mutex_unlock(&resources_mutex);
4361 /* Make sure all threads have actually stopped: state handling only
4362 * does drbd_thread_stop_nowait(). */
4363 list_for_each_entry(connection, &resource->connections, connections)
4364 drbd_thread_stop(&connection->worker);
4365 synchronize_rcu();
4366 drbd_free_resource(resource);
4367 return NO_ERROR;
4368 }
4369
4370 int drbd_adm_down(struct sk_buff *skb, struct genl_info *info)
4371 {
4372 struct drbd_config_context adm_ctx;
4373 struct drbd_resource *resource;
4374 struct drbd_connection *connection;
4375 struct drbd_device *device;
4376 int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
4377 unsigned i;
4378
4379 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
4380 if (!adm_ctx.reply_skb)
4381 return retcode;
4382 if (retcode != NO_ERROR)
4383 goto finish;
4384
4385 resource = adm_ctx.resource;
4386 mutex_lock(&resource->adm_mutex);
4387 /* demote */
4388 for_each_connection(connection, resource) {
4389 struct drbd_peer_device *peer_device;
4390
4391 idr_for_each_entry(&connection->peer_devices, peer_device, i) {
4392 retcode = drbd_set_role(peer_device->device, R_SECONDARY, 0);
4393 if (retcode < SS_SUCCESS) {
4394 drbd_msg_put_info(adm_ctx.reply_skb, "failed to demote");
4395 goto out;
4396 }
4397 }
4398
4399 retcode = conn_try_disconnect(connection, 0);
4400 if (retcode < SS_SUCCESS) {
4401 drbd_msg_put_info(adm_ctx.reply_skb, "failed to disconnect");
4402 goto out;
4403 }
4404 }
4405
4406 /* detach */
4407 idr_for_each_entry(&resource->devices, device, i) {
4408 retcode = adm_detach(device, 0);
4409 if (retcode < SS_SUCCESS || retcode > NO_ERROR) {
4410 drbd_msg_put_info(adm_ctx.reply_skb, "failed to detach");
4411 goto out;
4412 }
4413 }
4414
4415 /* delete volumes */
4416 idr_for_each_entry(&resource->devices, device, i) {
4417 retcode = adm_del_minor(device);
4418 if (retcode != NO_ERROR) {
4419 /* "can not happen" */
4420 drbd_msg_put_info(adm_ctx.reply_skb, "failed to delete volume");
4421 goto out;
4422 }
4423 }
4424
4425 retcode = adm_del_resource(resource);
4426 out:
4427 mutex_unlock(&resource->adm_mutex);
4428 finish:
4429 drbd_adm_finish(&adm_ctx, info, retcode);
4430 return 0;
4431 }
4432
4433 int drbd_adm_del_resource(struct sk_buff *skb, struct genl_info *info)
4434 {
4435 struct drbd_config_context adm_ctx;
4436 struct drbd_resource *resource;
4437 enum drbd_ret_code retcode;
4438
4439 retcode = drbd_adm_prepare(&adm_ctx, skb, info, DRBD_ADM_NEED_RESOURCE);
4440 if (!adm_ctx.reply_skb)
4441 return retcode;
4442 if (retcode != NO_ERROR)
4443 goto finish;
4444 resource = adm_ctx.resource;
4445
4446 mutex_lock(&resource->adm_mutex);
4447 retcode = adm_del_resource(resource);
4448 mutex_unlock(&resource->adm_mutex);
4449 finish:
4450 drbd_adm_finish(&adm_ctx, info, retcode);
4451 return 0;
4452 }
4453
4454 void drbd_bcast_event(struct drbd_device *device, const struct sib_info *sib)
4455 {
4456 struct sk_buff *msg;
4457 struct drbd_genlmsghdr *d_out;
4458 unsigned seq;
4459 int err = -ENOMEM;
4460
4461 seq = atomic_inc_return(&drbd_genl_seq);
4462 msg = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
4463 if (!msg)
4464 goto failed;
4465
4466 err = -EMSGSIZE;
4467 d_out = genlmsg_put(msg, 0, seq, &drbd_genl_family, 0, DRBD_EVENT);
4468 if (!d_out) /* cannot happen, but anyways. */
4469 goto nla_put_failure;
4470 d_out->minor = device_to_minor(device);
4471 d_out->ret_code = NO_ERROR;
4472
4473 if (nla_put_status_info(msg, device, sib))
4474 goto nla_put_failure;
4475 genlmsg_end(msg, d_out);
4476 err = drbd_genl_multicast_events(msg, GFP_NOWAIT);
4477 /* msg has been consumed or freed in netlink_broadcast() */
4478 if (err && err != -ESRCH)
4479 goto failed;
4480
4481 return;
4482
4483 nla_put_failure:
4484 nlmsg_free(msg);
4485 failed:
4486 drbd_err(device, "Error %d while broadcasting event. "
4487 "Event seq:%u sib_reason:%u\n",
4488 err, seq, sib->sib_reason);
4489 }
4490
4491 static int nla_put_notification_header(struct sk_buff *msg,
4492 enum drbd_notification_type type)
4493 {
4494 struct drbd_notification_header nh = {
4495 .nh_type = type,
4496 };
4497
4498 return drbd_notification_header_to_skb(msg, &nh, true);
4499 }
4500
4501 void notify_resource_state(struct sk_buff *skb,
4502 unsigned int seq,
4503 struct drbd_resource *resource,
4504 struct resource_info *resource_info,
4505 enum drbd_notification_type type)
4506 {
4507 struct resource_statistics resource_statistics;
4508 struct drbd_genlmsghdr *dh;
4509 bool multicast = false;
4510 int err;
4511
4512 if (!skb) {
4513 seq = atomic_inc_return(&notify_genl_seq);
4514 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
4515 err = -ENOMEM;
4516 if (!skb)
4517 goto failed;
4518 multicast = true;
4519 }
4520
4521 err = -EMSGSIZE;
4522 dh = genlmsg_put(skb, 0, seq, &drbd_genl_family, 0, DRBD_RESOURCE_STATE);
4523 if (!dh)
4524 goto nla_put_failure;
4525 dh->minor = -1U;
4526 dh->ret_code = NO_ERROR;
4527 if (nla_put_drbd_cfg_context(skb, resource, NULL, NULL) ||
4528 nla_put_notification_header(skb, type) ||
4529 ((type & ~NOTIFY_FLAGS) != NOTIFY_DESTROY &&
4530 resource_info_to_skb(skb, resource_info, true)))
4531 goto nla_put_failure;
4532 resource_statistics.res_stat_write_ordering = resource->write_ordering;
4533 err = resource_statistics_to_skb(skb, &resource_statistics, !capable(CAP_SYS_ADMIN));
4534 if (err)
4535 goto nla_put_failure;
4536 genlmsg_end(skb, dh);
4537 if (multicast) {
4538 err = drbd_genl_multicast_events(skb, GFP_NOWAIT);
4539 /* skb has been consumed or freed in netlink_broadcast() */
4540 if (err && err != -ESRCH)
4541 goto failed;
4542 }
4543 return;
4544
4545 nla_put_failure:
4546 nlmsg_free(skb);
4547 failed:
4548 drbd_err(resource, "Error %d while broadcasting event. Event seq:%u\n",
4549 err, seq);
4550 }
4551
4552 void notify_device_state(struct sk_buff *skb,
4553 unsigned int seq,
4554 struct drbd_device *device,
4555 struct device_info *device_info,
4556 enum drbd_notification_type type)
4557 {
4558 struct device_statistics device_statistics;
4559 struct drbd_genlmsghdr *dh;
4560 bool multicast = false;
4561 int err;
4562
4563 if (!skb) {
4564 seq = atomic_inc_return(&notify_genl_seq);
4565 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
4566 err = -ENOMEM;
4567 if (!skb)
4568 goto failed;
4569 multicast = true;
4570 }
4571
4572 err = -EMSGSIZE;
4573 dh = genlmsg_put(skb, 0, seq, &drbd_genl_family, 0, DRBD_DEVICE_STATE);
4574 if (!dh)
4575 goto nla_put_failure;
4576 dh->minor = device->minor;
4577 dh->ret_code = NO_ERROR;
4578 if (nla_put_drbd_cfg_context(skb, device->resource, NULL, device) ||
4579 nla_put_notification_header(skb, type) ||
4580 ((type & ~NOTIFY_FLAGS) != NOTIFY_DESTROY &&
4581 device_info_to_skb(skb, device_info, true)))
4582 goto nla_put_failure;
4583 device_to_statistics(&device_statistics, device);
4584 device_statistics_to_skb(skb, &device_statistics, !capable(CAP_SYS_ADMIN));
4585 genlmsg_end(skb, dh);
4586 if (multicast) {
4587 err = drbd_genl_multicast_events(skb, GFP_NOWAIT);
4588 /* skb has been consumed or freed in netlink_broadcast() */
4589 if (err && err != -ESRCH)
4590 goto failed;
4591 }
4592 return;
4593
4594 nla_put_failure:
4595 nlmsg_free(skb);
4596 failed:
4597 drbd_err(device, "Error %d while broadcasting event. Event seq:%u\n",
4598 err, seq);
4599 }
4600
4601 void notify_connection_state(struct sk_buff *skb,
4602 unsigned int seq,
4603 struct drbd_connection *connection,
4604 struct connection_info *connection_info,
4605 enum drbd_notification_type type)
4606 {
4607 struct connection_statistics connection_statistics;
4608 struct drbd_genlmsghdr *dh;
4609 bool multicast = false;
4610 int err;
4611
4612 if (!skb) {
4613 seq = atomic_inc_return(&notify_genl_seq);
4614 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
4615 err = -ENOMEM;
4616 if (!skb)
4617 goto failed;
4618 multicast = true;
4619 }
4620
4621 err = -EMSGSIZE;
4622 dh = genlmsg_put(skb, 0, seq, &drbd_genl_family, 0, DRBD_CONNECTION_STATE);
4623 if (!dh)
4624 goto nla_put_failure;
4625 dh->minor = -1U;
4626 dh->ret_code = NO_ERROR;
4627 if (nla_put_drbd_cfg_context(skb, connection->resource, connection, NULL) ||
4628 nla_put_notification_header(skb, type) ||
4629 ((type & ~NOTIFY_FLAGS) != NOTIFY_DESTROY &&
4630 connection_info_to_skb(skb, connection_info, true)))
4631 goto nla_put_failure;
4632 connection_statistics.conn_congested = test_bit(NET_CONGESTED, &connection->flags);
4633 connection_statistics_to_skb(skb, &connection_statistics, !capable(CAP_SYS_ADMIN));
4634 genlmsg_end(skb, dh);
4635 if (multicast) {
4636 err = drbd_genl_multicast_events(skb, GFP_NOWAIT);
4637 /* skb has been consumed or freed in netlink_broadcast() */
4638 if (err && err != -ESRCH)
4639 goto failed;
4640 }
4641 return;
4642
4643 nla_put_failure:
4644 nlmsg_free(skb);
4645 failed:
4646 drbd_err(connection, "Error %d while broadcasting event. Event seq:%u\n",
4647 err, seq);
4648 }
4649
4650 void notify_peer_device_state(struct sk_buff *skb,
4651 unsigned int seq,
4652 struct drbd_peer_device *peer_device,
4653 struct peer_device_info *peer_device_info,
4654 enum drbd_notification_type type)
4655 {
4656 struct peer_device_statistics peer_device_statistics;
4657 struct drbd_resource *resource = peer_device->device->resource;
4658 struct drbd_genlmsghdr *dh;
4659 bool multicast = false;
4660 int err;
4661
4662 if (!skb) {
4663 seq = atomic_inc_return(&notify_genl_seq);
4664 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
4665 err = -ENOMEM;
4666 if (!skb)
4667 goto failed;
4668 multicast = true;
4669 }
4670
4671 err = -EMSGSIZE;
4672 dh = genlmsg_put(skb, 0, seq, &drbd_genl_family, 0, DRBD_PEER_DEVICE_STATE);
4673 if (!dh)
4674 goto nla_put_failure;
4675 dh->minor = -1U;
4676 dh->ret_code = NO_ERROR;
4677 if (nla_put_drbd_cfg_context(skb, resource, peer_device->connection, peer_device->device) ||
4678 nla_put_notification_header(skb, type) ||
4679 ((type & ~NOTIFY_FLAGS) != NOTIFY_DESTROY &&
4680 peer_device_info_to_skb(skb, peer_device_info, true)))
4681 goto nla_put_failure;
4682 peer_device_to_statistics(&peer_device_statistics, peer_device);
4683 peer_device_statistics_to_skb(skb, &peer_device_statistics, !capable(CAP_SYS_ADMIN));
4684 genlmsg_end(skb, dh);
4685 if (multicast) {
4686 err = drbd_genl_multicast_events(skb, GFP_NOWAIT);
4687 /* skb has been consumed or freed in netlink_broadcast() */
4688 if (err && err != -ESRCH)
4689 goto failed;
4690 }
4691 return;
4692
4693 nla_put_failure:
4694 nlmsg_free(skb);
4695 failed:
4696 drbd_err(peer_device, "Error %d while broadcasting event. Event seq:%u\n",
4697 err, seq);
4698 }
4699
4700 void notify_helper(enum drbd_notification_type type,
4701 struct drbd_device *device, struct drbd_connection *connection,
4702 const char *name, int status)
4703 {
4704 struct drbd_resource *resource = device ? device->resource : connection->resource;
4705 struct drbd_helper_info helper_info;
4706 unsigned int seq = atomic_inc_return(&notify_genl_seq);
4707 struct sk_buff *skb = NULL;
4708 struct drbd_genlmsghdr *dh;
4709 int err;
4710
4711 strlcpy(helper_info.helper_name, name, sizeof(helper_info.helper_name));
4712 helper_info.helper_name_len = min(strlen(name), sizeof(helper_info.helper_name));
4713 helper_info.helper_status = status;
4714
4715 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_NOIO);
4716 err = -ENOMEM;
4717 if (!skb)
4718 goto fail;
4719
4720 err = -EMSGSIZE;
4721 dh = genlmsg_put(skb, 0, seq, &drbd_genl_family, 0, DRBD_HELPER);
4722 if (!dh)
4723 goto fail;
4724 dh->minor = device ? device->minor : -1;
4725 dh->ret_code = NO_ERROR;
4726 mutex_lock(&notification_mutex);
4727 if (nla_put_drbd_cfg_context(skb, resource, connection, device) ||
4728 nla_put_notification_header(skb, type) ||
4729 drbd_helper_info_to_skb(skb, &helper_info, true))
4730 goto unlock_fail;
4731 genlmsg_end(skb, dh);
4732 err = drbd_genl_multicast_events(skb, GFP_NOWAIT);
4733 skb = NULL;
4734 /* skb has been consumed or freed in netlink_broadcast() */
4735 if (err && err != -ESRCH)
4736 goto unlock_fail;
4737 mutex_unlock(&notification_mutex);
4738 return;
4739
4740 unlock_fail:
4741 mutex_unlock(&notification_mutex);
4742 fail:
4743 nlmsg_free(skb);
4744 drbd_err(resource, "Error %d while broadcasting event. Event seq:%u\n",
4745 err, seq);
4746 }
4747
4748 static void notify_initial_state_done(struct sk_buff *skb, unsigned int seq)
4749 {
4750 struct drbd_genlmsghdr *dh;
4751 int err;
4752
4753 err = -EMSGSIZE;
4754 dh = genlmsg_put(skb, 0, seq, &drbd_genl_family, 0, DRBD_INITIAL_STATE_DONE);
4755 if (!dh)
4756 goto nla_put_failure;
4757 dh->minor = -1U;
4758 dh->ret_code = NO_ERROR;
4759 if (nla_put_notification_header(skb, NOTIFY_EXISTS))
4760 goto nla_put_failure;
4761 genlmsg_end(skb, dh);
4762 return;
4763
4764 nla_put_failure:
4765 nlmsg_free(skb);
4766 pr_err("Error %d sending event. Event seq:%u\n", err, seq);
4767 }
4768
4769 static void free_state_changes(struct list_head *list)
4770 {
4771 while (!list_empty(list)) {
4772 struct drbd_state_change *state_change =
4773 list_first_entry(list, struct drbd_state_change, list);
4774 list_del(&state_change->list);
4775 forget_state_change(state_change);
4776 }
4777 }
4778
4779 static unsigned int notifications_for_state_change(struct drbd_state_change *state_change)
4780 {
4781 return 1 +
4782 state_change->n_connections +
4783 state_change->n_devices +
4784 state_change->n_devices * state_change->n_connections;
4785 }
4786
4787 static int get_initial_state(struct sk_buff *skb, struct netlink_callback *cb)
4788 {
4789 struct drbd_state_change *state_change = (struct drbd_state_change *)cb->args[0];
4790 unsigned int seq = cb->args[2];
4791 unsigned int n;
4792 enum drbd_notification_type flags = 0;
4793
4794 /* There is no need for taking notification_mutex here: it doesn't
4795 matter if the initial state events mix with later state chage
4796 events; we can always tell the events apart by the NOTIFY_EXISTS
4797 flag. */
4798
4799 cb->args[5]--;
4800 if (cb->args[5] == 1) {
4801 notify_initial_state_done(skb, seq);
4802 goto out;
4803 }
4804 n = cb->args[4]++;
4805 if (cb->args[4] < cb->args[3])
4806 flags |= NOTIFY_CONTINUES;
4807 if (n < 1) {
4808 notify_resource_state_change(skb, seq, state_change->resource,
4809 NOTIFY_EXISTS | flags);
4810 goto next;
4811 }
4812 n--;
4813 if (n < state_change->n_connections) {
4814 notify_connection_state_change(skb, seq, &state_change->connections[n],
4815 NOTIFY_EXISTS | flags);
4816 goto next;
4817 }
4818 n -= state_change->n_connections;
4819 if (n < state_change->n_devices) {
4820 notify_device_state_change(skb, seq, &state_change->devices[n],
4821 NOTIFY_EXISTS | flags);
4822 goto next;
4823 }
4824 n -= state_change->n_devices;
4825 if (n < state_change->n_devices * state_change->n_connections) {
4826 notify_peer_device_state_change(skb, seq, &state_change->peer_devices[n],
4827 NOTIFY_EXISTS | flags);
4828 goto next;
4829 }
4830
4831 next:
4832 if (cb->args[4] == cb->args[3]) {
4833 struct drbd_state_change *next_state_change =
4834 list_entry(state_change->list.next,
4835 struct drbd_state_change, list);
4836 cb->args[0] = (long)next_state_change;
4837 cb->args[3] = notifications_for_state_change(next_state_change);
4838 cb->args[4] = 0;
4839 }
4840 out:
4841 return skb->len;
4842 }
4843
4844 int drbd_adm_get_initial_state(struct sk_buff *skb, struct netlink_callback *cb)
4845 {
4846 struct drbd_resource *resource;
4847 LIST_HEAD(head);
4848
4849 if (cb->args[5] >= 1) {
4850 if (cb->args[5] > 1)
4851 return get_initial_state(skb, cb);
4852 if (cb->args[0]) {
4853 struct drbd_state_change *state_change =
4854 (struct drbd_state_change *)cb->args[0];
4855
4856 /* connect list to head */
4857 list_add(&head, &state_change->list);
4858 free_state_changes(&head);
4859 }
4860 return 0;
4861 }
4862
4863 cb->args[5] = 2; /* number of iterations */
4864 mutex_lock(&resources_mutex);
4865 for_each_resource(resource, &drbd_resources) {
4866 struct drbd_state_change *state_change;
4867
4868 state_change = remember_old_state(resource, GFP_KERNEL);
4869 if (!state_change) {
4870 if (!list_empty(&head))
4871 free_state_changes(&head);
4872 mutex_unlock(&resources_mutex);
4873 return -ENOMEM;
4874 }
4875 copy_old_to_new_state_change(state_change);
4876 list_add_tail(&state_change->list, &head);
4877 cb->args[5] += notifications_for_state_change(state_change);
4878 }
4879 mutex_unlock(&resources_mutex);
4880
4881 if (!list_empty(&head)) {
4882 struct drbd_state_change *state_change =
4883 list_entry(head.next, struct drbd_state_change, list);
4884 cb->args[0] = (long)state_change;
4885 cb->args[3] = notifications_for_state_change(state_change);
4886 list_del(&head); /* detach list from head */
4887 }
4888
4889 cb->args[2] = cb->nlh->nlmsg_seq;
4890 return get_initial_state(skb, cb);
4891 }
This page took 0.19537 seconds and 5 git commands to generate.