Skip to content

Commit

Permalink
libnvdimm, namespace: sort namespaces by dpa at init
Browse files Browse the repository at this point in the history
Add more determinism to initial namespace device-name assignments by
sorting the namespaces by starting dpa.

Signed-off-by: Dan Williams <dan.j.williams@intel.com>
  • Loading branch information
Dan Williams committed Oct 7, 2016
1 parent 0e3b0d1 commit 6ff3e91
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
35 changes: 32 additions & 3 deletions drivers/nvdimm/namespace_devs.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
#include <linux/module.h>
#include <linux/device.h>
#include <linux/sort.h>
#include <linux/slab.h>
#include <linux/pmem.h>
#include <linux/list.h>
Expand Down Expand Up @@ -66,17 +67,17 @@ static struct device_type namespace_blk_device_type = {
.release = namespace_blk_release,
};

static bool is_namespace_pmem(struct device *dev)
static bool is_namespace_pmem(const struct device *dev)
{
return dev ? dev->type == &namespace_pmem_device_type : false;
}

static bool is_namespace_blk(struct device *dev)
static bool is_namespace_blk(const struct device *dev)
{
return dev ? dev->type == &namespace_blk_device_type : false;
}

static bool is_namespace_io(struct device *dev)
static bool is_namespace_io(const struct device *dev)
{
return dev ? dev->type == &namespace_io_device_type : false;
}
Expand Down Expand Up @@ -1919,6 +1920,31 @@ struct device *create_namespace_blk(struct nd_region *nd_region,
return ERR_PTR(-ENXIO);
}

static int cmp_dpa(const void *a, const void *b)
{
const struct device *dev_a = *(const struct device **) a;
const struct device *dev_b = *(const struct device **) b;
struct nd_namespace_blk *nsblk_a, *nsblk_b;
struct nd_namespace_pmem *nspm_a, *nspm_b;

if (is_namespace_io(dev_a))
return 0;

if (is_namespace_blk(dev_a)) {
nsblk_a = to_nd_namespace_blk(dev_a);
nsblk_b = to_nd_namespace_blk(dev_b);

return memcmp(&nsblk_a->res[0]->start, &nsblk_b->res[0]->start,
sizeof(resource_size_t));
}

nspm_a = to_nd_namespace_pmem(dev_a);
nspm_b = to_nd_namespace_pmem(dev_b);

return memcmp(&nspm_a->nsio.res.start, &nspm_b->nsio.res.start,
sizeof(resource_size_t));
}

static struct device **scan_labels(struct nd_region *nd_region)
{
struct nd_mapping *nd_mapping = &nd_region->mapping[0];
Expand Down Expand Up @@ -2034,6 +2060,9 @@ static struct device **scan_labels(struct nd_region *nd_region)
}
}

if (count > 1)
sort(devs, count, sizeof(struct device *), cmp_dpa, NULL);

return devs;

err:
Expand Down
6 changes: 3 additions & 3 deletions include/linux/nd.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,19 @@ struct nd_namespace_blk {
struct resource **res;
};

static inline struct nd_namespace_io *to_nd_namespace_io(struct device *dev)
static inline struct nd_namespace_io *to_nd_namespace_io(const struct device *dev)
{
return container_of(dev, struct nd_namespace_io, common.dev);
}

static inline struct nd_namespace_pmem *to_nd_namespace_pmem(struct device *dev)
static inline struct nd_namespace_pmem *to_nd_namespace_pmem(const struct device *dev)
{
struct nd_namespace_io *nsio = to_nd_namespace_io(dev);

return container_of(nsio, struct nd_namespace_pmem, nsio);
}

static inline struct nd_namespace_blk *to_nd_namespace_blk(struct device *dev)
static inline struct nd_namespace_blk *to_nd_namespace_blk(const struct device *dev)
{
return container_of(dev, struct nd_namespace_blk, common.dev);
}
Expand Down

0 comments on commit 6ff3e91

Please sign in to comment.