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 int free_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
= atomic_read(&pblk
->rl
.free_blocks
);
57 rb_user_max
= pblk
->rl
.rb_user_max
;
58 rb_user_cnt
= atomic_read(&pblk
->rl
.rb_user_cnt
);
59 rb_gc_max
= pblk
->rl
.rb_gc_max
;
60 rb_gc_cnt
= atomic_read(&pblk
->rl
.rb_gc_cnt
);
61 rb_budget
= pblk
->rl
.rb_budget
;
62 rb_state
= pblk
->rl
.rb_state
;
64 total_blocks
= pblk
->rl
.total_blocks
;
66 return snprintf(page
, PAGE_SIZE
,
67 "u:%u/%u,gc:%u/%u(%u/%u)(stop:<%u,full:>%u,free:%d/%d)-%d\n",
78 READ_ONCE(pblk
->rl
.rb_user_active
));
81 static ssize_t
pblk_sysfs_gc_state_show(struct pblk
*pblk
, char *page
)
83 int gc_enabled
, gc_active
;
85 pblk_gc_sysfs_state_show(pblk
, &gc_enabled
, &gc_active
);
86 return snprintf(page
, PAGE_SIZE
, "gc_enabled=%d, gc_active=%d\n",
87 gc_enabled
, gc_active
);
90 static ssize_t
pblk_sysfs_stats(struct pblk
*pblk
, char *page
)
94 sz
= snprintf(page
, PAGE_SIZE
,
95 "read_failed=%lu, read_high_ecc=%lu, read_empty=%lu, read_failed_gc=%lu, write_failed=%lu, erase_failed=%lu\n",
96 atomic_long_read(&pblk
->read_failed
),
97 atomic_long_read(&pblk
->read_high_ecc
),
98 atomic_long_read(&pblk
->read_empty
),
99 atomic_long_read(&pblk
->read_failed_gc
),
100 atomic_long_read(&pblk
->write_failed
),
101 atomic_long_read(&pblk
->erase_failed
));
106 static ssize_t
pblk_sysfs_write_buffer(struct pblk
*pblk
, char *page
)
108 return pblk_rb_sysfs(&pblk
->rwb
, page
);
111 static ssize_t
pblk_sysfs_ppaf(struct pblk
*pblk
, char *page
)
113 struct nvm_tgt_dev
*dev
= pblk
->dev
;
114 struct nvm_geo
*geo
= &dev
->geo
;
117 sz
= snprintf(page
, PAGE_SIZE
- sz
,
118 "g:(b:%d)blk:%d/%d,pg:%d/%d,lun:%d/%d,ch:%d/%d,pl:%d/%d,sec:%d/%d\n",
120 pblk
->ppaf
.blk_offset
, geo
->ppaf
.blk_len
,
121 pblk
->ppaf
.pg_offset
, geo
->ppaf
.pg_len
,
122 pblk
->ppaf
.lun_offset
, geo
->ppaf
.lun_len
,
123 pblk
->ppaf
.ch_offset
, geo
->ppaf
.ch_len
,
124 pblk
->ppaf
.pln_offset
, geo
->ppaf
.pln_len
,
125 pblk
->ppaf
.sec_offset
, geo
->ppaf
.sect_len
);
127 sz
+= snprintf(page
+ sz
, PAGE_SIZE
- sz
,
128 "d:blk:%d/%d,pg:%d/%d,lun:%d/%d,ch:%d/%d,pl:%d/%d,sec:%d/%d\n",
129 geo
->ppaf
.blk_offset
, geo
->ppaf
.blk_len
,
130 geo
->ppaf
.pg_offset
, geo
->ppaf
.pg_len
,
131 geo
->ppaf
.lun_offset
, geo
->ppaf
.lun_len
,
132 geo
->ppaf
.ch_offset
, geo
->ppaf
.ch_len
,
133 geo
->ppaf
.pln_offset
, geo
->ppaf
.pln_len
,
134 geo
->ppaf
.sect_offset
, geo
->ppaf
.sect_len
);
139 static ssize_t
pblk_sysfs_lines(struct pblk
*pblk
, char *page
)
141 struct nvm_tgt_dev
*dev
= pblk
->dev
;
142 struct nvm_geo
*geo
= &dev
->geo
;
143 struct pblk_line_meta
*lm
= &pblk
->lm
;
144 struct pblk_line_mgmt
*l_mg
= &pblk
->l_mg
;
145 struct pblk_line
*line
;
148 int cur_data
, cur_log
;
149 int free_line_cnt
= 0, closed_line_cnt
= 0, emeta_line_cnt
= 0;
150 int d_line_cnt
= 0, l_line_cnt
= 0;
151 int gc_full
= 0, gc_high
= 0, gc_mid
= 0, gc_low
= 0, gc_empty
= 0;
152 int bad
= 0, cor
= 0;
153 int msecs
= 0, cur_sec
= 0, vsc
= 0, sec_in_line
= 0;
154 int map_weight
= 0, meta_weight
= 0;
156 spin_lock(&l_mg
->free_lock
);
157 cur_data
= (l_mg
->data_line
) ? l_mg
->data_line
->id
: -1;
158 cur_log
= (l_mg
->log_line
) ? l_mg
->log_line
->id
: -1;
159 nr_free_lines
= l_mg
->nr_free_lines
;
161 list_for_each_entry(line
, &l_mg
->free_list
, list
)
163 spin_unlock(&l_mg
->free_lock
);
165 spin_lock(&l_mg
->close_lock
);
166 list_for_each_entry(line
, &l_mg
->emeta_list
, list
)
168 spin_unlock(&l_mg
->close_lock
);
170 spin_lock(&l_mg
->gc_lock
);
171 list_for_each_entry(line
, &l_mg
->gc_full_list
, list
) {
172 if (line
->type
== PBLK_LINETYPE_DATA
)
174 else if (line
->type
== PBLK_LINETYPE_LOG
)
180 list_for_each_entry(line
, &l_mg
->gc_high_list
, list
) {
181 if (line
->type
== PBLK_LINETYPE_DATA
)
183 else if (line
->type
== PBLK_LINETYPE_LOG
)
189 list_for_each_entry(line
, &l_mg
->gc_mid_list
, list
) {
190 if (line
->type
== PBLK_LINETYPE_DATA
)
192 else if (line
->type
== PBLK_LINETYPE_LOG
)
198 list_for_each_entry(line
, &l_mg
->gc_low_list
, list
) {
199 if (line
->type
== PBLK_LINETYPE_DATA
)
201 else if (line
->type
== PBLK_LINETYPE_LOG
)
207 list_for_each_entry(line
, &l_mg
->gc_empty_list
, list
) {
208 if (line
->type
== PBLK_LINETYPE_DATA
)
210 else if (line
->type
== PBLK_LINETYPE_LOG
)
216 list_for_each_entry(line
, &l_mg
->bad_list
, list
)
218 list_for_each_entry(line
, &l_mg
->corrupt_list
, list
)
220 spin_unlock(&l_mg
->gc_lock
);
222 spin_lock(&l_mg
->free_lock
);
223 if (l_mg
->data_line
) {
224 cur_sec
= l_mg
->data_line
->cur_sec
;
225 msecs
= l_mg
->data_line
->left_msecs
;
226 vsc
= le32_to_cpu(*l_mg
->data_line
->vsc
);
227 sec_in_line
= l_mg
->data_line
->sec_in_line
;
228 meta_weight
= bitmap_weight(&l_mg
->meta_bitmap
,
230 map_weight
= bitmap_weight(l_mg
->data_line
->map_bitmap
,
233 spin_unlock(&l_mg
->free_lock
);
235 if (nr_free_lines
!= free_line_cnt
)
236 pr_err("pblk: corrupted free line list:%d/%d\n",
237 nr_free_lines
, free_line_cnt
);
239 sz
= snprintf(page
, PAGE_SIZE
- sz
,
240 "line: nluns:%d, nblks:%d, nsecs:%d\n",
241 geo
->nr_luns
, lm
->blk_per_line
, lm
->sec_per_line
);
243 sz
+= snprintf(page
+ sz
, PAGE_SIZE
- sz
,
244 "lines:d:%d,l:%d-f:%d,m:%d/%d,c:%d,b:%d,co:%d(d:%d,l:%d)t:%d\n",
247 emeta_line_cnt
, meta_weight
,
250 d_line_cnt
, l_line_cnt
,
253 sz
+= snprintf(page
+ sz
, PAGE_SIZE
- sz
,
254 "GC: full:%d, high:%d, mid:%d, low:%d, empty:%d, queue:%d\n",
255 gc_full
, gc_high
, gc_mid
, gc_low
, gc_empty
,
256 atomic_read(&pblk
->gc
.inflight_gc
));
258 sz
+= snprintf(page
+ sz
, PAGE_SIZE
- sz
,
259 "data (%d) cur:%d, left:%d, vsc:%d, s:%d, map:%d/%d (%d)\n",
260 cur_data
, cur_sec
, msecs
, vsc
, sec_in_line
,
261 map_weight
, lm
->sec_per_line
,
262 atomic_read(&pblk
->inflight_io
));
267 static ssize_t
pblk_sysfs_lines_info(struct pblk
*pblk
, char *page
)
269 struct nvm_tgt_dev
*dev
= pblk
->dev
;
270 struct nvm_geo
*geo
= &dev
->geo
;
271 struct pblk_line_meta
*lm
= &pblk
->lm
;
274 sz
= snprintf(page
, PAGE_SIZE
- sz
,
275 "smeta - len:%d, secs:%d\n",
276 lm
->smeta_len
, lm
->smeta_sec
);
277 sz
+= snprintf(page
+ sz
, PAGE_SIZE
- sz
,
278 "emeta - len:%d, sec:%d, bb_start:%d\n",
279 lm
->emeta_len
[0], lm
->emeta_sec
[0],
281 sz
+= snprintf(page
+ sz
, PAGE_SIZE
- sz
,
282 "bitmap lengths: sec:%d, blk:%d, lun:%d\n",
286 sz
+= snprintf(page
+ sz
, PAGE_SIZE
- sz
,
287 "blk_line:%d, sec_line:%d, sec_blk:%d\n",
295 static ssize_t
pblk_sysfs_get_sec_per_write(struct pblk
*pblk
, char *page
)
297 return snprintf(page
, PAGE_SIZE
, "%d\n", pblk
->sec_per_write
);
300 #ifdef CONFIG_NVM_DEBUG
301 static ssize_t
pblk_sysfs_stats_debug(struct pblk
*pblk
, char *page
)
303 return snprintf(page
, PAGE_SIZE
,
304 "%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",
305 atomic_long_read(&pblk
->inflight_writes
),
306 atomic_long_read(&pblk
->inflight_reads
),
307 atomic_long_read(&pblk
->req_writes
),
308 atomic_long_read(&pblk
->nr_flush
),
309 atomic_long_read(&pblk
->padded_writes
),
310 atomic_long_read(&pblk
->padded_wb
),
311 atomic_long_read(&pblk
->sub_writes
),
312 atomic_long_read(&pblk
->sync_writes
),
313 atomic_long_read(&pblk
->recov_writes
),
314 atomic_long_read(&pblk
->recov_gc_writes
),
315 atomic_long_read(&pblk
->recov_gc_reads
),
316 atomic_long_read(&pblk
->cache_reads
),
317 atomic_long_read(&pblk
->sync_reads
));
321 static ssize_t
pblk_sysfs_gc_force(struct pblk
*pblk
, const char *page
,
327 c_len
= strcspn(page
, "\n");
331 if (kstrtouint(page
, 0, &force
))
334 pblk_gc_sysfs_force(pblk
, force
);
339 static ssize_t
pblk_sysfs_set_sec_per_write(struct pblk
*pblk
,
340 const char *page
, size_t len
)
345 c_len
= strcspn(page
, "\n");
349 if (kstrtouint(page
, 0, &sec_per_write
))
352 if (sec_per_write
< pblk
->min_write_pgs
353 || sec_per_write
> pblk
->max_write_pgs
354 || sec_per_write
% pblk
->min_write_pgs
!= 0)
357 pblk_set_sec_per_write(pblk
, sec_per_write
);
362 static struct attribute sys_write_luns
= {
363 .name
= "write_luns",
367 static struct attribute sys_rate_limiter_attr
= {
368 .name
= "rate_limiter",
372 static struct attribute sys_gc_state
= {
377 static struct attribute sys_errors_attr
= {
382 static struct attribute sys_rb_attr
= {
383 .name
= "write_buffer",
387 static struct attribute sys_stats_ppaf_attr
= {
388 .name
= "ppa_format",
392 static struct attribute sys_lines_attr
= {
397 static struct attribute sys_lines_info_attr
= {
398 .name
= "lines_info",
402 static struct attribute sys_gc_force
= {
407 static struct attribute sys_max_sec_per_write
= {
408 .name
= "max_sec_per_write",
412 #ifdef CONFIG_NVM_DEBUG
413 static struct attribute sys_stats_debug_attr
= {
419 static struct attribute
*pblk_attrs
[] = {
421 &sys_rate_limiter_attr
,
425 &sys_max_sec_per_write
,
427 &sys_stats_ppaf_attr
,
429 &sys_lines_info_attr
,
430 #ifdef CONFIG_NVM_DEBUG
431 &sys_stats_debug_attr
,
436 static ssize_t
pblk_sysfs_show(struct kobject
*kobj
, struct attribute
*attr
,
439 struct pblk
*pblk
= container_of(kobj
, struct pblk
, kobj
);
441 if (strcmp(attr
->name
, "rate_limiter") == 0)
442 return pblk_sysfs_rate_limiter(pblk
, buf
);
443 else if (strcmp(attr
->name
, "write_luns") == 0)
444 return pblk_sysfs_luns_show(pblk
, buf
);
445 else if (strcmp(attr
->name
, "gc_state") == 0)
446 return pblk_sysfs_gc_state_show(pblk
, buf
);
447 else if (strcmp(attr
->name
, "errors") == 0)
448 return pblk_sysfs_stats(pblk
, buf
);
449 else if (strcmp(attr
->name
, "write_buffer") == 0)
450 return pblk_sysfs_write_buffer(pblk
, buf
);
451 else if (strcmp(attr
->name
, "ppa_format") == 0)
452 return pblk_sysfs_ppaf(pblk
, buf
);
453 else if (strcmp(attr
->name
, "lines") == 0)
454 return pblk_sysfs_lines(pblk
, buf
);
455 else if (strcmp(attr
->name
, "lines_info") == 0)
456 return pblk_sysfs_lines_info(pblk
, buf
);
457 else if (strcmp(attr
->name
, "max_sec_per_write") == 0)
458 return pblk_sysfs_get_sec_per_write(pblk
, buf
);
459 #ifdef CONFIG_NVM_DEBUG
460 else if (strcmp(attr
->name
, "stats") == 0)
461 return pblk_sysfs_stats_debug(pblk
, buf
);
466 static ssize_t
pblk_sysfs_store(struct kobject
*kobj
, struct attribute
*attr
,
467 const char *buf
, size_t len
)
469 struct pblk
*pblk
= container_of(kobj
, struct pblk
, kobj
);
471 if (strcmp(attr
->name
, "gc_force") == 0)
472 return pblk_sysfs_gc_force(pblk
, buf
, len
);
473 else if (strcmp(attr
->name
, "max_sec_per_write") == 0)
474 return pblk_sysfs_set_sec_per_write(pblk
, buf
, len
);
479 static const struct sysfs_ops pblk_sysfs_ops
= {
480 .show
= pblk_sysfs_show
,
481 .store
= pblk_sysfs_store
,
484 static struct kobj_type pblk_ktype
= {
485 .sysfs_ops
= &pblk_sysfs_ops
,
486 .default_attrs
= pblk_attrs
,
489 int pblk_sysfs_init(struct gendisk
*tdisk
)
491 struct pblk
*pblk
= tdisk
->private_data
;
492 struct device
*parent_dev
= disk_to_dev(pblk
->disk
);
495 ret
= kobject_init_and_add(&pblk
->kobj
, &pblk_ktype
,
496 kobject_get(&parent_dev
->kobj
),
499 pr_err("pblk: could not register %s/pblk\n",
504 kobject_uevent(&pblk
->kobj
, KOBJ_ADD
);
508 void pblk_sysfs_exit(struct gendisk
*tdisk
)
510 struct pblk
*pblk
= tdisk
->private_data
;
512 kobject_uevent(&pblk
->kobj
, KOBJ_REMOVE
);
513 kobject_del(&pblk
->kobj
);
514 kobject_put(&pblk
->kobj
);