Merge branch 'for-2.6.37/drivers' into for-linus
[~shefty/rdma-dev.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/connector.h>
33 #include <linux/blkpg.h>
34 #include <linux/cpumask.h>
35 #include "drbd_int.h"
36 #include "drbd_req.h"
37 #include "drbd_wrappers.h"
38 #include <asm/unaligned.h>
39 #include <linux/drbd_tag_magic.h>
40 #include <linux/drbd_limits.h>
41 #include <linux/compiler.h>
42 #include <linux/kthread.h>
43
44 static unsigned short *tl_add_blob(unsigned short *, enum drbd_tags, const void *, int);
45 static unsigned short *tl_add_str(unsigned short *, enum drbd_tags, const char *);
46 static unsigned short *tl_add_int(unsigned short *, enum drbd_tags, const void *);
47
48 /* see get_sb_bdev and bd_claim */
49 static char *drbd_m_holder = "Hands off! this is DRBD's meta data device.";
50
51 /* Generate the tag_list to struct functions */
52 #define NL_PACKET(name, number, fields) \
53 static int name ## _from_tags(struct drbd_conf *mdev, \
54         unsigned short *tags, struct name *arg) __attribute__ ((unused)); \
55 static int name ## _from_tags(struct drbd_conf *mdev, \
56         unsigned short *tags, struct name *arg) \
57 { \
58         int tag; \
59         int dlen; \
60         \
61         while ((tag = get_unaligned(tags++)) != TT_END) {       \
62                 dlen = get_unaligned(tags++);                   \
63                 switch (tag_number(tag)) { \
64                 fields \
65                 default: \
66                         if (tag & T_MANDATORY) { \
67                                 dev_err(DEV, "Unknown tag: %d\n", tag_number(tag)); \
68                                 return 0; \
69                         } \
70                 } \
71                 tags = (unsigned short *)((char *)tags + dlen); \
72         } \
73         return 1; \
74 }
75 #define NL_INTEGER(pn, pr, member) \
76         case pn: /* D_ASSERT( tag_type(tag) == TT_INTEGER ); */ \
77                 arg->member = get_unaligned((int *)(tags));     \
78                 break;
79 #define NL_INT64(pn, pr, member) \
80         case pn: /* D_ASSERT( tag_type(tag) == TT_INT64 ); */ \
81                 arg->member = get_unaligned((u64 *)(tags));     \
82                 break;
83 #define NL_BIT(pn, pr, member) \
84         case pn: /* D_ASSERT( tag_type(tag) == TT_BIT ); */ \
85                 arg->member = *(char *)(tags) ? 1 : 0; \
86                 break;
87 #define NL_STRING(pn, pr, member, len) \
88         case pn: /* D_ASSERT( tag_type(tag) == TT_STRING ); */ \
89                 if (dlen > len) { \
90                         dev_err(DEV, "arg too long: %s (%u wanted, max len: %u bytes)\n", \
91                                 #member, dlen, (unsigned int)len); \
92                         return 0; \
93                 } \
94                  arg->member ## _len = dlen; \
95                  memcpy(arg->member, tags, min_t(size_t, dlen, len)); \
96                  break;
97 #include "linux/drbd_nl.h"
98
99 /* Generate the struct to tag_list functions */
100 #define NL_PACKET(name, number, fields) \
101 static unsigned short* \
102 name ## _to_tags(struct drbd_conf *mdev, \
103         struct name *arg, unsigned short *tags) __attribute__ ((unused)); \
104 static unsigned short* \
105 name ## _to_tags(struct drbd_conf *mdev, \
106         struct name *arg, unsigned short *tags) \
107 { \
108         fields \
109         return tags; \
110 }
111
112 #define NL_INTEGER(pn, pr, member) \
113         put_unaligned(pn | pr | TT_INTEGER, tags++);    \
114         put_unaligned(sizeof(int), tags++);             \
115         put_unaligned(arg->member, (int *)tags);        \
116         tags = (unsigned short *)((char *)tags+sizeof(int));
117 #define NL_INT64(pn, pr, member) \
118         put_unaligned(pn | pr | TT_INT64, tags++);      \
119         put_unaligned(sizeof(u64), tags++);             \
120         put_unaligned(arg->member, (u64 *)tags);        \
121         tags = (unsigned short *)((char *)tags+sizeof(u64));
122 #define NL_BIT(pn, pr, member) \
123         put_unaligned(pn | pr | TT_BIT, tags++);        \
124         put_unaligned(sizeof(char), tags++);            \
125         *(char *)tags = arg->member; \
126         tags = (unsigned short *)((char *)tags+sizeof(char));
127 #define NL_STRING(pn, pr, member, len) \
128         put_unaligned(pn | pr | TT_STRING, tags++);     \
129         put_unaligned(arg->member ## _len, tags++);     \
130         memcpy(tags, arg->member, arg->member ## _len); \
131         tags = (unsigned short *)((char *)tags + arg->member ## _len);
132 #include "linux/drbd_nl.h"
133
134 void drbd_bcast_ev_helper(struct drbd_conf *mdev, char *helper_name);
135 void drbd_nl_send_reply(struct cn_msg *, int);
136
137 int drbd_khelper(struct drbd_conf *mdev, char *cmd)
138 {
139         char *envp[] = { "HOME=/",
140                         "TERM=linux",
141                         "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
142                         NULL, /* Will be set to address family */
143                         NULL, /* Will be set to address */
144                         NULL };
145
146         char mb[12], af[20], ad[60], *afs;
147         char *argv[] = {usermode_helper, cmd, mb, NULL };
148         int ret;
149
150         snprintf(mb, 12, "minor-%d", mdev_to_minor(mdev));
151
152         if (get_net_conf(mdev)) {
153                 switch (((struct sockaddr *)mdev->net_conf->peer_addr)->sa_family) {
154                 case AF_INET6:
155                         afs = "ipv6";
156                         snprintf(ad, 60, "DRBD_PEER_ADDRESS=%pI6",
157                                  &((struct sockaddr_in6 *)mdev->net_conf->peer_addr)->sin6_addr);
158                         break;
159                 case AF_INET:
160                         afs = "ipv4";
161                         snprintf(ad, 60, "DRBD_PEER_ADDRESS=%pI4",
162                                  &((struct sockaddr_in *)mdev->net_conf->peer_addr)->sin_addr);
163                         break;
164                 default:
165                         afs = "ssocks";
166                         snprintf(ad, 60, "DRBD_PEER_ADDRESS=%pI4",
167                                  &((struct sockaddr_in *)mdev->net_conf->peer_addr)->sin_addr);
168                 }
169                 snprintf(af, 20, "DRBD_PEER_AF=%s", afs);
170                 envp[3]=af;
171                 envp[4]=ad;
172                 put_net_conf(mdev);
173         }
174
175         /* The helper may take some time.
176          * write out any unsynced meta data changes now */
177         drbd_md_sync(mdev);
178
179         dev_info(DEV, "helper command: %s %s %s\n", usermode_helper, cmd, mb);
180
181         drbd_bcast_ev_helper(mdev, cmd);
182         ret = call_usermodehelper(usermode_helper, argv, envp, 1);
183         if (ret)
184                 dev_warn(DEV, "helper command: %s %s %s exit code %u (0x%x)\n",
185                                 usermode_helper, cmd, mb,
186                                 (ret >> 8) & 0xff, ret);
187         else
188                 dev_info(DEV, "helper command: %s %s %s exit code %u (0x%x)\n",
189                                 usermode_helper, cmd, mb,
190                                 (ret >> 8) & 0xff, ret);
191
192         if (ret < 0) /* Ignore any ERRNOs we got. */
193                 ret = 0;
194
195         return ret;
196 }
197
198 enum drbd_disk_state drbd_try_outdate_peer(struct drbd_conf *mdev)
199 {
200         char *ex_to_string;
201         int r;
202         enum drbd_disk_state nps;
203         enum drbd_fencing_p fp;
204
205         D_ASSERT(mdev->state.pdsk == D_UNKNOWN);
206
207         if (get_ldev_if_state(mdev, D_CONSISTENT)) {
208                 fp = mdev->ldev->dc.fencing;
209                 put_ldev(mdev);
210         } else {
211                 dev_warn(DEV, "Not fencing peer, I'm not even Consistent myself.\n");
212                 nps = mdev->state.pdsk;
213                 goto out;
214         }
215
216         r = drbd_khelper(mdev, "fence-peer");
217
218         switch ((r>>8) & 0xff) {
219         case 3: /* peer is inconsistent */
220                 ex_to_string = "peer is inconsistent or worse";
221                 nps = D_INCONSISTENT;
222                 break;
223         case 4: /* peer got outdated, or was already outdated */
224                 ex_to_string = "peer was fenced";
225                 nps = D_OUTDATED;
226                 break;
227         case 5: /* peer was down */
228                 if (mdev->state.disk == D_UP_TO_DATE) {
229                         /* we will(have) create(d) a new UUID anyways... */
230                         ex_to_string = "peer is unreachable, assumed to be dead";
231                         nps = D_OUTDATED;
232                 } else {
233                         ex_to_string = "peer unreachable, doing nothing since disk != UpToDate";
234                         nps = mdev->state.pdsk;
235                 }
236                 break;
237         case 6: /* Peer is primary, voluntarily outdate myself.
238                  * This is useful when an unconnected R_SECONDARY is asked to
239                  * become R_PRIMARY, but finds the other peer being active. */
240                 ex_to_string = "peer is active";
241                 dev_warn(DEV, "Peer is primary, outdating myself.\n");
242                 nps = D_UNKNOWN;
243                 _drbd_request_state(mdev, NS(disk, D_OUTDATED), CS_WAIT_COMPLETE);
244                 break;
245         case 7:
246                 if (fp != FP_STONITH)
247                         dev_err(DEV, "fence-peer() = 7 && fencing != Stonith !!!\n");
248                 ex_to_string = "peer was stonithed";
249                 nps = D_OUTDATED;
250                 break;
251         default:
252                 /* The script is broken ... */
253                 nps = D_UNKNOWN;
254                 dev_err(DEV, "fence-peer helper broken, returned %d\n", (r>>8)&0xff);
255                 return nps;
256         }
257
258         dev_info(DEV, "fence-peer helper returned %d (%s)\n",
259                         (r>>8) & 0xff, ex_to_string);
260
261 out:
262         if (mdev->state.susp_fen && nps >= D_UNKNOWN) {
263                 /* The handler was not successful... unfreeze here, the
264                    state engine can not unfreeze... */
265                 _drbd_request_state(mdev, NS(susp_fen, 0), CS_VERBOSE);
266         }
267
268         return nps;
269 }
270
271 static int _try_outdate_peer_async(void *data)
272 {
273         struct drbd_conf *mdev = (struct drbd_conf *)data;
274         enum drbd_disk_state nps;
275
276         nps = drbd_try_outdate_peer(mdev);
277         drbd_request_state(mdev, NS(pdsk, nps));
278
279         return 0;
280 }
281
282 void drbd_try_outdate_peer_async(struct drbd_conf *mdev)
283 {
284         struct task_struct *opa;
285
286         opa = kthread_run(_try_outdate_peer_async, mdev, "drbd%d_a_helper", mdev_to_minor(mdev));
287         if (IS_ERR(opa))
288                 dev_err(DEV, "out of mem, failed to invoke fence-peer helper\n");
289 }
290
291 int drbd_set_role(struct drbd_conf *mdev, enum drbd_role new_role, int force)
292 {
293         const int max_tries = 4;
294         int r = 0;
295         int try = 0;
296         int forced = 0;
297         union drbd_state mask, val;
298         enum drbd_disk_state nps;
299
300         if (new_role == R_PRIMARY)
301                 request_ping(mdev); /* Detect a dead peer ASAP */
302
303         mutex_lock(&mdev->state_mutex);
304
305         mask.i = 0; mask.role = R_MASK;
306         val.i  = 0; val.role  = new_role;
307
308         while (try++ < max_tries) {
309                 r = _drbd_request_state(mdev, mask, val, CS_WAIT_COMPLETE);
310
311                 /* in case we first succeeded to outdate,
312                  * but now suddenly could establish a connection */
313                 if (r == SS_CW_FAILED_BY_PEER && mask.pdsk != 0) {
314                         val.pdsk = 0;
315                         mask.pdsk = 0;
316                         continue;
317                 }
318
319                 if (r == SS_NO_UP_TO_DATE_DISK && force &&
320                     (mdev->state.disk < D_UP_TO_DATE &&
321                      mdev->state.disk >= D_INCONSISTENT)) {
322                         mask.disk = D_MASK;
323                         val.disk  = D_UP_TO_DATE;
324                         forced = 1;
325                         continue;
326                 }
327
328                 if (r == SS_NO_UP_TO_DATE_DISK &&
329                     mdev->state.disk == D_CONSISTENT && mask.pdsk == 0) {
330                         D_ASSERT(mdev->state.pdsk == D_UNKNOWN);
331                         nps = drbd_try_outdate_peer(mdev);
332
333                         if (nps == D_OUTDATED || nps == D_INCONSISTENT) {
334                                 val.disk = D_UP_TO_DATE;
335                                 mask.disk = D_MASK;
336                         }
337
338                         val.pdsk = nps;
339                         mask.pdsk = D_MASK;
340
341                         continue;
342                 }
343
344                 if (r == SS_NOTHING_TO_DO)
345                         goto fail;
346                 if (r == SS_PRIMARY_NOP && mask.pdsk == 0) {
347                         nps = drbd_try_outdate_peer(mdev);
348
349                         if (force && nps > D_OUTDATED) {
350                                 dev_warn(DEV, "Forced into split brain situation!\n");
351                                 nps = D_OUTDATED;
352                         }
353
354                         mask.pdsk = D_MASK;
355                         val.pdsk  = nps;
356
357                         continue;
358                 }
359                 if (r == SS_TWO_PRIMARIES) {
360                         /* Maybe the peer is detected as dead very soon...
361                            retry at most once more in this case. */
362                         __set_current_state(TASK_INTERRUPTIBLE);
363                         schedule_timeout((mdev->net_conf->ping_timeo+1)*HZ/10);
364                         if (try < max_tries)
365                                 try = max_tries - 1;
366                         continue;
367                 }
368                 if (r < SS_SUCCESS) {
369                         r = _drbd_request_state(mdev, mask, val,
370                                                 CS_VERBOSE + CS_WAIT_COMPLETE);
371                         if (r < SS_SUCCESS)
372                                 goto fail;
373                 }
374                 break;
375         }
376
377         if (r < SS_SUCCESS)
378                 goto fail;
379
380         if (forced)
381                 dev_warn(DEV, "Forced to consider local data as UpToDate!\n");
382
383         /* Wait until nothing is on the fly :) */
384         wait_event(mdev->misc_wait, atomic_read(&mdev->ap_pending_cnt) == 0);
385
386         if (new_role == R_SECONDARY) {
387                 set_disk_ro(mdev->vdisk, TRUE);
388                 if (get_ldev(mdev)) {
389                         mdev->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
390                         put_ldev(mdev);
391                 }
392         } else {
393                 if (get_net_conf(mdev)) {
394                         mdev->net_conf->want_lose = 0;
395                         put_net_conf(mdev);
396                 }
397                 set_disk_ro(mdev->vdisk, FALSE);
398                 if (get_ldev(mdev)) {
399                         if (((mdev->state.conn < C_CONNECTED ||
400                                mdev->state.pdsk <= D_FAILED)
401                               && mdev->ldev->md.uuid[UI_BITMAP] == 0) || forced)
402                                 drbd_uuid_new_current(mdev);
403
404                         mdev->ldev->md.uuid[UI_CURRENT] |=  (u64)1;
405                         put_ldev(mdev);
406                 }
407         }
408
409         if ((new_role == R_SECONDARY) && get_ldev(mdev)) {
410                 drbd_al_to_on_disk_bm(mdev);
411                 put_ldev(mdev);
412         }
413
414         if (mdev->state.conn >= C_WF_REPORT_PARAMS) {
415                 /* if this was forced, we should consider sync */
416                 if (forced)
417                         drbd_send_uuids(mdev);
418                 drbd_send_state(mdev);
419         }
420
421         drbd_md_sync(mdev);
422
423         kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
424  fail:
425         mutex_unlock(&mdev->state_mutex);
426         return r;
427 }
428
429 static struct drbd_conf *ensure_mdev(int minor, int create)
430 {
431         struct drbd_conf *mdev;
432
433         if (minor >= minor_count)
434                 return NULL;
435
436         mdev = minor_to_mdev(minor);
437
438         if (!mdev && create) {
439                 struct gendisk *disk = NULL;
440                 mdev = drbd_new_device(minor);
441
442                 spin_lock_irq(&drbd_pp_lock);
443                 if (minor_table[minor] == NULL) {
444                         minor_table[minor] = mdev;
445                         disk = mdev->vdisk;
446                         mdev = NULL;
447                 } /* else: we lost the race */
448                 spin_unlock_irq(&drbd_pp_lock);
449
450                 if (disk) /* we won the race above */
451                         /* in case we ever add a drbd_delete_device(),
452                          * don't forget the del_gendisk! */
453                         add_disk(disk);
454                 else /* we lost the race above */
455                         drbd_free_mdev(mdev);
456
457                 mdev = minor_to_mdev(minor);
458         }
459
460         return mdev;
461 }
462
463 static int drbd_nl_primary(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
464                            struct drbd_nl_cfg_reply *reply)
465 {
466         struct primary primary_args;
467
468         memset(&primary_args, 0, sizeof(struct primary));
469         if (!primary_from_tags(mdev, nlp->tag_list, &primary_args)) {
470                 reply->ret_code = ERR_MANDATORY_TAG;
471                 return 0;
472         }
473
474         reply->ret_code =
475                 drbd_set_role(mdev, R_PRIMARY, primary_args.primary_force);
476
477         return 0;
478 }
479
480 static int drbd_nl_secondary(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
481                              struct drbd_nl_cfg_reply *reply)
482 {
483         reply->ret_code = drbd_set_role(mdev, R_SECONDARY, 0);
484
485         return 0;
486 }
487
488 /* initializes the md.*_offset members, so we are able to find
489  * the on disk meta data */
490 static void drbd_md_set_sector_offsets(struct drbd_conf *mdev,
491                                        struct drbd_backing_dev *bdev)
492 {
493         sector_t md_size_sect = 0;
494         switch (bdev->dc.meta_dev_idx) {
495         default:
496                 /* v07 style fixed size indexed meta data */
497                 bdev->md.md_size_sect = MD_RESERVED_SECT;
498                 bdev->md.md_offset = drbd_md_ss__(mdev, bdev);
499                 bdev->md.al_offset = MD_AL_OFFSET;
500                 bdev->md.bm_offset = MD_BM_OFFSET;
501                 break;
502         case DRBD_MD_INDEX_FLEX_EXT:
503                 /* just occupy the full device; unit: sectors */
504                 bdev->md.md_size_sect = drbd_get_capacity(bdev->md_bdev);
505                 bdev->md.md_offset = 0;
506                 bdev->md.al_offset = MD_AL_OFFSET;
507                 bdev->md.bm_offset = MD_BM_OFFSET;
508                 break;
509         case DRBD_MD_INDEX_INTERNAL:
510         case DRBD_MD_INDEX_FLEX_INT:
511                 bdev->md.md_offset = drbd_md_ss__(mdev, bdev);
512                 /* al size is still fixed */
513                 bdev->md.al_offset = -MD_AL_MAX_SIZE;
514                 /* we need (slightly less than) ~ this much bitmap sectors: */
515                 md_size_sect = drbd_get_capacity(bdev->backing_bdev);
516                 md_size_sect = ALIGN(md_size_sect, BM_SECT_PER_EXT);
517                 md_size_sect = BM_SECT_TO_EXT(md_size_sect);
518                 md_size_sect = ALIGN(md_size_sect, 8);
519
520                 /* plus the "drbd meta data super block",
521                  * and the activity log; */
522                 md_size_sect += MD_BM_OFFSET;
523
524                 bdev->md.md_size_sect = md_size_sect;
525                 /* bitmap offset is adjusted by 'super' block size */
526                 bdev->md.bm_offset   = -md_size_sect + MD_AL_OFFSET;
527                 break;
528         }
529 }
530
531 char *ppsize(char *buf, unsigned long long size)
532 {
533         /* Needs 9 bytes at max. */
534         static char units[] = { 'K', 'M', 'G', 'T', 'P', 'E' };
535         int base = 0;
536         while (size >= 10000) {
537                 /* shift + round */
538                 size = (size >> 10) + !!(size & (1<<9));
539                 base++;
540         }
541         sprintf(buf, "%lu %cB", (long)size, units[base]);
542
543         return buf;
544 }
545
546 /* there is still a theoretical deadlock when called from receiver
547  * on an D_INCONSISTENT R_PRIMARY:
548  *  remote READ does inc_ap_bio, receiver would need to receive answer
549  *  packet from remote to dec_ap_bio again.
550  *  receiver receive_sizes(), comes here,
551  *  waits for ap_bio_cnt == 0. -> deadlock.
552  * but this cannot happen, actually, because:
553  *  R_PRIMARY D_INCONSISTENT, and peer's disk is unreachable
554  *  (not connected, or bad/no disk on peer):
555  *  see drbd_fail_request_early, ap_bio_cnt is zero.
556  *  R_PRIMARY D_INCONSISTENT, and C_SYNC_TARGET:
557  *  peer may not initiate a resize.
558  */
559 void drbd_suspend_io(struct drbd_conf *mdev)
560 {
561         set_bit(SUSPEND_IO, &mdev->flags);
562         if (is_susp(mdev->state))
563                 return;
564         wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_bio_cnt));
565 }
566
567 void drbd_resume_io(struct drbd_conf *mdev)
568 {
569         clear_bit(SUSPEND_IO, &mdev->flags);
570         wake_up(&mdev->misc_wait);
571 }
572
573 /**
574  * drbd_determine_dev_size() -  Sets the right device size obeying all constraints
575  * @mdev:       DRBD device.
576  *
577  * Returns 0 on success, negative return values indicate errors.
578  * You should call drbd_md_sync() after calling this function.
579  */
580 enum determine_dev_size drbd_determin_dev_size(struct drbd_conf *mdev, enum dds_flags flags) __must_hold(local)
581 {
582         sector_t prev_first_sect, prev_size; /* previous meta location */
583         sector_t la_size;
584         sector_t size;
585         char ppb[10];
586
587         int md_moved, la_size_changed;
588         enum determine_dev_size rv = unchanged;
589
590         /* race:
591          * application request passes inc_ap_bio,
592          * but then cannot get an AL-reference.
593          * this function later may wait on ap_bio_cnt == 0. -> deadlock.
594          *
595          * to avoid that:
596          * Suspend IO right here.
597          * still lock the act_log to not trigger ASSERTs there.
598          */
599         drbd_suspend_io(mdev);
600
601         /* no wait necessary anymore, actually we could assert that */
602         wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
603
604         prev_first_sect = drbd_md_first_sector(mdev->ldev);
605         prev_size = mdev->ldev->md.md_size_sect;
606         la_size = mdev->ldev->md.la_size_sect;
607
608         /* TODO: should only be some assert here, not (re)init... */
609         drbd_md_set_sector_offsets(mdev, mdev->ldev);
610
611         size = drbd_new_dev_size(mdev, mdev->ldev, flags & DDSF_FORCED);
612
613         if (drbd_get_capacity(mdev->this_bdev) != size ||
614             drbd_bm_capacity(mdev) != size) {
615                 int err;
616                 err = drbd_bm_resize(mdev, size, !(flags & DDSF_NO_RESYNC));
617                 if (unlikely(err)) {
618                         /* currently there is only one error: ENOMEM! */
619                         size = drbd_bm_capacity(mdev)>>1;
620                         if (size == 0) {
621                                 dev_err(DEV, "OUT OF MEMORY! "
622                                     "Could not allocate bitmap!\n");
623                         } else {
624                                 dev_err(DEV, "BM resizing failed. "
625                                     "Leaving size unchanged at size = %lu KB\n",
626                                     (unsigned long)size);
627                         }
628                         rv = dev_size_error;
629                 }
630                 /* racy, see comments above. */
631                 drbd_set_my_capacity(mdev, size);
632                 mdev->ldev->md.la_size_sect = size;
633                 dev_info(DEV, "size = %s (%llu KB)\n", ppsize(ppb, size>>1),
634                      (unsigned long long)size>>1);
635         }
636         if (rv == dev_size_error)
637                 goto out;
638
639         la_size_changed = (la_size != mdev->ldev->md.la_size_sect);
640
641         md_moved = prev_first_sect != drbd_md_first_sector(mdev->ldev)
642                 || prev_size       != mdev->ldev->md.md_size_sect;
643
644         if (la_size_changed || md_moved) {
645                 drbd_al_shrink(mdev); /* All extents inactive. */
646                 dev_info(DEV, "Writing the whole bitmap, %s\n",
647                          la_size_changed && md_moved ? "size changed and md moved" :
648                          la_size_changed ? "size changed" : "md moved");
649                 rv = drbd_bitmap_io(mdev, &drbd_bm_write, "size changed"); /* does drbd_resume_io() ! */
650                 drbd_md_mark_dirty(mdev);
651         }
652
653         if (size > la_size)
654                 rv = grew;
655         if (size < la_size)
656                 rv = shrunk;
657 out:
658         lc_unlock(mdev->act_log);
659         wake_up(&mdev->al_wait);
660         drbd_resume_io(mdev);
661
662         return rv;
663 }
664
665 sector_t
666 drbd_new_dev_size(struct drbd_conf *mdev, struct drbd_backing_dev *bdev, int assume_peer_has_space)
667 {
668         sector_t p_size = mdev->p_size;   /* partner's disk size. */
669         sector_t la_size = bdev->md.la_size_sect; /* last agreed size. */
670         sector_t m_size; /* my size */
671         sector_t u_size = bdev->dc.disk_size; /* size requested by user. */
672         sector_t size = 0;
673
674         m_size = drbd_get_max_capacity(bdev);
675
676         if (mdev->state.conn < C_CONNECTED && assume_peer_has_space) {
677                 dev_warn(DEV, "Resize while not connected was forced by the user!\n");
678                 p_size = m_size;
679         }
680
681         if (p_size && m_size) {
682                 size = min_t(sector_t, p_size, m_size);
683         } else {
684                 if (la_size) {
685                         size = la_size;
686                         if (m_size && m_size < size)
687                                 size = m_size;
688                         if (p_size && p_size < size)
689                                 size = p_size;
690                 } else {
691                         if (m_size)
692                                 size = m_size;
693                         if (p_size)
694                                 size = p_size;
695                 }
696         }
697
698         if (size == 0)
699                 dev_err(DEV, "Both nodes diskless!\n");
700
701         if (u_size) {
702                 if (u_size > size)
703                         dev_err(DEV, "Requested disk size is too big (%lu > %lu)\n",
704                             (unsigned long)u_size>>1, (unsigned long)size>>1);
705                 else
706                         size = u_size;
707         }
708
709         return size;
710 }
711
712 /**
713  * drbd_check_al_size() - Ensures that the AL is of the right size
714  * @mdev:       DRBD device.
715  *
716  * Returns -EBUSY if current al lru is still used, -ENOMEM when allocation
717  * failed, and 0 on success. You should call drbd_md_sync() after you called
718  * this function.
719  */
720 static int drbd_check_al_size(struct drbd_conf *mdev)
721 {
722         struct lru_cache *n, *t;
723         struct lc_element *e;
724         unsigned int in_use;
725         int i;
726
727         ERR_IF(mdev->sync_conf.al_extents < 7)
728                 mdev->sync_conf.al_extents = 127;
729
730         if (mdev->act_log &&
731             mdev->act_log->nr_elements == mdev->sync_conf.al_extents)
732                 return 0;
733
734         in_use = 0;
735         t = mdev->act_log;
736         n = lc_create("act_log", drbd_al_ext_cache,
737                 mdev->sync_conf.al_extents, sizeof(struct lc_element), 0);
738
739         if (n == NULL) {
740                 dev_err(DEV, "Cannot allocate act_log lru!\n");
741                 return -ENOMEM;
742         }
743         spin_lock_irq(&mdev->al_lock);
744         if (t) {
745                 for (i = 0; i < t->nr_elements; i++) {
746                         e = lc_element_by_index(t, i);
747                         if (e->refcnt)
748                                 dev_err(DEV, "refcnt(%d)==%d\n",
749                                     e->lc_number, e->refcnt);
750                         in_use += e->refcnt;
751                 }
752         }
753         if (!in_use)
754                 mdev->act_log = n;
755         spin_unlock_irq(&mdev->al_lock);
756         if (in_use) {
757                 dev_err(DEV, "Activity log still in use!\n");
758                 lc_destroy(n);
759                 return -EBUSY;
760         } else {
761                 if (t)
762                         lc_destroy(t);
763         }
764         drbd_md_mark_dirty(mdev); /* we changed mdev->act_log->nr_elemens */
765         return 0;
766 }
767
768 void drbd_setup_queue_param(struct drbd_conf *mdev, unsigned int max_seg_s) __must_hold(local)
769 {
770         struct request_queue * const q = mdev->rq_queue;
771         struct request_queue * const b = mdev->ldev->backing_bdev->bd_disk->queue;
772         int max_segments = mdev->ldev->dc.max_bio_bvecs;
773
774         max_seg_s = min(queue_max_sectors(b) * queue_logical_block_size(b), max_seg_s);
775
776         blk_queue_max_hw_sectors(q, max_seg_s >> 9);
777         blk_queue_max_segments(q, max_segments ? max_segments : BLK_MAX_SEGMENTS);
778         blk_queue_max_segment_size(q, max_seg_s);
779         blk_queue_logical_block_size(q, 512);
780         blk_queue_segment_boundary(q, PAGE_SIZE-1);
781         blk_stack_limits(&q->limits, &b->limits, 0);
782
783         dev_info(DEV, "max_segment_size ( = BIO size ) = %u\n", queue_max_segment_size(q));
784
785         if (q->backing_dev_info.ra_pages != b->backing_dev_info.ra_pages) {
786                 dev_info(DEV, "Adjusting my ra_pages to backing device's (%lu -> %lu)\n",
787                      q->backing_dev_info.ra_pages,
788                      b->backing_dev_info.ra_pages);
789                 q->backing_dev_info.ra_pages = b->backing_dev_info.ra_pages;
790         }
791 }
792
793 /* serialize deconfig (worker exiting, doing cleanup)
794  * and reconfig (drbdsetup disk, drbdsetup net)
795  *
796  * Wait for a potentially exiting worker, then restart it,
797  * or start a new one.  Flush any pending work, there may still be an
798  * after_state_change queued.
799  */
800 static void drbd_reconfig_start(struct drbd_conf *mdev)
801 {
802         wait_event(mdev->state_wait, !test_and_set_bit(CONFIG_PENDING, &mdev->flags));
803         wait_event(mdev->state_wait, !test_bit(DEVICE_DYING, &mdev->flags));
804         drbd_thread_start(&mdev->worker);
805         drbd_flush_workqueue(mdev);
806 }
807
808 /* if still unconfigured, stops worker again.
809  * if configured now, clears CONFIG_PENDING.
810  * wakes potential waiters */
811 static void drbd_reconfig_done(struct drbd_conf *mdev)
812 {
813         spin_lock_irq(&mdev->req_lock);
814         if (mdev->state.disk == D_DISKLESS &&
815             mdev->state.conn == C_STANDALONE &&
816             mdev->state.role == R_SECONDARY) {
817                 set_bit(DEVICE_DYING, &mdev->flags);
818                 drbd_thread_stop_nowait(&mdev->worker);
819         } else
820                 clear_bit(CONFIG_PENDING, &mdev->flags);
821         spin_unlock_irq(&mdev->req_lock);
822         wake_up(&mdev->state_wait);
823 }
824
825 /* Make sure IO is suspended before calling this function(). */
826 static void drbd_suspend_al(struct drbd_conf *mdev)
827 {
828         int s = 0;
829
830         if (lc_try_lock(mdev->act_log)) {
831                 drbd_al_shrink(mdev);
832                 lc_unlock(mdev->act_log);
833         } else {
834                 dev_warn(DEV, "Failed to lock al in drbd_suspend_al()\n");
835                 return;
836         }
837
838         spin_lock_irq(&mdev->req_lock);
839         if (mdev->state.conn < C_CONNECTED)
840                 s = !test_and_set_bit(AL_SUSPENDED, &mdev->flags);
841
842         spin_unlock_irq(&mdev->req_lock);
843
844         if (s)
845                 dev_info(DEV, "Suspended AL updates\n");
846 }
847
848 /* does always return 0;
849  * interesting return code is in reply->ret_code */
850 static int drbd_nl_disk_conf(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
851                              struct drbd_nl_cfg_reply *reply)
852 {
853         enum drbd_ret_codes retcode;
854         enum determine_dev_size dd;
855         sector_t max_possible_sectors;
856         sector_t min_md_device_sectors;
857         struct drbd_backing_dev *nbc = NULL; /* new_backing_conf */
858         struct inode *inode, *inode2;
859         struct lru_cache *resync_lru = NULL;
860         union drbd_state ns, os;
861         unsigned int max_seg_s;
862         int rv;
863         int cp_discovered = 0;
864         int logical_block_size;
865
866         drbd_reconfig_start(mdev);
867
868         /* if you want to reconfigure, please tear down first */
869         if (mdev->state.disk > D_DISKLESS) {
870                 retcode = ERR_DISK_CONFIGURED;
871                 goto fail;
872         }
873         /* It may just now have detached because of IO error.  Make sure
874          * drbd_ldev_destroy is done already, we may end up here very fast,
875          * e.g. if someone calls attach from the on-io-error handler,
876          * to realize a "hot spare" feature (not that I'd recommend that) */
877         wait_event(mdev->misc_wait, !atomic_read(&mdev->local_cnt));
878
879         /* allocation not in the IO path, cqueue thread context */
880         nbc = kzalloc(sizeof(struct drbd_backing_dev), GFP_KERNEL);
881         if (!nbc) {
882                 retcode = ERR_NOMEM;
883                 goto fail;
884         }
885
886         nbc->dc.disk_size     = DRBD_DISK_SIZE_SECT_DEF;
887         nbc->dc.on_io_error   = DRBD_ON_IO_ERROR_DEF;
888         nbc->dc.fencing       = DRBD_FENCING_DEF;
889         nbc->dc.max_bio_bvecs = DRBD_MAX_BIO_BVECS_DEF;
890
891         if (!disk_conf_from_tags(mdev, nlp->tag_list, &nbc->dc)) {
892                 retcode = ERR_MANDATORY_TAG;
893                 goto fail;
894         }
895
896         if (nbc->dc.meta_dev_idx < DRBD_MD_INDEX_FLEX_INT) {
897                 retcode = ERR_MD_IDX_INVALID;
898                 goto fail;
899         }
900
901         if (get_net_conf(mdev)) {
902                 int prot = mdev->net_conf->wire_protocol;
903                 put_net_conf(mdev);
904                 if (nbc->dc.fencing == FP_STONITH && prot == DRBD_PROT_A) {
905                         retcode = ERR_STONITH_AND_PROT_A;
906                         goto fail;
907                 }
908         }
909
910         nbc->lo_file = filp_open(nbc->dc.backing_dev, O_RDWR, 0);
911         if (IS_ERR(nbc->lo_file)) {
912                 dev_err(DEV, "open(\"%s\") failed with %ld\n", nbc->dc.backing_dev,
913                     PTR_ERR(nbc->lo_file));
914                 nbc->lo_file = NULL;
915                 retcode = ERR_OPEN_DISK;
916                 goto fail;
917         }
918
919         inode = nbc->lo_file->f_dentry->d_inode;
920
921         if (!S_ISBLK(inode->i_mode)) {
922                 retcode = ERR_DISK_NOT_BDEV;
923                 goto fail;
924         }
925
926         nbc->md_file = filp_open(nbc->dc.meta_dev, O_RDWR, 0);
927         if (IS_ERR(nbc->md_file)) {
928                 dev_err(DEV, "open(\"%s\") failed with %ld\n", nbc->dc.meta_dev,
929                     PTR_ERR(nbc->md_file));
930                 nbc->md_file = NULL;
931                 retcode = ERR_OPEN_MD_DISK;
932                 goto fail;
933         }
934
935         inode2 = nbc->md_file->f_dentry->d_inode;
936
937         if (!S_ISBLK(inode2->i_mode)) {
938                 retcode = ERR_MD_NOT_BDEV;
939                 goto fail;
940         }
941
942         nbc->backing_bdev = inode->i_bdev;
943         if (bd_claim(nbc->backing_bdev, mdev)) {
944                 printk(KERN_ERR "drbd: bd_claim(%p,%p); failed [%p;%p;%u]\n",
945                        nbc->backing_bdev, mdev,
946                        nbc->backing_bdev->bd_holder,
947                        nbc->backing_bdev->bd_contains->bd_holder,
948                        nbc->backing_bdev->bd_holders);
949                 retcode = ERR_BDCLAIM_DISK;
950                 goto fail;
951         }
952
953         resync_lru = lc_create("resync", drbd_bm_ext_cache,
954                         61, sizeof(struct bm_extent),
955                         offsetof(struct bm_extent, lce));
956         if (!resync_lru) {
957                 retcode = ERR_NOMEM;
958                 goto release_bdev_fail;
959         }
960
961         /* meta_dev_idx >= 0: external fixed size,
962          * possibly multiple drbd sharing one meta device.
963          * TODO in that case, paranoia check that [md_bdev, meta_dev_idx] is
964          * not yet used by some other drbd minor!
965          * (if you use drbd.conf + drbdadm,
966          * that should check it for you already; but if you don't, or someone
967          * fooled it, we need to double check here) */
968         nbc->md_bdev = inode2->i_bdev;
969         if (bd_claim(nbc->md_bdev, (nbc->dc.meta_dev_idx < 0) ? (void *)mdev
970                                 : (void *) drbd_m_holder)) {
971                 retcode = ERR_BDCLAIM_MD_DISK;
972                 goto release_bdev_fail;
973         }
974
975         if ((nbc->backing_bdev == nbc->md_bdev) !=
976             (nbc->dc.meta_dev_idx == DRBD_MD_INDEX_INTERNAL ||
977              nbc->dc.meta_dev_idx == DRBD_MD_INDEX_FLEX_INT)) {
978                 retcode = ERR_MD_IDX_INVALID;
979                 goto release_bdev2_fail;
980         }
981
982         /* RT - for drbd_get_max_capacity() DRBD_MD_INDEX_FLEX_INT */
983         drbd_md_set_sector_offsets(mdev, nbc);
984
985         if (drbd_get_max_capacity(nbc) < nbc->dc.disk_size) {
986                 dev_err(DEV, "max capacity %llu smaller than disk size %llu\n",
987                         (unsigned long long) drbd_get_max_capacity(nbc),
988                         (unsigned long long) nbc->dc.disk_size);
989                 retcode = ERR_DISK_TO_SMALL;
990                 goto release_bdev2_fail;
991         }
992
993         if (nbc->dc.meta_dev_idx < 0) {
994                 max_possible_sectors = DRBD_MAX_SECTORS_FLEX;
995                 /* at least one MB, otherwise it does not make sense */
996                 min_md_device_sectors = (2<<10);
997         } else {
998                 max_possible_sectors = DRBD_MAX_SECTORS;
999                 min_md_device_sectors = MD_RESERVED_SECT * (nbc->dc.meta_dev_idx + 1);
1000         }
1001
1002         if (drbd_get_capacity(nbc->md_bdev) < min_md_device_sectors) {
1003                 retcode = ERR_MD_DISK_TO_SMALL;
1004                 dev_warn(DEV, "refusing attach: md-device too small, "
1005                      "at least %llu sectors needed for this meta-disk type\n",
1006                      (unsigned long long) min_md_device_sectors);
1007                 goto release_bdev2_fail;
1008         }
1009
1010         /* Make sure the new disk is big enough
1011          * (we may currently be R_PRIMARY with no local disk...) */
1012         if (drbd_get_max_capacity(nbc) <
1013             drbd_get_capacity(mdev->this_bdev)) {
1014                 retcode = ERR_DISK_TO_SMALL;
1015                 goto release_bdev2_fail;
1016         }
1017
1018         nbc->known_size = drbd_get_capacity(nbc->backing_bdev);
1019
1020         if (nbc->known_size > max_possible_sectors) {
1021                 dev_warn(DEV, "==> truncating very big lower level device "
1022                         "to currently maximum possible %llu sectors <==\n",
1023                         (unsigned long long) max_possible_sectors);
1024                 if (nbc->dc.meta_dev_idx >= 0)
1025                         dev_warn(DEV, "==>> using internal or flexible "
1026                                       "meta data may help <<==\n");
1027         }
1028
1029         drbd_suspend_io(mdev);
1030         /* also wait for the last barrier ack. */
1031         wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_pending_cnt) || is_susp(mdev->state));
1032         /* and for any other previously queued work */
1033         drbd_flush_workqueue(mdev);
1034
1035         retcode = _drbd_request_state(mdev, NS(disk, D_ATTACHING), CS_VERBOSE);
1036         drbd_resume_io(mdev);
1037         if (retcode < SS_SUCCESS)
1038                 goto release_bdev2_fail;
1039
1040         if (!get_ldev_if_state(mdev, D_ATTACHING))
1041                 goto force_diskless;
1042
1043         drbd_md_set_sector_offsets(mdev, nbc);
1044
1045         /* allocate a second IO page if logical_block_size != 512 */
1046         logical_block_size = bdev_logical_block_size(nbc->md_bdev);
1047         if (logical_block_size == 0)
1048                 logical_block_size = MD_SECTOR_SIZE;
1049
1050         if (logical_block_size != MD_SECTOR_SIZE) {
1051                 if (!mdev->md_io_tmpp) {
1052                         struct page *page = alloc_page(GFP_NOIO);
1053                         if (!page)
1054                                 goto force_diskless_dec;
1055
1056                         dev_warn(DEV, "Meta data's bdev logical_block_size = %d != %d\n",
1057                              logical_block_size, MD_SECTOR_SIZE);
1058                         dev_warn(DEV, "Workaround engaged (has performance impact).\n");
1059
1060                         mdev->md_io_tmpp = page;
1061                 }
1062         }
1063
1064         if (!mdev->bitmap) {
1065                 if (drbd_bm_init(mdev)) {
1066                         retcode = ERR_NOMEM;
1067                         goto force_diskless_dec;
1068                 }
1069         }
1070
1071         retcode = drbd_md_read(mdev, nbc);
1072         if (retcode != NO_ERROR)
1073                 goto force_diskless_dec;
1074
1075         if (mdev->state.conn < C_CONNECTED &&
1076             mdev->state.role == R_PRIMARY &&
1077             (mdev->ed_uuid & ~((u64)1)) != (nbc->md.uuid[UI_CURRENT] & ~((u64)1))) {
1078                 dev_err(DEV, "Can only attach to data with current UUID=%016llX\n",
1079                     (unsigned long long)mdev->ed_uuid);
1080                 retcode = ERR_DATA_NOT_CURRENT;
1081                 goto force_diskless_dec;
1082         }
1083
1084         /* Since we are diskless, fix the activity log first... */
1085         if (drbd_check_al_size(mdev)) {
1086                 retcode = ERR_NOMEM;
1087                 goto force_diskless_dec;
1088         }
1089
1090         /* Prevent shrinking of consistent devices ! */
1091         if (drbd_md_test_flag(nbc, MDF_CONSISTENT) &&
1092             drbd_new_dev_size(mdev, nbc, 0) < nbc->md.la_size_sect) {
1093                 dev_warn(DEV, "refusing to truncate a consistent device\n");
1094                 retcode = ERR_DISK_TO_SMALL;
1095                 goto force_diskless_dec;
1096         }
1097
1098         if (!drbd_al_read_log(mdev, nbc)) {
1099                 retcode = ERR_IO_MD_DISK;
1100                 goto force_diskless_dec;
1101         }
1102
1103         /* Reset the "barriers don't work" bits here, then force meta data to
1104          * be written, to ensure we determine if barriers are supported. */
1105         if (nbc->dc.no_md_flush)
1106                 set_bit(MD_NO_FUA, &mdev->flags);
1107         else
1108                 clear_bit(MD_NO_FUA, &mdev->flags);
1109
1110         /* Point of no return reached.
1111          * Devices and memory are no longer released by error cleanup below.
1112          * now mdev takes over responsibility, and the state engine should
1113          * clean it up somewhere.  */
1114         D_ASSERT(mdev->ldev == NULL);
1115         mdev->ldev = nbc;
1116         mdev->resync = resync_lru;
1117         nbc = NULL;
1118         resync_lru = NULL;
1119
1120         mdev->write_ordering = WO_bdev_flush;
1121         drbd_bump_write_ordering(mdev, WO_bdev_flush);
1122
1123         if (drbd_md_test_flag(mdev->ldev, MDF_CRASHED_PRIMARY))
1124                 set_bit(CRASHED_PRIMARY, &mdev->flags);
1125         else
1126                 clear_bit(CRASHED_PRIMARY, &mdev->flags);
1127
1128         if (drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) &&
1129             !(mdev->state.role == R_PRIMARY && mdev->state.susp_nod)) {
1130                 set_bit(CRASHED_PRIMARY, &mdev->flags);
1131                 cp_discovered = 1;
1132         }
1133
1134         mdev->send_cnt = 0;
1135         mdev->recv_cnt = 0;
1136         mdev->read_cnt = 0;
1137         mdev->writ_cnt = 0;
1138
1139         max_seg_s = DRBD_MAX_SEGMENT_SIZE;
1140         if (mdev->state.conn == C_CONNECTED) {
1141                 /* We are Primary, Connected, and now attach a new local
1142                  * backing store. We must not increase the user visible maximum
1143                  * bio size on this device to something the peer may not be
1144                  * able to handle. */
1145                 if (mdev->agreed_pro_version < 94)
1146                         max_seg_s = queue_max_segment_size(mdev->rq_queue);
1147                 else if (mdev->agreed_pro_version == 94)
1148                         max_seg_s = DRBD_MAX_SIZE_H80_PACKET;
1149                 /* else: drbd 8.3.9 and later, stay with default */
1150         }
1151
1152         drbd_setup_queue_param(mdev, max_seg_s);
1153
1154         /* If I am currently not R_PRIMARY,
1155          * but meta data primary indicator is set,
1156          * I just now recover from a hard crash,
1157          * and have been R_PRIMARY before that crash.
1158          *
1159          * Now, if I had no connection before that crash
1160          * (have been degraded R_PRIMARY), chances are that
1161          * I won't find my peer now either.
1162          *
1163          * In that case, and _only_ in that case,
1164          * we use the degr-wfc-timeout instead of the default,
1165          * so we can automatically recover from a crash of a
1166          * degraded but active "cluster" after a certain timeout.
1167          */
1168         clear_bit(USE_DEGR_WFC_T, &mdev->flags);
1169         if (mdev->state.role != R_PRIMARY &&
1170              drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) &&
1171             !drbd_md_test_flag(mdev->ldev, MDF_CONNECTED_IND))
1172                 set_bit(USE_DEGR_WFC_T, &mdev->flags);
1173
1174         dd = drbd_determin_dev_size(mdev, 0);
1175         if (dd == dev_size_error) {
1176                 retcode = ERR_NOMEM_BITMAP;
1177                 goto force_diskless_dec;
1178         } else if (dd == grew)
1179                 set_bit(RESYNC_AFTER_NEG, &mdev->flags);
1180
1181         if (drbd_md_test_flag(mdev->ldev, MDF_FULL_SYNC)) {
1182                 dev_info(DEV, "Assuming that all blocks are out of sync "
1183                      "(aka FullSync)\n");
1184                 if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write, "set_n_write from attaching")) {
1185                         retcode = ERR_IO_MD_DISK;
1186                         goto force_diskless_dec;
1187                 }
1188         } else {
1189                 if (drbd_bitmap_io(mdev, &drbd_bm_read, "read from attaching") < 0) {
1190                         retcode = ERR_IO_MD_DISK;
1191                         goto force_diskless_dec;
1192                 }
1193         }
1194
1195         if (cp_discovered) {
1196                 drbd_al_apply_to_bm(mdev);
1197                 drbd_al_to_on_disk_bm(mdev);
1198         }
1199
1200         if (_drbd_bm_total_weight(mdev) == drbd_bm_bits(mdev))
1201                 drbd_suspend_al(mdev); /* IO is still suspended here... */
1202
1203         spin_lock_irq(&mdev->req_lock);
1204         os = mdev->state;
1205         ns.i = os.i;
1206         /* If MDF_CONSISTENT is not set go into inconsistent state,
1207            otherwise investigate MDF_WasUpToDate...
1208            If MDF_WAS_UP_TO_DATE is not set go into D_OUTDATED disk state,
1209            otherwise into D_CONSISTENT state.
1210         */
1211         if (drbd_md_test_flag(mdev->ldev, MDF_CONSISTENT)) {
1212                 if (drbd_md_test_flag(mdev->ldev, MDF_WAS_UP_TO_DATE))
1213                         ns.disk = D_CONSISTENT;
1214                 else
1215                         ns.disk = D_OUTDATED;
1216         } else {
1217                 ns.disk = D_INCONSISTENT;
1218         }
1219
1220         if (drbd_md_test_flag(mdev->ldev, MDF_PEER_OUT_DATED))
1221                 ns.pdsk = D_OUTDATED;
1222
1223         if ( ns.disk == D_CONSISTENT &&
1224             (ns.pdsk == D_OUTDATED || mdev->ldev->dc.fencing == FP_DONT_CARE))
1225                 ns.disk = D_UP_TO_DATE;
1226
1227         /* All tests on MDF_PRIMARY_IND, MDF_CONNECTED_IND,
1228            MDF_CONSISTENT and MDF_WAS_UP_TO_DATE must happen before
1229            this point, because drbd_request_state() modifies these
1230            flags. */
1231
1232         /* In case we are C_CONNECTED postpone any decision on the new disk
1233            state after the negotiation phase. */
1234         if (mdev->state.conn == C_CONNECTED) {
1235                 mdev->new_state_tmp.i = ns.i;
1236                 ns.i = os.i;
1237                 ns.disk = D_NEGOTIATING;
1238
1239                 /* We expect to receive up-to-date UUIDs soon.
1240                    To avoid a race in receive_state, free p_uuid while
1241                    holding req_lock. I.e. atomic with the state change */
1242                 kfree(mdev->p_uuid);
1243                 mdev->p_uuid = NULL;
1244         }
1245
1246         rv = _drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
1247         ns = mdev->state;
1248         spin_unlock_irq(&mdev->req_lock);
1249
1250         if (rv < SS_SUCCESS)
1251                 goto force_diskless_dec;
1252
1253         if (mdev->state.role == R_PRIMARY)
1254                 mdev->ldev->md.uuid[UI_CURRENT] |=  (u64)1;
1255         else
1256                 mdev->ldev->md.uuid[UI_CURRENT] &= ~(u64)1;
1257
1258         drbd_md_mark_dirty(mdev);
1259         drbd_md_sync(mdev);
1260
1261         kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
1262         put_ldev(mdev);
1263         reply->ret_code = retcode;
1264         drbd_reconfig_done(mdev);
1265         return 0;
1266
1267  force_diskless_dec:
1268         put_ldev(mdev);
1269  force_diskless:
1270         drbd_force_state(mdev, NS(disk, D_FAILED));
1271         drbd_md_sync(mdev);
1272  release_bdev2_fail:
1273         if (nbc)
1274                 bd_release(nbc->md_bdev);
1275  release_bdev_fail:
1276         if (nbc)
1277                 bd_release(nbc->backing_bdev);
1278  fail:
1279         if (nbc) {
1280                 if (nbc->lo_file)
1281                         fput(nbc->lo_file);
1282                 if (nbc->md_file)
1283                         fput(nbc->md_file);
1284                 kfree(nbc);
1285         }
1286         lc_destroy(resync_lru);
1287
1288         reply->ret_code = retcode;
1289         drbd_reconfig_done(mdev);
1290         return 0;
1291 }
1292
1293 /* Detaching the disk is a process in multiple stages.  First we need to lock
1294  * out application IO, in-flight IO, IO stuck in drbd_al_begin_io.
1295  * Then we transition to D_DISKLESS, and wait for put_ldev() to return all
1296  * internal references as well.
1297  * Only then we have finally detached. */
1298 static int drbd_nl_detach(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
1299                           struct drbd_nl_cfg_reply *reply)
1300 {
1301         drbd_suspend_io(mdev); /* so no-one is stuck in drbd_al_begin_io */
1302         reply->ret_code = drbd_request_state(mdev, NS(disk, D_DISKLESS));
1303         if (mdev->state.disk == D_DISKLESS)
1304                 wait_event(mdev->misc_wait, !atomic_read(&mdev->local_cnt));
1305         drbd_resume_io(mdev);
1306         return 0;
1307 }
1308
1309 static int drbd_nl_net_conf(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
1310                             struct drbd_nl_cfg_reply *reply)
1311 {
1312         int i, ns;
1313         enum drbd_ret_codes retcode;
1314         struct net_conf *new_conf = NULL;
1315         struct crypto_hash *tfm = NULL;
1316         struct crypto_hash *integrity_w_tfm = NULL;
1317         struct crypto_hash *integrity_r_tfm = NULL;
1318         struct hlist_head *new_tl_hash = NULL;
1319         struct hlist_head *new_ee_hash = NULL;
1320         struct drbd_conf *odev;
1321         char hmac_name[CRYPTO_MAX_ALG_NAME];
1322         void *int_dig_out = NULL;
1323         void *int_dig_in = NULL;
1324         void *int_dig_vv = NULL;
1325         struct sockaddr *new_my_addr, *new_peer_addr, *taken_addr;
1326
1327         drbd_reconfig_start(mdev);
1328
1329         if (mdev->state.conn > C_STANDALONE) {
1330                 retcode = ERR_NET_CONFIGURED;
1331                 goto fail;
1332         }
1333
1334         /* allocation not in the IO path, cqueue thread context */
1335         new_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
1336         if (!new_conf) {
1337                 retcode = ERR_NOMEM;
1338                 goto fail;
1339         }
1340
1341         new_conf->timeout          = DRBD_TIMEOUT_DEF;
1342         new_conf->try_connect_int  = DRBD_CONNECT_INT_DEF;
1343         new_conf->ping_int         = DRBD_PING_INT_DEF;
1344         new_conf->max_epoch_size   = DRBD_MAX_EPOCH_SIZE_DEF;
1345         new_conf->max_buffers      = DRBD_MAX_BUFFERS_DEF;
1346         new_conf->unplug_watermark = DRBD_UNPLUG_WATERMARK_DEF;
1347         new_conf->sndbuf_size      = DRBD_SNDBUF_SIZE_DEF;
1348         new_conf->rcvbuf_size      = DRBD_RCVBUF_SIZE_DEF;
1349         new_conf->ko_count         = DRBD_KO_COUNT_DEF;
1350         new_conf->after_sb_0p      = DRBD_AFTER_SB_0P_DEF;
1351         new_conf->after_sb_1p      = DRBD_AFTER_SB_1P_DEF;
1352         new_conf->after_sb_2p      = DRBD_AFTER_SB_2P_DEF;
1353         new_conf->want_lose        = 0;
1354         new_conf->two_primaries    = 0;
1355         new_conf->wire_protocol    = DRBD_PROT_C;
1356         new_conf->ping_timeo       = DRBD_PING_TIMEO_DEF;
1357         new_conf->rr_conflict      = DRBD_RR_CONFLICT_DEF;
1358
1359         if (!net_conf_from_tags(mdev, nlp->tag_list, new_conf)) {
1360                 retcode = ERR_MANDATORY_TAG;
1361                 goto fail;
1362         }
1363
1364         if (new_conf->two_primaries
1365             && (new_conf->wire_protocol != DRBD_PROT_C)) {
1366                 retcode = ERR_NOT_PROTO_C;
1367                 goto fail;
1368         }
1369
1370         if (get_ldev(mdev)) {
1371                 enum drbd_fencing_p fp = mdev->ldev->dc.fencing;
1372                 put_ldev(mdev);
1373                 if (new_conf->wire_protocol == DRBD_PROT_A && fp == FP_STONITH) {
1374                         retcode = ERR_STONITH_AND_PROT_A;
1375                         goto fail;
1376                 }
1377         }
1378
1379         if (mdev->state.role == R_PRIMARY && new_conf->want_lose) {
1380                 retcode = ERR_DISCARD;
1381                 goto fail;
1382         }
1383
1384         retcode = NO_ERROR;
1385
1386         new_my_addr = (struct sockaddr *)&new_conf->my_addr;
1387         new_peer_addr = (struct sockaddr *)&new_conf->peer_addr;
1388         for (i = 0; i < minor_count; i++) {
1389                 odev = minor_to_mdev(i);
1390                 if (!odev || odev == mdev)
1391                         continue;
1392                 if (get_net_conf(odev)) {
1393                         taken_addr = (struct sockaddr *)&odev->net_conf->my_addr;
1394                         if (new_conf->my_addr_len == odev->net_conf->my_addr_len &&
1395                             !memcmp(new_my_addr, taken_addr, new_conf->my_addr_len))
1396                                 retcode = ERR_LOCAL_ADDR;
1397
1398                         taken_addr = (struct sockaddr *)&odev->net_conf->peer_addr;
1399                         if (new_conf->peer_addr_len == odev->net_conf->peer_addr_len &&
1400                             !memcmp(new_peer_addr, taken_addr, new_conf->peer_addr_len))
1401                                 retcode = ERR_PEER_ADDR;
1402
1403                         put_net_conf(odev);
1404                         if (retcode != NO_ERROR)
1405                                 goto fail;
1406                 }
1407         }
1408
1409         if (new_conf->cram_hmac_alg[0] != 0) {
1410                 snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)",
1411                         new_conf->cram_hmac_alg);
1412                 tfm = crypto_alloc_hash(hmac_name, 0, CRYPTO_ALG_ASYNC);
1413                 if (IS_ERR(tfm)) {
1414                         tfm = NULL;
1415                         retcode = ERR_AUTH_ALG;
1416                         goto fail;
1417                 }
1418
1419                 if (!drbd_crypto_is_hash(crypto_hash_tfm(tfm))) {
1420                         retcode = ERR_AUTH_ALG_ND;
1421                         goto fail;
1422                 }
1423         }
1424
1425         if (new_conf->integrity_alg[0]) {
1426                 integrity_w_tfm = crypto_alloc_hash(new_conf->integrity_alg, 0, CRYPTO_ALG_ASYNC);
1427                 if (IS_ERR(integrity_w_tfm)) {
1428                         integrity_w_tfm = NULL;
1429                         retcode=ERR_INTEGRITY_ALG;
1430                         goto fail;
1431                 }
1432
1433                 if (!drbd_crypto_is_hash(crypto_hash_tfm(integrity_w_tfm))) {
1434                         retcode=ERR_INTEGRITY_ALG_ND;
1435                         goto fail;
1436                 }
1437
1438                 integrity_r_tfm = crypto_alloc_hash(new_conf->integrity_alg, 0, CRYPTO_ALG_ASYNC);
1439                 if (IS_ERR(integrity_r_tfm)) {
1440                         integrity_r_tfm = NULL;
1441                         retcode=ERR_INTEGRITY_ALG;
1442                         goto fail;
1443                 }
1444         }
1445
1446         ns = new_conf->max_epoch_size/8;
1447         if (mdev->tl_hash_s != ns) {
1448                 new_tl_hash = kzalloc(ns*sizeof(void *), GFP_KERNEL);
1449                 if (!new_tl_hash) {
1450                         retcode = ERR_NOMEM;
1451                         goto fail;
1452                 }
1453         }
1454
1455         ns = new_conf->max_buffers/8;
1456         if (new_conf->two_primaries && (mdev->ee_hash_s != ns)) {
1457                 new_ee_hash = kzalloc(ns*sizeof(void *), GFP_KERNEL);
1458                 if (!new_ee_hash) {
1459                         retcode = ERR_NOMEM;
1460                         goto fail;
1461                 }
1462         }
1463
1464         ((char *)new_conf->shared_secret)[SHARED_SECRET_MAX-1] = 0;
1465
1466         if (integrity_w_tfm) {
1467                 i = crypto_hash_digestsize(integrity_w_tfm);
1468                 int_dig_out = kmalloc(i, GFP_KERNEL);
1469                 if (!int_dig_out) {
1470                         retcode = ERR_NOMEM;
1471                         goto fail;
1472                 }
1473                 int_dig_in = kmalloc(i, GFP_KERNEL);
1474                 if (!int_dig_in) {
1475                         retcode = ERR_NOMEM;
1476                         goto fail;
1477                 }
1478                 int_dig_vv = kmalloc(i, GFP_KERNEL);
1479                 if (!int_dig_vv) {
1480                         retcode = ERR_NOMEM;
1481                         goto fail;
1482                 }
1483         }
1484
1485         if (!mdev->bitmap) {
1486                 if(drbd_bm_init(mdev)) {
1487                         retcode = ERR_NOMEM;
1488                         goto fail;
1489                 }
1490         }
1491
1492         drbd_flush_workqueue(mdev);
1493         spin_lock_irq(&mdev->req_lock);
1494         if (mdev->net_conf != NULL) {
1495                 retcode = ERR_NET_CONFIGURED;
1496                 spin_unlock_irq(&mdev->req_lock);
1497                 goto fail;
1498         }
1499         mdev->net_conf = new_conf;
1500
1501         mdev->send_cnt = 0;
1502         mdev->recv_cnt = 0;
1503
1504         if (new_tl_hash) {
1505                 kfree(mdev->tl_hash);
1506                 mdev->tl_hash_s = mdev->net_conf->max_epoch_size/8;
1507                 mdev->tl_hash = new_tl_hash;
1508         }
1509
1510         if (new_ee_hash) {
1511                 kfree(mdev->ee_hash);
1512                 mdev->ee_hash_s = mdev->net_conf->max_buffers/8;
1513                 mdev->ee_hash = new_ee_hash;
1514         }
1515
1516         crypto_free_hash(mdev->cram_hmac_tfm);
1517         mdev->cram_hmac_tfm = tfm;
1518
1519         crypto_free_hash(mdev->integrity_w_tfm);
1520         mdev->integrity_w_tfm = integrity_w_tfm;
1521
1522         crypto_free_hash(mdev->integrity_r_tfm);
1523         mdev->integrity_r_tfm = integrity_r_tfm;
1524
1525         kfree(mdev->int_dig_out);
1526         kfree(mdev->int_dig_in);
1527         kfree(mdev->int_dig_vv);
1528         mdev->int_dig_out=int_dig_out;
1529         mdev->int_dig_in=int_dig_in;
1530         mdev->int_dig_vv=int_dig_vv;
1531         retcode = _drbd_set_state(_NS(mdev, conn, C_UNCONNECTED), CS_VERBOSE, NULL);
1532         spin_unlock_irq(&mdev->req_lock);
1533
1534         kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
1535         reply->ret_code = retcode;
1536         drbd_reconfig_done(mdev);
1537         return 0;
1538
1539 fail:
1540         kfree(int_dig_out);
1541         kfree(int_dig_in);
1542         kfree(int_dig_vv);
1543         crypto_free_hash(tfm);
1544         crypto_free_hash(integrity_w_tfm);
1545         crypto_free_hash(integrity_r_tfm);
1546         kfree(new_tl_hash);
1547         kfree(new_ee_hash);
1548         kfree(new_conf);
1549
1550         reply->ret_code = retcode;
1551         drbd_reconfig_done(mdev);
1552         return 0;
1553 }
1554
1555 static int drbd_nl_disconnect(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
1556                               struct drbd_nl_cfg_reply *reply)
1557 {
1558         int retcode;
1559
1560         retcode = _drbd_request_state(mdev, NS(conn, C_DISCONNECTING), CS_ORDERED);
1561
1562         if (retcode == SS_NOTHING_TO_DO)
1563                 goto done;
1564         else if (retcode == SS_ALREADY_STANDALONE)
1565                 goto done;
1566         else if (retcode == SS_PRIMARY_NOP) {
1567                 /* Our statche checking code wants to see the peer outdated. */
1568                 retcode = drbd_request_state(mdev, NS2(conn, C_DISCONNECTING,
1569                                                       pdsk, D_OUTDATED));
1570         } else if (retcode == SS_CW_FAILED_BY_PEER) {
1571                 /* The peer probably wants to see us outdated. */
1572                 retcode = _drbd_request_state(mdev, NS2(conn, C_DISCONNECTING,
1573                                                         disk, D_OUTDATED),
1574                                               CS_ORDERED);
1575                 if (retcode == SS_IS_DISKLESS || retcode == SS_LOWER_THAN_OUTDATED) {
1576                         drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
1577                         retcode = SS_SUCCESS;
1578                 }
1579         }
1580
1581         if (retcode < SS_SUCCESS)
1582                 goto fail;
1583
1584         if (wait_event_interruptible(mdev->state_wait,
1585                                      mdev->state.conn != C_DISCONNECTING)) {
1586                 /* Do not test for mdev->state.conn == C_STANDALONE, since
1587                    someone else might connect us in the mean time! */
1588                 retcode = ERR_INTR;
1589                 goto fail;
1590         }
1591
1592  done:
1593         retcode = NO_ERROR;
1594  fail:
1595         drbd_md_sync(mdev);
1596         reply->ret_code = retcode;
1597         return 0;
1598 }
1599
1600 void resync_after_online_grow(struct drbd_conf *mdev)
1601 {
1602         int iass; /* I am sync source */
1603
1604         dev_info(DEV, "Resync of new storage after online grow\n");
1605         if (mdev->state.role != mdev->state.peer)
1606                 iass = (mdev->state.role == R_PRIMARY);
1607         else
1608                 iass = test_bit(DISCARD_CONCURRENT, &mdev->flags);
1609
1610         if (iass)
1611                 drbd_start_resync(mdev, C_SYNC_SOURCE);
1612         else
1613                 _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE + CS_SERIALIZE);
1614 }
1615
1616 static int drbd_nl_resize(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
1617                           struct drbd_nl_cfg_reply *reply)
1618 {
1619         struct resize rs;
1620         int retcode = NO_ERROR;
1621         enum determine_dev_size dd;
1622         enum dds_flags ddsf;
1623
1624         memset(&rs, 0, sizeof(struct resize));
1625         if (!resize_from_tags(mdev, nlp->tag_list, &rs)) {
1626                 retcode = ERR_MANDATORY_TAG;
1627                 goto fail;
1628         }
1629
1630         if (mdev->state.conn > C_CONNECTED) {
1631                 retcode = ERR_RESIZE_RESYNC;
1632                 goto fail;
1633         }
1634
1635         if (mdev->state.role == R_SECONDARY &&
1636             mdev->state.peer == R_SECONDARY) {
1637                 retcode = ERR_NO_PRIMARY;
1638                 goto fail;
1639         }
1640
1641         if (!get_ldev(mdev)) {
1642                 retcode = ERR_NO_DISK;
1643                 goto fail;
1644         }
1645
1646         if (rs.no_resync && mdev->agreed_pro_version < 93) {
1647                 retcode = ERR_NEED_APV_93;
1648                 goto fail;
1649         }
1650
1651         if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev))
1652                 mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev);
1653
1654         mdev->ldev->dc.disk_size = (sector_t)rs.resize_size;
1655         ddsf = (rs.resize_force ? DDSF_FORCED : 0) | (rs.no_resync ? DDSF_NO_RESYNC : 0);
1656         dd = drbd_determin_dev_size(mdev, ddsf);
1657         drbd_md_sync(mdev);
1658         put_ldev(mdev);
1659         if (dd == dev_size_error) {
1660                 retcode = ERR_NOMEM_BITMAP;
1661                 goto fail;
1662         }
1663
1664         if (mdev->state.conn == C_CONNECTED) {
1665                 if (dd == grew)
1666                         set_bit(RESIZE_PENDING, &mdev->flags);
1667
1668                 drbd_send_uuids(mdev);
1669                 drbd_send_sizes(mdev, 1, ddsf);
1670         }
1671
1672  fail:
1673         reply->ret_code = retcode;
1674         return 0;
1675 }
1676
1677 static int drbd_nl_syncer_conf(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
1678                                struct drbd_nl_cfg_reply *reply)
1679 {
1680         int retcode = NO_ERROR;
1681         int err;
1682         int ovr; /* online verify running */
1683         int rsr; /* re-sync running */
1684         struct crypto_hash *verify_tfm = NULL;
1685         struct crypto_hash *csums_tfm = NULL;
1686         struct syncer_conf sc;
1687         cpumask_var_t new_cpu_mask;
1688         int *rs_plan_s = NULL;
1689         int fifo_size;
1690
1691         if (!zalloc_cpumask_var(&new_cpu_mask, GFP_KERNEL)) {
1692                 retcode = ERR_NOMEM;
1693                 goto fail;
1694         }
1695
1696         if (nlp->flags & DRBD_NL_SET_DEFAULTS) {
1697                 memset(&sc, 0, sizeof(struct syncer_conf));
1698                 sc.rate       = DRBD_RATE_DEF;
1699                 sc.after      = DRBD_AFTER_DEF;
1700                 sc.al_extents = DRBD_AL_EXTENTS_DEF;
1701                 sc.on_no_data  = DRBD_ON_NO_DATA_DEF;
1702                 sc.c_plan_ahead = DRBD_C_PLAN_AHEAD_DEF;
1703                 sc.c_delay_target = DRBD_C_DELAY_TARGET_DEF;
1704                 sc.c_fill_target = DRBD_C_FILL_TARGET_DEF;
1705                 sc.c_max_rate = DRBD_C_MAX_RATE_DEF;
1706                 sc.c_min_rate = DRBD_C_MIN_RATE_DEF;
1707         } else
1708                 memcpy(&sc, &mdev->sync_conf, sizeof(struct syncer_conf));
1709
1710         if (!syncer_conf_from_tags(mdev, nlp->tag_list, &sc)) {
1711                 retcode = ERR_MANDATORY_TAG;
1712                 goto fail;
1713         }
1714
1715         /* re-sync running */
1716         rsr = ( mdev->state.conn == C_SYNC_SOURCE ||
1717                 mdev->state.conn == C_SYNC_TARGET ||
1718                 mdev->state.conn == C_PAUSED_SYNC_S ||
1719                 mdev->state.conn == C_PAUSED_SYNC_T );
1720
1721         if (rsr && strcmp(sc.csums_alg, mdev->sync_conf.csums_alg)) {
1722                 retcode = ERR_CSUMS_RESYNC_RUNNING;
1723                 goto fail;
1724         }
1725
1726         if (!rsr && sc.csums_alg[0]) {
1727                 csums_tfm = crypto_alloc_hash(sc.csums_alg, 0, CRYPTO_ALG_ASYNC);
1728                 if (IS_ERR(csums_tfm)) {
1729                         csums_tfm = NULL;
1730                         retcode = ERR_CSUMS_ALG;
1731                         goto fail;
1732                 }
1733
1734                 if (!drbd_crypto_is_hash(crypto_hash_tfm(csums_tfm))) {
1735                         retcode = ERR_CSUMS_ALG_ND;
1736                         goto fail;
1737                 }
1738         }
1739
1740         /* online verify running */
1741         ovr = (mdev->state.conn == C_VERIFY_S || mdev->state.conn == C_VERIFY_T);
1742
1743         if (ovr) {
1744                 if (strcmp(sc.verify_alg, mdev->sync_conf.verify_alg)) {
1745                         retcode = ERR_VERIFY_RUNNING;
1746                         goto fail;
1747                 }
1748         }
1749
1750         if (!ovr && sc.verify_alg[0]) {
1751                 verify_tfm = crypto_alloc_hash(sc.verify_alg, 0, CRYPTO_ALG_ASYNC);
1752                 if (IS_ERR(verify_tfm)) {
1753                         verify_tfm = NULL;
1754                         retcode = ERR_VERIFY_ALG;
1755                         goto fail;
1756                 }
1757
1758                 if (!drbd_crypto_is_hash(crypto_hash_tfm(verify_tfm))) {
1759                         retcode = ERR_VERIFY_ALG_ND;
1760                         goto fail;
1761                 }
1762         }
1763
1764         /* silently ignore cpu mask on UP kernel */
1765         if (nr_cpu_ids > 1 && sc.cpu_mask[0] != 0) {
1766                 err = __bitmap_parse(sc.cpu_mask, 32, 0,
1767                                 cpumask_bits(new_cpu_mask), nr_cpu_ids);
1768                 if (err) {
1769                         dev_warn(DEV, "__bitmap_parse() failed with %d\n", err);
1770                         retcode = ERR_CPU_MASK_PARSE;
1771                         goto fail;
1772                 }
1773         }
1774
1775         ERR_IF (sc.rate < 1) sc.rate = 1;
1776         ERR_IF (sc.al_extents < 7) sc.al_extents = 127; /* arbitrary minimum */
1777 #define AL_MAX ((MD_AL_MAX_SIZE-1) * AL_EXTENTS_PT)
1778         if (sc.al_extents > AL_MAX) {
1779                 dev_err(DEV, "sc.al_extents > %d\n", AL_MAX);
1780                 sc.al_extents = AL_MAX;
1781         }
1782 #undef AL_MAX
1783
1784         /* to avoid spurious errors when configuring minors before configuring
1785          * the minors they depend on: if necessary, first create the minor we
1786          * depend on */
1787         if (sc.after >= 0)
1788                 ensure_mdev(sc.after, 1);
1789
1790         /* most sanity checks done, try to assign the new sync-after
1791          * dependency.  need to hold the global lock in there,
1792          * to avoid a race in the dependency loop check. */
1793         retcode = drbd_alter_sa(mdev, sc.after);
1794         if (retcode != NO_ERROR)
1795                 goto fail;
1796
1797         fifo_size = (sc.c_plan_ahead * 10 * SLEEP_TIME) / HZ;
1798         if (fifo_size != mdev->rs_plan_s.size && fifo_size > 0) {
1799                 rs_plan_s   = kzalloc(sizeof(int) * fifo_size, GFP_KERNEL);
1800                 if (!rs_plan_s) {
1801                         dev_err(DEV, "kmalloc of fifo_buffer failed");
1802                         retcode = ERR_NOMEM;
1803                         goto fail;
1804                 }
1805         }
1806
1807         /* ok, assign the rest of it as well.
1808          * lock against receive_SyncParam() */
1809         spin_lock(&mdev->peer_seq_lock);
1810         mdev->sync_conf = sc;
1811
1812         if (!rsr) {
1813                 crypto_free_hash(mdev->csums_tfm);
1814                 mdev->csums_tfm = csums_tfm;
1815                 csums_tfm = NULL;
1816         }
1817
1818         if (!ovr) {
1819                 crypto_free_hash(mdev->verify_tfm);
1820                 mdev->verify_tfm = verify_tfm;
1821                 verify_tfm = NULL;
1822         }
1823
1824         if (fifo_size != mdev->rs_plan_s.size) {
1825                 kfree(mdev->rs_plan_s.values);
1826                 mdev->rs_plan_s.values = rs_plan_s;
1827                 mdev->rs_plan_s.size   = fifo_size;
1828                 mdev->rs_planed = 0;
1829                 rs_plan_s = NULL;
1830         }
1831
1832         spin_unlock(&mdev->peer_seq_lock);
1833
1834         if (get_ldev(mdev)) {
1835                 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
1836                 drbd_al_shrink(mdev);
1837                 err = drbd_check_al_size(mdev);
1838                 lc_unlock(mdev->act_log);
1839                 wake_up(&mdev->al_wait);
1840
1841                 put_ldev(mdev);
1842                 drbd_md_sync(mdev);
1843
1844                 if (err) {
1845                         retcode = ERR_NOMEM;
1846                         goto fail;
1847                 }
1848         }
1849
1850         if (mdev->state.conn >= C_CONNECTED)
1851                 drbd_send_sync_param(mdev, &sc);
1852
1853         if (!cpumask_equal(mdev->cpu_mask, new_cpu_mask)) {
1854                 cpumask_copy(mdev->cpu_mask, new_cpu_mask);
1855                 drbd_calc_cpu_mask(mdev);
1856                 mdev->receiver.reset_cpu_mask = 1;
1857                 mdev->asender.reset_cpu_mask = 1;
1858                 mdev->worker.reset_cpu_mask = 1;
1859         }
1860
1861         kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
1862 fail:
1863         kfree(rs_plan_s);
1864         free_cpumask_var(new_cpu_mask);
1865         crypto_free_hash(csums_tfm);
1866         crypto_free_hash(verify_tfm);
1867         reply->ret_code = retcode;
1868         return 0;
1869 }
1870
1871 static int drbd_nl_invalidate(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
1872                               struct drbd_nl_cfg_reply *reply)
1873 {
1874         int retcode;
1875
1876         retcode = _drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T), CS_ORDERED);
1877
1878         if (retcode < SS_SUCCESS && retcode != SS_NEED_CONNECTION)
1879                 retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T));
1880
1881         while (retcode == SS_NEED_CONNECTION) {
1882                 spin_lock_irq(&mdev->req_lock);
1883                 if (mdev->state.conn < C_CONNECTED)
1884                         retcode = _drbd_set_state(_NS(mdev, disk, D_INCONSISTENT), CS_VERBOSE, NULL);
1885                 spin_unlock_irq(&mdev->req_lock);
1886
1887                 if (retcode != SS_NEED_CONNECTION)
1888                         break;
1889
1890                 retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_T));
1891         }
1892
1893         reply->ret_code = retcode;
1894         return 0;
1895 }
1896
1897 static int drbd_bmio_set_susp_al(struct drbd_conf *mdev)
1898 {
1899         int rv;
1900
1901         rv = drbd_bmio_set_n_write(mdev);
1902         drbd_suspend_al(mdev);
1903         return rv;
1904 }
1905
1906 static int drbd_nl_invalidate_peer(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
1907                                    struct drbd_nl_cfg_reply *reply)
1908 {
1909         int retcode;
1910
1911         retcode = _drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_S), CS_ORDERED);
1912
1913         if (retcode < SS_SUCCESS) {
1914                 if (retcode == SS_NEED_CONNECTION && mdev->state.role == R_PRIMARY) {
1915                         /* The peer will get a resync upon connect anyways. Just make that
1916                            into a full resync. */
1917                         retcode = drbd_request_state(mdev, NS(pdsk, D_INCONSISTENT));
1918                         if (retcode >= SS_SUCCESS) {
1919                                 /* open coded drbd_bitmap_io() */
1920                                 if (drbd_bitmap_io(mdev, &drbd_bmio_set_susp_al,
1921                                                    "set_n_write from invalidate_peer"))
1922                                         retcode = ERR_IO_MD_DISK;
1923                         }
1924                 } else
1925                         retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_S));
1926         }
1927
1928         reply->ret_code = retcode;
1929         return 0;
1930 }
1931
1932 static int drbd_nl_pause_sync(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
1933                               struct drbd_nl_cfg_reply *reply)
1934 {
1935         int retcode = NO_ERROR;
1936
1937         if (drbd_request_state(mdev, NS(user_isp, 1)) == SS_NOTHING_TO_DO)
1938                 retcode = ERR_PAUSE_IS_SET;
1939
1940         reply->ret_code = retcode;
1941         return 0;
1942 }
1943
1944 static int drbd_nl_resume_sync(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
1945                                struct drbd_nl_cfg_reply *reply)
1946 {
1947         int retcode = NO_ERROR;
1948
1949         if (drbd_request_state(mdev, NS(user_isp, 0)) == SS_NOTHING_TO_DO)
1950                 retcode = ERR_PAUSE_IS_CLEAR;
1951
1952         reply->ret_code = retcode;
1953         return 0;
1954 }
1955
1956 static int drbd_nl_suspend_io(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
1957                               struct drbd_nl_cfg_reply *reply)
1958 {
1959         reply->ret_code = drbd_request_state(mdev, NS(susp, 1));
1960
1961         return 0;
1962 }
1963
1964 static int drbd_nl_resume_io(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
1965                              struct drbd_nl_cfg_reply *reply)
1966 {
1967         if (test_bit(NEW_CUR_UUID, &mdev->flags)) {
1968                 drbd_uuid_new_current(mdev);
1969                 clear_bit(NEW_CUR_UUID, &mdev->flags);
1970         }
1971         drbd_suspend_io(mdev);
1972         reply->ret_code = drbd_request_state(mdev, NS3(susp, 0, susp_nod, 0, susp_fen, 0));
1973         if (reply->ret_code == SS_SUCCESS) {
1974                 if (mdev->state.conn < C_CONNECTED)
1975                         tl_clear(mdev);
1976                 if (mdev->state.disk == D_DISKLESS || mdev->state.disk == D_FAILED)
1977                         tl_restart(mdev, fail_frozen_disk_io);
1978         }
1979         drbd_resume_io(mdev);
1980
1981         return 0;
1982 }
1983
1984 static int drbd_nl_outdate(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
1985                            struct drbd_nl_cfg_reply *reply)
1986 {
1987         reply->ret_code = drbd_request_state(mdev, NS(disk, D_OUTDATED));
1988         return 0;
1989 }
1990
1991 static int drbd_nl_get_config(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
1992                            struct drbd_nl_cfg_reply *reply)
1993 {
1994         unsigned short *tl;
1995
1996         tl = reply->tag_list;
1997
1998         if (get_ldev(mdev)) {
1999                 tl = disk_conf_to_tags(mdev, &mdev->ldev->dc, tl);
2000                 put_ldev(mdev);
2001         }
2002
2003         if (get_net_conf(mdev)) {
2004                 tl = net_conf_to_tags(mdev, mdev->net_conf, tl);
2005                 put_net_conf(mdev);
2006         }
2007         tl = syncer_conf_to_tags(mdev, &mdev->sync_conf, tl);
2008
2009         put_unaligned(TT_END, tl++); /* Close the tag list */
2010
2011         return (int)((char *)tl - (char *)reply->tag_list);
2012 }
2013
2014 static int drbd_nl_get_state(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
2015                              struct drbd_nl_cfg_reply *reply)
2016 {
2017         unsigned short *tl = reply->tag_list;
2018         union drbd_state s = mdev->state;
2019         unsigned long rs_left;
2020         unsigned int res;
2021
2022         tl = get_state_to_tags(mdev, (struct get_state *)&s, tl);
2023
2024         /* no local ref, no bitmap, no syncer progress. */
2025         if (s.conn >= C_SYNC_SOURCE && s.conn <= C_PAUSED_SYNC_T) {
2026                 if (get_ldev(mdev)) {
2027                         drbd_get_syncer_progress(mdev, &rs_left, &res);
2028                         tl = tl_add_int(tl, T_sync_progress, &res);
2029                         put_ldev(mdev);
2030                 }
2031         }
2032         put_unaligned(TT_END, tl++); /* Close the tag list */
2033
2034         return (int)((char *)tl - (char *)reply->tag_list);
2035 }
2036
2037 static int drbd_nl_get_uuids(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
2038                              struct drbd_nl_cfg_reply *reply)
2039 {
2040         unsigned short *tl;
2041
2042         tl = reply->tag_list;
2043
2044         if (get_ldev(mdev)) {
2045                 tl = tl_add_blob(tl, T_uuids, mdev->ldev->md.uuid, UI_SIZE*sizeof(u64));
2046                 tl = tl_add_int(tl, T_uuids_flags, &mdev->ldev->md.flags);
2047                 put_ldev(mdev);
2048         }
2049         put_unaligned(TT_END, tl++); /* Close the tag list */
2050
2051         return (int)((char *)tl - (char *)reply->tag_list);
2052 }
2053
2054 /**
2055  * drbd_nl_get_timeout_flag() - Used by drbdsetup to find out which timeout value to use
2056  * @mdev:       DRBD device.
2057  * @nlp:        Netlink/connector packet from drbdsetup
2058  * @reply:      Reply packet for drbdsetup
2059  */
2060 static int drbd_nl_get_timeout_flag(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
2061                                     struct drbd_nl_cfg_reply *reply)
2062 {
2063         unsigned short *tl;
2064         char rv;
2065
2066         tl = reply->tag_list;
2067
2068         rv = mdev->state.pdsk == D_OUTDATED        ? UT_PEER_OUTDATED :
2069           test_bit(USE_DEGR_WFC_T, &mdev->flags) ? UT_DEGRADED : UT_DEFAULT;
2070
2071         tl = tl_add_blob(tl, T_use_degraded, &rv, sizeof(rv));
2072         put_unaligned(TT_END, tl++); /* Close the tag list */
2073
2074         return (int)((char *)tl - (char *)reply->tag_list);
2075 }
2076
2077 static int drbd_nl_start_ov(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
2078                                     struct drbd_nl_cfg_reply *reply)
2079 {
2080         /* default to resume from last known position, if possible */
2081         struct start_ov args =
2082                 { .start_sector = mdev->ov_start_sector };
2083
2084         if (!start_ov_from_tags(mdev, nlp->tag_list, &args)) {
2085                 reply->ret_code = ERR_MANDATORY_TAG;
2086                 return 0;
2087         }
2088         /* w_make_ov_request expects position to be aligned */
2089         mdev->ov_start_sector = args.start_sector & ~BM_SECT_PER_BIT;
2090         reply->ret_code = drbd_request_state(mdev,NS(conn,C_VERIFY_S));
2091         return 0;
2092 }
2093
2094
2095 static int drbd_nl_new_c_uuid(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp,
2096                               struct drbd_nl_cfg_reply *reply)
2097 {
2098         int retcode = NO_ERROR;
2099         int skip_initial_sync = 0;
2100         int err;
2101
2102         struct new_c_uuid args;
2103
2104         memset(&args, 0, sizeof(struct new_c_uuid));
2105         if (!new_c_uuid_from_tags(mdev, nlp->tag_list, &args)) {
2106                 reply->ret_code = ERR_MANDATORY_TAG;
2107                 return 0;
2108         }
2109
2110         mutex_lock(&mdev->state_mutex); /* Protects us against serialized state changes. */
2111
2112         if (!get_ldev(mdev)) {
2113                 retcode = ERR_NO_DISK;
2114                 goto out;
2115         }
2116
2117         /* this is "skip initial sync", assume to be clean */
2118         if (mdev->state.conn == C_CONNECTED && mdev->agreed_pro_version >= 90 &&
2119             mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED && args.clear_bm) {
2120                 dev_info(DEV, "Preparing to skip initial sync\n");
2121                 skip_initial_sync = 1;
2122         } else if (mdev->state.conn != C_STANDALONE) {
2123                 retcode = ERR_CONNECTED;
2124                 goto out_dec;
2125         }
2126
2127         drbd_uuid_set(mdev, UI_BITMAP, 0); /* Rotate UI_BITMAP to History 1, etc... */
2128         drbd_uuid_new_current(mdev); /* New current, previous to UI_BITMAP */
2129
2130         if (args.clear_bm) {
2131                 err = drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write, "clear_n_write from new_c_uuid");
2132                 if (err) {
2133                         dev_err(DEV, "Writing bitmap failed with %d\n",err);
2134                         retcode = ERR_IO_MD_DISK;
2135                 }
2136                 if (skip_initial_sync) {
2137                         drbd_send_uuids_skip_initial_sync(mdev);
2138                         _drbd_uuid_set(mdev, UI_BITMAP, 0);
2139                         spin_lock_irq(&mdev->req_lock);
2140                         _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
2141                                         CS_VERBOSE, NULL);
2142                         spin_unlock_irq(&mdev->req_lock);
2143                 }
2144         }
2145
2146         drbd_md_sync(mdev);
2147 out_dec:
2148         put_ldev(mdev);
2149 out:
2150         mutex_unlock(&mdev->state_mutex);
2151
2152         reply->ret_code = retcode;
2153         return 0;
2154 }
2155
2156 struct cn_handler_struct {
2157         int (*function)(struct drbd_conf *,
2158                          struct drbd_nl_cfg_req *,
2159                          struct drbd_nl_cfg_reply *);
2160         int reply_body_size;
2161 };
2162
2163 static struct cn_handler_struct cnd_table[] = {
2164         [ P_primary ]           = { &drbd_nl_primary,           0 },
2165         [ P_secondary ]         = { &drbd_nl_secondary,         0 },
2166         [ P_disk_conf ]         = { &drbd_nl_disk_conf,         0 },
2167         [ P_detach ]            = { &drbd_nl_detach,            0 },
2168         [ P_net_conf ]          = { &drbd_nl_net_conf,          0 },
2169         [ P_disconnect ]        = { &drbd_nl_disconnect,        0 },
2170         [ P_resize ]            = { &drbd_nl_resize,            0 },
2171         [ P_syncer_conf ]       = { &drbd_nl_syncer_conf,       0 },
2172         [ P_invalidate ]        = { &drbd_nl_invalidate,        0 },
2173         [ P_invalidate_peer ]   = { &drbd_nl_invalidate_peer,   0 },
2174         [ P_pause_sync ]        = { &drbd_nl_pause_sync,        0 },
2175         [ P_resume_sync ]       = { &drbd_nl_resume_sync,       0 },
2176         [ P_suspend_io ]        = { &drbd_nl_suspend_io,        0 },
2177         [ P_resume_io ]         = { &drbd_nl_resume_io,         0 },
2178         [ P_outdate ]           = { &drbd_nl_outdate,           0 },
2179         [ P_get_config ]        = { &drbd_nl_get_config,
2180                                     sizeof(struct syncer_conf_tag_len_struct) +
2181                                     sizeof(struct disk_conf_tag_len_struct) +
2182                                     sizeof(struct net_conf_tag_len_struct) },
2183         [ P_get_state ]         = { &drbd_nl_get_state,
2184                                     sizeof(struct get_state_tag_len_struct) +
2185                                     sizeof(struct sync_progress_tag_len_struct) },
2186         [ P_get_uuids ]         = { &drbd_nl_get_uuids,
2187                                     sizeof(struct get_uuids_tag_len_struct) },
2188         [ P_get_timeout_flag ]  = { &drbd_nl_get_timeout_flag,
2189                                     sizeof(struct get_timeout_flag_tag_len_struct)},
2190         [ P_start_ov ]          = { &drbd_nl_start_ov,          0 },
2191         [ P_new_c_uuid ]        = { &drbd_nl_new_c_uuid,        0 },
2192 };
2193
2194 static void drbd_connector_callback(struct cn_msg *req, struct netlink_skb_parms *nsp)
2195 {
2196         struct drbd_nl_cfg_req *nlp = (struct drbd_nl_cfg_req *)req->data;
2197         struct cn_handler_struct *cm;
2198         struct cn_msg *cn_reply;
2199         struct drbd_nl_cfg_reply *reply;
2200         struct drbd_conf *mdev;
2201         int retcode, rr;
2202         int reply_size = sizeof(struct cn_msg)
2203                 + sizeof(struct drbd_nl_cfg_reply)
2204                 + sizeof(short int);
2205
2206         if (!try_module_get(THIS_MODULE)) {
2207                 printk(KERN_ERR "drbd: try_module_get() failed!\n");
2208                 return;
2209         }
2210
2211         if (!cap_raised(nsp->eff_cap, CAP_SYS_ADMIN)) {
2212                 retcode = ERR_PERM;
2213                 goto fail;
2214         }
2215
2216         mdev = ensure_mdev(nlp->drbd_minor,
2217                         (nlp->flags & DRBD_NL_CREATE_DEVICE));
2218         if (!mdev) {
2219                 retcode = ERR_MINOR_INVALID;
2220                 goto fail;
2221         }
2222
2223         if (nlp->packet_type >= P_nl_after_last_packet) {
2224                 retcode = ERR_PACKET_NR;
2225                 goto fail;
2226         }
2227
2228         cm = cnd_table + nlp->packet_type;
2229
2230         /* This may happen if packet number is 0: */
2231         if (cm->function == NULL) {
2232                 retcode = ERR_PACKET_NR;
2233                 goto fail;
2234         }
2235
2236         reply_size += cm->reply_body_size;
2237
2238         /* allocation not in the IO path, cqueue thread context */
2239         cn_reply = kmalloc(reply_size, GFP_KERNEL);
2240         if (!cn_reply) {
2241                 retcode = ERR_NOMEM;
2242                 goto fail;
2243         }
2244         reply = (struct drbd_nl_cfg_reply *) cn_reply->data;
2245
2246         reply->packet_type =
2247                 cm->reply_body_size ? nlp->packet_type : P_nl_after_last_packet;
2248         reply->minor = nlp->drbd_minor;
2249         reply->ret_code = NO_ERROR; /* Might by modified by cm->function. */
2250         /* reply->tag_list; might be modified by cm->function. */
2251
2252         rr = cm->function(mdev, nlp, reply);
2253
2254         cn_reply->id = req->id;
2255         cn_reply->seq = req->seq;
2256         cn_reply->ack = req->ack  + 1;
2257         cn_reply->len = sizeof(struct drbd_nl_cfg_reply) + rr;
2258         cn_reply->flags = 0;
2259
2260         rr = cn_netlink_send(cn_reply, CN_IDX_DRBD, GFP_KERNEL);
2261         if (rr && rr != -ESRCH)
2262                 printk(KERN_INFO "drbd: cn_netlink_send()=%d\n", rr);
2263
2264         kfree(cn_reply);
2265         module_put(THIS_MODULE);
2266         return;
2267  fail:
2268         drbd_nl_send_reply(req, retcode);
2269         module_put(THIS_MODULE);
2270 }
2271
2272 static atomic_t drbd_nl_seq = ATOMIC_INIT(2); /* two. */
2273
2274 static unsigned short *
2275 __tl_add_blob(unsigned short *tl, enum drbd_tags tag, const void *data,
2276         unsigned short len, int nul_terminated)
2277 {
2278         unsigned short l = tag_descriptions[tag_number(tag)].max_len;
2279         len = (len < l) ? len :  l;
2280         put_unaligned(tag, tl++);
2281         put_unaligned(len, tl++);
2282         memcpy(tl, data, len);
2283         tl = (unsigned short*)((char*)tl + len);
2284         if (nul_terminated)
2285                 *((char*)tl - 1) = 0;
2286         return tl;
2287 }
2288
2289 static unsigned short *
2290 tl_add_blob(unsigned short *tl, enum drbd_tags tag, const void *data, int len)
2291 {
2292         return __tl_add_blob(tl, tag, data, len, 0);
2293 }
2294
2295 static unsigned short *
2296 tl_add_str(unsigned short *tl, enum drbd_tags tag, const char *str)
2297 {
2298         return __tl_add_blob(tl, tag, str, strlen(str)+1, 0);
2299 }
2300
2301 static unsigned short *
2302 tl_add_int(unsigned short *tl, enum drbd_tags tag, const void *val)
2303 {
2304         put_unaligned(tag, tl++);
2305         switch(tag_type(tag)) {
2306         case TT_INTEGER:
2307                 put_unaligned(sizeof(int), tl++);
2308                 put_unaligned(*(int *)val, (int *)tl);
2309                 tl = (unsigned short*)((char*)tl+sizeof(int));
2310                 break;
2311         case TT_INT64:
2312                 put_unaligned(sizeof(u64), tl++);
2313                 put_unaligned(*(u64 *)val, (u64 *)tl);
2314                 tl = (unsigned short*)((char*)tl+sizeof(u64));
2315                 break;
2316         default:
2317                 /* someone did something stupid. */
2318                 ;
2319         }
2320         return tl;
2321 }
2322
2323 void drbd_bcast_state(struct drbd_conf *mdev, union drbd_state state)
2324 {
2325         char buffer[sizeof(struct cn_msg)+
2326                     sizeof(struct drbd_nl_cfg_reply)+
2327                     sizeof(struct get_state_tag_len_struct)+
2328                     sizeof(short int)];
2329         struct cn_msg *cn_reply = (struct cn_msg *) buffer;
2330         struct drbd_nl_cfg_reply *reply =
2331                 (struct drbd_nl_cfg_reply *)cn_reply->data;
2332         unsigned short *tl = reply->tag_list;
2333
2334         /* dev_warn(DEV, "drbd_bcast_state() got called\n"); */
2335
2336         tl = get_state_to_tags(mdev, (struct get_state *)&state, tl);
2337
2338         put_unaligned(TT_END, tl++); /* Close the tag list */
2339
2340         cn_reply->id.idx = CN_IDX_DRBD;
2341         cn_reply->id.val = CN_VAL_DRBD;
2342
2343         cn_reply->seq = atomic_add_return(1, &drbd_nl_seq);
2344         cn_reply->ack = 0; /* not used here. */
2345         cn_reply->len = sizeof(struct drbd_nl_cfg_reply) +
2346                 (int)((char *)tl - (char *)reply->tag_list);
2347         cn_reply->flags = 0;
2348
2349         reply->packet_type = P_get_state;
2350         reply->minor = mdev_to_minor(mdev);
2351         reply->ret_code = NO_ERROR;
2352
2353         cn_netlink_send(cn_reply, CN_IDX_DRBD, GFP_NOIO);
2354 }
2355
2356 void drbd_bcast_ev_helper(struct drbd_conf *mdev, char *helper_name)
2357 {
2358         char buffer[sizeof(struct cn_msg)+
2359                     sizeof(struct drbd_nl_cfg_reply)+
2360                     sizeof(struct call_helper_tag_len_struct)+
2361                     sizeof(short int)];
2362         struct cn_msg *cn_reply = (struct cn_msg *) buffer;
2363         struct drbd_nl_cfg_reply *reply =
2364                 (struct drbd_nl_cfg_reply *)cn_reply->data;
2365         unsigned short *tl = reply->tag_list;
2366
2367         /* dev_warn(DEV, "drbd_bcast_state() got called\n"); */
2368
2369         tl = tl_add_str(tl, T_helper, helper_name);
2370         put_unaligned(TT_END, tl++); /* Close the tag list */
2371
2372         cn_reply->id.idx = CN_IDX_DRBD;
2373         cn_reply->id.val = CN_VAL_DRBD;
2374
2375         cn_reply->seq = atomic_add_return(1, &drbd_nl_seq);
2376         cn_reply->ack = 0; /* not used here. */
2377         cn_reply->len = sizeof(struct drbd_nl_cfg_reply) +
2378                 (int)((char *)tl - (char *)reply->tag_list);
2379         cn_reply->flags = 0;
2380
2381         reply->packet_type = P_call_helper;
2382         reply->minor = mdev_to_minor(mdev);
2383         reply->ret_code = NO_ERROR;
2384
2385         cn_netlink_send(cn_reply, CN_IDX_DRBD, GFP_NOIO);
2386 }
2387
2388 void drbd_bcast_ee(struct drbd_conf *mdev,
2389                 const char *reason, const int dgs,
2390                 const char* seen_hash, const char* calc_hash,
2391                 const struct drbd_epoch_entry* e)
2392 {
2393         struct cn_msg *cn_reply;
2394         struct drbd_nl_cfg_reply *reply;
2395         unsigned short *tl;
2396         struct page *page;
2397         unsigned len;
2398
2399         if (!e)
2400                 return;
2401         if (!reason || !reason[0])
2402                 return;
2403
2404         /* apparently we have to memcpy twice, first to prepare the data for the
2405          * struct cn_msg, then within cn_netlink_send from the cn_msg to the
2406          * netlink skb. */
2407         /* receiver thread context, which is not in the writeout path (of this node),
2408          * but may be in the writeout path of the _other_ node.
2409          * GFP_NOIO to avoid potential "distributed deadlock". */
2410         cn_reply = kmalloc(
2411                 sizeof(struct cn_msg)+
2412                 sizeof(struct drbd_nl_cfg_reply)+
2413                 sizeof(struct dump_ee_tag_len_struct)+
2414                 sizeof(short int),
2415                 GFP_NOIO);
2416
2417         if (!cn_reply) {
2418                 dev_err(DEV, "could not kmalloc buffer for drbd_bcast_ee, sector %llu, size %u\n",
2419                                 (unsigned long long)e->sector, e->size);
2420                 return;
2421         }
2422
2423         reply = (struct drbd_nl_cfg_reply*)cn_reply->data;
2424         tl = reply->tag_list;
2425
2426         tl = tl_add_str(tl, T_dump_ee_reason, reason);
2427         tl = tl_add_blob(tl, T_seen_digest, seen_hash, dgs);
2428         tl = tl_add_blob(tl, T_calc_digest, calc_hash, dgs);
2429         tl = tl_add_int(tl, T_ee_sector, &e->sector);
2430         tl = tl_add_int(tl, T_ee_block_id, &e->block_id);
2431
2432         put_unaligned(T_ee_data, tl++);
2433         put_unaligned(e->size, tl++);
2434
2435         len = e->size;
2436         page = e->pages;
2437         page_chain_for_each(page) {
2438                 void *d = kmap_atomic(page, KM_USER0);
2439                 unsigned l = min_t(unsigned, len, PAGE_SIZE);
2440                 memcpy(tl, d, l);
2441                 kunmap_atomic(d, KM_USER0);
2442                 tl = (unsigned short*)((char*)tl + l);
2443                 len -= l;
2444         }
2445         put_unaligned(TT_END, tl++); /* Close the tag list */
2446
2447         cn_reply->id.idx = CN_IDX_DRBD;
2448         cn_reply->id.val = CN_VAL_DRBD;
2449
2450         cn_reply->seq = atomic_add_return(1,&drbd_nl_seq);
2451         cn_reply->ack = 0; // not used here.
2452         cn_reply->len = sizeof(struct drbd_nl_cfg_reply) +
2453                 (int)((char*)tl - (char*)reply->tag_list);
2454         cn_reply->flags = 0;
2455
2456         reply->packet_type = P_dump_ee;
2457         reply->minor = mdev_to_minor(mdev);
2458         reply->ret_code = NO_ERROR;
2459
2460         cn_netlink_send(cn_reply, CN_IDX_DRBD, GFP_NOIO);
2461         kfree(cn_reply);
2462 }
2463
2464 void drbd_bcast_sync_progress(struct drbd_conf *mdev)
2465 {
2466         char buffer[sizeof(struct cn_msg)+
2467                     sizeof(struct drbd_nl_cfg_reply)+
2468                     sizeof(struct sync_progress_tag_len_struct)+
2469                     sizeof(short int)];
2470         struct cn_msg *cn_reply = (struct cn_msg *) buffer;
2471         struct drbd_nl_cfg_reply *reply =
2472                 (struct drbd_nl_cfg_reply *)cn_reply->data;
2473         unsigned short *tl = reply->tag_list;
2474         unsigned long rs_left;
2475         unsigned int res;
2476
2477         /* no local ref, no bitmap, no syncer progress, no broadcast. */
2478         if (!get_ldev(mdev))
2479                 return;
2480         drbd_get_syncer_progress(mdev, &rs_left, &res);
2481         put_ldev(mdev);
2482
2483         tl = tl_add_int(tl, T_sync_progress, &res);
2484         put_unaligned(TT_END, tl++); /* Close the tag list */
2485
2486         cn_reply->id.idx = CN_IDX_DRBD;
2487         cn_reply->id.val = CN_VAL_DRBD;
2488
2489         cn_reply->seq = atomic_add_return(1, &drbd_nl_seq);
2490         cn_reply->ack = 0; /* not used here. */
2491         cn_reply->len = sizeof(struct drbd_nl_cfg_reply) +
2492                 (int)((char *)tl - (char *)reply->tag_list);
2493         cn_reply->flags = 0;
2494
2495         reply->packet_type = P_sync_progress;
2496         reply->minor = mdev_to_minor(mdev);
2497         reply->ret_code = NO_ERROR;
2498
2499         cn_netlink_send(cn_reply, CN_IDX_DRBD, GFP_NOIO);
2500 }
2501
2502 int __init drbd_nl_init(void)
2503 {
2504         static struct cb_id cn_id_drbd;
2505         int err, try=10;
2506
2507         cn_id_drbd.val = CN_VAL_DRBD;
2508         do {
2509                 cn_id_drbd.idx = cn_idx;
2510                 err = cn_add_callback(&cn_id_drbd, "cn_drbd", &drbd_connector_callback);
2511                 if (!err)
2512                         break;
2513                 cn_idx = (cn_idx + CN_IDX_STEP);
2514         } while (try--);
2515
2516         if (err) {
2517                 printk(KERN_ERR "drbd: cn_drbd failed to register\n");
2518                 return err;
2519         }
2520
2521         return 0;
2522 }
2523
2524 void drbd_nl_cleanup(void)
2525 {
2526         static struct cb_id cn_id_drbd;
2527
2528         cn_id_drbd.idx = cn_idx;
2529         cn_id_drbd.val = CN_VAL_DRBD;
2530
2531         cn_del_callback(&cn_id_drbd);
2532 }
2533
2534 void drbd_nl_send_reply(struct cn_msg *req, int ret_code)
2535 {
2536         char buffer[sizeof(struct cn_msg)+sizeof(struct drbd_nl_cfg_reply)];
2537         struct cn_msg *cn_reply = (struct cn_msg *) buffer;
2538         struct drbd_nl_cfg_reply *reply =
2539                 (struct drbd_nl_cfg_reply *)cn_reply->data;
2540         int rr;
2541
2542         cn_reply->id = req->id;
2543
2544         cn_reply->seq = req->seq;
2545         cn_reply->ack = req->ack  + 1;
2546         cn_reply->len = sizeof(struct drbd_nl_cfg_reply);
2547         cn_reply->flags = 0;
2548
2549         reply->minor = ((struct drbd_nl_cfg_req *)req->data)->drbd_minor;
2550         reply->ret_code = ret_code;
2551
2552         rr = cn_netlink_send(cn_reply, CN_IDX_DRBD, GFP_NOIO);
2553         if (rr && rr != -ESRCH)
2554                 printk(KERN_INFO "drbd: cn_netlink_send()=%d\n", rr);
2555 }
2556