linux/drivers/block/drbd/drbd_actlog.c
<<
>>
Prefs
   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/drbd.h>
  28#include "drbd_int.h"
  29#include "drbd_wrappers.h"
  30
  31/* We maintain a trivial check sum in our on disk activity log.
  32 * With that we can ensure correct operation even when the storage
  33 * device might do a partial (last) sector write while loosing power.
  34 */
  35struct __packed al_transaction {
  36        u32       magic;
  37        u32       tr_number;
  38        struct __packed {
  39                u32 pos;
  40                u32 extent; } updates[1 + AL_EXTENTS_PT];
  41        u32       xor_sum;
  42};
  43
  44struct update_odbm_work {
  45        struct drbd_work w;
  46        unsigned int enr;
  47};
  48
  49struct update_al_work {
  50        struct drbd_work w;
  51        struct lc_element *al_ext;
  52        struct completion event;
  53        unsigned int enr;
  54        /* if old_enr != LC_FREE, write corresponding bitmap sector, too */
  55        unsigned int old_enr;
  56};
  57
  58struct drbd_atodb_wait {
  59        atomic_t           count;
  60        struct completion  io_done;
  61        struct drbd_conf   *mdev;
  62        int                error;
  63};
  64
  65
  66int w_al_write_transaction(struct drbd_conf *, struct drbd_work *, int);
  67
  68static int _drbd_md_sync_page_io(struct drbd_conf *mdev,
  69                                 struct drbd_backing_dev *bdev,
  70                                 struct page *page, sector_t sector,
  71                                 int rw, int size)
  72{
  73        struct bio *bio;
  74        struct drbd_md_io md_io;
  75        int ok;
  76
  77        md_io.mdev = mdev;
  78        init_completion(&md_io.event);
  79        md_io.error = 0;
  80
  81        if ((rw & WRITE) && !test_bit(MD_NO_BARRIER, &mdev->flags))
  82                rw |= (1 << BIO_RW_BARRIER);
  83        rw |= ((1<<BIO_RW_UNPLUG) | (1<<BIO_RW_SYNCIO));
  84
  85 retry:
  86        bio = bio_alloc(GFP_NOIO, 1);
  87        bio->bi_bdev = bdev->md_bdev;
  88        bio->bi_sector = sector;
  89        ok = (bio_add_page(bio, page, size, 0) == size);
  90        if (!ok)
  91                goto out;
  92        bio->bi_private = &md_io;
  93        bio->bi_end_io = drbd_md_io_complete;
  94        bio->bi_rw = rw;
  95
  96        if (FAULT_ACTIVE(mdev, (rw & WRITE) ? DRBD_FAULT_MD_WR : DRBD_FAULT_MD_RD))
  97                bio_endio(bio, -EIO);
  98        else
  99                submit_bio(rw, bio);
 100        wait_for_completion(&md_io.event);
 101        ok = bio_flagged(bio, BIO_UPTODATE) && md_io.error == 0;
 102
 103        /* check for unsupported barrier op.
 104         * would rather check on EOPNOTSUPP, but that is not reliable.
 105         * don't try again for ANY return value != 0 */
 106        if (unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER) && !ok)) {
 107                /* Try again with no barrier */
 108                dev_warn(DEV, "Barriers not supported on meta data device - disabling\n");
 109                set_bit(MD_NO_BARRIER, &mdev->flags);
 110                rw &= ~(1 << BIO_RW_BARRIER);
 111                bio_put(bio);
 112                goto retry;
 113        }
 114 out:
 115        bio_put(bio);
 116        return ok;
 117}
 118
 119int drbd_md_sync_page_io(struct drbd_conf *mdev, struct drbd_backing_dev *bdev,
 120                         sector_t sector, int rw)
 121{
 122        int logical_block_size, mask, ok;
 123        int offset = 0;
 124        struct page *iop = mdev->md_io_page;
 125
 126        D_ASSERT(mutex_is_locked(&mdev->md_io_mutex));
 127
 128        BUG_ON(!bdev->md_bdev);
 129
 130        logical_block_size = bdev_logical_block_size(bdev->md_bdev);
 131        if (logical_block_size == 0)
 132                logical_block_size = MD_SECTOR_SIZE;
 133
 134        /* in case logical_block_size != 512 [ s390 only? ] */
 135        if (logical_block_size != MD_SECTOR_SIZE) {
 136                mask = (logical_block_size / MD_SECTOR_SIZE) - 1;
 137                D_ASSERT(mask == 1 || mask == 3 || mask == 7);
 138                D_ASSERT(logical_block_size == (mask+1) * MD_SECTOR_SIZE);
 139                offset = sector & mask;
 140                sector = sector & ~mask;
 141                iop = mdev->md_io_tmpp;
 142
 143                if (rw & WRITE) {
 144                        /* these are GFP_KERNEL pages, pre-allocated
 145                         * on device initialization */
 146                        void *p = page_address(mdev->md_io_page);
 147                        void *hp = page_address(mdev->md_io_tmpp);
 148
 149                        ok = _drbd_md_sync_page_io(mdev, bdev, iop, sector,
 150                                        READ, logical_block_size);
 151
 152                        if (unlikely(!ok)) {
 153                                dev_err(DEV, "drbd_md_sync_page_io(,%llus,"
 154                                    "READ [logical_block_size!=512]) failed!\n",
 155                                    (unsigned long long)sector);
 156                                return 0;
 157                        }
 158
 159                        memcpy(hp + offset*MD_SECTOR_SIZE, p, MD_SECTOR_SIZE);
 160                }
 161        }
 162
 163        if (sector < drbd_md_first_sector(bdev) ||
 164            sector > drbd_md_last_sector(bdev))
 165                dev_alert(DEV, "%s [%d]:%s(,%llus,%s) out of range md access!\n",
 166                     current->comm, current->pid, __func__,
 167                     (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ");
 168
 169        ok = _drbd_md_sync_page_io(mdev, bdev, iop, sector, rw, logical_block_size);
 170        if (unlikely(!ok)) {
 171                dev_err(DEV, "drbd_md_sync_page_io(,%llus,%s) failed!\n",
 172                    (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ");
 173                return 0;
 174        }
 175
 176        if (logical_block_size != MD_SECTOR_SIZE && !(rw & WRITE)) {
 177                void *p = page_address(mdev->md_io_page);
 178                void *hp = page_address(mdev->md_io_tmpp);
 179
 180                memcpy(p, hp + offset*MD_SECTOR_SIZE, MD_SECTOR_SIZE);
 181        }
 182
 183        return ok;
 184}
 185
 186static struct lc_element *_al_get(struct drbd_conf *mdev, unsigned int enr)
 187{
 188        struct lc_element *al_ext;
 189        struct lc_element *tmp;
 190        unsigned long     al_flags = 0;
 191
 192        spin_lock_irq(&mdev->al_lock);
 193        tmp = lc_find(mdev->resync, enr/AL_EXT_PER_BM_SECT);
 194        if (unlikely(tmp != NULL)) {
 195                struct bm_extent  *bm_ext = lc_entry(tmp, struct bm_extent, lce);
 196                if (test_bit(BME_NO_WRITES, &bm_ext->flags)) {
 197                        spin_unlock_irq(&mdev->al_lock);
 198                        return NULL;
 199                }
 200        }
 201        al_ext   = lc_get(mdev->act_log, enr);
 202        al_flags = mdev->act_log->flags;
 203        spin_unlock_irq(&mdev->al_lock);
 204
 205        /*
 206        if (!al_ext) {
 207                if (al_flags & LC_STARVING)
 208                        dev_warn(DEV, "Have to wait for LRU element (AL too small?)\n");
 209                if (al_flags & LC_DIRTY)
 210                        dev_warn(DEV, "Ongoing AL update (AL device too slow?)\n");
 211        }
 212        */
 213
 214        return al_ext;
 215}
 216
 217void drbd_al_begin_io(struct drbd_conf *mdev, sector_t sector)
 218{
 219        unsigned int enr = (sector >> (AL_EXTENT_SHIFT-9));
 220        struct lc_element *al_ext;
 221        struct update_al_work al_work;
 222
 223        D_ASSERT(atomic_read(&mdev->local_cnt) > 0);
 224
 225        wait_event(mdev->al_wait, (al_ext = _al_get(mdev, enr)));
 226
 227        if (al_ext->lc_number != enr) {
 228                /* drbd_al_write_transaction(mdev,al_ext,enr);
 229                 * recurses into generic_make_request(), which
 230                 * disallows recursion, bios being serialized on the
 231                 * current->bio_tail list now.
 232                 * we have to delegate updates to the activity log
 233                 * to the worker thread. */
 234                init_completion(&al_work.event);
 235                al_work.al_ext = al_ext;
 236                al_work.enr = enr;
 237                al_work.old_enr = al_ext->lc_number;
 238                al_work.w.cb = w_al_write_transaction;
 239                drbd_queue_work_front(&mdev->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                lc_changed(mdev->act_log, al_ext);
 246                spin_unlock_irq(&mdev->al_lock);
 247                wake_up(&mdev->al_wait);
 248        }
 249}
 250
 251void drbd_al_complete_io(struct drbd_conf *mdev, sector_t sector)
 252{
 253        unsigned int enr = (sector >> (AL_EXTENT_SHIFT-9));
 254        struct lc_element *extent;
 255        unsigned long flags;
 256
 257        spin_lock_irqsave(&mdev->al_lock, flags);
 258
 259        extent = lc_find(mdev->act_log, enr);
 260
 261        if (!extent) {
 262                spin_unlock_irqrestore(&mdev->al_lock, flags);
 263                dev_err(DEV, "al_complete_io() called on inactive extent %u\n", enr);
 264                return;
 265        }
 266
 267        if (lc_put(mdev->act_log, extent) == 0)
 268                wake_up(&mdev->al_wait);
 269
 270        spin_unlock_irqrestore(&mdev->al_lock, flags);
 271}
 272
 273int
 274w_al_write_transaction(struct drbd_conf *mdev, struct drbd_work *w, int unused)
 275{
 276        struct update_al_work *aw = container_of(w, struct update_al_work, w);
 277        struct lc_element *updated = aw->al_ext;
 278        const unsigned int new_enr = aw->enr;
 279        const unsigned int evicted = aw->old_enr;
 280        struct al_transaction *buffer;
 281        sector_t sector;
 282        int i, n, mx;
 283        unsigned int extent_nr;
 284        u32 xor_sum = 0;
 285
 286        if (!get_ldev(mdev)) {
 287                dev_err(DEV, "get_ldev() failed in w_al_write_transaction\n");
 288                complete(&((struct update_al_work *)w)->event);
 289                return 1;
 290        }
 291        /* do we have to do a bitmap write, first?
 292         * TODO reduce maximum latency:
 293         * submit both bios, then wait for both,
 294         * instead of doing two synchronous sector writes. */
 295        if (mdev->state.conn < C_CONNECTED && evicted != LC_FREE)
 296                drbd_bm_write_sect(mdev, evicted/AL_EXT_PER_BM_SECT);
 297
 298        mutex_lock(&mdev->md_io_mutex); /* protects md_io_page, al_tr_cycle, ... */
 299        buffer = (struct al_transaction *)page_address(mdev->md_io_page);
 300
 301        buffer->magic = __constant_cpu_to_be32(DRBD_MAGIC);
 302        buffer->tr_number = cpu_to_be32(mdev->al_tr_number);
 303
 304        n = lc_index_of(mdev->act_log, updated);
 305
 306        buffer->updates[0].pos = cpu_to_be32(n);
 307        buffer->updates[0].extent = cpu_to_be32(new_enr);
 308
 309        xor_sum ^= new_enr;
 310
 311        mx = min_t(int, AL_EXTENTS_PT,
 312                   mdev->act_log->nr_elements - mdev->al_tr_cycle);
 313        for (i = 0; i < mx; i++) {
 314                unsigned idx = mdev->al_tr_cycle + i;
 315                extent_nr = lc_element_by_index(mdev->act_log, idx)->lc_number;
 316                buffer->updates[i+1].pos = cpu_to_be32(idx);
 317                buffer->updates[i+1].extent = cpu_to_be32(extent_nr);
 318                xor_sum ^= extent_nr;
 319        }
 320        for (; i < AL_EXTENTS_PT; i++) {
 321                buffer->updates[i+1].pos = __constant_cpu_to_be32(-1);
 322                buffer->updates[i+1].extent = __constant_cpu_to_be32(LC_FREE);
 323                xor_sum ^= LC_FREE;
 324        }
 325        mdev->al_tr_cycle += AL_EXTENTS_PT;
 326        if (mdev->al_tr_cycle >= mdev->act_log->nr_elements)
 327                mdev->al_tr_cycle = 0;
 328
 329        buffer->xor_sum = cpu_to_be32(xor_sum);
 330
 331        sector =  mdev->ldev->md.md_offset
 332                + mdev->ldev->md.al_offset + mdev->al_tr_pos;
 333
 334        if (!drbd_md_sync_page_io(mdev, mdev->ldev, sector, WRITE))
 335                drbd_chk_io_error(mdev, 1, TRUE);
 336
 337        if (++mdev->al_tr_pos >
 338            div_ceil(mdev->act_log->nr_elements, AL_EXTENTS_PT))
 339                mdev->al_tr_pos = 0;
 340
 341        D_ASSERT(mdev->al_tr_pos < MD_AL_MAX_SIZE);
 342        mdev->al_tr_number++;
 343
 344        mutex_unlock(&mdev->md_io_mutex);
 345
 346        complete(&((struct update_al_work *)w)->event);
 347        put_ldev(mdev);
 348
 349        return 1;
 350}
 351
 352/**
 353 * drbd_al_read_tr() - Read a single transaction from the on disk activity log
 354 * @mdev:       DRBD device.
 355 * @bdev:       Block device to read form.
 356 * @b:          pointer to an al_transaction.
 357 * @index:      On disk slot of the transaction to read.
 358 *
 359 * Returns -1 on IO error, 0 on checksum error and 1 upon success.
 360 */
 361static int drbd_al_read_tr(struct drbd_conf *mdev,
 362                           struct drbd_backing_dev *bdev,
 363                           struct al_transaction *b,
 364                           int index)
 365{
 366        sector_t sector;
 367        int rv, i;
 368        u32 xor_sum = 0;
 369
 370        sector = bdev->md.md_offset + bdev->md.al_offset + index;
 371
 372        /* Dont process error normally,
 373         * as this is done before disk is attached! */
 374        if (!drbd_md_sync_page_io(mdev, bdev, sector, READ))
 375                return -1;
 376
 377        rv = (be32_to_cpu(b->magic) == DRBD_MAGIC);
 378
 379        for (i = 0; i < AL_EXTENTS_PT + 1; i++)
 380                xor_sum ^= be32_to_cpu(b->updates[i].extent);
 381        rv &= (xor_sum == be32_to_cpu(b->xor_sum));
 382
 383        return rv;
 384}
 385
 386/**
 387 * drbd_al_read_log() - Restores the activity log from its on disk representation.
 388 * @mdev:       DRBD device.
 389 * @bdev:       Block device to read form.
 390 *
 391 * Returns 1 on success, returns 0 when reading the log failed due to IO errors.
 392 */
 393int drbd_al_read_log(struct drbd_conf *mdev, struct drbd_backing_dev *bdev)
 394{
 395        struct al_transaction *buffer;
 396        int i;
 397        int rv;
 398        int mx;
 399        int active_extents = 0;
 400        int transactions = 0;
 401        int found_valid = 0;
 402        int from = 0;
 403        int to = 0;
 404        u32 from_tnr = 0;
 405        u32 to_tnr = 0;
 406        u32 cnr;
 407
 408        mx = div_ceil(mdev->act_log->nr_elements, AL_EXTENTS_PT);
 409
 410        /* lock out all other meta data io for now,
 411         * and make sure the page is mapped.
 412         */
 413        mutex_lock(&mdev->md_io_mutex);
 414        buffer = page_address(mdev->md_io_page);
 415
 416        /* Find the valid transaction in the log */
 417        for (i = 0; i <= mx; i++) {
 418                rv = drbd_al_read_tr(mdev, bdev, buffer, i);
 419                if (rv == 0)
 420                        continue;
 421                if (rv == -1) {
 422                        mutex_unlock(&mdev->md_io_mutex);
 423                        return 0;
 424                }
 425                cnr = be32_to_cpu(buffer->tr_number);
 426
 427                if (++found_valid == 1) {
 428                        from = i;
 429                        to = i;
 430                        from_tnr = cnr;
 431                        to_tnr = cnr;
 432                        continue;
 433                }
 434                if ((int)cnr - (int)from_tnr < 0) {
 435                        D_ASSERT(from_tnr - cnr + i - from == mx+1);
 436                        from = i;
 437                        from_tnr = cnr;
 438                }
 439                if ((int)cnr - (int)to_tnr > 0) {
 440                        D_ASSERT(cnr - to_tnr == i - to);
 441                        to = i;
 442                        to_tnr = cnr;
 443                }
 444        }
 445
 446        if (!found_valid) {
 447                dev_warn(DEV, "No usable activity log found.\n");
 448                mutex_unlock(&mdev->md_io_mutex);
 449                return 1;
 450        }
 451
 452        /* Read the valid transactions.
 453         * dev_info(DEV, "Reading from %d to %d.\n",from,to); */
 454        i = from;
 455        while (1) {
 456                int j, pos;
 457                unsigned int extent_nr;
 458                unsigned int trn;
 459
 460                rv = drbd_al_read_tr(mdev, bdev, buffer, i);
 461                ERR_IF(rv == 0) goto cancel;
 462                if (rv == -1) {
 463                        mutex_unlock(&mdev->md_io_mutex);
 464                        return 0;
 465                }
 466
 467                trn = be32_to_cpu(buffer->tr_number);
 468
 469                spin_lock_irq(&mdev->al_lock);
 470
 471                /* This loop runs backwards because in the cyclic
 472                   elements there might be an old version of the
 473                   updated element (in slot 0). So the element in slot 0
 474                   can overwrite old versions. */
 475                for (j = AL_EXTENTS_PT; j >= 0; j--) {
 476                        pos = be32_to_cpu(buffer->updates[j].pos);
 477                        extent_nr = be32_to_cpu(buffer->updates[j].extent);
 478
 479                        if (extent_nr == LC_FREE)
 480                                continue;
 481
 482                        lc_set(mdev->act_log, extent_nr, pos);
 483                        active_extents++;
 484                }
 485                spin_unlock_irq(&mdev->al_lock);
 486
 487                transactions++;
 488
 489cancel:
 490                if (i == to)
 491                        break;
 492                i++;
 493                if (i > mx)
 494                        i = 0;
 495        }
 496
 497        mdev->al_tr_number = to_tnr+1;
 498        mdev->al_tr_pos = to;
 499        if (++mdev->al_tr_pos >
 500            div_ceil(mdev->act_log->nr_elements, AL_EXTENTS_PT))
 501                mdev->al_tr_pos = 0;
 502
 503        /* ok, we are done with it */
 504        mutex_unlock(&mdev->md_io_mutex);
 505
 506        dev_info(DEV, "Found %d transactions (%d active extents) in activity log.\n",
 507             transactions, active_extents);
 508
 509        return 1;
 510}
 511
 512static void atodb_endio(struct bio *bio, int error)
 513{
 514        struct drbd_atodb_wait *wc = bio->bi_private;
 515        struct drbd_conf *mdev = wc->mdev;
 516        struct page *page;
 517        int uptodate = bio_flagged(bio, BIO_UPTODATE);
 518
 519        /* strange behavior of some lower level drivers...
 520         * fail the request by clearing the uptodate flag,
 521         * but do not return any error?! */
 522        if (!error && !uptodate)
 523                error = -EIO;
 524
 525        drbd_chk_io_error(mdev, error, TRUE);
 526        if (error && wc->error == 0)
 527                wc->error = error;
 528
 529        if (atomic_dec_and_test(&wc->count))
 530                complete(&wc->io_done);
 531
 532        page = bio->bi_io_vec[0].bv_page;
 533        put_page(page);
 534        bio_put(bio);
 535        mdev->bm_writ_cnt++;
 536        put_ldev(mdev);
 537}
 538
 539/* sector to word */
 540#define S2W(s)  ((s)<<(BM_EXT_SHIFT-BM_BLOCK_SHIFT-LN2_BPL))
 541
 542/* activity log to on disk bitmap -- prepare bio unless that sector
 543 * is already covered by previously prepared bios */
 544static int atodb_prepare_unless_covered(struct drbd_conf *mdev,
 545                                        struct bio **bios,
 546                                        unsigned int enr,
 547                                        struct drbd_atodb_wait *wc) __must_hold(local)
 548{
 549        struct bio *bio;
 550        struct page *page;
 551        sector_t on_disk_sector;
 552        unsigned int page_offset = PAGE_SIZE;
 553        int offset;
 554        int i = 0;
 555        int err = -ENOMEM;
 556
 557        /* We always write aligned, full 4k blocks,
 558         * so we can ignore the logical_block_size (for now) */
 559        enr &= ~7U;
 560        on_disk_sector = enr + mdev->ldev->md.md_offset
 561                             + mdev->ldev->md.bm_offset;
 562
 563        D_ASSERT(!(on_disk_sector & 7U));
 564
 565        /* Check if that enr is already covered by an already created bio.
 566         * Caution, bios[] is not NULL terminated,
 567         * but only initialized to all NULL.
 568         * For completely scattered activity log,
 569         * the last invocation iterates over all bios,
 570         * and finds the last NULL entry.
 571         */
 572        while ((bio = bios[i])) {
 573                if (bio->bi_sector == on_disk_sector)
 574                        return 0;
 575                i++;
 576        }
 577        /* bios[i] == NULL, the next not yet used slot */
 578
 579        /* GFP_KERNEL, we are not in the write-out path */
 580        bio = bio_alloc(GFP_KERNEL, 1);
 581        if (bio == NULL)
 582                return -ENOMEM;
 583
 584        if (i > 0) {
 585                const struct bio_vec *prev_bv = bios[i-1]->bi_io_vec;
 586                page_offset = prev_bv->bv_offset + prev_bv->bv_len;
 587                page = prev_bv->bv_page;
 588        }
 589        if (page_offset == PAGE_SIZE) {
 590                page = alloc_page(__GFP_HIGHMEM);
 591                if (page == NULL)
 592                        goto out_bio_put;
 593                page_offset = 0;
 594        } else {
 595                get_page(page);
 596        }
 597
 598        offset = S2W(enr);
 599        drbd_bm_get_lel(mdev, offset,
 600                        min_t(size_t, S2W(8), drbd_bm_words(mdev) - offset),
 601                        kmap(page) + page_offset);
 602        kunmap(page);
 603
 604        bio->bi_private = wc;
 605        bio->bi_end_io = atodb_endio;
 606        bio->bi_bdev = mdev->ldev->md_bdev;
 607        bio->bi_sector = on_disk_sector;
 608
 609        if (bio_add_page(bio, page, 4096, page_offset) != 4096)
 610                goto out_put_page;
 611
 612        atomic_inc(&wc->count);
 613        /* we already know that we may do this...
 614         * get_ldev_if_state(mdev,D_ATTACHING);
 615         * just get the extra reference, so that the local_cnt reflects
 616         * the number of pending IO requests DRBD at its backing device.
 617         */
 618        atomic_inc(&mdev->local_cnt);
 619
 620        bios[i] = bio;
 621
 622        return 0;
 623
 624out_put_page:
 625        err = -EINVAL;
 626        put_page(page);
 627out_bio_put:
 628        bio_put(bio);
 629        return err;
 630}
 631
 632/**
 633 * drbd_al_to_on_disk_bm() -  * Writes bitmap parts covered by active AL extents
 634 * @mdev:       DRBD device.
 635 *
 636 * Called when we detach (unconfigure) local storage,
 637 * or when we go from R_PRIMARY to R_SECONDARY role.
 638 */
 639void drbd_al_to_on_disk_bm(struct drbd_conf *mdev)
 640{
 641        int i, nr_elements;
 642        unsigned int enr;
 643        struct bio **bios;
 644        struct drbd_atodb_wait wc;
 645
 646        ERR_IF (!get_ldev_if_state(mdev, D_ATTACHING))
 647                return; /* sorry, I don't have any act_log etc... */
 648
 649        wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
 650
 651        nr_elements = mdev->act_log->nr_elements;
 652
 653        /* GFP_KERNEL, we are not in anyone's write-out path */
 654        bios = kzalloc(sizeof(struct bio *) * nr_elements, GFP_KERNEL);
 655        if (!bios)
 656                goto submit_one_by_one;
 657
 658        atomic_set(&wc.count, 0);
 659        init_completion(&wc.io_done);
 660        wc.mdev = mdev;
 661        wc.error = 0;
 662
 663        for (i = 0; i < nr_elements; i++) {
 664                enr = lc_element_by_index(mdev->act_log, i)->lc_number;
 665                if (enr == LC_FREE)
 666                        continue;
 667                /* next statement also does atomic_inc wc.count and local_cnt */
 668                if (atodb_prepare_unless_covered(mdev, bios,
 669                                                enr/AL_EXT_PER_BM_SECT,
 670                                                &wc))
 671                        goto free_bios_submit_one_by_one;
 672        }
 673
 674        /* unnecessary optimization? */
 675        lc_unlock(mdev->act_log);
 676        wake_up(&mdev->al_wait);
 677
 678        /* all prepared, submit them */
 679        for (i = 0; i < nr_elements; i++) {
 680                if (bios[i] == NULL)
 681                        break;
 682                if (FAULT_ACTIVE(mdev, DRBD_FAULT_MD_WR)) {
 683                        bios[i]->bi_rw = WRITE;
 684                        bio_endio(bios[i], -EIO);
 685                } else {
 686                        submit_bio(WRITE, bios[i]);
 687                }
 688        }
 689
 690        drbd_blk_run_queue(bdev_get_queue(mdev->ldev->md_bdev));
 691
 692        /* always (try to) flush bitmap to stable storage */
 693        drbd_md_flush(mdev);
 694
 695        /* In case we did not submit a single IO do not wait for
 696         * them to complete. ( Because we would wait forever here. )
 697         *
 698         * In case we had IOs and they are already complete, there
 699         * is not point in waiting anyways.
 700         * Therefore this if () ... */
 701        if (atomic_read(&wc.count))
 702                wait_for_completion(&wc.io_done);
 703
 704        put_ldev(mdev);
 705
 706        kfree(bios);
 707        return;
 708
 709 free_bios_submit_one_by_one:
 710        /* free everything by calling the endio callback directly. */
 711        for (i = 0; i < nr_elements && bios[i]; i++)
 712                bio_endio(bios[i], 0);
 713
 714        kfree(bios);
 715
 716 submit_one_by_one:
 717        dev_warn(DEV, "Using the slow drbd_al_to_on_disk_bm()\n");
 718
 719        for (i = 0; i < mdev->act_log->nr_elements; i++) {
 720                enr = lc_element_by_index(mdev->act_log, i)->lc_number;
 721                if (enr == LC_FREE)
 722                        continue;
 723                /* Really slow: if we have al-extents 16..19 active,
 724                 * sector 4 will be written four times! Synchronous! */
 725                drbd_bm_write_sect(mdev, enr/AL_EXT_PER_BM_SECT);
 726        }
 727
 728        lc_unlock(mdev->act_log);
 729        wake_up(&mdev->al_wait);
 730        put_ldev(mdev);
 731}
 732
 733/**
 734 * drbd_al_apply_to_bm() - Sets the bitmap to diry(1) where covered ba active AL extents
 735 * @mdev:       DRBD device.
 736 */
 737void drbd_al_apply_to_bm(struct drbd_conf *mdev)
 738{
 739        unsigned int enr;
 740        unsigned long add = 0;
 741        char ppb[10];
 742        int i;
 743
 744        wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
 745
 746        for (i = 0; i < mdev->act_log->nr_elements; i++) {
 747                enr = lc_element_by_index(mdev->act_log, i)->lc_number;
 748                if (enr == LC_FREE)
 749                        continue;
 750                add += drbd_bm_ALe_set_all(mdev, enr);
 751        }
 752
 753        lc_unlock(mdev->act_log);
 754        wake_up(&mdev->al_wait);
 755
 756        dev_info(DEV, "Marked additional %s as out-of-sync based on AL.\n",
 757             ppsize(ppb, Bit2KB(add)));
 758}
 759
 760static int _try_lc_del(struct drbd_conf *mdev, struct lc_element *al_ext)
 761{
 762        int rv;
 763
 764        spin_lock_irq(&mdev->al_lock);
 765        rv = (al_ext->refcnt == 0);
 766        if (likely(rv))
 767                lc_del(mdev->act_log, al_ext);
 768        spin_unlock_irq(&mdev->al_lock);
 769
 770        return rv;
 771}
 772
 773/**
 774 * drbd_al_shrink() - Removes all active extents form the activity log
 775 * @mdev:       DRBD device.
 776 *
 777 * Removes all active extents form the activity log, waiting until
 778 * the reference count of each entry dropped to 0 first, of course.
 779 *
 780 * You need to lock mdev->act_log with lc_try_lock() / lc_unlock()
 781 */
 782void drbd_al_shrink(struct drbd_conf *mdev)
 783{
 784        struct lc_element *al_ext;
 785        int i;
 786
 787        D_ASSERT(test_bit(__LC_DIRTY, &mdev->act_log->flags));
 788
 789        for (i = 0; i < mdev->act_log->nr_elements; i++) {
 790                al_ext = lc_element_by_index(mdev->act_log, i);
 791                if (al_ext->lc_number == LC_FREE)
 792                        continue;
 793                wait_event(mdev->al_wait, _try_lc_del(mdev, al_ext));
 794        }
 795
 796        wake_up(&mdev->al_wait);
 797}
 798
 799static int w_update_odbm(struct drbd_conf *mdev, struct drbd_work *w, int unused)
 800{
 801        struct update_odbm_work *udw = container_of(w, struct update_odbm_work, w);
 802
 803        if (!get_ldev(mdev)) {
 804                if (__ratelimit(&drbd_ratelimit_state))
 805                        dev_warn(DEV, "Can not update on disk bitmap, local IO disabled.\n");
 806                kfree(udw);
 807                return 1;
 808        }
 809
 810        drbd_bm_write_sect(mdev, udw->enr);
 811        put_ldev(mdev);
 812
 813        kfree(udw);
 814
 815        if (drbd_bm_total_weight(mdev) <= mdev->rs_failed) {
 816                switch (mdev->state.conn) {
 817                case C_SYNC_SOURCE:  case C_SYNC_TARGET:
 818                case C_PAUSED_SYNC_S: case C_PAUSED_SYNC_T:
 819                        drbd_resync_finished(mdev);
 820                default:
 821                        /* nothing to do */
 822                        break;
 823                }
 824        }
 825        drbd_bcast_sync_progress(mdev);
 826
 827        return 1;
 828}
 829
 830
 831/* ATTENTION. The AL's extents are 4MB each, while the extents in the
 832 * resync LRU-cache are 16MB each.
 833 * The caller of this function has to hold an get_ldev() reference.
 834 *
 835 * TODO will be obsoleted once we have a caching lru of the on disk bitmap
 836 */
 837static void drbd_try_clear_on_disk_bm(struct drbd_conf *mdev, sector_t sector,
 838                                      int count, int success)
 839{
 840        struct lc_element *e;
 841        struct update_odbm_work *udw;
 842
 843        unsigned int enr;
 844
 845        D_ASSERT(atomic_read(&mdev->local_cnt));
 846
 847        /* I simply assume that a sector/size pair never crosses
 848         * a 16 MB extent border. (Currently this is true...) */
 849        enr = BM_SECT_TO_EXT(sector);
 850
 851        e = lc_get(mdev->resync, enr);
 852        if (e) {
 853                struct bm_extent *ext = lc_entry(e, struct bm_extent, lce);
 854                if (ext->lce.lc_number == enr) {
 855                        if (success)
 856                                ext->rs_left -= count;
 857                        else
 858                                ext->rs_failed += count;
 859                        if (ext->rs_left < ext->rs_failed) {
 860                                dev_err(DEV, "BAD! sector=%llus enr=%u rs_left=%d "
 861                                    "rs_failed=%d count=%d\n",
 862                                     (unsigned long long)sector,
 863                                     ext->lce.lc_number, ext->rs_left,
 864                                     ext->rs_failed, count);
 865                                dump_stack();
 866
 867                                lc_put(mdev->resync, &ext->lce);
 868                                drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
 869                                return;
 870                        }
 871                } else {
 872                        /* Normally this element should be in the cache,
 873                         * since drbd_rs_begin_io() pulled it already in.
 874                         *
 875                         * But maybe an application write finished, and we set
 876                         * something outside the resync lru_cache in sync.
 877                         */
 878                        int rs_left = drbd_bm_e_weight(mdev, enr);
 879                        if (ext->flags != 0) {
 880                                dev_warn(DEV, "changing resync lce: %d[%u;%02lx]"
 881                                     " -> %d[%u;00]\n",
 882                                     ext->lce.lc_number, ext->rs_left,
 883                                     ext->flags, enr, rs_left);
 884                                ext->flags = 0;
 885                        }
 886                        if (ext->rs_failed) {
 887                                dev_warn(DEV, "Kicking resync_lru element enr=%u "
 888                                     "out with rs_failed=%d\n",
 889                                     ext->lce.lc_number, ext->rs_failed);
 890                                set_bit(WRITE_BM_AFTER_RESYNC, &mdev->flags);
 891                        }
 892                        ext->rs_left = rs_left;
 893                        ext->rs_failed = success ? 0 : count;
 894                        lc_changed(mdev->resync, &ext->lce);
 895                }
 896                lc_put(mdev->resync, &ext->lce);
 897                /* no race, we are within the al_lock! */
 898
 899                if (ext->rs_left == ext->rs_failed) {
 900                        ext->rs_failed = 0;
 901
 902                        udw = kmalloc(sizeof(*udw), GFP_ATOMIC);
 903                        if (udw) {
 904                                udw->enr = ext->lce.lc_number;
 905                                udw->w.cb = w_update_odbm;
 906                                drbd_queue_work_front(&mdev->data.work, &udw->w);
 907                        } else {
 908                                dev_warn(DEV, "Could not kmalloc an udw\n");
 909                                set_bit(WRITE_BM_AFTER_RESYNC, &mdev->flags);
 910                        }
 911                }
 912        } else {
 913                dev_err(DEV, "lc_get() failed! locked=%d/%d flags=%lu\n",
 914                    mdev->resync_locked,
 915                    mdev->resync->nr_elements,
 916                    mdev->resync->flags);
 917        }
 918}
 919
 920/* clear the bit corresponding to the piece of storage in question:
 921 * size byte of data starting from sector.  Only clear a bits of the affected
 922 * one ore more _aligned_ BM_BLOCK_SIZE blocks.
 923 *
 924 * called by worker on C_SYNC_TARGET and receiver on SyncSource.
 925 *
 926 */
 927void __drbd_set_in_sync(struct drbd_conf *mdev, sector_t sector, int size,
 928                       const char *file, const unsigned int line)
 929{
 930        /* Is called from worker and receiver context _only_ */
 931        unsigned long sbnr, ebnr, lbnr;
 932        unsigned long count = 0;
 933        sector_t esector, nr_sectors;
 934        int wake_up = 0;
 935        unsigned long flags;
 936
 937        if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_SEGMENT_SIZE) {
 938                dev_err(DEV, "drbd_set_in_sync: sector=%llus size=%d nonsense!\n",
 939                                (unsigned long long)sector, size);
 940                return;
 941        }
 942        nr_sectors = drbd_get_capacity(mdev->this_bdev);
 943        esector = sector + (size >> 9) - 1;
 944
 945        ERR_IF(sector >= nr_sectors) return;
 946        ERR_IF(esector >= nr_sectors) esector = (nr_sectors-1);
 947
 948        lbnr = BM_SECT_TO_BIT(nr_sectors-1);
 949
 950        /* we clear it (in sync).
 951         * round up start sector, round down end sector.  we make sure we only
 952         * clear full, aligned, BM_BLOCK_SIZE (4K) blocks */
 953        if (unlikely(esector < BM_SECT_PER_BIT-1))
 954                return;
 955        if (unlikely(esector == (nr_sectors-1)))
 956                ebnr = lbnr;
 957        else
 958                ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1));
 959        sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1);
 960
 961        if (sbnr > ebnr)
 962                return;
 963
 964        /*
 965         * ok, (capacity & 7) != 0 sometimes, but who cares...
 966         * we count rs_{total,left} in bits, not sectors.
 967         */
 968        spin_lock_irqsave(&mdev->al_lock, flags);
 969        count = drbd_bm_clear_bits(mdev, sbnr, ebnr);
 970        if (count) {
 971                /* we need the lock for drbd_try_clear_on_disk_bm */
 972                if (jiffies - mdev->rs_mark_time > HZ*10) {
 973                        /* should be rolling marks,
 974                         * but we estimate only anyways. */
 975                        if (mdev->rs_mark_left != drbd_bm_total_weight(mdev) &&
 976                            mdev->state.conn != C_PAUSED_SYNC_T &&
 977                            mdev->state.conn != C_PAUSED_SYNC_S) {
 978                                mdev->rs_mark_time = jiffies;
 979                                mdev->rs_mark_left = drbd_bm_total_weight(mdev);
 980                        }
 981                }
 982                if (get_ldev(mdev)) {
 983                        drbd_try_clear_on_disk_bm(mdev, sector, count, TRUE);
 984                        put_ldev(mdev);
 985                }
 986                /* just wake_up unconditional now, various lc_chaged(),
 987                 * lc_put() in drbd_try_clear_on_disk_bm(). */
 988                wake_up = 1;
 989        }
 990        spin_unlock_irqrestore(&mdev->al_lock, flags);
 991        if (wake_up)
 992                wake_up(&mdev->al_wait);
 993}
 994
 995/*
 996 * this is intended to set one request worth of data out of sync.
 997 * affects at least 1 bit,
 998 * and at most 1+DRBD_MAX_SEGMENT_SIZE/BM_BLOCK_SIZE bits.
 999 *
1000 * called by tl_clear and drbd_send_dblock (==drbd_make_request).
1001 * so this can be _any_ process.
1002 */
1003void __drbd_set_out_of_sync(struct drbd_conf *mdev, sector_t sector, int size,
1004                            const char *file, const unsigned int line)
1005{
1006        unsigned long sbnr, ebnr, lbnr, flags;
1007        sector_t esector, nr_sectors;
1008        unsigned int enr, count;
1009        struct lc_element *e;
1010
1011        if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_SEGMENT_SIZE) {
1012                dev_err(DEV, "sector: %llus, size: %d\n",
1013                        (unsigned long long)sector, size);
1014                return;
1015        }
1016
1017        if (!get_ldev(mdev))
1018                return; /* no disk, no metadata, no bitmap to set bits in */
1019
1020        nr_sectors = drbd_get_capacity(mdev->this_bdev);
1021        esector = sector + (size >> 9) - 1;
1022
1023        ERR_IF(sector >= nr_sectors)
1024                goto out;
1025        ERR_IF(esector >= nr_sectors)
1026                esector = (nr_sectors-1);
1027
1028        lbnr = BM_SECT_TO_BIT(nr_sectors-1);
1029
1030        /* we set it out of sync,
1031         * we do not need to round anything here */
1032        sbnr = BM_SECT_TO_BIT(sector);
1033        ebnr = BM_SECT_TO_BIT(esector);
1034
1035        /* ok, (capacity & 7) != 0 sometimes, but who cares...
1036         * we count rs_{total,left} in bits, not sectors.  */
1037        spin_lock_irqsave(&mdev->al_lock, flags);
1038        count = drbd_bm_set_bits(mdev, sbnr, ebnr);
1039
1040        enr = BM_SECT_TO_EXT(sector);
1041        e = lc_find(mdev->resync, enr);
1042        if (e)
1043                lc_entry(e, struct bm_extent, lce)->rs_left += count;
1044        spin_unlock_irqrestore(&mdev->al_lock, flags);
1045
1046out:
1047        put_ldev(mdev);
1048}
1049
1050static
1051struct bm_extent *_bme_get(struct drbd_conf *mdev, unsigned int enr)
1052{
1053        struct lc_element *e;
1054        struct bm_extent *bm_ext;
1055        int wakeup = 0;
1056        unsigned long rs_flags;
1057
1058        spin_lock_irq(&mdev->al_lock);
1059        if (mdev->resync_locked > mdev->resync->nr_elements/2) {
1060                spin_unlock_irq(&mdev->al_lock);
1061                return NULL;
1062        }
1063        e = lc_get(mdev->resync, enr);
1064        bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1065        if (bm_ext) {
1066                if (bm_ext->lce.lc_number != enr) {
1067                        bm_ext->rs_left = drbd_bm_e_weight(mdev, enr);
1068                        bm_ext->rs_failed = 0;
1069                        lc_changed(mdev->resync, &bm_ext->lce);
1070                        wakeup = 1;
1071                }
1072                if (bm_ext->lce.refcnt == 1)
1073                        mdev->resync_locked++;
1074                set_bit(BME_NO_WRITES, &bm_ext->flags);
1075        }
1076        rs_flags = mdev->resync->flags;
1077        spin_unlock_irq(&mdev->al_lock);
1078        if (wakeup)
1079                wake_up(&mdev->al_wait);
1080
1081        if (!bm_ext) {
1082                if (rs_flags & LC_STARVING)
1083                        dev_warn(DEV, "Have to wait for element"
1084                             " (resync LRU too small?)\n");
1085                BUG_ON(rs_flags & LC_DIRTY);
1086        }
1087
1088        return bm_ext;
1089}
1090
1091static int _is_in_al(struct drbd_conf *mdev, unsigned int enr)
1092{
1093        struct lc_element *al_ext;
1094        int rv = 0;
1095
1096        spin_lock_irq(&mdev->al_lock);
1097        if (unlikely(enr == mdev->act_log->new_number))
1098                rv = 1;
1099        else {
1100                al_ext = lc_find(mdev->act_log, enr);
1101                if (al_ext) {
1102                        if (al_ext->refcnt)
1103                                rv = 1;
1104                }
1105        }
1106        spin_unlock_irq(&mdev->al_lock);
1107
1108        /*
1109        if (unlikely(rv)) {
1110                dev_info(DEV, "Delaying sync read until app's write is done\n");
1111        }
1112        */
1113        return rv;
1114}
1115
1116/**
1117 * drbd_rs_begin_io() - Gets an extent in the resync LRU cache and sets it to BME_LOCKED
1118 * @mdev:       DRBD device.
1119 * @sector:     The sector number.
1120 *
1121 * This functions sleeps on al_wait. Returns 1 on success, 0 if interrupted.
1122 */
1123int drbd_rs_begin_io(struct drbd_conf *mdev, sector_t sector)
1124{
1125        unsigned int enr = BM_SECT_TO_EXT(sector);
1126        struct bm_extent *bm_ext;
1127        int i, sig;
1128
1129        sig = wait_event_interruptible(mdev->al_wait,
1130                        (bm_ext = _bme_get(mdev, enr)));
1131        if (sig)
1132                return 0;
1133
1134        if (test_bit(BME_LOCKED, &bm_ext->flags))
1135                return 1;
1136
1137        for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
1138                sig = wait_event_interruptible(mdev->al_wait,
1139                                !_is_in_al(mdev, enr * AL_EXT_PER_BM_SECT + i));
1140                if (sig) {
1141                        spin_lock_irq(&mdev->al_lock);
1142                        if (lc_put(mdev->resync, &bm_ext->lce) == 0) {
1143                                clear_bit(BME_NO_WRITES, &bm_ext->flags);
1144                                mdev->resync_locked--;
1145                                wake_up(&mdev->al_wait);
1146                        }
1147                        spin_unlock_irq(&mdev->al_lock);
1148                        return 0;
1149                }
1150        }
1151
1152        set_bit(BME_LOCKED, &bm_ext->flags);
1153
1154        return 1;
1155}
1156
1157/**
1158 * drbd_try_rs_begin_io() - Gets an extent in the resync LRU cache, does not sleep
1159 * @mdev:       DRBD device.
1160 * @sector:     The sector number.
1161 *
1162 * Gets an extent in the resync LRU cache, sets it to BME_NO_WRITES, then
1163 * tries to set it to BME_LOCKED. Returns 0 upon success, and -EAGAIN
1164 * if there is still application IO going on in this area.
1165 */
1166int drbd_try_rs_begin_io(struct drbd_conf *mdev, sector_t sector)
1167{
1168        unsigned int enr = BM_SECT_TO_EXT(sector);
1169        const unsigned int al_enr = enr*AL_EXT_PER_BM_SECT;
1170        struct lc_element *e;
1171        struct bm_extent *bm_ext;
1172        int i;
1173
1174        spin_lock_irq(&mdev->al_lock);
1175        if (mdev->resync_wenr != LC_FREE && mdev->resync_wenr != enr) {
1176                /* in case you have very heavy scattered io, it may
1177                 * stall the syncer undefined if we give up the ref count
1178                 * when we try again and requeue.
1179                 *
1180                 * if we don't give up the refcount, but the next time
1181                 * we are scheduled this extent has been "synced" by new
1182                 * application writes, we'd miss the lc_put on the
1183                 * extent we keep the refcount on.
1184                 * so we remembered which extent we had to try again, and
1185                 * if the next requested one is something else, we do
1186                 * the lc_put here...
1187                 * we also have to wake_up
1188                 */
1189                e = lc_find(mdev->resync, mdev->resync_wenr);
1190                bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1191                if (bm_ext) {
1192                        D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
1193                        D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags));
1194                        clear_bit(BME_NO_WRITES, &bm_ext->flags);
1195                        mdev->resync_wenr = LC_FREE;
1196                        if (lc_put(mdev->resync, &bm_ext->lce) == 0)
1197                                mdev->resync_locked--;
1198                        wake_up(&mdev->al_wait);
1199                } else {
1200                        dev_alert(DEV, "LOGIC BUG\n");
1201                }
1202        }
1203        /* TRY. */
1204        e = lc_try_get(mdev->resync, enr);
1205        bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1206        if (bm_ext) {
1207                if (test_bit(BME_LOCKED, &bm_ext->flags))
1208                        goto proceed;
1209                if (!test_and_set_bit(BME_NO_WRITES, &bm_ext->flags)) {
1210                        mdev->resync_locked++;
1211                } else {
1212                        /* we did set the BME_NO_WRITES,
1213                         * but then could not set BME_LOCKED,
1214                         * so we tried again.
1215                         * drop the extra reference. */
1216                        bm_ext->lce.refcnt--;
1217                        D_ASSERT(bm_ext->lce.refcnt > 0);
1218                }
1219                goto check_al;
1220        } else {
1221                /* do we rather want to try later? */
1222                if (mdev->resync_locked > mdev->resync->nr_elements-3)
1223                        goto try_again;
1224                /* Do or do not. There is no try. -- Yoda */
1225                e = lc_get(mdev->resync, enr);
1226                bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1227                if (!bm_ext) {
1228                        const unsigned long rs_flags = mdev->resync->flags;
1229                        if (rs_flags & LC_STARVING)
1230                                dev_warn(DEV, "Have to wait for element"
1231                                     " (resync LRU too small?)\n");
1232                        BUG_ON(rs_flags & LC_DIRTY);
1233                        goto try_again;
1234                }
1235                if (bm_ext->lce.lc_number != enr) {
1236                        bm_ext->rs_left = drbd_bm_e_weight(mdev, enr);
1237                        bm_ext->rs_failed = 0;
1238                        lc_changed(mdev->resync, &bm_ext->lce);
1239                        wake_up(&mdev->al_wait);
1240                        D_ASSERT(test_bit(BME_LOCKED, &bm_ext->flags) == 0);
1241                }
1242                set_bit(BME_NO_WRITES, &bm_ext->flags);
1243                D_ASSERT(bm_ext->lce.refcnt == 1);
1244                mdev->resync_locked++;
1245                goto check_al;
1246        }
1247check_al:
1248        for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
1249                if (unlikely(al_enr+i == mdev->act_log->new_number))
1250                        goto try_again;
1251                if (lc_is_used(mdev->act_log, al_enr+i))
1252                        goto try_again;
1253        }
1254        set_bit(BME_LOCKED, &bm_ext->flags);
1255proceed:
1256        mdev->resync_wenr = LC_FREE;
1257        spin_unlock_irq(&mdev->al_lock);
1258        return 0;
1259
1260try_again:
1261        if (bm_ext)
1262                mdev->resync_wenr = enr;
1263        spin_unlock_irq(&mdev->al_lock);
1264        return -EAGAIN;
1265}
1266
1267void drbd_rs_complete_io(struct drbd_conf *mdev, sector_t sector)
1268{
1269        unsigned int enr = BM_SECT_TO_EXT(sector);
1270        struct lc_element *e;
1271        struct bm_extent *bm_ext;
1272        unsigned long flags;
1273
1274        spin_lock_irqsave(&mdev->al_lock, flags);
1275        e = lc_find(mdev->resync, enr);
1276        bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1277        if (!bm_ext) {
1278                spin_unlock_irqrestore(&mdev->al_lock, flags);
1279                if (__ratelimit(&drbd_ratelimit_state))
1280                        dev_err(DEV, "drbd_rs_complete_io() called, but extent not found\n");
1281                return;
1282        }
1283
1284        if (bm_ext->lce.refcnt == 0) {
1285                spin_unlock_irqrestore(&mdev->al_lock, flags);
1286                dev_err(DEV, "drbd_rs_complete_io(,%llu [=%u]) called, "
1287                    "but refcnt is 0!?\n",
1288                    (unsigned long long)sector, enr);
1289                return;
1290        }
1291
1292        if (lc_put(mdev->resync, &bm_ext->lce) == 0) {
1293                clear_bit(BME_LOCKED, &bm_ext->flags);
1294                clear_bit(BME_NO_WRITES, &bm_ext->flags);
1295                mdev->resync_locked--;
1296                wake_up(&mdev->al_wait);
1297        }
1298
1299        spin_unlock_irqrestore(&mdev->al_lock, flags);
1300}
1301
1302/**
1303 * drbd_rs_cancel_all() - Removes all extents from the resync LRU (even BME_LOCKED)
1304 * @mdev:       DRBD device.
1305 */
1306void drbd_rs_cancel_all(struct drbd_conf *mdev)
1307{
1308        spin_lock_irq(&mdev->al_lock);
1309
1310        if (get_ldev_if_state(mdev, D_FAILED)) { /* Makes sure ->resync is there. */
1311                lc_reset(mdev->resync);
1312                put_ldev(mdev);
1313        }
1314        mdev->resync_locked = 0;
1315        mdev->resync_wenr = LC_FREE;
1316        spin_unlock_irq(&mdev->al_lock);
1317        wake_up(&mdev->al_wait);
1318}
1319
1320/**
1321 * drbd_rs_del_all() - Gracefully remove all extents from the resync LRU
1322 * @mdev:       DRBD device.
1323 *
1324 * Returns 0 upon success, -EAGAIN if at least one reference count was
1325 * not zero.
1326 */
1327int drbd_rs_del_all(struct drbd_conf *mdev)
1328{
1329        struct lc_element *e;
1330        struct bm_extent *bm_ext;
1331        int i;
1332
1333        spin_lock_irq(&mdev->al_lock);
1334
1335        if (get_ldev_if_state(mdev, D_FAILED)) {
1336                /* ok, ->resync is there. */
1337                for (i = 0; i < mdev->resync->nr_elements; i++) {
1338                        e = lc_element_by_index(mdev->resync, i);
1339                        bm_ext = lc_entry(e, struct bm_extent, lce);
1340                        if (bm_ext->lce.lc_number == LC_FREE)
1341                                continue;
1342                        if (bm_ext->lce.lc_number == mdev->resync_wenr) {
1343                                dev_info(DEV, "dropping %u in drbd_rs_del_all, apparently"
1344                                     " got 'synced' by application io\n",
1345                                     mdev->resync_wenr);
1346                                D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
1347                                D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags));
1348                                clear_bit(BME_NO_WRITES, &bm_ext->flags);
1349                                mdev->resync_wenr = LC_FREE;
1350                                lc_put(mdev->resync, &bm_ext->lce);
1351                        }
1352                        if (bm_ext->lce.refcnt != 0) {
1353                                dev_info(DEV, "Retrying drbd_rs_del_all() later. "
1354                                     "refcnt=%d\n", bm_ext->lce.refcnt);
1355                                put_ldev(mdev);
1356                                spin_unlock_irq(&mdev->al_lock);
1357                                return -EAGAIN;
1358                        }
1359                        D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
1360                        D_ASSERT(!test_bit(BME_NO_WRITES, &bm_ext->flags));
1361                        lc_del(mdev->resync, &bm_ext->lce);
1362                }
1363                D_ASSERT(mdev->resync->used == 0);
1364                put_ldev(mdev);
1365        }
1366        spin_unlock_irq(&mdev->al_lock);
1367
1368        return 0;
1369}
1370
1371/**
1372 * drbd_rs_failed_io() - Record information on a failure to resync the specified blocks
1373 * @mdev:       DRBD device.
1374 * @sector:     The sector number.
1375 * @size:       Size of failed IO operation, in byte.
1376 */
1377void drbd_rs_failed_io(struct drbd_conf *mdev, sector_t sector, int size)
1378{
1379        /* Is called from worker and receiver context _only_ */
1380        unsigned long sbnr, ebnr, lbnr;
1381        unsigned long count;
1382        sector_t esector, nr_sectors;
1383        int wake_up = 0;
1384
1385        if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_SEGMENT_SIZE) {
1386                dev_err(DEV, "drbd_rs_failed_io: sector=%llus size=%d nonsense!\n",
1387                                (unsigned long long)sector, size);
1388                return;
1389        }
1390        nr_sectors = drbd_get_capacity(mdev->this_bdev);
1391        esector = sector + (size >> 9) - 1;
1392
1393        ERR_IF(sector >= nr_sectors) return;
1394        ERR_IF(esector >= nr_sectors) esector = (nr_sectors-1);
1395
1396        lbnr = BM_SECT_TO_BIT(nr_sectors-1);
1397
1398        /*
1399         * round up start sector, round down end sector.  we make sure we only
1400         * handle full, aligned, BM_BLOCK_SIZE (4K) blocks */
1401        if (unlikely(esector < BM_SECT_PER_BIT-1))
1402                return;
1403        if (unlikely(esector == (nr_sectors-1)))
1404                ebnr = lbnr;
1405        else
1406                ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1));
1407        sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1);
1408
1409        if (sbnr > ebnr)
1410                return;
1411
1412        /*
1413         * ok, (capacity & 7) != 0 sometimes, but who cares...
1414         * we count rs_{total,left} in bits, not sectors.
1415         */
1416        spin_lock_irq(&mdev->al_lock);
1417        count = drbd_bm_count_bits(mdev, sbnr, ebnr);
1418        if (count) {
1419                mdev->rs_failed += count;
1420
1421                if (get_ldev(mdev)) {
1422                        drbd_try_clear_on_disk_bm(mdev, sector, count, FALSE);
1423                        put_ldev(mdev);
1424                }
1425
1426                /* just wake_up unconditional now, various lc_chaged(),
1427                 * lc_put() in drbd_try_clear_on_disk_bm(). */
1428                wake_up = 1;
1429        }
1430        spin_unlock_irq(&mdev->al_lock);
1431        if (wake_up)
1432                wake_up(&mdev->al_wait);
1433}
1434
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.