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