blk-sysfs.c 23 KB

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