blk-sysfs.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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 (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. spin_lock_irq(q->queue_lock);
  192. queue_flag_clear(QUEUE_FLAG_NOMERGES, q);
  193. queue_flag_clear(QUEUE_FLAG_NOXMERGES, q);
  194. if (nm == 2)
  195. queue_flag_set(QUEUE_FLAG_NOMERGES, q);
  196. else if (nm)
  197. queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
  198. spin_unlock_irq(q->queue_lock);
  199. return ret;
  200. }
  201. static ssize_t queue_rq_affinity_show(struct request_queue *q, char *page)
  202. {
  203. bool set = test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags);
  204. bool force = test_bit(QUEUE_FLAG_SAME_FORCE, &q->queue_flags);
  205. return queue_var_show(set << force, page);
  206. }
  207. static ssize_t
  208. queue_rq_affinity_store(struct request_queue *q, const char *page, size_t count)
  209. {
  210. ssize_t ret = -EINVAL;
  211. #if defined(CONFIG_USE_GENERIC_SMP_HELPERS)
  212. unsigned long val;
  213. ret = queue_var_store(&val, page, count);
  214. spin_lock_irq(q->queue_lock);
  215. if (val == 2) {
  216. queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
  217. queue_flag_set(QUEUE_FLAG_SAME_FORCE, q);
  218. } else if (val == 1) {
  219. queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
  220. queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
  221. } else if (val == 0) {
  222. queue_flag_clear(QUEUE_FLAG_SAME_COMP, q);
  223. queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
  224. }
  225. spin_unlock_irq(q->queue_lock);
  226. #endif
  227. return ret;
  228. }
  229. static struct queue_sysfs_entry queue_requests_entry = {
  230. .attr = {.name = "nr_requests", .mode = S_IRUGO | S_IWUSR },
  231. .show = queue_requests_show,
  232. .store = queue_requests_store,
  233. };
  234. static struct queue_sysfs_entry queue_ra_entry = {
  235. .attr = {.name = "read_ahead_kb", .mode = S_IRUGO | S_IWUSR },
  236. .show = queue_ra_show,
  237. .store = queue_ra_store,
  238. };
  239. static struct queue_sysfs_entry queue_max_sectors_entry = {
  240. .attr = {.name = "max_sectors_kb", .mode = S_IRUGO | S_IWUSR },
  241. .show = queue_max_sectors_show,
  242. .store = queue_max_sectors_store,
  243. };
  244. static struct queue_sysfs_entry queue_max_hw_sectors_entry = {
  245. .attr = {.name = "max_hw_sectors_kb", .mode = S_IRUGO },
  246. .show = queue_max_hw_sectors_show,
  247. };
  248. static struct queue_sysfs_entry queue_max_segments_entry = {
  249. .attr = {.name = "max_segments", .mode = S_IRUGO },
  250. .show = queue_max_segments_show,
  251. };
  252. static struct queue_sysfs_entry queue_max_integrity_segments_entry = {
  253. .attr = {.name = "max_integrity_segments", .mode = S_IRUGO },
  254. .show = queue_max_integrity_segments_show,
  255. };
  256. static struct queue_sysfs_entry queue_max_segment_size_entry = {
  257. .attr = {.name = "max_segment_size", .mode = S_IRUGO },
  258. .show = queue_max_segment_size_show,
  259. };
  260. static struct queue_sysfs_entry queue_iosched_entry = {
  261. .attr = {.name = "scheduler", .mode = S_IRUGO | S_IWUSR },
  262. .show = elv_iosched_show,
  263. .store = elv_iosched_store,
  264. };
  265. static struct queue_sysfs_entry queue_hw_sector_size_entry = {
  266. .attr = {.name = "hw_sector_size", .mode = S_IRUGO },
  267. .show = queue_logical_block_size_show,
  268. };
  269. static struct queue_sysfs_entry queue_logical_block_size_entry = {
  270. .attr = {.name = "logical_block_size", .mode = S_IRUGO },
  271. .show = queue_logical_block_size_show,
  272. };
  273. static struct queue_sysfs_entry queue_physical_block_size_entry = {
  274. .attr = {.name = "physical_block_size", .mode = S_IRUGO },
  275. .show = queue_physical_block_size_show,
  276. };
  277. static struct queue_sysfs_entry queue_io_min_entry = {
  278. .attr = {.name = "minimum_io_size", .mode = S_IRUGO },
  279. .show = queue_io_min_show,
  280. };
  281. static struct queue_sysfs_entry queue_io_opt_entry = {
  282. .attr = {.name = "optimal_io_size", .mode = S_IRUGO },
  283. .show = queue_io_opt_show,
  284. };
  285. static struct queue_sysfs_entry queue_discard_granularity_entry = {
  286. .attr = {.name = "discard_granularity", .mode = S_IRUGO },
  287. .show = queue_discard_granularity_show,
  288. };
  289. static struct queue_sysfs_entry queue_discard_max_entry = {
  290. .attr = {.name = "discard_max_bytes", .mode = S_IRUGO },
  291. .show = queue_discard_max_show,
  292. };
  293. static struct queue_sysfs_entry queue_discard_zeroes_data_entry = {
  294. .attr = {.name = "discard_zeroes_data", .mode = S_IRUGO },
  295. .show = queue_discard_zeroes_data_show,
  296. };
  297. static struct queue_sysfs_entry queue_nonrot_entry = {
  298. .attr = {.name = "rotational", .mode = S_IRUGO | S_IWUSR },
  299. .show = queue_show_nonrot,
  300. .store = queue_store_nonrot,
  301. };
  302. static struct queue_sysfs_entry queue_nomerges_entry = {
  303. .attr = {.name = "nomerges", .mode = S_IRUGO | S_IWUSR },
  304. .show = queue_nomerges_show,
  305. .store = queue_nomerges_store,
  306. };
  307. static struct queue_sysfs_entry queue_rq_affinity_entry = {
  308. .attr = {.name = "rq_affinity", .mode = S_IRUGO | S_IWUSR },
  309. .show = queue_rq_affinity_show,
  310. .store = queue_rq_affinity_store,
  311. };
  312. static struct queue_sysfs_entry queue_iostats_entry = {
  313. .attr = {.name = "iostats", .mode = S_IRUGO | S_IWUSR },
  314. .show = queue_show_iostats,
  315. .store = queue_store_iostats,
  316. };
  317. static struct queue_sysfs_entry queue_random_entry = {
  318. .attr = {.name = "add_random", .mode = S_IRUGO | S_IWUSR },
  319. .show = queue_show_random,
  320. .store = queue_store_random,
  321. };
  322. static struct attribute *default_attrs[] = {
  323. &queue_requests_entry.attr,
  324. &queue_ra_entry.attr,
  325. &queue_max_hw_sectors_entry.attr,
  326. &queue_max_sectors_entry.attr,
  327. &queue_max_segments_entry.attr,
  328. &queue_max_integrity_segments_entry.attr,
  329. &queue_max_segment_size_entry.attr,
  330. &queue_iosched_entry.attr,
  331. &queue_hw_sector_size_entry.attr,
  332. &queue_logical_block_size_entry.attr,
  333. &queue_physical_block_size_entry.attr,
  334. &queue_io_min_entry.attr,
  335. &queue_io_opt_entry.attr,
  336. &queue_discard_granularity_entry.attr,
  337. &queue_discard_max_entry.attr,
  338. &queue_discard_zeroes_data_entry.attr,
  339. &queue_nonrot_entry.attr,
  340. &queue_nomerges_entry.attr,
  341. &queue_rq_affinity_entry.attr,
  342. &queue_iostats_entry.attr,
  343. &queue_random_entry.attr,
  344. NULL,
  345. };
  346. #define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
  347. static ssize_t
  348. queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
  349. {
  350. struct queue_sysfs_entry *entry = to_queue(attr);
  351. struct request_queue *q =
  352. container_of(kobj, struct request_queue, kobj);
  353. ssize_t res;
  354. if (!entry->show)
  355. return -EIO;
  356. mutex_lock(&q->sysfs_lock);
  357. if (blk_queue_dead(q)) {
  358. mutex_unlock(&q->sysfs_lock);
  359. return -ENOENT;
  360. }
  361. res = entry->show(q, page);
  362. mutex_unlock(&q->sysfs_lock);
  363. return res;
  364. }
  365. static ssize_t
  366. queue_attr_store(struct kobject *kobj, struct attribute *attr,
  367. const char *page, size_t length)
  368. {
  369. struct queue_sysfs_entry *entry = to_queue(attr);
  370. struct request_queue *q;
  371. ssize_t res;
  372. if (!entry->store)
  373. return -EIO;
  374. q = container_of(kobj, struct request_queue, kobj);
  375. mutex_lock(&q->sysfs_lock);
  376. if (blk_queue_dead(q)) {
  377. mutex_unlock(&q->sysfs_lock);
  378. return -ENOENT;
  379. }
  380. res = entry->store(q, page, length);
  381. mutex_unlock(&q->sysfs_lock);
  382. return res;
  383. }
  384. /**
  385. * blk_release_queue: - release a &struct request_queue when it is no longer needed
  386. * @kobj: the kobj belonging to the request queue to be released
  387. *
  388. * Description:
  389. * blk_release_queue is the pair to blk_init_queue() or
  390. * blk_queue_make_request(). It should be called when a request queue is
  391. * being released; typically when a block device is being de-registered.
  392. * Currently, its primary task it to free all the &struct request
  393. * structures that were allocated to the queue and the queue itself.
  394. *
  395. * Caveat:
  396. * Hopefully the low level driver will have finished any
  397. * outstanding requests first...
  398. **/
  399. static void blk_release_queue(struct kobject *kobj)
  400. {
  401. struct request_queue *q =
  402. container_of(kobj, struct request_queue, kobj);
  403. struct request_list *rl = &q->rq;
  404. blk_sync_queue(q);
  405. if (q->elevator) {
  406. spin_lock_irq(q->queue_lock);
  407. ioc_clear_queue(q);
  408. spin_unlock_irq(q->queue_lock);
  409. elevator_exit(q->elevator);
  410. }
  411. blk_throtl_exit(q);
  412. if (rl->rq_pool)
  413. mempool_destroy(rl->rq_pool);
  414. if (q->queue_tags)
  415. __blk_queue_free_tags(q);
  416. blk_throtl_release(q);
  417. blk_trace_shutdown(q);
  418. bdi_destroy(&q->backing_dev_info);
  419. ida_simple_remove(&blk_queue_ida, q->id);
  420. kmem_cache_free(blk_requestq_cachep, q);
  421. }
  422. static const struct sysfs_ops queue_sysfs_ops = {
  423. .show = queue_attr_show,
  424. .store = queue_attr_store,
  425. };
  426. struct kobj_type blk_queue_ktype = {
  427. .sysfs_ops = &queue_sysfs_ops,
  428. .default_attrs = default_attrs,
  429. .release = blk_release_queue,
  430. };
  431. int blk_register_queue(struct gendisk *disk)
  432. {
  433. int ret;
  434. struct device *dev = disk_to_dev(disk);
  435. struct request_queue *q = disk->queue;
  436. if (WARN_ON(!q))
  437. return -ENXIO;
  438. ret = blk_trace_init_sysfs(dev);
  439. if (ret)
  440. return ret;
  441. ret = kobject_add(&q->kobj, kobject_get(&dev->kobj), "%s", "queue");
  442. if (ret < 0) {
  443. blk_trace_remove_sysfs(dev);
  444. return ret;
  445. }
  446. kobject_uevent(&q->kobj, KOBJ_ADD);
  447. if (!q->request_fn)
  448. return 0;
  449. ret = elv_register_queue(q);
  450. if (ret) {
  451. kobject_uevent(&q->kobj, KOBJ_REMOVE);
  452. kobject_del(&q->kobj);
  453. blk_trace_remove_sysfs(dev);
  454. kobject_put(&dev->kobj);
  455. return ret;
  456. }
  457. return 0;
  458. }
  459. void blk_unregister_queue(struct gendisk *disk)
  460. {
  461. struct request_queue *q = disk->queue;
  462. if (WARN_ON(!q))
  463. return;
  464. if (q->request_fn)
  465. elv_unregister_queue(q);
  466. kobject_uevent(&q->kobj, KOBJ_REMOVE);
  467. kobject_del(&q->kobj);
  468. blk_trace_remove_sysfs(disk_to_dev(disk));
  469. kobject_put(&disk_to_dev(disk)->kobj);
  470. }