1
0
mirror of git://sourceware.org/git/lvm2.git synced 2024-12-21 13:34:40 +03:00

device: Eliminate unnecessary buffer from dev_read.

This commit is contained in:
Alasdair G Kergon 2018-01-10 18:48:01 +00:00
parent 366493a1d1
commit c350f96c09
3 changed files with 17 additions and 37 deletions

View File

@ -817,7 +817,7 @@ static int _dev_read(struct device *dev, uint64_t offset, size_t len, dev_io_rea
/* Reuse this buffer */
devbuf->data = (char *) devbuf->buf + (offset - devbuf->where.start);
log_debug_io("Cached read for %" PRIu64 " bytes at %" PRIu64 " on %s (for %s)",
len, (uint64_t) offset, dev_name(dev), _reason_text(reason));
(uint64_t) len, (uint64_t) offset, dev_name(dev), _reason_text(reason));
return 1;
}
}
@ -833,27 +833,18 @@ static int _dev_read(struct device *dev, uint64_t offset, size_t len, dev_io_rea
return ret;
}
/* Caller is responsible for dm_free */
/* Returns pointer to read-only buffer. Caller does not free it. */
char *dev_read(struct device *dev, uint64_t offset, size_t len, dev_io_reason_t reason)
{
char *buf;
if (!(buf = dm_malloc(len))) {
log_error("Buffer allocation failed for device read.");
return NULL;
}
if (!_dev_read(dev, offset, len, reason)) {
log_error("Read from %s failed", dev_name(dev));
dm_free(buf);
return NULL;
}
memcpy(buf, DEV_DEVBUF(dev, reason)->data, len);
return buf;
return DEV_DEVBUF(dev, reason)->data;
}
/* Obtains data requested then passes it (read-only) to dev_read_callback_fn() */
int dev_read_callback(struct device *dev, uint64_t offset, size_t len, dev_io_reason_t reason,
lvm_callback_fn_t dev_read_callback_fn, void *callback_context)
{
@ -873,17 +864,15 @@ out:
return r;
}
/* Read into supplied retbuf */
/* Read into supplied retbuf owned by the caller. */
int dev_read_buf(struct device *dev, uint64_t offset, size_t len, dev_io_reason_t reason, void *retbuf)
{
char *buf = NULL;
if (!(buf = dev_read(dev, offset, len, reason)))
return_0;
if (!_dev_read(dev, offset, len, reason)) {
log_error("Read from %s failed", dev_name(dev));
return 0;
}
memcpy(retbuf, buf, len);
dm_free(buf);
memcpy(retbuf, DEV_DEVBUF(dev, reason)->data, len);
return 1;
}
@ -902,22 +891,18 @@ char *dev_read_circular(struct device *dev, uint64_t offset, size_t len,
return NULL;
}
if (!_dev_read(dev, offset, len, reason)) {
if (!dev_read_buf(dev, offset, len, reason, buf)) {
log_error("Read from %s failed", dev_name(dev));
dm_free(buf);
return NULL;
}
memcpy(buf, DEV_DEVBUF(dev, reason)->data, len);
if (!_dev_read(dev, offset2, len2, reason)) {
if (!dev_read_buf(dev, offset2, len2, reason, buf + len)) {
log_error("Circular read from %s failed", dev_name(dev));
dm_free(buf);
return NULL;
}
memcpy(buf + len, DEV_DEVBUF(dev, reason)->data, len2);
return buf;
}

View File

@ -272,20 +272,20 @@ static int _pv_analyze_mda_raw (const struct format_type * fmt,
size += SECTOR_SIZE;
}
}
dm_free(buf);
if (circular)
dm_free(buf);
buf = NULL;
}
r = 1;
out:
dm_free(buf);
if (circular)
dm_free(buf);
if (!dev_close(area->dev))
stack;
return r;
}
static int _text_lv_setup(struct format_instance *fid __attribute__((unused)),
struct logical_volume *lv)
{
@ -512,12 +512,8 @@ static struct raw_locn *_find_vg_rlocn(struct device_area *dev_area,
goto_bad;
if (!strncmp(buf, vgname, len = strlen(vgname)) &&
(isspace(*(buf + len)) || *(buf + len) == '{')) {
dm_free(buf);
(isspace(*(buf + len)) || *(buf + len) == '{'))
return rlocn;
}
dm_free(buf);
log_debug_metadata("Volume group name found in %smetadata on %s at " FMTu64 " does "
"not match expected name %s.",

View File

@ -307,7 +307,6 @@ int label_remove(struct device *dev)
if (!dev_close(dev))
stack;
dm_free(readbuf);
return r;
}