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