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