Blame view

drivers/nvdimm/btt.c 43 KB
2025cf9e1   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-only
5212e11fd   Vishal Verma   nd_btt: atomic se...
2
3
4
  /*
   * Block Translation Table
   * Copyright (c) 2014-2015, Intel Corporation.
5212e11fd   Vishal Verma   nd_btt: atomic se...
5
6
7
8
9
10
11
12
13
14
15
16
17
   */
  #include <linux/highmem.h>
  #include <linux/debugfs.h>
  #include <linux/blkdev.h>
  #include <linux/module.h>
  #include <linux/device.h>
  #include <linux/mutex.h>
  #include <linux/hdreg.h>
  #include <linux/genhd.h>
  #include <linux/sizes.h>
  #include <linux/ndctl.h>
  #include <linux/fs.h>
  #include <linux/nd.h>
23c47d2ad   Minchan Kim   bdi: introduce BD...
18
  #include <linux/backing-dev.h>
5212e11fd   Vishal Verma   nd_btt: atomic se...
19
20
21
22
23
24
25
  #include "btt.h"
  #include "nd.h"
  
  enum log_ent_request {
  	LOG_NEW_ENT = 0,
  	LOG_OLD_ENT
  };
86652d2eb   Vishal Verma   libnvdimm, btt: c...
26
27
28
29
  static struct device *to_dev(struct arena_info *arena)
  {
  	return &arena->nd_btt->dev;
  }
d9b83c756   Vishal Verma   libnvdimm, btt: r...
30
31
32
33
  static u64 adjust_initial_offset(struct nd_btt *nd_btt, u64 offset)
  {
  	return offset + nd_btt->initial_offset;
  }
5212e11fd   Vishal Verma   nd_btt: atomic se...
34
  static int arena_read_bytes(struct arena_info *arena, resource_size_t offset,
3ae3d67ba   Vishal Verma   libnvdimm: add an...
35
  		void *buf, size_t n, unsigned long flags)
5212e11fd   Vishal Verma   nd_btt: atomic se...
36
37
38
  {
  	struct nd_btt *nd_btt = arena->nd_btt;
  	struct nd_namespace_common *ndns = nd_btt->ndns;
14e494542   Vishal Verma   libnvdimm, btt: B...
39
  	/* arena offsets may be shifted from the base of the device */
d9b83c756   Vishal Verma   libnvdimm, btt: r...
40
  	offset = adjust_initial_offset(nd_btt, offset);
3ae3d67ba   Vishal Verma   libnvdimm: add an...
41
  	return nvdimm_read_bytes(ndns, offset, buf, n, flags);
5212e11fd   Vishal Verma   nd_btt: atomic se...
42
43
44
  }
  
  static int arena_write_bytes(struct arena_info *arena, resource_size_t offset,
3ae3d67ba   Vishal Verma   libnvdimm: add an...
45
  		void *buf, size_t n, unsigned long flags)
5212e11fd   Vishal Verma   nd_btt: atomic se...
46
47
48
  {
  	struct nd_btt *nd_btt = arena->nd_btt;
  	struct nd_namespace_common *ndns = nd_btt->ndns;
14e494542   Vishal Verma   libnvdimm, btt: B...
49
  	/* arena offsets may be shifted from the base of the device */
d9b83c756   Vishal Verma   libnvdimm, btt: r...
50
  	offset = adjust_initial_offset(nd_btt, offset);
3ae3d67ba   Vishal Verma   libnvdimm: add an...
51
  	return nvdimm_write_bytes(ndns, offset, buf, n, flags);
5212e11fd   Vishal Verma   nd_btt: atomic se...
52
53
54
55
56
  }
  
  static int btt_info_write(struct arena_info *arena, struct btt_sb *super)
  {
  	int ret;
b177fe85d   Vishal Verma   libnvdimm, btt: e...
57
58
59
60
61
  	/*
  	 * infooff and info2off should always be at least 512B aligned.
  	 * We rely on that to make sure rw_bytes does error clearing
  	 * correctly, so make sure that is the case.
  	 */
86652d2eb   Vishal Verma   libnvdimm, btt: c...
62
63
64
65
66
67
  	dev_WARN_ONCE(to_dev(arena), !IS_ALIGNED(arena->infooff, 512),
  		"arena->infooff: %#llx is unaligned
  ", arena->infooff);
  	dev_WARN_ONCE(to_dev(arena), !IS_ALIGNED(arena->info2off, 512),
  		"arena->info2off: %#llx is unaligned
  ", arena->info2off);
b177fe85d   Vishal Verma   libnvdimm, btt: e...
68

5212e11fd   Vishal Verma   nd_btt: atomic se...
69
  	ret = arena_write_bytes(arena, arena->info2off, super,
3ae3d67ba   Vishal Verma   libnvdimm: add an...
70
  			sizeof(struct btt_sb), 0);
5212e11fd   Vishal Verma   nd_btt: atomic se...
71
72
73
74
  	if (ret)
  		return ret;
  
  	return arena_write_bytes(arena, arena->infooff, super,
3ae3d67ba   Vishal Verma   libnvdimm: add an...
75
  			sizeof(struct btt_sb), 0);
5212e11fd   Vishal Verma   nd_btt: atomic se...
76
77
78
79
  }
  
  static int btt_info_read(struct arena_info *arena, struct btt_sb *super)
  {
5212e11fd   Vishal Verma   nd_btt: atomic se...
80
  	return arena_read_bytes(arena, arena->infooff, super,
3ae3d67ba   Vishal Verma   libnvdimm: add an...
81
  			sizeof(struct btt_sb), 0);
5212e11fd   Vishal Verma   nd_btt: atomic se...
82
83
84
85
86
87
88
89
  }
  
  /*
   * 'raw' version of btt_map write
   * Assumptions:
   *   mapping is in little-endian
   *   mapping contains 'E' and 'Z' flags as desired
   */
3ae3d67ba   Vishal Verma   libnvdimm: add an...
90
91
  static int __btt_map_write(struct arena_info *arena, u32 lba, __le32 mapping,
  		unsigned long flags)
5212e11fd   Vishal Verma   nd_btt: atomic se...
92
93
  {
  	u64 ns_off = arena->mapoff + (lba * MAP_ENT_SIZE);
86652d2eb   Vishal Verma   libnvdimm, btt: c...
94
95
96
97
98
  	if (unlikely(lba >= arena->external_nlba))
  		dev_err_ratelimited(to_dev(arena),
  			"%s: lba %#x out of range (max: %#x)
  ",
  			__func__, lba, arena->external_nlba);
3ae3d67ba   Vishal Verma   libnvdimm: add an...
99
  	return arena_write_bytes(arena, ns_off, &mapping, MAP_ENT_SIZE, flags);
5212e11fd   Vishal Verma   nd_btt: atomic se...
100
101
102
  }
  
  static int btt_map_write(struct arena_info *arena, u32 lba, u32 mapping,
3ae3d67ba   Vishal Verma   libnvdimm: add an...
103
  			u32 z_flag, u32 e_flag, unsigned long rwb_flags)
5212e11fd   Vishal Verma   nd_btt: atomic se...
104
105
106
107
108
109
110
111
  {
  	u32 ze;
  	__le32 mapping_le;
  
  	/*
  	 * This 'mapping' is supposed to be just the LBA mapping, without
  	 * any flags set, so strip the flag bits.
  	 */
0595d539a   Vishal Verma   libnvdimm, btt: r...
112
  	mapping = ent_lba(mapping);
5212e11fd   Vishal Verma   nd_btt: atomic se...
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
  
  	ze = (z_flag << 1) + e_flag;
  	switch (ze) {
  	case 0:
  		/*
  		 * We want to set neither of the Z or E flags, and
  		 * in the actual layout, this means setting the bit
  		 * positions of both to '1' to indicate a 'normal'
  		 * map entry
  		 */
  		mapping |= MAP_ENT_NORMAL;
  		break;
  	case 1:
  		mapping |= (1 << MAP_ERR_SHIFT);
  		break;
  	case 2:
  		mapping |= (1 << MAP_TRIM_SHIFT);
  		break;
  	default:
  		/*
  		 * The case where Z and E are both sent in as '1' could be
  		 * construed as a valid 'normal' case, but we decide not to,
  		 * to avoid confusion
  		 */
86652d2eb   Vishal Verma   libnvdimm, btt: c...
137
138
139
  		dev_err_ratelimited(to_dev(arena),
  			"Invalid use of Z and E flags
  ");
5212e11fd   Vishal Verma   nd_btt: atomic se...
140
141
142
143
  		return -EIO;
  	}
  
  	mapping_le = cpu_to_le32(mapping);
3ae3d67ba   Vishal Verma   libnvdimm: add an...
144
  	return __btt_map_write(arena, lba, mapping_le, rwb_flags);
5212e11fd   Vishal Verma   nd_btt: atomic se...
145
146
147
  }
  
  static int btt_map_read(struct arena_info *arena, u32 lba, u32 *mapping,
3ae3d67ba   Vishal Verma   libnvdimm: add an...
148
  			int *trim, int *error, unsigned long rwb_flags)
5212e11fd   Vishal Verma   nd_btt: atomic se...
149
150
151
152
153
  {
  	int ret;
  	__le32 in;
  	u32 raw_mapping, postmap, ze, z_flag, e_flag;
  	u64 ns_off = arena->mapoff + (lba * MAP_ENT_SIZE);
86652d2eb   Vishal Verma   libnvdimm, btt: c...
154
155
156
157
158
  	if (unlikely(lba >= arena->external_nlba))
  		dev_err_ratelimited(to_dev(arena),
  			"%s: lba %#x out of range (max: %#x)
  ",
  			__func__, lba, arena->external_nlba);
5212e11fd   Vishal Verma   nd_btt: atomic se...
159

3ae3d67ba   Vishal Verma   libnvdimm: add an...
160
  	ret = arena_read_bytes(arena, ns_off, &in, MAP_ENT_SIZE, rwb_flags);
5212e11fd   Vishal Verma   nd_btt: atomic se...
161
162
163
164
  	if (ret)
  		return ret;
  
  	raw_mapping = le32_to_cpu(in);
0595d539a   Vishal Verma   libnvdimm, btt: r...
165
166
  	z_flag = ent_z_flag(raw_mapping);
  	e_flag = ent_e_flag(raw_mapping);
5212e11fd   Vishal Verma   nd_btt: atomic se...
167
  	ze = (z_flag << 1) + e_flag;
0595d539a   Vishal Verma   libnvdimm, btt: r...
168
  	postmap = ent_lba(raw_mapping);
5212e11fd   Vishal Verma   nd_btt: atomic se...
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
  
  	/* Reuse the {z,e}_flag variables for *trim and *error */
  	z_flag = 0;
  	e_flag = 0;
  
  	switch (ze) {
  	case 0:
  		/* Initial state. Return postmap = premap */
  		*mapping = lba;
  		break;
  	case 1:
  		*mapping = postmap;
  		e_flag = 1;
  		break;
  	case 2:
  		*mapping = postmap;
  		z_flag = 1;
  		break;
  	case 3:
  		*mapping = postmap;
  		break;
  	default:
  		return -EIO;
  	}
  
  	if (trim)
  		*trim = z_flag;
  	if (error)
  		*error = e_flag;
  
  	return ret;
  }
24e3a7fb6   Vishal Verma   libnvdimm, btt: F...
201
202
  static int btt_log_group_read(struct arena_info *arena, u32 lane,
  			struct log_group *log)
5212e11fd   Vishal Verma   nd_btt: atomic se...
203
  {
5212e11fd   Vishal Verma   nd_btt: atomic se...
204
  	return arena_read_bytes(arena,
24e3a7fb6   Vishal Verma   libnvdimm, btt: F...
205
206
  			arena->logoff + (lane * LOG_GRP_SIZE), log,
  			LOG_GRP_SIZE, 0);
5212e11fd   Vishal Verma   nd_btt: atomic se...
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
  }
  
  static struct dentry *debugfs_root;
  
  static void arena_debugfs_init(struct arena_info *a, struct dentry *parent,
  				int idx)
  {
  	char dirname[32];
  	struct dentry *d;
  
  	/* If for some reason, parent bttN was not created, exit */
  	if (!parent)
  		return;
  
  	snprintf(dirname, 32, "arena%d", idx);
  	d = debugfs_create_dir(dirname, parent);
  	if (IS_ERR_OR_NULL(d))
  		return;
  	a->debugfs_dir = d;
  
  	debugfs_create_x64("size", S_IRUGO, d, &a->size);
  	debugfs_create_x64("external_lba_start", S_IRUGO, d,
  				&a->external_lba_start);
  	debugfs_create_x32("internal_nlba", S_IRUGO, d, &a->internal_nlba);
  	debugfs_create_u32("internal_lbasize", S_IRUGO, d,
  				&a->internal_lbasize);
  	debugfs_create_x32("external_nlba", S_IRUGO, d, &a->external_nlba);
  	debugfs_create_u32("external_lbasize", S_IRUGO, d,
  				&a->external_lbasize);
  	debugfs_create_u32("nfree", S_IRUGO, d, &a->nfree);
  	debugfs_create_u16("version_major", S_IRUGO, d, &a->version_major);
  	debugfs_create_u16("version_minor", S_IRUGO, d, &a->version_minor);
  	debugfs_create_x64("nextoff", S_IRUGO, d, &a->nextoff);
  	debugfs_create_x64("infooff", S_IRUGO, d, &a->infooff);
  	debugfs_create_x64("dataoff", S_IRUGO, d, &a->dataoff);
  	debugfs_create_x64("mapoff", S_IRUGO, d, &a->mapoff);
  	debugfs_create_x64("logoff", S_IRUGO, d, &a->logoff);
  	debugfs_create_x64("info2off", S_IRUGO, d, &a->info2off);
  	debugfs_create_x32("flags", S_IRUGO, d, &a->flags);
24e3a7fb6   Vishal Verma   libnvdimm, btt: F...
246
247
  	debugfs_create_u32("log_index_0", S_IRUGO, d, &a->log_index[0]);
  	debugfs_create_u32("log_index_1", S_IRUGO, d, &a->log_index[1]);
5212e11fd   Vishal Verma   nd_btt: atomic se...
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
  }
  
  static void btt_debugfs_init(struct btt *btt)
  {
  	int i = 0;
  	struct arena_info *arena;
  
  	btt->debugfs_dir = debugfs_create_dir(dev_name(&btt->nd_btt->dev),
  						debugfs_root);
  	if (IS_ERR_OR_NULL(btt->debugfs_dir))
  		return;
  
  	list_for_each_entry(arena, &btt->arena_list, list) {
  		arena_debugfs_init(arena, btt->debugfs_dir, i);
  		i++;
  	}
  }
24e3a7fb6   Vishal Verma   libnvdimm, btt: F...
265
266
267
268
  static u32 log_seq(struct log_group *log, int log_idx)
  {
  	return le32_to_cpu(log->ent[log_idx].seq);
  }
5212e11fd   Vishal Verma   nd_btt: atomic se...
269
270
271
272
273
274
275
276
277
  /*
   * This function accepts two log entries, and uses the
   * sequence number to find the 'older' entry.
   * It also updates the sequence number in this old entry to
   * make it the 'new' one if the mark_flag is set.
   * Finally, it returns which of the entries was the older one.
   *
   * TODO The logic feels a bit kludge-y. make it better..
   */
24e3a7fb6   Vishal Verma   libnvdimm, btt: F...
278
  static int btt_log_get_old(struct arena_info *a, struct log_group *log)
5212e11fd   Vishal Verma   nd_btt: atomic se...
279
  {
24e3a7fb6   Vishal Verma   libnvdimm, btt: F...
280
281
  	int idx0 = a->log_index[0];
  	int idx1 = a->log_index[1];
5212e11fd   Vishal Verma   nd_btt: atomic se...
282
283
284
285
286
287
288
  	int old;
  
  	/*
  	 * the first ever time this is seen, the entry goes into [0]
  	 * the next time, the following logic works out to put this
  	 * (next) entry into [1]
  	 */
24e3a7fb6   Vishal Verma   libnvdimm, btt: F...
289
290
  	if (log_seq(log, idx0) == 0) {
  		log->ent[idx0].seq = cpu_to_le32(1);
5212e11fd   Vishal Verma   nd_btt: atomic se...
291
292
  		return 0;
  	}
24e3a7fb6   Vishal Verma   libnvdimm, btt: F...
293
  	if (log_seq(log, idx0) == log_seq(log, idx1))
5212e11fd   Vishal Verma   nd_btt: atomic se...
294
  		return -EINVAL;
24e3a7fb6   Vishal Verma   libnvdimm, btt: F...
295
  	if (log_seq(log, idx0) + log_seq(log, idx1) > 5)
5212e11fd   Vishal Verma   nd_btt: atomic se...
296
  		return -EINVAL;
24e3a7fb6   Vishal Verma   libnvdimm, btt: F...
297
298
  	if (log_seq(log, idx0) < log_seq(log, idx1)) {
  		if ((log_seq(log, idx1) - log_seq(log, idx0)) == 1)
5212e11fd   Vishal Verma   nd_btt: atomic se...
299
300
301
302
  			old = 0;
  		else
  			old = 1;
  	} else {
24e3a7fb6   Vishal Verma   libnvdimm, btt: F...
303
  		if ((log_seq(log, idx0) - log_seq(log, idx1)) == 1)
5212e11fd   Vishal Verma   nd_btt: atomic se...
304
305
306
307
308
309
310
  			old = 1;
  		else
  			old = 0;
  	}
  
  	return old;
  }
5212e11fd   Vishal Verma   nd_btt: atomic se...
311
312
313
314
315
316
317
318
319
320
321
  /*
   * This function copies the desired (old/new) log entry into ent if
   * it is not NULL. It returns the sub-slot number (0 or 1)
   * where the desired log entry was found. Negative return values
   * indicate errors.
   */
  static int btt_log_read(struct arena_info *arena, u32 lane,
  			struct log_entry *ent, int old_flag)
  {
  	int ret;
  	int old_ent, ret_ent;
24e3a7fb6   Vishal Verma   libnvdimm, btt: F...
322
  	struct log_group log;
5212e11fd   Vishal Verma   nd_btt: atomic se...
323

24e3a7fb6   Vishal Verma   libnvdimm, btt: F...
324
  	ret = btt_log_group_read(arena, lane, &log);
5212e11fd   Vishal Verma   nd_btt: atomic se...
325
326
  	if (ret)
  		return -EIO;
24e3a7fb6   Vishal Verma   libnvdimm, btt: F...
327
  	old_ent = btt_log_get_old(arena, &log);
5212e11fd   Vishal Verma   nd_btt: atomic se...
328
  	if (old_ent < 0 || old_ent > 1) {
e6be2dcbe   Vishal Verma   libnvdimm, btt: c...
329
  		dev_err(to_dev(arena),
5212e11fd   Vishal Verma   nd_btt: atomic se...
330
331
  				"log corruption (%d): lane %d seq [%d, %d]
  ",
24e3a7fb6   Vishal Verma   libnvdimm, btt: F...
332
333
  				old_ent, lane, log.ent[arena->log_index[0]].seq,
  				log.ent[arena->log_index[1]].seq);
5212e11fd   Vishal Verma   nd_btt: atomic se...
334
335
336
337
338
339
340
  		/* TODO set error state? */
  		return -EIO;
  	}
  
  	ret_ent = (old_flag ? old_ent : (1 - old_ent));
  
  	if (ent != NULL)
24e3a7fb6   Vishal Verma   libnvdimm, btt: F...
341
  		memcpy(ent, &log.ent[arena->log_index[ret_ent]], LOG_ENT_SIZE);
5212e11fd   Vishal Verma   nd_btt: atomic se...
342
343
344
345
346
347
348
349
350
351
  
  	return ret_ent;
  }
  
  /*
   * This function commits a log entry to media
   * It does _not_ prepare the freelist entry for the next write
   * btt_flog_write is the wrapper for updating the freelist elements
   */
  static int __btt_log_write(struct arena_info *arena, u32 lane,
3ae3d67ba   Vishal Verma   libnvdimm: add an...
352
  			u32 sub, struct log_entry *ent, unsigned long flags)
5212e11fd   Vishal Verma   nd_btt: atomic se...
353
354
  {
  	int ret;
24e3a7fb6   Vishal Verma   libnvdimm, btt: F...
355
356
  	u32 group_slot = arena->log_index[sub];
  	unsigned int log_half = LOG_ENT_SIZE / 2;
5212e11fd   Vishal Verma   nd_btt: atomic se...
357
  	void *src = ent;
24e3a7fb6   Vishal Verma   libnvdimm, btt: F...
358
  	u64 ns_off;
5212e11fd   Vishal Verma   nd_btt: atomic se...
359

24e3a7fb6   Vishal Verma   libnvdimm, btt: F...
360
361
  	ns_off = arena->logoff + (lane * LOG_GRP_SIZE) +
  		(group_slot * LOG_ENT_SIZE);
5212e11fd   Vishal Verma   nd_btt: atomic se...
362
  	/* split the 16B write into atomic, durable halves */
3ae3d67ba   Vishal Verma   libnvdimm: add an...
363
  	ret = arena_write_bytes(arena, ns_off, src, log_half, flags);
5212e11fd   Vishal Verma   nd_btt: atomic se...
364
365
366
367
368
  	if (ret)
  		return ret;
  
  	ns_off += log_half;
  	src += log_half;
3ae3d67ba   Vishal Verma   libnvdimm: add an...
369
  	return arena_write_bytes(arena, ns_off, src, log_half, flags);
5212e11fd   Vishal Verma   nd_btt: atomic se...
370
371
372
373
374
375
  }
  
  static int btt_flog_write(struct arena_info *arena, u32 lane, u32 sub,
  			struct log_entry *ent)
  {
  	int ret;
3ae3d67ba   Vishal Verma   libnvdimm: add an...
376
  	ret = __btt_log_write(arena, lane, sub, ent, NVDIMM_IO_ATOMIC);
5212e11fd   Vishal Verma   nd_btt: atomic se...
377
378
379
380
381
382
383
  	if (ret)
  		return ret;
  
  	/* prepare the next free entry */
  	arena->freelist[lane].sub = 1 - arena->freelist[lane].sub;
  	if (++(arena->freelist[lane].seq) == 4)
  		arena->freelist[lane].seq = 1;
86aa66687   Aneesh Kumar K.V   libnvdimm: Fix en...
384
  	if (ent_e_flag(le32_to_cpu(ent->old_map)))
d9b83c756   Vishal Verma   libnvdimm, btt: r...
385
  		arena->freelist[lane].has_err = 1;
86aa66687   Aneesh Kumar K.V   libnvdimm: Fix en...
386
  	arena->freelist[lane].block = ent_lba(le32_to_cpu(ent->old_map));
5212e11fd   Vishal Verma   nd_btt: atomic se...
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
  
  	return ret;
  }
  
  /*
   * This function initializes the BTT map to the initial state, which is
   * all-zeroes, and indicates an identity mapping
   */
  static int btt_map_init(struct arena_info *arena)
  {
  	int ret = -EINVAL;
  	void *zerobuf;
  	size_t offset = 0;
  	size_t chunk_size = SZ_2M;
  	size_t mapsize = arena->logoff - arena->mapoff;
  
  	zerobuf = kzalloc(chunk_size, GFP_KERNEL);
  	if (!zerobuf)
  		return -ENOMEM;
b177fe85d   Vishal Verma   libnvdimm, btt: e...
406
407
408
409
410
  	/*
  	 * mapoff should always be at least 512B  aligned. We rely on that to
  	 * make sure rw_bytes does error clearing correctly, so make sure that
  	 * is the case.
  	 */
86652d2eb   Vishal Verma   libnvdimm, btt: c...
411
412
413
  	dev_WARN_ONCE(to_dev(arena), !IS_ALIGNED(arena->mapoff, 512),
  		"arena->mapoff: %#llx is unaligned
  ", arena->mapoff);
b177fe85d   Vishal Verma   libnvdimm, btt: e...
414

5212e11fd   Vishal Verma   nd_btt: atomic se...
415
416
  	while (mapsize) {
  		size_t size = min(mapsize, chunk_size);
86652d2eb   Vishal Verma   libnvdimm, btt: c...
417
  		dev_WARN_ONCE(to_dev(arena), size < 512,
04c3c982f   Randy Dunlap   libnvdimm, btt: f...
418
419
  			"chunk size: %#zx is unaligned
  ", size);
5212e11fd   Vishal Verma   nd_btt: atomic se...
420
  		ret = arena_write_bytes(arena, arena->mapoff + offset, zerobuf,
3ae3d67ba   Vishal Verma   libnvdimm: add an...
421
  				size, 0);
5212e11fd   Vishal Verma   nd_btt: atomic se...
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
  		if (ret)
  			goto free;
  
  		offset += size;
  		mapsize -= size;
  		cond_resched();
  	}
  
   free:
  	kfree(zerobuf);
  	return ret;
  }
  
  /*
   * This function initializes the BTT log with 'fake' entries pointing
   * to the initial reserved set of blocks as being free
   */
  static int btt_log_init(struct arena_info *arena)
  {
b177fe85d   Vishal Verma   libnvdimm, btt: e...
441
442
  	size_t logsize = arena->info2off - arena->logoff;
  	size_t chunk_size = SZ_4K, offset = 0;
24e3a7fb6   Vishal Verma   libnvdimm, btt: F...
443
  	struct log_entry ent;
b177fe85d   Vishal Verma   libnvdimm, btt: e...
444
  	void *zerobuf;
5212e11fd   Vishal Verma   nd_btt: atomic se...
445
446
  	int ret;
  	u32 i;
5212e11fd   Vishal Verma   nd_btt: atomic se...
447

b177fe85d   Vishal Verma   libnvdimm, btt: e...
448
449
450
451
452
453
454
455
  	zerobuf = kzalloc(chunk_size, GFP_KERNEL);
  	if (!zerobuf)
  		return -ENOMEM;
  	/*
  	 * logoff should always be at least 512B  aligned. We rely on that to
  	 * make sure rw_bytes does error clearing correctly, so make sure that
  	 * is the case.
  	 */
86652d2eb   Vishal Verma   libnvdimm, btt: c...
456
457
458
  	dev_WARN_ONCE(to_dev(arena), !IS_ALIGNED(arena->logoff, 512),
  		"arena->logoff: %#llx is unaligned
  ", arena->logoff);
b177fe85d   Vishal Verma   libnvdimm, btt: e...
459
460
461
  
  	while (logsize) {
  		size_t size = min(logsize, chunk_size);
86652d2eb   Vishal Verma   libnvdimm, btt: c...
462
  		dev_WARN_ONCE(to_dev(arena), size < 512,
04c3c982f   Randy Dunlap   libnvdimm, btt: f...
463
464
  			"chunk size: %#zx is unaligned
  ", size);
b177fe85d   Vishal Verma   libnvdimm, btt: e...
465
466
467
468
469
470
471
472
473
  		ret = arena_write_bytes(arena, arena->logoff + offset, zerobuf,
  				size, 0);
  		if (ret)
  			goto free;
  
  		offset += size;
  		logsize -= size;
  		cond_resched();
  	}
5212e11fd   Vishal Verma   nd_btt: atomic se...
474
475
  
  	for (i = 0; i < arena->nfree; i++) {
24e3a7fb6   Vishal Verma   libnvdimm, btt: F...
476
477
478
479
480
  		ent.lba = cpu_to_le32(i);
  		ent.old_map = cpu_to_le32(arena->external_nlba + i);
  		ent.new_map = cpu_to_le32(arena->external_nlba + i);
  		ent.seq = cpu_to_le32(LOG_SEQ_INIT);
  		ret = __btt_log_write(arena, i, 0, &ent, 0);
5212e11fd   Vishal Verma   nd_btt: atomic se...
481
  		if (ret)
b177fe85d   Vishal Verma   libnvdimm, btt: e...
482
  			goto free;
5212e11fd   Vishal Verma   nd_btt: atomic se...
483
  	}
b177fe85d   Vishal Verma   libnvdimm, btt: e...
484
485
486
   free:
  	kfree(zerobuf);
  	return ret;
5212e11fd   Vishal Verma   nd_btt: atomic se...
487
  }
d9b83c756   Vishal Verma   libnvdimm, btt: r...
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
  static u64 to_namespace_offset(struct arena_info *arena, u64 lba)
  {
  	return arena->dataoff + ((u64)lba * arena->internal_lbasize);
  }
  
  static int arena_clear_freelist_error(struct arena_info *arena, u32 lane)
  {
  	int ret = 0;
  
  	if (arena->freelist[lane].has_err) {
  		void *zero_page = page_address(ZERO_PAGE(0));
  		u32 lba = arena->freelist[lane].block;
  		u64 nsoff = to_namespace_offset(arena, lba);
  		unsigned long len = arena->sector_size;
  
  		mutex_lock(&arena->err_lock);
  
  		while (len) {
  			unsigned long chunk = min(len, PAGE_SIZE);
  
  			ret = arena_write_bytes(arena, nsoff, zero_page,
  				chunk, 0);
  			if (ret)
  				break;
  			len -= chunk;
  			nsoff += chunk;
  			if (len == 0)
  				arena->freelist[lane].has_err = 0;
  		}
  		mutex_unlock(&arena->err_lock);
  	}
  	return ret;
  }
5212e11fd   Vishal Verma   nd_btt: atomic se...
521
522
  static int btt_freelist_init(struct arena_info *arena)
  {
2f8c90111   Vishal Verma   libnvdimm/btt: Re...
523
  	int new, ret;
2f8c90111   Vishal Verma   libnvdimm/btt: Re...
524
  	struct log_entry log_new;
9dedc73a4   Vishal Verma   libnvdimm/btt: Fi...
525
  	u32 i, map_entry, log_oldmap, log_newmap;
5212e11fd   Vishal Verma   nd_btt: atomic se...
526
527
528
529
530
531
532
  
  	arena->freelist = kcalloc(arena->nfree, sizeof(struct free_entry),
  					GFP_KERNEL);
  	if (!arena->freelist)
  		return -ENOMEM;
  
  	for (i = 0; i < arena->nfree; i++) {
5212e11fd   Vishal Verma   nd_btt: atomic se...
533
534
535
  		new = btt_log_read(arena, i, &log_new, LOG_NEW_ENT);
  		if (new < 0)
  			return new;
9dedc73a4   Vishal Verma   libnvdimm/btt: Fi...
536
537
538
  		/* old and new map entries with any flags stripped out */
  		log_oldmap = ent_lba(le32_to_cpu(log_new.old_map));
  		log_newmap = ent_lba(le32_to_cpu(log_new.new_map));
5212e11fd   Vishal Verma   nd_btt: atomic se...
539
540
541
  		/* sub points to the next one to be overwritten */
  		arena->freelist[i].sub = 1 - new;
  		arena->freelist[i].seq = nd_inc_seq(le32_to_cpu(log_new.seq));
9dedc73a4   Vishal Verma   libnvdimm/btt: Fi...
542
  		arena->freelist[i].block = log_oldmap;
5212e11fd   Vishal Verma   nd_btt: atomic se...
543

d9b83c756   Vishal Verma   libnvdimm, btt: r...
544
545
546
547
  		/*
  		 * FIXME: if error clearing fails during init, we want to make
  		 * the BTT read-only
  		 */
86aa66687   Aneesh Kumar K.V   libnvdimm: Fix en...
548
549
  		if (ent_e_flag(le32_to_cpu(log_new.old_map)) &&
  		    !ent_normal(le32_to_cpu(log_new.old_map))) {
9dedc73a4   Vishal Verma   libnvdimm/btt: Fi...
550
  			arena->freelist[i].has_err = 1;
d9b83c756   Vishal Verma   libnvdimm, btt: r...
551
552
  			ret = arena_clear_freelist_error(arena, i);
  			if (ret)
86652d2eb   Vishal Verma   libnvdimm, btt: c...
553
554
555
  				dev_err_ratelimited(to_dev(arena),
  					"Unable to clear known errors
  ");
d9b83c756   Vishal Verma   libnvdimm, btt: r...
556
  		}
5212e11fd   Vishal Verma   nd_btt: atomic se...
557
  		/* This implies a newly created or untouched flog entry */
9dedc73a4   Vishal Verma   libnvdimm/btt: Fi...
558
  		if (log_oldmap == log_newmap)
5212e11fd   Vishal Verma   nd_btt: atomic se...
559
560
561
562
  			continue;
  
  		/* Check if map recovery is needed */
  		ret = btt_map_read(arena, le32_to_cpu(log_new.lba), &map_entry,
3ae3d67ba   Vishal Verma   libnvdimm: add an...
563
  				NULL, NULL, 0);
5212e11fd   Vishal Verma   nd_btt: atomic se...
564
565
  		if (ret)
  			return ret;
9dedc73a4   Vishal Verma   libnvdimm/btt: Fi...
566
567
568
569
570
571
572
573
574
  
  		/*
  		 * The map_entry from btt_read_map is stripped of any flag bits,
  		 * so use the stripped out versions from the log as well for
  		 * testing whether recovery is needed. For restoration, use the
  		 * 'raw' version of the log entries as that captured what we
  		 * were going to write originally.
  		 */
  		if ((log_newmap != map_entry) && (log_oldmap == map_entry)) {
5212e11fd   Vishal Verma   nd_btt: atomic se...
575
576
577
578
579
  			/*
  			 * Last transaction wrote the flog, but wasn't able
  			 * to complete the map write. So fix up the map.
  			 */
  			ret = btt_map_write(arena, le32_to_cpu(log_new.lba),
3ae3d67ba   Vishal Verma   libnvdimm: add an...
580
  					le32_to_cpu(log_new.new_map), 0, 0, 0);
5212e11fd   Vishal Verma   nd_btt: atomic se...
581
582
583
  			if (ret)
  				return ret;
  		}
5212e11fd   Vishal Verma   nd_btt: atomic se...
584
585
586
587
  	}
  
  	return 0;
  }
24e3a7fb6   Vishal Verma   libnvdimm, btt: F...
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
  static bool ent_is_padding(struct log_entry *ent)
  {
  	return (ent->lba == 0) && (ent->old_map == 0) && (ent->new_map == 0)
  		&& (ent->seq == 0);
  }
  
  /*
   * Detecting valid log indices: We read a log group (see the comments in btt.h
   * for a description of a 'log_group' and its 'slots'), and iterate over its
   * four slots. We expect that a padding slot will be all-zeroes, and use this
   * to detect a padding slot vs. an actual entry.
   *
   * If a log_group is in the initial state, i.e. hasn't been used since the
   * creation of this BTT layout, it will have three of the four slots with
   * zeroes. We skip over these log_groups for the detection of log_index. If
   * all log_groups are in the initial state (i.e. the BTT has never been
   * written to), it is safe to assume the 'new format' of log entries in slots
   * (0, 1).
   */
  static int log_set_indices(struct arena_info *arena)
  {
  	bool idx_set = false, initial_state = true;
  	int ret, log_index[2] = {-1, -1};
  	u32 i, j, next_idx = 0;
  	struct log_group log;
  	u32 pad_count = 0;
  
  	for (i = 0; i < arena->nfree; i++) {
  		ret = btt_log_group_read(arena, i, &log);
  		if (ret < 0)
  			return ret;
  
  		for (j = 0; j < 4; j++) {
  			if (!idx_set) {
  				if (ent_is_padding(&log.ent[j])) {
  					pad_count++;
  					continue;
  				} else {
  					/* Skip if index has been recorded */
  					if ((next_idx == 1) &&
  						(j == log_index[0]))
  						continue;
  					/* valid entry, record index */
  					log_index[next_idx] = j;
  					next_idx++;
  				}
  				if (next_idx == 2) {
  					/* two valid entries found */
  					idx_set = true;
  				} else if (next_idx > 2) {
  					/* too many valid indices */
  					return -ENXIO;
  				}
  			} else {
  				/*
  				 * once the indices have been set, just verify
  				 * that all subsequent log groups are either in
  				 * their initial state or follow the same
  				 * indices.
  				 */
  				if (j == log_index[0]) {
  					/* entry must be 'valid' */
  					if (ent_is_padding(&log.ent[j]))
  						return -ENXIO;
  				} else if (j == log_index[1]) {
  					;
  					/*
  					 * log_index[1] can be padding if the
  					 * lane never got used and it is still
  					 * in the initial state (three 'padding'
  					 * entries)
  					 */
  				} else {
  					/* entry must be invalid (padding) */
  					if (!ent_is_padding(&log.ent[j]))
  						return -ENXIO;
  				}
  			}
  		}
  		/*
  		 * If any of the log_groups have more than one valid,
  		 * non-padding entry, then the we are no longer in the
  		 * initial_state
  		 */
  		if (pad_count < 3)
  			initial_state = false;
  		pad_count = 0;
  	}
  
  	if (!initial_state && !idx_set)
  		return -ENXIO;
  
  	/*
  	 * If all the entries in the log were in the initial state,
  	 * assume new padding scheme
  	 */
  	if (initial_state)
  		log_index[1] = 1;
  
  	/*
  	 * Only allow the known permutations of log/padding indices,
  	 * i.e. (0, 1), and (0, 2)
  	 */
  	if ((log_index[0] == 0) && ((log_index[1] == 1) || (log_index[1] == 2)))
  		; /* known index possibilities */
  	else {
  		dev_err(to_dev(arena), "Found an unknown padding scheme
  ");
  		return -ENXIO;
  	}
  
  	arena->log_index[0] = log_index[0];
  	arena->log_index[1] = log_index[1];
  	dev_dbg(to_dev(arena), "log_index_0 = %d
  ", log_index[0]);
  	dev_dbg(to_dev(arena), "log_index_1 = %d
  ", log_index[1]);
  	return 0;
  }
5212e11fd   Vishal Verma   nd_btt: atomic se...
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
  static int btt_rtt_init(struct arena_info *arena)
  {
  	arena->rtt = kcalloc(arena->nfree, sizeof(u32), GFP_KERNEL);
  	if (arena->rtt == NULL)
  		return -ENOMEM;
  
  	return 0;
  }
  
  static int btt_maplocks_init(struct arena_info *arena)
  {
  	u32 i;
  
  	arena->map_locks = kcalloc(arena->nfree, sizeof(struct aligned_lock),
  				GFP_KERNEL);
  	if (!arena->map_locks)
  		return -ENOMEM;
  
  	for (i = 0; i < arena->nfree; i++)
  		spin_lock_init(&arena->map_locks[i].lock);
  
  	return 0;
  }
  
  static struct arena_info *alloc_arena(struct btt *btt, size_t size,
  				size_t start, size_t arena_off)
  {
  	struct arena_info *arena;
  	u64 logsize, mapsize, datasize;
  	u64 available = size;
  
  	arena = kzalloc(sizeof(struct arena_info), GFP_KERNEL);
  	if (!arena)
  		return NULL;
  	arena->nd_btt = btt->nd_btt;
758920045   Vishal Verma   libnvdimm, btt: c...
742
  	arena->sector_size = btt->sector_size;
d08cd5e0e   Jeff Moyer   libnvdimm, btt: f...
743
  	mutex_init(&arena->err_lock);
5212e11fd   Vishal Verma   nd_btt: atomic se...
744
745
746
747
748
749
750
751
752
753
  
  	if (!size)
  		return arena;
  
  	arena->size = size;
  	arena->external_lba_start = start;
  	arena->external_lbasize = btt->lbasize;
  	arena->internal_lbasize = roundup(arena->external_lbasize,
  					INT_LBASIZE_ALIGNMENT);
  	arena->nfree = BTT_DEFAULT_NFREE;
14e494542   Vishal Verma   libnvdimm, btt: B...
754
755
  	arena->version_major = btt->nd_btt->version_major;
  	arena->version_minor = btt->nd_btt->version_minor;
5212e11fd   Vishal Verma   nd_btt: atomic se...
756
757
758
759
760
761
762
763
  
  	if (available % BTT_PG_SIZE)
  		available -= (available % BTT_PG_SIZE);
  
  	/* Two pages are reserved for the super block and its copy */
  	available -= 2 * BTT_PG_SIZE;
  
  	/* The log takes a fixed amount of space based on nfree */
24e3a7fb6   Vishal Verma   libnvdimm, btt: F...
764
  	logsize = roundup(arena->nfree * LOG_GRP_SIZE, BTT_PG_SIZE);
5212e11fd   Vishal Verma   nd_btt: atomic se...
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
  	available -= logsize;
  
  	/* Calculate optimal split between map and data area */
  	arena->internal_nlba = div_u64(available - BTT_PG_SIZE,
  			arena->internal_lbasize + MAP_ENT_SIZE);
  	arena->external_nlba = arena->internal_nlba - arena->nfree;
  
  	mapsize = roundup((arena->external_nlba * MAP_ENT_SIZE), BTT_PG_SIZE);
  	datasize = available - mapsize;
  
  	/* 'Absolute' values, relative to start of storage space */
  	arena->infooff = arena_off;
  	arena->dataoff = arena->infooff + BTT_PG_SIZE;
  	arena->mapoff = arena->dataoff + datasize;
  	arena->logoff = arena->mapoff + mapsize;
  	arena->info2off = arena->logoff + logsize;
24e3a7fb6   Vishal Verma   libnvdimm, btt: F...
781
782
783
784
  
  	/* Default log indices are (0,1) */
  	arena->log_index[0] = 0;
  	arena->log_index[1] = 1;
5212e11fd   Vishal Verma   nd_btt: atomic se...
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
  	return arena;
  }
  
  static void free_arenas(struct btt *btt)
  {
  	struct arena_info *arena, *next;
  
  	list_for_each_entry_safe(arena, next, &btt->arena_list, list) {
  		list_del(&arena->list);
  		kfree(arena->rtt);
  		kfree(arena->map_locks);
  		kfree(arena->freelist);
  		debugfs_remove_recursive(arena->debugfs_dir);
  		kfree(arena);
  	}
  }
  
  /*
5212e11fd   Vishal Verma   nd_btt: atomic se...
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
   * This function reads an existing valid btt superblock and
   * populates the corresponding arena_info struct
   */
  static void parse_arena_meta(struct arena_info *arena, struct btt_sb *super,
  				u64 arena_off)
  {
  	arena->internal_nlba = le32_to_cpu(super->internal_nlba);
  	arena->internal_lbasize = le32_to_cpu(super->internal_lbasize);
  	arena->external_nlba = le32_to_cpu(super->external_nlba);
  	arena->external_lbasize = le32_to_cpu(super->external_lbasize);
  	arena->nfree = le32_to_cpu(super->nfree);
  	arena->version_major = le16_to_cpu(super->version_major);
  	arena->version_minor = le16_to_cpu(super->version_minor);
  
  	arena->nextoff = (super->nextoff == 0) ? 0 : (arena_off +
  			le64_to_cpu(super->nextoff));
  	arena->infooff = arena_off;
  	arena->dataoff = arena_off + le64_to_cpu(super->dataoff);
  	arena->mapoff = arena_off + le64_to_cpu(super->mapoff);
  	arena->logoff = arena_off + le64_to_cpu(super->logoff);
  	arena->info2off = arena_off + le64_to_cpu(super->info2off);
5e3294062   Dan Williams   libnvdimm, btt: s...
824
825
826
  	arena->size = (le64_to_cpu(super->nextoff) > 0)
  		? (le64_to_cpu(super->nextoff))
  		: (arena->info2off - arena->infooff + BTT_PG_SIZE);
5212e11fd   Vishal Verma   nd_btt: atomic se...
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
  
  	arena->flags = le32_to_cpu(super->flags);
  }
  
  static int discover_arenas(struct btt *btt)
  {
  	int ret = 0;
  	struct arena_info *arena;
  	struct btt_sb *super;
  	size_t remaining = btt->rawsize;
  	u64 cur_nlba = 0;
  	size_t cur_off = 0;
  	int num_arenas = 0;
  
  	super = kzalloc(sizeof(*super), GFP_KERNEL);
  	if (!super)
  		return -ENOMEM;
  
  	while (remaining) {
  		/* Alloc memory for arena */
  		arena = alloc_arena(btt, 0, 0, 0);
  		if (!arena) {
  			ret = -ENOMEM;
  			goto out_super;
  		}
  
  		arena->infooff = cur_off;
  		ret = btt_info_read(arena, super);
  		if (ret)
  			goto out;
ab45e7632   Vishal Verma   libnvdimm, btt: c...
857
  		if (!nd_btt_arena_is_valid(btt->nd_btt, super)) {
5212e11fd   Vishal Verma   nd_btt: atomic se...
858
859
860
861
862
863
  			if (remaining == btt->rawsize) {
  				btt->init_state = INIT_NOTFOUND;
  				dev_info(to_dev(arena), "No existing arenas
  ");
  				goto out;
  			} else {
e6be2dcbe   Vishal Verma   libnvdimm, btt: c...
864
  				dev_err(to_dev(arena),
5212e11fd   Vishal Verma   nd_btt: atomic se...
865
866
867
868
869
870
871
872
873
  						"Found corrupted metadata!
  ");
  				ret = -ENODEV;
  				goto out;
  			}
  		}
  
  		arena->external_lba_start = cur_nlba;
  		parse_arena_meta(arena, super, cur_off);
24e3a7fb6   Vishal Verma   libnvdimm, btt: F...
874
875
876
877
878
879
880
  		ret = log_set_indices(arena);
  		if (ret) {
  			dev_err(to_dev(arena),
  				"Unable to deduce log/padding indices
  ");
  			goto out;
  		}
5212e11fd   Vishal Verma   nd_btt: atomic se...
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
  		ret = btt_freelist_init(arena);
  		if (ret)
  			goto out;
  
  		ret = btt_rtt_init(arena);
  		if (ret)
  			goto out;
  
  		ret = btt_maplocks_init(arena);
  		if (ret)
  			goto out;
  
  		list_add_tail(&arena->list, &btt->arena_list);
  
  		remaining -= arena->size;
  		cur_off += arena->size;
  		cur_nlba += arena->external_nlba;
  		num_arenas++;
  
  		if (arena->nextoff == 0)
  			break;
  	}
  	btt->num_arenas = num_arenas;
  	btt->nlba = cur_nlba;
  	btt->init_state = INIT_READY;
  
  	kfree(super);
  	return ret;
  
   out:
  	kfree(arena);
  	free_arenas(btt);
   out_super:
  	kfree(super);
  	return ret;
  }
  
  static int create_arenas(struct btt *btt)
  {
  	size_t remaining = btt->rawsize;
  	size_t cur_off = 0;
  
  	while (remaining) {
  		struct arena_info *arena;
  		size_t arena_size = min_t(u64, ARENA_MAX_SIZE, remaining);
  
  		remaining -= arena_size;
  		if (arena_size < ARENA_MIN_SIZE)
  			break;
  
  		arena = alloc_arena(btt, arena_size, btt->nlba, cur_off);
  		if (!arena) {
  			free_arenas(btt);
  			return -ENOMEM;
  		}
  		btt->nlba += arena->external_nlba;
  		if (remaining >= ARENA_MIN_SIZE)
  			arena->nextoff = arena->size;
  		else
  			arena->nextoff = 0;
  		cur_off += arena_size;
  		list_add_tail(&arena->list, &btt->arena_list);
  	}
  
  	return 0;
  }
  
  /*
   * This function completes arena initialization by writing
   * all the metadata.
   * It is only called for an uninitialized arena when a write
   * to that arena occurs for the first time.
   */
fbde1414a   Vishal Verma   libnvdimm, btt: c...
954
  static int btt_arena_write_layout(struct arena_info *arena)
5212e11fd   Vishal Verma   nd_btt: atomic se...
955
956
  {
  	int ret;
e1455744b   Dan Williams   libnvdimm, pfn: '...
957
  	u64 sum;
5212e11fd   Vishal Verma   nd_btt: atomic se...
958
  	struct btt_sb *super;
fbde1414a   Vishal Verma   libnvdimm, btt: c...
959
  	struct nd_btt *nd_btt = arena->nd_btt;
6ec689542   Vishal Verma   libnvdimm, btt: w...
960
  	const u8 *parent_uuid = nd_dev_to_uuid(&nd_btt->ndns->dev);
5212e11fd   Vishal Verma   nd_btt: atomic se...
961
962
963
964
965
966
967
968
969
970
971
972
973
974
  
  	ret = btt_map_init(arena);
  	if (ret)
  		return ret;
  
  	ret = btt_log_init(arena);
  	if (ret)
  		return ret;
  
  	super = kzalloc(sizeof(struct btt_sb), GFP_NOIO);
  	if (!super)
  		return -ENOMEM;
  
  	strncpy(super->signature, BTT_SIG, BTT_SIG_LEN);
fbde1414a   Vishal Verma   libnvdimm, btt: c...
975
  	memcpy(super->uuid, nd_btt->uuid, 16);
6ec689542   Vishal Verma   libnvdimm, btt: w...
976
  	memcpy(super->parent_uuid, parent_uuid, 16);
5212e11fd   Vishal Verma   nd_btt: atomic se...
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
  	super->flags = cpu_to_le32(arena->flags);
  	super->version_major = cpu_to_le16(arena->version_major);
  	super->version_minor = cpu_to_le16(arena->version_minor);
  	super->external_lbasize = cpu_to_le32(arena->external_lbasize);
  	super->external_nlba = cpu_to_le32(arena->external_nlba);
  	super->internal_lbasize = cpu_to_le32(arena->internal_lbasize);
  	super->internal_nlba = cpu_to_le32(arena->internal_nlba);
  	super->nfree = cpu_to_le32(arena->nfree);
  	super->infosize = cpu_to_le32(sizeof(struct btt_sb));
  	super->nextoff = cpu_to_le64(arena->nextoff);
  	/*
  	 * Subtract arena->infooff (arena start) so numbers are relative
  	 * to 'this' arena
  	 */
  	super->dataoff = cpu_to_le64(arena->dataoff - arena->infooff);
  	super->mapoff = cpu_to_le64(arena->mapoff - arena->infooff);
  	super->logoff = cpu_to_le64(arena->logoff - arena->infooff);
  	super->info2off = cpu_to_le64(arena->info2off - arena->infooff);
  
  	super->flags = 0;
e1455744b   Dan Williams   libnvdimm, pfn: '...
997
998
  	sum = nd_sb_checksum((struct nd_gen_sb *) super);
  	super->checksum = cpu_to_le64(sum);
5212e11fd   Vishal Verma   nd_btt: atomic se...
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
  
  	ret = btt_info_write(arena, super);
  
  	kfree(super);
  	return ret;
  }
  
  /*
   * This function completes the initialization for the BTT namespace
   * such that it is ready to accept IOs
   */
  static int btt_meta_init(struct btt *btt)
  {
  	int ret = 0;
  	struct arena_info *arena;
  
  	mutex_lock(&btt->init_lock);
  	list_for_each_entry(arena, &btt->arena_list, list) {
fbde1414a   Vishal Verma   libnvdimm, btt: c...
1017
  		ret = btt_arena_write_layout(arena);
5212e11fd   Vishal Verma   nd_btt: atomic se...
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
  		if (ret)
  			goto unlock;
  
  		ret = btt_freelist_init(arena);
  		if (ret)
  			goto unlock;
  
  		ret = btt_rtt_init(arena);
  		if (ret)
  			goto unlock;
  
  		ret = btt_maplocks_init(arena);
  		if (ret)
  			goto unlock;
  	}
  
  	btt->init_state = INIT_READY;
  
   unlock:
  	mutex_unlock(&btt->init_lock);
  	return ret;
  }
41cd8b70c   Vishal Verma   libnvdimm, btt: a...
1040
1041
1042
1043
  static u32 btt_meta_size(struct btt *btt)
  {
  	return btt->lbasize - btt->sector_size;
  }
5212e11fd   Vishal Verma   nd_btt: atomic se...
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
  /*
   * This function calculates the arena in which the given LBA lies
   * by doing a linear walk. This is acceptable since we expect only
   * a few arenas. If we have backing devices that get much larger,
   * we can construct a balanced binary tree of arenas at init time
   * so that this range search becomes faster.
   */
  static int lba_to_arena(struct btt *btt, sector_t sector, __u32 *premap,
  				struct arena_info **arena)
  {
  	struct arena_info *arena_list;
  	__u64 lba = div_u64(sector << SECTOR_SHIFT, btt->sector_size);
  
  	list_for_each_entry(arena_list, &btt->arena_list, list) {
  		if (lba < arena_list->external_nlba) {
  			*arena = arena_list;
  			*premap = lba;
  			return 0;
  		}
  		lba -= arena_list->external_nlba;
  	}
  
  	return -EIO;
  }
  
  /*
   * The following (lock_map, unlock_map) are mostly just to improve
   * readability, since they index into an array of locks
   */
  static void lock_map(struct arena_info *arena, u32 premap)
  		__acquires(&arena->map_locks[idx].lock)
  {
  	u32 idx = (premap * MAP_ENT_SIZE / L1_CACHE_BYTES) % arena->nfree;
  
  	spin_lock(&arena->map_locks[idx].lock);
  }
  
  static void unlock_map(struct arena_info *arena, u32 premap)
  		__releases(&arena->map_locks[idx].lock)
  {
  	u32 idx = (premap * MAP_ENT_SIZE / L1_CACHE_BYTES) % arena->nfree;
  
  	spin_unlock(&arena->map_locks[idx].lock);
  }
5212e11fd   Vishal Verma   nd_btt: atomic se...
1088
1089
1090
1091
1092
1093
  static int btt_data_read(struct arena_info *arena, struct page *page,
  			unsigned int off, u32 lba, u32 len)
  {
  	int ret;
  	u64 nsoff = to_namespace_offset(arena, lba);
  	void *mem = kmap_atomic(page);
3ae3d67ba   Vishal Verma   libnvdimm: add an...
1094
  	ret = arena_read_bytes(arena, nsoff, mem + off, len, NVDIMM_IO_ATOMIC);
5212e11fd   Vishal Verma   nd_btt: atomic se...
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
  	kunmap_atomic(mem);
  
  	return ret;
  }
  
  static int btt_data_write(struct arena_info *arena, u32 lba,
  			struct page *page, unsigned int off, u32 len)
  {
  	int ret;
  	u64 nsoff = to_namespace_offset(arena, lba);
  	void *mem = kmap_atomic(page);
3ae3d67ba   Vishal Verma   libnvdimm: add an...
1106
  	ret = arena_write_bytes(arena, nsoff, mem + off, len, NVDIMM_IO_ATOMIC);
5212e11fd   Vishal Verma   nd_btt: atomic se...
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
  	kunmap_atomic(mem);
  
  	return ret;
  }
  
  static void zero_fill_data(struct page *page, unsigned int off, u32 len)
  {
  	void *mem = kmap_atomic(page);
  
  	memset(mem + off, 0, len);
  	kunmap_atomic(mem);
  }
41cd8b70c   Vishal Verma   libnvdimm, btt: a...
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
  #ifdef CONFIG_BLK_DEV_INTEGRITY
  static int btt_rw_integrity(struct btt *btt, struct bio_integrity_payload *bip,
  			struct arena_info *arena, u32 postmap, int rw)
  {
  	unsigned int len = btt_meta_size(btt);
  	u64 meta_nsoff;
  	int ret = 0;
  
  	if (bip == NULL)
  		return 0;
  
  	meta_nsoff = to_namespace_offset(arena, postmap) + btt->sector_size;
  
  	while (len) {
  		unsigned int cur_len;
  		struct bio_vec bv;
  		void *mem;
  
  		bv = bvec_iter_bvec(bip->bip_vec, bip->bip_iter);
  		/*
  		 * The 'bv' obtained from bvec_iter_bvec has its .bv_len and
  		 * .bv_offset already adjusted for iter->bi_bvec_done, and we
  		 * can use those directly
  		 */
  
  		cur_len = min(len, bv.bv_len);
  		mem = kmap_atomic(bv.bv_page);
  		if (rw)
  			ret = arena_write_bytes(arena, meta_nsoff,
3ae3d67ba   Vishal Verma   libnvdimm: add an...
1148
1149
  					mem + bv.bv_offset, cur_len,
  					NVDIMM_IO_ATOMIC);
41cd8b70c   Vishal Verma   libnvdimm, btt: a...
1150
1151
  		else
  			ret = arena_read_bytes(arena, meta_nsoff,
3ae3d67ba   Vishal Verma   libnvdimm: add an...
1152
1153
  					mem + bv.bv_offset, cur_len,
  					NVDIMM_IO_ATOMIC);
41cd8b70c   Vishal Verma   libnvdimm, btt: a...
1154
1155
1156
1157
1158
1159
1160
  
  		kunmap_atomic(mem);
  		if (ret)
  			return ret;
  
  		len -= cur_len;
  		meta_nsoff += cur_len;
b1fb2c52b   Dmitry Monakhov   block: guard bvec...
1161
1162
  		if (!bvec_iter_advance(bip->bip_vec, &bip->bip_iter, cur_len))
  			return -EIO;
41cd8b70c   Vishal Verma   libnvdimm, btt: a...
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
  	}
  
  	return ret;
  }
  
  #else /* CONFIG_BLK_DEV_INTEGRITY */
  static int btt_rw_integrity(struct btt *btt, struct bio_integrity_payload *bip,
  			struct arena_info *arena, u32 postmap, int rw)
  {
  	return 0;
  }
  #endif
  
  static int btt_read_pg(struct btt *btt, struct bio_integrity_payload *bip,
  			struct page *page, unsigned int off, sector_t sector,
  			unsigned int len)
5212e11fd   Vishal Verma   nd_btt: atomic se...
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
  {
  	int ret = 0;
  	int t_flag, e_flag;
  	struct arena_info *arena = NULL;
  	u32 lane = 0, premap, postmap;
  
  	while (len) {
  		u32 cur_len;
  
  		lane = nd_region_acquire_lane(btt->nd_region);
  
  		ret = lba_to_arena(btt, sector, &premap, &arena);
  		if (ret)
  			goto out_lane;
  
  		cur_len = min(btt->sector_size, len);
3ae3d67ba   Vishal Verma   libnvdimm: add an...
1195
1196
  		ret = btt_map_read(arena, premap, &postmap, &t_flag, &e_flag,
  				NVDIMM_IO_ATOMIC);
5212e11fd   Vishal Verma   nd_btt: atomic se...
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
  		if (ret)
  			goto out_lane;
  
  		/*
  		 * We loop to make sure that the post map LBA didn't change
  		 * from under us between writing the RTT and doing the actual
  		 * read.
  		 */
  		while (1) {
  			u32 new_map;
1398199d8   Vishal Verma   libnvdimm, btt: e...
1207
  			int new_t, new_e;
5212e11fd   Vishal Verma   nd_btt: atomic se...
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
  
  			if (t_flag) {
  				zero_fill_data(page, off, cur_len);
  				goto out_lane;
  			}
  
  			if (e_flag) {
  				ret = -EIO;
  				goto out_lane;
  			}
  
  			arena->rtt[lane] = RTT_VALID | postmap;
  			/*
  			 * Barrier to make sure this write is not reordered
  			 * to do the verification map_read before the RTT store
  			 */
  			barrier();
1398199d8   Vishal Verma   libnvdimm, btt: e...
1225
1226
  			ret = btt_map_read(arena, premap, &new_map, &new_t,
  						&new_e, NVDIMM_IO_ATOMIC);
5212e11fd   Vishal Verma   nd_btt: atomic se...
1227
1228
  			if (ret)
  				goto out_rtt;
1398199d8   Vishal Verma   libnvdimm, btt: e...
1229
1230
  			if ((postmap == new_map) && (t_flag == new_t) &&
  					(e_flag == new_e))
5212e11fd   Vishal Verma   nd_btt: atomic se...
1231
1232
1233
  				break;
  
  			postmap = new_map;
1398199d8   Vishal Verma   libnvdimm, btt: e...
1234
1235
  			t_flag = new_t;
  			e_flag = new_e;
5212e11fd   Vishal Verma   nd_btt: atomic se...
1236
1237
1238
  		}
  
  		ret = btt_data_read(arena, page, off, postmap, cur_len);
d9b83c756   Vishal Verma   libnvdimm, btt: r...
1239
  		if (ret) {
d9b83c756   Vishal Verma   libnvdimm, btt: r...
1240
  			/* Media error - set the e_flag */
4e24e37d5   Qian Cai   libnvdimm/btt: fi...
1241
1242
1243
1244
1245
  			if (btt_map_write(arena, premap, postmap, 0, 1, NVDIMM_IO_ATOMIC))
  				dev_warn_ratelimited(to_dev(arena),
  					"Error persistently tracking bad blocks at %#x
  ",
  					premap);
5212e11fd   Vishal Verma   nd_btt: atomic se...
1246
  			goto out_rtt;
d9b83c756   Vishal Verma   libnvdimm, btt: r...
1247
  		}
5212e11fd   Vishal Verma   nd_btt: atomic se...
1248

41cd8b70c   Vishal Verma   libnvdimm, btt: a...
1249
1250
1251
1252
1253
  		if (bip) {
  			ret = btt_rw_integrity(btt, bip, arena, postmap, READ);
  			if (ret)
  				goto out_rtt;
  		}
5212e11fd   Vishal Verma   nd_btt: atomic se...
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
  		arena->rtt[lane] = RTT_INVALID;
  		nd_region_release_lane(btt->nd_region, lane);
  
  		len -= cur_len;
  		off += cur_len;
  		sector += btt->sector_size >> SECTOR_SHIFT;
  	}
  
  	return 0;
  
   out_rtt:
  	arena->rtt[lane] = RTT_INVALID;
   out_lane:
  	nd_region_release_lane(btt->nd_region, lane);
  	return ret;
  }
d9b83c756   Vishal Verma   libnvdimm, btt: r...
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
  /*
   * Normally, arena_{read,write}_bytes will take care of the initial offset
   * adjustment, but in the case of btt_is_badblock, where we query is_bad_pmem,
   * we need the final, raw namespace offset here
   */
  static bool btt_is_badblock(struct btt *btt, struct arena_info *arena,
  		u32 postmap)
  {
  	u64 nsoff = adjust_initial_offset(arena->nd_btt,
  			to_namespace_offset(arena, postmap));
  	sector_t phys_sector = nsoff >> 9;
  
  	return is_bad_pmem(btt->phys_bb, phys_sector, arena->internal_lbasize);
  }
41cd8b70c   Vishal Verma   libnvdimm, btt: a...
1284
1285
1286
  static int btt_write_pg(struct btt *btt, struct bio_integrity_payload *bip,
  			sector_t sector, struct page *page, unsigned int off,
  			unsigned int len)
5212e11fd   Vishal Verma   nd_btt: atomic se...
1287
1288
1289
1290
1291
1292
1293
1294
1295
  {
  	int ret = 0;
  	struct arena_info *arena = NULL;
  	u32 premap = 0, old_postmap, new_postmap, lane = 0, i;
  	struct log_entry log;
  	int sub;
  
  	while (len) {
  		u32 cur_len;
d9b83c756   Vishal Verma   libnvdimm, btt: r...
1296
  		int e_flag;
5212e11fd   Vishal Verma   nd_btt: atomic se...
1297

d9b83c756   Vishal Verma   libnvdimm, btt: r...
1298
   retry:
5212e11fd   Vishal Verma   nd_btt: atomic se...
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
  		lane = nd_region_acquire_lane(btt->nd_region);
  
  		ret = lba_to_arena(btt, sector, &premap, &arena);
  		if (ret)
  			goto out_lane;
  		cur_len = min(btt->sector_size, len);
  
  		if ((arena->flags & IB_FLAG_ERROR_MASK) != 0) {
  			ret = -EIO;
  			goto out_lane;
  		}
d9b83c756   Vishal Verma   libnvdimm, btt: r...
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
  		if (btt_is_badblock(btt, arena, arena->freelist[lane].block))
  			arena->freelist[lane].has_err = 1;
  
  		if (mutex_is_locked(&arena->err_lock)
  				|| arena->freelist[lane].has_err) {
  			nd_region_release_lane(btt->nd_region, lane);
  
  			ret = arena_clear_freelist_error(arena, lane);
  			if (ret)
  				return ret;
  
  			/* OK to acquire a different lane/free block */
  			goto retry;
  		}
5212e11fd   Vishal Verma   nd_btt: atomic se...
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
  		new_postmap = arena->freelist[lane].block;
  
  		/* Wait if the new block is being read from */
  		for (i = 0; i < arena->nfree; i++)
  			while (arena->rtt[i] == (RTT_VALID | new_postmap))
  				cpu_relax();
  
  
  		if (new_postmap >= arena->internal_nlba) {
  			ret = -EIO;
  			goto out_lane;
41cd8b70c   Vishal Verma   libnvdimm, btt: a...
1335
1336
1337
  		}
  
  		ret = btt_data_write(arena, new_postmap, page, off, cur_len);
5212e11fd   Vishal Verma   nd_btt: atomic se...
1338
1339
  		if (ret)
  			goto out_lane;
41cd8b70c   Vishal Verma   libnvdimm, btt: a...
1340
1341
1342
1343
1344
1345
  		if (bip) {
  			ret = btt_rw_integrity(btt, bip, arena, new_postmap,
  						WRITE);
  			if (ret)
  				goto out_lane;
  		}
5212e11fd   Vishal Verma   nd_btt: atomic se...
1346
  		lock_map(arena, premap);
d9b83c756   Vishal Verma   libnvdimm, btt: r...
1347
  		ret = btt_map_read(arena, premap, &old_postmap, NULL, &e_flag,
3ae3d67ba   Vishal Verma   libnvdimm: add an...
1348
  				NVDIMM_IO_ATOMIC);
5212e11fd   Vishal Verma   nd_btt: atomic se...
1349
1350
1351
1352
1353
1354
  		if (ret)
  			goto out_map;
  		if (old_postmap >= arena->internal_nlba) {
  			ret = -EIO;
  			goto out_map;
  		}
d9b83c756   Vishal Verma   libnvdimm, btt: r...
1355
1356
  		if (e_flag)
  			set_e_flag(old_postmap);
5212e11fd   Vishal Verma   nd_btt: atomic se...
1357
1358
1359
1360
1361
1362
1363
1364
1365
  
  		log.lba = cpu_to_le32(premap);
  		log.old_map = cpu_to_le32(old_postmap);
  		log.new_map = cpu_to_le32(new_postmap);
  		log.seq = cpu_to_le32(arena->freelist[lane].seq);
  		sub = arena->freelist[lane].sub;
  		ret = btt_flog_write(arena, lane, sub, &log);
  		if (ret)
  			goto out_map;
1db1f3cea   Vishal Verma   libnvdimm, btt: f...
1366
1367
  		ret = btt_map_write(arena, premap, new_postmap, 0, 0,
  			NVDIMM_IO_ATOMIC);
5212e11fd   Vishal Verma   nd_btt: atomic se...
1368
1369
1370
1371
1372
  		if (ret)
  			goto out_map;
  
  		unlock_map(arena, premap);
  		nd_region_release_lane(btt->nd_region, lane);
d9b83c756   Vishal Verma   libnvdimm, btt: r...
1373
1374
1375
1376
1377
  		if (e_flag) {
  			ret = arena_clear_freelist_error(arena, lane);
  			if (ret)
  				return ret;
  		}
5212e11fd   Vishal Verma   nd_btt: atomic se...
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
  		len -= cur_len;
  		off += cur_len;
  		sector += btt->sector_size >> SECTOR_SHIFT;
  	}
  
  	return 0;
  
   out_map:
  	unlock_map(arena, premap);
   out_lane:
  	nd_region_release_lane(btt->nd_region, lane);
  	return ret;
  }
41cd8b70c   Vishal Verma   libnvdimm, btt: a...
1391
1392
  static int btt_do_bvec(struct btt *btt, struct bio_integrity_payload *bip,
  			struct page *page, unsigned int len, unsigned int off,
3f289dcb4   Tejun Heo   block: make bdev_...
1393
  			unsigned int op, sector_t sector)
5212e11fd   Vishal Verma   nd_btt: atomic se...
1394
1395
  {
  	int ret;
3f289dcb4   Tejun Heo   block: make bdev_...
1396
  	if (!op_is_write(op)) {
41cd8b70c   Vishal Verma   libnvdimm, btt: a...
1397
  		ret = btt_read_pg(btt, bip, page, off, sector, len);
5212e11fd   Vishal Verma   nd_btt: atomic se...
1398
1399
1400
  		flush_dcache_page(page);
  	} else {
  		flush_dcache_page(page);
41cd8b70c   Vishal Verma   libnvdimm, btt: a...
1401
  		ret = btt_write_pg(btt, bip, sector, page, off, len);
5212e11fd   Vishal Verma   nd_btt: atomic se...
1402
1403
1404
1405
  	}
  
  	return ret;
  }
c62b37d96   Christoph Hellwig   block: move ->mak...
1406
  static blk_qc_t btt_submit_bio(struct bio *bio)
5212e11fd   Vishal Verma   nd_btt: atomic se...
1407
  {
41cd8b70c   Vishal Verma   libnvdimm, btt: a...
1408
  	struct bio_integrity_payload *bip = bio_integrity(bio);
5713bcc3f   Christoph Hellwig   nvdimm/btt: stop ...
1409
  	struct btt *btt = bio->bi_disk->private_data;
5212e11fd   Vishal Verma   nd_btt: atomic se...
1410
  	struct bvec_iter iter;
f0dc089ce   Dan Williams   libnvdimm: enable...
1411
  	unsigned long start;
5212e11fd   Vishal Verma   nd_btt: atomic se...
1412
  	struct bio_vec bvec;
abf545484   Mike Christie   mm/block: convert...
1413
  	int err = 0;
f0dc089ce   Dan Williams   libnvdimm: enable...
1414
  	bool do_acct;
5212e11fd   Vishal Verma   nd_btt: atomic se...
1415

e23947bd7   Dmitry Monakhov   bio-integrity: fo...
1416
1417
  	if (!bio_integrity_prep(bio))
  		return BLK_QC_T_NONE;
41cd8b70c   Vishal Verma   libnvdimm, btt: a...
1418

0fd92f89a   Christoph Hellwig   nvdimm: use bio_{...
1419
1420
1421
  	do_acct = blk_queue_io_stat(bio->bi_disk->queue);
  	if (do_acct)
  		start = bio_start_io_acct(bio);
5212e11fd   Vishal Verma   nd_btt: atomic se...
1422
1423
  	bio_for_each_segment(bvec, bio, iter) {
  		unsigned int len = bvec.bv_len;
86652d2eb   Vishal Verma   libnvdimm, btt: c...
1424
1425
1426
1427
1428
1429
1430
1431
  		if (len > PAGE_SIZE || len < btt->sector_size ||
  				len % btt->sector_size) {
  			dev_err_ratelimited(&btt->nd_btt->dev,
  				"unaligned bio segment (len: %d)
  ", len);
  			bio->bi_status = BLK_STS_IOERR;
  			break;
  		}
5212e11fd   Vishal Verma   nd_btt: atomic se...
1432

41cd8b70c   Vishal Verma   libnvdimm, btt: a...
1433
  		err = btt_do_bvec(btt, bip, bvec.bv_page, len, bvec.bv_offset,
3f289dcb4   Tejun Heo   block: make bdev_...
1434
  				  bio_op(bio), iter.bi_sector);
5212e11fd   Vishal Verma   nd_btt: atomic se...
1435
  		if (err) {
e6be2dcbe   Vishal Verma   libnvdimm, btt: c...
1436
  			dev_err(&btt->nd_btt->dev,
5212e11fd   Vishal Verma   nd_btt: atomic se...
1437
1438
  					"io error in %s sector %lld, len %d,
  ",
abf545484   Mike Christie   mm/block: convert...
1439
1440
  					(op_is_write(bio_op(bio))) ? "WRITE" :
  					"READ",
5212e11fd   Vishal Verma   nd_btt: atomic se...
1441
  					(unsigned long long) iter.bi_sector, len);
4e4cbee93   Christoph Hellwig   block: switch bio...
1442
  			bio->bi_status = errno_to_blk_status(err);
f0dc089ce   Dan Williams   libnvdimm: enable...
1443
  			break;
5212e11fd   Vishal Verma   nd_btt: atomic se...
1444
1445
  		}
  	}
f0dc089ce   Dan Williams   libnvdimm: enable...
1446
  	if (do_acct)
0fd92f89a   Christoph Hellwig   nvdimm: use bio_{...
1447
  		bio_end_io_acct(bio, start);
5212e11fd   Vishal Verma   nd_btt: atomic se...
1448

4246a0b63   Christoph Hellwig   block: add a bi_e...
1449
  	bio_endio(bio);
dece16353   Jens Axboe   block: change ->m...
1450
  	return BLK_QC_T_NONE;
5212e11fd   Vishal Verma   nd_btt: atomic se...
1451
1452
1453
  }
  
  static int btt_rw_page(struct block_device *bdev, sector_t sector,
3f289dcb4   Tejun Heo   block: make bdev_...
1454
  		struct page *page, unsigned int op)
5212e11fd   Vishal Verma   nd_btt: atomic se...
1455
1456
  {
  	struct btt *btt = bdev->bd_disk->private_data;
c13c43d54   Vishal Verma   libnvdimm, btt: f...
1457
  	int rc;
5212e11fd   Vishal Verma   nd_btt: atomic se...
1458

af3bbc12d   Matthew Wilcox (Oracle)   mm: add thp_size
1459
  	rc = btt_do_bvec(btt, NULL, page, thp_size(page), 0, op, sector);
c13c43d54   Vishal Verma   libnvdimm, btt: f...
1460
  	if (rc == 0)
3f289dcb4   Tejun Heo   block: make bdev_...
1461
  		page_endio(page, op_is_write(op), 0);
c13c43d54   Vishal Verma   libnvdimm, btt: f...
1462
1463
  
  	return rc;
5212e11fd   Vishal Verma   nd_btt: atomic se...
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
  }
  
  
  static int btt_getgeo(struct block_device *bd, struct hd_geometry *geo)
  {
  	/* some standard values */
  	geo->heads = 1 << 6;
  	geo->sectors = 1 << 5;
  	geo->cylinders = get_capacity(bd->bd_disk) >> 11;
  	return 0;
  }
  
  static const struct block_device_operations btt_fops = {
  	.owner =		THIS_MODULE,
c62b37d96   Christoph Hellwig   block: move ->mak...
1478
  	.submit_bio =		btt_submit_bio,
5212e11fd   Vishal Verma   nd_btt: atomic se...
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
  	.rw_page =		btt_rw_page,
  	.getgeo =		btt_getgeo,
  };
  
  static int btt_blk_init(struct btt *btt)
  {
  	struct nd_btt *nd_btt = btt->nd_btt;
  	struct nd_namespace_common *ndns = nd_btt->ndns;
  
  	/* create a new disk and request queue for btt */
c62b37d96   Christoph Hellwig   block: move ->mak...
1489
  	btt->btt_queue = blk_alloc_queue(NUMA_NO_NODE);
5212e11fd   Vishal Verma   nd_btt: atomic se...
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
  	if (!btt->btt_queue)
  		return -ENOMEM;
  
  	btt->btt_disk = alloc_disk(0);
  	if (!btt->btt_disk) {
  		blk_cleanup_queue(btt->btt_queue);
  		return -ENOMEM;
  	}
  
  	nvdimm_namespace_disk_name(ndns, btt->btt_disk->disk_name);
5212e11fd   Vishal Verma   nd_btt: atomic se...
1500
1501
1502
1503
1504
  	btt->btt_disk->first_minor = 0;
  	btt->btt_disk->fops = &btt_fops;
  	btt->btt_disk->private_data = btt;
  	btt->btt_disk->queue = btt->btt_queue;
  	btt->btt_disk->flags = GENHD_FL_EXT_DEVT;
5212e11fd   Vishal Verma   nd_btt: atomic se...
1505
1506
  	blk_queue_logical_block_size(btt->btt_queue, btt->sector_size);
  	blk_queue_max_hw_sectors(btt->btt_queue, UINT_MAX);
8b904b5b6   Bart Van Assche   block: Use blk_qu...
1507
  	blk_queue_flag_set(QUEUE_FLAG_NONROT, btt->btt_queue);
5212e11fd   Vishal Verma   nd_btt: atomic se...
1508

41cd8b70c   Vishal Verma   libnvdimm, btt: a...
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
  	if (btt_meta_size(btt)) {
  		int rc = nd_integrity_init(btt->btt_disk, btt_meta_size(btt));
  
  		if (rc) {
  			del_gendisk(btt->btt_disk);
  			put_disk(btt->btt_disk);
  			blk_cleanup_queue(btt->btt_queue);
  			return rc;
  		}
  	}
  	set_capacity(btt->btt_disk, btt->nlba * btt->sector_size >> 9);
fef912bf8   Hannes Reinecke   block: genhd: add...
1520
  	device_add_disk(&btt->nd_btt->dev, btt->btt_disk, NULL);
abe8b4e3c   Vishal Verma   nvdimm, btt: add ...
1521
  	btt->nd_btt->size = btt->nlba * (u64)btt->sector_size;
32f61d675   Christoph Hellwig   nvdimm: simplify ...
1522
  	nvdimm_check_and_set_ro(btt->btt_disk);
5212e11fd   Vishal Verma   nd_btt: atomic se...
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
  
  	return 0;
  }
  
  static void btt_blk_cleanup(struct btt *btt)
  {
  	del_gendisk(btt->btt_disk);
  	put_disk(btt->btt_disk);
  	blk_cleanup_queue(btt->btt_queue);
  }
  
  /**
   * btt_init - initialize a block translation table for the given device
   * @nd_btt:	device with BTT geometry and backing device info
   * @rawsize:	raw size in bytes of the backing device
   * @lbasize:	lba size of the backing device
   * @uuid:	A uuid for the backing device - this is stored on media
   * @maxlane:	maximum number of parallel requests the device can handle
   *
   * Initialize a Block Translation Table on a backing device to provide
   * single sector power fail atomicity.
   *
   * Context:
   * Might sleep.
   *
   * Returns:
   * Pointer to a new struct btt on success, NULL on failure.
   */
  static struct btt *btt_init(struct nd_btt *nd_btt, unsigned long long rawsize,
  		u32 lbasize, u8 *uuid, struct nd_region *nd_region)
  {
  	int ret;
  	struct btt *btt;
d9b83c756   Vishal Verma   libnvdimm, btt: r...
1556
  	struct nd_namespace_io *nsio;
5212e11fd   Vishal Verma   nd_btt: atomic se...
1557
  	struct device *dev = &nd_btt->dev;
e32bc729a   Dan Williams   libnvdimm, btt, c...
1558
  	btt = devm_kzalloc(dev, sizeof(struct btt), GFP_KERNEL);
5212e11fd   Vishal Verma   nd_btt: atomic se...
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
  	if (!btt)
  		return NULL;
  
  	btt->nd_btt = nd_btt;
  	btt->rawsize = rawsize;
  	btt->lbasize = lbasize;
  	btt->sector_size = ((lbasize >= 4096) ? 4096 : 512);
  	INIT_LIST_HEAD(&btt->arena_list);
  	mutex_init(&btt->init_lock);
  	btt->nd_region = nd_region;
d9b83c756   Vishal Verma   libnvdimm, btt: r...
1569
1570
  	nsio = to_nd_namespace_io(&nd_btt->ndns->dev);
  	btt->phys_bb = &nsio->bb;
5212e11fd   Vishal Verma   nd_btt: atomic se...
1571
1572
1573
1574
1575
  
  	ret = discover_arenas(btt);
  	if (ret) {
  		dev_err(dev, "init: error in arena_discover: %d
  ", ret);
e32bc729a   Dan Williams   libnvdimm, btt, c...
1576
  		return NULL;
5212e11fd   Vishal Verma   nd_btt: atomic se...
1577
  	}
581388209   Dan Williams   libnvdimm, nfit: ...
1578
  	if (btt->init_state != INIT_READY && nd_region->ro) {
e6be2dcbe   Vishal Verma   libnvdimm, btt: c...
1579
1580
  		dev_warn(dev, "%s is read-only, unable to init btt metadata
  ",
581388209   Dan Williams   libnvdimm, nfit: ...
1581
  				dev_name(&nd_region->dev));
e32bc729a   Dan Williams   libnvdimm, btt, c...
1582
  		return NULL;
581388209   Dan Williams   libnvdimm, nfit: ...
1583
  	} else if (btt->init_state != INIT_READY) {
5212e11fd   Vishal Verma   nd_btt: atomic se...
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
  		btt->num_arenas = (rawsize / ARENA_MAX_SIZE) +
  			((rawsize % ARENA_MAX_SIZE) ? 1 : 0);
  		dev_dbg(dev, "init: %d arenas for %llu rawsize
  ",
  				btt->num_arenas, rawsize);
  
  		ret = create_arenas(btt);
  		if (ret) {
  			dev_info(dev, "init: create_arenas: %d
  ", ret);
e32bc729a   Dan Williams   libnvdimm, btt, c...
1594
  			return NULL;
5212e11fd   Vishal Verma   nd_btt: atomic se...
1595
1596
1597
1598
1599
1600
  		}
  
  		ret = btt_meta_init(btt);
  		if (ret) {
  			dev_err(dev, "init: error in meta_init: %d
  ", ret);
e32bc729a   Dan Williams   libnvdimm, btt, c...
1601
  			return NULL;
5212e11fd   Vishal Verma   nd_btt: atomic se...
1602
1603
1604
1605
1606
1607
1608
  		}
  	}
  
  	ret = btt_blk_init(btt);
  	if (ret) {
  		dev_err(dev, "init: error in blk_init: %d
  ", ret);
e32bc729a   Dan Williams   libnvdimm, btt, c...
1609
  		return NULL;
5212e11fd   Vishal Verma   nd_btt: atomic se...
1610
1611
1612
1613
1614
  	}
  
  	btt_debugfs_init(btt);
  
  	return btt;
5212e11fd   Vishal Verma   nd_btt: atomic se...
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
  }
  
  /**
   * btt_fini - de-initialize a BTT
   * @btt:	the BTT handle that was generated by btt_init
   *
   * De-initialize a Block Translation Table on device removal
   *
   * Context:
   * Might sleep.
   */
  static void btt_fini(struct btt *btt)
  {
  	if (btt) {
  		btt_blk_cleanup(btt);
  		free_arenas(btt);
  		debugfs_remove_recursive(btt->debugfs_dir);
5212e11fd   Vishal Verma   nd_btt: atomic se...
1632
1633
1634
1635
1636
1637
1638
  	}
  }
  
  int nvdimm_namespace_attach_btt(struct nd_namespace_common *ndns)
  {
  	struct nd_btt *nd_btt = to_nd_btt(ndns->claim);
  	struct nd_region *nd_region;
14e494542   Vishal Verma   libnvdimm, btt: B...
1639
  	struct btt_sb *btt_sb;
5212e11fd   Vishal Verma   nd_btt: atomic se...
1640
  	struct btt *btt;
8f4b01fcd   Aneesh Kumar K.V   libnvdimm/namespa...
1641
1642
  	size_t size, rawsize;
  	int rc;
5212e11fd   Vishal Verma   nd_btt: atomic se...
1643

9dec4892c   Dan Williams   libnvdimm, btt: a...
1644
1645
1646
  	if (!nd_btt->uuid || !nd_btt->ndns || !nd_btt->lbasize) {
  		dev_dbg(&nd_btt->dev, "incomplete btt configuration
  ");
5212e11fd   Vishal Verma   nd_btt: atomic se...
1647
  		return -ENODEV;
9dec4892c   Dan Williams   libnvdimm, btt: a...
1648
  	}
5212e11fd   Vishal Verma   nd_btt: atomic se...
1649

14e494542   Vishal Verma   libnvdimm, btt: B...
1650
  	btt_sb = devm_kzalloc(&nd_btt->dev, sizeof(*btt_sb), GFP_KERNEL);
ed36b4dba   Christophe Jaillet   libnvdimm, btt: c...
1651
1652
  	if (!btt_sb)
  		return -ENOMEM;
14e494542   Vishal Verma   libnvdimm, btt: B...
1653

8f4b01fcd   Aneesh Kumar K.V   libnvdimm/namespa...
1654
1655
1656
1657
  	size = nvdimm_namespace_capacity(ndns);
  	rc = devm_namespace_enable(&nd_btt->dev, ndns, size);
  	if (rc)
  		return rc;
14e494542   Vishal Verma   libnvdimm, btt: B...
1658
1659
1660
1661
1662
1663
1664
  	/*
  	 * If this returns < 0, that is ok as it just means there wasn't
  	 * an existing BTT, and we're creating a new one. We still need to
  	 * call this as we need the version dependent fields in nd_btt to be
  	 * set correctly based on the holder class
  	 */
  	nd_btt_version(nd_btt, ndns, btt_sb);
8f4b01fcd   Aneesh Kumar K.V   libnvdimm/namespa...
1665
  	rawsize = size - nd_btt->initial_offset;
5212e11fd   Vishal Verma   nd_btt: atomic se...
1666
  	if (rawsize < ARENA_MIN_SIZE) {
9dec4892c   Dan Williams   libnvdimm, btt: a...
1667
1668
  		dev_dbg(&nd_btt->dev, "%s must be at least %ld bytes
  ",
14e494542   Vishal Verma   libnvdimm, btt: B...
1669
1670
  				dev_name(&ndns->dev),
  				ARENA_MIN_SIZE + nd_btt->initial_offset);
5212e11fd   Vishal Verma   nd_btt: atomic se...
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
  		return -ENXIO;
  	}
  	nd_region = to_nd_region(nd_btt->dev.parent);
  	btt = btt_init(nd_btt, rawsize, nd_btt->lbasize, nd_btt->uuid,
  			nd_region);
  	if (!btt)
  		return -ENOMEM;
  	nd_btt->btt = btt;
  
  	return 0;
  }
  EXPORT_SYMBOL(nvdimm_namespace_attach_btt);
298f2bc5d   Dan Williams   libnvdimm, pmem: ...
1683
  int nvdimm_namespace_detach_btt(struct nd_btt *nd_btt)
5212e11fd   Vishal Verma   nd_btt: atomic se...
1684
  {
5212e11fd   Vishal Verma   nd_btt: atomic se...
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
  	struct btt *btt = nd_btt->btt;
  
  	btt_fini(btt);
  	nd_btt->btt = NULL;
  
  	return 0;
  }
  EXPORT_SYMBOL(nvdimm_namespace_detach_btt);
  
  static int __init nd_btt_init(void)
  {
ff8e92d5d   NeilBrown   nvdimm/btt: don't...
1696
  	int rc = 0;
5212e11fd   Vishal Verma   nd_btt: atomic se...
1697
1698
  
  	debugfs_root = debugfs_create_dir("btt", NULL);
ff8e92d5d   NeilBrown   nvdimm/btt: don't...
1699
  	if (IS_ERR_OR_NULL(debugfs_root))
5212e11fd   Vishal Verma   nd_btt: atomic se...
1700
  		rc = -ENXIO;
5212e11fd   Vishal Verma   nd_btt: atomic se...
1701
1702
1703
1704
1705
1706
1707
  
  	return rc;
  }
  
  static void __exit nd_btt_exit(void)
  {
  	debugfs_remove_recursive(debugfs_root);
5212e11fd   Vishal Verma   nd_btt: atomic se...
1708
1709
1710
1711
1712
1713
1714
  }
  
  MODULE_ALIAS_ND_DEVICE(ND_DEVICE_BTT);
  MODULE_AUTHOR("Vishal Verma <vishal.l.verma@linux.intel.com>");
  MODULE_LICENSE("GPL v2");
  module_init(nd_btt_init);
  module_exit(nd_btt_exit);