1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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 "drbd_int.h"
31
32
33enum al_transaction_types {
34 AL_TR_UPDATE = 0,
35 AL_TR_INITIALIZED = 0xffff
36};
37
38struct __packed al_transaction_on_disk {
39
40 __be32 magic;
41
42
43
44 __be32 tr_number;
45
46
47 __be32 crc32c;
48
49
50
51
52 __be16 transaction_type;
53
54
55
56
57
58 __be16 n_updates;
59
60
61
62
63 __be16 context_size;
64
65
66 __be16 context_start_slot_nr;
67
68
69
70
71 __be32 __reserved[4];
72
73
74
75
76
77
78
79
80
81
82 __be16 update_slot_nr[AL_UPDATES_PER_TRANSACTION];
83
84
85
86 __be32 update_extent_nr[AL_UPDATES_PER_TRANSACTION];
87
88
89
90
91 __be32 context[AL_CONTEXT_PER_TRANSACTION];
92};
93
94void *drbd_md_get_buffer(struct drbd_device *device, const char *intent)
95{
96 int r;
97
98 wait_event(device->misc_wait,
99 (r = atomic_cmpxchg(&device->md_io.in_use, 0, 1)) == 0 ||
100 device->state.disk <= D_FAILED);
101
102 if (r)
103 return NULL;
104
105 device->md_io.current_use = intent;
106 device->md_io.start_jif = jiffies;
107 device->md_io.submit_jif = device->md_io.start_jif - 1;
108 return page_address(device->md_io.page);
109}
110
111void drbd_md_put_buffer(struct drbd_device *device)
112{
113 if (atomic_dec_and_test(&device->md_io.in_use))
114 wake_up(&device->misc_wait);
115}
116
117void wait_until_done_or_force_detached(struct drbd_device *device, struct drbd_backing_dev *bdev,
118 unsigned int *done)
119{
120 long dt;
121
122 rcu_read_lock();
123 dt = rcu_dereference(bdev->disk_conf)->disk_timeout;
124 rcu_read_unlock();
125 dt = dt * HZ / 10;
126 if (dt == 0)
127 dt = MAX_SCHEDULE_TIMEOUT;
128
129 dt = wait_event_timeout(device->misc_wait,
130 *done || test_bit(FORCE_DETACH, &device->flags), dt);
131 if (dt == 0) {
132 drbd_err(device, "meta-data IO operation timed out\n");
133 drbd_chk_io_error(device, 1, DRBD_FORCE_DETACH);
134 }
135}
136
137static int _drbd_md_sync_page_io(struct drbd_device *device,
138 struct drbd_backing_dev *bdev,
139 sector_t sector, int op)
140{
141 struct bio *bio;
142
143 const int size = 4096;
144 int err, op_flags = 0;
145
146 device->md_io.done = 0;
147 device->md_io.error = -ENODEV;
148
149 if ((op == REQ_OP_WRITE) && !test_bit(MD_NO_FUA, &device->flags))
150 op_flags |= REQ_FUA | REQ_PREFLUSH;
151 op_flags |= REQ_SYNC;
152
153 bio = bio_alloc_drbd(GFP_NOIO);
154 bio_set_dev(bio, bdev->md_bdev);
155 bio->bi_iter.bi_sector = sector;
156 err = -EIO;
157 if (bio_add_page(bio, device->md_io.page, size, 0) != size)
158 goto out;
159 bio->bi_private = device;
160 bio->bi_end_io = drbd_md_endio;
161 bio_set_op_attrs(bio, op, op_flags);
162
163 if (op != REQ_OP_WRITE && device->state.disk == D_DISKLESS && device->ldev == NULL)
164
165 ;
166 else if (!get_ldev_if_state(device, D_ATTACHING)) {
167
168 drbd_err(device, "ASSERT FAILED: get_ldev_if_state() == 1 in _drbd_md_sync_page_io()\n");
169 err = -ENODEV;
170 goto out;
171 }
172
173 bio_get(bio);
174 atomic_inc(&device->md_io.in_use);
175 device->md_io.submit_jif = jiffies;
176 if (drbd_insert_fault(device, (op == REQ_OP_WRITE) ? DRBD_FAULT_MD_WR : DRBD_FAULT_MD_RD))
177 bio_io_error(bio);
178 else
179 submit_bio(bio);
180 wait_until_done_or_force_detached(device, bdev, &device->md_io.done);
181 if (!bio->bi_status)
182 err = device->md_io.error;
183
184 out:
185 bio_put(bio);
186 return err;
187}
188
189int drbd_md_sync_page_io(struct drbd_device *device, struct drbd_backing_dev *bdev,
190 sector_t sector, int op)
191{
192 int err;
193 D_ASSERT(device, atomic_read(&device->md_io.in_use) == 1);
194
195 BUG_ON(!bdev->md_bdev);
196
197 dynamic_drbd_dbg(device, "meta_data io: %s [%d]:%s(,%llus,%s) %pS\n",
198 current->comm, current->pid, __func__,
199 (unsigned long long)sector, (op == REQ_OP_WRITE) ? "WRITE" : "READ",
200 (void*)_RET_IP_ );
201
202 if (sector < drbd_md_first_sector(bdev) ||
203 sector + 7 > drbd_md_last_sector(bdev))
204 drbd_alert(device, "%s [%d]:%s(,%llus,%s) out of range md access!\n",
205 current->comm, current->pid, __func__,
206 (unsigned long long)sector,
207 (op == REQ_OP_WRITE) ? "WRITE" : "READ");
208
209 err = _drbd_md_sync_page_io(device, bdev, sector, op);
210 if (err) {
211 drbd_err(device, "drbd_md_sync_page_io(,%llus,%s) failed with error %d\n",
212 (unsigned long long)sector,
213 (op == REQ_OP_WRITE) ? "WRITE" : "READ", err);
214 }
215 return err;
216}
217
218static struct bm_extent *find_active_resync_extent(struct drbd_device *device, unsigned int enr)
219{
220 struct lc_element *tmp;
221 tmp = lc_find(device->resync, enr/AL_EXT_PER_BM_SECT);
222 if (unlikely(tmp != NULL)) {
223 struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce);
224 if (test_bit(BME_NO_WRITES, &bm_ext->flags))
225 return bm_ext;
226 }
227 return NULL;
228}
229
230static struct lc_element *_al_get(struct drbd_device *device, unsigned int enr, bool nonblock)
231{
232 struct lc_element *al_ext;
233 struct bm_extent *bm_ext;
234 int wake;
235
236 spin_lock_irq(&device->al_lock);
237 bm_ext = find_active_resync_extent(device, enr);
238 if (bm_ext) {
239 wake = !test_and_set_bit(BME_PRIORITY, &bm_ext->flags);
240 spin_unlock_irq(&device->al_lock);
241 if (wake)
242 wake_up(&device->al_wait);
243 return NULL;
244 }
245 if (nonblock)
246 al_ext = lc_try_get(device->act_log, enr);
247 else
248 al_ext = lc_get(device->act_log, enr);
249 spin_unlock_irq(&device->al_lock);
250 return al_ext;
251}
252
253bool drbd_al_begin_io_fastpath(struct drbd_device *device, struct drbd_interval *i)
254{
255
256
257 unsigned first = i->sector >> (AL_EXTENT_SHIFT-9);
258 unsigned last = i->size == 0 ? first : (i->sector + (i->size >> 9) - 1) >> (AL_EXTENT_SHIFT-9);
259
260 D_ASSERT(device, first <= last);
261 D_ASSERT(device, atomic_read(&device->local_cnt) > 0);
262
263
264 if (first != last)
265 return false;
266
267 return _al_get(device, first, true);
268}
269
270bool drbd_al_begin_io_prepare(struct drbd_device *device, struct drbd_interval *i)
271{
272
273
274 unsigned first = i->sector >> (AL_EXTENT_SHIFT-9);
275 unsigned last = i->size == 0 ? first : (i->sector + (i->size >> 9) - 1) >> (AL_EXTENT_SHIFT-9);
276 unsigned enr;
277 bool need_transaction = false;
278
279 D_ASSERT(device, first <= last);
280 D_ASSERT(device, atomic_read(&device->local_cnt) > 0);
281
282 for (enr = first; enr <= last; enr++) {
283 struct lc_element *al_ext;
284 wait_event(device->al_wait,
285 (al_ext = _al_get(device, enr, false)) != NULL);
286 if (al_ext->lc_number != enr)
287 need_transaction = true;
288 }
289 return need_transaction;
290}
291
292#if (PAGE_SHIFT + 3) < (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT)
293
294
295
296
297
298# error FIXME
299#endif
300
301static unsigned int al_extent_to_bm_page(unsigned int al_enr)
302{
303 return al_enr >>
304
305 ((PAGE_SHIFT + 3) -
306
307 (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT));
308}
309
310static sector_t al_tr_number_to_on_disk_sector(struct drbd_device *device)
311{
312 const unsigned int stripes = device->ldev->md.al_stripes;
313 const unsigned int stripe_size_4kB = device->ldev->md.al_stripe_size_4k;
314
315
316 unsigned int t = device->al_tr_number % (device->ldev->md.al_size_4k);
317
318
319 t = ((t % stripes) * stripe_size_4kB) + t/stripes;
320
321
322 t *= 8;
323
324
325 return device->ldev->md.md_offset + device->ldev->md.al_offset + t;
326}
327
328static int __al_write_transaction(struct drbd_device *device, struct al_transaction_on_disk *buffer)
329{
330 struct lc_element *e;
331 sector_t sector;
332 int i, mx;
333 unsigned extent_nr;
334 unsigned crc = 0;
335 int err = 0;
336
337 memset(buffer, 0, sizeof(*buffer));
338 buffer->magic = cpu_to_be32(DRBD_AL_MAGIC);
339 buffer->tr_number = cpu_to_be32(device->al_tr_number);
340
341 i = 0;
342
343 drbd_bm_reset_al_hints(device);
344
345
346
347
348
349 spin_lock_irq(&device->al_lock);
350 list_for_each_entry(e, &device->act_log->to_be_changed, list) {
351 if (i == AL_UPDATES_PER_TRANSACTION) {
352 i++;
353 break;
354 }
355 buffer->update_slot_nr[i] = cpu_to_be16(e->lc_index);
356 buffer->update_extent_nr[i] = cpu_to_be32(e->lc_new_number);
357 if (e->lc_number != LC_FREE)
358 drbd_bm_mark_for_writeout(device,
359 al_extent_to_bm_page(e->lc_number));
360 i++;
361 }
362 spin_unlock_irq(&device->al_lock);
363 BUG_ON(i > AL_UPDATES_PER_TRANSACTION);
364
365 buffer->n_updates = cpu_to_be16(i);
366 for ( ; i < AL_UPDATES_PER_TRANSACTION; i++) {
367 buffer->update_slot_nr[i] = cpu_to_be16(-1);
368 buffer->update_extent_nr[i] = cpu_to_be32(LC_FREE);
369 }
370
371 buffer->context_size = cpu_to_be16(device->act_log->nr_elements);
372 buffer->context_start_slot_nr = cpu_to_be16(device->al_tr_cycle);
373
374 mx = min_t(int, AL_CONTEXT_PER_TRANSACTION,
375 device->act_log->nr_elements - device->al_tr_cycle);
376 for (i = 0; i < mx; i++) {
377 unsigned idx = device->al_tr_cycle + i;
378 extent_nr = lc_element_by_index(device->act_log, idx)->lc_number;
379 buffer->context[i] = cpu_to_be32(extent_nr);
380 }
381 for (; i < AL_CONTEXT_PER_TRANSACTION; i++)
382 buffer->context[i] = cpu_to_be32(LC_FREE);
383
384 device->al_tr_cycle += AL_CONTEXT_PER_TRANSACTION;
385 if (device->al_tr_cycle >= device->act_log->nr_elements)
386 device->al_tr_cycle = 0;
387
388 sector = al_tr_number_to_on_disk_sector(device);
389
390 crc = crc32c(0, buffer, 4096);
391 buffer->crc32c = cpu_to_be32(crc);
392
393 if (drbd_bm_write_hinted(device))
394 err = -EIO;
395 else {
396 bool write_al_updates;
397 rcu_read_lock();
398 write_al_updates = rcu_dereference(device->ldev->disk_conf)->al_updates;
399 rcu_read_unlock();
400 if (write_al_updates) {
401 if (drbd_md_sync_page_io(device, device->ldev, sector, WRITE)) {
402 err = -EIO;
403 drbd_chk_io_error(device, 1, DRBD_META_IO_ERROR);
404 } else {
405 device->al_tr_number++;
406 device->al_writ_cnt++;
407 }
408 }
409 }
410
411 return err;
412}
413
414static int al_write_transaction(struct drbd_device *device)
415{
416 struct al_transaction_on_disk *buffer;
417 int err;
418
419 if (!get_ldev(device)) {
420 drbd_err(device, "disk is %s, cannot start al transaction\n",
421 drbd_disk_str(device->state.disk));
422 return -EIO;
423 }
424
425
426 if (device->state.disk < D_INCONSISTENT) {
427 drbd_err(device,
428 "disk is %s, cannot write al transaction\n",
429 drbd_disk_str(device->state.disk));
430 put_ldev(device);
431 return -EIO;
432 }
433
434
435 buffer = drbd_md_get_buffer(device, __func__);
436 if (!buffer) {
437 drbd_err(device, "disk failed while waiting for md_io buffer\n");
438 put_ldev(device);
439 return -ENODEV;
440 }
441
442 err = __al_write_transaction(device, buffer);
443
444 drbd_md_put_buffer(device);
445 put_ldev(device);
446
447 return err;
448}
449
450
451void drbd_al_begin_io_commit(struct drbd_device *device)
452{
453 bool locked = false;
454
455
456
457
458 wait_event(device->al_wait,
459 device->act_log->pending_changes == 0 ||
460 (locked = lc_try_lock_for_transaction(device->act_log)));
461
462 if (locked) {
463
464
465 if (device->act_log->pending_changes) {
466 bool write_al_updates;
467
468 rcu_read_lock();
469 write_al_updates = rcu_dereference(device->ldev->disk_conf)->al_updates;
470 rcu_read_unlock();
471
472 if (write_al_updates)
473 al_write_transaction(device);
474 spin_lock_irq(&device->al_lock);
475
476
477
478
479 lc_committed(device->act_log);
480 spin_unlock_irq(&device->al_lock);
481 }
482 lc_unlock(device->act_log);
483 wake_up(&device->al_wait);
484 }
485}
486
487
488
489
490void drbd_al_begin_io(struct drbd_device *device, struct drbd_interval *i)
491{
492 if (drbd_al_begin_io_prepare(device, i))
493 drbd_al_begin_io_commit(device);
494}
495
496int drbd_al_begin_io_nonblock(struct drbd_device *device, struct drbd_interval *i)
497{
498 struct lru_cache *al = device->act_log;
499
500
501 unsigned first = i->sector >> (AL_EXTENT_SHIFT-9);
502 unsigned last = i->size == 0 ? first : (i->sector + (i->size >> 9) - 1) >> (AL_EXTENT_SHIFT-9);
503 unsigned nr_al_extents;
504 unsigned available_update_slots;
505 unsigned enr;
506
507 D_ASSERT(device, first <= last);
508
509 nr_al_extents = 1 + last - first;
510 available_update_slots = min(al->nr_elements - al->used,
511 al->max_pending_changes - al->pending_changes);
512
513
514
515
516 if (available_update_slots < nr_al_extents) {
517
518
519
520
521
522
523
524
525 if (!al->pending_changes)
526 __set_bit(__LC_STARVING, &device->act_log->flags);
527 return -ENOBUFS;
528 }
529
530
531 for (enr = first; enr <= last; enr++) {
532 struct lc_element *tmp;
533 tmp = lc_find(device->resync, enr/AL_EXT_PER_BM_SECT);
534 if (unlikely(tmp != NULL)) {
535 struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce);
536 if (test_bit(BME_NO_WRITES, &bm_ext->flags)) {
537 if (!test_and_set_bit(BME_PRIORITY, &bm_ext->flags))
538 return -EBUSY;
539 return -EWOULDBLOCK;
540 }
541 }
542 }
543
544
545
546
547 for (enr = first; enr <= last; enr++) {
548 struct lc_element *al_ext;
549 al_ext = lc_get_cumulative(device->act_log, enr);
550 if (!al_ext)
551 drbd_info(device, "LOGIC BUG for enr=%u\n", enr);
552 }
553 return 0;
554}
555
556void drbd_al_complete_io(struct drbd_device *device, struct drbd_interval *i)
557{
558
559
560 unsigned first = i->sector >> (AL_EXTENT_SHIFT-9);
561 unsigned last = i->size == 0 ? first : (i->sector + (i->size >> 9) - 1) >> (AL_EXTENT_SHIFT-9);
562 unsigned enr;
563 struct lc_element *extent;
564 unsigned long flags;
565
566 D_ASSERT(device, first <= last);
567 spin_lock_irqsave(&device->al_lock, flags);
568
569 for (enr = first; enr <= last; enr++) {
570 extent = lc_find(device->act_log, enr);
571 if (!extent) {
572 drbd_err(device, "al_complete_io() called on inactive extent %u\n", enr);
573 continue;
574 }
575 lc_put(device->act_log, extent);
576 }
577 spin_unlock_irqrestore(&device->al_lock, flags);
578 wake_up(&device->al_wait);
579}
580
581static int _try_lc_del(struct drbd_device *device, struct lc_element *al_ext)
582{
583 int rv;
584
585 spin_lock_irq(&device->al_lock);
586 rv = (al_ext->refcnt == 0);
587 if (likely(rv))
588 lc_del(device->act_log, al_ext);
589 spin_unlock_irq(&device->al_lock);
590
591 return rv;
592}
593
594
595
596
597
598
599
600
601
602
603void drbd_al_shrink(struct drbd_device *device)
604{
605 struct lc_element *al_ext;
606 int i;
607
608 D_ASSERT(device, test_bit(__LC_LOCKED, &device->act_log->flags));
609
610 for (i = 0; i < device->act_log->nr_elements; i++) {
611 al_ext = lc_element_by_index(device->act_log, i);
612 if (al_ext->lc_number == LC_FREE)
613 continue;
614 wait_event(device->al_wait, _try_lc_del(device, al_ext));
615 }
616
617 wake_up(&device->al_wait);
618}
619
620int drbd_al_initialize(struct drbd_device *device, void *buffer)
621{
622 struct al_transaction_on_disk *al = buffer;
623 struct drbd_md *md = &device->ldev->md;
624 int al_size_4k = md->al_stripes * md->al_stripe_size_4k;
625 int i;
626
627 __al_write_transaction(device, al);
628
629 spin_lock_irq(&device->al_lock);
630 lc_committed(device->act_log);
631 spin_unlock_irq(&device->al_lock);
632
633
634
635
636 for (i = 1; i < al_size_4k; i++) {
637 int err = __al_write_transaction(device, al);
638 if (err)
639 return err;
640 }
641 return 0;
642}
643
644static const char *drbd_change_sync_fname[] = {
645 [RECORD_RS_FAILED] = "drbd_rs_failed_io",
646 [SET_IN_SYNC] = "drbd_set_in_sync",
647 [SET_OUT_OF_SYNC] = "drbd_set_out_of_sync"
648};
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663static bool update_rs_extent(struct drbd_device *device,
664 unsigned int enr, int count,
665 enum update_sync_bits_mode mode)
666{
667 struct lc_element *e;
668
669 D_ASSERT(device, atomic_read(&device->local_cnt));
670
671
672
673
674
675
676
677
678 if (mode == SET_OUT_OF_SYNC)
679 e = lc_find(device->resync, enr);
680 else
681 e = lc_get(device->resync, enr);
682 if (e) {
683 struct bm_extent *ext = lc_entry(e, struct bm_extent, lce);
684 if (ext->lce.lc_number == enr) {
685 if (mode == SET_IN_SYNC)
686 ext->rs_left -= count;
687 else if (mode == SET_OUT_OF_SYNC)
688 ext->rs_left += count;
689 else
690 ext->rs_failed += count;
691 if (ext->rs_left < ext->rs_failed) {
692 drbd_warn(device, "BAD! enr=%u rs_left=%d "
693 "rs_failed=%d count=%d cstate=%s\n",
694 ext->lce.lc_number, ext->rs_left,
695 ext->rs_failed, count,
696 drbd_conn_str(device->state.conn));
697
698
699
700
701
702
703
704 ext->rs_left = drbd_bm_e_weight(device, enr);
705 }
706 } else {
707
708
709
710
711
712
713 int rs_left = drbd_bm_e_weight(device, enr);
714 if (ext->flags != 0) {
715 drbd_warn(device, "changing resync lce: %d[%u;%02lx]"
716 " -> %d[%u;00]\n",
717 ext->lce.lc_number, ext->rs_left,
718 ext->flags, enr, rs_left);
719 ext->flags = 0;
720 }
721 if (ext->rs_failed) {
722 drbd_warn(device, "Kicking resync_lru element enr=%u "
723 "out with rs_failed=%d\n",
724 ext->lce.lc_number, ext->rs_failed);
725 }
726 ext->rs_left = rs_left;
727 ext->rs_failed = (mode == RECORD_RS_FAILED) ? count : 0;
728
729
730 lc_committed(device->resync);
731 }
732 if (mode != SET_OUT_OF_SYNC)
733 lc_put(device->resync, &ext->lce);
734
735
736 if (ext->rs_left <= ext->rs_failed) {
737 ext->rs_failed = 0;
738 return true;
739 }
740 } else if (mode != SET_OUT_OF_SYNC) {
741
742 drbd_err(device, "lc_get() failed! locked=%d/%d flags=%lu\n",
743 device->resync_locked,
744 device->resync->nr_elements,
745 device->resync->flags);
746 }
747 return false;
748}
749
750void drbd_advance_rs_marks(struct drbd_device *device, unsigned long still_to_go)
751{
752 unsigned long now = jiffies;
753 unsigned long last = device->rs_mark_time[device->rs_last_mark];
754 int next = (device->rs_last_mark + 1) % DRBD_SYNC_MARKS;
755 if (time_after_eq(now, last + DRBD_SYNC_MARK_STEP)) {
756 if (device->rs_mark_left[device->rs_last_mark] != still_to_go &&
757 device->state.conn != C_PAUSED_SYNC_T &&
758 device->state.conn != C_PAUSED_SYNC_S) {
759 device->rs_mark_time[next] = now;
760 device->rs_mark_left[next] = still_to_go;
761 device->rs_last_mark = next;
762 }
763 }
764}
765
766
767static bool lazy_bitmap_update_due(struct drbd_device *device)
768{
769 return time_after(jiffies, device->rs_last_bcast + 2*HZ);
770}
771
772static void maybe_schedule_on_disk_bitmap_update(struct drbd_device *device, bool rs_done)
773{
774 if (rs_done) {
775 struct drbd_connection *connection = first_peer_device(device)->connection;
776 if (connection->agreed_pro_version <= 95 ||
777 is_sync_target_state(device->state.conn))
778 set_bit(RS_DONE, &device->flags);
779
780
781
782
783
784
785 } else if (!lazy_bitmap_update_due(device))
786 return;
787
788 drbd_device_post_work(device, RS_PROGRESS);
789}
790
791static int update_sync_bits(struct drbd_device *device,
792 unsigned long sbnr, unsigned long ebnr,
793 enum update_sync_bits_mode mode)
794{
795
796
797
798
799
800 unsigned long flags;
801 unsigned long count = 0;
802 unsigned int cleared = 0;
803 while (sbnr <= ebnr) {
804
805
806
807 unsigned long tbnr = min(ebnr, sbnr | BM_BLOCKS_PER_BM_EXT_MASK);
808 unsigned long c;
809
810 if (mode == RECORD_RS_FAILED)
811
812
813
814
815
816 c = drbd_bm_count_bits(device, sbnr, tbnr);
817 else if (mode == SET_IN_SYNC)
818 c = drbd_bm_clear_bits(device, sbnr, tbnr);
819 else
820 c = drbd_bm_set_bits(device, sbnr, tbnr);
821
822 if (c) {
823 spin_lock_irqsave(&device->al_lock, flags);
824 cleared += update_rs_extent(device, BM_BIT_TO_EXT(sbnr), c, mode);
825 spin_unlock_irqrestore(&device->al_lock, flags);
826 count += c;
827 }
828 sbnr = tbnr + 1;
829 }
830 if (count) {
831 if (mode == SET_IN_SYNC) {
832 unsigned long still_to_go = drbd_bm_total_weight(device);
833 bool rs_is_done = (still_to_go <= device->rs_failed);
834 drbd_advance_rs_marks(device, still_to_go);
835 if (cleared || rs_is_done)
836 maybe_schedule_on_disk_bitmap_update(device, rs_is_done);
837 } else if (mode == RECORD_RS_FAILED)
838 device->rs_failed += count;
839 wake_up(&device->al_wait);
840 }
841 return count;
842}
843
844static bool plausible_request_size(int size)
845{
846 return size > 0
847 && size <= DRBD_MAX_BATCH_BIO_SIZE
848 && IS_ALIGNED(size, 512);
849}
850
851
852
853
854
855
856
857
858int __drbd_change_sync(struct drbd_device *device, sector_t sector, int size,
859 enum update_sync_bits_mode mode)
860{
861
862 unsigned long sbnr, ebnr, lbnr;
863 unsigned long count = 0;
864 sector_t esector, nr_sectors;
865
866
867 if ((mode == SET_OUT_OF_SYNC) && size == 0)
868 return 0;
869
870 if (!plausible_request_size(size)) {
871 drbd_err(device, "%s: sector=%llus size=%d nonsense!\n",
872 drbd_change_sync_fname[mode],
873 (unsigned long long)sector, size);
874 return 0;
875 }
876
877 if (!get_ldev(device))
878 return 0;
879
880 nr_sectors = drbd_get_capacity(device->this_bdev);
881 esector = sector + (size >> 9) - 1;
882
883 if (!expect(sector < nr_sectors))
884 goto out;
885 if (!expect(esector < nr_sectors))
886 esector = nr_sectors - 1;
887
888 lbnr = BM_SECT_TO_BIT(nr_sectors-1);
889
890 if (mode == SET_IN_SYNC) {
891
892
893 if (unlikely(esector < BM_SECT_PER_BIT-1))
894 goto out;
895 if (unlikely(esector == (nr_sectors-1)))
896 ebnr = lbnr;
897 else
898 ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1));
899 sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1);
900 } else {
901
902
903 sbnr = BM_SECT_TO_BIT(sector);
904 ebnr = BM_SECT_TO_BIT(esector);
905 }
906
907 count = update_sync_bits(device, sbnr, ebnr, mode);
908out:
909 put_ldev(device);
910 return count;
911}
912
913static
914struct bm_extent *_bme_get(struct drbd_device *device, unsigned int enr)
915{
916 struct lc_element *e;
917 struct bm_extent *bm_ext;
918 int wakeup = 0;
919 unsigned long rs_flags;
920
921 spin_lock_irq(&device->al_lock);
922 if (device->resync_locked > device->resync->nr_elements/2) {
923 spin_unlock_irq(&device->al_lock);
924 return NULL;
925 }
926 e = lc_get(device->resync, enr);
927 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
928 if (bm_ext) {
929 if (bm_ext->lce.lc_number != enr) {
930 bm_ext->rs_left = drbd_bm_e_weight(device, enr);
931 bm_ext->rs_failed = 0;
932 lc_committed(device->resync);
933 wakeup = 1;
934 }
935 if (bm_ext->lce.refcnt == 1)
936 device->resync_locked++;
937 set_bit(BME_NO_WRITES, &bm_ext->flags);
938 }
939 rs_flags = device->resync->flags;
940 spin_unlock_irq(&device->al_lock);
941 if (wakeup)
942 wake_up(&device->al_wait);
943
944 if (!bm_ext) {
945 if (rs_flags & LC_STARVING)
946 drbd_warn(device, "Have to wait for element"
947 " (resync LRU too small?)\n");
948 BUG_ON(rs_flags & LC_LOCKED);
949 }
950
951 return bm_ext;
952}
953
954static int _is_in_al(struct drbd_device *device, unsigned int enr)
955{
956 int rv;
957
958 spin_lock_irq(&device->al_lock);
959 rv = lc_is_used(device->act_log, enr);
960 spin_unlock_irq(&device->al_lock);
961
962 return rv;
963}
964
965
966
967
968
969
970
971
972int drbd_rs_begin_io(struct drbd_device *device, sector_t sector)
973{
974 unsigned int enr = BM_SECT_TO_EXT(sector);
975 struct bm_extent *bm_ext;
976 int i, sig;
977 bool sa;
978
979retry:
980 sig = wait_event_interruptible(device->al_wait,
981 (bm_ext = _bme_get(device, enr)));
982 if (sig)
983 return -EINTR;
984
985 if (test_bit(BME_LOCKED, &bm_ext->flags))
986 return 0;
987
988
989 sa = drbd_rs_c_min_rate_throttle(device);
990
991 for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
992 sig = wait_event_interruptible(device->al_wait,
993 !_is_in_al(device, enr * AL_EXT_PER_BM_SECT + i) ||
994 (sa && test_bit(BME_PRIORITY, &bm_ext->flags)));
995
996 if (sig || (sa && test_bit(BME_PRIORITY, &bm_ext->flags))) {
997 spin_lock_irq(&device->al_lock);
998 if (lc_put(device->resync, &bm_ext->lce) == 0) {
999 bm_ext->flags = 0;
1000 device->resync_locked--;
1001 wake_up(&device->al_wait);
1002 }
1003 spin_unlock_irq(&device->al_lock);
1004 if (sig)
1005 return -EINTR;
1006 if (schedule_timeout_interruptible(HZ/10))
1007 return -EINTR;
1008 goto retry;
1009 }
1010 }
1011 set_bit(BME_LOCKED, &bm_ext->flags);
1012 return 0;
1013}
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024int drbd_try_rs_begin_io(struct drbd_device *device, sector_t sector)
1025{
1026 unsigned int enr = BM_SECT_TO_EXT(sector);
1027 const unsigned int al_enr = enr*AL_EXT_PER_BM_SECT;
1028 struct lc_element *e;
1029 struct bm_extent *bm_ext;
1030 int i;
1031 bool throttle = drbd_rs_should_slow_down(device, sector, true);
1032
1033
1034
1035
1036
1037
1038 if (throttle && device->resync_wenr != enr)
1039 return -EAGAIN;
1040
1041 spin_lock_irq(&device->al_lock);
1042 if (device->resync_wenr != LC_FREE && device->resync_wenr != enr) {
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056 e = lc_find(device->resync, device->resync_wenr);
1057 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1058 if (bm_ext) {
1059 D_ASSERT(device, !test_bit(BME_LOCKED, &bm_ext->flags));
1060 D_ASSERT(device, test_bit(BME_NO_WRITES, &bm_ext->flags));
1061 clear_bit(BME_NO_WRITES, &bm_ext->flags);
1062 device->resync_wenr = LC_FREE;
1063 if (lc_put(device->resync, &bm_ext->lce) == 0) {
1064 bm_ext->flags = 0;
1065 device->resync_locked--;
1066 }
1067 wake_up(&device->al_wait);
1068 } else {
1069 drbd_alert(device, "LOGIC BUG\n");
1070 }
1071 }
1072
1073 e = lc_try_get(device->resync, enr);
1074 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1075 if (bm_ext) {
1076 if (test_bit(BME_LOCKED, &bm_ext->flags))
1077 goto proceed;
1078 if (!test_and_set_bit(BME_NO_WRITES, &bm_ext->flags)) {
1079 device->resync_locked++;
1080 } else {
1081
1082
1083
1084
1085 bm_ext->lce.refcnt--;
1086 D_ASSERT(device, bm_ext->lce.refcnt > 0);
1087 }
1088 goto check_al;
1089 } else {
1090
1091 if (device->resync_locked > device->resync->nr_elements-3)
1092 goto try_again;
1093
1094 e = lc_get(device->resync, enr);
1095 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1096 if (!bm_ext) {
1097 const unsigned long rs_flags = device->resync->flags;
1098 if (rs_flags & LC_STARVING)
1099 drbd_warn(device, "Have to wait for element"
1100 " (resync LRU too small?)\n");
1101 BUG_ON(rs_flags & LC_LOCKED);
1102 goto try_again;
1103 }
1104 if (bm_ext->lce.lc_number != enr) {
1105 bm_ext->rs_left = drbd_bm_e_weight(device, enr);
1106 bm_ext->rs_failed = 0;
1107 lc_committed(device->resync);
1108 wake_up(&device->al_wait);
1109 D_ASSERT(device, test_bit(BME_LOCKED, &bm_ext->flags) == 0);
1110 }
1111 set_bit(BME_NO_WRITES, &bm_ext->flags);
1112 D_ASSERT(device, bm_ext->lce.refcnt == 1);
1113 device->resync_locked++;
1114 goto check_al;
1115 }
1116check_al:
1117 for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
1118 if (lc_is_used(device->act_log, al_enr+i))
1119 goto try_again;
1120 }
1121 set_bit(BME_LOCKED, &bm_ext->flags);
1122proceed:
1123 device->resync_wenr = LC_FREE;
1124 spin_unlock_irq(&device->al_lock);
1125 return 0;
1126
1127try_again:
1128 if (bm_ext) {
1129 if (throttle) {
1130 D_ASSERT(device, !test_bit(BME_LOCKED, &bm_ext->flags));
1131 D_ASSERT(device, test_bit(BME_NO_WRITES, &bm_ext->flags));
1132 clear_bit(BME_NO_WRITES, &bm_ext->flags);
1133 device->resync_wenr = LC_FREE;
1134 if (lc_put(device->resync, &bm_ext->lce) == 0) {
1135 bm_ext->flags = 0;
1136 device->resync_locked--;
1137 }
1138 wake_up(&device->al_wait);
1139 } else
1140 device->resync_wenr = enr;
1141 }
1142 spin_unlock_irq(&device->al_lock);
1143 return -EAGAIN;
1144}
1145
1146void drbd_rs_complete_io(struct drbd_device *device, sector_t sector)
1147{
1148 unsigned int enr = BM_SECT_TO_EXT(sector);
1149 struct lc_element *e;
1150 struct bm_extent *bm_ext;
1151 unsigned long flags;
1152
1153 spin_lock_irqsave(&device->al_lock, flags);
1154 e = lc_find(device->resync, enr);
1155 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1156 if (!bm_ext) {
1157 spin_unlock_irqrestore(&device->al_lock, flags);
1158 if (__ratelimit(&drbd_ratelimit_state))
1159 drbd_err(device, "drbd_rs_complete_io() called, but extent not found\n");
1160 return;
1161 }
1162
1163 if (bm_ext->lce.refcnt == 0) {
1164 spin_unlock_irqrestore(&device->al_lock, flags);
1165 drbd_err(device, "drbd_rs_complete_io(,%llu [=%u]) called, "
1166 "but refcnt is 0!?\n",
1167 (unsigned long long)sector, enr);
1168 return;
1169 }
1170
1171 if (lc_put(device->resync, &bm_ext->lce) == 0) {
1172 bm_ext->flags = 0;
1173 device->resync_locked--;
1174 wake_up(&device->al_wait);
1175 }
1176
1177 spin_unlock_irqrestore(&device->al_lock, flags);
1178}
1179
1180
1181
1182
1183
1184void drbd_rs_cancel_all(struct drbd_device *device)
1185{
1186 spin_lock_irq(&device->al_lock);
1187
1188 if (get_ldev_if_state(device, D_FAILED)) {
1189 lc_reset(device->resync);
1190 put_ldev(device);
1191 }
1192 device->resync_locked = 0;
1193 device->resync_wenr = LC_FREE;
1194 spin_unlock_irq(&device->al_lock);
1195 wake_up(&device->al_wait);
1196}
1197
1198
1199
1200
1201
1202
1203
1204
1205int drbd_rs_del_all(struct drbd_device *device)
1206{
1207 struct lc_element *e;
1208 struct bm_extent *bm_ext;
1209 int i;
1210
1211 spin_lock_irq(&device->al_lock);
1212
1213 if (get_ldev_if_state(device, D_FAILED)) {
1214
1215 for (i = 0; i < device->resync->nr_elements; i++) {
1216 e = lc_element_by_index(device->resync, i);
1217 bm_ext = lc_entry(e, struct bm_extent, lce);
1218 if (bm_ext->lce.lc_number == LC_FREE)
1219 continue;
1220 if (bm_ext->lce.lc_number == device->resync_wenr) {
1221 drbd_info(device, "dropping %u in drbd_rs_del_all, apparently"
1222 " got 'synced' by application io\n",
1223 device->resync_wenr);
1224 D_ASSERT(device, !test_bit(BME_LOCKED, &bm_ext->flags));
1225 D_ASSERT(device, test_bit(BME_NO_WRITES, &bm_ext->flags));
1226 clear_bit(BME_NO_WRITES, &bm_ext->flags);
1227 device->resync_wenr = LC_FREE;
1228 lc_put(device->resync, &bm_ext->lce);
1229 }
1230 if (bm_ext->lce.refcnt != 0) {
1231 drbd_info(device, "Retrying drbd_rs_del_all() later. "
1232 "refcnt=%d\n", bm_ext->lce.refcnt);
1233 put_ldev(device);
1234 spin_unlock_irq(&device->al_lock);
1235 return -EAGAIN;
1236 }
1237 D_ASSERT(device, !test_bit(BME_LOCKED, &bm_ext->flags));
1238 D_ASSERT(device, !test_bit(BME_NO_WRITES, &bm_ext->flags));
1239 lc_del(device->resync, &bm_ext->lce);
1240 }
1241 D_ASSERT(device, device->resync->used == 0);
1242 put_ldev(device);
1243 }
1244 spin_unlock_irq(&device->al_lock);
1245 wake_up(&device->al_wait);
1246
1247 return 0;
1248}
1249