blk-sysfs.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  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. struct queue_sysfs_entry {
  16. struct attribute attr;
  17. ssize_t (*show)(struct request_queue *, char *);
  18. ssize_t (*store)(struct request_queue *, const char *, size_t);
  19. };
  20. static ssize_t
  21. queue_var_show(unsigned long var, char *page)
  22. {
  23. return sprintf(page, "%lu\n", var);
  24. }
  25. static ssize_t
  26. queue_var_store(unsigned long *var, const char *page, size_t count)
  27. {
  28. int err;
  29. unsigned long v;
  30. err = kstrtoul(page, 10, &v);
  31. if (err || v > UINT_MAX)
  32. return -EINVAL;
  33. *var = v;
  34. return count;
  35. }
  36. static ssize_t queue_requests_show(struct request_queue *q, char *page)
  37. {
  38. return queue_var_show(q->nr_requests, (page));
  39. }
  40. static ssize_t
  41. queue_requests_store(struct request_queue *q, const char *page, size_t count)
  42. {
  43. unsigned long nr;
  44. int ret, err;
  45. if (!q->request_fn && !q->mq_ops)
  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. if (q->request_fn)
  53. err = blk_update_nr_requests(q, nr);
  54. else
  55. err = blk_mq_update_nr_requests(q, nr);
  56. if (err)
  57. return err;
  58. return ret;
  59. }
  60. static ssize_t queue_ra_show(struct request_queue *q, char *page)
  61. {
  62. unsigned long ra_kb = q->backing_dev_info.ra_pages <<
  63. (PAGE_SHIFT - 10);
  64. return queue_var_show(ra_kb, (page));
  65. }
  66. static ssize_t
  67. queue_ra_store(struct request_queue *q, const char *page, size_t count)
  68. {
  69. unsigned long ra_kb;
  70. ssize_t ret = queue_var_store(&ra_kb, page, count);
  71. if (ret < 0)
  72. return ret;
  73. q->backing_dev_info.ra_pages = ra_kb >> (PAGE_SHIFT - 10);
  74. return ret;
  75. }
  76. static ssize_t queue_max_sectors_show(struct request_queue *q, char *page)
  77. {
  78. int max_sectors_kb = queue_max_sectors(q) >> 1;
  79. return queue_var_show(max_sectors_kb, (page));
  80. }
  81. static ssize_t queue_max_segments_show(struct request_queue *q, char *page)
  82. {
  83. return queue_var_show(queue_max_segments(q), (page));
  84. }
  85. static ssize_t queue_max_integrity_segments_show(struct request_queue *q, char *page)
  86. {
  87. return queue_var_show(q->limits.max_integrity_segments, (page));
  88. }
  89. static ssize_t queue_max_segment_size_show(struct request_queue *q, char *page)
  90. {
  91. if (blk_queue_cluster(q))
  92. return queue_var_show(queue_max_segment_size(q), (page));
  93. return queue_var_show(PAGE_SIZE, (page));
  94. }
  95. static ssize_t queue_logical_block_size_show(struct request_queue *q, char *page)
  96. {
  97. return queue_var_show(queue_logical_block_size(q), page);
  98. }
  99. static ssize_t queue_physical_block_size_show(struct request_queue *q, char *page)
  100. {
  101. return queue_var_show(queue_physical_block_size(q), page);
  102. }
  103. static ssize_t queue_io_min_show(struct request_queue *q, char *page)
  104. {
  105. return queue_var_show(queue_io_min(q), page);
  106. }
  107. static ssize_t queue_io_opt_show(struct request_queue *q, char *page)
  108. {
  109. return queue_var_show(queue_io_opt(q), page);
  110. }
  111. static ssize_t queue_discard_granularity_show(struct request_queue *q, char *page)
  112. {
  113. return queue_var_show(q->limits.discard_granularity, page);
  114. }
  115. static ssize_t queue_discard_max_hw_show(struct request_queue *q, char *page)
  116. {
  117. return sprintf(page, "%llu\n",
  118. (unsigned long long)q->limits.max_hw_discard_sectors << 9);
  119. }
  120. static ssize_t queue_discard_max_show(struct request_queue *q, char *page)
  121. {
  122. return sprintf(page, "%llu\n",
  123. (unsigned long long)q->limits.max_discard_sectors << 9);
  124. }
  125. static ssize_t queue_discard_max_store(struct request_queue *q,
  126. const char *page, size_t count)
  127. {
  128. unsigned long max_discard;
  129. ssize_t ret = queue_var_store(&max_discard, page, count);
  130. if (ret < 0)
  131. return ret;
  132. if (max_discard & (q->limits.discard_granularity - 1))
  133. return -EINVAL;
  134. max_discard >>= 9;
  135. if (max_discard > UINT_MAX)
  136. return -EINVAL;
  137. if (max_discard > q->limits.max_hw_discard_sectors)
  138. max_discard = q->limits.max_hw_discard_sectors;
  139. q->limits.max_discard_sectors = max_discard;
  140. return ret;
  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_SHIFT - 10);
  157. ssize_t ret = queue_var_store(&max_sectors_kb, page, count);
  158. if (ret < 0)
  159. return ret;
  160. max_hw_sectors_kb = min_not_zero(max_hw_sectors_kb, (unsigned long)
  161. q->limits.max_dev_sectors >> 1);
  162. if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb)
  163. return -EINVAL;
  164. spin_lock_irq(q->queue_lock);
  165. q->limits.max_sectors = max_sectors_kb << 1;
  166. spin_unlock_irq(q->queue_lock);
  167. return ret;
  168. }
  169. static ssize_t queue_max_hw_sectors_show(struct request_queue *q, char *page)
  170. {
  171. int max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1;
  172. return queue_var_show(max_hw_sectors_kb, (page));
  173. }
  174. #define QUEUE_SYSFS_BIT_FNS(name, flag, neg) \
  175. static ssize_t \
  176. queue_show_##name(struct request_queue *q, char *page) \
  177. { \
  178. int bit; \
  179. bit = test_bit(QUEUE_FLAG_##flag, &q->queue_flags); \
  180. return queue_var_show(neg ? !bit : bit, page); \
  181. } \
  182. static ssize_t \
  183. queue_store_##name(struct request_queue *q, const char *page, size_t count) \
  184. { \
  185. unsigned long val; \
  186. ssize_t ret; \
  187. ret = queue_var_store(&val, page, count); \
  188. if (ret < 0) \
  189. return ret; \
  190. if (neg) \
  191. val = !val; \
  192. \
  193. spin_lock_irq(q->queue_lock); \
  194. if (val) \
  195. queue_flag_set(QUEUE_FLAG_##flag, q); \
  196. else \
  197. queue_flag_clear(QUEUE_FLAG_##flag, q); \
  198. spin_unlock_irq(q->queue_lock); \
  199. return ret; \
  200. }
  201. QUEUE_SYSFS_BIT_FNS(nonrot, NONROT, 1);
  202. QUEUE_SYSFS_BIT_FNS(random, ADD_RANDOM, 0);
  203. QUEUE_SYSFS_BIT_FNS(iostats, IO_STAT, 0);
  204. #undef QUEUE_SYSFS_BIT_FNS
  205. static ssize_t queue_nomerges_show(struct request_queue *q, char *page)
  206. {
  207. return queue_var_show((blk_queue_nomerges(q) << 1) |
  208. blk_queue_noxmerges(q), page);
  209. }
  210. static ssize_t queue_nomerges_store(struct request_queue *q, const char *page,
  211. size_t count)
  212. {
  213. unsigned long nm;
  214. ssize_t ret = queue_var_store(&nm, page, count);
  215. if (ret < 0)
  216. return ret;
  217. spin_lock_irq(q->queue_lock);
  218. queue_flag_clear(QUEUE_FLAG_NOMERGES, q);
  219. queue_flag_clear(QUEUE_FLAG_NOXMERGES, q);
  220. if (nm == 2)
  221. queue_flag_set(QUEUE_FLAG_NOMERGES, q);
  222. else if (nm)
  223. queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
  224. spin_unlock_irq(q->queue_lock);
  225. return ret;
  226. }
  227. static ssize_t queue_rq_affinity_show(struct request_queue *q, char *page)
  228. {
  229. bool set = test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags);
  230. bool force = test_bit(QUEUE_FLAG_SAME_FORCE, &q->queue_flags);
  231. return queue_var_show(set << force, page);
  232. }
  233. static ssize_t
  234. queue_rq_affinity_store(struct request_queue *q, const char *page, size_t count)
  235. {
  236. ssize_t ret = -EINVAL;
  237. #ifdef CONFIG_SMP
  238. unsigned long val;
  239. ret = queue_var_store(&val, page, count);
  240. if (ret < 0)
  241. return ret;
  242. spin_lock_irq(q->queue_lock);
  243. if (val == 2) {
  244. queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
  245. queue_flag_set(QUEUE_FLAG_SAME_FORCE, q);
  246. } else if (val == 1) {
  247. queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
  248. queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
  249. } else if (val == 0) {
  250. queue_flag_clear(QUEUE_FLAG_SAME_COMP, q);
  251. queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
  252. }
  253. spin_unlock_irq(q->queue_lock);
  254. #endif
  255. return ret;
  256. }
  257. static ssize_t queue_poll_show(struct request_queue *q, char *page)
  258. {
  259. return queue_var_show(test_bit(QUEUE_FLAG_POLL, &q->queue_flags), page);
  260. }
  261. static ssize_t queue_poll_store(struct request_queue *q, const char *page,
  262. size_t count)
  263. {
  264. unsigned long poll_on;
  265. ssize_t ret;
  266. if (!q->mq_ops || !q->mq_ops->poll)
  267. return -EINVAL;
  268. ret = queue_var_store(&poll_on, page, count);
  269. if (ret < 0)
  270. return ret;
  271. spin_lock_irq(q->queue_lock);
  272. if (poll_on)
  273. queue_flag_set(QUEUE_FLAG_POLL, q);
  274. else
  275. queue_flag_clear(QUEUE_FLAG_POLL, q);
  276. spin_unlock_irq(q->queue_lock);
  277. return ret;
  278. }
  279. static ssize_t queue_wc_show(struct request_queue *q, char *page)
  280. {
  281. if (test_bit(QUEUE_FLAG_WC, &q->queue_flags))
  282. return sprintf(page, "write back\n");
  283. return sprintf(page, "write through\n");
  284. }
  285. static ssize_t queue_wc_store(struct request_queue *q, const char *page,
  286. size_t count)
  287. {
  288. int set = -1;
  289. if (!strncmp(page, "write back", 10))
  290. set = 1;
  291. else if (!strncmp(page, "write through", 13) ||
  292. !strncmp(page, "none", 4))
  293. set = 0;
  294. if (set == -1)
  295. return -EINVAL;
  296. spin_lock_irq(q->queue_lock);
  297. if (set)
  298. queue_flag_set(QUEUE_FLAG_WC, q);
  299. else
  300. queue_flag_clear(QUEUE_FLAG_WC, q);
  301. spin_unlock_irq(q->queue_lock);
  302. return count;
  303. }
  304. static ssize_t queue_dax_show(struct request_queue *q, char *page)
  305. {
  306. return queue_var_show(blk_queue_dax(q), page);
  307. }
  308. static struct queue_sysfs_entry queue_requests_entry = {
  309. .attr = {.name = "nr_requests", .mode = S_IRUGO | S_IWUSR },
  310. .show = queue_requests_show,
  311. .store = queue_requests_store,
  312. };
  313. static struct queue_sysfs_entry queue_ra_entry = {
  314. .attr = {.name = "read_ahead_kb", .mode = S_IRUGO | S_IWUSR },
  315. .show = queue_ra_show,
  316. .store = queue_ra_store,
  317. };
  318. static struct queue_sysfs_entry queue_max_sectors_entry = {
  319. .attr = {.name = "max_sectors_kb", .mode = S_IRUGO | S_IWUSR },
  320. .show = queue_max_sectors_show,
  321. .store = queue_max_sectors_store,
  322. };
  323. static struct queue_sysfs_entry queue_max_hw_sectors_entry = {
  324. .attr = {.name = "max_hw_sectors_kb", .mode = S_IRUGO },
  325. .show = queue_max_hw_sectors_show,
  326. };
  327. static struct queue_sysfs_entry queue_max_segments_entry = {
  328. .attr = {.name = "max_segments", .mode = S_IRUGO },
  329. .show = queue_max_segments_show,
  330. };
  331. static struct queue_sysfs_entry queue_max_integrity_segments_entry = {
  332. .attr = {.name = "max_integrity_segments", .mode = S_IRUGO },
  333. .show = queue_max_integrity_segments_show,
  334. };
  335. static struct queue_sysfs_entry queue_max_segment_size_entry = {
  336. .attr = {.name = "max_segment_size", .mode = S_IRUGO },
  337. .show = queue_max_segment_size_show,
  338. };
  339. static struct queue_sysfs_entry queue_iosched_entry = {
  340. .attr = {.name = "scheduler", .mode = S_IRUGO | S_IWUSR },
  341. .show = elv_iosched_show,
  342. .store = elv_iosched_store,
  343. };
  344. static struct queue_sysfs_entry queue_hw_sector_size_entry = {
  345. .attr = {.name = "hw_sector_size", .mode = S_IRUGO },
  346. .show = queue_logical_block_size_show,
  347. };
  348. static struct queue_sysfs_entry queue_logical_block_size_entry = {
  349. .attr = {.name = "logical_block_size", .mode = S_IRUGO },
  350. .show = queue_logical_block_size_show,
  351. };
  352. static struct queue_sysfs_entry queue_physical_block_size_entry = {
  353. .attr = {.name = "physical_block_size", .mode = S_IRUGO },
  354. .show = queue_physical_block_size_show,
  355. };
  356. static struct queue_sysfs_entry queue_io_min_entry = {
  357. .attr = {.name = "minimum_io_size", .mode = S_IRUGO },
  358. .show = queue_io_min_show,
  359. };
  360. static struct queue_sysfs_entry queue_io_opt_entry = {
  361. .attr = {.name = "optimal_io_size", .mode = S_IRUGO },
  362. .show = queue_io_opt_show,
  363. };
  364. static struct queue_sysfs_entry queue_discard_granularity_entry = {
  365. .attr = {.name = "discard_granularity", .mode = S_IRUGO },
  366. .show = queue_discard_granularity_show,
  367. };
  368. static struct queue_sysfs_entry queue_discard_max_hw_entry = {
  369. .attr = {.name = "discard_max_hw_bytes", .mode = S_IRUGO },
  370. .show = queue_discard_max_hw_show,
  371. };
  372. static struct queue_sysfs_entry queue_discard_max_entry = {
  373. .attr = {.name = "discard_max_bytes", .mode = S_IRUGO | S_IWUSR },
  374. .show = queue_discard_max_show,
  375. .store = queue_discard_max_store,
  376. };
  377. static struct queue_sysfs_entry queue_discard_zeroes_data_entry = {
  378. .attr = {.name = "discard_zeroes_data", .mode = S_IRUGO },
  379. .show = queue_discard_zeroes_data_show,
  380. };
  381. static struct queue_sysfs_entry queue_write_same_max_entry = {
  382. .attr = {.name = "write_same_max_bytes", .mode = S_IRUGO },
  383. .show = queue_write_same_max_show,
  384. };
  385. static struct queue_sysfs_entry queue_nonrot_entry = {
  386. .attr = {.name = "rotational", .mode = S_IRUGO | S_IWUSR },
  387. .show = queue_show_nonrot,
  388. .store = queue_store_nonrot,
  389. };
  390. static struct queue_sysfs_entry queue_nomerges_entry = {
  391. .attr = {.name = "nomerges", .mode = S_IRUGO | S_IWUSR },
  392. .show = queue_nomerges_show,
  393. .store = queue_nomerges_store,
  394. };
  395. static struct queue_sysfs_entry queue_rq_affinity_entry = {
  396. .attr = {.name = "rq_affinity", .mode = S_IRUGO | S_IWUSR },
  397. .show = queue_rq_affinity_show,
  398. .store = queue_rq_affinity_store,
  399. };
  400. static struct queue_sysfs_entry queue_iostats_entry = {
  401. .attr = {.name = "iostats", .mode = S_IRUGO | S_IWUSR },
  402. .show = queue_show_iostats,
  403. .store = queue_store_iostats,
  404. };
  405. static struct queue_sysfs_entry queue_random_entry = {
  406. .attr = {.name = "add_random", .mode = S_IRUGO | S_IWUSR },
  407. .show = queue_show_random,
  408. .store = queue_store_random,
  409. };
  410. static struct queue_sysfs_entry queue_poll_entry = {
  411. .attr = {.name = "io_poll", .mode = S_IRUGO | S_IWUSR },
  412. .show = queue_poll_show,
  413. .store = queue_poll_store,
  414. };
  415. static struct queue_sysfs_entry queue_wc_entry = {
  416. .attr = {.name = "write_cache", .mode = S_IRUGO | S_IWUSR },
  417. .show = queue_wc_show,
  418. .store = queue_wc_store,
  419. };
  420. static struct queue_sysfs_entry queue_dax_entry = {
  421. .attr = {.name = "dax", .mode = S_IRUGO },
  422. .show = queue_dax_show,
  423. };
  424. static struct attribute *default_attrs[] = {
  425. &queue_requests_entry.attr,
  426. &queue_ra_entry.attr,
  427. &queue_max_hw_sectors_entry.attr,
  428. &queue_max_sectors_entry.attr,
  429. &queue_max_segments_entry.attr,
  430. &queue_max_integrity_segments_entry.attr,
  431. &queue_max_segment_size_entry.attr,
  432. &queue_iosched_entry.attr,
  433. &queue_hw_sector_size_entry.attr,
  434. &queue_logical_block_size_entry.attr,
  435. &queue_physical_block_size_entry.attr,
  436. &queue_io_min_entry.attr,
  437. &queue_io_opt_entry.attr,
  438. &queue_discard_granularity_entry.attr,
  439. &queue_discard_max_entry.attr,
  440. &queue_discard_max_hw_entry.attr,
  441. &queue_discard_zeroes_data_entry.attr,
  442. &queue_write_same_max_entry.attr,
  443. &queue_nonrot_entry.attr,
  444. &queue_nomerges_entry.attr,
  445. &queue_rq_affinity_entry.attr,
  446. &queue_iostats_entry.attr,
  447. &queue_random_entry.attr,
  448. &queue_poll_entry.attr,
  449. &queue_wc_entry.attr,
  450. &queue_dax_entry.attr,
  451. NULL,
  452. };
  453. #define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
  454. static ssize_t
  455. queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
  456. {
  457. struct queue_sysfs_entry *entry = to_queue(attr);
  458. struct request_queue *q =
  459. container_of(kobj, struct request_queue, kobj);
  460. ssize_t res;
  461. if (!entry->show)
  462. return -EIO;
  463. mutex_lock(&q->sysfs_lock);
  464. if (blk_queue_dying(q)) {
  465. mutex_unlock(&q->sysfs_lock);
  466. return -ENOENT;
  467. }
  468. res = entry->show(q, page);
  469. mutex_unlock(&q->sysfs_lock);
  470. return res;
  471. }
  472. static ssize_t
  473. queue_attr_store(struct kobject *kobj, struct attribute *attr,
  474. const char *page, size_t length)
  475. {
  476. struct queue_sysfs_entry *entry = to_queue(attr);
  477. struct request_queue *q;
  478. ssize_t res;
  479. if (!entry->store)
  480. return -EIO;
  481. q = container_of(kobj, struct request_queue, kobj);
  482. mutex_lock(&q->sysfs_lock);
  483. if (blk_queue_dying(q)) {
  484. mutex_unlock(&q->sysfs_lock);
  485. return -ENOENT;
  486. }
  487. res = entry->store(q, page, length);
  488. mutex_unlock(&q->sysfs_lock);
  489. return res;
  490. }
  491. static void blk_free_queue_rcu(struct rcu_head *rcu_head)
  492. {
  493. struct request_queue *q = container_of(rcu_head, struct request_queue,
  494. rcu_head);
  495. kmem_cache_free(blk_requestq_cachep, q);
  496. }
  497. /**
  498. * blk_release_queue: - release a &struct request_queue when it is no longer needed
  499. * @kobj: the kobj belonging to the request queue to be released
  500. *
  501. * Description:
  502. * blk_release_queue is the pair to blk_init_queue() or
  503. * blk_queue_make_request(). It should be called when a request queue is
  504. * being released; typically when a block device is being de-registered.
  505. * Currently, its primary task it to free all the &struct request
  506. * structures that were allocated to the queue and the queue itself.
  507. *
  508. * Note:
  509. * The low level driver must have finished any outstanding requests first
  510. * via blk_cleanup_queue().
  511. **/
  512. static void blk_release_queue(struct kobject *kobj)
  513. {
  514. struct request_queue *q =
  515. container_of(kobj, struct request_queue, kobj);
  516. bdi_exit(&q->backing_dev_info);
  517. blkcg_exit_queue(q);
  518. if (q->elevator) {
  519. spin_lock_irq(q->queue_lock);
  520. ioc_clear_queue(q);
  521. spin_unlock_irq(q->queue_lock);
  522. elevator_exit(q->elevator);
  523. }
  524. blk_exit_rl(&q->root_rl);
  525. if (q->queue_tags)
  526. __blk_queue_free_tags(q);
  527. if (!q->mq_ops)
  528. blk_free_flush_queue(q->fq);
  529. else
  530. blk_mq_release(q);
  531. blk_trace_shutdown(q);
  532. if (q->bio_split)
  533. bioset_free(q->bio_split);
  534. ida_simple_remove(&blk_queue_ida, q->id);
  535. call_rcu(&q->rcu_head, blk_free_queue_rcu);
  536. }
  537. static const struct sysfs_ops queue_sysfs_ops = {
  538. .show = queue_attr_show,
  539. .store = queue_attr_store,
  540. };
  541. struct kobj_type blk_queue_ktype = {
  542. .sysfs_ops = &queue_sysfs_ops,
  543. .default_attrs = default_attrs,
  544. .release = blk_release_queue,
  545. };
  546. int blk_register_queue(struct gendisk *disk)
  547. {
  548. int ret;
  549. struct device *dev = disk_to_dev(disk);
  550. struct request_queue *q = disk->queue;
  551. if (WARN_ON(!q))
  552. return -ENXIO;
  553. /*
  554. * SCSI probing may synchronously create and destroy a lot of
  555. * request_queues for non-existent devices. Shutting down a fully
  556. * functional queue takes measureable wallclock time as RCU grace
  557. * periods are involved. To avoid excessive latency in these
  558. * cases, a request_queue starts out in a degraded mode which is
  559. * faster to shut down and is made fully functional here as
  560. * request_queues for non-existent devices never get registered.
  561. */
  562. if (!blk_queue_init_done(q)) {
  563. queue_flag_set_unlocked(QUEUE_FLAG_INIT_DONE, q);
  564. percpu_ref_switch_to_percpu(&q->q_usage_counter);
  565. blk_queue_bypass_end(q);
  566. }
  567. ret = blk_trace_init_sysfs(dev);
  568. if (ret)
  569. return ret;
  570. ret = kobject_add(&q->kobj, kobject_get(&dev->kobj), "%s", "queue");
  571. if (ret < 0) {
  572. blk_trace_remove_sysfs(dev);
  573. return ret;
  574. }
  575. kobject_uevent(&q->kobj, KOBJ_ADD);
  576. if (q->mq_ops)
  577. blk_mq_register_dev(dev, q);
  578. if (!q->request_fn)
  579. return 0;
  580. ret = elv_register_queue(q);
  581. if (ret) {
  582. kobject_uevent(&q->kobj, KOBJ_REMOVE);
  583. kobject_del(&q->kobj);
  584. blk_trace_remove_sysfs(dev);
  585. kobject_put(&dev->kobj);
  586. return ret;
  587. }
  588. return 0;
  589. }
  590. void blk_unregister_queue(struct gendisk *disk)
  591. {
  592. struct request_queue *q = disk->queue;
  593. if (WARN_ON(!q))
  594. return;
  595. if (q->mq_ops)
  596. blk_mq_unregister_dev(disk_to_dev(disk), q);
  597. if (q->request_fn)
  598. elv_unregister_queue(q);
  599. kobject_uevent(&q->kobj, KOBJ_REMOVE);
  600. kobject_del(&q->kobj);
  601. blk_trace_remove_sysfs(disk_to_dev(disk));
  602. kobject_put(&disk_to_dev(disk)->kobj);
  603. }