Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Copyright (C) 2003 Sistina Software | |
b7fd54a7 | 3 | * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved. |
1da177e4 LT |
4 | * |
5 | * This file is released under the LGPL. | |
6 | */ | |
7 | ||
8 | #include <linux/init.h> | |
9 | #include <linux/slab.h> | |
10 | #include <linux/module.h> | |
11 | #include <linux/vmalloc.h> | |
12 | ||
13 | #include "dm-log.h" | |
14 | #include "dm-io.h" | |
15 | ||
b7fd54a7 | 16 | #define DM_MSG_PREFIX "dirty region log" |
72d94861 | 17 | |
1da177e4 LT |
18 | static LIST_HEAD(_log_types); |
19 | static DEFINE_SPINLOCK(_lock); | |
20 | ||
21 | int dm_register_dirty_log_type(struct dirty_log_type *type) | |
22 | { | |
23 | spin_lock(&_lock); | |
24 | type->use_count = 0; | |
25 | list_add(&type->list, &_log_types); | |
26 | spin_unlock(&_lock); | |
27 | ||
28 | return 0; | |
29 | } | |
30 | ||
31 | int dm_unregister_dirty_log_type(struct dirty_log_type *type) | |
32 | { | |
33 | spin_lock(&_lock); | |
34 | ||
35 | if (type->use_count) | |
36 | DMWARN("Attempt to unregister a log type that is still in use"); | |
37 | else | |
38 | list_del(&type->list); | |
39 | ||
40 | spin_unlock(&_lock); | |
41 | ||
42 | return 0; | |
43 | } | |
44 | ||
fb8b2848 | 45 | static struct dirty_log_type *_get_type(const char *type_name) |
1da177e4 LT |
46 | { |
47 | struct dirty_log_type *type; | |
48 | ||
49 | spin_lock(&_lock); | |
50 | list_for_each_entry (type, &_log_types, list) | |
51 | if (!strcmp(type_name, type->name)) { | |
52 | if (!type->use_count && !try_module_get(type->module)){ | |
53 | spin_unlock(&_lock); | |
54 | return NULL; | |
55 | } | |
56 | type->use_count++; | |
57 | spin_unlock(&_lock); | |
58 | return type; | |
59 | } | |
60 | ||
61 | spin_unlock(&_lock); | |
62 | return NULL; | |
63 | } | |
64 | ||
fb8b2848 JB |
65 | /* |
66 | * get_type | |
67 | * @type_name | |
68 | * | |
69 | * Attempt to retrieve the dirty_log_type by name. If not already | |
70 | * available, attempt to load the appropriate module. | |
71 | * | |
72 | * Log modules are named "dm-log-" followed by the 'type_name'. | |
73 | * Modules may contain multiple types. | |
74 | * This function will first try the module "dm-log-<type_name>", | |
75 | * then truncate 'type_name' on the last '-' and try again. | |
76 | * | |
77 | * For example, if type_name was "clustered-disk", it would search | |
78 | * 'dm-log-clustered-disk' then 'dm-log-clustered'. | |
79 | * | |
80 | * Returns: dirty_log_type* on success, NULL on failure | |
81 | */ | |
82 | static struct dirty_log_type *get_type(const char *type_name) | |
83 | { | |
84 | char *p, *type_name_dup; | |
85 | struct dirty_log_type *type; | |
86 | ||
87 | type = _get_type(type_name); | |
88 | if (type) | |
89 | return type; | |
90 | ||
91 | type_name_dup = kstrdup(type_name, GFP_KERNEL); | |
92 | if (!type_name_dup) { | |
93 | DMWARN("No memory left to attempt log module load for \"%s\"", | |
94 | type_name); | |
95 | return NULL; | |
96 | } | |
97 | ||
98 | while (request_module("dm-log-%s", type_name_dup) || | |
99 | !(type = _get_type(type_name))) { | |
100 | p = strrchr(type_name_dup, '-'); | |
101 | if (!p) | |
102 | break; | |
103 | p[0] = '\0'; | |
104 | } | |
105 | ||
106 | if (!type) | |
107 | DMWARN("Module for logging type \"%s\" not found.", type_name); | |
108 | ||
109 | kfree(type_name_dup); | |
110 | ||
111 | return type; | |
112 | } | |
113 | ||
1da177e4 LT |
114 | static void put_type(struct dirty_log_type *type) |
115 | { | |
116 | spin_lock(&_lock); | |
117 | if (!--type->use_count) | |
118 | module_put(type->module); | |
119 | spin_unlock(&_lock); | |
120 | } | |
121 | ||
122 | struct dirty_log *dm_create_dirty_log(const char *type_name, struct dm_target *ti, | |
123 | unsigned int argc, char **argv) | |
124 | { | |
125 | struct dirty_log_type *type; | |
126 | struct dirty_log *log; | |
127 | ||
128 | log = kmalloc(sizeof(*log), GFP_KERNEL); | |
129 | if (!log) | |
130 | return NULL; | |
131 | ||
132 | type = get_type(type_name); | |
133 | if (!type) { | |
134 | kfree(log); | |
135 | return NULL; | |
136 | } | |
137 | ||
138 | log->type = type; | |
139 | if (type->ctr(log, ti, argc, argv)) { | |
140 | kfree(log); | |
141 | put_type(type); | |
142 | return NULL; | |
143 | } | |
144 | ||
145 | return log; | |
146 | } | |
147 | ||
148 | void dm_destroy_dirty_log(struct dirty_log *log) | |
149 | { | |
150 | log->type->dtr(log); | |
151 | put_type(log->type); | |
152 | kfree(log); | |
153 | } | |
154 | ||
155 | /*----------------------------------------------------------------- | |
156 | * Persistent and core logs share a lot of their implementation. | |
157 | * FIXME: need a reload method to be called from a resume | |
158 | *---------------------------------------------------------------*/ | |
159 | /* | |
160 | * Magic for persistent mirrors: "MiRr" | |
161 | */ | |
162 | #define MIRROR_MAGIC 0x4D695272 | |
163 | ||
164 | /* | |
165 | * The on-disk version of the metadata. | |
166 | */ | |
a4fc4717 | 167 | #define MIRROR_DISK_VERSION 2 |
1da177e4 LT |
168 | #define LOG_OFFSET 2 |
169 | ||
170 | struct log_header { | |
171 | uint32_t magic; | |
172 | ||
173 | /* | |
174 | * Simple, incrementing version. no backward | |
175 | * compatibility. | |
176 | */ | |
177 | uint32_t version; | |
178 | sector_t nr_regions; | |
179 | }; | |
180 | ||
181 | struct log_c { | |
182 | struct dm_target *ti; | |
183 | int touched; | |
184 | uint32_t region_size; | |
185 | unsigned int region_count; | |
186 | region_t sync_count; | |
187 | ||
188 | unsigned bitset_uint32_count; | |
189 | uint32_t *clean_bits; | |
190 | uint32_t *sync_bits; | |
191 | uint32_t *recovering_bits; /* FIXME: this seems excessive */ | |
192 | ||
193 | int sync_search; | |
194 | ||
195 | /* Resync flag */ | |
196 | enum sync { | |
197 | DEFAULTSYNC, /* Synchronize if necessary */ | |
198 | NOSYNC, /* Devices known to be already in sync */ | |
199 | FORCESYNC, /* Force a sync to happen */ | |
200 | } sync; | |
201 | ||
5d234d1e HM |
202 | struct dm_io_request io_req; |
203 | ||
1da177e4 LT |
204 | /* |
205 | * Disk log fields | |
206 | */ | |
01d03a66 | 207 | int log_dev_failed; |
1da177e4 LT |
208 | struct dm_dev *log_dev; |
209 | struct log_header header; | |
210 | ||
211 | struct io_region header_location; | |
212 | struct log_header *disk_header; | |
1da177e4 LT |
213 | }; |
214 | ||
215 | /* | |
216 | * The touched member needs to be updated every time we access | |
217 | * one of the bitsets. | |
218 | */ | |
219 | static inline int log_test_bit(uint32_t *bs, unsigned bit) | |
220 | { | |
a4fc4717 | 221 | return ext2_test_bit(bit, (unsigned long *) bs) ? 1 : 0; |
1da177e4 LT |
222 | } |
223 | ||
224 | static inline void log_set_bit(struct log_c *l, | |
225 | uint32_t *bs, unsigned bit) | |
226 | { | |
a4fc4717 | 227 | ext2_set_bit(bit, (unsigned long *) bs); |
1da177e4 LT |
228 | l->touched = 1; |
229 | } | |
230 | ||
231 | static inline void log_clear_bit(struct log_c *l, | |
232 | uint32_t *bs, unsigned bit) | |
233 | { | |
a4fc4717 | 234 | ext2_clear_bit(bit, (unsigned long *) bs); |
1da177e4 LT |
235 | l->touched = 1; |
236 | } | |
237 | ||
238 | /*---------------------------------------------------------------- | |
239 | * Header IO | |
240 | *--------------------------------------------------------------*/ | |
241 | static void header_to_disk(struct log_header *core, struct log_header *disk) | |
242 | { | |
243 | disk->magic = cpu_to_le32(core->magic); | |
244 | disk->version = cpu_to_le32(core->version); | |
245 | disk->nr_regions = cpu_to_le64(core->nr_regions); | |
246 | } | |
247 | ||
248 | static void header_from_disk(struct log_header *core, struct log_header *disk) | |
249 | { | |
250 | core->magic = le32_to_cpu(disk->magic); | |
251 | core->version = le32_to_cpu(disk->version); | |
252 | core->nr_regions = le64_to_cpu(disk->nr_regions); | |
253 | } | |
254 | ||
5d234d1e HM |
255 | static int rw_header(struct log_c *lc, int rw) |
256 | { | |
257 | lc->io_req.bi_rw = rw; | |
258 | lc->io_req.mem.ptr.vma = lc->disk_header; | |
259 | lc->io_req.notify.fn = NULL; | |
260 | ||
261 | return dm_io(&lc->io_req, 1, &lc->header_location, NULL); | |
262 | } | |
263 | ||
1da177e4 LT |
264 | static int read_header(struct log_c *log) |
265 | { | |
266 | int r; | |
1da177e4 | 267 | |
5d234d1e | 268 | r = rw_header(log, READ); |
1da177e4 LT |
269 | if (r) |
270 | return r; | |
271 | ||
272 | header_from_disk(&log->header, log->disk_header); | |
273 | ||
274 | /* New log required? */ | |
275 | if (log->sync != DEFAULTSYNC || log->header.magic != MIRROR_MAGIC) { | |
276 | log->header.magic = MIRROR_MAGIC; | |
277 | log->header.version = MIRROR_DISK_VERSION; | |
278 | log->header.nr_regions = 0; | |
279 | } | |
280 | ||
a4fc4717 PC |
281 | #ifdef __LITTLE_ENDIAN |
282 | if (log->header.version == 1) | |
283 | log->header.version = 2; | |
284 | #endif | |
285 | ||
1da177e4 LT |
286 | if (log->header.version != MIRROR_DISK_VERSION) { |
287 | DMWARN("incompatible disk log version"); | |
288 | return -EINVAL; | |
289 | } | |
290 | ||
291 | return 0; | |
292 | } | |
293 | ||
294 | static inline int write_header(struct log_c *log) | |
295 | { | |
1da177e4 | 296 | header_to_disk(&log->header, log->disk_header); |
5d234d1e | 297 | return rw_header(log, WRITE); |
1da177e4 LT |
298 | } |
299 | ||
1da177e4 LT |
300 | /*---------------------------------------------------------------- |
301 | * core log constructor/destructor | |
302 | * | |
303 | * argv contains region_size followed optionally by [no]sync | |
304 | *--------------------------------------------------------------*/ | |
305 | #define BYTE_SHIFT 3 | |
b7cca195 AK |
306 | static int create_log_context(struct dirty_log *log, struct dm_target *ti, |
307 | unsigned int argc, char **argv, | |
308 | struct dm_dev *dev) | |
1da177e4 LT |
309 | { |
310 | enum sync sync = DEFAULTSYNC; | |
311 | ||
312 | struct log_c *lc; | |
313 | uint32_t region_size; | |
314 | unsigned int region_count; | |
b7cca195 | 315 | size_t bitset_size, buf_size; |
5d234d1e | 316 | int r; |
1da177e4 LT |
317 | |
318 | if (argc < 1 || argc > 2) { | |
b7fd54a7 | 319 | DMWARN("wrong number of arguments to dirty region log"); |
1da177e4 LT |
320 | return -EINVAL; |
321 | } | |
322 | ||
323 | if (argc > 1) { | |
324 | if (!strcmp(argv[1], "sync")) | |
325 | sync = FORCESYNC; | |
326 | else if (!strcmp(argv[1], "nosync")) | |
327 | sync = NOSYNC; | |
328 | else { | |
b7fd54a7 HM |
329 | DMWARN("unrecognised sync argument to " |
330 | "dirty region log: %s", argv[1]); | |
1da177e4 LT |
331 | return -EINVAL; |
332 | } | |
333 | } | |
334 | ||
335 | if (sscanf(argv[0], "%u", ®ion_size) != 1) { | |
336 | DMWARN("invalid region size string"); | |
337 | return -EINVAL; | |
338 | } | |
339 | ||
340 | region_count = dm_sector_div_up(ti->len, region_size); | |
341 | ||
342 | lc = kmalloc(sizeof(*lc), GFP_KERNEL); | |
343 | if (!lc) { | |
344 | DMWARN("couldn't allocate core log"); | |
345 | return -ENOMEM; | |
346 | } | |
347 | ||
348 | lc->ti = ti; | |
349 | lc->touched = 0; | |
350 | lc->region_size = region_size; | |
351 | lc->region_count = region_count; | |
352 | lc->sync = sync; | |
353 | ||
354 | /* | |
0e56822d | 355 | * Work out how many "unsigned long"s we need to hold the bitset. |
1da177e4 LT |
356 | */ |
357 | bitset_size = dm_round_up(region_count, | |
29121bd0 | 358 | sizeof(*lc->clean_bits) << BYTE_SHIFT); |
1da177e4 LT |
359 | bitset_size >>= BYTE_SHIFT; |
360 | ||
29121bd0 | 361 | lc->bitset_uint32_count = bitset_size / sizeof(*lc->clean_bits); |
b7cca195 AK |
362 | |
363 | /* | |
364 | * Disk log? | |
365 | */ | |
366 | if (!dev) { | |
367 | lc->clean_bits = vmalloc(bitset_size); | |
368 | if (!lc->clean_bits) { | |
369 | DMWARN("couldn't allocate clean bitset"); | |
370 | kfree(lc); | |
371 | return -ENOMEM; | |
372 | } | |
373 | lc->disk_header = NULL; | |
374 | } else { | |
375 | lc->log_dev = dev; | |
01d03a66 | 376 | lc->log_dev_failed = 0; |
b7cca195 AK |
377 | lc->header_location.bdev = lc->log_dev->bdev; |
378 | lc->header_location.sector = 0; | |
379 | ||
380 | /* | |
381 | * Buffer holds both header and bitset. | |
382 | */ | |
383 | buf_size = dm_round_up((LOG_OFFSET << SECTOR_SHIFT) + | |
384 | bitset_size, ti->limits.hardsect_size); | |
385 | lc->header_location.count = buf_size >> SECTOR_SHIFT; | |
5d234d1e HM |
386 | lc->io_req.mem.type = DM_IO_VMA; |
387 | lc->io_req.client = dm_io_client_create(dm_div_up(buf_size, | |
388 | PAGE_SIZE)); | |
389 | if (IS_ERR(lc->io_req.client)) { | |
390 | r = PTR_ERR(lc->io_req.client); | |
391 | DMWARN("couldn't allocate disk io client"); | |
392 | kfree(lc); | |
393 | return -ENOMEM; | |
394 | } | |
b7cca195 AK |
395 | |
396 | lc->disk_header = vmalloc(buf_size); | |
397 | if (!lc->disk_header) { | |
398 | DMWARN("couldn't allocate disk log buffer"); | |
399 | kfree(lc); | |
400 | return -ENOMEM; | |
401 | } | |
402 | ||
403 | lc->clean_bits = (void *)lc->disk_header + | |
404 | (LOG_OFFSET << SECTOR_SHIFT); | |
1da177e4 | 405 | } |
b7cca195 | 406 | |
1da177e4 LT |
407 | memset(lc->clean_bits, -1, bitset_size); |
408 | ||
409 | lc->sync_bits = vmalloc(bitset_size); | |
410 | if (!lc->sync_bits) { | |
411 | DMWARN("couldn't allocate sync bitset"); | |
b7cca195 AK |
412 | if (!dev) |
413 | vfree(lc->clean_bits); | |
414 | vfree(lc->disk_header); | |
1da177e4 LT |
415 | kfree(lc); |
416 | return -ENOMEM; | |
417 | } | |
418 | memset(lc->sync_bits, (sync == NOSYNC) ? -1 : 0, bitset_size); | |
419 | lc->sync_count = (sync == NOSYNC) ? region_count : 0; | |
420 | ||
421 | lc->recovering_bits = vmalloc(bitset_size); | |
422 | if (!lc->recovering_bits) { | |
423 | DMWARN("couldn't allocate sync bitset"); | |
424 | vfree(lc->sync_bits); | |
b7cca195 AK |
425 | if (!dev) |
426 | vfree(lc->clean_bits); | |
427 | vfree(lc->disk_header); | |
1da177e4 LT |
428 | kfree(lc); |
429 | return -ENOMEM; | |
430 | } | |
431 | memset(lc->recovering_bits, 0, bitset_size); | |
432 | lc->sync_search = 0; | |
433 | log->context = lc; | |
b7cca195 | 434 | |
1da177e4 LT |
435 | return 0; |
436 | } | |
437 | ||
b7cca195 AK |
438 | static int core_ctr(struct dirty_log *log, struct dm_target *ti, |
439 | unsigned int argc, char **argv) | |
440 | { | |
441 | return create_log_context(log, ti, argc, argv, NULL); | |
442 | } | |
443 | ||
444 | static void destroy_log_context(struct log_c *lc) | |
1da177e4 | 445 | { |
1da177e4 LT |
446 | vfree(lc->sync_bits); |
447 | vfree(lc->recovering_bits); | |
448 | kfree(lc); | |
449 | } | |
450 | ||
b7cca195 AK |
451 | static void core_dtr(struct dirty_log *log) |
452 | { | |
453 | struct log_c *lc = (struct log_c *) log->context; | |
454 | ||
455 | vfree(lc->clean_bits); | |
456 | destroy_log_context(lc); | |
457 | } | |
458 | ||
1da177e4 LT |
459 | /*---------------------------------------------------------------- |
460 | * disk log constructor/destructor | |
461 | * | |
462 | * argv contains log_device region_size followed optionally by [no]sync | |
463 | *--------------------------------------------------------------*/ | |
464 | static int disk_ctr(struct dirty_log *log, struct dm_target *ti, | |
465 | unsigned int argc, char **argv) | |
466 | { | |
467 | int r; | |
1da177e4 LT |
468 | struct dm_dev *dev; |
469 | ||
470 | if (argc < 2 || argc > 3) { | |
b7fd54a7 | 471 | DMWARN("wrong number of arguments to disk dirty region log"); |
1da177e4 LT |
472 | return -EINVAL; |
473 | } | |
474 | ||
475 | r = dm_get_device(ti, argv[0], 0, 0 /* FIXME */, | |
476 | FMODE_READ | FMODE_WRITE, &dev); | |
477 | if (r) | |
478 | return r; | |
479 | ||
b7cca195 | 480 | r = create_log_context(log, ti, argc - 1, argv + 1, dev); |
1da177e4 LT |
481 | if (r) { |
482 | dm_put_device(ti, dev); | |
483 | return r; | |
484 | } | |
485 | ||
1da177e4 | 486 | return 0; |
1da177e4 LT |
487 | } |
488 | ||
489 | static void disk_dtr(struct dirty_log *log) | |
490 | { | |
491 | struct log_c *lc = (struct log_c *) log->context; | |
b7cca195 | 492 | |
1da177e4 LT |
493 | dm_put_device(lc->ti, lc->log_dev); |
494 | vfree(lc->disk_header); | |
5d234d1e | 495 | dm_io_client_destroy(lc->io_req.client); |
b7cca195 | 496 | destroy_log_context(lc); |
1da177e4 LT |
497 | } |
498 | ||
499 | static int count_bits32(uint32_t *addr, unsigned size) | |
500 | { | |
501 | int count = 0, i; | |
502 | ||
503 | for (i = 0; i < size; i++) { | |
504 | count += hweight32(*(addr+i)); | |
505 | } | |
506 | return count; | |
507 | } | |
508 | ||
01d03a66 JB |
509 | static void fail_log_device(struct log_c *lc) |
510 | { | |
511 | if (lc->log_dev_failed) | |
512 | return; | |
513 | ||
514 | lc->log_dev_failed = 1; | |
515 | dm_table_event(lc->ti->table); | |
516 | } | |
517 | ||
1da177e4 LT |
518 | static int disk_resume(struct dirty_log *log) |
519 | { | |
520 | int r; | |
521 | unsigned i; | |
522 | struct log_c *lc = (struct log_c *) log->context; | |
523 | size_t size = lc->bitset_uint32_count * sizeof(uint32_t); | |
524 | ||
525 | /* read the disk header */ | |
526 | r = read_header(lc); | |
01d03a66 | 527 | if (r) { |
b7fd54a7 | 528 | DMWARN("%s: Failed to read header on dirty region log device", |
01d03a66 JB |
529 | lc->log_dev->name); |
530 | fail_log_device(lc); | |
ba8b45ce JB |
531 | /* |
532 | * If the log device cannot be read, we must assume | |
533 | * all regions are out-of-sync. If we simply return | |
534 | * here, the state will be uninitialized and could | |
535 | * lead us to return 'in-sync' status for regions | |
536 | * that are actually 'out-of-sync'. | |
537 | */ | |
538 | lc->header.nr_regions = 0; | |
01d03a66 | 539 | } |
1da177e4 | 540 | |
8a835f11 | 541 | /* set or clear any new bits -- device has grown */ |
1da177e4 LT |
542 | if (lc->sync == NOSYNC) |
543 | for (i = lc->header.nr_regions; i < lc->region_count; i++) | |
544 | /* FIXME: amazingly inefficient */ | |
545 | log_set_bit(lc, lc->clean_bits, i); | |
546 | else | |
547 | for (i = lc->header.nr_regions; i < lc->region_count; i++) | |
548 | /* FIXME: amazingly inefficient */ | |
549 | log_clear_bit(lc, lc->clean_bits, i); | |
550 | ||
8a835f11 AK |
551 | /* clear any old bits -- device has shrunk */ |
552 | for (i = lc->region_count; i % (sizeof(*lc->clean_bits) << BYTE_SHIFT); i++) | |
553 | log_clear_bit(lc, lc->clean_bits, i); | |
554 | ||
1da177e4 LT |
555 | /* copy clean across to sync */ |
556 | memcpy(lc->sync_bits, lc->clean_bits, size); | |
557 | lc->sync_count = count_bits32(lc->clean_bits, lc->bitset_uint32_count); | |
88b20a1a | 558 | lc->sync_search = 0; |
1da177e4 | 559 | |
1da177e4 LT |
560 | /* set the correct number of regions in the header */ |
561 | lc->header.nr_regions = lc->region_count; | |
562 | ||
563 | /* write the new header */ | |
01d03a66 JB |
564 | r = write_header(lc); |
565 | if (r) { | |
b7fd54a7 | 566 | DMWARN("%s: Failed to write header on dirty region log device", |
01d03a66 JB |
567 | lc->log_dev->name); |
568 | fail_log_device(lc); | |
569 | } | |
570 | ||
571 | return r; | |
1da177e4 LT |
572 | } |
573 | ||
574 | static uint32_t core_get_region_size(struct dirty_log *log) | |
575 | { | |
576 | struct log_c *lc = (struct log_c *) log->context; | |
577 | return lc->region_size; | |
578 | } | |
579 | ||
88b20a1a JB |
580 | static int core_resume(struct dirty_log *log) |
581 | { | |
582 | struct log_c *lc = (struct log_c *) log->context; | |
583 | lc->sync_search = 0; | |
584 | return 0; | |
585 | } | |
586 | ||
1da177e4 LT |
587 | static int core_is_clean(struct dirty_log *log, region_t region) |
588 | { | |
589 | struct log_c *lc = (struct log_c *) log->context; | |
590 | return log_test_bit(lc->clean_bits, region); | |
591 | } | |
592 | ||
593 | static int core_in_sync(struct dirty_log *log, region_t region, int block) | |
594 | { | |
595 | struct log_c *lc = (struct log_c *) log->context; | |
596 | return log_test_bit(lc->sync_bits, region); | |
597 | } | |
598 | ||
599 | static int core_flush(struct dirty_log *log) | |
600 | { | |
601 | /* no op */ | |
602 | return 0; | |
603 | } | |
604 | ||
605 | static int disk_flush(struct dirty_log *log) | |
606 | { | |
607 | int r; | |
608 | struct log_c *lc = (struct log_c *) log->context; | |
609 | ||
610 | /* only write if the log has changed */ | |
611 | if (!lc->touched) | |
612 | return 0; | |
613 | ||
702ca6f0 | 614 | r = write_header(lc); |
01d03a66 JB |
615 | if (r) |
616 | fail_log_device(lc); | |
617 | else | |
1da177e4 LT |
618 | lc->touched = 0; |
619 | ||
620 | return r; | |
621 | } | |
622 | ||
623 | static void core_mark_region(struct dirty_log *log, region_t region) | |
624 | { | |
625 | struct log_c *lc = (struct log_c *) log->context; | |
626 | log_clear_bit(lc, lc->clean_bits, region); | |
627 | } | |
628 | ||
629 | static void core_clear_region(struct dirty_log *log, region_t region) | |
630 | { | |
631 | struct log_c *lc = (struct log_c *) log->context; | |
632 | log_set_bit(lc, lc->clean_bits, region); | |
633 | } | |
634 | ||
635 | static int core_get_resync_work(struct dirty_log *log, region_t *region) | |
636 | { | |
637 | struct log_c *lc = (struct log_c *) log->context; | |
638 | ||
639 | if (lc->sync_search >= lc->region_count) | |
640 | return 0; | |
641 | ||
642 | do { | |
1113a7e9 SB |
643 | *region = ext2_find_next_zero_bit( |
644 | (unsigned long *) lc->sync_bits, | |
1da177e4 LT |
645 | lc->region_count, |
646 | lc->sync_search); | |
647 | lc->sync_search = *region + 1; | |
648 | ||
ac81b2ee | 649 | if (*region >= lc->region_count) |
1da177e4 LT |
650 | return 0; |
651 | ||
652 | } while (log_test_bit(lc->recovering_bits, *region)); | |
653 | ||
654 | log_set_bit(lc, lc->recovering_bits, *region); | |
655 | return 1; | |
656 | } | |
657 | ||
f3ee6b2f JB |
658 | static void core_set_region_sync(struct dirty_log *log, region_t region, |
659 | int in_sync) | |
1da177e4 LT |
660 | { |
661 | struct log_c *lc = (struct log_c *) log->context; | |
662 | ||
663 | log_clear_bit(lc, lc->recovering_bits, region); | |
f3ee6b2f | 664 | if (in_sync) { |
1da177e4 LT |
665 | log_set_bit(lc, lc->sync_bits, region); |
666 | lc->sync_count++; | |
f3ee6b2f JB |
667 | } else if (log_test_bit(lc->sync_bits, region)) { |
668 | lc->sync_count--; | |
669 | log_clear_bit(lc, lc->sync_bits, region); | |
670 | } | |
1da177e4 LT |
671 | } |
672 | ||
673 | static region_t core_get_sync_count(struct dirty_log *log) | |
674 | { | |
675 | struct log_c *lc = (struct log_c *) log->context; | |
676 | ||
677 | return lc->sync_count; | |
678 | } | |
679 | ||
680 | #define DMEMIT_SYNC \ | |
681 | if (lc->sync != DEFAULTSYNC) \ | |
682 | DMEMIT("%ssync ", lc->sync == NOSYNC ? "no" : "") | |
683 | ||
684 | static int core_status(struct dirty_log *log, status_type_t status, | |
685 | char *result, unsigned int maxlen) | |
686 | { | |
687 | int sz = 0; | |
688 | struct log_c *lc = log->context; | |
689 | ||
690 | switch(status) { | |
691 | case STATUSTYPE_INFO: | |
315dcc22 | 692 | DMEMIT("1 %s", log->type->name); |
1da177e4 LT |
693 | break; |
694 | ||
695 | case STATUSTYPE_TABLE: | |
696 | DMEMIT("%s %u %u ", log->type->name, | |
697 | lc->sync == DEFAULTSYNC ? 1 : 2, lc->region_size); | |
698 | DMEMIT_SYNC; | |
699 | } | |
700 | ||
701 | return sz; | |
702 | } | |
703 | ||
704 | static int disk_status(struct dirty_log *log, status_type_t status, | |
705 | char *result, unsigned int maxlen) | |
706 | { | |
707 | int sz = 0; | |
1da177e4 LT |
708 | struct log_c *lc = log->context; |
709 | ||
710 | switch(status) { | |
711 | case STATUSTYPE_INFO: | |
315dcc22 JB |
712 | DMEMIT("3 %s %s %c", log->type->name, lc->log_dev->name, |
713 | lc->log_dev_failed ? 'D' : 'A'); | |
1da177e4 LT |
714 | break; |
715 | ||
716 | case STATUSTYPE_TABLE: | |
1da177e4 | 717 | DMEMIT("%s %u %s %u ", log->type->name, |
315dcc22 | 718 | lc->sync == DEFAULTSYNC ? 2 : 3, lc->log_dev->name, |
1da177e4 LT |
719 | lc->region_size); |
720 | DMEMIT_SYNC; | |
721 | } | |
722 | ||
723 | return sz; | |
724 | } | |
725 | ||
726 | static struct dirty_log_type _core_type = { | |
727 | .name = "core", | |
728 | .module = THIS_MODULE, | |
729 | .ctr = core_ctr, | |
730 | .dtr = core_dtr, | |
88b20a1a | 731 | .resume = core_resume, |
1da177e4 LT |
732 | .get_region_size = core_get_region_size, |
733 | .is_clean = core_is_clean, | |
734 | .in_sync = core_in_sync, | |
735 | .flush = core_flush, | |
736 | .mark_region = core_mark_region, | |
737 | .clear_region = core_clear_region, | |
738 | .get_resync_work = core_get_resync_work, | |
f3ee6b2f | 739 | .set_region_sync = core_set_region_sync, |
1da177e4 LT |
740 | .get_sync_count = core_get_sync_count, |
741 | .status = core_status, | |
742 | }; | |
743 | ||
744 | static struct dirty_log_type _disk_type = { | |
745 | .name = "disk", | |
746 | .module = THIS_MODULE, | |
747 | .ctr = disk_ctr, | |
748 | .dtr = disk_dtr, | |
6b3df0d7 | 749 | .postsuspend = disk_flush, |
1da177e4 LT |
750 | .resume = disk_resume, |
751 | .get_region_size = core_get_region_size, | |
752 | .is_clean = core_is_clean, | |
753 | .in_sync = core_in_sync, | |
754 | .flush = disk_flush, | |
755 | .mark_region = core_mark_region, | |
756 | .clear_region = core_clear_region, | |
757 | .get_resync_work = core_get_resync_work, | |
f3ee6b2f | 758 | .set_region_sync = core_set_region_sync, |
1da177e4 LT |
759 | .get_sync_count = core_get_sync_count, |
760 | .status = disk_status, | |
761 | }; | |
762 | ||
763 | int __init dm_dirty_log_init(void) | |
764 | { | |
765 | int r; | |
766 | ||
767 | r = dm_register_dirty_log_type(&_core_type); | |
768 | if (r) | |
769 | DMWARN("couldn't register core log"); | |
770 | ||
771 | r = dm_register_dirty_log_type(&_disk_type); | |
772 | if (r) { | |
773 | DMWARN("couldn't register disk type"); | |
774 | dm_unregister_dirty_log_type(&_core_type); | |
775 | } | |
776 | ||
777 | return r; | |
778 | } | |
779 | ||
769aef30 | 780 | void __exit dm_dirty_log_exit(void) |
1da177e4 LT |
781 | { |
782 | dm_unregister_dirty_log_type(&_disk_type); | |
783 | dm_unregister_dirty_log_type(&_core_type); | |
784 | } | |
785 | ||
786 | EXPORT_SYMBOL(dm_register_dirty_log_type); | |
787 | EXPORT_SYMBOL(dm_unregister_dirty_log_type); | |
788 | EXPORT_SYMBOL(dm_create_dirty_log); | |
789 | EXPORT_SYMBOL(dm_destroy_dirty_log); | |
769aef30 HM |
790 | |
791 | module_init(dm_dirty_log_init); | |
792 | module_exit(dm_dirty_log_exit); | |
793 | ||
794 | MODULE_DESCRIPTION(DM_NAME " dirty region log"); | |
795 | MODULE_AUTHOR("Joe Thornber, Heinz Mauelshagen <dm-devel@redhat.com>"); | |
796 | MODULE_LICENSE("GPL"); |