blk-sysfs.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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_CACHE_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_CACHE_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_CACHE_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. unsigned long long val;
  118. val = q->limits.max_hw_discard_sectors << 9;
  119. return sprintf(page, "%llu\n", val);
  120. }
  121. static ssize_t queue_discard_max_show(struct request_queue *q, char *page)
  122. {
  123. return sprintf(page, "%llu\n",
  124. (unsigned long long)q->limits.max_discard_sectors << 9);
  125. }
  126. static ssize_t queue_discard_max_store(struct request_queue *q,
  127. const char *page, size_t count)
  128. {
  129. unsigned long max_discard;
  130. ssize_t ret = queue_var_store(&max_discard, page, count);
  131. if (ret < 0)
  132. return ret;
  133. if (max_discard & (q->limits.discard_granularity - 1))
  134. return -EINVAL;
  135. max_discard >>= 9;
  136. if (max_discard > UINT_MAX)
  137. return -EINVAL;
  138. if (max_discard > q->limits.max_hw_discard_sectors)
  139. max_discard = q->limits.max_hw_discard_sectors;
  140. q->limits.max_discard_sectors = max_discard;
  141. return ret;
  142. }
  143. static ssize_t queue_discard_zeroes_data_show(struct request_queue *q, char *page)
  144. {
  145. return queue_var_show(queue_discard_zeroes_data(q), page);
  146. }
  147. static ssize_t queue_write_same_max_show(struct request_queue *q, char *page)
  148. {
  149. return sprintf(page, "%llu\n",
  150. (unsigned long long)q->limits.max_write_same_sectors << 9);
  151. }
  152. static ssize_t
  153. queue_max_sectors_store(struct request_queue *q, const char *page, size_t count)
  154. {
  155. unsigned long max_sectors_kb,
  156. max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1,
  157. page_kb = 1 << (PAGE_CACHE_SHIFT - 10);
  158. ssize_t ret = queue_var_store(&max_sectors_kb, page, count);
  159. if (ret < 0)
  160. return ret;
  161. max_hw_sectors_kb = min_not_zero(max_hw_sectors_kb, (unsigned long)
  162. q->limits.max_dev_sectors >> 1);
  163. if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb)
  164. return -EINVAL;
  165. spin_lock_irq(q->queue_lock);
  166. q->limits.max_sectors = max_sectors_kb << 1;
  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 struct queue_sysfs_entry queue_requests_entry = {
  281. .attr = {.name = "nr_requests", .mode = S_IRUGO | S_IWUSR },
  282. .show = queue_requests_show,
  283. .store = queue_requests_store,
  284. };
  285. static struct queue_sysfs_entry queue_ra_entry = {
  286. .attr = {.name = "read_ahead_kb", .mode = S_IRUGO | S_IWUSR },
  287. .show = queue_ra_show,
  288. .store = queue_ra_store,
  289. };
  290. static struct queue_sysfs_entry queue_max_sectors_entry = {
  291. .attr = {.name = "max_sectors_kb", .mode = S_IRUGO | S_IWUSR },
  292. .show = queue_max_sectors_show,
  293. .store = queue_max_sectors_store,
  294. };
  295. static struct queue_sysfs_entry queue_max_hw_sectors_entry = {
  296. .attr = {.name = "max_hw_sectors_kb", .mode = S_IRUGO },
  297. .show = queue_max_hw_sectors_show,
  298. };
  299. static struct queue_sysfs_entry queue_max_segments_entry = {
  300. .attr = {.name = "max_segments", .mode = S_IRUGO },
  301. .show = queue_max_segments_show,
  302. };
  303. static struct queue_sysfs_entry queue_max_integrity_segments_entry = {
  304. .attr = {.name = "max_integrity_segments", .mode = S_IRUGO },
  305. .show = queue_max_integrity_segments_show,
  306. };
  307. static struct queue_sysfs_entry queue_max_segment_size_entry = {
  308. .attr = {.name = "max_segment_size", .mode = S_IRUGO },
  309. .show = queue_max_segment_size_show,
  310. };
  311. static struct queue_sysfs_entry queue_iosched_entry = {
  312. .attr = {.name = "scheduler", .mode = S_IRUGO | S_IWUSR },
  313. .show = elv_iosched_show,
  314. .store = elv_iosched_store,
  315. };
  316. static struct queue_sysfs_entry queue_hw_sector_size_entry = {
  317. .attr = {.name = "hw_sector_size", .mode = S_IRUGO },
  318. .show = queue_logical_block_size_show,
  319. };
  320. static struct queue_sysfs_entry queue_logical_block_size_entry = {
  321. .attr = {.name = "logical_block_size", .mode = S_IRUGO },
  322. .show = queue_logical_block_size_show,
  323. };
  324. static struct queue_sysfs_entry queue_physical_block_size_entry = {
  325. .attr = {.name = "physical_block_size", .mode = S_IRUGO },
  326. .show = queue_physical_block_size_show,
  327. };
  328. static struct queue_sysfs_entry queue_io_min_entry = {
  329. .attr = {.name = "minimum_io_size", .mode = S_IRUGO },
  330. .show = queue_io_min_show,
  331. };
  332. static struct queue_sysfs_entry queue_io_opt_entry = {
  333. .attr = {.name = "optimal_io_size", .mode = S_IRUGO },
  334. .show = queue_io_opt_show,
  335. };
  336. static struct queue_sysfs_entry queue_discard_granularity_entry = {
  337. .attr = {.name = "discard_granularity", .mode = S_IRUGO },
  338. .show = queue_discard_granularity_show,
  339. };
  340. static struct queue_sysfs_entry queue_discard_max_hw_entry = {
  341. .attr = {.name = "discard_max_hw_bytes", .mode = S_IRUGO },
  342. .show = queue_discard_max_hw_show,
  343. };
  344. static struct queue_sysfs_entry queue_discard_max_entry = {
  345. .attr = {.name = "discard_max_bytes", .mode = S_IRUGO | S_IWUSR },
  346. .show = queue_discard_max_show,
  347. .store = queue_discard_max_store,
  348. };
  349. static struct queue_sysfs_entry queue_discard_zeroes_data_entry = {
  350. .attr = {.name = "discard_zeroes_data", .mode = S_IRUGO },
  351. .show = queue_discard_zeroes_data_show,
  352. };
  353. static struct queue_sysfs_entry queue_write_same_max_entry = {
  354. .attr = {.name = "write_same_max_bytes", .mode = S_IRUGO },
  355. .show = queue_write_same_max_show,
  356. };
  357. static struct queue_sysfs_entry queue_nonrot_entry = {
  358. .attr = {.name = "rotational", .mode = S_IRUGO | S_IWUSR },
  359. .show = queue_show_nonrot,
  360. .store = queue_store_nonrot,
  361. };
  362. static struct queue_sysfs_entry queue_nomerges_entry = {
  363. .attr = {.name = "nomerges", .mode = S_IRUGO | S_IWUSR },
  364. .show = queue_nomerges_show,
  365. .store = queue_nomerges_store,
  366. };
  367. static struct queue_sysfs_entry queue_rq_affinity_entry = {
  368. .attr = {.name = "rq_affinity", .mode = S_IRUGO | S_IWUSR },
  369. .show = queue_rq_affinity_show,
  370. .store = queue_rq_affinity_store,
  371. };
  372. static struct queue_sysfs_entry queue_iostats_entry = {
  373. .attr = {.name = "iostats", .mode = S_IRUGO | S_IWUSR },
  374. .show = queue_show_iostats,
  375. .store = queue_store_iostats,
  376. };
  377. static struct queue_sysfs_entry queue_random_entry = {
  378. .attr = {.name = "add_random", .mode = S_IRUGO | S_IWUSR },
  379. .show = queue_show_random,
  380. .store = queue_store_random,
  381. };
  382. static struct queue_sysfs_entry queue_poll_entry = {
  383. .attr = {.name = "io_poll", .mode = S_IRUGO | S_IWUSR },
  384. .show = queue_poll_show,
  385. .store = queue_poll_store,
  386. };
  387. static struct attribute *default_attrs[] = {
  388. &queue_requests_entry.attr,
  389. &queue_ra_entry.attr,
  390. &queue_max_hw_sectors_entry.attr,
  391. &queue_max_sectors_entry.attr,
  392. &queue_max_segments_entry.attr,
  393. &queue_max_integrity_segments_entry.attr,
  394. &queue_max_segment_size_entry.attr,
  395. &queue_iosched_entry.attr,
  396. &queue_hw_sector_size_entry.attr,
  397. &queue_logical_block_size_entry.attr,
  398. &queue_physical_block_size_entry.attr,
  399. &queue_io_min_entry.attr,
  400. &queue_io_opt_entry.attr,
  401. &queue_discard_granularity_entry.attr,
  402. &queue_discard_max_entry.attr,
  403. &queue_discard_max_hw_entry.attr,
  404. &queue_discard_zeroes_data_entry.attr,
  405. &queue_write_same_max_entry.attr,
  406. &queue_nonrot_entry.attr,
  407. &queue_nomerges_entry.attr,
  408. &queue_rq_affinity_entry.attr,
  409. &queue_iostats_entry.attr,
  410. &queue_random_entry.attr,
  411. &queue_poll_entry.attr,
  412. NULL,
  413. };
  414. #define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
  415. static ssize_t
  416. queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
  417. {
  418. struct queue_sysfs_entry *entry = to_queue(attr);
  419. struct request_queue *q =
  420. container_of(kobj, struct request_queue, kobj);
  421. ssize_t res;
  422. if (!entry->show)
  423. return -EIO;
  424. mutex_lock(&q->sysfs_lock);
  425. if (blk_queue_dying(q)) {
  426. mutex_unlock(&q->sysfs_lock);
  427. return -ENOENT;
  428. }
  429. res = entry->show(q, page);
  430. mutex_unlock(&q->sysfs_lock);
  431. return res;
  432. }
  433. static ssize_t
  434. queue_attr_store(struct kobject *kobj, struct attribute *attr,
  435. const char *page, size_t length)
  436. {
  437. struct queue_sysfs_entry *entry = to_queue(attr);
  438. struct request_queue *q;
  439. ssize_t res;
  440. if (!entry->store)
  441. return -EIO;
  442. q = container_of(kobj, struct request_queue, kobj);
  443. mutex_lock(&q->sysfs_lock);
  444. if (blk_queue_dying(q)) {
  445. mutex_unlock(&q->sysfs_lock);
  446. return -ENOENT;
  447. }
  448. res = entry->store(q, page, length);
  449. mutex_unlock(&q->sysfs_lock);
  450. return res;
  451. }
  452. static void blk_free_queue_rcu(struct rcu_head *rcu_head)
  453. {
  454. struct request_queue *q = container_of(rcu_head, struct request_queue,
  455. rcu_head);
  456. kmem_cache_free(blk_requestq_cachep, q);
  457. }
  458. /**
  459. * blk_release_queue: - release a &struct request_queue when it is no longer needed
  460. * @kobj: the kobj belonging to the request queue to be released
  461. *
  462. * Description:
  463. * blk_release_queue is the pair to blk_init_queue() or
  464. * blk_queue_make_request(). It should be called when a request queue is
  465. * being released; typically when a block device is being de-registered.
  466. * Currently, its primary task it to free all the &struct request
  467. * structures that were allocated to the queue and the queue itself.
  468. *
  469. * Note:
  470. * The low level driver must have finished any outstanding requests first
  471. * via blk_cleanup_queue().
  472. **/
  473. static void blk_release_queue(struct kobject *kobj)
  474. {
  475. struct request_queue *q =
  476. container_of(kobj, struct request_queue, kobj);
  477. bdi_exit(&q->backing_dev_info);
  478. blkcg_exit_queue(q);
  479. if (q->elevator) {
  480. spin_lock_irq(q->queue_lock);
  481. ioc_clear_queue(q);
  482. spin_unlock_irq(q->queue_lock);
  483. elevator_exit(q->elevator);
  484. }
  485. blk_exit_rl(&q->root_rl);
  486. if (q->queue_tags)
  487. __blk_queue_free_tags(q);
  488. if (!q->mq_ops)
  489. blk_free_flush_queue(q->fq);
  490. else
  491. blk_mq_release(q);
  492. blk_trace_shutdown(q);
  493. if (q->bio_split)
  494. bioset_free(q->bio_split);
  495. ida_simple_remove(&blk_queue_ida, q->id);
  496. call_rcu(&q->rcu_head, blk_free_queue_rcu);
  497. }
  498. static const struct sysfs_ops queue_sysfs_ops = {
  499. .show = queue_attr_show,
  500. .store = queue_attr_store,
  501. };
  502. struct kobj_type blk_queue_ktype = {
  503. .sysfs_ops = &queue_sysfs_ops,
  504. .default_attrs = default_attrs,
  505. .release = blk_release_queue,
  506. };
  507. int blk_register_queue(struct gendisk *disk)
  508. {
  509. int ret;
  510. struct device *dev = disk_to_dev(disk);
  511. struct request_queue *q = disk->queue;
  512. if (WARN_ON(!q))
  513. return -ENXIO;
  514. /*
  515. * SCSI probing may synchronously create and destroy a lot of
  516. * request_queues for non-existent devices. Shutting down a fully
  517. * functional queue takes measureable wallclock time as RCU grace
  518. * periods are involved. To avoid excessive latency in these
  519. * cases, a request_queue starts out in a degraded mode which is
  520. * faster to shut down and is made fully functional here as
  521. * request_queues for non-existent devices never get registered.
  522. */
  523. if (!blk_queue_init_done(q)) {
  524. queue_flag_set_unlocked(QUEUE_FLAG_INIT_DONE, q);
  525. percpu_ref_switch_to_percpu(&q->q_usage_counter);
  526. blk_queue_bypass_end(q);
  527. }
  528. ret = blk_trace_init_sysfs(dev);
  529. if (ret)
  530. return ret;
  531. ret = kobject_add(&q->kobj, kobject_get(&dev->kobj), "%s", "queue");
  532. if (ret < 0) {
  533. blk_trace_remove_sysfs(dev);
  534. return ret;
  535. }
  536. kobject_uevent(&q->kobj, KOBJ_ADD);
  537. if (q->mq_ops)
  538. blk_mq_register_disk(disk);
  539. if (!q->request_fn)
  540. return 0;
  541. ret = elv_register_queue(q);
  542. if (ret) {
  543. kobject_uevent(&q->kobj, KOBJ_REMOVE);
  544. kobject_del(&q->kobj);
  545. blk_trace_remove_sysfs(dev);
  546. kobject_put(&dev->kobj);
  547. return ret;
  548. }
  549. return 0;
  550. }
  551. void blk_unregister_queue(struct gendisk *disk)
  552. {
  553. struct request_queue *q = disk->queue;
  554. if (WARN_ON(!q))
  555. return;
  556. if (q->mq_ops)
  557. blk_mq_unregister_disk(disk);
  558. if (q->request_fn)
  559. elv_unregister_queue(q);
  560. kobject_uevent(&q->kobj, KOBJ_REMOVE);
  561. kobject_del(&q->kobj);
  562. blk_trace_remove_sysfs(disk_to_dev(disk));
  563. kobject_put(&disk_to_dev(disk)->kobj);
  564. }