Commit | Line | Data |
---|---|---|
b411b363 PR |
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> | |
7ad651b5 | 27 | #include <linux/crc32c.h> |
b411b363 | 28 | #include <linux/drbd.h> |
7ad651b5 LE |
29 | #include <linux/drbd_limits.h> |
30 | #include <linux/dynamic_debug.h> | |
b411b363 | 31 | #include "drbd_int.h" |
b411b363 PR |
32 | #include "drbd_wrappers.h" |
33 | ||
85f103d8 LE |
34 | |
35 | enum al_transaction_types { | |
36 | AL_TR_UPDATE = 0, | |
37 | AL_TR_INITIALIZED = 0xffff | |
38 | }; | |
7ad651b5 LE |
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: | |
85f103d8 LE |
52 | * purge-all, set-all-idle, set-all-active, ... to-be-defined |
53 | * see also enum al_transaction_types */ | |
7ad651b5 LE |
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]; | |
b411b363 PR |
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; | |
b411b363 | 103 | struct completion event; |
7ad651b5 | 104 | int err; |
b411b363 PR |
105 | }; |
106 | ||
1b7ab15b | 107 | static int al_write_transaction(struct drbd_conf *mdev); |
b411b363 | 108 | |
cdfda633 PR |
109 | void *drbd_md_get_buffer(struct drbd_conf *mdev) |
110 | { | |
111 | int r; | |
112 | ||
113 | wait_event(mdev->misc_wait, | |
114 | (r = atomic_cmpxchg(&mdev->md_io_in_use, 0, 1)) == 0 || | |
115 | mdev->state.disk <= D_FAILED); | |
116 | ||
117 | return r ? NULL : page_address(mdev->md_io_page); | |
118 | } | |
119 | ||
120 | void drbd_md_put_buffer(struct drbd_conf *mdev) | |
121 | { | |
122 | if (atomic_dec_and_test(&mdev->md_io_in_use)) | |
123 | wake_up(&mdev->misc_wait); | |
124 | } | |
125 | ||
126 | static bool md_io_allowed(struct drbd_conf *mdev) | |
127 | { | |
128 | enum drbd_disk_state ds = mdev->state.disk; | |
129 | return ds >= D_NEGOTIATING || ds == D_ATTACHING; | |
130 | } | |
131 | ||
132 | void wait_until_done_or_disk_failure(struct drbd_conf *mdev, unsigned int *done) | |
133 | { | |
134 | wait_event(mdev->misc_wait, *done || !md_io_allowed(mdev)); | |
135 | } | |
136 | ||
b411b363 PR |
137 | static int _drbd_md_sync_page_io(struct drbd_conf *mdev, |
138 | struct drbd_backing_dev *bdev, | |
139 | struct page *page, sector_t sector, | |
140 | int rw, int size) | |
141 | { | |
142 | struct bio *bio; | |
ac29f403 | 143 | int err; |
b411b363 | 144 | |
cdfda633 PR |
145 | mdev->md_io.done = 0; |
146 | mdev->md_io.error = -ENODEV; | |
b411b363 | 147 | |
a8a4e51e | 148 | if ((rw & WRITE) && !test_bit(MD_NO_FUA, &mdev->flags)) |
86e1e98e | 149 | rw |= REQ_FUA | REQ_FLUSH; |
721a9602 | 150 | rw |= REQ_SYNC; |
b411b363 | 151 | |
da4a75d2 | 152 | bio = bio_alloc_drbd(GFP_NOIO); |
b411b363 PR |
153 | bio->bi_bdev = bdev->md_bdev; |
154 | bio->bi_sector = sector; | |
ac29f403 AG |
155 | err = -EIO; |
156 | if (bio_add_page(bio, page, size, 0) != size) | |
b411b363 | 157 | goto out; |
cdfda633 | 158 | bio->bi_private = &mdev->md_io; |
b411b363 PR |
159 | bio->bi_end_io = drbd_md_io_complete; |
160 | bio->bi_rw = rw; | |
161 | ||
cdfda633 PR |
162 | if (!get_ldev_if_state(mdev, D_ATTACHING)) { /* Corresponding put_ldev in drbd_md_io_complete() */ |
163 | dev_err(DEV, "ASSERT FAILED: get_ldev_if_state() == 1 in _drbd_md_sync_page_io()\n"); | |
164 | err = -ENODEV; | |
165 | goto out; | |
166 | } | |
167 | ||
168 | bio_get(bio); /* one bio_put() is in the completion handler */ | |
169 | atomic_inc(&mdev->md_io_in_use); /* drbd_md_put_buffer() is in the completion handler */ | |
0cf9d27e | 170 | if (drbd_insert_fault(mdev, (rw & WRITE) ? DRBD_FAULT_MD_WR : DRBD_FAULT_MD_RD)) |
b411b363 PR |
171 | bio_endio(bio, -EIO); |
172 | else | |
173 | submit_bio(rw, bio); | |
cdfda633 | 174 | wait_until_done_or_disk_failure(mdev, &mdev->md_io.done); |
ac29f403 | 175 | if (bio_flagged(bio, BIO_UPTODATE)) |
cdfda633 | 176 | err = mdev->md_io.error; |
b411b363 | 177 | |
b411b363 PR |
178 | out: |
179 | bio_put(bio); | |
ac29f403 | 180 | return err; |
b411b363 PR |
181 | } |
182 | ||
183 | int drbd_md_sync_page_io(struct drbd_conf *mdev, struct drbd_backing_dev *bdev, | |
184 | sector_t sector, int rw) | |
185 | { | |
3fbf4d21 | 186 | int err; |
b411b363 PR |
187 | struct page *iop = mdev->md_io_page; |
188 | ||
cdfda633 | 189 | D_ASSERT(atomic_read(&mdev->md_io_in_use) == 1); |
b411b363 PR |
190 | |
191 | BUG_ON(!bdev->md_bdev); | |
192 | ||
7ad651b5 LE |
193 | dev_dbg(DEV, "meta_data io: %s [%d]:%s(,%llus,%s)\n", |
194 | current->comm, current->pid, __func__, | |
195 | (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ"); | |
b411b363 PR |
196 | |
197 | if (sector < drbd_md_first_sector(bdev) || | |
7ad651b5 | 198 | sector + 7 > drbd_md_last_sector(bdev)) |
b411b363 PR |
199 | dev_alert(DEV, "%s [%d]:%s(,%llus,%s) out of range md access!\n", |
200 | current->comm, current->pid, __func__, | |
201 | (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ"); | |
202 | ||
3fbf4d21 AG |
203 | err = _drbd_md_sync_page_io(mdev, bdev, iop, sector, rw, MD_BLOCK_SIZE); |
204 | if (err) { | |
935be260 AG |
205 | dev_err(DEV, "drbd_md_sync_page_io(,%llus,%s) failed with error %d\n", |
206 | (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ", err); | |
b411b363 | 207 | } |
3fbf4d21 | 208 | return err; |
b411b363 PR |
209 | } |
210 | ||
211 | static struct lc_element *_al_get(struct drbd_conf *mdev, unsigned int enr) | |
212 | { | |
213 | struct lc_element *al_ext; | |
214 | struct lc_element *tmp; | |
f91ab628 | 215 | int wake; |
b411b363 PR |
216 | |
217 | spin_lock_irq(&mdev->al_lock); | |
218 | tmp = lc_find(mdev->resync, enr/AL_EXT_PER_BM_SECT); | |
219 | if (unlikely(tmp != NULL)) { | |
220 | struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce); | |
221 | if (test_bit(BME_NO_WRITES, &bm_ext->flags)) { | |
f91ab628 | 222 | wake = !test_and_set_bit(BME_PRIORITY, &bm_ext->flags); |
b411b363 | 223 | spin_unlock_irq(&mdev->al_lock); |
f91ab628 PR |
224 | if (wake) |
225 | wake_up(&mdev->al_wait); | |
b411b363 PR |
226 | return NULL; |
227 | } | |
228 | } | |
46a15bc3 | 229 | al_ext = lc_get(mdev->act_log, enr); |
b411b363 | 230 | spin_unlock_irq(&mdev->al_lock); |
b411b363 PR |
231 | return al_ext; |
232 | } | |
233 | ||
181286ad | 234 | void drbd_al_begin_io(struct drbd_conf *mdev, struct drbd_interval *i) |
b411b363 | 235 | { |
7726547e LE |
236 | /* for bios crossing activity log extent boundaries, |
237 | * we may need to activate two extents in one go */ | |
e15766e9 LE |
238 | unsigned first = i->sector >> (AL_EXTENT_SHIFT-9); |
239 | unsigned last = (i->sector + (i->size >> 9) - 1) >> (AL_EXTENT_SHIFT-9); | |
240 | unsigned enr; | |
7dc1d67f LE |
241 | bool locked = false; |
242 | ||
b411b363 PR |
243 | |
244 | D_ASSERT(atomic_read(&mdev->local_cnt) > 0); | |
245 | ||
e15766e9 LE |
246 | for (enr = first; enr <= last; enr++) |
247 | wait_event(mdev->al_wait, _al_get(mdev, enr) != NULL); | |
b411b363 | 248 | |
7dc1d67f LE |
249 | /* Serialize multiple transactions. |
250 | * This uses test_and_set_bit, memory barrier is implicit. | |
251 | */ | |
252 | wait_event(mdev->al_wait, | |
253 | mdev->act_log->pending_changes == 0 || | |
254 | (locked = lc_try_lock_for_transaction(mdev->act_log))); | |
255 | ||
256 | if (locked) { | |
b411b363 PR |
257 | /* drbd_al_write_transaction(mdev,al_ext,enr); |
258 | * recurses into generic_make_request(), which | |
259 | * disallows recursion, bios being serialized on the | |
260 | * current->bio_tail list now. | |
261 | * we have to delegate updates to the activity log | |
262 | * to the worker thread. */ | |
b411b363 | 263 | |
7ad651b5 LE |
264 | /* Double check: it may have been committed by someone else, |
265 | * while we have been waiting for the lock. */ | |
e15766e9 | 266 | if (mdev->act_log->pending_changes) { |
1b7ab15b PR |
267 | int err; |
268 | err = al_write_transaction(mdev); | |
7ad651b5 LE |
269 | mdev->al_writ_cnt++; |
270 | ||
271 | spin_lock_irq(&mdev->al_lock); | |
272 | /* FIXME | |
1b7ab15b | 273 | if (err) |
7ad651b5 LE |
274 | we need an "lc_cancel" here; |
275 | */ | |
276 | lc_committed(mdev->act_log); | |
277 | spin_unlock_irq(&mdev->al_lock); | |
278 | } | |
279 | lc_unlock(mdev->act_log); | |
b411b363 PR |
280 | wake_up(&mdev->al_wait); |
281 | } | |
282 | } | |
283 | ||
181286ad | 284 | void drbd_al_complete_io(struct drbd_conf *mdev, struct drbd_interval *i) |
b411b363 | 285 | { |
e15766e9 LE |
286 | /* for bios crossing activity log extent boundaries, |
287 | * we may need to activate two extents in one go */ | |
288 | unsigned first = i->sector >> (AL_EXTENT_SHIFT-9); | |
289 | unsigned last = (i->sector + (i->size >> 9) - 1) >> (AL_EXTENT_SHIFT-9); | |
290 | unsigned enr; | |
b411b363 PR |
291 | struct lc_element *extent; |
292 | unsigned long flags; | |
e15766e9 | 293 | bool wake = false; |
b411b363 | 294 | |
b411b363 PR |
295 | spin_lock_irqsave(&mdev->al_lock, flags); |
296 | ||
e15766e9 LE |
297 | for (enr = first; enr <= last; enr++) { |
298 | extent = lc_find(mdev->act_log, enr); | |
299 | if (!extent) { | |
300 | dev_err(DEV, "al_complete_io() called on inactive extent %u\n", enr); | |
301 | continue; | |
302 | } | |
303 | if (lc_put(mdev->act_log, extent) == 0) | |
304 | wake = true; | |
b411b363 | 305 | } |
b411b363 | 306 | spin_unlock_irqrestore(&mdev->al_lock, flags); |
e15766e9 | 307 | wake_up(&mdev->al_wait); |
b411b363 PR |
308 | } |
309 | ||
19f843aa LE |
310 | #if (PAGE_SHIFT + 3) < (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT) |
311 | /* Currently BM_BLOCK_SHIFT, BM_EXT_SHIFT and AL_EXTENT_SHIFT | |
312 | * are still coupled, or assume too much about their relation. | |
313 | * Code below will not work if this is violated. | |
314 | * Will be cleaned up with some followup patch. | |
315 | */ | |
316 | # error FIXME | |
317 | #endif | |
318 | ||
319 | static unsigned int al_extent_to_bm_page(unsigned int al_enr) | |
320 | { | |
321 | return al_enr >> | |
322 | /* bit to page */ | |
323 | ((PAGE_SHIFT + 3) - | |
324 | /* al extent number to bit */ | |
325 | (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT)); | |
326 | } | |
327 | ||
328 | static unsigned int rs_extent_to_bm_page(unsigned int rs_enr) | |
329 | { | |
330 | return rs_enr >> | |
331 | /* bit to page */ | |
332 | ((PAGE_SHIFT + 3) - | |
acb104c3 | 333 | /* resync extent number to bit */ |
19f843aa LE |
334 | (BM_EXT_SHIFT - BM_BLOCK_SHIFT)); |
335 | } | |
336 | ||
99920dc5 | 337 | static int |
1b7ab15b | 338 | _al_write_transaction(struct drbd_conf *mdev) |
b411b363 | 339 | { |
7ad651b5 LE |
340 | struct al_transaction_on_disk *buffer; |
341 | struct lc_element *e; | |
b411b363 | 342 | sector_t sector; |
7ad651b5 LE |
343 | int i, mx; |
344 | unsigned extent_nr; | |
345 | unsigned crc = 0; | |
1b7ab15b | 346 | int err = 0; |
b411b363 PR |
347 | |
348 | if (!get_ldev(mdev)) { | |
7ad651b5 LE |
349 | dev_err(DEV, "disk is %s, cannot start al transaction\n", |
350 | drbd_disk_str(mdev->state.disk)); | |
1b7ab15b | 351 | return -EIO; |
b411b363 | 352 | } |
b411b363 | 353 | |
6719fb03 LE |
354 | /* The bitmap write may have failed, causing a state change. */ |
355 | if (mdev->state.disk < D_INCONSISTENT) { | |
356 | dev_err(DEV, | |
7ad651b5 LE |
357 | "disk is %s, cannot write al transaction\n", |
358 | drbd_disk_str(mdev->state.disk)); | |
6719fb03 | 359 | put_ldev(mdev); |
1b7ab15b | 360 | return -EIO; |
6719fb03 LE |
361 | } |
362 | ||
cdfda633 PR |
363 | buffer = drbd_md_get_buffer(mdev); /* protects md_io_buffer, al_tr_cycle, ... */ |
364 | if (!buffer) { | |
365 | dev_err(DEV, "disk failed while waiting for md_io buffer\n"); | |
cdfda633 | 366 | put_ldev(mdev); |
1b7ab15b | 367 | return -ENODEV; |
cdfda633 | 368 | } |
b411b363 | 369 | |
7ad651b5 LE |
370 | memset(buffer, 0, sizeof(*buffer)); |
371 | buffer->magic = cpu_to_be32(DRBD_AL_MAGIC); | |
b411b363 PR |
372 | buffer->tr_number = cpu_to_be32(mdev->al_tr_number); |
373 | ||
7ad651b5 LE |
374 | i = 0; |
375 | ||
376 | /* Even though no one can start to change this list | |
377 | * once we set the LC_LOCKED -- from drbd_al_begin_io(), | |
378 | * lc_try_lock_for_transaction() --, someone may still | |
379 | * be in the process of changing it. */ | |
380 | spin_lock_irq(&mdev->al_lock); | |
381 | list_for_each_entry(e, &mdev->act_log->to_be_changed, list) { | |
382 | if (i == AL_UPDATES_PER_TRANSACTION) { | |
383 | i++; | |
384 | break; | |
385 | } | |
386 | buffer->update_slot_nr[i] = cpu_to_be16(e->lc_index); | |
387 | buffer->update_extent_nr[i] = cpu_to_be32(e->lc_new_number); | |
388 | if (e->lc_number != LC_FREE) | |
389 | drbd_bm_mark_for_writeout(mdev, | |
390 | al_extent_to_bm_page(e->lc_number)); | |
391 | i++; | |
392 | } | |
393 | spin_unlock_irq(&mdev->al_lock); | |
394 | BUG_ON(i > AL_UPDATES_PER_TRANSACTION); | |
b411b363 | 395 | |
7ad651b5 LE |
396 | buffer->n_updates = cpu_to_be16(i); |
397 | for ( ; i < AL_UPDATES_PER_TRANSACTION; i++) { | |
398 | buffer->update_slot_nr[i] = cpu_to_be16(-1); | |
399 | buffer->update_extent_nr[i] = cpu_to_be32(LC_FREE); | |
400 | } | |
b411b363 | 401 | |
7ad651b5 LE |
402 | buffer->context_size = cpu_to_be16(mdev->act_log->nr_elements); |
403 | buffer->context_start_slot_nr = cpu_to_be16(mdev->al_tr_cycle); | |
b411b363 | 404 | |
7ad651b5 | 405 | mx = min_t(int, AL_CONTEXT_PER_TRANSACTION, |
b411b363 PR |
406 | mdev->act_log->nr_elements - mdev->al_tr_cycle); |
407 | for (i = 0; i < mx; i++) { | |
408 | unsigned idx = mdev->al_tr_cycle + i; | |
409 | extent_nr = lc_element_by_index(mdev->act_log, idx)->lc_number; | |
7ad651b5 | 410 | buffer->context[i] = cpu_to_be32(extent_nr); |
b411b363 | 411 | } |
7ad651b5 LE |
412 | for (; i < AL_CONTEXT_PER_TRANSACTION; i++) |
413 | buffer->context[i] = cpu_to_be32(LC_FREE); | |
414 | ||
415 | mdev->al_tr_cycle += AL_CONTEXT_PER_TRANSACTION; | |
b411b363 PR |
416 | if (mdev->al_tr_cycle >= mdev->act_log->nr_elements) |
417 | mdev->al_tr_cycle = 0; | |
418 | ||
b411b363 | 419 | sector = mdev->ldev->md.md_offset |
7ad651b5 LE |
420 | + mdev->ldev->md.al_offset |
421 | + mdev->al_tr_pos * (MD_BLOCK_SIZE>>9); | |
b411b363 | 422 | |
7ad651b5 LE |
423 | crc = crc32c(0, buffer, 4096); |
424 | buffer->crc32c = cpu_to_be32(crc); | |
b411b363 | 425 | |
7ad651b5 | 426 | if (drbd_bm_write_hinted(mdev)) |
1b7ab15b | 427 | err = -EIO; |
7ad651b5 | 428 | /* drbd_chk_io_error done already */ |
3fbf4d21 | 429 | else if (drbd_md_sync_page_io(mdev, mdev->ldev, sector, WRITE)) { |
1b7ab15b | 430 | err = -EIO; |
7ad651b5 LE |
431 | drbd_chk_io_error(mdev, 1, true); |
432 | } else { | |
433 | /* advance ringbuffer position and transaction counter */ | |
434 | mdev->al_tr_pos = (mdev->al_tr_pos + 1) % (MD_AL_SECTORS*512/MD_BLOCK_SIZE); | |
435 | mdev->al_tr_number++; | |
436 | } | |
b411b363 | 437 | |
cdfda633 | 438 | drbd_md_put_buffer(mdev); |
b411b363 PR |
439 | put_ldev(mdev); |
440 | ||
1b7ab15b PR |
441 | return err; |
442 | } | |
443 | ||
444 | ||
445 | static int w_al_write_transaction(struct drbd_work *w, int unused) | |
446 | { | |
447 | struct update_al_work *aw = container_of(w, struct update_al_work, w); | |
448 | struct drbd_conf *mdev = w->mdev; | |
449 | int err; | |
450 | ||
451 | err = _al_write_transaction(mdev); | |
452 | aw->err = err; | |
453 | complete(&aw->event); | |
454 | ||
455 | return err != -EIO ? err : 0; | |
456 | } | |
457 | ||
458 | /* Calls from worker context (see w_restart_disk_io()) need to write the | |
459 | transaction directly. Others came through generic_make_request(), | |
460 | those need to delegate it to the worker. */ | |
461 | static int al_write_transaction(struct drbd_conf *mdev) | |
462 | { | |
463 | struct update_al_work al_work; | |
464 | ||
465 | if (current == mdev->tconn->worker.task) | |
466 | return _al_write_transaction(mdev); | |
467 | ||
468 | init_completion(&al_work.event); | |
469 | al_work.w.cb = w_al_write_transaction; | |
470 | al_work.w.mdev = mdev; | |
471 | drbd_queue_work_front(&mdev->tconn->data.work, &al_work.w); | |
472 | wait_for_completion(&al_work.event); | |
473 | ||
474 | return al_work.err; | |
b411b363 PR |
475 | } |
476 | ||
b411b363 PR |
477 | static int _try_lc_del(struct drbd_conf *mdev, struct lc_element *al_ext) |
478 | { | |
479 | int rv; | |
480 | ||
481 | spin_lock_irq(&mdev->al_lock); | |
482 | rv = (al_ext->refcnt == 0); | |
483 | if (likely(rv)) | |
484 | lc_del(mdev->act_log, al_ext); | |
485 | spin_unlock_irq(&mdev->al_lock); | |
486 | ||
487 | return rv; | |
488 | } | |
489 | ||
490 | /** | |
491 | * drbd_al_shrink() - Removes all active extents form the activity log | |
492 | * @mdev: DRBD device. | |
493 | * | |
494 | * Removes all active extents form the activity log, waiting until | |
495 | * the reference count of each entry dropped to 0 first, of course. | |
496 | * | |
497 | * You need to lock mdev->act_log with lc_try_lock() / lc_unlock() | |
498 | */ | |
499 | void drbd_al_shrink(struct drbd_conf *mdev) | |
500 | { | |
501 | struct lc_element *al_ext; | |
502 | int i; | |
503 | ||
46a15bc3 | 504 | D_ASSERT(test_bit(__LC_LOCKED, &mdev->act_log->flags)); |
b411b363 PR |
505 | |
506 | for (i = 0; i < mdev->act_log->nr_elements; i++) { | |
507 | al_ext = lc_element_by_index(mdev->act_log, i); | |
508 | if (al_ext->lc_number == LC_FREE) | |
509 | continue; | |
510 | wait_event(mdev->al_wait, _try_lc_del(mdev, al_ext)); | |
511 | } | |
512 | ||
513 | wake_up(&mdev->al_wait); | |
514 | } | |
515 | ||
99920dc5 | 516 | static int w_update_odbm(struct drbd_work *w, int unused) |
b411b363 PR |
517 | { |
518 | struct update_odbm_work *udw = container_of(w, struct update_odbm_work, w); | |
00d56944 | 519 | struct drbd_conf *mdev = w->mdev; |
3b98c0c2 | 520 | struct sib_info sib = { .sib_reason = SIB_SYNC_PROGRESS, }; |
b411b363 PR |
521 | |
522 | if (!get_ldev(mdev)) { | |
523 | if (__ratelimit(&drbd_ratelimit_state)) | |
524 | dev_warn(DEV, "Can not update on disk bitmap, local IO disabled.\n"); | |
525 | kfree(udw); | |
99920dc5 | 526 | return 0; |
b411b363 PR |
527 | } |
528 | ||
19f843aa | 529 | drbd_bm_write_page(mdev, rs_extent_to_bm_page(udw->enr)); |
b411b363 PR |
530 | put_ldev(mdev); |
531 | ||
532 | kfree(udw); | |
533 | ||
534 | if (drbd_bm_total_weight(mdev) <= mdev->rs_failed) { | |
535 | switch (mdev->state.conn) { | |
536 | case C_SYNC_SOURCE: case C_SYNC_TARGET: | |
537 | case C_PAUSED_SYNC_S: case C_PAUSED_SYNC_T: | |
538 | drbd_resync_finished(mdev); | |
539 | default: | |
540 | /* nothing to do */ | |
541 | break; | |
542 | } | |
543 | } | |
3b98c0c2 | 544 | drbd_bcast_event(mdev, &sib); |
b411b363 | 545 | |
99920dc5 | 546 | return 0; |
b411b363 PR |
547 | } |
548 | ||
549 | ||
550 | /* ATTENTION. The AL's extents are 4MB each, while the extents in the | |
551 | * resync LRU-cache are 16MB each. | |
552 | * The caller of this function has to hold an get_ldev() reference. | |
553 | * | |
554 | * TODO will be obsoleted once we have a caching lru of the on disk bitmap | |
555 | */ | |
556 | static void drbd_try_clear_on_disk_bm(struct drbd_conf *mdev, sector_t sector, | |
557 | int count, int success) | |
558 | { | |
559 | struct lc_element *e; | |
560 | struct update_odbm_work *udw; | |
561 | ||
562 | unsigned int enr; | |
563 | ||
564 | D_ASSERT(atomic_read(&mdev->local_cnt)); | |
565 | ||
566 | /* I simply assume that a sector/size pair never crosses | |
567 | * a 16 MB extent border. (Currently this is true...) */ | |
568 | enr = BM_SECT_TO_EXT(sector); | |
569 | ||
570 | e = lc_get(mdev->resync, enr); | |
571 | if (e) { | |
572 | struct bm_extent *ext = lc_entry(e, struct bm_extent, lce); | |
573 | if (ext->lce.lc_number == enr) { | |
574 | if (success) | |
575 | ext->rs_left -= count; | |
576 | else | |
577 | ext->rs_failed += count; | |
578 | if (ext->rs_left < ext->rs_failed) { | |
579 | dev_err(DEV, "BAD! sector=%llus enr=%u rs_left=%d " | |
580 | "rs_failed=%d count=%d\n", | |
581 | (unsigned long long)sector, | |
582 | ext->lce.lc_number, ext->rs_left, | |
583 | ext->rs_failed, count); | |
584 | dump_stack(); | |
585 | ||
586 | lc_put(mdev->resync, &ext->lce); | |
38fa9988 | 587 | conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD); |
b411b363 PR |
588 | return; |
589 | } | |
590 | } else { | |
591 | /* Normally this element should be in the cache, | |
592 | * since drbd_rs_begin_io() pulled it already in. | |
593 | * | |
594 | * But maybe an application write finished, and we set | |
595 | * something outside the resync lru_cache in sync. | |
596 | */ | |
597 | int rs_left = drbd_bm_e_weight(mdev, enr); | |
598 | if (ext->flags != 0) { | |
599 | dev_warn(DEV, "changing resync lce: %d[%u;%02lx]" | |
600 | " -> %d[%u;00]\n", | |
601 | ext->lce.lc_number, ext->rs_left, | |
602 | ext->flags, enr, rs_left); | |
603 | ext->flags = 0; | |
604 | } | |
605 | if (ext->rs_failed) { | |
606 | dev_warn(DEV, "Kicking resync_lru element enr=%u " | |
607 | "out with rs_failed=%d\n", | |
608 | ext->lce.lc_number, ext->rs_failed); | |
b411b363 PR |
609 | } |
610 | ext->rs_left = rs_left; | |
611 | ext->rs_failed = success ? 0 : count; | |
46a15bc3 LE |
612 | /* we don't keep a persistent log of the resync lru, |
613 | * we can commit any change right away. */ | |
614 | lc_committed(mdev->resync); | |
b411b363 PR |
615 | } |
616 | lc_put(mdev->resync, &ext->lce); | |
617 | /* no race, we are within the al_lock! */ | |
618 | ||
619 | if (ext->rs_left == ext->rs_failed) { | |
620 | ext->rs_failed = 0; | |
621 | ||
622 | udw = kmalloc(sizeof(*udw), GFP_ATOMIC); | |
623 | if (udw) { | |
624 | udw->enr = ext->lce.lc_number; | |
625 | udw->w.cb = w_update_odbm; | |
a21e9298 | 626 | udw->w.mdev = mdev; |
e42325a5 | 627 | drbd_queue_work_front(&mdev->tconn->data.work, &udw->w); |
b411b363 PR |
628 | } else { |
629 | dev_warn(DEV, "Could not kmalloc an udw\n"); | |
b411b363 PR |
630 | } |
631 | } | |
632 | } else { | |
633 | dev_err(DEV, "lc_get() failed! locked=%d/%d flags=%lu\n", | |
634 | mdev->resync_locked, | |
635 | mdev->resync->nr_elements, | |
636 | mdev->resync->flags); | |
637 | } | |
638 | } | |
639 | ||
c6ea14df LE |
640 | void drbd_advance_rs_marks(struct drbd_conf *mdev, unsigned long still_to_go) |
641 | { | |
642 | unsigned long now = jiffies; | |
643 | unsigned long last = mdev->rs_mark_time[mdev->rs_last_mark]; | |
644 | int next = (mdev->rs_last_mark + 1) % DRBD_SYNC_MARKS; | |
645 | if (time_after_eq(now, last + DRBD_SYNC_MARK_STEP)) { | |
646 | if (mdev->rs_mark_left[mdev->rs_last_mark] != still_to_go && | |
647 | mdev->state.conn != C_PAUSED_SYNC_T && | |
648 | mdev->state.conn != C_PAUSED_SYNC_S) { | |
649 | mdev->rs_mark_time[next] = now; | |
650 | mdev->rs_mark_left[next] = still_to_go; | |
651 | mdev->rs_last_mark = next; | |
652 | } | |
653 | } | |
654 | } | |
655 | ||
b411b363 PR |
656 | /* clear the bit corresponding to the piece of storage in question: |
657 | * size byte of data starting from sector. Only clear a bits of the affected | |
658 | * one ore more _aligned_ BM_BLOCK_SIZE blocks. | |
659 | * | |
660 | * called by worker on C_SYNC_TARGET and receiver on SyncSource. | |
661 | * | |
662 | */ | |
663 | void __drbd_set_in_sync(struct drbd_conf *mdev, sector_t sector, int size, | |
664 | const char *file, const unsigned int line) | |
665 | { | |
666 | /* Is called from worker and receiver context _only_ */ | |
667 | unsigned long sbnr, ebnr, lbnr; | |
668 | unsigned long count = 0; | |
669 | sector_t esector, nr_sectors; | |
670 | int wake_up = 0; | |
671 | unsigned long flags; | |
672 | ||
c670a398 | 673 | if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) { |
b411b363 PR |
674 | dev_err(DEV, "drbd_set_in_sync: sector=%llus size=%d nonsense!\n", |
675 | (unsigned long long)sector, size); | |
676 | return; | |
677 | } | |
678 | nr_sectors = drbd_get_capacity(mdev->this_bdev); | |
679 | esector = sector + (size >> 9) - 1; | |
680 | ||
841ce241 AG |
681 | if (!expect(sector < nr_sectors)) |
682 | return; | |
683 | if (!expect(esector < nr_sectors)) | |
684 | esector = nr_sectors - 1; | |
b411b363 PR |
685 | |
686 | lbnr = BM_SECT_TO_BIT(nr_sectors-1); | |
687 | ||
688 | /* we clear it (in sync). | |
689 | * round up start sector, round down end sector. we make sure we only | |
690 | * clear full, aligned, BM_BLOCK_SIZE (4K) blocks */ | |
691 | if (unlikely(esector < BM_SECT_PER_BIT-1)) | |
692 | return; | |
693 | if (unlikely(esector == (nr_sectors-1))) | |
694 | ebnr = lbnr; | |
695 | else | |
696 | ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1)); | |
697 | sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1); | |
698 | ||
b411b363 PR |
699 | if (sbnr > ebnr) |
700 | return; | |
701 | ||
702 | /* | |
703 | * ok, (capacity & 7) != 0 sometimes, but who cares... | |
704 | * we count rs_{total,left} in bits, not sectors. | |
705 | */ | |
b411b363 | 706 | count = drbd_bm_clear_bits(mdev, sbnr, ebnr); |
1d7734a0 | 707 | if (count && get_ldev(mdev)) { |
c6ea14df | 708 | drbd_advance_rs_marks(mdev, drbd_bm_total_weight(mdev)); |
1d7734a0 | 709 | spin_lock_irqsave(&mdev->al_lock, flags); |
81e84650 | 710 | drbd_try_clear_on_disk_bm(mdev, sector, count, true); |
1d7734a0 LE |
711 | spin_unlock_irqrestore(&mdev->al_lock, flags); |
712 | ||
b411b363 PR |
713 | /* just wake_up unconditional now, various lc_chaged(), |
714 | * lc_put() in drbd_try_clear_on_disk_bm(). */ | |
715 | wake_up = 1; | |
1d7734a0 | 716 | put_ldev(mdev); |
b411b363 | 717 | } |
b411b363 PR |
718 | if (wake_up) |
719 | wake_up(&mdev->al_wait); | |
720 | } | |
721 | ||
722 | /* | |
723 | * this is intended to set one request worth of data out of sync. | |
724 | * affects at least 1 bit, | |
1816a2b4 | 725 | * and at most 1+DRBD_MAX_BIO_SIZE/BM_BLOCK_SIZE bits. |
b411b363 PR |
726 | * |
727 | * called by tl_clear and drbd_send_dblock (==drbd_make_request). | |
728 | * so this can be _any_ process. | |
729 | */ | |
73a01a18 | 730 | int __drbd_set_out_of_sync(struct drbd_conf *mdev, sector_t sector, int size, |
b411b363 PR |
731 | const char *file, const unsigned int line) |
732 | { | |
733 | unsigned long sbnr, ebnr, lbnr, flags; | |
734 | sector_t esector, nr_sectors; | |
73a01a18 | 735 | unsigned int enr, count = 0; |
b411b363 PR |
736 | struct lc_element *e; |
737 | ||
c670a398 | 738 | if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) { |
b411b363 PR |
739 | dev_err(DEV, "sector: %llus, size: %d\n", |
740 | (unsigned long long)sector, size); | |
73a01a18 | 741 | return 0; |
b411b363 PR |
742 | } |
743 | ||
744 | if (!get_ldev(mdev)) | |
73a01a18 | 745 | return 0; /* no disk, no metadata, no bitmap to set bits in */ |
b411b363 PR |
746 | |
747 | nr_sectors = drbd_get_capacity(mdev->this_bdev); | |
748 | esector = sector + (size >> 9) - 1; | |
749 | ||
841ce241 | 750 | if (!expect(sector < nr_sectors)) |
b411b363 | 751 | goto out; |
841ce241 AG |
752 | if (!expect(esector < nr_sectors)) |
753 | esector = nr_sectors - 1; | |
b411b363 PR |
754 | |
755 | lbnr = BM_SECT_TO_BIT(nr_sectors-1); | |
756 | ||
757 | /* we set it out of sync, | |
758 | * we do not need to round anything here */ | |
759 | sbnr = BM_SECT_TO_BIT(sector); | |
760 | ebnr = BM_SECT_TO_BIT(esector); | |
761 | ||
b411b363 PR |
762 | /* ok, (capacity & 7) != 0 sometimes, but who cares... |
763 | * we count rs_{total,left} in bits, not sectors. */ | |
764 | spin_lock_irqsave(&mdev->al_lock, flags); | |
765 | count = drbd_bm_set_bits(mdev, sbnr, ebnr); | |
766 | ||
767 | enr = BM_SECT_TO_EXT(sector); | |
768 | e = lc_find(mdev->resync, enr); | |
769 | if (e) | |
770 | lc_entry(e, struct bm_extent, lce)->rs_left += count; | |
771 | spin_unlock_irqrestore(&mdev->al_lock, flags); | |
772 | ||
773 | out: | |
774 | put_ldev(mdev); | |
73a01a18 PR |
775 | |
776 | return count; | |
b411b363 PR |
777 | } |
778 | ||
779 | static | |
780 | struct bm_extent *_bme_get(struct drbd_conf *mdev, unsigned int enr) | |
781 | { | |
782 | struct lc_element *e; | |
783 | struct bm_extent *bm_ext; | |
784 | int wakeup = 0; | |
785 | unsigned long rs_flags; | |
786 | ||
787 | spin_lock_irq(&mdev->al_lock); | |
788 | if (mdev->resync_locked > mdev->resync->nr_elements/2) { | |
789 | spin_unlock_irq(&mdev->al_lock); | |
790 | return NULL; | |
791 | } | |
792 | e = lc_get(mdev->resync, enr); | |
793 | bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; | |
794 | if (bm_ext) { | |
795 | if (bm_ext->lce.lc_number != enr) { | |
796 | bm_ext->rs_left = drbd_bm_e_weight(mdev, enr); | |
797 | bm_ext->rs_failed = 0; | |
46a15bc3 | 798 | lc_committed(mdev->resync); |
b411b363 PR |
799 | wakeup = 1; |
800 | } | |
801 | if (bm_ext->lce.refcnt == 1) | |
802 | mdev->resync_locked++; | |
803 | set_bit(BME_NO_WRITES, &bm_ext->flags); | |
804 | } | |
805 | rs_flags = mdev->resync->flags; | |
806 | spin_unlock_irq(&mdev->al_lock); | |
807 | if (wakeup) | |
808 | wake_up(&mdev->al_wait); | |
809 | ||
810 | if (!bm_ext) { | |
811 | if (rs_flags & LC_STARVING) | |
812 | dev_warn(DEV, "Have to wait for element" | |
813 | " (resync LRU too small?)\n"); | |
46a15bc3 | 814 | BUG_ON(rs_flags & LC_LOCKED); |
b411b363 PR |
815 | } |
816 | ||
817 | return bm_ext; | |
818 | } | |
819 | ||
820 | static int _is_in_al(struct drbd_conf *mdev, unsigned int enr) | |
821 | { | |
46a15bc3 | 822 | int rv; |
b411b363 PR |
823 | |
824 | spin_lock_irq(&mdev->al_lock); | |
46a15bc3 | 825 | rv = lc_is_used(mdev->act_log, enr); |
b411b363 PR |
826 | spin_unlock_irq(&mdev->al_lock); |
827 | ||
b411b363 PR |
828 | return rv; |
829 | } | |
830 | ||
831 | /** | |
832 | * drbd_rs_begin_io() - Gets an extent in the resync LRU cache and sets it to BME_LOCKED | |
833 | * @mdev: DRBD device. | |
834 | * @sector: The sector number. | |
835 | * | |
80a40e43 | 836 | * This functions sleeps on al_wait. Returns 0 on success, -EINTR if interrupted. |
b411b363 PR |
837 | */ |
838 | int drbd_rs_begin_io(struct drbd_conf *mdev, sector_t sector) | |
839 | { | |
840 | unsigned int enr = BM_SECT_TO_EXT(sector); | |
841 | struct bm_extent *bm_ext; | |
842 | int i, sig; | |
f91ab628 PR |
843 | int sa = 200; /* Step aside 200 times, then grab the extent and let app-IO wait. |
844 | 200 times -> 20 seconds. */ | |
b411b363 | 845 | |
f91ab628 | 846 | retry: |
b411b363 PR |
847 | sig = wait_event_interruptible(mdev->al_wait, |
848 | (bm_ext = _bme_get(mdev, enr))); | |
849 | if (sig) | |
80a40e43 | 850 | return -EINTR; |
b411b363 PR |
851 | |
852 | if (test_bit(BME_LOCKED, &bm_ext->flags)) | |
80a40e43 | 853 | return 0; |
b411b363 PR |
854 | |
855 | for (i = 0; i < AL_EXT_PER_BM_SECT; i++) { | |
856 | sig = wait_event_interruptible(mdev->al_wait, | |
f91ab628 | 857 | !_is_in_al(mdev, enr * AL_EXT_PER_BM_SECT + i) || |
c507f46f | 858 | test_bit(BME_PRIORITY, &bm_ext->flags)); |
f91ab628 PR |
859 | |
860 | if (sig || (test_bit(BME_PRIORITY, &bm_ext->flags) && sa)) { | |
b411b363 PR |
861 | spin_lock_irq(&mdev->al_lock); |
862 | if (lc_put(mdev->resync, &bm_ext->lce) == 0) { | |
f91ab628 | 863 | bm_ext->flags = 0; /* clears BME_NO_WRITES and eventually BME_PRIORITY */ |
b411b363 PR |
864 | mdev->resync_locked--; |
865 | wake_up(&mdev->al_wait); | |
866 | } | |
867 | spin_unlock_irq(&mdev->al_lock); | |
f91ab628 PR |
868 | if (sig) |
869 | return -EINTR; | |
870 | if (schedule_timeout_interruptible(HZ/10)) | |
871 | return -EINTR; | |
c507f46f PR |
872 | if (sa && --sa == 0) |
873 | dev_warn(DEV,"drbd_rs_begin_io() stepped aside for 20sec." | |
874 | "Resync stalled?\n"); | |
f91ab628 | 875 | goto retry; |
b411b363 PR |
876 | } |
877 | } | |
b411b363 | 878 | set_bit(BME_LOCKED, &bm_ext->flags); |
80a40e43 | 879 | return 0; |
b411b363 PR |
880 | } |
881 | ||
882 | /** | |
883 | * drbd_try_rs_begin_io() - Gets an extent in the resync LRU cache, does not sleep | |
884 | * @mdev: DRBD device. | |
885 | * @sector: The sector number. | |
886 | * | |
887 | * Gets an extent in the resync LRU cache, sets it to BME_NO_WRITES, then | |
888 | * tries to set it to BME_LOCKED. Returns 0 upon success, and -EAGAIN | |
889 | * if there is still application IO going on in this area. | |
890 | */ | |
891 | int drbd_try_rs_begin_io(struct drbd_conf *mdev, sector_t sector) | |
892 | { | |
893 | unsigned int enr = BM_SECT_TO_EXT(sector); | |
894 | const unsigned int al_enr = enr*AL_EXT_PER_BM_SECT; | |
895 | struct lc_element *e; | |
896 | struct bm_extent *bm_ext; | |
897 | int i; | |
898 | ||
b411b363 PR |
899 | spin_lock_irq(&mdev->al_lock); |
900 | if (mdev->resync_wenr != LC_FREE && mdev->resync_wenr != enr) { | |
901 | /* in case you have very heavy scattered io, it may | |
902 | * stall the syncer undefined if we give up the ref count | |
903 | * when we try again and requeue. | |
904 | * | |
905 | * if we don't give up the refcount, but the next time | |
906 | * we are scheduled this extent has been "synced" by new | |
907 | * application writes, we'd miss the lc_put on the | |
908 | * extent we keep the refcount on. | |
909 | * so we remembered which extent we had to try again, and | |
910 | * if the next requested one is something else, we do | |
911 | * the lc_put here... | |
912 | * we also have to wake_up | |
913 | */ | |
b411b363 PR |
914 | e = lc_find(mdev->resync, mdev->resync_wenr); |
915 | bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; | |
916 | if (bm_ext) { | |
917 | D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags)); | |
918 | D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags)); | |
919 | clear_bit(BME_NO_WRITES, &bm_ext->flags); | |
920 | mdev->resync_wenr = LC_FREE; | |
921 | if (lc_put(mdev->resync, &bm_ext->lce) == 0) | |
922 | mdev->resync_locked--; | |
923 | wake_up(&mdev->al_wait); | |
924 | } else { | |
925 | dev_alert(DEV, "LOGIC BUG\n"); | |
926 | } | |
927 | } | |
928 | /* TRY. */ | |
929 | e = lc_try_get(mdev->resync, enr); | |
930 | bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; | |
931 | if (bm_ext) { | |
932 | if (test_bit(BME_LOCKED, &bm_ext->flags)) | |
933 | goto proceed; | |
934 | if (!test_and_set_bit(BME_NO_WRITES, &bm_ext->flags)) { | |
935 | mdev->resync_locked++; | |
936 | } else { | |
937 | /* we did set the BME_NO_WRITES, | |
938 | * but then could not set BME_LOCKED, | |
939 | * so we tried again. | |
940 | * drop the extra reference. */ | |
b411b363 PR |
941 | bm_ext->lce.refcnt--; |
942 | D_ASSERT(bm_ext->lce.refcnt > 0); | |
943 | } | |
944 | goto check_al; | |
945 | } else { | |
946 | /* do we rather want to try later? */ | |
6a0afdf5 | 947 | if (mdev->resync_locked > mdev->resync->nr_elements-3) |
b411b363 | 948 | goto try_again; |
b411b363 PR |
949 | /* Do or do not. There is no try. -- Yoda */ |
950 | e = lc_get(mdev->resync, enr); | |
951 | bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; | |
952 | if (!bm_ext) { | |
953 | const unsigned long rs_flags = mdev->resync->flags; | |
954 | if (rs_flags & LC_STARVING) | |
955 | dev_warn(DEV, "Have to wait for element" | |
956 | " (resync LRU too small?)\n"); | |
46a15bc3 | 957 | BUG_ON(rs_flags & LC_LOCKED); |
b411b363 PR |
958 | goto try_again; |
959 | } | |
960 | if (bm_ext->lce.lc_number != enr) { | |
961 | bm_ext->rs_left = drbd_bm_e_weight(mdev, enr); | |
962 | bm_ext->rs_failed = 0; | |
46a15bc3 | 963 | lc_committed(mdev->resync); |
b411b363 PR |
964 | wake_up(&mdev->al_wait); |
965 | D_ASSERT(test_bit(BME_LOCKED, &bm_ext->flags) == 0); | |
966 | } | |
967 | set_bit(BME_NO_WRITES, &bm_ext->flags); | |
968 | D_ASSERT(bm_ext->lce.refcnt == 1); | |
969 | mdev->resync_locked++; | |
970 | goto check_al; | |
971 | } | |
972 | check_al: | |
b411b363 | 973 | for (i = 0; i < AL_EXT_PER_BM_SECT; i++) { |
b411b363 PR |
974 | if (lc_is_used(mdev->act_log, al_enr+i)) |
975 | goto try_again; | |
976 | } | |
977 | set_bit(BME_LOCKED, &bm_ext->flags); | |
978 | proceed: | |
979 | mdev->resync_wenr = LC_FREE; | |
980 | spin_unlock_irq(&mdev->al_lock); | |
981 | return 0; | |
982 | ||
983 | try_again: | |
b411b363 PR |
984 | if (bm_ext) |
985 | mdev->resync_wenr = enr; | |
986 | spin_unlock_irq(&mdev->al_lock); | |
987 | return -EAGAIN; | |
988 | } | |
989 | ||
990 | void drbd_rs_complete_io(struct drbd_conf *mdev, sector_t sector) | |
991 | { | |
992 | unsigned int enr = BM_SECT_TO_EXT(sector); | |
993 | struct lc_element *e; | |
994 | struct bm_extent *bm_ext; | |
995 | unsigned long flags; | |
996 | ||
b411b363 PR |
997 | spin_lock_irqsave(&mdev->al_lock, flags); |
998 | e = lc_find(mdev->resync, enr); | |
999 | bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; | |
1000 | if (!bm_ext) { | |
1001 | spin_unlock_irqrestore(&mdev->al_lock, flags); | |
1002 | if (__ratelimit(&drbd_ratelimit_state)) | |
1003 | dev_err(DEV, "drbd_rs_complete_io() called, but extent not found\n"); | |
1004 | return; | |
1005 | } | |
1006 | ||
1007 | if (bm_ext->lce.refcnt == 0) { | |
1008 | spin_unlock_irqrestore(&mdev->al_lock, flags); | |
1009 | dev_err(DEV, "drbd_rs_complete_io(,%llu [=%u]) called, " | |
1010 | "but refcnt is 0!?\n", | |
1011 | (unsigned long long)sector, enr); | |
1012 | return; | |
1013 | } | |
1014 | ||
1015 | if (lc_put(mdev->resync, &bm_ext->lce) == 0) { | |
e3555d85 | 1016 | bm_ext->flags = 0; /* clear BME_LOCKED, BME_NO_WRITES and BME_PRIORITY */ |
b411b363 PR |
1017 | mdev->resync_locked--; |
1018 | wake_up(&mdev->al_wait); | |
1019 | } | |
1020 | ||
1021 | spin_unlock_irqrestore(&mdev->al_lock, flags); | |
1022 | } | |
1023 | ||
1024 | /** | |
1025 | * drbd_rs_cancel_all() - Removes all extents from the resync LRU (even BME_LOCKED) | |
1026 | * @mdev: DRBD device. | |
1027 | */ | |
1028 | void drbd_rs_cancel_all(struct drbd_conf *mdev) | |
1029 | { | |
b411b363 PR |
1030 | spin_lock_irq(&mdev->al_lock); |
1031 | ||
1032 | if (get_ldev_if_state(mdev, D_FAILED)) { /* Makes sure ->resync is there. */ | |
1033 | lc_reset(mdev->resync); | |
1034 | put_ldev(mdev); | |
1035 | } | |
1036 | mdev->resync_locked = 0; | |
1037 | mdev->resync_wenr = LC_FREE; | |
1038 | spin_unlock_irq(&mdev->al_lock); | |
1039 | wake_up(&mdev->al_wait); | |
1040 | } | |
1041 | ||
1042 | /** | |
1043 | * drbd_rs_del_all() - Gracefully remove all extents from the resync LRU | |
1044 | * @mdev: DRBD device. | |
1045 | * | |
1046 | * Returns 0 upon success, -EAGAIN if at least one reference count was | |
1047 | * not zero. | |
1048 | */ | |
1049 | int drbd_rs_del_all(struct drbd_conf *mdev) | |
1050 | { | |
1051 | struct lc_element *e; | |
1052 | struct bm_extent *bm_ext; | |
1053 | int i; | |
1054 | ||
b411b363 PR |
1055 | spin_lock_irq(&mdev->al_lock); |
1056 | ||
1057 | if (get_ldev_if_state(mdev, D_FAILED)) { | |
1058 | /* ok, ->resync is there. */ | |
1059 | for (i = 0; i < mdev->resync->nr_elements; i++) { | |
1060 | e = lc_element_by_index(mdev->resync, i); | |
b2b163dd | 1061 | bm_ext = lc_entry(e, struct bm_extent, lce); |
b411b363 PR |
1062 | if (bm_ext->lce.lc_number == LC_FREE) |
1063 | continue; | |
1064 | if (bm_ext->lce.lc_number == mdev->resync_wenr) { | |
1065 | dev_info(DEV, "dropping %u in drbd_rs_del_all, apparently" | |
1066 | " got 'synced' by application io\n", | |
1067 | mdev->resync_wenr); | |
1068 | D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags)); | |
1069 | D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags)); | |
1070 | clear_bit(BME_NO_WRITES, &bm_ext->flags); | |
1071 | mdev->resync_wenr = LC_FREE; | |
1072 | lc_put(mdev->resync, &bm_ext->lce); | |
1073 | } | |
1074 | if (bm_ext->lce.refcnt != 0) { | |
1075 | dev_info(DEV, "Retrying drbd_rs_del_all() later. " | |
1076 | "refcnt=%d\n", bm_ext->lce.refcnt); | |
1077 | put_ldev(mdev); | |
1078 | spin_unlock_irq(&mdev->al_lock); | |
1079 | return -EAGAIN; | |
1080 | } | |
1081 | D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags)); | |
1082 | D_ASSERT(!test_bit(BME_NO_WRITES, &bm_ext->flags)); | |
1083 | lc_del(mdev->resync, &bm_ext->lce); | |
1084 | } | |
1085 | D_ASSERT(mdev->resync->used == 0); | |
1086 | put_ldev(mdev); | |
1087 | } | |
1088 | spin_unlock_irq(&mdev->al_lock); | |
1089 | ||
1090 | return 0; | |
1091 | } | |
1092 | ||
1093 | /** | |
1094 | * drbd_rs_failed_io() - Record information on a failure to resync the specified blocks | |
1095 | * @mdev: DRBD device. | |
1096 | * @sector: The sector number. | |
1097 | * @size: Size of failed IO operation, in byte. | |
1098 | */ | |
1099 | void drbd_rs_failed_io(struct drbd_conf *mdev, sector_t sector, int size) | |
1100 | { | |
1101 | /* Is called from worker and receiver context _only_ */ | |
1102 | unsigned long sbnr, ebnr, lbnr; | |
1103 | unsigned long count; | |
1104 | sector_t esector, nr_sectors; | |
1105 | int wake_up = 0; | |
1106 | ||
c670a398 | 1107 | if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) { |
b411b363 PR |
1108 | dev_err(DEV, "drbd_rs_failed_io: sector=%llus size=%d nonsense!\n", |
1109 | (unsigned long long)sector, size); | |
1110 | return; | |
1111 | } | |
1112 | nr_sectors = drbd_get_capacity(mdev->this_bdev); | |
1113 | esector = sector + (size >> 9) - 1; | |
1114 | ||
841ce241 AG |
1115 | if (!expect(sector < nr_sectors)) |
1116 | return; | |
1117 | if (!expect(esector < nr_sectors)) | |
1118 | esector = nr_sectors - 1; | |
b411b363 PR |
1119 | |
1120 | lbnr = BM_SECT_TO_BIT(nr_sectors-1); | |
1121 | ||
1122 | /* | |
1123 | * round up start sector, round down end sector. we make sure we only | |
1124 | * handle full, aligned, BM_BLOCK_SIZE (4K) blocks */ | |
1125 | if (unlikely(esector < BM_SECT_PER_BIT-1)) | |
1126 | return; | |
1127 | if (unlikely(esector == (nr_sectors-1))) | |
1128 | ebnr = lbnr; | |
1129 | else | |
1130 | ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1)); | |
1131 | sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1); | |
1132 | ||
1133 | if (sbnr > ebnr) | |
1134 | return; | |
1135 | ||
1136 | /* | |
1137 | * ok, (capacity & 7) != 0 sometimes, but who cares... | |
1138 | * we count rs_{total,left} in bits, not sectors. | |
1139 | */ | |
1140 | spin_lock_irq(&mdev->al_lock); | |
1141 | count = drbd_bm_count_bits(mdev, sbnr, ebnr); | |
1142 | if (count) { | |
1143 | mdev->rs_failed += count; | |
1144 | ||
1145 | if (get_ldev(mdev)) { | |
81e84650 | 1146 | drbd_try_clear_on_disk_bm(mdev, sector, count, false); |
b411b363 PR |
1147 | put_ldev(mdev); |
1148 | } | |
1149 | ||
1150 | /* just wake_up unconditional now, various lc_chaged(), | |
1151 | * lc_put() in drbd_try_clear_on_disk_bm(). */ | |
1152 | wake_up = 1; | |
1153 | } | |
1154 | spin_unlock_irq(&mdev->al_lock); | |
1155 | if (wake_up) | |
1156 | wake_up(&mdev->al_wait); | |
1157 | } |