blk-sysfs.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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/blktrace_api.h>
  10. #include "blk.h"
  11. #include "blk-cgroup.h"
  12. struct queue_sysfs_entry {
  13. struct attribute attr;
  14. ssize_t (*show)(struct request_queue *, char *);
  15. ssize_t (*store)(struct request_queue *, const char *, size_t);
  16. };
  17. static ssize_t
  18. queue_var_show(unsigned long var, char *page)
  19. {
  20. return sprintf(page, "%lu\n", var);
  21. }
  22. static ssize_t
  23. queue_var_store(unsigned long *var, const char *page, size_t count)
  24. {
  25. int err;
  26. unsigned long v;
  27. err = strict_strtoul(page, 10, &v);
  28. if (err || v > UINT_MAX)
  29. return -EINVAL;
  30. *var = v;
  31. return count;
  32. }
  33. static ssize_t queue_requests_show(struct request_queue *q, char *page)
  34. {
  35. return queue_var_show(q->nr_requests, (page));
  36. }
  37. static ssize_t
  38. queue_requests_store(struct request_queue *q, const char *page, size_t count)
  39. {
  40. struct request_list *rl;
  41. unsigned long nr;
  42. int ret;
  43. if (!q->request_fn)
  44. return -EINVAL;
  45. ret = queue_var_store(&nr, page, count);
  46. if (ret < 0)
  47. return ret;
  48. if (nr < BLKDEV_MIN_RQ)
  49. nr = BLKDEV_MIN_RQ;
  50. spin_lock_irq(q->queue_lock);
  51. q->nr_requests = nr;
  52. blk_queue_congestion_threshold(q);
  53. /* congestion isn't cgroup aware and follows root blkcg for now */
  54. rl = &q->root_rl;
  55. if (rl->count[BLK_RW_SYNC] >= queue_congestion_on_threshold(q))
  56. blk_set_queue_congested(q, BLK_RW_SYNC);
  57. else if (rl->count[BLK_RW_SYNC] < queue_congestion_off_threshold(q))
  58. blk_clear_queue_congested(q, BLK_RW_SYNC);
  59. if (rl->count[BLK_RW_ASYNC] >= queue_congestion_on_threshold(q))
  60. blk_set_queue_congested(q, BLK_RW_ASYNC);
  61. else if (rl->count[BLK_RW_ASYNC] < queue_congestion_off_threshold(q))
  62. blk_clear_queue_congested(q, BLK_RW_ASYNC);
  63. blk_queue_for_each_rl(rl, q) {
  64. if (rl->count[BLK_RW_SYNC] >= q->nr_requests) {
  65. blk_set_rl_full(rl, BLK_RW_SYNC);
  66. } else {
  67. blk_clear_rl_full(rl, BLK_RW_SYNC);
  68. wake_up(&rl->wait[BLK_RW_SYNC]);
  69. }
  70. if (rl->count[BLK_RW_ASYNC] >= q->nr_requests) {
  71. blk_set_rl_full(rl, BLK_RW_ASYNC);
  72. } else {
  73. blk_clear_rl_full(rl, BLK_RW_ASYNC);
  74. wake_up(&rl->wait[BLK_RW_ASYNC]);
  75. }
  76. }
  77. spin_unlock_irq(q->queue_lock);
  78. return ret;
  79. }
  80. static ssize_t queue_ra_show(struct request_queue *q, char *page)
  81. {
  82. unsigned long ra_kb = q->backing_dev_info.ra_pages <<
  83. (PAGE_CACHE_SHIFT - 10);
  84. return queue_var_show(ra_kb, (page));
  85. }
  86. static ssize_t
  87. queue_ra_store(struct request_queue *q, const char *page, size_t count)
  88. {
  89. unsigned long ra_kb;
  90. ssize_t ret = queue_var_store(&ra_kb, page, count);
  91. if (ret < 0)
  92. return ret;
  93. q->backing_dev_info.ra_pages = ra_kb >> (PAGE_CACHE_SHIFT - 10);
  94. return ret;
  95. }
  96. static ssize_t queue_max_sectors_show(struct request_queue *q, char *page)
  97. {
  98. int max_sectors_kb = queue_max_sectors(q) >> 1;
  99. return queue_var_show(max_sectors_kb, (page));
  100. }
  101. static ssize_t queue_max_segments_show(struct request_queue *q, char *page)
  102. {
  103. return queue_var_show(queue_max_segments(q), (page));
  104. }
  105. static ssize_t queue_max_integrity_segments_show(struct request_queue *q, char *page)
  106. {
  107. return queue_var_show(q->limits.max_integrity_segments, (page));
  108. }
  109. static ssize_t queue_max_segment_size_show(struct request_queue *q, char *page)
  110. {
  111. if (blk_queue_cluster(q))
  112. return queue_var_show(queue_max_segment_size(q), (page));
  113. return queue_var_show(PAGE_CACHE_SIZE, (page));
  114. }
  115. static ssize_t queue_logical_block_size_show(struct request_queue *q, char *page)
  116. {
  117. return queue_var_show(queue_logical_block_size(q), page);
  118. }
  119. static ssize_t queue_physical_block_size_show(struct request_queue *q, char *page)
  120. {
  121. return queue_var_show(queue_physical_block_size(q), page);
  122. }
  123. static ssize_t queue_io_min_show(struct request_queue *q, char *page)
  124. {
  125. return queue_var_show(queue_io_min(q), page);
  126. }
  127. static ssize_t queue_io_opt_show(struct request_queue *q, char *page)
  128. {
  129. return queue_var_show(queue_io_opt(q), page);
  130. }
  131. static ssize_t queue_discard_granularity_show(struct request_queue *q, char *page)
  132. {
  133. return queue_var_show(q->limits.discard_granularity, page);
  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_zeroes_data_show(struct request_queue *q, char *page)
  141. {
  142. return queue_var_show(queue_discard_zeroes_data(q), page);
  143. }
  144. static ssize_t queue_write_same_max_show(struct request_queue *q, char *page)
  145. {
  146. return sprintf(page, "%llu\n",
  147. (unsigned long long)q->limits.max_write_same_sectors << 9);
  148. }
  149. static ssize_t
  150. queue_max_sectors_store(struct request_queue *q, const char *page, size_t count)
  151. {
  152. unsigned long max_sectors_kb,
  153. max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1,
  154. page_kb = 1 << (PAGE_CACHE_SHIFT - 10);
  155. ssize_t ret = queue_var_store(&max_sectors_kb, page, count);
  156. if (ret < 0)
  157. return ret;
  158. if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb)
  159. return -EINVAL;
  160. spin_lock_irq(q->queue_lock);
  161. q->limits.max_sectors = max_sectors_kb << 1;
  162. spin_unlock_irq(q->queue_lock);
  163. return ret;
  164. }
  165. static ssize_t queue_max_hw_sectors_show(struct request_queue *q, char *page)
  166. {
  167. int max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1;
  168. return queue_var_show(max_hw_sectors_kb, (page));
  169. }
  170. #define QUEUE_SYSFS_BIT_FNS(name, flag, neg) \
  171. static ssize_t \
  172. queue_show_##name(struct request_queue *q, char *page) \
  173. { \
  174. int bit; \
  175. bit = test_bit(QUEUE_FLAG_##flag, &q->queue_flags); \
  176. return queue_var_show(neg ? !bit : bit, page); \
  177. } \
  178. static ssize_t \
  179. queue_store_##name(struct request_queue *q, const char *page, size_t count) \
  180. { \
  181. unsigned long val; \
  182. ssize_t ret; \
  183. ret = queue_var_store(&val, page, count); \
  184. if (neg) \
  185. val = !val; \
  186. \
  187. spin_lock_irq(q->queue_lock); \
  188. if (val) \
  189. queue_flag_set(QUEUE_FLAG_##flag, q); \
  190. else \
  191. queue_flag_clear(QUEUE_FLAG_##flag, q); \
  192. spin_unlock_irq(q->queue_lock); \
  193. return ret; \
  194. }
  195. QUEUE_SYSFS_BIT_FNS(nonrot, NONROT, 1);
  196. QUEUE_SYSFS_BIT_FNS(random, ADD_RANDOM, 0);
  197. QUEUE_SYSFS_BIT_FNS(iostats, IO_STAT, 0);
  198. #undef QUEUE_SYSFS_BIT_FNS
  199. static ssize_t queue_nomerges_show(struct request_queue *q, char *page)
  200. {
  201. return queue_var_show((blk_queue_nomerges(q) << 1) |
  202. blk_queue_noxmerges(q), page);
  203. }
  204. static ssize_t queue_nomerges_store(struct request_queue *q, const char *page,
  205. size_t count)
  206. {
  207. unsigned long nm;
  208. ssize_t ret = queue_var_store(&nm, page, count);
  209. if (ret < 0)
  210. return ret;
  211. spin_lock_irq(q->queue_lock);
  212. queue_flag_clear(QUEUE_FLAG_NOMERGES, q);
  213. queue_flag_clear(QUEUE_FLAG_NOXMERGES, q);
  214. if (nm == 2)
  215. queue_flag_set(QUEUE_FLAG_NOMERGES, q);
  216. else if (nm)
  217. queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
  218. spin_unlock_irq(q->queue_lock);
  219. return ret;
  220. }
  221. static ssize_t queue_rq_affinity_show(struct request_queue *q, char *page)
  222. {
  223. bool set = test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags);
  224. bool force = test_bit(QUEUE_FLAG_SAME_FORCE, &q->queue_flags);
  225. return queue_var_show(set << force, page);
  226. }
  227. static ssize_t
  228. queue_rq_affinity_store(struct request_queue *q, const char *page, size_t count)
  229. {
  230. ssize_t ret = -EINVAL;
  231. #if defined(CONFIG_USE_GENERIC_SMP_HELPERS)
  232. unsigned long val;
  233. ret = queue_var_store(&val, page, count);
  234. if (ret < 0)
  235. return ret;
  236. spin_lock_irq(q->queue_lock);
  237. if (val == 2) {
  238. queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
  239. queue_flag_set(QUEUE_FLAG_SAME_FORCE, q);
  240. } else if (val == 1) {
  241. queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
  242. queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
  243. } else if (val == 0) {
  244. queue_flag_clear(QUEUE_FLAG_SAME_COMP, q);
  245. queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
  246. }
  247. spin_unlock_irq(q->queue_lock);
  248. #endif
  249. return ret;
  250. }
  251. static struct queue_sysfs_entry queue_requests_entry = {
  252. .attr = {.name = "nr_requests", .mode = S_IRUGO | S_IWUSR },
  253. .show = queue_requests_show,
  254. .store = queue_requests_store,
  255. };
  256. static struct queue_sysfs_entry queue_ra_entry = {
  257. .attr = {.name = "read_ahead_kb", .mode = S_IRUGO | S_IWUSR },
  258. .show = queue_ra_show,
  259. .store = queue_ra_store,
  260. };
  261. static struct queue_sysfs_entry queue_max_sectors_entry = {
  262. .attr = {.name = "max_sectors_kb", .mode = S_IRUGO | S_IWUSR },
  263. .show = queue_max_sectors_show,
  264. .store = queue_max_sectors_store,
  265. };
  266. static struct queue_sysfs_entry queue_max_hw_sectors_entry = {
  267. .attr = {.name = "max_hw_sectors_kb", .mode = S_IRUGO },
  268. .show = queue_max_hw_sectors_show,
  269. };
  270. static struct queue_sysfs_entry queue_max_segments_entry = {
  271. .attr = {.name = "max_segments", .mode = S_IRUGO },
  272. .show = queue_max_segments_show,
  273. };
  274. static struct queue_sysfs_entry queue_max_integrity_segments_entry = {
  275. .attr = {.name = "max_integrity_segments", .mode = S_IRUGO },
  276. .show = queue_max_integrity_segments_show,
  277. };
  278. static struct queue_sysfs_entry queue_max_segment_size_entry = {
  279. .attr = {.name = "max_segment_size", .mode = S_IRUGO },
  280. .show = queue_max_segment_size_show,
  281. };
  282. static struct queue_sysfs_entry queue_iosched_entry = {
  283. .attr = {.name = "scheduler", .mode = S_IRUGO | S_IWUSR },
  284. .show = elv_iosched_show,
  285. .store = elv_iosched_store,
  286. };
  287. static struct queue_sysfs_entry queue_hw_sector_size_entry = {
  288. .attr = {.name = "hw_sector_size", .mode = S_IRUGO },
  289. .show = queue_logical_block_size_show,
  290. };
  291. static struct queue_sysfs_entry queue_logical_block_size_entry = {
  292. .attr = {.name = "logical_block_size", .mode = S_IRUGO },
  293. .show = queue_logical_block_size_show,
  294. };
  295. static struct queue_sysfs_entry queue_physical_block_size_entry = {
  296. .attr = {.name = "physical_block_size", .mode = S_IRUGO },
  297. .show = queue_physical_block_size_show,
  298. };
  299. static struct queue_sysfs_entry queue_io_min_entry = {
  300. .attr = {.name = "minimum_io_size", .mode = S_IRUGO },
  301. .show = queue_io_min_show,
  302. };
  303. static struct queue_sysfs_entry queue_io_opt_entry = {
  304. .attr = {.name = "optimal_io_size", .mode = S_IRUGO },
  305. .show = queue_io_opt_show,
  306. };
  307. static struct queue_sysfs_entry queue_discard_granularity_entry = {
  308. .attr = {.name = "discard_granularity", .mode = S_IRUGO },
  309. .show = queue_discard_granularity_show,
  310. };
  311. static struct queue_sysfs_entry queue_discard_max_entry = {
  312. .attr = {.name = "discard_max_bytes", .mode = S_IRUGO },
  313. .show = queue_discard_max_show,
  314. };
  315. static struct queue_sysfs_entry queue_discard_zeroes_data_entry = {
  316. .attr = {.name = "discard_zeroes_data", .mode = S_IRUGO },
  317. .show = queue_discard_zeroes_data_show,
  318. };
  319. static struct queue_sysfs_entry queue_write_same_max_entry = {
  320. .attr = {.name = "write_same_max_bytes", .mode = S_IRUGO },
  321. .show = queue_write_same_max_show,
  322. };
  323. static struct queue_sysfs_entry queue_nonrot_entry = {
  324. .attr = {.name = "rotational", .mode = S_IRUGO | S_IWUSR },
  325. .show = queue_show_nonrot,
  326. .store = queue_store_nonrot,
  327. };
  328. static struct queue_sysfs_entry queue_nomerges_entry = {
  329. .attr = {.name = "nomerges", .mode = S_IRUGO | S_IWUSR },
  330. .show = queue_nomerges_show,
  331. .store = queue_nomerges_store,
  332. };
  333. static struct queue_sysfs_entry queue_rq_affinity_entry = {
  334. .attr = {.name = "rq_affinity", .mode = S_IRUGO | S_IWUSR },
  335. .show = queue_rq_affinity_show,
  336. .store = queue_rq_affinity_store,
  337. };
  338. static struct queue_sysfs_entry queue_iostats_entry = {
  339. .attr = {.name = "iostats", .mode = S_IRUGO | S_IWUSR },
  340. .show = queue_show_iostats,
  341. .store = queue_store_iostats,
  342. };
  343. static struct queue_sysfs_entry queue_random_entry = {
  344. .attr = {.name = "add_random", .mode = S_IRUGO | S_IWUSR },
  345. .show = queue_show_random,
  346. .store = queue_store_random,
  347. };
  348. static struct attribute *default_attrs[] = {
  349. &queue_requests_entry.attr,
  350. &queue_ra_entry.attr,
  351. &queue_max_hw_sectors_entry.attr,
  352. &queue_max_sectors_entry.attr,
  353. &queue_max_segments_entry.attr,
  354. &queue_max_integrity_segments_entry.attr,
  355. &queue_max_segment_size_entry.attr,
  356. &queue_iosched_entry.attr,
  357. &queue_hw_sector_size_entry.attr,
  358. &queue_logical_block_size_entry.attr,
  359. &queue_physical_block_size_entry.attr,
  360. &queue_io_min_entry.attr,
  361. &queue_io_opt_entry.attr,
  362. &queue_discard_granularity_entry.attr,
  363. &queue_discard_max_entry.attr,
  364. &queue_discard_zeroes_data_entry.attr,
  365. &queue_write_same_max_entry.attr,
  366. &queue_nonrot_entry.attr,
  367. &queue_nomerges_entry.attr,
  368. &queue_rq_affinity_entry.attr,
  369. &queue_iostats_entry.attr,
  370. &queue_random_entry.attr,
  371. NULL,
  372. };
  373. #define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
  374. static ssize_t
  375. queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
  376. {
  377. struct queue_sysfs_entry *entry = to_queue(attr);
  378. struct request_queue *q =
  379. container_of(kobj, struct request_queue, kobj);
  380. ssize_t res;
  381. if (!entry->show)
  382. return -EIO;
  383. mutex_lock(&q->sysfs_lock);
  384. if (blk_queue_dying(q)) {
  385. mutex_unlock(&q->sysfs_lock);
  386. return -ENOENT;
  387. }
  388. res = entry->show(q, page);
  389. mutex_unlock(&q->sysfs_lock);
  390. return res;
  391. }
  392. static ssize_t
  393. queue_attr_store(struct kobject *kobj, struct attribute *attr,
  394. const char *page, size_t length)
  395. {
  396. struct queue_sysfs_entry *entry = to_queue(attr);
  397. struct request_queue *q;
  398. ssize_t res;
  399. if (!entry->store)
  400. return -EIO;
  401. q = container_of(kobj, struct request_queue, kobj);
  402. mutex_lock(&q->sysfs_lock);
  403. if (blk_queue_dying(q)) {
  404. mutex_unlock(&q->sysfs_lock);
  405. return -ENOENT;
  406. }
  407. res = entry->store(q, page, length);
  408. mutex_unlock(&q->sysfs_lock);
  409. return res;
  410. }
  411. static void blk_free_queue_rcu(struct rcu_head *rcu_head)
  412. {
  413. struct request_queue *q = container_of(rcu_head, struct request_queue,
  414. rcu_head);
  415. kmem_cache_free(blk_requestq_cachep, q);
  416. }
  417. /**
  418. * blk_release_queue: - release a &struct request_queue when it is no longer needed
  419. * @kobj: the kobj belonging to the request queue to be released
  420. *
  421. * Description:
  422. * blk_release_queue is the pair to blk_init_queue() or
  423. * blk_queue_make_request(). It should be called when a request queue is
  424. * being released; typically when a block device is being de-registered.
  425. * Currently, its primary task it to free all the &struct request
  426. * structures that were allocated to the queue and the queue itself.
  427. *
  428. * Caveat:
  429. * Hopefully the low level driver will have finished any
  430. * outstanding requests first...
  431. **/
  432. static void blk_release_queue(struct kobject *kobj)
  433. {
  434. struct request_queue *q =
  435. container_of(kobj, struct request_queue, kobj);
  436. blk_sync_queue(q);
  437. blkcg_exit_queue(q);
  438. if (q->elevator) {
  439. spin_lock_irq(q->queue_lock);
  440. ioc_clear_queue(q);
  441. spin_unlock_irq(q->queue_lock);
  442. elevator_exit(q->elevator);
  443. }
  444. blk_exit_rl(&q->root_rl);
  445. if (q->queue_tags)
  446. __blk_queue_free_tags(q);
  447. blk_trace_shutdown(q);
  448. bdi_destroy(&q->backing_dev_info);
  449. ida_simple_remove(&blk_queue_ida, q->id);
  450. call_rcu(&q->rcu_head, blk_free_queue_rcu);
  451. }
  452. static const struct sysfs_ops queue_sysfs_ops = {
  453. .show = queue_attr_show,
  454. .store = queue_attr_store,
  455. };
  456. struct kobj_type blk_queue_ktype = {
  457. .sysfs_ops = &queue_sysfs_ops,
  458. .default_attrs = default_attrs,
  459. .release = blk_release_queue,
  460. };
  461. int blk_register_queue(struct gendisk *disk)
  462. {
  463. int ret;
  464. struct device *dev = disk_to_dev(disk);
  465. struct request_queue *q = disk->queue;
  466. if (WARN_ON(!q))
  467. return -ENXIO;
  468. /*
  469. * Initialization must be complete by now. Finish the initial
  470. * bypass from queue allocation.
  471. */
  472. blk_queue_bypass_end(q);
  473. ret = blk_trace_init_sysfs(dev);
  474. if (ret)
  475. return ret;
  476. ret = kobject_add(&q->kobj, kobject_get(&dev->kobj), "%s", "queue");
  477. if (ret < 0) {
  478. blk_trace_remove_sysfs(dev);
  479. return ret;
  480. }
  481. kobject_uevent(&q->kobj, KOBJ_ADD);
  482. if (!q->request_fn)
  483. return 0;
  484. ret = elv_register_queue(q);
  485. if (ret) {
  486. kobject_uevent(&q->kobj, KOBJ_REMOVE);
  487. kobject_del(&q->kobj);
  488. blk_trace_remove_sysfs(dev);
  489. kobject_put(&dev->kobj);
  490. return ret;
  491. }
  492. return 0;
  493. }
  494. void blk_unregister_queue(struct gendisk *disk)
  495. {
  496. struct request_queue *q = disk->queue;
  497. if (WARN_ON(!q))
  498. return;
  499. if (q->request_fn)
  500. elv_unregister_queue(q);
  501. kobject_uevent(&q->kobj, KOBJ_REMOVE);
  502. kobject_del(&q->kobj);
  503. blk_trace_remove_sysfs(disk_to_dev(disk));
  504. kobject_put(&disk_to_dev(disk)->kobj);
  505. }