blk-sysfs.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Functions related to sysfs handling
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/slab.h>
  7. #include <linux/module.h>
  8. #include <linux/bio.h>
  9. #include <linux/blkdev.h>
  10. #include <linux/backing-dev.h>
  11. #include <linux/blktrace_api.h>
  12. #include <linux/blk-mq.h>
  13. #include <linux/blk-cgroup.h>
  14. #include "blk.h"
  15. #include "blk-mq.h"
  16. #include "blk-mq-debugfs.h"
  17. #include "blk-wbt.h"
  18. struct queue_sysfs_entry {
  19. struct attribute attr;
  20. ssize_t (*show)(struct request_queue *, char *);
  21. ssize_t (*store)(struct request_queue *, const char *, size_t);
  22. };
  23. static ssize_t
  24. queue_var_show(unsigned long var, char *page)
  25. {
  26. return sprintf(page, "%lu\n", var);
  27. }
  28. static ssize_t
  29. queue_var_store(unsigned long *var, const char *page, size_t count)
  30. {
  31. int err;
  32. unsigned long v;
  33. err = kstrtoul(page, 10, &v);
  34. if (err || v > UINT_MAX)
  35. return -EINVAL;
  36. *var = v;
  37. return count;
  38. }
  39. static ssize_t queue_var_store64(s64 *var, const char *page)
  40. {
  41. int err;
  42. s64 v;
  43. err = kstrtos64(page, 10, &v);
  44. if (err < 0)
  45. return err;
  46. *var = v;
  47. return 0;
  48. }
  49. static ssize_t queue_requests_show(struct request_queue *q, char *page)
  50. {
  51. return queue_var_show(q->nr_requests, (page));
  52. }
  53. static ssize_t
  54. queue_requests_store(struct request_queue *q, const char *page, size_t count)
  55. {
  56. unsigned long nr;
  57. int ret, err;
  58. if (!q->request_fn && !q->mq_ops)
  59. return -EINVAL;
  60. ret = queue_var_store(&nr, page, count);
  61. if (ret < 0)
  62. return ret;
  63. if (nr < BLKDEV_MIN_RQ)
  64. nr = BLKDEV_MIN_RQ;
  65. if (q->request_fn)
  66. err = blk_update_nr_requests(q, nr);
  67. else
  68. err = blk_mq_update_nr_requests(q, nr);
  69. if (err)
  70. return err;
  71. return ret;
  72. }
  73. static ssize_t queue_ra_show(struct request_queue *q, char *page)
  74. {
  75. unsigned long ra_kb = q->backing_dev_info->ra_pages <<
  76. (PAGE_SHIFT - 10);
  77. return queue_var_show(ra_kb, (page));
  78. }
  79. static ssize_t
  80. queue_ra_store(struct request_queue *q, const char *page, size_t count)
  81. {
  82. unsigned long ra_kb;
  83. ssize_t ret = queue_var_store(&ra_kb, page, count);
  84. if (ret < 0)
  85. return ret;
  86. q->backing_dev_info->ra_pages = ra_kb >> (PAGE_SHIFT - 10);
  87. return ret;
  88. }
  89. static ssize_t queue_max_sectors_show(struct request_queue *q, char *page)
  90. {
  91. int max_sectors_kb = queue_max_sectors(q) >> 1;
  92. return queue_var_show(max_sectors_kb, (page));
  93. }
  94. static ssize_t queue_max_segments_show(struct request_queue *q, char *page)
  95. {
  96. return queue_var_show(queue_max_segments(q), (page));
  97. }
  98. static ssize_t queue_max_discard_segments_show(struct request_queue *q,
  99. char *page)
  100. {
  101. return queue_var_show(queue_max_discard_segments(q), (page));
  102. }
  103. static ssize_t queue_max_integrity_segments_show(struct request_queue *q, char *page)
  104. {
  105. return queue_var_show(q->limits.max_integrity_segments, (page));
  106. }
  107. static ssize_t queue_max_segment_size_show(struct request_queue *q, char *page)
  108. {
  109. if (blk_queue_cluster(q))
  110. return queue_var_show(queue_max_segment_size(q), (page));
  111. return queue_var_show(PAGE_SIZE, (page));
  112. }
  113. static ssize_t queue_logical_block_size_show(struct request_queue *q, char *page)
  114. {
  115. return queue_var_show(queue_logical_block_size(q), page);
  116. }
  117. static ssize_t queue_physical_block_size_show(struct request_queue *q, char *page)
  118. {
  119. return queue_var_show(queue_physical_block_size(q), page);
  120. }
  121. static ssize_t queue_chunk_sectors_show(struct request_queue *q, char *page)
  122. {
  123. return queue_var_show(q->limits.chunk_sectors, 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_hw_show(struct request_queue *q, char *page)
  138. {
  139. return sprintf(page, "%llu\n",
  140. (unsigned long long)q->limits.max_hw_discard_sectors << 9);
  141. }
  142. static ssize_t queue_discard_max_show(struct request_queue *q, char *page)
  143. {
  144. return sprintf(page, "%llu\n",
  145. (unsigned long long)q->limits.max_discard_sectors << 9);
  146. }
  147. static ssize_t queue_discard_max_store(struct request_queue *q,
  148. const char *page, size_t count)
  149. {
  150. unsigned long max_discard;
  151. ssize_t ret = queue_var_store(&max_discard, page, count);
  152. if (ret < 0)
  153. return ret;
  154. if (max_discard & (q->limits.discard_granularity - 1))
  155. return -EINVAL;
  156. max_discard >>= 9;
  157. if (max_discard > UINT_MAX)
  158. return -EINVAL;
  159. if (max_discard > q->limits.max_hw_discard_sectors)
  160. max_discard = q->limits.max_hw_discard_sectors;
  161. q->limits.max_discard_sectors = max_discard;
  162. return ret;
  163. }
  164. static ssize_t queue_discard_zeroes_data_show(struct request_queue *q, char *page)
  165. {
  166. return queue_var_show(0, page);
  167. }
  168. static ssize_t queue_write_same_max_show(struct request_queue *q, char *page)
  169. {
  170. return sprintf(page, "%llu\n",
  171. (unsigned long long)q->limits.max_write_same_sectors << 9);
  172. }
  173. static ssize_t queue_write_zeroes_max_show(struct request_queue *q, char *page)
  174. {
  175. return sprintf(page, "%llu\n",
  176. (unsigned long long)q->limits.max_write_zeroes_sectors << 9);
  177. }
  178. static ssize_t
  179. queue_max_sectors_store(struct request_queue *q, const char *page, size_t count)
  180. {
  181. unsigned long max_sectors_kb,
  182. max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1,
  183. page_kb = 1 << (PAGE_SHIFT - 10);
  184. ssize_t ret = queue_var_store(&max_sectors_kb, page, count);
  185. if (ret < 0)
  186. return ret;
  187. max_hw_sectors_kb = min_not_zero(max_hw_sectors_kb, (unsigned long)
  188. q->limits.max_dev_sectors >> 1);
  189. if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb)
  190. return -EINVAL;
  191. spin_lock_irq(q->queue_lock);
  192. q->limits.max_sectors = max_sectors_kb << 1;
  193. q->backing_dev_info->io_pages = max_sectors_kb >> (PAGE_SHIFT - 10);
  194. spin_unlock_irq(q->queue_lock);
  195. return ret;
  196. }
  197. static ssize_t queue_max_hw_sectors_show(struct request_queue *q, char *page)
  198. {
  199. int max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1;
  200. return queue_var_show(max_hw_sectors_kb, (page));
  201. }
  202. #define QUEUE_SYSFS_BIT_FNS(name, flag, neg) \
  203. static ssize_t \
  204. queue_show_##name(struct request_queue *q, char *page) \
  205. { \
  206. int bit; \
  207. bit = test_bit(QUEUE_FLAG_##flag, &q->queue_flags); \
  208. return queue_var_show(neg ? !bit : bit, page); \
  209. } \
  210. static ssize_t \
  211. queue_store_##name(struct request_queue *q, const char *page, size_t count) \
  212. { \
  213. unsigned long val; \
  214. ssize_t ret; \
  215. ret = queue_var_store(&val, page, count); \
  216. if (ret < 0) \
  217. return ret; \
  218. if (neg) \
  219. val = !val; \
  220. \
  221. spin_lock_irq(q->queue_lock); \
  222. if (val) \
  223. queue_flag_set(QUEUE_FLAG_##flag, q); \
  224. else \
  225. queue_flag_clear(QUEUE_FLAG_##flag, q); \
  226. spin_unlock_irq(q->queue_lock); \
  227. return ret; \
  228. }
  229. QUEUE_SYSFS_BIT_FNS(nonrot, NONROT, 1);
  230. QUEUE_SYSFS_BIT_FNS(random, ADD_RANDOM, 0);
  231. QUEUE_SYSFS_BIT_FNS(iostats, IO_STAT, 0);
  232. #undef QUEUE_SYSFS_BIT_FNS
  233. static ssize_t queue_zoned_show(struct request_queue *q, char *page)
  234. {
  235. switch (blk_queue_zoned_model(q)) {
  236. case BLK_ZONED_HA:
  237. return sprintf(page, "host-aware\n");
  238. case BLK_ZONED_HM:
  239. return sprintf(page, "host-managed\n");
  240. default:
  241. return sprintf(page, "none\n");
  242. }
  243. }
  244. static ssize_t queue_nomerges_show(struct request_queue *q, char *page)
  245. {
  246. return queue_var_show((blk_queue_nomerges(q) << 1) |
  247. blk_queue_noxmerges(q), page);
  248. }
  249. static ssize_t queue_nomerges_store(struct request_queue *q, const char *page,
  250. size_t count)
  251. {
  252. unsigned long nm;
  253. ssize_t ret = queue_var_store(&nm, page, count);
  254. if (ret < 0)
  255. return ret;
  256. spin_lock_irq(q->queue_lock);
  257. queue_flag_clear(QUEUE_FLAG_NOMERGES, q);
  258. queue_flag_clear(QUEUE_FLAG_NOXMERGES, q);
  259. if (nm == 2)
  260. queue_flag_set(QUEUE_FLAG_NOMERGES, q);
  261. else if (nm)
  262. queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
  263. spin_unlock_irq(q->queue_lock);
  264. return ret;
  265. }
  266. static ssize_t queue_rq_affinity_show(struct request_queue *q, char *page)
  267. {
  268. bool set = test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags);
  269. bool force = test_bit(QUEUE_FLAG_SAME_FORCE, &q->queue_flags);
  270. return queue_var_show(set << force, page);
  271. }
  272. static ssize_t
  273. queue_rq_affinity_store(struct request_queue *q, const char *page, size_t count)
  274. {
  275. ssize_t ret = -EINVAL;
  276. #ifdef CONFIG_SMP
  277. unsigned long val;
  278. ret = queue_var_store(&val, page, count);
  279. if (ret < 0)
  280. return ret;
  281. spin_lock_irq(q->queue_lock);
  282. if (val == 2) {
  283. queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
  284. queue_flag_set(QUEUE_FLAG_SAME_FORCE, q);
  285. } else if (val == 1) {
  286. queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
  287. queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
  288. } else if (val == 0) {
  289. queue_flag_clear(QUEUE_FLAG_SAME_COMP, q);
  290. queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
  291. }
  292. spin_unlock_irq(q->queue_lock);
  293. #endif
  294. return ret;
  295. }
  296. static ssize_t queue_poll_delay_show(struct request_queue *q, char *page)
  297. {
  298. int val;
  299. if (q->poll_nsec == -1)
  300. val = -1;
  301. else
  302. val = q->poll_nsec / 1000;
  303. return sprintf(page, "%d\n", val);
  304. }
  305. static ssize_t queue_poll_delay_store(struct request_queue *q, const char *page,
  306. size_t count)
  307. {
  308. int err, val;
  309. if (!q->mq_ops || !q->mq_ops->poll)
  310. return -EINVAL;
  311. err = kstrtoint(page, 10, &val);
  312. if (err < 0)
  313. return err;
  314. if (val == -1)
  315. q->poll_nsec = -1;
  316. else
  317. q->poll_nsec = val * 1000;
  318. return count;
  319. }
  320. static ssize_t queue_poll_show(struct request_queue *q, char *page)
  321. {
  322. return queue_var_show(test_bit(QUEUE_FLAG_POLL, &q->queue_flags), page);
  323. }
  324. static ssize_t queue_poll_store(struct request_queue *q, const char *page,
  325. size_t count)
  326. {
  327. unsigned long poll_on;
  328. ssize_t ret;
  329. if (!q->mq_ops || !q->mq_ops->poll)
  330. return -EINVAL;
  331. ret = queue_var_store(&poll_on, page, count);
  332. if (ret < 0)
  333. return ret;
  334. spin_lock_irq(q->queue_lock);
  335. if (poll_on)
  336. queue_flag_set(QUEUE_FLAG_POLL, q);
  337. else
  338. queue_flag_clear(QUEUE_FLAG_POLL, q);
  339. spin_unlock_irq(q->queue_lock);
  340. return ret;
  341. }
  342. static ssize_t queue_wb_lat_show(struct request_queue *q, char *page)
  343. {
  344. if (!q->rq_wb)
  345. return -EINVAL;
  346. return sprintf(page, "%llu\n", div_u64(q->rq_wb->min_lat_nsec, 1000));
  347. }
  348. static ssize_t queue_wb_lat_store(struct request_queue *q, const char *page,
  349. size_t count)
  350. {
  351. struct rq_wb *rwb;
  352. ssize_t ret;
  353. s64 val;
  354. ret = queue_var_store64(&val, page);
  355. if (ret < 0)
  356. return ret;
  357. if (val < -1)
  358. return -EINVAL;
  359. rwb = q->rq_wb;
  360. if (!rwb) {
  361. ret = wbt_init(q);
  362. if (ret)
  363. return ret;
  364. rwb = q->rq_wb;
  365. if (!rwb)
  366. return -EINVAL;
  367. }
  368. if (val == -1)
  369. rwb->min_lat_nsec = wbt_default_latency_nsec(q);
  370. else if (val >= 0)
  371. rwb->min_lat_nsec = val * 1000ULL;
  372. if (rwb->enable_state == WBT_STATE_ON_DEFAULT)
  373. rwb->enable_state = WBT_STATE_ON_MANUAL;
  374. wbt_update_limits(rwb);
  375. return count;
  376. }
  377. static ssize_t queue_wc_show(struct request_queue *q, char *page)
  378. {
  379. if (test_bit(QUEUE_FLAG_WC, &q->queue_flags))
  380. return sprintf(page, "write back\n");
  381. return sprintf(page, "write through\n");
  382. }
  383. static ssize_t queue_wc_store(struct request_queue *q, const char *page,
  384. size_t count)
  385. {
  386. int set = -1;
  387. if (!strncmp(page, "write back", 10))
  388. set = 1;
  389. else if (!strncmp(page, "write through", 13) ||
  390. !strncmp(page, "none", 4))
  391. set = 0;
  392. if (set == -1)
  393. return -EINVAL;
  394. spin_lock_irq(q->queue_lock);
  395. if (set)
  396. queue_flag_set(QUEUE_FLAG_WC, q);
  397. else
  398. queue_flag_clear(QUEUE_FLAG_WC, q);
  399. spin_unlock_irq(q->queue_lock);
  400. return count;
  401. }
  402. static ssize_t queue_dax_show(struct request_queue *q, char *page)
  403. {
  404. return queue_var_show(blk_queue_dax(q), page);
  405. }
  406. static ssize_t queue_inline_crypt_show(struct request_queue *q, char *page)
  407. {
  408. return queue_var_show(blk_queue_inline_crypt(q), page);
  409. }
  410. static struct queue_sysfs_entry queue_requests_entry = {
  411. .attr = {.name = "nr_requests", .mode = S_IRUGO | S_IWUSR },
  412. .show = queue_requests_show,
  413. .store = queue_requests_store,
  414. };
  415. static struct queue_sysfs_entry queue_ra_entry = {
  416. .attr = {.name = "read_ahead_kb", .mode = S_IRUGO | S_IWUSR },
  417. .show = queue_ra_show,
  418. .store = queue_ra_store,
  419. };
  420. static struct queue_sysfs_entry queue_max_sectors_entry = {
  421. .attr = {.name = "max_sectors_kb", .mode = S_IRUGO | S_IWUSR },
  422. .show = queue_max_sectors_show,
  423. .store = queue_max_sectors_store,
  424. };
  425. static struct queue_sysfs_entry queue_max_hw_sectors_entry = {
  426. .attr = {.name = "max_hw_sectors_kb", .mode = S_IRUGO },
  427. .show = queue_max_hw_sectors_show,
  428. };
  429. static struct queue_sysfs_entry queue_max_segments_entry = {
  430. .attr = {.name = "max_segments", .mode = S_IRUGO },
  431. .show = queue_max_segments_show,
  432. };
  433. static struct queue_sysfs_entry queue_max_discard_segments_entry = {
  434. .attr = {.name = "max_discard_segments", .mode = S_IRUGO },
  435. .show = queue_max_discard_segments_show,
  436. };
  437. static struct queue_sysfs_entry queue_max_integrity_segments_entry = {
  438. .attr = {.name = "max_integrity_segments", .mode = S_IRUGO },
  439. .show = queue_max_integrity_segments_show,
  440. };
  441. static struct queue_sysfs_entry queue_max_segment_size_entry = {
  442. .attr = {.name = "max_segment_size", .mode = S_IRUGO },
  443. .show = queue_max_segment_size_show,
  444. };
  445. static struct queue_sysfs_entry queue_iosched_entry = {
  446. .attr = {.name = "scheduler", .mode = S_IRUGO | S_IWUSR },
  447. .show = elv_iosched_show,
  448. .store = elv_iosched_store,
  449. };
  450. static struct queue_sysfs_entry queue_hw_sector_size_entry = {
  451. .attr = {.name = "hw_sector_size", .mode = S_IRUGO },
  452. .show = queue_logical_block_size_show,
  453. };
  454. static struct queue_sysfs_entry queue_logical_block_size_entry = {
  455. .attr = {.name = "logical_block_size", .mode = S_IRUGO },
  456. .show = queue_logical_block_size_show,
  457. };
  458. static struct queue_sysfs_entry queue_physical_block_size_entry = {
  459. .attr = {.name = "physical_block_size", .mode = S_IRUGO },
  460. .show = queue_physical_block_size_show,
  461. };
  462. static struct queue_sysfs_entry queue_chunk_sectors_entry = {
  463. .attr = {.name = "chunk_sectors", .mode = S_IRUGO },
  464. .show = queue_chunk_sectors_show,
  465. };
  466. static struct queue_sysfs_entry queue_io_min_entry = {
  467. .attr = {.name = "minimum_io_size", .mode = S_IRUGO },
  468. .show = queue_io_min_show,
  469. };
  470. static struct queue_sysfs_entry queue_io_opt_entry = {
  471. .attr = {.name = "optimal_io_size", .mode = S_IRUGO },
  472. .show = queue_io_opt_show,
  473. };
  474. static struct queue_sysfs_entry queue_discard_granularity_entry = {
  475. .attr = {.name = "discard_granularity", .mode = S_IRUGO },
  476. .show = queue_discard_granularity_show,
  477. };
  478. static struct queue_sysfs_entry queue_discard_max_hw_entry = {
  479. .attr = {.name = "discard_max_hw_bytes", .mode = S_IRUGO },
  480. .show = queue_discard_max_hw_show,
  481. };
  482. static struct queue_sysfs_entry queue_discard_max_entry = {
  483. .attr = {.name = "discard_max_bytes", .mode = S_IRUGO | S_IWUSR },
  484. .show = queue_discard_max_show,
  485. .store = queue_discard_max_store,
  486. };
  487. static struct queue_sysfs_entry queue_discard_zeroes_data_entry = {
  488. .attr = {.name = "discard_zeroes_data", .mode = S_IRUGO },
  489. .show = queue_discard_zeroes_data_show,
  490. };
  491. static struct queue_sysfs_entry queue_write_same_max_entry = {
  492. .attr = {.name = "write_same_max_bytes", .mode = S_IRUGO },
  493. .show = queue_write_same_max_show,
  494. };
  495. static struct queue_sysfs_entry queue_write_zeroes_max_entry = {
  496. .attr = {.name = "write_zeroes_max_bytes", .mode = S_IRUGO },
  497. .show = queue_write_zeroes_max_show,
  498. };
  499. static struct queue_sysfs_entry queue_nonrot_entry = {
  500. .attr = {.name = "rotational", .mode = S_IRUGO | S_IWUSR },
  501. .show = queue_show_nonrot,
  502. .store = queue_store_nonrot,
  503. };
  504. static struct queue_sysfs_entry queue_zoned_entry = {
  505. .attr = {.name = "zoned", .mode = S_IRUGO },
  506. .show = queue_zoned_show,
  507. };
  508. static struct queue_sysfs_entry queue_nomerges_entry = {
  509. .attr = {.name = "nomerges", .mode = S_IRUGO | S_IWUSR },
  510. .show = queue_nomerges_show,
  511. .store = queue_nomerges_store,
  512. };
  513. static struct queue_sysfs_entry queue_rq_affinity_entry = {
  514. .attr = {.name = "rq_affinity", .mode = S_IRUGO | S_IWUSR },
  515. .show = queue_rq_affinity_show,
  516. .store = queue_rq_affinity_store,
  517. };
  518. static struct queue_sysfs_entry queue_iostats_entry = {
  519. .attr = {.name = "iostats", .mode = S_IRUGO | S_IWUSR },
  520. .show = queue_show_iostats,
  521. .store = queue_store_iostats,
  522. };
  523. static struct queue_sysfs_entry queue_random_entry = {
  524. .attr = {.name = "add_random", .mode = S_IRUGO | S_IWUSR },
  525. .show = queue_show_random,
  526. .store = queue_store_random,
  527. };
  528. static struct queue_sysfs_entry queue_poll_entry = {
  529. .attr = {.name = "io_poll", .mode = S_IRUGO | S_IWUSR },
  530. .show = queue_poll_show,
  531. .store = queue_poll_store,
  532. };
  533. static struct queue_sysfs_entry queue_poll_delay_entry = {
  534. .attr = {.name = "io_poll_delay", .mode = S_IRUGO | S_IWUSR },
  535. .show = queue_poll_delay_show,
  536. .store = queue_poll_delay_store,
  537. };
  538. static struct queue_sysfs_entry queue_inline_crypt_entry = {
  539. .attr = {.name = "inline_crypt", .mode = S_IRUGO },
  540. .show = queue_inline_crypt_show,
  541. };
  542. static struct queue_sysfs_entry queue_wc_entry = {
  543. .attr = {.name = "write_cache", .mode = S_IRUGO | S_IWUSR },
  544. .show = queue_wc_show,
  545. .store = queue_wc_store,
  546. };
  547. static struct queue_sysfs_entry queue_dax_entry = {
  548. .attr = {.name = "dax", .mode = S_IRUGO },
  549. .show = queue_dax_show,
  550. };
  551. static struct queue_sysfs_entry queue_wb_lat_entry = {
  552. .attr = {.name = "wbt_lat_usec", .mode = S_IRUGO | S_IWUSR },
  553. .show = queue_wb_lat_show,
  554. .store = queue_wb_lat_store,
  555. };
  556. #ifdef CONFIG_BLK_DEV_THROTTLING_LOW
  557. static struct queue_sysfs_entry throtl_sample_time_entry = {
  558. .attr = {.name = "throttle_sample_time", .mode = S_IRUGO | S_IWUSR },
  559. .show = blk_throtl_sample_time_show,
  560. .store = blk_throtl_sample_time_store,
  561. };
  562. #endif
  563. static struct attribute *default_attrs[] = {
  564. &queue_requests_entry.attr,
  565. &queue_ra_entry.attr,
  566. &queue_max_hw_sectors_entry.attr,
  567. &queue_max_sectors_entry.attr,
  568. &queue_max_segments_entry.attr,
  569. &queue_max_discard_segments_entry.attr,
  570. &queue_max_integrity_segments_entry.attr,
  571. &queue_max_segment_size_entry.attr,
  572. &queue_iosched_entry.attr,
  573. &queue_hw_sector_size_entry.attr,
  574. &queue_logical_block_size_entry.attr,
  575. &queue_physical_block_size_entry.attr,
  576. &queue_chunk_sectors_entry.attr,
  577. &queue_io_min_entry.attr,
  578. &queue_io_opt_entry.attr,
  579. &queue_discard_granularity_entry.attr,
  580. &queue_discard_max_entry.attr,
  581. &queue_discard_max_hw_entry.attr,
  582. &queue_discard_zeroes_data_entry.attr,
  583. &queue_write_same_max_entry.attr,
  584. &queue_write_zeroes_max_entry.attr,
  585. &queue_nonrot_entry.attr,
  586. &queue_zoned_entry.attr,
  587. &queue_nomerges_entry.attr,
  588. &queue_rq_affinity_entry.attr,
  589. &queue_iostats_entry.attr,
  590. &queue_random_entry.attr,
  591. &queue_poll_entry.attr,
  592. &queue_wc_entry.attr,
  593. &queue_dax_entry.attr,
  594. &queue_wb_lat_entry.attr,
  595. &queue_poll_delay_entry.attr,
  596. &queue_inline_crypt_entry.attr,
  597. #ifdef CONFIG_BLK_DEV_THROTTLING_LOW
  598. &throtl_sample_time_entry.attr,
  599. #endif
  600. NULL,
  601. };
  602. #define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
  603. static ssize_t
  604. queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
  605. {
  606. struct queue_sysfs_entry *entry = to_queue(attr);
  607. struct request_queue *q =
  608. container_of(kobj, struct request_queue, kobj);
  609. ssize_t res;
  610. if (!entry->show)
  611. return -EIO;
  612. mutex_lock(&q->sysfs_lock);
  613. if (blk_queue_dying(q)) {
  614. mutex_unlock(&q->sysfs_lock);
  615. return -ENOENT;
  616. }
  617. res = entry->show(q, page);
  618. mutex_unlock(&q->sysfs_lock);
  619. return res;
  620. }
  621. static ssize_t
  622. queue_attr_store(struct kobject *kobj, struct attribute *attr,
  623. const char *page, size_t length)
  624. {
  625. struct queue_sysfs_entry *entry = to_queue(attr);
  626. struct request_queue *q;
  627. ssize_t res;
  628. if (!entry->store)
  629. return -EIO;
  630. q = container_of(kobj, struct request_queue, kobj);
  631. mutex_lock(&q->sysfs_lock);
  632. if (blk_queue_dying(q)) {
  633. mutex_unlock(&q->sysfs_lock);
  634. return -ENOENT;
  635. }
  636. res = entry->store(q, page, length);
  637. mutex_unlock(&q->sysfs_lock);
  638. return res;
  639. }
  640. static void blk_free_queue_rcu(struct rcu_head *rcu_head)
  641. {
  642. struct request_queue *q = container_of(rcu_head, struct request_queue,
  643. rcu_head);
  644. kmem_cache_free(blk_requestq_cachep, q);
  645. }
  646. /**
  647. * __blk_release_queue - release a request queue when it is no longer needed
  648. * @work: pointer to the release_work member of the request queue to be released
  649. *
  650. * Description:
  651. * blk_release_queue is the counterpart of blk_init_queue(). It should be
  652. * called when a request queue is being released; typically when a block
  653. * device is being de-registered. Its primary task it to free the queue
  654. * itself.
  655. *
  656. * Notes:
  657. * The low level driver must have finished any outstanding requests first
  658. * via blk_cleanup_queue().
  659. *
  660. * Although blk_release_queue() may be called with preemption disabled,
  661. * __blk_release_queue() may sleep.
  662. */
  663. static void __blk_release_queue(struct work_struct *work)
  664. {
  665. struct request_queue *q = container_of(work, typeof(*q), release_work);
  666. if (test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags))
  667. blk_stat_remove_callback(q, q->poll_cb);
  668. blk_stat_free_callback(q->poll_cb);
  669. bdi_put(q->backing_dev_info);
  670. blkcg_exit_queue(q);
  671. if (q->elevator) {
  672. ioc_clear_queue(q);
  673. elevator_exit(q, q->elevator);
  674. }
  675. blk_free_queue_stats(q->stats);
  676. if (q->mq_ops)
  677. cancel_delayed_work_sync(&q->requeue_work);
  678. blk_exit_rl(q, &q->root_rl);
  679. if (q->queue_tags)
  680. __blk_queue_free_tags(q);
  681. if (!q->mq_ops) {
  682. if (q->exit_rq_fn)
  683. q->exit_rq_fn(q, q->fq->flush_rq);
  684. blk_free_flush_queue(q->fq);
  685. } else {
  686. blk_mq_release(q);
  687. }
  688. blk_trace_shutdown(q);
  689. if (q->mq_ops)
  690. blk_mq_debugfs_unregister(q);
  691. if (q->bio_split)
  692. bioset_free(q->bio_split);
  693. ida_simple_remove(&blk_queue_ida, q->id);
  694. call_rcu(&q->rcu_head, blk_free_queue_rcu);
  695. }
  696. static void blk_release_queue(struct kobject *kobj)
  697. {
  698. struct request_queue *q =
  699. container_of(kobj, struct request_queue, kobj);
  700. INIT_WORK(&q->release_work, __blk_release_queue);
  701. schedule_work(&q->release_work);
  702. }
  703. static const struct sysfs_ops queue_sysfs_ops = {
  704. .show = queue_attr_show,
  705. .store = queue_attr_store,
  706. };
  707. struct kobj_type blk_queue_ktype = {
  708. .sysfs_ops = &queue_sysfs_ops,
  709. .default_attrs = default_attrs,
  710. .release = blk_release_queue,
  711. };
  712. int blk_register_queue(struct gendisk *disk)
  713. {
  714. int ret;
  715. struct device *dev = disk_to_dev(disk);
  716. struct request_queue *q = disk->queue;
  717. if (WARN_ON(!q))
  718. return -ENXIO;
  719. WARN_ONCE(test_bit(QUEUE_FLAG_REGISTERED, &q->queue_flags),
  720. "%s is registering an already registered queue\n",
  721. kobject_name(&dev->kobj));
  722. queue_flag_set_unlocked(QUEUE_FLAG_REGISTERED, q);
  723. /*
  724. * SCSI probing may synchronously create and destroy a lot of
  725. * request_queues for non-existent devices. Shutting down a fully
  726. * functional queue takes measureable wallclock time as RCU grace
  727. * periods are involved. To avoid excessive latency in these
  728. * cases, a request_queue starts out in a degraded mode which is
  729. * faster to shut down and is made fully functional here as
  730. * request_queues for non-existent devices never get registered.
  731. */
  732. if (!blk_queue_init_done(q)) {
  733. queue_flag_set_unlocked(QUEUE_FLAG_INIT_DONE, q);
  734. percpu_ref_switch_to_percpu(&q->q_usage_counter);
  735. blk_queue_bypass_end(q);
  736. }
  737. ret = blk_trace_init_sysfs(dev);
  738. if (ret)
  739. return ret;
  740. /* Prevent changes through sysfs until registration is completed. */
  741. mutex_lock(&q->sysfs_lock);
  742. ret = kobject_add(&q->kobj, kobject_get(&dev->kobj), "%s", "queue");
  743. if (ret < 0) {
  744. blk_trace_remove_sysfs(dev);
  745. goto unlock;
  746. }
  747. if (q->mq_ops) {
  748. __blk_mq_register_dev(dev, q);
  749. blk_mq_debugfs_register(q);
  750. }
  751. kobject_uevent(&q->kobj, KOBJ_ADD);
  752. wbt_enable_default(q);
  753. blk_throtl_register_queue(q);
  754. if (q->request_fn || (q->mq_ops && q->elevator)) {
  755. ret = elv_register_queue(q);
  756. if (ret) {
  757. kobject_uevent(&q->kobj, KOBJ_REMOVE);
  758. kobject_del(&q->kobj);
  759. blk_trace_remove_sysfs(dev);
  760. kobject_put(&dev->kobj);
  761. goto unlock;
  762. }
  763. }
  764. ret = 0;
  765. unlock:
  766. mutex_unlock(&q->sysfs_lock);
  767. return ret;
  768. }
  769. void blk_unregister_queue(struct gendisk *disk)
  770. {
  771. struct request_queue *q = disk->queue;
  772. if (WARN_ON(!q))
  773. return;
  774. mutex_lock(&q->sysfs_lock);
  775. queue_flag_clear_unlocked(QUEUE_FLAG_REGISTERED, q);
  776. mutex_unlock(&q->sysfs_lock);
  777. wbt_exit(q);
  778. if (q->mq_ops)
  779. blk_mq_unregister_dev(disk_to_dev(disk), q);
  780. if (q->request_fn || (q->mq_ops && q->elevator))
  781. elv_unregister_queue(q);
  782. kobject_uevent(&q->kobj, KOBJ_REMOVE);
  783. kobject_del(&q->kobj);
  784. blk_trace_remove_sysfs(disk_to_dev(disk));
  785. kobject_put(&disk_to_dev(disk)->kobj);
  786. }