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