Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 1 | From 8eb02bcb5897b238b29ff762402bb0c3028f0eab Mon Sep 17 00:00:00 2001 |
| 2 | From: Michael Chang <mchang@suse.com> |
| 3 | Date: Thu, 19 Mar 2020 13:56:13 +0800 |
| 4 | Subject: [PATCH 3/9] lvm: Add LVM cache logical volume handling |
| 5 | |
| 6 | The LVM cache logical volume is the logical volume consisting of the original |
| 7 | and the cache pool logical volume. The original is usually on a larger and |
| 8 | slower storage device while the cache pool is on a smaller and faster one. The |
| 9 | performance of the original volume can be improved by storing the frequently |
| 10 | used data on the cache pool to utilize the greater performance of faster |
| 11 | device. |
| 12 | |
| 13 | The default cache mode "writethrough" ensures that any data written will be |
| 14 | stored both in the cache and on the origin LV, therefore grub can be straight |
| 15 | to read the original lv as no data loss is guarenteed. |
| 16 | |
| 17 | The second cache mode is "writeback", which delays writing from the cache pool |
| 18 | back to the origin LV to have increased performance. The drawback is potential |
| 19 | data loss if losing the associated cache device. |
| 20 | |
| 21 | During the boot time grub reads the LVM offline i.e. LVM volumes are not |
| 22 | activated and mounted, hence it should be fine to read directly from original |
| 23 | lv since all cached data should have been flushed back in the process of taking |
| 24 | it offline. |
| 25 | |
| 26 | It is also not much helpful to the situation by adding fsync calls to the |
| 27 | install code. The fsync did not force to write back dirty cache to the original |
| 28 | device and rather it would update associated cache metadata to complete the |
| 29 | write transaction with the cache device. IOW the writes to cached blocks still |
| 30 | go only to the cache device. |
| 31 | |
| 32 | To write back dirty cache, as LVM cache did not support dirty cache flush per |
| 33 | block range, there'no way to do it for file. On the other hand the "cleaner" |
| 34 | policy is implemented and can be used to write back "all" dirty blocks in a |
| 35 | cache, which effectively drain all dirty cache gradually to attain and last in |
| 36 | the "clean" state, which can be useful for shrinking or decommissioning a |
| 37 | cache. The result and effect is not what we are looking for here. |
| 38 | |
| 39 | In conclusion, as it seems no way to enforce file writes to the original |
| 40 | device, grub may suffer from power failure as it cannot assemble the cache |
| 41 | device and read the dirty data from it. However since the case is only |
| 42 | applicable to writeback mode which is sensitive to data lost in nature, I'd |
| 43 | still like to propose my (relatively simple) patch and treat reading dirty |
| 44 | cache as improvement. |
| 45 | |
| 46 | Upstream-Status: Backport [commit 0454b0445393aafc5600e92ef0c39494e333b135 |
| 47 | from https://git.savannah.gnu.org/git/grub.git] |
| 48 | |
| 49 | Signed-off-by: Michael Chang <mchang@suse.com> |
| 50 | Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com> |
| 51 | Signed-off-by: Yongxin Liu <yongxin.liu@windriver.com> |
| 52 | --- |
| 53 | grub-core/disk/lvm.c | 190 +++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 54 | 1 file changed, 190 insertions(+) |
| 55 | |
| 56 | diff --git a/grub-core/disk/lvm.c b/grub-core/disk/lvm.c |
| 57 | index 7b265c7..dc6b83b 100644 |
| 58 | --- a/grub-core/disk/lvm.c |
| 59 | +++ b/grub-core/disk/lvm.c |
| 60 | @@ -33,6 +33,14 @@ |
| 61 | |
| 62 | GRUB_MOD_LICENSE ("GPLv3+"); |
| 63 | |
| 64 | +struct cache_lv |
| 65 | +{ |
| 66 | + struct grub_diskfilter_lv *lv; |
| 67 | + char *cache_pool; |
| 68 | + char *origin; |
| 69 | + struct cache_lv *next; |
| 70 | +}; |
| 71 | + |
| 72 | |
| 73 | /* Go the string STR and return the number after STR. *P will point |
| 74 | at the number. In case STR is not found, *P will be NULL and the |
| 75 | @@ -95,6 +103,34 @@ grub_lvm_check_flag (char *p, const char *str, const char *flag) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | +static void |
| 80 | +grub_lvm_free_cache_lvs (struct cache_lv *cache_lvs) |
| 81 | +{ |
| 82 | + struct cache_lv *cache; |
| 83 | + |
| 84 | + while ((cache = cache_lvs)) |
| 85 | + { |
| 86 | + cache_lvs = cache_lvs->next; |
| 87 | + |
| 88 | + if (cache->lv) |
| 89 | + { |
| 90 | + unsigned int i; |
| 91 | + |
| 92 | + for (i = 0; i < cache->lv->segment_count; ++i) |
| 93 | + if (cache->lv->segments) |
| 94 | + grub_free (cache->lv->segments[i].nodes); |
| 95 | + grub_free (cache->lv->segments); |
| 96 | + grub_free (cache->lv->fullname); |
| 97 | + grub_free (cache->lv->idname); |
| 98 | + grub_free (cache->lv->name); |
| 99 | + } |
| 100 | + grub_free (cache->lv); |
| 101 | + grub_free (cache->origin); |
| 102 | + grub_free (cache->cache_pool); |
| 103 | + grub_free (cache); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | static struct grub_diskfilter_vg * |
| 108 | grub_lvm_detect (grub_disk_t disk, |
| 109 | struct grub_diskfilter_pv_id *id, |
| 110 | @@ -242,6 +278,8 @@ grub_lvm_detect (grub_disk_t disk, |
| 111 | |
| 112 | if (! vg) |
| 113 | { |
| 114 | + struct cache_lv *cache_lvs = NULL; |
| 115 | + |
| 116 | /* First time we see this volume group. We've to create the |
| 117 | whole volume group structure. */ |
| 118 | vg = grub_malloc (sizeof (*vg)); |
| 119 | @@ -671,6 +709,106 @@ grub_lvm_detect (grub_disk_t disk, |
| 120 | seg->nodes[seg->node_count - 1].name = tmp; |
| 121 | } |
| 122 | } |
| 123 | + else if (grub_memcmp (p, "cache\"", |
| 124 | + sizeof ("cache\"") - 1) == 0) |
| 125 | + { |
| 126 | + struct cache_lv *cache = NULL; |
| 127 | + |
| 128 | + char *p2, *p3; |
| 129 | + grub_size_t sz; |
| 130 | + |
| 131 | + cache = grub_zalloc (sizeof (*cache)); |
| 132 | + if (!cache) |
| 133 | + goto cache_lv_fail; |
| 134 | + cache->lv = grub_zalloc (sizeof (*cache->lv)); |
| 135 | + if (!cache->lv) |
| 136 | + goto cache_lv_fail; |
| 137 | + grub_memcpy (cache->lv, lv, sizeof (*cache->lv)); |
| 138 | + |
| 139 | + if (lv->fullname) |
| 140 | + { |
| 141 | + cache->lv->fullname = grub_strdup (lv->fullname); |
| 142 | + if (!cache->lv->fullname) |
| 143 | + goto cache_lv_fail; |
| 144 | + } |
| 145 | + if (lv->idname) |
| 146 | + { |
| 147 | + cache->lv->idname = grub_strdup (lv->idname); |
| 148 | + if (!cache->lv->idname) |
| 149 | + goto cache_lv_fail; |
| 150 | + } |
| 151 | + if (lv->name) |
| 152 | + { |
| 153 | + cache->lv->name = grub_strdup (lv->name); |
| 154 | + if (!cache->lv->name) |
| 155 | + goto cache_lv_fail; |
| 156 | + } |
| 157 | + |
| 158 | + skip_lv = 1; |
| 159 | + |
| 160 | + p2 = grub_strstr (p, "cache_pool = \""); |
| 161 | + if (!p2) |
| 162 | + goto cache_lv_fail; |
| 163 | + |
| 164 | + p2 = grub_strchr (p2, '"'); |
| 165 | + if (!p2) |
| 166 | + goto cache_lv_fail; |
| 167 | + |
| 168 | + p3 = ++p2; |
| 169 | + p3 = grub_strchr (p3, '"'); |
| 170 | + if (!p3) |
| 171 | + goto cache_lv_fail; |
| 172 | + |
| 173 | + sz = p3 - p2; |
| 174 | + |
| 175 | + cache->cache_pool = grub_malloc (sz + 1); |
| 176 | + if (!cache->cache_pool) |
| 177 | + goto cache_lv_fail; |
| 178 | + grub_memcpy (cache->cache_pool, p2, sz); |
| 179 | + cache->cache_pool[sz] = '\0'; |
| 180 | + |
| 181 | + p2 = grub_strstr (p, "origin = \""); |
| 182 | + if (!p2) |
| 183 | + goto cache_lv_fail; |
| 184 | + |
| 185 | + p2 = grub_strchr (p2, '"'); |
| 186 | + if (!p2) |
| 187 | + goto cache_lv_fail; |
| 188 | + |
| 189 | + p3 = ++p2; |
| 190 | + p3 = grub_strchr (p3, '"'); |
| 191 | + if (!p3) |
| 192 | + goto cache_lv_fail; |
| 193 | + |
| 194 | + sz = p3 - p2; |
| 195 | + |
| 196 | + cache->origin = grub_malloc (sz + 1); |
| 197 | + if (!cache->origin) |
| 198 | + goto cache_lv_fail; |
| 199 | + grub_memcpy (cache->origin, p2, sz); |
| 200 | + cache->origin[sz] = '\0'; |
| 201 | + |
| 202 | + cache->next = cache_lvs; |
| 203 | + cache_lvs = cache; |
| 204 | + break; |
| 205 | + |
| 206 | + cache_lv_fail: |
| 207 | + if (cache) |
| 208 | + { |
| 209 | + grub_free (cache->origin); |
| 210 | + grub_free (cache->cache_pool); |
| 211 | + if (cache->lv) |
| 212 | + { |
| 213 | + grub_free (cache->lv->fullname); |
| 214 | + grub_free (cache->lv->idname); |
| 215 | + grub_free (cache->lv->name); |
| 216 | + } |
| 217 | + grub_free (cache->lv); |
| 218 | + grub_free (cache); |
| 219 | + } |
| 220 | + grub_lvm_free_cache_lvs (cache_lvs); |
| 221 | + goto fail4; |
| 222 | + } |
| 223 | else |
| 224 | { |
| 225 | #ifdef GRUB_UTIL |
| 226 | @@ -747,6 +885,58 @@ grub_lvm_detect (grub_disk_t disk, |
| 227 | } |
| 228 | |
| 229 | } |
| 230 | + |
| 231 | + { |
| 232 | + struct cache_lv *cache; |
| 233 | + |
| 234 | + for (cache = cache_lvs; cache; cache = cache->next) |
| 235 | + { |
| 236 | + struct grub_diskfilter_lv *lv; |
| 237 | + |
| 238 | + for (lv = vg->lvs; lv; lv = lv->next) |
| 239 | + if (grub_strcmp (lv->name, cache->origin) == 0) |
| 240 | + break; |
| 241 | + if (lv) |
| 242 | + { |
| 243 | + cache->lv->segments = grub_malloc (lv->segment_count * sizeof (*lv->segments)); |
| 244 | + if (!cache->lv->segments) |
| 245 | + { |
| 246 | + grub_lvm_free_cache_lvs (cache_lvs); |
| 247 | + goto fail4; |
| 248 | + } |
| 249 | + grub_memcpy (cache->lv->segments, lv->segments, lv->segment_count * sizeof (*lv->segments)); |
| 250 | + |
| 251 | + for (i = 0; i < lv->segment_count; ++i) |
| 252 | + { |
| 253 | + struct grub_diskfilter_node *nodes = lv->segments[i].nodes; |
| 254 | + grub_size_t node_count = lv->segments[i].node_count; |
| 255 | + |
| 256 | + cache->lv->segments[i].nodes = grub_malloc (node_count * sizeof (*nodes)); |
| 257 | + if (!cache->lv->segments[i].nodes) |
| 258 | + { |
| 259 | + for (j = 0; j < i; ++j) |
| 260 | + grub_free (cache->lv->segments[j].nodes); |
| 261 | + grub_free (cache->lv->segments); |
| 262 | + cache->lv->segments = NULL; |
| 263 | + grub_lvm_free_cache_lvs (cache_lvs); |
| 264 | + goto fail4; |
| 265 | + } |
| 266 | + grub_memcpy (cache->lv->segments[i].nodes, nodes, node_count * sizeof (*nodes)); |
| 267 | + } |
| 268 | + |
| 269 | + if (cache->lv->segments) |
| 270 | + { |
| 271 | + cache->lv->segment_count = lv->segment_count; |
| 272 | + cache->lv->vg = vg; |
| 273 | + cache->lv->next = vg->lvs; |
| 274 | + vg->lvs = cache->lv; |
| 275 | + cache->lv = NULL; |
| 276 | + } |
| 277 | + } |
| 278 | + } |
| 279 | + } |
| 280 | + |
| 281 | + grub_lvm_free_cache_lvs (cache_lvs); |
| 282 | if (grub_diskfilter_vg_register (vg)) |
| 283 | goto fail4; |
| 284 | } |
| 285 | -- |
| 286 | 2.14.4 |
| 287 | |