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