Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1iRvDz-000222-QM for mharc-grub-devel@gnu.org; Tue, 05 Nov 2019 04:34:07 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:47698) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iRvDx-000212-Bt for grub-devel@gnu.org; Tue, 05 Nov 2019 04:34:06 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iRvDs-0005sp-Am for grub-devel@gnu.org; Tue, 05 Nov 2019 04:34:01 -0500 Received: from m9a0002g.houston.softwaregrp.com ([15.124.64.67]:37741) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1iRvDs-0005sX-49 for grub-devel@gnu.org; Tue, 05 Nov 2019 04:34:00 -0500 Received: FROM m9a0002g.houston.softwaregrp.com (15.121.0.190) BY m9a0002g.houston.softwaregrp.com WITH ESMTP FOR grub-devel@gnu.org; Tue, 5 Nov 2019 09:33:21 +0000 Received: from M9W0067.microfocus.com (2002:f79:be::f79:be) by M9W0067.microfocus.com (2002:f79:be::f79:be) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1591.10; Tue, 5 Nov 2019 09:33:01 +0000 Received: from NAM03-CO1-obe.outbound.protection.outlook.com (15.124.72.11) by M9W0067.microfocus.com (15.121.0.190) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1591.10 via Frontend Transport; Tue, 5 Nov 2019 09:33:01 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=EM5Dv/XxbF1lSNKr3rfNu64C1dLQ4umHPelzH/YOzhR5ic3yMF5GKP7vk3oTHr3a7L7FTSDw4r7+dyzXiH+3FpmQ2ysW7gq/ilByY3drQAlVse6cjzdA4SUix+jDtikdHg5yJcLKRzjjftPWe4rWMdLgjq0PMchniy4RUzeobXqOCszg39NEvKbhEQWXUvNzBBV5pzLiCzmdtqy5ytYbS9MMus3tc9MKPNc97vvDI1y7C1WtYaEbMnObRbjQzPaDPQrmHPHCZYJPtMUI9z7nYWPaf3wc5dyPQcuyufd5Yc5JdZxaMPwzsngqQhFjRU33oYQMHQpviGPlLE28FgQ9fg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=TZEbBhmDMV+2WKpoCD/rwshfrTFx/Xxp5jdijFknCCc=; b=UbVJ7fG/F4/p66EDuDl86zGdIiXCYUIA3wKbdDvBdPKR4jZfNJsbGeraqk6LFyUYPBqa4gTI6oaBAwzgxGlWdfjU1JgXW6HXJutUFUUQf0VgZ65HzMHMdy/sgUDEFI2EQL9xmRMTbX5qF8osJCdg1/LxRbno0VjQL13ZdiBx173+a52BSQf7ZmMfuDu2B5K4sfFPTLMNRIP2f2W5p6D1o2I0EpAgU9eEKdGd9ImcBdqCPCLJZBPrNeXd4DSezxIpeWRxwBfv1I0Znya8IeqQzMXcIvjxBxSLviWJvo5CIq4cMwIMN75gzPymzfDhXBdBHCW+XFHwmVWxaZovM66pJw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=suse.com; dmarc=pass action=none header.from=suse.com; dkim=pass header.d=suse.com; arc=none Received: from BY5PR18MB3122.namprd18.prod.outlook.com (10.255.139.11) by BY5PR18MB3283.namprd18.prod.outlook.com (10.255.139.203) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2408.24; Tue, 5 Nov 2019 09:33:00 +0000 Received: from BY5PR18MB3122.namprd18.prod.outlook.com ([fe80::297d:fc41:ae2a:13d9]) by BY5PR18MB3122.namprd18.prod.outlook.com ([fe80::297d:fc41:ae2a:13d9%5]) with mapi id 15.20.2408.024; Tue, 5 Nov 2019 09:33:00 +0000 From: Michael Chang To: "grub-devel@gnu.org" Subject: [PATCH v2] lvm: add lvm cache logical volume handling Thread-Topic: [PATCH v2] lvm: add lvm cache logical volume handling Thread-Index: AQHVk7wDMbTUyTUE90KAo75e0VEgMw== Date: Tue, 5 Nov 2019 09:33:00 +0000 Message-ID: <20191105093249.29959-1-mchang@suse.com> Accept-Language: zh-TW, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-clientproxiedby: HK0PR03CA0085.apcprd03.prod.outlook.com (2603:1096:203:72::25) To BY5PR18MB3122.namprd18.prod.outlook.com (2603:10b6:a03:1af::11) authentication-results: spf=none (sender IP is ) smtp.mailfrom=MChang@suse.com; x-ms-exchange-messagesentrepresentingtype: 1 x-mailer: git-send-email 2.16.4 x-originating-ip: [60.251.47.116] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: ec12ec24-beea-4b15-679a-08d761d3263d x-ms-traffictypediagnostic: BY5PR18MB3283: x-microsoft-antispam-prvs: x-ms-oob-tlc-oobclassifiers: OLM:8273; x-forefront-prvs: 0212BDE3BE x-forefront-antispam-report: SFV:NSPM; SFS:(10019020)(4636009)(376002)(366004)(346002)(396003)(39860400002)(136003)(189003)(199004)(6116002)(3846002)(14454004)(186003)(1076003)(6436002)(64756008)(66446008)(66946007)(7736002)(5660300002)(2906002)(25786009)(478600001)(305945005)(66066001)(2351001)(86362001)(316002)(81166006)(8676002)(81156014)(476003)(5640700003)(14444005)(8936002)(50226002)(36756003)(99286004)(6916009)(52116002)(102836004)(2616005)(6506007)(55236004)(486006)(6512007)(80792005)(2501003)(71200400001)(71190400001)(66476007)(256004)(6486002)(66556008)(26005)(386003)(403724002); DIR:OUT; SFP:1102; SCL:1; SRVR:BY5PR18MB3283; H:BY5PR18MB3122.namprd18.prod.outlook.com; FPR:; SPF:None; LANG:en; PTR:InfoNoRecords; MX:1; A:1; received-spf: None (protection.outlook.com: suse.com does not designate permitted sender hosts) x-ms-exchange-senderadcheck: 1 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: 0l3sXV6/vdluwxA0lTtjk77edVC0UQWJFY0dory44jejYbttlxHZ7og/X2hB7U/jPmewA8ZP7Y9VwUHVyatpzyoGijDt8EUdTrWnrMAY7UOZ4y6dQy9/YoVrnefb4DF6+JGi9ec+K9Bhx+0C57J2zy3h+RnpqtIyNcz0QmV+DQNhHkB5KPeZL1buSelWD4y0VoPh7RycgiGOtN2gKhSaqc8V3WmwZHDYnKWNfAjafTPAVrEh5dIG0h596SqhwcrbI+5P8Y4GK2l6u1M8JtBPXL/hpo4ng/sDX7o3+DtPiO6Nkl4zx8yq+MR3G5n9RhInqqkq66dVY4AeFFQxyc66SYFq4bIE1NwFd7qM4HeO39unq9lgZXklNkmHR1OE1CUkPTFIKVQ7K020wqJfcf2ZVnNK9SMn/Jq+isWzYJdZ0MHcmRvYKiE7eU+sWKYXzTet x-ms-exchange-transport-forked: True Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-MS-Exchange-CrossTenant-Network-Message-Id: ec12ec24-beea-4b15-679a-08d761d3263d X-MS-Exchange-CrossTenant-originalarrivaltime: 05 Nov 2019 09:33:00.6095 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 856b813c-16e5-49a5-85ec-6f081e13b527 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: 3wzY43v/4xvPN76NZmXGUCZsOV/6oD6vAWT2bPUeDmPtROk0XDwP9CX7j9Ba0WI3 X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR18MB3283 X-OriginatorOrg: suse.com X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 15.124.64.67 X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 05 Nov 2019 09:34:06 -0000 The lvm cache logical volume is the logical volume consisting of the original and the cache pool logical volume. The original is usually on a larger and slower storage device while the cache pool is on a smaller and faster one. The performance of the original volume can be improved by storing the frequently used data on the cache pool to utilize the greater performance of faster device. The default cache mode "writethrough" ensures that any data written will be stored both in the cache and on the origin LV, therefore grub can go straight to read the original lv as no data loss is guarenteed. The second cache mode is "writeback", which delays writing from the cache pool back to the origin LV to have increased performance. The drawback is potential data loss if losing the associated cache device. During the boot time grub reads the LVM offline i.e. LVM volumes are not activated and mounted, IMHO it should be fine to read directly from original lv since all cached data should have been flushed back in the process of taking it offline. Signed-off-by: Michael Chang --- Changes since v2: * Move struct cache_lv definition to the beginning of file * Add handling when grub_(zalloc|malloc|strdup) etc failed grub-core/disk/lvm.c | 149 +++++++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 149 insertions(+) diff --git a/grub-core/disk/lvm.c b/grub-core/disk/lvm.c index 7b265c780..e296b49e3 100644 --- a/grub-core/disk/lvm.c +++ b/grub-core/disk/lvm.c @@ -33,6 +33,14 @@ =20 GRUB_MOD_LICENSE ("GPLv3+"); =20 +struct cache_lv +{ + struct grub_diskfilter_lv *lv; + char *cache_pool; + char *origin; + struct cache_lv *next; +}; + =0C /* Go the string STR and return the number after STR. *P will point at the number. In case STR is not found, *P will be NULL and the @@ -242,6 +250,8 @@ grub_lvm_detect (grub_disk_t disk, =20 if (! vg) { + struct cache_lv *cache_lvs =3D NULL; + /* First time we see this volume group. We've to create the whole volume group structure. */ vg =3D grub_malloc (sizeof (*vg)); @@ -671,6 +681,79 @@ grub_lvm_detect (grub_disk_t disk, seg->nodes[seg->node_count - 1].name =3D tmp; } } + else if (grub_memcmp (p, "cache\"", + sizeof ("cache\"") - 1) =3D=3D 0) + { + struct cache_lv *cache; + + char *p2, *p3; + grub_ssize_t sz; + + if (!(cache =3D grub_zalloc (sizeof (*cache)))) + goto cache_lv_fail; + if (!(cache->lv =3D grub_zalloc (sizeof (*cache->lv)))) + goto cache_lv_fail; + grub_memcpy (cache->lv, lv, sizeof (*cache->lv)); + + if (lv->fullname) + if (!(cache->lv->fullname =3D grub_strdup (lv->fullname))) + goto cache_lv_fail; + if (lv->idname) + if (!(cache->lv->idname =3D grub_strdup (lv->idname))) + goto cache_lv_fail; + if (lv->name) + if (!(cache->lv->name =3D grub_strdup (lv->name))) + goto cache_lv_fail; + + skip_lv =3D 1; + + if (!(p2 =3D grub_strstr (p, "cache_pool =3D \""))) + goto cache_lv_fail; + + if (!(p2 =3D grub_strchr (p2, '"'))) + goto cache_lv_fail; + + p3 =3D ++p2; + if (!(p3 =3D grub_strchr (p3, '"'))) + goto cache_lv_fail; + + sz =3D p3 - p2; + + if (!(cache->cache_pool =3D grub_malloc (sz + 1))) + goto cache_lv_fail; + grub_memcpy (cache->cache_pool, p2, sz); + cache->cache_pool[sz] =3D '\0'; + + if (!(p2 =3D grub_strstr (p, "origin =3D \""))) + goto cache_lv_fail; + + if (!(p2 =3D grub_strchr (p2, '"'))) + goto cache_lv_fail; + + p3 =3D ++p2; + if (!(p3 =3D grub_strchr (p3, '"'))) + goto cache_lv_fail; + + sz =3D p3 - p2; + + if (!(cache->origin =3D grub_malloc (sz + 1))) + goto cache_lv_fail; + grub_memcpy (cache->origin, p2, sz); + cache->origin[sz] =3D '\0'; + + cache->next =3D cache_lvs; + cache_lvs =3D cache; + break; + + cache_lv_fail: + grub_free (cache->origin); + grub_free (cache->cache_pool); + grub_free (cache->lv->fullname); + grub_free (cache->lv->idname); + grub_free (cache->lv->name); + grub_free (cache); + break; + } else { #ifdef GRUB_UTIL @@ -747,6 +830,72 @@ grub_lvm_detect (grub_disk_t disk, } =09 } + + { + struct cache_lv *cache; + + for (cache =3D cache_lvs; cache; cache =3D cache->next) + { + struct grub_diskfilter_lv *lv; + + for (lv =3D vg->lvs; lv; lv =3D lv->next) + { + if (grub_strcmp (lv->name, cache->origin) =3D=3D 0) + break; + } + if (lv) + { + if (!(cache->lv->segments =3D grub_malloc (lv->segment_count * sizeof (*= lv->segments)))) + continue; + grub_memcpy (cache->lv->segments, lv->segments, lv->segment_count * size= of (*lv->segments)); + + for (i =3D 0; i < lv->segment_count; ++i) + { + struct grub_diskfilter_node *nodes =3D lv->segments[i].nodes; + grub_size_t node_count =3D lv->segments[i].node_count; + if (!(cache->lv->segments[i].nodes =3D grub_malloc (node_count * siz= eof (*nodes)))) + { + for (j =3D 0; j < i; ++j) + grub_free (cache->lv->segments[j].nodes); + grub_free (cache->lv->segments); + cache->lv->segments =3D NULL; + break; + } + grub_memcpy (cache->lv->segments[i].nodes, nodes, node_count * sizeo= f (*nodes)); + } + + if (cache->lv->segments) + { + cache->lv->segment_count =3D lv->segment_count; + cache->lv->vg =3D vg; + cache->lv->next =3D vg->lvs; + vg->lvs =3D cache->lv; + cache->lv =3D NULL; + } + } + } + + while ((cache =3D cache_lvs)) + { + cache_lvs =3D cache_lvs->next; + + if (cache->lv) + { + for (i =3D 0; i < cache->lv->segment_count; ++i) + if (cache->lv->segments) + grub_free (cache->lv->segments[i].nodes); + grub_free (cache->lv->segments); + grub_free (cache->lv->fullname); + grub_free (cache->lv->idname); + grub_free (cache->lv->name); + } + grub_free (cache->lv); + grub_free (cache->origin); + grub_free (cache->cache_pool); + grub_free (cache); + } + } + if (grub_diskfilter_vg_register (vg)) goto fail4; } --=20 2.16.4