blob: 0c46ada027cf4c4e967631e89ea4b4545538956f [file] [log] [blame]
Vishal Verma5212e112015-06-25 04:20:32 -04001/*
2 * Block Translation Table
3 * Copyright (c) 2014-2015, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 */
14#include <linux/highmem.h>
15#include <linux/debugfs.h>
16#include <linux/blkdev.h>
17#include <linux/module.h>
18#include <linux/device.h>
19#include <linux/mutex.h>
20#include <linux/hdreg.h>
21#include <linux/genhd.h>
22#include <linux/sizes.h>
23#include <linux/ndctl.h>
24#include <linux/fs.h>
25#include <linux/nd.h>
26#include "btt.h"
27#include "nd.h"
28
29enum log_ent_request {
30 LOG_NEW_ENT = 0,
31 LOG_OLD_ENT
32};
33
Vishal Verma5212e112015-06-25 04:20:32 -040034static int arena_read_bytes(struct arena_info *arena, resource_size_t offset,
35 void *buf, size_t n)
36{
37 struct nd_btt *nd_btt = arena->nd_btt;
38 struct nd_namespace_common *ndns = nd_btt->ndns;
39
40 /* arena offsets are 4K from the base of the device */
41 offset += SZ_4K;
42 return nvdimm_read_bytes(ndns, offset, buf, n);
43}
44
45static int arena_write_bytes(struct arena_info *arena, resource_size_t offset,
46 void *buf, size_t n)
47{
48 struct nd_btt *nd_btt = arena->nd_btt;
49 struct nd_namespace_common *ndns = nd_btt->ndns;
50
51 /* arena offsets are 4K from the base of the device */
52 offset += SZ_4K;
53 return nvdimm_write_bytes(ndns, offset, buf, n);
54}
55
56static int btt_info_write(struct arena_info *arena, struct btt_sb *super)
57{
58 int ret;
59
60 ret = arena_write_bytes(arena, arena->info2off, super,
61 sizeof(struct btt_sb));
62 if (ret)
63 return ret;
64
65 return arena_write_bytes(arena, arena->infooff, super,
66 sizeof(struct btt_sb));
67}
68
69static int btt_info_read(struct arena_info *arena, struct btt_sb *super)
70{
71 WARN_ON(!super);
72 return arena_read_bytes(arena, arena->infooff, super,
73 sizeof(struct btt_sb));
74}
75
76/*
77 * 'raw' version of btt_map write
78 * Assumptions:
79 * mapping is in little-endian
80 * mapping contains 'E' and 'Z' flags as desired
81 */
82static int __btt_map_write(struct arena_info *arena, u32 lba, __le32 mapping)
83{
84 u64 ns_off = arena->mapoff + (lba * MAP_ENT_SIZE);
85
86 WARN_ON(lba >= arena->external_nlba);
87 return arena_write_bytes(arena, ns_off, &mapping, MAP_ENT_SIZE);
88}
89
90static int btt_map_write(struct arena_info *arena, u32 lba, u32 mapping,
91 u32 z_flag, u32 e_flag)
92{
93 u32 ze;
94 __le32 mapping_le;
95
96 /*
97 * This 'mapping' is supposed to be just the LBA mapping, without
98 * any flags set, so strip the flag bits.
99 */
100 mapping &= MAP_LBA_MASK;
101
102 ze = (z_flag << 1) + e_flag;
103 switch (ze) {
104 case 0:
105 /*
106 * We want to set neither of the Z or E flags, and
107 * in the actual layout, this means setting the bit
108 * positions of both to '1' to indicate a 'normal'
109 * map entry
110 */
111 mapping |= MAP_ENT_NORMAL;
112 break;
113 case 1:
114 mapping |= (1 << MAP_ERR_SHIFT);
115 break;
116 case 2:
117 mapping |= (1 << MAP_TRIM_SHIFT);
118 break;
119 default:
120 /*
121 * The case where Z and E are both sent in as '1' could be
122 * construed as a valid 'normal' case, but we decide not to,
123 * to avoid confusion
124 */
125 WARN_ONCE(1, "Invalid use of Z and E flags\n");
126 return -EIO;
127 }
128
129 mapping_le = cpu_to_le32(mapping);
130 return __btt_map_write(arena, lba, mapping_le);
131}
132
133static int btt_map_read(struct arena_info *arena, u32 lba, u32 *mapping,
134 int *trim, int *error)
135{
136 int ret;
137 __le32 in;
138 u32 raw_mapping, postmap, ze, z_flag, e_flag;
139 u64 ns_off = arena->mapoff + (lba * MAP_ENT_SIZE);
140
141 WARN_ON(lba >= arena->external_nlba);
142
143 ret = arena_read_bytes(arena, ns_off, &in, MAP_ENT_SIZE);
144 if (ret)
145 return ret;
146
147 raw_mapping = le32_to_cpu(in);
148
149 z_flag = (raw_mapping & MAP_TRIM_MASK) >> MAP_TRIM_SHIFT;
150 e_flag = (raw_mapping & MAP_ERR_MASK) >> MAP_ERR_SHIFT;
151 ze = (z_flag << 1) + e_flag;
152 postmap = raw_mapping & MAP_LBA_MASK;
153
154 /* Reuse the {z,e}_flag variables for *trim and *error */
155 z_flag = 0;
156 e_flag = 0;
157
158 switch (ze) {
159 case 0:
160 /* Initial state. Return postmap = premap */
161 *mapping = lba;
162 break;
163 case 1:
164 *mapping = postmap;
165 e_flag = 1;
166 break;
167 case 2:
168 *mapping = postmap;
169 z_flag = 1;
170 break;
171 case 3:
172 *mapping = postmap;
173 break;
174 default:
175 return -EIO;
176 }
177
178 if (trim)
179 *trim = z_flag;
180 if (error)
181 *error = e_flag;
182
183 return ret;
184}
185
Vishal Vermac9ca9d92017-12-18 09:28:39 -0700186static int btt_log_group_read(struct arena_info *arena, u32 lane,
187 struct log_group *log)
Vishal Verma5212e112015-06-25 04:20:32 -0400188{
Vishal Vermac9ca9d92017-12-18 09:28:39 -0700189 WARN_ON(!log);
Vishal Verma5212e112015-06-25 04:20:32 -0400190 return arena_read_bytes(arena,
Vishal Vermac9ca9d92017-12-18 09:28:39 -0700191 arena->logoff + (lane * LOG_GRP_SIZE), log,
192 LOG_GRP_SIZE);
Vishal Verma5212e112015-06-25 04:20:32 -0400193}
194
195static struct dentry *debugfs_root;
196
197static void arena_debugfs_init(struct arena_info *a, struct dentry *parent,
198 int idx)
199{
200 char dirname[32];
201 struct dentry *d;
202
203 /* If for some reason, parent bttN was not created, exit */
204 if (!parent)
205 return;
206
207 snprintf(dirname, 32, "arena%d", idx);
208 d = debugfs_create_dir(dirname, parent);
209 if (IS_ERR_OR_NULL(d))
210 return;
211 a->debugfs_dir = d;
212
213 debugfs_create_x64("size", S_IRUGO, d, &a->size);
214 debugfs_create_x64("external_lba_start", S_IRUGO, d,
215 &a->external_lba_start);
216 debugfs_create_x32("internal_nlba", S_IRUGO, d, &a->internal_nlba);
217 debugfs_create_u32("internal_lbasize", S_IRUGO, d,
218 &a->internal_lbasize);
219 debugfs_create_x32("external_nlba", S_IRUGO, d, &a->external_nlba);
220 debugfs_create_u32("external_lbasize", S_IRUGO, d,
221 &a->external_lbasize);
222 debugfs_create_u32("nfree", S_IRUGO, d, &a->nfree);
223 debugfs_create_u16("version_major", S_IRUGO, d, &a->version_major);
224 debugfs_create_u16("version_minor", S_IRUGO, d, &a->version_minor);
225 debugfs_create_x64("nextoff", S_IRUGO, d, &a->nextoff);
226 debugfs_create_x64("infooff", S_IRUGO, d, &a->infooff);
227 debugfs_create_x64("dataoff", S_IRUGO, d, &a->dataoff);
228 debugfs_create_x64("mapoff", S_IRUGO, d, &a->mapoff);
229 debugfs_create_x64("logoff", S_IRUGO, d, &a->logoff);
230 debugfs_create_x64("info2off", S_IRUGO, d, &a->info2off);
231 debugfs_create_x32("flags", S_IRUGO, d, &a->flags);
Vishal Vermac9ca9d92017-12-18 09:28:39 -0700232 debugfs_create_u32("log_index_0", S_IRUGO, d, &a->log_index[0]);
233 debugfs_create_u32("log_index_1", S_IRUGO, d, &a->log_index[1]);
Vishal Verma5212e112015-06-25 04:20:32 -0400234}
235
236static void btt_debugfs_init(struct btt *btt)
237{
238 int i = 0;
239 struct arena_info *arena;
240
241 btt->debugfs_dir = debugfs_create_dir(dev_name(&btt->nd_btt->dev),
242 debugfs_root);
243 if (IS_ERR_OR_NULL(btt->debugfs_dir))
244 return;
245
246 list_for_each_entry(arena, &btt->arena_list, list) {
247 arena_debugfs_init(arena, btt->debugfs_dir, i);
248 i++;
249 }
250}
251
Vishal Vermac9ca9d92017-12-18 09:28:39 -0700252static u32 log_seq(struct log_group *log, int log_idx)
253{
254 return le32_to_cpu(log->ent[log_idx].seq);
255}
256
Vishal Verma5212e112015-06-25 04:20:32 -0400257/*
258 * This function accepts two log entries, and uses the
259 * sequence number to find the 'older' entry.
260 * It also updates the sequence number in this old entry to
261 * make it the 'new' one if the mark_flag is set.
262 * Finally, it returns which of the entries was the older one.
263 *
264 * TODO The logic feels a bit kludge-y. make it better..
265 */
Vishal Vermac9ca9d92017-12-18 09:28:39 -0700266static int btt_log_get_old(struct arena_info *a, struct log_group *log)
Vishal Verma5212e112015-06-25 04:20:32 -0400267{
Vishal Vermac9ca9d92017-12-18 09:28:39 -0700268 int idx0 = a->log_index[0];
269 int idx1 = a->log_index[1];
Vishal Verma5212e112015-06-25 04:20:32 -0400270 int old;
271
272 /*
273 * the first ever time this is seen, the entry goes into [0]
274 * the next time, the following logic works out to put this
275 * (next) entry into [1]
276 */
Vishal Vermac9ca9d92017-12-18 09:28:39 -0700277 if (log_seq(log, idx0) == 0) {
278 log->ent[idx0].seq = cpu_to_le32(1);
Vishal Verma5212e112015-06-25 04:20:32 -0400279 return 0;
280 }
281
Vishal Vermac9ca9d92017-12-18 09:28:39 -0700282 if (log_seq(log, idx0) == log_seq(log, idx1))
Vishal Verma5212e112015-06-25 04:20:32 -0400283 return -EINVAL;
Vishal Vermac9ca9d92017-12-18 09:28:39 -0700284 if (log_seq(log, idx0) + log_seq(log, idx1) > 5)
Vishal Verma5212e112015-06-25 04:20:32 -0400285 return -EINVAL;
286
Vishal Vermac9ca9d92017-12-18 09:28:39 -0700287 if (log_seq(log, idx0) < log_seq(log, idx1)) {
288 if ((log_seq(log, idx1) - log_seq(log, idx0)) == 1)
Vishal Verma5212e112015-06-25 04:20:32 -0400289 old = 0;
290 else
291 old = 1;
292 } else {
Vishal Vermac9ca9d92017-12-18 09:28:39 -0700293 if ((log_seq(log, idx0) - log_seq(log, idx1)) == 1)
Vishal Verma5212e112015-06-25 04:20:32 -0400294 old = 1;
295 else
296 old = 0;
297 }
298
299 return old;
300}
301
302static struct device *to_dev(struct arena_info *arena)
303{
304 return &arena->nd_btt->dev;
305}
306
307/*
308 * This function copies the desired (old/new) log entry into ent if
309 * it is not NULL. It returns the sub-slot number (0 or 1)
310 * where the desired log entry was found. Negative return values
311 * indicate errors.
312 */
313static int btt_log_read(struct arena_info *arena, u32 lane,
314 struct log_entry *ent, int old_flag)
315{
316 int ret;
317 int old_ent, ret_ent;
Vishal Vermac9ca9d92017-12-18 09:28:39 -0700318 struct log_group log;
Vishal Verma5212e112015-06-25 04:20:32 -0400319
Vishal Vermac9ca9d92017-12-18 09:28:39 -0700320 ret = btt_log_group_read(arena, lane, &log);
Vishal Verma5212e112015-06-25 04:20:32 -0400321 if (ret)
322 return -EIO;
323
Vishal Vermac9ca9d92017-12-18 09:28:39 -0700324 old_ent = btt_log_get_old(arena, &log);
Vishal Verma5212e112015-06-25 04:20:32 -0400325 if (old_ent < 0 || old_ent > 1) {
326 dev_info(to_dev(arena),
327 "log corruption (%d): lane %d seq [%d, %d]\n",
Vishal Vermac9ca9d92017-12-18 09:28:39 -0700328 old_ent, lane, log.ent[arena->log_index[0]].seq,
329 log.ent[arena->log_index[1]].seq);
Vishal Verma5212e112015-06-25 04:20:32 -0400330 /* TODO set error state? */
331 return -EIO;
332 }
333
334 ret_ent = (old_flag ? old_ent : (1 - old_ent));
335
336 if (ent != NULL)
Vishal Vermac9ca9d92017-12-18 09:28:39 -0700337 memcpy(ent, &log.ent[arena->log_index[ret_ent]], LOG_ENT_SIZE);
Vishal Verma5212e112015-06-25 04:20:32 -0400338
339 return ret_ent;
340}
341
342/*
343 * This function commits a log entry to media
344 * It does _not_ prepare the freelist entry for the next write
345 * btt_flog_write is the wrapper for updating the freelist elements
346 */
347static int __btt_log_write(struct arena_info *arena, u32 lane,
348 u32 sub, struct log_entry *ent)
349{
350 int ret;
Vishal Vermac9ca9d92017-12-18 09:28:39 -0700351 u32 group_slot = arena->log_index[sub];
352 unsigned int log_half = LOG_ENT_SIZE / 2;
Vishal Verma5212e112015-06-25 04:20:32 -0400353 void *src = ent;
Vishal Vermac9ca9d92017-12-18 09:28:39 -0700354 u64 ns_off;
Vishal Verma5212e112015-06-25 04:20:32 -0400355
Vishal Vermac9ca9d92017-12-18 09:28:39 -0700356 ns_off = arena->logoff + (lane * LOG_GRP_SIZE) +
357 (group_slot * LOG_ENT_SIZE);
Vishal Verma5212e112015-06-25 04:20:32 -0400358 /* split the 16B write into atomic, durable halves */
359 ret = arena_write_bytes(arena, ns_off, src, log_half);
360 if (ret)
361 return ret;
362
363 ns_off += log_half;
364 src += log_half;
365 return arena_write_bytes(arena, ns_off, src, log_half);
366}
367
368static int btt_flog_write(struct arena_info *arena, u32 lane, u32 sub,
369 struct log_entry *ent)
370{
371 int ret;
372
373 ret = __btt_log_write(arena, lane, sub, ent);
374 if (ret)
375 return ret;
376
377 /* prepare the next free entry */
378 arena->freelist[lane].sub = 1 - arena->freelist[lane].sub;
379 if (++(arena->freelist[lane].seq) == 4)
380 arena->freelist[lane].seq = 1;
381 arena->freelist[lane].block = le32_to_cpu(ent->old_map);
382
383 return ret;
384}
385
386/*
387 * This function initializes the BTT map to the initial state, which is
388 * all-zeroes, and indicates an identity mapping
389 */
390static int btt_map_init(struct arena_info *arena)
391{
392 int ret = -EINVAL;
393 void *zerobuf;
394 size_t offset = 0;
395 size_t chunk_size = SZ_2M;
396 size_t mapsize = arena->logoff - arena->mapoff;
397
398 zerobuf = kzalloc(chunk_size, GFP_KERNEL);
399 if (!zerobuf)
400 return -ENOMEM;
401
402 while (mapsize) {
403 size_t size = min(mapsize, chunk_size);
404
405 ret = arena_write_bytes(arena, arena->mapoff + offset, zerobuf,
406 size);
407 if (ret)
408 goto free;
409
410 offset += size;
411 mapsize -= size;
412 cond_resched();
413 }
414
415 free:
416 kfree(zerobuf);
417 return ret;
418}
419
420/*
421 * This function initializes the BTT log with 'fake' entries pointing
422 * to the initial reserved set of blocks as being free
423 */
424static int btt_log_init(struct arena_info *arena)
425{
426 int ret;
427 u32 i;
Vishal Vermac9ca9d92017-12-18 09:28:39 -0700428 struct log_entry ent, zerolog;
Vishal Verma5212e112015-06-25 04:20:32 -0400429
430 memset(&zerolog, 0, sizeof(zerolog));
431
432 for (i = 0; i < arena->nfree; i++) {
Vishal Vermac9ca9d92017-12-18 09:28:39 -0700433 ent.lba = cpu_to_le32(i);
434 ent.old_map = cpu_to_le32(arena->external_nlba + i);
435 ent.new_map = cpu_to_le32(arena->external_nlba + i);
436 ent.seq = cpu_to_le32(LOG_SEQ_INIT);
437 ret = __btt_log_write(arena, i, 0, &ent);
Vishal Verma5212e112015-06-25 04:20:32 -0400438 if (ret)
439 return ret;
440 ret = __btt_log_write(arena, i, 1, &zerolog);
441 if (ret)
442 return ret;
443 }
444
445 return 0;
446}
447
448static int btt_freelist_init(struct arena_info *arena)
449{
450 int old, new, ret;
451 u32 i, map_entry;
452 struct log_entry log_new, log_old;
453
454 arena->freelist = kcalloc(arena->nfree, sizeof(struct free_entry),
455 GFP_KERNEL);
456 if (!arena->freelist)
457 return -ENOMEM;
458
459 for (i = 0; i < arena->nfree; i++) {
460 old = btt_log_read(arena, i, &log_old, LOG_OLD_ENT);
461 if (old < 0)
462 return old;
463
464 new = btt_log_read(arena, i, &log_new, LOG_NEW_ENT);
465 if (new < 0)
466 return new;
467
468 /* sub points to the next one to be overwritten */
469 arena->freelist[i].sub = 1 - new;
470 arena->freelist[i].seq = nd_inc_seq(le32_to_cpu(log_new.seq));
471 arena->freelist[i].block = le32_to_cpu(log_new.old_map);
472
473 /* This implies a newly created or untouched flog entry */
474 if (log_new.old_map == log_new.new_map)
475 continue;
476
477 /* Check if map recovery is needed */
478 ret = btt_map_read(arena, le32_to_cpu(log_new.lba), &map_entry,
479 NULL, NULL);
480 if (ret)
481 return ret;
482 if ((le32_to_cpu(log_new.new_map) != map_entry) &&
483 (le32_to_cpu(log_new.old_map) == map_entry)) {
484 /*
485 * Last transaction wrote the flog, but wasn't able
486 * to complete the map write. So fix up the map.
487 */
488 ret = btt_map_write(arena, le32_to_cpu(log_new.lba),
489 le32_to_cpu(log_new.new_map), 0, 0);
490 if (ret)
491 return ret;
492 }
493
494 }
495
496 return 0;
497}
498
Vishal Vermac9ca9d92017-12-18 09:28:39 -0700499static bool ent_is_padding(struct log_entry *ent)
500{
501 return (ent->lba == 0) && (ent->old_map == 0) && (ent->new_map == 0)
502 && (ent->seq == 0);
503}
504
505/*
506 * Detecting valid log indices: We read a log group (see the comments in btt.h
507 * for a description of a 'log_group' and its 'slots'), and iterate over its
508 * four slots. We expect that a padding slot will be all-zeroes, and use this
509 * to detect a padding slot vs. an actual entry.
510 *
511 * If a log_group is in the initial state, i.e. hasn't been used since the
512 * creation of this BTT layout, it will have three of the four slots with
513 * zeroes. We skip over these log_groups for the detection of log_index. If
514 * all log_groups are in the initial state (i.e. the BTT has never been
515 * written to), it is safe to assume the 'new format' of log entries in slots
516 * (0, 1).
517 */
518static int log_set_indices(struct arena_info *arena)
519{
520 bool idx_set = false, initial_state = true;
521 int ret, log_index[2] = {-1, -1};
522 u32 i, j, next_idx = 0;
523 struct log_group log;
524 u32 pad_count = 0;
525
526 for (i = 0; i < arena->nfree; i++) {
527 ret = btt_log_group_read(arena, i, &log);
528 if (ret < 0)
529 return ret;
530
531 for (j = 0; j < 4; j++) {
532 if (!idx_set) {
533 if (ent_is_padding(&log.ent[j])) {
534 pad_count++;
535 continue;
536 } else {
537 /* Skip if index has been recorded */
538 if ((next_idx == 1) &&
539 (j == log_index[0]))
540 continue;
541 /* valid entry, record index */
542 log_index[next_idx] = j;
543 next_idx++;
544 }
545 if (next_idx == 2) {
546 /* two valid entries found */
547 idx_set = true;
548 } else if (next_idx > 2) {
549 /* too many valid indices */
550 return -ENXIO;
551 }
552 } else {
553 /*
554 * once the indices have been set, just verify
555 * that all subsequent log groups are either in
556 * their initial state or follow the same
557 * indices.
558 */
559 if (j == log_index[0]) {
560 /* entry must be 'valid' */
561 if (ent_is_padding(&log.ent[j]))
562 return -ENXIO;
563 } else if (j == log_index[1]) {
564 ;
565 /*
566 * log_index[1] can be padding if the
567 * lane never got used and it is still
568 * in the initial state (three 'padding'
569 * entries)
570 */
571 } else {
572 /* entry must be invalid (padding) */
573 if (!ent_is_padding(&log.ent[j]))
574 return -ENXIO;
575 }
576 }
577 }
578 /*
579 * If any of the log_groups have more than one valid,
580 * non-padding entry, then the we are no longer in the
581 * initial_state
582 */
583 if (pad_count < 3)
584 initial_state = false;
585 pad_count = 0;
586 }
587
588 if (!initial_state && !idx_set)
589 return -ENXIO;
590
591 /*
592 * If all the entries in the log were in the initial state,
593 * assume new padding scheme
594 */
595 if (initial_state)
596 log_index[1] = 1;
597
598 /*
599 * Only allow the known permutations of log/padding indices,
600 * i.e. (0, 1), and (0, 2)
601 */
602 if ((log_index[0] == 0) && ((log_index[1] == 1) || (log_index[1] == 2)))
603 ; /* known index possibilities */
604 else {
605 dev_err(to_dev(arena), "Found an unknown padding scheme\n");
606 return -ENXIO;
607 }
608
609 arena->log_index[0] = log_index[0];
610 arena->log_index[1] = log_index[1];
611 dev_dbg(to_dev(arena), "log_index_0 = %d\n", log_index[0]);
612 dev_dbg(to_dev(arena), "log_index_1 = %d\n", log_index[1]);
613 return 0;
614}
615
Vishal Verma5212e112015-06-25 04:20:32 -0400616static int btt_rtt_init(struct arena_info *arena)
617{
618 arena->rtt = kcalloc(arena->nfree, sizeof(u32), GFP_KERNEL);
619 if (arena->rtt == NULL)
620 return -ENOMEM;
621
622 return 0;
623}
624
625static int btt_maplocks_init(struct arena_info *arena)
626{
627 u32 i;
628
629 arena->map_locks = kcalloc(arena->nfree, sizeof(struct aligned_lock),
630 GFP_KERNEL);
631 if (!arena->map_locks)
632 return -ENOMEM;
633
634 for (i = 0; i < arena->nfree; i++)
635 spin_lock_init(&arena->map_locks[i].lock);
636
637 return 0;
638}
639
640static struct arena_info *alloc_arena(struct btt *btt, size_t size,
641 size_t start, size_t arena_off)
642{
643 struct arena_info *arena;
644 u64 logsize, mapsize, datasize;
645 u64 available = size;
646
647 arena = kzalloc(sizeof(struct arena_info), GFP_KERNEL);
648 if (!arena)
649 return NULL;
650 arena->nd_btt = btt->nd_btt;
651
652 if (!size)
653 return arena;
654
655 arena->size = size;
656 arena->external_lba_start = start;
657 arena->external_lbasize = btt->lbasize;
658 arena->internal_lbasize = roundup(arena->external_lbasize,
659 INT_LBASIZE_ALIGNMENT);
660 arena->nfree = BTT_DEFAULT_NFREE;
661 arena->version_major = 1;
662 arena->version_minor = 1;
663
664 if (available % BTT_PG_SIZE)
665 available -= (available % BTT_PG_SIZE);
666
667 /* Two pages are reserved for the super block and its copy */
668 available -= 2 * BTT_PG_SIZE;
669
670 /* The log takes a fixed amount of space based on nfree */
Vishal Vermac9ca9d92017-12-18 09:28:39 -0700671 logsize = roundup(arena->nfree * LOG_GRP_SIZE, BTT_PG_SIZE);
Vishal Verma5212e112015-06-25 04:20:32 -0400672 available -= logsize;
673
674 /* Calculate optimal split between map and data area */
675 arena->internal_nlba = div_u64(available - BTT_PG_SIZE,
676 arena->internal_lbasize + MAP_ENT_SIZE);
677 arena->external_nlba = arena->internal_nlba - arena->nfree;
678
679 mapsize = roundup((arena->external_nlba * MAP_ENT_SIZE), BTT_PG_SIZE);
680 datasize = available - mapsize;
681
682 /* 'Absolute' values, relative to start of storage space */
683 arena->infooff = arena_off;
684 arena->dataoff = arena->infooff + BTT_PG_SIZE;
685 arena->mapoff = arena->dataoff + datasize;
686 arena->logoff = arena->mapoff + mapsize;
687 arena->info2off = arena->logoff + logsize;
Vishal Vermac9ca9d92017-12-18 09:28:39 -0700688
689 /* Default log indices are (0,1) */
690 arena->log_index[0] = 0;
691 arena->log_index[1] = 1;
Vishal Verma5212e112015-06-25 04:20:32 -0400692 return arena;
693}
694
695static void free_arenas(struct btt *btt)
696{
697 struct arena_info *arena, *next;
698
699 list_for_each_entry_safe(arena, next, &btt->arena_list, list) {
700 list_del(&arena->list);
701 kfree(arena->rtt);
702 kfree(arena->map_locks);
703 kfree(arena->freelist);
704 debugfs_remove_recursive(arena->debugfs_dir);
705 kfree(arena);
706 }
707}
708
709/*
Vishal Verma5212e112015-06-25 04:20:32 -0400710 * This function reads an existing valid btt superblock and
711 * populates the corresponding arena_info struct
712 */
713static void parse_arena_meta(struct arena_info *arena, struct btt_sb *super,
714 u64 arena_off)
715{
716 arena->internal_nlba = le32_to_cpu(super->internal_nlba);
717 arena->internal_lbasize = le32_to_cpu(super->internal_lbasize);
718 arena->external_nlba = le32_to_cpu(super->external_nlba);
719 arena->external_lbasize = le32_to_cpu(super->external_lbasize);
720 arena->nfree = le32_to_cpu(super->nfree);
721 arena->version_major = le16_to_cpu(super->version_major);
722 arena->version_minor = le16_to_cpu(super->version_minor);
723
724 arena->nextoff = (super->nextoff == 0) ? 0 : (arena_off +
725 le64_to_cpu(super->nextoff));
726 arena->infooff = arena_off;
727 arena->dataoff = arena_off + le64_to_cpu(super->dataoff);
728 arena->mapoff = arena_off + le64_to_cpu(super->mapoff);
729 arena->logoff = arena_off + le64_to_cpu(super->logoff);
730 arena->info2off = arena_off + le64_to_cpu(super->info2off);
731
Dan Williams5e329402015-07-11 10:02:46 -0400732 arena->size = (le64_to_cpu(super->nextoff) > 0)
733 ? (le64_to_cpu(super->nextoff))
734 : (arena->info2off - arena->infooff + BTT_PG_SIZE);
Vishal Verma5212e112015-06-25 04:20:32 -0400735
736 arena->flags = le32_to_cpu(super->flags);
737}
738
739static int discover_arenas(struct btt *btt)
740{
741 int ret = 0;
742 struct arena_info *arena;
743 struct btt_sb *super;
744 size_t remaining = btt->rawsize;
745 u64 cur_nlba = 0;
746 size_t cur_off = 0;
747 int num_arenas = 0;
748
749 super = kzalloc(sizeof(*super), GFP_KERNEL);
750 if (!super)
751 return -ENOMEM;
752
753 while (remaining) {
754 /* Alloc memory for arena */
755 arena = alloc_arena(btt, 0, 0, 0);
756 if (!arena) {
757 ret = -ENOMEM;
758 goto out_super;
759 }
760
761 arena->infooff = cur_off;
762 ret = btt_info_read(arena, super);
763 if (ret)
764 goto out;
765
Vishal Vermaab45e762015-07-29 14:58:08 -0600766 if (!nd_btt_arena_is_valid(btt->nd_btt, super)) {
Vishal Verma5212e112015-06-25 04:20:32 -0400767 if (remaining == btt->rawsize) {
768 btt->init_state = INIT_NOTFOUND;
769 dev_info(to_dev(arena), "No existing arenas\n");
770 goto out;
771 } else {
772 dev_info(to_dev(arena),
773 "Found corrupted metadata!\n");
774 ret = -ENODEV;
775 goto out;
776 }
777 }
778
779 arena->external_lba_start = cur_nlba;
780 parse_arena_meta(arena, super, cur_off);
781
Vishal Vermac9ca9d92017-12-18 09:28:39 -0700782 ret = log_set_indices(arena);
783 if (ret) {
784 dev_err(to_dev(arena),
785 "Unable to deduce log/padding indices\n");
786 goto out;
787 }
788
Vishal Verma5212e112015-06-25 04:20:32 -0400789 ret = btt_freelist_init(arena);
790 if (ret)
791 goto out;
792
793 ret = btt_rtt_init(arena);
794 if (ret)
795 goto out;
796
797 ret = btt_maplocks_init(arena);
798 if (ret)
799 goto out;
800
801 list_add_tail(&arena->list, &btt->arena_list);
802
803 remaining -= arena->size;
804 cur_off += arena->size;
805 cur_nlba += arena->external_nlba;
806 num_arenas++;
807
808 if (arena->nextoff == 0)
809 break;
810 }
811 btt->num_arenas = num_arenas;
812 btt->nlba = cur_nlba;
813 btt->init_state = INIT_READY;
814
815 kfree(super);
816 return ret;
817
818 out:
819 kfree(arena);
820 free_arenas(btt);
821 out_super:
822 kfree(super);
823 return ret;
824}
825
826static int create_arenas(struct btt *btt)
827{
828 size_t remaining = btt->rawsize;
829 size_t cur_off = 0;
830
831 while (remaining) {
832 struct arena_info *arena;
833 size_t arena_size = min_t(u64, ARENA_MAX_SIZE, remaining);
834
835 remaining -= arena_size;
836 if (arena_size < ARENA_MIN_SIZE)
837 break;
838
839 arena = alloc_arena(btt, arena_size, btt->nlba, cur_off);
840 if (!arena) {
841 free_arenas(btt);
842 return -ENOMEM;
843 }
844 btt->nlba += arena->external_nlba;
845 if (remaining >= ARENA_MIN_SIZE)
846 arena->nextoff = arena->size;
847 else
848 arena->nextoff = 0;
849 cur_off += arena_size;
850 list_add_tail(&arena->list, &btt->arena_list);
851 }
852
853 return 0;
854}
855
856/*
857 * This function completes arena initialization by writing
858 * all the metadata.
859 * It is only called for an uninitialized arena when a write
860 * to that arena occurs for the first time.
861 */
Vishal Vermafbde1412015-07-29 14:58:07 -0600862static int btt_arena_write_layout(struct arena_info *arena)
Vishal Verma5212e112015-06-25 04:20:32 -0400863{
864 int ret;
Dan Williamse1455742015-07-30 17:57:47 -0400865 u64 sum;
Vishal Verma5212e112015-06-25 04:20:32 -0400866 struct btt_sb *super;
Vishal Vermafbde1412015-07-29 14:58:07 -0600867 struct nd_btt *nd_btt = arena->nd_btt;
Vishal Verma6ec68952015-07-29 14:58:09 -0600868 const u8 *parent_uuid = nd_dev_to_uuid(&nd_btt->ndns->dev);
Vishal Verma5212e112015-06-25 04:20:32 -0400869
870 ret = btt_map_init(arena);
871 if (ret)
872 return ret;
873
874 ret = btt_log_init(arena);
875 if (ret)
876 return ret;
877
878 super = kzalloc(sizeof(struct btt_sb), GFP_NOIO);
879 if (!super)
880 return -ENOMEM;
881
882 strncpy(super->signature, BTT_SIG, BTT_SIG_LEN);
Vishal Vermafbde1412015-07-29 14:58:07 -0600883 memcpy(super->uuid, nd_btt->uuid, 16);
Vishal Verma6ec68952015-07-29 14:58:09 -0600884 memcpy(super->parent_uuid, parent_uuid, 16);
Vishal Verma5212e112015-06-25 04:20:32 -0400885 super->flags = cpu_to_le32(arena->flags);
886 super->version_major = cpu_to_le16(arena->version_major);
887 super->version_minor = cpu_to_le16(arena->version_minor);
888 super->external_lbasize = cpu_to_le32(arena->external_lbasize);
889 super->external_nlba = cpu_to_le32(arena->external_nlba);
890 super->internal_lbasize = cpu_to_le32(arena->internal_lbasize);
891 super->internal_nlba = cpu_to_le32(arena->internal_nlba);
892 super->nfree = cpu_to_le32(arena->nfree);
893 super->infosize = cpu_to_le32(sizeof(struct btt_sb));
894 super->nextoff = cpu_to_le64(arena->nextoff);
895 /*
896 * Subtract arena->infooff (arena start) so numbers are relative
897 * to 'this' arena
898 */
899 super->dataoff = cpu_to_le64(arena->dataoff - arena->infooff);
900 super->mapoff = cpu_to_le64(arena->mapoff - arena->infooff);
901 super->logoff = cpu_to_le64(arena->logoff - arena->infooff);
902 super->info2off = cpu_to_le64(arena->info2off - arena->infooff);
903
904 super->flags = 0;
Dan Williamse1455742015-07-30 17:57:47 -0400905 sum = nd_sb_checksum((struct nd_gen_sb *) super);
906 super->checksum = cpu_to_le64(sum);
Vishal Verma5212e112015-06-25 04:20:32 -0400907
908 ret = btt_info_write(arena, super);
909
910 kfree(super);
911 return ret;
912}
913
914/*
915 * This function completes the initialization for the BTT namespace
916 * such that it is ready to accept IOs
917 */
918static int btt_meta_init(struct btt *btt)
919{
920 int ret = 0;
921 struct arena_info *arena;
922
923 mutex_lock(&btt->init_lock);
924 list_for_each_entry(arena, &btt->arena_list, list) {
Vishal Vermafbde1412015-07-29 14:58:07 -0600925 ret = btt_arena_write_layout(arena);
Vishal Verma5212e112015-06-25 04:20:32 -0400926 if (ret)
927 goto unlock;
928
929 ret = btt_freelist_init(arena);
930 if (ret)
931 goto unlock;
932
933 ret = btt_rtt_init(arena);
934 if (ret)
935 goto unlock;
936
937 ret = btt_maplocks_init(arena);
938 if (ret)
939 goto unlock;
940 }
941
942 btt->init_state = INIT_READY;
943
944 unlock:
945 mutex_unlock(&btt->init_lock);
946 return ret;
947}
948
Vishal Verma41cd8b72015-06-25 04:21:52 -0400949static u32 btt_meta_size(struct btt *btt)
950{
951 return btt->lbasize - btt->sector_size;
952}
953
Vishal Verma5212e112015-06-25 04:20:32 -0400954/*
955 * This function calculates the arena in which the given LBA lies
956 * by doing a linear walk. This is acceptable since we expect only
957 * a few arenas. If we have backing devices that get much larger,
958 * we can construct a balanced binary tree of arenas at init time
959 * so that this range search becomes faster.
960 */
961static int lba_to_arena(struct btt *btt, sector_t sector, __u32 *premap,
962 struct arena_info **arena)
963{
964 struct arena_info *arena_list;
965 __u64 lba = div_u64(sector << SECTOR_SHIFT, btt->sector_size);
966
967 list_for_each_entry(arena_list, &btt->arena_list, list) {
968 if (lba < arena_list->external_nlba) {
969 *arena = arena_list;
970 *premap = lba;
971 return 0;
972 }
973 lba -= arena_list->external_nlba;
974 }
975
976 return -EIO;
977}
978
979/*
980 * The following (lock_map, unlock_map) are mostly just to improve
981 * readability, since they index into an array of locks
982 */
983static void lock_map(struct arena_info *arena, u32 premap)
984 __acquires(&arena->map_locks[idx].lock)
985{
986 u32 idx = (premap * MAP_ENT_SIZE / L1_CACHE_BYTES) % arena->nfree;
987
988 spin_lock(&arena->map_locks[idx].lock);
989}
990
991static void unlock_map(struct arena_info *arena, u32 premap)
992 __releases(&arena->map_locks[idx].lock)
993{
994 u32 idx = (premap * MAP_ENT_SIZE / L1_CACHE_BYTES) % arena->nfree;
995
996 spin_unlock(&arena->map_locks[idx].lock);
997}
998
999static u64 to_namespace_offset(struct arena_info *arena, u64 lba)
1000{
1001 return arena->dataoff + ((u64)lba * arena->internal_lbasize);
1002}
1003
1004static int btt_data_read(struct arena_info *arena, struct page *page,
1005 unsigned int off, u32 lba, u32 len)
1006{
1007 int ret;
1008 u64 nsoff = to_namespace_offset(arena, lba);
1009 void *mem = kmap_atomic(page);
1010
1011 ret = arena_read_bytes(arena, nsoff, mem + off, len);
1012 kunmap_atomic(mem);
1013
1014 return ret;
1015}
1016
1017static int btt_data_write(struct arena_info *arena, u32 lba,
1018 struct page *page, unsigned int off, u32 len)
1019{
1020 int ret;
1021 u64 nsoff = to_namespace_offset(arena, lba);
1022 void *mem = kmap_atomic(page);
1023
1024 ret = arena_write_bytes(arena, nsoff, mem + off, len);
1025 kunmap_atomic(mem);
1026
1027 return ret;
1028}
1029
1030static void zero_fill_data(struct page *page, unsigned int off, u32 len)
1031{
1032 void *mem = kmap_atomic(page);
1033
1034 memset(mem + off, 0, len);
1035 kunmap_atomic(mem);
1036}
1037
Vishal Verma41cd8b72015-06-25 04:21:52 -04001038#ifdef CONFIG_BLK_DEV_INTEGRITY
1039static int btt_rw_integrity(struct btt *btt, struct bio_integrity_payload *bip,
1040 struct arena_info *arena, u32 postmap, int rw)
1041{
1042 unsigned int len = btt_meta_size(btt);
1043 u64 meta_nsoff;
1044 int ret = 0;
1045
1046 if (bip == NULL)
1047 return 0;
1048
1049 meta_nsoff = to_namespace_offset(arena, postmap) + btt->sector_size;
1050
1051 while (len) {
1052 unsigned int cur_len;
1053 struct bio_vec bv;
1054 void *mem;
1055
1056 bv = bvec_iter_bvec(bip->bip_vec, bip->bip_iter);
1057 /*
1058 * The 'bv' obtained from bvec_iter_bvec has its .bv_len and
1059 * .bv_offset already adjusted for iter->bi_bvec_done, and we
1060 * can use those directly
1061 */
1062
1063 cur_len = min(len, bv.bv_len);
1064 mem = kmap_atomic(bv.bv_page);
1065 if (rw)
1066 ret = arena_write_bytes(arena, meta_nsoff,
1067 mem + bv.bv_offset, cur_len);
1068 else
1069 ret = arena_read_bytes(arena, meta_nsoff,
1070 mem + bv.bv_offset, cur_len);
1071
1072 kunmap_atomic(mem);
1073 if (ret)
1074 return ret;
1075
1076 len -= cur_len;
1077 meta_nsoff += cur_len;
1078 bvec_iter_advance(bip->bip_vec, &bip->bip_iter, cur_len);
1079 }
1080
1081 return ret;
1082}
1083
1084#else /* CONFIG_BLK_DEV_INTEGRITY */
1085static int btt_rw_integrity(struct btt *btt, struct bio_integrity_payload *bip,
1086 struct arena_info *arena, u32 postmap, int rw)
1087{
1088 return 0;
1089}
1090#endif
1091
1092static int btt_read_pg(struct btt *btt, struct bio_integrity_payload *bip,
1093 struct page *page, unsigned int off, sector_t sector,
1094 unsigned int len)
Vishal Verma5212e112015-06-25 04:20:32 -04001095{
1096 int ret = 0;
1097 int t_flag, e_flag;
1098 struct arena_info *arena = NULL;
1099 u32 lane = 0, premap, postmap;
1100
1101 while (len) {
1102 u32 cur_len;
1103
1104 lane = nd_region_acquire_lane(btt->nd_region);
1105
1106 ret = lba_to_arena(btt, sector, &premap, &arena);
1107 if (ret)
1108 goto out_lane;
1109
1110 cur_len = min(btt->sector_size, len);
1111
1112 ret = btt_map_read(arena, premap, &postmap, &t_flag, &e_flag);
1113 if (ret)
1114 goto out_lane;
1115
1116 /*
1117 * We loop to make sure that the post map LBA didn't change
1118 * from under us between writing the RTT and doing the actual
1119 * read.
1120 */
1121 while (1) {
1122 u32 new_map;
1123
1124 if (t_flag) {
1125 zero_fill_data(page, off, cur_len);
1126 goto out_lane;
1127 }
1128
1129 if (e_flag) {
1130 ret = -EIO;
1131 goto out_lane;
1132 }
1133
1134 arena->rtt[lane] = RTT_VALID | postmap;
1135 /*
1136 * Barrier to make sure this write is not reordered
1137 * to do the verification map_read before the RTT store
1138 */
1139 barrier();
1140
1141 ret = btt_map_read(arena, premap, &new_map, &t_flag,
1142 &e_flag);
1143 if (ret)
1144 goto out_rtt;
1145
1146 if (postmap == new_map)
1147 break;
1148
1149 postmap = new_map;
1150 }
1151
1152 ret = btt_data_read(arena, page, off, postmap, cur_len);
1153 if (ret)
1154 goto out_rtt;
1155
Vishal Verma41cd8b72015-06-25 04:21:52 -04001156 if (bip) {
1157 ret = btt_rw_integrity(btt, bip, arena, postmap, READ);
1158 if (ret)
1159 goto out_rtt;
1160 }
1161
Vishal Verma5212e112015-06-25 04:20:32 -04001162 arena->rtt[lane] = RTT_INVALID;
1163 nd_region_release_lane(btt->nd_region, lane);
1164
1165 len -= cur_len;
1166 off += cur_len;
1167 sector += btt->sector_size >> SECTOR_SHIFT;
1168 }
1169
1170 return 0;
1171
1172 out_rtt:
1173 arena->rtt[lane] = RTT_INVALID;
1174 out_lane:
1175 nd_region_release_lane(btt->nd_region, lane);
1176 return ret;
1177}
1178
Vishal Verma41cd8b72015-06-25 04:21:52 -04001179static int btt_write_pg(struct btt *btt, struct bio_integrity_payload *bip,
1180 sector_t sector, struct page *page, unsigned int off,
1181 unsigned int len)
Vishal Verma5212e112015-06-25 04:20:32 -04001182{
1183 int ret = 0;
1184 struct arena_info *arena = NULL;
1185 u32 premap = 0, old_postmap, new_postmap, lane = 0, i;
1186 struct log_entry log;
1187 int sub;
1188
1189 while (len) {
1190 u32 cur_len;
1191
1192 lane = nd_region_acquire_lane(btt->nd_region);
1193
1194 ret = lba_to_arena(btt, sector, &premap, &arena);
1195 if (ret)
1196 goto out_lane;
1197 cur_len = min(btt->sector_size, len);
1198
1199 if ((arena->flags & IB_FLAG_ERROR_MASK) != 0) {
1200 ret = -EIO;
1201 goto out_lane;
1202 }
1203
1204 new_postmap = arena->freelist[lane].block;
1205
1206 /* Wait if the new block is being read from */
1207 for (i = 0; i < arena->nfree; i++)
1208 while (arena->rtt[i] == (RTT_VALID | new_postmap))
1209 cpu_relax();
1210
1211
1212 if (new_postmap >= arena->internal_nlba) {
1213 ret = -EIO;
1214 goto out_lane;
Vishal Verma41cd8b72015-06-25 04:21:52 -04001215 }
1216
1217 ret = btt_data_write(arena, new_postmap, page, off, cur_len);
Vishal Verma5212e112015-06-25 04:20:32 -04001218 if (ret)
1219 goto out_lane;
1220
Vishal Verma41cd8b72015-06-25 04:21:52 -04001221 if (bip) {
1222 ret = btt_rw_integrity(btt, bip, arena, new_postmap,
1223 WRITE);
1224 if (ret)
1225 goto out_lane;
1226 }
1227
Vishal Verma5212e112015-06-25 04:20:32 -04001228 lock_map(arena, premap);
1229 ret = btt_map_read(arena, premap, &old_postmap, NULL, NULL);
1230 if (ret)
1231 goto out_map;
1232 if (old_postmap >= arena->internal_nlba) {
1233 ret = -EIO;
1234 goto out_map;
1235 }
1236
1237 log.lba = cpu_to_le32(premap);
1238 log.old_map = cpu_to_le32(old_postmap);
1239 log.new_map = cpu_to_le32(new_postmap);
1240 log.seq = cpu_to_le32(arena->freelist[lane].seq);
1241 sub = arena->freelist[lane].sub;
1242 ret = btt_flog_write(arena, lane, sub, &log);
1243 if (ret)
1244 goto out_map;
1245
1246 ret = btt_map_write(arena, premap, new_postmap, 0, 0);
1247 if (ret)
1248 goto out_map;
1249
1250 unlock_map(arena, premap);
1251 nd_region_release_lane(btt->nd_region, lane);
1252
1253 len -= cur_len;
1254 off += cur_len;
1255 sector += btt->sector_size >> SECTOR_SHIFT;
1256 }
1257
1258 return 0;
1259
1260 out_map:
1261 unlock_map(arena, premap);
1262 out_lane:
1263 nd_region_release_lane(btt->nd_region, lane);
1264 return ret;
1265}
1266
Vishal Verma41cd8b72015-06-25 04:21:52 -04001267static int btt_do_bvec(struct btt *btt, struct bio_integrity_payload *bip,
1268 struct page *page, unsigned int len, unsigned int off,
Jens Axboec11f0c02016-08-05 08:11:04 -06001269 bool is_write, sector_t sector)
Vishal Verma5212e112015-06-25 04:20:32 -04001270{
1271 int ret;
1272
Jens Axboec11f0c02016-08-05 08:11:04 -06001273 if (!is_write) {
Vishal Verma41cd8b72015-06-25 04:21:52 -04001274 ret = btt_read_pg(btt, bip, page, off, sector, len);
Vishal Verma5212e112015-06-25 04:20:32 -04001275 flush_dcache_page(page);
1276 } else {
1277 flush_dcache_page(page);
Vishal Verma41cd8b72015-06-25 04:21:52 -04001278 ret = btt_write_pg(btt, bip, sector, page, off, len);
Vishal Verma5212e112015-06-25 04:20:32 -04001279 }
1280
1281 return ret;
1282}
1283
Jens Axboedece1632015-11-05 10:41:16 -07001284static blk_qc_t btt_make_request(struct request_queue *q, struct bio *bio)
Vishal Verma5212e112015-06-25 04:20:32 -04001285{
Vishal Verma41cd8b72015-06-25 04:21:52 -04001286 struct bio_integrity_payload *bip = bio_integrity(bio);
Vishal Verma5212e112015-06-25 04:20:32 -04001287 struct btt *btt = q->queuedata;
1288 struct bvec_iter iter;
Dan Williamsf0dc0892015-05-16 12:28:53 -04001289 unsigned long start;
Vishal Verma5212e112015-06-25 04:20:32 -04001290 struct bio_vec bvec;
Mike Christieabf54542016-08-04 14:23:34 -06001291 int err = 0;
Dan Williamsf0dc0892015-05-16 12:28:53 -04001292 bool do_acct;
Vishal Verma5212e112015-06-25 04:20:32 -04001293
Vishal Verma41cd8b72015-06-25 04:21:52 -04001294 /*
1295 * bio_integrity_enabled also checks if the bio already has an
1296 * integrity payload attached. If it does, we *don't* do a
1297 * bio_integrity_prep here - the payload has been generated by
1298 * another kernel subsystem, and we just pass it through.
1299 */
1300 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001301 bio->bi_error = -EIO;
Vishal Verma41cd8b72015-06-25 04:21:52 -04001302 goto out;
1303 }
1304
Dan Williamsf0dc0892015-05-16 12:28:53 -04001305 do_acct = nd_iostat_start(bio, &start);
Vishal Verma5212e112015-06-25 04:20:32 -04001306 bio_for_each_segment(bvec, bio, iter) {
1307 unsigned int len = bvec.bv_len;
1308
1309 BUG_ON(len > PAGE_SIZE);
1310 /* Make sure len is in multiples of sector size. */
1311 /* XXX is this right? */
1312 BUG_ON(len < btt->sector_size);
1313 BUG_ON(len % btt->sector_size);
1314
Vishal Verma41cd8b72015-06-25 04:21:52 -04001315 err = btt_do_bvec(btt, bip, bvec.bv_page, len, bvec.bv_offset,
Jens Axboec11f0c02016-08-05 08:11:04 -06001316 op_is_write(bio_op(bio)), iter.bi_sector);
Vishal Verma5212e112015-06-25 04:20:32 -04001317 if (err) {
1318 dev_info(&btt->nd_btt->dev,
1319 "io error in %s sector %lld, len %d,\n",
Mike Christieabf54542016-08-04 14:23:34 -06001320 (op_is_write(bio_op(bio))) ? "WRITE" :
1321 "READ",
Vishal Verma5212e112015-06-25 04:20:32 -04001322 (unsigned long long) iter.bi_sector, len);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001323 bio->bi_error = err;
Dan Williamsf0dc0892015-05-16 12:28:53 -04001324 break;
Vishal Verma5212e112015-06-25 04:20:32 -04001325 }
1326 }
Dan Williamsf0dc0892015-05-16 12:28:53 -04001327 if (do_acct)
1328 nd_iostat_end(bio, start);
Vishal Verma5212e112015-06-25 04:20:32 -04001329
1330out:
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001331 bio_endio(bio);
Jens Axboedece1632015-11-05 10:41:16 -07001332 return BLK_QC_T_NONE;
Vishal Verma5212e112015-06-25 04:20:32 -04001333}
1334
1335static int btt_rw_page(struct block_device *bdev, sector_t sector,
Jens Axboec11f0c02016-08-05 08:11:04 -06001336 struct page *page, bool is_write)
Vishal Verma5212e112015-06-25 04:20:32 -04001337{
1338 struct btt *btt = bdev->bd_disk->private_data;
Vishal Verma891c31e2017-06-29 16:59:11 -06001339 int rc;
Vishal Verma5212e112015-06-25 04:20:32 -04001340
Vishal Verma891c31e2017-06-29 16:59:11 -06001341 rc = btt_do_bvec(btt, NULL, page, PAGE_SIZE, 0, is_write, sector);
1342 if (rc == 0)
1343 page_endio(page, is_write, 0);
1344
1345 return rc;
Vishal Verma5212e112015-06-25 04:20:32 -04001346}
1347
1348
1349static int btt_getgeo(struct block_device *bd, struct hd_geometry *geo)
1350{
1351 /* some standard values */
1352 geo->heads = 1 << 6;
1353 geo->sectors = 1 << 5;
1354 geo->cylinders = get_capacity(bd->bd_disk) >> 11;
1355 return 0;
1356}
1357
1358static const struct block_device_operations btt_fops = {
1359 .owner = THIS_MODULE,
1360 .rw_page = btt_rw_page,
1361 .getgeo = btt_getgeo,
Dan Williams58138822015-06-23 20:08:34 -04001362 .revalidate_disk = nvdimm_revalidate_disk,
Vishal Verma5212e112015-06-25 04:20:32 -04001363};
1364
1365static int btt_blk_init(struct btt *btt)
1366{
1367 struct nd_btt *nd_btt = btt->nd_btt;
1368 struct nd_namespace_common *ndns = nd_btt->ndns;
1369
1370 /* create a new disk and request queue for btt */
1371 btt->btt_queue = blk_alloc_queue(GFP_KERNEL);
1372 if (!btt->btt_queue)
1373 return -ENOMEM;
1374
1375 btt->btt_disk = alloc_disk(0);
1376 if (!btt->btt_disk) {
1377 blk_cleanup_queue(btt->btt_queue);
1378 return -ENOMEM;
1379 }
1380
1381 nvdimm_namespace_disk_name(ndns, btt->btt_disk->disk_name);
Vishal Verma5212e112015-06-25 04:20:32 -04001382 btt->btt_disk->first_minor = 0;
1383 btt->btt_disk->fops = &btt_fops;
1384 btt->btt_disk->private_data = btt;
1385 btt->btt_disk->queue = btt->btt_queue;
1386 btt->btt_disk->flags = GENHD_FL_EXT_DEVT;
1387
1388 blk_queue_make_request(btt->btt_queue, btt_make_request);
1389 blk_queue_logical_block_size(btt->btt_queue, btt->sector_size);
1390 blk_queue_max_hw_sectors(btt->btt_queue, UINT_MAX);
1391 blk_queue_bounce_limit(btt->btt_queue, BLK_BOUNCE_ANY);
1392 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, btt->btt_queue);
1393 btt->btt_queue->queuedata = btt;
1394
Vishal Verma41cd8b72015-06-25 04:21:52 -04001395 if (btt_meta_size(btt)) {
1396 int rc = nd_integrity_init(btt->btt_disk, btt_meta_size(btt));
1397
1398 if (rc) {
1399 del_gendisk(btt->btt_disk);
1400 put_disk(btt->btt_disk);
1401 blk_cleanup_queue(btt->btt_queue);
1402 return rc;
1403 }
1404 }
1405 set_capacity(btt->btt_disk, btt->nlba * btt->sector_size >> 9);
Vishal Verma6fa877d2018-03-05 16:56:13 -07001406 device_add_disk(&btt->nd_btt->dev, btt->btt_disk);
Vishal Vermaabe8b4e2016-07-27 16:38:59 -06001407 btt->nd_btt->size = btt->nlba * (u64)btt->sector_size;
Dan Williams58138822015-06-23 20:08:34 -04001408 revalidate_disk(btt->btt_disk);
Vishal Verma5212e112015-06-25 04:20:32 -04001409
1410 return 0;
1411}
1412
1413static void btt_blk_cleanup(struct btt *btt)
1414{
Vishal Verma5212e112015-06-25 04:20:32 -04001415 del_gendisk(btt->btt_disk);
1416 put_disk(btt->btt_disk);
1417 blk_cleanup_queue(btt->btt_queue);
1418}
1419
1420/**
1421 * btt_init - initialize a block translation table for the given device
1422 * @nd_btt: device with BTT geometry and backing device info
1423 * @rawsize: raw size in bytes of the backing device
1424 * @lbasize: lba size of the backing device
1425 * @uuid: A uuid for the backing device - this is stored on media
1426 * @maxlane: maximum number of parallel requests the device can handle
1427 *
1428 * Initialize a Block Translation Table on a backing device to provide
1429 * single sector power fail atomicity.
1430 *
1431 * Context:
1432 * Might sleep.
1433 *
1434 * Returns:
1435 * Pointer to a new struct btt on success, NULL on failure.
1436 */
1437static struct btt *btt_init(struct nd_btt *nd_btt, unsigned long long rawsize,
1438 u32 lbasize, u8 *uuid, struct nd_region *nd_region)
1439{
1440 int ret;
1441 struct btt *btt;
1442 struct device *dev = &nd_btt->dev;
1443
Dan Williamse32bc722016-03-17 18:23:09 -07001444 btt = devm_kzalloc(dev, sizeof(struct btt), GFP_KERNEL);
Vishal Verma5212e112015-06-25 04:20:32 -04001445 if (!btt)
1446 return NULL;
1447
1448 btt->nd_btt = nd_btt;
1449 btt->rawsize = rawsize;
1450 btt->lbasize = lbasize;
1451 btt->sector_size = ((lbasize >= 4096) ? 4096 : 512);
1452 INIT_LIST_HEAD(&btt->arena_list);
1453 mutex_init(&btt->init_lock);
1454 btt->nd_region = nd_region;
1455
1456 ret = discover_arenas(btt);
1457 if (ret) {
1458 dev_err(dev, "init: error in arena_discover: %d\n", ret);
Dan Williamse32bc722016-03-17 18:23:09 -07001459 return NULL;
Vishal Verma5212e112015-06-25 04:20:32 -04001460 }
1461
Dan Williams58138822015-06-23 20:08:34 -04001462 if (btt->init_state != INIT_READY && nd_region->ro) {
1463 dev_info(dev, "%s is read-only, unable to init btt metadata\n",
1464 dev_name(&nd_region->dev));
Dan Williamse32bc722016-03-17 18:23:09 -07001465 return NULL;
Dan Williams58138822015-06-23 20:08:34 -04001466 } else if (btt->init_state != INIT_READY) {
Vishal Verma5212e112015-06-25 04:20:32 -04001467 btt->num_arenas = (rawsize / ARENA_MAX_SIZE) +
1468 ((rawsize % ARENA_MAX_SIZE) ? 1 : 0);
1469 dev_dbg(dev, "init: %d arenas for %llu rawsize\n",
1470 btt->num_arenas, rawsize);
1471
1472 ret = create_arenas(btt);
1473 if (ret) {
1474 dev_info(dev, "init: create_arenas: %d\n", ret);
Dan Williamse32bc722016-03-17 18:23:09 -07001475 return NULL;
Vishal Verma5212e112015-06-25 04:20:32 -04001476 }
1477
1478 ret = btt_meta_init(btt);
1479 if (ret) {
1480 dev_err(dev, "init: error in meta_init: %d\n", ret);
Dan Williamse32bc722016-03-17 18:23:09 -07001481 return NULL;
Vishal Verma5212e112015-06-25 04:20:32 -04001482 }
1483 }
1484
1485 ret = btt_blk_init(btt);
1486 if (ret) {
1487 dev_err(dev, "init: error in blk_init: %d\n", ret);
Dan Williamse32bc722016-03-17 18:23:09 -07001488 return NULL;
Vishal Verma5212e112015-06-25 04:20:32 -04001489 }
1490
1491 btt_debugfs_init(btt);
1492
1493 return btt;
Vishal Verma5212e112015-06-25 04:20:32 -04001494}
1495
1496/**
1497 * btt_fini - de-initialize a BTT
1498 * @btt: the BTT handle that was generated by btt_init
1499 *
1500 * De-initialize a Block Translation Table on device removal
1501 *
1502 * Context:
1503 * Might sleep.
1504 */
1505static void btt_fini(struct btt *btt)
1506{
1507 if (btt) {
1508 btt_blk_cleanup(btt);
1509 free_arenas(btt);
1510 debugfs_remove_recursive(btt->debugfs_dir);
Vishal Verma5212e112015-06-25 04:20:32 -04001511 }
1512}
1513
1514int nvdimm_namespace_attach_btt(struct nd_namespace_common *ndns)
1515{
1516 struct nd_btt *nd_btt = to_nd_btt(ndns->claim);
1517 struct nd_region *nd_region;
1518 struct btt *btt;
1519 size_t rawsize;
1520
Dan Williams9dec4892016-04-22 12:26:05 -07001521 if (!nd_btt->uuid || !nd_btt->ndns || !nd_btt->lbasize) {
1522 dev_dbg(&nd_btt->dev, "incomplete btt configuration\n");
Vishal Verma5212e112015-06-25 04:20:32 -04001523 return -ENODEV;
Dan Williams9dec4892016-04-22 12:26:05 -07001524 }
Vishal Verma5212e112015-06-25 04:20:32 -04001525
1526 rawsize = nvdimm_namespace_capacity(ndns) - SZ_4K;
1527 if (rawsize < ARENA_MIN_SIZE) {
Dan Williams9dec4892016-04-22 12:26:05 -07001528 dev_dbg(&nd_btt->dev, "%s must be at least %ld bytes\n",
1529 dev_name(&ndns->dev), ARENA_MIN_SIZE + SZ_4K);
Vishal Verma5212e112015-06-25 04:20:32 -04001530 return -ENXIO;
1531 }
1532 nd_region = to_nd_region(nd_btt->dev.parent);
1533 btt = btt_init(nd_btt, rawsize, nd_btt->lbasize, nd_btt->uuid,
1534 nd_region);
1535 if (!btt)
1536 return -ENOMEM;
1537 nd_btt->btt = btt;
1538
1539 return 0;
1540}
1541EXPORT_SYMBOL(nvdimm_namespace_attach_btt);
1542
Dan Williams298f2bc2016-03-15 16:41:04 -07001543int nvdimm_namespace_detach_btt(struct nd_btt *nd_btt)
Vishal Verma5212e112015-06-25 04:20:32 -04001544{
Vishal Verma5212e112015-06-25 04:20:32 -04001545 struct btt *btt = nd_btt->btt;
1546
1547 btt_fini(btt);
1548 nd_btt->btt = NULL;
1549
1550 return 0;
1551}
1552EXPORT_SYMBOL(nvdimm_namespace_detach_btt);
1553
1554static int __init nd_btt_init(void)
1555{
NeilBrownff8e92d2016-03-10 08:59:28 +11001556 int rc = 0;
Vishal Verma5212e112015-06-25 04:20:32 -04001557
1558 debugfs_root = debugfs_create_dir("btt", NULL);
NeilBrownff8e92d2016-03-10 08:59:28 +11001559 if (IS_ERR_OR_NULL(debugfs_root))
Vishal Verma5212e112015-06-25 04:20:32 -04001560 rc = -ENXIO;
Vishal Verma5212e112015-06-25 04:20:32 -04001561
1562 return rc;
1563}
1564
1565static void __exit nd_btt_exit(void)
1566{
1567 debugfs_remove_recursive(debugfs_root);
Vishal Verma5212e112015-06-25 04:20:32 -04001568}
1569
1570MODULE_ALIAS_ND_DEVICE(ND_DEVICE_BTT);
1571MODULE_AUTHOR("Vishal Verma <vishal.l.verma@linux.intel.com>");
1572MODULE_LICENSE("GPL v2");
1573module_init(nd_btt_init);
1574module_exit(nd_btt_exit);