Skip to contentSkip to conversation

lvm: don't set snapshot cow device read only

PATCH New Mikulas Patocka 1 participant 1 message Feb 5, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lvm: don't set snapshot cow device read only

Hi

This is the patch for the bug in lvm that it creates read-only snapshot-cow device and then lets the kernel driver to write to the device.

BTW. I'd like to ask - what does this "vg->status & LVM_WRITE" flag mean and how is it controlled? It is supposed to mean that the volume group is read-only or read-write, but I don't know how is it set (there is no read-only flag in vgchange or vgconvert) and what exactly does it cause.

Mikulas

---

lvm: don't set snapshot cow device read only

lvm has a bug: when creating snapshot with "--permission r", the underlying -cow device is set read-only. Kernel than tries to write to this read-only device when creating metadata header and when processing writes to the origin. The kernel doesn't check writes to read-only device originating from kernelspace, thus this bug becomes unnoticed. However, if we modify the kernel to not allow write bios to read-only devices, this bug shows up.

This patch fixes lvm so that the underlying -cow device is always read-write.

---
 lib/activate/activate.c    |    2 +-
 lib/activate/dev_manager.c |    9 ++++++---
 lib/activate/dev_manager.h |    3 ++-
 3 files changed, 9 insertions(+), 5 deletions(-)
lib/activate/dev_manager.c +6 -3
Index: LVM2.2.02.98/lib/activate/dev_manager.c
===================================================================
@@ -61,9 +61,12 @@ struct lv_layer {
 
 static const char _thin_layer[] = "tpool";
 
-int read_only_lv(struct logical_volume *lv, struct lv_activate_opts *laopts)
+int read_only_lv(struct logical_volume *lv, struct lv_activate_opts *laopts,
+		 const char *layer)
 {
-	return (laopts->read_only || !(lv->vg->status & LVM_WRITE) || !(lv->status & LVM_WRITE));
+	if (!(lv->vg->status & LVM_WRITE)) return 1;
+	if (layer && !strcmp(layer, "cow")) return 0;
+	return laopts->read_only || !(lv->status & LVM_WRITE);
 }
 
 /*
@@ -1968,7 +1971,7 @@ static int _add_new_lv_to_dtree(struct d
 	if (!(dnode = dm_tree_add_new_dev_with_udev_flags(dtree, name, dlid,
 					     layer ? UINT32_C(0) : (uint32_t) lv->major,
 					     layer ? UINT32_C(0) : (uint32_t) lv->minor,
-					     read_only_lv(lv, laopts),
+					     read_only_lv(lv, laopts, layer),
 					     ((lv->vg->status & PRECOMMITTED) | laopts->revert) ? 1 : 0,
 					     lvlayer,
 					     _get_udev_flags(dm, lv, layer))))
lib/activate/activate.c +1 -1
Index: LVM2.2.02.98/lib/activate/activate.c
===================================================================
@@ -1857,7 +1857,7 @@ static int _lv_activate(struct cmd_conte
 	 * Nothing to do?
 	 */
 	if (info.exists && !info.suspended && info.live_table &&
-	    (info.read_only == read_only_lv(lv, laopts))) {
+	    (info.read_only == read_only_lv(lv, laopts, NULL))) {
 		r = 1;
 		goto out;
 	}
lib/activate/dev_manager.h +2 -1
Index: LVM2.2.02.98/lib/activate/dev_manager.h
===================================================================
@@ -26,7 +26,8 @@ struct dev_manager;
 struct dm_info;
 struct device;
 
-int read_only_lv(struct logical_volume *lv, struct lv_activate_opts *laopts);
+int read_only_lv(struct logical_volume *lv, struct lv_activate_opts *laopts,
+		 const char *layer);
 
 /*
  * Constructor and destructor.