blk-sysfs.c 16 KB

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