dm-sysfs.c 3.1 KB

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