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
->nr_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 struct nvm_tgt_dev
*dev
= pblk
->dev
;
53 struct nvm_geo
*geo
= &dev
->geo
;
54 int free_blocks
, total_blocks
;
55 int rb_user_max
, rb_user_cnt
;
56 int rb_gc_max
, rb_gc_rsv
, rb_gc_cnt
, rb_budget
, rb_state
;
58 free_blocks
= atomic_read(&pblk
->rl
.free_blocks
);
59 rb_user_max
= pblk
->rl
.rb_user_max
;
60 rb_user_cnt
= atomic_read(&pblk
->rl
.rb_user_cnt
);
61 rb_gc_max
= pblk
->rl
.rb_gc_max
;
62 rb_gc_rsv
= pblk
->rl
.rb_gc_rsv
;
63 rb_gc_cnt
= atomic_read(&pblk
->rl
.rb_gc_cnt
);
64 rb_budget
= pblk
->rl
.rb_budget
;
65 rb_state
= pblk
->rl
.rb_state
;
67 total_blocks
= geo
->blks_per_lun
* geo
->nr_luns
;
69 return snprintf(page
, PAGE_SIZE
,
70 "u:%u/%u,gc:%u/%u/%u(%u/%u)(stop:<%u,full:>%u,free:%d/%d)-%d\n",
82 READ_ONCE(pblk
->rl
.rb_user_active
));
85 static ssize_t
pblk_sysfs_gc_state_show(struct pblk
*pblk
, char *page
)
87 int gc_enabled
, gc_active
;
89 pblk_gc_sysfs_state_show(pblk
, &gc_enabled
, &gc_active
);
90 return snprintf(page
, PAGE_SIZE
, "gc_enabled=%d, gc_active=%d\n",
91 gc_enabled
, gc_active
);
94 static ssize_t
pblk_sysfs_stats(struct pblk
*pblk
, char *page
)
98 sz
= snprintf(page
, PAGE_SIZE
,
99 "read_failed=%lu, read_high_ecc=%lu, read_empty=%lu, read_failed_gc=%lu, write_failed=%lu, erase_failed=%lu\n",
100 atomic_long_read(&pblk
->read_failed
),
101 atomic_long_read(&pblk
->read_high_ecc
),
102 atomic_long_read(&pblk
->read_empty
),
103 atomic_long_read(&pblk
->read_failed_gc
),
104 atomic_long_read(&pblk
->write_failed
),
105 atomic_long_read(&pblk
->erase_failed
));
110 static ssize_t
pblk_sysfs_write_buffer(struct pblk
*pblk
, char *page
)
112 return pblk_rb_sysfs(&pblk
->rwb
, page
);
115 static ssize_t
pblk_sysfs_ppaf(struct pblk
*pblk
, char *page
)
117 struct nvm_tgt_dev
*dev
= pblk
->dev
;
118 struct nvm_geo
*geo
= &dev
->geo
;
121 sz
= snprintf(page
, PAGE_SIZE
- sz
,
122 "g:(b:%d)blk:%d/%d,pg:%d/%d,lun:%d/%d,ch:%d/%d,pl:%d/%d,sec:%d/%d\n",
124 pblk
->ppaf
.blk_offset
, geo
->ppaf
.blk_len
,
125 pblk
->ppaf
.pg_offset
, geo
->ppaf
.pg_len
,
126 pblk
->ppaf
.lun_offset
, geo
->ppaf
.lun_len
,
127 pblk
->ppaf
.ch_offset
, geo
->ppaf
.ch_len
,
128 pblk
->ppaf
.pln_offset
, geo
->ppaf
.pln_len
,
129 pblk
->ppaf
.sec_offset
, geo
->ppaf
.sect_len
);
131 sz
+= snprintf(page
+ sz
, PAGE_SIZE
- sz
,
132 "d:blk:%d/%d,pg:%d/%d,lun:%d/%d,ch:%d/%d,pl:%d/%d,sec:%d/%d\n",
133 geo
->ppaf
.blk_offset
, geo
->ppaf
.blk_len
,
134 geo
->ppaf
.pg_offset
, geo
->ppaf
.pg_len
,
135 geo
->ppaf
.lun_offset
, geo
->ppaf
.lun_len
,
136 geo
->ppaf
.ch_offset
, geo
->ppaf
.ch_len
,
137 geo
->ppaf
.pln_offset
, geo
->ppaf
.pln_len
,
138 geo
->ppaf
.sect_offset
, geo
->ppaf
.sect_len
);
143 static ssize_t
pblk_sysfs_lines(struct pblk
*pblk
, char *page
)
145 struct nvm_tgt_dev
*dev
= pblk
->dev
;
146 struct nvm_geo
*geo
= &dev
->geo
;
147 struct pblk_line_meta
*lm
= &pblk
->lm
;
148 struct pblk_line_mgmt
*l_mg
= &pblk
->l_mg
;
149 struct pblk_line
*line
;
152 int cur_data
, cur_log
;
153 int free_line_cnt
= 0, closed_line_cnt
= 0;
154 int d_line_cnt
= 0, l_line_cnt
= 0;
155 int gc_full
= 0, gc_high
= 0, gc_mid
= 0, gc_low
= 0, gc_empty
= 0;
156 int free
= 0, bad
= 0, cor
= 0;
157 int msecs
= 0, ssecs
= 0, cur_sec
= 0, vsc
= 0, sec_in_line
= 0;
158 int map_weight
= 0, meta_weight
= 0;
160 spin_lock(&l_mg
->free_lock
);
161 cur_data
= (l_mg
->data_line
) ? l_mg
->data_line
->id
: -1;
162 cur_log
= (l_mg
->log_line
) ? l_mg
->log_line
->id
: -1;
163 nr_free_lines
= l_mg
->nr_free_lines
;
165 list_for_each_entry(line
, &l_mg
->free_list
, list
)
167 spin_unlock(&l_mg
->free_lock
);
169 spin_lock(&l_mg
->gc_lock
);
170 list_for_each_entry(line
, &l_mg
->gc_full_list
, list
) {
171 if (line
->type
== PBLK_LINETYPE_DATA
)
173 else if (line
->type
== PBLK_LINETYPE_LOG
)
179 list_for_each_entry(line
, &l_mg
->gc_high_list
, list
) {
180 if (line
->type
== PBLK_LINETYPE_DATA
)
182 else if (line
->type
== PBLK_LINETYPE_LOG
)
188 list_for_each_entry(line
, &l_mg
->gc_mid_list
, list
) {
189 if (line
->type
== PBLK_LINETYPE_DATA
)
191 else if (line
->type
== PBLK_LINETYPE_LOG
)
197 list_for_each_entry(line
, &l_mg
->gc_low_list
, list
) {
198 if (line
->type
== PBLK_LINETYPE_DATA
)
200 else if (line
->type
== PBLK_LINETYPE_LOG
)
206 list_for_each_entry(line
, &l_mg
->gc_empty_list
, list
) {
207 if (line
->type
== PBLK_LINETYPE_DATA
)
209 else if (line
->type
== PBLK_LINETYPE_LOG
)
215 list_for_each_entry(line
, &l_mg
->free_list
, list
)
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 ssecs
= l_mg
->data_line
->left_ssecs
;
228 vsc
= l_mg
->data_line
->vsc
;
229 sec_in_line
= l_mg
->data_line
->sec_in_line
;
230 meta_weight
= bitmap_weight(&l_mg
->meta_bitmap
,
232 map_weight
= bitmap_weight(l_mg
->data_line
->map_bitmap
,
235 spin_unlock(&l_mg
->free_lock
);
237 if (nr_free_lines
!= free_line_cnt
)
238 pr_err("pblk: corrupted free line list\n");
240 sz
= snprintf(page
, PAGE_SIZE
- sz
,
241 "line: nluns:%d, nblks:%d, nsecs:%d\n",
242 geo
->nr_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(%d),b:%d,co:%d,c:%d(d:%d,l:%d)t:%d\n",
247 free
, nr_free_lines
, bad
, cor
,
249 d_line_cnt
, l_line_cnt
,
252 sz
+= snprintf(page
+ sz
, PAGE_SIZE
- sz
,
253 "GC: full:%d, high:%d, mid:%d, low:%d, empty:%d, queue:%d\n",
254 gc_full
, gc_high
, gc_mid
, gc_low
, gc_empty
,
255 atomic_read(&pblk
->gc
.inflight_gc
));
257 sz
+= snprintf(page
+ sz
, PAGE_SIZE
- sz
,
258 "data (%d) cur:%d, left:%d/%d, vsc:%d, s:%d, map:%d/%d (%d)\n",
259 cur_data
, cur_sec
, msecs
, ssecs
, vsc
, sec_in_line
,
260 map_weight
, lm
->sec_per_line
, meta_weight
);
265 static ssize_t
pblk_sysfs_lines_info(struct pblk
*pblk
, char *page
)
267 struct nvm_tgt_dev
*dev
= pblk
->dev
;
268 struct nvm_geo
*geo
= &dev
->geo
;
269 struct pblk_line_meta
*lm
= &pblk
->lm
;
272 sz
= snprintf(page
, PAGE_SIZE
- sz
,
273 "smeta - len:%d, secs:%d\n",
274 lm
->smeta_len
, lm
->smeta_sec
);
275 sz
+= snprintf(page
+ sz
, PAGE_SIZE
- sz
,
276 "emeta - len:%d, sec:%d, bb_start:%d\n",
277 lm
->emeta_len
, lm
->emeta_sec
,
279 sz
+= snprintf(page
+ sz
, PAGE_SIZE
- sz
,
280 "bitmap lengths: sec:%d, blk:%d, lun:%d\n",
284 sz
+= snprintf(page
+ sz
, PAGE_SIZE
- sz
,
285 "blk_line:%d, sec_line:%d, sec_blk:%d\n",
293 #ifdef CONFIG_NVM_DEBUG
294 static ssize_t
pblk_sysfs_stats_debug(struct pblk
*pblk
, char *page
)
296 return snprintf(page
, PAGE_SIZE
,
297 "%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",
298 atomic_long_read(&pblk
->inflight_writes
),
299 atomic_long_read(&pblk
->inflight_reads
),
300 atomic_long_read(&pblk
->req_writes
),
301 atomic_long_read(&pblk
->nr_flush
),
302 atomic_long_read(&pblk
->padded_writes
),
303 atomic_long_read(&pblk
->padded_wb
),
304 atomic_long_read(&pblk
->sub_writes
),
305 atomic_long_read(&pblk
->sync_writes
),
306 atomic_long_read(&pblk
->compl_writes
),
307 atomic_long_read(&pblk
->recov_writes
),
308 atomic_long_read(&pblk
->recov_gc_writes
),
309 atomic_long_read(&pblk
->recov_gc_reads
),
310 atomic_long_read(&pblk
->sync_reads
));
314 static ssize_t
pblk_sysfs_rate_store(struct pblk
*pblk
, const char *page
,
317 struct pblk_gc
*gc
= &pblk
->gc
;
321 c_len
= strcspn(page
, "\n");
325 if (kstrtouint(page
, 0, &value
))
328 spin_lock(&gc
->lock
);
329 pblk_rl_set_gc_rsc(&pblk
->rl
, value
);
330 spin_unlock(&gc
->lock
);
335 static ssize_t
pblk_sysfs_gc_force(struct pblk
*pblk
, const char *page
,
341 c_len
= strcspn(page
, "\n");
345 if (kstrtouint(page
, 0, &force
))
348 if (force
< 0 || force
> 1)
351 pblk_gc_sysfs_force(pblk
, force
);
356 static struct attribute sys_write_luns
= {
357 .name
= "write_luns",
361 static struct attribute sys_rate_limiter_attr
= {
362 .name
= "rate_limiter",
366 static struct attribute sys_gc_state
= {
371 static struct attribute sys_errors_attr
= {
376 static struct attribute sys_rb_attr
= {
377 .name
= "write_buffer",
381 static struct attribute sys_stats_ppaf_attr
= {
382 .name
= "ppa_format",
386 static struct attribute sys_lines_attr
= {
391 static struct attribute sys_lines_info_attr
= {
392 .name
= "lines_info",
396 static struct attribute sys_gc_force
= {
401 static struct attribute sys_gc_rl_max
= {
406 #ifdef CONFIG_NVM_DEBUG
407 static struct attribute sys_stats_debug_attr
= {
413 static struct attribute
*pblk_attrs
[] = {
415 &sys_rate_limiter_attr
,
421 &sys_stats_ppaf_attr
,
423 &sys_lines_info_attr
,
424 #ifdef CONFIG_NVM_DEBUG
425 &sys_stats_debug_attr
,
430 static ssize_t
pblk_sysfs_show(struct kobject
*kobj
, struct attribute
*attr
,
433 struct pblk
*pblk
= container_of(kobj
, struct pblk
, kobj
);
435 if (strcmp(attr
->name
, "rate_limiter") == 0)
436 return pblk_sysfs_rate_limiter(pblk
, buf
);
437 else if (strcmp(attr
->name
, "write_luns") == 0)
438 return pblk_sysfs_luns_show(pblk
, buf
);
439 else if (strcmp(attr
->name
, "gc_state") == 0)
440 return pblk_sysfs_gc_state_show(pblk
, buf
);
441 else if (strcmp(attr
->name
, "errors") == 0)
442 return pblk_sysfs_stats(pblk
, buf
);
443 else if (strcmp(attr
->name
, "write_buffer") == 0)
444 return pblk_sysfs_write_buffer(pblk
, buf
);
445 else if (strcmp(attr
->name
, "ppa_format") == 0)
446 return pblk_sysfs_ppaf(pblk
, buf
);
447 else if (strcmp(attr
->name
, "lines") == 0)
448 return pblk_sysfs_lines(pblk
, buf
);
449 else if (strcmp(attr
->name
, "lines_info") == 0)
450 return pblk_sysfs_lines_info(pblk
, buf
);
451 #ifdef CONFIG_NVM_DEBUG
452 else if (strcmp(attr
->name
, "stats") == 0)
453 return pblk_sysfs_stats_debug(pblk
, buf
);
458 static ssize_t
pblk_sysfs_store(struct kobject
*kobj
, struct attribute
*attr
,
459 const char *buf
, size_t len
)
461 struct pblk
*pblk
= container_of(kobj
, struct pblk
, kobj
);
463 if (strcmp(attr
->name
, "gc_rl_max") == 0)
464 return pblk_sysfs_rate_store(pblk
, buf
, len
);
465 else if (strcmp(attr
->name
, "gc_force") == 0)
466 return pblk_sysfs_gc_force(pblk
, buf
, len
);
471 static const struct sysfs_ops pblk_sysfs_ops
= {
472 .show
= pblk_sysfs_show
,
473 .store
= pblk_sysfs_store
,
476 static struct kobj_type pblk_ktype
= {
477 .sysfs_ops
= &pblk_sysfs_ops
,
478 .default_attrs
= pblk_attrs
,
481 int pblk_sysfs_init(struct gendisk
*tdisk
)
483 struct pblk
*pblk
= tdisk
->private_data
;
484 struct device
*parent_dev
= disk_to_dev(pblk
->disk
);
487 ret
= kobject_init_and_add(&pblk
->kobj
, &pblk_ktype
,
488 kobject_get(&parent_dev
->kobj
),
491 pr_err("pblk: could not register %s/pblk\n",
496 kobject_uevent(&pblk
->kobj
, KOBJ_ADD
);
500 void pblk_sysfs_exit(struct gendisk
*tdisk
)
502 struct pblk
*pblk
= tdisk
->private_data
;
504 kobject_uevent(&pblk
->kobj
, KOBJ_REMOVE
);
505 kobject_del(&pblk
->kobj
);
506 kobject_put(&pblk
->kobj
);