Lines Matching refs:md

308 int dm_deleting_md(struct mapped_device *md)
310 return test_bit(DMF_DELETING, &md->flags);
315 struct mapped_device *md;
319 md = disk->private_data;
320 if (!md)
323 if (test_bit(DMF_FREEING, &md->flags) ||
324 dm_deleting_md(md)) {
325 md = NULL;
329 dm_get(md);
330 atomic_inc(&md->open_count);
334 return md ? 0 : -ENXIO;
339 struct mapped_device *md;
343 md = disk->private_data;
344 if (WARN_ON(!md))
347 if (atomic_dec_and_test(&md->open_count) &&
348 (test_bit(DMF_DEFERRED_REMOVE, &md->flags)))
351 dm_put(md);
356 int dm_open_count(struct mapped_device *md)
358 return atomic_read(&md->open_count);
364 int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred)
370 if (dm_open_count(md)) {
373 set_bit(DMF_DEFERRED_REMOVE, &md->flags);
374 } else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags))
377 set_bit(DMF_DELETING, &md->flags);
384 int dm_cancel_deferred_remove(struct mapped_device *md)
390 if (test_bit(DMF_DELETING, &md->flags))
393 clear_bit(DMF_DEFERRED_REMOVE, &md->flags);
407 struct mapped_device *md = bdev->bd_disk->private_data;
409 return dm_get_geometry(md, geo);
412 static int dm_prepare_ioctl(struct mapped_device *md, int *srcu_idx,
421 map = dm_get_live_table(md, srcu_idx);
433 if (dm_suspended_md(md))
438 dm_put_live_table(md, *srcu_idx);
446 static void dm_unprepare_ioctl(struct mapped_device *md, int srcu_idx)
448 dm_put_live_table(md, srcu_idx);
454 struct mapped_device *md = bdev->bd_disk->private_data;
457 r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
480 dm_unprepare_ioctl(md, srcu_idx);
523 unlikely(dm_stats_used(&io->md->stats))) {
531 dm_stats_account_io(&io->md->stats, bio_data_dir(bio),
573 static struct dm_io *alloc_io(struct mapped_device *md, struct bio *bio, gfp_t gfp_mask)
579 clone = bio_alloc_clone(NULL, bio, gfp_mask, &md->mempools->io_bs);
593 this_cpu_inc(*md->pending_io);
595 io->md = md;
599 if (blk_queue_io_stat(md->queue))
603 unlikely(dm_stats_used(&md->stats)))
604 dm_stats_record_start(&md->stats, &io->stats_aux);
617 struct mapped_device *md = ci->io->md;
628 &md->mempools->bs);
647 clone->bi_bdev = md->disk->part0;
649 bio_set_dev(clone, md->disk->part0);
670 static void queue_io(struct mapped_device *md, struct bio *bio)
674 spin_lock_irqsave(&md->deferred_lock, flags);
675 bio_list_add(&md->deferred, bio);
676 spin_unlock_irqrestore(&md->deferred_lock, flags);
677 queue_work(md->wq, &md->work);
682 * function to access the md->map field, and make sure they call
685 struct dm_table *dm_get_live_table(struct mapped_device *md,
686 int *srcu_idx) __acquires(md->io_barrier)
688 *srcu_idx = srcu_read_lock(&md->io_barrier);
690 return srcu_dereference(md->map, &md->io_barrier);
693 void dm_put_live_table(struct mapped_device *md,
694 int srcu_idx) __releases(md->io_barrier)
696 srcu_read_unlock(&md->io_barrier, srcu_idx);
699 void dm_sync_table(struct mapped_device *md)
701 synchronize_srcu(&md->io_barrier);
709 static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
712 return rcu_dereference(md->map);
715 static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
725 static struct table_device *open_table_device(struct mapped_device *md,
734 td = kmalloc_node(sizeof(*td), GFP_KERNEL, md->numa_node_id);
752 if (md->disk->slave_dir) {
753 r = bd_link_disk_holder(bdev, md->disk);
764 list_add(&td->list, &md->table_devices);
777 static void close_table_device(struct table_device *td, struct mapped_device *md)
779 if (md->disk->slave_dir)
780 bd_unlink_disk_holder(td->dm_dev.bdev, md->disk);
783 if (unlikely(test_bit(DMF_DEFERRED_REMOVE, &md->flags)))
805 int dm_get_table_device(struct mapped_device *md, dev_t dev, blk_mode_t mode,
810 mutex_lock(&md->table_devices_lock);
811 td = find_table_device(&md->table_devices, dev, mode);
813 td = open_table_device(md, dev, mode);
815 mutex_unlock(&md->table_devices_lock);
821 mutex_unlock(&md->table_devices_lock);
827 void dm_put_table_device(struct mapped_device *md, struct dm_dev *d)
831 mutex_lock(&md->table_devices_lock);
833 close_table_device(td, md);
834 mutex_unlock(&md->table_devices_lock);
840 int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
842 *geo = md->geometry;
850 int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
859 md->geometry = *geo;
864 static int __noflush_suspending(struct mapped_device *md)
866 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
871 struct mapped_device *md = io->md;
874 struct dm_io *next = md->requeue_list;
876 md->requeue_list = io;
879 bio_list_add_head(&md->deferred, io->orig_bio);
883 static void dm_kick_requeue(struct mapped_device *md, bool first_stage)
886 queue_work(md->wq, &md->requeue_work);
888 queue_work(md->wq, &md->work);
901 struct mapped_device *md = io->md;
920 spin_lock_irqsave(&md->deferred_lock, flags);
921 if ((__noflush_suspending(md) &&
922 !WARN_ON_ONCE(dm_is_zone_write(md, bio))) ||
933 spin_unlock_irqrestore(&md->deferred_lock, flags);
937 dm_kick_requeue(md, first_stage);
945 struct mapped_device *md = io->md;
966 this_cpu_dec(*md->pending_io);
969 if (unlikely(wq_has_sleeper(&md->wait)))
970 wake_up(&md->wait);
982 queue_io(md, bio);
993 struct mapped_device *md = container_of(work, struct mapped_device,
999 spin_lock_irqsave(&md->deferred_lock, flags);
1000 io = md->requeue_list;
1001 md->requeue_list = NULL;
1002 spin_unlock_irqrestore(&md->deferred_lock, flags);
1007 dm_io_rewind(io, &md->disk->bio_split);
1061 __noflush_suspending(io->md))) {
1077 * count on 'md'. But _not_ imposing verification to avoid atomic_read(),
1079 static inline struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
1081 return &md->queue->limits;
1084 void disable_discard(struct mapped_device *md)
1086 struct queue_limits *limits = dm_get_queue_limits(md);
1092 void disable_write_zeroes(struct mapped_device *md)
1094 struct queue_limits *limits = dm_get_queue_limits(md);
1112 struct mapped_device *md = io->md;
1117 disable_discard(md);
1120 disable_write_zeroes(md);
1138 if (WARN_ON_ONCE(dm_is_zone_write(md, bio)))
1158 up(&md->swap_bios_semaphore);
1190 min(max_sectors ? : queue_max_sectors(ti->table->md->queue),
1214 static struct dm_target *dm_dax_get_live_target(struct mapped_device *md,
1216 __acquires(md->io_barrier)
1221 map = dm_get_live_table(md, srcu_idx);
1236 struct mapped_device *md = dax_get_private(dax_dev);
1242 ti = dm_dax_get_live_target(md, sector, &srcu_idx);
1255 dm_put_live_table(md, srcu_idx);
1263 struct mapped_device *md = dax_get_private(dax_dev);
1269 ti = dm_dax_get_live_target(md, sector, &srcu_idx);
1282 dm_put_live_table(md, srcu_idx);
1290 struct mapped_device *md = dax_get_private(dax_dev);
1296 ti = dm_dax_get_live_target(md, sector, &srcu_idx);
1302 dm_put_live_table(md, srcu_idx);
1385 trace_block_bio_remap(tgt_clone, disk_devt(io->md->disk),
1391 static noinline void __set_swap_bios_limit(struct mapped_device *md, int latch)
1393 mutex_lock(&md->swap_bios_lock);
1394 while (latch < md->swap_bios) {
1396 down(&md->swap_bios_semaphore);
1397 md->swap_bios--;
1399 while (latch > md->swap_bios) {
1401 up(&md->swap_bios_semaphore);
1402 md->swap_bios++;
1404 mutex_unlock(&md->swap_bios_lock);
1412 struct mapped_device *md = io->md;
1426 if (unlikely(latch != md->swap_bios))
1427 __set_swap_bios_limit(md, latch);
1428 down(&md->swap_bios_semaphore);
1437 if (unlikely(dm_emulate_zone_append(md)))
1464 up(&md->swap_bios_semaphore);
1503 mutex_lock(&ci->io->md->table_devices_lock);
1513 mutex_unlock(&ci->io->md->table_devices_lock);
1562 bio_init(&flush_bio, ci->io->md->disk->part0, NULL, 0,
1636 struct queue_limits *limits = dm_get_queue_limits(ti->table->md);
1780 static void dm_split_and_process_bio(struct mapped_device *md,
1801 io = alloc_io(md, bio, GFP_NOWAIT);
1808 io = alloc_io(md, bio, GFP_NOIO);
1851 struct mapped_device *md = bio->bi_bdev->bd_disk->private_data;
1855 map = dm_get_live_table(md, &srcu_idx);
1858 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) ||
1865 queue_io(md, bio);
1869 dm_split_and_process_bio(md, map, bio);
1871 dm_put_live_table(md, srcu_idx);
2006 static void cleanup_mapped_device(struct mapped_device *md)
2008 if (md->wq)
2009 destroy_workqueue(md->wq);
2010 dm_free_md_mempools(md->mempools);
2012 if (md->dax_dev) {
2013 dax_remove_host(md->disk);
2014 kill_dax(md->dax_dev);
2015 put_dax(md->dax_dev);
2016 md->dax_dev = NULL;
2019 dm_cleanup_zoned_dev(md);
2020 if (md->disk) {
2022 md->disk->private_data = NULL;
2024 if (dm_get_md_type(md) != DM_TYPE_NONE) {
2027 dm_sysfs_exit(md);
2028 list_for_each_entry(td, &md->table_devices, list) {
2030 md->disk);
2037 mutex_lock(&md->table_devices_lock);
2038 del_gendisk(md->disk);
2039 mutex_unlock(&md->table_devices_lock);
2041 dm_queue_destroy_crypto_profile(md->queue);
2042 put_disk(md->disk);
2045 if (md->pending_io) {
2046 free_percpu(md->pending_io);
2047 md->pending_io = NULL;
2050 cleanup_srcu_struct(&md->io_barrier);
2052 mutex_destroy(&md->suspend_lock);
2053 mutex_destroy(&md->type_lock);
2054 mutex_destroy(&md->table_devices_lock);
2055 mutex_destroy(&md->swap_bios_lock);
2057 dm_mq_cleanup_mapped_device(md);
2067 struct mapped_device *md;
2070 md = kvzalloc_node(sizeof(*md), GFP_KERNEL, numa_node_id);
2071 if (!md) {
2087 r = init_srcu_struct(&md->io_barrier);
2091 md->numa_node_id = numa_node_id;
2092 md->init_tio_pdu = false;
2093 md->type = DM_TYPE_NONE;
2094 mutex_init(&md->suspend_lock);
2095 mutex_init(&md->type_lock);
2096 mutex_init(&md->table_devices_lock);
2097 spin_lock_init(&md->deferred_lock);
2098 atomic_set(&md->holders, 1);
2099 atomic_set(&md->open_count, 0);
2100 atomic_set(&md->event_nr, 0);
2101 atomic_set(&md->uevent_seq, 0);
2102 INIT_LIST_HEAD(&md->uevent_list);
2103 INIT_LIST_HEAD(&md->table_devices);
2104 spin_lock_init(&md->uevent_lock);
2107 * default to bio-based until DM table is loaded and md->type
2111 md->disk = blk_alloc_disk(NULL, md->numa_node_id);
2112 if (IS_ERR(md->disk))
2114 md->queue = md->disk->queue;
2116 init_waitqueue_head(&md->wait);
2117 INIT_WORK(&md->work, dm_wq_work);
2118 INIT_WORK(&md->requeue_work, dm_wq_requeue_work);
2119 init_waitqueue_head(&md->eventq);
2120 init_completion(&md->kobj_holder.completion);
2122 md->requeue_list = NULL;
2123 md->swap_bios = get_swap_bios();
2124 sema_init(&md->swap_bios_semaphore, md->swap_bios);
2125 mutex_init(&md->swap_bios_lock);
2127 md->disk->major = _major;
2128 md->disk->first_minor = minor;
2129 md->disk->minors = 1;
2130 md->disk->flags |= GENHD_FL_NO_PART;
2131 md->disk->fops = &dm_blk_dops;
2132 md->disk->private_data = md;
2133 sprintf(md->disk->disk_name, "dm-%d", minor);
2135 dax_dev = alloc_dax(md, &dm_dax_ops);
2142 md->dax_dev = dax_dev;
2143 if (dax_add_host(dax_dev, md->disk))
2147 format_dev_t(md->name, MKDEV(_major, minor));
2149 md->wq = alloc_workqueue("kdmflush/%s", WQ_MEM_RECLAIM, 0, md->name);
2150 if (!md->wq)
2153 md->pending_io = alloc_percpu(unsigned long);
2154 if (!md->pending_io)
2157 r = dm_stats_init(&md->stats);
2163 old_md = idr_replace(&_minor_idr, md, minor);
2168 return md;
2171 cleanup_mapped_device(md);
2177 kvfree(md);
2181 static void unlock_fs(struct mapped_device *md);
2183 static void free_dev(struct mapped_device *md)
2185 int minor = MINOR(disk_devt(md->disk));
2187 unlock_fs(md);
2189 cleanup_mapped_device(md);
2191 WARN_ON_ONCE(!list_empty(&md->table_devices));
2192 dm_stats_cleanup(&md->stats);
2196 kvfree(md);
2206 struct mapped_device *md = context;
2208 spin_lock_irqsave(&md->uevent_lock, flags);
2209 list_splice_init(&md->uevent_list, &uevents);
2210 spin_unlock_irqrestore(&md->uevent_lock, flags);
2212 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
2214 atomic_inc(&md->event_nr);
2215 wake_up(&md->eventq);
2222 static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2229 lockdep_assert_held(&md->suspend_lock);
2236 if (size != dm_get_size(md))
2237 memset(&md->geometry, 0, sizeof(md->geometry));
2239 set_capacity(md->disk, size);
2241 dm_table_event_callback(t, event_callback, md);
2248 md->immutable_target = dm_table_get_immutable_target(t);
2258 if (!md->mempools) {
2259 md->mempools = t->mempools;
2264 * The md may already have mempools that need changing.
2268 dm_free_md_mempools(md->mempools);
2269 md->mempools = t->mempools;
2273 ret = dm_table_set_restrictions(t, md->queue, limits);
2279 old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2280 rcu_assign_pointer(md->map, (void *)t);
2281 md->immutable_target_type = dm_table_get_immutable_target_type(t);
2284 dm_sync_table(md);
2292 static struct dm_table *__unbind(struct mapped_device *md)
2294 struct dm_table *map = rcu_dereference_protected(md->map, 1);
2300 RCU_INIT_POINTER(md->map, NULL);
2301 dm_sync_table(md);
2311 struct mapped_device *md;
2313 md = alloc_dev(minor);
2314 if (!md)
2317 dm_ima_reset_data(md);
2319 *result = md;
2324 * Functions to manage md->type.
2325 * All are required to hold md->type_lock.
2327 void dm_lock_md_type(struct mapped_device *md)
2329 mutex_lock(&md->type_lock);
2332 void dm_unlock_md_type(struct mapped_device *md)
2334 mutex_unlock(&md->type_lock);
2337 void dm_set_md_type(struct mapped_device *md, enum dm_queue_mode type)
2339 BUG_ON(!mutex_is_locked(&md->type_lock));
2340 md->type = type;
2343 enum dm_queue_mode dm_get_md_type(struct mapped_device *md)
2345 return md->type;
2348 struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
2350 return md->immutable_target_type;
2354 * Setup the DM device's queue based on md's type
2356 int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t)
2365 md->disk->fops = &dm_rq_blk_dops;
2366 r = dm_mq_init_request_queue(md, t);
2374 blk_queue_flag_set(QUEUE_FLAG_IO_STAT, md->queue);
2386 r = dm_table_set_restrictions(t, md->queue, &limits);
2394 mutex_lock(&md->table_devices_lock);
2395 r = add_disk(md->disk);
2396 mutex_unlock(&md->table_devices_lock);
2404 list_for_each_entry(td, &md->table_devices, list) {
2405 r = bd_link_disk_holder(td->dm_dev.bdev, md->disk);
2410 r = dm_sysfs_init(md);
2414 md->type = type;
2418 list_for_each_entry_continue_reverse(td, &md->table_devices, list)
2419 bd_unlink_disk_holder(td->dm_dev.bdev, md->disk);
2420 mutex_lock(&md->table_devices_lock);
2421 del_gendisk(md->disk);
2422 mutex_unlock(&md->table_devices_lock);
2428 struct mapped_device *md;
2436 md = idr_find(&_minor_idr, minor);
2437 if (!md || md == MINOR_ALLOCED || (MINOR(disk_devt(dm_disk(md))) != minor) ||
2438 test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) {
2439 md = NULL;
2442 dm_get(md);
2446 return md;
2450 void *dm_get_mdptr(struct mapped_device *md)
2452 return md->interface_ptr;
2455 void dm_set_mdptr(struct mapped_device *md, void *ptr)
2457 md->interface_ptr = ptr;
2460 void dm_get(struct mapped_device *md)
2462 atomic_inc(&md->holders);
2463 BUG_ON(test_bit(DMF_FREEING, &md->flags));
2466 int dm_hold(struct mapped_device *md)
2469 if (test_bit(DMF_FREEING, &md->flags)) {
2473 dm_get(md);
2479 const char *dm_device_name(struct mapped_device *md)
2481 return md->name;
2485 static void __dm_destroy(struct mapped_device *md, bool wait)
2493 idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2494 set_bit(DMF_FREEING, &md->flags);
2497 blk_mark_disk_dead(md->disk);
2503 mutex_lock(&md->suspend_lock);
2504 map = dm_get_live_table(md, &srcu_idx);
2505 if (!dm_suspended_md(md)) {
2507 set_bit(DMF_SUSPENDED, &md->flags);
2508 set_bit(DMF_POST_SUSPENDING, &md->flags);
2512 dm_put_live_table(md, srcu_idx);
2513 mutex_unlock(&md->suspend_lock);
2522 while (atomic_read(&md->holders))
2524 else if (atomic_read(&md->holders))
2526 dm_device_name(md), atomic_read(&md->holders));
2528 dm_table_destroy(__unbind(md));
2529 free_dev(md);
2532 void dm_destroy(struct mapped_device *md)
2534 __dm_destroy(md, true);
2537 void dm_destroy_immediate(struct mapped_device *md)
2539 __dm_destroy(md, false);
2542 void dm_put(struct mapped_device *md)
2544 atomic_dec(&md->holders);
2548 static bool dm_in_flight_bios(struct mapped_device *md)
2554 sum += *per_cpu_ptr(md->pending_io, cpu);
2559 static int dm_wait_for_bios_completion(struct mapped_device *md, unsigned int task_state)
2565 prepare_to_wait(&md->wait, &wait, task_state);
2567 if (!dm_in_flight_bios(md))
2577 finish_wait(&md->wait, &wait);
2584 static int dm_wait_for_completion(struct mapped_device *md, unsigned int task_state)
2588 if (!queue_is_mq(md->queue))
2589 return dm_wait_for_bios_completion(md, task_state);
2592 if (!blk_mq_queue_inflight(md->queue))
2611 struct mapped_device *md = container_of(work, struct mapped_device, work);
2614 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
2615 spin_lock_irq(&md->deferred_lock);
2616 bio = bio_list_pop(&md->deferred);
2617 spin_unlock_irq(&md->deferred_lock);
2627 static void dm_queue_flush(struct mapped_device *md)
2629 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2631 queue_work(md->wq, &md->work);
2637 struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
2643 mutex_lock(&md->suspend_lock);
2646 if (!dm_suspended_md(md))
2656 live_map = dm_get_live_table_fast(md);
2658 limits = md->queue->limits;
2659 dm_put_live_table_fast(md);
2670 map = __bind(md, table, &limits);
2674 mutex_unlock(&md->suspend_lock);
2682 static int lock_fs(struct mapped_device *md)
2686 WARN_ON(test_bit(DMF_FROZEN, &md->flags));
2688 r = bdev_freeze(md->disk->part0);
2690 set_bit(DMF_FROZEN, &md->flags);
2694 static void unlock_fs(struct mapped_device *md)
2696 if (!test_bit(DMF_FROZEN, &md->flags))
2698 bdev_thaw(md->disk->part0);
2699 clear_bit(DMF_FROZEN, &md->flags);
2709 * are being added to md->deferred list.
2711 static int __dm_suspend(struct mapped_device *md, struct dm_table *map,
2719 lockdep_assert_held(&md->suspend_lock);
2726 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
2728 DMDEBUG("%s: suspending with flush", dm_device_name(md));
2743 r = lock_fs(md);
2759 * flush_workqueue(md->wq).
2761 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2763 synchronize_srcu(&md->io_barrier);
2766 * Stop md->queue before flushing md->wq in case request-based
2767 * dm defers requests to md->wq from md->queue.
2769 if (dm_request_based(md))
2770 dm_stop_queue(md->queue);
2772 flush_workqueue(md->wq);
2779 r = dm_wait_for_completion(md, task_state);
2781 set_bit(dmf_suspended_flag, &md->flags);
2784 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
2786 synchronize_srcu(&md->io_barrier);
2790 dm_queue_flush(md);
2792 if (dm_request_based(md))
2793 dm_start_queue(md->queue);
2795 unlock_fs(md);
2819 int dm_suspend(struct mapped_device *md, unsigned int suspend_flags)
2825 mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
2827 if (dm_suspended_md(md)) {
2832 if (dm_suspended_internally_md(md)) {
2834 mutex_unlock(&md->suspend_lock);
2835 r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
2841 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2847 r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE, DMF_SUSPENDED);
2851 set_bit(DMF_POST_SUSPENDING, &md->flags);
2853 clear_bit(DMF_POST_SUSPENDING, &md->flags);
2856 mutex_unlock(&md->suspend_lock);
2860 static int __dm_resume(struct mapped_device *md, struct dm_table *map)
2869 dm_queue_flush(md);
2876 if (dm_request_based(md))
2877 dm_start_queue(md->queue);
2879 unlock_fs(md);
2884 int dm_resume(struct mapped_device *md)
2891 mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
2893 if (!dm_suspended_md(md))
2896 if (dm_suspended_internally_md(md)) {
2898 mutex_unlock(&md->suspend_lock);
2899 r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
2905 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2909 r = __dm_resume(md, map);
2913 clear_bit(DMF_SUSPENDED, &md->flags);
2915 mutex_unlock(&md->suspend_lock);
2926 static void __dm_internal_suspend(struct mapped_device *md, unsigned int suspend_flags)
2930 lockdep_assert_held(&md->suspend_lock);
2932 if (md->internal_suspend_count++)
2935 if (dm_suspended_md(md)) {
2936 set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2940 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2948 (void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE,
2951 set_bit(DMF_POST_SUSPENDING, &md->flags);
2953 clear_bit(DMF_POST_SUSPENDING, &md->flags);
2956 static void __dm_internal_resume(struct mapped_device *md)
2961 BUG_ON(!md->internal_suspend_count);
2963 if (--md->internal_suspend_count)
2966 if (dm_suspended_md(md))
2969 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2970 r = __dm_resume(md, map);
2984 set_bit(DMF_SUSPENDED, &md->flags);
2987 clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2989 wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY);
2992 void dm_internal_suspend_noflush(struct mapped_device *md)
2994 mutex_lock(&md->suspend_lock);
2995 __dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG);
2996 mutex_unlock(&md->suspend_lock);
3000 void dm_internal_resume(struct mapped_device *md)
3002 mutex_lock(&md->suspend_lock);
3003 __dm_internal_resume(md);
3004 mutex_unlock(&md->suspend_lock);
3009 * Fast variants of internal suspend/resume hold md->suspend_lock,
3013 void dm_internal_suspend_fast(struct mapped_device *md)
3015 mutex_lock(&md->suspend_lock);
3016 if (dm_suspended_md(md) || dm_suspended_internally_md(md))
3019 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
3020 synchronize_srcu(&md->io_barrier);
3021 flush_workqueue(md->wq);
3022 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
3026 void dm_internal_resume_fast(struct mapped_device *md)
3028 if (dm_suspended_md(md) || dm_suspended_internally_md(md))
3031 dm_queue_flush(md);
3034 mutex_unlock(&md->suspend_lock);
3043 int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
3062 r = kobject_uevent_env(&disk_to_dev(md->disk)->kobj, action, envp);
3069 uint32_t dm_next_uevent_seq(struct mapped_device *md)
3071 return atomic_add_return(1, &md->uevent_seq);
3074 uint32_t dm_get_event_nr(struct mapped_device *md)
3076 return atomic_read(&md->event_nr);
3079 int dm_wait_event(struct mapped_device *md, int event_nr)
3081 return wait_event_interruptible(md->eventq,
3082 (event_nr != atomic_read(&md->event_nr)));
3085 void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
3089 spin_lock_irqsave(&md->uevent_lock, flags);
3090 list_add(elist, &md->uevent_list);
3091 spin_unlock_irqrestore(&md->uevent_lock, flags);
3096 * count on 'md'.
3098 struct gendisk *dm_disk(struct mapped_device *md)
3100 return md->disk;
3104 struct kobject *dm_kobject(struct mapped_device *md)
3106 return &md->kobj_holder.kobj;
3111 struct mapped_device *md;
3113 md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
3116 if (test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) {
3117 md = NULL;
3120 dm_get(md);
3124 return md;
3127 int dm_suspended_md(struct mapped_device *md)
3129 return test_bit(DMF_SUSPENDED, &md->flags);
3132 static int dm_post_suspending_md(struct mapped_device *md)
3134 return test_bit(DMF_POST_SUSPENDING, &md->flags);
3137 int dm_suspended_internally_md(struct mapped_device *md)
3139 return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3142 int dm_test_deferred_remove_flag(struct mapped_device *md)
3144 return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
3149 return dm_suspended_md(ti->table->md);
3155 return dm_post_suspending_md(ti->table->md);
3161 return __noflush_suspending(ti->table->md);
3191 struct mapped_device *md = bdev->bd_disk->private_data;
3196 table = dm_get_live_table(md, &srcu_idx);
3205 if (dm_suspended_md(md)) {
3217 dm_put_live_table(md, srcu_idx);
3400 struct mapped_device *md = bdev->bd_disk->private_data;
3404 r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
3414 dm_unprepare_ioctl(md, srcu_idx);