18324aa91SJens Axboe /* 28324aa91SJens Axboe * Functions related to sysfs handling 38324aa91SJens Axboe */ 48324aa91SJens Axboe #include <linux/kernel.h> 58324aa91SJens Axboe #include <linux/module.h> 68324aa91SJens Axboe #include <linux/bio.h> 78324aa91SJens Axboe #include <linux/blkdev.h> 88324aa91SJens Axboe #include <linux/blktrace_api.h> 98324aa91SJens Axboe 108324aa91SJens Axboe #include "blk.h" 118324aa91SJens Axboe 128324aa91SJens Axboe struct queue_sysfs_entry { 138324aa91SJens Axboe struct attribute attr; 148324aa91SJens Axboe ssize_t (*show)(struct request_queue *, char *); 158324aa91SJens Axboe ssize_t (*store)(struct request_queue *, const char *, size_t); 168324aa91SJens Axboe }; 178324aa91SJens Axboe 188324aa91SJens Axboe static ssize_t 198324aa91SJens Axboe queue_var_show(unsigned int var, char *page) 208324aa91SJens Axboe { 218324aa91SJens Axboe return sprintf(page, "%d\n", var); 228324aa91SJens Axboe } 238324aa91SJens Axboe 248324aa91SJens Axboe static ssize_t 258324aa91SJens Axboe queue_var_store(unsigned long *var, const char *page, size_t count) 268324aa91SJens Axboe { 278324aa91SJens Axboe char *p = (char *) page; 288324aa91SJens Axboe 298324aa91SJens Axboe *var = simple_strtoul(p, &p, 10); 308324aa91SJens Axboe return count; 318324aa91SJens Axboe } 328324aa91SJens Axboe 338324aa91SJens Axboe static ssize_t queue_requests_show(struct request_queue *q, char *page) 348324aa91SJens Axboe { 358324aa91SJens Axboe return queue_var_show(q->nr_requests, (page)); 368324aa91SJens Axboe } 378324aa91SJens Axboe 388324aa91SJens Axboe static ssize_t 398324aa91SJens Axboe queue_requests_store(struct request_queue *q, const char *page, size_t count) 408324aa91SJens Axboe { 418324aa91SJens Axboe struct request_list *rl = &q->rq; 428324aa91SJens Axboe unsigned long nr; 438324aa91SJens Axboe int ret = queue_var_store(&nr, page, count); 448324aa91SJens Axboe if (nr < BLKDEV_MIN_RQ) 458324aa91SJens Axboe nr = BLKDEV_MIN_RQ; 468324aa91SJens Axboe 478324aa91SJens Axboe spin_lock_irq(q->queue_lock); 488324aa91SJens Axboe q->nr_requests = nr; 498324aa91SJens Axboe blk_queue_congestion_threshold(q); 508324aa91SJens Axboe 511faa16d2SJens Axboe if (rl->count[BLK_RW_SYNC] >= queue_congestion_on_threshold(q)) 521faa16d2SJens Axboe blk_set_queue_congested(q, BLK_RW_SYNC); 531faa16d2SJens Axboe else if (rl->count[BLK_RW_SYNC] < queue_congestion_off_threshold(q)) 541faa16d2SJens Axboe blk_clear_queue_congested(q, BLK_RW_SYNC); 558324aa91SJens Axboe 561faa16d2SJens Axboe if (rl->count[BLK_RW_ASYNC] >= queue_congestion_on_threshold(q)) 571faa16d2SJens Axboe blk_set_queue_congested(q, BLK_RW_ASYNC); 581faa16d2SJens Axboe else if (rl->count[BLK_RW_ASYNC] < queue_congestion_off_threshold(q)) 591faa16d2SJens Axboe blk_clear_queue_congested(q, BLK_RW_ASYNC); 608324aa91SJens Axboe 611faa16d2SJens Axboe if (rl->count[BLK_RW_SYNC] >= q->nr_requests) { 621faa16d2SJens Axboe blk_set_queue_full(q, BLK_RW_SYNC); 631faa16d2SJens Axboe } else if (rl->count[BLK_RW_SYNC]+1 <= q->nr_requests) { 641faa16d2SJens Axboe blk_clear_queue_full(q, BLK_RW_SYNC); 651faa16d2SJens Axboe wake_up(&rl->wait[BLK_RW_SYNC]); 668324aa91SJens Axboe } 678324aa91SJens Axboe 681faa16d2SJens Axboe if (rl->count[BLK_RW_ASYNC] >= q->nr_requests) { 691faa16d2SJens Axboe blk_set_queue_full(q, BLK_RW_ASYNC); 701faa16d2SJens Axboe } else if (rl->count[BLK_RW_ASYNC]+1 <= q->nr_requests) { 711faa16d2SJens Axboe blk_clear_queue_full(q, BLK_RW_ASYNC); 721faa16d2SJens Axboe wake_up(&rl->wait[BLK_RW_ASYNC]); 738324aa91SJens Axboe } 748324aa91SJens Axboe spin_unlock_irq(q->queue_lock); 758324aa91SJens Axboe return ret; 768324aa91SJens Axboe } 778324aa91SJens Axboe 788324aa91SJens Axboe static ssize_t queue_ra_show(struct request_queue *q, char *page) 798324aa91SJens Axboe { 808324aa91SJens Axboe int ra_kb = q->backing_dev_info.ra_pages << (PAGE_CACHE_SHIFT - 10); 818324aa91SJens Axboe 828324aa91SJens Axboe return queue_var_show(ra_kb, (page)); 838324aa91SJens Axboe } 848324aa91SJens Axboe 858324aa91SJens Axboe static ssize_t 868324aa91SJens Axboe queue_ra_store(struct request_queue *q, const char *page, size_t count) 878324aa91SJens Axboe { 888324aa91SJens Axboe unsigned long ra_kb; 898324aa91SJens Axboe ssize_t ret = queue_var_store(&ra_kb, page, count); 908324aa91SJens Axboe 918324aa91SJens Axboe q->backing_dev_info.ra_pages = ra_kb >> (PAGE_CACHE_SHIFT - 10); 928324aa91SJens Axboe 938324aa91SJens Axboe return ret; 948324aa91SJens Axboe } 958324aa91SJens Axboe 968324aa91SJens Axboe static ssize_t queue_max_sectors_show(struct request_queue *q, char *page) 978324aa91SJens Axboe { 98ae03bf63SMartin K. Petersen int max_sectors_kb = queue_max_sectors(q) >> 1; 998324aa91SJens Axboe 1008324aa91SJens Axboe return queue_var_show(max_sectors_kb, (page)); 1018324aa91SJens Axboe } 1028324aa91SJens Axboe 103e1defc4fSMartin K. Petersen static ssize_t queue_logical_block_size_show(struct request_queue *q, char *page) 104e68b903cSMartin K. Petersen { 105e1defc4fSMartin K. Petersen return queue_var_show(queue_logical_block_size(q), page); 106e68b903cSMartin K. Petersen } 107e68b903cSMartin K. Petersen 108c72758f3SMartin K. Petersen static ssize_t queue_physical_block_size_show(struct request_queue *q, char *page) 109c72758f3SMartin K. Petersen { 110c72758f3SMartin K. Petersen return queue_var_show(queue_physical_block_size(q), page); 111c72758f3SMartin K. Petersen } 112c72758f3SMartin K. Petersen 113c72758f3SMartin K. Petersen static ssize_t queue_io_min_show(struct request_queue *q, char *page) 114c72758f3SMartin K. Petersen { 115c72758f3SMartin K. Petersen return queue_var_show(queue_io_min(q), page); 116c72758f3SMartin K. Petersen } 117c72758f3SMartin K. Petersen 118c72758f3SMartin K. Petersen static ssize_t queue_io_opt_show(struct request_queue *q, char *page) 119c72758f3SMartin K. Petersen { 120c72758f3SMartin K. Petersen return queue_var_show(queue_io_opt(q), page); 1218324aa91SJens Axboe } 1228324aa91SJens Axboe 1238324aa91SJens Axboe static ssize_t 1248324aa91SJens Axboe queue_max_sectors_store(struct request_queue *q, const char *page, size_t count) 1258324aa91SJens Axboe { 1268324aa91SJens Axboe unsigned long max_sectors_kb, 127ae03bf63SMartin K. Petersen max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1, 1288324aa91SJens Axboe page_kb = 1 << (PAGE_CACHE_SHIFT - 10); 1298324aa91SJens Axboe ssize_t ret = queue_var_store(&max_sectors_kb, page, count); 1308324aa91SJens Axboe 1318324aa91SJens Axboe if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb) 1328324aa91SJens Axboe return -EINVAL; 1337c239517SWu Fengguang 1348324aa91SJens Axboe spin_lock_irq(q->queue_lock); 135ae03bf63SMartin K. Petersen blk_queue_max_sectors(q, max_sectors_kb << 1); 1368324aa91SJens Axboe spin_unlock_irq(q->queue_lock); 1378324aa91SJens Axboe 1388324aa91SJens Axboe return ret; 1398324aa91SJens Axboe } 1408324aa91SJens Axboe 1418324aa91SJens Axboe static ssize_t queue_max_hw_sectors_show(struct request_queue *q, char *page) 1428324aa91SJens Axboe { 143ae03bf63SMartin K. Petersen int max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1; 1448324aa91SJens Axboe 1458324aa91SJens Axboe return queue_var_show(max_hw_sectors_kb, (page)); 1468324aa91SJens Axboe } 1478324aa91SJens Axboe 1481308835fSBartlomiej Zolnierkiewicz static ssize_t queue_nonrot_show(struct request_queue *q, char *page) 1491308835fSBartlomiej Zolnierkiewicz { 1501308835fSBartlomiej Zolnierkiewicz return queue_var_show(!blk_queue_nonrot(q), page); 1511308835fSBartlomiej Zolnierkiewicz } 1521308835fSBartlomiej Zolnierkiewicz 1531308835fSBartlomiej Zolnierkiewicz static ssize_t queue_nonrot_store(struct request_queue *q, const char *page, 1541308835fSBartlomiej Zolnierkiewicz size_t count) 1551308835fSBartlomiej Zolnierkiewicz { 1561308835fSBartlomiej Zolnierkiewicz unsigned long nm; 1571308835fSBartlomiej Zolnierkiewicz ssize_t ret = queue_var_store(&nm, page, count); 1581308835fSBartlomiej Zolnierkiewicz 1591308835fSBartlomiej Zolnierkiewicz spin_lock_irq(q->queue_lock); 1601308835fSBartlomiej Zolnierkiewicz if (nm) 1611308835fSBartlomiej Zolnierkiewicz queue_flag_clear(QUEUE_FLAG_NONROT, q); 1621308835fSBartlomiej Zolnierkiewicz else 1631308835fSBartlomiej Zolnierkiewicz queue_flag_set(QUEUE_FLAG_NONROT, q); 1641308835fSBartlomiej Zolnierkiewicz spin_unlock_irq(q->queue_lock); 1651308835fSBartlomiej Zolnierkiewicz 1661308835fSBartlomiej Zolnierkiewicz return ret; 1671308835fSBartlomiej Zolnierkiewicz } 1681308835fSBartlomiej Zolnierkiewicz 169ac9fafa1SAlan D. Brunelle static ssize_t queue_nomerges_show(struct request_queue *q, char *page) 170ac9fafa1SAlan D. Brunelle { 171ac9fafa1SAlan D. Brunelle return queue_var_show(blk_queue_nomerges(q), page); 172ac9fafa1SAlan D. Brunelle } 173ac9fafa1SAlan D. Brunelle 174ac9fafa1SAlan D. Brunelle static ssize_t queue_nomerges_store(struct request_queue *q, const char *page, 175ac9fafa1SAlan D. Brunelle size_t count) 176ac9fafa1SAlan D. Brunelle { 177ac9fafa1SAlan D. Brunelle unsigned long nm; 178ac9fafa1SAlan D. Brunelle ssize_t ret = queue_var_store(&nm, page, count); 179ac9fafa1SAlan D. Brunelle 180bf0f9702SJens Axboe spin_lock_irq(q->queue_lock); 181ac9fafa1SAlan D. Brunelle if (nm) 182bf0f9702SJens Axboe queue_flag_set(QUEUE_FLAG_NOMERGES, q); 183ac9fafa1SAlan D. Brunelle else 184bf0f9702SJens Axboe queue_flag_clear(QUEUE_FLAG_NOMERGES, q); 185bf0f9702SJens Axboe spin_unlock_irq(q->queue_lock); 1861308835fSBartlomiej Zolnierkiewicz 187ac9fafa1SAlan D. Brunelle return ret; 188ac9fafa1SAlan D. Brunelle } 189ac9fafa1SAlan D. Brunelle 190c7c22e4dSJens Axboe static ssize_t queue_rq_affinity_show(struct request_queue *q, char *page) 191c7c22e4dSJens Axboe { 192c7c22e4dSJens Axboe unsigned int set = test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags); 193c7c22e4dSJens Axboe 194c7c22e4dSJens Axboe return queue_var_show(set != 0, page); 195c7c22e4dSJens Axboe } 196c7c22e4dSJens Axboe 197c7c22e4dSJens Axboe static ssize_t 198c7c22e4dSJens Axboe queue_rq_affinity_store(struct request_queue *q, const char *page, size_t count) 199c7c22e4dSJens Axboe { 200c7c22e4dSJens Axboe ssize_t ret = -EINVAL; 201c7c22e4dSJens Axboe #if defined(CONFIG_USE_GENERIC_SMP_HELPERS) 202c7c22e4dSJens Axboe unsigned long val; 203c7c22e4dSJens Axboe 204c7c22e4dSJens Axboe ret = queue_var_store(&val, page, count); 205c7c22e4dSJens Axboe spin_lock_irq(q->queue_lock); 206c7c22e4dSJens Axboe if (val) 207c7c22e4dSJens Axboe queue_flag_set(QUEUE_FLAG_SAME_COMP, q); 208c7c22e4dSJens Axboe else 209c7c22e4dSJens Axboe queue_flag_clear(QUEUE_FLAG_SAME_COMP, q); 210c7c22e4dSJens Axboe spin_unlock_irq(q->queue_lock); 211c7c22e4dSJens Axboe #endif 212c7c22e4dSJens Axboe return ret; 213c7c22e4dSJens Axboe } 2148324aa91SJens Axboe 215bc58ba94SJens Axboe static ssize_t queue_iostats_show(struct request_queue *q, char *page) 216bc58ba94SJens Axboe { 217bc58ba94SJens Axboe return queue_var_show(blk_queue_io_stat(q), page); 218bc58ba94SJens Axboe } 219bc58ba94SJens Axboe 220bc58ba94SJens Axboe static ssize_t queue_iostats_store(struct request_queue *q, const char *page, 221bc58ba94SJens Axboe size_t count) 222bc58ba94SJens Axboe { 223bc58ba94SJens Axboe unsigned long stats; 224bc58ba94SJens Axboe ssize_t ret = queue_var_store(&stats, page, count); 225bc58ba94SJens Axboe 226bc58ba94SJens Axboe spin_lock_irq(q->queue_lock); 227bc58ba94SJens Axboe if (stats) 228bc58ba94SJens Axboe queue_flag_set(QUEUE_FLAG_IO_STAT, q); 229bc58ba94SJens Axboe else 230bc58ba94SJens Axboe queue_flag_clear(QUEUE_FLAG_IO_STAT, q); 231bc58ba94SJens Axboe spin_unlock_irq(q->queue_lock); 232bc58ba94SJens Axboe 233bc58ba94SJens Axboe return ret; 234bc58ba94SJens Axboe } 235bc58ba94SJens Axboe 2368324aa91SJens Axboe static struct queue_sysfs_entry queue_requests_entry = { 2378324aa91SJens Axboe .attr = {.name = "nr_requests", .mode = S_IRUGO | S_IWUSR }, 2388324aa91SJens Axboe .show = queue_requests_show, 2398324aa91SJens Axboe .store = queue_requests_store, 2408324aa91SJens Axboe }; 2418324aa91SJens Axboe 2428324aa91SJens Axboe static struct queue_sysfs_entry queue_ra_entry = { 2438324aa91SJens Axboe .attr = {.name = "read_ahead_kb", .mode = S_IRUGO | S_IWUSR }, 2448324aa91SJens Axboe .show = queue_ra_show, 2458324aa91SJens Axboe .store = queue_ra_store, 2468324aa91SJens Axboe }; 2478324aa91SJens Axboe 2488324aa91SJens Axboe static struct queue_sysfs_entry queue_max_sectors_entry = { 2498324aa91SJens Axboe .attr = {.name = "max_sectors_kb", .mode = S_IRUGO | S_IWUSR }, 2508324aa91SJens Axboe .show = queue_max_sectors_show, 2518324aa91SJens Axboe .store = queue_max_sectors_store, 2528324aa91SJens Axboe }; 2538324aa91SJens Axboe 2548324aa91SJens Axboe static struct queue_sysfs_entry queue_max_hw_sectors_entry = { 2558324aa91SJens Axboe .attr = {.name = "max_hw_sectors_kb", .mode = S_IRUGO }, 2568324aa91SJens Axboe .show = queue_max_hw_sectors_show, 2578324aa91SJens Axboe }; 2588324aa91SJens Axboe 2598324aa91SJens Axboe static struct queue_sysfs_entry queue_iosched_entry = { 2608324aa91SJens Axboe .attr = {.name = "scheduler", .mode = S_IRUGO | S_IWUSR }, 2618324aa91SJens Axboe .show = elv_iosched_show, 2628324aa91SJens Axboe .store = elv_iosched_store, 2638324aa91SJens Axboe }; 2648324aa91SJens Axboe 265e68b903cSMartin K. Petersen static struct queue_sysfs_entry queue_hw_sector_size_entry = { 266e68b903cSMartin K. Petersen .attr = {.name = "hw_sector_size", .mode = S_IRUGO }, 267e1defc4fSMartin K. Petersen .show = queue_logical_block_size_show, 268e1defc4fSMartin K. Petersen }; 269e1defc4fSMartin K. Petersen 270e1defc4fSMartin K. Petersen static struct queue_sysfs_entry queue_logical_block_size_entry = { 271e1defc4fSMartin K. Petersen .attr = {.name = "logical_block_size", .mode = S_IRUGO }, 272e1defc4fSMartin K. Petersen .show = queue_logical_block_size_show, 273e68b903cSMartin K. Petersen }; 274e68b903cSMartin K. Petersen 275c72758f3SMartin K. Petersen static struct queue_sysfs_entry queue_physical_block_size_entry = { 276c72758f3SMartin K. Petersen .attr = {.name = "physical_block_size", .mode = S_IRUGO }, 277c72758f3SMartin K. Petersen .show = queue_physical_block_size_show, 278c72758f3SMartin K. Petersen }; 279c72758f3SMartin K. Petersen 280c72758f3SMartin K. Petersen static struct queue_sysfs_entry queue_io_min_entry = { 281c72758f3SMartin K. Petersen .attr = {.name = "minimum_io_size", .mode = S_IRUGO }, 282c72758f3SMartin K. Petersen .show = queue_io_min_show, 283c72758f3SMartin K. Petersen }; 284c72758f3SMartin K. Petersen 285c72758f3SMartin K. Petersen static struct queue_sysfs_entry queue_io_opt_entry = { 286c72758f3SMartin K. Petersen .attr = {.name = "optimal_io_size", .mode = S_IRUGO }, 287c72758f3SMartin K. Petersen .show = queue_io_opt_show, 2888324aa91SJens Axboe }; 2898324aa91SJens Axboe 2901308835fSBartlomiej Zolnierkiewicz static struct queue_sysfs_entry queue_nonrot_entry = { 2911308835fSBartlomiej Zolnierkiewicz .attr = {.name = "rotational", .mode = S_IRUGO | S_IWUSR }, 2921308835fSBartlomiej Zolnierkiewicz .show = queue_nonrot_show, 2931308835fSBartlomiej Zolnierkiewicz .store = queue_nonrot_store, 2941308835fSBartlomiej Zolnierkiewicz }; 2951308835fSBartlomiej Zolnierkiewicz 296ac9fafa1SAlan D. Brunelle static struct queue_sysfs_entry queue_nomerges_entry = { 297ac9fafa1SAlan D. Brunelle .attr = {.name = "nomerges", .mode = S_IRUGO | S_IWUSR }, 298ac9fafa1SAlan D. Brunelle .show = queue_nomerges_show, 299ac9fafa1SAlan D. Brunelle .store = queue_nomerges_store, 300ac9fafa1SAlan D. Brunelle }; 301ac9fafa1SAlan D. Brunelle 302c7c22e4dSJens Axboe static struct queue_sysfs_entry queue_rq_affinity_entry = { 303c7c22e4dSJens Axboe .attr = {.name = "rq_affinity", .mode = S_IRUGO | S_IWUSR }, 304c7c22e4dSJens Axboe .show = queue_rq_affinity_show, 305c7c22e4dSJens Axboe .store = queue_rq_affinity_store, 306c7c22e4dSJens Axboe }; 307c7c22e4dSJens Axboe 308bc58ba94SJens Axboe static struct queue_sysfs_entry queue_iostats_entry = { 309bc58ba94SJens Axboe .attr = {.name = "iostats", .mode = S_IRUGO | S_IWUSR }, 310bc58ba94SJens Axboe .show = queue_iostats_show, 311bc58ba94SJens Axboe .store = queue_iostats_store, 312bc58ba94SJens Axboe }; 313bc58ba94SJens Axboe 3148324aa91SJens Axboe static struct attribute *default_attrs[] = { 3158324aa91SJens Axboe &queue_requests_entry.attr, 3168324aa91SJens Axboe &queue_ra_entry.attr, 3178324aa91SJens Axboe &queue_max_hw_sectors_entry.attr, 3188324aa91SJens Axboe &queue_max_sectors_entry.attr, 3198324aa91SJens Axboe &queue_iosched_entry.attr, 320e68b903cSMartin K. Petersen &queue_hw_sector_size_entry.attr, 321e1defc4fSMartin K. Petersen &queue_logical_block_size_entry.attr, 322c72758f3SMartin K. Petersen &queue_physical_block_size_entry.attr, 323c72758f3SMartin K. Petersen &queue_io_min_entry.attr, 324c72758f3SMartin K. Petersen &queue_io_opt_entry.attr, 3251308835fSBartlomiej Zolnierkiewicz &queue_nonrot_entry.attr, 326ac9fafa1SAlan D. Brunelle &queue_nomerges_entry.attr, 327c7c22e4dSJens Axboe &queue_rq_affinity_entry.attr, 328bc58ba94SJens Axboe &queue_iostats_entry.attr, 3298324aa91SJens Axboe NULL, 3308324aa91SJens Axboe }; 3318324aa91SJens Axboe 3328324aa91SJens Axboe #define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr) 3338324aa91SJens Axboe 3348324aa91SJens Axboe static ssize_t 3358324aa91SJens Axboe queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page) 3368324aa91SJens Axboe { 3378324aa91SJens Axboe struct queue_sysfs_entry *entry = to_queue(attr); 3388324aa91SJens Axboe struct request_queue *q = 3398324aa91SJens Axboe container_of(kobj, struct request_queue, kobj); 3408324aa91SJens Axboe ssize_t res; 3418324aa91SJens Axboe 3428324aa91SJens Axboe if (!entry->show) 3438324aa91SJens Axboe return -EIO; 3448324aa91SJens Axboe mutex_lock(&q->sysfs_lock); 3458324aa91SJens Axboe if (test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)) { 3468324aa91SJens Axboe mutex_unlock(&q->sysfs_lock); 3478324aa91SJens Axboe return -ENOENT; 3488324aa91SJens Axboe } 3498324aa91SJens Axboe res = entry->show(q, page); 3508324aa91SJens Axboe mutex_unlock(&q->sysfs_lock); 3518324aa91SJens Axboe return res; 3528324aa91SJens Axboe } 3538324aa91SJens Axboe 3548324aa91SJens Axboe static ssize_t 3558324aa91SJens Axboe queue_attr_store(struct kobject *kobj, struct attribute *attr, 3568324aa91SJens Axboe const char *page, size_t length) 3578324aa91SJens Axboe { 3588324aa91SJens Axboe struct queue_sysfs_entry *entry = to_queue(attr); 3596728cb0eSJens Axboe struct request_queue *q; 3608324aa91SJens Axboe ssize_t res; 3618324aa91SJens Axboe 3628324aa91SJens Axboe if (!entry->store) 3638324aa91SJens Axboe return -EIO; 3646728cb0eSJens Axboe 3656728cb0eSJens Axboe q = container_of(kobj, struct request_queue, kobj); 3668324aa91SJens Axboe mutex_lock(&q->sysfs_lock); 3678324aa91SJens Axboe if (test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)) { 3688324aa91SJens Axboe mutex_unlock(&q->sysfs_lock); 3698324aa91SJens Axboe return -ENOENT; 3708324aa91SJens Axboe } 3718324aa91SJens Axboe res = entry->store(q, page, length); 3728324aa91SJens Axboe mutex_unlock(&q->sysfs_lock); 3738324aa91SJens Axboe return res; 3748324aa91SJens Axboe } 3758324aa91SJens Axboe 3768324aa91SJens Axboe /** 3778324aa91SJens Axboe * blk_cleanup_queue: - release a &struct request_queue when it is no longer needed 3788324aa91SJens Axboe * @kobj: the kobj belonging of the request queue to be released 3798324aa91SJens Axboe * 3808324aa91SJens Axboe * Description: 3818324aa91SJens Axboe * blk_cleanup_queue is the pair to blk_init_queue() or 3828324aa91SJens Axboe * blk_queue_make_request(). It should be called when a request queue is 3838324aa91SJens Axboe * being released; typically when a block device is being de-registered. 3848324aa91SJens Axboe * Currently, its primary task it to free all the &struct request 3858324aa91SJens Axboe * structures that were allocated to the queue and the queue itself. 3868324aa91SJens Axboe * 3878324aa91SJens Axboe * Caveat: 3888324aa91SJens Axboe * Hopefully the low level driver will have finished any 3898324aa91SJens Axboe * outstanding requests first... 3908324aa91SJens Axboe **/ 3918324aa91SJens Axboe static void blk_release_queue(struct kobject *kobj) 3928324aa91SJens Axboe { 3938324aa91SJens Axboe struct request_queue *q = 3948324aa91SJens Axboe container_of(kobj, struct request_queue, kobj); 3958324aa91SJens Axboe struct request_list *rl = &q->rq; 3968324aa91SJens Axboe 3978324aa91SJens Axboe blk_sync_queue(q); 3988324aa91SJens Axboe 3998324aa91SJens Axboe if (rl->rq_pool) 4008324aa91SJens Axboe mempool_destroy(rl->rq_pool); 4018324aa91SJens Axboe 4028324aa91SJens Axboe if (q->queue_tags) 4038324aa91SJens Axboe __blk_queue_free_tags(q); 4048324aa91SJens Axboe 4058324aa91SJens Axboe blk_trace_shutdown(q); 4068324aa91SJens Axboe 4078324aa91SJens Axboe bdi_destroy(&q->backing_dev_info); 4088324aa91SJens Axboe kmem_cache_free(blk_requestq_cachep, q); 4098324aa91SJens Axboe } 4108324aa91SJens Axboe 4118324aa91SJens Axboe static struct sysfs_ops queue_sysfs_ops = { 4128324aa91SJens Axboe .show = queue_attr_show, 4138324aa91SJens Axboe .store = queue_attr_store, 4148324aa91SJens Axboe }; 4158324aa91SJens Axboe 4168324aa91SJens Axboe struct kobj_type blk_queue_ktype = { 4178324aa91SJens Axboe .sysfs_ops = &queue_sysfs_ops, 4188324aa91SJens Axboe .default_attrs = default_attrs, 4198324aa91SJens Axboe .release = blk_release_queue, 4208324aa91SJens Axboe }; 4218324aa91SJens Axboe 4228324aa91SJens Axboe int blk_register_queue(struct gendisk *disk) 4238324aa91SJens Axboe { 4248324aa91SJens Axboe int ret; 4251d54ad6dSLi Zefan struct device *dev = disk_to_dev(disk); 4268324aa91SJens Axboe 4278324aa91SJens Axboe struct request_queue *q = disk->queue; 4288324aa91SJens Axboe 429fb199746SAkinobu Mita if (WARN_ON(!q)) 4308324aa91SJens Axboe return -ENXIO; 4318324aa91SJens Axboe 4321d54ad6dSLi Zefan ret = blk_trace_init_sysfs(dev); 4331d54ad6dSLi Zefan if (ret) 4341d54ad6dSLi Zefan return ret; 4351d54ad6dSLi Zefan 436*c9059598SLinus Torvalds ret = kobject_add(&q->kobj, kobject_get(&dev->kobj), "%s", "queue"); 4378324aa91SJens Axboe if (ret < 0) 4388324aa91SJens Axboe return ret; 4398324aa91SJens Axboe 4408324aa91SJens Axboe kobject_uevent(&q->kobj, KOBJ_ADD); 4418324aa91SJens Axboe 442cd43e26fSMartin K. Petersen if (!q->request_fn) 443cd43e26fSMartin K. Petersen return 0; 444cd43e26fSMartin K. Petersen 4458324aa91SJens Axboe ret = elv_register_queue(q); 4468324aa91SJens Axboe if (ret) { 4478324aa91SJens Axboe kobject_uevent(&q->kobj, KOBJ_REMOVE); 4488324aa91SJens Axboe kobject_del(&q->kobj); 4498324aa91SJens Axboe return ret; 4508324aa91SJens Axboe } 4518324aa91SJens Axboe 4528324aa91SJens Axboe return 0; 4538324aa91SJens Axboe } 4548324aa91SJens Axboe 4558324aa91SJens Axboe void blk_unregister_queue(struct gendisk *disk) 4568324aa91SJens Axboe { 4578324aa91SJens Axboe struct request_queue *q = disk->queue; 4588324aa91SJens Axboe 459fb199746SAkinobu Mita if (WARN_ON(!q)) 460fb199746SAkinobu Mita return; 461fb199746SAkinobu Mita 462fb199746SAkinobu Mita if (q->request_fn) { 4638324aa91SJens Axboe elv_unregister_queue(q); 4648324aa91SJens Axboe 4658324aa91SJens Axboe kobject_uevent(&q->kobj, KOBJ_REMOVE); 4668324aa91SJens Axboe kobject_del(&q->kobj); 467ed9e1982STejun Heo kobject_put(&disk_to_dev(disk)->kobj); 4688324aa91SJens Axboe } 4698324aa91SJens Axboe } 470