Skip to content

Commit b4366a8

Browse files
committed
libnvdimm/labels: Introduce getters for namespace label fields
In preparation for LIBNVDIMM to manage labels on CXL devices deploy helpers that abstract the label type from the implementation. The CXL label format is mostly similar to the EFI label format with concepts / fields added, like dynamic region creation and label type guids, and other concepts removed like BLK-mode and interleave-set-cookie ids. In addition to nsl_get_* helpers there is the nsl_ref_name() helper that returns a pointer to a label field rather than copying the data. Where changes touch the old whitespace style, update to clang-format expectations. Reviewed-by: Jonathan Cameron <[email protected]> Link: https://lore.kernel.org/r/162982113002.1124374.15922077050771304490.stgit@dwillia2-desk3.amr.corp.intel.com Signed-off-by: Dan Williams <[email protected]>
1 parent ceeb0da commit b4366a8

File tree

3 files changed

+110
-46
lines changed

3 files changed

+110
-46
lines changed

drivers/nvdimm/label.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -350,14 +350,14 @@ static bool slot_valid(struct nvdimm_drvdata *ndd,
350350
struct nd_namespace_label *nd_label, u32 slot)
351351
{
352352
/* check that we are written where we expect to be written */
353-
if (slot != __le32_to_cpu(nd_label->slot))
353+
if (slot != nsl_get_slot(ndd, nd_label))
354354
return false;
355355

356356
/* check checksum */
357357
if (namespace_label_has(ndd, checksum)) {
358358
u64 sum, sum_save;
359359

360-
sum_save = __le64_to_cpu(nd_label->checksum);
360+
sum_save = nsl_get_checksum(ndd, nd_label);
361361
nd_label->checksum = __cpu_to_le64(0);
362362
sum = nd_fletcher64(nd_label, sizeof_namespace_label(ndd), 1);
363363
nd_label->checksum = __cpu_to_le64(sum_save);
@@ -395,13 +395,13 @@ int nd_label_reserve_dpa(struct nvdimm_drvdata *ndd)
395395
continue;
396396

397397
memcpy(label_uuid, nd_label->uuid, NSLABEL_UUID_LEN);
398-
flags = __le32_to_cpu(nd_label->flags);
398+
flags = nsl_get_flags(ndd, nd_label);
399399
if (test_bit(NDD_NOBLK, &nvdimm->flags))
400400
flags &= ~NSLABEL_FLAG_LOCAL;
401401
nd_label_gen_id(&label_id, label_uuid, flags);
402402
res = nvdimm_allocate_dpa(ndd, &label_id,
403-
__le64_to_cpu(nd_label->dpa),
404-
__le64_to_cpu(nd_label->rawsize));
403+
nsl_get_dpa(ndd, nd_label),
404+
nsl_get_rawsize(ndd, nd_label));
405405
nd_dbg_dpa(nd_region, ndd, res, "reserve\n");
406406
if (!res)
407407
return -EBUSY;
@@ -548,9 +548,9 @@ int nd_label_active_count(struct nvdimm_drvdata *ndd)
548548
nd_label = to_label(ndd, slot);
549549

550550
if (!slot_valid(ndd, nd_label, slot)) {
551-
u32 label_slot = __le32_to_cpu(nd_label->slot);
552-
u64 size = __le64_to_cpu(nd_label->rawsize);
553-
u64 dpa = __le64_to_cpu(nd_label->dpa);
551+
u32 label_slot = nsl_get_slot(ndd, nd_label);
552+
u64 size = nsl_get_rawsize(ndd, nd_label);
553+
u64 dpa = nsl_get_dpa(ndd, nd_label);
554554

555555
dev_dbg(ndd->dev,
556556
"slot%d invalid slot: %d dpa: %llx size: %llx\n",
@@ -879,9 +879,9 @@ static struct resource *to_resource(struct nvdimm_drvdata *ndd,
879879
struct resource *res;
880880

881881
for_each_dpa_resource(ndd, res) {
882-
if (res->start != __le64_to_cpu(nd_label->dpa))
882+
if (res->start != nsl_get_dpa(ndd, nd_label))
883883
continue;
884-
if (resource_size(res) != __le64_to_cpu(nd_label->rawsize))
884+
if (resource_size(res) != nsl_get_rawsize(ndd, nd_label))
885885
continue;
886886
return res;
887887
}

drivers/nvdimm/namespace_devs.c

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,7 +1235,7 @@ static int namespace_update_uuid(struct nd_region *nd_region,
12351235
if (!nd_label)
12361236
continue;
12371237
nd_label_gen_id(&label_id, nd_label->uuid,
1238-
__le32_to_cpu(nd_label->flags));
1238+
nsl_get_flags(ndd, nd_label));
12391239
if (strcmp(old_label_id.id, label_id.id) == 0)
12401240
set_bit(ND_LABEL_REAP, &label_ent->flags);
12411241
}
@@ -1851,9 +1851,9 @@ static bool has_uuid_at_pos(struct nd_region *nd_region, u8 *uuid,
18511851

18521852
if (!nd_label)
18531853
continue;
1854-
isetcookie = __le64_to_cpu(nd_label->isetcookie);
1855-
position = __le16_to_cpu(nd_label->position);
1856-
nlabel = __le16_to_cpu(nd_label->nlabel);
1854+
isetcookie = nsl_get_isetcookie(ndd, nd_label);
1855+
position = nsl_get_position(ndd, nd_label);
1856+
nlabel = nsl_get_nlabel(ndd, nd_label);
18571857

18581858
if (isetcookie != cookie)
18591859
continue;
@@ -1923,8 +1923,8 @@ static int select_pmem_id(struct nd_region *nd_region, u8 *pmem_id)
19231923
*/
19241924
hw_start = nd_mapping->start;
19251925
hw_end = hw_start + nd_mapping->size;
1926-
pmem_start = __le64_to_cpu(nd_label->dpa);
1927-
pmem_end = pmem_start + __le64_to_cpu(nd_label->rawsize);
1926+
pmem_start = nsl_get_dpa(ndd, nd_label);
1927+
pmem_end = pmem_start + nsl_get_rawsize(ndd, nd_label);
19281928
if (pmem_start >= hw_start && pmem_start < hw_end
19291929
&& pmem_end <= hw_end && pmem_end > hw_start)
19301930
/* pass */;
@@ -1947,14 +1947,16 @@ static int select_pmem_id(struct nd_region *nd_region, u8 *pmem_id)
19471947
* @nd_label: target pmem namespace label to evaluate
19481948
*/
19491949
static struct device *create_namespace_pmem(struct nd_region *nd_region,
1950-
struct nd_namespace_index *nsindex,
1951-
struct nd_namespace_label *nd_label)
1950+
struct nd_mapping *nd_mapping,
1951+
struct nd_namespace_label *nd_label)
19521952
{
1953+
struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1954+
struct nd_namespace_index *nsindex =
1955+
to_namespace_index(ndd, ndd->ns_current);
19531956
u64 cookie = nd_region_interleave_set_cookie(nd_region, nsindex);
19541957
u64 altcookie = nd_region_interleave_set_altcookie(nd_region);
19551958
struct nd_label_ent *label_ent;
19561959
struct nd_namespace_pmem *nspm;
1957-
struct nd_mapping *nd_mapping;
19581960
resource_size_t size = 0;
19591961
struct resource *res;
19601962
struct device *dev;
@@ -1966,10 +1968,10 @@ static struct device *create_namespace_pmem(struct nd_region *nd_region,
19661968
return ERR_PTR(-ENXIO);
19671969
}
19681970

1969-
if (__le64_to_cpu(nd_label->isetcookie) != cookie) {
1971+
if (nsl_get_isetcookie(ndd, nd_label) != cookie) {
19701972
dev_dbg(&nd_region->dev, "invalid cookie in label: %pUb\n",
19711973
nd_label->uuid);
1972-
if (__le64_to_cpu(nd_label->isetcookie) != altcookie)
1974+
if (nsl_get_isetcookie(ndd, nd_label) != altcookie)
19731975
return ERR_PTR(-EAGAIN);
19741976

19751977
dev_dbg(&nd_region->dev, "valid altcookie in label: %pUb\n",
@@ -2037,16 +2039,16 @@ static struct device *create_namespace_pmem(struct nd_region *nd_region,
20372039
continue;
20382040
}
20392041

2040-
size += __le64_to_cpu(label0->rawsize);
2041-
if (__le16_to_cpu(label0->position) != 0)
2042+
ndd = to_ndd(nd_mapping);
2043+
size += nsl_get_rawsize(ndd, label0);
2044+
if (nsl_get_position(ndd, label0) != 0)
20422045
continue;
20432046
WARN_ON(nspm->alt_name || nspm->uuid);
2044-
nspm->alt_name = kmemdup((void __force *) label0->name,
2045-
NSLABEL_NAME_LEN, GFP_KERNEL);
2047+
nspm->alt_name = kmemdup(nsl_ref_name(ndd, label0),
2048+
NSLABEL_NAME_LEN, GFP_KERNEL);
20462049
nspm->uuid = kmemdup((void __force *) label0->uuid,
20472050
NSLABEL_UUID_LEN, GFP_KERNEL);
2048-
nspm->lbasize = __le64_to_cpu(label0->lbasize);
2049-
ndd = to_ndd(nd_mapping);
2051+
nspm->lbasize = nsl_get_lbasize(ndd, label0);
20502052
if (namespace_label_has(ndd, abstraction_guid))
20512053
nspm->nsio.common.claim_class
20522054
= to_nvdimm_cclass(&label0->abstraction_guid);
@@ -2237,7 +2239,7 @@ static int add_namespace_resource(struct nd_region *nd_region,
22372239
if (is_namespace_blk(devs[i])) {
22382240
res = nsblk_add_resource(nd_region, ndd,
22392241
to_nd_namespace_blk(devs[i]),
2240-
__le64_to_cpu(nd_label->dpa));
2242+
nsl_get_dpa(ndd, nd_label));
22412243
if (!res)
22422244
return -ENXIO;
22432245
nd_dbg_dpa(nd_region, ndd, res, "%d assign\n", count);
@@ -2276,7 +2278,7 @@ static struct device *create_namespace_blk(struct nd_region *nd_region,
22762278
if (nd_label->isetcookie != __cpu_to_le64(nd_set->cookie2)) {
22772279
dev_dbg(ndd->dev, "expect cookie %#llx got %#llx\n",
22782280
nd_set->cookie2,
2279-
__le64_to_cpu(nd_label->isetcookie));
2281+
nsl_get_isetcookie(ndd, nd_label));
22802282
return ERR_PTR(-EAGAIN);
22812283
}
22822284
}
@@ -2288,23 +2290,22 @@ static struct device *create_namespace_blk(struct nd_region *nd_region,
22882290
dev->type = &namespace_blk_device_type;
22892291
dev->parent = &nd_region->dev;
22902292
nsblk->id = -1;
2291-
nsblk->lbasize = __le64_to_cpu(nd_label->lbasize);
2293+
nsblk->lbasize = nsl_get_lbasize(ndd, nd_label);
22922294
nsblk->uuid = kmemdup(nd_label->uuid, NSLABEL_UUID_LEN,
22932295
GFP_KERNEL);
22942296
if (namespace_label_has(ndd, abstraction_guid))
22952297
nsblk->common.claim_class
22962298
= to_nvdimm_cclass(&nd_label->abstraction_guid);
22972299
if (!nsblk->uuid)
22982300
goto blk_err;
2299-
memcpy(name, nd_label->name, NSLABEL_NAME_LEN);
2301+
nsl_get_name(ndd, nd_label, name);
23002302
if (name[0]) {
2301-
nsblk->alt_name = kmemdup(name, NSLABEL_NAME_LEN,
2302-
GFP_KERNEL);
2303+
nsblk->alt_name = kmemdup(name, NSLABEL_NAME_LEN, GFP_KERNEL);
23032304
if (!nsblk->alt_name)
23042305
goto blk_err;
23052306
}
23062307
res = nsblk_add_resource(nd_region, ndd, nsblk,
2307-
__le64_to_cpu(nd_label->dpa));
2308+
nsl_get_dpa(ndd, nd_label));
23082309
if (!res)
23092310
goto blk_err;
23102311
nd_dbg_dpa(nd_region, ndd, res, "%d: assign\n", count);
@@ -2345,6 +2346,7 @@ static struct device **scan_labels(struct nd_region *nd_region)
23452346
struct device *dev, **devs = NULL;
23462347
struct nd_label_ent *label_ent, *e;
23472348
struct nd_mapping *nd_mapping = &nd_region->mapping[0];
2349+
struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
23482350
resource_size_t map_end = nd_mapping->start + nd_mapping->size - 1;
23492351

23502352
/* "safe" because create_namespace_pmem() might list_move() label_ent */
@@ -2355,17 +2357,17 @@ static struct device **scan_labels(struct nd_region *nd_region)
23552357

23562358
if (!nd_label)
23572359
continue;
2358-
flags = __le32_to_cpu(nd_label->flags);
2360+
flags = nsl_get_flags(ndd, nd_label);
23592361
if (is_nd_blk(&nd_region->dev)
23602362
== !!(flags & NSLABEL_FLAG_LOCAL))
23612363
/* pass, region matches label type */;
23622364
else
23632365
continue;
23642366

23652367
/* skip labels that describe extents outside of the region */
2366-
if (__le64_to_cpu(nd_label->dpa) < nd_mapping->start ||
2367-
__le64_to_cpu(nd_label->dpa) > map_end)
2368-
continue;
2368+
if (nsl_get_dpa(ndd, nd_label) < nd_mapping->start ||
2369+
nsl_get_dpa(ndd, nd_label) > map_end)
2370+
continue;
23692371

23702372
i = add_namespace_resource(nd_region, nd_label, devs, count);
23712373
if (i < 0)
@@ -2381,13 +2383,9 @@ static struct device **scan_labels(struct nd_region *nd_region)
23812383

23822384
if (is_nd_blk(&nd_region->dev))
23832385
dev = create_namespace_blk(nd_region, nd_label, count);
2384-
else {
2385-
struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
2386-
struct nd_namespace_index *nsindex;
2387-
2388-
nsindex = to_namespace_index(ndd, ndd->ns_current);
2389-
dev = create_namespace_pmem(nd_region, nsindex, nd_label);
2390-
}
2386+
else
2387+
dev = create_namespace_pmem(nd_region, nd_mapping,
2388+
nd_label);
23912389

23922390
if (IS_ERR(dev)) {
23932391
switch (PTR_ERR(dev)) {
@@ -2570,7 +2568,7 @@ static int init_active_labels(struct nd_region *nd_region)
25702568
break;
25712569
label = nd_label_active(ndd, j);
25722570
if (test_bit(NDD_NOBLK, &nvdimm->flags)) {
2573-
u32 flags = __le32_to_cpu(label->flags);
2571+
u32 flags = nsl_get_flags(ndd, label);
25742572

25752573
flags &= ~NSLABEL_FLAG_LOCAL;
25762574
label->flags = __cpu_to_le32(flags);

drivers/nvdimm/nd.h

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,72 @@ struct nvdimm_drvdata {
3535
struct kref kref;
3636
};
3737

38+
static inline const u8 *nsl_ref_name(struct nvdimm_drvdata *ndd,
39+
struct nd_namespace_label *nd_label)
40+
{
41+
return nd_label->name;
42+
}
43+
44+
static inline u8 *nsl_get_name(struct nvdimm_drvdata *ndd,
45+
struct nd_namespace_label *nd_label, u8 *name)
46+
{
47+
return memcpy(name, nd_label->name, NSLABEL_NAME_LEN);
48+
}
49+
50+
static inline u32 nsl_get_slot(struct nvdimm_drvdata *ndd,
51+
struct nd_namespace_label *nd_label)
52+
{
53+
return __le32_to_cpu(nd_label->slot);
54+
}
55+
56+
static inline u64 nsl_get_checksum(struct nvdimm_drvdata *ndd,
57+
struct nd_namespace_label *nd_label)
58+
{
59+
return __le64_to_cpu(nd_label->checksum);
60+
}
61+
62+
static inline u32 nsl_get_flags(struct nvdimm_drvdata *ndd,
63+
struct nd_namespace_label *nd_label)
64+
{
65+
return __le32_to_cpu(nd_label->flags);
66+
}
67+
68+
static inline u64 nsl_get_dpa(struct nvdimm_drvdata *ndd,
69+
struct nd_namespace_label *nd_label)
70+
{
71+
return __le64_to_cpu(nd_label->dpa);
72+
}
73+
74+
static inline u64 nsl_get_rawsize(struct nvdimm_drvdata *ndd,
75+
struct nd_namespace_label *nd_label)
76+
{
77+
return __le64_to_cpu(nd_label->rawsize);
78+
}
79+
80+
static inline u64 nsl_get_isetcookie(struct nvdimm_drvdata *ndd,
81+
struct nd_namespace_label *nd_label)
82+
{
83+
return __le64_to_cpu(nd_label->isetcookie);
84+
}
85+
86+
static inline u16 nsl_get_position(struct nvdimm_drvdata *ndd,
87+
struct nd_namespace_label *nd_label)
88+
{
89+
return __le16_to_cpu(nd_label->position);
90+
}
91+
92+
static inline u16 nsl_get_nlabel(struct nvdimm_drvdata *ndd,
93+
struct nd_namespace_label *nd_label)
94+
{
95+
return __le16_to_cpu(nd_label->nlabel);
96+
}
97+
98+
static inline u64 nsl_get_lbasize(struct nvdimm_drvdata *ndd,
99+
struct nd_namespace_label *nd_label)
100+
{
101+
return __le64_to_cpu(nd_label->lbasize);
102+
}
103+
38104
struct nd_region_data {
39105
int ns_count;
40106
int ns_active;

0 commit comments

Comments
 (0)