Skip to content

Commit 2c140a2

Browse files
Mikulas Patockasnitm
authored andcommitted
dm: allow remove to be deferred
This patch allows the removal of an open device to be deferred until it is closed. (Previously such a removal attempt would fail.) The deferred remove functionality is enabled by setting the flag DM_DEFERRED_REMOVE in the ioctl structure on DM_DEV_REMOVE or DM_REMOVE_ALL ioctl. On return from DM_DEV_REMOVE, the flag DM_DEFERRED_REMOVE indicates if the device was removed immediately or flagged to be removed on close - if the flag is clear, the device was removed. On return from DM_DEV_STATUS and other ioctls, the flag DM_DEFERRED_REMOVE is set if the device is scheduled to be removed on closure. A device that is scheduled to be deleted can be revived using the message "@cancel_deferred_remove". This message clears the DMF_DEFERRED_REMOVE flag so that the device won't be deleted on close. Signed-off-by: Mikulas Patocka <mpatocka@redhat.com> Signed-off-by: Alasdair G Kergon <agk@redhat.com> Signed-off-by: Mike Snitzer <snitzer@redhat.com>
1 parent 7833b08 commit 2c140a2

4 files changed

Lines changed: 99 additions & 12 deletions

File tree

‎drivers/md/dm-ioctl.c‎

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ struct vers_iter {
5757
static struct list_head _name_buckets[NUM_BUCKETS];
5858
static struct list_head _uuid_buckets[NUM_BUCKETS];
5959

60-
static void dm_hash_remove_all(int keep_open_devices);
60+
static void dm_hash_remove_all(bool keep_open_devices, bool mark_deferred, bool only_deferred);
6161

6262
/*
6363
* Guards access to both hash tables.
@@ -86,7 +86,7 @@ static int dm_hash_init(void)
8686

8787
static void dm_hash_exit(void)
8888
{
89-
dm_hash_remove_all(0);
89+
dm_hash_remove_all(false, false, false);
9090
}
9191

9292
/*-----------------------------------------------------------------
@@ -276,7 +276,7 @@ static struct dm_table *__hash_remove(struct hash_cell *hc)
276276
return table;
277277
}
278278

279-
static void dm_hash_remove_all(int keep_open_devices)
279+
static void dm_hash_remove_all(bool keep_open_devices, bool mark_deferred, bool only_deferred)
280280
{
281281
int i, dev_skipped;
282282
struct hash_cell *hc;
@@ -293,7 +293,8 @@ static void dm_hash_remove_all(int keep_open_devices)
293293
md = hc->md;
294294
dm_get(md);
295295

296-
if (keep_open_devices && dm_lock_for_deletion(md)) {
296+
if (keep_open_devices &&
297+
dm_lock_for_deletion(md, mark_deferred, only_deferred)) {
297298
dm_put(md);
298299
dev_skipped++;
299300
continue;
@@ -450,6 +451,11 @@ static struct mapped_device *dm_hash_rename(struct dm_ioctl *param,
450451
return md;
451452
}
452453

454+
void dm_deferred_remove(void)
455+
{
456+
dm_hash_remove_all(true, false, true);
457+
}
458+
453459
/*-----------------------------------------------------------------
454460
* Implementation of the ioctl commands
455461
*---------------------------------------------------------------*/
@@ -461,7 +467,7 @@ typedef int (*ioctl_fn)(struct dm_ioctl *param, size_t param_size);
461467

462468
static int remove_all(struct dm_ioctl *param, size_t param_size)
463469
{
464-
dm_hash_remove_all(1);
470+
dm_hash_remove_all(true, !!(param->flags & DM_DEFERRED_REMOVE), false);
465471
param->data_size = 0;
466472
return 0;
467473
}
@@ -683,6 +689,9 @@ static void __dev_status(struct mapped_device *md, struct dm_ioctl *param)
683689
if (dm_suspended_md(md))
684690
param->flags |= DM_SUSPEND_FLAG;
685691

692+
if (dm_test_deferred_remove_flag(md))
693+
param->flags |= DM_DEFERRED_REMOVE;
694+
686695
param->dev = huge_encode_dev(disk_devt(disk));
687696

688697
/*
@@ -832,8 +841,13 @@ static int dev_remove(struct dm_ioctl *param, size_t param_size)
832841
/*
833842
* Ensure the device is not open and nothing further can open it.
834843
*/
835-
r = dm_lock_for_deletion(md);
844+
r = dm_lock_for_deletion(md, !!(param->flags & DM_DEFERRED_REMOVE), false);
836845
if (r) {
846+
if (r == -EBUSY && param->flags & DM_DEFERRED_REMOVE) {
847+
up_write(&_hash_lock);
848+
dm_put(md);
849+
return 0;
850+
}
837851
DMDEBUG_LIMIT("unable to remove open device %s", hc->name);
838852
up_write(&_hash_lock);
839853
dm_put(md);
@@ -848,6 +862,8 @@ static int dev_remove(struct dm_ioctl *param, size_t param_size)
848862
dm_table_destroy(t);
849863
}
850864

865+
param->flags &= ~DM_DEFERRED_REMOVE;
866+
851867
if (!dm_kobject_uevent(md, KOBJ_REMOVE, param->event_nr))
852868
param->flags |= DM_UEVENT_GENERATED_FLAG;
853869

@@ -1469,6 +1485,14 @@ static int message_for_md(struct mapped_device *md, unsigned argc, char **argv,
14691485
if (**argv != '@')
14701486
return 2; /* no '@' prefix, deliver to target */
14711487

1488+
if (!strcasecmp(argv[0], "@cancel_deferred_remove")) {
1489+
if (argc != 1) {
1490+
DMERR("Invalid arguments for @cancel_deferred_remove");
1491+
return -EINVAL;
1492+
}
1493+
return dm_cancel_deferred_remove(md);
1494+
}
1495+
14721496
r = dm_stats_message(md, argc, argv, result, maxlen);
14731497
if (r < 2)
14741498
return r;

‎drivers/md/dm.c‎

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ static unsigned int _major = 0;
4949
static DEFINE_IDR(_minor_idr);
5050

5151
static DEFINE_SPINLOCK(_minor_lock);
52+
53+
static void do_deferred_remove(struct work_struct *w);
54+
55+
static DECLARE_WORK(deferred_remove_work, do_deferred_remove);
56+
5257
/*
5358
* For bio-based dm.
5459
* One of these is allocated per bio.
@@ -116,6 +121,7 @@ EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo);
116121
#define DMF_DELETING 4
117122
#define DMF_NOFLUSH_SUSPENDING 5
118123
#define DMF_MERGE_IS_OPTIONAL 6
124+
#define DMF_DEFERRED_REMOVE 7
119125

120126
/*
121127
* A dummy definition to make RCU happy.
@@ -299,6 +305,8 @@ static int __init local_init(void)
299305

300306
static void local_exit(void)
301307
{
308+
flush_scheduled_work();
309+
302310
kmem_cache_destroy(_rq_tio_cache);
303311
kmem_cache_destroy(_io_cache);
304312
unregister_blkdev(_major, _name);
@@ -404,7 +412,10 @@ static void dm_blk_close(struct gendisk *disk, fmode_t mode)
404412

405413
spin_lock(&_minor_lock);
406414

407-
atomic_dec(&md->open_count);
415+
if (atomic_dec_and_test(&md->open_count) &&
416+
(test_bit(DMF_DEFERRED_REMOVE, &md->flags)))
417+
schedule_work(&deferred_remove_work);
418+
408419
dm_put(md);
409420

410421
spin_unlock(&_minor_lock);
@@ -418,14 +429,18 @@ int dm_open_count(struct mapped_device *md)
418429
/*
419430
* Guarantees nothing is using the device before it's deleted.
420431
*/
421-
int dm_lock_for_deletion(struct mapped_device *md)
432+
int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred)
422433
{
423434
int r = 0;
424435

425436
spin_lock(&_minor_lock);
426437

427-
if (dm_open_count(md))
438+
if (dm_open_count(md)) {
428439
r = -EBUSY;
440+
if (mark_deferred)
441+
set_bit(DMF_DEFERRED_REMOVE, &md->flags);
442+
} else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags))
443+
r = -EEXIST;
429444
else
430445
set_bit(DMF_DELETING, &md->flags);
431446

@@ -434,6 +449,27 @@ int dm_lock_for_deletion(struct mapped_device *md)
434449
return r;
435450
}
436451

452+
int dm_cancel_deferred_remove(struct mapped_device *md)
453+
{
454+
int r = 0;
455+
456+
spin_lock(&_minor_lock);
457+
458+
if (test_bit(DMF_DELETING, &md->flags))
459+
r = -EBUSY;
460+
else
461+
clear_bit(DMF_DEFERRED_REMOVE, &md->flags);
462+
463+
spin_unlock(&_minor_lock);
464+
465+
return r;
466+
}
467+
468+
static void do_deferred_remove(struct work_struct *w)
469+
{
470+
dm_deferred_remove();
471+
}
472+
437473
sector_t dm_get_size(struct mapped_device *md)
438474
{
439475
return get_capacity(md->disk);
@@ -2894,6 +2930,11 @@ int dm_suspended_md(struct mapped_device *md)
28942930
return test_bit(DMF_SUSPENDED, &md->flags);
28952931
}
28962932

2933+
int dm_test_deferred_remove_flag(struct mapped_device *md)
2934+
{
2935+
return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
2936+
}
2937+
28972938
int dm_suspended(struct dm_target *ti)
28982939
{
28992940
return dm_suspended_md(dm_table_get_md(ti->table));

‎drivers/md/dm.h‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,16 @@ int dm_deleting_md(struct mapped_device *md);
128128
*/
129129
int dm_suspended_md(struct mapped_device *md);
130130

131+
/*
132+
* Test if the device is scheduled for deferred remove.
133+
*/
134+
int dm_test_deferred_remove_flag(struct mapped_device *md);
135+
136+
/*
137+
* Try to remove devices marked for deferred removal.
138+
*/
139+
void dm_deferred_remove(void);
140+
131141
/*
132142
* The device-mapper can be driven through one of two interfaces;
133143
* ioctl or filesystem, depending which patch you have applied.
@@ -158,7 +168,8 @@ void dm_stripe_exit(void);
158168
void dm_destroy(struct mapped_device *md);
159169
void dm_destroy_immediate(struct mapped_device *md);
160170
int dm_open_count(struct mapped_device *md);
161-
int dm_lock_for_deletion(struct mapped_device *md);
171+
int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred);
172+
int dm_cancel_deferred_remove(struct mapped_device *md);
162173
int dm_request_based(struct mapped_device *md);
163174
sector_t dm_get_size(struct mapped_device *md);
164175
struct dm_stats *dm_get_stats(struct mapped_device *md);

‎include/uapi/linux/dm-ioctl.h‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,9 @@ enum {
267267
#define DM_DEV_SET_GEOMETRY _IOWR(DM_IOCTL, DM_DEV_SET_GEOMETRY_CMD, struct dm_ioctl)
268268

269269
#define DM_VERSION_MAJOR 4
270-
#define DM_VERSION_MINOR 26
270+
#define DM_VERSION_MINOR 27
271271
#define DM_VERSION_PATCHLEVEL 0
272-
#define DM_VERSION_EXTRA "-ioctl (2013-08-15)"
272+
#define DM_VERSION_EXTRA "-ioctl (2013-10-30)"
273273

274274
/* Status bits */
275275
#define DM_READONLY_FLAG (1 << 0) /* In/Out */
@@ -341,4 +341,15 @@ enum {
341341
*/
342342
#define DM_DATA_OUT_FLAG (1 << 16) /* Out */
343343

344+
/*
345+
* If set with DM_DEV_REMOVE or DM_REMOVE_ALL this indicates that if
346+
* the device cannot be removed immediately because it is still in use
347+
* it should instead be scheduled for removal when it gets closed.
348+
*
349+
* On return from DM_DEV_REMOVE, DM_DEV_STATUS or other ioctls, this
350+
* flag indicates that the device is scheduled to be removed when it
351+
* gets closed.
352+
*/
353+
#define DM_DEFERRED_REMOVE (1 << 17) /* In/Out */
354+
344355
#endif /* _LINUX_DM_IOCTL_H */

0 commit comments

Comments
 (0)