2 * Copyright (C) 2016 CNEX Labs
3 * Initial release: Javier Gonzalez <javier@cnexlabs.com>
4 * Matias Bjorling <matias@cnexlabs.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version
8 * 2 as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * Implementation of a physical block-device target for Open-channel SSDs.
17 * pblk-sysfs.c - pblk's sysfs
23 static ssize_t
pblk_sysfs_luns_show(struct pblk
*pblk
, char *page
)
25 struct nvm_tgt_dev
*dev
= pblk
->dev
;
26 struct nvm_geo
*geo
= &dev
->geo
;
27 struct pblk_lun
*rlun
;
31 for (i
= 0; i
< geo
->all_luns
; i
++) {
34 rlun
= &pblk
->luns
[i
];
35 if (!down_trylock(&rlun
->wr_sem
)) {
39 sz
+= snprintf(page
+ sz
, PAGE_SIZE
- sz
,
40 "pblk: pos:%d, ch:%d, lun:%d - %d\n",
50 static ssize_t
pblk_sysfs_rate_limiter(struct pblk
*pblk
, char *page
)
52 int free_blocks
, free_user_blocks
, total_blocks
;
53 int rb_user_max
, rb_user_cnt
;
54 int rb_gc_max
, rb_gc_cnt
, rb_budget
, rb_state
;
56 free_blocks
= pblk_rl_nr_free_blks(&pblk
->rl
);
57 free_user_blocks
= pblk_rl_nr_user_free_blks(&pblk
->rl
);
58 rb_user_max
= pblk
->rl
.rb_user_max
;
59 rb_user_cnt
= atomic_read(&pblk
->rl
.rb_user_cnt
);
60 rb_gc_max
= pblk
->rl
.rb_gc_max
;
61 rb_gc_cnt
= atomic_read(&pblk
->rl
.rb_gc_cnt
);
62 rb_budget
= pblk
->rl
.rb_budget
;
63 rb_state
= pblk
->rl
.rb_state
;
65 total_blocks
= pblk
->rl
.total_blocks
;
67 return snprintf(page
, PAGE_SIZE
,
68 "u:%u/%u,gc:%u/%u(%u)(stop:<%u,full:>%u,free:%d/%d/%d)-%d\n",
79 READ_ONCE(pblk
->rl
.rb_user_active
));
82 static ssize_t
pblk_sysfs_gc_state_show(struct pblk
*pblk
, char *page
)
84 int gc_enabled
, gc_active
;
86 pblk_gc_sysfs_state_show(pblk
, &gc_enabled
, &gc_active
);
87 return snprintf(page
, PAGE_SIZE
, "gc_enabled=%d, gc_active=%d\n",
88 gc_enabled
, gc_active
);
91 static ssize_t
pblk_sysfs_stats(struct pblk
*pblk
, char *page
)
95 sz
= snprintf(page
, PAGE_SIZE
,
96 "read_failed=%lu, read_high_ecc=%lu, read_empty=%lu, read_failed_gc=%lu, write_failed=%lu, erase_failed=%lu\n",
97 atomic_long_read(&pblk
->read_failed
),
98 atomic_long_read(&pblk
->read_high_ecc
),
99 atomic_long_read(&pblk
->read_empty
),
100 atomic_long_read(&pblk
->read_failed_gc
),
101 atomic_long_read(&pblk
->write_failed
),
102 atomic_long_read(&pblk
->erase_failed
));
107 static ssize_t
pblk_sysfs_write_buffer(struct pblk
*pblk
, char *page
)
109 return pblk_rb_sysfs(&pblk
->rwb
, page
);
112 static ssize_t
pblk_sysfs_ppaf(struct pblk
*pblk
, char *page
)
114 struct nvm_tgt_dev
*dev
= pblk
->dev
;
115 struct nvm_geo
*geo
= &dev
->geo
;
118 sz
= snprintf(page
, PAGE_SIZE
- sz
,
119 "g:(b:%d)blk:%d/%d,pg:%d/%d,lun:%d/%d,ch:%d/%d,pl:%d/%d,sec:%d/%d\n",
121 pblk
->ppaf
.blk_offset
, geo
->ppaf
.blk_len
,
122 pblk
->ppaf
.pg_offset
, geo
->ppaf
.pg_len
,
123 pblk
->ppaf
.lun_offset
, geo
->ppaf
.lun_len
,
124 pblk
->ppaf
.ch_offset
, geo
->ppaf
.ch_len
,
125 pblk
->ppaf
.pln_offset
, geo
->ppaf
.pln_len
,
126 pblk
->ppaf
.sec_offset
, geo
->ppaf
.sect_len
);
128 sz
+= snprintf(page
+ sz
, PAGE_SIZE
- sz
,
129 "d:blk:%d/%d,pg:%d/%d,lun:%d/%d,ch:%d/%d,pl:%d/%d,sec:%d/%d\n",
130 geo
->ppaf
.blk_offset
, geo
->ppaf
.blk_len
,
131 geo
->ppaf
.pg_offset
, geo
->ppaf
.pg_len
,
132 geo
->ppaf
.lun_offset
, geo
->ppaf
.lun_len
,
133 geo
->ppaf
.ch_offset
, geo
->ppaf
.ch_len
,
134 geo
->ppaf
.pln_offset
, geo
->ppaf
.pln_len
,
135 geo
->ppaf
.sect_offset
, geo
->ppaf
.sect_len
);
140 static ssize_t
pblk_sysfs_lines(struct pblk
*pblk
, char *page
)
142 struct nvm_tgt_dev
*dev
= pblk
->dev
;
143 struct nvm_geo
*geo
= &dev
->geo
;
144 struct pblk_line_meta
*lm
= &pblk
->lm
;
145 struct pblk_line_mgmt
*l_mg
= &pblk
->l_mg
;
146 struct pblk_line
*line
;
149 int cur_data
, cur_log
;
150 int free_line_cnt
= 0, closed_line_cnt
= 0, emeta_line_cnt
= 0;
151 int d_line_cnt
= 0, l_line_cnt
= 0;
152 int gc_full
= 0, gc_high
= 0, gc_mid
= 0, gc_low
= 0, gc_empty
= 0;
153 int bad
= 0, cor
= 0;
154 int msecs
= 0, cur_sec
= 0, vsc
= 0, sec_in_line
= 0;
155 int map_weight
= 0, meta_weight
= 0;
157 spin_lock(&l_mg
->free_lock
);
158 cur_data
= (l_mg
->data_line
) ? l_mg
->data_line
->id
: -1;
159 cur_log
= (l_mg
->log_line
) ? l_mg
->log_line
->id
: -1;
160 nr_free_lines
= l_mg
->nr_free_lines
;
162 list_for_each_entry(line
, &l_mg
->free_list
, list
)
164 spin_unlock(&l_mg
->free_lock
);
166 spin_lock(&l_mg
->close_lock
);
167 list_for_each_entry(line
, &l_mg
->emeta_list
, list
)
169 spin_unlock(&l_mg
->close_lock
);
171 spin_lock(&l_mg
->gc_lock
);
172 list_for_each_entry(line
, &l_mg
->gc_full_list
, list
) {
173 if (line
->type
== PBLK_LINETYPE_DATA
)
175 else if (line
->type
== PBLK_LINETYPE_LOG
)
181 list_for_each_entry(line
, &l_mg
->gc_high_list
, list
) {
182 if (line
->type
== PBLK_LINETYPE_DATA
)
184 else if (line
->type
== PBLK_LINETYPE_LOG
)
190 list_for_each_entry(line
, &l_mg
->gc_mid_list
, list
) {
191 if (line
->type
== PBLK_LINETYPE_DATA
)
193 else if (line
->type
== PBLK_LINETYPE_LOG
)
199 list_for_each_entry(line
, &l_mg
->gc_low_list
, list
) {
200 if (line
->type
== PBLK_LINETYPE_DATA
)
202 else if (line
->type
== PBLK_LINETYPE_LOG
)
208 list_for_each_entry(line
, &l_mg
->gc_empty_list
, list
) {
209 if (line
->type
== PBLK_LINETYPE_DATA
)
211 else if (line
->type
== PBLK_LINETYPE_LOG
)
217 list_for_each_entry(line
, &l_mg
->bad_list
, list
)
219 list_for_each_entry(line
, &l_mg
->corrupt_list
, list
)
221 spin_unlock(&l_mg
->gc_lock
);
223 spin_lock(&l_mg
->free_lock
);
224 if (l_mg
->data_line
) {
225 cur_sec
= l_mg
->data_line
->cur_sec
;
226 msecs
= l_mg
->data_line
->left_msecs
;
227 vsc
= le32_to_cpu(*l_mg
->data_line
->vsc
);
228 sec_in_line
= l_mg
->data_line
->sec_in_line
;
229 meta_weight
= bitmap_weight(&l_mg
->meta_bitmap
,
231 map_weight
= bitmap_weight(l_mg
->data_line
->map_bitmap
,
234 spin_unlock(&l_mg
->free_lock
);
236 if (nr_free_lines
!= free_line_cnt
)
237 pr_err("pblk: corrupted free line list:%d/%d\n",
238 nr_free_lines
, free_line_cnt
);
240 sz
= snprintf(page
, PAGE_SIZE
- sz
,
241 "line: nluns:%d, nblks:%d, nsecs:%d\n",
242 geo
->all_luns
, lm
->blk_per_line
, lm
->sec_per_line
);
244 sz
+= snprintf(page
+ sz
, PAGE_SIZE
- sz
,
245 "lines:d:%d,l:%d-f:%d,m:%d/%d,c:%d,b:%d,co:%d(d:%d,l:%d)t:%d\n",
248 emeta_line_cnt
, meta_weight
,
251 d_line_cnt
, l_line_cnt
,
254 sz
+= snprintf(page
+ sz
, PAGE_SIZE
- sz
,
255 "GC: full:%d, high:%d, mid:%d, low:%d, empty:%d, queue:%d\n",
256 gc_full
, gc_high
, gc_mid
, gc_low
, gc_empty
,
257 atomic_read(&pblk
->gc
.read_inflight_gc
));
259 sz
+= snprintf(page
+ sz
, PAGE_SIZE
- sz
,
260 "data (%d) cur:%d, left:%d, vsc:%d, s:%d, map:%d/%d (%d)\n",
261 cur_data
, cur_sec
, msecs
, vsc
, sec_in_line
,
262 map_weight
, lm
->sec_per_line
,
263 atomic_read(&pblk
->inflight_io
));
268 static ssize_t
pblk_sysfs_lines_info(struct pblk
*pblk
, char *page
)
270 struct nvm_tgt_dev
*dev
= pblk
->dev
;
271 struct nvm_geo
*geo
= &dev
->geo
;
272 struct pblk_line_meta
*lm
= &pblk
->lm
;
275 sz
= snprintf(page
, PAGE_SIZE
- sz
,
276 "smeta - len:%d, secs:%d\n",
277 lm
->smeta_len
, lm
->smeta_sec
);
278 sz
+= snprintf(page
+ sz
, PAGE_SIZE
- sz
,
279 "emeta - len:%d, sec:%d, bb_start:%d\n",
280 lm
->emeta_len
[0], lm
->emeta_sec
[0],
282 sz
+= snprintf(page
+ sz
, PAGE_SIZE
- sz
,
283 "bitmap lengths: sec:%d, blk:%d, lun:%d\n",
287 sz
+= snprintf(page
+ sz
, PAGE_SIZE
- sz
,
288 "blk_line:%d, sec_line:%d, sec_blk:%d\n",
296 static ssize_t
pblk_sysfs_get_sec_per_write(struct pblk
*pblk
, char *page
)
298 return snprintf(page
, PAGE_SIZE
, "%d\n", pblk
->sec_per_write
);
301 #ifdef CONFIG_NVM_DEBUG
302 static ssize_t
pblk_sysfs_stats_debug(struct pblk
*pblk
, char *page
)
304 return snprintf(page
, PAGE_SIZE
,
305 "%lu\t%lu\t%lu\t%lu\t%lu\t%lu\t%lu\t%lu\t%lu\t%lu\t%lu\t%lu\t%lu\n",
306 atomic_long_read(&pblk
->inflight_writes
),
307 atomic_long_read(&pblk
->inflight_reads
),
308 atomic_long_read(&pblk
->req_writes
),
309 atomic_long_read(&pblk
->nr_flush
),
310 atomic_long_read(&pblk
->padded_writes
),
311 atomic_long_read(&pblk
->padded_wb
),
312 atomic_long_read(&pblk
->sub_writes
),
313 atomic_long_read(&pblk
->sync_writes
),
314 atomic_long_read(&pblk
->recov_writes
),
315 atomic_long_read(&pblk
->recov_gc_writes
),
316 atomic_long_read(&pblk
->recov_gc_reads
),
317 atomic_long_read(&pblk
->cache_reads
),
318 atomic_long_read(&pblk
->sync_reads
));
322 static ssize_t
pblk_sysfs_gc_force(struct pblk
*pblk
, const char *page
,
328 c_len
= strcspn(page
, "\n");
332 if (kstrtouint(page
, 0, &force
))
335 pblk_gc_sysfs_force(pblk
, force
);
340 static ssize_t
pblk_sysfs_set_sec_per_write(struct pblk
*pblk
,
341 const char *page
, size_t len
)
346 c_len
= strcspn(page
, "\n");
350 if (kstrtouint(page
, 0, &sec_per_write
))
353 if (sec_per_write
< pblk
->min_write_pgs
354 || sec_per_write
> pblk
->max_write_pgs
355 || sec_per_write
% pblk
->min_write_pgs
!= 0)
358 pblk_set_sec_per_write(pblk
, sec_per_write
);
363 static struct attribute sys_write_luns
= {
364 .name
= "write_luns",
368 static struct attribute sys_rate_limiter_attr
= {
369 .name
= "rate_limiter",
373 static struct attribute sys_gc_state
= {
378 static struct attribute sys_errors_attr
= {
383 static struct attribute sys_rb_attr
= {
384 .name
= "write_buffer",
388 static struct attribute sys_stats_ppaf_attr
= {
389 .name
= "ppa_format",
393 static struct attribute sys_lines_attr
= {
398 static struct attribute sys_lines_info_attr
= {
399 .name
= "lines_info",
403 static struct attribute sys_gc_force
= {
408 static struct attribute sys_max_sec_per_write
= {
409 .name
= "max_sec_per_write",
413 #ifdef CONFIG_NVM_DEBUG
414 static struct attribute sys_stats_debug_attr
= {
420 static struct attribute
*pblk_attrs
[] = {
422 &sys_rate_limiter_attr
,
426 &sys_max_sec_per_write
,
428 &sys_stats_ppaf_attr
,
430 &sys_lines_info_attr
,
431 #ifdef CONFIG_NVM_DEBUG
432 &sys_stats_debug_attr
,
437 static ssize_t
pblk_sysfs_show(struct kobject
*kobj
, struct attribute
*attr
,
440 struct pblk
*pblk
= container_of(kobj
, struct pblk
, kobj
);
442 if (strcmp(attr
->name
, "rate_limiter") == 0)
443 return pblk_sysfs_rate_limiter(pblk
, buf
);
444 else if (strcmp(attr
->name
, "write_luns") == 0)
445 return pblk_sysfs_luns_show(pblk
, buf
);
446 else if (strcmp(attr
->name
, "gc_state") == 0)
447 return pblk_sysfs_gc_state_show(pblk
, buf
);
448 else if (strcmp(attr
->name
, "errors") == 0)
449 return pblk_sysfs_stats(pblk
, buf
);
450 else if (strcmp(attr
->name
, "write_buffer") == 0)
451 return pblk_sysfs_write_buffer(pblk
, buf
);
452 else if (strcmp(attr
->name
, "ppa_format") == 0)
453 return pblk_sysfs_ppaf(pblk
, buf
);
454 else if (strcmp(attr
->name
, "lines") == 0)
455 return pblk_sysfs_lines(pblk
, buf
);
456 else if (strcmp(attr
->name
, "lines_info") == 0)
457 return pblk_sysfs_lines_info(pblk
, buf
);
458 else if (strcmp(attr
->name
, "max_sec_per_write") == 0)
459 return pblk_sysfs_get_sec_per_write(pblk
, buf
);
460 #ifdef CONFIG_NVM_DEBUG
461 else if (strcmp(attr
->name
, "stats") == 0)
462 return pblk_sysfs_stats_debug(pblk
, buf
);
467 static ssize_t
pblk_sysfs_store(struct kobject
*kobj
, struct attribute
*attr
,
468 const char *buf
, size_t len
)
470 struct pblk
*pblk
= container_of(kobj
, struct pblk
, kobj
);
472 if (strcmp(attr
->name
, "gc_force") == 0)
473 return pblk_sysfs_gc_force(pblk
, buf
, len
);
474 else if (strcmp(attr
->name
, "max_sec_per_write") == 0)
475 return pblk_sysfs_set_sec_per_write(pblk
, buf
, len
);
480 static const struct sysfs_ops pblk_sysfs_ops
= {
481 .show
= pblk_sysfs_show
,
482 .store
= pblk_sysfs_store
,
485 static struct kobj_type pblk_ktype
= {
486 .sysfs_ops
= &pblk_sysfs_ops
,
487 .default_attrs
= pblk_attrs
,
490 int pblk_sysfs_init(struct gendisk
*tdisk
)
492 struct pblk
*pblk
= tdisk
->private_data
;
493 struct device
*parent_dev
= disk_to_dev(pblk
->disk
);
496 ret
= kobject_init_and_add(&pblk
->kobj
, &pblk_ktype
,
497 kobject_get(&parent_dev
->kobj
),
500 pr_err("pblk: could not register %s/pblk\n",
505 kobject_uevent(&pblk
->kobj
, KOBJ_ADD
);
509 void pblk_sysfs_exit(struct gendisk
*tdisk
)
511 struct pblk
*pblk
= tdisk
->private_data
;
513 kobject_uevent(&pblk
->kobj
, KOBJ_REMOVE
);
514 kobject_del(&pblk
->kobj
);
515 kobject_put(&pblk
->kobj
);