blob: 60fda88c57f0dd83e930d3859ab0626407a01fd2 [file] [log] [blame]
Jens Axboe8324aa92008-01-29 14:51:59 +01001/*
2 * Functions related to sysfs handling
3 */
4#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09005#include <linux/slab.h>
Jens Axboe8324aa92008-01-29 14:51:59 +01006#include <linux/module.h>
7#include <linux/bio.h>
8#include <linux/blkdev.h>
9#include <linux/blktrace_api.h>
10
11#include "blk.h"
12
13struct queue_sysfs_entry {
14 struct attribute attr;
15 ssize_t (*show)(struct request_queue *, char *);
16 ssize_t (*store)(struct request_queue *, const char *, size_t);
17};
18
19static ssize_t
Xiaotian Feng9cb308c2009-07-17 15:26:26 +080020queue_var_show(unsigned long var, char *page)
Jens Axboe8324aa92008-01-29 14:51:59 +010021{
Xiaotian Feng9cb308c2009-07-17 15:26:26 +080022 return sprintf(page, "%lu\n", var);
Jens Axboe8324aa92008-01-29 14:51:59 +010023}
24
25static ssize_t
26queue_var_store(unsigned long *var, const char *page, size_t count)
27{
28 char *p = (char *) page;
29
30 *var = simple_strtoul(p, &p, 10);
31 return count;
32}
33
34static ssize_t queue_requests_show(struct request_queue *q, char *page)
35{
36 return queue_var_show(q->nr_requests, (page));
37}
38
39static ssize_t
40queue_requests_store(struct request_queue *q, const char *page, size_t count)
41{
42 struct request_list *rl = &q->rq;
43 unsigned long nr;
Jens Axboeb8a9ae72009-09-11 22:44:29 +020044 int ret;
45
46 if (!q->request_fn)
47 return -EINVAL;
48
49 ret = queue_var_store(&nr, page, count);
Jens Axboe8324aa92008-01-29 14:51:59 +010050 if (nr < BLKDEV_MIN_RQ)
51 nr = BLKDEV_MIN_RQ;
52
53 spin_lock_irq(q->queue_lock);
54 q->nr_requests = nr;
55 blk_queue_congestion_threshold(q);
56
Jens Axboe1faa16d2009-04-06 14:48:01 +020057 if (rl->count[BLK_RW_SYNC] >= queue_congestion_on_threshold(q))
58 blk_set_queue_congested(q, BLK_RW_SYNC);
59 else if (rl->count[BLK_RW_SYNC] < queue_congestion_off_threshold(q))
60 blk_clear_queue_congested(q, BLK_RW_SYNC);
Jens Axboe8324aa92008-01-29 14:51:59 +010061
Jens Axboe1faa16d2009-04-06 14:48:01 +020062 if (rl->count[BLK_RW_ASYNC] >= queue_congestion_on_threshold(q))
63 blk_set_queue_congested(q, BLK_RW_ASYNC);
64 else if (rl->count[BLK_RW_ASYNC] < queue_congestion_off_threshold(q))
65 blk_clear_queue_congested(q, BLK_RW_ASYNC);
Jens Axboe8324aa92008-01-29 14:51:59 +010066
Jens Axboe1faa16d2009-04-06 14:48:01 +020067 if (rl->count[BLK_RW_SYNC] >= q->nr_requests) {
68 blk_set_queue_full(q, BLK_RW_SYNC);
Tao Ma60735b62011-04-19 13:50:40 +020069 } else {
Jens Axboe1faa16d2009-04-06 14:48:01 +020070 blk_clear_queue_full(q, BLK_RW_SYNC);
71 wake_up(&rl->wait[BLK_RW_SYNC]);
Jens Axboe8324aa92008-01-29 14:51:59 +010072 }
73
Jens Axboe1faa16d2009-04-06 14:48:01 +020074 if (rl->count[BLK_RW_ASYNC] >= q->nr_requests) {
75 blk_set_queue_full(q, BLK_RW_ASYNC);
Tao Ma60735b62011-04-19 13:50:40 +020076 } else {
Jens Axboe1faa16d2009-04-06 14:48:01 +020077 blk_clear_queue_full(q, BLK_RW_ASYNC);
78 wake_up(&rl->wait[BLK_RW_ASYNC]);
Jens Axboe8324aa92008-01-29 14:51:59 +010079 }
80 spin_unlock_irq(q->queue_lock);
81 return ret;
82}
83
84static ssize_t queue_ra_show(struct request_queue *q, char *page)
85{
Xiaotian Feng9cb308c2009-07-17 15:26:26 +080086 unsigned long ra_kb = q->backing_dev_info.ra_pages <<
87 (PAGE_CACHE_SHIFT - 10);
Jens Axboe8324aa92008-01-29 14:51:59 +010088
89 return queue_var_show(ra_kb, (page));
90}
91
92static ssize_t
93queue_ra_store(struct request_queue *q, const char *page, size_t count)
94{
95 unsigned long ra_kb;
96 ssize_t ret = queue_var_store(&ra_kb, page, count);
97
Jens Axboe8324aa92008-01-29 14:51:59 +010098 q->backing_dev_info.ra_pages = ra_kb >> (PAGE_CACHE_SHIFT - 10);
Jens Axboe8324aa92008-01-29 14:51:59 +010099
100 return ret;
101}
102
103static ssize_t queue_max_sectors_show(struct request_queue *q, char *page)
104{
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400105 int max_sectors_kb = queue_max_sectors(q) >> 1;
Jens Axboe8324aa92008-01-29 14:51:59 +0100106
107 return queue_var_show(max_sectors_kb, (page));
108}
109
Martin K. Petersenc77a5712010-03-10 00:48:33 -0500110static ssize_t queue_max_segments_show(struct request_queue *q, char *page)
111{
112 return queue_var_show(queue_max_segments(q), (page));
113}
114
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200115static ssize_t queue_max_integrity_segments_show(struct request_queue *q, char *page)
116{
117 return queue_var_show(q->limits.max_integrity_segments, (page));
118}
119
Martin K. Petersenc77a5712010-03-10 00:48:33 -0500120static ssize_t queue_max_segment_size_show(struct request_queue *q, char *page)
121{
Martin K. Petersene692cb62010-12-01 19:41:49 +0100122 if (blk_queue_cluster(q))
Martin K. Petersenc77a5712010-03-10 00:48:33 -0500123 return queue_var_show(queue_max_segment_size(q), (page));
124
125 return queue_var_show(PAGE_CACHE_SIZE, (page));
126}
127
Martin K. Petersene1defc42009-05-22 17:17:49 -0400128static ssize_t queue_logical_block_size_show(struct request_queue *q, char *page)
Martin K. Petersene68b9032008-01-29 19:14:08 +0100129{
Martin K. Petersene1defc42009-05-22 17:17:49 -0400130 return queue_var_show(queue_logical_block_size(q), page);
Martin K. Petersene68b9032008-01-29 19:14:08 +0100131}
132
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400133static ssize_t queue_physical_block_size_show(struct request_queue *q, char *page)
134{
135 return queue_var_show(queue_physical_block_size(q), page);
136}
137
138static ssize_t queue_io_min_show(struct request_queue *q, char *page)
139{
140 return queue_var_show(queue_io_min(q), page);
141}
142
143static ssize_t queue_io_opt_show(struct request_queue *q, char *page)
144{
145 return queue_var_show(queue_io_opt(q), page);
Jens Axboe8324aa92008-01-29 14:51:59 +0100146}
147
Martin K. Petersen86b37282009-11-10 11:50:21 +0100148static ssize_t queue_discard_granularity_show(struct request_queue *q, char *page)
149{
150 return queue_var_show(q->limits.discard_granularity, page);
151}
152
153static ssize_t queue_discard_max_show(struct request_queue *q, char *page)
154{
Martin K. Petersena934a002011-05-18 10:37:35 +0200155 return sprintf(page, "%llu\n",
156 (unsigned long long)q->limits.max_discard_sectors << 9);
Martin K. Petersen86b37282009-11-10 11:50:21 +0100157}
158
Martin K. Petersen98262f22009-12-03 09:24:48 +0100159static ssize_t queue_discard_zeroes_data_show(struct request_queue *q, char *page)
160{
161 return queue_var_show(queue_discard_zeroes_data(q), page);
162}
163
Jens Axboe8324aa92008-01-29 14:51:59 +0100164static ssize_t
165queue_max_sectors_store(struct request_queue *q, const char *page, size_t count)
166{
167 unsigned long max_sectors_kb,
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400168 max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1,
Jens Axboe8324aa92008-01-29 14:51:59 +0100169 page_kb = 1 << (PAGE_CACHE_SHIFT - 10);
170 ssize_t ret = queue_var_store(&max_sectors_kb, page, count);
171
172 if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb)
173 return -EINVAL;
Wu Fengguang7c239512008-11-25 09:08:39 +0100174
Jens Axboe8324aa92008-01-29 14:51:59 +0100175 spin_lock_irq(q->queue_lock);
Nikanth Karthikesanc295fc02009-09-01 22:40:15 +0200176 q->limits.max_sectors = max_sectors_kb << 1;
Jens Axboe8324aa92008-01-29 14:51:59 +0100177 spin_unlock_irq(q->queue_lock);
178
179 return ret;
180}
181
182static ssize_t queue_max_hw_sectors_show(struct request_queue *q, char *page)
183{
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400184 int max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1;
Jens Axboe8324aa92008-01-29 14:51:59 +0100185
186 return queue_var_show(max_hw_sectors_kb, (page));
187}
188
Jens Axboe956bcb72010-08-07 18:13:50 +0200189#define QUEUE_SYSFS_BIT_FNS(name, flag, neg) \
190static ssize_t \
191queue_show_##name(struct request_queue *q, char *page) \
192{ \
193 int bit; \
194 bit = test_bit(QUEUE_FLAG_##flag, &q->queue_flags); \
195 return queue_var_show(neg ? !bit : bit, page); \
196} \
197static ssize_t \
198queue_store_##name(struct request_queue *q, const char *page, size_t count) \
199{ \
200 unsigned long val; \
201 ssize_t ret; \
202 ret = queue_var_store(&val, page, count); \
203 if (neg) \
204 val = !val; \
205 \
206 spin_lock_irq(q->queue_lock); \
207 if (val) \
208 queue_flag_set(QUEUE_FLAG_##flag, q); \
209 else \
210 queue_flag_clear(QUEUE_FLAG_##flag, q); \
211 spin_unlock_irq(q->queue_lock); \
212 return ret; \
Bartlomiej Zolnierkiewicz1308835f2009-01-07 12:22:39 +0100213}
214
Jens Axboe956bcb72010-08-07 18:13:50 +0200215QUEUE_SYSFS_BIT_FNS(nonrot, NONROT, 1);
216QUEUE_SYSFS_BIT_FNS(random, ADD_RANDOM, 0);
217QUEUE_SYSFS_BIT_FNS(iostats, IO_STAT, 0);
218#undef QUEUE_SYSFS_BIT_FNS
Bartlomiej Zolnierkiewicz1308835f2009-01-07 12:22:39 +0100219
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200220static ssize_t queue_nomerges_show(struct request_queue *q, char *page)
221{
Alan D. Brunelle488991e2010-01-29 09:04:08 +0100222 return queue_var_show((blk_queue_nomerges(q) << 1) |
223 blk_queue_noxmerges(q), page);
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200224}
225
226static ssize_t queue_nomerges_store(struct request_queue *q, const char *page,
227 size_t count)
228{
229 unsigned long nm;
230 ssize_t ret = queue_var_store(&nm, page, count);
231
Jens Axboebf0f9702008-05-07 09:09:39 +0200232 spin_lock_irq(q->queue_lock);
Alan D. Brunelle488991e2010-01-29 09:04:08 +0100233 queue_flag_clear(QUEUE_FLAG_NOMERGES, q);
234 queue_flag_clear(QUEUE_FLAG_NOXMERGES, q);
235 if (nm == 2)
Jens Axboebf0f9702008-05-07 09:09:39 +0200236 queue_flag_set(QUEUE_FLAG_NOMERGES, q);
Alan D. Brunelle488991e2010-01-29 09:04:08 +0100237 else if (nm)
238 queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
Jens Axboebf0f9702008-05-07 09:09:39 +0200239 spin_unlock_irq(q->queue_lock);
Bartlomiej Zolnierkiewicz1308835f2009-01-07 12:22:39 +0100240
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200241 return ret;
242}
243
Jens Axboec7c22e42008-09-13 20:26:01 +0200244static ssize_t queue_rq_affinity_show(struct request_queue *q, char *page)
245{
Xiaotian Feng9cb308c2009-07-17 15:26:26 +0800246 bool set = test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags);
Dan Williams5757a6d2011-07-23 20:44:25 +0200247 bool force = test_bit(QUEUE_FLAG_SAME_FORCE, &q->queue_flags);
Jens Axboec7c22e42008-09-13 20:26:01 +0200248
Dan Williams5757a6d2011-07-23 20:44:25 +0200249 return queue_var_show(set << force, page);
Jens Axboec7c22e42008-09-13 20:26:01 +0200250}
251
252static ssize_t
253queue_rq_affinity_store(struct request_queue *q, const char *page, size_t count)
254{
255 ssize_t ret = -EINVAL;
256#if defined(CONFIG_USE_GENERIC_SMP_HELPERS)
257 unsigned long val;
258
259 ret = queue_var_store(&val, page, count);
260 spin_lock_irq(q->queue_lock);
Eric Seppanene8037d42011-08-23 21:25:12 +0200261 if (val == 2) {
Jens Axboec7c22e42008-09-13 20:26:01 +0200262 queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
Eric Seppanene8037d42011-08-23 21:25:12 +0200263 queue_flag_set(QUEUE_FLAG_SAME_FORCE, q);
264 } else if (val == 1) {
265 queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
266 queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
267 } else if (val == 0) {
Dan Williams5757a6d2011-07-23 20:44:25 +0200268 queue_flag_clear(QUEUE_FLAG_SAME_COMP, q);
269 queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
270 }
Jens Axboec7c22e42008-09-13 20:26:01 +0200271 spin_unlock_irq(q->queue_lock);
272#endif
273 return ret;
274}
Jens Axboe8324aa92008-01-29 14:51:59 +0100275
276static struct queue_sysfs_entry queue_requests_entry = {
277 .attr = {.name = "nr_requests", .mode = S_IRUGO | S_IWUSR },
278 .show = queue_requests_show,
279 .store = queue_requests_store,
280};
281
282static struct queue_sysfs_entry queue_ra_entry = {
283 .attr = {.name = "read_ahead_kb", .mode = S_IRUGO | S_IWUSR },
284 .show = queue_ra_show,
285 .store = queue_ra_store,
286};
287
288static struct queue_sysfs_entry queue_max_sectors_entry = {
289 .attr = {.name = "max_sectors_kb", .mode = S_IRUGO | S_IWUSR },
290 .show = queue_max_sectors_show,
291 .store = queue_max_sectors_store,
292};
293
294static struct queue_sysfs_entry queue_max_hw_sectors_entry = {
295 .attr = {.name = "max_hw_sectors_kb", .mode = S_IRUGO },
296 .show = queue_max_hw_sectors_show,
297};
298
Martin K. Petersenc77a5712010-03-10 00:48:33 -0500299static struct queue_sysfs_entry queue_max_segments_entry = {
300 .attr = {.name = "max_segments", .mode = S_IRUGO },
301 .show = queue_max_segments_show,
302};
303
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200304static struct queue_sysfs_entry queue_max_integrity_segments_entry = {
305 .attr = {.name = "max_integrity_segments", .mode = S_IRUGO },
306 .show = queue_max_integrity_segments_show,
307};
308
Martin K. Petersenc77a5712010-03-10 00:48:33 -0500309static struct queue_sysfs_entry queue_max_segment_size_entry = {
310 .attr = {.name = "max_segment_size", .mode = S_IRUGO },
311 .show = queue_max_segment_size_show,
312};
313
Jens Axboe8324aa92008-01-29 14:51:59 +0100314static struct queue_sysfs_entry queue_iosched_entry = {
315 .attr = {.name = "scheduler", .mode = S_IRUGO | S_IWUSR },
316 .show = elv_iosched_show,
317 .store = elv_iosched_store,
318};
319
Martin K. Petersene68b9032008-01-29 19:14:08 +0100320static struct queue_sysfs_entry queue_hw_sector_size_entry = {
321 .attr = {.name = "hw_sector_size", .mode = S_IRUGO },
Martin K. Petersene1defc42009-05-22 17:17:49 -0400322 .show = queue_logical_block_size_show,
323};
324
325static struct queue_sysfs_entry queue_logical_block_size_entry = {
326 .attr = {.name = "logical_block_size", .mode = S_IRUGO },
327 .show = queue_logical_block_size_show,
Martin K. Petersene68b9032008-01-29 19:14:08 +0100328};
329
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400330static struct queue_sysfs_entry queue_physical_block_size_entry = {
331 .attr = {.name = "physical_block_size", .mode = S_IRUGO },
332 .show = queue_physical_block_size_show,
333};
334
335static struct queue_sysfs_entry queue_io_min_entry = {
336 .attr = {.name = "minimum_io_size", .mode = S_IRUGO },
337 .show = queue_io_min_show,
338};
339
340static struct queue_sysfs_entry queue_io_opt_entry = {
341 .attr = {.name = "optimal_io_size", .mode = S_IRUGO },
342 .show = queue_io_opt_show,
Jens Axboe8324aa92008-01-29 14:51:59 +0100343};
344
Martin K. Petersen86b37282009-11-10 11:50:21 +0100345static struct queue_sysfs_entry queue_discard_granularity_entry = {
346 .attr = {.name = "discard_granularity", .mode = S_IRUGO },
347 .show = queue_discard_granularity_show,
348};
349
350static struct queue_sysfs_entry queue_discard_max_entry = {
351 .attr = {.name = "discard_max_bytes", .mode = S_IRUGO },
352 .show = queue_discard_max_show,
353};
354
Martin K. Petersen98262f22009-12-03 09:24:48 +0100355static struct queue_sysfs_entry queue_discard_zeroes_data_entry = {
356 .attr = {.name = "discard_zeroes_data", .mode = S_IRUGO },
357 .show = queue_discard_zeroes_data_show,
358};
359
Bartlomiej Zolnierkiewicz1308835f2009-01-07 12:22:39 +0100360static struct queue_sysfs_entry queue_nonrot_entry = {
361 .attr = {.name = "rotational", .mode = S_IRUGO | S_IWUSR },
Jens Axboe956bcb72010-08-07 18:13:50 +0200362 .show = queue_show_nonrot,
363 .store = queue_store_nonrot,
Bartlomiej Zolnierkiewicz1308835f2009-01-07 12:22:39 +0100364};
365
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200366static struct queue_sysfs_entry queue_nomerges_entry = {
367 .attr = {.name = "nomerges", .mode = S_IRUGO | S_IWUSR },
368 .show = queue_nomerges_show,
369 .store = queue_nomerges_store,
370};
371
Jens Axboec7c22e42008-09-13 20:26:01 +0200372static struct queue_sysfs_entry queue_rq_affinity_entry = {
373 .attr = {.name = "rq_affinity", .mode = S_IRUGO | S_IWUSR },
374 .show = queue_rq_affinity_show,
375 .store = queue_rq_affinity_store,
376};
377
Jens Axboebc58ba92009-01-23 10:54:44 +0100378static struct queue_sysfs_entry queue_iostats_entry = {
379 .attr = {.name = "iostats", .mode = S_IRUGO | S_IWUSR },
Jens Axboe956bcb72010-08-07 18:13:50 +0200380 .show = queue_show_iostats,
381 .store = queue_store_iostats,
Jens Axboebc58ba92009-01-23 10:54:44 +0100382};
383
Jens Axboee2e1a142010-06-09 10:42:09 +0200384static struct queue_sysfs_entry queue_random_entry = {
385 .attr = {.name = "add_random", .mode = S_IRUGO | S_IWUSR },
Jens Axboe956bcb72010-08-07 18:13:50 +0200386 .show = queue_show_random,
387 .store = queue_store_random,
Jens Axboee2e1a142010-06-09 10:42:09 +0200388};
389
Jens Axboe8324aa92008-01-29 14:51:59 +0100390static struct attribute *default_attrs[] = {
391 &queue_requests_entry.attr,
392 &queue_ra_entry.attr,
393 &queue_max_hw_sectors_entry.attr,
394 &queue_max_sectors_entry.attr,
Martin K. Petersenc77a5712010-03-10 00:48:33 -0500395 &queue_max_segments_entry.attr,
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200396 &queue_max_integrity_segments_entry.attr,
Martin K. Petersenc77a5712010-03-10 00:48:33 -0500397 &queue_max_segment_size_entry.attr,
Jens Axboe8324aa92008-01-29 14:51:59 +0100398 &queue_iosched_entry.attr,
Martin K. Petersene68b9032008-01-29 19:14:08 +0100399 &queue_hw_sector_size_entry.attr,
Martin K. Petersene1defc42009-05-22 17:17:49 -0400400 &queue_logical_block_size_entry.attr,
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400401 &queue_physical_block_size_entry.attr,
402 &queue_io_min_entry.attr,
403 &queue_io_opt_entry.attr,
Martin K. Petersen86b37282009-11-10 11:50:21 +0100404 &queue_discard_granularity_entry.attr,
405 &queue_discard_max_entry.attr,
Martin K. Petersen98262f22009-12-03 09:24:48 +0100406 &queue_discard_zeroes_data_entry.attr,
Bartlomiej Zolnierkiewicz1308835f2009-01-07 12:22:39 +0100407 &queue_nonrot_entry.attr,
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200408 &queue_nomerges_entry.attr,
Jens Axboec7c22e42008-09-13 20:26:01 +0200409 &queue_rq_affinity_entry.attr,
Jens Axboebc58ba92009-01-23 10:54:44 +0100410 &queue_iostats_entry.attr,
Jens Axboee2e1a142010-06-09 10:42:09 +0200411 &queue_random_entry.attr,
Jens Axboe8324aa92008-01-29 14:51:59 +0100412 NULL,
413};
414
415#define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
416
417static ssize_t
418queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
419{
420 struct queue_sysfs_entry *entry = to_queue(attr);
421 struct request_queue *q =
422 container_of(kobj, struct request_queue, kobj);
423 ssize_t res;
424
425 if (!entry->show)
426 return -EIO;
427 mutex_lock(&q->sysfs_lock);
428 if (test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)) {
429 mutex_unlock(&q->sysfs_lock);
430 return -ENOENT;
431 }
432 res = entry->show(q, page);
433 mutex_unlock(&q->sysfs_lock);
434 return res;
435}
436
437static ssize_t
438queue_attr_store(struct kobject *kobj, struct attribute *attr,
439 const char *page, size_t length)
440{
441 struct queue_sysfs_entry *entry = to_queue(attr);
Jens Axboe6728cb02008-01-31 13:03:55 +0100442 struct request_queue *q;
Jens Axboe8324aa92008-01-29 14:51:59 +0100443 ssize_t res;
444
445 if (!entry->store)
446 return -EIO;
Jens Axboe6728cb02008-01-31 13:03:55 +0100447
448 q = container_of(kobj, struct request_queue, kobj);
Jens Axboe8324aa92008-01-29 14:51:59 +0100449 mutex_lock(&q->sysfs_lock);
450 if (test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)) {
451 mutex_unlock(&q->sysfs_lock);
452 return -ENOENT;
453 }
454 res = entry->store(q, page, length);
455 mutex_unlock(&q->sysfs_lock);
456 return res;
457}
458
459/**
460 * blk_cleanup_queue: - release a &struct request_queue when it is no longer needed
461 * @kobj: the kobj belonging of the request queue to be released
462 *
463 * Description:
464 * blk_cleanup_queue is the pair to blk_init_queue() or
465 * blk_queue_make_request(). It should be called when a request queue is
466 * being released; typically when a block device is being de-registered.
467 * Currently, its primary task it to free all the &struct request
468 * structures that were allocated to the queue and the queue itself.
469 *
470 * Caveat:
471 * Hopefully the low level driver will have finished any
472 * outstanding requests first...
473 **/
474static void blk_release_queue(struct kobject *kobj)
475{
476 struct request_queue *q =
477 container_of(kobj, struct request_queue, kobj);
478 struct request_list *rl = &q->rq;
479
480 blk_sync_queue(q);
481
Hannes Reinecke777eb1b2011-09-28 08:07:01 -0600482 if (q->elevator)
483 elevator_exit(q->elevator);
484
485 blk_throtl_exit(q);
486
Jens Axboe8324aa92008-01-29 14:51:59 +0100487 if (rl->rq_pool)
488 mempool_destroy(rl->rq_pool);
489
490 if (q->queue_tags)
491 __blk_queue_free_tags(q);
492
493 blk_trace_shutdown(q);
494
495 bdi_destroy(&q->backing_dev_info);
496 kmem_cache_free(blk_requestq_cachep, q);
497}
498
Emese Revfy52cf25d2010-01-19 02:58:23 +0100499static const struct sysfs_ops queue_sysfs_ops = {
Jens Axboe8324aa92008-01-29 14:51:59 +0100500 .show = queue_attr_show,
501 .store = queue_attr_store,
502};
503
504struct kobj_type blk_queue_ktype = {
505 .sysfs_ops = &queue_sysfs_ops,
506 .default_attrs = default_attrs,
507 .release = blk_release_queue,
508};
509
510int blk_register_queue(struct gendisk *disk)
511{
512 int ret;
Li Zefan1d54ad62009-04-14 14:00:05 +0800513 struct device *dev = disk_to_dev(disk);
Jens Axboe8324aa92008-01-29 14:51:59 +0100514 struct request_queue *q = disk->queue;
515
Akinobu Mitafb199742008-04-21 09:51:06 +0200516 if (WARN_ON(!q))
Jens Axboe8324aa92008-01-29 14:51:59 +0100517 return -ENXIO;
518
Li Zefan1d54ad62009-04-14 14:00:05 +0800519 ret = blk_trace_init_sysfs(dev);
520 if (ret)
521 return ret;
522
Linus Torvaldsc9059592009-06-11 10:52:27 -0700523 ret = kobject_add(&q->kobj, kobject_get(&dev->kobj), "%s", "queue");
Liu Yuaned5302d2011-04-19 13:47:58 +0200524 if (ret < 0) {
525 blk_trace_remove_sysfs(dev);
Jens Axboe8324aa92008-01-29 14:51:59 +0100526 return ret;
Liu Yuaned5302d2011-04-19 13:47:58 +0200527 }
Jens Axboe8324aa92008-01-29 14:51:59 +0100528
529 kobject_uevent(&q->kobj, KOBJ_ADD);
530
Martin K. Petersencd43e262009-05-22 17:17:52 -0400531 if (!q->request_fn)
532 return 0;
533
Jens Axboe8324aa92008-01-29 14:51:59 +0100534 ret = elv_register_queue(q);
535 if (ret) {
536 kobject_uevent(&q->kobj, KOBJ_REMOVE);
537 kobject_del(&q->kobj);
Liu Yuan80656b62011-04-13 22:14:54 +0200538 blk_trace_remove_sysfs(dev);
Xiaotian Fengc87ffbb2010-08-23 12:30:29 +0200539 kobject_put(&dev->kobj);
Jens Axboe8324aa92008-01-29 14:51:59 +0100540 return ret;
541 }
542
543 return 0;
544}
545
546void blk_unregister_queue(struct gendisk *disk)
547{
548 struct request_queue *q = disk->queue;
549
Akinobu Mitafb199742008-04-21 09:51:06 +0200550 if (WARN_ON(!q))
551 return;
552
Zdenek Kabelac48c0d4d2009-09-25 06:19:26 +0200553 if (q->request_fn)
Jens Axboe8324aa92008-01-29 14:51:59 +0100554 elv_unregister_queue(q);
555
Zdenek Kabelac48c0d4d2009-09-25 06:19:26 +0200556 kobject_uevent(&q->kobj, KOBJ_REMOVE);
557 kobject_del(&q->kobj);
558 blk_trace_remove_sysfs(disk_to_dev(disk));
559 kobject_put(&disk_to_dev(disk)->kobj);
Jens Axboe8324aa92008-01-29 14:51:59 +0100560}