Deactivating volume with mounted snapshot
Hi
When you type
lvchange -ay volume, it activates the volume and all its snapshots
Now you mount one of the snapshots. And then, type:
lvchange -an volume
--- the result is, that the volume and all the snapshots except the mounted one are deactivated. The mounted snapshot is still active.
It looks like a bug to me --- it was never indented to have some snapshots active and some not. With snapshot merging, this would cause data corruption on inactive snapshots (because they won't allocate new exceptions). Is it OK to disable this condition?
Mikulas
Quoted 7 lines · Mikulas Patocka
Now you mount one of the snapshots. And then, type:
lvchange -an volume
--- the result is, that the volume and all the snapshots except the mounted one are deactivated. The mounted snapshot is still active.It looks like a bug to me --- it was never indented to have some snapshots active and some not.
This does *not* of course lead to corruption: it's a constraint we chose to have LVM2 impose, not device-mapper.
It's not a perfect constraint as you discovered because it would be impossible to prevent this from ever happening in our current model.
The code "does its best" to do what you asked. Sure, we could try to add more checks before deactivating and make the process more robust, but I'd draw the line at attempting to reactivate already-deactivated devices in the event of a failure part way through.
Alasdair
Signature · 1 line
agk at redhat.com
Quoted 11 lines · Alasdair G Kergon
Quoted 7 lines · Mikulas Patocka
Now you mount one of the snapshots. And then, type:
lvchange -an volume
--- the result is, that the volume and all the snapshots except the mounted one are deactivated. The mounted snapshot is still active.It looks like a bug to me --- it was never indented to have some snapshots active and some not.
This does *not* of course lead to corruption: it's a constraint we chose to have LVM2 impose, not device-mapper.
With merging, it will cause corruption --- if one snapshot is merging and you don't have all the other snapshots loaded, the non-active snapshots won't be updated as the merging goes on. So they'll be corrupted.
Quoted 2 lines
It's not a perfect constraint as you discovered because it would be impossible to prevent this from ever happening in our current model.
Currently the contstraint is "if the device is open, you can't deactivate it". I'd like change it to "if the device is open or some of its snapshots are open, you can't deactivate it".
Even without merging, the current approach is not perfect, as you will end up with a device that you can't deactivate (the origin is deactivated, the snapshot is active; lvchange -an on the snapshot will return error; lvchange -an on the origin will see that it is already inactive and do nothing)
Quoted 8 lines
The code "does its best" to do what you asked. Sure, we could try to add more checks before deactivating and make the process more robust, but I'd draw the line at attempting to reactivate already-deactivated devices in the event of a failure part way through.
Alasdair
Signature · 1 line
agk at redhat.com
Mikulas
Quoted 3 lines · Mikulas Patocka
With merging, it will cause corruption --- if one snapshot is merging and you don't have all the other snapshots loaded, the non-active snapshots won't be updated as the merging goes on. So they'll be corrupted.
If that can happen it suggests the detailed set of activation steps needs further refining and is not correct yet.
Quoted 3 lines
Currently the contstraint is "if the device is open, you can't deactivate it". I'd like change it to "if the device is open or some of its snapshots are open, you can't deactivate it".
Catch-22 then. Which do you deactivate first? We cannot handle that at kernel level with the current interface.
But by all means add that extra protection to the userspace code. The userspace code should not assume it happened though.
Quoted 5 lines
Even without merging, the current approach is not perfect, as you will end up with a device that you can't deactivate (the origin is deactivated, the snapshot is active; lvchange -an on the snapshot will return error; lvchange -an on the origin will see that it is already inactive and do nothing)
That can be fixed: lvchange -an on the origin should complete the process. (It probably used to do that before some optimisation got added.)
Alasdair
Signature · 1 line
agk at redhat.com
Quoted 7 lines · Alasdair G Kergon
Quoted 3 lines · Mikulas Patocka
With merging, it will cause corruption --- if one snapshot is merging and you don't have all the other snapshots loaded, the non-active snapshots won't be updated as the merging goes on. So they'll be corrupted.
If that can happen it suggests the detailed set of activation steps needs further refining and is not correct yet.
I think I have it already correct. The patch that corrects it is
http://lacrosse.redhat.com/~mpatocka/patches/kernel/dm-snapshot-remove-active-variable.patch
Basically, lvm suspends all the devices, then loads the dm table for all of them and then resumes them.
I start merging at preresume method. When I start merging, all the other snapshots are already loaded in the table and can allocate new exceptions. The patch changes it so that they can accept new exceptions always.
Quoted 6 lines
Quoted 3 lines
Currently the contstraint is "if the device is open, you can't deactivate it". I'd like change it to "if the device is open or some of its snapshots are open, you can't deactivate it".
Catch-22 then. Which do you deactivate first? We cannot handle that at kernel level with the current interface.
You are right. The possible solutions:
* block opening of suspended device until the device is resumed (or deleted) --- easiest, but most scary. Theoretically it should be OK --- you already should not open a file while there is something suspended (the memory-allocation deadlock) --- but it may make some of these deadlocks very visible. Maybe making the deadlocks visible is a good thing, it's much easier to solve reproducible deadlock than nonreproducible.
* undo the deactivation if one of the devices can't be removed because it is open --- probably the hardest.
* replace the open device's table with error target --- if it has journaled filesystem, it will recover fine. And it won't damage other snapshots.
* let it be, hoping that admin won't simultaneously open and deactivate a device :-/
Quoted 9 lines
But by all means add that extra protection to the userspace code. The userspace code should not assume it happened though.
Quoted 5 lines
Even without merging, the current approach is not perfect, as you will end up with a device that you can't deactivate (the origin is deactivated, the snapshot is active; lvchange -an on the snapshot will return error; lvchange -an on the origin will see that it is already inactive and do nothing)
Quoted 6 lines
That can be fixed: lvchange -an on the origin should complete the process. (It probably used to do that before some optimisation got added.)
Alasdair
Signature · 1 line
agk at redhat.com
BTW. Tomorrow I'm going to Brno to medical exam at Red Hat on Thursday and meeting on Friday, so I won't be on IRC and email much.
Mikulas
Quoted 2 lines · Mikulas Patocka
Basically, lvm suspends all the devices, then loads the dm table for all of them and then resumes them.
Not strictly accurate.
You need to consider each case separately:
taking existing LV and adding/removing first snapshot adding/removing a subsequent snapshot activating/deactivating LV plus pre-existing snapshots
and note the sequence of significant events in each case:
table loaded
snapshot metadata read from disk
filesystem quiesced
snapshot becoming active
etc.
Then you'd need to show that removing 'active = 1' retains correctness in every case. I am not convinced: Consider the case of adding a subsequent snapshot: preload table does mem allocations, then suspend existing LV & quiesce fs, then resume snapshot (active = 1 now) etc.
Alasdair
Signature · 1 line
agk at redhat.com
Quoted 19 lines · Alasdair G Kergon
Quoted 2 lines · Mikulas Patocka
Basically, lvm suspends all the devices, then loads the dm table for all of them and then resumes them.
Not strictly accurate.
You need to consider each case separately:
taking existing LV and adding/removing first snapshot adding/removing a subsequent snapshot activating/deactivating LV plus pre-existing snapshots
and note the sequence of significant events in each case:
table loaded
snapshot metadata read from disk
filesystem quiesced
snapshot becoming active
etc.
Quoted 8 lines
Then you'd need to show that removing 'active = 1' retains correctness in every case. I am not convinced: Consider the case of adding a subsequent snapshot: preload table does mem allocations, then suspend existing LV & quiesce fs, then resume snapshot (active = 1 now) etc.
Alasdair
Signature · 1 line
agk at redhat.com
I put a printk points at ctr/dtr/suspend/resume functions into origin and snapshot and observed their calls when activating/deactivating, adding/removing a snapshot and adding/removing the first/last snapshot.
Because "active" variable was never reset to zero, it had effect only between snapshot ctr (set to 0) and snapshot resume (set to 1). When it was set to 0, the snapshot didn't allocate exceptions. After the patch, the snapshot always allocates exceptions (between snapshot ctr and snapshot dtr calls).
The change in the patch is safe because when someone is creating snapshot and simultaneously writing to the origin device, it is unspecified, which write requests trigger reallocations in the snapshot and which not.
I found another problem --- the lvchange -an is currently safe, but it is unsafe with snapshot merging. Ideally it should suspend all the devices first and then call destructors on the snapshots. I'll look at it.
Mikulas
lvchange -ay:
origin ctr: 254:4
snapshot ctr 254:4 254:6
snapshot ctr 254:4 254:8
origin resume: 254:4 254:6
origin resume: 254:4 254:8
origin resume: 254:4
lvchange -an:
origin suspend: 254:4
origin dtr: 254:4
snapshot suspend: 254:4 254:6
snapshot dtr 254:4 254:6
snapshot suspend: 254:4 254:8
snapshot dtr 254:4 254:8
lvcreate snapshot:
snapshot ctr 254:4 254:11
origin suspend: 254:4
snapshot suspend: 254:4 254:6
snapshot suspend: 254:4 254:8
snapshot resume: 254:4 254:6
snapshot resume: 254:4 254:8
snapshot resume: 254:4 254:11
origin resume: 254:4
lvremove snapshot:
snapshot suspend: 254:4 254:11
snapshot dtr 254:4 254:11
lvcreate, adding the first snapshot:
origin ctr: 254:12
snapshot ctr 254:12 254:13
snapshot resume: 254:12 254:13
origin resume: 254:12
lvremove, removing the last snapshot:
snapshot suspend: 254:12 254:13
snapshot dtr 254:12 254:13
origin suspend: 254:12
origin dtr: 254:12
Quoted 3 lines · Mikulas Patocka
The change in the patch is safe because when someone is creating snapshot and simultaneously writing to the origin device, it is unspecified, which write requests trigger reallocations in the snapshot and which not.
That isn't relevant: What matters is that any filesystem on the origin LV is quiesced first.
Sequence is:
perform necessary memory allocations (snapshot ctr) suspend origin LV (quiesce filesystem)
resume snapshots so that changes start to be tracked (sets active = 1) resume origin (resumes filesystem)
Quoted 3 lines
I found another problem --- the lvchange -an is currently safe, but it is unsafe with snapshot merging. Ideally it should suspend all the devices first and then call destructors on the snapshots. I'll look at it.
Again, this points to the interface not being right yet.
Alasdair
Signature · 1 line
agk at redhat.com
Quoted 4 lines
I put a printk points at ctr/dtr/suspend/resume functions into origin and snapshot and observed their calls when activating/deactivating, adding/removing a snapshot and adding/removing the first/last snapshot.
Quoted 8 lines
Because "active" variable was never reset to zero, it had effect only between snapshot ctr (set to 0) and snapshot resume (set to 1). When it was set to 0, the snapshot didn't allocate exceptions. After the patch, the snapshot always allocates exceptions (between snapshot ctr and snapshot dtr calls).
The change in the patch is safe because when someone is creating snapshot and simultaneously writing to the origin device, it is unspecified, which write requests trigger reallocations in the snapshot and which not.
Milan said that there is xfs freeze called between that (it is called just before origin suspend) --- and it's fact that dropping an "active" variable would make unclean snapshot on xfs.
So I'll change it so that no exceptions are reallocated if active is zero and the exception store is empty. That will make both xfs freeze and snapshot merging safe.
Mikulas
Quoted 49 lines
I found another problem --- the lvchange -an is currently safe, but it is unsafe with snapshot merging. Ideally it should suspend all the devices first and then call destructors on the snapshots. I'll look at it.
Mikulas
lvchange -ay:
origin ctr: 254:4
snapshot ctr 254:4 254:6
snapshot ctr 254:4 254:8
origin resume: 254:4 254:6
origin resume: 254:4 254:8
origin resume: 254:4lvchange -an:
origin suspend: 254:4
origin dtr: 254:4
snapshot suspend: 254:4 254:6
snapshot dtr 254:4 254:6
snapshot suspend: 254:4 254:8
snapshot dtr 254:4 254:8lvcreate snapshot:
snapshot ctr 254:4 254:11
origin suspend: 254:4
snapshot suspend: 254:4 254:6
snapshot suspend: 254:4 254:8
snapshot resume: 254:4 254:6
snapshot resume: 254:4 254:8
snapshot resume: 254:4 254:11
origin resume: 254:4lvremove snapshot:
snapshot suspend: 254:4 254:11
snapshot dtr 254:4 254:11lvcreate, adding the first snapshot:
origin ctr: 254:12
snapshot ctr 254:12 254:13
snapshot resume: 254:12 254:13
origin resume: 254:12lvremove, removing the last snapshot:
snapshot suspend: 254:12 254:13
snapshot dtr 254:12 254:13
origin suspend: 254:12
origin dtr: 254:12
Quoted 18 lines · Mikulas Patocka
Quoted 10 lines
I put a printk points at ctr/dtr/suspend/resume functions into origin and snapshot and observed their calls when activating/deactivating, adding/removing a snapshot and adding/removing the first/last snapshot.
Because "active" variable was never reset to zero, it had effect only between snapshot ctr (set to 0) and snapshot resume (set to 1). When it was set to 0, the snapshot didn't allocate exceptions. After the patch, the snapshot always allocates exceptions (between snapshot ctr and snapshot dtr calls).
Quoted 3 lines
The change in the patch is safe because when someone is creating snapshot and simultaneously writing to the origin device, it is unspecified, which write requests trigger reallocations in the snapshot and which not.
Milan said that there is xfs freeze called between that (it is called just before origin suspend) --- and it's fact that dropping an "active" variable would make unclean snapshot on xfs.
Quoted 5 lines
So I'll change it so that no exceptions are reallocated if active is zero and the exception store is empty. That will make both xfs freeze and snapshot merging safe.
Mikulas
... or maybe the best method would be to not merge if the origin is not in resumed state. That would solve all the possible problems with interaction with userspace. It needs some changing of the structures (making the snapshots point to the origin device), but I intended to do it anyway. Calling read_lock and __lookup_origin each time something needs doesn't seem nice to me and I'd like to replace this with a pointer.
Mikulas