2 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 #include <linux/device.h>
14 #include <linux/ndctl.h>
21 static u32 best_seq(u32 a, u32 b)
23 a &= NSINDEX_SEQ_MASK;
24 b &= NSINDEX_SEQ_MASK;
30 else if (nd_inc_seq(a) == b)
36 size_t sizeof_namespace_index(struct nvdimm_drvdata *ndd)
40 if (ndd->nsindex_size)
41 return ndd->nsindex_size;
44 * The minimum index space is 512 bytes, with that amount of
45 * index we can describe ~1400 labels which is less than a byte
46 * of overhead per label. Round up to a byte of overhead per
47 * label and determine the size of the index region. Yes, this
48 * starts to waste space at larger config_sizes, but it's
49 * unlikely we'll ever see anything but 128K.
51 index_span = ndd->nsarea.config_size / 129;
52 index_span /= NSINDEX_ALIGN * 2;
53 ndd->nsindex_size = index_span * NSINDEX_ALIGN;
55 return ndd->nsindex_size;
58 int nd_label_validate(struct nvdimm_drvdata *ndd)
61 * On media label format consists of two index blocks followed
62 * by an array of labels. None of these structures are ever
63 * updated in place. A sequence number tracks the current
64 * active index and the next one to write, while labels are
65 * written to free slots.
87 struct nd_namespace_index *nsindex[] = {
88 to_namespace_index(ndd, 0),
89 to_namespace_index(ndd, 1),
91 const int num_index = ARRAY_SIZE(nsindex);
92 struct device *dev = ndd->dev;
93 bool valid[2] = { 0 };
97 for (i = 0; i < num_index; i++) {
99 u8 sig[NSINDEX_SIG_LEN];
100 u64 sum_save, sum, size;
102 memcpy(sig, nsindex[i]->sig, NSINDEX_SIG_LEN);
103 if (memcmp(sig, NSINDEX_SIGNATURE, NSINDEX_SIG_LEN) != 0) {
104 dev_dbg(dev, "%s: nsindex%d signature invalid\n",
108 sum_save = __le64_to_cpu(nsindex[i]->checksum);
109 nsindex[i]->checksum = __cpu_to_le64(0);
110 sum = nd_fletcher64(nsindex[i], sizeof_namespace_index(ndd), 1);
111 nsindex[i]->checksum = __cpu_to_le64(sum_save);
112 if (sum != sum_save) {
113 dev_dbg(dev, "%s: nsindex%d checksum invalid\n",
118 seq = __le32_to_cpu(nsindex[i]->seq);
119 if ((seq & NSINDEX_SEQ_MASK) == 0) {
120 dev_dbg(dev, "%s: nsindex%d sequence: %#x invalid\n",
125 /* sanity check the index against expected values */
126 if (__le64_to_cpu(nsindex[i]->myoff)
127 != i * sizeof_namespace_index(ndd)) {
128 dev_dbg(dev, "%s: nsindex%d myoff: %#llx invalid\n",
129 __func__, i, (unsigned long long)
130 __le64_to_cpu(nsindex[i]->myoff));
133 if (__le64_to_cpu(nsindex[i]->otheroff)
134 != (!i) * sizeof_namespace_index(ndd)) {
135 dev_dbg(dev, "%s: nsindex%d otheroff: %#llx invalid\n",
136 __func__, i, (unsigned long long)
137 __le64_to_cpu(nsindex[i]->otheroff));
141 size = __le64_to_cpu(nsindex[i]->mysize);
142 if (size > sizeof_namespace_index(ndd)
143 || size < sizeof(struct nd_namespace_index)) {
144 dev_dbg(dev, "%s: nsindex%d mysize: %#llx invalid\n",
149 nslot = __le32_to_cpu(nsindex[i]->nslot);
150 if (nslot * sizeof(struct nd_namespace_label)
151 + 2 * sizeof_namespace_index(ndd)
152 > ndd->nsarea.config_size) {
153 dev_dbg(dev, "%s: nsindex%d nslot: %u invalid, config_size: %#x\n",
155 ndd->nsarea.config_size);
166 for (i = 0; i < num_index; i++)
169 /* can't have num_valid > 0 but valid[] = { false, false } */
173 /* pick the best index... */
174 seq = best_seq(__le32_to_cpu(nsindex[0]->seq),
175 __le32_to_cpu(nsindex[1]->seq));
176 if (seq == (__le32_to_cpu(nsindex[1]->seq) & NSINDEX_SEQ_MASK))
186 void nd_label_copy(struct nvdimm_drvdata *ndd, struct nd_namespace_index *dst,
187 struct nd_namespace_index *src)
194 memcpy(dst, src, sizeof_namespace_index(ndd));
197 static struct nd_namespace_label *nd_label_base(struct nvdimm_drvdata *ndd)
199 void *base = to_namespace_index(ndd, 0);
201 return base + 2 * sizeof_namespace_index(ndd);
204 #define for_each_clear_bit_le(bit, addr, size) \
205 for ((bit) = find_next_zero_bit_le((addr), (size), 0); \
207 (bit) = find_next_zero_bit_le((addr), (size), (bit) + 1))
210 * preamble_current - common variable initialization for nd_label_* routines
211 * @ndd: dimm container for the relevant label set
212 * @nsindex_out: on return set to the currently active namespace index
213 * @free: on return set to the free label bitmap in the index
214 * @nslot: on return set to the number of slots in the label space
216 static bool preamble_current(struct nvdimm_drvdata *ndd,
217 struct nd_namespace_index **nsindex_out,
218 unsigned long **free, u32 *nslot)
220 struct nd_namespace_index *nsindex;
222 nsindex = to_current_namespace_index(ndd);
226 *free = (unsigned long *) nsindex->free;
227 *nslot = __le32_to_cpu(nsindex->nslot);
228 *nsindex_out = nsindex;
233 static char *nd_label_gen_id(struct nd_label_id *label_id, u8 *uuid, u32 flags)
235 if (!label_id || !uuid)
237 snprintf(label_id->id, ND_LABEL_ID_SIZE, "%s-%pUb",
238 flags & NSLABEL_FLAG_LOCAL ? "blk" : "pmem", uuid);
242 static bool slot_valid(struct nd_namespace_label *nd_label, u32 slot)
244 /* check that we are written where we expect to be written */
245 if (slot != __le32_to_cpu(nd_label->slot))
248 /* check that DPA allocations are page aligned */
249 if ((__le64_to_cpu(nd_label->dpa)
250 | __le64_to_cpu(nd_label->rawsize)) % SZ_4K)
256 int nd_label_reserve_dpa(struct nvdimm_drvdata *ndd)
258 struct nd_namespace_index *nsindex;
262 if (!preamble_current(ndd, &nsindex, &free, &nslot))
263 return 0; /* no label, nothing to reserve */
265 for_each_clear_bit_le(slot, free, nslot) {
266 struct nd_namespace_label *nd_label;
267 struct nd_region *nd_region = NULL;
268 u8 label_uuid[NSLABEL_UUID_LEN];
269 struct nd_label_id label_id;
270 struct resource *res;
273 nd_label = nd_label_base(ndd) + slot;
275 if (!slot_valid(nd_label, slot))
278 memcpy(label_uuid, nd_label->uuid, NSLABEL_UUID_LEN);
279 flags = __le32_to_cpu(nd_label->flags);
280 nd_label_gen_id(&label_id, label_uuid, flags);
281 res = nvdimm_allocate_dpa(ndd, &label_id,
282 __le64_to_cpu(nd_label->dpa),
283 __le64_to_cpu(nd_label->rawsize));
284 nd_dbg_dpa(nd_region, ndd, res, "reserve\n");