Blame view

block/blk-sysfs.c 15.5 KB
8324aa91d   Jens Axboe   block: split tag ...
1
2
3
4
  /*
   * Functions related to sysfs handling
   */
  #include <linux/kernel.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
5
  #include <linux/slab.h>
8324aa91d   Jens Axboe   block: split tag ...
6
7
8
9
  #include <linux/module.h>
  #include <linux/bio.h>
  #include <linux/blkdev.h>
  #include <linux/blktrace_api.h>
320ae51fe   Jens Axboe   blk-mq: new multi...
10
  #include <linux/blk-mq.h>
8324aa91d   Jens Axboe   block: split tag ...
11
12
  
  #include "blk.h"
5efd61135   Tejun Heo   blkcg: add blkcg_...
13
  #include "blk-cgroup.h"
3edcc0ce8   Ming Lei   block: blk-mq: do...
14
  #include "blk-mq.h"
8324aa91d   Jens Axboe   block: split tag ...
15
16
17
18
19
20
21
22
  
  struct queue_sysfs_entry {
  	struct attribute attr;
  	ssize_t (*show)(struct request_queue *, char *);
  	ssize_t (*store)(struct request_queue *, const char *, size_t);
  };
  
  static ssize_t
9cb308ce8   Xiaotian Feng   block: sysfs fix ...
23
  queue_var_show(unsigned long var, char *page)
8324aa91d   Jens Axboe   block: split tag ...
24
  {
9cb308ce8   Xiaotian Feng   block: sysfs fix ...
25
26
  	return sprintf(page, "%lu
  ", var);
8324aa91d   Jens Axboe   block: split tag ...
27
28
29
30
31
  }
  
  static ssize_t
  queue_var_store(unsigned long *var, const char *page, size_t count)
  {
b1f3b64d7   Dave Reisner   block: reject inv...
32
33
  	int err;
  	unsigned long v;
ed751e683   Jingoo Han   block/blk-sysfs.c...
34
  	err = kstrtoul(page, 10, &v);
b1f3b64d7   Dave Reisner   block: reject inv...
35
36
37
38
  	if (err || v > UINT_MAX)
  		return -EINVAL;
  
  	*var = v;
8324aa91d   Jens Axboe   block: split tag ...
39

8324aa91d   Jens Axboe   block: split tag ...
40
41
42
43
44
45
46
47
48
49
50
  	return count;
  }
  
  static ssize_t queue_requests_show(struct request_queue *q, char *page)
  {
  	return queue_var_show(q->nr_requests, (page));
  }
  
  static ssize_t
  queue_requests_store(struct request_queue *q, const char *page, size_t count)
  {
8324aa91d   Jens Axboe   block: split tag ...
51
  	unsigned long nr;
e3a2b3f93   Jens Axboe   blk-mq: allow cha...
52
  	int ret, err;
b8a9ae779   Jens Axboe   block: don't assu...
53

e3a2b3f93   Jens Axboe   blk-mq: allow cha...
54
  	if (!q->request_fn && !q->mq_ops)
b8a9ae779   Jens Axboe   block: don't assu...
55
56
57
  		return -EINVAL;
  
  	ret = queue_var_store(&nr, page, count);
b1f3b64d7   Dave Reisner   block: reject inv...
58
59
  	if (ret < 0)
  		return ret;
8324aa91d   Jens Axboe   block: split tag ...
60
61
  	if (nr < BLKDEV_MIN_RQ)
  		nr = BLKDEV_MIN_RQ;
e3a2b3f93   Jens Axboe   blk-mq: allow cha...
62
63
64
65
66
67
68
  	if (q->request_fn)
  		err = blk_update_nr_requests(q, nr);
  	else
  		err = blk_mq_update_nr_requests(q, nr);
  
  	if (err)
  		return err;
8324aa91d   Jens Axboe   block: split tag ...
69
70
71
72
73
  	return ret;
  }
  
  static ssize_t queue_ra_show(struct request_queue *q, char *page)
  {
9cb308ce8   Xiaotian Feng   block: sysfs fix ...
74
75
  	unsigned long ra_kb = q->backing_dev_info.ra_pages <<
  					(PAGE_CACHE_SHIFT - 10);
8324aa91d   Jens Axboe   block: split tag ...
76
77
78
79
80
81
82
83
84
  
  	return queue_var_show(ra_kb, (page));
  }
  
  static ssize_t
  queue_ra_store(struct request_queue *q, const char *page, size_t count)
  {
  	unsigned long ra_kb;
  	ssize_t ret = queue_var_store(&ra_kb, page, count);
b1f3b64d7   Dave Reisner   block: reject inv...
85
86
  	if (ret < 0)
  		return ret;
8324aa91d   Jens Axboe   block: split tag ...
87
  	q->backing_dev_info.ra_pages = ra_kb >> (PAGE_CACHE_SHIFT - 10);
8324aa91d   Jens Axboe   block: split tag ...
88
89
90
91
92
93
  
  	return ret;
  }
  
  static ssize_t queue_max_sectors_show(struct request_queue *q, char *page)
  {
ae03bf639   Martin K. Petersen   block: Use access...
94
  	int max_sectors_kb = queue_max_sectors(q) >> 1;
8324aa91d   Jens Axboe   block: split tag ...
95
96
97
  
  	return queue_var_show(max_sectors_kb, (page));
  }
c77a5710b   Martin K. Petersen   block: Export max...
98
99
100
101
  static ssize_t queue_max_segments_show(struct request_queue *q, char *page)
  {
  	return queue_var_show(queue_max_segments(q), (page));
  }
13f05c8d8   Martin K. Petersen   block/scsi: Provi...
102
103
104
105
  static ssize_t queue_max_integrity_segments_show(struct request_queue *q, char *page)
  {
  	return queue_var_show(q->limits.max_integrity_segments, (page));
  }
c77a5710b   Martin K. Petersen   block: Export max...
106
107
  static ssize_t queue_max_segment_size_show(struct request_queue *q, char *page)
  {
e692cb668   Martin K. Petersen   block: Deprecate ...
108
  	if (blk_queue_cluster(q))
c77a5710b   Martin K. Petersen   block: Export max...
109
110
111
112
  		return queue_var_show(queue_max_segment_size(q), (page));
  
  	return queue_var_show(PAGE_CACHE_SIZE, (page));
  }
e1defc4ff   Martin K. Petersen   block: Do away wi...
113
  static ssize_t queue_logical_block_size_show(struct request_queue *q, char *page)
e68b903c6   Martin K. Petersen   Expose hardware s...
114
  {
e1defc4ff   Martin K. Petersen   block: Do away wi...
115
  	return queue_var_show(queue_logical_block_size(q), page);
e68b903c6   Martin K. Petersen   Expose hardware s...
116
  }
c72758f33   Martin K. Petersen   block: Export I/O...
117
118
119
120
121
122
123
124
125
126
127
128
129
  static ssize_t queue_physical_block_size_show(struct request_queue *q, char *page)
  {
  	return queue_var_show(queue_physical_block_size(q), page);
  }
  
  static ssize_t queue_io_min_show(struct request_queue *q, char *page)
  {
  	return queue_var_show(queue_io_min(q), page);
  }
  
  static ssize_t queue_io_opt_show(struct request_queue *q, char *page)
  {
  	return queue_var_show(queue_io_opt(q), page);
e68b903c6   Martin K. Petersen   Expose hardware s...
130
  }
86b372814   Martin K. Petersen   block: Expose dis...
131
132
133
134
135
136
137
  static ssize_t queue_discard_granularity_show(struct request_queue *q, char *page)
  {
  	return queue_var_show(q->limits.discard_granularity, page);
  }
  
  static ssize_t queue_discard_max_show(struct request_queue *q, char *page)
  {
a934a00a6   Martin K. Petersen   block: Fix discar...
138
139
140
  	return sprintf(page, "%llu
  ",
  		       (unsigned long long)q->limits.max_discard_sectors << 9);
86b372814   Martin K. Petersen   block: Expose dis...
141
  }
98262f276   Martin K. Petersen   block: Allow devi...
142
143
144
145
  static ssize_t queue_discard_zeroes_data_show(struct request_queue *q, char *page)
  {
  	return queue_var_show(queue_discard_zeroes_data(q), page);
  }
4363ac7c1   Martin K. Petersen   block: Implement ...
146
147
148
149
150
151
  static ssize_t queue_write_same_max_show(struct request_queue *q, char *page)
  {
  	return sprintf(page, "%llu
  ",
  		(unsigned long long)q->limits.max_write_same_sectors << 9);
  }
8324aa91d   Jens Axboe   block: split tag ...
152
153
154
155
  static ssize_t
  queue_max_sectors_store(struct request_queue *q, const char *page, size_t count)
  {
  	unsigned long max_sectors_kb,
ae03bf639   Martin K. Petersen   block: Use access...
156
  		max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1,
8324aa91d   Jens Axboe   block: split tag ...
157
158
  			page_kb = 1 << (PAGE_CACHE_SHIFT - 10);
  	ssize_t ret = queue_var_store(&max_sectors_kb, page, count);
b1f3b64d7   Dave Reisner   block: reject inv...
159
160
  	if (ret < 0)
  		return ret;
8324aa91d   Jens Axboe   block: split tag ...
161
162
  	if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb)
  		return -EINVAL;
7c239517d   Wu Fengguang   block: don't take...
163

8324aa91d   Jens Axboe   block: split tag ...
164
  	spin_lock_irq(q->queue_lock);
c295fc057   Nikanth Karthikesan   block: Allow chan...
165
  	q->limits.max_sectors = max_sectors_kb << 1;
8324aa91d   Jens Axboe   block: split tag ...
166
167
168
169
170
171
172
  	spin_unlock_irq(q->queue_lock);
  
  	return ret;
  }
  
  static ssize_t queue_max_hw_sectors_show(struct request_queue *q, char *page)
  {
ae03bf639   Martin K. Petersen   block: Use access...
173
  	int max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1;
8324aa91d   Jens Axboe   block: split tag ...
174
175
176
  
  	return queue_var_show(max_hw_sectors_kb, (page));
  }
956bcb7c1   Jens Axboe   block: add helper...
177
178
179
180
181
182
183
184
185
186
187
188
189
190
  #define QUEUE_SYSFS_BIT_FNS(name, flag, neg)				\
  static ssize_t								\
  queue_show_##name(struct request_queue *q, char *page)			\
  {									\
  	int bit;							\
  	bit = test_bit(QUEUE_FLAG_##flag, &q->queue_flags);		\
  	return queue_var_show(neg ? !bit : bit, page);			\
  }									\
  static ssize_t								\
  queue_store_##name(struct request_queue *q, const char *page, size_t count) \
  {									\
  	unsigned long val;						\
  	ssize_t ret;							\
  	ret = queue_var_store(&val, page, count);			\
c678ef528   Arnd Bergmann   block: avoid usin...
191
192
  	if (ret < 0)							\
  		 return ret;						\
956bcb7c1   Jens Axboe   block: add helper...
193
194
195
196
197
198
199
200
201
202
  	if (neg)							\
  		val = !val;						\
  									\
  	spin_lock_irq(q->queue_lock);					\
  	if (val)							\
  		queue_flag_set(QUEUE_FLAG_##flag, q);			\
  	else								\
  		queue_flag_clear(QUEUE_FLAG_##flag, q);			\
  	spin_unlock_irq(q->queue_lock);					\
  	return ret;							\
1308835ff   Bartlomiej Zolnierkiewicz   block: export SSD...
203
  }
956bcb7c1   Jens Axboe   block: add helper...
204
205
206
207
  QUEUE_SYSFS_BIT_FNS(nonrot, NONROT, 1);
  QUEUE_SYSFS_BIT_FNS(random, ADD_RANDOM, 0);
  QUEUE_SYSFS_BIT_FNS(iostats, IO_STAT, 0);
  #undef QUEUE_SYSFS_BIT_FNS
1308835ff   Bartlomiej Zolnierkiewicz   block: export SSD...
208

ac9fafa12   Alan D. Brunelle   block: Skip I/O m...
209
210
  static ssize_t queue_nomerges_show(struct request_queue *q, char *page)
  {
488991e28   Alan D. Brunelle   block: Added in s...
211
212
  	return queue_var_show((blk_queue_nomerges(q) << 1) |
  			       blk_queue_noxmerges(q), page);
ac9fafa12   Alan D. Brunelle   block: Skip I/O m...
213
214
215
216
217
218
219
  }
  
  static ssize_t queue_nomerges_store(struct request_queue *q, const char *page,
  				    size_t count)
  {
  	unsigned long nm;
  	ssize_t ret = queue_var_store(&nm, page, count);
b1f3b64d7   Dave Reisner   block: reject inv...
220
221
  	if (ret < 0)
  		return ret;
bf0f97025   Jens Axboe   block: sysfs stor...
222
  	spin_lock_irq(q->queue_lock);
488991e28   Alan D. Brunelle   block: Added in s...
223
224
225
  	queue_flag_clear(QUEUE_FLAG_NOMERGES, q);
  	queue_flag_clear(QUEUE_FLAG_NOXMERGES, q);
  	if (nm == 2)
bf0f97025   Jens Axboe   block: sysfs stor...
226
  		queue_flag_set(QUEUE_FLAG_NOMERGES, q);
488991e28   Alan D. Brunelle   block: Added in s...
227
228
  	else if (nm)
  		queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
bf0f97025   Jens Axboe   block: sysfs stor...
229
  	spin_unlock_irq(q->queue_lock);
1308835ff   Bartlomiej Zolnierkiewicz   block: export SSD...
230

ac9fafa12   Alan D. Brunelle   block: Skip I/O m...
231
232
  	return ret;
  }
c7c22e4d5   Jens Axboe   block: add suppor...
233
234
  static ssize_t queue_rq_affinity_show(struct request_queue *q, char *page)
  {
9cb308ce8   Xiaotian Feng   block: sysfs fix ...
235
  	bool set = test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags);
5757a6d76   Dan Williams   block: strict rq_...
236
  	bool force = test_bit(QUEUE_FLAG_SAME_FORCE, &q->queue_flags);
c7c22e4d5   Jens Axboe   block: add suppor...
237

5757a6d76   Dan Williams   block: strict rq_...
238
  	return queue_var_show(set << force, page);
c7c22e4d5   Jens Axboe   block: add suppor...
239
240
241
242
243
244
  }
  
  static ssize_t
  queue_rq_affinity_store(struct request_queue *q, const char *page, size_t count)
  {
  	ssize_t ret = -EINVAL;
0a06ff068   Christoph Hellwig   kernel: remove CO...
245
  #ifdef CONFIG_SMP
c7c22e4d5   Jens Axboe   block: add suppor...
246
247
248
  	unsigned long val;
  
  	ret = queue_var_store(&val, page, count);
b1f3b64d7   Dave Reisner   block: reject inv...
249
250
  	if (ret < 0)
  		return ret;
c7c22e4d5   Jens Axboe   block: add suppor...
251
  	spin_lock_irq(q->queue_lock);
e8037d498   Eric Seppanen   block: Fix queue_...
252
  	if (val == 2) {
c7c22e4d5   Jens Axboe   block: add suppor...
253
  		queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
e8037d498   Eric Seppanen   block: Fix queue_...
254
255
256
257
258
  		queue_flag_set(QUEUE_FLAG_SAME_FORCE, q);
  	} else if (val == 1) {
  		queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
  		queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
  	} else if (val == 0) {
5757a6d76   Dan Williams   block: strict rq_...
259
260
261
  		queue_flag_clear(QUEUE_FLAG_SAME_COMP, q);
  		queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
  	}
c7c22e4d5   Jens Axboe   block: add suppor...
262
263
264
265
  	spin_unlock_irq(q->queue_lock);
  #endif
  	return ret;
  }
8324aa91d   Jens Axboe   block: split tag ...
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
  
  static struct queue_sysfs_entry queue_requests_entry = {
  	.attr = {.name = "nr_requests", .mode = S_IRUGO | S_IWUSR },
  	.show = queue_requests_show,
  	.store = queue_requests_store,
  };
  
  static struct queue_sysfs_entry queue_ra_entry = {
  	.attr = {.name = "read_ahead_kb", .mode = S_IRUGO | S_IWUSR },
  	.show = queue_ra_show,
  	.store = queue_ra_store,
  };
  
  static struct queue_sysfs_entry queue_max_sectors_entry = {
  	.attr = {.name = "max_sectors_kb", .mode = S_IRUGO | S_IWUSR },
  	.show = queue_max_sectors_show,
  	.store = queue_max_sectors_store,
  };
  
  static struct queue_sysfs_entry queue_max_hw_sectors_entry = {
  	.attr = {.name = "max_hw_sectors_kb", .mode = S_IRUGO },
  	.show = queue_max_hw_sectors_show,
  };
c77a5710b   Martin K. Petersen   block: Export max...
289
290
291
292
  static struct queue_sysfs_entry queue_max_segments_entry = {
  	.attr = {.name = "max_segments", .mode = S_IRUGO },
  	.show = queue_max_segments_show,
  };
13f05c8d8   Martin K. Petersen   block/scsi: Provi...
293
294
295
296
  static struct queue_sysfs_entry queue_max_integrity_segments_entry = {
  	.attr = {.name = "max_integrity_segments", .mode = S_IRUGO },
  	.show = queue_max_integrity_segments_show,
  };
c77a5710b   Martin K. Petersen   block: Export max...
297
298
299
300
  static struct queue_sysfs_entry queue_max_segment_size_entry = {
  	.attr = {.name = "max_segment_size", .mode = S_IRUGO },
  	.show = queue_max_segment_size_show,
  };
8324aa91d   Jens Axboe   block: split tag ...
301
302
303
304
305
  static struct queue_sysfs_entry queue_iosched_entry = {
  	.attr = {.name = "scheduler", .mode = S_IRUGO | S_IWUSR },
  	.show = elv_iosched_show,
  	.store = elv_iosched_store,
  };
e68b903c6   Martin K. Petersen   Expose hardware s...
306
307
  static struct queue_sysfs_entry queue_hw_sector_size_entry = {
  	.attr = {.name = "hw_sector_size", .mode = S_IRUGO },
e1defc4ff   Martin K. Petersen   block: Do away wi...
308
309
310
311
312
313
  	.show = queue_logical_block_size_show,
  };
  
  static struct queue_sysfs_entry queue_logical_block_size_entry = {
  	.attr = {.name = "logical_block_size", .mode = S_IRUGO },
  	.show = queue_logical_block_size_show,
e68b903c6   Martin K. Petersen   Expose hardware s...
314
  };
c72758f33   Martin K. Petersen   block: Export I/O...
315
316
317
318
319
320
321
322
323
324
325
326
327
  static struct queue_sysfs_entry queue_physical_block_size_entry = {
  	.attr = {.name = "physical_block_size", .mode = S_IRUGO },
  	.show = queue_physical_block_size_show,
  };
  
  static struct queue_sysfs_entry queue_io_min_entry = {
  	.attr = {.name = "minimum_io_size", .mode = S_IRUGO },
  	.show = queue_io_min_show,
  };
  
  static struct queue_sysfs_entry queue_io_opt_entry = {
  	.attr = {.name = "optimal_io_size", .mode = S_IRUGO },
  	.show = queue_io_opt_show,
e68b903c6   Martin K. Petersen   Expose hardware s...
328
  };
86b372814   Martin K. Petersen   block: Expose dis...
329
330
331
332
333
334
335
336
337
  static struct queue_sysfs_entry queue_discard_granularity_entry = {
  	.attr = {.name = "discard_granularity", .mode = S_IRUGO },
  	.show = queue_discard_granularity_show,
  };
  
  static struct queue_sysfs_entry queue_discard_max_entry = {
  	.attr = {.name = "discard_max_bytes", .mode = S_IRUGO },
  	.show = queue_discard_max_show,
  };
98262f276   Martin K. Petersen   block: Allow devi...
338
339
340
341
  static struct queue_sysfs_entry queue_discard_zeroes_data_entry = {
  	.attr = {.name = "discard_zeroes_data", .mode = S_IRUGO },
  	.show = queue_discard_zeroes_data_show,
  };
4363ac7c1   Martin K. Petersen   block: Implement ...
342
343
344
345
  static struct queue_sysfs_entry queue_write_same_max_entry = {
  	.attr = {.name = "write_same_max_bytes", .mode = S_IRUGO },
  	.show = queue_write_same_max_show,
  };
1308835ff   Bartlomiej Zolnierkiewicz   block: export SSD...
346
347
  static struct queue_sysfs_entry queue_nonrot_entry = {
  	.attr = {.name = "rotational", .mode = S_IRUGO | S_IWUSR },
956bcb7c1   Jens Axboe   block: add helper...
348
349
  	.show = queue_show_nonrot,
  	.store = queue_store_nonrot,
1308835ff   Bartlomiej Zolnierkiewicz   block: export SSD...
350
  };
ac9fafa12   Alan D. Brunelle   block: Skip I/O m...
351
352
353
354
355
  static struct queue_sysfs_entry queue_nomerges_entry = {
  	.attr = {.name = "nomerges", .mode = S_IRUGO | S_IWUSR },
  	.show = queue_nomerges_show,
  	.store = queue_nomerges_store,
  };
c7c22e4d5   Jens Axboe   block: add suppor...
356
357
358
359
360
  static struct queue_sysfs_entry queue_rq_affinity_entry = {
  	.attr = {.name = "rq_affinity", .mode = S_IRUGO | S_IWUSR },
  	.show = queue_rq_affinity_show,
  	.store = queue_rq_affinity_store,
  };
bc58ba946   Jens Axboe   block: add sysfs ...
361
362
  static struct queue_sysfs_entry queue_iostats_entry = {
  	.attr = {.name = "iostats", .mode = S_IRUGO | S_IWUSR },
956bcb7c1   Jens Axboe   block: add helper...
363
364
  	.show = queue_show_iostats,
  	.store = queue_store_iostats,
bc58ba946   Jens Axboe   block: add sysfs ...
365
  };
e2e1a148b   Jens Axboe   block: add sysfs ...
366
367
  static struct queue_sysfs_entry queue_random_entry = {
  	.attr = {.name = "add_random", .mode = S_IRUGO | S_IWUSR },
956bcb7c1   Jens Axboe   block: add helper...
368
369
  	.show = queue_show_random,
  	.store = queue_store_random,
e2e1a148b   Jens Axboe   block: add sysfs ...
370
  };
8324aa91d   Jens Axboe   block: split tag ...
371
372
373
374
375
  static struct attribute *default_attrs[] = {
  	&queue_requests_entry.attr,
  	&queue_ra_entry.attr,
  	&queue_max_hw_sectors_entry.attr,
  	&queue_max_sectors_entry.attr,
c77a5710b   Martin K. Petersen   block: Export max...
376
  	&queue_max_segments_entry.attr,
13f05c8d8   Martin K. Petersen   block/scsi: Provi...
377
  	&queue_max_integrity_segments_entry.attr,
c77a5710b   Martin K. Petersen   block: Export max...
378
  	&queue_max_segment_size_entry.attr,
8324aa91d   Jens Axboe   block: split tag ...
379
  	&queue_iosched_entry.attr,
e68b903c6   Martin K. Petersen   Expose hardware s...
380
  	&queue_hw_sector_size_entry.attr,
e1defc4ff   Martin K. Petersen   block: Do away wi...
381
  	&queue_logical_block_size_entry.attr,
c72758f33   Martin K. Petersen   block: Export I/O...
382
383
384
  	&queue_physical_block_size_entry.attr,
  	&queue_io_min_entry.attr,
  	&queue_io_opt_entry.attr,
86b372814   Martin K. Petersen   block: Expose dis...
385
386
  	&queue_discard_granularity_entry.attr,
  	&queue_discard_max_entry.attr,
98262f276   Martin K. Petersen   block: Allow devi...
387
  	&queue_discard_zeroes_data_entry.attr,
4363ac7c1   Martin K. Petersen   block: Implement ...
388
  	&queue_write_same_max_entry.attr,
1308835ff   Bartlomiej Zolnierkiewicz   block: export SSD...
389
  	&queue_nonrot_entry.attr,
ac9fafa12   Alan D. Brunelle   block: Skip I/O m...
390
  	&queue_nomerges_entry.attr,
c7c22e4d5   Jens Axboe   block: add suppor...
391
  	&queue_rq_affinity_entry.attr,
bc58ba946   Jens Axboe   block: add sysfs ...
392
  	&queue_iostats_entry.attr,
e2e1a148b   Jens Axboe   block: add sysfs ...
393
  	&queue_random_entry.attr,
8324aa91d   Jens Axboe   block: split tag ...
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
  	NULL,
  };
  
  #define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
  
  static ssize_t
  queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
  {
  	struct queue_sysfs_entry *entry = to_queue(attr);
  	struct request_queue *q =
  		container_of(kobj, struct request_queue, kobj);
  	ssize_t res;
  
  	if (!entry->show)
  		return -EIO;
  	mutex_lock(&q->sysfs_lock);
3f3299d5c   Bart Van Assche   block: Rename que...
410
  	if (blk_queue_dying(q)) {
8324aa91d   Jens Axboe   block: split tag ...
411
412
413
414
415
416
417
418
419
420
421
422
423
  		mutex_unlock(&q->sysfs_lock);
  		return -ENOENT;
  	}
  	res = entry->show(q, page);
  	mutex_unlock(&q->sysfs_lock);
  	return res;
  }
  
  static ssize_t
  queue_attr_store(struct kobject *kobj, struct attribute *attr,
  		    const char *page, size_t length)
  {
  	struct queue_sysfs_entry *entry = to_queue(attr);
6728cb0e6   Jens Axboe   block: make core ...
424
  	struct request_queue *q;
8324aa91d   Jens Axboe   block: split tag ...
425
426
427
428
  	ssize_t res;
  
  	if (!entry->store)
  		return -EIO;
6728cb0e6   Jens Axboe   block: make core ...
429
430
  
  	q = container_of(kobj, struct request_queue, kobj);
8324aa91d   Jens Axboe   block: split tag ...
431
  	mutex_lock(&q->sysfs_lock);
3f3299d5c   Bart Van Assche   block: Rename que...
432
  	if (blk_queue_dying(q)) {
8324aa91d   Jens Axboe   block: split tag ...
433
434
435
436
437
438
439
  		mutex_unlock(&q->sysfs_lock);
  		return -ENOENT;
  	}
  	res = entry->store(q, page, length);
  	mutex_unlock(&q->sysfs_lock);
  	return res;
  }
548bc8e1b   Tejun Heo   block: RCU free r...
440
441
442
443
444
445
  static void blk_free_queue_rcu(struct rcu_head *rcu_head)
  {
  	struct request_queue *q = container_of(rcu_head, struct request_queue,
  					       rcu_head);
  	kmem_cache_free(blk_requestq_cachep, q);
  }
8324aa91d   Jens Axboe   block: split tag ...
446
  /**
499337bb6   Andrew Morton   block/blk-sysfs.c...
447
448
   * blk_release_queue: - release a &struct request_queue when it is no longer needed
   * @kobj:    the kobj belonging to the request queue to be released
8324aa91d   Jens Axboe   block: split tag ...
449
450
   *
   * Description:
499337bb6   Andrew Morton   block/blk-sysfs.c...
451
   *     blk_release_queue is the pair to blk_init_queue() or
8324aa91d   Jens Axboe   block: split tag ...
452
453
454
455
456
   *     blk_queue_make_request().  It should be called when a request queue is
   *     being released; typically when a block device is being de-registered.
   *     Currently, its primary task it to free all the &struct request
   *     structures that were allocated to the queue and the queue itself.
   *
45a9c9d90   Bart Van Assche   blk-mq: Fix a use...
457
458
459
   * Note:
   *     The low level driver must have finished any outstanding requests first
   *     via blk_cleanup_queue().
8324aa91d   Jens Axboe   block: split tag ...
460
461
462
463
464
   **/
  static void blk_release_queue(struct kobject *kobj)
  {
  	struct request_queue *q =
  		container_of(kobj, struct request_queue, kobj);
8324aa91d   Jens Axboe   block: split tag ...
465

e8989fae3   Tejun Heo   blkcg: unify blkg...
466
  	blkcg_exit_queue(q);
7e5a87944   Tejun Heo   block, cfq: move ...
467
468
469
470
  	if (q->elevator) {
  		spin_lock_irq(q->queue_lock);
  		ioc_clear_queue(q);
  		spin_unlock_irq(q->queue_lock);
777eb1bf1   Hannes Reinecke   block: Free queue...
471
  		elevator_exit(q->elevator);
7e5a87944   Tejun Heo   block, cfq: move ...
472
  	}
777eb1bf1   Hannes Reinecke   block: Free queue...
473

a051661ca   Tejun Heo   blkcg: implement ...
474
  	blk_exit_rl(&q->root_rl);
8324aa91d   Jens Axboe   block: split tag ...
475
476
477
  
  	if (q->queue_tags)
  		__blk_queue_free_tags(q);
45a9c9d90   Bart Van Assche   blk-mq: Fix a use...
478
  	if (!q->mq_ops)
f70ced091   Ming Lei   blk-mq: support p...
479
  		blk_free_flush_queue(q->fq);
e09aae7ed   Ming Lei   blk-mq: release m...
480
481
  	else
  		blk_mq_release(q);
18741986a   Christoph Hellwig   blk-mq: rework fl...
482

8324aa91d   Jens Axboe   block: split tag ...
483
  	blk_trace_shutdown(q);
a73f730d0   Tejun Heo   block, cfq: move ...
484
  	ida_simple_remove(&blk_queue_ida, q->id);
548bc8e1b   Tejun Heo   block: RCU free r...
485
  	call_rcu(&q->rcu_head, blk_free_queue_rcu);
8324aa91d   Jens Axboe   block: split tag ...
486
  }
52cf25d0a   Emese Revfy   Driver core: Cons...
487
  static const struct sysfs_ops queue_sysfs_ops = {
8324aa91d   Jens Axboe   block: split tag ...
488
489
490
491
492
493
494
495
496
497
498
499
500
  	.show	= queue_attr_show,
  	.store	= queue_attr_store,
  };
  
  struct kobj_type blk_queue_ktype = {
  	.sysfs_ops	= &queue_sysfs_ops,
  	.default_attrs	= default_attrs,
  	.release	= blk_release_queue,
  };
  
  int blk_register_queue(struct gendisk *disk)
  {
  	int ret;
1d54ad6da   Li Zefan   blktrace: add tra...
501
  	struct device *dev = disk_to_dev(disk);
8324aa91d   Jens Axboe   block: split tag ...
502
  	struct request_queue *q = disk->queue;
fb1997463   Akinobu Mita   block: fix blk_re...
503
  	if (WARN_ON(!q))
8324aa91d   Jens Axboe   block: split tag ...
504
  		return -ENXIO;
749fefe67   Tejun Heo   block: lift the i...
505
  	/*
17497acbd   Tejun Heo   blk-mq, percpu_re...
506
507
508
509
510
511
512
  	 * SCSI probing may synchronously create and destroy a lot of
  	 * request_queues for non-existent devices.  Shutting down a fully
  	 * functional queue takes measureable wallclock time as RCU grace
  	 * periods are involved.  To avoid excessive latency in these
  	 * cases, a request_queue starts out in a degraded mode which is
  	 * faster to shut down and is made fully functional here as
  	 * request_queues for non-existent devices never get registered.
749fefe67   Tejun Heo   block: lift the i...
513
  	 */
df35c7c91   Alan Stern   Block: fix unbala...
514
515
516
  	if (!blk_queue_init_done(q)) {
  		queue_flag_set_unlocked(QUEUE_FLAG_INIT_DONE, q);
  		blk_queue_bypass_end(q);
17497acbd   Tejun Heo   blk-mq, percpu_re...
517
518
  		if (q->mq_ops)
  			blk_mq_finish_init(q);
df35c7c91   Alan Stern   Block: fix unbala...
519
  	}
749fefe67   Tejun Heo   block: lift the i...
520

1d54ad6da   Li Zefan   blktrace: add tra...
521
522
523
  	ret = blk_trace_init_sysfs(dev);
  	if (ret)
  		return ret;
c9059598e   Linus Torvalds   Merge branch 'for...
524
  	ret = kobject_add(&q->kobj, kobject_get(&dev->kobj), "%s", "queue");
ed5302d3c   Liu Yuan   block, blk-sysfs:...
525
526
  	if (ret < 0) {
  		blk_trace_remove_sysfs(dev);
8324aa91d   Jens Axboe   block: split tag ...
527
  		return ret;
ed5302d3c   Liu Yuan   block, blk-sysfs:...
528
  	}
8324aa91d   Jens Axboe   block: split tag ...
529
530
  
  	kobject_uevent(&q->kobj, KOBJ_ADD);
320ae51fe   Jens Axboe   blk-mq: new multi...
531
532
  	if (q->mq_ops)
  		blk_mq_register_disk(disk);
cd43e26f0   Martin K. Petersen   block: Expose sta...
533
534
  	if (!q->request_fn)
  		return 0;
8324aa91d   Jens Axboe   block: split tag ...
535
536
537
538
  	ret = elv_register_queue(q);
  	if (ret) {
  		kobject_uevent(&q->kobj, KOBJ_REMOVE);
  		kobject_del(&q->kobj);
80656b67b   Liu Yuan   block, blk-sysfs:...
539
  		blk_trace_remove_sysfs(dev);
c87ffbb81   Xiaotian Feng   block: put dev->k...
540
  		kobject_put(&dev->kobj);
8324aa91d   Jens Axboe   block: split tag ...
541
542
543
544
545
546
547
548
549
  		return ret;
  	}
  
  	return 0;
  }
  
  void blk_unregister_queue(struct gendisk *disk)
  {
  	struct request_queue *q = disk->queue;
fb1997463   Akinobu Mita   block: fix blk_re...
550
551
  	if (WARN_ON(!q))
  		return;
320ae51fe   Jens Axboe   blk-mq: new multi...
552
553
  	if (q->mq_ops)
  		blk_mq_unregister_disk(disk);
48c0d4d4c   Zdenek Kabelac   Add missing blk_t...
554
  	if (q->request_fn)
8324aa91d   Jens Axboe   block: split tag ...
555
  		elv_unregister_queue(q);
48c0d4d4c   Zdenek Kabelac   Add missing blk_t...
556
557
558
559
  	kobject_uevent(&q->kobj, KOBJ_REMOVE);
  	kobject_del(&q->kobj);
  	blk_trace_remove_sysfs(disk_to_dev(disk));
  	kobject_put(&disk_to_dev(disk)->kobj);
8324aa91d   Jens Axboe   block: split tag ...
560
  }