blk-sysfs.c 16 KB

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