blob: 045384ddc1f9038a4b5c1de83146c03e89674a85 [file] [log] [blame]
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001/*
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;
57 rb->sync_point = EMPTY_ENTRY;
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
115 atomic_set(&rb->inflight_sync_point, 0);
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);
153}
154
155#define pblk_rb_ring_count(head, tail, size) CIRC_CNT(head, tail, size)
156#define pblk_rb_ring_space(rb, head, tail, size) \
157 (CIRC_SPACE(head, tail, size))
158
159/*
160 * Buffer space is calculated with respect to the back pointer signaling
161 * synchronized entries to the media.
162 */
163static unsigned int pblk_rb_space(struct pblk_rb *rb)
164{
165 unsigned int mem = READ_ONCE(rb->mem);
166 unsigned int sync = READ_ONCE(rb->sync);
167
168 return pblk_rb_ring_space(rb, mem, sync, rb->nr_entries);
169}
170
171/*
172 * Buffer count is calculated with respect to the submission entry signaling the
173 * entries that are available to send to the media
174 */
175unsigned int pblk_rb_read_count(struct pblk_rb *rb)
176{
177 unsigned int mem = READ_ONCE(rb->mem);
178 unsigned int subm = READ_ONCE(rb->subm);
179
180 return pblk_rb_ring_count(mem, subm, rb->nr_entries);
181}
182
183unsigned int pblk_rb_read_commit(struct pblk_rb *rb, unsigned int nr_entries)
184{
185 unsigned int subm;
186
187 subm = READ_ONCE(rb->subm);
188 /* Commit read means updating submission pointer */
189 smp_store_release(&rb->subm,
190 (subm + nr_entries) & (rb->nr_entries - 1));
191
192 return subm;
193}
194
195static int __pblk_rb_update_l2p(struct pblk_rb *rb, unsigned int *l2p_upd,
196 unsigned int to_update)
197{
198 struct pblk *pblk = container_of(rb, struct pblk, rwb);
199 struct pblk_line *line;
200 struct pblk_rb_entry *entry;
201 struct pblk_w_ctx *w_ctx;
202 unsigned int i;
203
204 for (i = 0; i < to_update; i++) {
205 entry = &rb->entries[*l2p_upd];
206 w_ctx = &entry->w_ctx;
207
208 pblk_update_map_dev(pblk, w_ctx->lba, w_ctx->ppa,
209 entry->cacheline);
210
211 line = &pblk->lines[pblk_tgt_ppa_to_line(w_ctx->ppa)];
212 kref_put(&line->ref, pblk_line_put);
213 clean_wctx(w_ctx);
214 *l2p_upd = (*l2p_upd + 1) & (rb->nr_entries - 1);
215 }
216
217 return 0;
218}
219
220/*
221 * When we move the l2p_update pointer, we update the l2p table - lookups will
222 * point to the physical address instead of to the cacheline in the write buffer
223 * from this moment on.
224 */
225static int pblk_rb_update_l2p(struct pblk_rb *rb, unsigned int nr_entries,
226 unsigned int mem, unsigned int sync)
227{
228 unsigned int space, count;
229 int ret = 0;
230
231 lockdep_assert_held(&rb->w_lock);
232
233 /* Update l2p only as buffer entries are being overwritten */
234 space = pblk_rb_ring_space(rb, mem, rb->l2p_update, rb->nr_entries);
235 if (space > nr_entries)
236 goto out;
237
238 count = nr_entries - space;
239 /* l2p_update used exclusively under rb->w_lock */
240 ret = __pblk_rb_update_l2p(rb, &rb->l2p_update, count);
241
242out:
243 return ret;
244}
245
246/*
247 * Update the l2p entry for all sectors stored on the write buffer. This means
248 * that all future lookups to the l2p table will point to a device address, not
249 * to the cacheline in the write buffer.
250 */
251void pblk_rb_sync_l2p(struct pblk_rb *rb)
252{
253 unsigned int sync;
254 unsigned int to_update;
255
256 spin_lock(&rb->w_lock);
257
258 /* Protect from reads and writes */
259 sync = smp_load_acquire(&rb->sync);
260
261 to_update = pblk_rb_ring_count(sync, rb->l2p_update, rb->nr_entries);
262 __pblk_rb_update_l2p(rb, &rb->l2p_update, to_update);
263
264 spin_unlock(&rb->w_lock);
265}
266
267/*
268 * Write @nr_entries to ring buffer from @data buffer if there is enough space.
269 * Typically, 4KB data chunks coming from a bio will be copied to the ring
270 * buffer, thus the write will fail if not all incoming data can be copied.
271 *
272 */
273static void __pblk_rb_write_entry(struct pblk_rb *rb, void *data,
274 struct pblk_w_ctx w_ctx,
275 struct pblk_rb_entry *entry)
276{
277 memcpy(entry->data, data, rb->seg_size);
278
279 entry->w_ctx.lba = w_ctx.lba;
280 entry->w_ctx.ppa = w_ctx.ppa;
281}
282
283void pblk_rb_write_entry_user(struct pblk_rb *rb, void *data,
284 struct pblk_w_ctx w_ctx, unsigned int ring_pos)
285{
286 struct pblk *pblk = container_of(rb, struct pblk, rwb);
287 struct pblk_rb_entry *entry;
288 int flags;
289
290 entry = &rb->entries[ring_pos];
291 flags = READ_ONCE(entry->w_ctx.flags);
292#ifdef CONFIG_NVM_DEBUG
293 /* Caller must guarantee that the entry is free */
294 BUG_ON(!(flags & PBLK_WRITABLE_ENTRY));
295#endif
296
297 __pblk_rb_write_entry(rb, data, w_ctx, entry);
298
299 pblk_update_map_cache(pblk, w_ctx.lba, entry->cacheline);
300 flags = w_ctx.flags | PBLK_WRITTEN_DATA;
301
302 /* Release flags on write context. Protect from writes */
303 smp_store_release(&entry->w_ctx.flags, flags);
304}
305
306void pblk_rb_write_entry_gc(struct pblk_rb *rb, void *data,
307 struct pblk_w_ctx w_ctx, struct pblk_line *gc_line,
308 unsigned int ring_pos)
309{
310 struct pblk *pblk = container_of(rb, struct pblk, rwb);
311 struct pblk_rb_entry *entry;
312 int flags;
313
314 entry = &rb->entries[ring_pos];
315 flags = READ_ONCE(entry->w_ctx.flags);
316#ifdef CONFIG_NVM_DEBUG
317 /* Caller must guarantee that the entry is free */
318 BUG_ON(!(flags & PBLK_WRITABLE_ENTRY));
319#endif
320
321 __pblk_rb_write_entry(rb, data, w_ctx, entry);
322
323 if (!pblk_update_map_gc(pblk, w_ctx.lba, entry->cacheline, gc_line))
324 entry->w_ctx.lba = ADDR_EMPTY;
325
326 flags = w_ctx.flags | PBLK_WRITTEN_DATA;
327
328 /* Release flags on write context. Protect from writes */
329 smp_store_release(&entry->w_ctx.flags, flags);
330}
331
332static int pblk_rb_sync_point_set(struct pblk_rb *rb, struct bio *bio,
333 unsigned int pos)
334{
335 struct pblk_rb_entry *entry;
336 unsigned int subm, sync_point;
337 int flags;
338
339 subm = READ_ONCE(rb->subm);
340
341#ifdef CONFIG_NVM_DEBUG
342 atomic_inc(&rb->inflight_sync_point);
343#endif
344
345 if (pos == subm)
346 return 0;
347
348 sync_point = (pos == 0) ? (rb->nr_entries - 1) : (pos - 1);
349 entry = &rb->entries[sync_point];
350
351 flags = READ_ONCE(entry->w_ctx.flags);
352 flags |= PBLK_FLUSH_ENTRY;
353
354 /* Release flags on context. Protect from writes */
355 smp_store_release(&entry->w_ctx.flags, flags);
356
357 /* Protect syncs */
358 smp_store_release(&rb->sync_point, sync_point);
359
360 spin_lock_irq(&rb->s_lock);
361 bio_list_add(&entry->w_ctx.bios, bio);
362 spin_unlock_irq(&rb->s_lock);
363
364 return 1;
365}
366
367static int __pblk_rb_may_write(struct pblk_rb *rb, unsigned int nr_entries,
368 unsigned int *pos)
369{
370 unsigned int mem;
371 unsigned int sync;
372
373 sync = READ_ONCE(rb->sync);
374 mem = READ_ONCE(rb->mem);
375
376 if (pblk_rb_ring_space(rb, mem, sync, rb->nr_entries) < nr_entries)
377 return 0;
378
379 if (pblk_rb_update_l2p(rb, nr_entries, mem, sync))
380 return 0;
381
382 *pos = mem;
383
384 return 1;
385}
386
387static int pblk_rb_may_write(struct pblk_rb *rb, unsigned int nr_entries,
388 unsigned int *pos)
389{
390 if (!__pblk_rb_may_write(rb, nr_entries, pos))
391 return 0;
392
393 /* Protect from read count */
394 smp_store_release(&rb->mem, (*pos + nr_entries) & (rb->nr_entries - 1));
395 return 1;
396}
397
398static int pblk_rb_may_write_flush(struct pblk_rb *rb, unsigned int nr_entries,
399 unsigned int *pos, struct bio *bio,
400 int *io_ret)
401{
402 unsigned int mem;
403
404 if (!__pblk_rb_may_write(rb, nr_entries, pos))
405 return 0;
406
407 mem = (*pos + nr_entries) & (rb->nr_entries - 1);
408 *io_ret = NVM_IO_DONE;
409
410 if (bio->bi_opf & REQ_PREFLUSH) {
411 struct pblk *pblk = container_of(rb, struct pblk, rwb);
412
413#ifdef CONFIG_NVM_DEBUG
414 atomic_long_inc(&pblk->nr_flush);
415#endif
416 if (pblk_rb_sync_point_set(&pblk->rwb, bio, mem))
417 *io_ret = NVM_IO_OK;
418 }
419
420 /* Protect from read count */
421 smp_store_release(&rb->mem, mem);
422 return 1;
423}
424
425/*
426 * Atomically check that (i) there is space on the write buffer for the
427 * incoming I/O, and (ii) the current I/O type has enough budget in the write
428 * buffer (rate-limiter).
429 */
430int pblk_rb_may_write_user(struct pblk_rb *rb, struct bio *bio,
431 unsigned int nr_entries, unsigned int *pos)
432{
433 struct pblk *pblk = container_of(rb, struct pblk, rwb);
434 int flush_done;
435
436 spin_lock(&rb->w_lock);
437 if (!pblk_rl_user_may_insert(&pblk->rl, nr_entries)) {
438 spin_unlock(&rb->w_lock);
439 return NVM_IO_REQUEUE;
440 }
441
442 if (!pblk_rb_may_write_flush(rb, nr_entries, pos, bio, &flush_done)) {
443 spin_unlock(&rb->w_lock);
444 return NVM_IO_REQUEUE;
445 }
446
447 pblk_rl_user_in(&pblk->rl, nr_entries);
448 spin_unlock(&rb->w_lock);
449
450 return flush_done;
451}
452
453/*
454 * Look at pblk_rb_may_write_user comment
455 */
456int pblk_rb_may_write_gc(struct pblk_rb *rb, unsigned int nr_entries,
457 unsigned int *pos)
458{
459 struct pblk *pblk = container_of(rb, struct pblk, rwb);
460
461 spin_lock(&rb->w_lock);
462 if (!pblk_rl_gc_may_insert(&pblk->rl, nr_entries)) {
463 spin_unlock(&rb->w_lock);
464 return 0;
465 }
466
467 if (!pblk_rb_may_write(rb, nr_entries, pos)) {
468 spin_unlock(&rb->w_lock);
469 return 0;
470 }
471
472 pblk_rl_gc_in(&pblk->rl, nr_entries);
473 spin_unlock(&rb->w_lock);
474
475 return 1;
476}
477
478/*
479 * The caller of this function must ensure that the backpointer will not
480 * overwrite the entries passed on the list.
481 */
482unsigned int pblk_rb_read_to_bio_list(struct pblk_rb *rb, struct bio *bio,
483 struct list_head *list,
484 unsigned int max)
485{
486 struct pblk_rb_entry *entry, *tentry;
487 struct page *page;
488 unsigned int read = 0;
489 int ret;
490
491 list_for_each_entry_safe(entry, tentry, list, index) {
492 if (read > max) {
493 pr_err("pblk: too many entries on list\n");
494 goto out;
495 }
496
497 page = virt_to_page(entry->data);
498 if (!page) {
499 pr_err("pblk: could not allocate write bio page\n");
500 goto out;
501 }
502
503 ret = bio_add_page(bio, page, rb->seg_size, 0);
504 if (ret != rb->seg_size) {
505 pr_err("pblk: could not add page to write bio\n");
506 goto out;
507 }
508
509 list_del(&entry->index);
510 read++;
511 }
512
513out:
514 return read;
515}
516
517/*
518 * Read available entries on rb and add them to the given bio. To avoid a memory
519 * copy, a page reference to the write buffer is used to be added to the bio.
520 *
521 * This function is used by the write thread to form the write bio that will
522 * persist data on the write buffer to the media.
523 */
524unsigned int pblk_rb_read_to_bio(struct pblk_rb *rb, struct bio *bio,
525 struct pblk_c_ctx *c_ctx,
526 unsigned int pos,
527 unsigned int nr_entries,
528 unsigned int count)
529{
530 struct pblk *pblk = container_of(rb, struct pblk, rwb);
531 struct pblk_rb_entry *entry;
532 struct page *page;
533 unsigned int pad = 0, read = 0, to_read = nr_entries;
534 unsigned int user_io = 0, gc_io = 0;
535 unsigned int i;
536 int flags;
537 int ret;
538
539 if (count < nr_entries) {
540 pad = nr_entries - count;
541 to_read = count;
542 }
543
544 c_ctx->sentry = pos;
545 c_ctx->nr_valid = to_read;
546 c_ctx->nr_padded = pad;
547
548 for (i = 0; i < to_read; i++) {
549 entry = &rb->entries[pos];
550
551 /* A write has been allowed into the buffer, but data is still
552 * being copied to it. It is ok to busy wait.
553 */
554try:
555 flags = READ_ONCE(entry->w_ctx.flags);
556 if (!(flags & PBLK_WRITTEN_DATA))
557 goto try;
558
559 if (flags & PBLK_IOTYPE_USER)
560 user_io++;
561 else if (flags & PBLK_IOTYPE_GC)
562 gc_io++;
563 else
564 WARN(1, "pblk: unknown IO type\n");
565
566 page = virt_to_page(entry->data);
567 if (!page) {
568 pr_err("pblk: could not allocate write bio page\n");
569 flags &= ~PBLK_WRITTEN_DATA;
570 flags |= PBLK_SUBMITTED_ENTRY;
571 /* Release flags on context. Protect from writes */
572 smp_store_release(&entry->w_ctx.flags, flags);
573 goto out;
574 }
575
576 ret = bio_add_page(bio, page, rb->seg_size, 0);
577 if (ret != rb->seg_size) {
578 pr_err("pblk: could not add page to write bio\n");
579 flags &= ~PBLK_WRITTEN_DATA;
580 flags |= PBLK_SUBMITTED_ENTRY;
581 /* Release flags on context. Protect from writes */
582 smp_store_release(&entry->w_ctx.flags, flags);
583 goto out;
584 }
585
586 if (flags & PBLK_FLUSH_ENTRY) {
587 unsigned int sync_point;
588
589 sync_point = READ_ONCE(rb->sync_point);
590 if (sync_point == pos) {
591 /* Protect syncs */
592 smp_store_release(&rb->sync_point, EMPTY_ENTRY);
593 }
594
595 flags &= ~PBLK_FLUSH_ENTRY;
596#ifdef CONFIG_NVM_DEBUG
597 atomic_dec(&rb->inflight_sync_point);
598#endif
599 }
600
601 flags &= ~PBLK_WRITTEN_DATA;
602 flags |= PBLK_SUBMITTED_ENTRY;
603
604 /* Release flags on context. Protect from writes */
605 smp_store_release(&entry->w_ctx.flags, flags);
606
607 pos = (pos + 1) & (rb->nr_entries - 1);
608 }
609
610 read = to_read;
611 pblk_rl_out(&pblk->rl, user_io, gc_io);
612#ifdef CONFIG_NVM_DEBUG
613 atomic_long_add(pad, &((struct pblk *)
614 (container_of(rb, struct pblk, rwb)))->padded_writes);
615#endif
616out:
617 return read;
618}
619
620/*
621 * Copy to bio only if the lba matches the one on the given cache entry.
622 * Otherwise, it means that the entry has been overwritten, and the bio should
623 * be directed to disk.
624 */
625int pblk_rb_copy_to_bio(struct pblk_rb *rb, struct bio *bio, sector_t lba,
626 u64 pos, int bio_iter)
627{
628 struct pblk_rb_entry *entry;
629 struct pblk_w_ctx *w_ctx;
630 void *data;
631 int flags;
632 int ret = 1;
633
634 spin_lock(&rb->w_lock);
635
636#ifdef CONFIG_NVM_DEBUG
637 /* Caller must ensure that the access will not cause an overflow */
638 BUG_ON(pos >= rb->nr_entries);
639#endif
640 entry = &rb->entries[pos];
641 w_ctx = &entry->w_ctx;
642 flags = READ_ONCE(w_ctx->flags);
643
644 /* Check if the entry has been overwritten or is scheduled to be */
645 if (w_ctx->lba != lba || flags & PBLK_WRITABLE_ENTRY) {
646 ret = 0;
647 goto out;
648 }
649
650 /* Only advance the bio if it hasn't been advanced already. If advanced,
651 * this bio is at least a partial bio (i.e., it has partially been
652 * filled with data from the cache). If part of the data resides on the
653 * media, we will read later on
654 */
655 if (unlikely(!bio->bi_iter.bi_idx))
656 bio_advance(bio, bio_iter * PBLK_EXPOSED_PAGE_SIZE);
657
658 data = bio_data(bio);
659 memcpy(data, entry->data, rb->seg_size);
660
661out:
662 spin_unlock(&rb->w_lock);
663 return ret;
664}
665
666struct pblk_w_ctx *pblk_rb_w_ctx(struct pblk_rb *rb, unsigned int pos)
667{
668 unsigned int entry = pos & (rb->nr_entries - 1);
669
670 return &rb->entries[entry].w_ctx;
671}
672
673unsigned int pblk_rb_sync_init(struct pblk_rb *rb, unsigned long *flags)
674 __acquires(&rb->s_lock)
675{
676 if (flags)
677 spin_lock_irqsave(&rb->s_lock, *flags);
678 else
679 spin_lock_irq(&rb->s_lock);
680
681 return rb->sync;
682}
683
684void pblk_rb_sync_end(struct pblk_rb *rb, unsigned long *flags)
685 __releases(&rb->s_lock)
686{
687 lockdep_assert_held(&rb->s_lock);
688
689 if (flags)
690 spin_unlock_irqrestore(&rb->s_lock, *flags);
691 else
692 spin_unlock_irq(&rb->s_lock);
693}
694
695unsigned int pblk_rb_sync_advance(struct pblk_rb *rb, unsigned int nr_entries)
696{
697 unsigned int sync;
698 unsigned int i;
699
700 lockdep_assert_held(&rb->s_lock);
701
702 sync = READ_ONCE(rb->sync);
703
704 for (i = 0; i < nr_entries; i++)
705 sync = (sync + 1) & (rb->nr_entries - 1);
706
707 /* Protect from counts */
708 smp_store_release(&rb->sync, sync);
709
710 return sync;
711}
712
713unsigned int pblk_rb_sync_point_count(struct pblk_rb *rb)
714{
715 unsigned int subm, sync_point;
716 unsigned int count;
717
718 /* Protect syncs */
719 sync_point = smp_load_acquire(&rb->sync_point);
720 if (sync_point == EMPTY_ENTRY)
721 return 0;
722
723 subm = READ_ONCE(rb->subm);
724
725 /* The sync point itself counts as a sector to sync */
726 count = pblk_rb_ring_count(sync_point, subm, rb->nr_entries) + 1;
727
728 return count;
729}
730
731/*
732 * Scan from the current position of the sync pointer to find the entry that
733 * corresponds to the given ppa. This is necessary since write requests can be
734 * completed out of order. The assumption is that the ppa is close to the sync
735 * pointer thus the search will not take long.
736 *
737 * The caller of this function must guarantee that the sync pointer will no
738 * reach the entry while it is using the metadata associated with it. With this
739 * assumption in mind, there is no need to take the sync lock.
740 */
741struct pblk_rb_entry *pblk_rb_sync_scan_entry(struct pblk_rb *rb,
742 struct ppa_addr *ppa)
743{
744 unsigned int sync, subm, count;
745 unsigned int i;
746
747 sync = READ_ONCE(rb->sync);
748 subm = READ_ONCE(rb->subm);
749 count = pblk_rb_ring_count(subm, sync, rb->nr_entries);
750
751 for (i = 0; i < count; i++)
752 sync = (sync + 1) & (rb->nr_entries - 1);
753
754 return NULL;
755}
756
757int pblk_rb_tear_down_check(struct pblk_rb *rb)
758{
759 struct pblk_rb_entry *entry;
760 int i;
761 int ret = 0;
762
763 spin_lock(&rb->w_lock);
764 spin_lock_irq(&rb->s_lock);
765
766 if ((rb->mem == rb->subm) && (rb->subm == rb->sync) &&
767 (rb->sync == rb->l2p_update) &&
768 (rb->sync_point == EMPTY_ENTRY)) {
769 goto out;
770 }
771
772 if (!rb->entries) {
773 ret = 1;
774 goto out;
775 }
776
777 for (i = 0; i < rb->nr_entries; i++) {
778 entry = &rb->entries[i];
779
780 if (!entry->data) {
781 ret = 1;
782 goto out;
783 }
784 }
785
786out:
787 spin_unlock(&rb->w_lock);
788 spin_unlock_irq(&rb->s_lock);
789
790 return ret;
791}
792
793unsigned int pblk_rb_wrap_pos(struct pblk_rb *rb, unsigned int pos)
794{
795 return (pos & (rb->nr_entries - 1));
796}
797
798int pblk_rb_pos_oob(struct pblk_rb *rb, u64 pos)
799{
800 return (pos >= rb->nr_entries);
801}
802
803ssize_t pblk_rb_sysfs(struct pblk_rb *rb, char *buf)
804{
805 struct pblk *pblk = container_of(rb, struct pblk, rwb);
806 struct pblk_c_ctx *c;
807 ssize_t offset;
808 int queued_entries = 0;
809
810 spin_lock_irq(&rb->s_lock);
811 list_for_each_entry(c, &pblk->compl_list, list)
812 queued_entries++;
813 spin_unlock_irq(&rb->s_lock);
814
815 if (rb->sync_point != EMPTY_ENTRY)
816 offset = scnprintf(buf, PAGE_SIZE,
817 "%u\t%u\t%u\t%u\t%u\t%u\t%u - %u/%u/%u - %d\n",
818 rb->nr_entries,
819 rb->mem,
820 rb->subm,
821 rb->sync,
822 rb->l2p_update,
823#ifdef CONFIG_NVM_DEBUG
824 atomic_read(&rb->inflight_sync_point),
825#else
826 0,
827#endif
828 rb->sync_point,
829 pblk_rb_read_count(rb),
830 pblk_rb_space(rb),
831 pblk_rb_sync_point_count(rb),
832 queued_entries);
833 else
834 offset = scnprintf(buf, PAGE_SIZE,
835 "%u\t%u\t%u\t%u\t%u\t%u\tNULL - %u/%u/%u - %d\n",
836 rb->nr_entries,
837 rb->mem,
838 rb->subm,
839 rb->sync,
840 rb->l2p_update,
841#ifdef CONFIG_NVM_DEBUG
842 atomic_read(&rb->inflight_sync_point),
843#else
844 0,
845#endif
846 pblk_rb_read_count(rb),
847 pblk_rb_space(rb),
848 pblk_rb_sync_point_count(rb),
849 queued_entries);
850
851 return offset;
852}