xref: /linux/block/blk-sysfs.c (revision b1f3b64d76cf88cc250e5cdd1de783ba9737078e)
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"
125efd6113STejun Heo #include "blk-cgroup.h"
138324aa91SJens Axboe 
148324aa91SJens Axboe struct queue_sysfs_entry {
158324aa91SJens Axboe 	struct attribute attr;
168324aa91SJens Axboe 	ssize_t (*show)(struct request_queue *, char *);
178324aa91SJens Axboe 	ssize_t (*store)(struct request_queue *, const char *, size_t);
188324aa91SJens Axboe };
198324aa91SJens Axboe 
208324aa91SJens Axboe static ssize_t
219cb308ceSXiaotian Feng queue_var_show(unsigned long var, char *page)
228324aa91SJens Axboe {
239cb308ceSXiaotian Feng 	return sprintf(page, "%lu\n", var);
248324aa91SJens Axboe }
258324aa91SJens Axboe 
268324aa91SJens Axboe static ssize_t
278324aa91SJens Axboe queue_var_store(unsigned long *var, const char *page, size_t count)
288324aa91SJens Axboe {
29*b1f3b64dSDave Reisner 	int err;
30*b1f3b64dSDave Reisner 	unsigned long v;
318324aa91SJens Axboe 
32*b1f3b64dSDave Reisner 	err = strict_strtoul(page, 10, &v);
33*b1f3b64dSDave Reisner 	if (err || v > UINT_MAX)
34*b1f3b64dSDave Reisner 		return -EINVAL;
35*b1f3b64dSDave Reisner 
36*b1f3b64dSDave Reisner 	*var = v;
37*b1f3b64dSDave Reisner 
388324aa91SJens Axboe 	return count;
398324aa91SJens Axboe }
408324aa91SJens Axboe 
418324aa91SJens Axboe static ssize_t queue_requests_show(struct request_queue *q, char *page)
428324aa91SJens Axboe {
438324aa91SJens Axboe 	return queue_var_show(q->nr_requests, (page));
448324aa91SJens Axboe }
458324aa91SJens Axboe 
468324aa91SJens Axboe static ssize_t
478324aa91SJens Axboe queue_requests_store(struct request_queue *q, const char *page, size_t count)
488324aa91SJens Axboe {
49a051661cSTejun Heo 	struct request_list *rl;
508324aa91SJens Axboe 	unsigned long nr;
51b8a9ae77SJens Axboe 	int ret;
52b8a9ae77SJens Axboe 
53b8a9ae77SJens Axboe 	if (!q->request_fn)
54b8a9ae77SJens Axboe 		return -EINVAL;
55b8a9ae77SJens Axboe 
56b8a9ae77SJens Axboe 	ret = queue_var_store(&nr, page, count);
57*b1f3b64dSDave Reisner 	if (ret < 0)
58*b1f3b64dSDave Reisner 		return ret;
59*b1f3b64dSDave Reisner 
608324aa91SJens Axboe 	if (nr < BLKDEV_MIN_RQ)
618324aa91SJens Axboe 		nr = BLKDEV_MIN_RQ;
628324aa91SJens Axboe 
638324aa91SJens Axboe 	spin_lock_irq(q->queue_lock);
648324aa91SJens Axboe 	q->nr_requests = nr;
658324aa91SJens Axboe 	blk_queue_congestion_threshold(q);
668324aa91SJens Axboe 
67a051661cSTejun Heo 	/* congestion isn't cgroup aware and follows root blkcg for now */
68a051661cSTejun Heo 	rl = &q->root_rl;
69a051661cSTejun Heo 
701faa16d2SJens Axboe 	if (rl->count[BLK_RW_SYNC] >= queue_congestion_on_threshold(q))
711faa16d2SJens Axboe 		blk_set_queue_congested(q, BLK_RW_SYNC);
721faa16d2SJens Axboe 	else if (rl->count[BLK_RW_SYNC] < queue_congestion_off_threshold(q))
731faa16d2SJens Axboe 		blk_clear_queue_congested(q, BLK_RW_SYNC);
748324aa91SJens Axboe 
751faa16d2SJens Axboe 	if (rl->count[BLK_RW_ASYNC] >= queue_congestion_on_threshold(q))
761faa16d2SJens Axboe 		blk_set_queue_congested(q, BLK_RW_ASYNC);
771faa16d2SJens Axboe 	else if (rl->count[BLK_RW_ASYNC] < queue_congestion_off_threshold(q))
781faa16d2SJens Axboe 		blk_clear_queue_congested(q, BLK_RW_ASYNC);
798324aa91SJens Axboe 
80a051661cSTejun Heo 	blk_queue_for_each_rl(rl, q) {
811faa16d2SJens Axboe 		if (rl->count[BLK_RW_SYNC] >= q->nr_requests) {
825b788ce3STejun Heo 			blk_set_rl_full(rl, BLK_RW_SYNC);
8360735b63STao Ma 		} else {
845b788ce3STejun Heo 			blk_clear_rl_full(rl, BLK_RW_SYNC);
851faa16d2SJens Axboe 			wake_up(&rl->wait[BLK_RW_SYNC]);
868324aa91SJens Axboe 		}
878324aa91SJens Axboe 
881faa16d2SJens Axboe 		if (rl->count[BLK_RW_ASYNC] >= q->nr_requests) {
895b788ce3STejun Heo 			blk_set_rl_full(rl, BLK_RW_ASYNC);
9060735b63STao Ma 		} else {
915b788ce3STejun Heo 			blk_clear_rl_full(rl, BLK_RW_ASYNC);
921faa16d2SJens Axboe 			wake_up(&rl->wait[BLK_RW_ASYNC]);
938324aa91SJens Axboe 		}
94a051661cSTejun Heo 	}
95a051661cSTejun Heo 
968324aa91SJens Axboe 	spin_unlock_irq(q->queue_lock);
978324aa91SJens Axboe 	return ret;
988324aa91SJens Axboe }
998324aa91SJens Axboe 
1008324aa91SJens Axboe static ssize_t queue_ra_show(struct request_queue *q, char *page)
1018324aa91SJens Axboe {
1029cb308ceSXiaotian Feng 	unsigned long ra_kb = q->backing_dev_info.ra_pages <<
1039cb308ceSXiaotian Feng 					(PAGE_CACHE_SHIFT - 10);
1048324aa91SJens Axboe 
1058324aa91SJens Axboe 	return queue_var_show(ra_kb, (page));
1068324aa91SJens Axboe }
1078324aa91SJens Axboe 
1088324aa91SJens Axboe static ssize_t
1098324aa91SJens Axboe queue_ra_store(struct request_queue *q, const char *page, size_t count)
1108324aa91SJens Axboe {
1118324aa91SJens Axboe 	unsigned long ra_kb;
1128324aa91SJens Axboe 	ssize_t ret = queue_var_store(&ra_kb, page, count);
1138324aa91SJens Axboe 
114*b1f3b64dSDave Reisner 	if (ret < 0)
115*b1f3b64dSDave Reisner 		return ret;
116*b1f3b64dSDave Reisner 
1178324aa91SJens Axboe 	q->backing_dev_info.ra_pages = ra_kb >> (PAGE_CACHE_SHIFT - 10);
1188324aa91SJens Axboe 
1198324aa91SJens Axboe 	return ret;
1208324aa91SJens Axboe }
1218324aa91SJens Axboe 
1228324aa91SJens Axboe static ssize_t queue_max_sectors_show(struct request_queue *q, char *page)
1238324aa91SJens Axboe {
124ae03bf63SMartin K. Petersen 	int max_sectors_kb = queue_max_sectors(q) >> 1;
1258324aa91SJens Axboe 
1268324aa91SJens Axboe 	return queue_var_show(max_sectors_kb, (page));
1278324aa91SJens Axboe }
1288324aa91SJens Axboe 
129c77a5710SMartin K. Petersen static ssize_t queue_max_segments_show(struct request_queue *q, char *page)
130c77a5710SMartin K. Petersen {
131c77a5710SMartin K. Petersen 	return queue_var_show(queue_max_segments(q), (page));
132c77a5710SMartin K. Petersen }
133c77a5710SMartin K. Petersen 
13413f05c8dSMartin K. Petersen static ssize_t queue_max_integrity_segments_show(struct request_queue *q, char *page)
13513f05c8dSMartin K. Petersen {
13613f05c8dSMartin K. Petersen 	return queue_var_show(q->limits.max_integrity_segments, (page));
13713f05c8dSMartin K. Petersen }
13813f05c8dSMartin K. Petersen 
139c77a5710SMartin K. Petersen static ssize_t queue_max_segment_size_show(struct request_queue *q, char *page)
140c77a5710SMartin K. Petersen {
141e692cb66SMartin K. Petersen 	if (blk_queue_cluster(q))
142c77a5710SMartin K. Petersen 		return queue_var_show(queue_max_segment_size(q), (page));
143c77a5710SMartin K. Petersen 
144c77a5710SMartin K. Petersen 	return queue_var_show(PAGE_CACHE_SIZE, (page));
145c77a5710SMartin K. Petersen }
146c77a5710SMartin K. Petersen 
147e1defc4fSMartin K. Petersen static ssize_t queue_logical_block_size_show(struct request_queue *q, char *page)
148e68b903cSMartin K. Petersen {
149e1defc4fSMartin K. Petersen 	return queue_var_show(queue_logical_block_size(q), page);
150e68b903cSMartin K. Petersen }
151e68b903cSMartin K. Petersen 
152c72758f3SMartin K. Petersen static ssize_t queue_physical_block_size_show(struct request_queue *q, char *page)
153c72758f3SMartin K. Petersen {
154c72758f3SMartin K. Petersen 	return queue_var_show(queue_physical_block_size(q), page);
155c72758f3SMartin K. Petersen }
156c72758f3SMartin K. Petersen 
157c72758f3SMartin K. Petersen static ssize_t queue_io_min_show(struct request_queue *q, char *page)
158c72758f3SMartin K. Petersen {
159c72758f3SMartin K. Petersen 	return queue_var_show(queue_io_min(q), page);
160c72758f3SMartin K. Petersen }
161c72758f3SMartin K. Petersen 
162c72758f3SMartin K. Petersen static ssize_t queue_io_opt_show(struct request_queue *q, char *page)
163c72758f3SMartin K. Petersen {
164c72758f3SMartin K. Petersen 	return queue_var_show(queue_io_opt(q), page);
1658324aa91SJens Axboe }
1668324aa91SJens Axboe 
16786b37281SMartin K. Petersen static ssize_t queue_discard_granularity_show(struct request_queue *q, char *page)
16886b37281SMartin K. Petersen {
16986b37281SMartin K. Petersen 	return queue_var_show(q->limits.discard_granularity, page);
17086b37281SMartin K. Petersen }
17186b37281SMartin K. Petersen 
17286b37281SMartin K. Petersen static ssize_t queue_discard_max_show(struct request_queue *q, char *page)
17386b37281SMartin K. Petersen {
174a934a00aSMartin K. Petersen 	return sprintf(page, "%llu\n",
175a934a00aSMartin K. Petersen 		       (unsigned long long)q->limits.max_discard_sectors << 9);
17686b37281SMartin K. Petersen }
17786b37281SMartin K. Petersen 
17898262f27SMartin K. Petersen static ssize_t queue_discard_zeroes_data_show(struct request_queue *q, char *page)
17998262f27SMartin K. Petersen {
18098262f27SMartin K. Petersen 	return queue_var_show(queue_discard_zeroes_data(q), page);
18198262f27SMartin K. Petersen }
18298262f27SMartin K. Petersen 
1838324aa91SJens Axboe static ssize_t
1848324aa91SJens Axboe queue_max_sectors_store(struct request_queue *q, const char *page, size_t count)
1858324aa91SJens Axboe {
1868324aa91SJens Axboe 	unsigned long max_sectors_kb,
187ae03bf63SMartin K. Petersen 		max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1,
1888324aa91SJens Axboe 			page_kb = 1 << (PAGE_CACHE_SHIFT - 10);
1898324aa91SJens Axboe 	ssize_t ret = queue_var_store(&max_sectors_kb, page, count);
1908324aa91SJens Axboe 
191*b1f3b64dSDave Reisner 	if (ret < 0)
192*b1f3b64dSDave Reisner 		return ret;
193*b1f3b64dSDave Reisner 
1948324aa91SJens Axboe 	if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb)
1958324aa91SJens Axboe 		return -EINVAL;
1967c239517SWu Fengguang 
1978324aa91SJens Axboe 	spin_lock_irq(q->queue_lock);
198c295fc05SNikanth Karthikesan 	q->limits.max_sectors = max_sectors_kb << 1;
1998324aa91SJens Axboe 	spin_unlock_irq(q->queue_lock);
2008324aa91SJens Axboe 
2018324aa91SJens Axboe 	return ret;
2028324aa91SJens Axboe }
2038324aa91SJens Axboe 
2048324aa91SJens Axboe static ssize_t queue_max_hw_sectors_show(struct request_queue *q, char *page)
2058324aa91SJens Axboe {
206ae03bf63SMartin K. Petersen 	int max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1;
2078324aa91SJens Axboe 
2088324aa91SJens Axboe 	return queue_var_show(max_hw_sectors_kb, (page));
2098324aa91SJens Axboe }
2108324aa91SJens Axboe 
211956bcb7cSJens Axboe #define QUEUE_SYSFS_BIT_FNS(name, flag, neg)				\
212956bcb7cSJens Axboe static ssize_t								\
213956bcb7cSJens Axboe queue_show_##name(struct request_queue *q, char *page)			\
214956bcb7cSJens Axboe {									\
215956bcb7cSJens Axboe 	int bit;							\
216956bcb7cSJens Axboe 	bit = test_bit(QUEUE_FLAG_##flag, &q->queue_flags);		\
217956bcb7cSJens Axboe 	return queue_var_show(neg ? !bit : bit, page);			\
218956bcb7cSJens Axboe }									\
219956bcb7cSJens Axboe static ssize_t								\
220956bcb7cSJens Axboe queue_store_##name(struct request_queue *q, const char *page, size_t count) \
221956bcb7cSJens Axboe {									\
222956bcb7cSJens Axboe 	unsigned long val;						\
223956bcb7cSJens Axboe 	ssize_t ret;							\
224956bcb7cSJens Axboe 	ret = queue_var_store(&val, page, count);			\
225956bcb7cSJens Axboe 	if (neg)							\
226956bcb7cSJens Axboe 		val = !val;						\
227956bcb7cSJens Axboe 									\
228956bcb7cSJens Axboe 	spin_lock_irq(q->queue_lock);					\
229956bcb7cSJens Axboe 	if (val)							\
230956bcb7cSJens Axboe 		queue_flag_set(QUEUE_FLAG_##flag, q);			\
231956bcb7cSJens Axboe 	else								\
232956bcb7cSJens Axboe 		queue_flag_clear(QUEUE_FLAG_##flag, q);			\
233956bcb7cSJens Axboe 	spin_unlock_irq(q->queue_lock);					\
234956bcb7cSJens Axboe 	return ret;							\
2351308835fSBartlomiej Zolnierkiewicz }
2361308835fSBartlomiej Zolnierkiewicz 
237956bcb7cSJens Axboe QUEUE_SYSFS_BIT_FNS(nonrot, NONROT, 1);
238956bcb7cSJens Axboe QUEUE_SYSFS_BIT_FNS(random, ADD_RANDOM, 0);
239956bcb7cSJens Axboe QUEUE_SYSFS_BIT_FNS(iostats, IO_STAT, 0);
240956bcb7cSJens Axboe #undef QUEUE_SYSFS_BIT_FNS
2411308835fSBartlomiej Zolnierkiewicz 
242ac9fafa1SAlan D. Brunelle static ssize_t queue_nomerges_show(struct request_queue *q, char *page)
243ac9fafa1SAlan D. Brunelle {
244488991e2SAlan D. Brunelle 	return queue_var_show((blk_queue_nomerges(q) << 1) |
245488991e2SAlan D. Brunelle 			       blk_queue_noxmerges(q), page);
246ac9fafa1SAlan D. Brunelle }
247ac9fafa1SAlan D. Brunelle 
248ac9fafa1SAlan D. Brunelle static ssize_t queue_nomerges_store(struct request_queue *q, const char *page,
249ac9fafa1SAlan D. Brunelle 				    size_t count)
250ac9fafa1SAlan D. Brunelle {
251ac9fafa1SAlan D. Brunelle 	unsigned long nm;
252ac9fafa1SAlan D. Brunelle 	ssize_t ret = queue_var_store(&nm, page, count);
253ac9fafa1SAlan D. Brunelle 
254*b1f3b64dSDave Reisner 	if (ret < 0)
255*b1f3b64dSDave Reisner 		return ret;
256*b1f3b64dSDave Reisner 
257bf0f9702SJens Axboe 	spin_lock_irq(q->queue_lock);
258bf0f9702SJens Axboe 	queue_flag_clear(QUEUE_FLAG_NOMERGES, q);
259488991e2SAlan D. Brunelle 	queue_flag_clear(QUEUE_FLAG_NOXMERGES, q);
260488991e2SAlan D. Brunelle 	if (nm == 2)
261488991e2SAlan D. Brunelle 		queue_flag_set(QUEUE_FLAG_NOMERGES, q);
262488991e2SAlan D. Brunelle 	else if (nm)
263488991e2SAlan D. Brunelle 		queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
264bf0f9702SJens Axboe 	spin_unlock_irq(q->queue_lock);
2651308835fSBartlomiej Zolnierkiewicz 
266ac9fafa1SAlan D. Brunelle 	return ret;
267ac9fafa1SAlan D. Brunelle }
268ac9fafa1SAlan D. Brunelle 
269c7c22e4dSJens Axboe static ssize_t queue_rq_affinity_show(struct request_queue *q, char *page)
270c7c22e4dSJens Axboe {
2719cb308ceSXiaotian Feng 	bool set = test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags);
2725757a6d7SDan Williams 	bool force = test_bit(QUEUE_FLAG_SAME_FORCE, &q->queue_flags);
273c7c22e4dSJens Axboe 
2745757a6d7SDan Williams 	return queue_var_show(set << force, page);
275c7c22e4dSJens Axboe }
276c7c22e4dSJens Axboe 
277c7c22e4dSJens Axboe static ssize_t
278c7c22e4dSJens Axboe queue_rq_affinity_store(struct request_queue *q, const char *page, size_t count)
279c7c22e4dSJens Axboe {
280c7c22e4dSJens Axboe 	ssize_t ret = -EINVAL;
281c7c22e4dSJens Axboe #if defined(CONFIG_USE_GENERIC_SMP_HELPERS)
282c7c22e4dSJens Axboe 	unsigned long val;
283c7c22e4dSJens Axboe 
284c7c22e4dSJens Axboe 	ret = queue_var_store(&val, page, count);
285*b1f3b64dSDave Reisner 	if (ret < 0)
286*b1f3b64dSDave Reisner 		return ret;
287*b1f3b64dSDave Reisner 
288c7c22e4dSJens Axboe 	spin_lock_irq(q->queue_lock);
289e8037d49SEric Seppanen 	if (val == 2) {
290c7c22e4dSJens Axboe 		queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
2915757a6d7SDan Williams 		queue_flag_set(QUEUE_FLAG_SAME_FORCE, q);
292e8037d49SEric Seppanen 	} else if (val == 1) {
293e8037d49SEric Seppanen 		queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
294e8037d49SEric Seppanen 		queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
295e8037d49SEric Seppanen 	} else if (val == 0) {
296c7c22e4dSJens Axboe 		queue_flag_clear(QUEUE_FLAG_SAME_COMP, q);
2975757a6d7SDan Williams 		queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
2985757a6d7SDan Williams 	}
299c7c22e4dSJens Axboe 	spin_unlock_irq(q->queue_lock);
300c7c22e4dSJens Axboe #endif
301c7c22e4dSJens Axboe 	return ret;
302c7c22e4dSJens Axboe }
3038324aa91SJens Axboe 
3048324aa91SJens Axboe static struct queue_sysfs_entry queue_requests_entry = {
3058324aa91SJens Axboe 	.attr = {.name = "nr_requests", .mode = S_IRUGO | S_IWUSR },
3068324aa91SJens Axboe 	.show = queue_requests_show,
3078324aa91SJens Axboe 	.store = queue_requests_store,
3088324aa91SJens Axboe };
3098324aa91SJens Axboe 
3108324aa91SJens Axboe static struct queue_sysfs_entry queue_ra_entry = {
3118324aa91SJens Axboe 	.attr = {.name = "read_ahead_kb", .mode = S_IRUGO | S_IWUSR },
3128324aa91SJens Axboe 	.show = queue_ra_show,
3138324aa91SJens Axboe 	.store = queue_ra_store,
3148324aa91SJens Axboe };
3158324aa91SJens Axboe 
3168324aa91SJens Axboe static struct queue_sysfs_entry queue_max_sectors_entry = {
3178324aa91SJens Axboe 	.attr = {.name = "max_sectors_kb", .mode = S_IRUGO | S_IWUSR },
3188324aa91SJens Axboe 	.show = queue_max_sectors_show,
3198324aa91SJens Axboe 	.store = queue_max_sectors_store,
3208324aa91SJens Axboe };
3218324aa91SJens Axboe 
3228324aa91SJens Axboe static struct queue_sysfs_entry queue_max_hw_sectors_entry = {
3238324aa91SJens Axboe 	.attr = {.name = "max_hw_sectors_kb", .mode = S_IRUGO },
3248324aa91SJens Axboe 	.show = queue_max_hw_sectors_show,
3258324aa91SJens Axboe };
3268324aa91SJens Axboe 
327c77a5710SMartin K. Petersen static struct queue_sysfs_entry queue_max_segments_entry = {
328c77a5710SMartin K. Petersen 	.attr = {.name = "max_segments", .mode = S_IRUGO },
329c77a5710SMartin K. Petersen 	.show = queue_max_segments_show,
330c77a5710SMartin K. Petersen };
331c77a5710SMartin K. Petersen 
33213f05c8dSMartin K. Petersen static struct queue_sysfs_entry queue_max_integrity_segments_entry = {
33313f05c8dSMartin K. Petersen 	.attr = {.name = "max_integrity_segments", .mode = S_IRUGO },
33413f05c8dSMartin K. Petersen 	.show = queue_max_integrity_segments_show,
33513f05c8dSMartin K. Petersen };
33613f05c8dSMartin K. Petersen 
337c77a5710SMartin K. Petersen static struct queue_sysfs_entry queue_max_segment_size_entry = {
338c77a5710SMartin K. Petersen 	.attr = {.name = "max_segment_size", .mode = S_IRUGO },
339c77a5710SMartin K. Petersen 	.show = queue_max_segment_size_show,
340c77a5710SMartin K. Petersen };
341c77a5710SMartin K. Petersen 
3428324aa91SJens Axboe static struct queue_sysfs_entry queue_iosched_entry = {
3438324aa91SJens Axboe 	.attr = {.name = "scheduler", .mode = S_IRUGO | S_IWUSR },
3448324aa91SJens Axboe 	.show = elv_iosched_show,
3458324aa91SJens Axboe 	.store = elv_iosched_store,
3468324aa91SJens Axboe };
3478324aa91SJens Axboe 
348e68b903cSMartin K. Petersen static struct queue_sysfs_entry queue_hw_sector_size_entry = {
349e68b903cSMartin K. Petersen 	.attr = {.name = "hw_sector_size", .mode = S_IRUGO },
350e1defc4fSMartin K. Petersen 	.show = queue_logical_block_size_show,
351e1defc4fSMartin K. Petersen };
352e1defc4fSMartin K. Petersen 
353e1defc4fSMartin K. Petersen static struct queue_sysfs_entry queue_logical_block_size_entry = {
354e1defc4fSMartin K. Petersen 	.attr = {.name = "logical_block_size", .mode = S_IRUGO },
355e1defc4fSMartin K. Petersen 	.show = queue_logical_block_size_show,
356e68b903cSMartin K. Petersen };
357e68b903cSMartin K. Petersen 
358c72758f3SMartin K. Petersen static struct queue_sysfs_entry queue_physical_block_size_entry = {
359c72758f3SMartin K. Petersen 	.attr = {.name = "physical_block_size", .mode = S_IRUGO },
360c72758f3SMartin K. Petersen 	.show = queue_physical_block_size_show,
361c72758f3SMartin K. Petersen };
362c72758f3SMartin K. Petersen 
363c72758f3SMartin K. Petersen static struct queue_sysfs_entry queue_io_min_entry = {
364c72758f3SMartin K. Petersen 	.attr = {.name = "minimum_io_size", .mode = S_IRUGO },
365c72758f3SMartin K. Petersen 	.show = queue_io_min_show,
366c72758f3SMartin K. Petersen };
367c72758f3SMartin K. Petersen 
368c72758f3SMartin K. Petersen static struct queue_sysfs_entry queue_io_opt_entry = {
369c72758f3SMartin K. Petersen 	.attr = {.name = "optimal_io_size", .mode = S_IRUGO },
370c72758f3SMartin K. Petersen 	.show = queue_io_opt_show,
3718324aa91SJens Axboe };
3728324aa91SJens Axboe 
37386b37281SMartin K. Petersen static struct queue_sysfs_entry queue_discard_granularity_entry = {
37486b37281SMartin K. Petersen 	.attr = {.name = "discard_granularity", .mode = S_IRUGO },
37586b37281SMartin K. Petersen 	.show = queue_discard_granularity_show,
37686b37281SMartin K. Petersen };
37786b37281SMartin K. Petersen 
37886b37281SMartin K. Petersen static struct queue_sysfs_entry queue_discard_max_entry = {
37986b37281SMartin K. Petersen 	.attr = {.name = "discard_max_bytes", .mode = S_IRUGO },
38086b37281SMartin K. Petersen 	.show = queue_discard_max_show,
38186b37281SMartin K. Petersen };
38286b37281SMartin K. Petersen 
38398262f27SMartin K. Petersen static struct queue_sysfs_entry queue_discard_zeroes_data_entry = {
38498262f27SMartin K. Petersen 	.attr = {.name = "discard_zeroes_data", .mode = S_IRUGO },
38598262f27SMartin K. Petersen 	.show = queue_discard_zeroes_data_show,
38698262f27SMartin K. Petersen };
38798262f27SMartin K. Petersen 
3881308835fSBartlomiej Zolnierkiewicz static struct queue_sysfs_entry queue_nonrot_entry = {
3891308835fSBartlomiej Zolnierkiewicz 	.attr = {.name = "rotational", .mode = S_IRUGO | S_IWUSR },
390956bcb7cSJens Axboe 	.show = queue_show_nonrot,
391956bcb7cSJens Axboe 	.store = queue_store_nonrot,
3921308835fSBartlomiej Zolnierkiewicz };
3931308835fSBartlomiej Zolnierkiewicz 
394ac9fafa1SAlan D. Brunelle static struct queue_sysfs_entry queue_nomerges_entry = {
395ac9fafa1SAlan D. Brunelle 	.attr = {.name = "nomerges", .mode = S_IRUGO | S_IWUSR },
396ac9fafa1SAlan D. Brunelle 	.show = queue_nomerges_show,
397ac9fafa1SAlan D. Brunelle 	.store = queue_nomerges_store,
398ac9fafa1SAlan D. Brunelle };
399ac9fafa1SAlan D. Brunelle 
400c7c22e4dSJens Axboe static struct queue_sysfs_entry queue_rq_affinity_entry = {
401c7c22e4dSJens Axboe 	.attr = {.name = "rq_affinity", .mode = S_IRUGO | S_IWUSR },
402c7c22e4dSJens Axboe 	.show = queue_rq_affinity_show,
403c7c22e4dSJens Axboe 	.store = queue_rq_affinity_store,
404c7c22e4dSJens Axboe };
405c7c22e4dSJens Axboe 
406bc58ba94SJens Axboe static struct queue_sysfs_entry queue_iostats_entry = {
407bc58ba94SJens Axboe 	.attr = {.name = "iostats", .mode = S_IRUGO | S_IWUSR },
408956bcb7cSJens Axboe 	.show = queue_show_iostats,
409956bcb7cSJens Axboe 	.store = queue_store_iostats,
410bc58ba94SJens Axboe };
411bc58ba94SJens Axboe 
412e2e1a148SJens Axboe static struct queue_sysfs_entry queue_random_entry = {
413e2e1a148SJens Axboe 	.attr = {.name = "add_random", .mode = S_IRUGO | S_IWUSR },
414956bcb7cSJens Axboe 	.show = queue_show_random,
415956bcb7cSJens Axboe 	.store = queue_store_random,
416e2e1a148SJens Axboe };
417e2e1a148SJens Axboe 
4188324aa91SJens Axboe static struct attribute *default_attrs[] = {
4198324aa91SJens Axboe 	&queue_requests_entry.attr,
4208324aa91SJens Axboe 	&queue_ra_entry.attr,
4218324aa91SJens Axboe 	&queue_max_hw_sectors_entry.attr,
4228324aa91SJens Axboe 	&queue_max_sectors_entry.attr,
423c77a5710SMartin K. Petersen 	&queue_max_segments_entry.attr,
42413f05c8dSMartin K. Petersen 	&queue_max_integrity_segments_entry.attr,
425c77a5710SMartin K. Petersen 	&queue_max_segment_size_entry.attr,
4268324aa91SJens Axboe 	&queue_iosched_entry.attr,
427e68b903cSMartin K. Petersen 	&queue_hw_sector_size_entry.attr,
428e1defc4fSMartin K. Petersen 	&queue_logical_block_size_entry.attr,
429c72758f3SMartin K. Petersen 	&queue_physical_block_size_entry.attr,
430c72758f3SMartin K. Petersen 	&queue_io_min_entry.attr,
431c72758f3SMartin K. Petersen 	&queue_io_opt_entry.attr,
43286b37281SMartin K. Petersen 	&queue_discard_granularity_entry.attr,
43386b37281SMartin K. Petersen 	&queue_discard_max_entry.attr,
43498262f27SMartin K. Petersen 	&queue_discard_zeroes_data_entry.attr,
4351308835fSBartlomiej Zolnierkiewicz 	&queue_nonrot_entry.attr,
436ac9fafa1SAlan D. Brunelle 	&queue_nomerges_entry.attr,
437c7c22e4dSJens Axboe 	&queue_rq_affinity_entry.attr,
438bc58ba94SJens Axboe 	&queue_iostats_entry.attr,
439e2e1a148SJens Axboe 	&queue_random_entry.attr,
4408324aa91SJens Axboe 	NULL,
4418324aa91SJens Axboe };
4428324aa91SJens Axboe 
4438324aa91SJens Axboe #define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
4448324aa91SJens Axboe 
4458324aa91SJens Axboe static ssize_t
4468324aa91SJens Axboe queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4478324aa91SJens Axboe {
4488324aa91SJens Axboe 	struct queue_sysfs_entry *entry = to_queue(attr);
4498324aa91SJens Axboe 	struct request_queue *q =
4508324aa91SJens Axboe 		container_of(kobj, struct request_queue, kobj);
4518324aa91SJens Axboe 	ssize_t res;
4528324aa91SJens Axboe 
4538324aa91SJens Axboe 	if (!entry->show)
4548324aa91SJens Axboe 		return -EIO;
4558324aa91SJens Axboe 	mutex_lock(&q->sysfs_lock);
45634f6055cSTejun Heo 	if (blk_queue_dead(q)) {
4578324aa91SJens Axboe 		mutex_unlock(&q->sysfs_lock);
4588324aa91SJens Axboe 		return -ENOENT;
4598324aa91SJens Axboe 	}
4608324aa91SJens Axboe 	res = entry->show(q, page);
4618324aa91SJens Axboe 	mutex_unlock(&q->sysfs_lock);
4628324aa91SJens Axboe 	return res;
4638324aa91SJens Axboe }
4648324aa91SJens Axboe 
4658324aa91SJens Axboe static ssize_t
4668324aa91SJens Axboe queue_attr_store(struct kobject *kobj, struct attribute *attr,
4678324aa91SJens Axboe 		    const char *page, size_t length)
4688324aa91SJens Axboe {
4698324aa91SJens Axboe 	struct queue_sysfs_entry *entry = to_queue(attr);
4706728cb0eSJens Axboe 	struct request_queue *q;
4718324aa91SJens Axboe 	ssize_t res;
4728324aa91SJens Axboe 
4738324aa91SJens Axboe 	if (!entry->store)
4748324aa91SJens Axboe 		return -EIO;
4756728cb0eSJens Axboe 
4766728cb0eSJens Axboe 	q = container_of(kobj, struct request_queue, kobj);
4778324aa91SJens Axboe 	mutex_lock(&q->sysfs_lock);
47834f6055cSTejun Heo 	if (blk_queue_dead(q)) {
4798324aa91SJens Axboe 		mutex_unlock(&q->sysfs_lock);
4808324aa91SJens Axboe 		return -ENOENT;
4818324aa91SJens Axboe 	}
4828324aa91SJens Axboe 	res = entry->store(q, page, length);
4838324aa91SJens Axboe 	mutex_unlock(&q->sysfs_lock);
4848324aa91SJens Axboe 	return res;
4858324aa91SJens Axboe }
4868324aa91SJens Axboe 
4878324aa91SJens Axboe /**
488499337bbSAndrew Morton  * blk_release_queue: - release a &struct request_queue when it is no longer needed
489499337bbSAndrew Morton  * @kobj:    the kobj belonging to the request queue to be released
4908324aa91SJens Axboe  *
4918324aa91SJens Axboe  * Description:
492499337bbSAndrew Morton  *     blk_release_queue is the pair to blk_init_queue() or
4938324aa91SJens Axboe  *     blk_queue_make_request().  It should be called when a request queue is
4948324aa91SJens Axboe  *     being released; typically when a block device is being de-registered.
4958324aa91SJens Axboe  *     Currently, its primary task it to free all the &struct request
4968324aa91SJens Axboe  *     structures that were allocated to the queue and the queue itself.
4978324aa91SJens Axboe  *
4988324aa91SJens Axboe  * Caveat:
4998324aa91SJens Axboe  *     Hopefully the low level driver will have finished any
5008324aa91SJens Axboe  *     outstanding requests first...
5018324aa91SJens Axboe  **/
5028324aa91SJens Axboe static void blk_release_queue(struct kobject *kobj)
5038324aa91SJens Axboe {
5048324aa91SJens Axboe 	struct request_queue *q =
5058324aa91SJens Axboe 		container_of(kobj, struct request_queue, kobj);
5068324aa91SJens Axboe 
5078324aa91SJens Axboe 	blk_sync_queue(q);
5088324aa91SJens Axboe 
509e8989faeSTejun Heo 	blkcg_exit_queue(q);
510e8989faeSTejun Heo 
5117e5a8794STejun Heo 	if (q->elevator) {
5127e5a8794STejun Heo 		spin_lock_irq(q->queue_lock);
5137e5a8794STejun Heo 		ioc_clear_queue(q);
5147e5a8794STejun Heo 		spin_unlock_irq(q->queue_lock);
515777eb1bfSHannes Reinecke 		elevator_exit(q->elevator);
5167e5a8794STejun Heo 	}
517777eb1bfSHannes Reinecke 
518a051661cSTejun Heo 	blk_exit_rl(&q->root_rl);
5198324aa91SJens Axboe 
5208324aa91SJens Axboe 	if (q->queue_tags)
5218324aa91SJens Axboe 		__blk_queue_free_tags(q);
5228324aa91SJens Axboe 
5238324aa91SJens Axboe 	blk_trace_shutdown(q);
5248324aa91SJens Axboe 
5258324aa91SJens Axboe 	bdi_destroy(&q->backing_dev_info);
526a73f730dSTejun Heo 
527a73f730dSTejun Heo 	ida_simple_remove(&blk_queue_ida, q->id);
5288324aa91SJens Axboe 	kmem_cache_free(blk_requestq_cachep, q);
5298324aa91SJens Axboe }
5308324aa91SJens Axboe 
53152cf25d0SEmese Revfy static const struct sysfs_ops queue_sysfs_ops = {
5328324aa91SJens Axboe 	.show	= queue_attr_show,
5338324aa91SJens Axboe 	.store	= queue_attr_store,
5348324aa91SJens Axboe };
5358324aa91SJens Axboe 
5368324aa91SJens Axboe struct kobj_type blk_queue_ktype = {
5378324aa91SJens Axboe 	.sysfs_ops	= &queue_sysfs_ops,
5388324aa91SJens Axboe 	.default_attrs	= default_attrs,
5398324aa91SJens Axboe 	.release	= blk_release_queue,
5408324aa91SJens Axboe };
5418324aa91SJens Axboe 
5428324aa91SJens Axboe int blk_register_queue(struct gendisk *disk)
5438324aa91SJens Axboe {
5448324aa91SJens Axboe 	int ret;
5451d54ad6dSLi Zefan 	struct device *dev = disk_to_dev(disk);
5468324aa91SJens Axboe 	struct request_queue *q = disk->queue;
5478324aa91SJens Axboe 
548fb199746SAkinobu Mita 	if (WARN_ON(!q))
5498324aa91SJens Axboe 		return -ENXIO;
5508324aa91SJens Axboe 
5511d54ad6dSLi Zefan 	ret = blk_trace_init_sysfs(dev);
5521d54ad6dSLi Zefan 	if (ret)
5531d54ad6dSLi Zefan 		return ret;
5541d54ad6dSLi Zefan 
555c9059598SLinus Torvalds 	ret = kobject_add(&q->kobj, kobject_get(&dev->kobj), "%s", "queue");
556ed5302d3SLiu Yuan 	if (ret < 0) {
557ed5302d3SLiu Yuan 		blk_trace_remove_sysfs(dev);
5588324aa91SJens Axboe 		return ret;
559ed5302d3SLiu Yuan 	}
5608324aa91SJens Axboe 
5618324aa91SJens Axboe 	kobject_uevent(&q->kobj, KOBJ_ADD);
5628324aa91SJens Axboe 
563cd43e26fSMartin K. Petersen 	if (!q->request_fn)
564cd43e26fSMartin K. Petersen 		return 0;
565cd43e26fSMartin K. Petersen 
5668324aa91SJens Axboe 	ret = elv_register_queue(q);
5678324aa91SJens Axboe 	if (ret) {
5688324aa91SJens Axboe 		kobject_uevent(&q->kobj, KOBJ_REMOVE);
5698324aa91SJens Axboe 		kobject_del(&q->kobj);
57080656b67SLiu Yuan 		blk_trace_remove_sysfs(dev);
571c87ffbb8SXiaotian Feng 		kobject_put(&dev->kobj);
5728324aa91SJens Axboe 		return ret;
5738324aa91SJens Axboe 	}
5748324aa91SJens Axboe 
5758324aa91SJens Axboe 	return 0;
5768324aa91SJens Axboe }
5778324aa91SJens Axboe 
5788324aa91SJens Axboe void blk_unregister_queue(struct gendisk *disk)
5798324aa91SJens Axboe {
5808324aa91SJens Axboe 	struct request_queue *q = disk->queue;
5818324aa91SJens Axboe 
582fb199746SAkinobu Mita 	if (WARN_ON(!q))
583fb199746SAkinobu Mita 		return;
584fb199746SAkinobu Mita 
58548c0d4d4SZdenek Kabelac 	if (q->request_fn)
5868324aa91SJens Axboe 		elv_unregister_queue(q);
5878324aa91SJens Axboe 
5888324aa91SJens Axboe 	kobject_uevent(&q->kobj, KOBJ_REMOVE);
5898324aa91SJens Axboe 	kobject_del(&q->kobj);
59048c0d4d4SZdenek Kabelac 	blk_trace_remove_sysfs(disk_to_dev(disk));
591ed9e1982STejun Heo 	kobject_put(&disk_to_dev(disk)->kobj);
5928324aa91SJens Axboe }
593