dm-sysfs.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 *, char *);
  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. static ssize_t dm_attr_name_show(struct mapped_device *md, char *buf)
  34. {
  35. if (dm_copy_name_and_uuid(md, buf, NULL))
  36. return -EIO;
  37. strcat(buf, "\n");
  38. return strlen(buf);
  39. }
  40. static ssize_t dm_attr_uuid_show(struct mapped_device *md, char *buf)
  41. {
  42. if (dm_copy_name_and_uuid(md, NULL, buf))
  43. return -EIO;
  44. strcat(buf, "\n");
  45. return strlen(buf);
  46. }
  47. static ssize_t dm_attr_suspended_show(struct mapped_device *md, char *buf)
  48. {
  49. sprintf(buf, "%d\n", dm_suspended_md(md));
  50. return strlen(buf);
  51. }
  52. static DM_ATTR_RO(name);
  53. static DM_ATTR_RO(uuid);
  54. static DM_ATTR_RO(suspended);
  55. static struct attribute *dm_attrs[] = {
  56. &dm_attr_name.attr,
  57. &dm_attr_uuid.attr,
  58. &dm_attr_suspended.attr,
  59. NULL,
  60. };
  61. static const struct sysfs_ops dm_sysfs_ops = {
  62. .show = dm_attr_show,
  63. };
  64. /*
  65. * dm kobject is embedded in mapped_device structure
  66. * no need to define release function here
  67. */
  68. static struct kobj_type dm_ktype = {
  69. .sysfs_ops = &dm_sysfs_ops,
  70. .default_attrs = dm_attrs,
  71. };
  72. /*
  73. * Initialize kobj
  74. * because nobody using md yet, no need to call explicit dm_get/put
  75. */
  76. int dm_sysfs_init(struct mapped_device *md)
  77. {
  78. return kobject_init_and_add(dm_kobject(md), &dm_ktype,
  79. &disk_to_dev(dm_disk(md))->kobj,
  80. "%s", "dm");
  81. }
  82. /*
  83. * Remove kobj, called after all references removed
  84. */
  85. void dm_sysfs_exit(struct mapped_device *md)
  86. {
  87. kobject_put(dm_kobject(md));
  88. }