blk-sysfs.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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_show(struct request_queue *q, char *page)
  116. {
  117. return sprintf(page, "%llu\n",
  118. (unsigned long long)q->limits.max_discard_sectors << 9);
  119. }
  120. static ssize_t queue_discard_zeroes_data_show(struct request_queue *q, char *page)
  121. {
  122. return queue_var_show(queue_discard_zeroes_data(q), page);
  123. }
  124. static ssize_t queue_write_same_max_show(struct request_queue *q, char *page)
  125. {
  126. return sprintf(page, "%llu\n",
  127. (unsigned long long)q->limits.max_write_same_sectors << 9);
  128. }
  129. static ssize_t
  130. queue_max_sectors_store(struct request_queue *q, const char *page, size_t count)
  131. {
  132. unsigned long max_sectors_kb,
  133. max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1,
  134. page_kb = 1 << (PAGE_CACHE_SHIFT - 10);
  135. ssize_t ret = queue_var_store(&max_sectors_kb, page, count);
  136. if (ret < 0)
  137. return ret;
  138. if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb)
  139. return -EINVAL;
  140. spin_lock_irq(q->queue_lock);
  141. q->limits.max_sectors = max_sectors_kb << 1;
  142. spin_unlock_irq(q->queue_lock);
  143. return ret;
  144. }
  145. static ssize_t queue_max_hw_sectors_show(struct request_queue *q, char *page)
  146. {
  147. int max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1;
  148. return queue_var_show(max_hw_sectors_kb, (page));
  149. }
  150. #define QUEUE_SYSFS_BIT_FNS(name, flag, neg) \
  151. static ssize_t \
  152. queue_show_##name(struct request_queue *q, char *page) \
  153. { \
  154. int bit; \
  155. bit = test_bit(QUEUE_FLAG_##flag, &q->queue_flags); \
  156. return queue_var_show(neg ? !bit : bit, page); \
  157. } \
  158. static ssize_t \
  159. queue_store_##name(struct request_queue *q, const char *page, size_t count) \
  160. { \
  161. unsigned long val; \
  162. ssize_t ret; \
  163. ret = queue_var_store(&val, page, count); \
  164. if (ret < 0) \
  165. return ret; \
  166. if (neg) \
  167. val = !val; \
  168. \
  169. spin_lock_irq(q->queue_lock); \
  170. if (val) \
  171. queue_flag_set(QUEUE_FLAG_##flag, q); \
  172. else \
  173. queue_flag_clear(QUEUE_FLAG_##flag, q); \
  174. spin_unlock_irq(q->queue_lock); \
  175. return ret; \
  176. }
  177. QUEUE_SYSFS_BIT_FNS(nonrot, NONROT, 1);
  178. QUEUE_SYSFS_BIT_FNS(random, ADD_RANDOM, 0);
  179. QUEUE_SYSFS_BIT_FNS(iostats, IO_STAT, 0);
  180. #undef QUEUE_SYSFS_BIT_FNS
  181. static ssize_t queue_nomerges_show(struct request_queue *q, char *page)
  182. {
  183. return queue_var_show((blk_queue_nomerges(q) << 1) |
  184. blk_queue_noxmerges(q), page);
  185. }
  186. static ssize_t queue_nomerges_store(struct request_queue *q, const char *page,
  187. size_t count)
  188. {
  189. unsigned long nm;
  190. ssize_t ret = queue_var_store(&nm, page, count);
  191. if (ret < 0)
  192. return ret;
  193. spin_lock_irq(q->queue_lock);
  194. queue_flag_clear(QUEUE_FLAG_NOMERGES, q);
  195. queue_flag_clear(QUEUE_FLAG_NOXMERGES, q);
  196. if (nm == 2)
  197. queue_flag_set(QUEUE_FLAG_NOMERGES, q);
  198. else if (nm)
  199. queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
  200. spin_unlock_irq(q->queue_lock);
  201. return ret;
  202. }
  203. static ssize_t queue_rq_affinity_show(struct request_queue *q, char *page)
  204. {
  205. bool set = test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags);
  206. bool force = test_bit(QUEUE_FLAG_SAME_FORCE, &q->queue_flags);
  207. return queue_var_show(set << force, page);
  208. }
  209. static ssize_t
  210. queue_rq_affinity_store(struct request_queue *q, const char *page, size_t count)
  211. {
  212. ssize_t ret = -EINVAL;
  213. #ifdef CONFIG_SMP
  214. unsigned long val;
  215. ret = queue_var_store(&val, page, count);
  216. if (ret < 0)
  217. return ret;
  218. spin_lock_irq(q->queue_lock);
  219. if (val == 2) {
  220. queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
  221. queue_flag_set(QUEUE_FLAG_SAME_FORCE, q);
  222. } else if (val == 1) {
  223. queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
  224. queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
  225. } else if (val == 0) {
  226. queue_flag_clear(QUEUE_FLAG_SAME_COMP, q);
  227. queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
  228. }
  229. spin_unlock_irq(q->queue_lock);
  230. #endif
  231. return ret;
  232. }
  233. static struct queue_sysfs_entry queue_requests_entry = {
  234. .attr = {.name = "nr_requests", .mode = S_IRUGO | S_IWUSR },
  235. .show = queue_requests_show,
  236. .store = queue_requests_store,
  237. };
  238. static struct queue_sysfs_entry queue_ra_entry = {
  239. .attr = {.name = "read_ahead_kb", .mode = S_IRUGO | S_IWUSR },
  240. .show = queue_ra_show,
  241. .store = queue_ra_store,
  242. };
  243. static struct queue_sysfs_entry queue_max_sectors_entry = {
  244. .attr = {.name = "max_sectors_kb", .mode = S_IRUGO | S_IWUSR },
  245. .show = queue_max_sectors_show,
  246. .store = queue_max_sectors_store,
  247. };
  248. static struct queue_sysfs_entry queue_max_hw_sectors_entry = {
  249. .attr = {.name = "max_hw_sectors_kb", .mode = S_IRUGO },
  250. .show = queue_max_hw_sectors_show,
  251. };
  252. static struct queue_sysfs_entry queue_max_segments_entry = {
  253. .attr = {.name = "max_segments", .mode = S_IRUGO },
  254. .show = queue_max_segments_show,
  255. };
  256. static struct queue_sysfs_entry queue_max_integrity_segments_entry = {
  257. .attr = {.name = "max_integrity_segments", .mode = S_IRUGO },
  258. .show = queue_max_integrity_segments_show,
  259. };
  260. static struct queue_sysfs_entry queue_max_segment_size_entry = {
  261. .attr = {.name = "max_segment_size", .mode = S_IRUGO },
  262. .show = queue_max_segment_size_show,
  263. };
  264. static struct queue_sysfs_entry queue_iosched_entry = {
  265. .attr = {.name = "scheduler", .mode = S_IRUGO | S_IWUSR },
  266. .show = elv_iosched_show,
  267. .store = elv_iosched_store,
  268. };
  269. static struct queue_sysfs_entry queue_hw_sector_size_entry = {
  270. .attr = {.name = "hw_sector_size", .mode = S_IRUGO },
  271. .show = queue_logical_block_size_show,
  272. };
  273. static struct queue_sysfs_entry queue_logical_block_size_entry = {
  274. .attr = {.name = "logical_block_size", .mode = S_IRUGO },
  275. .show = queue_logical_block_size_show,
  276. };
  277. static struct queue_sysfs_entry queue_physical_block_size_entry = {
  278. .attr = {.name = "physical_block_size", .mode = S_IRUGO },
  279. .show = queue_physical_block_size_show,
  280. };
  281. static struct queue_sysfs_entry queue_io_min_entry = {
  282. .attr = {.name = "minimum_io_size", .mode = S_IRUGO },
  283. .show = queue_io_min_show,
  284. };
  285. static struct queue_sysfs_entry queue_io_opt_entry = {
  286. .attr = {.name = "optimal_io_size", .mode = S_IRUGO },
  287. .show = queue_io_opt_show,
  288. };
  289. static struct queue_sysfs_entry queue_discard_granularity_entry = {
  290. .attr = {.name = "discard_granularity", .mode = S_IRUGO },
  291. .show = queue_discard_granularity_show,
  292. };
  293. static struct queue_sysfs_entry queue_discard_max_entry = {
  294. .attr = {.name = "discard_max_bytes", .mode = S_IRUGO },
  295. .show = queue_discard_max_show,
  296. };
  297. static struct queue_sysfs_entry queue_discard_zeroes_data_entry = {
  298. .attr = {.name = "discard_zeroes_data", .mode = S_IRUGO },
  299. .show = queue_discard_zeroes_data_show,
  300. };
  301. static struct queue_sysfs_entry queue_write_same_max_entry = {
  302. .attr = {.name = "write_same_max_bytes", .mode = S_IRUGO },
  303. .show = queue_write_same_max_show,
  304. };
  305. static struct queue_sysfs_entry queue_nonrot_entry = {
  306. .attr = {.name = "rotational", .mode = S_IRUGO | S_IWUSR },
  307. .show = queue_show_nonrot,
  308. .store = queue_store_nonrot,
  309. };
  310. static struct queue_sysfs_entry queue_nomerges_entry = {
  311. .attr = {.name = "nomerges", .mode = S_IRUGO | S_IWUSR },
  312. .show = queue_nomerges_show,
  313. .store = queue_nomerges_store,
  314. };
  315. static struct queue_sysfs_entry queue_rq_affinity_entry = {
  316. .attr = {.name = "rq_affinity", .mode = S_IRUGO | S_IWUSR },
  317. .show = queue_rq_affinity_show,
  318. .store = queue_rq_affinity_store,
  319. };
  320. static struct queue_sysfs_entry queue_iostats_entry = {
  321. .attr = {.name = "iostats", .mode = S_IRUGO | S_IWUSR },
  322. .show = queue_show_iostats,
  323. .store = queue_store_iostats,
  324. };
  325. static struct queue_sysfs_entry queue_random_entry = {
  326. .attr = {.name = "add_random", .mode = S_IRUGO | S_IWUSR },
  327. .show = queue_show_random,
  328. .store = queue_store_random,
  329. };
  330. static struct attribute *default_attrs[] = {
  331. &queue_requests_entry.attr,
  332. &queue_ra_entry.attr,
  333. &queue_max_hw_sectors_entry.attr,
  334. &queue_max_sectors_entry.attr,
  335. &queue_max_segments_entry.attr,
  336. &queue_max_integrity_segments_entry.attr,
  337. &queue_max_segment_size_entry.attr,
  338. &queue_iosched_entry.attr,
  339. &queue_hw_sector_size_entry.attr,
  340. &queue_logical_block_size_entry.attr,
  341. &queue_physical_block_size_entry.attr,
  342. &queue_io_min_entry.attr,
  343. &queue_io_opt_entry.attr,
  344. &queue_discard_granularity_entry.attr,
  345. &queue_discard_max_entry.attr,
  346. &queue_discard_zeroes_data_entry.attr,
  347. &queue_write_same_max_entry.attr,
  348. &queue_nonrot_entry.attr,
  349. &queue_nomerges_entry.attr,
  350. &queue_rq_affinity_entry.attr,
  351. &queue_iostats_entry.attr,
  352. &queue_random_entry.attr,
  353. NULL,
  354. };
  355. #define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
  356. static ssize_t
  357. queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
  358. {
  359. struct queue_sysfs_entry *entry = to_queue(attr);
  360. struct request_queue *q =
  361. container_of(kobj, struct request_queue, kobj);
  362. ssize_t res;
  363. if (!entry->show)
  364. return -EIO;
  365. mutex_lock(&q->sysfs_lock);
  366. if (blk_queue_dying(q)) {
  367. mutex_unlock(&q->sysfs_lock);
  368. return -ENOENT;
  369. }
  370. res = entry->show(q, page);
  371. mutex_unlock(&q->sysfs_lock);
  372. return res;
  373. }
  374. static ssize_t
  375. queue_attr_store(struct kobject *kobj, struct attribute *attr,
  376. const char *page, size_t length)
  377. {
  378. struct queue_sysfs_entry *entry = to_queue(attr);
  379. struct request_queue *q;
  380. ssize_t res;
  381. if (!entry->store)
  382. return -EIO;
  383. q = container_of(kobj, struct request_queue, kobj);
  384. mutex_lock(&q->sysfs_lock);
  385. if (blk_queue_dying(q)) {
  386. mutex_unlock(&q->sysfs_lock);
  387. return -ENOENT;
  388. }
  389. res = entry->store(q, page, length);
  390. mutex_unlock(&q->sysfs_lock);
  391. return res;
  392. }
  393. static void blk_free_queue_rcu(struct rcu_head *rcu_head)
  394. {
  395. struct request_queue *q = container_of(rcu_head, struct request_queue,
  396. rcu_head);
  397. kmem_cache_free(blk_requestq_cachep, q);
  398. }
  399. /**
  400. * blk_release_queue: - release a &struct request_queue when it is no longer needed
  401. * @kobj: the kobj belonging to the request queue to be released
  402. *
  403. * Description:
  404. * blk_release_queue is the pair to blk_init_queue() or
  405. * blk_queue_make_request(). It should be called when a request queue is
  406. * being released; typically when a block device is being de-registered.
  407. * Currently, its primary task it to free all the &struct request
  408. * structures that were allocated to the queue and the queue itself.
  409. *
  410. * Note:
  411. * The low level driver must have finished any outstanding requests first
  412. * via blk_cleanup_queue().
  413. **/
  414. static void blk_release_queue(struct kobject *kobj)
  415. {
  416. struct request_queue *q =
  417. container_of(kobj, struct request_queue, kobj);
  418. blkcg_exit_queue(q);
  419. if (q->elevator) {
  420. spin_lock_irq(q->queue_lock);
  421. ioc_clear_queue(q);
  422. spin_unlock_irq(q->queue_lock);
  423. elevator_exit(q->elevator);
  424. }
  425. blk_exit_rl(&q->root_rl);
  426. if (q->queue_tags)
  427. __blk_queue_free_tags(q);
  428. if (!q->mq_ops)
  429. blk_free_flush_queue(q->fq);
  430. else
  431. blk_mq_release(q);
  432. blk_trace_shutdown(q);
  433. ida_simple_remove(&blk_queue_ida, q->id);
  434. call_rcu(&q->rcu_head, blk_free_queue_rcu);
  435. }
  436. static const struct sysfs_ops queue_sysfs_ops = {
  437. .show = queue_attr_show,
  438. .store = queue_attr_store,
  439. };
  440. struct kobj_type blk_queue_ktype = {
  441. .sysfs_ops = &queue_sysfs_ops,
  442. .default_attrs = default_attrs,
  443. .release = blk_release_queue,
  444. };
  445. int blk_register_queue(struct gendisk *disk)
  446. {
  447. int ret;
  448. struct device *dev = disk_to_dev(disk);
  449. struct request_queue *q = disk->queue;
  450. if (WARN_ON(!q))
  451. return -ENXIO;
  452. /*
  453. * SCSI probing may synchronously create and destroy a lot of
  454. * request_queues for non-existent devices. Shutting down a fully
  455. * functional queue takes measureable wallclock time as RCU grace
  456. * periods are involved. To avoid excessive latency in these
  457. * cases, a request_queue starts out in a degraded mode which is
  458. * faster to shut down and is made fully functional here as
  459. * request_queues for non-existent devices never get registered.
  460. */
  461. if (!blk_queue_init_done(q)) {
  462. queue_flag_set_unlocked(QUEUE_FLAG_INIT_DONE, q);
  463. blk_queue_bypass_end(q);
  464. if (q->mq_ops)
  465. blk_mq_finish_init(q);
  466. }
  467. ret = blk_trace_init_sysfs(dev);
  468. if (ret)
  469. return ret;
  470. ret = kobject_add(&q->kobj, kobject_get(&dev->kobj), "%s", "queue");
  471. if (ret < 0) {
  472. blk_trace_remove_sysfs(dev);
  473. return ret;
  474. }
  475. kobject_uevent(&q->kobj, KOBJ_ADD);
  476. if (q->mq_ops)
  477. blk_mq_register_disk(disk);
  478. if (!q->request_fn)
  479. return 0;
  480. ret = elv_register_queue(q);
  481. if (ret) {
  482. kobject_uevent(&q->kobj, KOBJ_REMOVE);
  483. kobject_del(&q->kobj);
  484. blk_trace_remove_sysfs(dev);
  485. kobject_put(&dev->kobj);
  486. return ret;
  487. }
  488. return 0;
  489. }
  490. void blk_unregister_queue(struct gendisk *disk)
  491. {
  492. struct request_queue *q = disk->queue;
  493. if (WARN_ON(!q))
  494. return;
  495. if (q->mq_ops)
  496. blk_mq_unregister_disk(disk);
  497. if (q->request_fn)
  498. elv_unregister_queue(q);
  499. kobject_uevent(&q->kobj, KOBJ_REMOVE);
  500. kobject_del(&q->kobj);
  501. blk_trace_remove_sysfs(disk_to_dev(disk));
  502. kobject_put(&disk_to_dev(disk)->kobj);
  503. }