lightnvm: pblk: allow allocation of new lines during shutdown
[linux-2.6-block.git] / drivers / lightnvm / pblk-rb.c
CommitLineData
a4bd217b
JG
1/*
2 * Copyright (C) 2016 CNEX Labs
3 * Initial release: Javier Gonzalez <javier@cnexlabs.com>
4 *
5 * Based upon the circular ringbuffer.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version
9 * 2 as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * pblk-rb.c - pblk's write buffer
17 */
18
19#include <linux/circ_buf.h>
20
21#include "pblk.h"
22
23static DECLARE_RWSEM(pblk_rb_lock);
24
25void pblk_rb_data_free(struct pblk_rb *rb)
26{
27 struct pblk_rb_pages *p, *t;
28
29 down_write(&pblk_rb_lock);
30 list_for_each_entry_safe(p, t, &rb->pages, list) {
31 free_pages((unsigned long)page_address(p->pages), p->order);
32 list_del(&p->list);
33 kfree(p);
34 }
35 up_write(&pblk_rb_lock);
36}
37
38/*
39 * Initialize ring buffer. The data and metadata buffers must be previously
40 * allocated and their size must be a power of two
41 * (Documentation/circular-buffers.txt)
42 */
43int pblk_rb_init(struct pblk_rb *rb, struct pblk_rb_entry *rb_entry_base,
44 unsigned int power_size, unsigned int power_seg_sz)
45{
46 struct pblk *pblk = container_of(rb, struct pblk, rwb);
47 unsigned int init_entry = 0;
48 unsigned int alloc_order = power_size;
49 unsigned int max_order = MAX_ORDER - 1;
50 unsigned int order, iter;
51
52 down_write(&pblk_rb_lock);
53 rb->entries = rb_entry_base;
54 rb->seg_size = (1 << power_seg_sz);
55 rb->nr_entries = (1 << power_size);
56 rb->mem = rb->subm = rb->sync = rb->l2p_update = 0;
8154d296 57 rb->flush_point = EMPTY_ENTRY;
a4bd217b
JG
58
59 spin_lock_init(&rb->w_lock);
60 spin_lock_init(&rb->s_lock);
61
62 INIT_LIST_HEAD(&rb->pages);
63
64 if (alloc_order >= max_order) {
65 order = max_order;
66 iter = (1 << (alloc_order - max_order));
67 } else {
68 order = alloc_order;
69 iter = 1;
70 }
71
72 do {
73 struct pblk_rb_entry *entry;
74 struct pblk_rb_pages *page_set;
75 void *kaddr;
76 unsigned long set_size;
77 int i;
78
79 page_set = kmalloc(sizeof(struct pblk_rb_pages), GFP_KERNEL);
80 if (!page_set) {
81 up_write(&pblk_rb_lock);
82 return -ENOMEM;
83 }
84
85 page_set->order = order;
86 page_set->pages = alloc_pages(GFP_KERNEL, order);
87 if (!page_set->pages) {
88 kfree(page_set);
89 pblk_rb_data_free(rb);
90 up_write(&pblk_rb_lock);
91 return -ENOMEM;
92 }
93 kaddr = page_address(page_set->pages);
94
95 entry = &rb->entries[init_entry];
96 entry->data = kaddr;
97 entry->cacheline = pblk_cacheline_to_addr(init_entry++);
98 entry->w_ctx.flags = PBLK_WRITABLE_ENTRY;
99
100 set_size = (1 << order);
101 for (i = 1; i < set_size; i++) {
102 entry = &rb->entries[init_entry];
103 entry->cacheline = pblk_cacheline_to_addr(init_entry++);
104 entry->data = kaddr + (i * rb->seg_size);
105 entry->w_ctx.flags = PBLK_WRITABLE_ENTRY;
106 bio_list_init(&entry->w_ctx.bios);
107 }
108
109 list_add_tail(&page_set->list, &rb->pages);
110 iter--;
111 } while (iter > 0);
112 up_write(&pblk_rb_lock);
113
114#ifdef CONFIG_NVM_DEBUG
8154d296 115 atomic_set(&rb->inflight_flush_point, 0);
a4bd217b
JG
116#endif
117
118 /*
119 * Initialize rate-limiter, which controls access to the write buffer
120 * but user and GC I/O
121 */
122 pblk_rl_init(&pblk->rl, rb->nr_entries);
123
124 return 0;
125}
126
127/*
128 * pblk_rb_calculate_size -- calculate the size of the write buffer
129 */
130unsigned int pblk_rb_calculate_size(unsigned int nr_entries)
131{
132 /* Alloc a write buffer that can at least fit 128 entries */
133 return (1 << max(get_count_order(nr_entries), 7));
134}
135
136void *pblk_rb_entries_ref(struct pblk_rb *rb)
137{
138 return rb->entries;
139}
140
141static void clean_wctx(struct pblk_w_ctx *w_ctx)
142{
143 int flags;
144
145try:
146 flags = READ_ONCE(w_ctx->flags);
147 if (!(flags & PBLK_SUBMITTED_ENTRY))
148 goto try;
149
150 /* Release flags on context. Protect from writes and reads */
151 smp_store_release(&w_ctx->flags, PBLK_WRITABLE_ENTRY);
152 pblk_ppa_set_empty(&w_ctx->ppa);
07698466 153 w_ctx->lba = ADDR_EMPTY;
a4bd217b
JG
154}
155
156#define pblk_rb_ring_count(head, tail, size) CIRC_CNT(head, tail, size)
157#define pblk_rb_ring_space(rb, head, tail, size) \
158 (CIRC_SPACE(head, tail, size))
159
160/*
161 * Buffer space is calculated with respect to the back pointer signaling
162 * synchronized entries to the media.
163 */
164static unsigned int pblk_rb_space(struct pblk_rb *rb)
165{
166 unsigned int mem = READ_ONCE(rb->mem);
167 unsigned int sync = READ_ONCE(rb->sync);
168
169 return pblk_rb_ring_space(rb, mem, sync, rb->nr_entries);
170}
171
172/*
173 * Buffer count is calculated with respect to the submission entry signaling the
174 * entries that are available to send to the media
175 */
176unsigned int pblk_rb_read_count(struct pblk_rb *rb)
177{
178 unsigned int mem = READ_ONCE(rb->mem);
179 unsigned int subm = READ_ONCE(rb->subm);
180
181 return pblk_rb_ring_count(mem, subm, rb->nr_entries);
182}
183
ee8d5c1a
JG
184unsigned int pblk_rb_sync_count(struct pblk_rb *rb)
185{
186 unsigned int mem = READ_ONCE(rb->mem);
187 unsigned int sync = READ_ONCE(rb->sync);
188
189 return pblk_rb_ring_count(mem, sync, rb->nr_entries);
190}
191
a4bd217b
JG
192unsigned int pblk_rb_read_commit(struct pblk_rb *rb, unsigned int nr_entries)
193{
194 unsigned int subm;
195
196 subm = READ_ONCE(rb->subm);
197 /* Commit read means updating submission pointer */
198 smp_store_release(&rb->subm,
199 (subm + nr_entries) & (rb->nr_entries - 1));
200
201 return subm;
202}
203
05ed3447 204static int __pblk_rb_update_l2p(struct pblk_rb *rb, unsigned int to_update)
a4bd217b
JG
205{
206 struct pblk *pblk = container_of(rb, struct pblk, rwb);
207 struct pblk_line *line;
208 struct pblk_rb_entry *entry;
209 struct pblk_w_ctx *w_ctx;
b20ba1bc 210 unsigned int user_io = 0, gc_io = 0;
a4bd217b 211 unsigned int i;
b20ba1bc 212 int flags;
a4bd217b
JG
213
214 for (i = 0; i < to_update; i++) {
05ed3447 215 entry = &rb->entries[rb->l2p_update];
a4bd217b
JG
216 w_ctx = &entry->w_ctx;
217
b20ba1bc
JG
218 flags = READ_ONCE(entry->w_ctx.flags);
219 if (flags & PBLK_IOTYPE_USER)
220 user_io++;
221 else if (flags & PBLK_IOTYPE_GC)
222 gc_io++;
223 else
224 WARN(1, "pblk: unknown IO type\n");
225
a4bd217b
JG
226 pblk_update_map_dev(pblk, w_ctx->lba, w_ctx->ppa,
227 entry->cacheline);
228
b1bcfda1 229 line = &pblk->lines[pblk_ppa_to_line(w_ctx->ppa)];
a4bd217b
JG
230 kref_put(&line->ref, pblk_line_put);
231 clean_wctx(w_ctx);
05ed3447 232 rb->l2p_update = (rb->l2p_update + 1) & (rb->nr_entries - 1);
a4bd217b
JG
233 }
234
b20ba1bc
JG
235 pblk_rl_out(&pblk->rl, user_io, gc_io);
236
a4bd217b
JG
237 return 0;
238}
239
240/*
241 * When we move the l2p_update pointer, we update the l2p table - lookups will
242 * point to the physical address instead of to the cacheline in the write buffer
243 * from this moment on.
244 */
245static int pblk_rb_update_l2p(struct pblk_rb *rb, unsigned int nr_entries,
246 unsigned int mem, unsigned int sync)
247{
248 unsigned int space, count;
249 int ret = 0;
250
251 lockdep_assert_held(&rb->w_lock);
252
253 /* Update l2p only as buffer entries are being overwritten */
254 space = pblk_rb_ring_space(rb, mem, rb->l2p_update, rb->nr_entries);
255 if (space > nr_entries)
256 goto out;
257
258 count = nr_entries - space;
259 /* l2p_update used exclusively under rb->w_lock */
05ed3447 260 ret = __pblk_rb_update_l2p(rb, count);
a4bd217b
JG
261
262out:
263 return ret;
264}
265
266/*
267 * Update the l2p entry for all sectors stored on the write buffer. This means
268 * that all future lookups to the l2p table will point to a device address, not
269 * to the cacheline in the write buffer.
270 */
271void pblk_rb_sync_l2p(struct pblk_rb *rb)
272{
273 unsigned int sync;
274 unsigned int to_update;
275
276 spin_lock(&rb->w_lock);
277
278 /* Protect from reads and writes */
279 sync = smp_load_acquire(&rb->sync);
280
281 to_update = pblk_rb_ring_count(sync, rb->l2p_update, rb->nr_entries);
05ed3447 282 __pblk_rb_update_l2p(rb, to_update);
a4bd217b
JG
283
284 spin_unlock(&rb->w_lock);
285}
286
287/*
288 * Write @nr_entries to ring buffer from @data buffer if there is enough space.
289 * Typically, 4KB data chunks coming from a bio will be copied to the ring
290 * buffer, thus the write will fail if not all incoming data can be copied.
291 *
292 */
293static void __pblk_rb_write_entry(struct pblk_rb *rb, void *data,
294 struct pblk_w_ctx w_ctx,
295 struct pblk_rb_entry *entry)
296{
297 memcpy(entry->data, data, rb->seg_size);
298
299 entry->w_ctx.lba = w_ctx.lba;
300 entry->w_ctx.ppa = w_ctx.ppa;
301}
302
303void pblk_rb_write_entry_user(struct pblk_rb *rb, void *data,
304 struct pblk_w_ctx w_ctx, unsigned int ring_pos)
305{
306 struct pblk *pblk = container_of(rb, struct pblk, rwb);
307 struct pblk_rb_entry *entry;
308 int flags;
309
310 entry = &rb->entries[ring_pos];
311 flags = READ_ONCE(entry->w_ctx.flags);
312#ifdef CONFIG_NVM_DEBUG
313 /* Caller must guarantee that the entry is free */
314 BUG_ON(!(flags & PBLK_WRITABLE_ENTRY));
315#endif
316
317 __pblk_rb_write_entry(rb, data, w_ctx, entry);
318
319 pblk_update_map_cache(pblk, w_ctx.lba, entry->cacheline);
320 flags = w_ctx.flags | PBLK_WRITTEN_DATA;
321
322 /* Release flags on write context. Protect from writes */
323 smp_store_release(&entry->w_ctx.flags, flags);
324}
325
326void pblk_rb_write_entry_gc(struct pblk_rb *rb, void *data,
d340121e
JG
327 struct pblk_w_ctx w_ctx, struct pblk_line *line,
328 u64 paddr, unsigned int ring_pos)
a4bd217b
JG
329{
330 struct pblk *pblk = container_of(rb, struct pblk, rwb);
331 struct pblk_rb_entry *entry;
332 int flags;
333
334 entry = &rb->entries[ring_pos];
335 flags = READ_ONCE(entry->w_ctx.flags);
336#ifdef CONFIG_NVM_DEBUG
337 /* Caller must guarantee that the entry is free */
338 BUG_ON(!(flags & PBLK_WRITABLE_ENTRY));
339#endif
340
341 __pblk_rb_write_entry(rb, data, w_ctx, entry);
342
d340121e 343 if (!pblk_update_map_gc(pblk, w_ctx.lba, entry->cacheline, line, paddr))
a4bd217b
JG
344 entry->w_ctx.lba = ADDR_EMPTY;
345
346 flags = w_ctx.flags | PBLK_WRITTEN_DATA;
347
348 /* Release flags on write context. Protect from writes */
349 smp_store_release(&entry->w_ctx.flags, flags);
350}
351
8154d296 352static int pblk_rb_flush_point_set(struct pblk_rb *rb, struct bio *bio,
a4bd217b
JG
353 unsigned int pos)
354{
355 struct pblk_rb_entry *entry;
533657c1 356 unsigned int sync, flush_point;
a4bd217b 357
533657c1
HH
358 sync = READ_ONCE(rb->sync);
359
360 if (pos == sync)
361 return 0;
a4bd217b
JG
362
363#ifdef CONFIG_NVM_DEBUG
8154d296 364 atomic_inc(&rb->inflight_flush_point);
a4bd217b
JG
365#endif
366
8154d296
HH
367 flush_point = (pos == 0) ? (rb->nr_entries - 1) : (pos - 1);
368 entry = &rb->entries[flush_point];
a4bd217b 369
b36bbf9d
HH
370 pblk_rb_sync_init(rb, NULL);
371
8154d296
HH
372 /* Protect flush points */
373 smp_store_release(&rb->flush_point, flush_point);
a4bd217b 374
b36bbf9d
HH
375 if (bio)
376 bio_list_add(&entry->w_ctx.bios, bio);
588726d3 377
b36bbf9d 378 pblk_rb_sync_end(rb, NULL);
a4bd217b 379
b36bbf9d 380 return bio ? 1 : 0;
a4bd217b
JG
381}
382
383static int __pblk_rb_may_write(struct pblk_rb *rb, unsigned int nr_entries,
384 unsigned int *pos)
385{
386 unsigned int mem;
387 unsigned int sync;
388
389 sync = READ_ONCE(rb->sync);
390 mem = READ_ONCE(rb->mem);
391
392 if (pblk_rb_ring_space(rb, mem, sync, rb->nr_entries) < nr_entries)
393 return 0;
394
395 if (pblk_rb_update_l2p(rb, nr_entries, mem, sync))
396 return 0;
397
398 *pos = mem;
399
400 return 1;
401}
402
403static int pblk_rb_may_write(struct pblk_rb *rb, unsigned int nr_entries,
404 unsigned int *pos)
405{
406 if (!__pblk_rb_may_write(rb, nr_entries, pos))
407 return 0;
408
409 /* Protect from read count */
410 smp_store_release(&rb->mem, (*pos + nr_entries) & (rb->nr_entries - 1));
411 return 1;
412}
413
588726d3
JG
414void pblk_rb_flush(struct pblk_rb *rb)
415{
416 struct pblk *pblk = container_of(rb, struct pblk, rwb);
417 unsigned int mem = READ_ONCE(rb->mem);
418
8154d296 419 if (pblk_rb_flush_point_set(rb, NULL, mem))
588726d3
JG
420 return;
421
422 pblk_write_should_kick(pblk);
423}
424
a4bd217b
JG
425static int pblk_rb_may_write_flush(struct pblk_rb *rb, unsigned int nr_entries,
426 unsigned int *pos, struct bio *bio,
427 int *io_ret)
428{
429 unsigned int mem;
430
431 if (!__pblk_rb_may_write(rb, nr_entries, pos))
432 return 0;
433
434 mem = (*pos + nr_entries) & (rb->nr_entries - 1);
435 *io_ret = NVM_IO_DONE;
436
437 if (bio->bi_opf & REQ_PREFLUSH) {
438 struct pblk *pblk = container_of(rb, struct pblk, rwb);
439
5d149bfa 440 atomic64_inc(&pblk->nr_flush);
8154d296 441 if (pblk_rb_flush_point_set(&pblk->rwb, bio, mem))
a4bd217b
JG
442 *io_ret = NVM_IO_OK;
443 }
444
445 /* Protect from read count */
446 smp_store_release(&rb->mem, mem);
6ca2f71f 447
a4bd217b
JG
448 return 1;
449}
450
451/*
452 * Atomically check that (i) there is space on the write buffer for the
453 * incoming I/O, and (ii) the current I/O type has enough budget in the write
454 * buffer (rate-limiter).
455 */
456int pblk_rb_may_write_user(struct pblk_rb *rb, struct bio *bio,
457 unsigned int nr_entries, unsigned int *pos)
458{
459 struct pblk *pblk = container_of(rb, struct pblk, rwb);
588726d3 460 int io_ret;
a4bd217b
JG
461
462 spin_lock(&rb->w_lock);
588726d3
JG
463 io_ret = pblk_rl_user_may_insert(&pblk->rl, nr_entries);
464 if (io_ret) {
a4bd217b 465 spin_unlock(&rb->w_lock);
588726d3 466 return io_ret;
a4bd217b
JG
467 }
468
588726d3 469 if (!pblk_rb_may_write_flush(rb, nr_entries, pos, bio, &io_ret)) {
a4bd217b
JG
470 spin_unlock(&rb->w_lock);
471 return NVM_IO_REQUEUE;
472 }
473
474 pblk_rl_user_in(&pblk->rl, nr_entries);
475 spin_unlock(&rb->w_lock);
476
588726d3 477 return io_ret;
a4bd217b
JG
478}
479
480/*
481 * Look at pblk_rb_may_write_user comment
482 */
483int pblk_rb_may_write_gc(struct pblk_rb *rb, unsigned int nr_entries,
484 unsigned int *pos)
485{
486 struct pblk *pblk = container_of(rb, struct pblk, rwb);
487
488 spin_lock(&rb->w_lock);
489 if (!pblk_rl_gc_may_insert(&pblk->rl, nr_entries)) {
490 spin_unlock(&rb->w_lock);
491 return 0;
492 }
493
494 if (!pblk_rb_may_write(rb, nr_entries, pos)) {
495 spin_unlock(&rb->w_lock);
496 return 0;
497 }
498
499 pblk_rl_gc_in(&pblk->rl, nr_entries);
500 spin_unlock(&rb->w_lock);
501
502 return 1;
503}
504
505/*
506 * The caller of this function must ensure that the backpointer will not
507 * overwrite the entries passed on the list.
508 */
509unsigned int pblk_rb_read_to_bio_list(struct pblk_rb *rb, struct bio *bio,
510 struct list_head *list,
511 unsigned int max)
512{
513 struct pblk_rb_entry *entry, *tentry;
514 struct page *page;
515 unsigned int read = 0;
516 int ret;
517
518 list_for_each_entry_safe(entry, tentry, list, index) {
519 if (read > max) {
520 pr_err("pblk: too many entries on list\n");
521 goto out;
522 }
523
524 page = virt_to_page(entry->data);
525 if (!page) {
526 pr_err("pblk: could not allocate write bio page\n");
527 goto out;
528 }
529
530 ret = bio_add_page(bio, page, rb->seg_size, 0);
531 if (ret != rb->seg_size) {
532 pr_err("pblk: could not add page to write bio\n");
533 goto out;
534 }
535
536 list_del(&entry->index);
537 read++;
538 }
539
540out:
541 return read;
542}
543
544/*
545 * Read available entries on rb and add them to the given bio. To avoid a memory
546 * copy, a page reference to the write buffer is used to be added to the bio.
547 *
548 * This function is used by the write thread to form the write bio that will
549 * persist data on the write buffer to the media.
550 */
d624f371 551unsigned int pblk_rb_read_to_bio(struct pblk_rb *rb, struct nvm_rq *rqd,
875d94f3
JG
552 unsigned int pos, unsigned int nr_entries,
553 unsigned int count)
a4bd217b
JG
554{
555 struct pblk *pblk = container_of(rb, struct pblk, rwb);
d624f371
JG
556 struct request_queue *q = pblk->dev->q;
557 struct pblk_c_ctx *c_ctx = nvm_rq_to_pdu(rqd);
875d94f3 558 struct bio *bio = rqd->bio;
a4bd217b
JG
559 struct pblk_rb_entry *entry;
560 struct page *page;
d624f371 561 unsigned int pad = 0, to_read = nr_entries;
a4bd217b
JG
562 unsigned int i;
563 int flags;
a4bd217b
JG
564
565 if (count < nr_entries) {
566 pad = nr_entries - count;
567 to_read = count;
568 }
569
570 c_ctx->sentry = pos;
571 c_ctx->nr_valid = to_read;
572 c_ctx->nr_padded = pad;
573
574 for (i = 0; i < to_read; i++) {
575 entry = &rb->entries[pos];
576
577 /* A write has been allowed into the buffer, but data is still
578 * being copied to it. It is ok to busy wait.
579 */
580try:
581 flags = READ_ONCE(entry->w_ctx.flags);
10888129
JG
582 if (!(flags & PBLK_WRITTEN_DATA)) {
583 io_schedule();
a4bd217b 584 goto try;
10888129 585 }
a4bd217b 586
a4bd217b
JG
587 page = virt_to_page(entry->data);
588 if (!page) {
589 pr_err("pblk: could not allocate write bio page\n");
590 flags &= ~PBLK_WRITTEN_DATA;
591 flags |= PBLK_SUBMITTED_ENTRY;
592 /* Release flags on context. Protect from writes */
593 smp_store_release(&entry->w_ctx.flags, flags);
d624f371 594 return NVM_IO_ERR;
a4bd217b
JG
595 }
596
d624f371
JG
597 if (bio_add_pc_page(q, bio, page, rb->seg_size, 0) !=
598 rb->seg_size) {
a4bd217b
JG
599 pr_err("pblk: could not add page to write bio\n");
600 flags &= ~PBLK_WRITTEN_DATA;
601 flags |= PBLK_SUBMITTED_ENTRY;
602 /* Release flags on context. Protect from writes */
603 smp_store_release(&entry->w_ctx.flags, flags);
d624f371 604 return NVM_IO_ERR;
a4bd217b
JG
605 }
606
a4bd217b
JG
607 flags &= ~PBLK_WRITTEN_DATA;
608 flags |= PBLK_SUBMITTED_ENTRY;
609
610 /* Release flags on context. Protect from writes */
611 smp_store_release(&entry->w_ctx.flags, flags);
612
613 pos = (pos + 1) & (rb->nr_entries - 1);
614 }
615
d624f371
JG
616 if (pad) {
617 if (pblk_bio_add_pages(pblk, bio, GFP_KERNEL, pad)) {
618 pr_err("pblk: could not pad page in write bio\n");
619 return NVM_IO_ERR;
620 }
d624f371 621
5d149bfa
HH
622 if (pad < pblk->min_write_pgs)
623 atomic64_inc(&pblk->pad_dist[pad - 1]);
624 else
625 pr_warn("pblk: padding more than min. sectors\n");
626
627 atomic64_add(pad, &pblk->pad_wa);
628 }
76758390 629
a4bd217b 630#ifdef CONFIG_NVM_DEBUG
5d149bfa 631 atomic_long_add(pad, &pblk->padded_writes);
a4bd217b 632#endif
d624f371
JG
633
634 return NVM_IO_OK;
a4bd217b
JG
635}
636
637/*
638 * Copy to bio only if the lba matches the one on the given cache entry.
639 * Otherwise, it means that the entry has been overwritten, and the bio should
640 * be directed to disk.
641 */
642int pblk_rb_copy_to_bio(struct pblk_rb *rb, struct bio *bio, sector_t lba,
75cb8e93 643 struct ppa_addr ppa, int bio_iter, bool advanced_bio)
a4bd217b 644{
07698466 645 struct pblk *pblk = container_of(rb, struct pblk, rwb);
a4bd217b
JG
646 struct pblk_rb_entry *entry;
647 struct pblk_w_ctx *w_ctx;
07698466
JG
648 struct ppa_addr l2p_ppa;
649 u64 pos = pblk_addr_to_cacheline(ppa);
a4bd217b
JG
650 void *data;
651 int flags;
652 int ret = 1;
653
a4bd217b
JG
654
655#ifdef CONFIG_NVM_DEBUG
656 /* Caller must ensure that the access will not cause an overflow */
657 BUG_ON(pos >= rb->nr_entries);
658#endif
659 entry = &rb->entries[pos];
660 w_ctx = &entry->w_ctx;
661 flags = READ_ONCE(w_ctx->flags);
662
07698466
JG
663 spin_lock(&rb->w_lock);
664 spin_lock(&pblk->trans_lock);
665 l2p_ppa = pblk_trans_map_get(pblk, lba);
666 spin_unlock(&pblk->trans_lock);
667
a4bd217b 668 /* Check if the entry has been overwritten or is scheduled to be */
07698466
JG
669 if (!pblk_ppa_comp(l2p_ppa, ppa) || w_ctx->lba != lba ||
670 flags & PBLK_WRITABLE_ENTRY) {
a4bd217b
JG
671 ret = 0;
672 goto out;
673 }
674
675 /* Only advance the bio if it hasn't been advanced already. If advanced,
676 * this bio is at least a partial bio (i.e., it has partially been
677 * filled with data from the cache). If part of the data resides on the
678 * media, we will read later on
679 */
75cb8e93 680 if (unlikely(!advanced_bio))
a4bd217b
JG
681 bio_advance(bio, bio_iter * PBLK_EXPOSED_PAGE_SIZE);
682
683 data = bio_data(bio);
684 memcpy(data, entry->data, rb->seg_size);
685
686out:
687 spin_unlock(&rb->w_lock);
688 return ret;
689}
690
691struct pblk_w_ctx *pblk_rb_w_ctx(struct pblk_rb *rb, unsigned int pos)
692{
693 unsigned int entry = pos & (rb->nr_entries - 1);
694
695 return &rb->entries[entry].w_ctx;
696}
697
698unsigned int pblk_rb_sync_init(struct pblk_rb *rb, unsigned long *flags)
699 __acquires(&rb->s_lock)
700{
701 if (flags)
702 spin_lock_irqsave(&rb->s_lock, *flags);
703 else
704 spin_lock_irq(&rb->s_lock);
705
706 return rb->sync;
707}
708
709void pblk_rb_sync_end(struct pblk_rb *rb, unsigned long *flags)
710 __releases(&rb->s_lock)
711{
712 lockdep_assert_held(&rb->s_lock);
713
714 if (flags)
715 spin_unlock_irqrestore(&rb->s_lock, *flags);
716 else
717 spin_unlock_irq(&rb->s_lock);
718}
719
720unsigned int pblk_rb_sync_advance(struct pblk_rb *rb, unsigned int nr_entries)
721{
533657c1 722 unsigned int sync, flush_point;
a4bd217b
JG
723 lockdep_assert_held(&rb->s_lock);
724
725 sync = READ_ONCE(rb->sync);
533657c1
HH
726 flush_point = READ_ONCE(rb->flush_point);
727
728 if (flush_point != EMPTY_ENTRY) {
729 unsigned int secs_to_flush;
a4bd217b 730
533657c1
HH
731 secs_to_flush = pblk_rb_ring_count(flush_point, sync,
732 rb->nr_entries);
733 if (secs_to_flush < nr_entries) {
734 /* Protect flush points */
735 smp_store_release(&rb->flush_point, EMPTY_ENTRY);
736 }
737 }
738
739 sync = (sync + nr_entries) & (rb->nr_entries - 1);
a4bd217b
JG
740
741 /* Protect from counts */
742 smp_store_release(&rb->sync, sync);
743
744 return sync;
745}
746
533657c1 747/* Calculate how many sectors to submit up to the current flush point. */
8154d296 748unsigned int pblk_rb_flush_point_count(struct pblk_rb *rb)
a4bd217b 749{
533657c1
HH
750 unsigned int subm, sync, flush_point;
751 unsigned int submitted, to_flush;
a4bd217b 752
8154d296
HH
753 /* Protect flush points */
754 flush_point = smp_load_acquire(&rb->flush_point);
755 if (flush_point == EMPTY_ENTRY)
a4bd217b
JG
756 return 0;
757
533657c1
HH
758 /* Protect syncs */
759 sync = smp_load_acquire(&rb->sync);
760
a4bd217b 761 subm = READ_ONCE(rb->subm);
533657c1 762 submitted = pblk_rb_ring_count(subm, sync, rb->nr_entries);
a4bd217b
JG
763
764 /* The sync point itself counts as a sector to sync */
533657c1 765 to_flush = pblk_rb_ring_count(flush_point, sync, rb->nr_entries) + 1;
a4bd217b 766
533657c1 767 return (submitted < to_flush) ? (to_flush - submitted) : 0;
a4bd217b
JG
768}
769
770/*
771 * Scan from the current position of the sync pointer to find the entry that
772 * corresponds to the given ppa. This is necessary since write requests can be
773 * completed out of order. The assumption is that the ppa is close to the sync
774 * pointer thus the search will not take long.
775 *
776 * The caller of this function must guarantee that the sync pointer will no
777 * reach the entry while it is using the metadata associated with it. With this
778 * assumption in mind, there is no need to take the sync lock.
779 */
780struct pblk_rb_entry *pblk_rb_sync_scan_entry(struct pblk_rb *rb,
781 struct ppa_addr *ppa)
782{
783 unsigned int sync, subm, count;
784 unsigned int i;
785
786 sync = READ_ONCE(rb->sync);
787 subm = READ_ONCE(rb->subm);
788 count = pblk_rb_ring_count(subm, sync, rb->nr_entries);
789
790 for (i = 0; i < count; i++)
791 sync = (sync + 1) & (rb->nr_entries - 1);
792
793 return NULL;
794}
795
796int pblk_rb_tear_down_check(struct pblk_rb *rb)
797{
798 struct pblk_rb_entry *entry;
799 int i;
800 int ret = 0;
801
802 spin_lock(&rb->w_lock);
803 spin_lock_irq(&rb->s_lock);
804
805 if ((rb->mem == rb->subm) && (rb->subm == rb->sync) &&
806 (rb->sync == rb->l2p_update) &&
8154d296 807 (rb->flush_point == EMPTY_ENTRY)) {
a4bd217b
JG
808 goto out;
809 }
810
811 if (!rb->entries) {
812 ret = 1;
813 goto out;
814 }
815
816 for (i = 0; i < rb->nr_entries; i++) {
817 entry = &rb->entries[i];
818
819 if (!entry->data) {
820 ret = 1;
821 goto out;
822 }
823 }
824
825out:
826 spin_unlock(&rb->w_lock);
827 spin_unlock_irq(&rb->s_lock);
828
829 return ret;
830}
831
832unsigned int pblk_rb_wrap_pos(struct pblk_rb *rb, unsigned int pos)
833{
834 return (pos & (rb->nr_entries - 1));
835}
836
837int pblk_rb_pos_oob(struct pblk_rb *rb, u64 pos)
838{
839 return (pos >= rb->nr_entries);
840}
841
842ssize_t pblk_rb_sysfs(struct pblk_rb *rb, char *buf)
843{
844 struct pblk *pblk = container_of(rb, struct pblk, rwb);
845 struct pblk_c_ctx *c;
846 ssize_t offset;
847 int queued_entries = 0;
848
849 spin_lock_irq(&rb->s_lock);
850 list_for_each_entry(c, &pblk->compl_list, list)
851 queued_entries++;
852 spin_unlock_irq(&rb->s_lock);
853
8154d296 854 if (rb->flush_point != EMPTY_ENTRY)
a4bd217b
JG
855 offset = scnprintf(buf, PAGE_SIZE,
856 "%u\t%u\t%u\t%u\t%u\t%u\t%u - %u/%u/%u - %d\n",
857 rb->nr_entries,
858 rb->mem,
859 rb->subm,
860 rb->sync,
861 rb->l2p_update,
862#ifdef CONFIG_NVM_DEBUG
8154d296 863 atomic_read(&rb->inflight_flush_point),
a4bd217b
JG
864#else
865 0,
866#endif
8154d296 867 rb->flush_point,
a4bd217b
JG
868 pblk_rb_read_count(rb),
869 pblk_rb_space(rb),
8154d296 870 pblk_rb_flush_point_count(rb),
a4bd217b
JG
871 queued_entries);
872 else
873 offset = scnprintf(buf, PAGE_SIZE,
874 "%u\t%u\t%u\t%u\t%u\t%u\tNULL - %u/%u/%u - %d\n",
875 rb->nr_entries,
876 rb->mem,
877 rb->subm,
878 rb->sync,
879 rb->l2p_update,
880#ifdef CONFIG_NVM_DEBUG
8154d296 881 atomic_read(&rb->inflight_flush_point),
a4bd217b
JG
882#else
883 0,
884#endif
885 pblk_rb_read_count(rb),
886 pblk_rb_space(rb),
8154d296 887 pblk_rb_flush_point_count(rb),
a4bd217b
JG
888 queued_entries);
889
890 return offset;
891}