blob: b776701cc8faa2a9fd07be92314cf58c18c32697 [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"
Heinz Mauelshagen416cd172008-04-24 21:43:35 +010015#include "dm.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Heinz Mauelshagenb7fd54a2008-04-24 21:43:06 +010017#define DM_MSG_PREFIX "dirty region log"
Alasdair G Kergon72d94862006-06-26 00:27:35 -070018
Linus Torvalds1da177e2005-04-16 15:20:36 -070019static LIST_HEAD(_log_types);
20static DEFINE_SPINLOCK(_lock);
21
Heinz Mauelshagen416cd172008-04-24 21:43:35 +010022int dm_dirty_log_type_register(struct dm_dirty_log_type *type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070023{
24 spin_lock(&_lock);
25 type->use_count = 0;
26 list_add(&type->list, &_log_types);
27 spin_unlock(&_lock);
28
29 return 0;
30}
Heinz Mauelshagen416cd172008-04-24 21:43:35 +010031EXPORT_SYMBOL(dm_dirty_log_type_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Heinz Mauelshagen416cd172008-04-24 21:43:35 +010033int dm_dirty_log_type_unregister(struct dm_dirty_log_type *type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
35 spin_lock(&_lock);
36
37 if (type->use_count)
38 DMWARN("Attempt to unregister a log type that is still in use");
39 else
40 list_del(&type->list);
41
42 spin_unlock(&_lock);
43
44 return 0;
45}
Heinz Mauelshagen416cd172008-04-24 21:43:35 +010046EXPORT_SYMBOL(dm_dirty_log_type_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Heinz Mauelshagen416cd172008-04-24 21:43:35 +010048static struct dm_dirty_log_type *_get_type(const char *type_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Heinz Mauelshagen416cd172008-04-24 21:43:35 +010050 struct dm_dirty_log_type *type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52 spin_lock(&_lock);
53 list_for_each_entry (type, &_log_types, list)
54 if (!strcmp(type_name, type->name)) {
55 if (!type->use_count && !try_module_get(type->module)){
56 spin_unlock(&_lock);
57 return NULL;
58 }
59 type->use_count++;
60 spin_unlock(&_lock);
61 return type;
62 }
63
64 spin_unlock(&_lock);
65 return NULL;
66}
67
Jonathan Brassowfb8b2842008-02-08 02:11:19 +000068/*
69 * get_type
70 * @type_name
71 *
72 * Attempt to retrieve the dirty_log_type by name. If not already
73 * available, attempt to load the appropriate module.
74 *
75 * Log modules are named "dm-log-" followed by the 'type_name'.
76 * Modules may contain multiple types.
77 * This function will first try the module "dm-log-<type_name>",
78 * then truncate 'type_name' on the last '-' and try again.
79 *
80 * For example, if type_name was "clustered-disk", it would search
81 * 'dm-log-clustered-disk' then 'dm-log-clustered'.
82 *
83 * Returns: dirty_log_type* on success, NULL on failure
84 */
Heinz Mauelshagen416cd172008-04-24 21:43:35 +010085static struct dm_dirty_log_type *get_type(const char *type_name)
Jonathan Brassowfb8b2842008-02-08 02:11:19 +000086{
87 char *p, *type_name_dup;
Heinz Mauelshagen416cd172008-04-24 21:43:35 +010088 struct dm_dirty_log_type *type;
Jonathan Brassowfb8b2842008-02-08 02:11:19 +000089
90 type = _get_type(type_name);
91 if (type)
92 return type;
93
94 type_name_dup = kstrdup(type_name, GFP_KERNEL);
95 if (!type_name_dup) {
96 DMWARN("No memory left to attempt log module load for \"%s\"",
97 type_name);
98 return NULL;
99 }
100
101 while (request_module("dm-log-%s", type_name_dup) ||
102 !(type = _get_type(type_name))) {
103 p = strrchr(type_name_dup, '-');
104 if (!p)
105 break;
106 p[0] = '\0';
107 }
108
109 if (!type)
110 DMWARN("Module for logging type \"%s\" not found.", type_name);
111
112 kfree(type_name_dup);
113
114 return type;
115}
116
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100117static void put_type(struct dm_dirty_log_type *type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
119 spin_lock(&_lock);
120 if (!--type->use_count)
121 module_put(type->module);
122 spin_unlock(&_lock);
123}
124
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100125struct dm_dirty_log *dm_dirty_log_create(const char *type_name,
126 struct dm_target *ti,
127 unsigned int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100129 struct dm_dirty_log_type *type;
130 struct dm_dirty_log *log;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 log = kmalloc(sizeof(*log), GFP_KERNEL);
133 if (!log)
134 return NULL;
135
136 type = get_type(type_name);
137 if (!type) {
138 kfree(log);
139 return NULL;
140 }
141
142 log->type = type;
143 if (type->ctr(log, ti, argc, argv)) {
144 kfree(log);
145 put_type(type);
146 return NULL;
147 }
148
149 return log;
150}
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100151EXPORT_SYMBOL(dm_dirty_log_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100153void dm_dirty_log_destroy(struct dm_dirty_log *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
155 log->type->dtr(log);
156 put_type(log->type);
157 kfree(log);
158}
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100159EXPORT_SYMBOL(dm_dirty_log_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161/*-----------------------------------------------------------------
162 * Persistent and core logs share a lot of their implementation.
163 * FIXME: need a reload method to be called from a resume
164 *---------------------------------------------------------------*/
165/*
166 * Magic for persistent mirrors: "MiRr"
167 */
168#define MIRROR_MAGIC 0x4D695272
169
170/*
171 * The on-disk version of the metadata.
172 */
Patrick Caulfielda4fc4712006-02-01 03:04:51 -0800173#define MIRROR_DISK_VERSION 2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174#define LOG_OFFSET 2
175
176struct log_header {
177 uint32_t magic;
178
179 /*
180 * Simple, incrementing version. no backward
181 * compatibility.
182 */
183 uint32_t version;
184 sector_t nr_regions;
185};
186
187struct log_c {
188 struct dm_target *ti;
189 int touched;
190 uint32_t region_size;
191 unsigned int region_count;
192 region_t sync_count;
193
194 unsigned bitset_uint32_count;
195 uint32_t *clean_bits;
196 uint32_t *sync_bits;
197 uint32_t *recovering_bits; /* FIXME: this seems excessive */
198
199 int sync_search;
200
201 /* Resync flag */
202 enum sync {
203 DEFAULTSYNC, /* Synchronize if necessary */
204 NOSYNC, /* Devices known to be already in sync */
205 FORCESYNC, /* Force a sync to happen */
206 } sync;
207
Heinz Mauelshagen5d234d12007-05-09 02:33:03 -0700208 struct dm_io_request io_req;
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 /*
211 * Disk log fields
212 */
Jonathan E Brassow01d03a62007-05-09 02:32:57 -0700213 int log_dev_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 struct dm_dev *log_dev;
215 struct log_header header;
216
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100217 struct dm_io_region header_location;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 struct log_header *disk_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219};
220
221/*
222 * The touched member needs to be updated every time we access
223 * one of the bitsets.
224 */
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100225static inline int log_test_bit(uint32_t *bs, unsigned bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
Patrick Caulfielda4fc4712006-02-01 03:04:51 -0800227 return ext2_test_bit(bit, (unsigned long *) bs) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
230static inline void log_set_bit(struct log_c *l,
231 uint32_t *bs, unsigned bit)
232{
Patrick Caulfielda4fc4712006-02-01 03:04:51 -0800233 ext2_set_bit(bit, (unsigned long *) bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 l->touched = 1;
235}
236
237static inline void log_clear_bit(struct log_c *l,
238 uint32_t *bs, unsigned bit)
239{
Patrick Caulfielda4fc4712006-02-01 03:04:51 -0800240 ext2_clear_bit(bit, (unsigned long *) bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 l->touched = 1;
242}
243
244/*----------------------------------------------------------------
245 * Header IO
246 *--------------------------------------------------------------*/
247static void header_to_disk(struct log_header *core, struct log_header *disk)
248{
249 disk->magic = cpu_to_le32(core->magic);
250 disk->version = cpu_to_le32(core->version);
251 disk->nr_regions = cpu_to_le64(core->nr_regions);
252}
253
254static void header_from_disk(struct log_header *core, struct log_header *disk)
255{
256 core->magic = le32_to_cpu(disk->magic);
257 core->version = le32_to_cpu(disk->version);
258 core->nr_regions = le64_to_cpu(disk->nr_regions);
259}
260
Heinz Mauelshagen5d234d12007-05-09 02:33:03 -0700261static int rw_header(struct log_c *lc, int rw)
262{
263 lc->io_req.bi_rw = rw;
264 lc->io_req.mem.ptr.vma = lc->disk_header;
265 lc->io_req.notify.fn = NULL;
266
267 return dm_io(&lc->io_req, 1, &lc->header_location, NULL);
268}
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270static int read_header(struct log_c *log)
271{
272 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Heinz Mauelshagen5d234d12007-05-09 02:33:03 -0700274 r = rw_header(log, READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 if (r)
276 return r;
277
278 header_from_disk(&log->header, log->disk_header);
279
280 /* New log required? */
281 if (log->sync != DEFAULTSYNC || log->header.magic != MIRROR_MAGIC) {
282 log->header.magic = MIRROR_MAGIC;
283 log->header.version = MIRROR_DISK_VERSION;
284 log->header.nr_regions = 0;
285 }
286
Patrick Caulfielda4fc4712006-02-01 03:04:51 -0800287#ifdef __LITTLE_ENDIAN
288 if (log->header.version == 1)
289 log->header.version = 2;
290#endif
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if (log->header.version != MIRROR_DISK_VERSION) {
293 DMWARN("incompatible disk log version");
294 return -EINVAL;
295 }
296
297 return 0;
298}
299
300static inline int write_header(struct log_c *log)
301{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 header_to_disk(&log->header, log->disk_header);
Heinz Mauelshagen5d234d12007-05-09 02:33:03 -0700303 return rw_header(log, WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
306/*----------------------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 * core log constructor/destructor
308 *
309 * argv contains region_size followed optionally by [no]sync
310 *--------------------------------------------------------------*/
311#define BYTE_SHIFT 3
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100312static int create_log_context(struct dm_dirty_log *log, struct dm_target *ti,
Alasdair G Kergonb7cca192006-06-26 00:27:29 -0700313 unsigned int argc, char **argv,
314 struct dm_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
316 enum sync sync = DEFAULTSYNC;
317
318 struct log_c *lc;
319 uint32_t region_size;
320 unsigned int region_count;
Alasdair G Kergonb7cca192006-06-26 00:27:29 -0700321 size_t bitset_size, buf_size;
Heinz Mauelshagen5d234d12007-05-09 02:33:03 -0700322 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 if (argc < 1 || argc > 2) {
Heinz Mauelshagenb7fd54a2008-04-24 21:43:06 +0100325 DMWARN("wrong number of arguments to dirty region log");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 return -EINVAL;
327 }
328
329 if (argc > 1) {
330 if (!strcmp(argv[1], "sync"))
331 sync = FORCESYNC;
332 else if (!strcmp(argv[1], "nosync"))
333 sync = NOSYNC;
334 else {
Heinz Mauelshagenb7fd54a2008-04-24 21:43:06 +0100335 DMWARN("unrecognised sync argument to "
336 "dirty region log: %s", argv[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return -EINVAL;
338 }
339 }
340
341 if (sscanf(argv[0], "%u", &region_size) != 1) {
342 DMWARN("invalid region size string");
343 return -EINVAL;
344 }
345
346 region_count = dm_sector_div_up(ti->len, region_size);
347
348 lc = kmalloc(sizeof(*lc), GFP_KERNEL);
349 if (!lc) {
350 DMWARN("couldn't allocate core log");
351 return -ENOMEM;
352 }
353
354 lc->ti = ti;
355 lc->touched = 0;
356 lc->region_size = region_size;
357 lc->region_count = region_count;
358 lc->sync = sync;
359
360 /*
Alasdair G Kergon0e568222005-11-21 21:32:34 -0800361 * Work out how many "unsigned long"s we need to hold the bitset.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 */
363 bitset_size = dm_round_up(region_count,
Alasdair G Kergon29121bd2006-06-26 00:27:29 -0700364 sizeof(*lc->clean_bits) << BYTE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 bitset_size >>= BYTE_SHIFT;
366
Alasdair G Kergon29121bd2006-06-26 00:27:29 -0700367 lc->bitset_uint32_count = bitset_size / sizeof(*lc->clean_bits);
Alasdair G Kergonb7cca192006-06-26 00:27:29 -0700368
369 /*
370 * Disk log?
371 */
372 if (!dev) {
373 lc->clean_bits = vmalloc(bitset_size);
374 if (!lc->clean_bits) {
375 DMWARN("couldn't allocate clean bitset");
376 kfree(lc);
377 return -ENOMEM;
378 }
379 lc->disk_header = NULL;
380 } else {
381 lc->log_dev = dev;
Jonathan E Brassow01d03a62007-05-09 02:32:57 -0700382 lc->log_dev_failed = 0;
Alasdair G Kergonb7cca192006-06-26 00:27:29 -0700383 lc->header_location.bdev = lc->log_dev->bdev;
384 lc->header_location.sector = 0;
385
386 /*
387 * Buffer holds both header and bitset.
388 */
389 buf_size = dm_round_up((LOG_OFFSET << SECTOR_SHIFT) +
390 bitset_size, ti->limits.hardsect_size);
391 lc->header_location.count = buf_size >> SECTOR_SHIFT;
Heinz Mauelshagen5d234d12007-05-09 02:33:03 -0700392 lc->io_req.mem.type = DM_IO_VMA;
393 lc->io_req.client = dm_io_client_create(dm_div_up(buf_size,
394 PAGE_SIZE));
395 if (IS_ERR(lc->io_req.client)) {
396 r = PTR_ERR(lc->io_req.client);
397 DMWARN("couldn't allocate disk io client");
398 kfree(lc);
399 return -ENOMEM;
400 }
Alasdair G Kergonb7cca192006-06-26 00:27:29 -0700401
402 lc->disk_header = vmalloc(buf_size);
403 if (!lc->disk_header) {
404 DMWARN("couldn't allocate disk log buffer");
405 kfree(lc);
406 return -ENOMEM;
407 }
408
409 lc->clean_bits = (void *)lc->disk_header +
410 (LOG_OFFSET << SECTOR_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
Alasdair G Kergonb7cca192006-06-26 00:27:29 -0700412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 memset(lc->clean_bits, -1, bitset_size);
414
415 lc->sync_bits = vmalloc(bitset_size);
416 if (!lc->sync_bits) {
417 DMWARN("couldn't allocate sync bitset");
Alasdair G Kergonb7cca192006-06-26 00:27:29 -0700418 if (!dev)
419 vfree(lc->clean_bits);
420 vfree(lc->disk_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 kfree(lc);
422 return -ENOMEM;
423 }
424 memset(lc->sync_bits, (sync == NOSYNC) ? -1 : 0, bitset_size);
425 lc->sync_count = (sync == NOSYNC) ? region_count : 0;
426
427 lc->recovering_bits = vmalloc(bitset_size);
428 if (!lc->recovering_bits) {
429 DMWARN("couldn't allocate sync bitset");
430 vfree(lc->sync_bits);
Alasdair G Kergonb7cca192006-06-26 00:27:29 -0700431 if (!dev)
432 vfree(lc->clean_bits);
433 vfree(lc->disk_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 kfree(lc);
435 return -ENOMEM;
436 }
437 memset(lc->recovering_bits, 0, bitset_size);
438 lc->sync_search = 0;
439 log->context = lc;
Alasdair G Kergonb7cca192006-06-26 00:27:29 -0700440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return 0;
442}
443
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100444static int core_ctr(struct dm_dirty_log *log, struct dm_target *ti,
Alasdair G Kergonb7cca192006-06-26 00:27:29 -0700445 unsigned int argc, char **argv)
446{
447 return create_log_context(log, ti, argc, argv, NULL);
448}
449
450static void destroy_log_context(struct log_c *lc)
451{
452 vfree(lc->sync_bits);
453 vfree(lc->recovering_bits);
454 kfree(lc);
455}
456
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100457static void core_dtr(struct dm_dirty_log *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
459 struct log_c *lc = (struct log_c *) log->context;
Alasdair G Kergonb7cca192006-06-26 00:27:29 -0700460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 vfree(lc->clean_bits);
Alasdair G Kergonb7cca192006-06-26 00:27:29 -0700462 destroy_log_context(lc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
464
465/*----------------------------------------------------------------
466 * disk log constructor/destructor
467 *
468 * argv contains log_device region_size followed optionally by [no]sync
469 *--------------------------------------------------------------*/
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100470static int disk_ctr(struct dm_dirty_log *log, struct dm_target *ti,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 unsigned int argc, char **argv)
472{
473 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 struct dm_dev *dev;
475
476 if (argc < 2 || argc > 3) {
Heinz Mauelshagenb7fd54a2008-04-24 21:43:06 +0100477 DMWARN("wrong number of arguments to disk dirty region log");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return -EINVAL;
479 }
480
481 r = dm_get_device(ti, argv[0], 0, 0 /* FIXME */,
482 FMODE_READ | FMODE_WRITE, &dev);
483 if (r)
484 return r;
485
Alasdair G Kergonb7cca192006-06-26 00:27:29 -0700486 r = create_log_context(log, ti, argc - 1, argv + 1, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if (r) {
488 dm_put_device(ti, dev);
489 return r;
490 }
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
494
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100495static void disk_dtr(struct dm_dirty_log *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
497 struct log_c *lc = (struct log_c *) log->context;
Alasdair G Kergonb7cca192006-06-26 00:27:29 -0700498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 dm_put_device(lc->ti, lc->log_dev);
500 vfree(lc->disk_header);
Heinz Mauelshagen5d234d12007-05-09 02:33:03 -0700501 dm_io_client_destroy(lc->io_req.client);
Alasdair G Kergonb7cca192006-06-26 00:27:29 -0700502 destroy_log_context(lc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
505static int count_bits32(uint32_t *addr, unsigned size)
506{
507 int count = 0, i;
508
509 for (i = 0; i < size; i++) {
510 count += hweight32(*(addr+i));
511 }
512 return count;
513}
514
Jonathan E Brassow01d03a62007-05-09 02:32:57 -0700515static void fail_log_device(struct log_c *lc)
516{
517 if (lc->log_dev_failed)
518 return;
519
520 lc->log_dev_failed = 1;
521 dm_table_event(lc->ti->table);
522}
523
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100524static int disk_resume(struct dm_dirty_log *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
526 int r;
527 unsigned i;
528 struct log_c *lc = (struct log_c *) log->context;
529 size_t size = lc->bitset_uint32_count * sizeof(uint32_t);
530
531 /* read the disk header */
532 r = read_header(lc);
Jonathan E Brassow01d03a62007-05-09 02:32:57 -0700533 if (r) {
Heinz Mauelshagenb7fd54a2008-04-24 21:43:06 +0100534 DMWARN("%s: Failed to read header on dirty region log device",
Jonathan E Brassow01d03a62007-05-09 02:32:57 -0700535 lc->log_dev->name);
536 fail_log_device(lc);
Jonathan Brassowba8b45c2007-05-09 02:33:08 -0700537 /*
538 * If the log device cannot be read, we must assume
539 * all regions are out-of-sync. If we simply return
540 * here, the state will be uninitialized and could
541 * lead us to return 'in-sync' status for regions
542 * that are actually 'out-of-sync'.
543 */
544 lc->header.nr_regions = 0;
Jonathan E Brassow01d03a62007-05-09 02:32:57 -0700545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Alasdair G Kergon8a835f12006-06-26 00:27:30 -0700547 /* set or clear any new bits -- device has grown */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if (lc->sync == NOSYNC)
549 for (i = lc->header.nr_regions; i < lc->region_count; i++)
550 /* FIXME: amazingly inefficient */
551 log_set_bit(lc, lc->clean_bits, i);
552 else
553 for (i = lc->header.nr_regions; i < lc->region_count; i++)
554 /* FIXME: amazingly inefficient */
555 log_clear_bit(lc, lc->clean_bits, i);
556
Alasdair G Kergon8a835f12006-06-26 00:27:30 -0700557 /* clear any old bits -- device has shrunk */
558 for (i = lc->region_count; i % (sizeof(*lc->clean_bits) << BYTE_SHIFT); i++)
559 log_clear_bit(lc, lc->clean_bits, i);
560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 /* copy clean across to sync */
562 memcpy(lc->sync_bits, lc->clean_bits, size);
563 lc->sync_count = count_bits32(lc->clean_bits, lc->bitset_uint32_count);
Jonathan E Brassow88b20a12006-12-08 02:41:12 -0800564 lc->sync_search = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 /* set the correct number of regions in the header */
567 lc->header.nr_regions = lc->region_count;
568
569 /* write the new header */
Jonathan E Brassow01d03a62007-05-09 02:32:57 -0700570 r = write_header(lc);
571 if (r) {
Heinz Mauelshagenb7fd54a2008-04-24 21:43:06 +0100572 DMWARN("%s: Failed to write header on dirty region log device",
Jonathan E Brassow01d03a62007-05-09 02:32:57 -0700573 lc->log_dev->name);
574 fail_log_device(lc);
575 }
576
577 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100580static uint32_t core_get_region_size(struct dm_dirty_log *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
582 struct log_c *lc = (struct log_c *) log->context;
583 return lc->region_size;
584}
585
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100586static int core_resume(struct dm_dirty_log *log)
Jonathan E Brassow88b20a12006-12-08 02:41:12 -0800587{
588 struct log_c *lc = (struct log_c *) log->context;
589 lc->sync_search = 0;
590 return 0;
591}
592
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100593static int core_is_clean(struct dm_dirty_log *log, region_t region)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
595 struct log_c *lc = (struct log_c *) log->context;
596 return log_test_bit(lc->clean_bits, region);
597}
598
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100599static int core_in_sync(struct dm_dirty_log *log, region_t region, int block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
601 struct log_c *lc = (struct log_c *) log->context;
602 return log_test_bit(lc->sync_bits, region);
603}
604
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100605static int core_flush(struct dm_dirty_log *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
607 /* no op */
608 return 0;
609}
610
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100611static int disk_flush(struct dm_dirty_log *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
613 int r;
614 struct log_c *lc = (struct log_c *) log->context;
615
616 /* only write if the log has changed */
617 if (!lc->touched)
618 return 0;
619
Kevin Corry702ca6f2006-06-26 00:27:28 -0700620 r = write_header(lc);
Jonathan E Brassow01d03a62007-05-09 02:32:57 -0700621 if (r)
622 fail_log_device(lc);
623 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 lc->touched = 0;
625
626 return r;
627}
628
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100629static void core_mark_region(struct dm_dirty_log *log, region_t region)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
631 struct log_c *lc = (struct log_c *) log->context;
632 log_clear_bit(lc, lc->clean_bits, region);
633}
634
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100635static void core_clear_region(struct dm_dirty_log *log, region_t region)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
637 struct log_c *lc = (struct log_c *) log->context;
638 log_set_bit(lc, lc->clean_bits, region);
639}
640
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100641static int core_get_resync_work(struct dm_dirty_log *log, region_t *region)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
643 struct log_c *lc = (struct log_c *) log->context;
644
645 if (lc->sync_search >= lc->region_count)
646 return 0;
647
648 do {
Stefan Bader1113a7e2006-02-02 14:28:07 -0800649 *region = ext2_find_next_zero_bit(
650 (unsigned long *) lc->sync_bits,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 lc->region_count,
652 lc->sync_search);
653 lc->sync_search = *region + 1;
654
Darrick J. Wongac81b2e2006-01-06 00:20:11 -0800655 if (*region >= lc->region_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 return 0;
657
658 } while (log_test_bit(lc->recovering_bits, *region));
659
660 log_set_bit(lc, lc->recovering_bits, *region);
661 return 1;
662}
663
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100664static void core_set_region_sync(struct dm_dirty_log *log, region_t region,
Jonathan E Brassowf3ee6b22006-12-08 02:41:11 -0800665 int in_sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
667 struct log_c *lc = (struct log_c *) log->context;
668
669 log_clear_bit(lc, lc->recovering_bits, region);
Jonathan E Brassowf3ee6b22006-12-08 02:41:11 -0800670 if (in_sync) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 log_set_bit(lc, lc->sync_bits, region);
672 lc->sync_count++;
Jonathan E Brassowf3ee6b22006-12-08 02:41:11 -0800673 } else if (log_test_bit(lc->sync_bits, region)) {
674 lc->sync_count--;
675 log_clear_bit(lc, lc->sync_bits, region);
676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100679static region_t core_get_sync_count(struct dm_dirty_log *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
681 struct log_c *lc = (struct log_c *) log->context;
682
683 return lc->sync_count;
684}
685
686#define DMEMIT_SYNC \
687 if (lc->sync != DEFAULTSYNC) \
688 DMEMIT("%ssync ", lc->sync == NOSYNC ? "no" : "")
689
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100690static int core_status(struct dm_dirty_log *log, status_type_t status,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 char *result, unsigned int maxlen)
692{
693 int sz = 0;
694 struct log_c *lc = log->context;
695
696 switch(status) {
697 case STATUSTYPE_INFO:
Jonathan E Brassow315dcc22007-05-09 02:32:58 -0700698 DMEMIT("1 %s", log->type->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 break;
700
701 case STATUSTYPE_TABLE:
702 DMEMIT("%s %u %u ", log->type->name,
703 lc->sync == DEFAULTSYNC ? 1 : 2, lc->region_size);
704 DMEMIT_SYNC;
705 }
706
707 return sz;
708}
709
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100710static int disk_status(struct dm_dirty_log *log, status_type_t status,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 char *result, unsigned int maxlen)
712{
713 int sz = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 struct log_c *lc = log->context;
715
716 switch(status) {
717 case STATUSTYPE_INFO:
Jonathan E Brassow315dcc22007-05-09 02:32:58 -0700718 DMEMIT("3 %s %s %c", log->type->name, lc->log_dev->name,
719 lc->log_dev_failed ? 'D' : 'A');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 break;
721
722 case STATUSTYPE_TABLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 DMEMIT("%s %u %s %u ", log->type->name,
Jonathan E Brassow315dcc22007-05-09 02:32:58 -0700724 lc->sync == DEFAULTSYNC ? 2 : 3, lc->log_dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 lc->region_size);
726 DMEMIT_SYNC;
727 }
728
729 return sz;
730}
731
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100732static struct dm_dirty_log_type _core_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 .name = "core",
734 .module = THIS_MODULE,
735 .ctr = core_ctr,
736 .dtr = core_dtr,
Jonathan E Brassow88b20a12006-12-08 02:41:12 -0800737 .resume = core_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 .get_region_size = core_get_region_size,
739 .is_clean = core_is_clean,
740 .in_sync = core_in_sync,
741 .flush = core_flush,
742 .mark_region = core_mark_region,
743 .clear_region = core_clear_region,
744 .get_resync_work = core_get_resync_work,
Jonathan E Brassowf3ee6b22006-12-08 02:41:11 -0800745 .set_region_sync = core_set_region_sync,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 .get_sync_count = core_get_sync_count,
747 .status = core_status,
748};
749
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100750static struct dm_dirty_log_type _disk_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 .name = "disk",
752 .module = THIS_MODULE,
753 .ctr = disk_ctr,
754 .dtr = disk_dtr,
Jonathan Brassow6b3df0d2007-10-19 22:47:57 +0100755 .postsuspend = disk_flush,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 .resume = disk_resume,
757 .get_region_size = core_get_region_size,
758 .is_clean = core_is_clean,
759 .in_sync = core_in_sync,
760 .flush = disk_flush,
761 .mark_region = core_mark_region,
762 .clear_region = core_clear_region,
763 .get_resync_work = core_get_resync_work,
Jonathan E Brassowf3ee6b22006-12-08 02:41:11 -0800764 .set_region_sync = core_set_region_sync,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 .get_sync_count = core_get_sync_count,
766 .status = disk_status,
767};
768
769int __init dm_dirty_log_init(void)
770{
771 int r;
772
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100773 r = dm_dirty_log_type_register(&_core_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 if (r)
775 DMWARN("couldn't register core log");
776
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100777 r = dm_dirty_log_type_register(&_disk_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 if (r) {
779 DMWARN("couldn't register disk type");
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100780 dm_dirty_log_type_unregister(&_core_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
782
783 return r;
784}
785
Heinz Mauelshagen769aef32008-04-24 21:43:09 +0100786void __exit dm_dirty_log_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Heinz Mauelshagen416cd172008-04-24 21:43:35 +0100788 dm_dirty_log_type_unregister(&_disk_type);
789 dm_dirty_log_type_unregister(&_core_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
791
Heinz Mauelshagen769aef32008-04-24 21:43:09 +0100792module_init(dm_dirty_log_init);
793module_exit(dm_dirty_log_exit);
794
795MODULE_DESCRIPTION(DM_NAME " dirty region log");
796MODULE_AUTHOR("Joe Thornber, Heinz Mauelshagen <dm-devel@redhat.com>");
797MODULE_LICENSE("GPL");