From mboxrd@z Thu Jan 1 00:00:00 1970 Return-path: Received: from metis.ext.pengutronix.de ([2001:67c:670:201:290:27ff:fe1d:cc33]) by bombadil.infradead.org with esmtps (Exim 4.85_2 #1 (Red Hat Linux)) id 1bGO1J-0003Jb-WE for barebox@lists.infradead.org; Fri, 24 Jun 2016 10:07:32 +0000 From: Markus Pargmann Date: Fri, 24 Jun 2016 12:06:01 +0200 Message-Id: <1466762762-15212-6-git-send-email-mpa@pengutronix.de> In-Reply-To: <1466762762-15212-1-git-send-email-mpa@pengutronix.de> References: <1466762762-15212-1-git-send-email-mpa@pengutronix.de> List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "barebox" Errors-To: barebox-bounces+u.kleine-koenig=pengutronix.de@lists.infradead.org Subject: [PATCH v2 5/6] state: backend_storage_direct: also use cached data on write To: barebox@lists.infradead.org Cc: mgr@pengutronix.de From: Michael Grzeschik Signed-off-by: Michael Grzeschik Signed-off-by: Markus Pargmann --- common/state/backend_bucket_direct.c | 55 ++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/common/state/backend_bucket_direct.c b/common/state/backend_bucket_direct.c index 08892f001e25..772267f6dc1d 100644 --- a/common/state/backend_bucket_direct.c +++ b/common/state/backend_bucket_direct.c @@ -29,6 +29,10 @@ struct state_backend_storage_bucket_direct { int fd; + /* Cached data of the last read/write */ + u8 *current_data; + ssize_t current_data_len; + struct device_d *dev; }; @@ -99,6 +103,29 @@ static int state_backend_bucket_direct_read(struct state_backend_storage_bucket return 0; } + +static int state_backend_bucket_direct_fill_cache( + struct state_backend_storage_bucket *bucket) +{ + struct state_backend_storage_bucket_direct *direct = + get_bucket_direct(bucket); + ssize_t read_len; + uint8_t *buf; + int ret; + + ret = state_backend_bucket_direct_read(bucket, &buf, &read_len); + if (ret < 0) { + dev_err(direct->dev, "Failed to read from file, %d\n", ret); + free(buf); + return ret; + } + + direct->current_data = buf; + direct->current_data_len = read_len; + + return 0; +} + static int state_backend_bucket_direct_write(struct state_backend_storage_bucket *bucket, const uint8_t * buf, ssize_t len) @@ -111,6 +138,11 @@ static int state_backend_bucket_direct_write(struct state_backend_storage_bucket if (direct->max_size && len > direct->max_size) return -E2BIG; + /* Nothing in cache? Then read to see what is on the device currently */ + if (!direct->current_data) { + state_backend_bucket_direct_fill_cache(bucket); + } + ret = lseek(direct->fd, direct->offset, SEEK_SET); if (ret < 0) { dev_err(direct->dev, "Failed to seek file, %d\n", ret); @@ -119,6 +151,26 @@ static int state_backend_bucket_direct_write(struct state_backend_storage_bucket meta.magic = direct_magic; meta.written_length = len; + + /* + * If we would write the same data that is currently on the device, we + * can return successfully without writing. + * Note that the cache may still be empty if the storage is empty or + * errors occured. + */ + if (direct->current_data) { + dev_dbg(direct->dev, "Comparing cached data, writing %zd bytes, cached %zd bytes\n", + len, direct->current_data_len); + if (len == direct->current_data_len && + !memcmp(direct->current_data, buf, len)) { + dev_dbg(direct->dev, "Data already on device, not writing again\n"); + return ret; + } else { + free(direct->current_data); + direct->current_data = NULL; + } + } + ret = write_full(direct->fd, &meta, sizeof(meta)); if (ret < 0) { dev_err(direct->dev, "Failed to write metadata to file, %d\n", ret); @@ -147,6 +199,9 @@ static void state_backend_bucket_direct_free(struct struct state_backend_storage_bucket_direct *direct = get_bucket_direct(bucket); + if (direct->current_data) + free(direct->current_data); + close(direct->fd); free(direct); } -- 2.8.1 _______________________________________________ barebox mailing list barebox@lists.infradead.org http://lists.infradead.org/mailman/listinfo/barebox