blk-sysfs.c 14 KB

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