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