blob: 4dfe38655a49c495837b36e7984c28c60ef56100 [file] [log] [blame]
Josef Bacik0e9cebe2015-03-20 10:50:37 -04001/*
2 * Copyright (C) 2014 Facebook. All rights reserved.
3 *
4 * This file is released under the GPL.
5 */
6
7#include <linux/device-mapper.h>
8
9#include <linux/module.h>
10#include <linux/init.h>
11#include <linux/blkdev.h>
12#include <linux/bio.h>
13#include <linux/slab.h>
14#include <linux/kthread.h>
15#include <linux/freezer.h>
16
17#define DM_MSG_PREFIX "log-writes"
18
19/*
20 * This target will sequentially log all writes to the target device onto the
21 * log device. This is helpful for replaying writes to check for fs consistency
22 * at all times. This target provides a mechanism to mark specific events to
23 * check data at a later time. So for example you would:
24 *
25 * write data
26 * fsync
27 * dmsetup message /dev/whatever mark mymark
28 * unmount /mnt/test
29 *
30 * Then replay the log up to mymark and check the contents of the replay to
31 * verify it matches what was written.
32 *
33 * We log writes only after they have been flushed, this makes the log describe
34 * close to the order in which the data hits the actual disk, not its cache. So
35 * for example the following sequence (W means write, C means complete)
36 *
37 * Wa,Wb,Wc,Cc,Ca,FLUSH,FUAd,Cb,CFLUSH,CFUAd
38 *
39 * Would result in the log looking like this:
40 *
41 * c,a,flush,fuad,b,<other writes>,<next flush>
42 *
43 * This is meant to help expose problems where file systems do not properly wait
44 * on data being written before invoking a FLUSH. FUA bypasses cache so once it
45 * completes it is added to the log as it should be on disk.
46 *
47 * We treat DISCARDs as if they don't bypass cache so that they are logged in
48 * order of completion along with the normal writes. If we didn't do it this
49 * way we would process all the discards first and then write all the data, when
50 * in fact we want to do the data and the discard in the order that they
51 * completed.
52 */
53#define LOG_FLUSH_FLAG (1 << 0)
54#define LOG_FUA_FLAG (1 << 1)
55#define LOG_DISCARD_FLAG (1 << 2)
56#define LOG_MARK_FLAG (1 << 3)
57
Geert Uytterhoevenf4ad3172015-04-19 00:07:30 +020058#define WRITE_LOG_VERSION 1ULL
59#define WRITE_LOG_MAGIC 0x6a736677736872ULL
Josef Bacik0e9cebe2015-03-20 10:50:37 -040060
61/*
62 * The disk format for this is braindead simple.
63 *
64 * At byte 0 we have our super, followed by the following sequence for
65 * nr_entries:
66 *
67 * [ 1 sector ][ entry->nr_sectors ]
68 * [log_write_entry][ data written ]
69 *
70 * The log_write_entry takes up a full sector so we can have arbitrary length
71 * marks and it leaves us room for extra content in the future.
72 */
73
74/*
75 * Basic info about the log for userspace.
76 */
77struct log_write_super {
78 __le64 magic;
79 __le64 version;
80 __le64 nr_entries;
81 __le32 sectorsize;
82};
83
84/*
85 * sector - the sector we wrote.
86 * nr_sectors - the number of sectors we wrote.
87 * flags - flags for this log entry.
88 * data_len - the size of the data in this log entry, this is for private log
89 * entry stuff, the MARK data provided by userspace for example.
90 */
91struct log_write_entry {
92 __le64 sector;
93 __le64 nr_sectors;
94 __le64 flags;
95 __le64 data_len;
96};
97
98struct log_writes_c {
99 struct dm_dev *dev;
100 struct dm_dev *logdev;
101 u64 logged_entries;
102 u32 sectorsize;
103 atomic_t io_blocks;
104 atomic_t pending_blocks;
105 sector_t next_sector;
106 sector_t end_sector;
107 bool logging_enabled;
108 bool device_supports_discard;
109 spinlock_t blocks_lock;
110 struct list_head unflushed_blocks;
111 struct list_head logging_blocks;
112 wait_queue_head_t wait;
113 struct task_struct *log_kthread;
114};
115
116struct pending_block {
117 int vec_cnt;
118 u64 flags;
119 sector_t sector;
120 sector_t nr_sectors;
121 char *data;
122 u32 datalen;
123 struct list_head list;
124 struct bio_vec vecs[0];
125};
126
127struct per_bio_data {
128 struct pending_block *block;
129};
130
131static void put_pending_block(struct log_writes_c *lc)
132{
133 if (atomic_dec_and_test(&lc->pending_blocks)) {
134 smp_mb__after_atomic();
135 if (waitqueue_active(&lc->wait))
136 wake_up(&lc->wait);
137 }
138}
139
140static void put_io_block(struct log_writes_c *lc)
141{
142 if (atomic_dec_and_test(&lc->io_blocks)) {
143 smp_mb__after_atomic();
144 if (waitqueue_active(&lc->wait))
145 wake_up(&lc->wait);
146 }
147}
148
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200149static void log_end_io(struct bio *bio)
Josef Bacik0e9cebe2015-03-20 10:50:37 -0400150{
151 struct log_writes_c *lc = bio->bi_private;
Josef Bacik0e9cebe2015-03-20 10:50:37 -0400152
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200153 if (bio->bi_error) {
Josef Bacik0e9cebe2015-03-20 10:50:37 -0400154 unsigned long flags;
155
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200156 DMERR("Error writing log block, error=%d", bio->bi_error);
Josef Bacik0e9cebe2015-03-20 10:50:37 -0400157 spin_lock_irqsave(&lc->blocks_lock, flags);
158 lc->logging_enabled = false;
159 spin_unlock_irqrestore(&lc->blocks_lock, flags);
160 }
161
Guoqing Jiang491221f2016-09-22 03:10:01 -0400162 bio_free_pages(bio);
Josef Bacik0e9cebe2015-03-20 10:50:37 -0400163 put_io_block(lc);
164 bio_put(bio);
165}
166
167/*
168 * Meant to be called if there is an error, it will free all the pages
169 * associated with the block.
170 */
171static void free_pending_block(struct log_writes_c *lc,
172 struct pending_block *block)
173{
174 int i;
175
176 for (i = 0; i < block->vec_cnt; i++) {
177 if (block->vecs[i].bv_page)
178 __free_page(block->vecs[i].bv_page);
179 }
180 kfree(block->data);
181 kfree(block);
182 put_pending_block(lc);
183}
184
185static int write_metadata(struct log_writes_c *lc, void *entry,
186 size_t entrylen, void *data, size_t datalen,
187 sector_t sector)
188{
189 struct bio *bio;
190 struct page *page;
191 void *ptr;
192 size_t ret;
193
194 bio = bio_alloc(GFP_KERNEL, 1);
195 if (!bio) {
196 DMERR("Couldn't alloc log bio");
197 goto error;
198 }
199 bio->bi_iter.bi_size = 0;
200 bio->bi_iter.bi_sector = sector;
201 bio->bi_bdev = lc->logdev->bdev;
202 bio->bi_end_io = log_end_io;
203 bio->bi_private = lc;
Mike Christiee6047142016-06-05 14:32:04 -0500204 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Josef Bacik0e9cebe2015-03-20 10:50:37 -0400205
206 page = alloc_page(GFP_KERNEL);
207 if (!page) {
208 DMERR("Couldn't alloc log page");
209 bio_put(bio);
210 goto error;
211 }
212
213 ptr = kmap_atomic(page);
214 memcpy(ptr, entry, entrylen);
215 if (datalen)
216 memcpy(ptr + entrylen, data, datalen);
217 memset(ptr + entrylen + datalen, 0,
218 lc->sectorsize - entrylen - datalen);
219 kunmap_atomic(ptr);
220
221 ret = bio_add_page(bio, page, lc->sectorsize, 0);
222 if (ret != lc->sectorsize) {
223 DMERR("Couldn't add page to the log block");
224 goto error_bio;
225 }
Mike Christie4e49ea42016-06-05 14:31:41 -0500226 submit_bio(bio);
Josef Bacik0e9cebe2015-03-20 10:50:37 -0400227 return 0;
228error_bio:
229 bio_put(bio);
230 __free_page(page);
231error:
232 put_io_block(lc);
233 return -1;
234}
235
236static int log_one_block(struct log_writes_c *lc,
237 struct pending_block *block, sector_t sector)
238{
239 struct bio *bio;
240 struct log_write_entry entry;
241 size_t ret;
242 int i;
243
244 entry.sector = cpu_to_le64(block->sector);
245 entry.nr_sectors = cpu_to_le64(block->nr_sectors);
246 entry.flags = cpu_to_le64(block->flags);
247 entry.data_len = cpu_to_le64(block->datalen);
248 if (write_metadata(lc, &entry, sizeof(entry), block->data,
249 block->datalen, sector)) {
250 free_pending_block(lc, block);
251 return -1;
252 }
253
254 if (!block->vec_cnt)
255 goto out;
256 sector++;
257
Mikulas Patockaa5d60782016-08-30 16:11:53 -0400258 atomic_inc(&lc->io_blocks);
Mikulas Patocka7efb3672016-08-30 16:20:55 -0400259 bio = bio_alloc(GFP_KERNEL, min(block->vec_cnt, BIO_MAX_PAGES));
Josef Bacik0e9cebe2015-03-20 10:50:37 -0400260 if (!bio) {
261 DMERR("Couldn't alloc log bio");
262 goto error;
263 }
Josef Bacik0e9cebe2015-03-20 10:50:37 -0400264 bio->bi_iter.bi_size = 0;
265 bio->bi_iter.bi_sector = sector;
266 bio->bi_bdev = lc->logdev->bdev;
267 bio->bi_end_io = log_end_io;
268 bio->bi_private = lc;
Mike Christiee6047142016-06-05 14:32:04 -0500269 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Josef Bacik0e9cebe2015-03-20 10:50:37 -0400270
271 for (i = 0; i < block->vec_cnt; i++) {
272 /*
273 * The page offset is always 0 because we allocate a new page
274 * for every bvec in the original bio for simplicity sake.
275 */
276 ret = bio_add_page(bio, block->vecs[i].bv_page,
277 block->vecs[i].bv_len, 0);
278 if (ret != block->vecs[i].bv_len) {
279 atomic_inc(&lc->io_blocks);
Mike Christie4e49ea42016-06-05 14:31:41 -0500280 submit_bio(bio);
Mikulas Patocka7efb3672016-08-30 16:20:55 -0400281 bio = bio_alloc(GFP_KERNEL, min(block->vec_cnt - i, BIO_MAX_PAGES));
Josef Bacik0e9cebe2015-03-20 10:50:37 -0400282 if (!bio) {
283 DMERR("Couldn't alloc log bio");
284 goto error;
285 }
286 bio->bi_iter.bi_size = 0;
287 bio->bi_iter.bi_sector = sector;
288 bio->bi_bdev = lc->logdev->bdev;
289 bio->bi_end_io = log_end_io;
290 bio->bi_private = lc;
Mike Christiee6047142016-06-05 14:32:04 -0500291 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Josef Bacik0e9cebe2015-03-20 10:50:37 -0400292
293 ret = bio_add_page(bio, block->vecs[i].bv_page,
294 block->vecs[i].bv_len, 0);
295 if (ret != block->vecs[i].bv_len) {
296 DMERR("Couldn't add page on new bio?");
297 bio_put(bio);
298 goto error;
299 }
300 }
301 sector += block->vecs[i].bv_len >> SECTOR_SHIFT;
302 }
Mike Christie4e49ea42016-06-05 14:31:41 -0500303 submit_bio(bio);
Josef Bacik0e9cebe2015-03-20 10:50:37 -0400304out:
305 kfree(block->data);
306 kfree(block);
307 put_pending_block(lc);
308 return 0;
309error:
310 free_pending_block(lc, block);
311 put_io_block(lc);
312 return -1;
313}
314
315static int log_super(struct log_writes_c *lc)
316{
317 struct log_write_super super;
318
319 super.magic = cpu_to_le64(WRITE_LOG_MAGIC);
320 super.version = cpu_to_le64(WRITE_LOG_VERSION);
321 super.nr_entries = cpu_to_le64(lc->logged_entries);
322 super.sectorsize = cpu_to_le32(lc->sectorsize);
323
324 if (write_metadata(lc, &super, sizeof(super), NULL, 0, 0)) {
325 DMERR("Couldn't write super");
326 return -1;
327 }
328
329 return 0;
330}
331
332static inline sector_t logdev_last_sector(struct log_writes_c *lc)
333{
334 return i_size_read(lc->logdev->bdev->bd_inode) >> SECTOR_SHIFT;
335}
336
337static int log_writes_kthread(void *arg)
338{
339 struct log_writes_c *lc = (struct log_writes_c *)arg;
340 sector_t sector = 0;
341
342 while (!kthread_should_stop()) {
343 bool super = false;
344 bool logging_enabled;
345 struct pending_block *block = NULL;
346 int ret;
347
348 spin_lock_irq(&lc->blocks_lock);
349 if (!list_empty(&lc->logging_blocks)) {
350 block = list_first_entry(&lc->logging_blocks,
351 struct pending_block, list);
352 list_del_init(&block->list);
353 if (!lc->logging_enabled)
354 goto next;
355
356 sector = lc->next_sector;
357 if (block->flags & LOG_DISCARD_FLAG)
358 lc->next_sector++;
359 else
360 lc->next_sector += block->nr_sectors + 1;
361
362 /*
363 * Apparently the size of the device may not be known
364 * right away, so handle this properly.
365 */
366 if (!lc->end_sector)
367 lc->end_sector = logdev_last_sector(lc);
368 if (lc->end_sector &&
369 lc->next_sector >= lc->end_sector) {
370 DMERR("Ran out of space on the logdev");
371 lc->logging_enabled = false;
372 goto next;
373 }
374 lc->logged_entries++;
375 atomic_inc(&lc->io_blocks);
376
377 super = (block->flags & (LOG_FUA_FLAG | LOG_MARK_FLAG));
378 if (super)
379 atomic_inc(&lc->io_blocks);
380 }
381next:
382 logging_enabled = lc->logging_enabled;
383 spin_unlock_irq(&lc->blocks_lock);
384 if (block) {
385 if (logging_enabled) {
386 ret = log_one_block(lc, block, sector);
387 if (!ret && super)
388 ret = log_super(lc);
389 if (ret) {
390 spin_lock_irq(&lc->blocks_lock);
391 lc->logging_enabled = false;
392 spin_unlock_irq(&lc->blocks_lock);
393 }
394 } else
395 free_pending_block(lc, block);
396 continue;
397 }
398
399 if (!try_to_freeze()) {
400 set_current_state(TASK_INTERRUPTIBLE);
401 if (!kthread_should_stop() &&
402 !atomic_read(&lc->pending_blocks))
403 schedule();
404 __set_current_state(TASK_RUNNING);
405 }
406 }
407 return 0;
408}
409
410/*
411 * Construct a log-writes mapping:
412 * log-writes <dev_path> <log_dev_path>
413 */
414static int log_writes_ctr(struct dm_target *ti, unsigned int argc, char **argv)
415{
416 struct log_writes_c *lc;
417 struct dm_arg_set as;
418 const char *devname, *logdevname;
Vivek Goyale80d1c82015-07-31 09:20:36 -0400419 int ret;
Josef Bacik0e9cebe2015-03-20 10:50:37 -0400420
421 as.argc = argc;
422 as.argv = argv;
423
424 if (argc < 2) {
425 ti->error = "Invalid argument count";
426 return -EINVAL;
427 }
428
429 lc = kzalloc(sizeof(struct log_writes_c), GFP_KERNEL);
430 if (!lc) {
431 ti->error = "Cannot allocate context";
432 return -ENOMEM;
433 }
434 spin_lock_init(&lc->blocks_lock);
435 INIT_LIST_HEAD(&lc->unflushed_blocks);
436 INIT_LIST_HEAD(&lc->logging_blocks);
437 init_waitqueue_head(&lc->wait);
438 lc->sectorsize = 1 << SECTOR_SHIFT;
439 atomic_set(&lc->io_blocks, 0);
440 atomic_set(&lc->pending_blocks, 0);
441
442 devname = dm_shift_arg(&as);
Vivek Goyale80d1c82015-07-31 09:20:36 -0400443 ret = dm_get_device(ti, devname, dm_table_get_mode(ti->table), &lc->dev);
444 if (ret) {
Josef Bacik0e9cebe2015-03-20 10:50:37 -0400445 ti->error = "Device lookup failed";
446 goto bad;
447 }
448
449 logdevname = dm_shift_arg(&as);
Vivek Goyale80d1c82015-07-31 09:20:36 -0400450 ret = dm_get_device(ti, logdevname, dm_table_get_mode(ti->table),
451 &lc->logdev);
452 if (ret) {
Josef Bacik0e9cebe2015-03-20 10:50:37 -0400453 ti->error = "Log device lookup failed";
454 dm_put_device(ti, lc->dev);
455 goto bad;
456 }
457
458 lc->log_kthread = kthread_run(log_writes_kthread, lc, "log-write");
Vladimir Zapolskiy91e630d2016-03-10 01:22:19 +0200459 if (IS_ERR(lc->log_kthread)) {
460 ret = PTR_ERR(lc->log_kthread);
Josef Bacik0e9cebe2015-03-20 10:50:37 -0400461 ti->error = "Couldn't alloc kthread";
462 dm_put_device(ti, lc->dev);
463 dm_put_device(ti, lc->logdev);
464 goto bad;
465 }
466
467 /* We put the super at sector 0, start logging at sector 1 */
468 lc->next_sector = 1;
469 lc->logging_enabled = true;
470 lc->end_sector = logdev_last_sector(lc);
471 lc->device_supports_discard = true;
472
473 ti->num_flush_bios = 1;
474 ti->flush_supported = true;
475 ti->num_discard_bios = 1;
476 ti->discards_supported = true;
Mike Snitzer30187e12016-01-31 13:28:26 -0500477 ti->per_io_data_size = sizeof(struct per_bio_data);
Josef Bacik0e9cebe2015-03-20 10:50:37 -0400478 ti->private = lc;
479 return 0;
480
481bad:
482 kfree(lc);
Vivek Goyale80d1c82015-07-31 09:20:36 -0400483 return ret;
Josef Bacik0e9cebe2015-03-20 10:50:37 -0400484}
485
486static int log_mark(struct log_writes_c *lc, char *data)
487{
488 struct pending_block *block;
489 size_t maxsize = lc->sectorsize - sizeof(struct log_write_entry);
490
491 block = kzalloc(sizeof(struct pending_block), GFP_KERNEL);
492 if (!block) {
493 DMERR("Error allocating pending block");
494 return -ENOMEM;
495 }
496
497 block->data = kstrndup(data, maxsize, GFP_KERNEL);
498 if (!block->data) {
499 DMERR("Error copying mark data");
500 kfree(block);
501 return -ENOMEM;
502 }
503 atomic_inc(&lc->pending_blocks);
504 block->datalen = strlen(block->data);
505 block->flags |= LOG_MARK_FLAG;
506 spin_lock_irq(&lc->blocks_lock);
507 list_add_tail(&block->list, &lc->logging_blocks);
508 spin_unlock_irq(&lc->blocks_lock);
509 wake_up_process(lc->log_kthread);
510 return 0;
511}
512
513static void log_writes_dtr(struct dm_target *ti)
514{
515 struct log_writes_c *lc = ti->private;
516
517 spin_lock_irq(&lc->blocks_lock);
518 list_splice_init(&lc->unflushed_blocks, &lc->logging_blocks);
519 spin_unlock_irq(&lc->blocks_lock);
520
521 /*
522 * This is just nice to have since it'll update the super to include the
523 * unflushed blocks, if it fails we don't really care.
524 */
525 log_mark(lc, "dm-log-writes-end");
526 wake_up_process(lc->log_kthread);
527 wait_event(lc->wait, !atomic_read(&lc->io_blocks) &&
528 !atomic_read(&lc->pending_blocks));
529 kthread_stop(lc->log_kthread);
530
531 WARN_ON(!list_empty(&lc->logging_blocks));
532 WARN_ON(!list_empty(&lc->unflushed_blocks));
533 dm_put_device(ti, lc->dev);
534 dm_put_device(ti, lc->logdev);
535 kfree(lc);
536}
537
538static void normal_map_bio(struct dm_target *ti, struct bio *bio)
539{
540 struct log_writes_c *lc = ti->private;
541
542 bio->bi_bdev = lc->dev->bdev;
543}
544
545static int log_writes_map(struct dm_target *ti, struct bio *bio)
546{
547 struct log_writes_c *lc = ti->private;
548 struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data));
549 struct pending_block *block;
550 struct bvec_iter iter;
551 struct bio_vec bv;
552 size_t alloc_size;
553 int i = 0;
Jens Axboe1eff9d32016-08-05 15:35:16 -0600554 bool flush_bio = (bio->bi_opf & REQ_PREFLUSH);
555 bool fua_bio = (bio->bi_opf & REQ_FUA);
Mike Christiee6047142016-06-05 14:32:04 -0500556 bool discard_bio = (bio_op(bio) == REQ_OP_DISCARD);
Josef Bacik0e9cebe2015-03-20 10:50:37 -0400557
558 pb->block = NULL;
559
560 /* Don't bother doing anything if logging has been disabled */
561 if (!lc->logging_enabled)
562 goto map_bio;
563
564 /*
565 * Map reads as normal.
566 */
567 if (bio_data_dir(bio) == READ)
568 goto map_bio;
569
570 /* No sectors and not a flush? Don't care */
571 if (!bio_sectors(bio) && !flush_bio)
572 goto map_bio;
573
574 /*
575 * Discards will have bi_size set but there's no actual data, so just
576 * allocate the size of the pending block.
577 */
578 if (discard_bio)
579 alloc_size = sizeof(struct pending_block);
580 else
581 alloc_size = sizeof(struct pending_block) + sizeof(struct bio_vec) * bio_segments(bio);
582
583 block = kzalloc(alloc_size, GFP_NOIO);
584 if (!block) {
585 DMERR("Error allocating pending block");
586 spin_lock_irq(&lc->blocks_lock);
587 lc->logging_enabled = false;
588 spin_unlock_irq(&lc->blocks_lock);
589 return -ENOMEM;
590 }
591 INIT_LIST_HEAD(&block->list);
592 pb->block = block;
593 atomic_inc(&lc->pending_blocks);
594
595 if (flush_bio)
596 block->flags |= LOG_FLUSH_FLAG;
597 if (fua_bio)
598 block->flags |= LOG_FUA_FLAG;
599 if (discard_bio)
600 block->flags |= LOG_DISCARD_FLAG;
601
602 block->sector = bio->bi_iter.bi_sector;
603 block->nr_sectors = bio_sectors(bio);
604
605 /* We don't need the data, just submit */
606 if (discard_bio) {
607 WARN_ON(flush_bio || fua_bio);
608 if (lc->device_supports_discard)
609 goto map_bio;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200610 bio_endio(bio);
Josef Bacik0e9cebe2015-03-20 10:50:37 -0400611 return DM_MAPIO_SUBMITTED;
612 }
613
614 /* Flush bio, splice the unflushed blocks onto this list and submit */
615 if (flush_bio && !bio_sectors(bio)) {
616 spin_lock_irq(&lc->blocks_lock);
617 list_splice_init(&lc->unflushed_blocks, &block->list);
618 spin_unlock_irq(&lc->blocks_lock);
619 goto map_bio;
620 }
621
622 /*
623 * We will write this bio somewhere else way later so we need to copy
624 * the actual contents into new pages so we know the data will always be
625 * there.
626 *
627 * We do this because this could be a bio from O_DIRECT in which case we
628 * can't just hold onto the page until some later point, we have to
629 * manually copy the contents.
630 */
631 bio_for_each_segment(bv, bio, iter) {
632 struct page *page;
633 void *src, *dst;
634
635 page = alloc_page(GFP_NOIO);
636 if (!page) {
637 DMERR("Error allocing page");
638 free_pending_block(lc, block);
639 spin_lock_irq(&lc->blocks_lock);
640 lc->logging_enabled = false;
641 spin_unlock_irq(&lc->blocks_lock);
642 return -ENOMEM;
643 }
644
645 src = kmap_atomic(bv.bv_page);
646 dst = kmap_atomic(page);
647 memcpy(dst, src + bv.bv_offset, bv.bv_len);
648 kunmap_atomic(dst);
649 kunmap_atomic(src);
650 block->vecs[i].bv_page = page;
651 block->vecs[i].bv_len = bv.bv_len;
652 block->vec_cnt++;
653 i++;
654 }
655
656 /* Had a flush with data in it, weird */
657 if (flush_bio) {
658 spin_lock_irq(&lc->blocks_lock);
659 list_splice_init(&lc->unflushed_blocks, &block->list);
660 spin_unlock_irq(&lc->blocks_lock);
661 }
662map_bio:
663 normal_map_bio(ti, bio);
664 return DM_MAPIO_REMAPPED;
665}
666
667static int normal_end_io(struct dm_target *ti, struct bio *bio, int error)
668{
669 struct log_writes_c *lc = ti->private;
670 struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data));
671
672 if (bio_data_dir(bio) == WRITE && pb->block) {
673 struct pending_block *block = pb->block;
674 unsigned long flags;
675
676 spin_lock_irqsave(&lc->blocks_lock, flags);
677 if (block->flags & LOG_FLUSH_FLAG) {
678 list_splice_tail_init(&block->list, &lc->logging_blocks);
679 list_add_tail(&block->list, &lc->logging_blocks);
680 wake_up_process(lc->log_kthread);
681 } else if (block->flags & LOG_FUA_FLAG) {
682 list_add_tail(&block->list, &lc->logging_blocks);
683 wake_up_process(lc->log_kthread);
684 } else
685 list_add_tail(&block->list, &lc->unflushed_blocks);
686 spin_unlock_irqrestore(&lc->blocks_lock, flags);
687 }
688
689 return error;
690}
691
692/*
693 * INFO format: <logged entries> <highest allocated sector>
694 */
695static void log_writes_status(struct dm_target *ti, status_type_t type,
696 unsigned status_flags, char *result,
697 unsigned maxlen)
698{
699 unsigned sz = 0;
700 struct log_writes_c *lc = ti->private;
701
702 switch (type) {
703 case STATUSTYPE_INFO:
704 DMEMIT("%llu %llu", lc->logged_entries,
705 (unsigned long long)lc->next_sector - 1);
706 if (!lc->logging_enabled)
707 DMEMIT(" logging_disabled");
708 break;
709
710 case STATUSTYPE_TABLE:
711 DMEMIT("%s %s", lc->dev->name, lc->logdev->name);
712 break;
713 }
714}
715
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200716static int log_writes_prepare_ioctl(struct dm_target *ti,
717 struct block_device **bdev, fmode_t *mode)
Josef Bacik0e9cebe2015-03-20 10:50:37 -0400718{
719 struct log_writes_c *lc = ti->private;
720 struct dm_dev *dev = lc->dev;
Josef Bacik0e9cebe2015-03-20 10:50:37 -0400721
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200722 *bdev = dev->bdev;
Josef Bacik0e9cebe2015-03-20 10:50:37 -0400723 /*
724 * Only pass ioctls through if the device sizes match exactly.
725 */
726 if (ti->len != i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT)
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200727 return 1;
728 return 0;
Josef Bacik0e9cebe2015-03-20 10:50:37 -0400729}
730
Josef Bacik0e9cebe2015-03-20 10:50:37 -0400731static int log_writes_iterate_devices(struct dm_target *ti,
732 iterate_devices_callout_fn fn,
733 void *data)
734{
735 struct log_writes_c *lc = ti->private;
736
737 return fn(ti, lc->dev, 0, ti->len, data);
738}
739
740/*
741 * Messages supported:
742 * mark <mark data> - specify the marked data.
743 */
744static int log_writes_message(struct dm_target *ti, unsigned argc, char **argv)
745{
746 int r = -EINVAL;
747 struct log_writes_c *lc = ti->private;
748
749 if (argc != 2) {
750 DMWARN("Invalid log-writes message arguments, expect 2 arguments, got %d", argc);
751 return r;
752 }
753
754 if (!strcasecmp(argv[0], "mark"))
755 r = log_mark(lc, argv[1]);
756 else
757 DMWARN("Unrecognised log writes target message received: %s", argv[0]);
758
759 return r;
760}
761
762static void log_writes_io_hints(struct dm_target *ti, struct queue_limits *limits)
763{
764 struct log_writes_c *lc = ti->private;
765 struct request_queue *q = bdev_get_queue(lc->dev->bdev);
766
767 if (!q || !blk_queue_discard(q)) {
768 lc->device_supports_discard = false;
769 limits->discard_granularity = 1 << SECTOR_SHIFT;
770 limits->max_discard_sectors = (UINT_MAX >> SECTOR_SHIFT);
771 }
772}
773
774static struct target_type log_writes_target = {
775 .name = "log-writes",
776 .version = {1, 0, 0},
777 .module = THIS_MODULE,
778 .ctr = log_writes_ctr,
779 .dtr = log_writes_dtr,
780 .map = log_writes_map,
781 .end_io = normal_end_io,
782 .status = log_writes_status,
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200783 .prepare_ioctl = log_writes_prepare_ioctl,
Josef Bacik0e9cebe2015-03-20 10:50:37 -0400784 .message = log_writes_message,
785 .iterate_devices = log_writes_iterate_devices,
786 .io_hints = log_writes_io_hints,
787};
788
789static int __init dm_log_writes_init(void)
790{
791 int r = dm_register_target(&log_writes_target);
792
793 if (r < 0)
794 DMERR("register failed %d", r);
795
796 return r;
797}
798
799static void __exit dm_log_writes_exit(void)
800{
801 dm_unregister_target(&log_writes_target);
802}
803
804module_init(dm_log_writes_init);
805module_exit(dm_log_writes_exit);
806
807MODULE_DESCRIPTION(DM_NAME " log writes target");
808MODULE_AUTHOR("Josef Bacik <jbacik@fb.com>");
809MODULE_LICENSE("GPL");