18324aa91SJens Axboe /* 28324aa91SJens Axboe * Functions related to sysfs handling 38324aa91SJens Axboe */ 48324aa91SJens Axboe #include <linux/kernel.h> 55a0e3ad6STejun Heo #include <linux/slab.h> 68324aa91SJens Axboe #include <linux/module.h> 78324aa91SJens Axboe #include <linux/bio.h> 88324aa91SJens Axboe #include <linux/blkdev.h> 98324aa91SJens Axboe #include <linux/blktrace_api.h> 108324aa91SJens Axboe 118324aa91SJens Axboe #include "blk.h" 128324aa91SJens Axboe 138324aa91SJens Axboe struct queue_sysfs_entry { 148324aa91SJens Axboe struct attribute attr; 158324aa91SJens Axboe ssize_t (*show)(struct request_queue *, char *); 168324aa91SJens Axboe ssize_t (*store)(struct request_queue *, const char *, size_t); 178324aa91SJens Axboe }; 188324aa91SJens Axboe 198324aa91SJens Axboe static ssize_t 209cb308ceSXiaotian Feng queue_var_show(unsigned long var, char *page) 218324aa91SJens Axboe { 229cb308ceSXiaotian Feng return sprintf(page, "%lu\n", var); 238324aa91SJens Axboe } 248324aa91SJens Axboe 258324aa91SJens Axboe static ssize_t 268324aa91SJens Axboe queue_var_store(unsigned long *var, const char *page, size_t count) 278324aa91SJens Axboe { 288324aa91SJens Axboe char *p = (char *) page; 298324aa91SJens Axboe 308324aa91SJens Axboe *var = simple_strtoul(p, &p, 10); 318324aa91SJens Axboe return count; 328324aa91SJens Axboe } 338324aa91SJens Axboe 348324aa91SJens Axboe static ssize_t queue_requests_show(struct request_queue *q, char *page) 358324aa91SJens Axboe { 368324aa91SJens Axboe return queue_var_show(q->nr_requests, (page)); 378324aa91SJens Axboe } 388324aa91SJens Axboe 398324aa91SJens Axboe static ssize_t 408324aa91SJens Axboe queue_requests_store(struct request_queue *q, const char *page, size_t count) 418324aa91SJens Axboe { 428324aa91SJens Axboe struct request_list *rl = &q->rq; 438324aa91SJens Axboe unsigned long nr; 44b8a9ae77SJens Axboe int ret; 45b8a9ae77SJens Axboe 46b8a9ae77SJens Axboe if (!q->request_fn) 47b8a9ae77SJens Axboe return -EINVAL; 48b8a9ae77SJens Axboe 49b8a9ae77SJens Axboe ret = queue_var_store(&nr, page, count); 508324aa91SJens Axboe if (nr < BLKDEV_MIN_RQ) 518324aa91SJens Axboe nr = BLKDEV_MIN_RQ; 528324aa91SJens Axboe 538324aa91SJens Axboe spin_lock_irq(q->queue_lock); 548324aa91SJens Axboe q->nr_requests = nr; 558324aa91SJens Axboe blk_queue_congestion_threshold(q); 568324aa91SJens Axboe 571faa16d2SJens Axboe if (rl->count[BLK_RW_SYNC] >= queue_congestion_on_threshold(q)) 581faa16d2SJens Axboe blk_set_queue_congested(q, BLK_RW_SYNC); 591faa16d2SJens Axboe else if (rl->count[BLK_RW_SYNC] < queue_congestion_off_threshold(q)) 601faa16d2SJens Axboe blk_clear_queue_congested(q, BLK_RW_SYNC); 618324aa91SJens Axboe 621faa16d2SJens Axboe if (rl->count[BLK_RW_ASYNC] >= queue_congestion_on_threshold(q)) 631faa16d2SJens Axboe blk_set_queue_congested(q, BLK_RW_ASYNC); 641faa16d2SJens Axboe else if (rl->count[BLK_RW_ASYNC] < queue_congestion_off_threshold(q)) 651faa16d2SJens Axboe blk_clear_queue_congested(q, BLK_RW_ASYNC); 668324aa91SJens Axboe 671faa16d2SJens Axboe if (rl->count[BLK_RW_SYNC] >= q->nr_requests) { 681faa16d2SJens Axboe blk_set_queue_full(q, BLK_RW_SYNC); 691faa16d2SJens Axboe } else if (rl->count[BLK_RW_SYNC]+1 <= q->nr_requests) { 701faa16d2SJens Axboe blk_clear_queue_full(q, BLK_RW_SYNC); 711faa16d2SJens Axboe wake_up(&rl->wait[BLK_RW_SYNC]); 728324aa91SJens Axboe } 738324aa91SJens Axboe 741faa16d2SJens Axboe if (rl->count[BLK_RW_ASYNC] >= q->nr_requests) { 751faa16d2SJens Axboe blk_set_queue_full(q, BLK_RW_ASYNC); 761faa16d2SJens Axboe } else if (rl->count[BLK_RW_ASYNC]+1 <= q->nr_requests) { 771faa16d2SJens Axboe blk_clear_queue_full(q, BLK_RW_ASYNC); 781faa16d2SJens Axboe wake_up(&rl->wait[BLK_RW_ASYNC]); 798324aa91SJens Axboe } 808324aa91SJens Axboe spin_unlock_irq(q->queue_lock); 818324aa91SJens Axboe return ret; 828324aa91SJens Axboe } 838324aa91SJens Axboe 848324aa91SJens Axboe static ssize_t queue_ra_show(struct request_queue *q, char *page) 858324aa91SJens Axboe { 869cb308ceSXiaotian Feng unsigned long ra_kb = q->backing_dev_info.ra_pages << 879cb308ceSXiaotian Feng (PAGE_CACHE_SHIFT - 10); 888324aa91SJens Axboe 898324aa91SJens Axboe return queue_var_show(ra_kb, (page)); 908324aa91SJens Axboe } 918324aa91SJens Axboe 928324aa91SJens Axboe static ssize_t 938324aa91SJens Axboe queue_ra_store(struct request_queue *q, const char *page, size_t count) 948324aa91SJens Axboe { 958324aa91SJens Axboe unsigned long ra_kb; 968324aa91SJens Axboe ssize_t ret = queue_var_store(&ra_kb, page, count); 978324aa91SJens Axboe 988324aa91SJens Axboe q->backing_dev_info.ra_pages = ra_kb >> (PAGE_CACHE_SHIFT - 10); 998324aa91SJens Axboe 1008324aa91SJens Axboe return ret; 1018324aa91SJens Axboe } 1028324aa91SJens Axboe 1038324aa91SJens Axboe static ssize_t queue_max_sectors_show(struct request_queue *q, char *page) 1048324aa91SJens Axboe { 105ae03bf63SMartin K. Petersen int max_sectors_kb = queue_max_sectors(q) >> 1; 1068324aa91SJens Axboe 1078324aa91SJens Axboe return queue_var_show(max_sectors_kb, (page)); 1088324aa91SJens Axboe } 1098324aa91SJens Axboe 110c77a5710SMartin K. Petersen static ssize_t queue_max_segments_show(struct request_queue *q, char *page) 111c77a5710SMartin K. Petersen { 112c77a5710SMartin K. Petersen return queue_var_show(queue_max_segments(q), (page)); 113c77a5710SMartin K. Petersen } 114c77a5710SMartin K. Petersen 115c77a5710SMartin K. Petersen static ssize_t queue_max_segment_size_show(struct request_queue *q, char *page) 116c77a5710SMartin K. Petersen { 117c77a5710SMartin K. Petersen if (test_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags)) 118c77a5710SMartin K. Petersen return queue_var_show(queue_max_segment_size(q), (page)); 119c77a5710SMartin K. Petersen 120c77a5710SMartin K. Petersen return queue_var_show(PAGE_CACHE_SIZE, (page)); 121c77a5710SMartin K. Petersen } 122c77a5710SMartin K. Petersen 123e1defc4fSMartin K. Petersen static ssize_t queue_logical_block_size_show(struct request_queue *q, char *page) 124e68b903cSMartin K. Petersen { 125e1defc4fSMartin K. Petersen return queue_var_show(queue_logical_block_size(q), page); 126e68b903cSMartin K. Petersen } 127e68b903cSMartin K. Petersen 128c72758f3SMartin K. Petersen static ssize_t queue_physical_block_size_show(struct request_queue *q, char *page) 129c72758f3SMartin K. Petersen { 130c72758f3SMartin K. Petersen return queue_var_show(queue_physical_block_size(q), page); 131c72758f3SMartin K. Petersen } 132c72758f3SMartin K. Petersen 133c72758f3SMartin K. Petersen static ssize_t queue_io_min_show(struct request_queue *q, char *page) 134c72758f3SMartin K. Petersen { 135c72758f3SMartin K. Petersen return queue_var_show(queue_io_min(q), page); 136c72758f3SMartin K. Petersen } 137c72758f3SMartin K. Petersen 138c72758f3SMartin K. Petersen static ssize_t queue_io_opt_show(struct request_queue *q, char *page) 139c72758f3SMartin K. Petersen { 140c72758f3SMartin K. Petersen return queue_var_show(queue_io_opt(q), page); 1418324aa91SJens Axboe } 1428324aa91SJens Axboe 14386b37281SMartin K. Petersen static ssize_t queue_discard_granularity_show(struct request_queue *q, char *page) 14486b37281SMartin K. Petersen { 14586b37281SMartin K. Petersen return queue_var_show(q->limits.discard_granularity, page); 14686b37281SMartin K. Petersen } 14786b37281SMartin K. Petersen 14886b37281SMartin K. Petersen static ssize_t queue_discard_max_show(struct request_queue *q, char *page) 14986b37281SMartin K. Petersen { 15086b37281SMartin K. Petersen return queue_var_show(q->limits.max_discard_sectors << 9, page); 15186b37281SMartin K. Petersen } 15286b37281SMartin K. Petersen 15398262f27SMartin K. Petersen static ssize_t queue_discard_zeroes_data_show(struct request_queue *q, char *page) 15498262f27SMartin K. Petersen { 15598262f27SMartin K. Petersen return queue_var_show(queue_discard_zeroes_data(q), page); 15698262f27SMartin K. Petersen } 15798262f27SMartin K. Petersen 1588324aa91SJens Axboe static ssize_t 1598324aa91SJens Axboe queue_max_sectors_store(struct request_queue *q, const char *page, size_t count) 1608324aa91SJens Axboe { 1618324aa91SJens Axboe unsigned long max_sectors_kb, 162ae03bf63SMartin K. Petersen max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1, 1638324aa91SJens Axboe page_kb = 1 << (PAGE_CACHE_SHIFT - 10); 1648324aa91SJens Axboe ssize_t ret = queue_var_store(&max_sectors_kb, page, count); 1658324aa91SJens Axboe 1668324aa91SJens Axboe if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb) 1678324aa91SJens Axboe return -EINVAL; 1687c239517SWu Fengguang 1698324aa91SJens Axboe spin_lock_irq(q->queue_lock); 170c295fc05SNikanth Karthikesan q->limits.max_sectors = max_sectors_kb << 1; 1718324aa91SJens Axboe spin_unlock_irq(q->queue_lock); 1728324aa91SJens Axboe 1738324aa91SJens Axboe return ret; 1748324aa91SJens Axboe } 1758324aa91SJens Axboe 1768324aa91SJens Axboe static ssize_t queue_max_hw_sectors_show(struct request_queue *q, char *page) 1778324aa91SJens Axboe { 178ae03bf63SMartin K. Petersen int max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1; 1798324aa91SJens Axboe 1808324aa91SJens Axboe return queue_var_show(max_hw_sectors_kb, (page)); 1818324aa91SJens Axboe } 1828324aa91SJens Axboe 183*956bcb7cSJens Axboe #define QUEUE_SYSFS_BIT_FNS(name, flag, neg) \ 184*956bcb7cSJens Axboe static ssize_t \ 185*956bcb7cSJens Axboe queue_show_##name(struct request_queue *q, char *page) \ 186*956bcb7cSJens Axboe { \ 187*956bcb7cSJens Axboe int bit; \ 188*956bcb7cSJens Axboe bit = test_bit(QUEUE_FLAG_##flag, &q->queue_flags); \ 189*956bcb7cSJens Axboe return queue_var_show(neg ? !bit : bit, page); \ 190*956bcb7cSJens Axboe } \ 191*956bcb7cSJens Axboe static ssize_t \ 192*956bcb7cSJens Axboe queue_store_##name(struct request_queue *q, const char *page, size_t count) \ 193*956bcb7cSJens Axboe { \ 194*956bcb7cSJens Axboe unsigned long val; \ 195*956bcb7cSJens Axboe ssize_t ret; \ 196*956bcb7cSJens Axboe ret = queue_var_store(&val, page, count); \ 197*956bcb7cSJens Axboe if (neg) \ 198*956bcb7cSJens Axboe val = !val; \ 199*956bcb7cSJens Axboe \ 200*956bcb7cSJens Axboe spin_lock_irq(q->queue_lock); \ 201*956bcb7cSJens Axboe if (val) \ 202*956bcb7cSJens Axboe queue_flag_set(QUEUE_FLAG_##flag, q); \ 203*956bcb7cSJens Axboe else \ 204*956bcb7cSJens Axboe queue_flag_clear(QUEUE_FLAG_##flag, q); \ 205*956bcb7cSJens Axboe spin_unlock_irq(q->queue_lock); \ 206*956bcb7cSJens Axboe return ret; \ 2071308835fSBartlomiej Zolnierkiewicz } 2081308835fSBartlomiej Zolnierkiewicz 209*956bcb7cSJens Axboe QUEUE_SYSFS_BIT_FNS(nonrot, NONROT, 1); 210*956bcb7cSJens Axboe QUEUE_SYSFS_BIT_FNS(random, ADD_RANDOM, 0); 211*956bcb7cSJens Axboe QUEUE_SYSFS_BIT_FNS(iostats, IO_STAT, 0); 212*956bcb7cSJens Axboe #undef QUEUE_SYSFS_BIT_FNS 2131308835fSBartlomiej Zolnierkiewicz 214ac9fafa1SAlan D. Brunelle static ssize_t queue_nomerges_show(struct request_queue *q, char *page) 215ac9fafa1SAlan D. Brunelle { 216488991e2SAlan D. Brunelle return queue_var_show((blk_queue_nomerges(q) << 1) | 217488991e2SAlan D. Brunelle blk_queue_noxmerges(q), page); 218ac9fafa1SAlan D. Brunelle } 219ac9fafa1SAlan D. Brunelle 220ac9fafa1SAlan D. Brunelle static ssize_t queue_nomerges_store(struct request_queue *q, const char *page, 221ac9fafa1SAlan D. Brunelle size_t count) 222ac9fafa1SAlan D. Brunelle { 223ac9fafa1SAlan D. Brunelle unsigned long nm; 224ac9fafa1SAlan D. Brunelle ssize_t ret = queue_var_store(&nm, page, count); 225ac9fafa1SAlan D. Brunelle 226bf0f9702SJens Axboe spin_lock_irq(q->queue_lock); 227bf0f9702SJens Axboe queue_flag_clear(QUEUE_FLAG_NOMERGES, q); 228488991e2SAlan D. Brunelle queue_flag_clear(QUEUE_FLAG_NOXMERGES, q); 229488991e2SAlan D. Brunelle if (nm == 2) 230488991e2SAlan D. Brunelle queue_flag_set(QUEUE_FLAG_NOMERGES, q); 231488991e2SAlan D. Brunelle else if (nm) 232488991e2SAlan D. Brunelle queue_flag_set(QUEUE_FLAG_NOXMERGES, q); 233bf0f9702SJens Axboe spin_unlock_irq(q->queue_lock); 2341308835fSBartlomiej Zolnierkiewicz 235ac9fafa1SAlan D. Brunelle return ret; 236ac9fafa1SAlan D. Brunelle } 237ac9fafa1SAlan D. Brunelle 238c7c22e4dSJens Axboe static ssize_t queue_rq_affinity_show(struct request_queue *q, char *page) 239c7c22e4dSJens Axboe { 2409cb308ceSXiaotian Feng bool set = test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags); 241c7c22e4dSJens Axboe 2429cb308ceSXiaotian Feng return queue_var_show(set, page); 243c7c22e4dSJens Axboe } 244c7c22e4dSJens Axboe 245c7c22e4dSJens Axboe static ssize_t 246c7c22e4dSJens Axboe queue_rq_affinity_store(struct request_queue *q, const char *page, size_t count) 247c7c22e4dSJens Axboe { 248c7c22e4dSJens Axboe ssize_t ret = -EINVAL; 249c7c22e4dSJens Axboe #if defined(CONFIG_USE_GENERIC_SMP_HELPERS) 250c7c22e4dSJens Axboe unsigned long val; 251c7c22e4dSJens Axboe 252c7c22e4dSJens Axboe ret = queue_var_store(&val, page, count); 253c7c22e4dSJens Axboe spin_lock_irq(q->queue_lock); 254c7c22e4dSJens Axboe if (val) 255c7c22e4dSJens Axboe queue_flag_set(QUEUE_FLAG_SAME_COMP, q); 256c7c22e4dSJens Axboe else 257c7c22e4dSJens Axboe queue_flag_clear(QUEUE_FLAG_SAME_COMP, q); 258c7c22e4dSJens Axboe spin_unlock_irq(q->queue_lock); 259c7c22e4dSJens Axboe #endif 260c7c22e4dSJens Axboe return ret; 261c7c22e4dSJens Axboe } 2628324aa91SJens Axboe 2638324aa91SJens Axboe static struct queue_sysfs_entry queue_requests_entry = { 2648324aa91SJens Axboe .attr = {.name = "nr_requests", .mode = S_IRUGO | S_IWUSR }, 2658324aa91SJens Axboe .show = queue_requests_show, 2668324aa91SJens Axboe .store = queue_requests_store, 2678324aa91SJens Axboe }; 2688324aa91SJens Axboe 2698324aa91SJens Axboe static struct queue_sysfs_entry queue_ra_entry = { 2708324aa91SJens Axboe .attr = {.name = "read_ahead_kb", .mode = S_IRUGO | S_IWUSR }, 2718324aa91SJens Axboe .show = queue_ra_show, 2728324aa91SJens Axboe .store = queue_ra_store, 2738324aa91SJens Axboe }; 2748324aa91SJens Axboe 2758324aa91SJens Axboe static struct queue_sysfs_entry queue_max_sectors_entry = { 2768324aa91SJens Axboe .attr = {.name = "max_sectors_kb", .mode = S_IRUGO | S_IWUSR }, 2778324aa91SJens Axboe .show = queue_max_sectors_show, 2788324aa91SJens Axboe .store = queue_max_sectors_store, 2798324aa91SJens Axboe }; 2808324aa91SJens Axboe 2818324aa91SJens Axboe static struct queue_sysfs_entry queue_max_hw_sectors_entry = { 2828324aa91SJens Axboe .attr = {.name = "max_hw_sectors_kb", .mode = S_IRUGO }, 2838324aa91SJens Axboe .show = queue_max_hw_sectors_show, 2848324aa91SJens Axboe }; 2858324aa91SJens Axboe 286c77a5710SMartin K. Petersen static struct queue_sysfs_entry queue_max_segments_entry = { 287c77a5710SMartin K. Petersen .attr = {.name = "max_segments", .mode = S_IRUGO }, 288c77a5710SMartin K. Petersen .show = queue_max_segments_show, 289c77a5710SMartin K. Petersen }; 290c77a5710SMartin K. Petersen 291c77a5710SMartin K. Petersen static struct queue_sysfs_entry queue_max_segment_size_entry = { 292c77a5710SMartin K. Petersen .attr = {.name = "max_segment_size", .mode = S_IRUGO }, 293c77a5710SMartin K. Petersen .show = queue_max_segment_size_show, 294c77a5710SMartin K. Petersen }; 295c77a5710SMartin K. Petersen 2968324aa91SJens Axboe static struct queue_sysfs_entry queue_iosched_entry = { 2978324aa91SJens Axboe .attr = {.name = "scheduler", .mode = S_IRUGO | S_IWUSR }, 2988324aa91SJens Axboe .show = elv_iosched_show, 2998324aa91SJens Axboe .store = elv_iosched_store, 3008324aa91SJens Axboe }; 3018324aa91SJens Axboe 302e68b903cSMartin K. Petersen static struct queue_sysfs_entry queue_hw_sector_size_entry = { 303e68b903cSMartin K. Petersen .attr = {.name = "hw_sector_size", .mode = S_IRUGO }, 304e1defc4fSMartin K. Petersen .show = queue_logical_block_size_show, 305e1defc4fSMartin K. Petersen }; 306e1defc4fSMartin K. Petersen 307e1defc4fSMartin K. Petersen static struct queue_sysfs_entry queue_logical_block_size_entry = { 308e1defc4fSMartin K. Petersen .attr = {.name = "logical_block_size", .mode = S_IRUGO }, 309e1defc4fSMartin K. Petersen .show = queue_logical_block_size_show, 310e68b903cSMartin K. Petersen }; 311e68b903cSMartin K. Petersen 312c72758f3SMartin K. Petersen static struct queue_sysfs_entry queue_physical_block_size_entry = { 313c72758f3SMartin K. Petersen .attr = {.name = "physical_block_size", .mode = S_IRUGO }, 314c72758f3SMartin K. Petersen .show = queue_physical_block_size_show, 315c72758f3SMartin K. Petersen }; 316c72758f3SMartin K. Petersen 317c72758f3SMartin K. Petersen static struct queue_sysfs_entry queue_io_min_entry = { 318c72758f3SMartin K. Petersen .attr = {.name = "minimum_io_size", .mode = S_IRUGO }, 319c72758f3SMartin K. Petersen .show = queue_io_min_show, 320c72758f3SMartin K. Petersen }; 321c72758f3SMartin K. Petersen 322c72758f3SMartin K. Petersen static struct queue_sysfs_entry queue_io_opt_entry = { 323c72758f3SMartin K. Petersen .attr = {.name = "optimal_io_size", .mode = S_IRUGO }, 324c72758f3SMartin K. Petersen .show = queue_io_opt_show, 3258324aa91SJens Axboe }; 3268324aa91SJens Axboe 32786b37281SMartin K. Petersen static struct queue_sysfs_entry queue_discard_granularity_entry = { 32886b37281SMartin K. Petersen .attr = {.name = "discard_granularity", .mode = S_IRUGO }, 32986b37281SMartin K. Petersen .show = queue_discard_granularity_show, 33086b37281SMartin K. Petersen }; 33186b37281SMartin K. Petersen 33286b37281SMartin K. Petersen static struct queue_sysfs_entry queue_discard_max_entry = { 33386b37281SMartin K. Petersen .attr = {.name = "discard_max_bytes", .mode = S_IRUGO }, 33486b37281SMartin K. Petersen .show = queue_discard_max_show, 33586b37281SMartin K. Petersen }; 33686b37281SMartin K. Petersen 33798262f27SMartin K. Petersen static struct queue_sysfs_entry queue_discard_zeroes_data_entry = { 33898262f27SMartin K. Petersen .attr = {.name = "discard_zeroes_data", .mode = S_IRUGO }, 33998262f27SMartin K. Petersen .show = queue_discard_zeroes_data_show, 34098262f27SMartin K. Petersen }; 34198262f27SMartin K. Petersen 3421308835fSBartlomiej Zolnierkiewicz static struct queue_sysfs_entry queue_nonrot_entry = { 3431308835fSBartlomiej Zolnierkiewicz .attr = {.name = "rotational", .mode = S_IRUGO | S_IWUSR }, 344*956bcb7cSJens Axboe .show = queue_show_nonrot, 345*956bcb7cSJens Axboe .store = queue_store_nonrot, 3461308835fSBartlomiej Zolnierkiewicz }; 3471308835fSBartlomiej Zolnierkiewicz 348ac9fafa1SAlan D. Brunelle static struct queue_sysfs_entry queue_nomerges_entry = { 349ac9fafa1SAlan D. Brunelle .attr = {.name = "nomerges", .mode = S_IRUGO | S_IWUSR }, 350ac9fafa1SAlan D. Brunelle .show = queue_nomerges_show, 351ac9fafa1SAlan D. Brunelle .store = queue_nomerges_store, 352ac9fafa1SAlan D. Brunelle }; 353ac9fafa1SAlan D. Brunelle 354c7c22e4dSJens Axboe static struct queue_sysfs_entry queue_rq_affinity_entry = { 355c7c22e4dSJens Axboe .attr = {.name = "rq_affinity", .mode = S_IRUGO | S_IWUSR }, 356c7c22e4dSJens Axboe .show = queue_rq_affinity_show, 357c7c22e4dSJens Axboe .store = queue_rq_affinity_store, 358c7c22e4dSJens Axboe }; 359c7c22e4dSJens Axboe 360bc58ba94SJens Axboe static struct queue_sysfs_entry queue_iostats_entry = { 361bc58ba94SJens Axboe .attr = {.name = "iostats", .mode = S_IRUGO | S_IWUSR }, 362*956bcb7cSJens Axboe .show = queue_show_iostats, 363*956bcb7cSJens Axboe .store = queue_store_iostats, 364bc58ba94SJens Axboe }; 365bc58ba94SJens Axboe 366e2e1a148SJens Axboe static struct queue_sysfs_entry queue_random_entry = { 367e2e1a148SJens Axboe .attr = {.name = "add_random", .mode = S_IRUGO | S_IWUSR }, 368*956bcb7cSJens Axboe .show = queue_show_random, 369*956bcb7cSJens Axboe .store = queue_store_random, 370e2e1a148SJens Axboe }; 371e2e1a148SJens Axboe 3728324aa91SJens Axboe static struct attribute *default_attrs[] = { 3738324aa91SJens Axboe &queue_requests_entry.attr, 3748324aa91SJens Axboe &queue_ra_entry.attr, 3758324aa91SJens Axboe &queue_max_hw_sectors_entry.attr, 3768324aa91SJens Axboe &queue_max_sectors_entry.attr, 377c77a5710SMartin K. Petersen &queue_max_segments_entry.attr, 378c77a5710SMartin K. Petersen &queue_max_segment_size_entry.attr, 3798324aa91SJens Axboe &queue_iosched_entry.attr, 380e68b903cSMartin K. Petersen &queue_hw_sector_size_entry.attr, 381e1defc4fSMartin K. Petersen &queue_logical_block_size_entry.attr, 382c72758f3SMartin K. Petersen &queue_physical_block_size_entry.attr, 383c72758f3SMartin K. Petersen &queue_io_min_entry.attr, 384c72758f3SMartin K. Petersen &queue_io_opt_entry.attr, 38586b37281SMartin K. Petersen &queue_discard_granularity_entry.attr, 38686b37281SMartin K. Petersen &queue_discard_max_entry.attr, 38798262f27SMartin K. Petersen &queue_discard_zeroes_data_entry.attr, 3881308835fSBartlomiej Zolnierkiewicz &queue_nonrot_entry.attr, 389ac9fafa1SAlan D. Brunelle &queue_nomerges_entry.attr, 390c7c22e4dSJens Axboe &queue_rq_affinity_entry.attr, 391bc58ba94SJens Axboe &queue_iostats_entry.attr, 392e2e1a148SJens Axboe &queue_random_entry.attr, 3938324aa91SJens Axboe NULL, 3948324aa91SJens Axboe }; 3958324aa91SJens Axboe 3968324aa91SJens Axboe #define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr) 3978324aa91SJens Axboe 3988324aa91SJens Axboe static ssize_t 3998324aa91SJens Axboe queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page) 4008324aa91SJens Axboe { 4018324aa91SJens Axboe struct queue_sysfs_entry *entry = to_queue(attr); 4028324aa91SJens Axboe struct request_queue *q = 4038324aa91SJens Axboe container_of(kobj, struct request_queue, kobj); 4048324aa91SJens Axboe ssize_t res; 4058324aa91SJens Axboe 4068324aa91SJens Axboe if (!entry->show) 4078324aa91SJens Axboe return -EIO; 4088324aa91SJens Axboe mutex_lock(&q->sysfs_lock); 4098324aa91SJens Axboe if (test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)) { 4108324aa91SJens Axboe mutex_unlock(&q->sysfs_lock); 4118324aa91SJens Axboe return -ENOENT; 4128324aa91SJens Axboe } 4138324aa91SJens Axboe res = entry->show(q, page); 4148324aa91SJens Axboe mutex_unlock(&q->sysfs_lock); 4158324aa91SJens Axboe return res; 4168324aa91SJens Axboe } 4178324aa91SJens Axboe 4188324aa91SJens Axboe static ssize_t 4198324aa91SJens Axboe queue_attr_store(struct kobject *kobj, struct attribute *attr, 4208324aa91SJens Axboe const char *page, size_t length) 4218324aa91SJens Axboe { 4228324aa91SJens Axboe struct queue_sysfs_entry *entry = to_queue(attr); 4236728cb0eSJens Axboe struct request_queue *q; 4248324aa91SJens Axboe ssize_t res; 4258324aa91SJens Axboe 4268324aa91SJens Axboe if (!entry->store) 4278324aa91SJens Axboe return -EIO; 4286728cb0eSJens Axboe 4296728cb0eSJens Axboe q = container_of(kobj, struct request_queue, kobj); 4308324aa91SJens Axboe mutex_lock(&q->sysfs_lock); 4318324aa91SJens Axboe if (test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)) { 4328324aa91SJens Axboe mutex_unlock(&q->sysfs_lock); 4338324aa91SJens Axboe return -ENOENT; 4348324aa91SJens Axboe } 4358324aa91SJens Axboe res = entry->store(q, page, length); 4368324aa91SJens Axboe mutex_unlock(&q->sysfs_lock); 4378324aa91SJens Axboe return res; 4388324aa91SJens Axboe } 4398324aa91SJens Axboe 4408324aa91SJens Axboe /** 4418324aa91SJens Axboe * blk_cleanup_queue: - release a &struct request_queue when it is no longer needed 4428324aa91SJens Axboe * @kobj: the kobj belonging of the request queue to be released 4438324aa91SJens Axboe * 4448324aa91SJens Axboe * Description: 4458324aa91SJens Axboe * blk_cleanup_queue is the pair to blk_init_queue() or 4468324aa91SJens Axboe * blk_queue_make_request(). It should be called when a request queue is 4478324aa91SJens Axboe * being released; typically when a block device is being de-registered. 4488324aa91SJens Axboe * Currently, its primary task it to free all the &struct request 4498324aa91SJens Axboe * structures that were allocated to the queue and the queue itself. 4508324aa91SJens Axboe * 4518324aa91SJens Axboe * Caveat: 4528324aa91SJens Axboe * Hopefully the low level driver will have finished any 4538324aa91SJens Axboe * outstanding requests first... 4548324aa91SJens Axboe **/ 4558324aa91SJens Axboe static void blk_release_queue(struct kobject *kobj) 4568324aa91SJens Axboe { 4578324aa91SJens Axboe struct request_queue *q = 4588324aa91SJens Axboe container_of(kobj, struct request_queue, kobj); 4598324aa91SJens Axboe struct request_list *rl = &q->rq; 4608324aa91SJens Axboe 4618324aa91SJens Axboe blk_sync_queue(q); 4628324aa91SJens Axboe 4638324aa91SJens Axboe if (rl->rq_pool) 4648324aa91SJens Axboe mempool_destroy(rl->rq_pool); 4658324aa91SJens Axboe 4668324aa91SJens Axboe if (q->queue_tags) 4678324aa91SJens Axboe __blk_queue_free_tags(q); 4688324aa91SJens Axboe 4698324aa91SJens Axboe blk_trace_shutdown(q); 4708324aa91SJens Axboe 4718324aa91SJens Axboe bdi_destroy(&q->backing_dev_info); 4728324aa91SJens Axboe kmem_cache_free(blk_requestq_cachep, q); 4738324aa91SJens Axboe } 4748324aa91SJens Axboe 47552cf25d0SEmese Revfy static const struct sysfs_ops queue_sysfs_ops = { 4768324aa91SJens Axboe .show = queue_attr_show, 4778324aa91SJens Axboe .store = queue_attr_store, 4788324aa91SJens Axboe }; 4798324aa91SJens Axboe 4808324aa91SJens Axboe struct kobj_type blk_queue_ktype = { 4818324aa91SJens Axboe .sysfs_ops = &queue_sysfs_ops, 4828324aa91SJens Axboe .default_attrs = default_attrs, 4838324aa91SJens Axboe .release = blk_release_queue, 4848324aa91SJens Axboe }; 4858324aa91SJens Axboe 4868324aa91SJens Axboe int blk_register_queue(struct gendisk *disk) 4878324aa91SJens Axboe { 4888324aa91SJens Axboe int ret; 4891d54ad6dSLi Zefan struct device *dev = disk_to_dev(disk); 4908324aa91SJens Axboe 4918324aa91SJens Axboe struct request_queue *q = disk->queue; 4928324aa91SJens Axboe 493fb199746SAkinobu Mita if (WARN_ON(!q)) 4948324aa91SJens Axboe return -ENXIO; 4958324aa91SJens Axboe 4961d54ad6dSLi Zefan ret = blk_trace_init_sysfs(dev); 4971d54ad6dSLi Zefan if (ret) 4981d54ad6dSLi Zefan return ret; 4991d54ad6dSLi Zefan 500c9059598SLinus Torvalds ret = kobject_add(&q->kobj, kobject_get(&dev->kobj), "%s", "queue"); 5018324aa91SJens Axboe if (ret < 0) 5028324aa91SJens Axboe return ret; 5038324aa91SJens Axboe 5048324aa91SJens Axboe kobject_uevent(&q->kobj, KOBJ_ADD); 5058324aa91SJens Axboe 506cd43e26fSMartin K. Petersen if (!q->request_fn) 507cd43e26fSMartin K. Petersen return 0; 508cd43e26fSMartin K. Petersen 5098324aa91SJens Axboe ret = elv_register_queue(q); 5108324aa91SJens Axboe if (ret) { 5118324aa91SJens Axboe kobject_uevent(&q->kobj, KOBJ_REMOVE); 5128324aa91SJens Axboe kobject_del(&q->kobj); 51348c0d4d4SZdenek Kabelac blk_trace_remove_sysfs(disk_to_dev(disk)); 5148324aa91SJens Axboe return ret; 5158324aa91SJens Axboe } 5168324aa91SJens Axboe 5178324aa91SJens Axboe return 0; 5188324aa91SJens Axboe } 5198324aa91SJens Axboe 5208324aa91SJens Axboe void blk_unregister_queue(struct gendisk *disk) 5218324aa91SJens Axboe { 5228324aa91SJens Axboe struct request_queue *q = disk->queue; 5238324aa91SJens Axboe 524fb199746SAkinobu Mita if (WARN_ON(!q)) 525fb199746SAkinobu Mita return; 526fb199746SAkinobu Mita 52748c0d4d4SZdenek Kabelac if (q->request_fn) 5288324aa91SJens Axboe elv_unregister_queue(q); 5298324aa91SJens Axboe 5308324aa91SJens Axboe kobject_uevent(&q->kobj, KOBJ_REMOVE); 5318324aa91SJens Axboe kobject_del(&q->kobj); 53248c0d4d4SZdenek Kabelac blk_trace_remove_sysfs(disk_to_dev(disk)); 533ed9e1982STejun Heo kobject_put(&disk_to_dev(disk)->kobj); 5348324aa91SJens Axboe } 535