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

format_text: Transfer circular buf alloc to device layer.

Instead of the caller passing dev_read_circular() a buffer to fill with
data, the device layer itself now allocates it.
This commit is contained in:
Alasdair G Kergon 2017-12-15 22:34:26 +00:00
parent beee9940a5
commit 5f45cb90a7
4 changed files with 30 additions and 28 deletions

View File

@ -530,22 +530,15 @@ int config_file_read_fd(struct dm_config_tree *cft, struct device *dev, dev_io_r
fb = fb + mmap_offset; fb = fb + mmap_offset;
} else { } else {
if (circular) { if (circular) {
if (!(buf = dm_malloc(size + size2))) { if (!(buf = dev_read_circular(dev, (uint64_t) offset, size, (uint64_t) offset2, size2, reason)))
log_error("Failed to allocate circular buffer."); goto_out;
return 0;
}
if (!dev_read_circular(dev, (uint64_t) offset, size,
(uint64_t) offset2, size2, reason, buf)) {
goto out;
}
} else { } else {
if (!(buf = dm_malloc(size))) { if (!(buf = dm_malloc(size))) {
log_error("Failed to allocate buffer for metadata read."); log_error("Failed to allocate buffer for metadata read.");
return 0; return 0;
} }
if (!dev_read(dev, (uint64_t) offset, size, reason, buf)) { if (!dev_read(dev, (uint64_t) offset, size, reason, buf))
goto out; goto_out;
}
} }
fb = buf; fb = buf;
} }

View File

@ -745,25 +745,32 @@ int dev_read(struct device *dev, uint64_t offset, size_t len, dev_io_reason_t re
} }
/* /*
* Read from 'dev' into 'buf' in 2 distinct regions, denoted * Read from 'dev' in 2 distinct regions, denoted by (offset,len) and (offset2,len2).
* by (offset,len) and (offset2,len2). Thus, the total size of * Caller is responsible for dm_free().
* 'buf' should be len+len2.
*/ */
int dev_read_circular(struct device *dev, uint64_t offset, size_t len, char *dev_read_circular(struct device *dev, uint64_t offset, size_t len,
uint64_t offset2, size_t len2, dev_io_reason_t reason, char *buf) uint64_t offset2, size_t len2, dev_io_reason_t reason)
{ {
char *buf = NULL;
if (!(buf = dm_malloc(len + len2))) {
log_error("Buffer allocation failed for split metadata.");
return NULL;
}
if (!dev_read(dev, offset, len, reason, buf)) { if (!dev_read(dev, offset, len, reason, buf)) {
log_error("Read from %s failed", dev_name(dev)); log_error("Read from %s failed", dev_name(dev));
return 0; dm_free(buf);
return NULL;
} }
if (!dev_read(dev, offset2, len2, reason, buf + len)) { if (!dev_read(dev, offset2, len2, reason, buf + len)) {
log_error("Circular read from %s failed", log_error("Circular read from %s failed", dev_name(dev));
dev_name(dev)); dm_free(buf);
return 0; return NULL;
} }
return 1; return buf;
} }
/* FIXME If O_DIRECT can't extend file, dev_extend first; dev_truncate after. /* FIXME If O_DIRECT can't extend file, dev_extend first; dev_truncate after.

View File

@ -146,8 +146,8 @@ int dev_fd(struct device *dev);
const char *dev_name(const struct device *dev); const char *dev_name(const struct device *dev);
int dev_read(struct device *dev, uint64_t offset, size_t len, dev_io_reason_t reason, void *buffer); int dev_read(struct device *dev, uint64_t offset, size_t len, dev_io_reason_t reason, void *buffer);
int dev_read_circular(struct device *dev, uint64_t offset, size_t len, char *dev_read_circular(struct device *dev, uint64_t offset, size_t len,
uint64_t offset2, size_t len2, dev_io_reason_t reason, char *buf); uint64_t offset2, size_t len2, dev_io_reason_t reason);
int dev_write(struct device *dev, uint64_t offset, size_t len, dev_io_reason_t reason, void *buffer); int dev_write(struct device *dev, uint64_t offset, size_t len, dev_io_reason_t reason, void *buffer);
int dev_append(struct device *dev, size_t len, dev_io_reason_t reason, char *buffer); int dev_append(struct device *dev, size_t len, dev_io_reason_t reason, char *buffer);
int dev_set(struct device *dev, uint64_t offset, size_t len, dev_io_reason_t reason, int value); int dev_set(struct device *dev, uint64_t offset, size_t len, dev_io_reason_t reason, int value);

View File

@ -234,16 +234,18 @@ static int _pv_analyze_mda_raw (const struct format_type * fmt,
* "dm_config_maybe_section" returning true when there's no valid * "dm_config_maybe_section" returning true when there's no valid
* metadata in a sector (sectors with all nulls). * metadata in a sector (sectors with all nulls).
*/ */
if (!(buf = dm_malloc(size + size2)))
goto_out;
circular = size2 ? 1 : 0; circular = size2 ? 1 : 0;
if (circular) { if (circular) {
if (!dev_read_circular(area->dev, offset, size, offset2, size2, MDA_CONTENT_REASON(mda_is_primary(mda)), buf)) if (!(buf = dev_read_circular(area->dev, offset, size, offset2, size2, MDA_CONTENT_REASON(mda_is_primary(mda)))))
goto_out; goto_out;
} else if (!dev_read(area->dev, offset, size, MDA_CONTENT_REASON(mda_is_primary(mda)), buf)) } else {
goto_out; if (!(buf = dm_malloc(size)))
goto_out;
if (!dev_read(area->dev, offset, size, MDA_CONTENT_REASON(mda_is_primary(mda)), buf))
goto_out;
}
/* /*
* FIXME: We could add more sophisticated metadata detection * FIXME: We could add more sophisticated metadata detection