1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
28324aa91SJens Axboe /*
38324aa91SJens Axboe * Functions related to sysfs handling
48324aa91SJens Axboe */
58324aa91SJens Axboe #include <linux/kernel.h>
65a0e3ad6STejun Heo #include <linux/slab.h>
78324aa91SJens Axboe #include <linux/module.h>
88324aa91SJens Axboe #include <linux/bio.h>
98324aa91SJens Axboe #include <linux/blkdev.h>
1066114cadSTejun Heo #include <linux/backing-dev.h>
118324aa91SJens Axboe #include <linux/blktrace_api.h>
1285e0cbbbSLuis Chamberlain #include <linux/debugfs.h>
138324aa91SJens Axboe
148324aa91SJens Axboe #include "blk.h"
153edcc0ceSMing Lei #include "blk-mq.h"
16d173a251SOmar Sandoval #include "blk-mq-debugfs.h"
172aa7745bSChristoph Hellwig #include "blk-mq-sched.h"
180bc65bd4SChristoph Hellwig #include "blk-rq-qos.h"
1987760e5eSJens Axboe #include "blk-wbt.h"
20672fdcf0SMing Lei #include "blk-cgroup.h"
21a7b36ee6SJens Axboe #include "blk-throttle.h"
228324aa91SJens Axboe
238324aa91SJens Axboe struct queue_sysfs_entry {
248324aa91SJens Axboe struct attribute attr;
2562e35f94SChristoph Hellwig ssize_t (*show)(struct gendisk *disk, char *page);
26*734e1a86SDamien Le Moal int (*load_module)(struct gendisk *disk, const char *page, size_t count);
2762e35f94SChristoph Hellwig ssize_t (*store)(struct gendisk *disk, const char *page, size_t count);
288324aa91SJens Axboe };
298324aa91SJens Axboe
308324aa91SJens Axboe static ssize_t
queue_var_show(unsigned long var,char * page)319cb308ceSXiaotian Feng queue_var_show(unsigned long var, char *page)
328324aa91SJens Axboe {
339cb308ceSXiaotian Feng return sprintf(page, "%lu\n", var);
348324aa91SJens Axboe }
358324aa91SJens Axboe
368324aa91SJens Axboe static ssize_t
queue_var_store(unsigned long * var,const char * page,size_t count)378324aa91SJens Axboe queue_var_store(unsigned long *var, const char *page, size_t count)
388324aa91SJens Axboe {
39b1f3b64dSDave Reisner int err;
40b1f3b64dSDave Reisner unsigned long v;
418324aa91SJens Axboe
42ed751e68SJingoo Han err = kstrtoul(page, 10, &v);
43b1f3b64dSDave Reisner if (err || v > UINT_MAX)
44b1f3b64dSDave Reisner return -EINVAL;
45b1f3b64dSDave Reisner
46b1f3b64dSDave Reisner *var = v;
47b1f3b64dSDave Reisner
488324aa91SJens Axboe return count;
498324aa91SJens Axboe }
508324aa91SJens Axboe
queue_requests_show(struct gendisk * disk,char * page)5162e35f94SChristoph Hellwig static ssize_t queue_requests_show(struct gendisk *disk, char *page)
528324aa91SJens Axboe {
5362e35f94SChristoph Hellwig return queue_var_show(disk->queue->nr_requests, page);
548324aa91SJens Axboe }
558324aa91SJens Axboe
568324aa91SJens Axboe static ssize_t
queue_requests_store(struct gendisk * disk,const char * page,size_t count)5762e35f94SChristoph Hellwig queue_requests_store(struct gendisk *disk, const char *page, size_t count)
588324aa91SJens Axboe {
598324aa91SJens Axboe unsigned long nr;
60e3a2b3f9SJens Axboe int ret, err;
61b8a9ae77SJens Axboe
6262e35f94SChristoph Hellwig if (!queue_is_mq(disk->queue))
63b8a9ae77SJens Axboe return -EINVAL;
64b8a9ae77SJens Axboe
65b8a9ae77SJens Axboe ret = queue_var_store(&nr, page, count);
66b1f3b64dSDave Reisner if (ret < 0)
67b1f3b64dSDave Reisner return ret;
68b1f3b64dSDave Reisner
698324aa91SJens Axboe if (nr < BLKDEV_MIN_RQ)
708324aa91SJens Axboe nr = BLKDEV_MIN_RQ;
718324aa91SJens Axboe
7262e35f94SChristoph Hellwig err = blk_mq_update_nr_requests(disk->queue, nr);
73e3a2b3f9SJens Axboe if (err)
74e3a2b3f9SJens Axboe return err;
75a051661cSTejun Heo
768324aa91SJens Axboe return ret;
778324aa91SJens Axboe }
788324aa91SJens Axboe
queue_ra_show(struct gendisk * disk,char * page)7962e35f94SChristoph Hellwig static ssize_t queue_ra_show(struct gendisk *disk, char *page)
808324aa91SJens Axboe {
8162e35f94SChristoph Hellwig return queue_var_show(disk->bdi->ra_pages << (PAGE_SHIFT - 10), page);
828324aa91SJens Axboe }
838324aa91SJens Axboe
848324aa91SJens Axboe static ssize_t
queue_ra_store(struct gendisk * disk,const char * page,size_t count)8562e35f94SChristoph Hellwig queue_ra_store(struct gendisk *disk, const char *page, size_t count)
868324aa91SJens Axboe {
878324aa91SJens Axboe unsigned long ra_kb;
88edb0872fSChristoph Hellwig ssize_t ret;
898324aa91SJens Axboe
90edb0872fSChristoph Hellwig ret = queue_var_store(&ra_kb, page, count);
91b1f3b64dSDave Reisner if (ret < 0)
92b1f3b64dSDave Reisner return ret;
9362e35f94SChristoph Hellwig disk->bdi->ra_pages = ra_kb >> (PAGE_SHIFT - 10);
948324aa91SJens Axboe return ret;
958324aa91SJens Axboe }
968324aa91SJens Axboe
97319e8cfdSChristoph Hellwig #define QUEUE_SYSFS_LIMIT_SHOW(_field) \
9862e35f94SChristoph Hellwig static ssize_t queue_##_field##_show(struct gendisk *disk, char *page) \
99319e8cfdSChristoph Hellwig { \
10062e35f94SChristoph Hellwig return queue_var_show(disk->queue->limits._field, page); \
1018324aa91SJens Axboe }
1028324aa91SJens Axboe
103319e8cfdSChristoph Hellwig QUEUE_SYSFS_LIMIT_SHOW(max_segments)
QUEUE_SYSFS_LIMIT_SHOW(max_discard_segments)104319e8cfdSChristoph Hellwig QUEUE_SYSFS_LIMIT_SHOW(max_discard_segments)
105319e8cfdSChristoph Hellwig QUEUE_SYSFS_LIMIT_SHOW(max_integrity_segments)
106319e8cfdSChristoph Hellwig QUEUE_SYSFS_LIMIT_SHOW(max_segment_size)
107319e8cfdSChristoph Hellwig QUEUE_SYSFS_LIMIT_SHOW(logical_block_size)
108319e8cfdSChristoph Hellwig QUEUE_SYSFS_LIMIT_SHOW(physical_block_size)
109319e8cfdSChristoph Hellwig QUEUE_SYSFS_LIMIT_SHOW(chunk_sectors)
110319e8cfdSChristoph Hellwig QUEUE_SYSFS_LIMIT_SHOW(io_min)
111319e8cfdSChristoph Hellwig QUEUE_SYSFS_LIMIT_SHOW(io_opt)
112319e8cfdSChristoph Hellwig QUEUE_SYSFS_LIMIT_SHOW(discard_granularity)
113319e8cfdSChristoph Hellwig QUEUE_SYSFS_LIMIT_SHOW(zone_write_granularity)
114319e8cfdSChristoph Hellwig QUEUE_SYSFS_LIMIT_SHOW(virt_boundary_mask)
115319e8cfdSChristoph Hellwig QUEUE_SYSFS_LIMIT_SHOW(dma_alignment)
116319e8cfdSChristoph Hellwig QUEUE_SYSFS_LIMIT_SHOW(max_open_zones)
117319e8cfdSChristoph Hellwig QUEUE_SYSFS_LIMIT_SHOW(max_active_zones)
118319e8cfdSChristoph Hellwig QUEUE_SYSFS_LIMIT_SHOW(atomic_write_unit_min)
119319e8cfdSChristoph Hellwig QUEUE_SYSFS_LIMIT_SHOW(atomic_write_unit_max)
120319e8cfdSChristoph Hellwig
121319e8cfdSChristoph Hellwig #define QUEUE_SYSFS_LIMIT_SHOW_SECTORS_TO_BYTES(_field) \
12262e35f94SChristoph Hellwig static ssize_t queue_##_field##_show(struct gendisk *disk, char *page) \
123319e8cfdSChristoph Hellwig { \
124319e8cfdSChristoph Hellwig return sprintf(page, "%llu\n", \
12562e35f94SChristoph Hellwig (unsigned long long)disk->queue->limits._field << \
12662e35f94SChristoph Hellwig SECTOR_SHIFT); \
127c77a5710SMartin K. Petersen }
128c77a5710SMartin K. Petersen
129319e8cfdSChristoph Hellwig QUEUE_SYSFS_LIMIT_SHOW_SECTORS_TO_BYTES(max_discard_sectors)
130319e8cfdSChristoph Hellwig QUEUE_SYSFS_LIMIT_SHOW_SECTORS_TO_BYTES(max_hw_discard_sectors)
131319e8cfdSChristoph Hellwig QUEUE_SYSFS_LIMIT_SHOW_SECTORS_TO_BYTES(max_write_zeroes_sectors)
132319e8cfdSChristoph Hellwig QUEUE_SYSFS_LIMIT_SHOW_SECTORS_TO_BYTES(atomic_write_max_sectors)
133319e8cfdSChristoph Hellwig QUEUE_SYSFS_LIMIT_SHOW_SECTORS_TO_BYTES(atomic_write_boundary_sectors)
134319e8cfdSChristoph Hellwig
135319e8cfdSChristoph Hellwig #define QUEUE_SYSFS_LIMIT_SHOW_SECTORS_TO_KB(_field) \
13662e35f94SChristoph Hellwig static ssize_t queue_##_field##_show(struct gendisk *disk, char *page) \
137319e8cfdSChristoph Hellwig { \
13862e35f94SChristoph Hellwig return queue_var_show(disk->queue->limits._field >> 1, page); \
1391e739730SChristoph Hellwig }
1401e739730SChristoph Hellwig
141319e8cfdSChristoph Hellwig QUEUE_SYSFS_LIMIT_SHOW_SECTORS_TO_KB(max_sectors)
142319e8cfdSChristoph Hellwig QUEUE_SYSFS_LIMIT_SHOW_SECTORS_TO_KB(max_hw_sectors)
143319e8cfdSChristoph Hellwig
144319e8cfdSChristoph Hellwig #define QUEUE_SYSFS_SHOW_CONST(_name, _val) \
14562e35f94SChristoph Hellwig static ssize_t queue_##_name##_show(struct gendisk *disk, char *page) \
146319e8cfdSChristoph Hellwig { \
147319e8cfdSChristoph Hellwig return sprintf(page, "%d\n", _val); \
1489da3d1e9SJohn Garry }
1499da3d1e9SJohn Garry
150319e8cfdSChristoph Hellwig /* deprecated fields */
151319e8cfdSChristoph Hellwig QUEUE_SYSFS_SHOW_CONST(discard_zeroes_data, 0)
152319e8cfdSChristoph Hellwig QUEUE_SYSFS_SHOW_CONST(write_same_max, 0)
153319e8cfdSChristoph Hellwig QUEUE_SYSFS_SHOW_CONST(poll_delay, -1)
1549da3d1e9SJohn Garry
15562e35f94SChristoph Hellwig static ssize_t queue_max_discard_sectors_store(struct gendisk *disk,
1560034af03SJens Axboe const char *page, size_t count)
1570034af03SJens Axboe {
1584f563a64SChristoph Hellwig unsigned long max_discard_bytes;
159ff956a3bSChristoph Hellwig struct queue_limits lim;
1604f563a64SChristoph Hellwig ssize_t ret;
161ff956a3bSChristoph Hellwig int err;
1620034af03SJens Axboe
1634f563a64SChristoph Hellwig ret = queue_var_store(&max_discard_bytes, page, count);
1640034af03SJens Axboe if (ret < 0)
1650034af03SJens Axboe return ret;
1660034af03SJens Axboe
16762e35f94SChristoph Hellwig if (max_discard_bytes & (disk->queue->limits.discard_granularity - 1))
1680034af03SJens Axboe return -EINVAL;
1690034af03SJens Axboe
1704f563a64SChristoph Hellwig if ((max_discard_bytes >> SECTOR_SHIFT) > UINT_MAX)
1710034af03SJens Axboe return -EINVAL;
1720034af03SJens Axboe
17362e35f94SChristoph Hellwig lim = queue_limits_start_update(disk->queue);
174ff956a3bSChristoph Hellwig lim.max_user_discard_sectors = max_discard_bytes >> SECTOR_SHIFT;
17562e35f94SChristoph Hellwig err = queue_limits_commit_update(disk->queue, &lim);
176ff956a3bSChristoph Hellwig if (err)
177ff956a3bSChristoph Hellwig return err;
1780034af03SJens Axboe return ret;
1790034af03SJens Axboe }
1800034af03SJens Axboe
181319e8cfdSChristoph Hellwig /*
182319e8cfdSChristoph Hellwig * For zone append queue_max_zone_append_sectors does not just return the
183319e8cfdSChristoph Hellwig * underlying queue limits, but actually contains a calculation. Because of
184319e8cfdSChristoph Hellwig * that we can't simply use QUEUE_SYSFS_LIMIT_SHOW_SECTORS_TO_BYTES here.
185319e8cfdSChristoph Hellwig */
queue_zone_append_max_show(struct gendisk * disk,char * page)18662e35f94SChristoph Hellwig static ssize_t queue_zone_append_max_show(struct gendisk *disk, char *page)
1870512a75bSKeith Busch {
18862e35f94SChristoph Hellwig return sprintf(page, "%llu\n",
18962e35f94SChristoph Hellwig (u64)queue_max_zone_append_sectors(disk->queue) <<
19062e35f94SChristoph Hellwig SECTOR_SHIFT);
1910512a75bSKeith Busch }
1920512a75bSKeith Busch
1938324aa91SJens Axboe static ssize_t
queue_max_sectors_store(struct gendisk * disk,const char * page,size_t count)19462e35f94SChristoph Hellwig queue_max_sectors_store(struct gendisk *disk, const char *page, size_t count)
1958324aa91SJens Axboe {
1960327ca9dSChristoph Hellwig unsigned long max_sectors_kb;
1970327ca9dSChristoph Hellwig struct queue_limits lim;
1980327ca9dSChristoph Hellwig ssize_t ret;
1990327ca9dSChristoph Hellwig int err;
2008324aa91SJens Axboe
2010327ca9dSChristoph Hellwig ret = queue_var_store(&max_sectors_kb, page, count);
202b1f3b64dSDave Reisner if (ret < 0)
203b1f3b64dSDave Reisner return ret;
204b1f3b64dSDave Reisner
20562e35f94SChristoph Hellwig lim = queue_limits_start_update(disk->queue);
2060327ca9dSChristoph Hellwig lim.max_user_sectors = max_sectors_kb << 1;
20762e35f94SChristoph Hellwig err = queue_limits_commit_update(disk->queue, &lim);
2080327ca9dSChristoph Hellwig if (err)
2090327ca9dSChristoph Hellwig return err;
2108324aa91SJens Axboe return ret;
2118324aa91SJens Axboe }
2128324aa91SJens Axboe
queue_feature_store(struct gendisk * disk,const char * page,size_t count,blk_features_t feature)21362e35f94SChristoph Hellwig static ssize_t queue_feature_store(struct gendisk *disk, const char *page,
214fcf865e3SChristoph Hellwig size_t count, blk_features_t feature)
215bd4a633bSChristoph Hellwig {
216bd4a633bSChristoph Hellwig struct queue_limits lim;
217bd4a633bSChristoph Hellwig unsigned long val;
218bd4a633bSChristoph Hellwig ssize_t ret;
219bd4a633bSChristoph Hellwig
220bd4a633bSChristoph Hellwig ret = queue_var_store(&val, page, count);
221bd4a633bSChristoph Hellwig if (ret < 0)
222bd4a633bSChristoph Hellwig return ret;
223bd4a633bSChristoph Hellwig
22462e35f94SChristoph Hellwig lim = queue_limits_start_update(disk->queue);
225bd4a633bSChristoph Hellwig if (val)
226bd4a633bSChristoph Hellwig lim.features |= feature;
227bd4a633bSChristoph Hellwig else
228bd4a633bSChristoph Hellwig lim.features &= ~feature;
22962e35f94SChristoph Hellwig ret = queue_limits_commit_update(disk->queue, &lim);
230bd4a633bSChristoph Hellwig if (ret)
231bd4a633bSChristoph Hellwig return ret;
232bd4a633bSChristoph Hellwig return count;
233bd4a633bSChristoph Hellwig }
234bd4a633bSChristoph Hellwig
235bd4a633bSChristoph Hellwig #define QUEUE_SYSFS_FEATURE(_name, _feature) \
23662e35f94SChristoph Hellwig static ssize_t queue_##_name##_show(struct gendisk *disk, char *page) \
237bd4a633bSChristoph Hellwig { \
23862e35f94SChristoph Hellwig return sprintf(page, "%u\n", \
23962e35f94SChristoph Hellwig !!(disk->queue->limits.features & _feature)); \
240bd4a633bSChristoph Hellwig } \
24162e35f94SChristoph Hellwig static ssize_t queue_##_name##_store(struct gendisk *disk, \
242bd4a633bSChristoph Hellwig const char *page, size_t count) \
243bd4a633bSChristoph Hellwig { \
24462e35f94SChristoph Hellwig return queue_feature_store(disk, page, count, _feature); \
245bd4a633bSChristoph Hellwig }
246bd4a633bSChristoph Hellwig
247bd4a633bSChristoph Hellwig QUEUE_SYSFS_FEATURE(rotational, BLK_FEAT_ROTATIONAL)
24839a9f1c3SChristoph Hellwig QUEUE_SYSFS_FEATURE(add_random, BLK_FEAT_ADD_RANDOM)
249cdb24979SChristoph Hellwig QUEUE_SYSFS_FEATURE(iostats, BLK_FEAT_IO_STAT)
2501a02f3a7SChristoph Hellwig QUEUE_SYSFS_FEATURE(stable_writes, BLK_FEAT_STABLE_WRITES);
2511308835fSBartlomiej Zolnierkiewicz
252319e8cfdSChristoph Hellwig #define QUEUE_SYSFS_FEATURE_SHOW(_name, _feature) \
25362e35f94SChristoph Hellwig static ssize_t queue_##_name##_show(struct gendisk *disk, char *page) \
254319e8cfdSChristoph Hellwig { \
25562e35f94SChristoph Hellwig return sprintf(page, "%u\n", \
25662e35f94SChristoph Hellwig !!(disk->queue->limits.features & _feature)); \
257319e8cfdSChristoph Hellwig }
258319e8cfdSChristoph Hellwig
259319e8cfdSChristoph Hellwig QUEUE_SYSFS_FEATURE_SHOW(poll, BLK_FEAT_POLL);
260319e8cfdSChristoph Hellwig QUEUE_SYSFS_FEATURE_SHOW(fua, BLK_FEAT_FUA);
261319e8cfdSChristoph Hellwig QUEUE_SYSFS_FEATURE_SHOW(dax, BLK_FEAT_DAX);
262319e8cfdSChristoph Hellwig
queue_zoned_show(struct gendisk * disk,char * page)26362e35f94SChristoph Hellwig static ssize_t queue_zoned_show(struct gendisk *disk, char *page)
264797476b8SDamien Le Moal {
26562e35f94SChristoph Hellwig if (blk_queue_is_zoned(disk->queue))
266797476b8SDamien Le Moal return sprintf(page, "host-managed\n");
267797476b8SDamien Le Moal return sprintf(page, "none\n");
268797476b8SDamien Le Moal }
269797476b8SDamien Le Moal
queue_nr_zones_show(struct gendisk * disk,char * page)27062e35f94SChristoph Hellwig static ssize_t queue_nr_zones_show(struct gendisk *disk, char *page)
271965b652eSDamien Le Moal {
27262e35f94SChristoph Hellwig return queue_var_show(disk_nr_zones(disk), page);
273965b652eSDamien Le Moal }
274965b652eSDamien Le Moal
queue_nomerges_show(struct gendisk * disk,char * page)27562e35f94SChristoph Hellwig static ssize_t queue_nomerges_show(struct gendisk *disk, char *page)
276ac9fafa1SAlan D. Brunelle {
27762e35f94SChristoph Hellwig return queue_var_show((blk_queue_nomerges(disk->queue) << 1) |
27862e35f94SChristoph Hellwig blk_queue_noxmerges(disk->queue), page);
279ac9fafa1SAlan D. Brunelle }
280ac9fafa1SAlan D. Brunelle
queue_nomerges_store(struct gendisk * disk,const char * page,size_t count)28162e35f94SChristoph Hellwig static ssize_t queue_nomerges_store(struct gendisk *disk, const char *page,
282ac9fafa1SAlan D. Brunelle size_t count)
283ac9fafa1SAlan D. Brunelle {
284ac9fafa1SAlan D. Brunelle unsigned long nm;
285ac9fafa1SAlan D. Brunelle ssize_t ret = queue_var_store(&nm, page, count);
286ac9fafa1SAlan D. Brunelle
287b1f3b64dSDave Reisner if (ret < 0)
288b1f3b64dSDave Reisner return ret;
289b1f3b64dSDave Reisner
29062e35f94SChristoph Hellwig blk_queue_flag_clear(QUEUE_FLAG_NOMERGES, disk->queue);
29162e35f94SChristoph Hellwig blk_queue_flag_clear(QUEUE_FLAG_NOXMERGES, disk->queue);
292488991e2SAlan D. Brunelle if (nm == 2)
29362e35f94SChristoph Hellwig blk_queue_flag_set(QUEUE_FLAG_NOMERGES, disk->queue);
294488991e2SAlan D. Brunelle else if (nm)
29562e35f94SChristoph Hellwig blk_queue_flag_set(QUEUE_FLAG_NOXMERGES, disk->queue);
2961308835fSBartlomiej Zolnierkiewicz
297ac9fafa1SAlan D. Brunelle return ret;
298ac9fafa1SAlan D. Brunelle }
299ac9fafa1SAlan D. Brunelle
queue_rq_affinity_show(struct gendisk * disk,char * page)30062e35f94SChristoph Hellwig static ssize_t queue_rq_affinity_show(struct gendisk *disk, char *page)
301c7c22e4dSJens Axboe {
30262e35f94SChristoph Hellwig bool set = test_bit(QUEUE_FLAG_SAME_COMP, &disk->queue->queue_flags);
30362e35f94SChristoph Hellwig bool force = test_bit(QUEUE_FLAG_SAME_FORCE, &disk->queue->queue_flags);
304c7c22e4dSJens Axboe
3055757a6d7SDan Williams return queue_var_show(set << force, page);
306c7c22e4dSJens Axboe }
307c7c22e4dSJens Axboe
308c7c22e4dSJens Axboe static ssize_t
queue_rq_affinity_store(struct gendisk * disk,const char * page,size_t count)30962e35f94SChristoph Hellwig queue_rq_affinity_store(struct gendisk *disk, const char *page, size_t count)
310c7c22e4dSJens Axboe {
311c7c22e4dSJens Axboe ssize_t ret = -EINVAL;
3120a06ff06SChristoph Hellwig #ifdef CONFIG_SMP
31362e35f94SChristoph Hellwig struct request_queue *q = disk->queue;
314c7c22e4dSJens Axboe unsigned long val;
315c7c22e4dSJens Axboe
316c7c22e4dSJens Axboe ret = queue_var_store(&val, page, count);
317b1f3b64dSDave Reisner if (ret < 0)
318b1f3b64dSDave Reisner return ret;
319b1f3b64dSDave Reisner
320e8037d49SEric Seppanen if (val == 2) {
32157d74df9SChristoph Hellwig blk_queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
32257d74df9SChristoph Hellwig blk_queue_flag_set(QUEUE_FLAG_SAME_FORCE, q);
323e8037d49SEric Seppanen } else if (val == 1) {
32457d74df9SChristoph Hellwig blk_queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
32557d74df9SChristoph Hellwig blk_queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
326e8037d49SEric Seppanen } else if (val == 0) {
32757d74df9SChristoph Hellwig blk_queue_flag_clear(QUEUE_FLAG_SAME_COMP, q);
32857d74df9SChristoph Hellwig blk_queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
3295757a6d7SDan Williams }
330c7c22e4dSJens Axboe #endif
331c7c22e4dSJens Axboe return ret;
332c7c22e4dSJens Axboe }
3338324aa91SJens Axboe
queue_poll_delay_store(struct gendisk * disk,const char * page,size_t count)33462e35f94SChristoph Hellwig static ssize_t queue_poll_delay_store(struct gendisk *disk, const char *page,
33506426adfSJens Axboe size_t count)
33606426adfSJens Axboe {
33764f1c21eSJens Axboe return count;
33806426adfSJens Axboe }
33906426adfSJens Axboe
queue_poll_store(struct gendisk * disk,const char * page,size_t count)34062e35f94SChristoph Hellwig static ssize_t queue_poll_store(struct gendisk *disk, const char *page,
34105229beeSJens Axboe size_t count)
34205229beeSJens Axboe {
34362e35f94SChristoph Hellwig if (!(disk->queue->limits.features & BLK_FEAT_POLL))
34405229beeSJens Axboe return -EINVAL;
345a614dd22SChristoph Hellwig pr_info_ratelimited("writes to the poll attribute are ignored.\n");
346a614dd22SChristoph Hellwig pr_info_ratelimited("please use driver specific parameters instead.\n");
347a614dd22SChristoph Hellwig return count;
34805229beeSJens Axboe }
34905229beeSJens Axboe
queue_io_timeout_show(struct gendisk * disk,char * page)35062e35f94SChristoph Hellwig static ssize_t queue_io_timeout_show(struct gendisk *disk, char *page)
35165cd1d13SWeiping Zhang {
35262e35f94SChristoph Hellwig return sprintf(page, "%u\n", jiffies_to_msecs(disk->queue->rq_timeout));
35365cd1d13SWeiping Zhang }
35465cd1d13SWeiping Zhang
queue_io_timeout_store(struct gendisk * disk,const char * page,size_t count)35562e35f94SChristoph Hellwig static ssize_t queue_io_timeout_store(struct gendisk *disk, const char *page,
35665cd1d13SWeiping Zhang size_t count)
35765cd1d13SWeiping Zhang {
35865cd1d13SWeiping Zhang unsigned int val;
35965cd1d13SWeiping Zhang int err;
36065cd1d13SWeiping Zhang
36165cd1d13SWeiping Zhang err = kstrtou32(page, 10, &val);
36265cd1d13SWeiping Zhang if (err || val == 0)
36365cd1d13SWeiping Zhang return -EINVAL;
36465cd1d13SWeiping Zhang
36562e35f94SChristoph Hellwig blk_queue_rq_timeout(disk->queue, msecs_to_jiffies(val));
36665cd1d13SWeiping Zhang
36765cd1d13SWeiping Zhang return count;
36865cd1d13SWeiping Zhang }
36965cd1d13SWeiping Zhang
queue_wc_show(struct gendisk * disk,char * page)37062e35f94SChristoph Hellwig static ssize_t queue_wc_show(struct gendisk *disk, char *page)
37193e9d8e8SJens Axboe {
37262e35f94SChristoph Hellwig if (blk_queue_write_cache(disk->queue))
3731122c0c1SChristoph Hellwig return sprintf(page, "write back\n");
37478887d00SChristoph Hellwig return sprintf(page, "write through\n");
37593e9d8e8SJens Axboe }
37693e9d8e8SJens Axboe
queue_wc_store(struct gendisk * disk,const char * page,size_t count)37762e35f94SChristoph Hellwig static ssize_t queue_wc_store(struct gendisk *disk, const char *page,
37893e9d8e8SJens Axboe size_t count)
37993e9d8e8SJens Axboe {
3801122c0c1SChristoph Hellwig struct queue_limits lim;
3811122c0c1SChristoph Hellwig bool disable;
3821122c0c1SChristoph Hellwig int err;
3831122c0c1SChristoph Hellwig
38443c9835bSChristoph Hellwig if (!strncmp(page, "write back", 10)) {
3851122c0c1SChristoph Hellwig disable = false;
38643c9835bSChristoph Hellwig } else if (!strncmp(page, "write through", 13) ||
38743c9835bSChristoph Hellwig !strncmp(page, "none", 4)) {
3881122c0c1SChristoph Hellwig disable = true;
38943c9835bSChristoph Hellwig } else {
39043c9835bSChristoph Hellwig return -EINVAL;
39143c9835bSChristoph Hellwig }
39293e9d8e8SJens Axboe
39362e35f94SChristoph Hellwig lim = queue_limits_start_update(disk->queue);
3941122c0c1SChristoph Hellwig if (disable)
395bae1c743SChristoph Hellwig lim.flags |= BLK_FLAG_WRITE_CACHE_DISABLED;
3961122c0c1SChristoph Hellwig else
397bae1c743SChristoph Hellwig lim.flags &= ~BLK_FLAG_WRITE_CACHE_DISABLED;
39862e35f94SChristoph Hellwig err = queue_limits_commit_update(disk->queue, &lim);
3991122c0c1SChristoph Hellwig if (err)
4001122c0c1SChristoph Hellwig return err;
40193e9d8e8SJens Axboe return count;
40293e9d8e8SJens Axboe }
40393e9d8e8SJens Axboe
40435626147SChristoph Hellwig #define QUEUE_RO_ENTRY(_prefix, _name) \
40535626147SChristoph Hellwig static struct queue_sysfs_entry _prefix##_entry = { \
40635626147SChristoph Hellwig .attr = { .name = _name, .mode = 0444 }, \
40735626147SChristoph Hellwig .show = _prefix##_show, \
4088324aa91SJens Axboe };
4098324aa91SJens Axboe
41035626147SChristoph Hellwig #define QUEUE_RW_ENTRY(_prefix, _name) \
41135626147SChristoph Hellwig static struct queue_sysfs_entry _prefix##_entry = { \
41235626147SChristoph Hellwig .attr = { .name = _name, .mode = 0644 }, \
41335626147SChristoph Hellwig .show = _prefix##_show, \
41435626147SChristoph Hellwig .store = _prefix##_store, \
4158324aa91SJens Axboe };
4168324aa91SJens Axboe
417*734e1a86SDamien Le Moal #define QUEUE_RW_LOAD_MODULE_ENTRY(_prefix, _name) \
418*734e1a86SDamien Le Moal static struct queue_sysfs_entry _prefix##_entry = { \
419*734e1a86SDamien Le Moal .attr = { .name = _name, .mode = 0644 }, \
420*734e1a86SDamien Le Moal .show = _prefix##_show, \
421*734e1a86SDamien Le Moal .load_module = _prefix##_load_module, \
422*734e1a86SDamien Le Moal .store = _prefix##_store, \
423*734e1a86SDamien Le Moal }
424*734e1a86SDamien Le Moal
42535626147SChristoph Hellwig QUEUE_RW_ENTRY(queue_requests, "nr_requests");
42635626147SChristoph Hellwig QUEUE_RW_ENTRY(queue_ra, "read_ahead_kb");
42735626147SChristoph Hellwig QUEUE_RW_ENTRY(queue_max_sectors, "max_sectors_kb");
42835626147SChristoph Hellwig QUEUE_RO_ENTRY(queue_max_hw_sectors, "max_hw_sectors_kb");
42935626147SChristoph Hellwig QUEUE_RO_ENTRY(queue_max_segments, "max_segments");
43035626147SChristoph Hellwig QUEUE_RO_ENTRY(queue_max_integrity_segments, "max_integrity_segments");
43135626147SChristoph Hellwig QUEUE_RO_ENTRY(queue_max_segment_size, "max_segment_size");
432*734e1a86SDamien Le Moal QUEUE_RW_LOAD_MODULE_ENTRY(elv_iosched, "scheduler");
4338324aa91SJens Axboe
43435626147SChristoph Hellwig QUEUE_RO_ENTRY(queue_logical_block_size, "logical_block_size");
43535626147SChristoph Hellwig QUEUE_RO_ENTRY(queue_physical_block_size, "physical_block_size");
43635626147SChristoph Hellwig QUEUE_RO_ENTRY(queue_chunk_sectors, "chunk_sectors");
43735626147SChristoph Hellwig QUEUE_RO_ENTRY(queue_io_min, "minimum_io_size");
43835626147SChristoph Hellwig QUEUE_RO_ENTRY(queue_io_opt, "optimal_io_size");
4398324aa91SJens Axboe
44035626147SChristoph Hellwig QUEUE_RO_ENTRY(queue_max_discard_segments, "max_discard_segments");
44135626147SChristoph Hellwig QUEUE_RO_ENTRY(queue_discard_granularity, "discard_granularity");
442319e8cfdSChristoph Hellwig QUEUE_RO_ENTRY(queue_max_hw_discard_sectors, "discard_max_hw_bytes");
443319e8cfdSChristoph Hellwig QUEUE_RW_ENTRY(queue_max_discard_sectors, "discard_max_bytes");
44435626147SChristoph Hellwig QUEUE_RO_ENTRY(queue_discard_zeroes_data, "discard_zeroes_data");
445c77a5710SMartin K. Petersen
446319e8cfdSChristoph Hellwig QUEUE_RO_ENTRY(queue_atomic_write_max_sectors, "atomic_write_max_bytes");
447319e8cfdSChristoph Hellwig QUEUE_RO_ENTRY(queue_atomic_write_boundary_sectors,
448319e8cfdSChristoph Hellwig "atomic_write_boundary_bytes");
4499da3d1e9SJohn Garry QUEUE_RO_ENTRY(queue_atomic_write_unit_max, "atomic_write_unit_max_bytes");
4509da3d1e9SJohn Garry QUEUE_RO_ENTRY(queue_atomic_write_unit_min, "atomic_write_unit_min_bytes");
4519da3d1e9SJohn Garry
45235626147SChristoph Hellwig QUEUE_RO_ENTRY(queue_write_same_max, "write_same_max_bytes");
453319e8cfdSChristoph Hellwig QUEUE_RO_ENTRY(queue_max_write_zeroes_sectors, "write_zeroes_max_bytes");
45435626147SChristoph Hellwig QUEUE_RO_ENTRY(queue_zone_append_max, "zone_append_max_bytes");
455a805a4faSDamien Le Moal QUEUE_RO_ENTRY(queue_zone_write_granularity, "zone_write_granularity");
4561e739730SChristoph Hellwig
45735626147SChristoph Hellwig QUEUE_RO_ENTRY(queue_zoned, "zoned");
45835626147SChristoph Hellwig QUEUE_RO_ENTRY(queue_nr_zones, "nr_zones");
45935626147SChristoph Hellwig QUEUE_RO_ENTRY(queue_max_open_zones, "max_open_zones");
46035626147SChristoph Hellwig QUEUE_RO_ENTRY(queue_max_active_zones, "max_active_zones");
46113f05c8dSMartin K. Petersen
46235626147SChristoph Hellwig QUEUE_RW_ENTRY(queue_nomerges, "nomerges");
46335626147SChristoph Hellwig QUEUE_RW_ENTRY(queue_rq_affinity, "rq_affinity");
46435626147SChristoph Hellwig QUEUE_RW_ENTRY(queue_poll, "io_poll");
46535626147SChristoph Hellwig QUEUE_RW_ENTRY(queue_poll_delay, "io_poll_delay");
46635626147SChristoph Hellwig QUEUE_RW_ENTRY(queue_wc, "write_cache");
46735626147SChristoph Hellwig QUEUE_RO_ENTRY(queue_fua, "fua");
46835626147SChristoph Hellwig QUEUE_RO_ENTRY(queue_dax, "dax");
46935626147SChristoph Hellwig QUEUE_RW_ENTRY(queue_io_timeout, "io_timeout");
47028af7428SMax Gurtovoy QUEUE_RO_ENTRY(queue_virt_boundary_mask, "virt_boundary_mask");
4713850e13fSKeith Busch QUEUE_RO_ENTRY(queue_dma_alignment, "dma_alignment");
472c77a5710SMartin K. Petersen
47335626147SChristoph Hellwig /* legacy alias for logical_block_size: */
474e68b903cSMartin K. Petersen static struct queue_sysfs_entry queue_hw_sector_size_entry = {
4755657a819SJoe Perches .attr = {.name = "hw_sector_size", .mode = 0444 },
476e1defc4fSMartin K. Petersen .show = queue_logical_block_size_show,
477e1defc4fSMartin K. Petersen };
478e1defc4fSMartin K. Petersen
479bd4a633bSChristoph Hellwig QUEUE_RW_ENTRY(queue_rotational, "rotational");
480fc93fe14SChristoph Hellwig QUEUE_RW_ENTRY(queue_iostats, "iostats");
48139a9f1c3SChristoph Hellwig QUEUE_RW_ENTRY(queue_add_random, "add_random");
4821cb039f3SChristoph Hellwig QUEUE_RW_ENTRY(queue_stable_writes, "stable_writes");
483e2e1a148SJens Axboe
484645a829eSYu Kuai #ifdef CONFIG_BLK_WBT
queue_var_store64(s64 * var,const char * page)485645a829eSYu Kuai static ssize_t queue_var_store64(s64 *var, const char *page)
486645a829eSYu Kuai {
487645a829eSYu Kuai int err;
488645a829eSYu Kuai s64 v;
489645a829eSYu Kuai
490645a829eSYu Kuai err = kstrtos64(page, 10, &v);
491645a829eSYu Kuai if (err < 0)
492645a829eSYu Kuai return err;
493645a829eSYu Kuai
494645a829eSYu Kuai *var = v;
495645a829eSYu Kuai return 0;
496645a829eSYu Kuai }
497645a829eSYu Kuai
queue_wb_lat_show(struct gendisk * disk,char * page)49862e35f94SChristoph Hellwig static ssize_t queue_wb_lat_show(struct gendisk *disk, char *page)
499645a829eSYu Kuai {
50062e35f94SChristoph Hellwig if (!wbt_rq_qos(disk->queue))
501645a829eSYu Kuai return -EINVAL;
502645a829eSYu Kuai
50362e35f94SChristoph Hellwig if (wbt_disabled(disk->queue))
504645a829eSYu Kuai return sprintf(page, "0\n");
505645a829eSYu Kuai
50662e35f94SChristoph Hellwig return sprintf(page, "%llu\n",
50762e35f94SChristoph Hellwig div_u64(wbt_get_min_lat(disk->queue), 1000));
508645a829eSYu Kuai }
509645a829eSYu Kuai
queue_wb_lat_store(struct gendisk * disk,const char * page,size_t count)51062e35f94SChristoph Hellwig static ssize_t queue_wb_lat_store(struct gendisk *disk, const char *page,
511645a829eSYu Kuai size_t count)
512645a829eSYu Kuai {
51362e35f94SChristoph Hellwig struct request_queue *q = disk->queue;
514645a829eSYu Kuai struct rq_qos *rqos;
515645a829eSYu Kuai ssize_t ret;
516645a829eSYu Kuai s64 val;
517645a829eSYu Kuai
518645a829eSYu Kuai ret = queue_var_store64(&val, page);
519645a829eSYu Kuai if (ret < 0)
520645a829eSYu Kuai return ret;
521645a829eSYu Kuai if (val < -1)
522645a829eSYu Kuai return -EINVAL;
523645a829eSYu Kuai
524645a829eSYu Kuai rqos = wbt_rq_qos(q);
525645a829eSYu Kuai if (!rqos) {
52662e35f94SChristoph Hellwig ret = wbt_init(disk);
527645a829eSYu Kuai if (ret)
528645a829eSYu Kuai return ret;
529645a829eSYu Kuai }
530645a829eSYu Kuai
531645a829eSYu Kuai if (val == -1)
532645a829eSYu Kuai val = wbt_default_latency_nsec(q);
533645a829eSYu Kuai else if (val >= 0)
534645a829eSYu Kuai val *= 1000ULL;
535645a829eSYu Kuai
536645a829eSYu Kuai if (wbt_get_min_lat(q) == val)
537645a829eSYu Kuai return count;
538645a829eSYu Kuai
539645a829eSYu Kuai /*
540645a829eSYu Kuai * Ensure that the queue is idled, in case the latency update
541645a829eSYu Kuai * ends up either enabling or disabling wbt completely. We can't
542645a829eSYu Kuai * have IO inflight if that happens.
543645a829eSYu Kuai */
544645a829eSYu Kuai blk_mq_quiesce_queue(q);
545645a829eSYu Kuai
546645a829eSYu Kuai wbt_set_min_lat(q, val);
547645a829eSYu Kuai
548645a829eSYu Kuai blk_mq_unquiesce_queue(q);
549645a829eSYu Kuai
550645a829eSYu Kuai return count;
551645a829eSYu Kuai }
552645a829eSYu Kuai
553645a829eSYu Kuai QUEUE_RW_ENTRY(queue_wb_lat, "wbt_lat_usec");
554645a829eSYu Kuai #endif
555645a829eSYu Kuai
5563649ff0aSBart Van Assche /* Common attributes for bio-based and request-based queues. */
5574d25339eSWeiping Zhang static struct attribute *queue_attrs[] = {
5588324aa91SJens Axboe &queue_ra_entry.attr,
5598324aa91SJens Axboe &queue_max_hw_sectors_entry.attr,
5608324aa91SJens Axboe &queue_max_sectors_entry.attr,
561c77a5710SMartin K. Petersen &queue_max_segments_entry.attr,
5621e739730SChristoph Hellwig &queue_max_discard_segments_entry.attr,
56313f05c8dSMartin K. Petersen &queue_max_integrity_segments_entry.attr,
564c77a5710SMartin K. Petersen &queue_max_segment_size_entry.attr,
565e68b903cSMartin K. Petersen &queue_hw_sector_size_entry.attr,
566e1defc4fSMartin K. Petersen &queue_logical_block_size_entry.attr,
567c72758f3SMartin K. Petersen &queue_physical_block_size_entry.attr,
56887caf97cSHannes Reinecke &queue_chunk_sectors_entry.attr,
569c72758f3SMartin K. Petersen &queue_io_min_entry.attr,
570c72758f3SMartin K. Petersen &queue_io_opt_entry.attr,
57186b37281SMartin K. Petersen &queue_discard_granularity_entry.attr,
572319e8cfdSChristoph Hellwig &queue_max_discard_sectors_entry.attr,
573319e8cfdSChristoph Hellwig &queue_max_hw_discard_sectors_entry.attr,
57498262f27SMartin K. Petersen &queue_discard_zeroes_data_entry.attr,
575319e8cfdSChristoph Hellwig &queue_atomic_write_max_sectors_entry.attr,
576319e8cfdSChristoph Hellwig &queue_atomic_write_boundary_sectors_entry.attr,
5779da3d1e9SJohn Garry &queue_atomic_write_unit_min_entry.attr,
5789da3d1e9SJohn Garry &queue_atomic_write_unit_max_entry.attr,
5794363ac7cSMartin K. Petersen &queue_write_same_max_entry.attr,
580319e8cfdSChristoph Hellwig &queue_max_write_zeroes_sectors_entry.attr,
5810512a75bSKeith Busch &queue_zone_append_max_entry.attr,
582a805a4faSDamien Le Moal &queue_zone_write_granularity_entry.attr,
583bd4a633bSChristoph Hellwig &queue_rotational_entry.attr,
584797476b8SDamien Le Moal &queue_zoned_entry.attr,
585965b652eSDamien Le Moal &queue_nr_zones_entry.attr,
586e15864f8SNiklas Cassel &queue_max_open_zones_entry.attr,
587659bf827SNiklas Cassel &queue_max_active_zones_entry.attr,
588ac9fafa1SAlan D. Brunelle &queue_nomerges_entry.attr,
589bc58ba94SJens Axboe &queue_iostats_entry.attr,
5901cb039f3SChristoph Hellwig &queue_stable_writes_entry.attr,
59139a9f1c3SChristoph Hellwig &queue_add_random_entry.attr,
59205229beeSJens Axboe &queue_poll_entry.attr,
59393e9d8e8SJens Axboe &queue_wc_entry.attr,
5946fcefbe5SKent Overstreet &queue_fua_entry.attr,
595ea6ca600SYigal Korman &queue_dax_entry.attr,
59606426adfSJens Axboe &queue_poll_delay_entry.attr,
59728af7428SMax Gurtovoy &queue_virt_boundary_mask_entry.attr,
5983850e13fSKeith Busch &queue_dma_alignment_entry.attr,
5998324aa91SJens Axboe NULL,
6008324aa91SJens Axboe };
6018324aa91SJens Axboe
6023649ff0aSBart Van Assche /* Request-based queue attributes that are not relevant for bio-based queues. */
6036d85ebf9SYu Kuai static struct attribute *blk_mq_queue_attrs[] = {
6046d85ebf9SYu Kuai &queue_requests_entry.attr,
6056d85ebf9SYu Kuai &elv_iosched_entry.attr,
6066d85ebf9SYu Kuai &queue_rq_affinity_entry.attr,
6076d85ebf9SYu Kuai &queue_io_timeout_entry.attr,
6086d85ebf9SYu Kuai #ifdef CONFIG_BLK_WBT
6096d85ebf9SYu Kuai &queue_wb_lat_entry.attr,
6106d85ebf9SYu Kuai #endif
6116d85ebf9SYu Kuai NULL,
6126d85ebf9SYu Kuai };
6136d85ebf9SYu Kuai
queue_attr_visible(struct kobject * kobj,struct attribute * attr,int n)6144d25339eSWeiping Zhang static umode_t queue_attr_visible(struct kobject *kobj, struct attribute *attr,
6154d25339eSWeiping Zhang int n)
6164d25339eSWeiping Zhang {
6172bd85221SChristoph Hellwig struct gendisk *disk = container_of(kobj, struct gendisk, queue_kobj);
6182bd85221SChristoph Hellwig struct request_queue *q = disk->queue;
6194d25339eSWeiping Zhang
620659bf827SNiklas Cassel if ((attr == &queue_max_open_zones_entry.attr ||
621659bf827SNiklas Cassel attr == &queue_max_active_zones_entry.attr) &&
622e15864f8SNiklas Cassel !blk_queue_is_zoned(q))
623e15864f8SNiklas Cassel return 0;
624e15864f8SNiklas Cassel
6254d25339eSWeiping Zhang return attr->mode;
6264d25339eSWeiping Zhang }
6274d25339eSWeiping Zhang
blk_mq_queue_attr_visible(struct kobject * kobj,struct attribute * attr,int n)6286d85ebf9SYu Kuai static umode_t blk_mq_queue_attr_visible(struct kobject *kobj,
6296d85ebf9SYu Kuai struct attribute *attr, int n)
6306d85ebf9SYu Kuai {
6316d85ebf9SYu Kuai struct gendisk *disk = container_of(kobj, struct gendisk, queue_kobj);
6326d85ebf9SYu Kuai struct request_queue *q = disk->queue;
6336d85ebf9SYu Kuai
6346d85ebf9SYu Kuai if (!queue_is_mq(q))
6356d85ebf9SYu Kuai return 0;
6366d85ebf9SYu Kuai
6376d85ebf9SYu Kuai if (attr == &queue_io_timeout_entry.attr && !q->mq_ops->timeout)
6386d85ebf9SYu Kuai return 0;
6396d85ebf9SYu Kuai
6406d85ebf9SYu Kuai return attr->mode;
6416d85ebf9SYu Kuai }
6426d85ebf9SYu Kuai
6434d25339eSWeiping Zhang static struct attribute_group queue_attr_group = {
6444d25339eSWeiping Zhang .attrs = queue_attrs,
6454d25339eSWeiping Zhang .is_visible = queue_attr_visible,
6464d25339eSWeiping Zhang };
6474d25339eSWeiping Zhang
6486d85ebf9SYu Kuai static struct attribute_group blk_mq_queue_attr_group = {
6496d85ebf9SYu Kuai .attrs = blk_mq_queue_attrs,
6506d85ebf9SYu Kuai .is_visible = blk_mq_queue_attr_visible,
6516d85ebf9SYu Kuai };
6524d25339eSWeiping Zhang
6538324aa91SJens Axboe #define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
6548324aa91SJens Axboe
6558324aa91SJens Axboe static ssize_t
queue_attr_show(struct kobject * kobj,struct attribute * attr,char * page)6568324aa91SJens Axboe queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
6578324aa91SJens Axboe {
6588324aa91SJens Axboe struct queue_sysfs_entry *entry = to_queue(attr);
6592bd85221SChristoph Hellwig struct gendisk *disk = container_of(kobj, struct gendisk, queue_kobj);
6608324aa91SJens Axboe ssize_t res;
6618324aa91SJens Axboe
6628324aa91SJens Axboe if (!entry->show)
6638324aa91SJens Axboe return -EIO;
66462e35f94SChristoph Hellwig mutex_lock(&disk->queue->sysfs_lock);
66562e35f94SChristoph Hellwig res = entry->show(disk, page);
66662e35f94SChristoph Hellwig mutex_unlock(&disk->queue->sysfs_lock);
6678324aa91SJens Axboe return res;
6688324aa91SJens Axboe }
6698324aa91SJens Axboe
6708324aa91SJens Axboe static ssize_t
queue_attr_store(struct kobject * kobj,struct attribute * attr,const char * page,size_t length)6718324aa91SJens Axboe queue_attr_store(struct kobject *kobj, struct attribute *attr,
6728324aa91SJens Axboe const char *page, size_t length)
6738324aa91SJens Axboe {
6748324aa91SJens Axboe struct queue_sysfs_entry *entry = to_queue(attr);
6752bd85221SChristoph Hellwig struct gendisk *disk = container_of(kobj, struct gendisk, queue_kobj);
6762bd85221SChristoph Hellwig struct request_queue *q = disk->queue;
6778324aa91SJens Axboe ssize_t res;
6788324aa91SJens Axboe
6798324aa91SJens Axboe if (!entry->store)
6808324aa91SJens Axboe return -EIO;
6816728cb0eSJens Axboe
682*734e1a86SDamien Le Moal /*
683*734e1a86SDamien Le Moal * If the attribute needs to load a module, do it before freezing the
684*734e1a86SDamien Le Moal * queue to ensure that the module file can be read when the request
685*734e1a86SDamien Le Moal * queue is the one for the device storing the module file.
686*734e1a86SDamien Le Moal */
687*734e1a86SDamien Le Moal if (entry->load_module) {
688*734e1a86SDamien Le Moal res = entry->load_module(disk, page, length);
689*734e1a86SDamien Le Moal if (res)
690*734e1a86SDamien Le Moal return res;
691*734e1a86SDamien Le Moal }
692*734e1a86SDamien Le Moal
693af281414SChristoph Hellwig blk_mq_freeze_queue(q);
6948324aa91SJens Axboe mutex_lock(&q->sysfs_lock);
69562e35f94SChristoph Hellwig res = entry->store(disk, page, length);
6968324aa91SJens Axboe mutex_unlock(&q->sysfs_lock);
697af281414SChristoph Hellwig blk_mq_unfreeze_queue(q);
6988324aa91SJens Axboe return res;
6998324aa91SJens Axboe }
7008324aa91SJens Axboe
70152cf25d0SEmese Revfy static const struct sysfs_ops queue_sysfs_ops = {
7028324aa91SJens Axboe .show = queue_attr_show,
7038324aa91SJens Axboe .store = queue_attr_store,
7048324aa91SJens Axboe };
7058324aa91SJens Axboe
7064a8d14bbSChristoph Hellwig static const struct attribute_group *blk_queue_attr_groups[] = {
7074a8d14bbSChristoph Hellwig &queue_attr_group,
7086d85ebf9SYu Kuai &blk_mq_queue_attr_group,
7094a8d14bbSChristoph Hellwig NULL
7104a8d14bbSChristoph Hellwig };
7114a8d14bbSChristoph Hellwig
blk_queue_release(struct kobject * kobj)7122bd85221SChristoph Hellwig static void blk_queue_release(struct kobject *kobj)
7132bd85221SChristoph Hellwig {
7142bd85221SChristoph Hellwig /* nothing to do here, all data is associated with the parent gendisk */
7152bd85221SChristoph Hellwig }
7162bd85221SChristoph Hellwig
7175f622417SThomas Weißschuh static const struct kobj_type blk_queue_ktype = {
7184a8d14bbSChristoph Hellwig .default_groups = blk_queue_attr_groups,
7198324aa91SJens Axboe .sysfs_ops = &queue_sysfs_ops,
7202bd85221SChristoph Hellwig .release = blk_queue_release,
7218324aa91SJens Axboe };
7228324aa91SJens Axboe
blk_debugfs_remove(struct gendisk * disk)7236fc75f30SChristoph Hellwig static void blk_debugfs_remove(struct gendisk *disk)
7246fc75f30SChristoph Hellwig {
7256fc75f30SChristoph Hellwig struct request_queue *q = disk->queue;
7266fc75f30SChristoph Hellwig
7276fc75f30SChristoph Hellwig mutex_lock(&q->debugfs_mutex);
7286fc75f30SChristoph Hellwig blk_trace_shutdown(q);
7296fc75f30SChristoph Hellwig debugfs_remove_recursive(q->debugfs_dir);
7306fc75f30SChristoph Hellwig q->debugfs_dir = NULL;
7316fc75f30SChristoph Hellwig q->sched_debugfs_dir = NULL;
7326fc75f30SChristoph Hellwig q->rqos_debugfs_dir = NULL;
7336fc75f30SChristoph Hellwig mutex_unlock(&q->debugfs_mutex);
7346fc75f30SChristoph Hellwig }
7356fc75f30SChristoph Hellwig
7362c2086afSBart Van Assche /**
7372c2086afSBart Van Assche * blk_register_queue - register a block layer queue with sysfs
7382c2086afSBart Van Assche * @disk: Disk of which the request queue should be registered with sysfs.
7392c2086afSBart Van Assche */
blk_register_queue(struct gendisk * disk)7408324aa91SJens Axboe int blk_register_queue(struct gendisk *disk)
7418324aa91SJens Axboe {
7428324aa91SJens Axboe struct request_queue *q = disk->queue;
7438682b92eSChristoph Hellwig int ret;
7448324aa91SJens Axboe
745cecf5d87SMing Lei mutex_lock(&q->sysfs_dir_lock);
7462bd85221SChristoph Hellwig kobject_init(&disk->queue_kobj, &blk_queue_ktype);
7472bd85221SChristoph Hellwig ret = kobject_add(&disk->queue_kobj, &disk_to_dev(disk)->kobj, "queue");
748cc5c516dSChristoph Hellwig if (ret < 0)
7492bd85221SChristoph Hellwig goto out_put_queue_kobj;
7508324aa91SJens Axboe
75140602997SChristoph Hellwig if (queue_is_mq(q)) {
75240602997SChristoph Hellwig ret = blk_mq_sysfs_register(disk);
75340602997SChristoph Hellwig if (ret)
7542bd85221SChristoph Hellwig goto out_put_queue_kobj;
75540602997SChristoph Hellwig }
7565cf9c91bSChristoph Hellwig mutex_lock(&q->sysfs_lock);
7575cf9c91bSChristoph Hellwig
75885e0cbbbSLuis Chamberlain mutex_lock(&q->debugfs_mutex);
7592bd85221SChristoph Hellwig q->debugfs_dir = debugfs_create_dir(disk->disk_name, blk_debugfs_root);
7605cf9c91bSChristoph Hellwig if (queue_is_mq(q))
7619c1051aaSOmar Sandoval blk_mq_debugfs_register(q);
7625cf9c91bSChristoph Hellwig mutex_unlock(&q->debugfs_mutex);
763a2247f19SDamien Le Moal
76422d0c408SChristoph Hellwig ret = disk_register_independent_access_ranges(disk);
765a2247f19SDamien Le Moal if (ret)
76640602997SChristoph Hellwig goto out_debugfs_remove;
767a2247f19SDamien Le Moal
768344e9ffcSJens Axboe if (q->elevator) {
769cecf5d87SMing Lei ret = elv_register_queue(q, false);
770a2247f19SDamien Le Moal if (ret)
77140602997SChristoph Hellwig goto out_unregister_ia_ranges;
772b410aff2STahsin Erdogan }
773cecf5d87SMing Lei
774450deb93SChristoph Hellwig ret = blk_crypto_sysfs_register(disk);
77520f01f16SEric Biggers if (ret)
77640602997SChristoph Hellwig goto out_elv_unregister;
77720f01f16SEric Biggers
778cecf5d87SMing Lei blk_queue_flag_set(QUEUE_FLAG_REGISTERED, q);
77904aad37bSChristoph Hellwig wbt_enable_default(disk);
780cecf5d87SMing Lei
781cecf5d87SMing Lei /* Now everything is ready and send out KOBJ_ADD uevent */
7822bd85221SChristoph Hellwig kobject_uevent(&disk->queue_kobj, KOBJ_ADD);
7830546858cSYufen Yu if (q->elevator)
784cecf5d87SMing Lei kobject_uevent(&q->elevator->kobj, KOBJ_ADD);
785cecf5d87SMing Lei mutex_unlock(&q->sysfs_lock);
786cecf5d87SMing Lei mutex_unlock(&q->sysfs_dir_lock);
787a72c374fSMing Lei
788a72c374fSMing Lei /*
789a72c374fSMing Lei * SCSI probing may synchronously create and destroy a lot of
790a72c374fSMing Lei * request_queues for non-existent devices. Shutting down a fully
791a72c374fSMing Lei * functional queue takes measureable wallclock time as RCU grace
792a72c374fSMing Lei * periods are involved. To avoid excessive latency in these
793a72c374fSMing Lei * cases, a request_queue starts out in a degraded mode which is
794a72c374fSMing Lei * faster to shut down and is made fully functional here as
795a72c374fSMing Lei * request_queues for non-existent devices never get registered.
796a72c374fSMing Lei */
797a72c374fSMing Lei if (!blk_queue_init_done(q)) {
798a72c374fSMing Lei blk_queue_flag_set(QUEUE_FLAG_INIT_DONE, q);
799a72c374fSMing Lei percpu_ref_switch_to_percpu(&q->q_usage_counter);
800a72c374fSMing Lei }
801a72c374fSMing Lei
8028324aa91SJens Axboe return ret;
803a2247f19SDamien Le Moal
80440602997SChristoph Hellwig out_elv_unregister:
80520f01f16SEric Biggers elv_unregister_queue(q);
80640602997SChristoph Hellwig out_unregister_ia_ranges:
807a2247f19SDamien Le Moal disk_unregister_independent_access_ranges(disk);
80840602997SChristoph Hellwig out_debugfs_remove:
80940602997SChristoph Hellwig blk_debugfs_remove(disk);
810a2247f19SDamien Le Moal mutex_unlock(&q->sysfs_lock);
8112bd85221SChristoph Hellwig out_put_queue_kobj:
8122bd85221SChristoph Hellwig kobject_put(&disk->queue_kobj);
81340602997SChristoph Hellwig mutex_unlock(&q->sysfs_dir_lock);
814a2247f19SDamien Le Moal return ret;
8158324aa91SJens Axboe }
8168324aa91SJens Axboe
8172c2086afSBart Van Assche /**
8182c2086afSBart Van Assche * blk_unregister_queue - counterpart of blk_register_queue()
8192c2086afSBart Van Assche * @disk: Disk of which the request queue should be unregistered from sysfs.
8202c2086afSBart Van Assche *
8212c2086afSBart Van Assche * Note: the caller is responsible for guaranteeing that this function is called
8222c2086afSBart Van Assche * after blk_register_queue() has finished.
8232c2086afSBart Van Assche */
blk_unregister_queue(struct gendisk * disk)8248324aa91SJens Axboe void blk_unregister_queue(struct gendisk *disk)
8258324aa91SJens Axboe {
8268324aa91SJens Axboe struct request_queue *q = disk->queue;
8278324aa91SJens Axboe
828fb199746SAkinobu Mita if (WARN_ON(!q))
829fb199746SAkinobu Mita return;
830fb199746SAkinobu Mita
831fa70d2e2SMike Snitzer /* Return early if disk->queue was never registered. */
83258c898baSMing Lei if (!blk_queue_registered(q))
833fa70d2e2SMike Snitzer return;
834fa70d2e2SMike Snitzer
835667257e8SMike Snitzer /*
8362c2086afSBart Van Assche * Since sysfs_remove_dir() prevents adding new directory entries
8372c2086afSBart Van Assche * before removal of existing entries starts, protect against
8382c2086afSBart Van Assche * concurrent elv_iosched_store() calls.
839667257e8SMike Snitzer */
840e9a823fbSDavid Jeffery mutex_lock(&q->sysfs_lock);
8418814ce8aSBart Van Assche blk_queue_flag_clear(QUEUE_FLAG_REGISTERED, q);
842cecf5d87SMing Lei mutex_unlock(&q->sysfs_lock);
843334335d2SOmar Sandoval
844cecf5d87SMing Lei mutex_lock(&q->sysfs_dir_lock);
8452c2086afSBart Van Assche /*
8462c2086afSBart Van Assche * Remove the sysfs attributes before unregistering the queue data
8472c2086afSBart Van Assche * structures that can be modified through sysfs.
8482c2086afSBart Van Assche */
849344e9ffcSJens Axboe if (queue_is_mq(q))
8508682b92eSChristoph Hellwig blk_mq_sysfs_unregister(disk);
851450deb93SChristoph Hellwig blk_crypto_sysfs_unregister(disk);
852667257e8SMike Snitzer
853b89f625eSMing Lei mutex_lock(&q->sysfs_lock);
8542c2086afSBart Van Assche elv_unregister_queue(q);
855a2247f19SDamien Le Moal disk_unregister_independent_access_ranges(disk);
856b89f625eSMing Lei mutex_unlock(&q->sysfs_lock);
8570f692882SEric Biggers
8580f692882SEric Biggers /* Now that we've deleted all child objects, we can delete the queue. */
8592bd85221SChristoph Hellwig kobject_uevent(&disk->queue_kobj, KOBJ_REMOVE);
8602bd85221SChristoph Hellwig kobject_del(&disk->queue_kobj);
861cecf5d87SMing Lei mutex_unlock(&q->sysfs_dir_lock);
8622c2086afSBart Van Assche
8636fc75f30SChristoph Hellwig blk_debugfs_remove(disk);
8648324aa91SJens Axboe }
865