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 | 32 | |
85f103d8 LE |
33 | |
34 | enum al_transaction_types { | |
35 | AL_TR_UPDATE = 0, | |
36 | AL_TR_INITIALIZED = 0xffff | |
37 | }; | |
7ad651b5 LE |
38 | /* all fields on disc in big endian */ |
39 | struct __packed al_transaction_on_disk { | |
40 | /* don't we all like magic */ | |
41 | __be32 magic; | |
42 | ||
43 | /* to identify the most recent transaction block | |
44 | * in the on disk ring buffer */ | |
45 | __be32 tr_number; | |
46 | ||
47 | /* checksum on the full 4k block, with this field set to 0. */ | |
48 | __be32 crc32c; | |
49 | ||
50 | /* type of transaction, special transaction types like: | |
85f103d8 LE |
51 | * purge-all, set-all-idle, set-all-active, ... to-be-defined |
52 | * see also enum al_transaction_types */ | |
7ad651b5 LE |
53 | __be16 transaction_type; |
54 | ||
55 | /* we currently allow only a few thousand extents, | |
56 | * so 16bit will be enough for the slot number. */ | |
57 | ||
58 | /* how many updates in this transaction */ | |
59 | __be16 n_updates; | |
60 | ||
61 | /* maximum slot number, "al-extents" in drbd.conf speak. | |
62 | * Having this in each transaction should make reconfiguration | |
63 | * of that parameter easier. */ | |
64 | __be16 context_size; | |
65 | ||
66 | /* slot number the context starts with */ | |
67 | __be16 context_start_slot_nr; | |
68 | ||
69 | /* Some reserved bytes. Expected usage is a 64bit counter of | |
70 | * sectors-written since device creation, and other data generation tag | |
71 | * supporting usage */ | |
72 | __be32 __reserved[4]; | |
73 | ||
74 | /* --- 36 byte used --- */ | |
75 | ||
76 | /* Reserve space for up to AL_UPDATES_PER_TRANSACTION changes | |
77 | * in one transaction, then use the remaining byte in the 4k block for | |
78 | * context information. "Flexible" number of updates per transaction | |
79 | * does not help, as we have to account for the case when all update | |
80 | * slots are used anyways, so it would only complicate code without | |
81 | * additional benefit. | |
82 | */ | |
83 | __be16 update_slot_nr[AL_UPDATES_PER_TRANSACTION]; | |
84 | ||
85 | /* but the extent number is 32bit, which at an extent size of 4 MiB | |
86 | * allows to cover device sizes of up to 2**54 Byte (16 PiB) */ | |
87 | __be32 update_extent_nr[AL_UPDATES_PER_TRANSACTION]; | |
88 | ||
89 | /* --- 420 bytes used (36 + 64*6) --- */ | |
90 | ||
91 | /* 4096 - 420 = 3676 = 919 * 4 */ | |
92 | __be32 context[AL_CONTEXT_PER_TRANSACTION]; | |
b411b363 PR |
93 | }; |
94 | ||
e37d2438 | 95 | void *drbd_md_get_buffer(struct drbd_device *device, const char *intent) |
cdfda633 PR |
96 | { |
97 | int r; | |
98 | ||
b30ab791 | 99 | wait_event(device->misc_wait, |
e37d2438 | 100 | (r = atomic_cmpxchg(&device->md_io.in_use, 0, 1)) == 0 || |
b30ab791 | 101 | device->state.disk <= D_FAILED); |
cdfda633 | 102 | |
e37d2438 LE |
103 | if (r) |
104 | return NULL; | |
105 | ||
106 | device->md_io.current_use = intent; | |
107 | device->md_io.start_jif = jiffies; | |
108 | device->md_io.submit_jif = device->md_io.start_jif - 1; | |
109 | return page_address(device->md_io.page); | |
cdfda633 PR |
110 | } |
111 | ||
b30ab791 | 112 | void drbd_md_put_buffer(struct drbd_device *device) |
cdfda633 | 113 | { |
e37d2438 | 114 | if (atomic_dec_and_test(&device->md_io.in_use)) |
b30ab791 | 115 | wake_up(&device->misc_wait); |
cdfda633 PR |
116 | } |
117 | ||
b30ab791 | 118 | void wait_until_done_or_force_detached(struct drbd_device *device, struct drbd_backing_dev *bdev, |
32db80f6 | 119 | unsigned int *done) |
cdfda633 | 120 | { |
32db80f6 PR |
121 | long dt; |
122 | ||
123 | rcu_read_lock(); | |
124 | dt = rcu_dereference(bdev->disk_conf)->disk_timeout; | |
125 | rcu_read_unlock(); | |
126 | dt = dt * HZ / 10; | |
127 | if (dt == 0) | |
128 | dt = MAX_SCHEDULE_TIMEOUT; | |
129 | ||
b30ab791 AG |
130 | dt = wait_event_timeout(device->misc_wait, |
131 | *done || test_bit(FORCE_DETACH, &device->flags), dt); | |
e34b677d | 132 | if (dt == 0) { |
d0180171 | 133 | drbd_err(device, "meta-data IO operation timed out\n"); |
b30ab791 | 134 | drbd_chk_io_error(device, 1, DRBD_FORCE_DETACH); |
e34b677d | 135 | } |
cdfda633 PR |
136 | } |
137 | ||
b30ab791 | 138 | static int _drbd_md_sync_page_io(struct drbd_device *device, |
b411b363 | 139 | struct drbd_backing_dev *bdev, |
bb3cc85e | 140 | sector_t sector, int op) |
b411b363 PR |
141 | { |
142 | struct bio *bio; | |
193cb00c LE |
143 | /* we do all our meta data IO in aligned 4k blocks. */ |
144 | const int size = 4096; | |
bb3cc85e | 145 | int err, op_flags = 0; |
b411b363 | 146 | |
b30ab791 AG |
147 | device->md_io.done = 0; |
148 | device->md_io.error = -ENODEV; | |
b411b363 | 149 | |
bb3cc85e | 150 | if ((op == REQ_OP_WRITE) && !test_bit(MD_NO_FUA, &device->flags)) |
28a8f0d3 | 151 | op_flags |= REQ_FUA | REQ_PREFLUSH; |
bb3cc85e | 152 | op_flags |= REQ_SYNC | REQ_NOIDLE; |
b411b363 | 153 | |
da4a75d2 | 154 | bio = bio_alloc_drbd(GFP_NOIO); |
b411b363 | 155 | bio->bi_bdev = bdev->md_bdev; |
4f024f37 | 156 | bio->bi_iter.bi_sector = sector; |
ac29f403 | 157 | err = -EIO; |
193cb00c | 158 | if (bio_add_page(bio, device->md_io.page, size, 0) != size) |
b411b363 | 159 | goto out; |
e37d2438 | 160 | bio->bi_private = device; |
ed15b795 | 161 | bio->bi_end_io = drbd_md_endio; |
bb3cc85e | 162 | bio_set_op_attrs(bio, op, op_flags); |
b411b363 | 163 | |
bb3cc85e | 164 | if (op != REQ_OP_WRITE && device->state.disk == D_DISKLESS && device->ldev == NULL) |
c04ccaa6 LE |
165 | /* special case, drbd_md_read() during drbd_adm_attach(): no get_ldev */ |
166 | ; | |
b30ab791 | 167 | else if (!get_ldev_if_state(device, D_ATTACHING)) { |
ed15b795 | 168 | /* Corresponding put_ldev in drbd_md_endio() */ |
d0180171 | 169 | drbd_err(device, "ASSERT FAILED: get_ldev_if_state() == 1 in _drbd_md_sync_page_io()\n"); |
cdfda633 PR |
170 | err = -ENODEV; |
171 | goto out; | |
172 | } | |
173 | ||
174 | bio_get(bio); /* one bio_put() is in the completion handler */ | |
e37d2438 LE |
175 | atomic_inc(&device->md_io.in_use); /* drbd_md_put_buffer() is in the completion handler */ |
176 | device->md_io.submit_jif = jiffies; | |
bb3cc85e | 177 | if (drbd_insert_fault(device, (op == REQ_OP_WRITE) ? DRBD_FAULT_MD_WR : DRBD_FAULT_MD_RD)) |
4246a0b6 | 178 | bio_io_error(bio); |
b411b363 | 179 | else |
4e49ea4a | 180 | submit_bio(bio); |
b30ab791 | 181 | wait_until_done_or_force_detached(device, bdev, &device->md_io.done); |
4246a0b6 | 182 | if (!bio->bi_error) |
b30ab791 | 183 | err = device->md_io.error; |
b411b363 | 184 | |
b411b363 PR |
185 | out: |
186 | bio_put(bio); | |
ac29f403 | 187 | return err; |
b411b363 PR |
188 | } |
189 | ||
b30ab791 | 190 | int drbd_md_sync_page_io(struct drbd_device *device, struct drbd_backing_dev *bdev, |
bb3cc85e | 191 | sector_t sector, int op) |
b411b363 | 192 | { |
3fbf4d21 | 193 | int err; |
e37d2438 | 194 | D_ASSERT(device, atomic_read(&device->md_io.in_use) == 1); |
b411b363 PR |
195 | |
196 | BUG_ON(!bdev->md_bdev); | |
197 | ||
e4d7d6f4 | 198 | dynamic_drbd_dbg(device, "meta_data io: %s [%d]:%s(,%llus,%s) %pS\n", |
7ad651b5 | 199 | current->comm, current->pid, __func__, |
bb3cc85e | 200 | (unsigned long long)sector, (op == REQ_OP_WRITE) ? "WRITE" : "READ", |
c04ccaa6 | 201 | (void*)_RET_IP_ ); |
b411b363 PR |
202 | |
203 | if (sector < drbd_md_first_sector(bdev) || | |
7ad651b5 | 204 | sector + 7 > drbd_md_last_sector(bdev)) |
d0180171 | 205 | drbd_alert(device, "%s [%d]:%s(,%llus,%s) out of range md access!\n", |
b411b363 | 206 | current->comm, current->pid, __func__, |
bb3cc85e MC |
207 | (unsigned long long)sector, |
208 | (op == REQ_OP_WRITE) ? "WRITE" : "READ"); | |
b411b363 | 209 | |
bb3cc85e | 210 | err = _drbd_md_sync_page_io(device, bdev, sector, op); |
3fbf4d21 | 211 | if (err) { |
d0180171 | 212 | drbd_err(device, "drbd_md_sync_page_io(,%llus,%s) failed with error %d\n", |
bb3cc85e MC |
213 | (unsigned long long)sector, |
214 | (op == REQ_OP_WRITE) ? "WRITE" : "READ", err); | |
b411b363 | 215 | } |
3fbf4d21 | 216 | return err; |
b411b363 PR |
217 | } |
218 | ||
b30ab791 | 219 | static struct bm_extent *find_active_resync_extent(struct drbd_device *device, unsigned int enr) |
b411b363 | 220 | { |
b411b363 | 221 | struct lc_element *tmp; |
b30ab791 | 222 | tmp = lc_find(device->resync, enr/AL_EXT_PER_BM_SECT); |
b411b363 PR |
223 | if (unlikely(tmp != NULL)) { |
224 | struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce); | |
6c3c4355 LE |
225 | if (test_bit(BME_NO_WRITES, &bm_ext->flags)) |
226 | return bm_ext; | |
b411b363 | 227 | } |
6c3c4355 LE |
228 | return NULL; |
229 | } | |
230 | ||
b30ab791 | 231 | static struct lc_element *_al_get(struct drbd_device *device, unsigned int enr, bool nonblock) |
6c3c4355 LE |
232 | { |
233 | struct lc_element *al_ext; | |
234 | struct bm_extent *bm_ext; | |
235 | int wake; | |
236 | ||
b30ab791 AG |
237 | spin_lock_irq(&device->al_lock); |
238 | bm_ext = find_active_resync_extent(device, enr); | |
6c3c4355 LE |
239 | if (bm_ext) { |
240 | wake = !test_and_set_bit(BME_PRIORITY, &bm_ext->flags); | |
b30ab791 | 241 | spin_unlock_irq(&device->al_lock); |
6c3c4355 | 242 | if (wake) |
b30ab791 | 243 | wake_up(&device->al_wait); |
6c3c4355 LE |
244 | return NULL; |
245 | } | |
246 | if (nonblock) | |
b30ab791 | 247 | al_ext = lc_try_get(device->act_log, enr); |
6c3c4355 | 248 | else |
b30ab791 AG |
249 | al_ext = lc_get(device->act_log, enr); |
250 | spin_unlock_irq(&device->al_lock); | |
b411b363 PR |
251 | return al_ext; |
252 | } | |
253 | ||
b30ab791 | 254 | bool drbd_al_begin_io_fastpath(struct drbd_device *device, struct drbd_interval *i) |
b411b363 | 255 | { |
7726547e LE |
256 | /* for bios crossing activity log extent boundaries, |
257 | * we may need to activate two extents in one go */ | |
e15766e9 | 258 | unsigned first = i->sector >> (AL_EXTENT_SHIFT-9); |
81a3537a | 259 | unsigned last = i->size == 0 ? first : (i->sector + (i->size >> 9) - 1) >> (AL_EXTENT_SHIFT-9); |
7dc1d67f | 260 | |
0b0ba1ef AG |
261 | D_ASSERT(device, (unsigned)(last - first) <= 1); |
262 | D_ASSERT(device, atomic_read(&device->local_cnt) > 0); | |
b5bc8e08 LE |
263 | |
264 | /* FIXME figure out a fast path for bios crossing AL extent boundaries */ | |
265 | if (first != last) | |
266 | return false; | |
267 | ||
b30ab791 | 268 | return _al_get(device, first, true); |
b5bc8e08 | 269 | } |
56392d2f | 270 | |
b30ab791 | 271 | bool drbd_al_begin_io_prepare(struct drbd_device *device, struct drbd_interval *i) |
b5bc8e08 LE |
272 | { |
273 | /* for bios crossing activity log extent boundaries, | |
274 | * we may need to activate two extents in one go */ | |
275 | unsigned first = i->sector >> (AL_EXTENT_SHIFT-9); | |
276 | unsigned last = i->size == 0 ? first : (i->sector + (i->size >> 9) - 1) >> (AL_EXTENT_SHIFT-9); | |
277 | unsigned enr; | |
278 | bool need_transaction = false; | |
b411b363 | 279 | |
0b0ba1ef AG |
280 | D_ASSERT(device, first <= last); |
281 | D_ASSERT(device, atomic_read(&device->local_cnt) > 0); | |
b411b363 | 282 | |
ebfd5d8f LE |
283 | for (enr = first; enr <= last; enr++) { |
284 | struct lc_element *al_ext; | |
b30ab791 AG |
285 | wait_event(device->al_wait, |
286 | (al_ext = _al_get(device, enr, false)) != NULL); | |
ebfd5d8f LE |
287 | if (al_ext->lc_number != enr) |
288 | need_transaction = true; | |
289 | } | |
b5bc8e08 LE |
290 | return need_transaction; |
291 | } | |
292 | ||
603ee2c8 LE |
293 | #if (PAGE_SHIFT + 3) < (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT) |
294 | /* Currently BM_BLOCK_SHIFT, BM_EXT_SHIFT and AL_EXTENT_SHIFT | |
295 | * are still coupled, or assume too much about their relation. | |
296 | * Code below will not work if this is violated. | |
297 | * Will be cleaned up with some followup patch. | |
298 | */ | |
299 | # error FIXME | |
300 | #endif | |
301 | ||
302 | static unsigned int al_extent_to_bm_page(unsigned int al_enr) | |
303 | { | |
304 | return al_enr >> | |
305 | /* bit to page */ | |
306 | ((PAGE_SHIFT + 3) - | |
307 | /* al extent number to bit */ | |
308 | (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT)); | |
309 | } | |
310 | ||
311 | static sector_t al_tr_number_to_on_disk_sector(struct drbd_device *device) | |
312 | { | |
313 | const unsigned int stripes = device->ldev->md.al_stripes; | |
314 | const unsigned int stripe_size_4kB = device->ldev->md.al_stripe_size_4k; | |
315 | ||
316 | /* transaction number, modulo on-disk ring buffer wrap around */ | |
317 | unsigned int t = device->al_tr_number % (device->ldev->md.al_size_4k); | |
318 | ||
319 | /* ... to aligned 4k on disk block */ | |
320 | t = ((t % stripes) * stripe_size_4kB) + t/stripes; | |
321 | ||
322 | /* ... to 512 byte sector in activity log */ | |
323 | t *= 8; | |
324 | ||
325 | /* ... plus offset to the on disk position */ | |
326 | return device->ldev->md.md_offset + device->ldev->md.al_offset + t; | |
327 | } | |
328 | ||
329 | static int __al_write_transaction(struct drbd_device *device, struct al_transaction_on_disk *buffer) | |
330 | { | |
331 | struct lc_element *e; | |
332 | sector_t sector; | |
333 | int i, mx; | |
334 | unsigned extent_nr; | |
335 | unsigned crc = 0; | |
336 | int err = 0; | |
337 | ||
338 | memset(buffer, 0, sizeof(*buffer)); | |
339 | buffer->magic = cpu_to_be32(DRBD_AL_MAGIC); | |
340 | buffer->tr_number = cpu_to_be32(device->al_tr_number); | |
341 | ||
342 | i = 0; | |
343 | ||
344 | /* Even though no one can start to change this list | |
345 | * once we set the LC_LOCKED -- from drbd_al_begin_io(), | |
346 | * lc_try_lock_for_transaction() --, someone may still | |
347 | * be in the process of changing it. */ | |
348 | spin_lock_irq(&device->al_lock); | |
349 | list_for_each_entry(e, &device->act_log->to_be_changed, list) { | |
350 | if (i == AL_UPDATES_PER_TRANSACTION) { | |
351 | i++; | |
352 | break; | |
353 | } | |
354 | buffer->update_slot_nr[i] = cpu_to_be16(e->lc_index); | |
355 | buffer->update_extent_nr[i] = cpu_to_be32(e->lc_new_number); | |
356 | if (e->lc_number != LC_FREE) | |
357 | drbd_bm_mark_for_writeout(device, | |
358 | al_extent_to_bm_page(e->lc_number)); | |
359 | i++; | |
360 | } | |
361 | spin_unlock_irq(&device->al_lock); | |
362 | BUG_ON(i > AL_UPDATES_PER_TRANSACTION); | |
363 | ||
364 | buffer->n_updates = cpu_to_be16(i); | |
365 | for ( ; i < AL_UPDATES_PER_TRANSACTION; i++) { | |
366 | buffer->update_slot_nr[i] = cpu_to_be16(-1); | |
367 | buffer->update_extent_nr[i] = cpu_to_be32(LC_FREE); | |
368 | } | |
369 | ||
370 | buffer->context_size = cpu_to_be16(device->act_log->nr_elements); | |
371 | buffer->context_start_slot_nr = cpu_to_be16(device->al_tr_cycle); | |
372 | ||
373 | mx = min_t(int, AL_CONTEXT_PER_TRANSACTION, | |
374 | device->act_log->nr_elements - device->al_tr_cycle); | |
375 | for (i = 0; i < mx; i++) { | |
376 | unsigned idx = device->al_tr_cycle + i; | |
377 | extent_nr = lc_element_by_index(device->act_log, idx)->lc_number; | |
378 | buffer->context[i] = cpu_to_be32(extent_nr); | |
379 | } | |
380 | for (; i < AL_CONTEXT_PER_TRANSACTION; i++) | |
381 | buffer->context[i] = cpu_to_be32(LC_FREE); | |
382 | ||
383 | device->al_tr_cycle += AL_CONTEXT_PER_TRANSACTION; | |
384 | if (device->al_tr_cycle >= device->act_log->nr_elements) | |
385 | device->al_tr_cycle = 0; | |
386 | ||
387 | sector = al_tr_number_to_on_disk_sector(device); | |
388 | ||
389 | crc = crc32c(0, buffer, 4096); | |
390 | buffer->crc32c = cpu_to_be32(crc); | |
391 | ||
392 | if (drbd_bm_write_hinted(device)) | |
393 | err = -EIO; | |
394 | else { | |
395 | bool write_al_updates; | |
396 | rcu_read_lock(); | |
397 | write_al_updates = rcu_dereference(device->ldev->disk_conf)->al_updates; | |
398 | rcu_read_unlock(); | |
399 | if (write_al_updates) { | |
400 | if (drbd_md_sync_page_io(device, device->ldev, sector, WRITE)) { | |
401 | err = -EIO; | |
402 | drbd_chk_io_error(device, 1, DRBD_META_IO_ERROR); | |
403 | } else { | |
404 | device->al_tr_number++; | |
405 | device->al_writ_cnt++; | |
406 | } | |
407 | } | |
408 | } | |
409 | ||
410 | return err; | |
411 | } | |
412 | ||
413 | static int al_write_transaction(struct drbd_device *device) | |
414 | { | |
415 | struct al_transaction_on_disk *buffer; | |
416 | int err; | |
417 | ||
418 | if (!get_ldev(device)) { | |
419 | drbd_err(device, "disk is %s, cannot start al transaction\n", | |
420 | drbd_disk_str(device->state.disk)); | |
421 | return -EIO; | |
422 | } | |
423 | ||
424 | /* The bitmap write may have failed, causing a state change. */ | |
425 | if (device->state.disk < D_INCONSISTENT) { | |
426 | drbd_err(device, | |
427 | "disk is %s, cannot write al transaction\n", | |
428 | drbd_disk_str(device->state.disk)); | |
429 | put_ldev(device); | |
430 | return -EIO; | |
431 | } | |
432 | ||
433 | /* protects md_io_buffer, al_tr_cycle, ... */ | |
434 | buffer = drbd_md_get_buffer(device, __func__); | |
435 | if (!buffer) { | |
436 | drbd_err(device, "disk failed while waiting for md_io buffer\n"); | |
437 | put_ldev(device); | |
438 | return -ENODEV; | |
439 | } | |
440 | ||
441 | err = __al_write_transaction(device, buffer); | |
442 | ||
443 | drbd_md_put_buffer(device); | |
444 | put_ldev(device); | |
445 | ||
446 | return err; | |
447 | } | |
448 | ||
b5bc8e08 | 449 | |
4dd726f0 | 450 | void drbd_al_begin_io_commit(struct drbd_device *device) |
b5bc8e08 LE |
451 | { |
452 | bool locked = false; | |
ebfd5d8f | 453 | |
7dc1d67f LE |
454 | /* Serialize multiple transactions. |
455 | * This uses test_and_set_bit, memory barrier is implicit. | |
456 | */ | |
b30ab791 AG |
457 | wait_event(device->al_wait, |
458 | device->act_log->pending_changes == 0 || | |
459 | (locked = lc_try_lock_for_transaction(device->act_log))); | |
7dc1d67f LE |
460 | |
461 | if (locked) { | |
7ad651b5 LE |
462 | /* Double check: it may have been committed by someone else, |
463 | * while we have been waiting for the lock. */ | |
b30ab791 | 464 | if (device->act_log->pending_changes) { |
9a51ab1c PR |
465 | bool write_al_updates; |
466 | ||
467 | rcu_read_lock(); | |
b30ab791 | 468 | write_al_updates = rcu_dereference(device->ldev->disk_conf)->al_updates; |
9a51ab1c PR |
469 | rcu_read_unlock(); |
470 | ||
b5bc8e08 | 471 | if (write_al_updates) |
4dd726f0 | 472 | al_write_transaction(device); |
b30ab791 | 473 | spin_lock_irq(&device->al_lock); |
7ad651b5 | 474 | /* FIXME |
1b7ab15b | 475 | if (err) |
7ad651b5 LE |
476 | we need an "lc_cancel" here; |
477 | */ | |
b30ab791 AG |
478 | lc_committed(device->act_log); |
479 | spin_unlock_irq(&device->al_lock); | |
7ad651b5 | 480 | } |
b30ab791 AG |
481 | lc_unlock(device->act_log); |
482 | wake_up(&device->al_wait); | |
b411b363 PR |
483 | } |
484 | } | |
485 | ||
b5bc8e08 LE |
486 | /* |
487 | * @delegate: delegate activity log I/O to the worker thread | |
488 | */ | |
4dd726f0 | 489 | void drbd_al_begin_io(struct drbd_device *device, struct drbd_interval *i) |
b5bc8e08 | 490 | { |
b30ab791 | 491 | if (drbd_al_begin_io_prepare(device, i)) |
4dd726f0 | 492 | drbd_al_begin_io_commit(device); |
b5bc8e08 LE |
493 | } |
494 | ||
b30ab791 | 495 | int drbd_al_begin_io_nonblock(struct drbd_device *device, struct drbd_interval *i) |
08a1ddab | 496 | { |
b30ab791 | 497 | struct lru_cache *al = device->act_log; |
08a1ddab LE |
498 | /* for bios crossing activity log extent boundaries, |
499 | * we may need to activate two extents in one go */ | |
500 | unsigned first = i->sector >> (AL_EXTENT_SHIFT-9); | |
501 | unsigned last = i->size == 0 ? first : (i->sector + (i->size >> 9) - 1) >> (AL_EXTENT_SHIFT-9); | |
502 | unsigned nr_al_extents; | |
503 | unsigned available_update_slots; | |
504 | unsigned enr; | |
505 | ||
0b0ba1ef | 506 | D_ASSERT(device, first <= last); |
08a1ddab LE |
507 | |
508 | nr_al_extents = 1 + last - first; /* worst case: all touched extends are cold. */ | |
509 | available_update_slots = min(al->nr_elements - al->used, | |
510 | al->max_pending_changes - al->pending_changes); | |
511 | ||
512 | /* We want all necessary updates for a given request within the same transaction | |
513 | * We could first check how many updates are *actually* needed, | |
514 | * and use that instead of the worst-case nr_al_extents */ | |
f5b90b6b LE |
515 | if (available_update_slots < nr_al_extents) { |
516 | /* Too many activity log extents are currently "hot". | |
517 | * | |
518 | * If we have accumulated pending changes already, | |
519 | * we made progress. | |
520 | * | |
521 | * If we cannot get even a single pending change through, | |
522 | * stop the fast path until we made some progress, | |
523 | * or requests to "cold" extents could be starved. */ | |
524 | if (!al->pending_changes) | |
525 | __set_bit(__LC_STARVING, &device->act_log->flags); | |
526 | return -ENOBUFS; | |
527 | } | |
08a1ddab LE |
528 | |
529 | /* Is resync active in this area? */ | |
530 | for (enr = first; enr <= last; enr++) { | |
531 | struct lc_element *tmp; | |
b30ab791 | 532 | tmp = lc_find(device->resync, enr/AL_EXT_PER_BM_SECT); |
08a1ddab LE |
533 | if (unlikely(tmp != NULL)) { |
534 | struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce); | |
535 | if (test_bit(BME_NO_WRITES, &bm_ext->flags)) { | |
0b6ef416 | 536 | if (!test_and_set_bit(BME_PRIORITY, &bm_ext->flags)) |
08a1ddab LE |
537 | return -EBUSY; |
538 | return -EWOULDBLOCK; | |
539 | } | |
540 | } | |
541 | } | |
542 | ||
543 | /* Checkout the refcounts. | |
544 | * Given that we checked for available elements and update slots above, | |
545 | * this has to be successful. */ | |
546 | for (enr = first; enr <= last; enr++) { | |
547 | struct lc_element *al_ext; | |
b30ab791 | 548 | al_ext = lc_get_cumulative(device->act_log, enr); |
08a1ddab | 549 | if (!al_ext) |
d0180171 | 550 | drbd_info(device, "LOGIC BUG for enr=%u\n", enr); |
08a1ddab LE |
551 | } |
552 | return 0; | |
553 | } | |
554 | ||
b30ab791 | 555 | void drbd_al_complete_io(struct drbd_device *device, struct drbd_interval *i) |
b411b363 | 556 | { |
e15766e9 LE |
557 | /* for bios crossing activity log extent boundaries, |
558 | * we may need to activate two extents in one go */ | |
559 | unsigned first = i->sector >> (AL_EXTENT_SHIFT-9); | |
81a3537a | 560 | unsigned last = i->size == 0 ? first : (i->sector + (i->size >> 9) - 1) >> (AL_EXTENT_SHIFT-9); |
e15766e9 | 561 | unsigned enr; |
b411b363 PR |
562 | struct lc_element *extent; |
563 | unsigned long flags; | |
564 | ||
0b0ba1ef | 565 | D_ASSERT(device, first <= last); |
b30ab791 | 566 | spin_lock_irqsave(&device->al_lock, flags); |
b411b363 | 567 | |
e15766e9 | 568 | for (enr = first; enr <= last; enr++) { |
b30ab791 | 569 | extent = lc_find(device->act_log, enr); |
e15766e9 | 570 | if (!extent) { |
d0180171 | 571 | drbd_err(device, "al_complete_io() called on inactive extent %u\n", enr); |
e15766e9 LE |
572 | continue; |
573 | } | |
b30ab791 | 574 | lc_put(device->act_log, extent); |
b411b363 | 575 | } |
b30ab791 AG |
576 | spin_unlock_irqrestore(&device->al_lock, flags); |
577 | wake_up(&device->al_wait); | |
b411b363 PR |
578 | } |
579 | ||
b30ab791 | 580 | static int _try_lc_del(struct drbd_device *device, struct lc_element *al_ext) |
b411b363 PR |
581 | { |
582 | int rv; | |
583 | ||
b30ab791 | 584 | spin_lock_irq(&device->al_lock); |
b411b363 PR |
585 | rv = (al_ext->refcnt == 0); |
586 | if (likely(rv)) | |
b30ab791 AG |
587 | lc_del(device->act_log, al_ext); |
588 | spin_unlock_irq(&device->al_lock); | |
b411b363 PR |
589 | |
590 | return rv; | |
591 | } | |
592 | ||
593 | /** | |
594 | * drbd_al_shrink() - Removes all active extents form the activity log | |
b30ab791 | 595 | * @device: DRBD device. |
b411b363 PR |
596 | * |
597 | * Removes all active extents form the activity log, waiting until | |
598 | * the reference count of each entry dropped to 0 first, of course. | |
599 | * | |
b30ab791 | 600 | * You need to lock device->act_log with lc_try_lock() / lc_unlock() |
b411b363 | 601 | */ |
b30ab791 | 602 | void drbd_al_shrink(struct drbd_device *device) |
b411b363 PR |
603 | { |
604 | struct lc_element *al_ext; | |
605 | int i; | |
606 | ||
0b0ba1ef | 607 | D_ASSERT(device, test_bit(__LC_LOCKED, &device->act_log->flags)); |
b411b363 | 608 | |
b30ab791 AG |
609 | for (i = 0; i < device->act_log->nr_elements; i++) { |
610 | al_ext = lc_element_by_index(device->act_log, i); | |
b411b363 PR |
611 | if (al_ext->lc_number == LC_FREE) |
612 | continue; | |
b30ab791 | 613 | wait_event(device->al_wait, _try_lc_del(device, al_ext)); |
b411b363 PR |
614 | } |
615 | ||
b30ab791 | 616 | wake_up(&device->al_wait); |
b411b363 PR |
617 | } |
618 | ||
5f7c0124 | 619 | int drbd_al_initialize(struct drbd_device *device, void *buffer) |
d752b269 PR |
620 | { |
621 | struct al_transaction_on_disk *al = buffer; | |
b30ab791 | 622 | struct drbd_md *md = &device->ldev->md; |
d752b269 PR |
623 | int al_size_4k = md->al_stripes * md->al_stripe_size_4k; |
624 | int i; | |
625 | ||
5f7c0124 LE |
626 | __al_write_transaction(device, al); |
627 | /* There may or may not have been a pending transaction. */ | |
628 | spin_lock_irq(&device->al_lock); | |
629 | lc_committed(device->act_log); | |
630 | spin_unlock_irq(&device->al_lock); | |
d752b269 | 631 | |
5f7c0124 LE |
632 | /* The rest of the transactions will have an empty "updates" list, and |
633 | * are written out only to provide the context, and to initialize the | |
634 | * on-disk ring buffer. */ | |
635 | for (i = 1; i < al_size_4k; i++) { | |
636 | int err = __al_write_transaction(device, al); | |
d752b269 PR |
637 | if (err) |
638 | return err; | |
639 | } | |
640 | return 0; | |
641 | } | |
642 | ||
5ab7d2c0 LE |
643 | static const char *drbd_change_sync_fname[] = { |
644 | [RECORD_RS_FAILED] = "drbd_rs_failed_io", | |
645 | [SET_IN_SYNC] = "drbd_set_in_sync", | |
646 | [SET_OUT_OF_SYNC] = "drbd_set_out_of_sync" | |
647 | }; | |
648 | ||
b411b363 PR |
649 | /* ATTENTION. The AL's extents are 4MB each, while the extents in the |
650 | * resync LRU-cache are 16MB each. | |
651 | * The caller of this function has to hold an get_ldev() reference. | |
652 | * | |
5ab7d2c0 LE |
653 | * Adjusts the caching members ->rs_left (success) or ->rs_failed (!success), |
654 | * potentially pulling in (and recounting the corresponding bits) | |
655 | * this resync extent into the resync extent lru cache. | |
656 | * | |
657 | * Returns whether all bits have been cleared for this resync extent, | |
658 | * precisely: (rs_left <= rs_failed) | |
659 | * | |
b411b363 PR |
660 | * TODO will be obsoleted once we have a caching lru of the on disk bitmap |
661 | */ | |
5ab7d2c0 LE |
662 | static bool update_rs_extent(struct drbd_device *device, |
663 | unsigned int enr, int count, | |
664 | enum update_sync_bits_mode mode) | |
b411b363 PR |
665 | { |
666 | struct lc_element *e; | |
b411b363 | 667 | |
0b0ba1ef | 668 | D_ASSERT(device, atomic_read(&device->local_cnt)); |
b411b363 | 669 | |
5ab7d2c0 LE |
670 | /* When setting out-of-sync bits, |
671 | * we don't need it cached (lc_find). | |
672 | * But if it is present in the cache, | |
673 | * we should update the cached bit count. | |
674 | * Otherwise, that extent should be in the resync extent lru cache | |
675 | * already -- or we want to pull it in if necessary -- (lc_get), | |
676 | * then update and check rs_left and rs_failed. */ | |
677 | if (mode == SET_OUT_OF_SYNC) | |
678 | e = lc_find(device->resync, enr); | |
679 | else | |
680 | e = lc_get(device->resync, enr); | |
b411b363 PR |
681 | if (e) { |
682 | struct bm_extent *ext = lc_entry(e, struct bm_extent, lce); | |
683 | if (ext->lce.lc_number == enr) { | |
5ab7d2c0 | 684 | if (mode == SET_IN_SYNC) |
b411b363 | 685 | ext->rs_left -= count; |
5ab7d2c0 LE |
686 | else if (mode == SET_OUT_OF_SYNC) |
687 | ext->rs_left += count; | |
b411b363 PR |
688 | else |
689 | ext->rs_failed += count; | |
690 | if (ext->rs_left < ext->rs_failed) { | |
5ab7d2c0 | 691 | drbd_warn(device, "BAD! enr=%u rs_left=%d " |
975b2979 | 692 | "rs_failed=%d count=%d cstate=%s\n", |
b411b363 | 693 | ext->lce.lc_number, ext->rs_left, |
975b2979 | 694 | ext->rs_failed, count, |
b30ab791 | 695 | drbd_conn_str(device->state.conn)); |
975b2979 PR |
696 | |
697 | /* We don't expect to be able to clear more bits | |
698 | * than have been set when we originally counted | |
699 | * the set bits to cache that value in ext->rs_left. | |
700 | * Whatever the reason (disconnect during resync, | |
701 | * delayed local completion of an application write), | |
702 | * try to fix it up by recounting here. */ | |
b30ab791 | 703 | ext->rs_left = drbd_bm_e_weight(device, enr); |
b411b363 PR |
704 | } |
705 | } else { | |
706 | /* Normally this element should be in the cache, | |
707 | * since drbd_rs_begin_io() pulled it already in. | |
708 | * | |
709 | * But maybe an application write finished, and we set | |
710 | * something outside the resync lru_cache in sync. | |
711 | */ | |
b30ab791 | 712 | int rs_left = drbd_bm_e_weight(device, enr); |
b411b363 | 713 | if (ext->flags != 0) { |
d0180171 | 714 | drbd_warn(device, "changing resync lce: %d[%u;%02lx]" |
b411b363 PR |
715 | " -> %d[%u;00]\n", |
716 | ext->lce.lc_number, ext->rs_left, | |
717 | ext->flags, enr, rs_left); | |
718 | ext->flags = 0; | |
719 | } | |
720 | if (ext->rs_failed) { | |
d0180171 | 721 | drbd_warn(device, "Kicking resync_lru element enr=%u " |
b411b363 PR |
722 | "out with rs_failed=%d\n", |
723 | ext->lce.lc_number, ext->rs_failed); | |
b411b363 PR |
724 | } |
725 | ext->rs_left = rs_left; | |
5ab7d2c0 | 726 | ext->rs_failed = (mode == RECORD_RS_FAILED) ? count : 0; |
46a15bc3 LE |
727 | /* we don't keep a persistent log of the resync lru, |
728 | * we can commit any change right away. */ | |
b30ab791 | 729 | lc_committed(device->resync); |
b411b363 | 730 | } |
5ab7d2c0 LE |
731 | if (mode != SET_OUT_OF_SYNC) |
732 | lc_put(device->resync, &ext->lce); | |
b411b363 PR |
733 | /* no race, we are within the al_lock! */ |
734 | ||
5ab7d2c0 | 735 | if (ext->rs_left <= ext->rs_failed) { |
b411b363 | 736 | ext->rs_failed = 0; |
5ab7d2c0 | 737 | return true; |
b411b363 | 738 | } |
5ab7d2c0 LE |
739 | } else if (mode != SET_OUT_OF_SYNC) { |
740 | /* be quiet if lc_find() did not find it. */ | |
d0180171 | 741 | drbd_err(device, "lc_get() failed! locked=%d/%d flags=%lu\n", |
b30ab791 AG |
742 | device->resync_locked, |
743 | device->resync->nr_elements, | |
744 | device->resync->flags); | |
b411b363 | 745 | } |
5ab7d2c0 | 746 | return false; |
b411b363 PR |
747 | } |
748 | ||
b30ab791 | 749 | void drbd_advance_rs_marks(struct drbd_device *device, unsigned long still_to_go) |
c6ea14df LE |
750 | { |
751 | unsigned long now = jiffies; | |
b30ab791 AG |
752 | unsigned long last = device->rs_mark_time[device->rs_last_mark]; |
753 | int next = (device->rs_last_mark + 1) % DRBD_SYNC_MARKS; | |
c6ea14df | 754 | if (time_after_eq(now, last + DRBD_SYNC_MARK_STEP)) { |
b30ab791 AG |
755 | if (device->rs_mark_left[device->rs_last_mark] != still_to_go && |
756 | device->state.conn != C_PAUSED_SYNC_T && | |
757 | device->state.conn != C_PAUSED_SYNC_S) { | |
758 | device->rs_mark_time[next] = now; | |
759 | device->rs_mark_left[next] = still_to_go; | |
760 | device->rs_last_mark = next; | |
c6ea14df LE |
761 | } |
762 | } | |
763 | } | |
764 | ||
5ab7d2c0 LE |
765 | /* It is called lazy update, so don't do write-out too often. */ |
766 | static bool lazy_bitmap_update_due(struct drbd_device *device) | |
b411b363 | 767 | { |
5ab7d2c0 LE |
768 | return time_after(jiffies, device->rs_last_bcast + 2*HZ); |
769 | } | |
b411b363 | 770 | |
5ab7d2c0 LE |
771 | static void maybe_schedule_on_disk_bitmap_update(struct drbd_device *device, bool rs_done) |
772 | { | |
5ab7d2c0 LE |
773 | if (rs_done) |
774 | set_bit(RS_DONE, &device->flags); | |
775 | /* and also set RS_PROGRESS below */ | |
776 | else if (!lazy_bitmap_update_due(device)) | |
b411b363 | 777 | return; |
518a4d53 | 778 | |
e334f550 | 779 | drbd_device_post_work(device, RS_PROGRESS); |
5ab7d2c0 | 780 | } |
b411b363 | 781 | |
5ab7d2c0 LE |
782 | static int update_sync_bits(struct drbd_device *device, |
783 | unsigned long sbnr, unsigned long ebnr, | |
784 | enum update_sync_bits_mode mode) | |
785 | { | |
b411b363 | 786 | /* |
5ab7d2c0 LE |
787 | * We keep a count of set bits per resync-extent in the ->rs_left |
788 | * caching member, so we need to loop and work within the resync extent | |
789 | * alignment. Typically this loop will execute exactly once. | |
b411b363 | 790 | */ |
5ab7d2c0 LE |
791 | unsigned long flags; |
792 | unsigned long count = 0; | |
793 | unsigned int cleared = 0; | |
794 | while (sbnr <= ebnr) { | |
795 | /* set temporary boundary bit number to last bit number within | |
796 | * the resync extent of the current start bit number, | |
797 | * but cap at provided end bit number */ | |
798 | unsigned long tbnr = min(ebnr, sbnr | BM_BLOCKS_PER_BM_EXT_MASK); | |
799 | unsigned long c; | |
800 | ||
801 | if (mode == RECORD_RS_FAILED) | |
802 | /* Only called from drbd_rs_failed_io(), bits | |
803 | * supposedly still set. Recount, maybe some | |
804 | * of the bits have been successfully cleared | |
805 | * by application IO meanwhile. | |
806 | */ | |
807 | c = drbd_bm_count_bits(device, sbnr, tbnr); | |
808 | else if (mode == SET_IN_SYNC) | |
809 | c = drbd_bm_clear_bits(device, sbnr, tbnr); | |
810 | else /* if (mode == SET_OUT_OF_SYNC) */ | |
811 | c = drbd_bm_set_bits(device, sbnr, tbnr); | |
812 | ||
813 | if (c) { | |
814 | spin_lock_irqsave(&device->al_lock, flags); | |
815 | cleared += update_rs_extent(device, BM_BIT_TO_EXT(sbnr), c, mode); | |
816 | spin_unlock_irqrestore(&device->al_lock, flags); | |
817 | count += c; | |
818 | } | |
819 | sbnr = tbnr + 1; | |
b411b363 | 820 | } |
5ab7d2c0 LE |
821 | if (count) { |
822 | if (mode == SET_IN_SYNC) { | |
823 | unsigned long still_to_go = drbd_bm_total_weight(device); | |
824 | bool rs_is_done = (still_to_go <= device->rs_failed); | |
825 | drbd_advance_rs_marks(device, still_to_go); | |
826 | if (cleared || rs_is_done) | |
827 | maybe_schedule_on_disk_bitmap_update(device, rs_is_done); | |
828 | } else if (mode == RECORD_RS_FAILED) | |
829 | device->rs_failed += count; | |
b30ab791 | 830 | wake_up(&device->al_wait); |
5ab7d2c0 LE |
831 | } |
832 | return count; | |
b411b363 PR |
833 | } |
834 | ||
5ab7d2c0 LE |
835 | /* clear the bit corresponding to the piece of storage in question: |
836 | * size byte of data starting from sector. Only clear a bits of the affected | |
837 | * one ore more _aligned_ BM_BLOCK_SIZE blocks. | |
838 | * | |
839 | * called by worker on C_SYNC_TARGET and receiver on SyncSource. | |
b411b363 | 840 | * |
b411b363 | 841 | */ |
5ab7d2c0 | 842 | int __drbd_change_sync(struct drbd_device *device, sector_t sector, int size, |
179e20b8 | 843 | enum update_sync_bits_mode mode) |
b411b363 | 844 | { |
5ab7d2c0 LE |
845 | /* Is called from worker and receiver context _only_ */ |
846 | unsigned long sbnr, ebnr, lbnr; | |
847 | unsigned long count = 0; | |
b411b363 | 848 | sector_t esector, nr_sectors; |
b411b363 | 849 | |
28a8f0d3 | 850 | /* This would be an empty REQ_PREFLUSH, be silent. */ |
5ab7d2c0 | 851 | if ((mode == SET_OUT_OF_SYNC) && size == 0) |
81a3537a LE |
852 | return 0; |
853 | ||
5ab7d2c0 LE |
854 | if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_DISCARD_SIZE) { |
855 | drbd_err(device, "%s: sector=%llus size=%d nonsense!\n", | |
856 | drbd_change_sync_fname[mode], | |
857 | (unsigned long long)sector, size); | |
73a01a18 | 858 | return 0; |
b411b363 PR |
859 | } |
860 | ||
b30ab791 | 861 | if (!get_ldev(device)) |
5ab7d2c0 | 862 | return 0; /* no disk, no metadata, no bitmap to manipulate bits in */ |
b411b363 | 863 | |
b30ab791 | 864 | nr_sectors = drbd_get_capacity(device->this_bdev); |
b411b363 PR |
865 | esector = sector + (size >> 9) - 1; |
866 | ||
841ce241 | 867 | if (!expect(sector < nr_sectors)) |
b411b363 | 868 | goto out; |
841ce241 AG |
869 | if (!expect(esector < nr_sectors)) |
870 | esector = nr_sectors - 1; | |
b411b363 | 871 | |
5ab7d2c0 | 872 | lbnr = BM_SECT_TO_BIT(nr_sectors-1); |
b411b363 | 873 | |
5ab7d2c0 LE |
874 | if (mode == SET_IN_SYNC) { |
875 | /* Round up start sector, round down end sector. We make sure | |
876 | * we only clear full, aligned, BM_BLOCK_SIZE blocks. */ | |
877 | if (unlikely(esector < BM_SECT_PER_BIT-1)) | |
878 | goto out; | |
879 | if (unlikely(esector == (nr_sectors-1))) | |
880 | ebnr = lbnr; | |
881 | else | |
882 | ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1)); | |
883 | sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1); | |
884 | } else { | |
885 | /* We set it out of sync, or record resync failure. | |
886 | * Should not round anything here. */ | |
887 | sbnr = BM_SECT_TO_BIT(sector); | |
888 | ebnr = BM_SECT_TO_BIT(esector); | |
889 | } | |
b411b363 | 890 | |
5ab7d2c0 | 891 | count = update_sync_bits(device, sbnr, ebnr, mode); |
b411b363 | 892 | out: |
b30ab791 | 893 | put_ldev(device); |
73a01a18 | 894 | return count; |
b411b363 PR |
895 | } |
896 | ||
897 | static | |
b30ab791 | 898 | struct bm_extent *_bme_get(struct drbd_device *device, unsigned int enr) |
b411b363 PR |
899 | { |
900 | struct lc_element *e; | |
901 | struct bm_extent *bm_ext; | |
902 | int wakeup = 0; | |
903 | unsigned long rs_flags; | |
904 | ||
b30ab791 AG |
905 | spin_lock_irq(&device->al_lock); |
906 | if (device->resync_locked > device->resync->nr_elements/2) { | |
907 | spin_unlock_irq(&device->al_lock); | |
b411b363 PR |
908 | return NULL; |
909 | } | |
b30ab791 | 910 | e = lc_get(device->resync, enr); |
b411b363 PR |
911 | bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; |
912 | if (bm_ext) { | |
913 | if (bm_ext->lce.lc_number != enr) { | |
b30ab791 | 914 | bm_ext->rs_left = drbd_bm_e_weight(device, enr); |
b411b363 | 915 | bm_ext->rs_failed = 0; |
b30ab791 | 916 | lc_committed(device->resync); |
b411b363 PR |
917 | wakeup = 1; |
918 | } | |
919 | if (bm_ext->lce.refcnt == 1) | |
b30ab791 | 920 | device->resync_locked++; |
b411b363 PR |
921 | set_bit(BME_NO_WRITES, &bm_ext->flags); |
922 | } | |
b30ab791 AG |
923 | rs_flags = device->resync->flags; |
924 | spin_unlock_irq(&device->al_lock); | |
b411b363 | 925 | if (wakeup) |
b30ab791 | 926 | wake_up(&device->al_wait); |
b411b363 PR |
927 | |
928 | if (!bm_ext) { | |
929 | if (rs_flags & LC_STARVING) | |
d0180171 | 930 | drbd_warn(device, "Have to wait for element" |
b411b363 | 931 | " (resync LRU too small?)\n"); |
46a15bc3 | 932 | BUG_ON(rs_flags & LC_LOCKED); |
b411b363 PR |
933 | } |
934 | ||
935 | return bm_ext; | |
936 | } | |
937 | ||
b30ab791 | 938 | static int _is_in_al(struct drbd_device *device, unsigned int enr) |
b411b363 | 939 | { |
46a15bc3 | 940 | int rv; |
b411b363 | 941 | |
b30ab791 AG |
942 | spin_lock_irq(&device->al_lock); |
943 | rv = lc_is_used(device->act_log, enr); | |
944 | spin_unlock_irq(&device->al_lock); | |
b411b363 | 945 | |
b411b363 PR |
946 | return rv; |
947 | } | |
948 | ||
949 | /** | |
950 | * drbd_rs_begin_io() - Gets an extent in the resync LRU cache and sets it to BME_LOCKED | |
b30ab791 | 951 | * @device: DRBD device. |
b411b363 PR |
952 | * @sector: The sector number. |
953 | * | |
80a40e43 | 954 | * This functions sleeps on al_wait. Returns 0 on success, -EINTR if interrupted. |
b411b363 | 955 | */ |
b30ab791 | 956 | int drbd_rs_begin_io(struct drbd_device *device, sector_t sector) |
b411b363 PR |
957 | { |
958 | unsigned int enr = BM_SECT_TO_EXT(sector); | |
959 | struct bm_extent *bm_ext; | |
960 | int i, sig; | |
e8299874 | 961 | bool sa; |
b411b363 | 962 | |
f91ab628 | 963 | retry: |
b30ab791 AG |
964 | sig = wait_event_interruptible(device->al_wait, |
965 | (bm_ext = _bme_get(device, enr))); | |
b411b363 | 966 | if (sig) |
80a40e43 | 967 | return -EINTR; |
b411b363 PR |
968 | |
969 | if (test_bit(BME_LOCKED, &bm_ext->flags)) | |
80a40e43 | 970 | return 0; |
b411b363 | 971 | |
e8299874 LE |
972 | /* step aside only while we are above c-min-rate; unless disabled. */ |
973 | sa = drbd_rs_c_min_rate_throttle(device); | |
974 | ||
b411b363 | 975 | for (i = 0; i < AL_EXT_PER_BM_SECT; i++) { |
b30ab791 AG |
976 | sig = wait_event_interruptible(device->al_wait, |
977 | !_is_in_al(device, enr * AL_EXT_PER_BM_SECT + i) || | |
e8299874 | 978 | (sa && test_bit(BME_PRIORITY, &bm_ext->flags))); |
f91ab628 | 979 | |
e8299874 | 980 | if (sig || (sa && test_bit(BME_PRIORITY, &bm_ext->flags))) { |
b30ab791 AG |
981 | spin_lock_irq(&device->al_lock); |
982 | if (lc_put(device->resync, &bm_ext->lce) == 0) { | |
f91ab628 | 983 | bm_ext->flags = 0; /* clears BME_NO_WRITES and eventually BME_PRIORITY */ |
b30ab791 AG |
984 | device->resync_locked--; |
985 | wake_up(&device->al_wait); | |
b411b363 | 986 | } |
b30ab791 | 987 | spin_unlock_irq(&device->al_lock); |
f91ab628 PR |
988 | if (sig) |
989 | return -EINTR; | |
990 | if (schedule_timeout_interruptible(HZ/10)) | |
991 | return -EINTR; | |
f91ab628 | 992 | goto retry; |
b411b363 PR |
993 | } |
994 | } | |
b411b363 | 995 | set_bit(BME_LOCKED, &bm_ext->flags); |
80a40e43 | 996 | return 0; |
b411b363 PR |
997 | } |
998 | ||
999 | /** | |
1000 | * drbd_try_rs_begin_io() - Gets an extent in the resync LRU cache, does not sleep | |
b30ab791 | 1001 | * @device: DRBD device. |
b411b363 PR |
1002 | * @sector: The sector number. |
1003 | * | |
1004 | * Gets an extent in the resync LRU cache, sets it to BME_NO_WRITES, then | |
1005 | * tries to set it to BME_LOCKED. Returns 0 upon success, and -EAGAIN | |
1006 | * if there is still application IO going on in this area. | |
1007 | */ | |
b30ab791 | 1008 | int drbd_try_rs_begin_io(struct drbd_device *device, sector_t sector) |
b411b363 PR |
1009 | { |
1010 | unsigned int enr = BM_SECT_TO_EXT(sector); | |
1011 | const unsigned int al_enr = enr*AL_EXT_PER_BM_SECT; | |
1012 | struct lc_element *e; | |
1013 | struct bm_extent *bm_ext; | |
1014 | int i; | |
ad3fee79 LE |
1015 | bool throttle = drbd_rs_should_slow_down(device, sector, true); |
1016 | ||
1017 | /* If we need to throttle, a half-locked (only marked BME_NO_WRITES, | |
1018 | * not yet BME_LOCKED) extent needs to be kicked out explicitly if we | |
1019 | * need to throttle. There is at most one such half-locked extent, | |
1020 | * which is remembered in resync_wenr. */ | |
1021 | ||
1022 | if (throttle && device->resync_wenr != enr) | |
1023 | return -EAGAIN; | |
b411b363 | 1024 | |
b30ab791 AG |
1025 | spin_lock_irq(&device->al_lock); |
1026 | if (device->resync_wenr != LC_FREE && device->resync_wenr != enr) { | |
b411b363 PR |
1027 | /* in case you have very heavy scattered io, it may |
1028 | * stall the syncer undefined if we give up the ref count | |
1029 | * when we try again and requeue. | |
1030 | * | |
1031 | * if we don't give up the refcount, but the next time | |
1032 | * we are scheduled this extent has been "synced" by new | |
1033 | * application writes, we'd miss the lc_put on the | |
1034 | * extent we keep the refcount on. | |
1035 | * so we remembered which extent we had to try again, and | |
1036 | * if the next requested one is something else, we do | |
1037 | * the lc_put here... | |
1038 | * we also have to wake_up | |
1039 | */ | |
b30ab791 | 1040 | e = lc_find(device->resync, device->resync_wenr); |
b411b363 PR |
1041 | bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; |
1042 | if (bm_ext) { | |
0b0ba1ef AG |
1043 | D_ASSERT(device, !test_bit(BME_LOCKED, &bm_ext->flags)); |
1044 | D_ASSERT(device, test_bit(BME_NO_WRITES, &bm_ext->flags)); | |
b411b363 | 1045 | clear_bit(BME_NO_WRITES, &bm_ext->flags); |
b30ab791 | 1046 | device->resync_wenr = LC_FREE; |
ad3fee79 LE |
1047 | if (lc_put(device->resync, &bm_ext->lce) == 0) { |
1048 | bm_ext->flags = 0; | |
b30ab791 | 1049 | device->resync_locked--; |
ad3fee79 | 1050 | } |
b30ab791 | 1051 | wake_up(&device->al_wait); |
b411b363 | 1052 | } else { |
d0180171 | 1053 | drbd_alert(device, "LOGIC BUG\n"); |
b411b363 PR |
1054 | } |
1055 | } | |
1056 | /* TRY. */ | |
b30ab791 | 1057 | e = lc_try_get(device->resync, enr); |
b411b363 PR |
1058 | bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; |
1059 | if (bm_ext) { | |
1060 | if (test_bit(BME_LOCKED, &bm_ext->flags)) | |
1061 | goto proceed; | |
1062 | if (!test_and_set_bit(BME_NO_WRITES, &bm_ext->flags)) { | |
b30ab791 | 1063 | device->resync_locked++; |
b411b363 PR |
1064 | } else { |
1065 | /* we did set the BME_NO_WRITES, | |
1066 | * but then could not set BME_LOCKED, | |
1067 | * so we tried again. | |
1068 | * drop the extra reference. */ | |
b411b363 | 1069 | bm_ext->lce.refcnt--; |
0b0ba1ef | 1070 | D_ASSERT(device, bm_ext->lce.refcnt > 0); |
b411b363 PR |
1071 | } |
1072 | goto check_al; | |
1073 | } else { | |
1074 | /* do we rather want to try later? */ | |
b30ab791 | 1075 | if (device->resync_locked > device->resync->nr_elements-3) |
b411b363 | 1076 | goto try_again; |
b411b363 | 1077 | /* Do or do not. There is no try. -- Yoda */ |
b30ab791 | 1078 | e = lc_get(device->resync, enr); |
b411b363 PR |
1079 | bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; |
1080 | if (!bm_ext) { | |
b30ab791 | 1081 | const unsigned long rs_flags = device->resync->flags; |
b411b363 | 1082 | if (rs_flags & LC_STARVING) |
d0180171 | 1083 | drbd_warn(device, "Have to wait for element" |
b411b363 | 1084 | " (resync LRU too small?)\n"); |
46a15bc3 | 1085 | BUG_ON(rs_flags & LC_LOCKED); |
b411b363 PR |
1086 | goto try_again; |
1087 | } | |
1088 | if (bm_ext->lce.lc_number != enr) { | |
b30ab791 | 1089 | bm_ext->rs_left = drbd_bm_e_weight(device, enr); |
b411b363 | 1090 | bm_ext->rs_failed = 0; |
b30ab791 AG |
1091 | lc_committed(device->resync); |
1092 | wake_up(&device->al_wait); | |
0b0ba1ef | 1093 | D_ASSERT(device, test_bit(BME_LOCKED, &bm_ext->flags) == 0); |
b411b363 PR |
1094 | } |
1095 | set_bit(BME_NO_WRITES, &bm_ext->flags); | |
0b0ba1ef | 1096 | D_ASSERT(device, bm_ext->lce.refcnt == 1); |
b30ab791 | 1097 | device->resync_locked++; |
b411b363 PR |
1098 | goto check_al; |
1099 | } | |
1100 | check_al: | |
b411b363 | 1101 | for (i = 0; i < AL_EXT_PER_BM_SECT; i++) { |
b30ab791 | 1102 | if (lc_is_used(device->act_log, al_enr+i)) |
b411b363 PR |
1103 | goto try_again; |
1104 | } | |
1105 | set_bit(BME_LOCKED, &bm_ext->flags); | |
1106 | proceed: | |
b30ab791 AG |
1107 | device->resync_wenr = LC_FREE; |
1108 | spin_unlock_irq(&device->al_lock); | |
b411b363 PR |
1109 | return 0; |
1110 | ||
1111 | try_again: | |
ad3fee79 LE |
1112 | if (bm_ext) { |
1113 | if (throttle) { | |
1114 | D_ASSERT(device, !test_bit(BME_LOCKED, &bm_ext->flags)); | |
1115 | D_ASSERT(device, test_bit(BME_NO_WRITES, &bm_ext->flags)); | |
1116 | clear_bit(BME_NO_WRITES, &bm_ext->flags); | |
1117 | device->resync_wenr = LC_FREE; | |
1118 | if (lc_put(device->resync, &bm_ext->lce) == 0) { | |
1119 | bm_ext->flags = 0; | |
1120 | device->resync_locked--; | |
1121 | } | |
1122 | wake_up(&device->al_wait); | |
1123 | } else | |
1124 | device->resync_wenr = enr; | |
1125 | } | |
b30ab791 | 1126 | spin_unlock_irq(&device->al_lock); |
b411b363 PR |
1127 | return -EAGAIN; |
1128 | } | |
1129 | ||
b30ab791 | 1130 | void drbd_rs_complete_io(struct drbd_device *device, sector_t sector) |
b411b363 PR |
1131 | { |
1132 | unsigned int enr = BM_SECT_TO_EXT(sector); | |
1133 | struct lc_element *e; | |
1134 | struct bm_extent *bm_ext; | |
1135 | unsigned long flags; | |
1136 | ||
b30ab791 AG |
1137 | spin_lock_irqsave(&device->al_lock, flags); |
1138 | e = lc_find(device->resync, enr); | |
b411b363 PR |
1139 | bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; |
1140 | if (!bm_ext) { | |
b30ab791 | 1141 | spin_unlock_irqrestore(&device->al_lock, flags); |
b411b363 | 1142 | if (__ratelimit(&drbd_ratelimit_state)) |
d0180171 | 1143 | drbd_err(device, "drbd_rs_complete_io() called, but extent not found\n"); |
b411b363 PR |
1144 | return; |
1145 | } | |
1146 | ||
1147 | if (bm_ext->lce.refcnt == 0) { | |
b30ab791 | 1148 | spin_unlock_irqrestore(&device->al_lock, flags); |
d0180171 | 1149 | drbd_err(device, "drbd_rs_complete_io(,%llu [=%u]) called, " |
b411b363 PR |
1150 | "but refcnt is 0!?\n", |
1151 | (unsigned long long)sector, enr); | |
1152 | return; | |
1153 | } | |
1154 | ||
b30ab791 | 1155 | if (lc_put(device->resync, &bm_ext->lce) == 0) { |
e3555d85 | 1156 | bm_ext->flags = 0; /* clear BME_LOCKED, BME_NO_WRITES and BME_PRIORITY */ |
b30ab791 AG |
1157 | device->resync_locked--; |
1158 | wake_up(&device->al_wait); | |
b411b363 PR |
1159 | } |
1160 | ||
b30ab791 | 1161 | spin_unlock_irqrestore(&device->al_lock, flags); |
b411b363 PR |
1162 | } |
1163 | ||
1164 | /** | |
1165 | * drbd_rs_cancel_all() - Removes all extents from the resync LRU (even BME_LOCKED) | |
b30ab791 | 1166 | * @device: DRBD device. |
b411b363 | 1167 | */ |
b30ab791 | 1168 | void drbd_rs_cancel_all(struct drbd_device *device) |
b411b363 | 1169 | { |
b30ab791 | 1170 | spin_lock_irq(&device->al_lock); |
b411b363 | 1171 | |
b30ab791 AG |
1172 | if (get_ldev_if_state(device, D_FAILED)) { /* Makes sure ->resync is there. */ |
1173 | lc_reset(device->resync); | |
1174 | put_ldev(device); | |
b411b363 | 1175 | } |
b30ab791 AG |
1176 | device->resync_locked = 0; |
1177 | device->resync_wenr = LC_FREE; | |
1178 | spin_unlock_irq(&device->al_lock); | |
1179 | wake_up(&device->al_wait); | |
b411b363 PR |
1180 | } |
1181 | ||
1182 | /** | |
1183 | * drbd_rs_del_all() - Gracefully remove all extents from the resync LRU | |
b30ab791 | 1184 | * @device: DRBD device. |
b411b363 PR |
1185 | * |
1186 | * Returns 0 upon success, -EAGAIN if at least one reference count was | |
1187 | * not zero. | |
1188 | */ | |
b30ab791 | 1189 | int drbd_rs_del_all(struct drbd_device *device) |
b411b363 PR |
1190 | { |
1191 | struct lc_element *e; | |
1192 | struct bm_extent *bm_ext; | |
1193 | int i; | |
1194 | ||
b30ab791 | 1195 | spin_lock_irq(&device->al_lock); |
b411b363 | 1196 | |
b30ab791 | 1197 | if (get_ldev_if_state(device, D_FAILED)) { |
b411b363 | 1198 | /* ok, ->resync is there. */ |
b30ab791 AG |
1199 | for (i = 0; i < device->resync->nr_elements; i++) { |
1200 | e = lc_element_by_index(device->resync, i); | |
b2b163dd | 1201 | bm_ext = lc_entry(e, struct bm_extent, lce); |
b411b363 PR |
1202 | if (bm_ext->lce.lc_number == LC_FREE) |
1203 | continue; | |
b30ab791 | 1204 | if (bm_ext->lce.lc_number == device->resync_wenr) { |
d0180171 | 1205 | drbd_info(device, "dropping %u in drbd_rs_del_all, apparently" |
b411b363 | 1206 | " got 'synced' by application io\n", |
b30ab791 | 1207 | device->resync_wenr); |
0b0ba1ef AG |
1208 | D_ASSERT(device, !test_bit(BME_LOCKED, &bm_ext->flags)); |
1209 | D_ASSERT(device, test_bit(BME_NO_WRITES, &bm_ext->flags)); | |
b411b363 | 1210 | clear_bit(BME_NO_WRITES, &bm_ext->flags); |
b30ab791 AG |
1211 | device->resync_wenr = LC_FREE; |
1212 | lc_put(device->resync, &bm_ext->lce); | |
b411b363 PR |
1213 | } |
1214 | if (bm_ext->lce.refcnt != 0) { | |
d0180171 | 1215 | drbd_info(device, "Retrying drbd_rs_del_all() later. " |
b411b363 | 1216 | "refcnt=%d\n", bm_ext->lce.refcnt); |
b30ab791 AG |
1217 | put_ldev(device); |
1218 | spin_unlock_irq(&device->al_lock); | |
b411b363 PR |
1219 | return -EAGAIN; |
1220 | } | |
0b0ba1ef AG |
1221 | D_ASSERT(device, !test_bit(BME_LOCKED, &bm_ext->flags)); |
1222 | D_ASSERT(device, !test_bit(BME_NO_WRITES, &bm_ext->flags)); | |
b30ab791 | 1223 | lc_del(device->resync, &bm_ext->lce); |
b411b363 | 1224 | } |
0b0ba1ef | 1225 | D_ASSERT(device, device->resync->used == 0); |
b30ab791 | 1226 | put_ldev(device); |
b411b363 | 1227 | } |
b30ab791 AG |
1228 | spin_unlock_irq(&device->al_lock); |
1229 | wake_up(&device->al_wait); | |
b411b363 PR |
1230 | |
1231 | return 0; | |
1232 | } |