Lines Matching +full:suspend +full:- +full:to +full:- +full:disk

1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
9 #include "dm-core.h"
10 #include "dm-rq.h"
11 #include "dm-uevent.h"
12 #include "dm-ima.h"
14 #include <linux/bio-integrity.h>
33 #include <linux/blk-crypto.h>
34 #include <linux/blk-crypto-profile.h>
47 * dm_io into one list, and reuse bio->bi_private as the list head. Before
48 * ending this fs bio, we will recover its ->bi_private.
81 * One of these is allocated (on-stack) per original bio.
101 return (char *)bio - DM_TARGET_IO_BIO_OFFSET - data_size; in dm_per_bio_data()
102 return (char *)bio - DM_IO_BIO_OFFSET - data_size; in dm_per_bio_data()
110 if (io->magic == DM_IO_MAGIC) in dm_bio_from_per_bio_data()
112 BUG_ON(io->magic != DM_TIO_MAGIC); in dm_bio_from_per_bio_data()
119 return container_of(bio, struct dm_target_io, clone)->target_bio_nr; in dm_bio_get_target_bio_nr()
123 #define MINOR_ALLOCED ((void *)-1)
146 * Bio-based DM's mempools' reserved IOs set by the user.
200 DM_NUMA_NODE, num_online_nodes() - 1); in dm_get_numa_node()
213 r = -ENOMEM; in local_init()
287 while (i--) in dm_init()
297 while (i--) in dm_exit()
311 return test_bit(DMF_DELETING, &md->flags); in dm_deleting_md()
314 static int dm_blk_open(struct gendisk *disk, blk_mode_t mode) in dm_blk_open() argument
320 md = disk->private_data; in dm_blk_open()
324 if (test_bit(DMF_FREEING, &md->flags) || in dm_blk_open()
331 atomic_inc(&md->open_count); in dm_blk_open()
335 return md ? 0 : -ENXIO; in dm_blk_open()
338 static void dm_blk_close(struct gendisk *disk) in dm_blk_close() argument
344 md = disk->private_data; in dm_blk_close()
348 if (atomic_dec_and_test(&md->open_count) && in dm_blk_close()
349 (test_bit(DMF_DEFERRED_REMOVE, &md->flags))) in dm_blk_close()
359 return atomic_read(&md->open_count); in dm_open_count()
372 r = -EBUSY; in dm_lock_for_deletion()
374 set_bit(DMF_DEFERRED_REMOVE, &md->flags); in dm_lock_for_deletion()
375 } else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags)) in dm_lock_for_deletion()
376 r = -EEXIST; in dm_lock_for_deletion()
378 set_bit(DMF_DELETING, &md->flags); in dm_lock_for_deletion()
391 if (test_bit(DMF_DELETING, &md->flags)) in dm_cancel_deferred_remove()
392 r = -EBUSY; in dm_cancel_deferred_remove()
394 clear_bit(DMF_DEFERRED_REMOVE, &md->flags); in dm_cancel_deferred_remove()
408 struct mapped_device *md = bdev->bd_disk->private_data; in dm_blk_getgeo()
421 r = -ENOTTY; in dm_prepare_ioctl()
427 if (map->num_targets != 1) in dm_prepare_ioctl()
431 if (!ti->type->prepare_ioctl) in dm_prepare_ioctl()
435 return -EAGAIN; in dm_prepare_ioctl()
437 r = ti->type->prepare_ioctl(ti, bdev); in dm_prepare_ioctl()
438 if (r == -ENOTCONN && !fatal_signal_pending(current)) { in dm_prepare_ioctl()
455 struct mapped_device *md = bdev->bd_disk->private_data; in dm_blk_ioctl()
469 "%s: sending ioctl %x to DM device without required privilege.", in dm_blk_ioctl()
470 current->comm, cmd); in dm_blk_ioctl()
471 r = -ENOIOCTLCMD; in dm_blk_ioctl()
476 if (!bdev->bd_disk->fops->ioctl) in dm_blk_ioctl()
477 r = -ENOTTY; in dm_blk_ioctl()
479 r = bdev->bd_disk->fops->ioctl(bdev, mode, cmd, arg); in dm_blk_ioctl()
487 return jiffies_to_nsecs(clone_to_tio(bio)->io->start_time); in dm_start_time_ns_from_clone()
493 return ((bio->bi_opf & REQ_PREFLUSH) && bio->bi_iter.bi_size); in bio_is_flush_with_data()
505 return io->sectors; in dm_io_sectors()
511 struct bio *bio = io->orig_bio; in dm_io_acct()
515 bdev_start_io_acct(bio->bi_bdev, bio_op(bio), in dm_io_acct()
516 io->start_time); in dm_io_acct()
518 bdev_end_io_acct(bio->bi_bdev, bio_op(bio), in dm_io_acct()
520 io->start_time); in dm_io_acct()
524 unlikely(dm_stats_used(&io->md->stats))) { in dm_io_acct()
528 sector = bio_end_sector(bio) - io->sector_offset; in dm_io_acct()
530 sector = bio->bi_iter.bi_sector; in dm_io_acct()
532 dm_stats_account_io(&io->md->stats, bio_data_dir(bio), in dm_io_acct()
534 end, io->start_time, &io->stats_aux); in dm_io_acct()
557 spin_lock_irqsave(&io->lock, flags); in dm_start_io_acct()
559 spin_unlock_irqrestore(&io->lock, flags); in dm_start_io_acct()
563 spin_unlock_irqrestore(&io->lock, flags); in dm_start_io_acct()
580 clone = bio_alloc_clone(NULL, bio, gfp_mask, &md->mempools->io_bs); in alloc_io()
584 tio->flags = 0; in alloc_io()
586 tio->io = NULL; in alloc_io()
589 io->magic = DM_IO_MAGIC; in alloc_io()
590 io->status = BLK_STS_OK; in alloc_io()
593 atomic_set(&io->io_count, 2); in alloc_io()
594 this_cpu_inc(*md->pending_io); in alloc_io()
595 io->orig_bio = bio; in alloc_io()
596 io->md = md; in alloc_io()
597 spin_lock_init(&io->lock); in alloc_io()
598 io->start_time = jiffies; in alloc_io()
599 io->flags = 0; in alloc_io()
600 if (blk_queue_io_stat(md->queue)) in alloc_io()
604 unlikely(dm_stats_used(&md->stats))) in alloc_io()
605 dm_stats_record_start(&md->stats, &io->stats_aux); in alloc_io()
612 bio_put(&io->tio.clone); in free_io()
618 struct mapped_device *md = ci->io->md; in alloc_tio()
622 if (!ci->io->tio.io) { in alloc_tio()
623 /* the dm_target_io embedded in ci->io is available */ in alloc_tio()
624 tio = &ci->io->tio; in alloc_tio()
626 clone = &tio->clone; in alloc_tio()
628 clone = bio_alloc_clone(NULL, ci->bio, gfp_mask, in alloc_tio()
629 &md->mempools->bs); in alloc_tio()
634 clone->bi_opf &= ~REQ_DM_POLL_LIST; in alloc_tio()
637 tio->flags = 0; /* also clears DM_TIO_INSIDE_DM_IO */ in alloc_tio()
640 tio->magic = DM_TIO_MAGIC; in alloc_tio()
641 tio->io = ci->io; in alloc_tio()
642 tio->ti = ti; in alloc_tio()
643 tio->target_bio_nr = target_bio_nr; in alloc_tio()
644 tio->len_ptr = len; in alloc_tio()
645 tio->old_sector = 0; in alloc_tio()
648 clone->bi_bdev = md->disk->part0; in alloc_tio()
649 if (likely(ti != NULL) && unlikely(ti->needs_bio_set_dev)) in alloc_tio()
650 bio_set_dev(clone, md->disk->part0); in alloc_tio()
653 clone->bi_iter.bi_size = to_bytes(*len); in alloc_tio()
669 * Add the bio to the list of deferred io.
675 spin_lock_irqsave(&md->deferred_lock, flags); in queue_io()
676 bio_list_add(&md->deferred, bio); in queue_io()
677 spin_unlock_irqrestore(&md->deferred_lock, flags); in queue_io()
678 queue_work(md->wq, &md->work); in queue_io()
683 * function to access the md->map field, and make sure they call
687 int *srcu_idx) __acquires(md->io_barrier) in dm_get_live_table()
689 *srcu_idx = srcu_read_lock(&md->io_barrier); in dm_get_live_table()
691 return srcu_dereference(md->map, &md->io_barrier); in dm_get_live_table()
695 int srcu_idx) __releases(md->io_barrier) in dm_put_live_table()
697 srcu_read_unlock(&md->io_barrier, srcu_idx); in dm_put_live_table()
702 synchronize_srcu(&md->io_barrier); in dm_sync_table()
707 * A fast alternative to dm_get_live_table/dm_put_live_table.
713 return rcu_dereference(md->map); in dm_get_live_table_fast()
721 static char *_dm_claim_ptr = "I belong to device-mapper";
735 td = kmalloc_node(sizeof(*td), GFP_KERNEL, md->numa_node_id); in open_table_device()
737 return ERR_PTR(-ENOMEM); in open_table_device()
738 refcount_set(&td->count, 1); in open_table_device()
749 * We can be called before the dm disk is added. In that case we can't in open_table_device()
753 if (md->disk->slave_dir) { in open_table_device()
754 r = bd_link_disk_holder(bdev, md->disk); in open_table_device()
759 td->dm_dev.mode = mode; in open_table_device()
760 td->dm_dev.bdev = bdev; in open_table_device()
761 td->dm_dev.bdev_file = bdev_file; in open_table_device()
762 td->dm_dev.dax_dev = fs_dax_get_by_bdev(bdev, &part_off, in open_table_device()
764 format_dev_t(td->dm_dev.name, dev); in open_table_device()
765 list_add(&td->list, &md->table_devices); in open_table_device()
780 if (md->disk->slave_dir) in close_table_device()
781 bd_unlink_disk_holder(td->dm_dev.bdev, md->disk); in close_table_device()
784 if (unlikely(test_bit(DMF_DEFERRED_REMOVE, &md->flags))) in close_table_device()
785 fput(td->dm_dev.bdev_file); in close_table_device()
787 __fput_sync(td->dm_dev.bdev_file); in close_table_device()
789 put_dax(td->dm_dev.dax_dev); in close_table_device()
790 list_del(&td->list); in close_table_device()
800 if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode) in find_table_device()
811 mutex_lock(&md->table_devices_lock); in dm_get_table_device()
812 td = find_table_device(&md->table_devices, dev, mode); in dm_get_table_device()
816 mutex_unlock(&md->table_devices_lock); in dm_get_table_device()
820 refcount_inc(&td->count); in dm_get_table_device()
822 mutex_unlock(&md->table_devices_lock); in dm_get_table_device()
824 *result = &td->dm_dev; in dm_get_table_device()
832 mutex_lock(&md->table_devices_lock); in dm_put_table_device()
833 if (refcount_dec_and_test(&td->count)) in dm_put_table_device()
835 mutex_unlock(&md->table_devices_lock); in dm_put_table_device()
843 *geo = md->geometry; in dm_get_geometry()
853 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors; in dm_set_geometry()
855 if (geo->start > sz) { in dm_set_geometry()
857 return -EINVAL; in dm_set_geometry()
860 md->geometry = *geo; in dm_set_geometry()
867 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); in __noflush_suspending()
872 struct mapped_device *md = io->md; in dm_requeue_add_io()
875 struct dm_io *next = md->requeue_list; in dm_requeue_add_io()
877 md->requeue_list = io; in dm_requeue_add_io()
878 io->next = next; in dm_requeue_add_io()
880 bio_list_add_head(&md->deferred, io->orig_bio); in dm_requeue_add_io()
887 queue_work(md->wq, &md->requeue_work); in dm_kick_requeue()
889 queue_work(md->wq, &md->work); in dm_kick_requeue()
894 * io->status is updated with error if requeue disallowed.
898 struct bio *bio = io->orig_bio; in dm_handle_requeue()
899 bool handle_requeue = (io->status == BLK_STS_DM_REQUEUE); in dm_handle_requeue()
900 bool handle_polled_eagain = ((io->status == BLK_STS_AGAIN) && in dm_handle_requeue()
901 (bio->bi_opf & REQ_POLLED)); in dm_handle_requeue()
902 struct mapped_device *md = io->md; in dm_handle_requeue()
908 if (bio->bi_opf & REQ_POLLED) { in dm_handle_requeue()
911 * (io->orig_bio may only reflect a subset of the in dm_handle_requeue()
912 * pre-split original) so clear REQ_POLLED. in dm_handle_requeue()
921 spin_lock_irqsave(&md->deferred_lock, flags); in dm_handle_requeue()
929 * noflush suspend was interrupted or this is in dm_handle_requeue()
930 * a write to a zoned target. in dm_handle_requeue()
932 io->status = BLK_STS_IOERR; in dm_handle_requeue()
934 spin_unlock_irqrestore(&md->deferred_lock, flags); in dm_handle_requeue()
945 struct bio *bio = io->orig_bio; in __dm_io_complete()
946 struct mapped_device *md = io->md; in __dm_io_complete()
954 io_error = io->status; in __dm_io_complete()
959 * Must handle target that DM_MAPIO_SUBMITTED only to in __dm_io_complete()
967 this_cpu_dec(*md->pending_io); in __dm_io_complete()
969 /* nudge anyone waiting on suspend queue */ in __dm_io_complete()
970 if (unlikely(wq_has_sleeper(&md->wait))) in __dm_io_complete()
971 wake_up(&md->wait); in __dm_io_complete()
982 bio->bi_opf &= ~REQ_PREFLUSH; in __dm_io_complete()
987 bio->bi_status = io_error; in __dm_io_complete()
999 /* reuse deferred lock to simplify dm_handle_requeue */ in dm_wq_requeue_work()
1000 spin_lock_irqsave(&md->deferred_lock, flags); in dm_wq_requeue_work()
1001 io = md->requeue_list; in dm_wq_requeue_work()
1002 md->requeue_list = NULL; in dm_wq_requeue_work()
1003 spin_unlock_irqrestore(&md->deferred_lock, flags); in dm_wq_requeue_work()
1006 struct dm_io *next = io->next; in dm_wq_requeue_work()
1008 dm_io_rewind(io, &md->disk->bio_split); in dm_wq_requeue_work()
1010 io->next = NULL; in dm_wq_requeue_work()
1020 * 1) io->orig_bio points to the real original bio, and the part mapped to
1023 * 2) io->orig_bio points to new cloned bio which matches the requeued dm_io.
1031 * we may run into long bio clone chain during suspend and OOM could in dm_io_complete()
1051 if (atomic_dec_and_test(&io->io_count)) in __dm_io_dec_pending()
1059 /* Push-back supersedes any I/O errors */ in dm_io_set_error()
1060 spin_lock_irqsave(&io->lock, flags); in dm_io_set_error()
1061 if (!(io->status == BLK_STS_DM_REQUEUE && in dm_io_set_error()
1062 __noflush_suspending(io->md))) { in dm_io_set_error()
1063 io->status = error; in dm_io_set_error()
1065 spin_unlock_irqrestore(&io->lock, flags); in dm_io_set_error()
1078 * count on 'md'. But _not_ imposing verification to avoid atomic_read(),
1082 return &md->queue->limits; in dm_get_queue_limits()
1090 limits->max_hw_discard_sectors = 0; in disable_discard()
1098 limits->max_write_zeroes_sectors = 0; in disable_write_zeroes()
1103 return unlikely((bio->bi_opf & REQ_SWAP) != 0) && unlikely(ti->limit_swap_bios); in swap_bios_limit()
1108 blk_status_t error = bio->bi_status; in clone_endio()
1110 struct dm_target *ti = tio->ti; in clone_endio()
1111 dm_endio_fn endio = likely(ti != NULL) ? ti->type->end_io : NULL; in clone_endio()
1112 struct dm_io *io = tio->io; in clone_endio()
1113 struct mapped_device *md = io->md; in clone_endio()
1117 !bdev_max_discard_sectors(bio->bi_bdev)) in clone_endio()
1120 !bdev_write_zeroes_sectors(bio->bi_bdev)) in clone_endio()
1125 unlikely(bdev_is_zoned(bio->bi_bdev))) in clone_endio()
1135 * Requeuing writes to a sequential zone of a zoned in clone_endio()
1159 up(&md->swap_bios_semaphore); in clone_endio()
1166 * Return maximum size of I/O possible at the supplied sector up to the current
1172 return ti->len - target_offset; in max_io_len_target_boundary()
1183 * Does the target need to split IO even further? in __max_io_len()
1184 * - varied (per target) IO splitting is a tenet of DM; this in __max_io_len()
1191 min(max_sectors ? : queue_max_sectors(ti->table->md->queue), in __max_io_len()
1197 return __max_io_len(ti, sector, ti->max_io_len, 0); in max_io_len()
1205 ti->error = "Maximum size of target IO is too large"; in dm_set_target_max_io_len()
1206 return -EINVAL; in dm_set_target_max_io_len()
1209 ti->max_io_len = (uint32_t) len; in dm_set_target_max_io_len()
1217 __acquires(md->io_barrier) in dm_dax_get_live_target()
1240 long len, ret = -EIO; in dm_dax_direct_access()
1247 if (!ti->type->direct_access) in dm_dax_direct_access()
1253 ret = ti->type->direct_access(ti, pgoff, nr_pages, mode, kaddr, pfn); in dm_dax_direct_access()
1267 int ret = -EIO; in dm_dax_zero_page_range()
1274 if (WARN_ON(!ti->type->dax_zero_page_range)) { in dm_dax_zero_page_range()
1276 * ->zero_page_range() is mandatory dax operation. If we are in dm_dax_zero_page_range()
1281 ret = ti->type->dax_zero_page_range(ti, pgoff, nr_pages); in dm_dax_zero_page_range()
1298 if (!ti || !ti->type->dax_recovery_write) in dm_dax_recovery_write()
1301 ret = ti->type->dax_recovery_write(ti, pgoff, addr, bytes, i); in dm_dax_recovery_write()
1313 * dm_accept_partial_bio informs the dm that the target only wants to process
1318 * +--------------------+---------------+-------+
1320 * +--------------------+---------------+-------+
1322 * <-------------- *tio->len_ptr --------------->
1323 * <----- bio_sectors ----->
1324 * <-- n_sectors -->
1328 * Region 2 is the remaining bio size that the target wants to process.
1329 * (it may be empty if region 1 is non-empty, although there is no reason
1330 * to make it empty)
1331 * The target requires that region 3 is to be sent in the next bio.
1333 * If the target wants to receive multiple copies of the bio (via num_*bios, etc),
1340 struct dm_io *io = tio->io; in dm_accept_partial_bio()
1346 BUG_ON(bio_sectors > *tio->len_ptr); in dm_accept_partial_bio()
1349 *tio->len_ptr -= bio_sectors - n_sectors; in dm_accept_partial_bio()
1350 bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT; in dm_accept_partial_bio()
1357 io->sectors = n_sectors; in dm_accept_partial_bio()
1358 io->sector_offset = bio_sectors(io->orig_bio); in dm_accept_partial_bio()
1363 * @clone: clone bio that DM core passed to target's .map function
1366 * Targets should use this interface to submit bios they take
1369 * Target should also enable ti->accounts_remapped_io
1374 struct dm_io *io = tio->io; in dm_submit_bio_remap()
1381 * Account io->origin_bio to DM dev on behalf of target in dm_submit_bio_remap()
1386 trace_block_bio_remap(tgt_clone, disk_devt(io->md->disk), in dm_submit_bio_remap()
1387 tio->old_sector); in dm_submit_bio_remap()
1394 mutex_lock(&md->swap_bios_lock); in __set_swap_bios_limit()
1395 while (latch < md->swap_bios) { in __set_swap_bios_limit()
1397 down(&md->swap_bios_semaphore); in __set_swap_bios_limit()
1398 md->swap_bios--; in __set_swap_bios_limit()
1400 while (latch > md->swap_bios) { in __set_swap_bios_limit()
1402 up(&md->swap_bios_semaphore); in __set_swap_bios_limit()
1403 md->swap_bios++; in __set_swap_bios_limit()
1405 mutex_unlock(&md->swap_bios_lock); in __set_swap_bios_limit()
1411 struct dm_target *ti = tio->ti; in __map_bio()
1412 struct dm_io *io = tio->io; in __map_bio()
1413 struct mapped_device *md = io->md; in __map_bio()
1416 clone->bi_end_io = clone_endio; in __map_bio()
1421 tio->old_sector = clone->bi_iter.bi_sector; in __map_bio()
1427 if (unlikely(latch != md->swap_bios)) in __map_bio()
1429 down(&md->swap_bios_semaphore); in __map_bio()
1432 if (likely(ti->type->map == linear_map)) in __map_bio()
1434 else if (ti->type->map == stripe_map) in __map_bio()
1437 r = ti->type->map(ti, clone); in __map_bio()
1442 if (!ti->accounts_remapped_io) in __map_bio()
1452 up(&md->swap_bios_semaphore); in __map_bio()
1467 struct dm_io *io = ci->io; in setup_split_accounting()
1469 if (ci->sector_count > len) { in setup_split_accounting()
1475 io->sectors = len; in setup_split_accounting()
1476 io->sector_offset = bio_sectors(ci->bio); in setup_split_accounting()
1491 mutex_lock(&ci->io->md->table_devices_lock); in alloc_multiple_bios()
1501 mutex_unlock(&ci->io->md->table_devices_lock); in alloc_multiple_bios()
1520 /* dm_accept_partial_bio() is not supported with shared tio->len_ptr */ in __send_duplicate_bios()
1525 * Using alloc_multiple_bios(), even if num_bios is 1, to consistently in __send_duplicate_bios()
1541 struct dm_table *t = ci->map; in __send_empty_flush()
1545 * Use an on-stack bio for this, it's safe since we don't in __send_empty_flush()
1546 * need to reference it after submit. It's just used as in __send_empty_flush()
1549 bio_init(&flush_bio, ci->io->md->disk->part0, NULL, 0, in __send_empty_flush()
1552 ci->bio = &flush_bio; in __send_empty_flush()
1553 ci->sector_count = 0; in __send_empty_flush()
1554 ci->io->tio.clone.bi_iter.bi_size = 0; in __send_empty_flush()
1556 if (!t->flush_bypasses_map) { in __send_empty_flush()
1557 for (unsigned int i = 0; i < t->num_targets; i++) { in __send_empty_flush()
1561 if (unlikely(ti->num_flush_bios == 0)) in __send_empty_flush()
1564 atomic_add(ti->num_flush_bios, &ci->io->io_count); in __send_empty_flush()
1565 bios = __send_duplicate_bios(ci, ti, ti->num_flush_bios, in __send_empty_flush()
1567 atomic_sub(ti->num_flush_bios - bios, &ci->io->io_count); in __send_empty_flush()
1571 * Note that there's no need to grab t->devices_lock here in __send_empty_flush()
1583 * used by multiple targets), so we set tio->ti = NULL. in __send_empty_flush()
1584 * We must check for NULL in the I/O processing path, to in __send_empty_flush()
1588 atomic_add(1, &ci->io->io_count); in __send_empty_flush()
1589 bio_set_dev(clone, dd->dm_dev->bdev); in __send_empty_flush()
1590 clone->bi_end_io = clone_endio; in __send_empty_flush()
1599 atomic_sub(1, &ci->io->io_count); in __send_empty_flush()
1601 bio_uninit(ci->bio); in __send_empty_flush()
1610 len = min_t(sector_t, ci->sector_count, in __send_abnormal_io()
1611 __max_io_len(ti, ci->sector, max_granularity, max_sectors)); in __send_abnormal_io()
1613 atomic_add(num_bios, &ci->io->io_count); in __send_abnormal_io()
1619 atomic_sub(num_bios - bios + 1, &ci->io->io_count); in __send_abnormal_io()
1621 ci->sector += len; in __send_abnormal_io()
1622 ci->sector_count -= len; in __send_abnormal_io()
1648 struct queue_limits *limits = dm_get_queue_limits(ti->table->md); in __process_abnormal_io()
1650 switch (bio_op(ci->bio)) { in __process_abnormal_io()
1652 num_bios = ti->num_discard_bios; in __process_abnormal_io()
1653 max_sectors = limits->max_discard_sectors; in __process_abnormal_io()
1654 if (ti->max_discard_granularity) in __process_abnormal_io()
1658 num_bios = ti->num_secure_erase_bios; in __process_abnormal_io()
1659 max_sectors = limits->max_secure_erase_sectors; in __process_abnormal_io()
1662 num_bios = ti->num_write_zeroes_bios; in __process_abnormal_io()
1663 max_sectors = limits->max_write_zeroes_sectors; in __process_abnormal_io()
1684 * Reuse ->bi_private as dm_io list head for storing all dm_io instances
1685 * associated with this bio, and this bio's bi_private needs to be
1686 * stored in dm_io->data before the reuse.
1688 * bio->bi_private is owned by fs or upper layer, so block layer won't
1694 return (struct dm_io **)&bio->bi_private; in dm_poll_list_head()
1701 if (!(bio->bi_opf & REQ_DM_POLL_LIST)) { in dm_queue_poll_io()
1702 bio->bi_opf |= REQ_DM_POLL_LIST; in dm_queue_poll_io()
1707 io->data = bio->bi_private; in dm_queue_poll_io()
1709 /* tell block layer to poll for completion */ in dm_queue_poll_io()
1710 bio->bi_cookie = ~BLK_QC_T_NONE; in dm_queue_poll_io()
1712 io->next = NULL; in dm_queue_poll_io()
1715 * bio recursed due to split, reuse original poll list, in dm_queue_poll_io()
1716 * and save bio->bi_private too. in dm_queue_poll_io()
1718 io->data = (*head)->data; in dm_queue_poll_io()
1719 io->next = *head; in dm_queue_poll_io()
1726 * Select the correct strategy for processing a non-flush bio.
1734 ti = dm_table_find_target(ci->map, ci->sector); in __split_and_process_bio()
1738 if (unlikely(ci->is_abnormal_io)) in __split_and_process_bio()
1745 ci->submit_as_polled = !!(ci->bio->bi_opf & REQ_POLLED); in __split_and_process_bio()
1747 len = min_t(sector_t, max_io_len(ti, ci->sector), ci->sector_count); in __split_and_process_bio()
1748 if (ci->bio->bi_opf & REQ_ATOMIC && len != ci->sector_count) in __split_and_process_bio()
1753 if (unlikely(ci->bio->bi_opf & REQ_NOWAIT)) { in __split_and_process_bio()
1754 if (unlikely(!dm_target_supports_nowait(ti->type))) in __split_and_process_bio()
1765 ci->sector += len; in __split_and_process_bio()
1766 ci->sector_count -= len; in __split_and_process_bio()
1774 ci->map = map; in init_clone_info()
1775 ci->io = io; in init_clone_info()
1776 ci->bio = bio; in init_clone_info()
1777 ci->is_abnormal_io = is_abnormal; in init_clone_info()
1778 ci->submit_as_polled = false; in init_clone_info()
1779 ci->sector = bio->bi_iter.bi_sector; in init_clone_info()
1780 ci->sector_count = bio_sectors(bio); in init_clone_info()
1782 /* Shouldn't happen but sector_count was being set to 0 so... */ in init_clone_info()
1784 WARN_ON_ONCE(op_is_zone_mgmt(bio_op(bio)) && ci->sector_count)) in init_clone_info()
1785 ci->sector_count = 0; in init_clone_info()
1808 struct mapped_device *md = ci->io->md; in __send_zone_reset_all_emulated()
1809 unsigned int zone_sectors = md->disk->queue->limits.chunk_sectors; in __send_zone_reset_all_emulated()
1814 sector_t sector = ti->begin; in __send_zone_reset_all_emulated()
1818 nr_zones = ti->len >> ilog2(zone_sectors); in __send_zone_reset_all_emulated()
1823 ret = dm_zone_get_reset_bitmap(md, ci->map, ti->begin, in __send_zone_reset_all_emulated()
1830 /* If we have no zone to reset, we are done. */ in __send_zone_reset_all_emulated()
1835 atomic_add(nr_zones, &ci->io->io_count); in __send_zone_reset_all_emulated()
1845 /* This may take a while, so be nice to others */ in __send_zone_reset_all_emulated()
1850 * We may need to reset thousands of zones, so let's in __send_zone_reset_all_emulated()
1857 /* Get a clone and change it to a regular reset operation. */ in __send_zone_reset_all_emulated()
1859 clone->bi_opf &= ~REQ_OP_MASK; in __send_zone_reset_all_emulated()
1860 clone->bi_opf |= REQ_OP_ZONE_RESET | REQ_SYNC; in __send_zone_reset_all_emulated()
1861 clone->bi_iter.bi_sector = sector; in __send_zone_reset_all_emulated()
1862 clone->bi_iter.bi_size = 0; in __send_zone_reset_all_emulated()
1867 nr_reset--; in __send_zone_reset_all_emulated()
1871 atomic_sub(nr_zones - num_bios, &ci->io->io_count); in __send_zone_reset_all_emulated()
1872 ci->sector_count = 0; in __send_zone_reset_all_emulated()
1885 atomic_add(1, &ci->io->io_count); in __send_zone_reset_all_native()
1887 atomic_sub(1 - bios, &ci->io->io_count); in __send_zone_reset_all_native()
1889 ci->sector_count = 0; in __send_zone_reset_all_native()
1894 struct dm_table *t = ci->map; in __send_zone_reset_all()
1897 for (unsigned int i = 0; i < t->num_targets; i++) { in __send_zone_reset_all()
1900 if (ti->zone_reset_all_supported) { in __send_zone_reset_all()
1911 atomic_sub(1, &ci->io->io_count); in __send_zone_reset_all()
1933 * Entry point to split a bio into clones and submit them to the targets.
1957 * emulation to ensure that the BIO does not cross zone in dm_split_and_process_bio()
1967 * need zone append emulation (e.g. dm-crypt). in dm_split_and_process_bio()
1973 if (unlikely(bio->bi_opf & REQ_NOWAIT) && !is_abnormal) { in dm_split_and_process_bio()
1976 * multiple bios and there's no easy way how to undo the in dm_split_and_process_bio()
1979 if (bio->bi_opf & REQ_PREFLUSH) { in dm_split_and_process_bio()
1985 /* Unable to do anything without dm_io. */ in dm_split_and_process_bio()
1994 if (bio->bi_opf & REQ_PREFLUSH) { in dm_split_and_process_bio()
2010 * Remainder must be passed to submit_bio_noacct() so it gets handled in dm_split_and_process_bio()
2013 bio_trim(bio, io->sectors, ci.sector_count); in dm_split_and_process_bio()
2014 trace_block_split(bio, bio->bi_iter.bi_sector); in dm_split_and_process_bio()
2019 * Drop the extra reference count for non-POLLED bio, and hold one in dm_split_and_process_bio()
2023 * in bio->bi_private, so that dm_poll_bio can poll them all. in dm_split_and_process_bio()
2031 atomic_dec(&io->io_count); in dm_split_and_process_bio()
2039 struct mapped_device *md = bio->bi_bdev->bd_disk->private_data; in dm_submit_bio()
2052 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) { in dm_submit_bio()
2053 if (bio->bi_opf & REQ_NOWAIT) in dm_submit_bio()
2055 else if (bio->bi_opf & REQ_RAHEAD) in dm_submit_bio()
2070 WARN_ON_ONCE(!dm_tio_is_normal(&io->tio)); in dm_poll_dm_io()
2073 if (atomic_read(&io->io_count) > 1) in dm_poll_dm_io()
2074 bio_poll(&io->tio.clone, iob, flags); in dm_poll_dm_io()
2077 return atomic_read(&io->io_count) == 1; in dm_poll_dm_io()
2089 if (!(bio->bi_opf & REQ_DM_POLL_LIST)) in dm_poll_bio()
2098 * submitted via submit_bio_noacct()'s depth-first submission. in dm_poll_bio()
2102 bio->bi_opf &= ~REQ_DM_POLL_LIST; in dm_poll_bio()
2103 bio->bi_private = list->data; in dm_poll_bio()
2105 for (curr = list, next = curr->next; curr; curr = next, next = in dm_poll_bio()
2106 curr ? curr->next : NULL) { in dm_poll_bio()
2114 curr->next = tmp; in dm_poll_bio()
2121 bio->bi_opf |= REQ_DM_POLL_LIST; in dm_poll_bio()
2122 /* Reset bio->bi_private to dm_io list head */ in dm_poll_bio()
2130 *---------------------------------------------------------------
2131 * An IDR is used to keep track of allocated minor numbers.
2132 *---------------------------------------------------------------
2149 return -EINVAL; in specific_minor()
2159 return r == -ENOSPC ? -EBUSY : r; in specific_minor()
2189 dm_destroy_crypto_profile(q->crypto_profile); in dm_queue_destroy_crypto_profile()
2201 if (md->wq) in cleanup_mapped_device()
2202 destroy_workqueue(md->wq); in cleanup_mapped_device()
2203 dm_free_md_mempools(md->mempools); in cleanup_mapped_device()
2205 if (md->dax_dev) { in cleanup_mapped_device()
2206 dax_remove_host(md->disk); in cleanup_mapped_device()
2207 kill_dax(md->dax_dev); in cleanup_mapped_device()
2208 put_dax(md->dax_dev); in cleanup_mapped_device()
2209 md->dax_dev = NULL; in cleanup_mapped_device()
2212 if (md->disk) { in cleanup_mapped_device()
2214 md->disk->private_data = NULL; in cleanup_mapped_device()
2220 list_for_each_entry(td, &md->table_devices, list) { in cleanup_mapped_device()
2221 bd_unlink_disk_holder(td->dm_dev.bdev, in cleanup_mapped_device()
2222 md->disk); in cleanup_mapped_device()
2226 * Hold lock to make sure del_gendisk() won't concurrent in cleanup_mapped_device()
2229 mutex_lock(&md->table_devices_lock); in cleanup_mapped_device()
2230 del_gendisk(md->disk); in cleanup_mapped_device()
2231 mutex_unlock(&md->table_devices_lock); in cleanup_mapped_device()
2233 dm_queue_destroy_crypto_profile(md->queue); in cleanup_mapped_device()
2234 put_disk(md->disk); in cleanup_mapped_device()
2237 if (md->pending_io) { in cleanup_mapped_device()
2238 free_percpu(md->pending_io); in cleanup_mapped_device()
2239 md->pending_io = NULL; in cleanup_mapped_device()
2242 cleanup_srcu_struct(&md->io_barrier); in cleanup_mapped_device()
2244 mutex_destroy(&md->suspend_lock); in cleanup_mapped_device()
2245 mutex_destroy(&md->type_lock); in cleanup_mapped_device()
2246 mutex_destroy(&md->table_devices_lock); in cleanup_mapped_device()
2247 mutex_destroy(&md->swap_bios_lock); in cleanup_mapped_device()
2264 DMERR("unable to allocate device, out of memory."); in alloc_dev()
2279 r = init_srcu_struct(&md->io_barrier); in alloc_dev()
2283 md->numa_node_id = numa_node_id; in alloc_dev()
2284 md->init_tio_pdu = false; in alloc_dev()
2285 md->type = DM_TYPE_NONE; in alloc_dev()
2286 mutex_init(&md->suspend_lock); in alloc_dev()
2287 mutex_init(&md->type_lock); in alloc_dev()
2288 mutex_init(&md->table_devices_lock); in alloc_dev()
2289 spin_lock_init(&md->deferred_lock); in alloc_dev()
2290 atomic_set(&md->holders, 1); in alloc_dev()
2291 atomic_set(&md->open_count, 0); in alloc_dev()
2292 atomic_set(&md->event_nr, 0); in alloc_dev()
2293 atomic_set(&md->uevent_seq, 0); in alloc_dev()
2294 INIT_LIST_HEAD(&md->uevent_list); in alloc_dev()
2295 INIT_LIST_HEAD(&md->table_devices); in alloc_dev()
2296 spin_lock_init(&md->uevent_lock); in alloc_dev()
2299 * default to bio-based until DM table is loaded and md->type in alloc_dev()
2300 * established. If request-based table is loaded: blk-mq will in alloc_dev()
2303 md->disk = blk_alloc_disk(NULL, md->numa_node_id); in alloc_dev()
2304 if (IS_ERR(md->disk)) { in alloc_dev()
2305 md->disk = NULL; in alloc_dev()
2308 md->queue = md->disk->queue; in alloc_dev()
2310 init_waitqueue_head(&md->wait); in alloc_dev()
2311 INIT_WORK(&md->work, dm_wq_work); in alloc_dev()
2312 INIT_WORK(&md->requeue_work, dm_wq_requeue_work); in alloc_dev()
2313 init_waitqueue_head(&md->eventq); in alloc_dev()
2314 init_completion(&md->kobj_holder.completion); in alloc_dev()
2316 md->requeue_list = NULL; in alloc_dev()
2317 md->swap_bios = get_swap_bios(); in alloc_dev()
2318 sema_init(&md->swap_bios_semaphore, md->swap_bios); in alloc_dev()
2319 mutex_init(&md->swap_bios_lock); in alloc_dev()
2321 md->disk->major = _major; in alloc_dev()
2322 md->disk->first_minor = minor; in alloc_dev()
2323 md->disk->minors = 1; in alloc_dev()
2324 md->disk->flags |= GENHD_FL_NO_PART; in alloc_dev()
2325 md->disk->fops = &dm_blk_dops; in alloc_dev()
2326 md->disk->private_data = md; in alloc_dev()
2327 sprintf(md->disk->disk_name, "dm-%d", minor); in alloc_dev()
2331 if (PTR_ERR(dax_dev) != -EOPNOTSUPP) in alloc_dev()
2336 md->dax_dev = dax_dev; in alloc_dev()
2337 if (dax_add_host(dax_dev, md->disk)) in alloc_dev()
2341 format_dev_t(md->name, MKDEV(_major, minor)); in alloc_dev()
2343 md->wq = alloc_workqueue("kdmflush/%s", WQ_MEM_RECLAIM, 0, md->name); in alloc_dev()
2344 if (!md->wq) in alloc_dev()
2347 md->pending_io = alloc_percpu(unsigned long); in alloc_dev()
2348 if (!md->pending_io) in alloc_dev()
2351 r = dm_stats_init(&md->stats); in alloc_dev()
2379 int minor = MINOR(disk_devt(md->disk)); in free_dev()
2385 WARN_ON_ONCE(!list_empty(&md->table_devices)); in free_dev()
2386 dm_stats_cleanup(&md->stats); in free_dev()
2394 * Bind a table to the device.
2402 spin_lock_irqsave(&md->uevent_lock, flags); in event_callback()
2403 list_splice_init(&md->uevent_list, &uevents); in event_callback()
2404 spin_unlock_irqrestore(&md->uevent_lock, flags); in event_callback()
2406 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj); in event_callback()
2408 atomic_inc(&md->event_nr); in event_callback()
2409 wake_up(&md->eventq); in event_callback()
2423 lockdep_assert_held(&md->suspend_lock); in __bind()
2431 memset(&md->geometry, 0, sizeof(md->geometry)); in __bind()
2433 set_capacity(md->disk, size); in __bind()
2439 * Leverage the fact that request-based DM targets are in __bind()
2440 * immutable singletons - used to optimize dm_mq_queue_rq. in __bind()
2442 md->immutable_target = dm_table_get_immutable_target(t); in __bind()
2445 * There is no need to reload with request-based dm because the in __bind()
2448 * Note for future: If you are to reload bioset, prep-ed in __bind()
2449 * requests in the queue may refer to bio from the old bioset, in __bind()
2450 * so you must walk through the queue to unprep. in __bind()
2452 if (!md->mempools) { in __bind()
2453 md->mempools = t->mempools; in __bind()
2454 t->mempools = NULL; in __bind()
2462 dm_free_md_mempools(md->mempools); in __bind()
2463 md->mempools = t->mempools; in __bind()
2464 t->mempools = NULL; in __bind()
2467 ret = dm_table_set_restrictions(t, md->queue, limits); in __bind()
2473 old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); in __bind()
2474 rcu_assign_pointer(md->map, (void *)t); in __bind()
2475 md->immutable_target_type = dm_table_get_immutable_target_type(t); in __bind()
2484 * Returns unbound table for the caller to free.
2488 struct dm_table *map = rcu_dereference_protected(md->map, 1); in __unbind()
2494 RCU_INIT_POINTER(md->map, NULL); in __unbind()
2509 return -ENXIO; in dm_create()
2518 * Functions to manage md->type.
2519 * All are required to hold md->type_lock.
2523 mutex_lock(&md->type_lock); in dm_lock_md_type()
2528 mutex_unlock(&md->type_lock); in dm_unlock_md_type()
2533 return md->type; in dm_get_md_type()
2538 return md->immutable_target_type; in dm_get_immutable_target_type()
2554 md->disk->fops = &dm_rq_blk_dops; in dm_setup_md_queue()
2557 DMERR("Cannot initialize queue for request-based dm mapped device"); in dm_setup_md_queue()
2567 r = dm_table_set_restrictions(t, md->queue, &limits); in dm_setup_md_queue()
2572 * Hold lock to make sure add_disk() and del_gendisk() won't concurrent in dm_setup_md_queue()
2575 mutex_lock(&md->table_devices_lock); in dm_setup_md_queue()
2576 r = add_disk(md->disk); in dm_setup_md_queue()
2577 mutex_unlock(&md->table_devices_lock); in dm_setup_md_queue()
2582 * Register the holder relationship for devices added before the disk in dm_setup_md_queue()
2585 list_for_each_entry(td, &md->table_devices, list) { in dm_setup_md_queue()
2586 r = bd_link_disk_holder(td->dm_dev.bdev, md->disk); in dm_setup_md_queue()
2595 md->type = type; in dm_setup_md_queue()
2599 list_for_each_entry_continue_reverse(td, &md->table_devices, list) in dm_setup_md_queue()
2600 bd_unlink_disk_holder(td->dm_dev.bdev, md->disk); in dm_setup_md_queue()
2601 mutex_lock(&md->table_devices_lock); in dm_setup_md_queue()
2602 del_gendisk(md->disk); in dm_setup_md_queue()
2603 mutex_unlock(&md->table_devices_lock); in dm_setup_md_queue()
2619 test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) { in dm_get_md()
2633 return md->interface_ptr; in dm_get_mdptr()
2638 md->interface_ptr = ptr; in dm_set_mdptr()
2643 atomic_inc(&md->holders); in dm_get()
2644 BUG_ON(test_bit(DMF_FREEING, &md->flags)); in dm_get()
2650 if (test_bit(DMF_FREEING, &md->flags)) { in dm_hold()
2652 return -EBUSY; in dm_hold()
2662 return md->name; in dm_device_name()
2675 set_bit(DMF_FREEING, &md->flags); in __dm_destroy()
2678 blk_mark_disk_dead(md->disk); in __dm_destroy()
2682 * do not race with internal suspend. in __dm_destroy()
2684 mutex_lock(&md->suspend_lock); in __dm_destroy()
2688 set_bit(DMF_SUSPENDED, &md->flags); in __dm_destroy()
2689 set_bit(DMF_POST_SUSPENDING, &md->flags); in __dm_destroy()
2694 mutex_unlock(&md->suspend_lock); in __dm_destroy()
2697 * Rare, but there may be I/O requests still going to complete, in __dm_destroy()
2698 * for example. Wait for all references to disappear. in __dm_destroy()
2703 while (atomic_read(&md->holders)) in __dm_destroy()
2705 else if (atomic_read(&md->holders)) in __dm_destroy()
2707 dm_device_name(md), atomic_read(&md->holders)); in __dm_destroy()
2725 atomic_dec(&md->holders); in dm_put()
2735 sum += *per_cpu_ptr(md->pending_io, cpu); in dm_in_flight_bios()
2746 prepare_to_wait(&md->wait, &wait, task_state); in dm_wait_for_bios_completion()
2752 r = -ERESTARTSYS; in dm_wait_for_bios_completion()
2758 finish_wait(&md->wait, &wait); in dm_wait_for_bios_completion()
2769 if (!queue_is_mq(md->queue)) in dm_wait_for_completion()
2773 if (!blk_mq_queue_inflight(md->queue)) in dm_wait_for_completion()
2777 r = -ERESTARTSYS; in dm_wait_for_completion()
2795 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) { in dm_wq_work()
2796 spin_lock_irq(&md->deferred_lock); in dm_wq_work()
2797 bio = bio_list_pop(&md->deferred); in dm_wq_work()
2798 spin_unlock_irq(&md->deferred_lock); in dm_wq_work()
2810 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); in dm_queue_flush()
2812 queue_work(md->wq, &md->work); in dm_queue_flush()
2816 * Swap in a new table, returning the old one for the caller to destroy.
2820 struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL); in dm_swap_table()
2824 mutex_lock(&md->suspend_lock); in dm_swap_table()
2839 limits = md->queue->limits; in dm_swap_table()
2855 mutex_unlock(&md->suspend_lock); in dm_swap_table()
2860 * Functions to lock and unlock any filesystem running on the
2867 WARN_ON(test_bit(DMF_FROZEN, &md->flags)); in lock_fs()
2869 r = bdev_freeze(md->disk->part0); in lock_fs()
2871 set_bit(DMF_FROZEN, &md->flags); in lock_fs()
2877 if (!test_bit(DMF_FROZEN, &md->flags)) in unlock_fs()
2879 bdev_thaw(md->disk->part0); in unlock_fs()
2880 clear_bit(DMF_FROZEN, &md->flags); in unlock_fs()
2889 * now. There is no request-processing activity. All new requests
2890 * are being added to md->deferred list.
2900 lockdep_assert_held(&md->suspend_lock); in __dm_suspend()
2907 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); in __dm_suspend()
2918 * Flush I/O to the device. in __dm_suspend()
2921 * (lock_fs() flushes I/Os and waits for them to complete.) in __dm_suspend()
2933 * to target drivers i.e. no one may be executing in __dm_suspend()
2936 * To get all processes out of dm_split_and_process_bio in dm_submit_bio, in __dm_suspend()
2937 * we take the write lock. To prevent any process from reentering in __dm_suspend()
2940 * flush_workqueue(md->wq). in __dm_suspend()
2942 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); in __dm_suspend()
2944 synchronize_srcu(&md->io_barrier); in __dm_suspend()
2947 * Stop md->queue before flushing md->wq in case request-based in __dm_suspend()
2948 * dm defers requests to md->wq from md->queue. in __dm_suspend()
2951 dm_stop_queue(md->queue); in __dm_suspend()
2953 flush_workqueue(md->wq); in __dm_suspend()
2957 * We call dm_wait_for_completion to wait for all existing requests in __dm_suspend()
2958 * to finish. in __dm_suspend()
2962 set_bit(dmf_suspended_flag, &md->flags); in __dm_suspend()
2965 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); in __dm_suspend()
2967 synchronize_srcu(&md->io_barrier); in __dm_suspend()
2974 dm_start_queue(md->queue); in __dm_suspend()
2985 * We need to be able to change a mapping table under a mounted
2986 * filesystem. For example we might want to move some data in
2988 * dm_bind_table, dm_suspend must be called to flush any in
2992 * Suspend mechanism in request-based dm.
2996 * 3. Wait for all in-flight I/Os to be completed or requeued.
2998 * To abort suspend, start the request_queue.
3006 mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING); in dm_suspend()
3009 r = -EINVAL; in dm_suspend()
3015 mutex_unlock(&md->suspend_lock); in dm_suspend()
3016 r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE); in dm_suspend()
3022 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); in dm_suspend()
3032 set_bit(DMF_POST_SUSPENDING, &md->flags); in dm_suspend()
3034 clear_bit(DMF_POST_SUSPENDING, &md->flags); in dm_suspend()
3037 mutex_unlock(&md->suspend_lock); in dm_suspend()
3055 * Request-based dm is queueing the deferred I/Os in its request_queue. in __dm_resume()
3058 dm_start_queue(md->queue); in __dm_resume()
3071 r = -EINVAL; in dm_resume()
3072 mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING); in dm_resume()
3079 mutex_unlock(&md->suspend_lock); in dm_resume()
3080 r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE); in dm_resume()
3086 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); in dm_resume()
3094 clear_bit(DMF_SUSPENDED, &md->flags); in dm_resume()
3096 mutex_unlock(&md->suspend_lock); in dm_resume()
3102 * Internal suspend/resume works like userspace-driven suspend. It waits
3103 * until all bios finish and prevents issuing new bios to the target drivers.
3111 lockdep_assert_held(&md->suspend_lock); in __dm_internal_suspend()
3113 if (md->internal_suspend_count++) in __dm_internal_suspend()
3114 return; /* nested internal suspend */ in __dm_internal_suspend()
3117 set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags); in __dm_internal_suspend()
3118 return; /* nest suspend */ in __dm_internal_suspend()
3121 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); in __dm_internal_suspend()
3124 * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is in __dm_internal_suspend()
3125 * supported. Properly supporting a TASK_INTERRUPTIBLE internal suspend in __dm_internal_suspend()
3126 * would require changing .presuspend to return an error -- avoid this in __dm_internal_suspend()
3127 * until there is a need for more elaborate variants of internal suspend. in __dm_internal_suspend()
3132 set_bit(DMF_POST_SUSPENDING, &md->flags); in __dm_internal_suspend()
3134 clear_bit(DMF_POST_SUSPENDING, &md->flags); in __dm_internal_suspend()
3142 BUG_ON(!md->internal_suspend_count); in __dm_internal_resume()
3144 if (--md->internal_suspend_count) in __dm_internal_resume()
3145 return; /* resume from nested internal suspend */ in __dm_internal_resume()
3148 goto done; /* resume from nested suspend */ in __dm_internal_resume()
3150 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); in __dm_internal_resume()
3155 * tricky situation. We can't return an error to the caller. We in __dm_internal_resume()
3161 * So, we fake normal suspend here, to make sure that the in __dm_internal_resume()
3165 set_bit(DMF_SUSPENDED, &md->flags); in __dm_internal_resume()
3168 clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags); in __dm_internal_resume()
3170 wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY); in __dm_internal_resume()
3175 mutex_lock(&md->suspend_lock); in dm_internal_suspend_noflush()
3177 mutex_unlock(&md->suspend_lock); in dm_internal_suspend_noflush()
3183 mutex_lock(&md->suspend_lock); in dm_internal_resume()
3185 mutex_unlock(&md->suspend_lock); in dm_internal_resume()
3190 * Fast variants of internal suspend/resume hold md->suspend_lock,
3191 * which prevents interaction with userspace-driven suspend.
3196 mutex_lock(&md->suspend_lock); in dm_internal_suspend_fast()
3200 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); in dm_internal_suspend_fast()
3201 synchronize_srcu(&md->io_barrier); in dm_internal_suspend_fast()
3202 flush_workqueue(md->wq); in dm_internal_suspend_fast()
3215 mutex_unlock(&md->suspend_lock); in dm_internal_resume_fast()
3220 *---------------------------------------------------------------
3222 *---------------------------------------------------------------
3243 r = kobject_uevent_env(&disk_to_dev(md->disk)->kobj, action, envp); in dm_kobject_uevent()
3252 return atomic_add_return(1, &md->uevent_seq); in dm_next_uevent_seq()
3257 return atomic_read(&md->event_nr); in dm_get_event_nr()
3262 return wait_event_interruptible(md->eventq, in dm_wait_event()
3263 (event_nr != atomic_read(&md->event_nr))); in dm_wait_event()
3270 spin_lock_irqsave(&md->uevent_lock, flags); in dm_uevent_add()
3271 list_add(elist, &md->uevent_list); in dm_uevent_add()
3272 spin_unlock_irqrestore(&md->uevent_lock, flags); in dm_uevent_add()
3281 return md->disk; in dm_disk()
3287 return &md->kobj_holder.kobj; in dm_kobject()
3297 if (test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) { in dm_get_from_kobject()
3310 return test_bit(DMF_SUSPENDED, &md->flags); in dm_suspended_md()
3315 return test_bit(DMF_POST_SUSPENDING, &md->flags); in dm_post_suspending_md()
3320 return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags); in dm_suspended_internally_md()
3325 return test_bit(DMF_DEFERRED_REMOVE, &md->flags); in dm_test_deferred_remove_flag()
3330 return dm_suspended_md(ti->table->md); in dm_suspended()
3336 return dm_post_suspending_md(ti->table->md); in dm_post_suspending()
3342 return __noflush_suspending(ti->table->md); in dm_noflush_suspending()
3351 bioset_exit(&pools->bs); in dm_free_md_mempools()
3352 bioset_exit(&pools->io_bs); in dm_free_md_mempools()
3366 const struct block_device_operations *fops = dev->bdev->bd_disk->fops; in __dm_get_unique_id()
3368 if (!fops->get_unique_id) in __dm_get_unique_id()
3371 return fops->get_unique_id(dev->bdev->bd_disk, dm_id->id, dm_id->type); in __dm_get_unique_id()
3375 * Allow access to get_unique_id() for the first device returning a
3376 * non-zero result. Reasonable use expects all devices to have the
3379 static int dm_blk_get_unique_id(struct gendisk *disk, u8 *id, in dm_blk_get_unique_id() argument
3382 struct mapped_device *md = disk->private_data; in dm_blk_get_unique_id()
3397 if (table->num_targets != 1) in dm_blk_get_unique_id()
3401 if (!ti->type->iterate_devices) in dm_blk_get_unique_id()
3404 ret = ti->type->iterate_devices(ti, __dm_get_unique_id, &dm_id); in dm_blk_get_unique_id()
3425 struct mapped_device *md = bdev->bd_disk->private_data; in dm_call_pr()
3428 int ret = -ENOTTY, srcu_idx; in dm_call_pr()
3435 if (table->num_targets != 1) in dm_call_pr()
3440 ret = -EAGAIN; in dm_call_pr()
3444 ret = -EINVAL; in dm_call_pr()
3445 if (!ti->type->iterate_devices) in dm_call_pr()
3448 ti->type->iterate_devices(ti, fn, pr); in dm_call_pr()
3456 * For register / unregister we need to manually call out to every path.
3462 const struct pr_ops *ops = dev->bdev->bd_disk->fops->pr_ops; in __dm_pr_register()
3465 if (!ops || !ops->pr_register) { in __dm_pr_register()
3466 pr->ret = -EOPNOTSUPP; in __dm_pr_register()
3467 return -1; in __dm_pr_register()
3470 ret = ops->pr_register(dev->bdev, pr->old_key, pr->new_key, pr->flags); in __dm_pr_register()
3474 if (!pr->ret) in __dm_pr_register()
3475 pr->ret = ret; in __dm_pr_register()
3477 if (pr->fail_early) in __dm_pr_register()
3478 return -1; in __dm_pr_register()
3497 /* Didn't even get to register a path */ in dm_pr_register()
3508 /* unregister all paths if we failed to register any path */ in dm_pr_register()
3522 const struct pr_ops *ops = dev->bdev->bd_disk->fops->pr_ops; in __dm_pr_reserve()
3524 if (!ops || !ops->pr_reserve) { in __dm_pr_reserve()
3525 pr->ret = -EOPNOTSUPP; in __dm_pr_reserve()
3526 return -1; in __dm_pr_reserve()
3529 pr->ret = ops->pr_reserve(dev->bdev, pr->old_key, pr->type, pr->flags); in __dm_pr_reserve()
3530 if (!pr->ret) in __dm_pr_reserve()
3531 return -1; in __dm_pr_reserve()
3556 * If there is a non-All Registrants type of reservation, the release must be
3559 * try each path to make sure we got the correct path.
3565 const struct pr_ops *ops = dev->bdev->bd_disk->fops->pr_ops; in __dm_pr_release()
3567 if (!ops || !ops->pr_release) { in __dm_pr_release()
3568 pr->ret = -EOPNOTSUPP; in __dm_pr_release()
3569 return -1; in __dm_pr_release()
3572 pr->ret = ops->pr_release(dev->bdev, pr->old_key, pr->type); in __dm_pr_release()
3573 if (pr->ret) in __dm_pr_release()
3574 return -1; in __dm_pr_release()
3599 const struct pr_ops *ops = dev->bdev->bd_disk->fops->pr_ops; in __dm_pr_preempt()
3601 if (!ops || !ops->pr_preempt) { in __dm_pr_preempt()
3602 pr->ret = -EOPNOTSUPP; in __dm_pr_preempt()
3603 return -1; in __dm_pr_preempt()
3606 pr->ret = ops->pr_preempt(dev->bdev, pr->old_key, pr->new_key, pr->type, in __dm_pr_preempt()
3607 pr->abort); in __dm_pr_preempt()
3608 if (!pr->ret) in __dm_pr_preempt()
3609 return -1; in __dm_pr_preempt()
3634 struct mapped_device *md = bdev->bd_disk->private_data; in dm_pr_clear()
3642 ops = bdev->bd_disk->fops->pr_ops; in dm_pr_clear()
3643 if (ops && ops->pr_clear) in dm_pr_clear()
3644 r = ops->pr_clear(bdev, key); in dm_pr_clear()
3646 r = -EOPNOTSUPP; in dm_pr_clear()
3656 const struct pr_ops *ops = dev->bdev->bd_disk->fops->pr_ops; in __dm_pr_read_keys()
3658 if (!ops || !ops->pr_read_keys) { in __dm_pr_read_keys()
3659 pr->ret = -EOPNOTSUPP; in __dm_pr_read_keys()
3660 return -1; in __dm_pr_read_keys()
3663 pr->ret = ops->pr_read_keys(dev->bdev, pr->read_keys); in __dm_pr_read_keys()
3664 if (!pr->ret) in __dm_pr_read_keys()
3665 return -1; in __dm_pr_read_keys()
3688 const struct pr_ops *ops = dev->bdev->bd_disk->fops->pr_ops; in __dm_pr_read_reservation()
3690 if (!ops || !ops->pr_read_reservation) { in __dm_pr_read_reservation()
3691 pr->ret = -EOPNOTSUPP; in __dm_pr_read_reservation()
3692 return -1; in __dm_pr_read_reservation()
3695 pr->ret = ops->pr_read_reservation(dev->bdev, pr->rsv); in __dm_pr_read_reservation()
3696 if (!pr->ret) in __dm_pr_read_reservation()
3697 return -1; in __dm_pr_read_reservation()
3766 MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
3775 MODULE_AUTHOR("Joe Thornber <dm-[email protected]>");