blob: f6b20def2ac82583e84d88db06e686514f56f775 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2003 Sistina Software
Heinz Mauelshagenb7fd54a2008-04-24 21:43:06 +01003 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
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
Heinz Mauelshagenb7fd54a2008-04-24 21:43:06 +010016#define DM_MSG_PREFIX "dirty region log"
Alasdair G Kergon72d94862006-06-26 00:27:35 -070017
Linus Torvalds1da177e2005-04-16 15:20:36 -070018static LIST_HEAD(_log_types);
19static DEFINE_SPINLOCK(_lock);
20
21int 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
31int 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
Jonathan Brassowfb8b2842008-02-08 02:11:19 +000045static struct dirty_log_type *_get_type(const char *type_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
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
Jonathan Brassowfb8b2842008-02-08 02:11:19 +000065/*
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 */
82static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114static 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
122struct 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
148void 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 */
Patrick Caulfielda4fc4712006-02-01 03:04:51 -0800167#define MIRROR_DISK_VERSION 2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168#define LOG_OFFSET 2
169
170struct 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
181struct 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
Heinz Mauelshagen5d234d12007-05-09 02:33:03 -0700202 struct dm_io_request io_req;
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 /*
205 * Disk log fields
206 */
Jonathan E Brassow01d03a62007-05-09 02:32:57 -0700207 int log_dev_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 struct dm_dev *log_dev;
209 struct log_header header;
210
211 struct io_region header_location;
212 struct log_header *disk_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213};
214
215/*
216 * The touched member needs to be updated every time we access
217 * one of the bitsets.
218 */
219static inline int log_test_bit(uint32_t *bs, unsigned bit)
220{
Patrick Caulfielda4fc4712006-02-01 03:04:51 -0800221 return ext2_test_bit(bit, (unsigned long *) bs) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
224static inline void log_set_bit(struct log_c *l,
225 uint32_t *bs, unsigned bit)
226{
Patrick Caulfielda4fc4712006-02-01 03:04:51 -0800227 ext2_set_bit(bit, (unsigned long *) bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 l->touched = 1;
229}
230
231static inline void log_clear_bit(struct log_c *l,
232 uint32_t *bs, unsigned bit)
233{
Patrick Caulfielda4fc4712006-02-01 03:04:51 -0800234 ext2_clear_bit(bit, (unsigned long *) bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 l->touched = 1;
236}
237
238/*----------------------------------------------------------------
239 * Header IO
240 *--------------------------------------------------------------*/
241static 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
248static 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
Heinz Mauelshagen5d234d12007-05-09 02:33:03 -0700255static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264static int read_header(struct log_c *log)
265{
266 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Heinz Mauelshagen5d234d12007-05-09 02:33:03 -0700268 r = rw_header(log, READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 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
Patrick Caulfielda4fc4712006-02-01 03:04:51 -0800281#ifdef __LITTLE_ENDIAN
282 if (log->header.version == 1)
283 log->header.version = 2;
284#endif
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 if (log->header.version != MIRROR_DISK_VERSION) {
287 DMWARN("incompatible disk log version");
288 return -EINVAL;
289 }
290
291 return 0;
292}
293
294static inline int write_header(struct log_c *log)
295{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 header_to_disk(&log->header, log->disk_header);
Heinz Mauelshagen5d234d12007-05-09 02:33:03 -0700297 return rw_header(log, WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
300/*----------------------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 * core log constructor/destructor
302 *
303 * argv contains region_size followed optionally by [no]sync
304 *--------------------------------------------------------------*/
305#define BYTE_SHIFT 3
Alasdair G Kergonb7cca192006-06-26 00:27:29 -0700306static int create_log_context(struct dirty_log *log, struct dm_target *ti,
307 unsigned int argc, char **argv,
308 struct dm_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
310 enum sync sync = DEFAULTSYNC;
311
312 struct log_c *lc;
313 uint32_t region_size;
314 unsigned int region_count;
Alasdair G Kergonb7cca192006-06-26 00:27:29 -0700315 size_t bitset_size, buf_size;
Heinz Mauelshagen5d234d12007-05-09 02:33:03 -0700316 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 if (argc < 1 || argc > 2) {
Heinz Mauelshagenb7fd54a2008-04-24 21:43:06 +0100319 DMWARN("wrong number of arguments to dirty region log");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 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 {
Heinz Mauelshagenb7fd54a2008-04-24 21:43:06 +0100329 DMWARN("unrecognised sync argument to "
330 "dirty region log: %s", argv[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return -EINVAL;
332 }
333 }
334
335 if (sscanf(argv[0], "%u", &region_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 /*
Alasdair G Kergon0e568222005-11-21 21:32:34 -0800355 * Work out how many "unsigned long"s we need to hold the bitset.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 */
357 bitset_size = dm_round_up(region_count,
Alasdair G Kergon29121bd2006-06-26 00:27:29 -0700358 sizeof(*lc->clean_bits) << BYTE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 bitset_size >>= BYTE_SHIFT;
360
Alasdair G Kergon29121bd2006-06-26 00:27:29 -0700361 lc->bitset_uint32_count = bitset_size / sizeof(*lc->clean_bits);
Alasdair G Kergonb7cca192006-06-26 00:27:29 -0700362
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;
Jonathan E Brassow01d03a62007-05-09 02:32:57 -0700376 lc->log_dev_failed = 0;
Alasdair G Kergonb7cca192006-06-26 00:27:29 -0700377 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;
Heinz Mauelshagen5d234d12007-05-09 02:33:03 -0700386 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 }
Alasdair G Kergonb7cca192006-06-26 00:27:29 -0700395
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
Alasdair G Kergonb7cca192006-06-26 00:27:29 -0700406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 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");
Alasdair G Kergonb7cca192006-06-26 00:27:29 -0700412 if (!dev)
413 vfree(lc->clean_bits);
414 vfree(lc->disk_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 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);
Alasdair G Kergonb7cca192006-06-26 00:27:29 -0700425 if (!dev)
426 vfree(lc->clean_bits);
427 vfree(lc->disk_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 kfree(lc);
429 return -ENOMEM;
430 }
431 memset(lc->recovering_bits, 0, bitset_size);
432 lc->sync_search = 0;
433 log->context = lc;
Alasdair G Kergonb7cca192006-06-26 00:27:29 -0700434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 return 0;
436}
437
Alasdair G Kergonb7cca192006-06-26 00:27:29 -0700438static 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
444static void destroy_log_context(struct log_c *lc)
445{
446 vfree(lc->sync_bits);
447 vfree(lc->recovering_bits);
448 kfree(lc);
449}
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451static void core_dtr(struct dirty_log *log)
452{
453 struct log_c *lc = (struct log_c *) log->context;
Alasdair G Kergonb7cca192006-06-26 00:27:29 -0700454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 vfree(lc->clean_bits);
Alasdair G Kergonb7cca192006-06-26 00:27:29 -0700456 destroy_log_context(lc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
459/*----------------------------------------------------------------
460 * disk log constructor/destructor
461 *
462 * argv contains log_device region_size followed optionally by [no]sync
463 *--------------------------------------------------------------*/
464static int disk_ctr(struct dirty_log *log, struct dm_target *ti,
465 unsigned int argc, char **argv)
466{
467 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 struct dm_dev *dev;
469
470 if (argc < 2 || argc > 3) {
Heinz Mauelshagenb7fd54a2008-04-24 21:43:06 +0100471 DMWARN("wrong number of arguments to disk dirty region log");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 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
Alasdair G Kergonb7cca192006-06-26 00:27:29 -0700480 r = create_log_context(log, ti, argc - 1, argv + 1, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (r) {
482 dm_put_device(ti, dev);
483 return r;
484 }
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
489static void disk_dtr(struct dirty_log *log)
490{
491 struct log_c *lc = (struct log_c *) log->context;
Alasdair G Kergonb7cca192006-06-26 00:27:29 -0700492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 dm_put_device(lc->ti, lc->log_dev);
494 vfree(lc->disk_header);
Heinz Mauelshagen5d234d12007-05-09 02:33:03 -0700495 dm_io_client_destroy(lc->io_req.client);
Alasdair G Kergonb7cca192006-06-26 00:27:29 -0700496 destroy_log_context(lc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
499static 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
Jonathan E Brassow01d03a62007-05-09 02:32:57 -0700509static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518static 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);
Jonathan E Brassow01d03a62007-05-09 02:32:57 -0700527 if (r) {
Heinz Mauelshagenb7fd54a2008-04-24 21:43:06 +0100528 DMWARN("%s: Failed to read header on dirty region log device",
Jonathan E Brassow01d03a62007-05-09 02:32:57 -0700529 lc->log_dev->name);
530 fail_log_device(lc);
Jonathan Brassowba8b45c2007-05-09 02:33:08 -0700531 /*
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;
Jonathan E Brassow01d03a62007-05-09 02:32:57 -0700539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Alasdair G Kergon8a835f12006-06-26 00:27:30 -0700541 /* set or clear any new bits -- device has grown */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 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
Alasdair G Kergon8a835f12006-06-26 00:27:30 -0700551 /* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 /* 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);
Jonathan E Brassow88b20a12006-12-08 02:41:12 -0800558 lc->sync_search = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 /* set the correct number of regions in the header */
561 lc->header.nr_regions = lc->region_count;
562
563 /* write the new header */
Jonathan E Brassow01d03a62007-05-09 02:32:57 -0700564 r = write_header(lc);
565 if (r) {
Heinz Mauelshagenb7fd54a2008-04-24 21:43:06 +0100566 DMWARN("%s: Failed to write header on dirty region log device",
Jonathan E Brassow01d03a62007-05-09 02:32:57 -0700567 lc->log_dev->name);
568 fail_log_device(lc);
569 }
570
571 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
574static 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
Jonathan E Brassow88b20a12006-12-08 02:41:12 -0800580static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587static 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
593static 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
599static int core_flush(struct dirty_log *log)
600{
601 /* no op */
602 return 0;
603}
604
605static 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
Kevin Corry702ca6f2006-06-26 00:27:28 -0700614 r = write_header(lc);
Jonathan E Brassow01d03a62007-05-09 02:32:57 -0700615 if (r)
616 fail_log_device(lc);
617 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 lc->touched = 0;
619
620 return r;
621}
622
623static 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
629static 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
635static 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 {
Stefan Bader1113a7e2006-02-02 14:28:07 -0800643 *region = ext2_find_next_zero_bit(
644 (unsigned long *) lc->sync_bits,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 lc->region_count,
646 lc->sync_search);
647 lc->sync_search = *region + 1;
648
Darrick J. Wongac81b2e2006-01-06 00:20:11 -0800649 if (*region >= lc->region_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 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
Jonathan E Brassowf3ee6b22006-12-08 02:41:11 -0800658static void core_set_region_sync(struct dirty_log *log, region_t region,
659 int in_sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
661 struct log_c *lc = (struct log_c *) log->context;
662
663 log_clear_bit(lc, lc->recovering_bits, region);
Jonathan E Brassowf3ee6b22006-12-08 02:41:11 -0800664 if (in_sync) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 log_set_bit(lc, lc->sync_bits, region);
666 lc->sync_count++;
Jonathan E Brassowf3ee6b22006-12-08 02:41:11 -0800667 } else if (log_test_bit(lc->sync_bits, region)) {
668 lc->sync_count--;
669 log_clear_bit(lc, lc->sync_bits, region);
670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
672
673static 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
684static 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:
Jonathan E Brassow315dcc22007-05-09 02:32:58 -0700692 DMEMIT("1 %s", log->type->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 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
704static int disk_status(struct dirty_log *log, status_type_t status,
705 char *result, unsigned int maxlen)
706{
707 int sz = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 struct log_c *lc = log->context;
709
710 switch(status) {
711 case STATUSTYPE_INFO:
Jonathan E Brassow315dcc22007-05-09 02:32:58 -0700712 DMEMIT("3 %s %s %c", log->type->name, lc->log_dev->name,
713 lc->log_dev_failed ? 'D' : 'A');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 break;
715
716 case STATUSTYPE_TABLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 DMEMIT("%s %u %s %u ", log->type->name,
Jonathan E Brassow315dcc22007-05-09 02:32:58 -0700718 lc->sync == DEFAULTSYNC ? 2 : 3, lc->log_dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 lc->region_size);
720 DMEMIT_SYNC;
721 }
722
723 return sz;
724}
725
726static struct dirty_log_type _core_type = {
727 .name = "core",
728 .module = THIS_MODULE,
729 .ctr = core_ctr,
730 .dtr = core_dtr,
Jonathan E Brassow88b20a12006-12-08 02:41:12 -0800731 .resume = core_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 .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,
Jonathan E Brassowf3ee6b22006-12-08 02:41:11 -0800739 .set_region_sync = core_set_region_sync,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 .get_sync_count = core_get_sync_count,
741 .status = core_status,
742};
743
744static struct dirty_log_type _disk_type = {
745 .name = "disk",
746 .module = THIS_MODULE,
747 .ctr = disk_ctr,
748 .dtr = disk_dtr,
Jonathan Brassow6b3df0d2007-10-19 22:47:57 +0100749 .postsuspend = disk_flush,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 .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,
Jonathan E Brassowf3ee6b22006-12-08 02:41:11 -0800758 .set_region_sync = core_set_region_sync,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 .get_sync_count = core_get_sync_count,
760 .status = disk_status,
761};
762
763int __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
Heinz Mauelshagen769aef32008-04-24 21:43:09 +0100780void __exit dm_dirty_log_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
782 dm_unregister_dirty_log_type(&_disk_type);
783 dm_unregister_dirty_log_type(&_core_type);
784}
785
786EXPORT_SYMBOL(dm_register_dirty_log_type);
787EXPORT_SYMBOL(dm_unregister_dirty_log_type);
788EXPORT_SYMBOL(dm_create_dirty_log);
789EXPORT_SYMBOL(dm_destroy_dirty_log);
Heinz Mauelshagen769aef32008-04-24 21:43:09 +0100790
791module_init(dm_dirty_log_init);
792module_exit(dm_dirty_log_exit);
793
794MODULE_DESCRIPTION(DM_NAME " dirty region log");
795MODULE_AUTHOR("Joe Thornber, Heinz Mauelshagen <dm-devel@redhat.com>");
796MODULE_LICENSE("GPL");