Back to home page

LXR

 
 

    


0001 /*
0002  * Functions related to sysfs handling
0003  */
0004 #include <linux/kernel.h>
0005 #include <linux/slab.h>
0006 #include <linux/module.h>
0007 #include <linux/bio.h>
0008 #include <linux/blkdev.h>
0009 #include <linux/backing-dev.h>
0010 #include <linux/blktrace_api.h>
0011 #include <linux/blk-mq.h>
0012 #include <linux/blk-cgroup.h>
0013 
0014 #include "blk.h"
0015 #include "blk-mq.h"
0016 #include "blk-wbt.h"
0017 
0018 struct queue_sysfs_entry {
0019     struct attribute attr;
0020     ssize_t (*show)(struct request_queue *, char *);
0021     ssize_t (*store)(struct request_queue *, const char *, size_t);
0022 };
0023 
0024 static ssize_t
0025 queue_var_show(unsigned long var, char *page)
0026 {
0027     return sprintf(page, "%lu\n", var);
0028 }
0029 
0030 static ssize_t
0031 queue_var_store(unsigned long *var, const char *page, size_t count)
0032 {
0033     int err;
0034     unsigned long v;
0035 
0036     err = kstrtoul(page, 10, &v);
0037     if (err || v > UINT_MAX)
0038         return -EINVAL;
0039 
0040     *var = v;
0041 
0042     return count;
0043 }
0044 
0045 static ssize_t queue_var_store64(s64 *var, const char *page)
0046 {
0047     int err;
0048     s64 v;
0049 
0050     err = kstrtos64(page, 10, &v);
0051     if (err < 0)
0052         return err;
0053 
0054     *var = v;
0055     return 0;
0056 }
0057 
0058 static ssize_t queue_requests_show(struct request_queue *q, char *page)
0059 {
0060     return queue_var_show(q->nr_requests, (page));
0061 }
0062 
0063 static ssize_t
0064 queue_requests_store(struct request_queue *q, const char *page, size_t count)
0065 {
0066     unsigned long nr;
0067     int ret, err;
0068 
0069     if (!q->request_fn && !q->mq_ops)
0070         return -EINVAL;
0071 
0072     ret = queue_var_store(&nr, page, count);
0073     if (ret < 0)
0074         return ret;
0075 
0076     if (nr < BLKDEV_MIN_RQ)
0077         nr = BLKDEV_MIN_RQ;
0078 
0079     if (q->request_fn)
0080         err = blk_update_nr_requests(q, nr);
0081     else
0082         err = blk_mq_update_nr_requests(q, nr);
0083 
0084     if (err)
0085         return err;
0086 
0087     return ret;
0088 }
0089 
0090 static ssize_t queue_ra_show(struct request_queue *q, char *page)
0091 {
0092     unsigned long ra_kb = q->backing_dev_info.ra_pages <<
0093                     (PAGE_SHIFT - 10);
0094 
0095     return queue_var_show(ra_kb, (page));
0096 }
0097 
0098 static ssize_t
0099 queue_ra_store(struct request_queue *q, const char *page, size_t count)
0100 {
0101     unsigned long ra_kb;
0102     ssize_t ret = queue_var_store(&ra_kb, page, count);
0103 
0104     if (ret < 0)
0105         return ret;
0106 
0107     q->backing_dev_info.ra_pages = ra_kb >> (PAGE_SHIFT - 10);
0108 
0109     return ret;
0110 }
0111 
0112 static ssize_t queue_max_sectors_show(struct request_queue *q, char *page)
0113 {
0114     int max_sectors_kb = queue_max_sectors(q) >> 1;
0115 
0116     return queue_var_show(max_sectors_kb, (page));
0117 }
0118 
0119 static ssize_t queue_max_segments_show(struct request_queue *q, char *page)
0120 {
0121     return queue_var_show(queue_max_segments(q), (page));
0122 }
0123 
0124 static ssize_t queue_max_integrity_segments_show(struct request_queue *q, char *page)
0125 {
0126     return queue_var_show(q->limits.max_integrity_segments, (page));
0127 }
0128 
0129 static ssize_t queue_max_segment_size_show(struct request_queue *q, char *page)
0130 {
0131     if (blk_queue_cluster(q))
0132         return queue_var_show(queue_max_segment_size(q), (page));
0133 
0134     return queue_var_show(PAGE_SIZE, (page));
0135 }
0136 
0137 static ssize_t queue_logical_block_size_show(struct request_queue *q, char *page)
0138 {
0139     return queue_var_show(queue_logical_block_size(q), page);
0140 }
0141 
0142 static ssize_t queue_physical_block_size_show(struct request_queue *q, char *page)
0143 {
0144     return queue_var_show(queue_physical_block_size(q), page);
0145 }
0146 
0147 static ssize_t queue_chunk_sectors_show(struct request_queue *q, char *page)
0148 {
0149     return queue_var_show(q->limits.chunk_sectors, page);
0150 }
0151 
0152 static ssize_t queue_io_min_show(struct request_queue *q, char *page)
0153 {
0154     return queue_var_show(queue_io_min(q), page);
0155 }
0156 
0157 static ssize_t queue_io_opt_show(struct request_queue *q, char *page)
0158 {
0159     return queue_var_show(queue_io_opt(q), page);
0160 }
0161 
0162 static ssize_t queue_discard_granularity_show(struct request_queue *q, char *page)
0163 {
0164     return queue_var_show(q->limits.discard_granularity, page);
0165 }
0166 
0167 static ssize_t queue_discard_max_hw_show(struct request_queue *q, char *page)
0168 {
0169 
0170     return sprintf(page, "%llu\n",
0171         (unsigned long long)q->limits.max_hw_discard_sectors << 9);
0172 }
0173 
0174 static ssize_t queue_discard_max_show(struct request_queue *q, char *page)
0175 {
0176     return sprintf(page, "%llu\n",
0177                (unsigned long long)q->limits.max_discard_sectors << 9);
0178 }
0179 
0180 static ssize_t queue_discard_max_store(struct request_queue *q,
0181                        const char *page, size_t count)
0182 {
0183     unsigned long max_discard;
0184     ssize_t ret = queue_var_store(&max_discard, page, count);
0185 
0186     if (ret < 0)
0187         return ret;
0188 
0189     if (max_discard & (q->limits.discard_granularity - 1))
0190         return -EINVAL;
0191 
0192     max_discard >>= 9;
0193     if (max_discard > UINT_MAX)
0194         return -EINVAL;
0195 
0196     if (max_discard > q->limits.max_hw_discard_sectors)
0197         max_discard = q->limits.max_hw_discard_sectors;
0198 
0199     q->limits.max_discard_sectors = max_discard;
0200     return ret;
0201 }
0202 
0203 static ssize_t queue_discard_zeroes_data_show(struct request_queue *q, char *page)
0204 {
0205     return queue_var_show(queue_discard_zeroes_data(q), page);
0206 }
0207 
0208 static ssize_t queue_write_same_max_show(struct request_queue *q, char *page)
0209 {
0210     return sprintf(page, "%llu\n",
0211         (unsigned long long)q->limits.max_write_same_sectors << 9);
0212 }
0213 
0214 static ssize_t queue_write_zeroes_max_show(struct request_queue *q, char *page)
0215 {
0216     return sprintf(page, "%llu\n",
0217         (unsigned long long)q->limits.max_write_zeroes_sectors << 9);
0218 }
0219 
0220 static ssize_t
0221 queue_max_sectors_store(struct request_queue *q, const char *page, size_t count)
0222 {
0223     unsigned long max_sectors_kb,
0224         max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1,
0225             page_kb = 1 << (PAGE_SHIFT - 10);
0226     ssize_t ret = queue_var_store(&max_sectors_kb, page, count);
0227 
0228     if (ret < 0)
0229         return ret;
0230 
0231     max_hw_sectors_kb = min_not_zero(max_hw_sectors_kb, (unsigned long)
0232                      q->limits.max_dev_sectors >> 1);
0233 
0234     if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb)
0235         return -EINVAL;
0236 
0237     spin_lock_irq(q->queue_lock);
0238     q->limits.max_sectors = max_sectors_kb << 1;
0239     q->backing_dev_info.io_pages = max_sectors_kb >> (PAGE_SHIFT - 10);
0240     spin_unlock_irq(q->queue_lock);
0241 
0242     return ret;
0243 }
0244 
0245 static ssize_t queue_max_hw_sectors_show(struct request_queue *q, char *page)
0246 {
0247     int max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1;
0248 
0249     return queue_var_show(max_hw_sectors_kb, (page));
0250 }
0251 
0252 #define QUEUE_SYSFS_BIT_FNS(name, flag, neg)                \
0253 static ssize_t                              \
0254 queue_show_##name(struct request_queue *q, char *page)          \
0255 {                                   \
0256     int bit;                            \
0257     bit = test_bit(QUEUE_FLAG_##flag, &q->queue_flags);     \
0258     return queue_var_show(neg ? !bit : bit, page);          \
0259 }                                   \
0260 static ssize_t                              \
0261 queue_store_##name(struct request_queue *q, const char *page, size_t count) \
0262 {                                   \
0263     unsigned long val;                      \
0264     ssize_t ret;                            \
0265     ret = queue_var_store(&val, page, count);           \
0266     if (ret < 0)                            \
0267          return ret;                        \
0268     if (neg)                            \
0269         val = !val;                     \
0270                                     \
0271     spin_lock_irq(q->queue_lock);                   \
0272     if (val)                            \
0273         queue_flag_set(QUEUE_FLAG_##flag, q);           \
0274     else                                \
0275         queue_flag_clear(QUEUE_FLAG_##flag, q);         \
0276     spin_unlock_irq(q->queue_lock);                 \
0277     return ret;                         \
0278 }
0279 
0280 QUEUE_SYSFS_BIT_FNS(nonrot, NONROT, 1);
0281 QUEUE_SYSFS_BIT_FNS(random, ADD_RANDOM, 0);
0282 QUEUE_SYSFS_BIT_FNS(iostats, IO_STAT, 0);
0283 #undef QUEUE_SYSFS_BIT_FNS
0284 
0285 static ssize_t queue_zoned_show(struct request_queue *q, char *page)
0286 {
0287     switch (blk_queue_zoned_model(q)) {
0288     case BLK_ZONED_HA:
0289         return sprintf(page, "host-aware\n");
0290     case BLK_ZONED_HM:
0291         return sprintf(page, "host-managed\n");
0292     default:
0293         return sprintf(page, "none\n");
0294     }
0295 }
0296 
0297 static ssize_t queue_nomerges_show(struct request_queue *q, char *page)
0298 {
0299     return queue_var_show((blk_queue_nomerges(q) << 1) |
0300                    blk_queue_noxmerges(q), page);
0301 }
0302 
0303 static ssize_t queue_nomerges_store(struct request_queue *q, const char *page,
0304                     size_t count)
0305 {
0306     unsigned long nm;
0307     ssize_t ret = queue_var_store(&nm, page, count);
0308 
0309     if (ret < 0)
0310         return ret;
0311 
0312     spin_lock_irq(q->queue_lock);
0313     queue_flag_clear(QUEUE_FLAG_NOMERGES, q);
0314     queue_flag_clear(QUEUE_FLAG_NOXMERGES, q);
0315     if (nm == 2)
0316         queue_flag_set(QUEUE_FLAG_NOMERGES, q);
0317     else if (nm)
0318         queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
0319     spin_unlock_irq(q->queue_lock);
0320 
0321     return ret;
0322 }
0323 
0324 static ssize_t queue_rq_affinity_show(struct request_queue *q, char *page)
0325 {
0326     bool set = test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags);
0327     bool force = test_bit(QUEUE_FLAG_SAME_FORCE, &q->queue_flags);
0328 
0329     return queue_var_show(set << force, page);
0330 }
0331 
0332 static ssize_t
0333 queue_rq_affinity_store(struct request_queue *q, const char *page, size_t count)
0334 {
0335     ssize_t ret = -EINVAL;
0336 #ifdef CONFIG_SMP
0337     unsigned long val;
0338 
0339     ret = queue_var_store(&val, page, count);
0340     if (ret < 0)
0341         return ret;
0342 
0343     spin_lock_irq(q->queue_lock);
0344     if (val == 2) {
0345         queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
0346         queue_flag_set(QUEUE_FLAG_SAME_FORCE, q);
0347     } else if (val == 1) {
0348         queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
0349         queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
0350     } else if (val == 0) {
0351         queue_flag_clear(QUEUE_FLAG_SAME_COMP, q);
0352         queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
0353     }
0354     spin_unlock_irq(q->queue_lock);
0355 #endif
0356     return ret;
0357 }
0358 
0359 static ssize_t queue_poll_delay_show(struct request_queue *q, char *page)
0360 {
0361     int val;
0362 
0363     if (q->poll_nsec == -1)
0364         val = -1;
0365     else
0366         val = q->poll_nsec / 1000;
0367 
0368     return sprintf(page, "%d\n", val);
0369 }
0370 
0371 static ssize_t queue_poll_delay_store(struct request_queue *q, const char *page,
0372                 size_t count)
0373 {
0374     int err, val;
0375 
0376     if (!q->mq_ops || !q->mq_ops->poll)
0377         return -EINVAL;
0378 
0379     err = kstrtoint(page, 10, &val);
0380     if (err < 0)
0381         return err;
0382 
0383     if (val == -1)
0384         q->poll_nsec = -1;
0385     else
0386         q->poll_nsec = val * 1000;
0387 
0388     return count;
0389 }
0390 
0391 static ssize_t queue_poll_show(struct request_queue *q, char *page)
0392 {
0393     return queue_var_show(test_bit(QUEUE_FLAG_POLL, &q->queue_flags), page);
0394 }
0395 
0396 static ssize_t queue_poll_store(struct request_queue *q, const char *page,
0397                 size_t count)
0398 {
0399     unsigned long poll_on;
0400     ssize_t ret;
0401 
0402     if (!q->mq_ops || !q->mq_ops->poll)
0403         return -EINVAL;
0404 
0405     ret = queue_var_store(&poll_on, page, count);
0406     if (ret < 0)
0407         return ret;
0408 
0409     spin_lock_irq(q->queue_lock);
0410     if (poll_on)
0411         queue_flag_set(QUEUE_FLAG_POLL, q);
0412     else
0413         queue_flag_clear(QUEUE_FLAG_POLL, q);
0414     spin_unlock_irq(q->queue_lock);
0415 
0416     return ret;
0417 }
0418 
0419 static ssize_t queue_wb_lat_show(struct request_queue *q, char *page)
0420 {
0421     if (!q->rq_wb)
0422         return -EINVAL;
0423 
0424     return sprintf(page, "%llu\n", div_u64(q->rq_wb->min_lat_nsec, 1000));
0425 }
0426 
0427 static ssize_t queue_wb_lat_store(struct request_queue *q, const char *page,
0428                   size_t count)
0429 {
0430     struct rq_wb *rwb;
0431     ssize_t ret;
0432     s64 val;
0433 
0434     ret = queue_var_store64(&val, page);
0435     if (ret < 0)
0436         return ret;
0437     if (val < -1)
0438         return -EINVAL;
0439 
0440     rwb = q->rq_wb;
0441     if (!rwb) {
0442         ret = wbt_init(q);
0443         if (ret)
0444             return ret;
0445 
0446         rwb = q->rq_wb;
0447         if (!rwb)
0448             return -EINVAL;
0449     }
0450 
0451     if (val == -1)
0452         rwb->min_lat_nsec = wbt_default_latency_nsec(q);
0453     else if (val >= 0)
0454         rwb->min_lat_nsec = val * 1000ULL;
0455 
0456     if (rwb->enable_state == WBT_STATE_ON_DEFAULT)
0457         rwb->enable_state = WBT_STATE_ON_MANUAL;
0458 
0459     wbt_update_limits(rwb);
0460     return count;
0461 }
0462 
0463 static ssize_t queue_wc_show(struct request_queue *q, char *page)
0464 {
0465     if (test_bit(QUEUE_FLAG_WC, &q->queue_flags))
0466         return sprintf(page, "write back\n");
0467 
0468     return sprintf(page, "write through\n");
0469 }
0470 
0471 static ssize_t queue_wc_store(struct request_queue *q, const char *page,
0472                   size_t count)
0473 {
0474     int set = -1;
0475 
0476     if (!strncmp(page, "write back", 10))
0477         set = 1;
0478     else if (!strncmp(page, "write through", 13) ||
0479          !strncmp(page, "none", 4))
0480         set = 0;
0481 
0482     if (set == -1)
0483         return -EINVAL;
0484 
0485     spin_lock_irq(q->queue_lock);
0486     if (set)
0487         queue_flag_set(QUEUE_FLAG_WC, q);
0488     else
0489         queue_flag_clear(QUEUE_FLAG_WC, q);
0490     spin_unlock_irq(q->queue_lock);
0491 
0492     return count;
0493 }
0494 
0495 static ssize_t queue_dax_show(struct request_queue *q, char *page)
0496 {
0497     return queue_var_show(blk_queue_dax(q), page);
0498 }
0499 
0500 static ssize_t print_stat(char *page, struct blk_rq_stat *stat, const char *pre)
0501 {
0502     return sprintf(page, "%s samples=%llu, mean=%lld, min=%lld, max=%lld\n",
0503             pre, (long long) stat->nr_samples,
0504             (long long) stat->mean, (long long) stat->min,
0505             (long long) stat->max);
0506 }
0507 
0508 static ssize_t queue_stats_show(struct request_queue *q, char *page)
0509 {
0510     struct blk_rq_stat stat[2];
0511     ssize_t ret;
0512 
0513     blk_queue_stat_get(q, stat);
0514 
0515     ret = print_stat(page, &stat[BLK_STAT_READ], "read :");
0516     ret += print_stat(page + ret, &stat[BLK_STAT_WRITE], "write:");
0517     return ret;
0518 }
0519 
0520 static struct queue_sysfs_entry queue_requests_entry = {
0521     .attr = {.name = "nr_requests", .mode = S_IRUGO | S_IWUSR },
0522     .show = queue_requests_show,
0523     .store = queue_requests_store,
0524 };
0525 
0526 static struct queue_sysfs_entry queue_ra_entry = {
0527     .attr = {.name = "read_ahead_kb", .mode = S_IRUGO | S_IWUSR },
0528     .show = queue_ra_show,
0529     .store = queue_ra_store,
0530 };
0531 
0532 static struct queue_sysfs_entry queue_max_sectors_entry = {
0533     .attr = {.name = "max_sectors_kb", .mode = S_IRUGO | S_IWUSR },
0534     .show = queue_max_sectors_show,
0535     .store = queue_max_sectors_store,
0536 };
0537 
0538 static struct queue_sysfs_entry queue_max_hw_sectors_entry = {
0539     .attr = {.name = "max_hw_sectors_kb", .mode = S_IRUGO },
0540     .show = queue_max_hw_sectors_show,
0541 };
0542 
0543 static struct queue_sysfs_entry queue_max_segments_entry = {
0544     .attr = {.name = "max_segments", .mode = S_IRUGO },
0545     .show = queue_max_segments_show,
0546 };
0547 
0548 static struct queue_sysfs_entry queue_max_integrity_segments_entry = {
0549     .attr = {.name = "max_integrity_segments", .mode = S_IRUGO },
0550     .show = queue_max_integrity_segments_show,
0551 };
0552 
0553 static struct queue_sysfs_entry queue_max_segment_size_entry = {
0554     .attr = {.name = "max_segment_size", .mode = S_IRUGO },
0555     .show = queue_max_segment_size_show,
0556 };
0557 
0558 static struct queue_sysfs_entry queue_iosched_entry = {
0559     .attr = {.name = "scheduler", .mode = S_IRUGO | S_IWUSR },
0560     .show = elv_iosched_show,
0561     .store = elv_iosched_store,
0562 };
0563 
0564 static struct queue_sysfs_entry queue_hw_sector_size_entry = {
0565     .attr = {.name = "hw_sector_size", .mode = S_IRUGO },
0566     .show = queue_logical_block_size_show,
0567 };
0568 
0569 static struct queue_sysfs_entry queue_logical_block_size_entry = {
0570     .attr = {.name = "logical_block_size", .mode = S_IRUGO },
0571     .show = queue_logical_block_size_show,
0572 };
0573 
0574 static struct queue_sysfs_entry queue_physical_block_size_entry = {
0575     .attr = {.name = "physical_block_size", .mode = S_IRUGO },
0576     .show = queue_physical_block_size_show,
0577 };
0578 
0579 static struct queue_sysfs_entry queue_chunk_sectors_entry = {
0580     .attr = {.name = "chunk_sectors", .mode = S_IRUGO },
0581     .show = queue_chunk_sectors_show,
0582 };
0583 
0584 static struct queue_sysfs_entry queue_io_min_entry = {
0585     .attr = {.name = "minimum_io_size", .mode = S_IRUGO },
0586     .show = queue_io_min_show,
0587 };
0588 
0589 static struct queue_sysfs_entry queue_io_opt_entry = {
0590     .attr = {.name = "optimal_io_size", .mode = S_IRUGO },
0591     .show = queue_io_opt_show,
0592 };
0593 
0594 static struct queue_sysfs_entry queue_discard_granularity_entry = {
0595     .attr = {.name = "discard_granularity", .mode = S_IRUGO },
0596     .show = queue_discard_granularity_show,
0597 };
0598 
0599 static struct queue_sysfs_entry queue_discard_max_hw_entry = {
0600     .attr = {.name = "discard_max_hw_bytes", .mode = S_IRUGO },
0601     .show = queue_discard_max_hw_show,
0602 };
0603 
0604 static struct queue_sysfs_entry queue_discard_max_entry = {
0605     .attr = {.name = "discard_max_bytes", .mode = S_IRUGO | S_IWUSR },
0606     .show = queue_discard_max_show,
0607     .store = queue_discard_max_store,
0608 };
0609 
0610 static struct queue_sysfs_entry queue_discard_zeroes_data_entry = {
0611     .attr = {.name = "discard_zeroes_data", .mode = S_IRUGO },
0612     .show = queue_discard_zeroes_data_show,
0613 };
0614 
0615 static struct queue_sysfs_entry queue_write_same_max_entry = {
0616     .attr = {.name = "write_same_max_bytes", .mode = S_IRUGO },
0617     .show = queue_write_same_max_show,
0618 };
0619 
0620 static struct queue_sysfs_entry queue_write_zeroes_max_entry = {
0621     .attr = {.name = "write_zeroes_max_bytes", .mode = S_IRUGO },
0622     .show = queue_write_zeroes_max_show,
0623 };
0624 
0625 static struct queue_sysfs_entry queue_nonrot_entry = {
0626     .attr = {.name = "rotational", .mode = S_IRUGO | S_IWUSR },
0627     .show = queue_show_nonrot,
0628     .store = queue_store_nonrot,
0629 };
0630 
0631 static struct queue_sysfs_entry queue_zoned_entry = {
0632     .attr = {.name = "zoned", .mode = S_IRUGO },
0633     .show = queue_zoned_show,
0634 };
0635 
0636 static struct queue_sysfs_entry queue_nomerges_entry = {
0637     .attr = {.name = "nomerges", .mode = S_IRUGO | S_IWUSR },
0638     .show = queue_nomerges_show,
0639     .store = queue_nomerges_store,
0640 };
0641 
0642 static struct queue_sysfs_entry queue_rq_affinity_entry = {
0643     .attr = {.name = "rq_affinity", .mode = S_IRUGO | S_IWUSR },
0644     .show = queue_rq_affinity_show,
0645     .store = queue_rq_affinity_store,
0646 };
0647 
0648 static struct queue_sysfs_entry queue_iostats_entry = {
0649     .attr = {.name = "iostats", .mode = S_IRUGO | S_IWUSR },
0650     .show = queue_show_iostats,
0651     .store = queue_store_iostats,
0652 };
0653 
0654 static struct queue_sysfs_entry queue_random_entry = {
0655     .attr = {.name = "add_random", .mode = S_IRUGO | S_IWUSR },
0656     .show = queue_show_random,
0657     .store = queue_store_random,
0658 };
0659 
0660 static struct queue_sysfs_entry queue_poll_entry = {
0661     .attr = {.name = "io_poll", .mode = S_IRUGO | S_IWUSR },
0662     .show = queue_poll_show,
0663     .store = queue_poll_store,
0664 };
0665 
0666 static struct queue_sysfs_entry queue_poll_delay_entry = {
0667     .attr = {.name = "io_poll_delay", .mode = S_IRUGO | S_IWUSR },
0668     .show = queue_poll_delay_show,
0669     .store = queue_poll_delay_store,
0670 };
0671 
0672 static struct queue_sysfs_entry queue_wc_entry = {
0673     .attr = {.name = "write_cache", .mode = S_IRUGO | S_IWUSR },
0674     .show = queue_wc_show,
0675     .store = queue_wc_store,
0676 };
0677 
0678 static struct queue_sysfs_entry queue_dax_entry = {
0679     .attr = {.name = "dax", .mode = S_IRUGO },
0680     .show = queue_dax_show,
0681 };
0682 
0683 static struct queue_sysfs_entry queue_stats_entry = {
0684     .attr = {.name = "stats", .mode = S_IRUGO },
0685     .show = queue_stats_show,
0686 };
0687 
0688 static struct queue_sysfs_entry queue_wb_lat_entry = {
0689     .attr = {.name = "wbt_lat_usec", .mode = S_IRUGO | S_IWUSR },
0690     .show = queue_wb_lat_show,
0691     .store = queue_wb_lat_store,
0692 };
0693 
0694 static struct attribute *default_attrs[] = {
0695     &queue_requests_entry.attr,
0696     &queue_ra_entry.attr,
0697     &queue_max_hw_sectors_entry.attr,
0698     &queue_max_sectors_entry.attr,
0699     &queue_max_segments_entry.attr,
0700     &queue_max_integrity_segments_entry.attr,
0701     &queue_max_segment_size_entry.attr,
0702     &queue_iosched_entry.attr,
0703     &queue_hw_sector_size_entry.attr,
0704     &queue_logical_block_size_entry.attr,
0705     &queue_physical_block_size_entry.attr,
0706     &queue_chunk_sectors_entry.attr,
0707     &queue_io_min_entry.attr,
0708     &queue_io_opt_entry.attr,
0709     &queue_discard_granularity_entry.attr,
0710     &queue_discard_max_entry.attr,
0711     &queue_discard_max_hw_entry.attr,
0712     &queue_discard_zeroes_data_entry.attr,
0713     &queue_write_same_max_entry.attr,
0714     &queue_write_zeroes_max_entry.attr,
0715     &queue_nonrot_entry.attr,
0716     &queue_zoned_entry.attr,
0717     &queue_nomerges_entry.attr,
0718     &queue_rq_affinity_entry.attr,
0719     &queue_iostats_entry.attr,
0720     &queue_random_entry.attr,
0721     &queue_poll_entry.attr,
0722     &queue_wc_entry.attr,
0723     &queue_dax_entry.attr,
0724     &queue_stats_entry.attr,
0725     &queue_wb_lat_entry.attr,
0726     &queue_poll_delay_entry.attr,
0727     NULL,
0728 };
0729 
0730 #define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
0731 
0732 static ssize_t
0733 queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
0734 {
0735     struct queue_sysfs_entry *entry = to_queue(attr);
0736     struct request_queue *q =
0737         container_of(kobj, struct request_queue, kobj);
0738     ssize_t res;
0739 
0740     if (!entry->show)
0741         return -EIO;
0742     mutex_lock(&q->sysfs_lock);
0743     if (blk_queue_dying(q)) {
0744         mutex_unlock(&q->sysfs_lock);
0745         return -ENOENT;
0746     }
0747     res = entry->show(q, page);
0748     mutex_unlock(&q->sysfs_lock);
0749     return res;
0750 }
0751 
0752 static ssize_t
0753 queue_attr_store(struct kobject *kobj, struct attribute *attr,
0754             const char *page, size_t length)
0755 {
0756     struct queue_sysfs_entry *entry = to_queue(attr);
0757     struct request_queue *q;
0758     ssize_t res;
0759 
0760     if (!entry->store)
0761         return -EIO;
0762 
0763     q = container_of(kobj, struct request_queue, kobj);
0764     mutex_lock(&q->sysfs_lock);
0765     if (blk_queue_dying(q)) {
0766         mutex_unlock(&q->sysfs_lock);
0767         return -ENOENT;
0768     }
0769     res = entry->store(q, page, length);
0770     mutex_unlock(&q->sysfs_lock);
0771     return res;
0772 }
0773 
0774 static void blk_free_queue_rcu(struct rcu_head *rcu_head)
0775 {
0776     struct request_queue *q = container_of(rcu_head, struct request_queue,
0777                            rcu_head);
0778     kmem_cache_free(blk_requestq_cachep, q);
0779 }
0780 
0781 /**
0782  * blk_release_queue: - release a &struct request_queue when it is no longer needed
0783  * @kobj:    the kobj belonging to the request queue to be released
0784  *
0785  * Description:
0786  *     blk_release_queue is the pair to blk_init_queue() or
0787  *     blk_queue_make_request().  It should be called when a request queue is
0788  *     being released; typically when a block device is being de-registered.
0789  *     Currently, its primary task it to free all the &struct request
0790  *     structures that were allocated to the queue and the queue itself.
0791  *
0792  * Note:
0793  *     The low level driver must have finished any outstanding requests first
0794  *     via blk_cleanup_queue().
0795  **/
0796 static void blk_release_queue(struct kobject *kobj)
0797 {
0798     struct request_queue *q =
0799         container_of(kobj, struct request_queue, kobj);
0800 
0801     wbt_exit(q);
0802     bdi_exit(&q->backing_dev_info);
0803     blkcg_exit_queue(q);
0804 
0805     if (q->elevator) {
0806         spin_lock_irq(q->queue_lock);
0807         ioc_clear_queue(q);
0808         spin_unlock_irq(q->queue_lock);
0809         elevator_exit(q->elevator);
0810     }
0811 
0812     blk_exit_rl(&q->root_rl);
0813 
0814     if (q->queue_tags)
0815         __blk_queue_free_tags(q);
0816 
0817     if (!q->mq_ops)
0818         blk_free_flush_queue(q->fq);
0819     else
0820         blk_mq_release(q);
0821 
0822     blk_trace_shutdown(q);
0823 
0824     if (q->bio_split)
0825         bioset_free(q->bio_split);
0826 
0827     ida_simple_remove(&blk_queue_ida, q->id);
0828     call_rcu(&q->rcu_head, blk_free_queue_rcu);
0829 }
0830 
0831 static const struct sysfs_ops queue_sysfs_ops = {
0832     .show   = queue_attr_show,
0833     .store  = queue_attr_store,
0834 };
0835 
0836 struct kobj_type blk_queue_ktype = {
0837     .sysfs_ops  = &queue_sysfs_ops,
0838     .default_attrs  = default_attrs,
0839     .release    = blk_release_queue,
0840 };
0841 
0842 static void blk_wb_init(struct request_queue *q)
0843 {
0844 #ifndef CONFIG_BLK_WBT_MQ
0845     if (q->mq_ops)
0846         return;
0847 #endif
0848 #ifndef CONFIG_BLK_WBT_SQ
0849     if (q->request_fn)
0850         return;
0851 #endif
0852 
0853     /*
0854      * If this fails, we don't get throttling
0855      */
0856     wbt_init(q);
0857 }
0858 
0859 int blk_register_queue(struct gendisk *disk)
0860 {
0861     int ret;
0862     struct device *dev = disk_to_dev(disk);
0863     struct request_queue *q = disk->queue;
0864 
0865     if (WARN_ON(!q))
0866         return -ENXIO;
0867 
0868     /*
0869      * SCSI probing may synchronously create and destroy a lot of
0870      * request_queues for non-existent devices.  Shutting down a fully
0871      * functional queue takes measureable wallclock time as RCU grace
0872      * periods are involved.  To avoid excessive latency in these
0873      * cases, a request_queue starts out in a degraded mode which is
0874      * faster to shut down and is made fully functional here as
0875      * request_queues for non-existent devices never get registered.
0876      */
0877     if (!blk_queue_init_done(q)) {
0878         queue_flag_set_unlocked(QUEUE_FLAG_INIT_DONE, q);
0879         percpu_ref_switch_to_percpu(&q->q_usage_counter);
0880         blk_queue_bypass_end(q);
0881     }
0882 
0883     ret = blk_trace_init_sysfs(dev);
0884     if (ret)
0885         return ret;
0886 
0887     ret = kobject_add(&q->kobj, kobject_get(&dev->kobj), "%s", "queue");
0888     if (ret < 0) {
0889         blk_trace_remove_sysfs(dev);
0890         return ret;
0891     }
0892 
0893     kobject_uevent(&q->kobj, KOBJ_ADD);
0894 
0895     if (q->mq_ops)
0896         blk_mq_register_dev(dev, q);
0897 
0898     blk_wb_init(q);
0899 
0900     if (!q->request_fn)
0901         return 0;
0902 
0903     ret = elv_register_queue(q);
0904     if (ret) {
0905         kobject_uevent(&q->kobj, KOBJ_REMOVE);
0906         kobject_del(&q->kobj);
0907         blk_trace_remove_sysfs(dev);
0908         kobject_put(&dev->kobj);
0909         return ret;
0910     }
0911 
0912     return 0;
0913 }
0914 
0915 void blk_unregister_queue(struct gendisk *disk)
0916 {
0917     struct request_queue *q = disk->queue;
0918 
0919     if (WARN_ON(!q))
0920         return;
0921 
0922     if (q->mq_ops)
0923         blk_mq_unregister_dev(disk_to_dev(disk), q);
0924 
0925     if (q->request_fn)
0926         elv_unregister_queue(q);
0927 
0928     kobject_uevent(&q->kobj, KOBJ_REMOVE);
0929     kobject_del(&q->kobj);
0930     blk_trace_remove_sysfs(disk_to_dev(disk));
0931     kobject_put(&disk_to_dev(disk)->kobj);
0932 }