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