xref: /linux/block/blk-sysfs.c (revision 86b37281411cf1e9bc0a6b5406c45edb7bd9ea5d)
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
199cb308ceSXiaotian Feng queue_var_show(unsigned long var, char *page)
208324aa91SJens Axboe {
219cb308ceSXiaotian Feng 	return sprintf(page, "%lu\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;
43b8a9ae77SJens Axboe 	int ret;
44b8a9ae77SJens Axboe 
45b8a9ae77SJens Axboe 	if (!q->request_fn)
46b8a9ae77SJens Axboe 		return -EINVAL;
47b8a9ae77SJens Axboe 
48b8a9ae77SJens Axboe 	ret = queue_var_store(&nr, page, count);
498324aa91SJens Axboe 	if (nr < BLKDEV_MIN_RQ)
508324aa91SJens Axboe 		nr = BLKDEV_MIN_RQ;
518324aa91SJens Axboe 
528324aa91SJens Axboe 	spin_lock_irq(q->queue_lock);
538324aa91SJens Axboe 	q->nr_requests = nr;
548324aa91SJens Axboe 	blk_queue_congestion_threshold(q);
558324aa91SJens Axboe 
561faa16d2SJens Axboe 	if (rl->count[BLK_RW_SYNC] >= queue_congestion_on_threshold(q))
571faa16d2SJens Axboe 		blk_set_queue_congested(q, BLK_RW_SYNC);
581faa16d2SJens Axboe 	else if (rl->count[BLK_RW_SYNC] < queue_congestion_off_threshold(q))
591faa16d2SJens Axboe 		blk_clear_queue_congested(q, BLK_RW_SYNC);
608324aa91SJens Axboe 
611faa16d2SJens Axboe 	if (rl->count[BLK_RW_ASYNC] >= queue_congestion_on_threshold(q))
621faa16d2SJens Axboe 		blk_set_queue_congested(q, BLK_RW_ASYNC);
631faa16d2SJens Axboe 	else if (rl->count[BLK_RW_ASYNC] < queue_congestion_off_threshold(q))
641faa16d2SJens Axboe 		blk_clear_queue_congested(q, BLK_RW_ASYNC);
658324aa91SJens Axboe 
661faa16d2SJens Axboe 	if (rl->count[BLK_RW_SYNC] >= q->nr_requests) {
671faa16d2SJens Axboe 		blk_set_queue_full(q, BLK_RW_SYNC);
681faa16d2SJens Axboe 	} else if (rl->count[BLK_RW_SYNC]+1 <= q->nr_requests) {
691faa16d2SJens Axboe 		blk_clear_queue_full(q, BLK_RW_SYNC);
701faa16d2SJens Axboe 		wake_up(&rl->wait[BLK_RW_SYNC]);
718324aa91SJens Axboe 	}
728324aa91SJens Axboe 
731faa16d2SJens Axboe 	if (rl->count[BLK_RW_ASYNC] >= q->nr_requests) {
741faa16d2SJens Axboe 		blk_set_queue_full(q, BLK_RW_ASYNC);
751faa16d2SJens Axboe 	} else if (rl->count[BLK_RW_ASYNC]+1 <= q->nr_requests) {
761faa16d2SJens Axboe 		blk_clear_queue_full(q, BLK_RW_ASYNC);
771faa16d2SJens Axboe 		wake_up(&rl->wait[BLK_RW_ASYNC]);
788324aa91SJens Axboe 	}
798324aa91SJens Axboe 	spin_unlock_irq(q->queue_lock);
808324aa91SJens Axboe 	return ret;
818324aa91SJens Axboe }
828324aa91SJens Axboe 
838324aa91SJens Axboe static ssize_t queue_ra_show(struct request_queue *q, char *page)
848324aa91SJens Axboe {
859cb308ceSXiaotian Feng 	unsigned long ra_kb = q->backing_dev_info.ra_pages <<
869cb308ceSXiaotian Feng 					(PAGE_CACHE_SHIFT - 10);
878324aa91SJens Axboe 
888324aa91SJens Axboe 	return queue_var_show(ra_kb, (page));
898324aa91SJens Axboe }
908324aa91SJens Axboe 
918324aa91SJens Axboe static ssize_t
928324aa91SJens Axboe queue_ra_store(struct request_queue *q, const char *page, size_t count)
938324aa91SJens Axboe {
948324aa91SJens Axboe 	unsigned long ra_kb;
958324aa91SJens Axboe 	ssize_t ret = queue_var_store(&ra_kb, page, count);
968324aa91SJens Axboe 
978324aa91SJens Axboe 	q->backing_dev_info.ra_pages = ra_kb >> (PAGE_CACHE_SHIFT - 10);
988324aa91SJens Axboe 
998324aa91SJens Axboe 	return ret;
1008324aa91SJens Axboe }
1018324aa91SJens Axboe 
1028324aa91SJens Axboe static ssize_t queue_max_sectors_show(struct request_queue *q, char *page)
1038324aa91SJens Axboe {
104ae03bf63SMartin K. Petersen 	int max_sectors_kb = queue_max_sectors(q) >> 1;
1058324aa91SJens Axboe 
1068324aa91SJens Axboe 	return queue_var_show(max_sectors_kb, (page));
1078324aa91SJens Axboe }
1088324aa91SJens Axboe 
109e1defc4fSMartin K. Petersen static ssize_t queue_logical_block_size_show(struct request_queue *q, char *page)
110e68b903cSMartin K. Petersen {
111e1defc4fSMartin K. Petersen 	return queue_var_show(queue_logical_block_size(q), page);
112e68b903cSMartin K. Petersen }
113e68b903cSMartin K. Petersen 
114c72758f3SMartin K. Petersen static ssize_t queue_physical_block_size_show(struct request_queue *q, char *page)
115c72758f3SMartin K. Petersen {
116c72758f3SMartin K. Petersen 	return queue_var_show(queue_physical_block_size(q), page);
117c72758f3SMartin K. Petersen }
118c72758f3SMartin K. Petersen 
119c72758f3SMartin K. Petersen static ssize_t queue_io_min_show(struct request_queue *q, char *page)
120c72758f3SMartin K. Petersen {
121c72758f3SMartin K. Petersen 	return queue_var_show(queue_io_min(q), page);
122c72758f3SMartin K. Petersen }
123c72758f3SMartin K. Petersen 
124c72758f3SMartin K. Petersen static ssize_t queue_io_opt_show(struct request_queue *q, char *page)
125c72758f3SMartin K. Petersen {
126c72758f3SMartin K. Petersen 	return queue_var_show(queue_io_opt(q), page);
1278324aa91SJens Axboe }
1288324aa91SJens Axboe 
129*86b37281SMartin K. Petersen static ssize_t queue_discard_granularity_show(struct request_queue *q, char *page)
130*86b37281SMartin K. Petersen {
131*86b37281SMartin K. Petersen 	return queue_var_show(q->limits.discard_granularity, page);
132*86b37281SMartin K. Petersen }
133*86b37281SMartin K. Petersen 
134*86b37281SMartin K. Petersen static ssize_t queue_discard_max_show(struct request_queue *q, char *page)
135*86b37281SMartin K. Petersen {
136*86b37281SMartin K. Petersen 	return queue_var_show(q->limits.max_discard_sectors << 9, page);
137*86b37281SMartin K. Petersen }
138*86b37281SMartin K. Petersen 
1398324aa91SJens Axboe static ssize_t
1408324aa91SJens Axboe queue_max_sectors_store(struct request_queue *q, const char *page, size_t count)
1418324aa91SJens Axboe {
1428324aa91SJens Axboe 	unsigned long max_sectors_kb,
143ae03bf63SMartin K. Petersen 		max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1,
1448324aa91SJens Axboe 			page_kb = 1 << (PAGE_CACHE_SHIFT - 10);
1458324aa91SJens Axboe 	ssize_t ret = queue_var_store(&max_sectors_kb, page, count);
1468324aa91SJens Axboe 
1478324aa91SJens Axboe 	if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb)
1488324aa91SJens Axboe 		return -EINVAL;
1497c239517SWu Fengguang 
1508324aa91SJens Axboe 	spin_lock_irq(q->queue_lock);
151c295fc05SNikanth Karthikesan 	q->limits.max_sectors = max_sectors_kb << 1;
1528324aa91SJens Axboe 	spin_unlock_irq(q->queue_lock);
1538324aa91SJens Axboe 
1548324aa91SJens Axboe 	return ret;
1558324aa91SJens Axboe }
1568324aa91SJens Axboe 
1578324aa91SJens Axboe static ssize_t queue_max_hw_sectors_show(struct request_queue *q, char *page)
1588324aa91SJens Axboe {
159ae03bf63SMartin K. Petersen 	int max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1;
1608324aa91SJens Axboe 
1618324aa91SJens Axboe 	return queue_var_show(max_hw_sectors_kb, (page));
1628324aa91SJens Axboe }
1638324aa91SJens Axboe 
1641308835fSBartlomiej Zolnierkiewicz static ssize_t queue_nonrot_show(struct request_queue *q, char *page)
1651308835fSBartlomiej Zolnierkiewicz {
1661308835fSBartlomiej Zolnierkiewicz 	return queue_var_show(!blk_queue_nonrot(q), page);
1671308835fSBartlomiej Zolnierkiewicz }
1681308835fSBartlomiej Zolnierkiewicz 
1691308835fSBartlomiej Zolnierkiewicz static ssize_t queue_nonrot_store(struct request_queue *q, const char *page,
1701308835fSBartlomiej Zolnierkiewicz 				  size_t count)
1711308835fSBartlomiej Zolnierkiewicz {
1721308835fSBartlomiej Zolnierkiewicz 	unsigned long nm;
1731308835fSBartlomiej Zolnierkiewicz 	ssize_t ret = queue_var_store(&nm, page, count);
1741308835fSBartlomiej Zolnierkiewicz 
1751308835fSBartlomiej Zolnierkiewicz 	spin_lock_irq(q->queue_lock);
1761308835fSBartlomiej Zolnierkiewicz 	if (nm)
1771308835fSBartlomiej Zolnierkiewicz 		queue_flag_clear(QUEUE_FLAG_NONROT, q);
1781308835fSBartlomiej Zolnierkiewicz 	else
1791308835fSBartlomiej Zolnierkiewicz 		queue_flag_set(QUEUE_FLAG_NONROT, q);
1801308835fSBartlomiej Zolnierkiewicz 	spin_unlock_irq(q->queue_lock);
1811308835fSBartlomiej Zolnierkiewicz 
1821308835fSBartlomiej Zolnierkiewicz 	return ret;
1831308835fSBartlomiej Zolnierkiewicz }
1841308835fSBartlomiej Zolnierkiewicz 
185ac9fafa1SAlan D. Brunelle static ssize_t queue_nomerges_show(struct request_queue *q, char *page)
186ac9fafa1SAlan D. Brunelle {
187ac9fafa1SAlan D. Brunelle 	return queue_var_show(blk_queue_nomerges(q), page);
188ac9fafa1SAlan D. Brunelle }
189ac9fafa1SAlan D. Brunelle 
190ac9fafa1SAlan D. Brunelle static ssize_t queue_nomerges_store(struct request_queue *q, const char *page,
191ac9fafa1SAlan D. Brunelle 				    size_t count)
192ac9fafa1SAlan D. Brunelle {
193ac9fafa1SAlan D. Brunelle 	unsigned long nm;
194ac9fafa1SAlan D. Brunelle 	ssize_t ret = queue_var_store(&nm, page, count);
195ac9fafa1SAlan D. Brunelle 
196bf0f9702SJens Axboe 	spin_lock_irq(q->queue_lock);
197ac9fafa1SAlan D. Brunelle 	if (nm)
198bf0f9702SJens Axboe 		queue_flag_set(QUEUE_FLAG_NOMERGES, q);
199ac9fafa1SAlan D. Brunelle 	else
200bf0f9702SJens Axboe 		queue_flag_clear(QUEUE_FLAG_NOMERGES, q);
201bf0f9702SJens Axboe 	spin_unlock_irq(q->queue_lock);
2021308835fSBartlomiej Zolnierkiewicz 
203ac9fafa1SAlan D. Brunelle 	return ret;
204ac9fafa1SAlan D. Brunelle }
205ac9fafa1SAlan D. Brunelle 
206c7c22e4dSJens Axboe static ssize_t queue_rq_affinity_show(struct request_queue *q, char *page)
207c7c22e4dSJens Axboe {
2089cb308ceSXiaotian Feng 	bool set = test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags);
209c7c22e4dSJens Axboe 
2109cb308ceSXiaotian Feng 	return queue_var_show(set, page);
211c7c22e4dSJens Axboe }
212c7c22e4dSJens Axboe 
213c7c22e4dSJens Axboe static ssize_t
214c7c22e4dSJens Axboe queue_rq_affinity_store(struct request_queue *q, const char *page, size_t count)
215c7c22e4dSJens Axboe {
216c7c22e4dSJens Axboe 	ssize_t ret = -EINVAL;
217c7c22e4dSJens Axboe #if defined(CONFIG_USE_GENERIC_SMP_HELPERS)
218c7c22e4dSJens Axboe 	unsigned long val;
219c7c22e4dSJens Axboe 
220c7c22e4dSJens Axboe 	ret = queue_var_store(&val, page, count);
221c7c22e4dSJens Axboe 	spin_lock_irq(q->queue_lock);
222c7c22e4dSJens Axboe 	if (val)
223c7c22e4dSJens Axboe 		queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
224c7c22e4dSJens Axboe 	else
225c7c22e4dSJens Axboe 		queue_flag_clear(QUEUE_FLAG_SAME_COMP,  q);
226c7c22e4dSJens Axboe 	spin_unlock_irq(q->queue_lock);
227c7c22e4dSJens Axboe #endif
228c7c22e4dSJens Axboe 	return ret;
229c7c22e4dSJens Axboe }
2308324aa91SJens Axboe 
231bc58ba94SJens Axboe static ssize_t queue_iostats_show(struct request_queue *q, char *page)
232bc58ba94SJens Axboe {
233bc58ba94SJens Axboe 	return queue_var_show(blk_queue_io_stat(q), page);
234bc58ba94SJens Axboe }
235bc58ba94SJens Axboe 
236bc58ba94SJens Axboe static ssize_t queue_iostats_store(struct request_queue *q, const char *page,
237bc58ba94SJens Axboe 				   size_t count)
238bc58ba94SJens Axboe {
239bc58ba94SJens Axboe 	unsigned long stats;
240bc58ba94SJens Axboe 	ssize_t ret = queue_var_store(&stats, page, count);
241bc58ba94SJens Axboe 
242bc58ba94SJens Axboe 	spin_lock_irq(q->queue_lock);
243bc58ba94SJens Axboe 	if (stats)
244bc58ba94SJens Axboe 		queue_flag_set(QUEUE_FLAG_IO_STAT, q);
245bc58ba94SJens Axboe 	else
246bc58ba94SJens Axboe 		queue_flag_clear(QUEUE_FLAG_IO_STAT, q);
247bc58ba94SJens Axboe 	spin_unlock_irq(q->queue_lock);
248bc58ba94SJens Axboe 
249bc58ba94SJens Axboe 	return ret;
250bc58ba94SJens Axboe }
251bc58ba94SJens Axboe 
2528324aa91SJens Axboe static struct queue_sysfs_entry queue_requests_entry = {
2538324aa91SJens Axboe 	.attr = {.name = "nr_requests", .mode = S_IRUGO | S_IWUSR },
2548324aa91SJens Axboe 	.show = queue_requests_show,
2558324aa91SJens Axboe 	.store = queue_requests_store,
2568324aa91SJens Axboe };
2578324aa91SJens Axboe 
2588324aa91SJens Axboe static struct queue_sysfs_entry queue_ra_entry = {
2598324aa91SJens Axboe 	.attr = {.name = "read_ahead_kb", .mode = S_IRUGO | S_IWUSR },
2608324aa91SJens Axboe 	.show = queue_ra_show,
2618324aa91SJens Axboe 	.store = queue_ra_store,
2628324aa91SJens Axboe };
2638324aa91SJens Axboe 
2648324aa91SJens Axboe static struct queue_sysfs_entry queue_max_sectors_entry = {
2658324aa91SJens Axboe 	.attr = {.name = "max_sectors_kb", .mode = S_IRUGO | S_IWUSR },
2668324aa91SJens Axboe 	.show = queue_max_sectors_show,
2678324aa91SJens Axboe 	.store = queue_max_sectors_store,
2688324aa91SJens Axboe };
2698324aa91SJens Axboe 
2708324aa91SJens Axboe static struct queue_sysfs_entry queue_max_hw_sectors_entry = {
2718324aa91SJens Axboe 	.attr = {.name = "max_hw_sectors_kb", .mode = S_IRUGO },
2728324aa91SJens Axboe 	.show = queue_max_hw_sectors_show,
2738324aa91SJens Axboe };
2748324aa91SJens Axboe 
2758324aa91SJens Axboe static struct queue_sysfs_entry queue_iosched_entry = {
2768324aa91SJens Axboe 	.attr = {.name = "scheduler", .mode = S_IRUGO | S_IWUSR },
2778324aa91SJens Axboe 	.show = elv_iosched_show,
2788324aa91SJens Axboe 	.store = elv_iosched_store,
2798324aa91SJens Axboe };
2808324aa91SJens Axboe 
281e68b903cSMartin K. Petersen static struct queue_sysfs_entry queue_hw_sector_size_entry = {
282e68b903cSMartin K. Petersen 	.attr = {.name = "hw_sector_size", .mode = S_IRUGO },
283e1defc4fSMartin K. Petersen 	.show = queue_logical_block_size_show,
284e1defc4fSMartin K. Petersen };
285e1defc4fSMartin K. Petersen 
286e1defc4fSMartin K. Petersen static struct queue_sysfs_entry queue_logical_block_size_entry = {
287e1defc4fSMartin K. Petersen 	.attr = {.name = "logical_block_size", .mode = S_IRUGO },
288e1defc4fSMartin K. Petersen 	.show = queue_logical_block_size_show,
289e68b903cSMartin K. Petersen };
290e68b903cSMartin K. Petersen 
291c72758f3SMartin K. Petersen static struct queue_sysfs_entry queue_physical_block_size_entry = {
292c72758f3SMartin K. Petersen 	.attr = {.name = "physical_block_size", .mode = S_IRUGO },
293c72758f3SMartin K. Petersen 	.show = queue_physical_block_size_show,
294c72758f3SMartin K. Petersen };
295c72758f3SMartin K. Petersen 
296c72758f3SMartin K. Petersen static struct queue_sysfs_entry queue_io_min_entry = {
297c72758f3SMartin K. Petersen 	.attr = {.name = "minimum_io_size", .mode = S_IRUGO },
298c72758f3SMartin K. Petersen 	.show = queue_io_min_show,
299c72758f3SMartin K. Petersen };
300c72758f3SMartin K. Petersen 
301c72758f3SMartin K. Petersen static struct queue_sysfs_entry queue_io_opt_entry = {
302c72758f3SMartin K. Petersen 	.attr = {.name = "optimal_io_size", .mode = S_IRUGO },
303c72758f3SMartin K. Petersen 	.show = queue_io_opt_show,
3048324aa91SJens Axboe };
3058324aa91SJens Axboe 
306*86b37281SMartin K. Petersen static struct queue_sysfs_entry queue_discard_granularity_entry = {
307*86b37281SMartin K. Petersen 	.attr = {.name = "discard_granularity", .mode = S_IRUGO },
308*86b37281SMartin K. Petersen 	.show = queue_discard_granularity_show,
309*86b37281SMartin K. Petersen };
310*86b37281SMartin K. Petersen 
311*86b37281SMartin K. Petersen static struct queue_sysfs_entry queue_discard_max_entry = {
312*86b37281SMartin K. Petersen 	.attr = {.name = "discard_max_bytes", .mode = S_IRUGO },
313*86b37281SMartin K. Petersen 	.show = queue_discard_max_show,
314*86b37281SMartin K. Petersen };
315*86b37281SMartin K. Petersen 
3161308835fSBartlomiej Zolnierkiewicz static struct queue_sysfs_entry queue_nonrot_entry = {
3171308835fSBartlomiej Zolnierkiewicz 	.attr = {.name = "rotational", .mode = S_IRUGO | S_IWUSR },
3181308835fSBartlomiej Zolnierkiewicz 	.show = queue_nonrot_show,
3191308835fSBartlomiej Zolnierkiewicz 	.store = queue_nonrot_store,
3201308835fSBartlomiej Zolnierkiewicz };
3211308835fSBartlomiej Zolnierkiewicz 
322ac9fafa1SAlan D. Brunelle static struct queue_sysfs_entry queue_nomerges_entry = {
323ac9fafa1SAlan D. Brunelle 	.attr = {.name = "nomerges", .mode = S_IRUGO | S_IWUSR },
324ac9fafa1SAlan D. Brunelle 	.show = queue_nomerges_show,
325ac9fafa1SAlan D. Brunelle 	.store = queue_nomerges_store,
326ac9fafa1SAlan D. Brunelle };
327ac9fafa1SAlan D. Brunelle 
328c7c22e4dSJens Axboe static struct queue_sysfs_entry queue_rq_affinity_entry = {
329c7c22e4dSJens Axboe 	.attr = {.name = "rq_affinity", .mode = S_IRUGO | S_IWUSR },
330c7c22e4dSJens Axboe 	.show = queue_rq_affinity_show,
331c7c22e4dSJens Axboe 	.store = queue_rq_affinity_store,
332c7c22e4dSJens Axboe };
333c7c22e4dSJens Axboe 
334bc58ba94SJens Axboe static struct queue_sysfs_entry queue_iostats_entry = {
335bc58ba94SJens Axboe 	.attr = {.name = "iostats", .mode = S_IRUGO | S_IWUSR },
336bc58ba94SJens Axboe 	.show = queue_iostats_show,
337bc58ba94SJens Axboe 	.store = queue_iostats_store,
338bc58ba94SJens Axboe };
339bc58ba94SJens Axboe 
3408324aa91SJens Axboe static struct attribute *default_attrs[] = {
3418324aa91SJens Axboe 	&queue_requests_entry.attr,
3428324aa91SJens Axboe 	&queue_ra_entry.attr,
3438324aa91SJens Axboe 	&queue_max_hw_sectors_entry.attr,
3448324aa91SJens Axboe 	&queue_max_sectors_entry.attr,
3458324aa91SJens Axboe 	&queue_iosched_entry.attr,
346e68b903cSMartin K. Petersen 	&queue_hw_sector_size_entry.attr,
347e1defc4fSMartin K. Petersen 	&queue_logical_block_size_entry.attr,
348c72758f3SMartin K. Petersen 	&queue_physical_block_size_entry.attr,
349c72758f3SMartin K. Petersen 	&queue_io_min_entry.attr,
350c72758f3SMartin K. Petersen 	&queue_io_opt_entry.attr,
351*86b37281SMartin K. Petersen 	&queue_discard_granularity_entry.attr,
352*86b37281SMartin K. Petersen 	&queue_discard_max_entry.attr,
3531308835fSBartlomiej Zolnierkiewicz 	&queue_nonrot_entry.attr,
354ac9fafa1SAlan D. Brunelle 	&queue_nomerges_entry.attr,
355c7c22e4dSJens Axboe 	&queue_rq_affinity_entry.attr,
356bc58ba94SJens Axboe 	&queue_iostats_entry.attr,
3578324aa91SJens Axboe 	NULL,
3588324aa91SJens Axboe };
3598324aa91SJens Axboe 
3608324aa91SJens Axboe #define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
3618324aa91SJens Axboe 
3628324aa91SJens Axboe static ssize_t
3638324aa91SJens Axboe queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3648324aa91SJens Axboe {
3658324aa91SJens Axboe 	struct queue_sysfs_entry *entry = to_queue(attr);
3668324aa91SJens Axboe 	struct request_queue *q =
3678324aa91SJens Axboe 		container_of(kobj, struct request_queue, kobj);
3688324aa91SJens Axboe 	ssize_t res;
3698324aa91SJens Axboe 
3708324aa91SJens Axboe 	if (!entry->show)
3718324aa91SJens Axboe 		return -EIO;
3728324aa91SJens Axboe 	mutex_lock(&q->sysfs_lock);
3738324aa91SJens Axboe 	if (test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)) {
3748324aa91SJens Axboe 		mutex_unlock(&q->sysfs_lock);
3758324aa91SJens Axboe 		return -ENOENT;
3768324aa91SJens Axboe 	}
3778324aa91SJens Axboe 	res = entry->show(q, page);
3788324aa91SJens Axboe 	mutex_unlock(&q->sysfs_lock);
3798324aa91SJens Axboe 	return res;
3808324aa91SJens Axboe }
3818324aa91SJens Axboe 
3828324aa91SJens Axboe static ssize_t
3838324aa91SJens Axboe queue_attr_store(struct kobject *kobj, struct attribute *attr,
3848324aa91SJens Axboe 		    const char *page, size_t length)
3858324aa91SJens Axboe {
3868324aa91SJens Axboe 	struct queue_sysfs_entry *entry = to_queue(attr);
3876728cb0eSJens Axboe 	struct request_queue *q;
3888324aa91SJens Axboe 	ssize_t res;
3898324aa91SJens Axboe 
3908324aa91SJens Axboe 	if (!entry->store)
3918324aa91SJens Axboe 		return -EIO;
3926728cb0eSJens Axboe 
3936728cb0eSJens Axboe 	q = container_of(kobj, struct request_queue, kobj);
3948324aa91SJens Axboe 	mutex_lock(&q->sysfs_lock);
3958324aa91SJens Axboe 	if (test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)) {
3968324aa91SJens Axboe 		mutex_unlock(&q->sysfs_lock);
3978324aa91SJens Axboe 		return -ENOENT;
3988324aa91SJens Axboe 	}
3998324aa91SJens Axboe 	res = entry->store(q, page, length);
4008324aa91SJens Axboe 	mutex_unlock(&q->sysfs_lock);
4018324aa91SJens Axboe 	return res;
4028324aa91SJens Axboe }
4038324aa91SJens Axboe 
4048324aa91SJens Axboe /**
4058324aa91SJens Axboe  * blk_cleanup_queue: - release a &struct request_queue when it is no longer needed
4068324aa91SJens Axboe  * @kobj:    the kobj belonging of the request queue to be released
4078324aa91SJens Axboe  *
4088324aa91SJens Axboe  * Description:
4098324aa91SJens Axboe  *     blk_cleanup_queue is the pair to blk_init_queue() or
4108324aa91SJens Axboe  *     blk_queue_make_request().  It should be called when a request queue is
4118324aa91SJens Axboe  *     being released; typically when a block device is being de-registered.
4128324aa91SJens Axboe  *     Currently, its primary task it to free all the &struct request
4138324aa91SJens Axboe  *     structures that were allocated to the queue and the queue itself.
4148324aa91SJens Axboe  *
4158324aa91SJens Axboe  * Caveat:
4168324aa91SJens Axboe  *     Hopefully the low level driver will have finished any
4178324aa91SJens Axboe  *     outstanding requests first...
4188324aa91SJens Axboe  **/
4198324aa91SJens Axboe static void blk_release_queue(struct kobject *kobj)
4208324aa91SJens Axboe {
4218324aa91SJens Axboe 	struct request_queue *q =
4228324aa91SJens Axboe 		container_of(kobj, struct request_queue, kobj);
4238324aa91SJens Axboe 	struct request_list *rl = &q->rq;
4248324aa91SJens Axboe 
4258324aa91SJens Axboe 	blk_sync_queue(q);
4268324aa91SJens Axboe 
4278324aa91SJens Axboe 	if (rl->rq_pool)
4288324aa91SJens Axboe 		mempool_destroy(rl->rq_pool);
4298324aa91SJens Axboe 
4308324aa91SJens Axboe 	if (q->queue_tags)
4318324aa91SJens Axboe 		__blk_queue_free_tags(q);
4328324aa91SJens Axboe 
4338324aa91SJens Axboe 	blk_trace_shutdown(q);
4348324aa91SJens Axboe 
4358324aa91SJens Axboe 	bdi_destroy(&q->backing_dev_info);
4368324aa91SJens Axboe 	kmem_cache_free(blk_requestq_cachep, q);
4378324aa91SJens Axboe }
4388324aa91SJens Axboe 
4398324aa91SJens Axboe static struct sysfs_ops queue_sysfs_ops = {
4408324aa91SJens Axboe 	.show	= queue_attr_show,
4418324aa91SJens Axboe 	.store	= queue_attr_store,
4428324aa91SJens Axboe };
4438324aa91SJens Axboe 
4448324aa91SJens Axboe struct kobj_type blk_queue_ktype = {
4458324aa91SJens Axboe 	.sysfs_ops	= &queue_sysfs_ops,
4468324aa91SJens Axboe 	.default_attrs	= default_attrs,
4478324aa91SJens Axboe 	.release	= blk_release_queue,
4488324aa91SJens Axboe };
4498324aa91SJens Axboe 
4508324aa91SJens Axboe int blk_register_queue(struct gendisk *disk)
4518324aa91SJens Axboe {
4528324aa91SJens Axboe 	int ret;
4531d54ad6dSLi Zefan 	struct device *dev = disk_to_dev(disk);
4548324aa91SJens Axboe 
4558324aa91SJens Axboe 	struct request_queue *q = disk->queue;
4568324aa91SJens Axboe 
457fb199746SAkinobu Mita 	if (WARN_ON(!q))
4588324aa91SJens Axboe 		return -ENXIO;
4598324aa91SJens Axboe 
4601d54ad6dSLi Zefan 	ret = blk_trace_init_sysfs(dev);
4611d54ad6dSLi Zefan 	if (ret)
4621d54ad6dSLi Zefan 		return ret;
4631d54ad6dSLi Zefan 
464c9059598SLinus Torvalds 	ret = kobject_add(&q->kobj, kobject_get(&dev->kobj), "%s", "queue");
4658324aa91SJens Axboe 	if (ret < 0)
4668324aa91SJens Axboe 		return ret;
4678324aa91SJens Axboe 
4688324aa91SJens Axboe 	kobject_uevent(&q->kobj, KOBJ_ADD);
4698324aa91SJens Axboe 
470cd43e26fSMartin K. Petersen 	if (!q->request_fn)
471cd43e26fSMartin K. Petersen 		return 0;
472cd43e26fSMartin K. Petersen 
4738324aa91SJens Axboe 	ret = elv_register_queue(q);
4748324aa91SJens Axboe 	if (ret) {
4758324aa91SJens Axboe 		kobject_uevent(&q->kobj, KOBJ_REMOVE);
4768324aa91SJens Axboe 		kobject_del(&q->kobj);
47748c0d4d4SZdenek Kabelac 		blk_trace_remove_sysfs(disk_to_dev(disk));
4788324aa91SJens Axboe 		return ret;
4798324aa91SJens Axboe 	}
4808324aa91SJens Axboe 
4818324aa91SJens Axboe 	return 0;
4828324aa91SJens Axboe }
4838324aa91SJens Axboe 
4848324aa91SJens Axboe void blk_unregister_queue(struct gendisk *disk)
4858324aa91SJens Axboe {
4868324aa91SJens Axboe 	struct request_queue *q = disk->queue;
4878324aa91SJens Axboe 
488fb199746SAkinobu Mita 	if (WARN_ON(!q))
489fb199746SAkinobu Mita 		return;
490fb199746SAkinobu Mita 
49148c0d4d4SZdenek Kabelac 	if (q->request_fn)
4928324aa91SJens Axboe 		elv_unregister_queue(q);
4938324aa91SJens Axboe 
4948324aa91SJens Axboe 	kobject_uevent(&q->kobj, KOBJ_REMOVE);
4958324aa91SJens Axboe 	kobject_del(&q->kobj);
49648c0d4d4SZdenek Kabelac 	blk_trace_remove_sysfs(disk_to_dev(disk));
497ed9e1982STejun Heo 	kobject_put(&disk_to_dev(disk)->kobj);
4988324aa91SJens Axboe }
499