xref: /openbmc/linux/block/blk-sysfs.c (revision 94cdda6b)
1 /*
2  * Functions related to sysfs handling
3  */
4 #include <linux/kernel.h>
5 #include <linux/slab.h>
6 #include <linux/module.h>
7 #include <linux/bio.h>
8 #include <linux/blkdev.h>
9 #include <linux/blktrace_api.h>
10 #include <linux/blk-mq.h>
11 
12 #include "blk.h"
13 #include "blk-cgroup.h"
14 #include "blk-mq.h"
15 
16 struct queue_sysfs_entry {
17 	struct attribute attr;
18 	ssize_t (*show)(struct request_queue *, char *);
19 	ssize_t (*store)(struct request_queue *, const char *, size_t);
20 };
21 
22 static ssize_t
23 queue_var_show(unsigned long var, char *page)
24 {
25 	return sprintf(page, "%lu\n", var);
26 }
27 
28 static ssize_t
29 queue_var_store(unsigned long *var, const char *page, size_t count)
30 {
31 	int err;
32 	unsigned long v;
33 
34 	err = kstrtoul(page, 10, &v);
35 	if (err || v > UINT_MAX)
36 		return -EINVAL;
37 
38 	*var = v;
39 
40 	return count;
41 }
42 
43 static ssize_t queue_requests_show(struct request_queue *q, char *page)
44 {
45 	return queue_var_show(q->nr_requests, (page));
46 }
47 
48 static ssize_t
49 queue_requests_store(struct request_queue *q, const char *page, size_t count)
50 {
51 	unsigned long nr;
52 	int ret, err;
53 
54 	if (!q->request_fn && !q->mq_ops)
55 		return -EINVAL;
56 
57 	ret = queue_var_store(&nr, page, count);
58 	if (ret < 0)
59 		return ret;
60 
61 	if (nr < BLKDEV_MIN_RQ)
62 		nr = BLKDEV_MIN_RQ;
63 
64 	if (q->request_fn)
65 		err = blk_update_nr_requests(q, nr);
66 	else
67 		err = blk_mq_update_nr_requests(q, nr);
68 
69 	if (err)
70 		return err;
71 
72 	return ret;
73 }
74 
75 static ssize_t queue_ra_show(struct request_queue *q, char *page)
76 {
77 	unsigned long ra_kb = q->backing_dev_info.ra_pages <<
78 					(PAGE_CACHE_SHIFT - 10);
79 
80 	return queue_var_show(ra_kb, (page));
81 }
82 
83 static ssize_t
84 queue_ra_store(struct request_queue *q, const char *page, size_t count)
85 {
86 	unsigned long ra_kb;
87 	ssize_t ret = queue_var_store(&ra_kb, page, count);
88 
89 	if (ret < 0)
90 		return ret;
91 
92 	q->backing_dev_info.ra_pages = ra_kb >> (PAGE_CACHE_SHIFT - 10);
93 
94 	return ret;
95 }
96 
97 static ssize_t queue_max_sectors_show(struct request_queue *q, char *page)
98 {
99 	int max_sectors_kb = queue_max_sectors(q) >> 1;
100 
101 	return queue_var_show(max_sectors_kb, (page));
102 }
103 
104 static ssize_t queue_max_segments_show(struct request_queue *q, char *page)
105 {
106 	return queue_var_show(queue_max_segments(q), (page));
107 }
108 
109 static ssize_t queue_max_integrity_segments_show(struct request_queue *q, char *page)
110 {
111 	return queue_var_show(q->limits.max_integrity_segments, (page));
112 }
113 
114 static ssize_t queue_max_segment_size_show(struct request_queue *q, char *page)
115 {
116 	if (blk_queue_cluster(q))
117 		return queue_var_show(queue_max_segment_size(q), (page));
118 
119 	return queue_var_show(PAGE_CACHE_SIZE, (page));
120 }
121 
122 static ssize_t queue_logical_block_size_show(struct request_queue *q, char *page)
123 {
124 	return queue_var_show(queue_logical_block_size(q), page);
125 }
126 
127 static ssize_t queue_physical_block_size_show(struct request_queue *q, char *page)
128 {
129 	return queue_var_show(queue_physical_block_size(q), page);
130 }
131 
132 static ssize_t queue_io_min_show(struct request_queue *q, char *page)
133 {
134 	return queue_var_show(queue_io_min(q), page);
135 }
136 
137 static ssize_t queue_io_opt_show(struct request_queue *q, char *page)
138 {
139 	return queue_var_show(queue_io_opt(q), page);
140 }
141 
142 static ssize_t queue_discard_granularity_show(struct request_queue *q, char *page)
143 {
144 	return queue_var_show(q->limits.discard_granularity, page);
145 }
146 
147 static ssize_t queue_discard_max_show(struct request_queue *q, char *page)
148 {
149 	return sprintf(page, "%llu\n",
150 		       (unsigned long long)q->limits.max_discard_sectors << 9);
151 }
152 
153 static ssize_t queue_discard_zeroes_data_show(struct request_queue *q, char *page)
154 {
155 	return queue_var_show(queue_discard_zeroes_data(q), page);
156 }
157 
158 static ssize_t queue_write_same_max_show(struct request_queue *q, char *page)
159 {
160 	return sprintf(page, "%llu\n",
161 		(unsigned long long)q->limits.max_write_same_sectors << 9);
162 }
163 
164 
165 static ssize_t
166 queue_max_sectors_store(struct request_queue *q, const char *page, size_t count)
167 {
168 	unsigned long max_sectors_kb,
169 		max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1,
170 			page_kb = 1 << (PAGE_CACHE_SHIFT - 10);
171 	ssize_t ret = queue_var_store(&max_sectors_kb, page, count);
172 
173 	if (ret < 0)
174 		return ret;
175 
176 	if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb)
177 		return -EINVAL;
178 
179 	spin_lock_irq(q->queue_lock);
180 	q->limits.max_sectors = max_sectors_kb << 1;
181 	spin_unlock_irq(q->queue_lock);
182 
183 	return ret;
184 }
185 
186 static ssize_t queue_max_hw_sectors_show(struct request_queue *q, char *page)
187 {
188 	int max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1;
189 
190 	return queue_var_show(max_hw_sectors_kb, (page));
191 }
192 
193 #define QUEUE_SYSFS_BIT_FNS(name, flag, neg)				\
194 static ssize_t								\
195 queue_show_##name(struct request_queue *q, char *page)			\
196 {									\
197 	int bit;							\
198 	bit = test_bit(QUEUE_FLAG_##flag, &q->queue_flags);		\
199 	return queue_var_show(neg ? !bit : bit, page);			\
200 }									\
201 static ssize_t								\
202 queue_store_##name(struct request_queue *q, const char *page, size_t count) \
203 {									\
204 	unsigned long val;						\
205 	ssize_t ret;							\
206 	ret = queue_var_store(&val, page, count);			\
207 	if (ret < 0)							\
208 		 return ret;						\
209 	if (neg)							\
210 		val = !val;						\
211 									\
212 	spin_lock_irq(q->queue_lock);					\
213 	if (val)							\
214 		queue_flag_set(QUEUE_FLAG_##flag, q);			\
215 	else								\
216 		queue_flag_clear(QUEUE_FLAG_##flag, q);			\
217 	spin_unlock_irq(q->queue_lock);					\
218 	return ret;							\
219 }
220 
221 QUEUE_SYSFS_BIT_FNS(nonrot, NONROT, 1);
222 QUEUE_SYSFS_BIT_FNS(random, ADD_RANDOM, 0);
223 QUEUE_SYSFS_BIT_FNS(iostats, IO_STAT, 0);
224 #undef QUEUE_SYSFS_BIT_FNS
225 
226 static ssize_t queue_nomerges_show(struct request_queue *q, char *page)
227 {
228 	return queue_var_show((blk_queue_nomerges(q) << 1) |
229 			       blk_queue_noxmerges(q), page);
230 }
231 
232 static ssize_t queue_nomerges_store(struct request_queue *q, const char *page,
233 				    size_t count)
234 {
235 	unsigned long nm;
236 	ssize_t ret = queue_var_store(&nm, page, count);
237 
238 	if (ret < 0)
239 		return ret;
240 
241 	spin_lock_irq(q->queue_lock);
242 	queue_flag_clear(QUEUE_FLAG_NOMERGES, q);
243 	queue_flag_clear(QUEUE_FLAG_NOXMERGES, q);
244 	if (nm == 2)
245 		queue_flag_set(QUEUE_FLAG_NOMERGES, q);
246 	else if (nm)
247 		queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
248 	spin_unlock_irq(q->queue_lock);
249 
250 	return ret;
251 }
252 
253 static ssize_t queue_rq_affinity_show(struct request_queue *q, char *page)
254 {
255 	bool set = test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags);
256 	bool force = test_bit(QUEUE_FLAG_SAME_FORCE, &q->queue_flags);
257 
258 	return queue_var_show(set << force, page);
259 }
260 
261 static ssize_t
262 queue_rq_affinity_store(struct request_queue *q, const char *page, size_t count)
263 {
264 	ssize_t ret = -EINVAL;
265 #ifdef CONFIG_SMP
266 	unsigned long val;
267 
268 	ret = queue_var_store(&val, page, count);
269 	if (ret < 0)
270 		return ret;
271 
272 	spin_lock_irq(q->queue_lock);
273 	if (val == 2) {
274 		queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
275 		queue_flag_set(QUEUE_FLAG_SAME_FORCE, q);
276 	} else if (val == 1) {
277 		queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
278 		queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
279 	} else if (val == 0) {
280 		queue_flag_clear(QUEUE_FLAG_SAME_COMP, q);
281 		queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
282 	}
283 	spin_unlock_irq(q->queue_lock);
284 #endif
285 	return ret;
286 }
287 
288 static struct queue_sysfs_entry queue_requests_entry = {
289 	.attr = {.name = "nr_requests", .mode = S_IRUGO | S_IWUSR },
290 	.show = queue_requests_show,
291 	.store = queue_requests_store,
292 };
293 
294 static struct queue_sysfs_entry queue_ra_entry = {
295 	.attr = {.name = "read_ahead_kb", .mode = S_IRUGO | S_IWUSR },
296 	.show = queue_ra_show,
297 	.store = queue_ra_store,
298 };
299 
300 static struct queue_sysfs_entry queue_max_sectors_entry = {
301 	.attr = {.name = "max_sectors_kb", .mode = S_IRUGO | S_IWUSR },
302 	.show = queue_max_sectors_show,
303 	.store = queue_max_sectors_store,
304 };
305 
306 static struct queue_sysfs_entry queue_max_hw_sectors_entry = {
307 	.attr = {.name = "max_hw_sectors_kb", .mode = S_IRUGO },
308 	.show = queue_max_hw_sectors_show,
309 };
310 
311 static struct queue_sysfs_entry queue_max_segments_entry = {
312 	.attr = {.name = "max_segments", .mode = S_IRUGO },
313 	.show = queue_max_segments_show,
314 };
315 
316 static struct queue_sysfs_entry queue_max_integrity_segments_entry = {
317 	.attr = {.name = "max_integrity_segments", .mode = S_IRUGO },
318 	.show = queue_max_integrity_segments_show,
319 };
320 
321 static struct queue_sysfs_entry queue_max_segment_size_entry = {
322 	.attr = {.name = "max_segment_size", .mode = S_IRUGO },
323 	.show = queue_max_segment_size_show,
324 };
325 
326 static struct queue_sysfs_entry queue_iosched_entry = {
327 	.attr = {.name = "scheduler", .mode = S_IRUGO | S_IWUSR },
328 	.show = elv_iosched_show,
329 	.store = elv_iosched_store,
330 };
331 
332 static struct queue_sysfs_entry queue_hw_sector_size_entry = {
333 	.attr = {.name = "hw_sector_size", .mode = S_IRUGO },
334 	.show = queue_logical_block_size_show,
335 };
336 
337 static struct queue_sysfs_entry queue_logical_block_size_entry = {
338 	.attr = {.name = "logical_block_size", .mode = S_IRUGO },
339 	.show = queue_logical_block_size_show,
340 };
341 
342 static struct queue_sysfs_entry queue_physical_block_size_entry = {
343 	.attr = {.name = "physical_block_size", .mode = S_IRUGO },
344 	.show = queue_physical_block_size_show,
345 };
346 
347 static struct queue_sysfs_entry queue_io_min_entry = {
348 	.attr = {.name = "minimum_io_size", .mode = S_IRUGO },
349 	.show = queue_io_min_show,
350 };
351 
352 static struct queue_sysfs_entry queue_io_opt_entry = {
353 	.attr = {.name = "optimal_io_size", .mode = S_IRUGO },
354 	.show = queue_io_opt_show,
355 };
356 
357 static struct queue_sysfs_entry queue_discard_granularity_entry = {
358 	.attr = {.name = "discard_granularity", .mode = S_IRUGO },
359 	.show = queue_discard_granularity_show,
360 };
361 
362 static struct queue_sysfs_entry queue_discard_max_entry = {
363 	.attr = {.name = "discard_max_bytes", .mode = S_IRUGO },
364 	.show = queue_discard_max_show,
365 };
366 
367 static struct queue_sysfs_entry queue_discard_zeroes_data_entry = {
368 	.attr = {.name = "discard_zeroes_data", .mode = S_IRUGO },
369 	.show = queue_discard_zeroes_data_show,
370 };
371 
372 static struct queue_sysfs_entry queue_write_same_max_entry = {
373 	.attr = {.name = "write_same_max_bytes", .mode = S_IRUGO },
374 	.show = queue_write_same_max_show,
375 };
376 
377 static struct queue_sysfs_entry queue_nonrot_entry = {
378 	.attr = {.name = "rotational", .mode = S_IRUGO | S_IWUSR },
379 	.show = queue_show_nonrot,
380 	.store = queue_store_nonrot,
381 };
382 
383 static struct queue_sysfs_entry queue_nomerges_entry = {
384 	.attr = {.name = "nomerges", .mode = S_IRUGO | S_IWUSR },
385 	.show = queue_nomerges_show,
386 	.store = queue_nomerges_store,
387 };
388 
389 static struct queue_sysfs_entry queue_rq_affinity_entry = {
390 	.attr = {.name = "rq_affinity", .mode = S_IRUGO | S_IWUSR },
391 	.show = queue_rq_affinity_show,
392 	.store = queue_rq_affinity_store,
393 };
394 
395 static struct queue_sysfs_entry queue_iostats_entry = {
396 	.attr = {.name = "iostats", .mode = S_IRUGO | S_IWUSR },
397 	.show = queue_show_iostats,
398 	.store = queue_store_iostats,
399 };
400 
401 static struct queue_sysfs_entry queue_random_entry = {
402 	.attr = {.name = "add_random", .mode = S_IRUGO | S_IWUSR },
403 	.show = queue_show_random,
404 	.store = queue_store_random,
405 };
406 
407 static struct attribute *default_attrs[] = {
408 	&queue_requests_entry.attr,
409 	&queue_ra_entry.attr,
410 	&queue_max_hw_sectors_entry.attr,
411 	&queue_max_sectors_entry.attr,
412 	&queue_max_segments_entry.attr,
413 	&queue_max_integrity_segments_entry.attr,
414 	&queue_max_segment_size_entry.attr,
415 	&queue_iosched_entry.attr,
416 	&queue_hw_sector_size_entry.attr,
417 	&queue_logical_block_size_entry.attr,
418 	&queue_physical_block_size_entry.attr,
419 	&queue_io_min_entry.attr,
420 	&queue_io_opt_entry.attr,
421 	&queue_discard_granularity_entry.attr,
422 	&queue_discard_max_entry.attr,
423 	&queue_discard_zeroes_data_entry.attr,
424 	&queue_write_same_max_entry.attr,
425 	&queue_nonrot_entry.attr,
426 	&queue_nomerges_entry.attr,
427 	&queue_rq_affinity_entry.attr,
428 	&queue_iostats_entry.attr,
429 	&queue_random_entry.attr,
430 	NULL,
431 };
432 
433 #define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
434 
435 static ssize_t
436 queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
437 {
438 	struct queue_sysfs_entry *entry = to_queue(attr);
439 	struct request_queue *q =
440 		container_of(kobj, struct request_queue, kobj);
441 	ssize_t res;
442 
443 	if (!entry->show)
444 		return -EIO;
445 	mutex_lock(&q->sysfs_lock);
446 	if (blk_queue_dying(q)) {
447 		mutex_unlock(&q->sysfs_lock);
448 		return -ENOENT;
449 	}
450 	res = entry->show(q, page);
451 	mutex_unlock(&q->sysfs_lock);
452 	return res;
453 }
454 
455 static ssize_t
456 queue_attr_store(struct kobject *kobj, struct attribute *attr,
457 		    const char *page, size_t length)
458 {
459 	struct queue_sysfs_entry *entry = to_queue(attr);
460 	struct request_queue *q;
461 	ssize_t res;
462 
463 	if (!entry->store)
464 		return -EIO;
465 
466 	q = container_of(kobj, struct request_queue, kobj);
467 	mutex_lock(&q->sysfs_lock);
468 	if (blk_queue_dying(q)) {
469 		mutex_unlock(&q->sysfs_lock);
470 		return -ENOENT;
471 	}
472 	res = entry->store(q, page, length);
473 	mutex_unlock(&q->sysfs_lock);
474 	return res;
475 }
476 
477 static void blk_free_queue_rcu(struct rcu_head *rcu_head)
478 {
479 	struct request_queue *q = container_of(rcu_head, struct request_queue,
480 					       rcu_head);
481 	kmem_cache_free(blk_requestq_cachep, q);
482 }
483 
484 /**
485  * blk_release_queue: - release a &struct request_queue when it is no longer needed
486  * @kobj:    the kobj belonging to the request queue to be released
487  *
488  * Description:
489  *     blk_release_queue is the pair to blk_init_queue() or
490  *     blk_queue_make_request().  It should be called when a request queue is
491  *     being released; typically when a block device is being de-registered.
492  *     Currently, its primary task it to free all the &struct request
493  *     structures that were allocated to the queue and the queue itself.
494  *
495  * Note:
496  *     The low level driver must have finished any outstanding requests first
497  *     via blk_cleanup_queue().
498  **/
499 static void blk_release_queue(struct kobject *kobj)
500 {
501 	struct request_queue *q =
502 		container_of(kobj, struct request_queue, kobj);
503 
504 	blkcg_exit_queue(q);
505 
506 	if (q->elevator) {
507 		spin_lock_irq(q->queue_lock);
508 		ioc_clear_queue(q);
509 		spin_unlock_irq(q->queue_lock);
510 		elevator_exit(q->elevator);
511 	}
512 
513 	blk_exit_rl(&q->root_rl);
514 
515 	if (q->queue_tags)
516 		__blk_queue_free_tags(q);
517 
518 	if (!q->mq_ops)
519 		blk_free_flush_queue(q->fq);
520 	else
521 		blk_mq_release(q);
522 
523 	blk_trace_shutdown(q);
524 
525 	bdi_destroy(&q->backing_dev_info);
526 
527 	ida_simple_remove(&blk_queue_ida, q->id);
528 	call_rcu(&q->rcu_head, blk_free_queue_rcu);
529 }
530 
531 static const struct sysfs_ops queue_sysfs_ops = {
532 	.show	= queue_attr_show,
533 	.store	= queue_attr_store,
534 };
535 
536 struct kobj_type blk_queue_ktype = {
537 	.sysfs_ops	= &queue_sysfs_ops,
538 	.default_attrs	= default_attrs,
539 	.release	= blk_release_queue,
540 };
541 
542 int blk_register_queue(struct gendisk *disk)
543 {
544 	int ret;
545 	struct device *dev = disk_to_dev(disk);
546 	struct request_queue *q = disk->queue;
547 
548 	if (WARN_ON(!q))
549 		return -ENXIO;
550 
551 	/*
552 	 * SCSI probing may synchronously create and destroy a lot of
553 	 * request_queues for non-existent devices.  Shutting down a fully
554 	 * functional queue takes measureable wallclock time as RCU grace
555 	 * periods are involved.  To avoid excessive latency in these
556 	 * cases, a request_queue starts out in a degraded mode which is
557 	 * faster to shut down and is made fully functional here as
558 	 * request_queues for non-existent devices never get registered.
559 	 */
560 	if (!blk_queue_init_done(q)) {
561 		queue_flag_set_unlocked(QUEUE_FLAG_INIT_DONE, q);
562 		blk_queue_bypass_end(q);
563 		if (q->mq_ops)
564 			blk_mq_finish_init(q);
565 	}
566 
567 	ret = blk_trace_init_sysfs(dev);
568 	if (ret)
569 		return ret;
570 
571 	ret = kobject_add(&q->kobj, kobject_get(&dev->kobj), "%s", "queue");
572 	if (ret < 0) {
573 		blk_trace_remove_sysfs(dev);
574 		return ret;
575 	}
576 
577 	kobject_uevent(&q->kobj, KOBJ_ADD);
578 
579 	if (q->mq_ops)
580 		blk_mq_register_disk(disk);
581 
582 	if (!q->request_fn)
583 		return 0;
584 
585 	ret = elv_register_queue(q);
586 	if (ret) {
587 		kobject_uevent(&q->kobj, KOBJ_REMOVE);
588 		kobject_del(&q->kobj);
589 		blk_trace_remove_sysfs(dev);
590 		kobject_put(&dev->kobj);
591 		return ret;
592 	}
593 
594 	return 0;
595 }
596 
597 void blk_unregister_queue(struct gendisk *disk)
598 {
599 	struct request_queue *q = disk->queue;
600 
601 	if (WARN_ON(!q))
602 		return;
603 
604 	if (q->mq_ops)
605 		blk_mq_unregister_disk(disk);
606 
607 	if (q->request_fn)
608 		elv_unregister_queue(q);
609 
610 	kobject_uevent(&q->kobj, KOBJ_REMOVE);
611 	kobject_del(&q->kobj);
612 	blk_trace_remove_sysfs(disk_to_dev(disk));
613 	kobject_put(&disk_to_dev(disk)->kobj);
614 }
615