From: Joe Thornber Date: Thu, 25 Oct 2001 14:04:18 +0000 (+0000) Subject: o use dev_name(dev) to get the name of a device, this operation is cheap X-Git-Tag: v2_02_91~6119 X-Git-Url: https://sourceware.org/git/?a=commitdiff_plain;h=c7f0b573ac145e3b1b8605d95ce711d62d59f792;p=lvm2.git o use dev_name(dev) to get the name of a device, this operation is cheap since it just get's the first alias. --- diff --git a/lib/device/dev-cache.c b/lib/device/dev-cache.c index 7b4d6a793..11336ba8c 100644 --- a/lib/device/dev-cache.c +++ b/lib/device/dev-cache.c @@ -51,29 +51,18 @@ static struct { static int _insert(const char *path, int rec); -static struct device *_create_dev(const char *path, dev_t d) +static struct device *_create_dev(dev_t d) { struct device *dev; - char *name = pool_strdup(_cache.mem, path); - - if (!name) { - stack; - return NULL; - } if (!(dev = _alloc(sizeof(*dev)))) { stack; - goto bad; + return NULL; } - dev->name = name; INIT_LIST_HEAD(&dev->aliases); dev->dev = d; return dev; - - bad: - _free(name); - return NULL; } static int _add_alias(struct device *dev, const char *path) @@ -103,15 +92,9 @@ static int _insert_dev(const char *path, dev_t d) struct device *dev; /* is this device already registered ? */ - if ((dev = (struct device *) btree_lookup(_cache.devices, d))) { - if (!_add_alias(dev, path)) { - log_err("Couldn't add alias to dir cache."); - return 0; - } - - } else { + if (!(dev = (struct device *) btree_lookup(_cache.devices, d))) { /* create new device */ - if (!(dev = _create_dev(path, d))) { + if (!(dev = _create_dev(d))) { stack; return 0; } @@ -123,9 +106,8 @@ static int _insert_dev(const char *path, dev_t d) } } - /* insert the name/alias */ - if (!(hash_insert(_cache.names, path, dev))) { - log_err("Couldn't insert device into hash table."); + if (!_add_alias(dev, path)) { + log_err("Couldn't add alias to dir cache."); return 0; } diff --git a/lib/device/dev-io.c b/lib/device/dev-io.c index 60c37b6b1..8d132396e 100644 --- a/lib/device/dev-io.c +++ b/lib/device/dev-io.c @@ -19,16 +19,17 @@ int dev_get_size(struct device *dev, uint64_t *size) { int fd; long s; + const char *name = dev_name(dev); - log_very_verbose("Getting size of %s", dev->name); - if ((fd = open(dev->name, O_RDONLY)) < 0) { - log_sys_error("open", dev->name); + log_very_verbose("Getting size of %s", name); + if ((fd = open(name, O_RDONLY)) < 0) { + log_sys_error("open", name); return 0; } /* FIXME: add 64 bit ioctl */ if (ioctl(fd, BLKGETSIZE, &s) < 0) { - log_sys_error("ioctl BLKGETSIZE", dev->name); + log_sys_error("ioctl BLKGETSIZE", name); close(fd); return 0; } @@ -66,15 +67,16 @@ int64_t dev_read(struct device *dev, uint64_t offset, int64_t len, void *buffer) { int64_t r; - int fd = open(dev->name, O_RDONLY); + const char *name = dev_name(dev); + int fd = open(name, O_RDONLY); if (fd < 0) { - log_sys_very_verbose("open", dev->name); + log_sys_very_verbose("open", name); return 0; } if (lseek(fd, offset, SEEK_SET) < 0) { - log_sys_error("lseek", dev->name); + log_sys_error("lseek", name); return 0; } @@ -107,15 +109,16 @@ int64_t dev_write(struct device *dev, uint64_t offset, int64_t len, void *buffer) { int64_t r; - int fd = open(dev->name, O_WRONLY); + const char *name = dev_name(dev); + int fd = open(name, O_WRONLY); if (fd < 0) { - log_sys_error("open", dev->name); + log_sys_error("open", name); return 0; } if (lseek(fd, offset, SEEK_SET) < 0) { - log_sys_error("lseek", dev->name); + log_sys_error("lseek", name); return 0; } diff --git a/lib/device/device.h b/lib/device/device.h index bf6354e83..b4bdd9d40 100644 --- a/lib/device/device.h +++ b/lib/device/device.h @@ -15,7 +15,6 @@ * pointer comparisons are valid. */ struct device { - char *name; struct list_head aliases; /* struct str_list from lvm-types.h */ dev_t dev; }; @@ -31,6 +30,10 @@ int64_t dev_read(struct device *dev, int64_t dev_write(struct device *dev, uint64_t offset, int64_t len, void *buffer); +static inline const char *dev_name(struct device *dev) { + return list_entry(dev->aliases.next, struct str_list, list)->str; +} + static inline int is_lvm_partition(const char *name) { return 1; diff --git a/lib/display/display.c b/lib/display/display.c index c4d4692ba..4ae15a57b 100644 --- a/lib/display/display.c +++ b/lib/display/display.c @@ -102,7 +102,7 @@ void pvdisplay_colons(struct physical_volume *pv) uuid = display_uuid(pv->id.uuid); log_print("%s:%s:%llu:-1:%u:%u:-1:%llu:%u:%u:%u:%s", - pv->dev->name, + dev_name(pv->dev), pv->vg_name, pv->size, /* FIXME pv->pv_number, Derive or remove? */ @@ -133,7 +133,7 @@ void pvdisplay_full(struct physical_volume * pv) uuid = display_uuid(pv->id.uuid); log_print("--- %sPhysical volume ---", pv->pe_size ? "" : "NEW "); - log_print("PV Name %s", pv->dev->name); + log_print("PV Name %s", dev_name(pv->dev)); log_print("VG Name %s", pv->vg_name); size = display_size(pv->size / 2, SIZE_SHORT); @@ -194,7 +194,7 @@ void pv_display_short(struct physical_volume *pv) if (!pv) return; - log_print("PV Name %s ", pv->dev->name); + log_print("PV Name %s ", dev_name(pv->dev)); /* FIXME pv->pv_number); */ log_print("PV Status %savailable / %sallocatable", (pv->status & ACTIVE) ? "" : "NOT ", diff --git a/lib/filters/filter-persistent.c b/lib/filters/filter-persistent.c index 9750b398d..690c07b22 100644 --- a/lib/filters/filter-persistent.c +++ b/lib/filters/filter-persistent.c @@ -162,13 +162,13 @@ int persistent_filter_dump(struct dev_filter *f) static int _lookup_p(struct dev_filter *f, struct device *dev) { struct pfilter *pf = (struct pfilter *) f->private; - void *l = hash_lookup(pf->devices, dev->name); + void *l = hash_lookup(pf->devices, dev_name(dev)); if (!l) { l = pf->real->passes_filter(pf->real, dev) ? PF_GOOD_DEVICE : PF_BAD_DEVICE; - hash_insert(pf->devices, dev->name, l); + hash_insert(pf->devices, dev_name(dev), l); } return l == PF_GOOD_DEVICE; diff --git a/lib/filters/filter-regex.c b/lib/filters/filter-regex.c index 4054f652a..34c34eb1a 100644 --- a/lib/filters/filter-regex.c +++ b/lib/filters/filter-regex.c @@ -151,7 +151,7 @@ static int _accept_p(struct dev_filter *f, struct device *dev) int m; struct rfilter *rf = (struct rfilter *) f->private; - m = matcher_run(rf->engine, dev->name); + m = matcher_run(rf->engine, dev_name(dev)); /* * pass everything that doesn't match, diff --git a/lib/filters/filter.c b/lib/filters/filter.c index 30eb3b6d5..2cb6434fc 100644 --- a/lib/filters/filter.c +++ b/lib/filters/filter.c @@ -55,17 +55,19 @@ static device_info_t device_info[] = { static int *scan_proc_dev(void); -static int passes_lvm_type_device_filter(struct dev_filter *f, struct device *dev) +static int passes_lvm_type_device_filter(struct dev_filter *f, + struct device *dev) { int fd; + const char *name = dev_name(dev); /* Is this a recognised device type? */ if (!(((int *) f->private)[MAJOR(dev->dev)])) return 0; /* Check it's accessible */ - if ((fd = open(dev->name, O_RDONLY)) < 0) { - log_debug("Unable to open %s: %s", dev->name, strerror(errno)); + if ((fd = open(name, O_RDONLY)) < 0) { + log_debug("Unable to open %s: %s", name, strerror(errno)); return 0; } diff --git a/lib/format1/disk-rep.c b/lib/format1/disk-rep.c index 94a6f3438..50f4a7fd9 100644 --- a/lib/format1/disk-rep.c +++ b/lib/format1/disk-rep.c @@ -237,25 +237,27 @@ struct disk_list *read_pv(struct device *dev, struct pool *mem, const char *vg_name) { struct disk_list *data = pool_alloc(mem, sizeof(*data)); + const char *name = dev_name(dev); + data->dev = dev; data->mem = mem; INIT_LIST_HEAD(&data->uuids); INIT_LIST_HEAD(&data->lvs); if (!_read_pv(data)) { - log_debug("Failed to read PV data from %s", dev->name); + log_debug("Failed to read PV data from %s", name); goto bad; } if (data->pv.id[0] != 'H' || data->pv.id[1] != 'M') { log_very_verbose("%s does not have a valid PV identifier", - dev->name); + name); goto bad; } if (!_munge_formats(&data->pv)) { log_very_verbose("Unknown metadata version %d found on %s", - data->pv.version, dev->name); + data->pv.version, name); goto bad; } @@ -263,33 +265,33 @@ struct disk_list *read_pv(struct device *dev, struct pool *mem, * is it an orphan ? */ if (data->pv.vg_name == '\0') { - log_very_verbose("%s is not a member of any VG", dev->name); + log_very_verbose("%s is not a member of any VG", name); return data; } if (vg_name && strcmp(vg_name, data->pv.vg_name)) { log_very_verbose("%s is not a member of the VG %s", - dev->name, vg_name); + name, vg_name); goto bad; } if (!_read_vg(data)) { - log_error("Failed to read VG data from PV (%s)", dev->name); + log_error("Failed to read VG data from PV (%s)", name); goto bad; } if (!_read_uuids(data)) { - log_error("Failed to read PV uuid list from %s", dev->name); + log_error("Failed to read PV uuid list from %s", name); goto bad; } if (!_read_lvs(data)) { - log_error("Failed to read LV's from %s", dev->name); + log_error("Failed to read LV's from %s", name); goto bad; } if (!_read_extents(data)) { - log_error("Failed to read extents from %s", dev->name); + log_error("Failed to read extents from %s", name); goto bad; } @@ -350,7 +352,7 @@ static int _write_uuids(struct disk_list *data) list_for_each(tmp, &data->uuids) { if (pos >= end) { log_error("Too many uuids to fit on %s", - data->dev->name); + dev_name(data->dev)); return 0; } @@ -423,7 +425,7 @@ static int _write_pv(struct disk_list *data) static int _write_all_pv(struct disk_list *data) { - const char *pv_name = data->dev->name; + const char *pv_name = dev_name(data->dev); if (!_write_pv(data)) { log_error("Failed to write PV structure onto %s", pv_name); @@ -474,7 +476,7 @@ int write_pvs(struct list_head *pvs) if (!(_write_all_pv(dl))) fail; - log_debug("Successfully wrote data to %s", dl->dev->name); + log_debug("Successfully wrote data to %s", dev_name(dl->dev)); } return 1; diff --git a/lib/format1/format1.c b/lib/format1/format1.c index 1c7167f85..837fde5f7 100644 --- a/lib/format1/format1.c +++ b/lib/format1/format1.c @@ -29,7 +29,7 @@ static int _check_vgs(struct list_head *pvs) first = dl; else if (memcmp(&first->vg, &dl->vg, sizeof(first->vg))) { log_err("VG data differs between PVs %s and %s", - first->dev->name, dl->dev->name); + dev_name(first->dev), dev_name(dl->dev)); return 0; } pv_count++; diff --git a/lib/format1/layout.c b/lib/format1/layout.c index c1aeb5740..e4a9f166a 100644 --- a/lib/format1/layout.c +++ b/lib/format1/layout.c @@ -130,7 +130,7 @@ int calculate_extent_count(struct physical_volume *pv) if (pvd->pe_total < PE_SIZE_PV_SIZE_REL) { log_error("Insufficient space for extents on %s", - pv->dev->name); + dev_name(pv->dev)); return 0; } diff --git a/lib/metadata/metadata.c b/lib/metadata/metadata.c index 95d28041c..0a8f26af0 100644 --- a/lib/metadata/metadata.c +++ b/lib/metadata/metadata.c @@ -218,10 +218,13 @@ struct physical_volume *pv_create(struct io_space *ios, const char *name) struct list_head *find_pv_in_vg(struct volume_group *vg, const char *pv_name) { - struct list_head *pvh; - list_for_each(pvh, &vg->pvs) { - if (!strcmp(list_entry(pvh, struct pv_list, list)->pv.dev->name, - pv_name)) return pvh; + struct list_head *tmp; + struct pv_list *pv; + + list_for_each(tmp, &vg->pvs) { + pv = list_entry(tmp, struct pv_list, list); + if (!strcmp(dev_name(pv->pv.dev), pv_name)) + return tmp; } return NULL; diff --git a/tools/pvchange.c b/tools/pvchange.c index 3ab5a65ff..7fbdc4ce8 100644 --- a/tools/pvchange.c +++ b/tools/pvchange.c @@ -89,7 +89,7 @@ int pvchange_single(struct physical_volume *pv) struct volume_group *vg = NULL; struct list_head *pvh; - char *pv_name = pv->dev->name; + const char *pv_name = dev_name(pv->dev); int allocation = !strcmp(arg_str_value(allocation_ARG, "n"), "y"); @@ -135,7 +135,7 @@ int pvchange_single(struct physical_volume *pv) log_verbose("Physical volume %s inactive", pv_name); } - log_verbose("Updating physical volume %s", pv->dev->name); + log_verbose("Updating physical volume %s", pv_name); if (*pv->vg_name) { if (!(ios->vg_write(ios,vg))) { log_error("Failed to store physical volume %s in " diff --git a/tools/pvdisplay.c b/tools/pvdisplay.c index 3ba3af94a..2484f56ba 100644 --- a/tools/pvdisplay.c +++ b/tools/pvdisplay.c @@ -62,11 +62,11 @@ void pvdisplay_single(struct physical_volume *pv) char *sz; uint64_t size; - char *pv_name = pv->dev->name; + const char *pv_name = dev_name(pv->dev); if (!*pv->vg_name) size = pv->size; - else + else size = (pv->pe_count - pv->pe_allocated) * pv->pe_size; if (arg_count(short_ARG)) { @@ -77,7 +77,7 @@ void pvdisplay_single(struct physical_volume *pv) return; } - if (pv->status & EXPORTED_VG) + if (pv->status & EXPORTED_VG) log_print("Physical volume '%s' of volume group '%s' " "is exported" , pv_name, pv->vg_name); @@ -87,7 +87,7 @@ void pvdisplay_single(struct physical_volume *pv) if (!pv->vg_name) { log_print ( "'%s' is a new physical volume of %s", - pv_name, ( sz = display_size ( size / 2, + pv_name, ( sz = display_size ( size / 2, SIZE_SHORT))); dbg_free(sz); } diff --git a/tools/pvscan.c b/tools/pvscan.c index 7adc59f6e..6242369aa 100644 --- a/tools/pvscan.c +++ b/tools/pvscan.c @@ -97,7 +97,7 @@ int pvscan(int argc, char **argv) pv_max_name_len = vg_max_name_len = 0; list_for_each(pvh, pvs) { pv = &list_entry(pvh, struct pv_list, list)->pv; - len = strlen(pv->dev->name); + len = strlen(dev_name(pv->dev)); if (pv_max_name_len < len) pv_max_name_len = len; len = strlen(pv->vg_name); @@ -143,7 +143,7 @@ void pvscan_display_single(struct physical_volume *pv) /* short listing? */ if (arg_count(short_ARG) > 0) { - log_print("%s", pv->dev->name); + log_print("%s", dev_name(pv->dev)); return; } @@ -167,9 +167,9 @@ void pvscan_display_single(struct physical_volume *pv) sprintf(pv_tmp_name, "%-*s with UUID %s", pv_max_name_len - 2, - pv->dev->name, display_uuid(pv->id.uuid)); + dev_name(pv->dev), display_uuid(pv->id.uuid)); } else { - sprintf(pv_tmp_name, "%s", pv->dev->name); + sprintf(pv_tmp_name, "%s", dev_name(pv->dev)); } if (!*pv->vg_name) { diff --git a/tools/vgreduce.c b/tools/vgreduce.c index 83581ee58..4d4e6fecd 100644 --- a/tools/vgreduce.c +++ b/tools/vgreduce.c @@ -88,9 +88,10 @@ int vgreduce(int argc, char **argv) static int vgreduce_single(struct volume_group *vg, struct physical_volume *pv) { struct list_head *pvh; + const char *name = dev_name(pv->dev); if (pv->pe_allocated) { - log_error("Physical volume %s still in use", pv->dev->name); + log_error("Physical volume %s still in use", name); return ECMD_FAILED; } @@ -102,31 +103,30 @@ static int vgreduce_single(struct volume_group *vg, struct physical_volume *pv) if (vg->pv_count == 1) { log_error("Can't remove final physical volume %s from " - "volume group %s", pv->dev->name, vg->name); + "volume group %s", name, vg->name); return ECMD_FAILED; } - pvh = find_pv_in_vg(vg, pv->dev->name); + pvh = find_pv_in_vg(vg, name); - log_verbose("Removing %s from volume group %s", pv->dev->name, - vg->name); + log_verbose("Removing %s from volume group %s", name, vg->name); list_del(pvh); *pv->vg_name = '\0'; vg->pv_count--; if (!(ios->vg_write(ios, vg))) { log_error("Removal of physical volume %s from %s failed", - pv->dev->name, vg->name); + name, vg->name); return ECMD_FAILED; } if (!ios->pv_write(ios, pv)) { log_error("Failed to clear metadata from physical volume %s " - "after removal from %s", pv->dev->name, vg->name); + "after removal from %s", name, vg->name); return ECMD_FAILED; } - log_print("Removed %s from volume group %s", pv->dev->name, vg->name); + log_print("Removed %s from volume group %s", name, vg->name); return 0; } diff --git a/tools/vgremove.c b/tools/vgremove.c index da1bf299f..1a20d507b 100644 --- a/tools/vgremove.c +++ b/tools/vgremove.c @@ -62,11 +62,12 @@ static int vgremove_single(const char *vg_name) list_for_each(pvh, &vg->pvs) { pv = &list_entry(pvh, struct pv_list, list)->pv; log_verbose("Removing physical volume %s from volume group %s", - pv->dev->name, vg_name); + dev_name(pv->dev), vg_name); *pv->vg_name = '\0'; if (!(ios->pv_write(ios, pv))) { log_error("Failed to remove physical volume %s from " - "volume group %s", pv->dev->name, vg_name); + "volume group %s", dev_name(pv->dev), + vg_name); ret = ECMD_FAILED; } }