]> sourceware.org Git - lvm2.git/commitdiff
device: Eliminate unnecessary buffer from dev_read.
authorAlasdair G Kergon <agk@redhat.com>
Wed, 10 Jan 2018 18:48:01 +0000 (18:48 +0000)
committerAlasdair G Kergon <agk@redhat.com>
Wed, 10 Jan 2018 18:48:01 +0000 (18:48 +0000)
lib/device/dev-io.c
lib/format_text/format-text.c
lib/label/label.c

index bacc7deffd5f169b4810fad4f213f33da896c701..9920ee5feb335c130f73197e2ff7372428917637 100644 (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;
 }
 
index 26a500767f8b4a9064cd63827ff01353d0977a1d..d28449ce1cd5d77614a7320edae0df24f8332a01 100644 (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.", 
index 919a5076143c38d147e3677def27162a86a046c4..afb3b19e9f8b237a22a9079d96eb11e06f20f31d 100644 (file)
@@ -307,7 +307,6 @@ int label_remove(struct device *dev)
        if (!dev_close(dev))
                stack;
 
-       dm_free(readbuf);
        return r;
 }
 
This page took 0.041956 seconds and 5 git commands to generate.