dm-sysfs.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (C) 2008 Red Hat, Inc. All rights reserved.
  3. *
  4. * This file is released under the GPL.
  5. */
  6. #include <linux/sysfs.h>
  7. #include <linux/dm-ioctl.h>
  8. #include "dm-core.h"
  9. #include "dm-rq.h"
  10. struct dm_sysfs_attr {
  11. struct attribute attr;
  12. ssize_t (*show)(struct mapped_device *, char *);
  13. ssize_t (*store)(struct mapped_device *, const char *, size_t count);
  14. };
  15. #define DM_ATTR_RO(_name) \
  16. struct dm_sysfs_attr dm_attr_##_name = \
  17. __ATTR(_name, S_IRUGO, dm_attr_##_name##_show, NULL)
  18. static ssize_t dm_attr_show(struct kobject *kobj, struct attribute *attr,
  19. char *page)
  20. {
  21. struct dm_sysfs_attr *dm_attr;
  22. struct mapped_device *md;
  23. ssize_t ret;
  24. dm_attr = container_of(attr, struct dm_sysfs_attr, attr);
  25. if (!dm_attr->show)
  26. return -EIO;
  27. md = dm_get_from_kobject(kobj);
  28. if (!md)
  29. return -EINVAL;
  30. ret = dm_attr->show(md, page);
  31. dm_put(md);
  32. return ret;
  33. }
  34. #define DM_ATTR_RW(_name) \
  35. struct dm_sysfs_attr dm_attr_##_name = \
  36. __ATTR(_name, S_IRUGO | S_IWUSR, dm_attr_##_name##_show, dm_attr_##_name##_store)
  37. static ssize_t dm_attr_store(struct kobject *kobj, struct attribute *attr,
  38. const char *page, size_t count)
  39. {
  40. struct dm_sysfs_attr *dm_attr;
  41. struct mapped_device *md;
  42. ssize_t ret;
  43. dm_attr = container_of(attr, struct dm_sysfs_attr, attr);
  44. if (!dm_attr->store)
  45. return -EIO;
  46. md = dm_get_from_kobject(kobj);
  47. if (!md)
  48. return -EINVAL;
  49. ret = dm_attr->store(md, page, count);
  50. dm_put(md);
  51. return ret;
  52. }
  53. static ssize_t dm_attr_name_show(struct mapped_device *md, char *buf)
  54. {
  55. if (dm_copy_name_and_uuid(md, buf, NULL))
  56. return -EIO;
  57. strcat(buf, "\n");
  58. return strlen(buf);
  59. }
  60. static ssize_t dm_attr_uuid_show(struct mapped_device *md, char *buf)
  61. {
  62. if (dm_copy_name_and_uuid(md, NULL, buf))
  63. return -EIO;
  64. strcat(buf, "\n");
  65. return strlen(buf);
  66. }
  67. static ssize_t dm_attr_suspended_show(struct mapped_device *md, char *buf)
  68. {
  69. sprintf(buf, "%d\n", dm_suspended_md(md));
  70. return strlen(buf);
  71. }
  72. static ssize_t dm_attr_use_blk_mq_show(struct mapped_device *md, char *buf)
  73. {
  74. /* Purely for userspace compatibility */
  75. sprintf(buf, "%d\n", true);
  76. return strlen(buf);
  77. }
  78. static DM_ATTR_RO(name);
  79. static DM_ATTR_RO(uuid);
  80. static DM_ATTR_RO(suspended);
  81. static DM_ATTR_RO(use_blk_mq);
  82. static DM_ATTR_RW(rq_based_seq_io_merge_deadline);
  83. static struct attribute *dm_attrs[] = {
  84. &dm_attr_name.attr,
  85. &dm_attr_uuid.attr,
  86. &dm_attr_suspended.attr,
  87. &dm_attr_use_blk_mq.attr,
  88. &dm_attr_rq_based_seq_io_merge_deadline.attr,
  89. NULL,
  90. };
  91. static const struct sysfs_ops dm_sysfs_ops = {
  92. .show = dm_attr_show,
  93. .store = dm_attr_store,
  94. };
  95. static struct kobj_type dm_ktype = {
  96. .sysfs_ops = &dm_sysfs_ops,
  97. .default_attrs = dm_attrs,
  98. .release = dm_kobject_release,
  99. };
  100. /*
  101. * Initialize kobj
  102. * because nobody using md yet, no need to call explicit dm_get/put
  103. */
  104. int dm_sysfs_init(struct mapped_device *md)
  105. {
  106. return kobject_init_and_add(dm_kobject(md), &dm_ktype,
  107. &disk_to_dev(dm_disk(md))->kobj,
  108. "%s", "dm");
  109. }
  110. /*
  111. * Remove kobj, called after all references removed
  112. */
  113. void dm_sysfs_exit(struct mapped_device *md)
  114. {
  115. struct kobject *kobj = dm_kobject(md);
  116. kobject_put(kobj);
  117. wait_for_completion(dm_get_completion_from_kobject(kobj));
  118. }