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