Milan Broz | 784aae7 | 2009-01-06 03:05:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 Red Hat, Inc. All rights reserved. |
| 3 | * |
| 4 | * This file is released under the GPL. |
| 5 | */ |
| 6 | |
| 7 | #include <linux/sysfs.h> |
| 8 | #include <linux/dm-ioctl.h> |
| 9 | #include "dm.h" |
| 10 | |
| 11 | struct dm_sysfs_attr { |
| 12 | struct attribute attr; |
| 13 | ssize_t (*show)(struct mapped_device *, char *); |
Mike Snitzer | b898320 | 2015-02-27 17:58:42 -0500 | [diff] [blame] | 14 | ssize_t (*store)(struct mapped_device *, const char *, size_t count); |
Milan Broz | 784aae7 | 2009-01-06 03:05:12 +0000 | [diff] [blame] | 15 | }; |
| 16 | |
| 17 | #define DM_ATTR_RO(_name) \ |
| 18 | struct dm_sysfs_attr dm_attr_##_name = \ |
| 19 | __ATTR(_name, S_IRUGO, dm_attr_##_name##_show, NULL) |
| 20 | |
| 21 | static ssize_t dm_attr_show(struct kobject *kobj, struct attribute *attr, |
| 22 | char *page) |
| 23 | { |
| 24 | struct dm_sysfs_attr *dm_attr; |
| 25 | struct mapped_device *md; |
| 26 | ssize_t ret; |
| 27 | |
| 28 | dm_attr = container_of(attr, struct dm_sysfs_attr, attr); |
| 29 | if (!dm_attr->show) |
| 30 | return -EIO; |
| 31 | |
| 32 | md = dm_get_from_kobject(kobj); |
| 33 | if (!md) |
| 34 | return -EINVAL; |
| 35 | |
| 36 | ret = dm_attr->show(md, page); |
| 37 | dm_put(md); |
| 38 | |
| 39 | return ret; |
| 40 | } |
| 41 | |
Mike Snitzer | b898320 | 2015-02-27 17:58:42 -0500 | [diff] [blame] | 42 | #define DM_ATTR_RW(_name) \ |
| 43 | struct dm_sysfs_attr dm_attr_##_name = \ |
| 44 | __ATTR(_name, S_IRUGO | S_IWUSR, dm_attr_##_name##_show, dm_attr_##_name##_store) |
| 45 | |
| 46 | static ssize_t dm_attr_store(struct kobject *kobj, struct attribute *attr, |
| 47 | const char *page, size_t count) |
| 48 | { |
| 49 | struct dm_sysfs_attr *dm_attr; |
| 50 | struct mapped_device *md; |
| 51 | ssize_t ret; |
| 52 | |
| 53 | dm_attr = container_of(attr, struct dm_sysfs_attr, attr); |
| 54 | if (!dm_attr->store) |
| 55 | return -EIO; |
| 56 | |
| 57 | md = dm_get_from_kobject(kobj); |
| 58 | if (!md) |
| 59 | return -EINVAL; |
| 60 | |
| 61 | ret = dm_attr->store(md, page, count); |
| 62 | dm_put(md); |
| 63 | |
| 64 | return ret; |
| 65 | } |
| 66 | |
Milan Broz | 784aae7 | 2009-01-06 03:05:12 +0000 | [diff] [blame] | 67 | static ssize_t dm_attr_name_show(struct mapped_device *md, char *buf) |
| 68 | { |
| 69 | if (dm_copy_name_and_uuid(md, buf, NULL)) |
| 70 | return -EIO; |
| 71 | |
| 72 | strcat(buf, "\n"); |
| 73 | return strlen(buf); |
| 74 | } |
| 75 | |
| 76 | static ssize_t dm_attr_uuid_show(struct mapped_device *md, char *buf) |
| 77 | { |
| 78 | if (dm_copy_name_and_uuid(md, NULL, buf)) |
| 79 | return -EIO; |
| 80 | |
| 81 | strcat(buf, "\n"); |
| 82 | return strlen(buf); |
| 83 | } |
| 84 | |
Peter Rajnoha | 486d220 | 2009-06-22 10:12:29 +0100 | [diff] [blame] | 85 | static ssize_t dm_attr_suspended_show(struct mapped_device *md, char *buf) |
| 86 | { |
Kiyoshi Ueda | 4f186f8 | 2009-12-10 23:52:26 +0000 | [diff] [blame] | 87 | sprintf(buf, "%d\n", dm_suspended_md(md)); |
Peter Rajnoha | 486d220 | 2009-06-22 10:12:29 +0100 | [diff] [blame] | 88 | |
| 89 | return strlen(buf); |
| 90 | } |
| 91 | |
Mike Snitzer | 17e149b | 2015-03-11 15:01:09 -0400 | [diff] [blame] | 92 | static ssize_t dm_attr_use_blk_mq_show(struct mapped_device *md, char *buf) |
| 93 | { |
| 94 | sprintf(buf, "%d\n", dm_use_blk_mq(md)); |
| 95 | |
| 96 | return strlen(buf); |
| 97 | } |
| 98 | |
Milan Broz | 784aae7 | 2009-01-06 03:05:12 +0000 | [diff] [blame] | 99 | static DM_ATTR_RO(name); |
| 100 | static DM_ATTR_RO(uuid); |
Peter Rajnoha | 486d220 | 2009-06-22 10:12:29 +0100 | [diff] [blame] | 101 | static DM_ATTR_RO(suspended); |
Mike Snitzer | 17e149b | 2015-03-11 15:01:09 -0400 | [diff] [blame] | 102 | static DM_ATTR_RO(use_blk_mq); |
Mike Snitzer | 0ce6579 | 2015-02-26 00:50:28 -0500 | [diff] [blame] | 103 | static DM_ATTR_RW(rq_based_seq_io_merge_deadline); |
Milan Broz | 784aae7 | 2009-01-06 03:05:12 +0000 | [diff] [blame] | 104 | |
| 105 | static struct attribute *dm_attrs[] = { |
| 106 | &dm_attr_name.attr, |
| 107 | &dm_attr_uuid.attr, |
Peter Rajnoha | 486d220 | 2009-06-22 10:12:29 +0100 | [diff] [blame] | 108 | &dm_attr_suspended.attr, |
Mike Snitzer | 17e149b | 2015-03-11 15:01:09 -0400 | [diff] [blame] | 109 | &dm_attr_use_blk_mq.attr, |
Mike Snitzer | 0ce6579 | 2015-02-26 00:50:28 -0500 | [diff] [blame] | 110 | &dm_attr_rq_based_seq_io_merge_deadline.attr, |
Milan Broz | 784aae7 | 2009-01-06 03:05:12 +0000 | [diff] [blame] | 111 | NULL, |
| 112 | }; |
| 113 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 114 | static const struct sysfs_ops dm_sysfs_ops = { |
Milan Broz | 784aae7 | 2009-01-06 03:05:12 +0000 | [diff] [blame] | 115 | .show = dm_attr_show, |
Mike Snitzer | b898320 | 2015-02-27 17:58:42 -0500 | [diff] [blame] | 116 | .store = dm_attr_store, |
Milan Broz | 784aae7 | 2009-01-06 03:05:12 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
Milan Broz | 784aae7 | 2009-01-06 03:05:12 +0000 | [diff] [blame] | 119 | static struct kobj_type dm_ktype = { |
| 120 | .sysfs_ops = &dm_sysfs_ops, |
| 121 | .default_attrs = dm_attrs, |
Mikulas Patocka | be35f48 | 2014-01-06 23:01:22 -0500 | [diff] [blame] | 122 | .release = dm_kobject_release, |
Milan Broz | 784aae7 | 2009-01-06 03:05:12 +0000 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | /* |
| 126 | * Initialize kobj |
| 127 | * because nobody using md yet, no need to call explicit dm_get/put |
| 128 | */ |
| 129 | int dm_sysfs_init(struct mapped_device *md) |
| 130 | { |
| 131 | return kobject_init_and_add(dm_kobject(md), &dm_ktype, |
| 132 | &disk_to_dev(dm_disk(md))->kobj, |
| 133 | "%s", "dm"); |
| 134 | } |
| 135 | |
| 136 | /* |
| 137 | * Remove kobj, called after all references removed |
| 138 | */ |
| 139 | void dm_sysfs_exit(struct mapped_device *md) |
| 140 | { |
Mikulas Patocka | be35f48 | 2014-01-06 23:01:22 -0500 | [diff] [blame] | 141 | struct kobject *kobj = dm_kobject(md); |
| 142 | kobject_put(kobj); |
| 143 | wait_for_completion(dm_get_completion_from_kobject(kobj)); |
Milan Broz | 784aae7 | 2009-01-06 03:05:12 +0000 | [diff] [blame] | 144 | } |