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 518324aa91SJens Axboe if (rl->count[READ] >= queue_congestion_on_threshold(q)) 528324aa91SJens Axboe blk_set_queue_congested(q, READ); 538324aa91SJens Axboe else if (rl->count[READ] < queue_congestion_off_threshold(q)) 548324aa91SJens Axboe blk_clear_queue_congested(q, READ); 558324aa91SJens Axboe 568324aa91SJens Axboe if (rl->count[WRITE] >= queue_congestion_on_threshold(q)) 578324aa91SJens Axboe blk_set_queue_congested(q, WRITE); 588324aa91SJens Axboe else if (rl->count[WRITE] < queue_congestion_off_threshold(q)) 598324aa91SJens Axboe blk_clear_queue_congested(q, WRITE); 608324aa91SJens Axboe 618324aa91SJens Axboe if (rl->count[READ] >= q->nr_requests) { 628324aa91SJens Axboe blk_set_queue_full(q, READ); 638324aa91SJens Axboe } else if (rl->count[READ]+1 <= q->nr_requests) { 648324aa91SJens Axboe blk_clear_queue_full(q, READ); 658324aa91SJens Axboe wake_up(&rl->wait[READ]); 668324aa91SJens Axboe } 678324aa91SJens Axboe 688324aa91SJens Axboe if (rl->count[WRITE] >= q->nr_requests) { 698324aa91SJens Axboe blk_set_queue_full(q, WRITE); 708324aa91SJens Axboe } else if (rl->count[WRITE]+1 <= q->nr_requests) { 718324aa91SJens Axboe blk_clear_queue_full(q, WRITE); 728324aa91SJens Axboe wake_up(&rl->wait[WRITE]); 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 spin_lock_irq(q->queue_lock); 928324aa91SJens Axboe q->backing_dev_info.ra_pages = ra_kb >> (PAGE_CACHE_SHIFT - 10); 938324aa91SJens Axboe spin_unlock_irq(q->queue_lock); 948324aa91SJens Axboe 958324aa91SJens Axboe return ret; 968324aa91SJens Axboe } 978324aa91SJens Axboe 988324aa91SJens Axboe static ssize_t queue_max_sectors_show(struct request_queue *q, char *page) 998324aa91SJens Axboe { 1008324aa91SJens Axboe int max_sectors_kb = q->max_sectors >> 1; 1018324aa91SJens Axboe 1028324aa91SJens Axboe return queue_var_show(max_sectors_kb, (page)); 1038324aa91SJens Axboe } 1048324aa91SJens Axboe 105e68b903cSMartin K. Petersen static ssize_t queue_hw_sector_size_show(struct request_queue *q, char *page) 106e68b903cSMartin K. Petersen { 107e68b903cSMartin K. Petersen return queue_var_show(q->hardsect_size, page); 108e68b903cSMartin K. Petersen } 109e68b903cSMartin K. Petersen 1108324aa91SJens Axboe static ssize_t 1118324aa91SJens Axboe queue_max_sectors_store(struct request_queue *q, const char *page, size_t count) 1128324aa91SJens Axboe { 1138324aa91SJens Axboe unsigned long max_sectors_kb, 1148324aa91SJens Axboe max_hw_sectors_kb = q->max_hw_sectors >> 1, 1158324aa91SJens Axboe page_kb = 1 << (PAGE_CACHE_SHIFT - 10); 1168324aa91SJens Axboe ssize_t ret = queue_var_store(&max_sectors_kb, page, count); 1178324aa91SJens Axboe 1188324aa91SJens Axboe if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb) 1198324aa91SJens Axboe return -EINVAL; 1208324aa91SJens Axboe /* 1218324aa91SJens Axboe * Take the queue lock to update the readahead and max_sectors 1228324aa91SJens Axboe * values synchronously: 1238324aa91SJens Axboe */ 1248324aa91SJens Axboe spin_lock_irq(q->queue_lock); 1258324aa91SJens Axboe q->max_sectors = max_sectors_kb << 1; 1268324aa91SJens Axboe spin_unlock_irq(q->queue_lock); 1278324aa91SJens Axboe 1288324aa91SJens Axboe return ret; 1298324aa91SJens Axboe } 1308324aa91SJens Axboe 1318324aa91SJens Axboe static ssize_t queue_max_hw_sectors_show(struct request_queue *q, char *page) 1328324aa91SJens Axboe { 1338324aa91SJens Axboe int max_hw_sectors_kb = q->max_hw_sectors >> 1; 1348324aa91SJens Axboe 1358324aa91SJens Axboe return queue_var_show(max_hw_sectors_kb, (page)); 1368324aa91SJens Axboe } 1378324aa91SJens Axboe 138*ac9fafa1SAlan D. Brunelle static ssize_t queue_nomerges_show(struct request_queue *q, char *page) 139*ac9fafa1SAlan D. Brunelle { 140*ac9fafa1SAlan D. Brunelle return queue_var_show(blk_queue_nomerges(q), page); 141*ac9fafa1SAlan D. Brunelle } 142*ac9fafa1SAlan D. Brunelle 143*ac9fafa1SAlan D. Brunelle static ssize_t queue_nomerges_store(struct request_queue *q, const char *page, 144*ac9fafa1SAlan D. Brunelle size_t count) 145*ac9fafa1SAlan D. Brunelle { 146*ac9fafa1SAlan D. Brunelle unsigned long nm; 147*ac9fafa1SAlan D. Brunelle ssize_t ret = queue_var_store(&nm, page, count); 148*ac9fafa1SAlan D. Brunelle 149*ac9fafa1SAlan D. Brunelle if (nm) 150*ac9fafa1SAlan D. Brunelle set_bit(QUEUE_FLAG_NOMERGES, &q->queue_flags); 151*ac9fafa1SAlan D. Brunelle else 152*ac9fafa1SAlan D. Brunelle clear_bit(QUEUE_FLAG_NOMERGES, &q->queue_flags); 153*ac9fafa1SAlan D. Brunelle 154*ac9fafa1SAlan D. Brunelle return ret; 155*ac9fafa1SAlan D. Brunelle } 156*ac9fafa1SAlan D. Brunelle 1578324aa91SJens Axboe 1588324aa91SJens Axboe static struct queue_sysfs_entry queue_requests_entry = { 1598324aa91SJens Axboe .attr = {.name = "nr_requests", .mode = S_IRUGO | S_IWUSR }, 1608324aa91SJens Axboe .show = queue_requests_show, 1618324aa91SJens Axboe .store = queue_requests_store, 1628324aa91SJens Axboe }; 1638324aa91SJens Axboe 1648324aa91SJens Axboe static struct queue_sysfs_entry queue_ra_entry = { 1658324aa91SJens Axboe .attr = {.name = "read_ahead_kb", .mode = S_IRUGO | S_IWUSR }, 1668324aa91SJens Axboe .show = queue_ra_show, 1678324aa91SJens Axboe .store = queue_ra_store, 1688324aa91SJens Axboe }; 1698324aa91SJens Axboe 1708324aa91SJens Axboe static struct queue_sysfs_entry queue_max_sectors_entry = { 1718324aa91SJens Axboe .attr = {.name = "max_sectors_kb", .mode = S_IRUGO | S_IWUSR }, 1728324aa91SJens Axboe .show = queue_max_sectors_show, 1738324aa91SJens Axboe .store = queue_max_sectors_store, 1748324aa91SJens Axboe }; 1758324aa91SJens Axboe 1768324aa91SJens Axboe static struct queue_sysfs_entry queue_max_hw_sectors_entry = { 1778324aa91SJens Axboe .attr = {.name = "max_hw_sectors_kb", .mode = S_IRUGO }, 1788324aa91SJens Axboe .show = queue_max_hw_sectors_show, 1798324aa91SJens Axboe }; 1808324aa91SJens Axboe 1818324aa91SJens Axboe static struct queue_sysfs_entry queue_iosched_entry = { 1828324aa91SJens Axboe .attr = {.name = "scheduler", .mode = S_IRUGO | S_IWUSR }, 1838324aa91SJens Axboe .show = elv_iosched_show, 1848324aa91SJens Axboe .store = elv_iosched_store, 1858324aa91SJens Axboe }; 1868324aa91SJens Axboe 187e68b903cSMartin K. Petersen static struct queue_sysfs_entry queue_hw_sector_size_entry = { 188e68b903cSMartin K. Petersen .attr = {.name = "hw_sector_size", .mode = S_IRUGO }, 189e68b903cSMartin K. Petersen .show = queue_hw_sector_size_show, 190e68b903cSMartin K. Petersen }; 191e68b903cSMartin K. Petersen 192*ac9fafa1SAlan D. Brunelle static struct queue_sysfs_entry queue_nomerges_entry = { 193*ac9fafa1SAlan D. Brunelle .attr = {.name = "nomerges", .mode = S_IRUGO | S_IWUSR }, 194*ac9fafa1SAlan D. Brunelle .show = queue_nomerges_show, 195*ac9fafa1SAlan D. Brunelle .store = queue_nomerges_store, 196*ac9fafa1SAlan D. Brunelle }; 197*ac9fafa1SAlan D. Brunelle 1988324aa91SJens Axboe static struct attribute *default_attrs[] = { 1998324aa91SJens Axboe &queue_requests_entry.attr, 2008324aa91SJens Axboe &queue_ra_entry.attr, 2018324aa91SJens Axboe &queue_max_hw_sectors_entry.attr, 2028324aa91SJens Axboe &queue_max_sectors_entry.attr, 2038324aa91SJens Axboe &queue_iosched_entry.attr, 204e68b903cSMartin K. Petersen &queue_hw_sector_size_entry.attr, 205*ac9fafa1SAlan D. Brunelle &queue_nomerges_entry.attr, 2068324aa91SJens Axboe NULL, 2078324aa91SJens Axboe }; 2088324aa91SJens Axboe 2098324aa91SJens Axboe #define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr) 2108324aa91SJens Axboe 2118324aa91SJens Axboe static ssize_t 2128324aa91SJens Axboe queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page) 2138324aa91SJens Axboe { 2148324aa91SJens Axboe struct queue_sysfs_entry *entry = to_queue(attr); 2158324aa91SJens Axboe struct request_queue *q = 2168324aa91SJens Axboe container_of(kobj, struct request_queue, kobj); 2178324aa91SJens Axboe ssize_t res; 2188324aa91SJens Axboe 2198324aa91SJens Axboe if (!entry->show) 2208324aa91SJens Axboe return -EIO; 2218324aa91SJens Axboe mutex_lock(&q->sysfs_lock); 2228324aa91SJens Axboe if (test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)) { 2238324aa91SJens Axboe mutex_unlock(&q->sysfs_lock); 2248324aa91SJens Axboe return -ENOENT; 2258324aa91SJens Axboe } 2268324aa91SJens Axboe res = entry->show(q, page); 2278324aa91SJens Axboe mutex_unlock(&q->sysfs_lock); 2288324aa91SJens Axboe return res; 2298324aa91SJens Axboe } 2308324aa91SJens Axboe 2318324aa91SJens Axboe static ssize_t 2328324aa91SJens Axboe queue_attr_store(struct kobject *kobj, struct attribute *attr, 2338324aa91SJens Axboe const char *page, size_t length) 2348324aa91SJens Axboe { 2358324aa91SJens Axboe struct queue_sysfs_entry *entry = to_queue(attr); 2366728cb0eSJens Axboe struct request_queue *q; 2378324aa91SJens Axboe ssize_t res; 2388324aa91SJens Axboe 2398324aa91SJens Axboe if (!entry->store) 2408324aa91SJens Axboe return -EIO; 2416728cb0eSJens Axboe 2426728cb0eSJens Axboe q = container_of(kobj, struct request_queue, kobj); 2438324aa91SJens Axboe mutex_lock(&q->sysfs_lock); 2448324aa91SJens Axboe if (test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)) { 2458324aa91SJens Axboe mutex_unlock(&q->sysfs_lock); 2468324aa91SJens Axboe return -ENOENT; 2478324aa91SJens Axboe } 2488324aa91SJens Axboe res = entry->store(q, page, length); 2498324aa91SJens Axboe mutex_unlock(&q->sysfs_lock); 2508324aa91SJens Axboe return res; 2518324aa91SJens Axboe } 2528324aa91SJens Axboe 2538324aa91SJens Axboe /** 2548324aa91SJens Axboe * blk_cleanup_queue: - release a &struct request_queue when it is no longer needed 2558324aa91SJens Axboe * @kobj: the kobj belonging of the request queue to be released 2568324aa91SJens Axboe * 2578324aa91SJens Axboe * Description: 2588324aa91SJens Axboe * blk_cleanup_queue is the pair to blk_init_queue() or 2598324aa91SJens Axboe * blk_queue_make_request(). It should be called when a request queue is 2608324aa91SJens Axboe * being released; typically when a block device is being de-registered. 2618324aa91SJens Axboe * Currently, its primary task it to free all the &struct request 2628324aa91SJens Axboe * structures that were allocated to the queue and the queue itself. 2638324aa91SJens Axboe * 2648324aa91SJens Axboe * Caveat: 2658324aa91SJens Axboe * Hopefully the low level driver will have finished any 2668324aa91SJens Axboe * outstanding requests first... 2678324aa91SJens Axboe **/ 2688324aa91SJens Axboe static void blk_release_queue(struct kobject *kobj) 2698324aa91SJens Axboe { 2708324aa91SJens Axboe struct request_queue *q = 2718324aa91SJens Axboe container_of(kobj, struct request_queue, kobj); 2728324aa91SJens Axboe struct request_list *rl = &q->rq; 2738324aa91SJens Axboe 2748324aa91SJens Axboe blk_sync_queue(q); 2758324aa91SJens Axboe 2768324aa91SJens Axboe if (rl->rq_pool) 2778324aa91SJens Axboe mempool_destroy(rl->rq_pool); 2788324aa91SJens Axboe 2798324aa91SJens Axboe if (q->queue_tags) 2808324aa91SJens Axboe __blk_queue_free_tags(q); 2818324aa91SJens Axboe 2828324aa91SJens Axboe blk_trace_shutdown(q); 2838324aa91SJens Axboe 2848324aa91SJens Axboe bdi_destroy(&q->backing_dev_info); 2858324aa91SJens Axboe kmem_cache_free(blk_requestq_cachep, q); 2868324aa91SJens Axboe } 2878324aa91SJens Axboe 2888324aa91SJens Axboe static struct sysfs_ops queue_sysfs_ops = { 2898324aa91SJens Axboe .show = queue_attr_show, 2908324aa91SJens Axboe .store = queue_attr_store, 2918324aa91SJens Axboe }; 2928324aa91SJens Axboe 2938324aa91SJens Axboe struct kobj_type blk_queue_ktype = { 2948324aa91SJens Axboe .sysfs_ops = &queue_sysfs_ops, 2958324aa91SJens Axboe .default_attrs = default_attrs, 2968324aa91SJens Axboe .release = blk_release_queue, 2978324aa91SJens Axboe }; 2988324aa91SJens Axboe 2998324aa91SJens Axboe int blk_register_queue(struct gendisk *disk) 3008324aa91SJens Axboe { 3018324aa91SJens Axboe int ret; 3028324aa91SJens Axboe 3038324aa91SJens Axboe struct request_queue *q = disk->queue; 3048324aa91SJens Axboe 305fb199746SAkinobu Mita if (WARN_ON(!q)) 3068324aa91SJens Axboe return -ENXIO; 3078324aa91SJens Axboe 308fb199746SAkinobu Mita if (!q->request_fn) 309fb199746SAkinobu Mita return 0; 310fb199746SAkinobu Mita 3118324aa91SJens Axboe ret = kobject_add(&q->kobj, kobject_get(&disk->dev.kobj), 3128324aa91SJens Axboe "%s", "queue"); 3138324aa91SJens Axboe if (ret < 0) 3148324aa91SJens Axboe return ret; 3158324aa91SJens Axboe 3168324aa91SJens Axboe kobject_uevent(&q->kobj, KOBJ_ADD); 3178324aa91SJens Axboe 3188324aa91SJens Axboe ret = elv_register_queue(q); 3198324aa91SJens Axboe if (ret) { 3208324aa91SJens Axboe kobject_uevent(&q->kobj, KOBJ_REMOVE); 3218324aa91SJens Axboe kobject_del(&q->kobj); 3228324aa91SJens Axboe return ret; 3238324aa91SJens Axboe } 3248324aa91SJens Axboe 3258324aa91SJens Axboe return 0; 3268324aa91SJens Axboe } 3278324aa91SJens Axboe 3288324aa91SJens Axboe void blk_unregister_queue(struct gendisk *disk) 3298324aa91SJens Axboe { 3308324aa91SJens Axboe struct request_queue *q = disk->queue; 3318324aa91SJens Axboe 332fb199746SAkinobu Mita if (WARN_ON(!q)) 333fb199746SAkinobu Mita return; 334fb199746SAkinobu Mita 335fb199746SAkinobu Mita if (q->request_fn) { 3368324aa91SJens Axboe elv_unregister_queue(q); 3378324aa91SJens Axboe 3388324aa91SJens Axboe kobject_uevent(&q->kobj, KOBJ_REMOVE); 3398324aa91SJens Axboe kobject_del(&q->kobj); 3408324aa91SJens Axboe kobject_put(&disk->dev.kobj); 3418324aa91SJens Axboe } 3428324aa91SJens Axboe } 343