drbd: introduce the "initialized" activity log transaction type
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / block / drbd / drbd_actlog.c
1 /*
2    drbd_actlog.c
3
4    This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
5
6    Copyright (C) 2003-2008, LINBIT Information Technologies GmbH.
7    Copyright (C) 2003-2008, Philipp Reisner <philipp.reisner@linbit.com>.
8    Copyright (C) 2003-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/slab.h>
27 #include <linux/crc32c.h>
28 #include <linux/drbd.h>
29 #include <linux/drbd_limits.h>
30 #include <linux/dynamic_debug.h>
31 #include "drbd_int.h"
32 #include "drbd_wrappers.h"
33
34
35 enum al_transaction_types {
36         AL_TR_UPDATE = 0,
37         AL_TR_INITIALIZED = 0xffff
38 };
39 /* all fields on disc in big endian */
40 struct __packed al_transaction_on_disk {
41         /* don't we all like magic */
42         __be32  magic;
43
44         /* to identify the most recent transaction block
45          * in the on disk ring buffer */
46         __be32  tr_number;
47
48         /* checksum on the full 4k block, with this field set to 0. */
49         __be32  crc32c;
50
51         /* type of transaction, special transaction types like:
52          * purge-all, set-all-idle, set-all-active, ... to-be-defined
53          * see also enum al_transaction_types */
54         __be16  transaction_type;
55
56         /* we currently allow only a few thousand extents,
57          * so 16bit will be enough for the slot number. */
58
59         /* how many updates in this transaction */
60         __be16  n_updates;
61
62         /* maximum slot number, "al-extents" in drbd.conf speak.
63          * Having this in each transaction should make reconfiguration
64          * of that parameter easier. */
65         __be16  context_size;
66
67         /* slot number the context starts with */
68         __be16  context_start_slot_nr;
69
70         /* Some reserved bytes.  Expected usage is a 64bit counter of
71          * sectors-written since device creation, and other data generation tag
72          * supporting usage */
73         __be32  __reserved[4];
74
75         /* --- 36 byte used --- */
76
77         /* Reserve space for up to AL_UPDATES_PER_TRANSACTION changes
78          * in one transaction, then use the remaining byte in the 4k block for
79          * context information.  "Flexible" number of updates per transaction
80          * does not help, as we have to account for the case when all update
81          * slots are used anyways, so it would only complicate code without
82          * additional benefit.
83          */
84         __be16  update_slot_nr[AL_UPDATES_PER_TRANSACTION];
85
86         /* but the extent number is 32bit, which at an extent size of 4 MiB
87          * allows to cover device sizes of up to 2**54 Byte (16 PiB) */
88         __be32  update_extent_nr[AL_UPDATES_PER_TRANSACTION];
89
90         /* --- 420 bytes used (36 + 64*6) --- */
91
92         /* 4096 - 420 = 3676 = 919 * 4 */
93         __be32  context[AL_CONTEXT_PER_TRANSACTION];
94 };
95
96 struct update_odbm_work {
97         struct drbd_work w;
98         unsigned int enr;
99 };
100
101 struct update_al_work {
102         struct drbd_work w;
103         struct completion event;
104         int err;
105 };
106
107 struct drbd_atodb_wait {
108         atomic_t           count;
109         struct completion  io_done;
110         struct drbd_conf   *mdev;
111         int                error;
112 };
113
114
115 static int w_al_write_transaction(struct drbd_work *, int);
116
117 static int _drbd_md_sync_page_io(struct drbd_conf *mdev,
118                                  struct drbd_backing_dev *bdev,
119                                  struct page *page, sector_t sector,
120                                  int rw, int size)
121 {
122         struct bio *bio;
123         struct drbd_md_io md_io;
124         int err;
125
126         md_io.mdev = mdev;
127         init_completion(&md_io.event);
128         md_io.error = 0;
129
130         if ((rw & WRITE) && !test_bit(MD_NO_FUA, &mdev->flags))
131                 rw |= REQ_FUA | REQ_FLUSH;
132         rw |= REQ_SYNC;
133
134         bio = bio_alloc_drbd(GFP_NOIO);
135         bio->bi_bdev = bdev->md_bdev;
136         bio->bi_sector = sector;
137         err = -EIO;
138         if (bio_add_page(bio, page, size, 0) != size)
139                 goto out;
140         bio->bi_private = &md_io;
141         bio->bi_end_io = drbd_md_io_complete;
142         bio->bi_rw = rw;
143
144         if (drbd_insert_fault(mdev, (rw & WRITE) ? DRBD_FAULT_MD_WR : DRBD_FAULT_MD_RD))
145                 bio_endio(bio, -EIO);
146         else
147                 submit_bio(rw, bio);
148         wait_for_completion(&md_io.event);
149         if (bio_flagged(bio, BIO_UPTODATE))
150                 err = md_io.error;
151
152  out:
153         bio_put(bio);
154         return err;
155 }
156
157 int drbd_md_sync_page_io(struct drbd_conf *mdev, struct drbd_backing_dev *bdev,
158                          sector_t sector, int rw)
159 {
160         int err;
161         struct page *iop = mdev->md_io_page;
162
163         D_ASSERT(mutex_is_locked(&mdev->md_io_mutex));
164
165         BUG_ON(!bdev->md_bdev);
166
167         dev_dbg(DEV, "meta_data io: %s [%d]:%s(,%llus,%s)\n",
168              current->comm, current->pid, __func__,
169              (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ");
170
171         if (sector < drbd_md_first_sector(bdev) ||
172             sector + 7 > drbd_md_last_sector(bdev))
173                 dev_alert(DEV, "%s [%d]:%s(,%llus,%s) out of range md access!\n",
174                      current->comm, current->pid, __func__,
175                      (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ");
176
177         err = _drbd_md_sync_page_io(mdev, bdev, iop, sector, rw, MD_BLOCK_SIZE);
178         if (err) {
179                 dev_err(DEV, "drbd_md_sync_page_io(,%llus,%s) failed!\n",
180                     (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ");
181         }
182         return err;
183 }
184
185 static struct lc_element *_al_get(struct drbd_conf *mdev, unsigned int enr)
186 {
187         struct lc_element *al_ext;
188         struct lc_element *tmp;
189         int wake;
190
191         spin_lock_irq(&mdev->al_lock);
192         tmp = lc_find(mdev->resync, enr/AL_EXT_PER_BM_SECT);
193         if (unlikely(tmp != NULL)) {
194                 struct bm_extent  *bm_ext = lc_entry(tmp, struct bm_extent, lce);
195                 if (test_bit(BME_NO_WRITES, &bm_ext->flags)) {
196                         wake = !test_and_set_bit(BME_PRIORITY, &bm_ext->flags);
197                         spin_unlock_irq(&mdev->al_lock);
198                         if (wake)
199                                 wake_up(&mdev->al_wait);
200                         return NULL;
201                 }
202         }
203         al_ext = lc_get(mdev->act_log, enr);
204         spin_unlock_irq(&mdev->al_lock);
205         return al_ext;
206 }
207
208 void drbd_al_begin_io(struct drbd_conf *mdev, sector_t sector)
209 {
210         unsigned int enr = (sector >> (AL_EXTENT_SHIFT-9));
211         struct lc_element *al_ext;
212         struct update_al_work al_work;
213
214         D_ASSERT(atomic_read(&mdev->local_cnt) > 0);
215
216         wait_event(mdev->al_wait, (al_ext = _al_get(mdev, enr)));
217
218         if (al_ext->lc_number != enr) {
219                 /* drbd_al_write_transaction(mdev,al_ext,enr);
220                  * recurses into generic_make_request(), which
221                  * disallows recursion, bios being serialized on the
222                  * current->bio_tail list now.
223                  * we have to delegate updates to the activity log
224                  * to the worker thread. */
225
226                 /* Serialize multiple transactions.
227                  * This uses test_and_set_bit, memory barrier is implicit.
228                  * Optimization potential:
229                  * first check for transaction number > old transaction number,
230                  * so not all waiters have to lock/unlock.  */
231                 wait_event(mdev->al_wait, lc_try_lock_for_transaction(mdev->act_log));
232
233                 /* Double check: it may have been committed by someone else,
234                  * while we have been waiting for the lock. */
235                 if (al_ext->lc_number != enr) {
236                         init_completion(&al_work.event);
237                         al_work.w.cb = w_al_write_transaction;
238                         al_work.w.mdev = mdev;
239                         drbd_queue_work_front(&mdev->tconn->data.work, &al_work.w);
240                         wait_for_completion(&al_work.event);
241
242                         mdev->al_writ_cnt++;
243
244                         spin_lock_irq(&mdev->al_lock);
245                         /* FIXME
246                         if (al_work.err)
247                                 we need an "lc_cancel" here;
248                         */
249                         lc_committed(mdev->act_log);
250                         spin_unlock_irq(&mdev->al_lock);
251                 }
252                 lc_unlock(mdev->act_log);
253                 wake_up(&mdev->al_wait);
254         }
255 }
256
257 void drbd_al_complete_io(struct drbd_conf *mdev, sector_t sector)
258 {
259         unsigned int enr = (sector >> (AL_EXTENT_SHIFT-9));
260         struct lc_element *extent;
261         unsigned long flags;
262
263         spin_lock_irqsave(&mdev->al_lock, flags);
264
265         extent = lc_find(mdev->act_log, enr);
266
267         if (!extent) {
268                 spin_unlock_irqrestore(&mdev->al_lock, flags);
269                 dev_err(DEV, "al_complete_io() called on inactive extent %u\n", enr);
270                 return;
271         }
272
273         if (lc_put(mdev->act_log, extent) == 0)
274                 wake_up(&mdev->al_wait);
275
276         spin_unlock_irqrestore(&mdev->al_lock, flags);
277 }
278
279 #if (PAGE_SHIFT + 3) < (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT)
280 /* Currently BM_BLOCK_SHIFT, BM_EXT_SHIFT and AL_EXTENT_SHIFT
281  * are still coupled, or assume too much about their relation.
282  * Code below will not work if this is violated.
283  * Will be cleaned up with some followup patch.
284  */
285 # error FIXME
286 #endif
287
288 static unsigned int al_extent_to_bm_page(unsigned int al_enr)
289 {
290         return al_enr >>
291                 /* bit to page */
292                 ((PAGE_SHIFT + 3) -
293                 /* al extent number to bit */
294                  (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT));
295 }
296
297 static unsigned int rs_extent_to_bm_page(unsigned int rs_enr)
298 {
299         return rs_enr >>
300                 /* bit to page */
301                 ((PAGE_SHIFT + 3) -
302                 /* al extent number to bit */
303                  (BM_EXT_SHIFT - BM_BLOCK_SHIFT));
304 }
305
306 static int
307 w_al_write_transaction(struct drbd_work *w, int unused)
308 {
309         struct update_al_work *aw = container_of(w, struct update_al_work, w);
310         struct drbd_conf *mdev = w->mdev;
311         struct al_transaction_on_disk *buffer;
312         struct lc_element *e;
313         sector_t sector;
314         int i, mx;
315         unsigned extent_nr;
316         unsigned crc = 0;
317
318         if (!get_ldev(mdev)) {
319                 dev_err(DEV, "disk is %s, cannot start al transaction\n",
320                         drbd_disk_str(mdev->state.disk));
321                 aw->err = -EIO;
322                 complete(&((struct update_al_work *)w)->event);
323                 return 0;
324         }
325
326         /* The bitmap write may have failed, causing a state change. */
327         if (mdev->state.disk < D_INCONSISTENT) {
328                 dev_err(DEV,
329                         "disk is %s, cannot write al transaction\n",
330                         drbd_disk_str(mdev->state.disk));
331                 aw->err = -EIO;
332                 complete(&((struct update_al_work *)w)->event);
333                 put_ldev(mdev);
334                 return 0;
335         }
336
337         mutex_lock(&mdev->md_io_mutex); /* protects md_io_buffer, al_tr_cycle, ... */
338         buffer = page_address(mdev->md_io_page);
339
340         memset(buffer, 0, sizeof(*buffer));
341         buffer->magic = cpu_to_be32(DRBD_AL_MAGIC);
342         buffer->tr_number = cpu_to_be32(mdev->al_tr_number);
343
344         i = 0;
345
346         /* Even though no one can start to change this list
347          * once we set the LC_LOCKED -- from drbd_al_begin_io(),
348          * lc_try_lock_for_transaction() --, someone may still
349          * be in the process of changing it. */
350         spin_lock_irq(&mdev->al_lock);
351         list_for_each_entry(e, &mdev->act_log->to_be_changed, list) {
352                 if (i == AL_UPDATES_PER_TRANSACTION) {
353                         i++;
354                         break;
355                 }
356                 buffer->update_slot_nr[i] = cpu_to_be16(e->lc_index);
357                 buffer->update_extent_nr[i] = cpu_to_be32(e->lc_new_number);
358                 if (e->lc_number != LC_FREE)
359                         drbd_bm_mark_for_writeout(mdev,
360                                         al_extent_to_bm_page(e->lc_number));
361                 i++;
362         }
363         spin_unlock_irq(&mdev->al_lock);
364         BUG_ON(i > AL_UPDATES_PER_TRANSACTION);
365
366         buffer->n_updates = cpu_to_be16(i);
367         for ( ; i < AL_UPDATES_PER_TRANSACTION; i++) {
368                 buffer->update_slot_nr[i] = cpu_to_be16(-1);
369                 buffer->update_extent_nr[i] = cpu_to_be32(LC_FREE);
370         }
371
372         buffer->context_size = cpu_to_be16(mdev->act_log->nr_elements);
373         buffer->context_start_slot_nr = cpu_to_be16(mdev->al_tr_cycle);
374
375         mx = min_t(int, AL_CONTEXT_PER_TRANSACTION,
376                    mdev->act_log->nr_elements - mdev->al_tr_cycle);
377         for (i = 0; i < mx; i++) {
378                 unsigned idx = mdev->al_tr_cycle + i;
379                 extent_nr = lc_element_by_index(mdev->act_log, idx)->lc_number;
380                 buffer->context[i] = cpu_to_be32(extent_nr);
381         }
382         for (; i < AL_CONTEXT_PER_TRANSACTION; i++)
383                 buffer->context[i] = cpu_to_be32(LC_FREE);
384
385         mdev->al_tr_cycle += AL_CONTEXT_PER_TRANSACTION;
386         if (mdev->al_tr_cycle >= mdev->act_log->nr_elements)
387                 mdev->al_tr_cycle = 0;
388
389         sector =  mdev->ldev->md.md_offset
390                 + mdev->ldev->md.al_offset
391                 + mdev->al_tr_pos * (MD_BLOCK_SIZE>>9);
392
393         crc = crc32c(0, buffer, 4096);
394         buffer->crc32c = cpu_to_be32(crc);
395
396         if (drbd_bm_write_hinted(mdev))
397                 aw->err = -EIO;
398                 /* drbd_chk_io_error done already */
399         else if (drbd_md_sync_page_io(mdev, mdev->ldev, sector, WRITE)) {
400                 aw->err = -EIO;
401                 drbd_chk_io_error(mdev, 1, true);
402         } else {
403                 /* advance ringbuffer position and transaction counter */
404                 mdev->al_tr_pos = (mdev->al_tr_pos + 1) % (MD_AL_SECTORS*512/MD_BLOCK_SIZE);
405                 mdev->al_tr_number++;
406         }
407
408         mutex_unlock(&mdev->md_io_mutex);
409         complete(&((struct update_al_work *)w)->event);
410         put_ldev(mdev);
411
412         return 0;
413 }
414
415 /* FIXME
416  * reading of the activity log,
417  * and potentially dirtying of the affected bitmap regions,
418  * should be done from userland only.
419  * DRBD would simply always attach with an empty activity log,
420  * and refuse to attach to something that looks like a crashed primary.
421  */
422
423 /**
424  * drbd_al_read_tr() - Read a single transaction from the on disk activity log
425  * @mdev:       DRBD device.
426  * @bdev:       Block device to read form.
427  * @b:          pointer to an al_transaction.
428  * @index:      On disk slot of the transaction to read.
429  *
430  * Returns -1 on IO error, 0 on checksum error and 1 upon success.
431  */
432 static int drbd_al_read_tr(struct drbd_conf *mdev,
433                            struct drbd_backing_dev *bdev,
434                            int index)
435 {
436         struct al_transaction_on_disk *b = page_address(mdev->md_io_page);
437         sector_t sector;
438         u32 crc;
439
440         sector =  bdev->md.md_offset
441                 + bdev->md.al_offset
442                 + index * (MD_BLOCK_SIZE>>9);
443
444         /* Dont process error normally,
445          * as this is done before disk is attached! */
446         if (drbd_md_sync_page_io(mdev, bdev, sector, READ))
447                 return -1;
448
449         if (!expect(b->magic == cpu_to_be32(DRBD_AL_MAGIC)))
450                 return 0;
451
452         if (!expect(be16_to_cpu(b->n_updates) <= AL_UPDATES_PER_TRANSACTION))
453                 return 0;
454
455         if (!expect(be16_to_cpu(b->context_size) <= DRBD_AL_EXTENTS_MAX))
456                 return 0;
457
458         if (!expect(be16_to_cpu(b->context_start_slot_nr) < DRBD_AL_EXTENTS_MAX))
459                 return 0;
460
461         crc = be32_to_cpu(b->crc32c);
462         b->crc32c = 0;
463         if (!expect(crc == crc32c(0, b, 4096)))
464                 return 0;
465
466         return 1;
467 }
468
469 /**
470  * drbd_al_read_log() - Restores the activity log from its on disk representation.
471  * @mdev:       DRBD device.
472  * @bdev:       Block device to read form.
473  *
474  * Returns 1 on success, returns 0 when reading the log failed due to IO errors.
475  */
476 int drbd_al_read_log(struct drbd_conf *mdev, struct drbd_backing_dev *bdev)
477 {
478         struct al_transaction_on_disk *b;
479         int i;
480         int rv;
481         int mx;
482         int active_extents = 0;
483         int transactions = 0;
484         int found_valid = 0;
485         int found_initialized = 0;
486         int from = 0;
487         int to = 0;
488         u32 from_tnr = 0;
489         u32 to_tnr = 0;
490         u32 cnr;
491
492         /* Note that this is expected to be called with a newly created,
493          * clean and all unused activity log of the "expected size".
494          */
495
496         /* lock out all other meta data io for now,
497          * and make sure the page is mapped.
498          */
499         mutex_lock(&mdev->md_io_mutex);
500         b = page_address(mdev->md_io_page);
501
502         /* Always use the full ringbuffer space for now.
503          * possible optimization: read in all of it,
504          * then scan the in-memory pages. */
505
506         mx = (MD_AL_SECTORS*512/MD_BLOCK_SIZE);
507
508         /* Find the valid transaction in the log */
509         for (i = 0; i < mx; i++) {
510                 rv = drbd_al_read_tr(mdev, bdev, i);
511                 /* invalid data in that block */
512                 if (rv == 0)
513                         continue;
514                 if (be16_to_cpu(b->transaction_type) == AL_TR_INITIALIZED) {
515                         ++found_initialized;
516                         continue;
517                 }
518
519                 /* IO error */
520                 if (rv == -1) {
521                         mutex_unlock(&mdev->md_io_mutex);
522                         return 0;
523                 }
524
525                 cnr = be32_to_cpu(b->tr_number);
526                 if (++found_valid == 1) {
527                         from = i;
528                         to = i;
529                         from_tnr = cnr;
530                         to_tnr = cnr;
531                         continue;
532                 }
533
534                 D_ASSERT(cnr != to_tnr);
535                 D_ASSERT(cnr != from_tnr);
536                 if ((int)cnr - (int)from_tnr < 0) {
537                         D_ASSERT(from_tnr - cnr + i - from == mx);
538                         from = i;
539                         from_tnr = cnr;
540                 }
541                 if ((int)cnr - (int)to_tnr > 0) {
542                         D_ASSERT(cnr - to_tnr == i - to);
543                         to = i;
544                         to_tnr = cnr;
545                 }
546         }
547
548         if (!found_valid) {
549                 if (found_initialized != mx)
550                         dev_warn(DEV, "No usable activity log found.\n");
551                 mutex_unlock(&mdev->md_io_mutex);
552                 return 1;
553         }
554
555         /* Read the valid transactions.
556          * dev_info(DEV, "Reading from %d to %d.\n",from,to); */
557         i = from;
558         while (1) {
559                 struct lc_element *e;
560                 unsigned j, n, slot, extent_nr;
561
562                 rv = drbd_al_read_tr(mdev, bdev, i);
563                 if (!expect(rv != 0))
564                         goto cancel;
565                 if (rv == -1) {
566                         mutex_unlock(&mdev->md_io_mutex);
567                         return 0;
568                 }
569
570                 /* deal with different transaction types.
571                  * not yet implemented */
572                 if (!expect(b->transaction_type == 0))
573                         goto cancel;
574
575                 /* on the fly re-create/resize activity log?
576                  * will be a special transaction type flag. */
577                 if (!expect(be16_to_cpu(b->context_size) == mdev->act_log->nr_elements))
578                         goto cancel;
579                 if (!expect(be16_to_cpu(b->context_start_slot_nr) < mdev->act_log->nr_elements))
580                         goto cancel;
581
582                 /* We are the only user of the activity log right now,
583                  * don't actually need to take that lock. */
584                 spin_lock_irq(&mdev->al_lock);
585
586                 /* first, apply the context, ... */
587                 for (j = 0, slot = be16_to_cpu(b->context_start_slot_nr);
588                      j < AL_CONTEXT_PER_TRANSACTION &&
589                      slot < mdev->act_log->nr_elements; j++, slot++) {
590                         extent_nr = be32_to_cpu(b->context[j]);
591                         e = lc_element_by_index(mdev->act_log, slot);
592                         if (e->lc_number != extent_nr) {
593                                 if (extent_nr != LC_FREE)
594                                         active_extents++;
595                                 else
596                                         active_extents--;
597                         }
598                         lc_set(mdev->act_log, extent_nr, slot);
599                 }
600
601                 /* ... then apply the updates,
602                  * which override the context information.
603                  * drbd_al_read_tr already did the rangecheck
604                  * on n <= AL_UPDATES_PER_TRANSACTION */
605                 n = be16_to_cpu(b->n_updates);
606                 for (j = 0; j < n; j++) {
607                         slot = be16_to_cpu(b->update_slot_nr[j]);
608                         extent_nr = be32_to_cpu(b->update_extent_nr[j]);
609                         if (!expect(slot < mdev->act_log->nr_elements))
610                                 break;
611                         e = lc_element_by_index(mdev->act_log, slot);
612                         if (e->lc_number != extent_nr) {
613                                 if (extent_nr != LC_FREE)
614                                         active_extents++;
615                                 else
616                                         active_extents--;
617                         }
618                         lc_set(mdev->act_log, extent_nr, slot);
619                 }
620                 spin_unlock_irq(&mdev->al_lock);
621
622                 transactions++;
623
624 cancel:
625                 if (i == to)
626                         break;
627                 i++;
628                 if (i >= mx)
629                         i = 0;
630         }
631
632         mdev->al_tr_number = to_tnr+1;
633         mdev->al_tr_pos = (to + 1) % (MD_AL_SECTORS*512/MD_BLOCK_SIZE);
634
635         /* ok, we are done with it */
636         mutex_unlock(&mdev->md_io_mutex);
637
638         dev_info(DEV, "Found %d transactions (%d active extents) in activity log.\n",
639              transactions, active_extents);
640
641         return 1;
642 }
643
644 /**
645  * drbd_al_apply_to_bm() - Sets the bitmap to dirty(1) where covered by active AL extents
646  * @mdev:       DRBD device.
647  */
648 void drbd_al_apply_to_bm(struct drbd_conf *mdev)
649 {
650         unsigned int enr;
651         unsigned long add = 0;
652         char ppb[10];
653         int i, tmp;
654
655         wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
656
657         for (i = 0; i < mdev->act_log->nr_elements; i++) {
658                 enr = lc_element_by_index(mdev->act_log, i)->lc_number;
659                 if (enr == LC_FREE)
660                         continue;
661                 tmp = drbd_bm_ALe_set_all(mdev, enr);
662                 dynamic_dev_dbg(DEV, "AL: set %d bits in extent %u\n", tmp, enr);
663                 add += tmp;
664         }
665
666         lc_unlock(mdev->act_log);
667         wake_up(&mdev->al_wait);
668
669         dev_info(DEV, "Marked additional %s as out-of-sync based on AL.\n",
670              ppsize(ppb, Bit2KB(add)));
671 }
672
673 static int _try_lc_del(struct drbd_conf *mdev, struct lc_element *al_ext)
674 {
675         int rv;
676
677         spin_lock_irq(&mdev->al_lock);
678         rv = (al_ext->refcnt == 0);
679         if (likely(rv))
680                 lc_del(mdev->act_log, al_ext);
681         spin_unlock_irq(&mdev->al_lock);
682
683         return rv;
684 }
685
686 /**
687  * drbd_al_shrink() - Removes all active extents form the activity log
688  * @mdev:       DRBD device.
689  *
690  * Removes all active extents form the activity log, waiting until
691  * the reference count of each entry dropped to 0 first, of course.
692  *
693  * You need to lock mdev->act_log with lc_try_lock() / lc_unlock()
694  */
695 void drbd_al_shrink(struct drbd_conf *mdev)
696 {
697         struct lc_element *al_ext;
698         int i;
699
700         D_ASSERT(test_bit(__LC_LOCKED, &mdev->act_log->flags));
701
702         for (i = 0; i < mdev->act_log->nr_elements; i++) {
703                 al_ext = lc_element_by_index(mdev->act_log, i);
704                 if (al_ext->lc_number == LC_FREE)
705                         continue;
706                 wait_event(mdev->al_wait, _try_lc_del(mdev, al_ext));
707         }
708
709         wake_up(&mdev->al_wait);
710 }
711
712 static int w_update_odbm(struct drbd_work *w, int unused)
713 {
714         struct update_odbm_work *udw = container_of(w, struct update_odbm_work, w);
715         struct drbd_conf *mdev = w->mdev;
716         struct sib_info sib = { .sib_reason = SIB_SYNC_PROGRESS, };
717
718         if (!get_ldev(mdev)) {
719                 if (__ratelimit(&drbd_ratelimit_state))
720                         dev_warn(DEV, "Can not update on disk bitmap, local IO disabled.\n");
721                 kfree(udw);
722                 return 0;
723         }
724
725         drbd_bm_write_page(mdev, rs_extent_to_bm_page(udw->enr));
726         put_ldev(mdev);
727
728         kfree(udw);
729
730         if (drbd_bm_total_weight(mdev) <= mdev->rs_failed) {
731                 switch (mdev->state.conn) {
732                 case C_SYNC_SOURCE:  case C_SYNC_TARGET:
733                 case C_PAUSED_SYNC_S: case C_PAUSED_SYNC_T:
734                         drbd_resync_finished(mdev);
735                 default:
736                         /* nothing to do */
737                         break;
738                 }
739         }
740         drbd_bcast_event(mdev, &sib);
741
742         return 0;
743 }
744
745
746 /* ATTENTION. The AL's extents are 4MB each, while the extents in the
747  * resync LRU-cache are 16MB each.
748  * The caller of this function has to hold an get_ldev() reference.
749  *
750  * TODO will be obsoleted once we have a caching lru of the on disk bitmap
751  */
752 static void drbd_try_clear_on_disk_bm(struct drbd_conf *mdev, sector_t sector,
753                                       int count, int success)
754 {
755         struct lc_element *e;
756         struct update_odbm_work *udw;
757
758         unsigned int enr;
759
760         D_ASSERT(atomic_read(&mdev->local_cnt));
761
762         /* I simply assume that a sector/size pair never crosses
763          * a 16 MB extent border. (Currently this is true...) */
764         enr = BM_SECT_TO_EXT(sector);
765
766         e = lc_get(mdev->resync, enr);
767         if (e) {
768                 struct bm_extent *ext = lc_entry(e, struct bm_extent, lce);
769                 if (ext->lce.lc_number == enr) {
770                         if (success)
771                                 ext->rs_left -= count;
772                         else
773                                 ext->rs_failed += count;
774                         if (ext->rs_left < ext->rs_failed) {
775                                 dev_err(DEV, "BAD! sector=%llus enr=%u rs_left=%d "
776                                     "rs_failed=%d count=%d\n",
777                                      (unsigned long long)sector,
778                                      ext->lce.lc_number, ext->rs_left,
779                                      ext->rs_failed, count);
780                                 dump_stack();
781
782                                 lc_put(mdev->resync, &ext->lce);
783                                 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
784                                 return;
785                         }
786                 } else {
787                         /* Normally this element should be in the cache,
788                          * since drbd_rs_begin_io() pulled it already in.
789                          *
790                          * But maybe an application write finished, and we set
791                          * something outside the resync lru_cache in sync.
792                          */
793                         int rs_left = drbd_bm_e_weight(mdev, enr);
794                         if (ext->flags != 0) {
795                                 dev_warn(DEV, "changing resync lce: %d[%u;%02lx]"
796                                      " -> %d[%u;00]\n",
797                                      ext->lce.lc_number, ext->rs_left,
798                                      ext->flags, enr, rs_left);
799                                 ext->flags = 0;
800                         }
801                         if (ext->rs_failed) {
802                                 dev_warn(DEV, "Kicking resync_lru element enr=%u "
803                                      "out with rs_failed=%d\n",
804                                      ext->lce.lc_number, ext->rs_failed);
805                         }
806                         ext->rs_left = rs_left;
807                         ext->rs_failed = success ? 0 : count;
808                         /* we don't keep a persistent log of the resync lru,
809                          * we can commit any change right away. */
810                         lc_committed(mdev->resync);
811                 }
812                 lc_put(mdev->resync, &ext->lce);
813                 /* no race, we are within the al_lock! */
814
815                 if (ext->rs_left == ext->rs_failed) {
816                         ext->rs_failed = 0;
817
818                         udw = kmalloc(sizeof(*udw), GFP_ATOMIC);
819                         if (udw) {
820                                 udw->enr = ext->lce.lc_number;
821                                 udw->w.cb = w_update_odbm;
822                                 udw->w.mdev = mdev;
823                                 drbd_queue_work_front(&mdev->tconn->data.work, &udw->w);
824                         } else {
825                                 dev_warn(DEV, "Could not kmalloc an udw\n");
826                         }
827                 }
828         } else {
829                 dev_err(DEV, "lc_get() failed! locked=%d/%d flags=%lu\n",
830                     mdev->resync_locked,
831                     mdev->resync->nr_elements,
832                     mdev->resync->flags);
833         }
834 }
835
836 void drbd_advance_rs_marks(struct drbd_conf *mdev, unsigned long still_to_go)
837 {
838         unsigned long now = jiffies;
839         unsigned long last = mdev->rs_mark_time[mdev->rs_last_mark];
840         int next = (mdev->rs_last_mark + 1) % DRBD_SYNC_MARKS;
841         if (time_after_eq(now, last + DRBD_SYNC_MARK_STEP)) {
842                 if (mdev->rs_mark_left[mdev->rs_last_mark] != still_to_go &&
843                     mdev->state.conn != C_PAUSED_SYNC_T &&
844                     mdev->state.conn != C_PAUSED_SYNC_S) {
845                         mdev->rs_mark_time[next] = now;
846                         mdev->rs_mark_left[next] = still_to_go;
847                         mdev->rs_last_mark = next;
848                 }
849         }
850 }
851
852 /* clear the bit corresponding to the piece of storage in question:
853  * size byte of data starting from sector.  Only clear a bits of the affected
854  * one ore more _aligned_ BM_BLOCK_SIZE blocks.
855  *
856  * called by worker on C_SYNC_TARGET and receiver on SyncSource.
857  *
858  */
859 void __drbd_set_in_sync(struct drbd_conf *mdev, sector_t sector, int size,
860                        const char *file, const unsigned int line)
861 {
862         /* Is called from worker and receiver context _only_ */
863         unsigned long sbnr, ebnr, lbnr;
864         unsigned long count = 0;
865         sector_t esector, nr_sectors;
866         int wake_up = 0;
867         unsigned long flags;
868
869         if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
870                 dev_err(DEV, "drbd_set_in_sync: sector=%llus size=%d nonsense!\n",
871                                 (unsigned long long)sector, size);
872                 return;
873         }
874         nr_sectors = drbd_get_capacity(mdev->this_bdev);
875         esector = sector + (size >> 9) - 1;
876
877         if (!expect(sector < nr_sectors))
878                 return;
879         if (!expect(esector < nr_sectors))
880                 esector = nr_sectors - 1;
881
882         lbnr = BM_SECT_TO_BIT(nr_sectors-1);
883
884         /* we clear it (in sync).
885          * round up start sector, round down end sector.  we make sure we only
886          * clear full, aligned, BM_BLOCK_SIZE (4K) blocks */
887         if (unlikely(esector < BM_SECT_PER_BIT-1))
888                 return;
889         if (unlikely(esector == (nr_sectors-1)))
890                 ebnr = lbnr;
891         else
892                 ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1));
893         sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1);
894
895         if (sbnr > ebnr)
896                 return;
897
898         /*
899          * ok, (capacity & 7) != 0 sometimes, but who cares...
900          * we count rs_{total,left} in bits, not sectors.
901          */
902         count = drbd_bm_clear_bits(mdev, sbnr, ebnr);
903         if (count && get_ldev(mdev)) {
904                 drbd_advance_rs_marks(mdev, drbd_bm_total_weight(mdev));
905                 spin_lock_irqsave(&mdev->al_lock, flags);
906                 drbd_try_clear_on_disk_bm(mdev, sector, count, true);
907                 spin_unlock_irqrestore(&mdev->al_lock, flags);
908
909                 /* just wake_up unconditional now, various lc_chaged(),
910                  * lc_put() in drbd_try_clear_on_disk_bm(). */
911                 wake_up = 1;
912                 put_ldev(mdev);
913         }
914         if (wake_up)
915                 wake_up(&mdev->al_wait);
916 }
917
918 /*
919  * this is intended to set one request worth of data out of sync.
920  * affects at least 1 bit,
921  * and at most 1+DRBD_MAX_BIO_SIZE/BM_BLOCK_SIZE bits.
922  *
923  * called by tl_clear and drbd_send_dblock (==drbd_make_request).
924  * so this can be _any_ process.
925  */
926 int __drbd_set_out_of_sync(struct drbd_conf *mdev, sector_t sector, int size,
927                             const char *file, const unsigned int line)
928 {
929         unsigned long sbnr, ebnr, lbnr, flags;
930         sector_t esector, nr_sectors;
931         unsigned int enr, count = 0;
932         struct lc_element *e;
933
934         if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
935                 dev_err(DEV, "sector: %llus, size: %d\n",
936                         (unsigned long long)sector, size);
937                 return 0;
938         }
939
940         if (!get_ldev(mdev))
941                 return 0; /* no disk, no metadata, no bitmap to set bits in */
942
943         nr_sectors = drbd_get_capacity(mdev->this_bdev);
944         esector = sector + (size >> 9) - 1;
945
946         if (!expect(sector < nr_sectors))
947                 goto out;
948         if (!expect(esector < nr_sectors))
949                 esector = nr_sectors - 1;
950
951         lbnr = BM_SECT_TO_BIT(nr_sectors-1);
952
953         /* we set it out of sync,
954          * we do not need to round anything here */
955         sbnr = BM_SECT_TO_BIT(sector);
956         ebnr = BM_SECT_TO_BIT(esector);
957
958         /* ok, (capacity & 7) != 0 sometimes, but who cares...
959          * we count rs_{total,left} in bits, not sectors.  */
960         spin_lock_irqsave(&mdev->al_lock, flags);
961         count = drbd_bm_set_bits(mdev, sbnr, ebnr);
962
963         enr = BM_SECT_TO_EXT(sector);
964         e = lc_find(mdev->resync, enr);
965         if (e)
966                 lc_entry(e, struct bm_extent, lce)->rs_left += count;
967         spin_unlock_irqrestore(&mdev->al_lock, flags);
968
969 out:
970         put_ldev(mdev);
971
972         return count;
973 }
974
975 static
976 struct bm_extent *_bme_get(struct drbd_conf *mdev, unsigned int enr)
977 {
978         struct lc_element *e;
979         struct bm_extent *bm_ext;
980         int wakeup = 0;
981         unsigned long rs_flags;
982
983         spin_lock_irq(&mdev->al_lock);
984         if (mdev->resync_locked > mdev->resync->nr_elements/2) {
985                 spin_unlock_irq(&mdev->al_lock);
986                 return NULL;
987         }
988         e = lc_get(mdev->resync, enr);
989         bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
990         if (bm_ext) {
991                 if (bm_ext->lce.lc_number != enr) {
992                         bm_ext->rs_left = drbd_bm_e_weight(mdev, enr);
993                         bm_ext->rs_failed = 0;
994                         lc_committed(mdev->resync);
995                         wakeup = 1;
996                 }
997                 if (bm_ext->lce.refcnt == 1)
998                         mdev->resync_locked++;
999                 set_bit(BME_NO_WRITES, &bm_ext->flags);
1000         }
1001         rs_flags = mdev->resync->flags;
1002         spin_unlock_irq(&mdev->al_lock);
1003         if (wakeup)
1004                 wake_up(&mdev->al_wait);
1005
1006         if (!bm_ext) {
1007                 if (rs_flags & LC_STARVING)
1008                         dev_warn(DEV, "Have to wait for element"
1009                              " (resync LRU too small?)\n");
1010                 BUG_ON(rs_flags & LC_LOCKED);
1011         }
1012
1013         return bm_ext;
1014 }
1015
1016 static int _is_in_al(struct drbd_conf *mdev, unsigned int enr)
1017 {
1018         int rv;
1019
1020         spin_lock_irq(&mdev->al_lock);
1021         rv = lc_is_used(mdev->act_log, enr);
1022         spin_unlock_irq(&mdev->al_lock);
1023
1024         return rv;
1025 }
1026
1027 /**
1028  * drbd_rs_begin_io() - Gets an extent in the resync LRU cache and sets it to BME_LOCKED
1029  * @mdev:       DRBD device.
1030  * @sector:     The sector number.
1031  *
1032  * This functions sleeps on al_wait. Returns 0 on success, -EINTR if interrupted.
1033  */
1034 int drbd_rs_begin_io(struct drbd_conf *mdev, sector_t sector)
1035 {
1036         unsigned int enr = BM_SECT_TO_EXT(sector);
1037         struct bm_extent *bm_ext;
1038         int i, sig;
1039         int sa = 200; /* Step aside 200 times, then grab the extent and let app-IO wait.
1040                          200 times -> 20 seconds. */
1041
1042 retry:
1043         sig = wait_event_interruptible(mdev->al_wait,
1044                         (bm_ext = _bme_get(mdev, enr)));
1045         if (sig)
1046                 return -EINTR;
1047
1048         if (test_bit(BME_LOCKED, &bm_ext->flags))
1049                 return 0;
1050
1051         for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
1052                 sig = wait_event_interruptible(mdev->al_wait,
1053                                                !_is_in_al(mdev, enr * AL_EXT_PER_BM_SECT + i) ||
1054                                                test_bit(BME_PRIORITY, &bm_ext->flags));
1055
1056                 if (sig || (test_bit(BME_PRIORITY, &bm_ext->flags) && sa)) {
1057                         spin_lock_irq(&mdev->al_lock);
1058                         if (lc_put(mdev->resync, &bm_ext->lce) == 0) {
1059                                 bm_ext->flags = 0; /* clears BME_NO_WRITES and eventually BME_PRIORITY */
1060                                 mdev->resync_locked--;
1061                                 wake_up(&mdev->al_wait);
1062                         }
1063                         spin_unlock_irq(&mdev->al_lock);
1064                         if (sig)
1065                                 return -EINTR;
1066                         if (schedule_timeout_interruptible(HZ/10))
1067                                 return -EINTR;
1068                         if (sa && --sa == 0)
1069                                 dev_warn(DEV,"drbd_rs_begin_io() stepped aside for 20sec."
1070                                          "Resync stalled?\n");
1071                         goto retry;
1072                 }
1073         }
1074         set_bit(BME_LOCKED, &bm_ext->flags);
1075         return 0;
1076 }
1077
1078 /**
1079  * drbd_try_rs_begin_io() - Gets an extent in the resync LRU cache, does not sleep
1080  * @mdev:       DRBD device.
1081  * @sector:     The sector number.
1082  *
1083  * Gets an extent in the resync LRU cache, sets it to BME_NO_WRITES, then
1084  * tries to set it to BME_LOCKED. Returns 0 upon success, and -EAGAIN
1085  * if there is still application IO going on in this area.
1086  */
1087 int drbd_try_rs_begin_io(struct drbd_conf *mdev, sector_t sector)
1088 {
1089         unsigned int enr = BM_SECT_TO_EXT(sector);
1090         const unsigned int al_enr = enr*AL_EXT_PER_BM_SECT;
1091         struct lc_element *e;
1092         struct bm_extent *bm_ext;
1093         int i;
1094
1095         spin_lock_irq(&mdev->al_lock);
1096         if (mdev->resync_wenr != LC_FREE && mdev->resync_wenr != enr) {
1097                 /* in case you have very heavy scattered io, it may
1098                  * stall the syncer undefined if we give up the ref count
1099                  * when we try again and requeue.
1100                  *
1101                  * if we don't give up the refcount, but the next time
1102                  * we are scheduled this extent has been "synced" by new
1103                  * application writes, we'd miss the lc_put on the
1104                  * extent we keep the refcount on.
1105                  * so we remembered which extent we had to try again, and
1106                  * if the next requested one is something else, we do
1107                  * the lc_put here...
1108                  * we also have to wake_up
1109                  */
1110                 e = lc_find(mdev->resync, mdev->resync_wenr);
1111                 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1112                 if (bm_ext) {
1113                         D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
1114                         D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags));
1115                         clear_bit(BME_NO_WRITES, &bm_ext->flags);
1116                         mdev->resync_wenr = LC_FREE;
1117                         if (lc_put(mdev->resync, &bm_ext->lce) == 0)
1118                                 mdev->resync_locked--;
1119                         wake_up(&mdev->al_wait);
1120                 } else {
1121                         dev_alert(DEV, "LOGIC BUG\n");
1122                 }
1123         }
1124         /* TRY. */
1125         e = lc_try_get(mdev->resync, enr);
1126         bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1127         if (bm_ext) {
1128                 if (test_bit(BME_LOCKED, &bm_ext->flags))
1129                         goto proceed;
1130                 if (!test_and_set_bit(BME_NO_WRITES, &bm_ext->flags)) {
1131                         mdev->resync_locked++;
1132                 } else {
1133                         /* we did set the BME_NO_WRITES,
1134                          * but then could not set BME_LOCKED,
1135                          * so we tried again.
1136                          * drop the extra reference. */
1137                         bm_ext->lce.refcnt--;
1138                         D_ASSERT(bm_ext->lce.refcnt > 0);
1139                 }
1140                 goto check_al;
1141         } else {
1142                 /* do we rather want to try later? */
1143                 if (mdev->resync_locked > mdev->resync->nr_elements-3)
1144                         goto try_again;
1145                 /* Do or do not. There is no try. -- Yoda */
1146                 e = lc_get(mdev->resync, enr);
1147                 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1148                 if (!bm_ext) {
1149                         const unsigned long rs_flags = mdev->resync->flags;
1150                         if (rs_flags & LC_STARVING)
1151                                 dev_warn(DEV, "Have to wait for element"
1152                                      " (resync LRU too small?)\n");
1153                         BUG_ON(rs_flags & LC_LOCKED);
1154                         goto try_again;
1155                 }
1156                 if (bm_ext->lce.lc_number != enr) {
1157                         bm_ext->rs_left = drbd_bm_e_weight(mdev, enr);
1158                         bm_ext->rs_failed = 0;
1159                         lc_committed(mdev->resync);
1160                         wake_up(&mdev->al_wait);
1161                         D_ASSERT(test_bit(BME_LOCKED, &bm_ext->flags) == 0);
1162                 }
1163                 set_bit(BME_NO_WRITES, &bm_ext->flags);
1164                 D_ASSERT(bm_ext->lce.refcnt == 1);
1165                 mdev->resync_locked++;
1166                 goto check_al;
1167         }
1168 check_al:
1169         for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
1170                 if (lc_is_used(mdev->act_log, al_enr+i))
1171                         goto try_again;
1172         }
1173         set_bit(BME_LOCKED, &bm_ext->flags);
1174 proceed:
1175         mdev->resync_wenr = LC_FREE;
1176         spin_unlock_irq(&mdev->al_lock);
1177         return 0;
1178
1179 try_again:
1180         if (bm_ext)
1181                 mdev->resync_wenr = enr;
1182         spin_unlock_irq(&mdev->al_lock);
1183         return -EAGAIN;
1184 }
1185
1186 void drbd_rs_complete_io(struct drbd_conf *mdev, sector_t sector)
1187 {
1188         unsigned int enr = BM_SECT_TO_EXT(sector);
1189         struct lc_element *e;
1190         struct bm_extent *bm_ext;
1191         unsigned long flags;
1192
1193         spin_lock_irqsave(&mdev->al_lock, flags);
1194         e = lc_find(mdev->resync, enr);
1195         bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1196         if (!bm_ext) {
1197                 spin_unlock_irqrestore(&mdev->al_lock, flags);
1198                 if (__ratelimit(&drbd_ratelimit_state))
1199                         dev_err(DEV, "drbd_rs_complete_io() called, but extent not found\n");
1200                 return;
1201         }
1202
1203         if (bm_ext->lce.refcnt == 0) {
1204                 spin_unlock_irqrestore(&mdev->al_lock, flags);
1205                 dev_err(DEV, "drbd_rs_complete_io(,%llu [=%u]) called, "
1206                     "but refcnt is 0!?\n",
1207                     (unsigned long long)sector, enr);
1208                 return;
1209         }
1210
1211         if (lc_put(mdev->resync, &bm_ext->lce) == 0) {
1212                 bm_ext->flags = 0; /* clear BME_LOCKED, BME_NO_WRITES and BME_PRIORITY */
1213                 mdev->resync_locked--;
1214                 wake_up(&mdev->al_wait);
1215         }
1216
1217         spin_unlock_irqrestore(&mdev->al_lock, flags);
1218 }
1219
1220 /**
1221  * drbd_rs_cancel_all() - Removes all extents from the resync LRU (even BME_LOCKED)
1222  * @mdev:       DRBD device.
1223  */
1224 void drbd_rs_cancel_all(struct drbd_conf *mdev)
1225 {
1226         spin_lock_irq(&mdev->al_lock);
1227
1228         if (get_ldev_if_state(mdev, D_FAILED)) { /* Makes sure ->resync is there. */
1229                 lc_reset(mdev->resync);
1230                 put_ldev(mdev);
1231         }
1232         mdev->resync_locked = 0;
1233         mdev->resync_wenr = LC_FREE;
1234         spin_unlock_irq(&mdev->al_lock);
1235         wake_up(&mdev->al_wait);
1236 }
1237
1238 /**
1239  * drbd_rs_del_all() - Gracefully remove all extents from the resync LRU
1240  * @mdev:       DRBD device.
1241  *
1242  * Returns 0 upon success, -EAGAIN if at least one reference count was
1243  * not zero.
1244  */
1245 int drbd_rs_del_all(struct drbd_conf *mdev)
1246 {
1247         struct lc_element *e;
1248         struct bm_extent *bm_ext;
1249         int i;
1250
1251         spin_lock_irq(&mdev->al_lock);
1252
1253         if (get_ldev_if_state(mdev, D_FAILED)) {
1254                 /* ok, ->resync is there. */
1255                 for (i = 0; i < mdev->resync->nr_elements; i++) {
1256                         e = lc_element_by_index(mdev->resync, i);
1257                         bm_ext = lc_entry(e, struct bm_extent, lce);
1258                         if (bm_ext->lce.lc_number == LC_FREE)
1259                                 continue;
1260                         if (bm_ext->lce.lc_number == mdev->resync_wenr) {
1261                                 dev_info(DEV, "dropping %u in drbd_rs_del_all, apparently"
1262                                      " got 'synced' by application io\n",
1263                                      mdev->resync_wenr);
1264                                 D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
1265                                 D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags));
1266                                 clear_bit(BME_NO_WRITES, &bm_ext->flags);
1267                                 mdev->resync_wenr = LC_FREE;
1268                                 lc_put(mdev->resync, &bm_ext->lce);
1269                         }
1270                         if (bm_ext->lce.refcnt != 0) {
1271                                 dev_info(DEV, "Retrying drbd_rs_del_all() later. "
1272                                      "refcnt=%d\n", bm_ext->lce.refcnt);
1273                                 put_ldev(mdev);
1274                                 spin_unlock_irq(&mdev->al_lock);
1275                                 return -EAGAIN;
1276                         }
1277                         D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
1278                         D_ASSERT(!test_bit(BME_NO_WRITES, &bm_ext->flags));
1279                         lc_del(mdev->resync, &bm_ext->lce);
1280                 }
1281                 D_ASSERT(mdev->resync->used == 0);
1282                 put_ldev(mdev);
1283         }
1284         spin_unlock_irq(&mdev->al_lock);
1285
1286         return 0;
1287 }
1288
1289 /**
1290  * drbd_rs_failed_io() - Record information on a failure to resync the specified blocks
1291  * @mdev:       DRBD device.
1292  * @sector:     The sector number.
1293  * @size:       Size of failed IO operation, in byte.
1294  */
1295 void drbd_rs_failed_io(struct drbd_conf *mdev, sector_t sector, int size)
1296 {
1297         /* Is called from worker and receiver context _only_ */
1298         unsigned long sbnr, ebnr, lbnr;
1299         unsigned long count;
1300         sector_t esector, nr_sectors;
1301         int wake_up = 0;
1302
1303         if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
1304                 dev_err(DEV, "drbd_rs_failed_io: sector=%llus size=%d nonsense!\n",
1305                                 (unsigned long long)sector, size);
1306                 return;
1307         }
1308         nr_sectors = drbd_get_capacity(mdev->this_bdev);
1309         esector = sector + (size >> 9) - 1;
1310
1311         if (!expect(sector < nr_sectors))
1312                 return;
1313         if (!expect(esector < nr_sectors))
1314                 esector = nr_sectors - 1;
1315
1316         lbnr = BM_SECT_TO_BIT(nr_sectors-1);
1317
1318         /*
1319          * round up start sector, round down end sector.  we make sure we only
1320          * handle full, aligned, BM_BLOCK_SIZE (4K) blocks */
1321         if (unlikely(esector < BM_SECT_PER_BIT-1))
1322                 return;
1323         if (unlikely(esector == (nr_sectors-1)))
1324                 ebnr = lbnr;
1325         else
1326                 ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1));
1327         sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1);
1328
1329         if (sbnr > ebnr)
1330                 return;
1331
1332         /*
1333          * ok, (capacity & 7) != 0 sometimes, but who cares...
1334          * we count rs_{total,left} in bits, not sectors.
1335          */
1336         spin_lock_irq(&mdev->al_lock);
1337         count = drbd_bm_count_bits(mdev, sbnr, ebnr);
1338         if (count) {
1339                 mdev->rs_failed += count;
1340
1341                 if (get_ldev(mdev)) {
1342                         drbd_try_clear_on_disk_bm(mdev, sector, count, false);
1343                         put_ldev(mdev);
1344                 }
1345
1346                 /* just wake_up unconditional now, various lc_chaged(),
1347                  * lc_put() in drbd_try_clear_on_disk_bm(). */
1348                 wake_up = 1;
1349         }
1350         spin_unlock_irq(&mdev->al_lock);
1351         if (wake_up)
1352                 wake_up(&mdev->al_wait);
1353 }